readelf.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286
  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.103 2014/05/02 02:25:10 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. size_t i, j;
  664. unsigned char c;
  665. /*
  666. * Extract the program name. We assume
  667. * it to be 16 characters (that's what it
  668. * is in SunOS 5.x and Linux).
  669. *
  670. * Unfortunately, it's at a different offset
  671. * in various OSes, so try multiple offsets.
  672. * If the characters aren't all printable,
  673. * reject it.
  674. */
  675. for (i = 0; i < NOFFSETS; i++) {
  676. unsigned char *cname, *cp;
  677. size_t reloffset = prpsoffsets(i);
  678. size_t noffset = doff + reloffset;
  679. size_t k;
  680. for (j = 0; j < 16; j++, noffset++,
  681. reloffset++) {
  682. /*
  683. * Make sure we're not past
  684. * the end of the buffer; if
  685. * we are, just give up.
  686. */
  687. if (noffset >= size)
  688. goto tryanother;
  689. /*
  690. * Make sure we're not past
  691. * the end of the contents;
  692. * if we are, this obviously
  693. * isn't the right offset.
  694. */
  695. if (reloffset >= descsz)
  696. goto tryanother;
  697. c = nbuf[noffset];
  698. if (c == '\0') {
  699. /*
  700. * A '\0' at the
  701. * beginning is
  702. * obviously wrong.
  703. * Any other '\0'
  704. * means we're done.
  705. */
  706. if (j == 0)
  707. goto tryanother;
  708. else
  709. break;
  710. } else {
  711. /*
  712. * A nonprintable
  713. * character is also
  714. * wrong.
  715. */
  716. if (!isprint(c) || isquote(c))
  717. goto tryanother;
  718. }
  719. }
  720. /*
  721. * Well, that worked.
  722. */
  723. /*
  724. * Try next offsets, in case this match is
  725. * in the middle of a string.
  726. */
  727. for (k = i + 1 ; k < NOFFSETS ; k++) {
  728. size_t no;
  729. int adjust = 1;
  730. if (prpsoffsets(k) >= prpsoffsets(i))
  731. continue;
  732. for (no = doff + prpsoffsets(k);
  733. no < doff + prpsoffsets(i); no++)
  734. adjust = adjust
  735. && isprint(nbuf[no]);
  736. if (adjust)
  737. i = k;
  738. }
  739. cname = (unsigned char *)
  740. &nbuf[doff + prpsoffsets(i)];
  741. for (cp = cname; *cp && isprint(*cp); cp++)
  742. continue;
  743. /*
  744. * Linux apparently appends a space at the end
  745. * of the command line: remove it.
  746. */
  747. while (cp > cname && isspace(cp[-1]))
  748. cp--;
  749. if (file_printf(ms, ", from '%.*s'",
  750. (int)(cp - cname), cname) == -1)
  751. return size;
  752. *flags |= FLAGS_DID_CORE;
  753. return size;
  754. tryanother:
  755. ;
  756. }
  757. }
  758. break;
  759. }
  760. #endif
  761. return offset;
  762. }
  763. /* SunOS 5.x hardware capability descriptions */
  764. typedef struct cap_desc {
  765. uint64_t cd_mask;
  766. const char *cd_name;
  767. } cap_desc_t;
  768. static const cap_desc_t cap_desc_sparc[] = {
  769. { AV_SPARC_MUL32, "MUL32" },
  770. { AV_SPARC_DIV32, "DIV32" },
  771. { AV_SPARC_FSMULD, "FSMULD" },
  772. { AV_SPARC_V8PLUS, "V8PLUS" },
  773. { AV_SPARC_POPC, "POPC" },
  774. { AV_SPARC_VIS, "VIS" },
  775. { AV_SPARC_VIS2, "VIS2" },
  776. { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
  777. { AV_SPARC_FMAF, "FMAF" },
  778. { AV_SPARC_FJFMAU, "FJFMAU" },
  779. { AV_SPARC_IMA, "IMA" },
  780. { 0, NULL }
  781. };
  782. static const cap_desc_t cap_desc_386[] = {
  783. { AV_386_FPU, "FPU" },
  784. { AV_386_TSC, "TSC" },
  785. { AV_386_CX8, "CX8" },
  786. { AV_386_SEP, "SEP" },
  787. { AV_386_AMD_SYSC, "AMD_SYSC" },
  788. { AV_386_CMOV, "CMOV" },
  789. { AV_386_MMX, "MMX" },
  790. { AV_386_AMD_MMX, "AMD_MMX" },
  791. { AV_386_AMD_3DNow, "AMD_3DNow" },
  792. { AV_386_AMD_3DNowx, "AMD_3DNowx" },
  793. { AV_386_FXSR, "FXSR" },
  794. { AV_386_SSE, "SSE" },
  795. { AV_386_SSE2, "SSE2" },
  796. { AV_386_PAUSE, "PAUSE" },
  797. { AV_386_SSE3, "SSE3" },
  798. { AV_386_MON, "MON" },
  799. { AV_386_CX16, "CX16" },
  800. { AV_386_AHF, "AHF" },
  801. { AV_386_TSCP, "TSCP" },
  802. { AV_386_AMD_SSE4A, "AMD_SSE4A" },
  803. { AV_386_POPCNT, "POPCNT" },
  804. { AV_386_AMD_LZCNT, "AMD_LZCNT" },
  805. { AV_386_SSSE3, "SSSE3" },
  806. { AV_386_SSE4_1, "SSE4.1" },
  807. { AV_386_SSE4_2, "SSE4.2" },
  808. { 0, NULL }
  809. };
  810. private int
  811. doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
  812. size_t size, off_t fsize, int *flags, int mach, int strtab)
  813. {
  814. Elf32_Shdr sh32;
  815. Elf64_Shdr sh64;
  816. int stripped = 1;
  817. void *nbuf;
  818. off_t noff, coff, name_off;
  819. uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
  820. uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
  821. char name[50];
  822. if (size != xsh_sizeof) {
  823. if (file_printf(ms, ", corrupted section header size") == -1)
  824. return -1;
  825. return 0;
  826. }
  827. /* Read offset of name section to be able to read section names later */
  828. if (pread(fd, xsh_addr, xsh_sizeof, off + size * strtab) == -1) {
  829. file_badread(ms);
  830. return -1;
  831. }
  832. name_off = xsh_offset;
  833. for ( ; num; num--) {
  834. /* Read the name of this section. */
  835. if (pread(fd, name, sizeof(name), name_off + xsh_name) == -1) {
  836. file_badread(ms);
  837. return -1;
  838. }
  839. name[sizeof(name) - 1] = '\0';
  840. if (strcmp(name, ".debug_info") == 0)
  841. stripped = 0;
  842. if (pread(fd, xsh_addr, xsh_sizeof, off) == -1) {
  843. file_badread(ms);
  844. return -1;
  845. }
  846. off += size;
  847. /* Things we can determine before we seek */
  848. switch (xsh_type) {
  849. case SHT_SYMTAB:
  850. #if 0
  851. case SHT_DYNSYM:
  852. #endif
  853. stripped = 0;
  854. break;
  855. default:
  856. if (xsh_offset > fsize) {
  857. /* Perhaps warn here */
  858. continue;
  859. }
  860. break;
  861. }
  862. /* Things we can determine when we seek */
  863. switch (xsh_type) {
  864. case SHT_NOTE:
  865. if ((nbuf = malloc(xsh_size)) == NULL) {
  866. file_error(ms, errno, "Cannot allocate memory"
  867. " for note");
  868. return -1;
  869. }
  870. if (pread(fd, nbuf, xsh_size, xsh_offset) == -1) {
  871. file_badread(ms);
  872. free(nbuf);
  873. return -1;
  874. }
  875. noff = 0;
  876. for (;;) {
  877. if (noff >= (off_t)xsh_size)
  878. break;
  879. noff = donote(ms, nbuf, (size_t)noff,
  880. xsh_size, clazz, swap, 4, flags);
  881. if (noff == 0)
  882. break;
  883. }
  884. free(nbuf);
  885. break;
  886. case SHT_SUNW_cap:
  887. switch (mach) {
  888. case EM_SPARC:
  889. case EM_SPARCV9:
  890. case EM_IA_64:
  891. case EM_386:
  892. case EM_AMD64:
  893. break;
  894. default:
  895. goto skip;
  896. }
  897. if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
  898. file_badseek(ms);
  899. return -1;
  900. }
  901. coff = 0;
  902. for (;;) {
  903. Elf32_Cap cap32;
  904. Elf64_Cap cap64;
  905. char cbuf[/*CONSTCOND*/
  906. MAX(sizeof cap32, sizeof cap64)];
  907. if ((coff += xcap_sizeof) > (off_t)xsh_size)
  908. break;
  909. if (read(fd, cbuf, (size_t)xcap_sizeof) !=
  910. (ssize_t)xcap_sizeof) {
  911. file_badread(ms);
  912. return -1;
  913. }
  914. if (cbuf[0] == 'A') {
  915. #ifdef notyet
  916. char *p = cbuf + 1;
  917. uint32_t len, tag;
  918. memcpy(&len, p, sizeof(len));
  919. p += 4;
  920. len = getu32(swap, len);
  921. if (memcmp("gnu", p, 3) != 0) {
  922. if (file_printf(ms,
  923. ", unknown capability %.3s", p)
  924. == -1)
  925. return -1;
  926. break;
  927. }
  928. p += strlen(p) + 1;
  929. tag = *p++;
  930. memcpy(&len, p, sizeof(len));
  931. p += 4;
  932. len = getu32(swap, len);
  933. if (tag != 1) {
  934. if (file_printf(ms, ", unknown gnu"
  935. " capability tag %d", tag)
  936. == -1)
  937. return -1;
  938. break;
  939. }
  940. // gnu attributes
  941. #endif
  942. break;
  943. }
  944. (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
  945. switch (xcap_tag) {
  946. case CA_SUNW_NULL:
  947. break;
  948. case CA_SUNW_HW_1:
  949. cap_hw1 |= xcap_val;
  950. break;
  951. case CA_SUNW_SF_1:
  952. cap_sf1 |= xcap_val;
  953. break;
  954. default:
  955. if (file_printf(ms,
  956. ", with unknown capability "
  957. "0x%" INT64_T_FORMAT "x = 0x%"
  958. INT64_T_FORMAT "x",
  959. (unsigned long long)xcap_tag,
  960. (unsigned long long)xcap_val) == -1)
  961. return -1;
  962. break;
  963. }
  964. }
  965. /*FALLTHROUGH*/
  966. skip:
  967. default:
  968. break;
  969. }
  970. }
  971. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  972. return -1;
  973. if (cap_hw1) {
  974. const cap_desc_t *cdp;
  975. switch (mach) {
  976. case EM_SPARC:
  977. case EM_SPARC32PLUS:
  978. case EM_SPARCV9:
  979. cdp = cap_desc_sparc;
  980. break;
  981. case EM_386:
  982. case EM_IA_64:
  983. case EM_AMD64:
  984. cdp = cap_desc_386;
  985. break;
  986. default:
  987. cdp = NULL;
  988. break;
  989. }
  990. if (file_printf(ms, ", uses") == -1)
  991. return -1;
  992. if (cdp) {
  993. while (cdp->cd_name) {
  994. if (cap_hw1 & cdp->cd_mask) {
  995. if (file_printf(ms,
  996. " %s", cdp->cd_name) == -1)
  997. return -1;
  998. cap_hw1 &= ~cdp->cd_mask;
  999. }
  1000. ++cdp;
  1001. }
  1002. if (cap_hw1)
  1003. if (file_printf(ms,
  1004. " unknown hardware capability 0x%"
  1005. INT64_T_FORMAT "x",
  1006. (unsigned long long)cap_hw1) == -1)
  1007. return -1;
  1008. } else {
  1009. if (file_printf(ms,
  1010. " hardware capability 0x%" INT64_T_FORMAT "x",
  1011. (unsigned long long)cap_hw1) == -1)
  1012. return -1;
  1013. }
  1014. }
  1015. if (cap_sf1) {
  1016. if (cap_sf1 & SF1_SUNW_FPUSED) {
  1017. if (file_printf(ms,
  1018. (cap_sf1 & SF1_SUNW_FPKNWN)
  1019. ? ", uses frame pointer"
  1020. : ", not known to use frame pointer") == -1)
  1021. return -1;
  1022. }
  1023. cap_sf1 &= ~SF1_SUNW_MASK;
  1024. if (cap_sf1)
  1025. if (file_printf(ms,
  1026. ", with unknown software capability 0x%"
  1027. INT64_T_FORMAT "x",
  1028. (unsigned long long)cap_sf1) == -1)
  1029. return -1;
  1030. }
  1031. return 0;
  1032. }
  1033. /*
  1034. * Look through the program headers of an executable image, searching
  1035. * for a PT_INTERP section; if one is found, it's dynamically linked,
  1036. * otherwise it's statically linked.
  1037. */
  1038. private int
  1039. dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  1040. int num, size_t size, off_t fsize, int *flags, int sh_num)
  1041. {
  1042. Elf32_Phdr ph32;
  1043. Elf64_Phdr ph64;
  1044. const char *linking_style = "statically";
  1045. const char *shared_libraries = "";
  1046. unsigned char nbuf[BUFSIZ];
  1047. ssize_t bufsize;
  1048. size_t offset, align, len;
  1049. if (size != xph_sizeof) {
  1050. if (file_printf(ms, ", corrupted program header size") == -1)
  1051. return -1;
  1052. return 0;
  1053. }
  1054. for ( ; num; num--) {
  1055. if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
  1056. file_badread(ms);
  1057. return -1;
  1058. }
  1059. off += size;
  1060. /* Things we can determine before we seek */
  1061. switch (xph_type) {
  1062. case PT_DYNAMIC:
  1063. linking_style = "dynamically";
  1064. break;
  1065. case PT_INTERP:
  1066. shared_libraries = " (uses shared libs)";
  1067. break;
  1068. default:
  1069. if (xph_offset > fsize) {
  1070. /* Maybe warn here? */
  1071. continue;
  1072. }
  1073. break;
  1074. }
  1075. /* Things we can determine when we seek */
  1076. switch (xph_type) {
  1077. case PT_NOTE:
  1078. if ((align = xph_align) & 0x80000000UL) {
  1079. if (file_printf(ms,
  1080. ", invalid note alignment 0x%lx",
  1081. (unsigned long)align) == -1)
  1082. return -1;
  1083. align = 4;
  1084. }
  1085. if (sh_num)
  1086. break;
  1087. /*
  1088. * This is a PT_NOTE section; loop through all the notes
  1089. * in the section.
  1090. */
  1091. len = xph_filesz < sizeof(nbuf) ? xph_filesz
  1092. : sizeof(nbuf);
  1093. bufsize = pread(fd, nbuf, len, xph_offset);
  1094. if (bufsize == -1) {
  1095. file_badread(ms);
  1096. return -1;
  1097. }
  1098. offset = 0;
  1099. for (;;) {
  1100. if (offset >= (size_t)bufsize)
  1101. break;
  1102. offset = donote(ms, nbuf, offset,
  1103. (size_t)bufsize, clazz, swap, align,
  1104. flags);
  1105. if (offset == 0)
  1106. break;
  1107. }
  1108. break;
  1109. default:
  1110. break;
  1111. }
  1112. }
  1113. if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
  1114. == -1)
  1115. return -1;
  1116. return 0;
  1117. }
  1118. protected int
  1119. file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
  1120. size_t nbytes)
  1121. {
  1122. union {
  1123. int32_t l;
  1124. char c[sizeof (int32_t)];
  1125. } u;
  1126. int clazz;
  1127. int swap;
  1128. struct stat st;
  1129. off_t fsize;
  1130. int flags = 0;
  1131. Elf32_Ehdr elf32hdr;
  1132. Elf64_Ehdr elf64hdr;
  1133. uint16_t type;
  1134. if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
  1135. return 0;
  1136. /*
  1137. * ELF executables have multiple section headers in arbitrary
  1138. * file locations and thus file(1) cannot determine it from easily.
  1139. * Instead we traverse thru all section headers until a symbol table
  1140. * one is found or else the binary is stripped.
  1141. * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
  1142. */
  1143. if (buf[EI_MAG0] != ELFMAG0
  1144. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  1145. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  1146. return 0;
  1147. /*
  1148. * If we cannot seek, it must be a pipe, socket or fifo.
  1149. */
  1150. if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
  1151. fd = file_pipe2file(ms, fd, buf, nbytes);
  1152. if (fstat(fd, &st) == -1) {
  1153. file_badread(ms);
  1154. return -1;
  1155. }
  1156. fsize = st.st_size;
  1157. clazz = buf[EI_CLASS];
  1158. switch (clazz) {
  1159. case ELFCLASS32:
  1160. #undef elf_getu
  1161. #define elf_getu(a, b) elf_getu32(a, b)
  1162. #undef elfhdr
  1163. #define elfhdr elf32hdr
  1164. #include "elfclass.h"
  1165. case ELFCLASS64:
  1166. #undef elf_getu
  1167. #define elf_getu(a, b) elf_getu64(a, b)
  1168. #undef elfhdr
  1169. #define elfhdr elf64hdr
  1170. #include "elfclass.h"
  1171. default:
  1172. if (file_printf(ms, ", unknown class %d", clazz) == -1)
  1173. return -1;
  1174. break;
  1175. }
  1176. return 0;
  1177. }
  1178. #endif