1 /* $NetBSD: nlist_aout.c,v 1.8 2003/08/07 11:25:23 agc Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgement: 45 * This product includes software developed by the University of 46 * California, Berkeley and its contributors. 47 * 4. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 */ 63 64 #include <sys/cdefs.h> 65 #ifndef lint 66 #if 0 67 static char sccsid[] = "from: @(#)nlist.c 8.1 (Berkeley) 6/6/93"; 68 #else 69 __RCSID("$NetBSD: nlist_aout.c,v 1.8 2003/08/07 11:25:23 agc Exp $"); 70 #endif 71 #endif /* not lint */ 72 73 #include <sys/param.h> 74 75 #include <a.out.h> 76 #include <db.h> 77 #include <err.h> 78 #include <errno.h> 79 #include <fcntl.h> 80 #include <kvm.h> 81 #include <limits.h> 82 #include <stdio.h> 83 #include <stdlib.h> 84 #include <string.h> 85 #include <unistd.h> 86 87 #include "extern.h" 88 89 #ifdef NLIST_AOUT 90 91 typedef struct nlist NLIST; 92 #define _strx n_un.n_strx 93 #define _name n_un.n_name 94 95 #define badfmt(str) \ 96 do { \ 97 warnx("%s: %s: %s", kfile, str, strerror(EFTYPE)); \ 98 punt(); \ 99 } while (0) 100 101 static void badread __P((int, char *)); 102 static u_long get_kerntext __P((const char *kfn)); 103 104 static const char *kfile; 105 106 int 107 create_knlist_aout(name, db) 108 const char *name; 109 DB *db; 110 { 111 int nsyms; 112 struct exec ebuf; 113 FILE *fp; 114 NLIST nbuf; 115 DBT data, key; 116 int fd, nr, strsize; 117 u_long kerntextoff; 118 char *strtab, buf[1024]; 119 120 kfile = name; 121 if ((fd = open(name, O_RDONLY, 0)) < 0) { 122 warn("%s", kfile); 123 punt(); 124 } 125 126 /* Read in exec structure. */ 127 nr = read(fd, &ebuf, sizeof(struct exec)); 128 if (nr != sizeof(struct exec)) 129 return (-1); 130 131 /* Check magic number. */ 132 if (N_BADMAG(ebuf)) 133 return (-1); 134 135 /* 136 * We've recognized it as an a.out binary. From here 137 * on out, all errors are fatal. 138 */ 139 140 /* Check symbol count. */ 141 if (!ebuf.a_syms) 142 badfmt("stripped"); 143 144 /* Seek to string table. */ 145 if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1) 146 badfmt("corrupted string table"); 147 148 /* Read in the size of the symbol table. */ 149 nr = read(fd, (char *)&strsize, sizeof(strsize)); 150 if (nr != sizeof(strsize)) 151 badread(nr, "no symbol table"); 152 153 /* Read in the string table. */ 154 strsize -= sizeof(strsize); 155 if (!(strtab = malloc(strsize))) { 156 warn("malloc"); 157 punt(); 158 } 159 if ((nr = read(fd, strtab, strsize)) != strsize) 160 badread(nr, "corrupted symbol table"); 161 162 /* Seek to symbol table. */ 163 if (!(fp = fdopen(fd, "r"))) { 164 warn("%s", name); 165 punt(); 166 } 167 if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1) { 168 warn("%s", name); 169 punt(); 170 } 171 172 data.data = (u_char *)&nbuf; 173 data.size = sizeof(NLIST); 174 175 kerntextoff = get_kerntext(name); 176 177 /* Read each symbol and enter it into the database. */ 178 nsyms = ebuf.a_syms / sizeof(struct nlist); 179 while (nsyms--) { 180 if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) { 181 if (feof(fp)) 182 badfmt("corrupted symbol table"); 183 warn("%s", name); 184 punt(); 185 } 186 if (!nbuf._strx || nbuf.n_type&N_STAB) 187 continue; 188 189 key.data = (u_char *)strtab + nbuf._strx - sizeof(long); 190 key.size = strlen((char *)key.data); 191 if (db->put(db, &key, &data, 0)) { 192 warn("record enter"); 193 punt(); 194 } 195 196 if (strcmp((char *)key.data, VRS_SYM) == 0) { 197 long cur_off, voff; 198 /* 199 * Calculate offset relative to a normal (non-kernel) 200 * a.out. Kerntextoff is where the kernel is really 201 * loaded; N_TXTADDR is where a normal file is loaded. 202 * From there, locate file offset in text or data. 203 */ 204 voff = nbuf.n_value - kerntextoff + N_TXTADDR(ebuf); 205 if ((nbuf.n_type & N_TYPE) == N_TEXT) 206 voff += N_TXTOFF(ebuf) - N_TXTADDR(ebuf); 207 else 208 voff += N_DATOFF(ebuf) - N_DATADDR(ebuf); 209 cur_off = ftell(fp); 210 if (fseek(fp, voff, SEEK_SET) == -1) 211 badfmt("corrupted string table"); 212 213 /* 214 * Read version string up to, and including newline. 215 * This code assumes that a newline terminates the 216 * version line. 217 */ 218 if (fgets(buf, sizeof(buf), fp) == NULL) 219 badfmt("corrupted string table"); 220 221 key.data = (u_char *)VRS_KEY; 222 key.size = sizeof(VRS_KEY) - 1; 223 data.data = (u_char *)buf; 224 data.size = strlen(buf); 225 if (db->put(db, &key, &data, 0)) { 226 warn("record enter"); 227 punt(); 228 } 229 230 /* Restore to original values. */ 231 data.data = (u_char *)&nbuf; 232 data.size = sizeof(NLIST); 233 if (fseek(fp, cur_off, SEEK_SET) == -1) 234 badfmt("corrupted string table"); 235 } 236 } 237 (void)fclose(fp); 238 239 return (0); 240 } 241 242 static void 243 badread(nr, p) 244 int nr; 245 char *p; 246 { 247 if (nr < 0) { 248 warn("%s", kfile); 249 punt(); 250 } 251 badfmt(p); 252 } 253 254 /* 255 * Instead of compiling in KERNTEXTOFF or KERNBASE, try to 256 * determine the text start address from a standard symbol. 257 * For backward compatibility, use the old compiled-in way 258 * when the standard symbol name is not found. 259 */ 260 static u_long 261 get_kerntext(name) 262 const char *name; 263 { 264 struct nlist nl[2]; 265 266 memset((caddr_t)nl, 0, sizeof(nl)); 267 nl[0].n_un.n_name = "_kernel_text"; 268 269 if (nlist(name, nl) != 0) { 270 warnx("%s: %s symbol missing", 271 name, nl[0].n_un.n_name); 272 punt(); 273 } 274 275 return (nl[0].n_value); 276 } 277 #endif /* NLIST_AOUT */ 278