MCWin64EH.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. //===- lib/MC/MCWin64EH.cpp - MCWin64EH implementation --------------------===//
  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. #include "llvm/MC/MCWin64EH.h"
  9. #include "llvm/ADT/Twine.h"
  10. #include "llvm/MC/MCContext.h"
  11. #include "llvm/MC/MCExpr.h"
  12. #include "llvm/MC/MCObjectFileInfo.h"
  13. #include "llvm/MC/MCObjectStreamer.h"
  14. #include "llvm/MC/MCSectionCOFF.h"
  15. #include "llvm/MC/MCStreamer.h"
  16. #include "llvm/MC/MCSymbol.h"
  17. #include "llvm/Support/Win64EH.h"
  18. using namespace llvm;
  19. // NOTE: All relocations generated here are 4-byte image-relative.
  20. static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
  21. uint8_t Count = 0;
  22. for (const auto &I : Insns) {
  23. switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
  24. default:
  25. llvm_unreachable("Unsupported unwind code");
  26. case Win64EH::UOP_PushNonVol:
  27. case Win64EH::UOP_AllocSmall:
  28. case Win64EH::UOP_SetFPReg:
  29. case Win64EH::UOP_PushMachFrame:
  30. Count += 1;
  31. break;
  32. case Win64EH::UOP_SaveNonVol:
  33. case Win64EH::UOP_SaveXMM128:
  34. Count += 2;
  35. break;
  36. case Win64EH::UOP_SaveNonVolBig:
  37. case Win64EH::UOP_SaveXMM128Big:
  38. Count += 3;
  39. break;
  40. case Win64EH::UOP_AllocLarge:
  41. Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
  42. break;
  43. }
  44. }
  45. return Count;
  46. }
  47. static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
  48. const MCSymbol *RHS) {
  49. MCContext &Context = Streamer.getContext();
  50. const MCExpr *Diff =
  51. MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
  52. MCSymbolRefExpr::create(RHS, Context), Context);
  53. Streamer.emitValue(Diff, 1);
  54. }
  55. static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
  56. WinEH::Instruction &inst) {
  57. uint8_t b2;
  58. uint16_t w;
  59. b2 = (inst.Operation & 0x0F);
  60. switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
  61. default:
  62. llvm_unreachable("Unsupported unwind code");
  63. case Win64EH::UOP_PushNonVol:
  64. EmitAbsDifference(streamer, inst.Label, begin);
  65. b2 |= (inst.Register & 0x0F) << 4;
  66. streamer.emitInt8(b2);
  67. break;
  68. case Win64EH::UOP_AllocLarge:
  69. EmitAbsDifference(streamer, inst.Label, begin);
  70. if (inst.Offset > 512 * 1024 - 8) {
  71. b2 |= 0x10;
  72. streamer.emitInt8(b2);
  73. w = inst.Offset & 0xFFF8;
  74. streamer.emitInt16(w);
  75. w = inst.Offset >> 16;
  76. } else {
  77. streamer.emitInt8(b2);
  78. w = inst.Offset >> 3;
  79. }
  80. streamer.emitInt16(w);
  81. break;
  82. case Win64EH::UOP_AllocSmall:
  83. b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
  84. EmitAbsDifference(streamer, inst.Label, begin);
  85. streamer.emitInt8(b2);
  86. break;
  87. case Win64EH::UOP_SetFPReg:
  88. EmitAbsDifference(streamer, inst.Label, begin);
  89. streamer.emitInt8(b2);
  90. break;
  91. case Win64EH::UOP_SaveNonVol:
  92. case Win64EH::UOP_SaveXMM128:
  93. b2 |= (inst.Register & 0x0F) << 4;
  94. EmitAbsDifference(streamer, inst.Label, begin);
  95. streamer.emitInt8(b2);
  96. w = inst.Offset >> 3;
  97. if (inst.Operation == Win64EH::UOP_SaveXMM128)
  98. w >>= 1;
  99. streamer.emitInt16(w);
  100. break;
  101. case Win64EH::UOP_SaveNonVolBig:
  102. case Win64EH::UOP_SaveXMM128Big:
  103. b2 |= (inst.Register & 0x0F) << 4;
  104. EmitAbsDifference(streamer, inst.Label, begin);
  105. streamer.emitInt8(b2);
  106. if (inst.Operation == Win64EH::UOP_SaveXMM128Big)
  107. w = inst.Offset & 0xFFF0;
  108. else
  109. w = inst.Offset & 0xFFF8;
  110. streamer.emitInt16(w);
  111. w = inst.Offset >> 16;
  112. streamer.emitInt16(w);
  113. break;
  114. case Win64EH::UOP_PushMachFrame:
  115. if (inst.Offset == 1)
  116. b2 |= 0x10;
  117. EmitAbsDifference(streamer, inst.Label, begin);
  118. streamer.emitInt8(b2);
  119. break;
  120. }
  121. }
  122. static void EmitSymbolRefWithOfs(MCStreamer &streamer,
  123. const MCSymbol *Base,
  124. const MCSymbol *Other) {
  125. MCContext &Context = streamer.getContext();
  126. const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::create(Base, Context);
  127. const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::create(Other, Context);
  128. const MCExpr *Ofs = MCBinaryExpr::createSub(OtherRef, BaseRef, Context);
  129. const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base,
  130. MCSymbolRefExpr::VK_COFF_IMGREL32,
  131. Context);
  132. streamer.emitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
  133. }
  134. static void EmitRuntimeFunction(MCStreamer &streamer,
  135. const WinEH::FrameInfo *info) {
  136. MCContext &context = streamer.getContext();
  137. streamer.emitValueToAlignment(4);
  138. EmitSymbolRefWithOfs(streamer, info->Begin, info->Begin);
  139. EmitSymbolRefWithOfs(streamer, info->Begin, info->End);
  140. streamer.emitValue(MCSymbolRefExpr::create(info->Symbol,
  141. MCSymbolRefExpr::VK_COFF_IMGREL32,
  142. context), 4);
  143. }
  144. static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
  145. // If this UNWIND_INFO already has a symbol, it's already been emitted.
  146. if (info->Symbol)
  147. return;
  148. MCContext &context = streamer.getContext();
  149. MCSymbol *Label = context.createTempSymbol();
  150. streamer.emitValueToAlignment(4);
  151. streamer.emitLabel(Label);
  152. info->Symbol = Label;
  153. // Upper 3 bits are the version number (currently 1).
  154. uint8_t flags = 0x01;
  155. if (info->ChainedParent)
  156. flags |= Win64EH::UNW_ChainInfo << 3;
  157. else {
  158. if (info->HandlesUnwind)
  159. flags |= Win64EH::UNW_TerminateHandler << 3;
  160. if (info->HandlesExceptions)
  161. flags |= Win64EH::UNW_ExceptionHandler << 3;
  162. }
  163. streamer.emitInt8(flags);
  164. if (info->PrologEnd)
  165. EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
  166. else
  167. streamer.emitInt8(0);
  168. uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
  169. streamer.emitInt8(numCodes);
  170. uint8_t frame = 0;
  171. if (info->LastFrameInst >= 0) {
  172. WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
  173. assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
  174. frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
  175. }
  176. streamer.emitInt8(frame);
  177. // Emit unwind instructions (in reverse order).
  178. uint8_t numInst = info->Instructions.size();
  179. for (uint8_t c = 0; c < numInst; ++c) {
  180. WinEH::Instruction inst = info->Instructions.back();
  181. info->Instructions.pop_back();
  182. EmitUnwindCode(streamer, info->Begin, inst);
  183. }
  184. // For alignment purposes, the instruction array will always have an even
  185. // number of entries, with the final entry potentially unused (in which case
  186. // the array will be one longer than indicated by the count of unwind codes
  187. // field).
  188. if (numCodes & 1) {
  189. streamer.emitInt16(0);
  190. }
  191. if (flags & (Win64EH::UNW_ChainInfo << 3))
  192. EmitRuntimeFunction(streamer, info->ChainedParent);
  193. else if (flags &
  194. ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
  195. streamer.emitValue(MCSymbolRefExpr::create(info->ExceptionHandler,
  196. MCSymbolRefExpr::VK_COFF_IMGREL32,
  197. context), 4);
  198. else if (numCodes == 0) {
  199. // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
  200. // a chained unwind info, if there is no handler, and if there are fewer
  201. // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
  202. streamer.emitInt32(0);
  203. }
  204. }
  205. void llvm::Win64EH::UnwindEmitter::Emit(MCStreamer &Streamer) const {
  206. // Emit the unwind info structs first.
  207. for (const auto &CFI : Streamer.getWinFrameInfos()) {
  208. MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
  209. Streamer.SwitchSection(XData);
  210. ::EmitUnwindInfo(Streamer, CFI.get());
  211. }
  212. // Now emit RUNTIME_FUNCTION entries.
  213. for (const auto &CFI : Streamer.getWinFrameInfos()) {
  214. MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
  215. Streamer.SwitchSection(PData);
  216. EmitRuntimeFunction(Streamer, CFI.get());
  217. }
  218. }
  219. void llvm::Win64EH::UnwindEmitter::EmitUnwindInfo(MCStreamer &Streamer,
  220. WinEH::FrameInfo *info,
  221. bool HandlerData) const {
  222. // Switch sections (the static function above is meant to be called from
  223. // here and from Emit().
  224. MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
  225. Streamer.SwitchSection(XData);
  226. ::EmitUnwindInfo(Streamer, info);
  227. }
  228. static int64_t GetAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
  229. const MCSymbol *RHS) {
  230. MCContext &Context = Streamer.getContext();
  231. const MCExpr *Diff =
  232. MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
  233. MCSymbolRefExpr::create(RHS, Context), Context);
  234. MCObjectStreamer *OS = (MCObjectStreamer *)(&Streamer);
  235. // It should normally be possible to calculate the length of a function
  236. // at this point, but it might not be possible in the presence of certain
  237. // unusual constructs, like an inline asm with an alignment directive.
  238. int64_t value;
  239. if (!Diff->evaluateAsAbsolute(value, OS->getAssembler()))
  240. report_fatal_error("Failed to evaluate function length in SEH unwind info");
  241. return value;
  242. }
  243. static uint32_t ARM64CountOfUnwindCodes(ArrayRef<WinEH::Instruction> Insns) {
  244. uint32_t Count = 0;
  245. for (const auto &I : Insns) {
  246. switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
  247. default:
  248. llvm_unreachable("Unsupported ARM64 unwind code");
  249. case Win64EH::UOP_AllocSmall:
  250. Count += 1;
  251. break;
  252. case Win64EH::UOP_AllocMedium:
  253. Count += 2;
  254. break;
  255. case Win64EH::UOP_AllocLarge:
  256. Count += 4;
  257. break;
  258. case Win64EH::UOP_SaveR19R20X:
  259. Count += 1;
  260. break;
  261. case Win64EH::UOP_SaveFPLRX:
  262. Count += 1;
  263. break;
  264. case Win64EH::UOP_SaveFPLR:
  265. Count += 1;
  266. break;
  267. case Win64EH::UOP_SaveReg:
  268. Count += 2;
  269. break;
  270. case Win64EH::UOP_SaveRegP:
  271. Count += 2;
  272. break;
  273. case Win64EH::UOP_SaveRegPX:
  274. Count += 2;
  275. break;
  276. case Win64EH::UOP_SaveRegX:
  277. Count += 2;
  278. break;
  279. case Win64EH::UOP_SaveLRPair:
  280. Count += 2;
  281. break;
  282. case Win64EH::UOP_SaveFReg:
  283. Count += 2;
  284. break;
  285. case Win64EH::UOP_SaveFRegP:
  286. Count += 2;
  287. break;
  288. case Win64EH::UOP_SaveFRegX:
  289. Count += 2;
  290. break;
  291. case Win64EH::UOP_SaveFRegPX:
  292. Count += 2;
  293. break;
  294. case Win64EH::UOP_SetFP:
  295. Count += 1;
  296. break;
  297. case Win64EH::UOP_AddFP:
  298. Count += 2;
  299. break;
  300. case Win64EH::UOP_Nop:
  301. Count += 1;
  302. break;
  303. case Win64EH::UOP_End:
  304. Count += 1;
  305. break;
  306. case Win64EH::UOP_SaveNext:
  307. Count += 1;
  308. break;
  309. case Win64EH::UOP_TrapFrame:
  310. Count += 1;
  311. break;
  312. case Win64EH::UOP_PushMachFrame:
  313. Count += 1;
  314. break;
  315. case Win64EH::UOP_Context:
  316. Count += 1;
  317. break;
  318. case Win64EH::UOP_ClearUnwoundToCall:
  319. Count += 1;
  320. break;
  321. }
  322. }
  323. return Count;
  324. }
  325. // Unwind opcode encodings and restrictions are documented at
  326. // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
  327. static void ARM64EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
  328. const WinEH::Instruction &inst) {
  329. uint8_t b, reg;
  330. switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
  331. default:
  332. llvm_unreachable("Unsupported ARM64 unwind code");
  333. case Win64EH::UOP_AllocSmall:
  334. b = (inst.Offset >> 4) & 0x1F;
  335. streamer.emitInt8(b);
  336. break;
  337. case Win64EH::UOP_AllocMedium: {
  338. uint16_t hw = (inst.Offset >> 4) & 0x7FF;
  339. b = 0xC0;
  340. b |= (hw >> 8);
  341. streamer.emitInt8(b);
  342. b = hw & 0xFF;
  343. streamer.emitInt8(b);
  344. break;
  345. }
  346. case Win64EH::UOP_AllocLarge: {
  347. uint32_t w;
  348. b = 0xE0;
  349. streamer.emitInt8(b);
  350. w = inst.Offset >> 4;
  351. b = (w & 0x00FF0000) >> 16;
  352. streamer.emitInt8(b);
  353. b = (w & 0x0000FF00) >> 8;
  354. streamer.emitInt8(b);
  355. b = w & 0x000000FF;
  356. streamer.emitInt8(b);
  357. break;
  358. }
  359. case Win64EH::UOP_SetFP:
  360. b = 0xE1;
  361. streamer.emitInt8(b);
  362. break;
  363. case Win64EH::UOP_AddFP:
  364. b = 0xE2;
  365. streamer.emitInt8(b);
  366. b = (inst.Offset >> 3);
  367. streamer.emitInt8(b);
  368. break;
  369. case Win64EH::UOP_Nop:
  370. b = 0xE3;
  371. streamer.emitInt8(b);
  372. break;
  373. case Win64EH::UOP_SaveR19R20X:
  374. b = 0x20;
  375. b |= (inst.Offset >> 3) & 0x1F;
  376. streamer.emitInt8(b);
  377. break;
  378. case Win64EH::UOP_SaveFPLRX:
  379. b = 0x80;
  380. b |= ((inst.Offset - 1) >> 3) & 0x3F;
  381. streamer.emitInt8(b);
  382. break;
  383. case Win64EH::UOP_SaveFPLR:
  384. b = 0x40;
  385. b |= (inst.Offset >> 3) & 0x3F;
  386. streamer.emitInt8(b);
  387. break;
  388. case Win64EH::UOP_SaveReg:
  389. assert(inst.Register >= 19 && "Saved reg must be >= 19");
  390. reg = inst.Register - 19;
  391. b = 0xD0 | ((reg & 0xC) >> 2);
  392. streamer.emitInt8(b);
  393. b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
  394. streamer.emitInt8(b);
  395. break;
  396. case Win64EH::UOP_SaveRegX:
  397. assert(inst.Register >= 19 && "Saved reg must be >= 19");
  398. reg = inst.Register - 19;
  399. b = 0xD4 | ((reg & 0x8) >> 3);
  400. streamer.emitInt8(b);
  401. b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1);
  402. streamer.emitInt8(b);
  403. break;
  404. case Win64EH::UOP_SaveRegP:
  405. assert(inst.Register >= 19 && "Saved registers must be >= 19");
  406. reg = inst.Register - 19;
  407. b = 0xC8 | ((reg & 0xC) >> 2);
  408. streamer.emitInt8(b);
  409. b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
  410. streamer.emitInt8(b);
  411. break;
  412. case Win64EH::UOP_SaveRegPX:
  413. assert(inst.Register >= 19 && "Saved registers must be >= 19");
  414. reg = inst.Register - 19;
  415. b = 0xCC | ((reg & 0xC) >> 2);
  416. streamer.emitInt8(b);
  417. b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1);
  418. streamer.emitInt8(b);
  419. break;
  420. case Win64EH::UOP_SaveLRPair:
  421. assert(inst.Register >= 19 && "Saved reg must be >= 19");
  422. reg = inst.Register - 19;
  423. assert((reg % 2) == 0 && "Saved reg must be 19+2*X");
  424. reg /= 2;
  425. b = 0xD6 | ((reg & 0x7) >> 2);
  426. streamer.emitInt8(b);
  427. b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
  428. streamer.emitInt8(b);
  429. break;
  430. case Win64EH::UOP_SaveFReg:
  431. assert(inst.Register >= 8 && "Saved dreg must be >= 8");
  432. reg = inst.Register - 8;
  433. b = 0xDC | ((reg & 0x4) >> 2);
  434. streamer.emitInt8(b);
  435. b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
  436. streamer.emitInt8(b);
  437. break;
  438. case Win64EH::UOP_SaveFRegX:
  439. assert(inst.Register >= 8 && "Saved dreg must be >= 8");
  440. reg = inst.Register - 8;
  441. b = 0xDE;
  442. streamer.emitInt8(b);
  443. b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1);
  444. streamer.emitInt8(b);
  445. break;
  446. case Win64EH::UOP_SaveFRegP:
  447. assert(inst.Register >= 8 && "Saved dregs must be >= 8");
  448. reg = inst.Register - 8;
  449. b = 0xD8 | ((reg & 0x4) >> 2);
  450. streamer.emitInt8(b);
  451. b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
  452. streamer.emitInt8(b);
  453. break;
  454. case Win64EH::UOP_SaveFRegPX:
  455. assert(inst.Register >= 8 && "Saved dregs must be >= 8");
  456. reg = inst.Register - 8;
  457. b = 0xDA | ((reg & 0x4) >> 2);
  458. streamer.emitInt8(b);
  459. b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1);
  460. streamer.emitInt8(b);
  461. break;
  462. case Win64EH::UOP_End:
  463. b = 0xE4;
  464. streamer.emitInt8(b);
  465. break;
  466. case Win64EH::UOP_SaveNext:
  467. b = 0xE6;
  468. streamer.emitInt8(b);
  469. break;
  470. case Win64EH::UOP_TrapFrame:
  471. b = 0xE8;
  472. streamer.emitInt8(b);
  473. break;
  474. case Win64EH::UOP_PushMachFrame:
  475. b = 0xE9;
  476. streamer.emitInt8(b);
  477. break;
  478. case Win64EH::UOP_Context:
  479. b = 0xEA;
  480. streamer.emitInt8(b);
  481. break;
  482. case Win64EH::UOP_ClearUnwoundToCall:
  483. b = 0xEC;
  484. streamer.emitInt8(b);
  485. break;
  486. }
  487. }
  488. // Returns the epilog symbol of an epilog with the exact same unwind code
  489. // sequence, if it exists. Otherwise, returns nulltpr.
  490. // EpilogInstrs - Unwind codes for the current epilog.
  491. // Epilogs - Epilogs that potentialy match the current epilog.
  492. static MCSymbol*
  493. FindMatchingEpilog(const std::vector<WinEH::Instruction>& EpilogInstrs,
  494. const std::vector<MCSymbol *>& Epilogs,
  495. const WinEH::FrameInfo *info) {
  496. for (auto *EpilogStart : Epilogs) {
  497. auto InstrsIter = info->EpilogMap.find(EpilogStart);
  498. assert(InstrsIter != info->EpilogMap.end() &&
  499. "Epilog not found in EpilogMap");
  500. const auto &Instrs = InstrsIter->second;
  501. if (Instrs.size() != EpilogInstrs.size())
  502. continue;
  503. bool Match = true;
  504. for (unsigned i = 0; i < Instrs.size(); ++i)
  505. if (Instrs[i].Operation != EpilogInstrs[i].Operation ||
  506. Instrs[i].Offset != EpilogInstrs[i].Offset ||
  507. Instrs[i].Register != EpilogInstrs[i].Register) {
  508. Match = false;
  509. break;
  510. }
  511. if (Match)
  512. return EpilogStart;
  513. }
  514. return nullptr;
  515. }
  516. static void simplifyOpcodes(std::vector<WinEH::Instruction> &Instructions,
  517. bool Reverse) {
  518. unsigned PrevOffset = -1;
  519. unsigned PrevRegister = -1;
  520. auto VisitInstruction = [&](WinEH::Instruction &Inst) {
  521. // Convert 2-byte opcodes into equivalent 1-byte ones.
  522. if (Inst.Operation == Win64EH::UOP_SaveRegP && Inst.Register == 29) {
  523. Inst.Operation = Win64EH::UOP_SaveFPLR;
  524. Inst.Register = -1;
  525. } else if (Inst.Operation == Win64EH::UOP_SaveRegPX &&
  526. Inst.Register == 29) {
  527. Inst.Operation = Win64EH::UOP_SaveFPLRX;
  528. Inst.Register = -1;
  529. } else if (Inst.Operation == Win64EH::UOP_SaveRegPX &&
  530. Inst.Register == 19 && Inst.Offset <= 248) {
  531. Inst.Operation = Win64EH::UOP_SaveR19R20X;
  532. Inst.Register = -1;
  533. } else if (Inst.Operation == Win64EH::UOP_AddFP && Inst.Offset == 0) {
  534. Inst.Operation = Win64EH::UOP_SetFP;
  535. } else if (Inst.Operation == Win64EH::UOP_SaveRegP &&
  536. Inst.Register == PrevRegister + 2 &&
  537. Inst.Offset == PrevOffset + 16) {
  538. Inst.Operation = Win64EH::UOP_SaveNext;
  539. Inst.Register = -1;
  540. Inst.Offset = 0;
  541. // Intentionally not creating UOP_SaveNext for float register pairs,
  542. // as current versions of Windows (up to at least 20.04) is buggy
  543. // regarding SaveNext for float pairs.
  544. }
  545. // Update info about the previous instruction, for detecting if
  546. // the next one can be made a UOP_SaveNext
  547. if (Inst.Operation == Win64EH::UOP_SaveR19R20X) {
  548. PrevOffset = 0;
  549. PrevRegister = 19;
  550. } else if (Inst.Operation == Win64EH::UOP_SaveRegPX) {
  551. PrevOffset = 0;
  552. PrevRegister = Inst.Register;
  553. } else if (Inst.Operation == Win64EH::UOP_SaveRegP) {
  554. PrevOffset = Inst.Offset;
  555. PrevRegister = Inst.Register;
  556. } else if (Inst.Operation == Win64EH::UOP_SaveNext) {
  557. PrevRegister += 2;
  558. PrevOffset += 16;
  559. } else {
  560. PrevRegister = -1;
  561. PrevOffset = -1;
  562. }
  563. };
  564. // Iterate over instructions in a forward order (for prologues),
  565. // backwards for epilogues (i.e. always reverse compared to how the
  566. // opcodes are stored).
  567. if (Reverse) {
  568. for (auto It = Instructions.rbegin(); It != Instructions.rend(); It++)
  569. VisitInstruction(*It);
  570. } else {
  571. for (WinEH::Instruction &Inst : Instructions)
  572. VisitInstruction(Inst);
  573. }
  574. }
  575. static int checkPackedEpilog(MCStreamer &streamer, WinEH::FrameInfo *info,
  576. int PrologCodeBytes) {
  577. // Can only pack if there's one single epilog
  578. if (info->EpilogMap.size() != 1)
  579. return -1;
  580. const std::vector<WinEH::Instruction> &Epilog =
  581. info->EpilogMap.begin()->second;
  582. // Can pack if the epilog is a subset of the prolog but not vice versa
  583. if (Epilog.size() > info->Instructions.size())
  584. return -1;
  585. // Check that the epilog actually is a perfect match for the end (backwrds)
  586. // of the prolog.
  587. for (int I = Epilog.size() - 1; I >= 0; I--) {
  588. if (info->Instructions[I] != Epilog[Epilog.size() - 1 - I])
  589. return -1;
  590. }
  591. // Check that the epilog actually is at the very end of the function,
  592. // otherwise it can't be packed.
  593. uint32_t DistanceFromEnd = (uint32_t)GetAbsDifference(
  594. streamer, info->FuncletOrFuncEnd, info->EpilogMap.begin()->first);
  595. if (DistanceFromEnd / 4 != Epilog.size())
  596. return -1;
  597. int Offset = Epilog.size() == info->Instructions.size()
  598. ? 0
  599. : ARM64CountOfUnwindCodes(ArrayRef<WinEH::Instruction>(
  600. &info->Instructions[Epilog.size()],
  601. info->Instructions.size() - Epilog.size()));
  602. // Check that the offset and prolog size fits in the first word; it's
  603. // unclear whether the epilog count in the extension word can be taken
  604. // as packed epilog offset.
  605. if (Offset > 31 || PrologCodeBytes > 124)
  606. return -1;
  607. info->EpilogMap.clear();
  608. return Offset;
  609. }
  610. static bool tryPackedUnwind(WinEH::FrameInfo *info, uint32_t FuncLength,
  611. int PackedEpilogOffset) {
  612. if (PackedEpilogOffset == 0) {
  613. // Fully symmetric prolog and epilog, should be ok for packed format.
  614. // For CR=3, the corresponding synthesized epilog actually lacks the
  615. // SetFP opcode, but unwinding should work just fine despite that
  616. // (if at the SetFP opcode, the unwinder considers it as part of the
  617. // function body and just unwinds the full prolog instead).
  618. } else if (PackedEpilogOffset == 1) {
  619. // One single case of differences between prolog and epilog is allowed:
  620. // The epilog can lack a single SetFP that is the last opcode in the
  621. // prolog, for the CR=3 case.
  622. if (info->Instructions.back().Operation != Win64EH::UOP_SetFP)
  623. return false;
  624. } else {
  625. // Too much difference between prolog and epilog.
  626. return false;
  627. }
  628. unsigned RegI = 0, RegF = 0;
  629. int Predecrement = 0;
  630. enum {
  631. Start,
  632. Start2,
  633. IntRegs,
  634. FloatRegs,
  635. InputArgs,
  636. StackAdjust,
  637. FrameRecord,
  638. End
  639. } Location = Start;
  640. bool StandaloneLR = false, FPLRPair = false;
  641. int StackOffset = 0;
  642. int Nops = 0;
  643. // Iterate over the prolog and check that all opcodes exactly match
  644. // the canonical order and form. A more lax check could verify that
  645. // all saved registers are in the expected locations, but not enforce
  646. // the order - that would work fine when unwinding from within
  647. // functions, but not be exactly right if unwinding happens within
  648. // prologs/epilogs.
  649. for (const WinEH::Instruction &Inst : info->Instructions) {
  650. switch (Inst.Operation) {
  651. case Win64EH::UOP_End:
  652. if (Location != Start)
  653. return false;
  654. Location = Start2;
  655. break;
  656. case Win64EH::UOP_SaveR19R20X:
  657. if (Location != Start2)
  658. return false;
  659. Predecrement = Inst.Offset;
  660. RegI = 2;
  661. Location = IntRegs;
  662. break;
  663. case Win64EH::UOP_SaveRegX:
  664. if (Location != Start2)
  665. return false;
  666. Predecrement = Inst.Offset;
  667. if (Inst.Register == 19)
  668. RegI += 1;
  669. else if (Inst.Register == 30)
  670. StandaloneLR = true;
  671. else
  672. return false;
  673. // Odd register; can't be any further int registers.
  674. Location = FloatRegs;
  675. break;
  676. case Win64EH::UOP_SaveRegPX:
  677. // Can't have this in a canonical prologue. Either this has been
  678. // canonicalized into SaveR19R20X or SaveFPLRX, or it's an unsupported
  679. // register pair.
  680. // It can't be canonicalized into SaveR19R20X if the offset is
  681. // larger than 248 bytes, but even with the maximum case with
  682. // RegI=10/RegF=8/CR=1/H=1, we end up with SavSZ = 216, which should
  683. // fit into SaveR19R20X.
  684. // The unwinding opcodes can't describe the otherwise seemingly valid
  685. // case for RegI=1 CR=1, that would start with a
  686. // "stp x19, lr, [sp, #-...]!" as that fits neither SaveRegPX nor
  687. // SaveLRPair.
  688. return false;
  689. case Win64EH::UOP_SaveRegP:
  690. if (Location != IntRegs || Inst.Offset != 8 * RegI ||
  691. Inst.Register != 19 + RegI)
  692. return false;
  693. RegI += 2;
  694. break;
  695. case Win64EH::UOP_SaveReg:
  696. if (Location != IntRegs || Inst.Offset != 8 * RegI)
  697. return false;
  698. if (Inst.Register == 19 + RegI)
  699. RegI += 1;
  700. else if (Inst.Register == 30)
  701. StandaloneLR = true;
  702. else
  703. return false;
  704. // Odd register; can't be any further int registers.
  705. Location = FloatRegs;
  706. break;
  707. case Win64EH::UOP_SaveLRPair:
  708. if (Location != IntRegs || Inst.Offset != 8 * RegI ||
  709. Inst.Register != 19 + RegI)
  710. return false;
  711. RegI += 1;
  712. StandaloneLR = true;
  713. Location = FloatRegs;
  714. break;
  715. case Win64EH::UOP_SaveFRegX:
  716. // Packed unwind can't handle prologs that only save one single
  717. // float register.
  718. return false;
  719. case Win64EH::UOP_SaveFReg:
  720. if (Location != FloatRegs || RegF == 0 || Inst.Register != 8 + RegF ||
  721. Inst.Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
  722. return false;
  723. RegF += 1;
  724. Location = InputArgs;
  725. break;
  726. case Win64EH::UOP_SaveFRegPX:
  727. if (Location != Start2 || Inst.Register != 8)
  728. return false;
  729. Predecrement = Inst.Offset;
  730. RegF = 2;
  731. Location = FloatRegs;
  732. break;
  733. case Win64EH::UOP_SaveFRegP:
  734. if ((Location != IntRegs && Location != FloatRegs) ||
  735. Inst.Register != 8 + RegF ||
  736. Inst.Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
  737. return false;
  738. RegF += 2;
  739. Location = FloatRegs;
  740. break;
  741. case Win64EH::UOP_SaveNext:
  742. if (Location == IntRegs)
  743. RegI += 2;
  744. else if (Location == FloatRegs)
  745. RegF += 2;
  746. else
  747. return false;
  748. break;
  749. case Win64EH::UOP_Nop:
  750. if (Location != IntRegs && Location != FloatRegs && Location != InputArgs)
  751. return false;
  752. Location = InputArgs;
  753. Nops++;
  754. break;
  755. case Win64EH::UOP_AllocSmall:
  756. case Win64EH::UOP_AllocMedium:
  757. if (Location != Start2 && Location != IntRegs && Location != FloatRegs &&
  758. Location != InputArgs && Location != StackAdjust)
  759. return false;
  760. // Can have either a single decrement, or a pair of decrements with
  761. // 4080 and another decrement.
  762. if (StackOffset == 0)
  763. StackOffset = Inst.Offset;
  764. else if (StackOffset != 4080)
  765. return false;
  766. else
  767. StackOffset += Inst.Offset;
  768. Location = StackAdjust;
  769. break;
  770. case Win64EH::UOP_SaveFPLRX:
  771. // Not allowing FPLRX after StackAdjust; if a StackAdjust is used, it
  772. // should be followed by a FPLR instead.
  773. if (Location != Start2 && Location != IntRegs && Location != FloatRegs &&
  774. Location != InputArgs)
  775. return false;
  776. StackOffset = Inst.Offset;
  777. Location = FrameRecord;
  778. FPLRPair = true;
  779. break;
  780. case Win64EH::UOP_SaveFPLR:
  781. // This can only follow after a StackAdjust
  782. if (Location != StackAdjust || Inst.Offset != 0)
  783. return false;
  784. Location = FrameRecord;
  785. FPLRPair = true;
  786. break;
  787. case Win64EH::UOP_SetFP:
  788. if (Location != FrameRecord)
  789. return false;
  790. Location = End;
  791. break;
  792. }
  793. }
  794. if (RegI > 10 || RegF > 8)
  795. return false;
  796. if (StandaloneLR && FPLRPair)
  797. return false;
  798. if (FPLRPair && Location != End)
  799. return false;
  800. if (Nops != 0 && Nops != 4)
  801. return false;
  802. int H = Nops == 4;
  803. int IntSZ = 8 * RegI;
  804. if (StandaloneLR)
  805. IntSZ += 8;
  806. int FpSZ = 8 * RegF; // RegF not yet decremented
  807. int SavSZ = (IntSZ + FpSZ + 8 * 8 * H + 0xF) & ~0xF;
  808. if (Predecrement != SavSZ)
  809. return false;
  810. if (FPLRPair && StackOffset < 16)
  811. return false;
  812. if (StackOffset % 16)
  813. return false;
  814. uint32_t FrameSize = (StackOffset + SavSZ) / 16;
  815. if (FrameSize > 0x1FF)
  816. return false;
  817. assert(RegF != 1 && "One single float reg not allowed");
  818. if (RegF > 0)
  819. RegF--; // Convert from actual number of registers, to value stored
  820. assert(FuncLength <= 0x7FF && "FuncLength should have been checked earlier");
  821. int Flag = 0x01; // Function segments not supported yet
  822. int CR = FPLRPair ? 3 : StandaloneLR ? 1 : 0;
  823. info->PackedInfo |= Flag << 0;
  824. info->PackedInfo |= (FuncLength & 0x7FF) << 2;
  825. info->PackedInfo |= (RegF & 0x7) << 13;
  826. info->PackedInfo |= (RegI & 0xF) << 16;
  827. info->PackedInfo |= (H & 0x1) << 20;
  828. info->PackedInfo |= (CR & 0x3) << 21;
  829. info->PackedInfo |= (FrameSize & 0x1FF) << 23;
  830. return true;
  831. }
  832. // Populate the .xdata section. The format of .xdata on ARM64 is documented at
  833. // https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
  834. static void ARM64EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info,
  835. bool TryPacked = true) {
  836. // If this UNWIND_INFO already has a symbol, it's already been emitted.
  837. if (info->Symbol)
  838. return;
  839. // If there's no unwind info here (not even a terminating UOP_End), the
  840. // unwind info is considered bogus and skipped. If this was done in
  841. // response to an explicit .seh_handlerdata, the associated trailing
  842. // handler data is left orphaned in the xdata section.
  843. if (info->empty()) {
  844. info->EmitAttempted = true;
  845. return;
  846. }
  847. if (info->EmitAttempted) {
  848. // If we tried to emit unwind info before (due to an explicit
  849. // .seh_handlerdata directive), but skipped it (because there was no
  850. // valid information to emit at the time), and it later got valid unwind
  851. // opcodes, we can't emit it here, because the trailing handler data
  852. // was already emitted elsewhere in the xdata section.
  853. streamer.getContext().reportError(
  854. SMLoc(), "Earlier .seh_handlerdata for " + info->Function->getName() +
  855. " skipped due to no unwind info at the time "
  856. "(.seh_handlerdata too early?), but the function later "
  857. "did get unwind info that can't be emitted");
  858. return;
  859. }
  860. simplifyOpcodes(info->Instructions, false);
  861. for (auto &I : info->EpilogMap)
  862. simplifyOpcodes(I.second, true);
  863. MCContext &context = streamer.getContext();
  864. MCSymbol *Label = context.createTempSymbol();
  865. streamer.emitValueToAlignment(4);
  866. streamer.emitLabel(Label);
  867. info->Symbol = Label;
  868. int64_t RawFuncLength;
  869. if (!info->FuncletOrFuncEnd) {
  870. report_fatal_error("FuncletOrFuncEnd not set");
  871. } else {
  872. // FIXME: GetAbsDifference tries to compute the length of the function
  873. // immediately, before the whole file is emitted, but in general
  874. // that's impossible: the size in bytes of certain assembler directives
  875. // like .align and .fill is not known until the whole file is parsed and
  876. // relaxations are applied. Currently, GetAbsDifference fails with a fatal
  877. // error in that case. (We mostly don't hit this because inline assembly
  878. // specifying those directives is rare, and we don't normally try to
  879. // align loops on AArch64.)
  880. //
  881. // There are two potential approaches to delaying the computation. One,
  882. // we could emit something like ".word (endfunc-beginfunc)/4+0x10800000",
  883. // as long as we have some conservative estimate we could use to prove
  884. // that we don't need to split the unwind data. Emitting the constant
  885. // is straightforward, but there's no existing code for estimating the
  886. // size of the function.
  887. //
  888. // The other approach would be to use a dedicated, relaxable fragment,
  889. // which could grow to accommodate splitting the unwind data if
  890. // necessary. This is more straightforward, since it automatically works
  891. // without any new infrastructure, and it's consistent with how we handle
  892. // relaxation in other contexts. But it would require some refactoring
  893. // to move parts of the pdata/xdata emission into the implementation of
  894. // a fragment. We could probably continue to encode the unwind codes
  895. // here, but we'd have to emit the pdata, the xdata header, and the
  896. // epilogue scopes later, since they depend on whether the we need to
  897. // split the unwind data.
  898. RawFuncLength = GetAbsDifference(streamer, info->FuncletOrFuncEnd,
  899. info->Begin);
  900. }
  901. if (RawFuncLength > 0xFFFFF)
  902. report_fatal_error("SEH unwind data splitting not yet implemented");
  903. uint32_t FuncLength = (uint32_t)RawFuncLength / 4;
  904. uint32_t PrologCodeBytes = ARM64CountOfUnwindCodes(info->Instructions);
  905. uint32_t TotalCodeBytes = PrologCodeBytes;
  906. int PackedEpilogOffset = checkPackedEpilog(streamer, info, PrologCodeBytes);
  907. if (PackedEpilogOffset >= 0 && !info->HandlesExceptions &&
  908. FuncLength <= 0x7ff && TryPacked) {
  909. // Matching prolog/epilog and no exception handlers; check if the
  910. // prolog matches the patterns that can be described by the packed
  911. // format.
  912. // info->Symbol was already set even if we didn't actually write any
  913. // unwind info there. Keep using that as indicator that this unwind
  914. // info has been generated already.
  915. if (tryPackedUnwind(info, FuncLength, PackedEpilogOffset))
  916. return;
  917. }
  918. // Process epilogs.
  919. MapVector<MCSymbol *, uint32_t> EpilogInfo;
  920. // Epilogs processed so far.
  921. std::vector<MCSymbol *> AddedEpilogs;
  922. for (auto &I : info->EpilogMap) {
  923. MCSymbol *EpilogStart = I.first;
  924. auto &EpilogInstrs = I.second;
  925. uint32_t CodeBytes = ARM64CountOfUnwindCodes(EpilogInstrs);
  926. MCSymbol* MatchingEpilog =
  927. FindMatchingEpilog(EpilogInstrs, AddedEpilogs, info);
  928. if (MatchingEpilog) {
  929. assert(EpilogInfo.find(MatchingEpilog) != EpilogInfo.end() &&
  930. "Duplicate epilog not found");
  931. EpilogInfo[EpilogStart] = EpilogInfo.lookup(MatchingEpilog);
  932. // Clear the unwind codes in the EpilogMap, so that they don't get output
  933. // in the logic below.
  934. EpilogInstrs.clear();
  935. } else {
  936. EpilogInfo[EpilogStart] = TotalCodeBytes;
  937. TotalCodeBytes += CodeBytes;
  938. AddedEpilogs.push_back(EpilogStart);
  939. }
  940. }
  941. // Code Words, Epilog count, E, X, Vers, Function Length
  942. uint32_t row1 = 0x0;
  943. uint32_t CodeWords = TotalCodeBytes / 4;
  944. uint32_t CodeWordsMod = TotalCodeBytes % 4;
  945. if (CodeWordsMod)
  946. CodeWords++;
  947. uint32_t EpilogCount =
  948. PackedEpilogOffset >= 0 ? PackedEpilogOffset : info->EpilogMap.size();
  949. bool ExtensionWord = EpilogCount > 31 || TotalCodeBytes > 124;
  950. if (!ExtensionWord) {
  951. row1 |= (EpilogCount & 0x1F) << 22;
  952. row1 |= (CodeWords & 0x1F) << 27;
  953. }
  954. if (info->HandlesExceptions) // X
  955. row1 |= 1 << 20;
  956. if (PackedEpilogOffset >= 0) // E
  957. row1 |= 1 << 21;
  958. row1 |= FuncLength & 0x3FFFF;
  959. streamer.emitInt32(row1);
  960. // Extended Code Words, Extended Epilog Count
  961. if (ExtensionWord) {
  962. // FIXME: We should be able to split unwind info into multiple sections.
  963. // FIXME: We should share epilog codes across epilogs, where possible,
  964. // which would make this issue show up less frequently.
  965. if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
  966. report_fatal_error("SEH unwind data splitting not yet implemented");
  967. uint32_t row2 = 0x0;
  968. row2 |= (CodeWords & 0xFF) << 16;
  969. row2 |= (EpilogCount & 0xFFFF);
  970. streamer.emitInt32(row2);
  971. }
  972. // Epilog Start Index, Epilog Start Offset
  973. for (auto &I : EpilogInfo) {
  974. MCSymbol *EpilogStart = I.first;
  975. uint32_t EpilogIndex = I.second;
  976. uint32_t EpilogOffset =
  977. (uint32_t)GetAbsDifference(streamer, EpilogStart, info->Begin);
  978. if (EpilogOffset)
  979. EpilogOffset /= 4;
  980. uint32_t row3 = EpilogOffset;
  981. row3 |= (EpilogIndex & 0x3FF) << 22;
  982. streamer.emitInt32(row3);
  983. }
  984. // Emit prolog unwind instructions (in reverse order).
  985. uint8_t numInst = info->Instructions.size();
  986. for (uint8_t c = 0; c < numInst; ++c) {
  987. WinEH::Instruction inst = info->Instructions.back();
  988. info->Instructions.pop_back();
  989. ARM64EmitUnwindCode(streamer, info->Begin, inst);
  990. }
  991. // Emit epilog unwind instructions
  992. for (auto &I : info->EpilogMap) {
  993. auto &EpilogInstrs = I.second;
  994. for (const WinEH::Instruction &inst : EpilogInstrs)
  995. ARM64EmitUnwindCode(streamer, info->Begin, inst);
  996. }
  997. int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
  998. assert(BytesMod >= 0);
  999. for (int i = 0; i < BytesMod; i++)
  1000. streamer.emitInt8(0xE3);
  1001. if (info->HandlesExceptions)
  1002. streamer.emitValue(
  1003. MCSymbolRefExpr::create(info->ExceptionHandler,
  1004. MCSymbolRefExpr::VK_COFF_IMGREL32, context),
  1005. 4);
  1006. }
  1007. static void ARM64EmitRuntimeFunction(MCStreamer &streamer,
  1008. const WinEH::FrameInfo *info) {
  1009. MCContext &context = streamer.getContext();
  1010. streamer.emitValueToAlignment(4);
  1011. EmitSymbolRefWithOfs(streamer, info->Begin, info->Begin);
  1012. if (info->PackedInfo)
  1013. streamer.emitInt32(info->PackedInfo);
  1014. else
  1015. streamer.emitValue(
  1016. MCSymbolRefExpr::create(info->Symbol, MCSymbolRefExpr::VK_COFF_IMGREL32,
  1017. context),
  1018. 4);
  1019. }
  1020. void llvm::Win64EH::ARM64UnwindEmitter::Emit(MCStreamer &Streamer) const {
  1021. // Emit the unwind info structs first.
  1022. for (const auto &CFI : Streamer.getWinFrameInfos()) {
  1023. WinEH::FrameInfo *Info = CFI.get();
  1024. if (Info->empty())
  1025. continue;
  1026. MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
  1027. Streamer.SwitchSection(XData);
  1028. ARM64EmitUnwindInfo(Streamer, Info);
  1029. }
  1030. // Now emit RUNTIME_FUNCTION entries.
  1031. for (const auto &CFI : Streamer.getWinFrameInfos()) {
  1032. WinEH::FrameInfo *Info = CFI.get();
  1033. // ARM64EmitUnwindInfo above clears the info struct, so we can't check
  1034. // empty here. But if a Symbol is set, we should create the corresponding
  1035. // pdata entry.
  1036. if (!Info->Symbol)
  1037. continue;
  1038. MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
  1039. Streamer.SwitchSection(PData);
  1040. ARM64EmitRuntimeFunction(Streamer, Info);
  1041. }
  1042. }
  1043. void llvm::Win64EH::ARM64UnwindEmitter::EmitUnwindInfo(MCStreamer &Streamer,
  1044. WinEH::FrameInfo *info,
  1045. bool HandlerData) const {
  1046. // Called if there's an .seh_handlerdata directive before the end of the
  1047. // function. This forces writing the xdata record already here - and
  1048. // in this case, the function isn't actually ended already, but the xdata
  1049. // record needs to know the function length. In these cases, if the funclet
  1050. // end hasn't been marked yet, the xdata function length won't cover the
  1051. // whole function, only up to this point.
  1052. if (!info->FuncletOrFuncEnd) {
  1053. Streamer.SwitchSection(info->TextSection);
  1054. info->FuncletOrFuncEnd = Streamer.emitCFILabel();
  1055. }
  1056. // Switch sections (the static function above is meant to be called from
  1057. // here and from Emit().
  1058. MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
  1059. Streamer.SwitchSection(XData);
  1060. ARM64EmitUnwindInfo(Streamer, info, /* TryPacked = */ !HandlerData);
  1061. }