Home | History | Annotate | Line # | Download | only in gen
nlist_coff.c revision 1.7
      1  1.7    lukem /* $NetBSD: nlist_coff.c,v 1.7 2009/02/16 10:40:45 lukem Exp $ */
      2  1.1  msaitoh 
      3  1.1  msaitoh /*
      4  1.4      cgd  * Copyright (c) 1996 Christopher G. Demetriou
      5  1.4      cgd  * All rights reserved.
      6  1.4      cgd  *
      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.4      cgd  *          This product includes software developed for the
     18  1.5     salo  *          NetBSD Project.  See http://www.NetBSD.org/ for
     19  1.4      cgd  *          information about NetBSD.
     20  1.1  msaitoh  * 4. The name of the author may not be used to endorse or promote products
     21  1.4      cgd  *    derived from this software without specific prior written permission.
     22  1.4      cgd  *
     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.4      cgd  *
     34  1.4      cgd  * <<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.7    lukem __RCSID("$NetBSD: nlist_coff.c,v 1.7 2009/02/16 10:40:45 lukem 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.6      uwe #define	BAD		do { rv = -1; goto out; } while (/*CONSTCOND*/0)
     62  1.6      uwe #define	BADUNMAP	do { rv = -1; goto unmap; } while (/*CONSTCOND*/0)
     63  1.1  msaitoh 
     64  1.1  msaitoh #define ES_LEN 18
     65  1.1  msaitoh struct coff_extsym {
     66  1.1  msaitoh 	union {
     67  1.1  msaitoh 		char u_name[8];
     68  1.1  msaitoh 		struct {
     69  1.1  msaitoh 			int u_zero;
     70  1.1  msaitoh 			int u_offset;
     71  1.1  msaitoh 		} s;
     72  1.1  msaitoh 	} u;
     73  1.1  msaitoh 	int32_t es_value;
     74  1.1  msaitoh 	int16_t es_scnum;
     75  1.1  msaitoh 	int16_t es_type;
     76  1.1  msaitoh 	int8_t es_class;
     77  1.1  msaitoh 	int8_t es_numaux;
     78  1.1  msaitoh };
     79  1.1  msaitoh #define es_name u.u_name
     80  1.1  msaitoh #define es_zero u.s.u_zero
     81  1.1  msaitoh #define es_offset u.s.u_offset
     82  1.1  msaitoh 
     83  1.1  msaitoh int
     84  1.1  msaitoh __fdnlist_coff(fd, list)
     85  1.1  msaitoh 	int fd;
     86  1.1  msaitoh 	struct nlist *list;
     87  1.1  msaitoh {
     88  1.1  msaitoh 	struct nlist *p;
     89  1.1  msaitoh 	struct coff_filehdr *filehdrp;
     90  1.1  msaitoh 	struct stat st;
     91  1.1  msaitoh 	char *mappedfile;
     92  1.1  msaitoh 	size_t mappedsize;
     93  1.1  msaitoh 	u_long symoff, extstroff;
     94  1.1  msaitoh 	int rv, nent;
     95  1.1  msaitoh 	long i, nesyms;
     96  1.1  msaitoh 
     97  1.1  msaitoh 	_DIAGASSERT(fd != -1);
     98  1.1  msaitoh 	_DIAGASSERT(list != NULL);
     99  1.1  msaitoh 
    100  1.1  msaitoh 	rv = -1;
    101  1.1  msaitoh 
    102  1.1  msaitoh 	/*
    103  1.1  msaitoh 	 * If we can't fstat() the file, something bad is going on.
    104  1.1  msaitoh 	 */
    105  1.1  msaitoh 	if (fstat(fd, &st) < 0)
    106  1.1  msaitoh 		BAD;
    107  1.1  msaitoh 
    108  1.1  msaitoh 	/*
    109  1.1  msaitoh 	 * Map the file in its entirety.
    110  1.1  msaitoh 	 */
    111  1.7    lukem 	if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) {
    112  1.1  msaitoh 		errno = EFBIG;
    113  1.1  msaitoh 		BAD;
    114  1.1  msaitoh 	}
    115  1.1  msaitoh 	mappedsize = st.st_size;
    116  1.1  msaitoh 	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
    117  1.1  msaitoh 	    fd, 0);
    118  1.1  msaitoh 	if (mappedfile == (char *)-1)
    119  1.1  msaitoh 		BAD;
    120  1.1  msaitoh 
    121  1.1  msaitoh 	/*
    122  1.1  msaitoh 	 * Make sure we can access the executable's header
    123  1.6      uwe 	 * directly, and make sure we recognize the executable
    124  1.1  msaitoh 	 * as an COFF binary.
    125  1.1  msaitoh 	 */
    126  1.6      uwe 	if (mappedsize < sizeof (struct coff_filehdr))
    127  1.1  msaitoh 		BADUNMAP;
    128  1.1  msaitoh 	filehdrp = (struct coff_filehdr *)&mappedfile[0];
    129  1.1  msaitoh 
    130  1.1  msaitoh 	if (COFF_BADMAG(filehdrp))
    131  1.1  msaitoh 		BADUNMAP;
    132  1.1  msaitoh 
    133  1.1  msaitoh 	/*
    134  1.1  msaitoh 	 * Find the symbol list.
    135  1.1  msaitoh 	 */
    136  1.1  msaitoh 	symoff = filehdrp->f_symptr;
    137  1.1  msaitoh 	nesyms = filehdrp->f_nsyms;
    138  1.1  msaitoh 
    139  1.6      uwe 	if (symoff + ES_LEN * nesyms > mappedsize)
    140  1.1  msaitoh 		BADUNMAP;
    141  1.1  msaitoh 	extstroff = symoff + ES_LEN * nesyms;
    142  1.1  msaitoh 
    143  1.1  msaitoh 	nent = 0;
    144  1.1  msaitoh 	for (p = list; !ISLAST(p); ++p) {
    145  1.1  msaitoh 		p->n_type = 0;
    146  1.1  msaitoh 		p->n_other = 0;
    147  1.1  msaitoh 		p->n_desc = 0;
    148  1.1  msaitoh 		p->n_value = 0;
    149  1.1  msaitoh 		++nent;
    150  1.1  msaitoh 	}
    151  1.1  msaitoh 
    152  1.1  msaitoh 	for (i = 0; i < nesyms; i++) {
    153  1.1  msaitoh 		char *symtabname;
    154  1.1  msaitoh 		char *nlistname;
    155  1.1  msaitoh 		struct coff_extsym esym;
    156  1.1  msaitoh 		char name[10];
    157  1.1  msaitoh 
    158  1.1  msaitoh 		memcpy(&esym, &mappedfile[symoff + ES_LEN * i], ES_LEN);
    159  1.1  msaitoh 		if (esym.es_numaux != 0) {
    160  1.1  msaitoh 			i += esym.es_numaux;	/* XXX Skip aux entry */
    161  1.1  msaitoh 			continue;
    162  1.1  msaitoh 		}
    163  1.1  msaitoh 
    164  1.1  msaitoh 		if (esym.es_zero != 0) {
    165  1.1  msaitoh 			memcpy(name, esym.es_name, 8);
    166  1.1  msaitoh 			name[8] = 0;
    167  1.1  msaitoh 			symtabname = name;
    168  1.1  msaitoh 		} else if (esym.es_offset != 0)
    169  1.1  msaitoh 			symtabname = &mappedfile[extstroff + esym.es_offset];
    170  1.1  msaitoh 		else
    171  1.1  msaitoh 			continue;
    172  1.1  msaitoh 
    173  1.1  msaitoh 		for (p = list; !ISLAST(p); p++) {
    174  1.1  msaitoh 			nlistname = p->n_un.n_name;
    175  1.1  msaitoh 			if (!strcmp(symtabname, nlistname)) {
    176  1.1  msaitoh 				/*
    177  1.1  msaitoh 				 * Translate (roughly) from COFF to nlist
    178  1.1  msaitoh 				 */
    179  1.1  msaitoh 				p->n_value = esym.es_value;
    180  1.1  msaitoh 				p->n_type = N_EXT;		/* XXX */
    181  1.1  msaitoh 				p->n_desc = 0;			/* XXX */
    182  1.1  msaitoh 				p->n_other = 0;			/* XXX */
    183  1.1  msaitoh 
    184  1.1  msaitoh 				if (--nent <= 0)
    185  1.1  msaitoh 					goto done;
    186  1.1  msaitoh 				break;	/* into next run of outer loop */
    187  1.1  msaitoh 			}
    188  1.1  msaitoh 		}
    189  1.1  msaitoh 	}
    190  1.1  msaitoh 
    191  1.1  msaitoh done:
    192  1.1  msaitoh 	rv = nent;
    193  1.1  msaitoh unmap:
    194  1.1  msaitoh 	munmap(mappedfile, mappedsize);
    195  1.1  msaitoh out:
    196  1.1  msaitoh 	return (rv);
    197  1.1  msaitoh }
    198  1.1  msaitoh 
    199  1.1  msaitoh #endif /* NLIST_COFF */
    200