ld: Move note sections after .rodata section

Message ID CAMe9rOp8+wtNAWX3Epo9yGSCA1xsFMZ4SR=F0y4GQMc1_NOG-A@mail.gmail.com
State New
Headers
Series ld: Move note sections after .rodata section |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 fail Patch failed to apply

Commit Message

H.J. Lu Nov. 11, 2024, 11:44 a.m. UTC
  On Mon, Nov 11, 2024 at 7:08 PM Nick Clifton <nickc@redhat.com> wrote:
>
> Hi H.J.
>
> >> I like the idea of moving the note sections into the same segment as the
> >> .rodata section, but I also like the idea of linker scripts explicitly
> >> handling known sections, rather than assuming that, for example, the orphan
> >> section handling code will not change in the future.
> >>
> >> So how about something like the attached (untested) patch which moves the
> >> known note sections from misc-sections.sc to elf.sc ?  This should achieve
> >> your aims whilst also making sure that (known) notes will always be placed
> >> after the read only data.
> >>
> >
> > It works for me.
>
> OK, please could you apply that patch, together with your test case ?
>

This is what I am checking in.

Thanks.
  

Comments

Alan Modra Nov. 12, 2024, 6:39 a.m. UTC | #1
On Mon, Nov 11, 2024 at 07:44:08PM +0800, H.J. Lu wrote:
> This is what I am checking in.

The new test fails on arm-nacl and csky-linux.  I'll commit the
following to fix the csky-linux fail, which is due to that target
having its own .bss directive.

	PR ld/32341
	* testsuite/ld-elf/pr32341.s: Don't use .bss directive.  Specify
	progbits/nobits on all .section directives.

diff --git a/ld/testsuite/ld-elf/pr32341.s b/ld/testsuite/ld-elf/pr32341.s
index 11f31b7771a..c601801a476 100644
--- a/ld/testsuite/ld-elf/pr32341.s
+++ b/ld/testsuite/ld-elf/pr32341.s
@@ -15,11 +15,11 @@ main:
 	.global _main
 _main:
 	.dc.a 0
-	.section .rodata,"a"
+	.section .rodata,"a",%progbits
 	.zero 8
-	.bss
+	.section .bss,"aw",%nobits
 	.zero 8
-	.section	.note.ABI-tag,"a"
+	.section	.note.ABI-tag,"a",%progbits
 	.align 4
 	.zero 32
 	.ident	"GCC: (GNU) 14.2.1"
  

Patch

From 6a64b407f5f45657c9578878ace131459700d957 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 7 Nov 2024 12:39:19 +0800
Subject: [PATCH] ld: Move note sections after .rodata section

Move note sections after .rodata section so that note sections are
placed in the same PT_LOAD segment together with .rodata section,
instead of a separate PT_LOAD segment.

	PR ld/32341
	* scripttempl/misc-sections.sc: Move note sections to ...
	* scripttempl/elf.sc: Here, after .rodata section.
	* testsuite/ld-elf/pr32341.d: New file.
	* testsuite/ld-elf/pr32341.s: Likewise.

Co-Authored-By: Nick Clifton <nickc@redhat.com>
Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 ld/scripttempl/elf.sc           | 11 +++++++++++
 ld/scripttempl/misc-sections.sc | 11 -----------
 ld/testsuite/ld-elf/pr32341.d   | 15 +++++++++++++++
 ld/testsuite/ld-elf/pr32341.s   | 26 ++++++++++++++++++++++++++
 4 files changed, 52 insertions(+), 11 deletions(-)
 create mode 100644 ld/testsuite/ld-elf/pr32341.d
 create mode 100644 ld/testsuite/ld-elf/pr32341.s

diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
index 6ef93dcc906..0d539fcbd38 100644
--- a/ld/scripttempl/elf.sc
+++ b/ld/scripttempl/elf.sc
@@ -668,6 +668,17 @@  cat <<EOF
   .exception_ranges ${RELOCATING-0} : ONLY_IF_RO { *(.exception_ranges${RELOCATING+*}) }
   ${TEXT_PLT+${PLT_NEXT_DATA+${PLT} ${OTHER_PLT_SECTIONS}}}
 
+  /* Various note sections.  Placed here so that they are included in the
+     read-only segment, and so that they are not treated as orphan sections.  */
+  .note.build-id :      { *(.note.build-id) } ${RELOCATING+${REGION}}
+  .note.GNU-stack :     { *(.note.GNU-stack) } ${RELOCATING+${REGION}}
+  .note.gnu-property :  { *(.note.gnu-property) } ${RELOCATING+${REGION}}
+  .note.ABI-tag :       { *(.note.ABI-tag) } ${RELOCATING+${REGION}}
+  .note.package :       { *(.note.package) } ${RELOCATING+${REGION}}
+  .note.dlopen :        { *(.note.dlopen) } ${RELOCATING+${REGION}}
+  .note.netbsd.ident :  { *(.note.netbsd.ident) } ${RELOCATING+${REGION}}
+  .note.openbsd.ident : { *(.note.openbsd.ident) } ${RELOCATING+${REGION}}
+  
   ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (__${ETEXT_NAME} = .);}}
   ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (_${ETEXT_NAME} = .);}}
   ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (${ETEXT_NAME} = .);}}
diff --git a/ld/scripttempl/misc-sections.sc b/ld/scripttempl/misc-sections.sc
index 71b1a6ba643..93ce9b2871c 100644
--- a/ld/scripttempl/misc-sections.sc
+++ b/ld/scripttempl/misc-sections.sc
@@ -30,15 +30,4 @@  cat <<EOF
 
   .gnu.build.attributes : { *(.gnu.build.attributes${RELOCATING+ .gnu.build.attributes.*}) }
 
-  /* Various note sections.  Placed here so that they do not get
-     treated as orphan sections.  */
-  .note.build-id :      { *(.note.build-id) } ${RELOCATING+${REGION}}
-  .note.GNU-stack :     { *(.note.GNU-stack) } ${RELOCATING+${REGION}}
-  .note.gnu-property :  { *(.note.gnu-property) } ${RELOCATING+${REGION}}
-  .note.ABI-tag :       { *(.note.ABI-tag) } ${RELOCATING+${REGION}}
-  .note.package :       { *(.note.package) } ${RELOCATING+${REGION}}
-  .note.dlopen :        { *(.note.dlopen) } ${RELOCATING+${REGION}}
-  .note.netbsd.ident :  { *(.note.netbsd.ident) } ${RELOCATING+${REGION}}
-  .note.openbsd.ident : { *(.note.openbsd.ident) } ${RELOCATING+${REGION}}
-
 EOF
diff --git a/ld/testsuite/ld-elf/pr32341.d b/ld/testsuite/ld-elf/pr32341.d
new file mode 100644
index 00000000000..eb76c3aad24
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr32341.d
@@ -0,0 +1,15 @@ 
+#ld: --build-id -z separate-code -z relro --rosegment
+#readelf: -l --wide
+#target: *-*-linux-gnu *-*-gnu* *-*-nacl* arm*-*-uclinuxfdpiceabi
+#xfail: ![check_relro_support]
+
+#failif
+#...
+[ ]+LOAD[ 	]+0x[0-9a-f x]+.*
+#...
+[ ]+LOAD[ 	]+0x[0-9a-f x]+.*
+#...
+[ ]+LOAD[ 	]+0x[0-9a-f x]+.*
+#...
+[ ]+LOAD[ 	]+0x[0-9a-f x]+.*
+#...
diff --git a/ld/testsuite/ld-elf/pr32341.s b/ld/testsuite/ld-elf/pr32341.s
new file mode 100644
index 00000000000..11f31b7771a
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr32341.s
@@ -0,0 +1,26 @@ 
+	.text
+	.type start,"function"
+	.global start
+start:
+	.type _start,"function"
+	.global _start
+_start:
+	.type __start,"function"
+	.global __start
+__start:
+	.type main,"function"
+	.global main
+main:
+	.type _main,"function"
+	.global _main
+_main:
+	.dc.a 0
+	.section .rodata,"a"
+	.zero 8
+	.bss
+	.zero 8
+	.section	.note.ABI-tag,"a"
+	.align 4
+	.zero 32
+	.ident	"GCC: (GNU) 14.2.1"
+	.section	.note.GNU-stack,"",%progbits
-- 
2.47.0