source: rtems/cpukit/libdl/rtl-mdreloc-arm.c @ 7d9455e

5
Last change on this file since 7d9455e was b2ed712, checked in by Sebastian Huber <sebastian.huber@…>, on 08/25/17 at 08:58:58

Include missing <string.h>

Update #2133.

  • Property mode set to 100644
File size: 13.0 KB
Line 
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 <string.h>
12#include <sys/types.h>
13#include <sys/stat.h>
14#include <unwind.h>
15#include <unwind-arm-common.h>
16
17#include <rtems/rtl/rtl.h>
18#include "rtl-elf.h"
19#include "rtl-error.h"
20#include "rtl-trace.h"
21#include "rtl-unwind.h"
22
23/*
24 * It is possible for the compiler to emit relocations for unaligned data.
25 * We handle this situation with these inlines.
26 */
27#define RELOC_ALIGNED_P(x) \
28        (((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
29
30#define SHT_ARM_EXIDX  0x70000001 /* Section holds ARM unwind info. */
31
32static inline Elf_Addr
33load_ptr(void *where)
34{
35        Elf_Addr res;
36
37        memcpy(&res, where, sizeof(res));
38
39        return (res);
40}
41
42static inline void
43store_ptr(void *where, Elf_Addr val)
44{
45
46        memcpy(where, &val, sizeof(val));
47}
48
49/*
50 * The address of Thumb function symbols is it's real address plus one.
51 * This is done by compiler, thus do not consider symtype here.
52 */
53static inline int
54isThumb(Elf_Word symvalue)
55{
56  if ((symvalue & 0x1) == 0x1)
57    return true;
58  else return false;
59}
60
61static inline Elf_SOff
62sign_extend31(Elf_Addr val)
63{
64  if (0x40000000 & val)
65    val =  ~((Elf_Addr)0x7fffffff) | (0x7fffffff & val);
66  return 0x7fffffff & val;
67}
68
69uint32_t
70rtems_rtl_elf_section_flags (const rtems_rtl_obj_t* obj,
71                             const Elf_Shdr*        shdr)
72{
73  uint32_t flags = 0;
74  if (shdr->sh_type == SHT_ARM_EXIDX)
75    flags = RTEMS_RTL_OBJ_SECT_EH | RTEMS_RTL_OBJ_SECT_LOAD;
76  return flags;
77}
78
79bool
80rtems_rtl_elf_rel_resolve_sym (Elf_Word type)
81{
82  return true;
83}
84
85bool
86rtems_rtl_elf_relocate_rela (const rtems_rtl_obj_t*      obj,
87                             const Elf_Rela*             rela,
88                             const rtems_rtl_obj_sect_t* sect,
89                             const char*                 symname,
90                             const Elf_Byte              syminfo,
91                             const Elf_Word              symvalue)
92{
93  rtems_rtl_set_error (EINVAL, "rela type record not supported");
94  return false;
95}
96
97bool
98rtems_rtl_elf_relocate_rel (const rtems_rtl_obj_t*      obj,
99                            const Elf_Rel*              rel,
100                            const rtems_rtl_obj_sect_t* sect,
101                            const char*                 symname,
102                            const Elf_Byte              syminfo,
103                            const Elf_Word              symvalue)
104{
105  Elf_Addr *where;
106  Elf_Addr tmp;
107  Elf_Word insn, addend;
108  Elf_Word sign, i1, i2;
109  uint16_t lower_insn, upper_insn;
110
111  where = (Elf_Addr *)(sect->base + rel->r_offset);
112
113  switch (ELF_R_TYPE(rel->r_info)) {
114    case R_TYPE(NONE):
115      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) {
116        printf ("rtl: NONE %p in %s\n", where, rtems_rtl_obj_oname (obj));
117      }
118      break;
119
120    case R_TYPE(CALL):    /* BL/BLX */
121    case R_TYPE(JUMP24):  /* B/BL<cond> */
122      insn = *where;
123
124      if (insn & 0x00800000)
125        addend = insn | 0xff000000;
126      else addend = insn & 0x00ffffff;
127
128      if (isThumb(symvalue)) {
129        if ((insn & 0xfe000000) == 0xfa000000);         /* Already blx */
130        else {
131          if ((insn & 0xff000000) == 0xeb000000) {      /* BL <label> */
132            *where = (insn & 0x00ffffff) | 0xfa000000;  /* BL-->BLX */
133          } else {
134            printf("JUMP24 is not suppored from arm to thumb\n");
135            return false;
136          }
137        }
138      }
139
140      tmp = symvalue + (addend << 2) - (Elf_Addr)where;
141      tmp = (Elf_Sword)tmp >> 2;
142
143      if (((Elf_Sword)tmp > 0x7fffff) || ((Elf_Sword)tmp < -0x800000)) {
144        printf("CALL/JUMP24 Overflow\n");
145        return false;
146      }
147
148      *where = (*where & 0xff000000) | (tmp & 0xffffff);
149
150      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
151        printf ("rtl: JUMP24/PC24/CALL %p @ %p in %s\n",
152                (void *)*where, where, rtems_rtl_obj_oname (obj));
153
154      break;
155
156    case R_TYPE(V4BX):
157      /* Miscellaneous, ignore */
158      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) {
159        printf ("rtl: V4BX %p @ %p in %s\n",
160                (void *)*where, where, rtems_rtl_obj_oname (obj));
161      }
162      break;
163
164    case R_TYPE(MOVT_ABS):
165    case R_TYPE(MOVW_ABS_NC):
166      insn = *where;
167
168      addend = ((insn >> 4) & 0xf000) | (insn & 0x0fff);
169      if (addend & 0x8000)
170        addend |= 0xffff0000;
171
172      tmp = symvalue + addend;
173
174      if (ELF_R_TYPE(rel->r_info) == R_TYPE(MOVW_ABS_NC))
175        tmp &= 0xffff;
176      else {
177        tmp = (Elf_Sword)tmp >> 16;
178        if (((Elf_Sword)tmp > 0x7fff) || ((Elf_Sword)tmp < -0x8000)) {
179          printf("MOVT_ABS Overflow\n");
180          return false;
181        }
182      }
183
184      *where = (insn & 0xfff0f000) | ((tmp & 0xf000) << 4) | (tmp & 0xfff);
185
186      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
187        printf ("rtl: MOVT_ABS/MOVW_ABS_NC %p @ %p in %s\n",
188                (void *)*where, where, rtems_rtl_obj_oname (obj));
189      break;
190
191
192    case R_TYPE(REL32):     /* word32 (S + A) | T - P */
193    case R_TYPE(ABS32):     /* word32 (S + A) | T */
194    case R_TYPE(GLOB_DAT):  /* word32 (S + A) | T */
195    case R_TYPE(PREL31):    /* word32 (S + A) | T - P */
196    case R_TYPE(TARGET2):   /* Equivalent to REL32 */
197      if (__predict_true(RELOC_ALIGNED_P(where))) {
198        tmp = *where + symvalue;
199        if (isThumb(symvalue))
200          tmp |= 1;
201        if (ELF_R_TYPE(rel->r_info) == R_TYPE(REL32) ||
202            ELF_R_TYPE(rel->r_info) == R_TYPE(TARGET2))
203          tmp -= (Elf_Addr)where;
204        else if (ELF_R_TYPE(rel->r_info) == R_TYPE(PREL31))
205          tmp = sign_extend31(tmp - (Elf_Addr)where);
206        *where = tmp;
207      } else {
208        tmp = load_ptr(where) + symvalue;
209        if (isThumb(symvalue))
210          tmp |= 1;
211        if (ELF_R_TYPE(rel->r_info) == R_TYPE(REL32) ||
212            ELF_R_TYPE(rel->r_info) == R_TYPE(TARGET2))
213          tmp -= (Elf_Addr)where;
214        else if (ELF_R_TYPE(rel->r_info) == R_TYPE(PREL31))
215          tmp = sign_extend31(tmp - (Elf_Addr)where);
216        store_ptr(where, tmp);
217      }
218
219      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
220        printf ("rtl: REL32/ABS32/GLOB_DAT/PREL31/TARGET2 %p @ %p in %s\n",
221                (void *)tmp, where, rtems_rtl_obj_oname (obj));
222      break;
223
224    case R_TYPE(THM_MOVT_ABS):
225    case R_TYPE(THM_MOVW_ABS_NC):
226      upper_insn = *(uint16_t *)where;
227      lower_insn = *((uint16_t *)where + 1);
228
229      addend = ((upper_insn & 0x000f) << 12) | ((upper_insn & 0x0400) << 1) |
230               ((lower_insn & 0x7000) >> 4) | (lower_insn & 0x00ff);
231      addend = (addend ^ 0x8000) - 0x8000;
232
233      tmp = addend + symvalue;
234      if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
235        tmp >>= 16;
236
237      *(uint16_t *)where = (uint16_t)((upper_insn & 0xfbf0) |
238                                     ((tmp & 0xf000) >> 12) |
239                                     ((tmp & 0x0800) >> 1));
240      *((uint16_t *)where + 1) = (uint16_t)((lower_insn & 0x8f00) |
241                                           ((tmp & 0x0700) << 4) |
242                                           (tmp & 0x00ff));
243
244      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) {
245        printf ("rtl: THM_MOVT_ABS/THM_MOVW_ABS_NC %p @ %p in %s\n",
246                (void *)*where, where, rtems_rtl_obj_oname (obj));
247      }
248
249      break;
250
251    case R_TYPE(THM_JUMP24):
252      /* same to THM_CALL; insn b.w */
253    case R_TYPE(THM_CALL):
254      upper_insn = *(uint16_t *)where;
255      lower_insn = *((uint16_t *)where + 1);
256      sign = (upper_insn & (1 << 10)) >> 10;
257      i1 = ((lower_insn >> 13) & 1) ^ sign ? 0 : 1;
258      i2 = ((lower_insn >> 11) & 1) ^ sign ? 0 : 1;
259      tmp = (i1 << 23) | (i2 << 22) | ((upper_insn & 0x3ff) << 12) | ((lower_insn & 0x7ff) << 1);
260      addend = (tmp | ((sign ? 0 : 1) << 24)) - (1 << 24);
261
262      if (isThumb(symvalue)) ;/*Thumb to Thumb call, nothing to care */
263      else {
264        if (ELF_R_TYPE(rel->r_info) == R_TYPE(THM_JUMP24)) {
265          tmp = (tmp + 2) & ~3; /* aligned to 4 bytes only for JUMP24 */
266          printf("THM_JUMP24 to arm not supported\n");
267          return false;
268        }
269        else {
270          /* THM_CALL bl-->blx */
271          lower_insn &=~(1<<12);
272        }
273      }
274
275      tmp = symvalue + addend;
276      tmp = tmp - (Elf_Addr)where;
277
278      if (((Elf_Sword)tmp > 0x7fffff) || ((Elf_Sword)tmp < -0x800000)) {
279        printf("THM_CALL/JUMP24 overflow\n");
280        return false;
281      }
282
283      sign = (tmp >> 24) & 1;
284      *(uint16_t *)where = (uint16_t)((upper_insn & 0xf800) | (sign << 10) |
285                                     ((tmp >> 12) & 0x3ff));
286
287      *((uint16_t *)where + 1) = (uint16_t)((lower_insn & 0xd000)|
288                                           ((sign ^ (~(tmp >> 23) & 1)) << 13) |
289                                           ((sign ^ (~(tmp >> 22) & 1)) << 11) |
290                                           ((tmp >> 1) & 0x7ff));
291
292      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)){
293        printf ("rtl: THM_CALL/JUMP24 %p @ %p in %s\n",
294                (void *)*where, where, rtems_rtl_obj_oname (obj));
295      }
296
297      break;
298
299    case R_TYPE(THM_JUMP19):
300
301      if (!isThumb(symvalue)) {
302        printf("THM_JUMP19 to arm not supported\n");
303        return false;
304      }
305
306      upper_insn = *(uint16_t *)where;
307      lower_insn = *((uint16_t *)where + 1);
308      sign = (upper_insn >> 10) & 0x1;
309
310      if ((((upper_insn & 0x3f) >> 7) & 0x7) == 0x7) {
311        printf("THM_JUMP19 failed\n");
312        return false; /*if cond <3:1> == '111', see Related codings in armv7a manual */
313      }
314
315      i1 = (lower_insn >> 13) & 0x1;
316      i2 = (lower_insn >> 11) & 0x1;
317
318      tmp = ((i2 << 19) | (i1 << 18) | ((upper_insn & 0x3f) << 12) | ((lower_insn & 0x7ff) << 1));
319      addend = (tmp | ((sign ? 0 : 1) << 20)) - (1 << 20);
320      tmp = symvalue + addend;
321
322      tmp = tmp - (Elf_Addr)where;
323
324      if (((Elf_Sword)tmp > 0x7ffffe) || ((Elf_Sword)tmp < -0x800000)) {
325        rtems_rtl_set_error (EINVAL, "%s: Overflow %ld "
326                             "THM_JUMP19 relocations",
327                             sect->name, (uint32_t) ELF_R_TYPE(rel->r_info));
328        return false;
329      }
330
331      sign = (tmp >> 20) & 0x1;
332      i2 = (tmp >> 19) & 0x1;
333      i1 = (tmp >> 18) & 0x1;
334
335      *(uint16_t*)where = (upper_insn & 0xfbc0) | (sign << 10) | ((tmp >> 12) & 0x3f);
336      *((uint16_t*)where + 1) = (lower_insn & 0xd000) | (i1 << 13) |
337                                (i2 << 11) | ((tmp >> 1) & 0x7ff);
338
339      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
340        printf ("rtl: THM_JUMP19 %p @ %p in %s\n",
341                (void *)*where, where, rtems_rtl_obj_oname (obj));
342      break;
343
344    default:
345      printf ("rtl: reloc unknown: sym = %lu, type = %lu, offset = %p, "
346              "contents = %p\n",
347              ELF_R_SYM(rel->r_info), (uint32_t) ELF_R_TYPE(rel->r_info),
348              (void *)rel->r_offset, (void *)*where);
349      rtems_rtl_set_error (EINVAL,
350                           "%s: Unsupported relocation type %ld "
351                           "in non-PLT relocations",
352                           sect->name, (uint32_t) ELF_R_TYPE(rel->r_info));
353      return false;
354  }
355
356  return true;
357}
358
359bool
360rtems_rtl_elf_unwind_parse (const rtems_rtl_obj_t* obj,
361                            const char*            name,
362                            uint32_t               flags)
363{
364  /*
365   * We location the EH sections in section flags.
366   */
367  return false;
368}
369
370bool
371rtems_rtl_elf_unwind_register (rtems_rtl_obj_t* obj)
372{
373  return true;
374}
375
376bool
377rtems_rtl_elf_unwind_deregister (rtems_rtl_obj_t* obj)
378{
379  obj->loader = NULL;
380  return true;
381}
382
383/* An exception index table entry.  */
384typedef struct __EIT_entry
385{
386  _uw fnoffset;
387  _uw content;
388} __EIT_entry;
389
390/* The exception index table location in the base module */
391extern __EIT_entry __exidx_start;
392extern __EIT_entry __exidx_end;
393
394/*
395 * A weak reference is in libgcc, provide a real version and provide a way to
396 * manage loaded modules.
397 *
398 * Passed in the return address and a reference to the number of records
399 * found. We set the start of the exidx data and the number of records.
400 */
401_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address,
402                                     int*        nrec) __attribute__ ((__noinline__,
403                                                                       __used__,
404                                                                       __noclone__));
405
406_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address,
407                                     int*        nrec)
408{
409  rtems_rtl_data_t* rtl;
410  rtems_chain_node* node;
411  __EIT_entry*      exidx_start = &__exidx_start;
412  __EIT_entry*      exidx_end = &__exidx_end;
413
414  rtl = rtems_rtl_lock ();
415
416  node = rtems_chain_first (&rtl->objects);
417  while (!rtems_chain_is_tail (&rtl->objects, node)) {
418    rtems_rtl_obj_t* obj = (rtems_rtl_obj_t*) node;
419    if (rtems_rtl_obj_text_inside (obj, (void*) return_address)) {
420      exidx_start = (__EIT_entry*) obj->eh_base;
421      exidx_end = (__EIT_entry*) (obj->eh_base + obj->eh_size);
422      break;
423    }
424    node = rtems_chain_next (node);
425  }
426
427  rtems_rtl_unlock ();
428
429  *nrec = exidx_end - exidx_start;
430
431  return (_Unwind_Ptr) exidx_start;
432}
Note: See TracBrowser for help on using the repository browser.