1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. 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 8.1 (Berkeley) 6/4/93"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/param.h> 39 #include <sys/mman.h> 40 #include <sys/stat.h> 41 #include <sys/file.h> 42 43 #include <errno.h> 44 #include <a.out.h> 45 #include <stdio.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 int __fdnlist __P(( int, struct nlist * )); 50 51 int 52 nlist(name, list) 53 const char *name; 54 struct nlist *list; 55 { 56 int fd, n; 57 58 fd = open(name, O_RDONLY, 0); 59 if (fd < 0) 60 return (-1); 61 n = __fdnlist(fd, list); 62 (void)close(fd); 63 return (n); 64 } 65 66 #define ISLAST(p) (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0) 67 68 int 69 __fdnlist(fd, list) 70 register int fd; 71 register struct nlist *list; 72 { 73 register struct nlist *p, *symtab; 74 register caddr_t strtab, a_out_mmap; 75 register off_t stroff, symoff; 76 register u_long symsize; 77 register int nent; 78 struct exec * exec; 79 struct stat st; 80 81 /* check that file is at least as large as struct exec! */ 82 if ((fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec))) 83 return (-1); 84 85 /* Check for files too large to mmap. */ 86 if (st.st_size > SIZE_T_MAX) { 87 errno = EFBIG; 88 return (-1); 89 } 90 91 /* 92 * Map the whole a.out file into our address space. 93 * We then find the string table withing this area. 94 * We do not just mmap the string table, as it probably 95 * does not start at a page boundary - we save ourselves a 96 * lot of nastiness by mmapping the whole file. 97 * 98 * This gives us an easy way to randomly access all the strings, 99 * without making the memory allocation permanent as with 100 * malloc/free (i.e., munmap will return it to the system). 101 */ 102 a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0); 103 if (a_out_mmap == MAP_FAILED) 104 return (-1); 105 106 exec = (struct exec *)a_out_mmap; 107 if (N_BADMAG(*exec)) { 108 munmap(a_out_mmap, (size_t)st.st_size); 109 return (-1); 110 } 111 112 symoff = N_SYMOFF(*exec); 113 symsize = exec->a_syms; 114 stroff = symoff + symsize; 115 116 /* find the string table in our mmapped area */ 117 strtab = a_out_mmap + stroff; 118 symtab = (struct nlist *)(a_out_mmap + symoff); 119 120 /* 121 * clean out any left-over information for all valid entries. 122 * Type and value defined to be 0 if not found; historical 123 * versions cleared other and desc as well. Also figure out 124 * the largest string length so don't read any more of the 125 * string table than we have to. 126 * 127 * XXX clearing anything other than n_type and n_value violates 128 * the semantics given in the man page. 129 */ 130 nent = 0; 131 for (p = list; !ISLAST(p); ++p) { 132 p->n_type = 0; 133 p->n_other = 0; 134 p->n_desc = 0; 135 p->n_value = 0; 136 ++nent; 137 } 138 139 while (symsize > 0) { 140 register int soff; 141 142 symsize-= sizeof(struct nlist); 143 soff = symtab->n_un.n_strx; 144 145 146 if (soff != 0 && (symtab->n_type & N_STAB) == 0) 147 for (p = list; !ISLAST(p); p++) 148 if (!strcmp(&strtab[soff], p->n_un.n_name)) { 149 p->n_value = symtab->n_value; 150 p->n_type = symtab->n_type; 151 p->n_desc = symtab->n_desc; 152 p->n_other = symtab->n_other; 153 if (--nent <= 0) 154 break; 155 } 156 symtab++; 157 } 158 munmap(a_out_mmap, (size_t)st.st_size); 159 return (nent); 160 } 161