xref: /netbsd-src/lib/libc/gen/nlist_elf32.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: nlist_elf32.c,v 1.19 1999/11/04 02:00:17 erh 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 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
34 #ifndef ELFSIZE
35 #define	ELFSIZE		32
36 #endif
37 
38 #include "namespace.h"
39 #include <sys/param.h>
40 #include <sys/mman.h>
41 #include <sys/stat.h>
42 #include <sys/file.h>
43 
44 #include <assert.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <a.out.h>			/* for 'struct nlist' declaration */
50 
51 #include "nlist_private.h"
52 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
53 #include <sys/exec_elf.h>
54 #endif
55 
56 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
57     (defined(NLIST_ELF64) && (ELFSIZE == 64))
58 
59 /* No need to check for off < 0 because it is unsigned */
60 #define	check(off, size)	(off + size > mappedsize)
61 #define	BAD			goto out
62 #define	BADUNMAP		goto unmap
63 
64 int
65 ELFNAMEEND(__fdnlist)(fd, list)
66 	int fd;
67 	struct nlist *list;
68 {
69 	struct stat st;
70 	struct nlist *p;
71 	char *mappedfile, *strtab;
72 	size_t mappedsize;
73 	Elf_Ehdr *ehdrp;
74 	Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
75 	Elf_Sym *symp;
76 	Elf_Off shdr_off;
77 	Elf_Word shdr_size;
78 #if (ELFSIZE == 32)
79 	Elf32_Half nshdr;
80 #elif (ELFSIZE == 64)
81 	Elf64_Half nshdr;
82 #endif
83 	size_t i, nsyms;
84 	int rv, nent;
85 
86 	_DIAGASSERT(fd != -1);
87 	_DIAGASSERT(list != NULL);
88 
89 	rv = -1;
90 
91 	symshdrp = symstrshdrp = NULL;
92 
93 	/*
94 	 * If we can't fstat() the file, something bad is going on.
95 	 */
96 	if (fstat(fd, &st) < 0)
97 		BAD;
98 
99 	/*
100 	 * Map the file in its entirety.
101 	 */
102 	if (st.st_size > SIZE_T_MAX) {
103 		errno = EFBIG;
104 		BAD;
105 	}
106 	mappedsize = (size_t)st.st_size;
107 	mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
108 	    fd, (off_t)0);
109 	if (mappedfile == (char *)-1)
110 		BAD;
111 
112 	/*
113 	 * Make sure we can access the executable's header
114 	 * directly, and make sure the recognize the executable
115 	 * as an ELF binary.
116 	 */
117 	if (check(0, sizeof *ehdrp))
118 		BADUNMAP;
119 	ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0];
120 
121 	if (memcmp(ehdrp->e_ident, ELFMAG, SELFMAG) != 0 ||
122 	    ehdrp->e_ident[EI_CLASS] != ELFCLASS)
123 		BADUNMAP;
124 
125 	switch (ehdrp->e_machine) {
126 	ELFDEFNNAME(MACHDEP_ID_CASES)
127 
128 	default:
129 		BADUNMAP;
130 	}
131 
132 	/*
133 	 * Find the symbol list and string table.
134 	 */
135 	nshdr = ehdrp->e_shnum;
136 	shdr_off = ehdrp->e_shoff;
137 	shdr_size = ehdrp->e_shentsize * nshdr;
138 
139 	if (check(shdr_off, shdr_size) ||
140 	    (sizeof *shdrp != ehdrp->e_shentsize))
141 		BADUNMAP;
142 	shdrp = (Elf_Shdr *)(void *)&mappedfile[shdr_off];
143 
144 	for (i = 0; i < nshdr; i++) {
145 		if (shdrp[i].sh_type == SHT_SYMTAB) {
146 			symshdrp = &shdrp[i];
147 			symstrshdrp = &shdrp[shdrp[i].sh_link];
148 		}
149 	}
150 
151 	/* Make sure we're not stripped. */
152 	if (symshdrp == NULL || symshdrp->sh_offset == 0)
153 		BADUNMAP;
154 
155 	/* Make sure the symbols and strings are safely mapped. */
156 	if (check(symshdrp->sh_offset, symshdrp->sh_size))
157 		BADUNMAP;
158 	if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
159 		BADUNMAP;
160 
161 	symp = (Elf_Sym *)(void *)&mappedfile[symshdrp->sh_offset];
162 	nsyms = symshdrp->sh_size / sizeof(*symp);
163 	strtab = &mappedfile[symstrshdrp->sh_offset];
164 
165 	/*
166 	 * Clean out any left-over information for all valid entries.
167 	 * Type and value are defined to be 0 if not found; historical
168 	 * versions cleared other and desc as well.
169 	 *
170 	 * XXX Clearing anything other than n_type and n_value violates
171 	 * the semantics given in the man page.
172 	 */
173 	nent = 0;
174 	for (p = list; !ISLAST(p); ++p) {
175 		p->n_type = 0;
176 		p->n_other = 0;
177 		p->n_desc = 0;
178 		p->n_value = 0;
179 		++nent;
180 	}
181 
182 	for (i = 0; i < nsyms; i++) {
183 		for (p = list; !ISLAST(p); ++p) {
184 			const char *nlistname;
185 			char *symtabname;
186 
187 			/* This may be incorrect */
188 			nlistname = p->n_un.n_name;
189 			if (*nlistname == '_')
190 				nlistname++;
191 
192 			symtabname = &strtab[symp[i].st_name];
193 
194 			if (!strcmp(symtabname, nlistname)) {
195 				/*
196 				 * Translate (roughly) from ELF to nlist
197 				 */
198 				p->n_value = symp[i].st_value;
199 				switch (ELFDEFNNAME(ST_TYPE)(symp[i].st_info)) {
200 				case STT_NOTYPE:
201 					p->n_type = N_UNDF;
202 					break;
203 				case STT_OBJECT:
204 					p->n_type = N_DATA;
205 					break;
206 				case STT_FUNC:
207 					p->n_type = N_TEXT;
208 					break;
209 				case STT_FILE:
210 					p->n_type = N_FN;
211 					break;
212 				default:
213 					/* catch other enumerations for gcc */
214 					break;
215 				}
216 				if (ELFDEFNNAME(ST_BIND)(symp[i].st_info) !=
217 				    STB_LOCAL)
218 					p->n_type |= N_EXT;
219 				p->n_desc = 0;			/* XXX */
220 				p->n_other = 0;			/* XXX */
221 
222 				if (--nent <= 0)
223 					goto done;
224 				break;	/* into next run of outer loop */
225 			}
226 		}
227 	}
228 
229 done:
230 	rv = nent;
231 unmap:
232 	munmap(mappedfile, mappedsize);
233 out:
234 	return (rv);
235 }
236 
237 #endif
238