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"
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/TargetMachine.h"
33 #include "llvm/Target/TargetOptions.h"
34 using namespace llvm;
35 
36 DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
37     : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {}
38 
39 void DwarfCFIExceptionBase::markFunctionEnd() {
40   endFragment();
41 
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();
46   }
47 }
48 
49 void DwarfCFIExceptionBase::endFragment() {
50   if (shouldEmitCFI && !Asm->MF->hasBBSections())
51     Asm->OutStreamer->emitCFIEndProc();
52 }
53 
54 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
55     : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
56       forceEmitPersonality(false), shouldEmitLSDA(false) {}
57 
58 DwarfCFIException::~DwarfCFIException() {}
59 
60 /// endModule - Emit all exception information that should come after the
61 /// content.
62 void DwarfCFIException::endModule() {
63   // SjLj uses this pass and it doesn't need this info.
64   if (!Asm->MAI->usesCFIForEH())
65     return;
66 
67   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
68 
69   unsigned PerEncoding = TLOF.getPersonalityEncoding();
70 
71   if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
72     return;
73 
74   // Emit references to all used personality functions
75   for (const Function *Personality : MMI->getPersonalities()) {
76     if (!Personality)
77       continue;
78     MCSymbol *Sym = Asm->getSymbol(Personality);
79     TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
80   }
81 }
82 
83 static MCSymbol *getExceptionSym(AsmPrinter *Asm,
84                                  const MachineBasicBlock *MBB) {
85   return Asm->getMBBExceptionSym(*MBB);
86 }
87 
88 void DwarfCFIException::beginFunction(const MachineFunction *MF) {
89   shouldEmitPersonality = shouldEmitLSDA = false;
90   const Function &F = MF->getFunction();
91 
92   // If any landing pads survive, we need an EH table.
93   bool hasLandingPads = !MF->getLandingPads().empty();
94 
95   // See if we need frame move info.
96   bool shouldEmitMoves =
97       Asm->getFunctionCFISectionType(*MF) != AsmPrinter::CFISection::None;
98 
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());
104 
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();
113 
114   shouldEmitPersonality =
115       (forceEmitPersonality ||
116        (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
117       Per;
118 
119   unsigned LSDAEncoding = TLOF.getLSDAEncoding();
120   shouldEmitLSDA = shouldEmitPersonality &&
121     LSDAEncoding != dwarf::DW_EH_PE_omit;
122 
123   const MCAsmInfo &MAI = *MF->getMMI().getContext().getAsmInfo();
124   if (MAI.getExceptionHandlingType() != ExceptionHandling::None)
125     shouldEmitCFI =
126         MAI.usesCFIForEH() && (shouldEmitPersonality || shouldEmitMoves);
127   else
128     shouldEmitCFI = Asm->needsCFIForDebug() && shouldEmitMoves;
129 
130   beginFragment(&*MF->begin(), getExceptionSym);
131 }
132 
133 void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
134                                       ExceptionSymbolProvider ESP) {
135   if (!shouldEmitCFI)
136     return;
137 
138   if (!hasEmittedCFISections) {
139     AsmPrinter::CFISection CFISecType = Asm->getModuleCFISectionType();
140     // If we don't say anything it implies `.cfi_sections .eh_frame`, so we
141     // chose not to be verbose in that case. And with `ForceDwarfFrameSection`,
142     // we should always emit .debug_frame.
143     if (CFISecType == AsmPrinter::CFISection::Debug ||
144         Asm->TM.Options.ForceDwarfFrameSection)
145       Asm->OutStreamer->emitCFISections(
146           CFISecType == AsmPrinter::CFISection::EH, true);
147     hasEmittedCFISections = true;
148   }
149 
150   Asm->OutStreamer->emitCFIStartProc(/*IsSimple=*/false);
151 
152   // Indicate personality routine, if any.
153   if (!shouldEmitPersonality)
154     return;
155 
156   auto &F = MBB->getParent()->getFunction();
157   auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
158   assert(P && "Expected personality function");
159 
160   // If we are forced to emit this personality, make sure to record
161   // it because it might not appear in any landingpad
162   if (forceEmitPersonality)
163     MMI->addPersonality(P);
164 
165   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
166   unsigned PerEncoding = TLOF.getPersonalityEncoding();
167   const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI);
168   Asm->OutStreamer->emitCFIPersonality(Sym, PerEncoding);
169 
170   // Provide LSDA information.
171   if (shouldEmitLSDA)
172     Asm->OutStreamer->emitCFILsda(ESP(Asm, MBB), TLOF.getLSDAEncoding());
173 }
174 
175 /// endFunction - Gather and emit post-function exception information.
176 ///
177 void DwarfCFIException::endFunction(const MachineFunction *MF) {
178   if (!shouldEmitPersonality)
179     return;
180 
181   emitExceptionTable();
182 }
183 
184 void DwarfCFIException::beginBasicBlock(const MachineBasicBlock &MBB) {
185   beginFragment(&MBB, getExceptionSym);
186 }
187 
188 void DwarfCFIException::endBasicBlock(const MachineBasicBlock &MBB) {
189   if (shouldEmitCFI)
190     Asm->OutStreamer->emitCFIEndProc();
191 }
192