nlist_ecoff.c revision 1.5 1 /* $NetBSD: nlist_ecoff.c,v 1.5 1997/12/15 05:43:52 mrg 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.5 1997/12/15 05:43:52 mrg Exp $");
36 #endif /* LIBC_SCCS and not lint */
37
38 #include <sys/param.h>
39 #include <sys/mman.h>
40 #include <sys/stat.h>
41 #include <sys/file.h>
42
43 #include <errno.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <a.out.h> /* for 'struct nlist' declaration */
48
49 #include "nlist_private.h"
50 #ifdef NLIST_ECOFF
51 #include <sys/exec_ecoff.h>
52 #endif
53
54 #ifdef NLIST_ECOFF
55 #define check(off, size) ((off < 0) || (off + size > mappedsize))
56 #define BAD do { rv = -1; goto out; } while (0)
57 #define BADUNMAP do { rv = -1; goto unmap; } while (0)
58
59 int
60 __fdnlist_ecoff(fd, list)
61 register int fd;
62 register struct nlist *list;
63 {
64 struct nlist *p;
65 struct ecoff_exechdr *exechdrp;
66 struct ecoff_symhdr *symhdrp;
67 struct ecoff_extsym *esyms;
68 struct stat st;
69 char *mappedfile;
70 size_t mappedsize;
71 u_long symhdroff, extstroff;
72 u_int symhdrsize;
73 int rv, nent;
74 long i, nesyms;
75
76 rv = -1;
77
78 /*
79 * If we can't fstat() the file, something bad is going on.
80 */
81 if (fstat(fd, &st) < 0)
82 BAD;
83
84 /*
85 * Map the file in its entirety.
86 */
87 if (st.st_size > SIZE_T_MAX) {
88 errno = EFBIG;
89 BAD;
90 }
91 mappedsize = st.st_size;
92 mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_COPY|MAP_FILE,
93 fd, 0);
94 if (mappedfile == (char *)-1)
95 BAD;
96
97 /*
98 * Make sure we can access the executable's header
99 * directly, and make sure the recognize the executable
100 * as an ECOFF binary.
101 */
102 if (check(0, sizeof *exechdrp))
103 BADUNMAP;
104 exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
105
106 if (ECOFF_BADMAG(exechdrp))
107 BADUNMAP;
108
109 /*
110 * Find the symbol list.
111 */
112 symhdroff = exechdrp->f.f_symptr;
113 symhdrsize = exechdrp->f.f_nsyms;
114
115 if (check(symhdroff, sizeof *symhdrp) ||
116 sizeof *symhdrp != symhdrsize)
117 BADUNMAP;
118 symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
119
120 nesyms = symhdrp->esymMax;
121 if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
122 BADUNMAP;
123 esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
124 extstroff = symhdrp->cbSsExtOffset;
125
126 /*
127 * Clean out any left-over information for all valid entries.
128 * Type and value are defined to be 0 if not found; historical
129 * versions cleared other and desc as well.
130 *
131 * XXX Clearing anything other than n_type and n_value violates
132 * the semantics given in the man page.
133 */
134 nent = 0;
135 for (p = list; !ISLAST(p); ++p) {
136 p->n_type = 0;
137 p->n_other = 0;
138 p->n_desc = 0;
139 p->n_value = 0;
140 ++nent;
141 }
142
143 for (i = 0; i < nesyms; i++) {
144 for (p = list; !ISLAST(p); p++) {
145 char *nlistname;
146 char *symtabname;
147
148 /* This may be incorrect */
149 nlistname = p->n_un.n_name;
150 if (*nlistname == '_')
151 nlistname++;
152
153 symtabname =
154 &mappedfile[extstroff + esyms[i].es_strindex];
155
156 if (!strcmp(symtabname, nlistname)) {
157 /*
158 * Translate (roughly) from ECOFF to nlist
159 */
160 p->n_value = esyms[i].es_value;
161 p->n_type = N_EXT; /* XXX */
162 p->n_desc = 0; /* XXX */
163 p->n_other = 0; /* XXX */
164
165 if (--nent <= 0)
166 goto done;
167 break; /* into next run of outer loop */
168 }
169 }
170 }
171
172 done:
173 rv = nent;
174 unmap:
175 munmap(mappedfile, mappedsize);
176 out:
177 return (rv);
178 }
179
180 #endif /* NLIST_ECOFF */
181