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.81 2008/11/04 16:38:28 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, 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 1024
67 toomany(struct magic_set *ms, const char *name, uint16_t num)
69 if (file_printf(ms, ", too many %s header sections (%u)", name, num
76 getu16(int swap, uint16_t value)
86 retval.c[0] = tmpval.c[1];
87 retval.c[1] = tmpval.c[0];
95 getu32(int swap, uint32_t value)
105 retval.c[0] = tmpval.c[3];
106 retval.c[1] = tmpval.c[2];
107 retval.c[2] = tmpval.c[1];
108 retval.c[3] = tmpval.c[0];
116 getu64(int swap, uint64_t value)
126 retval.c[0] = tmpval.c[7];
127 retval.c[1] = tmpval.c[6];
128 retval.c[2] = tmpval.c[5];
129 retval.c[3] = tmpval.c[4];
130 retval.c[4] = tmpval.c[3];
131 retval.c[5] = tmpval.c[2];
132 retval.c[6] = tmpval.c[1];
133 retval.c[7] = tmpval.c[0];
140 #define elf_getu16(swap, value) getu16(swap, value)
141 #define elf_getu32(swap, value) getu32(swap, value)
142 #ifdef USE_ARRAY_FOR_64BIT_TYPES
143 # define elf_getu64(swap, array) \
144 ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
145 (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
147 # define elf_getu64(swap, value) getu64(swap, value)
150 #define xsh_addr (clazz == ELFCLASS32 \
153 #define xsh_sizeof (clazz == ELFCLASS32 \
156 #define xsh_size (clazz == ELFCLASS32 \
157 ? elf_getu32(swap, sh32.sh_size) \
158 : elf_getu64(swap, sh64.sh_size))
159 #define xsh_offset (clazz == ELFCLASS32 \
160 ? elf_getu32(swap, sh32.sh_offset) \
161 : elf_getu64(swap, sh64.sh_offset))
162 #define xsh_type (clazz == ELFCLASS32 \
163 ? elf_getu32(swap, sh32.sh_type) \
164 : elf_getu32(swap, sh64.sh_type))
165 #define xph_addr (clazz == ELFCLASS32 \
168 #define xph_sizeof (clazz == ELFCLASS32 \
171 #define xph_type (clazz == ELFCLASS32 \
172 ? elf_getu32(swap, ph32.p_type) \
173 : elf_getu32(swap, ph64.p_type))
174 #define xph_offset (off_t)(clazz == ELFCLASS32 \
175 ? elf_getu32(swap, ph32.p_offset) \
176 : elf_getu64(swap, ph64.p_offset))
177 #define xph_align (size_t)((clazz == ELFCLASS32 \
178 ? (off_t) (ph32.p_align ? \
179 elf_getu32(swap, ph32.p_align) : 4) \
180 : (off_t) (ph64.p_align ? \
181 elf_getu64(swap, ph64.p_align) : 4)))
182 #define xph_filesz (size_t)((clazz == ELFCLASS32 \
183 ? elf_getu32(swap, ph32.p_filesz) \
184 : elf_getu64(swap, ph64.p_filesz)))
185 #define xnh_addr (clazz == ELFCLASS32 \
188 #define xph_memsz (size_t)((clazz == ELFCLASS32 \
189 ? elf_getu32(swap, ph32.p_memsz) \
190 : elf_getu64(swap, ph64.p_memsz)))
191 #define xnh_sizeof (clazz == ELFCLASS32 \
194 #define xnh_type (clazz == ELFCLASS32 \
195 ? elf_getu32(swap, nh32.n_type) \
196 : elf_getu32(swap, nh64.n_type))
197 #define xnh_namesz (clazz == ELFCLASS32 \
198 ? elf_getu32(swap, nh32.n_namesz) \
199 : elf_getu32(swap, nh64.n_namesz))
200 #define xnh_descsz (clazz == ELFCLASS32 \
201 ? elf_getu32(swap, nh32.n_descsz) \
202 : elf_getu32(swap, nh64.n_descsz))
203 #define prpsoffsets(i) (clazz == ELFCLASS32 \
206 #define xcap_addr (clazz == ELFCLASS32 \
209 #define xcap_sizeof (clazz == ELFCLASS32 \
212 #define xcap_tag (clazz == ELFCLASS32 \
213 ? elf_getu32(swap, cap32.c_tag) \
214 : elf_getu64(swap, cap64.c_tag))
215 #define xcap_val (clazz == ELFCLASS32 \
216 ? elf_getu32(swap, cap32.c_un.c_val) \
217 : elf_getu64(swap, cap64.c_un.c_val))
221 * Try larger offsets first to avoid false matches
222 * from earlier data that happen to look like strings.
224 static const size_t prpsoffsets32[] = {
226 104, /* SunOS 5.x (command line) */
227 88, /* SunOS 5.x (short name) */
228 #endif /* USE_NT_PSINFO */
230 100, /* SunOS 5.x (command line) */
231 84, /* SunOS 5.x (short name) */
233 44, /* Linux (command line) */
234 28, /* Linux 2.0.36 (short name) */
239 static const size_t prpsoffsets64[] = {
241 152, /* SunOS 5.x (command line) */
242 136, /* SunOS 5.x (short name) */
243 #endif /* USE_NT_PSINFO */
245 136, /* SunOS 5.x, 64-bit (command line) */
246 120, /* SunOS 5.x, 64-bit (short name) */
248 56, /* Linux (command line) */
249 40, /* Linux (tested on core from 2.4.x, short name) */
251 16, /* FreeBSD, 64-bit */
254 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
255 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
257 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
260 * Look through the program headers of an executable image, searching
261 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
262 * "FreeBSD"; if one is found, try looking in various places in its
263 * contents for a 16-character string containing only printable
264 * characters - if found, that string should be the name of the program
265 * that dropped core. Note: right after that 16-character string is,
266 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
267 * Linux, a longer string (80 characters, in 5.x, probably other
268 * SVR4-flavored systems, and Linux) containing the start of the
269 * command line for that program.
271 * SunOS 5.x core files contain two PT_NOTE sections, with the types
272 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
273 * same info about the command name and command line, so it probably
274 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
275 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
276 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
277 * the SunOS 5.x file command relies on this (and prefers the latter).
279 * The signal number probably appears in a section of type NT_PRSTATUS,
280 * but that's also rather OS-dependent, in ways that are harder to
281 * dissect with heuristics, so I'm not bothering with the signal number.
282 * (I suppose the signal number could be of interest in situations where
283 * you don't have the binary of the program that dropped core; if you
284 * *do* have that binary, the debugger will probably tell you what
288 #define OS_STYLE_SVR4 0
289 #define OS_STYLE_FREEBSD 1
290 #define OS_STYLE_NETBSD 2
292 private const char os_style_names[][8] = {
298 #define FLAGS_DID_CORE 1
299 #define FLAGS_DID_NOTE 2
300 #define FLAGS_DID_CORE_STYLE 4
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];
314 if (fstat(fd, &st) < 0) {
319 if (size != xph_sizeof) {
320 if (file_printf(ms, ", corrupted program header size") == -1)
326 * Loop through all the program headers.
328 for ( ; num; num--) {
329 if ((savedoffset = lseek(fd, off, SEEK_SET)) == (off_t)-1) {
333 if (read(fd, xph_addr, xph_sizeof) == -1) {
337 if (xph_offset > fsize) {
338 if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
346 if (xph_type != PT_NOTE)
350 * This is a PT_NOTE section; loop through all the notes
353 if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
357 bufsize = read(fd, nbuf,
358 ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
365 if (offset >= (size_t)bufsize)
367 offset = donote(ms, nbuf, offset, (size_t)bufsize,
368 clazz, swap, 4, flags);
379 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
380 int clazz, int swap, size_t align, int *flags)
388 uint32_t namesz, descsz;
389 unsigned char *nbuf = CAST(unsigned char *, vbuf);
391 (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
392 offset += xnh_sizeof;
396 if ((namesz == 0) && (descsz == 0)) {
398 * We're out of note headers.
400 return (offset >= size) ? offset : size;
403 if (namesz & 0x80000000) {
404 (void)file_printf(ms, ", bad note name size 0x%lx",
405 (unsigned long)namesz);
409 if (descsz & 0x80000000) {
410 (void)file_printf(ms, ", bad note description size 0x%lx",
411 (unsigned long)descsz);
417 doff = ELF_ALIGN(offset + namesz);
419 if (offset + namesz > size) {
421 * We're past the end of the buffer.
426 offset = ELF_ALIGN(doff + descsz);
427 if (doff + descsz > size) {
429 * We're past the end of the buffer.
431 return (offset >= size) ? offset : size;
434 if (*flags & FLAGS_DID_NOTE)
437 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
438 xnh_type == NT_GNU_VERSION && descsz == 16) {
440 (void)memcpy(desc, &nbuf[doff], sizeof(desc));
442 if (file_printf(ms, ", for GNU/") == -1)
444 switch (elf_getu32(swap, desc[0])) {
446 if (file_printf(ms, "Linux") == -1)
450 if (file_printf(ms, "Hurd") == -1)
454 if (file_printf(ms, "Solaris") == -1)
457 case GNU_OS_KFREEBSD:
458 if (file_printf(ms, "kFreeBSD") == -1)
462 if (file_printf(ms, "kNetBSD") == -1)
466 if (file_printf(ms, "<unknown>") == -1)
469 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
470 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
472 *flags |= FLAGS_DID_NOTE;
476 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
477 xnh_type == NT_NETBSD_VERSION && descsz == 4) {
479 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
480 desc = elf_getu32(swap, desc);
482 if (file_printf(ms, ", for NetBSD") == -1)
485 * The version number used to be stuck as 199905, and was thus
486 * basically content-free. Newer versions of NetBSD have fixed
487 * this and now use the encoding of __NetBSD_Version__:
493 * r = release ["",A-Z,Z[A-Z] but numeric]
496 if (desc > 100000000U) {
497 uint32_t ver_patch = (desc / 100) % 100;
498 uint32_t ver_rel = (desc / 10000) % 100;
499 uint32_t ver_min = (desc / 1000000) % 100;
500 uint32_t ver_maj = desc / 100000000;
502 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
504 if (ver_rel == 0 && ver_patch != 0) {
505 if (file_printf(ms, ".%u", ver_patch) == -1)
507 } else if (ver_rel != 0) {
508 while (ver_rel > 26) {
509 if (file_printf(ms, "Z") == -1)
513 if (file_printf(ms, "%c", 'A' + ver_rel - 1)
518 *flags |= FLAGS_DID_NOTE;
522 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
523 xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
525 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
526 desc = elf_getu32(swap, desc);
527 if (file_printf(ms, ", for FreeBSD") == -1)
531 * Contents is __FreeBSD_version, whose relation to OS
532 * versions is defined by a huge table in the Porter's
533 * Handbook. This is the general scheme:
536 * Mmp000 (before 4.10)
537 * Mmi0p0 (before 5.0)
540 * Development branches:
541 * Mmpxxx (before 4.6)
542 * Mmp1xx (before 4.10)
543 * Mmi1xx (before 5.0)
549 * i = minor version increment (491000 -> 4.10)
553 * The first release of FreeBSD to use ELF by default
556 if (desc == 460002) {
557 if (file_printf(ms, " 4.6.2") == -1)
559 } else if (desc < 460100) {
560 if (file_printf(ms, " %d.%d", desc / 100000,
561 desc / 10000 % 10) == -1)
563 if (desc / 1000 % 10 > 0)
564 if (file_printf(ms, ".%d", desc / 1000 % 10)
567 if ((desc % 1000 > 0) || (desc % 100000 == 0))
568 if (file_printf(ms, " (%d)", desc) == -1)
570 } else if (desc < 500000) {
571 if (file_printf(ms, " %d.%d", desc / 100000,
572 desc / 10000 % 10 + desc / 1000 % 10) == -1)
574 if (desc / 100 % 10 > 0) {
575 if (file_printf(ms, " (%d)", desc) == -1)
577 } else if (desc / 10 % 10 > 0) {
578 if (file_printf(ms, ".%d", desc / 10 % 10)
583 if (file_printf(ms, " %d.%d", desc / 100000,
584 desc / 1000 % 100) == -1)
586 if ((desc / 100 % 10 > 0) ||
587 (desc % 100000 / 100 == 0)) {
588 if (file_printf(ms, " (%d)", desc) == -1)
590 } else if (desc / 10 % 10 > 0) {
591 if (file_printf(ms, ".%d", desc / 10 % 10)
596 *flags |= FLAGS_DID_NOTE;
600 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
601 xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
602 if (file_printf(ms, ", for OpenBSD") == -1)
604 /* Content of note is always 0 */
605 *flags |= FLAGS_DID_NOTE;
609 if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
610 xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
612 if (file_printf(ms, ", for DragonFly") == -1)
614 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
615 desc = elf_getu32(swap, desc);
616 if (file_printf(ms, " %d.%d.%d", desc / 100000,
617 desc / 10000 % 10, desc % 10000) == -1)
619 *flags |= FLAGS_DID_NOTE;
625 * Sigh. The 2.0.36 kernel in Debian 2.1, at
626 * least, doesn't correctly implement name
627 * sections, in core dumps, as specified by
628 * the "Program Linking" section of "UNIX(R) System
629 * V Release 4 Programmer's Guide: ANSI C and
630 * Programming Support Tools", because my copy
631 * clearly says "The first 'namesz' bytes in 'name'
632 * contain a *null-terminated* [emphasis mine]
633 * character representation of the entry's owner
634 * or originator", but the 2.0.36 kernel code
635 * doesn't include the terminating null in the
638 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
639 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
640 os_style = OS_STYLE_SVR4;
643 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
644 os_style = OS_STYLE_FREEBSD;
647 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
649 os_style = OS_STYLE_NETBSD;
653 if ((*flags & FLAGS_DID_CORE) != 0)
656 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
657 if (file_printf(ms, ", %s-style", os_style_names[os_style])
660 *flags |= FLAGS_DID_CORE_STYLE;
664 case OS_STYLE_NETBSD:
665 if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
668 * Extract the program name. It is at
669 * offset 0x7c, and is up to 32-bytes,
670 * including the terminating NUL.
672 if (file_printf(ms, ", from '%.31s'",
673 &nbuf[doff + 0x7c]) == -1)
677 * Extract the signal number. It is at
680 (void)memcpy(&signo, &nbuf[doff + 0x08],
682 if (file_printf(ms, " (signal %u)",
683 elf_getu32(swap, signo)) == -1)
685 *flags |= FLAGS_DID_CORE;
691 if (xnh_type == NT_PRPSINFO) {
695 * Extract the program name. We assume
696 * it to be 16 characters (that's what it
697 * is in SunOS 5.x and Linux).
699 * Unfortunately, it's at a different offset
700 * in various OSes, so try multiple offsets.
701 * If the characters aren't all printable,
704 for (i = 0; i < NOFFSETS; i++) {
705 unsigned char *cname, *cp;
706 size_t reloffset = prpsoffsets(i);
707 size_t noffset = doff + reloffset;
708 for (j = 0; j < 16; j++, noffset++,
711 * Make sure we're not past
712 * the end of the buffer; if
713 * we are, just give up.
719 * Make sure we're not past
720 * the end of the contents;
721 * if we are, this obviously
722 * isn't the right offset.
724 if (reloffset >= descsz)
746 if (!isprint(c) || isquote(c))
753 cname = (unsigned char *)
754 &nbuf[doff + prpsoffsets(i)];
755 for (cp = cname; *cp && isprint(*cp); cp++)
758 * Linux apparently appends a space at the end
759 * of the command line: remove it.
761 while (cp > cname && isspace(cp[-1]))
763 if (file_printf(ms, ", from '%.*s'",
764 (int)(cp - cname), cname) == -1)
766 *flags |= FLAGS_DID_CORE;
779 /* SunOS 5.x hardware capability descriptions */
780 typedef struct cap_desc {
785 static const cap_desc_t cap_desc_sparc[] = {
786 { AV_SPARC_MUL32, "MUL32" },
787 { AV_SPARC_DIV32, "DIV32" },
788 { AV_SPARC_FSMULD, "FSMULD" },
789 { AV_SPARC_V8PLUS, "V8PLUS" },
790 { AV_SPARC_POPC, "POPC" },
791 { AV_SPARC_VIS, "VIS" },
792 { AV_SPARC_VIS2, "VIS2" },
793 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
794 { AV_SPARC_FMAF, "FMAF" },
795 { AV_SPARC_FJFMAU, "FJFMAU" },
796 { AV_SPARC_IMA, "IMA" },
800 static const cap_desc_t cap_desc_386[] = {
801 { AV_386_FPU, "FPU" },
802 { AV_386_TSC, "TSC" },
803 { AV_386_CX8, "CX8" },
804 { AV_386_SEP, "SEP" },
805 { AV_386_AMD_SYSC, "AMD_SYSC" },
806 { AV_386_CMOV, "CMOV" },
807 { AV_386_MMX, "MMX" },
808 { AV_386_AMD_MMX, "AMD_MMX" },
809 { AV_386_AMD_3DNow, "AMD_3DNow" },
810 { AV_386_AMD_3DNowx, "AMD_3DNowx" },
811 { AV_386_FXSR, "FXSR" },
812 { AV_386_SSE, "SSE" },
813 { AV_386_SSE2, "SSE2" },
814 { AV_386_PAUSE, "PAUSE" },
815 { AV_386_SSE3, "SSE3" },
816 { AV_386_MON, "MON" },
817 { AV_386_CX16, "CX16" },
818 { AV_386_AHF, "AHF" },
819 { AV_386_TSCP, "TSCP" },
820 { AV_386_AMD_SSE4A, "AMD_SSE4A" },
821 { AV_386_POPCNT, "POPCNT" },
822 { AV_386_AMD_LZCNT, "AMD_LZCNT" },
823 { AV_386_SSSE3, "SSSE3" },
824 { AV_386_SSE4_1, "SSE4.1" },
825 { AV_386_SSE4_2, "SSE4.2" },
830 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
831 size_t size, int *flags, int mach)
839 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
840 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
842 if (size != xsh_sizeof) {
843 if (file_printf(ms, ", corrupted section header size") == -1)
848 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
853 for ( ; num; num--) {
854 if (read(fd, xsh_addr, xsh_sizeof) == -1) {
866 if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
871 if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
872 file_error(ms, errno, "Cannot allocate memory"
876 if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
882 if (read(fd, nbuf, (size_t)xsh_size) !=
891 if (noff >= (off_t)xsh_size)
893 noff = donote(ms, nbuf, (size_t)noff,
894 (size_t)xsh_size, clazz, swap, 4,
899 if ((lseek(fd, off, SEEK_SET)) == (off_t)-1) {
911 if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
916 if (lseek(fd, (off_t)xsh_offset, SEEK_SET) ==
925 char cbuf[/*CONSTCOND*/
926 MAX(sizeof cap32, sizeof cap64)];
927 if ((coff += xcap_sizeof) >= (off_t)xsh_size)
929 if (read(fd, cbuf, (size_t)xcap_sizeof) !=
930 (ssize_t)xcap_sizeof) {
934 (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
946 ", with unknown capability "
948 (unsigned long long)xcap_tag,
949 (unsigned long long)xcap_val) == -1)
956 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
964 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
967 const cap_desc_t *cdp;
972 cdp = cap_desc_sparc;
983 if (file_printf(ms, ", uses") == -1)
986 while (cdp->cd_name) {
987 if (cap_hw1 & cdp->cd_mask) {
989 " %s", cdp->cd_name) == -1)
991 cap_hw1 &= ~cdp->cd_mask;
997 " unknown hardware capability 0x%llx",
998 (unsigned long long)cap_hw1) == -1)
1002 " hardware capability 0x%llx",
1003 (unsigned long long)cap_hw1) == -1)
1008 if (cap_sf1 & SF1_SUNW_FPUSED) {
1010 (cap_sf1 & SF1_SUNW_FPKNWN)
1011 ? ", uses frame pointer"
1012 : ", not known to use frame pointer") == -1)
1015 cap_sf1 &= ~SF1_SUNW_MASK;
1018 ", with unknown software capability 0x%llx",
1019 (unsigned long long)cap_sf1) == -1)
1026 * Look through the program headers of an executable image, searching
1027 * for a PT_INTERP section; if one is found, it's dynamically linked,
1028 * otherwise it's statically linked.
1031 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1032 int num, size_t size, off_t fsize, int *flags, int sh_num)
1036 const char *linking_style = "statically";
1037 const char *shared_libraries = "";
1038 unsigned char nbuf[BUFSIZ];
1040 size_t offset, align;
1041 off_t savedoffset = (off_t)-1;
1044 if (fstat(fd, &st) < 0) {
1049 if (size != xph_sizeof) {
1050 if (file_printf(ms, ", corrupted program header size") == -1)
1055 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
1060 for ( ; num; num--) {
1061 if (read(fd, xph_addr, xph_sizeof) == -1) {
1065 if (xph_offset > st.st_size && savedoffset != (off_t)-1) {
1066 if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
1073 if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
1078 if (xph_offset > fsize) {
1079 if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
1088 linking_style = "dynamically";
1091 shared_libraries = " (uses shared libs)";
1094 if ((align = xph_align) & 0x80000000) {
1096 ", invalid note alignment 0x%lx",
1097 (unsigned long)align) == -1)
1104 * This is a PT_NOTE section; loop through all the notes
1107 if (lseek(fd, xph_offset, SEEK_SET)
1112 bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
1113 xph_filesz : sizeof(nbuf)));
1114 if (bufsize == -1) {
1120 if (offset >= (size_t)bufsize)
1122 offset = donote(ms, nbuf, offset,
1123 (size_t)bufsize, clazz, swap, align,
1128 if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
1137 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
1145 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
1150 char c[sizeof (int32_t)];
1157 Elf32_Ehdr elf32hdr;
1158 Elf64_Ehdr elf64hdr;
1159 uint16_t type, phnum, shnum;
1161 if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
1164 * ELF executables have multiple section headers in arbitrary
1165 * file locations and thus file(1) cannot determine it from easily.
1166 * Instead we traverse thru all section headers until a symbol table
1167 * one is found or else the binary is stripped.
1168 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
1170 if (buf[EI_MAG0] != ELFMAG0
1171 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1172 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1176 * If we cannot seek, it must be a pipe, socket or fifo.
1178 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1179 fd = file_pipe2file(ms, fd, buf, nbytes);
1181 if (fstat(fd, &st) == -1) {
1187 clazz = buf[EI_CLASS];
1192 #define elf_getu(a, b) elf_getu32(a, b)
1194 #define elfhdr elf32hdr
1195 #include "elfclass.h"
1198 #define elf_getu(a, b) elf_getu64(a, b)
1200 #define elfhdr elf64hdr
1201 #include "elfclass.h"
1203 if (file_printf(ms, ", unknown class %d", clazz) == -1)