2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid
[] = "@(#)nlist.c 8.1 (Berkeley) 6/4/93";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "namespace.h"
37 #include <sys/param.h>
41 #include <arpa/inet.h>
48 #include "un-namespace.h"
50 /* i386 is the only current FreeBSD architecture that used a.out format. */
52 #define _NLIST_DO_AOUT
57 #include <machine/elf.h>
58 #include <elf-hints.h>
61 int __fdnlist(int, struct nlist
*);
62 int __aout_fdnlist(int, struct nlist
*);
63 int __elf_fdnlist(int, struct nlist
*);
64 int __elf_is_okay__(Elf_Ehdr
*);
67 nlist(const char *name
, struct nlist
*list
)
71 fd
= _open(name
, O_RDONLY
| O_CLOEXEC
, 0);
74 n
= __fdnlist(fd
, list
);
79 static struct nlist_handlers
{
80 int (*fn
)(int fd
, struct nlist
*list
);
91 __fdnlist(int fd
, struct nlist
*list
)
96 for (i
= 0; i
< sizeof(nlist_fn
) / sizeof(nlist_fn
[0]); i
++) {
97 n
= (nlist_fn
[i
].fn
)(fd
, list
);
104 #define ISLAST(p) (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
106 #ifdef _NLIST_DO_AOUT
108 __aout_fdnlist(int fd
, struct nlist
*list
)
110 struct nlist
*p
, *symtab
;
111 caddr_t strtab
, a_out_mmap
;
112 off_t stroff
, symoff
;
118 /* check that file is at least as large as struct exec! */
119 if ((_fstat(fd
, &st
) < 0) || (st
.st_size
< sizeof(struct exec
)))
122 /* Check for files too large to mmap. */
123 if (st
.st_size
> SIZE_T_MAX
) {
129 * Map the whole a.out file into our address space.
130 * We then find the string table withing this area.
131 * We do not just mmap the string table, as it probably
132 * does not start at a page boundary - we save ourselves a
133 * lot of nastiness by mmapping the whole file.
135 * This gives us an easy way to randomly access all the strings,
136 * without making the memory allocation permanent as with
137 * malloc/free (i.e., munmap will return it to the system).
139 a_out_mmap
= mmap(NULL
, (size_t)st
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, (off_t
)0);
140 if (a_out_mmap
== MAP_FAILED
)
143 exec
= (struct exec
*)a_out_mmap
;
144 if (N_BADMAG(*exec
)) {
145 munmap(a_out_mmap
, (size_t)st
.st_size
);
149 symoff
= N_SYMOFF(*exec
);
150 symsize
= exec
->a_syms
;
151 stroff
= symoff
+ symsize
;
153 /* find the string table in our mmapped area */
154 strtab
= a_out_mmap
+ stroff
;
155 symtab
= (struct nlist
*)(a_out_mmap
+ symoff
);
158 * clean out any left-over information for all valid entries.
159 * Type and value defined to be 0 if not found; historical
160 * versions cleared other and desc as well. Also figure out
161 * the largest string length so don't read any more of the
162 * string table than we have to.
164 * XXX clearing anything other than n_type and n_value violates
165 * the semantics given in the man page.
168 for (p
= list
; !ISLAST(p
); ++p
) {
176 while (symsize
> 0) {
179 symsize
-= sizeof(struct nlist
);
180 soff
= symtab
->n_un
.n_strx
;
183 if (soff
!= 0 && (symtab
->n_type
& N_STAB
) == 0)
184 for (p
= list
; !ISLAST(p
); p
++)
185 if (!strcmp(&strtab
[soff
], p
->n_un
.n_name
)) {
186 p
->n_value
= symtab
->n_value
;
187 p
->n_type
= symtab
->n_type
;
188 p
->n_desc
= symtab
->n_desc
;
189 p
->n_other
= symtab
->n_other
;
195 munmap(a_out_mmap
, (size_t)st
.st_size
);
201 static void elf_sym_to_nlist(struct nlist
*, Elf_Sym
*, Elf_Shdr
*, int);
204 * __elf_is_okay__ - Determine if ehdr really
205 * is ELF and valid for the target platform.
207 * WARNING: This is NOT an ELF ABI function and
208 * as such its use should be restricted.
211 __elf_is_okay__(Elf_Ehdr
*ehdr
)
215 * We need to check magic, class size, endianess,
216 * and version before we look at the rest of the
217 * Elf_Ehdr structure. These few elements are
218 * represented in a machine independant fashion.
221 ehdr
->e_ident
[EI_CLASS
] == ELF_TARG_CLASS
&&
222 ehdr
->e_ident
[EI_DATA
] == ELF_TARG_DATA
&&
223 ehdr
->e_ident
[EI_VERSION
] == ELF_TARG_VER
) {
225 /* Now check the machine dependant header */
226 if (ehdr
->e_machine
== ELF_TARG_MACH
&&
227 ehdr
->e_version
== ELF_TARG_VER
)
234 __elf_fdnlist(int fd
, struct nlist
*list
)
237 Elf_Off symoff
= 0, symstroff
= 0;
238 Elf_Size symsize
= 0, symstrsize
= 0;
246 Elf_Shdr
*shdr
= NULL
;
251 /* Make sure obj is OK */
252 if (lseek(fd
, (off_t
)0, SEEK_SET
) == -1 ||
253 _read(fd
, &ehdr
, sizeof(Elf_Ehdr
)) != sizeof(Elf_Ehdr
) ||
254 !__elf_is_okay__(&ehdr
) ||
258 /* calculate section header table size */
259 shdr_size
= ehdr
.e_shentsize
* ehdr
.e_shnum
;
261 /* Make sure it's not too big to mmap */
262 if (shdr_size
> SIZE_T_MAX
) {
267 /* mmap section header table */
268 base
= mmap(NULL
, (size_t)shdr_size
, PROT_READ
, MAP_PRIVATE
, fd
,
269 (off_t
)ehdr
.e_shoff
);
270 if (base
== MAP_FAILED
)
272 shdr
= (Elf_Shdr
*)base
;
275 * Find the symbol table entry and it's corresponding
276 * string table entry. Version 1.1 of the ABI states
277 * that there is only one symbol table but that this
278 * could change in the future.
280 for (i
= 0; i
< ehdr
.e_shnum
; i
++) {
281 if (shdr
[i
].sh_type
== SHT_SYMTAB
) {
282 symoff
= shdr
[i
].sh_offset
;
283 symsize
= shdr
[i
].sh_size
;
284 symstroff
= shdr
[shdr
[i
].sh_link
].sh_offset
;
285 symstrsize
= shdr
[shdr
[i
].sh_link
].sh_size
;
290 /* Check for files too large to mmap. */
291 if (symstrsize
> SIZE_T_MAX
) {
296 * Map string table into our address space. This gives us
297 * an easy way to randomly access all the strings, without
298 * making the memory allocation permanent as with malloc/free
299 * (i.e., munmap will return it to the system).
301 base
= mmap(NULL
, (size_t)symstrsize
, PROT_READ
, MAP_PRIVATE
, fd
,
303 if (base
== MAP_FAILED
)
305 strtab
= (char *)base
;
308 * clean out any left-over information for all valid entries.
309 * Type and value defined to be 0 if not found; historical
310 * versions cleared other and desc as well. Also figure out
311 * the largest string length so don't read any more of the
312 * string table than we have to.
314 * XXX clearing anything other than n_type and n_value violates
315 * the semantics given in the man page.
318 for (p
= list
; !ISLAST(p
); ++p
) {
326 /* Don't process any further if object is stripped. */
330 if (lseek(fd
, (off_t
) symoff
, SEEK_SET
) == -1) {
335 while (symsize
> 0 && nent
> 0) {
336 cc
= MIN(symsize
, sizeof(sbuf
));
337 if (_read(fd
, sbuf
, cc
) != cc
)
340 for (s
= sbuf
; cc
> 0 && nent
> 0; ++s
, cc
-= sizeof(*s
)) {
344 name
= strtab
+ s
->st_name
;
347 for (p
= list
; !ISLAST(p
); p
++) {
348 if ((p
->n_un
.n_name
[0] == '_' &&
349 strcmp(name
, p
->n_un
.n_name
+1) == 0)
350 || strcmp(name
, p
->n_un
.n_name
) == 0) {
351 elf_sym_to_nlist(p
, s
, shdr
,
362 munmap(strtab
, symstrsize
);
364 munmap(shdr
, shdr_size
);
370 * Convert an Elf_Sym into an nlist structure. This fills in only the
371 * n_value and n_type members.
374 elf_sym_to_nlist(struct nlist
*nl
, Elf_Sym
*s
, Elf_Shdr
*shdr
, int shnum
)
376 nl
->n_value
= s
->st_value
;
378 switch (s
->st_shndx
) {
384 nl
->n_type
= ELF_ST_TYPE(s
->st_info
) == STT_FILE
?
388 if (s
->st_shndx
>= shnum
)
391 Elf_Shdr
*sh
= shdr
+ s
->st_shndx
;
393 nl
->n_type
= sh
->sh_type
== SHT_PROGBITS
?
394 (sh
->sh_flags
& SHF_WRITE
? N_DATA
: N_TEXT
) :
395 (sh
->sh_type
== SHT_NOBITS
? N_BSS
: N_UNDF
);
400 if (ELF_ST_BIND(s
->st_info
) == STB_GLOBAL
||
401 ELF_ST_BIND(s
->st_info
) == STB_WEAK
)
404 #endif /* _NLIST_DO_ELF */