llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.03k stars 11.96k forks source link

[lld][RISC-V] No Warning/Error on R_RISCV_PCREL_LO referencing symbol/relocation in different section #107304

Closed quic-seaswara closed 1 month ago

quic-seaswara commented 2 months ago

The following test shows that lld creates a image that has a LO relocation thats connected to a HI relocation in a different section.

.section .text.foo
.global foo
foo:
        addi    a0, a0, %pcrel_lo(.Lpcrel_hi0)
        lw      a0, 0(a0)
.section .text.bar
.Lpcrel_hi0:
        auipc   a0, %pcrel_hi(a)
.data
.global a
a:
.word 100
$ riscv-ld r.o
riscv-ld: warning: cannot find entry symbol _start; defaulting to 00010094
r.o: in function `foo':
(.text.foo+0x0): dangerous relocation: %pcrel_lo missing matching %pcrel_hi
$ ld.lld r.o
ld.lld: warning: cannot find entry symbol _start; not setting start address

The RISC-V ABI mentions this :-

https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc

The R_RISCV_PCREL_LO12_I or R_RISCV_PCREL_LO12_S relocations contain a label pointing to an instruction in the same section with an R_RISCV_PCREL_HI20 relocation entry that points to the target symbol:

At label: R_RISCV_PCREL_HI20 relocation entry → symbol

R_RISCV_PCREL_LO12_I relocation entry → label

lenary commented 2 months ago

GNU ld's error message isn't very obvious here, it says "missing matching %pcrel_hi", presumably because it's only looking in the same section? It's not actually saying anything about not matching the psABI.

Looking at LLD's behaviour:

llvmbot commented 2 months ago

@llvm/issue-subscribers-lld-elf

Author: None (quic-seaswara)

The following test shows that lld creates a image that has a LO relocation thats connected to a HI relocation in a different section. ``` .section .text.foo .global foo foo: addi a0, a0, %pcrel_lo(.Lpcrel_hi0) lw a0, 0(a0) .section .text.bar .Lpcrel_hi0: auipc a0, %pcrel_hi(a) .data .global a a: .word 100 ``` ``` $ riscv-ld r.o riscv-ld: warning: cannot find entry symbol _start; defaulting to 00010094 r.o: in function `foo': (.text.foo+0x0): dangerous relocation: %pcrel_lo missing matching %pcrel_hi ``` ``` $ ld.lld r.o ld.lld: warning: cannot find entry symbol _start; not setting start address ``` The RISC-V ABI mentions this :- https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc > The `R_RISCV_PCREL_LO12_I` or `R_RISCV_PCREL_LO12_S` relocations contain a label pointing to an instruction in the same section with an `R_RISCV_PCREL_HI20` relocation entry that points to the target symbol: > > At label: `R_RISCV_PCREL_HI20` relocation entry → symbol > > `R_RISCV_PCREL_LO12_I` relocation entry → label
llvmbot commented 2 months ago

@llvm/issue-subscribers-backend-risc-v

Author: None (quic-seaswara)

The following test shows that lld creates a image that has a LO relocation thats connected to a HI relocation in a different section. ``` .section .text.foo .global foo foo: addi a0, a0, %pcrel_lo(.Lpcrel_hi0) lw a0, 0(a0) .section .text.bar .Lpcrel_hi0: auipc a0, %pcrel_hi(a) .data .global a a: .word 100 ``` ``` $ riscv-ld r.o riscv-ld: warning: cannot find entry symbol _start; defaulting to 00010094 r.o: in function `foo': (.text.foo+0x0): dangerous relocation: %pcrel_lo missing matching %pcrel_hi ``` ``` $ ld.lld r.o ld.lld: warning: cannot find entry symbol _start; not setting start address ``` The RISC-V ABI mentions this :- https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-elf.adoc > The `R_RISCV_PCREL_LO12_I` or `R_RISCV_PCREL_LO12_S` relocations contain a label pointing to an instruction in the same section with an `R_RISCV_PCREL_HI20` relocation entry that points to the target symbol: > > At label: `R_RISCV_PCREL_HI20` relocation entry → symbol > > `R_RISCV_PCREL_LO12_I` relocation entry → label