[PATCH 2/2] RISC-V: Don't generate R_RISCV_RELATIVE if symbol referenced section is SH_ABS
Fangrui Song
i@maskray.me
Fri Aug 22 05:59:21 GMT 2025
On Thu, Aug 21, 2025 at 10:51 PM Nelson Chu <nelson@rivosinc.com> wrote:
>
> Thanks H.J, the information is really helpful ;)
>
> cc risc-v kernel experts to see if risc-v have those section symbols defined in linker script. Please feel free to cc anyone who I've missed but should be notified.
>
> Thanks
> Nelson
I don't think we need to do anything different.
A relocation of formula (S + A) referencing a non-preemptible SHN_ABS
symbol should be resolved statically , without generating a dynamic
relocation.
All ports of lld/ELF use this logic and Linux kernels linked with lld
are working well:
https://github.com/llvm/llvm-project/commit/ba2de8f22d0cac86d89c1806fb54ed3463349342
> On Fri, Aug 22, 2025 at 11:00 AM H.J. Lu <hjl.tools@gmail.com> wrote:
>>
>> On Wed, Aug 20, 2025 at 9:33 PM Nelson Chu <nelson@rivosinc.com> wrote:
>> >
>> > It's werid if it's absolute symbol but have a relative reloc for got entry
>> > under pie. So don't generate relative reloc if symbol referenced section is
>> > SH_ABS. However, x86 allows the absolute symbol defined in linker script has
>> > a relative reloc, not sure if risc-v needs this or not.
>>
>> If I remember correctly, this is expected by x86-64 Linux kernel which
>> defines many symbols for different sections in linker script and expects their
>> values will be section addresses at run-time.
>>
>> > ---
>> > bfd/elfnn-riscv.c | 3 ++-
>> > ld/testsuite/ld-riscv-elf/absolute-no-relative.d | 9 +++++++++
>> > ld/testsuite/ld-riscv-elf/absolute-no-relative.ld | 6 ++++++
>> > ld/testsuite/ld-riscv-elf/absolute-no-relative.s | 8 ++++++++
>> > ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp | 1 +
>> > 5 files changed, 26 insertions(+), 1 deletion(-)
>> > create mode 100644 ld/testsuite/ld-riscv-elf/absolute-no-relative.d
>> > create mode 100644 ld/testsuite/ld-riscv-elf/absolute-no-relative.ld
>> > create mode 100644 ld/testsuite/ld-riscv-elf/absolute-no-relative.s
>> >
>> > diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
>> > index 6ff64325f85..aac061fb43d 100644
>> > --- a/bfd/elfnn-riscv.c
>> > +++ b/bfd/elfnn-riscv.c
>> > @@ -2880,7 +2880,8 @@ riscv_elf_relocate_section (bfd *output_bfd,
>> > if (h->dynindx == -1
>> > && !h->forced_local
>> > && h->root.type != bfd_link_hash_undefweak
>> > - && bfd_link_pic (info))
>> > + && bfd_link_pic (info)
>> > + && !bfd_is_abs_section(h->root.u.def.section))
>> > relative_got = true;
>> >
>> > bfd_put_NN (output_bfd, relocation,
>> > diff --git a/ld/testsuite/ld-riscv-elf/absolute-no-relative.d b/ld/testsuite/ld-riscv-elf/absolute-no-relative.d
>> > new file mode 100644
>> > index 00000000000..6699ac1d0ef
>> > --- /dev/null
>> > +++ b/ld/testsuite/ld-riscv-elf/absolute-no-relative.d
>> > @@ -0,0 +1,9 @@
>> > +#source: absolute-no-relative.s
>> > +#as:
>> > +#ld: -Tabsolute-no-relative.ld -pie
>> > +#readelf: -Wr
>> > +
>> > +Relocation section '.rela.dyn' at .*
>> > +[ ]+Offset[ ]+Info[ ]+Type[ ]+.*
>> > +0+[ ]+0+[ ]+R_RISCV_NONE[ ]+0
>> > +0+[ ]+0+[ ]+R_RISCV_NONE[ ]+0
>> > diff --git a/ld/testsuite/ld-riscv-elf/absolute-no-relative.ld b/ld/testsuite/ld-riscv-elf/absolute-no-relative.ld
>> > new file mode 100644
>> > index 00000000000..34bbcba7e73
>> > --- /dev/null
>> > +++ b/ld/testsuite/ld-riscv-elf/absolute-no-relative.ld
>> > @@ -0,0 +1,6 @@
>> > +ENTRY(_start)
>> > +SECTIONS {
>> > + __symbol_abs__ = 0x1234;
>> > + .text 0x10000 : { *(.text*)}
>> > + .got 0x20000 : { *(.got*) }
>> > +}
>> > diff --git a/ld/testsuite/ld-riscv-elf/absolute-no-relative.s b/ld/testsuite/ld-riscv-elf/absolute-no-relative.s
>> > new file mode 100644
>> > index 00000000000..4515661cfd5
>> > --- /dev/null
>> > +++ b/ld/testsuite/ld-riscv-elf/absolute-no-relative.s
>> > @@ -0,0 +1,8 @@
>> > +.text
>> > +.global _start
>> > +_start:
>> > +lga x1, symbol_abs
>> > +lga x1, __symbol_abs__
>> > +
>> > +.global symbol_abs
>> > +.set symbol_abs, 0x100
>> > diff --git a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
>> > index bae1105cad6..b0d510ac8da 100644
>> > --- a/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
>> > +++ b/ld/testsuite/ld-riscv-elf/ld-riscv-elf.exp
>> > @@ -338,4 +338,5 @@ if [istarget "riscv*-*-*"] {
>> > }
>> > run_dump_test "tls"
>> > run_dump_test "tlsbin"
>> > + run_dump_test "absolute-no-relative"
>> > }
>> > --
>> > 2.39.5 (Apple Git-154)
>> >
>>
>>
>> --
>> H.J.
More information about the Binutils
mailing list