1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
/* $NetBSD: nlist_elf32.c,v 1.38 2015/05/20 04:37:45 matt Exp $ */
/*
* Copyright (c) 1996 Christopher G. Demetriou
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed for the
* NetBSD Project. See http://www.NetBSD.org/ for
* information about NetBSD.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: nlist_elf32.c,v 1.38 2015/05/20 04:37:45 matt Exp $");
#endif /* LIBC_SCCS and not lint */
/* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
#ifndef ELFSIZE
#define ELFSIZE 32
#endif
#include "namespace.h"
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nlist.h>
#include "nlist_private.h"
#if defined(NLIST_ELF32) || defined(NLIST_ELF64)
#include <sys/exec_elf.h>
#endif
#include <sys/ksyms.h> /* after sys/exec_elf.h */
#if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
(defined(NLIST_ELF64) && (ELFSIZE == 64))
/* No need to check for off < 0 because it is unsigned */
#define check(off, size) (off + size > mappedsize)
#define BAD goto out
#define BADUNMAP goto unmap
int
ELFNAMEEND(__fdnlist)(int fd, struct nlist *list)
{
struct stat st;
Elf_Ehdr ehdr;
#if defined(_LP64) || ELFSIZE == 32 || defined(ELF64_MACHDEP_ID)
#if (ELFSIZE == 32)
Elf32_Half nshdr;
#elif (ELFSIZE == 64)
Elf64_Word nshdr;
#endif
/* Only support 64+32 mode on LP64 and those that have defined */
/* ELF64_MACHDEP_ID, otherwise no support for 64 mode on ILP32 */
Elf_Ehdr *ehdrp;
Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
Elf_Sym *symp;
Elf_Off shdr_off;
Elf_Word shdr_size;
struct nlist *p;
char *mappedfile, *strtab;
size_t mappedsize, nsyms;
int nent;
#endif
int rv;
size_t i;
_DIAGASSERT(fd != -1);
_DIAGASSERT(list != NULL);
rv = -1;
/*
* If we can't fstat() the file, something bad is going on.
*/
if (fstat(fd, &st) < 0)
BAD;
/*
* Map the file in its entirety.
*/
if ((uintmax_t)st.st_size > (uintmax_t)SIZE_T_MAX) {
errno = EFBIG;
BAD;
}
/*
* Read the elf header of the file.
*/
if ((ssize_t)(i = pread(fd, &ehdr, sizeof(Elf_Ehdr), (off_t)0)) == -1)
BAD;
/*
* Check that the elf header is correct.
*/
if (i != sizeof(Elf_Ehdr))
BAD;
if (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0 ||
ehdr.e_ident[EI_CLASS] != ELFCLASS)
BAD;
switch (ehdr.e_machine) {
ELFDEFNNAME(MACHDEP_ID_CASES)
default:
BAD;
}
#if defined(_LP64) || ELFSIZE == 32 || defined(ELF64_MACHDEP_ID)
symshdrp = symstrshdrp = NULL;
/* Only support 64+32 mode on LP64 and those that have defined */
/* ELF64_MACHDEP_ID, otherwise no support for 64 mode on ILP32 */
if (S_ISCHR(st.st_mode)) {
const char *nlistname;
Elf_Sym sym;
/*
* Character device; assume /dev/ksyms.
*/
nent = 0;
for (p = list; !ISLAST(p); ++p) {
struct ksyms_gsymbol kg;
int error;
p->n_other = 0;
p->n_desc = 0;
nlistname = N_NAME(p);
if (*nlistname == '_')
nlistname++;
memset(&kg, 0, sizeof(kg));
kg.kg_name = nlistname;
#ifdef OKIOCGSYMBOL
struct ksyms_ogsymbol okg;
error = ioctl(fd, KIOCGSYMBOL, &kg);
if (error == 0) {
sym = kg.kg_sym;
} else if (error && errno == ENOTTY) {
memset(&okg, 0, sizeof(okg));
okg.kg_name = nlistname;
okg.kg_sym = &sym;
error = ioctl(fd, OKIOCGSYMBOL, &okg);
}
#else
kg.kg_sym = &sym;
error = ioctl(fd, KIOCGSYMBOL, &kg);
#endif
if (error == 0
#if !defined(_LP64) && ELFSIZE == 64
#if __mips__
&& (intptr_t)sym.st_value == (intmax_t)sym.st_value
#else
&& (uintptr_t)sym.st_value == sym.st_value
#endif
#endif
&& 1) {
p->n_value = (uintptr_t)sym.st_value;
switch (ELF_ST_TYPE(sym.st_info)) {
case STT_NOTYPE:
p->n_type = N_UNDF;
break;
case STT_COMMON:
case STT_OBJECT:
p->n_type = N_DATA;
break;
case STT_FUNC:
p->n_type = N_TEXT;
break;
case STT_FILE:
p->n_type = N_FN;
break;
default:
p->n_type = 0;
/* catch other enumerations for gcc */
break;
}
if (ELF_ST_BIND(sym.st_info) != STB_LOCAL)
p->n_type |= N_EXT;
} else {
nent++;
p->n_value = 0;
p->n_type = 0;
}
}
return nent;
}
mappedsize = (size_t)st.st_size;
mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
fd, (off_t)0);
if (mappedfile == (char *)-1)
BAD;
/*
* Make sure we can access the executable's header
* directly, and make sure the recognize the executable
* as an ELF binary.
*/
if (check(0, sizeof *ehdrp))
BADUNMAP;
ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0];
/*
* Find the symbol list and string table.
*/
nshdr = ehdrp->e_shnum;
shdr_off = ehdrp->e_shoff;
shdr_size = ehdrp->e_shentsize * nshdr;
if (check(shdr_off, shdr_size) ||
(sizeof *shdrp != ehdrp->e_shentsize))
BADUNMAP;
shdrp = (void *)&mappedfile[(size_t)shdr_off];
for (i = 0; i < nshdr; i++) {
if (shdrp[i].sh_type == SHT_SYMTAB) {
symshdrp = &shdrp[i];
symstrshdrp = &shdrp[shdrp[i].sh_link];
}
}
/* Make sure we're not stripped. */
if (symshdrp == NULL || symshdrp->sh_offset == 0)
BADUNMAP;
/* Make sure the symbols and strings are safely mapped. */
if (check(symshdrp->sh_offset, symshdrp->sh_size))
BADUNMAP;
if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
BADUNMAP;
symp = (void *)&mappedfile[(size_t)symshdrp->sh_offset];
nsyms = (size_t)(symshdrp->sh_size / sizeof(*symp));
strtab = &mappedfile[(size_t)symstrshdrp->sh_offset];
/*
* Clean out any left-over information for all valid entries.
* Type and value are defined to be 0 if not found; historical
* versions cleared other and desc as well.
*
* XXX Clearing anything other than n_type and n_value violates
* the semantics given in the man page.
*/
nent = 0;
for (p = list; !ISLAST(p); ++p) {
p->n_type = 0;
p->n_other = 0;
p->n_desc = 0;
p->n_value = 0;
++nent;
}
for (i = 0; i < nsyms; i++) {
for (p = list; !ISLAST(p); ++p) {
const char *nlistname;
char *symtabname;
/* This may be incorrect */
nlistname = N_NAME(p);
if (*nlistname == '_')
nlistname++;
symtabname = &strtab[symp[i].st_name];
if (!strcmp(symtabname, nlistname)) {
/*
* Translate (roughly) from ELF to nlist
*/
p->n_value = (uintptr_t)symp[i].st_value;
switch (ELF_ST_TYPE(symp[i].st_info)) {
case STT_NOTYPE:
p->n_type = N_UNDF;
break;
case STT_OBJECT:
case STT_COMMON:
p->n_type = N_DATA;
break;
case STT_FUNC:
p->n_type = N_TEXT;
break;
case STT_FILE:
p->n_type = N_FN;
break;
default:
/* catch other enumerations for gcc */
break;
}
if (ELF_ST_BIND(symp[i].st_info) != STB_LOCAL)
p->n_type |= N_EXT;
p->n_desc = 0; /* XXX */
p->n_other = 0; /* XXX */
if (--nent <= 0)
goto done;
break; /* into next run of outer loop */
}
}
}
done:
rv = nent;
unmap:
munmap(mappedfile, mappedsize);
#endif /* _LP64 || ELFSIZE == 32 || ELF64_MACHDEP_ID */
out:
return (rv);
}
#endif
|