Skip to content

Commit d545ca5

Browse files
committed
[CHERIoT] Add support for generating export table entries for GlobalAlias's.
1 parent 44b9b1f commit d545ca5

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

llvm/lib/Target/RISCV/RISCVAsmPrinter.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,14 @@ class RISCVAsmPrinter : public AsmPrinter {
133133
uint32_t stackSize = 0;
134134
};
135135
SmallVector<CompartmentExport, 1> CompartmentEntries;
136+
SmallDenseMap<const Function *, SmallVector<const GlobalAlias *, 1>, 1>
137+
CompartmentEntryAliases;
136138

137139
void emitAttributes(const MCSubtargetInfo &SubtargetInfo);
138140

139141
void emitNTLHint(const MachineInstr *MI);
140142
void emitGlobalVariable(const GlobalVariable *GV) override;
143+
void emitGlobalAlias(const Module &M, const GlobalAlias &GV) override;
141144

142145
// XRay Support
143146
void LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr *MI);
@@ -313,6 +316,17 @@ void RISCVAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
313316
return AsmPrinter::emitGlobalVariable(GV);
314317
}
315318

319+
void RISCVAsmPrinter::emitGlobalAlias(const Module &M, const GlobalAlias &GA) {
320+
AsmPrinter::emitGlobalAlias(M, GA);
321+
322+
const MCSubtargetInfo &SubtargetInfo = *TM.getMCSubtargetInfo();
323+
if (SubtargetInfo.hasFeature(RISCV::FeatureVendorXCheriot)) {
324+
if (auto *Fn = dyn_cast<Function>(GA.getAliasee()->stripPointerCasts())) {
325+
CompartmentEntryAliases[Fn].push_back(&GA);
326+
}
327+
}
328+
}
329+
316330
// If the target supports Zihintntl and the instruction has a nontemporal
317331
// MachineMemOperand, emit an NTLH hint instruction before it.
318332
void RISCVAsmPrinter::emitNTLHint(const MachineInstr *MI) {
@@ -558,7 +572,6 @@ bool RISCVAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
558572
};
559573

560574
if (Fn.getCallingConv() == CallingConv::CHERI_CCallee) {
561-
Function &Fn = MF.getFunction();
562575
uint32_t stackSize;
563576
if (Fn.hasFnAttribute("minimum-stack-size")) {
564577
bool converted =
@@ -712,6 +725,26 @@ void RISCVAsmPrinter::emitEndOfAsmFile(Module &M) {
712725
OutStreamer->emitIntValue(std::min(uint32_t(255), stackSize), 1);
713726
OutStreamer->emitIntValue(Entry.LiveIns, 1);
714727
OutStreamer->emitELFSize(Sym, MCConstantExpr::create(4, C));
728+
729+
// Emit aliases for this export symbol entry.
730+
auto I = CompartmentEntryAliases.find(&Entry.Fn);
731+
if (I == CompartmentEntryAliases.end())
732+
continue;
733+
for (const GlobalAlias *GA : I->second) {
734+
std::string AliasExportName = getImportExportTableName(
735+
Entry.CompartmentName, GA->getName(), Entry.Fn.getCallingConv(),
736+
/*IsImport*/ false);
737+
auto AliasExportSym = C.getOrCreateSymbol(AliasExportName);
738+
739+
// Emit symbol alias in the export table for the alias using the same
740+
// attributes, linkage, and size as the primary entry.
741+
OutStreamer->emitSymbolAttribute(AliasExportSym, MCSA_ELF_TypeObject);
742+
if (GA->hasExternalLinkage() && !Entry.forceLocal)
743+
OutStreamer->emitSymbolAttribute(AliasExportSym, MCSA_Global);
744+
OutStreamer->emitAssignment(AliasExportSym,
745+
MCSymbolRefExpr::create(Sym, C));
746+
OutStreamer->emitELFSize(AliasExportSym, MCConstantExpr::create(4, C));
747+
}
715748
}
716749
}
717750
// Generate CHERIoT imports if there are any.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; RUN: llc --filetype=asm --mcpu=cheriot --mtriple=riscv32cheriot-unknown-cheriotrtos -target-abi cheriot %s -mattr=+xcheri,+cap-mode,+xcheriot -o - | FileCheck %s
2+
target datalayout = "e-m:e-p:32:32-i64:64-n32-S128-pf200:64:64:64:32-A200-P200-G200"
3+
target triple = "riscv32cheriot-unknown-cheriotrtos"
4+
5+
; Verify that we emit an export table entry for a GlobalAlias that is itself an alias to the
6+
; export table entry for the aliasee.
7+
8+
; CHECK: .section .compartment_exports,"aR",@progbits
9+
; CHECK: .type __library_export_libcalls__Z3foov,@object
10+
; CHECK: .globl __library_export_libcalls__Z3foov
11+
; CHECK: .p2align 2, 0x0
12+
; CHECK: __library_export_libcalls__Z3foov:
13+
; CHECK: .half _Z3foov-__compartment_pcc_start
14+
; CHECK: .byte 0
15+
; CHECK: .byte 0
16+
; CHECK: .size __library_export_libcalls__Z3foov, 4
17+
; CHECK: .type __library_export_libcalls__Z3barv,@object
18+
; CHECK: .globl __library_export_libcalls__Z3barv
19+
; CHECK: .set __library_export_libcalls__Z3barv, __library_export_libcalls__Z3foov
20+
; CHECK: .size __library_export_libcalls__Z3barv, 4
21+
22+
; Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
23+
define dso_local cherilibcallcc noundef i32 @_Z3foov() local_unnamed_addr addrspace(200) #0 {
24+
entry:
25+
ret i32 42
26+
}
27+
28+
@_Z3barv = alias i32(), ptr addrspace(200) @_Z3foov
29+
30+
attributes #0 = { mustprogress nofree norecurse nosync nounwind willreturn memory(none) "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="cheriot" "target-features"="+32bit,+c,+cap-mode,+e,+m,+relax,+xcheri,+zmmul,-a,-b,-d,-experimental-sdext,-experimental-sdtrig,-experimental-smctr,-experimental-ssctr,-experimental-svukte,-experimental-xqcia,-experimental-xqciac,-experimental-xqcicli,-experimental-xqcicm,-experimental-xqcics,-experimental-xqcicsr,-experimental-xqciint,-experimental-xqcilo,-experimental-xqcilsm,-experimental-xqcisls,-experimental-zalasr,-experimental-zicfilp,-experimental-zicfiss,-experimental-zvbc32e,-experimental-zvkgs,-f,-h,-i,-sha,-shcounterenw,-shgatpa,-shtvala,-shvsatpa,-shvstvala,-shvstvecd,-smaia,-smcdeleg,-smcsrind,-smdbltrp,-smepmp,-smmpm,-smnpm,-smrnmi,-smstateen,-ssaia,-ssccfg,-ssccptr,-sscofpmf,-sscounterenw,-sscsrind,-ssdbltrp,-ssnpm,-sspm,-ssqosid,-ssstateen,-ssstrict,-sstc,-sstvala,-sstvecd,-ssu64xl,-supm,-svade,-svadu,-svbare,-svinval,-svnapot,-svpbmt,-svvptc,-v,-xcheriot,-xcvalu,-xcvbi,-xcvbitmanip,-xcvelw,-xcvmac,-xcvmem,-xcvsimd,-xmipscmove,-xmipslsp,-xsfcease,-xsfvcp,-xsfvfnrclipxfqf,-xsfvfwmaccqqq,-xsfvqmaccdod,-xsfvqmaccqoq,-xsifivecdiscarddlone,-xsifivecflushdlone,-xtheadba,-xtheadbb,-xtheadbs,-xtheadcmo,-xtheadcondmov,-xtheadfmemidx,-xtheadmac,-xtheadmemidx,-xtheadmempair,-xtheadsync,-xtheadvdot,-xventanacondops,-xwchc,-za128rs,-za64rs,-zaamo,-zabha,-zacas,-zalrsc,-zama16b,-zawrs,-zba,-zbb,-zbc,-zbkb,-zbkc,-zbkx,-zbs,-zca,-zcb,-zcd,-zce,-zcf,-zcmop,-zcmp,-zcmt,-zdinx,-zfa,-zfbfmin,-zfh,-zfhmin,-zfinx,-zhinx,-zhinxmin,-zic64b,-zicbom,-zicbop,-zicboz,-ziccamoa,-ziccif,-zicclsm,-ziccrse,-zicntr,-zicond,-zicsr,-zifencei,-zihintntl,-zihintpause,-zihpm,-zimop,-zk,-zkn,-zknd,-zkne,-zknh,-zkr,-zks,-zksed,-zksh,-zkt,-ztso,-zvbb,-zvbc,-zve32f,-zve32x,-zve64d,-zve64f,-zve64x,-zvfbfmin,-zvfbfwma,-zvfh,-zvfhmin,-zvkb,-zvkg,-zvkn,-zvknc,-zvkned,-zvkng,-zvknha,-zvknhb,-zvks,-zvksc,-zvksed,-zvksg,-zvksh,-zvkt,-zvl1024b,-zvl128b,-zvl16384b,-zvl2048b,-zvl256b,-zvl32768b,-zvl32b,-zvl4096b,-zvl512b,-zvl64b,-zvl65536b,-zvl8192b" }
31+
32+
!llvm.module.flags = !{!0, !1, !2, !4}
33+
!llvm.ident = !{!5}
34+
35+
!0 = !{i32 1, !"wchar_size", i32 4}
36+
!1 = !{i32 1, !"target-abi", !"cheriot"}
37+
!2 = !{i32 6, !"riscv-isa", !3}
38+
!3 = !{!"rv32e2p0_m2p0_c2p0_zmmul1p0_xcheri0p0"}
39+
!4 = !{i32 8, !"SmallDataLimit", i32 0}
40+
!5 = !{!"clang version 20.1.3 ([email protected]:resistor/llvm-project-1.git b5150572190bf7926bc7c77858e6f6d49f49b0bb)"}

0 commit comments

Comments
 (0)