RISC-V: Prevent objcopy/strip from adding attributes section
Fixes objcopy and strip on older LLD versions not to break binaries by adding RISC-V attributes program headers.
This change prevents objcopy and strip from adding a PT_RISCV_ATTRIBUTES program header to RISC-V ELF files when the linker (LLD) version is older than 17. Older LLD versions do not create this header, and attempting to add it later can corrupt binaries by failing to reserve space for program headers.
In Details
This commit addresses a bug in binutils-gdb where the RISC-V ELF backend incorrectly attempted to add a PT_RISCV_ATTRIBUTES program header. The functions riscv_elf_additional_program_headers and riscv_elf_modify_segment_map in bfd/elfnn-riscv.c are modified to return early if struct bfd_link_info *info is NULL. This mirrors behavior in other ELF backends (SPU, S390, HPPA) and prevents corruption on toolchains that do not pre-allocate space for this header, specifically targeting LLD versions prior to 17.
- objcopy
- A binary utility that copies and translates object files. It can be used to strip symbols, change format, or modify sections.
- strip
- A binary utility that removes symbols and other information from object files to reduce their size.
- PT_RISCV_ATTRIBUTES
- An ELF program header type specific to RISC-V, used to store processor and architecture attributes.
- LLD
- The LLVM Linker. A linker developed by the LLVM project, often used as an alternative to GNU ld.
- ELF
- Executable and Linkable Format. A common file format for object files, executables, and shared libraries on Unix-like systems.
- program header
- A structure within an ELF file that describes how the operating system should load the executable into memory.