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