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]; 198bdd1243dSDimitry Andric MCSymbol *EndLabel = LandingPad->BeginLabels[j]; 199bdd1243dSDimitry Andric // If we have deleted the code for a given invoke after registering it in 200bdd1243dSDimitry Andric // the LandingPad label list, the associated symbols will not have been 201bdd1243dSDimitry Andric // emitted. In that case, ignore this callsite entry. 202bdd1243dSDimitry Andric if (!BeginLabel->isDefined() || !EndLabel->isDefined()) 203bdd1243dSDimitry Andric continue; 2040b57cec5SDimitry Andric assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 2050b57cec5SDimitry Andric PadRange P = { i, j }; 2060b57cec5SDimitry Andric PadMap[BeginLabel] = P; 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric /// Compute the call-site table. The entry for an invoke has a try-range 2120b57cec5SDimitry Andric /// containing the call, a non-zero landing pad, and an appropriate action. The 2130b57cec5SDimitry Andric /// entry for an ordinary call has a try-range containing the call and zero for 2140b57cec5SDimitry Andric /// the landing pad and the action. Calls marked 'nounwind' have no entry and 2150b57cec5SDimitry Andric /// must not be contained in the try-range of any entry - they form gaps in the 2160b57cec5SDimitry Andric /// table. Entries must be ordered by try-range address. 217e8d8bef9SDimitry Andric /// 218e8d8bef9SDimitry Andric /// Call-sites are split into one or more call-site ranges associated with 219e8d8bef9SDimitry Andric /// different sections of the function. 220e8d8bef9SDimitry Andric /// 221e8d8bef9SDimitry Andric /// - Without -basic-block-sections, all call-sites are grouped into one 222e8d8bef9SDimitry Andric /// call-site-range corresponding to the function section. 223e8d8bef9SDimitry Andric /// 224e8d8bef9SDimitry Andric /// - With -basic-block-sections, one call-site range is created for each 225e8d8bef9SDimitry Andric /// section, with its FragmentBeginLabel and FragmentEndLabel respectively 226e8d8bef9SDimitry Andric // set to the beginning and ending of the corresponding section and its 227e8d8bef9SDimitry Andric // ExceptionLabel set to the exception symbol dedicated for this section. 228e8d8bef9SDimitry Andric // Later, one LSDA header will be emitted for each call-site range with its 229e8d8bef9SDimitry Andric // call-sites following. The action table and type info table will be 230e8d8bef9SDimitry Andric // shared across all ranges. 231e8d8bef9SDimitry Andric void EHStreamer::computeCallSiteTable( 232e8d8bef9SDimitry Andric SmallVectorImpl<CallSiteEntry> &CallSites, 233e8d8bef9SDimitry Andric SmallVectorImpl<CallSiteRange> &CallSiteRanges, 2340b57cec5SDimitry Andric const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 2350b57cec5SDimitry Andric const SmallVectorImpl<unsigned> &FirstActions) { 2360b57cec5SDimitry Andric RangeMapType PadMap; 2370b57cec5SDimitry Andric computePadMap(LandingPads, PadMap); 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric // The end label of the previous invoke or nounwind try-range. 240e8d8bef9SDimitry Andric MCSymbol *LastLabel = Asm->getFunctionBegin(); 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric // Whether there is a potentially throwing instruction (currently this means 2430b57cec5SDimitry Andric // an ordinary call) between the end of the previous try-range and now. 2440b57cec5SDimitry Andric bool SawPotentiallyThrowing = false; 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric // Whether the last CallSite entry was for an invoke. 2470b57cec5SDimitry Andric bool PreviousIsInvoke = false; 2480b57cec5SDimitry Andric 2490b57cec5SDimitry Andric bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric // Visit all instructions in order of address. 2520b57cec5SDimitry Andric for (const auto &MBB : *Asm->MF) { 253e8d8bef9SDimitry Andric if (&MBB == &Asm->MF->front() || MBB.isBeginSection()) { 254e8d8bef9SDimitry Andric // We start a call-site range upon function entry and at the beginning of 255e8d8bef9SDimitry Andric // every basic block section. 256e8d8bef9SDimitry Andric CallSiteRanges.push_back( 257e8d8bef9SDimitry Andric {Asm->MBBSectionRanges[MBB.getSectionIDNum()].BeginLabel, 258e8d8bef9SDimitry Andric Asm->MBBSectionRanges[MBB.getSectionIDNum()].EndLabel, 259e8d8bef9SDimitry Andric Asm->getMBBExceptionSym(MBB), CallSites.size()}); 260e8d8bef9SDimitry Andric PreviousIsInvoke = false; 261e8d8bef9SDimitry Andric SawPotentiallyThrowing = false; 262e8d8bef9SDimitry Andric LastLabel = nullptr; 263e8d8bef9SDimitry Andric } 264e8d8bef9SDimitry Andric 265e8d8bef9SDimitry Andric if (MBB.isEHPad()) 266e8d8bef9SDimitry Andric CallSiteRanges.back().IsLPRange = true; 267e8d8bef9SDimitry Andric 2680b57cec5SDimitry Andric for (const auto &MI : MBB) { 2690b57cec5SDimitry Andric if (!MI.isEHLabel()) { 2700b57cec5SDimitry Andric if (MI.isCall()) 2710b57cec5SDimitry Andric SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI); 2720b57cec5SDimitry Andric continue; 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // End of the previous try-range? 2760b57cec5SDimitry Andric MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol(); 2770b57cec5SDimitry Andric if (BeginLabel == LastLabel) 2780b57cec5SDimitry Andric SawPotentiallyThrowing = false; 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric // Beginning of a new try-range? 2810b57cec5SDimitry Andric RangeMapType::const_iterator L = PadMap.find(BeginLabel); 2820b57cec5SDimitry Andric if (L == PadMap.end()) 2830b57cec5SDimitry Andric // Nope, it was just some random label. 2840b57cec5SDimitry Andric continue; 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric const PadRange &P = L->second; 2870b57cec5SDimitry Andric const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 2880b57cec5SDimitry Andric assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 2890b57cec5SDimitry Andric "Inconsistent landing pad map!"); 2900b57cec5SDimitry Andric 291e8d8bef9SDimitry Andric // For Dwarf and AIX exception handling (SjLj handling doesn't use this). 292e8d8bef9SDimitry Andric // If some instruction between the previous try-range and this one may 293e8d8bef9SDimitry Andric // throw, create a call-site entry with no landing pad for the region 294e8d8bef9SDimitry Andric // between the try-ranges. 295e8d8bef9SDimitry Andric if (SawPotentiallyThrowing && 296e8d8bef9SDimitry Andric (Asm->MAI->usesCFIForEH() || 297e8d8bef9SDimitry Andric Asm->MAI->getExceptionHandlingType() == ExceptionHandling::AIX)) { 298e8d8bef9SDimitry Andric CallSites.push_back({LastLabel, BeginLabel, nullptr, 0}); 2990b57cec5SDimitry Andric PreviousIsInvoke = false; 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric LastLabel = LandingPad->EndLabels[P.RangeIndex]; 3030b57cec5SDimitry Andric assert(BeginLabel && LastLabel && "Invalid landing pad!"); 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric if (!LandingPad->LandingPadLabel) { 3060b57cec5SDimitry Andric // Create a gap. 3070b57cec5SDimitry Andric PreviousIsInvoke = false; 3080b57cec5SDimitry Andric } else { 3090b57cec5SDimitry Andric // This try-range is for an invoke. 3100b57cec5SDimitry Andric CallSiteEntry Site = { 3110b57cec5SDimitry Andric BeginLabel, 3120b57cec5SDimitry Andric LastLabel, 3130b57cec5SDimitry Andric LandingPad, 3140b57cec5SDimitry Andric FirstActions[P.PadIndex] 3150b57cec5SDimitry Andric }; 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric // Try to merge with the previous call-site. SJLJ doesn't do this 3180b57cec5SDimitry Andric if (PreviousIsInvoke && !IsSJLJ) { 3190b57cec5SDimitry Andric CallSiteEntry &Prev = CallSites.back(); 3200b57cec5SDimitry Andric if (Site.LPad == Prev.LPad && Site.Action == Prev.Action) { 3210b57cec5SDimitry Andric // Extend the range of the previous entry. 3220b57cec5SDimitry Andric Prev.EndLabel = Site.EndLabel; 3230b57cec5SDimitry Andric continue; 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric // Otherwise, create a new call-site. 3280b57cec5SDimitry Andric if (!IsSJLJ) 3290b57cec5SDimitry Andric CallSites.push_back(Site); 3300b57cec5SDimitry Andric else { 3310b57cec5SDimitry Andric // SjLj EH must maintain the call sites in the order assigned 3320b57cec5SDimitry Andric // to them by the SjLjPrepare pass. 3330b57cec5SDimitry Andric unsigned SiteNo = Asm->MF->getCallSiteBeginLabel(BeginLabel); 3340b57cec5SDimitry Andric if (CallSites.size() < SiteNo) 3350b57cec5SDimitry Andric CallSites.resize(SiteNo); 3360b57cec5SDimitry Andric CallSites[SiteNo - 1] = Site; 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric PreviousIsInvoke = true; 3390b57cec5SDimitry Andric } 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 342e8d8bef9SDimitry Andric // We end the call-site range upon function exit and at the end of every 343e8d8bef9SDimitry Andric // basic block section. 344e8d8bef9SDimitry Andric if (&MBB == &Asm->MF->back() || MBB.isEndSection()) { 3450b57cec5SDimitry Andric // If some instruction between the previous try-range and the end of the 346e8d8bef9SDimitry Andric // function may throw, create a call-site entry with no landing pad for 347e8d8bef9SDimitry Andric // the region following the try-range. 3480b57cec5SDimitry Andric if (SawPotentiallyThrowing && !IsSJLJ) { 349e8d8bef9SDimitry Andric CallSiteEntry Site = {LastLabel, CallSiteRanges.back().FragmentEndLabel, 350e8d8bef9SDimitry Andric nullptr, 0}; 3510b57cec5SDimitry Andric CallSites.push_back(Site); 352e8d8bef9SDimitry Andric SawPotentiallyThrowing = false; 353e8d8bef9SDimitry Andric } 354e8d8bef9SDimitry Andric CallSiteRanges.back().CallSiteEndIdx = CallSites.size(); 355e8d8bef9SDimitry Andric } 3560b57cec5SDimitry Andric } 3570b57cec5SDimitry Andric } 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric /// Emit landing pads and actions. 3600b57cec5SDimitry Andric /// 3610b57cec5SDimitry Andric /// The general organization of the table is complex, but the basic concepts are 3620b57cec5SDimitry Andric /// easy. First there is a header which describes the location and organization 3630b57cec5SDimitry Andric /// of the three components that follow. 3640b57cec5SDimitry Andric /// 3650b57cec5SDimitry Andric /// 1. The landing pad site information describes the range of code covered by 3660b57cec5SDimitry Andric /// the try. In our case it's an accumulation of the ranges covered by the 3670b57cec5SDimitry Andric /// invokes in the try. There is also a reference to the landing pad that 3680b57cec5SDimitry Andric /// handles the exception once processed. Finally an index into the actions 3690b57cec5SDimitry Andric /// table. 3700b57cec5SDimitry Andric /// 2. The action table, in our case, is composed of pairs of type IDs and next 3710b57cec5SDimitry Andric /// action offset. Starting with the action index from the landing pad 3720b57cec5SDimitry Andric /// site, each type ID is checked for a match to the current exception. If 3730b57cec5SDimitry Andric /// it matches then the exception and type id are passed on to the landing 3740b57cec5SDimitry Andric /// pad. Otherwise the next action is looked up. This chain is terminated 3750b57cec5SDimitry Andric /// with a next action of zero. If no type id is found then the frame is 3760b57cec5SDimitry Andric /// unwound and handling continues. 3770b57cec5SDimitry Andric /// 3. Type ID table contains references to all the C++ typeinfo for all 3780b57cec5SDimitry Andric /// catches in the function. This tables is reverse indexed base 1. 3790b57cec5SDimitry Andric /// 3800b57cec5SDimitry Andric /// Returns the starting symbol of an exception table. 3810b57cec5SDimitry Andric MCSymbol *EHStreamer::emitExceptionTable() { 3820b57cec5SDimitry Andric const MachineFunction *MF = Asm->MF; 3830b57cec5SDimitry Andric const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); 3840b57cec5SDimitry Andric const std::vector<unsigned> &FilterIds = MF->getFilterIds(); 3850b57cec5SDimitry Andric const std::vector<LandingPadInfo> &PadInfos = MF->getLandingPads(); 3860b57cec5SDimitry Andric 3870b57cec5SDimitry Andric // Sort the landing pads in order of their type ids. This is used to fold 3880b57cec5SDimitry Andric // duplicate actions. 3890b57cec5SDimitry Andric SmallVector<const LandingPadInfo *, 64> LandingPads; 3900b57cec5SDimitry Andric LandingPads.reserve(PadInfos.size()); 3910b57cec5SDimitry Andric 392bdd1243dSDimitry Andric for (const LandingPadInfo &LPI : PadInfos) { 393bdd1243dSDimitry Andric // If a landing-pad has an associated label, but the label wasn't ever 394bdd1243dSDimitry Andric // emitted, then skip it. (This can occur if the landingpad's MBB was 395bdd1243dSDimitry Andric // deleted). 396bdd1243dSDimitry Andric if (LPI.LandingPadLabel && !LPI.LandingPadLabel->isDefined()) 397bdd1243dSDimitry Andric continue; 3984824e7fdSDimitry Andric LandingPads.push_back(&LPI); 399bdd1243dSDimitry Andric } 4000b57cec5SDimitry Andric 4010b57cec5SDimitry Andric // Order landing pads lexicographically by type id. 4020b57cec5SDimitry Andric llvm::sort(LandingPads, [](const LandingPadInfo *L, const LandingPadInfo *R) { 4030b57cec5SDimitry Andric return L->TypeIds < R->TypeIds; 4040b57cec5SDimitry Andric }); 4050b57cec5SDimitry Andric 4060b57cec5SDimitry Andric // Compute the actions table and gather the first action index for each 4070b57cec5SDimitry Andric // landing pad site. 4080b57cec5SDimitry Andric SmallVector<ActionEntry, 32> Actions; 4090b57cec5SDimitry Andric SmallVector<unsigned, 64> FirstActions; 4100b57cec5SDimitry Andric computeActionsTable(LandingPads, Actions, FirstActions); 4110b57cec5SDimitry Andric 412e8d8bef9SDimitry Andric // Compute the call-site table and call-site ranges. Normally, there is only 413*06c3fb27SDimitry Andric // one call-site-range which covers the whole function. With 414e8d8bef9SDimitry Andric // -basic-block-sections, there is one call-site-range per basic block 415e8d8bef9SDimitry Andric // section. 4160b57cec5SDimitry Andric SmallVector<CallSiteEntry, 64> CallSites; 417e8d8bef9SDimitry Andric SmallVector<CallSiteRange, 4> CallSiteRanges; 418e8d8bef9SDimitry Andric computeCallSiteTable(CallSites, CallSiteRanges, LandingPads, FirstActions); 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 4210b57cec5SDimitry Andric bool IsWasm = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::Wasm; 422e8d8bef9SDimitry Andric bool HasLEB128Directives = Asm->MAI->hasLEB128Directives(); 4230b57cec5SDimitry Andric unsigned CallSiteEncoding = 4240b57cec5SDimitry Andric IsSJLJ ? static_cast<unsigned>(dwarf::DW_EH_PE_udata4) : 4250b57cec5SDimitry Andric Asm->getObjFileLowering().getCallSiteEncoding(); 4260b57cec5SDimitry Andric bool HaveTTData = !TypeInfos.empty() || !FilterIds.empty(); 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric // Type infos. 429fe6060f1SDimitry Andric MCSection *LSDASection = Asm->getObjFileLowering().getSectionForLSDA( 430fe6060f1SDimitry Andric MF->getFunction(), *Asm->CurrentFnSym, Asm->TM); 4310b57cec5SDimitry Andric unsigned TTypeEncoding; 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric if (!HaveTTData) { 4340b57cec5SDimitry Andric // If there is no TypeInfo, then we just explicitly say that we're omitting 4350b57cec5SDimitry Andric // that bit. 4360b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_omit; 4370b57cec5SDimitry Andric } else { 4380b57cec5SDimitry Andric // Okay, we have actual filters or typeinfos to emit. As such, we need to 4390b57cec5SDimitry Andric // pick a type encoding for them. We're about to emit a list of pointers to 4400b57cec5SDimitry Andric // typeinfo objects at the end of the LSDA. However, unless we're in static 4410b57cec5SDimitry Andric // mode, this reference will require a relocation by the dynamic linker. 4420b57cec5SDimitry Andric // 4430b57cec5SDimitry Andric // Because of this, we have a couple of options: 4440b57cec5SDimitry Andric // 4450b57cec5SDimitry Andric // 1) If we are in -static mode, we can always use an absolute reference 4460b57cec5SDimitry Andric // from the LSDA, because the static linker will resolve it. 4470b57cec5SDimitry Andric // 4480b57cec5SDimitry Andric // 2) Otherwise, if the LSDA section is writable, we can output the direct 4490b57cec5SDimitry Andric // reference to the typeinfo and allow the dynamic linker to relocate 4500b57cec5SDimitry Andric // it. Since it is in a writable section, the dynamic linker won't 4510b57cec5SDimitry Andric // have a problem. 4520b57cec5SDimitry Andric // 4530b57cec5SDimitry Andric // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, 4540b57cec5SDimitry Andric // we need to use some form of indirection. For example, on Darwin, 4550b57cec5SDimitry Andric // we can output a statically-relocatable reference to a dyld stub. The 4560b57cec5SDimitry Andric // offset to the stub is constant, but the contents are in a section 4570b57cec5SDimitry Andric // that is updated by the dynamic linker. This is easy enough, but we 4580b57cec5SDimitry Andric // need to tell the personality function of the unwinder to indirect 4590b57cec5SDimitry Andric // through the dyld stub. 4600b57cec5SDimitry Andric // 4610b57cec5SDimitry Andric // FIXME: When (3) is actually implemented, we'll have to emit the stubs 4620b57cec5SDimitry Andric // somewhere. This predicate should be moved to a shared location that is 4630b57cec5SDimitry Andric // in target-independent code. 4640b57cec5SDimitry Andric // 4650b57cec5SDimitry Andric TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding(); 4660b57cec5SDimitry Andric } 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric // Begin the exception table. 4690b57cec5SDimitry Andric // Sometimes we want not to emit the data into separate section (e.g. ARM 4700b57cec5SDimitry Andric // EHABI). In this case LSDASection will be NULL. 4710b57cec5SDimitry Andric if (LSDASection) 47281ad6265SDimitry Andric Asm->OutStreamer->switchSection(LSDASection); 4735ffd83dbSDimitry Andric Asm->emitAlignment(Align(4)); 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric // Emit the LSDA. 4760b57cec5SDimitry Andric MCSymbol *GCCETSym = 4770b57cec5SDimitry Andric Asm->OutContext.getOrCreateSymbol(Twine("GCC_except_table")+ 4780b57cec5SDimitry Andric Twine(Asm->getFunctionNumber())); 4795ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(GCCETSym); 480e8d8bef9SDimitry Andric MCSymbol *CstEndLabel = Asm->createTempSymbol( 481e8d8bef9SDimitry Andric CallSiteRanges.size() > 1 ? "action_table_base" : "cst_end"); 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric MCSymbol *TTBaseLabel = nullptr; 484e8d8bef9SDimitry Andric if (HaveTTData) 485e8d8bef9SDimitry Andric TTBaseLabel = Asm->createTempSymbol("ttbase"); 486e8d8bef9SDimitry Andric 487e8d8bef9SDimitry Andric const bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); 488e8d8bef9SDimitry Andric 489e8d8bef9SDimitry Andric // Helper for emitting references (offsets) for type table and the end of the 490e8d8bef9SDimitry Andric // call-site table (which marks the beginning of the action table). 491e8d8bef9SDimitry Andric // * For Itanium, these references will be emitted for every callsite range. 492e8d8bef9SDimitry Andric // * For SJLJ and Wasm, they will be emitted only once in the LSDA header. 493e8d8bef9SDimitry Andric auto EmitTypeTableRefAndCallSiteTableEndRef = [&]() { 494e8d8bef9SDimitry Andric Asm->emitEncodingByte(TTypeEncoding, "@TType"); 4950b57cec5SDimitry Andric if (HaveTTData) { 4960b57cec5SDimitry Andric // N.B.: There is a dependency loop between the size of the TTBase uleb128 4970b57cec5SDimitry Andric // here and the amount of padding before the aligned type table. The 498e8d8bef9SDimitry Andric // assembler must sometimes pad this uleb128 or insert extra padding 499e8d8bef9SDimitry Andric // before the type table. See PR35809 or GNU as bug 4029. 5000b57cec5SDimitry Andric MCSymbol *TTBaseRefLabel = Asm->createTempSymbol("ttbaseref"); 5015ffd83dbSDimitry Andric Asm->emitLabelDifferenceAsULEB128(TTBaseLabel, TTBaseRefLabel); 5025ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(TTBaseRefLabel); 5030b57cec5SDimitry Andric } 5040b57cec5SDimitry Andric 505e8d8bef9SDimitry Andric // The Action table follows the call-site table. So we emit the 506e8d8bef9SDimitry Andric // label difference from here (start of the call-site table for SJLJ and 507e8d8bef9SDimitry Andric // Wasm, and start of a call-site range for Itanium) to the end of the 508e8d8bef9SDimitry Andric // whole call-site table (end of the last call-site range for Itanium). 5090b57cec5SDimitry Andric MCSymbol *CstBeginLabel = Asm->createTempSymbol("cst_begin"); 5105ffd83dbSDimitry Andric Asm->emitEncodingByte(CallSiteEncoding, "Call site"); 5115ffd83dbSDimitry Andric Asm->emitLabelDifferenceAsULEB128(CstEndLabel, CstBeginLabel); 5125ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(CstBeginLabel); 513e8d8bef9SDimitry Andric }; 514e8d8bef9SDimitry Andric 515e8d8bef9SDimitry Andric // An alternative path to EmitTypeTableRefAndCallSiteTableEndRef. 516e8d8bef9SDimitry Andric // For some platforms, the system assembler does not accept the form of 517e8d8bef9SDimitry Andric // `.uleb128 label2 - label1`. In those situations, we would need to calculate 518e8d8bef9SDimitry Andric // the size between label1 and label2 manually. 519e8d8bef9SDimitry Andric // In this case, we would need to calculate the LSDA size and the call 520e8d8bef9SDimitry Andric // site table size. 521e8d8bef9SDimitry Andric auto EmitTypeTableOffsetAndCallSiteTableOffset = [&]() { 522e8d8bef9SDimitry Andric assert(CallSiteEncoding == dwarf::DW_EH_PE_udata4 && !HasLEB128Directives && 523e8d8bef9SDimitry Andric "Targets supporting .uleb128 do not need to take this path."); 524e8d8bef9SDimitry Andric if (CallSiteRanges.size() > 1) 525e8d8bef9SDimitry Andric report_fatal_error( 526e8d8bef9SDimitry Andric "-fbasic-block-sections is not yet supported on " 527e8d8bef9SDimitry Andric "platforms that do not have general LEB128 directive support."); 528e8d8bef9SDimitry Andric 529e8d8bef9SDimitry Andric uint64_t CallSiteTableSize = 0; 530e8d8bef9SDimitry Andric const CallSiteRange &CSRange = CallSiteRanges.back(); 531e8d8bef9SDimitry Andric for (size_t CallSiteIdx = CSRange.CallSiteBeginIdx; 532e8d8bef9SDimitry Andric CallSiteIdx < CSRange.CallSiteEndIdx; ++CallSiteIdx) { 533e8d8bef9SDimitry Andric const CallSiteEntry &S = CallSites[CallSiteIdx]; 534e8d8bef9SDimitry Andric // Each call site entry consists of 3 udata4 fields (12 bytes) and 535e8d8bef9SDimitry Andric // 1 ULEB128 field. 536e8d8bef9SDimitry Andric CallSiteTableSize += 12 + getULEB128Size(S.Action); 537e8d8bef9SDimitry Andric assert(isUInt<32>(CallSiteTableSize) && "CallSiteTableSize overflows."); 538e8d8bef9SDimitry Andric } 539e8d8bef9SDimitry Andric 540e8d8bef9SDimitry Andric Asm->emitEncodingByte(TTypeEncoding, "@TType"); 541e8d8bef9SDimitry Andric if (HaveTTData) { 542e8d8bef9SDimitry Andric const unsigned ByteSizeOfCallSiteOffset = 543e8d8bef9SDimitry Andric getULEB128Size(CallSiteTableSize); 544e8d8bef9SDimitry Andric uint64_t ActionTableSize = 0; 545e8d8bef9SDimitry Andric for (const ActionEntry &Action : Actions) { 546e8d8bef9SDimitry Andric // Each action entry consists of two SLEB128 fields. 547e8d8bef9SDimitry Andric ActionTableSize += getSLEB128Size(Action.ValueForTypeID) + 548e8d8bef9SDimitry Andric getSLEB128Size(Action.NextAction); 549e8d8bef9SDimitry Andric assert(isUInt<32>(ActionTableSize) && "ActionTableSize overflows."); 550e8d8bef9SDimitry Andric } 551e8d8bef9SDimitry Andric 552e8d8bef9SDimitry Andric const unsigned TypeInfoSize = 553e8d8bef9SDimitry Andric Asm->GetSizeOfEncodedValue(TTypeEncoding) * MF->getTypeInfos().size(); 554e8d8bef9SDimitry Andric 555e8d8bef9SDimitry Andric const uint64_t LSDASizeBeforeAlign = 556e8d8bef9SDimitry Andric 1 // Call site encoding byte. 557e8d8bef9SDimitry Andric + ByteSizeOfCallSiteOffset // ULEB128 encoding of CallSiteTableSize. 558e8d8bef9SDimitry Andric + CallSiteTableSize // Call site table content. 559e8d8bef9SDimitry Andric + ActionTableSize; // Action table content. 560e8d8bef9SDimitry Andric 561e8d8bef9SDimitry Andric const uint64_t LSDASizeWithoutAlign = LSDASizeBeforeAlign + TypeInfoSize; 562e8d8bef9SDimitry Andric const unsigned ByteSizeOfLSDAWithoutAlign = 563e8d8bef9SDimitry Andric getULEB128Size(LSDASizeWithoutAlign); 564e8d8bef9SDimitry Andric const uint64_t DisplacementBeforeAlign = 565e8d8bef9SDimitry Andric 2 // LPStartEncoding and TypeTableEncoding. 566e8d8bef9SDimitry Andric + ByteSizeOfLSDAWithoutAlign + LSDASizeBeforeAlign; 567e8d8bef9SDimitry Andric 568e8d8bef9SDimitry Andric // The type info area starts with 4 byte alignment. 569e8d8bef9SDimitry Andric const unsigned NeedAlignVal = (4 - DisplacementBeforeAlign % 4) % 4; 570e8d8bef9SDimitry Andric uint64_t LSDASizeWithAlign = LSDASizeWithoutAlign + NeedAlignVal; 571e8d8bef9SDimitry Andric const unsigned ByteSizeOfLSDAWithAlign = 572e8d8bef9SDimitry Andric getULEB128Size(LSDASizeWithAlign); 573e8d8bef9SDimitry Andric 574e8d8bef9SDimitry Andric // The LSDASizeWithAlign could use 1 byte less padding for alignment 575e8d8bef9SDimitry Andric // when the data we use to represent the LSDA Size "needs" to be 1 byte 576e8d8bef9SDimitry Andric // larger than the one previously calculated without alignment. 577e8d8bef9SDimitry Andric if (ByteSizeOfLSDAWithAlign > ByteSizeOfLSDAWithoutAlign) 578e8d8bef9SDimitry Andric LSDASizeWithAlign -= 1; 579e8d8bef9SDimitry Andric 580e8d8bef9SDimitry Andric Asm->OutStreamer->emitULEB128IntValue(LSDASizeWithAlign, 581e8d8bef9SDimitry Andric ByteSizeOfLSDAWithAlign); 582e8d8bef9SDimitry Andric } 583e8d8bef9SDimitry Andric 584e8d8bef9SDimitry Andric Asm->emitEncodingByte(CallSiteEncoding, "Call site"); 585e8d8bef9SDimitry Andric Asm->OutStreamer->emitULEB128IntValue(CallSiteTableSize); 586e8d8bef9SDimitry Andric }; 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric // SjLj / Wasm Exception handling 5890b57cec5SDimitry Andric if (IsSJLJ || IsWasm) { 590e8d8bef9SDimitry Andric Asm->OutStreamer->emitLabel(Asm->getMBBExceptionSym(Asm->MF->front())); 591e8d8bef9SDimitry Andric 592e8d8bef9SDimitry Andric // emit the LSDA header. 593e8d8bef9SDimitry Andric Asm->emitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); 594e8d8bef9SDimitry Andric EmitTypeTableRefAndCallSiteTableEndRef(); 595e8d8bef9SDimitry Andric 5960b57cec5SDimitry Andric unsigned idx = 0; 5970b57cec5SDimitry Andric for (SmallVectorImpl<CallSiteEntry>::const_iterator 5980b57cec5SDimitry Andric I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { 5990b57cec5SDimitry Andric const CallSiteEntry &S = *I; 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric // Index of the call site entry. 6020b57cec5SDimitry Andric if (VerboseAsm) { 6030b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Call Site " + Twine(idx) + " <<"); 6040b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" On exception at call site "+Twine(idx)); 6050b57cec5SDimitry Andric } 6065ffd83dbSDimitry Andric Asm->emitULEB128(idx); 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric // Offset of the first associated action record, relative to the start of 6090b57cec5SDimitry Andric // the action table. This value is biased by 1 (1 indicates the start of 6100b57cec5SDimitry Andric // the action table), and 0 indicates that there are no actions. 6110b57cec5SDimitry Andric if (VerboseAsm) { 6120b57cec5SDimitry Andric if (S.Action == 0) 6130b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Action: cleanup"); 6140b57cec5SDimitry Andric else 6150b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Action: " + 6160b57cec5SDimitry Andric Twine((S.Action - 1) / 2 + 1)); 6170b57cec5SDimitry Andric } 6185ffd83dbSDimitry Andric Asm->emitULEB128(S.Action); 6190b57cec5SDimitry Andric } 620e8d8bef9SDimitry Andric Asm->OutStreamer->emitLabel(CstEndLabel); 6210b57cec5SDimitry Andric } else { 6220b57cec5SDimitry Andric // Itanium LSDA exception handling 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric // The call-site table is a list of all call sites that may throw an 6250b57cec5SDimitry Andric // exception (including C++ 'throw' statements) in the procedure 6260b57cec5SDimitry Andric // fragment. It immediately follows the LSDA header. Each entry indicates, 6270b57cec5SDimitry Andric // for a given call, the first corresponding action record and corresponding 6280b57cec5SDimitry Andric // landing pad. 6290b57cec5SDimitry Andric // 6300b57cec5SDimitry Andric // The table begins with the number of bytes, stored as an LEB128 6310b57cec5SDimitry Andric // compressed, unsigned integer. The records immediately follow the record 6320b57cec5SDimitry Andric // count. They are sorted in increasing call-site address. Each record 6330b57cec5SDimitry Andric // indicates: 6340b57cec5SDimitry Andric // 6350b57cec5SDimitry Andric // * The position of the call-site. 6360b57cec5SDimitry Andric // * The position of the landing pad. 6370b57cec5SDimitry Andric // * The first action record for that call site. 6380b57cec5SDimitry Andric // 6390b57cec5SDimitry Andric // A missing entry in the call-site table indicates that a call is not 6400b57cec5SDimitry Andric // supposed to throw. 6410b57cec5SDimitry Andric 642e8d8bef9SDimitry Andric assert(CallSiteRanges.size() != 0 && "No call-site ranges!"); 6430b57cec5SDimitry Andric 644e8d8bef9SDimitry Andric // There should be only one call-site range which includes all the landing 645e8d8bef9SDimitry Andric // pads. Find that call-site range here. 646e8d8bef9SDimitry Andric const CallSiteRange *LandingPadRange = nullptr; 647e8d8bef9SDimitry Andric for (const CallSiteRange &CSRange : CallSiteRanges) { 648e8d8bef9SDimitry Andric if (CSRange.IsLPRange) { 649e8d8bef9SDimitry Andric assert(LandingPadRange == nullptr && 650e8d8bef9SDimitry Andric "All landing pads must be in a single callsite range."); 651e8d8bef9SDimitry Andric LandingPadRange = &CSRange; 652e8d8bef9SDimitry Andric } 653e8d8bef9SDimitry Andric } 654e8d8bef9SDimitry Andric 655e8d8bef9SDimitry Andric // The call-site table is split into its call-site ranges, each being 656e8d8bef9SDimitry Andric // emitted as: 657e8d8bef9SDimitry Andric // [ LPStartEncoding | LPStart ] 658e8d8bef9SDimitry Andric // [ TypeTableEncoding | TypeTableOffset ] 659e8d8bef9SDimitry Andric // [ CallSiteEncoding | CallSiteTableEndOffset ] 660e8d8bef9SDimitry Andric // cst_begin -> { call-site entries contained in this range } 661e8d8bef9SDimitry Andric // 662e8d8bef9SDimitry Andric // and is followed by the next call-site range. 663e8d8bef9SDimitry Andric // 664e8d8bef9SDimitry Andric // For each call-site range, CallSiteTableEndOffset is computed as the 665e8d8bef9SDimitry Andric // difference between cst_begin of that range and the last call-site-table's 666e8d8bef9SDimitry Andric // end label. This offset is used to find the action table. 667e8d8bef9SDimitry Andric 668e8d8bef9SDimitry Andric unsigned Entry = 0; 669e8d8bef9SDimitry Andric for (const CallSiteRange &CSRange : CallSiteRanges) { 670e8d8bef9SDimitry Andric if (CSRange.CallSiteBeginIdx != 0) { 671e8d8bef9SDimitry Andric // Align the call-site range for all ranges except the first. The 672e8d8bef9SDimitry Andric // first range is already aligned due to the exception table alignment. 673e8d8bef9SDimitry Andric Asm->emitAlignment(Align(4)); 674e8d8bef9SDimitry Andric } 675e8d8bef9SDimitry Andric Asm->OutStreamer->emitLabel(CSRange.ExceptionLabel); 676e8d8bef9SDimitry Andric 677e8d8bef9SDimitry Andric // Emit the LSDA header. 678bdd1243dSDimitry Andric // LPStart is omitted if either we have a single call-site range (in which 679bdd1243dSDimitry Andric // case the function entry is treated as @LPStart) or if this function has 680bdd1243dSDimitry Andric // no landing pads (in which case @LPStart is undefined). 681bdd1243dSDimitry Andric if (CallSiteRanges.size() == 1 || LandingPadRange == nullptr) { 682e8d8bef9SDimitry Andric Asm->emitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); 683e8d8bef9SDimitry Andric } else if (!Asm->isPositionIndependent()) { 684e8d8bef9SDimitry Andric // For more than one call-site ranges, LPStart must be explicitly 685e8d8bef9SDimitry Andric // specified. 686e8d8bef9SDimitry Andric // For non-PIC we can simply use the absolute value. 687e8d8bef9SDimitry Andric Asm->emitEncodingByte(dwarf::DW_EH_PE_absptr, "@LPStart"); 688e8d8bef9SDimitry Andric Asm->OutStreamer->emitSymbolValue(LandingPadRange->FragmentBeginLabel, 689e8d8bef9SDimitry Andric Asm->MAI->getCodePointerSize()); 690e8d8bef9SDimitry Andric } else { 691e8d8bef9SDimitry Andric // For PIC mode, we Emit a PC-relative address for LPStart. 692e8d8bef9SDimitry Andric Asm->emitEncodingByte(dwarf::DW_EH_PE_pcrel, "@LPStart"); 693e8d8bef9SDimitry Andric MCContext &Context = Asm->OutStreamer->getContext(); 694e8d8bef9SDimitry Andric MCSymbol *Dot = Context.createTempSymbol(); 695e8d8bef9SDimitry Andric Asm->OutStreamer->emitLabel(Dot); 696e8d8bef9SDimitry Andric Asm->OutStreamer->emitValue( 697e8d8bef9SDimitry Andric MCBinaryExpr::createSub( 698e8d8bef9SDimitry Andric MCSymbolRefExpr::create(LandingPadRange->FragmentBeginLabel, 699e8d8bef9SDimitry Andric Context), 700e8d8bef9SDimitry Andric MCSymbolRefExpr::create(Dot, Context), Context), 701e8d8bef9SDimitry Andric Asm->MAI->getCodePointerSize()); 702e8d8bef9SDimitry Andric } 703e8d8bef9SDimitry Andric 704e8d8bef9SDimitry Andric if (HasLEB128Directives) 705e8d8bef9SDimitry Andric EmitTypeTableRefAndCallSiteTableEndRef(); 706e8d8bef9SDimitry Andric else 707e8d8bef9SDimitry Andric EmitTypeTableOffsetAndCallSiteTableOffset(); 708e8d8bef9SDimitry Andric 709e8d8bef9SDimitry Andric for (size_t CallSiteIdx = CSRange.CallSiteBeginIdx; 710e8d8bef9SDimitry Andric CallSiteIdx != CSRange.CallSiteEndIdx; ++CallSiteIdx) { 711e8d8bef9SDimitry Andric const CallSiteEntry &S = CallSites[CallSiteIdx]; 712e8d8bef9SDimitry Andric 713e8d8bef9SDimitry Andric MCSymbol *EHFuncBeginSym = CSRange.FragmentBeginLabel; 714e8d8bef9SDimitry Andric MCSymbol *EHFuncEndSym = CSRange.FragmentEndLabel; 7150b57cec5SDimitry Andric 7160b57cec5SDimitry Andric MCSymbol *BeginLabel = S.BeginLabel; 7170b57cec5SDimitry Andric if (!BeginLabel) 7180b57cec5SDimitry Andric BeginLabel = EHFuncBeginSym; 7190b57cec5SDimitry Andric MCSymbol *EndLabel = S.EndLabel; 7200b57cec5SDimitry Andric if (!EndLabel) 721e8d8bef9SDimitry Andric EndLabel = EHFuncEndSym; 7220b57cec5SDimitry Andric 7230b57cec5SDimitry Andric // Offset of the call site relative to the start of the procedure. 7240b57cec5SDimitry Andric if (VerboseAsm) 725e8d8bef9SDimitry Andric Asm->OutStreamer->AddComment(">> Call Site " + Twine(++Entry) + 726e8d8bef9SDimitry Andric " <<"); 7275ffd83dbSDimitry Andric Asm->emitCallSiteOffset(BeginLabel, EHFuncBeginSym, CallSiteEncoding); 7280b57cec5SDimitry Andric if (VerboseAsm) 7290b57cec5SDimitry Andric Asm->OutStreamer->AddComment(Twine(" Call between ") + 7300b57cec5SDimitry Andric BeginLabel->getName() + " and " + 7310b57cec5SDimitry Andric EndLabel->getName()); 7325ffd83dbSDimitry Andric Asm->emitCallSiteOffset(EndLabel, BeginLabel, CallSiteEncoding); 7330b57cec5SDimitry Andric 734e8d8bef9SDimitry Andric // Offset of the landing pad relative to the start of the landing pad 735e8d8bef9SDimitry Andric // fragment. 7360b57cec5SDimitry Andric if (!S.LPad) { 7370b57cec5SDimitry Andric if (VerboseAsm) 7380b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" has no landing pad"); 7395ffd83dbSDimitry Andric Asm->emitCallSiteValue(0, CallSiteEncoding); 7400b57cec5SDimitry Andric } else { 7410b57cec5SDimitry Andric if (VerboseAsm) 7420b57cec5SDimitry Andric Asm->OutStreamer->AddComment(Twine(" jumps to ") + 7430b57cec5SDimitry Andric S.LPad->LandingPadLabel->getName()); 744e8d8bef9SDimitry Andric Asm->emitCallSiteOffset(S.LPad->LandingPadLabel, 745e8d8bef9SDimitry Andric LandingPadRange->FragmentBeginLabel, 7460b57cec5SDimitry Andric CallSiteEncoding); 7470b57cec5SDimitry Andric } 7480b57cec5SDimitry Andric 749e8d8bef9SDimitry Andric // Offset of the first associated action record, relative to the start 750e8d8bef9SDimitry Andric // of the action table. This value is biased by 1 (1 indicates the start 751e8d8bef9SDimitry Andric // of the action table), and 0 indicates that there are no actions. 7520b57cec5SDimitry Andric if (VerboseAsm) { 7530b57cec5SDimitry Andric if (S.Action == 0) 7540b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" On action: cleanup"); 7550b57cec5SDimitry Andric else 7560b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" On action: " + 7570b57cec5SDimitry Andric Twine((S.Action - 1) / 2 + 1)); 7580b57cec5SDimitry Andric } 7595ffd83dbSDimitry Andric Asm->emitULEB128(S.Action); 7600b57cec5SDimitry Andric } 7610b57cec5SDimitry Andric } 7625ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(CstEndLabel); 763e8d8bef9SDimitry Andric } 7640b57cec5SDimitry Andric 7650b57cec5SDimitry Andric // Emit the Action Table. 7660b57cec5SDimitry Andric int Entry = 0; 767fe6060f1SDimitry Andric for (const ActionEntry &Action : Actions) { 7680b57cec5SDimitry Andric if (VerboseAsm) { 7690b57cec5SDimitry Andric // Emit comments that decode the action table. 7700b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Action Record " + Twine(++Entry) + " <<"); 7710b57cec5SDimitry Andric } 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric // Type Filter 7740b57cec5SDimitry Andric // 7750b57cec5SDimitry Andric // Used by the runtime to match the type of the thrown exception to the 7760b57cec5SDimitry Andric // type of the catch clauses or the types in the exception specification. 7770b57cec5SDimitry Andric if (VerboseAsm) { 7780b57cec5SDimitry Andric if (Action.ValueForTypeID > 0) 7790b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Catch TypeInfo " + 7800b57cec5SDimitry Andric Twine(Action.ValueForTypeID)); 7810b57cec5SDimitry Andric else if (Action.ValueForTypeID < 0) 7820b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Filter TypeInfo " + 7830b57cec5SDimitry Andric Twine(Action.ValueForTypeID)); 7840b57cec5SDimitry Andric else 7850b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Cleanup"); 7860b57cec5SDimitry Andric } 7875ffd83dbSDimitry Andric Asm->emitSLEB128(Action.ValueForTypeID); 7880b57cec5SDimitry Andric 7890b57cec5SDimitry Andric // Action Record 7900b57cec5SDimitry Andric if (VerboseAsm) { 791e8d8bef9SDimitry Andric if (Action.Previous == unsigned(-1)) { 7920b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" No further actions"); 7930b57cec5SDimitry Andric } else { 794e8d8bef9SDimitry Andric Asm->OutStreamer->AddComment(" Continue to action " + 795e8d8bef9SDimitry Andric Twine(Action.Previous + 1)); 7960b57cec5SDimitry Andric } 7970b57cec5SDimitry Andric } 7985ffd83dbSDimitry Andric Asm->emitSLEB128(Action.NextAction); 7990b57cec5SDimitry Andric } 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric if (HaveTTData) { 8025ffd83dbSDimitry Andric Asm->emitAlignment(Align(4)); 8030b57cec5SDimitry Andric emitTypeInfos(TTypeEncoding, TTBaseLabel); 8040b57cec5SDimitry Andric } 8050b57cec5SDimitry Andric 8065ffd83dbSDimitry Andric Asm->emitAlignment(Align(4)); 8070b57cec5SDimitry Andric return GCCETSym; 8080b57cec5SDimitry Andric } 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric void EHStreamer::emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) { 8110b57cec5SDimitry Andric const MachineFunction *MF = Asm->MF; 8120b57cec5SDimitry Andric const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); 8130b57cec5SDimitry Andric const std::vector<unsigned> &FilterIds = MF->getFilterIds(); 8140b57cec5SDimitry Andric 815e8d8bef9SDimitry Andric const bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric int Entry = 0; 8180b57cec5SDimitry Andric // Emit the Catch TypeInfos. 8190b57cec5SDimitry Andric if (VerboseAsm && !TypeInfos.empty()) { 8200b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Catch TypeInfos <<"); 82181ad6265SDimitry Andric Asm->OutStreamer->addBlankLine(); 8220b57cec5SDimitry Andric Entry = TypeInfos.size(); 8230b57cec5SDimitry Andric } 8240b57cec5SDimitry Andric 825349cc55cSDimitry Andric for (const GlobalValue *GV : llvm::reverse(TypeInfos)) { 8260b57cec5SDimitry Andric if (VerboseAsm) 8270b57cec5SDimitry Andric Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--)); 8285ffd83dbSDimitry Andric Asm->emitTTypeReference(GV, TTypeEncoding); 8290b57cec5SDimitry Andric } 8300b57cec5SDimitry Andric 8315ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(TTBaseLabel); 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andric // Emit the Exception Specifications. 8340b57cec5SDimitry Andric if (VerboseAsm && !FilterIds.empty()) { 8350b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Filter TypeInfos <<"); 83681ad6265SDimitry Andric Asm->OutStreamer->addBlankLine(); 8370b57cec5SDimitry Andric Entry = 0; 8380b57cec5SDimitry Andric } 8390b57cec5SDimitry Andric for (std::vector<unsigned>::const_iterator 8400b57cec5SDimitry Andric I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 8410b57cec5SDimitry Andric unsigned TypeID = *I; 8420b57cec5SDimitry Andric if (VerboseAsm) { 8430b57cec5SDimitry Andric --Entry; 8440b57cec5SDimitry Andric if (isFilterEHSelector(TypeID)) 8450b57cec5SDimitry Andric Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry)); 8460b57cec5SDimitry Andric } 8470b57cec5SDimitry Andric 8485ffd83dbSDimitry Andric Asm->emitULEB128(TypeID); 8490b57cec5SDimitry Andric } 8500b57cec5SDimitry Andric } 851