1 /* $NetBSD: readelf.c,v 1.11 2015/01/02 21:15:32 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.117 2014/12/16 23:29:42 christos Exp $")
35 __RCSID("$NetBSD: readelf.c,v 1.11 2015/01/02 21:15:32 christos Exp $");
51 private int dophn_core(struct magic_set
*, int, int, int, off_t
, int, size_t,
52 off_t
, int *, uint16_t *);
54 private int dophn_exec(struct magic_set
*, int, int, int, off_t
, int, size_t,
55 off_t
, int, int *, uint16_t *);
56 private int doshn(struct magic_set
*, int, int, int, off_t
, int, size_t,
57 off_t
, int, int, int *, uint16_t *);
58 private size_t donote(struct magic_set
*, void *, size_t, size_t, int,
59 int, size_t, int *, uint16_t *);
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 #define MAX_SHNUM 32768
71 #define SIZE_UNKNOWN ((off_t)-1)
74 toomany(struct magic_set
*ms
, const char *name
, uint16_t num
)
76 if (file_printf(ms
, ", too many %s (%u)", name
, num
83 getu16(int swap
, uint16_t value
)
93 retval
.c
[0] = tmpval
.c
[1];
94 retval
.c
[1] = tmpval
.c
[0];
102 getu32(int swap
, uint32_t value
)
112 retval
.c
[0] = tmpval
.c
[3];
113 retval
.c
[1] = tmpval
.c
[2];
114 retval
.c
[2] = tmpval
.c
[1];
115 retval
.c
[3] = tmpval
.c
[0];
123 getu64(int swap
, uint64_t value
)
133 retval
.c
[0] = tmpval
.c
[7];
134 retval
.c
[1] = tmpval
.c
[6];
135 retval
.c
[2] = tmpval
.c
[5];
136 retval
.c
[3] = tmpval
.c
[4];
137 retval
.c
[4] = tmpval
.c
[3];
138 retval
.c
[5] = tmpval
.c
[2];
139 retval
.c
[6] = tmpval
.c
[1];
140 retval
.c
[7] = tmpval
.c
[0];
147 #define elf_getu16(swap, value) getu16(swap, value)
148 #define elf_getu32(swap, value) getu32(swap, value)
149 #define elf_getu64(swap, value) getu64(swap, value)
151 #define xsh_addr (clazz == ELFCLASS32 \
154 #define xsh_sizeof (clazz == ELFCLASS32 \
157 #define xsh_size (size_t)(clazz == ELFCLASS32 \
158 ? elf_getu32(swap, sh32.sh_size) \
159 : elf_getu64(swap, sh64.sh_size))
160 #define xsh_offset (off_t)(clazz == ELFCLASS32 \
161 ? elf_getu32(swap, sh32.sh_offset) \
162 : elf_getu64(swap, sh64.sh_offset))
163 #define xsh_type (clazz == ELFCLASS32 \
164 ? elf_getu32(swap, sh32.sh_type) \
165 : elf_getu32(swap, sh64.sh_type))
166 #define xsh_name (clazz == ELFCLASS32 \
167 ? elf_getu32(swap, sh32.sh_name) \
168 : elf_getu32(swap, sh64.sh_name))
169 #define xph_addr (clazz == ELFCLASS32 \
172 #define xph_sizeof (clazz == ELFCLASS32 \
175 #define xph_type (clazz == ELFCLASS32 \
176 ? elf_getu32(swap, ph32.p_type) \
177 : elf_getu32(swap, ph64.p_type))
178 #define xph_offset (off_t)(clazz == ELFCLASS32 \
179 ? elf_getu32(swap, ph32.p_offset) \
180 : elf_getu64(swap, ph64.p_offset))
181 #define xph_align (size_t)((clazz == ELFCLASS32 \
182 ? (off_t) (ph32.p_align ? \
183 elf_getu32(swap, ph32.p_align) : 4) \
184 : (off_t) (ph64.p_align ? \
185 elf_getu64(swap, ph64.p_align) : 4)))
186 #define xph_filesz (size_t)((clazz == ELFCLASS32 \
187 ? elf_getu32(swap, ph32.p_filesz) \
188 : elf_getu64(swap, ph64.p_filesz)))
189 #define xnh_addr (clazz == ELFCLASS32 \
192 #define xph_memsz (size_t)((clazz == ELFCLASS32 \
193 ? elf_getu32(swap, ph32.p_memsz) \
194 : elf_getu64(swap, ph64.p_memsz)))
195 #define xnh_sizeof (clazz == ELFCLASS32 \
198 #define xnh_type (clazz == ELFCLASS32 \
199 ? elf_getu32(swap, nh32.n_type) \
200 : elf_getu32(swap, nh64.n_type))
201 #define xnh_namesz (clazz == ELFCLASS32 \
202 ? elf_getu32(swap, nh32.n_namesz) \
203 : elf_getu32(swap, nh64.n_namesz))
204 #define xnh_descsz (clazz == ELFCLASS32 \
205 ? elf_getu32(swap, nh32.n_descsz) \
206 : elf_getu32(swap, nh64.n_descsz))
207 #define prpsoffsets(i) (clazz == ELFCLASS32 \
210 #define xcap_addr (clazz == ELFCLASS32 \
213 #define xcap_sizeof (clazz == ELFCLASS32 \
216 #define xcap_tag (clazz == ELFCLASS32 \
217 ? elf_getu32(swap, cap32.c_tag) \
218 : elf_getu64(swap, cap64.c_tag))
219 #define xcap_val (clazz == ELFCLASS32 \
220 ? elf_getu32(swap, cap32.c_un.c_val) \
221 : elf_getu64(swap, cap64.c_un.c_val))
225 * Try larger offsets first to avoid false matches
226 * from earlier data that happen to look like strings.
228 static const size_t prpsoffsets32
[] = {
230 104, /* SunOS 5.x (command line) */
231 88, /* SunOS 5.x (short name) */
232 #endif /* USE_NT_PSINFO */
234 100, /* SunOS 5.x (command line) */
235 84, /* SunOS 5.x (short name) */
237 44, /* Linux (command line) */
238 28, /* Linux 2.0.36 (short name) */
243 static const size_t prpsoffsets64
[] = {
245 152, /* SunOS 5.x (command line) */
246 136, /* SunOS 5.x (short name) */
247 #endif /* USE_NT_PSINFO */
249 136, /* SunOS 5.x, 64-bit (command line) */
250 120, /* SunOS 5.x, 64-bit (short name) */
252 56, /* Linux (command line) */
253 40, /* Linux (tested on core from 2.4.x, short name) */
255 16, /* FreeBSD, 64-bit */
258 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
259 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
261 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
264 * Look through the program headers of an executable image, searching
265 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
266 * "FreeBSD"; if one is found, try looking in various places in its
267 * contents for a 16-character string containing only printable
268 * characters - if found, that string should be the name of the program
269 * that dropped core. Note: right after that 16-character string is,
270 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
271 * Linux, a longer string (80 characters, in 5.x, probably other
272 * SVR4-flavored systems, and Linux) containing the start of the
273 * command line for that program.
275 * SunOS 5.x core files contain two PT_NOTE sections, with the types
276 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
277 * same info about the command name and command line, so it probably
278 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
279 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
280 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
281 * the SunOS 5.x file command relies on this (and prefers the latter).
283 * The signal number probably appears in a section of type NT_PRSTATUS,
284 * but that's also rather OS-dependent, in ways that are harder to
285 * dissect with heuristics, so I'm not bothering with the signal number.
286 * (I suppose the signal number could be of interest in situations where
287 * you don't have the binary of the program that dropped core; if you
288 * *do* have that binary, the debugger will probably tell you what
292 #define OS_STYLE_SVR4 0
293 #define OS_STYLE_FREEBSD 1
294 #define OS_STYLE_NETBSD 2
296 private const char os_style_names
[][8] = {
302 #define FLAGS_DID_CORE 0x001
303 #define FLAGS_DID_OS_NOTE 0x002
304 #define FLAGS_DID_BUILD_ID 0x004
305 #define FLAGS_DID_CORE_STYLE 0x008
306 #define FLAGS_DID_NETBSD_PAX 0x010
307 #define FLAGS_DID_NETBSD_MARCH 0x020
308 #define FLAGS_DID_NETBSD_CMODEL 0x040
309 #define FLAGS_DID_NETBSD_UNKNOWN 0x080
310 #define FLAGS_IS_CORE 0x100
313 dophn_core(struct magic_set
*ms
, int clazz
, int swap
, int fd
, off_t off
,
314 int num
, size_t size
, off_t fsize
, int *flags
, uint16_t *notecount
)
319 unsigned char nbuf
[BUFSIZ
];
322 if (size
!= xph_sizeof
) {
323 if (file_printf(ms
, ", corrupted program header size") == -1)
329 * Loop through all the program headers.
331 for ( ; num
; num
--) {
332 if (pread(fd
, xph_addr
, xph_sizeof
, off
) < (ssize_t
)xph_sizeof
) {
338 if (fsize
!= SIZE_UNKNOWN
&& xph_offset
> fsize
) {
339 /* Perhaps warn here */
343 if (xph_type
!= PT_NOTE
)
347 * This is a PT_NOTE section; loop through all the notes
350 len
= xph_filesz
< sizeof(nbuf
) ? xph_filesz
: sizeof(nbuf
);
351 if ((bufsize
= pread(fd
, nbuf
, len
, xph_offset
)) == -1) {
357 if (offset
>= (size_t)bufsize
)
359 offset
= donote(ms
, nbuf
, offset
, (size_t)bufsize
,
360 clazz
, swap
, 4, flags
, notecount
);
371 do_note_netbsd_version(struct magic_set
*ms
, int swap
, void *v
)
374 (void)memcpy(&desc
, v
, sizeof(desc
));
375 desc
= elf_getu32(swap
, desc
);
377 if (file_printf(ms
, ", for NetBSD") == -1)
380 * The version number used to be stuck as 199905, and was thus
381 * basically content-free. Newer versions of NetBSD have fixed
382 * this and now use the encoding of __NetBSD_Version__:
388 * r = release ["",A-Z,Z[A-Z] but numeric]
391 if (desc
> 100000000U) {
392 uint32_t ver_patch
= (desc
/ 100) % 100;
393 uint32_t ver_rel
= (desc
/ 10000) % 100;
394 uint32_t ver_min
= (desc
/ 1000000) % 100;
395 uint32_t ver_maj
= desc
/ 100000000;
397 if (file_printf(ms
, " %u.%u", ver_maj
, ver_min
) == -1)
399 if (ver_rel
== 0 && ver_patch
!= 0) {
400 if (file_printf(ms
, ".%u", ver_patch
) == -1)
402 } else if (ver_rel
!= 0) {
403 while (ver_rel
> 26) {
404 if (file_printf(ms
, "Z") == -1)
408 if (file_printf(ms
, "%c", 'A' + ver_rel
- 1)
416 do_note_freebsd_version(struct magic_set
*ms
, int swap
, void *v
)
420 (void)memcpy(&desc
, v
, sizeof(desc
));
421 desc
= elf_getu32(swap
, desc
);
422 if (file_printf(ms
, ", for FreeBSD") == -1)
426 * Contents is __FreeBSD_version, whose relation to OS
427 * versions is defined by a huge table in the Porter's
428 * Handbook. This is the general scheme:
431 * Mmp000 (before 4.10)
432 * Mmi0p0 (before 5.0)
435 * Development branches:
436 * Mmpxxx (before 4.6)
437 * Mmp1xx (before 4.10)
438 * Mmi1xx (before 5.0)
444 * i = minor version increment (491000 -> 4.10)
448 * The first release of FreeBSD to use ELF by default
451 if (desc
== 460002) {
452 if (file_printf(ms
, " 4.6.2") == -1)
454 } else if (desc
< 460100) {
455 if (file_printf(ms
, " %d.%d", desc
/ 100000,
456 desc
/ 10000 % 10) == -1)
458 if (desc
/ 1000 % 10 > 0)
459 if (file_printf(ms
, ".%d", desc
/ 1000 % 10) == -1)
461 if ((desc
% 1000 > 0) || (desc
% 100000 == 0))
462 if (file_printf(ms
, " (%d)", desc
) == -1)
464 } else if (desc
< 500000) {
465 if (file_printf(ms
, " %d.%d", desc
/ 100000,
466 desc
/ 10000 % 10 + desc
/ 1000 % 10) == -1)
468 if (desc
/ 100 % 10 > 0) {
469 if (file_printf(ms
, " (%d)", desc
) == -1)
471 } else if (desc
/ 10 % 10 > 0) {
472 if (file_printf(ms
, ".%d", desc
/ 10 % 10) == -1)
476 if (file_printf(ms
, " %d.%d", desc
/ 100000,
477 desc
/ 1000 % 100) == -1)
479 if ((desc
/ 100 % 10 > 0) ||
480 (desc
% 100000 / 100 == 0)) {
481 if (file_printf(ms
, " (%d)", desc
) == -1)
483 } else if (desc
/ 10 % 10 > 0) {
484 if (file_printf(ms
, ".%d", desc
/ 10 % 10) == -1)
492 do_bid_note(struct magic_set
*ms
, unsigned char *nbuf
, uint32_t type
,
493 int swap
__attribute__((__unused__
)), uint32_t namesz
, uint32_t descsz
,
494 size_t noff
, size_t doff
, int *flags
)
496 if (namesz
== 4 && strcmp((char *)&nbuf
[noff
], "GNU") == 0 &&
497 type
== NT_GNU_BUILD_ID
&& (descsz
== 16 || descsz
== 20)) {
500 *flags
|= FLAGS_DID_BUILD_ID
;
501 if (file_printf(ms
, ", BuildID[%s]=", descsz
== 16 ? "md5/uuid" :
504 (void)memcpy(desc
, &nbuf
[doff
], descsz
);
505 for (i
= 0; i
< descsz
; i
++)
506 if (file_printf(ms
, "%02x", desc
[i
]) == -1)
514 do_os_note(struct magic_set
*ms
, unsigned char *nbuf
, uint32_t type
,
515 int swap
, uint32_t namesz
, uint32_t descsz
,
516 size_t noff
, size_t doff
, int *flags
)
518 if (namesz
== 5 && strcmp((char *)&nbuf
[noff
], "SuSE") == 0 &&
519 type
== NT_GNU_VERSION
&& descsz
== 2) {
520 *flags
|= FLAGS_DID_OS_NOTE
;
521 file_printf(ms
, ", for SuSE %d.%d", nbuf
[doff
], nbuf
[doff
+ 1]);
525 if (namesz
== 4 && strcmp((char *)&nbuf
[noff
], "GNU") == 0 &&
526 type
== NT_GNU_VERSION
&& descsz
== 16) {
528 (void)memcpy(desc
, &nbuf
[doff
], sizeof(desc
));
530 *flags
|= FLAGS_DID_OS_NOTE
;
531 if (file_printf(ms
, ", for GNU/") == -1)
533 switch (elf_getu32(swap
, desc
[0])) {
535 if (file_printf(ms
, "Linux") == -1)
539 if (file_printf(ms
, "Hurd") == -1)
543 if (file_printf(ms
, "Solaris") == -1)
546 case GNU_OS_KFREEBSD
:
547 if (file_printf(ms
, "kFreeBSD") == -1)
551 if (file_printf(ms
, "kNetBSD") == -1)
555 if (file_printf(ms
, "<unknown>") == -1)
558 if (file_printf(ms
, " %d.%d.%d", elf_getu32(swap
, desc
[1]),
559 elf_getu32(swap
, desc
[2]), elf_getu32(swap
, desc
[3])) == -1)
564 if (namesz
== 7 && strcmp((char *)&nbuf
[noff
], "NetBSD") == 0) {
565 if (type
== NT_NETBSD_VERSION
&& descsz
== 4) {
566 *flags
|= FLAGS_DID_OS_NOTE
;
567 do_note_netbsd_version(ms
, swap
, &nbuf
[doff
]);
572 if (namesz
== 8 && strcmp((char *)&nbuf
[noff
], "FreeBSD") == 0) {
573 if (type
== NT_FREEBSD_VERSION
&& descsz
== 4) {
574 *flags
|= FLAGS_DID_OS_NOTE
;
575 do_note_freebsd_version(ms
, swap
, &nbuf
[doff
]);
580 if (namesz
== 8 && strcmp((char *)&nbuf
[noff
], "OpenBSD") == 0 &&
581 type
== NT_OPENBSD_VERSION
&& descsz
== 4) {
582 *flags
|= FLAGS_DID_OS_NOTE
;
583 if (file_printf(ms
, ", for OpenBSD") == -1)
585 /* Content of note is always 0 */
589 if (namesz
== 10 && strcmp((char *)&nbuf
[noff
], "DragonFly") == 0 &&
590 type
== NT_DRAGONFLY_VERSION
&& descsz
== 4) {
592 *flags
|= FLAGS_DID_OS_NOTE
;
593 if (file_printf(ms
, ", for DragonFly") == -1)
595 (void)memcpy(&desc
, &nbuf
[doff
], sizeof(desc
));
596 desc
= elf_getu32(swap
, desc
);
597 if (file_printf(ms
, " %d.%d.%d", desc
/ 100000,
598 desc
/ 10000 % 10, desc
% 10000) == -1)
606 do_pax_note(struct magic_set
*ms
, unsigned char *nbuf
, uint32_t type
,
607 int swap
, uint32_t namesz
, uint32_t descsz
,
608 size_t noff
, size_t doff
, int *flags
)
610 if (namesz
== 4 && strcmp((char *)&nbuf
[noff
], "PaX") == 0 &&
611 type
== NT_NETBSD_PAX
&& descsz
== 4) {
612 static const char *pax
[] = {
624 *flags
|= FLAGS_DID_NETBSD_PAX
;
625 (void)memcpy(&desc
, &nbuf
[doff
], sizeof(desc
));
626 desc
= elf_getu32(swap
, desc
);
628 if (desc
&& file_printf(ms
, ", PaX: ") == -1)
631 for (i
= 0; i
< __arraycount(pax
); i
++) {
632 if (((1 << (int)i
) & desc
) == 0)
634 if (file_printf(ms
, "%s%s", did
++ ? "," : "",
644 do_core_note(struct magic_set
*ms
, unsigned char *nbuf
, uint32_t type
,
645 int swap
, uint32_t namesz
, uint32_t descsz
,
646 size_t noff
, size_t doff
, int *flags
, size_t size
, int clazz
)
651 * Sigh. The 2.0.36 kernel in Debian 2.1, at
652 * least, doesn't correctly implement name
653 * sections, in core dumps, as specified by
654 * the "Program Linking" section of "UNIX(R) System
655 * V Release 4 Programmer's Guide: ANSI C and
656 * Programming Support Tools", because my copy
657 * clearly says "The first 'namesz' bytes in 'name'
658 * contain a *null-terminated* [emphasis mine]
659 * character representation of the entry's owner
660 * or originator", but the 2.0.36 kernel code
661 * doesn't include the terminating null in the
664 if ((namesz
== 4 && strncmp((char *)&nbuf
[noff
], "CORE", 4) == 0) ||
665 (namesz
== 5 && strcmp((char *)&nbuf
[noff
], "CORE") == 0)) {
666 os_style
= OS_STYLE_SVR4
;
669 if ((namesz
== 8 && strcmp((char *)&nbuf
[noff
], "FreeBSD") == 0)) {
670 os_style
= OS_STYLE_FREEBSD
;
673 if ((namesz
>= 11 && strncmp((char *)&nbuf
[noff
], "NetBSD-CORE", 11)
675 os_style
= OS_STYLE_NETBSD
;
678 if (os_style
!= -1 && (*flags
& FLAGS_DID_CORE_STYLE
) == 0) {
679 if (file_printf(ms
, ", %s-style", os_style_names
[os_style
])
682 *flags
|= FLAGS_DID_CORE_STYLE
;
686 case OS_STYLE_NETBSD
:
687 if (type
== NT_NETBSD_CORE_PROCINFO
) {
691 * Extract the program name. It is at
692 * offset 0x7c, and is up to 32-bytes,
693 * including the terminating NUL.
695 if (file_printf(ms
, ", from '%.31s'",
696 file_printable(sbuf
, sizeof(sbuf
),
697 (const char *)&nbuf
[doff
+ 0x7c])) == -1)
701 * Extract the signal number. It is at
704 (void)memcpy(&signo
, &nbuf
[doff
+ 0x08],
706 if (file_printf(ms
, " (signal %u)",
707 elf_getu32(swap
, signo
)) == -1)
709 *flags
|= FLAGS_DID_CORE
;
715 if (type
== NT_PRPSINFO
&& *flags
& FLAGS_IS_CORE
) {
719 * Extract the program name. We assume
720 * it to be 16 characters (that's what it
721 * is in SunOS 5.x and Linux).
723 * Unfortunately, it's at a different offset
724 * in various OSes, so try multiple offsets.
725 * If the characters aren't all printable,
728 for (i
= 0; i
< NOFFSETS
; i
++) {
729 unsigned char *cname
, *cp
;
730 size_t reloffset
= prpsoffsets(i
);
731 size_t noffset
= doff
+ reloffset
;
733 for (j
= 0; j
< 16; j
++, noffset
++,
736 * Make sure we're not past
737 * the end of the buffer; if
738 * we are, just give up.
744 * Make sure we're not past
745 * the end of the contents;
746 * if we are, this obviously
747 * isn't the right offset.
749 if (reloffset
>= descsz
)
771 if (!isprint(c
) || isquote(c
))
780 * Try next offsets, in case this match is
781 * in the middle of a string.
783 for (k
= i
+ 1 ; k
< NOFFSETS
; k
++) {
786 if (prpsoffsets(k
) >= prpsoffsets(i
))
788 for (no
= doff
+ prpsoffsets(k
);
789 no
< doff
+ prpsoffsets(i
); no
++)
791 && isprint(nbuf
[no
]);
796 cname
= (unsigned char *)
797 &nbuf
[doff
+ prpsoffsets(i
)];
798 for (cp
= cname
; *cp
&& isprint(*cp
); cp
++)
801 * Linux apparently appends a space at the end
802 * of the command line: remove it.
804 while (cp
> cname
&& isspace(cp
[-1]))
806 if (file_printf(ms
, ", from '%.*s'",
807 (int)(cp
- cname
), cname
) == -1)
809 *flags
|= FLAGS_DID_CORE
;
823 donote(struct magic_set
*ms
, void *vbuf
, size_t offset
, size_t size
,
824 int clazz
, int swap
, size_t align
, int *flags
, uint16_t *notecount
)
829 uint32_t namesz
, descsz
;
830 unsigned char *nbuf
= CAST(unsigned char *, vbuf
);
836 if (xnh_sizeof
+ offset
> size
) {
838 * We're out of note headers.
840 return xnh_sizeof
+ offset
;
843 (void)memcpy(xnh_addr
, &nbuf
[offset
], xnh_sizeof
);
844 offset
+= xnh_sizeof
;
848 if ((namesz
== 0) && (descsz
== 0)) {
850 * We're out of note headers.
852 return (offset
>= size
) ? offset
: size
;
855 if (namesz
& 0x80000000) {
856 (void)file_printf(ms
, ", bad note name size 0x%lx",
857 (unsigned long)namesz
);
861 if (descsz
& 0x80000000) {
862 (void)file_printf(ms
, ", bad note description size 0x%lx",
863 (unsigned long)descsz
);
868 doff
= ELF_ALIGN(offset
+ namesz
);
870 if (offset
+ namesz
> size
) {
872 * We're past the end of the buffer.
877 offset
= ELF_ALIGN(doff
+ descsz
);
878 if (doff
+ descsz
> size
) {
880 * We're past the end of the buffer.
882 return (offset
>= size
) ? offset
: size
;
885 if ((*flags
& FLAGS_DID_OS_NOTE
) == 0) {
886 if (do_os_note(ms
, nbuf
, xnh_type
, swap
,
887 namesz
, descsz
, noff
, doff
, flags
))
891 if ((*flags
& FLAGS_DID_BUILD_ID
) == 0) {
892 if (do_bid_note(ms
, nbuf
, xnh_type
, swap
,
893 namesz
, descsz
, noff
, doff
, flags
))
897 if ((*flags
& FLAGS_DID_NETBSD_PAX
) == 0) {
898 if (do_pax_note(ms
, nbuf
, xnh_type
, swap
,
899 namesz
, descsz
, noff
, doff
, flags
))
903 if ((*flags
& FLAGS_DID_CORE
) == 0) {
904 if (do_core_note(ms
, nbuf
, xnh_type
, swap
,
905 namesz
, descsz
, noff
, doff
, flags
, size
, clazz
))
909 if (namesz
== 7 && strcmp((char *)&nbuf
[noff
], "NetBSD") == 0) {
913 case NT_NETBSD_VERSION
:
915 case NT_NETBSD_MARCH
:
916 if (*flags
& FLAGS_DID_NETBSD_MARCH
)
918 *flags
|= FLAGS_DID_NETBSD_MARCH
;
919 if (file_printf(ms
, ", compiled for: %.*s",
920 (int)descsz
, (const char *)&nbuf
[doff
]) == -1)
923 case NT_NETBSD_CMODEL
:
924 if (*flags
& FLAGS_DID_NETBSD_CMODEL
)
926 *flags
|= FLAGS_DID_NETBSD_CMODEL
;
927 if (file_printf(ms
, ", compiler model: %.*s",
928 (int)descsz
, (const char *)&nbuf
[doff
]) == -1)
932 if (*flags
& FLAGS_DID_NETBSD_UNKNOWN
)
934 *flags
|= FLAGS_DID_NETBSD_UNKNOWN
;
935 if (file_printf(ms
, ", note=%u", xnh_type
) == -1)
945 /* SunOS 5.x hardware capability descriptions */
946 typedef struct cap_desc
{
951 static const cap_desc_t cap_desc_sparc
[] = {
952 { AV_SPARC_MUL32
, "MUL32" },
953 { AV_SPARC_DIV32
, "DIV32" },
954 { AV_SPARC_FSMULD
, "FSMULD" },
955 { AV_SPARC_V8PLUS
, "V8PLUS" },
956 { AV_SPARC_POPC
, "POPC" },
957 { AV_SPARC_VIS
, "VIS" },
958 { AV_SPARC_VIS2
, "VIS2" },
959 { AV_SPARC_ASI_BLK_INIT
, "ASI_BLK_INIT" },
960 { AV_SPARC_FMAF
, "FMAF" },
961 { AV_SPARC_FJFMAU
, "FJFMAU" },
962 { AV_SPARC_IMA
, "IMA" },
966 static const cap_desc_t cap_desc_386
[] = {
967 { AV_386_FPU
, "FPU" },
968 { AV_386_TSC
, "TSC" },
969 { AV_386_CX8
, "CX8" },
970 { AV_386_SEP
, "SEP" },
971 { AV_386_AMD_SYSC
, "AMD_SYSC" },
972 { AV_386_CMOV
, "CMOV" },
973 { AV_386_MMX
, "MMX" },
974 { AV_386_AMD_MMX
, "AMD_MMX" },
975 { AV_386_AMD_3DNow
, "AMD_3DNow" },
976 { AV_386_AMD_3DNowx
, "AMD_3DNowx" },
977 { AV_386_FXSR
, "FXSR" },
978 { AV_386_SSE
, "SSE" },
979 { AV_386_SSE2
, "SSE2" },
980 { AV_386_PAUSE
, "PAUSE" },
981 { AV_386_SSE3
, "SSE3" },
982 { AV_386_MON
, "MON" },
983 { AV_386_CX16
, "CX16" },
984 { AV_386_AHF
, "AHF" },
985 { AV_386_TSCP
, "TSCP" },
986 { AV_386_AMD_SSE4A
, "AMD_SSE4A" },
987 { AV_386_POPCNT
, "POPCNT" },
988 { AV_386_AMD_LZCNT
, "AMD_LZCNT" },
989 { AV_386_SSSE3
, "SSSE3" },
990 { AV_386_SSE4_1
, "SSE4.1" },
991 { AV_386_SSE4_2
, "SSE4.2" },
996 doshn(struct magic_set
*ms
, int clazz
, int swap
, int fd
, off_t off
, int num
,
997 size_t size
, off_t fsize
, int mach
, int strtab
, int *flags
,
1005 off_t noff
, coff
, name_off
;
1006 uint64_t cap_hw1
= 0; /* SunOS 5.x hardware capabilites */
1007 uint64_t cap_sf1
= 0; /* SunOS 5.x software capabilites */
1011 if (size
!= xsh_sizeof
) {
1012 if (file_printf(ms
, ", corrupted section header size") == -1)
1017 /* Read offset of name section to be able to read section names later */
1018 if (pread(fd
, xsh_addr
, xsh_sizeof
, CAST(off_t
, (off
+ size
* strtab
)))
1019 < (ssize_t
)xsh_sizeof
) {
1023 name_off
= xsh_offset
;
1025 for ( ; num
; num
--) {
1026 /* Read the name of this section. */
1027 if ((namesize
= pread(fd
, name
, sizeof(name
) - 1, name_off
+ xsh_name
)) == -1) {
1031 name
[namesize
] = '\0';
1032 if (strcmp(name
, ".debug_info") == 0)
1035 if (pread(fd
, xsh_addr
, xsh_sizeof
, off
) < (ssize_t
)xsh_sizeof
) {
1041 /* Things we can determine before we seek */
1050 if (fsize
!= SIZE_UNKNOWN
&& xsh_offset
> fsize
) {
1051 /* Perhaps warn here */
1057 /* Things we can determine when we seek */
1060 if ((nbuf
= malloc(xsh_size
)) == NULL
) {
1061 file_error(ms
, errno
, "Cannot allocate memory"
1065 if (pread(fd
, nbuf
, xsh_size
, xsh_offset
) < (ssize_t
)xsh_size
) {
1073 if (noff
>= (off_t
)xsh_size
)
1075 noff
= donote(ms
, nbuf
, (size_t)noff
,
1076 xsh_size
, clazz
, swap
, 4, flags
, notecount
);
1096 if (lseek(fd
, xsh_offset
, SEEK_SET
) == (off_t
)-1) {
1104 char cbuf
[/*CONSTCOND*/
1105 MAX(sizeof cap32
, sizeof cap64
)];
1106 if ((coff
+= xcap_sizeof
) > (off_t
)xsh_size
)
1108 if (read(fd
, cbuf
, (size_t)xcap_sizeof
) !=
1109 (ssize_t
)xcap_sizeof
) {
1113 if (cbuf
[0] == 'A') {
1117 memcpy(&len
, p
, sizeof(len
));
1119 len
= getu32(swap
, len
);
1120 if (memcmp("gnu", p
, 3) != 0) {
1122 ", unknown capability %.3s", p
)
1129 memcpy(&len
, p
, sizeof(len
));
1131 len
= getu32(swap
, len
);
1133 if (file_printf(ms
, ", unknown gnu"
1134 " capability tag %d", tag
)
1143 (void)memcpy(xcap_addr
, cbuf
, xcap_sizeof
);
1148 cap_hw1
|= xcap_val
;
1151 cap_sf1
|= xcap_val
;
1155 ", with unknown capability "
1156 "0x%" INT64_T_FORMAT
"x = 0x%"
1158 (unsigned long long)xcap_tag
,
1159 (unsigned long long)xcap_val
) == -1)
1173 if (file_printf(ms
, ", %sstripped", stripped
? "" : "not ") == -1)
1176 const cap_desc_t
*cdp
;
1179 case EM_SPARC32PLUS
:
1181 cdp
= cap_desc_sparc
;
1192 if (file_printf(ms
, ", uses") == -1)
1195 while (cdp
->cd_name
) {
1196 if (cap_hw1
& cdp
->cd_mask
) {
1198 " %s", cdp
->cd_name
) == -1)
1200 cap_hw1
&= ~cdp
->cd_mask
;
1206 " unknown hardware capability 0x%"
1208 (unsigned long long)cap_hw1
) == -1)
1212 " hardware capability 0x%" INT64_T_FORMAT
"x",
1213 (unsigned long long)cap_hw1
) == -1)
1218 if (cap_sf1
& SF1_SUNW_FPUSED
) {
1220 (cap_sf1
& SF1_SUNW_FPKNWN
)
1221 ? ", uses frame pointer"
1222 : ", not known to use frame pointer") == -1)
1225 cap_sf1
&= ~SF1_SUNW_MASK
;
1228 ", with unknown software capability 0x%"
1230 (unsigned long long)cap_sf1
) == -1)
1237 * Look through the program headers of an executable image, searching
1238 * for a PT_INTERP section; if one is found, it's dynamically linked,
1239 * otherwise it's statically linked.
1242 dophn_exec(struct magic_set
*ms
, int clazz
, int swap
, int fd
, off_t off
,
1243 int num
, size_t size
, off_t fsize
, int sh_num
, int *flags
,
1244 uint16_t *notecount
)
1248 const char *linking_style
= "statically";
1249 const char *interp
= "";
1250 unsigned char nbuf
[BUFSIZ
];
1253 size_t offset
, align
, len
;
1255 if (size
!= xph_sizeof
) {
1256 if (file_printf(ms
, ", corrupted program header size") == -1)
1261 for ( ; num
; num
--) {
1262 if (pread(fd
, xph_addr
, xph_sizeof
, off
) < (ssize_t
)xph_sizeof
) {
1271 /* Things we can determine before we seek */
1274 linking_style
= "dynamically";
1277 if (sh_num
) /* Did this through section headers */
1279 if (((align
= xph_align
) & 0x80000000UL
) != 0 ||
1282 ", invalid note alignment 0x%lx",
1283 (unsigned long)align
) == -1)
1289 len
= xph_filesz
< sizeof(nbuf
) ? xph_filesz
1291 bufsize
= pread(fd
, nbuf
, len
, xph_offset
);
1292 if (bufsize
== -1) {
1298 if (fsize
!= SIZE_UNKNOWN
&& xph_offset
> fsize
) {
1299 /* Maybe warn here? */
1305 /* Things we can determine when we seek */
1308 if (bufsize
&& nbuf
[0]) {
1309 nbuf
[bufsize
- 1] = '\0';
1310 interp
= (const char *)nbuf
;
1316 * This is a PT_NOTE section; loop through all the notes
1321 if (offset
>= (size_t)bufsize
)
1323 offset
= donote(ms
, nbuf
, offset
,
1324 (size_t)bufsize
, clazz
, swap
, align
,
1334 if (file_printf(ms
, ", %s linked", linking_style
)
1338 if (file_printf(ms
, ", interpreter %s",
1339 file_printable(ibuf
, sizeof(ibuf
), interp
)) == -1)
1346 file_tryelf(struct magic_set
*ms
, int fd
, const unsigned char *buf
,
1351 char c
[sizeof (int32_t)];
1358 Elf32_Ehdr elf32hdr
;
1359 Elf64_Ehdr elf64hdr
;
1360 uint16_t type
, phnum
, shnum
, notecount
;
1362 if (ms
->flags
& (MAGIC_MIME
|MAGIC_APPLE
))
1365 * ELF executables have multiple section headers in arbitrary
1366 * file locations and thus file(1) cannot determine it from easily.
1367 * Instead we traverse thru all section headers until a symbol table
1368 * one is found or else the binary is stripped.
1369 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
1371 if (buf
[EI_MAG0
] != ELFMAG0
1372 || (buf
[EI_MAG1
] != ELFMAG1
&& buf
[EI_MAG1
] != OLFMAG1
)
1373 || buf
[EI_MAG2
] != ELFMAG2
|| buf
[EI_MAG3
] != ELFMAG3
)
1377 * If we cannot seek, it must be a pipe, socket or fifo.
1379 if((lseek(fd
, (off_t
)0, SEEK_SET
) == (off_t
)-1) && (errno
== ESPIPE
))
1380 fd
= file_pipe2file(ms
, fd
, buf
, nbytes
);
1382 if (fstat(fd
, &st
) == -1) {
1386 if (S_ISREG(st
.st_mode
) || st
.st_size
!= 0)
1389 fsize
= SIZE_UNKNOWN
;
1391 clazz
= buf
[EI_CLASS
];
1396 #define elf_getu(a, b) elf_getu32(a, b)
1398 #define elfhdr elf32hdr
1399 #include "elfclass.h"
1402 #define elf_getu(a, b) elf_getu64(a, b)
1404 #define elfhdr elf64hdr
1405 #include "elfclass.h"
1407 if (file_printf(ms
, ", unknown class %d", clazz
) == -1)