-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheh.c
344 lines (303 loc) · 11.5 KB
/
eh.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (c) 1994-1998 by Symantec
* Copyright (c) 2000-2011 by Digital Mars
* All Rights Reserved
* http://www.digitalmars.com
* http://www.dsource.org/projects/dmd/browser/branches/dmd-1.x/src/eh.c
* http://www.dsource.org/projects/dmd/browser/trunk/src/eh.c
* Written by Walter Bright
*
* This source file is made available for personal use
* only. The license is in /dmd/src/dmd/backendlicense.txt
* For any other uses, please contact Digital Mars.
*/
// Support for D exception handling
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "cc.h"
#include "el.h"
#include "code.h"
#include "oper.h"
#include "global.h"
#include "type.h"
#include "dt.h"
#include "exh.h"
static char __file__[] = __FILE__; /* for tassert.h */
#include "tassert.h"
/* If we do our own EH tables and stack walking scheme
* (Otherwise use NT Structured Exception Handling)
*/
#define OUREH (TARGET_LINUX || TARGET_OSX || TARGET_FREEBSD || TARGET_OPENBSD || TARGET_SOLARIS)
/****************************
* Generate and output scope table.
*/
symbol *except_gentables()
{
//printf("except_gentables()\n");
#if OUREH
// BUG: alloca() changes the stack size, which is not reflected
// in the fixed eh tables.
assert(!usedalloca);
symbol *s = symbol_generate(SCstatic,tsint);
s->Sseg = UNKNOWN;
symbol_keep(s);
symbol_debug(s);
except_fillInEHTable(s);
outdata(s); // output the scope table
obj_ehtables(funcsym_p,funcsym_p->Ssize,s);
#endif
return NULL;
}
/**********************************************
* Initializes the symbol s with the contents of the exception handler table.
*/
struct Guard
{
#if OUREH
unsigned offset; // offset of start of guarded section (Linux)
unsigned endoffset; // ending offset of guarded section (Linux)
#endif
int last_index; // previous index (enclosing guarded section)
unsigned catchoffset; // offset to catch block from symbol
void *finally; // finally code to execute
};
void except_fillInEHTable(symbol *s)
{
unsigned fsize = NPTRSIZE; // target size of function pointer
dt_t **pdt = &s->Sdt;
/*
void* pointer to start of function
unsigned offset of ESP from EBP
unsigned offset from start of function to return code
unsigned nguards; // dimension of guard[] (Linux)
Guard guard[]; // sorted such that the enclosing guarded sections come first
catchoffset:
unsigned ncatches; // number of catch blocks
{ void *type; // symbol representing type
unsigned bpoffset; // EBP offset of catch variable
void *handler; // catch handler code
} catch[];
*/
/* Be careful of this, as we need the sizeof Guard on the target, not
* in the compiler.
*/
#if OUREH
#define GUARD_SIZE (I64 ? 3*8 : 5*4) // sizeof(Guard)
#else
#define GUARD_SIZE (sizeof(Guard))
#endif
int sz = 0;
// Address of start of function
symbol_debug(funcsym_p);
pdt = dtxoff(pdt,funcsym_p,0,TYnptr);
sz += fsize;
//printf("ehtables: func = %s, offset = x%x, startblock->Boffset = x%x\n", funcsym_p->Sident, funcsym_p->Soffset, startblock->Boffset);
// Get offset of ESP from EBP
long spoff = cod3_spoff();
pdt = dtdword(pdt,spoff);
sz += 4;
// Offset from start of function to return code
pdt = dtdword(pdt,retoffset);
sz += 4;
// First, calculate starting catch offset
int guarddim = 0; // max dimension of guard[]
int ndctors = 0; // number of ESCdctor's
for (block *b = startblock; b; b = b->Bnext)
{
if (b->BC == BC_try && b->Bscope_index >= guarddim)
guarddim = b->Bscope_index + 1;
// printf("b->BC = %2d, Bscope_index = %2d, last_index = %2d, offset = x%x\n",
// b->BC, b->Bscope_index, b->Blast_index, b->Boffset);
if (usednteh & EHcleanup)
for (code *c = b->Bcode; c; c = code_next(c))
{
if (c->Iop == (ESCAPE | ESCddtor))
ndctors++;
}
}
//printf("guarddim = %d, ndctors = %d\n", guarddim, ndctors);
#if OUREH
pdt = dtsize_t(pdt,guarddim + ndctors);
sz += NPTRSIZE;
#endif
unsigned catchoffset = sz + (guarddim + ndctors) * GUARD_SIZE;
// Generate guard[]
int i = 0;
for (block *b = startblock; b; b = b->Bnext)
{
//printf("b = %p, b->Btry = %p, b->offset = %x\n", b, b->Btry, b->Boffset);
if (b->BC == BC_try)
{
assert(b->Bscope_index >= i);
if (i < b->Bscope_index)
{ int fillsize = (b->Bscope_index - i) * GUARD_SIZE;
pdt = dtnzeros(pdt, fillsize);
sz += fillsize;
}
i = b->Bscope_index + 1;
int nsucc = list_nitems(b->Bsucc);
#if OUREH
//printf("DHandlerInfo: offset = %x", (int)(b->Boffset - startblock->Boffset));
pdt = dtdword(pdt,b->Boffset - startblock->Boffset); // offset to start of block
// Compute ending offset
unsigned endoffset;
for (block *bn = b->Bnext; 1; bn = bn->Bnext)
{
//printf("\tbn = %p, bn->Btry = %p, bn->offset = %x\n", bn, bn->Btry, bn->Boffset);
assert(bn);
if (bn->Btry == b->Btry)
{ endoffset = bn->Boffset - startblock->Boffset;
break;
}
}
//printf(" endoffset = %x, prev_index = %d\n", endoffset, b->Blast_index);
pdt = dtdword(pdt,endoffset); // offset past end of guarded block
#endif
pdt = dtdword(pdt,b->Blast_index); // parent index
if (b->jcatchvar) // if try-catch
{
pdt = dtdword(pdt,catchoffset);
pdt = dtsize_t(pdt,0); // no finally handler
catchoffset += NPTRSIZE + (nsucc - 1) * (3 * NPTRSIZE);
}
else // else try-finally
{
assert(nsucc == 2);
pdt = dtdword(pdt,0); // no catch offset
block *bhandler = list_block(list_next(b->Bsucc));
assert(bhandler->BC == BC_finally);
// To successor of BC_finally block
bhandler = list_block(bhandler->Bsucc);
#if OUREH
pdt = dtxoff(pdt,funcsym_p,bhandler->Boffset - startblock->Boffset, TYnptr); // finally handler address
#else
pdt = dtcoff(pdt,bhandler->Boffset); // finally handler address
#endif
}
sz += GUARD_SIZE;
}
}
/* Append to guard[] the guard blocks for temporaries that are created and destroyed
* within a single expression. These are marked by the special instruction pairs
* (ESCAPE | ESCdctor) and (ESCAPE | ESCddtor).
*/
if (usednteh & EHcleanup)
{
int scopeindex = guarddim;
for (block *b = startblock; b; b = b->Bnext)
{
/* Set up stack of scope indices
*/
#define STACKINC 16
int stackbuf[STACKINC];
int *stack = stackbuf;
int stackmax = STACKINC;
stack[0] = b->Btry ? b->Btry->Bscope_index : -1;
int stacki = 1;
unsigned boffset = b->Boffset;
for (code *c = b->Bcode; c; c = code_next(c))
{
if (c->Iop == (ESCAPE | ESCdctor))
{
code *c2 = code_next(c);
if (config.flags2 & CFG2seh)
c2->IEV2.Vsize_t = scopeindex;
#if OUREH
pdt = dtdword(pdt,boffset - startblock->Boffset); // guard offset
#endif
// Find corresponding ddtor instruction
int n = 0;
unsigned eoffset = boffset;
unsigned foffset;
for (; 1; c2 = code_next(c2))
{
assert(c2);
if (c2->Iop == (ESCAPE | ESCddtor))
{
if (n)
n--;
else
{
foffset = eoffset;
code *cf = code_next(c2);
if (config.flags2 & CFG2seh)
{ cf->IEV2.Vsize_t = stack[stacki - 1];
foffset += calccodsize(cf);
cf = code_next(cf);
}
foffset += calccodsize(cf);
while (cf->Iop != JMP && cf->Iop != JMPS)
{
cf = code_next(cf);
foffset += calccodsize(cf);
}
cf = code_next(cf);
foffset += calccodsize(cf);
#if OUREH
pdt = dtdword(pdt,eoffset - startblock->Boffset); // guard offset
#endif
break;
}
}
else if (c2->Iop == (ESCAPE | ESCdctor))
{
n++;
}
else
eoffset += calccodsize(c2);
}
//printf("boffset = %x, eoffset = %x, foffset = %x\n", boffset, eoffset, foffset);
pdt = dtdword(pdt,stack[stacki - 1]); // parent index
pdt = dtdword(pdt,0); // no catch offset
#if OUREH
pdt = dtxoff(pdt,funcsym_p,foffset - startblock->Boffset, TYnptr); // finally handler offset
#else
pdt = dtcoff(pdt,foffset); // finally handler address
#endif
if (stacki == stackmax)
{ // stack[] is out of space; enlarge it
int *pi = (int *)alloca((stackmax + STACKINC) * sizeof(int));
assert(pi);
memcpy(pi, stack, stackmax * sizeof(int));
stack = pi;
stackmax += STACKINC;
}
stack[stacki++] = scopeindex;
++scopeindex;
sz += GUARD_SIZE;
}
else if (c->Iop == (ESCAPE | ESCddtor))
{
stacki--;
assert(stacki != 0);
}
boffset += calccodsize(c);
}
}
}
// Generate catch[]
for (block *b = startblock; b; b = b->Bnext)
{
if (b->BC == BC_try && b->jcatchvar) // if try-catch
{
int nsucc = list_nitems(b->Bsucc);
pdt = dtsize_t(pdt,nsucc - 1); // # of catch blocks
sz += NPTRSIZE;
for (list_t bl = list_next(b->Bsucc); bl; bl = list_next(bl))
{
block *bcatch = list_block(bl);
pdt = dtxoff(pdt,bcatch->Bcatchtype,0,TYjhandle);
pdt = dtsize_t(pdt,cod3_bpoffset(b->jcatchvar)); // EBP offset
#if OUREH
pdt = dtxoff(pdt,funcsym_p,bcatch->Boffset - startblock->Boffset, TYnptr); // catch handler address
#else
pdt = dtcoff(pdt,bcatch->Boffset); // catch handler address
#endif
sz += 3 * NPTRSIZE;
}
}
}
assert(sz != 0);
}