@@ -923,20 +923,21 @@ template <class PltSection, class GotPltSection>
923923static void addPltEntry (Ctx &ctx, PltSection &plt, GotPltSection &gotPlt,
924924 RelocationBaseSection &rel, RelType type, Symbol &sym) {
925925 plt.addEntry (sym);
926- if (ctx.arg .isCheriAbi ) {
927- // TODO: More normal .got.plt rather than piggy-backing on .captable. We
928- // pass R_CHERI_CAPABILITY_TABLE_INDEX rather than the more obvious
929- // R_CHERI_CAPABILITY_TABLE_INDEX_CALL to force dynamic relocations into
930- // .rela.dyn rather than .rela.plt so no rtld changes are needed, as the
931- // latter doesn't really achieve anything without lazy binding.
932- ctx.in .cheriCapTable ->addEntry (sym, R_CHERI_CAPABILITY_TABLE_INDEX, &plt, 0 );
933- } else {
934- gotPlt.addEntry (sym);
935- rel.addReloc ({type, &gotPlt, sym.getGotPltOffset (ctx),
936- sym.isPreemptible ? DynamicReloc::AgainstSymbol
937- : DynamicReloc::AddendOnlyWithTargetVA,
938- sym, 0 , R_ABS});
939- }
926+
927+ // For CHERI-RISC-V we mark the symbol NEEDS_GOT so it will end up in .got as
928+ // a function pointer, and uses .rela.dyn rather than .rela.plt, so no rtld
929+ // changes are needed.
930+ //
931+ // TODO: More normal .got.plt with lazy-binding rather than piggy-backing on
932+ // .got once rtld supports it.
933+ if (ctx.arg .emachine == EM_RISCV && ctx.arg .isCheriAbi )
934+ return ;
935+
936+ gotPlt.addEntry (sym);
937+ rel.addReloc ({type, &gotPlt, sym.getGotPltOffset (ctx),
938+ sym.isPreemptible ? DynamicReloc::AgainstSymbol
939+ : DynamicReloc::AddendOnlyWithTargetVA,
940+ sym, 0 , R_ABS});
940941}
941942
942943void elf::addGotEntry (Ctx &ctx, Symbol &sym) {
@@ -952,8 +953,12 @@ void elf::addGotEntry(Ctx &ctx, Symbol &sym) {
952953 }
953954
954955 // Otherwise, the value is either a link-time constant or the load base
955- // plus a constant.
956- if (!ctx.arg .isPic || isAbsolute (sym))
956+ // plus a constant. For CHERI it always requires run-time initialisation.
957+ if (ctx.arg .isCheriAbi ) {
958+ invokeELFT (addCapabilityRelocation, ctx, &sym, *ctx.target ->cheriCapRel ,
959+ ctx.in .got .get (), off, R_CHERI_CAPABILITY, 0 , false ,
960+ [] { return " " ; });
961+ } else if (!ctx.arg .isPic || isAbsolute (sym))
957962 ctx.in .got ->addConstant ({R_ABS, ctx.target ->symbolicRel , off, 0 , &sym});
958963 else
959964 addRelativeReloc (ctx, *ctx.in .got , off, sym, 0 , R_ABS,
@@ -1023,25 +1028,22 @@ bool RelocationScanner::isStaticLinkTimeConstant(RelExpr e, RelType type,
10231028 const Symbol &sym,
10241029 uint64_t relOff) const {
10251030 // These expressions always compute a constant
1026- if (oneof<
1027- R_GOTPLT, R_GOT_OFF, R_RELAX_HINT, RE_MIPS_GOT_LOCAL_PAGE ,
1028- RE_MIPS_GOTREL, RE_MIPS_GOT_OFF, RE_MIPS_GOT_OFF32, RE_MIPS_GOT_GP_PC ,
1029- RE_AARCH64_GOT_PAGE_PC, RE_AARCH64_AUTH_GOT_PAGE_PC, R_GOT_PC,
1030- R_GOTONLY_PC, R_GOTPLTONLY_PC, R_PLT_PC, R_PLT_GOTREL, R_PLT_GOTPLT,
1031- R_GOTPLT_GOTREL, R_GOTPLT_PC, RE_PPC32_PLTREL, RE_PPC64_CALL_PLT,
1032- RE_PPC64_RELAX_TOC, RE_RISCV_ADD, RE_AARCH64_GOT_PAGE,
1033- RE_AARCH64_AUTH_GOT, RE_AARCH64_AUTH_GOT_PC, RE_LOONGARCH_PLT_PAGE_PC ,
1034- RE_LOONGARCH_GOT, RE_LOONGARCH_GOT_PAGE_PC ,
1035- R_CHERI_CAPABILITY_TABLE_INDEX,
1031+ if (oneof<R_GOTPLT, R_GOT_OFF, R_RELAX_HINT, RE_MIPS_GOT_LOCAL_PAGE,
1032+ RE_MIPS_GOTREL, RE_MIPS_GOT_OFF, RE_MIPS_GOT_OFF32 ,
1033+ RE_MIPS_GOT_GP_PC, RE_AARCH64_GOT_PAGE_PC ,
1034+ RE_AARCH64_AUTH_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC ,
1035+ R_GOTPLTONLY_PC, R_PLT_PC, R_PLT_GOTREL, R_PLT_GOTPLT,
1036+ R_GOTPLT_GOTREL, R_GOTPLT_PC, RE_PPC32_PLTREL, RE_PPC64_CALL_PLT,
1037+ RE_PPC64_RELAX_TOC, RE_RISCV_ADD, RE_AARCH64_GOT_PAGE,
1038+ RE_AARCH64_AUTH_GOT, RE_AARCH64_AUTH_GOT_PC,
1039+ RE_LOONGARCH_PLT_PAGE_PC, RE_LOONGARCH_GOT ,
1040+ RE_LOONGARCH_GOT_PAGE_PC, R_CHERI_CAPABILITY_TABLE_INDEX,
10361041 R_CHERI_CAPABILITY_TABLE_INDEX_SMALL_IMMEDIATE,
10371042 R_CHERI_CAPABILITY_TABLE_INDEX_CALL,
10381043 R_CHERI_CAPABILITY_TABLE_INDEX_CALL_SMALL_IMMEDIATE,
1039- R_CHERI_CAPABILITY_TABLE_ENTRY_PC,
1040- R_CHERI_CAPABILITY_TABLE_REL,
1041- R_CHERIOT_COMPARTMENT_CGPREL_HI,
1044+ R_CHERI_CAPABILITY_TABLE_REL, R_CHERIOT_COMPARTMENT_CGPREL_HI,
10421045 R_CHERIOT_COMPARTMENT_CGPREL_LO_I,
1043- R_CHERIOT_COMPARTMENT_CGPREL_LO_S,
1044- R_CHERIOT_COMPARTMENT_SIZE>(e))
1046+ R_CHERIOT_COMPARTMENT_CGPREL_LO_S, R_CHERIOT_COMPARTMENT_SIZE>(e))
10451047 return true ;
10461048
10471049 // Cheri capability relocations are never static link time constants since
@@ -1161,20 +1163,7 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
11611163 if (oneof<R_CHERI_CAPABILITY_TABLE_INDEX,
11621164 R_CHERI_CAPABILITY_TABLE_INDEX_SMALL_IMMEDIATE,
11631165 R_CHERI_CAPABILITY_TABLE_INDEX_CALL,
1164- R_CHERI_CAPABILITY_TABLE_INDEX_CALL_SMALL_IMMEDIATE,
1165- R_CHERI_CAPABILITY_TABLE_ENTRY_PC>(expr)) {
1166- std::lock_guard<std::mutex> lock (ctx.relocMutex );
1167- ctx.in .cheriCapTable ->addEntry (sym, expr, sec, offset);
1168- // Write out the index into the instruction
1169- sec->relocations .push_back ({expr, type, offset, addend, &sym});
1170- return ;
1171- }
1172-
1173- if (oneof<R_CHERI_CAPABILITY_TABLE_INDEX,
1174- R_CHERI_CAPABILITY_TABLE_INDEX_SMALL_IMMEDIATE,
1175- R_CHERI_CAPABILITY_TABLE_INDEX_CALL,
1176- R_CHERI_CAPABILITY_TABLE_INDEX_CALL_SMALL_IMMEDIATE,
1177- R_CHERI_CAPABILITY_TABLE_ENTRY_PC>(expr)) {
1166+ R_CHERI_CAPABILITY_TABLE_INDEX_CALL_SMALL_IMMEDIATE>(expr)) {
11781167 std::lock_guard<std::mutex> lock (ctx.relocMutex );
11791168 ctx.in .cheriCapTable ->addEntry (sym, expr, sec, offset);
11801169 // Write out the index into the instruction
@@ -1203,6 +1192,9 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
12031192 }
12041193 } else if (needsPlt (expr)) {
12051194 sym.setFlags (NEEDS_PLT);
1195+ // See addPltEntry
1196+ if (ctx.arg .emachine == EM_RISCV && ctx.arg .isCheriAbi )
1197+ sym.setFlags (NEEDS_GOT);
12061198 } else if (LLVM_UNLIKELY (isIfunc)) {
12071199 sym.setFlags (HAS_DIRECT_RELOC);
12081200 }
@@ -1359,6 +1351,9 @@ void RelocationScanner::processAux(RelExpr expr, RelType type, uint64_t offset,
13591351 printLocation (diag, *sec, sym, offset);
13601352 }
13611353 sym.setFlags (NEEDS_COPY | NEEDS_PLT);
1354+ // See addPltEntry
1355+ if (ctx.arg .emachine == EM_RISCV && ctx.arg .isCheriAbi )
1356+ sym.setFlags (NEEDS_GOT);
13621357 sec->addReloc ({expr, type, offset, addend, &sym});
13631358 return ;
13641359 }
@@ -1501,36 +1496,6 @@ unsigned RelocationScanner::handleTlsRelocation(RelExpr expr, RelType type,
15011496 !(isRISCV && expr != R_TLSDESC_PC && expr != R_TLSDESC_CALL) &&
15021497 !sec->file ->ppc64DisableTLSRelax ;
15031498
1504- // No targets currently support TLS relaxation, so we can avoid duplicating
1505- // much of the logic below for the captable.
1506- if (expr == R_CHERI_CAPABILITY_TABLE_TLSGD_ENTRY_PC) {
1507- std::lock_guard<std::mutex> lock (ctx.relocMutex );
1508- ctx.in .cheriCapTable ->addDynTlsEntry (sym);
1509- sec->relocations .push_back ({expr, type, offset, addend, &sym});
1510- return 1 ;
1511- }
1512- if (expr == R_CHERI_CAPABILITY_TABLE_TLSIE_ENTRY_PC) {
1513- std::lock_guard<std::mutex> lock (ctx.relocMutex );
1514- ctx.in .cheriCapTable ->addTlsEntry (sym);
1515- sec->relocations .push_back ({expr, type, offset, addend, &sym});
1516- return 1 ;
1517- }
1518-
1519- // No targets currently support TLS relaxation, so we can avoid duplicating
1520- // much of the logic below for the captable.
1521- if (expr == R_CHERI_CAPABILITY_TABLE_TLSGD_ENTRY_PC) {
1522- std::lock_guard<std::mutex> lock (ctx.relocMutex );
1523- ctx.in .cheriCapTable ->addDynTlsEntry (sym);
1524- sec->relocations .push_back ({expr, type, offset, addend, &sym});
1525- return 1 ;
1526- }
1527- if (expr == R_CHERI_CAPABILITY_TABLE_TLSIE_ENTRY_PC) {
1528- std::lock_guard<std::mutex> lock (ctx.relocMutex );
1529- ctx.in .cheriCapTable ->addTlsEntry (sym);
1530- sec->relocations .push_back ({expr, type, offset, addend, &sym});
1531- return 1 ;
1532- }
1533-
15341499 // If we are producing an executable and the symbol is non-preemptable, it
15351500 // must be defined and the code sequence can be optimized to use
15361501 // Local-Exesec->
0 commit comments