@@ -766,15 +766,18 @@ uint64_t MipsCheriCapTableSection::assignIndices(uint64_t startIndex,
766766 RelocationBaseSection *dynRelSec = it.second .usedInCallExpr
767767 ? ctx.in .relaPlt .get ()
768768 : ctx.mainPart ->relaDyn .get ();
769- addCapabilityRelocation<ELFT>(
770- ctx, targetSym, elfCapabilityReloc, this , off, R_CHERI_CAPABILITY, 0 ,
771- it.second .usedInCallExpr ,
772- [&]() {
773- return (" \n >>> referenced by " + refName + " \n >>> first used in " +
774- it.second .firstUse ->verboseToString (ctx))
775- .str ();
776- },
777- dynRelSec);
769+ if (!targetSym->isPreemptible && targetSym->isUndefWeak ())
770+ addNullDerivedCapability (ctx, *targetSym, *this , off, 0 );
771+ else
772+ addCapabilityRelocation<ELFT>(
773+ ctx, targetSym, elfCapabilityReloc, this , off, R_CHERI_CAPABILITY, 0 ,
774+ it.second .usedInCallExpr ,
775+ [&]() {
776+ return (" \n >>> referenced by " + refName + " \n >>> first used in " +
777+ it.second .firstUse ->verboseToString (ctx))
778+ .str ();
779+ },
780+ dynRelSec);
778781 }
779782 assert (assignedSmallIndexes + assignedLargeIndexes == entries.size ());
780783 return assignedSmallIndexes + assignedLargeIndexes;
@@ -1030,26 +1033,10 @@ void addCapabilityRelocation(
10301033 }
10311034 }
10321035
1033- // Non-preemptible undef weak symbols are link-time constants
1034- if (sym && !sym->isPreemptible && sym->isUndefWeak ()) {
1035- if (ctx.arg .isLE ) {
1036- sec->addReloc ({R_ABS, ctx.target ->symbolicRel , offset, addend, sym});
1037- sec->addReloc ({R_ADDEND, ctx.target ->symbolicRel ,
1038- offset + ctx.arg .wordsize , 0 , sym});
1039- } else {
1040- sec->addReloc ({R_ADDEND, ctx.target ->symbolicRel , offset, 0 , sym});
1041- sec->addReloc ({R_ABS, ctx.target ->symbolicRel , offset + ctx.arg .wordsize ,
1042- addend, sym});
1043- }
1044- // Handle deprecated CHERI-256
1045- if (ctx.target ->getCapabilitySize () == ctx.arg .wordsize * 4 ) {
1046- sec->addReloc ({R_ADDEND, ctx.target ->symbolicRel ,
1047- offset + 2 * ctx.arg .wordsize , 0 , sym});
1048- sec->addReloc ({R_ADDEND, ctx.target ->symbolicRel ,
1049- offset + 3 * ctx.arg .wordsize , 0 , sym});
1050- }
1051- return ;
1052- }
1036+ // Non-preemptible undef weak symbols are link-time constants and should use
1037+ // addNullDerivedCapability
1038+ if (sym)
1039+ assert (sym->isPreemptible || !sym->isUndefWeak ());
10531040
10541041 // local cap relocs don't need a Elf relocation with a full symbol lookup:
10551042 if (capRelocMode == CapRelocsMode::ElfReloc) {
@@ -1121,6 +1108,28 @@ void addCapabilityRelocation(
11211108 }
11221109}
11231110
1111+ void addNullDerivedCapability (Ctx &ctx, Symbol &sym, InputSectionBase &sec,
1112+ uint64_t offset, int64_t addend) {
1113+ // Only non-preemptible undef weak symbols are link-time constants
1114+ assert (!sym.isPreemptible && sym.isUndefWeak ());
1115+ if (ctx.arg .isLE ) {
1116+ sec.addReloc ({R_ABS, ctx.target ->symbolicRel , offset, addend, &sym});
1117+ sec.addReloc ({R_ADDEND, ctx.target ->symbolicRel , offset + ctx.arg .wordsize ,
1118+ 0 , &sym});
1119+ } else {
1120+ sec.addReloc ({R_ADDEND, ctx.target ->symbolicRel , offset, 0 , &sym});
1121+ sec.addReloc ({R_ABS, ctx.target ->symbolicRel , offset + ctx.arg .wordsize ,
1122+ addend, &sym});
1123+ }
1124+ // Handle deprecated CHERI-256
1125+ if (ctx.target ->getCapabilitySize () == ctx.arg .wordsize * 4 ) {
1126+ sec.addReloc ({R_ADDEND, ctx.target ->symbolicRel ,
1127+ offset + 2 * ctx.arg .wordsize , 0 , &sym});
1128+ sec.addReloc ({R_ADDEND, ctx.target ->symbolicRel ,
1129+ offset + 3 * ctx.arg .wordsize , 0 , &sym});
1130+ }
1131+ }
1132+
11241133} // namespace elf
11251134} // namespace lld
11261135
0 commit comments