nlist.c revision 1.11 1 1.1 cgd /*-
2 1.9 pk * Copyright (c) 1990, 1993
3 1.9 pk * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.10 mycroft /*static char sccsid[] = "from: @(#)nlist.c 8.1 (Berkeley) 6/6/93";*/
36 1.11 gwr static char *rcsid = "$Id: nlist.c,v 1.11 1996/05/16 21:17:21 gwr Exp $";
37 1.1 cgd #endif /* not lint */
38 1.1 cgd
39 1.1 cgd #include <sys/param.h>
40 1.9 pk
41 1.1 cgd #include <a.out.h>
42 1.1 cgd #include <db.h>
43 1.9 pk #include <err.h>
44 1.1 cgd #include <errno.h>
45 1.9 pk #include <fcntl.h>
46 1.1 cgd #include <kvm.h>
47 1.9 pk #include <limits.h>
48 1.1 cgd #include <stdio.h>
49 1.9 pk #include <stdlib.h>
50 1.1 cgd #include <string.h>
51 1.9 pk #include <unistd.h>
52 1.9 pk
53 1.9 pk #include "extern.h"
54 1.1 cgd
55 1.1 cgd typedef struct nlist NLIST;
56 1.1 cgd #define _strx n_un.n_strx
57 1.1 cgd #define _name n_un.n_name
58 1.1 cgd
59 1.9 pk #define badfmt(str) errx(1, "%s: %s: %s", kfile, str, strerror(EFTYPE))
60 1.9 pk
61 1.9 pk static void badread __P((int, char *));
62 1.11 gwr static u_long get_kerntext __P((char *kfn));
63 1.9 pk
64 1.1 cgd static char *kfile;
65 1.1 cgd
66 1.7 pk void
67 1.1 cgd create_knlist(name, db)
68 1.1 cgd char *name;
69 1.1 cgd DB *db;
70 1.1 cgd {
71 1.1 cgd register int nsyms;
72 1.1 cgd struct exec ebuf;
73 1.1 cgd FILE *fp;
74 1.1 cgd NLIST nbuf;
75 1.1 cgd DBT data, key;
76 1.1 cgd int fd, nr, strsize;
77 1.11 gwr u_long kerntextoff;
78 1.1 cgd char *strtab, buf[1024];
79 1.1 cgd
80 1.1 cgd kfile = name;
81 1.1 cgd if ((fd = open(name, O_RDONLY, 0)) < 0)
82 1.9 pk err(1, "%s", name);
83 1.1 cgd
84 1.1 cgd /* Read in exec structure. */
85 1.9 pk nr = read(fd, &ebuf, sizeof(struct exec));
86 1.1 cgd if (nr != sizeof(struct exec))
87 1.9 pk badfmt("no exec header");
88 1.1 cgd
89 1.1 cgd /* Check magic number and symbol count. */
90 1.1 cgd if (N_BADMAG(ebuf))
91 1.1 cgd badfmt("bad magic number");
92 1.1 cgd if (!ebuf.a_syms)
93 1.1 cgd badfmt("stripped");
94 1.1 cgd
95 1.1 cgd /* Seek to string table. */
96 1.1 cgd if (lseek(fd, N_STROFF(ebuf), SEEK_SET) == -1)
97 1.1 cgd badfmt("corrupted string table");
98 1.1 cgd
99 1.1 cgd /* Read in the size of the symbol table. */
100 1.1 cgd nr = read(fd, (char *)&strsize, sizeof(strsize));
101 1.1 cgd if (nr != sizeof(strsize))
102 1.1 cgd badread(nr, "no symbol table");
103 1.1 cgd
104 1.1 cgd /* Read in the string table. */
105 1.1 cgd strsize -= sizeof(strsize);
106 1.9 pk if (!(strtab = malloc(strsize)))
107 1.9 pk err(1, NULL);
108 1.1 cgd if ((nr = read(fd, strtab, strsize)) != strsize)
109 1.1 cgd badread(nr, "corrupted symbol table");
110 1.1 cgd
111 1.1 cgd /* Seek to symbol table. */
112 1.1 cgd if (!(fp = fdopen(fd, "r")))
113 1.9 pk err(1, "%s", name);
114 1.1 cgd if (fseek(fp, N_SYMOFF(ebuf), SEEK_SET) == -1)
115 1.9 pk err(1, "%s", name);
116 1.1 cgd
117 1.1 cgd data.data = (u_char *)&nbuf;
118 1.1 cgd data.size = sizeof(NLIST);
119 1.1 cgd
120 1.11 gwr kerntextoff = get_kerntext(name);
121 1.11 gwr
122 1.1 cgd /* Read each symbol and enter it into the database. */
123 1.1 cgd nsyms = ebuf.a_syms / sizeof(struct nlist);
124 1.1 cgd while (nsyms--) {
125 1.1 cgd if (fread((char *)&nbuf, sizeof (NLIST), 1, fp) != 1) {
126 1.1 cgd if (feof(fp))
127 1.1 cgd badfmt("corrupted symbol table");
128 1.9 pk err(1, "%s", name);
129 1.1 cgd }
130 1.1 cgd if (!nbuf._strx || nbuf.n_type&N_STAB)
131 1.1 cgd continue;
132 1.1 cgd
133 1.1 cgd key.data = (u_char *)strtab + nbuf._strx - sizeof(long);
134 1.1 cgd key.size = strlen((char *)key.data);
135 1.9 pk if (db->put(db, &key, &data, 0))
136 1.9 pk err(1, "record enter");
137 1.1 cgd
138 1.9 pk if (strcmp((char *)key.data, VRS_SYM) == 0) {
139 1.9 pk long cur_off, voff;
140 1.4 mycroft /*
141 1.9 pk * Calculate offset relative to a normal (non-kernel)
142 1.9 pk * a.out. KERNTEXTOFF is where the kernel is really
143 1.9 pk * loaded; N_TXTADDR is where a normal file is loaded.
144 1.9 pk * From there, locate file offset in text or data.
145 1.4 mycroft */
146 1.11 gwr voff = nbuf.n_value - kerntextoff + N_TXTADDR(ebuf);
147 1.9 pk if ((nbuf.n_type & N_TYPE) == N_TEXT)
148 1.9 pk voff += N_TXTOFF(ebuf) - N_TXTADDR(ebuf);
149 1.9 pk else
150 1.9 pk voff += N_DATOFF(ebuf) - N_DATADDR(ebuf);
151 1.1 cgd cur_off = ftell(fp);
152 1.9 pk if (fseek(fp, voff, SEEK_SET) == -1)
153 1.1 cgd badfmt("corrupted string table");
154 1.1 cgd
155 1.1 cgd /*
156 1.1 cgd * Read version string up to, and including newline.
157 1.1 cgd * This code assumes that a newline terminates the
158 1.1 cgd * version line.
159 1.1 cgd */
160 1.1 cgd if (fgets(buf, sizeof(buf), fp) == NULL)
161 1.1 cgd badfmt("corrupted string table");
162 1.1 cgd
163 1.1 cgd key.data = (u_char *)VRS_KEY;
164 1.1 cgd key.size = sizeof(VRS_KEY) - 1;
165 1.1 cgd data.data = (u_char *)buf;
166 1.1 cgd data.size = strlen(buf);
167 1.9 pk if (db->put(db, &key, &data, 0))
168 1.9 pk err(1, "record enter");
169 1.1 cgd
170 1.1 cgd /* Restore to original values. */
171 1.1 cgd data.data = (u_char *)&nbuf;
172 1.1 cgd data.size = sizeof(NLIST);
173 1.1 cgd if (fseek(fp, cur_off, SEEK_SET) == -1)
174 1.1 cgd badfmt("corrupted string table");
175 1.1 cgd }
176 1.1 cgd }
177 1.1 cgd (void)fclose(fp);
178 1.1 cgd }
179 1.1 cgd
180 1.7 pk static void
181 1.1 cgd badread(nr, p)
182 1.1 cgd int nr;
183 1.1 cgd char *p;
184 1.1 cgd {
185 1.1 cgd if (nr < 0)
186 1.9 pk err(1, "%s", kfile);
187 1.1 cgd badfmt(p);
188 1.11 gwr }
189 1.11 gwr
190 1.11 gwr /*
191 1.11 gwr * XXX: Using this value from machine/param.h introduces a
192 1.11 gwr * XXX: machine dependency on this program, so /usr can not
193 1.11 gwr * XXX: be shared between (i.e.) several m68k machines.
194 1.11 gwr * Instead of compiling in KERNTEXTOFF or KERNBASE, try to
195 1.11 gwr * determine the text start address from a standard symbol.
196 1.11 gwr * For backward compatibility, use the old compiled-in way
197 1.11 gwr * when the standard symbol name is not found.
198 1.11 gwr */
199 1.11 gwr #ifndef KERNTEXTOFF
200 1.11 gwr #define KERNTEXTOFF KERNBASE
201 1.11 gwr #endif
202 1.11 gwr
203 1.11 gwr static u_long
204 1.11 gwr get_kerntext(name)
205 1.11 gwr char *name;
206 1.11 gwr {
207 1.11 gwr NLIST nl[2];
208 1.11 gwr
209 1.11 gwr bzero((caddr_t)nl, sizeof(nl));
210 1.11 gwr nl[0]._name = "_kernel_text";
211 1.11 gwr
212 1.11 gwr if (nlist(name, nl) != 0)
213 1.11 gwr return (KERNTEXTOFF);
214 1.11 gwr
215 1.11 gwr return (nl[0].n_value);
216 1.1 cgd }
217