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