Home | History | Annotate | Line # | Download | only in gen
nlist_ecoff.c revision 1.9
      1 /*	$NetBSD: nlist_ecoff.c,v 1.9 1999/09/16 11:45:01 lukem 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.9 1999/09/16 11:45:01 lukem 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 <assert.h>
     45 #include <errno.h>
     46 #include <stdio.h>
     47 #include <string.h>
     48 #include <unistd.h>
     49 #include <a.out.h>			/* for 'struct nlist' declaration */
     50 
     51 #include "nlist_private.h"
     52 #ifdef NLIST_ECOFF
     53 #include <sys/exec_ecoff.h>
     54 #endif
     55 
     56 #ifdef NLIST_ECOFF
     57 #define	check(off, size)	((off < 0) || (off + size > mappedsize))
     58 #define	BAD			do { rv = -1; goto out; } while (0)
     59 #define	BADUNMAP		do { rv = -1; goto unmap; } while (0)
     60 
     61 int
     62 __fdnlist_ecoff(fd, list)
     63 	int fd;
     64 	struct nlist *list;
     65 {
     66 	struct nlist *p;
     67 	struct ecoff_exechdr *exechdrp;
     68 	struct ecoff_symhdr *symhdrp;
     69 	struct ecoff_extsym *esyms;
     70 	struct stat st;
     71 	char *mappedfile;
     72 	size_t mappedsize;
     73 	u_long symhdroff, extstroff;
     74 	u_int symhdrsize;
     75 	int rv, nent;
     76 	long i, nesyms;
     77 
     78 	_DIAGASSERT(fd != -1);
     79 	_DIAGASSERT(list != NULL);
     80 #ifdef _DIAGNOSTIC
     81 	if (fd == -1) {
     82 		errno = EBADF;
     83 		return (-1);
     84 	}
     85 	if (list == NULL) {
     86 		errno = EFAULT;
     87 		return (-1);
     88 	}
     89 #endif
     90 
     91 	rv = -1;
     92 
     93 	/*
     94 	 * If we can't fstat() the file, something bad is going on.
     95 	 */
     96 	if (fstat(fd, &st) < 0)
     97 		BAD;
     98 
     99 	/*
    100 	 * Map the file in its entirety.
    101 	 */
    102 	if (st.st_size > SIZE_T_MAX) {
    103 		errno = EFBIG;
    104 		BAD;
    105 	}
    106 	mappedsize = st.st_size;
    107 	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
    108 	    fd, 0);
    109 	if (mappedfile == (char *)-1)
    110 		BAD;
    111 
    112 	/*
    113 	 * Make sure we can access the executable's header
    114 	 * directly, and make sure the recognize the executable
    115 	 * as an ECOFF binary.
    116 	 */
    117 	if (check(0, sizeof *exechdrp))
    118 		BADUNMAP;
    119 	exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
    120 
    121 	if (ECOFF_BADMAG(exechdrp))
    122 		BADUNMAP;
    123 
    124 	/*
    125 	 * Find the symbol list.
    126 	 */
    127 	symhdroff = exechdrp->f.f_symptr;
    128 	symhdrsize = exechdrp->f.f_nsyms;
    129 
    130 	if (check(symhdroff, sizeof *symhdrp) ||
    131 	    sizeof *symhdrp != symhdrsize)
    132 		BADUNMAP;
    133 	symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
    134 
    135 	nesyms = symhdrp->esymMax;
    136 	if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
    137 		BADUNMAP;
    138 	esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
    139 	extstroff = symhdrp->cbSsExtOffset;
    140 
    141 	/*
    142 	 * Clean out any left-over information for all valid entries.
    143 	 * Type and value are defined to be 0 if not found; historical
    144 	 * versions cleared other and desc as well.
    145 	 *
    146 	 * XXX Clearing anything other than n_type and n_value violates
    147 	 * the semantics given in the man page.
    148 	 */
    149 	nent = 0;
    150 	for (p = list; !ISLAST(p); ++p) {
    151 		p->n_type = 0;
    152 		p->n_other = 0;
    153 		p->n_desc = 0;
    154 		p->n_value = 0;
    155 		++nent;
    156 	}
    157 
    158 	for (i = 0; i < nesyms; i++) {
    159 		for (p = list; !ISLAST(p); p++) {
    160 			char *nlistname;
    161 			char *symtabname;
    162 
    163 			/* This may be incorrect */
    164 			nlistname = p->n_un.n_name;
    165 			if (*nlistname == '_')
    166 				nlistname++;
    167 
    168 			symtabname =
    169 			    &mappedfile[extstroff + esyms[i].es_strindex];
    170 
    171 			if (!strcmp(symtabname, nlistname)) {
    172 				/*
    173 				 * Translate (roughly) from ECOFF to nlist
    174 				 */
    175 				p->n_value = esyms[i].es_value;
    176 				p->n_type = N_EXT;		/* XXX */
    177 				p->n_desc = 0;			/* XXX */
    178 				p->n_other = 0;			/* XXX */
    179 
    180 				if (--nent <= 0)
    181 					goto done;
    182 				break;	/* into next run of outer loop */
    183 			}
    184 		}
    185 	}
    186 
    187 done:
    188 	rv = nent;
    189 unmap:
    190 	munmap(mappedfile, mappedsize);
    191 out:
    192 	return (rv);
    193 }
    194 
    195 #endif /* NLIST_ECOFF */
    196