readelf.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  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.178 2021/06/30 10:08:48 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 *, int, off_t, int, off_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 CAST(off_t, -1)
  58. private int
  59. toomany(struct magic_set *ms, const char *name, uint16_t num)
  60. {
  61. if (ms->flags & MAGIC_MIME)
  62. return 1;
  63. if (file_printf(ms, ", too many %s (%u)", name, num) == -1)
  64. return -1;
  65. return 1;
  66. }
  67. private uint16_t
  68. getu16(int swap, uint16_t value)
  69. {
  70. union {
  71. uint16_t ui;
  72. char c[2];
  73. } retval, tmpval;
  74. if (swap) {
  75. tmpval.ui = value;
  76. retval.c[0] = tmpval.c[1];
  77. retval.c[1] = tmpval.c[0];
  78. return retval.ui;
  79. } else
  80. return value;
  81. }
  82. private uint32_t
  83. getu32(int swap, uint32_t value)
  84. {
  85. union {
  86. uint32_t ui;
  87. char c[4];
  88. } retval, tmpval;
  89. if (swap) {
  90. tmpval.ui = value;
  91. retval.c[0] = tmpval.c[3];
  92. retval.c[1] = tmpval.c[2];
  93. retval.c[2] = tmpval.c[1];
  94. retval.c[3] = tmpval.c[0];
  95. return retval.ui;
  96. } else
  97. return value;
  98. }
  99. private uint64_t
  100. getu64(int swap, uint64_t value)
  101. {
  102. union {
  103. uint64_t ui;
  104. char c[8];
  105. } retval, tmpval;
  106. if (swap) {
  107. tmpval.ui = value;
  108. retval.c[0] = tmpval.c[7];
  109. retval.c[1] = tmpval.c[6];
  110. retval.c[2] = tmpval.c[5];
  111. retval.c[3] = tmpval.c[4];
  112. retval.c[4] = tmpval.c[3];
  113. retval.c[5] = tmpval.c[2];
  114. retval.c[6] = tmpval.c[1];
  115. retval.c[7] = tmpval.c[0];
  116. return retval.ui;
  117. } else
  118. return value;
  119. }
  120. #define elf_getu16(swap, value) getu16(swap, value)
  121. #define elf_getu32(swap, value) getu32(swap, value)
  122. #define elf_getu64(swap, value) getu64(swap, value)
  123. #define xsh_addr (clazz == ELFCLASS32 \
  124. ? CAST(void *, &sh32) \
  125. : CAST(void *, &sh64))
  126. #define xsh_sizeof (clazz == ELFCLASS32 \
  127. ? sizeof(sh32) \
  128. : sizeof(sh64))
  129. #define xsh_size CAST(size_t, (clazz == ELFCLASS32 \
  130. ? elf_getu32(swap, sh32.sh_size) \
  131. : elf_getu64(swap, sh64.sh_size)))
  132. #define xsh_offset CAST(off_t, (clazz == ELFCLASS32 \
  133. ? elf_getu32(swap, sh32.sh_offset) \
  134. : elf_getu64(swap, sh64.sh_offset)))
  135. #define xsh_type (clazz == ELFCLASS32 \
  136. ? elf_getu32(swap, sh32.sh_type) \
  137. : elf_getu32(swap, sh64.sh_type))
  138. #define xsh_name (clazz == ELFCLASS32 \
  139. ? elf_getu32(swap, sh32.sh_name) \
  140. : elf_getu32(swap, sh64.sh_name))
  141. #define xph_addr (clazz == ELFCLASS32 \
  142. ? CAST(void *, &ph32) \
  143. : CAST(void *, &ph64))
  144. #define xph_sizeof (clazz == ELFCLASS32 \
  145. ? sizeof(ph32) \
  146. : sizeof(ph64))
  147. #define xph_type (clazz == ELFCLASS32 \
  148. ? elf_getu32(swap, ph32.p_type) \
  149. : elf_getu32(swap, ph64.p_type))
  150. #define xph_offset CAST(off_t, (clazz == ELFCLASS32 \
  151. ? elf_getu32(swap, ph32.p_offset) \
  152. : elf_getu64(swap, ph64.p_offset)))
  153. #define xph_align CAST(size_t, (clazz == ELFCLASS32 \
  154. ? CAST(off_t, (ph32.p_align ? \
  155. elf_getu32(swap, ph32.p_align) : 4))\
  156. : CAST(off_t, (ph64.p_align ? \
  157. elf_getu64(swap, ph64.p_align) : 4))))
  158. #define xph_vaddr CAST(size_t, (clazz == ELFCLASS32 \
  159. ? CAST(off_t, (ph32.p_vaddr ? \
  160. elf_getu32(swap, ph32.p_vaddr) : 4))\
  161. : CAST(off_t, (ph64.p_vaddr ? \
  162. elf_getu64(swap, ph64.p_vaddr) : 4))))
  163. #define xph_filesz CAST(size_t, (clazz == ELFCLASS32 \
  164. ? elf_getu32(swap, ph32.p_filesz) \
  165. : elf_getu64(swap, ph64.p_filesz)))
  166. #define xph_memsz CAST(size_t, ((clazz == ELFCLASS32 \
  167. ? elf_getu32(swap, ph32.p_memsz) \
  168. : elf_getu64(swap, ph64.p_memsz))))
  169. #define xnh_addr (clazz == ELFCLASS32 \
  170. ? CAST(void *, &nh32) \
  171. : CAST(void *, &nh64))
  172. #define xnh_sizeof (clazz == ELFCLASS32 \
  173. ? sizeof(nh32) \
  174. : sizeof(nh64))
  175. #define xnh_type (clazz == ELFCLASS32 \
  176. ? elf_getu32(swap, nh32.n_type) \
  177. : elf_getu32(swap, nh64.n_type))
  178. #define xnh_namesz (clazz == ELFCLASS32 \
  179. ? elf_getu32(swap, nh32.n_namesz) \
  180. : elf_getu32(swap, nh64.n_namesz))
  181. #define xnh_descsz (clazz == ELFCLASS32 \
  182. ? elf_getu32(swap, nh32.n_descsz) \
  183. : elf_getu32(swap, nh64.n_descsz))
  184. #define xdh_addr (clazz == ELFCLASS32 \
  185. ? CAST(void *, &dh32) \
  186. : CAST(void *, &dh64))
  187. #define xdh_sizeof (clazz == ELFCLASS32 \
  188. ? sizeof(dh32) \
  189. : sizeof(dh64))
  190. #define xdh_tag (clazz == ELFCLASS32 \
  191. ? elf_getu32(swap, dh32.d_tag) \
  192. : elf_getu64(swap, dh64.d_tag))
  193. #define xdh_val (clazz == ELFCLASS32 \
  194. ? elf_getu32(swap, dh32.d_un.d_val) \
  195. : elf_getu64(swap, dh64.d_un.d_val))
  196. #define xcap_addr (clazz == ELFCLASS32 \
  197. ? CAST(void *, &cap32) \
  198. : CAST(void *, &cap64))
  199. #define xcap_sizeof (clazz == ELFCLASS32 \
  200. ? sizeof(cap32) \
  201. : sizeof(cap64))
  202. #define xcap_tag (clazz == ELFCLASS32 \
  203. ? elf_getu32(swap, cap32.c_tag) \
  204. : elf_getu64(swap, cap64.c_tag))
  205. #define xcap_val (clazz == ELFCLASS32 \
  206. ? elf_getu32(swap, cap32.c_un.c_val) \
  207. : elf_getu64(swap, cap64.c_un.c_val))
  208. #define xauxv_addr (clazz == ELFCLASS32 \
  209. ? CAST(void *, &auxv32) \
  210. : CAST(void *, &auxv64))
  211. #define xauxv_sizeof (clazz == ELFCLASS32 \
  212. ? sizeof(auxv32) \
  213. : sizeof(auxv64))
  214. #define xauxv_type (clazz == ELFCLASS32 \
  215. ? elf_getu32(swap, auxv32.a_type) \
  216. : elf_getu64(swap, auxv64.a_type))
  217. #define xauxv_val (clazz == ELFCLASS32 \
  218. ? elf_getu32(swap, auxv32.a_v) \
  219. : elf_getu64(swap, auxv64.a_v))
  220. #define prpsoffsets(i) (clazz == ELFCLASS32 \
  221. ? prpsoffsets32[i] \
  222. : prpsoffsets64[i])
  223. #ifdef ELFCORE
  224. /*
  225. * Try larger offsets first to avoid false matches
  226. * from earlier data that happen to look like strings.
  227. */
  228. static const size_t prpsoffsets32[] = {
  229. #ifdef USE_NT_PSINFO
  230. 104, /* SunOS 5.x (command line) */
  231. 88, /* SunOS 5.x (short name) */
  232. #endif /* USE_NT_PSINFO */
  233. 100, /* SunOS 5.x (command line) */
  234. 84, /* SunOS 5.x (short name) */
  235. 44, /* Linux (command line) */
  236. 28, /* Linux (short name) */
  237. 48, /* Linux PowerPC (command line) */
  238. 32, /* Linux PowerPC (short name) */
  239. 8, /* FreeBSD */
  240. };
  241. static const size_t prpsoffsets64[] = {
  242. #ifdef USE_NT_PSINFO
  243. 152, /* SunOS 5.x (command line) */
  244. 136, /* SunOS 5.x (short name) */
  245. #endif /* USE_NT_PSINFO */
  246. 136, /* SunOS 5.x, 64-bit (command line) */
  247. 120, /* SunOS 5.x, 64-bit (short name) */
  248. 56, /* Linux (command line) */
  249. 40, /* Linux (tested on core from 2.4.x, short name) */
  250. 16, /* FreeBSD, 64-bit */
  251. };
  252. #define NOFFSETS32 __arraycount(prpsoffsets32)
  253. #define NOFFSETS64 __arraycount(prpsoffsets64)
  254. #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
  255. /*
  256. * Look through the program headers of an executable image, searching
  257. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
  258. * "FreeBSD"; if one is found, try looking in various places in its
  259. * contents for a 16-character string containing only printable
  260. * characters - if found, that string should be the name of the program
  261. * that dropped core. Note: right after that 16-character string is,
  262. * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
  263. * Linux, a longer string (80 characters, in 5.x, probably other
  264. * SVR4-flavored systems, and Linux) containing the start of the
  265. * command line for that program.
  266. *
  267. * SunOS 5.x core files contain two PT_NOTE sections, with the types
  268. * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
  269. * same info about the command name and command line, so it probably
  270. * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
  271. * above (see USE_NT_PSINFO), in case we ever decide to do so. The
  272. * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
  273. * the SunOS 5.x file command relies on this (and prefers the latter).
  274. *
  275. * The signal number probably appears in a section of type NT_PRSTATUS,
  276. * but that's also rather OS-dependent, in ways that are harder to
  277. * dissect with heuristics, so I'm not bothering with the signal number.
  278. * (I suppose the signal number could be of interest in situations where
  279. * you don't have the binary of the program that dropped core; if you
  280. * *do* have that binary, the debugger will probably tell you what
  281. * signal it was.)
  282. */
  283. #define OS_STYLE_SVR4 0
  284. #define OS_STYLE_FREEBSD 1
  285. #define OS_STYLE_NETBSD 2
  286. private const char os_style_names[][8] = {
  287. "SVR4",
  288. "FreeBSD",
  289. "NetBSD",
  290. };
  291. #define FLAGS_CORE_STYLE 0x0003
  292. #define FLAGS_DID_CORE 0x0004
  293. #define FLAGS_DID_OS_NOTE 0x0008
  294. #define FLAGS_DID_BUILD_ID 0x0010
  295. #define FLAGS_DID_CORE_STYLE 0x0020
  296. #define FLAGS_DID_NETBSD_PAX 0x0040
  297. #define FLAGS_DID_NETBSD_MARCH 0x0080
  298. #define FLAGS_DID_NETBSD_CMODEL 0x0100
  299. #define FLAGS_DID_NETBSD_EMULATION 0x0200
  300. #define FLAGS_DID_NETBSD_UNKNOWN 0x0400
  301. #define FLAGS_IS_CORE 0x0800
  302. #define FLAGS_DID_AUXV 0x1000
  303. private int
  304. dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  305. int num, size_t size, off_t fsize, int *flags, uint16_t *notecount)
  306. {
  307. Elf32_Phdr ph32;
  308. Elf64_Phdr ph64;
  309. size_t offset, len;
  310. unsigned char nbuf[BUFSIZ];
  311. ssize_t bufsize;
  312. off_t ph_off = off, offs;
  313. int ph_num = num;
  314. if (ms->flags & MAGIC_MIME)
  315. return 0;
  316. if (num == 0) {
  317. if (file_printf(ms, ", no program header") == -1)
  318. return -1;
  319. return 0;
  320. }
  321. if (size != xph_sizeof) {
  322. if (file_printf(ms, ", corrupted program header size") == -1)
  323. return -1;
  324. return 0;
  325. }
  326. /*
  327. * Loop through all the program headers.
  328. */
  329. for ( ; num; num--) {
  330. if (pread(fd, xph_addr, xph_sizeof, off) <
  331. CAST(ssize_t, xph_sizeof)) {
  332. if (file_printf(ms,
  333. ", can't read elf program headers at %jd",
  334. (intmax_t)off) == -1)
  335. return -1;
  336. return 0;
  337. }
  338. off += size;
  339. if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
  340. /* Perhaps warn here */
  341. continue;
  342. }
  343. if (xph_type != PT_NOTE)
  344. continue;
  345. /*
  346. * This is a PT_NOTE section; loop through all the notes
  347. * in the section.
  348. */
  349. len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
  350. offs = xph_offset;
  351. if ((bufsize = pread(fd, nbuf, len, offs)) == -1) {
  352. if (file_printf(ms, " can't read note section at %jd",
  353. (intmax_t)offs) == -1)
  354. return -1;
  355. return 0;
  356. }
  357. offset = 0;
  358. for (;;) {
  359. if (offset >= CAST(size_t, bufsize))
  360. break;
  361. offset = donote(ms, nbuf, offset, CAST(size_t, bufsize),
  362. clazz, swap, 4, flags, notecount, fd, ph_off,
  363. ph_num, fsize);
  364. if (offset == 0)
  365. break;
  366. }
  367. }
  368. return 0;
  369. }
  370. #endif
  371. static int
  372. do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
  373. {
  374. uint32_t desc;
  375. memcpy(&desc, v, sizeof(desc));
  376. desc = elf_getu32(swap, desc);
  377. if (file_printf(ms, ", for NetBSD") == -1)
  378. return -1;
  379. /*
  380. * The version number used to be stuck as 199905, and was thus
  381. * basically content-free. Newer versions of NetBSD have fixed
  382. * this and now use the encoding of __NetBSD_Version__:
  383. *
  384. * MMmmrrpp00
  385. *
  386. * M = major version
  387. * m = minor version
  388. * r = release ["",A-Z,Z[A-Z] but numeric]
  389. * p = patchlevel
  390. */
  391. if (desc > 100000000U) {
  392. uint32_t ver_patch = (desc / 100) % 100;
  393. uint32_t ver_rel = (desc / 10000) % 100;
  394. uint32_t ver_min = (desc / 1000000) % 100;
  395. uint32_t ver_maj = desc / 100000000;
  396. if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
  397. return -1;
  398. if (ver_rel == 0 && ver_patch != 0) {
  399. if (file_printf(ms, ".%u", ver_patch) == -1)
  400. return -1;
  401. } else if (ver_rel != 0) {
  402. while (ver_rel > 26) {
  403. if (file_printf(ms, "Z") == -1)
  404. return -1;
  405. ver_rel -= 26;
  406. }
  407. if (file_printf(ms, "%c", 'A' + ver_rel - 1)
  408. == -1)
  409. return -1;
  410. }
  411. }
  412. return 0;
  413. }
  414. static int
  415. do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
  416. {
  417. uint32_t desc;
  418. memcpy(&desc, v, sizeof(desc));
  419. desc = elf_getu32(swap, desc);
  420. if (file_printf(ms, ", for FreeBSD") == -1)
  421. return -1;
  422. /*
  423. * Contents is __FreeBSD_version, whose relation to OS
  424. * versions is defined by a huge table in the Porter's
  425. * Handbook. This is the general scheme:
  426. *
  427. * Releases:
  428. * Mmp000 (before 4.10)
  429. * Mmi0p0 (before 5.0)
  430. * Mmm0p0
  431. *
  432. * Development branches:
  433. * Mmpxxx (before 4.6)
  434. * Mmp1xx (before 4.10)
  435. * Mmi1xx (before 5.0)
  436. * M000xx (pre-M.0)
  437. * Mmm1xx
  438. *
  439. * M = major version
  440. * m = minor version
  441. * i = minor version increment (491000 -> 4.10)
  442. * p = patchlevel
  443. * x = revision
  444. *
  445. * The first release of FreeBSD to use ELF by default
  446. * was version 3.0.
  447. */
  448. if (desc == 460002) {
  449. if (file_printf(ms, " 4.6.2") == -1)
  450. return -1;
  451. } else if (desc < 460100) {
  452. if (file_printf(ms, " %d.%d", desc / 100000,
  453. desc / 10000 % 10) == -1)
  454. return -1;
  455. if (desc / 1000 % 10 > 0)
  456. if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
  457. return -1;
  458. if ((desc % 1000 > 0) || (desc % 100000 == 0))
  459. if (file_printf(ms, " (%d)", desc) == -1)
  460. return -1;
  461. } else if (desc < 500000) {
  462. if (file_printf(ms, " %d.%d", desc / 100000,
  463. desc / 10000 % 10 + desc / 1000 % 10) == -1)
  464. return -1;
  465. if (desc / 100 % 10 > 0) {
  466. if (file_printf(ms, " (%d)", desc) == -1)
  467. return -1;
  468. } else if (desc / 10 % 10 > 0) {
  469. if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
  470. return -1;
  471. }
  472. } else {
  473. if (file_printf(ms, " %d.%d", desc / 100000,
  474. desc / 1000 % 100) == -1)
  475. return -1;
  476. if ((desc / 100 % 10 > 0) ||
  477. (desc % 100000 / 100 == 0)) {
  478. if (file_printf(ms, " (%d)", desc) == -1)
  479. return -1;
  480. } else if (desc / 10 % 10 > 0) {
  481. if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
  482. return -1;
  483. }
  484. }
  485. return 0;
  486. }
  487. private int
  488. /*ARGSUSED*/
  489. do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  490. int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz,
  491. size_t noff, size_t doff, int *flags)
  492. {
  493. if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "GNU") == 0 &&
  494. type == NT_GNU_BUILD_ID && (descsz >= 4 && descsz <= 20)) {
  495. uint8_t desc[20];
  496. const char *btype;
  497. uint32_t i;
  498. *flags |= FLAGS_DID_BUILD_ID;
  499. switch (descsz) {
  500. case 8:
  501. btype = "xxHash";
  502. break;
  503. case 16:
  504. btype = "md5/uuid";
  505. break;
  506. case 20:
  507. btype = "sha1";
  508. break;
  509. default:
  510. btype = "unknown";
  511. break;
  512. }
  513. if (file_printf(ms, ", BuildID[%s]=", btype) == -1)
  514. return -1;
  515. memcpy(desc, &nbuf[doff], descsz);
  516. for (i = 0; i < descsz; i++)
  517. if (file_printf(ms, "%02x", desc[i]) == -1)
  518. return -1;
  519. return 1;
  520. }
  521. if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "Go") == 0 &&
  522. type == NT_GO_BUILD_ID && descsz < 128) {
  523. char buf[256];
  524. if (file_printf(ms, ", Go BuildID=%s",
  525. file_copystr(buf, sizeof(buf), descsz,
  526. RCAST(const char *, &nbuf[doff]))) == -1)
  527. return -1;
  528. return 1;
  529. }
  530. return 0;
  531. }
  532. private int
  533. do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  534. int swap, uint32_t namesz, uint32_t descsz,
  535. size_t noff, size_t doff, int *flags)
  536. {
  537. const char *name = RCAST(const char *, &nbuf[noff]);
  538. if (namesz == 5 && strcmp(name, "SuSE") == 0 &&
  539. type == NT_GNU_VERSION && descsz == 2) {
  540. *flags |= FLAGS_DID_OS_NOTE;
  541. if (file_printf(ms, ", for SuSE %d.%d", nbuf[doff],
  542. nbuf[doff + 1]) == -1)
  543. return -1;
  544. return 1;
  545. }
  546. if (namesz == 4 && strcmp(name, "GNU") == 0 &&
  547. type == NT_GNU_VERSION && descsz == 16) {
  548. uint32_t desc[4];
  549. memcpy(desc, &nbuf[doff], sizeof(desc));
  550. *flags |= FLAGS_DID_OS_NOTE;
  551. if (file_printf(ms, ", for GNU/") == -1)
  552. return -1;
  553. switch (elf_getu32(swap, desc[0])) {
  554. case GNU_OS_LINUX:
  555. if (file_printf(ms, "Linux") == -1)
  556. return -1;
  557. break;
  558. case GNU_OS_HURD:
  559. if (file_printf(ms, "Hurd") == -1)
  560. return -1;
  561. break;
  562. case GNU_OS_SOLARIS:
  563. if (file_printf(ms, "Solaris") == -1)
  564. return -1;
  565. break;
  566. case GNU_OS_KFREEBSD:
  567. if (file_printf(ms, "kFreeBSD") == -1)
  568. return -1;
  569. break;
  570. case GNU_OS_KNETBSD:
  571. if (file_printf(ms, "kNetBSD") == -1)
  572. return -1;
  573. break;
  574. default:
  575. if (file_printf(ms, "<unknown>") == -1)
  576. return -1;
  577. }
  578. if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
  579. elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
  580. return -1;
  581. return 1;
  582. }
  583. if (namesz == 7 && strcmp(name, "NetBSD") == 0) {
  584. if (type == NT_NETBSD_VERSION && descsz == 4) {
  585. *flags |= FLAGS_DID_OS_NOTE;
  586. if (do_note_netbsd_version(ms, swap, &nbuf[doff]) == -1)
  587. return -1;
  588. return 1;
  589. }
  590. }
  591. if (namesz == 8 && strcmp(name, "FreeBSD") == 0) {
  592. if (type == NT_FREEBSD_VERSION && descsz == 4) {
  593. *flags |= FLAGS_DID_OS_NOTE;
  594. if (do_note_freebsd_version(ms, swap, &nbuf[doff])
  595. == -1)
  596. return -1;
  597. return 1;
  598. }
  599. }
  600. if (namesz == 8 && strcmp(name, "OpenBSD") == 0 &&
  601. type == NT_OPENBSD_VERSION && descsz == 4) {
  602. *flags |= FLAGS_DID_OS_NOTE;
  603. if (file_printf(ms, ", for OpenBSD") == -1)
  604. return -1;
  605. /* Content of note is always 0 */
  606. return 1;
  607. }
  608. if (namesz == 10 && strcmp(name, "DragonFly") == 0 &&
  609. type == NT_DRAGONFLY_VERSION && descsz == 4) {
  610. uint32_t desc;
  611. *flags |= FLAGS_DID_OS_NOTE;
  612. if (file_printf(ms, ", for DragonFly") == -1)
  613. return -1;
  614. memcpy(&desc, &nbuf[doff], sizeof(desc));
  615. desc = elf_getu32(swap, desc);
  616. if (file_printf(ms, " %d.%d.%d", desc / 100000,
  617. desc / 10000 % 10, desc % 10000) == -1)
  618. return -1;
  619. return 1;
  620. }
  621. return 0;
  622. }
  623. private int
  624. do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  625. int swap, uint32_t namesz, uint32_t descsz,
  626. size_t noff, size_t doff, int *flags)
  627. {
  628. const char *name = RCAST(const char *, &nbuf[noff]);
  629. if (namesz == 4 && strcmp(name, "PaX") == 0 &&
  630. type == NT_NETBSD_PAX && descsz == 4) {
  631. static const char *pax[] = {
  632. "+mprotect",
  633. "-mprotect",
  634. "+segvguard",
  635. "-segvguard",
  636. "+ASLR",
  637. "-ASLR",
  638. };
  639. uint32_t desc;
  640. size_t i;
  641. int did = 0;
  642. *flags |= FLAGS_DID_NETBSD_PAX;
  643. memcpy(&desc, &nbuf[doff], sizeof(desc));
  644. desc = elf_getu32(swap, desc);
  645. if (desc && file_printf(ms, ", PaX: ") == -1)
  646. return -1;
  647. for (i = 0; i < __arraycount(pax); i++) {
  648. if (((1 << CAST(int, i)) & desc) == 0)
  649. continue;
  650. if (file_printf(ms, "%s%s", did++ ? "," : "",
  651. pax[i]) == -1)
  652. return -1;
  653. }
  654. return 1;
  655. }
  656. return 0;
  657. }
  658. private int
  659. do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  660. int swap, uint32_t namesz, uint32_t descsz,
  661. size_t noff, size_t doff, int *flags, size_t size, int clazz)
  662. {
  663. #ifdef ELFCORE
  664. char buf[256];
  665. const char *name = RCAST(const char *, &nbuf[noff]);
  666. int os_style = -1;
  667. /*
  668. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  669. * least, doesn't correctly implement name
  670. * sections, in core dumps, as specified by
  671. * the "Program Linking" section of "UNIX(R) System
  672. * V Release 4 Programmer's Guide: ANSI C and
  673. * Programming Support Tools", because my copy
  674. * clearly says "The first 'namesz' bytes in 'name'
  675. * contain a *null-terminated* [emphasis mine]
  676. * character representation of the entry's owner
  677. * or originator", but the 2.0.36 kernel code
  678. * doesn't include the terminating null in the
  679. * name....
  680. */
  681. if ((namesz == 4 && strncmp(name, "CORE", 4) == 0) ||
  682. (namesz == 5 && strcmp(name, "CORE") == 0)) {
  683. os_style = OS_STYLE_SVR4;
  684. }
  685. if ((namesz == 8 && strcmp(name, "FreeBSD") == 0)) {
  686. os_style = OS_STYLE_FREEBSD;
  687. }
  688. if ((namesz >= 11 && strncmp(name, "NetBSD-CORE", 11)
  689. == 0)) {
  690. os_style = OS_STYLE_NETBSD;
  691. }
  692. if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
  693. if (file_printf(ms, ", %s-style", os_style_names[os_style])
  694. == -1)
  695. return -1;
  696. *flags |= FLAGS_DID_CORE_STYLE;
  697. *flags |= os_style;
  698. }
  699. switch (os_style) {
  700. case OS_STYLE_NETBSD:
  701. if (type == NT_NETBSD_CORE_PROCINFO) {
  702. char sbuf[512];
  703. struct NetBSD_elfcore_procinfo pi;
  704. memset(&pi, 0, sizeof(pi));
  705. memcpy(&pi, nbuf + doff, MIN(descsz, sizeof(pi)));
  706. if (file_printf(ms, ", from '%.31s', pid=%u, uid=%u, "
  707. "gid=%u, nlwps=%u, lwp=%u (signal %u/code %u)",
  708. file_printable(ms, sbuf, sizeof(sbuf),
  709. RCAST(char *, pi.cpi_name), sizeof(pi.cpi_name)),
  710. elf_getu32(swap, CAST(uint32_t, pi.cpi_pid)),
  711. elf_getu32(swap, pi.cpi_euid),
  712. elf_getu32(swap, pi.cpi_egid),
  713. elf_getu32(swap, pi.cpi_nlwps),
  714. elf_getu32(swap, CAST(uint32_t, pi.cpi_siglwp)),
  715. elf_getu32(swap, pi.cpi_signo),
  716. elf_getu32(swap, pi.cpi_sigcode)) == -1)
  717. return -1;
  718. *flags |= FLAGS_DID_CORE;
  719. return 1;
  720. }
  721. break;
  722. case OS_STYLE_FREEBSD:
  723. if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
  724. size_t argoff, pidoff;
  725. if (clazz == ELFCLASS32)
  726. argoff = 4 + 4 + 17;
  727. else
  728. argoff = 4 + 4 + 8 + 17;
  729. if (file_printf(ms, ", from '%.80s'", nbuf + doff +
  730. argoff) == -1)
  731. return -1;
  732. pidoff = argoff + 81 + 2;
  733. if (doff + pidoff + 4 <= size) {
  734. if (file_printf(ms, ", pid=%u",
  735. elf_getu32(swap, *RCAST(uint32_t *, (nbuf +
  736. doff + pidoff)))) == -1)
  737. return -1;
  738. }
  739. *flags |= FLAGS_DID_CORE;
  740. }
  741. break;
  742. default:
  743. if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
  744. size_t i, j;
  745. unsigned char c;
  746. /*
  747. * Extract the program name. We assume
  748. * it to be 16 characters (that's what it
  749. * is in SunOS 5.x and Linux).
  750. *
  751. * Unfortunately, it's at a different offset
  752. * in various OSes, so try multiple offsets.
  753. * If the characters aren't all printable,
  754. * reject it.
  755. */
  756. for (i = 0; i < NOFFSETS; i++) {
  757. unsigned char *cname, *cp;
  758. size_t reloffset = prpsoffsets(i);
  759. size_t noffset = doff + reloffset;
  760. size_t k;
  761. for (j = 0; j < 16; j++, noffset++,
  762. reloffset++) {
  763. /*
  764. * Make sure we're not past
  765. * the end of the buffer; if
  766. * we are, just give up.
  767. */
  768. if (noffset >= size)
  769. goto tryanother;
  770. /*
  771. * Make sure we're not past
  772. * the end of the contents;
  773. * if we are, this obviously
  774. * isn't the right offset.
  775. */
  776. if (reloffset >= descsz)
  777. goto tryanother;
  778. c = nbuf[noffset];
  779. if (c == '\0') {
  780. /*
  781. * A '\0' at the
  782. * beginning is
  783. * obviously wrong.
  784. * Any other '\0'
  785. * means we're done.
  786. */
  787. if (j == 0)
  788. goto tryanother;
  789. else
  790. break;
  791. } else {
  792. /*
  793. * A nonprintable
  794. * character is also
  795. * wrong.
  796. */
  797. if (!isprint(c) || isquote(c))
  798. goto tryanother;
  799. }
  800. }
  801. /*
  802. * Well, that worked.
  803. */
  804. /*
  805. * Try next offsets, in case this match is
  806. * in the middle of a string.
  807. */
  808. for (k = i + 1 ; k < NOFFSETS; k++) {
  809. size_t no;
  810. int adjust = 1;
  811. if (prpsoffsets(k) >= prpsoffsets(i))
  812. continue;
  813. for (no = doff + prpsoffsets(k);
  814. no < doff + prpsoffsets(i); no++)
  815. adjust = adjust
  816. && isprint(nbuf[no]);
  817. if (adjust)
  818. i = k;
  819. }
  820. cname = CAST(unsigned char *,
  821. &nbuf[doff + prpsoffsets(i)]);
  822. for (cp = cname; cp < nbuf + size && *cp
  823. && isprint(*cp); cp++)
  824. continue;
  825. /*
  826. * Linux apparently appends a space at the end
  827. * of the command line: remove it.
  828. */
  829. while (cp > cname && isspace(cp[-1]))
  830. cp--;
  831. if (file_printf(ms, ", from '%s'",
  832. file_copystr(buf, sizeof(buf),
  833. CAST(size_t, cp - cname),
  834. RCAST(char *, cname))) == -1)
  835. return -1;
  836. *flags |= FLAGS_DID_CORE;
  837. return 1;
  838. tryanother:
  839. ;
  840. }
  841. }
  842. break;
  843. }
  844. #endif
  845. return 0;
  846. }
  847. private off_t
  848. get_offset_from_virtaddr(struct magic_set *ms, int swap, int clazz, int fd,
  849. off_t off, int num, off_t fsize, uint64_t virtaddr)
  850. {
  851. Elf32_Phdr ph32;
  852. Elf64_Phdr ph64;
  853. /*
  854. * Loop through all the program headers and find the header with
  855. * virtual address in which the "virtaddr" belongs to.
  856. */
  857. for ( ; num; num--) {
  858. if (pread(fd, xph_addr, xph_sizeof, off) <
  859. CAST(ssize_t, xph_sizeof)) {
  860. if (file_printf(ms,
  861. ", can't read elf program header at %jd",
  862. (intmax_t)off) == -1)
  863. return -1;
  864. return 0;
  865. }
  866. off += xph_sizeof;
  867. if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
  868. /* Perhaps warn here */
  869. continue;
  870. }
  871. if (virtaddr >= xph_vaddr && virtaddr < xph_vaddr + xph_filesz)
  872. return xph_offset + (virtaddr - xph_vaddr);
  873. }
  874. return 0;
  875. }
  876. private size_t
  877. get_string_on_virtaddr(struct magic_set *ms,
  878. int swap, int clazz, int fd, off_t ph_off, int ph_num,
  879. off_t fsize, uint64_t virtaddr, char *buf, ssize_t buflen)
  880. {
  881. char *bptr;
  882. off_t offset;
  883. if (buflen == 0)
  884. return 0;
  885. offset = get_offset_from_virtaddr(ms, swap, clazz, fd, ph_off, ph_num,
  886. fsize, virtaddr);
  887. if (offset < 0 ||
  888. (buflen = pread(fd, buf, CAST(size_t, buflen), offset)) <= 0) {
  889. (void)file_printf(ms, ", can't read elf string at %jd",
  890. (intmax_t)offset);
  891. return 0;
  892. }
  893. buf[buflen - 1] = '\0';
  894. /* We expect only printable characters, so return if buffer contains
  895. * non-printable character before the '\0' or just '\0'. */
  896. for (bptr = buf; *bptr && isprint(CAST(unsigned char, *bptr)); bptr++)
  897. continue;
  898. if (*bptr != '\0')
  899. return 0;
  900. return bptr - buf;
  901. }
  902. /*ARGSUSED*/
  903. private int
  904. do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
  905. int swap, uint32_t namesz __attribute__((__unused__)),
  906. uint32_t descsz __attribute__((__unused__)),
  907. size_t noff __attribute__((__unused__)), size_t doff,
  908. int *flags, size_t size __attribute__((__unused__)), int clazz,
  909. int fd, off_t ph_off, int ph_num, off_t fsize)
  910. {
  911. #ifdef ELFCORE
  912. Aux32Info auxv32;
  913. Aux64Info auxv64;
  914. size_t elsize = xauxv_sizeof;
  915. const char *tag;
  916. int is_string;
  917. size_t nval;
  918. if ((*flags & (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE)) !=
  919. (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE))
  920. return 0;
  921. switch (*flags & FLAGS_CORE_STYLE) {
  922. case OS_STYLE_SVR4:
  923. if (type != NT_AUXV)
  924. return 0;
  925. break;
  926. #ifdef notyet
  927. case OS_STYLE_NETBSD:
  928. if (type != NT_NETBSD_CORE_AUXV)
  929. return 0;
  930. break;
  931. case OS_STYLE_FREEBSD:
  932. if (type != NT_FREEBSD_PROCSTAT_AUXV)
  933. return 0;
  934. break;
  935. #endif
  936. default:
  937. return 0;
  938. }
  939. *flags |= FLAGS_DID_AUXV;
  940. nval = 0;
  941. for (size_t off = 0; off + elsize <= descsz; off += elsize) {
  942. memcpy(xauxv_addr, &nbuf[doff + off], xauxv_sizeof);
  943. /* Limit processing to 50 vector entries to prevent DoS */
  944. if (nval++ >= 50) {
  945. file_error(ms, 0, "Too many ELF Auxv elements");
  946. return 1;
  947. }
  948. switch(xauxv_type) {
  949. case AT_LINUX_EXECFN:
  950. is_string = 1;
  951. tag = "execfn";
  952. break;
  953. case AT_LINUX_PLATFORM:
  954. is_string = 1;
  955. tag = "platform";
  956. break;
  957. case AT_LINUX_UID:
  958. is_string = 0;
  959. tag = "real uid";
  960. break;
  961. case AT_LINUX_GID:
  962. is_string = 0;
  963. tag = "real gid";
  964. break;
  965. case AT_LINUX_EUID:
  966. is_string = 0;
  967. tag = "effective uid";
  968. break;
  969. case AT_LINUX_EGID:
  970. is_string = 0;
  971. tag = "effective gid";
  972. break;
  973. default:
  974. is_string = 0;
  975. tag = NULL;
  976. break;
  977. }
  978. if (tag == NULL)
  979. continue;
  980. if (is_string) {
  981. char buf[256];
  982. ssize_t buflen;
  983. buflen = get_string_on_virtaddr(ms, swap, clazz, fd,
  984. ph_off, ph_num, fsize, xauxv_val, buf, sizeof(buf));
  985. if (buflen == 0)
  986. continue;
  987. if (file_printf(ms, ", %s: '%s'", tag, buf) == -1)
  988. return -1;
  989. } else {
  990. if (file_printf(ms, ", %s: %d", tag,
  991. CAST(int, xauxv_val)) == -1)
  992. return -1;
  993. }
  994. }
  995. return 1;
  996. #else
  997. return 0;
  998. #endif
  999. }
  1000. private size_t
  1001. dodynamic(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
  1002. int clazz, int swap, int *pie, size_t *need)
  1003. {
  1004. Elf32_Dyn dh32;
  1005. Elf64_Dyn dh64;
  1006. unsigned char *dbuf = CAST(unsigned char *, vbuf);
  1007. if (xdh_sizeof + offset > size) {
  1008. /*
  1009. * We're out of note headers.
  1010. */
  1011. return xdh_sizeof + offset;
  1012. }
  1013. memcpy(xdh_addr, &dbuf[offset], xdh_sizeof);
  1014. offset += xdh_sizeof;
  1015. switch (xdh_tag) {
  1016. case DT_FLAGS_1:
  1017. *pie = 1;
  1018. if (xdh_val & DF_1_PIE)
  1019. ms->mode |= 0111;
  1020. else
  1021. ms->mode &= ~0111;
  1022. break;
  1023. case DT_NEEDED:
  1024. (*need)++;
  1025. break;
  1026. default:
  1027. break;
  1028. }
  1029. return offset;
  1030. }
  1031. private size_t
  1032. donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
  1033. int clazz, int swap, size_t align, int *flags, uint16_t *notecount,
  1034. int fd, off_t ph_off, int ph_num, off_t fsize)
  1035. {
  1036. Elf32_Nhdr nh32;
  1037. Elf64_Nhdr nh64;
  1038. size_t noff, doff;
  1039. uint32_t namesz, descsz;
  1040. char buf[256];
  1041. unsigned char *nbuf = CAST(unsigned char *, vbuf);
  1042. if (*notecount == 0)
  1043. return 0;
  1044. --*notecount;
  1045. if (xnh_sizeof + offset > size) {
  1046. /*
  1047. * We're out of note headers.
  1048. */
  1049. return xnh_sizeof + offset;
  1050. }
  1051. /*XXX: GCC */
  1052. memset(&nh32, 0, sizeof(nh32));
  1053. memset(&nh64, 0, sizeof(nh64));
  1054. memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
  1055. offset += xnh_sizeof;
  1056. namesz = xnh_namesz;
  1057. descsz = xnh_descsz;
  1058. if ((namesz == 0) && (descsz == 0)) {
  1059. /*
  1060. * We're out of note headers.
  1061. */
  1062. return (offset >= size) ? offset : size;
  1063. }
  1064. if (namesz & 0x80000000) {
  1065. (void)file_printf(ms, ", bad note name size %#lx",
  1066. CAST(unsigned long, namesz));
  1067. return 0;
  1068. }
  1069. if (descsz & 0x80000000) {
  1070. (void)file_printf(ms, ", bad note description size %#lx",
  1071. CAST(unsigned long, descsz));
  1072. return 0;
  1073. }
  1074. noff = offset;
  1075. doff = ELF_ALIGN(offset + namesz);
  1076. if (offset + namesz > size) {
  1077. /*
  1078. * We're past the end of the buffer.
  1079. */
  1080. return doff;
  1081. }
  1082. offset = ELF_ALIGN(doff + descsz);
  1083. if (doff + descsz > size) {
  1084. /*
  1085. * We're past the end of the buffer.
  1086. */
  1087. return (offset >= size) ? offset : size;
  1088. }
  1089. if ((*flags & FLAGS_DID_OS_NOTE) == 0) {
  1090. if (do_os_note(ms, nbuf, xnh_type, swap,
  1091. namesz, descsz, noff, doff, flags))
  1092. return offset;
  1093. }
  1094. if ((*flags & FLAGS_DID_BUILD_ID) == 0) {
  1095. if (do_bid_note(ms, nbuf, xnh_type, swap,
  1096. namesz, descsz, noff, doff, flags))
  1097. return offset;
  1098. }
  1099. if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) {
  1100. if (do_pax_note(ms, nbuf, xnh_type, swap,
  1101. namesz, descsz, noff, doff, flags))
  1102. return offset;
  1103. }
  1104. if ((*flags & FLAGS_DID_CORE) == 0) {
  1105. if (do_core_note(ms, nbuf, xnh_type, swap,
  1106. namesz, descsz, noff, doff, flags, size, clazz))
  1107. return offset;
  1108. }
  1109. if ((*flags & FLAGS_DID_AUXV) == 0) {
  1110. if (do_auxv_note(ms, nbuf, xnh_type, swap,
  1111. namesz, descsz, noff, doff, flags, size, clazz,
  1112. fd, ph_off, ph_num, fsize))
  1113. return offset;
  1114. }
  1115. if (namesz == 7 && strcmp(RCAST(char *, &nbuf[noff]), "NetBSD") == 0) {
  1116. int descw, flag;
  1117. const char *str, *tag;
  1118. if (descsz > 100)
  1119. descsz = 100;
  1120. switch (xnh_type) {
  1121. case NT_NETBSD_VERSION:
  1122. return offset;
  1123. case NT_NETBSD_MARCH:
  1124. flag = FLAGS_DID_NETBSD_MARCH;
  1125. tag = "compiled for";
  1126. break;
  1127. case NT_NETBSD_CMODEL:
  1128. flag = FLAGS_DID_NETBSD_CMODEL;
  1129. tag = "compiler model";
  1130. break;
  1131. case NT_NETBSD_EMULATION:
  1132. flag = FLAGS_DID_NETBSD_EMULATION;
  1133. tag = "emulation:";
  1134. break;
  1135. default:
  1136. if (*flags & FLAGS_DID_NETBSD_UNKNOWN)
  1137. return offset;
  1138. *flags |= FLAGS_DID_NETBSD_UNKNOWN;
  1139. if (file_printf(ms, ", note=%u", xnh_type) == -1)
  1140. return offset;
  1141. return offset;
  1142. }
  1143. if (*flags & flag)
  1144. return offset;
  1145. str = RCAST(const char *, &nbuf[doff]);
  1146. descw = CAST(int, descsz);
  1147. *flags |= flag;
  1148. file_printf(ms, ", %s: %s", tag,
  1149. file_copystr(buf, sizeof(buf), descw, str));
  1150. return offset;
  1151. }
  1152. return offset;
  1153. }
  1154. /* SunOS 5.x hardware capability descriptions */
  1155. typedef struct cap_desc {
  1156. uint64_t cd_mask;
  1157. const char *cd_name;
  1158. } cap_desc_t;
  1159. static const cap_desc_t cap_desc_sparc[] = {
  1160. { AV_SPARC_MUL32, "MUL32" },
  1161. { AV_SPARC_DIV32, "DIV32" },
  1162. { AV_SPARC_FSMULD, "FSMULD" },
  1163. { AV_SPARC_V8PLUS, "V8PLUS" },
  1164. { AV_SPARC_POPC, "POPC" },
  1165. { AV_SPARC_VIS, "VIS" },
  1166. { AV_SPARC_VIS2, "VIS2" },
  1167. { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
  1168. { AV_SPARC_FMAF, "FMAF" },
  1169. { AV_SPARC_FJFMAU, "FJFMAU" },
  1170. { AV_SPARC_IMA, "IMA" },
  1171. { 0, NULL }
  1172. };
  1173. static const cap_desc_t cap_desc_386[] = {
  1174. { AV_386_FPU, "FPU" },
  1175. { AV_386_TSC, "TSC" },
  1176. { AV_386_CX8, "CX8" },
  1177. { AV_386_SEP, "SEP" },
  1178. { AV_386_AMD_SYSC, "AMD_SYSC" },
  1179. { AV_386_CMOV, "CMOV" },
  1180. { AV_386_MMX, "MMX" },
  1181. { AV_386_AMD_MMX, "AMD_MMX" },
  1182. { AV_386_AMD_3DNow, "AMD_3DNow" },
  1183. { AV_386_AMD_3DNowx, "AMD_3DNowx" },
  1184. { AV_386_FXSR, "FXSR" },
  1185. { AV_386_SSE, "SSE" },
  1186. { AV_386_SSE2, "SSE2" },
  1187. { AV_386_PAUSE, "PAUSE" },
  1188. { AV_386_SSE3, "SSE3" },
  1189. { AV_386_MON, "MON" },
  1190. { AV_386_CX16, "CX16" },
  1191. { AV_386_AHF, "AHF" },
  1192. { AV_386_TSCP, "TSCP" },
  1193. { AV_386_AMD_SSE4A, "AMD_SSE4A" },
  1194. { AV_386_POPCNT, "POPCNT" },
  1195. { AV_386_AMD_LZCNT, "AMD_LZCNT" },
  1196. { AV_386_SSSE3, "SSSE3" },
  1197. { AV_386_SSE4_1, "SSE4.1" },
  1198. { AV_386_SSE4_2, "SSE4.2" },
  1199. { 0, NULL }
  1200. };
  1201. private int
  1202. doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
  1203. size_t size, off_t fsize, int mach, int strtab, int *flags,
  1204. uint16_t *notecount)
  1205. {
  1206. Elf32_Shdr sh32;
  1207. Elf64_Shdr sh64;
  1208. int stripped = 1, has_debug_info = 0;
  1209. size_t nbadcap = 0;
  1210. void *nbuf;
  1211. off_t noff, coff, name_off, offs;
  1212. uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilities */
  1213. uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilities */
  1214. char name[50];
  1215. ssize_t namesize;
  1216. if (ms->flags & MAGIC_MIME)
  1217. return 0;
  1218. if (num == 0) {
  1219. if (file_printf(ms, ", no section header") == -1)
  1220. return -1;
  1221. return 0;
  1222. }
  1223. if (size != xsh_sizeof) {
  1224. if (file_printf(ms, ", corrupted section header size") == -1)
  1225. return -1;
  1226. return 0;
  1227. }
  1228. /* Read offset of name section to be able to read section names later */
  1229. offs = CAST(off_t, (off + size * strtab));
  1230. if (pread(fd, xsh_addr, xsh_sizeof, offs) < CAST(ssize_t, xsh_sizeof)) {
  1231. if (file_printf(ms, ", missing section headers at %jd",
  1232. (intmax_t)offs) == -1)
  1233. return -1;
  1234. return 0;
  1235. }
  1236. name_off = xsh_offset;
  1237. if (fsize != SIZE_UNKNOWN && fsize < name_off) {
  1238. if (file_printf(ms, ", too large section header offset %jd",
  1239. (intmax_t)name_off) == -1)
  1240. return -1;
  1241. return 0;
  1242. }
  1243. for ( ; num; num--) {
  1244. /* Read the name of this section. */
  1245. offs = name_off + xsh_name;
  1246. if ((namesize = pread(fd, name, sizeof(name) - 1, offs))
  1247. == -1) {
  1248. if (file_printf(ms,
  1249. ", can't read name of elf section at %jd",
  1250. (intmax_t)offs) == -1)
  1251. return -1;
  1252. return 0;
  1253. }
  1254. name[namesize] = '\0';
  1255. if (strcmp(name, ".debug_info") == 0) {
  1256. has_debug_info = 1;
  1257. stripped = 0;
  1258. }
  1259. if (pread(fd, xsh_addr, xsh_sizeof, off) <
  1260. CAST(ssize_t, xsh_sizeof)) {
  1261. if (file_printf(ms, ", can't read elf section at %jd",
  1262. (intmax_t)off) == -1)
  1263. return -1;
  1264. return 0;
  1265. }
  1266. off += size;
  1267. /* Things we can determine before we seek */
  1268. switch (xsh_type) {
  1269. case SHT_SYMTAB:
  1270. #if 0
  1271. case SHT_DYNSYM:
  1272. #endif
  1273. stripped = 0;
  1274. break;
  1275. default:
  1276. if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
  1277. /* Perhaps warn here */
  1278. continue;
  1279. }
  1280. break;
  1281. }
  1282. /* Things we can determine when we seek */
  1283. switch (xsh_type) {
  1284. case SHT_NOTE:
  1285. if (CAST(uintmax_t, (xsh_size + xsh_offset)) >
  1286. CAST(uintmax_t, fsize)) {
  1287. if (file_printf(ms,
  1288. ", note offset/size %#" INTMAX_T_FORMAT
  1289. "x+%#" INTMAX_T_FORMAT "x exceeds"
  1290. " file size %#" INTMAX_T_FORMAT "x",
  1291. CAST(uintmax_t, xsh_offset),
  1292. CAST(uintmax_t, xsh_size),
  1293. CAST(uintmax_t, fsize)) == -1)
  1294. return -1;
  1295. return 0;
  1296. }
  1297. if ((nbuf = malloc(xsh_size)) == NULL) {
  1298. file_error(ms, errno, "Cannot allocate memory"
  1299. " for note");
  1300. return -1;
  1301. }
  1302. offs = xsh_offset;
  1303. if (pread(fd, nbuf, xsh_size, offs) <
  1304. CAST(ssize_t, xsh_size)) {
  1305. free(nbuf);
  1306. if (file_printf(ms,
  1307. ", can't read elf note at %jd",
  1308. (intmax_t)offs) == -1)
  1309. return -1;
  1310. return 0;
  1311. }
  1312. noff = 0;
  1313. for (;;) {
  1314. if (noff >= CAST(off_t, xsh_size))
  1315. break;
  1316. noff = donote(ms, nbuf, CAST(size_t, noff),
  1317. xsh_size, clazz, swap, 4, flags, notecount,
  1318. fd, 0, 0, 0);
  1319. if (noff == 0)
  1320. break;
  1321. }
  1322. free(nbuf);
  1323. break;
  1324. case SHT_SUNW_cap:
  1325. switch (mach) {
  1326. case EM_SPARC:
  1327. case EM_SPARCV9:
  1328. case EM_IA_64:
  1329. case EM_386:
  1330. case EM_AMD64:
  1331. break;
  1332. default:
  1333. goto skip;
  1334. }
  1335. if (nbadcap > 5)
  1336. break;
  1337. if (lseek(fd, xsh_offset, SEEK_SET)
  1338. == CAST(off_t, -1)) {
  1339. file_badseek(ms);
  1340. return -1;
  1341. }
  1342. coff = 0;
  1343. for (;;) {
  1344. Elf32_Cap cap32;
  1345. Elf64_Cap cap64;
  1346. char cbuf[/*CONSTCOND*/
  1347. MAX(sizeof(cap32), sizeof(cap64))];
  1348. if ((coff += xcap_sizeof) >
  1349. CAST(off_t, xsh_size))
  1350. break;
  1351. if (read(fd, cbuf, CAST(size_t, xcap_sizeof)) !=
  1352. CAST(ssize_t, xcap_sizeof)) {
  1353. file_badread(ms);
  1354. return -1;
  1355. }
  1356. if (cbuf[0] == 'A') {
  1357. #ifdef notyet
  1358. char *p = cbuf + 1;
  1359. uint32_t len, tag;
  1360. memcpy(&len, p, sizeof(len));
  1361. p += 4;
  1362. len = getu32(swap, len);
  1363. if (memcmp("gnu", p, 3) != 0) {
  1364. if (file_printf(ms,
  1365. ", unknown capability %.3s", p)
  1366. == -1)
  1367. return -1;
  1368. break;
  1369. }
  1370. p += strlen(p) + 1;
  1371. tag = *p++;
  1372. memcpy(&len, p, sizeof(len));
  1373. p += 4;
  1374. len = getu32(swap, len);
  1375. if (tag != 1) {
  1376. if (file_printf(ms, ", unknown gnu"
  1377. " capability tag %d", tag)
  1378. == -1)
  1379. return -1;
  1380. break;
  1381. }
  1382. // gnu attributes
  1383. #endif
  1384. break;
  1385. }
  1386. memcpy(xcap_addr, cbuf, xcap_sizeof);
  1387. switch (xcap_tag) {
  1388. case CA_SUNW_NULL:
  1389. break;
  1390. case CA_SUNW_HW_1:
  1391. cap_hw1 |= xcap_val;
  1392. break;
  1393. case CA_SUNW_SF_1:
  1394. cap_sf1 |= xcap_val;
  1395. break;
  1396. default:
  1397. if (file_printf(ms,
  1398. ", with unknown capability "
  1399. "%#" INT64_T_FORMAT "x = %#"
  1400. INT64_T_FORMAT "x",
  1401. CAST(unsigned long long, xcap_tag),
  1402. CAST(unsigned long long, xcap_val))
  1403. == -1)
  1404. return -1;
  1405. if (nbadcap++ > 2)
  1406. coff = xsh_size;
  1407. break;
  1408. }
  1409. }
  1410. /*FALLTHROUGH*/
  1411. skip:
  1412. default:
  1413. break;
  1414. }
  1415. }
  1416. if (has_debug_info) {
  1417. if (file_printf(ms, ", with debug_info") == -1)
  1418. return -1;
  1419. }
  1420. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  1421. return -1;
  1422. if (cap_hw1) {
  1423. const cap_desc_t *cdp;
  1424. switch (mach) {
  1425. case EM_SPARC:
  1426. case EM_SPARC32PLUS:
  1427. case EM_SPARCV9:
  1428. cdp = cap_desc_sparc;
  1429. break;
  1430. case EM_386:
  1431. case EM_IA_64:
  1432. case EM_AMD64:
  1433. cdp = cap_desc_386;
  1434. break;
  1435. default:
  1436. cdp = NULL;
  1437. break;
  1438. }
  1439. if (file_printf(ms, ", uses") == -1)
  1440. return -1;
  1441. if (cdp) {
  1442. while (cdp->cd_name) {
  1443. if (cap_hw1 & cdp->cd_mask) {
  1444. if (file_printf(ms,
  1445. " %s", cdp->cd_name) == -1)
  1446. return -1;
  1447. cap_hw1 &= ~cdp->cd_mask;
  1448. }
  1449. ++cdp;
  1450. }
  1451. if (cap_hw1)
  1452. if (file_printf(ms,
  1453. " unknown hardware capability %#"
  1454. INT64_T_FORMAT "x",
  1455. CAST(unsigned long long, cap_hw1)) == -1)
  1456. return -1;
  1457. } else {
  1458. if (file_printf(ms,
  1459. " hardware capability %#" INT64_T_FORMAT "x",
  1460. CAST(unsigned long long, cap_hw1)) == -1)
  1461. return -1;
  1462. }
  1463. }
  1464. if (cap_sf1) {
  1465. if (cap_sf1 & SF1_SUNW_FPUSED) {
  1466. if (file_printf(ms,
  1467. (cap_sf1 & SF1_SUNW_FPKNWN)
  1468. ? ", uses frame pointer"
  1469. : ", not known to use frame pointer") == -1)
  1470. return -1;
  1471. }
  1472. cap_sf1 &= ~SF1_SUNW_MASK;
  1473. if (cap_sf1)
  1474. if (file_printf(ms,
  1475. ", with unknown software capability %#"
  1476. INT64_T_FORMAT "x",
  1477. CAST(unsigned long long, cap_sf1)) == -1)
  1478. return -1;
  1479. }
  1480. return 0;
  1481. }
  1482. /*
  1483. * Look through the program headers of an executable image, to determine
  1484. * if it is statically or dynamically linked. If it has a dynamic section,
  1485. * it is pie, and does not have an interpreter or needed libraries, we
  1486. * call it static pie.
  1487. */
  1488. private int
  1489. dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  1490. int num, size_t size, off_t fsize, int sh_num, int *flags,
  1491. uint16_t *notecount)
  1492. {
  1493. Elf32_Phdr ph32;
  1494. Elf64_Phdr ph64;
  1495. const char *linking_style;
  1496. unsigned char nbuf[BUFSIZ];
  1497. char ibuf[BUFSIZ];
  1498. char interp[BUFSIZ];
  1499. ssize_t bufsize;
  1500. size_t offset, align, len, need = 0;
  1501. int pie = 0, dynamic = 0;
  1502. if (num == 0) {
  1503. if (file_printf(ms, ", no program header") == -1)
  1504. return -1;
  1505. return 0;
  1506. }
  1507. if (size != xph_sizeof) {
  1508. if (file_printf(ms, ", corrupted program header size") == -1)
  1509. return -1;
  1510. return 0;
  1511. }
  1512. interp[0] = '\0';
  1513. for ( ; num; num--) {
  1514. int doread;
  1515. if (pread(fd, xph_addr, xph_sizeof, off) <
  1516. CAST(ssize_t, xph_sizeof)) {
  1517. if (file_printf(ms,
  1518. ", can't read elf program headers at %jd",
  1519. (intmax_t)off) == -1)
  1520. return -1;
  1521. return 0;
  1522. }
  1523. off += size;
  1524. bufsize = 0;
  1525. align = 4;
  1526. /* Things we can determine before we seek */
  1527. switch (xph_type) {
  1528. case PT_DYNAMIC:
  1529. doread = 1;
  1530. break;
  1531. case PT_NOTE:
  1532. if (sh_num) /* Did this through section headers */
  1533. continue;
  1534. if (((align = xph_align) & 0x80000000UL) != 0 ||
  1535. align < 4) {
  1536. if (file_printf(ms,
  1537. ", invalid note alignment %#lx",
  1538. CAST(unsigned long, align)) == -1)
  1539. return -1;
  1540. align = 4;
  1541. }
  1542. /*FALLTHROUGH*/
  1543. case PT_INTERP:
  1544. doread = 1;
  1545. break;
  1546. default:
  1547. doread = 0;
  1548. if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
  1549. /* Maybe warn here? */
  1550. continue;
  1551. }
  1552. break;
  1553. }
  1554. if (doread) {
  1555. len = xph_filesz < sizeof(nbuf) ? xph_filesz
  1556. : sizeof(nbuf);
  1557. off_t offs = xph_offset;
  1558. bufsize = pread(fd, nbuf, len, offs);
  1559. if (bufsize == -1) {
  1560. if (file_printf(ms,
  1561. ", can't read section at %jd",
  1562. (intmax_t)offs) == -1)
  1563. return -1;
  1564. return 0;
  1565. }
  1566. } else
  1567. len = 0;
  1568. /* Things we can determine when we seek */
  1569. switch (xph_type) {
  1570. case PT_DYNAMIC:
  1571. dynamic = 1;
  1572. offset = 0;
  1573. // Let DF_1 determine if we are PIE or not.
  1574. ms->mode &= ~0111;
  1575. for (;;) {
  1576. if (offset >= CAST(size_t, bufsize))
  1577. break;
  1578. offset = dodynamic(ms, nbuf, offset,
  1579. CAST(size_t, bufsize), clazz, swap,
  1580. &pie, &need);
  1581. if (offset == 0)
  1582. break;
  1583. }
  1584. if (ms->flags & MAGIC_MIME)
  1585. continue;
  1586. break;
  1587. case PT_INTERP:
  1588. need++;
  1589. if (ms->flags & MAGIC_MIME)
  1590. continue;
  1591. if (bufsize && nbuf[0]) {
  1592. nbuf[bufsize - 1] = '\0';
  1593. memcpy(interp, nbuf, CAST(size_t, bufsize));
  1594. } else
  1595. strlcpy(interp, "*empty*", sizeof(interp));
  1596. break;
  1597. case PT_NOTE:
  1598. if (ms->flags & MAGIC_MIME)
  1599. return 0;
  1600. /*
  1601. * This is a PT_NOTE section; loop through all the notes
  1602. * in the section.
  1603. */
  1604. offset = 0;
  1605. for (;;) {
  1606. if (offset >= CAST(size_t, bufsize))
  1607. break;
  1608. offset = donote(ms, nbuf, offset,
  1609. CAST(size_t, bufsize), clazz, swap, align,
  1610. flags, notecount, fd, 0, 0, 0);
  1611. if (offset == 0)
  1612. break;
  1613. }
  1614. break;
  1615. default:
  1616. if (ms->flags & MAGIC_MIME)
  1617. continue;
  1618. break;
  1619. }
  1620. }
  1621. if (ms->flags & MAGIC_MIME)
  1622. return 0;
  1623. if (dynamic) {
  1624. if (pie && need == 0)
  1625. linking_style = "static-pie";
  1626. else
  1627. linking_style = "dynamically";
  1628. } else {
  1629. linking_style = "statically";
  1630. }
  1631. if (file_printf(ms, ", %s linked", linking_style) == -1)
  1632. return -1;
  1633. if (interp[0])
  1634. if (file_printf(ms, ", interpreter %s", file_printable(ms,
  1635. ibuf, sizeof(ibuf), interp, sizeof(interp))) == -1)
  1636. return -1;
  1637. return 0;
  1638. }
  1639. protected int
  1640. file_tryelf(struct magic_set *ms, const struct buffer *b)
  1641. {
  1642. int fd = b->fd;
  1643. const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
  1644. size_t nbytes = b->flen;
  1645. union {
  1646. int32_t l;
  1647. char c[sizeof(int32_t)];
  1648. } u;
  1649. int clazz;
  1650. int swap;
  1651. struct stat st;
  1652. const struct stat *stp;
  1653. off_t fsize;
  1654. int flags = 0;
  1655. Elf32_Ehdr elf32hdr;
  1656. Elf64_Ehdr elf64hdr;
  1657. uint16_t type, phnum, shnum, notecount;
  1658. if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
  1659. return 0;
  1660. /*
  1661. * ELF executables have multiple section headers in arbitrary
  1662. * file locations and thus file(1) cannot determine it from easily.
  1663. * Instead we traverse thru all section headers until a symbol table
  1664. * one is found or else the binary is stripped.
  1665. * Return immediately if it's not ELF (so we avoid pipe2file unless
  1666. * needed).
  1667. */
  1668. if (buf[EI_MAG0] != ELFMAG0
  1669. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  1670. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  1671. return 0;
  1672. /*
  1673. * If we cannot seek, it must be a pipe, socket or fifo.
  1674. */
  1675. if((lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(off_t, -1))
  1676. && (errno == ESPIPE))
  1677. fd = file_pipe2file(ms, fd, buf, nbytes);
  1678. if (fd == -1) {
  1679. file_badread(ms);
  1680. return -1;
  1681. }
  1682. stp = &b->st;
  1683. /*
  1684. * b->st.st_size != 0 if previous fstat() succeeded,
  1685. * which is likely, we can avoid extra stat() call.
  1686. */
  1687. if (b->st.st_size == 0) {
  1688. stp = &st;
  1689. if (fstat(fd, &st) == -1) {
  1690. file_badread(ms);
  1691. return -1;
  1692. }
  1693. }
  1694. if (S_ISREG(stp->st_mode) || stp->st_size != 0)
  1695. fsize = stp->st_size;
  1696. else
  1697. fsize = SIZE_UNKNOWN;
  1698. clazz = buf[EI_CLASS];
  1699. switch (clazz) {
  1700. case ELFCLASS32:
  1701. #undef elf_getu
  1702. #define elf_getu(a, b) elf_getu32(a, b)
  1703. #undef elfhdr
  1704. #define elfhdr elf32hdr
  1705. #include "elfclass.h"
  1706. case ELFCLASS64:
  1707. #undef elf_getu
  1708. #define elf_getu(a, b) elf_getu64(a, b)
  1709. #undef elfhdr
  1710. #define elfhdr elf64hdr
  1711. #include "elfclass.h"
  1712. default:
  1713. if (file_printf(ms, ", unknown class %d", clazz) == -1)
  1714. return -1;
  1715. break;
  1716. }
  1717. return 0;
  1718. }
  1719. #endif