readelf.c 31 KB

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