1 //===-- CodeGen/AsmPrinter/EHStreamer.cpp - Exception Directive Streamer --===// 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 exception info into assembly files. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "EHStreamer.h" 15 #include "llvm/CodeGen/AsmPrinter.h" 16 #include "llvm/CodeGen/MachineFunction.h" 17 #include "llvm/CodeGen/MachineInstr.h" 18 #include "llvm/CodeGen/MachineModuleInfo.h" 19 #include "llvm/IR/Function.h" 20 #include "llvm/MC/MCAsmInfo.h" 21 #include "llvm/MC/MCStreamer.h" 22 #include "llvm/MC/MCSymbol.h" 23 #include "llvm/Support/LEB128.h" 24 #include "llvm/Target/TargetLoweringObjectFile.h" 25 26 using namespace llvm; 27 28 EHStreamer::EHStreamer(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} 29 30 EHStreamer::~EHStreamer() {} 31 32 /// How many leading type ids two landing pads have in common. 33 unsigned EHStreamer::sharedTypeIDs(const LandingPadInfo *L, 34 const LandingPadInfo *R) { 35 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 36 unsigned LSize = LIds.size(), RSize = RIds.size(); 37 unsigned MinSize = LSize < RSize ? LSize : RSize; 38 unsigned Count = 0; 39 40 for (; Count != MinSize; ++Count) 41 if (LIds[Count] != RIds[Count]) 42 return Count; 43 44 return Count; 45 } 46 47 /// Compute the actions table and gather the first action index for each landing 48 /// pad site. 49 unsigned EHStreamer:: 50 computeActionsTable(const SmallVectorImpl<const LandingPadInfo*> &LandingPads, 51 SmallVectorImpl<ActionEntry> &Actions, 52 SmallVectorImpl<unsigned> &FirstActions) { 53 54 // The action table follows the call-site table in the LSDA. The individual 55 // records are of two types: 56 // 57 // * Catch clause 58 // * Exception specification 59 // 60 // The two record kinds have the same format, with only small differences. 61 // They are distinguished by the "switch value" field: Catch clauses 62 // (TypeInfos) have strictly positive switch values, and exception 63 // specifications (FilterIds) have strictly negative switch values. Value 0 64 // indicates a catch-all clause. 65 // 66 // Negative type IDs index into FilterIds. Positive type IDs index into 67 // TypeInfos. The value written for a positive type ID is just the type ID 68 // itself. For a negative type ID, however, the value written is the 69 // (negative) byte offset of the corresponding FilterIds entry. The byte 70 // offset is usually equal to the type ID (because the FilterIds entries are 71 // written using a variable width encoding, which outputs one byte per entry 72 // as long as the value written is not too large) but can differ. This kind 73 // of complication does not occur for positive type IDs because type infos are 74 // output using a fixed width encoding. FilterOffsets[i] holds the byte 75 // offset corresponding to FilterIds[i]. 76 77 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 78 SmallVector<int, 16> FilterOffsets; 79 FilterOffsets.reserve(FilterIds.size()); 80 int Offset = -1; 81 82 for (std::vector<unsigned>::const_iterator 83 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { 84 FilterOffsets.push_back(Offset); 85 Offset -= getULEB128Size(*I); 86 } 87 88 FirstActions.reserve(LandingPads.size()); 89 90 int FirstAction = 0; 91 unsigned SizeActions = 0; 92 const LandingPadInfo *PrevLPI = nullptr; 93 94 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator 95 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { 96 const LandingPadInfo *LPI = *I; 97 const std::vector<int> &TypeIds = LPI->TypeIds; 98 unsigned NumShared = PrevLPI ? sharedTypeIDs(LPI, PrevLPI) : 0; 99 unsigned SizeSiteActions = 0; 100 101 if (NumShared < TypeIds.size()) { 102 unsigned SizeAction = 0; 103 unsigned PrevAction = (unsigned)-1; 104 105 if (NumShared) { 106 unsigned SizePrevIds = PrevLPI->TypeIds.size(); 107 assert(Actions.size()); 108 PrevAction = Actions.size() - 1; 109 SizeAction = getSLEB128Size(Actions[PrevAction].NextAction) + 110 getSLEB128Size(Actions[PrevAction].ValueForTypeID); 111 112 for (unsigned j = NumShared; j != SizePrevIds; ++j) { 113 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!"); 114 SizeAction -= getSLEB128Size(Actions[PrevAction].ValueForTypeID); 115 SizeAction += -Actions[PrevAction].NextAction; 116 PrevAction = Actions[PrevAction].Previous; 117 } 118 } 119 120 // Compute the actions. 121 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { 122 int TypeID = TypeIds[J]; 123 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); 124 int ValueForTypeID = 125 isFilterEHSelector(TypeID) ? FilterOffsets[-1 - TypeID] : TypeID; 126 unsigned SizeTypeID = getSLEB128Size(ValueForTypeID); 127 128 int NextAction = SizeAction ? -(SizeAction + SizeTypeID) : 0; 129 SizeAction = SizeTypeID + getSLEB128Size(NextAction); 130 SizeSiteActions += SizeAction; 131 132 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; 133 Actions.push_back(Action); 134 PrevAction = Actions.size() - 1; 135 } 136 137 // Record the first action of the landing pad site. 138 FirstAction = SizeActions + SizeSiteActions - SizeAction + 1; 139 } // else identical - re-use previous FirstAction 140 141 // Information used when created the call-site table. The action record 142 // field of the call site record is the offset of the first associated 143 // action record, relative to the start of the actions table. This value is 144 // biased by 1 (1 indicating the start of the actions table), and 0 145 // indicates that there are no actions. 146 FirstActions.push_back(FirstAction); 147 148 // Compute this sites contribution to size. 149 SizeActions += SizeSiteActions; 150 151 PrevLPI = LPI; 152 } 153 154 return SizeActions; 155 } 156 157 /// Return `true' if this is a call to a function marked `nounwind'. Return 158 /// `false' otherwise. 159 bool EHStreamer::callToNoUnwindFunction(const MachineInstr *MI) { 160 assert(MI->isCall() && "This should be a call instruction!"); 161 162 bool MarkedNoUnwind = false; 163 bool SawFunc = false; 164 165 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { 166 const MachineOperand &MO = MI->getOperand(I); 167 168 if (!MO.isGlobal()) continue; 169 170 const Function *F = dyn_cast<Function>(MO.getGlobal()); 171 if (!F) continue; 172 173 if (SawFunc) { 174 // Be conservative. If we have more than one function operand for this 175 // call, then we can't make the assumption that it's the callee and 176 // not a parameter to the call. 177 // 178 // FIXME: Determine if there's a way to say that `F' is the callee or 179 // parameter. 180 MarkedNoUnwind = false; 181 break; 182 } 183 184 MarkedNoUnwind = F->doesNotThrow(); 185 SawFunc = true; 186 } 187 188 return MarkedNoUnwind; 189 } 190 191 /// Compute the call-site table. The entry for an invoke has a try-range 192 /// containing the call, a non-zero landing pad, and an appropriate action. The 193 /// entry for an ordinary call has a try-range containing the call and zero for 194 /// the landing pad and the action. Calls marked 'nounwind' have no entry and 195 /// must not be contained in the try-range of any entry - they form gaps in the 196 /// table. Entries must be ordered by try-range address. 197 void EHStreamer:: 198 computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, 199 const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 200 const SmallVectorImpl<unsigned> &FirstActions) { 201 // Invokes and nounwind calls have entries in PadMap (due to being bracketed 202 // by try-range labels when lowered). Ordinary calls do not, so appropriate 203 // try-ranges for them need be deduced so we can put them in the LSDA. 204 RangeMapType PadMap; 205 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 206 const LandingPadInfo *LandingPad = LandingPads[i]; 207 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 208 MCSymbol *BeginLabel = LandingPad->BeginLabels[j]; 209 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 210 PadRange P = { i, j }; 211 PadMap[BeginLabel] = P; 212 } 213 } 214 215 // The end label of the previous invoke or nounwind try-range. 216 MCSymbol *LastLabel = nullptr; 217 218 // Whether there is a potentially throwing instruction (currently this means 219 // an ordinary call) between the end of the previous try-range and now. 220 bool SawPotentiallyThrowing = false; 221 222 // Whether the last CallSite entry was for an invoke. 223 bool PreviousIsInvoke = false; 224 225 bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 226 227 // Visit all instructions in order of address. 228 for (const auto &MBB : *Asm->MF) { 229 for (const auto &MI : MBB) { 230 if (!MI.isEHLabel()) { 231 if (MI.isCall()) 232 SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI); 233 continue; 234 } 235 236 // End of the previous try-range? 237 MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol(); 238 if (BeginLabel == LastLabel) 239 SawPotentiallyThrowing = false; 240 241 // Beginning of a new try-range? 242 RangeMapType::const_iterator L = PadMap.find(BeginLabel); 243 if (L == PadMap.end()) 244 // Nope, it was just some random label. 245 continue; 246 247 const PadRange &P = L->second; 248 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 249 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 250 "Inconsistent landing pad map!"); 251 252 // For Dwarf exception handling (SjLj handling doesn't use this). If some 253 // instruction between the previous try-range and this one may throw, 254 // create a call-site entry with no landing pad for the region between the 255 // try-ranges. 256 if (SawPotentiallyThrowing && Asm->MAI->usesCFIForEH()) { 257 CallSiteEntry Site = { LastLabel, BeginLabel, nullptr, 0 }; 258 CallSites.push_back(Site); 259 PreviousIsInvoke = false; 260 } 261 262 LastLabel = LandingPad->EndLabels[P.RangeIndex]; 263 assert(BeginLabel && LastLabel && "Invalid landing pad!"); 264 265 if (!LandingPad->LandingPadLabel) { 266 // Create a gap. 267 PreviousIsInvoke = false; 268 } else { 269 // This try-range is for an invoke. 270 CallSiteEntry Site = { 271 BeginLabel, 272 LastLabel, 273 LandingPad, 274 FirstActions[P.PadIndex] 275 }; 276 277 // Try to merge with the previous call-site. SJLJ doesn't do this 278 if (PreviousIsInvoke && !IsSJLJ) { 279 CallSiteEntry &Prev = CallSites.back(); 280 if (Site.LPad == Prev.LPad && Site.Action == Prev.Action) { 281 // Extend the range of the previous entry. 282 Prev.EndLabel = Site.EndLabel; 283 continue; 284 } 285 } 286 287 // Otherwise, create a new call-site. 288 if (!IsSJLJ) 289 CallSites.push_back(Site); 290 else { 291 // SjLj EH must maintain the call sites in the order assigned 292 // to them by the SjLjPrepare pass. 293 unsigned SiteNo = MMI->getCallSiteBeginLabel(BeginLabel); 294 if (CallSites.size() < SiteNo) 295 CallSites.resize(SiteNo); 296 CallSites[SiteNo - 1] = Site; 297 } 298 PreviousIsInvoke = true; 299 } 300 } 301 } 302 303 // If some instruction between the previous try-range and the end of the 304 // function may throw, create a call-site entry with no landing pad for the 305 // region following the try-range. 306 if (SawPotentiallyThrowing && !IsSJLJ) { 307 CallSiteEntry Site = { LastLabel, nullptr, nullptr, 0 }; 308 CallSites.push_back(Site); 309 } 310 } 311 312 /// Emit landing pads and actions. 313 /// 314 /// The general organization of the table is complex, but the basic concepts are 315 /// easy. First there is a header which describes the location and organization 316 /// of the three components that follow. 317 /// 318 /// 1. The landing pad site information describes the range of code covered by 319 /// the try. In our case it's an accumulation of the ranges covered by the 320 /// invokes in the try. There is also a reference to the landing pad that 321 /// handles the exception once processed. Finally an index into the actions 322 /// table. 323 /// 2. The action table, in our case, is composed of pairs of type IDs and next 324 /// action offset. Starting with the action index from the landing pad 325 /// site, each type ID is checked for a match to the current exception. If 326 /// it matches then the exception and type id are passed on to the landing 327 /// pad. Otherwise the next action is looked up. This chain is terminated 328 /// with a next action of zero. If no type id is found then the frame is 329 /// unwound and handling continues. 330 /// 3. Type ID table contains references to all the C++ typeinfo for all 331 /// catches in the function. This tables is reverse indexed base 1. 332 void EHStreamer::emitExceptionTable() { 333 const std::vector<const GlobalValue *> &TypeInfos = MMI->getTypeInfos(); 334 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 335 const std::vector<LandingPadInfo> &PadInfos = MMI->getLandingPads(); 336 337 // Sort the landing pads in order of their type ids. This is used to fold 338 // duplicate actions. 339 SmallVector<const LandingPadInfo *, 64> LandingPads; 340 LandingPads.reserve(PadInfos.size()); 341 342 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) 343 LandingPads.push_back(&PadInfos[i]); 344 345 // Order landing pads lexicographically by type id. 346 std::sort(LandingPads.begin(), LandingPads.end(), 347 [](const LandingPadInfo *L, 348 const LandingPadInfo *R) { return L->TypeIds < R->TypeIds; }); 349 350 // Compute the actions table and gather the first action index for each 351 // landing pad site. 352 SmallVector<ActionEntry, 32> Actions; 353 SmallVector<unsigned, 64> FirstActions; 354 unsigned SizeActions = 355 computeActionsTable(LandingPads, Actions, FirstActions); 356 357 // Compute the call-site table. 358 SmallVector<CallSiteEntry, 64> CallSites; 359 computeCallSiteTable(CallSites, LandingPads, FirstActions); 360 361 // Final tallies. 362 363 // Call sites. 364 bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 365 bool HaveTTData = IsSJLJ ? (!TypeInfos.empty() || !FilterIds.empty()) : true; 366 367 unsigned CallSiteTableLength; 368 if (IsSJLJ) 369 CallSiteTableLength = 0; 370 else { 371 unsigned SiteStartSize = 4; // dwarf::DW_EH_PE_udata4 372 unsigned SiteLengthSize = 4; // dwarf::DW_EH_PE_udata4 373 unsigned LandingPadSize = 4; // dwarf::DW_EH_PE_udata4 374 CallSiteTableLength = 375 CallSites.size() * (SiteStartSize + SiteLengthSize + LandingPadSize); 376 } 377 378 for (unsigned i = 0, e = CallSites.size(); i < e; ++i) { 379 CallSiteTableLength += getULEB128Size(CallSites[i].Action); 380 if (IsSJLJ) 381 CallSiteTableLength += getULEB128Size(i); 382 } 383 384 // Type infos. 385 const MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); 386 unsigned TTypeEncoding; 387 unsigned TypeFormatSize; 388 389 if (!HaveTTData) { 390 // For SjLj exceptions, if there is no TypeInfo, then we just explicitly say 391 // that we're omitting that bit. 392 TTypeEncoding = dwarf::DW_EH_PE_omit; 393 // dwarf::DW_EH_PE_absptr 394 TypeFormatSize = Asm->getDataLayout().getPointerSize(); 395 } else { 396 // Okay, we have actual filters or typeinfos to emit. As such, we need to 397 // pick a type encoding for them. We're about to emit a list of pointers to 398 // typeinfo objects at the end of the LSDA. However, unless we're in static 399 // mode, this reference will require a relocation by the dynamic linker. 400 // 401 // Because of this, we have a couple of options: 402 // 403 // 1) If we are in -static mode, we can always use an absolute reference 404 // from the LSDA, because the static linker will resolve it. 405 // 406 // 2) Otherwise, if the LSDA section is writable, we can output the direct 407 // reference to the typeinfo and allow the dynamic linker to relocate 408 // it. Since it is in a writable section, the dynamic linker won't 409 // have a problem. 410 // 411 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, 412 // we need to use some form of indirection. For example, on Darwin, 413 // we can output a statically-relocatable reference to a dyld stub. The 414 // offset to the stub is constant, but the contents are in a section 415 // that is updated by the dynamic linker. This is easy enough, but we 416 // need to tell the personality function of the unwinder to indirect 417 // through the dyld stub. 418 // 419 // FIXME: When (3) is actually implemented, we'll have to emit the stubs 420 // somewhere. This predicate should be moved to a shared location that is 421 // in target-independent code. 422 // 423 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding(); 424 TypeFormatSize = Asm->GetSizeOfEncodedValue(TTypeEncoding); 425 } 426 427 // Begin the exception table. 428 // Sometimes we want not to emit the data into separate section (e.g. ARM 429 // EHABI). In this case LSDASection will be NULL. 430 if (LSDASection) 431 Asm->OutStreamer.SwitchSection(LSDASection); 432 Asm->EmitAlignment(2); 433 434 // Emit the LSDA. 435 MCSymbol *GCCETSym = 436 Asm->OutContext.GetOrCreateSymbol(Twine("GCC_except_table")+ 437 Twine(Asm->getFunctionNumber())); 438 Asm->OutStreamer.EmitLabel(GCCETSym); 439 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("exception", 440 Asm->getFunctionNumber())); 441 442 if (IsSJLJ) 443 Asm->OutStreamer.EmitLabel(Asm->GetTempSymbol("_LSDA_", 444 Asm->getFunctionNumber())); 445 446 // Emit the LSDA header. 447 Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); 448 Asm->EmitEncodingByte(TTypeEncoding, "@TType"); 449 450 // The type infos need to be aligned. GCC does this by inserting padding just 451 // before the type infos. However, this changes the size of the exception 452 // table, so you need to take this into account when you output the exception 453 // table size. However, the size is output using a variable length encoding. 454 // So by increasing the size by inserting padding, you may increase the number 455 // of bytes used for writing the size. If it increases, say by one byte, then 456 // you now need to output one less byte of padding to get the type infos 457 // aligned. However this decreases the size of the exception table. This 458 // changes the value you have to output for the exception table size. Due to 459 // the variable length encoding, the number of bytes used for writing the 460 // length may decrease. If so, you then have to increase the amount of 461 // padding. And so on. If you look carefully at the GCC code you will see that 462 // it indeed does this in a loop, going on and on until the values stabilize. 463 // We chose another solution: don't output padding inside the table like GCC 464 // does, instead output it before the table. 465 unsigned SizeTypes = TypeInfos.size() * TypeFormatSize; 466 unsigned CallSiteTableLengthSize = getULEB128Size(CallSiteTableLength); 467 unsigned TTypeBaseOffset = 468 sizeof(int8_t) + // Call site format 469 CallSiteTableLengthSize + // Call site table length size 470 CallSiteTableLength + // Call site table length 471 SizeActions + // Actions size 472 SizeTypes; 473 unsigned TTypeBaseOffsetSize = getULEB128Size(TTypeBaseOffset); 474 unsigned TotalSize = 475 sizeof(int8_t) + // LPStart format 476 sizeof(int8_t) + // TType format 477 (HaveTTData ? TTypeBaseOffsetSize : 0) + // TType base offset size 478 TTypeBaseOffset; // TType base offset 479 unsigned SizeAlign = (4 - TotalSize) & 3; 480 481 if (HaveTTData) { 482 // Account for any extra padding that will be added to the call site table 483 // length. 484 Asm->EmitULEB128(TTypeBaseOffset, "@TType base offset", SizeAlign); 485 SizeAlign = 0; 486 } 487 488 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm(); 489 490 // SjLj Exception handling 491 if (IsSJLJ) { 492 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site"); 493 494 // Add extra padding if it wasn't added to the TType base offset. 495 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign); 496 497 // Emit the landing pad site information. 498 unsigned idx = 0; 499 for (SmallVectorImpl<CallSiteEntry>::const_iterator 500 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { 501 const CallSiteEntry &S = *I; 502 503 // Offset of the landing pad, counted in 16-byte bundles relative to the 504 // @LPStart address. 505 if (VerboseAsm) { 506 Asm->OutStreamer.AddComment(">> Call Site " + Twine(idx) + " <<"); 507 Asm->OutStreamer.AddComment(" On exception at call site "+Twine(idx)); 508 } 509 Asm->EmitULEB128(idx); 510 511 // Offset of the first associated action record, relative to the start of 512 // the action table. This value is biased by 1 (1 indicates the start of 513 // the action table), and 0 indicates that there are no actions. 514 if (VerboseAsm) { 515 if (S.Action == 0) 516 Asm->OutStreamer.AddComment(" Action: cleanup"); 517 else 518 Asm->OutStreamer.AddComment(" Action: " + 519 Twine((S.Action - 1) / 2 + 1)); 520 } 521 Asm->EmitULEB128(S.Action); 522 } 523 } else { 524 // Itanium LSDA exception handling 525 526 // The call-site table is a list of all call sites that may throw an 527 // exception (including C++ 'throw' statements) in the procedure 528 // fragment. It immediately follows the LSDA header. Each entry indicates, 529 // for a given call, the first corresponding action record and corresponding 530 // landing pad. 531 // 532 // The table begins with the number of bytes, stored as an LEB128 533 // compressed, unsigned integer. The records immediately follow the record 534 // count. They are sorted in increasing call-site address. Each record 535 // indicates: 536 // 537 // * The position of the call-site. 538 // * The position of the landing pad. 539 // * The first action record for that call site. 540 // 541 // A missing entry in the call-site table indicates that a call is not 542 // supposed to throw. 543 544 // Emit the landing pad call site table. 545 Asm->EmitEncodingByte(dwarf::DW_EH_PE_udata4, "Call site"); 546 547 // Add extra padding if it wasn't added to the TType base offset. 548 Asm->EmitULEB128(CallSiteTableLength, "Call site table length", SizeAlign); 549 550 unsigned Entry = 0; 551 for (SmallVectorImpl<CallSiteEntry>::const_iterator 552 I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { 553 const CallSiteEntry &S = *I; 554 555 MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin(); 556 557 MCSymbol *BeginLabel = S.BeginLabel; 558 if (!BeginLabel) 559 BeginLabel = EHFuncBeginSym; 560 MCSymbol *EndLabel = S.EndLabel; 561 if (!EndLabel) 562 EndLabel = Asm->getFunctionEnd(); 563 564 // Offset of the call site relative to the previous call site, counted in 565 // number of 16-byte bundles. The first call site is counted relative to 566 // the start of the procedure fragment. 567 if (VerboseAsm) 568 Asm->OutStreamer.AddComment(">> Call Site " + Twine(++Entry) + " <<"); 569 Asm->EmitLabelDifference(BeginLabel, EHFuncBeginSym, 4); 570 if (VerboseAsm) 571 Asm->OutStreamer.AddComment(Twine(" Call between ") + 572 BeginLabel->getName() + " and " + 573 EndLabel->getName()); 574 Asm->EmitLabelDifference(EndLabel, BeginLabel, 4); 575 576 // Offset of the landing pad, counted in 16-byte bundles relative to the 577 // @LPStart address. 578 if (!S.LPad) { 579 if (VerboseAsm) 580 Asm->OutStreamer.AddComment(" has no landing pad"); 581 Asm->OutStreamer.EmitIntValue(0, 4/*size*/); 582 } else { 583 if (VerboseAsm) 584 Asm->OutStreamer.AddComment(Twine(" jumps to ") + 585 S.LPad->LandingPadLabel->getName()); 586 Asm->EmitLabelDifference(S.LPad->LandingPadLabel, EHFuncBeginSym, 4); 587 } 588 589 // Offset of the first associated action record, relative to the start of 590 // the action table. This value is biased by 1 (1 indicates the start of 591 // the action table), and 0 indicates that there are no actions. 592 if (VerboseAsm) { 593 if (S.Action == 0) 594 Asm->OutStreamer.AddComment(" On action: cleanup"); 595 else 596 Asm->OutStreamer.AddComment(" On action: " + 597 Twine((S.Action - 1) / 2 + 1)); 598 } 599 Asm->EmitULEB128(S.Action); 600 } 601 } 602 603 // Emit the Action Table. 604 int Entry = 0; 605 for (SmallVectorImpl<ActionEntry>::const_iterator 606 I = Actions.begin(), E = Actions.end(); I != E; ++I) { 607 const ActionEntry &Action = *I; 608 609 if (VerboseAsm) { 610 // Emit comments that decode the action table. 611 Asm->OutStreamer.AddComment(">> Action Record " + Twine(++Entry) + " <<"); 612 } 613 614 // Type Filter 615 // 616 // Used by the runtime to match the type of the thrown exception to the 617 // type of the catch clauses or the types in the exception specification. 618 if (VerboseAsm) { 619 if (Action.ValueForTypeID > 0) 620 Asm->OutStreamer.AddComment(" Catch TypeInfo " + 621 Twine(Action.ValueForTypeID)); 622 else if (Action.ValueForTypeID < 0) 623 Asm->OutStreamer.AddComment(" Filter TypeInfo " + 624 Twine(Action.ValueForTypeID)); 625 else 626 Asm->OutStreamer.AddComment(" Cleanup"); 627 } 628 Asm->EmitSLEB128(Action.ValueForTypeID); 629 630 // Action Record 631 // 632 // Self-relative signed displacement in bytes of the next action record, 633 // or 0 if there is no next action record. 634 if (VerboseAsm) { 635 if (Action.NextAction == 0) { 636 Asm->OutStreamer.AddComment(" No further actions"); 637 } else { 638 unsigned NextAction = Entry + (Action.NextAction + 1) / 2; 639 Asm->OutStreamer.AddComment(" Continue to action "+Twine(NextAction)); 640 } 641 } 642 Asm->EmitSLEB128(Action.NextAction); 643 } 644 645 emitTypeInfos(TTypeEncoding); 646 647 Asm->EmitAlignment(2); 648 } 649 650 void EHStreamer::emitTypeInfos(unsigned TTypeEncoding) { 651 const std::vector<const GlobalValue *> &TypeInfos = MMI->getTypeInfos(); 652 const std::vector<unsigned> &FilterIds = MMI->getFilterIds(); 653 654 bool VerboseAsm = Asm->OutStreamer.isVerboseAsm(); 655 656 int Entry = 0; 657 // Emit the Catch TypeInfos. 658 if (VerboseAsm && !TypeInfos.empty()) { 659 Asm->OutStreamer.AddComment(">> Catch TypeInfos <<"); 660 Asm->OutStreamer.AddBlankLine(); 661 Entry = TypeInfos.size(); 662 } 663 664 for (std::vector<const GlobalValue *>::const_reverse_iterator 665 I = TypeInfos.rbegin(), E = TypeInfos.rend(); I != E; ++I) { 666 const GlobalValue *GV = *I; 667 if (VerboseAsm) 668 Asm->OutStreamer.AddComment("TypeInfo " + Twine(Entry--)); 669 Asm->EmitTTypeReference(GV, TTypeEncoding); 670 } 671 672 // Emit the Exception Specifications. 673 if (VerboseAsm && !FilterIds.empty()) { 674 Asm->OutStreamer.AddComment(">> Filter TypeInfos <<"); 675 Asm->OutStreamer.AddBlankLine(); 676 Entry = 0; 677 } 678 for (std::vector<unsigned>::const_iterator 679 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 680 unsigned TypeID = *I; 681 if (VerboseAsm) { 682 --Entry; 683 if (isFilterEHSelector(TypeID)) 684 Asm->OutStreamer.AddComment("FilterInfo " + Twine(Entry)); 685 } 686 687 Asm->EmitULEB128(TypeID); 688 } 689 } 690 691 /// Emit all exception information that should come after the content. 692 void EHStreamer::endModule() { 693 llvm_unreachable("Should be implemented"); 694 } 695 696 /// Gather pre-function exception information. Assumes it's being emitted 697 /// immediately after the function entry point. 698 void EHStreamer::beginFunction(const MachineFunction *MF) { 699 llvm_unreachable("Should be implemented"); 700 } 701 702 /// Gather and emit post-function exception information. 703 void EHStreamer::endFunction(const MachineFunction *) { 704 llvm_unreachable("Should be implemented"); 705 } 706