1 /* $NetBSD: nlist_coff.c,v 1.7 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_coff.c,v 1.7 2003/09/19 06:24:04 itojun Exp $");
42 #include <sys/param.h>
61 #include <sys/exec_coff.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_coff(name
, db
)
84 struct coff_filehdr
*filehdrp
;
85 struct coff_aouthdr
*aouthdrp
;
89 char *mappedfile
, *symname
, *nsymname
, *fsymname
;
90 size_t mappedsize
, symnamesize
, fsymnamesize
;
91 u_long symhdroff
, extrstroff
;
92 u_long symhdrsize
, i
, nesyms
;
94 struct external_syment
*syment
;
102 * Open and map the whole file. If we can't open/stat it,
103 * something bad is going on so we punt.
106 if ((fd
= open(name
, O_RDONLY
, 0)) < 0) {
110 if (fstat(fd
, &st
) < 0) {
114 if (st
.st_size
> SIZE_T_MAX
)
118 * Map the file in its entirety.
120 mappedsize
= st
.st_size
;
121 mappedfile
= mmap(NULL
, mappedsize
, PROT_READ
, MAP_FILE
|MAP_PRIVATE
,
123 if (mappedfile
== (char *)-1)
127 * Make sure we can access the executable's header
128 * directly, and make sure the recognize the executable
131 if (check(0, sizeof *filehdrp
))
133 filehdrp
= (struct coff_filehdr
*)&mappedfile
[0];
135 if (COFF_BADMAG(filehdrp
))
139 * We've recognized it as an COFF binary. From here
140 * on out, all errors are fatal.
143 aouthdrp
= (struct coff_aouthdr
*)
144 &mappedfile
[sizeof(struct coff_filehdr
)];
147 * Find the symbol list and string table.
149 symhdroff
= filehdrp
->f_symptr
;
150 symhdrsize
= filehdrp
->f_nsyms
;
151 extrstroff
= symhdroff
+ symhdrsize
*COFF_ES_SYMENTSZ
;
154 printf("sizeof syment = %d\n",sizeof(struct external_syment
));
155 printf("symhdroff = 0x%lx,symhdrsize=%ld,stroff = 0x%lx",
156 symhdroff
,symhdrsize
, extrstroff
);
163 * Set up the data item, pointing to a nlist structure.
164 * which we fill in for each symbol.
166 data
.data
= (u_char
*)&nbuf
;
167 data
.size
= sizeof(nbuf
);
170 * Create a buffer (to be expanded later, if necessary)
171 * to hold symbol names after we've added underscores
175 if ((symname
= malloc(symnamesize
)) == NULL
) {
180 nesyms
= filehdrp
->f_nsyms
;
183 * Read each symbol and enter it into the database.
185 for (i
= 0; i
< nesyms
; i
++) {
188 * Find symbol name, copy it (with added underscore) to
189 * temporary buffer, and prepare the database key for
192 syment
= (struct external_syment
*)&mappedfile
[symhdroff
+
195 if(syment
->e_sclass
[0] != 2){
199 if(syment
->e
.e
.e_zeroes
[0]){
200 if( syment
->e
.e_name
[COFF_ES_SYMNMLEN
-1] ){
201 memcpy( snamebuf
, syment
->e
.e_name
,
203 snamebuf
[COFF_ES_SYMNMLEN
] = '\0';
207 fsymname
= syment
->e
.e_name
;
209 fsymnamesize
= strlen(fsymname
) + 1;
212 printf("%s\n",fsymname
);
216 memcpy(&soff
, syment
->e
.e
.e_offset
, sizeof(long));
217 fsymname
= &mappedfile
[extrstroff
+soff
];
218 fsymnamesize
= strlen(fsymname
) + 1;
221 printf("*%s\n",fsymname
);
225 while (symnamesize
< fsymnamesize
+ 1) {
226 if ((nsymname
= realloc(symname
, symnamesize
* 2)) == NULL
){
234 strlcpy(symname
, "_", symnamesize
);
235 strlcat(symname
, fsymname
, symnamesize
);
237 strlcpy(symname
, fsymname
, symnamesize
);
241 key
.size
= strlen((char *)key
.data
);
244 * Convert the symbol information into an nlist structure,
247 memcpy(&val
, syment
->e_value
, sizeof( long ));
249 nbuf
.n_type
= N_EXT
; /* XXX */
250 nbuf
.n_desc
= 0; /* XXX */
251 nbuf
.n_other
= 0; /* XXX */
254 * Enter the symbol into the database.
256 if (db
->put(db
, &key
, &data
, 0)) {
257 warn("record enter");
261 * If it's the kernel version string, we've gotta keep
262 * some extra data around. Under a separate key,
263 * we enter the first line (i.e. up to the first newline,
264 * with the newline replaced by a NUL to terminate the
265 * entered string) of the version string.
267 if (strcmp((char *)key
.data
, VRS_SYM
) == 0) {
270 struct coff_scnhdr
*sh
;
273 key
.data
= (u_char
*)VRS_KEY
;
274 key
.size
= sizeof(VRS_KEY
) - 1;
276 /* Find the version string, relative to start */
280 printf("vma = %lx,tstart = %lx, dstart=%lx\n",
281 vma
, aouthdrp
->a_tstart
,
283 printf("tsize = %lx, dsize=%lx\n",
287 if (aouthdrp
->a_tstart
<= vma
&&
288 vma
< (aouthdrp
->a_tstart
+ aouthdrp
->a_tsize
)){
289 for(i
=0;i
<filehdrp
->f_nscns
;i
++){
290 sh
= (struct coff_scnhdr
*)
291 &mappedfile
[COFF_HDR_SIZE
+
294 if( sh
->s_flags
== COFF_STYP_TEXT
){
298 vma
= vma
- sh
->s_vaddr
+ sh
->s_scnptr
;
300 else if (aouthdrp
->a_dstart
<= vma
&&
301 vma
< (aouthdrp
->a_dstart
+ aouthdrp
->a_dsize
)){
302 for(i
=0;i
<filehdrp
->f_nscns
;i
++){
303 sh
= (struct coff_scnhdr
*)
304 &mappedfile
[COFF_HDR_SIZE
+
307 if( sh
->s_flags
== COFF_STYP_DATA
){
311 vma
= vma
- sh
->s_vaddr
+ sh
->s_scnptr
;
314 warn("version string neither text nor data");
317 data
.data
= strdup(&mappedfile
[vma
]);
320 printf("vma = %lx,version = %s\n",
321 vma
, (char *)data
.data
);
324 /* assumes newline terminates version. */
325 if ((tmpcp
= strchr(data
.data
, '\n')) != NULL
)
327 data
.size
= strlen((char *)data
.data
);
329 if (db
->put(db
, &key
, &data
, 0)) {
330 warn("record enter");
334 /* free pointer created by strdup(). */
337 /* Restore to original values */
338 data
.data
= (u_char
*)&nbuf
;
339 data
.size
= sizeof(nbuf
);
346 munmap(mappedfile
, mappedsize
);
351 #endif /* NLIST_COFF */