summaryrefslogtreecommitdiffstats
path: root/cpukit/libdl/rtl-mdreloc-aarch64.c
blob: c60ecb31c92c5a47cf7009305d3a03ce188c70f2 (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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/*
 * Taken from NetBSD and stripped of the relocations not needed on RTEMS.
 */

/* $NetBSD: mdreloc.c,v 1.14 2020/06/16 21:01:30 joerg Exp $ */

/*-
 * Copyright (c) 2014 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Matt Thomas of 3am Software Foundry.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*-
 * Copyright (c) 2014-2015 The FreeBSD Foundation
 * All rights reserved.
 *
 * Portions of this software were developed by Andrew Turner
 * under sponsorship from the FreeBSD Foundation.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: mdreloc.c,v 1.14 2020/06/16 21:01:30 joerg Exp $");
#endif /* not lint */

#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <sys/endian.h>

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

struct tls_data {
  size_t    td_tlsindex;
  Elf_Addr  td_tlsoffs;
};

rtems_rtl_elf_rel_status
rtems_rtl_elf_reloc_rela (
  rtems_rtl_obj*            obj,
  const Elf_Rela*           rela,
  const rtems_rtl_obj_sect* sect,
  const char*               symname,
  const Elf_Byte            syminfo,
  const Elf_Word            symvalue,
  const bool                parsing
);

#define __BITS(hi,lo) ((~((~(Elf_Addr)0)<<((hi)+1)))&((~(Elf_Addr)0)<<(lo)))
#define WIDTHMASK(w) (0xffffffffffffffffUL >> (64 - (w)))

static inline bool
checkoverflow(Elf_Addr addr, int bitwidth, Elf_Addr targetaddr,
    const char *bitscale, void *where, Elf64_Addr off)
{
  const Elf_Addr mask = ~__BITS(bitwidth - 1, 0);

  if (((addr & mask) != 0) && ((addr & mask) != mask)) {
    printf("kobj_reloc: Relocation 0x%jx too far from %p"
        " (base+0x%jx) for %dbit%s\n",
        (uintptr_t)targetaddr, where, off, bitwidth, bitscale);
    return true;
  }

  return false;
}

static inline bool
checkalign(Elf_Addr addr, int alignbyte, void *where, Elf64_Addr off)
{
  if ((addr & (alignbyte - 1)) != 0) {
    printf("kobj_reloc: Relocation 0x%jx unaligned at %p"
        " (base+0x%jx). must be aligned %d\n",
        (uintptr_t)addr, where, off, alignbyte);
    return true;
  }
  return false;
}

/*
 * Set to 1 to allow untested relocations. If you tested one and it
 * works or you fixed the relocation please remove the guard.
 */
#define ALLOW_UNTESTED_RELOCS 1

static void*
set_veneer(void* tramopline, Elf_Addr target)
{
  /*
   * http://shell-storm.org/online/Online-Assembler-and-Disassembler/
   *
   * ldr x9, #8
   * br x9
   *
   */
  uint64_t* tramp = (uint64_t*) tramopline;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  *tramp++ = 0xd61f012058000049;
#else
  *tramp++ = 0xd61f012058000049; /* not tested */
#endif
  *tramp++ = (uint64_t) target;
  return tramp;
}

static size_t
get_veneer_size(int type)
{
  (void) type;
  return 16;
}

size_t
rtems_rtl_elf_relocate_tramp_max_size (void)
{
  return 16;
}

uint32_t
rtems_rtl_elf_section_flags (const rtems_rtl_obj* obj,
                             const Elf_Shdr*      shdr)
{
  return 0;
}

uint32_t
rtems_rtl_elf_arch_parse_section (const rtems_rtl_obj* obj,
                                  int                  section,
                                  const char*          name,
                                  const Elf_Shdr*      shdr,
                                  const uint32_t       flags)
{
  (void) obj;
  (void) section;
  (void) name;
  (void) shdr;
  return flags;
}

bool
rtems_rtl_elf_arch_section_alloc (const rtems_rtl_obj* obj,
                                  rtems_rtl_obj_sect*  sect)
{
  (void) obj;
  (void) sect;
  return false;
}

bool
rtems_rtl_elf_arch_section_free (const rtems_rtl_obj* obj,
                                 rtems_rtl_obj_sect*  sect)
{
  (void) obj;
  (void) sect;
  return false;
}

bool
rtems_rtl_elf_rel_resolve_sym (Elf_Word type)
{
  return true;
}

rtems_rtl_elf_rel_status
rtems_rtl_elf_reloc_rela (rtems_rtl_obj*            obj,
                          const Elf_Rela*           rela,
                          const rtems_rtl_obj_sect* sect,
                          const char*               symname,
                          const Elf_Byte            syminfo,
                          const Elf_Word            symvalue,
                          const bool                parsing)
{
  Elf64_Addr *where;
  Elf32_Addr *where32;
  Elf_Addr    off = rela->r_offset;
  Elf_Addr    target;
  Elf_Addr    raddr;
  uint32_t   *insn, immhi, immlo, shift;

  where = (Elf_Addr *)(sect->base + rela->r_offset);
  where32 = (void *)where;

  insn = (uint32_t *)where;

  /*
   * S - the address of the symbol
   * A - the addend of the reolcation
   * P - the address of the place being relocated (derived from r_offset)
   * Page(expr) - the page address of the expression expr, defined as (expr & ~0xFFF).
   */
  switch (ELF_R_TYPE(rela->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(ABS64): /* word S + A */
    case R_TYPE(GLOB_DAT): /* word S + A */
      if (!parsing) {
        target = (Elf_Addr)symvalue + rela->r_addend;

        if (*where != target)
          *where = target;

        if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
          printf ("rtl: reloc 64/GLOB_DAT in %s --> %p in %s\n",
                  sect->name, (void *)*where,
                  rtems_rtl_obj_oname (obj));
      }
      break;

    /*
     * If S is a normal symbol, resolves to the difference between the static
     * link address of S and the execution address of S. If S is the null symbol
     * (ELF symbol index 0), resolves to the difference between the static link
     * address of P and the execution address of P.
     */
    case R_TYPE(RELATIVE):  /* Delta(S) + A */
      if (!parsing) {
        *where = (Elf_Addr)(sect->base + rela->r_addend);
        if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
          printf ("rtl: reloc RELATIVE in %s --> %p in %s\n",
                  sect->name, (void *)*where,
                  rtems_rtl_obj_oname (obj));
      }
      break;

    case R_TYPE(COPY):
      /*
       * These are deferred until all other relocations have
       * been done.  All we do here is make sure that the
       * COPY relocation is not in a shared library.  They
       * are allowed only in executable files.
       */
      printf("rtl: reloc COPY (please report)\n");
      break;

    case R_AARCH64_ADD_ABS_LO12_NC: /* S + A */
    case R_AARCH64_LDST8_ABS_LO12_NC:
    case R_AARCH_LDST16_ABS_LO12_NC:
    case R_AARCH_LDST32_ABS_LO12_NC:
    case R_AARCH_LDST64_ABS_LO12_NC:
      switch (ELF_R_TYPE(rela->r_info)) {
        case R_AARCH64_ADD_ABS_LO12_NC:
        case R_AARCH64_LDST8_ABS_LO12_NC:
          shift = 0;
          break;
        case R_AARCH_LDST16_ABS_LO12_NC:
          shift = 1;
          break;
        case R_AARCH_LDST32_ABS_LO12_NC:
          shift = 2;
          break;
        case R_AARCH_LDST64_ABS_LO12_NC:
          shift = 3;
          break;
        default:
          printf("illegal rtype: %ld\n", ELF_R_TYPE(rela->r_info));
          break;
      }

      /*
       * S + A
       *  e.g.) add  x0,x0,#:lo12:<sym>+<addend>
       *        ldrb w0,[x0,#:lo12:<sym>+<addend>]
       *        ldrh w0,[x0,#:lo12:<sym>+<addend>]
       *        ldr  w0,[x0,#:lo12:<sym>+<addend>]
       *        ldr  x0,[x0,#:lo12:<sym>+<addend>]
       */
      if (!parsing) {
        target = (Elf_Addr)symvalue + rela->r_addend;
        if (checkalign(target, 1 << shift, where, off)) {
          printf ("rtl: reloc checkalign failed in %s --> %p in %s\n",
                  sect->name, (void *)*where,
                  rtems_rtl_obj_oname (obj));
          printf("ELF_R_TYPE is : %ld\n", ELF_R_TYPE(rela->r_info));
          break;
        }
        target &= WIDTHMASK(12);
        target >>= shift;
        *insn = htole32(
            (le32toh(*insn) & ~__BITS(21,10)) | (target << 10));
      }
      break;

    case R_AARCH64_ADR_PREL_PG_HI21:
      /*
       * Page(S + A) - Page(P)
       *  e.g.) adrp x0,<sym>+<addend>
       */
      if (!parsing) {
        target = (Elf_Addr)symvalue + rela->r_addend;
        target = target >> 12;
        raddr = target << 12;
        target -= (uintptr_t)where >> 12;

        if (checkoverflow(target, 21, raddr, " x 4k", where, off)) {
          return rtems_rtl_elf_rel_failure;
        }

        immlo = target & WIDTHMASK(2);
        immhi = (target >> 2) & WIDTHMASK(19);
        *insn = htole32((le32toh(*insn) &
            ~(__BITS(30,29) | __BITS(23,5))) |
            (immlo << 29) | (immhi << 5));
      }
      break;

    case R_AARCH_JUMP26:
    case R_AARCH_CALL26:
      /*
       * S + A - P
       *  e.g.) b <sym>+<addend>
       *        bl <sym>+<addend>
       */
      if (parsing && sect->base == 0) {
        if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
          printf ("rtl: JUMP26/PC26/CALL tramp cache\n");
        return rtems_rtl_elf_rel_tramp_cache;
      }

      raddr = (Elf_Addr)symvalue + rela->r_addend;
      target = raddr - (uintptr_t)where;
      if (checkalign(target, 4, where, off)) {
        return rtems_rtl_elf_rel_failure;
      }

      target = (intptr_t)target >> 2;

      if (((Elf_Sword)target > 0x1FFFFFF) || ((Elf_Sword)target < -0x2000000)) {
        Elf_Word tramp_addr;
        size_t   tramp_size = get_veneer_size(ELF_R_TYPE(rela->r_info));

        if (parsing) {
          if (rtems_rtl_trace (RTEMS_RTL_TRACE_RELOC))
            printf ("rtl: JUMP26/PC26/CALL tramp add\n");
          return rtems_rtl_elf_rel_tramp_add;
        }

        if (!rtems_rtl_obj_has_tramp_space (obj, tramp_size)) {
          rtems_rtl_set_error (EINVAL,
                               "%s: CALL/JUMP26: overflow: no tramp memory",
                               sect->name);
          return rtems_rtl_elf_rel_failure;
        }

        tramp_addr = ((Elf_Addr) obj->tramp_brk) | (symvalue & 1);
        obj->tramp_brk = set_veneer(obj->tramp_brk, symvalue);

        target = tramp_addr + rela->r_addend - (uintptr_t)where;
        target = (uintptr_t)target >> 2;
      }

      if (checkoverflow(target, 26, raddr, " word", where, off)) {
        return rtems_rtl_elf_rel_failure;
      }

      if (!parsing) {
        target &= WIDTHMASK(26);
        *insn = htole32((le32toh(*insn) & ~__BITS(25,0)) | target);
      }

      break;

    case R_AARCH64_PREL32:
      /*
       * S + A - P
       *  e.g.) 1: .word <sym>+<addend>-1b
       */
      if (!parsing) {
        raddr = (Elf_Addr)symvalue + rela->r_addend;
        target = raddr - (uintptr_t)where;
        if (checkoverflow(target, 32, raddr, "", where, off)) {
          return rtems_rtl_elf_rel_failure;
        }
        *where32 = target;
      }
      break;

    case R_TYPE(TLSDESC):
      printf ("rtl: reloc TLSDESC in %s --> %p in %s\n",
              sect->name, (void *)*where,
              rtems_rtl_obj_oname (obj));
      break;

    case R_TLS_TYPE(TLS_DTPREL):
      printf ("rtl: reloc TLS_DTPREL in %s --> %p in %s\n",
              sect->name, (void *)*where,
              rtems_rtl_obj_oname (obj));
      break;
    case R_TLS_TYPE(TLS_DTPMOD):
      printf ("rtl: reloc TLS_DTPMOD in %s --> %p in %s\n",
              sect->name, (void *)*where,
              rtems_rtl_obj_oname (obj));
      break;

    case R_TLS_TYPE(TLS_TPREL):
      printf ("rtl: reloc TLS_TPREL in %s --> %p in %s\n",
              sect->name, (void *)*where,
              rtems_rtl_obj_oname (obj));
      break;

    default:
      printf ("rtl: Unsupported relocation type in %s --> %p in %s\n",
              sect->name, (void *)where,
              rtems_rtl_obj_oname (obj));
      return rtems_rtl_elf_rel_failure;
  }

  return rtems_rtl_elf_rel_no_error;
}

rtems_rtl_elf_rel_status
rtems_rtl_elf_relocate_rela_tramp (rtems_rtl_obj*            obj,
                                   const Elf_Rela*           rela,
                                   const rtems_rtl_obj_sect* sect,
                                   const char*               symname,
                                   const Elf_Byte            syminfo,
                                   const Elf_Word            symvalue)
{
  return rtems_rtl_elf_reloc_rela (obj,
                                   rela,
                                   sect,
                                   symname,
                                   syminfo,
                                   symvalue,
                                   true);
}

rtems_rtl_elf_rel_status
rtems_rtl_elf_relocate_rela (rtems_rtl_obj*            obj,
                             const Elf_Rela*           rela,
                             const rtems_rtl_obj_sect* sect,
                             const char*               symname,
                             const Elf_Byte            syminfo,
                             const Elf_Word            symvalue)
{
  return rtems_rtl_elf_reloc_rela (obj,
                                   rela,
                                   sect,
                                   symname,
                                   syminfo,
                                   symvalue,
                                   false);
}

rtems_rtl_elf_rel_status
rtems_rtl_elf_relocate_rel_tramp (rtems_rtl_obj*            obj,
                                  const Elf_Rel*            rel,
                                  const rtems_rtl_obj_sect* sect,
                                  const char*               symname,
                                  const Elf_Byte            syminfo,
                                  const Elf_Word            symvalue)
{
  rtems_rtl_set_error (EINVAL, "rela type record not supported");
  return rtems_rtl_elf_rel_failure;
}

rtems_rtl_elf_rel_status
rtems_rtl_elf_relocate_rel (rtems_rtl_obj*            obj,
                            const Elf_Rel*            rel,
                            const rtems_rtl_obj_sect* sect,
                            const char*               symname,
                            const Elf_Byte            syminfo,
                            const Elf_Word            symvalue)
{
  rtems_rtl_set_error (EINVAL, "rela type record not supported");
  return rtems_rtl_elf_rel_failure;
}