Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
| 16 | #include "llvm/ADT/Twine.h" |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/AsmPrinter.h" |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 19 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/DataLayout.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 22 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Module.h" |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 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" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MachineLocation.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Dwarf.h" |
| 32 | #include "llvm/Support/ErrorHandling.h" |
| 33 | #include "llvm/Support/FormattedStream.h" |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 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" |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Rafael Espindola | a600179 | 2015-03-09 18:29:12 +0000 | [diff] [blame] | 41 | DwarfCFIExceptionBase::DwarfCFIExceptionBase(AsmPrinter *A) |
Joerg Sonnenberger | 7b83732 | 2017-01-02 18:05:27 +0000 | [diff] [blame] | 42 | : EHStreamer(A), shouldEmitCFI(false), hasEmittedCFISections(false) {} |
Rafael Espindola | a600179 | 2015-03-09 18:29:12 +0000 | [diff] [blame] | 43 | |
| 44 | void DwarfCFIExceptionBase::markFunctionEnd() { |
Amaury Sechet | 7067ad3 | 2016-02-26 20:30:37 +0000 | [diff] [blame] | 45 | endFragment(); |
Rafael Espindola | a600179 | 2015-03-09 18:29:12 +0000 | [diff] [blame] | 46 | |
Rafael Espindola | a600179 | 2015-03-09 18:29:12 +0000 | [diff] [blame] | 47 | // Map all labels and get rid of any dead landing pads. |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 48 | if (!Asm->MF->getLandingPads().empty()) { |
| 49 | MachineFunction *NonConstMF = const_cast<MachineFunction*>(Asm->MF); |
| 50 | NonConstMF->tidyLandingPads(); |
| 51 | } |
Rafael Espindola | a600179 | 2015-03-09 18:29:12 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Amaury Sechet | 7067ad3 | 2016-02-26 20:30:37 +0000 | [diff] [blame] | 54 | void DwarfCFIExceptionBase::endFragment() { |
| 55 | if (shouldEmitCFI) |
| 56 | Asm->OutStreamer->EmitCFIEndProc(); |
| 57 | } |
| 58 | |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 59 | DwarfCFIException::DwarfCFIException(AsmPrinter *A) |
Rafael Espindola | a600179 | 2015-03-09 18:29:12 +0000 | [diff] [blame] | 60 | : DwarfCFIExceptionBase(A), shouldEmitPersonality(false), |
Amaury Sechet | b2055c5 | 2016-02-26 20:49:07 +0000 | [diff] [blame] | 61 | forceEmitPersonality(false), shouldEmitLSDA(false), |
Joerg Sonnenberger | 7b83732 | 2017-01-02 18:05:27 +0000 | [diff] [blame] | 62 | shouldEmitMoves(false) {} |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 63 | |
| 64 | DwarfCFIException::~DwarfCFIException() {} |
| 65 | |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 66 | /// endModule - Emit all exception information that should come after the |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 67 | /// content. |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 68 | void DwarfCFIException::endModule() { |
Reid Kleckner | 5cc1569 | 2015-01-23 18:49:01 +0000 | [diff] [blame] | 69 | // SjLj uses this pass and it doesn't need this info. |
| 70 | if (!Asm->MAI->usesCFIForEH()) |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 71 | return; |
| 72 | |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 73 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
Rafael Espindola | 1fc5bf9 | 2011-04-29 15:09:53 +0000 | [diff] [blame] | 74 | |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 75 | unsigned PerEncoding = TLOF.getPersonalityEncoding(); |
| 76 | |
Logan Chien | c002981 | 2014-05-30 16:48:56 +0000 | [diff] [blame] | 77 | if ((PerEncoding & 0x80) != dwarf::DW_EH_PE_indirect) |
Rafael Espindola | a01cdb0 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 78 | return; |
| 79 | |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 80 | // Emit references to all used personality functions |
Reid Kleckner | e00faf8 | 2015-08-31 20:02:16 +0000 | [diff] [blame] | 81 | for (const Function *Personality : MMI->getPersonalities()) { |
| 82 | if (!Personality) |
Rafael Espindola | 697edc8 | 2011-04-29 14:48:51 +0000 | [diff] [blame] | 83 | continue; |
Reid Kleckner | e00faf8 | 2015-08-31 20:02:16 +0000 | [diff] [blame] | 84 | MCSymbol *Sym = Asm->getSymbol(Personality); |
Mehdi Amini | 5c0fa58 | 2015-07-16 06:04:17 +0000 | [diff] [blame] | 85 | TLOF.emitPersonalityValue(*Asm->OutStreamer, Asm->getDataLayout(), Sym); |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Amaury Sechet | 7067ad3 | 2016-02-26 20:30:37 +0000 | [diff] [blame] | 89 | static MCSymbol *getExceptionSym(AsmPrinter *Asm) { |
| 90 | return Asm->getCurExceptionSym(); |
| 91 | } |
| 92 | |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 93 | void DwarfCFIException::beginFunction(const MachineFunction *MF) { |
Rafael Espindola | 697edc8 | 2011-04-29 14:48:51 +0000 | [diff] [blame] | 94 | shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false; |
Keno Fischer | aff703a | 2015-07-14 19:22:51 +0000 | [diff] [blame] | 95 | const Function *F = MF->getFunction(); |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 96 | |
| 97 | // If any landing pads survive, we need an EH table. |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 98 | bool hasLandingPads = !MF->getLandingPads().empty(); |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 99 | |
| 100 | // See if we need frame move info. |
Rafael Espindola | fdc3e6f | 2011-05-10 18:39:09 +0000 | [diff] [blame] | 101 | AsmPrinter::CFIMoveType MoveType = Asm->needsCFIMoves(); |
Rafael Espindola | fdc3e6f | 2011-05-10 18:39:09 +0000 | [diff] [blame] | 102 | |
| 103 | shouldEmitMoves = MoveType != AsmPrinter::CFI_M_None; |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 104 | |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 105 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 106 | unsigned PerEncoding = TLOF.getPersonalityEncoding(); |
Keno Fischer | aff703a | 2015-07-14 19:22:51 +0000 | [diff] [blame] | 107 | const Function *Per = nullptr; |
| 108 | if (F->hasPersonalityFn()) |
| 109 | Per = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts()); |
Rafael Espindola | 697edc8 | 2011-04-29 14:48:51 +0000 | [diff] [blame] | 110 | |
Keno Fischer | aff703a | 2015-07-14 19:22:51 +0000 | [diff] [blame] | 111 | // Emit a personality function even when there are no landing pads |
Amaury Sechet | 7067ad3 | 2016-02-26 20:30:37 +0000 | [diff] [blame] | 112 | forceEmitPersonality = |
Keno Fischer | aff703a | 2015-07-14 19:22:51 +0000 | [diff] [blame] | 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(); |
| 119 | |
| 120 | shouldEmitPersonality = |
| 121 | (forceEmitPersonality || |
| 122 | (hasLandingPads && PerEncoding != dwarf::DW_EH_PE_omit)) && |
| 123 | Per; |
Rafael Espindola | 697edc8 | 2011-04-29 14:48:51 +0000 | [diff] [blame] | 124 | |
| 125 | unsigned LSDAEncoding = TLOF.getLSDAEncoding(); |
| 126 | shouldEmitLSDA = shouldEmitPersonality && |
| 127 | LSDAEncoding != dwarf::DW_EH_PE_omit; |
| 128 | |
Saleem Abdulrasool | 467269a | 2016-07-15 21:10:29 +0000 | [diff] [blame] | 129 | shouldEmitCFI = MF->getMMI().getContext().getAsmInfo()->usesCFIForEH() && |
| 130 | (shouldEmitPersonality || shouldEmitMoves); |
Amaury Sechet | 7067ad3 | 2016-02-26 20:30:37 +0000 | [diff] [blame] | 131 | beginFragment(&*MF->begin(), getExceptionSym); |
| 132 | } |
| 133 | |
| 134 | void DwarfCFIException::beginFragment(const MachineBasicBlock *MBB, |
| 135 | ExceptionSymbolProvider ESP) { |
Rafael Espindola | a600179 | 2015-03-09 18:29:12 +0000 | [diff] [blame] | 136 | if (!shouldEmitCFI) |
Rafael Espindola | 697edc8 | 2011-04-29 14:48:51 +0000 | [diff] [blame] | 137 | return; |
| 138 | |
Joerg Sonnenberger | 7b83732 | 2017-01-02 18:05:27 +0000 | [diff] [blame] | 139 | if (!hasEmittedCFISections) { |
Joerg Sonnenberger | 8396399 | 2017-01-05 20:55:28 +0000 | [diff] [blame] | 140 | if (Asm->needsOnlyDebugCFIMoves()) |
Joerg Sonnenberger | 7b83732 | 2017-01-02 18:05:27 +0000 | [diff] [blame] | 141 | Asm->OutStreamer->EmitCFISections(false, true); |
| 142 | hasEmittedCFISections = true; |
| 143 | } |
| 144 | |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 145 | Asm->OutStreamer->EmitCFIStartProc(/*IsSimple=*/false); |
Rafael Espindola | 697edc8 | 2011-04-29 14:48:51 +0000 | [diff] [blame] | 146 | |
| 147 | // Indicate personality routine, if any. |
| 148 | if (!shouldEmitPersonality) |
Rafael Espindola | a01cdb0 | 2011-04-15 15:11:06 +0000 | [diff] [blame] | 149 | return; |
| 150 | |
Amaury Sechet | 7067ad3 | 2016-02-26 20:30:37 +0000 | [diff] [blame] | 151 | auto *F = MBB->getParent()->getFunction(); |
| 152 | auto *P = dyn_cast<Function>(F->getPersonalityFn()->stripPointerCasts()); |
| 153 | assert(P && "Expected personality function"); |
| 154 | |
Keno Fischer | aff703a | 2015-07-14 19:22:51 +0000 | [diff] [blame] | 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) |
Amaury Sechet | 7067ad3 | 2016-02-26 20:30:37 +0000 | [diff] [blame] | 158 | MMI->addPersonality(P); |
Keno Fischer | aff703a | 2015-07-14 19:22:51 +0000 | [diff] [blame] | 159 | |
Amaury Sechet | 7067ad3 | 2016-02-26 20:30:37 +0000 | [diff] [blame] | 160 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 161 | unsigned PerEncoding = TLOF.getPersonalityEncoding(); |
Eric Christopher | 4367c7f | 2016-09-16 07:33:15 +0000 | [diff] [blame] | 162 | const MCSymbol *Sym = TLOF.getCFIPersonalitySymbol(P, Asm->TM, MMI); |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 163 | Asm->OutStreamer->EmitCFIPersonality(Sym, PerEncoding); |
Rafael Espindola | 697edc8 | 2011-04-29 14:48:51 +0000 | [diff] [blame] | 164 | |
Rafael Espindola | 697edc8 | 2011-04-29 14:48:51 +0000 | [diff] [blame] | 165 | // Provide LSDA information. |
Amaury Sechet | 7067ad3 | 2016-02-26 20:30:37 +0000 | [diff] [blame] | 166 | if (shouldEmitLSDA) |
| 167 | Asm->OutStreamer->EmitCFILsda(ESP(Asm), TLOF.getLSDAEncoding()); |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 170 | /// endFunction - Gather and emit post-function exception information. |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 171 | /// |
Matthias Braun | d0ee66c | 2016-12-01 19:32:15 +0000 | [diff] [blame] | 172 | void DwarfCFIException::endFunction(const MachineFunction *MF) { |
Rafael Espindola | cbda0e2 | 2012-01-17 04:19:20 +0000 | [diff] [blame] | 173 | if (!shouldEmitPersonality) |
| 174 | return; |
| 175 | |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 176 | emitExceptionTable(); |
Anton Korobeynikov | b46ef57 | 2011-01-14 21:57:53 +0000 | [diff] [blame] | 177 | } |