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