Home | History | Annotate | Line # | Download | only in gen
nlist.c revision 1.6
      1 /*	$NetBSD: nlist.c,v 1.6 1995/09/29 04:19:59 cgd 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.6 1995/09/29 04:19:59 cgd 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 <sys/param.h>
     55 #include <sys/mman.h>
     56 #include <sys/stat.h>
     57 #include <sys/file.h>
     58 
     59 #include <errno.h>
     60 #include <a.out.h>
     61 #ifdef DO_ECOFF
     62 #include <sys/exec_ecoff.h>
     63 #endif
     64 #include <stdio.h>
     65 #include <string.h>
     66 #include <unistd.h>
     67 
     68 int
     69 nlist(name, list)
     70 	const char *name;
     71 	struct nlist *list;
     72 {
     73 	int fd, n;
     74 
     75 	fd = open(name, O_RDONLY, 0);
     76 	if (fd < 0)
     77 		return (-1);
     78 	n = __fdnlist(fd, list);
     79 	(void)close(fd);
     80 	return (n);
     81 }
     82 
     83 #define	ISLAST(p)	(p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
     84 
     85 #ifdef DO_AOUT
     86 int
     87 __fdnlist(fd, list)
     88 	register int fd;
     89 	register struct nlist *list;
     90 {
     91 	register struct nlist *p, *s;
     92 	register caddr_t strtab;
     93 	register off_t stroff, symoff;
     94 	register u_long symsize;
     95 	register int nent, cc;
     96 	size_t strsize;
     97 	struct nlist nbuf[1024];
     98 	struct exec exec;
     99 	struct stat st;
    100 
    101 	if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
    102 	    read(fd, &exec, sizeof(exec)) != sizeof(exec) ||
    103 	    N_BADMAG(exec) || fstat(fd, &st) < 0)
    104 		return (-1);
    105 
    106 	symoff = N_SYMOFF(exec);
    107 	symsize = exec.a_syms;
    108 	stroff = symoff + symsize;
    109 
    110 	/* Check for files too large to mmap. */
    111 	if (st.st_size - stroff > SIZE_T_MAX) {
    112 		errno = EFBIG;
    113 		return (-1);
    114 	}
    115 	/*
    116 	 * Map string table into our address space.  This gives us
    117 	 * an easy way to randomly access all the strings, without
    118 	 * making the memory allocation permanent as with malloc/free
    119 	 * (i.e., munmap will return it to the system).
    120 	 */
    121 	strsize = st.st_size - stroff;
    122 	strtab = mmap(NULL, (size_t)strsize, PROT_READ, 0, fd, stroff);
    123 	if (strtab == (char *)-1)
    124 		return (-1);
    125 	/*
    126 	 * clean out any left-over information for all valid entries.
    127 	 * Type and value defined to be 0 if not found; historical
    128 	 * versions cleared other and desc as well.  Also figure out
    129 	 * the largest string length so don't read any more of the
    130 	 * string table than we have to.
    131 	 *
    132 	 * XXX clearing anything other than n_type and n_value violates
    133 	 * the semantics given in the man page.
    134 	 */
    135 	nent = 0;
    136 	for (p = list; !ISLAST(p); ++p) {
    137 		p->n_type = 0;
    138 		p->n_other = 0;
    139 		p->n_desc = 0;
    140 		p->n_value = 0;
    141 		++nent;
    142 	}
    143 	if (lseek(fd, symoff, SEEK_SET) == -1)
    144 		return (-1);
    145 
    146 	while (symsize > 0) {
    147 		cc = MIN(symsize, sizeof(nbuf));
    148 		if (read(fd, nbuf, cc) != cc)
    149 			break;
    150 		symsize -= cc;
    151 		for (s = nbuf; cc > 0; ++s, cc -= sizeof(*s)) {
    152 			register int soff = s->n_un.n_strx;
    153 
    154 			if (soff == 0 || (s->n_type & N_STAB) != 0)
    155 				continue;
    156 			for (p = list; !ISLAST(p); p++)
    157 				if (!strcmp(&strtab[soff], p->n_un.n_name)) {
    158 					p->n_value = s->n_value;
    159 					p->n_type = s->n_type;
    160 					p->n_desc = s->n_desc;
    161 					p->n_other = s->n_other;
    162 					if (--nent <= 0)
    163 						break;
    164 				}
    165 		}
    166 	}
    167 	munmap(strtab, strsize);
    168 	return (nent);
    169 }
    170 #endif /* DO_AOUT */
    171 
    172 #ifdef DO_ECOFF
    173 #define check(off, size)	((off < 0) || (off + size > mappedsize))
    174 #define	BAD			do { rv = -1; goto out; } while (0)
    175 #define	BADUNMAP		do { rv = -1; goto unmap; } while (0)
    176 
    177 int
    178 __fdnlist(fd, list)
    179 	register int fd;
    180 	register struct nlist *list;
    181 {
    182 	struct nlist *p;
    183 	struct ecoff_filehdr *filehdrp;
    184 	struct ecoff_symhdr *symhdrp;
    185 	struct ecoff_extsym *esyms;
    186 	struct stat st;
    187 	char *mappedfile;
    188 	size_t mappedsize;
    189 	u_long symhdroff, extstroff;
    190 	u_int symhdrsize;
    191 	int rv, nent;
    192 	long i, nesyms;
    193 
    194 	rv = -3;
    195 
    196 	if (fstat(fd, &st) < 0)
    197 		BAD;
    198 	if (st.st_size > SIZE_T_MAX) {
    199 		errno = EFBIG;
    200 		BAD;
    201 	}
    202 	mappedsize = st.st_size;
    203 	mappedfile = mmap(NULL, mappedsize, PROT_READ, 0, fd, 0);
    204 	if (mappedfile == (char *)-1)
    205 		BAD;
    206 
    207 	if (check(0, sizeof *filehdrp))
    208 		BADUNMAP;
    209 	filehdrp = (struct ecoff_filehdr *)&mappedfile[0];
    210 
    211 	if (ECOFF_BADMAG(filehdrp))
    212 		BADUNMAP;
    213 
    214 	symhdroff = filehdrp->ef_symptr;
    215 	symhdrsize = filehdrp->ef_syms;
    216 
    217 	if (check(symhdroff, sizeof *symhdrp) ||
    218 	    sizeof *symhdrp != symhdrsize)
    219 		BADUNMAP;
    220 	symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
    221 
    222 	nesyms = symhdrp->sh_esymmax;
    223 	if (check(symhdrp->sh_esymoff, nesyms * sizeof *esyms))
    224 		BADUNMAP;
    225 	esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->sh_esymoff];
    226 	extstroff = symhdrp->sh_estroff;
    227 
    228 	/*
    229 	 * clean out any left-over information for all valid entries.
    230 	 * Type and value defined to be 0 if not found; historical
    231 	 * versions cleared other and desc as well.
    232 	 *
    233 	 * XXX clearing anything other than n_type and n_value violates
    234 	 * the semantics given in the man page.
    235 	 */
    236 	nent = 0;
    237 	for (p = list; !ISLAST(p); ++p) {
    238 		p->n_type = 0;
    239 		p->n_other = 0;
    240 		p->n_desc = 0;
    241 		p->n_value = 0;
    242 		++nent;
    243 	}
    244 
    245 	for (i = 0; i < nesyms; i++) {
    246 		for (p = list; !ISLAST(p); p++) {
    247 			char *nlistname;
    248 			char *symtabname;
    249 
    250 			nlistname = p->n_un.n_name;
    251 			if (*nlistname == '_')
    252 				nlistname++;
    253 			symtabname =
    254 			    &mappedfile[extstroff + esyms[i].es_strindex];
    255 
    256 			if (!strcmp(symtabname, nlistname)) {
    257 				p->n_value = esyms[i].es_value;
    258 				p->n_type = N_EXT;		/* XXX */
    259 				p->n_desc = 0;			/* XXX */
    260 				p->n_other = 0;			/* XXX */
    261 				if (--nent <= 0)
    262 					break;
    263 			}
    264 		}
    265 	}
    266 	rv = nent;
    267 
    268 unmap:
    269 	munmap(mappedfile, mappedsize);
    270 out:
    271 	return (rv);
    272 }
    273 #endif /* DO_ECOFF */
    274