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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/lib/libc/gen/nlist.c,v 1.12.2.1 2001/07/11 23:59:09 obrien Exp $
34 * $DragonFly: src/lib/libc/gen/nlist.c,v 1.5 2005/01/31 22:29:15 dillon Exp $
36 * @(#)nlist.c 8.1 (Berkeley) 6/4/93
39 #include "namespace.h"
40 #include <sys/param.h>
50 #include "un-namespace.h"
52 #define _NLIST_DO_AOUT
56 #include <machine/elf.h>
57 #include <elf-hints.h>
60 int __fdnlist (int, struct nlist *);
61 int __aout_fdnlist (int, struct nlist *);
62 int __elf_fdnlist (int, struct nlist *);
71 fd = _open(name, O_RDONLY, 0);
74 n = __fdnlist(fd, list);
79 static struct nlist_handlers {
80 int (*fn) (int fd, struct nlist *list);
97 for (i = 0; i < sizeof(nlist_fn) / sizeof(nlist_fn[0]); i++) {
98 n = (nlist_fn[i].fn)(fd, list);
105 #define ISLAST(p) (p->n_un.n_name == 0 || p->n_un.n_name[0] == 0)
107 #ifdef _NLIST_DO_AOUT
109 __aout_fdnlist(fd, list)
113 struct nlist *p, *symtab;
114 caddr_t strtab, a_out_mmap;
115 off_t stroff, symoff;
121 /* check that file is at least as large as struct exec! */
122 if ((_fstat(fd, &st) < 0) || (st.st_size < sizeof(struct exec)))
125 /* Check for files too large to mmap. */
126 if (st.st_size > SIZE_T_MAX) {
132 * Map the whole a.out file into our address space.
133 * We then find the string table withing this area.
134 * We do not just mmap the string table, as it probably
135 * does not start at a page boundary - we save ourselves a
136 * lot of nastiness by mmapping the whole file.
138 * This gives us an easy way to randomly access all the strings,
139 * without making the memory allocation permanent as with
140 * malloc/free (i.e., munmap will return it to the system).
142 a_out_mmap = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t)0);
143 if (a_out_mmap == MAP_FAILED)
146 exec = (struct exec *)a_out_mmap;
147 if (N_BADMAG(*exec)) {
148 munmap(a_out_mmap, (size_t)st.st_size);
152 symoff = N_SYMOFF(*exec);
153 symsize = exec->a_syms;
154 stroff = symoff + symsize;
156 /* find the string table in our mmapped area */
157 strtab = a_out_mmap + stroff;
158 symtab = (struct nlist *)(a_out_mmap + symoff);
161 * clean out any left-over information for all valid entries.
162 * Type and value defined to be 0 if not found; historical
163 * versions cleared other and desc as well. Also figure out
164 * the largest string length so don't read any more of the
165 * string table than we have to.
167 * XXX clearing anything other than n_type and n_value violates
168 * the semantics given in the man page.
171 for (p = list; !ISLAST(p); ++p) {
179 while (symsize > 0) {
182 symsize-= sizeof(struct nlist);
183 soff = symtab->n_un.n_strx;
186 if (soff != 0 && (symtab->n_type & N_STAB) == 0)
187 for (p = list; !ISLAST(p); p++)
188 if (!strcmp(&strtab[soff], p->n_un.n_name)) {
189 p->n_value = symtab->n_value;
190 p->n_type = symtab->n_type;
191 p->n_desc = symtab->n_desc;
192 p->n_other = symtab->n_other;
198 munmap(a_out_mmap, (size_t)st.st_size);
204 static void elf_sym_to_nlist (struct nlist *, Elf_Sym *, Elf_Shdr *, int);
207 * __elf_is_okay__ - Determine if ehdr really
208 * is ELF and valid for the target platform.
210 * WARNING: This is NOT a ELF ABI function and
211 * as such it's use should be restricted.
214 __elf_is_okay__(ehdr)
219 * We need to check magic, class size, endianess,
220 * and version before we look at the rest of the
221 * Elf_Ehdr structure. These few elements are
222 * represented in a machine independant fashion.
225 ehdr->e_ident[EI_CLASS] == ELF_TARG_CLASS &&
226 ehdr->e_ident[EI_DATA] == ELF_TARG_DATA &&
227 ehdr->e_ident[EI_VERSION] == ELF_TARG_VER) {
229 /* Now check the machine dependant header */
230 if (ehdr->e_machine == ELF_TARG_MACH &&
231 ehdr->e_version == ELF_TARG_VER)
238 __elf_fdnlist(fd, list)
243 Elf_Off symoff = 0, symstroff = 0;
244 Elf_Word symsize = 0, symstrsize = 0;
252 Elf_Shdr *shdr = NULL;
258 /* Make sure obj is OK */
259 if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
260 _read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
261 !__elf_is_okay__(&ehdr) ||
265 /* calculate section header table size */
266 shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
268 /* Make sure it's not too big to mmap */
269 if (shdr_size > SIZE_T_MAX) {
274 /* mmap section header table */
275 base = mmap(NULL, (size_t)shdr_size, PROT_READ, 0, fd,
276 (off_t)ehdr.e_shoff);
277 if (base == MAP_FAILED)
279 shdr = (Elf_Shdr *)base;
282 * Find the symbol table entry and it's corresponding
283 * string table entry. Version 1.1 of the ABI states
284 * that there is only one symbol table but that this
285 * could change in the future.
287 for (i = 0; i < ehdr.e_shnum; i++) {
288 if (shdr[i].sh_type == SHT_SYMTAB) {
289 symoff = shdr[i].sh_offset;
290 symsize = shdr[i].sh_size;
291 symstroff = shdr[shdr[i].sh_link].sh_offset;
292 symstrsize = shdr[shdr[i].sh_link].sh_size;
297 /* Check for files too large to mmap. */
298 if (symstrsize > SIZE_T_MAX) {
303 * Map string table into our address space. This gives us
304 * an easy way to randomly access all the strings, without
305 * making the memory allocation permanent as with malloc/free
306 * (i.e., munmap will return it to the system).
308 base = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd,
310 if (base == MAP_FAILED)
312 strtab = (char *)base;
315 * clean out any left-over information for all valid entries.
316 * Type and value defined to be 0 if not found; historical
317 * versions cleared other and desc as well. Also figure out
318 * the largest string length so don't read any more of the
319 * string table than we have to.
321 * XXX clearing anything other than n_type and n_value violates
322 * the semantics given in the man page.
325 for (p = list; !ISLAST(p); ++p) {
333 /* Don't process any further if object is stripped. */
337 if (lseek(fd, (off_t) symoff, SEEK_SET) == -1) {
342 while (symsize > 0 && nent > 0) {
343 cc = MIN(symsize, sizeof(sbuf));
344 if (_read(fd, sbuf, cc) != cc)
347 for (s = sbuf; cc > 0 && nent > 0; ++s, cc -= sizeof(*s)) {
351 name = strtab + s->st_name;
354 for (p = list; !ISLAST(p); p++) {
355 if ((p->n_un.n_name[0] == '_' &&
356 strcmp(name, p->n_un.n_name+1) == 0)
357 || strcmp(name, p->n_un.n_name) == 0) {
358 elf_sym_to_nlist(p, s, shdr,
369 munmap(strtab, symstrsize);
371 munmap(shdr, shdr_size);
377 * Convert an Elf_Sym into an nlist structure. This fills in only the
378 * n_value and n_type members.
381 elf_sym_to_nlist(nl, s, shdr, shnum)
387 nl->n_value = s->st_value;
389 switch (s->st_shndx) {
395 nl->n_type = ELF_ST_TYPE(s->st_info) == STT_FILE ?
399 if (s->st_shndx >= shnum)
402 Elf_Shdr *sh = shdr + s->st_shndx;
404 nl->n_type = sh->sh_type == SHT_PROGBITS ?
405 (sh->sh_flags & SHF_WRITE ? N_DATA : N_TEXT) :
406 (sh->sh_type == SHT_NOBITS ? N_BSS : N_UNDF);
411 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
412 ELF_ST_BIND(s->st_info) == STB_WEAK)
415 #endif /* _NLIST_DO_ELF */