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.90 2011/08/23 08:01:12 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,
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 #ifdef USE_ARRAY_FOR_64BIT_TYPES
131 # define elf_getu64(swap, array) \
132 ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
133 (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
135 # define elf_getu64(swap, value) getu64(swap, value)
138 #define xsh_addr (clazz == ELFCLASS32 \
141 #define xsh_sizeof (clazz == ELFCLASS32 \
144 #define xsh_size (clazz == ELFCLASS32 \
145 ? elf_getu32(swap, sh32.sh_size) \
146 : elf_getu64(swap, sh64.sh_size))
147 #define xsh_offset (off_t)(clazz == ELFCLASS32 \
148 ? elf_getu32(swap, sh32.sh_offset) \
149 : elf_getu64(swap, sh64.sh_offset))
150 #define xsh_type (clazz == ELFCLASS32 \
151 ? elf_getu32(swap, sh32.sh_type) \
152 : elf_getu32(swap, sh64.sh_type))
153 #define xph_addr (clazz == ELFCLASS32 \
156 #define xph_sizeof (clazz == ELFCLASS32 \
159 #define xph_type (clazz == ELFCLASS32 \
160 ? elf_getu32(swap, ph32.p_type) \
161 : elf_getu32(swap, ph64.p_type))
162 #define xph_offset (off_t)(clazz == ELFCLASS32 \
163 ? elf_getu32(swap, ph32.p_offset) \
164 : elf_getu64(swap, ph64.p_offset))
165 #define xph_align (size_t)((clazz == ELFCLASS32 \
166 ? (off_t) (ph32.p_align ? \
167 elf_getu32(swap, ph32.p_align) : 4) \
168 : (off_t) (ph64.p_align ? \
169 elf_getu64(swap, ph64.p_align) : 4)))
170 #define xph_filesz (size_t)((clazz == ELFCLASS32 \
171 ? elf_getu32(swap, ph32.p_filesz) \
172 : elf_getu64(swap, ph64.p_filesz)))
173 #define xnh_addr (clazz == ELFCLASS32 \
176 #define xph_memsz (size_t)((clazz == ELFCLASS32 \
177 ? elf_getu32(swap, ph32.p_memsz) \
178 : elf_getu64(swap, ph64.p_memsz)))
179 #define xnh_sizeof (clazz == ELFCLASS32 \
182 #define xnh_type (clazz == ELFCLASS32 \
183 ? elf_getu32(swap, nh32.n_type) \
184 : elf_getu32(swap, nh64.n_type))
185 #define xnh_namesz (clazz == ELFCLASS32 \
186 ? elf_getu32(swap, nh32.n_namesz) \
187 : elf_getu32(swap, nh64.n_namesz))
188 #define xnh_descsz (clazz == ELFCLASS32 \
189 ? elf_getu32(swap, nh32.n_descsz) \
190 : elf_getu32(swap, nh64.n_descsz))
191 #define prpsoffsets(i) (clazz == ELFCLASS32 \
194 #define xcap_addr (clazz == ELFCLASS32 \
197 #define xcap_sizeof (clazz == ELFCLASS32 \
200 #define xcap_tag (clazz == ELFCLASS32 \
201 ? elf_getu32(swap, cap32.c_tag) \
202 : elf_getu64(swap, cap64.c_tag))
203 #define xcap_val (clazz == ELFCLASS32 \
204 ? elf_getu32(swap, cap32.c_un.c_val) \
205 : elf_getu64(swap, cap64.c_un.c_val))
209 * Try larger offsets first to avoid false matches
210 * from earlier data that happen to look like strings.
212 static const size_t prpsoffsets32[] = {
214 104, /* SunOS 5.x (command line) */
215 88, /* SunOS 5.x (short name) */
216 #endif /* USE_NT_PSINFO */
218 100, /* SunOS 5.x (command line) */
219 84, /* SunOS 5.x (short name) */
221 44, /* Linux (command line) */
222 28, /* Linux 2.0.36 (short name) */
227 static const size_t prpsoffsets64[] = {
229 152, /* SunOS 5.x (command line) */
230 136, /* SunOS 5.x (short name) */
231 #endif /* USE_NT_PSINFO */
233 136, /* SunOS 5.x, 64-bit (command line) */
234 120, /* SunOS 5.x, 64-bit (short name) */
236 56, /* Linux (command line) */
237 40, /* Linux (tested on core from 2.4.x, short name) */
239 16, /* FreeBSD, 64-bit */
242 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
243 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
245 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
248 * Look through the program headers of an executable image, searching
249 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
250 * "FreeBSD"; if one is found, try looking in various places in its
251 * contents for a 16-character string containing only printable
252 * characters - if found, that string should be the name of the program
253 * that dropped core. Note: right after that 16-character string is,
254 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
255 * Linux, a longer string (80 characters, in 5.x, probably other
256 * SVR4-flavored systems, and Linux) containing the start of the
257 * command line for that program.
259 * SunOS 5.x core files contain two PT_NOTE sections, with the types
260 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
261 * same info about the command name and command line, so it probably
262 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
263 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
264 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
265 * the SunOS 5.x file command relies on this (and prefers the latter).
267 * The signal number probably appears in a section of type NT_PRSTATUS,
268 * but that's also rather OS-dependent, in ways that are harder to
269 * dissect with heuristics, so I'm not bothering with the signal number.
270 * (I suppose the signal number could be of interest in situations where
271 * you don't have the binary of the program that dropped core; if you
272 * *do* have that binary, the debugger will probably tell you what
276 #define OS_STYLE_SVR4 0
277 #define OS_STYLE_FREEBSD 1
278 #define OS_STYLE_NETBSD 2
280 private const char os_style_names[][8] = {
286 #define FLAGS_DID_CORE 0x01
287 #define FLAGS_DID_NOTE 0x02
288 #define FLAGS_DID_BUILD_ID 0x04
289 #define FLAGS_DID_CORE_STYLE 0x08
290 #define FLAGS_IS_CORE 0x10
293 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
294 int num, size_t size, off_t fsize, int *flags)
299 unsigned char nbuf[BUFSIZ];
302 if (size != xph_sizeof) {
303 if (file_printf(ms, ", corrupted program header size") == -1)
309 * Loop through all the program headers.
311 for ( ; num; num--) {
312 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
316 if (read(fd, xph_addr, xph_sizeof) == -1) {
322 if (xph_offset > fsize) {
323 /* Perhaps warn here */
327 if (xph_type != PT_NOTE)
331 * This is a PT_NOTE section; loop through all the notes
334 if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
338 bufsize = read(fd, nbuf,
339 ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
346 if (offset >= (size_t)bufsize)
348 offset = donote(ms, nbuf, offset, (size_t)bufsize,
349 clazz, swap, 4, flags);
360 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
361 int clazz, int swap, size_t align, int *flags)
369 uint32_t namesz, descsz;
370 unsigned char *nbuf = CAST(unsigned char *, vbuf);
372 (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
373 offset += xnh_sizeof;
377 if ((namesz == 0) && (descsz == 0)) {
379 * We're out of note headers.
381 return (offset >= size) ? offset : size;
384 if (namesz & 0x80000000) {
385 (void)file_printf(ms, ", bad note name size 0x%lx",
386 (unsigned long)namesz);
390 if (descsz & 0x80000000) {
391 (void)file_printf(ms, ", bad note description size 0x%lx",
392 (unsigned long)descsz);
398 doff = ELF_ALIGN(offset + namesz);
400 if (offset + namesz > size) {
402 * We're past the end of the buffer.
407 offset = ELF_ALIGN(doff + descsz);
408 if (doff + descsz > size) {
410 * We're past the end of the buffer.
412 return (offset >= size) ? offset : size;
415 if ((*flags & (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) ==
416 (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID))
419 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
420 xnh_type == NT_GNU_VERSION && descsz == 16) {
422 (void)memcpy(desc, &nbuf[doff], sizeof(desc));
424 if (file_printf(ms, ", for GNU/") == -1)
426 switch (elf_getu32(swap, desc[0])) {
428 if (file_printf(ms, "Linux") == -1)
432 if (file_printf(ms, "Hurd") == -1)
436 if (file_printf(ms, "Solaris") == -1)
439 case GNU_OS_KFREEBSD:
440 if (file_printf(ms, "kFreeBSD") == -1)
444 if (file_printf(ms, "kNetBSD") == -1)
448 if (file_printf(ms, "<unknown>") == -1)
451 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
452 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
454 *flags |= FLAGS_DID_NOTE;
458 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
459 xnh_type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) {
461 if (file_printf(ms, ", BuildID[%s]=0x", descsz == 16 ? "md5/uuid" :
464 (void)memcpy(desc, &nbuf[doff], descsz);
465 for (i = 0; i < descsz >> 2; i++)
466 if (file_printf(ms, "%.8x", desc[i]) == -1)
468 *flags |= FLAGS_DID_BUILD_ID;
471 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
472 xnh_type == NT_NETBSD_VERSION && descsz == 4) {
474 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
475 desc = elf_getu32(swap, desc);
477 if (file_printf(ms, ", for NetBSD") == -1)
480 * The version number used to be stuck as 199905, and was thus
481 * basically content-free. Newer versions of NetBSD have fixed
482 * this and now use the encoding of __NetBSD_Version__:
488 * r = release ["",A-Z,Z[A-Z] but numeric]
491 if (desc > 100000000U) {
492 uint32_t ver_patch = (desc / 100) % 100;
493 uint32_t ver_rel = (desc / 10000) % 100;
494 uint32_t ver_min = (desc / 1000000) % 100;
495 uint32_t ver_maj = desc / 100000000;
497 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
499 if (ver_rel == 0 && ver_patch != 0) {
500 if (file_printf(ms, ".%u", ver_patch) == -1)
502 } else if (ver_rel != 0) {
503 while (ver_rel > 26) {
504 if (file_printf(ms, "Z") == -1)
508 if (file_printf(ms, "%c", 'A' + ver_rel - 1)
513 *flags |= FLAGS_DID_NOTE;
517 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
518 xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
520 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
521 desc = elf_getu32(swap, desc);
522 if (file_printf(ms, ", for FreeBSD") == -1)
526 * Contents is __FreeBSD_version, whose relation to OS
527 * versions is defined by a huge table in the Porter's
528 * Handbook. This is the general scheme:
531 * Mmp000 (before 4.10)
532 * Mmi0p0 (before 5.0)
535 * Development branches:
536 * Mmpxxx (before 4.6)
537 * Mmp1xx (before 4.10)
538 * Mmi1xx (before 5.0)
544 * i = minor version increment (491000 -> 4.10)
548 * The first release of FreeBSD to use ELF by default
551 if (desc == 460002) {
552 if (file_printf(ms, " 4.6.2") == -1)
554 } else if (desc < 460100) {
555 if (file_printf(ms, " %d.%d", desc / 100000,
556 desc / 10000 % 10) == -1)
558 if (desc / 1000 % 10 > 0)
559 if (file_printf(ms, ".%d", desc / 1000 % 10)
562 if ((desc % 1000 > 0) || (desc % 100000 == 0))
563 if (file_printf(ms, " (%d)", desc) == -1)
565 } else if (desc < 500000) {
566 if (file_printf(ms, " %d.%d", desc / 100000,
567 desc / 10000 % 10 + desc / 1000 % 10) == -1)
569 if (desc / 100 % 10 > 0) {
570 if (file_printf(ms, " (%d)", desc) == -1)
572 } else if (desc / 10 % 10 > 0) {
573 if (file_printf(ms, ".%d", desc / 10 % 10)
578 if (file_printf(ms, " %d.%d", desc / 100000,
579 desc / 1000 % 100) == -1)
581 if ((desc / 100 % 10 > 0) ||
582 (desc % 100000 / 100 == 0)) {
583 if (file_printf(ms, " (%d)", desc) == -1)
585 } else if (desc / 10 % 10 > 0) {
586 if (file_printf(ms, ".%d", desc / 10 % 10)
591 *flags |= FLAGS_DID_NOTE;
595 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
596 xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
597 if (file_printf(ms, ", for OpenBSD") == -1)
599 /* Content of note is always 0 */
600 *flags |= FLAGS_DID_NOTE;
604 if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
605 xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
607 if (file_printf(ms, ", for DragonFly") == -1)
609 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
610 desc = elf_getu32(swap, desc);
611 if (file_printf(ms, " %d.%d.%d", desc / 100000,
612 desc / 10000 % 10, desc % 10000) == -1)
614 *flags |= FLAGS_DID_NOTE;
620 * Sigh. The 2.0.36 kernel in Debian 2.1, at
621 * least, doesn't correctly implement name
622 * sections, in core dumps, as specified by
623 * the "Program Linking" section of "UNIX(R) System
624 * V Release 4 Programmer's Guide: ANSI C and
625 * Programming Support Tools", because my copy
626 * clearly says "The first 'namesz' bytes in 'name'
627 * contain a *null-terminated* [emphasis mine]
628 * character representation of the entry's owner
629 * or originator", but the 2.0.36 kernel code
630 * doesn't include the terminating null in the
633 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
634 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
635 os_style = OS_STYLE_SVR4;
638 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
639 os_style = OS_STYLE_FREEBSD;
642 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
644 os_style = OS_STYLE_NETBSD;
648 if ((*flags & FLAGS_DID_CORE) != 0)
651 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
652 if (file_printf(ms, ", %s-style", os_style_names[os_style])
655 *flags |= FLAGS_DID_CORE_STYLE;
659 case OS_STYLE_NETBSD:
660 if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
663 * Extract the program name. It is at
664 * offset 0x7c, and is up to 32-bytes,
665 * including the terminating NUL.
667 if (file_printf(ms, ", from '%.31s'",
668 &nbuf[doff + 0x7c]) == -1)
672 * Extract the signal number. It is at
675 (void)memcpy(&signo, &nbuf[doff + 0x08],
677 if (file_printf(ms, " (signal %u)",
678 elf_getu32(swap, signo)) == -1)
680 *flags |= FLAGS_DID_CORE;
686 if (xnh_type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
690 * Extract the program name. We assume
691 * it to be 16 characters (that's what it
692 * is in SunOS 5.x and Linux).
694 * Unfortunately, it's at a different offset
695 * in various OSes, so try multiple offsets.
696 * If the characters aren't all printable,
699 for (i = 0; i < NOFFSETS; i++) {
700 unsigned char *cname, *cp;
701 size_t reloffset = prpsoffsets(i);
702 size_t noffset = doff + reloffset;
704 for (j = 0; j < 16; j++, noffset++,
707 * Make sure we're not past
708 * the end of the buffer; if
709 * we are, just give up.
715 * Make sure we're not past
716 * the end of the contents;
717 * if we are, this obviously
718 * isn't the right offset.
720 if (reloffset >= descsz)
742 if (!isprint(c) || isquote(c))
751 * Try next offsets, in case this match is
752 * in the middle of a string.
754 for (k = i + 1 ; k < NOFFSETS ; k++) {
757 if (prpsoffsets(k) >= prpsoffsets(i))
759 for (no = doff + prpsoffsets(k);
760 no < doff + prpsoffsets(i); no++)
762 && isprint(nbuf[no]);
767 cname = (unsigned char *)
768 &nbuf[doff + prpsoffsets(i)];
769 for (cp = cname; *cp && isprint(*cp); cp++)
772 * Linux apparently appends a space at the end
773 * of the command line: remove it.
775 while (cp > cname && isspace(cp[-1]))
777 if (file_printf(ms, ", from '%.*s'",
778 (int)(cp - cname), cname) == -1)
780 *flags |= FLAGS_DID_CORE;
793 /* SunOS 5.x hardware capability descriptions */
794 typedef struct cap_desc {
799 static const cap_desc_t cap_desc_sparc[] = {
800 { AV_SPARC_MUL32, "MUL32" },
801 { AV_SPARC_DIV32, "DIV32" },
802 { AV_SPARC_FSMULD, "FSMULD" },
803 { AV_SPARC_V8PLUS, "V8PLUS" },
804 { AV_SPARC_POPC, "POPC" },
805 { AV_SPARC_VIS, "VIS" },
806 { AV_SPARC_VIS2, "VIS2" },
807 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
808 { AV_SPARC_FMAF, "FMAF" },
809 { AV_SPARC_FJFMAU, "FJFMAU" },
810 { AV_SPARC_IMA, "IMA" },
814 static const cap_desc_t cap_desc_386[] = {
815 { AV_386_FPU, "FPU" },
816 { AV_386_TSC, "TSC" },
817 { AV_386_CX8, "CX8" },
818 { AV_386_SEP, "SEP" },
819 { AV_386_AMD_SYSC, "AMD_SYSC" },
820 { AV_386_CMOV, "CMOV" },
821 { AV_386_MMX, "MMX" },
822 { AV_386_AMD_MMX, "AMD_MMX" },
823 { AV_386_AMD_3DNow, "AMD_3DNow" },
824 { AV_386_AMD_3DNowx, "AMD_3DNowx" },
825 { AV_386_FXSR, "FXSR" },
826 { AV_386_SSE, "SSE" },
827 { AV_386_SSE2, "SSE2" },
828 { AV_386_PAUSE, "PAUSE" },
829 { AV_386_SSE3, "SSE3" },
830 { AV_386_MON, "MON" },
831 { AV_386_CX16, "CX16" },
832 { AV_386_AHF, "AHF" },
833 { AV_386_TSCP, "TSCP" },
834 { AV_386_AMD_SSE4A, "AMD_SSE4A" },
835 { AV_386_POPCNT, "POPCNT" },
836 { AV_386_AMD_LZCNT, "AMD_LZCNT" },
837 { AV_386_SSSE3, "SSSE3" },
838 { AV_386_SSE4_1, "SSE4.1" },
839 { AV_386_SSE4_2, "SSE4.2" },
844 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
845 size_t size, off_t fsize, int *flags, int mach)
852 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
853 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
855 if (size != xsh_sizeof) {
856 if (file_printf(ms, ", corrupted section header size") == -1)
861 for ( ; num; num--) {
862 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
866 if (read(fd, xsh_addr, xsh_sizeof) == -1) {
872 /* Things we can determine before we seek */
881 if (xsh_offset > fsize) {
882 /* Perhaps warn here */
888 /* Things we can determine when we seek */
891 if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
892 file_error(ms, errno, "Cannot allocate memory"
896 if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
902 if (read(fd, nbuf, (size_t)xsh_size) !=
911 if (noff >= (off_t)xsh_size)
913 noff = donote(ms, nbuf, (size_t)noff,
914 (size_t)xsh_size, clazz, swap, 4,
922 if (lseek(fd, (off_t)xsh_offset, SEEK_SET) ==
931 char cbuf[/*CONSTCOND*/
932 MAX(sizeof cap32, sizeof cap64)];
933 if ((coff += xcap_sizeof) > (off_t)xsh_size)
935 if (read(fd, cbuf, (size_t)xcap_sizeof) !=
936 (ssize_t)xcap_sizeof) {
940 (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
952 ", with unknown capability "
953 "0x%" INT64_T_FORMAT "x = 0x%"
955 (unsigned long long)xcap_tag,
956 (unsigned long long)xcap_val) == -1)
967 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
970 const cap_desc_t *cdp;
975 cdp = cap_desc_sparc;
986 if (file_printf(ms, ", uses") == -1)
989 while (cdp->cd_name) {
990 if (cap_hw1 & cdp->cd_mask) {
992 " %s", cdp->cd_name) == -1)
994 cap_hw1 &= ~cdp->cd_mask;
1000 " unknown hardware capability 0x%"
1002 (unsigned long long)cap_hw1) == -1)
1006 " hardware capability 0x%" INT64_T_FORMAT "x",
1007 (unsigned long long)cap_hw1) == -1)
1012 if (cap_sf1 & SF1_SUNW_FPUSED) {
1014 (cap_sf1 & SF1_SUNW_FPKNWN)
1015 ? ", uses frame pointer"
1016 : ", not known to use frame pointer") == -1)
1019 cap_sf1 &= ~SF1_SUNW_MASK;
1022 ", with unknown software capability 0x%"
1024 (unsigned long long)cap_sf1) == -1)
1031 * Look through the program headers of an executable image, searching
1032 * for a PT_INTERP section; if one is found, it's dynamically linked,
1033 * otherwise it's statically linked.
1036 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1037 int num, size_t size, off_t fsize, int *flags, int sh_num)
1041 const char *linking_style = "statically";
1042 const char *shared_libraries = "";
1043 unsigned char nbuf[BUFSIZ];
1045 size_t offset, align;
1047 if (size != xph_sizeof) {
1048 if (file_printf(ms, ", corrupted program header size") == -1)
1053 for ( ; num; num--) {
1054 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
1059 if (read(fd, xph_addr, xph_sizeof) == -1) {
1066 /* Things we can determine before we seek */
1069 linking_style = "dynamically";
1072 shared_libraries = " (uses shared libs)";
1075 if (xph_offset > fsize) {
1076 /* Maybe warn here? */
1082 /* Things we can determine when we seek */
1085 if ((align = xph_align) & 0x80000000UL) {
1087 ", invalid note alignment 0x%lx",
1088 (unsigned long)align) == -1)
1095 * This is a PT_NOTE section; loop through all the notes
1098 if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
1102 bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
1103 xph_filesz : sizeof(nbuf)));
1104 if (bufsize == -1) {
1110 if (offset >= (size_t)bufsize)
1112 offset = donote(ms, nbuf, offset,
1113 (size_t)bufsize, clazz, swap, align,
1123 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
1131 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
1136 char c[sizeof (int32_t)];
1143 Elf32_Ehdr elf32hdr;
1144 Elf64_Ehdr elf64hdr;
1147 if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
1150 * ELF executables have multiple section headers in arbitrary
1151 * file locations and thus file(1) cannot determine it from easily.
1152 * Instead we traverse thru all section headers until a symbol table
1153 * one is found or else the binary is stripped.
1154 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
1156 if (buf[EI_MAG0] != ELFMAG0
1157 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1158 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1162 * If we cannot seek, it must be a pipe, socket or fifo.
1164 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1165 fd = file_pipe2file(ms, fd, buf, nbytes);
1167 if (fstat(fd, &st) == -1) {
1173 clazz = buf[EI_CLASS];
1178 #define elf_getu(a, b) elf_getu32(a, b)
1180 #define elfhdr elf32hdr
1181 #include "elfclass.h"
1184 #define elf_getu(a, b) elf_getu64(a, b)
1186 #define elfhdr elf64hdr
1187 #include "elfclass.h"
1189 if (file_printf(ms, ", unknown class %d", clazz) == -1)