LLVM  15.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"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCStreamer.h"
24 using namespace llvm;
25 
27 
29  endFragment();
30 
31  // Map all labels and get rid of any dead landing pads.
32  if (!Asm->MF->getLandingPads().empty()) {
33  MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
34  NonConstMF->tidyLandingPads();
35  }
36 }
37 
39  if (shouldEmitCFI && !Asm->MF->hasBBSections())
40  Asm->OutStreamer->emitCFIEndProc();
41 }
42 
44  : DwarfCFIExceptionBase(A) {}
45 
47 
48 /// endModule - Emit all exception information that should come after the
49 /// content.
51  // SjLj uses this pass and it doesn't need this info.
52  if (!Asm->MAI->usesCFIForEH())
53  return;
54 
56 
57  unsigned PerEncoding = TLOF.getPersonalityEncoding();
58 
59  if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
60  return;
61 
62  // Emit references to all used personality functions
63  for (const Function *Personality : MMI->getPersonalities()) {
64  if (!Personality)
65  continue;
66  MCSymbol *Sym = Asm->getSymbol(Personality);
68  }
69 }
70 
72  const MachineBasicBlock *MBB) {
73  return Asm->getMBBExceptionSym(*MBB);
74 }
75 
77  shouldEmitPersonality = shouldEmitLSDA = false;
78  const Function &F = MF->getFunction();
79 
80  // If any landing pads survive, we need an EH table.
81  bool hasLandingPads = !MF->getLandingPads().empty();
82 
83  // See if we need frame move info.
84  bool shouldEmitMoves =
86 
88  unsigned PerEncoding = TLOF.getPersonalityEncoding();
89  const Function *Per = nullptr;
90  if (F.hasPersonalityFn())
91  Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
92 
93  // Emit a personality function even when there are no landing pads
94  forceEmitPersonality =
95  // ...if a personality function is explicitly specified
96  F.hasPersonalityFn() &&
97  // ... and it's not known to be a noop in the absence of invokes
99  // ... and we're not explicitly asked not to emit it
100  F.needsUnwindTableEntry();
101 
102  shouldEmitPersonality =
103  (forceEmitPersonality ||
104  (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
105  Per;
106 
107  unsigned LSDAEncoding = TLOF.getLSDAEncoding();
108  shouldEmitLSDA = shouldEmitPersonality &&
109  LSDAEncoding != dwarf::DW_EH_PE_omit;
110 
111  const MCAsmInfo &MAI = *MF->getMMI().getContext().getAsmInfo();
113  shouldEmitCFI =
114  MAI.usesCFIForEH() && (shouldEmitPersonality || shouldEmitMoves);
115  else
116  shouldEmitCFI = Asm->needsCFIForDebug() && shouldEmitMoves;
117 
119 }
120 
123  if (!shouldEmitCFI)
124  return;
125 
126  if (!hasEmittedCFISections) {
128  // If we don't say anything it implies `.cfi_sections .eh_frame`, so we
129  // chose not to be verbose in that case. And with `ForceDwarfFrameSection`,
130  // we should always emit .debug_frame.
131  if (CFISecType == AsmPrinter::CFISection::Debug ||
133  Asm->OutStreamer->emitCFISections(
134  CFISecType == AsmPrinter::CFISection::EH, true);
135  hasEmittedCFISections = true;
136  }
137 
138  Asm->OutStreamer->emitCFIStartProc(/*IsSimple=*/false);
139 
140  // Indicate personality routine, if any.
141  if (!shouldEmitPersonality)
142  return;
143 
144  auto &F = MBB->getParent()->getFunction();
145  auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
146  assert(P && "Expected personality function");
147 
148  // If we are forced to emit this personality, make sure to record
149  // it because it might not appear in any landingpad
150  if (forceEmitPersonality)
151  MMI->addPersonality(P);
152 
154  unsigned PerEncoding = TLOF.getPersonalityEncoding();
155  const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
156  Asm->OutStreamer->emitCFIPersonality(Sym, PerEncoding);
157 
158  // Provide LSDA information.
159  if (shouldEmitLSDA)
160  Asm->OutStreamer->emitCFILsda(ESP(Asm, MBB), TLOF.getLSDAEncoding());
161 }
162 
163 /// endFunction - Gather and emit post-function exception information.
164 ///
166  if (!shouldEmitPersonality)
167  return;
168 
170 }
171 
174 }
175 
177  if (shouldEmitCFI)
178  Asm->OutStreamer->emitCFIEndProc();
179 }
AsmPrinter.h
getExceptionSym
static MCSymbol * getExceptionSym(AsmPrinter *Asm, const MachineBasicBlock *MBB)
Definition: DwarfCFIException.cpp:71
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
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:160
llvm::DwarfCFIExceptionBase::markFunctionEnd
void markFunctionEnd() override
Definition: DwarfCFIException.cpp:28
llvm::DwarfCFIException::~DwarfCFIException
~DwarfCFIException() override
llvm::MachineModuleInfo::getContext
const MCContext & getContext() const
Definition: MachineModuleInfo.h:144
llvm::Function
Definition: Function.h:60
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:176
llvm::AsmPrinter::MAI
const MCAsmInfo * MAI
Target Asm Printer information.
Definition: AsmPrinter.h:87
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:43
llvm::DwarfCFIException::beginFragment
void beginFragment(const MachineBasicBlock *MBB, ExceptionSymbolProvider ESP) override
Definition: DwarfCFIException.cpp:121
llvm::AsmPrinter::getFunctionCFISectionType
CFISection getFunctionCFISectionType(const Function &F) const
Get the CFISection type for a function.
Definition: AsmPrinter.cpp:1221
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:788
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::TargetLoweringObjectFile
Definition: TargetLoweringObjectFile.h:45
llvm::classifyEHPersonality
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Definition: EHPersonalities.cpp:21
TargetMachine.h
llvm::dwarf::DW_EH_PE_indirect
@ DW_EH_PE_indirect
Definition: Dwarf.h:453
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:96
llvm::TargetOptions::ForceDwarfFrameSection
unsigned ForceDwarfFrameSection
Emit DWARF debug frame section.
Definition: TargetOptions.h:342
MCContext.h
llvm::EHStreamer::emitExceptionTable
MCSymbol * emitExceptionTable()
Emit landing pads and actions.
Definition: EHStreamer.cpp:375
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:95
llvm::AsmPrinter::getModuleCFISectionType
CFISection getModuleCFISectionType() const
Get the CFISection type for the module.
Definition: AsmPrinter.h:414
llvm::MachineFunction::begin
iterator begin()
Definition: MachineFunction.h:822
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:810
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
llvm::TargetLoweringObjectFile::emitPersonalityValue
virtual void emitPersonalityValue(MCStreamer &Streamer, const DataLayout &TM, const MCSymbol *Sym) const
Definition: TargetLoweringObjectFile.cpp:144
llvm::AsmPrinter::CFISection::None
@ None
Do not emit either .eh_frame or .debug_frame.
llvm::DwarfCFIExceptionBase::endFragment
void endFragment() override
Definition: DwarfCFIException.cpp:38
llvm::MachineFunction::getMMI
MachineModuleInfo & getMMI() const
Definition: MachineFunction.h:591
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:203
llvm::DwarfCFIException::endModule
void endModule() override
Emit all exception information that should come after the content.
Definition: DwarfCFIException.cpp:50
llvm::DwarfCFIException::beginBasicBlock
void beginBasicBlock(const MachineBasicBlock &MBB) override
Process beginning of a basic block during basic block sections.
Definition: DwarfCFIException.cpp:172
llvm::DwarfCFIException::beginFunction
void beginFunction(const MachineFunction *MF) override
Gather pre-function exception information.
Definition: DwarfCFIException.cpp:76
llvm::AsmPrinter::needsCFIForDebug
bool needsCFIForDebug() const
Since emitting CFI unwind information is entangled with supporting the exceptions,...
Definition: AsmPrinter.cpp:1245
llvm::MCContext::getAsmInfo
const MCAsmInfo * getAsmInfo() const
Definition: MCContext.h:439
llvm::TargetLoweringObjectFile::getPersonalityEncoding
unsigned getPersonalityEncoding() const
Definition: TargetLoweringObjectFile.h:168
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:118
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::AsmPrinter::getSymbol
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:654
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition: MachineBasicBlock.h:234
llvm::AsmPrinter::MF
MachineFunction * MF
The current machine function.
Definition: AsmPrinter.h:99
llvm::MachineFunction::getLandingPads
const std::vector< LandingPadInfo > & getLandingPads() const
Return a reference to the landing pad info for the current function.
Definition: MachineFunction.h:1078
MachineModuleInfo.h
llvm::MachineFunction
Definition: MachineFunction.h:241
TargetOptions.h
MCAsmInfo.h
llvm::EHStreamer::Asm
AsmPrinter * Asm
Target of directive emission.
Definition: EHStreamer.h:33
llvm::TargetLoweringObjectFile::getLSDAEncoding
unsigned getLSDAEncoding() const
Definition: TargetLoweringObjectFile.h:169
Dwarf.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:618
llvm::N86::ESP
@ ESP
Definition: X86MCTargetDesc.h:51
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:606
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:81
llvm::DwarfCFIException::endFunction
void endFunction(const MachineFunction *) override
Gather and emit post-function exception information.
Definition: DwarfCFIException.cpp:165
llvm::ExceptionHandling::None
@ None
No exception support.
llvm::MCAsmInfo::getExceptionHandlingType
ExceptionHandling getExceptionHandlingType() const
Definition: MCAsmInfo.h:777
llvm::AsmPrinter::TM
TargetMachine & TM
Target machine description.
Definition: AsmPrinter.h:84
llvm::MachineModuleInfo::addPersonality
void addPersonality(const Function *Personality)
Provide the personality function for the exception information.
Definition: MachineModuleInfo.cpp:95
llvm::AsmPrinter::getObjFileLowering
const TargetLoweringObjectFile & getObjFileLowering() const
Return information about object file lowering.
Definition: AsmPrinter.cpp:382
MCStreamer.h
DwarfException.h
llvm::dwarf::DW_EH_PE_omit
@ DW_EH_PE_omit
Definition: Dwarf.h:438
llvm::TargetLoweringObjectFile::getCFIPersonalitySymbol
virtual MCSymbol * getCFIPersonalitySymbol(const GlobalValue *GV, const TargetMachine &TM, MachineModuleInfo *MMI) const
Definition: TargetLoweringObjectFile.cpp:138
llvm::AsmPrinter::getDataLayout
const DataLayout & getDataLayout() const
Return information about data layout.
Definition: AsmPrinter.cpp:386
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::DwarfCFIExceptionBase::DwarfCFIExceptionBase
DwarfCFIExceptionBase(AsmPrinter *A)
Definition: DwarfCFIException.cpp:26
MachineFunction.h
llvm::AsmPrinter::CFISection::EH
@ EH
Emit .eh_frame.