mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Barebox List <barebox@lists.infradead.org>
Subject: Re: [PATCH 30/78] ARM: aarch64: Add relocation support
Date: Fri, 16 Mar 2018 14:51:19 -0700	[thread overview]
Message-ID: <CAHQ1cqGbkHN+Csi7-Bb=_iTea4zXK3-mmK8EK0x-UW9niHNCQg@mail.gmail.com> (raw)
In-Reply-To: <20180316125354.23462-31-s.hauer@pengutronix.de>

On Fri, Mar 16, 2018 at 5:53 AM, Sascha Hauer <s.hauer@pengutronix.de> wrote:
> This adds aarch64 support for relocating binaries linked with -pie.
>
> Support is integrated into the already exisiting
> relocate_to_current_adr() function which is now used for both arm32
> and aarch64.
>
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>


Sascha:

Two small suggestions w.r.t. this patch:

 - I'd consider changing the code of relocate_to_current_adr() such
that AARCH64 specific codepaths are not taken on ARM32 (via IS_ENABLED
check or something similar)

 - I've always wanted to fix the original code to use Elf32_rel type
instead of magic hard-coded offsets, so depending on your
willingness/time-budget, maybe now would be a good time to do that as
well as use Elf64_rela for AARCH64?

Thanks,
Andrey Smirnov

> ---
>  arch/arm/cpu/common.c    | 38 ++++++++++++++++++++++++-------
>  arch/arm/cpu/setupc_64.S | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
>  common/Kconfig           |  2 +-
>  3 files changed, 89 insertions(+), 9 deletions(-)
>
> diff --git a/arch/arm/cpu/common.c b/arch/arm/cpu/common.c
> index 3766116d97..c317e502d0 100644
> --- a/arch/arm/cpu/common.c
> +++ b/arch/arm/cpu/common.c
> @@ -24,39 +24,61 @@
>  #include <asm-generic/memory_layout.h>
>  #include <asm/sections.h>
>  #include <asm/cache.h>
> +#include <debug_ll.h>
> +
> +#define R_ARM_RELATIVE 23
> +#define R_AARCH64_RELATIVE 1027
>
>  /*
>   * relocate binary to the currently running address
>   */
>  void relocate_to_current_adr(void)
>  {
> -       unsigned long offset;
> +       unsigned long offset, offset_var;
>         unsigned long *dstart, *dend, *dynsym, *dynend;
>
>         /* Get offset between linked address and runtime address */
>         offset = get_runtime_offset();
> +       offset_var = global_variable_offset();
>
> -       dstart = (void *)__rel_dyn_start + offset;
> -       dend = (void *)__rel_dyn_end + offset;
> +       dstart = (void *)__rel_dyn_start + offset_var;
> +       dend = (void *)__rel_dyn_end + offset_var;
>
> -       dynsym = (void *)__dynsym_start + offset;
> -       dynend = (void *)__dynsym_end + offset;
> +       dynsym = (void *)__dynsym_start + offset_var;
> +       dynend = (void *)__dynsym_end + offset_var;
>
>         while (dstart < dend) {
>                 unsigned long *fixup = (unsigned long *)(*dstart + offset);
>                 unsigned long type = *(dstart + 1);
> +               int add;
> +
> +               if (ELF64_R_TYPE(type) == R_AARCH64_RELATIVE) {
> +                       unsigned long addend = *(dstart + 2);
>
> -               if ((type & 0xff) == 0x17) {
> +                       *fixup = addend + offset;
> +
> +                       add = 3;
> +               } else if (ELF32_R_TYPE(type) == R_ARM_RELATIVE) {
>                         *fixup = *fixup + offset;
> -               } else {
> +
> +                       add = 2;
> +               } else if (ELF32_R_TYPE(type) == R_ARM_ABS32) {
>                         int index = type >> 8;
>                         unsigned long r = dynsym[index * 4 + 1];
>
>                         *fixup = *fixup + r + offset;
> +
> +                       add = 2;
> +               } else {
> +                       putc_ll('>');
> +                       puthex_ll(type);
> +                       putc_ll('\n');
> +                       /* We're doomed */
> +                       panic(NULL);
>                 }
>
>                 *dstart += offset;
> -               dstart += 2;
> +               dstart += add;
>         }
>
>         memset(dynsym, 0, (unsigned long)dynend - (unsigned long)dynsym);
> diff --git a/arch/arm/cpu/setupc_64.S b/arch/arm/cpu/setupc_64.S
> index 3515854784..88c7899205 100644
> --- a/arch/arm/cpu/setupc_64.S
> +++ b/arch/arm/cpu/setupc_64.S
> @@ -16,3 +16,61 @@ ENTRY(setup_c)
>         mov     x30, x15
>         ret
>  ENDPROC(setup_c)
> +
> +/*
> + * void relocate_to_adr(unsigned long targetadr)
> + *
> + * Copy binary to targetadr, relocate code and continue
> + * executing at new address.
> + */
> +.section .text.relocate_to_adr
> +ENTRY(relocate_to_adr)
> +                                       /* x0: target address */
> +
> +       stp     x19, x20, [sp, #-16]!
> +
> +       mov     x19, lr
> +
> +       mov     x6, x0
> +
> +       bl      get_runtime_offset
> +       mov     x5, x0
> +
> +       ldr     x0, =_text
> +       mov     x8, x0
> +
> +       add     x1, x0, x5              /* x1: from address */
> +
> +       cmp     x1, x6                  /* already at correct address? */
> +       beq     1f                      /* yes, skip copy to new address */
> +
> +       ldr     x2, =__bss_start
> +
> +       sub     x2, x2, x0              /* x2: size */
> +       mov     x0, x6                  /* x0: target */
> +
> +       /* adjust return address */
> +       sub     x19, x19, x1            /* sub address where we are actually running */
> +       add     x19, x19, x0            /* add address where we are going to run */
> +
> +       bl      memcpy                  /* copy binary */
> +
> +#ifdef CONFIG_MMU
> +       bl      arm_early_mmu_cache_flush
> +#endif
> +       mov     x0,#0
> +       ic      ivau, x0        /* flush icache */
> +
> +       ldr     x0,=1f
> +       sub     x0, x0, x8
> +       add     x0, x0, x6
> +       br      x0                      /* jump to relocated address */
> +1:
> +       bl      relocate_to_current_adr /* relocate binary */
> +
> +       mov     lr, x19
> +
> +       ldp     x19, x20, [sp], #16
> +       ret
> +
> +ENDPROC(relocate_to_adr)
> diff --git a/common/Kconfig b/common/Kconfig
> index af71d6888a..b7000c4d73 100644
> --- a/common/Kconfig
> +++ b/common/Kconfig
> @@ -344,7 +344,7 @@ config KALLSYMS
>           This is useful to print a nice backtrace when an exception occurs.
>
>  config RELOCATABLE
> -       depends on PPC || (ARM && !CPU_V8)
> +       depends on PPC || ARM
>         bool "generate relocatable barebox binary"
>         help
>           A non relocatable barebox binary will run at it's compiled in
> --
> 2.16.1
>
>
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2018-03-16 21:51 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-16 12:52 [PATCH 00/78] ARM aarch64 updates Sascha Hauer
2018-03-16 12:52 ` [PATCH 01/78] ARM: Use obj-pbl- where appropriate Sascha Hauer
2018-03-16 12:52 ` [PATCH 02/78] ARM: Add 64bit compilation alternative Sascha Hauer
2018-03-16 12:52 ` [PATCH 03/78] ARM: return positive offset in get_runtime_offset() Sascha Hauer
2018-03-16 12:52 ` [PATCH 04/78] ARM: mmu: include pgtable header from where it's needed Sascha Hauer
2018-03-16 12:52 ` [PATCH 05/78] ARM: For relocatable image force TEXT_BASE 0x0 Sascha Hauer
2018-03-16 12:52 ` [PATCH 06/78] ARM: scroll past image end without ld_var Sascha Hauer
2018-03-16 12:52 ` [PATCH 07/78] ARM: move away from ld_var Sascha Hauer
2018-03-16 12:52 ` [PATCH 08/78] ARM: remove ld_var support Sascha Hauer
2018-03-16 12:52 ` [PATCH 09/78] ARM: android image: Fix compiler warning on aarch64 Sascha Hauer
2018-03-16 12:52 ` [PATCH 10/78] ARM: bootm: Fix wrong format specifier Sascha Hauer
2018-03-16 12:52 ` [PATCH 11/78] ARM: shutdown: Fix compiler warning Sascha Hauer
2018-03-16 12:52 ` [PATCH 12/78] ARM: aarch64: silence " Sascha Hauer
2018-03-16 12:52 ` [PATCH 13/78] ARM: aarch64: Add dummy naked attribute Sascha Hauer
2018-03-16 12:52 ` [PATCH 14/78] ARM: get_runtime_offset() returns unsigned long Sascha Hauer
2018-03-16 12:52 ` [PATCH 15/78] ARM: aarch64: Add runtime-offset Sascha Hauer
2018-03-16 12:52 ` [PATCH 16/78] ARM: aarch64: implement get_pc() Sascha Hauer
2018-03-16 12:52 ` [PATCH 17/78] ARM: Use generic ffz() Sascha Hauer
2018-03-16 12:52 ` [PATCH 18/78] ARM: bitops: remove unnecessary #ifdef Sascha Hauer
2018-03-16 12:52 ` [PATCH 19/78] ARM: aarch64: Do not use 32bit optimized fls Sascha Hauer
2018-03-16 12:52 ` [PATCH 20/78] ARM: Move mmu_disable to mmu.c Sascha Hauer
2018-03-16 12:52 ` [PATCH 21/78] debug_ll: support 64bit longs Sascha Hauer
2018-03-16 12:52 ` [PATCH 22/78] ARM: aarch64: fix early cache flushing Sascha Hauer
2018-03-16 12:52 ` [PATCH 23/78] ARM: aarch64: cache: Add v8_inv_dcache_range Sascha Hauer
2018-03-16 12:53 ` [PATCH 24/78] ARM: aarch64: cache: no need to ifdef prototypes Sascha Hauer
2018-03-16 12:53 ` [PATCH 25/78] ARM: Add function to return offset to global variables Sascha Hauer
2018-03-16 12:53 ` [PATCH 26/78] ARM: remove function prototypes from the past Sascha Hauer
2018-03-16 12:53 ` [PATCH 27/78] ARM: move linker variable declarations to sections.h Sascha Hauer
2018-03-16 12:53 ` [PATCH 28/78] ARM: relocate_to_current_adr: Use unsigned long for variables Sascha Hauer
2018-03-16 12:53 ` [PATCH 29/78] clocksource: Add armv8 generic timer support Sascha Hauer
2018-03-16 12:53 ` [PATCH 30/78] ARM: aarch64: Add relocation support Sascha Hauer
2018-03-16 21:51   ` Andrey Smirnov [this message]
2018-03-19  8:50     ` Sascha Hauer
2018-03-21  5:27       ` Andrey Smirnov
2018-03-21 11:26   ` [PATCH v2 " Sascha Hauer
2018-03-16 12:53 ` [PATCH 31/78] ARM: aarch64: fix pbl linker script for aarch64 Sascha Hauer
2018-03-16 12:53 ` [PATCH 32/78] ARM: aarch64: mmu: Allocate page tables dynamically Sascha Hauer
2018-03-16 12:53 ` [PATCH 33/78] ARM: aarch64: mmu: create_sections() takes size in bytes Sascha Hauer
2018-03-16 12:53 ` [PATCH 34/78] ARM: aarch64: mmu: fix creation of flat mapping Sascha Hauer
2018-03-16 12:53 ` [PATCH 35/78] ARM: aarch64: mmu: remove unused map_io_sections() Sascha Hauer
2018-03-16 12:53 ` [PATCH 36/78] ARM: aarch64: mmu: by default map as device memory Sascha Hauer
2018-03-16 12:53 ` [PATCH 37/78] ARM: aarch64: mmu: Fix mair register setting Sascha Hauer
2018-03-16 12:53 ` [PATCH 38/78] ARM: aarch64: qemu board: remove unnecessary mapping Sascha Hauer
2018-03-16 12:53 ` [PATCH 39/78] ARM: aarch64: mmu: enable mmu in generic code Sascha Hauer
2018-03-16 12:53 ` [PATCH 40/78] ARM: aarch64: mmu: use PTE_* definitions from U-Boot Sascha Hauer
2018-03-16 12:53 ` [PATCH 41/78] ARM: aarch64: mmu: Fix adding additional page table levels Sascha Hauer
2018-03-16 12:53 ` [PATCH 42/78] ARM: aarch64: mmu: Fix PTE_TYPE_* flags Sascha Hauer
2018-03-16 12:53 ` [PATCH 43/78] ARM: aarch64: mmu: Fix TCR setting Sascha Hauer
2018-03-16 12:53 ` [PATCH 44/78] ARM: aarch64: mmu: No need to disable icache Sascha Hauer
2018-03-16 12:53 ` [PATCH 45/78] ARM: aarch64: mmu: drop ttb check when disabling the MMU Sascha Hauer
2018-03-16 12:53 ` [PATCH 46/78] ARM: aarch64: mmu: Fix " Sascha Hauer
2018-03-16 12:53 ` [PATCH 47/78] ARM: Make some variables 64bit aware Sascha Hauer
2018-03-16 12:53 ` [PATCH 48/78] dma: Use dma_addr_t as type for DMA addresses Sascha Hauer
2018-03-16 12:53 ` [PATCH 49/78] dma: Add prototypes for dma mapping functions Sascha Hauer
2018-03-16 12:53 ` [PATCH 50/78] ARM: implement " Sascha Hauer
2018-03-16 12:53 ` [PATCH 51/78] ARM: aarch64: implement dma operations Sascha Hauer
2018-03-16 12:53 ` [PATCH 52/78] ARM: aarch64: compile with strict alignment Sascha Hauer
2018-03-16 12:53 ` [PATCH 53/78] ARM: aarch64: move aarch64 exception support to separate file Sascha Hauer
2018-03-16 12:53 ` [PATCH 54/78] ARM: aarch64: fix exception level mixup Sascha Hauer
2018-03-16 12:53 ` [PATCH 55/78] ARM: aarch64: Setup exception vectors in initcall Sascha Hauer
2018-03-16 12:53 ` [PATCH 56/78] ARM: aarch64: lowlevel: Use switch_el Sascha Hauer
2018-03-16 12:53 ` [PATCH 57/78] ARM: aarch64: remove dead code in linker script Sascha Hauer
2018-03-16 12:53 ` [PATCH 58/78] ARM: aarch64: hide some config options Sascha Hauer
2018-03-16 12:53 ` [PATCH 59/78] ARM: aarch64: implement show_regs() Sascha Hauer
2018-03-16 12:53 ` [PATCH 60/78] ARM: aarch64: implement stacktraces Sascha Hauer
2018-03-16 12:53 ` [PATCH 61/78] ARM: aarch64: mmu: Make zero page faulting Sascha Hauer
2018-03-21  4:53   ` Andrey Smirnov
2018-04-09  6:55     ` Sascha Hauer
2018-03-16 12:53 ` [PATCH 62/78] ARM: aarch64: Allow to leave exceptions Sascha Hauer
2018-03-16 12:53 ` [PATCH 63/78] ARM: aarch64: Add esr strings Sascha Hauer
2018-03-16 12:53 ` [PATCH 64/78] ARM: aarch64: print more information on sync exception Sascha Hauer
2018-03-16 12:53 ` [PATCH 65/78] ARM: aarch64: implement ignoring data aborts Sascha Hauer
2018-03-16 12:53 ` [PATCH 66/78] dt-bindings: Drop unused files Sascha Hauer
2018-03-16 12:53 ` [PATCH 67/78] ARM: aarch64: Add barebox head support Sascha Hauer
2018-03-16 12:53 ` [PATCH 68/78] filetype: Detect ARM aarch64 Linux images Sascha Hauer
2018-03-16 12:53 ` [PATCH 69/78] common: Add functions to find free RAM Sascha Hauer
2018-03-16 12:53 ` [PATCH 70/78] bootm: provide handlers the start of the OS image Sascha Hauer
2018-03-16 12:53 ` [PATCH 71/78] ARM: aarch64: disable 32bit boot commands Sascha Hauer
2018-03-16 12:53 ` [PATCH 72/78] ARM: aarch64: Add support to start kernel and barebox Sascha Hauer
2018-03-16 12:53 ` [PATCH 73/78] ARM: cache-armv4: Fix wrong section Sascha Hauer
2018-03-16 12:53 ` [PATCH 74/78] ARM: build: Remove duplicate file compilation Sascha Hauer
2018-03-16 12:53 ` [PATCH 75/78] ARM: Create own cache.c file for aarch64 Sascha Hauer
2018-03-16 12:53 ` [PATCH 76/78] ARM: create separate mmu_64.h file Sascha Hauer
2018-03-16 12:53 ` [PATCH 77/78] ARM: change mmu_early_enable() prototype Sascha Hauer
2018-03-16 12:53 ` [PATCH 78/78] ARM: aarch64: Make early MMU support work Sascha Hauer
2018-03-21  5:35 ` [PATCH 00/78] ARM aarch64 updates Andrey Smirnov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAHQ1cqGbkHN+Csi7-Bb=_iTea4zXK3-mmK8EK0x-UW9niHNCQg@mail.gmail.com' \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox