xref: /netbsd-src/usr.sbin/kvm_mkdb/nlist.c (revision cda4f8f6ee55684e8d311b86c99ea59191e6b74f)
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
42 static 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 
57 typedef struct nlist NLIST;
58 #define	_strx	n_un.n_strx
59 #define	_name	n_un.n_name
60 
61 static char *kfile;
62 
63 create_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 			/*
150 			 * XXX: This is a KLUGE to handle the kernel being
151 			 * loaded at a different address than KERNBASE.  Stupid
152 			 * a.out format has no way of recording the text
153 			 * address we gave ld.  It only works for multiples of
154 			 * 1MB.
155 			 */
156 			rel_off = ((nbuf.n_value - (ebuf.a_entry & -0x100000))
157 				  + CLBYTES);
158 #endif
159 			/*
160 			 * When loaded, data is rounded to next page cluster
161 			 * after text, but not in file.
162 			 */
163 			rel_off -= CLBYTES - (ebuf.a_text % CLBYTES);
164 			vers_off = N_TXTOFF(ebuf) + rel_off;
165 
166 			cur_off = ftell(fp);
167 			if (fseek(fp, vers_off, SEEK_SET) == -1)
168 				badfmt("corrupted string table");
169 
170 			/*
171 			 * Read version string up to, and including newline.
172 			 * This code assumes that a newline terminates the
173 			 * version line.
174 			 */
175 			if (fgets(buf, sizeof(buf), fp) == NULL)
176 				badfmt("corrupted string table");
177 
178 			key.data = (u_char *)VRS_KEY;
179 			key.size = sizeof(VRS_KEY) - 1;
180 			data.data = (u_char *)buf;
181 			data.size = strlen(buf);
182 			if ((db->put)(db, &key, &data, 0))
183 				error("put");
184 
185 			/* Restore to original values. */
186 			data.data = (u_char *)&nbuf;
187 			data.size = sizeof(NLIST);
188 			if (fseek(fp, cur_off, SEEK_SET) == -1)
189 				badfmt("corrupted string table");
190 		}
191 	}
192 	(void)fclose(fp);
193 }
194 
195 badread(nr, p)
196 	int nr;
197 	char *p;
198 {
199 	if (nr < 0)
200 		error(kfile);
201 	badfmt(p);
202 }
203 
204 badfmt(p)
205 	char *p;
206 {
207 	(void)fprintf(stderr,
208 	    "kvm_mkdb: %s: %s: %s\n", kfile, p, strerror(EFTYPE));
209 	exit(1);
210 }
211