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