nlist.c revision 1.7 1 /*-
2 * Copyright (c) 1990 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 #ifndef lint
35 /*static char sccsid[] = "from: @(#)nlist.c 5.4 (Berkeley) 4/27/91";*/
36 static char rcsid[] = "$Id: nlist.c,v 1.7 1993/12/02 20:55:52 pk Exp $";
37 #endif /* not lint */
38
39 #include <sys/param.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <a.out.h>
43 #include <db.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <kvm.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <stdlib.h>
50
51 typedef struct nlist NLIST;
52 #define _strx n_un.n_strx
53 #define _name n_un.n_name
54
55 static char *kfile;
56
57 extern void cleanup();
58 static void badread();
59 static void badfmt();
60
61 void
62 create_knlist(name, db)
63 char *name;
64 DB *db;
65 {
66 register int nsyms;
67 struct exec ebuf;
68 FILE *fp;
69 NLIST nbuf;
70 DBT data, key;
71 int fd, nr, strsize;
72 char *strtab, buf[1024];
73
74 kfile = name;
75 if ((fd = open(name, O_RDONLY, 0)) < 0)
76 error(name);
77
78 /* Read in exec structure. */
79 nr = read(fd, (char *)&ebuf, sizeof(struct exec));
80 if (nr != sizeof(struct exec))
81 badfmt(nr, "no exec header");
82
83 /* Check magic number and symbol count. */
84 if (N_BADMAG(ebuf))
85 badfmt("bad magic number");
86 if (!ebuf.a_syms)
87 badfmt("stripped");
88
89 /* Seek to string table. */
90 if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
91 badfmt("corrupted string table");
92
93 /* Read in the size of the symbol table. */
94 nr = read(fd, (char *)&strsize, sizeof(strsize));
95 if (nr != sizeof(strsize))
96 badread(nr, "no symbol table");
97
98 /* Read in the string table. */
99 strsize -= sizeof(strsize);
100 if (!(strtab = (char *)malloc(strsize)))
101 error(name);
102 if ((nr = read(fd, strtab, strsize)) != strsize)
103 badread(nr, "corrupted symbol table");
104
105 /* Seek to symbol table. */
106 if (!(fp = fdopen(fd, "r")))
107 error(name);
108 if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
109 error(name);
110
111 data.data = (u_char *)&nbuf;
112 data.size = sizeof(NLIST);
113
114 /* Read each symbol and enter it into the database. */
115 nsyms = ebuf.a_syms / sizeof(struct nlist);
116 while (nsyms--) {
117 if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
118 if (feof(fp))
119 badfmt("corrupted symbol table");
120 error(name);
121 }
122 if (!nbuf._strx || nbuf.n_type&N_STAB)
123 continue;
124
125 key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
126 key.size = strlen((char *)key.data);
127 if ((db->put)(db, &key, &data, 0))
128 error("put");
129
130 if (!strncmp((char *)key.data, VRS_SYM, sizeof(VRS_SYM) - 1)) {
131 off_t cur_off, rel_off, vers_off;
132
133 /* Offset relative to start of text image in VM. */
134 #ifdef hp300
135 rel_off = nbuf.n_value;
136 #endif
137 #ifdef tahoe
138 /*
139 * On tahoe, first 0x800 is reserved for communication
140 * with the console processor.
141 */
142 rel_off = ((nbuf.n_value & ~KERNBASE) - 0x800);
143 #endif
144 #ifdef vax
145 rel_off = nbuf.n_value & ~KERNBASE;
146 #endif
147 #ifdef sparc
148 /*
149 * On sparc, first 0x4000 is reserved for ?
150 */
151 rel_off = (nbuf.n_value & ~KERNBASE) - 0x4000;
152 #endif
153 #ifdef i386
154 /*
155 * XXX: This is a KLUGE to handle the kernel being
156 * loaded at a different address than KERNBASE. Stupid
157 * a.out format has no way of recording the text
158 * address we gave ld. It only works for multiples of
159 * 1MB.
160 */
161 rel_off = ((nbuf.n_value - (ebuf.a_entry & -0x100000))
162 + CLBYTES);
163 #endif
164 /*
165 * When loaded, data is rounded to next page cluster
166 * after text, but not in file.
167 */
168 if (ebuf.a_text % CLBYTES)
169 rel_off -= CLBYTES - (ebuf.a_text % CLBYTES);
170 vers_off = N_TXTOFF(ebuf) + rel_off;
171
172 cur_off = ftell(fp);
173 if (fseek(fp, vers_off, SEEK_SET) == -1)
174 badfmt("corrupted string table");
175
176 /*
177 * Read version string up to, and including newline.
178 * This code assumes that a newline terminates the
179 * version line.
180 */
181 if (fgets(buf, sizeof(buf), fp) == NULL)
182 badfmt("corrupted string table");
183
184 key.data = (u_char *)VRS_KEY;
185 key.size = sizeof(VRS_KEY) - 1;
186 data.data = (u_char *)buf;
187 data.size = strlen(buf);
188 if ((db->put)(db, &key, &data, 0))
189 error("put");
190
191 /* Restore to original values. */
192 data.data = (u_char *)&nbuf;
193 data.size = sizeof(NLIST);
194 if (fseek(fp, cur_off, SEEK_SET) == -1)
195 badfmt("corrupted string table");
196 }
197 }
198 (void)fclose(fp);
199 }
200
201 static void
202 badread(nr, p)
203 int nr;
204 char *p;
205 {
206 if (nr < 0)
207 error(kfile);
208 badfmt(p);
209 }
210
211 static void
212 badfmt(p)
213 char *p;
214 {
215 (void)fprintf(stderr,
216 "kvm_mkdb: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
217 cleanup();
218 exit(1);
219 }
220