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