nlist_elf32.c revision 1.12 1 /* $NetBSD: nlist_elf32.c,v 1.12 1998/11/13 10:26:19 christos 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 <errno.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <a.out.h> /* for 'struct nlist' declaration */
49
50 #include "nlist_private.h"
51 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
52 #include <sys/exec_elf.h>
53 #endif
54
55 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
56 (defined(NLIST_ELF64) && (ELFSIZE == 64))
57
58 #define CONCAT(x,y) __CONCAT(x,y)
59 #define ELFNAME(x) CONCAT(elf,CONCAT(ELFSIZE,CONCAT(_,x)))
60 #define ELFNAME2(x,y) CONCAT(x,CONCAT(_elf,CONCAT(ELFSIZE,CONCAT(_,y))))
61 #define ELFNAMEEND(x) CONCAT(x,CONCAT(_elf,ELFSIZE))
62 #define ELFDEFNNAME(x) CONCAT(ELF,CONCAT(ELFSIZE,CONCAT(_,x)))
63
64 /* No need to check for off < 0 because it is unsigned */
65 #define check(off, size) (off + size > mappedsize)
66 #define BAD goto out
67 #define BADUNMAP goto unmap
68
69 int
70 ELFNAMEEND(__fdnlist)(fd, list)
71 int fd;
72 struct nlist *list;
73 {
74 struct stat st;
75 struct nlist *p;
76 char *mappedfile, *strtab;
77 size_t mappedsize;
78 Elf_Ehdr *ehdrp;
79 Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
80 Elf_Sym *symp;
81 Elf_Off shdr_off;
82 Elf_Word shdr_size;
83 #if (ELFSIZE == 32)
84 Elf32_Half nshdr;
85 #elif (ELFSIZE == 64)
86 Elf64_Half nshdr;
87 #endif
88 size_t i, nsyms;
89 int rv, nent;
90
91 rv = -1;
92
93 symshdrp = symstrshdrp = NULL;
94
95 /*
96 * If we can't fstat() the file, something bad is going on.
97 */
98 if (fstat(fd, &st) < 0)
99 BAD;
100
101 /*
102 * Map the file in its entirety.
103 */
104 if (st.st_size > SIZE_T_MAX) {
105 errno = EFBIG;
106 BAD;
107 }
108 mappedsize = (size_t)st.st_size;
109 mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_COPY|MAP_FILE,
110 fd, (off_t)0);
111 if (mappedfile == (char *)-1)
112 BAD;
113
114 /*
115 * Make sure we can access the executable's header
116 * directly, and make sure the recognize the executable
117 * as an ELF binary.
118 */
119 if (check(0, sizeof *ehdrp))
120 BADUNMAP;
121 ehdrp = (Elf_Ehdr *)(void *)&mappedfile[0];
122
123 if (memcmp(ehdrp->e_ident, Elf_e_ident, Elf_e_siz))
124 BADUNMAP;
125
126 switch (ehdrp->e_machine) {
127 ELFDEFNNAME(MACHDEP_ID_CASES)
128
129 default:
130 BADUNMAP;
131 }
132
133 /*
134 * Find the symbol list and string table.
135 */
136 nshdr = ehdrp->e_shnum;
137 shdr_off = ehdrp->e_shoff;
138 shdr_size = ehdrp->e_shentsize * nshdr;
139
140 if (check(shdr_off, shdr_size) ||
141 (sizeof *shdrp != ehdrp->e_shentsize))
142 BADUNMAP;
143 shdrp = (Elf_Shdr *)(void *)&mappedfile[shdr_off];
144
145 for (i = 0; i < nshdr; i++) {
146 if (shdrp[i].sh_type == Elf_sht_symtab) {
147 symshdrp = &shdrp[i];
148 symstrshdrp = &shdrp[shdrp[i].sh_link];
149 }
150 }
151
152 /* Make sure we're not stripped. */
153 if (symshdrp == NULL || symshdrp->sh_offset == 0)
154 BADUNMAP;
155
156 /* Make sure the symbols and strings are safely mapped. */
157 if (check(symshdrp->sh_offset, symshdrp->sh_size))
158 BADUNMAP;
159 if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
160 BADUNMAP;
161
162 symp = (Elf_Sym *)(void *)&mappedfile[symshdrp->sh_offset];
163 nsyms = symshdrp->sh_size / sizeof(*symp);
164 strtab = &mappedfile[symstrshdrp->sh_offset];
165
166 /*
167 * Clean out any left-over information for all valid entries.
168 * Type and value are defined to be 0 if not found; historical
169 * versions cleared other and desc as well.
170 *
171 * XXX Clearing anything other than n_type and n_value violates
172 * the semantics given in the man page.
173 */
174 nent = 0;
175 for (p = list; !ISLAST(p); ++p) {
176 p->n_type = 0;
177 p->n_other = 0;
178 p->n_desc = 0;
179 p->n_value = 0;
180 ++nent;
181 }
182
183 for (i = 0; i < nsyms; i++) {
184 for (p = list; !ISLAST(p); ++p) {
185 const char *nlistname;
186 char *symtabname;
187
188 /* This may be incorrect */
189 nlistname = p->n_un.n_name;
190 if (*nlistname == '_')
191 nlistname++;
192
193 symtabname = &strtab[symp[i].st_name];
194
195 if (!strcmp(symtabname, nlistname)) {
196 /*
197 * Translate (roughly) from ELF to nlist
198 */
199 p->n_value = symp[i].st_value;
200 switch(ELF_SYM_TYPE(symp[i].st_info)) {
201 case Elf_estt_notype:
202 p->n_type = N_UNDF;
203 break;
204 case Elf_estt_object:
205 p->n_type = N_DATA;
206 break;
207 case Elf_estt_func:
208 p->n_type = N_TEXT;
209 break;
210 case Elf_estt_file:
211 p->n_type = N_FN;
212 break;
213 default:
214 /* catch other enumerations for gcc */
215 break;
216 }
217 if (ELF_SYM_BIND(symp[i].st_info) !=
218 Elf_estb_local)
219 p->n_type |= N_EXT;
220 p->n_desc = 0; /* XXX */
221 p->n_other = 0; /* XXX */
222
223 if (--nent <= 0)
224 goto done;
225 break; /* into next run of outer loop */
226 }
227 }
228 }
229
230 done:
231 rv = nent;
232 unmap:
233 munmap(mappedfile, mappedsize);
234 out:
235 return (rv);
236 }
237
238 #endif
239