1 /* $NetBSD: readelf.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $ */
4 * Copyright (c) Christos Zoulas 2003.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 FILE_RCSID("@(#)$File: readelf.c,v 1.81 2008/11/04 16:38:28 christos Exp $")
35 __RCSID("$NetBSD: readelf.c,v 1.1.1.1 2009/05/08 16:35:06 christos Exp $");
51 private int dophn_core(struct magic_set
*, int, int, int, off_t
, int, size_t,
54 private int dophn_exec(struct magic_set
*, int, int, int, off_t
, int, size_t,
56 private int doshn(struct magic_set
*, int, int, int, off_t
, int, size_t, int *,
58 private size_t donote(struct magic_set
*, void *, size_t, size_t, int,
61 #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
63 #define isquote(c) (strchr("'\"`", (c)) != NULL)
65 private uint16_t getu16(int, uint16_t);
66 private uint32_t getu32(int, uint32_t);
67 private uint64_t getu64(int, uint64_t);
70 getu16(int swap
, uint16_t value
)
80 retval
.c
[0] = tmpval
.c
[1];
81 retval
.c
[1] = tmpval
.c
[0];
89 getu32(int swap
, uint32_t value
)
99 retval
.c
[0] = tmpval
.c
[3];
100 retval
.c
[1] = tmpval
.c
[2];
101 retval
.c
[2] = tmpval
.c
[1];
102 retval
.c
[3] = tmpval
.c
[0];
110 getu64(int swap
, uint64_t value
)
120 retval
.c
[0] = tmpval
.c
[7];
121 retval
.c
[1] = tmpval
.c
[6];
122 retval
.c
[2] = tmpval
.c
[5];
123 retval
.c
[3] = tmpval
.c
[4];
124 retval
.c
[4] = tmpval
.c
[3];
125 retval
.c
[5] = tmpval
.c
[2];
126 retval
.c
[6] = tmpval
.c
[1];
127 retval
.c
[7] = tmpval
.c
[0];
134 #define elf_getu16(swap, value) getu16(swap, value)
135 #define elf_getu32(swap, value) getu32(swap, value)
136 #ifdef USE_ARRAY_FOR_64BIT_TYPES
137 # define elf_getu64(swap, array) \
138 ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
139 (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
141 # define elf_getu64(swap, value) getu64(swap, value)
144 #define xsh_addr (clazz == ELFCLASS32 \
147 #define xsh_sizeof (clazz == ELFCLASS32 \
150 #define xsh_size (clazz == ELFCLASS32 \
151 ? elf_getu32(swap, sh32.sh_size) \
152 : elf_getu64(swap, sh64.sh_size))
153 #define xsh_offset (clazz == ELFCLASS32 \
154 ? elf_getu32(swap, sh32.sh_offset) \
155 : elf_getu64(swap, sh64.sh_offset))
156 #define xsh_type (clazz == ELFCLASS32 \
157 ? elf_getu32(swap, sh32.sh_type) \
158 : elf_getu32(swap, sh64.sh_type))
159 #define xph_addr (clazz == ELFCLASS32 \
162 #define xph_sizeof (clazz == ELFCLASS32 \
165 #define xph_type (clazz == ELFCLASS32 \
166 ? elf_getu32(swap, ph32.p_type) \
167 : elf_getu32(swap, ph64.p_type))
168 #define xph_offset (off_t)(clazz == ELFCLASS32 \
169 ? elf_getu32(swap, ph32.p_offset) \
170 : elf_getu64(swap, ph64.p_offset))
171 #define xph_align (size_t)((clazz == ELFCLASS32 \
172 ? (off_t) (ph32.p_align ? \
173 elf_getu32(swap, ph32.p_align) : 4) \
174 : (off_t) (ph64.p_align ? \
175 elf_getu64(swap, ph64.p_align) : 4)))
176 #define xph_filesz (size_t)((clazz == ELFCLASS32 \
177 ? elf_getu32(swap, ph32.p_filesz) \
178 : elf_getu64(swap, ph64.p_filesz)))
179 #define xnh_addr (clazz == ELFCLASS32 \
182 #define xph_memsz (size_t)((clazz == ELFCLASS32 \
183 ? elf_getu32(swap, ph32.p_memsz) \
184 : elf_getu64(swap, ph64.p_memsz)))
185 #define xnh_sizeof (clazz == ELFCLASS32 \
188 #define xnh_type (clazz == ELFCLASS32 \
189 ? elf_getu32(swap, nh32.n_type) \
190 : elf_getu32(swap, nh64.n_type))
191 #define xnh_namesz (clazz == ELFCLASS32 \
192 ? elf_getu32(swap, nh32.n_namesz) \
193 : elf_getu32(swap, nh64.n_namesz))
194 #define xnh_descsz (clazz == ELFCLASS32 \
195 ? elf_getu32(swap, nh32.n_descsz) \
196 : elf_getu32(swap, nh64.n_descsz))
197 #define prpsoffsets(i) (clazz == ELFCLASS32 \
200 #define xcap_addr (clazz == ELFCLASS32 \
203 #define xcap_sizeof (clazz == ELFCLASS32 \
206 #define xcap_tag (clazz == ELFCLASS32 \
207 ? elf_getu32(swap, cap32.c_tag) \
208 : elf_getu64(swap, cap64.c_tag))
209 #define xcap_val (clazz == ELFCLASS32 \
210 ? elf_getu32(swap, cap32.c_un.c_val) \
211 : elf_getu64(swap, cap64.c_un.c_val))
215 * Try larger offsets first to avoid false matches
216 * from earlier data that happen to look like strings.
218 static const size_t prpsoffsets32
[] = {
220 104, /* SunOS 5.x (command line) */
221 88, /* SunOS 5.x (short name) */
222 #endif /* USE_NT_PSINFO */
224 100, /* SunOS 5.x (command line) */
225 84, /* SunOS 5.x (short name) */
227 44, /* Linux (command line) */
228 28, /* Linux 2.0.36 (short name) */
233 static const size_t prpsoffsets64
[] = {
235 152, /* SunOS 5.x (command line) */
236 136, /* SunOS 5.x (short name) */
237 #endif /* USE_NT_PSINFO */
239 136, /* SunOS 5.x, 64-bit (command line) */
240 120, /* SunOS 5.x, 64-bit (short name) */
242 56, /* Linux (command line) */
243 40, /* Linux (tested on core from 2.4.x, short name) */
245 16, /* FreeBSD, 64-bit */
248 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
249 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
251 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
254 * Look through the program headers of an executable image, searching
255 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
256 * "FreeBSD"; if one is found, try looking in various places in its
257 * contents for a 16-character string containing only printable
258 * characters - if found, that string should be the name of the program
259 * that dropped core. Note: right after that 16-character string is,
260 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
261 * Linux, a longer string (80 characters, in 5.x, probably other
262 * SVR4-flavored systems, and Linux) containing the start of the
263 * command line for that program.
265 * SunOS 5.x core files contain two PT_NOTE sections, with the types
266 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
267 * same info about the command name and command line, so it probably
268 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
269 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
270 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
271 * the SunOS 5.x file command relies on this (and prefers the latter).
273 * The signal number probably appears in a section of type NT_PRSTATUS,
274 * but that's also rather OS-dependent, in ways that are harder to
275 * dissect with heuristics, so I'm not bothering with the signal number.
276 * (I suppose the signal number could be of interest in situations where
277 * you don't have the binary of the program that dropped core; if you
278 * *do* have that binary, the debugger will probably tell you what
282 #define OS_STYLE_SVR4 0
283 #define OS_STYLE_FREEBSD 1
284 #define OS_STYLE_NETBSD 2
286 private const char os_style_names
[][8] = {
292 #define FLAGS_DID_CORE 1
293 #define FLAGS_DID_NOTE 2
294 #define FLAGS_DID_CORE_STYLE 4
297 dophn_core(struct magic_set
*ms
, int clazz
, int swap
, int fd
, off_t off
,
298 int num
, size_t size
, off_t fsize
, int *flags
)
303 unsigned char nbuf
[BUFSIZ
];
308 if (fstat(fd
, &st
) < 0) {
313 if (size
!= xph_sizeof
) {
314 if (file_printf(ms
, ", corrupted program header size") == -1)
320 * Loop through all the program headers.
322 for ( ; num
; num
--) {
323 if ((savedoffset
= lseek(fd
, off
, SEEK_SET
)) == (off_t
)-1) {
327 if (read(fd
, xph_addr
, xph_sizeof
) == -1) {
331 if (xph_offset
> fsize
) {
332 if (lseek(fd
, savedoffset
, SEEK_SET
) == (off_t
)-1) {
340 if (xph_type
!= PT_NOTE
)
344 * This is a PT_NOTE section; loop through all the notes
347 if (lseek(fd
, xph_offset
, SEEK_SET
) == (off_t
)-1) {
351 bufsize
= read(fd
, nbuf
,
352 ((xph_filesz
< sizeof(nbuf
)) ? xph_filesz
: sizeof(nbuf
)));
359 if (offset
>= (size_t)bufsize
)
361 offset
= donote(ms
, nbuf
, offset
, (size_t)bufsize
,
362 clazz
, swap
, 4, flags
);
373 donote(struct magic_set
*ms
, void *vbuf
, size_t offset
, size_t size
,
374 int clazz
, int swap
, size_t align
, int *flags
)
382 uint32_t namesz
, descsz
;
383 unsigned char *nbuf
= CAST(unsigned char *, vbuf
);
385 (void)memcpy(xnh_addr
, &nbuf
[offset
], xnh_sizeof
);
386 offset
+= xnh_sizeof
;
390 if ((namesz
== 0) && (descsz
== 0)) {
392 * We're out of note headers.
394 return (offset
>= size
) ? offset
: size
;
397 if (namesz
& 0x80000000) {
398 (void)file_printf(ms
, ", bad note name size 0x%lx",
399 (unsigned long)namesz
);
403 if (descsz
& 0x80000000) {
404 (void)file_printf(ms
, ", bad note description size 0x%lx",
405 (unsigned long)descsz
);
411 doff
= ELF_ALIGN(offset
+ namesz
);
413 if (offset
+ namesz
> size
) {
415 * We're past the end of the buffer.
420 offset
= ELF_ALIGN(doff
+ descsz
);
421 if (doff
+ descsz
> size
) {
423 * We're past the end of the buffer.
425 return (offset
>= size
) ? offset
: size
;
428 if (*flags
& FLAGS_DID_NOTE
)
431 if (namesz
== 4 && strcmp((char *)&nbuf
[noff
], "GNU") == 0 &&
432 xnh_type
== NT_GNU_VERSION
&& descsz
== 16) {
434 (void)memcpy(desc
, &nbuf
[doff
], sizeof(desc
));
436 if (file_printf(ms
, ", for GNU/") == -1)
438 switch (elf_getu32(swap
, desc
[0])) {
440 if (file_printf(ms
, "Linux") == -1)
444 if (file_printf(ms
, "Hurd") == -1)
448 if (file_printf(ms
, "Solaris") == -1)
451 case GNU_OS_KFREEBSD
:
452 if (file_printf(ms
, "kFreeBSD") == -1)
456 if (file_printf(ms
, "kNetBSD") == -1)
460 if (file_printf(ms
, "<unknown>") == -1)
463 if (file_printf(ms
, " %d.%d.%d", elf_getu32(swap
, desc
[1]),
464 elf_getu32(swap
, desc
[2]), elf_getu32(swap
, desc
[3])) == -1)
466 *flags
|= FLAGS_DID_NOTE
;
470 if (namesz
== 7 && strcmp((char *)&nbuf
[noff
], "NetBSD") == 0 &&
471 xnh_type
== NT_NETBSD_VERSION
&& descsz
== 4) {
473 (void)memcpy(&desc
, &nbuf
[doff
], sizeof(desc
));
474 desc
= elf_getu32(swap
, desc
);
476 if (file_printf(ms
, ", for NetBSD") == -1)
479 * The version number used to be stuck as 199905, and was thus
480 * basically content-free. Newer versions of NetBSD have fixed
481 * this and now use the encoding of __NetBSD_Version__:
487 * r = release ["",A-Z,Z[A-Z] but numeric]
490 if (desc
> 100000000U) {
491 uint32_t ver_patch
= (desc
/ 100) % 100;
492 uint32_t ver_rel
= (desc
/ 10000) % 100;
493 uint32_t ver_min
= (desc
/ 1000000) % 100;
494 uint32_t ver_maj
= desc
/ 100000000;
496 if (file_printf(ms
, " %u.%u", ver_maj
, ver_min
) == -1)
498 if (ver_rel
== 0 && ver_patch
!= 0) {
499 if (file_printf(ms
, ".%u", ver_patch
) == -1)
501 } else if (ver_rel
!= 0) {
502 while (ver_rel
> 26) {
503 if (file_printf(ms
, "Z") == -1)
507 if (file_printf(ms
, "%c", 'A' + ver_rel
- 1)
512 *flags
|= FLAGS_DID_NOTE
;
516 if (namesz
== 8 && strcmp((char *)&nbuf
[noff
], "FreeBSD") == 0 &&
517 xnh_type
== NT_FREEBSD_VERSION
&& descsz
== 4) {
519 (void)memcpy(&desc
, &nbuf
[doff
], sizeof(desc
));
520 desc
= elf_getu32(swap
, desc
);
521 if (file_printf(ms
, ", for FreeBSD") == -1)
525 * Contents is __FreeBSD_version, whose relation to OS
526 * versions is defined by a huge table in the Porter's
527 * Handbook. This is the general scheme:
530 * Mmp000 (before 4.10)
531 * Mmi0p0 (before 5.0)
534 * Development branches:
535 * Mmpxxx (before 4.6)
536 * Mmp1xx (before 4.10)
537 * Mmi1xx (before 5.0)
543 * i = minor version increment (491000 -> 4.10)
547 * The first release of FreeBSD to use ELF by default
550 if (desc
== 460002) {
551 if (file_printf(ms
, " 4.6.2") == -1)
553 } else if (desc
< 460100) {
554 if (file_printf(ms
, " %d.%d", desc
/ 100000,
555 desc
/ 10000 % 10) == -1)
557 if (desc
/ 1000 % 10 > 0)
558 if (file_printf(ms
, ".%d", desc
/ 1000 % 10)
561 if ((desc
% 1000 > 0) || (desc
% 100000 == 0))
562 if (file_printf(ms
, " (%d)", desc
) == -1)
564 } else if (desc
< 500000) {
565 if (file_printf(ms
, " %d.%d", desc
/ 100000,
566 desc
/ 10000 % 10 + desc
/ 1000 % 10) == -1)
568 if (desc
/ 100 % 10 > 0) {
569 if (file_printf(ms
, " (%d)", desc
) == -1)
571 } else if (desc
/ 10 % 10 > 0) {
572 if (file_printf(ms
, ".%d", desc
/ 10 % 10)
577 if (file_printf(ms
, " %d.%d", desc
/ 100000,
578 desc
/ 1000 % 100) == -1)
580 if ((desc
/ 100 % 10 > 0) ||
581 (desc
% 100000 / 100 == 0)) {
582 if (file_printf(ms
, " (%d)", desc
) == -1)
584 } else if (desc
/ 10 % 10 > 0) {
585 if (file_printf(ms
, ".%d", desc
/ 10 % 10)
590 *flags
|= FLAGS_DID_NOTE
;
594 if (namesz
== 8 && strcmp((char *)&nbuf
[noff
], "OpenBSD") == 0 &&
595 xnh_type
== NT_OPENBSD_VERSION
&& descsz
== 4) {
596 if (file_printf(ms
, ", for OpenBSD") == -1)
598 /* Content of note is always 0 */
599 *flags
|= FLAGS_DID_NOTE
;
603 if (namesz
== 10 && strcmp((char *)&nbuf
[noff
], "DragonFly") == 0 &&
604 xnh_type
== NT_DRAGONFLY_VERSION
&& descsz
== 4) {
606 if (file_printf(ms
, ", for DragonFly") == -1)
608 (void)memcpy(&desc
, &nbuf
[doff
], sizeof(desc
));
609 desc
= elf_getu32(swap
, desc
);
610 if (file_printf(ms
, " %d.%d.%d", desc
/ 100000,
611 desc
/ 10000 % 10, desc
% 10000) == -1)
613 *flags
|= FLAGS_DID_NOTE
;
619 * Sigh. The 2.0.36 kernel in Debian 2.1, at
620 * least, doesn't correctly implement name
621 * sections, in core dumps, as specified by
622 * the "Program Linking" section of "UNIX(R) System
623 * V Release 4 Programmer's Guide: ANSI C and
624 * Programming Support Tools", because my copy
625 * clearly says "The first 'namesz' bytes in 'name'
626 * contain a *null-terminated* [emphasis mine]
627 * character representation of the entry's owner
628 * or originator", but the 2.0.36 kernel code
629 * doesn't include the terminating null in the
632 if ((namesz
== 4 && strncmp((char *)&nbuf
[noff
], "CORE", 4) == 0) ||
633 (namesz
== 5 && strcmp((char *)&nbuf
[noff
], "CORE") == 0)) {
634 os_style
= OS_STYLE_SVR4
;
637 if ((namesz
== 8 && strcmp((char *)&nbuf
[noff
], "FreeBSD") == 0)) {
638 os_style
= OS_STYLE_FREEBSD
;
641 if ((namesz
>= 11 && strncmp((char *)&nbuf
[noff
], "NetBSD-CORE", 11)
643 os_style
= OS_STYLE_NETBSD
;
647 if ((*flags
& FLAGS_DID_CORE
) != 0)
650 if (os_style
!= -1 && (*flags
& FLAGS_DID_CORE_STYLE
) == 0) {
651 if (file_printf(ms
, ", %s-style", os_style_names
[os_style
])
654 *flags
|= FLAGS_DID_CORE_STYLE
;
658 case OS_STYLE_NETBSD
:
659 if (xnh_type
== NT_NETBSD_CORE_PROCINFO
) {
662 * Extract the program name. It is at
663 * offset 0x7c, and is up to 32-bytes,
664 * including the terminating NUL.
666 if (file_printf(ms
, ", from '%.31s'",
667 &nbuf
[doff
+ 0x7c]) == -1)
671 * Extract the signal number. It is at
674 (void)memcpy(&signo
, &nbuf
[doff
+ 0x08],
676 if (file_printf(ms
, " (signal %u)",
677 elf_getu32(swap
, signo
)) == -1)
679 *flags
|= FLAGS_DID_CORE
;
685 if (xnh_type
== NT_PRPSINFO
) {
689 * Extract the program name. We assume
690 * it to be 16 characters (that's what it
691 * is in SunOS 5.x and Linux).
693 * Unfortunately, it's at a different offset
694 * in various OSes, so try multiple offsets.
695 * If the characters aren't all printable,
698 for (i
= 0; i
< NOFFSETS
; i
++) {
699 unsigned char *cname
, *cp
;
700 size_t reloffset
= prpsoffsets(i
);
701 size_t noffset
= doff
+ reloffset
;
702 for (j
= 0; j
< 16; j
++, noffset
++,
705 * Make sure we're not past
706 * the end of the buffer; if
707 * we are, just give up.
713 * Make sure we're not past
714 * the end of the contents;
715 * if we are, this obviously
716 * isn't the right offset.
718 if (reloffset
>= descsz
)
740 if (!isprint(c
) || isquote(c
))
747 cname
= (unsigned char *)
748 &nbuf
[doff
+ prpsoffsets(i
)];
749 for (cp
= cname
; *cp
&& isprint(*cp
); cp
++)
752 * Linux apparently appends a space at the end
753 * of the command line: remove it.
755 while (cp
> cname
&& isspace(cp
[-1]))
757 if (file_printf(ms
, ", from '%.*s'",
758 (int)(cp
- cname
), cname
) == -1)
760 *flags
|= FLAGS_DID_CORE
;
773 /* SunOS 5.x hardware capability descriptions */
774 typedef struct cap_desc
{
779 static const cap_desc_t cap_desc_sparc
[] = {
780 { AV_SPARC_MUL32
, "MUL32" },
781 { AV_SPARC_DIV32
, "DIV32" },
782 { AV_SPARC_FSMULD
, "FSMULD" },
783 { AV_SPARC_V8PLUS
, "V8PLUS" },
784 { AV_SPARC_POPC
, "POPC" },
785 { AV_SPARC_VIS
, "VIS" },
786 { AV_SPARC_VIS2
, "VIS2" },
787 { AV_SPARC_ASI_BLK_INIT
, "ASI_BLK_INIT" },
788 { AV_SPARC_FMAF
, "FMAF" },
789 { AV_SPARC_FJFMAU
, "FJFMAU" },
790 { AV_SPARC_IMA
, "IMA" },
794 static const cap_desc_t cap_desc_386
[] = {
795 { AV_386_FPU
, "FPU" },
796 { AV_386_TSC
, "TSC" },
797 { AV_386_CX8
, "CX8" },
798 { AV_386_SEP
, "SEP" },
799 { AV_386_AMD_SYSC
, "AMD_SYSC" },
800 { AV_386_CMOV
, "CMOV" },
801 { AV_386_MMX
, "MMX" },
802 { AV_386_AMD_MMX
, "AMD_MMX" },
803 { AV_386_AMD_3DNow
, "AMD_3DNow" },
804 { AV_386_AMD_3DNowx
, "AMD_3DNowx" },
805 { AV_386_FXSR
, "FXSR" },
806 { AV_386_SSE
, "SSE" },
807 { AV_386_SSE2
, "SSE2" },
808 { AV_386_PAUSE
, "PAUSE" },
809 { AV_386_SSE3
, "SSE3" },
810 { AV_386_MON
, "MON" },
811 { AV_386_CX16
, "CX16" },
812 { AV_386_AHF
, "AHF" },
813 { AV_386_TSCP
, "TSCP" },
814 { AV_386_AMD_SSE4A
, "AMD_SSE4A" },
815 { AV_386_POPCNT
, "POPCNT" },
816 { AV_386_AMD_LZCNT
, "AMD_LZCNT" },
817 { AV_386_SSSE3
, "SSSE3" },
818 { AV_386_SSE4_1
, "SSE4.1" },
819 { AV_386_SSE4_2
, "SSE4.2" },
824 doshn(struct magic_set
*ms
, int clazz
, int swap
, int fd
, off_t off
, int num
,
825 size_t size
, int *flags
, int mach
)
832 uint64_t cap_hw1
= 0; /* SunOS 5.x hardware capabilites */
833 uint64_t cap_sf1
= 0; /* SunOS 5.x software capabilites */
835 if (size
!= xsh_sizeof
) {
836 if (file_printf(ms
, ", corrupted section header size") == -1)
841 if (lseek(fd
, off
, SEEK_SET
) == (off_t
)-1) {
846 for ( ; num
; num
--) {
847 if (read(fd
, xsh_addr
, xsh_sizeof
) == -1) {
859 if ((off
= lseek(fd
, (off_t
)0, SEEK_CUR
)) ==
864 if ((nbuf
= malloc((size_t)xsh_size
)) == NULL
) {
865 file_error(ms
, errno
, "Cannot allocate memory"
869 if ((noff
= lseek(fd
, (off_t
)xsh_offset
, SEEK_SET
)) ==
875 if (read(fd
, nbuf
, (size_t)xsh_size
) !=
884 if (noff
>= (off_t
)xsh_size
)
886 noff
= donote(ms
, nbuf
, (size_t)noff
,
887 (size_t)xsh_size
, clazz
, swap
, 4,
892 if ((lseek(fd
, off
, SEEK_SET
)) == (off_t
)-1) {
902 if ((off
= lseek(fd
, (off_t
)0, SEEK_CUR
)) ==
907 if (lseek(fd
, (off_t
)xsh_offset
, SEEK_SET
) ==
916 char cbuf
[/*CONSTCOND*/
917 MAX(sizeof cap32
, sizeof cap64
)];
918 if ((coff
+= xcap_sizeof
) >= (off_t
)xsh_size
)
920 if (read(fd
, cbuf
, (size_t)xcap_sizeof
) !=
921 (ssize_t
)xcap_sizeof
) {
925 (void)memcpy(xcap_addr
, cbuf
, xcap_sizeof
);
937 ", with unknown capability "
939 (unsigned long long)xcap_tag
,
940 (unsigned long long)xcap_val
) == -1)
945 if (lseek(fd
, off
, SEEK_SET
) == (off_t
)-1) {
953 if (file_printf(ms
, ", %sstripped", stripped
? "" : "not ") == -1)
956 const cap_desc_t
*cdp
;
961 cdp
= cap_desc_sparc
;
972 if (file_printf(ms
, ", uses") == -1)
975 while (cdp
->cd_name
) {
976 if (cap_hw1
& cdp
->cd_mask
) {
978 " %s", cdp
->cd_name
) == -1)
980 cap_hw1
&= ~cdp
->cd_mask
;
986 " unknown hardware capability 0x%llx",
987 (unsigned long long)cap_hw1
) == -1)
991 " hardware capability 0x%llx",
992 (unsigned long long)cap_hw1
) == -1)
997 if (cap_sf1
& SF1_SUNW_FPUSED
) {
999 (cap_sf1
& SF1_SUNW_FPKNWN
)
1000 ? ", uses frame pointer"
1001 : ", not known to use frame pointer") == -1)
1004 cap_sf1
&= ~SF1_SUNW_MASK
;
1007 ", with unknown software capability 0x%llx",
1008 (unsigned long long)cap_sf1
) == -1)
1015 * Look through the program headers of an executable image, searching
1016 * for a PT_INTERP section; if one is found, it's dynamically linked,
1017 * otherwise it's statically linked.
1020 dophn_exec(struct magic_set
*ms
, int clazz
, int swap
, int fd
, off_t off
,
1021 int num
, size_t size
, off_t fsize
, int *flags
, int sh_num
)
1025 const char *linking_style
= "statically";
1026 const char *shared_libraries
= "";
1027 unsigned char nbuf
[BUFSIZ
];
1029 size_t offset
, align
;
1030 off_t savedoffset
= (off_t
)-1;
1033 if (fstat(fd
, &st
) < 0) {
1038 if (size
!= xph_sizeof
) {
1039 if (file_printf(ms
, ", corrupted program header size") == -1)
1044 if (lseek(fd
, off
, SEEK_SET
) == (off_t
)-1) {
1049 for ( ; num
; num
--) {
1050 if (read(fd
, xph_addr
, xph_sizeof
) == -1) {
1054 if (xph_offset
> st
.st_size
&& savedoffset
!= (off_t
)-1) {
1055 if (lseek(fd
, savedoffset
, SEEK_SET
) == (off_t
)-1) {
1062 if ((savedoffset
= lseek(fd
, (off_t
)0, SEEK_CUR
)) == (off_t
)-1) {
1067 if (xph_offset
> fsize
) {
1068 if (lseek(fd
, savedoffset
, SEEK_SET
) == (off_t
)-1) {
1077 linking_style
= "dynamically";
1080 shared_libraries
= " (uses shared libs)";
1083 if ((align
= xph_align
) & 0x80000000UL
) {
1085 ", invalid note alignment 0x%lx",
1086 (unsigned long)align
) == -1)
1093 * This is a PT_NOTE section; loop through all the notes
1096 if (lseek(fd
, xph_offset
, SEEK_SET
)
1101 bufsize
= read(fd
, nbuf
, ((xph_filesz
< sizeof(nbuf
)) ?
1102 xph_filesz
: sizeof(nbuf
)));
1103 if (bufsize
== -1) {
1109 if (offset
>= (size_t)bufsize
)
1111 offset
= donote(ms
, nbuf
, offset
,
1112 (size_t)bufsize
, clazz
, swap
, align
,
1117 if (lseek(fd
, savedoffset
, SEEK_SET
) == (off_t
)-1) {
1126 if (file_printf(ms
, ", %s linked%s", linking_style
, shared_libraries
)
1134 file_tryelf(struct magic_set
*ms
, int fd
, const unsigned char *buf
,
1139 char c
[sizeof (int32_t)];
1146 Elf32_Ehdr elf32hdr
;
1147 Elf64_Ehdr elf64hdr
;
1150 if (ms
->flags
& (MAGIC_MIME
|MAGIC_APPLE
))
1153 * ELF executables have multiple section headers in arbitrary
1154 * file locations and thus file(1) cannot determine it from easily.
1155 * Instead we traverse thru all section headers until a symbol table
1156 * one is found or else the binary is stripped.
1157 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
1159 if (buf
[EI_MAG0
] != ELFMAG0
1160 || (buf
[EI_MAG1
] != ELFMAG1
&& buf
[EI_MAG1
] != OLFMAG1
)
1161 || buf
[EI_MAG2
] != ELFMAG2
|| buf
[EI_MAG3
] != ELFMAG3
)
1165 * If we cannot seek, it must be a pipe, socket or fifo.
1167 if((lseek(fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1) && (errno
== ESPIPE
))
1168 fd
= file_pipe2file(ms
, fd
, buf
, nbytes
);
1170 if (fstat(fd
, &st
) == -1) {
1176 clazz
= buf
[EI_CLASS
];
1181 #define elf_getu(a, b) elf_getu32(a, b)
1183 #define elfhdr elf32hdr
1184 #include "elfclass.h"
1187 #define elf_getu(a, b) elf_getu64(a, b)
1189 #define elfhdr elf64hdr
1190 #include "elfclass.h"
1192 if (file_printf(ms
, ", unknown class %d", clazz
) == -1)