1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
//===-- ARMWinCOFFStreamer.cpp - ARM Target WinCOFF Streamer ----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "ARMMCTargetDesc.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCCodeEmitter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCWin64EH.h"
#include "llvm/MC/MCWinCOFFStreamer.h"
using namespace llvm;
namespace {
class ARMWinCOFFStreamer : public MCWinCOFFStreamer {
Win64EH::ARMUnwindEmitter EHStreamer;
public:
ARMWinCOFFStreamer(MCContext &C, std::unique_ptr<MCAsmBackend> AB,
std::unique_ptr<MCCodeEmitter> CE,
std::unique_ptr<MCObjectWriter> OW)
: MCWinCOFFStreamer(C, std::move(AB), std::move(CE), std::move(OW)) {}
void emitWinEHHandlerData(SMLoc Loc) override;
void emitWindowsUnwindTables() override;
void emitWindowsUnwindTables(WinEH::FrameInfo *Frame) override;
void emitThumbFunc(MCSymbol *Symbol) override;
void finishImpl() override;
};
void ARMWinCOFFStreamer::emitWinEHHandlerData(SMLoc Loc) {
MCStreamer::emitWinEHHandlerData(Loc);
// We have to emit the unwind info now, because this directive
// actually switches to the .xdata section!
EHStreamer.EmitUnwindInfo(*this, getCurrentWinFrameInfo(),
/* HandlerData = */ true);
}
void ARMWinCOFFStreamer::emitWindowsUnwindTables(WinEH::FrameInfo *Frame) {
EHStreamer.EmitUnwindInfo(*this, Frame, /* HandlerData = */ false);
}
void ARMWinCOFFStreamer::emitWindowsUnwindTables() {
if (!getNumWinFrameInfos())
return;
EHStreamer.Emit(*this);
}
void ARMWinCOFFStreamer::emitThumbFunc(MCSymbol *Symbol) {
getAssembler().setIsThumbFunc(Symbol);
}
void ARMWinCOFFStreamer::finishImpl() {
emitFrames(nullptr);
emitWindowsUnwindTables();
MCWinCOFFStreamer::finishImpl();
}
}
MCStreamer *llvm::createARMWinCOFFStreamer(
MCContext &Context, std::unique_ptr<MCAsmBackend> &&MAB,
std::unique_ptr<MCObjectWriter> &&OW,
std::unique_ptr<MCCodeEmitter> &&Emitter, bool RelaxAll,
bool IncrementalLinkerCompatible) {
auto *S = new ARMWinCOFFStreamer(Context, std::move(MAB), std::move(Emitter),
std::move(OW));
S->getAssembler().setIncrementalLinkerCompatible(IncrementalLinkerCompatible);
return S;
}
namespace {
class ARMTargetWinCOFFStreamer : public llvm::ARMTargetStreamer {
private:
// True if we are processing SEH directives in an epilogue.
bool InEpilogCFI = false;
// Symbol of the current epilog for which we are processing SEH directives.
MCSymbol *CurrentEpilog = nullptr;
public:
ARMTargetWinCOFFStreamer(llvm::MCStreamer &S) : ARMTargetStreamer(S) {}
// The unwind codes on ARM Windows are documented at
// https://docs.microsoft.com/en-us/cpp/build/arm-exception-handling
void emitARMWinCFIAllocStack(unsigned Size, bool Wide) override;
void emitARMWinCFISaveRegMask(unsigned Mask, bool Wide) override;
void emitARMWinCFISaveSP(unsigned Reg) override;
void emitARMWinCFISaveFRegs(unsigned First, unsigned Last) override;
void emitARMWinCFISaveLR(unsigned Offset) override;
void emitARMWinCFIPrologEnd(bool Fragment) override;
void emitARMWinCFINop(bool Wide) override;
void emitARMWinCFIEpilogStart(unsigned Condition) override;
void emitARMWinCFIEpilogEnd() override;
void emitARMWinCFICustom(unsigned Opcode) override;
private:
void emitARMWinUnwindCode(unsigned UnwindCode, int Reg, int Offset);
};
// Helper function to common out unwind code setup for those codes that can
// belong to both prolog and epilog.
void ARMTargetWinCOFFStreamer::emitARMWinUnwindCode(unsigned UnwindCode,
int Reg, int Offset) {
auto &S = getStreamer();
WinEH::FrameInfo *CurFrame = S.EnsureValidWinFrameInfo(SMLoc());
if (!CurFrame)
return;
MCSymbol *Label = S.emitCFILabel();
auto Inst = WinEH::Instruction(UnwindCode, Label, Reg, Offset);
if (InEpilogCFI)
CurFrame->EpilogMap[CurrentEpilog].Instructions.push_back(Inst);
else
CurFrame->Instructions.push_back(Inst);
}
void ARMTargetWinCOFFStreamer::emitARMWinCFIAllocStack(unsigned Size,
bool Wide) {
unsigned Op = Win64EH::UOP_AllocSmall;
if (!Wide) {
if (Size / 4 > 0xffff)
Op = Win64EH::UOP_AllocHuge;
else if (Size / 4 > 0x7f)
Op = Win64EH::UOP_AllocLarge;
} else {
Op = Win64EH::UOP_WideAllocMedium;
if (Size / 4 > 0xffff)
Op = Win64EH::UOP_WideAllocHuge;
else if (Size / 4 > 0x3ff)
Op = Win64EH::UOP_WideAllocLarge;
}
emitARMWinUnwindCode(Op, -1, Size);
}
void ARMTargetWinCOFFStreamer::emitARMWinCFISaveRegMask(unsigned Mask,
bool Wide) {
assert(Mask != 0);
int Lr = (Mask & 0x4000) ? 1 : 0;
Mask &= ~0x4000;
if (Wide)
assert((Mask & ~0x1fff) == 0);
else
assert((Mask & ~0x00ff) == 0);
if (Mask && ((Mask + (1 << 4)) & Mask) == 0) {
if (Wide && (Mask & 0x1000) == 0 && (Mask & 0xff) == 0xf0) {
// One continuous range from r4 to r8-r11
for (int I = 11; I >= 8; I--) {
if (Mask & (1 << I)) {
emitARMWinUnwindCode(Win64EH::UOP_WideSaveRegsR4R11LR, I, Lr);
return;
}
}
// If it actually was from r4 to r4-r7, continue below.
} else if (!Wide) {
// One continuous range from r4 to r4-r7
for (int I = 7; I >= 4; I--) {
if (Mask & (1 << I)) {
emitARMWinUnwindCode(Win64EH::UOP_SaveRegsR4R7LR, I, Lr);
return;
}
}
llvm_unreachable("logic error");
}
}
Mask |= Lr << 14;
if (Wide)
emitARMWinUnwindCode(Win64EH::UOP_WideSaveRegMask, Mask, 0);
else
emitARMWinUnwindCode(Win64EH::UOP_SaveRegMask, Mask, 0);
}
void ARMTargetWinCOFFStreamer::emitARMWinCFISaveSP(unsigned Reg) {
emitARMWinUnwindCode(Win64EH::UOP_SaveSP, Reg, 0);
}
void ARMTargetWinCOFFStreamer::emitARMWinCFISaveFRegs(unsigned First,
unsigned Last) {
assert(First <= Last);
assert(First >= 16 || Last < 16);
assert(First <= 31 && Last <= 31);
if (First == 8)
emitARMWinUnwindCode(Win64EH::UOP_SaveFRegD8D15, Last, 0);
else if (First <= 15)
emitARMWinUnwindCode(Win64EH::UOP_SaveFRegD0D15, First, Last);
else
emitARMWinUnwindCode(Win64EH::UOP_SaveFRegD16D31, First, Last);
}
void ARMTargetWinCOFFStreamer::emitARMWinCFISaveLR(unsigned Offset) {
emitARMWinUnwindCode(Win64EH::UOP_SaveLR, 0, Offset);
}
void ARMTargetWinCOFFStreamer::emitARMWinCFINop(bool Wide) {
if (Wide)
emitARMWinUnwindCode(Win64EH::UOP_WideNop, -1, 0);
else
emitARMWinUnwindCode(Win64EH::UOP_Nop, -1, 0);
}
void ARMTargetWinCOFFStreamer::emitARMWinCFIPrologEnd(bool Fragment) {
auto &S = getStreamer();
WinEH::FrameInfo *CurFrame = S.EnsureValidWinFrameInfo(SMLoc());
if (!CurFrame)
return;
MCSymbol *Label = S.emitCFILabel();
CurFrame->PrologEnd = Label;
WinEH::Instruction Inst =
WinEH::Instruction(Win64EH::UOP_End, /*Label=*/nullptr, -1, 0);
auto it = CurFrame->Instructions.begin();
CurFrame->Instructions.insert(it, Inst);
CurFrame->Fragment = Fragment;
}
void ARMTargetWinCOFFStreamer::emitARMWinCFIEpilogStart(unsigned Condition) {
auto &S = getStreamer();
WinEH::FrameInfo *CurFrame = S.EnsureValidWinFrameInfo(SMLoc());
if (!CurFrame)
return;
InEpilogCFI = true;
CurrentEpilog = S.emitCFILabel();
CurFrame->EpilogMap[CurrentEpilog].Condition = Condition;
}
void ARMTargetWinCOFFStreamer::emitARMWinCFIEpilogEnd() {
auto &S = getStreamer();
WinEH::FrameInfo *CurFrame = S.EnsureValidWinFrameInfo(SMLoc());
if (!CurFrame)
return;
if (!CurrentEpilog) {
S.getContext().reportError(SMLoc(), "Stray .seh_endepilogue in " +
CurFrame->Function->getName());
return;
}
std::vector<WinEH::Instruction> &Epilog =
CurFrame->EpilogMap[CurrentEpilog].Instructions;
unsigned UnwindCode = Win64EH::UOP_End;
if (!Epilog.empty()) {
WinEH::Instruction EndInstr = Epilog.back();
if (EndInstr.Operation == Win64EH::UOP_Nop) {
UnwindCode = Win64EH::UOP_EndNop;
Epilog.pop_back();
} else if (EndInstr.Operation == Win64EH::UOP_WideNop) {
UnwindCode = Win64EH::UOP_WideEndNop;
Epilog.pop_back();
}
}
InEpilogCFI = false;
WinEH::Instruction Inst = WinEH::Instruction(UnwindCode, nullptr, -1, 0);
CurFrame->EpilogMap[CurrentEpilog].Instructions.push_back(Inst);
MCSymbol *Label = S.emitCFILabel();
CurFrame->EpilogMap[CurrentEpilog].End = Label;
CurrentEpilog = nullptr;
}
void ARMTargetWinCOFFStreamer::emitARMWinCFICustom(unsigned Opcode) {
emitARMWinUnwindCode(Win64EH::UOP_Custom, 0, Opcode);
}
} // end anonymous namespace
MCTargetStreamer *llvm::createARMObjectTargetWinCOFFStreamer(MCStreamer &S) {
return new ARMTargetWinCOFFStreamer(S);
}
|