rumpuser_dl.c revision 1.10 1 /* $NetBSD: rumpuser_dl.c,v 1.10 2012/11/26 17:55:11 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.10 2012/11/26 17:55:11 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 <link.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include <rump/rumpuser.h>
54
55 #if defined(__ELF__) && (defined(__NetBSD__) || defined(__FreeBSD__) \
56 || (defined(__sun__) && defined(__svr4__))) || defined(__linux__) \
57 || defined(__DragonFly__)
58 static size_t symtabsize = 0, strtabsize = 0;
59 static size_t symtaboff = 0, strtaboff = 0;
60 static uint8_t *symtab = NULL;
61 static char *strtab = NULL;
62 static unsigned char eident;
63
64 /* nb5 compat */
65 #ifndef Elf_Symindx
66 #define Elf_Symindx uint32_t
67 #endif
68
69 /*
70 * Linux ld.so requires a valid handle for dlinfo(), so use the main
71 * handle. We initialize this variable in rumpuser_dl_bootstrap()
72 */
73 static void *mainhandle;
74
75 static void *
76 reservespace(void *store, size_t *storesize,
77 size_t storeoff, size_t required)
78 {
79 size_t chunk, newsize;
80
81 assert(storeoff <= *storesize);
82 chunk = *storesize - storeoff;
83
84 if (chunk >= required)
85 return store;
86
87 newsize = *storesize + ((size_t)required - chunk);
88 store = realloc(store, newsize);
89 if (store == NULL) {
90 return NULL;
91 }
92 *((uint8_t *)store + storeoff) = '\0';
93 *storesize = newsize;
94
95 return store;
96 }
97
98 /*
99 * Macros to make handling elf32/64 in the code a little saner.
100 */
101
102 #define DYNn_GETMEMBER(base, n, thevar, result) \
103 do { \
104 if (eident == ELFCLASS32) { \
105 const Elf32_Dyn *dyn = base; \
106 /*LINTED*/ \
107 result = dyn[n].thevar; \
108 } else { \
109 const Elf64_Dyn *dyn = base; \
110 /*LINTED*/ \
111 result = dyn[n].thevar; \
112 } \
113 } while (/*CONSTCOND*/0)
114
115 #define SYMn_GETMEMBER(base, n, thevar, result) \
116 do { \
117 if (eident == ELFCLASS32) { \
118 const Elf32_Sym *sym = base; \
119 /*LINTED*/ \
120 result = sym[n].thevar; \
121 } else { \
122 const Elf64_Sym *sym = base; \
123 /*LINTED*/ \
124 result = sym[n].thevar; \
125 } \
126 } while (/*CONSTCOND*/0)
127
128 #define SYMn_SETMEMBER(base, n, thevar, value) \
129 do { \
130 if (eident == ELFCLASS32) { \
131 Elf32_Sym *sym = base; \
132 /*LINTED*/ \
133 sym[n].thevar = value; \
134 } else { \
135 Elf64_Sym *sym = base; \
136 /*LINTED*/ \
137 sym[n].thevar = value; \
138 } \
139 } while (/*CONSTCOND*/0)
140
141 #define SYM_GETSIZE() ((eident==ELFCLASS32)?sizeof(Elf32_Sym):sizeof(Elf64_Sym))
142
143 /*
144 * On NetBSD, the dynamic section pointer values seem to be relative to
145 * the address the dso is mapped at. On Linux, they seem to contain
146 * the absolute address. I couldn't find anything definite from a quick
147 * read of the standard and therefore I will not go and figure beyond ifdef.
148 */
149 #ifdef __linux__
150 #define adjptr(_map_, _ptr_) ((void *)(_ptr_))
151 #else
152 #define adjptr(_map_, _ptr_) ((void *)(_map_->l_addr + (_ptr_)))
153 #endif
154
155 static int
156 getsymbols(struct link_map *map)
157 {
158 char *str_base;
159 void *syms_base = NULL; /* XXXgcc */
160 size_t curstrsize;
161 const void *ed_base;
162 uint64_t ed_tag;
163 size_t cursymcount;
164 unsigned i;
165
166 if (map->l_addr) {
167 if (memcmp((void *)map->l_addr, ELFMAG, SELFMAG) != 0)
168 return ENOEXEC;
169 eident = *(unsigned char *)(map->l_addr + EI_CLASS);
170 if (eident != ELFCLASS32 && eident != ELFCLASS64)
171 return ENOEXEC;
172 }
173
174 /*
175 * ok, we probably have only the main object. instead of going
176 * to disk and reading the ehdr, just try to guess the size.
177 */
178 if (eident == 0) {
179 if (/*CONSTCOND*/sizeof(void *) == 4)
180 eident = ELFCLASS32;
181 else
182 eident = ELFCLASS64;
183 }
184
185 /*
186 * Find symtab and strtab and their sizes.
187 */
188 str_base = NULL;
189 curstrsize = 0;
190 cursymcount = 0;
191 ed_base = map->l_ld;
192 DYNn_GETMEMBER(ed_base, 0, d_tag, ed_tag);
193 for (i = 0; ed_tag != DT_NULL;) {
194 uintptr_t edptr;
195 size_t edval;
196 Elf_Symindx *hashtab;
197
198 switch (ed_tag) {
199 case DT_SYMTAB:
200 DYNn_GETMEMBER(ed_base, i, d_un.d_ptr, edptr);
201 syms_base = adjptr(map, edptr);
202 break;
203 case DT_STRTAB:
204 DYNn_GETMEMBER(ed_base, i, d_un.d_ptr, edptr);
205 str_base = adjptr(map, edptr);
206 break;
207 case DT_STRSZ:
208 DYNn_GETMEMBER(ed_base, i, d_un.d_val, edval);
209 curstrsize = edval;
210 break;
211 case DT_HASH:
212 DYNn_GETMEMBER(ed_base, i, d_un.d_ptr, edptr);
213 hashtab = (Elf_Symindx *)adjptr(map, edptr);
214 cursymcount = hashtab[1];
215 break;
216 case DT_SYMENT:
217 DYNn_GETMEMBER(ed_base, i, d_un.d_val, edval);
218 assert(edval == SYM_GETSIZE());
219 break;
220 default:
221 break;
222 }
223 i++;
224 DYNn_GETMEMBER(ed_base, i, d_tag, ed_tag);
225 }
226
227 if (str_base == NULL || syms_base == NULL ||
228 curstrsize == 0 || cursymcount == 0) {
229 fprintf(stderr, "could not find strtab, symtab or their sizes "
230 "in %s\n", map->l_name);
231 return ENOEXEC;
232 }
233
234 /*
235 * Make sure we have enough space for the contents of the symbol
236 * and string tables we are currently processing. The total used
237 * space will be smaller due to undefined symbols we are not
238 * interested in.
239 */
240 symtab = reservespace(symtab, &symtabsize,
241 symtaboff, cursymcount * SYM_GETSIZE());
242 strtab = reservespace(strtab, &strtabsize, strtaboff, curstrsize);
243 if (symtab == NULL || strtab == NULL) {
244 fprintf(stderr, "failed to reserve memory");
245 return ENOMEM;
246 }
247
248 /* iterate over all symbols in current symtab */
249 for (i = 0; i < cursymcount; i++) {
250 const char *cursymname;
251 int shndx, name;
252 uintptr_t value;
253 void *csym;
254
255 SYMn_GETMEMBER(syms_base, i, st_shndx, shndx);
256 SYMn_GETMEMBER(syms_base, i, st_value, value);
257 if (shndx == SHN_UNDEF || value == 0)
258 continue;
259
260 /* get symbol name */
261 SYMn_GETMEMBER(syms_base, i, st_name, name);
262 cursymname = name + str_base;
263
264 /*
265 * Only accept symbols which are decidedly in
266 * the rump kernel namespace.
267 * XXX: quirks, but they wouldn't matter here
268 */
269 if (strncmp(cursymname, "rump", 4) != 0 &&
270 strncmp(cursymname, "RUMP", 4) != 0 &&
271 strncmp(cursymname, "__", 2) != 0) {
272 continue;
273 }
274
275 memcpy(symtab + symtaboff,
276 (const uint8_t *)syms_base + i*SYM_GETSIZE(),SYM_GETSIZE());
277
278 /*
279 * set name to point at new strtab, offset symbol value
280 * with lib base address.
281 */
282 csym = symtab + symtaboff;
283 SYMn_SETMEMBER(csym, 0, st_name, strtaboff);
284 SYMn_GETMEMBER(csym, 0, st_value, value);
285 SYMn_SETMEMBER(csym, 0, st_value,(intptr_t)(value+map->l_addr));
286 symtaboff += SYM_GETSIZE();
287
288 strcpy(strtab + strtaboff, cursymname);
289 strtaboff += strlen(cursymname)+1;
290 }
291
292 return 0;
293 }
294
295 static void
296 process(const char *soname, rump_modinit_fn domodinit)
297 {
298 void *handle;
299 const struct modinfo *const *mi_start, *const *mi_end;
300
301 if (strstr(soname, "librump") == NULL)
302 return;
303
304 handle = dlopen(soname, RTLD_LAZY);
305 if (handle == NULL)
306 return;
307
308 mi_start = dlsym(handle, "__start_link_set_modules");
309 if (!mi_start)
310 goto out;
311 mi_end = dlsym(handle, "__stop_link_set_modules");
312 if (!mi_end)
313 goto out;
314
315 domodinit(mi_start, (size_t)(mi_end-mi_start));
316
317 out:
318 dlclose(handle);
319 }
320
321 /*
322 * Get the linkmap from the dynlinker. Try to load kernel modules
323 * from all objects in the linkmap.
324 */
325 void
326 rumpuser_dl_bootstrap(rump_modinit_fn domodinit,
327 rump_symload_fn symload)
328 {
329 struct link_map *map, *origmap;
330 int error;
331
332 mainhandle = dlopen(NULL, RTLD_NOW);
333 if (dlinfo(mainhandle, RTLD_DI_LINKMAP, &origmap) == -1) {
334 fprintf(stderr, "warning: rumpuser module bootstrap "
335 "failed: %s\n", dlerror());
336 return;
337 }
338 /*
339 * Process last->first because that's the most probable
340 * order for dependencies
341 */
342 for (; origmap->l_next; origmap = origmap->l_next)
343 continue;
344
345 /*
346 * Build symbol table to hand to the rump kernel. Do this by
347 * iterating over all rump libraries and collecting symbol
348 * addresses and relocation info.
349 */
350 error = 0;
351 for (map = origmap; map && !error; map = map->l_prev) {
352 if (strstr(map->l_name, "librump") != NULL)
353 error = getsymbols(map);
354 /* this should be the main object */
355 else if (!map->l_addr && map->l_prev == NULL)
356 error = getsymbols(map);
357 }
358
359 if (error == 0) {
360 void *trimmedsym, *trimmedstr;
361
362 /*
363 * Allocate optimum-sized memory for storing tables
364 * and feed to kernel. If memory allocation fails,
365 * just give the ones with extra context (although
366 * I'm pretty sure we'll die moments later due to
367 * memory running out).
368 */
369 if ((trimmedsym = malloc(symtaboff)) != NULL) {
370 memcpy(trimmedsym, symtab, symtaboff);
371 } else {
372 trimmedsym = symtab;
373 symtab = NULL;
374 }
375 if ((trimmedstr = malloc(strtaboff)) != NULL) {
376 memcpy(trimmedstr, strtab, strtaboff);
377 } else {
378 trimmedstr = strtab;
379 strtab = NULL;
380 }
381 symload(trimmedsym, symtaboff, trimmedstr, strtaboff);
382 }
383 free(symtab);
384 free(strtab);
385
386 /*
387 * Next, load modules from dynlibs.
388 */
389 for (map = origmap; map; map = map->l_prev)
390 process(map->l_name, domodinit);
391 }
392
393 void
394 rumpuser_dl_component_init(int type, rump_component_init_fn compinit)
395 {
396 struct link_map *map;
397
398 if (dlinfo(mainhandle, RTLD_DI_LINKMAP, &map) == -1) {
399 fprintf(stderr, "warning: rumpuser module bootstrap "
400 "failed: %s\n", dlerror());
401 return;
402 }
403
404 for (; map->l_next; map = map->l_next)
405 continue;
406 for (; map; map = map->l_prev) {
407 if (strstr(map->l_name, "librump") != NULL) {
408 void *handle;
409 struct rump_component **rc, **rc_end;
410
411 handle = dlopen(map->l_name, RTLD_LAZY);
412 if (handle == NULL)
413 continue;
414
415 rc = dlsym(handle,
416 "__start_link_set_rump_components");
417 if (!rc)
418 goto loop;
419 rc_end = dlsym(handle,
420 "__stop_link_set_rump_components");
421 if (!rc_end)
422 goto loop;
423
424 for (; rc < rc_end; rc++)
425 compinit(*rc, type);
426 assert(rc == rc_end);
427 loop:
428 dlclose(handle);
429 }
430 }
431 }
432 #else
433 void
434 rumpuser_dl_bootstrap(rump_modinit_fn domodinit,
435 rump_symload_fn symload)
436 {
437
438 fprintf(stderr, "Warning, dlinfo() unsupported on host?\n");
439 }
440
441 void
442 rumpuser_dl_component_init(int type, rump_component_init_fn compinit)
443 {
444
445 fprintf(stderr, "Warning, dlinfo() unsupported on host?\n");
446 }
447 #endif
448
449 void *
450 rumpuser_dl_globalsym(const char *symname)
451 {
452
453 return dlsym(RTLD_DEFAULT, symname);
454 }
455