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/Module.h" 16 #include "llvm/CodeGen/AsmPrinter.h" 17 #include "llvm/CodeGen/MachineModuleInfo.h" 18 #include "llvm/CodeGen/MachineFrameInfo.h" 19 #include "llvm/CodeGen/MachineFunction.h" 20 #include "llvm/CodeGen/MachineLocation.h" 21 #include "llvm/MC/MCAsmInfo.h" 22 #include "llvm/MC/MCContext.h" 23 #include "llvm/MC/MCExpr.h" 24 #include "llvm/MC/MCSection.h" 25 #include "llvm/MC/MCStreamer.h" 26 #include "llvm/MC/MCSymbol.h" 27 #include "llvm/Target/Mangler.h" 28 #include "llvm/Target/TargetData.h" 29 #include "llvm/Target/TargetFrameLowering.h" 30 #include "llvm/Target/TargetLoweringObjectFile.h" 31 #include "llvm/Target/TargetMachine.h" 32 #include "llvm/Target/TargetOptions.h" 33 #include "llvm/Target/TargetRegisterInfo.h" 34 #include "llvm/Support/Dwarf.h" 35 #include "llvm/Support/ErrorHandling.h" 36 #include "llvm/Support/FormattedStream.h" 37 #include "llvm/ADT/SmallString.h" 38 #include "llvm/ADT/StringExtras.h" 39 #include "llvm/ADT/Twine.h" 40 using namespace llvm; 41 42 DwarfCFIException::DwarfCFIException(AsmPrinter *A) 43 : DwarfException(A), 44 shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false) 45 {} 46 47 DwarfCFIException::~DwarfCFIException() {} 48 49 /// EndModule - Emit all exception information that should come after the 50 /// content. 51 void DwarfCFIException::EndModule() { 52 if (!Asm->MAI->isExceptionHandlingDwarf()) 53 return; 54 55 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 56 57 unsigned PerEncoding = TLOF.getPersonalityEncoding(); 58 59 if ((PerEncoding & 0x70) != dwarf::DW_EH_PE_pcrel) 60 return; 61 62 // Emit references to all used personality functions 63 bool AtLeastOne = false; 64 const std::vector<const Function*> &Personalities = MMI->getPersonalities(); 65 for (size_t i = 0, e = Personalities.size(); i != e; ++i) { 66 if (!Personalities[i]) 67 continue; 68 MCSymbol *Sym = Asm->Mang->getSymbol(Personalities[i]); 69 TLOF.emitPersonalityValue(Asm->OutStreamer, Asm->TM, Sym); 70 AtLeastOne = true; 71 } 72 73 if (AtLeastOne && !TLOF.isFunctionEHFrameSymbolPrivate()) { 74 // This is a temporary hack to keep sections in the same order they 75 // were before. This lets us produce bit identical outputs while 76 // transitioning to CFI. 77 Asm->OutStreamer.SwitchSection(TLOF.getEHFrameSection()); 78 } 79 } 80 81 /// BeginFunction - Gather pre-function exception information. Assumes it's 82 /// being emitted immediately after the function entry point. 83 void DwarfCFIException::BeginFunction(const MachineFunction *MF) { 84 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false; 85 86 // If any landing pads survive, we need an EH table. 87 bool hasLandingPads = !MMI->getLandingPads().empty(); 88 89 // See if we need frame move info. 90 shouldEmitMoves = Asm->needsCFIMoves(); 91 92 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 93 unsigned PerEncoding = TLOF.getPersonalityEncoding(); 94 const Function *Per = MMI->getPersonalities()[MMI->getPersonalityIndex()]; 95 96 shouldEmitPersonality = hasLandingPads && 97 PerEncoding != dwarf::DW_EH_PE_omit && Per; 98 99 unsigned LSDAEncoding = TLOF.getLSDAEncoding(); 100 shouldEmitLSDA = shouldEmitPersonality && 101 LSDAEncoding != dwarf::DW_EH_PE_omit; 102 103 if (!shouldEmitPersonality && !shouldEmitMoves) 104 return; 105 106 Asm->OutStreamer.EmitCFIStartProc(); 107 108 // Indicate personality routine, if any. 109 if (!shouldEmitPersonality) 110 return; 111 112 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI); 113 Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding); 114 115 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin", 116 Asm->getFunctionNumber())); 117 118 // Provide LSDA information. 119 if (!shouldEmitLSDA) 120 return; 121 122 Asm->OutStreamer.EmitCFILsda(Asm->GetTempSymbol("exception", 123 Asm->getFunctionNumber()), 124 LSDAEncoding); 125 } 126 127 /// EndFunction - Gather and emit post-function exception information. 128 /// 129 void DwarfCFIException::EndFunction() { 130 if (!shouldEmitPersonality && !shouldEmitMoves) 131 return; 132 133 Asm->OutStreamer.EmitCFIEndProc(); 134 135 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end", 136 Asm->getFunctionNumber())); 137 138 // Map all labels and get rid of any dead landing pads. 139 MMI->TidyLandingPads(); 140 141 if (shouldEmitPersonality) 142 EmitExceptionTable(); 143 } 144