xref: /netbsd-src/lib/libc/gen/nlist_elf32.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /* $NetBSD: nlist_elf32.c,v 1.24 2003/07/26 19:24:43 salo 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 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
38 #ifndef ELFSIZE
39 #define	ELFSIZE		32
40 #endif
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 #include <sys/ioctl.h>
48 #include <sys/ksyms.h>
49 
50 #include <assert.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <a.out.h>			/* for 'struct nlist' declaration */
56 
57 #include "nlist_private.h"
58 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
59 #include <sys/exec_elf.h>
60 #endif
61 
62 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
63     (defined(NLIST_ELF64) && (ELFSIZE == 64))
64 
65 /* No need to check for off < 0 because it is unsigned */
66 #define	check(off, size)	(off + size > mappedsize)
67 #define	BAD			goto out
68 #define	BADUNMAP		goto unmap
69 
70 int
71 ELFNAMEEND(__fdnlist)(fd, list)
72 	int fd;
73 	struct nlist *list;
74 {
75 	struct stat st;
76 	struct nlist *p;
77 	char *mappedfile, *strtab;
78 	size_t mappedsize;
79 	Elf_Ehdr *ehdrp, ehdr;
80 	Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
81 	Elf_Sym *symp;
82 	Elf_Off shdr_off;
83 	Elf_Word shdr_size;
84 #if (ELFSIZE == 32)
85 	Elf32_Half nshdr;
86 #elif (ELFSIZE == 64)
87 	Elf64_Half nshdr;
88 #endif
89 	size_t i, nsyms;
90 	int rv, nent;
91 
92 	_DIAGASSERT(fd != -1);
93 	_DIAGASSERT(list != NULL);
94 
95 	rv = -1;
96 
97 	symshdrp = symstrshdrp = NULL;
98 
99 	/*
100 	 * If we can't fstat() the file, something bad is going on.
101 	 */
102 	if (fstat(fd, &st) < 0)
103 		BAD;
104 
105 	/*
106 	 * Map the file in its entirety.
107 	 */
108 	if (st.st_size > SIZE_T_MAX) {
109 		errno = EFBIG;
110 		BAD;
111 	}
112 
113 	/*
114 	 * Read the elf header of the file.
115 	 */
116 	if ((ssize_t)(i = pread(fd, &ehdr, sizeof(Elf_Ehdr), (off_t)0)) == -1)
117 		BAD;
118 
119 	/*
120 	 * Check that the elf header is correct.
121 	 */
122 	if (i != sizeof(Elf_Ehdr))
123 		BAD;
124 	if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
125 	    ehdr.e_ident[EI_CLASS] != ELFCLASS)
126 		BAD;
127 
128 	switch (ehdr.e_machine) {
129 	ELFDEFNNAME(MACHDEP_ID_CASES)
130 
131 	default:
132 		BADUNMAP;
133 	}
134 
135 	if (S_ISCHR(st.st_mode)) {
136 		const char *nlistname;
137 		struct ksyms_gsymbol kg;
138 		Elf_Sym sym;
139 
140 		/*
141 		 * Character device; assume /dev/ksyms.
142 		 */
143 		nent = 0;
144 		for (p = list; !ISLAST(p); ++p) {
145 
146 			p->n_other = 0;
147 			p->n_desc = 0;
148 			nlistname = p->n_un.n_name;
149 			if (*nlistname == '_')
150 				nlistname++;
151 
152 			kg.kg_name = nlistname;
153 			kg.kg_sym = &sym;
154 			if (ioctl(fd, KIOCGSYMBOL, &kg) == 0) {
155 				p->n_value = sym.st_value;
156 				switch (ELFDEFNNAME(ST_TYPE)(sym.st_info)) {
157 				case STT_NOTYPE:
158 					p->n_type = N_UNDF;
159 					break;
160 				case STT_OBJECT:
161 					p->n_type = N_DATA;
162 					break;
163 				case STT_FUNC:
164 					p->n_type = N_TEXT;
165 					break;
166 				case STT_FILE:
167 					p->n_type = N_FN;
168 					break;
169 				default:
170 					p->n_type = 0;
171 					/* catch other enumerations for gcc */
172 					break;
173 				}
174 				if (ELFDEFNNAME(ST_BIND)(sym.st_info) !=
175 				    STB_LOCAL)
176 					p->n_type |= N_EXT;
177 			} else {
178 				nent++;
179 				p->n_value = 0;
180 				p->n_type = 0;
181 			}
182 		}
183 		return nent;
184 	}
185 
186 	mappedsize = (size_t)st.st_size;
187 	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
188 	    fd, (off_t)0);
189 	if (mappedfile == (char *)-1)
190 		BAD;
191 
192 	/*
193 	 * Make sure we can access the executable's header
194 	 * directly, and make sure the recognize the executable
195 	 * as an ELF binary.
196 	 */
197 	if (check(0, sizeof *ehdrp))
198 		BADUNMAP;
199 	ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0];
200 
201 	/*
202 	 * Find the symbol list and string table.
203 	 */
204 	nshdr = ehdrp->e_shnum;
205 	shdr_off = ehdrp->e_shoff;
206 	shdr_size = ehdrp->e_shentsize * nshdr;
207 
208 	if (check(shdr_off, shdr_size) ||
209 	    (sizeof *shdrp != ehdrp->e_shentsize))
210 		BADUNMAP;
211 	shdrp = (Elf_Shdr *)(void *)&mappedfile[shdr_off];
212 
213 	for (i = 0; i < nshdr; i++) {
214 		if (shdrp[i].sh_type == SHT_SYMTAB) {
215 			symshdrp = &shdrp[i];
216 			symstrshdrp = &shdrp[shdrp[i].sh_link];
217 		}
218 	}
219 
220 	/* Make sure we're not stripped. */
221 	if (symshdrp == NULL || symshdrp->sh_offset == 0)
222 		BADUNMAP;
223 
224 	/* Make sure the symbols and strings are safely mapped. */
225 	if (check(symshdrp->sh_offset, symshdrp->sh_size))
226 		BADUNMAP;
227 	if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
228 		BADUNMAP;
229 
230 	symp = (Elf_Sym *)(void *)&mappedfile[symshdrp->sh_offset];
231 	nsyms = symshdrp->sh_size / sizeof(*symp);
232 	strtab = &mappedfile[symstrshdrp->sh_offset];
233 
234 	/*
235 	 * Clean out any left-over information for all valid entries.
236 	 * Type and value are defined to be 0 if not found; historical
237 	 * versions cleared other and desc as well.
238 	 *
239 	 * XXX Clearing anything other than n_type and n_value violates
240 	 * the semantics given in the man page.
241 	 */
242 	nent = 0;
243 	for (p = list; !ISLAST(p); ++p) {
244 		p->n_type = 0;
245 		p->n_other = 0;
246 		p->n_desc = 0;
247 		p->n_value = 0;
248 		++nent;
249 	}
250 
251 	for (i = 0; i < nsyms; i++) {
252 		for (p = list; !ISLAST(p); ++p) {
253 			const char *nlistname;
254 			char *symtabname;
255 
256 			/* This may be incorrect */
257 			nlistname = p->n_un.n_name;
258 			if (*nlistname == '_')
259 				nlistname++;
260 
261 			symtabname = &strtab[symp[i].st_name];
262 
263 			if (!strcmp(symtabname, nlistname)) {
264 				/*
265 				 * Translate (roughly) from ELF to nlist
266 				 */
267 				p->n_value = symp[i].st_value;
268 				switch (ELFDEFNNAME(ST_TYPE)(symp[i].st_info)) {
269 				case STT_NOTYPE:
270 					p->n_type = N_UNDF;
271 					break;
272 				case STT_OBJECT:
273 					p->n_type = N_DATA;
274 					break;
275 				case STT_FUNC:
276 					p->n_type = N_TEXT;
277 					break;
278 				case STT_FILE:
279 					p->n_type = N_FN;
280 					break;
281 				default:
282 					/* catch other enumerations for gcc */
283 					break;
284 				}
285 				if (ELFDEFNNAME(ST_BIND)(symp[i].st_info) !=
286 				    STB_LOCAL)
287 					p->n_type |= N_EXT;
288 				p->n_desc = 0;			/* XXX */
289 				p->n_other = 0;			/* XXX */
290 
291 				if (--nent <= 0)
292 					goto done;
293 				break;	/* into next run of outer loop */
294 			}
295 		}
296 	}
297 
298 done:
299 	rv = nent;
300 unmap:
301 	munmap(mappedfile, mappedsize);
302 out:
303 	return (rv);
304 }
305 
306 #endif
307