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/StringExtras.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/BinaryFormat/Dwarf.h"
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineModuleInfo.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/IR/Mangler.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCContext.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MachineLocation.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/FormattedStream.h"
34 #include "llvm/Target/TargetFrameLowering.h"
35 #include "llvm/Target/TargetLoweringObjectFile.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetOptions.h"
38 #include "llvm/Target/TargetRegisterInfo.h"
41 DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
42 : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
44 void DwarfCFIExceptionBase::markFunctionEnd() {
47 // Map all labels and get rid of any dead landing pads.
48 if (!Asm->MF->getLandingPads().empty()) {
49 MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF);
50 NonConstMF->tidyLandingPads();
54 void DwarfCFIExceptionBase::endFragment() {
56 Asm->OutStreamer->EmitCFIEndProc();
59 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
60 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
61 forceEmitPersonality(false), shouldEmitLSDA(false),
62 shouldEmitMoves(false) {}
64 DwarfCFIException::~DwarfCFIException() {}
66 /// endModule - Emit all exception information that should come after the
68 void DwarfCFIException::endModule() {
69 // SjLj uses this pass and it doesn't need this info.
70 if (!Asm->MAI->usesCFIForEH())
73 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
75 unsigned PerEncoding = TLOF.getPersonalityEncoding();
77 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
80 // Emit references to all used personality functions
81 for (const Function *Personality : MMI->getPersonalities()) {
84 MCSymbol *Sym = Asm->getSymbol(Personality);
85 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
89 static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
90 return Asm->getCurExceptionSym();
93 void DwarfCFIException::beginFunction(const MachineFunction *MF) {
94 shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
95 const Function *F = MF->getFunction();
97 // If any landing pads survive, we need an EH table.
98 bool hasLandingPads = !MF->getLandingPads().empty();
100 // See if we need frame move info.
101 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
103 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
105 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
106 unsigned PerEncoding = TLOF.getPersonalityEncoding();
107 const Function *Per = nullptr;
108 if (F->hasPersonalityFn())
109 Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
111 // Emit a personality function even when there are no landing pads
112 forceEmitPersonality =
113 // ...if a personality function is explicitly specified
114 F->hasPersonalityFn() &&
115 // ... and it's not known to be a noop in the absence of invokes
116 !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
117 // ... and we're not explicitly asked not to emit it
118 F->needsUnwindTableEntry();
120 shouldEmitPersonality =
121 (forceEmitPersonality ||
122 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
125 unsigned LSDAEncoding = TLOF.getLSDAEncoding();
126 shouldEmitLSDA = shouldEmitPersonality &&
127 LSDAEncoding != dwarf::DW_EH_PE_omit;
129 shouldEmitCFI = MF->getMMI().getContext().getAsmInfo()->usesCFIForEH() &&
130 (shouldEmitPersonality || shouldEmitMoves);
131 beginFragment(&*MF->begin(), getExceptionSym);
134 void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
135 ExceptionSymbolProvider ESP) {
139 if (!hasEmittedCFISections) {
140 if (Asm->needsOnlyDebugCFIMoves())
141 Asm->OutStreamer->EmitCFISections(false, true);
142 hasEmittedCFISections = true;
145 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
147 // Indicate personality routine, if any.
148 if (!shouldEmitPersonality)
151 auto *F = MBB->getParent()->getFunction();
152 auto *P = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
153 assert(P && "Expected personality function");
155 // If we are forced to emit this personality, make sure to record
156 // it because it might not appear in any landingpad
157 if (forceEmitPersonality)
158 MMI->addPersonality(P);
160 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
161 unsigned PerEncoding = TLOF.getPersonalityEncoding();
162 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
163 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
165 // Provide LSDA information.
167 Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
170 /// endFunction - Gather and emit post-function exception information.
172 void DwarfCFIException::endFunction(const MachineFunction *MF) {
173 if (!shouldEmitPersonality)
176 emitExceptionTable();