kobj_machdep.c revision 1.13 1 /* $NetBSD: kobj_machdep.c,v 1.13 2020/06/20 07:01:16 skrll 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.13 2020/06/20 07:01:16 skrll 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
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 int error;
82
83 if (isrela) {
84 rela = (const Elf_Rela *)data;
85 where = (Elf_Addr *) (relocbase + rela->r_offset);
86 addend = rela->r_addend;
87 rtype = ELF_R_TYPE(rela->r_info);
88 symidx = ELF_R_SYM(rela->r_info);
89 } else {
90 rel = (const Elf_Rel *)data;
91 where = (Elf_Addr *) (relocbase + rel->r_offset);
92 addend = *where;
93 rtype = ELF_R_TYPE(rel->r_info);
94 symidx = ELF_R_SYM(rel->r_info);
95 }
96
97 switch (rtype) {
98 case R_ARM_NONE: /* none */
99 case R_ARM_V4BX: /* none */
100 return 0;
101
102 case R_ARM_ABS32:
103 error = kobj_sym_lookup(ko, symidx, &addr);
104 if (error)
105 break;
106 *where = addr + addend;
107 return 0;
108
109 case R_ARM_COPY: /* none */
110 /* There shouldn't be copy relocations in kernel objects. */
111 break;
112
113 case R_ARM_JUMP_SLOT:
114 error = kobj_sym_lookup(ko, symidx, &addr);
115 if (error)
116 break;
117 *where = addr;
118 return 0;
119
120 case R_ARM_RELATIVE: /* A + B */
121 addr = relocbase + addend;
122 if (*where != addr)
123 *where = addr;
124 return 0;
125
126 case R_ARM_MOVW_ABS_NC: /* (S + A) | T */
127 case R_ARM_MOVT_ABS:
128 if ((*where & 0x0fb00000) != 0x03000000)
129 break;
130 error = kobj_sym_lookup(ko, symidx, &addr);
131 if (error)
132 break;
133 if (rtype == R_ARM_MOVT_ABS)
134 addr >>= 16;
135 *where = (*where & 0xfff0f000)
136 | ((addr << 4) & 0x000f0000) | (addr & 0x00000fff);
137 return 0;
138
139 case R_ARM_CALL: /* ((S + A) | T) - P */
140 case R_ARM_JUMP24:
141 case R_ARM_PC24: /* Deprecated */
142 if (local && (*where & 0x00ffffff) != 0x00fffffe)
143 return 0;
144
145 /* Remove the instruction from the 24 bit offset */
146 addend &= 0x00ffffff;
147
148 /* Sign extend if necessary */
149 if (addend & 0x00800000)
150 addend |= 0xff000000;
151
152 addend <<= 2;
153
154 error = kobj_sym_lookup(ko, symidx, &addr);
155 if (error)
156 break;
157
158 addend += (uintptr_t)addr - (uintptr_t)where;
159
160 if (addend & 3) {
161 printf ("Relocation %x unaligned @ %p\n", addend, where);
162 return -1;
163 }
164
165 if ((addend & 0xfe000000) != 0x00000000 &&
166 (addend & 0xfe000000) != 0xfe000000) {
167 printf ("Relocation %x too far @ %p\n", addend, where);
168 return -1;
169 }
170 *where = (*where & 0xff000000) | ((addend >> 2) & 0x00ffffff);
171 return 0;
172
173 case R_ARM_REL32: /* ((S + A) | T) - P */
174 /* T = 0 for now */
175 error = kobj_sym_lookup(ko, symidx, &addr);
176 if (error)
177 break;
178
179 addend += (uintptr_t)addr - (uintptr_t)where;
180 *where = addend;
181 return 0;
182
183 case R_ARM_PREL31: /* ((S + A) | T) - P */
184 /* Sign extend if necessary */
185 if (addend & 0x40000000)
186 addend |= 0xc0000000;
187 /* T = 0 for now */
188 error = kobj_sym_lookup(ko, symidx, &addr);
189 if (error)
190 break;
191
192 addend += (uintptr_t)addr - (uintptr_t)where;
193
194 if ((addend & 0x80000000) != 0x00000000 &&
195 (addend & 0x80000000) != 0x80000000) {
196 printf ("Relocation %x too far @ %p\n", addend, where);
197 return -1;
198 }
199
200 *where = (*where & 0x80000000) | (addend & 0x7fffffff);
201
202 default:
203 break;
204 }
205
206 printf("kobj_reloc: unexpected/invalid relocation type %d @ %p symidx %u\n",
207 rtype, where, symidx);
208 return -1;
209 }
210
211 #if __ARMEB__
212
213 enum be8_magic_sym_type {
214 Other, ArmStart, ThumbStart, DataStart
215 };
216
217 struct be8_marker {
218 enum be8_magic_sym_type type;
219 void *addr;
220 };
221
222 struct be8_marker_list {
223 size_t cnt;
224 struct be8_marker *markers;
225 };
226
227 /*
228 * See ELF for the ARM Architecture, Section 4.5.5: Mapping Symbols
229 * ARM reserves $a/$d/$t (and variants like $a.2) to mark start of
230 * arm/thumb code sections to allow conversion from ARM32-EB to -BE8
231 * format.
232 */
233 static enum be8_magic_sym_type
234 be8_sym_type(const char *name, int info)
235 {
236 if (ELF_ST_BIND(info) != STB_LOCAL)
237 return Other;
238 if (ELF_ST_TYPE(info) != STT_NOTYPE)
239 return Other;
240 if (name[0] != '$' || name[1] == '\0' ||
241 (name[2] != '\0' && name[2] != '.'))
242 return Other;
243
244 switch (name[1]) {
245 case 'a':
246 return ArmStart;
247 case 'd':
248 return DataStart;
249 case 't':
250 return ThumbStart;
251 default:
252 return Other;
253 }
254 }
255
256 static int
257 be8_ksym_count(const char *name, int symindex, void *value, uint32_t size,
258 int info, void *cookie)
259 {
260 size_t *res = cookie;
261 enum be8_magic_sym_type t = be8_sym_type(name, info);
262
263 if (t != Other)
264 (*res)++;
265 return 0;
266 }
267
268 static int
269 be8_ksym_add(const char *name, int symindex, void *value, uint32_t size,
270 int info, void *cookie)
271 {
272 size_t ndx;
273 struct be8_marker_list *list = cookie;
274 enum be8_magic_sym_type t = be8_sym_type(name, info);
275
276 if (t == Other)
277 return 0;
278
279 ndx = list->cnt++;
280 list->markers[ndx].type = t;
281 list->markers[ndx].addr = value;
282
283 return 0;
284 }
285
286 static int
287 be8_ksym_comp(const void *a, const void *b)
288 {
289 const struct be8_marker *ma = a, *mb = b;
290 uintptr_t va = (uintptr_t)ma->addr, vb = (uintptr_t)mb->addr;
291
292 if (va == vb)
293 return 0;
294 if (va < vb)
295 return -1;
296 return 1;
297 }
298
299 static void
300 be8_ksym_swap(void *start, size_t size, const struct be8_marker_list *list)
301 {
302 uintptr_t va_end = (uintptr_t)start + size;
303 size_t i;
304 uint32_t *p32, *p32_end, v32;
305 uint16_t *p16, *p16_end, v16;
306
307 /* find first relevant list entry */
308 for (i = 0; i < list->cnt; i++)
309 if (start <= list->markers[i].addr)
310 break;
311
312 /* swap all arm and thumb code parts of this section */
313 for ( ; i < list->cnt; i++) {
314 switch (list->markers[i].type) {
315 case ArmStart:
316 p32 = (uint32_t*)list->markers[i].addr;
317 p32_end = (uint32_t*)va_end;
318 if (i+1 < list->cnt) {
319 if ((uintptr_t)list->markers[i+1].addr
320 < va_end)
321 p32_end = (uint32_t*)
322 list->markers[i+1].addr;
323 }
324 while (p32 < p32_end) {
325 v32 = bswap32(*p32);
326 *p32++ = v32;
327 }
328 break;
329 case ThumbStart:
330 p16 = (uint16_t*)list->markers[i].addr;
331 p16_end = (uint16_t*)va_end;
332 if (i+1 < list->cnt) {
333 if ((uintptr_t)list->markers[i+1].addr
334 < va_end)
335 p16_end = (uint16_t*)
336 list->markers[i+1].addr;
337 }
338 while (p16 < p16_end) {
339 v16 = bswap16(*p16);
340 *p16++ = v16;
341 }
342 break;
343 default:
344 break;
345 }
346 }
347 }
348
349 static void
350 kobj_be8_fixup(kobj_t ko)
351 {
352 size_t relsym_cnt = 0, i, msize;
353 struct be8_marker_list list;
354 struct be8_marker tmp;
355
356 /*
357 * Count all special relocations symbols
358 */
359 ksyms_mod_foreach(ko->ko_name, be8_ksym_count, &relsym_cnt);
360
361 /*
362 * Provide storage for the address list and add the symbols
363 */
364 list.cnt = 0;
365 msize = relsym_cnt*sizeof(*list.markers);
366 list.markers = kmem_alloc(msize, KM_SLEEP);
367 ksyms_mod_foreach(ko->ko_name, be8_ksym_add, &list);
368 KASSERT(list.cnt == relsym_cnt);
369
370 /*
371 * Sort symbols by ascending address
372 */
373 if (kheapsort(list.markers, relsym_cnt, sizeof(*list.markers),
374 be8_ksym_comp, &tmp) != 0)
375 panic("could not sort be8 marker symbols");
376
377 /*
378 * Apply swaps to the .text section (XXX we do not have the
379 * section header available any more, it has been jetisoned
380 * already, so we can not check for all PROGBIT sections).
381 */
382 for (i = 0; i < ko->ko_nprogtab; i++) {
383 if (strcmp(ko->ko_progtab[i].name, ".text") != 0)
384 continue;
385 be8_ksym_swap(ko->ko_progtab[i].addr,
386 (size_t)ko->ko_progtab[i].size,
387 &list);
388 }
389
390 /*
391 * Done, free list
392 */
393 kmem_free(list.markers, msize);
394 }
395 #endif
396
397 int
398 kobj_machdep(kobj_t ko, void *base, size_t size, bool load)
399 {
400
401 if (load) {
402 #if __ARMEB__
403 if (CPU_IS_ARMV7_P() && base == (void*)ko->ko_text_address)
404 kobj_be8_fixup(ko);
405 #endif
406 #ifndef _RUMPKERNEL
407 cpu_idcache_wbinv_range((vaddr_t)base, size);
408 cpu_tlb_flushID();
409 #endif
410 }
411
412 return 0;
413 }
414