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
40 FILE_RCSID("@(#)$Id: readelf.c,v 1.47 2005/06/25 15:52:14 christos Exp $")
44 private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t);
46 private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t);
47 private int doshn(struct magic_set *, int, int, int, off_t, int, size_t);
48 private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
51 #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
53 private uint16_t getu16(int, uint16_t);
54 private uint32_t getu32(int, uint32_t);
55 private uint64_t getu64(int, uint64_t);
58 getu16(int swap, uint16_t value)
68 retval.c[0] = tmpval.c[1];
69 retval.c[1] = tmpval.c[0];
77 getu32(int swap, uint32_t value)
87 retval.c[0] = tmpval.c[3];
88 retval.c[1] = tmpval.c[2];
89 retval.c[2] = tmpval.c[1];
90 retval.c[3] = tmpval.c[0];
98 getu64(int swap, uint64_t value)
108 retval.c[0] = tmpval.c[7];
109 retval.c[1] = tmpval.c[6];
110 retval.c[2] = tmpval.c[5];
111 retval.c[3] = tmpval.c[4];
112 retval.c[4] = tmpval.c[3];
113 retval.c[5] = tmpval.c[2];
114 retval.c[6] = tmpval.c[1];
115 retval.c[7] = tmpval.c[0];
122 #define sh_addr (class == ELFCLASS32 \
125 #define sh_size (class == ELFCLASS32 \
128 #define shs_type (class == ELFCLASS32 \
129 ? getu32(swap, sh32.sh_type) \
130 : getu32(swap, sh64.sh_type))
131 #define ph_addr (class == ELFCLASS32 \
134 #define ph_size (class == ELFCLASS32 \
137 #define ph_type (class == ELFCLASS32 \
138 ? getu32(swap, ph32.p_type) \
139 : getu32(swap, ph64.p_type))
140 #define ph_offset (class == ELFCLASS32 \
141 ? getu32(swap, ph32.p_offset) \
142 : getu64(swap, ph64.p_offset))
143 #define ph_align (size_t)((class == ELFCLASS32 \
144 ? (off_t) (ph32.p_align ? \
145 getu32(swap, ph32.p_align) : 4) \
146 : (off_t) (ph64.p_align ? \
147 getu64(swap, ph64.p_align) : 4)))
148 #define ph_filesz (size_t)((class == ELFCLASS32 \
149 ? getu32(swap, ph32.p_filesz) \
150 : getu64(swap, ph64.p_filesz)))
151 #define ph_memsz (size_t)((class == ELFCLASS32 \
152 ? getu32(swap, ph32.p_memsz) \
153 : getu64(swap, ph64.p_memsz)))
154 #define nh_size (class == ELFCLASS32 \
157 #define nh_type (class == ELFCLASS32 \
158 ? getu32(swap, nh32.n_type) \
159 : getu32(swap, nh64.n_type))
160 #define nh_namesz (class == ELFCLASS32 \
161 ? getu32(swap, nh32.n_namesz) \
162 : getu32(swap, nh64.n_namesz))
163 #define nh_descsz (class == ELFCLASS32 \
164 ? getu32(swap, nh32.n_descsz) \
165 : getu32(swap, nh64.n_descsz))
166 #define prpsoffsets(i) (class == ELFCLASS32 \
171 size_t prpsoffsets32[] = {
173 28, /* Linux 2.0.36 */
174 32, /* Linux (I forget which kernel version) */
178 size_t prpsoffsets64[] = {
179 40, /* Linux (tested on core from 2.4.x) */
180 120, /* SunOS 5.x, 64-bit */
183 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
184 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
186 #define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
189 * Look through the program headers of an executable image, searching
190 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
191 * "FreeBSD"; if one is found, try looking in various places in its
192 * contents for a 16-character string containing only printable
193 * characters - if found, that string should be the name of the program
194 * that dropped core. Note: right after that 16-character string is,
195 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
196 * Linux, a longer string (80 characters, in 5.x, probably other
197 * SVR4-flavored systems, and Linux) containing the start of the
198 * command line for that program.
200 * The signal number probably appears in a section of type NT_PRSTATUS,
201 * but that's also rather OS-dependent, in ways that are harder to
202 * dissect with heuristics, so I'm not bothering with the signal number.
203 * (I suppose the signal number could be of interest in situations where
204 * you don't have the binary of the program that dropped core; if you
205 * *do* have that binary, the debugger will probably tell you what
209 #define OS_STYLE_SVR4 0
210 #define OS_STYLE_FREEBSD 1
211 #define OS_STYLE_NETBSD 2
213 private const char *os_style_names[] = {
220 dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
221 int num, size_t size)
226 unsigned char nbuf[BUFSIZ];
229 if (size != ph_size) {
230 if (file_printf(ms, ", corrupted program header size") == -1)
235 * Loop through all the program headers.
237 for ( ; num; num--) {
238 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
242 if (read(fd, ph_addr, ph_size) == -1) {
247 if (ph_type != PT_NOTE)
251 * This is a PT_NOTE section; loop through all the notes
254 if (lseek(fd, (off_t) ph_offset, SEEK_SET) == (off_t)-1) {
258 bufsize = read(fd, nbuf,
259 ((ph_filesz < sizeof(nbuf)) ? ph_filesz : sizeof(nbuf)));
266 if (offset >= (size_t)bufsize)
268 offset = donote(ms, nbuf, offset, (size_t)bufsize,
280 donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
281 int class, int swap, size_t align)
289 uint32_t namesz, descsz;
291 if (class == ELFCLASS32)
292 memcpy(&nh32, &nbuf[offset], sizeof(nh32));
294 memcpy(&nh64, &nbuf[offset], sizeof(nh64));
299 if ((namesz == 0) && (descsz == 0)) {
301 * We're out of note headers.
306 if (namesz & 0x80000000) {
307 (void)file_printf(ms, ", bad note name size 0x%lx",
308 (unsigned long)namesz);
312 if (descsz & 0x80000000) {
313 (void)file_printf(ms, ", bad note description size 0x%lx",
314 (unsigned long)descsz);
320 doff = ELF_ALIGN(offset + namesz);
322 if (offset + namesz > size) {
324 * We're past the end of the buffer.
329 offset = ELF_ALIGN(doff + descsz);
330 if (doff + descsz > size) {
334 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
335 nh_type == NT_GNU_VERSION && descsz == 16) {
337 (void)memcpy(desc, &nbuf[doff], sizeof(desc));
339 if (file_printf(ms, ", for GNU/") == -1)
341 switch (getu32(swap, desc[0])) {
343 if (file_printf(ms, "Linux") == -1)
347 if (file_printf(ms, "Hurd") == -1)
351 if (file_printf(ms, "Solaris") == -1)
355 if (file_printf(ms, "<unknown>") == -1)
358 if (file_printf(ms, " %d.%d.%d", getu32(swap, desc[1]),
359 getu32(swap, desc[2]), getu32(swap, desc[3])) == -1)
364 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
365 nh_type == NT_NETBSD_VERSION && descsz == 4) {
367 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
368 desc = getu32(swap, desc);
370 if (file_printf(ms, ", for NetBSD") == -1)
373 * The version number used to be stuck as 199905, and was thus
374 * basically content-free. Newer versions of NetBSD have fixed
375 * this and now use the encoding of __NetBSD_Version__:
381 * r = release ["",A-Z,Z[A-Z] but numeric]
384 if (desc > 100000000U) {
385 u_int ver_patch = (desc / 100) % 100;
386 u_int ver_rel = (desc / 10000) % 100;
387 u_int ver_min = (desc / 1000000) % 100;
388 u_int ver_maj = desc / 100000000;
390 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
392 if (ver_rel == 0 && ver_patch != 0) {
393 if (file_printf(ms, ".%u", ver_patch) == -1)
395 } else if (ver_rel != 0) {
396 while (ver_rel > 26) {
397 file_printf(ms, "Z");
400 file_printf(ms, "%c", 'A' + ver_rel - 1);
406 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
407 nh_type == NT_FREEBSD_VERSION && descsz == 4) {
409 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
410 desc = getu32(swap, desc);
411 if (file_printf(ms, ", for FreeBSD") == -1)
415 * Contents is __FreeBSD_version, whose relation to OS
416 * versions is defined by a huge table in the Porter's
417 * Handbook. This is the general scheme:
420 * Mmp000 (before 4.10)
421 * Mmi0p0 (before 5.0)
424 * Development branches:
425 * Mmpxxx (before 4.6)
426 * Mmp1xx (before 4.10)
427 * Mmi1xx (before 5.0)
433 * i = minor version increment (491000 -> 4.10)
437 * The first release of FreeBSD to use ELF by default
440 if (desc == 460002) {
441 if (file_printf(ms, " 4.6.2") == -1)
443 } else if (desc < 460100) {
444 if (file_printf(ms, " %d.%d", desc / 100000,
445 desc / 10000 % 10) == -1)
447 if (desc / 1000 % 10 > 0)
448 if (file_printf(ms, ".%d", desc / 1000 % 10)
451 if ((desc % 1000 > 0) || (desc % 100000 == 0))
452 if (file_printf(ms, " (%d)", desc) == -1)
454 } else if (desc < 500000) {
455 if (file_printf(ms, " %d.%d", desc / 100000,
456 desc / 10000 % 10 + desc / 1000 % 10) == -1)
458 if (desc / 100 % 10 > 0) {
459 if (file_printf(ms, " (%d)", desc) == -1)
461 } else if (desc / 10 % 10 > 0) {
462 if (file_printf(ms, ".%d", desc / 10 % 10)
467 if (file_printf(ms, " %d.%d", desc / 100000,
468 desc / 1000 % 100) == -1)
470 if ((desc / 100 % 10 > 0) ||
471 (desc % 100000 / 100 == 0)) {
472 if (file_printf(ms, " (%d)", desc) == -1)
474 } else if (desc / 10 % 10 > 0) {
475 if (file_printf(ms, ".%d", desc / 10 % 10)
483 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
484 nh_type == NT_OPENBSD_VERSION && descsz == 4) {
485 if (file_printf(ms, ", for OpenBSD") == -1)
487 /* Content of note is always 0 */
491 if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
492 nh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
493 if (file_printf(ms, ", for DragonFly") == -1)
496 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
497 desc = getu32(swap, desc);
498 if (file_printf(ms, " %d.%d.%d", desc / 100000,
499 desc / 10000 % 10, desc % 10000) == -1)
505 * Sigh. The 2.0.36 kernel in Debian 2.1, at
506 * least, doesn't correctly implement name
507 * sections, in core dumps, as specified by
508 * the "Program Linking" section of "UNIX(R) System
509 * V Release 4 Programmer's Guide: ANSI C and
510 * Programming Support Tools", because my copy
511 * clearly says "The first 'namesz' bytes in 'name'
512 * contain a *null-terminated* [emphasis mine]
513 * character representation of the entry's owner
514 * or originator", but the 2.0.36 kernel code
515 * doesn't include the terminating null in the
518 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
519 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
520 os_style = OS_STYLE_SVR4;
523 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
524 os_style = OS_STYLE_FREEBSD;
527 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
529 os_style = OS_STYLE_NETBSD;
534 if (file_printf(ms, ", %s-style", os_style_names[os_style]) == -1)
537 if (os_style == OS_STYLE_NETBSD && nh_type == NT_NETBSD_CORE_PROCINFO) {
540 * Extract the program name. It is at
541 * offset 0x7c, and is up to 32-bytes,
542 * including the terminating NUL.
544 if (file_printf(ms, ", from '%.31s'", &nbuf[doff + 0x7c]) == -1)
548 * Extract the signal number. It is at
551 memcpy(&signo, &nbuf[doff + 0x08],
553 if (file_printf(ms, " (signal %u)", getu32(swap, signo)) == -1)
556 } else if (os_style != OS_STYLE_NETBSD && nh_type == NT_PRPSINFO) {
560 * Extract the program name. We assume
561 * it to be 16 characters (that's what it
562 * is in SunOS 5.x and Linux).
564 * Unfortunately, it's at a different offset
565 * in varous OSes, so try multiple offsets.
566 * If the characters aren't all printable,
569 for (i = 0; i < NOFFSETS; i++) {
570 size_t reloffset = prpsoffsets(i);
571 size_t noffset = doff + reloffset;
572 for (j = 0; j < 16; j++, noffset++, reloffset++) {
574 * Make sure we're not past
575 * the end of the buffer; if
576 * we are, just give up.
582 * Make sure we're not past
583 * the end of the contents;
584 * if we are, this obviously
585 * isn't the right offset.
587 if (reloffset >= descsz)
609 #define isquote(c) (strchr("'\"`", (c)) != NULL)
610 if (!isprint(c) || isquote(c))
618 if (file_printf(ms, ", from '%.16s'",
619 &nbuf[doff + prpsoffsets(i)]) == -1)
633 doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
639 if (size != sh_size) {
640 if (file_printf(ms, ", corrupted section header size") == -1)
645 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
650 for ( ; num; num--) {
651 if (read(fd, sh_addr, sh_size) == -1) {
655 if (shs_type == SHT_SYMTAB /* || shs_type == SHT_DYNSYM */) {
656 if (file_printf(ms, ", not stripped") == -1)
661 if (file_printf(ms, ", stripped") == -1)
667 * Look through the program headers of an executable image, searching
668 * for a PT_INTERP section; if one is found, it's dynamically linked,
669 * otherwise it's statically linked.
672 dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
673 int num, size_t size)
677 const char *linking_style = "statically";
678 const char *shared_libraries = "";
679 unsigned char nbuf[BUFSIZ];
681 size_t offset, align;
684 if (size != ph_size) {
685 if (file_printf(ms, ", corrupted program header size") == -1)
689 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
694 for ( ; num; num--) {
695 if (read(fd, ph_addr, ph_size) == -1) {
699 if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
706 linking_style = "dynamically";
709 shared_libraries = " (uses shared libs)";
712 if ((align = ph_align) & 0x80000000) {
714 ", invalid note alignment 0x%lx",
715 (unsigned long)align) == -1)
720 * This is a PT_NOTE section; loop through all the notes
723 if (lseek(fd, (off_t) ph_offset, SEEK_SET)
728 bufsize = read(fd, nbuf, ((ph_filesz < sizeof(nbuf)) ?
729 ph_filesz : sizeof(nbuf)));
736 if (offset >= (size_t)bufsize)
738 offset = donote(ms, nbuf, offset,
739 (size_t)bufsize, class, swap, align);
743 if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
750 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
758 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
763 char c[sizeof (int32_t)];
769 * If we cannot seek, it must be a pipe, socket or fifo.
771 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
772 fd = file_pipe2file(ms, fd, buf, nbytes);
775 * ELF executables have multiple section headers in arbitrary
776 * file locations and thus file(1) cannot determine it from easily.
777 * Instead we traverse thru all section headers until a symbol table
778 * one is found or else the binary is stripped.
780 if (buf[EI_MAG0] != ELFMAG0
781 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
782 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
788 if (class == ELFCLASS32) {
790 if (nbytes <= sizeof (Elf32_Ehdr))
795 (void) memcpy(&elfhdr, buf, sizeof elfhdr);
796 swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
798 if (getu16(swap, elfhdr.e_type) == ET_CORE) {
800 if (dophn_core(ms, class, swap, fd,
801 (off_t)getu32(swap, elfhdr.e_phoff),
802 getu16(swap, elfhdr.e_phnum),
803 (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
809 if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
810 if (dophn_exec(ms, class, swap,
811 fd, (off_t)getu32(swap, elfhdr.e_phoff),
812 getu16(swap, elfhdr.e_phnum),
813 (size_t)getu16(swap, elfhdr.e_phentsize))
817 if (doshn(ms, class, swap, fd,
818 (off_t)getu32(swap, elfhdr.e_shoff),
819 getu16(swap, elfhdr.e_shnum),
820 (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)
826 if (class == ELFCLASS64) {
828 if (nbytes <= sizeof (Elf64_Ehdr))
833 (void) memcpy(&elfhdr, buf, sizeof elfhdr);
834 swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
836 if (getu16(swap, elfhdr.e_type) == ET_CORE) {
838 if (dophn_core(ms, class, swap, fd,
839 #ifdef USE_ARRAY_FOR_64BIT_TYPES
840 (off_t)getu32(swap, elfhdr.e_phoff[1]),
842 (off_t)getu64(swap, elfhdr.e_phoff),
844 getu16(swap, elfhdr.e_phnum),
845 (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
851 if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
852 if (dophn_exec(ms, class, swap, fd,
853 #ifdef USE_ARRAY_FOR_64BIT_TYPES
854 (off_t)getu32(swap, elfhdr.e_phoff[1]),
856 (off_t)getu64(swap, elfhdr.e_phoff),
858 getu16(swap, elfhdr.e_phnum),
859 (size_t)getu16(swap, elfhdr.e_phentsize))
863 if (doshn(ms, class, swap, fd,
864 #ifdef USE_ARRAY_FOR_64BIT_TYPES
865 (off_t)getu32(swap, elfhdr.e_shoff[1]),
867 (off_t)getu64(swap, elfhdr.e_shoff),
869 getu16(swap, elfhdr.e_shnum),
870 (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)