@@ -124,6 +124,19 @@ static uint32_t setLO12_S(uint32_t insn, uint32_t imm) {
124124 (extractBits (imm, 4 , 0 ) << 7 );
125125}
126126
127+ static bool isCheriotHighPartReloc (RelType ty) {
128+ switch (ty) {
129+ case R_RISCV_CHERIOT_COMPARTMENT_HI:
130+ case R_RISCV_CHERI_CAPTAB_PCREL_HI20:
131+ // FIXME: This can be enabled once they handle cheriot 11-bit AUIPC shifts.
132+ // case R_RISCV_CHERI_TLS_IE_CAPTAB_PCREL_HI20:
133+ // case R_RISCV_CHERI_TLS_GD_CAPTAB_PCREL_HI20:
134+ return true ;
135+ default :
136+ return false ;
137+ }
138+ }
139+
127140RISCV::RISCV (Ctx &ctx) : TargetInfo(ctx) {
128141 copyRel = R_RISCV_COPY;
129142 pltRel = R_RISCV_JUMP_SLOT;
@@ -540,6 +553,13 @@ void RISCV::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
540553 case R_RISCV_TLS_GOT_HI20:
541554 case R_RISCV_TPREL_HI20:
542555 case R_RISCV_HI20: {
556+ if (ctx.arg .isCheriot && isCheriotHighPartReloc (rel.type )) {
557+ relocate (loc,
558+ Relocation{rel.expr , R_RISCV_CHERIOT_COMPARTMENT_HI, rel.offset ,
559+ rel.addend , rel.sym },
560+ val);
561+ return ;
562+ }
543563 uint64_t hi = val + 0x800 ;
544564 checkInt (ctx, loc, SignExtend64 (hi, bits) >> 12 , 20 , rel);
545565 write32le (loc, (read32le (loc) & 0xFFF ) | (hi & 0xFFFFF000 ));
@@ -1079,8 +1099,7 @@ static bool rewriteCheriotLowRelocs(Ctx &ctx, InputSection &sec) {
10791099 if (isPCCRelative (ctx, nullptr , r.sym )) {
10801100 const Defined *d = cast<Defined>(r.sym );
10811101 if (!d->section )
1082- error (" R_RISCV_CHERIOT_COMPARTMENT_LO_I relocation points to an "
1083- " absolute symbol: " +
1102+ error (" high-part relocation points to an absolute symbol: " +
10841103 r.sym ->getName ());
10851104 InputSection *isec = cast<InputSection>(d->section );
10861105
@@ -1096,14 +1115,14 @@ static bool rewriteCheriotLowRelocs(Ctx &ctx, InputSection &sec) {
10961115
10971116 const Relocation *target = nullptr ;
10981117 for (auto it = range.first ; it != range.second ; ++it)
1099- if (it->type == R_RISCV_CHERIOT_COMPARTMENT_HI ) {
1118+ if (isCheriotHighPartReloc ( it->type ) ) {
11001119 target = &*it;
11011120 break ;
11021121 }
11031122 if (!target) {
1104- error (
1105- " Could not find R_RISCV_CHERIOT_COMPARTMENT_HI relocation for " +
1106- toStr (ctx, *r. sym ));
1123+ error (" Could not find high-part relocation for " +
1124+ toStr (ctx, *r. sym ) + " at 0x " +
1125+ llvm::utohexstr (r. offset , false , 8 ) + " @ " + toStr (ctx, &sec ));
11071126 }
11081127 // If the target is PCC-relative then the auipcc can't be erased and so
11091128 // skip the rewriting.
0 commit comments