readelf.c 30 KB

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