|
@@ -37,7 +37,7 @@
|
|
|
#include "readelf.h"
|
|
|
|
|
|
#ifndef lint
|
|
|
-FILE_RCSID("@(#)$Id: readelf.c,v 1.49 2005/07/14 02:22:25 christos Exp $")
|
|
|
+FILE_RCSID("@(#)$Id: readelf.c,v 1.54 2006/01/13 00:45:21 christos Exp $")
|
|
|
#endif
|
|
|
|
|
|
#ifdef ELFCORE
|
|
@@ -46,10 +46,12 @@ private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t);
|
|
|
private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t);
|
|
|
private int doshn(struct magic_set *, int, int, int, off_t, int, size_t);
|
|
|
private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
|
|
|
- int, size_t);
|
|
|
+ int, size_t, int *);
|
|
|
|
|
|
#define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
|
|
|
|
|
|
+#define isquote(c) (strchr("'\"`", (c)) != NULL)
|
|
|
+
|
|
|
private uint16_t getu16(int, uint16_t);
|
|
|
private uint32_t getu32(int, uint32_t);
|
|
|
private uint64_t getu64(int, uint64_t);
|
|
@@ -119,48 +121,65 @@ getu64(int swap, uint64_t value)
|
|
|
return value;
|
|
|
}
|
|
|
|
|
|
-#define sh_addr (class == ELFCLASS32 \
|
|
|
+#ifdef USE_ARRAY_FOR_64BIT_TYPES
|
|
|
+# define elf_getu64(swap, array) \
|
|
|
+ ((swap ? ((uint64_t)getu32(swap, array[0])) << 32 : getu32(swap, array[0])) + \
|
|
|
+ (swap ? getu32(swap, array[1]) : ((uint64_t)getu32(swap, array[1]) << 32)))
|
|
|
+#else
|
|
|
+# define elf_getu64(swap, value) getu64(swap, value)
|
|
|
+#endif
|
|
|
+
|
|
|
+#define xsh_addr (class == ELFCLASS32 \
|
|
|
? (void *) &sh32 \
|
|
|
: (void *) &sh64)
|
|
|
-#define sh_size (class == ELFCLASS32 \
|
|
|
+#define xsh_sizeof (class == ELFCLASS32 \
|
|
|
? sizeof sh32 \
|
|
|
: sizeof sh64)
|
|
|
-#define shs_type (class == ELFCLASS32 \
|
|
|
+#define xsh_size (class == ELFCLASS32 \
|
|
|
+ ? getu32(swap, sh32.sh_size) \
|
|
|
+ : getu64(swap, sh64.sh_size))
|
|
|
+#define xsh_offset (class == ELFCLASS32 \
|
|
|
+ ? getu32(swap, sh32.sh_offset) \
|
|
|
+ : getu64(swap, sh64.sh_offset))
|
|
|
+#define xsh_type (class == ELFCLASS32 \
|
|
|
? getu32(swap, sh32.sh_type) \
|
|
|
: getu32(swap, sh64.sh_type))
|
|
|
-#define ph_addr (class == ELFCLASS32 \
|
|
|
+#define xph_addr (class == ELFCLASS32 \
|
|
|
? (void *) &ph32 \
|
|
|
: (void *) &ph64)
|
|
|
-#define ph_size (class == ELFCLASS32 \
|
|
|
+#define xph_sizeof (class == ELFCLASS32 \
|
|
|
? sizeof ph32 \
|
|
|
: sizeof ph64)
|
|
|
-#define ph_type (class == ELFCLASS32 \
|
|
|
+#define xph_type (class == ELFCLASS32 \
|
|
|
? getu32(swap, ph32.p_type) \
|
|
|
: getu32(swap, ph64.p_type))
|
|
|
-#define ph_offset (class == ELFCLASS32 \
|
|
|
+#define xph_offset (class == ELFCLASS32 \
|
|
|
? getu32(swap, ph32.p_offset) \
|
|
|
: getu64(swap, ph64.p_offset))
|
|
|
-#define ph_align (size_t)((class == ELFCLASS32 \
|
|
|
+#define xph_align (size_t)((class == ELFCLASS32 \
|
|
|
? (off_t) (ph32.p_align ? \
|
|
|
getu32(swap, ph32.p_align) : 4) \
|
|
|
: (off_t) (ph64.p_align ? \
|
|
|
getu64(swap, ph64.p_align) : 4)))
|
|
|
-#define ph_filesz (size_t)((class == ELFCLASS32 \
|
|
|
+#define xph_filesz (size_t)((class == ELFCLASS32 \
|
|
|
? getu32(swap, ph32.p_filesz) \
|
|
|
: getu64(swap, ph64.p_filesz)))
|
|
|
-#define ph_memsz (size_t)((class == ELFCLASS32 \
|
|
|
+#define xnh_addr (class == ELFCLASS32 \
|
|
|
+ ? (void *) &nh32 \
|
|
|
+ : (void *) &nh64)
|
|
|
+#define xph_memsz (size_t)((class == ELFCLASS32 \
|
|
|
? getu32(swap, ph32.p_memsz) \
|
|
|
: getu64(swap, ph64.p_memsz)))
|
|
|
-#define nh_size (class == ELFCLASS32 \
|
|
|
+#define xnh_sizeof (class == ELFCLASS32 \
|
|
|
? sizeof nh32 \
|
|
|
: sizeof nh64)
|
|
|
-#define nh_type (class == ELFCLASS32 \
|
|
|
+#define xnh_type (class == ELFCLASS32 \
|
|
|
? getu32(swap, nh32.n_type) \
|
|
|
: getu32(swap, nh64.n_type))
|
|
|
-#define nh_namesz (class == ELFCLASS32 \
|
|
|
+#define xnh_namesz (class == ELFCLASS32 \
|
|
|
? getu32(swap, nh32.n_namesz) \
|
|
|
: getu32(swap, nh64.n_namesz))
|
|
|
-#define nh_descsz (class == ELFCLASS32 \
|
|
|
+#define xnh_descsz (class == ELFCLASS32 \
|
|
|
? getu32(swap, nh32.n_descsz) \
|
|
|
: getu32(swap, nh64.n_descsz))
|
|
|
#define prpsoffsets(i) (class == ELFCLASS32 \
|
|
@@ -217,6 +236,8 @@ private const char *os_style_names[] = {
|
|
|
"NetBSD",
|
|
|
};
|
|
|
|
|
|
+#define FLAGS_DID_CORE 1
|
|
|
+
|
|
|
private int
|
|
|
dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
int num, size_t size)
|
|
@@ -226,12 +247,14 @@ dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
size_t offset;
|
|
|
unsigned char nbuf[BUFSIZ];
|
|
|
ssize_t bufsize;
|
|
|
+ int flags = 0;
|
|
|
|
|
|
- if (size != ph_size) {
|
|
|
+ if (size != xph_sizeof) {
|
|
|
if (file_printf(ms, ", corrupted program header size") == -1)
|
|
|
return -1;
|
|
|
return 0;
|
|
|
}
|
|
|
+
|
|
|
/*
|
|
|
* Loop through all the program headers.
|
|
|
*/
|
|
@@ -240,24 +263,24 @@ dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
file_badseek(ms);
|
|
|
return -1;
|
|
|
}
|
|
|
- if (read(fd, ph_addr, ph_size) == -1) {
|
|
|
+ if (read(fd, xph_addr, xph_sizeof) == -1) {
|
|
|
file_badread(ms);
|
|
|
return -1;
|
|
|
}
|
|
|
off += size;
|
|
|
- if (ph_type != PT_NOTE)
|
|
|
+ if (xph_type != PT_NOTE)
|
|
|
continue;
|
|
|
|
|
|
/*
|
|
|
* This is a PT_NOTE section; loop through all the notes
|
|
|
* in the section.
|
|
|
*/
|
|
|
- if (lseek(fd, (off_t) ph_offset, SEEK_SET) == (off_t)-1) {
|
|
|
+ if (lseek(fd, (off_t)xph_offset, SEEK_SET) == (off_t)-1) {
|
|
|
file_badseek(ms);
|
|
|
return -1;
|
|
|
}
|
|
|
bufsize = read(fd, nbuf,
|
|
|
- ((ph_filesz < sizeof(nbuf)) ? ph_filesz : sizeof(nbuf)));
|
|
|
+ ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
|
|
|
if (bufsize == -1) {
|
|
|
file_badread(ms);
|
|
|
return -1;
|
|
@@ -267,7 +290,7 @@ dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
if (offset >= (size_t)bufsize)
|
|
|
break;
|
|
|
offset = donote(ms, nbuf, offset, (size_t)bufsize,
|
|
|
- class, swap, 4);
|
|
|
+ class, swap, 4, &flags);
|
|
|
if (offset == 0)
|
|
|
break;
|
|
|
|
|
@@ -279,7 +302,7 @@ dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
|
|
|
private size_t
|
|
|
donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
|
|
|
- int class, int swap, size_t align)
|
|
|
+ int class, int swap, size_t align, int *flags)
|
|
|
{
|
|
|
Elf32_Nhdr nh32;
|
|
|
Elf64_Nhdr nh64;
|
|
@@ -289,14 +312,11 @@ donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
|
|
|
#endif
|
|
|
uint32_t namesz, descsz;
|
|
|
|
|
|
- if (class == ELFCLASS32)
|
|
|
- memcpy(&nh32, &nbuf[offset], sizeof(nh32));
|
|
|
- else
|
|
|
- memcpy(&nh64, &nbuf[offset], sizeof(nh64));
|
|
|
- offset += nh_size;
|
|
|
+ (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
|
|
|
+ offset += xnh_sizeof;
|
|
|
|
|
|
- namesz = nh_namesz;
|
|
|
- descsz = nh_descsz;
|
|
|
+ namesz = xnh_namesz;
|
|
|
+ descsz = xnh_descsz;
|
|
|
if ((namesz == 0) && (descsz == 0)) {
|
|
|
/*
|
|
|
* We're out of note headers.
|
|
@@ -333,7 +353,7 @@ donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
|
|
|
}
|
|
|
|
|
|
if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
|
|
|
- nh_type == NT_GNU_VERSION && descsz == 16) {
|
|
|
+ xnh_type == NT_GNU_VERSION && descsz == 16) {
|
|
|
uint32_t desc[4];
|
|
|
(void)memcpy(desc, &nbuf[doff], sizeof(desc));
|
|
|
|
|
@@ -352,6 +372,14 @@ donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
|
|
|
if (file_printf(ms, "Solaris") == -1)
|
|
|
return size;
|
|
|
break;
|
|
|
+ case GNU_OS_KFREEBSD:
|
|
|
+ if (file_printf(ms, "kFreeBSD") == -1)
|
|
|
+ return size;
|
|
|
+ break;
|
|
|
+ case GNU_OS_KNETBSD:
|
|
|
+ if (file_printf(ms, "kNetBSD") == -1)
|
|
|
+ return size;
|
|
|
+ break;
|
|
|
default:
|
|
|
if (file_printf(ms, "<unknown>") == -1)
|
|
|
return size;
|
|
@@ -363,7 +391,7 @@ donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
|
|
|
}
|
|
|
|
|
|
if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
|
|
|
- nh_type == NT_NETBSD_VERSION && descsz == 4) {
|
|
|
+ xnh_type == NT_NETBSD_VERSION && descsz == 4) {
|
|
|
uint32_t desc;
|
|
|
(void)memcpy(&desc, &nbuf[doff], sizeof(desc));
|
|
|
desc = getu32(swap, desc);
|
|
@@ -405,7 +433,7 @@ donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
|
|
|
}
|
|
|
|
|
|
if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
|
|
|
- nh_type == NT_FREEBSD_VERSION && descsz == 4) {
|
|
|
+ xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
|
|
|
uint32_t desc;
|
|
|
(void)memcpy(&desc, &nbuf[doff], sizeof(desc));
|
|
|
desc = getu32(swap, desc);
|
|
@@ -482,7 +510,7 @@ donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
|
|
|
}
|
|
|
|
|
|
if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
|
|
|
- nh_type == NT_OPENBSD_VERSION && descsz == 4) {
|
|
|
+ xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
|
|
|
if (file_printf(ms, ", for OpenBSD") == -1)
|
|
|
return size;
|
|
|
/* Content of note is always 0 */
|
|
@@ -490,7 +518,7 @@ donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
|
|
|
}
|
|
|
|
|
|
if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
|
|
|
- nh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
|
|
|
+ xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
|
|
|
uint32_t desc;
|
|
|
if (file_printf(ms, ", for DragonFly") == -1)
|
|
|
return size;
|
|
@@ -531,100 +559,113 @@ donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
|
|
|
}
|
|
|
|
|
|
#ifdef ELFCORE
|
|
|
- if (os_style != -1)
|
|
|
- if (file_printf(ms, ", %s-style", os_style_names[os_style]) == -1)
|
|
|
- return size;
|
|
|
+ if (os_style != -1) {
|
|
|
+ if ((*flags & FLAGS_DID_CORE) == 0) {
|
|
|
+ if (file_printf(ms, ", %s-style",
|
|
|
+ os_style_names[os_style]) == -1)
|
|
|
+ return size;
|
|
|
+ *flags |= FLAGS_DID_CORE;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (os_style == OS_STYLE_NETBSD && nh_type == NT_NETBSD_CORE_PROCINFO) {
|
|
|
- uint32_t signo;
|
|
|
- /*
|
|
|
- * Extract the program name. It is at
|
|
|
- * offset 0x7c, and is up to 32-bytes,
|
|
|
- * including the terminating NUL.
|
|
|
- */
|
|
|
- if (file_printf(ms, ", from '%.31s'", &nbuf[doff + 0x7c]) == -1)
|
|
|
- return size;
|
|
|
-
|
|
|
- /*
|
|
|
- * Extract the signal number. It is at
|
|
|
- * offset 0x08.
|
|
|
- */
|
|
|
- memcpy(&signo, &nbuf[doff + 0x08],
|
|
|
- sizeof(signo));
|
|
|
- if (file_printf(ms, " (signal %u)", getu32(swap, signo)) == -1)
|
|
|
+ switch (os_style) {
|
|
|
+ case OS_STYLE_NETBSD:
|
|
|
+ if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
|
|
|
+ uint32_t signo;
|
|
|
+ /*
|
|
|
+ * Extract the program name. It is at
|
|
|
+ * offset 0x7c, and is up to 32-bytes,
|
|
|
+ * including the terminating NUL.
|
|
|
+ */
|
|
|
+ if (file_printf(ms, ", from '%.31s'",
|
|
|
+ &nbuf[doff + 0x7c]) == -1)
|
|
|
+ return size;
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Extract the signal number. It is at
|
|
|
+ * offset 0x08.
|
|
|
+ */
|
|
|
+ (void)memcpy(&signo, &nbuf[doff + 0x08],
|
|
|
+ sizeof(signo));
|
|
|
+ if (file_printf(ms, " (signal %u)",
|
|
|
+ getu32(swap, signo)) == -1)
|
|
|
+ return size;
|
|
|
return size;
|
|
|
- return size;
|
|
|
- } else if (os_style != OS_STYLE_NETBSD && nh_type == NT_PRPSINFO) {
|
|
|
- size_t i, j;
|
|
|
- unsigned char c;
|
|
|
- /*
|
|
|
- * Extract the program name. We assume
|
|
|
- * it to be 16 characters (that's what it
|
|
|
- * is in SunOS 5.x and Linux).
|
|
|
- *
|
|
|
- * Unfortunately, it's at a different offset
|
|
|
- * in varous OSes, so try multiple offsets.
|
|
|
- * If the characters aren't all printable,
|
|
|
- * reject it.
|
|
|
- */
|
|
|
- for (i = 0; i < NOFFSETS; i++) {
|
|
|
- size_t reloffset = prpsoffsets(i);
|
|
|
- size_t noffset = doff + reloffset;
|
|
|
- for (j = 0; j < 16; j++, noffset++, reloffset++) {
|
|
|
- /*
|
|
|
- * Make sure we're not past
|
|
|
- * the end of the buffer; if
|
|
|
- * we are, just give up.
|
|
|
- */
|
|
|
- if (noffset >= size)
|
|
|
- goto tryanother;
|
|
|
-
|
|
|
- /*
|
|
|
- * Make sure we're not past
|
|
|
- * the end of the contents;
|
|
|
- * if we are, this obviously
|
|
|
- * isn't the right offset.
|
|
|
- */
|
|
|
- if (reloffset >= descsz)
|
|
|
- goto tryanother;
|
|
|
+ }
|
|
|
+ break;
|
|
|
|
|
|
- c = nbuf[noffset];
|
|
|
- if (c == '\0') {
|
|
|
+ default:
|
|
|
+ if (xnh_type == NT_PRPSINFO) {
|
|
|
+ size_t i, j;
|
|
|
+ unsigned char c;
|
|
|
+ /*
|
|
|
+ * Extract the program name. We assume
|
|
|
+ * it to be 16 characters (that's what it
|
|
|
+ * is in SunOS 5.x and Linux).
|
|
|
+ *
|
|
|
+ * Unfortunately, it's at a different offset
|
|
|
+ * in varous OSes, so try multiple offsets.
|
|
|
+ * If the characters aren't all printable,
|
|
|
+ * reject it.
|
|
|
+ */
|
|
|
+ for (i = 0; i < NOFFSETS; i++) {
|
|
|
+ size_t reloffset = prpsoffsets(i);
|
|
|
+ size_t noffset = doff + reloffset;
|
|
|
+ for (j = 0; j < 16; j++, noffset++,
|
|
|
+ reloffset++) {
|
|
|
/*
|
|
|
- * A '\0' at the
|
|
|
- * beginning is
|
|
|
- * obviously wrong.
|
|
|
- * Any other '\0'
|
|
|
- * means we're done.
|
|
|
+ * Make sure we're not past
|
|
|
+ * the end of the buffer; if
|
|
|
+ * we are, just give up.
|
|
|
*/
|
|
|
- if (j == 0)
|
|
|
+ if (noffset >= size)
|
|
|
goto tryanother;
|
|
|
- else
|
|
|
- break;
|
|
|
- } else {
|
|
|
+
|
|
|
/*
|
|
|
- * A nonprintable
|
|
|
- * character is also
|
|
|
- * wrong.
|
|
|
+ * Make sure we're not past
|
|
|
+ * the end of the contents;
|
|
|
+ * if we are, this obviously
|
|
|
+ * isn't the right offset.
|
|
|
*/
|
|
|
-#define isquote(c) (strchr("'\"`", (c)) != NULL)
|
|
|
- if (!isprint(c) || isquote(c))
|
|
|
+ if (reloffset >= descsz)
|
|
|
goto tryanother;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- /*
|
|
|
- * Well, that worked.
|
|
|
- */
|
|
|
- if (file_printf(ms, ", from '%.16s'",
|
|
|
- &nbuf[doff + prpsoffsets(i)]) == -1)
|
|
|
+ c = nbuf[noffset];
|
|
|
+ if (c == '\0') {
|
|
|
+ /*
|
|
|
+ * A '\0' at the
|
|
|
+ * beginning is
|
|
|
+ * obviously wrong.
|
|
|
+ * Any other '\0'
|
|
|
+ * means we're done.
|
|
|
+ */
|
|
|
+ if (j == 0)
|
|
|
+ goto tryanother;
|
|
|
+ else
|
|
|
+ break;
|
|
|
+ } else {
|
|
|
+ /*
|
|
|
+ * A nonprintable
|
|
|
+ * character is also
|
|
|
+ * wrong.
|
|
|
+ */
|
|
|
+ if (!isprint(c) || isquote(c))
|
|
|
+ goto tryanother;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /*
|
|
|
+ * Well, that worked.
|
|
|
+ */
|
|
|
+ if (file_printf(ms, ", from '%.16s'",
|
|
|
+ &nbuf[doff + prpsoffsets(i)]) == -1)
|
|
|
+ return size;
|
|
|
return size;
|
|
|
- return size;
|
|
|
|
|
|
- tryanother:
|
|
|
- ;
|
|
|
+ tryanother:
|
|
|
+ ;
|
|
|
+ }
|
|
|
}
|
|
|
- return offset;
|
|
|
+ break;
|
|
|
}
|
|
|
#endif
|
|
|
return offset;
|
|
@@ -636,8 +677,12 @@ doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
|
|
|
{
|
|
|
Elf32_Shdr sh32;
|
|
|
Elf64_Shdr sh64;
|
|
|
+ int stripped = 1;
|
|
|
+ int flags = 0;
|
|
|
+ void *nbuf;
|
|
|
+ off_t noff;
|
|
|
|
|
|
- if (size != sh_size) {
|
|
|
+ if (size != xsh_sizeof) {
|
|
|
if (file_printf(ms, ", corrupted section header size") == -1)
|
|
|
return -1;
|
|
|
return 0;
|
|
@@ -649,17 +694,61 @@ doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
|
|
|
}
|
|
|
|
|
|
for ( ; num; num--) {
|
|
|
- if (read(fd, sh_addr, sh_size) == -1) {
|
|
|
+ if (read(fd, xsh_addr, xsh_sizeof) == -1) {
|
|
|
file_badread(ms);
|
|
|
return -1;
|
|
|
}
|
|
|
- if (shs_type == SHT_SYMTAB /* || shs_type == SHT_DYNSYM */) {
|
|
|
- if (file_printf(ms, ", not stripped") == -1)
|
|
|
+ switch (xsh_type) {
|
|
|
+ case SHT_SYMTAB:
|
|
|
+#if 0
|
|
|
+ case SHT_DYNSYM:
|
|
|
+#endif
|
|
|
+ stripped = 0;
|
|
|
+ break;
|
|
|
+ case SHT_NOTE:
|
|
|
+ if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
|
|
|
+ (off_t)-1) {
|
|
|
+ file_badread(ms);
|
|
|
return -1;
|
|
|
- return 0;
|
|
|
+ }
|
|
|
+ if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
|
|
|
+ file_error(ms, errno, "Cannot allocate memory"
|
|
|
+ " for note");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
|
|
|
+ (off_t)-1) {
|
|
|
+ file_badread(ms);
|
|
|
+ free(nbuf);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ if (read(fd, nbuf, (size_t)xsh_size) !=
|
|
|
+ (ssize_t)xsh_size) {
|
|
|
+ free(nbuf);
|
|
|
+ file_badread(ms);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ noff = 0;
|
|
|
+ for (;;) {
|
|
|
+ if (noff >= (size_t)xsh_size)
|
|
|
+ break;
|
|
|
+ noff = donote(ms, nbuf, (size_t)noff,
|
|
|
+ (size_t)xsh_size, class, swap, 4,
|
|
|
+ &flags);
|
|
|
+ if (noff == 0)
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if ((lseek(fd, off, SEEK_SET)) == (off_t)-1) {
|
|
|
+ free(nbuf);
|
|
|
+ file_badread(ms);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ free(nbuf);
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
- if (file_printf(ms, ", stripped") == -1)
|
|
|
+ if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
|
|
|
return -1;
|
|
|
return 0;
|
|
|
}
|
|
@@ -681,8 +770,9 @@ dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
int bufsize;
|
|
|
size_t offset, align;
|
|
|
off_t savedoffset;
|
|
|
+ int flags = 0;
|
|
|
|
|
|
- if (size != ph_size) {
|
|
|
+ if (size != xph_sizeof) {
|
|
|
if (file_printf(ms, ", corrupted program header size") == -1)
|
|
|
return -1;
|
|
|
return 0;
|
|
@@ -693,7 +783,7 @@ dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
}
|
|
|
|
|
|
for ( ; num; num--) {
|
|
|
- if (read(fd, ph_addr, ph_size) == -1) {
|
|
|
+ if (read(fd, xph_addr, xph_sizeof) == -1) {
|
|
|
file_badread(ms);
|
|
|
return -1;
|
|
|
}
|
|
@@ -702,7 +792,7 @@ dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- switch (ph_type) {
|
|
|
+ switch (xph_type) {
|
|
|
case PT_DYNAMIC:
|
|
|
linking_style = "dynamically";
|
|
|
break;
|
|
@@ -710,7 +800,7 @@ dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
shared_libraries = " (uses shared libs)";
|
|
|
break;
|
|
|
case PT_NOTE:
|
|
|
- if ((align = ph_align) & 0x80000000) {
|
|
|
+ if ((align = xph_align) & 0x80000000) {
|
|
|
if (file_printf(ms,
|
|
|
", invalid note alignment 0x%lx",
|
|
|
(unsigned long)align) == -1)
|
|
@@ -721,13 +811,13 @@ dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
* This is a PT_NOTE section; loop through all the notes
|
|
|
* in the section.
|
|
|
*/
|
|
|
- if (lseek(fd, (off_t) ph_offset, SEEK_SET)
|
|
|
+ if (lseek(fd, (off_t)xph_offset, SEEK_SET)
|
|
|
== (off_t)-1) {
|
|
|
file_badseek(ms);
|
|
|
return -1;
|
|
|
}
|
|
|
- bufsize = read(fd, nbuf, ((ph_filesz < sizeof(nbuf)) ?
|
|
|
- ph_filesz : sizeof(nbuf)));
|
|
|
+ bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
|
|
|
+ xph_filesz : sizeof(nbuf)));
|
|
|
if (bufsize == -1) {
|
|
|
file_badread(ms);
|
|
|
return -1;
|
|
@@ -737,7 +827,8 @@ dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
|
|
|
if (offset >= (size_t)bufsize)
|
|
|
break;
|
|
|
offset = donote(ms, nbuf, offset,
|
|
|
- (size_t)bufsize, class, swap, align);
|
|
|
+ (size_t)bufsize, class, swap, align,
|
|
|
+ &flags);
|
|
|
if (offset == 0)
|
|
|
break;
|
|
|
}
|
|
@@ -766,22 +857,24 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
|
|
|
int class;
|
|
|
int swap;
|
|
|
|
|
|
- /*
|
|
|
- * If we cannot seek, it must be a pipe, socket or fifo.
|
|
|
- */
|
|
|
- if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
|
|
|
- fd = file_pipe2file(ms, fd, buf, nbytes);
|
|
|
+ /* Return if it's not ELF (so we avoid pipe2file unless needed) */
|
|
|
+ if (buf[EI_MAG0] != ELFMAG0
|
|
|
+ || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
|
|
|
+ || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
|
|
|
+ return 0;
|
|
|
|
|
|
/*
|
|
|
* ELF executables have multiple section headers in arbitrary
|
|
|
* file locations and thus file(1) cannot determine it from easily.
|
|
|
* Instead we traverse thru all section headers until a symbol table
|
|
|
- * one is found or else the binary is stripped.
|
|
|
+ * one is found or else the binary is stripped. So whole file is needed
|
|
|
*/
|
|
|
- if (buf[EI_MAG0] != ELFMAG0
|
|
|
- || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
|
|
|
- || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
|
|
|
- return 0;
|
|
|
+
|
|
|
+ /*
|
|
|
+ * If we cannot seek, it must be a pipe, socket or fifo.
|
|
|
+ */
|
|
|
+ if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
|
|
|
+ fd = file_pipe2file(ms, fd, buf, nbytes);
|
|
|
|
|
|
|
|
|
class = buf[4];
|
|
@@ -837,11 +930,7 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
|
|
|
if (getu16(swap, elfhdr.e_type) == ET_CORE) {
|
|
|
#ifdef ELFCORE
|
|
|
if (dophn_core(ms, class, swap, fd,
|
|
|
-#ifdef USE_ARRAY_FOR_64BIT_TYPES
|
|
|
- (off_t)getu32(swap, elfhdr.e_phoff[1]),
|
|
|
-#else
|
|
|
- (off_t)getu64(swap, elfhdr.e_phoff),
|
|
|
-#endif
|
|
|
+ (off_t)elf_getu64(swap, elfhdr.e_phoff),
|
|
|
getu16(swap, elfhdr.e_phnum),
|
|
|
(size_t)getu16(swap, elfhdr.e_phentsize)) == -1)
|
|
|
return -1;
|
|
@@ -851,22 +940,14 @@ file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
|
|
|
} else {
|
|
|
if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
|
|
|
if (dophn_exec(ms, class, swap, fd,
|
|
|
-#ifdef USE_ARRAY_FOR_64BIT_TYPES
|
|
|
- (off_t)getu32(swap, elfhdr.e_phoff[1]),
|
|
|
-#else
|
|
|
- (off_t)getu64(swap, elfhdr.e_phoff),
|
|
|
-#endif
|
|
|
+ (off_t)elf_getu64(swap, elfhdr.e_phoff),
|
|
|
getu16(swap, elfhdr.e_phnum),
|
|
|
(size_t)getu16(swap, elfhdr.e_phentsize))
|
|
|
== -1)
|
|
|
return -1;
|
|
|
}
|
|
|
if (doshn(ms, class, swap, fd,
|
|
|
-#ifdef USE_ARRAY_FOR_64BIT_TYPES
|
|
|
- (off_t)getu32(swap, elfhdr.e_shoff[1]),
|
|
|
-#else
|
|
|
- (off_t)getu64(swap, elfhdr.e_shoff),
|
|
|
-#endif
|
|
|
+ (off_t)elf_getu64(swap, elfhdr.e_shoff),
|
|
|
getu16(swap, elfhdr.e_shnum),
|
|
|
(size_t)getu16(swap, elfhdr.e_shentsize)) == -1)
|
|
|
return -1;
|