xref: /netbsd-src/lib/libc/gen/nlist_ecoff.c (revision 26cc2d4f50bf5015b51b947dfbb51b520da568e8)
1 /*	$NetBSD: nlist_ecoff.c,v 1.4 1997/07/13 19:46:07 christos 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.4 1997/07/13 19:46:07 christos 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, 0, fd, 0);
93 	if (mappedfile == (char *)-1)
94 		BAD;
95 
96 	/*
97 	 * Make sure we can access the executable's header
98 	 * directly, and make sure the recognize the executable
99 	 * as an ECOFF binary.
100 	 */
101 	if (check(0, sizeof *exechdrp))
102 		BADUNMAP;
103 	exechdrp = (struct ecoff_exechdr *)&mappedfile[0];
104 
105 	if (ECOFF_BADMAG(exechdrp))
106 		BADUNMAP;
107 
108 	/*
109 	 * Find the symbol list.
110 	 */
111 	symhdroff = exechdrp->f.f_symptr;
112 	symhdrsize = exechdrp->f.f_nsyms;
113 
114 	if (check(symhdroff, sizeof *symhdrp) ||
115 	    sizeof *symhdrp != symhdrsize)
116 		BADUNMAP;
117 	symhdrp = (struct ecoff_symhdr *)&mappedfile[symhdroff];
118 
119 	nesyms = symhdrp->esymMax;
120 	if (check(symhdrp->cbExtOffset, nesyms * sizeof *esyms))
121 		BADUNMAP;
122 	esyms = (struct ecoff_extsym *)&mappedfile[symhdrp->cbExtOffset];
123 	extstroff = symhdrp->cbSsExtOffset;
124 
125 	/*
126 	 * Clean out any left-over information for all valid entries.
127 	 * Type and value are defined to be 0 if not found; historical
128 	 * versions cleared other and desc as well.
129 	 *
130 	 * XXX Clearing anything other than n_type and n_value violates
131 	 * the semantics given in the man page.
132 	 */
133 	nent = 0;
134 	for (p = list; !ISLAST(p); ++p) {
135 		p->n_type = 0;
136 		p->n_other = 0;
137 		p->n_desc = 0;
138 		p->n_value = 0;
139 		++nent;
140 	}
141 
142 	for (i = 0; i < nesyms; i++) {
143 		for (p = list; !ISLAST(p); p++) {
144 			char *nlistname;
145 			char *symtabname;
146 
147 			/* This may be incorrect */
148 			nlistname = p->n_un.n_name;
149 			if (*nlistname == '_')
150 				nlistname++;
151 
152 			symtabname =
153 			    &mappedfile[extstroff + esyms[i].es_strindex];
154 
155 			if (!strcmp(symtabname, nlistname)) {
156 				/*
157 				 * Translate (roughly) from ECOFF to nlist
158 				 */
159 				p->n_value = esyms[i].es_value;
160 				p->n_type = N_EXT;		/* XXX */
161 				p->n_desc = 0;			/* XXX */
162 				p->n_other = 0;			/* XXX */
163 
164 				if (--nent <= 0)
165 					goto done;
166 				break;	/* into next run of outer loop */
167 			}
168 		}
169 	}
170 
171 done:
172 	rv = nent;
173 unmap:
174 	munmap(mappedfile, mappedsize);
175 out:
176 	return (rv);
177 }
178 
179 #endif /* NLIST_ECOFF */
180