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.168 2019/12/16 03:49:19 christos Exp $")
45 private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
46 off_t, int *, uint16_t *);
48 private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
49 off_t, int, int *, uint16_t *);
50 private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
51 off_t, int, int, int *, uint16_t *);
52 private size_t donote(struct magic_set *, void *, size_t, size_t, int,
53 int, size_t, int *, uint16_t *, int, off_t, int, off_t);
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 32768
65 #define SIZE_UNKNOWN CAST(off_t, -1)
68 toomany(struct magic_set *ms, const char *name, uint16_t num)
70 if (file_printf(ms, ", too many %s (%u)", name, num) == -1)
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 #define elf_getu64(swap, value) getu64(swap, value)
144 #define xsh_addr (clazz == ELFCLASS32 \
145 ? CAST(void *, &sh32) \
146 : CAST(void *, &sh64))
147 #define xsh_sizeof (clazz == ELFCLASS32 \
150 #define xsh_size CAST(size_t, (clazz == ELFCLASS32 \
151 ? elf_getu32(swap, sh32.sh_size) \
152 : elf_getu64(swap, sh64.sh_size)))
153 #define xsh_offset CAST(off_t, (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 xsh_name (clazz == ELFCLASS32 \
160 ? elf_getu32(swap, sh32.sh_name) \
161 : elf_getu32(swap, sh64.sh_name))
163 #define xph_addr (clazz == ELFCLASS32 \
164 ? CAST(void *, &ph32) \
165 : CAST(void *, &ph64))
166 #define xph_sizeof (clazz == ELFCLASS32 \
169 #define xph_type (clazz == ELFCLASS32 \
170 ? elf_getu32(swap, ph32.p_type) \
171 : elf_getu32(swap, ph64.p_type))
172 #define xph_offset CAST(off_t, (clazz == ELFCLASS32 \
173 ? elf_getu32(swap, ph32.p_offset) \
174 : elf_getu64(swap, ph64.p_offset)))
175 #define xph_align CAST(size_t, (clazz == ELFCLASS32 \
176 ? CAST(off_t, (ph32.p_align ? \
177 elf_getu32(swap, ph32.p_align) : 4))\
178 : CAST(off_t, (ph64.p_align ? \
179 elf_getu64(swap, ph64.p_align) : 4))))
180 #define xph_vaddr CAST(size_t, (clazz == ELFCLASS32 \
181 ? CAST(off_t, (ph32.p_vaddr ? \
182 elf_getu32(swap, ph32.p_vaddr) : 4))\
183 : CAST(off_t, (ph64.p_vaddr ? \
184 elf_getu64(swap, ph64.p_vaddr) : 4))))
185 #define xph_filesz CAST(size_t, (clazz == ELFCLASS32 \
186 ? elf_getu32(swap, ph32.p_filesz) \
187 : elf_getu64(swap, ph64.p_filesz)))
188 #define xph_memsz CAST(size_t, ((clazz == ELFCLASS32 \
189 ? elf_getu32(swap, ph32.p_memsz) \
190 : elf_getu64(swap, ph64.p_memsz))))
191 #define xnh_addr (clazz == ELFCLASS32 \
192 ? CAST(void *, &nh32) \
193 : CAST(void *, &nh64))
194 #define xnh_sizeof (clazz == ELFCLASS32 \
197 #define xnh_type (clazz == ELFCLASS32 \
198 ? elf_getu32(swap, nh32.n_type) \
199 : elf_getu32(swap, nh64.n_type))
200 #define xnh_namesz (clazz == ELFCLASS32 \
201 ? elf_getu32(swap, nh32.n_namesz) \
202 : elf_getu32(swap, nh64.n_namesz))
203 #define xnh_descsz (clazz == ELFCLASS32 \
204 ? elf_getu32(swap, nh32.n_descsz) \
205 : elf_getu32(swap, nh64.n_descsz))
207 #define xdh_addr (clazz == ELFCLASS32 \
208 ? CAST(void *, &dh32) \
209 : CAST(void *, &dh64))
210 #define xdh_sizeof (clazz == ELFCLASS32 \
213 #define xdh_tag (clazz == ELFCLASS32 \
214 ? elf_getu32(swap, dh32.d_tag) \
215 : elf_getu64(swap, dh64.d_tag))
216 #define xdh_val (clazz == ELFCLASS32 \
217 ? elf_getu32(swap, dh32.d_un.d_val) \
218 : elf_getu64(swap, dh64.d_un.d_val))
220 #define xcap_addr (clazz == ELFCLASS32 \
221 ? CAST(void *, &cap32) \
222 : CAST(void *, &cap64))
223 #define xcap_sizeof (clazz == ELFCLASS32 \
226 #define xcap_tag (clazz == ELFCLASS32 \
227 ? elf_getu32(swap, cap32.c_tag) \
228 : elf_getu64(swap, cap64.c_tag))
229 #define xcap_val (clazz == ELFCLASS32 \
230 ? elf_getu32(swap, cap32.c_un.c_val) \
231 : elf_getu64(swap, cap64.c_un.c_val))
233 #define xauxv_addr (clazz == ELFCLASS32 \
234 ? CAST(void *, &auxv32) \
235 : CAST(void *, &auxv64))
236 #define xauxv_sizeof (clazz == ELFCLASS32 \
239 #define xauxv_type (clazz == ELFCLASS32 \
240 ? elf_getu32(swap, auxv32.a_type) \
241 : elf_getu64(swap, auxv64.a_type))
242 #define xauxv_val (clazz == ELFCLASS32 \
243 ? elf_getu32(swap, auxv32.a_v) \
244 : elf_getu64(swap, auxv64.a_v))
246 #define prpsoffsets(i) (clazz == ELFCLASS32 \
252 * Try larger offsets first to avoid false matches
253 * from earlier data that happen to look like strings.
255 static const size_t prpsoffsets32[] = {
257 104, /* SunOS 5.x (command line) */
258 88, /* SunOS 5.x (short name) */
259 #endif /* USE_NT_PSINFO */
261 100, /* SunOS 5.x (command line) */
262 84, /* SunOS 5.x (short name) */
264 44, /* Linux (command line) */
265 28, /* Linux (short name) */
267 48, /* Linux PowerPC (command line) */
268 32, /* Linux PowerPC (short name) */
273 static const size_t prpsoffsets64[] = {
275 152, /* SunOS 5.x (command line) */
276 136, /* SunOS 5.x (short name) */
277 #endif /* USE_NT_PSINFO */
279 136, /* SunOS 5.x, 64-bit (command line) */
280 120, /* SunOS 5.x, 64-bit (short name) */
282 56, /* Linux (command line) */
283 40, /* Linux (tested on core from 2.4.x, short name) */
285 16, /* FreeBSD, 64-bit */
288 #define NOFFSETS32 __arraycount(prpsoffsets32)
289 #define NOFFSETS64 __arraycount(prpsoffsets64)
291 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
294 * Look through the program headers of an executable image, searching
295 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
296 * "FreeBSD"; if one is found, try looking in various places in its
297 * contents for a 16-character string containing only printable
298 * characters - if found, that string should be the name of the program
299 * that dropped core. Note: right after that 16-character string is,
300 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
301 * Linux, a longer string (80 characters, in 5.x, probably other
302 * SVR4-flavored systems, and Linux) containing the start of the
303 * command line for that program.
305 * SunOS 5.x core files contain two PT_NOTE sections, with the types
306 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
307 * same info about the command name and command line, so it probably
308 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
309 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
310 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
311 * the SunOS 5.x file command relies on this (and prefers the latter).
313 * The signal number probably appears in a section of type NT_PRSTATUS,
314 * but that's also rather OS-dependent, in ways that are harder to
315 * dissect with heuristics, so I'm not bothering with the signal number.
316 * (I suppose the signal number could be of interest in situations where
317 * you don't have the binary of the program that dropped core; if you
318 * *do* have that binary, the debugger will probably tell you what
322 #define OS_STYLE_SVR4 0
323 #define OS_STYLE_FREEBSD 1
324 #define OS_STYLE_NETBSD 2
326 private const char os_style_names[][8] = {
332 #define FLAGS_CORE_STYLE 0x0003
334 #define FLAGS_DID_CORE 0x0004
335 #define FLAGS_DID_OS_NOTE 0x0008
336 #define FLAGS_DID_BUILD_ID 0x0010
337 #define FLAGS_DID_CORE_STYLE 0x0020
338 #define FLAGS_DID_NETBSD_PAX 0x0040
339 #define FLAGS_DID_NETBSD_MARCH 0x0080
340 #define FLAGS_DID_NETBSD_CMODEL 0x0100
341 #define FLAGS_DID_NETBSD_EMULATION 0x0200
342 #define FLAGS_DID_NETBSD_UNKNOWN 0x0400
343 #define FLAGS_IS_CORE 0x0800
344 #define FLAGS_DID_AUXV 0x1000
347 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
348 int num, size_t size, off_t fsize, int *flags, uint16_t *notecount)
353 unsigned char nbuf[BUFSIZ];
359 if (file_printf(ms, ", no program header") == -1)
363 if (size != xph_sizeof) {
364 if (file_printf(ms, ", corrupted program header size") == -1)
370 * Loop through all the program headers.
372 for ( ; num; num--) {
373 if (pread(fd, xph_addr, xph_sizeof, off) <
374 CAST(ssize_t, xph_sizeof)) {
380 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
381 /* Perhaps warn here */
385 if (xph_type != PT_NOTE)
389 * This is a PT_NOTE section; loop through all the notes
392 len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
393 if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
399 if (offset >= CAST(size_t, bufsize))
401 offset = donote(ms, nbuf, offset, CAST(size_t, bufsize),
402 clazz, swap, 4, flags, notecount, fd, ph_off,
414 do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
417 memcpy(&desc, v, sizeof(desc));
418 desc = elf_getu32(swap, desc);
420 if (file_printf(ms, ", for NetBSD") == -1)
423 * The version number used to be stuck as 199905, and was thus
424 * basically content-free. Newer versions of NetBSD have fixed
425 * this and now use the encoding of __NetBSD_Version__:
431 * r = release ["",A-Z,Z[A-Z] but numeric]
434 if (desc > 100000000U) {
435 uint32_t ver_patch = (desc / 100) % 100;
436 uint32_t ver_rel = (desc / 10000) % 100;
437 uint32_t ver_min = (desc / 1000000) % 100;
438 uint32_t ver_maj = desc / 100000000;
440 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
442 if (ver_rel == 0 && ver_patch != 0) {
443 if (file_printf(ms, ".%u", ver_patch) == -1)
445 } else if (ver_rel != 0) {
446 while (ver_rel > 26) {
447 if (file_printf(ms, "Z") == -1)
451 if (file_printf(ms, "%c", 'A' + ver_rel - 1)
460 do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
464 memcpy(&desc, v, sizeof(desc));
465 desc = elf_getu32(swap, desc);
466 if (file_printf(ms, ", for FreeBSD") == -1)
470 * Contents is __FreeBSD_version, whose relation to OS
471 * versions is defined by a huge table in the Porter's
472 * Handbook. This is the general scheme:
475 * Mmp000 (before 4.10)
476 * Mmi0p0 (before 5.0)
479 * Development branches:
480 * Mmpxxx (before 4.6)
481 * Mmp1xx (before 4.10)
482 * Mmi1xx (before 5.0)
488 * i = minor version increment (491000 -> 4.10)
492 * The first release of FreeBSD to use ELF by default
495 if (desc == 460002) {
496 if (file_printf(ms, " 4.6.2") == -1)
498 } else if (desc < 460100) {
499 if (file_printf(ms, " %d.%d", desc / 100000,
500 desc / 10000 % 10) == -1)
502 if (desc / 1000 % 10 > 0)
503 if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
505 if ((desc % 1000 > 0) || (desc % 100000 == 0))
506 if (file_printf(ms, " (%d)", desc) == -1)
508 } else if (desc < 500000) {
509 if (file_printf(ms, " %d.%d", desc / 100000,
510 desc / 10000 % 10 + desc / 1000 % 10) == -1)
512 if (desc / 100 % 10 > 0) {
513 if (file_printf(ms, " (%d)", desc) == -1)
515 } else if (desc / 10 % 10 > 0) {
516 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
520 if (file_printf(ms, " %d.%d", desc / 100000,
521 desc / 1000 % 100) == -1)
523 if ((desc / 100 % 10 > 0) ||
524 (desc % 100000 / 100 == 0)) {
525 if (file_printf(ms, " (%d)", desc) == -1)
527 } else if (desc / 10 % 10 > 0) {
528 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
537 do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
538 int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz,
539 size_t noff, size_t doff, int *flags)
541 if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "GNU") == 0 &&
542 type == NT_GNU_BUILD_ID && (descsz >= 4 && descsz <= 20)) {
546 *flags |= FLAGS_DID_BUILD_ID;
561 if (file_printf(ms, ", BuildID[%s]=", btype) == -1)
563 memcpy(desc, &nbuf[doff], descsz);
564 for (i = 0; i < descsz; i++)
565 if (file_printf(ms, "%02x", desc[i]) == -1)
569 if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "Go") == 0 &&
570 type == NT_GO_BUILD_ID && descsz < 128) {
571 if (file_printf(ms, ", Go BuildID=%.*s",
572 CAST(int, descsz), RCAST(char *, &nbuf[doff])) == -1)
580 do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
581 int swap, uint32_t namesz, uint32_t descsz,
582 size_t noff, size_t doff, int *flags)
584 const char *name = RCAST(const char *, &nbuf[noff]);
586 if (namesz == 5 && strcmp(name, "SuSE") == 0 &&
587 type == NT_GNU_VERSION && descsz == 2) {
588 *flags |= FLAGS_DID_OS_NOTE;
589 if (file_printf(ms, ", for SuSE %d.%d", nbuf[doff],
590 nbuf[doff + 1]) == -1)
595 if (namesz == 4 && strcmp(name, "GNU") == 0 &&
596 type == NT_GNU_VERSION && descsz == 16) {
598 memcpy(desc, &nbuf[doff], sizeof(desc));
600 *flags |= FLAGS_DID_OS_NOTE;
601 if (file_printf(ms, ", for GNU/") == -1)
603 switch (elf_getu32(swap, desc[0])) {
605 if (file_printf(ms, "Linux") == -1)
609 if (file_printf(ms, "Hurd") == -1)
613 if (file_printf(ms, "Solaris") == -1)
616 case GNU_OS_KFREEBSD:
617 if (file_printf(ms, "kFreeBSD") == -1)
621 if (file_printf(ms, "kNetBSD") == -1)
625 if (file_printf(ms, "<unknown>") == -1)
628 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
629 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
634 if (namesz == 7 && strcmp(name, "NetBSD") == 0) {
635 if (type == NT_NETBSD_VERSION && descsz == 4) {
636 *flags |= FLAGS_DID_OS_NOTE;
637 if (do_note_netbsd_version(ms, swap, &nbuf[doff]) == -1)
643 if (namesz == 8 && strcmp(name, "FreeBSD") == 0) {
644 if (type == NT_FREEBSD_VERSION && descsz == 4) {
645 *flags |= FLAGS_DID_OS_NOTE;
646 if (do_note_freebsd_version(ms, swap, &nbuf[doff])
653 if (namesz == 8 && strcmp(name, "OpenBSD") == 0 &&
654 type == NT_OPENBSD_VERSION && descsz == 4) {
655 *flags |= FLAGS_DID_OS_NOTE;
656 if (file_printf(ms, ", for OpenBSD") == -1)
658 /* Content of note is always 0 */
662 if (namesz == 10 && strcmp(name, "DragonFly") == 0 &&
663 type == NT_DRAGONFLY_VERSION && descsz == 4) {
665 *flags |= FLAGS_DID_OS_NOTE;
666 if (file_printf(ms, ", for DragonFly") == -1)
668 memcpy(&desc, &nbuf[doff], sizeof(desc));
669 desc = elf_getu32(swap, desc);
670 if (file_printf(ms, " %d.%d.%d", desc / 100000,
671 desc / 10000 % 10, desc % 10000) == -1)
679 do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
680 int swap, uint32_t namesz, uint32_t descsz,
681 size_t noff, size_t doff, int *flags)
683 const char *name = RCAST(const char *, &nbuf[noff]);
685 if (namesz == 4 && strcmp(name, "PaX") == 0 &&
686 type == NT_NETBSD_PAX && descsz == 4) {
687 static const char *pax[] = {
699 *flags |= FLAGS_DID_NETBSD_PAX;
700 memcpy(&desc, &nbuf[doff], sizeof(desc));
701 desc = elf_getu32(swap, desc);
703 if (desc && file_printf(ms, ", PaX: ") == -1)
706 for (i = 0; i < __arraycount(pax); i++) {
707 if (((1 << CAST(int, i)) & desc) == 0)
709 if (file_printf(ms, "%s%s", did++ ? "," : "",
719 do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
720 int swap, uint32_t namesz, uint32_t descsz,
721 size_t noff, size_t doff, int *flags, size_t size, int clazz)
724 const char *name = RCAST(const char *, &nbuf[noff]);
728 * Sigh. The 2.0.36 kernel in Debian 2.1, at
729 * least, doesn't correctly implement name
730 * sections, in core dumps, as specified by
731 * the "Program Linking" section of "UNIX(R) System
732 * V Release 4 Programmer's Guide: ANSI C and
733 * Programming Support Tools", because my copy
734 * clearly says "The first 'namesz' bytes in 'name'
735 * contain a *null-terminated* [emphasis mine]
736 * character representation of the entry's owner
737 * or originator", but the 2.0.36 kernel code
738 * doesn't include the terminating null in the
741 if ((namesz == 4 && strncmp(name, "CORE", 4) == 0) ||
742 (namesz == 5 && strcmp(name, "CORE") == 0)) {
743 os_style = OS_STYLE_SVR4;
746 if ((namesz == 8 && strcmp(name, "FreeBSD") == 0)) {
747 os_style = OS_STYLE_FREEBSD;
750 if ((namesz >= 11 && strncmp(name, "NetBSD-CORE", 11)
752 os_style = OS_STYLE_NETBSD;
755 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
756 if (file_printf(ms, ", %s-style", os_style_names[os_style])
759 *flags |= FLAGS_DID_CORE_STYLE;
764 case OS_STYLE_NETBSD:
765 if (type == NT_NETBSD_CORE_PROCINFO) {
767 struct NetBSD_elfcore_procinfo pi;
768 memset(&pi, 0, sizeof(pi));
769 memcpy(&pi, nbuf + doff, MIN(descsz, sizeof(pi)));
771 if (file_printf(ms, ", from '%.31s', pid=%u, uid=%u, "
772 "gid=%u, nlwps=%u, lwp=%u (signal %u/code %u)",
773 file_printable(sbuf, sizeof(sbuf),
774 RCAST(char *, pi.cpi_name), sizeof(pi.cpi_name)),
775 elf_getu32(swap, CAST(uint32_t, pi.cpi_pid)),
776 elf_getu32(swap, pi.cpi_euid),
777 elf_getu32(swap, pi.cpi_egid),
778 elf_getu32(swap, pi.cpi_nlwps),
779 elf_getu32(swap, CAST(uint32_t, pi.cpi_siglwp)),
780 elf_getu32(swap, pi.cpi_signo),
781 elf_getu32(swap, pi.cpi_sigcode)) == -1)
784 *flags |= FLAGS_DID_CORE;
789 case OS_STYLE_FREEBSD:
790 if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
791 size_t argoff, pidoff;
793 if (clazz == ELFCLASS32)
796 argoff = 4 + 4 + 8 + 17;
797 if (file_printf(ms, ", from '%.80s'", nbuf + doff +
800 pidoff = argoff + 81 + 2;
801 if (doff + pidoff + 4 <= size) {
802 if (file_printf(ms, ", pid=%u",
803 elf_getu32(swap, *RCAST(uint32_t *, (nbuf +
804 doff + pidoff)))) == -1)
807 *flags |= FLAGS_DID_CORE;
812 if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
816 * Extract the program name. We assume
817 * it to be 16 characters (that's what it
818 * is in SunOS 5.x and Linux).
820 * Unfortunately, it's at a different offset
821 * in various OSes, so try multiple offsets.
822 * If the characters aren't all printable,
825 for (i = 0; i < NOFFSETS; i++) {
826 unsigned char *cname, *cp;
827 size_t reloffset = prpsoffsets(i);
828 size_t noffset = doff + reloffset;
830 for (j = 0; j < 16; j++, noffset++,
833 * Make sure we're not past
834 * the end of the buffer; if
835 * we are, just give up.
841 * Make sure we're not past
842 * the end of the contents;
843 * if we are, this obviously
844 * isn't the right offset.
846 if (reloffset >= descsz)
868 if (!isprint(c) || isquote(c))
877 * Try next offsets, in case this match is
878 * in the middle of a string.
880 for (k = i + 1 ; k < NOFFSETS; k++) {
883 if (prpsoffsets(k) >= prpsoffsets(i))
885 for (no = doff + prpsoffsets(k);
886 no < doff + prpsoffsets(i); no++)
888 && isprint(nbuf[no]);
893 cname = CAST(unsigned char *,
894 &nbuf[doff + prpsoffsets(i)]);
895 for (cp = cname; cp < nbuf + size && *cp
896 && isprint(*cp); cp++)
899 * Linux apparently appends a space at the end
900 * of the command line: remove it.
902 while (cp > cname && isspace(cp[-1]))
904 if (file_printf(ms, ", from '%.*s'",
905 CAST(int, cp - cname), cname) == -1)
907 *flags |= FLAGS_DID_CORE;
921 get_offset_from_virtaddr(struct magic_set *ms, int swap, int clazz, int fd,
922 off_t off, int num, off_t fsize, uint64_t virtaddr)
928 * Loop through all the program headers and find the header with
929 * virtual address in which the "virtaddr" belongs to.
931 for ( ; num; num--) {
932 if (pread(fd, xph_addr, xph_sizeof, off) <
933 CAST(ssize_t, xph_sizeof)) {
939 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
940 /* Perhaps warn here */
944 if (virtaddr >= xph_vaddr && virtaddr < xph_vaddr + xph_filesz)
945 return xph_offset + (virtaddr - xph_vaddr);
951 get_string_on_virtaddr(struct magic_set *ms,
952 int swap, int clazz, int fd, off_t ph_off, int ph_num,
953 off_t fsize, uint64_t virtaddr, char *buf, ssize_t buflen)
961 offset = get_offset_from_virtaddr(ms, swap, clazz, fd, ph_off, ph_num,
964 (buflen = pread(fd, buf, CAST(size_t, buflen), offset)) <= 0) {
969 buf[buflen - 1] = '\0';
971 /* We expect only printable characters, so return if buffer contains
972 * non-printable character before the '\0' or just '\0'. */
973 for (bptr = buf; *bptr && isprint(CAST(unsigned char, *bptr)); bptr++)
984 do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
985 int swap, uint32_t namesz __attribute__((__unused__)),
986 uint32_t descsz __attribute__((__unused__)),
987 size_t noff __attribute__((__unused__)), size_t doff,
988 int *flags, size_t size __attribute__((__unused__)), int clazz,
989 int fd, off_t ph_off, int ph_num, off_t fsize)
994 size_t elsize = xauxv_sizeof;
999 if ((*flags & (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE)) !=
1000 (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE))
1003 switch (*flags & FLAGS_CORE_STYLE) {
1005 if (type != NT_AUXV)
1009 case OS_STYLE_NETBSD:
1010 if (type != NT_NETBSD_CORE_AUXV)
1013 case OS_STYLE_FREEBSD:
1014 if (type != NT_FREEBSD_PROCSTAT_AUXV)
1022 *flags |= FLAGS_DID_AUXV;
1025 for (size_t off = 0; off + elsize <= descsz; off += elsize) {
1026 memcpy(xauxv_addr, &nbuf[doff + off], xauxv_sizeof);
1027 /* Limit processing to 50 vector entries to prevent DoS */
1029 file_error(ms, 0, "Too many ELF Auxv elements");
1033 switch(xauxv_type) {
1034 case AT_LINUX_EXECFN:
1038 case AT_LINUX_PLATFORM:
1052 tag = "effective uid";
1056 tag = "effective gid";
1070 buflen = get_string_on_virtaddr(ms, swap, clazz, fd,
1071 ph_off, ph_num, fsize, xauxv_val, buf, sizeof(buf));
1076 if (file_printf(ms, ", %s: '%s'", tag, buf) == -1)
1079 if (file_printf(ms, ", %s: %d", tag,
1080 CAST(int, xauxv_val)) == -1)
1091 dodynamic(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
1092 int clazz, int swap)
1096 unsigned char *dbuf = CAST(unsigned char *, vbuf);
1098 if (xdh_sizeof + offset > size) {
1100 * We're out of note headers.
1102 return xdh_sizeof + offset;
1105 memcpy(xdh_addr, &dbuf[offset], xdh_sizeof);
1106 offset += xdh_sizeof;
1110 if (xdh_val & DF_1_PIE)
1123 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
1124 int clazz, int swap, size_t align, int *flags, uint16_t *notecount,
1125 int fd, off_t ph_off, int ph_num, off_t fsize)
1130 uint32_t namesz, descsz;
1131 unsigned char *nbuf = CAST(unsigned char *, vbuf);
1133 if (*notecount == 0)
1137 if (xnh_sizeof + offset > size) {
1139 * We're out of note headers.
1141 return xnh_sizeof + offset;
1144 memset(&nh32, 0, sizeof(nh32));
1145 memset(&nh64, 0, sizeof(nh64));
1147 memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
1148 offset += xnh_sizeof;
1150 namesz = xnh_namesz;
1151 descsz = xnh_descsz;
1153 if ((namesz == 0) && (descsz == 0)) {
1155 * We're out of note headers.
1157 return (offset >= size) ? offset : size;
1160 if (namesz & 0x80000000) {
1161 if (file_printf(ms, ", bad note name size %#lx",
1162 CAST(unsigned long, namesz)) == -1)
1167 if (descsz & 0x80000000) {
1168 if (file_printf(ms, ", bad note description size %#lx",
1169 CAST(unsigned long, descsz)) == -1)
1175 doff = ELF_ALIGN(offset + namesz);
1177 if (offset + namesz > size) {
1179 * We're past the end of the buffer.
1184 offset = ELF_ALIGN(doff + descsz);
1185 if (doff + descsz > size) {
1187 * We're past the end of the buffer.
1189 return (offset >= size) ? offset : size;
1193 if ((*flags & FLAGS_DID_OS_NOTE) == 0) {
1194 if (do_os_note(ms, nbuf, xnh_type, swap,
1195 namesz, descsz, noff, doff, flags))
1199 if ((*flags & FLAGS_DID_BUILD_ID) == 0) {
1200 if (do_bid_note(ms, nbuf, xnh_type, swap,
1201 namesz, descsz, noff, doff, flags))
1205 if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) {
1206 if (do_pax_note(ms, nbuf, xnh_type, swap,
1207 namesz, descsz, noff, doff, flags))
1211 if ((*flags & FLAGS_DID_CORE) == 0) {
1212 if (do_core_note(ms, nbuf, xnh_type, swap,
1213 namesz, descsz, noff, doff, flags, size, clazz))
1217 if ((*flags & FLAGS_DID_AUXV) == 0) {
1218 if (do_auxv_note(ms, nbuf, xnh_type, swap,
1219 namesz, descsz, noff, doff, flags, size, clazz,
1220 fd, ph_off, ph_num, fsize))
1224 if (namesz == 7 && strcmp(RCAST(char *, &nbuf[noff]), "NetBSD") == 0) {
1226 const char *str, *tag;
1230 case NT_NETBSD_VERSION:
1232 case NT_NETBSD_MARCH:
1233 flag = FLAGS_DID_NETBSD_MARCH;
1234 tag = "compiled for";
1236 case NT_NETBSD_CMODEL:
1237 flag = FLAGS_DID_NETBSD_CMODEL;
1238 tag = "compiler model";
1240 case NT_NETBSD_EMULATION:
1241 flag = FLAGS_DID_NETBSD_EMULATION;
1245 if (*flags & FLAGS_DID_NETBSD_UNKNOWN)
1247 *flags |= FLAGS_DID_NETBSD_UNKNOWN;
1248 if (file_printf(ms, ", note=%u", xnh_type) == -1)
1255 str = RCAST(const char *, &nbuf[doff]);
1256 descw = CAST(int, descsz);
1258 file_printf(ms, ", %s: %.*s", tag, descw, str);
1265 /* SunOS 5.x hardware capability descriptions */
1266 typedef struct cap_desc {
1268 const char *cd_name;
1271 static const cap_desc_t cap_desc_sparc[] = {
1272 { AV_SPARC_MUL32, "MUL32" },
1273 { AV_SPARC_DIV32, "DIV32" },
1274 { AV_SPARC_FSMULD, "FSMULD" },
1275 { AV_SPARC_V8PLUS, "V8PLUS" },
1276 { AV_SPARC_POPC, "POPC" },
1277 { AV_SPARC_VIS, "VIS" },
1278 { AV_SPARC_VIS2, "VIS2" },
1279 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
1280 { AV_SPARC_FMAF, "FMAF" },
1281 { AV_SPARC_FJFMAU, "FJFMAU" },
1282 { AV_SPARC_IMA, "IMA" },
1286 static const cap_desc_t cap_desc_386[] = {
1287 { AV_386_FPU, "FPU" },
1288 { AV_386_TSC, "TSC" },
1289 { AV_386_CX8, "CX8" },
1290 { AV_386_SEP, "SEP" },
1291 { AV_386_AMD_SYSC, "AMD_SYSC" },
1292 { AV_386_CMOV, "CMOV" },
1293 { AV_386_MMX, "MMX" },
1294 { AV_386_AMD_MMX, "AMD_MMX" },
1295 { AV_386_AMD_3DNow, "AMD_3DNow" },
1296 { AV_386_AMD_3DNowx, "AMD_3DNowx" },
1297 { AV_386_FXSR, "FXSR" },
1298 { AV_386_SSE, "SSE" },
1299 { AV_386_SSE2, "SSE2" },
1300 { AV_386_PAUSE, "PAUSE" },
1301 { AV_386_SSE3, "SSE3" },
1302 { AV_386_MON, "MON" },
1303 { AV_386_CX16, "CX16" },
1304 { AV_386_AHF, "AHF" },
1305 { AV_386_TSCP, "TSCP" },
1306 { AV_386_AMD_SSE4A, "AMD_SSE4A" },
1307 { AV_386_POPCNT, "POPCNT" },
1308 { AV_386_AMD_LZCNT, "AMD_LZCNT" },
1309 { AV_386_SSSE3, "SSSE3" },
1310 { AV_386_SSE4_1, "SSE4.1" },
1311 { AV_386_SSE4_2, "SSE4.2" },
1316 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
1317 size_t size, off_t fsize, int mach, int strtab, int *flags,
1318 uint16_t *notecount)
1322 int stripped = 1, has_debug_info = 0;
1325 off_t noff, coff, name_off;
1326 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilities */
1327 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilities */
1332 if (file_printf(ms, ", no section header") == -1)
1336 if (size != xsh_sizeof) {
1337 if (file_printf(ms, ", corrupted section header size") == -1)
1342 /* Read offset of name section to be able to read section names later */
1343 if (pread(fd, xsh_addr, xsh_sizeof, CAST(off_t, (off + size * strtab)))
1344 < CAST(ssize_t, xsh_sizeof)) {
1345 if (file_printf(ms, ", missing section headers") == -1)
1349 name_off = xsh_offset;
1351 if (fsize != SIZE_UNKNOWN && fsize < name_off) {
1352 if (file_printf(ms, ", too large section header offset %jd",
1353 (intmax_t)name_off) == -1)
1358 for ( ; num; num--) {
1359 /* Read the name of this section. */
1360 if ((namesize = pread(fd, name, sizeof(name) - 1,
1361 name_off + xsh_name)) == -1) {
1365 name[namesize] = '\0';
1366 if (strcmp(name, ".debug_info") == 0) {
1371 if (pread(fd, xsh_addr, xsh_sizeof, off) <
1372 CAST(ssize_t, xsh_sizeof)) {
1378 /* Things we can determine before we seek */
1387 if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
1388 /* Perhaps warn here */
1395 /* Things we can determine when we seek */
1398 if (CAST(uintmax_t, (xsh_size + xsh_offset)) >
1399 CAST(uintmax_t, fsize)) {
1401 ", note offset/size %#" INTMAX_T_FORMAT
1402 "x+%#" INTMAX_T_FORMAT "x exceeds"
1403 " file size %#" INTMAX_T_FORMAT "x",
1404 CAST(uintmax_t, xsh_offset),
1405 CAST(uintmax_t, xsh_size),
1406 CAST(uintmax_t, fsize)) == -1)
1410 if ((nbuf = malloc(xsh_size)) == NULL) {
1411 file_error(ms, errno, "Cannot allocate memory"
1415 if (pread(fd, nbuf, xsh_size, xsh_offset) <
1416 CAST(ssize_t, xsh_size)) {
1424 if (noff >= CAST(off_t, xsh_size))
1426 noff = donote(ms, nbuf, CAST(size_t, noff),
1427 xsh_size, clazz, swap, 4, flags, notecount,
1448 if (lseek(fd, xsh_offset, SEEK_SET)
1449 == CAST(off_t, -1)) {
1457 char cbuf[/*CONSTCOND*/
1458 MAX(sizeof(cap32), sizeof(cap64))];
1459 if ((coff += xcap_sizeof) >
1460 CAST(off_t, xsh_size))
1462 if (read(fd, cbuf, CAST(size_t, xcap_sizeof)) !=
1463 CAST(ssize_t, xcap_sizeof)) {
1467 if (cbuf[0] == 'A') {
1471 memcpy(&len, p, sizeof(len));
1473 len = getu32(swap, len);
1474 if (memcmp("gnu", p, 3) != 0) {
1476 ", unknown capability %.3s", p)
1483 memcpy(&len, p, sizeof(len));
1485 len = getu32(swap, len);
1487 if (file_printf(ms, ", unknown gnu"
1488 " capability tag %d", tag)
1497 memcpy(xcap_addr, cbuf, xcap_sizeof);
1502 cap_hw1 |= xcap_val;
1505 cap_sf1 |= xcap_val;
1509 ", with unknown capability "
1510 "%#" INT64_T_FORMAT "x = %#"
1512 CAST(unsigned long long, xcap_tag),
1513 CAST(unsigned long long, xcap_val))
1528 if (has_debug_info) {
1529 if (file_printf(ms, ", with debug_info") == -1)
1532 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1535 const cap_desc_t *cdp;
1538 case EM_SPARC32PLUS:
1540 cdp = cap_desc_sparc;
1551 if (file_printf(ms, ", uses") == -1)
1554 while (cdp->cd_name) {
1555 if (cap_hw1 & cdp->cd_mask) {
1557 " %s", cdp->cd_name) == -1)
1559 cap_hw1 &= ~cdp->cd_mask;
1565 " unknown hardware capability %#"
1567 CAST(unsigned long long, cap_hw1)) == -1)
1571 " hardware capability %#" INT64_T_FORMAT "x",
1572 CAST(unsigned long long, cap_hw1)) == -1)
1577 if (cap_sf1 & SF1_SUNW_FPUSED) {
1579 (cap_sf1 & SF1_SUNW_FPKNWN)
1580 ? ", uses frame pointer"
1581 : ", not known to use frame pointer") == -1)
1584 cap_sf1 &= ~SF1_SUNW_MASK;
1587 ", with unknown software capability %#"
1589 CAST(unsigned long long, cap_sf1)) == -1)
1596 * Look through the program headers of an executable image, searching
1597 * for a PT_INTERP section; if one is found, it's dynamically linked,
1598 * otherwise it's statically linked.
1601 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1602 int num, size_t size, off_t fsize, int sh_num, int *flags,
1603 uint16_t *notecount)
1607 const char *linking_style = "statically";
1608 unsigned char nbuf[BUFSIZ];
1610 char interp[BUFSIZ];
1612 size_t offset, align, len;
1615 if (file_printf(ms, ", no program header") == -1)
1619 if (size != xph_sizeof) {
1620 if (file_printf(ms, ", corrupted program header size") == -1)
1626 for ( ; num; num--) {
1628 if (pread(fd, xph_addr, xph_sizeof, off) <
1629 CAST(ssize_t, xph_sizeof)) {
1638 /* Things we can determine before we seek */
1644 if (sh_num) /* Did this through section headers */
1646 if (((align = xph_align) & 0x80000000UL) != 0 ||
1649 ", invalid note alignment %#lx",
1650 CAST(unsigned long, align)) == -1)
1656 linking_style = "dynamically";
1661 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
1662 /* Maybe warn here? */
1669 len = xph_filesz < sizeof(nbuf) ? xph_filesz
1671 bufsize = pread(fd, nbuf, len, xph_offset);
1672 if (bufsize == -1) {
1679 /* Things we can determine when we seek */
1683 // Let DF_1 determine if we are PIE or not.
1686 if (offset >= CAST(size_t, bufsize))
1688 offset = dodynamic(ms, nbuf, offset,
1689 CAST(size_t, bufsize), clazz, swap);
1696 if (bufsize && nbuf[0]) {
1697 nbuf[bufsize - 1] = '\0';
1698 memcpy(interp, nbuf, CAST(size_t, bufsize));
1700 strlcpy(interp, "*empty*", sizeof(interp));
1704 * This is a PT_NOTE section; loop through all the notes
1709 if (offset >= CAST(size_t, bufsize))
1711 offset = donote(ms, nbuf, offset,
1712 CAST(size_t, bufsize), clazz, swap, align,
1713 flags, notecount, fd, 0, 0, 0);
1722 if (file_printf(ms, ", %s linked", linking_style)
1726 if (file_printf(ms, ", interpreter %s",
1727 file_printable(ibuf, sizeof(ibuf), interp, sizeof(interp)))
1735 file_tryelf(struct magic_set *ms, const struct buffer *b)
1738 const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
1739 size_t nbytes = b->flen;
1742 char c[sizeof(int32_t)];
1747 const struct stat *stp;
1750 Elf32_Ehdr elf32hdr;
1751 Elf64_Ehdr elf64hdr;
1752 uint16_t type, phnum, shnum, notecount;
1754 if (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION))
1757 * ELF executables have multiple section headers in arbitrary
1758 * file locations and thus file(1) cannot determine it from easily.
1759 * Instead we traverse thru all section headers until a symbol table
1760 * one is found or else the binary is stripped.
1761 * Return immediately if it's not ELF (so we avoid pipe2file unless
1764 if (buf[EI_MAG0] != ELFMAG0
1765 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1766 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1770 * If we cannot seek, it must be a pipe, socket or fifo.
1772 if((lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(off_t, -1))
1773 && (errno == ESPIPE))
1774 fd = file_pipe2file(ms, fd, buf, nbytes);
1783 * b->st.st_size != 0 if previous fstat() succeeded,
1784 * which is likely, we can avoid extra stat() call.
1786 if (b->st.st_size == 0) {
1788 if (fstat(fd, &st) == -1) {
1793 if (S_ISREG(stp->st_mode) || stp->st_size != 0)
1794 fsize = stp->st_size;
1796 fsize = SIZE_UNKNOWN;
1798 clazz = buf[EI_CLASS];
1803 #define elf_getu(a, b) elf_getu32(a, b)
1805 #define elfhdr elf32hdr
1806 #include "elfclass.h"
1809 #define elf_getu(a, b) elf_getu64(a, b)
1811 #define elfhdr elf64hdr
1812 #include "elfclass.h"
1814 if (file_printf(ms, ", unknown class %d", clazz) == -1)