Skip to content

Commit f7d4caf

Browse files
committed
[JITLink][RISCV] Support R_RISCV_SET* and R_RISCV_32_PCREL relocations
This patch supports R_RISCV_SET* and R_RISCV_32_PCREL relocations in JITLink. Reviewed By: StephenFan Differential Revision: https://reviews.llvm.org/D117082
1 parent fdb6578 commit f7d4caf

File tree

5 files changed

+134
-1
lines changed

5 files changed

+134
-1
lines changed

llvm/include/llvm/ExecutionEngine/JITLink/riscv.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,37 @@ enum EdgeKind_riscv : Edge::Kind {
137137
///
138138
/// Fixup expression
139139
/// Fixup <- (Target - *{1}Fixup - Addend)
140-
R_RISCV_SUB8
140+
R_RISCV_SUB8,
141+
142+
/// Local label assignment
143+
///
144+
/// Fixup expression:
145+
/// Fixup <- (Target + Addend)
146+
R_RISCV_SET6,
147+
148+
/// Local label assignment
149+
///
150+
/// Fixup expression:
151+
/// Fixup <- (Target + Addend)
152+
R_RISCV_SET8,
153+
154+
/// Local label assignment
155+
///
156+
/// Fixup expression:
157+
/// Fixup <- (Target + Addend)
158+
R_RISCV_SET16,
159+
160+
/// Local label assignment
161+
///
162+
/// Fixup expression:
163+
/// Fixup <- (Target + Addend)
164+
R_RISCV_SET32,
165+
166+
/// Local label assignment
167+
///
168+
/// Fixup expression:
169+
/// Fixup <- (Target - Fixup + Addend)
170+
R_RISCV_32_PCREL,
141171
};
142172

143173
/// Returns a string name for the given riscv edge. For debugging purposes

llvm/lib/ExecutionEngine/JITLink/ELF_riscv.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,39 @@ class ELFJITLinker_riscv : public JITLinker<ELFJITLinker_riscv> {
359359
*FixupPtr = static_cast<uint8_t>(Value);
360360
break;
361361
}
362+
case R_RISCV_SET6: {
363+
int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
364+
uint32_t RawData = *(little32_t *)FixupPtr;
365+
int64_t Word6 = Value & 0x3f;
366+
*(little32_t *)FixupPtr = (RawData & 0xffffffc0) | Word6;
367+
break;
368+
}
369+
case R_RISCV_SET8: {
370+
int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
371+
uint32_t RawData = *(little32_t *)FixupPtr;
372+
int64_t Word8 = Value & 0xff;
373+
*(little32_t *)FixupPtr = (RawData & 0xffffff00) | Word8;
374+
break;
375+
}
376+
case R_RISCV_SET16: {
377+
int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
378+
uint32_t RawData = *(little32_t *)FixupPtr;
379+
int64_t Word16 = Value & 0xffff;
380+
*(little32_t *)FixupPtr = (RawData & 0xffff0000) | Word16;
381+
break;
382+
}
383+
case R_RISCV_SET32: {
384+
int64_t Value = (E.getTarget().getAddress() + E.getAddend()).getValue();
385+
int64_t Word32 = Value & 0xffffffff;
386+
*(little32_t *)FixupPtr = Word32;
387+
break;
388+
}
389+
case R_RISCV_32_PCREL: {
390+
int64_t Value = E.getTarget().getAddress() + E.getAddend() - FixupAddress;
391+
int64_t Word32 = Value & 0xffffffff;
392+
*(little32_t *)FixupPtr = Word32;
393+
break;
394+
}
362395
}
363396
return Error::success();
364397
}
@@ -409,6 +442,16 @@ class ELFLinkGraphBuilder_riscv : public ELFLinkGraphBuilder<ELFT> {
409442
return EdgeKind_riscv::R_RISCV_SUB16;
410443
case ELF::R_RISCV_SUB8:
411444
return EdgeKind_riscv::R_RISCV_SUB8;
445+
case ELF::R_RISCV_SET6:
446+
return EdgeKind_riscv::R_RISCV_SET6;
447+
case ELF::R_RISCV_SET8:
448+
return EdgeKind_riscv::R_RISCV_SET8;
449+
case ELF::R_RISCV_SET16:
450+
return EdgeKind_riscv::R_RISCV_SET16;
451+
case ELF::R_RISCV_SET32:
452+
return EdgeKind_riscv::R_RISCV_SET32;
453+
case ELF::R_RISCV_32_PCREL:
454+
return EdgeKind_riscv::R_RISCV_32_PCREL;
412455
}
413456

414457
return make_error<JITLinkError>("Unsupported riscv relocation:" +

llvm/lib/ExecutionEngine/JITLink/riscv.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ const char *getEdgeKindName(Edge::Kind K) {
5454
return "R_RISCV_SUB16";
5555
case R_RISCV_SUB8:
5656
return "R_RISCV_SUB8";
57+
case R_RISCV_SET6:
58+
return "R_RISCV_SET6";
59+
case R_RISCV_SET8:
60+
return "R_RISCV_SET8";
61+
case R_RISCV_SET16:
62+
return "R_RISCV_SET16";
63+
case R_RISCV_SET32:
64+
return "R_RISCV_SET32";
65+
case R_RISCV_32_PCREL:
66+
return "R_RISCV_32_PCREL";
5767
}
5868
return getGenericEdgeKindName(K);
5969
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# RUN: rm -rf %t && mkdir -p %t
2+
# RUN: llvm-mc -triple=riscv64 -filetype=obj -o %t/riscv64_pc_relative.o %s
3+
# RUN: llvm-mc -triple=riscv32 -filetype=obj -o %t/riscv32_pc_relative.o %s
4+
# RUN: llvm-jitlink -noexec -check %s %t/riscv64_pc_relative.o
5+
# RUN: llvm-jitlink -noexec -check %s %t/riscv32_pc_relative.o
6+
7+
# jitlink-check: *{4}(foo) = 0x4
8+
9+
.global main
10+
main:
11+
lw a0, foo
12+
13+
.section ".text","",@progbits
14+
.type foo,@function
15+
foo:
16+
nop
17+
nop
18+
.reloc foo, R_RISCV_32_PCREL, foo+4
19+
.size foo, 8
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# RUN: rm -rf %t && mkdir -p %t
2+
# RUN: llvm-mc -triple=riscv64 -filetype=obj -o %t/riscv64_reloc_set.o %s
3+
# RUN: llvm-mc -triple=riscv32 -filetype=obj -o %t/riscv32_reloc_set.o %s
4+
# RUN: llvm-jitlink -noexec \
5+
# RUN: -slab-allocate 100Kb -slab-address 0xfff0f0f0 -slab-page-size 4096 \
6+
# RUN: -check %s %t/riscv64_reloc_set.o
7+
# RUN: llvm-jitlink -noexec \
8+
# RUN: -slab-allocate 100Kb -slab-address 0xfff0f0f0 -slab-page-size 4096 \
9+
# RUN: -check %s %t/riscv32_reloc_set.o
10+
11+
# jitlink-check: *{4}(foo) = foo
12+
# jitlink-check: *{2}(foo+4) = foo[15:0]
13+
# jitlink-check: *{1}(foo+6) = foo[7:0]
14+
# jitlink-check: *{1}(foo+7) = foo[5:0]
15+
16+
.global main
17+
main:
18+
lw a0, foo
19+
20+
.section ".rodata","",@progbits
21+
.type foo,@object
22+
foo:
23+
.reloc foo, R_RISCV_SET32, foo
24+
.reloc foo+4, R_RISCV_SET16, foo
25+
.reloc foo+6, R_RISCV_SET8, foo
26+
.reloc foo+7, R_RISCV_SET6, foo
27+
.word 0
28+
.half 0
29+
.byte 0
30+
.byte 0
31+
.size foo, 8

0 commit comments

Comments
 (0)