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