-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
pdata.d
294 lines (247 loc) · 8.4 KB
/
pdata.d
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* Compiler implementation of the
* $(LINK2 http://www.dlang.org, D programming language).
*
* Copyright: Copyright (C) 2012-2019 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/src/dmd/backend/pdata.d, backend/pdata.d)
*/
// This module generates the .pdata and .xdata sections for Win64
module dmd.backend.pdata;
version (MARS)
{
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import dmd.backend.cc;
import dmd.backend.cdef;
import dmd.backend.code;
import dmd.backend.code_x86;
import dmd.backend.dt;
import dmd.backend.el;
import dmd.backend.exh;
import dmd.backend.global;
import dmd.backend.obj;
import dmd.backend.rtlsym;
import dmd.backend.ty;
import dmd.backend.type;
extern (C++):
nothrow:
static if (TARGET_WINDOS)
{
// Determine if this Symbol is stored in a COMDAT
bool symbol_iscomdat3(Symbol* s)
{
version (MARS)
{
return s.Sclass == SCcomdat ||
config.flags2 & CFG2comdat && s.Sclass == SCinline ||
config.flags4 & CFG4allcomdat && s.Sclass == SCglobal;
}
else
{
return s.Sclass == SCcomdat ||
config.flags2 & CFG2comdat && s.Sclass == SCinline ||
config.flags4 & CFG4allcomdat && (s.Sclass == SCglobal || s.Sclass == SCstatic);
}
}
enum ALLOCA_LIMIT = 0x10000;
/**********************************
* The .pdata section is used on Win64 by the VS debugger and dbghelp to get information
* to walk the stack and unwind exceptions.
* Absent it, it is assumed to be a "leaf function" where [RSP] is the return address.
* Creates an instance of struct RUNTIME_FUNCTION:
* http://msdn.microsoft.com/en-US/library/ft9x1kdx(v=vs.80).aspx
*
* Input:
* sf function to generate unwind data for
*/
void win64_pdata(Symbol *sf)
{
// return; // doesn't work yet
//printf("win64_pdata()\n");
assert(config.exe == EX_WIN64);
// Generate the pdata name, which is $pdata$funcname
size_t sflen = strlen(sf.Sident.ptr);
char *pdata_name = cast(char *)(sflen < ALLOCA_LIMIT ? alloca(7 + sflen + 1) : malloc(7 + sflen + 1));
assert(pdata_name);
memcpy(pdata_name, "$pdata$".ptr, 7);
memcpy(pdata_name + 7, sf.Sident.ptr, sflen + 1); // include terminating 0
Symbol *spdata = symbol_name(pdata_name,SCstatic,tstypes[TYint]);
symbol_keep(spdata);
symbol_debug(spdata);
Symbol *sunwind = win64_unwind(sf);
/* 3 pointers are emitted:
* 1. pointer to start of function sf
* 2. pointer past end of function sf
* 3. pointer to unwind data
*/
auto dtb = DtBuilder(0);
dtb.xoff(sf,0,TYint); // Note the TYint, these are 32 bit fixups
dtb.xoff(sf,cast(uint)(retoffset + retsize),TYint);
dtb.xoff(sunwind,0,TYint);
spdata.Sdt = dtb.finish();
spdata.Sseg = symbol_iscomdat3(sf) ? MsCoffObj_seg_pdata_comdat(sf) : MsCoffObj_seg_pdata();
spdata.Salignment = 4;
outdata(spdata);
if (sflen >= ALLOCA_LIMIT) free(pdata_name);
}
/**************************************************
* Unwind data symbol goes in the .xdata section.
* Input:
* sf function to generate unwind data for
* Returns:
* generated symbol referring to unwind data
*/
Symbol *win64_unwind(Symbol *sf)
{
// Generate the unwind name, which is $unwind$funcname
size_t sflen = strlen(sf.Sident.ptr);
char *unwind_name = cast(char *)(sflen < ALLOCA_LIMIT ? alloca(8 + sflen + 1) : malloc(8 + sflen + 1));
assert(unwind_name);
memcpy(unwind_name, "$unwind$".ptr, 8);
memcpy(unwind_name + 8, sf.Sident.ptr, sflen + 1); // include terminating 0
Symbol *sunwind = symbol_name(unwind_name,SCstatic,tstypes[TYint]);
symbol_keep(sunwind);
symbol_debug(sunwind);
sunwind.Sdt = unwind_data();
sunwind.Sseg = symbol_iscomdat3(sf) ? MsCoffObj_seg_xdata_comdat(sf) : MsCoffObj_seg_xdata();
sunwind.Salignment = 1;
outdata(sunwind);
if (sflen >= ALLOCA_LIMIT) free(unwind_name);
return sunwind;
}
/************************* Win64 Unwind Data ******************************************/
/************************************************************************
* Creates an instance of struct UNWIND_INFO:
* http://msdn.microsoft.com/en-US/library/ddssxxy8(v=vs.80).aspx
*/
enum UWOP
{ // http://www.osronline.com/ddkx/kmarch/64bitamd_7btz.htm
// http://uninformed.org/index.cgi?v=4&a=1&p=17
PUSH_NONVOL, // push saved register, OpInfo is register
ALLOC_LARGE, // alloc large size on stack, OpInfo is 0 or 1
ALLOC_SMALL, // alloc small size on stack, OpInfo is size / 8 - 1
SET_FPREG, // set frame pointer
SAVE_NONVOL, // save register, OpInfo is reg, frame offset in next FrameOffset
SAVE_NONVOL_FAR, // save register, OpInfo is reg, frame offset in next 2 FrameOffsets
SAVE_XMM128, // save 64 bits of XMM reg, frame offset in next FrameOffset
SAVE_XMM128_FAR, // save 64 bits of XMM reg, frame offset in next 2 FrameOffsets
PUSH_MACHFRAME // push interrupt frame, OpInfo is 0 or 1 (pushes error code too)
}
union UNWIND_CODE
{
/+
struct
{
ubyte CodeOffset; // offset of start of next instruction
ubyte UnwindOp : 4; // UWOP
ubyte OpInfo : 4; // extra information depending on UWOP
} op;
+/
ushort FrameOffset;
}
ushort setUnwindCode(ubyte CodeOffset, ubyte UnwindOp, ubyte OpInfo)
{
return cast(ushort)(CodeOffset | (UnwindOp << 8) | (OpInfo << 12));
}
enum
{
UNW_FLAG_EHANDLER = 1, // function has an exception handler
UNW_FLAG_UHANDLER = 2, // function has a termination handler
UNW_FLAG_CHAININFO = 4 // not the primary one for the function
}
struct UNWIND_INFO
{
ubyte Version; //: 3; // 1
//ubyte Flags : 5; // UNW_FLAG_xxxx
ubyte SizeOfProlog; // bytes in the function prolog
ubyte CountOfCodes; // dimension of UnwindCode[]
ubyte FrameRegister; //: 4; // if !=0, then frame pointer register
//ubyte FrameOffset : 4; // frame register offset from RSP divided by 16
UNWIND_CODE[6] UnwindCode;
static if (0)
{
UNWIND_CODE[((CountOfCodes + 1) & ~1) - 1] MoreUnwindCode;
union
{
// UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER
struct
{
uint ExceptionHandler;
void[n] Language_specific_handler_data;
}
// UNW_FLAG_CHAININFO
RUNTIME_FUNCTION chained_unwind_info;
}
}
}
dt_t *unwind_data()
{
UNWIND_INFO ui;
/* 4 allocation size strategy:
* 0: no unwind instruction
* 8..128: UWOP.ALLOC_SMALL
* 136..512K-8: UWOP.ALLOC_LARGE, OpInfo = 0
* 512K..4GB-8: UWOP.ALLOC_LARGE, OpInfo = 1
*/
targ_size_t sz = localsize;
assert((localsize & 7) == 0);
int strategy;
if (sz == 0)
strategy = 0;
else if (sz <= 128)
strategy = 1;
else if (sz <= 512 * 1024 - 8)
strategy = 2;
else
// 512KB to 4GB-8
strategy = 3;
ui.Version = 1;
//ui.Flags = 0;
ui.SizeOfProlog = cast(ubyte)startoffset;
static if (0)
{
ui.CountOfCodes = strategy + 1;
ui.FrameRegister = 0;
//ui.FrameOffset = 0;
}
else
{
strategy = 0;
ui.CountOfCodes = cast(ubyte)(strategy + 2);
ui.FrameRegister = BP;
//ui.FrameOffset = 0; //cod3_spoff() / 16;
}
static if (0)
{
switch (strategy)
{
case 0:
break;
case 1:
ui.UnwindCode[0].FrameOffset = setUnwindCode(prolog_allocoffset, UWOP.ALLOC_SMALL, (sz - 8) / 8);
break;
case 2:
ui.UnwindCode[0].FrameOffset = setUnwindCode(prolog_allocoffset, UWOP.ALLOC_LARGE, 0);
ui.UnwindCode[1].FrameOffset = (sz - 8) / 8;
break;
case 3:
ui.UnwindCode[0].FrameOffset = setUnwindCode(prolog_allocoffset, UWOP.ALLOC_LARGE, 1);
ui.UnwindCode[1].FrameOffset = sz & 0x0FFFF;
ui.UnwindCode[2].FrameOffset = sz / 0x10000;
break;
}
}
static if (1)
{
ui.UnwindCode[ui.CountOfCodes-2].FrameOffset = setUnwindCode(4, UWOP.SET_FPREG, 0);
}
ui.UnwindCode[ui.CountOfCodes-1].FrameOffset = setUnwindCode(1, UWOP.PUSH_NONVOL, BP);
auto dtb = DtBuilder(0);
dtb.nbytes(4 + ((ui.CountOfCodes + 1) & ~1) * 2,cast(char *)&ui);
return dtb.finish();
}
}
}