EHStreamer.cpp 25 KB

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