1 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing DWARF exception info into asm files.
12 //===----------------------------------------------------------------------===//
14 #include "DwarfException.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/BinaryFormat/Dwarf.h"
17 #include "llvm/CodeGen/AsmPrinter.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineModuleInfo.h"
20 #include "llvm/CodeGen/TargetLoweringObjectFile.h"
21 #include "llvm/IR/DataLayout.h"
22 #include "llvm/IR/Mangler.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"
30 #include "llvm/MC/MachineLocation.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FormattedStream.h"
33 #include "llvm/Target/TargetOptions.h"
36 DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
37 : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
39 void DwarfCFIExceptionBase::markFunctionEnd() {
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();
49 void DwarfCFIExceptionBase::endFragment() {
51 Asm->OutStreamer->EmitCFIEndProc();
54 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
55 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
56 forceEmitPersonality(false), shouldEmitLSDA(false),
57 shouldEmitMoves(false) {}
59 DwarfCFIException::~DwarfCFIException() {}
61 /// endModule - Emit all exception information that should come after the
63 void DwarfCFIException::endModule() {
64 // SjLj uses this pass and it doesn't need this info.
65 if (!Asm->MAI->usesCFIForEH())
68 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
70 unsigned PerEncoding = TLOF.getPersonalityEncoding();
72 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
75 // Emit references to all used personality functions
76 for (const Function *Personality : MMI->getPersonalities()) {
79 MCSymbol *Sym = Asm->getSymbol(Personality);
80 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
84 static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
85 return Asm->getCurExceptionSym();
88 void DwarfCFIException::beginFunction(const MachineFunction *MF) {
89 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
90 const Function &F = MF->getFunction();
92 // If any landing pads survive, we need an EH table.
93 bool hasLandingPads = !MF->getLandingPads().empty();
95 // See if we need frame move info.
96 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
98 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
100 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
101 unsigned PerEncoding = TLOF.getPersonalityEncoding();
102 const Function *Per = nullptr;
103 if (F.hasPersonalityFn())
104 Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
106 // Emit a personality function even when there are no landing pads
107 forceEmitPersonality =
108 // ...if a personality function is explicitly specified
109 F.hasPersonalityFn() &&
110 // ... and it's not known to be a noop in the absence of invokes
111 !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
112 // ... and we're not explicitly asked not to emit it
113 F.needsUnwindTableEntry();
115 shouldEmitPersonality =
116 (forceEmitPersonality ||
117 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
120 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
121 shouldEmitLSDA = shouldEmitPersonality &&
122 LSDAEncoding != dwarf::DW_EH_PE_omit;
124 shouldEmitCFI = MF->getMMI().getContext().getAsmInfo()->usesCFIForEH() &&
125 (shouldEmitPersonality || shouldEmitMoves);
126 beginFragment(&*MF->begin(), getExceptionSym);
129 void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
130 ExceptionSymbolProvider ESP) {
134 if (!hasEmittedCFISections) {
135 if (Asm->needsOnlyDebugCFIMoves())
136 Asm->OutStreamer->EmitCFISections(false, true);
137 hasEmittedCFISections = true;
140 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
142 // Indicate personality routine, if any.
143 if (!shouldEmitPersonality)
146 auto &F = MBB->getParent()->getFunction();
147 auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
148 assert(P && "Expected personality function");
150 // If we are forced to emit this personality, make sure to record
151 // it because it might not appear in any landingpad
152 if (forceEmitPersonality)
153 MMI->addPersonality(P);
155 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
156 unsigned PerEncoding = TLOF.getPersonalityEncoding();
157 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
158 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
160 // Provide LSDA information.
162 Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
165 /// endFunction - Gather and emit post-function exception information.
167 void DwarfCFIException::endFunction(const MachineFunction *MF) {
168 if (!shouldEmitPersonality)
171 emitExceptionTable();