readelf.c 22 KB

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