readelf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*
  2. * Copyright (c) Christos Zoulas 2003.
  3. * All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice immediately at the beginning of the file, without modification,
  10. * this list of conditions, and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include "file.h"
  28. #ifdef BUILTIN_ELF
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <stdlib.h>
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #include "readelf.h"
  36. #ifndef lint
  37. FILE_RCSID("@(#)$Id: readelf.c,v 1.54 2006/01/13 00:45:21 christos Exp $")
  38. #endif
  39. #ifdef ELFCORE
  40. private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t);
  41. #endif
  42. private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t);
  43. private int doshn(struct magic_set *, int, int, int, off_t, int, size_t);
  44. private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
  45. int, size_t, int *);
  46. #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
  47. #define isquote(c) (strchr("'\"`", (c)) != NULL)
  48. private uint16_t getu16(int, uint16_t);
  49. private uint32_t getu32(int, uint32_t);
  50. private uint64_t getu64(int, uint64_t);
  51. private uint16_t
  52. getu16(int swap, uint16_t value)
  53. {
  54. union {
  55. uint16_t ui;
  56. char c[2];
  57. } retval, tmpval;
  58. if (swap) {
  59. tmpval.ui = value;
  60. retval.c[0] = tmpval.c[1];
  61. retval.c[1] = tmpval.c[0];
  62. return retval.ui;
  63. } else
  64. return value;
  65. }
  66. private uint32_t
  67. getu32(int swap, uint32_t value)
  68. {
  69. union {
  70. uint32_t ui;
  71. char c[4];
  72. } retval, tmpval;
  73. if (swap) {
  74. tmpval.ui = value;
  75. retval.c[0] = tmpval.c[3];
  76. retval.c[1] = tmpval.c[2];
  77. retval.c[2] = tmpval.c[1];
  78. retval.c[3] = tmpval.c[0];
  79. return retval.ui;
  80. } else
  81. return value;
  82. }
  83. private uint64_t
  84. getu64(int swap, uint64_t value)
  85. {
  86. union {
  87. uint64_t ui;
  88. char c[8];
  89. } retval, tmpval;
  90. if (swap) {
  91. tmpval.ui = value;
  92. retval.c[0] = tmpval.c[7];
  93. retval.c[1] = tmpval.c[6];
  94. retval.c[2] = tmpval.c[5];
  95. retval.c[3] = tmpval.c[4];
  96. retval.c[4] = tmpval.c[3];
  97. retval.c[5] = tmpval.c[2];
  98. retval.c[6] = tmpval.c[1];
  99. retval.c[7] = tmpval.c[0];
  100. return retval.ui;
  101. } else
  102. return value;
  103. }
  104. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  105. # define elf_getu64(swap, array) \
  106. ((swap ? ((uint64_t)getu32(swap, array[0])) << 32 : getu32(swap, array[0])) + \
  107. (swap ? getu32(swap, array[1]) : ((uint64_t)getu32(swap, array[1]) << 32)))
  108. #else
  109. # define elf_getu64(swap, value) getu64(swap, value)
  110. #endif
  111. #define xsh_addr (class == ELFCLASS32 \
  112. ? (void *) &sh32 \
  113. : (void *) &sh64)
  114. #define xsh_sizeof (class == ELFCLASS32 \
  115. ? sizeof sh32 \
  116. : sizeof sh64)
  117. #define xsh_size (class == ELFCLASS32 \
  118. ? getu32(swap, sh32.sh_size) \
  119. : getu64(swap, sh64.sh_size))
  120. #define xsh_offset (class == ELFCLASS32 \
  121. ? getu32(swap, sh32.sh_offset) \
  122. : getu64(swap, sh64.sh_offset))
  123. #define xsh_type (class == ELFCLASS32 \
  124. ? getu32(swap, sh32.sh_type) \
  125. : getu32(swap, sh64.sh_type))
  126. #define xph_addr (class == ELFCLASS32 \
  127. ? (void *) &ph32 \
  128. : (void *) &ph64)
  129. #define xph_sizeof (class == ELFCLASS32 \
  130. ? sizeof ph32 \
  131. : sizeof ph64)
  132. #define xph_type (class == ELFCLASS32 \
  133. ? getu32(swap, ph32.p_type) \
  134. : getu32(swap, ph64.p_type))
  135. #define xph_offset (class == ELFCLASS32 \
  136. ? getu32(swap, ph32.p_offset) \
  137. : getu64(swap, ph64.p_offset))
  138. #define xph_align (size_t)((class == ELFCLASS32 \
  139. ? (off_t) (ph32.p_align ? \
  140. getu32(swap, ph32.p_align) : 4) \
  141. : (off_t) (ph64.p_align ? \
  142. getu64(swap, ph64.p_align) : 4)))
  143. #define xph_filesz (size_t)((class == ELFCLASS32 \
  144. ? getu32(swap, ph32.p_filesz) \
  145. : getu64(swap, ph64.p_filesz)))
  146. #define xnh_addr (class == ELFCLASS32 \
  147. ? (void *) &nh32 \
  148. : (void *) &nh64)
  149. #define xph_memsz (size_t)((class == ELFCLASS32 \
  150. ? getu32(swap, ph32.p_memsz) \
  151. : getu64(swap, ph64.p_memsz)))
  152. #define xnh_sizeof (class == ELFCLASS32 \
  153. ? sizeof nh32 \
  154. : sizeof nh64)
  155. #define xnh_type (class == ELFCLASS32 \
  156. ? getu32(swap, nh32.n_type) \
  157. : getu32(swap, nh64.n_type))
  158. #define xnh_namesz (class == ELFCLASS32 \
  159. ? getu32(swap, nh32.n_namesz) \
  160. : getu32(swap, nh64.n_namesz))
  161. #define xnh_descsz (class == ELFCLASS32 \
  162. ? getu32(swap, nh32.n_descsz) \
  163. : getu32(swap, nh64.n_descsz))
  164. #define prpsoffsets(i) (class == ELFCLASS32 \
  165. ? prpsoffsets32[i] \
  166. : prpsoffsets64[i])
  167. #ifdef ELFCORE
  168. size_t prpsoffsets32[] = {
  169. 8, /* FreeBSD */
  170. 28, /* Linux 2.0.36 */
  171. 32, /* Linux (I forget which kernel version) */
  172. 84, /* SunOS 5.x */
  173. };
  174. size_t prpsoffsets64[] = {
  175. 16, /* FreeBSD, 64-bit */
  176. 40, /* Linux (tested on core from 2.4.x) */
  177. 120, /* SunOS 5.x, 64-bit */
  178. };
  179. #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
  180. #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
  181. #define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
  182. /*
  183. * Look through the program headers of an executable image, searching
  184. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
  185. * "FreeBSD"; if one is found, try looking in various places in its
  186. * contents for a 16-character string containing only printable
  187. * characters - if found, that string should be the name of the program
  188. * that dropped core. Note: right after that 16-character string is,
  189. * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
  190. * Linux, a longer string (80 characters, in 5.x, probably other
  191. * SVR4-flavored systems, and Linux) containing the start of the
  192. * command line for that program.
  193. *
  194. * The signal number probably appears in a section of type NT_PRSTATUS,
  195. * but that's also rather OS-dependent, in ways that are harder to
  196. * dissect with heuristics, so I'm not bothering with the signal number.
  197. * (I suppose the signal number could be of interest in situations where
  198. * you don't have the binary of the program that dropped core; if you
  199. * *do* have that binary, the debugger will probably tell you what
  200. * signal it was.)
  201. */
  202. #define OS_STYLE_SVR4 0
  203. #define OS_STYLE_FREEBSD 1
  204. #define OS_STYLE_NETBSD 2
  205. private const char *os_style_names[] = {
  206. "SVR4",
  207. "FreeBSD",
  208. "NetBSD",
  209. };
  210. #define FLAGS_DID_CORE 1
  211. private int
  212. dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
  213. int num, size_t size)
  214. {
  215. Elf32_Phdr ph32;
  216. Elf64_Phdr ph64;
  217. size_t offset;
  218. unsigned char nbuf[BUFSIZ];
  219. ssize_t bufsize;
  220. int flags = 0;
  221. if (size != xph_sizeof) {
  222. if (file_printf(ms, ", corrupted program header size") == -1)
  223. return -1;
  224. return 0;
  225. }
  226. /*
  227. * Loop through all the program headers.
  228. */
  229. for ( ; num; num--) {
  230. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  231. file_badseek(ms);
  232. return -1;
  233. }
  234. if (read(fd, xph_addr, xph_sizeof) == -1) {
  235. file_badread(ms);
  236. return -1;
  237. }
  238. off += size;
  239. if (xph_type != PT_NOTE)
  240. continue;
  241. /*
  242. * This is a PT_NOTE section; loop through all the notes
  243. * in the section.
  244. */
  245. if (lseek(fd, (off_t)xph_offset, SEEK_SET) == (off_t)-1) {
  246. file_badseek(ms);
  247. return -1;
  248. }
  249. bufsize = read(fd, nbuf,
  250. ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
  251. if (bufsize == -1) {
  252. file_badread(ms);
  253. return -1;
  254. }
  255. offset = 0;
  256. for (;;) {
  257. if (offset >= (size_t)bufsize)
  258. break;
  259. offset = donote(ms, nbuf, offset, (size_t)bufsize,
  260. class, swap, 4, &flags);
  261. if (offset == 0)
  262. break;
  263. }
  264. }
  265. return 0;
  266. }
  267. #endif
  268. private size_t
  269. donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
  270. int class, int swap, size_t align, int *flags)
  271. {
  272. Elf32_Nhdr nh32;
  273. Elf64_Nhdr nh64;
  274. size_t noff, doff;
  275. #ifdef ELFCORE
  276. int os_style = -1;
  277. #endif
  278. uint32_t namesz, descsz;
  279. (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
  280. offset += xnh_sizeof;
  281. namesz = xnh_namesz;
  282. descsz = xnh_descsz;
  283. if ((namesz == 0) && (descsz == 0)) {
  284. /*
  285. * We're out of note headers.
  286. */
  287. return offset;
  288. }
  289. if (namesz & 0x80000000) {
  290. (void)file_printf(ms, ", bad note name size 0x%lx",
  291. (unsigned long)namesz);
  292. return offset;
  293. }
  294. if (descsz & 0x80000000) {
  295. (void)file_printf(ms, ", bad note description size 0x%lx",
  296. (unsigned long)descsz);
  297. return offset;
  298. }
  299. noff = offset;
  300. doff = ELF_ALIGN(offset + namesz);
  301. if (offset + namesz > size) {
  302. /*
  303. * We're past the end of the buffer.
  304. */
  305. return doff;
  306. }
  307. offset = ELF_ALIGN(doff + descsz);
  308. if (doff + descsz > size) {
  309. return offset;
  310. }
  311. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  312. xnh_type == NT_GNU_VERSION && descsz == 16) {
  313. uint32_t desc[4];
  314. (void)memcpy(desc, &nbuf[doff], sizeof(desc));
  315. if (file_printf(ms, ", for GNU/") == -1)
  316. return size;
  317. switch (getu32(swap, desc[0])) {
  318. case GNU_OS_LINUX:
  319. if (file_printf(ms, "Linux") == -1)
  320. return size;
  321. break;
  322. case GNU_OS_HURD:
  323. if (file_printf(ms, "Hurd") == -1)
  324. return size;
  325. break;
  326. case GNU_OS_SOLARIS:
  327. if (file_printf(ms, "Solaris") == -1)
  328. return size;
  329. break;
  330. default:
  331. if (file_printf(ms, "<unknown>") == -1)
  332. return size;
  333. }
  334. if (file_printf(ms, " %d.%d.%d", getu32(swap, desc[1]),
  335. getu32(swap, desc[2]), getu32(swap, desc[3])) == -1)
  336. return size;
  337. return size;
  338. }
  339. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
  340. xnh_type == NT_NETBSD_VERSION && descsz == 4) {
  341. uint32_t desc;
  342. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  343. desc = getu32(swap, desc);
  344. if (file_printf(ms, ", for NetBSD") == -1)
  345. return size;
  346. /*
  347. * The version number used to be stuck as 199905, and was thus
  348. * basically content-free. Newer versions of NetBSD have fixed
  349. * this and now use the encoding of __NetBSD_Version__:
  350. *
  351. * MMmmrrpp00
  352. *
  353. * M = major version
  354. * m = minor version
  355. * r = release ["",A-Z,Z[A-Z] but numeric]
  356. * p = patchlevel
  357. */
  358. if (desc > 100000000U) {
  359. u_int ver_patch = (desc / 100) % 100;
  360. u_int ver_rel = (desc / 10000) % 100;
  361. u_int ver_min = (desc / 1000000) % 100;
  362. u_int ver_maj = desc / 100000000;
  363. if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
  364. return size;
  365. if (ver_rel == 0 && ver_patch != 0) {
  366. if (file_printf(ms, ".%u", ver_patch) == -1)
  367. return size;
  368. } else if (ver_rel != 0) {
  369. while (ver_rel > 26) {
  370. file_printf(ms, "Z");
  371. ver_rel -= 26;
  372. }
  373. file_printf(ms, "%c", 'A' + ver_rel - 1);
  374. }
  375. }
  376. return size;
  377. }
  378. if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
  379. xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
  380. uint32_t desc;
  381. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  382. desc = getu32(swap, desc);
  383. if (file_printf(ms, ", for FreeBSD") == -1)
  384. return size;
  385. /*
  386. * Contents is __FreeBSD_version, whose relation to OS
  387. * versions is defined by a huge table in the Porter's
  388. * Handbook. This is the general scheme:
  389. *
  390. * Releases:
  391. * Mmp000 (before 4.10)
  392. * Mmi0p0 (before 5.0)
  393. * Mmm0p0
  394. *
  395. * Development branches:
  396. * Mmpxxx (before 4.6)
  397. * Mmp1xx (before 4.10)
  398. * Mmi1xx (before 5.0)
  399. * M000xx (pre-M.0)
  400. * Mmm1xx
  401. *
  402. * M = major version
  403. * m = minor version
  404. * i = minor version increment (491000 -> 4.10)
  405. * p = patchlevel
  406. * x = revision
  407. *
  408. * The first release of FreeBSD to use ELF by default
  409. * was version 3.0.
  410. */
  411. if (desc == 460002) {
  412. if (file_printf(ms, " 4.6.2") == -1)
  413. return size;
  414. } else if (desc < 460100) {
  415. if (file_printf(ms, " %d.%d", desc / 100000,
  416. desc / 10000 % 10) == -1)
  417. return size;
  418. if (desc / 1000 % 10 > 0)
  419. if (file_printf(ms, ".%d", desc / 1000 % 10)
  420. == -1)
  421. return size;
  422. if ((desc % 1000 > 0) || (desc % 100000 == 0))
  423. if (file_printf(ms, " (%d)", desc) == -1)
  424. return size;
  425. } else if (desc < 500000) {
  426. if (file_printf(ms, " %d.%d", desc / 100000,
  427. desc / 10000 % 10 + desc / 1000 % 10) == -1)
  428. return size;
  429. if (desc / 100 % 10 > 0) {
  430. if (file_printf(ms, " (%d)", desc) == -1)
  431. return size;
  432. } else if (desc / 10 % 10 > 0) {
  433. if (file_printf(ms, ".%d", desc / 10 % 10)
  434. == -1)
  435. return size;
  436. }
  437. } else {
  438. if (file_printf(ms, " %d.%d", desc / 100000,
  439. desc / 1000 % 100) == -1)
  440. return size;
  441. if ((desc / 100 % 10 > 0) ||
  442. (desc % 100000 / 100 == 0)) {
  443. if (file_printf(ms, " (%d)", desc) == -1)
  444. return size;
  445. } else if (desc / 10 % 10 > 0) {
  446. if (file_printf(ms, ".%d", desc / 10 % 10)
  447. == -1)
  448. return size;
  449. }
  450. }
  451. return size;
  452. }
  453. if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
  454. xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
  455. if (file_printf(ms, ", for OpenBSD") == -1)
  456. return size;
  457. /* Content of note is always 0 */
  458. return size;
  459. }
  460. if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
  461. xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
  462. uint32_t desc;
  463. if (file_printf(ms, ", for DragonFly") == -1)
  464. return size;
  465. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  466. desc = getu32(swap, desc);
  467. if (file_printf(ms, " %d.%d.%d", desc / 100000,
  468. desc / 10000 % 10, desc % 10000) == -1)
  469. return size;
  470. return size;
  471. }
  472. /*
  473. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  474. * least, doesn't correctly implement name
  475. * sections, in core dumps, as specified by
  476. * the "Program Linking" section of "UNIX(R) System
  477. * V Release 4 Programmer's Guide: ANSI C and
  478. * Programming Support Tools", because my copy
  479. * clearly says "The first 'namesz' bytes in 'name'
  480. * contain a *null-terminated* [emphasis mine]
  481. * character representation of the entry's owner
  482. * or originator", but the 2.0.36 kernel code
  483. * doesn't include the terminating null in the
  484. * name....
  485. */
  486. if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
  487. (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
  488. os_style = OS_STYLE_SVR4;
  489. }
  490. if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
  491. os_style = OS_STYLE_FREEBSD;
  492. }
  493. if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
  494. == 0)) {
  495. os_style = OS_STYLE_NETBSD;
  496. }
  497. #ifdef ELFCORE
  498. if (os_style != -1) {
  499. if ((*flags & FLAGS_DID_CORE) == 0) {
  500. if (file_printf(ms, ", %s-style",
  501. os_style_names[os_style]) == -1)
  502. return size;
  503. *flags |= FLAGS_DID_CORE;
  504. }
  505. }
  506. switch (os_style) {
  507. case OS_STYLE_NETBSD:
  508. if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
  509. uint32_t signo;
  510. /*
  511. * Extract the program name. It is at
  512. * offset 0x7c, and is up to 32-bytes,
  513. * including the terminating NUL.
  514. */
  515. if (file_printf(ms, ", from '%.31s'",
  516. &nbuf[doff + 0x7c]) == -1)
  517. return size;
  518. /*
  519. * Extract the signal number. It is at
  520. * offset 0x08.
  521. */
  522. (void)memcpy(&signo, &nbuf[doff + 0x08],
  523. sizeof(signo));
  524. if (file_printf(ms, " (signal %u)",
  525. getu32(swap, signo)) == -1)
  526. return size;
  527. return size;
  528. }
  529. break;
  530. default:
  531. if (xnh_type == NT_PRPSINFO) {
  532. size_t i, j;
  533. unsigned char c;
  534. /*
  535. * Extract the program name. We assume
  536. * it to be 16 characters (that's what it
  537. * is in SunOS 5.x and Linux).
  538. *
  539. * Unfortunately, it's at a different offset
  540. * in varous OSes, so try multiple offsets.
  541. * If the characters aren't all printable,
  542. * reject it.
  543. */
  544. for (i = 0; i < NOFFSETS; i++) {
  545. size_t reloffset = prpsoffsets(i);
  546. size_t noffset = doff + reloffset;
  547. for (j = 0; j < 16; j++, noffset++,
  548. reloffset++) {
  549. /*
  550. * Make sure we're not past
  551. * the end of the buffer; if
  552. * we are, just give up.
  553. */
  554. if (noffset >= size)
  555. goto tryanother;
  556. /*
  557. * Make sure we're not past
  558. * the end of the contents;
  559. * if we are, this obviously
  560. * isn't the right offset.
  561. */
  562. if (reloffset >= descsz)
  563. goto tryanother;
  564. c = nbuf[noffset];
  565. if (c == '\0') {
  566. /*
  567. * A '\0' at the
  568. * beginning is
  569. * obviously wrong.
  570. * Any other '\0'
  571. * means we're done.
  572. */
  573. if (j == 0)
  574. goto tryanother;
  575. else
  576. break;
  577. } else {
  578. /*
  579. * A nonprintable
  580. * character is also
  581. * wrong.
  582. */
  583. if (!isprint(c) || isquote(c))
  584. goto tryanother;
  585. }
  586. }
  587. /*
  588. * Well, that worked.
  589. */
  590. if (file_printf(ms, ", from '%.16s'",
  591. &nbuf[doff + prpsoffsets(i)]) == -1)
  592. return size;
  593. return size;
  594. tryanother:
  595. ;
  596. }
  597. }
  598. break;
  599. }
  600. #endif
  601. return offset;
  602. }
  603. private int
  604. doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
  605. size_t size)
  606. {
  607. Elf32_Shdr sh32;
  608. Elf64_Shdr sh64;
  609. int stripped = 1;
  610. int flags = 0;
  611. void *nbuf;
  612. off_t noff;
  613. if (size != xsh_sizeof) {
  614. if (file_printf(ms, ", corrupted section header size") == -1)
  615. return -1;
  616. return 0;
  617. }
  618. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  619. file_badseek(ms);
  620. return -1;
  621. }
  622. for ( ; num; num--) {
  623. if (read(fd, xsh_addr, xsh_sizeof) == -1) {
  624. file_badread(ms);
  625. return -1;
  626. }
  627. switch (xsh_type) {
  628. case SHT_SYMTAB:
  629. #if 0
  630. case SHT_DYNSYM:
  631. #endif
  632. stripped = 0;
  633. break;
  634. case SHT_NOTE:
  635. if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
  636. (off_t)-1) {
  637. file_badread(ms);
  638. return -1;
  639. }
  640. if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
  641. file_error(ms, errno, "Cannot allocate memory"
  642. " for note");
  643. return -1;
  644. }
  645. if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
  646. (off_t)-1) {
  647. file_badread(ms);
  648. free(nbuf);
  649. return -1;
  650. }
  651. if (read(fd, nbuf, (size_t)xsh_size) !=
  652. (ssize_t)xsh_size) {
  653. free(nbuf);
  654. file_badread(ms);
  655. return -1;
  656. }
  657. noff = 0;
  658. for (;;) {
  659. if (noff >= (size_t)xsh_size)
  660. break;
  661. noff = donote(ms, nbuf, (size_t)noff,
  662. (size_t)xsh_size, class, swap, 4,
  663. &flags);
  664. if (noff == 0)
  665. break;
  666. }
  667. if ((lseek(fd, off, SEEK_SET)) == (off_t)-1) {
  668. free(nbuf);
  669. file_badread(ms);
  670. return -1;
  671. }
  672. free(nbuf);
  673. break;
  674. }
  675. }
  676. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  677. return -1;
  678. return 0;
  679. }
  680. /*
  681. * Look through the program headers of an executable image, searching
  682. * for a PT_INTERP section; if one is found, it's dynamically linked,
  683. * otherwise it's statically linked.
  684. */
  685. private int
  686. dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
  687. int num, size_t size)
  688. {
  689. Elf32_Phdr ph32;
  690. Elf64_Phdr ph64;
  691. const char *linking_style = "statically";
  692. const char *shared_libraries = "";
  693. unsigned char nbuf[BUFSIZ];
  694. int bufsize;
  695. size_t offset, align;
  696. off_t savedoffset;
  697. int flags = 0;
  698. if (size != xph_sizeof) {
  699. if (file_printf(ms, ", corrupted program header size") == -1)
  700. return -1;
  701. return 0;
  702. }
  703. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  704. file_badseek(ms);
  705. return -1;
  706. }
  707. for ( ; num; num--) {
  708. if (read(fd, xph_addr, xph_sizeof) == -1) {
  709. file_badread(ms);
  710. return -1;
  711. }
  712. if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
  713. file_badseek(ms);
  714. return -1;
  715. }
  716. switch (xph_type) {
  717. case PT_DYNAMIC:
  718. linking_style = "dynamically";
  719. break;
  720. case PT_INTERP:
  721. shared_libraries = " (uses shared libs)";
  722. break;
  723. case PT_NOTE:
  724. if ((align = xph_align) & 0x80000000) {
  725. if (file_printf(ms,
  726. ", invalid note alignment 0x%lx",
  727. (unsigned long)align) == -1)
  728. return -1;
  729. align = 4;
  730. }
  731. /*
  732. * This is a PT_NOTE section; loop through all the notes
  733. * in the section.
  734. */
  735. if (lseek(fd, (off_t)xph_offset, SEEK_SET)
  736. == (off_t)-1) {
  737. file_badseek(ms);
  738. return -1;
  739. }
  740. bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
  741. xph_filesz : sizeof(nbuf)));
  742. if (bufsize == -1) {
  743. file_badread(ms);
  744. return -1;
  745. }
  746. offset = 0;
  747. for (;;) {
  748. if (offset >= (size_t)bufsize)
  749. break;
  750. offset = donote(ms, nbuf, offset,
  751. (size_t)bufsize, class, swap, align,
  752. &flags);
  753. if (offset == 0)
  754. break;
  755. }
  756. if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
  757. file_badseek(ms);
  758. return -1;
  759. }
  760. break;
  761. }
  762. }
  763. if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
  764. == -1)
  765. return -1;
  766. return 0;
  767. }
  768. protected int
  769. file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
  770. size_t nbytes)
  771. {
  772. union {
  773. int32_t l;
  774. char c[sizeof (int32_t)];
  775. } u;
  776. int class;
  777. int swap;
  778. /*
  779. * If we cannot seek, it must be a pipe, socket or fifo.
  780. */
  781. if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
  782. fd = file_pipe2file(ms, fd, buf, nbytes);
  783. /*
  784. * ELF executables have multiple section headers in arbitrary
  785. * file locations and thus file(1) cannot determine it from easily.
  786. * Instead we traverse thru all section headers until a symbol table
  787. * one is found or else the binary is stripped.
  788. */
  789. if (buf[EI_MAG0] != ELFMAG0
  790. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  791. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  792. return 0;
  793. class = buf[4];
  794. if (class == ELFCLASS32) {
  795. Elf32_Ehdr elfhdr;
  796. if (nbytes <= sizeof (Elf32_Ehdr))
  797. return 0;
  798. u.l = 1;
  799. (void) memcpy(&elfhdr, buf, sizeof elfhdr);
  800. swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
  801. if (getu16(swap, elfhdr.e_type) == ET_CORE) {
  802. #ifdef ELFCORE
  803. if (dophn_core(ms, class, swap, fd,
  804. (off_t)getu32(swap, elfhdr.e_phoff),
  805. getu16(swap, elfhdr.e_phnum),
  806. (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
  807. return -1;
  808. #else
  809. ;
  810. #endif
  811. } else {
  812. if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
  813. if (dophn_exec(ms, class, swap,
  814. fd, (off_t)getu32(swap, elfhdr.e_phoff),
  815. getu16(swap, elfhdr.e_phnum),
  816. (size_t)getu16(swap, elfhdr.e_phentsize))
  817. == -1)
  818. return -1;
  819. }
  820. if (doshn(ms, class, swap, fd,
  821. (off_t)getu32(swap, elfhdr.e_shoff),
  822. getu16(swap, elfhdr.e_shnum),
  823. (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)
  824. return -1;
  825. }
  826. return 1;
  827. }
  828. if (class == ELFCLASS64) {
  829. Elf64_Ehdr elfhdr;
  830. if (nbytes <= sizeof (Elf64_Ehdr))
  831. return 0;
  832. u.l = 1;
  833. (void) memcpy(&elfhdr, buf, sizeof elfhdr);
  834. swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
  835. if (getu16(swap, elfhdr.e_type) == ET_CORE) {
  836. #ifdef ELFCORE
  837. if (dophn_core(ms, class, swap, fd,
  838. (off_t)elf_getu64(swap, elfhdr.e_phoff),
  839. getu16(swap, elfhdr.e_phnum),
  840. (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
  841. return -1;
  842. #else
  843. ;
  844. #endif
  845. } else {
  846. if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
  847. if (dophn_exec(ms, class, swap, fd,
  848. (off_t)elf_getu64(swap, elfhdr.e_phoff),
  849. getu16(swap, elfhdr.e_phnum),
  850. (size_t)getu16(swap, elfhdr.e_phentsize))
  851. == -1)
  852. return -1;
  853. }
  854. if (doshn(ms, class, swap, fd,
  855. (off_t)elf_getu64(swap, elfhdr.e_shoff),
  856. getu16(swap, elfhdr.e_shnum),
  857. (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)
  858. return -1;
  859. }
  860. return 1;
  861. }
  862. return 0;
  863. }
  864. #endif