Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

powerpc/boot: Allow building the zImage wrapper as a relocatable ET_DYN

This patch adds code, linker script and makefile support to allow
building the zImage wrapper around the kernel as a position independent
executable. This results in an ET_DYN instead of an ET_EXEC ELF output
file, which can be loaded at any location by the firmware and will
process its own relocations to work correctly at the loaded address.

This is of interest particularly since the standard ePAPR image format
must be an ET_DYN (although this patch alone is not sufficient to
produce a fully ePAPR compliant boot image).

Note for now we don't enable building with -pie for anything.

Signed-off-by: Paul Mackerras <paulus@samba.org>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Michael Ellerman <michael@ellerman.id.au>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>

authored by

Michael Ellerman and committed by
Benjamin Herrenschmidt
6975a783 ee7a2aa3

+118 -70
+74 -42
arch/powerpc/boot/crt0.S
··· 6 6 * as published by the Free Software Foundation; either version 7 7 * 2 of the License, or (at your option) any later version. 8 8 * 9 - * NOTE: this code runs in 32 bit mode and is packaged as ELF32. 9 + * NOTE: this code runs in 32 bit mode, is position-independent, 10 + * and is packaged as ELF32. 10 11 */ 11 12 12 13 #include "ppc_asm.h" 13 14 14 15 .text 15 - /* a procedure descriptor used when booting this as a COFF file */ 16 + /* A procedure descriptor used when booting this as a COFF file. 17 + * When making COFF, this comes first in the link and we're 18 + * linked at 0x500000. 19 + */ 16 20 .globl _zimage_start_opd 17 21 _zimage_start_opd: 18 - .long _zimage_start, 0, 0, 0 22 + .long 0x500000, 0, 0, 0 23 + 24 + p_start: .long _start 25 + p_etext: .long _etext 26 + p_bss_start: .long __bss_start 27 + p_end: .long _end 28 + 29 + .weak _platform_stack_top 30 + p_pstack: .long _platform_stack_top 19 31 20 32 .weak _zimage_start 21 33 .globl _zimage_start ··· 36 24 _zimage_start_lib: 37 25 /* Work out the offset between the address we were linked at 38 26 and the address where we're running. */ 39 - bl 1f 40 - 1: mflr r0 41 - lis r9,1b@ha 42 - addi r9,r9,1b@l 43 - subf. r0,r9,r0 44 - beq 3f /* if running at same address as linked */ 27 + bl .+4 28 + p_base: mflr r10 /* r10 now points to runtime addr of p_base */ 29 + /* grab the link address of the dynamic section in r11 */ 30 + addis r11,r10,(_GLOBAL_OFFSET_TABLE_-p_base)@ha 31 + lwz r11,(_GLOBAL_OFFSET_TABLE_-p_base)@l(r11) 32 + cmpwi r11,0 33 + beq 3f /* if not linked -pie */ 34 + /* get the runtime address of the dynamic section in r12 */ 35 + .weak __dynamic_start 36 + addis r12,r10,(__dynamic_start-p_base)@ha 37 + addi r12,r12,(__dynamic_start-p_base)@l 38 + subf r11,r11,r12 /* runtime - linktime offset */ 45 39 46 - /* The .got2 section contains a list of addresses, so add 47 - the address offset onto each entry. */ 48 - lis r9,__got2_start@ha 49 - addi r9,r9,__got2_start@l 50 - lis r8,__got2_end@ha 51 - addi r8,r8,__got2_end@l 52 - subf. r8,r9,r8 40 + /* The dynamic section contains a series of tagged entries. 41 + * We need the RELA and RELACOUNT entries. */ 42 + RELA = 7 43 + RELACOUNT = 0x6ffffff9 44 + li r9,0 45 + li r0,0 46 + 9: lwz r8,0(r12) /* get tag */ 47 + cmpwi r8,0 48 + beq 10f /* end of list */ 49 + cmpwi r8,RELA 50 + bne 11f 51 + lwz r9,4(r12) /* get RELA pointer in r9 */ 52 + b 12f 53 + 11: addis r8,r8,(-RELACOUNT)@ha 54 + cmpwi r8,RELACOUNT@l 55 + bne 12f 56 + lwz r0,4(r12) /* get RELACOUNT value in r0 */ 57 + 12: addi r12,r12,8 58 + b 9b 59 + 60 + /* The relocation section contains a list of relocations. 61 + * We now do the R_PPC_RELATIVE ones, which point to words 62 + * which need to be initialized with addend + offset. 63 + * The R_PPC_RELATIVE ones come first and there are RELACOUNT 64 + * of them. */ 65 + 10: /* skip relocation if we don't have both */ 66 + cmpwi r0,0 53 67 beq 3f 54 - srwi. r8,r8,2 55 - mtctr r8 56 - add r9,r0,r9 57 - 2: lwz r8,0(r9) 58 - add r8,r8,r0 59 - stw r8,0(r9) 60 - addi r9,r9,4 68 + cmpwi r9,0 69 + beq 3f 70 + 71 + add r9,r9,r11 /* Relocate RELA pointer */ 72 + mtctr r0 73 + 2: lbz r0,4+3(r9) /* ELF32_R_INFO(reloc->r_info) */ 74 + cmpwi r0,22 /* R_PPC_RELATIVE */ 75 + bne 3f 76 + lwz r12,0(r9) /* reloc->r_offset */ 77 + lwz r0,8(r9) /* reloc->r_addend */ 78 + add r0,r0,r11 79 + stwx r0,r11,r12 80 + addi r9,r9,12 61 81 bdnz 2b 62 82 63 83 /* Do a cache flush for our text, in case the loader didn't */ 64 - 3: lis r9,_start@ha 65 - addi r9,r9,_start@l 66 - add r9,r0,r9 67 - lis r8,_etext@ha 68 - addi r8,r8,_etext@l 69 - add r8,r0,r8 84 + 3: lwz r9,p_start-p_base(r10) /* note: these are relocated now */ 85 + lwz r8,p_etext-p_base(r10) 70 86 4: dcbf r0,r9 71 87 icbi r0,r9 72 88 addi r9,r9,0x20 ··· 104 64 isync 105 65 106 66 /* Clear the BSS */ 107 - lis r9,__bss_start@ha 108 - addi r9,r9,__bss_start@l 109 - add r9,r0,r9 110 - lis r8,_end@ha 111 - addi r8,r8,_end@l 112 - add r8,r0,r8 113 - li r10,0 114 - 5: stw r10,0(r9) 67 + lwz r9,p_bss_start-p_base(r10) 68 + lwz r8,p_end-p_base(r10) 69 + li r0,0 70 + 5: stw r0,0(r9) 115 71 addi r9,r9,4 116 72 cmplw cr0,r9,r8 117 73 blt 5b 118 74 119 75 /* Possibly set up a custom stack */ 120 - .weak _platform_stack_top 121 - lis r8,_platform_stack_top@ha 122 - addi r8,r8,_platform_stack_top@l 76 + lwz r8,p_pstack-p_base(r10) 123 77 cmpwi r8,0 124 78 beq 6f 125 - add r8,r0,r8 126 79 lwz r1,0(r8) 127 - add r1,r0,r1 128 80 li r0,0 129 81 stwu r0,-16(r1) /* establish a stack frame */ 130 82 6:
+6 -3
arch/powerpc/boot/wrapper
··· 39 39 cacheit= 40 40 binary= 41 41 gzip=.gz 42 + pie= 42 43 43 44 # cross-compilation prefix 44 45 CROSS= ··· 158 157 platformo=$object/of.o 159 158 ;; 160 159 coff) 161 - platformo=$object/of.o 160 + platformo="$object/crt0.o $object/of.o" 162 161 lds=$object/zImage.coff.lds 163 162 link_address='0x500000' 163 + pie= 164 164 ;; 165 165 miboot|uboot) 166 166 # miboot and U-boot want just the bare bits, not an ELF binary ··· 210 208 ksection=.kernel:vmlinux.bin 211 209 isection=.kernel:initrd 212 210 link_address='' 211 + pie= 213 212 ;; 214 213 ep88xc|ep405|ep8248e) 215 214 platformo="$object/fixed-head.o $object/$platform.o" ··· 313 310 314 311 if [ "$platform" != "miboot" ]; then 315 312 if [ -n "$link_address" ] ; then 316 - text_start="-Ttext $link_address --defsym _start=$link_address" 313 + text_start="-Ttext $link_address" 317 314 fi 318 - ${CROSS}ld -m elf32ppc -T $lds $text_start -o "$ofile" \ 315 + ${CROSS}ld -m elf32ppc -T $lds $text_start $pie -o "$ofile" \ 319 316 $platformo $tmp $object/wrapper.a 320 317 rm $tmp 321 318 fi
+2 -4
arch/powerpc/boot/zImage.coff.lds.S
··· 3 3 EXTERN(_zimage_start_opd) 4 4 SECTIONS 5 5 { 6 - _start = .; 7 6 .text : 8 7 { 8 + _start = .; 9 9 *(.text) 10 10 *(.fixup) 11 + _etext = .; 11 12 } 12 - _etext = .; 13 13 . = ALIGN(4096); 14 14 .data : 15 15 { ··· 17 17 *(.data*) 18 18 *(__builtin_*) 19 19 *(.sdata*) 20 - __got2_start = .; 21 20 *(.got2) 22 - __got2_end = .; 23 21 24 22 _dtb_start = .; 25 23 *(.kernel:dtb)
+36 -21
arch/powerpc/boot/zImage.lds.S
··· 3 3 EXTERN(_zimage_start) 4 4 SECTIONS 5 5 { 6 - _start = .; 7 6 .text : 8 7 { 8 + _start = .; 9 9 *(.text) 10 10 *(.fixup) 11 + _etext = .; 11 12 } 12 - _etext = .; 13 13 . = ALIGN(4096); 14 14 .data : 15 15 { 16 16 *(.rodata*) 17 17 *(.data*) 18 18 *(.sdata*) 19 - __got2_start = .; 20 19 *(.got2) 21 - __got2_end = .; 22 20 } 21 + .dynsym : { *(.dynsym) } 22 + .dynstr : { *(.dynstr) } 23 + .dynamic : 24 + { 25 + __dynamic_start = .; 26 + *(.dynamic) 27 + } 28 + .hash : { *(.hash) } 29 + .interp : { *(.interp) } 30 + .rela.dyn : { *(.rela*) } 23 31 24 32 . = ALIGN(8); 25 - _dtb_start = .; 26 - .kernel:dtb : { *(.kernel:dtb) } 27 - _dtb_end = .; 33 + .kernel:dtb : 34 + { 35 + _dtb_start = .; 36 + *(.kernel:dtb) 37 + _dtb_end = .; 38 + } 28 39 29 40 . = ALIGN(4096); 30 - _vmlinux_start = .; 31 - .kernel:vmlinux.strip : { *(.kernel:vmlinux.strip) } 32 - _vmlinux_end = .; 41 + .kernel:vmlinux.strip : 42 + { 43 + _vmlinux_start = .; 44 + *(.kernel:vmlinux.strip) 45 + _vmlinux_end = .; 46 + } 33 47 34 48 . = ALIGN(4096); 35 - _initrd_start = .; 36 - .kernel:initrd : { *(.kernel:initrd) } 37 - _initrd_end = .; 49 + .kernel:initrd : 50 + { 51 + _initrd_start = .; 52 + *(.kernel:initrd) 53 + _initrd_end = .; 54 + } 38 55 39 56 . = ALIGN(4096); 40 - _edata = .; 41 - 42 - . = ALIGN(4096); 43 - __bss_start = .; 44 57 .bss : 45 58 { 46 - *(.sbss) 47 - *(.bss) 59 + _edata = .; 60 + __bss_start = .; 61 + *(.sbss) 62 + *(.bss) 63 + *(COMMON) 64 + _end = . ; 48 65 } 49 - . = ALIGN(4096); 50 - _end = . ; 51 66 }