readelf.c 45 KB

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