1998-02-03 21:23:37 +03:00
|
|
|
/* $NetBSD: nlist_ecoff.c,v 1.6 1998/02/03 18:23:48 perry Exp $ */
|
1996-09-28 02:23:03 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1996-10-01 17:37:05 +04:00
|
|
|
* This product includes software developed by Christopher G. Demetriou
|
|
|
|
* for the NetBSD Project.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission
|
1996-09-28 02:23:03 +04:00
|
|
|
*
|
1996-10-01 17:37:05 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1996-09-28 02:23:03 +04:00
|
|
|
*/
|
|
|
|
|
1997-07-13 23:45:36 +04:00
|
|
|
#include <sys/cdefs.h>
|
1996-09-28 02:23:03 +04:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1998-02-03 21:23:37 +03:00
|
|
|
__RCSID("$NetBSD: nlist_ecoff.c,v 1.6 1998/02/03 18:23:48 perry Exp $");
|
1996-09-28 02:23:03 +04:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <a.out.h> /* for 'struct nlist' declaration */
|
|
|
|
|
|
|
|
#include "nlist_private.h"
|
|
|
|
#ifdef NLIST_ECOFF
|
|
|
|
#include <sys/exec_ecoff.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NLIST_ECOFF
|
|
|
|
#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)
|
|
|
|
|
|
|
|
int
|
|
|
|
__fdnlist_ecoff(fd, list)
|
1998-02-03 21:23:37 +03:00
|
|
|
int fd;
|
|
|
|
struct nlist *list;
|
1996-09-28 02:23:03 +04:00
|
|
|
{
|
|
|
|
struct nlist *p;
|
|
|
|
struct ecoff_exechdr *exechdrp;
|
|
|
|
struct ecoff_symhdr *symhdrp;
|
|
|
|
struct ecoff_extsym *esyms;
|
|
|
|
struct stat st;
|
|
|
|
char *mappedfile;
|
|
|
|
size_t mappedsize;
|
|
|
|
u_long symhdroff, extstroff;
|
|
|
|
u_int symhdrsize;
|
|
|
|
int rv, nent;
|
|
|
|
long i, nesyms;
|
|
|
|
|
1996-10-01 17:37:05 +04:00
|
|
|
rv = -1;
|
1996-09-28 02:23:03 +04:00
|
|
|
|
1996-10-01 17:37:05 +04:00
|
|
|
/*
|
|
|
|
* If we can't fstat() the file, something bad is going on.
|
|
|
|
*/
|
1996-09-28 02:23:03 +04:00
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
BAD;
|
1996-10-01 17:37:05 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Map the file in its entirety.
|
|
|
|
*/
|
1996-09-28 02:23:03 +04:00
|
|
|
if (st.st_size > SIZE_T_MAX) {
|
|
|
|
errno = EFBIG;
|
|
|
|
BAD;
|
|
|
|
}
|
|
|
|
mappedsize = st.st_size;
|
1997-12-15 08:43:50 +03:00
|
|
|
mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_COPY|MAP_FILE,
|
|
|
|
fd, 0);
|
1996-09-28 02:23:03 +04:00
|
|
|
if (mappedfile == (char *)-1)
|
|
|
|
BAD;
|
|
|
|
|
1996-10-01 17:37:05 +04:00
|
|
|
/*
|
|
|
|
* Make sure we can access the executable's header
|
|
|
|
* directly, and make sure the recognize the executable
|
|
|
|
* as an ECOFF binary.
|
|
|
|
*/
|
1996-09-28 02:23:03 +04:00
|
|
|
if (check(0, sizeof *exechdrp))
|
|
|
|
BADUNMAP;
|
|
|
|
exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
|
|
|
|
|
|
|
|
if (ECOFF_BADMAG(exechdrp))
|
|
|
|
BADUNMAP;
|
|
|
|
|
1996-10-01 17:37:05 +04:00
|
|
|
/*
|
|
|
|
* Find the symbol list.
|
|
|
|
*/
|
1996-09-28 02:23:03 +04:00
|
|
|
symhdroff = exechdrp->f.f_symptr;
|
|
|
|
symhdrsize = exechdrp->f.f_nsyms;
|
|
|
|
|
|
|
|
if (check(symhdroff, sizeof *symhdrp) ||
|
|
|
|
sizeof *symhdrp != symhdrsize)
|
|
|
|
BADUNMAP;
|
|
|
|
symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
|
|
|
|
|
|
|
|
nesyms = symhdrp->esymMax;
|
|
|
|
if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
|
|
|
|
BADUNMAP;
|
|
|
|
esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
|
|
|
|
extstroff = symhdrp->cbSsExtOffset;
|
|
|
|
|
|
|
|
/*
|
1996-10-01 17:37:05 +04:00
|
|
|
* Clean out any left-over information for all valid entries.
|
|
|
|
* Type and value are defined to be 0 if not found; historical
|
1996-09-28 02:23:03 +04:00
|
|
|
* versions cleared other and desc as well.
|
|
|
|
*
|
1996-10-01 17:37:05 +04:00
|
|
|
* XXX Clearing anything other than n_type and n_value violates
|
1996-09-28 02:23:03 +04:00
|
|
|
* the semantics given in the man page.
|
|
|
|
*/
|
|
|
|
nent = 0;
|
|
|
|
for (p = list; !ISLAST(p); ++p) {
|
|
|
|
p->n_type = 0;
|
|
|
|
p->n_other = 0;
|
|
|
|
p->n_desc = 0;
|
|
|
|
p->n_value = 0;
|
|
|
|
++nent;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nesyms; i++) {
|
|
|
|
for (p = list; !ISLAST(p); p++) {
|
|
|
|
char *nlistname;
|
|
|
|
char *symtabname;
|
|
|
|
|
|
|
|
/* This may be incorrect */
|
|
|
|
nlistname = p->n_un.n_name;
|
|
|
|
if (*nlistname == '_')
|
|
|
|
nlistname++;
|
|
|
|
|
|
|
|
symtabname =
|
|
|
|
&mappedfile[extstroff + esyms[i].es_strindex];
|
|
|
|
|
|
|
|
if (!strcmp(symtabname, nlistname)) {
|
1996-10-01 17:37:05 +04:00
|
|
|
/*
|
|
|
|
* Translate (roughly) from ECOFF to nlist
|
|
|
|
*/
|
1996-09-28 02:23:03 +04:00
|
|
|
p->n_value = esyms[i].es_value;
|
|
|
|
p->n_type = N_EXT; /* XXX */
|
|
|
|
p->n_desc = 0; /* XXX */
|
|
|
|
p->n_other = 0; /* XXX */
|
1996-10-01 17:37:05 +04:00
|
|
|
|
1996-09-28 02:23:03 +04:00
|
|
|
if (--nent <= 0)
|
1996-10-01 17:37:05 +04:00
|
|
|
goto done;
|
|
|
|
break; /* into next run of outer loop */
|
1996-09-28 02:23:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-10-01 17:37:05 +04:00
|
|
|
done:
|
|
|
|
rv = nent;
|
1996-09-28 02:23:03 +04:00
|
|
|
unmap:
|
|
|
|
munmap(mappedfile, mappedsize);
|
|
|
|
out:
|
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* NLIST_ECOFF */
|