1 /* $NetBSD: nlist_aout.c,v 1.7 2000/06/14 06:49:25 cgd Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64 #include <sys/cdefs.h>
67 static char sccsid
[] = "from: @(#)nlist.c 8.1 (Berkeley) 6/6/93";
69 __RCSID("$NetBSD: nlist_aout.c,v 1.7 2000/06/14 06:49:25 cgd Exp $");
73 #include <sys/param.h>
91 typedef struct nlist NLIST
;
92 #define _strx n_un.n_strx
93 #define _name n_un.n_name
97 warnx("%s: %s: %s", kfile, str, strerror(EFTYPE)); \
101 static void badread
__P((int, char *));
102 static u_long get_kerntext
__P((const char *kfn
));
104 static const char *kfile
;
107 create_knlist_aout(name
, db
)
118 char *strtab
, buf
[1024];
121 if ((fd
= open(name
, O_RDONLY
, 0)) < 0) {
126 /* Read in exec structure. */
127 nr
= read(fd
, &ebuf
, sizeof(struct exec
));
128 if (nr
!= sizeof(struct exec
))
131 /* Check magic number. */
136 * We've recognized it as an a.out binary. From here
137 * on out, all errors are fatal.
140 /* Check symbol count. */
144 /* Seek to string table. */
145 if (lseek(fd
, N_STROFF(ebuf
), SEEK_SET
) == -1)
146 badfmt("corrupted string table");
148 /* Read in the size of the symbol table. */
149 nr
= read(fd
, (char *)&strsize
, sizeof(strsize
));
150 if (nr
!= sizeof(strsize
))
151 badread(nr
, "no symbol table");
153 /* Read in the string table. */
154 strsize
-= sizeof(strsize
);
155 if (!(strtab
= malloc(strsize
))) {
159 if ((nr
= read(fd
, strtab
, strsize
)) != strsize
)
160 badread(nr
, "corrupted symbol table");
162 /* Seek to symbol table. */
163 if (!(fp
= fdopen(fd
, "r"))) {
167 if (fseek(fp
, N_SYMOFF(ebuf
), SEEK_SET
) == -1) {
172 data
.data
= (u_char
*)&nbuf
;
173 data
.size
= sizeof(NLIST
);
175 kerntextoff
= get_kerntext(name
);
177 /* Read each symbol and enter it into the database. */
178 nsyms
= ebuf
.a_syms
/ sizeof(struct nlist
);
180 if (fread((char *)&nbuf
, sizeof (NLIST
), 1, fp
) != 1) {
182 badfmt("corrupted symbol table");
186 if (!nbuf
._strx
|| nbuf
.n_type
&N_STAB
)
189 key
.data
= (u_char
*)strtab
+ nbuf
._strx
- sizeof(long);
190 key
.size
= strlen((char *)key
.data
);
191 if (db
->put(db
, &key
, &data
, 0)) {
192 warn("record enter");
196 if (strcmp((char *)key
.data
, VRS_SYM
) == 0) {
199 * Calculate offset relative to a normal (non-kernel)
200 * a.out. Kerntextoff is where the kernel is really
201 * loaded; N_TXTADDR is where a normal file is loaded.
202 * From there, locate file offset in text or data.
204 voff
= nbuf
.n_value
- kerntextoff
+ N_TXTADDR(ebuf
);
205 if ((nbuf
.n_type
& N_TYPE
) == N_TEXT
)
206 voff
+= N_TXTOFF(ebuf
) - N_TXTADDR(ebuf
);
208 voff
+= N_DATOFF(ebuf
) - N_DATADDR(ebuf
);
210 if (fseek(fp
, voff
, SEEK_SET
) == -1)
211 badfmt("corrupted string table");
214 * Read version string up to, and including newline.
215 * This code assumes that a newline terminates the
218 if (fgets(buf
, sizeof(buf
), fp
) == NULL
)
219 badfmt("corrupted string table");
221 key
.data
= (u_char
*)VRS_KEY
;
222 key
.size
= sizeof(VRS_KEY
) - 1;
223 data
.data
= (u_char
*)buf
;
224 data
.size
= strlen(buf
);
225 if (db
->put(db
, &key
, &data
, 0)) {
226 warn("record enter");
230 /* Restore to original values. */
231 data
.data
= (u_char
*)&nbuf
;
232 data
.size
= sizeof(NLIST
);
233 if (fseek(fp
, cur_off
, SEEK_SET
) == -1)
234 badfmt("corrupted string table");
255 * Instead of compiling in KERNTEXTOFF or KERNBASE, try to
256 * determine the text start address from a standard symbol.
257 * For backward compatibility, use the old compiled-in way
258 * when the standard symbol name is not found.
266 memset((caddr_t
)nl
, 0, sizeof(nl
));
267 nl
[0].n_un
.n_name
= "_kernel_text";
269 if (nlist(name
, nl
) != 0) {
270 warnx("%s: %s symbol missing",
271 name
, nl
[0].n_un
.n_name
);
275 return (nl
[0].n_value
);
277 #endif /* NLIST_AOUT */