1 //===---------------------------- FaultMaps.cpp ---------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/CodeGen/FaultMaps.h" 11 12 #include "llvm/CodeGen/AsmPrinter.h" 13 #include "llvm/MC/MCContext.h" 14 #include "llvm/MC/MCExpr.h" 15 #include "llvm/MC/MCObjectFileInfo.h" 16 #include "llvm/MC/MCStreamer.h" 17 #include "llvm/Support/Debug.h" 18 19 using namespace llvm; 20 21 #define DEBUG_TYPE "faultmaps" 22 23 static const int FaultMapVersion = 1; 24 const char *FaultMaps::WFMP = "Fault Maps: "; 25 26 FaultMaps::FaultMaps(AsmPrinter &AP) : AP(AP) {} 27 28 void FaultMaps::recordFaultingOp(FaultType FaultTy, 29 const MCSymbol *HandlerLabel) { 30 MCContext &OutContext = AP.OutStreamer->getContext(); 31 MCSymbol *FaultingLabel = OutContext.createTempSymbol(); 32 33 AP.OutStreamer->EmitLabel(FaultingLabel); 34 35 const MCExpr *FaultingOffset = MCBinaryExpr::createSub( 36 MCSymbolRefExpr::create(FaultingLabel, OutContext), 37 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext); 38 39 const MCExpr *HandlerOffset = MCBinaryExpr::createSub( 40 MCSymbolRefExpr::create(HandlerLabel, OutContext), 41 MCSymbolRefExpr::create(AP.CurrentFnSymForSize, OutContext), OutContext); 42 43 FunctionInfos[AP.CurrentFnSym].emplace_back(FaultTy, FaultingOffset, 44 HandlerOffset); 45 } 46 47 void FaultMaps::serializeToFaultMapSection() { 48 if (FunctionInfos.empty()) 49 return; 50 51 MCContext &OutContext = AP.OutStreamer->getContext(); 52 MCStreamer &OS = *AP.OutStreamer; 53 54 // Create the section. 55 MCSection *FaultMapSection = 56 OutContext.getObjectFileInfo()->getFaultMapSection(); 57 OS.SwitchSection(FaultMapSection); 58 59 // Emit a dummy symbol to force section inclusion. 60 OS.EmitLabel(OutContext.getOrCreateSymbol(Twine("__LLVM_FaultMaps"))); 61 62 DEBUG(dbgs() << "********** Fault Map Output **********\n"); 63 64 // Header 65 OS.EmitIntValue(FaultMapVersion, 1); // Version. 66 OS.EmitIntValue(0, 1); // Reserved. 67 OS.EmitIntValue(0, 2); // Reserved. 68 69 DEBUG(dbgs() << WFMP << "#functions = " << FunctionInfos.size() << "\n"); 70 OS.EmitIntValue(FunctionInfos.size(), 4); 71 72 DEBUG(dbgs() << WFMP << "functions:\n"); 73 74 for (const auto &FFI : FunctionInfos) 75 emitFunctionInfo(FFI.first, FFI.second); 76 } 77 78 void FaultMaps::emitFunctionInfo(const MCSymbol *FnLabel, 79 const FunctionFaultInfos &FFI) { 80 MCStreamer &OS = *AP.OutStreamer; 81 82 DEBUG(dbgs() << WFMP << " function addr: " << *FnLabel << "\n"); 83 OS.EmitSymbolValue(FnLabel, 8); 84 85 DEBUG(dbgs() << WFMP << " #faulting PCs: " << FFI.size() << "\n"); 86 OS.EmitIntValue(FFI.size(), 4); 87 88 OS.EmitIntValue(0, 4); // Reserved 89 90 for (auto &Fault : FFI) { 91 DEBUG(dbgs() << WFMP << " fault type: " 92 << faultTypeToString(Fault.FaultType) << "\n"); 93 OS.EmitIntValue(Fault.FaultType, 4); 94 95 DEBUG(dbgs() << WFMP << " faulting PC offset: " 96 << *Fault.FaultingOffsetExpr << "\n"); 97 OS.EmitValue(Fault.FaultingOffsetExpr, 4); 98 99 DEBUG(dbgs() << WFMP << " fault handler PC offset: " 100 << *Fault.HandlerOffsetExpr << "\n"); 101 OS.EmitValue(Fault.HandlerOffsetExpr, 4); 102 } 103 } 104 105 106 const char *FaultMaps::faultTypeToString(FaultMaps::FaultType FT) { 107 switch (FT) { 108 default: 109 llvm_unreachable("unhandled fault type!"); 110 111 case FaultMaps::FaultingLoad: 112 return "FaultingLoad"; 113 } 114 } 115