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