[v2] riscv: Ignore attributes in input file with empty code section

Message ID CAMe9rOq9Rf=4OfnXgaTo1X-pSychr6ikCRd9oh0fwx2K-ZGqCQ@mail.gmail.com
State New
Headers
Series [v2] riscv: Ignore attributes in input file with empty code section |

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-aarch64 success Test passed
linaro-tcwg-bot/tcwg_binutils_check--master-arm success Test passed

Commit Message

H.J. Lu May 28, 2026, 9:45 p.m. UTC
  On Thu, May 28, 2026 at 5:29 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> 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
>

Here is the v2 patch to ignore attributes in input file with empty code section
when merging attributes.
  

Comments

Jan Beulich June 2, 2026, 8:04 a.m. UTC | #1
On 28.05.2026 23:45, H.J. Lu wrote:
> On Thu, May 28, 2026 at 5:29 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>>
>> 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
> 
> Here is the v2 patch to ignore attributes in input file with empty code section
> when merging attributes.

I'm not sure if this is correct: Attributes may have a meaning even if there are
only empty code sections (or none at all). RISC-V folks will need to comment
here.

Jan
  

Patch

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