Home | History | Annotate | Line # | Download | only in gen
nlist_ecoff.c revision 1.1
      1 /*	$NetBSD: nlist_ecoff.c,v 1.1 1996/09/27 22:23:05 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.1 1996/09/27 22:23:05 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_is_ecoff(fd)
     68 	int fd;
     69 {
     70 	struct stat st;
     71 	struct ecoff_exechdr *exechdrp;
     72 	char *mappedfile;
     73 	size_t mappedsize;
     74 	int rv = -1;
     75 
     76 	if (fstat(fd, &st) < 0)
     77 		BAD;
     78 	if (st.st_size > SIZE_T_MAX) {
     79 		errno = EFBIG;
     80 		BAD;
     81 	}
     82 	mappedsize = st.st_size;
     83 	mappedfile = mmap(NULL, mappedsize, PROT_READ, 0, fd, 0);
     84 	if (mappedfile == (char *)-1)
     85 		BAD;
     86 
     87 	if (check(0, sizeof *exechdrp))
     88 		BADUNMAP;
     89 	exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
     90 
     91 	if (ECOFF_BADMAG(exechdrp))
     92 		BADUNMAP;
     93 
     94 	rv = 0;
     95 
     96 unmap:
     97 	munmap(mappedfile, mappedsize);
     98 out:
     99 	return (rv);
    100 }
    101 
    102 int
    103 __fdnlist_ecoff(fd, list)
    104 	register int fd;
    105 	register struct nlist *list;
    106 {
    107 	struct nlist *p;
    108 	struct ecoff_exechdr *exechdrp;
    109 	struct ecoff_symhdr *symhdrp;
    110 	struct ecoff_extsym *esyms;
    111 	struct stat st;
    112 	char *mappedfile;
    113 	size_t mappedsize;
    114 	u_long symhdroff, extstroff;
    115 	u_int symhdrsize;
    116 	int rv, nent;
    117 	long i, nesyms;
    118 
    119 	rv = -3;
    120 
    121 	if (fstat(fd, &st) < 0)
    122 		BAD;
    123 	if (st.st_size > SIZE_T_MAX) {
    124 		errno = EFBIG;
    125 		BAD;
    126 	}
    127 	mappedsize = st.st_size;
    128 	mappedfile = mmap(NULL, mappedsize, PROT_READ, 0, fd, 0);
    129 	if (mappedfile == (char *)-1)
    130 		BAD;
    131 
    132 	if (check(0, sizeof *exechdrp))
    133 		BADUNMAP;
    134 	exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
    135 
    136 	if (ECOFF_BADMAG(exechdrp))
    137 		BADUNMAP;
    138 
    139 	symhdroff = exechdrp->f.f_symptr;
    140 	symhdrsize = exechdrp->f.f_nsyms;
    141 
    142 	if (check(symhdroff, sizeof *symhdrp) ||
    143 	    sizeof *symhdrp != symhdrsize)
    144 		BADUNMAP;
    145 	symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
    146 
    147 	nesyms = symhdrp->esymMax;
    148 	if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
    149 		BADUNMAP;
    150 	esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
    151 	extstroff = symhdrp->cbSsExtOffset;
    152 
    153 	/*
    154 	 * clean out any left-over information for all valid entries.
    155 	 * Type and value defined to be 0 if not found; historical
    156 	 * versions cleared other and desc as well.
    157 	 *
    158 	 * XXX clearing anything other than n_type and n_value violates
    159 	 * the semantics given in the man page.
    160 	 */
    161 	nent = 0;
    162 	for (p = list; !ISLAST(p); ++p) {
    163 		p->n_type = 0;
    164 		p->n_other = 0;
    165 		p->n_desc = 0;
    166 		p->n_value = 0;
    167 		++nent;
    168 	}
    169 
    170 	for (i = 0; i < nesyms; i++) {
    171 		for (p = list; !ISLAST(p); p++) {
    172 			char *nlistname;
    173 			char *symtabname;
    174 
    175 			/* This may be incorrect */
    176 			nlistname = p->n_un.n_name;
    177 			if (*nlistname == '_')
    178 				nlistname++;
    179 
    180 			symtabname =
    181 			    &mappedfile[extstroff + esyms[i].es_strindex];
    182 
    183 			if (!strcmp(symtabname, nlistname)) {
    184 				p->n_value = esyms[i].es_value;
    185 				p->n_type = N_EXT;		/* XXX */
    186 				p->n_desc = 0;			/* XXX */
    187 				p->n_other = 0;			/* XXX */
    188 				if (--nent <= 0)
    189 					break;
    190 			}
    191 		}
    192 	}
    193 	rv = nent;
    194 
    195 unmap:
    196 	munmap(mappedfile, mappedsize);
    197 out:
    198 	return (rv);
    199 }
    200 
    201 #endif /* NLIST_ECOFF */
    202