2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1996, 1997 Alexandre Julliard
5 * Copyright 1997, 1998 Marcus Meissner
6 * Copyright 1998 Ulrich Weigand
11 #include <sys/types.h>
17 #include "wine/winbase16.h"
19 #include "builtin16.h"
21 #include "debugtools.h"
22 #include "flatthunk.h"
25 #include "selectors.h"
26 #include "stackframe.h"
29 DEFAULT_DEBUG_CHANNEL(thunk
);
32 /***********************************************************************
34 * Win95 internal thunks *
36 ***********************************************************************/
38 /***********************************************************************
39 * LogApiThk (KERNEL.423)
41 void WINAPI
LogApiThk( LPSTR func
)
43 TRACE( "%s\n", debugstr_a(func
) );
46 /***********************************************************************
47 * LogApiThkLSF (KERNEL32.42)
49 * NOTE: needs to preserve all registers!
51 void WINAPI
LogApiThkLSF( LPSTR func
, CONTEXT86
*context
)
53 TRACE( "%s\n", debugstr_a(func
) );
56 /***********************************************************************
57 * LogApiThkSL (KERNEL32.44)
59 * NOTE: needs to preserve all registers!
61 void WINAPI
LogApiThkSL( LPSTR func
, CONTEXT86
*context
)
63 TRACE( "%s\n", debugstr_a(func
) );
66 /***********************************************************************
67 * LogCBThkSL (KERNEL32.47)
69 * NOTE: needs to preserve all registers!
71 void WINAPI
LogCBThkSL( LPSTR func
, CONTEXT86
*context
)
73 TRACE( "%s\n", debugstr_a(func
) );
76 /***********************************************************************
77 * Generates a FT_Prolog call.
79 * 0FB6D1 movzbl edx,cl
80 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
81 * 68xxxxxxxx push FT_Prolog
84 static void _write_ftprolog(LPBYTE relayCode
,DWORD
*targetTable
) {
88 *x
++ = 0x0f;*x
++=0xb6;*x
++=0xd1; /* movzbl edx,cl */
89 *x
++ = 0x8B;*x
++=0x14;*x
++=0x95;*(DWORD
**)x
= targetTable
;
90 x
+=4; /* mov edx, [4*edx + targetTable] */
91 *x
++ = 0x68; *(DWORD
*)x
= (DWORD
)GetProcAddress(GetModuleHandleA("KERNEL32"),"FT_Prolog");
92 x
+=4; /* push FT_Prolog */
93 *x
++ = 0xC3; /* lret */
94 /* fill rest with 0xCC / int 3 */
97 /***********************************************************************
98 * _write_qtthunk (internal)
99 * Generates a QT_Thunk style call.
102 * 8A4DFC mov cl , [ebp-04]
103 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
104 * B8yyyyyyyy mov eax, QT_Thunk
107 static void _write_qtthunk(
108 LPBYTE relayCode
, /* [in] start of QT_Thunk stub */
109 DWORD
*targetTable
/* [in] start of thunk (for index lookup) */
114 *x
++ = 0x33;*x
++=0xC9; /* xor ecx,ecx */
115 *x
++ = 0x8A;*x
++=0x4D;*x
++=0xFC; /* movb cl,[ebp-04] */
116 *x
++ = 0x8B;*x
++=0x14;*x
++=0x8D;*(DWORD
**)x
= targetTable
;
117 x
+=4; /* mov edx, [4*ecx + targetTable */
118 *x
++ = 0xB8; *(DWORD
*)x
= (DWORD
)GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
119 x
+=4; /* mov eax , QT_Thunk */
120 *x
++ = 0xFF; *x
++ = 0xE0; /* jmp eax */
121 /* should fill the rest of the 32 bytes with 0xCC */
124 /***********************************************************************
127 static LPVOID
_loadthunk(LPCSTR module
, LPCSTR func
, LPCSTR module32
,
128 struct ThunkDataCommon
*TD32
, DWORD checksum
)
130 struct ThunkDataCommon
*TD16
;
134 if ((hmod
= LoadLibrary16(module
)) <= 32)
136 ERR("(%s, %s, %s): Unable to load '%s', error %d\n",
137 module
, func
, module32
, module
, hmod
);
141 if ( !(ordinal
= NE_GetOrdinal(hmod
, func
))
142 || !(TD16
= MapSL((SEGPTR
)NE_GetEntryPointEx(hmod
, ordinal
, FALSE
))))
144 ERR("Unable to find thunk data '%s' in %s, required by %s (conflicting/incorrect DLL versions !?).\n",
145 func
, module
, module32
);
149 if (TD32
&& memcmp(TD16
->magic
, TD32
->magic
, 4))
151 ERR("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
152 module
, func
, module32
,
153 TD16
->magic
[0], TD16
->magic
[1], TD16
->magic
[2], TD16
->magic
[3],
154 TD32
->magic
[0], TD32
->magic
[1], TD32
->magic
[2], TD32
->magic
[3]);
158 if (TD32
&& TD16
->checksum
!= TD32
->checksum
)
160 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
161 module
, func
, module32
, TD16
->checksum
, TD32
->checksum
);
165 if (!TD32
&& checksum
&& checksum
!= *(LPDWORD
)TD16
)
167 ERR("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
168 module
, func
, module32
, *(LPDWORD
)TD16
, checksum
);
175 /***********************************************************************
176 * GetThunkStuff (KERNEL32.53)
178 LPVOID WINAPI
GetThunkStuff(LPSTR module
, LPSTR func
)
180 return _loadthunk(module
, func
, "<kernel>", NULL
, 0L);
183 /***********************************************************************
184 * GetThunkBuff (KERNEL32.52)
185 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
187 LPVOID WINAPI
GetThunkBuff(void)
189 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
192 /***********************************************************************
193 * ThunkConnect32 (KERNEL32.@)
194 * Connects a 32bit and a 16bit thunkbuffer.
196 UINT WINAPI
ThunkConnect32(
197 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
198 LPSTR thunkfun16
, /* [in] win16 thunkfunction */
199 LPSTR module16
, /* [in] name of win16 dll */
200 LPSTR module32
, /* [in] name of win32 dll */
201 HMODULE hmod32
, /* [in] hmodule of win32 dll */
202 DWORD dwReason
/* [in] initialisation argument */
206 if (!strncmp(TD
->magic
, "SL01", 4))
210 TRACE("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
211 module32
, (DWORD
)TD
, module16
, thunkfun16
, dwReason
);
213 else if (!strncmp(TD
->magic
, "LS01", 4))
217 TRACE("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
218 module32
, (DWORD
)TD
, module16
, thunkfun16
, dwReason
);
222 ERR("Invalid magic %c%c%c%c\n",
223 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
229 case DLL_PROCESS_ATTACH
:
231 struct ThunkDataCommon
*TD16
;
232 if (!(TD16
= _loadthunk(module16
, thunkfun16
, module32
, TD
, 0L)))
237 struct ThunkDataSL32
*SL32
= (struct ThunkDataSL32
*)TD
;
238 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD16
;
239 struct SLTargetDB
*tdb
;
241 if (SL16
->fpData
== NULL
)
243 ERR("ThunkConnect16 was not called!\n");
247 SL32
->data
= SL16
->fpData
;
249 tdb
= HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb
));
250 tdb
->process
= GetCurrentProcessId();
251 tdb
->targetTable
= (DWORD
*)(thunkfun16
+ SL32
->offsetTargetTable
);
253 tdb
->next
= SL32
->data
->targetDB
; /* FIXME: not thread-safe! */
254 SL32
->data
->targetDB
= tdb
;
256 TRACE("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
257 GetCurrentProcessId(), (DWORD
)SL32
->data
);
261 struct ThunkDataLS32
*LS32
= (struct ThunkDataLS32
*)TD
;
262 struct ThunkDataLS16
*LS16
= (struct ThunkDataLS16
*)TD16
;
264 LS32
->targetTable
= MapSL(LS16
->targetTable
);
266 /* write QT_Thunk and FT_Prolog stubs */
267 _write_qtthunk ((LPBYTE
)TD
+ LS32
->offsetQTThunk
, LS32
->targetTable
);
268 _write_ftprolog((LPBYTE
)TD
+ LS32
->offsetFTProlog
, LS32
->targetTable
);
273 case DLL_PROCESS_DETACH
:
281 /**********************************************************************
282 * QT_Thunk (KERNEL32.@)
284 * The target address is in EDX.
285 * The 16 bit arguments start at ESP.
286 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
289 void WINAPI
QT_Thunk( CONTEXT86
*context
)
294 memcpy(&context16
,context
,sizeof(context16
));
296 context16
.SegCs
= HIWORD(context
->Edx
);
297 context16
.Eip
= LOWORD(context
->Edx
);
298 context16
.Ebp
= OFFSETOF( NtCurrentTeb()->cur_stack
)
299 + (WORD
)&((STACK16FRAME
*)0)->bp
;
301 argsize
= context
->Ebp
-context
->Esp
-0x40;
303 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
304 (LPBYTE
)context
->Esp
, argsize
);
306 wine_call_to_16_regs_short( &context16
, argsize
);
307 context
->Eax
= context16
.Eax
;
308 context
->Edx
= context16
.Edx
;
309 context
->Ecx
= context16
.Ecx
;
311 context
->Esp
+= LOWORD(context16
.Esp
) -
312 ( OFFSETOF( NtCurrentTeb()->cur_stack
) - argsize
);
316 /**********************************************************************
317 * FT_Prolog (KERNEL32.@)
319 * The set of FT_... thunk routines is used instead of QT_Thunk,
320 * if structures have to be converted from 32-bit to 16-bit
321 * (change of member alignment, conversion of members).
323 * The thunk function (as created by the thunk compiler) calls
324 * FT_Prolog at the beginning, to set up a stack frame and
325 * allocate a 64 byte buffer on the stack.
326 * The input parameters (target address and some flags) are
327 * saved for later use by FT_Thunk.
329 * Input: EDX 16-bit target address (SEGPTR)
330 * CX bits 0..7 target number (in target table)
331 * bits 8..9 some flags (unclear???)
332 * bits 10..15 number of DWORD arguments
334 * Output: A new stackframe is created, and a 64 byte buffer
335 * allocated on the stack. The layout of the stack
336 * on return is as follows:
338 * (ebp+4) return address to caller of thunk function
340 * (ebp-4) saved EBX register of caller
341 * (ebp-8) saved ESI register of caller
342 * (ebp-12) saved EDI register of caller
343 * (ebp-16) saved ECX register, containing flags
344 * (ebp-20) bitmap containing parameters that are to be converted
345 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
346 * filled in by the thunk code before calling FT_Thunk
350 * (ebp-48) saved EAX register of caller (unclear, never restored???)
351 * (ebp-52) saved EDX register, containing 16-bit thunk target
356 * ESP is EBP-64 after return.
360 void WINAPI
FT_Prolog( CONTEXT86
*context
)
362 /* Build stack frame */
363 stack32_push(context
, context
->Ebp
);
364 context
->Ebp
= context
->Esp
;
366 /* Allocate 64-byte Thunk Buffer */
368 memset((char *)context
->Esp
, '\0', 64);
370 /* Store Flags (ECX) and Target Address (EDX) */
371 /* Save other registers to be restored later */
372 *(DWORD
*)(context
->Ebp
- 4) = context
->Ebx
;
373 *(DWORD
*)(context
->Ebp
- 8) = context
->Esi
;
374 *(DWORD
*)(context
->Ebp
- 12) = context
->Edi
;
375 *(DWORD
*)(context
->Ebp
- 16) = context
->Ecx
;
377 *(DWORD
*)(context
->Ebp
- 48) = context
->Eax
;
378 *(DWORD
*)(context
->Ebp
- 52) = context
->Edx
;
381 /**********************************************************************
382 * FT_Thunk (KERNEL32.@)
384 * This routine performs the actual call to 16-bit code,
385 * similar to QT_Thunk. The differences are:
386 * - The call target is taken from the buffer created by FT_Prolog
387 * - Those arguments requested by the thunk code (by setting the
388 * corresponding bit in the bitmap at EBP-20) are converted
389 * from 32-bit pointers to segmented pointers (those pointers
390 * are guaranteed to point to structures copied to the stack
391 * by the thunk code, so we always use the 16-bit stack selector
392 * for those addresses).
394 * The bit #i of EBP-20 corresponds here to the DWORD starting at
397 * FIXME: It is unclear what happens if there are more than 32 WORDs
398 * of arguments, so that the single DWORD bitmap is no longer
402 void WINAPI
FT_Thunk( CONTEXT86
*context
)
404 DWORD mapESPrelative
= *(DWORD
*)(context
->Ebp
- 20);
405 DWORD callTarget
= *(DWORD
*)(context
->Ebp
- 52);
409 LPBYTE newstack
, oldstack
;
411 memcpy(&context16
,context
,sizeof(context16
));
413 context16
.SegCs
= HIWORD(callTarget
);
414 context16
.Eip
= LOWORD(callTarget
);
415 context16
.Ebp
= OFFSETOF( NtCurrentTeb()->cur_stack
)
416 + (WORD
)&((STACK16FRAME
*)0)->bp
;
418 argsize
= context
->Ebp
-context
->Esp
-0x40;
419 newstack
= (LPBYTE
)CURRENT_STACK16
- argsize
;
420 oldstack
= (LPBYTE
)context
->Esp
;
422 memcpy( newstack
, oldstack
, argsize
);
424 for (i
= 0; i
< 32; i
++) /* NOTE: What about > 32 arguments? */
425 if (mapESPrelative
& (1 << i
))
427 SEGPTR
*arg
= (SEGPTR
*)(newstack
+ 2*i
);
428 *arg
= MAKESEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack
),
429 OFFSETOF(NtCurrentTeb()->cur_stack
) - argsize
430 + (*(LPBYTE
*)arg
- oldstack
));
433 wine_call_to_16_regs_short( &context16
, argsize
);
434 context
->Eax
= context16
.Eax
;
435 context
->Edx
= context16
.Edx
;
436 context
->Ecx
= context16
.Ecx
;
438 context
->Esp
+= LOWORD(context16
.Esp
) -
439 ( OFFSETOF( NtCurrentTeb()->cur_stack
) - argsize
);
441 /* Copy modified buffers back to 32-bit stack */
442 memcpy( oldstack
, newstack
, argsize
);
445 /**********************************************************************
446 * FT_ExitNN (KERNEL32.218 - 232)
448 * One of the FT_ExitNN functions is called at the end of the thunk code.
449 * It removes the stack frame created by FT_Prolog, moves the function
450 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
451 * value, but the thunk code has moved it from EAX to EBX in the
452 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
453 * and perform a return to the CALLER of the thunk code (while removing
454 * the given number of arguments from the caller's stack).
457 static void FT_Exit(CONTEXT86
*context
, int nPopArgs
)
459 /* Return value is in EBX */
460 context
->Eax
= context
->Ebx
;
462 /* Restore EBX, ESI, and EDI registers */
463 context
->Ebx
= *(DWORD
*)(context
->Ebp
- 4);
464 context
->Esi
= *(DWORD
*)(context
->Ebp
- 8);
465 context
->Edi
= *(DWORD
*)(context
->Ebp
- 12);
467 /* Clean up stack frame */
468 context
->Esp
= context
->Ebp
;
469 context
->Ebp
= stack32_pop(context
);
471 /* Pop return address to CALLER of thunk code */
472 context
->Eip
= stack32_pop(context
);
473 /* Remove arguments */
474 context
->Esp
+= nPopArgs
;
477 /***********************************************************************
478 * FT_Exit0 (KERNEL32.@)
480 void WINAPI
FT_Exit0 (CONTEXT86
*context
) { FT_Exit(context
, 0); }
482 /***********************************************************************
483 * FT_Exit4 (KERNEL32.@)
485 void WINAPI
FT_Exit4 (CONTEXT86
*context
) { FT_Exit(context
, 4); }
487 /***********************************************************************
488 * FT_Exit8 (KERNEL32.@)
490 void WINAPI
FT_Exit8 (CONTEXT86
*context
) { FT_Exit(context
, 8); }
492 /***********************************************************************
493 * FT_Exit12 (KERNEL32.@)
495 void WINAPI
FT_Exit12(CONTEXT86
*context
) { FT_Exit(context
, 12); }
497 /***********************************************************************
498 * FT_Exit16 (KERNEL32.@)
500 void WINAPI
FT_Exit16(CONTEXT86
*context
) { FT_Exit(context
, 16); }
502 /***********************************************************************
503 * FT_Exit20 (KERNEL32.@)
505 void WINAPI
FT_Exit20(CONTEXT86
*context
) { FT_Exit(context
, 20); }
507 /***********************************************************************
508 * FT_Exit24 (KERNEL32.@)
510 void WINAPI
FT_Exit24(CONTEXT86
*context
) { FT_Exit(context
, 24); }
512 /***********************************************************************
513 * FT_Exit28 (KERNEL32.@)
515 void WINAPI
FT_Exit28(CONTEXT86
*context
) { FT_Exit(context
, 28); }
517 /***********************************************************************
518 * FT_Exit32 (KERNEL32.@)
520 void WINAPI
FT_Exit32(CONTEXT86
*context
) { FT_Exit(context
, 32); }
522 /***********************************************************************
523 * FT_Exit36 (KERNEL32.@)
525 void WINAPI
FT_Exit36(CONTEXT86
*context
) { FT_Exit(context
, 36); }
527 /***********************************************************************
528 * FT_Exit40 (KERNEL32.@)
530 void WINAPI
FT_Exit40(CONTEXT86
*context
) { FT_Exit(context
, 40); }
532 /***********************************************************************
533 * FT_Exit44 (KERNEL32.@)
535 void WINAPI
FT_Exit44(CONTEXT86
*context
) { FT_Exit(context
, 44); }
537 /***********************************************************************
538 * FT_Exit48 (KERNEL32.@)
540 void WINAPI
FT_Exit48(CONTEXT86
*context
) { FT_Exit(context
, 48); }
542 /***********************************************************************
543 * FT_Exit52 (KERNEL32.@)
545 void WINAPI
FT_Exit52(CONTEXT86
*context
) { FT_Exit(context
, 52); }
547 /***********************************************************************
548 * FT_Exit56 (KERNEL32.@)
550 void WINAPI
FT_Exit56(CONTEXT86
*context
) { FT_Exit(context
, 56); }
552 /***********************************************************************
553 * ThunkInitLS (KERNEL32.43)
554 * A thunkbuffer link routine
555 * The thunkbuf looks like:
557 * 00: DWORD length ? don't know exactly
558 * 04: SEGPTR ptr ? where does it point to?
559 * The pointer ptr is written into the first DWORD of 'thunk'.
560 * (probably correctly implemented)
563 * segmented pointer to thunk?
565 DWORD WINAPI
ThunkInitLS(
566 LPDWORD thunk
, /* [in] win32 thunk */
567 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
568 DWORD len
, /* [in] thkbuffer length */
569 LPCSTR dll16
, /* [in] name of win16 dll */
570 LPCSTR dll32
/* [in] name of win32 dll (FIXME: not used?) */
574 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
579 *(DWORD
*)thunk
= addr
[1];
584 /***********************************************************************
585 * Common32ThkLS (KERNEL32.45)
587 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
588 * style thunks. The basic difference is that the parameter conversion
589 * is done completely on the *16-bit* side here. Thus we do not call
590 * the 16-bit target directly, but call a common entry point instead.
591 * This entry function then calls the target according to the target
592 * number passed in the DI register.
594 * Input: EAX SEGPTR to the common 16-bit entry point
595 * CX offset in thunk table (target number * 4)
596 * DX error return value if execution fails (unclear???)
597 * EDX.HI number of DWORD parameters
599 * (Note that we need to move the thunk table offset from CX to DI !)
601 * The called 16-bit stub expects its stack to look like this:
603 * (esp+40) 32-bit arguments
605 * (esp+8) 32 byte of stack space available as buffer
606 * (esp) 8 byte return address for use with 0x66 lret
608 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
609 * and uses the EAX register to return a DWORD return value.
610 * Thus we need to use a special assembly glue routine
611 * (CallRegisterLongProc instead of CallRegisterShortProc).
613 * Finally, we return to the caller, popping the arguments off
614 * the stack. The number of arguments to be popped is returned
615 * in the BL register by the called 16-bit routine.
618 void WINAPI
Common32ThkLS( CONTEXT86
*context
)
623 memcpy(&context16
,context
,sizeof(context16
));
625 context16
.Edi
= LOWORD(context
->Ecx
);
626 context16
.SegCs
= HIWORD(context
->Eax
);
627 context16
.Eip
= LOWORD(context
->Eax
);
628 context16
.Ebp
= OFFSETOF( NtCurrentTeb()->cur_stack
)
629 + (WORD
)&((STACK16FRAME
*)0)->bp
;
631 argsize
= HIWORD(context
->Edx
) * 4;
633 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
634 if (context
->Edx
== context
->Eip
)
637 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
638 (LPBYTE
)context
->Esp
, argsize
);
640 wine_call_to_16_regs_long(&context16
, argsize
+ 32);
641 context
->Eax
= context16
.Eax
;
643 /* Clean up caller's stack frame */
644 context
->Esp
+= BL_reg(&context16
);
647 /***********************************************************************
648 * OT_32ThkLSF (KERNEL32.40)
650 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
651 * argument processing is done on both the 32-bit and the 16-bit side:
652 * The 32-bit side prepares arguments, copying them onto the stack.
654 * When this routine is called, the first word on the stack is the
655 * number of argument bytes prepared by the 32-bit code, and EDX
656 * contains the 16-bit target address.
658 * The called 16-bit routine is another relaycode, doing further
659 * argument processing and then calling the real 16-bit target
660 * whose address is stored at [bp-04].
662 * The call proceeds using a normal CallRegisterShortProc.
663 * After return from the 16-bit relaycode, the arguments need
664 * to be copied *back* to the 32-bit stack, since the 32-bit
665 * relaycode processes output parameters.
667 * Note that we copy twice the number of arguments, since some of the
668 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
669 * arguments of the caller!
671 * (Note that this function seems only to be used for
672 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
674 void WINAPI
OT_32ThkLSF( CONTEXT86
*context
)
679 memcpy(&context16
,context
,sizeof(context16
));
681 context16
.SegCs
= HIWORD(context
->Edx
);
682 context16
.Eip
= LOWORD(context
->Edx
);
683 context16
.Ebp
= OFFSETOF( NtCurrentTeb()->cur_stack
)
684 + (WORD
)&((STACK16FRAME
*)0)->bp
;
686 argsize
= 2 * *(WORD
*)context
->Esp
+ 2;
688 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
689 (LPBYTE
)context
->Esp
, argsize
);
691 wine_call_to_16_regs_short(&context16
, argsize
);
692 context
->Eax
= context16
.Eax
;
693 context
->Edx
= context16
.Edx
;
695 /* Copy modified buffers back to 32-bit stack */
696 memcpy( (LPBYTE
)context
->Esp
,
697 (LPBYTE
)CURRENT_STACK16
- argsize
, argsize
);
699 context
->Esp
+= LOWORD(context16
.Esp
) -
700 ( OFFSETOF( NtCurrentTeb()->cur_stack
) - argsize
);
703 /***********************************************************************
704 * ThunkInitLSF (KERNEL32.41)
705 * A thunk setup routine.
706 * Expects a pointer to a preinitialized thunkbuffer in the first argument
708 * 00..03: unknown (pointer, check _41, _43, _46)
711 * 06..23: unknown (space for replacement code, check .90)
713 * 24:>E800000000 call offset 29
714 * 29:>58 pop eax ( target of call )
715 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
716 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
717 * 34: 68yyyyyyyy push KERNEL32.90
721 * 3E ... 59: unknown (space for replacement code?)
722 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
724 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
726 * 67: 68xxxxxxxx push xxxxxxxx
727 * 6C: 68yyyyyyyy push KERNEL32.89
730 * This function checks if the code is there, and replaces the yyyyyyyy entries
731 * by the functionpointers.
732 * The thunkbuf looks like:
734 * 00: DWORD length ? don't know exactly
735 * 04: SEGPTR ptr ? where does it point to?
736 * The segpointer ptr is written into the first DWORD of 'thunk'.
739 * unclear, pointer to win16 thkbuffer?
741 LPVOID WINAPI
ThunkInitLSF(
742 LPBYTE thunk
, /* [in] win32 thunk */
743 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
744 DWORD len
, /* [in] length of thkbuffer */
745 LPCSTR dll16
, /* [in] name of win16 dll */
746 LPCSTR dll32
/* [in] name of win32 dll */
748 HMODULE hkrnl32
= GetModuleHandleA("KERNEL32");
751 /* FIXME: add checks for valid code ... */
752 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
753 *(DWORD
*)(thunk
+0x35) = (DWORD
)GetProcAddress(hkrnl32
,(LPSTR
)90);
754 *(DWORD
*)(thunk
+0x6D) = (DWORD
)GetProcAddress(hkrnl32
,(LPSTR
)89);
757 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
760 addr2
= MapSL(addr
[1]);
762 *(DWORD
*)thunk
= (DWORD
)addr2
;
767 /***********************************************************************
768 * FT_PrologPrime (KERNEL32.89)
770 * This function is called from the relay code installed by
771 * ThunkInitLSF. It replaces the location from where it was
772 * called by a standard FT_Prolog call stub (which is 'primed'
773 * by inserting the correct target table pointer).
774 * Finally, it calls that stub.
776 * Input: ECX target number + flags (passed through to FT_Prolog)
777 * (ESP) offset of location where target table pointer
778 * is stored, relative to the start of the relay code
779 * (ESP+4) pointer to start of relay code
780 * (this is where the FT_Prolog call stub gets written to)
782 * Note: The two DWORD arguments get popped off the stack.
785 void WINAPI
FT_PrologPrime( CONTEXT86
*context
)
787 DWORD targetTableOffset
;
790 /* Compensate for the fact that the Wine register relay code thought
791 we were being called, although we were in fact jumped to */
794 /* Write FT_Prolog call stub */
795 targetTableOffset
= stack32_pop(context
);
796 relayCode
= (LPBYTE
)stack32_pop(context
);
797 _write_ftprolog( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
799 /* Jump to the call stub just created */
800 context
->Eip
= (DWORD
)relayCode
;
803 /***********************************************************************
804 * QT_ThunkPrime (KERNEL32.90)
806 * This function corresponds to FT_PrologPrime, but installs a
807 * call stub for QT_Thunk instead.
809 * Input: (EBP-4) target number (passed through to QT_Thunk)
810 * EDX target table pointer location offset
811 * EAX start of relay code
814 void WINAPI
QT_ThunkPrime( CONTEXT86
*context
)
816 DWORD targetTableOffset
;
819 /* Compensate for the fact that the Wine register relay code thought
820 we were being called, although we were in fact jumped to */
823 /* Write QT_Thunk call stub */
824 targetTableOffset
= context
->Edx
;
825 relayCode
= (LPBYTE
)context
->Eax
;
826 _write_qtthunk( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
828 /* Jump to the call stub just created */
829 context
->Eip
= (DWORD
)relayCode
;
832 /***********************************************************************
833 * ThunkInitSL (KERNEL32.46)
834 * Another thunkbuf link routine.
835 * The start of the thunkbuf looks like this:
837 * 04: SEGPTR address for thunkbuffer pointer
840 VOID WINAPI
ThunkInitSL(
841 LPBYTE thunk
, /* [in] start of thunkbuffer */
842 LPCSTR thkbuf
, /* [in] name/ordinal of thunkbuffer in win16 dll */
843 DWORD len
, /* [in] length of thunkbuffer */
844 LPCSTR dll16
, /* [in] name of win16 dll containing the thkbuf */
845 LPCSTR dll32
/* [in] win32 dll. FIXME: strange, unused */
849 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
852 *(DWORD
*)MapSL(addr
[1]) = (DWORD
)thunk
;
855 /**********************************************************************
856 * SSInit (KERNEL.700)
860 BOOL WINAPI
SSInit16()
865 /**********************************************************************
866 * SSOnBigStack (KERNEL32.87)
867 * Check if thunking is initialized (ss selector set up etc.)
868 * We do that differently, so just return TRUE.
873 BOOL WINAPI
SSOnBigStack()
875 TRACE("Yes, thunking is initialized\n");
879 /**********************************************************************
880 * SSConfirmSmallStack (KERNEL.704)
882 * Abort if not on small stack.
884 * This must be a register routine as it has to preserve *all* registers.
886 void WINAPI
SSConfirmSmallStack( CONTEXT86
*context
)
888 /* We are always on the small stack while in 16-bit code ... */
891 /**********************************************************************
892 * SSCall (KERNEL32.88)
893 * One of the real thunking functions. This one seems to be for 32<->32
894 * thunks. It should probably be capable of crossing processboundaries.
896 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
899 DWORD WINAPIV
SSCall(
900 DWORD nr
, /* [in] number of argument bytes */
901 DWORD flags
, /* [in] FIXME: flags ? */
902 FARPROC fun
, /* [in] function to call */
903 ... /* [in/out] arguments */
906 DWORD
*args
= ((DWORD
*)&fun
) + 1;
910 DPRINTF("(%ld,0x%08lx,%p,[",nr
,flags
,fun
);
912 DPRINTF("0x%08lx,",args
[i
]);
918 case 4: ret
= fun(args
[0]);
920 case 8: ret
= fun(args
[0],args
[1]);
922 case 12: ret
= fun(args
[0],args
[1],args
[2]);
924 case 16: ret
= fun(args
[0],args
[1],args
[2],args
[3]);
926 case 20: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4]);
928 case 24: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
930 case 28: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
932 case 32: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
934 case 36: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
936 case 40: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
938 case 44: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],args
[10]);
940 case 48: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9],args
[10],args
[11]);
943 WARN("Unsupported nr of arguments, %ld\n",nr
);
948 TRACE(" returning %ld ...\n",ret
);
952 /**********************************************************************
953 * W32S_BackTo32 (KERNEL32.51)
955 void WINAPI
W32S_BackTo32( CONTEXT86
*context
)
957 LPDWORD stack
= (LPDWORD
)context
->Esp
;
958 FARPROC proc
= (FARPROC
)context
->Eip
;
960 context
->Eax
= proc( stack
[1], stack
[2], stack
[3], stack
[4], stack
[5],
961 stack
[6], stack
[7], stack
[8], stack
[9], stack
[10] );
963 context
->Eip
= stack32_pop(context
);
966 /**********************************************************************
967 * AllocSLCallback (KERNEL32.@)
969 * Win95 uses some structchains for callbacks. It allocates them
970 * in blocks of 100 entries, size 32 bytes each, layout:
972 * 0: PTR nextblockstart
974 * 8: WORD sel ( start points to blockstart)
978 * 18: PDB *owning_process;
981 * We ignore this for now. (Just a note for further developers)
982 * FIXME: use this method, so we don't waste selectors...
984 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
985 * the 0x66 prefix switches from word->long registers.
988 * 6668x arg2 x pushl <arg2>
990 * EAx arg1 x jmpf <arg1>
992 * returns the startaddress of this thunk.
994 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
996 * segmented pointer to the start of the thunk
1000 DWORD finalizer
, /* [in] finalizer function */
1001 DWORD callback
/* [in] callback function */
1003 LPBYTE x
,thunk
= HeapAlloc( GetProcessHeap(), 0, 32 );
1007 *x
++=0x66;*x
++=0x5a; /* popl edx */
1008 *x
++=0x66;*x
++=0x68;*(DWORD
*)x
=finalizer
;x
+=4; /* pushl finalizer */
1009 *x
++=0x66;*x
++=0x52; /* pushl edx */
1010 *x
++=0xea;*(DWORD
*)x
=callback
;x
+=4; /* jmpf callback */
1012 *(DWORD
*)(thunk
+18) = GetCurrentProcessId();
1014 sel
= SELECTOR_AllocBlock( thunk
, 32, WINE_LDT_FLAGS_CODE
);
1018 /**********************************************************************
1019 * FreeSLCallback (KERNEL32.@)
1020 * Frees the specified 16->32 callback
1024 DWORD x
/* [in] 16 bit callback (segmented pointer?) */
1026 FIXME("(0x%08lx): stub\n",x
);
1030 /**********************************************************************
1031 * GetTEBSelectorFS (KERNEL.475)
1032 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1034 void WINAPI
GetTEBSelectorFS16(void)
1036 CURRENT_STACK16
->fs
= __get_fs();
1039 /**********************************************************************
1040 * IsPeFormat (KERNEL.431)
1041 * Checks the passed filename if it is a PE format executeable
1046 BOOL16 WINAPI
IsPeFormat16(
1047 LPSTR fn
, /* [in] filename to executeable */
1048 HFILE16 hf16
/* [in] open file, if filename is NULL */
1051 IMAGE_DOS_HEADER mzh
;
1055 if (fn
) hf16
= OpenFile16(fn
,&ofs
,OF_READ
);
1056 if (hf16
== HFILE_ERROR16
) return FALSE
;
1057 _llseek16(hf16
,0,SEEK_SET
);
1058 if (sizeof(mzh
)!=_lread16(hf16
,&mzh
,sizeof(mzh
))) goto done
;
1059 if (mzh
.e_magic
!=IMAGE_DOS_SIGNATURE
) goto done
;
1060 _llseek16(hf16
,mzh
.e_lfanew
,SEEK_SET
);
1061 if (sizeof(DWORD
)!=_lread16(hf16
,&xmagic
,sizeof(DWORD
))) goto done
;
1062 ret
= (xmagic
== IMAGE_NT_SIGNATURE
);
1069 /***********************************************************************
1070 * K32Thk1632Prolog (KERNEL32.@)
1072 void WINAPI
K32Thk1632Prolog( CONTEXT86
*context
)
1074 LPBYTE code
= (LPBYTE
)context
->Eip
- 5;
1076 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1077 of 16->32 thunks instead of using one of the standard methods!
1078 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1079 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1080 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1081 bypassed, which means it will crash the next time the 32-bit OLE
1082 code thunks down again to 16-bit (this *will* happen!).
1084 The following hack tries to recognize this situation.
1085 This is possible since the called stubs in OLECLI32/OLESVR32 all
1086 look exactly the same:
1087 00 E8xxxxxxxx call K32Thk1632Prolog
1088 05 FF55FC call [ebp-04]
1089 08 E8xxxxxxxx call K32Thk1632Epilog
1092 If we recognize this situation, we try to simulate the actions
1093 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1094 to our 32-bit stack, creating a proper STACK16FRAME and
1095 updating cur_stack. */
1097 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1098 && code
[13] == 0x66 && code
[14] == 0xCB)
1100 WORD stackSel
= NtCurrentTeb()->stack_sel
;
1101 DWORD stackBase
= GetSelectorBase(stackSel
);
1103 DWORD argSize
= context
->Ebp
- context
->Esp
;
1104 char *stack16
= (char *)context
->Esp
- 4;
1105 char *stack32
= (char *)NtCurrentTeb()->cur_stack
- argSize
;
1106 STACK16FRAME
*frame16
= (STACK16FRAME
*)stack16
- 1;
1108 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1109 context
->Ebp
, context
->Esp
, NtCurrentTeb()->cur_stack
);
1111 memset(frame16
, '\0', sizeof(STACK16FRAME
));
1112 frame16
->frame32
= (STACK32FRAME
*)NtCurrentTeb()->cur_stack
;
1113 frame16
->ebp
= context
->Ebp
;
1115 memcpy(stack32
, stack16
, argSize
);
1116 NtCurrentTeb()->cur_stack
= MAKESEGPTR(stackSel
, (DWORD
)frame16
- stackBase
);
1118 context
->Esp
= (DWORD
)stack32
+ 4;
1119 context
->Ebp
= context
->Esp
+ argSize
;
1121 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1122 context
->Ebp
, context
->Esp
, NtCurrentTeb()->cur_stack
);
1125 /* entry_point is never used again once the entry point has
1126 been called. Thus we re-use it to hold the Win16Lock count */
1127 ReleaseThunkLock(&CURRENT_STACK16
->entry_point
);
1130 /***********************************************************************
1131 * K32Thk1632Epilog (KERNEL32.@)
1133 void WINAPI
K32Thk1632Epilog( CONTEXT86
*context
)
1135 LPBYTE code
= (LPBYTE
)context
->Eip
- 13;
1137 RestoreThunkLock(CURRENT_STACK16
->entry_point
);
1139 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1141 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1142 && code
[13] == 0x66 && code
[14] == 0xCB)
1144 STACK16FRAME
*frame16
= MapSL(NtCurrentTeb()->cur_stack
);
1145 char *stack16
= (char *)(frame16
+ 1);
1146 DWORD argSize
= frame16
->ebp
- (DWORD
)stack16
;
1147 char *stack32
= (char *)frame16
->frame32
- argSize
;
1149 DWORD nArgsPopped
= context
->Esp
- (DWORD
)stack32
;
1151 TRACE("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1152 context
->Ebp
, context
->Esp
, NtCurrentTeb()->cur_stack
);
1154 NtCurrentTeb()->cur_stack
= (DWORD
)frame16
->frame32
;
1156 context
->Esp
= (DWORD
)stack16
+ nArgsPopped
;
1157 context
->Ebp
= frame16
->ebp
;
1159 TRACE("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1160 context
->Ebp
, context
->Esp
, NtCurrentTeb()->cur_stack
);
1164 /*********************************************************************
1165 * PK16FNF [KERNEL32.91]
1167 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1168 * string buffer with the 8.3 filename of a recently loaded 16-bit
1169 * module. It is unknown exactly what modules trigger this
1170 * mechanism or what purpose this serves. Win98 Explorer (and
1171 * probably also Win95 with IE 4 shell integration) calls this
1172 * several times during initialization.
1174 * FIXME: find out what this really does and make it work.
1176 void WINAPI
PK16FNF(LPSTR strPtr
)
1178 FIXME("(%p): stub\n", strPtr
);
1180 /* fill in a fake filename that'll be easy to recognize */
1181 strcpy(strPtr
, "WINESTUB.FIX");
1184 /***********************************************************************
1185 * 16->32 Flat Thunk routines:
1188 /***********************************************************************
1189 * ThunkConnect16 (KERNEL.651)
1190 * Connects a 32bit and a 16bit thunkbuffer.
1192 UINT WINAPI
ThunkConnect16(
1193 LPSTR module16
, /* [in] name of win16 dll */
1194 LPSTR module32
, /* [in] name of win32 dll */
1195 HINSTANCE16 hInst16
, /* [in] hInst of win16 dll */
1196 DWORD dwReason
, /* [in] initialisation argument */
1197 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
1198 LPSTR thunkfun32
, /* [in] win32 thunkfunction */
1199 WORD cs
/* [in] CS of win16 dll */
1203 if (!strncmp(TD
->magic
, "SL01", 4))
1207 TRACE("SL01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
1208 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
1210 else if (!strncmp(TD
->magic
, "LS01", 4))
1212 directionSL
= FALSE
;
1214 TRACE("LS01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
1215 module16
, (DWORD
)TD
, module32
, thunkfun32
, dwReason
);
1219 ERR("Invalid magic %c%c%c%c\n",
1220 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
1226 case DLL_PROCESS_ATTACH
:
1229 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD
;
1230 struct ThunkDataSL
*SL
= SL16
->fpData
;
1234 SL
= HeapAlloc(GetProcessHeap(), 0, sizeof(*SL
));
1236 SL
->common
= SL16
->common
;
1237 SL
->flags1
= SL16
->flags1
;
1238 SL
->flags2
= SL16
->flags2
;
1240 SL
->apiDB
= MapSL(SL16
->apiDatabase
);
1241 SL
->targetDB
= NULL
;
1243 lstrcpynA(SL
->pszDll16
, module16
, 255);
1244 lstrcpynA(SL
->pszDll32
, module32
, 255);
1246 /* We should create a SEGPTR to the ThunkDataSL,
1247 but since the contents are not in the original format,
1248 any access to this by 16-bit code would crash anyway. */
1254 if (SL
->flags2
& 0x80000000)
1256 TRACE("Preloading 32-bit library\n");
1257 LoadLibraryA(module32
);
1266 case DLL_PROCESS_DETACH
:
1267 /* FIXME: cleanup */
1275 /***********************************************************************
1276 * C16ThkSL (KERNEL.630)
1279 void WINAPI
C16ThkSL(CONTEXT86
*context
)
1281 LPBYTE stub
= MapSL(context
->Eax
), x
= stub
;
1282 WORD cs
= __get_cs();
1283 WORD ds
= __get_ds();
1285 /* We produce the following code:
1290 * mov edx, es:[ecx + $EDX]
1295 * call __FLATCS:__wine_call_from_16_thunk
1298 *x
++ = 0xB8; *((WORD
*)x
)++ = ds
;
1299 *x
++ = 0x8E; *x
++ = 0xC0;
1300 *x
++ = 0x66; *x
++ = 0x0F; *x
++ = 0xB7; *x
++ = 0xC9;
1301 *x
++ = 0x67; *x
++ = 0x66; *x
++ = 0x26; *x
++ = 0x8B;
1302 *x
++ = 0x91; *((DWORD
*)x
)++ = context
->Edx
;
1305 *x
++ = 0x66; *x
++ = 0x52;
1307 *x
++ = 0x66; *x
++ = 0x52;
1308 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)__wine_call_from_16_thunk
;
1309 *((WORD
*)x
)++ = cs
;
1311 /* Jump to the stub code just created */
1312 context
->Eip
= LOWORD(context
->Eax
);
1313 context
->SegCs
= HIWORD(context
->Eax
);
1315 /* Since C16ThkSL got called by a jmp, we need to leave the
1316 original return address on the stack */
1320 /***********************************************************************
1321 * C16ThkSL01 (KERNEL.631)
1324 void WINAPI
C16ThkSL01(CONTEXT86
*context
)
1326 LPBYTE stub
= MapSL(context
->Eax
), x
= stub
;
1330 struct ThunkDataSL16
*SL16
= MapSL(context
->Edx
);
1331 struct ThunkDataSL
*td
= SL16
->fpData
;
1333 DWORD procAddress
= (DWORD
)GetProcAddress16(GetModuleHandle16("KERNEL"), (LPCSTR
)631);
1334 WORD cs
= __get_cs();
1338 ERR("ThunkConnect16 was not called!\n");
1342 TRACE("Creating stub for ThunkDataSL %08lx\n", (DWORD
)td
);
1345 /* We produce the following code:
1354 * call __FLATCS:__wine_call_from_16_thunk
1357 *x
++ = 0x66; *x
++ = 0x33; *x
++ = 0xC0;
1358 *x
++ = 0x66; *x
++ = 0xBA; *((DWORD
*)x
)++ = (DWORD
)td
;
1359 *x
++ = 0x9A; *((DWORD
*)x
)++ = procAddress
;
1362 *x
++ = 0x66; *x
++ = 0x52;
1364 *x
++ = 0x66; *x
++ = 0x52;
1365 *x
++ = 0x66; *x
++ = 0x9A; *((DWORD
*)x
)++ = (DWORD
)__wine_call_from_16_thunk
;
1366 *((WORD
*)x
)++ = cs
;
1368 /* Jump to the stub code just created */
1369 context
->Eip
= LOWORD(context
->Eax
);
1370 context
->SegCs
= HIWORD(context
->Eax
);
1372 /* Since C16ThkSL01 got called by a jmp, we need to leave the
1373 orginal return address on the stack */
1378 struct ThunkDataSL
*td
= (struct ThunkDataSL
*)context
->Edx
;
1379 DWORD targetNr
= CX_reg(context
) / 4;
1380 struct SLTargetDB
*tdb
;
1382 TRACE("Process %08lx calling target %ld of ThunkDataSL %08lx\n",
1383 GetCurrentProcessId(), targetNr
, (DWORD
)td
);
1385 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1386 if (tdb
->process
== GetCurrentProcessId())
1391 TRACE("Loading 32-bit library %s\n", td
->pszDll32
);
1392 LoadLibraryA(td
->pszDll32
);
1394 for (tdb
= td
->targetDB
; tdb
; tdb
= tdb
->next
)
1395 if (tdb
->process
== GetCurrentProcessId())
1401 context
->Edx
= tdb
->targetTable
[targetNr
];
1403 TRACE("Call target is %08lx\n", context
->Edx
);
1407 WORD
*stack
= MapSL( MAKESEGPTR(context
->SegSs
, LOWORD(context
->Esp
)) );
1408 DX_reg(context
) = HIWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1409 AX_reg(context
) = LOWORD(td
->apiDB
[targetNr
].errorReturnValue
);
1410 context
->Eip
= stack
[2];
1411 context
->SegCs
= stack
[3];
1412 context
->Esp
+= td
->apiDB
[targetNr
].nrArgBytes
+ 4;
1414 ERR("Process %08lx did not ThunkConnect32 %s to %s\n",
1415 GetCurrentProcessId(), td
->pszDll32
, td
->pszDll16
);
1421 /***********************************************************************
1422 * 16<->32 Thunklet/Callback API:
1425 #include "pshpack1.h"
1426 typedef struct _THUNKLET
1441 struct _THUNKLET
*next
;
1443 #include "poppack.h"
1445 #define THUNKLET_TYPE_LS 1
1446 #define THUNKLET_TYPE_SL 2
1448 static HANDLE ThunkletHeap
= 0;
1449 static THUNKLET
*ThunkletAnchor
= NULL
;
1451 static FARPROC ThunkletSysthunkGlueLS
= 0;
1452 static SEGPTR ThunkletSysthunkGlueSL
= 0;
1454 static FARPROC ThunkletCallbackGlueLS
= 0;
1455 static SEGPTR ThunkletCallbackGlueSL
= 0;
1457 /***********************************************************************
1460 BOOL
THUNK_Init(void)
1464 ThunkletHeap
= HeapCreate(HEAP_WINE_SEGPTR
| HEAP_WINE_CODE16SEG
, 0, 0);
1465 if (!ThunkletHeap
) return FALSE
;
1467 thunk
= HeapAlloc( ThunkletHeap
, 0, 5 );
1468 if (!thunk
) return FALSE
;
1470 ThunkletSysthunkGlueLS
= (FARPROC
)thunk
;
1471 *thunk
++ = 0x58; /* popl eax */
1472 *thunk
++ = 0xC3; /* ret */
1474 ThunkletSysthunkGlueSL
= HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1475 *thunk
++ = 0x66; *thunk
++ = 0x58; /* popl eax */
1476 *thunk
++ = 0xCB; /* lret */
1481 /***********************************************************************
1482 * SetThunkletCallbackGlue (KERNEL.560)
1484 void WINAPI
SetThunkletCallbackGlue16( FARPROC glueLS
, SEGPTR glueSL
)
1486 ThunkletCallbackGlueLS
= glueLS
;
1487 ThunkletCallbackGlueSL
= glueSL
;
1491 /***********************************************************************
1492 * THUNK_FindThunklet
1494 THUNKLET
*THUNK_FindThunklet( DWORD target
, DWORD relay
,
1495 DWORD glue
, BYTE type
)
1499 for (thunk
= ThunkletAnchor
; thunk
; thunk
= thunk
->next
)
1500 if ( thunk
->type
== type
1501 && thunk
->target
== target
1502 && thunk
->relay
== relay
1503 && ( type
== THUNKLET_TYPE_LS
?
1504 ( thunk
->glue
== glue
- (DWORD
)&thunk
->type
)
1505 : ( thunk
->glue
== glue
) ) )
1511 /***********************************************************************
1512 * THUNK_AllocLSThunklet
1514 FARPROC
THUNK_AllocLSThunklet( SEGPTR target
, DWORD relay
,
1515 FARPROC glue
, HTASK16 owner
)
1517 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1521 TDB
*pTask
= TASK_GetPtr( owner
);
1523 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1526 thunk
->prefix_target
= thunk
->prefix_relay
= 0x90;
1527 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1528 thunk
->jmp_glue
= 0xE9;
1530 thunk
->target
= (DWORD
)target
;
1531 thunk
->relay
= (DWORD
)relay
;
1532 thunk
->glue
= (DWORD
)glue
- (DWORD
)&thunk
->type
;
1534 thunk
->type
= THUNKLET_TYPE_LS
;
1535 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1537 thunk
->next
= ThunkletAnchor
;
1538 ThunkletAnchor
= thunk
;
1541 return (FARPROC
)thunk
;
1544 /***********************************************************************
1545 * THUNK_AllocSLThunklet
1547 SEGPTR
THUNK_AllocSLThunklet( FARPROC target
, DWORD relay
,
1548 SEGPTR glue
, HTASK16 owner
)
1550 THUNKLET
*thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
, (DWORD
)glue
,
1554 TDB
*pTask
= TASK_GetPtr( owner
);
1556 if ( !(thunk
= HeapAlloc( ThunkletHeap
, 0, sizeof(THUNKLET
) )) )
1559 thunk
->prefix_target
= thunk
->prefix_relay
= 0x66;
1560 thunk
->pushl_target
= thunk
->pushl_relay
= 0x68;
1561 thunk
->jmp_glue
= 0xEA;
1563 thunk
->target
= (DWORD
)target
;
1564 thunk
->relay
= (DWORD
)relay
;
1565 thunk
->glue
= (DWORD
)glue
;
1567 thunk
->type
= THUNKLET_TYPE_SL
;
1568 thunk
->owner
= pTask
? pTask
->hInstance
: 0;
1570 thunk
->next
= ThunkletAnchor
;
1571 ThunkletAnchor
= thunk
;
1574 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1577 /**********************************************************************
1580 BOOL16 WINAPI
IsLSThunklet( THUNKLET
*thunk
)
1582 return thunk
->prefix_target
== 0x90 && thunk
->pushl_target
== 0x68
1583 && thunk
->prefix_relay
== 0x90 && thunk
->pushl_relay
== 0x68
1584 && thunk
->jmp_glue
== 0xE9 && thunk
->type
== THUNKLET_TYPE_LS
;
1587 /**********************************************************************
1588 * IsSLThunklet (KERNEL.612)
1590 BOOL16 WINAPI
IsSLThunklet16( THUNKLET
*thunk
)
1592 return thunk
->prefix_target
== 0x66 && thunk
->pushl_target
== 0x68
1593 && thunk
->prefix_relay
== 0x66 && thunk
->pushl_relay
== 0x68
1594 && thunk
->jmp_glue
== 0xEA && thunk
->type
== THUNKLET_TYPE_SL
;
1599 /***********************************************************************
1600 * AllocLSThunkletSysthunk (KERNEL.607)
1602 FARPROC WINAPI
AllocLSThunkletSysthunk16( SEGPTR target
,
1603 FARPROC relay
, DWORD dummy
)
1605 return THUNK_AllocLSThunklet( (SEGPTR
)relay
, (DWORD
)target
,
1606 ThunkletSysthunkGlueLS
, GetCurrentTask() );
1609 /***********************************************************************
1610 * AllocSLThunkletSysthunk (KERNEL.608)
1612 SEGPTR WINAPI
AllocSLThunkletSysthunk16( FARPROC target
,
1613 SEGPTR relay
, DWORD dummy
)
1615 return THUNK_AllocSLThunklet( (FARPROC
)relay
, (DWORD
)target
,
1616 ThunkletSysthunkGlueSL
, GetCurrentTask() );
1620 /***********************************************************************
1621 * AllocLSThunkletCallbackEx (KERNEL.567)
1623 FARPROC WINAPI
AllocLSThunkletCallbackEx16( SEGPTR target
,
1624 DWORD relay
, HTASK16 task
)
1626 THUNKLET
*thunk
= MapSL( target
);
1627 if ( !thunk
) return NULL
;
1629 if ( IsSLThunklet16( thunk
) && thunk
->relay
== relay
1630 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1631 return (FARPROC
)thunk
->target
;
1633 return THUNK_AllocLSThunklet( target
, relay
,
1634 ThunkletCallbackGlueLS
, task
);
1637 /***********************************************************************
1638 * AllocSLThunkletCallbackEx (KERNEL.568)
1640 SEGPTR WINAPI
AllocSLThunkletCallbackEx16( FARPROC target
,
1641 DWORD relay
, HTASK16 task
)
1643 THUNKLET
*thunk
= (THUNKLET
*)target
;
1644 if ( !thunk
) return 0;
1646 if ( IsLSThunklet( thunk
) && thunk
->relay
== relay
1647 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1648 return (SEGPTR
)thunk
->target
;
1650 return THUNK_AllocSLThunklet( target
, relay
,
1651 ThunkletCallbackGlueSL
, task
);
1654 /***********************************************************************
1655 * AllocLSThunkletCallback (KERNEL.561)
1656 * AllocLSThunkletCallback_dup (KERNEL.606)
1658 FARPROC WINAPI
AllocLSThunkletCallback16( SEGPTR target
, DWORD relay
)
1660 return AllocLSThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1663 /***********************************************************************
1664 * AllocSLThunkletCallback (KERNEL.562)
1665 * AllocSLThunkletCallback_dup (KERNEL.605)
1667 SEGPTR WINAPI
AllocSLThunkletCallback16( FARPROC target
, DWORD relay
)
1669 return AllocSLThunkletCallbackEx16( target
, relay
, GetCurrentTask() );
1672 /***********************************************************************
1673 * FindLSThunkletCallback (KERNEL.563)
1674 * FindLSThunkletCallback_dup (KERNEL.609)
1676 FARPROC WINAPI
FindLSThunkletCallback( SEGPTR target
, DWORD relay
)
1678 THUNKLET
*thunk
= MapSL( target
);
1679 if ( thunk
&& IsSLThunklet16( thunk
) && thunk
->relay
== relay
1680 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueSL
)
1681 return (FARPROC
)thunk
->target
;
1683 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1684 (DWORD
)ThunkletCallbackGlueLS
,
1686 return (FARPROC
)thunk
;
1689 /***********************************************************************
1690 * FindSLThunkletCallback (KERNEL.564)
1691 * FindSLThunkletCallback_dup (KERNEL.610)
1693 SEGPTR WINAPI
FindSLThunkletCallback( FARPROC target
, DWORD relay
)
1695 THUNKLET
*thunk
= (THUNKLET
*)target
;
1696 if ( thunk
&& IsLSThunklet( thunk
) && thunk
->relay
== relay
1697 && thunk
->glue
== (DWORD
)ThunkletCallbackGlueLS
- (DWORD
)&thunk
->type
)
1698 return (SEGPTR
)thunk
->target
;
1700 thunk
= THUNK_FindThunklet( (DWORD
)target
, relay
,
1701 (DWORD
)ThunkletCallbackGlueSL
,
1703 return HEAP_GetSegptr( ThunkletHeap
, 0, thunk
);
1707 /***********************************************************************
1708 * FreeThunklet (KERNEL.611)
1710 BOOL16 WINAPI
FreeThunklet16( DWORD unused1
, DWORD unused2
)
1716 /***********************************************************************
1717 * Callback Client API
1720 #define N_CBC_FIXED 20
1721 #define N_CBC_VARIABLE 10
1722 #define N_CBC_TOTAL (N_CBC_FIXED + N_CBC_VARIABLE)
1724 static SEGPTR CBClientRelay16
[ N_CBC_TOTAL
];
1725 static FARPROC
*CBClientRelay32
[ N_CBC_TOTAL
];
1727 /***********************************************************************
1728 * RegisterCBClient (KERNEL.619)
1730 INT16 WINAPI
RegisterCBClient16( INT16 wCBCId
,
1731 SEGPTR relay16
, FARPROC
*relay32
)
1733 /* Search for free Callback ID */
1735 for ( wCBCId
= N_CBC_FIXED
; wCBCId
< N_CBC_TOTAL
; wCBCId
++ )
1736 if ( !CBClientRelay16
[ wCBCId
] )
1739 /* Register Callback ID */
1740 if ( wCBCId
> 0 && wCBCId
< N_CBC_TOTAL
)
1742 CBClientRelay16
[ wCBCId
] = relay16
;
1743 CBClientRelay32
[ wCBCId
] = relay32
;
1751 /***********************************************************************
1752 * UnRegisterCBClient (KERNEL.622)
1754 INT16 WINAPI
UnRegisterCBClient16( INT16 wCBCId
,
1755 SEGPTR relay16
, FARPROC
*relay32
)
1757 if ( wCBCId
>= N_CBC_FIXED
&& wCBCId
< N_CBC_TOTAL
1758 && CBClientRelay16
[ wCBCId
] == relay16
1759 && CBClientRelay32
[ wCBCId
] == relay32
)
1761 CBClientRelay16
[ wCBCId
] = 0;
1762 CBClientRelay32
[ wCBCId
] = 0;
1771 /***********************************************************************
1772 * InitCBClient (KERNEL.623)
1774 void WINAPI
InitCBClient16( FARPROC glueLS
)
1776 HMODULE16 kernel
= GetModuleHandle16( "KERNEL" );
1777 SEGPTR glueSL
= (SEGPTR
)GetProcAddress16( kernel
, (LPCSTR
)604 );
1779 SetThunkletCallbackGlue16( glueLS
, glueSL
);
1782 /***********************************************************************
1783 * CBClientGlueSL (KERNEL.604)
1785 void WINAPI
CBClientGlueSL( CONTEXT86
*context
)
1787 /* Create stack frame */
1788 SEGPTR stackSeg
= stack16_push( 12 );
1789 LPWORD stackLin
= MapSL( stackSeg
);
1790 SEGPTR glue
, *glueTab
;
1792 stackLin
[3] = BP_reg( context
);
1793 stackLin
[2] = SI_reg( context
);
1794 stackLin
[1] = DI_reg( context
);
1795 stackLin
[0] = context
->SegDs
;
1797 context
->Ebp
= OFFSETOF( stackSeg
) + 6;
1798 context
->Esp
= OFFSETOF( stackSeg
) - 4;
1801 /* Jump to 16-bit relay code */
1802 glueTab
= MapSL( CBClientRelay16
[ stackLin
[5] ] );
1803 glue
= glueTab
[ stackLin
[4] ];
1804 context
->SegCs
= SELECTOROF( glue
);
1805 context
->Eip
= OFFSETOF ( glue
);
1808 /***********************************************************************
1809 * CBClientThunkSL (KERNEL.620)
1811 extern DWORD
CALL32_CBClient( FARPROC proc
, LPWORD args
, DWORD
*esi
);
1812 void WINAPI
CBClientThunkSL( CONTEXT86
*context
)
1814 /* Call 32-bit relay code */
1816 LPWORD args
= MapSL( MAKESEGPTR( context
->SegSs
, BP_reg( context
) ) );
1817 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1819 context
->Eax
= CALL32_CBClient( proc
, args
, &context
->Esi
);
1822 /***********************************************************************
1823 * CBClientThunkSLEx (KERNEL.621)
1825 extern DWORD
CALL32_CBClientEx( FARPROC proc
, LPWORD args
, DWORD
*esi
, INT
*nArgs
);
1826 void WINAPI
CBClientThunkSLEx( CONTEXT86
*context
)
1828 /* Call 32-bit relay code */
1830 LPWORD args
= MapSL( MAKESEGPTR( context
->SegSs
, BP_reg( context
) ) );
1831 FARPROC proc
= CBClientRelay32
[ args
[2] ][ args
[1] ];
1835 context
->Eax
= CALL32_CBClientEx( proc
, args
, &context
->Esi
, &nArgs
);
1837 /* Restore registers saved by CBClientGlueSL */
1838 stackLin
= (LPWORD
)((LPBYTE
)CURRENT_STACK16
+ sizeof(STACK16FRAME
) - 4);
1839 BP_reg( context
) = stackLin
[3];
1840 SI_reg( context
) = stackLin
[2];
1841 DI_reg( context
) = stackLin
[1];
1842 context
->SegDs
= stackLin
[0];
1843 context
->Esp
+= 16+nArgs
;
1845 /* Return to caller of CBClient thunklet */
1846 context
->SegCs
= stackLin
[9];
1847 context
->Eip
= stackLin
[8];
1851 /***********************************************************************
1852 * Get16DLLAddress (KERNEL32.@)
1854 * This function is used by a Win32s DLL if it wants to call a Win16 function.
1855 * A 16:16 segmented pointer to the function is returned.
1856 * Written without any docu.
1858 SEGPTR WINAPI
Get16DLLAddress(HMODULE handle
, LPSTR func_name
) {
1859 HANDLE ThunkHeap
= HeapCreate(HEAP_WINE_SEGPTR
| HEAP_WINE_CODESEG
, 0, 64);
1861 LPVOID tmpheap
= HeapAlloc(ThunkHeap
, 0, 32);
1862 SEGPTR thunk
= HEAP_GetSegptr(ThunkHeap
, 0, tmpheap
);
1865 if (!handle
) handle
=GetModuleHandle16("WIN32S16");
1866 proc_16
= (DWORD
)GetProcAddress16(handle
, func_name
);
1869 *x
++=0xba; *(DWORD
*)x
=proc_16
;x
+=4; /* movl proc_16, $edx */
1870 *x
++=0xea; *(DWORD
*)x
=(DWORD
)GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");x
+=4; /* jmpl QT_Thunk */
1871 *(WORD
*)x
=__get_cs();
1876 /***********************************************************************
1877 * GetWin16DOSEnv (KERNEL32.34)
1878 * Returns some internal value.... probably the default environment database?
1880 DWORD WINAPI
GetWin16DOSEnv()
1882 FIXME("stub, returning 0\n");
1886 /**********************************************************************
1887 * GetPK16SysVar (KERNEL32.92)
1889 LPVOID WINAPI
GetPK16SysVar(void)
1891 static BYTE PK16SysVar
[128];
1897 /**********************************************************************
1898 * CommonUnimpStub (KERNEL32.17)
1900 void WINAPI
CommonUnimpStub( CONTEXT86
*context
)
1903 MESSAGE( "*** Unimplemented Win32 API: %s\n", (LPSTR
)context
->Eax
);
1905 switch ((context
->Ecx
>> 4) & 0x0f)
1907 case 15: context
->Eax
= -1; break;
1908 case 14: context
->Eax
= 0x78; break;
1909 case 13: context
->Eax
= 0x32; break;
1910 case 1: context
->Eax
= 1; break;
1911 default: context
->Eax
= 0; break;
1914 context
->Esp
+= (context
->Ecx
& 0x0f) * 4;
1917 /**********************************************************************
1918 * HouseCleanLogicallyDeadHandles (KERNEL32.33)
1920 void WINAPI
HouseCleanLogicallyDeadHandles(void)
1922 /* Whatever this is supposed to do, our handles probably
1923 don't need it :-) */
1926 /**********************************************************************
1929 BOOL WINAPI
_KERNEL32_100(HANDLE threadid
,DWORD exitcode
,DWORD x
)
1931 FIXME("(%d,%ld,0x%08lx): stub\n",threadid
,exitcode
,x
);
1935 /**********************************************************************
1938 * Checks whether the clock has to be switched from daylight
1939 * savings time to standard time or vice versa.
1942 DWORD WINAPI
_KERNEL32_99(DWORD x
)
1944 FIXME("(0x%08lx): stub\n",x
);
1949 /**********************************************************************
1952 * Real prototype is:
1953 * INT16 WINAPI Catch( LPCATCHBUF lpbuf );
1955 void WINAPI
Catch16( LPCATCHBUF lpbuf
, CONTEXT86
*context
)
1957 /* Note: we don't save the current ss, as the catch buffer is */
1958 /* only 9 words long. Hopefully no one will have the silly */
1959 /* idea to change the current stack before calling Throw()... */
1973 lpbuf
[0] = LOWORD(context
->Eip
);
1974 lpbuf
[1] = context
->SegCs
;
1975 /* Windows pushes 4 more words before saving sp */
1976 lpbuf
[2] = LOWORD(context
->Esp
) - 4 * sizeof(WORD
);
1977 lpbuf
[3] = LOWORD(context
->Ebp
);
1978 lpbuf
[4] = LOWORD(context
->Esi
);
1979 lpbuf
[5] = LOWORD(context
->Edi
);
1980 lpbuf
[6] = context
->SegDs
;
1982 lpbuf
[8] = context
->SegSs
;
1983 AX_reg(context
) = 0; /* Return 0 */
1987 /**********************************************************************
1990 * Real prototype is:
1991 * INT16 WINAPI Throw( LPCATCHBUF lpbuf, INT16 retval );
1993 void WINAPI
Throw16( LPCATCHBUF lpbuf
, INT16 retval
, CONTEXT86
*context
)
1995 STACK16FRAME
*pFrame
;
1996 STACK32FRAME
*frame32
;
1997 TEB
*teb
= NtCurrentTeb();
1999 AX_reg(context
) = retval
;
2001 /* Find the frame32 corresponding to the frame16 we are jumping to */
2002 pFrame
= THREAD_STACK16(teb
);
2003 frame32
= pFrame
->frame32
;
2004 while (frame32
&& frame32
->frame16
)
2006 if (OFFSETOF(frame32
->frame16
) < OFFSETOF(teb
->cur_stack
))
2007 break; /* Something strange is going on */
2008 if (OFFSETOF(frame32
->frame16
) > lpbuf
[2])
2010 /* We found the right frame */
2011 pFrame
->frame32
= frame32
;
2014 frame32
= ((STACK16FRAME
*)MapSL(frame32
->frame16
))->frame32
;
2017 context
->Eip
= lpbuf
[0];
2018 context
->SegCs
= lpbuf
[1];
2019 context
->Esp
= lpbuf
[2] + 4 * sizeof(WORD
) - sizeof(WORD
) /*extra arg*/;
2020 context
->Ebp
= lpbuf
[3];
2021 context
->Esi
= lpbuf
[4];
2022 context
->Edi
= lpbuf
[5];
2023 context
->SegDs
= lpbuf
[6];
2025 if (lpbuf
[8] != context
->SegSs
)
2026 ERR("Switching stack segment with Throw() not supported; expect crash now\n" );