#ifdef BUILTIN_ELF
#include <sys/types.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "readelf.h"
#include "file.h"
static void
doshn(fd, off, num, size, buf)
int fd;
off_t off;
int num;
size_t size;
char *buf;
{
Elf32_Shdr *sh = (Elf32_Shdr *) buf;
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
for ( ; num; num--) {
if (read(fd, buf, size) == -1)
error("read failed (%s).\n", strerror(errno));
if (sh->sh_type == SHT_SYMTAB) {
(void) printf (", not stripped");
return;
}
}
(void) printf (", stripped");
}
static void
dophn_exec(fd, off, num, size, buf)
int fd;
off_t off;
int num;
size_t size;
char *buf;
{
Elf32_Phdr *ph = (Elf32_Phdr *) buf;
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
for ( ; num; num--) {
if (read(fd, buf, size) == -1)
error("read failed (%s).\n", strerror(errno));
if (ph->p_type == PT_INTERP) {
printf(", dynamically linked");
return;
}
}
printf(", statically linked");
}
size_t prpsoffsets[] = {
100,
32,
};
#define NOFFSETS (sizeof prpsoffsets / sizeof prpsoffsets[0])
static void
dophn_core(fd, off, num, size, buf)
int fd;
off_t off;
int num;
size_t size;
char *buf;
{
Elf32_Phdr *ph = (Elf32_Phdr *) buf;
Elf32_Nhdr *nh;
size_t offset, noffset, reloffset;
unsigned char c;
int i, j;
char nbuf[BUFSIZ];
int bufsize;
for ( ; num; num--) {
if (lseek(fd, off, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
if (read(fd, buf, size) == -1)
error("read failed (%s).\n", strerror(errno));
off += size;
if (ph->p_type != PT_NOTE)
continue;
if (lseek(fd, ph->p_offset, SEEK_SET) == -1)
error("lseek failed (%s).\n", strerror(errno));
bufsize = read(fd, nbuf, BUFSIZ);
if (bufsize == -1)
error("read failed (%s).\n", strerror(errno));
offset = 0;
for (;;) {
if (offset >= bufsize)
break;
nh = (Elf32_Nhdr *)&nbuf[offset];
offset += sizeof *nh;
if (nh->n_type != NT_PRPSINFO) {
offset += nh->n_namesz;
offset = ((offset + 3)/4)*4;
offset += nh->n_descsz;
offset = ((offset + 3)/4)*4;
continue;
}
if (offset + nh->n_namesz >= bufsize) {
break;
}
if (nh->n_namesz != 5
|| strcmp(&nbuf[offset], "CORE") != 0)
continue;
offset += nh->n_namesz;
offset = ((offset + 3)/4)*4;
for (i = 0; i < NOFFSETS; i++) {
reloffset = prpsoffsets[i];
noffset = offset + reloffset;
for (j = 0; j < 16;
j++, noffset++, reloffset++) {
if (noffset >= bufsize)
return;
if (reloffset >= nh->n_descsz)
goto tryanother;
c = nbuf[noffset];
if (c != '\0' && !isprint(c))
goto tryanother;
}
printf(", from '%.16s'",
&nbuf[offset + prpsoffsets[i]]);
return;
tryanother:
;
}
offset += nh->n_descsz;
offset = ((offset + 3)/4)*4;
}
}
}
void
tryelf(fd, buf, nbytes)
int fd;
char *buf;
int nbytes;
{
union {
int32 l;
char c[sizeof (int32)];
} u;
if (buf[EI_MAG0] != ELFMAG0 || buf[EI_MAG1] != ELFMAG1
|| buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
return;
if (buf[4] == ELFCLASS32) {
Elf32_Ehdr elfhdr;
if (nbytes <= sizeof (Elf32_Ehdr))
return;
u.l = 1;
(void) memcpy(&elfhdr, buf, sizeof elfhdr);
if ((u.c[sizeof(long) - 1] + 1) == elfhdr.e_ident[5]) {
if (elfhdr.e_type == ET_CORE)
dophn_core(fd, elfhdr.e_phoff, elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
else {
if (elfhdr.e_type == ET_EXEC) {
dophn_exec(fd, elfhdr.e_phoff,
elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
}
doshn(fd, elfhdr.e_shoff, elfhdr.e_shnum,
elfhdr.e_shentsize, buf);
}
}
return;
}
if (buf[4] == ELFCLASS64) {
Elf64_Ehdr elfhdr;
if (nbytes <= sizeof (Elf64_Ehdr))
return;
u.l = 1;
(void) memcpy(&elfhdr, buf, sizeof elfhdr);
if ((u.c[sizeof(long) - 1] + 1) == elfhdr.e_ident[5]) {
#ifdef notyet
if (elfhdr.e_type == ET_CORE)
dophn_core(fd, elfhdr.e_phoff, elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
else
#endif
{
#ifdef notyet
if (elfhdr.e_type == ET_EXEC) {
dophn_exec(fd, elfhdr.e_phoff,
elfhdr.e_phnum,
elfhdr.e_phentsize, buf);
}
#endif
doshn(fd, elfhdr.e_shoff, elfhdr.e_shnum,
elfhdr.e_shentsize, buf);
}
}
return;
}
}
#endif