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.173 2020/06/07 22:12:54 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 (ms->flags & MAGIC_MIME)
72 if (file_printf(ms, ", too many %s (%u)", name, num) == -1)
78 getu16(int swap, uint16_t value)
88 retval.c[0] = tmpval.c[1];
89 retval.c[1] = tmpval.c[0];
97 getu32(int swap, uint32_t value)
107 retval.c[0] = tmpval.c[3];
108 retval.c[1] = tmpval.c[2];
109 retval.c[2] = tmpval.c[1];
110 retval.c[3] = tmpval.c[0];
118 getu64(int swap, uint64_t value)
128 retval.c[0] = tmpval.c[7];
129 retval.c[1] = tmpval.c[6];
130 retval.c[2] = tmpval.c[5];
131 retval.c[3] = tmpval.c[4];
132 retval.c[4] = tmpval.c[3];
133 retval.c[5] = tmpval.c[2];
134 retval.c[6] = tmpval.c[1];
135 retval.c[7] = tmpval.c[0];
142 #define elf_getu16(swap, value) getu16(swap, value)
143 #define elf_getu32(swap, value) getu32(swap, value)
144 #define elf_getu64(swap, value) getu64(swap, value)
146 #define xsh_addr (clazz == ELFCLASS32 \
147 ? CAST(void *, &sh32) \
148 : CAST(void *, &sh64))
149 #define xsh_sizeof (clazz == ELFCLASS32 \
152 #define xsh_size CAST(size_t, (clazz == ELFCLASS32 \
153 ? elf_getu32(swap, sh32.sh_size) \
154 : elf_getu64(swap, sh64.sh_size)))
155 #define xsh_offset CAST(off_t, (clazz == ELFCLASS32 \
156 ? elf_getu32(swap, sh32.sh_offset) \
157 : elf_getu64(swap, sh64.sh_offset)))
158 #define xsh_type (clazz == ELFCLASS32 \
159 ? elf_getu32(swap, sh32.sh_type) \
160 : elf_getu32(swap, sh64.sh_type))
161 #define xsh_name (clazz == ELFCLASS32 \
162 ? elf_getu32(swap, sh32.sh_name) \
163 : elf_getu32(swap, sh64.sh_name))
165 #define xph_addr (clazz == ELFCLASS32 \
166 ? CAST(void *, &ph32) \
167 : CAST(void *, &ph64))
168 #define xph_sizeof (clazz == ELFCLASS32 \
171 #define xph_type (clazz == ELFCLASS32 \
172 ? elf_getu32(swap, ph32.p_type) \
173 : elf_getu32(swap, ph64.p_type))
174 #define xph_offset CAST(off_t, (clazz == ELFCLASS32 \
175 ? elf_getu32(swap, ph32.p_offset) \
176 : elf_getu64(swap, ph64.p_offset)))
177 #define xph_align CAST(size_t, (clazz == ELFCLASS32 \
178 ? CAST(off_t, (ph32.p_align ? \
179 elf_getu32(swap, ph32.p_align) : 4))\
180 : CAST(off_t, (ph64.p_align ? \
181 elf_getu64(swap, ph64.p_align) : 4))))
182 #define xph_vaddr CAST(size_t, (clazz == ELFCLASS32 \
183 ? CAST(off_t, (ph32.p_vaddr ? \
184 elf_getu32(swap, ph32.p_vaddr) : 4))\
185 : CAST(off_t, (ph64.p_vaddr ? \
186 elf_getu64(swap, ph64.p_vaddr) : 4))))
187 #define xph_filesz CAST(size_t, (clazz == ELFCLASS32 \
188 ? elf_getu32(swap, ph32.p_filesz) \
189 : elf_getu64(swap, ph64.p_filesz)))
190 #define xph_memsz CAST(size_t, ((clazz == ELFCLASS32 \
191 ? elf_getu32(swap, ph32.p_memsz) \
192 : elf_getu64(swap, ph64.p_memsz))))
193 #define xnh_addr (clazz == ELFCLASS32 \
194 ? CAST(void *, &nh32) \
195 : CAST(void *, &nh64))
196 #define xnh_sizeof (clazz == ELFCLASS32 \
199 #define xnh_type (clazz == ELFCLASS32 \
200 ? elf_getu32(swap, nh32.n_type) \
201 : elf_getu32(swap, nh64.n_type))
202 #define xnh_namesz (clazz == ELFCLASS32 \
203 ? elf_getu32(swap, nh32.n_namesz) \
204 : elf_getu32(swap, nh64.n_namesz))
205 #define xnh_descsz (clazz == ELFCLASS32 \
206 ? elf_getu32(swap, nh32.n_descsz) \
207 : elf_getu32(swap, nh64.n_descsz))
209 #define xdh_addr (clazz == ELFCLASS32 \
210 ? CAST(void *, &dh32) \
211 : CAST(void *, &dh64))
212 #define xdh_sizeof (clazz == ELFCLASS32 \
215 #define xdh_tag (clazz == ELFCLASS32 \
216 ? elf_getu32(swap, dh32.d_tag) \
217 : elf_getu64(swap, dh64.d_tag))
218 #define xdh_val (clazz == ELFCLASS32 \
219 ? elf_getu32(swap, dh32.d_un.d_val) \
220 : elf_getu64(swap, dh64.d_un.d_val))
222 #define xcap_addr (clazz == ELFCLASS32 \
223 ? CAST(void *, &cap32) \
224 : CAST(void *, &cap64))
225 #define xcap_sizeof (clazz == ELFCLASS32 \
228 #define xcap_tag (clazz == ELFCLASS32 \
229 ? elf_getu32(swap, cap32.c_tag) \
230 : elf_getu64(swap, cap64.c_tag))
231 #define xcap_val (clazz == ELFCLASS32 \
232 ? elf_getu32(swap, cap32.c_un.c_val) \
233 : elf_getu64(swap, cap64.c_un.c_val))
235 #define xauxv_addr (clazz == ELFCLASS32 \
236 ? CAST(void *, &auxv32) \
237 : CAST(void *, &auxv64))
238 #define xauxv_sizeof (clazz == ELFCLASS32 \
241 #define xauxv_type (clazz == ELFCLASS32 \
242 ? elf_getu32(swap, auxv32.a_type) \
243 : elf_getu64(swap, auxv64.a_type))
244 #define xauxv_val (clazz == ELFCLASS32 \
245 ? elf_getu32(swap, auxv32.a_v) \
246 : elf_getu64(swap, auxv64.a_v))
248 #define prpsoffsets(i) (clazz == ELFCLASS32 \
254 * Try larger offsets first to avoid false matches
255 * from earlier data that happen to look like strings.
257 static const size_t prpsoffsets32[] = {
259 104, /* SunOS 5.x (command line) */
260 88, /* SunOS 5.x (short name) */
261 #endif /* USE_NT_PSINFO */
263 100, /* SunOS 5.x (command line) */
264 84, /* SunOS 5.x (short name) */
266 44, /* Linux (command line) */
267 28, /* Linux (short name) */
269 48, /* Linux PowerPC (command line) */
270 32, /* Linux PowerPC (short name) */
275 static const size_t prpsoffsets64[] = {
277 152, /* SunOS 5.x (command line) */
278 136, /* SunOS 5.x (short name) */
279 #endif /* USE_NT_PSINFO */
281 136, /* SunOS 5.x, 64-bit (command line) */
282 120, /* SunOS 5.x, 64-bit (short name) */
284 56, /* Linux (command line) */
285 40, /* Linux (tested on core from 2.4.x, short name) */
287 16, /* FreeBSD, 64-bit */
290 #define NOFFSETS32 __arraycount(prpsoffsets32)
291 #define NOFFSETS64 __arraycount(prpsoffsets64)
293 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
296 * Look through the program headers of an executable image, searching
297 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
298 * "FreeBSD"; if one is found, try looking in various places in its
299 * contents for a 16-character string containing only printable
300 * characters - if found, that string should be the name of the program
301 * that dropped core. Note: right after that 16-character string is,
302 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
303 * Linux, a longer string (80 characters, in 5.x, probably other
304 * SVR4-flavored systems, and Linux) containing the start of the
305 * command line for that program.
307 * SunOS 5.x core files contain two PT_NOTE sections, with the types
308 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
309 * same info about the command name and command line, so it probably
310 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
311 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
312 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
313 * the SunOS 5.x file command relies on this (and prefers the latter).
315 * The signal number probably appears in a section of type NT_PRSTATUS,
316 * but that's also rather OS-dependent, in ways that are harder to
317 * dissect with heuristics, so I'm not bothering with the signal number.
318 * (I suppose the signal number could be of interest in situations where
319 * you don't have the binary of the program that dropped core; if you
320 * *do* have that binary, the debugger will probably tell you what
324 #define OS_STYLE_SVR4 0
325 #define OS_STYLE_FREEBSD 1
326 #define OS_STYLE_NETBSD 2
328 private const char os_style_names[][8] = {
334 #define FLAGS_CORE_STYLE 0x0003
336 #define FLAGS_DID_CORE 0x0004
337 #define FLAGS_DID_OS_NOTE 0x0008
338 #define FLAGS_DID_BUILD_ID 0x0010
339 #define FLAGS_DID_CORE_STYLE 0x0020
340 #define FLAGS_DID_NETBSD_PAX 0x0040
341 #define FLAGS_DID_NETBSD_MARCH 0x0080
342 #define FLAGS_DID_NETBSD_CMODEL 0x0100
343 #define FLAGS_DID_NETBSD_EMULATION 0x0200
344 #define FLAGS_DID_NETBSD_UNKNOWN 0x0400
345 #define FLAGS_IS_CORE 0x0800
346 #define FLAGS_DID_AUXV 0x1000
349 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
350 int num, size_t size, off_t fsize, int *flags, uint16_t *notecount)
355 unsigned char nbuf[BUFSIZ];
360 if (ms->flags & MAGIC_MIME)
364 if (file_printf(ms, ", no program header") == -1)
368 if (size != xph_sizeof) {
369 if (file_printf(ms, ", corrupted program header size") == -1)
375 * Loop through all the program headers.
377 for ( ; num; num--) {
378 if (pread(fd, xph_addr, xph_sizeof, off) <
379 CAST(ssize_t, xph_sizeof)) {
385 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
386 /* Perhaps warn here */
390 if (xph_type != PT_NOTE)
394 * This is a PT_NOTE section; loop through all the notes
397 len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
398 if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
404 if (offset >= CAST(size_t, bufsize))
406 offset = donote(ms, nbuf, offset, CAST(size_t, bufsize),
407 clazz, swap, 4, flags, notecount, fd, ph_off,
419 do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
422 memcpy(&desc, v, sizeof(desc));
423 desc = elf_getu32(swap, desc);
425 if (file_printf(ms, ", for NetBSD") == -1)
428 * The version number used to be stuck as 199905, and was thus
429 * basically content-free. Newer versions of NetBSD have fixed
430 * this and now use the encoding of __NetBSD_Version__:
436 * r = release ["",A-Z,Z[A-Z] but numeric]
439 if (desc > 100000000U) {
440 uint32_t ver_patch = (desc / 100) % 100;
441 uint32_t ver_rel = (desc / 10000) % 100;
442 uint32_t ver_min = (desc / 1000000) % 100;
443 uint32_t ver_maj = desc / 100000000;
445 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
447 if (ver_rel == 0 && ver_patch != 0) {
448 if (file_printf(ms, ".%u", ver_patch) == -1)
450 } else if (ver_rel != 0) {
451 while (ver_rel > 26) {
452 if (file_printf(ms, "Z") == -1)
456 if (file_printf(ms, "%c", 'A' + ver_rel - 1)
465 do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
469 memcpy(&desc, v, sizeof(desc));
470 desc = elf_getu32(swap, desc);
471 if (file_printf(ms, ", for FreeBSD") == -1)
475 * Contents is __FreeBSD_version, whose relation to OS
476 * versions is defined by a huge table in the Porter's
477 * Handbook. This is the general scheme:
480 * Mmp000 (before 4.10)
481 * Mmi0p0 (before 5.0)
484 * Development branches:
485 * Mmpxxx (before 4.6)
486 * Mmp1xx (before 4.10)
487 * Mmi1xx (before 5.0)
493 * i = minor version increment (491000 -> 4.10)
497 * The first release of FreeBSD to use ELF by default
500 if (desc == 460002) {
501 if (file_printf(ms, " 4.6.2") == -1)
503 } else if (desc < 460100) {
504 if (file_printf(ms, " %d.%d", desc / 100000,
505 desc / 10000 % 10) == -1)
507 if (desc / 1000 % 10 > 0)
508 if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
510 if ((desc % 1000 > 0) || (desc % 100000 == 0))
511 if (file_printf(ms, " (%d)", desc) == -1)
513 } else if (desc < 500000) {
514 if (file_printf(ms, " %d.%d", desc / 100000,
515 desc / 10000 % 10 + desc / 1000 % 10) == -1)
517 if (desc / 100 % 10 > 0) {
518 if (file_printf(ms, " (%d)", desc) == -1)
520 } else if (desc / 10 % 10 > 0) {
521 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
525 if (file_printf(ms, " %d.%d", desc / 100000,
526 desc / 1000 % 100) == -1)
528 if ((desc / 100 % 10 > 0) ||
529 (desc % 100000 / 100 == 0)) {
530 if (file_printf(ms, " (%d)", desc) == -1)
532 } else if (desc / 10 % 10 > 0) {
533 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
542 do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
543 int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz,
544 size_t noff, size_t doff, int *flags)
546 if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "GNU") == 0 &&
547 type == NT_GNU_BUILD_ID && (descsz >= 4 && descsz <= 20)) {
551 *flags |= FLAGS_DID_BUILD_ID;
566 if (file_printf(ms, ", BuildID[%s]=", btype) == -1)
568 memcpy(desc, &nbuf[doff], descsz);
569 for (i = 0; i < descsz; i++)
570 if (file_printf(ms, "%02x", desc[i]) == -1)
574 if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "Go") == 0 &&
575 type == NT_GO_BUILD_ID && descsz < 128) {
577 if (file_printf(ms, ", Go BuildID=%s",
578 file_copystr(buf, sizeof(buf), descsz,
579 RCAST(const char *, &nbuf[doff]))) == -1)
587 do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
588 int swap, uint32_t namesz, uint32_t descsz,
589 size_t noff, size_t doff, int *flags)
591 const char *name = RCAST(const char *, &nbuf[noff]);
593 if (namesz == 5 && strcmp(name, "SuSE") == 0 &&
594 type == NT_GNU_VERSION && descsz == 2) {
595 *flags |= FLAGS_DID_OS_NOTE;
596 if (file_printf(ms, ", for SuSE %d.%d", nbuf[doff],
597 nbuf[doff + 1]) == -1)
602 if (namesz == 4 && strcmp(name, "GNU") == 0 &&
603 type == NT_GNU_VERSION && descsz == 16) {
605 memcpy(desc, &nbuf[doff], sizeof(desc));
607 *flags |= FLAGS_DID_OS_NOTE;
608 if (file_printf(ms, ", for GNU/") == -1)
610 switch (elf_getu32(swap, desc[0])) {
612 if (file_printf(ms, "Linux") == -1)
616 if (file_printf(ms, "Hurd") == -1)
620 if (file_printf(ms, "Solaris") == -1)
623 case GNU_OS_KFREEBSD:
624 if (file_printf(ms, "kFreeBSD") == -1)
628 if (file_printf(ms, "kNetBSD") == -1)
632 if (file_printf(ms, "<unknown>") == -1)
635 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
636 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
641 if (namesz == 7 && strcmp(name, "NetBSD") == 0) {
642 if (type == NT_NETBSD_VERSION && descsz == 4) {
643 *flags |= FLAGS_DID_OS_NOTE;
644 if (do_note_netbsd_version(ms, swap, &nbuf[doff]) == -1)
650 if (namesz == 8 && strcmp(name, "FreeBSD") == 0) {
651 if (type == NT_FREEBSD_VERSION && descsz == 4) {
652 *flags |= FLAGS_DID_OS_NOTE;
653 if (do_note_freebsd_version(ms, swap, &nbuf[doff])
660 if (namesz == 8 && strcmp(name, "OpenBSD") == 0 &&
661 type == NT_OPENBSD_VERSION && descsz == 4) {
662 *flags |= FLAGS_DID_OS_NOTE;
663 if (file_printf(ms, ", for OpenBSD") == -1)
665 /* Content of note is always 0 */
669 if (namesz == 10 && strcmp(name, "DragonFly") == 0 &&
670 type == NT_DRAGONFLY_VERSION && descsz == 4) {
672 *flags |= FLAGS_DID_OS_NOTE;
673 if (file_printf(ms, ", for DragonFly") == -1)
675 memcpy(&desc, &nbuf[doff], sizeof(desc));
676 desc = elf_getu32(swap, desc);
677 if (file_printf(ms, " %d.%d.%d", desc / 100000,
678 desc / 10000 % 10, desc % 10000) == -1)
686 do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
687 int swap, uint32_t namesz, uint32_t descsz,
688 size_t noff, size_t doff, int *flags)
690 const char *name = RCAST(const char *, &nbuf[noff]);
692 if (namesz == 4 && strcmp(name, "PaX") == 0 &&
693 type == NT_NETBSD_PAX && descsz == 4) {
694 static const char *pax[] = {
706 *flags |= FLAGS_DID_NETBSD_PAX;
707 memcpy(&desc, &nbuf[doff], sizeof(desc));
708 desc = elf_getu32(swap, desc);
710 if (desc && file_printf(ms, ", PaX: ") == -1)
713 for (i = 0; i < __arraycount(pax); i++) {
714 if (((1 << CAST(int, i)) & desc) == 0)
716 if (file_printf(ms, "%s%s", did++ ? "," : "",
726 do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
727 int swap, uint32_t namesz, uint32_t descsz,
728 size_t noff, size_t doff, int *flags, size_t size, int clazz)
732 const char *name = RCAST(const char *, &nbuf[noff]);
736 * Sigh. The 2.0.36 kernel in Debian 2.1, at
737 * least, doesn't correctly implement name
738 * sections, in core dumps, as specified by
739 * the "Program Linking" section of "UNIX(R) System
740 * V Release 4 Programmer's Guide: ANSI C and
741 * Programming Support Tools", because my copy
742 * clearly says "The first 'namesz' bytes in 'name'
743 * contain a *null-terminated* [emphasis mine]
744 * character representation of the entry's owner
745 * or originator", but the 2.0.36 kernel code
746 * doesn't include the terminating null in the
749 if ((namesz == 4 && strncmp(name, "CORE", 4) == 0) ||
750 (namesz == 5 && strcmp(name, "CORE") == 0)) {
751 os_style = OS_STYLE_SVR4;
754 if ((namesz == 8 && strcmp(name, "FreeBSD") == 0)) {
755 os_style = OS_STYLE_FREEBSD;
758 if ((namesz >= 11 && strncmp(name, "NetBSD-CORE", 11)
760 os_style = OS_STYLE_NETBSD;
763 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
764 if (file_printf(ms, ", %s-style", os_style_names[os_style])
767 *flags |= FLAGS_DID_CORE_STYLE;
772 case OS_STYLE_NETBSD:
773 if (type == NT_NETBSD_CORE_PROCINFO) {
775 struct NetBSD_elfcore_procinfo pi;
776 memset(&pi, 0, sizeof(pi));
777 memcpy(&pi, nbuf + doff, MIN(descsz, sizeof(pi)));
779 if (file_printf(ms, ", from '%.31s', pid=%u, uid=%u, "
780 "gid=%u, nlwps=%u, lwp=%u (signal %u/code %u)",
781 file_printable(sbuf, sizeof(sbuf),
782 RCAST(char *, pi.cpi_name), sizeof(pi.cpi_name)),
783 elf_getu32(swap, CAST(uint32_t, pi.cpi_pid)),
784 elf_getu32(swap, pi.cpi_euid),
785 elf_getu32(swap, pi.cpi_egid),
786 elf_getu32(swap, pi.cpi_nlwps),
787 elf_getu32(swap, CAST(uint32_t, pi.cpi_siglwp)),
788 elf_getu32(swap, pi.cpi_signo),
789 elf_getu32(swap, pi.cpi_sigcode)) == -1)
792 *flags |= FLAGS_DID_CORE;
797 case OS_STYLE_FREEBSD:
798 if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
799 size_t argoff, pidoff;
801 if (clazz == ELFCLASS32)
804 argoff = 4 + 4 + 8 + 17;
805 if (file_printf(ms, ", from '%.80s'", nbuf + doff +
808 pidoff = argoff + 81 + 2;
809 if (doff + pidoff + 4 <= size) {
810 if (file_printf(ms, ", pid=%u",
811 elf_getu32(swap, *RCAST(uint32_t *, (nbuf +
812 doff + pidoff)))) == -1)
815 *flags |= FLAGS_DID_CORE;
820 if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
824 * Extract the program name. We assume
825 * it to be 16 characters (that's what it
826 * is in SunOS 5.x and Linux).
828 * Unfortunately, it's at a different offset
829 * in various OSes, so try multiple offsets.
830 * If the characters aren't all printable,
833 for (i = 0; i < NOFFSETS; i++) {
834 unsigned char *cname, *cp;
835 size_t reloffset = prpsoffsets(i);
836 size_t noffset = doff + reloffset;
838 for (j = 0; j < 16; j++, noffset++,
841 * Make sure we're not past
842 * the end of the buffer; if
843 * we are, just give up.
849 * Make sure we're not past
850 * the end of the contents;
851 * if we are, this obviously
852 * isn't the right offset.
854 if (reloffset >= descsz)
876 if (!isprint(c) || isquote(c))
885 * Try next offsets, in case this match is
886 * in the middle of a string.
888 for (k = i + 1 ; k < NOFFSETS; k++) {
891 if (prpsoffsets(k) >= prpsoffsets(i))
893 for (no = doff + prpsoffsets(k);
894 no < doff + prpsoffsets(i); no++)
896 && isprint(nbuf[no]);
901 cname = CAST(unsigned char *,
902 &nbuf[doff + prpsoffsets(i)]);
903 for (cp = cname; cp < nbuf + size && *cp
904 && isprint(*cp); cp++)
907 * Linux apparently appends a space at the end
908 * of the command line: remove it.
910 while (cp > cname && isspace(cp[-1]))
912 if (file_printf(ms, ", from '%s'",
913 file_copystr(buf, sizeof(buf),
914 CAST(size_t, cp - cname),
915 CAST(const char *, cname))) == -1)
917 *flags |= FLAGS_DID_CORE;
931 get_offset_from_virtaddr(struct magic_set *ms, int swap, int clazz, int fd,
932 off_t off, int num, off_t fsize, uint64_t virtaddr)
938 * Loop through all the program headers and find the header with
939 * virtual address in which the "virtaddr" belongs to.
941 for ( ; num; num--) {
942 if (pread(fd, xph_addr, xph_sizeof, off) <
943 CAST(ssize_t, xph_sizeof)) {
949 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
950 /* Perhaps warn here */
954 if (virtaddr >= xph_vaddr && virtaddr < xph_vaddr + xph_filesz)
955 return xph_offset + (virtaddr - xph_vaddr);
961 get_string_on_virtaddr(struct magic_set *ms,
962 int swap, int clazz, int fd, off_t ph_off, int ph_num,
963 off_t fsize, uint64_t virtaddr, char *buf, ssize_t buflen)
971 offset = get_offset_from_virtaddr(ms, swap, clazz, fd, ph_off, ph_num,
974 (buflen = pread(fd, buf, CAST(size_t, buflen), offset)) <= 0) {
979 buf[buflen - 1] = '\0';
981 /* We expect only printable characters, so return if buffer contains
982 * non-printable character before the '\0' or just '\0'. */
983 for (bptr = buf; *bptr && isprint(CAST(unsigned char, *bptr)); bptr++)
994 do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type,
995 int swap, uint32_t namesz __attribute__((__unused__)),
996 uint32_t descsz __attribute__((__unused__)),
997 size_t noff __attribute__((__unused__)), size_t doff,
998 int *flags, size_t size __attribute__((__unused__)), int clazz,
999 int fd, off_t ph_off, int ph_num, off_t fsize)
1004 size_t elsize = xauxv_sizeof;
1009 if ((*flags & (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE)) !=
1010 (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE))
1013 switch (*flags & FLAGS_CORE_STYLE) {
1015 if (type != NT_AUXV)
1019 case OS_STYLE_NETBSD:
1020 if (type != NT_NETBSD_CORE_AUXV)
1023 case OS_STYLE_FREEBSD:
1024 if (type != NT_FREEBSD_PROCSTAT_AUXV)
1032 *flags |= FLAGS_DID_AUXV;
1035 for (size_t off = 0; off + elsize <= descsz; off += elsize) {
1036 memcpy(xauxv_addr, &nbuf[doff + off], xauxv_sizeof);
1037 /* Limit processing to 50 vector entries to prevent DoS */
1039 file_error(ms, 0, "Too many ELF Auxv elements");
1043 switch(xauxv_type) {
1044 case AT_LINUX_EXECFN:
1048 case AT_LINUX_PLATFORM:
1062 tag = "effective uid";
1066 tag = "effective gid";
1080 buflen = get_string_on_virtaddr(ms, swap, clazz, fd,
1081 ph_off, ph_num, fsize, xauxv_val, buf, sizeof(buf));
1086 if (file_printf(ms, ", %s: '%s'", tag, buf) == -1)
1089 if (file_printf(ms, ", %s: %d", tag,
1090 CAST(int, xauxv_val)) == -1)
1101 dodynamic(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
1102 int clazz, int swap)
1106 unsigned char *dbuf = CAST(unsigned char *, vbuf);
1108 if (xdh_sizeof + offset > size) {
1110 * We're out of note headers.
1112 return xdh_sizeof + offset;
1115 memcpy(xdh_addr, &dbuf[offset], xdh_sizeof);
1116 offset += xdh_sizeof;
1120 if (xdh_val & DF_1_PIE)
1133 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
1134 int clazz, int swap, size_t align, int *flags, uint16_t *notecount,
1135 int fd, off_t ph_off, int ph_num, off_t fsize)
1140 uint32_t namesz, descsz;
1142 unsigned char *nbuf = CAST(unsigned char *, vbuf);
1144 if (*notecount == 0)
1148 if (xnh_sizeof + offset > size) {
1150 * We're out of note headers.
1152 return xnh_sizeof + offset;
1155 memset(&nh32, 0, sizeof(nh32));
1156 memset(&nh64, 0, sizeof(nh64));
1158 memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
1159 offset += xnh_sizeof;
1161 namesz = xnh_namesz;
1162 descsz = xnh_descsz;
1164 if ((namesz == 0) && (descsz == 0)) {
1166 * We're out of note headers.
1168 return (offset >= size) ? offset : size;
1171 if (namesz & 0x80000000) {
1172 if (file_printf(ms, ", bad note name size %#lx",
1173 CAST(unsigned long, namesz)) == -1)
1178 if (descsz & 0x80000000) {
1179 if (file_printf(ms, ", bad note description size %#lx",
1180 CAST(unsigned long, descsz)) == -1)
1186 doff = ELF_ALIGN(offset + namesz);
1188 if (offset + namesz > size) {
1190 * We're past the end of the buffer.
1195 offset = ELF_ALIGN(doff + descsz);
1196 if (doff + descsz > size) {
1198 * We're past the end of the buffer.
1200 return (offset >= size) ? offset : size;
1204 if ((*flags & FLAGS_DID_OS_NOTE) == 0) {
1205 if (do_os_note(ms, nbuf, xnh_type, swap,
1206 namesz, descsz, noff, doff, flags))
1210 if ((*flags & FLAGS_DID_BUILD_ID) == 0) {
1211 if (do_bid_note(ms, nbuf, xnh_type, swap,
1212 namesz, descsz, noff, doff, flags))
1216 if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) {
1217 if (do_pax_note(ms, nbuf, xnh_type, swap,
1218 namesz, descsz, noff, doff, flags))
1222 if ((*flags & FLAGS_DID_CORE) == 0) {
1223 if (do_core_note(ms, nbuf, xnh_type, swap,
1224 namesz, descsz, noff, doff, flags, size, clazz))
1228 if ((*flags & FLAGS_DID_AUXV) == 0) {
1229 if (do_auxv_note(ms, nbuf, xnh_type, swap,
1230 namesz, descsz, noff, doff, flags, size, clazz,
1231 fd, ph_off, ph_num, fsize))
1235 if (namesz == 7 && strcmp(RCAST(char *, &nbuf[noff]), "NetBSD") == 0) {
1237 const char *str, *tag;
1241 case NT_NETBSD_VERSION:
1243 case NT_NETBSD_MARCH:
1244 flag = FLAGS_DID_NETBSD_MARCH;
1245 tag = "compiled for";
1247 case NT_NETBSD_CMODEL:
1248 flag = FLAGS_DID_NETBSD_CMODEL;
1249 tag = "compiler model";
1251 case NT_NETBSD_EMULATION:
1252 flag = FLAGS_DID_NETBSD_EMULATION;
1256 if (*flags & FLAGS_DID_NETBSD_UNKNOWN)
1258 *flags |= FLAGS_DID_NETBSD_UNKNOWN;
1259 if (file_printf(ms, ", note=%u", xnh_type) == -1)
1266 str = RCAST(const char *, &nbuf[doff]);
1267 descw = CAST(int, descsz);
1269 file_printf(ms, ", %s: %s", tag,
1270 file_copystr(buf, sizeof(buf), descw, str));
1277 /* SunOS 5.x hardware capability descriptions */
1278 typedef struct cap_desc {
1280 const char *cd_name;
1283 static const cap_desc_t cap_desc_sparc[] = {
1284 { AV_SPARC_MUL32, "MUL32" },
1285 { AV_SPARC_DIV32, "DIV32" },
1286 { AV_SPARC_FSMULD, "FSMULD" },
1287 { AV_SPARC_V8PLUS, "V8PLUS" },
1288 { AV_SPARC_POPC, "POPC" },
1289 { AV_SPARC_VIS, "VIS" },
1290 { AV_SPARC_VIS2, "VIS2" },
1291 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
1292 { AV_SPARC_FMAF, "FMAF" },
1293 { AV_SPARC_FJFMAU, "FJFMAU" },
1294 { AV_SPARC_IMA, "IMA" },
1298 static const cap_desc_t cap_desc_386[] = {
1299 { AV_386_FPU, "FPU" },
1300 { AV_386_TSC, "TSC" },
1301 { AV_386_CX8, "CX8" },
1302 { AV_386_SEP, "SEP" },
1303 { AV_386_AMD_SYSC, "AMD_SYSC" },
1304 { AV_386_CMOV, "CMOV" },
1305 { AV_386_MMX, "MMX" },
1306 { AV_386_AMD_MMX, "AMD_MMX" },
1307 { AV_386_AMD_3DNow, "AMD_3DNow" },
1308 { AV_386_AMD_3DNowx, "AMD_3DNowx" },
1309 { AV_386_FXSR, "FXSR" },
1310 { AV_386_SSE, "SSE" },
1311 { AV_386_SSE2, "SSE2" },
1312 { AV_386_PAUSE, "PAUSE" },
1313 { AV_386_SSE3, "SSE3" },
1314 { AV_386_MON, "MON" },
1315 { AV_386_CX16, "CX16" },
1316 { AV_386_AHF, "AHF" },
1317 { AV_386_TSCP, "TSCP" },
1318 { AV_386_AMD_SSE4A, "AMD_SSE4A" },
1319 { AV_386_POPCNT, "POPCNT" },
1320 { AV_386_AMD_LZCNT, "AMD_LZCNT" },
1321 { AV_386_SSSE3, "SSSE3" },
1322 { AV_386_SSE4_1, "SSE4.1" },
1323 { AV_386_SSE4_2, "SSE4.2" },
1328 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
1329 size_t size, off_t fsize, int mach, int strtab, int *flags,
1330 uint16_t *notecount)
1334 int stripped = 1, has_debug_info = 0;
1337 off_t noff, coff, name_off;
1338 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilities */
1339 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilities */
1343 if (ms->flags & MAGIC_MIME)
1347 if (file_printf(ms, ", no section header") == -1)
1351 if (size != xsh_sizeof) {
1352 if (file_printf(ms, ", corrupted section header size") == -1)
1357 /* Read offset of name section to be able to read section names later */
1358 if (pread(fd, xsh_addr, xsh_sizeof, CAST(off_t, (off + size * strtab)))
1359 < CAST(ssize_t, xsh_sizeof)) {
1360 if (file_printf(ms, ", missing section headers") == -1)
1364 name_off = xsh_offset;
1366 if (fsize != SIZE_UNKNOWN && fsize < name_off) {
1367 if (file_printf(ms, ", too large section header offset %jd",
1368 (intmax_t)name_off) == -1)
1373 for ( ; num; num--) {
1374 /* Read the name of this section. */
1375 if ((namesize = pread(fd, name, sizeof(name) - 1,
1376 name_off + xsh_name)) == -1) {
1380 name[namesize] = '\0';
1381 if (strcmp(name, ".debug_info") == 0) {
1386 if (pread(fd, xsh_addr, xsh_sizeof, off) <
1387 CAST(ssize_t, xsh_sizeof)) {
1393 /* Things we can determine before we seek */
1402 if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
1403 /* Perhaps warn here */
1410 /* Things we can determine when we seek */
1413 if (CAST(uintmax_t, (xsh_size + xsh_offset)) >
1414 CAST(uintmax_t, fsize)) {
1416 ", note offset/size %#" INTMAX_T_FORMAT
1417 "x+%#" INTMAX_T_FORMAT "x exceeds"
1418 " file size %#" INTMAX_T_FORMAT "x",
1419 CAST(uintmax_t, xsh_offset),
1420 CAST(uintmax_t, xsh_size),
1421 CAST(uintmax_t, fsize)) == -1)
1425 if ((nbuf = malloc(xsh_size)) == NULL) {
1426 file_error(ms, errno, "Cannot allocate memory"
1430 if (pread(fd, nbuf, xsh_size, xsh_offset) <
1431 CAST(ssize_t, xsh_size)) {
1439 if (noff >= CAST(off_t, xsh_size))
1441 noff = donote(ms, nbuf, CAST(size_t, noff),
1442 xsh_size, clazz, swap, 4, flags, notecount,
1463 if (lseek(fd, xsh_offset, SEEK_SET)
1464 == CAST(off_t, -1)) {
1472 char cbuf[/*CONSTCOND*/
1473 MAX(sizeof(cap32), sizeof(cap64))];
1474 if ((coff += xcap_sizeof) >
1475 CAST(off_t, xsh_size))
1477 if (read(fd, cbuf, CAST(size_t, xcap_sizeof)) !=
1478 CAST(ssize_t, xcap_sizeof)) {
1482 if (cbuf[0] == 'A') {
1486 memcpy(&len, p, sizeof(len));
1488 len = getu32(swap, len);
1489 if (memcmp("gnu", p, 3) != 0) {
1491 ", unknown capability %.3s", p)
1498 memcpy(&len, p, sizeof(len));
1500 len = getu32(swap, len);
1502 if (file_printf(ms, ", unknown gnu"
1503 " capability tag %d", tag)
1512 memcpy(xcap_addr, cbuf, xcap_sizeof);
1517 cap_hw1 |= xcap_val;
1520 cap_sf1 |= xcap_val;
1524 ", with unknown capability "
1525 "%#" INT64_T_FORMAT "x = %#"
1527 CAST(unsigned long long, xcap_tag),
1528 CAST(unsigned long long, xcap_val))
1543 if (has_debug_info) {
1544 if (file_printf(ms, ", with debug_info") == -1)
1547 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1550 const cap_desc_t *cdp;
1553 case EM_SPARC32PLUS:
1555 cdp = cap_desc_sparc;
1566 if (file_printf(ms, ", uses") == -1)
1569 while (cdp->cd_name) {
1570 if (cap_hw1 & cdp->cd_mask) {
1572 " %s", cdp->cd_name) == -1)
1574 cap_hw1 &= ~cdp->cd_mask;
1580 " unknown hardware capability %#"
1582 CAST(unsigned long long, cap_hw1)) == -1)
1586 " hardware capability %#" INT64_T_FORMAT "x",
1587 CAST(unsigned long long, cap_hw1)) == -1)
1592 if (cap_sf1 & SF1_SUNW_FPUSED) {
1594 (cap_sf1 & SF1_SUNW_FPKNWN)
1595 ? ", uses frame pointer"
1596 : ", not known to use frame pointer") == -1)
1599 cap_sf1 &= ~SF1_SUNW_MASK;
1602 ", with unknown software capability %#"
1604 CAST(unsigned long long, cap_sf1)) == -1)
1611 * Look through the program headers of an executable image, searching
1612 * for a PT_INTERP section; if one is found, it's dynamically linked,
1613 * otherwise it's statically linked.
1616 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1617 int num, size_t size, off_t fsize, int sh_num, int *flags,
1618 uint16_t *notecount)
1622 const char *linking_style = "statically";
1623 unsigned char nbuf[BUFSIZ];
1625 char interp[BUFSIZ];
1627 size_t offset, align, len;
1630 if (file_printf(ms, ", no program header") == -1)
1634 if (size != xph_sizeof) {
1635 if (file_printf(ms, ", corrupted program header size") == -1)
1641 for ( ; num; num--) {
1643 if (pread(fd, xph_addr, xph_sizeof, off) <
1644 CAST(ssize_t, xph_sizeof)) {
1653 /* Things we can determine before we seek */
1657 linking_style = "dynamically";
1660 if (sh_num) /* Did this through section headers */
1662 if (((align = xph_align) & 0x80000000UL) != 0 ||
1665 ", invalid note alignment %#lx",
1666 CAST(unsigned long, align)) == -1)
1676 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
1677 /* Maybe warn here? */
1684 len = xph_filesz < sizeof(nbuf) ? xph_filesz
1686 bufsize = pread(fd, nbuf, len, xph_offset);
1687 if (bufsize == -1) {
1694 /* Things we can determine when we seek */
1698 // Let DF_1 determine if we are PIE or not.
1701 if (offset >= CAST(size_t, bufsize))
1703 offset = dodynamic(ms, nbuf, offset,
1704 CAST(size_t, bufsize), clazz, swap);
1708 if (ms->flags & MAGIC_MIME)
1713 if (ms->flags & MAGIC_MIME)
1715 if (bufsize && nbuf[0]) {
1716 nbuf[bufsize - 1] = '\0';
1717 memcpy(interp, nbuf, CAST(size_t, bufsize));
1719 strlcpy(interp, "*empty*", sizeof(interp));
1722 if (ms->flags & MAGIC_MIME)
1725 * This is a PT_NOTE section; loop through all the notes
1730 if (offset >= CAST(size_t, bufsize))
1732 offset = donote(ms, nbuf, offset,
1733 CAST(size_t, bufsize), clazz, swap, align,
1734 flags, notecount, fd, 0, 0, 0);
1740 if (ms->flags & MAGIC_MIME)
1745 if (ms->flags & MAGIC_MIME)
1747 if (file_printf(ms, ", %s linked", linking_style)
1751 if (file_printf(ms, ", interpreter %s",
1752 file_printable(ibuf, sizeof(ibuf), interp, sizeof(interp)))
1760 file_tryelf(struct magic_set *ms, const struct buffer *b)
1763 const unsigned char *buf = CAST(const unsigned char *, b->fbuf);
1764 size_t nbytes = b->flen;
1767 char c[sizeof(int32_t)];
1772 const struct stat *stp;
1775 Elf32_Ehdr elf32hdr;
1776 Elf64_Ehdr elf64hdr;
1777 uint16_t type, phnum, shnum, notecount;
1779 if (ms->flags & (MAGIC_APPLE|MAGIC_EXTENSION))
1782 * ELF executables have multiple section headers in arbitrary
1783 * file locations and thus file(1) cannot determine it from easily.
1784 * Instead we traverse thru all section headers until a symbol table
1785 * one is found or else the binary is stripped.
1786 * Return immediately if it's not ELF (so we avoid pipe2file unless
1789 if (buf[EI_MAG0] != ELFMAG0
1790 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1791 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1795 * If we cannot seek, it must be a pipe, socket or fifo.
1797 if((lseek(fd, CAST(off_t, 0), SEEK_SET) == CAST(off_t, -1))
1798 && (errno == ESPIPE))
1799 fd = file_pipe2file(ms, fd, buf, nbytes);
1808 * b->st.st_size != 0 if previous fstat() succeeded,
1809 * which is likely, we can avoid extra stat() call.
1811 if (b->st.st_size == 0) {
1813 if (fstat(fd, &st) == -1) {
1818 if (S_ISREG(stp->st_mode) || stp->st_size != 0)
1819 fsize = stp->st_size;
1821 fsize = SIZE_UNKNOWN;
1823 clazz = buf[EI_CLASS];
1828 #define elf_getu(a, b) elf_getu32(a, b)
1830 #define elfhdr elf32hdr
1831 #include "elfclass.h"
1834 #define elf_getu(a, b) elf_getu64(a, b)
1836 #define elfhdr elf64hdr
1837 #include "elfclass.h"
1839 if (file_printf(ms, ", unknown class %d", clazz) == -1)