2 * KERNEL32 thunks and other undocumented stuff
4 * Copyright 1997-1998 Marcus Meissner
5 * Copyright 1998 Ulrich Weigand
13 #include "wine/winbase16.h"
21 #include "stackframe.h"
23 #include "selectors.h"
26 #include "debugtools.h"
27 #include "flatthunk.h"
31 DECLARE_DEBUG_CHANNEL(dosmem
)
32 DECLARE_DEBUG_CHANNEL(thunk
)
33 DECLARE_DEBUG_CHANNEL(win32
)
36 /***********************************************************************
38 * Win95 internal thunks *
40 ***********************************************************************/
42 /***********************************************************************
43 * LogApiThk (KERNEL.423)
45 void WINAPI
LogApiThk( LPSTR func
)
47 TRACE_(thunk
)( "%s\n", debugstr_a(func
) );
50 /***********************************************************************
51 * LogApiThkLSF (KERNEL32.42)
53 * NOTE: needs to preserve all registers!
55 void WINAPI
REGS_FUNC(LogApiThkLSF
)( LPSTR func
, CONTEXT
*context
)
57 TRACE_(thunk
)( "%s\n", debugstr_a(func
) );
60 /***********************************************************************
61 * LogApiThkSL (KERNEL32.44)
63 * NOTE: needs to preserve all registers!
65 void WINAPI
REGS_FUNC(LogApiThkSL
)( LPSTR func
, CONTEXT
*context
)
67 TRACE_(thunk
)( "%s\n", debugstr_a(func
) );
70 /***********************************************************************
71 * LogCBThkSL (KERNEL32.47)
73 * NOTE: needs to preserve all registers!
75 void WINAPI
REGS_FUNC(LogCBThkSL
)( LPSTR func
, CONTEXT
*context
)
77 TRACE_(thunk
)( "%s\n", debugstr_a(func
) );
80 /***********************************************************************
81 * Generates a FT_Prolog call.
83 * 0FB6D1 movzbl edx,cl
84 * 8B1495xxxxxxxx mov edx,[4*edx + targetTable]
85 * 68xxxxxxxx push FT_Prolog
88 static void _write_ftprolog(LPBYTE relayCode
,DWORD
*targetTable
) {
92 *x
++ = 0x0f;*x
++=0xb6;*x
++=0xd1; /* movzbl edx,cl */
93 *x
++ = 0x8B;*x
++=0x14;*x
++=0x95;*(DWORD
**)x
= targetTable
;
94 x
+=4; /* mov edx, [4*edx + targetTable] */
95 *x
++ = 0x68; *(DWORD
*)x
= (DWORD
)GetProcAddress(GetModuleHandleA("KERNEL32"),"FT_Prolog");
96 x
+=4; /* push FT_Prolog */
97 *x
++ = 0xC3; /* lret */
98 /* fill rest with 0xCC / int 3 */
101 /***********************************************************************
102 * _write_qtthunk (internal)
103 * Generates a QT_Thunk style call.
106 * 8A4DFC mov cl , [ebp-04]
107 * 8B148Dxxxxxxxx mov edx, [4*ecx + targetTable]
108 * B8yyyyyyyy mov eax, QT_Thunk
111 static void _write_qtthunk(
112 LPBYTE relayCode
, /* [in] start of QT_Thunk stub */
113 DWORD
*targetTable
/* [in] start of thunk (for index lookup) */
118 *x
++ = 0x33;*x
++=0xC9; /* xor ecx,ecx */
119 *x
++ = 0x8A;*x
++=0x4D;*x
++=0xFC; /* movb cl,[ebp-04] */
120 *x
++ = 0x8B;*x
++=0x14;*x
++=0x8D;*(DWORD
**)x
= targetTable
;
121 x
+=4; /* mov edx, [4*ecx + targetTable */
122 *x
++ = 0xB8; *(DWORD
*)x
= (DWORD
)GetProcAddress(GetModuleHandleA("KERNEL32"),"QT_Thunk");
123 x
+=4; /* mov eax , QT_Thunk */
124 *x
++ = 0xFF; *x
++ = 0xE0; /* jmp eax */
125 /* should fill the rest of the 32 bytes with 0xCC */
128 /***********************************************************************
131 static LPVOID
_loadthunk(LPCSTR module
, LPCSTR func
, LPCSTR module32
,
132 struct ThunkDataCommon
*TD32
, DWORD checksum
)
134 struct ThunkDataCommon
*TD16
;
138 if ((hmod
= LoadLibrary16(module
)) <= 32)
140 ERR_(thunk
)("(%s, %s, %s): Unable to load '%s', error %d\n",
141 module
, func
, module32
, module
, hmod
);
145 if ( !(ordinal
= NE_GetOrdinal(hmod
, func
))
146 || !(TD16
= PTR_SEG_TO_LIN(NE_GetEntryPointEx(hmod
, ordinal
, FALSE
))))
148 ERR_(thunk
)("(%s, %s, %s): Unable to find '%s'\n",
149 module
, func
, module32
, func
);
153 if (TD32
&& memcmp(TD16
->magic
, TD32
->magic
, 4))
155 ERR_(thunk
)("(%s, %s, %s): Bad magic %c%c%c%c (should be %c%c%c%c)\n",
156 module
, func
, module32
,
157 TD16
->magic
[0], TD16
->magic
[1], TD16
->magic
[2], TD16
->magic
[3],
158 TD32
->magic
[0], TD32
->magic
[1], TD32
->magic
[2], TD32
->magic
[3]);
162 if (TD32
&& TD16
->checksum
!= TD32
->checksum
)
164 ERR_(thunk
)("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
165 module
, func
, module32
, TD16
->checksum
, TD32
->checksum
);
169 if (!TD32
&& checksum
&& checksum
!= *(LPDWORD
)TD16
)
171 ERR_(thunk
)("(%s, %s, %s): Wrong checksum %08lx (should be %08lx)\n",
172 module
, func
, module32
, *(LPDWORD
)TD16
, checksum
);
179 /***********************************************************************
180 * GetThunkStuff (KERNEL32.53)
182 LPVOID WINAPI
GetThunkStuff(LPSTR module
, LPSTR func
)
184 return _loadthunk(module
, func
, "<kernel>", NULL
, 0L);
187 /***********************************************************************
188 * GetThunkBuff (KERNEL32.52)
189 * Returns a pointer to ThkBuf in the 16bit library SYSTHUNK.DLL.
191 LPVOID WINAPI
GetThunkBuff(void)
193 return GetThunkStuff("SYSTHUNK.DLL", "ThkBuf");
196 /***********************************************************************
197 * ThunkConnect32 (KERNEL32)
198 * Connects a 32bit and a 16bit thunkbuffer.
200 UINT WINAPI
ThunkConnect32(
201 struct ThunkDataCommon
*TD
, /* [in/out] thunkbuffer */
202 LPSTR thunkfun16
, /* [in] win16 thunkfunction */
203 LPSTR module16
, /* [in] name of win16 dll */
204 LPSTR module32
, /* [in] name of win32 dll */
205 HMODULE hmod32
, /* [in] hmodule of win32 dll */
206 DWORD dwReason
/* [in] initialisation argument */
210 if (!strncmp(TD
->magic
, "SL01", 4))
214 TRACE_(thunk
)("SL01 thunk %s (%lx) <- %s (%s), Reason: %ld\n",
215 module32
, (DWORD
)TD
, module16
, thunkfun16
, dwReason
);
217 else if (!strncmp(TD
->magic
, "LS01", 4))
221 TRACE_(thunk
)("LS01 thunk %s (%lx) -> %s (%s), Reason: %ld\n",
222 module32
, (DWORD
)TD
, module16
, thunkfun16
, dwReason
);
226 ERR_(thunk
)("Invalid magic %c%c%c%c\n",
227 TD
->magic
[0], TD
->magic
[1], TD
->magic
[2], TD
->magic
[3]);
233 case DLL_PROCESS_ATTACH
:
235 struct ThunkDataCommon
*TD16
;
236 if (!(TD16
= _loadthunk(module16
, thunkfun16
, module32
, TD
, 0L)))
241 struct ThunkDataSL32
*SL32
= (struct ThunkDataSL32
*)TD
;
242 struct ThunkDataSL16
*SL16
= (struct ThunkDataSL16
*)TD16
;
243 struct SLTargetDB
*tdb
;
245 if (SL16
->fpData
== NULL
)
247 ERR_(thunk
)("ThunkConnect16 was not called!\n");
251 SL32
->data
= SL16
->fpData
;
253 tdb
= HeapAlloc(GetProcessHeap(), 0, sizeof(*tdb
));
254 tdb
->process
= PROCESS_Current();
255 tdb
->targetTable
= (DWORD
*)(thunkfun16
+ SL32
->offsetTargetTable
);
257 tdb
->next
= SL32
->data
->targetDB
; /* FIXME: not thread-safe! */
258 SL32
->data
->targetDB
= tdb
;
260 TRACE_(thunk
)("Process %08lx allocated TargetDB entry for ThunkDataSL %08lx\n",
261 (DWORD
)PROCESS_Current(), (DWORD
)SL32
->data
);
265 struct ThunkDataLS32
*LS32
= (struct ThunkDataLS32
*)TD
;
266 struct ThunkDataLS16
*LS16
= (struct ThunkDataLS16
*)TD16
;
268 LS32
->targetTable
= PTR_SEG_TO_LIN(LS16
->targetTable
);
270 /* write QT_Thunk and FT_Prolog stubs */
271 _write_qtthunk ((LPBYTE
)TD
+ LS32
->offsetQTThunk
, LS32
->targetTable
);
272 _write_ftprolog((LPBYTE
)TD
+ LS32
->offsetFTProlog
, LS32
->targetTable
);
277 case DLL_PROCESS_DETACH
:
285 /**********************************************************************
286 * QT_Thunk (KERNEL32)
288 * The target address is in EDX.
289 * The 16 bit arguments start at ESP.
290 * The number of 16bit argument bytes is EBP-ESP-0x40 (64 Byte thunksetup).
293 void WINAPI
REGS_FUNC(QT_Thunk
)( CONTEXT
*context
)
299 memcpy(&context16
,context
,sizeof(context16
));
301 CS_reg(&context16
) = HIWORD(EDX_reg(context
));
302 EIP_reg(&context16
) = LOWORD(EDX_reg(context
));
303 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
304 + (WORD
)&((STACK16FRAME
*)0)->bp
;
306 argsize
= EBP_reg(context
)-ESP_reg(context
)-0x40;
308 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
309 (LPBYTE
)ESP_reg(context
), argsize
);
311 EAX_reg(context
) = Callbacks
->CallRegisterShortProc( &context16
, argsize
);
312 EDX_reg(context
) = HIWORD(EAX_reg(context
));
313 EAX_reg(context
) = LOWORD(EAX_reg(context
));
318 /**********************************************************************
319 * FT_Prolog (KERNEL32.233)
321 * The set of FT_... thunk routines is used instead of QT_Thunk,
322 * if structures have to be converted from 32-bit to 16-bit
323 * (change of member alignment, conversion of members).
325 * The thunk function (as created by the thunk compiler) calls
326 * FT_Prolog at the beginning, to set up a stack frame and
327 * allocate a 64 byte buffer on the stack.
328 * The input parameters (target address and some flags) are
329 * saved for later use by FT_Thunk.
331 * Input: EDX 16-bit target address (SEGPTR)
332 * CX bits 0..7 target number (in target table)
333 * bits 8..9 some flags (unclear???)
334 * bits 10..15 number of DWORD arguments
336 * Output: A new stackframe is created, and a 64 byte buffer
337 * allocated on the stack. The layout of the stack
338 * on return is as follows:
340 * (ebp+4) return address to caller of thunk function
342 * (ebp-4) saved EBX register of caller
343 * (ebp-8) saved ESI register of caller
344 * (ebp-12) saved EDI register of caller
345 * (ebp-16) saved ECX register, containing flags
346 * (ebp-20) bitmap containing parameters that are to be converted
347 * by FT_Thunk; it is initialized to 0 by FT_Prolog and
348 * filled in by the thunk code before calling FT_Thunk
352 * (ebp-48) saved EAX register of caller (unclear, never restored???)
353 * (ebp-52) saved EDX register, containing 16-bit thunk target
358 * ESP is EBP-64 after return.
362 void WINAPI
REGS_FUNC(FT_Prolog
)( CONTEXT
*context
)
365 /* Build stack frame */
366 stack32_push(context
, EBP_reg(context
));
367 EBP_reg(context
) = ESP_reg(context
);
369 /* Allocate 64-byte Thunk Buffer */
370 ESP_reg(context
) -= 64;
371 memset((char *)ESP_reg(context
), '\0', 64);
373 /* Store Flags (ECX) and Target Address (EDX) */
374 /* Save other registers to be restored later */
375 *(DWORD
*)(EBP_reg(context
) - 4) = EBX_reg(context
);
376 *(DWORD
*)(EBP_reg(context
) - 8) = ESI_reg(context
);
377 *(DWORD
*)(EBP_reg(context
) - 12) = EDI_reg(context
);
378 *(DWORD
*)(EBP_reg(context
) - 16) = ECX_reg(context
);
380 *(DWORD
*)(EBP_reg(context
) - 48) = EAX_reg(context
);
381 *(DWORD
*)(EBP_reg(context
) - 52) = EDX_reg(context
);
385 /**********************************************************************
386 * FT_Thunk (KERNEL32.234)
388 * This routine performs the actual call to 16-bit code,
389 * similar to QT_Thunk. The differences are:
390 * - The call target is taken from the buffer created by FT_Prolog
391 * - Those arguments requested by the thunk code (by setting the
392 * corresponding bit in the bitmap at EBP-20) are converted
393 * from 32-bit pointers to segmented pointers (those pointers
394 * are guaranteed to point to structures copied to the stack
395 * by the thunk code, so we always use the 16-bit stack selector
396 * for those addresses).
398 * The bit #i of EBP-20 corresponds here to the DWORD starting at
401 * FIXME: It is unclear what happens if there are more than 32 WORDs
402 * of arguments, so that the single DWORD bitmap is no longer
406 void WINAPI
REGS_FUNC(FT_Thunk
)( CONTEXT
*context
)
409 DWORD mapESPrelative
= *(DWORD
*)(EBP_reg(context
) - 20);
410 DWORD callTarget
= *(DWORD
*)(EBP_reg(context
) - 52);
414 LPBYTE newstack
, oldstack
;
416 memcpy(&context16
,context
,sizeof(context16
));
418 CS_reg(&context16
) = HIWORD(callTarget
);
419 EIP_reg(&context16
) = LOWORD(callTarget
);
420 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
421 + (WORD
)&((STACK16FRAME
*)0)->bp
;
423 argsize
= EBP_reg(context
)-ESP_reg(context
)-0x40;
424 newstack
= (LPBYTE
)CURRENT_STACK16
- argsize
;
425 oldstack
= (LPBYTE
)ESP_reg(context
);
427 memcpy( newstack
, oldstack
, argsize
);
429 for (i
= 0; i
< 32; i
++) /* NOTE: What about > 32 arguments? */
430 if (mapESPrelative
& (1 << i
))
432 SEGPTR
*arg
= (SEGPTR
*)(newstack
+ 2*i
);
433 *arg
= PTR_SEG_OFF_TO_SEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack
),
434 OFFSETOF(NtCurrentTeb()->cur_stack
) - argsize
435 + (*(LPBYTE
*)arg
- oldstack
));
438 EAX_reg(context
) = Callbacks
->CallRegisterShortProc( &context16
, argsize
);
439 EDX_reg(context
) = HIWORD(EAX_reg(context
));
440 EAX_reg(context
) = LOWORD(EAX_reg(context
));
442 /* Copy modified buffers back to 32-bit stack */
443 memcpy( oldstack
, newstack
, argsize
);
447 /**********************************************************************
448 * FT_ExitNN (KERNEL32.218 - 232)
450 * One of the FT_ExitNN functions is called at the end of the thunk code.
451 * It removes the stack frame created by FT_Prolog, moves the function
452 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
453 * value, but the thunk code has moved it from EAX to EBX in the
454 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
455 * and perform a return to the CALLER of the thunk code (while removing
456 * the given number of arguments from the caller's stack).
459 static void FT_Exit(CONTEXT
*context
, int nPopArgs
)
462 /* Return value is in EBX */
463 EAX_reg(context
) = EBX_reg(context
);
465 /* Restore EBX, ESI, and EDI registers */
466 EBX_reg(context
) = *(DWORD
*)(EBP_reg(context
) - 4);
467 ESI_reg(context
) = *(DWORD
*)(EBP_reg(context
) - 8);
468 EDI_reg(context
) = *(DWORD
*)(EBP_reg(context
) - 12);
470 /* Clean up stack frame */
471 ESP_reg(context
) = EBP_reg(context
);
472 EBP_reg(context
) = stack32_pop(context
);
474 /* Pop return address to CALLER of thunk code */
475 EIP_reg(context
) = stack32_pop(context
);
476 /* Remove arguments */
477 ESP_reg(context
) += nPopArgs
;
481 void WINAPI
REGS_FUNC(FT_Exit0
)(CONTEXT
*context
) { FT_Exit(context
, 0); }
482 void WINAPI
REGS_FUNC(FT_Exit4
)(CONTEXT
*context
) { FT_Exit(context
, 4); }
483 void WINAPI
REGS_FUNC(FT_Exit8
)(CONTEXT
*context
) { FT_Exit(context
, 8); }
484 void WINAPI
REGS_FUNC(FT_Exit12
)(CONTEXT
*context
) { FT_Exit(context
, 12); }
485 void WINAPI
REGS_FUNC(FT_Exit16
)(CONTEXT
*context
) { FT_Exit(context
, 16); }
486 void WINAPI
REGS_FUNC(FT_Exit20
)(CONTEXT
*context
) { FT_Exit(context
, 20); }
487 void WINAPI
REGS_FUNC(FT_Exit24
)(CONTEXT
*context
) { FT_Exit(context
, 24); }
488 void WINAPI
REGS_FUNC(FT_Exit28
)(CONTEXT
*context
) { FT_Exit(context
, 28); }
489 void WINAPI
REGS_FUNC(FT_Exit32
)(CONTEXT
*context
) { FT_Exit(context
, 32); }
490 void WINAPI
REGS_FUNC(FT_Exit36
)(CONTEXT
*context
) { FT_Exit(context
, 36); }
491 void WINAPI
REGS_FUNC(FT_Exit40
)(CONTEXT
*context
) { FT_Exit(context
, 40); }
492 void WINAPI
REGS_FUNC(FT_Exit44
)(CONTEXT
*context
) { FT_Exit(context
, 44); }
493 void WINAPI
REGS_FUNC(FT_Exit48
)(CONTEXT
*context
) { FT_Exit(context
, 48); }
494 void WINAPI
REGS_FUNC(FT_Exit52
)(CONTEXT
*context
) { FT_Exit(context
, 52); }
495 void WINAPI
REGS_FUNC(FT_Exit56
)(CONTEXT
*context
) { FT_Exit(context
, 56); }
498 /***********************************************************************
499 * ThunkInitLS (KERNEL32.43)
500 * A thunkbuffer link routine
501 * The thunkbuf looks like:
503 * 00: DWORD length ? don't know exactly
504 * 04: SEGPTR ptr ? where does it point to?
505 * The pointer ptr is written into the first DWORD of 'thunk'.
506 * (probably correct implemented)
509 * segmented pointer to thunk?
511 DWORD WINAPI
ThunkInitLS(
512 LPDWORD thunk
, /* [in] win32 thunk */
513 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
514 DWORD len
, /* [in] thkbuffer length */
515 LPCSTR dll16
, /* [in] name of win16 dll */
516 LPCSTR dll32
/* [in] name of win32 dll (FIXME: not used?) */
520 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
525 *(DWORD
*)thunk
= addr
[1];
530 /***********************************************************************
531 * Common32ThkLS (KERNEL32.45)
533 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
534 * style thunks. The basic difference is that the parameter conversion
535 * is done completely on the *16-bit* side here. Thus we do not call
536 * the 16-bit target directly, but call a common entry point instead.
537 * This entry function then calls the target according to the target
538 * number passed in the DI register.
540 * Input: EAX SEGPTR to the common 16-bit entry point
541 * CX offset in thunk table (target number * 4)
542 * DX error return value if execution fails (unclear???)
543 * EDX.HI number of DWORD parameters
545 * (Note that we need to move the thunk table offset from CX to DI !)
547 * The called 16-bit stub expects its stack to look like this:
549 * (esp+40) 32-bit arguments
551 * (esp+8) 32 byte of stack space available as buffer
552 * (esp) 8 byte return address for use with 0x66 lret
554 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
555 * and uses the EAX register to return a DWORD return value.
556 * Thus we need to use a special assembly glue routine
557 * (CallRegisterLongProc instead of CallRegisterShortProc).
559 * Finally, we return to the caller, popping the arguments off
562 * FIXME: The called function uses EBX to return the number of
563 * arguments that are to be popped off the caller's stack.
564 * This is clobbered by the assembly glue, so we simply use
565 * the original EDX.HI to get the number of arguments.
566 * (Those two values should be equal anyway ...?)
569 void WINAPI
REGS_FUNC(Common32ThkLS
)( CONTEXT
*context
)
575 memcpy(&context16
,context
,sizeof(context16
));
577 DI_reg(&context16
) = CX_reg(context
);
578 CS_reg(&context16
) = HIWORD(EAX_reg(context
));
579 EIP_reg(&context16
) = LOWORD(EAX_reg(context
));
580 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
581 + (WORD
)&((STACK16FRAME
*)0)->bp
;
583 argsize
= HIWORD(EDX_reg(context
)) * 4;
585 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
586 if (EDX_reg(context
) == EIP_reg(context
))
589 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
590 (LPBYTE
)ESP_reg(context
), argsize
);
592 EAX_reg(context
) = Callbacks
->CallRegisterLongProc(&context16
, argsize
+ 32);
594 /* Clean up caller's stack frame */
595 ESP_reg(context
) += argsize
;
599 /***********************************************************************
600 * OT_32ThkLSF (KERNEL32.40)
602 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
603 * argument processing is done on both the 32-bit and the 16-bit side:
604 * The 32-bit side prepares arguments, copying them onto the stack.
606 * When this routine is called, the first word on the stack is the
607 * number of argument bytes prepared by the 32-bit code, and EDX
608 * contains the 16-bit target address.
610 * The called 16-bit routine is another relaycode, doing further
611 * argument processing and then calling the real 16-bit target
612 * whose address is stored at [bp-04].
614 * The call proceeds using a normal CallRegisterShortProc.
615 * After return from the 16-bit relaycode, the arguments need
616 * to be copied *back* to the 32-bit stack, since the 32-bit
617 * relaycode processes output parameters.
619 * Note that we copy twice the number of arguments, since some of the
620 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
621 * arguments of the caller!
623 * (Note that this function seems only to be used for
624 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
626 void WINAPI
REGS_FUNC(OT_32ThkLSF
)( CONTEXT
*context
)
632 memcpy(&context16
,context
,sizeof(context16
));
634 CS_reg(&context16
) = HIWORD(EDX_reg(context
));
635 EIP_reg(&context16
) = LOWORD(EDX_reg(context
));
636 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
637 + (WORD
)&((STACK16FRAME
*)0)->bp
;
639 argsize
= 2 * *(WORD
*)ESP_reg(context
) + 2;
641 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
642 (LPBYTE
)ESP_reg(context
), argsize
);
644 EAX_reg(context
) = Callbacks
->CallRegisterShortProc(&context16
, argsize
);
646 memcpy( (LPBYTE
)ESP_reg(context
),
647 (LPBYTE
)CURRENT_STACK16
- argsize
, argsize
);
651 /***********************************************************************
652 * ThunkInitLSF (KERNEL32.41)
653 * A thunk setup routine.
654 * Expects a pointer to a preinitialized thunkbuffer in the first argument
656 * 00..03: unknown (pointer, check _41, _43, _46)
659 * 06..23: unknown (space for replacement code, check .90)
661 * 24:>E800000000 call offset 29
662 * 29:>58 pop eax ( target of call )
663 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
664 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
665 * 34: 68yyyyyyyy push KERNEL32.90
669 * 3E ... 59: unknown (space for replacement code?)
670 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
672 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
674 * 67: 68xxxxxxxx push xxxxxxxx
675 * 6C: 68yyyyyyyy push KERNEL32.89
678 * This function checks if the code is there, and replaces the yyyyyyyy entries
679 * by the functionpointers.
680 * The thunkbuf looks like:
682 * 00: DWORD length ? don't know exactly
683 * 04: SEGPTR ptr ? where does it point to?
684 * The segpointer ptr is written into the first DWORD of 'thunk'.
687 * unclear, pointer to win16 thkbuffer?
689 LPVOID WINAPI
ThunkInitLSF(
690 LPBYTE thunk
, /* [in] win32 thunk */
691 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
692 DWORD len
, /* [in] length of thkbuffer */
693 LPCSTR dll16
, /* [in] name of win16 dll */
694 LPCSTR dll32
/* [in] name of win32 dll */
696 HMODULE hkrnl32
= GetModuleHandleA("KERNEL32");
699 /* FIXME: add checks for valid code ... */
700 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
701 *(DWORD
*)(thunk
+0x35) = (DWORD
)GetProcAddress(hkrnl32
,(LPSTR
)90);
702 *(DWORD
*)(thunk
+0x6D) = (DWORD
)GetProcAddress(hkrnl32
,(LPSTR
)89);
705 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
708 addr2
= PTR_SEG_TO_LIN(addr
[1]);
710 *(DWORD
*)thunk
= (DWORD
)addr2
;
715 /***********************************************************************
716 * FT_PrologPrime (KERNEL32.89)
718 * This function is called from the relay code installed by
719 * ThunkInitLSF. It replaces the location from where it was
720 * called by a standard FT_Prolog call stub (which is 'primed'
721 * by inserting the correct target table pointer).
722 * Finally, it calls that stub.
724 * Input: ECX target number + flags (passed through to FT_Prolog)
725 * (ESP) offset of location where target table pointer
726 * is stored, relative to the start of the relay code
727 * (ESP+4) pointer to start of relay code
728 * (this is where the FT_Prolog call stub gets written to)
730 * Note: The two DWORD arguments get popped off the stack.
733 void WINAPI
REGS_FUNC(FT_PrologPrime
)( CONTEXT
*context
)
736 DWORD targetTableOffset
;
739 /* Compensate for the fact that the Wine register relay code thought
740 we were being called, although we were in fact jumped to */
741 ESP_reg(context
) -= 4;
743 /* Write FT_Prolog call stub */
744 targetTableOffset
= stack32_pop(context
);
745 relayCode
= (LPBYTE
)stack32_pop(context
);
746 _write_ftprolog( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
748 /* Jump to the call stub just created */
749 EIP_reg(context
) = (DWORD
)relayCode
;
753 /***********************************************************************
754 * QT_ThunkPrime (KERNEL32.90)
756 * This function corresponds to FT_PrologPrime, but installs a
757 * call stub for QT_Thunk instead.
759 * Input: (EBP-4) target number (passed through to QT_Thunk)
760 * EDX target table pointer location offset
761 * EAX start of relay code
764 void WINAPI
REGS_FUNC(QT_ThunkPrime
)( CONTEXT
*context
)
767 DWORD targetTableOffset
;
770 /* Compensate for the fact that the Wine register relay code thought
771 we were being called, although we were in fact jumped to */
772 ESP_reg(context
) -= 4;
774 /* Write QT_Thunk call stub */
775 targetTableOffset
= EDX_reg(context
);
776 relayCode
= (LPBYTE
)EAX_reg(context
);
777 _write_qtthunk( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
779 /* Jump to the call stub just created */
780 EIP_reg(context
) = (DWORD
)relayCode
;
784 /***********************************************************************
786 * Another thunkbuf link routine.
787 * The start of the thunkbuf looks like this:
789 * 04: SEGPTR address for thunkbuffer pointer
792 VOID WINAPI
ThunkInitSL(
793 LPBYTE thunk
, /* [in] start of thunkbuffer */
794 LPCSTR thkbuf
, /* [in] name/ordinal of thunkbuffer in win16 dll */
795 DWORD len
, /* [in] length of thunkbuffer */
796 LPCSTR dll16
, /* [in] name of win16 dll containing the thkbuf */
797 LPCSTR dll32
/* [in] win32 dll. FIXME: strange, unused */
801 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
804 *(DWORD
*)PTR_SEG_TO_LIN(addr
[1]) = (DWORD
)thunk
;
807 /**********************************************************************
812 BOOL WINAPI
SSInit16()
817 /**********************************************************************
818 * SSOnBigStack KERNEL32.87
819 * Check if thunking is initialized (ss selector set up etc.)
820 * We do that differently, so just return TRUE.
825 BOOL WINAPI
SSOnBigStack()
827 TRACE_(thunk
)("Yes, thunking is initialized\n");
831 /**********************************************************************
832 * SSConfirmSmallStack KERNEL.704
834 * Abort if not on small stack.
836 * This must be a register routine as it has to preserve *all* registers.
838 void WINAPI
SSConfirmSmallStack( CONTEXT86
*context
)
840 /* We are always on the small stack while in 16-bit code ... */
843 /**********************************************************************
845 * One of the real thunking functions. This one seems to be for 32<->32
846 * thunks. It should probably be capable of crossing processboundaries.
848 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
851 DWORD WINAPIV
SSCall(
852 DWORD nr
, /* [in] number of argument bytes */
853 DWORD flags
, /* [in] FIXME: flags ? */
854 FARPROC fun
, /* [in] function to call */
855 ... /* [in/out] arguments */
858 DWORD
*args
= ((DWORD
*)&fun
) + 1;
862 DPRINTF("(%ld,0x%08lx,%p,[",nr
,flags
,fun
);
864 DPRINTF("0x%08lx,",args
[i
]);
870 case 4: ret
= fun(args
[0]);
872 case 8: ret
= fun(args
[0],args
[1]);
874 case 12: ret
= fun(args
[0],args
[1],args
[2]);
876 case 16: ret
= fun(args
[0],args
[1],args
[2],args
[3]);
878 case 20: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4]);
880 case 24: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
882 case 28: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
884 case 32: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
886 case 36: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
888 case 40: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
890 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]);
892 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]);
895 WARN_(thunk
)("Unsupported nr of arguments, %ld\n",nr
);
900 TRACE_(thunk
)(" returning %ld ...\n",ret
);
904 /**********************************************************************
905 * W32S_BackTo32 (KERNEL32.51)
907 void WINAPI
REGS_FUNC(W32S_BackTo32
)( CONTEXT
*context
)
910 LPDWORD stack
= (LPDWORD
)ESP_reg( context
);
911 FARPROC proc
= (FARPROC
)EIP_reg(context
);
913 EAX_reg( context
) = proc( stack
[1], stack
[2], stack
[3], stack
[4], stack
[5],
914 stack
[6], stack
[7], stack
[8], stack
[9], stack
[10] );
916 EIP_reg( context
) = stack32_pop(context
);
920 /**********************************************************************
921 * AllocSLCallback (KERNEL32)
923 * Win95 uses some structchains for callbacks. It allocates them
924 * in blocks of 100 entries, size 32 bytes each, layout:
926 * 0: PTR nextblockstart
928 * 8: WORD sel ( start points to blockstart)
932 * 18: PDB *owning_process;
935 * We ignore this for now. (Just a note for further developers)
936 * FIXME: use this method, so we don't waste selectors...
938 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
939 * the 0x66 prefix switches from word->long registers.
942 * 6668x arg2 x pushl <arg2>
944 * EAx arg1 x jmpf <arg1>
946 * returns the startaddress of this thunk.
948 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
950 * segmented pointer to the start of the thunk
954 DWORD finalizer
, /* [in] finalizer function */
955 DWORD callback
/* [in] callback function */
957 LPBYTE x
,thunk
= HeapAlloc( GetProcessHeap(), 0, 32 );
961 *x
++=0x66;*x
++=0x5a; /* popl edx */
962 *x
++=0x66;*x
++=0x68;*(DWORD
*)x
=finalizer
;x
+=4; /* pushl finalizer */
963 *x
++=0x66;*x
++=0x52; /* pushl edx */
964 *x
++=0xea;*(DWORD
*)x
=callback
;x
+=4; /* jmpf callback */
966 *(PDB
**)(thunk
+18) = PROCESS_Current();
968 sel
= SELECTOR_AllocBlock( thunk
, 32, SEGMENT_CODE
, FALSE
, FALSE
);
972 /**********************************************************************
973 * FreeSLCallback (KERNEL32.274)
974 * Frees the specified 16->32 callback
978 DWORD x
/* [in] 16 bit callback (segmented pointer?) */
980 FIXME_(win32
)("(0x%08lx): stub\n",x
);
984 /**********************************************************************
985 * GetTEBSelectorFS (KERNEL.475)
986 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
988 void WINAPI
GetTEBSelectorFS16(void)
990 GET_FS( CURRENT_STACK16
->fs
);
993 /**********************************************************************
994 * KERNEL_431 (KERNEL.431)
995 * IsPeFormat (W32SYS.2)
996 * Checks the passed filename if it is a PE format executeable
1001 BOOL16 WINAPI
IsPeFormat16(
1002 LPSTR fn
, /* [in] filename to executeable */
1003 HFILE16 hf16
/* [in] open file, if filename is NULL */
1005 IMAGE_DOS_HEADER mzh
;
1006 HFILE hf
=FILE_GetHandle(hf16
);
1011 hf
= OpenFile(fn
,&ofs
,OF_READ
);
1012 if (hf
==HFILE_ERROR
)
1015 _llseek(hf
,0,SEEK_SET
);
1016 if (sizeof(mzh
)!=_lread(hf
,&mzh
,sizeof(mzh
))) {
1020 if (mzh
.e_magic
!=IMAGE_DOS_SIGNATURE
) {
1021 WARN_(dosmem
)("File has not got dos signature!\n");
1025 _llseek(hf
,mzh
.e_lfanew
,SEEK_SET
);
1026 if (sizeof(DWORD
)!=_lread(hf
,&xmagic
,sizeof(DWORD
))) {
1031 return (xmagic
== IMAGE_NT_SIGNATURE
);
1035 /***********************************************************************
1036 * K32Thk1632Prolog (KERNEL32.492)
1038 void WINAPI
REGS_FUNC(K32Thk1632Prolog
)( CONTEXT
*context
)
1041 LPBYTE code
= (LPBYTE
)EIP_reg(context
) - 5;
1043 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1044 of 16->32 thunks instead of using one of the standard methods!
1045 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1046 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1047 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1048 bypassed, which means it will crash the next time the 32-bit OLE
1049 code thunks down again to 16-bit (this *will* happen!).
1051 The following hack tries to recognize this situation.
1052 This is possible since the called stubs in OLECLI32/OLESVR32 all
1053 look exactly the same:
1054 00 E8xxxxxxxx call K32Thk1632Prolog
1055 05 FF55FC call [ebp-04]
1056 08 E8xxxxxxxx call K32Thk1632Epilog
1059 If we recognize this situation, we try to simulate the actions
1060 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1061 to our 32-bit stack, creating a proper STACK16FRAME and
1062 updating cur_stack. */
1064 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1065 && code
[13] == 0x66 && code
[14] == 0xCB)
1067 WORD stackSel
= NtCurrentTeb()->stack_sel
;
1068 DWORD stackBase
= GetSelectorBase(stackSel
);
1070 DWORD argSize
= EBP_reg(context
) - ESP_reg(context
);
1071 char *stack16
= (char *)ESP_reg(context
) - 4;
1072 char *stack32
= (char *)NtCurrentTeb()->cur_stack
- argSize
;
1073 STACK16FRAME
*frame16
= (STACK16FRAME
*)stack16
- 1;
1075 TRACE_(thunk
)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1076 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1078 memset(frame16
, '\0', sizeof(STACK16FRAME
));
1079 frame16
->frame32
= (STACK32FRAME
*)NtCurrentTeb()->cur_stack
;
1080 frame16
->ebp
= EBP_reg(context
);
1082 memcpy(stack32
, stack16
, argSize
);
1083 NtCurrentTeb()->cur_stack
= PTR_SEG_OFF_TO_SEGPTR(stackSel
, (DWORD
)frame16
- stackBase
);
1085 ESP_reg(context
) = (DWORD
)stack32
+ 4;
1086 EBP_reg(context
) = ESP_reg(context
) + argSize
;
1088 TRACE_(thunk
)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1089 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1092 SYSLEVEL_ReleaseWin16Lock();
1096 /***********************************************************************
1097 * K32Thk1632Epilog (KERNEL32.491)
1099 void WINAPI
REGS_FUNC(K32Thk1632Epilog
)( CONTEXT
*context
)
1102 LPBYTE code
= (LPBYTE
)EIP_reg(context
) - 13;
1104 SYSLEVEL_RestoreWin16Lock();
1106 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1108 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1109 && code
[13] == 0x66 && code
[14] == 0xCB)
1111 STACK16FRAME
*frame16
= (STACK16FRAME
*)PTR_SEG_TO_LIN(NtCurrentTeb()->cur_stack
);
1112 char *stack16
= (char *)(frame16
+ 1);
1113 DWORD argSize
= frame16
->ebp
- (DWORD
)stack16
;
1114 char *stack32
= (char *)frame16
->frame32
- argSize
;
1116 DWORD nArgsPopped
= ESP_reg(context
) - (DWORD
)stack32
;
1118 TRACE_(thunk
)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1119 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1121 NtCurrentTeb()->cur_stack
= (DWORD
)frame16
->frame32
;
1123 ESP_reg(context
) = (DWORD
)stack16
+ nArgsPopped
;
1124 EBP_reg(context
) = frame16
->ebp
;
1126 TRACE_(thunk
)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1127 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1132 /***********************************************************************
1133 * UpdateResource32A (KERNEL32.707)
1135 BOOL WINAPI
UpdateResourceA(
1143 FIXME_(win32
)(": stub\n");
1144 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1148 /***********************************************************************
1149 * UpdateResource32W (KERNEL32.708)
1151 BOOL WINAPI
UpdateResourceW(
1159 FIXME_(win32
)(": stub\n");
1160 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1165 /***********************************************************************
1166 * WaitNamedPipe32A [KERNEL32.725]
1168 BOOL WINAPI
WaitNamedPipeA (LPCSTR lpNamedPipeName
, DWORD nTimeOut
)
1169 { FIXME_(win32
)("%s 0x%08lx\n",lpNamedPipeName
,nTimeOut
);
1170 SetLastError(ERROR_PIPE_NOT_CONNECTED
);
1173 /***********************************************************************
1174 * WaitNamedPipe32W [KERNEL32.726]
1176 BOOL WINAPI
WaitNamedPipeW (LPCWSTR lpNamedPipeName
, DWORD nTimeOut
)
1177 { FIXME_(win32
)("%s 0x%08lx\n",debugstr_w(lpNamedPipeName
),nTimeOut
);
1178 SetLastError(ERROR_PIPE_NOT_CONNECTED
);
1182 /*********************************************************************
1183 * PK16FNF [KERNEL32.91]
1185 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1186 * string buffer with the 8.3 filename of a recently loaded 16-bit
1187 * module. It is unknown exactly what modules trigger this
1188 * mechanism or what purpose this serves. Win98 Explorer (and
1189 * probably also Win95 with IE 4 shell integration) calls this
1190 * several times during initialization.
1192 * FIXME: find out what this really does and make it work.
1194 void WINAPI
PK16FNF(LPSTR strPtr
)
1196 FIXME_(win32
)("(%p): stub\n", strPtr
);
1198 /* fill in a fake filename that'll be easy to recognize */
1199 lstrcpyA(strPtr
, "WINESTUB.FIX");