LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/Module.h"
24 #include "llvm/MC/MCAsmInfo.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCStreamer.h"
29 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/Support/Dwarf.h"
34 #include "llvm/Target/Mangler.h"
40 using namespace llvm;
41 
43  : DwarfException(A),
44  shouldEmitPersonality(false), shouldEmitLSDA(false), shouldEmitMoves(false),
45  moveTypeModule(AsmPrinter::CFI_M_None) {}
46 
48 
49 /// EndModule - Emit all exception information that should come after the
50 /// content.
52  if (moveTypeModule == AsmPrinter::CFI_M_Debug)
53  Asm->OutStreamer.EmitCFISections(false, true);
54 
56  return;
57 
59 
60  unsigned PerEncoding = TLOF.getPersonalityEncoding();
61 
62  if ((PerEncoding & 0x70) != dwarf::DW_EH_PE_pcrel)
63  return;
64 
65  // Emit references to all used personality functions
66  bool AtLeastOne = false;
67  const std::vector<const Function*> &Personalities = MMI->getPersonalities();
68  for (size_t i = 0, e = Personalities.size(); i != e; ++i) {
69  if (!Personalities[i])
70  continue;
71  MCSymbol *Sym = Asm->getSymbol(Personalities[i]);
73  AtLeastOne = true;
74  }
75 
76  if (AtLeastOne && !TLOF.isFunctionEHFrameSymbolPrivate()) {
77  // This is a temporary hack to keep sections in the same order they
78  // were before. This lets us produce bit identical outputs while
79  // transitioning to CFI.
81  const_cast<TargetLoweringObjectFile&>(TLOF).getEHFrameSection());
82  }
83 }
84 
85 /// BeginFunction - Gather pre-function exception information. Assumes it's
86 /// being emitted immediately after the function entry point.
88  shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
89 
90  // If any landing pads survive, we need an EH table.
91  bool hasLandingPads = !MMI->getLandingPads().empty();
92 
93  // See if we need frame move info.
95  if (MoveType == AsmPrinter::CFI_M_EH ||
96  (MoveType == AsmPrinter::CFI_M_Debug &&
97  moveTypeModule == AsmPrinter::CFI_M_None))
98  moveTypeModule = MoveType;
99 
100  shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
101 
103  unsigned PerEncoding = TLOF.getPersonalityEncoding();
105 
106  shouldEmitPersonality = hasLandingPads &&
107  PerEncoding != dwarf::DW_EH_PE_omit && Per;
108 
109  unsigned LSDAEncoding = TLOF.getLSDAEncoding();
110  shouldEmitLSDA = shouldEmitPersonality &&
111  LSDAEncoding != dwarf::DW_EH_PE_omit;
112 
113  if (!shouldEmitPersonality && !shouldEmitMoves)
114  return;
115 
117 
118  // Indicate personality routine, if any.
119  if (!shouldEmitPersonality)
120  return;
121 
122  const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(Per, Asm->Mang, MMI);
123  Asm->OutStreamer.EmitCFIPersonality(Sym, PerEncoding);
124 
126  (Asm->GetTempSymbol("eh_func_begin",
127  Asm->getFunctionNumber()));
128 
129  // Provide LSDA information.
130  if (!shouldEmitLSDA)
131  return;
132 
135  LSDAEncoding);
136 }
137 
138 /// EndFunction - Gather and emit post-function exception information.
139 ///
141  if (!shouldEmitPersonality && !shouldEmitMoves)
142  return;
143 
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 
156 }
bool isExceptionHandlingDwarf() const
Definition: MCAsmInfo.h:522
virtual void EmitCFIPersonality(const MCSymbol *Sym, unsigned Encoding)
Definition: MCStreamer.cpp:334
MachineModuleInfo * MMI
MMI - Collected machine module information.
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:277
unsigned getFunctionNumber() const
Definition: AsmPrinter.cpp:125
DwarfCFIException(AsmPrinter *A)
void EmitCFIStartProc()
Definition: MCStreamer.cpp:238
virtual void EmitCFISections(bool EH, bool Debug)
Definition: MCStreamer.cpp:232
virtual void EmitCFILsda(const MCSymbol *Sym, unsigned Encoding)
Definition: MCStreamer.cpp:342
void TidyLandingPads(DenseMap< MCSymbol *, uintptr_t > *LPMap=0)
#define false
Definition: ConvertUTF.c:64
void SwitchSection(const MCSection *Section, const MCExpr *Subsection=0)
Definition: MCStreamer.h:284
Mangler * Mang
Definition: AsmPrinter.h:88
MCStreamer & OutStreamer
Definition: AsmPrinter.h:78
AsmPrinter * Asm
Asm - Target of Dwarf emission.
bool isFunctionEHFrameSymbolPrivate() const
const MCAsmInfo * MAI
Definition: AsmPrinter.h:66
TargetMachine & TM
Definition: AsmPrinter.h:62
const std::vector< const Function * > & getPersonalities() const
getPersonalities - Return array of personality functions ever seen.
virtual void EndFunction()
EndFunction - Gather and emit post-function exception information.
virtual void EmitLabel(MCSymbol *Symbol)
Definition: MCStreamer.cpp:212
unsigned getLSDAEncoding() const
virtual void EmitDebugLabel(MCSymbol *Symbol)
Definition: MCStreamer.cpp:219
unsigned getPersonalityEncoding() const
virtual void emitPersonalityValue(MCStreamer &Streamer, const TargetMachine &TM, const MCSymbol *Sym) const
const std::vector< LandingPadInfo > & getLandingPads() const
unsigned getPersonalityIndex() const
CFIMoveType needsCFIMoves()
Definition: AsmPrinter.cpp:644
const TargetLoweringObjectFile & getObjFileLowering() const
getObjFileLowering - Return information about object file lowering.
Definition: AsmPrinter.cpp:129
virtual void BeginFunction(const MachineFunction *MF)
virtual MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang, MachineModuleInfo *MMI) const
MCSymbol * GetTempSymbol(StringRef Name, unsigned ID) const