readelf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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.11 1999/10/31 22:23:04 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. 84, /* SunOS 5.x */
  173. 32, /* Linux */
  174. };
  175. size_t prpsoffsets64[] = {
  176. 120, /* SunOS 5.x, 64-bit */
  177. };
  178. #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
  179. #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
  180. #define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
  181. /*
  182. * Look through the program headers of an executable image, searching
  183. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE"; if one
  184. * is found, try looking in various places in its contents for a 16-character
  185. * string containing only printable characters - if found, that string
  186. * should be the name of the program that dropped core.
  187. * Note: right after that 16-character string is, at least in SunOS 5.x
  188. * (and possibly other SVR4-flavored systems) and Linux, a longer string
  189. * (80 characters, in 5.x, probably other SVR4-flavored systems, and Linux)
  190. * containing the start of the command line for that program.
  191. */
  192. static void
  193. dophn_core(class, swap, fd, off, num, size)
  194. int class;
  195. int swap;
  196. int fd;
  197. off_t off;
  198. int num;
  199. size_t size;
  200. {
  201. Elf32_Phdr ph32;
  202. Elf32_Nhdr *nh32;
  203. Elf64_Phdr ph64;
  204. Elf64_Nhdr *nh64;
  205. size_t offset, noffset, reloffset;
  206. unsigned char c;
  207. int i, j;
  208. char nbuf[BUFSIZ];
  209. int bufsize;
  210. for ( ; num; num--) {
  211. if (lseek(fd, off, SEEK_SET) == -1)
  212. error("lseek failed (%s).\n", strerror(errno));
  213. if (read(fd, ph_addr, size) == -1)
  214. error("read failed (%s).\n", strerror(errno));
  215. off += size;
  216. if (ph_type != PT_NOTE)
  217. continue;
  218. if (lseek(fd, (off_t) ph_offset, SEEK_SET) == -1)
  219. error("lseek failed (%s).\n", strerror(errno));
  220. bufsize = read(fd, nbuf, BUFSIZ);
  221. if (bufsize == -1)
  222. error("read failed (%s).\n", strerror(errno));
  223. offset = 0;
  224. for (;;) {
  225. if (offset >= bufsize)
  226. break;
  227. if (class == ELFCLASS32)
  228. nh32 = (Elf32_Nhdr *)&nbuf[offset];
  229. else
  230. nh64 = (Elf64_Nhdr *)&nbuf[offset];
  231. offset += nh_size;
  232. /*
  233. * If this note isn't an NT_PRPSINFO note, it's
  234. * not what we're looking for.
  235. */
  236. if (nh_type != NT_PRPSINFO) {
  237. offset += nh_namesz;
  238. offset = ((offset + 3)/4)*4;
  239. offset += nh_descsz;
  240. offset = ((offset + 3)/4)*4;
  241. continue;
  242. }
  243. /*
  244. * Make sure this note has the name "CORE".
  245. */
  246. if (offset + nh_namesz >= bufsize) {
  247. /*
  248. * We're past the end of the buffer.
  249. */
  250. break;
  251. }
  252. if (nh_namesz != 5
  253. || strcmp(&nbuf[offset], "CORE") != 0)
  254. continue;
  255. offset += nh_namesz;
  256. offset = ((offset + 3)/4)*4;
  257. /*
  258. * Extract the program name. We assume it to be
  259. * 16 characters (that's what it is in SunOS 5.x
  260. * and Linux).
  261. *
  262. * Unfortunately, it's at a different offset in
  263. * SunOS 5.x and Linux, so try multiple offsets.
  264. * If the characters aren't all printable, reject
  265. * it.
  266. */
  267. for (i = 0; i < NOFFSETS; i++) {
  268. reloffset = prpsoffsets(i);
  269. noffset = offset + reloffset;
  270. for (j = 0; j < 16;
  271. j++, noffset++, reloffset++) {
  272. /*
  273. * Make sure we're not past the end
  274. * of the buffer; if we are, just
  275. * give up.
  276. */
  277. if (noffset >= bufsize)
  278. return;
  279. /*
  280. * Make sure we're not past the
  281. * end of the contents; if we
  282. * are, this obviously isn't
  283. * the right offset.
  284. */
  285. if (reloffset >= nh_descsz)
  286. goto tryanother;
  287. c = nbuf[noffset];
  288. if (c != '\0' && !isprint(c))
  289. goto tryanother;
  290. }
  291. /*
  292. * Well, that worked.
  293. */
  294. printf(", from '%.16s'",
  295. &nbuf[offset + prpsoffsets(i)]);
  296. return;
  297. tryanother:
  298. ;
  299. }
  300. offset += nh_descsz;
  301. offset = ((offset + 3)/4)*4;
  302. }
  303. }
  304. }
  305. #endif
  306. void
  307. tryelf(fd, buf, nbytes)
  308. int fd;
  309. unsigned char *buf;
  310. int nbytes;
  311. {
  312. union {
  313. int32 l;
  314. char c[sizeof (int32)];
  315. } u;
  316. int class;
  317. int swap;
  318. /*
  319. * ELF executables have multiple section headers in arbitrary
  320. * file locations and thus file(1) cannot determine it from easily.
  321. * Instead we traverse thru all section headers until a symbol table
  322. * one is found or else the binary is stripped.
  323. */
  324. if (buf[EI_MAG0] != ELFMAG0
  325. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  326. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  327. return;
  328. class = buf[4];
  329. if (class == ELFCLASS32) {
  330. Elf32_Ehdr elfhdr;
  331. if (nbytes <= sizeof (Elf32_Ehdr))
  332. return;
  333. u.l = 1;
  334. (void) memcpy(&elfhdr, buf, sizeof elfhdr);
  335. swap = (u.c[sizeof(long) - 1] + 1) != elfhdr.e_ident[5];
  336. if (getu16(swap, elfhdr.e_type) == ET_CORE)
  337. #ifdef ELFCORE
  338. dophn_core(class, swap,
  339. fd,
  340. getu32(swap, elfhdr.e_phoff),
  341. getu16(swap, elfhdr.e_phnum),
  342. getu16(swap, elfhdr.e_phentsize));
  343. #else
  344. ;
  345. #endif
  346. else {
  347. if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
  348. dophn_exec(class, swap,
  349. fd,
  350. getu32(swap, elfhdr.e_phoff),
  351. getu16(swap, elfhdr.e_phnum),
  352. getu16(swap, elfhdr.e_phentsize));
  353. }
  354. doshn(class, swap,
  355. fd,
  356. getu32(swap, elfhdr.e_shoff),
  357. getu16(swap, elfhdr.e_shnum),
  358. getu16(swap, elfhdr.e_shentsize));
  359. }
  360. return;
  361. }
  362. if (class == ELFCLASS64) {
  363. Elf64_Ehdr elfhdr;
  364. if (nbytes <= sizeof (Elf64_Ehdr))
  365. return;
  366. u.l = 1;
  367. (void) memcpy(&elfhdr, buf, sizeof elfhdr);
  368. swap = (u.c[sizeof(long) - 1] + 1) != elfhdr.e_ident[5];
  369. if (getu16(swap, elfhdr.e_type) == ET_CORE)
  370. #ifdef ELFCORE
  371. dophn_core(class, swap,
  372. fd,
  373. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  374. getu32(swap, elfhdr.e_phoff[1]),
  375. #else
  376. getu64(swap, elfhdr.e_phoff),
  377. #endif
  378. getu16(swap, elfhdr.e_phnum),
  379. getu16(swap, elfhdr.e_phentsize));
  380. #else
  381. ;
  382. #endif
  383. else
  384. {
  385. if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
  386. dophn_exec(class, swap,
  387. fd,
  388. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  389. getu32(swap, elfhdr.e_phoff[1]),
  390. #else
  391. getu64(swap, elfhdr.e_phoff),
  392. #endif
  393. getu16(swap, elfhdr.e_phnum),
  394. getu16(swap, elfhdr.e_phentsize));
  395. }
  396. doshn(class, swap,
  397. fd,
  398. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  399. getu32(swap, elfhdr.e_shoff[1]),
  400. #else
  401. getu64(swap, elfhdr.e_shoff),
  402. #endif
  403. getu16(swap, elfhdr.e_shnum),
  404. getu16(swap, elfhdr.e_shentsize));
  405. }
  406. return;
  407. }
  408. }
  409. #endif