readelf.c 35 KB

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