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.54 2006/01/13 00:45:21 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 #define isquote(c) (strchr("'\"`", (c)) != NULL)
55 private uint16_t getu16(int, uint16_t);
56 private uint32_t getu32(int, uint32_t);
57 private uint64_t getu64(int, uint64_t);
60 getu16(int swap, uint16_t value)
70 retval.c[0] = tmpval.c[1];
71 retval.c[1] = tmpval.c[0];
79 getu32(int swap, uint32_t value)
89 retval.c[0] = tmpval.c[3];
90 retval.c[1] = tmpval.c[2];
91 retval.c[2] = tmpval.c[1];
92 retval.c[3] = tmpval.c[0];
100 getu64(int swap, uint64_t value)
110 retval.c[0] = tmpval.c[7];
111 retval.c[1] = tmpval.c[6];
112 retval.c[2] = tmpval.c[5];
113 retval.c[3] = tmpval.c[4];
114 retval.c[4] = tmpval.c[3];
115 retval.c[5] = tmpval.c[2];
116 retval.c[6] = tmpval.c[1];
117 retval.c[7] = tmpval.c[0];
124 #ifdef USE_ARRAY_FOR_64BIT_TYPES
125 # define elf_getu64(swap, array) \
126 ((swap ? ((uint64_t)getu32(swap, array[0])) << 32 : getu32(swap, array[0])) + \
127 (swap ? getu32(swap, array[1]) : ((uint64_t)getu32(swap, array[1]) << 32)))
129 # define elf_getu64(swap, value) getu64(swap, value)
132 #define xsh_addr (class == ELFCLASS32 \
135 #define xsh_sizeof (class == ELFCLASS32 \
138 #define xsh_size (class == ELFCLASS32 \
139 ? getu32(swap, sh32.sh_size) \
140 : getu64(swap, sh64.sh_size))
141 #define xsh_offset (class == ELFCLASS32 \
142 ? getu32(swap, sh32.sh_offset) \
143 : getu64(swap, sh64.sh_offset))
144 #define xsh_type (class == ELFCLASS32 \
145 ? getu32(swap, sh32.sh_type) \
146 : getu32(swap, sh64.sh_type))
147 #define xph_addr (class == ELFCLASS32 \
150 #define xph_sizeof (class == ELFCLASS32 \
153 #define xph_type (class == ELFCLASS32 \
154 ? getu32(swap, ph32.p_type) \
155 : getu32(swap, ph64.p_type))
156 #define xph_offset (class == ELFCLASS32 \
157 ? getu32(swap, ph32.p_offset) \
158 : getu64(swap, ph64.p_offset))
159 #define xph_align (size_t)((class == ELFCLASS32 \
160 ? (off_t) (ph32.p_align ? \
161 getu32(swap, ph32.p_align) : 4) \
162 : (off_t) (ph64.p_align ? \
163 getu64(swap, ph64.p_align) : 4)))
164 #define xph_filesz (size_t)((class == ELFCLASS32 \
165 ? getu32(swap, ph32.p_filesz) \
166 : getu64(swap, ph64.p_filesz)))
167 #define xnh_addr (class == ELFCLASS32 \
170 #define xph_memsz (size_t)((class == ELFCLASS32 \
171 ? getu32(swap, ph32.p_memsz) \
172 : getu64(swap, ph64.p_memsz)))
173 #define xnh_sizeof (class == ELFCLASS32 \
176 #define xnh_type (class == ELFCLASS32 \
177 ? getu32(swap, nh32.n_type) \
178 : getu32(swap, nh64.n_type))
179 #define xnh_namesz (class == ELFCLASS32 \
180 ? getu32(swap, nh32.n_namesz) \
181 : getu32(swap, nh64.n_namesz))
182 #define xnh_descsz (class == ELFCLASS32 \
183 ? getu32(swap, nh32.n_descsz) \
184 : getu32(swap, nh64.n_descsz))
185 #define prpsoffsets(i) (class == ELFCLASS32 \
190 size_t prpsoffsets32[] = {
192 28, /* Linux 2.0.36 */
193 32, /* Linux (I forget which kernel version) */
197 size_t prpsoffsets64[] = {
198 16, /* FreeBSD, 64-bit */
199 40, /* Linux (tested on core from 2.4.x) */
200 120, /* SunOS 5.x, 64-bit */
203 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
204 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
206 #define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
209 * Look through the program headers of an executable image, searching
210 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
211 * "FreeBSD"; if one is found, try looking in various places in its
212 * contents for a 16-character string containing only printable
213 * characters - if found, that string should be the name of the program
214 * that dropped core. Note: right after that 16-character string is,
215 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
216 * Linux, a longer string (80 characters, in 5.x, probably other
217 * SVR4-flavored systems, and Linux) containing the start of the
218 * command line for that program.
220 * The signal number probably appears in a section of type NT_PRSTATUS,
221 * but that's also rather OS-dependent, in ways that are harder to
222 * dissect with heuristics, so I'm not bothering with the signal number.
223 * (I suppose the signal number could be of interest in situations where
224 * you don't have the binary of the program that dropped core; if you
225 * *do* have that binary, the debugger will probably tell you what
229 #define OS_STYLE_SVR4 0
230 #define OS_STYLE_FREEBSD 1
231 #define OS_STYLE_NETBSD 2
233 private const char *os_style_names[] = {
239 #define FLAGS_DID_CORE 1
242 dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
243 int num, size_t size)
248 unsigned char nbuf[BUFSIZ];
252 if (size != xph_sizeof) {
253 if (file_printf(ms, ", corrupted program header size") == -1)
259 * Loop through all the program headers.
261 for ( ; num; num--) {
262 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
266 if (read(fd, xph_addr, xph_sizeof) == -1) {
271 if (xph_type != PT_NOTE)
275 * This is a PT_NOTE section; loop through all the notes
278 if (lseek(fd, (off_t)xph_offset, SEEK_SET) == (off_t)-1) {
282 bufsize = read(fd, nbuf,
283 ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
290 if (offset >= (size_t)bufsize)
292 offset = donote(ms, nbuf, offset, (size_t)bufsize,
293 class, swap, 4, &flags);
304 donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
305 int class, int swap, size_t align, int *flags)
313 uint32_t namesz, descsz;
315 (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
316 offset += xnh_sizeof;
320 if ((namesz == 0) && (descsz == 0)) {
322 * We're out of note headers.
327 if (namesz & 0x80000000) {
328 (void)file_printf(ms, ", bad note name size 0x%lx",
329 (unsigned long)namesz);
333 if (descsz & 0x80000000) {
334 (void)file_printf(ms, ", bad note description size 0x%lx",
335 (unsigned long)descsz);
341 doff = ELF_ALIGN(offset + namesz);
343 if (offset + namesz > size) {
345 * We're past the end of the buffer.
350 offset = ELF_ALIGN(doff + descsz);
351 if (doff + descsz > size) {
355 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
356 xnh_type == NT_GNU_VERSION && descsz == 16) {
358 (void)memcpy(desc, &nbuf[doff], sizeof(desc));
360 if (file_printf(ms, ", for GNU/") == -1)
362 switch (getu32(swap, desc[0])) {
364 if (file_printf(ms, "Linux") == -1)
368 if (file_printf(ms, "Hurd") == -1)
372 if (file_printf(ms, "Solaris") == -1)
376 if (file_printf(ms, "<unknown>") == -1)
379 if (file_printf(ms, " %d.%d.%d", getu32(swap, desc[1]),
380 getu32(swap, desc[2]), getu32(swap, desc[3])) == -1)
385 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
386 xnh_type == NT_NETBSD_VERSION && descsz == 4) {
388 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
389 desc = getu32(swap, desc);
391 if (file_printf(ms, ", for NetBSD") == -1)
394 * The version number used to be stuck as 199905, and was thus
395 * basically content-free. Newer versions of NetBSD have fixed
396 * this and now use the encoding of __NetBSD_Version__:
402 * r = release ["",A-Z,Z[A-Z] but numeric]
405 if (desc > 100000000U) {
406 u_int ver_patch = (desc / 100) % 100;
407 u_int ver_rel = (desc / 10000) % 100;
408 u_int ver_min = (desc / 1000000) % 100;
409 u_int ver_maj = desc / 100000000;
411 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
413 if (ver_rel == 0 && ver_patch != 0) {
414 if (file_printf(ms, ".%u", ver_patch) == -1)
416 } else if (ver_rel != 0) {
417 while (ver_rel > 26) {
418 file_printf(ms, "Z");
421 file_printf(ms, "%c", 'A' + ver_rel - 1);
427 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
428 xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
430 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
431 desc = getu32(swap, desc);
432 if (file_printf(ms, ", for FreeBSD") == -1)
436 * Contents is __FreeBSD_version, whose relation to OS
437 * versions is defined by a huge table in the Porter's
438 * Handbook. This is the general scheme:
441 * Mmp000 (before 4.10)
442 * Mmi0p0 (before 5.0)
445 * Development branches:
446 * Mmpxxx (before 4.6)
447 * Mmp1xx (before 4.10)
448 * Mmi1xx (before 5.0)
454 * i = minor version increment (491000 -> 4.10)
458 * The first release of FreeBSD to use ELF by default
461 if (desc == 460002) {
462 if (file_printf(ms, " 4.6.2") == -1)
464 } else if (desc < 460100) {
465 if (file_printf(ms, " %d.%d", desc / 100000,
466 desc / 10000 % 10) == -1)
468 if (desc / 1000 % 10 > 0)
469 if (file_printf(ms, ".%d", desc / 1000 % 10)
472 if ((desc % 1000 > 0) || (desc % 100000 == 0))
473 if (file_printf(ms, " (%d)", desc) == -1)
475 } else if (desc < 500000) {
476 if (file_printf(ms, " %d.%d", desc / 100000,
477 desc / 10000 % 10 + desc / 1000 % 10) == -1)
479 if (desc / 100 % 10 > 0) {
480 if (file_printf(ms, " (%d)", desc) == -1)
482 } else if (desc / 10 % 10 > 0) {
483 if (file_printf(ms, ".%d", desc / 10 % 10)
488 if (file_printf(ms, " %d.%d", desc / 100000,
489 desc / 1000 % 100) == -1)
491 if ((desc / 100 % 10 > 0) ||
492 (desc % 100000 / 100 == 0)) {
493 if (file_printf(ms, " (%d)", desc) == -1)
495 } else if (desc / 10 % 10 > 0) {
496 if (file_printf(ms, ".%d", desc / 10 % 10)
504 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
505 xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
506 if (file_printf(ms, ", for OpenBSD") == -1)
508 /* Content of note is always 0 */
512 if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
513 xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
515 if (file_printf(ms, ", for DragonFly") == -1)
517 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
518 desc = getu32(swap, desc);
519 if (file_printf(ms, " %d.%d.%d", desc / 100000,
520 desc / 10000 % 10, desc % 10000) == -1)
526 * Sigh. The 2.0.36 kernel in Debian 2.1, at
527 * least, doesn't correctly implement name
528 * sections, in core dumps, as specified by
529 * the "Program Linking" section of "UNIX(R) System
530 * V Release 4 Programmer's Guide: ANSI C and
531 * Programming Support Tools", because my copy
532 * clearly says "The first 'namesz' bytes in 'name'
533 * contain a *null-terminated* [emphasis mine]
534 * character representation of the entry's owner
535 * or originator", but the 2.0.36 kernel code
536 * doesn't include the terminating null in the
539 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
540 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
541 os_style = OS_STYLE_SVR4;
544 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
545 os_style = OS_STYLE_FREEBSD;
548 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
550 os_style = OS_STYLE_NETBSD;
554 if (os_style != -1) {
555 if ((*flags & FLAGS_DID_CORE) == 0) {
556 if (file_printf(ms, ", %s-style",
557 os_style_names[os_style]) == -1)
559 *flags |= FLAGS_DID_CORE;
564 case OS_STYLE_NETBSD:
565 if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
568 * Extract the program name. It is at
569 * offset 0x7c, and is up to 32-bytes,
570 * including the terminating NUL.
572 if (file_printf(ms, ", from '%.31s'",
573 &nbuf[doff + 0x7c]) == -1)
577 * Extract the signal number. It is at
580 (void)memcpy(&signo, &nbuf[doff + 0x08],
582 if (file_printf(ms, " (signal %u)",
583 getu32(swap, signo)) == -1)
590 if (xnh_type == NT_PRPSINFO) {
594 * Extract the program name. We assume
595 * it to be 16 characters (that's what it
596 * is in SunOS 5.x and Linux).
598 * Unfortunately, it's at a different offset
599 * in varous OSes, so try multiple offsets.
600 * If the characters aren't all printable,
603 for (i = 0; i < NOFFSETS; i++) {
604 size_t reloffset = prpsoffsets(i);
605 size_t noffset = doff + reloffset;
606 for (j = 0; j < 16; j++, noffset++,
609 * Make sure we're not past
610 * the end of the buffer; if
611 * we are, just give up.
617 * Make sure we're not past
618 * the end of the contents;
619 * if we are, this obviously
620 * isn't the right offset.
622 if (reloffset >= descsz)
644 if (!isprint(c) || isquote(c))
651 if (file_printf(ms, ", from '%.16s'",
652 &nbuf[doff + prpsoffsets(i)]) == -1)
667 doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
677 if (size != xsh_sizeof) {
678 if (file_printf(ms, ", corrupted section header size") == -1)
683 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
688 for ( ; num; num--) {
689 if (read(fd, xsh_addr, xsh_sizeof) == -1) {
701 if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
706 if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
707 file_error(ms, errno, "Cannot allocate memory"
711 if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
717 if (read(fd, nbuf, (size_t)xsh_size) !=
726 if (noff >= (size_t)xsh_size)
728 noff = donote(ms, nbuf, (size_t)noff,
729 (size_t)xsh_size, class, swap, 4,
734 if ((lseek(fd, off, SEEK_SET)) == (off_t)-1) {
743 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
749 * Look through the program headers of an executable image, searching
750 * for a PT_INTERP section; if one is found, it's dynamically linked,
751 * otherwise it's statically linked.
754 dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
755 int num, size_t size)
759 const char *linking_style = "statically";
760 const char *shared_libraries = "";
761 unsigned char nbuf[BUFSIZ];
763 size_t offset, align;
767 if (size != xph_sizeof) {
768 if (file_printf(ms, ", corrupted program header size") == -1)
772 if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
777 for ( ; num; num--) {
778 if (read(fd, xph_addr, xph_sizeof) == -1) {
782 if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
789 linking_style = "dynamically";
792 shared_libraries = " (uses shared libs)";
795 if ((align = xph_align) & 0x80000000) {
797 ", invalid note alignment 0x%lx",
798 (unsigned long)align) == -1)
803 * This is a PT_NOTE section; loop through all the notes
806 if (lseek(fd, (off_t)xph_offset, SEEK_SET)
811 bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
812 xph_filesz : sizeof(nbuf)));
819 if (offset >= (size_t)bufsize)
821 offset = donote(ms, nbuf, offset,
822 (size_t)bufsize, class, swap, align,
827 if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
834 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
842 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
847 char c[sizeof (int32_t)];
853 * If we cannot seek, it must be a pipe, socket or fifo.
855 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
856 fd = file_pipe2file(ms, fd, buf, nbytes);
859 * ELF executables have multiple section headers in arbitrary
860 * file locations and thus file(1) cannot determine it from easily.
861 * Instead we traverse thru all section headers until a symbol table
862 * one is found or else the binary is stripped.
864 if (buf[EI_MAG0] != ELFMAG0
865 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
866 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
872 if (class == ELFCLASS32) {
874 if (nbytes <= sizeof (Elf32_Ehdr))
879 (void) memcpy(&elfhdr, buf, sizeof elfhdr);
880 swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
882 if (getu16(swap, elfhdr.e_type) == ET_CORE) {
884 if (dophn_core(ms, class, swap, fd,
885 (off_t)getu32(swap, elfhdr.e_phoff),
886 getu16(swap, elfhdr.e_phnum),
887 (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
893 if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
894 if (dophn_exec(ms, class, swap,
895 fd, (off_t)getu32(swap, elfhdr.e_phoff),
896 getu16(swap, elfhdr.e_phnum),
897 (size_t)getu16(swap, elfhdr.e_phentsize))
901 if (doshn(ms, class, swap, fd,
902 (off_t)getu32(swap, elfhdr.e_shoff),
903 getu16(swap, elfhdr.e_shnum),
904 (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)
910 if (class == ELFCLASS64) {
912 if (nbytes <= sizeof (Elf64_Ehdr))
917 (void) memcpy(&elfhdr, buf, sizeof elfhdr);
918 swap = (u.c[sizeof(int32_t) - 1] + 1) != elfhdr.e_ident[5];
920 if (getu16(swap, elfhdr.e_type) == ET_CORE) {
922 if (dophn_core(ms, class, swap, fd,
923 (off_t)elf_getu64(swap, elfhdr.e_phoff),
924 getu16(swap, elfhdr.e_phnum),
925 (size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
931 if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
932 if (dophn_exec(ms, class, swap, fd,
933 (off_t)elf_getu64(swap, elfhdr.e_phoff),
934 getu16(swap, elfhdr.e_phnum),
935 (size_t)getu16(swap, elfhdr.e_phentsize))
939 if (doshn(ms, class, swap, fd,
940 (off_t)elf_getu64(swap, elfhdr.e_shoff),
941 getu16(swap, elfhdr.e_shnum),
942 (size_t)getu16(swap, elfhdr.e_shentsize)) == -1)