Home | History | Annotate | Line # | Download | only in rumpkern
kobj_rename.c revision 1.1.14.1
      1  1.1.14.1   yamt /*	$NetBSD: kobj_rename.c,v 1.1.14.1 2014/05/22 11:41:15 yamt Exp $	*/
      2       1.1  pooka 
      3       1.1  pooka /*-
      4       1.1  pooka  * Copyright (c) 2010 Antti Kantee.  All Rights Reserved.
      5       1.1  pooka  *
      6       1.1  pooka  * Redistribution and use in source and binary forms, with or without
      7       1.1  pooka  * modification, are permitted provided that the following conditions
      8       1.1  pooka  * are met:
      9       1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     10       1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     11       1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     12       1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     13       1.1  pooka  *    documentation and/or other materials provided with the distribution.
     14       1.1  pooka  *
     15       1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16       1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17       1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18       1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19       1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20       1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21       1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22       1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23       1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24       1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25       1.1  pooka  * SUCH DAMAGE.
     26       1.1  pooka  */
     27       1.1  pooka 
     28       1.1  pooka #include <sys/cdefs.h>
     29  1.1.14.1   yamt __KERNEL_RCSID(0, "$NetBSD: kobj_rename.c,v 1.1.14.1 2014/05/22 11:41:15 yamt Exp $");
     30       1.1  pooka 
     31       1.1  pooka #define ELFSIZE ARCH_ELFSIZE
     32       1.1  pooka 
     33       1.1  pooka #include <sys/param.h>
     34       1.1  pooka #include <sys/exec_elf.h>
     35       1.1  pooka #include <sys/kmem.h>
     36       1.1  pooka #include <sys/kobj.h>
     37       1.1  pooka #include <sys/systm.h>
     38       1.1  pooka 
     39  1.1.14.1   yamt #include <rump/rump.h>
     40  1.1.14.1   yamt 
     41       1.1  pooka /*
     42       1.1  pooka  * Mangle symbols into rump kernel namespace.  This means
     43       1.1  pooka  * putting "rumpns" in front of select symbols.
     44       1.1  pooka  * See src/sys/rump/Makefile.rump for more details on the rump kernel
     45       1.1  pooka  * namespace.
     46       1.1  pooka  */
     47       1.1  pooka const char *norentab[] = {
     48       1.1  pooka 	"RUMP",
     49       1.1  pooka 	"rump",
     50       1.1  pooka 	"__",
     51       1.1  pooka 	"_GLOBAL_OFFSET_TABLE",
     52       1.1  pooka };
     53       1.1  pooka static int
     54       1.1  pooka norename(const char *name)
     55       1.1  pooka {
     56       1.1  pooka 	unsigned i;
     57       1.1  pooka 
     58       1.1  pooka 	for (i = 0; i < __arraycount(norentab); i++) {
     59       1.1  pooka 		if (strncmp(norentab[i], name, strlen(norentab[i])) == 0)
     60       1.1  pooka 			return 1;
     61       1.1  pooka 	}
     62       1.1  pooka 	return 0;
     63       1.1  pooka }
     64       1.1  pooka 
     65       1.1  pooka #define RUMPNS "rumpns_"
     66       1.1  pooka int
     67       1.1  pooka kobj_renamespace(Elf_Sym *symtab, size_t symcount,
     68       1.1  pooka 	char **strtab, size_t *strtabsz)
     69       1.1  pooka {
     70       1.1  pooka 	Elf_Sym *sym;
     71       1.1  pooka 	char *worktab, *newtab;
     72       1.1  pooka 	size_t worktabsz, worktabidx;
     73       1.1  pooka 	unsigned i;
     74       1.1  pooka 	const size_t prefixlen = strlen(RUMPNS);
     75       1.1  pooka 	static int warned;
     76       1.1  pooka 
     77  1.1.14.1   yamt 	if (!rump_nativeabi_p() && !warned) {
     78       1.1  pooka 		printf("warning: kernel ABI not supported on this arch\n");
     79       1.1  pooka 		warned = 1;
     80       1.1  pooka 	}
     81       1.1  pooka 
     82       1.1  pooka 	/* allocate space for worst-case stringtab */
     83       1.1  pooka 	worktabsz = *strtabsz + symcount * prefixlen;
     84       1.1  pooka 	worktab = kmem_alloc(worktabsz, KM_SLEEP);
     85       1.1  pooka 
     86       1.1  pooka 	/* now, adjust stringtab into temporary space */
     87       1.1  pooka #define WORKTABP (worktab + worktabidx)
     88       1.1  pooka 	for (i = 0, worktabidx = 0; i < symcount; i++) {
     89       1.1  pooka 		const char *fromname;
     90       1.1  pooka 
     91       1.1  pooka 		sym = &symtab[i];
     92       1.1  pooka 		if (sym->st_name == 0) {
     93       1.1  pooka 			continue;
     94       1.1  pooka 		}
     95       1.1  pooka 
     96       1.1  pooka 		fromname = *strtab + sym->st_name;
     97       1.1  pooka 		sym->st_name = worktabidx;
     98       1.1  pooka 
     99       1.1  pooka 		if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL ||
    100       1.1  pooka 		    norename(fromname)) {
    101       1.1  pooka 			strcpy(WORKTABP, fromname);
    102       1.1  pooka 			worktabidx += strlen(fromname) + 1;
    103       1.1  pooka 		} else {
    104       1.1  pooka 			strcpy(WORKTABP, RUMPNS);
    105       1.1  pooka 			worktabidx += prefixlen;
    106       1.1  pooka 			strcpy(WORKTABP, fromname);
    107       1.1  pooka 			worktabidx += strlen(fromname) + 1;
    108       1.1  pooka 		}
    109       1.1  pooka 		KASSERT(worktabidx <= worktabsz);
    110       1.1  pooka 	}
    111       1.1  pooka #undef WORKTABP
    112       1.1  pooka 
    113       1.1  pooka 	/*
    114       1.1  pooka 	 * Finally, free old strtab, allocate new one, and copy contents.
    115       1.1  pooka 	 */
    116       1.1  pooka 	kmem_free(*strtab, *strtabsz);
    117       1.1  pooka 	*strtab = NULL; /* marvin the paradroid 999 */
    118       1.1  pooka 	newtab = kmem_alloc(worktabidx, KM_SLEEP);
    119       1.1  pooka 	memcpy(newtab, worktab, worktabidx);
    120       1.1  pooka 
    121       1.1  pooka 	kmem_free(worktab, worktabsz);
    122       1.1  pooka 
    123       1.1  pooka 	*strtab = newtab;
    124       1.1  pooka 	*strtabsz = worktabidx;
    125       1.1  pooka 
    126       1.1  pooka 	return 0;
    127       1.1  pooka }
    128