1 /* $NetBSD: nlist_ecoff.c,v 1.10 2003/09/19 06:24:04 itojun Exp $ */
4 * Copyright (c) 1996 Christopher G. Demetriou
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the
18 * NetBSD Project. See http://www.NetBSD.org/ for
19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
37 #include <sys/cdefs.h>
39 __RCSID("$NetBSD: nlist_ecoff.c,v 1.10 2003/09/19 06:24:04 itojun Exp $");
42 #include <sys/param.h>
61 #include <sys/exec_ecoff.h>
63 typedef struct nlist NLIST
;
64 #define _strx n_un.n_strx
65 #define _name n_un.n_name
69 warnx("%s: %s: %s", kfile, str, strerror(EFTYPE)); \
73 #define check(off, size) ((off < 0) || (off + size > mappedsize))
74 #define BAD do { rv = -1; goto out; } while (0)
75 #define BADUNMAP do { rv = -1; goto unmap; } while (0)
77 static const char *kfile
;
80 create_knlist_ecoff(name
, db
)
84 struct ecoff_exechdr
*exechdrp
;
85 struct ecoff_symhdr
*symhdrp
;
86 struct ecoff_extsym
*esyms
;
90 char *mappedfile
, *symname
, *nsymname
, *fsymname
, *tmpcp
;
91 size_t mappedsize
, symnamesize
, fsymnamesize
;
92 u_long symhdroff
, extrstroff
;
93 u_long symhdrsize
, i
, nesyms
;
99 * Open and map the whole file. If we can't open/stat it,
100 * something bad is going on so we punt.
103 if ((fd
= open(name
, O_RDONLY
, 0)) < 0) {
107 if (fstat(fd
, &st
) < 0) {
111 if (st
.st_size
> SIZE_T_MAX
)
115 * Map the file in its entirety.
117 mappedsize
= st
.st_size
;
118 mappedfile
= mmap(NULL
, mappedsize
, PROT_READ
, MAP_FILE
|MAP_PRIVATE
,
120 if (mappedfile
== (char *)-1)
124 * Make sure we can access the executable's header
125 * directly, and make sure the recognize the executable
126 * as an ECOFF binary.
128 if (check(0, sizeof *exechdrp
))
130 exechdrp
= (struct ecoff_exechdr
*)&mappedfile
[0];
132 if (ECOFF_BADMAG(exechdrp
))
136 * We've recognized it as an ECOFF binary. From here
137 * on out, all errors are fatal.
141 * Find the symbol list and string table.
143 symhdroff
= exechdrp
->f
.f_symptr
;
144 symhdrsize
= exechdrp
->f
.f_nsyms
;
148 if (check(symhdroff
, sizeof *symhdrp
) || sizeof *symhdrp
!= symhdrsize
)
149 badfmt("bogus symbol table");
150 symhdrp
= (struct ecoff_symhdr
*)&mappedfile
[symhdroff
];
152 nesyms
= symhdrp
->esymMax
;
153 if (check(symhdrp
->cbExtOffset
, nesyms
* sizeof *esyms
))
154 badfmt("bogus external symbol list");
155 esyms
= (struct ecoff_extsym
*)&mappedfile
[symhdrp
->cbExtOffset
];
156 extrstroff
= symhdrp
->cbSsExtOffset
;
159 * Set up the data item, pointing to a nlist structure.
160 * which we fill in for each symbol.
162 data
.data
= (u_char
*)&nbuf
;
163 data
.size
= sizeof(nbuf
);
166 * Create a buffer (to be expanded later, if necessary)
167 * to hold symbol names after we've added underscores
171 if ((symname
= malloc(symnamesize
)) == NULL
) {
177 * Read each symbol and enter it into the database.
179 for (i
= 0; i
< nesyms
; i
++) {
182 * Find symbol name, copy it (with added underscore) to
183 * temporary buffer, and prepare the database key for
186 fsymname
= &mappedfile
[extrstroff
+ esyms
[i
].es_strindex
];
187 fsymnamesize
= strlen(fsymname
) + 1;
188 while (symnamesize
< fsymnamesize
+ 1) {
189 if ((nsymname
= realloc(symname
, symnamesize
* 2)) == NULL
) {
196 strlcpy(symname
, "_", symnamesize
);
197 strlcat(symname
, fsymname
, symnamesize
);
200 key
.size
= strlen((char *)key
.data
);
203 * Convert the symbol information into an nlist structure,
206 nbuf
.n_value
= esyms
[i
].es_value
;
207 nbuf
.n_type
= N_EXT
; /* XXX */
208 nbuf
.n_desc
= 0; /* XXX */
209 nbuf
.n_other
= 0; /* XXX */
212 * Enter the symbol into the database.
214 if (db
->put(db
, &key
, &data
, 0)) {
215 warn("record enter");
220 * If it's the kernel version string, we've gotta keep
221 * some extra data around. Under a separate key,
222 * we enter the first line (i.e. up to the first newline,
223 * with the newline replaced by a NUL to terminate the
224 * entered string) of the version string.
226 if (strcmp((char *)key
.data
, VRS_SYM
) == 0) {
229 key
.data
= (u_char
*)VRS_KEY
;
230 key
.size
= sizeof(VRS_KEY
) - 1;
232 /* Find the version string, relative to start */
234 if (exechdrp
->a
.text_start
<= vma
&&
235 vma
< (exechdrp
->a
.text_start
+ exechdrp
->a
.tsize
))
236 vma
= vma
- exechdrp
->a
.text_start
+
237 ECOFF_TXTOFF(exechdrp
);
238 else if (exechdrp
->a
.data_start
<= vma
&&
239 vma
< (exechdrp
->a
.data_start
+ exechdrp
->a
.dsize
))
240 vma
= vma
- exechdrp
->a
.data_start
+
241 ECOFF_DATOFF(exechdrp
);
243 warn("version string neither text nor data");
246 data
.data
= strdup(&mappedfile
[vma
]);
248 /* assumes newline terminates version. */
249 if ((tmpcp
= strchr(data
.data
, '\n')) != NULL
)
251 data
.size
= strlen((char *)data
.data
);
253 if (db
->put(db
, &key
, &data
, 0)) {
254 warn("record enter");
258 /* free pointer created by strdup(). */
261 /* Restore to original values */
262 data
.data
= (u_char
*)&nbuf
;
263 data
.size
= sizeof(nbuf
);
270 munmap(mappedfile
, mappedsize
);
275 #endif /* NLIST_ECOFF */