nlist_elf32.c revision 1.2 1 1.2 cgd /* $NetBSD: nlist_elf32.c,v 1.2 1996/09/30 23:51:05 cgd Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.1 cgd * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 1.1 cgd * Copyright (c) 1989, 1993
6 1.1 cgd * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.1 cgd #if 0
39 1.1 cgd static char sccsid[] = "@(#)nlist.c 8.1 (Berkeley) 6/4/93";
40 1.1 cgd #else
41 1.2 cgd static char rcsid[] = "$NetBSD: nlist_elf32.c,v 1.2 1996/09/30 23:51:05 cgd Exp $";
42 1.1 cgd #endif
43 1.1 cgd #endif /* LIBC_SCCS and not lint */
44 1.1 cgd
45 1.1 cgd /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
46 1.1 cgd #ifndef ELFSIZE
47 1.1 cgd #define ELFSIZE 32
48 1.1 cgd #endif
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/mman.h>
52 1.1 cgd #include <sys/stat.h>
53 1.1 cgd #include <sys/file.h>
54 1.1 cgd
55 1.1 cgd #include <errno.h>
56 1.1 cgd #include <stdio.h>
57 1.1 cgd #include <string.h>
58 1.1 cgd #include <unistd.h>
59 1.1 cgd #include <a.out.h> /* for 'struct nlist' declaration */
60 1.1 cgd
61 1.1 cgd #include "nlist_private.h"
62 1.1 cgd #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
63 1.1 cgd #include <sys/exec_elf.h>
64 1.1 cgd #endif
65 1.1 cgd
66 1.1 cgd #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
67 1.1 cgd (defined(NLIST_ELF64) && (ELFSIZE == 64))
68 1.1 cgd
69 1.1 cgd #define CONCAT(x,y) __CONCAT(x,y)
70 1.1 cgd #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
71 1.1 cgd #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
72 1.1 cgd #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
73 1.1 cgd #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
74 1.1 cgd
75 1.1 cgd int
76 1.2 cgd ELFNAMEEND(__fdnlist)(fd, list)
77 1.2 cgd register int fd;
78 1.2 cgd register struct nlist *list;
79 1.1 cgd {
80 1.2 cgd register struct nlist *p;
81 1.2 cgd register caddr_t strtab;
82 1.2 cgd register Elf32_Word symsize, symstrsize;
83 1.2 cgd register Elf32_Off symoff = 0, symstroff;
84 1.2 cgd register long nent, cc, i;
85 1.2 cgd Elf_Sym sbuf[1024];
86 1.2 cgd Elf_Sym *s;
87 1.2 cgd Elf_Ehdr ehdr;
88 1.2 cgd Elf_Shdr *shdr = NULL;
89 1.2 cgd Elf_Word shdr_size;
90 1.2 cgd struct stat st;
91 1.2 cgd
92 1.2 cgd /* Make sure obj is OK */
93 1.2 cgd if (lseek(fd, (off_t)0, SEEK_SET) == -1 ||
94 1.2 cgd read(fd, &ehdr, sizeof(Elf_Ehdr)) != sizeof(Elf_Ehdr) ||
95 1.2 cgd fstat(fd, &st) < 0)
96 1.2 cgd return (-1);
97 1.1 cgd
98 1.1 cgd if (bcmp(ehdr.e_ident, Elf_e_ident, Elf_e_siz))
99 1.1 cgd return (-1);
100 1.1 cgd
101 1.1 cgd switch (ehdr.e_machine) {
102 1.1 cgd ELFDEFNNAME(MACHDEP_ID_CASES)
103 1.1 cgd
104 1.1 cgd default:
105 1.1 cgd return (-1);
106 1.1 cgd }
107 1.1 cgd
108 1.2 cgd /* calculate section header table size */
109 1.2 cgd shdr_size = ehdr.e_shentsize * ehdr.e_shnum;
110 1.2 cgd
111 1.2 cgd /* Make sure it's not too big to mmap */
112 1.2 cgd if (shdr_size > SIZE_T_MAX) {
113 1.2 cgd errno = EFBIG;
114 1.2 cgd return (-1);
115 1.2 cgd }
116 1.2 cgd
117 1.2 cgd /* mmap section header table */
118 1.2 cgd shdr = (Elf_Shdr *)mmap(NULL, (size_t)shdr_size,
119 1.2 cgd PROT_READ, 0, fd, ehdr.e_shoff);
120 1.2 cgd if (shdr == (Elf_Shdr *)-1)
121 1.2 cgd return (-1);
122 1.2 cgd
123 1.2 cgd /*
124 1.2 cgd * Find the symbol table entry and it's corresponding
125 1.2 cgd * string table entry. Version 1.1 of the ABI states
126 1.2 cgd * that there is only one symbol table but that this
127 1.2 cgd * could change in the future.
128 1.2 cgd */
129 1.2 cgd for (i = 0; i < ehdr.e_shnum; i++) {
130 1.2 cgd if (shdr[i].sh_type == Elf_sht_symtab) {
131 1.2 cgd symoff = shdr[i].sh_offset;
132 1.2 cgd symsize = shdr[i].sh_size;
133 1.2 cgd symstroff = shdr[shdr[i].sh_link].sh_offset;
134 1.2 cgd symstrsize = shdr[shdr[i].sh_link].sh_size;
135 1.2 cgd break;
136 1.2 cgd }
137 1.2 cgd }
138 1.2 cgd
139 1.2 cgd /* Flush the section header table */
140 1.2 cgd munmap((caddr_t)shdr, shdr_size);
141 1.1 cgd
142 1.2 cgd /* Check for files too large to mmap. */
143 1.2 cgd /* XXX is this really possible? */
144 1.2 cgd if (symstrsize > SIZE_T_MAX) {
145 1.2 cgd errno = EFBIG;
146 1.2 cgd return (-1);
147 1.2 cgd }
148 1.2 cgd /*
149 1.2 cgd * Map string table into our address space. This gives us
150 1.2 cgd * an easy way to randomly access all the strings, without
151 1.2 cgd * making the memory allocation permanent as with malloc/free
152 1.2 cgd * (i.e., munmap will return it to the system).
153 1.2 cgd */
154 1.2 cgd strtab = mmap(NULL, (size_t)symstrsize, PROT_READ, 0, fd, symstroff);
155 1.2 cgd if (strtab == (char *)-1)
156 1.2 cgd return (-1);
157 1.2 cgd /*
158 1.2 cgd * clean out any left-over information for all valid entries.
159 1.2 cgd * Type and value defined to be 0 if not found; historical
160 1.2 cgd * versions cleared other and desc as well. Also figure out
161 1.2 cgd * the largest string length so don't read any more of the
162 1.2 cgd * string table than we have to.
163 1.2 cgd *
164 1.2 cgd * XXX clearing anything other than n_type and n_value violates
165 1.2 cgd * the semantics given in the man page.
166 1.2 cgd */
167 1.2 cgd nent = 0;
168 1.2 cgd for (p = list; !ISLAST(p); ++p) {
169 1.2 cgd p->n_type = 0;
170 1.2 cgd p->n_other = 0;
171 1.2 cgd p->n_desc = 0;
172 1.2 cgd p->n_value = 0;
173 1.2 cgd ++nent;
174 1.2 cgd }
175 1.2 cgd
176 1.2 cgd /* Don't process any further if object is stripped. */
177 1.2 cgd /* ELFism - dunno if stripped by looking at header */
178 1.2 cgd if (symoff == 0)
179 1.2 cgd goto done;
180 1.2 cgd
181 1.2 cgd if (lseek(fd, symoff, SEEK_SET) == -1) {
182 1.2 cgd nent = -1;
183 1.2 cgd goto done;
184 1.2 cgd }
185 1.2 cgd
186 1.2 cgd while (symsize > 0) {
187 1.2 cgd cc = MIN(symsize, sizeof(sbuf));
188 1.2 cgd if (read(fd, sbuf, cc) != cc)
189 1.2 cgd break;
190 1.2 cgd symsize -= cc;
191 1.2 cgd for (s = sbuf; cc > 0; ++s, cc -= sizeof(*s)) {
192 1.2 cgd register int soff = s->st_name;
193 1.1 cgd
194 1.2 cgd if (soff == 0)
195 1.2 cgd continue;
196 1.1 cgd
197 1.2 cgd for (p = list; !ISLAST(p); p++) {
198 1.1 cgd char *nlistname;
199 1.1 cgd
200 1.1 cgd nlistname = p->n_un.n_name;
201 1.1 cgd if (*nlistname == '_')
202 1.1 cgd nlistname++;
203 1.1 cgd
204 1.2 cgd if (!strcmp(&strtab[soff], nlistname)) {
205 1.2 cgd p->n_value = s->st_value;
206 1.1 cgd
207 1.2 cgd switch(ELF_SYM_TYPE(s->st_info)) {
208 1.2 cgd case Elf_estt_notype:
209 1.2 cgd p->n_type = N_UNDF;
210 1.2 cgd break;
211 1.2 cgd case Elf_estt_object:
212 1.2 cgd p->n_type = N_DATA;
213 1.2 cgd break;
214 1.2 cgd case Elf_estt_func:
215 1.2 cgd p->n_type = N_TEXT;
216 1.2 cgd break;
217 1.2 cgd case Elf_estt_file:
218 1.2 cgd p->n_type = N_FN;
219 1.2 cgd break;
220 1.2 cgd }
221 1.2 cgd if (ELF_SYM_BIND(s->st_info) !=
222 1.2 cgd Elf_estb_local)
223 1.2 cgd p->n_type |= N_EXT;
224 1.2 cgd p->n_desc = 0;
225 1.2 cgd p->n_other = 0;
226 1.2 cgd if (--nent <= 0)
227 1.2 cgd break;
228 1.2 cgd }
229 1.2 cgd }
230 1.2 cgd }
231 1.2 cgd }
232 1.2 cgd done:
233 1.2 cgd munmap(strtab, symstrsize);
234 1.1 cgd
235 1.2 cgd return (nent);
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd #endif
239