nlist_ecoff.c revision 1.11 1 /* $NetBSD: nlist_ecoff.c,v 1.11 2000/06/09 16:22:55 simonb Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 __RCSID("$NetBSD: nlist_ecoff.c,v 1.11 2000/06/09 16:22:55 simonb Exp $");
36 #endif /* LIBC_SCCS and not lint */
37
38 #include "namespace.h"
39 #include <sys/param.h>
40 #include <sys/mman.h>
41 #include <sys/stat.h>
42 #include <sys/file.h>
43
44 #include <assert.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <a.out.h> /* for 'struct nlist' declaration */
50
51 #include "nlist_private.h"
52 #ifdef NLIST_ECOFF
53 #include <sys/exec_ecoff.h>
54 #endif
55
56 #ifdef NLIST_ECOFF
57 #define check(off, size) ((off < 0) || (off + size > mappedsize))
58 #define BAD do { rv = -1; goto out; } while (/*CONSTCOND*/0)
59 #define BADUNMAP do { rv = -1; goto unmap; } while (/*CONSTCOND*/0)
60
61 int
62 __fdnlist_ecoff(fd, list)
63 int fd;
64 struct nlist *list;
65 {
66 struct nlist *p;
67 struct ecoff_exechdr *exechdrp;
68 struct ecoff_symhdr *symhdrp;
69 struct ecoff_extsym *esyms;
70 struct stat st;
71 char *mappedfile;
72 size_t mappedsize;
73 u_long symhdroff, extstroff;
74 u_int symhdrsize;
75 int rv, nent;
76 long i, nesyms;
77
78 _DIAGASSERT(fd != -1);
79 _DIAGASSERT(list != NULL);
80
81 rv = -1;
82
83 /*
84 * If we can't fstat() the file, something bad is going on.
85 */
86 if (fstat(fd, &st) < 0)
87 BAD;
88
89 /*
90 * Map the file in its entirety.
91 */
92 if (st.st_size > SIZE_T_MAX) {
93 errno = EFBIG;
94 BAD;
95 }
96 mappedsize = st.st_size;
97 mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
98 fd, 0);
99 if (mappedfile == (char *)-1)
100 BAD;
101
102 /*
103 * Make sure we can access the executable's header
104 * directly, and make sure the recognize the executable
105 * as an ECOFF binary.
106 */
107 if (check(0, sizeof *exechdrp))
108 BADUNMAP;
109 exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
110
111 if (ECOFF_BADMAG(exechdrp))
112 BADUNMAP;
113
114 /*
115 * Find the symbol list.
116 */
117 symhdroff = exechdrp->f.f_symptr;
118 symhdrsize = exechdrp->f.f_nsyms;
119
120 if (check(symhdroff, sizeof *symhdrp) ||
121 sizeof *symhdrp != symhdrsize)
122 BADUNMAP;
123 symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
124
125 nesyms = symhdrp->esymMax;
126 if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
127 BADUNMAP;
128 esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
129 extstroff = symhdrp->cbSsExtOffset;
130
131 /*
132 * Clean out any left-over information for all valid entries.
133 * Type and value are defined to be 0 if not found; historical
134 * versions cleared other and desc as well.
135 *
136 * XXX Clearing anything other than n_type and n_value violates
137 * the semantics given in the man page.
138 */
139 nent = 0;
140 for (p = list; !ISLAST(p); ++p) {
141 p->n_type = 0;
142 p->n_other = 0;
143 p->n_desc = 0;
144 p->n_value = 0;
145 ++nent;
146 }
147
148 for (i = 0; i < nesyms; i++) {
149 for (p = list; !ISLAST(p); p++) {
150 char *nlistname;
151 char *symtabname;
152
153 /* This may be incorrect */
154 nlistname = p->n_un.n_name;
155 if (*nlistname == '_')
156 nlistname++;
157
158 symtabname =
159 &mappedfile[extstroff + esyms[i].es_strindex];
160
161 if (!strcmp(symtabname, nlistname)) {
162 /*
163 * Translate (roughly) from ECOFF to nlist
164 */
165 p->n_value = esyms[i].es_value;
166 p->n_type = N_EXT; /* XXX */
167 p->n_desc = 0; /* XXX */
168 p->n_other = 0; /* XXX */
169
170 if (--nent <= 0)
171 goto done;
172 break; /* into next run of outer loop */
173 }
174 }
175 }
176
177 done:
178 rv = nent;
179 unmap:
180 munmap(mappedfile, mappedsize);
181 out:
182 return (rv);
183 }
184
185 #endif /* NLIST_ECOFF */
186