x86: Improve relocation error reporting

Message ID CAMe9rOptwXvtZ1iB=tJc21KGAs2kaZr6pFmW_XbKh2PNoc2Kjw@mail.gmail.com
State New
Headers
Series x86: Improve relocation error reporting |

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:36 a.m. UTC
  For bfd_reloc_outofrange relocation error, instead of

ld: pr34448-bug_4.o(.debug_addr+0x7f000008): reloc against
`.text.get_tls': error 4

linker now reports:

ld: pr34448-bug_4.o(.debug_addr+0x7f000008): relocation `R_X86_64_64'
against `.text.get_tls' is out of section range

PR ld/34448
* elf32-i386.c (elf_i386_relocate_section): Call
_bfd_x86_elf_link_report_relocation_error for relocation error.
* elf64-x86-64.c (elf_x86_64_relocate_section): Likewise.
* elfxx-x86.c (_bfd_x86_elf_link_report_relocation_error): New.
* elfxx-x86.h (_bfd_x86_elf_link_report_relocation_error): New.
  

Comments

H.J. Lu Aug. 5, 2026, 9:55 p.m. UTC | #1
On Tue, Aug 4, 2026 at 11:36 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> For bfd_reloc_outofrange relocation error, instead of
>
> ld: pr34448-bug_4.o(.debug_addr+0x7f000008): reloc against
> `.text.get_tls': error 4
>
> linker now reports:
>
> ld: pr34448-bug_4.o(.debug_addr+0x7f000008): relocation `R_X86_64_64'
> against `.text.get_tls' is out of section range
>
> PR ld/34448
> * elf32-i386.c (elf_i386_relocate_section): Call
> _bfd_x86_elf_link_report_relocation_error for relocation error.
> * elf64-x86-64.c (elf_x86_64_relocate_section): Likewise.
> * elfxx-x86.c (_bfd_x86_elf_link_report_relocation_error): New.
> * elfxx-x86.h (_bfd_x86_elf_link_report_relocation_error): New.
>
>

This is the patch I am checking in.
  

Patch

From 634b2757c1a8b5203906c3f60a243f10e165d2f0 Mon Sep 17 00:00:00 2001
From: "H.J. Lu" <hjl.tools@gmail.com>
Date: Fri, 31 Jul 2026 05:47:15 +0800
Subject: [PATCH] x86: Improve relocation error reporting

For bfd_reloc_outofrange relocation error, instead of

ld: pr34448-bug_4.o(.debug_addr+0x7f000008): reloc against `.text.get_tls': error 4

linker now reports:

ld: pr34448-bug_4.o(.debug_addr+0x7f000008): relocation `R_X86_64_64' against `.text.get_tls' is out of section range

	PR ld/34448
	* elf32-i386.c (elf_i386_relocate_section): Call
	_bfd_x86_elf_link_report_relocation_error for relocation error.
	* elf64-x86-64.c (elf_x86_64_relocate_section): Likewise.
	* elfxx-x86.c (_bfd_x86_elf_link_report_relocation_error): New.
	* elfxx-x86.h (_bfd_x86_elf_link_report_relocation_error): New.

Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
---
 bfd/elf32-i386.c   |  9 ++++-----
 bfd/elf64-x86-64.c |  9 ++++-----
 bfd/elfxx-x86.c    | 33 +++++++++++++++++++++++++++++++++
 bfd/elfxx-x86.h    |  4 ++++
 4 files changed, 45 insertions(+), 10 deletions(-)

diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c
index 8ad5cb4e354..36a957d03f1 100644
--- a/bfd/elf32-i386.c
+++ b/bfd/elf32-i386.c
@@ -3648,11 +3648,10 @@  elf_i386_relocate_section (struct bfd_link_info *info,
 	       (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
 	  else
 	    {
-	      _bfd_error_handler
-		/* xgettext:c-format */
-		(_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
-		 input_bfd, input_section,
-		 (uint64_t) rel->r_offset, name, (int) r);
+	      _bfd_x86_elf_link_report_relocation_error (input_bfd,
+							 input_section,
+							 name, rel,
+							 howto, r);
 	      return false;
 	    }
 	}
diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c
index 3d85886a3d5..49b2b88e2af 100644
--- a/bfd/elf64-x86-64.c
+++ b/bfd/elf64-x86-64.c
@@ -5152,11 +5152,10 @@  elf_x86_64_relocate_section (struct bfd_link_info *info,
 	    }
 	  else
 	    {
-	      _bfd_error_handler
-		/* xgettext:c-format */
-		(_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
-		 input_bfd, input_section,
-		 (uint64_t) rel->r_offset, name, (int) r);
+	      _bfd_x86_elf_link_report_relocation_error (input_bfd,
+							 input_section,
+							 name, rel,
+							 howto, r);
 	      return false;
 	    }
 	}
diff --git a/bfd/elfxx-x86.c b/bfd/elfxx-x86.c
index e08fec52636..439cbe2e9e7 100644
--- a/bfd/elfxx-x86.c
+++ b/bfd/elfxx-x86.c
@@ -3424,6 +3424,39 @@  _bfd_x86_elf_link_report_error
   bfd_set_error (bfd_error_bad_value);
 }
 
+/* Report link relocation error.  */
+
+void
+_bfd_x86_elf_link_report_relocation_error (bfd *abfd, asection *sec,
+					   const char *name,
+					   Elf_Internal_Rela *rel,
+					   reloc_howto_type *howto,
+					   bfd_reloc_status_type status)
+{
+  switch (status)
+    {
+    case bfd_reloc_outofrange:
+      _bfd_error_handler
+	/* xgettext:c-format */
+	(_("%pB(%pA+%#" PRIx64 "): relocation `%s' against `%s' is "
+	   "out of section range"), abfd, sec, (uint64_t) rel->r_offset,
+	 howto->name, name);
+      break;
+
+    case bfd_reloc_notsupported:
+      _bfd_error_handler
+	/* xgettext:c-format */
+	(_("%pB(%pA+%#" PRIx64 "): relocation `%s' against `%s' is "
+	   "not supported"), abfd, sec, (uint64_t) rel->r_offset,
+	 howto->name, name);
+      break;
+
+    default:
+      abort ();
+      break;
+    }
+}
+
 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section.  */
 
 bool
diff --git a/bfd/elfxx-x86.h b/bfd/elfxx-x86.h
index 430d61d3097..48e8ab4ad73 100644
--- a/bfd/elfxx-x86.h
+++ b/bfd/elfxx-x86.h
@@ -962,6 +962,10 @@  extern void _bfd_x86_elf_link_report_error
    Elf_Internal_Sym *, reloc_howto_type *,
    enum elf_x86_error_type) ATTRIBUTE_HIDDEN;
 
+extern void _bfd_x86_elf_link_report_relocation_error
+  (bfd *, asection *, const char *, Elf_Internal_Rela *,
+   reloc_howto_type *, bfd_reloc_status_type) ATTRIBUTE_HIDDEN;
+
 extern bool
 _bfd_elf_x86_copy_special_section_fields
   (const bfd *, bfd *, const Elf_Internal_Shdr *,
-- 
2.55.0