1 | /* |
---|
2 | * Taken from NetBSD and stripped of the relocations not needed on RTEMS. |
---|
3 | */ |
---|
4 | |
---|
5 | /* $NetBSD: mdreloc.c,v 1.33 2010/01/14 12:12:07 skrll Exp $ */ |
---|
6 | |
---|
7 | #include <sys/cdefs.h> |
---|
8 | |
---|
9 | #include <errno.h> |
---|
10 | #include <inttypes.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include <string.h> |
---|
13 | #include <sys/types.h> |
---|
14 | #include <sys/stat.h> |
---|
15 | #include <unwind.h> |
---|
16 | #include <unwind-arm-common.h> |
---|
17 | |
---|
18 | #include <rtems/rtl/rtl.h> |
---|
19 | #include "rtl-elf.h" |
---|
20 | #include "rtl-error.h" |
---|
21 | #include <rtems/rtl/rtl-trace.h> |
---|
22 | #include "rtl-unwind.h" |
---|
23 | |
---|
24 | /* |
---|
25 | * Set to 1 to allow untested relocations. If you tested one and it |
---|
26 | * works or you fixed the relocation please remove the guard. |
---|
27 | */ |
---|
28 | #define ALLOW_UNTESTED_RELOCS 1 |
---|
29 | |
---|
30 | /* |
---|
31 | * It is possible for the compiler to emit relocations for unaligned data. |
---|
32 | * We handle this situation with these inlines. |
---|
33 | */ |
---|
34 | #define RELOC_ALIGNED_P(x) \ |
---|
35 | (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0) |
---|
36 | |
---|
37 | #define SHT_ARM_EXIDX 0x70000001 /* Section holds ARM unwind info. */ |
---|
38 | |
---|
39 | static inline Elf_Addr |
---|
40 | load_ptr(void *where) |
---|
41 | { |
---|
42 | Elf_Addr res; |
---|
43 | |
---|
44 | memcpy(&res, where, sizeof(res)); |
---|
45 | |
---|
46 | return (res); |
---|
47 | } |
---|
48 | |
---|
49 | static inline void |
---|
50 | store_ptr(void *where, Elf_Addr val) |
---|
51 | { |
---|
52 | memcpy(where, &val, sizeof(val)); |
---|
53 | } |
---|
54 | |
---|
55 | /* |
---|
56 | * The address of Thumb function symbols is it's real address plus one. |
---|
57 | * This is done by compiler, thus do not consider symtype here. |
---|
58 | */ |
---|
59 | static inline int |
---|
60 | isThumb(Elf_Word symvalue) |
---|
61 | { |
---|
62 | if ((symvalue & 0x1) == 0x1) |
---|
63 | return true; |
---|
64 | else return false; |
---|
65 | } |
---|
66 | |
---|
67 | static inline Elf_SOff |
---|
68 | sign_extend31(Elf_Addr val) |
---|
69 | { |
---|
70 | if (0x40000000 & val) |
---|
71 | val = ~((Elf_Addr)0x7fffffff) | (0x7fffffff & val); |
---|
72 | return 0x7fffffff & val; |
---|
73 | } |
---|
74 | |
---|
75 | static void* |
---|
76 | set_veneer(void* tramopline, Elf_Addr target) |
---|
77 | { |
---|
78 | /* |
---|
79 | * http://shell-storm.org/online/Online-Assembler-and-Disassembler/ |
---|
80 | * |
---|
81 | * Thumb mode: |
---|
82 | * ldr.w pc, [pc] |
---|
83 | * |
---|
84 | * ARM mode: |
---|
85 | * ldr pc, [pc, #-4] |
---|
86 | * |
---|
87 | */ |
---|
88 | uint32_t* tramp = (uint32_t*) tramopline; |
---|
89 | #if defined(__thumb__) |
---|
90 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
---|
91 | *tramp++ = 0xf000f8df; |
---|
92 | #else |
---|
93 | *tramp++ = 0xf8dff000; /* not tested */ |
---|
94 | #endif |
---|
95 | #else |
---|
96 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
---|
97 | *tramp++ = 0xe51ff004; |
---|
98 | #else |
---|
99 | *tramp++ = 0xe51ff004; /* not tested */ |
---|
100 | #endif |
---|
101 | #endif |
---|
102 | *tramp++ = (uint32_t) target; |
---|
103 | return tramp; |
---|
104 | } |
---|
105 | |
---|
106 | static size_t |
---|
107 | get_veneer_size(int type) |
---|
108 | { |
---|
109 | (void) type; |
---|
110 | return 8; |
---|
111 | } |
---|
112 | |
---|
113 | size_t |
---|
114 | rtems_rtl_elf_relocate_tramp_max_size (void) |
---|
115 | { |
---|
116 | return 8; |
---|
117 | } |
---|
118 | |
---|
119 | uint32_t |
---|
120 | rtems_rtl_elf_section_flags (const rtems_rtl_obj* obj, |
---|
121 | const Elf_Shdr* shdr) |
---|
122 | { |
---|
123 | uint32_t flags = 0; |
---|
124 | if (shdr->sh_type == SHT_ARM_EXIDX) |
---|
125 | flags = RTEMS_RTL_OBJ_SECT_EH | RTEMS_RTL_OBJ_SECT_LOAD; |
---|
126 | return flags; |
---|
127 | } |
---|
128 | |
---|
129 | uint32_t |
---|
130 | rtems_rtl_elf_arch_parse_section (const rtems_rtl_obj* obj, |
---|
131 | int section, |
---|
132 | const char* name, |
---|
133 | const Elf_Shdr* shdr, |
---|
134 | const uint32_t flags) |
---|
135 | { |
---|
136 | (void) obj; |
---|
137 | (void) section; |
---|
138 | (void) name; |
---|
139 | (void) shdr; |
---|
140 | return flags; |
---|
141 | } |
---|
142 | |
---|
143 | bool |
---|
144 | rtems_rtl_elf_arch_section_alloc (const rtems_rtl_obj* obj, |
---|
145 | rtems_rtl_obj_sect* sect) |
---|
146 | { |
---|
147 | (void) obj; |
---|
148 | (void) sect; |
---|
149 | return false; |
---|
150 | } |
---|
151 | |
---|
152 | bool |
---|
153 | rtems_rtl_elf_arch_section_free (const rtems_rtl_obj* obj, |
---|
154 | rtems_rtl_obj_sect* sect) |
---|
155 | { |
---|
156 | (void) obj; |
---|
157 | (void) sect; |
---|
158 | return false; |
---|
159 | } |
---|
160 | |
---|
161 | bool |
---|
162 | rtems_rtl_elf_rel_resolve_sym (Elf_Word type) |
---|
163 | { |
---|
164 | return true; |
---|
165 | } |
---|
166 | |
---|
167 | rtems_rtl_elf_rel_status |
---|
168 | rtems_rtl_elf_relocate_rela_tramp (rtems_rtl_obj* obj, |
---|
169 | const Elf_Rela* rela, |
---|
170 | const rtems_rtl_obj_sect* sect, |
---|
171 | const char* symname, |
---|
172 | const Elf_Byte syminfo, |
---|
173 | const Elf_Word symvalue) |
---|
174 | { |
---|
175 | (void) obj; |
---|
176 | (void) rela; |
---|
177 | (void) sect; |
---|
178 | (void) symname; |
---|
179 | (void) syminfo; |
---|
180 | (void) symvalue; |
---|
181 | rtems_rtl_set_error (EINVAL, "rela type record not supported"); |
---|
182 | return rtems_rtl_elf_rel_failure; |
---|
183 | } |
---|
184 | |
---|
185 | rtems_rtl_elf_rel_status |
---|
186 | rtems_rtl_elf_relocate_rela (rtems_rtl_obj* obj, |
---|
187 | const Elf_Rela* rela, |
---|
188 | const rtems_rtl_obj_sect* sect, |
---|
189 | const char* symname, |
---|
190 | const Elf_Byte syminfo, |
---|
191 | const Elf_Word symvalue) |
---|
192 | { |
---|
193 | (void) obj; |
---|
194 | (void) rela; |
---|
195 | (void) sect; |
---|
196 | (void) symname; |
---|
197 | (void) syminfo; |
---|
198 | (void) symvalue; |
---|
199 | rtems_rtl_set_error (EINVAL, "rela type record not supported"); |
---|
200 | return rtems_rtl_elf_rel_failure; |
---|
201 | } |
---|
202 | |
---|
203 | static rtems_rtl_elf_rel_status |
---|
204 | rtems_rtl_elf_reloc_rel (rtems_rtl_obj* obj, |
---|
205 | const Elf_Rel* rel, |
---|
206 | const rtems_rtl_obj_sect* sect, |
---|
207 | const char* symname, |
---|
208 | const Elf_Byte syminfo, |
---|
209 | const Elf_Word symvalue, |
---|
210 | const bool parsing) |
---|
211 | { |
---|
212 | Elf_Addr *where; |
---|
213 | Elf_Addr tmp; |
---|
214 | Elf_Word insn, addend; |
---|
215 | Elf_Word sign, i1, i2; |
---|
216 | uint16_t lower_insn, upper_insn; |
---|
217 | |
---|
218 | where = (Elf_Addr *)(sect->base + rel->r_offset); |
---|
219 | |
---|
220 | switch (ELF_R_TYPE(rel->r_info)) { |
---|
221 | case R_TYPE(NONE): |
---|
222 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) { |
---|
223 | printf ("rtl: NONE %p in %s\n", where, rtems_rtl_obj_oname (obj)); |
---|
224 | } |
---|
225 | break; |
---|
226 | |
---|
227 | case R_TYPE(CALL): /* BL/BLX */ |
---|
228 | case R_TYPE(JUMP24): /* B/BL<cond> */ |
---|
229 | if (parsing) |
---|
230 | { |
---|
231 | addend = 0; |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | insn = *where; |
---|
236 | |
---|
237 | if (insn & 0x00800000) |
---|
238 | addend = insn | 0xff000000; |
---|
239 | else addend = insn & 0x00ffffff; |
---|
240 | |
---|
241 | if (isThumb(symvalue)) { |
---|
242 | if ((insn & 0xfe000000) == 0xfa000000); /* Already blx */ |
---|
243 | else { |
---|
244 | if ((insn & 0xff000000) == 0xeb000000) { /* BL <label> */ |
---|
245 | *where = (insn & 0x00ffffff) | 0xfa000000; /* BL-->BLX */ |
---|
246 | } else { |
---|
247 | printf("JUMP24 is not suppored from arm to thumb\n"); |
---|
248 | return rtems_rtl_elf_rel_failure; |
---|
249 | } |
---|
250 | } |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | if (parsing && sect->base == 0) { |
---|
255 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
256 | printf ("rtl: JUMP24/PC24/CALL tramp cache\n"); |
---|
257 | return rtems_rtl_elf_rel_tramp_cache; |
---|
258 | } |
---|
259 | |
---|
260 | tmp = symvalue + (addend << 2) - (Elf_Addr)where; |
---|
261 | tmp = (Elf_Sword)tmp >> 2; |
---|
262 | |
---|
263 | if (((Elf_Sword)tmp > 0x7fffff) || ((Elf_Sword)tmp < -0x800000)) { |
---|
264 | Elf_Word tramp_addr; |
---|
265 | size_t tramp_size = get_veneer_size(ELF_R_TYPE(rel->r_info)); |
---|
266 | |
---|
267 | if (parsing) { |
---|
268 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
269 | printf ("rtl: JUMP24/PC24/CALL tramp add\n"); |
---|
270 | return rtems_rtl_elf_rel_tramp_add; |
---|
271 | } |
---|
272 | |
---|
273 | if (!rtems_rtl_obj_has_tramp_space (obj, tramp_size)) { |
---|
274 | rtems_rtl_set_error (EINVAL, |
---|
275 | "%s: CALL/JUMP24: overflow: no tramp memory", |
---|
276 | sect->name); |
---|
277 | return rtems_rtl_elf_rel_failure; |
---|
278 | } |
---|
279 | |
---|
280 | tramp_addr = ((Elf_Addr) obj->tramp_brk) | (symvalue & 1); |
---|
281 | obj->tramp_brk = set_veneer(obj->tramp_brk, symvalue); |
---|
282 | |
---|
283 | tmp = tramp_addr + (addend << 2) - (Elf_Addr)where; |
---|
284 | tmp = (Elf_Sword)tmp >> 2; |
---|
285 | } |
---|
286 | |
---|
287 | if (!parsing) { |
---|
288 | *where = (*where & 0xff000000) | (tmp & 0xffffff); |
---|
289 | |
---|
290 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
291 | printf ("rtl: JUMP24/PC24/CALL %p @ %p in %s\n", |
---|
292 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
293 | } |
---|
294 | break; |
---|
295 | |
---|
296 | case R_TYPE(V4BX): |
---|
297 | /* Miscellaneous, ignore */ |
---|
298 | if (!parsing && rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) { |
---|
299 | printf ("rtl: V4BX @ %p in %s\n", |
---|
300 | where, rtems_rtl_obj_oname (obj)); |
---|
301 | } |
---|
302 | break; |
---|
303 | |
---|
304 | case R_TYPE(MOVT_ABS): |
---|
305 | case R_TYPE(MOVW_ABS_NC): |
---|
306 | if (!parsing) { |
---|
307 | insn = *where; |
---|
308 | |
---|
309 | addend = ((insn >> 4) & 0xf000) | (insn & 0x0fff); |
---|
310 | if (addend & 0x8000) |
---|
311 | addend |= 0xffff0000; |
---|
312 | |
---|
313 | tmp = symvalue + addend; |
---|
314 | |
---|
315 | if (ELF_R_TYPE(rel->r_info) == R_TYPE(MOVW_ABS_NC)) |
---|
316 | tmp &= 0xffff; |
---|
317 | else { |
---|
318 | tmp = (Elf_Sword)tmp >> 16; |
---|
319 | if (((Elf_Sword)tmp > 0x7fff) || ((Elf_Sword)tmp < -0x8000)) { |
---|
320 | printf("MOVT_ABS Overflow\n"); |
---|
321 | return rtems_rtl_elf_rel_failure; |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | *where = (insn & 0xfff0f000) | ((tmp & 0xf000) << 4) | (tmp & 0xfff); |
---|
326 | |
---|
327 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
328 | printf ("rtl: MOVT_ABS/MOVW_ABS_NC %p @ %p in %s\n", |
---|
329 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
330 | } |
---|
331 | break; |
---|
332 | |
---|
333 | case R_TYPE(REL32): /* word32 (S + A) | T - P */ |
---|
334 | case R_TYPE(ABS32): /* word32 (S + A) | T */ |
---|
335 | case R_TYPE(GLOB_DAT): /* word32 (S + A) | T */ |
---|
336 | case R_TYPE(PREL31): /* word32 (S + A) | T - P */ |
---|
337 | case R_TYPE(TARGET1): /* Equivalent to ABS32 */ |
---|
338 | case R_TYPE(TARGET2): /* Equivalent to REL32 */ |
---|
339 | if (!parsing) { |
---|
340 | if (__predict_true(RELOC_ALIGNED_P(where))) { |
---|
341 | tmp = *where + symvalue; |
---|
342 | if (isThumb(symvalue)) |
---|
343 | tmp |= 1; |
---|
344 | if (ELF_R_TYPE(rel->r_info) == R_TYPE(REL32) || |
---|
345 | ELF_R_TYPE(rel->r_info) == R_TYPE(TARGET2)) |
---|
346 | tmp -= (Elf_Addr)where; |
---|
347 | else if (ELF_R_TYPE(rel->r_info) == R_TYPE(PREL31)) |
---|
348 | tmp = sign_extend31(tmp - (Elf_Addr)where); |
---|
349 | if (!parsing) { |
---|
350 | *where = tmp; |
---|
351 | } |
---|
352 | } else { |
---|
353 | tmp = load_ptr(where) + symvalue; |
---|
354 | if (isThumb(symvalue)) |
---|
355 | tmp |= 1; |
---|
356 | if (ELF_R_TYPE(rel->r_info) == R_TYPE(REL32) || |
---|
357 | ELF_R_TYPE(rel->r_info) == R_TYPE(TARGET2)) |
---|
358 | tmp -= (Elf_Addr)where; |
---|
359 | else if (ELF_R_TYPE(rel->r_info) == R_TYPE(PREL31)) |
---|
360 | tmp = sign_extend31(tmp - (Elf_Addr)where); |
---|
361 | store_ptr(where, tmp); |
---|
362 | } |
---|
363 | |
---|
364 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
365 | printf ("rtl: REL32/ABS32/GLOB_DAT/PREL31/TARGET2 %p @ %p in %s\n", |
---|
366 | (void *)tmp, where, rtems_rtl_obj_oname (obj)); |
---|
367 | } |
---|
368 | break; |
---|
369 | |
---|
370 | case R_TYPE(THM_MOVT_ABS): |
---|
371 | case R_TYPE(THM_MOVW_ABS_NC): |
---|
372 | if (!parsing) { |
---|
373 | upper_insn = *(uint16_t *)where; |
---|
374 | lower_insn = *((uint16_t *)where + 1); |
---|
375 | |
---|
376 | addend = ((upper_insn & 0x000f) << 12) | ((upper_insn & 0x0400) << 1) | |
---|
377 | ((lower_insn & 0x7000) >> 4) | (lower_insn & 0x00ff); |
---|
378 | addend = (addend ^ 0x8000) - 0x8000; |
---|
379 | |
---|
380 | tmp = addend + symvalue; |
---|
381 | if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS) |
---|
382 | tmp >>= 16; |
---|
383 | |
---|
384 | *(uint16_t *)where = (uint16_t)((upper_insn & 0xfbf0) | |
---|
385 | ((tmp & 0xf000) >> 12) | |
---|
386 | ((tmp & 0x0800) >> 1)); |
---|
387 | *((uint16_t *)where + 1) = (uint16_t)((lower_insn & 0x8f00) | |
---|
388 | ((tmp & 0x0700) << 4) | |
---|
389 | (tmp & 0x00ff)); |
---|
390 | |
---|
391 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) { |
---|
392 | printf ("rtl: THM_MOVT_ABS/THM_MOVW_ABS_NC %p @ %p in %s\n", |
---|
393 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
394 | } |
---|
395 | } |
---|
396 | break; |
---|
397 | |
---|
398 | case R_TYPE(THM_JUMP24): |
---|
399 | /* same as THM_PC22; insn b.w */ |
---|
400 | case R_TYPE(THM_PC22): |
---|
401 | if (parsing) |
---|
402 | { |
---|
403 | addend = 0; |
---|
404 | upper_insn = 0; |
---|
405 | lower_insn = 0; |
---|
406 | } |
---|
407 | else |
---|
408 | { |
---|
409 | upper_insn = *(uint16_t *)where; |
---|
410 | lower_insn = *((uint16_t *)where + 1); |
---|
411 | sign = (upper_insn & (1 << 10)) >> 10; |
---|
412 | i1 = ((lower_insn >> 13) & 1) ^ sign ? 0 : 1; |
---|
413 | i2 = ((lower_insn >> 11) & 1) ^ sign ? 0 : 1; |
---|
414 | tmp = (i1 << 23) | (i2 << 22) | ((upper_insn & 0x3ff) << 12) | ((lower_insn & 0x7ff) << 1); |
---|
415 | addend = (tmp | ((sign ? 0 : 1) << 24)) - (1 << 24); |
---|
416 | } |
---|
417 | |
---|
418 | if (isThumb(symvalue)) ;/*Thumb to Thumb call, nothing to care */ |
---|
419 | else { |
---|
420 | if (ELF_R_TYPE(rel->r_info) == R_TYPE(THM_JUMP24)) { |
---|
421 | tmp = (tmp + 2) & ~3; /* aligned to 4 bytes only for JUMP24 */ |
---|
422 | #if !ALLOW_UNTESTED_RELOCS |
---|
423 | printf("THM_JUMP24 to arm not supported\n"); |
---|
424 | return rtems_rtl_elf_rel_failure; |
---|
425 | #endif |
---|
426 | } |
---|
427 | else { |
---|
428 | /* THM_CALL bl-->blx */ |
---|
429 | lower_insn &= ~(1<<12); |
---|
430 | } |
---|
431 | } |
---|
432 | |
---|
433 | if (parsing && sect->base == 0) { |
---|
434 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
435 | printf ("rtl: THM_CALL/JUMP24 tramp cache\n"); |
---|
436 | return rtems_rtl_elf_rel_tramp_cache; |
---|
437 | } |
---|
438 | |
---|
439 | tmp = symvalue + addend; |
---|
440 | tmp = tmp - (Elf_Addr)where; |
---|
441 | |
---|
442 | if (((Elf_Sword)tmp > 0x7fffff) || ((Elf_Sword)tmp < -0x800000)) { |
---|
443 | Elf_Word tramp_addr; |
---|
444 | size_t tramp_size = get_veneer_size(ELF_R_TYPE(rel->r_info)); |
---|
445 | |
---|
446 | if (parsing) { |
---|
447 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
448 | printf ("rtl: THM_CALL/JUMP24 tramp add: %08x - %p = %i\n", |
---|
449 | symvalue + addend, where, (Elf_Sword) tmp); |
---|
450 | return rtems_rtl_elf_rel_tramp_add; |
---|
451 | } |
---|
452 | |
---|
453 | if (!rtems_rtl_obj_has_tramp_space (obj, tramp_size)) { |
---|
454 | rtems_rtl_set_error (EINVAL, |
---|
455 | "%s: THM_CALL/JUMP24: overflow: no tramp memory", |
---|
456 | sect->name); |
---|
457 | return rtems_rtl_elf_rel_failure; |
---|
458 | } |
---|
459 | |
---|
460 | tramp_addr = ((Elf_Addr) obj->tramp_brk) | (symvalue & 1); |
---|
461 | obj->tramp_brk = set_veneer(obj->tramp_brk, symvalue); |
---|
462 | |
---|
463 | |
---|
464 | tmp = tramp_addr + addend; |
---|
465 | tmp = tmp - (Elf_Addr)where; |
---|
466 | } |
---|
467 | |
---|
468 | if (!parsing) { |
---|
469 | sign = (tmp >> 24) & 1; |
---|
470 | *(uint16_t *)where = (uint16_t)((upper_insn & 0xf800) | (sign << 10) | |
---|
471 | ((tmp >> 12) & 0x3ff)); |
---|
472 | |
---|
473 | *((uint16_t *)where + 1) = (uint16_t)((lower_insn & 0xd000)| |
---|
474 | ((sign ^ (~(tmp >> 23) & 1)) << 13) | |
---|
475 | ((sign ^ (~(tmp >> 22) & 1)) << 11) | |
---|
476 | ((tmp >> 1) & 0x7ff)); |
---|
477 | |
---|
478 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)){ |
---|
479 | printf ("rtl: THM_CALL/JUMP24 %p @ %p in %s\n", |
---|
480 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
481 | } |
---|
482 | } |
---|
483 | break; |
---|
484 | |
---|
485 | case R_TYPE(THM_JUMP19): |
---|
486 | |
---|
487 | if (!isThumb(symvalue)) { |
---|
488 | printf("THM_JUMP19 to arm not supported\n"); |
---|
489 | return rtems_rtl_elf_rel_failure; |
---|
490 | } |
---|
491 | |
---|
492 | upper_insn = *(uint16_t *)where; |
---|
493 | lower_insn = *((uint16_t *)where + 1); |
---|
494 | sign = (upper_insn >> 10) & 0x1; |
---|
495 | |
---|
496 | if ((((upper_insn & 0x3f) >> 7) & 0x7) == 0x7) { |
---|
497 | printf("THM_JUMP19 failed\n"); |
---|
498 | return false; /*if cond <3:1> == '111', see Related codings in armv7a manual */ |
---|
499 | } |
---|
500 | |
---|
501 | i1 = (lower_insn >> 13) & 0x1; |
---|
502 | i2 = (lower_insn >> 11) & 0x1; |
---|
503 | |
---|
504 | tmp = ((i2 << 19) | (i1 << 18) | ((upper_insn & 0x3f) << 12) | ((lower_insn & 0x7ff) << 1)); |
---|
505 | addend = (tmp | ((sign ? 0 : 1) << 20)) - (1 << 20); |
---|
506 | tmp = symvalue + addend; |
---|
507 | |
---|
508 | tmp = tmp - (Elf_Addr)where; |
---|
509 | |
---|
510 | if (((Elf_Sword)tmp > 0x7ffffe) || ((Elf_Sword)tmp < -0x800000)) { |
---|
511 | rtems_rtl_set_error (EINVAL, "%s: Overflow %" PRIu32 " " |
---|
512 | "THM_JUMP19 relocations", |
---|
513 | sect->name, (uint32_t) ELF_R_TYPE(rel->r_info)); |
---|
514 | return rtems_rtl_elf_rel_failure; |
---|
515 | } |
---|
516 | |
---|
517 | sign = (tmp >> 20) & 0x1; |
---|
518 | i2 = (tmp >> 19) & 0x1; |
---|
519 | i1 = (tmp >> 18) & 0x1; |
---|
520 | |
---|
521 | *(uint16_t*)where = (upper_insn & 0xfbc0) | (sign << 10) | ((tmp >> 12) & 0x3f); |
---|
522 | *((uint16_t*)where + 1) = (lower_insn & 0xd000) | (i1 << 13) | |
---|
523 | (i2 << 11) | ((tmp >> 1) & 0x7ff); |
---|
524 | |
---|
525 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
526 | printf ("rtl: THM_JUMP19 %p @ %p in %s\n", |
---|
527 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
528 | break; |
---|
529 | |
---|
530 | case R_TYPE(TLS_LE32): |
---|
531 | #if ALLOW_UNTESTED_RELOCS |
---|
532 | if (!parsing) { |
---|
533 | addend = *where; |
---|
534 | *where = symvalue + addend; |
---|
535 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
536 | printf ("rtl: TLS_LE32 %p @ %p in %s\n", |
---|
537 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
538 | } |
---|
539 | break; |
---|
540 | #endif |
---|
541 | case R_TYPE(TLS_GD32): |
---|
542 | case R_TYPE(TLS_LDM32): |
---|
543 | case R_TYPE(TLS_LDO32): |
---|
544 | case R_TYPE(TLS_IE32): |
---|
545 | case R_TYPE(TLS_LDO12): |
---|
546 | case R_TYPE(TLS_LE12): |
---|
547 | case R_TYPE(TLS_IE12GP): |
---|
548 | printf("TSL relocations not supported\n"); |
---|
549 | |
---|
550 | default: |
---|
551 | printf ("rtl: reloc unknown: sym = %" PRIu32 ", type = %" PRIu32 ", offset = %p", |
---|
552 | ELF_R_SYM(rel->r_info), (uint32_t) ELF_R_TYPE(rel->r_info), |
---|
553 | (void *)rel->r_offset); |
---|
554 | if (!parsing) |
---|
555 | printf("contents = %p", (void *)*where); |
---|
556 | printf("\n"); |
---|
557 | rtems_rtl_set_error (EINVAL, |
---|
558 | "%s: Unsupported relocation type %" PRIu32 " " |
---|
559 | "in non-PLT relocations", |
---|
560 | sect->name, (uint32_t) ELF_R_TYPE(rel->r_info)); |
---|
561 | return rtems_rtl_elf_rel_failure; |
---|
562 | } |
---|
563 | |
---|
564 | return rtems_rtl_elf_rel_no_error; |
---|
565 | } |
---|
566 | |
---|
567 | rtems_rtl_elf_rel_status |
---|
568 | rtems_rtl_elf_relocate_rel_tramp (rtems_rtl_obj* obj, |
---|
569 | const Elf_Rel* rel, |
---|
570 | const rtems_rtl_obj_sect* sect, |
---|
571 | const char* symname, |
---|
572 | const Elf_Byte syminfo, |
---|
573 | const Elf_Word symvalue) |
---|
574 | { |
---|
575 | return rtems_rtl_elf_reloc_rel (obj, |
---|
576 | rel, |
---|
577 | sect, |
---|
578 | symname, |
---|
579 | syminfo, |
---|
580 | symvalue, |
---|
581 | true); |
---|
582 | } |
---|
583 | |
---|
584 | rtems_rtl_elf_rel_status |
---|
585 | rtems_rtl_elf_relocate_rel (rtems_rtl_obj* obj, |
---|
586 | const Elf_Rel* rel, |
---|
587 | const rtems_rtl_obj_sect* sect, |
---|
588 | const char* symname, |
---|
589 | const Elf_Byte syminfo, |
---|
590 | const Elf_Word symvalue) |
---|
591 | { |
---|
592 | return rtems_rtl_elf_reloc_rel (obj, |
---|
593 | rel, |
---|
594 | sect, |
---|
595 | symname, |
---|
596 | syminfo, |
---|
597 | symvalue, |
---|
598 | false); |
---|
599 | } |
---|
600 | |
---|
601 | bool |
---|
602 | rtems_rtl_elf_unwind_parse (const rtems_rtl_obj* obj, |
---|
603 | const char* name, |
---|
604 | uint32_t flags) |
---|
605 | { |
---|
606 | /* |
---|
607 | * We location the EH sections in section flags. |
---|
608 | */ |
---|
609 | return false; |
---|
610 | } |
---|
611 | |
---|
612 | bool |
---|
613 | rtems_rtl_elf_unwind_register (rtems_rtl_obj* obj) |
---|
614 | { |
---|
615 | return true; |
---|
616 | } |
---|
617 | |
---|
618 | bool |
---|
619 | rtems_rtl_elf_unwind_deregister (rtems_rtl_obj* obj) |
---|
620 | { |
---|
621 | obj->loader = NULL; |
---|
622 | return true; |
---|
623 | } |
---|
624 | |
---|
625 | /* An exception index table entry. */ |
---|
626 | typedef struct __EIT_entry |
---|
627 | { |
---|
628 | _uw fnoffset; |
---|
629 | _uw content; |
---|
630 | } __EIT_entry; |
---|
631 | |
---|
632 | /* The exception index table location in the base module */ |
---|
633 | extern __EIT_entry __exidx_start; |
---|
634 | extern __EIT_entry __exidx_end; |
---|
635 | |
---|
636 | /* |
---|
637 | * A weak reference is in libgcc, provide a real version and provide a way to |
---|
638 | * manage loaded modules. |
---|
639 | * |
---|
640 | * Passed in the return address and a reference to the number of records |
---|
641 | * found. We set the start of the exidx data and the number of records. |
---|
642 | */ |
---|
643 | _Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, |
---|
644 | int* nrec) __attribute__ ((__noinline__, |
---|
645 | __used__, |
---|
646 | __noclone__)); |
---|
647 | |
---|
648 | _Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address, |
---|
649 | int* nrec) |
---|
650 | { |
---|
651 | rtems_rtl_data* rtl; |
---|
652 | rtems_chain_node* node; |
---|
653 | __EIT_entry* exidx_start = &__exidx_start; |
---|
654 | __EIT_entry* exidx_end = &__exidx_end; |
---|
655 | |
---|
656 | rtl = rtems_rtl_lock (); |
---|
657 | |
---|
658 | node = rtems_chain_first (&rtl->objects); |
---|
659 | while (!rtems_chain_is_tail (&rtl->objects, node)) { |
---|
660 | rtems_rtl_obj* obj = (rtems_rtl_obj*) node; |
---|
661 | if (rtems_rtl_obj_text_inside (obj, (void*) return_address)) { |
---|
662 | exidx_start = (__EIT_entry*) obj->eh_base; |
---|
663 | exidx_end = (__EIT_entry*) (obj->eh_base + obj->eh_size); |
---|
664 | break; |
---|
665 | } |
---|
666 | node = rtems_chain_next (node); |
---|
667 | } |
---|
668 | |
---|
669 | rtems_rtl_unlock (); |
---|
670 | |
---|
671 | *nrec = exidx_end - exidx_start; |
---|
672 | |
---|
673 | return (_Unwind_Ptr) exidx_start; |
---|
674 | } |
---|