#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: nlist_ecoff.c,v 1.11 2003/11/12 13:31:07 grant Exp $");
#endif
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <a.out.h>
#include <db.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <kvm.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "extern.h"
#ifdef NLIST_ECOFF
#include <sys/exec_ecoff.h>
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
create_knlist_ecoff(name, db)
const char *name;
DB *db;
{
struct ecoff_exechdr *exechdrp;
struct ecoff_symhdr *symhdrp;
struct ecoff_extsym *esyms;
struct stat st;
struct nlist nbuf;
DBT key, data;
char *mappedfile, *symname, *nsymname, *fsymname, *tmpcp;
size_t mappedsize, symnamesize, fsymnamesize;
u_long symhdroff, extrstroff;
u_long symhdrsize, i, nesyms;
int fd, rv;
rv = -1;
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;
mappedsize = st.st_size;
mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_FILE|MAP_PRIVATE,
fd, 0);
if (mappedfile == (char *)-1)
BAD;
if (check(0, sizeof *exechdrp))
BADUNMAP;
exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
if (ECOFF_BADMAG(exechdrp))
BADUNMAP;
symhdroff = exechdrp->f.f_symptr;
symhdrsize = exechdrp->f.f_nsyms;
if (symhdrsize == 0)
badfmt("stripped");
if (check(symhdroff, sizeof *symhdrp) || sizeof *symhdrp != symhdrsize)
badfmt("bogus symbol table");
symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
nesyms = symhdrp->esymMax;
if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
badfmt("bogus external symbol list");
esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
extrstroff = symhdrp->cbSsExtOffset;
data.data = (u_char *)&nbuf;
data.size = sizeof(nbuf);
symnamesize = 1024;
if ((symname = malloc(symnamesize)) == NULL) {
warn("malloc");
punt();
}
for (i = 0; i < nesyms; i++) {
fsymname = &mappedfile[extrstroff + esyms[i].es_strindex];
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 = esyms[i].es_value;
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) {
unsigned long vma;
key.data = (u_char *)VRS_KEY;
key.size = sizeof(VRS_KEY) - 1;
vma = nbuf.n_value;
if (exechdrp->a.text_start <= vma &&
vma < (exechdrp->a.text_start + exechdrp->a.tsize))
vma = vma - exechdrp->a.text_start +
ECOFF_TXTOFF(exechdrp);
else if (exechdrp->a.data_start <= vma &&
vma < (exechdrp->a.data_start + exechdrp->a.dsize))
vma = vma - exechdrp->a.data_start +
ECOFF_DATOFF(exechdrp);
else {
warn("version string neither text nor data");
punt();
}
data.data = strdup(&mappedfile[vma]);
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:
munmap(mappedfile, mappedsize);
out:
return (rv);
}
#endif