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 DwarfCFIException::DwarfCFIException(AsmPrinter *A) 43 : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false), 44 shouldEmitMoves(false), moveTypeModule(AsmPrinter::CFI_M_None) {} 45 46 DwarfCFIException::~DwarfCFIException() {} 47 48 /// endModule - Emit all exception information that should come after the 49 /// content. 50 void DwarfCFIException::endModule() { 51 if (moveTypeModule == AsmPrinter::CFI_M_Debug) 52 Asm->OutStreamer.EmitCFISections(false, true); 53 54 if (!Asm->MAI->isExceptionHandlingDwarf()) 55 return; 56 57 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 58 59 unsigned PerEncoding = TLOF.getPersonalityEncoding(); 60 61 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect) 62 return; 63 64 // Emit references to all used personality functions 65 bool AtLeastOne = false; 66 const std::vector<const Function*> &Personalities = MMI->getPersonalities(); 67 for (size_t i = 0, e = Personalities.size(); i != e; ++i) { 68 if (!Personalities[i]) 69 continue; 70 MCSymbol *Sym = Asm->getSymbol(Personalities[i]); 71 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym); 72 AtLeastOne = true; 73 } 74 75 if (AtLeastOne && !TLOF.isFunctionEHFrameSymbolPrivate()) { 76 // This is a temporary hack to keep sections in the same order they 77 // were before. This lets us produce bit identical outputs while 78 // transitioning to CFI. 79 Asm->OutStreamer.SwitchSection( 80 const_cast<TargetLoweringObjectFile&>(TLOF).getEHFrameSection()); 81 } 82 } 83 84 /// beginFunction - Gather pre-function exception information. Assumes it's 85 /// being emitted immediately after the function entry point. 86 void DwarfCFIException::beginFunction(const MachineFunction *MF) { 87 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false; 88 89 // If any landing pads survive, we need an EH table. 90 bool hasLandingPads = !MMI->getLandingPads().empty(); 91 92 // See if we need frame move info. 93 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves(); 94 if (MoveType == AsmPrinter::CFI_M_EH || 95 (MoveType == AsmPrinter::CFI_M_Debug && 96 moveTypeModule == AsmPrinter::CFI_M_None)) 97 moveTypeModule = MoveType; 98 99 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None; 100 101 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 102 unsigned PerEncoding = TLOF.getPersonalityEncoding(); 103 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()]; 104 105 shouldEmitPersonality = hasLandingPads && 106 PerEncoding != dwarf::DW_EH_PE_omit && Per; 107 108 unsigned LSDAEncoding = TLOF.getLSDAEncoding(); 109 shouldEmitLSDA = shouldEmitPersonality && 110 LSDAEncoding != dwarf::DW_EH_PE_omit; 111 112 if (!shouldEmitPersonality && !shouldEmitMoves) 113 return; 114 115 Asm->OutStreamer.EmitCFIStartProc(/*IsSimple=*/false); 116 117 // Indicate personality routine, if any. 118 if (!shouldEmitPersonality) 119 return; 120 121 const MCSymbol *Sym = 122 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI); 123 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding); 124 125 Asm->OutStreamer.EmitDebugLabel 126 (Asm->GetTempSymbol("eh_func_begin", 127 Asm->getFunctionNumber())); 128 129 // Provide LSDA information. 130 if (!shouldEmitLSDA) 131 return; 132 133 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception", 134 Asm->getFunctionNumber()), 135 LSDAEncoding); 136 } 137 138 /// endFunction - Gather and emit post-function exception information. 139 /// 140 void DwarfCFIException::endFunction(const MachineFunction *) { 141 if (!shouldEmitPersonality && !shouldEmitMoves) 142 return; 143 144 Asm->OutStreamer.EmitCFIEndProc(); 145 146 if (!shouldEmitPersonality) 147 return; 148 149 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end", 150 Asm->getFunctionNumber())); 151 152 // Map all labels and get rid of any dead landing pads. 153 MMI->TidyLandingPads(); 154 155 emitExceptionTable(); 156 } 157