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 <stdio.h> |
---|
11 | #include <sys/types.h> |
---|
12 | #include <sys/stat.h> |
---|
13 | |
---|
14 | #include <rtems/rtl/rtl.h> |
---|
15 | #include "rtl-elf.h" |
---|
16 | #include "rtl-error.h" |
---|
17 | #include "rtl-trace.h" |
---|
18 | |
---|
19 | /* |
---|
20 | * It is possible for the compiler to emit relocations for unaligned data. |
---|
21 | * We handle this situation with these inlines. |
---|
22 | */ |
---|
23 | #define RELOC_ALIGNED_P(x) \ |
---|
24 | (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0) |
---|
25 | |
---|
26 | static inline Elf_Addr |
---|
27 | load_ptr(void *where) |
---|
28 | { |
---|
29 | Elf_Addr res; |
---|
30 | |
---|
31 | memcpy(&res, where, sizeof(res)); |
---|
32 | |
---|
33 | return (res); |
---|
34 | } |
---|
35 | |
---|
36 | static inline void |
---|
37 | store_ptr(void *where, Elf_Addr val) |
---|
38 | { |
---|
39 | |
---|
40 | memcpy(where, &val, sizeof(val)); |
---|
41 | } |
---|
42 | |
---|
43 | /* |
---|
44 | * The address of Thumb function symbols is it's real address plus one. |
---|
45 | * This is done by compiler, thus do not consider symtype here. |
---|
46 | */ |
---|
47 | static inline int |
---|
48 | isThumb(Elf_Word symvalue) |
---|
49 | { |
---|
50 | if ((symvalue & 0x1) == 0x1) |
---|
51 | return true; |
---|
52 | else return false; |
---|
53 | } |
---|
54 | |
---|
55 | bool |
---|
56 | rtems_rtl_elf_rel_resolve_sym (Elf_Word type) |
---|
57 | { |
---|
58 | return true; |
---|
59 | } |
---|
60 | |
---|
61 | bool |
---|
62 | rtems_rtl_elf_relocate_rela (const rtems_rtl_obj_t* obj, |
---|
63 | const Elf_Rela* rela, |
---|
64 | const rtems_rtl_obj_sect_t* sect, |
---|
65 | const char* symname, |
---|
66 | const Elf_Byte syminfo, |
---|
67 | const Elf_Word symvalue) |
---|
68 | { |
---|
69 | rtems_rtl_set_error (EINVAL, "rela type record not supported"); |
---|
70 | return false; |
---|
71 | } |
---|
72 | |
---|
73 | bool |
---|
74 | rtems_rtl_elf_relocate_rel (const rtems_rtl_obj_t* obj, |
---|
75 | const Elf_Rel* rel, |
---|
76 | const rtems_rtl_obj_sect_t* sect, |
---|
77 | const char* symname, |
---|
78 | const Elf_Byte syminfo, |
---|
79 | const Elf_Word symvalue) |
---|
80 | { |
---|
81 | Elf_Addr *where; |
---|
82 | Elf_Addr tmp; |
---|
83 | Elf_Word insn, addend; |
---|
84 | Elf_Word sign, i1, i2; |
---|
85 | uint16_t lower_insn, upper_insn; |
---|
86 | |
---|
87 | where = (Elf_Addr *)(sect->base + rel->r_offset); |
---|
88 | |
---|
89 | switch (ELF_R_TYPE(rel->r_info)) { |
---|
90 | case R_TYPE(NONE): |
---|
91 | break; |
---|
92 | |
---|
93 | case R_TYPE(CALL): /* BL/BLX */ |
---|
94 | case R_TYPE(JUMP24): /* B/BL<cond> */ |
---|
95 | insn = *where; |
---|
96 | |
---|
97 | if (insn & 0x00800000) |
---|
98 | addend = insn | 0xff000000; |
---|
99 | else addend = insn & 0x00ffffff; |
---|
100 | |
---|
101 | if (isThumb(symvalue)) { |
---|
102 | if ((insn & 0xfe000000) == 0xfa000000); /* Already blx */ |
---|
103 | else { |
---|
104 | if ((insn & 0xff000000) == 0xeb000000) { /* BL <label> */ |
---|
105 | *where = (insn & 0x00ffffff) | 0xfa000000; /* BL-->BLX */ |
---|
106 | } else { |
---|
107 | printf("JUMP24 is not suppored from arm to thumb\n"); |
---|
108 | return false; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | |
---|
113 | tmp = symvalue + (addend << 2) - (Elf_Addr)where; |
---|
114 | tmp = (Elf_Sword)tmp >> 2; |
---|
115 | |
---|
116 | if (((Elf_Sword)tmp > 0x7fffff) || ((Elf_Sword)tmp < -0x800000)) { |
---|
117 | printf("CALL/JUMP24 Overflow\n"); |
---|
118 | return false; |
---|
119 | } |
---|
120 | |
---|
121 | *where = (*where & 0xff000000) | (tmp & 0xffffff); |
---|
122 | |
---|
123 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
124 | printf ("rtl: JUMP24/PC24/CALL %p @ %p in %s\n", |
---|
125 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
126 | |
---|
127 | break; |
---|
128 | |
---|
129 | case R_TYPE(V4BX): |
---|
130 | /* Miscellaneous, ignore */ |
---|
131 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) { |
---|
132 | printf ("rtl: V4BX %p @ %p in %s\n", |
---|
133 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
134 | } |
---|
135 | break; |
---|
136 | |
---|
137 | case R_TYPE(MOVT_ABS): |
---|
138 | case R_TYPE(MOVW_ABS_NC): |
---|
139 | insn = *where; |
---|
140 | |
---|
141 | addend = ((insn >> 4) & 0xf000) | (insn & 0x0fff); |
---|
142 | if (addend & 0x8000) |
---|
143 | addend |= 0xffff0000; |
---|
144 | |
---|
145 | tmp = symvalue + addend; |
---|
146 | |
---|
147 | if (ELF_R_TYPE(rel->r_info) == R_TYPE(MOVW_ABS_NC)) |
---|
148 | tmp &= 0xffff; |
---|
149 | else { |
---|
150 | tmp = (Elf_Sword)tmp >> 16; |
---|
151 | if (((Elf_Sword)tmp > 0x7fff) || ((Elf_Sword)tmp < -0x8000)) { |
---|
152 | printf("MOVT_ABS Overflow\n"); |
---|
153 | return false; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | *where = (insn & 0xfff0f000) | ((tmp & 0xf000) << 4) | (tmp & 0xfff); |
---|
158 | |
---|
159 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
160 | printf ("rtl: MOVT_ABS/MOVW_ABS_NC %p @ %p in %s\n", |
---|
161 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
162 | break; |
---|
163 | |
---|
164 | |
---|
165 | case R_TYPE(REL32): /* word32 (S + A) | T - P */ |
---|
166 | case R_TYPE(ABS32): /* word32 (S + A) | T */ |
---|
167 | case R_TYPE(GLOB_DAT): /* word32 (S + A) | T */ |
---|
168 | if (__predict_true(RELOC_ALIGNED_P(where))) { |
---|
169 | tmp = *where + symvalue; |
---|
170 | if (isThumb(symvalue)) |
---|
171 | tmp |= 1; |
---|
172 | if (ELF_R_TYPE(rel->r_info) == R_TYPE(REL32)) |
---|
173 | tmp -= (Elf_Addr)where; |
---|
174 | *where = tmp; |
---|
175 | } else { |
---|
176 | tmp = load_ptr(where) + symvalue; |
---|
177 | if (isThumb(symvalue)) |
---|
178 | tmp |= 1; |
---|
179 | if (ELF_R_TYPE(rel->r_info) == R_TYPE(REL32)) |
---|
180 | tmp -= (Elf_Addr)where; |
---|
181 | store_ptr(where, tmp); |
---|
182 | } |
---|
183 | |
---|
184 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
185 | printf ("rtl: REL32/ABS32/GLOB_DAT %p @ %p in %s", |
---|
186 | (void *)tmp, where, rtems_rtl_obj_oname (obj)); |
---|
187 | break; |
---|
188 | |
---|
189 | case R_TYPE(THM_MOVT_ABS): |
---|
190 | case R_TYPE(THM_MOVW_ABS_NC): |
---|
191 | upper_insn = *(uint16_t *)where; |
---|
192 | lower_insn = *((uint16_t *)where + 1); |
---|
193 | |
---|
194 | addend = ((upper_insn & 0x000f) << 12) | ((upper_insn & 0x0400) << 1) | |
---|
195 | ((lower_insn & 0x7000) >> 4) | (lower_insn & 0x00ff); |
---|
196 | addend = (addend ^ 0x8000) - 0x8000; |
---|
197 | |
---|
198 | tmp = addend + symvalue; |
---|
199 | if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS) |
---|
200 | tmp >>= 16; |
---|
201 | |
---|
202 | *(uint16_t *)where = (uint16_t)((upper_insn & 0xfbf0) | |
---|
203 | ((tmp & 0xf000) >> 12) | |
---|
204 | ((tmp & 0x0800) >> 1)); |
---|
205 | *((uint16_t *)where + 1) = (uint16_t)((lower_insn & 0x8f00) | |
---|
206 | ((tmp & 0x0700) << 4) | |
---|
207 | (tmp & 0x00ff)); |
---|
208 | |
---|
209 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) { |
---|
210 | printf ("rtl: THM_MOVT_ABS/THM_MOVW_ABS_NC %p @ %p in %s\n", |
---|
211 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
212 | } |
---|
213 | |
---|
214 | break; |
---|
215 | |
---|
216 | case R_TYPE(THM_JUMP24): |
---|
217 | /* same to THM_CALL; insn b.w */ |
---|
218 | case R_TYPE(THM_CALL): |
---|
219 | upper_insn = *(uint16_t *)where; |
---|
220 | lower_insn = *((uint16_t *)where + 1); |
---|
221 | sign = (upper_insn & (1 << 10)) >> 10; |
---|
222 | i1 = ((lower_insn >> 13) & 1) ^ sign ? 0 : 1; |
---|
223 | i2 = ((lower_insn >> 11) & 1) ^ sign ? 0 : 1; |
---|
224 | tmp = (i1 << 23) | (i2 << 22) | ((upper_insn & 0x3ff) << 12) | ((lower_insn & 0x7ff) << 1); |
---|
225 | addend = (tmp | ((sign ? 0 : 1) << 24)) - (1 << 24); |
---|
226 | |
---|
227 | if (isThumb(symvalue)) ;/*Thumb to Thumb call, nothing to care */ |
---|
228 | else { |
---|
229 | if (ELF_R_TYPE(rel->r_info) == R_TYPE(THM_JUMP24)) { |
---|
230 | tmp = (tmp + 2) & ~3; /* aligned to 4 bytes only for JUMP24 */ |
---|
231 | printf("THM_JUMP24 to arm not supported\n"); |
---|
232 | return false; |
---|
233 | } |
---|
234 | else { |
---|
235 | /* THM_CALL bl-->blx */ |
---|
236 | lower_insn &=~(1<<12); |
---|
237 | } |
---|
238 | } |
---|
239 | |
---|
240 | tmp = symvalue + addend; |
---|
241 | tmp = tmp - (Elf_Addr)where; |
---|
242 | |
---|
243 | if (((Elf_Sword)tmp > 0x7fffff) || ((Elf_Sword)tmp < -0x800000)) { |
---|
244 | printf("THM_CALL/JUMP24 overflow\n"); |
---|
245 | return false; |
---|
246 | } |
---|
247 | |
---|
248 | sign = (tmp >> 24) & 1; |
---|
249 | *(uint16_t *)where = (uint16_t)((upper_insn & 0xf800) | (sign << 10) | |
---|
250 | ((tmp >> 12) & 0x3ff)); |
---|
251 | |
---|
252 | *((uint16_t *)where + 1) = (uint16_t)((lower_insn & 0xd000)| |
---|
253 | ((sign ^ (~(tmp >> 23) & 1)) << 13) | |
---|
254 | ((sign ^ (~(tmp >> 22) & 1)) << 11) | |
---|
255 | ((tmp >> 1) & 0x7ff)); |
---|
256 | |
---|
257 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)){ |
---|
258 | printf ("rtl: THM_CALL/JUMP24 %p @ %p in %s\n", |
---|
259 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
260 | } |
---|
261 | |
---|
262 | break; |
---|
263 | |
---|
264 | case R_TYPE(THM_JUMP19): |
---|
265 | |
---|
266 | if (!isThumb(symvalue)) { |
---|
267 | printf("THM_JUMP19 to arm not supported\n"); |
---|
268 | return false; |
---|
269 | } |
---|
270 | |
---|
271 | upper_insn = *(uint16_t *)where; |
---|
272 | lower_insn = *((uint16_t *)where + 1); |
---|
273 | sign = (upper_insn >> 10) & 0x1; |
---|
274 | |
---|
275 | if ((((upper_insn & 0x3f) >> 7) & 0x7) == 0x7) { |
---|
276 | printf("THM_JUMP19 failed\n"); |
---|
277 | return false; /*if cond <3:1> == '111', see Related codings in armv7a manual */ |
---|
278 | } |
---|
279 | |
---|
280 | i1 = (lower_insn >> 13) & 0x1; |
---|
281 | i2 = (lower_insn >> 11) & 0x1; |
---|
282 | |
---|
283 | tmp = ((i2 << 19) | (i1 << 18) | ((upper_insn & 0x3f) << 12) | ((lower_insn & 0x7ff) << 1)); |
---|
284 | addend = (tmp | ((sign ? 0 : 1) << 20)) - (1 << 20); |
---|
285 | tmp = symvalue + addend; |
---|
286 | |
---|
287 | tmp = tmp - (Elf_Addr)where; |
---|
288 | |
---|
289 | if (((Elf_Sword)tmp > 0x7ffffe) || ((Elf_Sword)tmp < -0x800000)) { |
---|
290 | rtems_rtl_set_error (EINVAL, "%s: Overflow %ld " |
---|
291 | "THM_JUMP19 relocations", |
---|
292 | sect->name, (uint32_t) ELF_R_TYPE(rel->r_info)); |
---|
293 | return false; |
---|
294 | } |
---|
295 | |
---|
296 | sign = (tmp >> 20) & 0x1; |
---|
297 | i2 = (tmp >> 19) & 0x1; |
---|
298 | i1 = (tmp >> 18) & 0x1; |
---|
299 | |
---|
300 | *(uint16_t*)where = (upper_insn & 0xfbc0) | (sign << 10) | ((tmp >> 12) & 0x3f); |
---|
301 | *((uint16_t*)where + 1) = (lower_insn & 0xd000) | (i1 << 13) | |
---|
302 | (i2 << 11) | ((tmp >> 1) & 0x7ff); |
---|
303 | |
---|
304 | if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) |
---|
305 | printf ("rtl: THM_JUMP19 %p @ %p in %s\n", |
---|
306 | (void *)*where, where, rtems_rtl_obj_oname (obj)); |
---|
307 | break; |
---|
308 | |
---|
309 | default: |
---|
310 | printf ("rtl: reloc unknown: sym = %lu, type = %lu, offset = %p, " |
---|
311 | "contents = %p\n", |
---|
312 | ELF_R_SYM(rel->r_info), (uint32_t) ELF_R_TYPE(rel->r_info), |
---|
313 | (void *)rel->r_offset, (void *)*where); |
---|
314 | rtems_rtl_set_error (EINVAL, |
---|
315 | "%s: Unsupported relocation type %ld " |
---|
316 | "in non-PLT relocations", |
---|
317 | sect->name, (uint32_t) ELF_R_TYPE(rel->r_info)); |
---|
318 | return false; |
---|
319 | } |
---|
320 | |
---|
321 | return true; |
---|
322 | } |
---|
323 | |
---|