readelf.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  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. #ifndef lint
  29. FILE_RCSID("@(#)$File: readelf.c,v 1.81 2008/11/04 16:38:28 christos Exp $")
  30. #endif
  31. #ifdef BUILTIN_ELF
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #include "readelf.h"
  39. #include "magic.h"
  40. #ifdef ELFCORE
  41. private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
  42. off_t, int *);
  43. #endif
  44. private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
  45. off_t, int *, int);
  46. private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, int *,
  47. int);
  48. private size_t donote(struct magic_set *, void *, size_t, size_t, int,
  49. int, size_t, int *);
  50. #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
  51. #define isquote(c) (strchr("'\"`", (c)) != NULL)
  52. private uint16_t getu16(int, uint16_t);
  53. private uint32_t getu32(int, uint32_t);
  54. private uint64_t getu64(int, uint64_t);
  55. private uint16_t
  56. getu16(int swap, uint16_t value)
  57. {
  58. union {
  59. uint16_t ui;
  60. char c[2];
  61. } retval, tmpval;
  62. if (swap) {
  63. tmpval.ui = value;
  64. retval.c[0] = tmpval.c[1];
  65. retval.c[1] = tmpval.c[0];
  66. return retval.ui;
  67. } else
  68. return value;
  69. }
  70. private uint32_t
  71. getu32(int swap, uint32_t value)
  72. {
  73. union {
  74. uint32_t ui;
  75. char c[4];
  76. } retval, tmpval;
  77. if (swap) {
  78. tmpval.ui = value;
  79. retval.c[0] = tmpval.c[3];
  80. retval.c[1] = tmpval.c[2];
  81. retval.c[2] = tmpval.c[1];
  82. retval.c[3] = tmpval.c[0];
  83. return retval.ui;
  84. } else
  85. return value;
  86. }
  87. private uint64_t
  88. getu64(int swap, uint64_t value)
  89. {
  90. union {
  91. uint64_t ui;
  92. char c[8];
  93. } retval, tmpval;
  94. if (swap) {
  95. tmpval.ui = value;
  96. retval.c[0] = tmpval.c[7];
  97. retval.c[1] = tmpval.c[6];
  98. retval.c[2] = tmpval.c[5];
  99. retval.c[3] = tmpval.c[4];
  100. retval.c[4] = tmpval.c[3];
  101. retval.c[5] = tmpval.c[2];
  102. retval.c[6] = tmpval.c[1];
  103. retval.c[7] = tmpval.c[0];
  104. return retval.ui;
  105. } else
  106. return value;
  107. }
  108. #define elf_getu16(swap, value) getu16(swap, value)
  109. #define elf_getu32(swap, value) getu32(swap, value)
  110. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  111. # define elf_getu64(swap, array) \
  112. ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
  113. (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
  114. #else
  115. # define elf_getu64(swap, value) getu64(swap, value)
  116. #endif
  117. #define xsh_addr (clazz == ELFCLASS32 \
  118. ? (void *) &sh32 \
  119. : (void *) &sh64)
  120. #define xsh_sizeof (clazz == ELFCLASS32 \
  121. ? sizeof sh32 \
  122. : sizeof sh64)
  123. #define xsh_size (clazz == ELFCLASS32 \
  124. ? elf_getu32(swap, sh32.sh_size) \
  125. : elf_getu64(swap, sh64.sh_size))
  126. #define xsh_offset (clazz == ELFCLASS32 \
  127. ? elf_getu32(swap, sh32.sh_offset) \
  128. : elf_getu64(swap, sh64.sh_offset))
  129. #define xsh_type (clazz == ELFCLASS32 \
  130. ? elf_getu32(swap, sh32.sh_type) \
  131. : elf_getu32(swap, sh64.sh_type))
  132. #define xph_addr (clazz == ELFCLASS32 \
  133. ? (void *) &ph32 \
  134. : (void *) &ph64)
  135. #define xph_sizeof (clazz == ELFCLASS32 \
  136. ? sizeof ph32 \
  137. : sizeof ph64)
  138. #define xph_type (clazz == ELFCLASS32 \
  139. ? elf_getu32(swap, ph32.p_type) \
  140. : elf_getu32(swap, ph64.p_type))
  141. #define xph_offset (off_t)(clazz == ELFCLASS32 \
  142. ? elf_getu32(swap, ph32.p_offset) \
  143. : elf_getu64(swap, ph64.p_offset))
  144. #define xph_align (size_t)((clazz == ELFCLASS32 \
  145. ? (off_t) (ph32.p_align ? \
  146. elf_getu32(swap, ph32.p_align) : 4) \
  147. : (off_t) (ph64.p_align ? \
  148. elf_getu64(swap, ph64.p_align) : 4)))
  149. #define xph_filesz (size_t)((clazz == ELFCLASS32 \
  150. ? elf_getu32(swap, ph32.p_filesz) \
  151. : elf_getu64(swap, ph64.p_filesz)))
  152. #define xnh_addr (clazz == ELFCLASS32 \
  153. ? (void *) &nh32 \
  154. : (void *) &nh64)
  155. #define xph_memsz (size_t)((clazz == ELFCLASS32 \
  156. ? elf_getu32(swap, ph32.p_memsz) \
  157. : elf_getu64(swap, ph64.p_memsz)))
  158. #define xnh_sizeof (clazz == ELFCLASS32 \
  159. ? sizeof nh32 \
  160. : sizeof nh64)
  161. #define xnh_type (clazz == ELFCLASS32 \
  162. ? elf_getu32(swap, nh32.n_type) \
  163. : elf_getu32(swap, nh64.n_type))
  164. #define xnh_namesz (clazz == ELFCLASS32 \
  165. ? elf_getu32(swap, nh32.n_namesz) \
  166. : elf_getu32(swap, nh64.n_namesz))
  167. #define xnh_descsz (clazz == ELFCLASS32 \
  168. ? elf_getu32(swap, nh32.n_descsz) \
  169. : elf_getu32(swap, nh64.n_descsz))
  170. #define prpsoffsets(i) (clazz == ELFCLASS32 \
  171. ? prpsoffsets32[i] \
  172. : prpsoffsets64[i])
  173. #define xcap_addr (clazz == ELFCLASS32 \
  174. ? (void *) &cap32 \
  175. : (void *) &cap64)
  176. #define xcap_sizeof (clazz == ELFCLASS32 \
  177. ? sizeof cap32 \
  178. : sizeof cap64)
  179. #define xcap_tag (clazz == ELFCLASS32 \
  180. ? elf_getu32(swap, cap32.c_tag) \
  181. : elf_getu64(swap, cap64.c_tag))
  182. #define xcap_val (clazz == ELFCLASS32 \
  183. ? elf_getu32(swap, cap32.c_un.c_val) \
  184. : elf_getu64(swap, cap64.c_un.c_val))
  185. #ifdef ELFCORE
  186. /*
  187. * Try larger offsets first to avoid false matches
  188. * from earlier data that happen to look like strings.
  189. */
  190. static const size_t prpsoffsets32[] = {
  191. #ifdef USE_NT_PSINFO
  192. 104, /* SunOS 5.x (command line) */
  193. 88, /* SunOS 5.x (short name) */
  194. #endif /* USE_NT_PSINFO */
  195. 100, /* SunOS 5.x (command line) */
  196. 84, /* SunOS 5.x (short name) */
  197. 44, /* Linux (command line) */
  198. 28, /* Linux 2.0.36 (short name) */
  199. 8, /* FreeBSD */
  200. };
  201. static const size_t prpsoffsets64[] = {
  202. #ifdef USE_NT_PSINFO
  203. 152, /* SunOS 5.x (command line) */
  204. 136, /* SunOS 5.x (short name) */
  205. #endif /* USE_NT_PSINFO */
  206. 136, /* SunOS 5.x, 64-bit (command line) */
  207. 120, /* SunOS 5.x, 64-bit (short name) */
  208. 56, /* Linux (command line) */
  209. 40, /* Linux (tested on core from 2.4.x, short name) */
  210. 16, /* FreeBSD, 64-bit */
  211. };
  212. #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
  213. #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
  214. #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
  215. /*
  216. * Look through the program headers of an executable image, searching
  217. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
  218. * "FreeBSD"; if one is found, try looking in various places in its
  219. * contents for a 16-character string containing only printable
  220. * characters - if found, that string should be the name of the program
  221. * that dropped core. Note: right after that 16-character string is,
  222. * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
  223. * Linux, a longer string (80 characters, in 5.x, probably other
  224. * SVR4-flavored systems, and Linux) containing the start of the
  225. * command line for that program.
  226. *
  227. * SunOS 5.x core files contain two PT_NOTE sections, with the types
  228. * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
  229. * same info about the command name and command line, so it probably
  230. * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
  231. * above (see USE_NT_PSINFO), in case we ever decide to do so. The
  232. * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
  233. * the SunOS 5.x file command relies on this (and prefers the latter).
  234. *
  235. * The signal number probably appears in a section of type NT_PRSTATUS,
  236. * but that's also rather OS-dependent, in ways that are harder to
  237. * dissect with heuristics, so I'm not bothering with the signal number.
  238. * (I suppose the signal number could be of interest in situations where
  239. * you don't have the binary of the program that dropped core; if you
  240. * *do* have that binary, the debugger will probably tell you what
  241. * signal it was.)
  242. */
  243. #define OS_STYLE_SVR4 0
  244. #define OS_STYLE_FREEBSD 1
  245. #define OS_STYLE_NETBSD 2
  246. private const char os_style_names[][8] = {
  247. "SVR4",
  248. "FreeBSD",
  249. "NetBSD",
  250. };
  251. #define FLAGS_DID_CORE 1
  252. #define FLAGS_DID_NOTE 2
  253. #define FLAGS_DID_CORE_STYLE 4
  254. private int
  255. dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  256. int num, size_t size, off_t fsize, int *flags)
  257. {
  258. Elf32_Phdr ph32;
  259. Elf64_Phdr ph64;
  260. size_t offset;
  261. unsigned char nbuf[BUFSIZ];
  262. ssize_t bufsize;
  263. off_t savedoffset;
  264. struct stat st;
  265. if (fstat(fd, &st) < 0) {
  266. file_badread(ms);
  267. return -1;
  268. }
  269. if (size != xph_sizeof) {
  270. if (file_printf(ms, ", corrupted program header size") == -1)
  271. return -1;
  272. return 0;
  273. }
  274. /*
  275. * Loop through all the program headers.
  276. */
  277. for ( ; num; num--) {
  278. if ((savedoffset = lseek(fd, off, SEEK_SET)) == (off_t)-1) {
  279. file_badseek(ms);
  280. return -1;
  281. }
  282. if (read(fd, xph_addr, xph_sizeof) == -1) {
  283. file_badread(ms);
  284. return -1;
  285. }
  286. if (xph_offset > fsize) {
  287. if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
  288. file_badseek(ms);
  289. return -1;
  290. }
  291. continue;
  292. }
  293. off += size;
  294. if (xph_type != PT_NOTE)
  295. continue;
  296. /*
  297. * This is a PT_NOTE section; loop through all the notes
  298. * in the section.
  299. */
  300. if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
  301. file_badseek(ms);
  302. return -1;
  303. }
  304. bufsize = read(fd, nbuf,
  305. ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
  306. if (bufsize == -1) {
  307. file_badread(ms);
  308. return -1;
  309. }
  310. offset = 0;
  311. for (;;) {
  312. if (offset >= (size_t)bufsize)
  313. break;
  314. offset = donote(ms, nbuf, offset, (size_t)bufsize,
  315. clazz, swap, 4, flags);
  316. if (offset == 0)
  317. break;
  318. }
  319. }
  320. return 0;
  321. }
  322. #endif
  323. private size_t
  324. donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
  325. int clazz, int swap, size_t align, int *flags)
  326. {
  327. Elf32_Nhdr nh32;
  328. Elf64_Nhdr nh64;
  329. size_t noff, doff;
  330. #ifdef ELFCORE
  331. int os_style = -1;
  332. #endif
  333. uint32_t namesz, descsz;
  334. unsigned char *nbuf = CAST(unsigned char *, vbuf);
  335. (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
  336. offset += xnh_sizeof;
  337. namesz = xnh_namesz;
  338. descsz = xnh_descsz;
  339. if ((namesz == 0) && (descsz == 0)) {
  340. /*
  341. * We're out of note headers.
  342. */
  343. return (offset >= size) ? offset : size;
  344. }
  345. if (namesz & 0x80000000) {
  346. (void)file_printf(ms, ", bad note name size 0x%lx",
  347. (unsigned long)namesz);
  348. return offset;
  349. }
  350. if (descsz & 0x80000000) {
  351. (void)file_printf(ms, ", bad note description size 0x%lx",
  352. (unsigned long)descsz);
  353. return offset;
  354. }
  355. noff = offset;
  356. doff = ELF_ALIGN(offset + namesz);
  357. if (offset + namesz > size) {
  358. /*
  359. * We're past the end of the buffer.
  360. */
  361. return doff;
  362. }
  363. offset = ELF_ALIGN(doff + descsz);
  364. if (doff + descsz > size) {
  365. /*
  366. * We're past the end of the buffer.
  367. */
  368. return (offset >= size) ? offset : size;
  369. }
  370. if (*flags & FLAGS_DID_NOTE)
  371. goto core;
  372. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  373. xnh_type == NT_GNU_VERSION && descsz == 16) {
  374. uint32_t desc[4];
  375. (void)memcpy(desc, &nbuf[doff], sizeof(desc));
  376. if (file_printf(ms, ", for GNU/") == -1)
  377. return size;
  378. switch (elf_getu32(swap, desc[0])) {
  379. case GNU_OS_LINUX:
  380. if (file_printf(ms, "Linux") == -1)
  381. return size;
  382. break;
  383. case GNU_OS_HURD:
  384. if (file_printf(ms, "Hurd") == -1)
  385. return size;
  386. break;
  387. case GNU_OS_SOLARIS:
  388. if (file_printf(ms, "Solaris") == -1)
  389. return size;
  390. break;
  391. case GNU_OS_KFREEBSD:
  392. if (file_printf(ms, "kFreeBSD") == -1)
  393. return size;
  394. break;
  395. case GNU_OS_KNETBSD:
  396. if (file_printf(ms, "kNetBSD") == -1)
  397. return size;
  398. break;
  399. default:
  400. if (file_printf(ms, "<unknown>") == -1)
  401. return size;
  402. }
  403. if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
  404. elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
  405. return size;
  406. *flags |= FLAGS_DID_NOTE;
  407. return size;
  408. }
  409. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
  410. xnh_type == NT_NETBSD_VERSION && descsz == 4) {
  411. uint32_t desc;
  412. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  413. desc = elf_getu32(swap, desc);
  414. if (file_printf(ms, ", for NetBSD") == -1)
  415. return size;
  416. /*
  417. * The version number used to be stuck as 199905, and was thus
  418. * basically content-free. Newer versions of NetBSD have fixed
  419. * this and now use the encoding of __NetBSD_Version__:
  420. *
  421. * MMmmrrpp00
  422. *
  423. * M = major version
  424. * m = minor version
  425. * r = release ["",A-Z,Z[A-Z] but numeric]
  426. * p = patchlevel
  427. */
  428. if (desc > 100000000U) {
  429. uint32_t ver_patch = (desc / 100) % 100;
  430. uint32_t ver_rel = (desc / 10000) % 100;
  431. uint32_t ver_min = (desc / 1000000) % 100;
  432. uint32_t ver_maj = desc / 100000000;
  433. if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
  434. return size;
  435. if (ver_rel == 0 && ver_patch != 0) {
  436. if (file_printf(ms, ".%u", ver_patch) == -1)
  437. return size;
  438. } else if (ver_rel != 0) {
  439. while (ver_rel > 26) {
  440. if (file_printf(ms, "Z") == -1)
  441. return size;
  442. ver_rel -= 26;
  443. }
  444. if (file_printf(ms, "%c", 'A' + ver_rel - 1)
  445. == -1)
  446. return size;
  447. }
  448. }
  449. *flags |= FLAGS_DID_NOTE;
  450. return size;
  451. }
  452. if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
  453. xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
  454. uint32_t desc;
  455. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  456. desc = elf_getu32(swap, desc);
  457. if (file_printf(ms, ", for FreeBSD") == -1)
  458. return size;
  459. /*
  460. * Contents is __FreeBSD_version, whose relation to OS
  461. * versions is defined by a huge table in the Porter's
  462. * Handbook. This is the general scheme:
  463. *
  464. * Releases:
  465. * Mmp000 (before 4.10)
  466. * Mmi0p0 (before 5.0)
  467. * Mmm0p0
  468. *
  469. * Development branches:
  470. * Mmpxxx (before 4.6)
  471. * Mmp1xx (before 4.10)
  472. * Mmi1xx (before 5.0)
  473. * M000xx (pre-M.0)
  474. * Mmm1xx
  475. *
  476. * M = major version
  477. * m = minor version
  478. * i = minor version increment (491000 -> 4.10)
  479. * p = patchlevel
  480. * x = revision
  481. *
  482. * The first release of FreeBSD to use ELF by default
  483. * was version 3.0.
  484. */
  485. if (desc == 460002) {
  486. if (file_printf(ms, " 4.6.2") == -1)
  487. return size;
  488. } else if (desc < 460100) {
  489. if (file_printf(ms, " %d.%d", desc / 100000,
  490. desc / 10000 % 10) == -1)
  491. return size;
  492. if (desc / 1000 % 10 > 0)
  493. if (file_printf(ms, ".%d", desc / 1000 % 10)
  494. == -1)
  495. return size;
  496. if ((desc % 1000 > 0) || (desc % 100000 == 0))
  497. if (file_printf(ms, " (%d)", desc) == -1)
  498. return size;
  499. } else if (desc < 500000) {
  500. if (file_printf(ms, " %d.%d", desc / 100000,
  501. desc / 10000 % 10 + desc / 1000 % 10) == -1)
  502. return size;
  503. if (desc / 100 % 10 > 0) {
  504. if (file_printf(ms, " (%d)", desc) == -1)
  505. return size;
  506. } else if (desc / 10 % 10 > 0) {
  507. if (file_printf(ms, ".%d", desc / 10 % 10)
  508. == -1)
  509. return size;
  510. }
  511. } else {
  512. if (file_printf(ms, " %d.%d", desc / 100000,
  513. desc / 1000 % 100) == -1)
  514. return size;
  515. if ((desc / 100 % 10 > 0) ||
  516. (desc % 100000 / 100 == 0)) {
  517. if (file_printf(ms, " (%d)", desc) == -1)
  518. return size;
  519. } else if (desc / 10 % 10 > 0) {
  520. if (file_printf(ms, ".%d", desc / 10 % 10)
  521. == -1)
  522. return size;
  523. }
  524. }
  525. *flags |= FLAGS_DID_NOTE;
  526. return size;
  527. }
  528. if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
  529. xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
  530. if (file_printf(ms, ", for OpenBSD") == -1)
  531. return size;
  532. /* Content of note is always 0 */
  533. *flags |= FLAGS_DID_NOTE;
  534. return size;
  535. }
  536. if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
  537. xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
  538. uint32_t desc;
  539. if (file_printf(ms, ", for DragonFly") == -1)
  540. return size;
  541. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  542. desc = elf_getu32(swap, desc);
  543. if (file_printf(ms, " %d.%d.%d", desc / 100000,
  544. desc / 10000 % 10, desc % 10000) == -1)
  545. return size;
  546. *flags |= FLAGS_DID_NOTE;
  547. return size;
  548. }
  549. core:
  550. /*
  551. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  552. * least, doesn't correctly implement name
  553. * sections, in core dumps, as specified by
  554. * the "Program Linking" section of "UNIX(R) System
  555. * V Release 4 Programmer's Guide: ANSI C and
  556. * Programming Support Tools", because my copy
  557. * clearly says "The first 'namesz' bytes in 'name'
  558. * contain a *null-terminated* [emphasis mine]
  559. * character representation of the entry's owner
  560. * or originator", but the 2.0.36 kernel code
  561. * doesn't include the terminating null in the
  562. * name....
  563. */
  564. if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
  565. (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
  566. os_style = OS_STYLE_SVR4;
  567. }
  568. if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
  569. os_style = OS_STYLE_FREEBSD;
  570. }
  571. if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
  572. == 0)) {
  573. os_style = OS_STYLE_NETBSD;
  574. }
  575. #ifdef ELFCORE
  576. if ((*flags & FLAGS_DID_CORE) != 0)
  577. return size;
  578. if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
  579. if (file_printf(ms, ", %s-style", os_style_names[os_style])
  580. == -1)
  581. return size;
  582. *flags |= FLAGS_DID_CORE_STYLE;
  583. }
  584. switch (os_style) {
  585. case OS_STYLE_NETBSD:
  586. if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
  587. uint32_t signo;
  588. /*
  589. * Extract the program name. It is at
  590. * offset 0x7c, and is up to 32-bytes,
  591. * including the terminating NUL.
  592. */
  593. if (file_printf(ms, ", from '%.31s'",
  594. &nbuf[doff + 0x7c]) == -1)
  595. return size;
  596. /*
  597. * Extract the signal number. It is at
  598. * offset 0x08.
  599. */
  600. (void)memcpy(&signo, &nbuf[doff + 0x08],
  601. sizeof(signo));
  602. if (file_printf(ms, " (signal %u)",
  603. elf_getu32(swap, signo)) == -1)
  604. return size;
  605. *flags |= FLAGS_DID_CORE;
  606. return size;
  607. }
  608. break;
  609. default:
  610. if (xnh_type == NT_PRPSINFO) {
  611. size_t i, j;
  612. unsigned char c;
  613. /*
  614. * Extract the program name. We assume
  615. * it to be 16 characters (that's what it
  616. * is in SunOS 5.x and Linux).
  617. *
  618. * Unfortunately, it's at a different offset
  619. * in various OSes, so try multiple offsets.
  620. * If the characters aren't all printable,
  621. * reject it.
  622. */
  623. for (i = 0; i < NOFFSETS; i++) {
  624. unsigned char *cname, *cp;
  625. size_t reloffset = prpsoffsets(i);
  626. size_t noffset = doff + reloffset;
  627. for (j = 0; j < 16; j++, noffset++,
  628. reloffset++) {
  629. /*
  630. * Make sure we're not past
  631. * the end of the buffer; if
  632. * we are, just give up.
  633. */
  634. if (noffset >= size)
  635. goto tryanother;
  636. /*
  637. * Make sure we're not past
  638. * the end of the contents;
  639. * if we are, this obviously
  640. * isn't the right offset.
  641. */
  642. if (reloffset >= descsz)
  643. goto tryanother;
  644. c = nbuf[noffset];
  645. if (c == '\0') {
  646. /*
  647. * A '\0' at the
  648. * beginning is
  649. * obviously wrong.
  650. * Any other '\0'
  651. * means we're done.
  652. */
  653. if (j == 0)
  654. goto tryanother;
  655. else
  656. break;
  657. } else {
  658. /*
  659. * A nonprintable
  660. * character is also
  661. * wrong.
  662. */
  663. if (!isprint(c) || isquote(c))
  664. goto tryanother;
  665. }
  666. }
  667. /*
  668. * Well, that worked.
  669. */
  670. cname = (unsigned char *)
  671. &nbuf[doff + prpsoffsets(i)];
  672. for (cp = cname; *cp && isprint(*cp); cp++)
  673. continue;
  674. /*
  675. * Linux apparently appends a space at the end
  676. * of the command line: remove it.
  677. */
  678. while (cp > cname && isspace(cp[-1]))
  679. cp--;
  680. if (file_printf(ms, ", from '%.*s'",
  681. (int)(cp - cname), cname) == -1)
  682. return size;
  683. *flags |= FLAGS_DID_CORE;
  684. return size;
  685. tryanother:
  686. ;
  687. }
  688. }
  689. break;
  690. }
  691. #endif
  692. return offset;
  693. }
  694. /* SunOS 5.x hardware capability descriptions */
  695. typedef struct cap_desc {
  696. uint64_t cd_mask;
  697. const char *cd_name;
  698. } cap_desc_t;
  699. static const cap_desc_t cap_desc_sparc[] = {
  700. { AV_SPARC_MUL32, "MUL32" },
  701. { AV_SPARC_DIV32, "DIV32" },
  702. { AV_SPARC_FSMULD, "FSMULD" },
  703. { AV_SPARC_V8PLUS, "V8PLUS" },
  704. { AV_SPARC_POPC, "POPC" },
  705. { AV_SPARC_VIS, "VIS" },
  706. { AV_SPARC_VIS2, "VIS2" },
  707. { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
  708. { AV_SPARC_FMAF, "FMAF" },
  709. { AV_SPARC_FJFMAU, "FJFMAU" },
  710. { AV_SPARC_IMA, "IMA" },
  711. { 0, NULL }
  712. };
  713. static const cap_desc_t cap_desc_386[] = {
  714. { AV_386_FPU, "FPU" },
  715. { AV_386_TSC, "TSC" },
  716. { AV_386_CX8, "CX8" },
  717. { AV_386_SEP, "SEP" },
  718. { AV_386_AMD_SYSC, "AMD_SYSC" },
  719. { AV_386_CMOV, "CMOV" },
  720. { AV_386_MMX, "MMX" },
  721. { AV_386_AMD_MMX, "AMD_MMX" },
  722. { AV_386_AMD_3DNow, "AMD_3DNow" },
  723. { AV_386_AMD_3DNowx, "AMD_3DNowx" },
  724. { AV_386_FXSR, "FXSR" },
  725. { AV_386_SSE, "SSE" },
  726. { AV_386_SSE2, "SSE2" },
  727. { AV_386_PAUSE, "PAUSE" },
  728. { AV_386_SSE3, "SSE3" },
  729. { AV_386_MON, "MON" },
  730. { AV_386_CX16, "CX16" },
  731. { AV_386_AHF, "AHF" },
  732. { AV_386_TSCP, "TSCP" },
  733. { AV_386_AMD_SSE4A, "AMD_SSE4A" },
  734. { AV_386_POPCNT, "POPCNT" },
  735. { AV_386_AMD_LZCNT, "AMD_LZCNT" },
  736. { AV_386_SSSE3, "SSSE3" },
  737. { AV_386_SSE4_1, "SSE4.1" },
  738. { AV_386_SSE4_2, "SSE4.2" },
  739. { 0, NULL }
  740. };
  741. private int
  742. doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
  743. size_t size, int *flags, int mach)
  744. {
  745. Elf32_Shdr sh32;
  746. Elf64_Shdr sh64;
  747. int stripped = 1;
  748. void *nbuf;
  749. off_t noff;
  750. uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
  751. uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
  752. if (size != xsh_sizeof) {
  753. if (file_printf(ms, ", corrupted section header size") == -1)
  754. return -1;
  755. return 0;
  756. }
  757. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  758. file_badseek(ms);
  759. return -1;
  760. }
  761. for ( ; num; num--) {
  762. if (read(fd, xsh_addr, xsh_sizeof) == -1) {
  763. file_badread(ms);
  764. return -1;
  765. }
  766. switch (xsh_type) {
  767. case SHT_SYMTAB:
  768. #if 0
  769. case SHT_DYNSYM:
  770. #endif
  771. stripped = 0;
  772. break;
  773. case SHT_NOTE:
  774. if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
  775. (off_t)-1) {
  776. file_badread(ms);
  777. return -1;
  778. }
  779. if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
  780. file_error(ms, errno, "Cannot allocate memory"
  781. " for note");
  782. return -1;
  783. }
  784. if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
  785. (off_t)-1) {
  786. file_badread(ms);
  787. free(nbuf);
  788. return -1;
  789. }
  790. if (read(fd, nbuf, (size_t)xsh_size) !=
  791. (ssize_t)xsh_size) {
  792. free(nbuf);
  793. file_badread(ms);
  794. return -1;
  795. }
  796. noff = 0;
  797. for (;;) {
  798. if (noff >= (off_t)xsh_size)
  799. break;
  800. noff = donote(ms, nbuf, (size_t)noff,
  801. (size_t)xsh_size, clazz, swap, 4,
  802. flags);
  803. if (noff == 0)
  804. break;
  805. }
  806. if ((lseek(fd, off, SEEK_SET)) == (off_t)-1) {
  807. free(nbuf);
  808. file_badread(ms);
  809. return -1;
  810. }
  811. free(nbuf);
  812. break;
  813. case SHT_SUNW_cap:
  814. {
  815. off_t coff;
  816. if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
  817. (off_t)-1) {
  818. file_badread(ms);
  819. return -1;
  820. }
  821. if (lseek(fd, (off_t)xsh_offset, SEEK_SET) ==
  822. (off_t)-1) {
  823. file_badread(ms);
  824. return -1;
  825. }
  826. coff = 0;
  827. for (;;) {
  828. Elf32_Cap cap32;
  829. Elf64_Cap cap64;
  830. char cbuf[/*CONSTCOND*/
  831. MAX(sizeof cap32, sizeof cap64)];
  832. if ((coff += xcap_sizeof) >= (off_t)xsh_size)
  833. break;
  834. if (read(fd, cbuf, (size_t)xcap_sizeof) !=
  835. (ssize_t)xcap_sizeof) {
  836. file_badread(ms);
  837. return -1;
  838. }
  839. (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
  840. switch (xcap_tag) {
  841. case CA_SUNW_NULL:
  842. break;
  843. case CA_SUNW_HW_1:
  844. cap_hw1 |= xcap_val;
  845. break;
  846. case CA_SUNW_SF_1:
  847. cap_sf1 |= xcap_val;
  848. break;
  849. default:
  850. if (file_printf(ms,
  851. ", with unknown capability "
  852. "0x%llx = 0x%llx",
  853. (unsigned long long)xcap_tag,
  854. (unsigned long long)xcap_val) == -1)
  855. return -1;
  856. break;
  857. }
  858. }
  859. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  860. file_badread(ms);
  861. return -1;
  862. }
  863. break;
  864. }
  865. }
  866. }
  867. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  868. return -1;
  869. if (cap_hw1) {
  870. const cap_desc_t *cdp;
  871. switch (mach) {
  872. case EM_SPARC:
  873. case EM_SPARC32PLUS:
  874. case EM_SPARCV9:
  875. cdp = cap_desc_sparc;
  876. break;
  877. case EM_386:
  878. case EM_IA_64:
  879. case EM_AMD64:
  880. cdp = cap_desc_386;
  881. break;
  882. default:
  883. cdp = NULL;
  884. break;
  885. }
  886. if (file_printf(ms, ", uses") == -1)
  887. return -1;
  888. if (cdp) {
  889. while (cdp->cd_name) {
  890. if (cap_hw1 & cdp->cd_mask) {
  891. if (file_printf(ms,
  892. " %s", cdp->cd_name) == -1)
  893. return -1;
  894. cap_hw1 &= ~cdp->cd_mask;
  895. }
  896. ++cdp;
  897. }
  898. if (cap_hw1)
  899. if (file_printf(ms,
  900. " unknown hardware capability 0x%llx",
  901. (unsigned long long)cap_hw1) == -1)
  902. return -1;
  903. } else {
  904. if (file_printf(ms,
  905. " hardware capability 0x%llx",
  906. (unsigned long long)cap_hw1) == -1)
  907. return -1;
  908. }
  909. }
  910. if (cap_sf1) {
  911. if (cap_sf1 & SF1_SUNW_FPUSED) {
  912. if (file_printf(ms,
  913. (cap_sf1 & SF1_SUNW_FPKNWN)
  914. ? ", uses frame pointer"
  915. : ", not known to use frame pointer") == -1)
  916. return -1;
  917. }
  918. cap_sf1 &= ~SF1_SUNW_MASK;
  919. if (cap_sf1)
  920. if (file_printf(ms,
  921. ", with unknown software capability 0x%llx",
  922. (unsigned long long)cap_sf1) == -1)
  923. return -1;
  924. }
  925. return 0;
  926. }
  927. /*
  928. * Look through the program headers of an executable image, searching
  929. * for a PT_INTERP section; if one is found, it's dynamically linked,
  930. * otherwise it's statically linked.
  931. */
  932. private int
  933. dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  934. int num, size_t size, off_t fsize, int *flags, int sh_num)
  935. {
  936. Elf32_Phdr ph32;
  937. Elf64_Phdr ph64;
  938. const char *linking_style = "statically";
  939. const char *shared_libraries = "";
  940. unsigned char nbuf[BUFSIZ];
  941. int bufsize;
  942. size_t offset, align;
  943. off_t savedoffset = (off_t)-1;
  944. struct stat st;
  945. if (fstat(fd, &st) < 0) {
  946. file_badread(ms);
  947. return -1;
  948. }
  949. if (size != xph_sizeof) {
  950. if (file_printf(ms, ", corrupted program header size") == -1)
  951. return -1;
  952. return 0;
  953. }
  954. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  955. file_badseek(ms);
  956. return -1;
  957. }
  958. for ( ; num; num--) {
  959. if (read(fd, xph_addr, xph_sizeof) == -1) {
  960. file_badread(ms);
  961. return -1;
  962. }
  963. if (xph_offset > st.st_size && savedoffset != (off_t)-1) {
  964. if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
  965. file_badseek(ms);
  966. return -1;
  967. }
  968. continue;
  969. }
  970. if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
  971. file_badseek(ms);
  972. return -1;
  973. }
  974. if (xph_offset > fsize) {
  975. if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
  976. file_badseek(ms);
  977. return -1;
  978. }
  979. continue;
  980. }
  981. switch (xph_type) {
  982. case PT_DYNAMIC:
  983. linking_style = "dynamically";
  984. break;
  985. case PT_INTERP:
  986. shared_libraries = " (uses shared libs)";
  987. break;
  988. case PT_NOTE:
  989. if ((align = xph_align) & 0x80000000) {
  990. if (file_printf(ms,
  991. ", invalid note alignment 0x%lx",
  992. (unsigned long)align) == -1)
  993. return -1;
  994. align = 4;
  995. }
  996. if (sh_num)
  997. break;
  998. /*
  999. * This is a PT_NOTE section; loop through all the notes
  1000. * in the section.
  1001. */
  1002. if (lseek(fd, xph_offset, SEEK_SET)
  1003. == (off_t)-1) {
  1004. file_badseek(ms);
  1005. return -1;
  1006. }
  1007. bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
  1008. xph_filesz : sizeof(nbuf)));
  1009. if (bufsize == -1) {
  1010. file_badread(ms);
  1011. return -1;
  1012. }
  1013. offset = 0;
  1014. for (;;) {
  1015. if (offset >= (size_t)bufsize)
  1016. break;
  1017. offset = donote(ms, nbuf, offset,
  1018. (size_t)bufsize, clazz, swap, align,
  1019. flags);
  1020. if (offset == 0)
  1021. break;
  1022. }
  1023. if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
  1024. file_badseek(ms);
  1025. return -1;
  1026. }
  1027. break;
  1028. default:
  1029. break;
  1030. }
  1031. }
  1032. if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
  1033. == -1)
  1034. return -1;
  1035. return 0;
  1036. }
  1037. protected int
  1038. file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
  1039. size_t nbytes)
  1040. {
  1041. union {
  1042. int32_t l;
  1043. char c[sizeof (int32_t)];
  1044. } u;
  1045. int clazz;
  1046. int swap;
  1047. struct stat st;
  1048. off_t fsize;
  1049. int flags = 0;
  1050. Elf32_Ehdr elf32hdr;
  1051. Elf64_Ehdr elf64hdr;
  1052. uint16_t type;
  1053. if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
  1054. return 0;
  1055. /*
  1056. * ELF executables have multiple section headers in arbitrary
  1057. * file locations and thus file(1) cannot determine it from easily.
  1058. * Instead we traverse thru all section headers until a symbol table
  1059. * one is found or else the binary is stripped.
  1060. * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
  1061. */
  1062. if (buf[EI_MAG0] != ELFMAG0
  1063. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  1064. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  1065. return 0;
  1066. /*
  1067. * If we cannot seek, it must be a pipe, socket or fifo.
  1068. */
  1069. if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
  1070. fd = file_pipe2file(ms, fd, buf, nbytes);
  1071. if (fstat(fd, &st) == -1) {
  1072. file_badread(ms);
  1073. return -1;
  1074. }
  1075. fsize = st.st_size;
  1076. clazz = buf[EI_CLASS];
  1077. switch (clazz) {
  1078. case ELFCLASS32:
  1079. #undef elf_getu
  1080. #define elf_getu(a, b) elf_getu32(a, b)
  1081. #undef elfhdr
  1082. #define elfhdr elf32hdr
  1083. #include "elfclass.h"
  1084. case ELFCLASS64:
  1085. #undef elf_getu
  1086. #define elf_getu(a, b) elf_getu64(a, b)
  1087. #undef elfhdr
  1088. #define elfhdr elf64hdr
  1089. #include "elfclass.h"
  1090. default:
  1091. if (file_printf(ms, ", unknown class %d", clazz) == -1)
  1092. return -1;
  1093. break;
  1094. }
  1095. return 0;
  1096. }
  1097. #endif