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   endFragment();
47 
48   if (MMI->getLandingPads().empty())
49     return;
50 
51   // Map all labels and get rid of any dead landing pads.
52   MMI->TidyLandingPads();
53 }
54 
55 void DwarfCFIExceptionBase::endFragment() {
56   if (shouldEmitCFI)
57     Asm->OutStreamer->EmitCFIEndProc();
58 }
59 
60 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
61     : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
62       forceEmitPersonality(false), shouldEmitLSDA(false),
63       shouldEmitMoves(false), moveTypeModule(AsmPrinter::CFI_M_None) {}
64 
65 DwarfCFIException::~DwarfCFIException() {}
66 
67 /// endModule - Emit all exception information that should come after the
68 /// content.
69 void DwarfCFIException::endModule() {
70   if (moveTypeModule == AsmPrinter::CFI_M_Debug)
71     Asm->OutStreamer->EmitCFISections(false, true);
72 
73   // SjLj uses this pass and it doesn't need this info.
74   if (!Asm->MAI->usesCFIForEH())
75     return;
76 
77   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
78 
79   unsigned PerEncoding = TLOF.getPersonalityEncoding();
80 
81   if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
82     return;
83 
84   // Emit references to all used personality functions
85   for (const Function *Personality : MMI->getPersonalities()) {
86     if (!Personality)
87       continue;
88     MCSymbol *Sym = Asm->getSymbol(Personality);
89     TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
90   }
91 }
92 
93 static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
94   return Asm->getCurExceptionSym();
95 }
96 
97 void DwarfCFIException::beginFunction(const MachineFunction *MF) {
98   shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
99   const Function *F = MF->getFunction();
100 
101   // If any landing pads survive, we need an EH table.
102   bool hasLandingPads = !MMI->getLandingPads().empty();
103 
104   // See if we need frame move info.
105   AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
106   if (MoveType == AsmPrinter::CFI_M_EH ||
107       (MoveType == AsmPrinter::CFI_M_Debug &&
108        moveTypeModule == AsmPrinter::CFI_M_None))
109     moveTypeModule = MoveType;
110 
111   shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
112 
113   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
114   unsigned PerEncoding = TLOF.getPersonalityEncoding();
115   const Function *Per = nullptr;
116   if (F->hasPersonalityFn())
117     Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
118 
119   // Emit a personality function even when there are no landing pads
120   forceEmitPersonality =
121       // ...if a personality function is explicitly specified
122       F->hasPersonalityFn() &&
123       // ... and it's not known to be a noop in the absence of invokes
124       !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
125       // ... and we're not explicitly asked not to emit it
126       F->needsUnwindTableEntry();
127 
128   shouldEmitPersonality =
129       (forceEmitPersonality ||
130        (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
131       Per;
132 
133   unsigned LSDAEncoding = TLOF.getLSDAEncoding();
134   shouldEmitLSDA = shouldEmitPersonality &&
135     LSDAEncoding != dwarf::DW_EH_PE_omit;
136 
137   shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves;
138   beginFragment(&*MF->begin(), getExceptionSym);
139 }
140 
141 void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
142                                       ExceptionSymbolProvider ESP) {
143   if (!shouldEmitCFI)
144     return;
145 
146   Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
147 
148   // Indicate personality routine, if any.
149   if (!shouldEmitPersonality)
150     return;
151 
152   auto *F = MBB->getParent()->getFunction();
153   auto *P = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
154   assert(P && "Expected personality function");
155 
156   // If we are forced to emit this personality, make sure to record
157   // it because it might not appear in any landingpad
158   if (forceEmitPersonality)
159     MMI->addPersonality(P);
160 
161   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
162   unsigned PerEncoding = TLOF.getPersonalityEncoding();
163   const MCSymbol *Sym =
164       TLOF.getCFIPersonalitySymbol(P, *Asm->Mang, Asm->TM, MMI);
165   Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
166 
167   // Provide LSDA information.
168   if (shouldEmitLSDA)
169     Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
170 }
171 
172 /// endFunction - Gather and emit post-function exception information.
173 ///
174 void DwarfCFIException::endFunction(const MachineFunction *) {
175   if (!shouldEmitPersonality)
176     return;
177 
178   emitExceptionTable();
179 }
180