readelf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. #include "file.h"
  2. #ifdef BUILTIN_ELF
  3. #include <sys/types.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8. #ifdef HAVE_UNISTD_H
  9. #include <unistd.h>
  10. #endif
  11. #include <errno.h>
  12. #include "readelf.h"
  13. #ifndef lint
  14. FILE_RCSID("@(#)$Id: readelf.c,v 1.17 2000/08/05 19:00:12 christos Exp $")
  15. #endif
  16. #ifdef ELFCORE
  17. static void dophn_core __P((int, int, int, off_t, int, size_t));
  18. #endif
  19. static void dophn_exec __P((int, int, int, off_t, int, size_t));
  20. static void doshn __P((int, int, int, off_t, int, size_t));
  21. static uint16_t getu16 __P((int, int));
  22. static uint32_t getu32 __P((int, uint32_t));
  23. static uint64_t getu64 __P((int, uint64_t));
  24. static uint16_t
  25. getu16(swap, value)
  26. int swap;
  27. uint16_t value;
  28. {
  29. union {
  30. uint16_t ui;
  31. char c[2];
  32. } retval, tmpval;
  33. if (swap) {
  34. tmpval.ui = value;
  35. retval.c[0] = tmpval.c[1];
  36. retval.c[1] = tmpval.c[0];
  37. return retval.ui;
  38. } else
  39. return value;
  40. }
  41. static uint32_t
  42. getu32(swap, value)
  43. int swap;
  44. uint32_t value;
  45. {
  46. union {
  47. uint32_t ui;
  48. char c[4];
  49. } retval, tmpval;
  50. if (swap) {
  51. tmpval.ui = value;
  52. retval.c[0] = tmpval.c[3];
  53. retval.c[1] = tmpval.c[2];
  54. retval.c[2] = tmpval.c[1];
  55. retval.c[3] = tmpval.c[0];
  56. return retval.ui;
  57. } else
  58. return value;
  59. }
  60. static uint64_t
  61. getu64(swap, value)
  62. int swap;
  63. uint64_t value;
  64. {
  65. union {
  66. uint64_t ui;
  67. char c[8];
  68. } retval, tmpval;
  69. if (swap) {
  70. tmpval.ui = value;
  71. retval.c[0] = tmpval.c[7];
  72. retval.c[1] = tmpval.c[6];
  73. retval.c[2] = tmpval.c[5];
  74. retval.c[3] = tmpval.c[4];
  75. retval.c[4] = tmpval.c[3];
  76. retval.c[5] = tmpval.c[2];
  77. retval.c[6] = tmpval.c[1];
  78. retval.c[7] = tmpval.c[0];
  79. return retval.ui;
  80. } else
  81. return value;
  82. }
  83. #define sh_addr (class == ELFCLASS32 \
  84. ? (void *) &sh32 \
  85. : (void *) &sh64)
  86. #define shs_type (class == ELFCLASS32 \
  87. ? getu32(swap, sh32.sh_type) \
  88. : getu32(swap, sh64.sh_type))
  89. #define ph_addr (class == ELFCLASS32 \
  90. ? (void *) &ph32 \
  91. : (void *) &ph64)
  92. #define ph_type (class == ELFCLASS32 \
  93. ? getu32(swap, ph32.p_type) \
  94. : getu32(swap, ph64.p_type))
  95. #define ph_offset (class == ELFCLASS32 \
  96. ? getu32(swap, ph32.p_offset) \
  97. : getu64(swap, ph64.p_offset))
  98. #define nh_size (class == ELFCLASS32 \
  99. ? sizeof *nh32 \
  100. : sizeof *nh64)
  101. #define nh_type (class == ELFCLASS32 \
  102. ? getu32(swap, nh32->n_type) \
  103. : getu32(swap, nh64->n_type))
  104. #define nh_namesz (class == ELFCLASS32 \
  105. ? getu32(swap, nh32->n_namesz) \
  106. : getu32(swap, nh64->n_namesz))
  107. #define nh_descsz (class == ELFCLASS32 \
  108. ? getu32(swap, nh32->n_descsz) \
  109. : getu32(swap, nh64->n_descsz))
  110. #define prpsoffsets(i) (class == ELFCLASS32 \
  111. ? prpsoffsets32[i] \
  112. : prpsoffsets64[i])
  113. static void
  114. doshn(class, swap, fd, off, num, size)
  115. int class;
  116. int swap;
  117. int fd;
  118. off_t off;
  119. int num;
  120. size_t size;
  121. {
  122. Elf32_Shdr sh32;
  123. Elf64_Shdr sh64;
  124. if (lseek(fd, off, SEEK_SET) == -1)
  125. error("lseek failed (%s).\n", strerror(errno));
  126. for ( ; num; num--) {
  127. if (read(fd, sh_addr, size) == -1)
  128. error("read failed (%s).\n", strerror(errno));
  129. if (shs_type == SHT_SYMTAB /* || shs_type == SHT_DYNSYM */) {
  130. (void) printf (", not stripped");
  131. return;
  132. }
  133. }
  134. (void) printf (", stripped");
  135. }
  136. /*
  137. * Look through the program headers of an executable image, searching
  138. * for a PT_INTERP section; if one is found, it's dynamically linked,
  139. * otherwise it's statically linked.
  140. */
  141. static void
  142. dophn_exec(class, swap, fd, off, num, size)
  143. int class;
  144. int swap;
  145. int fd;
  146. off_t off;
  147. int num;
  148. size_t size;
  149. {
  150. Elf32_Phdr ph32;
  151. Elf64_Phdr ph64;
  152. char *linking_style = "statically";
  153. char *shared_libraries = "";
  154. if (lseek(fd, off, SEEK_SET) == -1)
  155. error("lseek failed (%s).\n", strerror(errno));
  156. for ( ; num; num--) {
  157. if (read(fd, ph_addr, size) == -1)
  158. error("read failed (%s).\n", strerror(errno));
  159. switch (ph_type) {
  160. case PT_DYNAMIC:
  161. linking_style = "dynamically";
  162. break;
  163. case PT_INTERP:
  164. shared_libraries = " (uses shared libs)";
  165. break;
  166. }
  167. }
  168. printf(", %s linked%s", linking_style, shared_libraries);
  169. }
  170. #ifdef ELFCORE
  171. size_t prpsoffsets32[] = {
  172. 8, /* FreeBSD */
  173. 28, /* Linux 2.0.36 */
  174. 32, /* Linux (I forget which kernel version) */
  175. 84, /* SunOS 5.x */
  176. };
  177. size_t prpsoffsets64[] = {
  178. 120, /* SunOS 5.x, 64-bit */
  179. };
  180. #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
  181. #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
  182. #define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
  183. /*
  184. * Look through the program headers of an executable image, searching
  185. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
  186. * "FreeBSD"; if one is found, try looking in various places in its
  187. * contents for a 16-character string containing only printable
  188. * characters - if found, that string should be the name of the program
  189. * that dropped core. Note: right after that 16-character string is,
  190. * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
  191. * Linux, a longer string (80 characters, in 5.x, probably other
  192. * SVR4-flavored systems, and Linux) containing the start of the
  193. * command line for that program.
  194. *
  195. * The signal number probably appears in a section of type NT_PRSTATUS,
  196. * but that's also rather OS-dependent, in ways that are harder to
  197. * dissect with heuristics, so I'm not bothering with the signal number.
  198. * (I suppose the signal number could be of interest in situations where
  199. * you don't have the binary of the program that dropped core; if you
  200. * *do* have that binary, the debugger will probably tell you what
  201. * signal it was.)
  202. */
  203. static void
  204. dophn_core(class, swap, fd, off, num, size)
  205. int class;
  206. int swap;
  207. int fd;
  208. off_t off;
  209. int num;
  210. size_t size;
  211. {
  212. Elf32_Phdr ph32;
  213. Elf32_Nhdr *nh32;
  214. Elf64_Phdr ph64;
  215. Elf64_Nhdr *nh64;
  216. size_t offset, nameoffset, noffset, reloffset;
  217. unsigned char c;
  218. int i, j;
  219. char nbuf[BUFSIZ];
  220. int bufsize;
  221. int is_freebsd;
  222. /*
  223. * Loop through all the program headers.
  224. */
  225. for ( ; num; num--) {
  226. if (lseek(fd, off, SEEK_SET) == -1)
  227. error("lseek failed (%s).\n", strerror(errno));
  228. if (read(fd, ph_addr, size) == -1)
  229. error("read failed (%s).\n", strerror(errno));
  230. off += size;
  231. if (ph_type != PT_NOTE)
  232. continue;
  233. /*
  234. * This is a PT_NOTE section; loop through all the notes
  235. * in the section.
  236. */
  237. if (lseek(fd, (off_t) ph_offset, SEEK_SET) == -1)
  238. error("lseek failed (%s).\n", strerror(errno));
  239. bufsize = read(fd, nbuf, BUFSIZ);
  240. if (bufsize == -1)
  241. error(": " "read failed (%s).\n", strerror(errno));
  242. offset = 0;
  243. for (;;) {
  244. if (offset >= bufsize)
  245. break;
  246. if (class == ELFCLASS32)
  247. nh32 = (Elf32_Nhdr *)&nbuf[offset];
  248. else
  249. nh64 = (Elf64_Nhdr *)&nbuf[offset];
  250. offset += nh_size;
  251. /*
  252. * Check whether this note has the name "CORE" or
  253. * "FreeBSD".
  254. */
  255. if (offset + nh_namesz >= bufsize) {
  256. /*
  257. * We're past the end of the buffer.
  258. */
  259. break;
  260. }
  261. nameoffset = offset;
  262. offset += nh_namesz;
  263. offset = ((offset + 3)/4)*4;
  264. /*
  265. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  266. * least, doesn't correctly implement name
  267. * sections, in core dumps, as specified by
  268. * the "Program Linking" section of "UNIX(R) System
  269. * V Release 4 Programmer's Guide: ANSI C and
  270. * Programming Support Tools", because my copy
  271. * clearly says "The first 'namesz' bytes in 'name'
  272. * contain a *null-terminated* [emphasis mine]
  273. * character representation of the entry's owner
  274. * or originator", but the 2.0.36 kernel code
  275. * doesn't include the terminating null in the
  276. * name....
  277. */
  278. if ((nh_namesz == 4 &&
  279. strncmp(&nbuf[nameoffset], "CORE", 4) == 0) ||
  280. (nh_namesz == 5 &&
  281. strcmp(&nbuf[nameoffset], "CORE") == 0))
  282. is_freebsd = 0;
  283. else if ((nh_namesz == 8 &&
  284. strcmp(&nbuf[nameoffset], "FreeBSD") == 0))
  285. is_freebsd = 1;
  286. else
  287. continue;
  288. if (nh_type == NT_PRPSINFO) {
  289. /*
  290. * Extract the program name. We assume
  291. * it to be 16 characters (that's what it
  292. * is in SunOS 5.x and Linux).
  293. *
  294. * Unfortunately, it's at a different offset
  295. * in varous OSes, so try multiple offsets.
  296. * If the characters aren't all printable,
  297. * reject it.
  298. */
  299. for (i = 0; i < NOFFSETS; i++) {
  300. reloffset = prpsoffsets(i);
  301. noffset = offset + reloffset;
  302. for (j = 0; j < 16;
  303. j++, noffset++, reloffset++) {
  304. /*
  305. * Make sure we're not past
  306. * the end of the buffer; if
  307. * we are, just give up.
  308. */
  309. if (noffset >= bufsize)
  310. goto tryanother;
  311. /*
  312. * Make sure we're not past
  313. * the end of the contents;
  314. * if we are, this obviously
  315. * isn't the right offset.
  316. */
  317. if (reloffset >= nh_descsz)
  318. goto tryanother;
  319. c = nbuf[noffset];
  320. if (c == '\0') {
  321. /*
  322. * A '\0' at the
  323. * beginning is
  324. * obviously wrong.
  325. * Any other '\0'
  326. * means we're done.
  327. */
  328. if (j == 0)
  329. goto tryanother;
  330. else
  331. break;
  332. } else {
  333. /*
  334. * A nonprintable
  335. * character is also
  336. * wrong.
  337. */
  338. #define isquote(c) (strchr("'\"`", (c)) != NULL)
  339. if (!isprint(c) ||
  340. isquote(c))
  341. goto tryanother;
  342. }
  343. }
  344. /*
  345. * Well, that worked.
  346. */
  347. printf(", from '%.16s'",
  348. &nbuf[offset + prpsoffsets(i)]);
  349. break;
  350. tryanother:
  351. ;
  352. }
  353. break;
  354. }
  355. offset += nh_descsz;
  356. offset = ((offset + 3)/4)*4;
  357. }
  358. }
  359. }
  360. #endif
  361. void
  362. tryelf(fd, buf, nbytes)
  363. int fd;
  364. unsigned char *buf;
  365. int nbytes;
  366. {
  367. union {
  368. int32 l;
  369. char c[sizeof (int32)];
  370. } u;
  371. int class;
  372. int swap;
  373. /*
  374. * ELF executables have multiple section headers in arbitrary
  375. * file locations and thus file(1) cannot determine it from easily.
  376. * Instead we traverse thru all section headers until a symbol table
  377. * one is found or else the binary is stripped.
  378. */
  379. if (buf[EI_MAG0] != ELFMAG0
  380. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  381. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  382. return;
  383. class = buf[4];
  384. if (class == ELFCLASS32) {
  385. Elf32_Ehdr elfhdr;
  386. if (nbytes <= sizeof (Elf32_Ehdr))
  387. return;
  388. u.l = 1;
  389. (void) memcpy(&elfhdr, buf, sizeof elfhdr);
  390. swap = (u.c[sizeof(int32) - 1] + 1) != elfhdr.e_ident[5];
  391. if (getu16(swap, elfhdr.e_type) == ET_CORE)
  392. #ifdef ELFCORE
  393. dophn_core(class, swap,
  394. fd,
  395. getu32(swap, elfhdr.e_phoff),
  396. getu16(swap, elfhdr.e_phnum),
  397. getu16(swap, elfhdr.e_phentsize));
  398. #else
  399. ;
  400. #endif
  401. else {
  402. if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
  403. dophn_exec(class, swap,
  404. fd,
  405. getu32(swap, elfhdr.e_phoff),
  406. getu16(swap, elfhdr.e_phnum),
  407. getu16(swap, elfhdr.e_phentsize));
  408. }
  409. doshn(class, swap,
  410. fd,
  411. getu32(swap, elfhdr.e_shoff),
  412. getu16(swap, elfhdr.e_shnum),
  413. getu16(swap, elfhdr.e_shentsize));
  414. }
  415. return;
  416. }
  417. if (class == ELFCLASS64) {
  418. Elf64_Ehdr elfhdr;
  419. if (nbytes <= sizeof (Elf64_Ehdr))
  420. return;
  421. u.l = 1;
  422. (void) memcpy(&elfhdr, buf, sizeof elfhdr);
  423. swap = (u.c[sizeof(int32) - 1] + 1) != elfhdr.e_ident[5];
  424. if (getu16(swap, elfhdr.e_type) == ET_CORE)
  425. #ifdef ELFCORE
  426. dophn_core(class, swap,
  427. fd,
  428. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  429. getu32(swap, elfhdr.e_phoff[1]),
  430. #else
  431. getu64(swap, elfhdr.e_phoff),
  432. #endif
  433. getu16(swap, elfhdr.e_phnum),
  434. getu16(swap, elfhdr.e_phentsize));
  435. #else
  436. ;
  437. #endif
  438. else
  439. {
  440. if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
  441. dophn_exec(class, swap,
  442. fd,
  443. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  444. getu32(swap, elfhdr.e_phoff[1]),
  445. #else
  446. getu64(swap, elfhdr.e_phoff),
  447. #endif
  448. getu16(swap, elfhdr.e_phnum),
  449. getu16(swap, elfhdr.e_phentsize));
  450. }
  451. doshn(class, swap,
  452. fd,
  453. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  454. getu32(swap, elfhdr.e_shoff[1]),
  455. #else
  456. getu64(swap, elfhdr.e_shoff),
  457. #endif
  458. getu16(swap, elfhdr.e_shnum),
  459. getu16(swap, elfhdr.e_shentsize));
  460. }
  461. return;
  462. }
  463. }
  464. #endif