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