Skip to content

Commit 26ee894

Browse files
committed
[JITLink][RISCV] Only generate PLT entries for external symbols
R_RISCV_CALL has been deprecated. [1] Both GCC and LLVM seem to not generate it anymore and always use R_RISCV_CALL_PLT (even for calls that do not need a PLT entry). Generating PLT entries based on relocation type is not recommended and a better heuristic is to only generate them when the target symbol is preemptable [2]. This patch implements this by only generating PLT entries for undefined symbols. [1] riscv-non-isa/riscv-elf-psabi-doc#340 [2] riscv-non-isa/riscv-elf-psabi-doc#98 Reviewed By: lhames Differential Revision: https://reviews.llvm.org/D149525
1 parent 7c5cbe9 commit 26ee894

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ class PerGraphGOTAndPLTStubsBuilder_ELF_riscv
7676
}
7777

7878
bool isExternalBranchEdge(Edge &E) const {
79-
return E.getKind() == R_RISCV_CALL_PLT;
79+
return (E.getKind() == R_RISCV_CALL || E.getKind() == R_RISCV_CALL_PLT) &&
80+
!E.getTarget().isDefined();
8081
}
8182

8283
private:

llvm/test/ExecutionEngine/JITLink/RISCV/ELF_pc_indirect.s

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# RUN: -o %t/elf_riscv32_sm_pic_reloc.o %s
66
# RUN: llvm-jitlink -noexec \
77
# RUN: -slab-allocate 100Kb -slab-address 0x1ff00000 -slab-page-size 4096 \
8-
# RUN: -abs external_func=0x1 -abs external_data=0x2 \
8+
# RUN: -abs external_data=0x2 \
99
# RUN: -check %s %t/elf_riscv64_sm_pic_reloc.o
1010
# RUN: llvm-jitlink -noexec \
1111
# RUN: -slab-allocate 100Kb -slab-address 0x1ff00000 -slab-page-size 4096 \
12-
# RUN: -abs external_func=0x1 -abs external_data=0x2 \
12+
# RUN: -abs external_data=0x2 \
1313
# RUN: -check %s %t/elf_riscv32_sm_pic_reloc.o
1414
#
1515
# Test ELF small/PIC relocations
@@ -51,14 +51,34 @@ test_pcrel32_s:
5151
.size test_pcrel32_s, .-test_pcrel32_s
5252

5353
# Test R_RISCV_CALL
54-
# jitlink-check: decode_operand(test_call, 1) = ((external_func - test_call) + 0x800)[31:12]
55-
# jitlink-check: decode_operand(test_call+4, 2)[11:0] = (external_func - test_call)[11:0]
56-
.globl test_call, external_func
54+
# jitlink-check: decode_operand(test_call, 1) = ((internal_func - test_call) + 0x800)[31:12]
55+
# jitlink-check: decode_operand(test_call+4, 2)[11:0] = (internal_func - test_call)[11:0]
56+
.globl test_call
5757
.p2align 1
5858
.type test_call,@function
5959
test_call:
60-
.reloc ., R_RISCV_CALL, external_func
60+
.reloc ., R_RISCV_CALL, internal_func
6161
auipc ra, 0
6262
jalr ra
6363
ret
6464
.size test_call, .-test_call
65+
66+
# Test R_RISCV_CALL_PLT
67+
# jitlink-check: decode_operand(test_call_plt, 1) = ((internal_func - test_call_plt) + 0x800)[31:12]
68+
# jitlink-check: decode_operand(test_call_plt+4, 2)[11:0] = (internal_func - test_call_plt)[11:0]
69+
.globl test_call_plt
70+
.p2align 1
71+
.type test_call_plt,@function
72+
test_call_plt:
73+
.reloc ., R_RISCV_CALL_PLT, internal_func
74+
auipc ra, 0
75+
jalr ra
76+
ret
77+
.size test_call_plt, .-test_call_plt
78+
79+
.globl internal_func
80+
.p2align 1
81+
.type internal_func,@function
82+
internal_func:
83+
ret
84+
.size internal_func, .-internal_func

0 commit comments

Comments
 (0)