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