1 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===// 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 // This file contains support for writing DWARF exception info into asm files. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "DwarfException.h" 15 #include "llvm/ADT/SmallString.h" 16 #include "llvm/ADT/StringExtras.h" 17 #include "llvm/ADT/Twine.h" 18 #include "llvm/CodeGen/AsmPrinter.h" 19 #include "llvm/CodeGen/MachineFrameInfo.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineModuleInfo.h" 22 #include "llvm/IR/DataLayout.h" 23 #include "llvm/IR/Mangler.h" 24 #include "llvm/IR/Module.h" 25 #include "llvm/MC/MCAsmInfo.h" 26 #include "llvm/MC/MCContext.h" 27 #include "llvm/MC/MCExpr.h" 28 #include "llvm/MC/MCSection.h" 29 #include "llvm/MC/MCStreamer.h" 30 #include "llvm/MC/MCSymbol.h" 31 #include "llvm/MC/MachineLocation.h" 32 #include "llvm/Support/Dwarf.h" 33 #include "llvm/Support/ErrorHandling.h" 34 #include "llvm/Support/FormattedStream.h" 35 #include "llvm/Target/TargetFrameLowering.h" 36 #include "llvm/Target/TargetLoweringObjectFile.h" 37 #include "llvm/Target/TargetMachine.h" 38 #include "llvm/Target/TargetOptions.h" 39 #include "llvm/Target/TargetRegisterInfo.h" 40 using namespace llvm; 41 42 DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A) 43 : EHStreamer(A), shouldEmitCFI(false) {} 44 45 void DwarfCFIExceptionBase::markFunctionEnd() { 46 if (shouldEmitCFI) 47 Asm->OutStreamer->EmitCFIEndProc(); 48 49 if (MMI->getLandingPads().empty()) 50 return; 51 52 // Map all labels and get rid of any dead landing pads. 53 MMI->TidyLandingPads(); 54 } 55 56 DwarfCFIException::DwarfCFIException(AsmPrinter *A) 57 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false), 58 shouldEmitLSDA(false), shouldEmitMoves(false), 59 moveTypeModule(AsmPrinter::CFI_M_None) {} 60 61 DwarfCFIException::~DwarfCFIException() {} 62 63 /// endModule - Emit all exception information that should come after the 64 /// content. 65 void DwarfCFIException::endModule() { 66 if (moveTypeModule == AsmPrinter::CFI_M_Debug) 67 Asm->OutStreamer->EmitCFISections(false, true); 68 69 // SjLj uses this pass and it doesn't need this info. 70 if (!Asm->MAI->usesCFIForEH()) 71 return; 72 73 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 74 75 unsigned PerEncoding = TLOF.getPersonalityEncoding(); 76 77 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect) 78 return; 79 80 // Emit references to all used personality functions 81 const std::vector<const Function*> &Personalities = MMI->getPersonalities(); 82 for (size_t i = 0, e = Personalities.size(); i != e; ++i) { 83 if (!Personalities[i]) 84 continue; 85 MCSymbol *Sym = Asm->getSymbol(Personalities[i]); 86 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym); 87 } 88 } 89 90 void DwarfCFIException::beginFunction(const MachineFunction *MF) { 91 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false; 92 const Function *F = MF->getFunction(); 93 94 // If any landing pads survive, we need an EH table. 95 bool hasLandingPads = !MMI->getLandingPads().empty(); 96 97 // See if we need frame move info. 98 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves(); 99 if (MoveType == AsmPrinter::CFI_M_EH || 100 (MoveType == AsmPrinter::CFI_M_Debug && 101 moveTypeModule == AsmPrinter::CFI_M_None)) 102 moveTypeModule = MoveType; 103 104 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None; 105 106 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 107 unsigned PerEncoding = TLOF.getPersonalityEncoding(); 108 const Function *Per = nullptr; 109 if (F->hasPersonalityFn()) 110 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts()); 111 assert(!MMI->getPersonality() || Per == MMI->getPersonality()); 112 113 // Emit a personality function even when there are no landing pads 114 bool forceEmitPersonality = 115 // ...if a personality function is explicitly specified 116 F->hasPersonalityFn() && 117 // ... and it's not known to be a noop in the absence of invokes 118 !isNoOpWithoutInvoke(classifyEHPersonality(Per)) && 119 // ... and we're not explicitly asked not to emit it 120 F->needsUnwindTableEntry(); 121 122 shouldEmitPersonality = 123 (forceEmitPersonality || 124 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) && 125 Per; 126 127 unsigned LSDAEncoding = TLOF.getLSDAEncoding(); 128 shouldEmitLSDA = shouldEmitPersonality && 129 LSDAEncoding != dwarf::DW_EH_PE_omit; 130 131 shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves; 132 if (!shouldEmitCFI) 133 return; 134 135 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false); 136 137 // Indicate personality routine, if any. 138 if (!shouldEmitPersonality) 139 return; 140 141 // If we are forced to emit this personality, make sure to record 142 // it because it might not appear in any landingpad 143 if (forceEmitPersonality) 144 MMI->addPersonality(Per); 145 146 const MCSymbol *Sym = 147 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI); 148 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding); 149 150 // Provide LSDA information. 151 if (!shouldEmitLSDA) 152 return; 153 154 Asm->OutStreamer->EmitCFILsda(Asm->getCurExceptionSym(), LSDAEncoding); 155 } 156 157 /// endFunction - Gather and emit post-function exception information. 158 /// 159 void DwarfCFIException::endFunction(const MachineFunction *) { 160 if (!shouldEmitPersonality) 161 return; 162 163 emitExceptionTable(); 164 } 165