Design of R_RISCV_PCREL_LO12

120 views
Skip to first unread message

PkmX

unread,
Nov 20, 2017, 1:56:55 AM11/20/17
to sw-...@groups.riscv.org, rui...@gmail.com
Hi all,

While reviewing the patches to upstream lld, it is brought up
(https://reviews.llvm.org/D39322?id=120794#inline-344646) that there
can be an alternative design of R_RISCV_PCREL_LO12 where the distance
to the corresponding PCREL_HI20 can be stored in the addend (as
opposed to using labels to link both together).

Given:

L1:
auipc a0, %pcrel_hi(foo)
lw a1, %pcrel_lo(L1)(a0)

Currently, the assembler produces two relocations (as described at
https://github.com/riscv/riscv-elf-psabi-doc/blob/master/riscv-elf.md#pc-relative-symbol-addresses):

Offset Type Symbol + Addend
0 R_RISCV_PCREL_HI20 foo + 0
4 R_RISCV_PCREL_LO12_I L1 + 0

To perform the R_RISCV_PCREL_LO12_I relocation, the linker has to
first find the label "L1", search the relocation entries for the
R_RISCV_PCREL_HI20 at "L1", and then search for the symbol "foo" to
obtain the address.

With the alternative design, the assembler would produce:

Offset Type Symbol + Addend
0 R_RISCV_PCREL_HI20 foo + 0
4 R_RISCV_PCREL_LO12_I foo + 4

The assembler knows the offset between HI20/LO12 pair can fill it into
the addend of LO12. Now the linker only needs to find the symbol "foo"
and the address calculation can modified as:

P = address of LO12
S = address of symbol
P' = P - A # address of auipc
hi20 = (S - P' + 0x800) >> 12
lo12 = S - P' - hi20 << 12

This allows the LO12 to be computed independently, and additionally it
also eliminates one search in the symbol table and another in the
relocation table. This does comes complicate linker relaxation, as the
linker now also has to update the addend of LO12 after adjusting
symbol addresses.

I understand it may be too late to change the ABI so the new design
would have to use a new relocation number. Any thoughts?

--
Chih-Mao Chen (PkmX)
Software R&D, Andes Technology

Andrew Waterman

unread,
Nov 20, 2017, 2:30:27 AM11/20/17
to PkmX, rui...@gmail.com, sw-...@groups.riscv.org
It’s too disruptive to change the ABI at this point.

Also, the current design works with linker relaxations automatically. The assembler doesn’t actually know the offsets, since code may be deleted at link time. It is possible to make the proposed design work with relaxations, but you end up building similar data structures as for the current design. So it might not be simpler in the end.

--
You received this message because you are subscribed to the Google Groups "RISC-V SW Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sw-dev+un...@groups.riscv.org.
To post to this group, send email to sw-...@groups.riscv.org.
Visit this group at https://groups.google.com/a/groups.riscv.org/group/sw-dev/.
To view this discussion on the web visit https://groups.google.com/a/groups.riscv.org/d/msgid/sw-dev/CACSh3m3pwKZrqhfZ_LWwkHOLVUx%3Dadamrf-yiMCH468a5%3DrtGw%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages