readelf.c 22 KB

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