readelf.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #ifdef BUILTIN_ELF
  2. #include <sys/types.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #ifdef HAVE_CONFIG_H
  10. #include "config.h"
  11. #endif
  12. #include "readelf.h"
  13. #include "file.h"
  14. static void
  15. doshn(fd, off, num, size, buf)
  16. int fd;
  17. off_t off;
  18. int num;
  19. size_t size;
  20. char *buf;
  21. {
  22. /*
  23. * This works for both 32-bit and 64-bit ELF formats,
  24. * because it looks only at the "sh_type" field, which is
  25. * always 32 bits, and is preceded only by the "sh_name"
  26. * field which is also always 32 bits, and because it uses
  27. * the shdr size from the ELF header rather than using
  28. * the size of an "Elf32_Shdr".
  29. */
  30. Elf32_Shdr *sh = (Elf32_Shdr *) buf;
  31. if (lseek(fd, off, SEEK_SET) == -1)
  32. error("lseek failed (%s).\n", strerror(errno));
  33. for ( ; num; num--) {
  34. if (read(fd, buf, size) == -1)
  35. error("read failed (%s).\n", strerror(errno));
  36. if (sh->sh_type == SHT_SYMTAB) {
  37. (void) printf (", not stripped");
  38. return;
  39. }
  40. }
  41. (void) printf (", stripped");
  42. }
  43. /*
  44. * Look through the program headers of an executable image, searching
  45. * for a PT_INTERP section; if one is found, it's dynamically linked,
  46. * otherwise it's statically linked.
  47. */
  48. static void
  49. dophn_exec(fd, off, num, size, buf)
  50. int fd;
  51. off_t off;
  52. int num;
  53. size_t size;
  54. char *buf;
  55. {
  56. /* I am not sure if this works for 64 bit elf formats */
  57. Elf32_Phdr *ph = (Elf32_Phdr *) buf;
  58. if (lseek(fd, off, SEEK_SET) == -1)
  59. error("lseek failed (%s).\n", strerror(errno));
  60. for ( ; num; num--) {
  61. if (read(fd, buf, size) == -1)
  62. error("read failed (%s).\n", strerror(errno));
  63. if (ph->p_type == PT_INTERP) {
  64. /*
  65. * Has an interpreter - must be a dynamically-linked
  66. * executable.
  67. */
  68. printf(", dynamically linked");
  69. return;
  70. }
  71. }
  72. printf(", statically linked");
  73. }
  74. size_t prpsoffsets[] = {
  75. 84, /* SunOS 5.x */
  76. 32, /* Linux */
  77. };
  78. #define NOFFSETS (sizeof prpsoffsets / sizeof prpsoffsets[0])
  79. /*
  80. * Look through the program headers of an executable image, searching
  81. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE"; if one
  82. * is found, try looking in various places in its contents for a 16-character
  83. * string containing only printable characters - if found, that string
  84. * should be the name of the program that dropped core.
  85. * Note: right after that 16-character string is, at least in SunOS 5.x
  86. * (and possibly other SVR4-flavored systems) and Linux, a longer string
  87. * (80 characters, in 5.x, probably other SVR4-flavored systems, and Linux)
  88. * containing the start of the command line for that program.
  89. */
  90. static void
  91. dophn_core(fd, off, num, size, buf)
  92. int fd;
  93. off_t off;
  94. int num;
  95. size_t size;
  96. char *buf;
  97. {
  98. /*
  99. * This doesn't work for 64-bit ELF, as the "p_offset" field is
  100. * 64 bits in 64-bit ELF.
  101. */
  102. Elf32_Phdr *ph = (Elf32_Phdr *) buf;
  103. Elf32_Nhdr *nh;
  104. size_t offset, noffset, reloffset;
  105. unsigned char c;
  106. int i, j;
  107. char nbuf[BUFSIZ];
  108. int bufsize;
  109. for ( ; num; num--) {
  110. if (lseek(fd, off, SEEK_SET) == -1)
  111. error("lseek failed (%s).\n", strerror(errno));
  112. if (read(fd, buf, size) == -1)
  113. error("read failed (%s).\n", strerror(errno));
  114. off += size;
  115. if (ph->p_type != PT_NOTE)
  116. continue;
  117. if (lseek(fd, ph->p_offset, SEEK_SET) == -1)
  118. error("lseek failed (%s).\n", strerror(errno));
  119. bufsize = read(fd, nbuf, BUFSIZ);
  120. if (bufsize == -1)
  121. error("read failed (%s).\n", strerror(errno));
  122. offset = 0;
  123. for (;;) {
  124. if (offset >= bufsize)
  125. break;
  126. nh = (Elf32_Nhdr *)&nbuf[offset];
  127. offset += sizeof *nh;
  128. /*
  129. * If this note isn't an NT_PRPSINFO note, it's
  130. * not what we're looking for.
  131. */
  132. if (nh->n_type != NT_PRPSINFO) {
  133. offset += nh->n_namesz;
  134. offset = ((offset + 3)/4)*4;
  135. offset += nh->n_descsz;
  136. offset = ((offset + 3)/4)*4;
  137. continue;
  138. }
  139. /*
  140. * Make sure this note has the name "CORE".
  141. */
  142. if (offset + nh->n_namesz >= bufsize) {
  143. /*
  144. * We're past the end of the buffer.
  145. */
  146. break;
  147. }
  148. if (nh->n_namesz != 5
  149. || strcmp(&nbuf[offset], "CORE") != 0)
  150. continue;
  151. offset += nh->n_namesz;
  152. offset = ((offset + 3)/4)*4;
  153. /*
  154. * Extract the program name. We assume it to be
  155. * 16 characters (that's what it is in SunOS 5.x
  156. * and Linux).
  157. *
  158. * Unfortunately, it's at a different offset in
  159. * SunOS 5.x and Linux, so try multiple offsets.
  160. * If the characters aren't all printable, reject
  161. * it.
  162. */
  163. for (i = 0; i < NOFFSETS; i++) {
  164. reloffset = prpsoffsets[i];
  165. noffset = offset + reloffset;
  166. for (j = 0; j < 16;
  167. j++, noffset++, reloffset++) {
  168. /*
  169. * Make sure we're not past the end
  170. * of the buffer; if we are, just
  171. * give up.
  172. */
  173. if (noffset >= bufsize)
  174. return;
  175. /*
  176. * Make sure we're not past the
  177. * end of the contents; if we
  178. * are, this obviously isn't
  179. * the right offset.
  180. */
  181. if (reloffset >= nh->n_descsz)
  182. goto tryanother;
  183. c = nbuf[noffset];
  184. if (c != '\0' && !isprint(c))
  185. goto tryanother;
  186. }
  187. /*
  188. * Well, that worked.
  189. */
  190. printf(", from '%.16s'",
  191. &nbuf[offset + prpsoffsets[i]]);
  192. return;
  193. tryanother:
  194. ;
  195. }
  196. offset += nh->n_descsz;
  197. offset = ((offset + 3)/4)*4;
  198. }
  199. }
  200. }
  201. void
  202. tryelf(fd, buf, nbytes)
  203. int fd;
  204. char *buf;
  205. int nbytes;
  206. {
  207. union {
  208. int32 l;
  209. char c[sizeof (int32)];
  210. } u;
  211. /*
  212. * ELF executables have multiple section headers in arbitrary
  213. * file locations and thus file(1) cannot determine it from easily.
  214. * Instead we traverse thru all section headers until a symbol table
  215. * one is found or else the binary is stripped.
  216. */
  217. if (buf[EI_MAG0] != ELFMAG0 || buf[EI_MAG1] != ELFMAG1
  218. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  219. return;
  220. if (buf[4] == ELFCLASS32) {
  221. Elf32_Ehdr elfhdr;
  222. if (nbytes <= sizeof (Elf32_Ehdr))
  223. return;
  224. u.l = 1;
  225. (void) memcpy(&elfhdr, buf, sizeof elfhdr);
  226. /*
  227. * If the system byteorder does not equal the
  228. * object byteorder then don't test.
  229. * XXX - we could conceivably fix up the "dophn_XXX()" and
  230. * "doshn()" routines to extract stuff in the right
  231. * byte order....
  232. */
  233. if ((u.c[sizeof(long) - 1] + 1) == elfhdr.e_ident[5]) {
  234. if (elfhdr.e_type == ET_CORE)
  235. dophn_core(fd, elfhdr.e_phoff, elfhdr.e_phnum,
  236. elfhdr.e_phentsize, buf);
  237. else {
  238. if (elfhdr.e_type == ET_EXEC) {
  239. dophn_exec(fd, elfhdr.e_phoff,
  240. elfhdr.e_phnum,
  241. elfhdr.e_phentsize, buf);
  242. }
  243. doshn(fd, elfhdr.e_shoff, elfhdr.e_shnum,
  244. elfhdr.e_shentsize, buf);
  245. }
  246. }
  247. return;
  248. }
  249. if (buf[4] == ELFCLASS64) {
  250. Elf64_Ehdr elfhdr;
  251. if (nbytes <= sizeof (Elf64_Ehdr))
  252. return;
  253. u.l = 1;
  254. (void) memcpy(&elfhdr, buf, sizeof elfhdr);
  255. /*
  256. * If the system byteorder does not equal the
  257. * object byteorder then don't test.
  258. * XXX - we could conceivably fix up the "dophn_XXX()" and
  259. * "doshn()" routines to extract stuff in the right
  260. * byte order....
  261. */
  262. if ((u.c[sizeof(long) - 1] + 1) == elfhdr.e_ident[5]) {
  263. #ifdef notyet
  264. if (elfhdr.e_type == ET_CORE)
  265. dophn_core(fd, elfhdr.e_phoff[1],
  266. elfhdr.e_phnum,
  267. elfhdr.e_phentsize, buf);
  268. else
  269. #endif
  270. {
  271. #ifdef notyet
  272. if (elfhdr.e_type == ET_EXEC) {
  273. dophn_exec(fd, elfhdr.e_phoff[1],
  274. elfhdr.e_phnum,
  275. elfhdr.e_phentsize, buf);
  276. }
  277. #endif
  278. doshn(fd, elfhdr.e_shoff[1],
  279. elfhdr.e_shnum,
  280. elfhdr.e_shentsize, buf);
  281. }
  282. }
  283. return;
  284. }
  285. }
  286. #endif