1 /*        $NetBSD: kobj_rename.c,v 1.3 2020/02/20 22:52:10 joerg 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.3 2020/02/20 22:52:10 joerg 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           ".TOC.",
53 };
54 static int
norename(const char * name)55 norename(const char *name)
56 {
57           unsigned i;
58 
59           for (i = 0; i < __arraycount(norentab); i++) {
60                     if (strncmp(norentab[i], name, strlen(norentab[i])) == 0)
61                               return 1;
62           }
63           return 0;
64 }
65 
66 #define RUMPNS "rumpns_"
67 int
kobj_renamespace(Elf_Sym * symtab,size_t symcount,char ** strtab,size_t * strtabsz)68 kobj_renamespace(Elf_Sym *symtab, size_t symcount,
69           char **strtab, size_t *strtabsz)
70 {
71           Elf_Sym *sym;
72           char *worktab, *newtab;
73           size_t worktabsz, worktabidx;
74           unsigned i;
75           const size_t prefixlen = strlen(RUMPNS);
76           static int warned;
77 
78           if (!rump_nativeabi_p() && !warned) {
79                     printf("warning: kernel ABI not supported on this arch\n");
80                     warned = 1;
81           }
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