Skip to content

Commit a653e5b

Browse files
jrtc27resistor
authored andcommitted
[NFCI][CodeGen][MC][Mips][RISCV] Follow emitValue for EmitCheriCapability
Rename the underlying function to emitCheriCapability, and have the symbol-only version be non-virtual and called emitSymbolCheriCapability like emitSymbolValue. Drop the addend overloads and let the callers create the adds. Not many callers exist, no equivalent exists for integers, and two of the three uses will be changed in a future commit anyway.
1 parent a731b07 commit a653e5b

File tree

21 files changed

+94
-198
lines changed

21 files changed

+94
-198
lines changed

lld/test/ELF/cheri/exception-table.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88
; RUN: llvm-readobj -r %t/riscv.o | FileCheck %s --check-prefix=RV64-OBJ-RELOCS
99
;; Should have two relocations against a local alias for _Z4testll
1010
; MIPS-OBJ-RELOCS: Section ({{.+}}) .rela.gcc_except_table {
11-
; MIPS-OBJ-RELOCS-NEXT: R_MIPS_CHERI_CAPABILITY/R_MIPS_NONE/R_MIPS_NONE .Llpad0 0x0{{$}}
12-
; MIPS-OBJ-RELOCS-NEXT: R_MIPS_CHERI_CAPABILITY/R_MIPS_NONE/R_MIPS_NONE .Llpad1 0x0{{$}}
13-
; MIPS-OBJ-RELOCS-NEXT: R_MIPS_CHERI_CAPABILITY/R_MIPS_NONE/R_MIPS_NONE .Llpad2 0x0{{$}}
11+
; MIPS-OBJ-RELOCS-NEXT: R_MIPS_CHERI_CAPABILITY/R_MIPS_NONE/R_MIPS_NONE .L_Z4testll$local 0x80
12+
; MIPS-OBJ-RELOCS-NEXT: R_MIPS_CHERI_CAPABILITY/R_MIPS_NONE/R_MIPS_NONE .L_Z4testll$local 0x60
13+
; MIPS-OBJ-RELOCS-NEXT: R_MIPS_CHERI_CAPABILITY/R_MIPS_NONE/R_MIPS_NONE .L_Z5test2ll$local 0x5C
1414
; MIPS-OBJ-RELOCS-NEXT: R_MIPS_PC32/R_MIPS_NONE/R_MIPS_NONE .L_ZTIl.DW.stub 0x0
15-
; MIPS-OBJ-RELOCS-NEXT: R_MIPS_CHERI_CAPABILITY/R_MIPS_NONE/R_MIPS_NONE .Llpad3 0x0{{$}}
15+
; MIPS-OBJ-RELOCS-NEXT: R_MIPS_CHERI_CAPABILITY/R_MIPS_NONE/R_MIPS_NONE .L_Z9test_weakll$local 0x34
1616
; MIPS-OBJ-RELOCS-NEXT: }
1717
; RV64-OBJ-RELOCS: Section ({{.+}}) .rela.gcc_except_table {
18-
; RV64-OBJ-RELOCS-NEXT: 0x20 R_RISCV_CHERI_CAPABILITY .Llpad0 0x0{{$}}
19-
; RV64-OBJ-RELOCS-NEXT: 0x40 R_RISCV_CHERI_CAPABILITY .Llpad1 0x0{{$}}
20-
; RV64-OBJ-RELOCS-NEXT: 0x80 R_RISCV_CHERI_CAPABILITY .Llpad2 0x0{{$}}
18+
; RV64-OBJ-RELOCS-NEXT: 0x20 R_RISCV_CHERI_CAPABILITY .L_Z4testll$local 0x5C
19+
; RV64-OBJ-RELOCS-NEXT: 0x40 R_RISCV_CHERI_CAPABILITY .L_Z4testll$local 0x48
20+
; RV64-OBJ-RELOCS-NEXT: 0x80 R_RISCV_CHERI_CAPABILITY .L_Z5test2ll$local 0x44
2121
; RV64-OBJ-RELOCS-NEXT: 0xA4 R_RISCV_ADD32 .L_ZTIl.DW.stub 0x0
2222
; RV64-OBJ-RELOCS-NEXT: 0xA4 R_RISCV_SUB32 .L0 0x0
23-
; RV64-OBJ-RELOCS-NEXT: 0xC0 R_RISCV_CHERI_CAPABILITY .Llpad3 0x0{{$}}
23+
; RV64-OBJ-RELOCS-NEXT: 0xC0 R_RISCV_CHERI_CAPABILITY .L_Z9test_weakll$local 0x1C
2424
; RV64-OBJ-RELOCS-NEXT: }
2525

2626
;; This should work with both -z text and -z notext

llvm/include/llvm/MC/MCStreamer.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,12 @@ class LLVM_ABI MCStreamer {
778778
// MCAsmInfo only knowns about the triple which is not enough
779779

780780
// Emit the expression \p Value into the output as a CHERI capability
781-
void EmitCheriCapability(const MCSymbol *Value, int64_t Addend,
782-
unsigned CapSize, SMLoc Loc = SMLoc());
783-
void EmitCheriCapability(const MCSymbol *Value, const MCExpr *Addend,
784-
unsigned CapSize, SMLoc Loc = SMLoc());
781+
virtual void emitCheriCapability(const MCExpr *Value, unsigned CapSize,
782+
SMLoc Loc = SMLoc());
783+
784+
/// Special case of emitCheriCapability that avoids the client having to pass
785+
/// in a MCExpr for MCSymbols.
786+
void emitSymbolCheriCapability(const MCSymbol *Sym, unsigned CapSize);
785787

786788
// Emit \p Value as an untagged capability-size value
787789
virtual void emitCheriIntcap(int64_t Value, unsigned CapSize,
@@ -1130,11 +1132,8 @@ class LLVM_ABI MCStreamer {
11301132
/// Return the end symbol generated inside, the caller needs to emit it.
11311133
virtual MCSymbol *emitDwarfUnitLength(const Twine &Prefix,
11321134
const Twine &Comment);
1133-
protected:
1134-
virtual void EmitCheriCapabilityImpl(const MCSymbol *Value,
1135-
const MCExpr *Addend, unsigned CapSize,
1136-
SMLoc Loc = SMLoc());
11371135

1136+
protected:
11381137
/// Target-independent untagged CHERI capability
11391138
virtual void emitCheriIntcapGeneric(const MCExpr *Expr, unsigned CapSize,
11401139
SMLoc Loc);

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,8 +2655,8 @@ bool AsmPrinter::doFinalization(Module &M) {
26552655
for (const auto &Stub : Stubs) {
26562656
OutStreamer->emitLabel(Stub.first);
26572657
if (DL.isFatPointer(AS))
2658-
OutStreamer->EmitCheriCapability(Stub.second.getPointer(), nullptr,
2659-
Size);
2658+
OutStreamer->emitSymbolCheriCapability(Stub.second.getPointer(),
2659+
Size);
26602660
else
26612661
OutStreamer->emitSymbolValue(Stub.second.getPointer(), Size);
26622662
}
@@ -4149,19 +4149,25 @@ static void emitGlobalConstantCHERICap(const DataLayout &DL, const Constant *CV,
41494149
return;
41504150
}
41514151
GlobalValue *GV;
4152-
APInt Addend;
4153-
if (IsConstantOffsetFromGlobal(const_cast<Constant *>(CV), GV, Addend, DL,
4152+
APInt Offset;
4153+
MCContext &Ctx = AP.OutContext;
4154+
if (IsConstantOffsetFromGlobal(const_cast<Constant *>(CV), GV, Offset, DL,
41544155
true)) {
4155-
AP.OutStreamer->EmitCheriCapability(AP.getSymbol(GV), Addend.getSExtValue(),
4156-
CapWidth);
4156+
const MCExpr *CapExpr = MCSymbolRefExpr::create(AP.getSymbol(GV), Ctx);
4157+
int64_t Addend = Offset.getSExtValue();
4158+
if (Addend != 0)
4159+
CapExpr = MCBinaryExpr::createAdd(
4160+
CapExpr, MCConstantExpr::create(Addend, Ctx), Ctx);
4161+
AP.OutStreamer->emitCheriCapability(CapExpr, CapWidth);
41574162
return;
41584163
} else if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Expr)) {
41594164
if (auto BA = dyn_cast<BlockAddress>(CV)) {
41604165
// For block addresses we emit `.chericap FN+(.LtmpN - FN)`
41614166
auto FnStart = AP.getSymbol(BA->getFunction());
4162-
const MCExpr *DiffToStart = MCBinaryExpr::createSub(
4163-
SRE, MCSymbolRefExpr::create(FnStart, AP.OutContext), AP.OutContext);
4164-
AP.OutStreamer->EmitCheriCapability(FnStart, DiffToStart, CapWidth);
4167+
const MCExpr *Start = MCSymbolRefExpr::create(FnStart, Ctx);
4168+
const MCExpr *DiffToStart = MCBinaryExpr::createSub(SRE, Start, Ctx);
4169+
const MCExpr *CapExpr = MCBinaryExpr::createAdd(Start, DiffToStart, Ctx);
4170+
AP.OutStreamer->emitCheriCapability(CapExpr, CapWidth);
41654171
return;
41664172
}
41674173
// Emit capability for label whose address is stored in a global variable
@@ -4170,7 +4176,7 @@ static void emitGlobalConstantCHERICap(const DataLayout &DL, const Constant *CV,
41704176
report_fatal_error(
41714177
"Cannot emit a global .chericap referring to a temporary since this "
41724178
"will result in the wrong value at runtime!");
4173-
AP.OutStreamer->EmitCheriCapability(&SRE->getSymbol(), nullptr, CapWidth);
4179+
AP.OutStreamer->emitSymbolCheriCapability(&SRE->getSymbol(), CapWidth);
41744180
return;
41754181
}
41764182
}

llvm/lib/CodeGen/AsmPrinter/AsmPrinterDwarf.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,22 @@ void AsmPrinter::emitCallSiteValue(uint64_t Value, unsigned Encoding) const {
203203

204204
void AsmPrinter::emitCallSiteCheriCapability(const MCSymbol *Hi,
205205
const MCSymbol *Lo) const {
206+
assert(CurrentFnBeginLocal && "Missing local function entry alias for EH!");
207+
208+
const TargetLoweringObjectFile &TLOF = getObjFileLowering();
209+
210+
// Get the Hi-Lo expression. We use (and need) Lo since the offset needs to
211+
// be a constant expression, whereas CurrentFnSym is preemptible.
212+
const MCExpr *DiffToStart = MCBinaryExpr::createSub(
213+
MCSymbolRefExpr::create(Hi, OutContext),
214+
MCSymbolRefExpr::create(Lo, OutContext), OutContext);
206215
// Note: we cannot use Lo here since that is an assembler-local symbol and
207-
// this would result in EmitCheriCapability() creating a relocation against
216+
// this would result in emitCheriCapability() creating a relocation against
208217
// section plus offset rather than function + offset. We need the right
209218
// bounds and permissions info and need to use a non-preemptible alias.
210-
assert(CurrentFnBeginLocal && "Missing local function entry alias for EH!");
211-
// Ensure that CurrentFnBeginLocal ends up in the symbol table so that ld.lld
212-
// can find the surrounding function even if the actual function is not used.
213-
// This happens with weak functions where the unused function's landing pads
214-
// would otherwise no longer have a valid surrounding symbol. While this does
215-
// not matter as they are unused, it does trigger ld.lld warnings. Always
216-
// emitting the local symbol also ensures we can find a valid surrounding and
217-
// non-preemptible symbol with a size set.
218-
CurrentFnBeginLocal->setUsedInReloc();
219-
OutStreamer->EmitCheriCapability(
220-
Hi, (int64_t)0, getObjFileLowering().getCheriCapabilitySize(TM));
219+
const MCExpr *Expr = MCSymbolRefExpr::create(CurrentFnBeginLocal, OutContext);
220+
Expr = MCBinaryExpr::createAdd(Expr, DiffToStart, OutContext);
221+
OutStreamer->emitCheriCapability(Expr, TLOF.getCheriCapabilitySize(TM));
221222
}
222223

223224
//===----------------------------------------------------------------------===//

llvm/lib/CodeGen/MachineFunction.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -831,13 +831,7 @@ void MachineFunction::addInvoke(MachineBasicBlock *LandingPad,
831831
}
832832

833833
MCSymbol *MachineFunction::addLandingPad(MachineBasicBlock *LandingPad) {
834-
// For the purecap ABIs we create a relocation against this symbol, so ensure
835-
// that there is a named symbol in the object file to make objdump/readelf
836-
// output more easily parseable. We avoid doing this unconditonally as it
837-
// would affect many upstream tests.
838-
MCSymbol *LandingPadLabel = Ctx.getAsmInfo()->isCheriPurecapABI()
839-
? Ctx.createNamedTempSymbol("lpad")
840-
: Ctx.createTempSymbol();
834+
MCSymbol *LandingPadLabel = Ctx.createTempSymbol();
841835
LandingPadInfo &LP = getOrCreateLandingPadInfo(LandingPad);
842836
LP.LandingPadLabel = LandingPadLabel;
843837

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ void TargetLoweringObjectFileELF::emitPersonalityValueImpl(
428428
unsigned AS = DL.getProgramAddressSpace();
429429
unsigned Size = DL.getPointerSize(AS);
430430
if (DL.isFatPointer(DL.getProgramAddressSpace())) {
431-
Streamer.EmitCheriCapability(Sym, nullptr, Size);
431+
Streamer.emitSymbolCheriCapability(Sym, Size);
432432
} else {
433433
Streamer.emitSymbolValue(Sym, Size);
434434
}

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,8 @@ class MCAsmStreamer final : public MCStreamer {
414414
void emitBundleLock(bool AlignToEnd) override;
415415
void emitBundleUnlock() override;
416416

417-
void EmitCheriCapabilityImpl(const MCSymbol *Symbol, const MCExpr *Addend,
418-
unsigned CapSize, SMLoc Loc = SMLoc()) override;
417+
virtual void emitCheriCapability(const MCExpr *Value, unsigned CapSize,
418+
SMLoc Loc = SMLoc()) override;
419419
void emitCheriIntcap(const MCExpr *Expr, unsigned CapSize,
420420
SMLoc Loc = SMLoc()) override;
421421

@@ -2548,24 +2548,15 @@ void MCAsmStreamer::emitBundleUnlock() {
25482548
EmitEOL();
25492549
}
25502550

2551-
void MCAsmStreamer::EmitCheriCapabilityImpl(const MCSymbol *Symbol,
2552-
const MCExpr *Addend,
2553-
unsigned CapSize, SMLoc Loc) {
2551+
void MCAsmStreamer::emitCheriCapability(const MCExpr *Value, unsigned CapSize,
2552+
SMLoc Loc) {
25542553
OS << "\t.chericap\t";
2555-
Symbol->print(OS, MAI);
2556-
// Avoid parens,unary minus, and zero for constants:
2557-
assert(Addend);
2558-
if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Addend)) {
2559-
int64_t Offset = CE->getValue();
2560-
if (Offset > 0)
2561-
OS << "+" << Offset;
2562-
else if (Offset < 0)
2563-
OS << Offset;
2554+
if (MCTargetStreamer *TS = getTargetStreamer()) {
2555+
TS->emitValue(Value);
25642556
} else {
2565-
OS << " + ";
2566-
MAI->printExpr(OS, *Addend);
2557+
MAI->printExpr(OS, *Value);
2558+
EmitEOL();
25672559
}
2568-
EmitEOL();
25692560
}
25702561

25712562
void MCAsmStreamer::emitCheriIntcap(const MCExpr *Expr, unsigned CapSize,

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5849,39 +5849,11 @@ bool AsmParser::parseDirectiveCheriCap(SMLoc DirectiveLoc) {
58495849
int64_t Offset = 0;
58505850
unsigned CapSize = getTargetParser().getCheriCapabilitySize();
58515851
// Allow .chericap 0x123456 to create an untagged uintcap_t
5852-
if (SymExpr->evaluateAsAbsolute(Offset)) {
5852+
if (SymExpr->evaluateAsAbsolute(Offset, getStreamer().getAssemblerPtr()))
58535853
getStreamer().emitCheriIntcap(Offset, CapSize, ExprLoc);
5854-
} else {
5855-
const MCSymbolRefExpr *SRE = nullptr;
5856-
if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(SymExpr)) {
5857-
const MCConstantExpr *CE = nullptr;
5858-
bool Neg = false;
5859-
switch (BE->getOpcode()) {
5860-
case MCBinaryExpr::Sub:
5861-
Neg = true;
5862-
LLVM_FALLTHROUGH;
5863-
case MCBinaryExpr::Add:
5864-
CE = dyn_cast<MCConstantExpr>(BE->getRHS());
5865-
break;
5866-
default:
5867-
break;
5868-
}
5854+
else
5855+
getStreamer().emitCheriCapability(SymExpr, CapSize, ExprLoc);
58695856

5870-
SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
5871-
if (!SRE || !CE)
5872-
return Error(ExprLoc, "must be sym[+const]");
5873-
Offset = CE->getValue();
5874-
if (Neg)
5875-
Offset = -Offset;
5876-
} else {
5877-
SRE = dyn_cast<MCSymbolRefExpr>(SymExpr);
5878-
if (!SRE)
5879-
return Error(ExprLoc, "must be sym[+const]");
5880-
Offset = 0;
5881-
}
5882-
const MCSymbol &Symbol = SRE->getSymbol();
5883-
getStreamer().EmitCheriCapability(&Symbol, Offset, CapSize, ExprLoc);
5884-
}
58855857
if (parseToken(AsmToken::EndOfStatement, "expected end of statement"))
58865858
return true;
58875859
return false;

llvm/lib/MC/MCStreamer.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,25 +195,15 @@ void MCStreamer::emitSymbolValue(const MCSymbol *Sym, unsigned Size,
195195
emitCOFFSecRel32(Sym, /*Offset=*/0);
196196
}
197197

198-
void MCStreamer::EmitCheriCapability(const MCSymbol *Value,
199-
const MCExpr *Addend, unsigned CapSize,
198+
void MCStreamer::emitCheriCapability(const MCExpr *Value, unsigned CapSize,
200199
SMLoc Loc) {
201-
if (!Addend) {
202-
Addend = MCConstantExpr::create(0, Context);
203-
}
204-
EmitCheriCapabilityImpl(Value, Addend, CapSize, Loc);
205-
}
206-
207-
void MCStreamer::EmitCheriCapabilityImpl(const MCSymbol *Value,
208-
const MCExpr *Addend, unsigned CapSize,
209-
SMLoc Loc) {
210-
report_fatal_error("EmitCheriCapability is not implemented for this target!");
200+
report_fatal_error("emitCheriCapability is not implemented for this target!");
211201
}
212202

213-
void MCStreamer::EmitCheriCapability(const MCSymbol *Value, int64_t Addend,
214-
unsigned CapSize, SMLoc Loc) {
215-
EmitCheriCapability(Value, MCConstantExpr::create(Addend, Context), CapSize,
216-
Loc);
203+
void MCStreamer::emitSymbolCheriCapability(const MCSymbol *Sym,
204+
unsigned CapSize) {
205+
const MCExpr *Expr = MCSymbolRefExpr::create(Sym, getContext());
206+
emitCheriCapability(Expr, CapSize);
217207
}
218208

219209
void MCStreamer::emitCheriIntcap(int64_t Value, unsigned CapSize, SMLoc Loc) {

llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,7 @@ unsigned MipsELFObjectWriter::getRelocType(const MCFixup &Fixup,
379379
const auto ElfSym = cast<const MCSymbolELF>(Target.getAddSym());
380380
// Assert that we don't create .chericap relocations against temporary
381381
// symbols since those will result in wrong relocations (against sec+offset)
382-
if (ElfSym->isDefined() && !ElfSym->getSize() &&
383-
!ElfSym->getName().starts_with(".Llpad")) {
382+
if (ElfSym->isDefined() && !ElfSym->getSize()) {
384383
getContext().reportWarning(Fixup.getLoc(),
385384
"creating a R_MIPS_CHERI_CAPABILITY relocation against an unsized "
386385
"defined symbol: " + ElfSym->getName() +

0 commit comments

Comments
 (0)