readelf.c 13 KB

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