ksyms.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* $OpenBSD: ksyms.c,v 1.28 2015/03/14 03:38:46 jsg Exp $ */
  2. /*
  3. * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
  4. * Copyright (c) 2001 Artur Grabowski <art@openbsd.org>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  18. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  19. * THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <sys/param.h>
  28. #include <sys/exec.h>
  29. #include <sys/systm.h>
  30. #include <sys/uio.h>
  31. #include <sys/malloc.h>
  32. #include <sys/fcntl.h>
  33. #include <sys/conf.h>
  34. #ifdef _NLIST_DO_ELF
  35. #include <sys/exec_elf.h>
  36. #endif
  37. extern char *esym; /* end of symbol table */
  38. #if defined(__sparc64__) || defined(__mips__) || defined(__amd64__)
  39. extern char *ssym; /* end of kernel */
  40. #else
  41. extern long end; /* end of kernel */
  42. #endif
  43. static caddr_t ksym_head;
  44. static caddr_t ksym_syms;
  45. static size_t ksym_head_size;
  46. static size_t ksym_syms_size;
  47. void ksymsattach(int);
  48. /*
  49. * We assume __LDPGSZ is a multiple of PAGE_SIZE (it is)
  50. */
  51. /*ARGSUSED*/
  52. void
  53. ksymsattach(int num)
  54. {
  55. #if defined(__sparc64__) || defined(__mips__) || defined(__amd64__)
  56. if (esym <= ssym) {
  57. printf("/dev/ksyms: Symbol table not valid.\n");
  58. return;
  59. }
  60. #else
  61. if (esym <= (char *)&end) {
  62. printf("/dev/ksyms: Symbol table not valid.\n");
  63. return;
  64. }
  65. #endif
  66. #ifdef _NLIST_DO_ELF
  67. do {
  68. #if defined(__sparc64__) || defined(__mips__) || defined(__amd64__)
  69. caddr_t symtab = ssym;
  70. #else
  71. caddr_t symtab = (caddr_t)&end;
  72. #endif
  73. Elf_Ehdr *elf;
  74. Elf_Shdr *shdr;
  75. int i;
  76. elf = (Elf_Ehdr *)symtab;
  77. if (memcmp(elf->e_ident, ELFMAG, SELFMAG) != 0 ||
  78. elf->e_ident[EI_CLASS] != ELFCLASS ||
  79. elf->e_machine != ELF_TARG_MACH)
  80. break;
  81. shdr = (Elf_Shdr *)&symtab[elf->e_shoff];
  82. for (i = 0; i < elf->e_shnum; i++) {
  83. if (shdr[i].sh_type == SHT_SYMTAB) {
  84. break;
  85. }
  86. }
  87. /*
  88. * No symbol table found.
  89. */
  90. if (i == elf->e_shnum)
  91. break;
  92. /*
  93. * No additional header.
  94. */
  95. ksym_head_size = 0;
  96. ksym_syms = symtab;
  97. ksym_syms_size = (size_t)(esym - symtab);
  98. return;
  99. } while (0);
  100. #endif
  101. }
  102. /*ARGSUSED*/
  103. int
  104. ksymsopen(dev_t dev, int flag, int mode, struct proc *p)
  105. {
  106. /* There are no non-zero minor devices */
  107. if (minor(dev) != 0)
  108. return (ENXIO);
  109. /* This device is read-only */
  110. if ((flag & FWRITE))
  111. return (EPERM);
  112. /* ksym_syms must be initialized */
  113. if (ksym_syms == NULL)
  114. return (ENXIO);
  115. return (0);
  116. }
  117. /*ARGSUSED*/
  118. int
  119. ksymsclose(dev_t dev, int flag, int mode, struct proc *p)
  120. {
  121. return (0);
  122. }
  123. /*ARGSUSED*/
  124. int
  125. ksymsread(dev_t dev, struct uio *uio, int flags)
  126. {
  127. int error;
  128. size_t len;
  129. caddr_t v;
  130. size_t off;
  131. if (uio->uio_offset < 0)
  132. return (EINVAL);
  133. while (uio->uio_resid > 0) {
  134. if (uio->uio_offset >= ksym_head_size + ksym_syms_size)
  135. break;
  136. if (uio->uio_offset < ksym_head_size) {
  137. v = ksym_head + uio->uio_offset;
  138. len = ksym_head_size - uio->uio_offset;
  139. } else {
  140. off = uio->uio_offset - ksym_head_size;
  141. v = ksym_syms + off;
  142. len = ksym_syms_size - off;
  143. }
  144. if (len > uio->uio_resid)
  145. len = uio->uio_resid;
  146. if ((error = uiomovei(v, len, uio)) != 0)
  147. return (error);
  148. }
  149. return (0);
  150. }