Home | History | Annotate | Line # | Download | only in gen
nlist_coff.c revision 1.9
      1  1.9  christos /* $NetBSD: nlist_coff.c,v 1.9 2012/03/21 15:32:26 christos 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.9  christos __RCSID("$NetBSD: nlist_coff.c,v 1.9 2012/03/21 15:32:26 christos 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.8        he #include <nlist.h>
     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.9  christos __fdnlist_coff(int fd, struct nlist *list)
     85  1.1   msaitoh {
     86  1.1   msaitoh 	struct nlist *p;
     87  1.1   msaitoh 	struct coff_filehdr *filehdrp;
     88  1.1   msaitoh 	struct stat st;
     89  1.1   msaitoh 	char *mappedfile;
     90  1.1   msaitoh 	size_t mappedsize;
     91  1.1   msaitoh 	u_long symoff, extstroff;
     92  1.1   msaitoh 	int rv, nent;
     93  1.1   msaitoh 	long i, nesyms;
     94  1.1   msaitoh 
     95  1.1   msaitoh 	_DIAGASSERT(fd != -1);
     96  1.1   msaitoh 	_DIAGASSERT(list != NULL);
     97  1.1   msaitoh 
     98  1.1   msaitoh 	rv = -1;
     99  1.1   msaitoh 
    100  1.1   msaitoh 	/*
    101  1.1   msaitoh 	 * If we can't fstat() the file, something bad is going on.
    102  1.1   msaitoh 	 */
    103  1.1   msaitoh 	if (fstat(fd, &st) < 0)
    104  1.1   msaitoh 		BAD;
    105  1.1   msaitoh 
    106  1.1   msaitoh 	/*
    107  1.1   msaitoh 	 * Map the file in its entirety.
    108  1.1   msaitoh 	 */
    109  1.7     lukem 	if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) {
    110  1.1   msaitoh 		errno = EFBIG;
    111  1.1   msaitoh 		BAD;
    112  1.1   msaitoh 	}
    113  1.1   msaitoh 	mappedsize = st.st_size;
    114  1.1   msaitoh 	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
    115  1.1   msaitoh 	    fd, 0);
    116  1.1   msaitoh 	if (mappedfile == (char *)-1)
    117  1.1   msaitoh 		BAD;
    118  1.1   msaitoh 
    119  1.1   msaitoh 	/*
    120  1.1   msaitoh 	 * Make sure we can access the executable's header
    121  1.6       uwe 	 * directly, and make sure we recognize the executable
    122  1.1   msaitoh 	 * as an COFF binary.
    123  1.1   msaitoh 	 */
    124  1.6       uwe 	if (mappedsize < sizeof (struct coff_filehdr))
    125  1.1   msaitoh 		BADUNMAP;
    126  1.1   msaitoh 	filehdrp = (struct coff_filehdr *)&mappedfile[0];
    127  1.1   msaitoh 
    128  1.1   msaitoh 	if (COFF_BADMAG(filehdrp))
    129  1.1   msaitoh 		BADUNMAP;
    130  1.1   msaitoh 
    131  1.1   msaitoh 	/*
    132  1.1   msaitoh 	 * Find the symbol list.
    133  1.1   msaitoh 	 */
    134  1.1   msaitoh 	symoff = filehdrp->f_symptr;
    135  1.1   msaitoh 	nesyms = filehdrp->f_nsyms;
    136  1.1   msaitoh 
    137  1.6       uwe 	if (symoff + ES_LEN * nesyms > mappedsize)
    138  1.1   msaitoh 		BADUNMAP;
    139  1.1   msaitoh 	extstroff = symoff + ES_LEN * nesyms;
    140  1.1   msaitoh 
    141  1.1   msaitoh 	nent = 0;
    142  1.1   msaitoh 	for (p = list; !ISLAST(p); ++p) {
    143  1.1   msaitoh 		p->n_type = 0;
    144  1.1   msaitoh 		p->n_other = 0;
    145  1.1   msaitoh 		p->n_desc = 0;
    146  1.1   msaitoh 		p->n_value = 0;
    147  1.1   msaitoh 		++nent;
    148  1.1   msaitoh 	}
    149  1.1   msaitoh 
    150  1.1   msaitoh 	for (i = 0; i < nesyms; i++) {
    151  1.1   msaitoh 		char *symtabname;
    152  1.8        he 		const char *nlistname;
    153  1.1   msaitoh 		struct coff_extsym esym;
    154  1.1   msaitoh 		char name[10];
    155  1.1   msaitoh 
    156  1.1   msaitoh 		memcpy(&esym, &mappedfile[symoff + ES_LEN * i], ES_LEN);
    157  1.1   msaitoh 		if (esym.es_numaux != 0) {
    158  1.1   msaitoh 			i += esym.es_numaux;	/* XXX Skip aux entry */
    159  1.1   msaitoh 			continue;
    160  1.1   msaitoh 		}
    161  1.1   msaitoh 
    162  1.1   msaitoh 		if (esym.es_zero != 0) {
    163  1.1   msaitoh 			memcpy(name, esym.es_name, 8);
    164  1.1   msaitoh 			name[8] = 0;
    165  1.1   msaitoh 			symtabname = name;
    166  1.1   msaitoh 		} else if (esym.es_offset != 0)
    167  1.1   msaitoh 			symtabname = &mappedfile[extstroff + esym.es_offset];
    168  1.1   msaitoh 		else
    169  1.1   msaitoh 			continue;
    170  1.1   msaitoh 
    171  1.1   msaitoh 		for (p = list; !ISLAST(p); p++) {
    172  1.8        he 			nlistname = N_NAME(p);
    173  1.1   msaitoh 			if (!strcmp(symtabname, nlistname)) {
    174  1.1   msaitoh 				/*
    175  1.1   msaitoh 				 * Translate (roughly) from COFF to nlist
    176  1.1   msaitoh 				 */
    177  1.1   msaitoh 				p->n_value = esym.es_value;
    178  1.1   msaitoh 				p->n_type = N_EXT;		/* XXX */
    179  1.1   msaitoh 				p->n_desc = 0;			/* XXX */
    180  1.1   msaitoh 				p->n_other = 0;			/* XXX */
    181  1.1   msaitoh 
    182  1.1   msaitoh 				if (--nent <= 0)
    183  1.1   msaitoh 					goto done;
    184  1.1   msaitoh 				break;	/* into next run of outer loop */
    185  1.1   msaitoh 			}
    186  1.1   msaitoh 		}
    187  1.1   msaitoh 	}
    188  1.1   msaitoh 
    189  1.1   msaitoh done:
    190  1.1   msaitoh 	rv = nent;
    191  1.1   msaitoh unmap:
    192  1.1   msaitoh 	munmap(mappedfile, mappedsize);
    193  1.1   msaitoh out:
    194  1.1   msaitoh 	return (rv);
    195  1.1   msaitoh }
    196  1.1   msaitoh 
    197  1.1   msaitoh #endif /* NLIST_COFF */
    198