nlist_elf32.c revision 1.14 1 /* $NetBSD: nlist_elf32.c,v 1.14 1999/09/16 11:45:01 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou
17 * for the NetBSD Project.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
34 #ifndef ELFSIZE
35 #define ELFSIZE 32
36 #endif
37
38 #include "namespace.h"
39 #include <sys/param.h>
40 #include <sys/mman.h>
41 #include <sys/stat.h>
42 #include <sys/file.h>
43
44 #include <assert.h>
45 #include <errno.h>
46 #include <stdio.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <a.out.h> /* for 'struct nlist' declaration */
50
51 #include "nlist_private.h"
52 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
53 #include <sys/exec_elf.h>
54 #endif
55
56 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
57 (defined(NLIST_ELF64) && (ELFSIZE == 64))
58
59 #define CONCAT(x,y) __CONCAT(x,y)
60 #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
61 #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
62 #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
63 #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
64
65 /* No need to check for off < 0 because it is unsigned */
66 #define check(off, size) (off + size > mappedsize)
67 #define BAD goto out
68 #define BADUNMAP goto unmap
69
70 int
71 ELFNAMEEND(__fdnlist)(fd, list)
72 int fd;
73 struct nlist *list;
74 {
75 struct stat st;
76 struct nlist *p;
77 char *mappedfile, *strtab;
78 size_t mappedsize;
79 Elf_Ehdr *ehdrp;
80 Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
81 Elf_Sym *symp;
82 Elf_Off shdr_off;
83 Elf_Word shdr_size;
84 #if (ELFSIZE == 32)
85 Elf32_Half nshdr;
86 #elif (ELFSIZE == 64)
87 Elf64_Half nshdr;
88 #endif
89 size_t i, nsyms;
90 int rv, nent;
91
92 _DIAGASSERT(fd != -1);
93 _DIAGASSERT(list != NULL);
94 #ifdef _DIAGNOSTIC
95 if (fd == -1) {
96 errno = EBADF;
97 return (-1);
98 }
99 if (list == NULL) {
100 errno = EFAULT;
101 return (-1);
102 }
103 #endif
104
105 rv = -1;
106
107 symshdrp = symstrshdrp = NULL;
108
109 /*
110 * If we can't fstat() the file, something bad is going on.
111 */
112 if (fstat(fd, &st) < 0)
113 BAD;
114
115 /*
116 * Map the file in its entirety.
117 */
118 if (st.st_size > SIZE_T_MAX) {
119 errno = EFBIG;
120 BAD;
121 }
122 mappedsize = (size_t)st.st_size;
123 mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
124 fd, (off_t)0);
125 if (mappedfile == (char *)-1)
126 BAD;
127
128 /*
129 * Make sure we can access the executable's header
130 * directly, and make sure the recognize the executable
131 * as an ELF binary.
132 */
133 if (check(0, sizeof *ehdrp))
134 BADUNMAP;
135 ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0];
136
137 if (memcmp(ehdrp->e_ident, Elf_e_ident, Elf_e_siz))
138 BADUNMAP;
139
140 switch (ehdrp->e_machine) {
141 ELFDEFNNAME(MACHDEP_ID_CASES)
142
143 default:
144 BADUNMAP;
145 }
146
147 /*
148 * Find the symbol list and string table.
149 */
150 nshdr = ehdrp->e_shnum;
151 shdr_off = ehdrp->e_shoff;
152 shdr_size = ehdrp->e_shentsize * nshdr;
153
154 if (check(shdr_off, shdr_size) ||
155 (sizeof *shdrp != ehdrp->e_shentsize))
156 BADUNMAP;
157 shdrp = (Elf_Shdr *)(void *)&mappedfile[shdr_off];
158
159 for (i = 0; i < nshdr; i++) {
160 if (shdrp[i].sh_type == Elf_sht_symtab) {
161 symshdrp = &shdrp[i];
162 symstrshdrp = &shdrp[shdrp[i].sh_link];
163 }
164 }
165
166 /* Make sure we're not stripped. */
167 if (symshdrp == NULL || symshdrp->sh_offset == 0)
168 BADUNMAP;
169
170 /* Make sure the symbols and strings are safely mapped. */
171 if (check(symshdrp->sh_offset, symshdrp->sh_size))
172 BADUNMAP;
173 if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
174 BADUNMAP;
175
176 symp = (Elf_Sym *)(void *)&mappedfile[symshdrp->sh_offset];
177 nsyms = symshdrp->sh_size / sizeof(*symp);
178 strtab = &mappedfile[symstrshdrp->sh_offset];
179
180 /*
181 * Clean out any left-over information for all valid entries.
182 * Type and value are defined to be 0 if not found; historical
183 * versions cleared other and desc as well.
184 *
185 * XXX Clearing anything other than n_type and n_value violates
186 * the semantics given in the man page.
187 */
188 nent = 0;
189 for (p = list; !ISLAST(p); ++p) {
190 p->n_type = 0;
191 p->n_other = 0;
192 p->n_desc = 0;
193 p->n_value = 0;
194 ++nent;
195 }
196
197 for (i = 0; i < nsyms; i++) {
198 for (p = list; !ISLAST(p); ++p) {
199 const char *nlistname;
200 char *symtabname;
201
202 /* This may be incorrect */
203 nlistname = p->n_un.n_name;
204 if (*nlistname == '_')
205 nlistname++;
206
207 symtabname = &strtab[symp[i].st_name];
208
209 if (!strcmp(symtabname, nlistname)) {
210 /*
211 * Translate (roughly) from ELF to nlist
212 */
213 p->n_value = symp[i].st_value;
214 switch(ELF_SYM_TYPE(symp[i].st_info)) {
215 case Elf_estt_notype:
216 p->n_type = N_UNDF;
217 break;
218 case Elf_estt_object:
219 p->n_type = N_DATA;
220 break;
221 case Elf_estt_func:
222 p->n_type = N_TEXT;
223 break;
224 case Elf_estt_file:
225 p->n_type = N_FN;
226 break;
227 default:
228 /* catch other enumerations for gcc */
229 break;
230 }
231 if (ELF_SYM_BIND(symp[i].st_info) !=
232 Elf_estb_local)
233 p->n_type |= N_EXT;
234 p->n_desc = 0; /* XXX */
235 p->n_other = 0; /* XXX */
236
237 if (--nent <= 0)
238 goto done;
239 break; /* into next run of outer loop */
240 }
241 }
242 }
243
244 done:
245 rv = nent;
246 unmap:
247 munmap(mappedfile, mappedsize);
248 out:
249 return (rv);
250 }
251
252 #endif
253