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.102 2014/03/11 21:00:13 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 getu16(int swap, uint16_t value)
74 retval.c[0] = tmpval.c[1];
75 retval.c[1] = tmpval.c[0];
83 getu32(int swap, uint32_t value)
93 retval.c[0] = tmpval.c[3];
94 retval.c[1] = tmpval.c[2];
95 retval.c[2] = tmpval.c[1];
96 retval.c[3] = tmpval.c[0];
104 getu64(int swap, uint64_t value)
114 retval.c[0] = tmpval.c[7];
115 retval.c[1] = tmpval.c[6];
116 retval.c[2] = tmpval.c[5];
117 retval.c[3] = tmpval.c[4];
118 retval.c[4] = tmpval.c[3];
119 retval.c[5] = tmpval.c[2];
120 retval.c[6] = tmpval.c[1];
121 retval.c[7] = tmpval.c[0];
128 #define elf_getu16(swap, value) getu16(swap, value)
129 #define elf_getu32(swap, value) getu32(swap, value)
130 #define elf_getu64(swap, value) getu64(swap, value)
132 #define xsh_addr (clazz == ELFCLASS32 \
135 #define xsh_sizeof (clazz == ELFCLASS32 \
138 #define xsh_size (size_t)(clazz == ELFCLASS32 \
139 ? elf_getu32(swap, sh32.sh_size) \
140 : elf_getu64(swap, sh64.sh_size))
141 #define xsh_offset (off_t)(clazz == ELFCLASS32 \
142 ? elf_getu32(swap, sh32.sh_offset) \
143 : elf_getu64(swap, sh64.sh_offset))
144 #define xsh_type (clazz == ELFCLASS32 \
145 ? elf_getu32(swap, sh32.sh_type) \
146 : elf_getu32(swap, sh64.sh_type))
147 #define xsh_name (clazz == ELFCLASS32 \
148 ? elf_getu32(swap, sh32.sh_name) \
149 : elf_getu32(swap, sh64.sh_name))
150 #define xph_addr (clazz == ELFCLASS32 \
153 #define xph_sizeof (clazz == ELFCLASS32 \
156 #define xph_type (clazz == ELFCLASS32 \
157 ? elf_getu32(swap, ph32.p_type) \
158 : elf_getu32(swap, ph64.p_type))
159 #define xph_offset (off_t)(clazz == ELFCLASS32 \
160 ? elf_getu32(swap, ph32.p_offset) \
161 : elf_getu64(swap, ph64.p_offset))
162 #define xph_align (size_t)((clazz == ELFCLASS32 \
163 ? (off_t) (ph32.p_align ? \
164 elf_getu32(swap, ph32.p_align) : 4) \
165 : (off_t) (ph64.p_align ? \
166 elf_getu64(swap, ph64.p_align) : 4)))
167 #define xph_filesz (size_t)((clazz == ELFCLASS32 \
168 ? elf_getu32(swap, ph32.p_filesz) \
169 : elf_getu64(swap, ph64.p_filesz)))
170 #define xnh_addr (clazz == ELFCLASS32 \
173 #define xph_memsz (size_t)((clazz == ELFCLASS32 \
174 ? elf_getu32(swap, ph32.p_memsz) \
175 : elf_getu64(swap, ph64.p_memsz)))
176 #define xnh_sizeof (clazz == ELFCLASS32 \
179 #define xnh_type (clazz == ELFCLASS32 \
180 ? elf_getu32(swap, nh32.n_type) \
181 : elf_getu32(swap, nh64.n_type))
182 #define xnh_namesz (clazz == ELFCLASS32 \
183 ? elf_getu32(swap, nh32.n_namesz) \
184 : elf_getu32(swap, nh64.n_namesz))
185 #define xnh_descsz (clazz == ELFCLASS32 \
186 ? elf_getu32(swap, nh32.n_descsz) \
187 : elf_getu32(swap, nh64.n_descsz))
188 #define prpsoffsets(i) (clazz == ELFCLASS32 \
191 #define xcap_addr (clazz == ELFCLASS32 \
194 #define xcap_sizeof (clazz == ELFCLASS32 \
197 #define xcap_tag (clazz == ELFCLASS32 \
198 ? elf_getu32(swap, cap32.c_tag) \
199 : elf_getu64(swap, cap64.c_tag))
200 #define xcap_val (clazz == ELFCLASS32 \
201 ? elf_getu32(swap, cap32.c_un.c_val) \
202 : elf_getu64(swap, cap64.c_un.c_val))
206 * Try larger offsets first to avoid false matches
207 * from earlier data that happen to look like strings.
209 static const size_t prpsoffsets32[] = {
211 104, /* SunOS 5.x (command line) */
212 88, /* SunOS 5.x (short name) */
213 #endif /* USE_NT_PSINFO */
215 100, /* SunOS 5.x (command line) */
216 84, /* SunOS 5.x (short name) */
218 44, /* Linux (command line) */
219 28, /* Linux 2.0.36 (short name) */
224 static const size_t prpsoffsets64[] = {
226 152, /* SunOS 5.x (command line) */
227 136, /* SunOS 5.x (short name) */
228 #endif /* USE_NT_PSINFO */
230 136, /* SunOS 5.x, 64-bit (command line) */
231 120, /* SunOS 5.x, 64-bit (short name) */
233 56, /* Linux (command line) */
234 40, /* Linux (tested on core from 2.4.x, short name) */
236 16, /* FreeBSD, 64-bit */
239 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
240 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
242 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
245 * Look through the program headers of an executable image, searching
246 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
247 * "FreeBSD"; if one is found, try looking in various places in its
248 * contents for a 16-character string containing only printable
249 * characters - if found, that string should be the name of the program
250 * that dropped core. Note: right after that 16-character string is,
251 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
252 * Linux, a longer string (80 characters, in 5.x, probably other
253 * SVR4-flavored systems, and Linux) containing the start of the
254 * command line for that program.
256 * SunOS 5.x core files contain two PT_NOTE sections, with the types
257 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
258 * same info about the command name and command line, so it probably
259 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
260 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
261 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
262 * the SunOS 5.x file command relies on this (and prefers the latter).
264 * The signal number probably appears in a section of type NT_PRSTATUS,
265 * but that's also rather OS-dependent, in ways that are harder to
266 * dissect with heuristics, so I'm not bothering with the signal number.
267 * (I suppose the signal number could be of interest in situations where
268 * you don't have the binary of the program that dropped core; if you
269 * *do* have that binary, the debugger will probably tell you what
273 #define OS_STYLE_SVR4 0
274 #define OS_STYLE_FREEBSD 1
275 #define OS_STYLE_NETBSD 2
277 private const char os_style_names[][8] = {
283 #define FLAGS_DID_CORE 0x01
284 #define FLAGS_DID_NOTE 0x02
285 #define FLAGS_DID_BUILD_ID 0x04
286 #define FLAGS_DID_CORE_STYLE 0x08
287 #define FLAGS_IS_CORE 0x10
290 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
291 int num, size_t size, off_t fsize, int *flags)
296 unsigned char nbuf[BUFSIZ];
299 if (size != xph_sizeof) {
300 if (file_printf(ms, ", corrupted program header size") == -1)
306 * Loop through all the program headers.
308 for ( ; num; num--) {
309 if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
315 if (xph_offset > fsize) {
316 /* Perhaps warn here */
320 if (xph_type != PT_NOTE)
324 * This is a PT_NOTE section; loop through all the notes
327 len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
328 if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
334 if (offset >= (size_t)bufsize)
336 offset = donote(ms, nbuf, offset, (size_t)bufsize,
337 clazz, swap, 4, flags);
348 do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
351 (void)memcpy(&desc, v, sizeof(desc));
352 desc = elf_getu32(swap, desc);
354 if (file_printf(ms, ", for NetBSD") == -1)
357 * The version number used to be stuck as 199905, and was thus
358 * basically content-free. Newer versions of NetBSD have fixed
359 * this and now use the encoding of __NetBSD_Version__:
365 * r = release ["",A-Z,Z[A-Z] but numeric]
368 if (desc > 100000000U) {
369 uint32_t ver_patch = (desc / 100) % 100;
370 uint32_t ver_rel = (desc / 10000) % 100;
371 uint32_t ver_min = (desc / 1000000) % 100;
372 uint32_t ver_maj = desc / 100000000;
374 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
376 if (ver_rel == 0 && ver_patch != 0) {
377 if (file_printf(ms, ".%u", ver_patch) == -1)
379 } else if (ver_rel != 0) {
380 while (ver_rel > 26) {
381 if (file_printf(ms, "Z") == -1)
385 if (file_printf(ms, "%c", 'A' + ver_rel - 1)
393 do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
397 (void)memcpy(&desc, v, sizeof(desc));
398 desc = elf_getu32(swap, desc);
399 if (file_printf(ms, ", for FreeBSD") == -1)
403 * Contents is __FreeBSD_version, whose relation to OS
404 * versions is defined by a huge table in the Porter's
405 * Handbook. This is the general scheme:
408 * Mmp000 (before 4.10)
409 * Mmi0p0 (before 5.0)
412 * Development branches:
413 * Mmpxxx (before 4.6)
414 * Mmp1xx (before 4.10)
415 * Mmi1xx (before 5.0)
421 * i = minor version increment (491000 -> 4.10)
425 * The first release of FreeBSD to use ELF by default
428 if (desc == 460002) {
429 if (file_printf(ms, " 4.6.2") == -1)
431 } else if (desc < 460100) {
432 if (file_printf(ms, " %d.%d", desc / 100000,
433 desc / 10000 % 10) == -1)
435 if (desc / 1000 % 10 > 0)
436 if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
438 if ((desc % 1000 > 0) || (desc % 100000 == 0))
439 if (file_printf(ms, " (%d)", desc) == -1)
441 } else if (desc < 500000) {
442 if (file_printf(ms, " %d.%d", desc / 100000,
443 desc / 10000 % 10 + desc / 1000 % 10) == -1)
445 if (desc / 100 % 10 > 0) {
446 if (file_printf(ms, " (%d)", desc) == -1)
448 } else if (desc / 10 % 10 > 0) {
449 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
453 if (file_printf(ms, " %d.%d", desc / 100000,
454 desc / 1000 % 100) == -1)
456 if ((desc / 100 % 10 > 0) ||
457 (desc % 100000 / 100 == 0)) {
458 if (file_printf(ms, " (%d)", desc) == -1)
460 } else if (desc / 10 % 10 > 0) {
461 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
468 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
469 int clazz, int swap, size_t align, int *flags)
477 uint32_t namesz, descsz;
478 unsigned char *nbuf = CAST(unsigned char *, vbuf);
480 (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
481 offset += xnh_sizeof;
485 if ((namesz == 0) && (descsz == 0)) {
487 * We're out of note headers.
489 return (offset >= size) ? offset : size;
492 if (namesz & 0x80000000) {
493 (void)file_printf(ms, ", bad note name size 0x%lx",
494 (unsigned long)namesz);
498 if (descsz & 0x80000000) {
499 (void)file_printf(ms, ", bad note description size 0x%lx",
500 (unsigned long)descsz);
506 doff = ELF_ALIGN(offset + namesz);
508 if (offset + namesz > size) {
510 * We're past the end of the buffer.
515 offset = ELF_ALIGN(doff + descsz);
516 if (doff + descsz > size) {
518 * We're past the end of the buffer.
520 return (offset >= size) ? offset : size;
523 if ((*flags & (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) ==
524 (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID))
527 if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
528 xnh_type == NT_GNU_VERSION && descsz == 2) {
529 file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
531 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
532 xnh_type == NT_GNU_VERSION && descsz == 16) {
534 (void)memcpy(desc, &nbuf[doff], sizeof(desc));
536 if (file_printf(ms, ", for GNU/") == -1)
538 switch (elf_getu32(swap, desc[0])) {
540 if (file_printf(ms, "Linux") == -1)
544 if (file_printf(ms, "Hurd") == -1)
548 if (file_printf(ms, "Solaris") == -1)
551 case GNU_OS_KFREEBSD:
552 if (file_printf(ms, "kFreeBSD") == -1)
556 if (file_printf(ms, "kNetBSD") == -1)
560 if (file_printf(ms, "<unknown>") == -1)
563 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
564 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
566 *flags |= FLAGS_DID_NOTE;
570 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
571 xnh_type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) {
574 if (file_printf(ms, ", BuildID[%s]=", descsz == 16 ? "md5/uuid" :
577 (void)memcpy(desc, &nbuf[doff], descsz);
578 for (i = 0; i < descsz; i++)
579 if (file_printf(ms, "%02x", desc[i]) == -1)
581 *flags |= FLAGS_DID_BUILD_ID;
584 if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 &&
585 xnh_type == NT_NETBSD_PAX && descsz == 4) {
586 static const char *pax[] = {
598 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
599 desc = elf_getu32(swap, desc);
601 if (desc && file_printf(ms, ", PaX: ") == -1)
604 for (i = 0; i < __arraycount(pax); i++) {
605 if (((1 << i) & desc) == 0)
607 if (file_printf(ms, "%s%s", did++ ? "," : "",
613 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
615 case NT_NETBSD_VERSION:
617 do_note_netbsd_version(ms, swap, &nbuf[doff]);
618 *flags |= FLAGS_DID_NOTE;
622 case NT_NETBSD_MARCH:
623 if (file_printf(ms, ", compiled for: %.*s", (int)descsz,
624 (const char *)&nbuf[doff]) == -1)
627 case NT_NETBSD_CMODEL:
628 if (file_printf(ms, ", compiler model: %.*s",
629 (int)descsz, (const char *)&nbuf[doff]) == -1)
633 if (file_printf(ms, ", note=%u", xnh_type) == -1)
640 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) {
641 if (xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
642 do_note_freebsd_version(ms, swap, &nbuf[doff]);
643 *flags |= FLAGS_DID_NOTE;
648 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
649 xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
650 if (file_printf(ms, ", for OpenBSD") == -1)
652 /* Content of note is always 0 */
653 *flags |= FLAGS_DID_NOTE;
657 if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
658 xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
660 if (file_printf(ms, ", for DragonFly") == -1)
662 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
663 desc = elf_getu32(swap, desc);
664 if (file_printf(ms, " %d.%d.%d", desc / 100000,
665 desc / 10000 % 10, desc % 10000) == -1)
667 *flags |= FLAGS_DID_NOTE;
673 * Sigh. The 2.0.36 kernel in Debian 2.1, at
674 * least, doesn't correctly implement name
675 * sections, in core dumps, as specified by
676 * the "Program Linking" section of "UNIX(R) System
677 * V Release 4 Programmer's Guide: ANSI C and
678 * Programming Support Tools", because my copy
679 * clearly says "The first 'namesz' bytes in 'name'
680 * contain a *null-terminated* [emphasis mine]
681 * character representation of the entry's owner
682 * or originator", but the 2.0.36 kernel code
683 * doesn't include the terminating null in the
686 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
687 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
688 os_style = OS_STYLE_SVR4;
691 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
692 os_style = OS_STYLE_FREEBSD;
695 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
697 os_style = OS_STYLE_NETBSD;
701 if ((*flags & FLAGS_DID_CORE) != 0)
704 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
705 if (file_printf(ms, ", %s-style", os_style_names[os_style])
708 *flags |= FLAGS_DID_CORE_STYLE;
712 case OS_STYLE_NETBSD:
713 if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
716 * Extract the program name. It is at
717 * offset 0x7c, and is up to 32-bytes,
718 * including the terminating NUL.
720 if (file_printf(ms, ", from '%.31s'",
721 &nbuf[doff + 0x7c]) == -1)
725 * Extract the signal number. It is at
728 (void)memcpy(&signo, &nbuf[doff + 0x08],
730 if (file_printf(ms, " (signal %u)",
731 elf_getu32(swap, signo)) == -1)
733 *flags |= FLAGS_DID_CORE;
739 if (xnh_type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
743 * Extract the program name. We assume
744 * it to be 16 characters (that's what it
745 * is in SunOS 5.x and Linux).
747 * Unfortunately, it's at a different offset
748 * in various OSes, so try multiple offsets.
749 * If the characters aren't all printable,
752 for (i = 0; i < NOFFSETS; i++) {
753 unsigned char *cname, *cp;
754 size_t reloffset = prpsoffsets(i);
755 size_t noffset = doff + reloffset;
757 for (j = 0; j < 16; j++, noffset++,
760 * Make sure we're not past
761 * the end of the buffer; if
762 * we are, just give up.
768 * Make sure we're not past
769 * the end of the contents;
770 * if we are, this obviously
771 * isn't the right offset.
773 if (reloffset >= descsz)
795 if (!isprint(c) || isquote(c))
804 * Try next offsets, in case this match is
805 * in the middle of a string.
807 for (k = i + 1 ; k < NOFFSETS ; k++) {
810 if (prpsoffsets(k) >= prpsoffsets(i))
812 for (no = doff + prpsoffsets(k);
813 no < doff + prpsoffsets(i); no++)
815 && isprint(nbuf[no]);
820 cname = (unsigned char *)
821 &nbuf[doff + prpsoffsets(i)];
822 for (cp = cname; *cp && isprint(*cp); cp++)
825 * Linux apparently appends a space at the end
826 * of the command line: remove it.
828 while (cp > cname && isspace(cp[-1]))
830 if (file_printf(ms, ", from '%.*s'",
831 (int)(cp - cname), cname) == -1)
833 *flags |= FLAGS_DID_CORE;
846 /* SunOS 5.x hardware capability descriptions */
847 typedef struct cap_desc {
852 static const cap_desc_t cap_desc_sparc[] = {
853 { AV_SPARC_MUL32, "MUL32" },
854 { AV_SPARC_DIV32, "DIV32" },
855 { AV_SPARC_FSMULD, "FSMULD" },
856 { AV_SPARC_V8PLUS, "V8PLUS" },
857 { AV_SPARC_POPC, "POPC" },
858 { AV_SPARC_VIS, "VIS" },
859 { AV_SPARC_VIS2, "VIS2" },
860 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
861 { AV_SPARC_FMAF, "FMAF" },
862 { AV_SPARC_FJFMAU, "FJFMAU" },
863 { AV_SPARC_IMA, "IMA" },
867 static const cap_desc_t cap_desc_386[] = {
868 { AV_386_FPU, "FPU" },
869 { AV_386_TSC, "TSC" },
870 { AV_386_CX8, "CX8" },
871 { AV_386_SEP, "SEP" },
872 { AV_386_AMD_SYSC, "AMD_SYSC" },
873 { AV_386_CMOV, "CMOV" },
874 { AV_386_MMX, "MMX" },
875 { AV_386_AMD_MMX, "AMD_MMX" },
876 { AV_386_AMD_3DNow, "AMD_3DNow" },
877 { AV_386_AMD_3DNowx, "AMD_3DNowx" },
878 { AV_386_FXSR, "FXSR" },
879 { AV_386_SSE, "SSE" },
880 { AV_386_SSE2, "SSE2" },
881 { AV_386_PAUSE, "PAUSE" },
882 { AV_386_SSE3, "SSE3" },
883 { AV_386_MON, "MON" },
884 { AV_386_CX16, "CX16" },
885 { AV_386_AHF, "AHF" },
886 { AV_386_TSCP, "TSCP" },
887 { AV_386_AMD_SSE4A, "AMD_SSE4A" },
888 { AV_386_POPCNT, "POPCNT" },
889 { AV_386_AMD_LZCNT, "AMD_LZCNT" },
890 { AV_386_SSSE3, "SSSE3" },
891 { AV_386_SSE4_1, "SSE4.1" },
892 { AV_386_SSE4_2, "SSE4.2" },
897 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
898 size_t size, off_t fsize, int *flags, int mach, int strtab)
904 off_t noff, coff, name_off;
905 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
906 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
909 if (size != xsh_sizeof) {
910 if (file_printf(ms, ", corrupted section header size") == -1)
915 /* Read offset of name section to be able to read section names later */
916 if (pread(fd, xsh_addr, xsh_sizeof, off + size * strtab) == -1) {
920 name_off = xsh_offset;
922 for ( ; num; num--) {
923 /* Read the name of this section. */
924 if (pread(fd, name, sizeof(name), name_off + xsh_name) == -1) {
928 name[sizeof(name) - 1] = '\0';
929 if (strcmp(name, ".debug_info") == 0)
932 if (pread(fd, xsh_addr, xsh_sizeof, off) == -1) {
938 /* Things we can determine before we seek */
947 if (xsh_offset > fsize) {
948 /* Perhaps warn here */
954 /* Things we can determine when we seek */
957 if ((nbuf = malloc(xsh_size)) == NULL) {
958 file_error(ms, errno, "Cannot allocate memory"
962 if (pread(fd, nbuf, xsh_size, xsh_offset) == -1) {
970 if (noff >= (off_t)xsh_size)
972 noff = donote(ms, nbuf, (size_t)noff,
973 xsh_size, clazz, swap, 4, flags);
991 if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
999 char cbuf[/*CONSTCOND*/
1000 MAX(sizeof cap32, sizeof cap64)];
1001 if ((coff += xcap_sizeof) > (off_t)xsh_size)
1003 if (read(fd, cbuf, (size_t)xcap_sizeof) !=
1004 (ssize_t)xcap_sizeof) {
1008 (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
1013 cap_hw1 |= xcap_val;
1016 cap_sf1 |= xcap_val;
1020 ", with unknown capability "
1021 "0x%" INT64_T_FORMAT "x = 0x%"
1023 (unsigned long long)xcap_tag,
1024 (unsigned long long)xcap_val) == -1)
1036 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1039 const cap_desc_t *cdp;
1042 case EM_SPARC32PLUS:
1044 cdp = cap_desc_sparc;
1055 if (file_printf(ms, ", uses") == -1)
1058 while (cdp->cd_name) {
1059 if (cap_hw1 & cdp->cd_mask) {
1061 " %s", cdp->cd_name) == -1)
1063 cap_hw1 &= ~cdp->cd_mask;
1069 " unknown hardware capability 0x%"
1071 (unsigned long long)cap_hw1) == -1)
1075 " hardware capability 0x%" INT64_T_FORMAT "x",
1076 (unsigned long long)cap_hw1) == -1)
1081 if (cap_sf1 & SF1_SUNW_FPUSED) {
1083 (cap_sf1 & SF1_SUNW_FPKNWN)
1084 ? ", uses frame pointer"
1085 : ", not known to use frame pointer") == -1)
1088 cap_sf1 &= ~SF1_SUNW_MASK;
1091 ", with unknown software capability 0x%"
1093 (unsigned long long)cap_sf1) == -1)
1100 * Look through the program headers of an executable image, searching
1101 * for a PT_INTERP section; if one is found, it's dynamically linked,
1102 * otherwise it's statically linked.
1105 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1106 int num, size_t size, off_t fsize, int *flags, int sh_num)
1110 const char *linking_style = "statically";
1111 const char *shared_libraries = "";
1112 unsigned char nbuf[BUFSIZ];
1114 size_t offset, align, len;
1116 if (size != xph_sizeof) {
1117 if (file_printf(ms, ", corrupted program header size") == -1)
1122 for ( ; num; num--) {
1123 if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
1130 /* Things we can determine before we seek */
1133 linking_style = "dynamically";
1136 shared_libraries = " (uses shared libs)";
1139 if (xph_offset > fsize) {
1140 /* Maybe warn here? */
1146 /* Things we can determine when we seek */
1149 if ((align = xph_align) & 0x80000000UL) {
1151 ", invalid note alignment 0x%lx",
1152 (unsigned long)align) == -1)
1159 * This is a PT_NOTE section; loop through all the notes
1162 len = xph_filesz < sizeof(nbuf) ? xph_filesz
1164 bufsize = pread(fd, nbuf, len, xph_offset);
1165 if (bufsize == -1) {
1171 if (offset >= (size_t)bufsize)
1173 offset = donote(ms, nbuf, offset,
1174 (size_t)bufsize, clazz, swap, align,
1184 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
1192 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
1197 char c[sizeof (int32_t)];
1204 Elf32_Ehdr elf32hdr;
1205 Elf64_Ehdr elf64hdr;
1208 if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
1211 * ELF executables have multiple section headers in arbitrary
1212 * file locations and thus file(1) cannot determine it from easily.
1213 * Instead we traverse thru all section headers until a symbol table
1214 * one is found or else the binary is stripped.
1215 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
1217 if (buf[EI_MAG0] != ELFMAG0
1218 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1219 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1223 * If we cannot seek, it must be a pipe, socket or fifo.
1225 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1226 fd = file_pipe2file(ms, fd, buf, nbytes);
1228 if (fstat(fd, &st) == -1) {
1234 clazz = buf[EI_CLASS];
1239 #define elf_getu(a, b) elf_getu32(a, b)
1241 #define elfhdr elf32hdr
1242 #include "elfclass.h"
1245 #define elf_getu(a, b) elf_getu64(a, b)
1247 #define elfhdr elf64hdr
1248 #include "elfclass.h"
1250 if (file_printf(ms, ", unknown class %d", clazz) == -1)