Skip to content

Commit c76f623

Browse files
jrtc27resistor
authored andcommitted
[NFC][Target][AsmPrinter] Introduce and use lowerCheriCodeReference
This will allow targets to create a target-specific MCExpr for referring to code, bypassing any use of trampolines in the presence of a c18n runtime.
1 parent b6560ef commit c76f623

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

llvm/include/llvm/Target/TargetLoweringObjectFile.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ class LLVM_ABI TargetLoweringObjectFile : public MCObjectFileInfo {
215215
return nullptr;
216216
}
217217

218+
/// Return an MCExpr to use for a reference to the specified symbol with the
219+
/// specified offset, where the symbol is a function symbol but we do not
220+
/// want any interposition, as this reference is for a specific instruction
221+
/// within the function (e.g. a BlockAddress).
222+
virtual const MCExpr *lowerCheriCodeReference(const MCSymbol *Sym,
223+
const MCExpr *Addend) const;
224+
218225
/// Target supports a PC-relative relocation that references the PLT of a
219226
/// function.
220227
bool hasPLTPCRelative() const { return PLTPCRelativeSpecifier; }

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4193,12 +4193,14 @@ static void emitGlobalConstantCHERICap(const DataLayout &DL, const Constant *CV,
41934193
return;
41944194
} else if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
41954195
if (auto BA = dyn_cast<BlockAddress>(CV)) {
4196-
// For block addresses we emit `.chericap FN+(.LtmpN - FN)`
4196+
// For block addresses we emit `.chericap FN+(.LtmpN - FN)` as a code
4197+
// capability.
41974198
// NB: Must use a non-preemptible symbol
41984199
auto FnStart = AP.getSymbolPreferLocal(*BA->getFunction(), true);
41994200
const MCExpr *Start = MCSymbolRefExpr::create(FnStart, Ctx);
42004201
const MCExpr *DiffToStart = MCBinaryExpr::createSub(SRE, Start, Ctx);
4201-
const MCExpr *CapExpr = MCBinaryExpr::createAdd(Start, DiffToStart, Ctx);
4202+
const MCExpr *CapExpr =
4203+
AP.getObjFileLowering().lowerCheriCodeReference(FnStart, DiffToStart);
42024204
AP.OutStreamer->emitCheriCapability(CapExpr, CapWidth);
42034205
return;
42044206
}

llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ void AsmPrinter::emitCallSiteCheriCapability(const MCSymbol *Hi,
216216
// this would result in emitCheriCapability() creating a relocation against
217217
// section plus offset rather than function + offset. We need the right
218218
// bounds and permissions info and need to use a non-preemptible alias.
219-
const MCExpr *Expr = MCSymbolRefExpr::create(CurrentFnBeginLocal, OutContext);
220-
Expr = MCBinaryExpr::createAdd(Expr, DiffToStart, OutContext);
219+
const MCExpr *Expr =
220+
TLOF.lowerCheriCodeReference(CurrentFnBeginLocal, DiffToStart);
221221
OutStreamer->emitCheriCapability(Expr, TLOF.getCheriCapabilitySize(TM));
222222
}
223223

llvm/lib/Target/TargetLoweringObjectFile.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,18 @@ const MCExpr *TargetLoweringObjectFile::getDebugThreadLocalSymbol(const MCSymbol
501501
return MCSymbolRefExpr::create(Sym, getContext());
502502
}
503503

504+
const MCExpr *
505+
TargetLoweringObjectFile::lowerCheriCodeReference(const MCSymbol *Sym,
506+
const MCExpr *Addend) const {
507+
// Default to a normal expression if the target does not treat these
508+
// specially.
509+
// TODO: Require every CHERI target to implement this?
510+
const MCExpr *Expr = MCSymbolRefExpr::create(Sym, getContext());
511+
if (Addend != nullptr)
512+
Expr = MCBinaryExpr::createAdd(Expr, Addend, getContext());
513+
return Expr;
514+
}
515+
504516
void TargetLoweringObjectFile::getNameWithPrefix(
505517
SmallVectorImpl<char> &OutName, const GlobalValue *GV,
506518
const TargetMachine &TM) const {

0 commit comments

Comments
 (0)