10b57cec5SDimitry Andric //===- CodeGen/AsmPrinter/EHStreamer.cpp - Exception Directive Streamer ---===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file contains support for writing exception info into assembly files. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "EHStreamer.h" 140b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 150b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 160b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h" 170b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 220b57cec5SDimitry Andric #include "llvm/IR/Function.h" 230b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 240b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 250b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 260b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 270b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h" 280b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 290b57cec5SDimitry Andric #include "llvm/Support/LEB128.h" 300b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h" 310b57cec5SDimitry Andric #include <algorithm> 320b57cec5SDimitry Andric #include <cassert> 330b57cec5SDimitry Andric #include <cstdint> 340b57cec5SDimitry Andric #include <vector> 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric using namespace llvm; 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric EHStreamer::EHStreamer(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric EHStreamer::~EHStreamer() = default; 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric /// How many leading type ids two landing pads have in common. 430b57cec5SDimitry Andric unsigned EHStreamer::sharedTypeIDs(const LandingPadInfo *L, 440b57cec5SDimitry Andric const LandingPadInfo *R) { 450b57cec5SDimitry Andric const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 46e8d8bef9SDimitry Andric return std::mismatch(LIds.begin(), LIds.end(), RIds.begin(), RIds.end()) 47e8d8bef9SDimitry Andric .first - 48e8d8bef9SDimitry Andric LIds.begin(); 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric /// Compute the actions table and gather the first action index for each landing 520b57cec5SDimitry Andric /// pad site. 530b57cec5SDimitry Andric void EHStreamer::computeActionsTable( 540b57cec5SDimitry Andric const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 550b57cec5SDimitry Andric SmallVectorImpl<ActionEntry> &Actions, 560b57cec5SDimitry Andric SmallVectorImpl<unsigned> &FirstActions) { 570b57cec5SDimitry Andric // The action table follows the call-site table in the LSDA. The individual 580b57cec5SDimitry Andric // records are of two types: 590b57cec5SDimitry Andric // 600b57cec5SDimitry Andric // * Catch clause 610b57cec5SDimitry Andric // * Exception specification 620b57cec5SDimitry Andric // 630b57cec5SDimitry Andric // The two record kinds have the same format, with only small differences. 640b57cec5SDimitry Andric // They are distinguished by the "switch value" field: Catch clauses 650b57cec5SDimitry Andric // (TypeInfos) have strictly positive switch values, and exception 660b57cec5SDimitry Andric // specifications (FilterIds) have strictly negative switch values. Value 0 670b57cec5SDimitry Andric // indicates a catch-all clause. 680b57cec5SDimitry Andric // 690b57cec5SDimitry Andric // Negative type IDs index into FilterIds. Positive type IDs index into 700b57cec5SDimitry Andric // TypeInfos. The value written for a positive type ID is just the type ID 710b57cec5SDimitry Andric // itself. For a negative type ID, however, the value written is the 720b57cec5SDimitry Andric // (negative) byte offset of the corresponding FilterIds entry. The byte 730b57cec5SDimitry Andric // offset is usually equal to the type ID (because the FilterIds entries are 740b57cec5SDimitry Andric // written using a variable width encoding, which outputs one byte per entry 750b57cec5SDimitry Andric // as long as the value written is not too large) but can differ. This kind 760b57cec5SDimitry Andric // of complication does not occur for positive type IDs because type infos are 770b57cec5SDimitry Andric // output using a fixed width encoding. FilterOffsets[i] holds the byte 780b57cec5SDimitry Andric // offset corresponding to FilterIds[i]. 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric const std::vector<unsigned> &FilterIds = Asm->MF->getFilterIds(); 810b57cec5SDimitry Andric SmallVector<int, 16> FilterOffsets; 820b57cec5SDimitry Andric FilterOffsets.reserve(FilterIds.size()); 830b57cec5SDimitry Andric int Offset = -1; 840b57cec5SDimitry Andric 85fe6060f1SDimitry Andric for (unsigned FilterId : FilterIds) { 860b57cec5SDimitry Andric FilterOffsets.push_back(Offset); 87fe6060f1SDimitry Andric Offset -= getULEB128Size(FilterId); 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric FirstActions.reserve(LandingPads.size()); 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric int FirstAction = 0; 930b57cec5SDimitry Andric unsigned SizeActions = 0; // Total size of all action entries for a function 940b57cec5SDimitry Andric const LandingPadInfo *PrevLPI = nullptr; 950b57cec5SDimitry Andric 96fe6060f1SDimitry Andric for (const LandingPadInfo *LPI : LandingPads) { 970b57cec5SDimitry Andric const std::vector<int> &TypeIds = LPI->TypeIds; 980b57cec5SDimitry Andric unsigned NumShared = PrevLPI ? sharedTypeIDs(LPI, PrevLPI) : 0; 990b57cec5SDimitry Andric unsigned SizeSiteActions = 0; // Total size of all entries for a landingpad 1000b57cec5SDimitry Andric 1010b57cec5SDimitry Andric if (NumShared < TypeIds.size()) { 1020b57cec5SDimitry Andric // Size of one action entry (typeid + next action) 1030b57cec5SDimitry Andric unsigned SizeActionEntry = 0; 1040b57cec5SDimitry Andric unsigned PrevAction = (unsigned)-1; 1050b57cec5SDimitry Andric 1060b57cec5SDimitry Andric if (NumShared) { 1070b57cec5SDimitry Andric unsigned SizePrevIds = PrevLPI->TypeIds.size(); 1080b57cec5SDimitry Andric assert(Actions.size()); 1090b57cec5SDimitry Andric PrevAction = Actions.size() - 1; 1100b57cec5SDimitry Andric SizeActionEntry = getSLEB128Size(Actions[PrevAction].NextAction) + 1110b57cec5SDimitry Andric getSLEB128Size(Actions[PrevAction].ValueForTypeID); 1120b57cec5SDimitry Andric 1130b57cec5SDimitry Andric for (unsigned j = NumShared; j != SizePrevIds; ++j) { 1140b57cec5SDimitry Andric assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!"); 1150b57cec5SDimitry Andric SizeActionEntry -= getSLEB128Size(Actions[PrevAction].ValueForTypeID); 1160b57cec5SDimitry Andric SizeActionEntry += -Actions[PrevAction].NextAction; 1170b57cec5SDimitry Andric PrevAction = Actions[PrevAction].Previous; 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric } 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric // Compute the actions. 1220b57cec5SDimitry Andric for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { 1230b57cec5SDimitry Andric int TypeID = TypeIds[J]; 1240b57cec5SDimitry Andric assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); 1250b57cec5SDimitry Andric int ValueForTypeID = 1260b57cec5SDimitry Andric isFilterEHSelector(TypeID) ? FilterOffsets[-1 - TypeID] : TypeID; 1270b57cec5SDimitry Andric unsigned SizeTypeID = getSLEB128Size(ValueForTypeID); 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric int NextAction = SizeActionEntry ? -(SizeActionEntry + SizeTypeID) : 0; 1300b57cec5SDimitry Andric SizeActionEntry = SizeTypeID + getSLEB128Size(NextAction); 1310b57cec5SDimitry Andric SizeSiteActions += SizeActionEntry; 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; 1340b57cec5SDimitry Andric Actions.push_back(Action); 1350b57cec5SDimitry Andric PrevAction = Actions.size() - 1; 1360b57cec5SDimitry Andric } 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric // Record the first action of the landing pad site. 1390b57cec5SDimitry Andric FirstAction = SizeActions + SizeSiteActions - SizeActionEntry + 1; 1400b57cec5SDimitry Andric } // else identical - re-use previous FirstAction 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric // Information used when creating the call-site table. The action record 1430b57cec5SDimitry Andric // field of the call site record is the offset of the first associated 1440b57cec5SDimitry Andric // action record, relative to the start of the actions table. This value is 1450b57cec5SDimitry Andric // biased by 1 (1 indicating the start of the actions table), and 0 1460b57cec5SDimitry Andric // indicates that there are no actions. 1470b57cec5SDimitry Andric FirstActions.push_back(FirstAction); 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric // Compute this sites contribution to size. 1500b57cec5SDimitry Andric SizeActions += SizeSiteActions; 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric PrevLPI = LPI; 1530b57cec5SDimitry Andric } 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric /// Return `true' if this is a call to a function marked `nounwind'. Return 1570b57cec5SDimitry Andric /// `false' otherwise. 1580b57cec5SDimitry Andric bool EHStreamer::callToNoUnwindFunction(const MachineInstr *MI) { 1590b57cec5SDimitry Andric assert(MI->isCall() && "This should be a call instruction!"); 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric bool MarkedNoUnwind = false; 1620b57cec5SDimitry Andric bool SawFunc = false; 1630b57cec5SDimitry Andric 1644824e7fdSDimitry Andric for (const MachineOperand &MO : MI->operands()) { 1650b57cec5SDimitry Andric if (!MO.isGlobal()) continue; 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric const Function *F = dyn_cast<Function>(MO.getGlobal()); 1680b57cec5SDimitry Andric if (!F) continue; 1690b57cec5SDimitry Andric 1700b57cec5SDimitry Andric if (SawFunc) { 1710b57cec5SDimitry Andric // Be conservative. If we have more than one function operand for this 1720b57cec5SDimitry Andric // call, then we can't make the assumption that it's the callee and 1730b57cec5SDimitry Andric // not a parameter to the call. 1740b57cec5SDimitry Andric // 1750b57cec5SDimitry Andric // FIXME: Determine if there's a way to say that `F' is the callee or 1760b57cec5SDimitry Andric // parameter. 1770b57cec5SDimitry Andric MarkedNoUnwind = false; 1780b57cec5SDimitry Andric break; 1790b57cec5SDimitry Andric } 1800b57cec5SDimitry Andric 1810b57cec5SDimitry Andric MarkedNoUnwind = F->doesNotThrow(); 1820b57cec5SDimitry Andric SawFunc = true; 1830b57cec5SDimitry Andric } 1840b57cec5SDimitry Andric 1850b57cec5SDimitry Andric return MarkedNoUnwind; 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric void EHStreamer::computePadMap( 1890b57cec5SDimitry Andric const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 1900b57cec5SDimitry Andric RangeMapType &PadMap) { 1910b57cec5SDimitry Andric // Invokes and nounwind calls have entries in PadMap (due to being bracketed 1920b57cec5SDimitry Andric // by try-range labels when lowered). Ordinary calls do not, so appropriate 1930b57cec5SDimitry Andric // try-ranges for them need be deduced so we can put them in the LSDA. 1940b57cec5SDimitry Andric for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 1950b57cec5SDimitry Andric const LandingPadInfo *LandingPad = LandingPads[i]; 1960b57cec5SDimitry Andric for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 1970b57cec5SDimitry Andric MCSymbol *BeginLabel = LandingPad->BeginLabels[j]; 1980b57cec5SDimitry Andric assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 1990b57cec5SDimitry Andric PadRange P = { i, j }; 2000b57cec5SDimitry Andric PadMap[BeginLabel] = P; 2010b57cec5SDimitry Andric } 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric } 2040b57cec5SDimitry Andric 2050b57cec5SDimitry Andric /// Compute the call-site table. The entry for an invoke has a try-range 2060b57cec5SDimitry Andric /// containing the call, a non-zero landing pad, and an appropriate action. The 2070b57cec5SDimitry Andric /// entry for an ordinary call has a try-range containing the call and zero for 2080b57cec5SDimitry Andric /// the landing pad and the action. Calls marked 'nounwind' have no entry and 2090b57cec5SDimitry Andric /// must not be contained in the try-range of any entry - they form gaps in the 2100b57cec5SDimitry Andric /// table. Entries must be ordered by try-range address. 211e8d8bef9SDimitry Andric /// 212e8d8bef9SDimitry Andric /// Call-sites are split into one or more call-site ranges associated with 213e8d8bef9SDimitry Andric /// different sections of the function. 214e8d8bef9SDimitry Andric /// 215e8d8bef9SDimitry Andric /// - Without -basic-block-sections, all call-sites are grouped into one 216e8d8bef9SDimitry Andric /// call-site-range corresponding to the function section. 217e8d8bef9SDimitry Andric /// 218e8d8bef9SDimitry Andric /// - With -basic-block-sections, one call-site range is created for each 219e8d8bef9SDimitry Andric /// section, with its FragmentBeginLabel and FragmentEndLabel respectively 220e8d8bef9SDimitry Andric // set to the beginning and ending of the corresponding section and its 221e8d8bef9SDimitry Andric // ExceptionLabel set to the exception symbol dedicated for this section. 222e8d8bef9SDimitry Andric // Later, one LSDA header will be emitted for each call-site range with its 223e8d8bef9SDimitry Andric // call-sites following. The action table and type info table will be 224e8d8bef9SDimitry Andric // shared across all ranges. 225e8d8bef9SDimitry Andric void EHStreamer::computeCallSiteTable( 226e8d8bef9SDimitry Andric SmallVectorImpl<CallSiteEntry> &CallSites, 227e8d8bef9SDimitry Andric SmallVectorImpl<CallSiteRange> &CallSiteRanges, 2280b57cec5SDimitry Andric const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 2290b57cec5SDimitry Andric const SmallVectorImpl<unsigned> &FirstActions) { 2300b57cec5SDimitry Andric RangeMapType PadMap; 2310b57cec5SDimitry Andric computePadMap(LandingPads, PadMap); 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric // The end label of the previous invoke or nounwind try-range. 234e8d8bef9SDimitry Andric MCSymbol *LastLabel = Asm->getFunctionBegin(); 2350b57cec5SDimitry Andric 2360b57cec5SDimitry Andric // Whether there is a potentially throwing instruction (currently this means 2370b57cec5SDimitry Andric // an ordinary call) between the end of the previous try-range and now. 2380b57cec5SDimitry Andric bool SawPotentiallyThrowing = false; 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric // Whether the last CallSite entry was for an invoke. 2410b57cec5SDimitry Andric bool PreviousIsInvoke = false; 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric // Visit all instructions in order of address. 2460b57cec5SDimitry Andric for (const auto &MBB : *Asm->MF) { 247e8d8bef9SDimitry Andric if (&MBB == &Asm->MF->front() || MBB.isBeginSection()) { 248e8d8bef9SDimitry Andric // We start a call-site range upon function entry and at the beginning of 249e8d8bef9SDimitry Andric // every basic block section. 250e8d8bef9SDimitry Andric CallSiteRanges.push_back( 251e8d8bef9SDimitry Andric {Asm->MBBSectionRanges[MBB.getSectionIDNum()].BeginLabel, 252e8d8bef9SDimitry Andric Asm->MBBSectionRanges[MBB.getSectionIDNum()].EndLabel, 253e8d8bef9SDimitry Andric Asm->getMBBExceptionSym(MBB), CallSites.size()}); 254e8d8bef9SDimitry Andric PreviousIsInvoke = false; 255e8d8bef9SDimitry Andric SawPotentiallyThrowing = false; 256e8d8bef9SDimitry Andric LastLabel = nullptr; 257e8d8bef9SDimitry Andric } 258e8d8bef9SDimitry Andric 259e8d8bef9SDimitry Andric if (MBB.isEHPad()) 260e8d8bef9SDimitry Andric CallSiteRanges.back().IsLPRange = true; 261e8d8bef9SDimitry Andric 2620b57cec5SDimitry Andric for (const auto &MI : MBB) { 2630b57cec5SDimitry Andric if (!MI.isEHLabel()) { 2640b57cec5SDimitry Andric if (MI.isCall()) 2650b57cec5SDimitry Andric SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI); 2660b57cec5SDimitry Andric continue; 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric // End of the previous try-range? 2700b57cec5SDimitry Andric MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol(); 2710b57cec5SDimitry Andric if (BeginLabel == LastLabel) 2720b57cec5SDimitry Andric SawPotentiallyThrowing = false; 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric // Beginning of a new try-range? 2750b57cec5SDimitry Andric RangeMapType::const_iterator L = PadMap.find(BeginLabel); 2760b57cec5SDimitry Andric if (L == PadMap.end()) 2770b57cec5SDimitry Andric // Nope, it was just some random label. 2780b57cec5SDimitry Andric continue; 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric const PadRange &P = L->second; 2810b57cec5SDimitry Andric const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 2820b57cec5SDimitry Andric assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 2830b57cec5SDimitry Andric "Inconsistent landing pad map!"); 2840b57cec5SDimitry Andric 285e8d8bef9SDimitry Andric // For Dwarf and AIX exception handling (SjLj handling doesn't use this). 286e8d8bef9SDimitry Andric // If some instruction between the previous try-range and this one may 287e8d8bef9SDimitry Andric // throw, create a call-site entry with no landing pad for the region 288e8d8bef9SDimitry Andric // between the try-ranges. 289e8d8bef9SDimitry Andric if (SawPotentiallyThrowing && 290e8d8bef9SDimitry Andric (Asm->MAI->usesCFIForEH() || 291e8d8bef9SDimitry Andric Asm->MAI->getExceptionHandlingType() == ExceptionHandling::AIX)) { 292e8d8bef9SDimitry Andric CallSites.push_back({LastLabel, BeginLabel, nullptr, 0}); 2930b57cec5SDimitry Andric PreviousIsInvoke = false; 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric LastLabel = LandingPad->EndLabels[P.RangeIndex]; 2970b57cec5SDimitry Andric assert(BeginLabel && LastLabel && "Invalid landing pad!"); 2980b57cec5SDimitry Andric 2990b57cec5SDimitry Andric if (!LandingPad->LandingPadLabel) { 3000b57cec5SDimitry Andric // Create a gap. 3010b57cec5SDimitry Andric PreviousIsInvoke = false; 3020b57cec5SDimitry Andric } else { 3030b57cec5SDimitry Andric // This try-range is for an invoke. 3040b57cec5SDimitry Andric CallSiteEntry Site = { 3050b57cec5SDimitry Andric BeginLabel, 3060b57cec5SDimitry Andric LastLabel, 3070b57cec5SDimitry Andric LandingPad, 3080b57cec5SDimitry Andric FirstActions[P.PadIndex] 3090b57cec5SDimitry Andric }; 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric // Try to merge with the previous call-site. SJLJ doesn't do this 3120b57cec5SDimitry Andric if (PreviousIsInvoke && !IsSJLJ) { 3130b57cec5SDimitry Andric CallSiteEntry &Prev = CallSites.back(); 3140b57cec5SDimitry Andric if (Site.LPad == Prev.LPad && Site.Action == Prev.Action) { 3150b57cec5SDimitry Andric // Extend the range of the previous entry. 3160b57cec5SDimitry Andric Prev.EndLabel = Site.EndLabel; 3170b57cec5SDimitry Andric continue; 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric // Otherwise, create a new call-site. 3220b57cec5SDimitry Andric if (!IsSJLJ) 3230b57cec5SDimitry Andric CallSites.push_back(Site); 3240b57cec5SDimitry Andric else { 3250b57cec5SDimitry Andric // SjLj EH must maintain the call sites in the order assigned 3260b57cec5SDimitry Andric // to them by the SjLjPrepare pass. 3270b57cec5SDimitry Andric unsigned SiteNo = Asm->MF->getCallSiteBeginLabel(BeginLabel); 3280b57cec5SDimitry Andric if (CallSites.size() < SiteNo) 3290b57cec5SDimitry Andric CallSites.resize(SiteNo); 3300b57cec5SDimitry Andric CallSites[SiteNo - 1] = Site; 3310b57cec5SDimitry Andric } 3320b57cec5SDimitry Andric PreviousIsInvoke = true; 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric } 3350b57cec5SDimitry Andric 336e8d8bef9SDimitry Andric // We end the call-site range upon function exit and at the end of every 337e8d8bef9SDimitry Andric // basic block section. 338e8d8bef9SDimitry Andric if (&MBB == &Asm->MF->back() || MBB.isEndSection()) { 3390b57cec5SDimitry Andric // If some instruction between the previous try-range and the end of the 340e8d8bef9SDimitry Andric // function may throw, create a call-site entry with no landing pad for 341e8d8bef9SDimitry Andric // the region following the try-range. 3420b57cec5SDimitry Andric if (SawPotentiallyThrowing && !IsSJLJ) { 343e8d8bef9SDimitry Andric CallSiteEntry Site = {LastLabel, CallSiteRanges.back().FragmentEndLabel, 344e8d8bef9SDimitry Andric nullptr, 0}; 3450b57cec5SDimitry Andric CallSites.push_back(Site); 346e8d8bef9SDimitry Andric SawPotentiallyThrowing = false; 347e8d8bef9SDimitry Andric } 348e8d8bef9SDimitry Andric CallSiteRanges.back().CallSiteEndIdx = CallSites.size(); 349e8d8bef9SDimitry Andric } 3500b57cec5SDimitry Andric } 3510b57cec5SDimitry Andric } 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric /// Emit landing pads and actions. 3540b57cec5SDimitry Andric /// 3550b57cec5SDimitry Andric /// The general organization of the table is complex, but the basic concepts are 3560b57cec5SDimitry Andric /// easy. First there is a header which describes the location and organization 3570b57cec5SDimitry Andric /// of the three components that follow. 3580b57cec5SDimitry Andric /// 3590b57cec5SDimitry Andric /// 1. The landing pad site information describes the range of code covered by 3600b57cec5SDimitry Andric /// the try. In our case it's an accumulation of the ranges covered by the 3610b57cec5SDimitry Andric /// invokes in the try. There is also a reference to the landing pad that 3620b57cec5SDimitry Andric /// handles the exception once processed. Finally an index into the actions 3630b57cec5SDimitry Andric /// table. 3640b57cec5SDimitry Andric /// 2. The action table, in our case, is composed of pairs of type IDs and next 3650b57cec5SDimitry Andric /// action offset. Starting with the action index from the landing pad 3660b57cec5SDimitry Andric /// site, each type ID is checked for a match to the current exception. If 3670b57cec5SDimitry Andric /// it matches then the exception and type id are passed on to the landing 3680b57cec5SDimitry Andric /// pad. Otherwise the next action is looked up. This chain is terminated 3690b57cec5SDimitry Andric /// with a next action of zero. If no type id is found then the frame is 3700b57cec5SDimitry Andric /// unwound and handling continues. 3710b57cec5SDimitry Andric /// 3. Type ID table contains references to all the C++ typeinfo for all 3720b57cec5SDimitry Andric /// catches in the function. This tables is reverse indexed base 1. 3730b57cec5SDimitry Andric /// 3740b57cec5SDimitry Andric /// Returns the starting symbol of an exception table. 3750b57cec5SDimitry Andric MCSymbol *EHStreamer::emitExceptionTable() { 3760b57cec5SDimitry Andric const MachineFunction *MF = Asm->MF; 3770b57cec5SDimitry Andric const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); 3780b57cec5SDimitry Andric const std::vector<unsigned> &FilterIds = MF->getFilterIds(); 3790b57cec5SDimitry Andric const std::vector<LandingPadInfo> &PadInfos = MF->getLandingPads(); 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric // Sort the landing pads in order of their type ids. This is used to fold 3820b57cec5SDimitry Andric // duplicate actions. 3830b57cec5SDimitry Andric SmallVector<const LandingPadInfo *, 64> LandingPads; 3840b57cec5SDimitry Andric LandingPads.reserve(PadInfos.size()); 3850b57cec5SDimitry Andric 3864824e7fdSDimitry Andric for (const LandingPadInfo &LPI : PadInfos) 3874824e7fdSDimitry Andric LandingPads.push_back(&LPI); 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric // Order landing pads lexicographically by type id. 3900b57cec5SDimitry Andric llvm::sort(LandingPads, [](const LandingPadInfo *L, const LandingPadInfo *R) { 3910b57cec5SDimitry Andric return L->TypeIds < R->TypeIds; 3920b57cec5SDimitry Andric }); 3930b57cec5SDimitry Andric 3940b57cec5SDimitry Andric // Compute the actions table and gather the first action index for each 3950b57cec5SDimitry Andric // landing pad site. 3960b57cec5SDimitry Andric SmallVector<ActionEntry, 32> Actions; 3970b57cec5SDimitry Andric SmallVector<unsigned, 64> FirstActions; 3980b57cec5SDimitry Andric computeActionsTable(LandingPads, Actions, FirstActions); 3990b57cec5SDimitry Andric 400e8d8bef9SDimitry Andric // Compute the call-site table and call-site ranges. Normally, there is only 401e8d8bef9SDimitry Andric // one call-site-range which covers the whole funciton. With 402e8d8bef9SDimitry Andric // -basic-block-sections, there is one call-site-range per basic block 403e8d8bef9SDimitry Andric // section. 4040b57cec5SDimitry Andric SmallVector<CallSiteEntry, 64> CallSites; 405e8d8bef9SDimitry Andric SmallVector<CallSiteRange, 4> CallSiteRanges; 406e8d8bef9SDimitry Andric computeCallSiteTable(CallSites, CallSiteRanges, LandingPads, FirstActions); 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 4090b57cec5SDimitry Andric bool IsWasm = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::Wasm; 410e8d8bef9SDimitry Andric bool HasLEB128Directives = Asm->MAI->hasLEB128Directives(); 4110b57cec5SDimitry Andric unsigned CallSiteEncoding = 4120b57cec5SDimitry Andric IsSJLJ ? static_cast<unsigned>(dwarf::DW_EH_PE_udata4) : 4130b57cec5SDimitry Andric Asm->getObjFileLowering().getCallSiteEncoding(); 4140b57cec5SDimitry Andric bool HaveTTData = !TypeInfos.empty() || !FilterIds.empty(); 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric // Type infos. 417fe6060f1SDimitry Andric MCSection *LSDASection = Asm->getObjFileLowering().getSectionForLSDA( 418fe6060f1SDimitry Andric MF->getFunction(), *Asm->CurrentFnSym, Asm->TM); 4190b57cec5SDimitry Andric unsigned TTypeEncoding; 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric if (!HaveTTData) { 4220b57cec5SDimitry Andric // If there is no TypeInfo, then we just explicitly say that we're omitting 4230b57cec5SDimitry Andric // that bit. 4240b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_omit; 4250b57cec5SDimitry Andric } else { 4260b57cec5SDimitry Andric // Okay, we have actual filters or typeinfos to emit. As such, we need to 4270b57cec5SDimitry Andric // pick a type encoding for them. We're about to emit a list of pointers to 4280b57cec5SDimitry Andric // typeinfo objects at the end of the LSDA. However, unless we're in static 4290b57cec5SDimitry Andric // mode, this reference will require a relocation by the dynamic linker. 4300b57cec5SDimitry Andric // 4310b57cec5SDimitry Andric // Because of this, we have a couple of options: 4320b57cec5SDimitry Andric // 4330b57cec5SDimitry Andric // 1) If we are in -static mode, we can always use an absolute reference 4340b57cec5SDimitry Andric // from the LSDA, because the static linker will resolve it. 4350b57cec5SDimitry Andric // 4360b57cec5SDimitry Andric // 2) Otherwise, if the LSDA section is writable, we can output the direct 4370b57cec5SDimitry Andric // reference to the typeinfo and allow the dynamic linker to relocate 4380b57cec5SDimitry Andric // it. Since it is in a writable section, the dynamic linker won't 4390b57cec5SDimitry Andric // have a problem. 4400b57cec5SDimitry Andric // 4410b57cec5SDimitry Andric // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, 4420b57cec5SDimitry Andric // we need to use some form of indirection. For example, on Darwin, 4430b57cec5SDimitry Andric // we can output a statically-relocatable reference to a dyld stub. The 4440b57cec5SDimitry Andric // offset to the stub is constant, but the contents are in a section 4450b57cec5SDimitry Andric // that is updated by the dynamic linker. This is easy enough, but we 4460b57cec5SDimitry Andric // need to tell the personality function of the unwinder to indirect 4470b57cec5SDimitry Andric // through the dyld stub. 4480b57cec5SDimitry Andric // 4490b57cec5SDimitry Andric // FIXME: When (3) is actually implemented, we'll have to emit the stubs 4500b57cec5SDimitry Andric // somewhere. This predicate should be moved to a shared location that is 4510b57cec5SDimitry Andric // in target-independent code. 4520b57cec5SDimitry Andric // 4530b57cec5SDimitry Andric TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding(); 4540b57cec5SDimitry Andric } 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric // Begin the exception table. 4570b57cec5SDimitry Andric // Sometimes we want not to emit the data into separate section (e.g. ARM 4580b57cec5SDimitry Andric // EHABI). In this case LSDASection will be NULL. 4590b57cec5SDimitry Andric if (LSDASection) 460*81ad6265SDimitry Andric Asm->OutStreamer->switchSection(LSDASection); 4615ffd83dbSDimitry Andric Asm->emitAlignment(Align(4)); 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric // Emit the LSDA. 4640b57cec5SDimitry Andric MCSymbol *GCCETSym = 4650b57cec5SDimitry Andric Asm->OutContext.getOrCreateSymbol(Twine("GCC_except_table")+ 4660b57cec5SDimitry Andric Twine(Asm->getFunctionNumber())); 4675ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(GCCETSym); 468e8d8bef9SDimitry Andric MCSymbol *CstEndLabel = Asm->createTempSymbol( 469e8d8bef9SDimitry Andric CallSiteRanges.size() > 1 ? "action_table_base" : "cst_end"); 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric MCSymbol *TTBaseLabel = nullptr; 472e8d8bef9SDimitry Andric if (HaveTTData) 473e8d8bef9SDimitry Andric TTBaseLabel = Asm->createTempSymbol("ttbase"); 474e8d8bef9SDimitry Andric 475e8d8bef9SDimitry Andric const bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); 476e8d8bef9SDimitry Andric 477e8d8bef9SDimitry Andric // Helper for emitting references (offsets) for type table and the end of the 478e8d8bef9SDimitry Andric // call-site table (which marks the beginning of the action table). 479e8d8bef9SDimitry Andric // * For Itanium, these references will be emitted for every callsite range. 480e8d8bef9SDimitry Andric // * For SJLJ and Wasm, they will be emitted only once in the LSDA header. 481e8d8bef9SDimitry Andric auto EmitTypeTableRefAndCallSiteTableEndRef = [&]() { 482e8d8bef9SDimitry Andric Asm->emitEncodingByte(TTypeEncoding, "@TType"); 4830b57cec5SDimitry Andric if (HaveTTData) { 4840b57cec5SDimitry Andric // N.B.: There is a dependency loop between the size of the TTBase uleb128 4850b57cec5SDimitry Andric // here and the amount of padding before the aligned type table. The 486e8d8bef9SDimitry Andric // assembler must sometimes pad this uleb128 or insert extra padding 487e8d8bef9SDimitry Andric // before the type table. See PR35809 or GNU as bug 4029. 4880b57cec5SDimitry Andric MCSymbol *TTBaseRefLabel = Asm->createTempSymbol("ttbaseref"); 4895ffd83dbSDimitry Andric Asm->emitLabelDifferenceAsULEB128(TTBaseLabel, TTBaseRefLabel); 4905ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(TTBaseRefLabel); 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric 493e8d8bef9SDimitry Andric // The Action table follows the call-site table. So we emit the 494e8d8bef9SDimitry Andric // label difference from here (start of the call-site table for SJLJ and 495e8d8bef9SDimitry Andric // Wasm, and start of a call-site range for Itanium) to the end of the 496e8d8bef9SDimitry Andric // whole call-site table (end of the last call-site range for Itanium). 4970b57cec5SDimitry Andric MCSymbol *CstBeginLabel = Asm->createTempSymbol("cst_begin"); 4985ffd83dbSDimitry Andric Asm->emitEncodingByte(CallSiteEncoding, "Call site"); 4995ffd83dbSDimitry Andric Asm->emitLabelDifferenceAsULEB128(CstEndLabel, CstBeginLabel); 5005ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(CstBeginLabel); 501e8d8bef9SDimitry Andric }; 502e8d8bef9SDimitry Andric 503e8d8bef9SDimitry Andric // An alternative path to EmitTypeTableRefAndCallSiteTableEndRef. 504e8d8bef9SDimitry Andric // For some platforms, the system assembler does not accept the form of 505e8d8bef9SDimitry Andric // `.uleb128 label2 - label1`. In those situations, we would need to calculate 506e8d8bef9SDimitry Andric // the size between label1 and label2 manually. 507e8d8bef9SDimitry Andric // In this case, we would need to calculate the LSDA size and the call 508e8d8bef9SDimitry Andric // site table size. 509e8d8bef9SDimitry Andric auto EmitTypeTableOffsetAndCallSiteTableOffset = [&]() { 510e8d8bef9SDimitry Andric assert(CallSiteEncoding == dwarf::DW_EH_PE_udata4 && !HasLEB128Directives && 511e8d8bef9SDimitry Andric "Targets supporting .uleb128 do not need to take this path."); 512e8d8bef9SDimitry Andric if (CallSiteRanges.size() > 1) 513e8d8bef9SDimitry Andric report_fatal_error( 514e8d8bef9SDimitry Andric "-fbasic-block-sections is not yet supported on " 515e8d8bef9SDimitry Andric "platforms that do not have general LEB128 directive support."); 516e8d8bef9SDimitry Andric 517e8d8bef9SDimitry Andric uint64_t CallSiteTableSize = 0; 518e8d8bef9SDimitry Andric const CallSiteRange &CSRange = CallSiteRanges.back(); 519e8d8bef9SDimitry Andric for (size_t CallSiteIdx = CSRange.CallSiteBeginIdx; 520e8d8bef9SDimitry Andric CallSiteIdx < CSRange.CallSiteEndIdx; ++CallSiteIdx) { 521e8d8bef9SDimitry Andric const CallSiteEntry &S = CallSites[CallSiteIdx]; 522e8d8bef9SDimitry Andric // Each call site entry consists of 3 udata4 fields (12 bytes) and 523e8d8bef9SDimitry Andric // 1 ULEB128 field. 524e8d8bef9SDimitry Andric CallSiteTableSize += 12 + getULEB128Size(S.Action); 525e8d8bef9SDimitry Andric assert(isUInt<32>(CallSiteTableSize) && "CallSiteTableSize overflows."); 526e8d8bef9SDimitry Andric } 527e8d8bef9SDimitry Andric 528e8d8bef9SDimitry Andric Asm->emitEncodingByte(TTypeEncoding, "@TType"); 529e8d8bef9SDimitry Andric if (HaveTTData) { 530e8d8bef9SDimitry Andric const unsigned ByteSizeOfCallSiteOffset = 531e8d8bef9SDimitry Andric getULEB128Size(CallSiteTableSize); 532e8d8bef9SDimitry Andric uint64_t ActionTableSize = 0; 533e8d8bef9SDimitry Andric for (const ActionEntry &Action : Actions) { 534e8d8bef9SDimitry Andric // Each action entry consists of two SLEB128 fields. 535e8d8bef9SDimitry Andric ActionTableSize += getSLEB128Size(Action.ValueForTypeID) + 536e8d8bef9SDimitry Andric getSLEB128Size(Action.NextAction); 537e8d8bef9SDimitry Andric assert(isUInt<32>(ActionTableSize) && "ActionTableSize overflows."); 538e8d8bef9SDimitry Andric } 539e8d8bef9SDimitry Andric 540e8d8bef9SDimitry Andric const unsigned TypeInfoSize = 541e8d8bef9SDimitry Andric Asm->GetSizeOfEncodedValue(TTypeEncoding) * MF->getTypeInfos().size(); 542e8d8bef9SDimitry Andric 543e8d8bef9SDimitry Andric const uint64_t LSDASizeBeforeAlign = 544e8d8bef9SDimitry Andric 1 // Call site encoding byte. 545e8d8bef9SDimitry Andric + ByteSizeOfCallSiteOffset // ULEB128 encoding of CallSiteTableSize. 546e8d8bef9SDimitry Andric + CallSiteTableSize // Call site table content. 547e8d8bef9SDimitry Andric + ActionTableSize; // Action table content. 548e8d8bef9SDimitry Andric 549e8d8bef9SDimitry Andric const uint64_t LSDASizeWithoutAlign = LSDASizeBeforeAlign + TypeInfoSize; 550e8d8bef9SDimitry Andric const unsigned ByteSizeOfLSDAWithoutAlign = 551e8d8bef9SDimitry Andric getULEB128Size(LSDASizeWithoutAlign); 552e8d8bef9SDimitry Andric const uint64_t DisplacementBeforeAlign = 553e8d8bef9SDimitry Andric 2 // LPStartEncoding and TypeTableEncoding. 554e8d8bef9SDimitry Andric + ByteSizeOfLSDAWithoutAlign + LSDASizeBeforeAlign; 555e8d8bef9SDimitry Andric 556e8d8bef9SDimitry Andric // The type info area starts with 4 byte alignment. 557e8d8bef9SDimitry Andric const unsigned NeedAlignVal = (4 - DisplacementBeforeAlign % 4) % 4; 558e8d8bef9SDimitry Andric uint64_t LSDASizeWithAlign = LSDASizeWithoutAlign + NeedAlignVal; 559e8d8bef9SDimitry Andric const unsigned ByteSizeOfLSDAWithAlign = 560e8d8bef9SDimitry Andric getULEB128Size(LSDASizeWithAlign); 561e8d8bef9SDimitry Andric 562e8d8bef9SDimitry Andric // The LSDASizeWithAlign could use 1 byte less padding for alignment 563e8d8bef9SDimitry Andric // when the data we use to represent the LSDA Size "needs" to be 1 byte 564e8d8bef9SDimitry Andric // larger than the one previously calculated without alignment. 565e8d8bef9SDimitry Andric if (ByteSizeOfLSDAWithAlign > ByteSizeOfLSDAWithoutAlign) 566e8d8bef9SDimitry Andric LSDASizeWithAlign -= 1; 567e8d8bef9SDimitry Andric 568e8d8bef9SDimitry Andric Asm->OutStreamer->emitULEB128IntValue(LSDASizeWithAlign, 569e8d8bef9SDimitry Andric ByteSizeOfLSDAWithAlign); 570e8d8bef9SDimitry Andric } 571e8d8bef9SDimitry Andric 572e8d8bef9SDimitry Andric Asm->emitEncodingByte(CallSiteEncoding, "Call site"); 573e8d8bef9SDimitry Andric Asm->OutStreamer->emitULEB128IntValue(CallSiteTableSize); 574e8d8bef9SDimitry Andric }; 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric // SjLj / Wasm Exception handling 5770b57cec5SDimitry Andric if (IsSJLJ || IsWasm) { 578e8d8bef9SDimitry Andric Asm->OutStreamer->emitLabel(Asm->getMBBExceptionSym(Asm->MF->front())); 579e8d8bef9SDimitry Andric 580e8d8bef9SDimitry Andric // emit the LSDA header. 581e8d8bef9SDimitry Andric Asm->emitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); 582e8d8bef9SDimitry Andric EmitTypeTableRefAndCallSiteTableEndRef(); 583e8d8bef9SDimitry Andric 5840b57cec5SDimitry Andric unsigned idx = 0; 5850b57cec5SDimitry Andric for (SmallVectorImpl<CallSiteEntry>::const_iterator 5860b57cec5SDimitry Andric I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { 5870b57cec5SDimitry Andric const CallSiteEntry &S = *I; 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric // Index of the call site entry. 5900b57cec5SDimitry Andric if (VerboseAsm) { 5910b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Call Site " + Twine(idx) + " <<"); 5920b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" On exception at call site "+Twine(idx)); 5930b57cec5SDimitry Andric } 5945ffd83dbSDimitry Andric Asm->emitULEB128(idx); 5950b57cec5SDimitry Andric 5960b57cec5SDimitry Andric // Offset of the first associated action record, relative to the start of 5970b57cec5SDimitry Andric // the action table. This value is biased by 1 (1 indicates the start of 5980b57cec5SDimitry Andric // the action table), and 0 indicates that there are no actions. 5990b57cec5SDimitry Andric if (VerboseAsm) { 6000b57cec5SDimitry Andric if (S.Action == 0) 6010b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Action: cleanup"); 6020b57cec5SDimitry Andric else 6030b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Action: " + 6040b57cec5SDimitry Andric Twine((S.Action - 1) / 2 + 1)); 6050b57cec5SDimitry Andric } 6065ffd83dbSDimitry Andric Asm->emitULEB128(S.Action); 6070b57cec5SDimitry Andric } 608e8d8bef9SDimitry Andric Asm->OutStreamer->emitLabel(CstEndLabel); 6090b57cec5SDimitry Andric } else { 6100b57cec5SDimitry Andric // Itanium LSDA exception handling 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric // The call-site table is a list of all call sites that may throw an 6130b57cec5SDimitry Andric // exception (including C++ 'throw' statements) in the procedure 6140b57cec5SDimitry Andric // fragment. It immediately follows the LSDA header. Each entry indicates, 6150b57cec5SDimitry Andric // for a given call, the first corresponding action record and corresponding 6160b57cec5SDimitry Andric // landing pad. 6170b57cec5SDimitry Andric // 6180b57cec5SDimitry Andric // The table begins with the number of bytes, stored as an LEB128 6190b57cec5SDimitry Andric // compressed, unsigned integer. The records immediately follow the record 6200b57cec5SDimitry Andric // count. They are sorted in increasing call-site address. Each record 6210b57cec5SDimitry Andric // indicates: 6220b57cec5SDimitry Andric // 6230b57cec5SDimitry Andric // * The position of the call-site. 6240b57cec5SDimitry Andric // * The position of the landing pad. 6250b57cec5SDimitry Andric // * The first action record for that call site. 6260b57cec5SDimitry Andric // 6270b57cec5SDimitry Andric // A missing entry in the call-site table indicates that a call is not 6280b57cec5SDimitry Andric // supposed to throw. 6290b57cec5SDimitry Andric 630e8d8bef9SDimitry Andric assert(CallSiteRanges.size() != 0 && "No call-site ranges!"); 6310b57cec5SDimitry Andric 632e8d8bef9SDimitry Andric // There should be only one call-site range which includes all the landing 633e8d8bef9SDimitry Andric // pads. Find that call-site range here. 634e8d8bef9SDimitry Andric const CallSiteRange *LandingPadRange = nullptr; 635e8d8bef9SDimitry Andric for (const CallSiteRange &CSRange : CallSiteRanges) { 636e8d8bef9SDimitry Andric if (CSRange.IsLPRange) { 637e8d8bef9SDimitry Andric assert(LandingPadRange == nullptr && 638e8d8bef9SDimitry Andric "All landing pads must be in a single callsite range."); 639e8d8bef9SDimitry Andric LandingPadRange = &CSRange; 640e8d8bef9SDimitry Andric } 641e8d8bef9SDimitry Andric } 642e8d8bef9SDimitry Andric 643e8d8bef9SDimitry Andric // The call-site table is split into its call-site ranges, each being 644e8d8bef9SDimitry Andric // emitted as: 645e8d8bef9SDimitry Andric // [ LPStartEncoding | LPStart ] 646e8d8bef9SDimitry Andric // [ TypeTableEncoding | TypeTableOffset ] 647e8d8bef9SDimitry Andric // [ CallSiteEncoding | CallSiteTableEndOffset ] 648e8d8bef9SDimitry Andric // cst_begin -> { call-site entries contained in this range } 649e8d8bef9SDimitry Andric // 650e8d8bef9SDimitry Andric // and is followed by the next call-site range. 651e8d8bef9SDimitry Andric // 652e8d8bef9SDimitry Andric // For each call-site range, CallSiteTableEndOffset is computed as the 653e8d8bef9SDimitry Andric // difference between cst_begin of that range and the last call-site-table's 654e8d8bef9SDimitry Andric // end label. This offset is used to find the action table. 655e8d8bef9SDimitry Andric 656e8d8bef9SDimitry Andric unsigned Entry = 0; 657e8d8bef9SDimitry Andric for (const CallSiteRange &CSRange : CallSiteRanges) { 658e8d8bef9SDimitry Andric if (CSRange.CallSiteBeginIdx != 0) { 659e8d8bef9SDimitry Andric // Align the call-site range for all ranges except the first. The 660e8d8bef9SDimitry Andric // first range is already aligned due to the exception table alignment. 661e8d8bef9SDimitry Andric Asm->emitAlignment(Align(4)); 662e8d8bef9SDimitry Andric } 663e8d8bef9SDimitry Andric Asm->OutStreamer->emitLabel(CSRange.ExceptionLabel); 664e8d8bef9SDimitry Andric 665e8d8bef9SDimitry Andric // Emit the LSDA header. 666e8d8bef9SDimitry Andric // If only one call-site range exists, LPStart is omitted as it is the 667e8d8bef9SDimitry Andric // same as the function entry. 668e8d8bef9SDimitry Andric if (CallSiteRanges.size() == 1) { 669e8d8bef9SDimitry Andric Asm->emitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); 670e8d8bef9SDimitry Andric } else if (!Asm->isPositionIndependent()) { 671e8d8bef9SDimitry Andric // For more than one call-site ranges, LPStart must be explicitly 672e8d8bef9SDimitry Andric // specified. 673e8d8bef9SDimitry Andric // For non-PIC we can simply use the absolute value. 674e8d8bef9SDimitry Andric Asm->emitEncodingByte(dwarf::DW_EH_PE_absptr, "@LPStart"); 675e8d8bef9SDimitry Andric Asm->OutStreamer->emitSymbolValue(LandingPadRange->FragmentBeginLabel, 676e8d8bef9SDimitry Andric Asm->MAI->getCodePointerSize()); 677e8d8bef9SDimitry Andric } else { 678e8d8bef9SDimitry Andric // For PIC mode, we Emit a PC-relative address for LPStart. 679e8d8bef9SDimitry Andric Asm->emitEncodingByte(dwarf::DW_EH_PE_pcrel, "@LPStart"); 680e8d8bef9SDimitry Andric MCContext &Context = Asm->OutStreamer->getContext(); 681e8d8bef9SDimitry Andric MCSymbol *Dot = Context.createTempSymbol(); 682e8d8bef9SDimitry Andric Asm->OutStreamer->emitLabel(Dot); 683e8d8bef9SDimitry Andric Asm->OutStreamer->emitValue( 684e8d8bef9SDimitry Andric MCBinaryExpr::createSub( 685e8d8bef9SDimitry Andric MCSymbolRefExpr::create(LandingPadRange->FragmentBeginLabel, 686e8d8bef9SDimitry Andric Context), 687e8d8bef9SDimitry Andric MCSymbolRefExpr::create(Dot, Context), Context), 688e8d8bef9SDimitry Andric Asm->MAI->getCodePointerSize()); 689e8d8bef9SDimitry Andric } 690e8d8bef9SDimitry Andric 691e8d8bef9SDimitry Andric if (HasLEB128Directives) 692e8d8bef9SDimitry Andric EmitTypeTableRefAndCallSiteTableEndRef(); 693e8d8bef9SDimitry Andric else 694e8d8bef9SDimitry Andric EmitTypeTableOffsetAndCallSiteTableOffset(); 695e8d8bef9SDimitry Andric 696e8d8bef9SDimitry Andric for (size_t CallSiteIdx = CSRange.CallSiteBeginIdx; 697e8d8bef9SDimitry Andric CallSiteIdx != CSRange.CallSiteEndIdx; ++CallSiteIdx) { 698e8d8bef9SDimitry Andric const CallSiteEntry &S = CallSites[CallSiteIdx]; 699e8d8bef9SDimitry Andric 700e8d8bef9SDimitry Andric MCSymbol *EHFuncBeginSym = CSRange.FragmentBeginLabel; 701e8d8bef9SDimitry Andric MCSymbol *EHFuncEndSym = CSRange.FragmentEndLabel; 7020b57cec5SDimitry Andric 7030b57cec5SDimitry Andric MCSymbol *BeginLabel = S.BeginLabel; 7040b57cec5SDimitry Andric if (!BeginLabel) 7050b57cec5SDimitry Andric BeginLabel = EHFuncBeginSym; 7060b57cec5SDimitry Andric MCSymbol *EndLabel = S.EndLabel; 7070b57cec5SDimitry Andric if (!EndLabel) 708e8d8bef9SDimitry Andric EndLabel = EHFuncEndSym; 7090b57cec5SDimitry Andric 7100b57cec5SDimitry Andric // Offset of the call site relative to the start of the procedure. 7110b57cec5SDimitry Andric if (VerboseAsm) 712e8d8bef9SDimitry Andric Asm->OutStreamer->AddComment(">> Call Site " + Twine(++Entry) + 713e8d8bef9SDimitry Andric " <<"); 7145ffd83dbSDimitry Andric Asm->emitCallSiteOffset(BeginLabel, EHFuncBeginSym, CallSiteEncoding); 7150b57cec5SDimitry Andric if (VerboseAsm) 7160b57cec5SDimitry Andric Asm->OutStreamer->AddComment(Twine(" Call between ") + 7170b57cec5SDimitry Andric BeginLabel->getName() + " and " + 7180b57cec5SDimitry Andric EndLabel->getName()); 7195ffd83dbSDimitry Andric Asm->emitCallSiteOffset(EndLabel, BeginLabel, CallSiteEncoding); 7200b57cec5SDimitry Andric 721e8d8bef9SDimitry Andric // Offset of the landing pad relative to the start of the landing pad 722e8d8bef9SDimitry Andric // fragment. 7230b57cec5SDimitry Andric if (!S.LPad) { 7240b57cec5SDimitry Andric if (VerboseAsm) 7250b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" has no landing pad"); 7265ffd83dbSDimitry Andric Asm->emitCallSiteValue(0, CallSiteEncoding); 7270b57cec5SDimitry Andric } else { 7280b57cec5SDimitry Andric if (VerboseAsm) 7290b57cec5SDimitry Andric Asm->OutStreamer->AddComment(Twine(" jumps to ") + 7300b57cec5SDimitry Andric S.LPad->LandingPadLabel->getName()); 731e8d8bef9SDimitry Andric Asm->emitCallSiteOffset(S.LPad->LandingPadLabel, 732e8d8bef9SDimitry Andric LandingPadRange->FragmentBeginLabel, 7330b57cec5SDimitry Andric CallSiteEncoding); 7340b57cec5SDimitry Andric } 7350b57cec5SDimitry Andric 736e8d8bef9SDimitry Andric // Offset of the first associated action record, relative to the start 737e8d8bef9SDimitry Andric // of the action table. This value is biased by 1 (1 indicates the start 738e8d8bef9SDimitry Andric // of the action table), and 0 indicates that there are no actions. 7390b57cec5SDimitry Andric if (VerboseAsm) { 7400b57cec5SDimitry Andric if (S.Action == 0) 7410b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" On action: cleanup"); 7420b57cec5SDimitry Andric else 7430b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" On action: " + 7440b57cec5SDimitry Andric Twine((S.Action - 1) / 2 + 1)); 7450b57cec5SDimitry Andric } 7465ffd83dbSDimitry Andric Asm->emitULEB128(S.Action); 7470b57cec5SDimitry Andric } 7480b57cec5SDimitry Andric } 7495ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(CstEndLabel); 750e8d8bef9SDimitry Andric } 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric // Emit the Action Table. 7530b57cec5SDimitry Andric int Entry = 0; 754fe6060f1SDimitry Andric for (const ActionEntry &Action : Actions) { 7550b57cec5SDimitry Andric if (VerboseAsm) { 7560b57cec5SDimitry Andric // Emit comments that decode the action table. 7570b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Action Record " + Twine(++Entry) + " <<"); 7580b57cec5SDimitry Andric } 7590b57cec5SDimitry Andric 7600b57cec5SDimitry Andric // Type Filter 7610b57cec5SDimitry Andric // 7620b57cec5SDimitry Andric // Used by the runtime to match the type of the thrown exception to the 7630b57cec5SDimitry Andric // type of the catch clauses or the types in the exception specification. 7640b57cec5SDimitry Andric if (VerboseAsm) { 7650b57cec5SDimitry Andric if (Action.ValueForTypeID > 0) 7660b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Catch TypeInfo " + 7670b57cec5SDimitry Andric Twine(Action.ValueForTypeID)); 7680b57cec5SDimitry Andric else if (Action.ValueForTypeID < 0) 7690b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Filter TypeInfo " + 7700b57cec5SDimitry Andric Twine(Action.ValueForTypeID)); 7710b57cec5SDimitry Andric else 7720b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Cleanup"); 7730b57cec5SDimitry Andric } 7745ffd83dbSDimitry Andric Asm->emitSLEB128(Action.ValueForTypeID); 7750b57cec5SDimitry Andric 7760b57cec5SDimitry Andric // Action Record 7770b57cec5SDimitry Andric if (VerboseAsm) { 778e8d8bef9SDimitry Andric if (Action.Previous == unsigned(-1)) { 7790b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" No further actions"); 7800b57cec5SDimitry Andric } else { 781e8d8bef9SDimitry Andric Asm->OutStreamer->AddComment(" Continue to action " + 782e8d8bef9SDimitry Andric Twine(Action.Previous + 1)); 7830b57cec5SDimitry Andric } 7840b57cec5SDimitry Andric } 7855ffd83dbSDimitry Andric Asm->emitSLEB128(Action.NextAction); 7860b57cec5SDimitry Andric } 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric if (HaveTTData) { 7895ffd83dbSDimitry Andric Asm->emitAlignment(Align(4)); 7900b57cec5SDimitry Andric emitTypeInfos(TTypeEncoding, TTBaseLabel); 7910b57cec5SDimitry Andric } 7920b57cec5SDimitry Andric 7935ffd83dbSDimitry Andric Asm->emitAlignment(Align(4)); 7940b57cec5SDimitry Andric return GCCETSym; 7950b57cec5SDimitry Andric } 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric void EHStreamer::emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) { 7980b57cec5SDimitry Andric const MachineFunction *MF = Asm->MF; 7990b57cec5SDimitry Andric const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); 8000b57cec5SDimitry Andric const std::vector<unsigned> &FilterIds = MF->getFilterIds(); 8010b57cec5SDimitry Andric 802e8d8bef9SDimitry Andric const bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric int Entry = 0; 8050b57cec5SDimitry Andric // Emit the Catch TypeInfos. 8060b57cec5SDimitry Andric if (VerboseAsm && !TypeInfos.empty()) { 8070b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Catch TypeInfos <<"); 808*81ad6265SDimitry Andric Asm->OutStreamer->addBlankLine(); 8090b57cec5SDimitry Andric Entry = TypeInfos.size(); 8100b57cec5SDimitry Andric } 8110b57cec5SDimitry Andric 812349cc55cSDimitry Andric for (const GlobalValue *GV : llvm::reverse(TypeInfos)) { 8130b57cec5SDimitry Andric if (VerboseAsm) 8140b57cec5SDimitry Andric Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--)); 8155ffd83dbSDimitry Andric Asm->emitTTypeReference(GV, TTypeEncoding); 8160b57cec5SDimitry Andric } 8170b57cec5SDimitry Andric 8185ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(TTBaseLabel); 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric // Emit the Exception Specifications. 8210b57cec5SDimitry Andric if (VerboseAsm && !FilterIds.empty()) { 8220b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Filter TypeInfos <<"); 823*81ad6265SDimitry Andric Asm->OutStreamer->addBlankLine(); 8240b57cec5SDimitry Andric Entry = 0; 8250b57cec5SDimitry Andric } 8260b57cec5SDimitry Andric for (std::vector<unsigned>::const_iterator 8270b57cec5SDimitry Andric I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 8280b57cec5SDimitry Andric unsigned TypeID = *I; 8290b57cec5SDimitry Andric if (VerboseAsm) { 8300b57cec5SDimitry Andric --Entry; 8310b57cec5SDimitry Andric if (isFilterEHSelector(TypeID)) 8320b57cec5SDimitry Andric Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry)); 8330b57cec5SDimitry Andric } 8340b57cec5SDimitry Andric 8355ffd83dbSDimitry Andric Asm->emitULEB128(TypeID); 8360b57cec5SDimitry Andric } 8370b57cec5SDimitry Andric } 838