1 /* $NetBSD: kobj_machdep.c,v 1.11 2016/07/11 15:51:01 martin Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*-
30 * Copyright 1996-1998 John D. Polstra.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
44 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
45 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
46 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
51 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 */
53
54 #include <sys/cdefs.h>
55 __KERNEL_RCSID(0, "$NetBSD: kobj_machdep.c,v 1.11 2016/07/11 15:51:01 martin Exp $");
56
57 #define ELFSIZE ARCH_ELFSIZE
58
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/kobj.h>
62 #include <sys/exec.h>
63 #include <sys/exec_elf.h>
64 #include <sys/kmem.h>
65 #include <sys/ksyms.h>
66 #include <sys/kobj_impl.h>
67
68 #include <arm/cpufunc.h>
69 #include <arm/locore.h>
70
71 int
kobj_reloc(kobj_t ko,uintptr_t relocbase,const void * data,bool isrela,bool local)72 kobj_reloc(kobj_t ko, uintptr_t relocbase, const void *data,
73 bool isrela, bool local)
74 {
75 Elf_Addr *where;
76 Elf_Addr addr;
77 Elf_Addr addend;
78 Elf_Word rtype, symidx;
79 const Elf_Rel *rel;
80 const Elf_Rela *rela;
81
82 if (isrela) {
83 rela = (const Elf_Rela *)data;
84 where = (Elf_Addr *) (relocbase + rela->r_offset);
85 addend = rela->r_addend;
86 rtype = ELF_R_TYPE(rela->r_info);
87 symidx = ELF_R_SYM(rela->r_info);
88 } else {
89 rel = (const Elf_Rel *)data;
90 where = (Elf_Addr *) (relocbase + rel->r_offset);
91 addend = *where;
92 rtype = ELF_R_TYPE(rel->r_info);
93 symidx = ELF_R_SYM(rel->r_info);
94 }
95
96 switch (rtype) {
97 case R_ARM_NONE: /* none */
98 case R_ARM_V4BX: /* none */
99 return 0;
100
101 case R_ARM_ABS32:
102 addr = kobj_sym_lookup(ko, symidx);
103 if (addr == 0)
104 break;
105 *where = addr + addend;
106 return 0;
107
108 case R_ARM_COPY: /* none */
109 /* There shouldn't be copy relocations in kernel objects. */
110 break;
111
112 case R_ARM_JUMP_SLOT:
113 addr = kobj_sym_lookup(ko, symidx);
114 if (addr == 0)
115 break;
116 *where = addr;
117 return 0;
118
119 case R_ARM_RELATIVE: /* A + B */
120 addr = relocbase + addend;
121 if (*where != addr)
122 *where = addr;
123 return 0;
124
125 case R_ARM_MOVW_ABS_NC: /* (S + A) | T */
126 case R_ARM_MOVT_ABS:
127 if ((*where & 0x0fb00000) != 0x03000000)
128 break;
129 addr = kobj_sym_lookup(ko, symidx);
130 if (addr == 0)
131 break;
132 if (rtype == R_ARM_MOVT_ABS)
133 addr >>= 16;
134 *where = (*where & 0xfff0f000)
135 | ((addr << 4) & 0x000f0000) | (addr & 0x00000fff);
136 return 0;
137
138 case R_ARM_CALL: /* ((S + A) | T) - P */
139 case R_ARM_JUMP24:
140 case R_ARM_PC24: /* Deprecated */
141 if (local && (*where & 0x00ffffff) != 0x00fffffe)
142 return 0;
143
144 /* Remove the instruction from the 24 bit offset */
145 addend &= 0x00ffffff;
146
147 /* Sign extend if necessary */
148 if (addend & 0x00800000)
149 addend |= 0xff000000;
150
151 addend <<= 2;
152
153 addr = kobj_sym_lookup(ko, symidx);
154 if (addr == 0)
155 break;
156
157 addend += (uintptr_t)addr - (uintptr_t)where;
158
159 if (addend & 3) {
160 printf ("Relocation %x unaligned @ %p\n", addend, where);
161 return -1;
162 }
163
164 if ((addend & 0xfe000000) != 0x00000000 &&
165 (addend & 0xfe000000) != 0xfe000000) {
166 printf ("Relocation %x too far @ %p\n", addend, where);
167 return -1;
168 }
169 *where = (*where & 0xff000000) | ((addend >> 2) & 0x00ffffff);
170 return 0;
171
172 case R_ARM_REL32: /* ((S + A) | T) - P */
173 /* T = 0 for now */
174 addr = kobj_sym_lookup(ko, symidx);
175 if (addr == 0)
176 break;
177
178 addend += (uintptr_t)addr - (uintptr_t)where;
179 *where = addend;
180 return 0;
181
182 case R_ARM_PREL31: /* ((S + A) | T) - P */
183 /* Sign extend if necessary */
184 if (addend & 0x40000000)
185 addend |= 0xc0000000;
186 /* T = 0 for now */
187 addr = kobj_sym_lookup(ko, symidx);
188 if (addr == 0)
189 break;
190
191 addend += (uintptr_t)addr - (uintptr_t)where;
192
193 if ((addend & 0x80000000) != 0x00000000 &&
194 (addend & 0x80000000) != 0x80000000) {
195 printf ("Relocation %x too far @ %p\n", addend, where);
196 return -1;
197 }
198
199 *where = (*where & 0x80000000) | (addend & 0x7fffffff);
200
201 default:
202 break;
203 }
204
205 printf("kobj_reloc: unexpected/invalid relocation type %d @ %p symidx %u\n",
206 rtype, where, symidx);
207 return -1;
208 }
209
210 #if __ARMEB__
211
212 enum be8_magic_sym_type {
213 Other, ArmStart, ThumbStart, DataStart
214 };
215
216 struct be8_marker {
217 enum be8_magic_sym_type type;
218 void *addr;
219 };
220
221 struct be8_marker_list {
222 size_t cnt;
223 struct be8_marker *markers;
224 };
225
226 /*
227 * See ELF for the ARM Architecture, Section 4.5.5: Mapping Symbols
228 * ARM reserves $a/$d/$t (and variants like $a.2) to mark start of
229 * arm/thumb code sections to allow conversion from ARM32-EB to -BE8
230 * format.
231 */
232 static enum be8_magic_sym_type
be8_sym_type(const char * name,int info)233 be8_sym_type(const char *name, int info)
234 {
235 if (ELF_ST_BIND(info) != STB_LOCAL)
236 return Other;
237 if (ELF_ST_TYPE(info) != STT_NOTYPE)
238 return Other;
239 if (name[0] != '$' || name[1] == '\0' ||
240 (name[2] != '\0' && name[2] != '.'))
241 return Other;
242
243 switch (name[1]) {
244 case 'a':
245 return ArmStart;
246 case 'd':
247 return DataStart;
248 case 't':
249 return ThumbStart;
250 default:
251 return Other;
252 }
253 }
254
255 static int
be8_ksym_count(const char * name,int symindex,void * value,uint32_t size,int info,void * cookie)256 be8_ksym_count(const char *name, int symindex, void *value, uint32_t size,
257 int info, void *cookie)
258 {
259 size_t *res = cookie;
260 enum be8_magic_sym_type t = be8_sym_type(name, info);
261
262 if (t != Other)
263 (*res)++;
264 return 0;
265 }
266
267 static int
be8_ksym_add(const char * name,int symindex,void * value,uint32_t size,int info,void * cookie)268 be8_ksym_add(const char *name, int symindex, void *value, uint32_t size,
269 int info, void *cookie)
270 {
271 size_t ndx;
272 struct be8_marker_list *list = cookie;
273 enum be8_magic_sym_type t = be8_sym_type(name, info);
274
275 if (t == Other)
276 return 0;
277
278 ndx = list->cnt++;
279 list->markers[ndx].type = t;
280 list->markers[ndx].addr = value;
281
282 return 0;
283 }
284
285 static int
be8_ksym_comp(const void * a,const void * b)286 be8_ksym_comp(const void *a, const void *b)
287 {
288 const struct be8_marker *ma = a, *mb = b;
289 uintptr_t va = (uintptr_t)ma->addr, vb = (uintptr_t)mb->addr;
290
291 if (va == vb)
292 return 0;
293 if (va < vb)
294 return -1;
295 return 1;
296 }
297
298 static void
be8_ksym_swap(void * start,size_t size,const struct be8_marker_list * list)299 be8_ksym_swap(void *start, size_t size, const struct be8_marker_list *list)
300 {
301 uintptr_t va_end = (uintptr_t)start + size;
302 size_t i;
303 uint32_t *p32, *p32_end, v32;
304 uint16_t *p16, *p16_end, v16;
305
306 /* find first relevant list entry */
307 for (i = 0; i < list->cnt; i++)
308 if (start <= list->markers[i].addr)
309 break;
310
311 /* swap all arm and thumb code parts of this section */
312 for ( ; i < list->cnt; i++) {
313 switch (list->markers[i].type) {
314 case ArmStart:
315 p32 = (uint32_t*)list->markers[i].addr;
316 p32_end = (uint32_t*)va_end;
317 if (i+1 < list->cnt) {
318 if ((uintptr_t)list->markers[i+1].addr
319 < va_end)
320 p32_end = (uint32_t*)
321 list->markers[i+1].addr;
322 }
323 while (p32 < p32_end) {
324 v32 = bswap32(*p32);
325 *p32++ = v32;
326 }
327 break;
328 case ThumbStart:
329 p16 = (uint16_t*)list->markers[i].addr;
330 p16_end = (uint16_t*)va_end;
331 if (i+1 < list->cnt) {
332 if ((uintptr_t)list->markers[i+1].addr
333 < va_end)
334 p16_end = (uint16_t*)
335 list->markers[i+1].addr;
336 }
337 while (p16 < p16_end) {
338 v16 = bswap16(*p16);
339 *p16++ = v16;
340 }
341 break;
342 default:
343 break;
344 }
345 }
346 }
347
348 static void
kobj_be8_fixup(kobj_t ko)349 kobj_be8_fixup(kobj_t ko)
350 {
351 size_t relsym_cnt = 0, i, msize;
352 struct be8_marker_list list;
353 struct be8_marker tmp;
354
355 /*
356 * Count all special relocations symbols
357 */
358 ksyms_mod_foreach(ko->ko_name, be8_ksym_count, &relsym_cnt);
359
360 /*
361 * Provide storage for the address list and add the symbols
362 */
363 list.cnt = 0;
364 msize = relsym_cnt*sizeof(*list.markers);
365 list.markers = kmem_alloc(msize, KM_SLEEP);
366 ksyms_mod_foreach(ko->ko_name, be8_ksym_add, &list);
367 KASSERT(list.cnt == relsym_cnt);
368
369 /*
370 * Sort symbols by ascending address
371 */
372 if (kheapsort(list.markers, relsym_cnt, sizeof(*list.markers),
373 be8_ksym_comp, &tmp) != 0)
374 panic("could not sort be8 marker symbols");
375
376 /*
377 * Apply swaps to the .text section (XXX we do not have the
378 * section header available any more, it has been jetisoned
379 * already, so we can not check for all PROGBIT sections).
380 */
381 for (i = 0; i < ko->ko_nprogtab; i++) {
382 if (strcmp(ko->ko_progtab[i].name, ".text") != 0)
383 continue;
384 be8_ksym_swap(ko->ko_progtab[i].addr,
385 (size_t)ko->ko_progtab[i].size,
386 &list);
387 }
388
389 /*
390 * Done, free list
391 */
392 kmem_free(list.markers, msize);
393 }
394 #endif
395
396 int
kobj_machdep(kobj_t ko,void * base,size_t size,bool load)397 kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
398 {
399
400 if (load) {
401 #if __ARMEB__
402 if (CPU_IS_ARMV7_P() && base == (void*)ko->ko_text_address)
403 kobj_be8_fixup(ko);
404 #endif
405 #ifndef _RUMPKERNEL
406 cpu_idcache_wbinv_range((vaddr_t)base, size);
407 cpu_tlb_flushID();
408 #endif
409 }
410
411 return 0;
412 }
413