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