rumpuser_dl.c revision 1.13 1 /* $NetBSD: rumpuser_dl.c,v 1.13 2013/03/08 19:04:27 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2009 Antti Kantee. 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 /*
29 * Load all module link sets and feed symbol table to the kernel.
30 * Called during rump bootstrap.
31 */
32
33 #include "rumpuser_port.h"
34
35 #if !defined(lint)
36 __RCSID("$NetBSD: rumpuser_dl.c,v 1.13 2013/03/08 19:04:27 pooka Exp $");
37 #endif /* !lint */
38
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <assert.h>
42
43 #include <dlfcn.h>
44 #include <elf.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51
52 #include <rump/rumpuser.h>
53
54 #if defined(__ELF__) && (defined(__NetBSD__) || defined(__FreeBSD__) \
55 || (defined(__sun__) && defined(__svr4__))) || defined(__linux__) \
56 || defined(__DragonFly__)
57 #include <link.h>
58
59 static size_t symtabsize = 0, strtabsize = 0;
60 static size_t symtaboff = 0, strtaboff = 0;
61 static uint8_t *symtab = NULL;
62 static char *strtab = NULL;
63 static unsigned char eident;
64
65 /* nb5 compat */
66 #ifndef Elf_Symindx
67 #define Elf_Symindx uint32_t
68 #endif
69
70 static void *
71 reservespace(void *store, size_t *storesize,
72 size_t storeoff, size_t required)
73 {
74 size_t chunk, newsize;
75
76 assert(storeoff <= *storesize);
77 chunk = *storesize - storeoff;
78
79 if (chunk >= required)
80 return store;
81
82 newsize = *storesize + ((size_t)required - chunk);
83 store = realloc(store, newsize);
84 if (store == NULL) {
85 return NULL;
86 }
87 *((uint8_t *)store + storeoff) = '\0';
88 *storesize = newsize;
89
90 return store;
91 }
92
93 /*
94 * Macros to make handling elf32/64 in the code a little saner.
95 */
96
97 #define DYNn_GETMEMBER(base, n, thevar, result) \
98 do { \
99 if (eident == ELFCLASS32) { \
100 const Elf32_Dyn *dyn = base; \
101 /*LINTED*/ \
102 result = dyn[n].thevar; \
103 } else { \
104 const Elf64_Dyn *dyn = base; \
105 /*LINTED*/ \
106 result = dyn[n].thevar; \
107 } \
108 } while (/*CONSTCOND*/0)
109
110 #define SYMn_GETMEMBER(base, n, thevar, result) \
111 do { \
112 if (eident == ELFCLASS32) { \
113 const Elf32_Sym *sym = base; \
114 /*LINTED*/ \
115 result = sym[n].thevar; \
116 } else { \
117 const Elf64_Sym *sym = base; \
118 /*LINTED*/ \
119 result = sym[n].thevar; \
120 } \
121 } while (/*CONSTCOND*/0)
122
123 #define SYMn_SETMEMBER(base, n, thevar, value) \
124 do { \
125 if (eident == ELFCLASS32) { \
126 Elf32_Sym *sym = base; \
127 /*LINTED*/ \
128 sym[n].thevar = value; \
129 } else { \
130 Elf64_Sym *sym = base; \
131 /*LINTED*/ \
132 sym[n].thevar = value; \
133 } \
134 } while (/*CONSTCOND*/0)
135
136 #define SYM_GETSIZE() ((eident==ELFCLASS32)?sizeof(Elf32_Sym):sizeof(Elf64_Sym))
137
138 /*
139 * On NetBSD, the dynamic section pointer values seem to be relative to
140 * the address the dso is mapped at. On Linux, they seem to contain
141 * the absolute address. I couldn't find anything definite from a quick
142 * read of the standard and therefore I will not go and figure beyond ifdef.
143 * On Solaris, the main object works differently ... uuuuh.
144 */
145 #if defined(__linux__)
146 #define adjptr(_map_, _ptr_) ((void *)(_ptr_))
147 #elif defined(__sun__)
148 #define adjptr(_map_, _ptr_) \
149 (mainmap_p(_map_) ? (void *)(_ptr_) : (void *)(_map_->l_addr + (_ptr_)))
150 #else
151 #define adjptr(_map_, _ptr_) ((void *)(_map_->l_addr + (_ptr_)))
152 #endif
153
154 static int
155 getsymbols(struct link_map *map)
156 {
157 char *str_base;
158 void *syms_base = NULL; /* XXXgcc */
159 size_t curstrsize;
160 const void *ed_base;
161 uint64_t ed_tag;
162 size_t cursymcount;
163 unsigned i;
164
165 if (map->l_addr) {
166 if (memcmp((void *)map->l_addr, ELFMAG, SELFMAG) != 0)
167 return ENOEXEC;
168 eident = *(unsigned char *)(map->l_addr + EI_CLASS);
169 if (eident != ELFCLASS32 && eident != ELFCLASS64)
170 return ENOEXEC;
171 }
172
173 /*
174 * ok, we probably have only the main object. instead of going
175 * to disk and reading the ehdr, just try to guess the size.
176 */
177 if (eident == 0) {
178 if (/*CONSTCOND*/sizeof(void *) == 4)
179 eident = ELFCLASS32;
180 else
181 eident = ELFCLASS64;
182 }
183
184 /*
185 * Find symtab and strtab and their sizes.
186 */
187 str_base = NULL;
188 curstrsize = 0;
189 cursymcount = 0;
190 ed_base = map->l_ld;
191 DYNn_GETMEMBER(ed_base, 0, d_tag, ed_tag);
192 for (i = 0; ed_tag != DT_NULL;) {
193 uintptr_t edptr;
194 size_t edval;
195 Elf_Symindx *hashtab;
196
197 switch (ed_tag) {
198 case DT_SYMTAB:
199 DYNn_GETMEMBER(ed_base, i, d_un.d_ptr, edptr);
200 syms_base = adjptr(map, edptr);
201 break;
202 case DT_STRTAB:
203 DYNn_GETMEMBER(ed_base, i, d_un.d_ptr, edptr);
204 str_base = adjptr(map, edptr);
205 break;
206 case DT_STRSZ:
207 DYNn_GETMEMBER(ed_base, i, d_un.d_val, edval);
208 curstrsize = edval;
209 break;
210 case DT_HASH:
211 DYNn_GETMEMBER(ed_base, i, d_un.d_ptr, edptr);
212 hashtab = (Elf_Symindx *)adjptr(map, edptr);
213 cursymcount = hashtab[1];
214 break;
215 #ifdef DT_GNU_HASH
216 /*
217 * DT_GNU_HASH is a bit more complicated than DT_HASH
218 * in this regard since apparently there is no field
219 * telling us the total symbol count. Instead, we look
220 * for the last valid hash bucket and add its chain lenght
221 * to the bucket's base index.
222 */
223 case DT_GNU_HASH: {
224 Elf32_Word nbuck, symndx, maskwords, maxchain = 0;
225 Elf32_Word *gnuhash, *buckets, *ptr;
226 int bi;
227
228 DYNn_GETMEMBER(ed_base, i, d_un.d_ptr, edptr);
229 gnuhash = (Elf32_Word *)adjptr(map, edptr);
230
231 nbuck = gnuhash[0];
232 symndx = gnuhash[1];
233 maskwords = gnuhash[2];
234
235 /*
236 * First, find the last valid bucket and grab its index
237 */
238 if (eident == ELFCLASS64)
239 maskwords *= 2; /* sizeof(*buckets) == 4 */
240 buckets = gnuhash + 4 + maskwords;
241 for (bi = nbuck-1; bi >= 0; bi--) {
242 if (buckets[bi] != 0) {
243 maxchain = buckets[bi];
244 break;
245 }
246 }
247 if (maxchain == 0 || maxchain < symndx)
248 break;
249
250 /*
251 * Then, traverse the last chain and count symbols.
252 */
253
254 cursymcount = maxchain;
255 ptr = buckets + nbuck + (maxchain - symndx);
256 do {
257 cursymcount++;
258 } while ((*ptr++ & 1) == 0);
259 }
260 break;
261 #endif
262 case DT_SYMENT:
263 DYNn_GETMEMBER(ed_base, i, d_un.d_val, edval);
264 assert(edval == SYM_GETSIZE());
265 break;
266 default:
267 break;
268 }
269 i++;
270 DYNn_GETMEMBER(ed_base, i, d_tag, ed_tag);
271 }
272
273 if (str_base == NULL || syms_base == NULL ||
274 curstrsize == 0 || cursymcount == 0) {
275 fprintf(stderr, "could not find strtab, symtab or their sizes "
276 "in %s\n", map->l_name);
277 return ENOEXEC;
278 }
279
280 /*
281 * Make sure we have enough space for the contents of the symbol
282 * and string tables we are currently processing. The total used
283 * space will be smaller due to undefined symbols we are not
284 * interested in.
285 */
286 symtab = reservespace(symtab, &symtabsize,
287 symtaboff, cursymcount * SYM_GETSIZE());
288 strtab = reservespace(strtab, &strtabsize, strtaboff, curstrsize);
289 if (symtab == NULL || strtab == NULL) {
290 fprintf(stderr, "failed to reserve memory");
291 return ENOMEM;
292 }
293
294 /* iterate over all symbols in current symtab */
295 for (i = 0; i < cursymcount; i++) {
296 const char *cursymname;
297 int shndx, name;
298 uintptr_t value;
299 void *csym;
300
301 SYMn_GETMEMBER(syms_base, i, st_shndx, shndx);
302 SYMn_GETMEMBER(syms_base, i, st_value, value);
303 if (shndx == SHN_UNDEF || value == 0)
304 continue;
305
306 /* get symbol name */
307 SYMn_GETMEMBER(syms_base, i, st_name, name);
308 cursymname = name + str_base;
309
310 /*
311 * Only accept symbols which are decidedly in
312 * the rump kernel namespace.
313 * XXX: quirks, but they wouldn't matter here
314 */
315 if (strncmp(cursymname, "rump", 4) != 0 &&
316 strncmp(cursymname, "RUMP", 4) != 0 &&
317 strncmp(cursymname, "__", 2) != 0) {
318 continue;
319 }
320
321 memcpy(symtab + symtaboff,
322 (const uint8_t *)syms_base + i*SYM_GETSIZE(),SYM_GETSIZE());
323
324 /*
325 * set name to point at new strtab, offset symbol value
326 * with lib base address.
327 */
328 csym = symtab + symtaboff;
329 SYMn_SETMEMBER(csym, 0, st_name, strtaboff);
330 SYMn_GETMEMBER(csym, 0, st_value, value);
331 SYMn_SETMEMBER(csym, 0, st_value,(intptr_t)(value+map->l_addr));
332 symtaboff += SYM_GETSIZE();
333
334 strcpy(strtab + strtaboff, cursymname);
335 strtaboff += strlen(cursymname)+1;
336 }
337
338 return 0;
339 }
340
341 static void
342 process_object(void *handle,
343 rump_modinit_fn domodinit, rump_compload_fn docompload)
344 {
345 const struct modinfo *const *mi_start, *const *mi_end;
346 struct rump_component *const *rc, *const *rc_end;
347
348 mi_start = dlsym(handle, "__start_link_set_modules");
349 mi_end = dlsym(handle, "__stop_link_set_modules");
350 if (mi_start && mi_end)
351 domodinit(mi_start, (size_t)(mi_end-mi_start));
352
353 rc = dlsym(handle, "__start_link_set_rump_components");
354 rc_end = dlsym(handle, "__stop_link_set_rump_components");
355 if (rc && rc_end) {
356 for (; rc < rc_end; rc++)
357 docompload(*rc);
358 assert(rc == rc_end);
359 }
360 }
361
362 /*
363 * Get the linkmap from the dynlinker. Try to load kernel modules
364 * from all objects in the linkmap.
365 */
366 void
367 rumpuser_dl_bootstrap(rump_modinit_fn domodinit,
368 rump_symload_fn symload, rump_compload_fn compload)
369 {
370 struct link_map *map, *origmap, *mainmap;
371 void *mainhandle;
372 int error;
373
374 mainhandle = dlopen(NULL, RTLD_NOW);
375 if (dlinfo(mainhandle, RTLD_DI_LINKMAP, &mainmap) == -1) {
376 fprintf(stderr, "warning: rumpuser module bootstrap "
377 "failed: %s\n", dlerror());
378 return;
379 }
380 origmap = mainmap;
381
382 /*
383 * Process last->first because that's the most probable
384 * order for dependencies
385 */
386 for (; origmap->l_next; origmap = origmap->l_next)
387 continue;
388
389 /*
390 * Build symbol table to hand to the rump kernel. Do this by
391 * iterating over all rump libraries and collecting symbol
392 * addresses and relocation info.
393 */
394 error = 0;
395 for (map = origmap; map && !error; map = map->l_prev) {
396 if (strstr(map->l_name, "librump") != NULL || map == mainmap)
397 error = getsymbols(map);
398 }
399
400 if (error == 0) {
401 void *trimmedsym, *trimmedstr;
402
403 /*
404 * Allocate optimum-sized memory for storing tables
405 * and feed to kernel. If memory allocation fails,
406 * just give the ones with extra context (although
407 * I'm pretty sure we'll die moments later due to
408 * memory running out).
409 */
410 if ((trimmedsym = malloc(symtaboff)) != NULL) {
411 memcpy(trimmedsym, symtab, symtaboff);
412 } else {
413 trimmedsym = symtab;
414 symtab = NULL;
415 }
416 if ((trimmedstr = malloc(strtaboff)) != NULL) {
417 memcpy(trimmedstr, strtab, strtaboff);
418 } else {
419 trimmedstr = strtab;
420 strtab = NULL;
421 }
422 symload(trimmedsym, symtaboff, trimmedstr, strtaboff);
423 }
424 free(symtab);
425 free(strtab);
426
427 /*
428 * Next, load modules and components.
429 *
430 * Simply loop through all objects, ones unrelated to rump kernels
431 * will not contain link_set_rump_components (well, not including
432 * "sabotage", but that needs to be solved at another level anyway).
433 */
434 for (map = origmap; map; map = map->l_prev) {
435 void *handle;
436
437 if (map == mainmap) {
438 handle = mainhandle;
439 } else {
440 handle = dlopen(map->l_name, RTLD_LAZY);
441 if (handle == NULL)
442 continue;
443 }
444 process_object(handle, domodinit, compload);
445 if (map != mainmap)
446 dlclose(handle);
447 }
448 }
449 #else
450 /*
451 * "default" implementation for platforms where we don't support
452 * dynamic linking. Assumes that all rump kernel components are
453 * statically linked with the local client. No need to handle modules
454 * since the module code does that all by itself.
455 */
456 void
457 rumpuser_dl_bootstrap(rump_modinit_fn domodinit,
458 rump_symload_fn symload, rump_compload_fn compload)
459 {
460 extern void *__start_link_set_rump_components;
461 extern void *__stop_link_set_rump_components;
462 void **rc = &__start_link_set_rump_components;
463 void **rc_end = &__stop_link_set_rump_components;
464
465 for (; rc < rc_end; rc++)
466 compload(*rc);
467 }
468 #endif
469
470 void *
471 rumpuser_dl_globalsym(const char *symname)
472 {
473
474 return dlsym(RTLD_DEFAULT, symname);
475 }
476