History

win32: register SEH in startup code (i386 only)

Needed to handle signal() etc. with msvcrt

grischka committed 16 years ago

Commit 92288421 parent 036d9414 files · +126 −2Browse files

Unified · Split

4 files
  1. win32/include/_mingw.h +2 −0
  2. win32/lib/chkstk.S +122 −0
  3. win32/lib/crt1.c +1 −2
  4. win32/lib/wincrt1.c +1 −0
win32/include/_mingw.h+2−0
@@ -70,11 +70,13 @@ · 69 unmodified lines
7070 #define __x86_64 1
7171 #define USE_MINGW_SETJMP_TWO_ARGS
7272 #define mingw_getsp tinyc_getbp
73+#define __TRY__
7374 #else
7475 #define __stdcall __attribute__((__stdcall__))
7576 #define _X86_ 1
7677 #define WIN32 1
7778 #define _USE_32BIT_TIME_T
79+#define __TRY__ void __try__(void**), *_sehrec[6]; __try__(_sehrec);
7880 #endif
7981
8082 /* in stddef.h */
win32/lib/chkstk.S+122−0
@@ -75,3 +75,125 @@ t1: · 74 unmodified lines
7575 /* ---------------------------------------------- */
7676 #endif
7777 /* ---------------------------------------------- */
78+
79+
80+/* ---------------------------------------------- */
81+#ifndef TCC_TARGET_X86_64
82+/* ---------------------------------------------- */
83+
84+/*
85+ int _except_handler3(
86+ PEXCEPTION_RECORD exception_record,
87+ PEXCEPTION_REGISTRATION registration,
88+ PCONTEXT context,
89+ PEXCEPTION_REGISTRATION dispatcher
90+ );
91+
92+ int __cdecl _XcptFilter(
93+ unsigned long xcptnum,
94+ PEXCEPTION_POINTERS pxcptinfoptrs
95+ );
96+
97+ struct _sehrec {
98+ void *esp; // 0
99+ void *exception_pointers; // 1
100+ void *prev; // 2
101+ void *handler; // 3
102+ void *scopetable; // 4
103+ int trylevel; // 5
104+ void *ebp // 6
105+ };
106+
107+ // this is what the assembler code below means:
108+ __try
109+ {
110+ // ...
111+ }
112+ __except (_XcptFilter(GetExceptionCode(), GetExceptionInformation()))
113+ {
114+ exit(GetExceptionCode());
115+ }
116+*/
117+
118+.globl _exception_info
119+_exception_info:
120+ mov 1*4-24(%ebp),%eax
121+ ret
122+
123+.globl _exception_code
124+_exception_code:
125+ call _exception_info
126+ mov (%eax),%eax
127+ mov (%eax),%eax
128+ ret
129+
130+seh_filter:
131+ call _exception_info
132+ push %eax
133+ call _exception_code
134+ push %eax
135+ call _XcptFilter
136+ add $ 8,%esp
137+ ret
138+
139+seh_except:
140+ mov 0*4-24(%ebp),%esp
141+ call _exception_code
142+ push %eax
143+ call _exit
144+
145+// msvcrt wants scopetables aligned and in read-only segment (using .text)
146+.align 4
147+seh_scopetable:
148+ .long -1
149+ .long seh_filter
150+ .long seh_except
151+
152+seh_handler:
153+ jmp _except_handler3
154+
155+.globl __try__
156+__try__:
157+ push %ebp
158+ mov 8(%esp),%ebp
159+
160+// void *esp;
161+ lea 12(%esp),%eax
162+ mov %eax,0*4(%ebp)
163+
164+// void *exception_pointers;
165+ xor %eax,%eax
166+ mov %eax,1*4(%ebp)
167+
168+// void *prev;
169+ mov %fs:0,%eax
170+ mov %eax,2*4(%ebp)
171+
172+// void *handler;
173+ mov $ seh_handler,%eax
174+ mov %eax,3*4(%ebp)
175+
176+// void *scopetable;
177+ mov $ seh_scopetable,%eax
178+ mov %eax,4*4(%ebp)
179+
180+// int trylevel;
181+ xor %eax,%eax
182+ mov %eax,5*4(%ebp)
183+
184+// register new SEH
185+ lea 2*4(%ebp),%eax
186+ mov %eax,%fs:0
187+
188+ pop %ebp
189+ ret
190+
191+/* ---------------------------------------------- */
192+#else
193+/* ---------------------------------------------- */
194+
195+/* SEH on x86-64 not implemented */
196+
197+/* ---------------------------------------------- */
198+#endif
199+/* ---------------------------------------------- */
win32/lib/crt1.c+1−2
@@ -15,11 +15,11 @@ typedef struct · 14 unmodified lines
1515 } _startupinfo;
1616
1717 void __getmainargs(int *pargc, char ***pargv, char ***penv, int globb, _startupinfo*);
18−
1918 int main(int argc, char **argv, char **env);
2019
2120 int _start(void)
2221 {
22+ __TRY__
2323 int argc; char **argv; char **env; int ret;
2424 _startupinfo start_info = {0};
2525
@@ -32,4 +32,3 @@ int _start(void) · 6 unmodified lines
3232 }
3333
3434 // =============================================
35−
win32/lib/wincrt1.c+1−0
@@ -10,6 +10,7 @@ void _controlfp(unsigned a, unsigned b); · 9 unmodified lines
1010
1111 int _winstart(void)
1212 {
13+ __TRY__
1314 char *szCmd;
1415 STARTUPINFO startinfo;
1516 int fShow;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t