[binutils-gdb] Improve the placement of orphan note sections.

Nick Clifton nickc@sourceware.org
Mon Sep 30 13:41:44 GMT 2024


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=7ecf0250f7f0868f3b828ffadd47d0e5f2dbd875

commit 7ecf0250f7f0868f3b828ffadd47d0e5f2dbd875
Author: Nick Clifton <nickc@redhat.com>
Date:   Mon Sep 30 14:41:11 2024 +0100

    Improve the placement of orphan note sections.
    
    PR 32219

Diff:
---
 ld/ldelf.c                       | 15 +++++++++++++--
 ld/ldlang.c                      |  2 +-
 ld/scripttempl/arclinux.sc       |  1 +
 ld/scripttempl/elf32cr16.sc      |  2 +-
 ld/scripttempl/elf32crx.sc       |  2 +-
 ld/scripttempl/elfd10v.sc        |  6 +++++-
 ld/scripttempl/elfxtensa.sc      |  5 +++++
 ld/scripttempl/mep.sc            |  5 +++++
 ld/scripttempl/misc-sections.sc  | 28 ++++++++++++++++++++++++++--
 ld/scripttempl/nds32elf.sc       |  3 ++-
 ld/scripttempl/v850.sc           |  4 ++++
 ld/scripttempl/v850_rh850.sc     |  4 ++++
 ld/testsuite/ld-elf/pr23658-1a.d |  4 +---
 ld/testsuite/ld-elf/pr23658-1e.d |  3 ++-
 14 files changed, 71 insertions(+), 13 deletions(-)

diff --git a/ld/ldelf.c b/ld/ldelf.c
index 09691609fc0..2371af38b79 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -2118,7 +2118,7 @@ ldelf_place_orphan (asection *s, const char *secname, int constraint)
       { ".bss",
 	SEC_ALLOC,
 	0, 0, 0, 0 },
-      { 0,
+      { NULL,
 	SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA,
 	0, 0, 0, 0 },
       { ".interp",
@@ -2328,7 +2328,18 @@ ldelf_place_orphan (asection *s, const char *secname, int constraint)
 	   && (elfinput
 	       ? sh_type == SHT_NOTE
 	       : startswith (secname, ".note")))
-    place = &hold[orphan_interp];
+    {
+      /* PR 32219: Check that the .interp section
+	 exists before attaching orphans to it.  */
+      if (lang_output_section_find (hold[orphan_interp].name))
+	place = &hold[orphan_interp];
+      /* Next best place: after .rodata.  */
+      else if (lang_output_section_find (hold[orphan_rodata].name))
+	place = &hold[orphan_rodata];
+      /* Last attempt: the .text section.  */
+      else
+	place = &hold[orphan_text];
+    }
   else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
     place = &hold[orphan_bss];
   else if ((flags & SEC_SMALL_DATA) != 0)
diff --git a/ld/ldlang.c b/ld/ldlang.c
index 95d64fd05e2..7f9e3d2b119 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -9012,7 +9012,7 @@ lang_record_phdrs (void)
 		continue;
 
 	      /* Don't add orphans to PT_INTERP header.  */
-	      if (l->type == 3)
+	      if (l->type == PT_INTERP)
 		continue;
 
 	      if (last == NULL)
diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc
index 36ba5a664d3..245e0208f0b 100644
--- a/ld/scripttempl/arclinux.sc
+++ b/ld/scripttempl/arclinux.sc
@@ -661,6 +661,7 @@ source_sh $srcdir/scripttempl/DWARF.sc
 
 cat <<EOF
   ${ATTRS_SECTIONS}
+  .ARC.attributes 0 : { KEEP (*(.ARC.attributes)) }
   ${OTHER_SECTIONS}
   ${RELOCATING+${OTHER_SYMBOLS}}
   ${RELOCATING+${DISCARDED}}
diff --git a/ld/scripttempl/elf32cr16.sc b/ld/scripttempl/elf32cr16.sc
index 3e7d92a4f34..305f1f27cd2 100644
--- a/ld/scripttempl/elf32cr16.sc
+++ b/ld/scripttempl/elf32cr16.sc
@@ -171,7 +171,7 @@ SECTIONS
 
 EOF
 
-source_sh $srcdir/scripttempl/misc-sections.sc
+source_sh $srcdir/scripttempl/misc-sections.sc rom
 source_sh $srcdir/scripttempl/DWARF.sc
 
 cat <<EOF
diff --git a/ld/scripttempl/elf32crx.sc b/ld/scripttempl/elf32crx.sc
index 1b1316676a7..0dcf6adad23 100644
--- a/ld/scripttempl/elf32crx.sc
+++ b/ld/scripttempl/elf32crx.sc
@@ -169,7 +169,7 @@ SECTIONS
 
 EOF
 
-source_sh $srcdir/scripttempl/misc-sections.sc
+source_sh $srcdir/scripttempl/misc-sections.sc rom
 source_sh $srcdir/scripttempl/DWARF.sc
 
 cat <<EOF
diff --git a/ld/scripttempl/elfd10v.sc b/ld/scripttempl/elfd10v.sc
index 1ecf4a19092..8d139de8b51 100644
--- a/ld/scripttempl/elfd10v.sc
+++ b/ld/scripttempl/elfd10v.sc
@@ -102,6 +102,10 @@ EOF
 cat <<EOF
 SECTIONS
 {
+  /* PR 32100: GDB makes use of the fact that the .note.gnu.build-id
+     section is typically placed next to the ELF headers.  */
+  .note.gnu.build-id ${RELOCATING-0}: { *(.note.gnu.build-id) } ${RELOCATING+ >INSN}
+
   .text ${RELOCATING+${TEXT_START_ADDR}} :
   {
     ${RELOCATING+${TEXT_START_SYMBOLS}
@@ -175,7 +179,7 @@ SECTIONS
 
 EOF
 
-source_sh $srcdir/scripttempl/misc-sections.sc
+source_sh $srcdir/scripttempl/misc-sections.sc DATA
 source_sh $srcdir/scripttempl/DWARF.sc
 
 cat <<EOF
diff --git a/ld/scripttempl/elfxtensa.sc b/ld/scripttempl/elfxtensa.sc
index 231f53b5f5b..046e8724487 100644
--- a/ld/scripttempl/elfxtensa.sc
+++ b/ld/scripttempl/elfxtensa.sc
@@ -305,6 +305,10 @@ ${RELOCATING- /* For some reason, the Solaris linker makes bad executables
 
 SECTIONS
 {
+  /* PR 32100: GDB makes use of the fact that the .note.gnu.build-id
+     section is typically placed next to the ELF headers.  */
+  .note.gnu.build-id ${RELOCATING-0}: { *(.note.gnu.build-id) }
+
   /* Read-only sections, merged into text segment: */
   ${CREATE_SHLIB-${CREATE_PIE-${RELOCATING+PROVIDE (__executable_start = ${TEXT_START_ADDR}); . = ${TEXT_BASE_ADDRESS};}}}
   ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_TEXT_START_ADDR:-0} + SIZEOF_HEADERS;}}
@@ -549,6 +553,7 @@ source_sh $srcdir/scripttempl/DWARF.sc
 
 cat <<EOF
   ${ATTRS_SECTIONS}
+  .xtensa.info : { *(.xtensa.info) }
   ${OTHER_SECTIONS}
   ${RELOCATING+${OTHER_SYMBOLS}}
   ${RELOCATING+${DISCARDED}}
diff --git a/ld/scripttempl/mep.sc b/ld/scripttempl/mep.sc
index 39e4d64857c..2f085d75000 100644
--- a/ld/scripttempl/mep.sc
+++ b/ld/scripttempl/mep.sc
@@ -230,6 +230,11 @@ SECTIONS
   ${CREATE_SHLIB-${CREATE_PIE-${RELOCATING+PROVIDE (__executable_start = ${TEXT_START_ADDR}); . = ${TEXT_BASE_ADDRESS};}}}
   ${CREATE_SHLIB+${RELOCATING+. = ${SHLIB_TEXT_START_ADDR:-0} + SIZEOF_HEADERS;}}
   ${CREATE_PIE+${RELOCATING+. = ${SHLIB_TEXT_START_ADDR:-0} + SIZEOF_HEADERS;}}
+
+  /* PR 32100: GDB makes use of the fact that the .note.gnu.build-id
+     section is typically placed next to the ELF headers.  */
+  .note.gnu.build-id ${RELOCATING-0}: { *(.note.gnu.build-id) }
+
   ${CREATE_SHLIB-${INTERP}}
   ${INITIAL_READONLY_SECTIONS}
   ${TEXT_DYNAMIC+${DYNAMIC}}
diff --git a/ld/scripttempl/misc-sections.sc b/ld/scripttempl/misc-sections.sc
index 94ad7157b88..71b1a6ba643 100644
--- a/ld/scripttempl/misc-sections.sc
+++ b/ld/scripttempl/misc-sections.sc
@@ -3,10 +3,23 @@
 # Copying and distribution of this file, with or without modification,
 # are permitted in any medium without royalty provided the copyright
 # notice and this notice are preserved.
-#
+
+# This script fragment gathers together some of the more miscellaneous
+# sections that might appear in a linker's input.  It can be invoked
+# with a single parameter which is the memory region into which loadable
+# sections should be placed.  If the parameter is missing, no memory
+# section placement is used.
+
+if test "x$2" = "x" ;
+then
+  REGION=""
+else
+  REGION="> $2"
+fi
+
 cat <<EOF
   /* Stabs debugging sections.  */
-  .stab          0 : { *(.stab) }
+  .stab          0 : { *(.stab) } 
   .stabstr       0 : { *(.stabstr) }
   .stab.excl     0 : { *(.stab.excl) }
   .stab.exclstr  0 : { *(.stab.exclstr) }
@@ -17,4 +30,15 @@ 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/scripttempl/nds32elf.sc b/ld/scripttempl/nds32elf.sc
index 0f8366d91ac..ac7de72a326 100644
--- a/ld/scripttempl/nds32elf.sc
+++ b/ld/scripttempl/nds32elf.sc
@@ -597,6 +597,8 @@ SHLIB_LARGE_DATA_ADDR=". = SEGMENT_START(\"ldata-segment\", ${SHLIB_LARGE_DATA_A
   ${TINY_DATA_SECTION}
   ${TINY_BSS_SECTION}
   ${STACK_ADDR+${STACK}}
+
+  ${RELOCATING+${DISCARDED}}
 EOF
 
 test -z "${NON_ALLOC_DYN}" || emit_dyn
@@ -608,6 +610,5 @@ cat <<EOF
   ${ATTRS_SECTIONS}
   ${OTHER_SECTIONS}
   ${RELOCATING+${OTHER_SYMBOLS}}
-  ${RELOCATING+${DISCARDED}}
 }
 EOF
diff --git a/ld/scripttempl/v850.sc b/ld/scripttempl/v850.sc
index 10d1da8b5bb..7daea326d2f 100644
--- a/ld/scripttempl/v850.sc
+++ b/ld/scripttempl/v850.sc
@@ -19,6 +19,10 @@ SEARCH_DIR(.);
 ${RELOCATING+EXTERN(__ctbp __ep __gp)};
 SECTIONS
 {
+  /* PR 32100: GDB makes use of the fact that the .note.gnu.build-id
+     section is typically placed next to the ELF headers.  */
+  .note.gnu.build-id ${RELOCATING-0}: { *(.note.gnu.build-id) }
+
   /* This saves a little space in the ELF file, since the zda starts
      at a higher location that the ELF headers take up.  */
 
diff --git a/ld/scripttempl/v850_rh850.sc b/ld/scripttempl/v850_rh850.sc
index e63e7db8884..dd9d832d5fa 100644
--- a/ld/scripttempl/v850_rh850.sc
+++ b/ld/scripttempl/v850_rh850.sc
@@ -19,6 +19,10 @@ SEARCH_DIR(.);
 ${RELOCATING+EXTERN(__ctbp __ep __gp)};
 SECTIONS
 {
+  /* PR 32100: GDB makes use of the fact that the .note.gnu.build-id
+     section is typically placed next to the ELF headers.  */
+  .note.gnu.build-id ${RELOCATING-0}: { *(.note.gnu.build-id) }
+
   /* This saves a little space in the ELF file, since the zda starts
      at a higher location that the ELF headers take up.  */
 
diff --git a/ld/testsuite/ld-elf/pr23658-1a.d b/ld/testsuite/ld-elf/pr23658-1a.d
index 388f7a42fa7..21847f9350c 100644
--- a/ld/testsuite/ld-elf/pr23658-1a.d
+++ b/ld/testsuite/ld-elf/pr23658-1a.d
@@ -9,10 +9,8 @@
 # SHT_NOTE sections aren't grouped nor sorted.
 #xfail: [uses_genelf]
 #xfail: m68hc12-*
-# The following targets don't support --build-id.
-#xfail: cr16-* crx-* visium-*
 # The following targets place .note.gnu.build-id in unusual places.
-#xfail: pru-*
+#xfail: d10v-* pru-*
 
 #...
  +[0-9]+ +\.note\.4 \.note\.1 +
diff --git a/ld/testsuite/ld-elf/pr23658-1e.d b/ld/testsuite/ld-elf/pr23658-1e.d
index a281e48d730..992c7484ff7 100644
--- a/ld/testsuite/ld-elf/pr23658-1e.d
+++ b/ld/testsuite/ld-elf/pr23658-1e.d
@@ -14,10 +14,11 @@
 # The following targets don't support --build-id.
 #xfail: cr16-* crx-* visium-*
 # The following targets place .note.gnu.build-id in unusual places.
-#xfail: pru-*
+#xfail: *-*-hpux* arc*-* avr-* microblaze-*-* nds32*-* spu-*-*
 
 #...
  +[0-9]+ +\.note.gnu.build-id +
+#...
  +[0-9]+ +\.note\.4 \.note\.1 +
  +[0-9]+ +\.note\.2 \.note\.3 +
 #pass


More information about the Binutils-cvs mailing list