ld: microblaze: Prevent out-of-bounds read in relax_section
Fixes microblaze linker relaxation by preventing an out-of-bounds read from the local symbol cache.
This commit corrects a bug in the microblaze linker’s section relaxation process that caused an out-of-bounds read from the local symbol cache. The issue stemmed from a copied function missing a bounds check present in the original. This error could silently corrupt relocation addends, leading to miscompilations, particularly affecting structure member accesses after optimization. The fix restores the missing bounds check and adds new test cases to verify relocation addend integrity during relaxation.
In Details
The microblaze_elf_relax_section() function, originally copied from sh_elf_relax_delete_bytes(), lacked a crucial bounds check on symbol table indexing. This commit adds a two-line guard, mirroring bfd/elf32-sh.c, to prevent out-of-bounds reads from isymbuf. The bug occurs when isymbuf (containing local symbols) is indexed with a global symbol index that exceeds its bounds, corrupting relocation addends. The fix ensures that relocation addends are correctly preserved during section relaxation, especially in scenarios involving gc-sections or .eh_frame editing.
- ld
- The linker program, responsible for combining object files and libraries into an executable or shared library.
- microblaze
- A 32-bit RISC soft processor core developed by Xilinx, often used in embedded systems. Binutils provides support for targeting this architecture.
- Section relaxation
- A linker optimization process where certain instructions or data layouts are adjusted to occupy less space, often by replacing multi-byte instructions with shorter equivalents.
- Local symbol cache
- A temporary storage or index within the linker that holds information about local symbols within a section or object file, used during optimization processes like relaxation.
- Relocation addend
- A value embedded within a relocation entry that is added to the final symbol address to compute the correct memory location for a reference.
- gc-sections
- Garbage collection of sections. A linker option that discards unused sections (code or data) from the final executable, reducing its size.
- .eh_frame
- A section in an ELF file that contains DWARF debugging information, specifically exception handling information used by debuggers and runtime environments.