1 //===- CodeGen/AsmPrinter/EHStreamer.cpp - Exception Directive Streamer ---===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains support for writing exception info into assembly files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "EHStreamer.h" 14 #include "llvm/ADT/SmallVector.h" 15 #include "llvm/ADT/Twine.h" 16 #include "llvm/ADT/iterator_range.h" 17 #include "llvm/BinaryFormat/Dwarf.h" 18 #include "llvm/CodeGen/AsmPrinter.h" 19 #include "llvm/CodeGen/MachineFunction.h" 20 #include "llvm/CodeGen/MachineInstr.h" 21 #include "llvm/CodeGen/MachineOperand.h" 22 #include "llvm/IR/DataLayout.h" 23 #include "llvm/IR/Function.h" 24 #include "llvm/MC/MCAsmInfo.h" 25 #include "llvm/MC/MCContext.h" 26 #include "llvm/MC/MCStreamer.h" 27 #include "llvm/MC/MCSymbol.h" 28 #include "llvm/MC/MCTargetOptions.h" 29 #include "llvm/Support/Casting.h" 30 #include "llvm/Support/LEB128.h" 31 #include "llvm/Target/TargetLoweringObjectFile.h" 32 #include <algorithm> 33 #include <cassert> 34 #include <cstdint> 35 #include <vector> 36 37 using namespace llvm; 38 39 EHStreamer::EHStreamer(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} 40 41 EHStreamer::~EHStreamer() = default; 42 43 /// How many leading type ids two landing pads have in common. 44 unsigned EHStreamer::sharedTypeIDs(const LandingPadInfo *L, 45 const LandingPadInfo *R) { 46 const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 47 return std::mismatch(LIds.begin(), LIds.end(), RIds.begin(), RIds.end()) 48 .first - 49 LIds.begin(); 50 } 51 52 /// Compute the actions table and gather the first action index for each landing 53 /// pad site. 54 void EHStreamer::computeActionsTable( 55 const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 56 SmallVectorImpl<ActionEntry> &Actions, 57 SmallVectorImpl<unsigned> &FirstActions) { 58 // The action table follows the call-site table in the LSDA. The individual 59 // records are of two types: 60 // 61 // * Catch clause 62 // * Exception specification 63 // 64 // The two record kinds have the same format, with only small differences. 65 // They are distinguished by the "switch value" field: Catch clauses 66 // (TypeInfos) have strictly positive switch values, and exception 67 // specifications (FilterIds) have strictly negative switch values. Value 0 68 // indicates a catch-all clause. 69 // 70 // Negative type IDs index into FilterIds. Positive type IDs index into 71 // TypeInfos. The value written for a positive type ID is just the type ID 72 // itself. For a negative type ID, however, the value written is the 73 // (negative) byte offset of the corresponding FilterIds entry. The byte 74 // offset is usually equal to the type ID (because the FilterIds entries are 75 // written using a variable width encoding, which outputs one byte per entry 76 // as long as the value written is not too large) but can differ. This kind 77 // of complication does not occur for positive type IDs because type infos are 78 // output using a fixed width encoding. FilterOffsets[i] holds the byte 79 // offset corresponding to FilterIds[i]. 80 81 const std::vector<unsigned> &FilterIds = Asm->MF->getFilterIds(); 82 SmallVector<int, 16> FilterOffsets; 83 FilterOffsets.reserve(FilterIds.size()); 84 int Offset = -1; 85 86 for (std::vector<unsigned>::const_iterator 87 I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { 88 FilterOffsets.push_back(Offset); 89 Offset -= getULEB128Size(*I); 90 } 91 92 FirstActions.reserve(LandingPads.size()); 93 94 int FirstAction = 0; 95 unsigned SizeActions = 0; // Total size of all action entries for a function 96 const LandingPadInfo *PrevLPI = nullptr; 97 98 for (SmallVectorImpl<const LandingPadInfo *>::const_iterator 99 I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { 100 const LandingPadInfo *LPI = *I; 101 const std::vector<int> &TypeIds = LPI->TypeIds; 102 unsigned NumShared = PrevLPI ? sharedTypeIDs(LPI, PrevLPI) : 0; 103 unsigned SizeSiteActions = 0; // Total size of all entries for a landingpad 104 105 if (NumShared < TypeIds.size()) { 106 // Size of one action entry (typeid + next action) 107 unsigned SizeActionEntry = 0; 108 unsigned PrevAction = (unsigned)-1; 109 110 if (NumShared) { 111 unsigned SizePrevIds = PrevLPI->TypeIds.size(); 112 assert(Actions.size()); 113 PrevAction = Actions.size() - 1; 114 SizeActionEntry = getSLEB128Size(Actions[PrevAction].NextAction) + 115 getSLEB128Size(Actions[PrevAction].ValueForTypeID); 116 117 for (unsigned j = NumShared; j != SizePrevIds; ++j) { 118 assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!"); 119 SizeActionEntry -= getSLEB128Size(Actions[PrevAction].ValueForTypeID); 120 SizeActionEntry += -Actions[PrevAction].NextAction; 121 PrevAction = Actions[PrevAction].Previous; 122 } 123 } 124 125 // Compute the actions. 126 for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { 127 int TypeID = TypeIds[J]; 128 assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); 129 int ValueForTypeID = 130 isFilterEHSelector(TypeID) ? FilterOffsets[-1 - TypeID] : TypeID; 131 unsigned SizeTypeID = getSLEB128Size(ValueForTypeID); 132 133 int NextAction = SizeActionEntry ? -(SizeActionEntry + SizeTypeID) : 0; 134 SizeActionEntry = SizeTypeID + getSLEB128Size(NextAction); 135 SizeSiteActions += SizeActionEntry; 136 137 ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; 138 Actions.push_back(Action); 139 PrevAction = Actions.size() - 1; 140 } 141 142 // Record the first action of the landing pad site. 143 FirstAction = SizeActions + SizeSiteActions - SizeActionEntry + 1; 144 } // else identical - re-use previous FirstAction 145 146 // Information used when creating the call-site table. The action record 147 // field of the call site record is the offset of the first associated 148 // action record, relative to the start of the actions table. This value is 149 // biased by 1 (1 indicating the start of the actions table), and 0 150 // indicates that there are no actions. 151 FirstActions.push_back(FirstAction); 152 153 // Compute this sites contribution to size. 154 SizeActions += SizeSiteActions; 155 156 PrevLPI = LPI; 157 } 158 } 159 160 /// Return `true' if this is a call to a function marked `nounwind'. Return 161 /// `false' otherwise. 162 bool EHStreamer::callToNoUnwindFunction(const MachineInstr *MI) { 163 assert(MI->isCall() && "This should be a call instruction!"); 164 165 bool MarkedNoUnwind = false; 166 bool SawFunc = false; 167 168 for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { 169 const MachineOperand &MO = MI->getOperand(I); 170 171 if (!MO.isGlobal()) continue; 172 173 const Function *F = dyn_cast<Function>(MO.getGlobal()); 174 if (!F) continue; 175 176 if (SawFunc) { 177 // Be conservative. If we have more than one function operand for this 178 // call, then we can't make the assumption that it's the callee and 179 // not a parameter to the call. 180 // 181 // FIXME: Determine if there's a way to say that `F' is the callee or 182 // parameter. 183 MarkedNoUnwind = false; 184 break; 185 } 186 187 MarkedNoUnwind = F->doesNotThrow(); 188 SawFunc = true; 189 } 190 191 return MarkedNoUnwind; 192 } 193 194 void EHStreamer::computePadMap( 195 const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 196 RangeMapType &PadMap) { 197 // Invokes and nounwind calls have entries in PadMap (due to being bracketed 198 // by try-range labels when lowered). Ordinary calls do not, so appropriate 199 // try-ranges for them need be deduced so we can put them in the LSDA. 200 for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 201 const LandingPadInfo *LandingPad = LandingPads[i]; 202 for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 203 MCSymbol *BeginLabel = LandingPad->BeginLabels[j]; 204 assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 205 PadRange P = { i, j }; 206 PadMap[BeginLabel] = P; 207 } 208 } 209 } 210 211 /// Compute the call-site table. The entry for an invoke has a try-range 212 /// containing the call, a non-zero landing pad, and an appropriate action. The 213 /// entry for an ordinary call has a try-range containing the call and zero for 214 /// the landing pad and the action. Calls marked 'nounwind' have no entry and 215 /// must not be contained in the try-range of any entry - they form gaps in the 216 /// table. Entries must be ordered by try-range address. 217 /// 218 /// Call-sites are split into one or more call-site ranges associated with 219 /// different sections of the function. 220 /// 221 /// - Without -basic-block-sections, all call-sites are grouped into one 222 /// call-site-range corresponding to the function section. 223 /// 224 /// - With -basic-block-sections, one call-site range is created for each 225 /// section, with its FragmentBeginLabel and FragmentEndLabel respectively 226 // set to the beginning and ending of the corresponding section and its 227 // ExceptionLabel set to the exception symbol dedicated for this section. 228 // Later, one LSDA header will be emitted for each call-site range with its 229 // call-sites following. The action table and type info table will be 230 // shared across all ranges. 231 void EHStreamer::computeCallSiteTable( 232 SmallVectorImpl<CallSiteEntry> &CallSites, 233 SmallVectorImpl<CallSiteRange> &CallSiteRanges, 234 const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 235 const SmallVectorImpl<unsigned> &FirstActions) { 236 RangeMapType PadMap; 237 computePadMap(LandingPads, PadMap); 238 239 // The end label of the previous invoke or nounwind try-range. 240 MCSymbol *LastLabel = Asm->getFunctionBegin(); 241 242 // Whether there is a potentially throwing instruction (currently this means 243 // an ordinary call) between the end of the previous try-range and now. 244 bool SawPotentiallyThrowing = false; 245 246 // Whether the last CallSite entry was for an invoke. 247 bool PreviousIsInvoke = false; 248 249 bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 250 251 // Visit all instructions in order of address. 252 for (const auto &MBB : *Asm->MF) { 253 if (&MBB == &Asm->MF->front() || MBB.isBeginSection()) { 254 // We start a call-site range upon function entry and at the beginning of 255 // every basic block section. 256 CallSiteRanges.push_back( 257 {Asm->MBBSectionRanges[MBB.getSectionIDNum()].BeginLabel, 258 Asm->MBBSectionRanges[MBB.getSectionIDNum()].EndLabel, 259 Asm->getMBBExceptionSym(MBB), CallSites.size()}); 260 PreviousIsInvoke = false; 261 SawPotentiallyThrowing = false; 262 LastLabel = nullptr; 263 } 264 265 if (MBB.isEHPad()) 266 CallSiteRanges.back().IsLPRange = true; 267 268 for (const auto &MI : MBB) { 269 if (!MI.isEHLabel()) { 270 if (MI.isCall()) 271 SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI); 272 continue; 273 } 274 275 // End of the previous try-range? 276 MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol(); 277 if (BeginLabel == LastLabel) 278 SawPotentiallyThrowing = false; 279 280 // Beginning of a new try-range? 281 RangeMapType::const_iterator L = PadMap.find(BeginLabel); 282 if (L == PadMap.end()) 283 // Nope, it was just some random label. 284 continue; 285 286 const PadRange &P = L->second; 287 const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 288 assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 289 "Inconsistent landing pad map!"); 290 291 // For Dwarf and AIX exception handling (SjLj handling doesn't use this). 292 // If some instruction between the previous try-range and this one may 293 // throw, create a call-site entry with no landing pad for the region 294 // between the try-ranges. 295 if (SawPotentiallyThrowing && 296 (Asm->MAI->usesCFIForEH() || 297 Asm->MAI->getExceptionHandlingType() == ExceptionHandling::AIX)) { 298 CallSites.push_back({LastLabel, BeginLabel, nullptr, 0}); 299 PreviousIsInvoke = false; 300 } 301 302 LastLabel = LandingPad->EndLabels[P.RangeIndex]; 303 assert(BeginLabel && LastLabel && "Invalid landing pad!"); 304 305 if (!LandingPad->LandingPadLabel) { 306 // Create a gap. 307 PreviousIsInvoke = false; 308 } else { 309 // This try-range is for an invoke. 310 CallSiteEntry Site = { 311 BeginLabel, 312 LastLabel, 313 LandingPad, 314 FirstActions[P.PadIndex] 315 }; 316 317 // Try to merge with the previous call-site. SJLJ doesn't do this 318 if (PreviousIsInvoke && !IsSJLJ) { 319 CallSiteEntry &Prev = CallSites.back(); 320 if (Site.LPad == Prev.LPad && Site.Action == Prev.Action) { 321 // Extend the range of the previous entry. 322 Prev.EndLabel = Site.EndLabel; 323 continue; 324 } 325 } 326 327 // Otherwise, create a new call-site. 328 if (!IsSJLJ) 329 CallSites.push_back(Site); 330 else { 331 // SjLj EH must maintain the call sites in the order assigned 332 // to them by the SjLjPrepare pass. 333 unsigned SiteNo = Asm->MF->getCallSiteBeginLabel(BeginLabel); 334 if (CallSites.size() < SiteNo) 335 CallSites.resize(SiteNo); 336 CallSites[SiteNo - 1] = Site; 337 } 338 PreviousIsInvoke = true; 339 } 340 } 341 342 // We end the call-site range upon function exit and at the end of every 343 // basic block section. 344 if (&MBB == &Asm->MF->back() || MBB.isEndSection()) { 345 // If some instruction between the previous try-range and the end of the 346 // function may throw, create a call-site entry with no landing pad for 347 // the region following the try-range. 348 if (SawPotentiallyThrowing && !IsSJLJ) { 349 CallSiteEntry Site = {LastLabel, CallSiteRanges.back().FragmentEndLabel, 350 nullptr, 0}; 351 CallSites.push_back(Site); 352 SawPotentiallyThrowing = false; 353 } 354 CallSiteRanges.back().CallSiteEndIdx = CallSites.size(); 355 } 356 } 357 } 358 359 /// Emit landing pads and actions. 360 /// 361 /// The general organization of the table is complex, but the basic concepts are 362 /// easy. First there is a header which describes the location and organization 363 /// of the three components that follow. 364 /// 365 /// 1. The landing pad site information describes the range of code covered by 366 /// the try. In our case it's an accumulation of the ranges covered by the 367 /// invokes in the try. There is also a reference to the landing pad that 368 /// handles the exception once processed. Finally an index into the actions 369 /// table. 370 /// 2. The action table, in our case, is composed of pairs of type IDs and next 371 /// action offset. Starting with the action index from the landing pad 372 /// site, each type ID is checked for a match to the current exception. If 373 /// it matches then the exception and type id are passed on to the landing 374 /// pad. Otherwise the next action is looked up. This chain is terminated 375 /// with a next action of zero. If no type id is found then the frame is 376 /// unwound and handling continues. 377 /// 3. Type ID table contains references to all the C++ typeinfo for all 378 /// catches in the function. This tables is reverse indexed base 1. 379 /// 380 /// Returns the starting symbol of an exception table. 381 MCSymbol *EHStreamer::emitExceptionTable() { 382 const MachineFunction *MF = Asm->MF; 383 const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); 384 const std::vector<unsigned> &FilterIds = MF->getFilterIds(); 385 const std::vector<LandingPadInfo> &PadInfos = MF->getLandingPads(); 386 387 // Sort the landing pads in order of their type ids. This is used to fold 388 // duplicate actions. 389 SmallVector<const LandingPadInfo *, 64> LandingPads; 390 LandingPads.reserve(PadInfos.size()); 391 392 for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) 393 LandingPads.push_back(&PadInfos[i]); 394 395 // Order landing pads lexicographically by type id. 396 llvm::sort(LandingPads, [](const LandingPadInfo *L, const LandingPadInfo *R) { 397 return L->TypeIds < R->TypeIds; 398 }); 399 400 // Compute the actions table and gather the first action index for each 401 // landing pad site. 402 SmallVector<ActionEntry, 32> Actions; 403 SmallVector<unsigned, 64> FirstActions; 404 computeActionsTable(LandingPads, Actions, FirstActions); 405 406 // Compute the call-site table and call-site ranges. Normally, there is only 407 // one call-site-range which covers the whole funciton. With 408 // -basic-block-sections, there is one call-site-range per basic block 409 // section. 410 SmallVector<CallSiteEntry, 64> CallSites; 411 SmallVector<CallSiteRange, 4> CallSiteRanges; 412 computeCallSiteTable(CallSites, CallSiteRanges, LandingPads, FirstActions); 413 414 bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 415 bool IsWasm = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::Wasm; 416 bool HasLEB128Directives = Asm->MAI->hasLEB128Directives(); 417 unsigned CallSiteEncoding = 418 IsSJLJ ? static_cast<unsigned>(dwarf::DW_EH_PE_udata4) : 419 Asm->getObjFileLowering().getCallSiteEncoding(); 420 bool HaveTTData = !TypeInfos.empty() || !FilterIds.empty(); 421 422 // Type infos. 423 MCSection *LSDASection = 424 Asm->getObjFileLowering().getSectionForLSDA(MF->getFunction(), Asm->TM); 425 unsigned TTypeEncoding; 426 427 if (!HaveTTData) { 428 // If there is no TypeInfo, then we just explicitly say that we're omitting 429 // that bit. 430 TTypeEncoding = dwarf::DW_EH_PE_omit; 431 } else { 432 // Okay, we have actual filters or typeinfos to emit. As such, we need to 433 // pick a type encoding for them. We're about to emit a list of pointers to 434 // typeinfo objects at the end of the LSDA. However, unless we're in static 435 // mode, this reference will require a relocation by the dynamic linker. 436 // 437 // Because of this, we have a couple of options: 438 // 439 // 1) If we are in -static mode, we can always use an absolute reference 440 // from the LSDA, because the static linker will resolve it. 441 // 442 // 2) Otherwise, if the LSDA section is writable, we can output the direct 443 // reference to the typeinfo and allow the dynamic linker to relocate 444 // it. Since it is in a writable section, the dynamic linker won't 445 // have a problem. 446 // 447 // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, 448 // we need to use some form of indirection. For example, on Darwin, 449 // we can output a statically-relocatable reference to a dyld stub. The 450 // offset to the stub is constant, but the contents are in a section 451 // that is updated by the dynamic linker. This is easy enough, but we 452 // need to tell the personality function of the unwinder to indirect 453 // through the dyld stub. 454 // 455 // FIXME: When (3) is actually implemented, we'll have to emit the stubs 456 // somewhere. This predicate should be moved to a shared location that is 457 // in target-independent code. 458 // 459 TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding(); 460 } 461 462 // Begin the exception table. 463 // Sometimes we want not to emit the data into separate section (e.g. ARM 464 // EHABI). In this case LSDASection will be NULL. 465 if (LSDASection) 466 Asm->OutStreamer->SwitchSection(LSDASection); 467 Asm->emitAlignment(Align(4)); 468 469 // Emit the LSDA. 470 MCSymbol *GCCETSym = 471 Asm->OutContext.getOrCreateSymbol(Twine("GCC_except_table")+ 472 Twine(Asm->getFunctionNumber())); 473 Asm->OutStreamer->emitLabel(GCCETSym); 474 MCSymbol *CstEndLabel = Asm->createTempSymbol( 475 CallSiteRanges.size() > 1 ? "action_table_base" : "cst_end"); 476 477 MCSymbol *TTBaseLabel = nullptr; 478 if (HaveTTData) 479 TTBaseLabel = Asm->createTempSymbol("ttbase"); 480 481 const bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); 482 483 // Helper for emitting references (offsets) for type table and the end of the 484 // call-site table (which marks the beginning of the action table). 485 // * For Itanium, these references will be emitted for every callsite range. 486 // * For SJLJ and Wasm, they will be emitted only once in the LSDA header. 487 auto EmitTypeTableRefAndCallSiteTableEndRef = [&]() { 488 Asm->emitEncodingByte(TTypeEncoding, "@TType"); 489 if (HaveTTData) { 490 // N.B.: There is a dependency loop between the size of the TTBase uleb128 491 // here and the amount of padding before the aligned type table. The 492 // assembler must sometimes pad this uleb128 or insert extra padding 493 // before the type table. See PR35809 or GNU as bug 4029. 494 MCSymbol *TTBaseRefLabel = Asm->createTempSymbol("ttbaseref"); 495 Asm->emitLabelDifferenceAsULEB128(TTBaseLabel, TTBaseRefLabel); 496 Asm->OutStreamer->emitLabel(TTBaseRefLabel); 497 } 498 499 // The Action table follows the call-site table. So we emit the 500 // label difference from here (start of the call-site table for SJLJ and 501 // Wasm, and start of a call-site range for Itanium) to the end of the 502 // whole call-site table (end of the last call-site range for Itanium). 503 MCSymbol *CstBeginLabel = Asm->createTempSymbol("cst_begin"); 504 Asm->emitEncodingByte(CallSiteEncoding, "Call site"); 505 Asm->emitLabelDifferenceAsULEB128(CstEndLabel, CstBeginLabel); 506 Asm->OutStreamer->emitLabel(CstBeginLabel); 507 }; 508 509 // An alternative path to EmitTypeTableRefAndCallSiteTableEndRef. 510 // For some platforms, the system assembler does not accept the form of 511 // `.uleb128 label2 - label1`. In those situations, we would need to calculate 512 // the size between label1 and label2 manually. 513 // In this case, we would need to calculate the LSDA size and the call 514 // site table size. 515 auto EmitTypeTableOffsetAndCallSiteTableOffset = [&]() { 516 assert(CallSiteEncoding == dwarf::DW_EH_PE_udata4 && !HasLEB128Directives && 517 "Targets supporting .uleb128 do not need to take this path."); 518 if (CallSiteRanges.size() > 1) 519 report_fatal_error( 520 "-fbasic-block-sections is not yet supported on " 521 "platforms that do not have general LEB128 directive support."); 522 523 uint64_t CallSiteTableSize = 0; 524 const CallSiteRange &CSRange = CallSiteRanges.back(); 525 for (size_t CallSiteIdx = CSRange.CallSiteBeginIdx; 526 CallSiteIdx < CSRange.CallSiteEndIdx; ++CallSiteIdx) { 527 const CallSiteEntry &S = CallSites[CallSiteIdx]; 528 // Each call site entry consists of 3 udata4 fields (12 bytes) and 529 // 1 ULEB128 field. 530 CallSiteTableSize += 12 + getULEB128Size(S.Action); 531 assert(isUInt<32>(CallSiteTableSize) && "CallSiteTableSize overflows."); 532 } 533 534 Asm->emitEncodingByte(TTypeEncoding, "@TType"); 535 if (HaveTTData) { 536 const unsigned ByteSizeOfCallSiteOffset = 537 getULEB128Size(CallSiteTableSize); 538 uint64_t ActionTableSize = 0; 539 for (const ActionEntry &Action : Actions) { 540 // Each action entry consists of two SLEB128 fields. 541 ActionTableSize += getSLEB128Size(Action.ValueForTypeID) + 542 getSLEB128Size(Action.NextAction); 543 assert(isUInt<32>(ActionTableSize) && "ActionTableSize overflows."); 544 } 545 546 const unsigned TypeInfoSize = 547 Asm->GetSizeOfEncodedValue(TTypeEncoding) * MF->getTypeInfos().size(); 548 549 const uint64_t LSDASizeBeforeAlign = 550 1 // Call site encoding byte. 551 + ByteSizeOfCallSiteOffset // ULEB128 encoding of CallSiteTableSize. 552 + CallSiteTableSize // Call site table content. 553 + ActionTableSize; // Action table content. 554 555 const uint64_t LSDASizeWithoutAlign = LSDASizeBeforeAlign + TypeInfoSize; 556 const unsigned ByteSizeOfLSDAWithoutAlign = 557 getULEB128Size(LSDASizeWithoutAlign); 558 const uint64_t DisplacementBeforeAlign = 559 2 // LPStartEncoding and TypeTableEncoding. 560 + ByteSizeOfLSDAWithoutAlign + LSDASizeBeforeAlign; 561 562 // The type info area starts with 4 byte alignment. 563 const unsigned NeedAlignVal = (4 - DisplacementBeforeAlign % 4) % 4; 564 uint64_t LSDASizeWithAlign = LSDASizeWithoutAlign + NeedAlignVal; 565 const unsigned ByteSizeOfLSDAWithAlign = 566 getULEB128Size(LSDASizeWithAlign); 567 568 // The LSDASizeWithAlign could use 1 byte less padding for alignment 569 // when the data we use to represent the LSDA Size "needs" to be 1 byte 570 // larger than the one previously calculated without alignment. 571 if (ByteSizeOfLSDAWithAlign > ByteSizeOfLSDAWithoutAlign) 572 LSDASizeWithAlign -= 1; 573 574 Asm->OutStreamer->emitULEB128IntValue(LSDASizeWithAlign, 575 ByteSizeOfLSDAWithAlign); 576 } 577 578 Asm->emitEncodingByte(CallSiteEncoding, "Call site"); 579 Asm->OutStreamer->emitULEB128IntValue(CallSiteTableSize); 580 }; 581 582 // SjLj / Wasm Exception handling 583 if (IsSJLJ || IsWasm) { 584 Asm->OutStreamer->emitLabel(Asm->getMBBExceptionSym(Asm->MF->front())); 585 586 // emit the LSDA header. 587 Asm->emitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); 588 EmitTypeTableRefAndCallSiteTableEndRef(); 589 590 unsigned idx = 0; 591 for (SmallVectorImpl<CallSiteEntry>::const_iterator 592 I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { 593 const CallSiteEntry &S = *I; 594 595 // Index of the call site entry. 596 if (VerboseAsm) { 597 Asm->OutStreamer->AddComment(">> Call Site " + Twine(idx) + " <<"); 598 Asm->OutStreamer->AddComment(" On exception at call site "+Twine(idx)); 599 } 600 Asm->emitULEB128(idx); 601 602 // Offset of the first associated action record, relative to the start of 603 // the action table. This value is biased by 1 (1 indicates the start of 604 // the action table), and 0 indicates that there are no actions. 605 if (VerboseAsm) { 606 if (S.Action == 0) 607 Asm->OutStreamer->AddComment(" Action: cleanup"); 608 else 609 Asm->OutStreamer->AddComment(" Action: " + 610 Twine((S.Action - 1) / 2 + 1)); 611 } 612 Asm->emitULEB128(S.Action); 613 } 614 Asm->OutStreamer->emitLabel(CstEndLabel); 615 } else { 616 // Itanium LSDA exception handling 617 618 // The call-site table is a list of all call sites that may throw an 619 // exception (including C++ 'throw' statements) in the procedure 620 // fragment. It immediately follows the LSDA header. Each entry indicates, 621 // for a given call, the first corresponding action record and corresponding 622 // landing pad. 623 // 624 // The table begins with the number of bytes, stored as an LEB128 625 // compressed, unsigned integer. The records immediately follow the record 626 // count. They are sorted in increasing call-site address. Each record 627 // indicates: 628 // 629 // * The position of the call-site. 630 // * The position of the landing pad. 631 // * The first action record for that call site. 632 // 633 // A missing entry in the call-site table indicates that a call is not 634 // supposed to throw. 635 636 assert(CallSiteRanges.size() != 0 && "No call-site ranges!"); 637 638 // There should be only one call-site range which includes all the landing 639 // pads. Find that call-site range here. 640 const CallSiteRange *LandingPadRange = nullptr; 641 for (const CallSiteRange &CSRange : CallSiteRanges) { 642 if (CSRange.IsLPRange) { 643 assert(LandingPadRange == nullptr && 644 "All landing pads must be in a single callsite range."); 645 LandingPadRange = &CSRange; 646 } 647 } 648 649 // The call-site table is split into its call-site ranges, each being 650 // emitted as: 651 // [ LPStartEncoding | LPStart ] 652 // [ TypeTableEncoding | TypeTableOffset ] 653 // [ CallSiteEncoding | CallSiteTableEndOffset ] 654 // cst_begin -> { call-site entries contained in this range } 655 // 656 // and is followed by the next call-site range. 657 // 658 // For each call-site range, CallSiteTableEndOffset is computed as the 659 // difference between cst_begin of that range and the last call-site-table's 660 // end label. This offset is used to find the action table. 661 662 unsigned Entry = 0; 663 for (const CallSiteRange &CSRange : CallSiteRanges) { 664 if (CSRange.CallSiteBeginIdx != 0) { 665 // Align the call-site range for all ranges except the first. The 666 // first range is already aligned due to the exception table alignment. 667 Asm->emitAlignment(Align(4)); 668 } 669 Asm->OutStreamer->emitLabel(CSRange.ExceptionLabel); 670 671 // Emit the LSDA header. 672 // If only one call-site range exists, LPStart is omitted as it is the 673 // same as the function entry. 674 if (CallSiteRanges.size() == 1) { 675 Asm->emitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); 676 } else if (!Asm->isPositionIndependent()) { 677 // For more than one call-site ranges, LPStart must be explicitly 678 // specified. 679 // For non-PIC we can simply use the absolute value. 680 Asm->emitEncodingByte(dwarf::DW_EH_PE_absptr, "@LPStart"); 681 Asm->OutStreamer->emitSymbolValue(LandingPadRange->FragmentBeginLabel, 682 Asm->MAI->getCodePointerSize()); 683 } else { 684 // For PIC mode, we Emit a PC-relative address for LPStart. 685 Asm->emitEncodingByte(dwarf::DW_EH_PE_pcrel, "@LPStart"); 686 MCContext &Context = Asm->OutStreamer->getContext(); 687 MCSymbol *Dot = Context.createTempSymbol(); 688 Asm->OutStreamer->emitLabel(Dot); 689 Asm->OutStreamer->emitValue( 690 MCBinaryExpr::createSub( 691 MCSymbolRefExpr::create(LandingPadRange->FragmentBeginLabel, 692 Context), 693 MCSymbolRefExpr::create(Dot, Context), Context), 694 Asm->MAI->getCodePointerSize()); 695 } 696 697 if (HasLEB128Directives) 698 EmitTypeTableRefAndCallSiteTableEndRef(); 699 else 700 EmitTypeTableOffsetAndCallSiteTableOffset(); 701 702 for (size_t CallSiteIdx = CSRange.CallSiteBeginIdx; 703 CallSiteIdx != CSRange.CallSiteEndIdx; ++CallSiteIdx) { 704 const CallSiteEntry &S = CallSites[CallSiteIdx]; 705 706 MCSymbol *EHFuncBeginSym = CSRange.FragmentBeginLabel; 707 MCSymbol *EHFuncEndSym = CSRange.FragmentEndLabel; 708 709 MCSymbol *BeginLabel = S.BeginLabel; 710 if (!BeginLabel) 711 BeginLabel = EHFuncBeginSym; 712 MCSymbol *EndLabel = S.EndLabel; 713 if (!EndLabel) 714 EndLabel = EHFuncEndSym; 715 716 // Offset of the call site relative to the start of the procedure. 717 if (VerboseAsm) 718 Asm->OutStreamer->AddComment(">> Call Site " + Twine(++Entry) + 719 " <<"); 720 Asm->emitCallSiteOffset(BeginLabel, EHFuncBeginSym, CallSiteEncoding); 721 if (VerboseAsm) 722 Asm->OutStreamer->AddComment(Twine(" Call between ") + 723 BeginLabel->getName() + " and " + 724 EndLabel->getName()); 725 Asm->emitCallSiteOffset(EndLabel, BeginLabel, CallSiteEncoding); 726 727 // Offset of the landing pad relative to the start of the landing pad 728 // fragment. 729 if (!S.LPad) { 730 if (VerboseAsm) 731 Asm->OutStreamer->AddComment(" has no landing pad"); 732 Asm->emitCallSiteValue(0, CallSiteEncoding); 733 } else { 734 if (VerboseAsm) 735 Asm->OutStreamer->AddComment(Twine(" jumps to ") + 736 S.LPad->LandingPadLabel->getName()); 737 Asm->emitCallSiteOffset(S.LPad->LandingPadLabel, 738 LandingPadRange->FragmentBeginLabel, 739 CallSiteEncoding); 740 } 741 742 // Offset of the first associated action record, relative to the start 743 // of the action table. This value is biased by 1 (1 indicates the start 744 // of the action table), and 0 indicates that there are no actions. 745 if (VerboseAsm) { 746 if (S.Action == 0) 747 Asm->OutStreamer->AddComment(" On action: cleanup"); 748 else 749 Asm->OutStreamer->AddComment(" On action: " + 750 Twine((S.Action - 1) / 2 + 1)); 751 } 752 Asm->emitULEB128(S.Action); 753 } 754 } 755 Asm->OutStreamer->emitLabel(CstEndLabel); 756 } 757 758 // Emit the Action Table. 759 int Entry = 0; 760 for (SmallVectorImpl<ActionEntry>::const_iterator 761 I = Actions.begin(), E = Actions.end(); I != E; ++I) { 762 const ActionEntry &Action = *I; 763 764 if (VerboseAsm) { 765 // Emit comments that decode the action table. 766 Asm->OutStreamer->AddComment(">> Action Record " + Twine(++Entry) + " <<"); 767 } 768 769 // Type Filter 770 // 771 // Used by the runtime to match the type of the thrown exception to the 772 // type of the catch clauses or the types in the exception specification. 773 if (VerboseAsm) { 774 if (Action.ValueForTypeID > 0) 775 Asm->OutStreamer->AddComment(" Catch TypeInfo " + 776 Twine(Action.ValueForTypeID)); 777 else if (Action.ValueForTypeID < 0) 778 Asm->OutStreamer->AddComment(" Filter TypeInfo " + 779 Twine(Action.ValueForTypeID)); 780 else 781 Asm->OutStreamer->AddComment(" Cleanup"); 782 } 783 Asm->emitSLEB128(Action.ValueForTypeID); 784 785 // Action Record 786 if (VerboseAsm) { 787 if (Action.Previous == unsigned(-1)) { 788 Asm->OutStreamer->AddComment(" No further actions"); 789 } else { 790 Asm->OutStreamer->AddComment(" Continue to action " + 791 Twine(Action.Previous + 1)); 792 } 793 } 794 Asm->emitSLEB128(Action.NextAction); 795 } 796 797 if (HaveTTData) { 798 Asm->emitAlignment(Align(4)); 799 emitTypeInfos(TTypeEncoding, TTBaseLabel); 800 } 801 802 Asm->emitAlignment(Align(4)); 803 return GCCETSym; 804 } 805 806 void EHStreamer::emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) { 807 const MachineFunction *MF = Asm->MF; 808 const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); 809 const std::vector<unsigned> &FilterIds = MF->getFilterIds(); 810 811 const bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); 812 813 int Entry = 0; 814 // Emit the Catch TypeInfos. 815 if (VerboseAsm && !TypeInfos.empty()) { 816 Asm->OutStreamer->AddComment(">> Catch TypeInfos <<"); 817 Asm->OutStreamer->AddBlankLine(); 818 Entry = TypeInfos.size(); 819 } 820 821 for (const GlobalValue *GV : make_range(TypeInfos.rbegin(), 822 TypeInfos.rend())) { 823 if (VerboseAsm) 824 Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--)); 825 Asm->emitTTypeReference(GV, TTypeEncoding); 826 } 827 828 Asm->OutStreamer->emitLabel(TTBaseLabel); 829 830 // Emit the Exception Specifications. 831 if (VerboseAsm && !FilterIds.empty()) { 832 Asm->OutStreamer->AddComment(">> Filter TypeInfos <<"); 833 Asm->OutStreamer->AddBlankLine(); 834 Entry = 0; 835 } 836 for (std::vector<unsigned>::const_iterator 837 I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 838 unsigned TypeID = *I; 839 if (VerboseAsm) { 840 --Entry; 841 if (isFilterEHSelector(TypeID)) 842 Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry)); 843 } 844 845 Asm->emitULEB128(TypeID); 846 } 847 } 848