Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 1 | //===-- CodeGen/AsmPrinter/Win64Exception.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 Win64 exception info into asm files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Stephen Hines | 37ed9c1 | 2014-12-01 14:51:49 -0800 | [diff] [blame] | 14 | #include "Win64Exception.h" |
Chandler Carruth | d04a8d4 | 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" |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/AsmPrinter.h" |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 22 | #include "llvm/IR/DataLayout.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 23 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Module.h" |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +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 | d04a8d4 | 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" |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetFrameLowering.h" |
| 35 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 36 | #include "llvm/Target/TargetOptions.h" |
| 37 | #include "llvm/Target/TargetRegisterInfo.h" |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 38 | using namespace llvm; |
| 39 | |
| 40 | Win64Exception::Win64Exception(AsmPrinter *A) |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 41 | : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false), |
| 42 | shouldEmitMoves(false) {} |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 43 | |
| 44 | Win64Exception::~Win64Exception() {} |
| 45 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 46 | /// endModule - Emit all exception information that should come after the |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 47 | /// content. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 48 | void Win64Exception::endModule() { |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 51 | /// beginFunction - Gather pre-function exception information. Assumes it's |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 52 | /// being emitted immediately after the function entry point. |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 53 | void Win64Exception::beginFunction(const MachineFunction *MF) { |
Charles Davis | f463370 | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 54 | shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false; |
| 55 | |
| 56 | // If any landing pads survive, we need an EH table. |
| 57 | bool hasLandingPads = !MMI->getLandingPads().empty(); |
| 58 | |
| 59 | shouldEmitMoves = Asm->needsSEHMoves(); |
| 60 | |
| 61 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 62 | unsigned PerEncoding = TLOF.getPersonalityEncoding(); |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 63 | const Function *Per = MF->getMMI().getPersonality(); |
Charles Davis | f463370 | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 64 | |
| 65 | shouldEmitPersonality = hasLandingPads && |
| 66 | PerEncoding != dwarf::DW_EH_PE_omit && Per; |
| 67 | |
| 68 | unsigned LSDAEncoding = TLOF.getLSDAEncoding(); |
| 69 | shouldEmitLSDA = shouldEmitPersonality && |
| 70 | LSDAEncoding != dwarf::DW_EH_PE_omit; |
| 71 | |
| 72 | if (!shouldEmitPersonality && !shouldEmitMoves) |
| 73 | return; |
| 74 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 75 | Asm->OutStreamer.EmitWinCFIStartProc(Asm->CurrentFnSym); |
Charles Davis | 12e3349 | 2011-05-29 04:28:35 +0000 | [diff] [blame] | 76 | |
| 77 | if (!shouldEmitPersonality) |
| 78 | return; |
| 79 | |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 80 | const MCSymbol *PersHandlerSym = |
| 81 | TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI); |
| 82 | Asm->OutStreamer.EmitWinEHHandler(PersHandlerSym, true, true); |
Charles Davis | 59ed415 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 83 | |
| 84 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_begin", |
| 85 | Asm->getFunctionNumber())); |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 88 | /// endFunction - Gather and emit post-function exception information. |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 89 | /// |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 90 | void Win64Exception::endFunction(const MachineFunction *) { |
Charles Davis | f463370 | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 91 | if (!shouldEmitPersonality && !shouldEmitMoves) |
| 92 | return; |
| 93 | |
Charles Davis | 59ed415 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 94 | Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("eh_func_end", |
| 95 | Asm->getFunctionNumber())); |
| 96 | |
| 97 | // Map all labels and get rid of any dead landing pads. |
| 98 | MMI->TidyLandingPads(); |
| 99 | |
| 100 | if (shouldEmitPersonality) { |
Charles Davis | 59ed415 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 101 | Asm->OutStreamer.PushSection(); |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 102 | |
| 103 | // Emit an UNWIND_INFO struct describing the prologue. |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 104 | Asm->OutStreamer.EmitWinEHHandlerData(); |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 105 | |
| 106 | // Emit the tables appropriate to the personality function in use. If we |
| 107 | // don't recognize the personality, assume it uses an Itanium-style LSDA. |
| 108 | EHPersonality Per = MMI->getPersonalityType(); |
| 109 | if (Per == EHPersonality::MSVC_Win64SEH) |
| 110 | emitCSpecificHandlerTable(); |
| 111 | else |
| 112 | emitExceptionTable(); |
| 113 | |
Charles Davis | 59ed415 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 114 | Asm->OutStreamer.PopSection(); |
| 115 | } |
Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 116 | Asm->OutStreamer.EmitWinCFIEndProc(); |
Charles Davis | d652b13 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 117 | } |
Stephen Hines | ebe69fe | 2015-03-23 12:10:34 -0700 | [diff] [blame^] | 118 | |
| 119 | const MCSymbolRefExpr *Win64Exception::createImageRel32(const MCSymbol *Value) { |
| 120 | return MCSymbolRefExpr::Create(Value, MCSymbolRefExpr::VK_COFF_IMGREL32, |
| 121 | Asm->OutContext); |
| 122 | } |
| 123 | |
| 124 | /// Emit the language-specific data that __C_specific_handler expects. This |
| 125 | /// handler lives in the x64 Microsoft C runtime and allows catching or cleaning |
| 126 | /// up after faults with __try, __except, and __finally. The typeinfo values |
| 127 | /// are not really RTTI data, but pointers to filter functions that return an |
| 128 | /// integer (1, 0, or -1) indicating how to handle the exception. For __finally |
| 129 | /// blocks and other cleanups, the landing pad label is zero, and the filter |
| 130 | /// function is actually a cleanup handler with the same prototype. A catch-all |
| 131 | /// entry is modeled with a null filter function field and a non-zero landing |
| 132 | /// pad label. |
| 133 | /// |
| 134 | /// Possible filter function return values: |
| 135 | /// EXCEPTION_EXECUTE_HANDLER (1): |
| 136 | /// Jump to the landing pad label after cleanups. |
| 137 | /// EXCEPTION_CONTINUE_SEARCH (0): |
| 138 | /// Continue searching this table or continue unwinding. |
| 139 | /// EXCEPTION_CONTINUE_EXECUTION (-1): |
| 140 | /// Resume execution at the trapping PC. |
| 141 | /// |
| 142 | /// Inferred table structure: |
| 143 | /// struct Table { |
| 144 | /// int NumEntries; |
| 145 | /// struct Entry { |
| 146 | /// imagerel32 LabelStart; |
| 147 | /// imagerel32 LabelEnd; |
| 148 | /// imagerel32 FilterOrFinally; // One means catch-all. |
| 149 | /// imagerel32 LabelLPad; // Zero means __finally. |
| 150 | /// } Entries[NumEntries]; |
| 151 | /// }; |
| 152 | void Win64Exception::emitCSpecificHandlerTable() { |
| 153 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
| 154 | |
| 155 | // Simplifying assumptions for first implementation: |
| 156 | // - Cleanups are not implemented. |
| 157 | // - Filters are not implemented. |
| 158 | |
| 159 | // The Itanium LSDA table sorts similar landing pads together to simplify the |
| 160 | // actions table, but we don't need that. |
| 161 | SmallVector<const LandingPadInfo *, 64> LandingPads; |
| 162 | LandingPads.reserve(PadInfos.size()); |
| 163 | for (const auto &LP : PadInfos) |
| 164 | LandingPads.push_back(&LP); |
| 165 | |
| 166 | // Compute label ranges for call sites as we would for the Itanium LSDA, but |
| 167 | // use an all zero action table because we aren't using these actions. |
| 168 | SmallVector<unsigned, 64> FirstActions; |
| 169 | FirstActions.resize(LandingPads.size()); |
| 170 | SmallVector<CallSiteEntry, 64> CallSites; |
| 171 | computeCallSiteTable(CallSites, LandingPads, FirstActions); |
| 172 | |
| 173 | MCSymbol *EHFuncBeginSym = |
| 174 | Asm->GetTempSymbol("eh_func_begin", Asm->getFunctionNumber()); |
| 175 | MCSymbol *EHFuncEndSym = |
| 176 | Asm->GetTempSymbol("eh_func_end", Asm->getFunctionNumber()); |
| 177 | |
| 178 | // Emit the number of table entries. |
| 179 | unsigned NumEntries = 0; |
| 180 | for (const CallSiteEntry &CSE : CallSites) { |
| 181 | if (!CSE.LPad) |
| 182 | continue; // Ignore gaps. |
| 183 | for (int Selector : CSE.LPad->TypeIds) { |
| 184 | // Ignore C++ filter clauses in SEH. |
| 185 | // FIXME: Implement cleanup clauses. |
| 186 | if (isCatchEHSelector(Selector)) |
| 187 | ++NumEntries; |
| 188 | } |
| 189 | } |
| 190 | Asm->OutStreamer.EmitIntValue(NumEntries, 4); |
| 191 | |
| 192 | // Emit the four-label records for each call site entry. The table has to be |
| 193 | // sorted in layout order, and the call sites should already be sorted. |
| 194 | for (const CallSiteEntry &CSE : CallSites) { |
| 195 | // Ignore gaps. Unlike the Itanium model, unwinding through a frame without |
| 196 | // an EH table entry will propagate the exception rather than terminating |
| 197 | // the program. |
| 198 | if (!CSE.LPad) |
| 199 | continue; |
| 200 | const LandingPadInfo *LPad = CSE.LPad; |
| 201 | |
| 202 | // Compute the label range. We may reuse the function begin and end labels |
| 203 | // rather than forming new ones. |
| 204 | const MCExpr *Begin = |
| 205 | createImageRel32(CSE.BeginLabel ? CSE.BeginLabel : EHFuncBeginSym); |
| 206 | const MCExpr *End; |
| 207 | if (CSE.EndLabel) { |
| 208 | // The interval is half-open, so we have to add one to include the return |
| 209 | // address of the last invoke in the range. |
| 210 | End = MCBinaryExpr::CreateAdd(createImageRel32(CSE.EndLabel), |
| 211 | MCConstantExpr::Create(1, Asm->OutContext), |
| 212 | Asm->OutContext); |
| 213 | } else { |
| 214 | End = createImageRel32(EHFuncEndSym); |
| 215 | } |
| 216 | |
| 217 | // These aren't really type info globals, they are actually pointers to |
| 218 | // filter functions ordered by selector. The zero selector is used for |
| 219 | // cleanups, so slot zero corresponds to selector 1. |
| 220 | const std::vector<const GlobalValue *> &SelectorToFilter = MMI->getTypeInfos(); |
| 221 | |
| 222 | // Do a parallel iteration across typeids and clause labels, skipping filter |
| 223 | // clauses. |
| 224 | size_t NextClauseLabel = 0; |
| 225 | for (size_t I = 0, E = LPad->TypeIds.size(); I < E; ++I) { |
| 226 | // AddLandingPadInfo stores the clauses in reverse, but there is a FIXME |
| 227 | // to change that. |
| 228 | int Selector = LPad->TypeIds[E - I - 1]; |
| 229 | |
| 230 | // Ignore C++ filter clauses in SEH. |
| 231 | // FIXME: Implement cleanup clauses. |
| 232 | if (!isCatchEHSelector(Selector)) |
| 233 | continue; |
| 234 | |
| 235 | Asm->OutStreamer.EmitValue(Begin, 4); |
| 236 | Asm->OutStreamer.EmitValue(End, 4); |
| 237 | if (isCatchEHSelector(Selector)) { |
| 238 | assert(unsigned(Selector - 1) < SelectorToFilter.size()); |
| 239 | const GlobalValue *TI = SelectorToFilter[Selector - 1]; |
| 240 | if (TI) // Emit the filter function pointer. |
| 241 | Asm->OutStreamer.EmitValue(createImageRel32(Asm->getSymbol(TI)), 4); |
| 242 | else // Otherwise, this is a "catch i8* null", or catch all. |
| 243 | Asm->OutStreamer.EmitIntValue(1, 4); |
| 244 | } |
| 245 | MCSymbol *ClauseLabel = LPad->ClauseLabels[NextClauseLabel++]; |
| 246 | Asm->OutStreamer.EmitValue(createImageRel32(ClauseLabel), 4); |
| 247 | } |
| 248 | } |
| 249 | } |