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