readelf.c 25 KB

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