readelf.c 29 KB

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