Home | History | Annotate | Line # | Download | only in gen
nlist.c revision 1.7.2.1
      1 /*	$NetBSD: nlist.c,v 1.7.2.1 1996/09/19 20:03:23 jtc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  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 by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #if defined(LIBC_SCCS) && !defined(lint)
     37 #if 0
     38 static char sccsid[] = "@(#)nlist.c	8.1 (Berkeley) 6/4/93";
     39 #else
     40 static char rcsid[] = "$NetBSD: nlist.c,v 1.7.2.1 1996/09/19 20:03:23 jtc Exp $";
     41 #endif
     42 #endif /* LIBC_SCCS and not lint */
     43 
     44 #ifdef __alpha__
     45 #define		DO_ECOFF
     46 #else
     47 #define		DO_AOUT
     48 #endif
     49 
     50 #if defined(DO_AOUT) + defined(DO_ECOFF) != 1
     51 	ERROR: NOT PROPERLY CONFIGURED
     52 #endif
     53 
     54 #include "namespace.h"
     55 #include <sys/param.h>
     56 #include <sys/mman.h>
     57 #include <sys/stat.h>
     58 #include <sys/file.h>
     59 
     60 #include <errno.h>
     61 #include <a.out.h>
     62 #ifdef DO_ECOFF
     63 #include <sys/exec_ecoff.h>
     64 #endif
     65 #include <stdio.h>
     66 #include <string.h>
     67 #include <unistd.h>
     68 
     69 #ifdef __weak_alias
     70 __weak_alias(nlist,_nlist);
     71 #endif
     72 
     73 int
     74 nlist(name, list)
     75 	const char *name;
     76 	struct nlist *list;
     77 {
     78 	int fd, n;
     79 
     80 	fd = open(name, O_RDONLY, 0);
     81 	if (fd < 0)
     82 		return (-1);
     83 	n = __fdnlist(fd, list);
     84 	(void)close(fd);
     85 	return (n);
     86 }
     87 
     88 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
     89 
     90 #ifdef DO_AOUT
     91 int
     92 __fdnlist(fd, list)
     93 	register int fd;
     94 	register struct nlist *list;
     95 {
     96 	register struct nlist *p, *s;
     97 	register caddr_t strtab;
     98 	register off_t stroff, symoff;
     99 	register u_long symsize;
    100 	register int nent, cc;
    101 	size_t strsize;
    102 	struct nlist nbuf[1024];
    103 	struct exec exec;
    104 	struct stat st;
    105 
    106 	if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
    107 	    read(fd, &exec, sizeof(exec)) != sizeof(exec) ||
    108 	    N_BADMAG(exec) || fstat(fd, &st) < 0)
    109 		return (-1);
    110 
    111 	symoff = N_SYMOFF(exec);
    112 	symsize = exec.a_syms;
    113 	stroff = symoff + symsize;
    114 
    115 	/* Check for files too large to mmap. */
    116 	if (st.st_size - stroff > SIZE_T_MAX) {
    117 		errno = EFBIG;
    118 		return (-1);
    119 	}
    120 	/*
    121 	 * Map string table into our address space.  This gives us
    122 	 * an easy way to randomly access all the strings, without
    123 	 * making the memory allocation permanent as with malloc/free
    124 	 * (i.e., munmap will return it to the system).
    125 	 */
    126 	strsize = st.st_size - stroff;
    127 	strtab = mmap(NULL, (size_t)strsize, PROT_READ, 0, fd, stroff);
    128 	if (strtab == (char *)-1)
    129 		return (-1);
    130 	/*
    131 	 * clean out any left-over information for all valid entries.
    132 	 * Type and value defined to be 0 if not found; historical
    133 	 * versions cleared other and desc as well.  Also figure out
    134 	 * the largest string length so don't read any more of the
    135 	 * string table than we have to.
    136 	 *
    137 	 * XXX clearing anything other than n_type and n_value violates
    138 	 * the semantics given in the man page.
    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 	if (lseek(fd, symoff, SEEK_SET) == -1)
    149 		return (-1);
    150 
    151 	while (symsize > 0) {
    152 		cc = MIN(symsize, sizeof(nbuf));
    153 		if (read(fd, nbuf, cc) != cc)
    154 			break;
    155 		symsize -= cc;
    156 		for (s = nbuf; cc > 0; ++s, cc -= sizeof(*s)) {
    157 			register int soff = s->n_un.n_strx;
    158 
    159 			if (soff == 0 || (s->n_type & N_STAB) != 0)
    160 				continue;
    161 			for (p = list; !ISLAST(p); p++)
    162 				if (!strcmp(&strtab[soff], p->n_un.n_name)) {
    163 					p->n_value = s->n_value;
    164 					p->n_type = s->n_type;
    165 					p->n_desc = s->n_desc;
    166 					p->n_other = s->n_other;
    167 					if (--nent <= 0)
    168 						break;
    169 				}
    170 		}
    171 	}
    172 	munmap(strtab, strsize);
    173 	return (nent);
    174 }
    175 #endif /* DO_AOUT */
    176 
    177 #ifdef DO_ECOFF
    178 #define check(off, size)	((off < 0) || (off + size > mappedsize))
    179 #define	BAD			do { rv = -1; goto out; } while (0)
    180 #define	BADUNMAP		do { rv = -1; goto unmap; } while (0)
    181 
    182 int
    183 __fdnlist(fd, list)
    184 	register int fd;
    185 	register struct nlist *list;
    186 {
    187 	struct nlist *p;
    188 	struct ecoff_exechdr *exechdrp;
    189 	struct ecoff_symhdr *symhdrp;
    190 	struct ecoff_extsym *esyms;
    191 	struct stat st;
    192 	char *mappedfile;
    193 	size_t mappedsize;
    194 	u_long symhdroff, extstroff;
    195 	u_int symhdrsize;
    196 	int rv, nent;
    197 	long i, nesyms;
    198 
    199 	rv = -3;
    200 
    201 	if (fstat(fd, &st) < 0)
    202 		BAD;
    203 	if (st.st_size > SIZE_T_MAX) {
    204 		errno = EFBIG;
    205 		BAD;
    206 	}
    207 	mappedsize = st.st_size;
    208 	mappedfile = mmap(NULL, mappedsize, PROT_READ, 0, fd, 0);
    209 	if (mappedfile == (char *)-1)
    210 		BAD;
    211 
    212 	if (check(0, sizeof *exechdrp))
    213 		BADUNMAP;
    214 	exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
    215 
    216 	if (ECOFF_BADMAG(exechdrp))
    217 		BADUNMAP;
    218 
    219 	symhdroff = exechdrp->f.f_symptr;
    220 	symhdrsize = exechdrp->f.f_nsyms;
    221 
    222 	if (check(symhdroff, sizeof *symhdrp) ||
    223 	    sizeof *symhdrp != symhdrsize)
    224 		BADUNMAP;
    225 	symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
    226 
    227 	nesyms = symhdrp->esymMax;
    228 	if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
    229 		BADUNMAP;
    230 	esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
    231 	extstroff = symhdrp->cbSsExtOffset;
    232 
    233 	/*
    234 	 * clean out any left-over information for all valid entries.
    235 	 * Type and value defined to be 0 if not found; historical
    236 	 * versions cleared other and desc as well.
    237 	 *
    238 	 * XXX clearing anything other than n_type and n_value violates
    239 	 * the semantics given in the man page.
    240 	 */
    241 	nent = 0;
    242 	for (p = list; !ISLAST(p); ++p) {
    243 		p->n_type = 0;
    244 		p->n_other = 0;
    245 		p->n_desc = 0;
    246 		p->n_value = 0;
    247 		++nent;
    248 	}
    249 
    250 	for (i = 0; i < nesyms; i++) {
    251 		for (p = list; !ISLAST(p); p++) {
    252 			char *nlistname;
    253 			char *symtabname;
    254 
    255 			nlistname = p->n_un.n_name;
    256 			if (*nlistname == '_')
    257 				nlistname++;
    258 			symtabname =
    259 			    &mappedfile[extstroff + esyms[i].es_strindex];
    260 
    261 			if (!strcmp(symtabname, nlistname)) {
    262 				p->n_value = esyms[i].es_value;
    263 				p->n_type = N_EXT;		/* XXX */
    264 				p->n_desc = 0;			/* XXX */
    265 				p->n_other = 0;			/* XXX */
    266 				if (--nent <= 0)
    267 					break;
    268 			}
    269 		}
    270 	}
    271 	rv = nent;
    272 
    273 unmap:
    274 	munmap(mappedfile, mappedsize);
    275 out:
    276 	return (rv);
    277 }
    278 #endif /* DO_ECOFF */
    279