riscv: Ignore attributes in empty input file

Message ID CAMe9rOpM413U8i5oFK=Um4d3ns3+bAyYWf3EAqq7gSMFaXjHhA@mail.gmail.com
State New
Headers
Series riscv: Ignore attributes in empty input file |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_binutils_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_binutils_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-aarch64 success Test passed

Commit Message

H.J. Lu May 28, 2026, 9:29 a.m. UTC
  Ignore empty and .note.gnu.build-id sections when merging attributes.
The following linker tests now pass for riscv:

FAIL: Diagnostics - Wrong magic number mixed with valid CTF sections
FAIL: --gc-sections with --defsym
FAIL: --gc-sections with KEEP
FAIL: --gc-sections with __start_SECTIONNAME
FAIL: ld-plugin/lto-10r
FAIL: ld-plugin/lto-3r
FAIL: ld-plugin/lto-4r-a
FAIL: ld-plugin/lto-4r-b
FAIL: ld-plugin/lto-4r-c
FAIL: ld-plugin/lto-4r-d
FAIL: ld-plugin/lto-5r
FAIL: PR ld/19317 (2)
FAIL: PR ld/19317 (3)

PR ld/34185
* elfnn-riscv.c (_bfd_riscv_elf_merge_private_bfd_data): Ignore
attributes in empty input file
  

Patch

From 4147af9a6b6ecf632e4309d6e7ec275bf5b9ffb6 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Thu, 28 May 2026 16:10:19 +0800
Subject: [PATCH] riscv: Ignore attributes in empty input file

Ignore empty and .note.gnu.build-id sections when merging attributes.
The following linker tests now pass for riscv:

FAIL: Diagnostics - Wrong magic number mixed with valid CTF sections
FAIL: --gc-sections with --defsym
FAIL: --gc-sections with KEEP
FAIL: --gc-sections with __start_SECTIONNAME
FAIL: ld-plugin/lto-10r
FAIL: ld-plugin/lto-3r
FAIL: ld-plugin/lto-4r-a
FAIL: ld-plugin/lto-4r-b
FAIL: ld-plugin/lto-4r-c
FAIL: ld-plugin/lto-4r-d
FAIL: ld-plugin/lto-5r
FAIL: PR ld/19317 (2)
FAIL: PR ld/19317 (3)

	PR ld/34185
	* elfnn-riscv.c (_bfd_riscv_elf_merge_private_bfd_data): Ignore
	attributes in empty input file

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 bfd/elfnn-riscv.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index eb3d7926c26..8f8a716ef52 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -4414,22 +4414,26 @@  _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
     {
       bool null_input_bfd = true;
       bool only_data_sections = true;
+      bool has_load_contents = false;
       asection *sec;
 
       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
 	{
 	  null_input_bfd = false;
 
-	  if ((bfd_section_flags (sec)
-	       & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
-	      == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
+	  if ((bfd_section_flags (sec) & (SEC_LOAD | SEC_HAS_CONTENTS))
+	      == (SEC_LOAD | SEC_HAS_CONTENTS))
 	    {
-	      only_data_sections = false;
-	      break;
+	      /* Ignore empty and .note.gnu.build-id sections.  */
+	      if (sec->size != 0
+		  && strcmp (sec->name, ".note.gnu.build-id") != 0)
+		has_load_contents = true;
+	      if ((bfd_section_flags (sec) & SEC_CODE) != 0)
+		only_data_sections = false;
 	    }
 	}
 
-      if (null_input_bfd || only_data_sections)
+      if (!has_load_contents || null_input_bfd || only_data_sections)
 	return true;
     }
 
-- 
2.54.0