readelf.c 31 KB

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