#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: nlist_coff.c,v 1.8 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_COFF
#include <sys/exec_coff.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_coff(name, db)
const char *name;
DB *db;
{
struct coff_filehdr *filehdrp;
struct coff_aouthdr *aouthdrp;
struct stat st;
struct nlist nbuf;
DBT key, data;
char *mappedfile, *symname, *nsymname, *fsymname;
size_t mappedsize, symnamesize, fsymnamesize;
u_long symhdroff, extrstroff;
u_long symhdrsize, i, nesyms;
int fd, rv;
struct external_syment *syment;
u_long soff;
u_long val;
char snamebuf[16];
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 *filehdrp))
BADUNMAP;
filehdrp = (struct coff_filehdr *)&mappedfile[0];
if (COFF_BADMAG(filehdrp))
BADUNMAP;
aouthdrp = (struct coff_aouthdr *)
&mappedfile[sizeof(struct coff_filehdr)];
symhdroff = filehdrp->f_symptr;
symhdrsize = filehdrp->f_nsyms;
extrstroff = symhdroff + symhdrsize*COFF_ES_SYMENTSZ;
#ifdef DEGBU
printf("sizeof syment = %d\n",sizeof(struct external_syment ));
printf("symhdroff = 0x%lx,symhdrsize=%ld,stroff = 0x%lx",
symhdroff,symhdrsize, extrstroff);
#endif
if (symhdrsize == 0)
badfmt("stripped");
data.data = (u_char *)&nbuf;
data.size = sizeof(nbuf);
symnamesize = 1024;
if ((symname = malloc(symnamesize)) == NULL) {
warn("malloc");
punt();
}
nesyms = filehdrp->f_nsyms;
for (i = 0; i < nesyms; i++) {
syment = (struct external_syment *)&mappedfile[symhdroff +
COFF_ES_SYMENTSZ*i];
if(syment->e_sclass[0] != 2){
continue;
}
if(syment->e.e.e_zeroes[0]){
if( syment->e.e_name[COFF_ES_SYMNMLEN-1] ){
memcpy( snamebuf, syment->e.e_name,
COFF_ES_SYMNMLEN);
snamebuf[COFF_ES_SYMNMLEN] = '\0';
fsymname = snamebuf;
}
else{
fsymname = syment->e.e_name ;
}
fsymnamesize = strlen(fsymname) + 1;
#ifdef DEBUG
printf("%s\n",fsymname );
#endif
}
else{
memcpy(&soff, syment->e.e.e_offset, sizeof(long));
fsymname = &mappedfile[extrstroff+soff];
fsymnamesize = strlen(fsymname) + 1;
#ifdef DEBUG
printf("*%s\n",fsymname );
#endif
}
while (symnamesize < fsymnamesize + 1) {
if ((nsymname = realloc(symname, symnamesize * 2)) == NULL){
warn("malloc");
punt();
}
symname = nsymname;
symnamesize *= 2;
}
#if 0
strlcpy(symname, "_", symnamesize);
strlcat(symname, fsymname, symnamesize);
#else
strlcpy(symname, fsymname, symnamesize);
#endif
key.data = symname;
key.size = strlen((char *)key.data);
memcpy(&val, syment->e_value, sizeof( long ));
nbuf.n_value = val;
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;
char *tmpcp;
struct coff_scnhdr *sh;
int i;
key.data = (u_char *)VRS_KEY;
key.size = sizeof(VRS_KEY) - 1;
vma = nbuf.n_value;
#ifdef DEBUG
printf("vma = %lx,tstart = %lx, dstart=%lx\n",
vma, aouthdrp->a_tstart,
aouthdrp->a_dstart);
printf("tsize = %lx, dsize=%lx\n",
aouthdrp->a_tsize,
aouthdrp->a_dsize);
#endif
if (aouthdrp->a_tstart <= vma &&
vma < (aouthdrp->a_tstart + aouthdrp->a_tsize)){
for(i=0;i<filehdrp->f_nscns;i++){
sh = (struct coff_scnhdr *)
&mappedfile[COFF_HDR_SIZE+
i*sizeof(struct
coff_scnhdr)];
if( sh->s_flags == COFF_STYP_TEXT ){
break;
}
}
vma = vma - sh->s_vaddr + sh->s_scnptr;
}
else if (aouthdrp->a_dstart <= vma &&
vma < (aouthdrp->a_dstart + aouthdrp->a_dsize)){
for(i=0;i<filehdrp->f_nscns;i++){
sh = (struct coff_scnhdr *)
&mappedfile[COFF_HDR_SIZE+
i*sizeof(struct
coff_scnhdr)];
if( sh->s_flags == COFF_STYP_DATA ){
break;
}
}
vma = vma - sh->s_vaddr + sh->s_scnptr;
}
else {
warn("version string neither text nor data");
punt();
}
data.data = strdup(&mappedfile[vma]);
#ifdef DEBUG
printf("vma = %lx,version = %s\n",
vma, (char *)data.data);
#endif
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