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