Skip to content

Commit 8ea7c18

Browse files
committed
[CHERIoT] Un-break R_RISCV_CHERIOT_COMPARTMENT_LO_S code generation.
What started as a simple plan to add a test for R_RISCV_CHERIOT_COMPARTMENT_LO_S linking uncovered the fact that we've been silently generating R_RISCV_CHERIOT_COMPARTMENT_SIZE in place of R_RISCV_CHERIOT_COMPARTMENT_LO_S since 08a3929, and somehow nobody ever noticed. This change corrects that, fixes the relatively minor downstream issues exposed in LLD by doing so, and adds a basic sanity test so that we don't break it again.
1 parent 0545e43 commit 8ea7c18

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

lld/ELF/Arch/RISCV.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,16 +1088,17 @@ static bool rewriteCheriotLowRelocs(Ctx &ctx, InputSection &sec) {
10881088
modified = true;
10891089
r.type = INTERNAL_R_RISCV_CHERIOT_COMPARTMENT_PCCREL_HI;
10901090
r.expr = R_PC;
1091-
} else if (r.type == R_RISCV_CHERIOT_COMPARTMENT_LO_I) {
1091+
} else if (r.type == R_RISCV_CHERIOT_COMPARTMENT_LO_I ||
1092+
r.type == R_RISCV_CHERIOT_COMPARTMENT_LO_S) {
10921093
// If this is PCC-relative, then the relocation points to the auicgp /
10931094
// auipcc instruction and we need to look there to find the real target.
10941095
if (!isPCCRelative(ctx, nullptr, r.sym))
1095-
fatal("R_RISCV_CHERIOT_COMPARTMENT_LO_I must point to "
1096+
fatal("R_RISCV_CHERIOT_COMPARTMENT_LO_[I/S] must point to "
10961097
"R_RISCV_COMPARTMENT_HI");
10971098

10981099
const Defined *d = cast<Defined>(r.sym);
10991100
if (!d->section)
1100-
error("R_RISCV_CHERIOT_COMPARTMENT_LO_I relocation points to an "
1101+
error("R_RISCV_CHERIOT_COMPARTMENT_LO_[I/S] relocation points to an "
11011102
"absolute symbol: " +
11021103
r.sym->getName());
11031104
InputSection *isec = cast<InputSection>(d->section);
@@ -1127,6 +1128,8 @@ static bool rewriteCheriotLowRelocs(Ctx &ctx, InputSection &sec) {
11271128
// If the target is PCC-relative then the auipcc can't be erased and so
11281129
// skip the rewriting.
11291130
if (isPCCRelative(ctx, nullptr, target->sym)) {
1131+
assert(r.type != R_RISCV_CHERIOT_COMPARTMENT_LO_S &&
1132+
"Malformed R_RISCV_CHERIOT_COMPARTMENT_LO_S relocation!");
11301133
r.type = INTERNAL_R_RISCV_CHERIOT_COMPARTMENT_PCCREL_LO_I;
11311134
r.expr = RE_RISCV_PC_INDIRECT;
11321135
continue;

lld/ELF/InputSection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,8 +1048,8 @@ uint64_t InputSectionBase::getRelocTargetVA(Ctx &ctx, const Relocation &r,
10481048
// Reached only for CGP-relative relocations. PCC-relative addresses are
10491049
// calculated with the R_PC and R_PC_INDIRECT cases.
10501050
case RE_CHERIOT_COMPARTMENT_CGPREL_LO:
1051-
if (isPCCRelative(ctx, nullptr, r.sym))
1052-
fatal("Malformed RE_CHERIOT_COMPARTMENT_CGPREL_LO_I relocation!");
1051+
assert(!isPCCRelative(ctx, nullptr, r.sym) &&
1052+
"Malformed RE_CHERIOT_COMPARTMENT_CGPREL_LO relocation!");
10531053
return getBiasedCGPOffsetLo12(ctx, *r.sym);
10541054
case RE_CHERIOT_COMPARTMENT_CGPREL_HI:
10551055
return (getBiasedCGPOffset(ctx, *r.sym) -
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# REQUIRES: riscv
2+
# RUN: llvm-mc -triple=riscv32cheriot-unknown-cheriotrtos -mcpu=cheriot -mattr=+c,+xcheri,+xcheripurecap,+xcheriot -filetype=obj %s -o %t.o
3+
# RUN: ld.lld %t.o -o %t.exe
4+
# RUN: llvm-objdump -d %t.exe | FileCheck %s
5+
6+
.attribute 4, 16
7+
.attribute 5, "rv32e2p0_m2p0_c2p0_zmmul1p0_xcheri0p0_xcheriot1p0_xcheripurecap0p0"
8+
.section .text,"ax",@progbits
9+
.globl _start
10+
.p2align 1
11+
.type _start,@function
12+
.CGP_BLOCK:
13+
ct.auipcc t1, %cheriot_compartment_hi(cgp_label)
14+
ct.csw ra, %cheriot_compartment_lo_s(.CGP_BLOCK)(t1)
15+
.CGP_FAR_BLOCK:
16+
ct.auipcc t1, %cheriot_compartment_hi(cgp_far_label)
17+
ct.csw ra, %cheriot_compartment_lo_s(.CGP_FAR_BLOCK)(t1)
18+
19+
# CHECK: 000110f4 <.CGP_BLOCK>:
20+
# CHECK-NEXT: 110f4: ffffe37b ct.auicgp t1, 0xffffe
21+
# CHECK-NEXT: 110f8: fe132e23 ct.csw ra, -0x4(t1)
22+
23+
# CHECK: 000110fc <.CGP_FAR_BLOCK>:
24+
# CHECK-NEXT: 110fc: 0000237b ct.auicgp t1, 0x2
25+
# CHECK-NEXT: 11100: 00132023 ct.csw ra, 0x0(t1)
26+
27+
.section .data,"aw",@progbits
28+
.type cgp_label,@object
29+
.globl cgp_label
30+
.p2align 4, 0x0
31+
cgp_label:
32+
.word 2
33+
.zero 8192
34+
cgp_far_label:
35+
.word 3

llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFObjectWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ unsigned RISCVELFObjectWriter::getRelocType(const MCFixup &Fixup,
174174
case RISCV::fixup_riscv_cheriot_compartment_lo_i:
175175
return ELF::R_RISCV_CHERIOT_COMPARTMENT_LO_I;
176176
case RISCV::fixup_riscv_cheriot_compartment_lo_s:
177+
return ELF::R_RISCV_CHERIOT_COMPARTMENT_LO_S;
177178
case RISCV::fixup_riscv_cheriot_compartment_size:
178179
return ELF::R_RISCV_CHERIOT_COMPARTMENT_SIZE;
179180
}

0 commit comments

Comments
 (0)