blob: 8bc1f08c88750eb1b3fbc12c0c4358beb7c27fda [file] [log] [blame]
Charles Davis3185f5c2011-05-22 03:01:05 +00001//===- lib/MC/MCWin64EH.cpp - MCWin64EH implementation --------------------===//
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#include "llvm/MC/MCWin64EH.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000011#include "llvm/ADT/Twine.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000012#include "llvm/MC/MCContext.h"
13#include "llvm/MC/MCExpr.h"
Sanjin Sijaric18412a62018-10-27 06:13:06 +000014#include "llvm/MC/MCObjectFileInfo.h"
15#include "llvm/MC/MCObjectStreamer.h"
16#include "llvm/MC/MCSectionCOFF.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000017#include "llvm/MC/MCStreamer.h"
18#include "llvm/MC/MCSymbol.h"
Saleem Abdulrasooladcb2d12014-07-17 03:08:50 +000019#include "llvm/Support/Win64EH.h"
Charles Davis3185f5c2011-05-22 03:01:05 +000020
Reid Kleckner7f90dd22016-05-02 23:22:18 +000021using namespace llvm;
Charles Davis3185f5c2011-05-22 03:01:05 +000022
23// NOTE: All relocations generated here are 4-byte image-relative.
24
Saleem Abdulrasooladcb2d12014-07-17 03:08:50 +000025static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
Saleem Abdulrasool34451792014-07-10 04:50:09 +000026 uint8_t Count = 0;
27 for (const auto &I : Insns) {
Saleem Abdulrasooladcb2d12014-07-17 03:08:50 +000028 switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
Sanjin Sijaric18412a62018-10-27 06:13:06 +000029 default:
30 llvm_unreachable("Unsupported unwind code");
Charles Davis3185f5c2011-05-22 03:01:05 +000031 case Win64EH::UOP_PushNonVol:
32 case Win64EH::UOP_AllocSmall:
33 case Win64EH::UOP_SetFPReg:
34 case Win64EH::UOP_PushMachFrame:
Saleem Abdulrasool34451792014-07-10 04:50:09 +000035 Count += 1;
Charles Davisef607242011-05-27 02:43:19 +000036 break;
Charles Davis3185f5c2011-05-22 03:01:05 +000037 case Win64EH::UOP_SaveNonVol:
38 case Win64EH::UOP_SaveXMM128:
Saleem Abdulrasool34451792014-07-10 04:50:09 +000039 Count += 2;
Charles Davisef607242011-05-27 02:43:19 +000040 break;
Charles Davis3185f5c2011-05-22 03:01:05 +000041 case Win64EH::UOP_SaveNonVolBig:
42 case Win64EH::UOP_SaveXMM128Big:
Saleem Abdulrasool34451792014-07-10 04:50:09 +000043 Count += 3;
Charles Davisef607242011-05-27 02:43:19 +000044 break;
Charles Davis3185f5c2011-05-22 03:01:05 +000045 case Win64EH::UOP_AllocLarge:
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +000046 Count += (I.Offset > 512 * 1024 - 8) ? 3 : 2;
Charles Davisef607242011-05-27 02:43:19 +000047 break;
Charles Davis3185f5c2011-05-22 03:01:05 +000048 }
49 }
Saleem Abdulrasool34451792014-07-10 04:50:09 +000050 return Count;
Charles Davis3185f5c2011-05-22 03:01:05 +000051}
52
Saleem Abdulrasool117ff622014-07-13 19:03:40 +000053static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
54 const MCSymbol *RHS) {
55 MCContext &Context = Streamer.getContext();
56 const MCExpr *Diff =
Jim Grosbach586c0042015-05-30 01:25:56 +000057 MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
58 MCSymbolRefExpr::create(RHS, Context), Context);
Rafael Espindola746b9062014-08-15 02:51:31 +000059 Streamer.EmitValue(Diff, 1);
Charles Davisc4cbf9b2011-05-27 03:25:01 +000060}
61
Saleem Abdulrasool2aded1f2014-08-03 18:51:17 +000062static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
Saleem Abdulrasooladcb2d12014-07-17 03:08:50 +000063 WinEH::Instruction &inst) {
Kai Nackef0a0d572013-08-27 04:16:16 +000064 uint8_t b2;
Charles Davis3185f5c2011-05-22 03:01:05 +000065 uint16_t w;
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +000066 b2 = (inst.Operation & 0x0F);
Saleem Abdulrasooladcb2d12014-07-17 03:08:50 +000067 switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
Sanjin Sijaric18412a62018-10-27 06:13:06 +000068 default:
69 llvm_unreachable("Unsupported unwind code");
Charles Davis3185f5c2011-05-22 03:01:05 +000070 case Win64EH::UOP_PushNonVol:
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +000071 EmitAbsDifference(streamer, inst.Label, begin);
72 b2 |= (inst.Register & 0x0F) << 4;
Charles Davis3185f5c2011-05-22 03:01:05 +000073 streamer.EmitIntValue(b2, 1);
74 break;
75 case Win64EH::UOP_AllocLarge:
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +000076 EmitAbsDifference(streamer, inst.Label, begin);
77 if (inst.Offset > 512 * 1024 - 8) {
Charles Davis07cbe232011-05-27 15:10:25 +000078 b2 |= 0x10;
Charles Davis3185f5c2011-05-22 03:01:05 +000079 streamer.EmitIntValue(b2, 1);
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +000080 w = inst.Offset & 0xFFF8;
Charles Davis3185f5c2011-05-22 03:01:05 +000081 streamer.EmitIntValue(w, 2);
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +000082 w = inst.Offset >> 16;
Charles Davis3185f5c2011-05-22 03:01:05 +000083 } else {
84 streamer.EmitIntValue(b2, 1);
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +000085 w = inst.Offset >> 3;
Charles Davis3185f5c2011-05-22 03:01:05 +000086 }
87 streamer.EmitIntValue(w, 2);
88 break;
89 case Win64EH::UOP_AllocSmall:
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +000090 b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
91 EmitAbsDifference(streamer, inst.Label, begin);
Charles Davis3185f5c2011-05-22 03:01:05 +000092 streamer.EmitIntValue(b2, 1);
93 break;
94 case Win64EH::UOP_SetFPReg:
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +000095 EmitAbsDifference(streamer, inst.Label, begin);
Charles Davis3185f5c2011-05-22 03:01:05 +000096 streamer.EmitIntValue(b2, 1);
97 break;
98 case Win64EH::UOP_SaveNonVol:
99 case Win64EH::UOP_SaveXMM128:
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +0000100 b2 |= (inst.Register & 0x0F) << 4;
101 EmitAbsDifference(streamer, inst.Label, begin);
Charles Davis3185f5c2011-05-22 03:01:05 +0000102 streamer.EmitIntValue(b2, 1);
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +0000103 w = inst.Offset >> 3;
104 if (inst.Operation == Win64EH::UOP_SaveXMM128)
Charles Davis3185f5c2011-05-22 03:01:05 +0000105 w >>= 1;
106 streamer.EmitIntValue(w, 2);
107 break;
108 case Win64EH::UOP_SaveNonVolBig:
109 case Win64EH::UOP_SaveXMM128Big:
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +0000110 b2 |= (inst.Register & 0x0F) << 4;
111 EmitAbsDifference(streamer, inst.Label, begin);
Charles Davis3185f5c2011-05-22 03:01:05 +0000112 streamer.EmitIntValue(b2, 1);
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +0000113 if (inst.Operation == Win64EH::UOP_SaveXMM128Big)
114 w = inst.Offset & 0xFFF0;
Charles Davis3185f5c2011-05-22 03:01:05 +0000115 else
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +0000116 w = inst.Offset & 0xFFF8;
Charles Davis3185f5c2011-05-22 03:01:05 +0000117 streamer.EmitIntValue(w, 2);
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +0000118 w = inst.Offset >> 16;
Charles Davis3185f5c2011-05-22 03:01:05 +0000119 streamer.EmitIntValue(w, 2);
120 break;
121 case Win64EH::UOP_PushMachFrame:
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +0000122 if (inst.Offset == 1)
Charles Davis07cbe232011-05-27 15:10:25 +0000123 b2 |= 0x10;
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +0000124 EmitAbsDifference(streamer, inst.Label, begin);
Charles Davis3185f5c2011-05-22 03:01:05 +0000125 streamer.EmitIntValue(b2, 1);
126 break;
127 }
128}
129
Kai Nacke7185bdd2013-09-15 17:46:46 +0000130static void EmitSymbolRefWithOfs(MCStreamer &streamer,
131 const MCSymbol *Base,
132 const MCSymbol *Other) {
133 MCContext &Context = streamer.getContext();
Jim Grosbach586c0042015-05-30 01:25:56 +0000134 const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::create(Base, Context);
135 const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::create(Other, Context);
136 const MCExpr *Ofs = MCBinaryExpr::createSub(OtherRef, BaseRef, Context);
137 const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base,
Kai Nacke7185bdd2013-09-15 17:46:46 +0000138 MCSymbolRefExpr::VK_COFF_IMGREL32,
139 Context);
Jim Grosbach586c0042015-05-30 01:25:56 +0000140 streamer.EmitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
Kai Nacke7185bdd2013-09-15 17:46:46 +0000141}
142
Charles Davis3185f5c2011-05-22 03:01:05 +0000143static void EmitRuntimeFunction(MCStreamer &streamer,
Saleem Abdulrasool2aded1f2014-08-03 18:51:17 +0000144 const WinEH::FrameInfo *info) {
Charles Davis3185f5c2011-05-22 03:01:05 +0000145 MCContext &context = streamer.getContext();
146
Charles Davisef607242011-05-27 02:43:19 +0000147 streamer.EmitValueToAlignment(4);
Kai Nacke7185bdd2013-09-15 17:46:46 +0000148 EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
149 EmitSymbolRefWithOfs(streamer, info->Function, info->End);
Jim Grosbach586c0042015-05-30 01:25:56 +0000150 streamer.EmitValue(MCSymbolRefExpr::create(info->Symbol,
Kai Nacke7185bdd2013-09-15 17:46:46 +0000151 MCSymbolRefExpr::VK_COFF_IMGREL32,
152 context), 4);
Charles Davis3185f5c2011-05-22 03:01:05 +0000153}
154
Saleem Abdulrasool2aded1f2014-08-03 18:51:17 +0000155static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
Charles Davis3185f5c2011-05-22 03:01:05 +0000156 // If this UNWIND_INFO already has a symbol, it's already been emitted.
Saleem Abdulrasool2aded1f2014-08-03 18:51:17 +0000157 if (info->Symbol)
158 return;
Charles Davis3185f5c2011-05-22 03:01:05 +0000159
160 MCContext &context = streamer.getContext();
Jim Grosbach19696da2015-05-18 18:43:14 +0000161 MCSymbol *Label = context.createTempSymbol();
Saleem Abdulrasool2aded1f2014-08-03 18:51:17 +0000162
Charles Davisef607242011-05-27 02:43:19 +0000163 streamer.EmitValueToAlignment(4);
Saleem Abdulrasool2aded1f2014-08-03 18:51:17 +0000164 streamer.EmitLabel(Label);
165 info->Symbol = Label;
Charles Davis3185f5c2011-05-22 03:01:05 +0000166
Kai Nackec29a7202013-09-15 18:01:09 +0000167 // Upper 3 bits are the version number (currently 1).
168 uint8_t flags = 0x01;
Charles Davis3185f5c2011-05-22 03:01:05 +0000169 if (info->ChainedParent)
Charles Davis07cbe232011-05-27 15:10:25 +0000170 flags |= Win64EH::UNW_ChainInfo << 3;
Charles Davis3185f5c2011-05-22 03:01:05 +0000171 else {
172 if (info->HandlesUnwind)
Charles Davis07cbe232011-05-27 15:10:25 +0000173 flags |= Win64EH::UNW_TerminateHandler << 3;
Charles Davis3185f5c2011-05-22 03:01:05 +0000174 if (info->HandlesExceptions)
Charles Davis07cbe232011-05-27 15:10:25 +0000175 flags |= Win64EH::UNW_ExceptionHandler << 3;
Charles Davis3185f5c2011-05-22 03:01:05 +0000176 }
177 streamer.EmitIntValue(flags, 1);
178
Charles Davis07cbe232011-05-27 15:10:25 +0000179 if (info->PrologEnd)
180 EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
181 else
182 streamer.EmitIntValue(0, 1);
Charles Davis3185f5c2011-05-22 03:01:05 +0000183
184 uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
185 streamer.EmitIntValue(numCodes, 1);
186
187 uint8_t frame = 0;
188 if (info->LastFrameInst >= 0) {
Saleem Abdulrasooladcb2d12014-07-17 03:08:50 +0000189 WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
Saleem Abdulrasool083db8b2014-07-13 19:03:45 +0000190 assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
191 frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
Charles Davis3185f5c2011-05-22 03:01:05 +0000192 }
193 streamer.EmitIntValue(frame, 1);
194
195 // Emit unwind instructions (in reverse order).
196 uint8_t numInst = info->Instructions.size();
197 for (uint8_t c = 0; c < numInst; ++c) {
Saleem Abdulrasooladcb2d12014-07-17 03:08:50 +0000198 WinEH::Instruction inst = info->Instructions.back();
Charles Davis3185f5c2011-05-22 03:01:05 +0000199 info->Instructions.pop_back();
Charles Davisc4cbf9b2011-05-27 03:25:01 +0000200 EmitUnwindCode(streamer, info->Begin, inst);
Charles Davis3185f5c2011-05-22 03:01:05 +0000201 }
202
Kai Nackec29a7202013-09-15 18:01:09 +0000203 // For alignment purposes, the instruction array will always have an even
204 // number of entries, with the final entry potentially unused (in which case
205 // the array will be one longer than indicated by the count of unwind codes
206 // field).
207 if (numCodes & 1) {
208 streamer.EmitIntValue(0, 2);
209 }
210
Charles Davis07cbe232011-05-27 15:10:25 +0000211 if (flags & (Win64EH::UNW_ChainInfo << 3))
Charles Davis3185f5c2011-05-22 03:01:05 +0000212 EmitRuntimeFunction(streamer, info->ChainedParent);
Charles Davis07cbe232011-05-27 15:10:25 +0000213 else if (flags &
214 ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
Jim Grosbach586c0042015-05-30 01:25:56 +0000215 streamer.EmitValue(MCSymbolRefExpr::create(info->ExceptionHandler,
Kai Nacke7185bdd2013-09-15 17:46:46 +0000216 MCSymbolRefExpr::VK_COFF_IMGREL32,
217 context), 4);
Kai Nackec29a7202013-09-15 18:01:09 +0000218 else if (numCodes == 0) {
Charles Davis07cbe232011-05-27 15:10:25 +0000219 // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
220 // a chained unwind info, if there is no handler, and if there are fewer
221 // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
Kai Nackec29a7202013-09-15 18:01:09 +0000222 streamer.EmitIntValue(0, 4);
Charles Davis07cbe232011-05-27 15:10:25 +0000223 }
Charles Davis3185f5c2011-05-22 03:01:05 +0000224}
225
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000226void llvm::Win64EH::UnwindEmitter::Emit(MCStreamer &Streamer) const {
Charles Davis38ea9ee2011-05-22 04:15:07 +0000227 // Emit the unwind info structs first.
Reid Kleckner91aeb5c2017-10-06 17:21:49 +0000228 for (const auto &CFI : Streamer.getWinFrameInfos()) {
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000229 MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
Saleem Abdulrasool09f505a2014-07-10 04:50:06 +0000230 Streamer.SwitchSection(XData);
Reid Kleckner91aeb5c2017-10-06 17:21:49 +0000231 ::EmitUnwindInfo(Streamer, CFI.get());
Charles Davis7b06b732011-05-27 19:09:24 +0000232 }
Saleem Abdulrasool09f505a2014-07-10 04:50:06 +0000233
Charles Davis38ea9ee2011-05-22 04:15:07 +0000234 // Now emit RUNTIME_FUNCTION entries.
Reid Kleckner91aeb5c2017-10-06 17:21:49 +0000235 for (const auto &CFI : Streamer.getWinFrameInfos()) {
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000236 MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
Saleem Abdulrasool09f505a2014-07-10 04:50:06 +0000237 Streamer.SwitchSection(PData);
Reid Kleckner91aeb5c2017-10-06 17:21:49 +0000238 EmitRuntimeFunction(Streamer, CFI.get());
Charles Davis7b06b732011-05-27 19:09:24 +0000239 }
Charles Davis38ea9ee2011-05-22 04:15:07 +0000240}
241
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000242void llvm::Win64EH::UnwindEmitter::EmitUnwindInfo(
243 MCStreamer &Streamer, WinEH::FrameInfo *info) const {
Saleem Abdulrasool7cc1de12014-08-07 02:59:41 +0000244 // Switch sections (the static function above is meant to be called from
245 // here and from Emit().
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000246 MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
247 Streamer.SwitchSection(XData);
Saleem Abdulrasool7cc1de12014-08-07 02:59:41 +0000248
Reid Kleckner7f90dd22016-05-02 23:22:18 +0000249 ::EmitUnwindInfo(Streamer, info);
Saleem Abdulrasool7cc1de12014-08-07 02:59:41 +0000250}
Charles Davis3185f5c2011-05-22 03:01:05 +0000251
Sanjin Sijaric18412a62018-10-27 06:13:06 +0000252static int64_t GetAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
253 const MCSymbol *RHS) {
254 MCContext &Context = Streamer.getContext();
255 const MCExpr *Diff =
256 MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
257 MCSymbolRefExpr::create(RHS, Context), Context);
258 MCObjectStreamer *OS = (MCObjectStreamer *)(&Streamer);
259 int64_t value;
260 Diff->evaluateAsAbsolute(value, OS->getAssembler());
261 return value;
262}
263
264static uint32_t
265ARM64CountOfUnwindCodes(const std::vector<WinEH::Instruction> &Insns) {
266 uint32_t Count = 0;
267 for (const auto &I : Insns) {
268 switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
269 default:
270 llvm_unreachable("Unsupported ARM64 unwind code");
271 case Win64EH::UOP_AllocSmall:
272 Count += 1;
273 break;
274 case Win64EH::UOP_AllocMedium:
275 Count += 2;
276 break;
277 case Win64EH::UOP_AllocLarge:
278 Count += 4;
279 break;
280 case Win64EH::UOP_SaveFPLRX:
281 Count += 1;
282 break;
283 case Win64EH::UOP_SaveFPLR:
284 Count += 1;
285 break;
286 case Win64EH::UOP_SaveReg:
287 Count += 2;
288 break;
289 case Win64EH::UOP_SaveRegP:
290 Count += 2;
291 break;
292 case Win64EH::UOP_SaveRegPX:
293 Count += 2;
294 break;
295 case Win64EH::UOP_SaveRegX:
296 Count += 2;
297 break;
298 case Win64EH::UOP_SaveFReg:
299 Count += 2;
300 break;
301 case Win64EH::UOP_SaveFRegP:
302 Count += 2;
303 break;
304 case Win64EH::UOP_SaveFRegX:
305 Count += 2;
306 break;
307 case Win64EH::UOP_SaveFRegPX:
308 Count += 2;
309 break;
310 case Win64EH::UOP_SetFP:
311 Count += 1;
312 break;
313 case Win64EH::UOP_AddFP:
314 Count += 2;
315 break;
316 case Win64EH::UOP_Nop:
317 Count += 1;
318 break;
319 case Win64EH::UOP_End:
320 Count += 1;
321 break;
322 }
323 }
324 return Count;
325}
326
327// Unwind opcode encodings and restrictions are documented at
328// https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
329static void ARM64EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
330 WinEH::Instruction &inst) {
331 uint8_t b, reg;
332 switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
333 default:
334 llvm_unreachable("Unsupported ARM64 unwind code");
335 case Win64EH::UOP_AllocSmall:
336 b = (inst.Offset >> 4) & 0x1F;
337 streamer.EmitIntValue(b, 1);
338 break;
339 case Win64EH::UOP_AllocMedium: {
340 uint16_t hw = (inst.Offset >> 4) & 0x7FF;
341 b = 0xC0;
342 b |= (hw >> 8);
343 streamer.EmitIntValue(b, 1);
344 b = hw & 0xFF;
345 streamer.EmitIntValue(b, 1);
346 break;
347 }
348 case Win64EH::UOP_AllocLarge: {
349 uint32_t w;
350 b = 0xE0;
351 streamer.EmitIntValue(b, 1);
352 w = inst.Offset >> 4;
353 b = (w & 0x00FF0000) >> 16;
354 streamer.EmitIntValue(b, 1);
355 b = (w & 0x0000FF00) >> 8;
356 streamer.EmitIntValue(b, 1);
357 b = w & 0x000000FF;
358 streamer.EmitIntValue(b, 1);
359 break;
360 }
361 case Win64EH::UOP_SetFP:
362 b = 0xE1;
363 streamer.EmitIntValue(b, 1);
364 break;
365 case Win64EH::UOP_AddFP:
366 b = 0xE2;
367 streamer.EmitIntValue(b, 1);
368 b = (inst.Offset >> 3);
369 streamer.EmitIntValue(b, 1);
370 break;
371 case Win64EH::UOP_Nop:
372 b = 0xE3;
373 streamer.EmitIntValue(b, 1);
374 break;
375 case Win64EH::UOP_SaveFPLRX:
376 b = 0x80;
377 b |= ((inst.Offset - 1) >> 3) & 0x3F;
378 streamer.EmitIntValue(b, 1);
379 break;
380 case Win64EH::UOP_SaveFPLR:
381 b = 0x40;
382 b |= (inst.Offset >> 3) & 0x3F;
383 streamer.EmitIntValue(b, 1);
384 break;
385 case Win64EH::UOP_SaveReg:
386 assert(inst.Register >= 19 && "Saved reg must be >= 19");
387 reg = inst.Register - 19;
388 b = 0xD0 | ((reg & 0xC) >> 2);
389 streamer.EmitIntValue(b, 1);
390 b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
391 streamer.EmitIntValue(b, 1);
392 break;
393 case Win64EH::UOP_SaveRegX:
394 assert(inst.Register >= 19 && "Saved reg must be >= 19");
395 reg = inst.Register - 19;
396 b = 0xD4 | ((reg & 0x8) >> 3);
397 streamer.EmitIntValue(b, 1);
398 b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1);
399 streamer.EmitIntValue(b, 1);
400 break;
401 case Win64EH::UOP_SaveRegP:
402 assert(inst.Register >= 19 && "Saved registers must be >= 19");
403 reg = inst.Register - 19;
404 b = 0xC8 | ((reg & 0xC) >> 2);
405 streamer.EmitIntValue(b, 1);
406 b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
407 streamer.EmitIntValue(b, 1);
408 break;
409 case Win64EH::UOP_SaveRegPX:
410 assert(inst.Register >= 19 && "Saved registers must be >= 19");
411 reg = inst.Register - 19;
412 b = 0xCC | ((reg & 0xC) >> 2);
413 streamer.EmitIntValue(b, 1);
414 b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1);
415 streamer.EmitIntValue(b, 1);
416 break;
417 case Win64EH::UOP_SaveFReg:
418 assert(inst.Register >= 8 && "Saved dreg must be >= 8");
419 reg = inst.Register - 8;
420 b = 0xDC | ((reg & 0x4) >> 2);
421 streamer.EmitIntValue(b, 1);
422 b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
423 streamer.EmitIntValue(b, 1);
424 break;
425 case Win64EH::UOP_SaveFRegX:
426 assert(inst.Register >= 8 && "Saved dreg must be >= 8");
427 reg = inst.Register - 8;
428 b = 0xDE;
429 streamer.EmitIntValue(b, 1);
430 b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1);
431 streamer.EmitIntValue(b, 1);
432 break;
433 case Win64EH::UOP_SaveFRegP:
434 assert(inst.Register >= 8 && "Saved dregs must be >= 8");
435 reg = inst.Register - 8;
436 b = 0xD8 | ((reg & 0x4) >> 2);
437 streamer.EmitIntValue(b, 1);
438 b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
439 streamer.EmitIntValue(b, 1);
440 break;
441 case Win64EH::UOP_SaveFRegPX:
442 assert(inst.Register >= 8 && "Saved dregs must be >= 8");
443 reg = inst.Register - 8;
444 b = 0xDA | ((reg & 0x4) >> 2);
445 streamer.EmitIntValue(b, 1);
446 b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1);
447 streamer.EmitIntValue(b, 1);
448 break;
449 case Win64EH::UOP_End:
450 b = 0xE4;
451 streamer.EmitIntValue(b, 1);
452 break;
453 }
454}
455
Hans Wennborg87790772019-01-17 15:39:31 +0000456// Returns the epilog symbol of an epilog with the exact same unwind code
457// sequence, if it exists. Otherwise, returns nulltpr.
458// EpilogInstrs - Unwind codes for the current epilog.
459// Epilogs - Epilogs that potentialy match the current epilog.
460static MCSymbol*
461FindMatchingEpilog(const std::vector<WinEH::Instruction>& EpilogInstrs,
462 const std::vector<MCSymbol *>& Epilogs,
463 const WinEH::FrameInfo *info) {
464 for (auto *EpilogStart : Epilogs) {
465 auto InstrsIter = info->EpilogMap.find(EpilogStart);
466 assert(InstrsIter != info->EpilogMap.end() &&
467 "Epilog not found in EpilogMap");
468 const auto &Instrs = InstrsIter->second;
469
470 if (Instrs.size() != EpilogInstrs.size())
471 continue;
472
473 bool Match = true;
474 for (unsigned i = 0; i < Instrs.size(); ++i)
475 if (Instrs[i].Operation != EpilogInstrs[i].Operation ||
476 Instrs[i].Offset != EpilogInstrs[i].Offset ||
477 Instrs[i].Register != EpilogInstrs[i].Register) {
478 Match = false;
479 break;
480 }
481
482 if (Match)
483 return EpilogStart;
484 }
485 return nullptr;
486}
487
Sanjin Sijaric18412a62018-10-27 06:13:06 +0000488// Populate the .xdata section. The format of .xdata on ARM64 is documented at
489// https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
490static void ARM64EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
491 // If this UNWIND_INFO already has a symbol, it's already been emitted.
492 if (info->Symbol)
493 return;
494
495 MCContext &context = streamer.getContext();
496 MCSymbol *Label = context.createTempSymbol();
497
498 streamer.EmitValueToAlignment(4);
499 streamer.EmitLabel(Label);
500 info->Symbol = Label;
501
502 uint32_t FuncLength = 0x0;
Martin Storsjoe15b0c62018-12-18 22:10:17 +0000503 if (info->FuncletOrFuncEnd)
504 FuncLength = (uint32_t)GetAbsDifference(streamer, info->FuncletOrFuncEnd,
505 info->Begin);
506 FuncLength /= 4;
Sanjin Sijaric18412a62018-10-27 06:13:06 +0000507 uint32_t PrologCodeBytes = ARM64CountOfUnwindCodes(info->Instructions);
508 uint32_t TotalCodeBytes = PrologCodeBytes;
509
510 // Process epilogs.
511 MapVector<MCSymbol *, uint32_t> EpilogInfo;
Hans Wennborg87790772019-01-17 15:39:31 +0000512 // Epilogs processed so far.
513 std::vector<MCSymbol *> AddedEpilogs;
514
Sanjin Sijaric18412a62018-10-27 06:13:06 +0000515 for (auto &I : info->EpilogMap) {
516 MCSymbol *EpilogStart = I.first;
517 auto &EpilogInstrs = I.second;
518 uint32_t CodeBytes = ARM64CountOfUnwindCodes(EpilogInstrs);
Hans Wennborg87790772019-01-17 15:39:31 +0000519
Hans Wennborg87790772019-01-17 15:39:31 +0000520 MCSymbol* MatchingEpilog =
521 FindMatchingEpilog(EpilogInstrs, AddedEpilogs, info);
522 if (MatchingEpilog) {
523 assert(EpilogInfo.find(MatchingEpilog) != EpilogInfo.end() &&
524 "Duplicate epilog not found");
525 EpilogInfo[EpilogStart] = EpilogInfo[MatchingEpilog];
526 // Clear the unwind codes in the EpilogMap, so that they don't get output
527 // in the logic below.
528 EpilogInstrs.clear();
529 } else {
530 EpilogInfo[EpilogStart] = TotalCodeBytes;
531 TotalCodeBytes += CodeBytes;
532 AddedEpilogs.push_back(EpilogStart);
533 }
Sanjin Sijaric18412a62018-10-27 06:13:06 +0000534 }
535
536 // Code Words, Epilog count, E, X, Vers, Function Length
537 uint32_t row1 = 0x0;
Eli Friedmana9be2392018-11-08 21:20:52 +0000538 uint32_t CodeWords = TotalCodeBytes / 4;
539 uint32_t CodeWordsMod = TotalCodeBytes % 4;
Sanjin Sijaric18412a62018-10-27 06:13:06 +0000540 if (CodeWordsMod)
541 CodeWords++;
542 uint32_t EpilogCount = info->EpilogMap.size();
543 bool ExtensionWord = EpilogCount > 31 || TotalCodeBytes > 124;
544 if (!ExtensionWord) {
545 row1 |= (EpilogCount & 0x1F) << 22;
546 row1 |= (CodeWords & 0x1F) << 27;
547 }
548 // E is always 0 right now, TODO: packed epilog setup
549 if (info->HandlesExceptions) // X
550 row1 |= 1 << 20;
551 row1 |= FuncLength & 0x3FFFF;
552 streamer.EmitIntValue(row1, 4);
553
554 // Extended Code Words, Extended Epilog Count
555 if (ExtensionWord) {
Eli Friedmana9be2392018-11-08 21:20:52 +0000556 // FIXME: We should be able to split unwind info into multiple sections.
557 // FIXME: We should share epilog codes across epilogs, where possible,
558 // which would make this issue show up less frequently.
559 if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
560 report_fatal_error("SEH unwind data splitting not yet implemented");
Sanjin Sijaric18412a62018-10-27 06:13:06 +0000561 uint32_t row2 = 0x0;
562 row2 |= (CodeWords & 0xFF) << 16;
563 row2 |= (EpilogCount & 0xFFFF);
564 streamer.EmitIntValue(row2, 4);
565 }
566
567 // Epilog Start Index, Epilog Start Offset
568 for (auto &I : EpilogInfo) {
569 MCSymbol *EpilogStart = I.first;
570 uint32_t EpilogIndex = I.second;
571 uint32_t EpilogOffset =
572 (uint32_t)GetAbsDifference(streamer, EpilogStart, info->Begin);
573 if (EpilogOffset)
574 EpilogOffset /= 4;
575 uint32_t row3 = EpilogOffset;
576 row3 |= (EpilogIndex & 0x3FF) << 22;
577 streamer.EmitIntValue(row3, 4);
578 }
579
580 // Emit prolog unwind instructions (in reverse order).
581 uint8_t numInst = info->Instructions.size();
582 for (uint8_t c = 0; c < numInst; ++c) {
583 WinEH::Instruction inst = info->Instructions.back();
584 info->Instructions.pop_back();
585 ARM64EmitUnwindCode(streamer, info->Begin, inst);
586 }
587
588 // Emit epilog unwind instructions
589 for (auto &I : info->EpilogMap) {
590 auto &EpilogInstrs = I.second;
591 for (uint32_t i = 0; i < EpilogInstrs.size(); i++) {
592 WinEH::Instruction inst = EpilogInstrs[i];
593 ARM64EmitUnwindCode(streamer, info->Begin, inst);
594 }
595 }
596
597 int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
598 assert(BytesMod >= 0);
599 for (int i = 0; i < BytesMod; i++)
600 streamer.EmitIntValue(0xE3, 1);
601
602 if (info->HandlesExceptions)
603 streamer.EmitValue(
604 MCSymbolRefExpr::create(info->ExceptionHandler,
605 MCSymbolRefExpr::VK_COFF_IMGREL32, context),
606 4);
607}
608
609static void ARM64EmitRuntimeFunction(MCStreamer &streamer,
610 const WinEH::FrameInfo *info) {
611 MCContext &context = streamer.getContext();
612
613 streamer.EmitValueToAlignment(4);
614 EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
615 streamer.EmitValue(MCSymbolRefExpr::create(info->Symbol,
616 MCSymbolRefExpr::VK_COFF_IMGREL32,
617 context),
618 4);
619}
620
621void llvm::Win64EH::ARM64UnwindEmitter::Emit(MCStreamer &Streamer) const {
622 // Emit the unwind info structs first.
623 for (const auto &CFI : Streamer.getWinFrameInfos()) {
624 MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
625 Streamer.SwitchSection(XData);
626 ARM64EmitUnwindInfo(Streamer, CFI.get());
627 }
628
629 // Now emit RUNTIME_FUNCTION entries.
630 for (const auto &CFI : Streamer.getWinFrameInfos()) {
631 MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
632 Streamer.SwitchSection(PData);
633 ARM64EmitRuntimeFunction(Streamer, CFI.get());
634 }
635}
636
637void llvm::Win64EH::ARM64UnwindEmitter::EmitUnwindInfo(
638 MCStreamer &Streamer, WinEH::FrameInfo *info) const {
639 // Switch sections (the static function above is meant to be called from
640 // here and from Emit().
641 MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
642 Streamer.SwitchSection(XData);
643 ARM64EmitUnwindInfo(Streamer, info);
644}