1 /* $NetBSD: nlist_ecoff.c,v 1.23 2012/03/20 00:31:24 matt 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_ecoff.c,v 1.23 2012/03/20 00:31:24 matt 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_ecoff.h>
61 #define check(off, size) \
62 ((size_t)off >= mappedsize || (size_t)(off + size) > mappedsize)
65 __fdnlist_ecoff(int fd
, struct nlist
*list
)
68 const struct ecoff_exechdr
*exechdrp
;
69 const struct ecoff_symhdr
*symhdrp
;
70 const struct ecoff_extsym
*esyms
;
72 const char *mappedfile
;
74 u_long symhdroff
, extstroff
;
79 _DIAGASSERT(fd
!= -1);
80 _DIAGASSERT(list
!= NULL
);
85 * If we can't fstat() the file, something bad is going on.
87 if (fstat(fd
, &st
) < 0)
91 * Map the file in its entirety.
93 if ((uintmax_t)st
.st_size
> (uintmax_t)SIZE_T_MAX
) {
97 mappedsize
= (size_t)st
.st_size
;
98 mappedfile
= mmap(NULL
, mappedsize
, PROT_READ
, MAP_PRIVATE
|MAP_FILE
,
100 if (mappedfile
== MAP_FAILED
)
104 * Make sure we can access the executable's header
105 * directly, and make sure the recognize the executable
106 * as an ECOFF binary.
108 if (check(0, sizeof *exechdrp
))
110 exechdrp
= (const void *)mappedfile
;
112 if (ECOFF_BADMAG(exechdrp
))
116 * Find the symbol list.
118 symhdroff
= exechdrp
->f
.f_symptr
;
119 symhdrsize
= exechdrp
->f
.f_nsyms
;
121 if ((symhdroff
+ sizeof *symhdrp
) > mappedsize
||
122 sizeof *symhdrp
!= symhdrsize
)
124 symhdrp
= (const void *)&mappedfile
[symhdroff
];
126 nesyms
= symhdrp
->esymMax
;
127 if (check(symhdrp
->cbExtOffset
, nesyms
* sizeof *esyms
))
129 esyms
= (const void *)&mappedfile
[symhdrp
->cbExtOffset
];
130 extstroff
= symhdrp
->cbSsExtOffset
;
133 * Clean out any left-over information for all valid entries.
134 * Type and value are defined to be 0 if not found; historical
135 * versions cleared other and desc as well.
137 * XXX Clearing anything other than n_type and n_value violates
138 * the semantics given in the man page.
141 for (p
= list
; !ISLAST(p
); ++p
) {
149 for (i
= 0; i
< nesyms
; i
++) {
150 for (p
= list
; !ISLAST(p
); p
++) {
151 const char *nlistname
;
152 const char *symtabname
;
154 /* This may be incorrect */
155 nlistname
= N_NAME(p
);
156 if (*nlistname
== '_')
160 &mappedfile
[extstroff
+ esyms
[i
].es_strindex
];
162 if (!strcmp(symtabname
, nlistname
)) {
164 * Translate (roughly) from ECOFF to nlist
166 p
->n_value
= esyms
[i
].es_value
;
167 p
->n_type
= N_EXT
; /* XXX */
168 p
->n_desc
= 0; /* XXX */
169 p
->n_other
= 0; /* XXX */
173 break; /* into next run of outer loop */
181 munmap(__UNCONST(mappedfile
), mappedsize
);
186 #endif /* NLIST_ECOFF */