binutils Newspaper
OCTOBER 6, 2026
ld — Proposed — Sentiment 5 / 10

ld: Check bfd_gc_sections return

Ensures the linker (`ld`) properly handles errors returned by `bfd_gc_sections`, specifically when `bfd_error_bad_value` is set.

This patch for the GNU linker (ld) improves error handling by checking the return value of bfd_gc_sections. This function can return false and set bfd_error_bad_value on invalid input, as seen in a reloc symbol index error. The change ensures that ld reports a fatal error in such cases, preventing potentially misleading or incomplete diagnostics. This addresses a specific bug report (PR ld/34448).

In the Thread 6 participants
  1. H.J. Lu proposer

    Submits a patch to make the linker check the return value of `bfd_gc_sections` and handle `bfd_error_bad_value` as a fatal error.

    “bfd_gc_sections may return false on invalid input: ld: pr34448-bug_18.o: bad reloc symbol index (0xf2000005 >= 0x13) for offset 0x4 in section `.text.get_tls[get_tls]' and set the BFD error to bfd_error_bad_value. Check bfd_gc_sections return and report the fatal error if the BFD error is bfd_error_bad_value.”
  2. Jan Beulich reviewer

    Questions the guarantee that `bfd_error_bad_value` is only set for the specific error condition being addressed.

    “And what guarantees that bfd_error_bad_value isn't also set for other kinds of errors, from about anywhere in the library? Does bfd_gc_”
  3. H.J. Lu proposer

    Responds to Jan Beulich's concern, indicating further discussion or clarification is needed.

    “And what guarantees that bfd_error_bad_value i”
  4. Jan Beulich reviewer

    Continues the discussion about the scope of `bfd_error_bad_value`.

    “And ”
  5. H.J. Lu proposer

    Continues to engage in the discussion about error handling.

    “And”
  6. Alan Modra contributor

    Weighs in on the discussion, likely providing context or further questions about error handling.

In Details

This patch addresses error handling within the GNU linker (ld) related to garbage collection of sections. The bfd_gc_sections function, which is responsible for this, can fail and report bfd_error_bad_value for various reasons, including invalid relocation symbol indices. The proposed change ensures that ld treats this specific error as fatal, preventing it from proceeding with potentially corrupt link results. The discussion involves clarifying whether bfd_error_bad_value might be set for other, non-fatal errors, which could impact the robustness of this fix.

For Context
ld
The GNU linker, which combines object files and libraries into an executable or another object file.
bfd_gc_sections
A function within the BFD library that performs garbage collection on sections, removing unreferenced ones.
bfd_error_bad_value
An error code from the BFD library indicating that a value encountered is invalid or out of range.
relocation
Information in an object file that specifies how to modify code or data addresses when the file is linked or loaded.
reloc symbol index
The index of a symbol within the symbol table, used in relocation entries.
PR ld/34448
Problem Report number 34448 in the binutils linker component, likely referencing the bug this patch aims to fix.
Filed Under: linkererror-handlingBFD