readelf.c 25 KB

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