Home | History | Annotate | Line # | Download | only in gen
nlist_ecoff.c revision 1.8
      1  1.8   thorpej /*	$NetBSD: nlist_ecoff.c,v 1.8 1999/06/17 21:15:52 thorpej Exp $	*/
      2  1.1       cgd 
      3  1.1       cgd /*
      4  1.1       cgd  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5  1.1       cgd  *
      6  1.1       cgd  * Redistribution and use in source and binary forms, with or without
      7  1.1       cgd  * modification, are permitted provided that the following conditions
      8  1.1       cgd  * are met:
      9  1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     10  1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     11  1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     13  1.1       cgd  *    documentation and/or other materials provided with the distribution.
     14  1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     15  1.1       cgd  *    must display the following acknowledgement:
     16  1.3       cgd  *      This product includes software developed by Christopher G. Demetriou
     17  1.3       cgd  *	for the NetBSD Project.
     18  1.3       cgd  * 4. The name of the author may not be used to endorse or promote products
     19  1.3       cgd  *    derived from this software without specific prior written permission
     20  1.1       cgd  *
     21  1.3       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  1.3       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  1.3       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  1.3       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  1.3       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  1.3       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  1.3       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  1.3       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  1.3       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  1.3       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  1.1       cgd  */
     32  1.1       cgd 
     33  1.4  christos #include <sys/cdefs.h>
     34  1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     35  1.8   thorpej __RCSID("$NetBSD: nlist_ecoff.c,v 1.8 1999/06/17 21:15:52 thorpej Exp $");
     36  1.1       cgd #endif /* LIBC_SCCS and not lint */
     37  1.1       cgd 
     38  1.7    kleink #include "namespace.h"
     39  1.1       cgd #include <sys/param.h>
     40  1.1       cgd #include <sys/mman.h>
     41  1.1       cgd #include <sys/stat.h>
     42  1.1       cgd #include <sys/file.h>
     43  1.1       cgd 
     44  1.1       cgd #include <errno.h>
     45  1.1       cgd #include <stdio.h>
     46  1.1       cgd #include <string.h>
     47  1.1       cgd #include <unistd.h>
     48  1.1       cgd #include <a.out.h>			/* for 'struct nlist' declaration */
     49  1.1       cgd 
     50  1.1       cgd #include "nlist_private.h"
     51  1.1       cgd #ifdef NLIST_ECOFF
     52  1.1       cgd #include <sys/exec_ecoff.h>
     53  1.1       cgd #endif
     54  1.1       cgd 
     55  1.1       cgd #ifdef NLIST_ECOFF
     56  1.1       cgd #define	check(off, size)	((off < 0) || (off + size > mappedsize))
     57  1.1       cgd #define	BAD			do { rv = -1; goto out; } while (0)
     58  1.1       cgd #define	BADUNMAP		do { rv = -1; goto unmap; } while (0)
     59  1.1       cgd 
     60  1.1       cgd int
     61  1.1       cgd __fdnlist_ecoff(fd, list)
     62  1.6     perry 	int fd;
     63  1.6     perry 	struct nlist *list;
     64  1.1       cgd {
     65  1.1       cgd 	struct nlist *p;
     66  1.1       cgd 	struct ecoff_exechdr *exechdrp;
     67  1.1       cgd 	struct ecoff_symhdr *symhdrp;
     68  1.1       cgd 	struct ecoff_extsym *esyms;
     69  1.1       cgd 	struct stat st;
     70  1.1       cgd 	char *mappedfile;
     71  1.1       cgd 	size_t mappedsize;
     72  1.1       cgd 	u_long symhdroff, extstroff;
     73  1.1       cgd 	u_int symhdrsize;
     74  1.1       cgd 	int rv, nent;
     75  1.1       cgd 	long i, nesyms;
     76  1.1       cgd 
     77  1.3       cgd 	rv = -1;
     78  1.1       cgd 
     79  1.3       cgd 	/*
     80  1.3       cgd 	 * If we can't fstat() the file, something bad is going on.
     81  1.3       cgd 	 */
     82  1.1       cgd 	if (fstat(fd, &st) < 0)
     83  1.1       cgd 		BAD;
     84  1.3       cgd 
     85  1.3       cgd 	/*
     86  1.3       cgd 	 * Map the file in its entirety.
     87  1.3       cgd 	 */
     88  1.1       cgd 	if (st.st_size > SIZE_T_MAX) {
     89  1.1       cgd 		errno = EFBIG;
     90  1.1       cgd 		BAD;
     91  1.1       cgd 	}
     92  1.1       cgd 	mappedsize = st.st_size;
     93  1.8   thorpej 	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
     94  1.5       mrg 	    fd, 0);
     95  1.1       cgd 	if (mappedfile == (char *)-1)
     96  1.1       cgd 		BAD;
     97  1.1       cgd 
     98  1.3       cgd 	/*
     99  1.3       cgd 	 * Make sure we can access the executable's header
    100  1.3       cgd 	 * directly, and make sure the recognize the executable
    101  1.3       cgd 	 * as an ECOFF binary.
    102  1.3       cgd 	 */
    103  1.1       cgd 	if (check(0, sizeof *exechdrp))
    104  1.1       cgd 		BADUNMAP;
    105  1.1       cgd 	exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
    106  1.1       cgd 
    107  1.1       cgd 	if (ECOFF_BADMAG(exechdrp))
    108  1.1       cgd 		BADUNMAP;
    109  1.1       cgd 
    110  1.3       cgd 	/*
    111  1.3       cgd 	 * Find the symbol list.
    112  1.3       cgd 	 */
    113  1.1       cgd 	symhdroff = exechdrp->f.f_symptr;
    114  1.1       cgd 	symhdrsize = exechdrp->f.f_nsyms;
    115  1.1       cgd 
    116  1.1       cgd 	if (check(symhdroff, sizeof *symhdrp) ||
    117  1.1       cgd 	    sizeof *symhdrp != symhdrsize)
    118  1.1       cgd 		BADUNMAP;
    119  1.1       cgd 	symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
    120  1.1       cgd 
    121  1.1       cgd 	nesyms = symhdrp->esymMax;
    122  1.1       cgd 	if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
    123  1.1       cgd 		BADUNMAP;
    124  1.1       cgd 	esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
    125  1.1       cgd 	extstroff = symhdrp->cbSsExtOffset;
    126  1.1       cgd 
    127  1.1       cgd 	/*
    128  1.3       cgd 	 * Clean out any left-over information for all valid entries.
    129  1.3       cgd 	 * Type and value are defined to be 0 if not found; historical
    130  1.1       cgd 	 * versions cleared other and desc as well.
    131  1.1       cgd 	 *
    132  1.3       cgd 	 * XXX Clearing anything other than n_type and n_value violates
    133  1.1       cgd 	 * the semantics given in the man page.
    134  1.1       cgd 	 */
    135  1.1       cgd 	nent = 0;
    136  1.1       cgd 	for (p = list; !ISLAST(p); ++p) {
    137  1.1       cgd 		p->n_type = 0;
    138  1.1       cgd 		p->n_other = 0;
    139  1.1       cgd 		p->n_desc = 0;
    140  1.1       cgd 		p->n_value = 0;
    141  1.1       cgd 		++nent;
    142  1.1       cgd 	}
    143  1.1       cgd 
    144  1.1       cgd 	for (i = 0; i < nesyms; i++) {
    145  1.1       cgd 		for (p = list; !ISLAST(p); p++) {
    146  1.1       cgd 			char *nlistname;
    147  1.1       cgd 			char *symtabname;
    148  1.1       cgd 
    149  1.1       cgd 			/* This may be incorrect */
    150  1.1       cgd 			nlistname = p->n_un.n_name;
    151  1.1       cgd 			if (*nlistname == '_')
    152  1.1       cgd 				nlistname++;
    153  1.1       cgd 
    154  1.1       cgd 			symtabname =
    155  1.1       cgd 			    &mappedfile[extstroff + esyms[i].es_strindex];
    156  1.1       cgd 
    157  1.1       cgd 			if (!strcmp(symtabname, nlistname)) {
    158  1.3       cgd 				/*
    159  1.3       cgd 				 * Translate (roughly) from ECOFF to nlist
    160  1.3       cgd 				 */
    161  1.1       cgd 				p->n_value = esyms[i].es_value;
    162  1.1       cgd 				p->n_type = N_EXT;		/* XXX */
    163  1.1       cgd 				p->n_desc = 0;			/* XXX */
    164  1.1       cgd 				p->n_other = 0;			/* XXX */
    165  1.3       cgd 
    166  1.1       cgd 				if (--nent <= 0)
    167  1.3       cgd 					goto done;
    168  1.3       cgd 				break;	/* into next run of outer loop */
    169  1.1       cgd 			}
    170  1.1       cgd 		}
    171  1.1       cgd 	}
    172  1.3       cgd 
    173  1.3       cgd done:
    174  1.1       cgd 	rv = nent;
    175  1.1       cgd unmap:
    176  1.1       cgd 	munmap(mappedfile, mappedsize);
    177  1.1       cgd out:
    178  1.1       cgd 	return (rv);
    179  1.1       cgd }
    180  1.1       cgd 
    181  1.1       cgd #endif /* NLIST_ECOFF */
    182