readelf.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  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.117 2014/12/16 23:29:42 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. /*ARGSUSED*/
  435. do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  436. int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz,
  437. size_t noff, size_t doff, int *flags)
  438. {
  439. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  440. type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) {
  441. uint8_t desc[20];
  442. uint32_t i;
  443. *flags |= FLAGS_DID_BUILD_ID;
  444. if (file_printf(ms, ", BuildID[%s]=", descsz == 16 ? "md5/uuid" :
  445. "sha1") == -1)
  446. return 1;
  447. (void)memcpy(desc, &nbuf[doff], descsz);
  448. for (i = 0; i < descsz; i++)
  449. if (file_printf(ms, "%02x", desc[i]) == -1)
  450. return 1;
  451. return 1;
  452. }
  453. return 0;
  454. }
  455. private int
  456. do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  457. int swap, uint32_t namesz, uint32_t descsz,
  458. size_t noff, size_t doff, int *flags)
  459. {
  460. if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
  461. type == NT_GNU_VERSION && descsz == 2) {
  462. *flags |= FLAGS_DID_OS_NOTE;
  463. file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
  464. return 1;
  465. }
  466. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  467. type == NT_GNU_VERSION && descsz == 16) {
  468. uint32_t desc[4];
  469. (void)memcpy(desc, &nbuf[doff], sizeof(desc));
  470. *flags |= FLAGS_DID_OS_NOTE;
  471. if (file_printf(ms, ", for GNU/") == -1)
  472. return 1;
  473. switch (elf_getu32(swap, desc[0])) {
  474. case GNU_OS_LINUX:
  475. if (file_printf(ms, "Linux") == -1)
  476. return 1;
  477. break;
  478. case GNU_OS_HURD:
  479. if (file_printf(ms, "Hurd") == -1)
  480. return 1;
  481. break;
  482. case GNU_OS_SOLARIS:
  483. if (file_printf(ms, "Solaris") == -1)
  484. return 1;
  485. break;
  486. case GNU_OS_KFREEBSD:
  487. if (file_printf(ms, "kFreeBSD") == -1)
  488. return 1;
  489. break;
  490. case GNU_OS_KNETBSD:
  491. if (file_printf(ms, "kNetBSD") == -1)
  492. return 1;
  493. break;
  494. default:
  495. if (file_printf(ms, "<unknown>") == -1)
  496. return 1;
  497. }
  498. if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
  499. elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
  500. return 1;
  501. return 1;
  502. }
  503. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
  504. if (type == NT_NETBSD_VERSION && descsz == 4) {
  505. *flags |= FLAGS_DID_OS_NOTE;
  506. do_note_netbsd_version(ms, swap, &nbuf[doff]);
  507. return 1;
  508. }
  509. }
  510. if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) {
  511. if (type == NT_FREEBSD_VERSION && descsz == 4) {
  512. *flags |= FLAGS_DID_OS_NOTE;
  513. do_note_freebsd_version(ms, swap, &nbuf[doff]);
  514. return 1;
  515. }
  516. }
  517. if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
  518. type == NT_OPENBSD_VERSION && descsz == 4) {
  519. *flags |= FLAGS_DID_OS_NOTE;
  520. if (file_printf(ms, ", for OpenBSD") == -1)
  521. return 1;
  522. /* Content of note is always 0 */
  523. return 1;
  524. }
  525. if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
  526. type == NT_DRAGONFLY_VERSION && descsz == 4) {
  527. uint32_t desc;
  528. *flags |= FLAGS_DID_OS_NOTE;
  529. if (file_printf(ms, ", for DragonFly") == -1)
  530. return 1;
  531. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  532. desc = elf_getu32(swap, desc);
  533. if (file_printf(ms, " %d.%d.%d", desc / 100000,
  534. desc / 10000 % 10, desc % 10000) == -1)
  535. return 1;
  536. return 1;
  537. }
  538. return 0;
  539. }
  540. private int
  541. do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  542. int swap, uint32_t namesz, uint32_t descsz,
  543. size_t noff, size_t doff, int *flags)
  544. {
  545. if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 &&
  546. type == NT_NETBSD_PAX && descsz == 4) {
  547. static const char *pax[] = {
  548. "+mprotect",
  549. "-mprotect",
  550. "+segvguard",
  551. "-segvguard",
  552. "+ASLR",
  553. "-ASLR",
  554. };
  555. uint32_t desc;
  556. size_t i;
  557. int did = 0;
  558. *flags |= FLAGS_DID_NETBSD_PAX;
  559. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  560. desc = elf_getu32(swap, desc);
  561. if (desc && file_printf(ms, ", PaX: ") == -1)
  562. return 1;
  563. for (i = 0; i < __arraycount(pax); i++) {
  564. if (((1 << (int)i) & desc) == 0)
  565. continue;
  566. if (file_printf(ms, "%s%s", did++ ? "," : "",
  567. pax[i]) == -1)
  568. return 1;
  569. }
  570. return 1;
  571. }
  572. return 0;
  573. }
  574. private int
  575. do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  576. int swap, uint32_t namesz, uint32_t descsz,
  577. size_t noff, size_t doff, int *flags, size_t size, int clazz)
  578. {
  579. #ifdef ELFCORE
  580. int os_style = -1;
  581. /*
  582. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  583. * least, doesn't correctly implement name
  584. * sections, in core dumps, as specified by
  585. * the "Program Linking" section of "UNIX(R) System
  586. * V Release 4 Programmer's Guide: ANSI C and
  587. * Programming Support Tools", because my copy
  588. * clearly says "The first 'namesz' bytes in 'name'
  589. * contain a *null-terminated* [emphasis mine]
  590. * character representation of the entry's owner
  591. * or originator", but the 2.0.36 kernel code
  592. * doesn't include the terminating null in the
  593. * name....
  594. */
  595. if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
  596. (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
  597. os_style = OS_STYLE_SVR4;
  598. }
  599. if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
  600. os_style = OS_STYLE_FREEBSD;
  601. }
  602. if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
  603. == 0)) {
  604. os_style = OS_STYLE_NETBSD;
  605. }
  606. if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
  607. if (file_printf(ms, ", %s-style", os_style_names[os_style])
  608. == -1)
  609. return 1;
  610. *flags |= FLAGS_DID_CORE_STYLE;
  611. }
  612. switch (os_style) {
  613. case OS_STYLE_NETBSD:
  614. if (type == NT_NETBSD_CORE_PROCINFO) {
  615. char sbuf[512];
  616. uint32_t signo;
  617. /*
  618. * Extract the program name. It is at
  619. * offset 0x7c, and is up to 32-bytes,
  620. * including the terminating NUL.
  621. */
  622. if (file_printf(ms, ", from '%.31s'",
  623. file_printable(sbuf, sizeof(sbuf),
  624. (const char *)&nbuf[doff + 0x7c])) == -1)
  625. return 1;
  626. /*
  627. * Extract the signal number. It is at
  628. * offset 0x08.
  629. */
  630. (void)memcpy(&signo, &nbuf[doff + 0x08],
  631. sizeof(signo));
  632. if (file_printf(ms, " (signal %u)",
  633. elf_getu32(swap, signo)) == -1)
  634. return 1;
  635. *flags |= FLAGS_DID_CORE;
  636. return 1;
  637. }
  638. break;
  639. default:
  640. if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
  641. size_t i, j;
  642. unsigned char c;
  643. /*
  644. * Extract the program name. We assume
  645. * it to be 16 characters (that's what it
  646. * is in SunOS 5.x and Linux).
  647. *
  648. * Unfortunately, it's at a different offset
  649. * in various OSes, so try multiple offsets.
  650. * If the characters aren't all printable,
  651. * reject it.
  652. */
  653. for (i = 0; i < NOFFSETS; i++) {
  654. unsigned char *cname, *cp;
  655. size_t reloffset = prpsoffsets(i);
  656. size_t noffset = doff + reloffset;
  657. size_t k;
  658. for (j = 0; j < 16; j++, noffset++,
  659. reloffset++) {
  660. /*
  661. * Make sure we're not past
  662. * the end of the buffer; if
  663. * we are, just give up.
  664. */
  665. if (noffset >= size)
  666. goto tryanother;
  667. /*
  668. * Make sure we're not past
  669. * the end of the contents;
  670. * if we are, this obviously
  671. * isn't the right offset.
  672. */
  673. if (reloffset >= descsz)
  674. goto tryanother;
  675. c = nbuf[noffset];
  676. if (c == '\0') {
  677. /*
  678. * A '\0' at the
  679. * beginning is
  680. * obviously wrong.
  681. * Any other '\0'
  682. * means we're done.
  683. */
  684. if (j == 0)
  685. goto tryanother;
  686. else
  687. break;
  688. } else {
  689. /*
  690. * A nonprintable
  691. * character is also
  692. * wrong.
  693. */
  694. if (!isprint(c) || isquote(c))
  695. goto tryanother;
  696. }
  697. }
  698. /*
  699. * Well, that worked.
  700. */
  701. /*
  702. * Try next offsets, in case this match is
  703. * in the middle of a string.
  704. */
  705. for (k = i + 1 ; k < NOFFSETS; k++) {
  706. size_t no;
  707. int adjust = 1;
  708. if (prpsoffsets(k) >= prpsoffsets(i))
  709. continue;
  710. for (no = doff + prpsoffsets(k);
  711. no < doff + prpsoffsets(i); no++)
  712. adjust = adjust
  713. && isprint(nbuf[no]);
  714. if (adjust)
  715. i = k;
  716. }
  717. cname = (unsigned char *)
  718. &nbuf[doff + prpsoffsets(i)];
  719. for (cp = cname; *cp && isprint(*cp); cp++)
  720. continue;
  721. /*
  722. * Linux apparently appends a space at the end
  723. * of the command line: remove it.
  724. */
  725. while (cp > cname && isspace(cp[-1]))
  726. cp--;
  727. if (file_printf(ms, ", from '%.*s'",
  728. (int)(cp - cname), cname) == -1)
  729. return 1;
  730. *flags |= FLAGS_DID_CORE;
  731. return 1;
  732. tryanother:
  733. ;
  734. }
  735. }
  736. break;
  737. }
  738. #endif
  739. return 0;
  740. }
  741. private size_t
  742. donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
  743. int clazz, int swap, size_t align, int *flags, uint16_t *notecount)
  744. {
  745. Elf32_Nhdr nh32;
  746. Elf64_Nhdr nh64;
  747. size_t noff, doff;
  748. uint32_t namesz, descsz;
  749. unsigned char *nbuf = CAST(unsigned char *, vbuf);
  750. if (*notecount == 0)
  751. return 0;
  752. --*notecount;
  753. if (xnh_sizeof + offset > size) {
  754. /*
  755. * We're out of note headers.
  756. */
  757. return xnh_sizeof + offset;
  758. }
  759. (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
  760. offset += xnh_sizeof;
  761. namesz = xnh_namesz;
  762. descsz = xnh_descsz;
  763. if ((namesz == 0) && (descsz == 0)) {
  764. /*
  765. * We're out of note headers.
  766. */
  767. return (offset >= size) ? offset : size;
  768. }
  769. if (namesz & 0x80000000) {
  770. (void)file_printf(ms, ", bad note name size 0x%lx",
  771. (unsigned long)namesz);
  772. return 0;
  773. }
  774. if (descsz & 0x80000000) {
  775. (void)file_printf(ms, ", bad note description size 0x%lx",
  776. (unsigned long)descsz);
  777. return 0;
  778. }
  779. noff = offset;
  780. doff = ELF_ALIGN(offset + namesz);
  781. if (offset + namesz > size) {
  782. /*
  783. * We're past the end of the buffer.
  784. */
  785. return doff;
  786. }
  787. offset = ELF_ALIGN(doff + descsz);
  788. if (doff + descsz > size) {
  789. /*
  790. * We're past the end of the buffer.
  791. */
  792. return (offset >= size) ? offset : size;
  793. }
  794. if ((*flags & FLAGS_DID_OS_NOTE) == 0) {
  795. if (do_os_note(ms, nbuf, xnh_type, swap,
  796. namesz, descsz, noff, doff, flags))
  797. return size;
  798. }
  799. if ((*flags & FLAGS_DID_BUILD_ID) == 0) {
  800. if (do_bid_note(ms, nbuf, xnh_type, swap,
  801. namesz, descsz, noff, doff, flags))
  802. return size;
  803. }
  804. if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) {
  805. if (do_pax_note(ms, nbuf, xnh_type, swap,
  806. namesz, descsz, noff, doff, flags))
  807. return size;
  808. }
  809. if ((*flags & FLAGS_DID_CORE) == 0) {
  810. if (do_core_note(ms, nbuf, xnh_type, swap,
  811. namesz, descsz, noff, doff, flags, size, clazz))
  812. return size;
  813. }
  814. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
  815. if (descsz > 100)
  816. descsz = 100;
  817. switch (xnh_type) {
  818. case NT_NETBSD_VERSION:
  819. return size;
  820. case NT_NETBSD_MARCH:
  821. if (*flags & FLAGS_DID_NETBSD_MARCH)
  822. return size;
  823. *flags |= FLAGS_DID_NETBSD_MARCH;
  824. if (file_printf(ms, ", compiled for: %.*s",
  825. (int)descsz, (const char *)&nbuf[doff]) == -1)
  826. return size;
  827. break;
  828. case NT_NETBSD_CMODEL:
  829. if (*flags & FLAGS_DID_NETBSD_CMODEL)
  830. return size;
  831. *flags |= FLAGS_DID_NETBSD_CMODEL;
  832. if (file_printf(ms, ", compiler model: %.*s",
  833. (int)descsz, (const char *)&nbuf[doff]) == -1)
  834. return size;
  835. break;
  836. default:
  837. if (*flags & FLAGS_DID_NETBSD_UNKNOWN)
  838. return size;
  839. *flags |= FLAGS_DID_NETBSD_UNKNOWN;
  840. if (file_printf(ms, ", note=%u", xnh_type) == -1)
  841. return size;
  842. break;
  843. }
  844. return size;
  845. }
  846. return offset;
  847. }
  848. /* SunOS 5.x hardware capability descriptions */
  849. typedef struct cap_desc {
  850. uint64_t cd_mask;
  851. const char *cd_name;
  852. } cap_desc_t;
  853. static const cap_desc_t cap_desc_sparc[] = {
  854. { AV_SPARC_MUL32, "MUL32" },
  855. { AV_SPARC_DIV32, "DIV32" },
  856. { AV_SPARC_FSMULD, "FSMULD" },
  857. { AV_SPARC_V8PLUS, "V8PLUS" },
  858. { AV_SPARC_POPC, "POPC" },
  859. { AV_SPARC_VIS, "VIS" },
  860. { AV_SPARC_VIS2, "VIS2" },
  861. { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
  862. { AV_SPARC_FMAF, "FMAF" },
  863. { AV_SPARC_FJFMAU, "FJFMAU" },
  864. { AV_SPARC_IMA, "IMA" },
  865. { 0, NULL }
  866. };
  867. static const cap_desc_t cap_desc_386[] = {
  868. { AV_386_FPU, "FPU" },
  869. { AV_386_TSC, "TSC" },
  870. { AV_386_CX8, "CX8" },
  871. { AV_386_SEP, "SEP" },
  872. { AV_386_AMD_SYSC, "AMD_SYSC" },
  873. { AV_386_CMOV, "CMOV" },
  874. { AV_386_MMX, "MMX" },
  875. { AV_386_AMD_MMX, "AMD_MMX" },
  876. { AV_386_AMD_3DNow, "AMD_3DNow" },
  877. { AV_386_AMD_3DNowx, "AMD_3DNowx" },
  878. { AV_386_FXSR, "FXSR" },
  879. { AV_386_SSE, "SSE" },
  880. { AV_386_SSE2, "SSE2" },
  881. { AV_386_PAUSE, "PAUSE" },
  882. { AV_386_SSE3, "SSE3" },
  883. { AV_386_MON, "MON" },
  884. { AV_386_CX16, "CX16" },
  885. { AV_386_AHF, "AHF" },
  886. { AV_386_TSCP, "TSCP" },
  887. { AV_386_AMD_SSE4A, "AMD_SSE4A" },
  888. { AV_386_POPCNT, "POPCNT" },
  889. { AV_386_AMD_LZCNT, "AMD_LZCNT" },
  890. { AV_386_SSSE3, "SSSE3" },
  891. { AV_386_SSE4_1, "SSE4.1" },
  892. { AV_386_SSE4_2, "SSE4.2" },
  893. { 0, NULL }
  894. };
  895. private int
  896. doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
  897. size_t size, off_t fsize, int mach, int strtab, int *flags,
  898. uint16_t *notecount)
  899. {
  900. Elf32_Shdr sh32;
  901. Elf64_Shdr sh64;
  902. int stripped = 1;
  903. size_t nbadcap = 0;
  904. void *nbuf;
  905. off_t noff, coff, name_off;
  906. uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
  907. uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
  908. char name[50];
  909. ssize_t namesize;
  910. if (size != xsh_sizeof) {
  911. if (file_printf(ms, ", corrupted section header size") == -1)
  912. return -1;
  913. return 0;
  914. }
  915. /* Read offset of name section to be able to read section names later */
  916. if (pread(fd, xsh_addr, xsh_sizeof, CAST(off_t, (off + size * strtab)))
  917. < (ssize_t)xsh_sizeof) {
  918. file_badread(ms);
  919. return -1;
  920. }
  921. name_off = xsh_offset;
  922. for ( ; num; num--) {
  923. /* Read the name of this section. */
  924. if ((namesize = pread(fd, name, sizeof(name) - 1, name_off + xsh_name)) == -1) {
  925. file_badread(ms);
  926. return -1;
  927. }
  928. name[namesize] = '\0';
  929. if (strcmp(name, ".debug_info") == 0)
  930. stripped = 0;
  931. if (pread(fd, xsh_addr, xsh_sizeof, off) < (ssize_t)xsh_sizeof) {
  932. file_badread(ms);
  933. return -1;
  934. }
  935. off += size;
  936. /* Things we can determine before we seek */
  937. switch (xsh_type) {
  938. case SHT_SYMTAB:
  939. #if 0
  940. case SHT_DYNSYM:
  941. #endif
  942. stripped = 0;
  943. break;
  944. default:
  945. if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
  946. /* Perhaps warn here */
  947. continue;
  948. }
  949. break;
  950. }
  951. /* Things we can determine when we seek */
  952. switch (xsh_type) {
  953. case SHT_NOTE:
  954. if ((nbuf = malloc(xsh_size)) == NULL) {
  955. file_error(ms, errno, "Cannot allocate memory"
  956. " for note");
  957. return -1;
  958. }
  959. if (pread(fd, nbuf, xsh_size, xsh_offset) < (ssize_t)xsh_size) {
  960. file_badread(ms);
  961. free(nbuf);
  962. return -1;
  963. }
  964. noff = 0;
  965. for (;;) {
  966. if (noff >= (off_t)xsh_size)
  967. break;
  968. noff = donote(ms, nbuf, (size_t)noff,
  969. xsh_size, clazz, swap, 4, flags, notecount);
  970. if (noff == 0)
  971. break;
  972. }
  973. free(nbuf);
  974. break;
  975. case SHT_SUNW_cap:
  976. switch (mach) {
  977. case EM_SPARC:
  978. case EM_SPARCV9:
  979. case EM_IA_64:
  980. case EM_386:
  981. case EM_AMD64:
  982. break;
  983. default:
  984. goto skip;
  985. }
  986. if (nbadcap > 5)
  987. break;
  988. if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
  989. file_badseek(ms);
  990. return -1;
  991. }
  992. coff = 0;
  993. for (;;) {
  994. Elf32_Cap cap32;
  995. Elf64_Cap cap64;
  996. char cbuf[/*CONSTCOND*/
  997. MAX(sizeof cap32, sizeof cap64)];
  998. if ((coff += xcap_sizeof) > (off_t)xsh_size)
  999. break;
  1000. if (read(fd, cbuf, (size_t)xcap_sizeof) !=
  1001. (ssize_t)xcap_sizeof) {
  1002. file_badread(ms);
  1003. return -1;
  1004. }
  1005. if (cbuf[0] == 'A') {
  1006. #ifdef notyet
  1007. char *p = cbuf + 1;
  1008. uint32_t len, tag;
  1009. memcpy(&len, p, sizeof(len));
  1010. p += 4;
  1011. len = getu32(swap, len);
  1012. if (memcmp("gnu", p, 3) != 0) {
  1013. if (file_printf(ms,
  1014. ", unknown capability %.3s", p)
  1015. == -1)
  1016. return -1;
  1017. break;
  1018. }
  1019. p += strlen(p) + 1;
  1020. tag = *p++;
  1021. memcpy(&len, p, sizeof(len));
  1022. p += 4;
  1023. len = getu32(swap, len);
  1024. if (tag != 1) {
  1025. if (file_printf(ms, ", unknown gnu"
  1026. " capability tag %d", tag)
  1027. == -1)
  1028. return -1;
  1029. break;
  1030. }
  1031. // gnu attributes
  1032. #endif
  1033. break;
  1034. }
  1035. (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
  1036. switch (xcap_tag) {
  1037. case CA_SUNW_NULL:
  1038. break;
  1039. case CA_SUNW_HW_1:
  1040. cap_hw1 |= xcap_val;
  1041. break;
  1042. case CA_SUNW_SF_1:
  1043. cap_sf1 |= xcap_val;
  1044. break;
  1045. default:
  1046. if (file_printf(ms,
  1047. ", with unknown capability "
  1048. "0x%" INT64_T_FORMAT "x = 0x%"
  1049. INT64_T_FORMAT "x",
  1050. (unsigned long long)xcap_tag,
  1051. (unsigned long long)xcap_val) == -1)
  1052. return -1;
  1053. if (nbadcap++ > 2)
  1054. coff = xsh_size;
  1055. break;
  1056. }
  1057. }
  1058. /*FALLTHROUGH*/
  1059. skip:
  1060. default:
  1061. break;
  1062. }
  1063. }
  1064. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  1065. return -1;
  1066. if (cap_hw1) {
  1067. const cap_desc_t *cdp;
  1068. switch (mach) {
  1069. case EM_SPARC:
  1070. case EM_SPARC32PLUS:
  1071. case EM_SPARCV9:
  1072. cdp = cap_desc_sparc;
  1073. break;
  1074. case EM_386:
  1075. case EM_IA_64:
  1076. case EM_AMD64:
  1077. cdp = cap_desc_386;
  1078. break;
  1079. default:
  1080. cdp = NULL;
  1081. break;
  1082. }
  1083. if (file_printf(ms, ", uses") == -1)
  1084. return -1;
  1085. if (cdp) {
  1086. while (cdp->cd_name) {
  1087. if (cap_hw1 & cdp->cd_mask) {
  1088. if (file_printf(ms,
  1089. " %s", cdp->cd_name) == -1)
  1090. return -1;
  1091. cap_hw1 &= ~cdp->cd_mask;
  1092. }
  1093. ++cdp;
  1094. }
  1095. if (cap_hw1)
  1096. if (file_printf(ms,
  1097. " unknown hardware capability 0x%"
  1098. INT64_T_FORMAT "x",
  1099. (unsigned long long)cap_hw1) == -1)
  1100. return -1;
  1101. } else {
  1102. if (file_printf(ms,
  1103. " hardware capability 0x%" INT64_T_FORMAT "x",
  1104. (unsigned long long)cap_hw1) == -1)
  1105. return -1;
  1106. }
  1107. }
  1108. if (cap_sf1) {
  1109. if (cap_sf1 & SF1_SUNW_FPUSED) {
  1110. if (file_printf(ms,
  1111. (cap_sf1 & SF1_SUNW_FPKNWN)
  1112. ? ", uses frame pointer"
  1113. : ", not known to use frame pointer") == -1)
  1114. return -1;
  1115. }
  1116. cap_sf1 &= ~SF1_SUNW_MASK;
  1117. if (cap_sf1)
  1118. if (file_printf(ms,
  1119. ", with unknown software capability 0x%"
  1120. INT64_T_FORMAT "x",
  1121. (unsigned long long)cap_sf1) == -1)
  1122. return -1;
  1123. }
  1124. return 0;
  1125. }
  1126. /*
  1127. * Look through the program headers of an executable image, searching
  1128. * for a PT_INTERP section; if one is found, it's dynamically linked,
  1129. * otherwise it's statically linked.
  1130. */
  1131. private int
  1132. dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  1133. int num, size_t size, off_t fsize, int sh_num, int *flags,
  1134. uint16_t *notecount)
  1135. {
  1136. Elf32_Phdr ph32;
  1137. Elf64_Phdr ph64;
  1138. const char *linking_style = "statically";
  1139. const char *interp = "";
  1140. unsigned char nbuf[BUFSIZ];
  1141. char ibuf[BUFSIZ];
  1142. ssize_t bufsize;
  1143. size_t offset, align, len;
  1144. if (size != xph_sizeof) {
  1145. if (file_printf(ms, ", corrupted program header size") == -1)
  1146. return -1;
  1147. return 0;
  1148. }
  1149. for ( ; num; num--) {
  1150. if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) {
  1151. file_badread(ms);
  1152. return -1;
  1153. }
  1154. off += size;
  1155. bufsize = 0;
  1156. align = 4;
  1157. /* Things we can determine before we seek */
  1158. switch (xph_type) {
  1159. case PT_DYNAMIC:
  1160. linking_style = "dynamically";
  1161. break;
  1162. case PT_NOTE:
  1163. if (sh_num) /* Did this through section headers */
  1164. continue;
  1165. if (((align = xph_align) & 0x80000000UL) != 0 ||
  1166. align < 4) {
  1167. if (file_printf(ms,
  1168. ", invalid note alignment 0x%lx",
  1169. (unsigned long)align) == -1)
  1170. return -1;
  1171. align = 4;
  1172. }
  1173. /*FALLTHROUGH*/
  1174. case PT_INTERP:
  1175. len = xph_filesz < sizeof(nbuf) ? xph_filesz
  1176. : sizeof(nbuf);
  1177. bufsize = pread(fd, nbuf, len, xph_offset);
  1178. if (bufsize == -1) {
  1179. file_badread(ms);
  1180. return -1;
  1181. }
  1182. break;
  1183. default:
  1184. if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
  1185. /* Maybe warn here? */
  1186. continue;
  1187. }
  1188. break;
  1189. }
  1190. /* Things we can determine when we seek */
  1191. switch (xph_type) {
  1192. case PT_INTERP:
  1193. if (bufsize && nbuf[0]) {
  1194. nbuf[bufsize - 1] = '\0';
  1195. interp = (const char *)nbuf;
  1196. } else
  1197. interp = "*empty*";
  1198. break;
  1199. case PT_NOTE:
  1200. /*
  1201. * This is a PT_NOTE section; loop through all the notes
  1202. * in the section.
  1203. */
  1204. offset = 0;
  1205. for (;;) {
  1206. if (offset >= (size_t)bufsize)
  1207. break;
  1208. offset = donote(ms, nbuf, offset,
  1209. (size_t)bufsize, clazz, swap, align,
  1210. flags, notecount);
  1211. if (offset == 0)
  1212. break;
  1213. }
  1214. break;
  1215. default:
  1216. break;
  1217. }
  1218. }
  1219. if (file_printf(ms, ", %s linked", linking_style)
  1220. == -1)
  1221. return -1;
  1222. if (interp[0])
  1223. if (file_printf(ms, ", interpreter %s",
  1224. file_printable(ibuf, sizeof(ibuf), interp)) == -1)
  1225. return -1;
  1226. return 0;
  1227. }
  1228. protected int
  1229. file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
  1230. size_t nbytes)
  1231. {
  1232. union {
  1233. int32_t l;
  1234. char c[sizeof (int32_t)];
  1235. } u;
  1236. int clazz;
  1237. int swap;
  1238. struct stat st;
  1239. off_t fsize;
  1240. int flags = 0;
  1241. Elf32_Ehdr elf32hdr;
  1242. Elf64_Ehdr elf64hdr;
  1243. uint16_t type, phnum, shnum, notecount;
  1244. if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
  1245. return 0;
  1246. /*
  1247. * ELF executables have multiple section headers in arbitrary
  1248. * file locations and thus file(1) cannot determine it from easily.
  1249. * Instead we traverse thru all section headers until a symbol table
  1250. * one is found or else the binary is stripped.
  1251. * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
  1252. */
  1253. if (buf[EI_MAG0] != ELFMAG0
  1254. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  1255. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  1256. return 0;
  1257. /*
  1258. * If we cannot seek, it must be a pipe, socket or fifo.
  1259. */
  1260. if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
  1261. fd = file_pipe2file(ms, fd, buf, nbytes);
  1262. if (fstat(fd, &st) == -1) {
  1263. file_badread(ms);
  1264. return -1;
  1265. }
  1266. if (S_ISREG(st.st_mode) || st.st_size != 0)
  1267. fsize = st.st_size;
  1268. else
  1269. fsize = SIZE_UNKNOWN;
  1270. clazz = buf[EI_CLASS];
  1271. switch (clazz) {
  1272. case ELFCLASS32:
  1273. #undef elf_getu
  1274. #define elf_getu(a, b) elf_getu32(a, b)
  1275. #undef elfhdr
  1276. #define elfhdr elf32hdr
  1277. #include "elfclass.h"
  1278. case ELFCLASS64:
  1279. #undef elf_getu
  1280. #define elf_getu(a, b) elf_getu64(a, b)
  1281. #undef elfhdr
  1282. #define elfhdr elf64hdr
  1283. #include "elfclass.h"
  1284. default:
  1285. if (file_printf(ms, ", unknown class %d", clazz) == -1)
  1286. return -1;
  1287. break;
  1288. }
  1289. return 0;
  1290. }
  1291. #endif