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