arm64: add support for building vmlinux as a relocatable PIE binary
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Tue, 26 Jan 2016 08:13:44 +0000 (09:13 +0100)
committerAlex Shi <alex.shi@linaro.org>
Wed, 11 May 2016 15:18:45 +0000 (23:18 +0800)
This implements CONFIG_RELOCATABLE, which links the final vmlinux
image with a dynamic relocation section, allowing the early boot code
to perform a relocation to a different virtual address at runtime.

This is a prerequisite for KASLR (CONFIG_RANDOMIZE_BASE).

Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
(cherry picked from commit 1e48ef7fcc374051730381a2a05da77eb4eafdb0)
Signed-off-by: Alex Shi <alex.shi@linaro.org>
arch/arm64/Kconfig
arch/arm64/Makefile
arch/arm64/include/asm/elf.h
arch/arm64/kernel/head.S
arch/arm64/kernel/vmlinux.lds.S

index 22db20491733f7b20c87c7eec3dbc88c83a91211..ac1475f559e6b398838142f02bc75be61a7d7d49 100644 (file)
@@ -737,6 +737,17 @@ config ARM64_MODULE_PLTS
        select ARM64_MODULE_CMODEL_LARGE
        select HAVE_MOD_ARCH_SPECIFIC
 
+config RELOCATABLE
+       bool
+       help
+         This builds the kernel as a Position Independent Executable (PIE),
+         which retains all relocation metadata required to relocate the
+         kernel binary at runtime to a different virtual address than the
+         address it was linked at.
+         Since AArch64 uses the RELA relocation format, this requires a
+         relocation pass at runtime even if the kernel is loaded at the
+         same address it was linked at.
+
 endmenu
 
 menu "Boot options"
index 71054a38decf81ae1d7e73cfd36cf7bb21f33e58..304dcc3da06f5f5d238f77a183c940607296f31a 100644 (file)
@@ -15,6 +15,10 @@ CPPFLAGS_vmlinux.lds = -DTEXT_OFFSET=$(TEXT_OFFSET)
 OBJCOPYFLAGS   :=-O binary -R .note -R .note.gnu.build-id -R .comment -S
 GZFLAGS                :=-9
 
+ifneq ($(CONFIG_RELOCATABLE),)
+LDFLAGS_vmlinux                += -pie
+endif
+
 KBUILD_DEFCONFIG := defconfig
 
 # Check for binutils support for specific extensions
index 435f55952e1f4adcab3a658969c922c202d1aab3..24ed037f09fd32385b9ccfb0e8d380b5c0d5c7ed 100644 (file)
@@ -77,6 +77,8 @@
 #define R_AARCH64_MOVW_PREL_G2_NC      292
 #define R_AARCH64_MOVW_PREL_G3         293
 
+#define R_AARCH64_RELATIVE             1027
+
 /*
  * These are used to set parameters in the core dumps.
  */
index 4cad8f9f226836eeba169954d6e7b3c5d42357e5..4e69412a7323b54b45217b2719e2e3cf761567af 100644 (file)
@@ -29,6 +29,7 @@
 #include <asm/asm-offsets.h>
 #include <asm/cache.h>
 #include <asm/cputype.h>
+#include <asm/elf.h>
 #include <asm/kernel-pgtable.h>
 #include <asm/memory.h>
 #include <asm/pgtable-hwdef.h>
@@ -432,6 +433,37 @@ __mmap_switched:
        bl      __pi_memset
        dsb     ishst                           // Make zero page visible to PTW
 
+#ifdef CONFIG_RELOCATABLE
+
+       /*
+        * Iterate over each entry in the relocation table, and apply the
+        * relocations in place.
+        */
+       adr_l   x8, __dynsym_start              // start of symbol table
+       adr_l   x9, __reloc_start               // start of reloc table
+       adr_l   x10, __reloc_end                // end of reloc table
+
+0:     cmp     x9, x10
+       b.hs    2f
+       ldp     x11, x12, [x9], #24
+       ldr     x13, [x9, #-8]
+       cmp     w12, #R_AARCH64_RELATIVE
+       b.ne    1f
+       str     x13, [x11]
+       b       0b
+
+1:     cmp     w12, #R_AARCH64_ABS64
+       b.ne    0b
+       add     x12, x12, x12, lsl #1           // symtab offset: 24x top word
+       add     x12, x8, x12, lsr #(32 - 3)     // ... shifted into bottom word
+       ldr     x15, [x12, #8]                  // Elf64_Sym::st_value
+       add     x15, x13, x15
+       str     x15, [x11]
+       b       0b
+
+2:
+#endif
+
        adr_l   sp, initial_sp, x4
        mov     x4, sp
        and     x4, x4, #~(THREAD_SIZE - 1)
index 282e3e64a17e424f1806433d7b2648db34600837..e3f6cd740ea346bd887f01e2b46a558d8075d966 100644 (file)
@@ -87,6 +87,7 @@ SECTIONS
                EXIT_CALL
                *(.discard)
                *(.discard.*)
+               *(.interp .dynamic)
        }
 
        . = KIMAGE_VADDR + TEXT_OFFSET;
@@ -149,6 +150,21 @@ SECTIONS
        .altinstr_replacement : {
                *(.altinstr_replacement)
        }
+       .rela : ALIGN(8) {
+               __reloc_start = .;
+               *(.rela .rela*)
+               __reloc_end = .;
+       }
+       .dynsym : ALIGN(8) {
+               __dynsym_start = .;
+               *(.dynsym)
+       }
+       .dynstr : {
+               *(.dynstr)
+       }
+       .hash : {
+               *(.hash)
+       }
 
        . = ALIGN(PAGE_SIZE);
        __init_end = .;