[binutils-gdb] BFD: Unify relocation error reporting
Maciej W. Rozycki
macro@sourceware.org
Tue Dec 9 12:39:43 GMT 2025
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8eeed95625ab7283032ed73e8866ace82daf30aa
commit 8eeed95625ab7283032ed73e8866ace82daf30aa
Author: Maciej W. Rozycki <macro@orcam.me.uk>
Date: Tue Dec 9 12:38:52 2025 +0000
BFD: Unify relocation error reporting
A couple of targets use virtually the same code for error reporting in
relocation processing. Merge the copies, removing small discrepancies
such as a fallback call to `abort' for NDS32 targets as per PR 17512,
and factor out for backends to use.
Diff:
---
bfd/coff-alpha.c | 41 ++---------------
bfd/elf32-nds32.c | 39 ++--------------
bfd/elfxx-mips.c | 55 ++--------------------
bfd/libbfd.h | 8 ++++
bfd/reloc.c | 134 +++++++++++++++++++++++++++++++++---------------------
5 files changed, 103 insertions(+), 174 deletions(-)
diff --git a/bfd/coff-alpha.c b/bfd/coff-alpha.c
index b20269b077b..d201e8d5de8 100644
--- a/bfd/coff-alpha.c
+++ b/bfd/coff-alpha.c
@@ -1154,43 +1154,10 @@ alpha_ecoff_get_relocated_section_contents (bfd *abfd,
if (r != bfd_reloc_ok)
{
- switch (r)
- {
- case bfd_reloc_undefined:
- (*link_info->callbacks->undefined_symbol)
- (link_info, bfd_asymbol_name (*rel->sym_ptr_ptr),
- input_bfd, input_section, rel->address, true);
- break;
- case bfd_reloc_dangerous:
- (*link_info->callbacks->reloc_dangerous)
- (link_info, err, input_bfd, input_section, rel->address);
- break;
- case bfd_reloc_overflow:
- (*link_info->callbacks->reloc_overflow)
- (link_info, NULL, bfd_asymbol_name (*rel->sym_ptr_ptr),
- rel->howto->name, rel->addend, input_bfd,
- input_section, rel->address);
- break;
- case bfd_reloc_outofrange:
- (*link_info->callbacks->einfo)
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
- input_bfd, input_section, rel);
- goto error_return;
- case bfd_reloc_notsupported:
- (*link_info->callbacks->einfo)
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
- input_bfd, input_section, rel);
- goto error_return;
- default:
- (*link_info->callbacks->einfo)
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\""
- " returns an unrecognized value %x\n"),
- input_bfd, input_section, rel, r);
- break;
- }
+ _bfd_link_reloc_status_error (abfd, link_info, input_section,
+ rel, err, r);
+ if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
+ goto error_return;
}
}
diff --git a/bfd/elf32-nds32.c b/bfd/elf32-nds32.c
index acfc659773d..07616acccd5 100644
--- a/bfd/elf32-nds32.c
+++ b/bfd/elf32-nds32.c
@@ -13162,41 +13162,10 @@ nds32_elf_get_relocated_section_contents (bfd *abfd,
if (r != bfd_reloc_ok)
{
- switch (r)
- {
- case bfd_reloc_undefined:
- (*link_info->callbacks->undefined_symbol)
- (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
- input_bfd, input_section, (*parent)->address, true);
- break;
- case bfd_reloc_dangerous:
- BFD_ASSERT (error_message != NULL);
- (*link_info->callbacks->reloc_dangerous)
- (link_info, error_message,
- input_bfd, input_section, (*parent)->address);
- break;
- case bfd_reloc_overflow:
- (*link_info->callbacks->reloc_overflow)
- (link_info, NULL,
- bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
- (*parent)->howto->name, (*parent)->addend,
- input_bfd, input_section, (*parent)->address);
- break;
- case bfd_reloc_outofrange:
- /* PR ld/13730:
- This error can result when processing some partially
- complete binaries. Do not abort, but issue an error
- message instead. */
- link_info->callbacks->einfo
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
- abfd, input_section, * parent);
- goto error_return;
-
- default:
- abort ();
- break;
- }
+ _bfd_link_reloc_status_error (abfd, link_info, input_section,
+ *parent, error_message, r);
+ if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
+ goto error_return;
}
}
}
diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c
index bf2ee4801bb..4db3cfb5fe9 100644
--- a/bfd/elfxx-mips.c
+++ b/bfd/elfxx-mips.c
@@ -13570,57 +13570,10 @@ _bfd_elf_mips_get_relocated_section_contents
if (r != bfd_reloc_ok)
{
- switch (r)
- {
- case bfd_reloc_undefined:
- (*link_info->callbacks->undefined_symbol)
- (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
- input_bfd, input_section, (*parent)->address, true);
- break;
- case bfd_reloc_dangerous:
- BFD_ASSERT (error_message != NULL);
- (*link_info->callbacks->reloc_dangerous)
- (link_info, error_message,
- input_bfd, input_section, (*parent)->address);
- break;
- case bfd_reloc_overflow:
- (*link_info->callbacks->reloc_overflow)
- (link_info, NULL,
- bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
- (*parent)->howto->name, (*parent)->addend,
- input_bfd, input_section, (*parent)->address);
- break;
- case bfd_reloc_outofrange:
- /* PR ld/13730:
- This error can result when processing some partially
- complete binaries. Do not abort, but issue an error
- message instead. */
- link_info->callbacks->einfo
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
- abfd, input_section, * parent);
- goto error_return;
-
- case bfd_reloc_notsupported:
- /* PR ld/17512
- This error can result when processing a corrupt binary.
- Do not abort. Issue an error message instead. */
- link_info->callbacks->einfo
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
- abfd, input_section, * parent);
- goto error_return;
-
- default:
- /* PR 17512; file: 90c2a92e.
- Report unexpected results, without aborting. */
- link_info->callbacks->einfo
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
- abfd, input_section, * parent, r);
- break;
- }
-
+ _bfd_link_reloc_status_error (abfd, link_info, input_section,
+ *parent, error_message, r);
+ if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
+ goto error_return;
}
}
}
diff --git a/bfd/libbfd.h b/bfd/libbfd.h
index 6a2eff436a7..c22665935c9 100644
--- a/bfd/libbfd.h
+++ b/bfd/libbfd.h
@@ -3628,6 +3628,14 @@ bool _bfd_unrecognized_reloc
sec_ptr section,
unsigned int r_type) ATTRIBUTE_HIDDEN;
+void _bfd_link_reloc_status_error
+ (bfd *abfd,
+ struct bfd_link_info *link_info,
+ asection *input_section,
+ arelent *reloc_entry,
+ char *error_message,
+ bfd_reloc_status_type r) ATTRIBUTE_HIDDEN;
+
/* Extracted from section.c. */
#define BFD_FAKE_SECTION(SEC, SYM, NAME, IDX, FLAGS) \
/* name, next, prev, id, section_id, index, flags, user_set_vma, */ \
diff --git a/bfd/reloc.c b/bfd/reloc.c
index 45ba8612e97..112f3439c70 100644
--- a/bfd/reloc.c
+++ b/bfd/reloc.c
@@ -8555,57 +8555,10 @@ bfd_generic_get_relocated_section_contents (bfd *abfd,
if (r != bfd_reloc_ok)
{
- switch (r)
- {
- case bfd_reloc_undefined:
- (*link_info->callbacks->undefined_symbol)
- (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
- input_bfd, input_section, (*parent)->address, true);
- break;
- case bfd_reloc_dangerous:
- BFD_ASSERT (error_message != NULL);
- (*link_info->callbacks->reloc_dangerous)
- (link_info, error_message,
- input_bfd, input_section, (*parent)->address);
- break;
- case bfd_reloc_overflow:
- (*link_info->callbacks->reloc_overflow)
- (link_info, NULL,
- bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
- (*parent)->howto->name, (*parent)->addend,
- input_bfd, input_section, (*parent)->address);
- break;
- case bfd_reloc_outofrange:
- /* PR ld/13730:
- This error can result when processing some partially
- complete binaries. Do not abort, but issue an error
- message instead. */
- link_info->callbacks->einfo
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
- abfd, input_section, * parent);
- goto error_return;
-
- case bfd_reloc_notsupported:
- /* PR ld/17512
- This error can result when processing a corrupt binary.
- Do not abort. Issue an error message instead. */
- link_info->callbacks->einfo
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
- abfd, input_section, * parent);
- goto error_return;
-
- default:
- /* PR 17512; file: 90c2a92e.
- Report unexpected results, without aborting. */
- link_info->callbacks->einfo
- /* xgettext:c-format */
- (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
- abfd, input_section, * parent, r);
- break;
- }
-
+ _bfd_link_reloc_status_error (abfd, link_info, input_section,
+ *parent, error_message, r);
+ if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
+ goto error_return;
}
}
}
@@ -8680,6 +8633,85 @@ _bfd_unrecognized_reloc (bfd * abfd, sec_ptr section, unsigned int r_type)
return false;
}
+/*
+INTERNAL_FUNCTION
+ _bfd_link_reloc_status_error
+
+SYNOPSIS
+ void _bfd_link_reloc_status_error
+ (bfd *abfd,
+ struct bfd_link_info *link_info,
+ asection *input_section,
+ arelent *reloc_entry,
+ char *error_message,
+ bfd_reloc_status_type r);
+
+DESCRIPTION
+ Mark a link relocation error according to R, with a suitable
+ message as applicable.
+ Written as a function in order to reduce code duplication.
+*/
+
+void
+_bfd_link_reloc_status_error (bfd *abfd, struct bfd_link_info *link_info,
+ asection *input_section, arelent *reloc_entry,
+ char *error_message, bfd_reloc_status_type r)
+{
+ bfd_size_type reloc_address = reloc_entry->address;
+ bfd *input_bfd = input_section->owner;
+
+ switch (r)
+ {
+ case bfd_reloc_ok:
+ break;
+ case bfd_reloc_undefined:
+ (*link_info->callbacks->undefined_symbol)
+ (link_info, bfd_asymbol_name (*reloc_entry->sym_ptr_ptr),
+ input_bfd, input_section, reloc_address, true);
+ break;
+ case bfd_reloc_dangerous:
+ BFD_ASSERT (error_message != NULL);
+ (*link_info->callbacks->reloc_dangerous)
+ (link_info, error_message,
+ input_bfd, input_section, reloc_address);
+ break;
+ case bfd_reloc_overflow:
+ (*link_info->callbacks->reloc_overflow)
+ (link_info, NULL,
+ bfd_asymbol_name (*reloc_entry->sym_ptr_ptr),
+ reloc_entry->howto->name, reloc_entry->addend,
+ input_bfd, input_section, reloc_address);
+ break;
+ case bfd_reloc_outofrange:
+ /* PR ld/13730:
+ This error can result when processing some partially
+ complete binaries. Do not abort, but issue an error
+ message instead. */
+ link_info->callbacks->einfo
+ /* xgettext:c-format */
+ (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
+ abfd, input_section, reloc_entry);
+ break;
+ case bfd_reloc_notsupported:
+ /* PR ld/17512
+ This error can result when processing a corrupt binary.
+ Do not abort. Issue an error message instead. */
+ link_info->callbacks->einfo
+ /* xgettext:c-format */
+ (_("%X%P: %pB(%pA): relocation \"%pR\" is not supported\n"),
+ abfd, input_section, reloc_entry);
+ break;
+ default:
+ /* PR 17512; file: 90c2a92e.
+ Report unexpected results, without aborting. */
+ link_info->callbacks->einfo
+ /* xgettext:c-format */
+ (_("%X%P: %pB(%pA): relocation \"%pR\" returns an unrecognized value %x\n"),
+ abfd, input_section, reloc_entry, r);
+ break;
+ }
+}
+
reloc_howto_type *
_bfd_norelocs_bfd_reloc_type_lookup
(bfd *abfd,
More information about the Binutils-cvs
mailing list