[PATCH] ld: Clear SEC_ALLOC for NOLOAD notes sections

H.J. Lu hjl.tools@gmail.com
Thu Mar 13 19:14:50 GMT 2025


If a note section is marked as NOLOAD, clear the SEC_ALLOC so that it
won't be treated as an SHF_ALLOC section, like a .bss section.

	PR ld/32787
	* ldlang.c (lang_add_section): Clear SEC_ALLOC for NOLOAD note
	sections.
	* testsuite/ld-elf/pr32787.d: New file.
	* testsuite/ld-elf/pr32787.t: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 ld/ldlang.c                   |  6 ++++--
 ld/testsuite/ld-elf/pr32787.d |  9 +++++++++
 ld/testsuite/ld-elf/pr32787.t | 12 ++++++++++++
 3 files changed, 25 insertions(+), 2 deletions(-)
 create mode 100644 ld/testsuite/ld-elf/pr32787.d
 create mode 100644 ld/testsuite/ld-elf/pr32787.t

diff --git a/ld/ldlang.c b/ld/ldlang.c
index 011c5262f87..a4b803613a4 100644
--- a/ld/ldlang.c
+++ b/ld/ldlang.c
@@ -2827,8 +2827,10 @@ lang_add_section (lang_statement_list_type *ptr,
       /* Unfortunately GNU ld has managed to evolve two different
 	 meanings to NOLOAD in scripts.  ELF gets a .bss style noload,
 	 alloc, no contents section.  All others get a noload, noalloc
-	 section.  */
-      if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour)
+	 section.  Unlike a .bss section, if a note section is marked
+	 as NOLOAD, also clear SEC_ALLOC.  */
+      if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour
+	  && !startswith (section->name, ".note."))
 	flags &= ~SEC_HAS_CONTENTS;
       else
 	flags &= ~SEC_ALLOC;
diff --git a/ld/testsuite/ld-elf/pr32787.d b/ld/testsuite/ld-elf/pr32787.d
new file mode 100644
index 00000000000..8d07b8d7914
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr32787.d
@@ -0,0 +1,9 @@
+#source: start.s
+#ld: --build-id -T pr32787.t
+#nm: -n
+# The following targets don't support --build-id.
+#xfail: [uses_genelf]
+
+#...
+.* start
+#pass
diff --git a/ld/testsuite/ld-elf/pr32787.t b/ld/testsuite/ld-elf/pr32787.t
new file mode 100644
index 00000000000..cf5aeb18ad1
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr32787.t
@@ -0,0 +1,12 @@
+SECTIONS {
+	. = SIZEOF_HEADERS;
+	.text : {
+		*(.text)
+	}
+	.data : {
+		_data_start = .;
+		*(.data)
+	}
+	.note.gnu.build-id (NOLOAD) : { *(.note.gnu.build-id) }
+	/DISCARD/ : { *(*) }
+}
-- 
2.48.1



More information about the Binutils mailing list