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