1 /* $NetBSD: nlist_coff.c,v 1.7 2009/02/16 10:40:45 lukem 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.7 2009/02/16 10:40:45 lukem 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>
61 #define BAD do { rv = -1; goto out; } while (/*CONSTCOND*/0)
62 #define BADUNMAP do { rv = -1; goto unmap; } while (/*CONSTCOND*/0)
79 #define es_name u.u_name
80 #define es_zero u.s.u_zero
81 #define es_offset u.s.u_offset
84 __fdnlist_coff(fd
, list
)
89 struct coff_filehdr
*filehdrp
;
93 u_long symoff
, extstroff
;
97 _DIAGASSERT(fd
!= -1);
98 _DIAGASSERT(list
!= NULL
);
103 * If we can't fstat() the file, something bad is going on.
105 if (fstat(fd
, &st
) < 0)
109 * Map the file in its entirety.
111 if ((uintmax_t)st
.st_size
> (uintmax_t)SIZE_T_MAX
) {
115 mappedsize
= st
.st_size
;
116 mappedfile
= mmap(NULL
, mappedsize
, PROT_READ
, MAP_PRIVATE
|MAP_FILE
,
118 if (mappedfile
== (char *)-1)
122 * Make sure we can access the executable's header
123 * directly, and make sure we recognize the executable
126 if (mappedsize
< sizeof (struct coff_filehdr
))
128 filehdrp
= (struct coff_filehdr
*)&mappedfile
[0];
130 if (COFF_BADMAG(filehdrp
))
134 * Find the symbol list.
136 symoff
= filehdrp
->f_symptr
;
137 nesyms
= filehdrp
->f_nsyms
;
139 if (symoff
+ ES_LEN
* nesyms
> mappedsize
)
141 extstroff
= symoff
+ ES_LEN
* nesyms
;
144 for (p
= list
; !ISLAST(p
); ++p
) {
152 for (i
= 0; i
< nesyms
; i
++) {
154 const char *nlistname
;
155 struct coff_extsym esym
;
158 memcpy(&esym
, &mappedfile
[symoff
+ ES_LEN
* i
], ES_LEN
);
159 if (esym
.es_numaux
!= 0) {
160 i
+= esym
.es_numaux
; /* XXX Skip aux entry */
164 if (esym
.es_zero
!= 0) {
165 memcpy(name
, esym
.es_name
, 8);
168 } else if (esym
.es_offset
!= 0)
169 symtabname
= &mappedfile
[extstroff
+ esym
.es_offset
];
173 for (p
= list
; !ISLAST(p
); p
++) {
174 nlistname
= N_NAME(p
);
175 if (!strcmp(symtabname
, nlistname
)) {
177 * Translate (roughly) from COFF to nlist
179 p
->n_value
= esym
.es_value
;
180 p
->n_type
= N_EXT
; /* XXX */
181 p
->n_desc
= 0; /* XXX */
182 p
->n_other
= 0; /* XXX */
186 break; /* into next run of outer loop */
194 munmap(mappedfile
, mappedsize
);
199 #endif /* NLIST_COFF */