2
0

EHStreamer.cpp 27 KB

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