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/ADT/Twine.h"
15 #include "llvm/BinaryFormat/Dwarf.h"
16 #include "llvm/CodeGen/AsmPrinter.h"
17 #include "llvm/CodeGen/MachineFunction.h"
18 #include "llvm/CodeGen/MachineModuleInfo.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"
28 #include "llvm/MC/MachineLocation.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/FormattedStream.h"
31 #include "llvm/Target/TargetLoweringObjectFile.h"
32 #include "llvm/Target/TargetOptions.h"
35 DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter
*A
)
36 : EHStreamer(A
), shouldEmitCFI(false), hasEmittedCFISections(false) {}
38 void DwarfCFIExceptionBase::markFunctionEnd() {
41 // Map all labels and get rid of any dead landing pads.
42 if (!Asm
->MF
->getLandingPads().empty()) {
43 MachineFunction
*NonConstMF
= const_cast<MachineFunction
*>(Asm
->MF
);
44 NonConstMF
->tidyLandingPads();
48 void DwarfCFIExceptionBase::endFragment() {
50 Asm
->OutStreamer
->EmitCFIEndProc();
53 DwarfCFIException::DwarfCFIException(AsmPrinter
*A
)
54 : DwarfCFIExceptionBase(A
), shouldEmitPersonality(false),
55 forceEmitPersonality(false), shouldEmitLSDA(false),
56 shouldEmitMoves(false) {}
58 DwarfCFIException::~DwarfCFIException() {}
60 /// endModule - Emit all exception information that should come after the
62 void DwarfCFIException::endModule() {
63 // SjLj uses this pass and it doesn't need this info.
64 if (!Asm
->MAI
->usesCFIForEH())
67 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
69 unsigned PerEncoding
= TLOF
.getPersonalityEncoding();
71 if ((PerEncoding
& 0x80) != dwarf::DW_EH_PE_indirect
)
74 // Emit references to all used personality functions
75 for (const Function
*Personality
: MMI
->getPersonalities()) {
78 MCSymbol
*Sym
= Asm
->getSymbol(Personality
);
79 TLOF
.emitPersonalityValue(*Asm
->OutStreamer
, Asm
->getDataLayout(), Sym
);
83 static MCSymbol
*getExceptionSym(AsmPrinter
*Asm
) {
84 return Asm
->getCurExceptionSym();
87 void DwarfCFIException::beginFunction(const MachineFunction
*MF
) {
88 shouldEmitMoves
= shouldEmitPersonality
= shouldEmitLSDA
= false;
89 const Function
&F
= MF
->getFunction();
91 // If any landing pads survive, we need an EH table.
92 bool hasLandingPads
= !MF
->getLandingPads().empty();
94 // See if we need frame move info.
95 AsmPrinter::CFIMoveType MoveType
= Asm
->needsCFIMoves();
97 shouldEmitMoves
= MoveType
!= AsmPrinter::CFI_M_None
;
99 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
100 unsigned PerEncoding
= TLOF
.getPersonalityEncoding();
101 const Function
*Per
= nullptr;
102 if (F
.hasPersonalityFn())
103 Per
= dyn_cast
<Function
>(F
.getPersonalityFn()->stripPointerCasts());
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
110 !isNoOpWithoutInvoke(classifyEHPersonality(Per
)) &&
111 // ... and we're not explicitly asked not to emit it
112 F
.needsUnwindTableEntry();
114 shouldEmitPersonality
=
115 (forceEmitPersonality
||
116 (hasLandingPads
&& PerEncoding
!= dwarf::DW_EH_PE_omit
)) &&
119 unsigned LSDAEncoding
= TLOF
.getLSDAEncoding();
120 shouldEmitLSDA
= shouldEmitPersonality
&&
121 LSDAEncoding
!= dwarf::DW_EH_PE_omit
;
123 shouldEmitCFI
= MF
->getMMI().getContext().getAsmInfo()->usesCFIForEH() &&
124 (shouldEmitPersonality
|| shouldEmitMoves
);
125 beginFragment(&*MF
->begin(), getExceptionSym
);
128 void DwarfCFIException::beginFragment(const MachineBasicBlock
*MBB
,
129 ExceptionSymbolProvider ESP
) {
133 if (!hasEmittedCFISections
) {
134 if (Asm
->needsOnlyDebugCFIMoves())
135 Asm
->OutStreamer
->EmitCFISections(false, true);
136 hasEmittedCFISections
= true;
139 Asm
->OutStreamer
->EmitCFIStartProc(/*IsSimple=*/false);
141 // Indicate personality routine, if any.
142 if (!shouldEmitPersonality
)
145 auto &F
= MBB
->getParent()->getFunction();
146 auto *P
= dyn_cast
<Function
>(F
.getPersonalityFn()->stripPointerCasts());
147 assert(P
&& "Expected personality function");
149 // If we are forced to emit this personality, make sure to record
150 // it because it might not appear in any landingpad
151 if (forceEmitPersonality
)
152 MMI
->addPersonality(P
);
154 const TargetLoweringObjectFile
&TLOF
= Asm
->getObjFileLowering();
155 unsigned PerEncoding
= TLOF
.getPersonalityEncoding();
156 const MCSymbol
*Sym
= TLOF
.getCFIPersonalitySymbol(P
, Asm
->TM
, MMI
);
157 Asm
->OutStreamer
->EmitCFIPersonality(Sym
, PerEncoding
);
159 // Provide LSDA information.
161 Asm
->OutStreamer
->EmitCFILsda(ESP(Asm
), TLOF
.getLSDAEncoding());
164 /// endFunction - Gather and emit post-function exception information.
166 void DwarfCFIException::endFunction(const MachineFunction
*MF
) {
167 if (!shouldEmitPersonality
)
170 emitExceptionTable();