-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathEhFrame.cpp
228 lines (188 loc) · 4.84 KB
/
EhFrame.cpp
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "Backend.h"
#include "EhFrame.h"
// AMD64 ABI -- DWARF register number mapping
static const ubyte DWARF_RegNum[] =
{
// Exactly same order as RegList.h!
-1, // NOREG,
0, // RAX,
2, // RCX,
1, // RDX,
3, // RBX,
7, // RSP,
6, // RBP,
4, // RSI,
5, // RDI,
8, // R8,
9, // R9,
10, // R10,
11, // R11,
12, // R12,
13, // R13,
14, // R14,
15, // R15,
17, // XMM0,
18, // XMM1,
19, // XMM2,
20, // XMM3,
21, // XMM4,
22, // XMM5,
23, // XMM6,
24, // XMM7,
25, // XMM8,
26, // XMM9,
27, // XMM10,
28, // XMM11,
29, // XMM12,
30, // XMM13,
31, // XMM14,
32, // XMM15,
};
static const ubyte DWARF_RegRA = 16;
ubyte GetDwarfRegNum(ubyte regNum)
{
return DWARF_RegNum[regNum];
}
// Encode into ULEB128 (Unsigned Little Endian Base 128)
BYTE* EmitLEB128(BYTE* pc, unsigned value)
{
do
{
BYTE b = value & 0x7F; // low order 7 bits
value >>= 7;
if (value) // more bytes to come
{
b |= 0x80;
}
*pc++ = b;
}
while (value != 0);
return pc;
}
// Encode into signed LEB128 (Signed Little Endian Base 128)
BYTE* EmitLEB128(BYTE* pc, int value)
{
static const int size = sizeof(value) * 8;
static const bool isLogicShift = (-1 >> 1) != -1;
const bool signExtend = isLogicShift && value < 0;
bool more = true;
while (more)
{
BYTE b = value & 0x7F; // low order 7 bits
value >>= 7;
if (signExtend)
{
value |= - (1 << (size - 7)); // sign extend
}
const bool signBit = (b & 0x40) != 0;
if ((value == 0 && !signBit) || (value == -1 && signBit))
{
more = false;
}
else
{
b |= 0x80;
}
*pc++ = b;
}
return pc;
}
void EhFrame::Entry::Begin()
{
Assert(beginOffset == -1);
beginOffset = writer->Count();
// Write Length place holder
const uword length = 0;
writer->Write(length);
}
void EhFrame::Entry::End()
{
Assert(beginOffset != -1); // Must have called Begin()
// padding
size_t padding = (MachPtr - writer->Count() % MachPtr) % MachPtr;
for (size_t i = 0; i < padding; i++)
{
cfi_nop();
}
// update length record
uword length = writer->Count() - beginOffset
- sizeof(length); // exclude length itself
writer->Write(beginOffset, length);
}
void EhFrame::Entry::cfi_advance(uword advance)
{
if (advance <= 0x3F) // 6-bits
{
cfi_advance_loc(static_cast<ubyte>(advance));
}
else if (advance <= 0xFF) // 1-byte
{
cfi_advance_loc1(static_cast<ubyte>(advance));
}
else if (advance <= 0xFFFF) // 2-byte
{
cfi_advance_loc2(static_cast<uword>(advance));
}
else // 4-byte
{
cfi_advance_loc4(advance);
}
}
void EhFrame::CIE::Begin()
{
Assert(writer->Count() == 0);
Entry::Begin();
const uword cie_id = 0;
Emit(cie_id);
const ubyte version = 1;
Emit(version);
const ubyte augmentationString = 0; // none
Emit(augmentationString);
const ULEB128 codeAlignmentFactor = 1;
Emit(codeAlignmentFactor);
const LEB128 dataAlignmentFactor = - MachPtr;
Emit(dataAlignmentFactor);
const ubyte returnAddressRegister = DWARF_RegRA;
Emit(returnAddressRegister);
}
void EhFrame::FDE::Begin()
{
Entry::Begin();
const uword cie_id = writer->Count();
Emit(cie_id);
// Write pc <begin, range> placeholder
pcBeginOffset = writer->Count();
const void* pc = nullptr;
Emit(pc);
Emit(pc);
}
void EhFrame::FDE::UpdateAddressRange(const void* pcBegin, size_t pcRange)
{
writer->Write(pcBeginOffset, pcBegin);
writer->Write(pcBeginOffset + sizeof(pcBegin),
reinterpret_cast<const void*>(pcRange));
}
EhFrame::EhFrame(BYTE* buffer, size_t size)
: writer(buffer, size), fde(&writer)
{
CIE cie(&writer);
cie.Begin();
// CIE initial instructions
// DW_CFA_def_cfa: r7 (rsp) ofs 8
cie.cfi_def_cfa(DWARF_RegNum[LowererMDArch::GetRegStackPointer()], MachPtr);
// DW_CFA_offset: r16 (rip) at cfa-8 (data alignment -8)
cie.cfi_offset(DWARF_RegRA, 1);
cie.End();
fde.Begin();
}
void EhFrame::End()
{
fde.End();
// Write length 0 to mark terminate entry
const uword terminate_entry_length = 0;
writer.Write(terminate_entry_length);
}