1 /* $NetBSD: nlist_ecoff.c,v 1.17 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_ecoff.c,v 1.17 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_ecoff.h>
61 #define check(off, size) ((off < 0) || (off + size > mappedsize))
62 #define BAD do { rv = -1; goto out; } while (/*CONSTCOND*/0)
63 #define BADUNMAP do { rv = -1; goto unmap; } while (/*CONSTCOND*/0)
66 __fdnlist_ecoff(fd
, list
)
71 struct ecoff_exechdr
*exechdrp
;
72 struct ecoff_symhdr
*symhdrp
;
73 struct ecoff_extsym
*esyms
;
77 u_long symhdroff
, extstroff
;
82 _DIAGASSERT(fd
!= -1);
83 _DIAGASSERT(list
!= NULL
);
88 * If we can't fstat() the file, something bad is going on.
90 if (fstat(fd
, &st
) < 0)
94 * Map the file in its entirety.
96 if ((uintmax_t)st
.st_size
> (uintmax_t)SIZE_T_MAX
) {
100 mappedsize
= st
.st_size
;
101 mappedfile
= mmap(NULL
, mappedsize
, PROT_READ
, MAP_PRIVATE
|MAP_FILE
,
103 if (mappedfile
== (char *)-1)
107 * Make sure we can access the executable's header
108 * directly, and make sure the recognize the executable
109 * as an ECOFF binary.
111 if (check(0, sizeof *exechdrp
))
113 exechdrp
= (struct ecoff_exechdr
*)&mappedfile
[0];
115 if (ECOFF_BADMAG(exechdrp
))
119 * Find the symbol list.
121 symhdroff
= exechdrp
->f
.f_symptr
;
122 symhdrsize
= exechdrp
->f
.f_nsyms
;
124 if ((symhdroff
+ sizeof *symhdrp
) > mappedsize
||
125 sizeof *symhdrp
!= symhdrsize
)
127 symhdrp
= (struct ecoff_symhdr
*)&mappedfile
[symhdroff
];
129 nesyms
= symhdrp
->esymMax
;
130 if (check(symhdrp
->cbExtOffset
, nesyms
* sizeof *esyms
))
132 esyms
= (struct ecoff_extsym
*)&mappedfile
[symhdrp
->cbExtOffset
];
133 extstroff
= symhdrp
->cbSsExtOffset
;
136 * Clean out any left-over information for all valid entries.
137 * Type and value are defined to be 0 if not found; historical
138 * versions cleared other and desc as well.
140 * XXX Clearing anything other than n_type and n_value violates
141 * the semantics given in the man page.
144 for (p
= list
; !ISLAST(p
); ++p
) {
152 for (i
= 0; i
< nesyms
; i
++) {
153 for (p
= list
; !ISLAST(p
); p
++) {
154 const char *nlistname
;
157 /* This may be incorrect */
158 nlistname
= N_NAME(p
);
159 if (*nlistname
== '_')
163 &mappedfile
[extstroff
+ esyms
[i
].es_strindex
];
165 if (!strcmp(symtabname
, nlistname
)) {
167 * Translate (roughly) from ECOFF to nlist
169 p
->n_value
= esyms
[i
].es_value
;
170 p
->n_type
= N_EXT
; /* XXX */
171 p
->n_desc
= 0; /* XXX */
172 p
->n_other
= 0; /* XXX */
176 break; /* into next run of outer loop */
184 munmap(mappedfile
, mappedsize
);
189 #endif /* NLIST_ECOFF */