LoongArch: Discard unused .eh_frame sections in ld -r
Fixes linker errors caused by stale .eh_frame relocations in relocatable links.
This patch addresses an issue where the linker (ld -r) with garbage collection (--gc-sections) retained stale .eh_frame relocations for discarded sections. This could lead to a bad relocation offset during a subsequent final link. The change ensures that bfd_elf_discard_info is called for relocatable links, preventing these errors.
- proposer
Introduces a change to call `bfd_elf_discard_info` for relocatable links to prevent stale .eh_frame relocations, and adds a new test case to verify the fix.
“Previously, bfd_elf_discard_info was skipped for relocatable links. With --gc-sections, ld -r retained the FDE of a discarded section. This left a stale .eh_frame relocation pointing beyond the end of the section, so a following final link could fail with a bad relocation offset. Call bfd_elf_discard_info for relocatable links.”
In Details
This patch modifies ld/emultempl/loongarchelf.em to call bfd_elf_discard_info even for relocatable links. Previously, this function was skipped in relocatable linking, causing --gc-sections to retain stale Frame Description Entries (FDEs) in .eh_frame. These stale entries resulted in invalid relocation offsets that could break a final link. The change ensures that unused .eh_frame sections are correctly discarded during relocatable linking.
- .eh_frame
- A standard ELF section containing Call Frame Information (CFI) used for stack unwinding and exception handling.
- FDE
- Frame Description Entry. A data structure within the .eh_frame section that describes how to unwind the stack for a particular function.
- ld -r
- A linker option that performs a partial link, producing a relocatable object file from multiple input object files.
- --gc-sections
- A linker option that removes unused sections from the output. Garbage Collection (GC) of sections.
- relocatable link
- A linking process that produces an object file that can be further linked with other object files. This is often an intermediate step before a final executable or shared library is created.