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[] = "from: @(#)nlist.c 5.8 (Berkeley) 2/23/91";*/ 36 static char rcsid[] = "$Id: nlist.c,v 1.2 1993/07/30 08:22:55 mycroft Exp $"; 37 #endif /* LIBC_SCCS and not lint */ 38 39 #include <sys/types.h> 40 #include <sys/file.h> 41 #include <a.out.h> 42 #include <stdio.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 typedef struct nlist NLIST; 47 #define _strx n_un.n_strx 48 #define _name n_un.n_name 49 #define ISVALID(p) (p->_name && p->_name[0]) 50 51 int 52 nlist(name, list) 53 const char *name; 54 NLIST *list; 55 { 56 register NLIST *p, *s; 57 struct exec ebuf; 58 FILE *fstr, *fsym; 59 NLIST nbuf; 60 off_t strings_offset, symbol_offset, symbol_size, lseek(); 61 int entries, len, maxlen; 62 char sbuf[256]; 63 64 entries = -1; 65 66 if (!(fsym = fopen(name, "r"))) 67 return(-1); 68 if (fread((char *)&ebuf, sizeof(struct exec), 1, fsym) != 1 || 69 N_BADMAG(ebuf)) 70 goto done1; 71 72 symbol_offset = N_SYMOFF(ebuf); 73 symbol_size = ebuf.a_syms; 74 strings_offset = symbol_offset + symbol_size; 75 if (fseek(fsym, symbol_offset, SEEK_SET)) 76 goto done1; 77 78 if (!(fstr = fopen(name, "r"))) 79 goto done1; 80 81 /* 82 * clean out any left-over information for all valid entries. 83 * Type and value defined to be 0 if not found; historical 84 * versions cleared other and desc as well. Also figure out 85 * the largest string length so don't read any more of the 86 * string table than we have to. 87 */ 88 for (p = list, entries = maxlen = 0; ISVALID(p); ++p, ++entries) { 89 p->n_type = 0; 90 p->n_other = 0; 91 p->n_desc = 0; 92 p->n_value = 0; 93 if ((len = strlen(p->_name)) > maxlen) 94 maxlen = len; 95 } 96 if (++maxlen > sizeof(sbuf)) { /* for the NULL */ 97 (void)fprintf(stderr, "nlist: symbol too large.\n"); 98 entries = -1; 99 goto done2; 100 } 101 102 for (s = &nbuf; symbol_size; symbol_size -= sizeof(NLIST)) { 103 if (fread((char *)s, sizeof(NLIST), 1, fsym) != 1) 104 goto done2; 105 if (!s->_strx || s->n_type&N_STAB) 106 continue; 107 if (fseek(fstr, strings_offset + s->_strx, SEEK_SET)) 108 goto done2; 109 (void)fread(sbuf, sizeof(sbuf[0]), maxlen, fstr); 110 for (p = list; ISVALID(p); p++) 111 if (!strcmp(p->_name, sbuf)) { 112 p->n_value = s->n_value; 113 p->n_type = s->n_type; 114 p->n_desc = s->n_desc; 115 p->n_other = s->n_other; 116 if (!--entries) 117 goto done2; 118 } 119 } 120 done2: (void)fclose(fstr); 121 done1: (void)fclose(fsym); 122 return(entries); 123 } 124