summaryrefslogtreecommitdiffstats
path: root/cpukit/libdl/rtl-mdreloc-arm.c
blob: f2f91f171c5b48a29314eb879abc41ff091593d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * Taken from NetBSD and stripped of the relocations not needed on RTEMS.
 */

/*  $NetBSD: mdreloc.c,v 1.33 2010/01/14 12:12:07 skrll Exp $  */

#include <sys/cdefs.h>

#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unwind.h>
#include <unwind-arm-common.h>

#include <rtems/rtl/rtl.h>
#include "rtl-elf.h"
#include "rtl-error.h"
#include "rtl-trace.h"
#include "rtl-unwind.h"

/*
 * It is possible for the compiler to emit relocations for unaligned data.
 * We handle this situation with these inlines.
 */
#define	RELOC_ALIGNED_P(x) \
	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)

#define SHT_ARM_EXIDX  0x70000001 /* Section holds ARM unwind info. */

static inline Elf_Addr
load_ptr(void *where)
{
	Elf_Addr res;

	memcpy(&res, where, sizeof(res));

	return (res);
}

static inline void
store_ptr(void *where, Elf_Addr val)
{

	memcpy(where, &val, sizeof(val));
}

/*
 * The address of Thumb function symbols is it's real address plus one.
 * This is done by compiler, thus do not consider symtype here.
 */
static inline int
isThumb(Elf_Word symvalue)
{
  if ((symvalue & 0x1) == 0x1)
    return true;
  else return false;
}

static inline Elf_SOff
sign_extend31(Elf_Addr val)
{
  if (0x40000000 & val)
    val =  ~((Elf_Addr)0x7fffffff) | (0x7fffffff & val);
  return 0x7fffffff & val;
}

uint32_t
rtems_rtl_elf_section_flags (const rtems_rtl_obj_t* obj,
                             const Elf_Shdr*        shdr)
{
  uint32_t flags = 0;
  if (shdr->sh_type == SHT_ARM_EXIDX)
    flags = RTEMS_RTL_OBJ_SECT_EH | RTEMS_RTL_OBJ_SECT_LOAD;
  return flags;
}

bool
rtems_rtl_elf_rel_resolve_sym (Elf_Word type)
{
  return true;
}

bool
rtems_rtl_elf_relocate_rela (const rtems_rtl_obj_t*      obj,
                             const Elf_Rela*             rela,
                             const rtems_rtl_obj_sect_t* sect,
                             const char*                 symname,
                             const Elf_Byte              syminfo,
                             const Elf_Word              symvalue)
{
  rtems_rtl_set_error (EINVAL, "rela type record not supported");
  return false;
}

bool
rtems_rtl_elf_relocate_rel (const rtems_rtl_obj_t*      obj,
                            const Elf_Rel*              rel,
                            const rtems_rtl_obj_sect_t* sect,
                            const char*                 symname,
                            const Elf_Byte              syminfo,
                            const Elf_Word              symvalue)
{
  Elf_Addr *where;
  Elf_Addr tmp;
  Elf_Word insn, addend;
  Elf_Word sign, i1, i2;
  uint16_t lower_insn, upper_insn;

  where = (Elf_Addr *)(sect->base + rel->r_offset);

  switch (ELF_R_TYPE(rel->r_info)) {
    case R_TYPE(NONE):
      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) {
        printf ("rtl: NONE %p in %s\n", where, rtems_rtl_obj_oname (obj));
      }
      break;

    case R_TYPE(CALL):    /* BL/BLX */
    case R_TYPE(JUMP24):  /* B/BL<cond> */
      insn = *where;

      if (insn & 0x00800000)
        addend = insn | 0xff000000;
      else addend = insn & 0x00ffffff;

      if (isThumb(symvalue)) {
        if ((insn & 0xfe000000) == 0xfa000000);         /* Already blx */
        else {
          if ((insn & 0xff000000) == 0xeb000000) {      /* BL <label> */
            *where = (insn & 0x00ffffff) | 0xfa000000;  /* BL-->BLX */
          } else {
            printf("JUMP24 is not suppored from arm to thumb\n");
            return false;
          }
        }
      }

      tmp = symvalue + (addend << 2) - (Elf_Addr)where;
      tmp = (Elf_Sword)tmp >> 2;

      if (((Elf_Sword)tmp > 0x7fffff) || ((Elf_Sword)tmp < -0x800000)) {
        printf("CALL/JUMP24 Overflow\n");
        return false;
      }

      *where = (*where & 0xff000000) | (tmp & 0xffffff);

      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
        printf ("rtl: JUMP24/PC24/CALL %p @ %p in %s\n",
                (void *)*where, where, rtems_rtl_obj_oname (obj));

      break;

    case R_TYPE(V4BX):
      /* Miscellaneous, ignore */
      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) {
        printf ("rtl: V4BX %p @ %p in %s\n",
                (void *)*where, where, rtems_rtl_obj_oname (obj));
      }
      break;

    case R_TYPE(MOVT_ABS):
    case R_TYPE(MOVW_ABS_NC):
      insn = *where;

      addend = ((insn >> 4) & 0xf000) | (insn & 0x0fff);
      if (addend & 0x8000)
        addend |= 0xffff0000;

      tmp = symvalue + addend;

      if (ELF_R_TYPE(rel->r_info) == R_TYPE(MOVW_ABS_NC))
        tmp &= 0xffff;
      else {
        tmp = (Elf_Sword)tmp >> 16;
        if (((Elf_Sword)tmp > 0x7fff) || ((Elf_Sword)tmp < -0x8000)) {
          printf("MOVT_ABS Overflow\n");
          return false;
        }
      }

      *where = (insn & 0xfff0f000) | ((tmp & 0xf000) << 4) | (tmp & 0xfff);

      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
        printf ("rtl: MOVT_ABS/MOVW_ABS_NC %p @ %p in %s\n",
                (void *)*where, where, rtems_rtl_obj_oname (obj));
      break;


    case R_TYPE(REL32):     /* word32 (S + A) | T - P */
    case R_TYPE(ABS32):     /* word32 (S + A) | T */
    case R_TYPE(GLOB_DAT):  /* word32 (S + A) | T */
    case R_TYPE(PREL31):    /* word32 (S + A) | T - P */
    case R_TYPE(TARGET2):   /* Equivalent to REL32 */
      if (__predict_true(RELOC_ALIGNED_P(where))) {
        tmp = *where + symvalue;
        if (isThumb(symvalue))
          tmp |= 1;
        if (ELF_R_TYPE(rel->r_info) == R_TYPE(REL32) ||
            ELF_R_TYPE(rel->r_info) == R_TYPE(TARGET2))
          tmp -= (Elf_Addr)where;
        else if (ELF_R_TYPE(rel->r_info) == R_TYPE(PREL31))
          tmp = sign_extend31(tmp - (Elf_Addr)where);
        *where = tmp;
      } else {
        tmp = load_ptr(where) + symvalue;
        if (isThumb(symvalue))
          tmp |= 1;
        if (ELF_R_TYPE(rel->r_info) == R_TYPE(REL32) ||
            ELF_R_TYPE(rel->r_info) == R_TYPE(TARGET2))
          tmp -= (Elf_Addr)where;
        else if (ELF_R_TYPE(rel->r_info) == R_TYPE(PREL31))
          tmp = sign_extend31(tmp - (Elf_Addr)where);
        store_ptr(where, tmp);
      }

      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
        printf ("rtl: REL32/ABS32/GLOB_DAT/PREL31/TARGET2 %p @ %p in %s\n",
                (void *)tmp, where, rtems_rtl_obj_oname (obj));
      break;

    case R_TYPE(THM_MOVT_ABS):
    case R_TYPE(THM_MOVW_ABS_NC):
      upper_insn = *(uint16_t *)where;
      lower_insn = *((uint16_t *)where + 1);

      addend = ((upper_insn & 0x000f) << 12) | ((upper_insn & 0x0400) << 1) |
               ((lower_insn & 0x7000) >> 4) | (lower_insn & 0x00ff);
      addend = (addend ^ 0x8000) - 0x8000;

      tmp = addend + symvalue;
      if (ELF32_R_TYPE(rel->r_info) == R_ARM_THM_MOVT_ABS)
        tmp >>= 16;

      *(uint16_t *)where = (uint16_t)((upper_insn & 0xfbf0) |
                                     ((tmp & 0xf000) >> 12) |
                                     ((tmp & 0x0800) >> 1));
      *((uint16_t *)where + 1) = (uint16_t)((lower_insn & 0x8f00) |
                                           ((tmp & 0x0700) << 4) |
                                           (tmp & 0x00ff));

      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)) {
        printf ("rtl: THM_MOVT_ABS/THM_MOVW_ABS_NC %p @ %p in %s\n",
                (void *)*where, where, rtems_rtl_obj_oname (obj));
      }

      break;

    case R_TYPE(THM_JUMP24):
      /* same to THM_CALL; insn b.w */
    case R_TYPE(THM_CALL):
      upper_insn = *(uint16_t *)where;
      lower_insn = *((uint16_t *)where + 1);
      sign = (upper_insn & (1 << 10)) >> 10;
      i1 = ((lower_insn >> 13) & 1) ^ sign ? 0 : 1;
      i2 = ((lower_insn >> 11) & 1) ^ sign ? 0 : 1;
      tmp = (i1 << 23) | (i2 << 22) | ((upper_insn & 0x3ff) << 12) | ((lower_insn & 0x7ff) << 1);
      addend = (tmp | ((sign ? 0 : 1) << 24)) - (1 << 24);

      if (isThumb(symvalue)) ;/*Thumb to Thumb call, nothing to care */
      else {
        if (ELF_R_TYPE(rel->r_info) == R_TYPE(THM_JUMP24)) {
          tmp = (tmp + 2) & ~3; /* aligned to 4 bytes only for JUMP24 */
          printf("THM_JUMP24 to arm not supported\n");
          return false;
        }
        else {
          /* THM_CALL bl-->blx */
          lower_insn &=~(1<<12);
        }
      }

      tmp = symvalue + addend;
      tmp = tmp - (Elf_Addr)where;

      if (((Elf_Sword)tmp > 0x7fffff) || ((Elf_Sword)tmp < -0x800000)) {
        printf("THM_CALL/JUMP24 overflow\n");
        return false;
      }

      sign = (tmp >> 24) & 1;
      *(uint16_t *)where = (uint16_t)((upper_insn & 0xf800) | (sign << 10) |
                                     ((tmp >> 12) & 0x3ff));

      *((uint16_t *)where + 1) = (uint16_t)((lower_insn & 0xd000)|
                                           ((sign ^ (~(tmp >> 23) & 1)) << 13) |
                                           ((sign ^ (~(tmp >> 22) & 1)) << 11) |
                                           ((tmp >> 1) & 0x7ff));

      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC)){
        printf ("rtl: THM_CALL/JUMP24 %p @ %p in %s\n",
                (void *)*where, where, rtems_rtl_obj_oname (obj));
      }

      break;

    case R_TYPE(THM_JUMP19):

      if (!isThumb(symvalue)) {
        printf("THM_JUMP19 to arm not supported\n");
        return false;
      }

      upper_insn = *(uint16_t *)where;
      lower_insn = *((uint16_t *)where + 1);
      sign = (upper_insn >> 10) & 0x1;

      if ((((upper_insn & 0x3f) >> 7) & 0x7) == 0x7) {
        printf("THM_JUMP19 failed\n");
        return false; /*if cond <3:1> == '111', see Related codings in armv7a manual */
      }

      i1 = (lower_insn >> 13) & 0x1;
      i2 = (lower_insn >> 11) & 0x1;

      tmp = ((i2 << 19) | (i1 << 18) | ((upper_insn & 0x3f) << 12) | ((lower_insn & 0x7ff) << 1));
      addend = (tmp | ((sign ? 0 : 1) << 20)) - (1 << 20);
      tmp = symvalue + addend;

      tmp = tmp - (Elf_Addr)where;

      if (((Elf_Sword)tmp > 0x7ffffe) || ((Elf_Sword)tmp < -0x800000)) {
        rtems_rtl_set_error (EINVAL, "%s: Overflow %ld "
                             "THM_JUMP19 relocations",
                             sect->name, (uint32_t) ELF_R_TYPE(rel->r_info));
        return false;
      }

      sign = (tmp >> 20) & 0x1;
      i2 = (tmp >> 19) & 0x1;
      i1 = (tmp >> 18) & 0x1;

      *(uint16_t*)where = (upper_insn & 0xfbc0) | (sign << 10) | ((tmp >> 12) & 0x3f);
      *((uint16_t*)where + 1) = (lower_insn & 0xd000) | (i1 << 13) |
                                (i2 << 11) | ((tmp >> 1) & 0x7ff);

      if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
        printf ("rtl: THM_JUMP19 %p @ %p in %s\n",
                (void *)*where, where, rtems_rtl_obj_oname (obj));
      break;

    default:
      printf ("rtl: reloc unknown: sym = %lu, type = %lu, offset = %p, "
              "contents = %p\n",
              ELF_R_SYM(rel->r_info), (uint32_t) ELF_R_TYPE(rel->r_info),
              (void *)rel->r_offset, (void *)*where);
      rtems_rtl_set_error (EINVAL,
                           "%s: Unsupported relocation type %ld "
                           "in non-PLT relocations",
                           sect->name, (uint32_t) ELF_R_TYPE(rel->r_info));
      return false;
  }

  return true;
}

bool
rtems_rtl_elf_unwind_parse (const rtems_rtl_obj_t* obj,
                            const char*            name,
                            uint32_t               flags)
{
  /*
   * We location the EH sections in section flags.
   */
  return false;
}

bool
rtems_rtl_elf_unwind_register (rtems_rtl_obj_t* obj)
{
  return true;
}

bool
rtems_rtl_elf_unwind_deregister (rtems_rtl_obj_t* obj)
{
  obj->loader = NULL;
  return true;
}

/* An exception index table entry.  */
typedef struct __EIT_entry
{
  _uw fnoffset;
  _uw content;
} __EIT_entry;

/* The exception index table location in the base module */
extern __EIT_entry __exidx_start;
extern __EIT_entry __exidx_end;

/*
 * A weak reference is in libgcc, provide a real version and provide a way to
 * manage loaded modules.
 *
 * Passed in the return address and a reference to the number of records
 * found. We set the start of the exidx data and the number of records.
 */
_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address,
                                     int*        nrec) __attribute__ ((__noinline__,
                                                                       __used__,
                                                                       __noclone__));

_Unwind_Ptr __gnu_Unwind_Find_exidx (_Unwind_Ptr return_address,
                                     int*        nrec)
{
  rtems_rtl_data_t* rtl;
  rtems_chain_node* node;
  __EIT_entry*      exidx_start = &__exidx_start;
  __EIT_entry*      exidx_end = &__exidx_end;

  rtl = rtems_rtl_lock ();

  node = rtems_chain_first (&rtl->objects);
  while (!rtems_chain_is_tail (&rtl->objects, node)) {
    rtems_rtl_obj_t* obj = (rtems_rtl_obj_t*) node;
    if (rtems_rtl_obj_text_inside (obj, (void*) return_address)) {
      exidx_start = (__EIT_entry*) obj->eh_base;
      exidx_end = (__EIT_entry*) (obj->eh_base + obj->eh_size);
      break;
    }
    node = rtems_chain_next (node);
  }

  rtems_rtl_unlock ();

  *nrec = exidx_end - exidx_start;

  return (_Unwind_Ptr) exidx_start;
}