1 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains support for writing DWARF exception info into asm files.
11 //===----------------------------------------------------------------------===//
13 #include "DwarfException.h"
14 #include "llvm/BinaryFormat/Dwarf.h"
15 #include "llvm/CodeGen/AsmPrinter.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineModuleInfo.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCContext.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/Target/TargetLoweringObjectFile.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Target/TargetOptions.h"
27 DwarfCFIException::DwarfCFIException(AsmPrinter
*A
) : EHStreamer(A
) {}
29 DwarfCFIException::~DwarfCFIException() = default;
31 void DwarfCFIException::addPersonality(const GlobalValue
*Personality
) {
32 if (!llvm::is_contained(Personalities
, Personality
))
33 Personalities
.push_back(Personality
);
36 /// endModule - Emit all exception information that should come after the
38 void DwarfCFIException::endModule() {
39 // SjLj uses this pass and it doesn't need this info.
40 if (!Asm
->MAI
->usesCFIForEH())
43 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
45 unsigned PerEncoding
= TLOF
.getPersonalityEncoding();
47 if ((PerEncoding
& 0x80) != dwarf::DW_EH_PE_indirect
)
50 // Emit indirect reference table for all used personality functions
51 for (const GlobalValue
*Personality
: Personalities
) {
52 MCSymbol
*Sym
= Asm
->getSymbol(Personality
);
53 TLOF
.emitPersonalityValue(*Asm
->OutStreamer
, Asm
->getDataLayout(), Sym
);
55 Personalities
.clear();
58 void DwarfCFIException::beginFunction(const MachineFunction
*MF
) {
59 shouldEmitPersonality
= shouldEmitLSDA
= false;
60 const Function
&F
= MF
->getFunction();
62 // If any landing pads survive, we need an EH table.
63 bool hasLandingPads
= !MF
->getLandingPads().empty();
65 // See if we need frame move info.
66 bool shouldEmitMoves
=
67 Asm
->getFunctionCFISectionType(*MF
) != AsmPrinter::CFISection::None
;
69 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
70 unsigned PerEncoding
= TLOF
.getPersonalityEncoding();
71 const GlobalValue
*Per
= nullptr;
72 if (F
.hasPersonalityFn())
73 Per
= dyn_cast
<GlobalValue
>(F
.getPersonalityFn()->stripPointerCasts());
75 // Emit a personality function even when there are no landing pads
76 forceEmitPersonality
=
77 // ...if a personality function is explicitly specified
78 F
.hasPersonalityFn() &&
79 // ... and it's not known to be a noop in the absence of invokes
80 !isNoOpWithoutInvoke(classifyEHPersonality(Per
)) &&
81 // ... and we're not explicitly asked not to emit it
82 F
.needsUnwindTableEntry();
84 shouldEmitPersonality
=
85 (forceEmitPersonality
||
86 (hasLandingPads
&& PerEncoding
!= dwarf::DW_EH_PE_omit
)) &&
89 unsigned LSDAEncoding
= TLOF
.getLSDAEncoding();
90 shouldEmitLSDA
= shouldEmitPersonality
&&
91 LSDAEncoding
!= dwarf::DW_EH_PE_omit
;
93 const MCAsmInfo
&MAI
= *MF
->getContext().getAsmInfo();
94 if (MAI
.getExceptionHandlingType() != ExceptionHandling::None
)
96 MAI
.usesCFIForEH() && (shouldEmitPersonality
|| shouldEmitMoves
);
98 shouldEmitCFI
= Asm
->usesCFIWithoutEH() && shouldEmitMoves
;
101 void DwarfCFIException::beginBasicBlockSection(const MachineBasicBlock
&MBB
) {
105 if (!hasEmittedCFISections
) {
106 AsmPrinter::CFISection CFISecType
= Asm
->getModuleCFISectionType();
107 // If we don't say anything it implies `.cfi_sections .eh_frame`, so we
108 // chose not to be verbose in that case. And with `ForceDwarfFrameSection`,
109 // we should always emit .debug_frame.
110 if (CFISecType
== AsmPrinter::CFISection::Debug
||
111 Asm
->TM
.Options
.ForceDwarfFrameSection
)
112 Asm
->OutStreamer
->emitCFISections(
113 CFISecType
== AsmPrinter::CFISection::EH
, true);
114 hasEmittedCFISections
= true;
117 Asm
->OutStreamer
->emitCFIStartProc(/*IsSimple=*/false);
119 // Indicate personality routine, if any.
120 if (!shouldEmitPersonality
)
123 auto &F
= MBB
.getParent()->getFunction();
124 auto *P
= dyn_cast
<GlobalValue
>(F
.getPersonalityFn()->stripPointerCasts());
125 assert(P
&& "Expected personality function");
126 // Record the personality function.
129 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
130 unsigned PerEncoding
= TLOF
.getPersonalityEncoding();
131 const MCSymbol
*Sym
= TLOF
.getCFIPersonalitySymbol(P
, Asm
->TM
, MMI
);
132 Asm
->OutStreamer
->emitCFIPersonality(Sym
, PerEncoding
);
134 // Provide LSDA information.
136 Asm
->OutStreamer
->emitCFILsda(Asm
->getMBBExceptionSym(MBB
),
137 TLOF
.getLSDAEncoding());
140 void DwarfCFIException::endBasicBlockSection(const MachineBasicBlock
&MBB
) {
142 Asm
->OutStreamer
->emitCFIEndProc();
145 /// endFunction - Gather and emit post-function exception information.
147 void DwarfCFIException::endFunction(const MachineFunction
*MF
) {
148 if (!shouldEmitPersonality
)
151 emitExceptionTable();