LoongArch: Detect relocations out of section range
Adds checks to detect and reject relocations outside the section range during final linking for LoongArch.
This patch introduces checks in the LoongArch ELF backend to ensure that relocations are within the bounds of their input section. It utilizes bfd_reloc_offset_in_range in check_relocs to reject invalid relocations during the final linking phase. New test cases are also added to verify this functionality.
- proposer
Adds checks to reject relocations whose fields are out of the input section range by using `bfd_reloc_offset_in_range`.
“Use bfd_reloc_offset_in_range in check_relocs to reject relocations whose fields are out of the input section range during final linking.”
- reviewer
Suggests that the relocation checks might be better placed in `relocate_section` instead of `check_relocs`, as `check_relocs` is not called for all sections.
“This is better done in relocate_section, unless loongarch reads section contents in check_relocs. check_relocs is not called for some sections (see _bfd_elf_link_iterate_on_relocs), so for instance, debug sections won't have their relocations sanity checked.”
Technical Tradeoffs
- The reviewer questions the placement of the check, suggesting `relocate_section` might be more appropriate than `check_relocs` for comprehensive coverage.
In Details
This patch enhances the LoongArch ELF backend by adding validation for relocation offsets. It prevents relocations from referencing addresses outside their designated section by using bfd_reloc_offset_in_range within elfnn-loongarch.c. The reviewer suggests moving this check to relocate_section for more comprehensive coverage, as check_relocs has limitations.
- LoongArch
- A RISC-V based instruction set architecture.
- relocation
- A modification to an instruction or data in an object file that is required to resolve external references or to adapt code for execution in a specific memory address.
- ELF
- Executable and Linkable Format, a common standard for object file formats on Unix-like systems.
- bfd_reloc_offset_in_range
- A BFD library function that checks if a relocation's offset falls within the boundaries of a given section.