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