2 * Copyright (c) Christos Zoulas 2003.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 FILE_RCSID("@(#)$File: readelf.c,v 1.111 2014/12/09 02:47:45 christos Exp $")
45 private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
48 private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
50 private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
51 off_t, int *, int, int);
52 private size_t donote(struct magic_set *, void *, size_t, size_t, int,
55 #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
57 #define isquote(c) (strchr("'\"`", (c)) != NULL)
59 private uint16_t getu16(int, uint16_t);
60 private uint32_t getu32(int, uint32_t);
61 private uint64_t getu64(int, uint64_t);
64 #define MAX_SHNUM 32768
65 #define SIZE_UNKNOWN ((off_t)-1)
68 toomany(struct magic_set *ms, const char *name, uint16_t num)
70 if (file_printf(ms, ", too many %s header sections (%u)", name, num
77 getu16(int swap, uint16_t value)
87 retval.c[0] = tmpval.c[1];
88 retval.c[1] = tmpval.c[0];
96 getu32(int swap, uint32_t value)
106 retval.c[0] = tmpval.c[3];
107 retval.c[1] = tmpval.c[2];
108 retval.c[2] = tmpval.c[1];
109 retval.c[3] = tmpval.c[0];
117 getu64(int swap, uint64_t value)
127 retval.c[0] = tmpval.c[7];
128 retval.c[1] = tmpval.c[6];
129 retval.c[2] = tmpval.c[5];
130 retval.c[3] = tmpval.c[4];
131 retval.c[4] = tmpval.c[3];
132 retval.c[5] = tmpval.c[2];
133 retval.c[6] = tmpval.c[1];
134 retval.c[7] = tmpval.c[0];
141 #define elf_getu16(swap, value) getu16(swap, value)
142 #define elf_getu32(swap, value) getu32(swap, value)
143 #define elf_getu64(swap, value) getu64(swap, value)
145 #define xsh_addr (clazz == ELFCLASS32 \
148 #define xsh_sizeof (clazz == ELFCLASS32 \
151 #define xsh_size (size_t)(clazz == ELFCLASS32 \
152 ? elf_getu32(swap, sh32.sh_size) \
153 : elf_getu64(swap, sh64.sh_size))
154 #define xsh_offset (off_t)(clazz == ELFCLASS32 \
155 ? elf_getu32(swap, sh32.sh_offset) \
156 : elf_getu64(swap, sh64.sh_offset))
157 #define xsh_type (clazz == ELFCLASS32 \
158 ? elf_getu32(swap, sh32.sh_type) \
159 : elf_getu32(swap, sh64.sh_type))
160 #define xsh_name (clazz == ELFCLASS32 \
161 ? elf_getu32(swap, sh32.sh_name) \
162 : elf_getu32(swap, sh64.sh_name))
163 #define xph_addr (clazz == ELFCLASS32 \
166 #define xph_sizeof (clazz == ELFCLASS32 \
169 #define xph_type (clazz == ELFCLASS32 \
170 ? elf_getu32(swap, ph32.p_type) \
171 : elf_getu32(swap, ph64.p_type))
172 #define xph_offset (off_t)(clazz == ELFCLASS32 \
173 ? elf_getu32(swap, ph32.p_offset) \
174 : elf_getu64(swap, ph64.p_offset))
175 #define xph_align (size_t)((clazz == ELFCLASS32 \
176 ? (off_t) (ph32.p_align ? \
177 elf_getu32(swap, ph32.p_align) : 4) \
178 : (off_t) (ph64.p_align ? \
179 elf_getu64(swap, ph64.p_align) : 4)))
180 #define xph_filesz (size_t)((clazz == ELFCLASS32 \
181 ? elf_getu32(swap, ph32.p_filesz) \
182 : elf_getu64(swap, ph64.p_filesz)))
183 #define xnh_addr (clazz == ELFCLASS32 \
186 #define xph_memsz (size_t)((clazz == ELFCLASS32 \
187 ? elf_getu32(swap, ph32.p_memsz) \
188 : elf_getu64(swap, ph64.p_memsz)))
189 #define xnh_sizeof (clazz == ELFCLASS32 \
192 #define xnh_type (clazz == ELFCLASS32 \
193 ? elf_getu32(swap, nh32.n_type) \
194 : elf_getu32(swap, nh64.n_type))
195 #define xnh_namesz (clazz == ELFCLASS32 \
196 ? elf_getu32(swap, nh32.n_namesz) \
197 : elf_getu32(swap, nh64.n_namesz))
198 #define xnh_descsz (clazz == ELFCLASS32 \
199 ? elf_getu32(swap, nh32.n_descsz) \
200 : elf_getu32(swap, nh64.n_descsz))
201 #define prpsoffsets(i) (clazz == ELFCLASS32 \
204 #define xcap_addr (clazz == ELFCLASS32 \
207 #define xcap_sizeof (clazz == ELFCLASS32 \
210 #define xcap_tag (clazz == ELFCLASS32 \
211 ? elf_getu32(swap, cap32.c_tag) \
212 : elf_getu64(swap, cap64.c_tag))
213 #define xcap_val (clazz == ELFCLASS32 \
214 ? elf_getu32(swap, cap32.c_un.c_val) \
215 : elf_getu64(swap, cap64.c_un.c_val))
219 * Try larger offsets first to avoid false matches
220 * from earlier data that happen to look like strings.
222 static const size_t prpsoffsets32[] = {
224 104, /* SunOS 5.x (command line) */
225 88, /* SunOS 5.x (short name) */
226 #endif /* USE_NT_PSINFO */
228 100, /* SunOS 5.x (command line) */
229 84, /* SunOS 5.x (short name) */
231 44, /* Linux (command line) */
232 28, /* Linux 2.0.36 (short name) */
237 static const size_t prpsoffsets64[] = {
239 152, /* SunOS 5.x (command line) */
240 136, /* SunOS 5.x (short name) */
241 #endif /* USE_NT_PSINFO */
243 136, /* SunOS 5.x, 64-bit (command line) */
244 120, /* SunOS 5.x, 64-bit (short name) */
246 56, /* Linux (command line) */
247 40, /* Linux (tested on core from 2.4.x, short name) */
249 16, /* FreeBSD, 64-bit */
252 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
253 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
255 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
258 * Look through the program headers of an executable image, searching
259 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
260 * "FreeBSD"; if one is found, try looking in various places in its
261 * contents for a 16-character string containing only printable
262 * characters - if found, that string should be the name of the program
263 * that dropped core. Note: right after that 16-character string is,
264 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
265 * Linux, a longer string (80 characters, in 5.x, probably other
266 * SVR4-flavored systems, and Linux) containing the start of the
267 * command line for that program.
269 * SunOS 5.x core files contain two PT_NOTE sections, with the types
270 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
271 * same info about the command name and command line, so it probably
272 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
273 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
274 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
275 * the SunOS 5.x file command relies on this (and prefers the latter).
277 * The signal number probably appears in a section of type NT_PRSTATUS,
278 * but that's also rather OS-dependent, in ways that are harder to
279 * dissect with heuristics, so I'm not bothering with the signal number.
280 * (I suppose the signal number could be of interest in situations where
281 * you don't have the binary of the program that dropped core; if you
282 * *do* have that binary, the debugger will probably tell you what
286 #define OS_STYLE_SVR4 0
287 #define OS_STYLE_FREEBSD 1
288 #define OS_STYLE_NETBSD 2
290 private const char os_style_names[][8] = {
296 #define FLAGS_DID_CORE 0x01
297 #define FLAGS_DID_NOTE 0x02
298 #define FLAGS_DID_BUILD_ID 0x04
299 #define FLAGS_DID_CORE_STYLE 0x08
300 #define FLAGS_IS_CORE 0x10
303 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
304 int num, size_t size, off_t fsize, int *flags)
309 unsigned char nbuf[BUFSIZ];
312 if (size != xph_sizeof) {
313 if (file_printf(ms, ", corrupted program header size") == -1)
319 * Loop through all the program headers.
321 for ( ; num; num--) {
322 if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
328 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
329 /* Perhaps warn here */
333 if (xph_type != PT_NOTE)
337 * This is a PT_NOTE section; loop through all the notes
340 len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
341 if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
347 if (offset >= (size_t)bufsize)
349 offset = donote(ms, nbuf, offset, (size_t)bufsize,
350 clazz, swap, 4, flags);
361 do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
364 (void)memcpy(&desc, v, sizeof(desc));
365 desc = elf_getu32(swap, desc);
367 if (file_printf(ms, ", for NetBSD") == -1)
370 * The version number used to be stuck as 199905, and was thus
371 * basically content-free. Newer versions of NetBSD have fixed
372 * this and now use the encoding of __NetBSD_Version__:
378 * r = release ["",A-Z,Z[A-Z] but numeric]
381 if (desc > 100000000U) {
382 uint32_t ver_patch = (desc / 100) % 100;
383 uint32_t ver_rel = (desc / 10000) % 100;
384 uint32_t ver_min = (desc / 1000000) % 100;
385 uint32_t ver_maj = desc / 100000000;
387 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
389 if (ver_rel == 0 && ver_patch != 0) {
390 if (file_printf(ms, ".%u", ver_patch) == -1)
392 } else if (ver_rel != 0) {
393 while (ver_rel > 26) {
394 if (file_printf(ms, "Z") == -1)
398 if (file_printf(ms, "%c", 'A' + ver_rel - 1)
406 do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
410 (void)memcpy(&desc, v, sizeof(desc));
411 desc = elf_getu32(swap, desc);
412 if (file_printf(ms, ", for FreeBSD") == -1)
416 * Contents is __FreeBSD_version, whose relation to OS
417 * versions is defined by a huge table in the Porter's
418 * Handbook. This is the general scheme:
421 * Mmp000 (before 4.10)
422 * Mmi0p0 (before 5.0)
425 * Development branches:
426 * Mmpxxx (before 4.6)
427 * Mmp1xx (before 4.10)
428 * Mmi1xx (before 5.0)
434 * i = minor version increment (491000 -> 4.10)
438 * The first release of FreeBSD to use ELF by default
441 if (desc == 460002) {
442 if (file_printf(ms, " 4.6.2") == -1)
444 } else if (desc < 460100) {
445 if (file_printf(ms, " %d.%d", desc / 100000,
446 desc / 10000 % 10) == -1)
448 if (desc / 1000 % 10 > 0)
449 if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
451 if ((desc % 1000 > 0) || (desc % 100000 == 0))
452 if (file_printf(ms, " (%d)", desc) == -1)
454 } else if (desc < 500000) {
455 if (file_printf(ms, " %d.%d", desc / 100000,
456 desc / 10000 % 10 + desc / 1000 % 10) == -1)
458 if (desc / 100 % 10 > 0) {
459 if (file_printf(ms, " (%d)", desc) == -1)
461 } else if (desc / 10 % 10 > 0) {
462 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
466 if (file_printf(ms, " %d.%d", desc / 100000,
467 desc / 1000 % 100) == -1)
469 if ((desc / 100 % 10 > 0) ||
470 (desc % 100000 / 100 == 0)) {
471 if (file_printf(ms, " (%d)", desc) == -1)
473 } else if (desc / 10 % 10 > 0) {
474 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
481 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
482 int clazz, int swap, size_t align, int *flags)
490 uint32_t namesz, descsz;
491 unsigned char *nbuf = CAST(unsigned char *, vbuf);
493 if (xnh_sizeof + offset > size) {
495 * We're out of note headers.
497 return xnh_sizeof + offset;
500 (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
501 offset += xnh_sizeof;
505 if ((namesz == 0) && (descsz == 0)) {
507 * We're out of note headers.
509 return (offset >= size) ? offset : size;
512 if (namesz & 0x80000000) {
513 (void)file_printf(ms, ", bad note name size 0x%lx",
514 (unsigned long)namesz);
518 if (descsz & 0x80000000) {
519 (void)file_printf(ms, ", bad note description size 0x%lx",
520 (unsigned long)descsz);
526 doff = ELF_ALIGN(offset + namesz);
528 if (offset + namesz > size) {
530 * We're past the end of the buffer.
535 offset = ELF_ALIGN(doff + descsz);
536 if (doff + descsz > size) {
538 * We're past the end of the buffer.
540 return (offset >= size) ? offset : size;
543 if ((*flags & (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) ==
544 (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID))
547 if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
548 xnh_type == NT_GNU_VERSION && descsz == 2) {
549 file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
551 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
552 xnh_type == NT_GNU_VERSION && descsz == 16) {
554 (void)memcpy(desc, &nbuf[doff], sizeof(desc));
556 if (file_printf(ms, ", for GNU/") == -1)
558 switch (elf_getu32(swap, desc[0])) {
560 if (file_printf(ms, "Linux") == -1)
564 if (file_printf(ms, "Hurd") == -1)
568 if (file_printf(ms, "Solaris") == -1)
571 case GNU_OS_KFREEBSD:
572 if (file_printf(ms, "kFreeBSD") == -1)
576 if (file_printf(ms, "kNetBSD") == -1)
580 if (file_printf(ms, "<unknown>") == -1)
583 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
584 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
586 *flags |= FLAGS_DID_NOTE;
590 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
591 xnh_type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) {
594 if (file_printf(ms, ", BuildID[%s]=", descsz == 16 ? "md5/uuid" :
597 (void)memcpy(desc, &nbuf[doff], descsz);
598 for (i = 0; i < descsz; i++)
599 if (file_printf(ms, "%02x", desc[i]) == -1)
601 *flags |= FLAGS_DID_BUILD_ID;
604 if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 &&
605 xnh_type == NT_NETBSD_PAX && descsz == 4) {
606 static const char *pax[] = {
618 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
619 desc = elf_getu32(swap, desc);
621 if (desc && file_printf(ms, ", PaX: ") == -1)
624 for (i = 0; i < __arraycount(pax); i++) {
625 if (((1 << i) & desc) == 0)
627 if (file_printf(ms, "%s%s", did++ ? "," : "",
633 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
635 case NT_NETBSD_VERSION:
637 do_note_netbsd_version(ms, swap, &nbuf[doff]);
638 *flags |= FLAGS_DID_NOTE;
642 case NT_NETBSD_MARCH:
643 if (file_printf(ms, ", compiled for: %.*s", (int)descsz,
644 (const char *)&nbuf[doff]) == -1)
647 case NT_NETBSD_CMODEL:
648 if (file_printf(ms, ", compiler model: %.*s",
649 (int)descsz, (const char *)&nbuf[doff]) == -1)
653 if (file_printf(ms, ", note=%u", xnh_type) == -1)
660 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) {
661 if (xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
662 do_note_freebsd_version(ms, swap, &nbuf[doff]);
663 *flags |= FLAGS_DID_NOTE;
668 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
669 xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
670 if (file_printf(ms, ", for OpenBSD") == -1)
672 /* Content of note is always 0 */
673 *flags |= FLAGS_DID_NOTE;
677 if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
678 xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
680 if (file_printf(ms, ", for DragonFly") == -1)
682 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
683 desc = elf_getu32(swap, desc);
684 if (file_printf(ms, " %d.%d.%d", desc / 100000,
685 desc / 10000 % 10, desc % 10000) == -1)
687 *flags |= FLAGS_DID_NOTE;
693 * Sigh. The 2.0.36 kernel in Debian 2.1, at
694 * least, doesn't correctly implement name
695 * sections, in core dumps, as specified by
696 * the "Program Linking" section of "UNIX(R) System
697 * V Release 4 Programmer's Guide: ANSI C and
698 * Programming Support Tools", because my copy
699 * clearly says "The first 'namesz' bytes in 'name'
700 * contain a *null-terminated* [emphasis mine]
701 * character representation of the entry's owner
702 * or originator", but the 2.0.36 kernel code
703 * doesn't include the terminating null in the
706 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
707 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
708 os_style = OS_STYLE_SVR4;
711 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
712 os_style = OS_STYLE_FREEBSD;
715 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
717 os_style = OS_STYLE_NETBSD;
721 if ((*flags & FLAGS_DID_CORE) != 0)
724 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
725 if (file_printf(ms, ", %s-style", os_style_names[os_style])
728 *flags |= FLAGS_DID_CORE_STYLE;
732 case OS_STYLE_NETBSD:
733 if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
736 * Extract the program name. It is at
737 * offset 0x7c, and is up to 32-bytes,
738 * including the terminating NUL.
740 if (file_printf(ms, ", from '%.31s'",
741 &nbuf[doff + 0x7c]) == -1)
745 * Extract the signal number. It is at
748 (void)memcpy(&signo, &nbuf[doff + 0x08],
750 if (file_printf(ms, " (signal %u)",
751 elf_getu32(swap, signo)) == -1)
753 *flags |= FLAGS_DID_CORE;
759 if (xnh_type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
763 * Extract the program name. We assume
764 * it to be 16 characters (that's what it
765 * is in SunOS 5.x and Linux).
767 * Unfortunately, it's at a different offset
768 * in various OSes, so try multiple offsets.
769 * If the characters aren't all printable,
772 for (i = 0; i < NOFFSETS; i++) {
773 unsigned char *cname, *cp;
774 size_t reloffset = prpsoffsets(i);
775 size_t noffset = doff + reloffset;
777 for (j = 0; j < 16; j++, noffset++,
780 * Make sure we're not past
781 * the end of the buffer; if
782 * we are, just give up.
788 * Make sure we're not past
789 * the end of the contents;
790 * if we are, this obviously
791 * isn't the right offset.
793 if (reloffset >= descsz)
815 if (!isprint(c) || isquote(c))
824 * Try next offsets, in case this match is
825 * in the middle of a string.
827 for (k = i + 1 ; k < NOFFSETS ; k++) {
830 if (prpsoffsets(k) >= prpsoffsets(i))
832 for (no = doff + prpsoffsets(k);
833 no < doff + prpsoffsets(i); no++)
835 && isprint(nbuf[no]);
840 cname = (unsigned char *)
841 &nbuf[doff + prpsoffsets(i)];
842 for (cp = cname; *cp && isprint(*cp); cp++)
845 * Linux apparently appends a space at the end
846 * of the command line: remove it.
848 while (cp > cname && isspace(cp[-1]))
850 if (file_printf(ms, ", from '%.*s'",
851 (int)(cp - cname), cname) == -1)
853 *flags |= FLAGS_DID_CORE;
866 /* SunOS 5.x hardware capability descriptions */
867 typedef struct cap_desc {
872 static const cap_desc_t cap_desc_sparc[] = {
873 { AV_SPARC_MUL32, "MUL32" },
874 { AV_SPARC_DIV32, "DIV32" },
875 { AV_SPARC_FSMULD, "FSMULD" },
876 { AV_SPARC_V8PLUS, "V8PLUS" },
877 { AV_SPARC_POPC, "POPC" },
878 { AV_SPARC_VIS, "VIS" },
879 { AV_SPARC_VIS2, "VIS2" },
880 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
881 { AV_SPARC_FMAF, "FMAF" },
882 { AV_SPARC_FJFMAU, "FJFMAU" },
883 { AV_SPARC_IMA, "IMA" },
887 static const cap_desc_t cap_desc_386[] = {
888 { AV_386_FPU, "FPU" },
889 { AV_386_TSC, "TSC" },
890 { AV_386_CX8, "CX8" },
891 { AV_386_SEP, "SEP" },
892 { AV_386_AMD_SYSC, "AMD_SYSC" },
893 { AV_386_CMOV, "CMOV" },
894 { AV_386_MMX, "MMX" },
895 { AV_386_AMD_MMX, "AMD_MMX" },
896 { AV_386_AMD_3DNow, "AMD_3DNow" },
897 { AV_386_AMD_3DNowx, "AMD_3DNowx" },
898 { AV_386_FXSR, "FXSR" },
899 { AV_386_SSE, "SSE" },
900 { AV_386_SSE2, "SSE2" },
901 { AV_386_PAUSE, "PAUSE" },
902 { AV_386_SSE3, "SSE3" },
903 { AV_386_MON, "MON" },
904 { AV_386_CX16, "CX16" },
905 { AV_386_AHF, "AHF" },
906 { AV_386_TSCP, "TSCP" },
907 { AV_386_AMD_SSE4A, "AMD_SSE4A" },
908 { AV_386_POPCNT, "POPCNT" },
909 { AV_386_AMD_LZCNT, "AMD_LZCNT" },
910 { AV_386_SSSE3, "SSSE3" },
911 { AV_386_SSE4_1, "SSE4.1" },
912 { AV_386_SSE4_2, "SSE4.2" },
917 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
918 size_t size, off_t fsize, int *flags, int mach, int strtab)
925 off_t noff, coff, name_off;
926 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
927 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
930 if (size != xsh_sizeof) {
931 if (file_printf(ms, ", corrupted section header size") == -1)
936 /* Read offset of name section to be able to read section names later */
937 if (pread(fd, xsh_addr, xsh_sizeof, off + size * strtab) == -1) {
941 name_off = xsh_offset;
943 for ( ; num; num--) {
944 /* Read the name of this section. */
945 if (pread(fd, name, sizeof(name), name_off + xsh_name) == -1) {
949 name[sizeof(name) - 1] = '\0';
950 if (strcmp(name, ".debug_info") == 0)
953 if (pread(fd, xsh_addr, xsh_sizeof, off) == -1) {
959 /* Things we can determine before we seek */
968 if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
969 /* Perhaps warn here */
975 /* Things we can determine when we seek */
978 if ((nbuf = malloc(xsh_size)) == NULL) {
979 file_error(ms, errno, "Cannot allocate memory"
983 if (pread(fd, nbuf, xsh_size, xsh_offset) == -1) {
991 if (noff >= (off_t)xsh_size)
993 noff = donote(ms, nbuf, (size_t)noff,
994 xsh_size, clazz, swap, 4, flags);
1014 if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
1022 char cbuf[/*CONSTCOND*/
1023 MAX(sizeof cap32, sizeof cap64)];
1024 if ((coff += xcap_sizeof) > (off_t)xsh_size)
1026 if (read(fd, cbuf, (size_t)xcap_sizeof) !=
1027 (ssize_t)xcap_sizeof) {
1031 if (cbuf[0] == 'A') {
1035 memcpy(&len, p, sizeof(len));
1037 len = getu32(swap, len);
1038 if (memcmp("gnu", p, 3) != 0) {
1040 ", unknown capability %.3s", p)
1047 memcpy(&len, p, sizeof(len));
1049 len = getu32(swap, len);
1051 if (file_printf(ms, ", unknown gnu"
1052 " capability tag %d", tag)
1061 (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
1066 cap_hw1 |= xcap_val;
1069 cap_sf1 |= xcap_val;
1073 ", with unknown capability "
1074 "0x%" INT64_T_FORMAT "x = 0x%"
1076 (unsigned long long)xcap_tag,
1077 (unsigned long long)xcap_val) == -1)
1091 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1094 const cap_desc_t *cdp;
1097 case EM_SPARC32PLUS:
1099 cdp = cap_desc_sparc;
1110 if (file_printf(ms, ", uses") == -1)
1113 while (cdp->cd_name) {
1114 if (cap_hw1 & cdp->cd_mask) {
1116 " %s", cdp->cd_name) == -1)
1118 cap_hw1 &= ~cdp->cd_mask;
1124 " unknown hardware capability 0x%"
1126 (unsigned long long)cap_hw1) == -1)
1130 " hardware capability 0x%" INT64_T_FORMAT "x",
1131 (unsigned long long)cap_hw1) == -1)
1136 if (cap_sf1 & SF1_SUNW_FPUSED) {
1138 (cap_sf1 & SF1_SUNW_FPKNWN)
1139 ? ", uses frame pointer"
1140 : ", not known to use frame pointer") == -1)
1143 cap_sf1 &= ~SF1_SUNW_MASK;
1146 ", with unknown software capability 0x%"
1148 (unsigned long long)cap_sf1) == -1)
1155 * Look through the program headers of an executable image, searching
1156 * for a PT_INTERP section; if one is found, it's dynamically linked,
1157 * otherwise it's statically linked.
1160 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1161 int num, size_t size, off_t fsize, int *flags, int sh_num)
1165 const char *linking_style = "statically";
1166 const char *shared_libraries = "";
1167 unsigned char nbuf[BUFSIZ];
1169 size_t offset, align, len;
1171 if (size != xph_sizeof) {
1172 if (file_printf(ms, ", corrupted program header size") == -1)
1177 for ( ; num; num--) {
1178 if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
1185 /* Things we can determine before we seek */
1188 linking_style = "dynamically";
1191 shared_libraries = " (uses shared libs)";
1194 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
1195 /* Maybe warn here? */
1201 /* Things we can determine when we seek */
1204 if (((align = xph_align) & 0x80000000UL) != 0 ||
1207 ", invalid note alignment 0x%lx",
1208 (unsigned long)align) == -1)
1215 * This is a PT_NOTE section; loop through all the notes
1218 len = xph_filesz < sizeof(nbuf) ? xph_filesz
1220 bufsize = pread(fd, nbuf, len, xph_offset);
1221 if (bufsize == -1) {
1227 if (offset >= (size_t)bufsize)
1229 offset = donote(ms, nbuf, offset,
1230 (size_t)bufsize, clazz, swap, align,
1240 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
1248 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
1253 char c[sizeof (int32_t)];
1260 Elf32_Ehdr elf32hdr;
1261 Elf64_Ehdr elf64hdr;
1262 uint16_t type, phnum, shnum;
1264 if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
1267 * ELF executables have multiple section headers in arbitrary
1268 * file locations and thus file(1) cannot determine it from easily.
1269 * Instead we traverse thru all section headers until a symbol table
1270 * one is found or else the binary is stripped.
1271 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
1273 if (buf[EI_MAG0] != ELFMAG0
1274 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1275 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1279 * If we cannot seek, it must be a pipe, socket or fifo.
1281 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1282 fd = file_pipe2file(ms, fd, buf, nbytes);
1284 if (fstat(fd, &st) == -1) {
1288 if (S_ISREG(st.st_mode) || st.st_size != 0)
1291 fsize = SIZE_UNKNOWN;
1293 clazz = buf[EI_CLASS];
1298 #define elf_getu(a, b) elf_getu32(a, b)
1300 #define elfhdr elf32hdr
1301 #include "elfclass.h"
1304 #define elf_getu(a, b) elf_getu64(a, b)
1306 #define elfhdr elf64hdr
1307 #include "elfclass.h"
1309 if (file_printf(ms, ", unknown class %d", clazz) == -1)