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