LLVM  14.0.0git
DwarfCFIException.cpp
Go to the documentation of this file.
1 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing DWARF exception info into asm files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "DwarfException.h"
14 #include "llvm/ADT/Twine.h"
19 #include "llvm/IR/DataLayout.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCAsmInfo.h"
23 #include "llvm/MC/MCContext.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCSection.h"
26 #include "llvm/MC/MCStreamer.h"
27 #include "llvm/MC/MCSymbol.h"
34 using namespace llvm;
35 
37  : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
38 
40  endFragment();
41 
42  // Map all labels and get rid of any dead landing pads.
43  if (!Asm->MF->getLandingPads().empty()) {
44  MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
45  NonConstMF->tidyLandingPads();
46  }
47 }
48 
50  if (shouldEmitCFI && !Asm->MF->hasBBSections())
51  Asm->OutStreamer->emitCFIEndProc();
52 }
53 
55  : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
56  forceEmitPersonality(false), shouldEmitLSDA(false) {}
57 
59 
60 /// endModule - Emit all exception information that should come after the
61 /// content.
63  // SjLj uses this pass and it doesn't need this info.
64  if (!Asm->MAI->usesCFIForEH())
65  return;
66 
68 
69  unsigned PerEncoding = TLOF.getPersonalityEncoding();
70 
71  if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
72  return;
73 
74  // Emit references to all used personality functions
75  for (const Function *Personality : MMI->getPersonalities()) {
76  if (!Personality)
77  continue;
78  MCSymbol *Sym = Asm->getSymbol(Personality);
80  }
81 }
82 
84  const MachineBasicBlock *MBB) {
85  return Asm->getMBBExceptionSym(*MBB);
86 }
87 
89  shouldEmitPersonality = shouldEmitLSDA = false;
90  const Function &F = MF->getFunction();
91 
92  // If any landing pads survive, we need an EH table.
93  bool hasLandingPads = !MF->getLandingPads().empty();
94 
95  // See if we need frame move info.
96  bool shouldEmitMoves =
98 
100  unsigned PerEncoding = TLOF.getPersonalityEncoding();
101  const Function *Per = nullptr;
102  if (F.hasPersonalityFn())
103  Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
104 
105  // Emit a personality function even when there are no landing pads
106  forceEmitPersonality =
107  // ...if a personality function is explicitly specified
108  F.hasPersonalityFn() &&
109  // ... and it's not known to be a noop in the absence of invokes
111  // ... and we're not explicitly asked not to emit it
112  F.needsUnwindTableEntry();
113 
114  shouldEmitPersonality =
115  (forceEmitPersonality ||
116  (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
117  Per;
118 
119  unsigned LSDAEncoding = TLOF.getLSDAEncoding();
120  shouldEmitLSDA = shouldEmitPersonality &&
121  LSDAEncoding != dwarf::DW_EH_PE_omit;
122 
123  const MCAsmInfo &MAI = *MF->getMMI().getContext().getAsmInfo();
125  shouldEmitCFI =
126  MAI.usesCFIForEH() && (shouldEmitPersonality || shouldEmitMoves);
127  else
128  shouldEmitCFI = Asm->needsCFIForDebug() && shouldEmitMoves;
129 
131 }
132 
135  if (!shouldEmitCFI)
136  return;
137 
138  if (!hasEmittedCFISections) {
140  // If we don't say anything it implies `.cfi_sections .eh_frame`, so we
141  // chose not to be verbose in that case. And with `ForceDwarfFrameSection`,
142  // we should always emit .debug_frame.
143  if (CFISecType == AsmPrinter::CFISection::Debug ||
145  Asm->OutStreamer->emitCFISections(
146  CFISecType == AsmPrinter::CFISection::EH, true);
147  hasEmittedCFISections = true;
148  }
149 
150  Asm->OutStreamer->emitCFIStartProc(/*IsSimple=*/false);
151 
152  // Indicate personality routine, if any.
153  if (!shouldEmitPersonality)
154  return;
155 
156  auto &F = MBB->getParent()->getFunction();
157  auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
158  assert(P && "Expected personality function");
159 
160  // If we are forced to emit this personality, make sure to record
161  // it because it might not appear in any landingpad
162  if (forceEmitPersonality)
163  MMI->addPersonality(P);
164 
166  unsigned PerEncoding = TLOF.getPersonalityEncoding();
167  const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
168  Asm->OutStreamer->emitCFIPersonality(Sym, PerEncoding);
169 
170  // Provide LSDA information.
171  if (shouldEmitLSDA)
172  Asm->OutStreamer->emitCFILsda(ESP(Asm, MBB), TLOF.getLSDAEncoding());
173 }
174 
175 /// endFunction - Gather and emit post-function exception information.
176 ///
178  if (!shouldEmitPersonality)
179  return;
180 
182 }
183 
186 }
187 
189  if (shouldEmitCFI)
190  Asm->OutStreamer->emitCFIEndProc();
191 }
AsmPrinter.h
getExceptionSym
static MCSymbol * getExceptionSym(AsmPrinter *Asm, const MachineBasicBlock *MBB)
Definition: DwarfCFIException.cpp:83
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::AsmPrinter::CFISection
CFISection
Definition: AsmPrinter.h:161
llvm::DwarfCFIExceptionBase::markFunctionEnd
void markFunctionEnd() override
Definition: DwarfCFIException.cpp:39
llvm::DwarfCFIException::~DwarfCFIException
~DwarfCFIException() override
Definition: DwarfCFIException.cpp:58
llvm::MachineModuleInfo::getContext
const MCContext & getContext() const
Definition: MachineModuleInfo.h:167
llvm::Function
Definition: Function.h:61
llvm::EHStreamer
Emits exception handling directives.
Definition: EHStreamer.h:30
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
llvm::DwarfCFIException::endBasicBlock
void endBasicBlock(const MachineBasicBlock &MBB) override
Process end of a basic block during basic block sections.
Definition: DwarfCFIException.cpp:188
llvm::AsmPrinter::MAI
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:88
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::AsmPrinter::CFISection::Debug
@ Debug
Emit .debug_frame.
llvm::DwarfCFIException::DwarfCFIException
DwarfCFIException(AsmPrinter *A)
Definition: DwarfCFIException.cpp:54
ErrorHandling.h
llvm::DwarfCFIException::beginFragment
void beginFragment(const MachineBasicBlock *MBB, ExceptionSymbolProvider ESP) override
Definition: DwarfCFIException.cpp:133
Module.h
llvm::AsmPrinter::getFunctionCFISectionType
CFISection getFunctionCFISectionType(const Function &F) const
Get the CFISection type for a function.
Definition: AsmPrinter.cpp:1054
llvm::EHStreamer::MMI
MachineModuleInfo * MMI
Collected machine module information.
Definition: EHStreamer.h:36
llvm::MCAsmInfo::usesCFIForEH
bool usesCFIForEH() const
Returns true if the exception handling method for the platform uses call frame information to unwind.
Definition: MCAsmInfo.h:773
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::TargetLoweringObjectFile
Definition: TargetLoweringObjectFile.h:43
llvm::classifyEHPersonality
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Definition: EHPersonalities.cpp:21
FormattedStream.h
TargetMachine.h
llvm::dwarf::DW_EH_PE_indirect
@ DW_EH_PE_indirect
Definition: Dwarf.h:448
llvm::N86::ESP
@ ESP
Definition: X86MCTargetDesc.h:51
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:97
llvm::TargetOptions::ForceDwarfFrameSection
unsigned ForceDwarfFrameSection
Emit DWARF debug frame section.
Definition: TargetOptions.h:330
Twine.h
MCContext.h
llvm::EHStreamer::emitExceptionTable
MCSymbol * emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:378
MCSymbol.h
false
Definition: StackSlotColoring.cpp:142
llvm::isNoOpWithoutInvoke
bool isNoOpWithoutInvoke(EHPersonality Pers)
Return true if this personality may be safely removed if there are no invoke instructions remaining i...
Definition: EHPersonalities.h:96
llvm::AsmPrinter::getModuleCFISectionType
CFISection getModuleCFISectionType() const
Get the CFISection type for the module.
Definition: AsmPrinter.h:379
llvm::MachineFunction::begin
iterator begin()
Definition: MachineFunction.h:808
llvm::MachineFunction::tidyLandingPads
void tidyLandingPads(DenseMap< MCSymbol *, uintptr_t > *LPMap=nullptr, bool TidyIfNoBeginLabels=true)
Remap landing pad labels and remove any deleted landing pads.
Definition: MachineFunction.cpp:785
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::TargetLoweringObjectFile::emitPersonalityValue
virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, const MCSymbol *Sym) const
Definition: TargetLoweringObjectFile.cpp:146
llvm::AsmPrinter::CFISection::None
@ None
Do not emit either .eh_frame or .debug_frame.
llvm::DwarfCFIExceptionBase::endFragment
void endFragment() override
Definition: DwarfCFIException.cpp:49
llvm::MachineFunction::getMMI
MachineModuleInfo & getMMI() const
Definition: MachineFunction.h:577
llvm::DwarfCFIExceptionBase
Definition: DwarfException.h:24
llvm::MachineModuleInfo::getPersonalities
const std::vector< const Function * > & getPersonalities() const
Return array of personality functions ever seen.
Definition: MachineModuleInfo.h:267
llvm::DwarfCFIException::endModule
void endModule() override
Emit all exception information that should come after the content.
Definition: DwarfCFIException.cpp:62
llvm::DwarfCFIException::beginBasicBlock
void beginBasicBlock(const MachineBasicBlock &MBB) override
Process beginning of a basic block during basic block sections.
Definition: DwarfCFIException.cpp:184
llvm::DwarfCFIException::beginFunction
void beginFunction(const MachineFunction *MF) override
Gather pre-function exception information.
Definition: DwarfCFIException.cpp:88
llvm::AsmPrinter::needsCFIForDebug
bool needsCFIForDebug() const
Since emitting CFI unwind information is entangled with supporting the exceptions,...
Definition: AsmPrinter.cpp:1078
llvm::MCContext::getAsmInfo
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:423
llvm::TargetLoweringObjectFile::getPersonalityEncoding
unsigned getPersonalityEncoding() const
Definition: TargetLoweringObjectFile.h:166
llvm::DwarfCFIExceptionBase::hasEmittedCFISections
bool hasEmittedCFISections
Per-module flag to indicate if .cfi_section has beeen emitted.
Definition: DwarfException.h:31
llvm::TargetMachine::Options
TargetOptions Options
Definition: TargetMachine.h:120
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AsmPrinter::getSymbol
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:486
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition: MachineBasicBlock.h:225
llvm::AsmPrinter::MF
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:100
llvm::MachineFunction::getLandingPads
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
Definition: MachineFunction.h:1061
MachineModuleInfo.h
MCSection.h
llvm::MachineFunction
Definition: MachineFunction.h:230
Mangler.h
TargetOptions.h
MCAsmInfo.h
llvm::EHStreamer::Asm
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:33
DataLayout.h
llvm::TargetLoweringObjectFile::getLSDAEncoding
unsigned getLSDAEncoding() const
Definition: TargetLoweringObjectFile.h:167
Dwarf.h
MachineLocation.h
TargetLoweringObjectFile.h
llvm::ExceptionSymbolProvider
MCSymbol * ExceptionSymbolProvider(AsmPrinter *Asm, const MachineBasicBlock *MBB)
Definition: AsmPrinterHandler.h:28
llvm::MachineFunction::hasBBSections
bool hasBBSections() const
Returns true if this function has basic block sections enabled.
Definition: MachineFunction.h:604
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:592
llvm::DwarfCFIExceptionBase::shouldEmitCFI
bool shouldEmitCFI
Per-function flag to indicate if frame CFI info should be emitted.
Definition: DwarfException.h:29
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::DwarfCFIException::endFunction
void endFunction(const MachineFunction *) override
Gather and emit post-function exception information.
Definition: DwarfCFIException.cpp:177
llvm::ExceptionHandling::None
@ None
No exception support.
llvm::MCAsmInfo::getExceptionHandlingType
ExceptionHandling getExceptionHandlingType() const
Definition: MCAsmInfo.h:762
llvm::AsmPrinter::TM
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:85
llvm::MachineModuleInfo::addPersonality
void addPersonality(const Function *Personality)
Provide the personality function for the exception information.
Definition: MachineModuleInfo.cpp:278
llvm::AsmPrinter::getObjFileLowering
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:224
MCStreamer.h
DwarfException.h
llvm::dwarf::DW_EH_PE_omit
@ DW_EH_PE_omit
Definition: Dwarf.h:433
llvm::TargetLoweringObjectFile::getCFIPersonalitySymbol
virtual MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const
Definition: TargetLoweringObjectFile.cpp:140
llvm::AsmPrinter::getDataLayout
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:228
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::DwarfCFIExceptionBase::DwarfCFIExceptionBase
DwarfCFIExceptionBase(AsmPrinter *A)
Definition: DwarfCFIException.cpp:36
MachineFunction.h
MCExpr.h
llvm::AsmPrinter::CFISection::EH
@ EH
Emit .eh_frame.