readelf.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  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.94 2012/12/13 13:48:31 christos Exp $")
  30. #endif
  31. #ifdef BUILTIN_ELF
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #include "readelf.h"
  39. #include "magic.h"
  40. #ifdef ELFCORE
  41. private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
  42. off_t, int *);
  43. #endif
  44. private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
  45. off_t, int *, int);
  46. private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
  47. off_t, int *, int);
  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. #ifndef USE_ARRAY_FOR_64BIT_TYPES
  55. private uint64_t getu64(int, uint64_t);
  56. #endif
  57. private uint16_t
  58. getu16(int swap, uint16_t value)
  59. {
  60. union {
  61. uint16_t ui;
  62. char c[2];
  63. } retval, tmpval;
  64. if (swap) {
  65. tmpval.ui = value;
  66. retval.c[0] = tmpval.c[1];
  67. retval.c[1] = tmpval.c[0];
  68. return retval.ui;
  69. } else
  70. return value;
  71. }
  72. private uint32_t
  73. getu32(int swap, uint32_t value)
  74. {
  75. union {
  76. uint32_t ui;
  77. char c[4];
  78. } retval, tmpval;
  79. if (swap) {
  80. tmpval.ui = value;
  81. retval.c[0] = tmpval.c[3];
  82. retval.c[1] = tmpval.c[2];
  83. retval.c[2] = tmpval.c[1];
  84. retval.c[3] = tmpval.c[0];
  85. return retval.ui;
  86. } else
  87. return value;
  88. }
  89. #ifndef USE_ARRAY_FOR_64BIT_TYPES
  90. private uint64_t
  91. getu64(int swap, uint64_t value)
  92. {
  93. union {
  94. uint64_t ui;
  95. char c[8];
  96. } retval, tmpval;
  97. if (swap) {
  98. tmpval.ui = value;
  99. retval.c[0] = tmpval.c[7];
  100. retval.c[1] = tmpval.c[6];
  101. retval.c[2] = tmpval.c[5];
  102. retval.c[3] = tmpval.c[4];
  103. retval.c[4] = tmpval.c[3];
  104. retval.c[5] = tmpval.c[2];
  105. retval.c[6] = tmpval.c[1];
  106. retval.c[7] = tmpval.c[0];
  107. return retval.ui;
  108. } else
  109. return value;
  110. }
  111. #endif
  112. #define elf_getu16(swap, value) getu16(swap, value)
  113. #define elf_getu32(swap, value) getu32(swap, value)
  114. #ifdef USE_ARRAY_FOR_64BIT_TYPES
  115. # define elf_getu64(swap, array) \
  116. ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
  117. (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
  118. #else
  119. # define elf_getu64(swap, value) getu64(swap, value)
  120. #endif
  121. #define xsh_addr (clazz == ELFCLASS32 \
  122. ? (void *) &sh32 \
  123. : (void *) &sh64)
  124. #define xsh_sizeof (clazz == ELFCLASS32 \
  125. ? sizeof sh32 \
  126. : sizeof sh64)
  127. #define xsh_size (clazz == ELFCLASS32 \
  128. ? elf_getu32(swap, sh32.sh_size) \
  129. : elf_getu64(swap, sh64.sh_size))
  130. #define xsh_offset (off_t)(clazz == ELFCLASS32 \
  131. ? elf_getu32(swap, sh32.sh_offset) \
  132. : elf_getu64(swap, sh64.sh_offset))
  133. #define xsh_type (clazz == ELFCLASS32 \
  134. ? elf_getu32(swap, sh32.sh_type) \
  135. : elf_getu32(swap, sh64.sh_type))
  136. #define xph_addr (clazz == ELFCLASS32 \
  137. ? (void *) &ph32 \
  138. : (void *) &ph64)
  139. #define xph_sizeof (clazz == ELFCLASS32 \
  140. ? sizeof ph32 \
  141. : sizeof ph64)
  142. #define xph_type (clazz == ELFCLASS32 \
  143. ? elf_getu32(swap, ph32.p_type) \
  144. : elf_getu32(swap, ph64.p_type))
  145. #define xph_offset (off_t)(clazz == ELFCLASS32 \
  146. ? elf_getu32(swap, ph32.p_offset) \
  147. : elf_getu64(swap, ph64.p_offset))
  148. #define xph_align (size_t)((clazz == ELFCLASS32 \
  149. ? (off_t) (ph32.p_align ? \
  150. elf_getu32(swap, ph32.p_align) : 4) \
  151. : (off_t) (ph64.p_align ? \
  152. elf_getu64(swap, ph64.p_align) : 4)))
  153. #define xph_filesz (size_t)((clazz == ELFCLASS32 \
  154. ? elf_getu32(swap, ph32.p_filesz) \
  155. : elf_getu64(swap, ph64.p_filesz)))
  156. #define xnh_addr (clazz == ELFCLASS32 \
  157. ? (void *) &nh32 \
  158. : (void *) &nh64)
  159. #define xph_memsz (size_t)((clazz == ELFCLASS32 \
  160. ? elf_getu32(swap, ph32.p_memsz) \
  161. : elf_getu64(swap, ph64.p_memsz)))
  162. #define xnh_sizeof (clazz == ELFCLASS32 \
  163. ? sizeof nh32 \
  164. : sizeof nh64)
  165. #define xnh_type (clazz == ELFCLASS32 \
  166. ? elf_getu32(swap, nh32.n_type) \
  167. : elf_getu32(swap, nh64.n_type))
  168. #define xnh_namesz (clazz == ELFCLASS32 \
  169. ? elf_getu32(swap, nh32.n_namesz) \
  170. : elf_getu32(swap, nh64.n_namesz))
  171. #define xnh_descsz (clazz == ELFCLASS32 \
  172. ? elf_getu32(swap, nh32.n_descsz) \
  173. : elf_getu32(swap, nh64.n_descsz))
  174. #define prpsoffsets(i) (clazz == ELFCLASS32 \
  175. ? prpsoffsets32[i] \
  176. : prpsoffsets64[i])
  177. #define xcap_addr (clazz == ELFCLASS32 \
  178. ? (void *) &cap32 \
  179. : (void *) &cap64)
  180. #define xcap_sizeof (clazz == ELFCLASS32 \
  181. ? sizeof cap32 \
  182. : sizeof cap64)
  183. #define xcap_tag (clazz == ELFCLASS32 \
  184. ? elf_getu32(swap, cap32.c_tag) \
  185. : elf_getu64(swap, cap64.c_tag))
  186. #define xcap_val (clazz == ELFCLASS32 \
  187. ? elf_getu32(swap, cap32.c_un.c_val) \
  188. : elf_getu64(swap, cap64.c_un.c_val))
  189. #ifdef ELFCORE
  190. /*
  191. * Try larger offsets first to avoid false matches
  192. * from earlier data that happen to look like strings.
  193. */
  194. static const size_t prpsoffsets32[] = {
  195. #ifdef USE_NT_PSINFO
  196. 104, /* SunOS 5.x (command line) */
  197. 88, /* SunOS 5.x (short name) */
  198. #endif /* USE_NT_PSINFO */
  199. 100, /* SunOS 5.x (command line) */
  200. 84, /* SunOS 5.x (short name) */
  201. 44, /* Linux (command line) */
  202. 28, /* Linux 2.0.36 (short name) */
  203. 8, /* FreeBSD */
  204. };
  205. static const size_t prpsoffsets64[] = {
  206. #ifdef USE_NT_PSINFO
  207. 152, /* SunOS 5.x (command line) */
  208. 136, /* SunOS 5.x (short name) */
  209. #endif /* USE_NT_PSINFO */
  210. 136, /* SunOS 5.x, 64-bit (command line) */
  211. 120, /* SunOS 5.x, 64-bit (short name) */
  212. 56, /* Linux (command line) */
  213. 40, /* Linux (tested on core from 2.4.x, short name) */
  214. 16, /* FreeBSD, 64-bit */
  215. };
  216. #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
  217. #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
  218. #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
  219. /*
  220. * Look through the program headers of an executable image, searching
  221. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
  222. * "FreeBSD"; if one is found, try looking in various places in its
  223. * contents for a 16-character string containing only printable
  224. * characters - if found, that string should be the name of the program
  225. * that dropped core. Note: right after that 16-character string is,
  226. * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
  227. * Linux, a longer string (80 characters, in 5.x, probably other
  228. * SVR4-flavored systems, and Linux) containing the start of the
  229. * command line for that program.
  230. *
  231. * SunOS 5.x core files contain two PT_NOTE sections, with the types
  232. * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
  233. * same info about the command name and command line, so it probably
  234. * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
  235. * above (see USE_NT_PSINFO), in case we ever decide to do so. The
  236. * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
  237. * the SunOS 5.x file command relies on this (and prefers the latter).
  238. *
  239. * The signal number probably appears in a section of type NT_PRSTATUS,
  240. * but that's also rather OS-dependent, in ways that are harder to
  241. * dissect with heuristics, so I'm not bothering with the signal number.
  242. * (I suppose the signal number could be of interest in situations where
  243. * you don't have the binary of the program that dropped core; if you
  244. * *do* have that binary, the debugger will probably tell you what
  245. * signal it was.)
  246. */
  247. #define OS_STYLE_SVR4 0
  248. #define OS_STYLE_FREEBSD 1
  249. #define OS_STYLE_NETBSD 2
  250. private const char os_style_names[][8] = {
  251. "SVR4",
  252. "FreeBSD",
  253. "NetBSD",
  254. };
  255. #define FLAGS_DID_CORE 0x01
  256. #define FLAGS_DID_NOTE 0x02
  257. #define FLAGS_DID_BUILD_ID 0x04
  258. #define FLAGS_DID_CORE_STYLE 0x08
  259. #define FLAGS_IS_CORE 0x10
  260. private int
  261. dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  262. int num, size_t size, off_t fsize, int *flags)
  263. {
  264. Elf32_Phdr ph32;
  265. Elf64_Phdr ph64;
  266. size_t offset;
  267. unsigned char nbuf[BUFSIZ];
  268. ssize_t bufsize;
  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 (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. off += size;
  287. if (xph_offset > fsize) {
  288. /* Perhaps warn here */
  289. continue;
  290. }
  291. if (xph_type != PT_NOTE)
  292. continue;
  293. /*
  294. * This is a PT_NOTE section; loop through all the notes
  295. * in the section.
  296. */
  297. if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
  298. file_badseek(ms);
  299. return -1;
  300. }
  301. bufsize = read(fd, nbuf,
  302. ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
  303. if (bufsize == -1) {
  304. file_badread(ms);
  305. return -1;
  306. }
  307. offset = 0;
  308. for (;;) {
  309. if (offset >= (size_t)bufsize)
  310. break;
  311. offset = donote(ms, nbuf, offset, (size_t)bufsize,
  312. clazz, swap, 4, flags);
  313. if (offset == 0)
  314. break;
  315. }
  316. }
  317. return 0;
  318. }
  319. #endif
  320. private size_t
  321. donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
  322. int clazz, int swap, size_t align, int *flags)
  323. {
  324. Elf32_Nhdr nh32;
  325. Elf64_Nhdr nh64;
  326. size_t noff, doff;
  327. #ifdef ELFCORE
  328. int os_style = -1;
  329. #endif
  330. uint32_t namesz, descsz;
  331. unsigned char *nbuf = CAST(unsigned char *, vbuf);
  332. (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
  333. offset += xnh_sizeof;
  334. namesz = xnh_namesz;
  335. descsz = xnh_descsz;
  336. if ((namesz == 0) && (descsz == 0)) {
  337. /*
  338. * We're out of note headers.
  339. */
  340. return (offset >= size) ? offset : size;
  341. }
  342. if (namesz & 0x80000000) {
  343. (void)file_printf(ms, ", bad note name size 0x%lx",
  344. (unsigned long)namesz);
  345. return offset;
  346. }
  347. if (descsz & 0x80000000) {
  348. (void)file_printf(ms, ", bad note description size 0x%lx",
  349. (unsigned long)descsz);
  350. return offset;
  351. }
  352. noff = offset;
  353. doff = ELF_ALIGN(offset + namesz);
  354. if (offset + namesz > size) {
  355. /*
  356. * We're past the end of the buffer.
  357. */
  358. return doff;
  359. }
  360. offset = ELF_ALIGN(doff + descsz);
  361. if (doff + descsz > size) {
  362. /*
  363. * We're past the end of the buffer.
  364. */
  365. return (offset >= size) ? offset : size;
  366. }
  367. if ((*flags & (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) ==
  368. (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID))
  369. goto core;
  370. if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
  371. xnh_type == NT_GNU_VERSION && descsz == 2) {
  372. file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
  373. }
  374. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  375. xnh_type == NT_GNU_VERSION && descsz == 16) {
  376. uint32_t desc[4];
  377. (void)memcpy(desc, &nbuf[doff], sizeof(desc));
  378. if (file_printf(ms, ", for GNU/") == -1)
  379. return size;
  380. switch (elf_getu32(swap, desc[0])) {
  381. case GNU_OS_LINUX:
  382. if (file_printf(ms, "Linux") == -1)
  383. return size;
  384. break;
  385. case GNU_OS_HURD:
  386. if (file_printf(ms, "Hurd") == -1)
  387. return size;
  388. break;
  389. case GNU_OS_SOLARIS:
  390. if (file_printf(ms, "Solaris") == -1)
  391. return size;
  392. break;
  393. case GNU_OS_KFREEBSD:
  394. if (file_printf(ms, "kFreeBSD") == -1)
  395. return size;
  396. break;
  397. case GNU_OS_KNETBSD:
  398. if (file_printf(ms, "kNetBSD") == -1)
  399. return size;
  400. break;
  401. default:
  402. if (file_printf(ms, "<unknown>") == -1)
  403. return size;
  404. }
  405. if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
  406. elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
  407. return size;
  408. *flags |= FLAGS_DID_NOTE;
  409. return size;
  410. }
  411. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  412. xnh_type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) {
  413. uint8_t desc[20];
  414. uint32_t i;
  415. if (file_printf(ms, ", BuildID[%s]=", descsz == 16 ? "md5/uuid" :
  416. "sha1") == -1)
  417. return size;
  418. (void)memcpy(desc, &nbuf[doff], descsz);
  419. for (i = 0; i < descsz; i++)
  420. if (file_printf(ms, "%02x", 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, off_t fsize, int *flags, int mach)
  776. {
  777. Elf32_Shdr sh32;
  778. Elf64_Shdr sh64;
  779. int stripped = 1;
  780. void *nbuf;
  781. off_t noff, coff;
  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. for ( ; num; num--) {
  790. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  791. file_badseek(ms);
  792. return -1;
  793. }
  794. if (read(fd, xsh_addr, xsh_sizeof) == -1) {
  795. file_badread(ms);
  796. return -1;
  797. }
  798. off += size;
  799. /* Things we can determine before we seek */
  800. switch (xsh_type) {
  801. case SHT_SYMTAB:
  802. #if 0
  803. case SHT_DYNSYM:
  804. #endif
  805. stripped = 0;
  806. break;
  807. default:
  808. if (xsh_offset > fsize) {
  809. /* Perhaps warn here */
  810. continue;
  811. }
  812. break;
  813. }
  814. /* Things we can determine when we seek */
  815. switch (xsh_type) {
  816. case SHT_NOTE:
  817. if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
  818. file_error(ms, errno, "Cannot allocate memory"
  819. " for note");
  820. return -1;
  821. }
  822. if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
  823. (off_t)-1) {
  824. file_badread(ms);
  825. free(nbuf);
  826. return -1;
  827. }
  828. if (read(fd, nbuf, (size_t)xsh_size) !=
  829. (ssize_t)xsh_size) {
  830. free(nbuf);
  831. file_badread(ms);
  832. return -1;
  833. }
  834. noff = 0;
  835. for (;;) {
  836. if (noff >= (off_t)xsh_size)
  837. break;
  838. noff = donote(ms, nbuf, (size_t)noff,
  839. (size_t)xsh_size, clazz, swap, 4,
  840. flags);
  841. if (noff == 0)
  842. break;
  843. }
  844. free(nbuf);
  845. break;
  846. case SHT_SUNW_cap:
  847. switch (mach) {
  848. case EM_SPARC:
  849. case EM_SPARCV9:
  850. case EM_IA_64:
  851. case EM_386:
  852. case EM_AMD64:
  853. break;
  854. default:
  855. goto skip;
  856. }
  857. if (lseek(fd, (off_t)xsh_offset, SEEK_SET) ==
  858. (off_t)-1) {
  859. file_badseek(ms);
  860. return -1;
  861. }
  862. coff = 0;
  863. for (;;) {
  864. Elf32_Cap cap32;
  865. Elf64_Cap cap64;
  866. char cbuf[/*CONSTCOND*/
  867. MAX(sizeof cap32, sizeof cap64)];
  868. if ((coff += xcap_sizeof) > (off_t)xsh_size)
  869. break;
  870. if (read(fd, cbuf, (size_t)xcap_sizeof) !=
  871. (ssize_t)xcap_sizeof) {
  872. file_badread(ms);
  873. return -1;
  874. }
  875. (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
  876. switch (xcap_tag) {
  877. case CA_SUNW_NULL:
  878. break;
  879. case CA_SUNW_HW_1:
  880. cap_hw1 |= xcap_val;
  881. break;
  882. case CA_SUNW_SF_1:
  883. cap_sf1 |= xcap_val;
  884. break;
  885. default:
  886. if (file_printf(ms,
  887. ", with unknown capability "
  888. "0x%" INT64_T_FORMAT "x = 0x%"
  889. INT64_T_FORMAT "x",
  890. (unsigned long long)xcap_tag,
  891. (unsigned long long)xcap_val) == -1)
  892. return -1;
  893. break;
  894. }
  895. }
  896. /*FALLTHROUGH*/
  897. skip:
  898. default:
  899. break;
  900. }
  901. }
  902. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  903. return -1;
  904. if (cap_hw1) {
  905. const cap_desc_t *cdp;
  906. switch (mach) {
  907. case EM_SPARC:
  908. case EM_SPARC32PLUS:
  909. case EM_SPARCV9:
  910. cdp = cap_desc_sparc;
  911. break;
  912. case EM_386:
  913. case EM_IA_64:
  914. case EM_AMD64:
  915. cdp = cap_desc_386;
  916. break;
  917. default:
  918. cdp = NULL;
  919. break;
  920. }
  921. if (file_printf(ms, ", uses") == -1)
  922. return -1;
  923. if (cdp) {
  924. while (cdp->cd_name) {
  925. if (cap_hw1 & cdp->cd_mask) {
  926. if (file_printf(ms,
  927. " %s", cdp->cd_name) == -1)
  928. return -1;
  929. cap_hw1 &= ~cdp->cd_mask;
  930. }
  931. ++cdp;
  932. }
  933. if (cap_hw1)
  934. if (file_printf(ms,
  935. " unknown hardware capability 0x%"
  936. INT64_T_FORMAT "x",
  937. (unsigned long long)cap_hw1) == -1)
  938. return -1;
  939. } else {
  940. if (file_printf(ms,
  941. " hardware capability 0x%" INT64_T_FORMAT "x",
  942. (unsigned long long)cap_hw1) == -1)
  943. return -1;
  944. }
  945. }
  946. if (cap_sf1) {
  947. if (cap_sf1 & SF1_SUNW_FPUSED) {
  948. if (file_printf(ms,
  949. (cap_sf1 & SF1_SUNW_FPKNWN)
  950. ? ", uses frame pointer"
  951. : ", not known to use frame pointer") == -1)
  952. return -1;
  953. }
  954. cap_sf1 &= ~SF1_SUNW_MASK;
  955. if (cap_sf1)
  956. if (file_printf(ms,
  957. ", with unknown software capability 0x%"
  958. INT64_T_FORMAT "x",
  959. (unsigned long long)cap_sf1) == -1)
  960. return -1;
  961. }
  962. return 0;
  963. }
  964. /*
  965. * Look through the program headers of an executable image, searching
  966. * for a PT_INTERP section; if one is found, it's dynamically linked,
  967. * otherwise it's statically linked.
  968. */
  969. private int
  970. dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  971. int num, size_t size, off_t fsize, int *flags, int sh_num)
  972. {
  973. Elf32_Phdr ph32;
  974. Elf64_Phdr ph64;
  975. const char *linking_style = "statically";
  976. const char *shared_libraries = "";
  977. unsigned char nbuf[BUFSIZ];
  978. ssize_t bufsize;
  979. size_t offset, align;
  980. if (size != xph_sizeof) {
  981. if (file_printf(ms, ", corrupted program header size") == -1)
  982. return -1;
  983. return 0;
  984. }
  985. for ( ; num; num--) {
  986. if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
  987. file_badseek(ms);
  988. return -1;
  989. }
  990. if (read(fd, xph_addr, xph_sizeof) == -1) {
  991. file_badread(ms);
  992. return -1;
  993. }
  994. off += size;
  995. /* Things we can determine before we seek */
  996. switch (xph_type) {
  997. case PT_DYNAMIC:
  998. linking_style = "dynamically";
  999. break;
  1000. case PT_INTERP:
  1001. shared_libraries = " (uses shared libs)";
  1002. break;
  1003. default:
  1004. if (xph_offset > fsize) {
  1005. /* Maybe warn here? */
  1006. continue;
  1007. }
  1008. break;
  1009. }
  1010. /* Things we can determine when we seek */
  1011. switch (xph_type) {
  1012. case PT_NOTE:
  1013. if ((align = xph_align) & 0x80000000UL) {
  1014. if (file_printf(ms,
  1015. ", invalid note alignment 0x%lx",
  1016. (unsigned long)align) == -1)
  1017. return -1;
  1018. align = 4;
  1019. }
  1020. if (sh_num)
  1021. break;
  1022. /*
  1023. * This is a PT_NOTE section; loop through all the notes
  1024. * in the section.
  1025. */
  1026. if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
  1027. file_badseek(ms);
  1028. return -1;
  1029. }
  1030. bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
  1031. xph_filesz : sizeof(nbuf)));
  1032. if (bufsize == -1) {
  1033. file_badread(ms);
  1034. return -1;
  1035. }
  1036. offset = 0;
  1037. for (;;) {
  1038. if (offset >= (size_t)bufsize)
  1039. break;
  1040. offset = donote(ms, nbuf, offset,
  1041. (size_t)bufsize, clazz, swap, align,
  1042. flags);
  1043. if (offset == 0)
  1044. break;
  1045. }
  1046. break;
  1047. default:
  1048. break;
  1049. }
  1050. }
  1051. if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
  1052. == -1)
  1053. return -1;
  1054. return 0;
  1055. }
  1056. protected int
  1057. file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
  1058. size_t nbytes)
  1059. {
  1060. union {
  1061. int32_t l;
  1062. char c[sizeof (int32_t)];
  1063. } u;
  1064. int clazz;
  1065. int swap;
  1066. struct stat st;
  1067. off_t fsize;
  1068. int flags = 0;
  1069. Elf32_Ehdr elf32hdr;
  1070. Elf64_Ehdr elf64hdr;
  1071. uint16_t type;
  1072. if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
  1073. return 0;
  1074. /*
  1075. * ELF executables have multiple section headers in arbitrary
  1076. * file locations and thus file(1) cannot determine it from easily.
  1077. * Instead we traverse thru all section headers until a symbol table
  1078. * one is found or else the binary is stripped.
  1079. * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
  1080. */
  1081. if (buf[EI_MAG0] != ELFMAG0
  1082. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  1083. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  1084. return 0;
  1085. /*
  1086. * If we cannot seek, it must be a pipe, socket or fifo.
  1087. */
  1088. if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
  1089. fd = file_pipe2file(ms, fd, buf, nbytes);
  1090. if (fstat(fd, &st) == -1) {
  1091. file_badread(ms);
  1092. return -1;
  1093. }
  1094. fsize = st.st_size;
  1095. clazz = buf[EI_CLASS];
  1096. switch (clazz) {
  1097. case ELFCLASS32:
  1098. #undef elf_getu
  1099. #define elf_getu(a, b) elf_getu32(a, b)
  1100. #undef elfhdr
  1101. #define elfhdr elf32hdr
  1102. #include "elfclass.h"
  1103. case ELFCLASS64:
  1104. #undef elf_getu
  1105. #define elf_getu(a, b) elf_getu64(a, b)
  1106. #undef elfhdr
  1107. #define elfhdr elf64hdr
  1108. #include "elfclass.h"
  1109. default:
  1110. if (file_printf(ms, ", unknown class %d", clazz) == -1)
  1111. return -1;
  1112. break;
  1113. }
  1114. return 0;
  1115. }
  1116. #endif