Home | History | Annotate | Download | only in AsmPrinter
      1 //===-- CodeGen/AsmPrinter/DwarfException.cpp - Dwarf Exception Impl ------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file contains support for writing DWARF exception info into asm files.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "DwarfException.h"
     15 #include "llvm/ADT/StringExtras.h"
     16 #include "llvm/ADT/Twine.h"
     17 #include "llvm/CodeGen/AsmPrinter.h"
     18 #include "llvm/CodeGen/MachineFrameInfo.h"
     19 #include "llvm/CodeGen/MachineFunction.h"
     20 #include "llvm/CodeGen/MachineModuleInfo.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/Dwarf.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"
     39 using namespace llvm;
     40 
     41 DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A)
     42     : EHStreamer(A), shouldEmitCFI(false) {}
     43 
     44 void DwarfCFIExceptionBase::markFunctionEnd() {
     45   endFragment();
     46 
     47   if (MMI->getLandingPads().empty())
     48     return;
     49 
     50   // Map all labels and get rid of any dead landing pads.
     51   MMI->TidyLandingPads();
     52 }
     53 
     54 void DwarfCFIExceptionBase::endFragment() {
     55   if (shouldEmitCFI)
     56     Asm->OutStreamer->EmitCFIEndProc();
     57 }
     58 
     59 DwarfCFIException::DwarfCFIException(AsmPrinter *A)
     60     : DwarfCFIExceptionBase(A), shouldEmitPersonality(false),
     61       forceEmitPersonality(false), shouldEmitLSDA(false),
     62       shouldEmitMoves(false), moveTypeModule(AsmPrinter::CFI_M_None) {}
     63 
     64 DwarfCFIException::~DwarfCFIException() {}
     65 
     66 /// endModule - Emit all exception information that should come after the
     67 /// content.
     68 void DwarfCFIException::endModule() {
     69   if (moveTypeModule == AsmPrinter::CFI_M_Debug)
     70     Asm->OutStreamer->EmitCFISections(false, true);
     71 
     72   // SjLj uses this pass and it doesn't need this info.
     73   if (!Asm->MAI->usesCFIForEH())
     74     return;
     75 
     76   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
     77 
     78   unsigned PerEncoding = TLOF.getPersonalityEncoding();
     79 
     80   if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect)
     81     return;
     82 
     83   // Emit references to all used personality functions
     84   for (const Function *Personality : MMI->getPersonalities()) {
     85     if (!Personality)
     86       continue;
     87     MCSymbol *Sym = Asm->getSymbol(Personality);
     88     TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym);
     89   }
     90 }
     91 
     92 static MCSymbol *getExceptionSym(AsmPrinter *Asm) {
     93   return Asm->getCurExceptionSym();
     94 }
     95 
     96 void DwarfCFIException::beginFunction(const MachineFunction *MF) {
     97   shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false;
     98   const Function *F = MF->getFunction();
     99 
    100   // If any landing pads survive, we need an EH table.
    101   bool hasLandingPads = !MMI->getLandingPads().empty();
    102 
    103   // See if we need frame move info.
    104   AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves();
    105   if (MoveType == AsmPrinter::CFI_M_EH ||
    106       (MoveType == AsmPrinter::CFI_M_Debug &&
    107        moveTypeModule == AsmPrinter::CFI_M_None))
    108     moveTypeModule = MoveType;
    109 
    110   shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None;
    111 
    112   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
    113   unsigned PerEncoding = TLOF.getPersonalityEncoding();
    114   const Function *Per = nullptr;
    115   if (F->hasPersonalityFn())
    116     Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
    117 
    118   // Emit a personality function even when there are no landing pads
    119   forceEmitPersonality =
    120       // ...if a personality function is explicitly specified
    121       F->hasPersonalityFn() &&
    122       // ... and it's not known to be a noop in the absence of invokes
    123       !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
    124       // ... and we're not explicitly asked not to emit it
    125       F->needsUnwindTableEntry();
    126 
    127   shouldEmitPersonality =
    128       (forceEmitPersonality ||
    129        (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) &&
    130       Per;
    131 
    132   unsigned LSDAEncoding = TLOF.getLSDAEncoding();
    133   shouldEmitLSDA = shouldEmitPersonality &&
    134     LSDAEncoding != dwarf::DW_EH_PE_omit;
    135 
    136   shouldEmitCFI = shouldEmitPersonality || shouldEmitMoves;
    137   beginFragment(&*MF->begin(), getExceptionSym);
    138 }
    139 
    140 void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB,
    141                                       ExceptionSymbolProvider ESP) {
    142   if (!shouldEmitCFI)
    143     return;
    144 
    145   Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false);
    146 
    147   // Indicate personality routine, if any.
    148   if (!shouldEmitPersonality)
    149     return;
    150 
    151   auto *F = MBB->getParent()->getFunction();
    152   auto *P = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts());
    153   assert(P && "Expected personality function");
    154 
    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);
    159 
    160   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
    161   unsigned PerEncoding = TLOF.getPersonalityEncoding();
    162   const MCSymbol *Sym =
    163       TLOF.getCFIPersonalitySymbol(P, *Asm->Mang, Asm->TM, MMI);
    164   Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding);
    165 
    166   // Provide LSDA information.
    167   if (shouldEmitLSDA)
    168     Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding());
    169 }
    170 
    171 /// endFunction - Gather and emit post-function exception information.
    172 ///
    173 void DwarfCFIException::endFunction(const MachineFunction *) {
    174   if (!shouldEmitPersonality)
    175     return;
    176 
    177   emitExceptionTable();
    178 }
    179