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