Home | History | Annotate | Line # | Download | only in gen
nlist.c revision 1.1
      1 /*
      2  * Copyright (c) 1989 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #if defined(LIBC_SCCS) && !defined(lint)
     35 static char sccsid[] = "@(#)nlist.c	5.8 (Berkeley) 2/23/91";
     36 #endif /* LIBC_SCCS and not lint */
     37 
     38 #include <sys/types.h>
     39 #include <sys/file.h>
     40 #include <a.out.h>
     41 #include <stdio.h>
     42 #include <string.h>
     43 #include <unistd.h>
     44 
     45 typedef struct nlist NLIST;
     46 #define	_strx	n_un.n_strx
     47 #define	_name	n_un.n_name
     48 #define	ISVALID(p)	(p->_name && p->_name[0])
     49 
     50 int
     51 nlist(name, list)
     52 	const char *name;
     53 	NLIST *list;
     54 {
     55 	register NLIST *p, *s;
     56 	struct exec ebuf;
     57 	FILE *fstr, *fsym;
     58 	NLIST nbuf;
     59 	off_t strings_offset, symbol_offset, symbol_size, lseek();
     60 	int entries, len, maxlen;
     61 	char sbuf[256];
     62 
     63 	entries = -1;
     64 
     65 	if (!(fsym = fopen(name, "r")))
     66 		return(-1);
     67 	if (fread((char *)&ebuf, sizeof(struct exec), 1, fsym) != 1 ||
     68 	    N_BADMAG(ebuf))
     69 		goto done1;
     70 
     71 	symbol_offset = N_SYMOFF(ebuf);
     72 	symbol_size = ebuf.a_syms;
     73 	strings_offset = symbol_offset + symbol_size;
     74 	if (fseek(fsym, symbol_offset, SEEK_SET))
     75 		goto done1;
     76 
     77 	if (!(fstr = fopen(name, "r")))
     78 		goto done1;
     79 
     80 	/*
     81 	 * clean out any left-over information for all valid entries.
     82 	 * Type and value defined to be 0 if not found; historical
     83 	 * versions cleared other and desc as well.  Also figure out
     84 	 * the largest string length so don't read any more of the
     85 	 * string table than we have to.
     86 	 */
     87 	for (p = list, entries = maxlen = 0; ISVALID(p); ++p, ++entries) {
     88 		p->n_type = 0;
     89 		p->n_other = 0;
     90 		p->n_desc = 0;
     91 		p->n_value = 0;
     92 		if ((len = strlen(p->_name)) > maxlen)
     93 			maxlen = len;
     94 	}
     95 	if (++maxlen > sizeof(sbuf)) {		/* for the NULL */
     96 		(void)fprintf(stderr, "nlist: symbol too large.\n");
     97 		entries = -1;
     98 		goto done2;
     99 	}
    100 
    101 	for (s = &nbuf; symbol_size; symbol_size -= sizeof(NLIST)) {
    102 		if (fread((char *)s, sizeof(NLIST), 1, fsym) != 1)
    103 			goto done2;
    104 		if (!s->_strx || s->n_type&N_STAB)
    105 			continue;
    106 		if (fseek(fstr, strings_offset + s->_strx, SEEK_SET))
    107 			goto done2;
    108 		(void)fread(sbuf, sizeof(sbuf[0]), maxlen, fstr);
    109 		for (p = list; ISVALID(p); p++)
    110 			if (!strcmp(p->_name, sbuf)) {
    111 				p->n_value = s->n_value;
    112 				p->n_type = s->n_type;
    113 				p->n_desc = s->n_desc;
    114 				p->n_other = s->n_other;
    115 				if (!--entries)
    116 					goto done2;
    117 			}
    118 	}
    119 done2:	(void)fclose(fstr);
    120 done1:	(void)fclose(fsym);
    121 	return(entries);
    122 }
    123