2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if defined(LIBC_SCCS) && !defined(lint)
33 static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/4/93";
34 #endif /* LIBC_SCCS and not lint */
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include "namespace.h"
39 #include <sys/param.h>
43 #include <arpa/inet.h>
50 #include "un-namespace.h"
52 /* i386 is the only current FreeBSD architecture that used a.out format. */
54 #define _NLIST_DO_AOUT
59 #include <machine/elf.h>
60 #include <elf-hints.h>
63 int __fdnlist(int, struct nlist *);
64 int __aout_fdnlist(int, struct nlist *);
65 int __elf_fdnlist(int, struct nlist *);
66 int __elf_is_okay__(Elf_Ehdr *);
69 nlist(const char *name, struct nlist *list)
73 fd = _open(name, O_RDONLY | O_CLOEXEC, 0);
76 n = __fdnlist(fd, list);
81 static struct nlist_handlers {
82 int (*fn)(int fd, struct nlist *list);
93 __fdnlist(int fd, struct nlist *list)
98 for (i = 0; i < nitems(nlist_fn); i++) {
99 n = (nlist_fn[i].fn)(fd, list);
106 #define ISLAST(p) (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
108 #ifdef _NLIST_DO_AOUT
110 __aout_fdnlist(int fd, struct nlist *list)
112 struct nlist *p, *symtab;
113 caddr_t strtab, a_out_mmap;
114 off_t stroff, symoff;
120 /* check that file is at least as large as struct exec! */
121 if ((_fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec)))
124 /* Check for files too large to mmap. */
125 if (st.st_size > SIZE_T_MAX) {
131 * Map the whole a.out file into our address space.
132 * We then find the string table withing this area.
133 * We do not just mmap the string table, as it probably
134 * does not start at a page boundary - we save ourselves a
135 * lot of nastiness by mmapping the whole file.
137 * This gives us an easy way to randomly access all the strings,
138 * without making the memory allocation permanent as with
139 * malloc/free (i.e., munmap will return it to the system).
141 a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
142 if (a_out_mmap == MAP_FAILED)
145 exec = (struct exec *)a_out_mmap;
146 if (N_BADMAG(*exec)) {
147 munmap(a_out_mmap, (size_t)st.st_size);
151 symoff = N_SYMOFF(*exec);
152 symsize = exec->a_syms;
153 stroff = symoff + symsize;
155 /* find the string table in our mmapped area */
156 strtab = a_out_mmap + stroff;
157 symtab = (struct nlist *)(a_out_mmap + symoff);
160 * clean out any left-over information for all valid entries.
161 * Type and value defined to be 0 if not found; historical
162 * versions cleared other and desc as well. Also figure out
163 * the largest string length so don't read any more of the
164 * string table than we have to.
166 * XXX clearing anything other than n_type and n_value violates
167 * the semantics given in the man page.
170 for (p = list; !ISLAST(p); ++p) {
178 while (symsize > 0) {
181 symsize-= sizeof(struct nlist);
182 soff = symtab->n_un.n_strx;
185 if (soff != 0 && (symtab->n_type & N_STAB) == 0)
186 for (p = list; !ISLAST(p); p++)
187 if (!strcmp(&strtab[soff], p->n_un.n_name)) {
188 p->n_value = symtab->n_value;
189 p->n_type = symtab->n_type;
190 p->n_desc = symtab->n_desc;
191 p->n_other = symtab->n_other;
197 munmap(a_out_mmap, (size_t)st.st_size);
203 static void elf_sym_to_nlist(struct nlist *, Elf_Sym *, Elf_Shdr *, int);
206 * __elf_is_okay__ - Determine if ehdr really
207 * is ELF and valid for the target platform.
209 * WARNING: This is NOT an ELF ABI function and
210 * as such its use should be restricted.
213 __elf_is_okay__(Elf_Ehdr *ehdr)
217 * We need to check magic, class size, endianess,
218 * and version before we look at the rest of the
219 * Elf_Ehdr structure. These few elements are
220 * represented in a machine independant fashion.
223 ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
224 ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
225 ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
227 /* Now check the machine dependant header */
228 if (ehdr->e_machine == ELF_TARG_MACH &&
229 ehdr->e_version == ELF_TARG_VER)
236 __elf_fdnlist(int fd, struct nlist *list)
239 Elf_Off symoff = 0, symstroff = 0;
240 Elf_Size symsize = 0, symstrsize = 0;
248 Elf_Shdr *shdr = NULL;
253 /* Make sure obj is OK */
254 if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
255 _read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
256 !__elf_is_okay__(&ehdr) ||
260 /* calculate section header table size */
261 shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
263 /* Make sure it's not too big to mmap */
264 if (shdr_size > SIZE_T_MAX) {
269 /* mmap section header table */
270 base = mmap(NULL, (size_t)shdr_size, PROT_READ, MAP_PRIVATE, fd,
271 (off_t)ehdr.e_shoff);
272 if (base == MAP_FAILED)
274 shdr = (Elf_Shdr *)base;
277 * Find the symbol table entry and it's corresponding
278 * string table entry. Version 1.1 of the ABI states
279 * that there is only one symbol table but that this
280 * could change in the future.
282 for (i = 0; i < ehdr.e_shnum; i++) {
283 if (shdr[i].sh_type == SHT_SYMTAB) {
284 symoff = shdr[i].sh_offset;
285 symsize = shdr[i].sh_size;
286 symstroff = shdr[shdr[i].sh_link].sh_offset;
287 symstrsize = shdr[shdr[i].sh_link].sh_size;
292 /* Check for files too large to mmap. */
293 if (symstrsize > SIZE_T_MAX) {
298 * Map string table into our address space. This gives us
299 * an easy way to randomly access all the strings, without
300 * making the memory allocation permanent as with malloc/free
301 * (i.e., munmap will return it to the system).
303 base = mmap(NULL, (size_t)symstrsize, PROT_READ, MAP_PRIVATE, fd,
305 if (base == MAP_FAILED)
307 strtab = (char *)base;
310 * clean out any left-over information for all valid entries.
311 * Type and value defined to be 0 if not found; historical
312 * versions cleared other and desc as well. Also figure out
313 * the largest string length so don't read any more of the
314 * string table than we have to.
316 * XXX clearing anything other than n_type and n_value violates
317 * the semantics given in the man page.
320 for (p = list; !ISLAST(p); ++p) {
328 /* Don't process any further if object is stripped. */
332 if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) {
337 while (symsize > 0 && nent > 0) {
338 cc = MIN(symsize, sizeof(sbuf));
339 if (_read(fd, sbuf, cc) != cc)
342 for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) {
346 name = strtab + s->st_name;
349 for (p = list; !ISLAST(p); p++) {
350 if ((p->n_un.n_name[0] == '_' &&
351 strcmp(name, p->n_un.n_name+1) == 0)
352 || strcmp(name, p->n_un.n_name) == 0) {
353 elf_sym_to_nlist(p, s, shdr,
364 munmap(strtab, symstrsize);
366 munmap(shdr, shdr_size);
372 * Convert an Elf_Sym into an nlist structure. This fills in only the
373 * n_value and n_type members.
376 elf_sym_to_nlist(struct nlist *nl, Elf_Sym *s, Elf_Shdr *shdr, int shnum)
378 nl->n_value = s->st_value;
380 switch (s->st_shndx) {
386 nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ?
390 if (s->st_shndx >= shnum)
393 Elf_Shdr *sh = shdr + s->st_shndx;
395 nl->n_type = sh->sh_type == SHT_PROGBITS ?
396 (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) :
397 (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF);
402 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
403 ELF_ST_BIND(s->st_info) == STB_WEAK)
406 #endif /* _NLIST_DO_ELF */