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