x86: Check if needed dynamic relocation section is created

Message ID CAMe9rOpTBfSWyDtPphPOqaeazfxBiOUPsFfNB5wKgzEuf9ewFg@mail.gmail.com
State New
Headers
Series x86: Check if needed dynamic relocation section is created |

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 Aug. 4, 2026, 3:39 a.m. UTC
  Since elf_link_read_relocs_from_section aborts for bad relocation,
further relocations won't be processed and needed dynamic relocation
section won't be created.  Skip dynamic relocation count if needed
dynamic relocation section hasn't been created.

PR ld/34448
* elfxx-x86.c (_bfd_x86_elf_late_size_sections): Skip dynamic
relocation count if needed dynamic relocation section hasn't been
created.
  

Patch

From c9c25b5a0aedcfb9ed591fe8ead9429e5210bd82 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Sat, 1 Aug 2026 11:23:39 +0800
Subject: [PATCH] x86: Check if needed dynamic relocation section is created

Since elf_link_read_relocs_from_section aborts for bad relocation,
further relocations won't be processed and needed dynamic relocation
section won't be created.  Skip dynamic relocation count if needed
dynamic relocation section hasn't been created.

	PR ld/34448
	* elfxx-x86.c (_bfd_x86_elf_late_size_sections): Skip dynamic
	relocation count if needed dynamic relocation section hasn't been
	created.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 bfd/elfxx-x86.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c
index 439cbe2e9e7..b1b64183c9c 100644
--- a/bfd/elfxx-x86.c
+++ b/bfd/elfxx-x86.c
@@ -2331,6 +2331,19 @@  _bfd_x86_elf_late_size_sections (struct bfd_link_info *info)
 	      else if (p->count != 0)
 		{
 		  srel = elf_section_data (p->sec)->sreloc;
+		  if (!srel)
+		    {
+		      /* NB: Since elf_link_read_relocs_from_section
+			 aborts for bad relocation, further relocations
+			 won't be processed and needed dynamic relocation
+			 section won't be created afterwards.  */
+		      _bfd_error_handler
+			/* xgettext:c-format */
+			(_("%pB: dynamic relocation section is needed "
+			   "for section `%pA'"),
+			 p->sec->owner, p->sec);
+		      continue;
+		    }
 		  srel->size += p->count * htab->sizeof_reloc;
 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0
 		      && (info->flags & DF_TEXTREL) == 0)
-- 
2.55.0