[2.43] ld: Don't explicitly add .note.gnu.build-id in elf.sc

Message ID 20240922002308.1852158-1-hjl.tools@gmail.com
State New
Headers
Series [2.43] ld: Don't explicitly add .note.gnu.build-id in elf.sc |

Checks

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

Commit Message

H.J. Lu Sept. 22, 2024, 12:23 a.m. UTC
  The commit:

e8e10743f7b Add --rosegment option to BFD linker to stop the '-z separate-code' from generating two read-only segments.

places .note.gnu.property and text sections in the same PT_LOAD segment
when --rosegment and -z separate-code are used together.  After

commit f3e660db14a0a95f3953496d8beb7c58ef34c6d5
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Mon Oct 8 04:39:09 2018 -0700

    ELF: Properly group and place orphan note sections

    Properly group orphan note sections.  When placing orphan note section
    as the first note section, place it after the section before all note
    sections.

ld properly groups and places orphan note sections.  If .note.gnu.build-id
is added to elf.sc, the orphan .note.gnu.property section will be placed
before the .note.gnu.build-id section due to the larger section alignment
and may be placed in the same PT_LOAD segment with text sections.  Remove
.note.gnu.build-id from elf.sc to let ld properly group and add all orphan
note sections.

NB: Binutils 2.44 needs a different fix since .note.gnu.build-id has been
removed from emit_early_ro in elf.sc.

	PR ld/32191
	* scripttempl/elf.sc (emit_early_ro): Remove .note.gnu.build-id.
	* testsuite/ld-i386/i386.exp: Run PR ld/32191 test.
	* testsuite/ld-i386/pr32191.d: New file.
	* testsuite/ld-x86-64/pr32191-x32.d: New file.
	* testsuite/ld-x86-64/pr32191.d: Likewise.
	* testsuite/ld-x86-64/pr32191.s: Likewise.
	* testsuite/ld-x86-64/x86-64.exp: Run PR ld/32191 tests.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 ld/scripttempl/elf.sc                | 1 -
 ld/testsuite/ld-i386/i386.exp        | 1 +
 ld/testsuite/ld-i386/pr32191.d       | 9 +++++++++
 ld/testsuite/ld-x86-64/pr32191-x32.d | 9 +++++++++
 ld/testsuite/ld-x86-64/pr32191.d     | 9 +++++++++
 ld/testsuite/ld-x86-64/pr32191.s     | 5 +++++
 ld/testsuite/ld-x86-64/x86-64.exp    | 2 ++
 7 files changed, 35 insertions(+), 1 deletion(-)
 create mode 100644 ld/testsuite/ld-i386/pr32191.d
 create mode 100644 ld/testsuite/ld-x86-64/pr32191-x32.d
 create mode 100644 ld/testsuite/ld-x86-64/pr32191.d
 create mode 100644 ld/testsuite/ld-x86-64/pr32191.s
  

Patch

diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
index 54716110b61..3fdbff52094 100644
--- a/ld/scripttempl/elf.sc
+++ b/ld/scripttempl/elf.sc
@@ -425,7 +425,6 @@  emit_early_ro()
 {
   cat <<EOF
   ${INITIAL_READONLY_SECTIONS}
-  .note.gnu.build-id ${RELOCATING-0}: { *(.note.gnu.build-id) }
 EOF
 }
 
diff --git a/ld/testsuite/ld-i386/i386.exp b/ld/testsuite/ld-i386/i386.exp
index 8dafb6261fb..7a8a7e438a1 100644
--- a/ld/testsuite/ld-i386/i386.exp
+++ b/ld/testsuite/ld-i386/i386.exp
@@ -513,6 +513,7 @@  run_dump_test "pr28870"
 run_dump_test "pr28894"
 run_dump_test "pr30787"
 run_dump_test "pr31047"
+run_dump_test "pr32191"
 
 if { !([istarget "i?86-*-linux*"]
        || [istarget "i?86-*-gnu*"]
diff --git a/ld/testsuite/ld-i386/pr32191.d b/ld/testsuite/ld-i386/pr32191.d
new file mode 100644
index 00000000000..d4a00bb92f4
--- /dev/null
+++ b/ld/testsuite/ld-i386/pr32191.d
@@ -0,0 +1,9 @@ 
+#source: ../ld-x86-64/pr32191.s
+#as: --32 -mx86-used-note=yes
+#ld: -shared -m elf_i386 -z separate-code --build-id --rosegment
+#readelf: -lW
+
+#...
+ +[0-9]+ +\.text 
+ +[0-9]+ +\..* \.note\.gnu\.property \.note\.gnu\.build-id .*
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr32191-x32.d b/ld/testsuite/ld-x86-64/pr32191-x32.d
new file mode 100644
index 00000000000..13f38a21fa3
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr32191-x32.d
@@ -0,0 +1,9 @@ 
+#source: pr32191.s
+#as: --x32 -mx86-used-note=yes
+#ld: -shared -m elf32_x86_64 -z separate-code --build-id --rosegment
+#readelf: -lW
+
+#...
+ +[0-9]+ +\.text 
+ +[0-9]+ +\..* \.note\.gnu\.property \.note\.gnu\.build-id .*
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr32191.d b/ld/testsuite/ld-x86-64/pr32191.d
new file mode 100644
index 00000000000..7f37f752541
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr32191.d
@@ -0,0 +1,9 @@ 
+#source: pr32191.s
+#as: --64 -mx86-used-note=yes
+#ld: -shared -m elf_x86_64 -z separate-code --build-id --rosegment
+#readelf: -lW
+
+#...
+ +[0-9]+ +\.text 
+ +[0-9]+ +\..* \.note\.gnu\.property \.note\.gnu\.build-id .*
+#pass
diff --git a/ld/testsuite/ld-x86-64/pr32191.s b/ld/testsuite/ld-x86-64/pr32191.s
new file mode 100644
index 00000000000..953f0de670f
--- /dev/null
+++ b/ld/testsuite/ld-x86-64/pr32191.s
@@ -0,0 +1,5 @@ 
+	.text
+	.global foo
+foo:
+	.nops 4
+	.section	.note.GNU-stack,"",@progbits
diff --git a/ld/testsuite/ld-x86-64/x86-64.exp b/ld/testsuite/ld-x86-64/x86-64.exp
index 0697e67faf3..607418279db 100644
--- a/ld/testsuite/ld-x86-64/x86-64.exp
+++ b/ld/testsuite/ld-x86-64/x86-64.exp
@@ -540,6 +540,8 @@  run_dump_test "pr31868c-x32"
 run_dump_test "tlsie5"
 run_dump_test "tlsdesc3"
 run_dump_test "tlsdesc4"
+run_dump_test "pr32191"
+run_dump_test "pr32191-x32"
 
 if { ![skip_sframe_tests] } {
     run_dump_test "sframe-simple-1"