summaryrefslogtreecommitdiff
path: root/win32/lib/chkstk.S
blob: ec5c07ff8dcfc2c7f35bcabd4a5ca011f6d8d809 (plain)
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
/* ---------------------------------------------- */
/* chkstk86.s */

/* ---------------------------------------------- */
#ifndef __x86_64__
/* ---------------------------------------------- */

.globl __chkstk

__chkstk:
    xchg    (%esp),%ebp     /* store ebp, get ret.addr */
    push    %ebp            /* push ret.addr */
    lea     4(%esp),%ebp    /* setup frame ptr */
    push    %ecx            /* save ecx */
    mov     %ebp,%ecx
P0:
    sub     $4096,%ecx
    test    %eax,(%ecx)
    sub     $4096,%eax
    cmp     $4096,%eax
    jge     P0
    sub     %eax,%ecx
    test    %eax,(%ecx)

    mov     %esp,%eax
    mov     %ecx,%esp
    mov     (%eax),%ecx     /* restore ecx */
    jmp     *4(%eax)

/* ---------------------------------------------- */
#else
/* ---------------------------------------------- */

.globl __chkstk

__chkstk:
    xchg    (%rsp),%rbp     /* store ebp, get ret.addr */
    push    %rbp            /* push ret.addr */
    lea     8(%rsp),%rbp    /* setup frame ptr */
    push    %rcx            /* save ecx */
    mov     %rbp,%rcx
    movslq  %eax,%rax
P0:
    sub     $4096,%rcx
    test    %rax,(%rcx)
    sub     $4096,%rax
    cmp     $4096,%rax
    jge     P0
    sub     %rax,%rcx
    test    %rax,(%rcx)

    mov     %rsp,%rax
    mov     %rcx,%rsp
    mov     (%rax),%rcx     /* restore ecx */
    jmp     *8(%rax)

/* ---------------------------------------------- */
/* setjmp/longjmp support */

.globl tinyc_getbp
tinyc_getbp:
    mov %rbp,%rax
    ret

/* ---------------------------------------------- */
#endif
/* ---------------------------------------------- */


/* ---------------------------------------------- */
#ifndef __x86_64__
/* ---------------------------------------------- */

/*
    int _except_handler3(
       PEXCEPTION_RECORD exception_record,
       PEXCEPTION_REGISTRATION registration,
       PCONTEXT context,
       PEXCEPTION_REGISTRATION dispatcher
    );

    int __cdecl _XcptFilter(
       unsigned long xcptnum,
       PEXCEPTION_POINTERS pxcptinfoptrs
    );

    struct _sehrec {
       void *esp;                // 0
       void *exception_pointers; // 1
       void *prev;               // 2
       void *handler;            // 3
       void *scopetable;         // 4
       int trylevel;             // 5
       void *ebp                 // 6
    };

    // this is what the assembler code below means:
    __try
    {
         // ...
    }
    __except (_XcptFilter(GetExceptionCode(), GetExceptionInformation()))
    {
         exit(GetExceptionCode());
    }
*/

.globl _exception_info
_exception_info:
    mov 1*4-24(%ebp),%eax
    ret

.globl _exception_code
_exception_code:
    call _exception_info
    mov (%eax),%eax
    mov (%eax),%eax
    ret

seh_filter:
    call _exception_info
    push %eax
    call _exception_code
    push %eax
    call _XcptFilter
    add $ 8,%esp
    ret

seh_except:
    mov 0*4-24(%ebp),%esp
    call _exception_code
    push %eax
    call _exit

// msvcrt wants scopetables aligned and in read-only segment (using .text)
.align 4
seh_scopetable:
    .long -1
    .long seh_filter
    .long seh_except

seh_handler:
    jmp _except_handler3

.globl ___try__
___try__:
.globl __try__
__try__:
    push %ebp
    mov 8(%esp),%ebp

//    void *esp;
    lea 12(%esp),%eax
    mov %eax,0*4(%ebp)

//    void *exception_pointers;
    xor %eax,%eax
    mov %eax,1*4(%ebp)

//    void *prev;
    mov %fs:0,%eax
    mov %eax,2*4(%ebp)

//    void *handler;
    mov $ seh_handler,%eax
    mov %eax,3*4(%ebp)

//    void *scopetable;
    mov $ seh_scopetable,%eax
    mov %eax,4*4(%ebp)

//    int trylevel;
    xor %eax,%eax
    mov %eax,5*4(%ebp)

//    register new SEH
    lea 2*4(%ebp),%eax
    mov %eax,%fs:0

    pop %ebp
    ret

/* ---------------------------------------------- */
#else
/* ---------------------------------------------- */

/* SEH on x86-64 not implemented */

/* ---------------------------------------------- */
#endif
/* ---------------------------------------------- */