readelf.c 25 KB

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