Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- CodeGen/AsmPrinter/EHStreamer.cpp - Exception Directive Streamer ---===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
// This file contains support for writing exception info into assembly files.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "EHStreamer.h"
14
#include "llvm/ADT/SmallVector.h"
15
#include "llvm/ADT/Twine.h"
16
#include "llvm/ADT/iterator_range.h"
17
#include "llvm/BinaryFormat/Dwarf.h"
18
#include "llvm/CodeGen/AsmPrinter.h"
19
#include "llvm/CodeGen/MachineFunction.h"
20
#include "llvm/CodeGen/MachineInstr.h"
21
#include "llvm/CodeGen/MachineOperand.h"
22
#include "llvm/IR/DataLayout.h"
23
#include "llvm/IR/Function.h"
24
#include "llvm/MC/MCAsmInfo.h"
25
#include "llvm/MC/MCContext.h"
26
#include "llvm/MC/MCStreamer.h"
27
#include "llvm/MC/MCSymbol.h"
28
#include "llvm/MC/MCTargetOptions.h"
29
#include "llvm/Support/Casting.h"
30
#include "llvm/Support/LEB128.h"
31
#include "llvm/Target/TargetLoweringObjectFile.h"
32
#include <algorithm>
33
#include <cassert>
34
#include <cstdint>
35
#include <vector>
36
37
using namespace llvm;
38
39
32.5k
EHStreamer::EHStreamer(AsmPrinter *A) : Asm(A), MMI(Asm->MMI) {}
40
41
32.4k
EHStreamer::~EHStreamer() = default;
42
43
/// How many leading type ids two landing pads have in common.
44
unsigned EHStreamer::sharedTypeIDs(const LandingPadInfo *L,
45
12.6k
                                   const LandingPadInfo *R) {
46
12.6k
  const std::vector<int> &LIds = L->TypeIds, &RIds = R->TypeIds;
47
12.6k
  unsigned LSize = LIds.size(), RSize = RIds.size();
48
12.6k
  unsigned MinSize = LSize < RSize ? 
LSize9
:
RSize12.6k
;
49
12.6k
  unsigned Count = 0;
50
12.6k
51
16.0k
  for (; Count != MinSize; 
++Count3.41k
)
52
3.43k
    if (LIds[Count] != RIds[Count])
53
19
      return Count;
54
12.6k
55
12.6k
  
return Count12.6k
;
56
12.6k
}
57
58
/// Compute the actions table and gather the first action index for each landing
59
/// pad site.
60
void EHStreamer::computeActionsTable(
61
    const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
62
    SmallVectorImpl<ActionEntry> &Actions,
63
3.45k
    SmallVectorImpl<unsigned> &FirstActions) {
64
3.45k
  // The action table follows the call-site table in the LSDA. The individual
65
3.45k
  // records are of two types:
66
3.45k
  //
67
3.45k
  //   * Catch clause
68
3.45k
  //   * Exception specification
69
3.45k
  //
70
3.45k
  // The two record kinds have the same format, with only small differences.
71
3.45k
  // They are distinguished by the "switch value" field: Catch clauses
72
3.45k
  // (TypeInfos) have strictly positive switch values, and exception
73
3.45k
  // specifications (FilterIds) have strictly negative switch values. Value 0
74
3.45k
  // indicates a catch-all clause.
75
3.45k
  //
76
3.45k
  // Negative type IDs index into FilterIds. Positive type IDs index into
77
3.45k
  // TypeInfos.  The value written for a positive type ID is just the type ID
78
3.45k
  // itself.  For a negative type ID, however, the value written is the
79
3.45k
  // (negative) byte offset of the corresponding FilterIds entry.  The byte
80
3.45k
  // offset is usually equal to the type ID (because the FilterIds entries are
81
3.45k
  // written using a variable width encoding, which outputs one byte per entry
82
3.45k
  // as long as the value written is not too large) but can differ.  This kind
83
3.45k
  // of complication does not occur for positive type IDs because type infos are
84
3.45k
  // output using a fixed width encoding.  FilterOffsets[i] holds the byte
85
3.45k
  // offset corresponding to FilterIds[i].
86
3.45k
87
3.45k
  const std::vector<unsigned> &FilterIds = Asm->MF->getFilterIds();
88
3.45k
  SmallVector<int, 16> FilterOffsets;
89
3.45k
  FilterOffsets.reserve(FilterIds.size());
90
3.45k
  int Offset = -1;
91
3.45k
92
3.45k
  for (std::vector<unsigned>::const_iterator
93
3.46k
         I = FilterIds.begin(), E = FilterIds.end(); I != E; 
++I15
) {
94
15
    FilterOffsets.push_back(Offset);
95
15
    Offset -= getULEB128Size(*I);
96
15
  }
97
3.45k
98
3.45k
  FirstActions.reserve(LandingPads.size());
99
3.45k
100
3.45k
  int FirstAction = 0;
101
3.45k
  unsigned SizeActions = 0; // Total size of all action entries for a function
102
3.45k
  const LandingPadInfo *PrevLPI = nullptr;
103
3.45k
104
3.45k
  for (SmallVectorImpl<const LandingPadInfo *>::const_iterator
105
19.5k
         I = LandingPads.begin(), E = LandingPads.end(); I != E; 
++I16.0k
) {
106
16.0k
    const LandingPadInfo *LPI = *I;
107
16.0k
    const std::vector<int> &TypeIds = LPI->TypeIds;
108
16.0k
    unsigned NumShared = PrevLPI ? 
sharedTypeIDs(LPI, PrevLPI)12.6k
:
03.44k
;
109
16.0k
    unsigned SizeSiteActions = 0; // Total size of all entries for a landingpad
110
16.0k
111
16.0k
    if (NumShared < TypeIds.size()) {
112
1.88k
      // Size of one action entry (typeid + next action)
113
1.88k
      unsigned SizeActionEntry = 0;
114
1.88k
      unsigned PrevAction = (unsigned)-1;
115
1.88k
116
1.88k
      if (NumShared) {
117
63
        unsigned SizePrevIds = PrevLPI->TypeIds.size();
118
63
        assert(Actions.size());
119
63
        PrevAction = Actions.size() - 1;
120
63
        SizeActionEntry = getSLEB128Size(Actions[PrevAction].NextAction) +
121
63
                          getSLEB128Size(Actions[PrevAction].ValueForTypeID);
122
63
123
70
        for (unsigned j = NumShared; j != SizePrevIds; 
++j7
) {
124
7
          assert(PrevAction != (unsigned)-1 && "PrevAction is invalid!");
125
7
          SizeActionEntry -= getSLEB128Size(Actions[PrevAction].ValueForTypeID);
126
7
          SizeActionEntry += -Actions[PrevAction].NextAction;
127
7
          PrevAction = Actions[PrevAction].Previous;
128
7
        }
129
63
      }
130
1.88k
131
1.88k
      // Compute the actions.
132
3.90k
      for (unsigned J = NumShared, M = TypeIds.size(); J != M; 
++J2.02k
) {
133
2.02k
        int TypeID = TypeIds[J];
134
2.02k
        assert(-1 - TypeID < (int)FilterOffsets.size() && "Unknown filter id!");
135
2.02k
        int ValueForTypeID =
136
2.02k
            isFilterEHSelector(TypeID) ? 
FilterOffsets[-1 - TypeID]10
:
TypeID2.01k
;
137
2.02k
        unsigned SizeTypeID = getSLEB128Size(ValueForTypeID);
138
2.02k
139
2.02k
        int NextAction = SizeActionEntry ? 
-(SizeActionEntry + SizeTypeID)200
:
01.82k
;
140
2.02k
        SizeActionEntry = SizeTypeID + getSLEB128Size(NextAction);
141
2.02k
        SizeSiteActions += SizeActionEntry;
142
2.02k
143
2.02k
        ActionEntry Action = { ValueForTypeID, NextAction, PrevAction };
144
2.02k
        Actions.push_back(Action);
145
2.02k
        PrevAction = Actions.size() - 1;
146
2.02k
      }
147
1.88k
148
1.88k
      // Record the first action of the landing pad site.
149
1.88k
      FirstAction = SizeActions + SizeSiteActions - SizeActionEntry + 1;
150
1.88k
    } // else identical - re-use previous FirstAction
151
16.0k
152
16.0k
    // Information used when creating the call-site table. The action record
153
16.0k
    // field of the call site record is the offset of the first associated
154
16.0k
    // action record, relative to the start of the actions table. This value is
155
16.0k
    // biased by 1 (1 indicating the start of the actions table), and 0
156
16.0k
    // indicates that there are no actions.
157
16.0k
    FirstActions.push_back(FirstAction);
158
16.0k
159
16.0k
    // Compute this sites contribution to size.
160
16.0k
    SizeActions += SizeSiteActions;
161
16.0k
162
16.0k
    PrevLPI = LPI;
163
16.0k
  }
164
3.45k
}
165
166
/// Return `true' if this is a call to a function marked `nounwind'. Return
167
/// `false' otherwise.
168
75.8k
bool EHStreamer::callToNoUnwindFunction(const MachineInstr *MI) {
169
75.8k
  assert(MI->isCall() && "This should be a call instruction!");
170
75.8k
171
75.8k
  bool MarkedNoUnwind = false;
172
75.8k
  bool SawFunc = false;
173
75.8k
174
561k
  for (unsigned I = 0, E = MI->getNumOperands(); I != E; 
++I485k
) {
175
485k
    const MachineOperand &MO = MI->getOperand(I);
176
485k
177
485k
    if (!MO.isGlobal()) 
continue423k
;
178
62.3k
179
62.3k
    const Function *F = dyn_cast<Function>(MO.getGlobal());
180
62.3k
    if (!F) 
continue0
;
181
62.3k
182
62.3k
    if (SawFunc) {
183
0
      // Be conservative. If we have more than one function operand for this
184
0
      // call, then we can't make the assumption that it's the callee and
185
0
      // not a parameter to the call.
186
0
      //
187
0
      // FIXME: Determine if there's a way to say that `F' is the callee or
188
0
      // parameter.
189
0
      MarkedNoUnwind = false;
190
0
      break;
191
0
    }
192
62.3k
193
62.3k
    MarkedNoUnwind = F->doesNotThrow();
194
62.3k
    SawFunc = true;
195
62.3k
  }
196
75.8k
197
75.8k
  return MarkedNoUnwind;
198
75.8k
}
199
200
void EHStreamer::computePadMap(
201
    const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
202
3.43k
    RangeMapType &PadMap) {
203
3.43k
  // Invokes and nounwind calls have entries in PadMap (due to being bracketed
204
3.43k
  // by try-range labels when lowered).  Ordinary calls do not, so appropriate
205
3.43k
  // try-ranges for them need be deduced so we can put them in the LSDA.
206
19.4k
  for (unsigned i = 0, N = LandingPads.size(); i != N; 
++i16.0k
) {
207
16.0k
    const LandingPadInfo *LandingPad = LandingPads[i];
208
43.1k
    for (unsigned j = 0, E = LandingPad->BeginLabels.size(); j != E; 
++j27.1k
) {
209
27.1k
      MCSymbol *BeginLabel = LandingPad->BeginLabels[j];
210
27.1k
      assert(!PadMap.count(BeginLabel) && "Duplicate landing pad labels!");
211
27.1k
      PadRange P = { i, j };
212
27.1k
      PadMap[BeginLabel] = P;
213
27.1k
    }
214
16.0k
  }
215
3.43k
}
216
217
/// Compute the call-site table.  The entry for an invoke has a try-range
218
/// containing the call, a non-zero landing pad, and an appropriate action.  The
219
/// entry for an ordinary call has a try-range containing the call and zero for
220
/// the landing pad and the action.  Calls marked 'nounwind' have no entry and
221
/// must not be contained in the try-range of any entry - they form gaps in the
222
/// table.  Entries must be ordered by try-range address.
223
void EHStreamer::
224
computeCallSiteTable(SmallVectorImpl<CallSiteEntry> &CallSites,
225
                     const SmallVectorImpl<const LandingPadInfo *> &LandingPads,
226
3.43k
                     const SmallVectorImpl<unsigned> &FirstActions) {
227
3.43k
  RangeMapType PadMap;
228
3.43k
  computePadMap(LandingPads, PadMap);
229
3.43k
230
3.43k
  // The end label of the previous invoke or nounwind try-range.
231
3.43k
  MCSymbol *LastLabel = nullptr;
232
3.43k
233
3.43k
  // Whether there is a potentially throwing instruction (currently this means
234
3.43k
  // an ordinary call) between the end of the previous try-range and now.
235
3.43k
  bool SawPotentiallyThrowing = false;
236
3.43k
237
3.43k
  // Whether the last CallSite entry was for an invoke.
238
3.43k
  bool PreviousIsInvoke = false;
239
3.43k
240
3.43k
  bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
241
3.43k
242
3.43k
  // Visit all instructions in order of address.
243
144k
  for (const auto &MBB : *Asm->MF) {
244
950k
    for (const auto &MI : MBB) {
245
950k
      if (!MI.isEHLabel()) {
246
880k
        if (MI.isCall())
247
75.8k
          SawPotentiallyThrowing |= !callToNoUnwindFunction(&MI);
248
880k
        continue;
249
880k
      }
250
70.2k
251
70.2k
      // End of the previous try-range?
252
70.2k
      MCSymbol *BeginLabel = MI.getOperand(0).getMCSymbol();
253
70.2k
      if (BeginLabel == LastLabel)
254
27.1k
        SawPotentiallyThrowing = false;
255
70.2k
256
70.2k
      // Beginning of a new try-range?
257
70.2k
      RangeMapType::const_iterator L = PadMap.find(BeginLabel);
258
70.2k
      if (L == PadMap.end())
259
43.1k
        // Nope, it was just some random label.
260
43.1k
        continue;
261
27.1k
262
27.1k
      const PadRange &P = L->second;
263
27.1k
      const LandingPadInfo *LandingPad = LandingPads[P.PadIndex];
264
27.1k
      assert(BeginLabel == LandingPad->BeginLabels[P.RangeIndex] &&
265
27.1k
             "Inconsistent landing pad map!");
266
27.1k
267
27.1k
      // For Dwarf exception handling (SjLj handling doesn't use this). If some
268
27.1k
      // instruction between the previous try-range and this one may throw,
269
27.1k
      // create a call-site entry with no landing pad for the region between the
270
27.1k
      // try-ranges.
271
27.1k
      if (SawPotentiallyThrowing && 
Asm->MAI->usesCFIForEH()6.31k
) {
272
6.18k
        CallSiteEntry Site = { LastLabel, BeginLabel, nullptr, 0 };
273
6.18k
        CallSites.push_back(Site);
274
6.18k
        PreviousIsInvoke = false;
275
6.18k
      }
276
27.1k
277
27.1k
      LastLabel = LandingPad->EndLabels[P.RangeIndex];
278
27.1k
      assert(BeginLabel && LastLabel && "Invalid landing pad!");
279
27.1k
280
27.1k
      if (!LandingPad->LandingPadLabel) {
281
0
        // Create a gap.
282
0
        PreviousIsInvoke = false;
283
27.1k
      } else {
284
27.1k
        // This try-range is for an invoke.
285
27.1k
        CallSiteEntry Site = {
286
27.1k
          BeginLabel,
287
27.1k
          LastLabel,
288
27.1k
          LandingPad,
289
27.1k
          FirstActions[P.PadIndex]
290
27.1k
        };
291
27.1k
292
27.1k
        // Try to merge with the previous call-site. SJLJ doesn't do this
293
27.1k
        if (PreviousIsInvoke && 
!IsSJLJ19.2k
) {
294
19.1k
          CallSiteEntry &Prev = CallSites.back();
295
19.1k
          if (Site.LPad == Prev.LPad && 
Site.Action == Prev.Action8.60k
) {
296
8.60k
            // Extend the range of the previous entry.
297
8.60k
            Prev.EndLabel = Site.EndLabel;
298
8.60k
            continue;
299
8.60k
          }
300
18.5k
        }
301
18.5k
302
18.5k
        // Otherwise, create a new call-site.
303
18.5k
        if (!IsSJLJ)
304
18.3k
          CallSites.push_back(Site);
305
175
        else {
306
175
          // SjLj EH must maintain the call sites in the order assigned
307
175
          // to them by the SjLjPrepare pass.
308
175
          unsigned SiteNo = Asm->MF->getCallSiteBeginLabel(BeginLabel);
309
175
          if (CallSites.size() < SiteNo)
310
140
            CallSites.resize(SiteNo);
311
175
          CallSites[SiteNo - 1] = Site;
312
175
        }
313
18.5k
        PreviousIsInvoke = true;
314
18.5k
      }
315
27.1k
    }
316
144k
  }
317
3.43k
318
3.43k
  // If some instruction between the previous try-range and the end of the
319
3.43k
  // function may throw, create a call-site entry with no landing pad for the
320
3.43k
  // region following the try-range.
321
3.43k
  if (SawPotentiallyThrowing && 
!IsSJLJ2.66k
) {
322
2.63k
    CallSiteEntry Site = { LastLabel, nullptr, nullptr, 0 };
323
2.63k
    CallSites.push_back(Site);
324
2.63k
  }
325
3.43k
}
326
327
/// Emit landing pads and actions.
328
///
329
/// The general organization of the table is complex, but the basic concepts are
330
/// easy.  First there is a header which describes the location and organization
331
/// of the three components that follow.
332
///
333
///  1. The landing pad site information describes the range of code covered by
334
///     the try.  In our case it's an accumulation of the ranges covered by the
335
///     invokes in the try.  There is also a reference to the landing pad that
336
///     handles the exception once processed.  Finally an index into the actions
337
///     table.
338
///  2. The action table, in our case, is composed of pairs of type IDs and next
339
///     action offset.  Starting with the action index from the landing pad
340
///     site, each type ID is checked for a match to the current exception.  If
341
///     it matches then the exception and type id are passed on to the landing
342
///     pad.  Otherwise the next action is looked up.  This chain is terminated
343
///     with a next action of zero.  If no type id is found then the frame is
344
///     unwound and handling continues.
345
///  3. Type ID table contains references to all the C++ typeinfo for all
346
///     catches in the function.  This tables is reverse indexed base 1.
347
///
348
/// Returns the starting symbol of an exception table.
349
3.45k
MCSymbol *EHStreamer::emitExceptionTable() {
350
3.45k
  const MachineFunction *MF = Asm->MF;
351
3.45k
  const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos();
352
3.45k
  const std::vector<unsigned> &FilterIds = MF->getFilterIds();
353
3.45k
  const std::vector<LandingPadInfo> &PadInfos = MF->getLandingPads();
354
3.45k
355
3.45k
  // Sort the landing pads in order of their type ids.  This is used to fold
356
3.45k
  // duplicate actions.
357
3.45k
  SmallVector<const LandingPadInfo *, 64> LandingPads;
358
3.45k
  LandingPads.reserve(PadInfos.size());
359
3.45k
360
19.5k
  for (unsigned i = 0, N = PadInfos.size(); i != N; 
++i16.0k
)
361
16.0k
    LandingPads.push_back(&PadInfos[i]);
362
3.45k
363
3.45k
  // Order landing pads lexicographically by type id.
364
22.1k
  llvm::sort(LandingPads, [](const LandingPadInfo *L, const LandingPadInfo *R) {
365
22.1k
    return L->TypeIds < R->TypeIds;
366
22.1k
  });
367
3.45k
368
3.45k
  // Compute the actions table and gather the first action index for each
369
3.45k
  // landing pad site.
370
3.45k
  SmallVector<ActionEntry, 32> Actions;
371
3.45k
  SmallVector<unsigned, 64> FirstActions;
372
3.45k
  computeActionsTable(LandingPads, Actions, FirstActions);
373
3.45k
374
3.45k
  // Compute the call-site table.
375
3.45k
  SmallVector<CallSiteEntry, 64> CallSites;
376
3.45k
  computeCallSiteTable(CallSites, LandingPads, FirstActions);
377
3.45k
378
3.45k
  bool IsSJLJ = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::SjLj;
379
3.45k
  bool IsWasm = Asm->MAI->getExceptionHandlingType() == ExceptionHandling::Wasm;
380
3.45k
  unsigned CallSiteEncoding =
381
3.45k
      IsSJLJ ? 
static_cast<unsigned>(dwarf::DW_EH_PE_udata4)36
:
382
3.45k
               
Asm->getObjFileLowering().getCallSiteEncoding()3.41k
;
383
3.45k
  bool HaveTTData = !TypeInfos.empty() || 
!FilterIds.empty()1.64k
;
384
3.45k
385
3.45k
  // Type infos.
386
3.45k
  MCSection *LSDASection = Asm->getObjFileLowering().getLSDASection();
387
3.45k
  unsigned TTypeEncoding;
388
3.45k
389
3.45k
  if (!HaveTTData) {
390
1.64k
    // If there is no TypeInfo, then we just explicitly say that we're omitting
391
1.64k
    // that bit.
392
1.64k
    TTypeEncoding = dwarf::DW_EH_PE_omit;
393
1.80k
  } else {
394
1.80k
    // Okay, we have actual filters or typeinfos to emit.  As such, we need to
395
1.80k
    // pick a type encoding for them.  We're about to emit a list of pointers to
396
1.80k
    // typeinfo objects at the end of the LSDA.  However, unless we're in static
397
1.80k
    // mode, this reference will require a relocation by the dynamic linker.
398
1.80k
    //
399
1.80k
    // Because of this, we have a couple of options:
400
1.80k
    //
401
1.80k
    //   1) If we are in -static mode, we can always use an absolute reference
402
1.80k
    //      from the LSDA, because the static linker will resolve it.
403
1.80k
    //
404
1.80k
    //   2) Otherwise, if the LSDA section is writable, we can output the direct
405
1.80k
    //      reference to the typeinfo and allow the dynamic linker to relocate
406
1.80k
    //      it.  Since it is in a writable section, the dynamic linker won't
407
1.80k
    //      have a problem.
408
1.80k
    //
409
1.80k
    //   3) Finally, if we're in PIC mode and the LDSA section isn't writable,
410
1.80k
    //      we need to use some form of indirection.  For example, on Darwin,
411
1.80k
    //      we can output a statically-relocatable reference to a dyld stub. The
412
1.80k
    //      offset to the stub is constant, but the contents are in a section
413
1.80k
    //      that is updated by the dynamic linker.  This is easy enough, but we
414
1.80k
    //      need to tell the personality function of the unwinder to indirect
415
1.80k
    //      through the dyld stub.
416
1.80k
    //
417
1.80k
    // FIXME: When (3) is actually implemented, we'll have to emit the stubs
418
1.80k
    // somewhere.  This predicate should be moved to a shared location that is
419
1.80k
    // in target-independent code.
420
1.80k
    //
421
1.80k
    TTypeEncoding = Asm->getObjFileLowering().getTTypeEncoding();
422
1.80k
  }
423
3.45k
424
3.45k
  // Begin the exception table.
425
3.45k
  // Sometimes we want not to emit the data into separate section (e.g. ARM
426
3.45k
  // EHABI). In this case LSDASection will be NULL.
427
3.45k
  if (LSDASection)
428
3.38k
    Asm->OutStreamer->SwitchSection(LSDASection);
429
3.45k
  Asm->EmitAlignment(2);
430
3.45k
431
3.45k
  // Emit the LSDA.
432
3.45k
  MCSymbol *GCCETSym =
433
3.45k
    Asm->OutContext.getOrCreateSymbol(Twine("GCC_except_table")+
434
3.45k
                                      Twine(Asm->getFunctionNumber()));
435
3.45k
  Asm->OutStreamer->EmitLabel(GCCETSym);
436
3.45k
  Asm->OutStreamer->EmitLabel(Asm->getCurExceptionSym());
437
3.45k
438
3.45k
  // Emit the LSDA header.
439
3.45k
  Asm->EmitEncodingByte(dwarf::DW_EH_PE_omit, "@LPStart");
440
3.45k
  Asm->EmitEncodingByte(TTypeEncoding, "@TType");
441
3.45k
442
3.45k
  MCSymbol *TTBaseLabel = nullptr;
443
3.45k
  if (HaveTTData) {
444
1.80k
    // N.B.: There is a dependency loop between the size of the TTBase uleb128
445
1.80k
    // here and the amount of padding before the aligned type table. The
446
1.80k
    // assembler must sometimes pad this uleb128 or insert extra padding before
447
1.80k
    // the type table. See PR35809 or GNU as bug 4029.
448
1.80k
    MCSymbol *TTBaseRefLabel = Asm->createTempSymbol("ttbaseref");
449
1.80k
    TTBaseLabel = Asm->createTempSymbol("ttbase");
450
1.80k
    Asm->EmitLabelDifferenceAsULEB128(TTBaseLabel, TTBaseRefLabel);
451
1.80k
    Asm->OutStreamer->EmitLabel(TTBaseRefLabel);
452
1.80k
  }
453
3.45k
454
3.45k
  bool VerboseAsm = Asm->OutStreamer->isVerboseAsm();
455
3.45k
456
3.45k
  // Emit the landing pad call site table.
457
3.45k
  MCSymbol *CstBeginLabel = Asm->createTempSymbol("cst_begin");
458
3.45k
  MCSymbol *CstEndLabel = Asm->createTempSymbol("cst_end");
459
3.45k
  Asm->EmitEncodingByte(CallSiteEncoding, "Call site");
460
3.45k
  Asm->EmitLabelDifferenceAsULEB128(CstEndLabel, CstBeginLabel);
461
3.45k
  Asm->OutStreamer->EmitLabel(CstBeginLabel);
462
3.45k
463
3.45k
  // SjLj / Wasm Exception handling
464
3.45k
  if (IsSJLJ || 
IsWasm3.41k
) {
465
47
    unsigned idx = 0;
466
47
    for (SmallVectorImpl<CallSiteEntry>::const_iterator
467
238
         I = CallSites.begin(), E = CallSites.end(); I != E; 
++I, ++idx191
) {
468
191
      const CallSiteEntry &S = *I;
469
191
470
191
      // Index of the call site entry.
471
191
      if (VerboseAsm) {
472
61
        Asm->OutStreamer->AddComment(">> Call Site " + Twine(idx) + " <<");
473
61
        Asm->OutStreamer->AddComment("  On exception at call site "+Twine(idx));
474
61
      }
475
191
      Asm->EmitULEB128(idx);
476
191
477
191
      // Offset of the first associated action record, relative to the start of
478
191
      // the action table. This value is biased by 1 (1 indicates the start of
479
191
      // the action table), and 0 indicates that there are no actions.
480
191
      if (VerboseAsm) {
481
61
        if (S.Action == 0)
482
20
          Asm->OutStreamer->AddComment("  Action: cleanup");
483
41
        else
484
41
          Asm->OutStreamer->AddComment("  Action: " +
485
41
                                       Twine((S.Action - 1) / 2 + 1));
486
61
      }
487
191
      Asm->EmitULEB128(S.Action);
488
191
    }
489
3.40k
  } else {
490
3.40k
    // Itanium LSDA exception handling
491
3.40k
492
3.40k
    // The call-site table is a list of all call sites that may throw an
493
3.40k
    // exception (including C++ 'throw' statements) in the procedure
494
3.40k
    // fragment. It immediately follows the LSDA header. Each entry indicates,
495
3.40k
    // for a given call, the first corresponding action record and corresponding
496
3.40k
    // landing pad.
497
3.40k
    //
498
3.40k
    // The table begins with the number of bytes, stored as an LEB128
499
3.40k
    // compressed, unsigned integer. The records immediately follow the record
500
3.40k
    // count. They are sorted in increasing call-site address. Each record
501
3.40k
    // indicates:
502
3.40k
    //
503
3.40k
    //   * The position of the call-site.
504
3.40k
    //   * The position of the landing pad.
505
3.40k
    //   * The first action record for that call site.
506
3.40k
    //
507
3.40k
    // A missing entry in the call-site table indicates that a call is not
508
3.40k
    // supposed to throw.
509
3.40k
510
3.40k
    unsigned Entry = 0;
511
3.40k
    for (SmallVectorImpl<CallSiteEntry>::const_iterator
512
30.5k
         I = CallSites.begin(), E = CallSites.end(); I != E; 
++I27.1k
) {
513
27.1k
      const CallSiteEntry &S = *I;
514
27.1k
515
27.1k
      MCSymbol *EHFuncBeginSym = Asm->getFunctionBegin();
516
27.1k
517
27.1k
      MCSymbol *BeginLabel = S.BeginLabel;
518
27.1k
      if (!BeginLabel)
519
1.79k
        BeginLabel = EHFuncBeginSym;
520
27.1k
      MCSymbol *EndLabel = S.EndLabel;
521
27.1k
      if (!EndLabel)
522
2.63k
        EndLabel = Asm->getFunctionEnd();
523
27.1k
524
27.1k
      // Offset of the call site relative to the start of the procedure.
525
27.1k
      if (VerboseAsm)
526
651
        Asm->OutStreamer->AddComment(">> Call Site " + Twine(++Entry) + " <<");
527
27.1k
      Asm->EmitCallSiteOffset(BeginLabel, EHFuncBeginSym, CallSiteEncoding);
528
27.1k
      if (VerboseAsm)
529
651
        Asm->OutStreamer->AddComment(Twine("  Call between ") +
530
651
                                     BeginLabel->getName() + " and " +
531
651
                                     EndLabel->getName());
532
27.1k
      Asm->EmitCallSiteOffset(EndLabel, BeginLabel, CallSiteEncoding);
533
27.1k
534
27.1k
      // Offset of the landing pad relative to the start of the procedure.
535
27.1k
      if (!S.LPad) {
536
8.81k
        if (VerboseAsm)
537
250
          Asm->OutStreamer->AddComment("    has no landing pad");
538
8.81k
        Asm->EmitCallSiteValue(0, CallSiteEncoding);
539
18.3k
      } else {
540
18.3k
        if (VerboseAsm)
541
401
          Asm->OutStreamer->AddComment(Twine("    jumps to ") +
542
401
                                       S.LPad->LandingPadLabel->getName());
543
18.3k
        Asm->EmitCallSiteOffset(S.LPad->LandingPadLabel, EHFuncBeginSym,
544
18.3k
                                CallSiteEncoding);
545
18.3k
      }
546
27.1k
547
27.1k
      // Offset of the first associated action record, relative to the start of
548
27.1k
      // the action table. This value is biased by 1 (1 indicates the start of
549
27.1k
      // the action table), and 0 indicates that there are no actions.
550
27.1k
      if (VerboseAsm) {
551
651
        if (S.Action == 0)
552
417
          Asm->OutStreamer->AddComment("  On action: cleanup");
553
234
        else
554
234
          Asm->OutStreamer->AddComment("  On action: " +
555
234
                                       Twine((S.Action - 1) / 2 + 1));
556
651
      }
557
27.1k
      Asm->EmitULEB128(S.Action);
558
27.1k
    }
559
3.40k
  }
560
3.45k
  Asm->OutStreamer->EmitLabel(CstEndLabel);
561
3.45k
562
3.45k
  // Emit the Action Table.
563
3.45k
  int Entry = 0;
564
3.45k
  for (SmallVectorImpl<ActionEntry>::const_iterator
565
5.47k
         I = Actions.begin(), E = Actions.end(); I != E; 
++I2.02k
) {
566
2.02k
    const ActionEntry &Action = *I;
567
2.02k
568
2.02k
    if (VerboseAsm) {
569
257
      // Emit comments that decode the action table.
570
257
      Asm->OutStreamer->AddComment(">> Action Record " + Twine(++Entry) + " <<");
571
257
    }
572
2.02k
573
2.02k
    // Type Filter
574
2.02k
    //
575
2.02k
    //   Used by the runtime to match the type of the thrown exception to the
576
2.02k
    //   type of the catch clauses or the types in the exception specification.
577
2.02k
    if (VerboseAsm) {
578
257
      if (Action.ValueForTypeID > 0)
579
241
        Asm->OutStreamer->AddComment("  Catch TypeInfo " +
580
241
                                     Twine(Action.ValueForTypeID));
581
16
      else if (Action.ValueForTypeID < 0)
582
6
        Asm->OutStreamer->AddComment("  Filter TypeInfo " +
583
6
                                     Twine(Action.ValueForTypeID));
584
10
      else
585
10
        Asm->OutStreamer->AddComment("  Cleanup");
586
257
    }
587
2.02k
    Asm->EmitSLEB128(Action.ValueForTypeID);
588
2.02k
589
2.02k
    // Action Record
590
2.02k
    //
591
2.02k
    //   Self-relative signed displacement in bytes of the next action record,
592
2.02k
    //   or 0 if there is no next action record.
593
2.02k
    if (VerboseAsm) {
594
257
      if (Action.NextAction == 0) {
595
220
        Asm->OutStreamer->AddComment("  No further actions");
596
220
      } else {
597
37
        unsigned NextAction = Entry + (Action.NextAction + 1) / 2;
598
37
        Asm->OutStreamer->AddComment("  Continue to action "+Twine(NextAction));
599
37
      }
600
257
    }
601
2.02k
    Asm->EmitSLEB128(Action.NextAction);
602
2.02k
  }
603
3.45k
604
3.45k
  if (HaveTTData) {
605
1.80k
    Asm->EmitAlignment(2);
606
1.80k
    emitTypeInfos(TTypeEncoding, TTBaseLabel);
607
1.80k
  }
608
3.45k
609
3.45k
  Asm->EmitAlignment(2);
610
3.45k
  return GCCETSym;
611
3.45k
}
612
613
1.77k
void EHStreamer::emitTypeInfos(unsigned TTypeEncoding, MCSymbol *TTBaseLabel) {
614
1.77k
  const MachineFunction *MF = Asm->MF;
615
1.77k
  const std::vector<const GlobalValue *> &TypeInfos = MF->getTypeInfos();
616
1.77k
  const std::vector<unsigned> &FilterIds = MF->getFilterIds();
617
1.77k
618
1.77k
  bool VerboseAsm = Asm->OutStreamer->isVerboseAsm();
619
1.77k
620
1.77k
  int Entry = 0;
621
1.77k
  // Emit the Catch TypeInfos.
622
1.77k
  if (VerboseAsm && 
!TypeInfos.empty()191
) {
623
186
    Asm->OutStreamer->AddComment(">> Catch TypeInfos <<");
624
186
    Asm->OutStreamer->AddBlankLine();
625
186
    Entry = TypeInfos.size();
626
186
  }
627
1.77k
628
1.77k
  for (const GlobalValue *GV : make_range(TypeInfos.rbegin(),
629
1.92k
                                          TypeInfos.rend())) {
630
1.92k
    if (VerboseAsm)
631
213
      Asm->OutStreamer->AddComment("TypeInfo " + Twine(Entry--));
632
1.92k
    Asm->EmitTTypeReference(GV, TTypeEncoding);
633
1.92k
  }
634
1.77k
635
1.77k
  Asm->OutStreamer->EmitLabel(TTBaseLabel);
636
1.77k
637
1.77k
  // Emit the Exception Specifications.
638
1.77k
  if (VerboseAsm && 
!FilterIds.empty()191
) {
639
6
    Asm->OutStreamer->AddComment(">> Filter TypeInfos <<");
640
6
    Asm->OutStreamer->AddBlankLine();
641
6
    Entry = 0;
642
6
  }
643
1.77k
  for (std::vector<unsigned>::const_iterator
644
1.79k
         I = FilterIds.begin(), E = FilterIds.end(); I < E; 
++I13
) {
645
13
    unsigned TypeID = *I;
646
13
    if (VerboseAsm) {
647
7
      --Entry;
648
7
      if (isFilterEHSelector(TypeID))
649
0
        Asm->OutStreamer->AddComment("FilterInfo " + Twine(Entry));
650
7
    }
651
13
652
13
    Asm->EmitULEB128(TypeID);
653
13
  }
654
1.77k
}