readelf.c 8.0 KB

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