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