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