1 /* $NetBSD: nlist_coff.c,v 1.11 2012/03/22 14:18:34 christos 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>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 __RCSID("$NetBSD: nlist_coff.c,v 1.11 2012/03/22 14:18:34 christos Exp $");
40 #endif /* LIBC_SCCS and not lint */
42 #include "namespace.h"
43 #include <sys/param.h>
55 #include "nlist_private.h"
57 #include <sys/exec_coff.h>
77 #define es_name u.u_name
78 #define es_zero u.s.u_zero
79 #define es_offset u.s.u_offset
82 __fdnlist_coff(int fd
, struct nlist
*list
)
85 struct coff_filehdr
*filehdrp
;
89 u_long symoff
, extstroff
;
93 _DIAGASSERT(fd
!= -1);
94 _DIAGASSERT(list
!= NULL
);
99 * If we can't fstat() the file, something bad is going on.
101 if (fstat(fd
, &st
) < 0)
105 * Map the file in its entirety.
107 if ((uintmax_t)st
.st_size
> (uintmax_t)SIZE_T_MAX
) {
111 mappedsize
= (size_t)st
.st_size
;
112 mappedfile
= mmap(NULL
, mappedsize
, PROT_READ
, MAP_PRIVATE
|MAP_FILE
,
114 if (mappedfile
== MAP_FAILED
)
118 * Make sure we can access the executable's header
119 * directly, and make sure we recognize the executable
122 if (mappedsize
< sizeof (struct coff_filehdr
))
124 filehdrp
= (void *)&mappedfile
[0];
126 if (COFF_BADMAG(filehdrp
))
130 * Find the symbol list.
132 symoff
= filehdrp
->f_symptr
;
133 nesyms
= filehdrp
->f_nsyms
;
135 if (symoff
+ ES_LEN
* nesyms
> mappedsize
)
137 extstroff
= symoff
+ ES_LEN
* nesyms
;
140 for (p
= list
; !ISLAST(p
); ++p
) {
148 for (i
= 0; i
< nesyms
; i
++) {
150 const char *nlistname
;
151 struct coff_extsym esym
;
154 memcpy(&esym
, &mappedfile
[symoff
+ ES_LEN
* i
], ES_LEN
);
155 if (esym
.es_numaux
!= 0) {
156 i
+= esym
.es_numaux
; /* XXX Skip aux entry */
160 if (esym
.es_zero
!= 0) {
161 memcpy(name
, esym
.es_name
, 8);
164 } else if (esym
.es_offset
!= 0)
165 symtabname
= &mappedfile
[extstroff
+ esym
.es_offset
];
169 for (p
= list
; !ISLAST(p
); p
++) {
170 nlistname
= N_NAME(p
);
171 if (!strcmp(symtabname
, nlistname
)) {
173 * Translate (roughly) from COFF to nlist
175 p
->n_value
= esym
.es_value
;
176 p
->n_type
= N_EXT
; /* XXX */
177 p
->n_desc
= 0; /* XXX */
178 p
->n_other
= 0; /* XXX */
182 break; /* into next run of outer loop */
190 munmap(mappedfile
, mappedsize
);
195 #endif /* NLIST_COFF */