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