RISC-V assembler preserves branch relocations across sections with -mno-relax
Fixes RISC-V assembler to correctly emit branch relocations to labels in different sections when relaxation is disabled.
This commit corrects an issue in the RISC-V assembler where, with -mno-relax enabled, branch and jump fixups to labels in other sections were incorrectly resolved. This led to wrong offsets and the omission of necessary relocations. The assembler now correctly handles these cross-section branch fixups only when the symbol resides in the same section, aligning its behavior with existing PCREL_HI20 relocation handling.
In Details
This change addresses a bug in gas/config/tc-riscv.c related to how branch and jump fixups are handled when riscv_opts.relax is false. Previously, commit af514e5f6d1 caused gas to resolve fixups to local symbols even if they were in different sections. This commit adds a condition to the md_apply_fix function: the fixup is only resolved if the symbol fixP->fx_addsy is in the same segment (seg) as the current instruction. This restores correct behavior for branches and jumps targeting labels outside the current section when relaxation is disabled.
- -mno-relax
- A compiler or assembler flag that disables code relaxation. Relaxation is an optimization where fixed-size instructions can be replaced by smaller, variable-size instructions (like RVC on RISC-V) to reduce code size. Disabling it means the generated code uses fixed-size instructions, simplifying address calculations.
- fixup
- A placeholder or instruction in assembled code that needs to be resolved later by the linker. For branches and jumps, this involves calculating the correct offset to the target label.
- relocation
- Information embedded in an object file that instructs the linker how to modify addresses in the code or data sections to correctly reference symbols (functions or variables) that are defined elsewhere, potentially in different object files or shared libraries.
- PCREL_HI20
- A RISC-V relocation type used for PC-relative addressing, typically for the upper 20 bits of an immediate offset. It's often used in conjunction with another relocation (like PCREL_LO12_I) to form a full 32-bit offset for branches and loads.