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