Home | History | Annotate | Line # | Download | only in gen
nlist_ecoff.c revision 1.2
      1 /*	$NetBSD: nlist_ecoff.c,v 1.2 1996/09/30 23:49:29 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1989, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 #if 0
     39 static char sccsid[] = "@(#)nlist.c	8.1 (Berkeley) 6/4/93";
     40 #else
     41 static char rcsid[] = "$NetBSD: nlist_ecoff.c,v 1.2 1996/09/30 23:49:29 cgd Exp $";
     42 #endif
     43 #endif /* LIBC_SCCS and not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/mman.h>
     47 #include <sys/stat.h>
     48 #include <sys/file.h>
     49 
     50 #include <errno.h>
     51 #include <stdio.h>
     52 #include <string.h>
     53 #include <unistd.h>
     54 #include <a.out.h>			/* for 'struct nlist' declaration */
     55 
     56 #include "nlist_private.h"
     57 #ifdef NLIST_ECOFF
     58 #include <sys/exec_ecoff.h>
     59 #endif
     60 
     61 #ifdef NLIST_ECOFF
     62 #define	check(off, size)	((off < 0) || (off + size > mappedsize))
     63 #define	BAD			do { rv = -1; goto out; } while (0)
     64 #define	BADUNMAP		do { rv = -1; goto unmap; } while (0)
     65 
     66 int
     67 __fdnlist_ecoff(fd, list)
     68 	register int fd;
     69 	register struct nlist *list;
     70 {
     71 	struct nlist *p;
     72 	struct ecoff_exechdr *exechdrp;
     73 	struct ecoff_symhdr *symhdrp;
     74 	struct ecoff_extsym *esyms;
     75 	struct stat st;
     76 	char *mappedfile;
     77 	size_t mappedsize;
     78 	u_long symhdroff, extstroff;
     79 	u_int symhdrsize;
     80 	int rv, nent;
     81 	long i, nesyms;
     82 
     83 	rv = -3;
     84 
     85 	if (fstat(fd, &st) < 0)
     86 		BAD;
     87 	if (st.st_size > SIZE_T_MAX) {
     88 		errno = EFBIG;
     89 		BAD;
     90 	}
     91 	mappedsize = st.st_size;
     92 	mappedfile = mmap(NULL, mappedsize, PROT_READ, 0, fd, 0);
     93 	if (mappedfile == (char *)-1)
     94 		BAD;
     95 
     96 	if (check(0, sizeof *exechdrp))
     97 		BADUNMAP;
     98 	exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
     99 
    100 	if (ECOFF_BADMAG(exechdrp))
    101 		BADUNMAP;
    102 
    103 	symhdroff = exechdrp->f.f_symptr;
    104 	symhdrsize = exechdrp->f.f_nsyms;
    105 
    106 	if (check(symhdroff, sizeof *symhdrp) ||
    107 	    sizeof *symhdrp != symhdrsize)
    108 		BADUNMAP;
    109 	symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
    110 
    111 	nesyms = symhdrp->esymMax;
    112 	if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
    113 		BADUNMAP;
    114 	esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
    115 	extstroff = symhdrp->cbSsExtOffset;
    116 
    117 	/*
    118 	 * clean out any left-over information for all valid entries.
    119 	 * Type and value defined to be 0 if not found; historical
    120 	 * versions cleared other and desc as well.
    121 	 *
    122 	 * XXX clearing anything other than n_type and n_value violates
    123 	 * the semantics given in the man page.
    124 	 */
    125 	nent = 0;
    126 	for (p = list; !ISLAST(p); ++p) {
    127 		p->n_type = 0;
    128 		p->n_other = 0;
    129 		p->n_desc = 0;
    130 		p->n_value = 0;
    131 		++nent;
    132 	}
    133 
    134 	for (i = 0; i < nesyms; i++) {
    135 		for (p = list; !ISLAST(p); p++) {
    136 			char *nlistname;
    137 			char *symtabname;
    138 
    139 			/* This may be incorrect */
    140 			nlistname = p->n_un.n_name;
    141 			if (*nlistname == '_')
    142 				nlistname++;
    143 
    144 			symtabname =
    145 			    &mappedfile[extstroff + esyms[i].es_strindex];
    146 
    147 			if (!strcmp(symtabname, nlistname)) {
    148 				p->n_value = esyms[i].es_value;
    149 				p->n_type = N_EXT;		/* XXX */
    150 				p->n_desc = 0;			/* XXX */
    151 				p->n_other = 0;			/* XXX */
    152 				if (--nent <= 0)
    153 					break;
    154 			}
    155 		}
    156 	}
    157 	rv = nent;
    158 
    159 unmap:
    160 	munmap(mappedfile, mappedsize);
    161 out:
    162 	return (rv);
    163 }
    164 
    165 #endif /* NLIST_ECOFF */
    166