xref: /netbsd-src/sys/rump/librump/rumpkern/kobj_rename.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /*	$NetBSD: kobj_rename.c,v 1.2 2014/04/25 18:31:35 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 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 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: kobj_rename.c,v 1.2 2014/04/25 18:31:35 pooka Exp $");
30 
31 #define ELFSIZE ARCH_ELFSIZE
32 
33 #include <sys/param.h>
34 #include <sys/exec_elf.h>
35 #include <sys/kmem.h>
36 #include <sys/kobj.h>
37 #include <sys/systm.h>
38 
39 #include <rump/rump.h>
40 
41 /*
42  * Mangle symbols into rump kernel namespace.  This means
43  * putting "rumpns" in front of select symbols.
44  * See src/sys/rump/Makefile.rump for more details on the rump kernel
45  * namespace.
46  */
47 const char *norentab[] = {
48 	"RUMP",
49 	"rump",
50 	"__",
51 	"_GLOBAL_OFFSET_TABLE",
52 };
53 static int
54 norename(const char *name)
55 {
56 	unsigned i;
57 
58 	for (i = 0; i < __arraycount(norentab); i++) {
59 		if (strncmp(norentab[i], name, strlen(norentab[i])) == 0)
60 			return 1;
61 	}
62 	return 0;
63 }
64 
65 #define RUMPNS "rumpns_"
66 int
67 kobj_renamespace(Elf_Sym *symtab, size_t symcount,
68 	char **strtab, size_t *strtabsz)
69 {
70 	Elf_Sym *sym;
71 	char *worktab, *newtab;
72 	size_t worktabsz, worktabidx;
73 	unsigned i;
74 	const size_t prefixlen = strlen(RUMPNS);
75 	static int warned;
76 
77 	if (!rump_nativeabi_p() && !warned) {
78 		printf("warning: kernel ABI not supported on this arch\n");
79 		warned = 1;
80 	}
81 
82 	/* allocate space for worst-case stringtab */
83 	worktabsz = *strtabsz + symcount * prefixlen;
84 	worktab = kmem_alloc(worktabsz, KM_SLEEP);
85 
86 	/* now, adjust stringtab into temporary space */
87 #define WORKTABP (worktab + worktabidx)
88 	for (i = 0, worktabidx = 0; i < symcount; i++) {
89 		const char *fromname;
90 
91 		sym = &symtab[i];
92 		if (sym->st_name == 0) {
93 			continue;
94 		}
95 
96 		fromname = *strtab + sym->st_name;
97 		sym->st_name = worktabidx;
98 
99 		if (ELF_ST_BIND(sym->st_info) != STB_GLOBAL ||
100 		    norename(fromname)) {
101 			strcpy(WORKTABP, fromname);
102 			worktabidx += strlen(fromname) + 1;
103 		} else {
104 			strcpy(WORKTABP, RUMPNS);
105 			worktabidx += prefixlen;
106 			strcpy(WORKTABP, fromname);
107 			worktabidx += strlen(fromname) + 1;
108 		}
109 		KASSERT(worktabidx <= worktabsz);
110 	}
111 #undef WORKTABP
112 
113 	/*
114 	 * Finally, free old strtab, allocate new one, and copy contents.
115 	 */
116 	kmem_free(*strtab, *strtabsz);
117 	*strtab = NULL; /* marvin the paradroid 999 */
118 	newtab = kmem_alloc(worktabidx, KM_SLEEP);
119 	memcpy(newtab, worktab, worktabidx);
120 
121 	kmem_free(worktab, worktabsz);
122 
123 	*strtab = newtab;
124 	*strtabsz = worktabidx;
125 
126 	return 0;
127 }
128