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 (!lstrncmpA(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 (!lstrncmpA(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 * WOWCallback16 (KERNEL32.62)(WOW32.2)
500 * Calls a win16 function with a single DWORD argument.
504 DWORD WINAPI
WOWCallback16(
505 FARPROC16 fproc
, /* [in] win16 function to call */
506 DWORD arg
/* [in] single DWORD argument to function */
509 TRACE_(thunk
)("(%p,0x%08lx)...\n",fproc
,arg
);
510 ret
= Callbacks
->CallWOWCallbackProc(fproc
,arg
);
511 TRACE_(thunk
)("... returns %ld\n",ret
);
515 /**********************************************************************
516 * WOWCallback16Ex (KERNEL32.55)(WOW32.3)
517 * Calls a function in 16bit code.
521 BOOL WINAPI
WOWCallback16Ex(
522 FARPROC16 vpfn16
, /* [in] win16 function to call */
523 DWORD dwFlags
, /* [in] flags */
524 DWORD cbArgs
, /* [in] nr of arguments */
525 LPVOID pArgs
, /* [in] pointer to arguments (LPDWORD) */
526 LPDWORD pdwRetCode
/* [out] return value of win16 function */
528 return Callbacks
->CallWOWCallback16Ex(vpfn16
,dwFlags
,cbArgs
,pArgs
,pdwRetCode
);
531 /***********************************************************************
532 * ThunkInitLS (KERNEL32.43)
533 * A thunkbuffer link routine
534 * The thunkbuf looks like:
536 * 00: DWORD length ? don't know exactly
537 * 04: SEGPTR ptr ? where does it point to?
538 * The pointer ptr is written into the first DWORD of 'thunk'.
539 * (probably correct implemented)
542 * segmented pointer to thunk?
544 DWORD WINAPI
ThunkInitLS(
545 LPDWORD thunk
, /* [in] win32 thunk */
546 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
547 DWORD len
, /* [in] thkbuffer length */
548 LPCSTR dll16
, /* [in] name of win16 dll */
549 LPCSTR dll32
/* [in] name of win32 dll (FIXME: not used?) */
553 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
558 *(DWORD
*)thunk
= addr
[1];
563 /***********************************************************************
564 * Common32ThkLS (KERNEL32.45)
566 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
567 * style thunks. The basic difference is that the parameter conversion
568 * is done completely on the *16-bit* side here. Thus we do not call
569 * the 16-bit target directly, but call a common entry point instead.
570 * This entry function then calls the target according to the target
571 * number passed in the DI register.
573 * Input: EAX SEGPTR to the common 16-bit entry point
574 * CX offset in thunk table (target number * 4)
575 * DX error return value if execution fails (unclear???)
576 * EDX.HI number of DWORD parameters
578 * (Note that we need to move the thunk table offset from CX to DI !)
580 * The called 16-bit stub expects its stack to look like this:
582 * (esp+40) 32-bit arguments
584 * (esp+8) 32 byte of stack space available as buffer
585 * (esp) 8 byte return address for use with 0x66 lret
587 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
588 * and uses the EAX register to return a DWORD return value.
589 * Thus we need to use a special assembly glue routine
590 * (CallRegisterLongProc instead of CallRegisterShortProc).
592 * Finally, we return to the caller, popping the arguments off
595 * FIXME: The called function uses EBX to return the number of
596 * arguments that are to be popped off the caller's stack.
597 * This is clobbered by the assembly glue, so we simply use
598 * the original EDX.HI to get the number of arguments.
599 * (Those two values should be equal anyway ...?)
602 void WINAPI
REGS_FUNC(Common32ThkLS
)( CONTEXT
*context
)
608 memcpy(&context16
,context
,sizeof(context16
));
610 DI_reg(&context16
) = CX_reg(context
);
611 CS_reg(&context16
) = HIWORD(EAX_reg(context
));
612 EIP_reg(&context16
) = LOWORD(EAX_reg(context
));
613 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
614 + (WORD
)&((STACK16FRAME
*)0)->bp
;
616 argsize
= HIWORD(EDX_reg(context
)) * 4;
618 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
619 if (EDX_reg(context
) == EIP_reg(context
))
622 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
623 (LPBYTE
)ESP_reg(context
), argsize
);
625 EAX_reg(context
) = Callbacks
->CallRegisterLongProc(&context16
, argsize
+ 32);
627 /* Clean up caller's stack frame */
628 ESP_reg(context
) += argsize
;
632 /***********************************************************************
633 * OT_32ThkLSF (KERNEL32.40)
635 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
636 * argument processing is done on both the 32-bit and the 16-bit side:
637 * The 32-bit side prepares arguments, copying them onto the stack.
639 * When this routine is called, the first word on the stack is the
640 * number of argument bytes prepared by the 32-bit code, and EDX
641 * contains the 16-bit target address.
643 * The called 16-bit routine is another relaycode, doing further
644 * argument processing and then calling the real 16-bit target
645 * whose address is stored at [bp-04].
647 * The call proceeds using a normal CallRegisterShortProc.
648 * After return from the 16-bit relaycode, the arguments need
649 * to be copied *back* to the 32-bit stack, since the 32-bit
650 * relaycode processes output parameters.
652 * Note that we copy twice the number of arguments, since some of the
653 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
654 * arguments of the caller!
656 * (Note that this function seems only to be used for
657 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
659 void WINAPI
REGS_FUNC(OT_32ThkLSF
)( CONTEXT
*context
)
665 memcpy(&context16
,context
,sizeof(context16
));
667 CS_reg(&context16
) = HIWORD(EDX_reg(context
));
668 EIP_reg(&context16
) = LOWORD(EDX_reg(context
));
669 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
670 + (WORD
)&((STACK16FRAME
*)0)->bp
;
672 argsize
= 2 * *(WORD
*)ESP_reg(context
) + 2;
674 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
675 (LPBYTE
)ESP_reg(context
), argsize
);
677 EAX_reg(context
) = Callbacks
->CallRegisterShortProc(&context16
, argsize
);
679 memcpy( (LPBYTE
)ESP_reg(context
),
680 (LPBYTE
)CURRENT_STACK16
- argsize
, argsize
);
684 /***********************************************************************
685 * ThunkInitLSF (KERNEL32.41)
686 * A thunk setup routine.
687 * Expects a pointer to a preinitialized thunkbuffer in the first argument
689 * 00..03: unknown (pointer, check _41, _43, _46)
692 * 06..23: unknown (space for replacement code, check .90)
694 * 24:>E800000000 call offset 29
695 * 29:>58 pop eax ( target of call )
696 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
697 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
698 * 34: 68yyyyyyyy push KERNEL32.90
702 * 3E ... 59: unknown (space for replacement code?)
703 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
705 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
707 * 67: 68xxxxxxxx push xxxxxxxx
708 * 6C: 68yyyyyyyy push KERNEL32.89
711 * This function checks if the code is there, and replaces the yyyyyyyy entries
712 * by the functionpointers.
713 * The thunkbuf looks like:
715 * 00: DWORD length ? don't know exactly
716 * 04: SEGPTR ptr ? where does it point to?
717 * The segpointer ptr is written into the first DWORD of 'thunk'.
720 * unclear, pointer to win16 thkbuffer?
722 LPVOID WINAPI
ThunkInitLSF(
723 LPBYTE thunk
, /* [in] win32 thunk */
724 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
725 DWORD len
, /* [in] length of thkbuffer */
726 LPCSTR dll16
, /* [in] name of win16 dll */
727 LPCSTR dll32
/* [in] name of win32 dll */
729 HMODULE hkrnl32
= GetModuleHandleA("KERNEL32");
732 /* FIXME: add checks for valid code ... */
733 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
734 *(DWORD
*)(thunk
+0x35) = (DWORD
)GetProcAddress(hkrnl32
,(LPSTR
)90);
735 *(DWORD
*)(thunk
+0x6D) = (DWORD
)GetProcAddress(hkrnl32
,(LPSTR
)89);
738 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
741 addr2
= PTR_SEG_TO_LIN(addr
[1]);
743 *(DWORD
*)thunk
= (DWORD
)addr2
;
748 /***********************************************************************
749 * FT_PrologPrime (KERNEL32.89)
751 * This function is called from the relay code installed by
752 * ThunkInitLSF. It replaces the location from where it was
753 * called by a standard FT_Prolog call stub (which is 'primed'
754 * by inserting the correct target table pointer).
755 * Finally, it calls that stub.
757 * Input: ECX target number + flags (passed through to FT_Prolog)
758 * (ESP) offset of location where target table pointer
759 * is stored, relative to the start of the relay code
760 * (ESP+4) pointer to start of relay code
761 * (this is where the FT_Prolog call stub gets written to)
763 * Note: The two DWORD arguments get popped off the stack.
766 void WINAPI
REGS_FUNC(FT_PrologPrime
)( CONTEXT
*context
)
769 DWORD targetTableOffset
;
772 /* Compensate for the fact that the Wine register relay code thought
773 we were being called, although we were in fact jumped to */
774 ESP_reg(context
) -= 4;
776 /* Write FT_Prolog call stub */
777 targetTableOffset
= STACK32_POP(context
);
778 relayCode
= (LPBYTE
)STACK32_POP(context
);
779 _write_ftprolog( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
781 /* Jump to the call stub just created */
782 EIP_reg(context
) = (DWORD
)relayCode
;
786 /***********************************************************************
787 * QT_ThunkPrime (KERNEL32.90)
789 * This function corresponds to FT_PrologPrime, but installs a
790 * call stub for QT_Thunk instead.
792 * Input: (EBP-4) target number (passed through to QT_Thunk)
793 * EDX target table pointer location offset
794 * EAX start of relay code
797 void WINAPI
REGS_FUNC(QT_ThunkPrime
)( CONTEXT
*context
)
800 DWORD targetTableOffset
;
803 /* Compensate for the fact that the Wine register relay code thought
804 we were being called, although we were in fact jumped to */
805 ESP_reg(context
) -= 4;
807 /* Write QT_Thunk call stub */
808 targetTableOffset
= EDX_reg(context
);
809 relayCode
= (LPBYTE
)EAX_reg(context
);
810 _write_qtthunk( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
812 /* Jump to the call stub just created */
813 EIP_reg(context
) = (DWORD
)relayCode
;
817 /***********************************************************************
819 * Another thunkbuf link routine.
820 * The start of the thunkbuf looks like this:
822 * 04: SEGPTR address for thunkbuffer pointer
825 VOID WINAPI
ThunkInitSL(
826 LPBYTE thunk
, /* [in] start of thunkbuffer */
827 LPCSTR thkbuf
, /* [in] name/ordinal of thunkbuffer in win16 dll */
828 DWORD len
, /* [in] length of thunkbuffer */
829 LPCSTR dll16
, /* [in] name of win16 dll containing the thkbuf */
830 LPCSTR dll32
/* [in] win32 dll. FIXME: strange, unused */
834 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
837 *(DWORD
*)PTR_SEG_TO_LIN(addr
[1]) = (DWORD
)thunk
;
840 /**********************************************************************
845 BOOL WINAPI
SSInit16()
850 /**********************************************************************
851 * SSOnBigStack KERNEL32.87
852 * Check if thunking is initialized (ss selector set up etc.)
853 * We do that differently, so just return TRUE.
858 BOOL WINAPI
SSOnBigStack()
860 TRACE_(thunk
)("Yes, thunking is initialized\n");
864 /**********************************************************************
865 * SSConfirmSmallStack KERNEL.704
867 * Abort if not on small stack.
869 * This must be a register routine as it has to preserve *all* registers.
871 void WINAPI
SSConfirmSmallStack( CONTEXT86
*context
)
873 /* We are always on the small stack while in 16-bit code ... */
876 /**********************************************************************
878 * One of the real thunking functions. This one seems to be for 32<->32
879 * thunks. It should probably be capable of crossing processboundaries.
881 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
884 DWORD WINAPIV
SSCall(
885 DWORD nr
, /* [in] number of argument bytes */
886 DWORD flags
, /* [in] FIXME: flags ? */
887 FARPROC fun
, /* [in] function to call */
888 ... /* [in/out] arguments */
891 DWORD
*args
= ((DWORD
*)&fun
) + 1;
894 dbg_decl_str(thunk
, 256);
896 dsprintf(thunk
,"0x%08lx,",args
[i
]);
897 TRACE_(thunk
)("(%ld,0x%08lx,%p,[%s])\n",
898 nr
,flags
,fun
,dbg_str(thunk
));
903 case 4: ret
= fun(args
[0]);
905 case 8: ret
= fun(args
[0],args
[1]);
907 case 12: ret
= fun(args
[0],args
[1],args
[2]);
909 case 16: ret
= fun(args
[0],args
[1],args
[2],args
[3]);
911 case 20: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4]);
913 case 24: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
915 case 28: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
917 case 32: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
919 case 36: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
921 case 40: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
923 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]);
925 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]);
928 WARN_(thunk
)("Unsupported nr of arguments, %ld\n",nr
);
933 TRACE_(thunk
)(" returning %ld ...\n",ret
);
937 /**********************************************************************
938 * W32S_BackTo32 (KERNEL32.51)
940 void WINAPI
REGS_FUNC(W32S_BackTo32
)( CONTEXT
*context
)
943 LPDWORD stack
= (LPDWORD
)ESP_reg( context
);
944 FARPROC proc
= (FARPROC
)EIP_reg(context
);
946 EAX_reg( context
) = proc( stack
[1], stack
[2], stack
[3], stack
[4], stack
[5],
947 stack
[6], stack
[7], stack
[8], stack
[9], stack
[10] );
949 EIP_reg( context
) = STACK32_POP(context
);
953 /**********************************************************************
954 * AllocSLCallback (KERNEL32)
956 * Win95 uses some structchains for callbacks. It allocates them
957 * in blocks of 100 entries, size 32 bytes each, layout:
959 * 0: PTR nextblockstart
961 * 8: WORD sel ( start points to blockstart)
965 * 18: PDB *owning_process;
968 * We ignore this for now. (Just a note for further developers)
969 * FIXME: use this method, so we don't waste selectors...
971 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
972 * the 0x66 prefix switches from word->long registers.
975 * 6668x arg2 x pushl <arg2>
977 * EAx arg1 x jmpf <arg1>
979 * returns the startaddress of this thunk.
981 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
983 * segmented pointer to the start of the thunk
987 DWORD finalizer
, /* [in] finalizer function */
988 DWORD callback
/* [in] callback function */
990 LPBYTE x
,thunk
= HeapAlloc( GetProcessHeap(), 0, 32 );
994 *x
++=0x66;*x
++=0x5a; /* popl edx */
995 *x
++=0x66;*x
++=0x68;*(DWORD
*)x
=finalizer
;x
+=4; /* pushl finalizer */
996 *x
++=0x66;*x
++=0x52; /* pushl edx */
997 *x
++=0xea;*(DWORD
*)x
=callback
;x
+=4; /* jmpf callback */
999 *(PDB
**)(thunk
+18) = PROCESS_Current();
1001 sel
= SELECTOR_AllocBlock( thunk
, 32, SEGMENT_CODE
, FALSE
, FALSE
);
1005 /**********************************************************************
1006 * FreeSLCallback (KERNEL32.274)
1007 * Frees the specified 16->32 callback
1011 DWORD x
/* [in] 16 bit callback (segmented pointer?) */
1013 FIXME_(win32
)("(0x%08lx): stub\n",x
);
1017 /**********************************************************************
1018 * GetTEBSelectorFS (KERNEL.475)
1019 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
1021 void WINAPI
GetTEBSelectorFS16(void)
1023 GET_FS( CURRENT_STACK16
->fs
);
1026 /**********************************************************************
1027 * KERNEL_431 (KERNEL.431)
1028 * IsPeFormat (W32SYS.2)
1029 * Checks the passed filename if it is a PE format executeable
1034 BOOL16 WINAPI
IsPeFormat16(
1035 LPSTR fn
, /* [in] filename to executeable */
1036 HFILE16 hf16
/* [in] open file, if filename is NULL */
1038 IMAGE_DOS_HEADER mzh
;
1039 HFILE hf
=FILE_GetHandle(hf16
);
1044 hf
= OpenFile(fn
,&ofs
,OF_READ
);
1045 if (hf
==HFILE_ERROR
)
1048 _llseek(hf
,0,SEEK_SET
);
1049 if (sizeof(mzh
)!=_lread(hf
,&mzh
,sizeof(mzh
))) {
1053 if (mzh
.e_magic
!=IMAGE_DOS_SIGNATURE
) {
1054 WARN_(dosmem
)("File has not got dos signature!\n");
1058 _llseek(hf
,mzh
.e_lfanew
,SEEK_SET
);
1059 if (sizeof(DWORD
)!=_lread(hf
,&xmagic
,sizeof(DWORD
))) {
1064 return (xmagic
== IMAGE_NT_SIGNATURE
);
1067 /***********************************************************************
1068 * WOWHandle32 (KERNEL32.57)(WOW32.16)
1069 * Converts a win16 handle of type into the respective win32 handle.
1070 * We currently just return this handle, since most handles are the same
1071 * for win16 and win32.
1075 HANDLE WINAPI
WOWHandle32(
1076 WORD handle
, /* [in] win16 handle */
1077 WOW_HANDLE_TYPE type
/* [in] handle type */
1079 TRACE_(win32
)("(0x%04x,%d)\n",handle
,type
);
1080 return (HANDLE
)handle
;
1083 /***********************************************************************
1084 * K32Thk1632Prolog (KERNEL32.492)
1086 void WINAPI
REGS_FUNC(K32Thk1632Prolog
)( CONTEXT
*context
)
1089 LPBYTE code
= (LPBYTE
)EIP_reg(context
) - 5;
1091 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1092 of 16->32 thunks instead of using one of the standard methods!
1093 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1094 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1095 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1096 bypassed, which means it will crash the next time the 32-bit OLE
1097 code thunks down again to 16-bit (this *will* happen!).
1099 The following hack tries to recognize this situation.
1100 This is possible since the called stubs in OLECLI32/OLESVR32 all
1101 look exactly the same:
1102 00 E8xxxxxxxx call K32Thk1632Prolog
1103 05 FF55FC call [ebp-04]
1104 08 E8xxxxxxxx call K32Thk1632Epilog
1107 If we recognize this situation, we try to simulate the actions
1108 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1109 to our 32-bit stack, creating a proper STACK16FRAME and
1110 updating cur_stack. */
1112 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1113 && code
[13] == 0x66 && code
[14] == 0xCB)
1115 WORD stackSel
= NtCurrentTeb()->stack_sel
;
1116 DWORD stackBase
= GetSelectorBase(stackSel
);
1118 DWORD argSize
= EBP_reg(context
) - ESP_reg(context
);
1119 char *stack16
= (char *)ESP_reg(context
) - 4;
1120 char *stack32
= (char *)NtCurrentTeb()->cur_stack
- argSize
;
1121 STACK16FRAME
*frame16
= (STACK16FRAME
*)stack16
- 1;
1123 TRACE_(thunk
)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1124 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1126 memset(frame16
, '\0', sizeof(STACK16FRAME
));
1127 frame16
->frame32
= (STACK32FRAME
*)NtCurrentTeb()->cur_stack
;
1128 frame16
->ebp
= EBP_reg(context
);
1130 memcpy(stack32
, stack16
, argSize
);
1131 NtCurrentTeb()->cur_stack
= PTR_SEG_OFF_TO_SEGPTR(stackSel
, (DWORD
)frame16
- stackBase
);
1133 ESP_reg(context
) = (DWORD
)stack32
+ 4;
1134 EBP_reg(context
) = ESP_reg(context
) + argSize
;
1136 TRACE_(thunk
)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1137 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1140 SYSLEVEL_ReleaseWin16Lock();
1144 /***********************************************************************
1145 * K32Thk1632Epilog (KERNEL32.491)
1147 void WINAPI
REGS_FUNC(K32Thk1632Epilog
)( CONTEXT
*context
)
1150 LPBYTE code
= (LPBYTE
)EIP_reg(context
) - 13;
1152 SYSLEVEL_RestoreWin16Lock();
1154 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1156 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1157 && code
[13] == 0x66 && code
[14] == 0xCB)
1159 STACK16FRAME
*frame16
= (STACK16FRAME
*)PTR_SEG_TO_LIN(NtCurrentTeb()->cur_stack
);
1160 char *stack16
= (char *)(frame16
+ 1);
1161 DWORD argSize
= frame16
->ebp
- (DWORD
)stack16
;
1162 char *stack32
= (char *)frame16
->frame32
- argSize
;
1164 DWORD nArgsPopped
= ESP_reg(context
) - (DWORD
)stack32
;
1166 TRACE_(thunk
)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1167 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1169 NtCurrentTeb()->cur_stack
= (DWORD
)frame16
->frame32
;
1171 ESP_reg(context
) = (DWORD
)stack16
+ nArgsPopped
;
1172 EBP_reg(context
) = frame16
->ebp
;
1174 TRACE_(thunk
)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1175 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1180 /***********************************************************************
1181 * UpdateResource32A (KERNEL32.707)
1183 BOOL WINAPI
UpdateResourceA(
1191 FIXME_(win32
)(": stub\n");
1192 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1196 /***********************************************************************
1197 * UpdateResource32W (KERNEL32.708)
1199 BOOL WINAPI
UpdateResourceW(
1207 FIXME_(win32
)(": stub\n");
1208 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1213 /***********************************************************************
1214 * WaitNamedPipe32A [KERNEL32.725]
1216 BOOL WINAPI
WaitNamedPipeA (LPCSTR lpNamedPipeName
, DWORD nTimeOut
)
1217 { FIXME_(win32
)("%s 0x%08lx\n",lpNamedPipeName
,nTimeOut
);
1218 SetLastError(ERROR_PIPE_NOT_CONNECTED
);
1221 /***********************************************************************
1222 * WaitNamedPipe32W [KERNEL32.726]
1224 BOOL WINAPI
WaitNamedPipeW (LPCWSTR lpNamedPipeName
, DWORD nTimeOut
)
1225 { FIXME_(win32
)("%s 0x%08lx\n",debugstr_w(lpNamedPipeName
),nTimeOut
);
1226 SetLastError(ERROR_PIPE_NOT_CONNECTED
);
1230 /*********************************************************************
1231 * PK16FNF [KERNEL32.91]
1233 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1234 * string buffer with the 8.3 filename of a recently loaded 16-bit
1235 * module. It is unknown exactly what modules trigger this
1236 * mechanism or what purpose this serves. Win98 Explorer (and
1237 * probably also Win95 with IE 4 shell integration) calls this
1238 * several times during initialization.
1240 * FIXME: find out what this really does and make it work.
1242 void WINAPI
PK16FNF(LPSTR strPtr
)
1244 FIXME_(win32
)("(%p): stub\n", strPtr
);
1246 /* fill in a fake filename that'll be easy to recognize */
1247 lstrcpyA(strPtr
, "WINESTUB.FIX");