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
DwarfCFIExceptionBase(AsmPrinter * A)42 DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
43 : EHStreamer(A), shouldEmitCFI(false) {}
44
markFunctionEnd()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
DwarfCFIException(AsmPrinter * A)56 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
57 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
58 shouldEmitLSDA(false), shouldEmitMoves(false),
59 moveTypeModule(AsmPrinter::CFI_M_None) {}
60
~DwarfCFIException()61 DwarfCFIException::~DwarfCFIException() {}
62
63 /// endModule - Emit all exception information that should come after the
64 /// content.
endModule()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 for (const Function *Personality : MMI->getPersonalities()) {
82 if (!Personality)
83 continue;
84 MCSymbol *Sym = Asm->getSymbol(Personality);
85 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
86 }
87 }
88
beginFunction(const MachineFunction * MF)89 void DwarfCFIException::beginFunction(const MachineFunction *MF) {
90 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
91 const Function *F = MF->getFunction();
92
93 // If any landing pads survive, we need an EH table.
94 bool hasLandingPads = !MMI->getLandingPads().empty();
95
96 // See if we need frame move info.
97 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
98 if (MoveType == AsmPrinter::CFI_M_EH ||
99 (MoveType == AsmPrinter::CFI_M_Debug &&
100 moveTypeModule == AsmPrinter::CFI_M_None))
101 moveTypeModule = MoveType;
102
103 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
104
105 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
106 unsigned PerEncoding = TLOF.getPersonalityEncoding();
107 const Function *Per = nullptr;
108 if (F->hasPersonalityFn())
109 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
110
111 // Emit a personality function even when there are no landing pads
112 bool forceEmitPersonality =
113 // ...if a personality function is explicitly specified
114 F->hasPersonalityFn() &&
115 // ... and it's not known to be a noop in the absence of invokes
116 !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
117 // ... and we're not explicitly asked not to emit it
118 F->needsUnwindTableEntry();
119
120 shouldEmitPersonality =
121 (forceEmitPersonality ||
122 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
123 Per;
124
125 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
126 shouldEmitLSDA = shouldEmitPersonality &&
127 LSDAEncoding != dwarf::DW_EH_PE_omit;
128
129 shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves;
130 if (!shouldEmitCFI)
131 return;
132
133 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
134
135 // Indicate personality routine, if any.
136 if (!shouldEmitPersonality)
137 return;
138
139 // If we are forced to emit this personality, make sure to record
140 // it because it might not appear in any landingpad
141 if (forceEmitPersonality)
142 MMI->addPersonality(Per);
143
144 const MCSymbol *Sym =
145 TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI);
146 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
147
148 // Provide LSDA information.
149 if (!shouldEmitLSDA)
150 return;
151
152 Asm->OutStreamer->EmitCFILsda(Asm->getCurExceptionSym(), LSDAEncoding);
153 }
154
155 /// endFunction - Gather and emit post-function exception information.
156 ///
endFunction(const MachineFunction *)157 void DwarfCFIException::endFunction(const MachineFunction *) {
158 if (!shouldEmitPersonality)
159 return;
160
161 emitExceptionTable();
162 }
163