#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: nlist_elf32.c,v 1.19 2010/08/28 21:30:03 joerg Exp $");
#endif
#ifndef ELFSIZE
#define ELFSIZE 32
#endif
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <sys/ksyms.h>
#include <a.out.h>
#include <db.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <kvm.h>
#include <limits.h>
#include <paths.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#if defined(NLIST_ELF32) || defined(NLIST_ELF64)
#include <sys/exec_elf.h>
#endif
#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
(defined(NLIST_ELF64) && (ELFSIZE == 64))
typedef struct nlist NLIST;
#define _strx n_un.n_strx
#define _name n_un.n_name
#define badfmt(str) \
do { \
warnx("%s: %s: %s", kfile, str, strerror(EFTYPE)); \
punt(); \
} while (0)
#define check(off, size) ((off < 0) || (off + size > mappedsize))
#define BAD do { rv = -1; goto out; } while (0)
#define BADUNMAP do { rv = -1; goto unmap; } while (0)
static const char *kfile;
int
ELFNAMEEND(create_knlist)(name, db)
const char *name;
DB *db;
{
struct stat st;
struct nlist nbuf;
DBT key, data;
char *mappedfile, *symname, *nsymname, *fsymname, *tmpcp, *strtab;
size_t mappedsize, symnamesize, fsymnamesize;
Elf_Ehdr *ehdrp;
Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
Elf_Sym *symp;
Elf_Off shdr_off;
Elf_Word shdr_size;
#if (ELFSIZE == 32)
Elf32_Half nshdr;
#elif (ELFSIZE == 64)
Elf64_Word nshdr;
#endif
unsigned long i, nsyms;
int fd, rv, malloced = 0, isksyms;
rv = -1;
#ifdef __GNUC__
symshdrp = NULL;
symstrshdrp = NULL;
#endif
kfile = name;
if ((fd = open(name, O_RDONLY, 0)) < 0) {
warn("%s", kfile);
punt();
}
if (fstat(fd, &st) < 0) {
warn("%s", kfile);
punt();
}
if (st.st_size > SIZE_T_MAX)
BAD;
mappedfile = MAP_FAILED;
mappedsize = st.st_size;
isksyms = S_ISCHR(st.st_mode) &&
strncmp(name, _PATH_KSYMS, sizeof(_PATH_KSYMS)) == 0;
if (mappedsize == 0) {
if (!isksyms)
BAD;
} else {
mappedfile = mmap(NULL, mappedsize, PROT_READ,
MAP_PRIVATE|MAP_FILE, fd, 0);
}
if (mappedfile == MAP_FAILED) {
int allocsiz, readsz;
if (isksyms != 0) {
if (ioctl(fd, KIOCGSIZE, &allocsiz) < 0)
BAD;
mappedsize = allocsiz;
} else
allocsiz = mappedsize;
if ((mappedfile = malloc(mappedsize)) == NULL)
BAD;
malloced = 1;
if ((readsz = read(fd, mappedfile, mappedsize)) < 0)
BADUNMAP;
if (readsz != mappedsize)
BADUNMAP;
}
if (check(0, sizeof *ehdrp))
BADUNMAP;
ehdrp = (Elf_Ehdr *)&mappedfile[0];
if (memcmp(ehdrp->e_ident, ELFMAG, SELFMAG) != 0 ||
ehdrp->e_ident[EI_CLASS] != ELFCLASS)
BADUNMAP;
switch (ehdrp->e_machine) {
ELFDEFNNAME(MACHDEP_ID_CASES)
default:
BADUNMAP;
}
nshdr = ehdrp->e_shnum;
shdr_off = ehdrp->e_shoff;
shdr_size = ehdrp->e_shentsize * nshdr;
if (check(shdr_off, shdr_size) ||
(sizeof *shdrp != ehdrp->e_shentsize))
badfmt("bogus section header table");
shdrp = (Elf_Shdr *)&mappedfile[shdr_off];
for (i = 0; i < nshdr; i++) {
if (shdrp[i].sh_type == SHT_SYMTAB) {
symshdrp = &shdrp[i];
symstrshdrp = &shdrp[shdrp[i].sh_link];
}
}
if (symshdrp == NULL)
badfmt("no symbol section header found");
if (symshdrp->sh_offset == 0)
badfmt("stripped");
if (check(symshdrp->sh_offset, symshdrp->sh_size))
badfmt("bogus symbol section header");
if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
badfmt("bogus symbol string section header");
symp = (Elf_Sym *)&mappedfile[symshdrp->sh_offset];
nsyms = symshdrp->sh_size / sizeof(*symp);
strtab = &mappedfile[symstrshdrp->sh_offset];
data.data = (u_char *)&nbuf;
data.size = sizeof(nbuf);
symnamesize = 1024;
if ((symname = malloc(symnamesize)) == NULL) {
warn("malloc");
punt();
}
for (i = 0; i < nsyms; i++) {
if (symp[i].st_name == 0)
continue;
fsymname = &strtab[symp[i].st_name];
fsymnamesize = strlen(fsymname) + 1;
while (symnamesize < fsymnamesize + 1) {
if ((nsymname = realloc(symname, symnamesize * 2)) == NULL) {
warn("malloc");
punt();
}
symname = nsymname;
symnamesize *= 2;
}
strlcpy(symname, "_", symnamesize);
strlcat(symname, fsymname, symnamesize);
key.data = symname;
key.size = strlen((char *)key.data);
nbuf.n_value = symp[i].st_value;
switch (ELFDEFNNAME(ST_TYPE)(symp[i].st_info)) {
default:
case STT_NOTYPE:
nbuf.n_type = N_UNDF;
break;
case STT_OBJECT:
nbuf.n_type = N_DATA;
break;
case STT_FUNC:
nbuf.n_type = N_TEXT;
break;
case STT_FILE:
nbuf.n_type = N_FN;
break;
}
if (ELFDEFNNAME(ST_BIND)(symp[i].st_info) != STB_LOCAL)
nbuf.n_type |= N_EXT;
nbuf.n_desc = 0;
nbuf.n_other = 0;
if (db->put(db, &key, &data, 0)) {
warn("record enter");
punt();
}
if (strcmp((char *)key.data, VRS_SYM) == 0) {
key.data = (u_char *)VRS_KEY;
key.size = sizeof(VRS_KEY) - 1;
if (isksyms) {
size_t sz;
int mib[2];
char *kv;
mib[0] = CTL_KERN;
mib[1] = KERN_VERSION;
if (sysctl(mib, 2, NULL, &sz, NULL, 0) == -1) {
warn("sysctl version size");
punt();
}
if ((kv = malloc(sz)) == NULL) {
warn("malloc version string");
punt();
}
if (sysctl(mib, 2, kv, &sz, NULL, 0) == -1) {
warn("sysctl version string");
punt();
}
data.data = kv;
} else
data.data = strdup(&mappedfile[nbuf.n_value -
shdrp[symp[i].st_shndx].sh_addr +
shdrp[symp[i].st_shndx].sh_offset]);
if ((tmpcp = strchr(data.data, '\n')) != NULL)
*tmpcp = '\0';
data.size = strlen((char *)data.data);
if (db->put(db, &key, &data, 0)) {
warn("record enter");
punt();
}
free(data.data);
data.data = (u_char *)&nbuf;
data.size = sizeof(nbuf);
}
}
rv = 0;
unmap:
if (malloced)
free(mappedfile);
else
munmap(mappedfile, mappedsize);
out:
return (rv);
}
#endif