From 5ddc0d3a01f32b5f8c6bb3dacde2d490083fc592 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 v2] riscv: Ignore attributes in input file with empty code
 section

Ignore attributes in input file with empty code section 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)

bfd/

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

	PR ld/34185
	* testsuite/ld-elf/pr17068d.s: Make text section non-empty in the
	only input file to create a shared library with the proper ELF
	header for riscv.
	* testsuite/ld-elf/pr20995c.s: Likewise.
	* testsuite/ld-elf/pr22471a.s: Likewise.
	* testsuite/ld-elf/pr25458b.s: Likewise.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 bfd/elfnn-riscv.c              | 8 +++++---
 ld/testsuite/ld-elf/pr17068d.s | 4 ++++
 ld/testsuite/ld-elf/pr20995c.s | 4 ++++
 ld/testsuite/ld-elf/pr22471a.s | 5 ++++-
 ld/testsuite/ld-elf/pr25458b.s | 4 ++++
 5 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 295d02e69c2..e8e5f862155 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -4420,9 +4420,11 @@ _bfd_riscv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
 	{
 	  null_input_bfd = false;
 
-	  if ((bfd_section_flags (sec)
-	       & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
-	      == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
+	  /* Ignore empty code section.  */
+	  if (sec->size != 0
+	      && ((bfd_section_flags (sec)
+		   & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
+		  == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS)))
 	    {
 	      only_data_sections = false;
 	      break;
diff --git a/ld/testsuite/ld-elf/pr17068d.s b/ld/testsuite/ld-elf/pr17068d.s
index 6165128a672..464606927e9 100644
--- a/ld/testsuite/ld-elf/pr17068d.s
+++ b/ld/testsuite/ld-elf/pr17068d.s
@@ -1,3 +1,7 @@
+# Make text section non-empty in the only input file to create a shared
+# library with the proper ELF header for riscv.
+	.text
+	.byte 0
  .data
  .globl d
  .type d,%object
diff --git a/ld/testsuite/ld-elf/pr20995c.s b/ld/testsuite/ld-elf/pr20995c.s
index ccca92d1aae..aa8e252ad69 100644
--- a/ld/testsuite/ld-elf/pr20995c.s
+++ b/ld/testsuite/ld-elf/pr20995c.s
@@ -1,3 +1,7 @@
+# Make text section non-empty in the only input file to create a shared
+# library with the proper ELF header for riscv.
+	.text
+	.byte 0
 	.data
 	.type rw,%object
 	.globl rw
diff --git a/ld/testsuite/ld-elf/pr22471a.s b/ld/testsuite/ld-elf/pr22471a.s
index 3bb6a92bdbd..f659423e8c1 100644
--- a/ld/testsuite/ld-elf/pr22471a.s
+++ b/ld/testsuite/ld-elf/pr22471a.s
@@ -1 +1,4 @@
-# Empty input.
+# Make text section non-empty in the only input file to create a shared
+# library with the proper ELF header for riscv.
+	.text
+	.byte 0
diff --git a/ld/testsuite/ld-elf/pr25458b.s b/ld/testsuite/ld-elf/pr25458b.s
index ff64cc7584b..7a4943cb9c2 100644
--- a/ld/testsuite/ld-elf/pr25458b.s
+++ b/ld/testsuite/ld-elf/pr25458b.s
@@ -1,3 +1,7 @@
+# Make text section non-empty in the only input file to create a shared
+# library with the proper ELF header for riscv.
+	.text
+	.byte 0
 	.data
 	.globl __environ
 	.type __environ,%object
-- 
2.54.0

