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