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