nlist.c revision 1.4.2.2 1 1.4.2.2 mycroft /*-
2 1.4.2.2 mycroft * Copyright (c) 1990 The Regents of the University of California.
3 1.4.2.2 mycroft * All rights reserved.
4 1.4.2.2 mycroft *
5 1.4.2.2 mycroft * Redistribution and use in source and binary forms, with or without
6 1.4.2.2 mycroft * modification, are permitted provided that the following conditions
7 1.4.2.2 mycroft * are met:
8 1.4.2.2 mycroft * 1. Redistributions of source code must retain the above copyright
9 1.4.2.2 mycroft * notice, this list of conditions and the following disclaimer.
10 1.4.2.2 mycroft * 2. Redistributions in binary form must reproduce the above copyright
11 1.4.2.2 mycroft * notice, this list of conditions and the following disclaimer in the
12 1.4.2.2 mycroft * documentation and/or other materials provided with the distribution.
13 1.4.2.2 mycroft * 3. All advertising materials mentioning features or use of this software
14 1.4.2.2 mycroft * must display the following acknowledgement:
15 1.4.2.2 mycroft * This product includes software developed by the University of
16 1.4.2.2 mycroft * California, Berkeley and its contributors.
17 1.4.2.2 mycroft * 4. Neither the name of the University nor the names of its contributors
18 1.4.2.2 mycroft * may be used to endorse or promote products derived from this software
19 1.4.2.2 mycroft * without specific prior written permission.
20 1.4.2.2 mycroft *
21 1.4.2.2 mycroft * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.4.2.2 mycroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.4.2.2 mycroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.4.2.2 mycroft * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.4.2.2 mycroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.4.2.2 mycroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.4.2.2 mycroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.4.2.2 mycroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.4.2.2 mycroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.4.2.2 mycroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.4.2.2 mycroft * SUCH DAMAGE.
32 1.4.2.2 mycroft *
33 1.4.2.2 mycroft * PATCHES MAGIC LEVEL PATCH THAT GOT US HERE
34 1.4.2.2 mycroft * -------------------- ----- ----------------------
35 1.4.2.2 mycroft * CURRENT PATCH LEVEL: 1 00032
36 1.4.2.2 mycroft * -------------------- ----- ----------------------
37 1.4.2.2 mycroft *
38 1.4.2.2 mycroft * 05 Aug 92 David Greenman Fix kernel namelist db create/use
39 1.4.2.2 mycroft */
40 1.4.2.2 mycroft
41 1.4.2.2 mycroft #ifndef lint
42 1.4.2.2 mycroft static char sccsid[] = "@(#)nlist.c 5.4 (Berkeley) 4/27/91";
43 1.4.2.2 mycroft #endif /* not lint */
44 1.4.2.2 mycroft
45 1.4.2.2 mycroft #include <sys/param.h>
46 1.4.2.2 mycroft #include <fcntl.h>
47 1.4.2.2 mycroft #include <limits.h>
48 1.4.2.2 mycroft #include <a.out.h>
49 1.4.2.2 mycroft #include <db.h>
50 1.4.2.2 mycroft #include <errno.h>
51 1.4.2.2 mycroft #include <unistd.h>
52 1.4.2.2 mycroft #include <kvm.h>
53 1.4.2.2 mycroft #include <stdio.h>
54 1.4.2.2 mycroft #include <string.h>
55 1.4.2.2 mycroft #include <stdlib.h>
56 1.4.2.2 mycroft
57 1.4.2.2 mycroft typedef struct nlist NLIST;
58 1.4.2.2 mycroft #define _strx n_un.n_strx
59 1.4.2.2 mycroft #define _name n_un.n_name
60 1.4.2.2 mycroft
61 1.4.2.2 mycroft static char *kfile;
62 1.4.2.2 mycroft
63 1.4.2.2 mycroft create_knlist(name, db)
64 1.4.2.2 mycroft char *name;
65 1.4.2.2 mycroft DB *db;
66 1.4.2.2 mycroft {
67 1.4.2.2 mycroft register int nsyms;
68 1.4.2.2 mycroft struct exec ebuf;
69 1.4.2.2 mycroft FILE *fp;
70 1.4.2.2 mycroft NLIST nbuf;
71 1.4.2.2 mycroft DBT data, key;
72 1.4.2.2 mycroft int fd, nr, strsize;
73 1.4.2.2 mycroft char *strtab, buf[1024];
74 1.4.2.2 mycroft
75 1.4.2.2 mycroft kfile = name;
76 1.4.2.2 mycroft if ((fd = open(name, O_RDONLY, 0)) < 0)
77 1.4.2.2 mycroft error(name);
78 1.4.2.2 mycroft
79 1.4.2.2 mycroft /* Read in exec structure. */
80 1.4.2.2 mycroft nr = read(fd, (char *)&ebuf, sizeof(struct exec));
81 1.4.2.2 mycroft if (nr != sizeof(struct exec))
82 1.4.2.2 mycroft badfmt(nr, "no exec header");
83 1.4.2.2 mycroft
84 1.4.2.2 mycroft /* Check magic number and symbol count. */
85 1.4.2.2 mycroft if (N_BADMAG(ebuf))
86 1.4.2.2 mycroft badfmt("bad magic number");
87 1.4.2.2 mycroft if (!ebuf.a_syms)
88 1.4.2.2 mycroft badfmt("stripped");
89 1.4.2.2 mycroft
90 1.4.2.2 mycroft /* Seek to string table. */
91 1.4.2.2 mycroft if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
92 1.4.2.2 mycroft badfmt("corrupted string table");
93 1.4.2.2 mycroft
94 1.4.2.2 mycroft /* Read in the size of the symbol table. */
95 1.4.2.2 mycroft nr = read(fd, (char *)&strsize, sizeof(strsize));
96 1.4.2.2 mycroft if (nr != sizeof(strsize))
97 1.4.2.2 mycroft badread(nr, "no symbol table");
98 1.4.2.2 mycroft
99 1.4.2.2 mycroft /* Read in the string table. */
100 1.4.2.2 mycroft strsize -= sizeof(strsize);
101 1.4.2.2 mycroft if (!(strtab = (char *)malloc(strsize)))
102 1.4.2.2 mycroft error(name);
103 1.4.2.2 mycroft if ((nr = read(fd, strtab, strsize)) != strsize)
104 1.4.2.2 mycroft badread(nr, "corrupted symbol table");
105 1.4.2.2 mycroft
106 1.4.2.2 mycroft /* Seek to symbol table. */
107 1.4.2.2 mycroft if (!(fp = fdopen(fd, "r")))
108 1.4.2.2 mycroft error(name);
109 1.4.2.2 mycroft if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
110 1.4.2.2 mycroft error(name);
111 1.4.2.2 mycroft
112 1.4.2.2 mycroft data.data = (u_char *)&nbuf;
113 1.4.2.2 mycroft data.size = sizeof(NLIST);
114 1.4.2.2 mycroft
115 1.4.2.2 mycroft /* Read each symbol and enter it into the database. */
116 1.4.2.2 mycroft nsyms = ebuf.a_syms / sizeof(struct nlist);
117 1.4.2.2 mycroft while (nsyms--) {
118 1.4.2.2 mycroft if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
119 1.4.2.2 mycroft if (feof(fp))
120 1.4.2.2 mycroft badfmt("corrupted symbol table");
121 1.4.2.2 mycroft error(name);
122 1.4.2.2 mycroft }
123 1.4.2.2 mycroft if (!nbuf._strx || nbuf.n_type&N_STAB)
124 1.4.2.2 mycroft continue;
125 1.4.2.2 mycroft
126 1.4.2.2 mycroft key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
127 1.4.2.2 mycroft key.size = strlen((char *)key.data);
128 1.4.2.2 mycroft if ((db->put)(db, &key, &data, 0))
129 1.4.2.2 mycroft error("put");
130 1.4.2.2 mycroft
131 1.4.2.2 mycroft if (!strncmp((char *)key.data, VRS_SYM, sizeof(VRS_SYM) - 1)) {
132 1.4.2.2 mycroft off_t cur_off, rel_off, vers_off;
133 1.4.2.2 mycroft
134 1.4.2.2 mycroft /* Offset relative to start of text image in VM. */
135 1.4.2.2 mycroft #ifdef hp300
136 1.4.2.2 mycroft rel_off = nbuf.n_value;
137 1.4.2.2 mycroft #endif
138 1.4.2.2 mycroft #ifdef tahoe
139 1.4.2.2 mycroft /*
140 1.4.2.2 mycroft * On tahoe, first 0x800 is reserved for communication
141 1.4.2.2 mycroft * with the console processor.
142 1.4.2.2 mycroft */
143 1.4.2.2 mycroft rel_off = ((nbuf.n_value & ~KERNBASE) - 0x800);
144 1.4.2.2 mycroft #endif
145 1.4.2.2 mycroft #ifdef vax
146 1.4.2.2 mycroft rel_off = nbuf.n_value & ~KERNBASE;
147 1.4.2.2 mycroft #endif
148 1.4.2.2 mycroft #ifdef i386
149 1.4.2.2 mycroft /*
150 1.4.2.2 mycroft * XXX: This is a KLUGE to handle the kernel being
151 1.4.2.2 mycroft * loaded at a different address than KERNBASE. Stupid
152 1.4.2.2 mycroft * a.out format has no way of recording the text
153 1.4.2.2 mycroft * address we gave ld. It only works for multiples of
154 1.4.2.2 mycroft * 1MB.
155 1.4.2.2 mycroft */
156 1.4.2.2 mycroft rel_off = ((nbuf.n_value - (ebuf.a_entry & -0x100000))
157 1.4.2.2 mycroft + CLBYTES);
158 1.4.2.2 mycroft #endif
159 1.4.2.2 mycroft /*
160 1.4.2.2 mycroft * When loaded, data is rounded to next page cluster
161 1.4.2.2 mycroft * after text, but not in file.
162 1.4.2.2 mycroft */
163 1.4.2.2 mycroft rel_off -= CLBYTES - (ebuf.a_text % CLBYTES);
164 1.4.2.2 mycroft vers_off = N_TXTOFF(ebuf) + rel_off;
165 1.4.2.2 mycroft
166 1.4.2.2 mycroft cur_off = ftell(fp);
167 1.4.2.2 mycroft if (fseek(fp, vers_off, SEEK_SET) == -1)
168 1.4.2.2 mycroft badfmt("corrupted string table");
169 1.4.2.2 mycroft
170 1.4.2.2 mycroft /*
171 1.4.2.2 mycroft * Read version string up to, and including newline.
172 1.4.2.2 mycroft * This code assumes that a newline terminates the
173 1.4.2.2 mycroft * version line.
174 1.4.2.2 mycroft */
175 1.4.2.2 mycroft if (fgets(buf, sizeof(buf), fp) == NULL)
176 1.4.2.2 mycroft badfmt("corrupted string table");
177 1.4.2.2 mycroft
178 1.4.2.2 mycroft key.data = (u_char *)VRS_KEY;
179 1.4.2.2 mycroft key.size = sizeof(VRS_KEY) - 1;
180 1.4.2.2 mycroft data.data = (u_char *)buf;
181 1.4.2.2 mycroft data.size = strlen(buf);
182 1.4.2.2 mycroft if ((db->put)(db, &key, &data, 0))
183 1.4.2.2 mycroft error("put");
184 1.4.2.2 mycroft
185 1.4.2.2 mycroft /* Restore to original values. */
186 1.4.2.2 mycroft data.data = (u_char *)&nbuf;
187 1.4.2.2 mycroft data.size = sizeof(NLIST);
188 1.4.2.2 mycroft if (fseek(fp, cur_off, SEEK_SET) == -1)
189 1.4.2.2 mycroft badfmt("corrupted string table");
190 1.4.2.2 mycroft }
191 1.4.2.2 mycroft }
192 1.4.2.2 mycroft (void)fclose(fp);
193 1.4.2.2 mycroft }
194 1.4.2.2 mycroft
195 1.4.2.2 mycroft badread(nr, p)
196 1.4.2.2 mycroft int nr;
197 1.4.2.2 mycroft char *p;
198 1.4.2.2 mycroft {
199 1.4.2.2 mycroft if (nr < 0)
200 1.4.2.2 mycroft error(kfile);
201 1.4.2.2 mycroft badfmt(p);
202 1.4.2.2 mycroft }
203 1.4.2.2 mycroft
204 1.4.2.2 mycroft badfmt(p)
205 1.4.2.2 mycroft char *p;
206 1.4.2.2 mycroft {
207 1.4.2.2 mycroft (void)fprintf(stderr,
208 1.4.2.2 mycroft "kvm_mkdb: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
209 1.4.2.2 mycroft exit(1);
210 1.4.2.2 mycroft }
211