]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commit
RISC-V: check offsets when linker relaxation is disabled
authorTsukasa OI <research_trasio@irq.a4lg.com>
Tue, 13 May 2025 08:14:01 +0000 (08:14 +0000)
committerNelson Chu <nelson@rivosinc.com>
Fri, 16 May 2025 01:47:27 +0000 (09:47 +0800)
commit27f754ee503500d4986bfec4e4daf6fda014e159
tree2894b096da64aac288079582d1fda96fef828a75
parentbbac5532cc14433df45b08d89c2d25cee3e9d218
RISC-V: check offsets when linker relaxation is disabled

The assembler partially relied on the linker to check whether the
offset is valid.  However, some optimization logic (added later)
removes relocations relative to local symbols without checking offsets.

For instance, it caused following code to silently emit wrong jumps
(to the jump instruction "." itself) without relocations:

> .option norelax
> j .+0x200000   # J (or JAL) instruction cannot encode this offset.
> j .+1          # Jump to odd address is not valid.

This commit adds offset checks where necessary.

gas/ChangeLog:

* config/tc-riscv.c (md_apply_fix): Check offsets when the
relocation relative to a local symbol is being optimized out.
* testsuite/gas/riscv/no-relax-branch-offset-fail.s: Failure
case where the branch offset is invalid.
* testsuite/gas/riscv/no-relax-branch-offset-fail.d: Ditto.
* testsuite/gas/riscv/no-relax-branch-offset-fail.l: Ditto.
* testsuite/gas/riscv/no-relax-branch-offset-ok.s: Border case.
* testsuite/gas/riscv/no-relax-branch-offset-ok.d: Ditto.
* testsuite/gas/riscv/no-relax-pcrel-offset-fail-64.s: Failure
case only on RV64 where the PC-relative offset exceed limits.
* testsuite/gas/riscv/no-relax-pcrel-offset-fail-64.d: Ditto.
* testsuite/gas/riscv/no-relax-pcrel-offset-fail-64.l: Ditto.
* testsuite/gas/riscv/no-relax-pcrel-offset-fail-not-32.d: Test
case for RV32 so that no errors occur.
* testsuite/gas/riscv/no-relax-pcrel-offset-ok.s: Border case.
* testsuite/gas/riscv/no-relax-pcrel-offset-ok.d: Ditto.
12 files changed:
gas/config/tc-riscv.c
gas/testsuite/gas/riscv/no-relax-branch-offset-fail.d [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-branch-offset-fail.l [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-branch-offset-fail.s [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-branch-offset-ok.d [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-branch-offset-ok.s [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-pcrel-offset-fail-64.d [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-pcrel-offset-fail-64.l [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-pcrel-offset-fail-64.s [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-pcrel-offset-fail-not-32.d [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-pcrel-offset-ok.d [new file with mode: 0644]
gas/testsuite/gas/riscv/no-relax-pcrel-offset-ok.s [new file with mode: 0644]