10b57cec5SDimitry Andric //===- CodeGen/AsmPrinter/EHStreamer.cpp - Exception Directive Streamer ---===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file contains support for writing exception info into assembly files. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "EHStreamer.h" 140b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 150b57cec5SDimitry Andric #include "llvm/ADT/Twine.h" 160b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h" 170b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/AsmPrinter.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 220b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 230b57cec5SDimitry Andric #include "llvm/IR/Function.h" 240b57cec5SDimitry Andric #include "llvm/MC/MCAsmInfo.h" 250b57cec5SDimitry Andric #include "llvm/MC/MCContext.h" 260b57cec5SDimitry Andric #include "llvm/MC/MCStreamer.h" 270b57cec5SDimitry Andric #include "llvm/MC/MCSymbol.h" 280b57cec5SDimitry Andric #include "llvm/MC/MCTargetOptions.h" 290b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 300b57cec5SDimitry Andric #include "llvm/Support/LEB128.h" 310b57cec5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h" 320b57cec5SDimitry Andric #include <algorithm> 330b57cec5SDimitry Andric #include <cassert> 340b57cec5SDimitry Andric #include <cstdint> 350b57cec5SDimitry Andric #include <vector> 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric using namespace llvm; 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric EHStreamer::EHStreamer(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {} 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric EHStreamer::~EHStreamer() = default; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric /// How many leading type ids two landing pads have in common. 440b57cec5SDimitry Andric unsigned EHStreamer::sharedTypeIDs(const LandingPadInfo *L, 450b57cec5SDimitry Andric const LandingPadInfo *R) { 460b57cec5SDimitry Andric const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds; 470b57cec5SDimitry Andric unsigned LSize = LIds.size(), RSize = RIds.size(); 480b57cec5SDimitry Andric unsigned MinSize = LSize < RSize ? LSize : RSize; 490b57cec5SDimitry Andric unsigned Count = 0; 500b57cec5SDimitry Andric 510b57cec5SDimitry Andric for (; Count != MinSize; ++Count) 520b57cec5SDimitry Andric if (LIds[Count] != RIds[Count]) 530b57cec5SDimitry Andric return Count; 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric return Count; 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric /// Compute the actions table and gather the first action index for each landing 590b57cec5SDimitry Andric /// pad site. 600b57cec5SDimitry Andric void EHStreamer::computeActionsTable( 610b57cec5SDimitry Andric const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 620b57cec5SDimitry Andric SmallVectorImpl<ActionEntry> &Actions, 630b57cec5SDimitry Andric SmallVectorImpl<unsigned> &FirstActions) { 640b57cec5SDimitry Andric // The action table follows the call-site table in the LSDA. The individual 650b57cec5SDimitry Andric // records are of two types: 660b57cec5SDimitry Andric // 670b57cec5SDimitry Andric // * Catch clause 680b57cec5SDimitry Andric // * Exception specification 690b57cec5SDimitry Andric // 700b57cec5SDimitry Andric // The two record kinds have the same format, with only small differences. 710b57cec5SDimitry Andric // They are distinguished by the "switch value" field: Catch clauses 720b57cec5SDimitry Andric // (TypeInfos) have strictly positive switch values, and exception 730b57cec5SDimitry Andric // specifications (FilterIds) have strictly negative switch values. Value 0 740b57cec5SDimitry Andric // indicates a catch-all clause. 750b57cec5SDimitry Andric // 760b57cec5SDimitry Andric // Negative type IDs index into FilterIds. Positive type IDs index into 770b57cec5SDimitry Andric // TypeInfos. The value written for a positive type ID is just the type ID 780b57cec5SDimitry Andric // itself. For a negative type ID, however, the value written is the 790b57cec5SDimitry Andric // (negative) byte offset of the corresponding FilterIds entry. The byte 800b57cec5SDimitry Andric // offset is usually equal to the type ID (because the FilterIds entries are 810b57cec5SDimitry Andric // written using a variable width encoding, which outputs one byte per entry 820b57cec5SDimitry Andric // as long as the value written is not too large) but can differ. This kind 830b57cec5SDimitry Andric // of complication does not occur for positive type IDs because type infos are 840b57cec5SDimitry Andric // output using a fixed width encoding. FilterOffsets[i] holds the byte 850b57cec5SDimitry Andric // offset corresponding to FilterIds[i]. 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric const std::vector<unsigned> &FilterIds = Asm->MF->getFilterIds(); 880b57cec5SDimitry Andric SmallVector<int, 16> FilterOffsets; 890b57cec5SDimitry Andric FilterOffsets.reserve(FilterIds.size()); 900b57cec5SDimitry Andric int Offset = -1; 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric for (std::vector<unsigned>::const_iterator 930b57cec5SDimitry Andric I = FilterIds.begin(), E = FilterIds.end(); I != E; ++I) { 940b57cec5SDimitry Andric FilterOffsets.push_back(Offset); 950b57cec5SDimitry Andric Offset -= getULEB128Size(*I); 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric FirstActions.reserve(LandingPads.size()); 990b57cec5SDimitry Andric 1000b57cec5SDimitry Andric int FirstAction = 0; 1010b57cec5SDimitry Andric unsigned SizeActions = 0; // Total size of all action entries for a function 1020b57cec5SDimitry Andric const LandingPadInfo *PrevLPI = nullptr; 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric for (SmallVectorImpl<const LandingPadInfo *>::const_iterator 1050b57cec5SDimitry Andric I = LandingPads.begin(), E = LandingPads.end(); I != E; ++I) { 1060b57cec5SDimitry Andric const LandingPadInfo *LPI = *I; 1070b57cec5SDimitry Andric const std::vector<int> &TypeIds = LPI->TypeIds; 1080b57cec5SDimitry Andric unsigned NumShared = PrevLPI ? sharedTypeIDs(LPI, PrevLPI) : 0; 1090b57cec5SDimitry Andric unsigned SizeSiteActions = 0; // Total size of all entries for a landingpad 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric if (NumShared < TypeIds.size()) { 1120b57cec5SDimitry Andric // Size of one action entry (typeid + next action) 1130b57cec5SDimitry Andric unsigned SizeActionEntry = 0; 1140b57cec5SDimitry Andric unsigned PrevAction = (unsigned)-1; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric if (NumShared) { 1170b57cec5SDimitry Andric unsigned SizePrevIds = PrevLPI->TypeIds.size(); 1180b57cec5SDimitry Andric assert(Actions.size()); 1190b57cec5SDimitry Andric PrevAction = Actions.size() - 1; 1200b57cec5SDimitry Andric SizeActionEntry = getSLEB128Size(Actions[PrevAction].NextAction) + 1210b57cec5SDimitry Andric getSLEB128Size(Actions[PrevAction].ValueForTypeID); 1220b57cec5SDimitry Andric 1230b57cec5SDimitry Andric for (unsigned j = NumShared; j != SizePrevIds; ++j) { 1240b57cec5SDimitry Andric assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!"); 1250b57cec5SDimitry Andric SizeActionEntry -= getSLEB128Size(Actions[PrevAction].ValueForTypeID); 1260b57cec5SDimitry Andric SizeActionEntry += -Actions[PrevAction].NextAction; 1270b57cec5SDimitry Andric PrevAction = Actions[PrevAction].Previous; 1280b57cec5SDimitry Andric } 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric // Compute the actions. 1320b57cec5SDimitry Andric for (unsigned J = NumShared, M = TypeIds.size(); J != M; ++J) { 1330b57cec5SDimitry Andric int TypeID = TypeIds[J]; 1340b57cec5SDimitry Andric assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!"); 1350b57cec5SDimitry Andric int ValueForTypeID = 1360b57cec5SDimitry Andric isFilterEHSelector(TypeID) ? FilterOffsets[-1 - TypeID] : TypeID; 1370b57cec5SDimitry Andric unsigned SizeTypeID = getSLEB128Size(ValueForTypeID); 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric int NextAction = SizeActionEntry ? -(SizeActionEntry + SizeTypeID) : 0; 1400b57cec5SDimitry Andric SizeActionEntry = SizeTypeID + getSLEB128Size(NextAction); 1410b57cec5SDimitry Andric SizeSiteActions += SizeActionEntry; 1420b57cec5SDimitry Andric 1430b57cec5SDimitry Andric ActionEntry Action = { ValueForTypeID, NextAction, PrevAction }; 1440b57cec5SDimitry Andric Actions.push_back(Action); 1450b57cec5SDimitry Andric PrevAction = Actions.size() - 1; 1460b57cec5SDimitry Andric } 1470b57cec5SDimitry Andric 1480b57cec5SDimitry Andric // Record the first action of the landing pad site. 1490b57cec5SDimitry Andric FirstAction = SizeActions + SizeSiteActions - SizeActionEntry + 1; 1500b57cec5SDimitry Andric } // else identical - re-use previous FirstAction 1510b57cec5SDimitry Andric 1520b57cec5SDimitry Andric // Information used when creating the call-site table. The action record 1530b57cec5SDimitry Andric // field of the call site record is the offset of the first associated 1540b57cec5SDimitry Andric // action record, relative to the start of the actions table. This value is 1550b57cec5SDimitry Andric // biased by 1 (1 indicating the start of the actions table), and 0 1560b57cec5SDimitry Andric // indicates that there are no actions. 1570b57cec5SDimitry Andric FirstActions.push_back(FirstAction); 1580b57cec5SDimitry Andric 1590b57cec5SDimitry Andric // Compute this sites contribution to size. 1600b57cec5SDimitry Andric SizeActions += SizeSiteActions; 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric PrevLPI = LPI; 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric /// Return `true' if this is a call to a function marked `nounwind'. Return 1670b57cec5SDimitry Andric /// `false' otherwise. 1680b57cec5SDimitry Andric bool EHStreamer::callToNoUnwindFunction(const MachineInstr *MI) { 1690b57cec5SDimitry Andric assert(MI->isCall() && "This should be a call instruction!"); 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric bool MarkedNoUnwind = false; 1720b57cec5SDimitry Andric bool SawFunc = false; 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) { 1750b57cec5SDimitry Andric const MachineOperand &MO = MI->getOperand(I); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric if (!MO.isGlobal()) continue; 1780b57cec5SDimitry Andric 1790b57cec5SDimitry Andric const Function *F = dyn_cast<Function>(MO.getGlobal()); 1800b57cec5SDimitry Andric if (!F) continue; 1810b57cec5SDimitry Andric 1820b57cec5SDimitry Andric if (SawFunc) { 1830b57cec5SDimitry Andric // Be conservative. If we have more than one function operand for this 1840b57cec5SDimitry Andric // call, then we can't make the assumption that it's the callee and 1850b57cec5SDimitry Andric // not a parameter to the call. 1860b57cec5SDimitry Andric // 1870b57cec5SDimitry Andric // FIXME: Determine if there's a way to say that `F' is the callee or 1880b57cec5SDimitry Andric // parameter. 1890b57cec5SDimitry Andric MarkedNoUnwind = false; 1900b57cec5SDimitry Andric break; 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric MarkedNoUnwind = F->doesNotThrow(); 1940b57cec5SDimitry Andric SawFunc = true; 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric return MarkedNoUnwind; 1980b57cec5SDimitry Andric } 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric void EHStreamer::computePadMap( 2010b57cec5SDimitry Andric const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 2020b57cec5SDimitry Andric RangeMapType &PadMap) { 2030b57cec5SDimitry Andric // Invokes and nounwind calls have entries in PadMap (due to being bracketed 2040b57cec5SDimitry Andric // by try-range labels when lowered). Ordinary calls do not, so appropriate 2050b57cec5SDimitry Andric // try-ranges for them need be deduced so we can put them in the LSDA. 2060b57cec5SDimitry Andric for (unsigned i = 0, N = LandingPads.size(); i != N; ++i) { 2070b57cec5SDimitry Andric const LandingPadInfo *LandingPad = LandingPads[i]; 2080b57cec5SDimitry Andric for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; ++j) { 2090b57cec5SDimitry Andric MCSymbol *BeginLabel = LandingPad->BeginLabels[j]; 2100b57cec5SDimitry Andric assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!"); 2110b57cec5SDimitry Andric PadRange P = { i, j }; 2120b57cec5SDimitry Andric PadMap[BeginLabel] = P; 2130b57cec5SDimitry Andric } 2140b57cec5SDimitry Andric } 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric /// Compute the call-site table. The entry for an invoke has a try-range 2180b57cec5SDimitry Andric /// containing the call, a non-zero landing pad, and an appropriate action. The 2190b57cec5SDimitry Andric /// entry for an ordinary call has a try-range containing the call and zero for 2200b57cec5SDimitry Andric /// the landing pad and the action. Calls marked 'nounwind' have no entry and 2210b57cec5SDimitry Andric /// must not be contained in the try-range of any entry - they form gaps in the 2220b57cec5SDimitry Andric /// table. Entries must be ordered by try-range address. 2230b57cec5SDimitry Andric void EHStreamer:: 2240b57cec5SDimitry Andric computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites, 2250b57cec5SDimitry Andric const SmallVectorImpl<const LandingPadInfo *> &LandingPads, 2260b57cec5SDimitry Andric const SmallVectorImpl<unsigned> &FirstActions) { 2270b57cec5SDimitry Andric RangeMapType PadMap; 2280b57cec5SDimitry Andric computePadMap(LandingPads, PadMap); 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric // The end label of the previous invoke or nounwind try-range. 2310b57cec5SDimitry Andric MCSymbol *LastLabel = nullptr; 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric // Whether there is a potentially throwing instruction (currently this means 2340b57cec5SDimitry Andric // an ordinary call) between the end of the previous try-range and now. 2350b57cec5SDimitry Andric bool SawPotentiallyThrowing = false; 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // Whether the last CallSite entry was for an invoke. 2380b57cec5SDimitry Andric bool PreviousIsInvoke = false; 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric // Visit all instructions in order of address. 2430b57cec5SDimitry Andric for (const auto &MBB : *Asm->MF) { 2440b57cec5SDimitry Andric for (const auto &MI : MBB) { 2450b57cec5SDimitry Andric if (!MI.isEHLabel()) { 2460b57cec5SDimitry Andric if (MI.isCall()) 2470b57cec5SDimitry Andric SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI); 2480b57cec5SDimitry Andric continue; 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric // End of the previous try-range? 2520b57cec5SDimitry Andric MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol(); 2530b57cec5SDimitry Andric if (BeginLabel == LastLabel) 2540b57cec5SDimitry Andric SawPotentiallyThrowing = false; 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric // Beginning of a new try-range? 2570b57cec5SDimitry Andric RangeMapType::const_iterator L = PadMap.find(BeginLabel); 2580b57cec5SDimitry Andric if (L == PadMap.end()) 2590b57cec5SDimitry Andric // Nope, it was just some random label. 2600b57cec5SDimitry Andric continue; 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric const PadRange &P = L->second; 2630b57cec5SDimitry Andric const LandingPadInfo *LandingPad = LandingPads[P.PadIndex]; 2640b57cec5SDimitry Andric assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] && 2650b57cec5SDimitry Andric "Inconsistent landing pad map!"); 2660b57cec5SDimitry Andric 2670b57cec5SDimitry Andric // For Dwarf exception handling (SjLj handling doesn't use this). If some 2680b57cec5SDimitry Andric // instruction between the previous try-range and this one may throw, 2690b57cec5SDimitry Andric // create a call-site entry with no landing pad for the region between the 2700b57cec5SDimitry Andric // try-ranges. 2710b57cec5SDimitry Andric if (SawPotentiallyThrowing && Asm->MAI->usesCFIForEH()) { 2720b57cec5SDimitry Andric CallSiteEntry Site = { LastLabel, BeginLabel, nullptr, 0 }; 2730b57cec5SDimitry Andric CallSites.push_back(Site); 2740b57cec5SDimitry Andric PreviousIsInvoke = false; 2750b57cec5SDimitry Andric } 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric LastLabel = LandingPad->EndLabels[P.RangeIndex]; 2780b57cec5SDimitry Andric assert(BeginLabel && LastLabel && "Invalid landing pad!"); 2790b57cec5SDimitry Andric 2800b57cec5SDimitry Andric if (!LandingPad->LandingPadLabel) { 2810b57cec5SDimitry Andric // Create a gap. 2820b57cec5SDimitry Andric PreviousIsInvoke = false; 2830b57cec5SDimitry Andric } else { 2840b57cec5SDimitry Andric // This try-range is for an invoke. 2850b57cec5SDimitry Andric CallSiteEntry Site = { 2860b57cec5SDimitry Andric BeginLabel, 2870b57cec5SDimitry Andric LastLabel, 2880b57cec5SDimitry Andric LandingPad, 2890b57cec5SDimitry Andric FirstActions[P.PadIndex] 2900b57cec5SDimitry Andric }; 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // Try to merge with the previous call-site. SJLJ doesn't do this 2930b57cec5SDimitry Andric if (PreviousIsInvoke && !IsSJLJ) { 2940b57cec5SDimitry Andric CallSiteEntry &Prev = CallSites.back(); 2950b57cec5SDimitry Andric if (Site.LPad == Prev.LPad && Site.Action == Prev.Action) { 2960b57cec5SDimitry Andric // Extend the range of the previous entry. 2970b57cec5SDimitry Andric Prev.EndLabel = Site.EndLabel; 2980b57cec5SDimitry Andric continue; 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric } 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric // Otherwise, create a new call-site. 3030b57cec5SDimitry Andric if (!IsSJLJ) 3040b57cec5SDimitry Andric CallSites.push_back(Site); 3050b57cec5SDimitry Andric else { 3060b57cec5SDimitry Andric // SjLj EH must maintain the call sites in the order assigned 3070b57cec5SDimitry Andric // to them by the SjLjPrepare pass. 3080b57cec5SDimitry Andric unsigned SiteNo = Asm->MF->getCallSiteBeginLabel(BeginLabel); 3090b57cec5SDimitry Andric if (CallSites.size() < SiteNo) 3100b57cec5SDimitry Andric CallSites.resize(SiteNo); 3110b57cec5SDimitry Andric CallSites[SiteNo - 1] = Site; 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric PreviousIsInvoke = true; 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric } 3160b57cec5SDimitry Andric } 3170b57cec5SDimitry Andric 3180b57cec5SDimitry Andric // If some instruction between the previous try-range and the end of the 3190b57cec5SDimitry Andric // function may throw, create a call-site entry with no landing pad for the 3200b57cec5SDimitry Andric // region following the try-range. 3210b57cec5SDimitry Andric if (SawPotentiallyThrowing && !IsSJLJ) { 3220b57cec5SDimitry Andric CallSiteEntry Site = { LastLabel, nullptr, nullptr, 0 }; 3230b57cec5SDimitry Andric CallSites.push_back(Site); 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric /// Emit landing pads and actions. 3280b57cec5SDimitry Andric /// 3290b57cec5SDimitry Andric /// The general organization of the table is complex, but the basic concepts are 3300b57cec5SDimitry Andric /// easy. First there is a header which describes the location and organization 3310b57cec5SDimitry Andric /// of the three components that follow. 3320b57cec5SDimitry Andric /// 3330b57cec5SDimitry Andric /// 1. The landing pad site information describes the range of code covered by 3340b57cec5SDimitry Andric /// the try. In our case it's an accumulation of the ranges covered by the 3350b57cec5SDimitry Andric /// invokes in the try. There is also a reference to the landing pad that 3360b57cec5SDimitry Andric /// handles the exception once processed. Finally an index into the actions 3370b57cec5SDimitry Andric /// table. 3380b57cec5SDimitry Andric /// 2. The action table, in our case, is composed of pairs of type IDs and next 3390b57cec5SDimitry Andric /// action offset. Starting with the action index from the landing pad 3400b57cec5SDimitry Andric /// site, each type ID is checked for a match to the current exception. If 3410b57cec5SDimitry Andric /// it matches then the exception and type id are passed on to the landing 3420b57cec5SDimitry Andric /// pad. Otherwise the next action is looked up. This chain is terminated 3430b57cec5SDimitry Andric /// with a next action of zero. If no type id is found then the frame is 3440b57cec5SDimitry Andric /// unwound and handling continues. 3450b57cec5SDimitry Andric /// 3. Type ID table contains references to all the C++ typeinfo for all 3460b57cec5SDimitry Andric /// catches in the function. This tables is reverse indexed base 1. 3470b57cec5SDimitry Andric /// 3480b57cec5SDimitry Andric /// Returns the starting symbol of an exception table. 3490b57cec5SDimitry Andric MCSymbol *EHStreamer::emitExceptionTable() { 3500b57cec5SDimitry Andric const MachineFunction *MF = Asm->MF; 3510b57cec5SDimitry Andric const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); 3520b57cec5SDimitry Andric const std::vector<unsigned> &FilterIds = MF->getFilterIds(); 3530b57cec5SDimitry Andric const std::vector<LandingPadInfo> &PadInfos = MF->getLandingPads(); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric // Sort the landing pads in order of their type ids. This is used to fold 3560b57cec5SDimitry Andric // duplicate actions. 3570b57cec5SDimitry Andric SmallVector<const LandingPadInfo *, 64> LandingPads; 3580b57cec5SDimitry Andric LandingPads.reserve(PadInfos.size()); 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric for (unsigned i = 0, N = PadInfos.size(); i != N; ++i) 3610b57cec5SDimitry Andric LandingPads.push_back(&PadInfos[i]); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric // Order landing pads lexicographically by type id. 3640b57cec5SDimitry Andric llvm::sort(LandingPads, [](const LandingPadInfo *L, const LandingPadInfo *R) { 3650b57cec5SDimitry Andric return L->TypeIds < R->TypeIds; 3660b57cec5SDimitry Andric }); 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric // Compute the actions table and gather the first action index for each 3690b57cec5SDimitry Andric // landing pad site. 3700b57cec5SDimitry Andric SmallVector<ActionEntry, 32> Actions; 3710b57cec5SDimitry Andric SmallVector<unsigned, 64> FirstActions; 3720b57cec5SDimitry Andric computeActionsTable(LandingPads, Actions, FirstActions); 3730b57cec5SDimitry Andric 3740b57cec5SDimitry Andric // Compute the call-site table. 3750b57cec5SDimitry Andric SmallVector<CallSiteEntry, 64> CallSites; 3760b57cec5SDimitry Andric computeCallSiteTable(CallSites, LandingPads, FirstActions); 3770b57cec5SDimitry Andric 3780b57cec5SDimitry Andric bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj; 3790b57cec5SDimitry Andric bool IsWasm = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::Wasm; 3800b57cec5SDimitry Andric unsigned CallSiteEncoding = 3810b57cec5SDimitry Andric IsSJLJ ? static_cast<unsigned>(dwarf::DW_EH_PE_udata4) : 3820b57cec5SDimitry Andric Asm->getObjFileLowering().getCallSiteEncoding(); 3830b57cec5SDimitry Andric bool HaveTTData = !TypeInfos.empty() || !FilterIds.empty(); 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric // Type infos. 3860b57cec5SDimitry Andric MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection(); 3870b57cec5SDimitry Andric unsigned TTypeEncoding; 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric if (!HaveTTData) { 3900b57cec5SDimitry Andric // If there is no TypeInfo, then we just explicitly say that we're omitting 3910b57cec5SDimitry Andric // that bit. 3920b57cec5SDimitry Andric TTypeEncoding = dwarf::DW_EH_PE_omit; 3930b57cec5SDimitry Andric } else { 3940b57cec5SDimitry Andric // Okay, we have actual filters or typeinfos to emit. As such, we need to 3950b57cec5SDimitry Andric // pick a type encoding for them. We're about to emit a list of pointers to 3960b57cec5SDimitry Andric // typeinfo objects at the end of the LSDA. However, unless we're in static 3970b57cec5SDimitry Andric // mode, this reference will require a relocation by the dynamic linker. 3980b57cec5SDimitry Andric // 3990b57cec5SDimitry Andric // Because of this, we have a couple of options: 4000b57cec5SDimitry Andric // 4010b57cec5SDimitry Andric // 1) If we are in -static mode, we can always use an absolute reference 4020b57cec5SDimitry Andric // from the LSDA, because the static linker will resolve it. 4030b57cec5SDimitry Andric // 4040b57cec5SDimitry Andric // 2) Otherwise, if the LSDA section is writable, we can output the direct 4050b57cec5SDimitry Andric // reference to the typeinfo and allow the dynamic linker to relocate 4060b57cec5SDimitry Andric // it. Since it is in a writable section, the dynamic linker won't 4070b57cec5SDimitry Andric // have a problem. 4080b57cec5SDimitry Andric // 4090b57cec5SDimitry Andric // 3) Finally, if we're in PIC mode and the LDSA section isn't writable, 4100b57cec5SDimitry Andric // we need to use some form of indirection. For example, on Darwin, 4110b57cec5SDimitry Andric // we can output a statically-relocatable reference to a dyld stub. The 4120b57cec5SDimitry Andric // offset to the stub is constant, but the contents are in a section 4130b57cec5SDimitry Andric // that is updated by the dynamic linker. This is easy enough, but we 4140b57cec5SDimitry Andric // need to tell the personality function of the unwinder to indirect 4150b57cec5SDimitry Andric // through the dyld stub. 4160b57cec5SDimitry Andric // 4170b57cec5SDimitry Andric // FIXME: When (3) is actually implemented, we'll have to emit the stubs 4180b57cec5SDimitry Andric // somewhere. This predicate should be moved to a shared location that is 4190b57cec5SDimitry Andric // in target-independent code. 4200b57cec5SDimitry Andric // 4210b57cec5SDimitry Andric TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding(); 4220b57cec5SDimitry Andric } 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric // Begin the exception table. 4250b57cec5SDimitry Andric // Sometimes we want not to emit the data into separate section (e.g. ARM 4260b57cec5SDimitry Andric // EHABI). In this case LSDASection will be NULL. 4270b57cec5SDimitry Andric if (LSDASection) 4280b57cec5SDimitry Andric Asm->OutStreamer->SwitchSection(LSDASection); 429*5ffd83dbSDimitry Andric Asm->emitAlignment(Align(4)); 4300b57cec5SDimitry Andric 4310b57cec5SDimitry Andric // Emit the LSDA. 4320b57cec5SDimitry Andric MCSymbol *GCCETSym = 4330b57cec5SDimitry Andric Asm->OutContext.getOrCreateSymbol(Twine("GCC_except_table")+ 4340b57cec5SDimitry Andric Twine(Asm->getFunctionNumber())); 435*5ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(GCCETSym); 436*5ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(Asm->getCurExceptionSym()); 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric // Emit the LSDA header. 439*5ffd83dbSDimitry Andric Asm->emitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart"); 440*5ffd83dbSDimitry Andric Asm->emitEncodingByte(TTypeEncoding, "@TType"); 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric MCSymbol *TTBaseLabel = nullptr; 4430b57cec5SDimitry Andric if (HaveTTData) { 4440b57cec5SDimitry Andric // N.B.: There is a dependency loop between the size of the TTBase uleb128 4450b57cec5SDimitry Andric // here and the amount of padding before the aligned type table. The 4460b57cec5SDimitry Andric // assembler must sometimes pad this uleb128 or insert extra padding before 4470b57cec5SDimitry Andric // the type table. See PR35809 or GNU as bug 4029. 4480b57cec5SDimitry Andric MCSymbol *TTBaseRefLabel = Asm->createTempSymbol("ttbaseref"); 4490b57cec5SDimitry Andric TTBaseLabel = Asm->createTempSymbol("ttbase"); 450*5ffd83dbSDimitry Andric Asm->emitLabelDifferenceAsULEB128(TTBaseLabel, TTBaseRefLabel); 451*5ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(TTBaseRefLabel); 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric // Emit the landing pad call site table. 4570b57cec5SDimitry Andric MCSymbol *CstBeginLabel = Asm->createTempSymbol("cst_begin"); 4580b57cec5SDimitry Andric MCSymbol *CstEndLabel = Asm->createTempSymbol("cst_end"); 459*5ffd83dbSDimitry Andric Asm->emitEncodingByte(CallSiteEncoding, "Call site"); 460*5ffd83dbSDimitry Andric Asm->emitLabelDifferenceAsULEB128(CstEndLabel, CstBeginLabel); 461*5ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(CstBeginLabel); 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric // SjLj / Wasm Exception handling 4640b57cec5SDimitry Andric if (IsSJLJ || IsWasm) { 4650b57cec5SDimitry Andric unsigned idx = 0; 4660b57cec5SDimitry Andric for (SmallVectorImpl<CallSiteEntry>::const_iterator 4670b57cec5SDimitry Andric I = CallSites.begin(), E = CallSites.end(); I != E; ++I, ++idx) { 4680b57cec5SDimitry Andric const CallSiteEntry &S = *I; 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric // Index of the call site entry. 4710b57cec5SDimitry Andric if (VerboseAsm) { 4720b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Call Site " + Twine(idx) + " <<"); 4730b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" On exception at call site "+Twine(idx)); 4740b57cec5SDimitry Andric } 475*5ffd83dbSDimitry Andric Asm->emitULEB128(idx); 4760b57cec5SDimitry Andric 4770b57cec5SDimitry Andric // Offset of the first associated action record, relative to the start of 4780b57cec5SDimitry Andric // the action table. This value is biased by 1 (1 indicates the start of 4790b57cec5SDimitry Andric // the action table), and 0 indicates that there are no actions. 4800b57cec5SDimitry Andric if (VerboseAsm) { 4810b57cec5SDimitry Andric if (S.Action == 0) 4820b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Action: cleanup"); 4830b57cec5SDimitry Andric else 4840b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Action: " + 4850b57cec5SDimitry Andric Twine((S.Action - 1) / 2 + 1)); 4860b57cec5SDimitry Andric } 487*5ffd83dbSDimitry Andric Asm->emitULEB128(S.Action); 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric } else { 4900b57cec5SDimitry Andric // Itanium LSDA exception handling 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric // The call-site table is a list of all call sites that may throw an 4930b57cec5SDimitry Andric // exception (including C++ 'throw' statements) in the procedure 4940b57cec5SDimitry Andric // fragment. It immediately follows the LSDA header. Each entry indicates, 4950b57cec5SDimitry Andric // for a given call, the first corresponding action record and corresponding 4960b57cec5SDimitry Andric // landing pad. 4970b57cec5SDimitry Andric // 4980b57cec5SDimitry Andric // The table begins with the number of bytes, stored as an LEB128 4990b57cec5SDimitry Andric // compressed, unsigned integer. The records immediately follow the record 5000b57cec5SDimitry Andric // count. They are sorted in increasing call-site address. Each record 5010b57cec5SDimitry Andric // indicates: 5020b57cec5SDimitry Andric // 5030b57cec5SDimitry Andric // * The position of the call-site. 5040b57cec5SDimitry Andric // * The position of the landing pad. 5050b57cec5SDimitry Andric // * The first action record for that call site. 5060b57cec5SDimitry Andric // 5070b57cec5SDimitry Andric // A missing entry in the call-site table indicates that a call is not 5080b57cec5SDimitry Andric // supposed to throw. 5090b57cec5SDimitry Andric 5100b57cec5SDimitry Andric unsigned Entry = 0; 5110b57cec5SDimitry Andric for (SmallVectorImpl<CallSiteEntry>::const_iterator 5120b57cec5SDimitry Andric I = CallSites.begin(), E = CallSites.end(); I != E; ++I) { 5130b57cec5SDimitry Andric const CallSiteEntry &S = *I; 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin(); 5160b57cec5SDimitry Andric 5170b57cec5SDimitry Andric MCSymbol *BeginLabel = S.BeginLabel; 5180b57cec5SDimitry Andric if (!BeginLabel) 5190b57cec5SDimitry Andric BeginLabel = EHFuncBeginSym; 5200b57cec5SDimitry Andric MCSymbol *EndLabel = S.EndLabel; 5210b57cec5SDimitry Andric if (!EndLabel) 5220b57cec5SDimitry Andric EndLabel = Asm->getFunctionEnd(); 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric // Offset of the call site relative to the start of the procedure. 5250b57cec5SDimitry Andric if (VerboseAsm) 5260b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Call Site " + Twine(++Entry) + " <<"); 527*5ffd83dbSDimitry Andric Asm->emitCallSiteOffset(BeginLabel, EHFuncBeginSym, CallSiteEncoding); 5280b57cec5SDimitry Andric if (VerboseAsm) 5290b57cec5SDimitry Andric Asm->OutStreamer->AddComment(Twine(" Call between ") + 5300b57cec5SDimitry Andric BeginLabel->getName() + " and " + 5310b57cec5SDimitry Andric EndLabel->getName()); 532*5ffd83dbSDimitry Andric Asm->emitCallSiteOffset(EndLabel, BeginLabel, CallSiteEncoding); 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric // Offset of the landing pad relative to the start of the procedure. 5350b57cec5SDimitry Andric if (!S.LPad) { 5360b57cec5SDimitry Andric if (VerboseAsm) 5370b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" has no landing pad"); 538*5ffd83dbSDimitry Andric Asm->emitCallSiteValue(0, CallSiteEncoding); 5390b57cec5SDimitry Andric } else { 5400b57cec5SDimitry Andric if (VerboseAsm) 5410b57cec5SDimitry Andric Asm->OutStreamer->AddComment(Twine(" jumps to ") + 5420b57cec5SDimitry Andric S.LPad->LandingPadLabel->getName()); 543*5ffd83dbSDimitry Andric Asm->emitCallSiteOffset(S.LPad->LandingPadLabel, EHFuncBeginSym, 5440b57cec5SDimitry Andric CallSiteEncoding); 5450b57cec5SDimitry Andric } 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric // Offset of the first associated action record, relative to the start of 5480b57cec5SDimitry Andric // the action table. This value is biased by 1 (1 indicates the start of 5490b57cec5SDimitry Andric // the action table), and 0 indicates that there are no actions. 5500b57cec5SDimitry Andric if (VerboseAsm) { 5510b57cec5SDimitry Andric if (S.Action == 0) 5520b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" On action: cleanup"); 5530b57cec5SDimitry Andric else 5540b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" On action: " + 5550b57cec5SDimitry Andric Twine((S.Action - 1) / 2 + 1)); 5560b57cec5SDimitry Andric } 557*5ffd83dbSDimitry Andric Asm->emitULEB128(S.Action); 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric } 560*5ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(CstEndLabel); 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric // Emit the Action Table. 5630b57cec5SDimitry Andric int Entry = 0; 5640b57cec5SDimitry Andric for (SmallVectorImpl<ActionEntry>::const_iterator 5650b57cec5SDimitry Andric I = Actions.begin(), E = Actions.end(); I != E; ++I) { 5660b57cec5SDimitry Andric const ActionEntry &Action = *I; 5670b57cec5SDimitry Andric 5680b57cec5SDimitry Andric if (VerboseAsm) { 5690b57cec5SDimitry Andric // Emit comments that decode the action table. 5700b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Action Record " + Twine(++Entry) + " <<"); 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric // Type Filter 5740b57cec5SDimitry Andric // 5750b57cec5SDimitry Andric // Used by the runtime to match the type of the thrown exception to the 5760b57cec5SDimitry Andric // type of the catch clauses or the types in the exception specification. 5770b57cec5SDimitry Andric if (VerboseAsm) { 5780b57cec5SDimitry Andric if (Action.ValueForTypeID > 0) 5790b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Catch TypeInfo " + 5800b57cec5SDimitry Andric Twine(Action.ValueForTypeID)); 5810b57cec5SDimitry Andric else if (Action.ValueForTypeID < 0) 5820b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Filter TypeInfo " + 5830b57cec5SDimitry Andric Twine(Action.ValueForTypeID)); 5840b57cec5SDimitry Andric else 5850b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Cleanup"); 5860b57cec5SDimitry Andric } 587*5ffd83dbSDimitry Andric Asm->emitSLEB128(Action.ValueForTypeID); 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric // Action Record 5900b57cec5SDimitry Andric // 5910b57cec5SDimitry Andric // Self-relative signed displacement in bytes of the next action record, 5920b57cec5SDimitry Andric // or 0 if there is no next action record. 5930b57cec5SDimitry Andric if (VerboseAsm) { 5940b57cec5SDimitry Andric if (Action.NextAction == 0) { 5950b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" No further actions"); 5960b57cec5SDimitry Andric } else { 5970b57cec5SDimitry Andric unsigned NextAction = Entry + (Action.NextAction + 1) / 2; 5980b57cec5SDimitry Andric Asm->OutStreamer->AddComment(" Continue to action "+Twine(NextAction)); 5990b57cec5SDimitry Andric } 6000b57cec5SDimitry Andric } 601*5ffd83dbSDimitry Andric Asm->emitSLEB128(Action.NextAction); 6020b57cec5SDimitry Andric } 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric if (HaveTTData) { 605*5ffd83dbSDimitry Andric Asm->emitAlignment(Align(4)); 6060b57cec5SDimitry Andric emitTypeInfos(TTypeEncoding, TTBaseLabel); 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric 609*5ffd83dbSDimitry Andric Asm->emitAlignment(Align(4)); 6100b57cec5SDimitry Andric return GCCETSym; 6110b57cec5SDimitry Andric } 6120b57cec5SDimitry Andric 6130b57cec5SDimitry Andric void EHStreamer::emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) { 6140b57cec5SDimitry Andric const MachineFunction *MF = Asm->MF; 6150b57cec5SDimitry Andric const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos(); 6160b57cec5SDimitry Andric const std::vector<unsigned> &FilterIds = MF->getFilterIds(); 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric bool VerboseAsm = Asm->OutStreamer->isVerboseAsm(); 6190b57cec5SDimitry Andric 6200b57cec5SDimitry Andric int Entry = 0; 6210b57cec5SDimitry Andric // Emit the Catch TypeInfos. 6220b57cec5SDimitry Andric if (VerboseAsm && !TypeInfos.empty()) { 6230b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Catch TypeInfos <<"); 6240b57cec5SDimitry Andric Asm->OutStreamer->AddBlankLine(); 6250b57cec5SDimitry Andric Entry = TypeInfos.size(); 6260b57cec5SDimitry Andric } 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric for (const GlobalValue *GV : make_range(TypeInfos.rbegin(), 6290b57cec5SDimitry Andric TypeInfos.rend())) { 6300b57cec5SDimitry Andric if (VerboseAsm) 6310b57cec5SDimitry Andric Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--)); 632*5ffd83dbSDimitry Andric Asm->emitTTypeReference(GV, TTypeEncoding); 6330b57cec5SDimitry Andric } 6340b57cec5SDimitry Andric 635*5ffd83dbSDimitry Andric Asm->OutStreamer->emitLabel(TTBaseLabel); 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric // Emit the Exception Specifications. 6380b57cec5SDimitry Andric if (VerboseAsm && !FilterIds.empty()) { 6390b57cec5SDimitry Andric Asm->OutStreamer->AddComment(">> Filter TypeInfos <<"); 6400b57cec5SDimitry Andric Asm->OutStreamer->AddBlankLine(); 6410b57cec5SDimitry Andric Entry = 0; 6420b57cec5SDimitry Andric } 6430b57cec5SDimitry Andric for (std::vector<unsigned>::const_iterator 6440b57cec5SDimitry Andric I = FilterIds.begin(), E = FilterIds.end(); I < E; ++I) { 6450b57cec5SDimitry Andric unsigned TypeID = *I; 6460b57cec5SDimitry Andric if (VerboseAsm) { 6470b57cec5SDimitry Andric --Entry; 6480b57cec5SDimitry Andric if (isFilterEHSelector(TypeID)) 6490b57cec5SDimitry Andric Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry)); 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric 652*5ffd83dbSDimitry Andric Asm->emitULEB128(TypeID); 6530b57cec5SDimitry Andric } 6540b57cec5SDimitry Andric } 655