nlist_elf32.c revision 1.14 1 /* $NetBSD: nlist_elf32.c,v 1.14 2001/07/22 13:34:17 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the
18 * NetBSD Project. See http://www.netbsd.org/ for
19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35 */
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: nlist_elf32.c,v 1.14 2001/07/22 13:34:17 wiz Exp $");
40 #endif /* not lint */
41
42 /* If not included by nlist_elf64.c, ELFSIZE won't be defined. */
43 #ifndef ELFSIZE
44 #define ELFSIZE 32
45 #endif
46
47 #include <sys/param.h>
48 #include <sys/mman.h>
49 #include <sys/stat.h>
50
51 #include <a.out.h>
52 #include <db.h>
53 #include <err.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #include <kvm.h>
57 #include <limits.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include "extern.h"
64
65 #if defined(NLIST_ELF32) || defined(NLIST_ELF64)
66 #include <sys/exec_elf.h>
67 #endif
68
69 #if (defined(NLIST_ELF32) && (ELFSIZE == 32)) || \
70 (defined(NLIST_ELF64) && (ELFSIZE == 64))
71
72 typedef struct nlist NLIST;
73 #define _strx n_un.n_strx
74 #define _name n_un.n_name
75
76 #define badfmt(str) \
77 do { \
78 warnx("%s: %s: %s", kfile, str, strerror(EFTYPE)); \
79 punt(); \
80 } while (0)
81
82 #define check(off, size) ((off < 0) || (off + size > mappedsize))
83 #define BAD do { rv = -1; goto out; } while (0)
84 #define BADUNMAP do { rv = -1; goto unmap; } while (0)
85
86 static const char *kfile;
87
88 int
89 ELFNAMEEND(create_knlist)(name, db)
90 const char *name;
91 DB *db;
92 {
93 struct stat st;
94 struct nlist nbuf;
95 DBT key, data;
96 char *mappedfile, *symname, *fsymname, *tmpcp, *strtab;
97 size_t mappedsize, symnamesize, fsymnamesize;
98 Elf_Ehdr *ehdrp;
99 Elf_Shdr *shdrp, *symshdrp, *symstrshdrp;
100 Elf_Sym *symp;
101 Elf_Off shdr_off;
102 Elf_Word shdr_size;
103 #if (ELFSIZE == 32)
104 Elf32_Half nshdr;
105 #elif (ELFSIZE == 64)
106 Elf64_Half nshdr;
107 #endif
108 unsigned long i, nsyms;
109 int fd, rv;
110
111 rv = -1;
112 #ifdef __GNUC__
113 /* fix compiler warnings */
114 symshdrp = NULL;
115 symstrshdrp = NULL;
116 #endif
117
118 /*
119 * Open and map the whole file. If we can't open/stat it,
120 * something bad is going on so we punt.
121 */
122 kfile = name;
123 if ((fd = open(name, O_RDONLY, 0)) < 0) {
124 warn("%s", kfile);
125 punt();
126 }
127 if (fstat(fd, &st) < 0) {
128 warn("%s", kfile);
129 punt();
130 }
131 if (st.st_size > SIZE_T_MAX)
132 BAD;
133
134 /*
135 * Map the file in its entirety.
136 */
137 mappedsize = st.st_size;
138 mappedfile = mmap(NULL, mappedsize, PROT_READ, MAP_PRIVATE|MAP_FILE,
139 fd, 0);
140 if (mappedfile == (char *)-1)
141 BAD;
142
143 /*
144 * Make sure we can access the executable's header
145 * directly, and make sure the recognize the executable
146 * as an ELF binary.
147 */
148 if (check(0, sizeof *ehdrp))
149 BADUNMAP;
150 ehdrp = (Elf_Ehdr *)&mappedfile[0];
151
152 if (memcmp(ehdrp->e_ident, ELFMAG, SELFMAG) != 0 ||
153 ehdrp->e_ident[EI_CLASS] != ELFCLASS)
154 BADUNMAP;
155
156 switch (ehdrp->e_machine) {
157 ELFDEFNNAME(MACHDEP_ID_CASES)
158
159 default:
160 BADUNMAP;
161 }
162
163 /*
164 * We've recognized it as an ELF binary. From here
165 * on out, all errors are fatal.
166 */
167
168 /*
169 * Find the symbol list and string table.
170 */
171 nshdr = ehdrp->e_shnum;
172 shdr_off = ehdrp->e_shoff;
173 shdr_size = ehdrp->e_shentsize * nshdr;
174
175 if (check(shdr_off, shdr_size) ||
176 (sizeof *shdrp != ehdrp->e_shentsize))
177 badfmt("bogus section header table");
178 shdrp = (Elf_Shdr *)&mappedfile[shdr_off];
179
180 for (i = 0; i < nshdr; i++) {
181 if (shdrp[i].sh_type == SHT_SYMTAB) {
182 symshdrp = &shdrp[i];
183 symstrshdrp = &shdrp[shdrp[i].sh_link];
184 }
185 }
186
187 if (symshdrp == NULL)
188 badfmt("no symbol section header found");
189 if (symshdrp->sh_offset == 0)
190 badfmt("stripped");
191 if (check(symshdrp->sh_offset, symshdrp->sh_size))
192 badfmt("bogus symbol section header");
193 if (check(symstrshdrp->sh_offset, symstrshdrp->sh_size))
194 badfmt("bogus symbol string section header");
195
196 symp = (Elf_Sym *)&mappedfile[symshdrp->sh_offset];
197 nsyms = symshdrp->sh_size / sizeof(*symp);
198 strtab = &mappedfile[symstrshdrp->sh_offset];
199
200 /*
201 * Set up the data item, pointing to a nlist structure.
202 * which we fill in for each symbol.
203 */
204 data.data = (u_char *)&nbuf;
205 data.size = sizeof(nbuf);
206
207 /*
208 * Create a buffer (to be expanded later, if necessary)
209 * to hold symbol names after we've added underscores
210 * to them.
211 */
212 symnamesize = 1024;
213 if ((symname = malloc(symnamesize)) == NULL) {
214 warn("malloc");
215 punt();
216 }
217
218 /*
219 * Read each symbol and enter it into the database.
220 */
221 for (i = 0; i < nsyms; i++) {
222
223 /*
224 * No symbol name; ignore this symbol.
225 */
226 if (symp[i].st_name == 0)
227 continue;
228
229 /*
230 * Find symbol name, copy it (with added underscore) to
231 * temporary buffer, and prepare the database key for
232 * insertion.
233 */
234 fsymname = &strtab[symp[i].st_name];
235 fsymnamesize = strlen(fsymname) + 1;
236 while (symnamesize < fsymnamesize + 1) {
237 symnamesize *= 2;
238 if ((symname = realloc(symname, symnamesize)) == NULL) {
239 warn("malloc");
240 punt();
241 }
242 }
243 strcpy(symname, "_");
244 strcat(symname, fsymname);
245
246 key.data = symname;
247 key.size = strlen((char *)key.data);
248
249 /*
250 * Convert the symbol information into an nlist structure,
251 * as best we can.
252 */
253 nbuf.n_value = symp[i].st_value;
254 switch (ELFDEFNNAME(ST_TYPE)(symp[i].st_info)) {
255 default:
256 case STT_NOTYPE:
257 nbuf.n_type = N_UNDF;
258 break;
259 case STT_OBJECT:
260 nbuf.n_type = N_DATA;
261 break;
262 case STT_FUNC:
263 nbuf.n_type = N_TEXT;
264 break;
265 case STT_FILE:
266 nbuf.n_type = N_FN;
267 break;
268 }
269 if (ELFDEFNNAME(ST_BIND)(symp[i].st_info) != STB_LOCAL)
270 nbuf.n_type |= N_EXT;
271 nbuf.n_desc = 0; /* XXX */
272 nbuf.n_other = 0; /* XXX */
273
274 /*
275 * Enter the symbol into the database.
276 */
277 if (db->put(db, &key, &data, 0)) {
278 warn("record enter");
279 punt();
280 }
281
282 /*
283 * If it's the kernel version string, we've gotta keep
284 * some extra data around. Under a separate key,
285 * we enter the first line (i.e. up to the first newline,
286 * with the newline replaced by a NUL to terminate the
287 * entered string) of the version string.
288 */
289 if (strcmp((char *)key.data, VRS_SYM) == 0) {
290 key.data = (u_char *)VRS_KEY;
291 key.size = sizeof(VRS_KEY) - 1;
292 /* Find the version string, relative to its section */
293 data.data = strdup(&mappedfile[nbuf.n_value -
294 shdrp[symp[i].st_shndx].sh_addr +
295 shdrp[symp[i].st_shndx].sh_offset]);
296 /* assumes newline terminates version. */
297 if ((tmpcp = strchr(data.data, '\n')) != NULL)
298 *tmpcp = '\0';
299 data.size = strlen((char *)data.data);
300
301 if (db->put(db, &key, &data, 0)) {
302 warn("record enter");
303 punt();
304 }
305
306 /* free pointer created by strdup(). */
307 free(data.data);
308
309 /* Restore to original values */
310 data.data = (u_char *)&nbuf;
311 data.size = sizeof(nbuf);
312 }
313 }
314
315 rv = 0;
316
317 unmap:
318 munmap(mappedfile, mappedsize);
319 out:
320 return (rv);
321 }
322
323 #endif
324