Skip to contentSkip to conversation

powerpc/boot: move the .got section to after the .dynamic section

PATCH Applied Bill Wendling 3 participants 6 messages Oct 17, 2020 – Dec 10, 2020 PowerPC

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc/boot: move the .got section to after the .dynamic section

Both .dynamic and .got are RELRO sections and should be placed together, and LLD emits an error:

ld.lld: error: section: .got is not contiguous with other relro sections

Place them together to avoid this.

Cc Fangrui Song <[email protected]>
Cc Alan Modra <[email protected]>
---
 arch/powerpc/boot/zImage.lds.S | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
arch/powerpc/boot/zImage.lds.S +11 -10
index a21f3a76e06f..d6f072865627 100644
@@ -34,6 +34,17 @@ SECTIONS
     __dynamic_start = .;
     *(.dynamic)
   }
+
+#ifdef CONFIG_PPC64_BOOT_WRAPPER
+  . = ALIGN(256);
+  .got :
+  {
+    __toc_start = .;
+    *(.got)
+    *(.toc)
+  }
+#endif
+
   .hash : { *(.hash) }
   .interp : { *(.interp) }
   .rela.dyn :
@@ -76,16 +87,6 @@ SECTIONS
     _esm_blob_end =  .;
   }
 
-#ifdef CONFIG_PPC64_BOOT_WRAPPER
-  . = ALIGN(256);
-  .got :
-  {
-    __toc_start = .;
-    *(.got)
-    *(.toc)
-  }
-#endif
-
   . = ALIGN(4096);
   .bss       :
   {
Signature · 1 line
2.29.0.rc1.297.gfa9743e501-goog
#1 lore raw
Quoted 40 lines · Bill Wendling

Both .dynamic and .got are RELRO sections and should be placed together, and LLD emits an error:

ld.lld: error: section: .got is not contiguous with other relro sections

Place them together to avoid this.

Cc Fangrui Song <[email protected]>
Cc Alan Modra <[email protected]>
---
 arch/powerpc/boot/zImage.lds.S | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
arch/powerpc/boot/zImage.lds.S +11 -0
index a21f3a76e06f..d6f072865627 100644
@@ -34,6 +34,17 @@ SECTIONS
     __dynamic_start = .;
     *(.dynamic)
   }
+
+#ifdef CONFIG_PPC64_BOOT_WRAPPER
+  . = ALIGN(256);
+  .got :
+  {
+    __toc_start = .;
+    *(.got)
+    *(.toc)
+  }
+#endif
+
   .hash : { *(.hash) }
   .interp : { *(.interp) }
   .rela.dyn :
@@ -76,16 +87,6 @@ SECTIONS
     _esm_blob_end =  .;
   }

arch/powerpc/boot/zImage.lds.S SECTIONS · line 87

Quoted 10 lines
arch/powerpc/boot/zImage.lds.S +0 -9
 
-#ifdef CONFIG_PPC64_BOOT_WRAPPER
-  . = ALIGN(256);
-  .got :
-  {
-    __toc_start = .;
-    *(.got)
-    *(.toc)
-  }
-#endif

The kernel does not require this but normally all read-only sections precede SHF_WRITE sections.
.dynamic and .got have the SHF_WRITE flag and should be placed here.

Ideally, the order is: R RX RW(RELRO) RW(non-RELRO) (LLD order)

For comparison:
GNU ld -z separate-code order: R RX R RW(RELRO) RW(non-RELRO) (GNU ld>=2.31 enables -z separate-code by default for Linux x86) GNU ld -z noseparate-code order: RX RW(RELRO) RW(non-RELRO)

(AFAIK The only reason .dynamic is writable is due to DT_DEBUG (whose purpose is questionable nowadays). mips .dynamic is read-only. LLD has an option -z rodynamic to make .dynamic readonly)

Quoted 6 lines
arch/powerpc/boot/zImage.lds.S +0 -1
   . = ALIGN(4096);
   .bss       :
   {
--

2.29.0.rc1.297.gfa9743e501-goog

#2 lore raw

PATCH 1/1 powerpc/wrapper: add "-z rodynamic" when using LLD

In reply to Bill Wendling

Normally all read-only sections precede SHF_WRITE sections. .dynamic and .got have the SHF_WRITE flag; .dynamic probably because of DT_DEBUG. LLD emits an error when this happens, so use "-z rodynamic" to mark .dynamic as read-only.

Cc Fangrui Song <[email protected]>
Cc Alan Modra <[email protected]>
---
 arch/powerpc/boot/wrapper | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/boot/wrapper +3 -1
index cd58a62e810d..e1194955adbb 100755
@@ -46,6 +46,7 @@ compression=.gz
 uboot_comp=gzip
 pie=
 format=
+rodynamic=
 
 # cross-compilation prefix
 CROSS=
@@ -353,6 +354,7 @@ epapr)
     platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
     link_address='0x20000000'
     pie=-pie
+    rodynamic=$(if ${CROSS}ld -V 2>&1 | grep -q LLD ; then echo "-z rodynamic"; fi)
     ;;
 mvme5100)
     platformo="$object/fixed-head.o $object/mvme5100.o"
@@ -493,7 +495,7 @@ if [ "$platform" != "miboot" ]; then
         text_start="-Ttext $link_address"
     fi
 #link everything
-    ${CROSS}ld -m $format -T $lds $text_start $pie $nodl -o "$ofile" $map \
+    ${CROSS}ld -m $format -T $lds $text_start $pie $nodl $rodynamic -o "$ofile" $map \
 	$platformo $tmp $object/wrapper.a
     rm $tmp
 fi
Signature · 1 line
2.29.2.454.gaff20da3a2-goog
#3 lore raw

Re: [PATCH] powerpc/wrapper: add "-z rodynamic" when using LLD

We could wait for https://lkml.org/lkml/2020/11/13/19
"[PATCH] kbuild: Always link with '-z norelro'"

Then we would not need -z rodynamic to work around a -z relro issue.

(The issue is that some sections don't strictly follow the normal R/RX/RW(RELRO)/RW(non-RELRO) section flag partition. As a linker person I would suggest that we don't create multiple clusters with the same section flags (e.g. RW in two separate places), but this is my very minor complaint.)

Quoted 44 lines · Bill Wendling

Normally all read-only sections precede SHF_WRITE sections. .dynamic and .got have the SHF_WRITE flag; .dynamic probably because of DT_DEBUG. LLD emits an error when this happens, so use "-z rodynamic" to mark .dynamic as read-only.

Cc Fangrui Song <[email protected]>
Cc Alan Modra <[email protected]>

---
arch/powerpc/boot/wrapper | 4 +++-

1 file changed, 3 insertions(+), 1 deletion(-)
arch/powerpc/boot/wrapper +0 -0
index cd58a62e810d..e1194955adbb 100755
@@ -46,6 +46,7 @@ compression=.gz

uboot_comp=gzip
pie=
format=
+rodynamic=

# cross-compilation prefix
CROSS=
arch/powerpc/boot/wrapper +2 -1
@@ -353,6 +354,7 @@ epapr)
    platformo="$object/pseries-head.o $object/epapr.o $object/epapr-wrapper.o"
    link_address='0x20000000'
    pie=-pie
+    rodynamic=$(if ${CROSS}ld -V 2>&1 | grep -q LLD ; then echo "-z rodynamic"; fi)
    ;;
 mvme5100)
    platformo="$object/fixed-head.o $object/mvme5100.o"
@@ -493,7 +495,7 @@ if [ "$platform" != "miboot" ]; then
        text_start="-Ttext $link_address"
    fi
 #link everything
-    ${CROSS}ld -m $format -T $lds $text_start $pie $nodl -o "$ofile" $map \
+    ${CROSS}ld -m $format -T $lds $text_start $pie $nodl $rodynamic -o "$ofile" $map \
 	$platformo $tmp $object/wrapper.a
    rm $tmp

fi

Signature · 1 line
2.29.2.454.gaff20da3a2-goog
#4 lore raw

Re: [PATCH] powerpc/wrapper: add "-z rodynamic" when using LLD

In reply to Bill Wendling
Quoted 4 lines · Bill Wendling

Normally all read-only sections precede SHF_WRITE sections. .dynamic and .got have the SHF_WRITE flag; .dynamic probably because of DT_DEBUG. LLD emits an error when this happens, so use "-z rodynamic" to mark .dynamic as read-only.

Applied to powerpc/next.

[1/1] powerpc/boot/wrapper: Add "-z rodynamic" when using LLD

      https://git.kernel.org/powerpc/c/26ba9f9651d802ba38583138f43fea5dc7eb0fd6

cheers