1 /* $NetBSD: nlist_elf32.c,v 1.35 2012/03/21 02:18:14 christos Exp $ */
4 * Copyright (c) 1996 Christopher G. Demetriou
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
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 */
42 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
47 #include "namespace.h"
48 #include <sys/param.h>
52 #include <sys/ioctl.h>
53 #include <sys/ksyms.h>
62 #include "nlist_private.h"
63 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
64 #include <sys/exec_elf.h>
67 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
68 (defined(NLIST_ELF64) && (ELFSIZE == 64))
70 /* No need to check for off < 0 because it is unsigned */
71 #define check(off, size) (off + size > mappedsize)
73 #define BADUNMAP goto unmap
76 ELFNAMEEND(__fdnlist
)(int fd
, struct nlist
*list
)
80 #if _LP64 || ELFSIZE == 32
86 /* Only support 64+32 mode on LP64; no support for 64 mode on ILP32 */
88 Elf_Shdr
*shdrp
, *symshdrp
, *symstrshdrp
;
93 char *mappedfile
, *strtab
;
94 size_t mappedsize
, nsyms
;
100 _DIAGASSERT(fd
!= -1);
101 _DIAGASSERT(list
!= NULL
);
106 * If we can't fstat() the file, something bad is going on.
108 if (fstat(fd
, &st
) < 0)
112 * Map the file in its entirety.
114 if ((uintmax_t)st
.st_size
> (uintmax_t)SIZE_T_MAX
) {
120 * Read the elf header of the file.
122 if ((ssize_t
)(i
= pread(fd
, &ehdr
, sizeof(Elf_Ehdr
), (off_t
)0)) == -1)
126 * Check that the elf header is correct.
128 if (i
!= sizeof(Elf_Ehdr
))
130 if (memcmp(ehdr
.e_ident
, ELFMAG
, SELFMAG
) != 0 ||
131 ehdr
.e_ident
[EI_CLASS
] != ELFCLASS
)
134 switch (ehdr
.e_machine
) {
135 ELFDEFNNAME(MACHDEP_ID_CASES
)
140 #if _LP64 || ELFSIZE == 32
141 symshdrp
= symstrshdrp
= NULL
;
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
;
150 * Character device; assume /dev/ksyms.
153 for (p
= list
; !ISLAST(p
); ++p
) {
157 nlistname
= N_NAME(p
);
158 if (*nlistname
== '_')
161 kg
.kg_name
= nlistname
;
163 if (ioctl(fd
, KIOCGSYMBOL
, &kg
) == 0) {
164 p
->n_value
= (uintptr_t)sym
.st_value
;
165 switch (ELF_ST_TYPE(sym
.st_info
)) {
181 /* catch other enumerations for gcc */
184 if (ELF_ST_BIND(sym
.st_info
) != STB_LOCAL
)
195 mappedsize
= (size_t)st
.st_size
;
196 mappedfile
= mmap(NULL
, mappedsize
, PROT_READ
, MAP_PRIVATE
|MAP_FILE
,
198 if (mappedfile
== (char *)-1)
202 * Make sure we can access the executable's header
203 * directly, and make sure the recognize the executable
206 if (check(0, sizeof *ehdrp
))
208 ehdrp
= (Elf_Ehdr
*)(void *)&mappedfile
[0];
211 * Find the symbol list and string table.
213 nshdr
= ehdrp
->e_shnum
;
214 shdr_off
= ehdrp
->e_shoff
;
215 shdr_size
= ehdrp
->e_shentsize
* nshdr
;
217 if (check(shdr_off
, shdr_size
) ||
218 (sizeof *shdrp
!= ehdrp
->e_shentsize
))
220 shdrp
= (void *)&mappedfile
[(size_t)shdr_off
];
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
];
229 /* Make sure we're not stripped. */
230 if (symshdrp
== NULL
|| symshdrp
->sh_offset
== 0)
233 /* Make sure the symbols and strings are safely mapped. */
234 if (check(symshdrp
->sh_offset
, symshdrp
->sh_size
))
236 if (check(symstrshdrp
->sh_offset
, symstrshdrp
->sh_size
))
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
];
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.
248 * XXX Clearing anything other than n_type and n_value violates
249 * the semantics given in the man page.
252 for (p
= list
; !ISLAST(p
); ++p
) {
260 for (i
= 0; i
< nsyms
; i
++) {
261 for (p
= list
; !ISLAST(p
); ++p
) {
262 const char *nlistname
;
265 /* This may be incorrect */
266 nlistname
= N_NAME(p
);
267 if (*nlistname
== '_')
270 symtabname
= &strtab
[symp
[i
].st_name
];
272 if (!strcmp(symtabname
, nlistname
)) {
274 * Translate (roughly) from ELF to nlist
276 p
->n_value
= (uintptr_t)symp
[i
].st_value
;
277 switch (ELF_ST_TYPE(symp
[i
].st_info
)) {
292 /* catch other enumerations for gcc */
295 if (ELF_ST_BIND(symp
[i
].st_info
) != STB_LOCAL
)
297 p
->n_desc
= 0; /* XXX */
298 p
->n_other
= 0; /* XXX */
302 break; /* into next run of outer loop */
310 munmap(mappedfile
, mappedsize
);
311 #endif /* _LP64 || ELFSIZE == 32 */