LLVM  3.7.0
DwarfCFIException.cpp
Go to the documentation of this file.
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"
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"
32 #include "llvm/Support/Dwarf.h"
40 using namespace llvm;
41 
43  : EHStreamer(A), shouldEmitCFI(false) {}
44 
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.
54 }
55 
57  : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
58  shouldEmitLSDA(false), shouldEmitMoves(false),
59  moveTypeModule(AsmPrinter::CFI_M_None) {}
60 
62 
63 /// endModule - Emit all exception information that should come after the
64 /// content.
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 
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->TM, Sym);
87  }
88 }
89 
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.
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 
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
119  // ... and we're not explicitly asked not to emit it
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 ///
160  if (!shouldEmitPersonality)
161  return;
162 
164 }
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:83
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:339
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
bool shouldEmitCFI
Per-function flag to indicate if frame CFI info should be emitted.
F(f)
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
DwarfCFIException(AsmPrinter *A)
MachineModuleInfo * MMI
Collected machine module information.
Definition: EHStreamer.h:39
Emits exception handling directives.
Definition: EHStreamer.h:33
void endFunction(const MachineFunction *) override
Gather and emit post-function exception information.
#define false
Definition: ConvertUTF.c:65
void emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:338
Mangler * Mang
Name-mangler for global names.
Definition: AsmPrinter.h:93
void addPersonality(MachineBasicBlock *LandingPad, const Function *Personality)
addPersonality - Provide the personality function for the exception information.
void beginFunction(const MachineFunction *MF) override
Gather pre-function exception information.
Constant * stripPointerCasts()
Definition: Constant.h:170
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:74
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:70
bool hasPersonalityFn() const
Get the personality function associated with this function.
Definition: Function.h:132
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
const std::vector< const Function * > & getPersonalities() const
getPersonalities - Return array of personality functions ever seen.
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:36
unsigned getLSDAEncoding() const
Module.h This file contains the declarations for the Module class.
bool needsUnwindTableEntry() const
True if this function needs an unwind table.
Definition: Function.h:354
unsigned getPersonalityEncoding() const
virtual void emitPersonalityValue(MCStreamer &Streamer, const TargetMachine &TM, const MCSymbol *Sym) const
MCSymbol * getCurExceptionSym()
void endModule() override
Emit all exception information that should come after the content.
Constant * getPersonalityFn() const
Definition: Function.h:133
const std::vector< LandingPadInfo > & getLandingPads() const
getLandingPads - Return a reference to the landing pad info for the current function.
CFIMoveType needsCFIMoves()
Definition: AsmPrinter.cpp:741
void TidyLandingPads(DenseMap< MCSymbol *, uintptr_t > *LPMap=nullptr)
TidyLandingPads - Remap landing pad labels and remove any deleted landing pads.
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:134
virtual MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, Mangler &Mang, const TargetMachine &TM, MachineModuleInfo *MMI) const
bool usesCFIForEH() const
Returns true if the exception handling method for the platform uses call frame information to unwind...
Definition: MCAsmInfo.h:516
bool isNoOpWithoutInvoke(EHPersonality Pers)
Return true if this personality may be safely removed if there are no invoke instructions remaining i...
const Function * getPersonality() const
getPersonality - Return a personality function if available.