Issue 244738
Summary [fuzzer-found] [lld] [RISCV] Calls to absolute addresses incorrectly resolve to PC + absolute address
Labels lld
Assignees
Reporter mu-mu-mu
    On RISC-V, ld.lld incorrectly resolves `R_RISCV_CALL_PLT`/`R_RISCV_JAL`
relocations with symbol index 0 to the current PC plus the absolute target
address, rather than the absolute target address itself. A call to an absolute
symbol silently goes to the wrong address.

### Reproducer

```asm
.set rom_func, 0x1000
call rom_func
call rom_func
```

```console
$ clang --target=riscv64-linux-gnu -c lld-riscv-call-abs-symbol.s
$ ld.lld -Ttext=0x20000 lld-riscv-call-abs-symbol.o
```

### Actual behavior

Trunk (c0005dfe81fe) with assertions:

```console
$ clang --version
clang version 24.0.0git
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/llvm/bin
Build config: +assertions
$ ld.lld --version
LLD 24.0.0 (compatible with GNU linkers)
$ clang --target=riscv64-linux-gnu -c lld-riscv-call-abs-symbol.s
$ llvm-objdump -dr lld-riscv-call-abs-symbol.o
       0: 00000097     	auipc	ra, 0x0
		0000000000000000: R_RISCV_CALL_PLT	*ABS*+0x1000
		0000000000000000:  R_RISCV_RELAX	*ABS*
 4: 000080e7     	jalr	ra <.text>
       8: 00000097     	auipc	ra, 0x0
		0000000000000008: R_RISCV_CALL_PLT	*ABS*+0x1000
		0000000000000008:  R_RISCV_RELAX	*ABS*
 c: 000080e7     	jalr	ra <.text+0x8>
$ ld.lld -Ttext=0x20000 lld-riscv-call-abs-symbol.o
ld.lld: warning: cannot find entry symbol _start; not setting start address
$ llvm-objdump -d a.out
   20000: 000010ef     	jal	0x21000 <.text+0x1000>
   20004: 000010ef 	jal	0x21004 <.text+0x1004>
```

The two calls to the same absolute address go to different addresses (0x21000 and 0x21004), i.e. PC + 0x1000.

### Expected behavior

Both calls target 0x1000.
GNU ld (2.47.50.20261116) links the same object correctly:
```console
$ riscv64-linux-gnu-ld -Ttext=0x20000 lld-riscv-call-abs-symbol.o
$ llvm-objdump -d a.out
   20000: 800e10ef     	jal	0x1000 <rom_func>
 20004: ffde00ef     	jal	0x1000 <rom_func>
```

Found by fuzzing/automated testing: https://discourse.llvm.org/t/assembler-and-inline-assembly-bugs-found-by-fuzzing-and-automated-testing/92917

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to