Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/MC/MCWin64EH.cpp
Line
Count
Source (jump to first uncovered line)
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
9
#include "llvm/MC/MCWin64EH.h"
10
#include "llvm/ADT/Twine.h"
11
#include "llvm/MC/MCContext.h"
12
#include "llvm/MC/MCExpr.h"
13
#include "llvm/MC/MCObjectFileInfo.h"
14
#include "llvm/MC/MCObjectStreamer.h"
15
#include "llvm/MC/MCSectionCOFF.h"
16
#include "llvm/MC/MCStreamer.h"
17
#include "llvm/MC/MCSymbol.h"
18
#include "llvm/Support/Win64EH.h"
19
20
using namespace llvm;
21
22
// NOTE: All relocations generated here are 4-byte image-relative.
23
24
200
static uint8_t CountOfUnwindCodes(std::vector<WinEH::Instruction> &Insns) {
25
200
  uint8_t Count = 0;
26
258
  for (const auto &I : Insns) {
27
258
    switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
28
258
    default:
29
0
      llvm_unreachable("Unsupported unwind code");
30
258
    case Win64EH::UOP_PushNonVol:
31
244
    case Win64EH::UOP_AllocSmall:
32
244
    case Win64EH::UOP_SetFPReg:
33
244
    case Win64EH::UOP_PushMachFrame:
34
244
      Count += 1;
35
244
      break;
36
244
    case Win64EH::UOP_SaveNonVol:
37
4
    case Win64EH::UOP_SaveXMM128:
38
4
      Count += 2;
39
4
      break;
40
4
    case Win64EH::UOP_SaveNonVolBig:
41
0
    case Win64EH::UOP_SaveXMM128Big:
42
0
      Count += 3;
43
0
      break;
44
10
    case Win64EH::UOP_AllocLarge:
45
10
      Count += (I.Offset > 512 * 1024 - 8) ? 
30
: 2;
46
10
      break;
47
258
    }
48
258
  }
49
200
  return Count;
50
200
}
51
52
static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
53
453
                              const MCSymbol *RHS) {
54
453
  MCContext &Context = Streamer.getContext();
55
453
  const MCExpr *Diff =
56
453
      MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
57
453
                              MCSymbolRefExpr::create(RHS, Context), Context);
58
453
  Streamer.EmitValue(Diff, 1);
59
453
}
60
61
static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
62
258
                           WinEH::Instruction &inst) {
63
258
  uint8_t b2;
64
258
  uint16_t w;
65
258
  b2 = (inst.Operation & 0x0F);
66
258
  switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
67
258
  default:
68
0
    llvm_unreachable("Unsupported unwind code");
69
258
  case Win64EH::UOP_PushNonVol:
70
57
    EmitAbsDifference(streamer, inst.Label, begin);
71
57
    b2 |= (inst.Register & 0x0F) << 4;
72
57
    streamer.EmitIntValue(b2, 1);
73
57
    break;
74
258
  case Win64EH::UOP_AllocLarge:
75
10
    EmitAbsDifference(streamer, inst.Label, begin);
76
10
    if (inst.Offset > 512 * 1024 - 8) {
77
0
      b2 |= 0x10;
78
0
      streamer.EmitIntValue(b2, 1);
79
0
      w = inst.Offset & 0xFFF8;
80
0
      streamer.EmitIntValue(w, 2);
81
0
      w = inst.Offset >> 16;
82
10
    } else {
83
10
      streamer.EmitIntValue(b2, 1);
84
10
      w = inst.Offset >> 3;
85
10
    }
86
10
    streamer.EmitIntValue(w, 2);
87
10
    break;
88
258
  case Win64EH::UOP_AllocSmall:
89
182
    b2 |= (((inst.Offset - 8) >> 3) & 0x0F) << 4;
90
182
    EmitAbsDifference(streamer, inst.Label, begin);
91
182
    streamer.EmitIntValue(b2, 1);
92
182
    break;
93
258
  case Win64EH::UOP_SetFPReg:
94
4
    EmitAbsDifference(streamer, inst.Label, begin);
95
4
    streamer.EmitIntValue(b2, 1);
96
4
    break;
97
258
  case Win64EH::UOP_SaveNonVol:
98
4
  case Win64EH::UOP_SaveXMM128:
99
4
    b2 |= (inst.Register & 0x0F) << 4;
100
4
    EmitAbsDifference(streamer, inst.Label, begin);
101
4
    streamer.EmitIntValue(b2, 1);
102
4
    w = inst.Offset >> 3;
103
4
    if (inst.Operation == Win64EH::UOP_SaveXMM128)
104
3
      w >>= 1;
105
4
    streamer.EmitIntValue(w, 2);
106
4
    break;
107
4
  case Win64EH::UOP_SaveNonVolBig:
108
0
  case Win64EH::UOP_SaveXMM128Big:
109
0
    b2 |= (inst.Register & 0x0F) << 4;
110
0
    EmitAbsDifference(streamer, inst.Label, begin);
111
0
    streamer.EmitIntValue(b2, 1);
112
0
    if (inst.Operation == Win64EH::UOP_SaveXMM128Big)
113
0
      w = inst.Offset & 0xFFF0;
114
0
    else
115
0
      w = inst.Offset & 0xFFF8;
116
0
    streamer.EmitIntValue(w, 2);
117
0
    w = inst.Offset >> 16;
118
0
    streamer.EmitIntValue(w, 2);
119
0
    break;
120
1
  case Win64EH::UOP_PushMachFrame:
121
1
    if (inst.Offset == 1)
122
1
      b2 |= 0x10;
123
1
    EmitAbsDifference(streamer, inst.Label, begin);
124
1
    streamer.EmitIntValue(b2, 1);
125
1
    break;
126
258
  }
127
258
}
128
129
static void EmitSymbolRefWithOfs(MCStreamer &streamer,
130
                                 const MCSymbol *Base,
131
415
                                 const MCSymbol *Other) {
132
415
  MCContext &Context = streamer.getContext();
133
415
  const MCSymbolRefExpr *BaseRef = MCSymbolRefExpr::create(Base, Context);
134
415
  const MCSymbolRefExpr *OtherRef = MCSymbolRefExpr::create(Other, Context);
135
415
  const MCExpr *Ofs = MCBinaryExpr::createSub(OtherRef, BaseRef, Context);
136
415
  const MCSymbolRefExpr *BaseRefRel = MCSymbolRefExpr::create(Base,
137
415
                                              MCSymbolRefExpr::VK_COFF_IMGREL32,
138
415
                                              Context);
139
415
  streamer.EmitValue(MCBinaryExpr::createAdd(BaseRefRel, Ofs, Context), 4);
140
415
}
141
142
static void EmitRuntimeFunction(MCStreamer &streamer,
143
198
                                const WinEH::FrameInfo *info) {
144
198
  MCContext &context = streamer.getContext();
145
198
146
198
  streamer.EmitValueToAlignment(4);
147
198
  EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
148
198
  EmitSymbolRefWithOfs(streamer, info->Function, info->End);
149
198
  streamer.EmitValue(MCSymbolRefExpr::create(info->Symbol,
150
198
                                             MCSymbolRefExpr::VK_COFF_IMGREL32,
151
198
                                             context), 4);
152
198
}
153
154
388
static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
155
388
  // If this UNWIND_INFO already has a symbol, it's already been emitted.
156
388
  if (info->Symbol)
157
188
    return;
158
200
159
200
  MCContext &context = streamer.getContext();
160
200
  MCSymbol *Label = context.createTempSymbol();
161
200
162
200
  streamer.EmitValueToAlignment(4);
163
200
  streamer.EmitLabel(Label);
164
200
  info->Symbol = Label;
165
200
166
200
  // Upper 3 bits are the version number (currently 1).
167
200
  uint8_t flags = 0x01;
168
200
  if (info->ChainedParent)
169
1
    flags |= Win64EH::UNW_ChainInfo << 3;
170
199
  else {
171
199
    if (info->HandlesUnwind)
172
2
      flags |= Win64EH::UNW_TerminateHandler << 3;
173
199
    if (info->HandlesExceptions)
174
4
      flags |= Win64EH::UNW_ExceptionHandler << 3;
175
199
  }
176
200
  streamer.EmitIntValue(flags, 1);
177
200
178
200
  if (info->PrologEnd)
179
195
    EmitAbsDifference(streamer, info->PrologEnd, info->Begin);
180
5
  else
181
5
    streamer.EmitIntValue(0, 1);
182
200
183
200
  uint8_t numCodes = CountOfUnwindCodes(info->Instructions);
184
200
  streamer.EmitIntValue(numCodes, 1);
185
200
186
200
  uint8_t frame = 0;
187
200
  if (info->LastFrameInst >= 0) {
188
4
    WinEH::Instruction &frameInst = info->Instructions[info->LastFrameInst];
189
4
    assert(frameInst.Operation == Win64EH::UOP_SetFPReg);
190
4
    frame = (frameInst.Register & 0x0F) | (frameInst.Offset & 0xF0);
191
4
  }
192
200
  streamer.EmitIntValue(frame, 1);
193
200
194
200
  // Emit unwind instructions (in reverse order).
195
200
  uint8_t numInst = info->Instructions.size();
196
458
  for (uint8_t c = 0; c < numInst; 
++c258
) {
197
258
    WinEH::Instruction inst = info->Instructions.back();
198
258
    info->Instructions.pop_back();
199
258
    EmitUnwindCode(streamer, info->Begin, inst);
200
258
  }
201
200
202
200
  // For alignment purposes, the instruction array will always have an even
203
200
  // number of entries, with the final entry potentially unused (in which case
204
200
  // the array will be one longer than indicated by the count of unwind codes
205
200
  // field).
206
200
  if (numCodes & 1) {
207
158
    streamer.EmitIntValue(0, 2);
208
158
  }
209
200
210
200
  if (flags & (Win64EH::UNW_ChainInfo << 3))
211
1
    EmitRuntimeFunction(streamer, info->ChainedParent);
212
199
  else if (flags &
213
199
           ((Win64EH::UNW_TerminateHandler|Win64EH::UNW_ExceptionHandler) << 3))
214
4
    streamer.EmitValue(MCSymbolRefExpr::create(info->ExceptionHandler,
215
4
                                              MCSymbolRefExpr::VK_COFF_IMGREL32,
216
4
                                              context), 4);
217
195
  else if (numCodes == 0) {
218
5
    // The minimum size of an UNWIND_INFO struct is 8 bytes. If we're not
219
5
    // a chained unwind info, if there is no handler, and if there are fewer
220
5
    // than 2 slots used in the unwind code array, we have to pad to 8 bytes.
221
5
    streamer.EmitIntValue(0, 4);
222
5
  }
223
200
}
224
225
77
void llvm::Win64EH::UnwindEmitter::Emit(MCStreamer &Streamer) const {
226
77
  // Emit the unwind info structs first.
227
197
  for (const auto &CFI : Streamer.getWinFrameInfos()) {
228
197
    MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
229
197
    Streamer.SwitchSection(XData);
230
197
    ::EmitUnwindInfo(Streamer, CFI.get());
231
197
  }
232
77
233
77
  // Now emit RUNTIME_FUNCTION entries.
234
197
  for (const auto &CFI : Streamer.getWinFrameInfos()) {
235
197
    MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
236
197
    Streamer.SwitchSection(PData);
237
197
    EmitRuntimeFunction(Streamer, CFI.get());
238
197
  }
239
77
}
240
241
void llvm::Win64EH::UnwindEmitter::EmitUnwindInfo(
242
191
    MCStreamer &Streamer, WinEH::FrameInfo *info) const {
243
191
  // Switch sections (the static function above is meant to be called from
244
191
  // here and from Emit().
245
191
  MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
246
191
  Streamer.SwitchSection(XData);
247
191
248
191
  ::EmitUnwindInfo(Streamer, info);
249
191
}
250
251
static int64_t GetAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS,
252
34
                                const MCSymbol *RHS) {
253
34
  MCContext &Context = Streamer.getContext();
254
34
  const MCExpr *Diff =
255
34
      MCBinaryExpr::createSub(MCSymbolRefExpr::create(LHS, Context),
256
34
                              MCSymbolRefExpr::create(RHS, Context), Context);
257
34
  MCObjectStreamer *OS = (MCObjectStreamer *)(&Streamer);
258
34
  // It should normally be possible to calculate the length of a function
259
34
  // at this point, but it might not be possible in the presence of certain
260
34
  // unusual constructs, like an inline asm with an alignment directive.
261
34
  int64_t value;
262
34
  if (!Diff->evaluateAsAbsolute(value, OS->getAssembler()))
263
0
    report_fatal_error("Failed to evaluate function length in SEH unwind info");
264
34
  return value;
265
34
}
266
267
static uint32_t
268
37
ARM64CountOfUnwindCodes(const std::vector<WinEH::Instruction> &Insns) {
269
37
  uint32_t Count = 0;
270
173
  for (const auto &I : Insns) {
271
173
    switch (static_cast<Win64EH::UnwindOpcodes>(I.Operation)) {
272
173
    default:
273
0
      llvm_unreachable("Unsupported ARM64 unwind code");
274
173
    case Win64EH::UOP_AllocSmall:
275
6
      Count += 1;
276
6
      break;
277
173
    case Win64EH::UOP_AllocMedium:
278
3
      Count += 2;
279
3
      break;
280
173
    case Win64EH::UOP_AllocLarge:
281
2
      Count += 4;
282
2
      break;
283
173
    case Win64EH::UOP_SaveFPLRX:
284
2
      Count += 1;
285
2
      break;
286
173
    case Win64EH::UOP_SaveFPLR:
287
8
      Count += 1;
288
8
      break;
289
173
    case Win64EH::UOP_SaveReg:
290
12
      Count += 2;
291
12
      break;
292
173
    case Win64EH::UOP_SaveRegP:
293
61
      Count += 2;
294
61
      break;
295
173
    case Win64EH::UOP_SaveRegPX:
296
4
      Count += 2;
297
4
      break;
298
173
    case Win64EH::UOP_SaveRegX:
299
10
      Count += 2;
300
10
      break;
301
173
    case Win64EH::UOP_SaveFReg:
302
4
      Count += 2;
303
4
      break;
304
173
    case Win64EH::UOP_SaveFRegP:
305
10
      Count += 2;
306
10
      break;
307
173
    case Win64EH::UOP_SaveFRegX:
308
2
      Count += 2;
309
2
      break;
310
173
    case Win64EH::UOP_SaveFRegPX:
311
8
      Count += 2;
312
8
      break;
313
173
    case Win64EH::UOP_SetFP:
314
2
      Count += 1;
315
2
      break;
316
173
    case Win64EH::UOP_AddFP:
317
3
      Count += 2;
318
3
      break;
319
173
    case Win64EH::UOP_Nop:
320
4
      Count += 1;
321
4
      break;
322
173
    case Win64EH::UOP_End:
323
32
      Count += 1;
324
32
      break;
325
173
    }
326
173
  }
327
37
  return Count;
328
37
}
329
330
// Unwind opcode encodings and restrictions are documented at
331
// https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
332
static void ARM64EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin,
333
165
                                WinEH::Instruction &inst) {
334
165
  uint8_t b, reg;
335
165
  switch (static_cast<Win64EH::UnwindOpcodes>(inst.Operation)) {
336
165
  default:
337
0
    llvm_unreachable("Unsupported ARM64 unwind code");
338
165
  case Win64EH::UOP_AllocSmall:
339
6
    b = (inst.Offset >> 4) & 0x1F;
340
6
    streamer.EmitIntValue(b, 1);
341
6
    break;
342
165
  case Win64EH::UOP_AllocMedium: {
343
3
    uint16_t hw = (inst.Offset >> 4) & 0x7FF;
344
3
    b = 0xC0;
345
3
    b |= (hw >> 8);
346
3
    streamer.EmitIntValue(b, 1);
347
3
    b = hw & 0xFF;
348
3
    streamer.EmitIntValue(b, 1);
349
3
    break;
350
165
  }
351
165
  case Win64EH::UOP_AllocLarge: {
352
2
    uint32_t w;
353
2
    b = 0xE0;
354
2
    streamer.EmitIntValue(b, 1);
355
2
    w = inst.Offset >> 4;
356
2
    b = (w & 0x00FF0000) >> 16;
357
2
    streamer.EmitIntValue(b, 1);
358
2
    b = (w & 0x0000FF00) >> 8;
359
2
    streamer.EmitIntValue(b, 1);
360
2
    b = w & 0x000000FF;
361
2
    streamer.EmitIntValue(b, 1);
362
2
    break;
363
165
  }
364
165
  case Win64EH::UOP_SetFP:
365
2
    b = 0xE1;
366
2
    streamer.EmitIntValue(b, 1);
367
2
    break;
368
165
  case Win64EH::UOP_AddFP:
369
3
    b = 0xE2;
370
3
    streamer.EmitIntValue(b, 1);
371
3
    b = (inst.Offset >> 3);
372
3
    streamer.EmitIntValue(b, 1);
373
3
    break;
374
165
  case Win64EH::UOP_Nop:
375
4
    b = 0xE3;
376
4
    streamer.EmitIntValue(b, 1);
377
4
    break;
378
165
  case Win64EH::UOP_SaveFPLRX:
379
2
    b = 0x80;
380
2
    b |= ((inst.Offset - 1) >> 3) & 0x3F;
381
2
    streamer.EmitIntValue(b, 1);
382
2
    break;
383
165
  case Win64EH::UOP_SaveFPLR:
384
8
    b = 0x40;
385
8
    b |= (inst.Offset >> 3) & 0x3F;
386
8
    streamer.EmitIntValue(b, 1);
387
8
    break;
388
165
  case Win64EH::UOP_SaveReg:
389
12
    assert(inst.Register >= 19 && "Saved reg must be >= 19");
390
12
    reg = inst.Register - 19;
391
12
    b = 0xD0 | ((reg & 0xC) >> 2);
392
12
    streamer.EmitIntValue(b, 1);
393
12
    b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
394
12
    streamer.EmitIntValue(b, 1);
395
12
    break;
396
165
  case Win64EH::UOP_SaveRegX:
397
10
    assert(inst.Register >= 19 && "Saved reg must be >= 19");
398
10
    reg = inst.Register - 19;
399
10
    b = 0xD4 | ((reg & 0x8) >> 3);
400
10
    streamer.EmitIntValue(b, 1);
401
10
    b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1);
402
10
    streamer.EmitIntValue(b, 1);
403
10
    break;
404
165
  case Win64EH::UOP_SaveRegP:
405
56
    assert(inst.Register >= 19 && "Saved registers must be >= 19");
406
56
    reg = inst.Register - 19;
407
56
    b = 0xC8 | ((reg & 0xC) >> 2);
408
56
    streamer.EmitIntValue(b, 1);
409
56
    b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
410
56
    streamer.EmitIntValue(b, 1);
411
56
    break;
412
165
  case Win64EH::UOP_SaveRegPX:
413
4
    assert(inst.Register >= 19 && "Saved registers must be >= 19");
414
4
    reg = inst.Register - 19;
415
4
    b = 0xCC | ((reg & 0xC) >> 2);
416
4
    streamer.EmitIntValue(b, 1);
417
4
    b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1);
418
4
    streamer.EmitIntValue(b, 1);
419
4
    break;
420
165
  case Win64EH::UOP_SaveFReg:
421
4
    assert(inst.Register >= 8 && "Saved dreg must be >= 8");
422
4
    reg = inst.Register - 8;
423
4
    b = 0xDC | ((reg & 0x4) >> 2);
424
4
    streamer.EmitIntValue(b, 1);
425
4
    b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
426
4
    streamer.EmitIntValue(b, 1);
427
4
    break;
428
165
  case Win64EH::UOP_SaveFRegX:
429
2
    assert(inst.Register >= 8 && "Saved dreg must be >= 8");
430
2
    reg = inst.Register - 8;
431
2
    b = 0xDE;
432
2
    streamer.EmitIntValue(b, 1);
433
2
    b = ((reg & 0x7) << 5) | ((inst.Offset >> 3) - 1);
434
2
    streamer.EmitIntValue(b, 1);
435
2
    break;
436
165
  case Win64EH::UOP_SaveFRegP:
437
9
    assert(inst.Register >= 8 && "Saved dregs must be >= 8");
438
9
    reg = inst.Register - 8;
439
9
    b = 0xD8 | ((reg & 0x4) >> 2);
440
9
    streamer.EmitIntValue(b, 1);
441
9
    b = ((reg & 0x3) << 6) | (inst.Offset >> 3);
442
9
    streamer.EmitIntValue(b, 1);
443
9
    break;
444
165
  case Win64EH::UOP_SaveFRegPX:
445
7
    assert(inst.Register >= 8 && "Saved dregs must be >= 8");
446
7
    reg = inst.Register - 8;
447
7
    b = 0xDA | ((reg & 0x4) >> 2);
448
7
    streamer.EmitIntValue(b, 1);
449
7
    b = ((reg & 0x3) << 6) | ((inst.Offset >> 3) - 1);
450
7
    streamer.EmitIntValue(b, 1);
451
7
    break;
452
165
  case Win64EH::UOP_End:
453
31
    b = 0xE4;
454
31
    streamer.EmitIntValue(b, 1);
455
31
    break;
456
165
  }
457
165
}
458
459
// Returns the epilog symbol of an epilog with the exact same unwind code
460
// sequence, if it exists.  Otherwise, returns nulltpr.
461
// EpilogInstrs - Unwind codes for the current epilog.
462
// Epilogs - Epilogs that potentialy match the current epilog.
463
static MCSymbol*
464
FindMatchingEpilog(const std::vector<WinEH::Instruction>& EpilogInstrs,
465
                   const std::vector<MCSymbol *>& Epilogs,
466
18
                   const WinEH::FrameInfo *info) {
467
18
  for (auto *EpilogStart : Epilogs) {
468
2
    auto InstrsIter = info->EpilogMap.find(EpilogStart);
469
2
    assert(InstrsIter != info->EpilogMap.end() &&
470
2
           "Epilog not found in EpilogMap");
471
2
    const auto &Instrs = InstrsIter->second;
472
2
473
2
    if (Instrs.size() != EpilogInstrs.size())
474
1
      continue;
475
1
476
1
    bool Match = true;
477
9
    for (unsigned i = 0; i < Instrs.size(); 
++i8
)
478
8
      if (Instrs[i].Operation != EpilogInstrs[i].Operation ||
479
8
          Instrs[i].Offset != EpilogInstrs[i].Offset ||
480
8
          Instrs[i].Register != EpilogInstrs[i].Register) {
481
0
         Match = false;
482
0
         break;
483
0
      }
484
1
485
1
    if (Match)
486
1
      return EpilogStart;
487
1
  }
488
18
  
return nullptr17
;
489
18
}
490
491
// Populate the .xdata section.  The format of .xdata on ARM64 is documented at
492
// https://docs.microsoft.com/en-us/cpp/build/arm64-exception-handling
493
36
static void ARM64EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info) {
494
36
  // If this UNWIND_INFO already has a symbol, it's already been emitted.
495
36
  if (info->Symbol)
496
17
    return;
497
19
498
19
  MCContext &context = streamer.getContext();
499
19
  MCSymbol *Label = context.createTempSymbol();
500
19
501
19
  streamer.EmitValueToAlignment(4);
502
19
  streamer.EmitLabel(Label);
503
19
  info->Symbol = Label;
504
19
505
19
  int64_t RawFuncLength;
506
19
  if (!info->FuncletOrFuncEnd) {
507
3
    // FIXME: This is very wrong; we emit SEH data which covers zero bytes
508
3
    // of code. But otherwise test/MC/AArch64/seh.s crashes.
509
3
    RawFuncLength = 0;
510
16
  } else {
511
16
    // FIXME: GetAbsDifference tries to compute the length of the function
512
16
    // immediately, before the whole file is emitted, but in general
513
16
    // that's impossible: the size in bytes of certain assembler directives
514
16
    // like .align and .fill is not known until the whole file is parsed and
515
16
    // relaxations are applied. Currently, GetAbsDifference fails with a fatal
516
16
    // error in that case. (We mostly don't hit this because inline assembly
517
16
    // specifying those directives is rare, and we don't normally try to
518
16
    // align loops on AArch64.)
519
16
    //
520
16
    // There are two potential approaches to delaying the computation. One,
521
16
    // we could emit something like ".word (endfunc-beginfunc)/4+0x10800000",
522
16
    // as long as we have some conservative estimate we could use to prove
523
16
    // that we don't need to split the unwind data. Emitting the constant
524
16
    // is straightforward, but there's no existing code for estimating the
525
16
    // size of the function.
526
16
    //
527
16
    // The other approach would be to use a dedicated, relaxable fragment,
528
16
    // which could grow to accommodate splitting the unwind data if
529
16
    // necessary. This is more straightforward, since it automatically works
530
16
    // without any new infrastructure, and it's consistent with how we handle
531
16
    // relaxation in other contexts.  But it would require some refactoring
532
16
    // to move parts of the pdata/xdata emission into the implementation of
533
16
    // a fragment. We could probably continue to encode the unwind codes
534
16
    // here, but we'd have to emit the pdata, the xdata header, and the
535
16
    // epilogue scopes later, since they depend on whether the we need to
536
16
    // split the unwind data.
537
16
    RawFuncLength = GetAbsDifference(streamer, info->FuncletOrFuncEnd,
538
16
                                     info->Begin);
539
16
  }
540
19
  if (RawFuncLength > 0xFFFFF)
541
0
    report_fatal_error("SEH unwind data splitting not yet implemented");
542
19
  uint32_t FuncLength = (uint32_t)RawFuncLength / 4;
543
19
  uint32_t PrologCodeBytes = ARM64CountOfUnwindCodes(info->Instructions);
544
19
  uint32_t TotalCodeBytes = PrologCodeBytes;
545
19
546
19
  // Process epilogs.
547
19
  MapVector<MCSymbol *, uint32_t> EpilogInfo;
548
19
  // Epilogs processed so far.
549
19
  std::vector<MCSymbol *> AddedEpilogs;
550
19
551
19
  for (auto &I : info->EpilogMap) {
552
18
    MCSymbol *EpilogStart = I.first;
553
18
    auto &EpilogInstrs = I.second;
554
18
    uint32_t CodeBytes = ARM64CountOfUnwindCodes(EpilogInstrs);
555
18
556
18
    MCSymbol* MatchingEpilog =
557
18
      FindMatchingEpilog(EpilogInstrs, AddedEpilogs, info);
558
18
    if (MatchingEpilog) {
559
1
      assert(EpilogInfo.find(MatchingEpilog) != EpilogInfo.end() &&
560
1
             "Duplicate epilog not found");
561
1
      EpilogInfo[EpilogStart] = EpilogInfo.lookup(MatchingEpilog);
562
1
      // Clear the unwind codes in the EpilogMap, so that they don't get output
563
1
      // in the logic below.
564
1
      EpilogInstrs.clear();
565
17
    } else {
566
17
      EpilogInfo[EpilogStart] = TotalCodeBytes;
567
17
      TotalCodeBytes += CodeBytes;
568
17
      AddedEpilogs.push_back(EpilogStart);
569
17
    }
570
18
  }
571
19
572
19
  // Code Words, Epilog count, E, X, Vers, Function Length
573
19
  uint32_t row1 = 0x0;
574
19
  uint32_t CodeWords = TotalCodeBytes / 4;
575
19
  uint32_t CodeWordsMod = TotalCodeBytes % 4;
576
19
  if (CodeWordsMod)
577
12
    CodeWords++;
578
19
  uint32_t EpilogCount = info->EpilogMap.size();
579
19
  bool ExtensionWord = EpilogCount > 31 || TotalCodeBytes > 124;
580
19
  if (!ExtensionWord) {
581
19
    row1 |= (EpilogCount & 0x1F) << 22;
582
19
    row1 |= (CodeWords & 0x1F) << 27;
583
19
  }
584
19
  // E is always 0 right now, TODO: packed epilog setup
585
19
  if (info->HandlesExceptions) // X
586
4
    row1 |= 1 << 20;
587
19
  row1 |= FuncLength & 0x3FFFF;
588
19
  streamer.EmitIntValue(row1, 4);
589
19
590
19
  // Extended Code Words, Extended Epilog Count
591
19
  if (ExtensionWord) {
592
0
    // FIXME: We should be able to split unwind info into multiple sections.
593
0
    // FIXME: We should share epilog codes across epilogs, where possible,
594
0
    // which would make this issue show up less frequently.
595
0
    if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
596
0
      report_fatal_error("SEH unwind data splitting not yet implemented");
597
0
    uint32_t row2 = 0x0;
598
0
    row2 |= (CodeWords & 0xFF) << 16;
599
0
    row2 |= (EpilogCount & 0xFFFF);
600
0
    streamer.EmitIntValue(row2, 4);
601
0
  }
602
19
603
19
  // Epilog Start Index, Epilog Start Offset
604
19
  for (auto &I : EpilogInfo) {
605
18
    MCSymbol *EpilogStart = I.first;
606
18
    uint32_t EpilogIndex = I.second;
607
18
    uint32_t EpilogOffset =
608
18
        (uint32_t)GetAbsDifference(streamer, EpilogStart, info->Begin);
609
18
    if (EpilogOffset)
610
16
      EpilogOffset /= 4;
611
18
    uint32_t row3 = EpilogOffset;
612
18
    row3 |= (EpilogIndex & 0x3FF) << 22;
613
18
    streamer.EmitIntValue(row3, 4);
614
18
  }
615
19
616
19
  // Emit prolog unwind instructions (in reverse order).
617
19
  uint8_t numInst = info->Instructions.size();
618
99
  for (uint8_t c = 0; c < numInst; 
++c80
) {
619
80
    WinEH::Instruction inst = info->Instructions.back();
620
80
    info->Instructions.pop_back();
621
80
    ARM64EmitUnwindCode(streamer, info->Begin, inst);
622
80
  }
623
19
624
19
  // Emit epilog unwind instructions
625
19
  for (auto &I : info->EpilogMap) {
626
18
    auto &EpilogInstrs = I.second;
627
103
    for (uint32_t i = 0; i < EpilogInstrs.size(); 
i++85
) {
628
85
      WinEH::Instruction inst = EpilogInstrs[i];
629
85
      ARM64EmitUnwindCode(streamer, info->Begin, inst);
630
85
    }
631
18
  }
632
19
633
19
  int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
634
19
  assert(BytesMod >= 0);
635
42
  for (int i = 0; i < BytesMod; 
i++23
)
636
23
    streamer.EmitIntValue(0xE3, 1);
637
19
638
19
  if (info->HandlesExceptions)
639
4
    streamer.EmitValue(
640
4
        MCSymbolRefExpr::create(info->ExceptionHandler,
641
4
                                MCSymbolRefExpr::VK_COFF_IMGREL32, context),
642
4
        4);
643
19
}
644
645
static void ARM64EmitRuntimeFunction(MCStreamer &streamer,
646
19
                                     const WinEH::FrameInfo *info) {
647
19
  MCContext &context = streamer.getContext();
648
19
649
19
  streamer.EmitValueToAlignment(4);
650
19
  EmitSymbolRefWithOfs(streamer, info->Function, info->Begin);
651
19
  streamer.EmitValue(MCSymbolRefExpr::create(info->Symbol,
652
19
                                             MCSymbolRefExpr::VK_COFF_IMGREL32,
653
19
                                             context),
654
19
                     4);
655
19
}
656
657
14
void llvm::Win64EH::ARM64UnwindEmitter::Emit(MCStreamer &Streamer) const {
658
14
  // Emit the unwind info structs first.
659
19
  for (const auto &CFI : Streamer.getWinFrameInfos()) {
660
19
    MCSection *XData = Streamer.getAssociatedXDataSection(CFI->TextSection);
661
19
    Streamer.SwitchSection(XData);
662
19
    ARM64EmitUnwindInfo(Streamer, CFI.get());
663
19
  }
664
14
665
14
  // Now emit RUNTIME_FUNCTION entries.
666
19
  for (const auto &CFI : Streamer.getWinFrameInfos()) {
667
19
    MCSection *PData = Streamer.getAssociatedPDataSection(CFI->TextSection);
668
19
    Streamer.SwitchSection(PData);
669
19
    ARM64EmitRuntimeFunction(Streamer, CFI.get());
670
19
  }
671
14
}
672
673
void llvm::Win64EH::ARM64UnwindEmitter::EmitUnwindInfo(
674
17
    MCStreamer &Streamer, WinEH::FrameInfo *info) const {
675
17
  // Switch sections (the static function above is meant to be called from
676
17
  // here and from Emit().
677
17
  MCSection *XData = Streamer.getAssociatedXDataSection(info->TextSection);
678
17
  Streamer.SwitchSection(XData);
679
17
  ARM64EmitUnwindInfo(Streamer, info);
680
17
}