DwarfCFIException.cpp revision 360784
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" 34using namespace llvm; 35 36DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A) 37 : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {} 38 39void 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 49void DwarfCFIExceptionBase::endFragment() { 50 if (shouldEmitCFI) 51 Asm->OutStreamer->EmitCFIEndProc(); 52} 53 54DwarfCFIException::DwarfCFIException(AsmPrinter *A) 55 : DwarfCFIExceptionBase(A), shouldEmitPersonality(false), 56 forceEmitPersonality(false), shouldEmitLSDA(false), 57 shouldEmitMoves(false) {} 58 59DwarfCFIException::~DwarfCFIException() {} 60 61/// endModule - Emit all exception information that should come after the 62/// content. 63void DwarfCFIException::endModule() { 64 // SjLj uses this pass and it doesn't need this info. 65 if (!Asm->MAI->usesCFIForEH()) 66 return; 67 68 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 69 70 unsigned PerEncoding = TLOF.getPersonalityEncoding(); 71 72 if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect) 73 return; 74 75 // Emit references to all used personality functions 76 for (const Function *Personality : MMI->getPersonalities()) { 77 if (!Personality) 78 continue; 79 MCSymbol *Sym = Asm->getSymbol(Personality); 80 TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym); 81 } 82} 83 84static MCSymbol *getExceptionSym(AsmPrinter *Asm) { 85 return Asm->getCurExceptionSym(); 86} 87 88void DwarfCFIException::beginFunction(const MachineFunction *MF) { 89 shouldEmitMoves = 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 AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves(); 97 98 shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None; 99 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()); 105 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(); 114 115 shouldEmitPersonality = 116 (forceEmitPersonality || 117 (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) && 118 Per; 119 120 unsigned LSDAEncoding = TLOF.getLSDAEncoding(); 121 shouldEmitLSDA = shouldEmitPersonality && 122 LSDAEncoding != dwarf::DW_EH_PE_omit; 123 124 shouldEmitCFI = MF->getMMI().getContext().getAsmInfo()->usesCFIForEH() && 125 (shouldEmitPersonality || shouldEmitMoves); 126 beginFragment(&*MF->begin(), getExceptionSym); 127} 128 129void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB, 130 ExceptionSymbolProvider ESP) { 131 if (!shouldEmitCFI) 132 return; 133 134 if (!hasEmittedCFISections) { 135 if (Asm->needsOnlyDebugCFIMoves()) 136 Asm->OutStreamer->EmitCFISections(false, true); 137 else if (Asm->TM.Options.ForceDwarfFrameSection) 138 Asm->OutStreamer->EmitCFISections(true, true); 139 hasEmittedCFISections = true; 140 } 141 142 Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false); 143 144 // Indicate personality routine, if any. 145 if (!shouldEmitPersonality) 146 return; 147 148 auto &F = MBB->getParent()->getFunction(); 149 auto *P = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts()); 150 assert(P && "Expected personality function"); 151 152 // If we are forced to emit this personality, make sure to record 153 // it because it might not appear in any landingpad 154 if (forceEmitPersonality) 155 MMI->addPersonality(P); 156 157 const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); 158 unsigned PerEncoding = TLOF.getPersonalityEncoding(); 159 const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI); 160 Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding); 161 162 // Provide LSDA information. 163 if (shouldEmitLSDA) 164 Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding()); 165} 166 167/// endFunction - Gather and emit post-function exception information. 168/// 169void DwarfCFIException::endFunction(const MachineFunction *MF) { 170 if (!shouldEmitPersonality) 171 return; 172 173 emitExceptionTable(); 174} 175