NetBSD/usr.bin/file/readelf.c

535 lines
12 KiB
C
Raw Normal View History

2000-11-24 02:21:14 +03:00
/* $NetBSD: readelf.c,v 1.9 2000/11/23 23:21:15 pooka Exp $ */
1998-01-09 11:03:16 +03:00
1999-11-01 20:39:26 +03:00
#include "file.h"
1996-10-06 00:20:24 +04:00
#ifdef BUILTIN_ELF
1997-01-28 03:49:36 +03:00
#include <sys/types.h>
#include <string.h>
1996-10-06 00:20:24 +04:00
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
1999-11-01 20:39:26 +03:00
#ifdef HAVE_UNISTD_H
1996-10-06 00:20:24 +04:00
#include <unistd.h>
1999-11-01 20:39:26 +03:00
#endif
1996-10-06 00:20:24 +04:00
#include <errno.h>
#include "readelf.h"
1998-09-20 19:27:15 +04:00
#include <sys/cdefs.h>
#ifndef lint
#if 0
2000-11-24 02:21:14 +03:00
FILE_RCSID("@(#)Id: readelf.c,v 1.17 2000/08/05 19:00:12 christos Exp ")
1998-09-20 19:27:15 +04:00
#else
2000-11-24 02:21:14 +03:00
__RCSID("$NetBSD: readelf.c,v 1.9 2000/11/23 23:21:15 pooka Exp $");
1998-09-20 19:27:15 +04:00
#endif
#endif
#ifdef ELFCORE
1999-11-01 20:39:26 +03:00
static void dophn_core __P((int, int, int, off_t, int, size_t));
1998-09-20 19:27:15 +04:00
#endif
1999-11-01 20:39:26 +03:00
static void dophn_exec __P((int, int, int, off_t, int, size_t));
static void doshn __P((int, int, int, off_t, int, size_t));
static uint16_t getu16 __P((int, int));
static uint32_t getu32 __P((int, uint32_t));
static uint64_t getu64 __P((int, uint64_t));
static uint16_t
getu16(swap, value)
int swap;
uint16_t value;
{
union {
uint16_t ui;
char c[2];
} retval, tmpval;
if (swap) {
tmpval.ui = value;
retval.c[0] = tmpval.c[1];
retval.c[1] = tmpval.c[0];
return retval.ui;
} else
return value;
}
static uint32_t
getu32(swap, value)
int swap;
uint32_t value;
{
union {
uint32_t ui;
char c[4];
} retval, tmpval;
if (swap) {
tmpval.ui = value;
retval.c[0] = tmpval.c[3];
retval.c[1] = tmpval.c[2];
retval.c[2] = tmpval.c[1];
retval.c[3] = tmpval.c[0];
return retval.ui;
} else
return value;
}
static uint64_t
getu64(swap, value)
int swap;
uint64_t value;
{
union {
uint64_t ui;
char c[8];
} retval, tmpval;
if (swap) {
tmpval.ui = value;
retval.c[0] = tmpval.c[7];
retval.c[1] = tmpval.c[6];
retval.c[2] = tmpval.c[5];
retval.c[3] = tmpval.c[4];
retval.c[4] = tmpval.c[3];
retval.c[5] = tmpval.c[2];
retval.c[6] = tmpval.c[1];
retval.c[7] = tmpval.c[0];
return retval.ui;
} else
return value;
}
#define sh_addr (class == ELFCLASS32 \
? (void *) &sh32 \
: (void *) &sh64)
#define shs_type (class == ELFCLASS32 \
? getu32(swap, sh32.sh_type) \
: getu32(swap, sh64.sh_type))
#define ph_addr (class == ELFCLASS32 \
? (void *) &ph32 \
: (void *) &ph64)
#define ph_type (class == ELFCLASS32 \
? getu32(swap, ph32.p_type) \
: getu32(swap, ph64.p_type))
#define ph_offset (class == ELFCLASS32 \
? getu32(swap, ph32.p_offset) \
: getu64(swap, ph64.p_offset))
#define nh_size (class == ELFCLASS32 \
? sizeof *nh32 \
: sizeof *nh64)
#define nh_type (class == ELFCLASS32 \
? getu32(swap, nh32->n_type) \
: getu32(swap, nh64->n_type))
#define nh_namesz (class == ELFCLASS32 \
? getu32(swap, nh32->n_namesz) \
: getu32(swap, nh64->n_namesz))
#define nh_descsz (class == ELFCLASS32 \
? getu32(swap, nh32->n_descsz) \
: getu32(swap, nh64->n_descsz))
#define prpsoffsets(i) (class == ELFCLASS32 \
? prpsoffsets32[i] \
: prpsoffsets64[i])
1998-09-20 19:27:15 +04:00
1996-10-06 00:20:24 +04:00
static void
1999-11-01 20:39:26 +03:00
doshn(class, swap, fd, off, num, size)
int class;
int swap;
1996-10-06 00:20:24 +04:00
int fd;
off_t off;
int num;
size_t size;
{
1999-11-01 20:39:26 +03:00
Elf32_Shdr sh32;
Elf64_Shdr sh64;
1996-10-06 00:20:24 +04:00
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
for ( ; num; num--) {
1999-11-01 20:39:26 +03:00
if (read(fd, sh_addr, size) == -1)
1996-10-06 00:20:24 +04:00
error("read failed (%s).\n", strerror(errno));
1999-11-01 20:39:26 +03:00
if (shs_type == SHT_SYMTAB /* || shs_type == SHT_DYNSYM */) {
1997-01-28 03:49:36 +03:00
(void) printf (", not stripped");
1996-10-06 00:20:24 +04:00
return;
1997-01-28 03:49:36 +03:00
}
1996-10-06 00:20:24 +04:00
}
(void) printf (", stripped");
}
1997-01-28 03:49:36 +03:00
/*
* Look through the program headers of an executable image, searching
* for a PT_INTERP section; if one is found, it's dynamically linked,
* otherwise it's statically linked.
*/
1996-10-06 00:20:24 +04:00
static void
1999-11-01 20:39:26 +03:00
dophn_exec(class, swap, fd, off, num, size)
int class;
int swap;
1996-10-06 00:20:24 +04:00
int fd;
off_t off;
int num;
size_t size;
{
1999-11-01 20:39:26 +03:00
Elf32_Phdr ph32;
Elf64_Phdr ph64;
1998-09-20 19:27:15 +04:00
char *linking_style = "statically";
char *shared_libraries = "";
1996-10-06 00:20:24 +04:00
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
1997-01-28 03:49:36 +03:00
for ( ; num; num--) {
1999-11-01 20:39:26 +03:00
if (read(fd, ph_addr, size) == -1)
1997-01-28 03:49:36 +03:00
error("read failed (%s).\n", strerror(errno));
1998-09-20 19:27:15 +04:00
1999-11-01 20:39:26 +03:00
switch (ph_type) {
1998-09-20 19:27:15 +04:00
case PT_DYNAMIC:
linking_style = "dynamically";
break;
case PT_INTERP:
shared_libraries = " (uses shared libs)";
break;
1997-01-28 03:49:36 +03:00
}
}
1998-09-20 19:27:15 +04:00
printf(", %s linked%s", linking_style, shared_libraries);
1997-01-28 03:49:36 +03:00
}
1998-09-20 19:27:15 +04:00
#ifdef ELFCORE
1999-11-01 20:39:26 +03:00
size_t prpsoffsets32[] = {
2000-09-22 20:34:59 +04:00
8, /* FreeBSD */
28, /* Linux 2.0.36 */
32, /* Linux (I forget which kernel version) */
1998-09-20 19:27:15 +04:00
84, /* SunOS 5.x */
1997-01-28 03:49:36 +03:00
};
1999-11-01 20:39:26 +03:00
size_t prpsoffsets64[] = {
120, /* SunOS 5.x, 64-bit */
};
#define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
#define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
#define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
1997-01-28 03:49:36 +03:00
/*
* Look through the program headers of an executable image, searching
2000-09-22 20:34:59 +04:00
* for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
* "FreeBSD"; if one is found, try looking in various places in its
* contents for a 16-character string containing only printable
* characters - if found, that string should be the name of the program
* that dropped core. Note: right after that 16-character string is,
* at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
* Linux, a longer string (80 characters, in 5.x, probably other
* SVR4-flavored systems, and Linux) containing the start of the
* command line for that program.
*
* The signal number probably appears in a section of type NT_PRSTATUS,
* but that's also rather OS-dependent, in ways that are harder to
* dissect with heuristics, so I'm not bothering with the signal number.
* (I suppose the signal number could be of interest in situations where
* you don't have the binary of the program that dropped core; if you
* *do* have that binary, the debugger will probably tell you what
* signal it was.)
1997-01-28 03:49:36 +03:00
*/
static void
1999-11-01 20:39:26 +03:00
dophn_core(class, swap, fd, off, num, size)
int class;
int swap;
1997-01-28 03:49:36 +03:00
int fd;
off_t off;
int num;
size_t size;
{
1999-11-01 20:39:26 +03:00
Elf32_Phdr ph32;
Elf32_Nhdr *nh32;
Elf64_Phdr ph64;
Elf64_Nhdr *nh64;
2000-09-22 20:34:59 +04:00
size_t offset, nameoffset, noffset, reloffset;
1997-01-28 03:49:36 +03:00
unsigned char c;
int i, j;
char nbuf[BUFSIZ];
int bufsize;
2000-09-22 20:34:59 +04:00
int is_freebsd;
1997-01-28 03:49:36 +03:00
2000-09-22 20:34:59 +04:00
/*
* Loop through all the program headers.
*/
1996-10-06 00:20:24 +04:00
for ( ; num; num--) {
1997-01-28 03:49:36 +03:00
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
1999-11-01 20:39:26 +03:00
if (read(fd, ph_addr, size) == -1)
1996-10-06 00:20:24 +04:00
error("read failed (%s).\n", strerror(errno));
1997-01-28 03:49:36 +03:00
off += size;
1999-11-01 20:39:26 +03:00
if (ph_type != PT_NOTE)
1996-10-06 00:20:24 +04:00
continue;
2000-09-22 20:34:59 +04:00
/*
* This is a PT_NOTE section; loop through all the notes
* in the section.
*/
1999-11-01 20:39:26 +03:00
if (lseek(fd, (off_t) ph_offset, SEEK_SET) == -1)
1996-10-06 00:20:24 +04:00
error("lseek failed (%s).\n", strerror(errno));
1997-01-28 03:49:36 +03:00
bufsize = read(fd, nbuf, BUFSIZ);
if (bufsize == -1)
2000-05-15 02:53:37 +04:00
error(": " "read failed (%s).\n", strerror(errno));
1997-01-28 03:49:36 +03:00
offset = 0;
for (;;) {
if (offset >= bufsize)
break;
1999-11-01 20:39:26 +03:00
if (class == ELFCLASS32)
nh32 = (Elf32_Nhdr *)&nbuf[offset];
else
nh64 = (Elf64_Nhdr *)&nbuf[offset];
offset += nh_size;
1997-01-28 03:49:36 +03:00
/*
* If this note isn't an NT_PRPSINFO note, it's
* not what we're looking for.
*/
1999-11-01 20:39:26 +03:00
if (nh_type != NT_PRPSINFO) {
offset += nh_namesz;
1997-01-28 03:49:36 +03:00
offset = ((offset + 3)/4)*4;
1999-11-01 20:39:26 +03:00
offset += nh_descsz;
1997-01-28 03:49:36 +03:00
offset = ((offset + 3)/4)*4;
continue;
}
/*
2000-09-22 20:34:59 +04:00
* Check whether this note has the name "CORE" or
* "FreeBSD".
1997-01-28 03:49:36 +03:00
*/
1999-11-01 20:39:26 +03:00
if (offset + nh_namesz >= bufsize) {
1997-01-28 03:49:36 +03:00
/*
* We're past the end of the buffer.
*/
break;
}
2000-09-22 20:34:59 +04:00
nameoffset = offset;
1999-11-01 20:39:26 +03:00
offset += nh_namesz;
1997-01-28 03:49:36 +03:00
offset = ((offset + 3)/4)*4;
/*
2000-09-22 20:34:59 +04:00
* Sigh. The 2.0.36 kernel in Debian 2.1, at
* least, doesn't correctly implement name
* sections, in core dumps, as specified by
* the "Program Linking" section of "UNIX(R) System
* V Release 4 Programmer's Guide: ANSI C and
* Programming Support Tools", because my copy
* clearly says "The first 'namesz' bytes in 'name'
* contain a *null-terminated* [emphasis mine]
* character representation of the entry's owner
* or originator", but the 2.0.36 kernel code
* doesn't include the terminating null in the
* name....
1997-01-28 03:49:36 +03:00
*/
2000-09-22 20:34:59 +04:00
if ((nh_namesz == 4 &&
strncmp(&nbuf[nameoffset], "CORE", 4) == 0) ||
(nh_namesz == 5 &&
strcmp(&nbuf[nameoffset], "CORE") == 0))
is_freebsd = 0;
else if ((nh_namesz == 8 &&
strcmp(&nbuf[nameoffset], "FreeBSD") == 0))
is_freebsd = 1;
else
continue;
if (nh_type == NT_PRPSINFO) {
/*
* 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++) {
reloffset = prpsoffsets(i);
noffset = offset + 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 >= bufsize)
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 >= nh_descsz)
goto tryanother;
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.
*/
#define isquote(c) (strchr("'\"`", (c)) != NULL)
if (!isprint(c) ||
isquote(c))
goto tryanother;
}
}
1997-01-28 03:49:36 +03:00
/*
2000-09-22 20:34:59 +04:00
* Well, that worked.
1997-01-28 03:49:36 +03:00
*/
2000-09-22 20:34:59 +04:00
printf(", from '%.16s'",
&nbuf[offset + prpsoffsets(i)]);
break;
1997-01-28 03:49:36 +03:00
2000-09-22 20:34:59 +04:00
tryanother:
;
1997-01-28 03:49:36 +03:00
}
2000-09-22 20:34:59 +04:00
break;
1997-01-28 03:49:36 +03:00
}
1999-11-01 20:39:26 +03:00
offset += nh_descsz;
1997-01-28 03:49:36 +03:00
offset = ((offset + 3)/4)*4;
}
1996-10-06 00:20:24 +04:00
}
}
1998-09-20 19:27:15 +04:00
#endif
1996-10-06 00:20:24 +04:00
void
tryelf(fd, buf, nbytes)
int fd;
1999-11-01 20:39:26 +03:00
unsigned char *buf;
1996-10-06 00:20:24 +04:00
int nbytes;
{
union {
1997-01-28 03:49:36 +03:00
int32 l;
char c[sizeof (int32)];
1996-10-06 00:20:24 +04:00
} u;
1999-11-01 20:39:26 +03:00
int class;
int swap;
1996-10-06 00:20:24 +04:00
/*
* 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.
*/
1998-09-20 19:27:15 +04:00
if (buf[EI_MAG0] != ELFMAG0
|| (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1997-01-28 03:49:36 +03:00
|| buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1996-10-06 00:20:24 +04:00
return;
1999-11-01 20:39:26 +03:00
class = buf[4];
if (class == ELFCLASS32) {
1996-10-06 00:20:24 +04:00
Elf32_Ehdr elfhdr;
if (nbytes <= sizeof (Elf32_Ehdr))
return;
u.l = 1;
(void) memcpy(&elfhdr, buf, sizeof elfhdr);
swap = (u.c[sizeof(int32) - 1] + 1) != elfhdr.e_ident[5];
1999-11-01 20:39:26 +03:00
if (getu16(swap, elfhdr.e_type) == ET_CORE)
1998-09-20 19:27:15 +04:00
#ifdef ELFCORE
1999-11-01 20:39:26 +03:00
dophn_core(class, swap,
fd,
getu32(swap, elfhdr.e_phoff),
getu16(swap, elfhdr.e_phnum),
getu16(swap, elfhdr.e_phentsize));
1998-09-20 19:27:15 +04:00
#else
1999-11-01 20:39:26 +03:00
;
1998-09-20 19:27:15 +04:00
#endif
1999-11-01 20:39:26 +03:00
else {
if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
dophn_exec(class, swap,
fd,
getu32(swap, elfhdr.e_phoff),
getu16(swap, elfhdr.e_phnum),
getu16(swap, elfhdr.e_phentsize));
1997-01-28 03:49:36 +03:00
}
1999-11-01 20:39:26 +03:00
doshn(class, swap,
fd,
getu32(swap, elfhdr.e_shoff),
getu16(swap, elfhdr.e_shnum),
getu16(swap, elfhdr.e_shentsize));
1996-10-06 00:20:24 +04:00
}
return;
}
1999-11-01 20:39:26 +03:00
if (class == ELFCLASS64) {
1996-10-06 00:20:24 +04:00
Elf64_Ehdr elfhdr;
if (nbytes <= sizeof (Elf64_Ehdr))
return;
u.l = 1;
(void) memcpy(&elfhdr, buf, sizeof elfhdr);
swap = (u.c[sizeof(int32) - 1] + 1) != elfhdr.e_ident[5];
1996-10-06 00:20:24 +04:00
1999-11-01 20:39:26 +03:00
if (getu16(swap, elfhdr.e_type) == ET_CORE)
1998-09-20 19:27:15 +04:00
#ifdef ELFCORE
1999-11-01 20:39:26 +03:00
dophn_core(class, swap,
fd,
#ifdef USE_ARRAY_FOR_64BIT_TYPES
getu32(swap, elfhdr.e_phoff[1]),
1998-09-20 19:27:15 +04:00
#else
1999-11-01 20:39:26 +03:00
getu64(swap, elfhdr.e_phoff),
1998-09-20 19:27:15 +04:00
#endif
1999-11-01 20:39:26 +03:00
getu16(swap, elfhdr.e_phnum),
getu16(swap, elfhdr.e_phentsize));
1998-09-20 19:27:15 +04:00
#else
1999-11-01 20:39:26 +03:00
;
1997-01-28 03:49:36 +03:00
#endif
1999-11-01 20:39:26 +03:00
else
{
if (getu16(swap, elfhdr.e_type) == ET_EXEC) {
dophn_exec(class, swap,
fd,
#ifdef USE_ARRAY_FOR_64BIT_TYPES
getu32(swap, elfhdr.e_phoff[1]),
1998-09-20 19:27:15 +04:00
#else
1999-11-01 20:39:26 +03:00
getu64(swap, elfhdr.e_phoff),
1998-09-20 19:27:15 +04:00
#endif
1999-11-01 20:39:26 +03:00
getu16(swap, elfhdr.e_phnum),
getu16(swap, elfhdr.e_phentsize));
}
doshn(class, swap,
fd,
#ifdef USE_ARRAY_FOR_64BIT_TYPES
getu32(swap, elfhdr.e_shoff[1]),
1998-09-20 19:27:15 +04:00
#else
1999-11-01 20:39:26 +03:00
getu64(swap, elfhdr.e_shoff),
1996-10-06 00:20:24 +04:00
#endif
1999-11-01 20:39:26 +03:00
getu16(swap, elfhdr.e_shnum),
getu16(swap, elfhdr.e_shentsize));
1996-10-06 00:20:24 +04:00
}
return;
}
}
#endif