Charles Davis | 91ed799 | 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 | |
Saleem Abdulrasool | 0fba7b5 | 2014-09-01 23:48:34 +0000 | [diff] [blame] | 14 | #include "Win64Exception.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" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/AsmPrinter.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +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" |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/WinEHFuncInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 23 | #include "llvm/IR/DataLayout.h" |
Rafael Espindola | 894843c | 2014-01-07 21:19:40 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Mangler.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/Module.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCAsmInfo.h" |
| 27 | #include "llvm/MC/MCContext.h" |
| 28 | #include "llvm/MC/MCExpr.h" |
| 29 | #include "llvm/MC/MCSection.h" |
| 30 | #include "llvm/MC/MCStreamer.h" |
| 31 | #include "llvm/MC/MCSymbol.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" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetFrameLowering.h" |
| 36 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 37 | #include "llvm/Target/TargetOptions.h" |
| 38 | #include "llvm/Target/TargetRegisterInfo.h" |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
| 41 | Win64Exception::Win64Exception(AsmPrinter *A) |
Saleem Abdulrasool | 8076cab | 2014-06-11 01:19:03 +0000 | [diff] [blame] | 42 | : EHStreamer(A), shouldEmitPersonality(false), shouldEmitLSDA(false), |
| 43 | shouldEmitMoves(false) {} |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 44 | |
| 45 | Win64Exception::~Win64Exception() {} |
| 46 | |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 47 | /// endModule - Emit all exception information that should come after the |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 48 | /// content. |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 49 | void Win64Exception::endModule() { |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 52 | void Win64Exception::beginFunction(const MachineFunction *MF) { |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 53 | shouldEmitMoves = shouldEmitPersonality = shouldEmitLSDA = false; |
| 54 | |
| 55 | // If any landing pads survive, we need an EH table. |
| 56 | bool hasLandingPads = !MMI->getLandingPads().empty(); |
| 57 | |
| 58 | shouldEmitMoves = Asm->needsSEHMoves(); |
| 59 | |
| 60 | const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering(); |
| 61 | unsigned PerEncoding = TLOF.getPersonalityEncoding(); |
Reid Kleckner | e80a0a7 | 2015-01-14 22:47:54 +0000 | [diff] [blame] | 62 | const Function *Per = MF->getMMI().getPersonality(); |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 63 | |
| 64 | shouldEmitPersonality = hasLandingPads && |
| 65 | PerEncoding != dwarf::DW_EH_PE_omit && Per; |
| 66 | |
| 67 | unsigned LSDAEncoding = TLOF.getLSDAEncoding(); |
| 68 | shouldEmitLSDA = shouldEmitPersonality && |
| 69 | LSDAEncoding != dwarf::DW_EH_PE_omit; |
| 70 | |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 71 | |
| 72 | // If this was an outlined handler, we need to define the label corresponding |
| 73 | // to the offset of the parent frame relative to the stack pointer after the |
| 74 | // prologue. |
| 75 | const Function *F = MF->getFunction(); |
| 76 | const Function *ParentF = MMI->getWinEHParent(F); |
| 77 | if (F != ParentF) { |
| 78 | WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF); |
| 79 | auto I = FuncInfo.CatchHandlerParentFrameObjOffset.find(F); |
| 80 | if (I != FuncInfo.CatchHandlerParentFrameObjOffset.end()) { |
| 81 | MCSymbol *HandlerTypeParentFrameOffset = |
| 82 | Asm->OutContext.getOrCreateParentFrameOffsetSymbol( |
| 83 | GlobalValue::getRealLinkageName(F->getName())); |
| 84 | |
| 85 | // Emit a symbol assignment. |
| 86 | Asm->OutStreamer.EmitAssignment( |
| 87 | HandlerTypeParentFrameOffset, |
| 88 | MCConstantExpr::Create(I->second, Asm->OutContext)); |
| 89 | } |
| 90 | } |
| 91 | |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 92 | if (!shouldEmitPersonality && !shouldEmitMoves) |
| 93 | return; |
| 94 | |
Saleem Abdulrasool | 7206a52 | 2014-06-29 01:52:01 +0000 | [diff] [blame] | 95 | Asm->OutStreamer.EmitWinCFIStartProc(Asm->CurrentFnSym); |
Charles Davis | b025724 | 2011-05-29 04:28:35 +0000 | [diff] [blame] | 96 | |
| 97 | if (!shouldEmitPersonality) |
| 98 | return; |
| 99 | |
NAKAMURA Takumi | 1db5995 | 2014-06-25 12:41:52 +0000 | [diff] [blame] | 100 | const MCSymbol *PersHandlerSym = |
| 101 | TLOF.getCFIPersonalitySymbol(Per, *Asm->Mang, Asm->TM, MMI); |
Saleem Abdulrasool | 7206a52 | 2014-06-29 01:52:01 +0000 | [diff] [blame] | 102 | Asm->OutStreamer.EmitWinEHHandler(PersHandlerSym, true, true); |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Timur Iskhodzhanov | 119f307 | 2013-11-26 13:34:55 +0000 | [diff] [blame] | 105 | /// endFunction - Gather and emit post-function exception information. |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 106 | /// |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 107 | void Win64Exception::endFunction(const MachineFunction *MF) { |
Charles Davis | 5638b9f | 2011-05-28 04:21:04 +0000 | [diff] [blame] | 108 | if (!shouldEmitPersonality && !shouldEmitMoves) |
| 109 | return; |
| 110 | |
Reid Kleckner | 3e9fadf | 2015-04-15 18:48:15 +0000 | [diff] [blame] | 111 | EHPersonality Per = MMI->getPersonalityType(); |
| 112 | |
| 113 | // Get rid of any dead landing pads if we're not using a Windows EH scheme. In |
| 114 | // Windows EH schemes, the landing pad is not actually reachable. It only |
| 115 | // exists so that we can emit the right table data. |
| 116 | if (!isMSVCEHPersonality(Per)) |
| 117 | MMI->TidyLandingPads(); |
Charles Davis | a575226 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 118 | |
| 119 | if (shouldEmitPersonality) { |
Charles Davis | a575226 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 120 | Asm->OutStreamer.PushSection(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 121 | |
| 122 | // Emit an UNWIND_INFO struct describing the prologue. |
Saleem Abdulrasool | 7206a52 | 2014-06-29 01:52:01 +0000 | [diff] [blame] | 123 | Asm->OutStreamer.EmitWinEHHandlerData(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 124 | |
Reid Kleckner | 9b5eaf0 | 2015-01-14 18:50:10 +0000 | [diff] [blame] | 125 | // Emit the tables appropriate to the personality function in use. If we |
| 126 | // don't recognize the personality, assume it uses an Itanium-style LSDA. |
Reid Kleckner | 2d5fb68 | 2015-02-14 00:21:02 +0000 | [diff] [blame] | 127 | if (Per == EHPersonality::MSVC_Win64SEH) |
Reid Kleckner | 9b5eaf0 | 2015-01-14 18:50:10 +0000 | [diff] [blame] | 128 | emitCSpecificHandlerTable(); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 129 | else if (Per == EHPersonality::MSVC_CXX) |
| 130 | emitCXXFrameHandler3Table(MF); |
Reid Kleckner | 9b5eaf0 | 2015-01-14 18:50:10 +0000 | [diff] [blame] | 131 | else |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 132 | emitExceptionTable(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 133 | |
Charles Davis | a575226 | 2011-05-30 00:13:34 +0000 | [diff] [blame] | 134 | Asm->OutStreamer.PopSection(); |
| 135 | } |
Saleem Abdulrasool | 7206a52 | 2014-06-29 01:52:01 +0000 | [diff] [blame] | 136 | Asm->OutStreamer.EmitWinCFIEndProc(); |
Charles Davis | 91ed799 | 2011-05-27 23:47:32 +0000 | [diff] [blame] | 137 | } |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 138 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 139 | const MCExpr *Win64Exception::createImageRel32(const MCSymbol *Value) { |
| 140 | if (!Value) |
| 141 | return MCConstantExpr::Create(0, Asm->OutContext); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 142 | return MCSymbolRefExpr::Create(Value, MCSymbolRefExpr::VK_COFF_IMGREL32, |
| 143 | Asm->OutContext); |
| 144 | } |
| 145 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 146 | const MCExpr *Win64Exception::createImageRel32(const GlobalValue *GV) { |
| 147 | if (!GV) |
| 148 | return MCConstantExpr::Create(0, Asm->OutContext); |
| 149 | return createImageRel32(Asm->getSymbol(GV)); |
| 150 | } |
| 151 | |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 152 | /// Emit the language-specific data that __C_specific_handler expects. This |
| 153 | /// handler lives in the x64 Microsoft C runtime and allows catching or cleaning |
| 154 | /// up after faults with __try, __except, and __finally. The typeinfo values |
| 155 | /// are not really RTTI data, but pointers to filter functions that return an |
| 156 | /// integer (1, 0, or -1) indicating how to handle the exception. For __finally |
| 157 | /// blocks and other cleanups, the landing pad label is zero, and the filter |
| 158 | /// function is actually a cleanup handler with the same prototype. A catch-all |
| 159 | /// entry is modeled with a null filter function field and a non-zero landing |
| 160 | /// pad label. |
| 161 | /// |
| 162 | /// Possible filter function return values: |
| 163 | /// EXCEPTION_EXECUTE_HANDLER (1): |
| 164 | /// Jump to the landing pad label after cleanups. |
| 165 | /// EXCEPTION_CONTINUE_SEARCH (0): |
| 166 | /// Continue searching this table or continue unwinding. |
| 167 | /// EXCEPTION_CONTINUE_EXECUTION (-1): |
| 168 | /// Resume execution at the trapping PC. |
| 169 | /// |
| 170 | /// Inferred table structure: |
| 171 | /// struct Table { |
| 172 | /// int NumEntries; |
| 173 | /// struct Entry { |
| 174 | /// imagerel32 LabelStart; |
| 175 | /// imagerel32 LabelEnd; |
Reid Kleckner | f690f50 | 2015-01-22 02:27:44 +0000 | [diff] [blame] | 176 | /// imagerel32 FilterOrFinally; // One means catch-all. |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 177 | /// imagerel32 LabelLPad; // Zero means __finally. |
| 178 | /// } Entries[NumEntries]; |
| 179 | /// }; |
| 180 | void Win64Exception::emitCSpecificHandlerTable() { |
| 181 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
| 182 | |
| 183 | // Simplifying assumptions for first implementation: |
| 184 | // - Cleanups are not implemented. |
| 185 | // - Filters are not implemented. |
| 186 | |
| 187 | // The Itanium LSDA table sorts similar landing pads together to simplify the |
| 188 | // actions table, but we don't need that. |
| 189 | SmallVector<const LandingPadInfo *, 64> LandingPads; |
| 190 | LandingPads.reserve(PadInfos.size()); |
| 191 | for (const auto &LP : PadInfos) |
| 192 | LandingPads.push_back(&LP); |
| 193 | |
| 194 | // Compute label ranges for call sites as we would for the Itanium LSDA, but |
| 195 | // use an all zero action table because we aren't using these actions. |
| 196 | SmallVector<unsigned, 64> FirstActions; |
| 197 | FirstActions.resize(LandingPads.size()); |
| 198 | SmallVector<CallSiteEntry, 64> CallSites; |
| 199 | computeCallSiteTable(CallSites, LandingPads, FirstActions); |
| 200 | |
Rafael Espindola | 629cdba | 2015-02-27 18:18:39 +0000 | [diff] [blame] | 201 | MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin(); |
| 202 | MCSymbol *EHFuncEndSym = Asm->getFunctionEnd(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 203 | |
| 204 | // Emit the number of table entries. |
| 205 | unsigned NumEntries = 0; |
| 206 | for (const CallSiteEntry &CSE : CallSites) { |
| 207 | if (!CSE.LPad) |
| 208 | continue; // Ignore gaps. |
Nico Weber | a762fa6 | 2015-04-17 09:10:43 +0000 | [diff] [blame] | 209 | for (int Selector : CSE.LPad->TypeIds) { |
| 210 | // Ignore C++ filter clauses in SEH. |
| 211 | // FIXME: Implement cleanup clauses. |
| 212 | if (isCatchEHSelector(Selector)) |
| 213 | ++NumEntries; |
| 214 | } |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame^] | 215 | NumEntries += CSE.LPad->SEHHandlers.size(); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 216 | } |
| 217 | Asm->OutStreamer.EmitIntValue(NumEntries, 4); |
| 218 | |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame^] | 219 | // If there are no actions, we don't need to iterate again. |
| 220 | if (NumEntries == 0) |
| 221 | return; |
| 222 | |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 223 | // Emit the four-label records for each call site entry. The table has to be |
| 224 | // sorted in layout order, and the call sites should already be sorted. |
| 225 | for (const CallSiteEntry &CSE : CallSites) { |
| 226 | // Ignore gaps. Unlike the Itanium model, unwinding through a frame without |
| 227 | // an EH table entry will propagate the exception rather than terminating |
| 228 | // the program. |
| 229 | if (!CSE.LPad) |
| 230 | continue; |
| 231 | const LandingPadInfo *LPad = CSE.LPad; |
| 232 | |
| 233 | // Compute the label range. We may reuse the function begin and end labels |
| 234 | // rather than forming new ones. |
| 235 | const MCExpr *Begin = |
| 236 | createImageRel32(CSE.BeginLabel ? CSE.BeginLabel : EHFuncBeginSym); |
| 237 | const MCExpr *End; |
| 238 | if (CSE.EndLabel) { |
| 239 | // The interval is half-open, so we have to add one to include the return |
| 240 | // address of the last invoke in the range. |
| 241 | End = MCBinaryExpr::CreateAdd(createImageRel32(CSE.EndLabel), |
| 242 | MCConstantExpr::Create(1, Asm->OutContext), |
| 243 | Asm->OutContext); |
| 244 | } else { |
| 245 | End = createImageRel32(EHFuncEndSym); |
| 246 | } |
| 247 | |
Reid Kleckner | d2a1a51 | 2015-04-21 18:23:57 +0000 | [diff] [blame^] | 248 | // Emit an entry for each action. |
| 249 | for (SEHHandler Handler : LPad->SEHHandlers) { |
| 250 | Asm->OutStreamer.EmitValue(Begin, 4); |
| 251 | Asm->OutStreamer.EmitValue(End, 4); |
| 252 | |
| 253 | // Emit the filter or finally function pointer, if present. Otherwise, |
| 254 | // emit '1' to indicate a catch-all. |
| 255 | const Function *F = Handler.FilterOrFinally; |
| 256 | if (F) |
| 257 | Asm->OutStreamer.EmitValue(createImageRel32(Asm->getSymbol(F)), 4); |
| 258 | else |
| 259 | Asm->OutStreamer.EmitIntValue(1, 4); |
| 260 | |
| 261 | // Emit the recovery address, if present. Otherwise, this must be a |
| 262 | // finally. |
| 263 | const BlockAddress *BA = Handler.RecoverBA; |
| 264 | if (BA) |
| 265 | Asm->OutStreamer.EmitValue( |
| 266 | createImageRel32(Asm->GetBlockAddressSymbol(BA)), 4); |
| 267 | else |
| 268 | Asm->OutStreamer.EmitIntValue(0, 4); |
| 269 | } |
| 270 | if (!LPad->SEHHandlers.empty()) |
| 271 | continue; |
| 272 | |
Nico Weber | a762fa6 | 2015-04-17 09:10:43 +0000 | [diff] [blame] | 273 | // These aren't really type info globals, they are actually pointers to |
| 274 | // filter functions ordered by selector. The zero selector is used for |
| 275 | // cleanups, so slot zero corresponds to selector 1. |
| 276 | const std::vector<const GlobalValue *> &SelectorToFilter = MMI->getTypeInfos(); |
| 277 | |
| 278 | // Do a parallel iteration across typeids and clause labels, skipping filter |
| 279 | // clauses. |
| 280 | size_t NextClauseLabel = 0; |
| 281 | for (size_t I = 0, E = LPad->TypeIds.size(); I < E; ++I) { |
| 282 | // AddLandingPadInfo stores the clauses in reverse, but there is a FIXME |
| 283 | // to change that. |
| 284 | int Selector = LPad->TypeIds[E - I - 1]; |
| 285 | |
| 286 | // Ignore C++ filter clauses in SEH. |
| 287 | // FIXME: Implement cleanup clauses. |
| 288 | if (!isCatchEHSelector(Selector)) |
| 289 | continue; |
| 290 | |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 291 | Asm->OutStreamer.EmitValue(Begin, 4); |
| 292 | Asm->OutStreamer.EmitValue(End, 4); |
Nico Weber | a762fa6 | 2015-04-17 09:10:43 +0000 | [diff] [blame] | 293 | if (isCatchEHSelector(Selector)) { |
| 294 | assert(unsigned(Selector - 1) < SelectorToFilter.size()); |
| 295 | const GlobalValue *TI = SelectorToFilter[Selector - 1]; |
| 296 | if (TI) // Emit the filter function pointer. |
| 297 | Asm->OutStreamer.EmitValue(createImageRel32(Asm->getSymbol(TI)), 4); |
| 298 | else // Otherwise, this is a "catch i8* null", or catch all. |
| 299 | Asm->OutStreamer.EmitIntValue(1, 4); |
| 300 | } |
| 301 | MCSymbol *ClauseLabel = LPad->ClauseLabels[NextClauseLabel++]; |
| 302 | Asm->OutStreamer.EmitValue(createImageRel32(ClauseLabel), 4); |
Reid Kleckner | 0a57f65 | 2015-01-14 01:05:27 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | } |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 306 | |
| 307 | void Win64Exception::emitCXXFrameHandler3Table(const MachineFunction *MF) { |
| 308 | const Function *F = MF->getFunction(); |
| 309 | const Function *ParentF = MMI->getWinEHParent(F); |
| 310 | auto &OS = Asm->OutStreamer; |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 311 | WinEHFuncInfo &FuncInfo = MMI->getWinEHFuncInfo(ParentF); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 312 | |
| 313 | StringRef ParentLinkageName = |
| 314 | GlobalValue::getRealLinkageName(ParentF->getName()); |
| 315 | |
| 316 | MCSymbol *FuncInfoXData = |
| 317 | Asm->OutContext.GetOrCreateSymbol(Twine("$cppxdata$", ParentLinkageName)); |
| 318 | OS.EmitValue(createImageRel32(FuncInfoXData), 4); |
| 319 | |
| 320 | // The Itanium LSDA table sorts similar landing pads together to simplify the |
| 321 | // actions table, but we don't need that. |
| 322 | SmallVector<const LandingPadInfo *, 64> LandingPads; |
| 323 | const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); |
| 324 | LandingPads.reserve(PadInfos.size()); |
| 325 | for (const auto &LP : PadInfos) |
| 326 | LandingPads.push_back(&LP); |
| 327 | |
| 328 | RangeMapType PadMap; |
| 329 | computePadMap(LandingPads, PadMap); |
| 330 | |
| 331 | // The end label of the previous invoke or nounwind try-range. |
| 332 | MCSymbol *LastLabel = Asm->getFunctionBegin(); |
| 333 | |
| 334 | // Whether there is a potentially throwing instruction (currently this means |
| 335 | // an ordinary call) between the end of the previous try-range and now. |
| 336 | bool SawPotentiallyThrowing = false; |
| 337 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 338 | int LastEHState = -2; |
| 339 | |
| 340 | // The parent function and the catch handlers contribute to the 'ip2state' |
| 341 | // table. |
| 342 | for (const auto &MBB : *MF) { |
| 343 | for (const auto &MI : MBB) { |
| 344 | if (!MI.isEHLabel()) { |
| 345 | if (MI.isCall()) |
| 346 | SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI); |
| 347 | continue; |
| 348 | } |
| 349 | |
| 350 | // End of the previous try-range? |
| 351 | MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol(); |
| 352 | if (BeginLabel == LastLabel) |
| 353 | SawPotentiallyThrowing = false; |
| 354 | |
| 355 | // Beginning of a new try-range? |
| 356 | RangeMapType::const_iterator L = PadMap.find(BeginLabel); |
| 357 | if (L == PadMap.end()) |
| 358 | // Nope, it was just some random label. |
| 359 | continue; |
| 360 | |
| 361 | const PadRange &P = L->second; |
| 362 | const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; |
| 363 | assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && |
| 364 | "Inconsistent landing pad map!"); |
| 365 | |
| 366 | if (SawPotentiallyThrowing) { |
| 367 | FuncInfo.IPToStateList.push_back(std::make_pair(LastLabel, -1)); |
| 368 | SawPotentiallyThrowing = false; |
| 369 | LastEHState = -1; |
| 370 | } |
| 371 | |
| 372 | if (LandingPad->WinEHState != LastEHState) |
| 373 | FuncInfo.IPToStateList.push_back( |
| 374 | std::make_pair(BeginLabel, LandingPad->WinEHState)); |
| 375 | LastEHState = LandingPad->WinEHState; |
| 376 | LastLabel = LandingPad->EndLabels[P.RangeIndex]; |
| 377 | } |
| 378 | } |
| 379 | |
David Majnemer | 5c65f58 | 2015-04-10 04:56:17 +0000 | [diff] [blame] | 380 | // Defer emission until we've visited the parent function and all the catch |
Reid Kleckner | e5f1383 | 2015-04-14 21:42:36 +0000 | [diff] [blame] | 381 | // handlers. Cleanups don't contribute to the ip2state table yet, so don't |
| 382 | // count them. |
| 383 | if (ParentF != F && !FuncInfo.CatchHandlerMaxState.count(F)) |
| 384 | return; |
| 385 | ++FuncInfo.NumIPToStateFuncsVisited; |
David Majnemer | 5c65f58 | 2015-04-10 04:56:17 +0000 | [diff] [blame] | 386 | if (FuncInfo.NumIPToStateFuncsVisited != FuncInfo.CatchHandlerMaxState.size()) |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 387 | return; |
| 388 | |
| 389 | MCSymbol *UnwindMapXData = nullptr; |
| 390 | MCSymbol *TryBlockMapXData = nullptr; |
| 391 | MCSymbol *IPToStateXData = nullptr; |
| 392 | if (!FuncInfo.UnwindMap.empty()) |
| 393 | UnwindMapXData = Asm->OutContext.GetOrCreateSymbol( |
| 394 | Twine("$stateUnwindMap$", ParentLinkageName)); |
| 395 | if (!FuncInfo.TryBlockMap.empty()) |
| 396 | TryBlockMapXData = Asm->OutContext.GetOrCreateSymbol( |
| 397 | Twine("$tryMap$", ParentLinkageName)); |
| 398 | if (!FuncInfo.IPToStateList.empty()) |
| 399 | IPToStateXData = Asm->OutContext.GetOrCreateSymbol( |
| 400 | Twine("$ip2state$", ParentLinkageName)); |
| 401 | |
| 402 | // FuncInfo { |
| 403 | // uint32_t MagicNumber |
| 404 | // int32_t MaxState; |
| 405 | // UnwindMapEntry *UnwindMap; |
| 406 | // uint32_t NumTryBlocks; |
| 407 | // TryBlockMapEntry *TryBlockMap; |
| 408 | // uint32_t IPMapEntries; |
| 409 | // IPToStateMapEntry *IPToStateMap; |
| 410 | // uint32_t UnwindHelp; // (x64/ARM only) |
| 411 | // ESTypeList *ESTypeList; |
| 412 | // int32_t EHFlags; |
| 413 | // } |
| 414 | // EHFlags & 1 -> Synchronous exceptions only, no async exceptions. |
| 415 | // EHFlags & 2 -> ??? |
| 416 | // EHFlags & 4 -> The function is noexcept(true), unwinding can't continue. |
| 417 | OS.EmitLabel(FuncInfoXData); |
| 418 | OS.EmitIntValue(0x19930522, 4); // MagicNumber |
| 419 | OS.EmitIntValue(FuncInfo.UnwindMap.size(), 4); // MaxState |
| 420 | OS.EmitValue(createImageRel32(UnwindMapXData), 4); // UnwindMap |
| 421 | OS.EmitIntValue(FuncInfo.TryBlockMap.size(), 4); // NumTryBlocks |
| 422 | OS.EmitValue(createImageRel32(TryBlockMapXData), 4); // TryBlockMap |
| 423 | OS.EmitIntValue(FuncInfo.IPToStateList.size(), 4); // IPMapEntries |
| 424 | OS.EmitValue(createImageRel32(IPToStateXData), 4); // IPToStateMap |
| 425 | OS.EmitIntValue(FuncInfo.UnwindHelpFrameOffset, 4); // UnwindHelp |
| 426 | OS.EmitIntValue(0, 4); // ESTypeList |
| 427 | OS.EmitIntValue(1, 4); // EHFlags |
| 428 | |
| 429 | // UnwindMapEntry { |
| 430 | // int32_t ToState; |
| 431 | // void (*Action)(); |
| 432 | // }; |
| 433 | if (UnwindMapXData) { |
| 434 | OS.EmitLabel(UnwindMapXData); |
| 435 | for (const WinEHUnwindMapEntry &UME : FuncInfo.UnwindMap) { |
| 436 | OS.EmitIntValue(UME.ToState, 4); // ToState |
| 437 | OS.EmitValue(createImageRel32(UME.Cleanup), 4); // Action |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // TryBlockMap { |
| 442 | // int32_t TryLow; |
| 443 | // int32_t TryHigh; |
| 444 | // int32_t CatchHigh; |
| 445 | // int32_t NumCatches; |
| 446 | // HandlerType *HandlerArray; |
| 447 | // }; |
| 448 | if (TryBlockMapXData) { |
| 449 | OS.EmitLabel(TryBlockMapXData); |
| 450 | SmallVector<MCSymbol *, 1> HandlerMaps; |
| 451 | for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) { |
| 452 | WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I]; |
| 453 | MCSymbol *HandlerMapXData = nullptr; |
| 454 | |
| 455 | if (!TBME.HandlerArray.empty()) |
| 456 | HandlerMapXData = |
| 457 | Asm->OutContext.GetOrCreateSymbol(Twine("$handlerMap$") |
| 458 | .concat(Twine(I)) |
| 459 | .concat("$") |
| 460 | .concat(ParentLinkageName)); |
| 461 | |
| 462 | HandlerMaps.push_back(HandlerMapXData); |
| 463 | |
David Majnemer | 7f5e714 | 2015-04-03 23:37:34 +0000 | [diff] [blame] | 464 | int CatchHigh = -1; |
| 465 | for (WinEHHandlerType &HT : TBME.HandlerArray) |
| 466 | CatchHigh = |
| 467 | std::max(CatchHigh, FuncInfo.CatchHandlerMaxState[HT.Handler]); |
| 468 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 469 | assert(TBME.TryLow <= TBME.TryHigh); |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 470 | OS.EmitIntValue(TBME.TryLow, 4); // TryLow |
| 471 | OS.EmitIntValue(TBME.TryHigh, 4); // TryHigh |
David Majnemer | 7f5e714 | 2015-04-03 23:37:34 +0000 | [diff] [blame] | 472 | OS.EmitIntValue(CatchHigh, 4); // CatchHigh |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 473 | OS.EmitIntValue(TBME.HandlerArray.size(), 4); // NumCatches |
| 474 | OS.EmitValue(createImageRel32(HandlerMapXData), 4); // HandlerArray |
| 475 | } |
| 476 | |
| 477 | for (size_t I = 0, E = FuncInfo.TryBlockMap.size(); I != E; ++I) { |
| 478 | WinEHTryBlockMapEntry &TBME = FuncInfo.TryBlockMap[I]; |
| 479 | MCSymbol *HandlerMapXData = HandlerMaps[I]; |
| 480 | if (!HandlerMapXData) |
| 481 | continue; |
| 482 | // HandlerType { |
| 483 | // int32_t Adjectives; |
| 484 | // TypeDescriptor *Type; |
| 485 | // int32_t CatchObjOffset; |
| 486 | // void (*Handler)(); |
| 487 | // int32_t ParentFrameOffset; // x64 only |
| 488 | // }; |
| 489 | OS.EmitLabel(HandlerMapXData); |
| 490 | for (const WinEHHandlerType &HT : TBME.HandlerArray) { |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 491 | MCSymbol *ParentFrameOffset = |
| 492 | Asm->OutContext.getOrCreateParentFrameOffsetSymbol( |
| 493 | GlobalValue::getRealLinkageName(HT.Handler->getName())); |
| 494 | const MCSymbolRefExpr *ParentFrameOffsetRef = MCSymbolRefExpr::Create( |
| 495 | ParentFrameOffset, MCSymbolRefExpr::VK_None, Asm->OutContext); |
| 496 | |
Reid Kleckner | f1853c6 | 2015-04-07 19:46:38 +0000 | [diff] [blame] | 497 | // Get the frame escape label with the offset of the catch object. If |
| 498 | // the index is -1, then there is no catch object, and we should emit an |
| 499 | // offset of zero, indicating that no copy will occur. |
| 500 | const MCExpr *FrameAllocOffsetRef = nullptr; |
| 501 | if (HT.CatchObjRecoverIdx >= 0) { |
| 502 | MCSymbol *FrameAllocOffset = |
| 503 | Asm->OutContext.getOrCreateFrameAllocSymbol( |
Reid Kleckner | 6e3b5d4 | 2015-04-15 17:47:26 +0000 | [diff] [blame] | 504 | GlobalValue::getRealLinkageName(ParentF->getName()), |
Reid Kleckner | f1853c6 | 2015-04-07 19:46:38 +0000 | [diff] [blame] | 505 | HT.CatchObjRecoverIdx); |
| 506 | FrameAllocOffsetRef = MCSymbolRefExpr::Create( |
| 507 | FrameAllocOffset, MCSymbolRefExpr::VK_None, Asm->OutContext); |
| 508 | } else { |
| 509 | FrameAllocOffsetRef = MCConstantExpr::Create(0, Asm->OutContext); |
| 510 | } |
David Majnemer | 69132a7 | 2015-04-03 22:49:05 +0000 | [diff] [blame] | 511 | |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 512 | OS.EmitIntValue(HT.Adjectives, 4); // Adjectives |
| 513 | OS.EmitValue(createImageRel32(HT.TypeDescriptor), 4); // Type |
David Majnemer | 69132a7 | 2015-04-03 22:49:05 +0000 | [diff] [blame] | 514 | OS.EmitValue(FrameAllocOffsetRef, 4); // CatchObjOffset |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 515 | OS.EmitValue(createImageRel32(HT.Handler), 4); // Handler |
David Majnemer | a225a19 | 2015-03-31 22:35:44 +0000 | [diff] [blame] | 516 | OS.EmitValue(ParentFrameOffsetRef, 4); // ParentFrameOffset |
David Majnemer | cde3303 | 2015-03-30 22:58:10 +0000 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | // IPToStateMapEntry { |
| 522 | // void *IP; |
| 523 | // int32_t State; |
| 524 | // }; |
| 525 | if (IPToStateXData) { |
| 526 | OS.EmitLabel(IPToStateXData); |
| 527 | for (auto &IPStatePair : FuncInfo.IPToStateList) { |
| 528 | OS.EmitValue(createImageRel32(IPStatePair.first), 4); // IP |
| 529 | OS.EmitIntValue(IPStatePair.second, 4); // State |
| 530 | } |
| 531 | } |
| 532 | } |