1 /*- 2 * Copyright (c) 1990, 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 #ifndef lint 35 /*static char sccsid[] = "from: @(#)nlist.c 8.1 (Berkeley) 6/6/93";*/ 36 static char *rcsid = "$Id: nlist.c,v 1.10 1994/06/11 07:57:41 mycroft Exp $"; 37 #endif /* not lint */ 38 39 #include <sys/param.h> 40 41 #include <a.out.h> 42 #include <db.h> 43 #include <err.h> 44 #include <errno.h> 45 #include <fcntl.h> 46 #include <kvm.h> 47 #include <limits.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 53 #include "extern.h" 54 55 typedef struct nlist NLIST; 56 #define _strx n_un.n_strx 57 #define _name n_un.n_name 58 59 #define badfmt(str) errx(1, "%s: %s: %s", kfile, str, strerror(EFTYPE)) 60 61 static void badread __P((int, char *)); 62 63 static char *kfile; 64 65 void 66 create_knlist(name, db) 67 char *name; 68 DB *db; 69 { 70 register int nsyms; 71 struct exec ebuf; 72 FILE *fp; 73 NLIST nbuf; 74 DBT data, key; 75 int fd, nr, strsize; 76 char *strtab, buf[1024]; 77 78 kfile = name; 79 if ((fd = open(name, O_RDONLY, 0)) < 0) 80 err(1, "%s", name); 81 82 /* Read in exec structure. */ 83 nr = read(fd, &ebuf, sizeof(struct exec)); 84 if (nr != sizeof(struct exec)) 85 badfmt("no exec header"); 86 87 /* Check magic number and symbol count. */ 88 if (N_BADMAG(ebuf)) 89 badfmt("bad magic number"); 90 if (!ebuf.a_syms) 91 badfmt("stripped"); 92 93 /* Seek to string table. */ 94 if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1) 95 badfmt("corrupted string table"); 96 97 /* Read in the size of the symbol table. */ 98 nr = read(fd, (char *)&strsize, sizeof(strsize)); 99 if (nr != sizeof(strsize)) 100 badread(nr, "no symbol table"); 101 102 /* Read in the string table. */ 103 strsize -= sizeof(strsize); 104 if (!(strtab = malloc(strsize))) 105 err(1, NULL); 106 if ((nr = read(fd, strtab, strsize)) != strsize) 107 badread(nr, "corrupted symbol table"); 108 109 /* Seek to symbol table. */ 110 if (!(fp = fdopen(fd, "r"))) 111 err(1, "%s", name); 112 if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1) 113 err(1, "%s", name); 114 115 data.data = (u_char *)&nbuf; 116 data.size = sizeof(NLIST); 117 118 /* Read each symbol and enter it into the database. */ 119 nsyms = ebuf.a_syms / sizeof(struct nlist); 120 while (nsyms--) { 121 if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) { 122 if (feof(fp)) 123 badfmt("corrupted symbol table"); 124 err(1, "%s", name); 125 } 126 if (!nbuf._strx || nbuf.n_type&N_STAB) 127 continue; 128 129 key.data = (u_char *)strtab + nbuf._strx - sizeof(long); 130 key.size = strlen((char *)key.data); 131 if (db->put(db, &key, &data, 0)) 132 err(1, "record enter"); 133 134 if (strcmp((char *)key.data, VRS_SYM) == 0) { 135 long cur_off, voff; 136 #ifndef KERNTEXTOFF 137 #define KERNTEXTOFF KERNBASE 138 #endif 139 /* 140 * Calculate offset relative to a normal (non-kernel) 141 * a.out. KERNTEXTOFF is where the kernel is really 142 * loaded; N_TXTADDR is where a normal file is loaded. 143 * From there, locate file offset in text or data. 144 */ 145 voff = nbuf.n_value - KERNTEXTOFF + N_TXTADDR(ebuf); 146 if ((nbuf.n_type & N_TYPE) == N_TEXT) 147 voff += N_TXTOFF(ebuf) - N_TXTADDR(ebuf); 148 else 149 voff += N_DATOFF(ebuf) - N_DATADDR(ebuf); 150 cur_off = ftell(fp); 151 if (fseek(fp, voff, SEEK_SET) == -1) 152 badfmt("corrupted string table"); 153 154 /* 155 * Read version string up to, and including newline. 156 * This code assumes that a newline terminates the 157 * version line. 158 */ 159 if (fgets(buf, sizeof(buf), fp) == NULL) 160 badfmt("corrupted string table"); 161 162 key.data = (u_char *)VRS_KEY; 163 key.size = sizeof(VRS_KEY) - 1; 164 data.data = (u_char *)buf; 165 data.size = strlen(buf); 166 if (db->put(db, &key, &data, 0)) 167 err(1, "record enter"); 168 169 /* Restore to original values. */ 170 data.data = (u_char *)&nbuf; 171 data.size = sizeof(NLIST); 172 if (fseek(fp, cur_off, SEEK_SET) == -1) 173 badfmt("corrupted string table"); 174 } 175 } 176 (void)fclose(fp); 177 } 178 179 static void 180 badread(nr, p) 181 int nr; 182 char *p; 183 { 184 if (nr < 0) 185 err(1, "%s", kfile); 186 badfmt(p); 187 } 188