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.45 2004/11/24 17:38:24 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 120, /* SunOS 5.x, 64-bit */
182 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
183 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
185 #define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
188 * Look through the program headers of an executable image, searching
189 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
190 * "FreeBSD"; if one is found, try looking in various places in its
191 * contents for a 16-character string containing only printable
192 * characters - if found, that string should be the name of the program
193 * that dropped core. Note: right after that 16-character string is,
194 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
195 * Linux, a longer string (80 characters, in 5.x, probably other
196 * SVR4-flavored systems, and Linux) containing the start of the
197 * command line for that program.
199 * The signal number probably appears in a section of type NT_PRSTATUS,
200 * but that's also rather OS-dependent, in ways that are harder to
201 * dissect with heuristics, so I'm not bothering with the signal number.
202 * (I suppose the signal number could be of interest in situations where
203 * you don't have the binary of the program that dropped core; if you
204 * *do* have that binary, the debugger will probably tell you what
208 #define OS_STYLE_SVR4 0
209 #define OS_STYLE_FREEBSD 1
210 #define OS_STYLE_NETBSD 2
212 private const char *os_style_names[] = {
219 dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
220 int num, size_t size)
225 unsigned char nbuf[BUFSIZ];
228 if (size != ph_size) {
229 if (file_printf(ms, ", corrupted program header size") == -1)
234 * Loop through all the program headers.
236 for ( ; num; num--) {
237 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
241 if (read(fd, ph_addr, ph_size) == -1) {
246 if (ph_type != PT_NOTE)
250 * This is a PT_NOTE section; loop through all the notes
253 if (lseek(fd, (off_t) ph_offset, SEEK_SET) == (off_t)-1) {
257 bufsize = read(fd, nbuf,
258 ((ph_filesz < sizeof(nbuf)) ? ph_filesz : sizeof(nbuf)));
265 if (offset >= (size_t)bufsize)
267 offset = donote(ms, nbuf, offset, (size_t)bufsize,
279 donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
280 int class, int swap, size_t align)
288 uint32_t namesz, descsz;
290 if (class == ELFCLASS32)
291 memcpy(&nh32, &nbuf[offset], sizeof(nh32));
293 memcpy(&nh64, &nbuf[offset], sizeof(nh64));
298 if ((namesz == 0) && (descsz == 0)) {
300 * We're out of note headers.
305 if (namesz & 0x80000000) {
306 (void)file_printf(ms, ", bad note name size 0x%lx",
307 (unsigned long)namesz);
311 if (descsz & 0x80000000) {
312 (void)file_printf(ms, ", bad note description size 0x%lx",
313 (unsigned long)descsz);
319 doff = ELF_ALIGN(offset + namesz);
321 if (offset + namesz > size) {
323 * We're past the end of the buffer.
328 offset = ELF_ALIGN(doff + descsz);
329 if (doff + descsz > size) {
333 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
334 nh_type == NT_GNU_VERSION && descsz == 16) {
336 (void)memcpy(desc, &nbuf[doff], sizeof(desc));
338 if (file_printf(ms, ", for GNU/") == -1)
340 switch (getu32(swap, desc[0])) {
342 if (file_printf(ms, "Linux") == -1)
346 if (file_printf(ms, "Hurd") == -1)
350 if (file_printf(ms, "Solaris") == -1)
354 if (file_printf(ms, "<unknown>") == -1)
357 if (file_printf(ms, " %d.%d.%d", getu32(swap, desc[1]),
358 getu32(swap, desc[2]), getu32(swap, desc[3])) == -1)
363 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
364 nh_type == NT_NETBSD_VERSION && descsz == 4) {
366 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
367 desc = getu32(swap, desc);
369 if (file_printf(ms, ", for NetBSD") == -1)
372 * The version number used to be stuck as 199905, and was thus
373 * basically content-free. Newer versions of NetBSD have fixed
374 * this and now use the encoding of __NetBSD_Version__:
380 * r = release ["",A-Z,Z[A-Z] but numeric]
383 if (desc > 100000000U) {
384 u_int ver_patch = (desc / 100) % 100;
385 u_int ver_rel = (desc / 10000) % 100;
386 u_int ver_min = (desc / 1000000) % 100;
387 u_int ver_maj = desc / 100000000;
389 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
391 if (ver_rel == 0 && ver_patch != 0) {
392 if (file_printf(ms, ".%u", ver_patch) == -1)
394 } else if (ver_rel != 0) {
395 while (ver_rel > 26) {
396 file_printf(ms, "Z");
399 file_printf(ms, "%c", 'A' + ver_rel - 1);
405 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
406 nh_type == NT_FREEBSD_VERSION && descsz == 4) {
408 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
409 desc = getu32(swap, desc);
410 if (file_printf(ms, ", for FreeBSD") == -1)
414 * Contents is __FreeBSD_version, whose relation to OS
415 * versions is defined by a huge table in the Porter's
416 * Handbook. This is the general scheme:
419 * Mmp000 (before 4.10)
420 * Mmi0p0 (before 5.0)
423 * Development branches:
424 * Mmpxxx (before 4.6)
425 * Mmp1xx (before 4.10)
426 * Mmi1xx (before 5.0)
432 * i = minor version increment (491000 -> 4.10)
436 * The first release of FreeBSD to use ELF by default
439 if (desc == 460002) {
440 if (file_printf(ms, " 4.6.2") == -1)
442 } else if (desc < 460100) {
443 if (file_printf(ms, " %d.%d", desc / 100000,
444 desc / 10000 % 10) == -1)
446 if (desc / 1000 % 10 > 0)
447 if (file_printf(ms, ".%d", desc / 1000 % 10)
450 if ((desc % 1000 > 0) || (desc % 100000 == 0))
451 if (file_printf(ms, " (%d)", desc) == -1)
453 } else if (desc < 500000) {
454 if (file_printf(ms, " %d.%d", desc / 100000,
455 desc / 10000 % 10 + desc / 1000 % 10) == -1)
457 if (desc / 100 % 10 > 0) {
458 if (file_printf(ms, " (%d)", desc) == -1)
460 } else if (desc / 10 % 10 > 0) {
461 if (file_printf(ms, ".%d", desc / 10 % 10)
466 if (file_printf(ms, " %d.%d", desc / 100000,
467 desc / 1000 % 100) == -1)
469 if ((desc / 100 % 10 > 0) ||
470 (desc % 100000 / 100 == 0)) {
471 if (file_printf(ms, " (%d)", desc) == -1)
473 } else if (desc / 10 % 10 > 0) {
474 if (file_printf(ms, ".%d", desc / 10 % 10)
482 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
483 nh_type == NT_OPENBSD_VERSION && descsz == 4) {
484 if (file_printf(ms, ", for OpenBSD") == -1)
486 /* Content of note is always 0 */
491 * Sigh. The 2.0.36 kernel in Debian 2.1, at
492 * least, doesn't correctly implement name
493 * sections, in core dumps, as specified by
494 * the "Program Linking" section of "UNIX(R) System
495 * V Release 4 Programmer's Guide: ANSI C and
496 * Programming Support Tools", because my copy
497 * clearly says "The first 'namesz' bytes in 'name'
498 * contain a *null-terminated* [emphasis mine]
499 * character representation of the entry's owner
500 * or originator", but the 2.0.36 kernel code
501 * doesn't include the terminating null in the
504 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
505 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
506 os_style = OS_STYLE_SVR4;
509 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
510 os_style = OS_STYLE_FREEBSD;
513 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
515 os_style = OS_STYLE_NETBSD;
520 if (file_printf(ms, ", %s-style", os_style_names[os_style]) == -1)
523 if (os_style == OS_STYLE_NETBSD && nh_type == NT_NETBSD_CORE_PROCINFO) {
526 * Extract the program name. It is at
527 * offset 0x7c, and is up to 32-bytes,
528 * including the terminating NUL.
530 if (file_printf(ms, ", from '%.31s'", &nbuf[doff + 0x7c]) == -1)
534 * Extract the signal number. It is at
537 memcpy(&signo, &nbuf[doff + 0x08],
539 if (file_printf(ms, " (signal %u)", getu32(swap, signo)) == -1)
542 } else if (os_style != OS_STYLE_NETBSD && nh_type == NT_PRPSINFO) {
546 * Extract the program name. We assume
547 * it to be 16 characters (that's what it
548 * is in SunOS 5.x and Linux).
550 * Unfortunately, it's at a different offset
551 * in varous OSes, so try multiple offsets.
552 * If the characters aren't all printable,
555 for (i = 0; i < NOFFSETS; i++) {
556 size_t reloffset = prpsoffsets(i);
557 size_t noffset = doff + reloffset;
558 for (j = 0; j < 16; j++, noffset++, reloffset++) {
560 * Make sure we're not past
561 * the end of the buffer; if
562 * we are, just give up.
568 * Make sure we're not past
569 * the end of the contents;
570 * if we are, this obviously
571 * isn't the right offset.
573 if (reloffset >= descsz)
595 #define isquote(c) (strchr("'\"`", (c)) != NULL)
596 if (!isprint(c) || isquote(c))
604 if (file_printf(ms, ", from '%.16s'",
605 &nbuf[doff + prpsoffsets(i)]) == -1)
619 doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
625 if (size != sh_size) {
626 if (file_printf(ms, ", corrupted section header size") == -1)
631 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
636 for ( ; num; num--) {
637 if (read(fd, sh_addr, sh_size) == -1) {
641 if (shs_type == SHT_SYMTAB /* || shs_type == SHT_DYNSYM */) {
642 if (file_printf(ms, ", not stripped") == -1)
647 if (file_printf(ms, ", stripped") == -1)
653 * Look through the program headers of an executable image, searching
654 * for a PT_INTERP section; if one is found, it's dynamically linked,
655 * otherwise it's statically linked.
658 dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
659 int num, size_t size)
663 const char *linking_style = "statically";
664 const char *shared_libraries = "";
665 unsigned char nbuf[BUFSIZ];
667 size_t offset, align;
670 if (size != ph_size) {
671 if (file_printf(ms, ", corrupted program header size") == -1)
675 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
680 for ( ; num; num--) {
681 if (read(fd, ph_addr, ph_size) == -1) {
685 if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
692 linking_style = "dynamically";
695 shared_libraries = " (uses shared libs)";
698 if ((align = ph_align) & 0x80000000) {
700 ", invalid note alignment 0x%lx",
701 (unsigned long)align) == -1)
706 * This is a PT_NOTE section; loop through all the notes
709 if (lseek(fd, (off_t) ph_offset, SEEK_SET)
714 bufsize = read(fd, nbuf, ((ph_filesz < sizeof(nbuf)) ?
715 ph_filesz : sizeof(nbuf)));
722 if (offset >= (size_t)bufsize)
724 offset = donote(ms, nbuf, offset,
725 (size_t)bufsize, class, swap, align);
729 if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
736 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
744 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
749 char c[sizeof (int32_t)];
755 * If we cannot seek, it must be a pipe, socket or fifo.
757 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
758 fd = file_pipe2file(ms, fd, buf, nbytes);
761 * ELF executables have multiple section headers in arbitrary
762 * file locations and thus file(1) cannot determine it from easily.
763 * Instead we traverse thru all section headers until a symbol table
764 * one is found or else the binary is stripped.
766 if (buf[EI_MAG0] != ELFMAG0
767 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
768 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
774 if (class == ELFCLASS32) {
776 if (nbytes <= sizeof (Elf32_Ehdr))
781 (void) memcpy(&elfhdr, buf, sizeof elfhdr);
782 swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
784 if (getu16(swap, elfhdr.e_type) == ET_CORE) {
786 if (dophn_core(ms, class, swap, fd,
787 (off_t)getu32(swap, elfhdr.e_phoff),
788 getu16(swap, elfhdr.e_phnum),
789 (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
795 if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
796 if (dophn_exec(ms, class, swap,
797 fd, (off_t)getu32(swap, elfhdr.e_phoff),
798 getu16(swap, elfhdr.e_phnum),
799 (size_t)getu16(swap, elfhdr.e_phentsize))
803 if (doshn(ms, class, swap, fd,
804 (off_t)getu32(swap, elfhdr.e_shoff),
805 getu16(swap, elfhdr.e_shnum),
806 (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)
812 if (class == ELFCLASS64) {
814 if (nbytes <= sizeof (Elf64_Ehdr))
819 (void) memcpy(&elfhdr, buf, sizeof elfhdr);
820 swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
822 if (getu16(swap, elfhdr.e_type) == ET_CORE) {
824 if (dophn_core(ms, class, swap, fd,
825 #ifdef USE_ARRAY_FOR_64BIT_TYPES
826 (off_t)getu32(swap, elfhdr.e_phoff[1]),
828 (off_t)getu64(swap, elfhdr.e_phoff),
830 getu16(swap, elfhdr.e_phnum),
831 (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
837 if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
838 if (dophn_exec(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))
849 if (doshn(ms, class, swap, fd,
850 #ifdef USE_ARRAY_FOR_64BIT_TYPES
851 (off_t)getu32(swap, elfhdr.e_shoff[1]),
853 (off_t)getu64(swap, elfhdr.e_shoff),
855 getu16(swap, elfhdr.e_shnum),
856 (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)