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
LogApiThkLSF( LPSTR func
, CONTEXT86
*context
)
57 TRACE_(thunk
)( "%s\n", debugstr_a(func
) );
60 /***********************************************************************
61 * LogApiThkSL (KERNEL32.44)
63 * NOTE: needs to preserve all registers!
65 void WINAPI
LogApiThkSL( LPSTR func
, CONTEXT86
*context
)
67 TRACE_(thunk
)( "%s\n", debugstr_a(func
) );
70 /***********************************************************************
71 * LogCBThkSL (KERNEL32.47)
73 * NOTE: needs to preserve all registers!
75 void WINAPI
LogCBThkSL( LPSTR func
, CONTEXT86
*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
QT_Thunk( CONTEXT86
*context
)
298 memcpy(&context16
,context
,sizeof(context16
));
300 CS_reg(&context16
) = HIWORD(EDX_reg(context
));
301 EIP_reg(&context16
) = LOWORD(EDX_reg(context
));
302 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
303 + (WORD
)&((STACK16FRAME
*)0)->bp
;
305 argsize
= EBP_reg(context
)-ESP_reg(context
)-0x40;
307 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
308 (LPBYTE
)ESP_reg(context
), argsize
);
310 EAX_reg(context
) = Callbacks
->CallRegisterShortProc( &context16
, argsize
);
311 EDX_reg(context
) = HIWORD(EAX_reg(context
));
312 EAX_reg(context
) = LOWORD(EAX_reg(context
));
316 /**********************************************************************
317 * FT_Prolog (KERNEL32.233)
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
, EBP_reg(context
));
364 EBP_reg(context
) = ESP_reg(context
);
366 /* Allocate 64-byte Thunk Buffer */
367 ESP_reg(context
) -= 64;
368 memset((char *)ESP_reg(context
), '\0', 64);
370 /* Store Flags (ECX) and Target Address (EDX) */
371 /* Save other registers to be restored later */
372 *(DWORD
*)(EBP_reg(context
) - 4) = EBX_reg(context
);
373 *(DWORD
*)(EBP_reg(context
) - 8) = ESI_reg(context
);
374 *(DWORD
*)(EBP_reg(context
) - 12) = EDI_reg(context
);
375 *(DWORD
*)(EBP_reg(context
) - 16) = ECX_reg(context
);
377 *(DWORD
*)(EBP_reg(context
) - 48) = EAX_reg(context
);
378 *(DWORD
*)(EBP_reg(context
) - 52) = EDX_reg(context
);
381 /**********************************************************************
382 * FT_Thunk (KERNEL32.234)
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
*)(EBP_reg(context
) - 20);
405 DWORD callTarget
= *(DWORD
*)(EBP_reg(context
) - 52);
409 LPBYTE newstack
, oldstack
;
411 memcpy(&context16
,context
,sizeof(context16
));
413 CS_reg(&context16
) = HIWORD(callTarget
);
414 EIP_reg(&context16
) = LOWORD(callTarget
);
415 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
416 + (WORD
)&((STACK16FRAME
*)0)->bp
;
418 argsize
= EBP_reg(context
)-ESP_reg(context
)-0x40;
419 newstack
= (LPBYTE
)CURRENT_STACK16
- argsize
;
420 oldstack
= (LPBYTE
)ESP_reg(context
);
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
= PTR_SEG_OFF_TO_SEGPTR(SELECTOROF(NtCurrentTeb()->cur_stack
),
429 OFFSETOF(NtCurrentTeb()->cur_stack
) - argsize
430 + (*(LPBYTE
*)arg
- oldstack
));
433 EAX_reg(context
) = Callbacks
->CallRegisterShortProc( &context16
, argsize
);
434 EDX_reg(context
) = HIWORD(EAX_reg(context
));
435 EAX_reg(context
) = LOWORD(EAX_reg(context
));
437 /* Copy modified buffers back to 32-bit stack */
438 memcpy( oldstack
, newstack
, argsize
);
441 /**********************************************************************
442 * FT_ExitNN (KERNEL32.218 - 232)
444 * One of the FT_ExitNN functions is called at the end of the thunk code.
445 * It removes the stack frame created by FT_Prolog, moves the function
446 * return from EBX to EAX (yes, FT_Thunk did use EAX for the return
447 * value, but the thunk code has moved it from EAX to EBX in the
448 * meantime ... :-), restores the caller's EBX, ESI, and EDI registers,
449 * and perform a return to the CALLER of the thunk code (while removing
450 * the given number of arguments from the caller's stack).
453 static void FT_Exit(CONTEXT86
*context
, int nPopArgs
)
455 /* Return value is in EBX */
456 EAX_reg(context
) = EBX_reg(context
);
458 /* Restore EBX, ESI, and EDI registers */
459 EBX_reg(context
) = *(DWORD
*)(EBP_reg(context
) - 4);
460 ESI_reg(context
) = *(DWORD
*)(EBP_reg(context
) - 8);
461 EDI_reg(context
) = *(DWORD
*)(EBP_reg(context
) - 12);
463 /* Clean up stack frame */
464 ESP_reg(context
) = EBP_reg(context
);
465 EBP_reg(context
) = stack32_pop(context
);
467 /* Pop return address to CALLER of thunk code */
468 EIP_reg(context
) = stack32_pop(context
);
469 /* Remove arguments */
470 ESP_reg(context
) += nPopArgs
;
473 void WINAPI
FT_Exit0 (CONTEXT86
*context
) { FT_Exit(context
, 0); }
474 void WINAPI
FT_Exit4 (CONTEXT86
*context
) { FT_Exit(context
, 4); }
475 void WINAPI
FT_Exit8 (CONTEXT86
*context
) { FT_Exit(context
, 8); }
476 void WINAPI
FT_Exit12(CONTEXT86
*context
) { FT_Exit(context
, 12); }
477 void WINAPI
FT_Exit16(CONTEXT86
*context
) { FT_Exit(context
, 16); }
478 void WINAPI
FT_Exit20(CONTEXT86
*context
) { FT_Exit(context
, 20); }
479 void WINAPI
FT_Exit24(CONTEXT86
*context
) { FT_Exit(context
, 24); }
480 void WINAPI
FT_Exit28(CONTEXT86
*context
) { FT_Exit(context
, 28); }
481 void WINAPI
FT_Exit32(CONTEXT86
*context
) { FT_Exit(context
, 32); }
482 void WINAPI
FT_Exit36(CONTEXT86
*context
) { FT_Exit(context
, 36); }
483 void WINAPI
FT_Exit40(CONTEXT86
*context
) { FT_Exit(context
, 40); }
484 void WINAPI
FT_Exit44(CONTEXT86
*context
) { FT_Exit(context
, 44); }
485 void WINAPI
FT_Exit48(CONTEXT86
*context
) { FT_Exit(context
, 48); }
486 void WINAPI
FT_Exit52(CONTEXT86
*context
) { FT_Exit(context
, 52); }
487 void WINAPI
FT_Exit56(CONTEXT86
*context
) { FT_Exit(context
, 56); }
490 /***********************************************************************
491 * ThunkInitLS (KERNEL32.43)
492 * A thunkbuffer link routine
493 * The thunkbuf looks like:
495 * 00: DWORD length ? don't know exactly
496 * 04: SEGPTR ptr ? where does it point to?
497 * The pointer ptr is written into the first DWORD of 'thunk'.
498 * (probably correct implemented)
501 * segmented pointer to thunk?
503 DWORD WINAPI
ThunkInitLS(
504 LPDWORD thunk
, /* [in] win32 thunk */
505 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
506 DWORD len
, /* [in] thkbuffer length */
507 LPCSTR dll16
, /* [in] name of win16 dll */
508 LPCSTR dll32
/* [in] name of win32 dll (FIXME: not used?) */
512 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
517 *(DWORD
*)thunk
= addr
[1];
522 /***********************************************************************
523 * Common32ThkLS (KERNEL32.45)
525 * This is another 32->16 thunk, independent of the QT_Thunk/FT_Thunk
526 * style thunks. The basic difference is that the parameter conversion
527 * is done completely on the *16-bit* side here. Thus we do not call
528 * the 16-bit target directly, but call a common entry point instead.
529 * This entry function then calls the target according to the target
530 * number passed in the DI register.
532 * Input: EAX SEGPTR to the common 16-bit entry point
533 * CX offset in thunk table (target number * 4)
534 * DX error return value if execution fails (unclear???)
535 * EDX.HI number of DWORD parameters
537 * (Note that we need to move the thunk table offset from CX to DI !)
539 * The called 16-bit stub expects its stack to look like this:
541 * (esp+40) 32-bit arguments
543 * (esp+8) 32 byte of stack space available as buffer
544 * (esp) 8 byte return address for use with 0x66 lret
546 * The called 16-bit stub uses a 0x66 lret to return to 32-bit code,
547 * and uses the EAX register to return a DWORD return value.
548 * Thus we need to use a special assembly glue routine
549 * (CallRegisterLongProc instead of CallRegisterShortProc).
551 * Finally, we return to the caller, popping the arguments off
554 * FIXME: The called function uses EBX to return the number of
555 * arguments that are to be popped off the caller's stack.
556 * This is clobbered by the assembly glue, so we simply use
557 * the original EDX.HI to get the number of arguments.
558 * (Those two values should be equal anyway ...?)
561 void WINAPI
Common32ThkLS( CONTEXT86
*context
)
566 memcpy(&context16
,context
,sizeof(context16
));
568 DI_reg(&context16
) = CX_reg(context
);
569 CS_reg(&context16
) = HIWORD(EAX_reg(context
));
570 EIP_reg(&context16
) = LOWORD(EAX_reg(context
));
571 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
572 + (WORD
)&((STACK16FRAME
*)0)->bp
;
574 argsize
= HIWORD(EDX_reg(context
)) * 4;
576 /* FIXME: hack for stupid USER32 CallbackGlueLS routine */
577 if (EDX_reg(context
) == EIP_reg(context
))
580 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
581 (LPBYTE
)ESP_reg(context
), argsize
);
583 EAX_reg(context
) = Callbacks
->CallRegisterLongProc(&context16
, argsize
+ 32);
585 /* Clean up caller's stack frame */
586 ESP_reg(context
) += argsize
;
589 /***********************************************************************
590 * OT_32ThkLSF (KERNEL32.40)
592 * YET Another 32->16 thunk. The difference to Common32ThkLS is that
593 * argument processing is done on both the 32-bit and the 16-bit side:
594 * The 32-bit side prepares arguments, copying them onto the stack.
596 * When this routine is called, the first word on the stack is the
597 * number of argument bytes prepared by the 32-bit code, and EDX
598 * contains the 16-bit target address.
600 * The called 16-bit routine is another relaycode, doing further
601 * argument processing and then calling the real 16-bit target
602 * whose address is stored at [bp-04].
604 * The call proceeds using a normal CallRegisterShortProc.
605 * After return from the 16-bit relaycode, the arguments need
606 * to be copied *back* to the 32-bit stack, since the 32-bit
607 * relaycode processes output parameters.
609 * Note that we copy twice the number of arguments, since some of the
610 * 16-bit relaycodes in SYSTHUNK.DLL directly access the original
611 * arguments of the caller!
613 * (Note that this function seems only to be used for
614 * OLECLI32 -> OLECLI and OLESVR32 -> OLESVR thunking.)
616 void WINAPI
OT_32ThkLSF( CONTEXT86
*context
)
621 memcpy(&context16
,context
,sizeof(context16
));
623 CS_reg(&context16
) = HIWORD(EDX_reg(context
));
624 EIP_reg(&context16
) = LOWORD(EDX_reg(context
));
625 EBP_reg(&context16
) = OFFSETOF( NtCurrentTeb()->cur_stack
)
626 + (WORD
)&((STACK16FRAME
*)0)->bp
;
628 argsize
= 2 * *(WORD
*)ESP_reg(context
) + 2;
630 memcpy( (LPBYTE
)CURRENT_STACK16
- argsize
,
631 (LPBYTE
)ESP_reg(context
), argsize
);
633 EAX_reg(context
) = Callbacks
->CallRegisterShortProc(&context16
, argsize
);
635 memcpy( (LPBYTE
)ESP_reg(context
),
636 (LPBYTE
)CURRENT_STACK16
- argsize
, argsize
);
639 /***********************************************************************
640 * ThunkInitLSF (KERNEL32.41)
641 * A thunk setup routine.
642 * Expects a pointer to a preinitialized thunkbuffer in the first argument
644 * 00..03: unknown (pointer, check _41, _43, _46)
647 * 06..23: unknown (space for replacement code, check .90)
649 * 24:>E800000000 call offset 29
650 * 29:>58 pop eax ( target of call )
651 * 2A: 2D25000000 sub eax,0x00000025 ( now points to offset 4 )
652 * 2F: BAxxxxxxxx mov edx,xxxxxxxx
653 * 34: 68yyyyyyyy push KERNEL32.90
657 * 3E ... 59: unknown (space for replacement code?)
658 * 5A: E8xxxxxxxx call <32bitoffset xxxxxxxx>
660 * 60: 81EA25xxxxxx sub edx, 0x25xxxxxx
662 * 67: 68xxxxxxxx push xxxxxxxx
663 * 6C: 68yyyyyyyy push KERNEL32.89
666 * This function checks if the code is there, and replaces the yyyyyyyy entries
667 * by the functionpointers.
668 * The thunkbuf looks like:
670 * 00: DWORD length ? don't know exactly
671 * 04: SEGPTR ptr ? where does it point to?
672 * The segpointer ptr is written into the first DWORD of 'thunk'.
675 * unclear, pointer to win16 thkbuffer?
677 LPVOID WINAPI
ThunkInitLSF(
678 LPBYTE thunk
, /* [in] win32 thunk */
679 LPCSTR thkbuf
, /* [in] thkbuffer name in win16 dll */
680 DWORD len
, /* [in] length of thkbuffer */
681 LPCSTR dll16
, /* [in] name of win16 dll */
682 LPCSTR dll32
/* [in] name of win32 dll */
684 HMODULE hkrnl32
= GetModuleHandleA("KERNEL32");
687 /* FIXME: add checks for valid code ... */
688 /* write pointers to kernel32.89 and kernel32.90 (+ordinal base of 1) */
689 *(DWORD
*)(thunk
+0x35) = (DWORD
)GetProcAddress(hkrnl32
,(LPSTR
)90);
690 *(DWORD
*)(thunk
+0x6D) = (DWORD
)GetProcAddress(hkrnl32
,(LPSTR
)89);
693 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
696 addr2
= PTR_SEG_TO_LIN(addr
[1]);
698 *(DWORD
*)thunk
= (DWORD
)addr2
;
703 /***********************************************************************
704 * FT_PrologPrime (KERNEL32.89)
706 * This function is called from the relay code installed by
707 * ThunkInitLSF. It replaces the location from where it was
708 * called by a standard FT_Prolog call stub (which is 'primed'
709 * by inserting the correct target table pointer).
710 * Finally, it calls that stub.
712 * Input: ECX target number + flags (passed through to FT_Prolog)
713 * (ESP) offset of location where target table pointer
714 * is stored, relative to the start of the relay code
715 * (ESP+4) pointer to start of relay code
716 * (this is where the FT_Prolog call stub gets written to)
718 * Note: The two DWORD arguments get popped off the stack.
721 void WINAPI
FT_PrologPrime( CONTEXT86
*context
)
723 DWORD targetTableOffset
;
726 /* Compensate for the fact that the Wine register relay code thought
727 we were being called, although we were in fact jumped to */
728 ESP_reg(context
) -= 4;
730 /* Write FT_Prolog call stub */
731 targetTableOffset
= stack32_pop(context
);
732 relayCode
= (LPBYTE
)stack32_pop(context
);
733 _write_ftprolog( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
735 /* Jump to the call stub just created */
736 EIP_reg(context
) = (DWORD
)relayCode
;
739 /***********************************************************************
740 * QT_ThunkPrime (KERNEL32.90)
742 * This function corresponds to FT_PrologPrime, but installs a
743 * call stub for QT_Thunk instead.
745 * Input: (EBP-4) target number (passed through to QT_Thunk)
746 * EDX target table pointer location offset
747 * EAX start of relay code
750 void WINAPI
QT_ThunkPrime( CONTEXT86
*context
)
752 DWORD targetTableOffset
;
755 /* Compensate for the fact that the Wine register relay code thought
756 we were being called, although we were in fact jumped to */
757 ESP_reg(context
) -= 4;
759 /* Write QT_Thunk call stub */
760 targetTableOffset
= EDX_reg(context
);
761 relayCode
= (LPBYTE
)EAX_reg(context
);
762 _write_qtthunk( relayCode
, *(DWORD
**)(relayCode
+targetTableOffset
) );
764 /* Jump to the call stub just created */
765 EIP_reg(context
) = (DWORD
)relayCode
;
768 /***********************************************************************
770 * Another thunkbuf link routine.
771 * The start of the thunkbuf looks like this:
773 * 04: SEGPTR address for thunkbuffer pointer
776 VOID WINAPI
ThunkInitSL(
777 LPBYTE thunk
, /* [in] start of thunkbuffer */
778 LPCSTR thkbuf
, /* [in] name/ordinal of thunkbuffer in win16 dll */
779 DWORD len
, /* [in] length of thunkbuffer */
780 LPCSTR dll16
, /* [in] name of win16 dll containing the thkbuf */
781 LPCSTR dll32
/* [in] win32 dll. FIXME: strange, unused */
785 if (!(addr
= _loadthunk( dll16
, thkbuf
, dll32
, NULL
, len
)))
788 *(DWORD
*)PTR_SEG_TO_LIN(addr
[1]) = (DWORD
)thunk
;
791 /**********************************************************************
796 BOOL WINAPI
SSInit16()
801 /**********************************************************************
802 * SSOnBigStack KERNEL32.87
803 * Check if thunking is initialized (ss selector set up etc.)
804 * We do that differently, so just return TRUE.
809 BOOL WINAPI
SSOnBigStack()
811 TRACE_(thunk
)("Yes, thunking is initialized\n");
815 /**********************************************************************
816 * SSConfirmSmallStack KERNEL.704
818 * Abort if not on small stack.
820 * This must be a register routine as it has to preserve *all* registers.
822 void WINAPI
SSConfirmSmallStack( CONTEXT86
*context
)
824 /* We are always on the small stack while in 16-bit code ... */
827 /**********************************************************************
829 * One of the real thunking functions. This one seems to be for 32<->32
830 * thunks. It should probably be capable of crossing processboundaries.
832 * And YES, I've seen nr=48 (somewhere in the Win95 32<->16 OLE coupling)
835 DWORD WINAPIV
SSCall(
836 DWORD nr
, /* [in] number of argument bytes */
837 DWORD flags
, /* [in] FIXME: flags ? */
838 FARPROC fun
, /* [in] function to call */
839 ... /* [in/out] arguments */
842 DWORD
*args
= ((DWORD
*)&fun
) + 1;
846 DPRINTF("(%ld,0x%08lx,%p,[",nr
,flags
,fun
);
848 DPRINTF("0x%08lx,",args
[i
]);
854 case 4: ret
= fun(args
[0]);
856 case 8: ret
= fun(args
[0],args
[1]);
858 case 12: ret
= fun(args
[0],args
[1],args
[2]);
860 case 16: ret
= fun(args
[0],args
[1],args
[2],args
[3]);
862 case 20: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4]);
864 case 24: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5]);
866 case 28: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6]);
868 case 32: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7]);
870 case 36: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8]);
872 case 40: ret
= fun(args
[0],args
[1],args
[2],args
[3],args
[4],args
[5],args
[6],args
[7],args
[8],args
[9]);
874 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]);
876 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]);
879 WARN_(thunk
)("Unsupported nr of arguments, %ld\n",nr
);
884 TRACE_(thunk
)(" returning %ld ...\n",ret
);
888 /**********************************************************************
889 * W32S_BackTo32 (KERNEL32.51)
891 void WINAPI
W32S_BackTo32( CONTEXT86
*context
)
893 LPDWORD stack
= (LPDWORD
)ESP_reg( context
);
894 FARPROC proc
= (FARPROC
)EIP_reg(context
);
896 EAX_reg( context
) = proc( stack
[1], stack
[2], stack
[3], stack
[4], stack
[5],
897 stack
[6], stack
[7], stack
[8], stack
[9], stack
[10] );
899 EIP_reg( context
) = stack32_pop(context
);
902 /**********************************************************************
903 * AllocSLCallback (KERNEL32)
905 * Win95 uses some structchains for callbacks. It allocates them
906 * in blocks of 100 entries, size 32 bytes each, layout:
908 * 0: PTR nextblockstart
910 * 8: WORD sel ( start points to blockstart)
914 * 18: PDB *owning_process;
917 * We ignore this for now. (Just a note for further developers)
918 * FIXME: use this method, so we don't waste selectors...
920 * Following code is then generated by AllocSLCallback. The code is 16 bit, so
921 * the 0x66 prefix switches from word->long registers.
924 * 6668x arg2 x pushl <arg2>
926 * EAx arg1 x jmpf <arg1>
928 * returns the startaddress of this thunk.
930 * Note, that they look very similair to the ones allocates by THUNK_Alloc.
932 * segmented pointer to the start of the thunk
936 DWORD finalizer
, /* [in] finalizer function */
937 DWORD callback
/* [in] callback function */
939 LPBYTE x
,thunk
= HeapAlloc( GetProcessHeap(), 0, 32 );
943 *x
++=0x66;*x
++=0x5a; /* popl edx */
944 *x
++=0x66;*x
++=0x68;*(DWORD
*)x
=finalizer
;x
+=4; /* pushl finalizer */
945 *x
++=0x66;*x
++=0x52; /* pushl edx */
946 *x
++=0xea;*(DWORD
*)x
=callback
;x
+=4; /* jmpf callback */
948 *(PDB
**)(thunk
+18) = PROCESS_Current();
950 sel
= SELECTOR_AllocBlock( thunk
, 32, SEGMENT_CODE
, FALSE
, FALSE
);
954 /**********************************************************************
955 * FreeSLCallback (KERNEL32.274)
956 * Frees the specified 16->32 callback
960 DWORD x
/* [in] 16 bit callback (segmented pointer?) */
962 FIXME_(win32
)("(0x%08lx): stub\n",x
);
966 /**********************************************************************
967 * GetTEBSelectorFS (KERNEL.475)
968 * Set the 16-bit %fs to the 32-bit %fs (current TEB selector)
970 void WINAPI
GetTEBSelectorFS16(void)
972 GET_FS( CURRENT_STACK16
->fs
);
975 /**********************************************************************
976 * KERNEL_431 (KERNEL.431)
977 * IsPeFormat (W32SYS.2)
978 * Checks the passed filename if it is a PE format executeable
983 BOOL16 WINAPI
IsPeFormat16(
984 LPSTR fn
, /* [in] filename to executeable */
985 HFILE16 hf16
/* [in] open file, if filename is NULL */
987 IMAGE_DOS_HEADER mzh
;
988 HFILE hf
=FILE_GetHandle(hf16
);
993 hf
= OpenFile(fn
,&ofs
,OF_READ
);
997 _llseek(hf
,0,SEEK_SET
);
998 if (sizeof(mzh
)!=_lread(hf
,&mzh
,sizeof(mzh
))) {
1002 if (mzh
.e_magic
!=IMAGE_DOS_SIGNATURE
) {
1003 WARN_(dosmem
)("File has not got dos signature!\n");
1007 _llseek(hf
,mzh
.e_lfanew
,SEEK_SET
);
1008 if (sizeof(DWORD
)!=_lread(hf
,&xmagic
,sizeof(DWORD
))) {
1013 return (xmagic
== IMAGE_NT_SIGNATURE
);
1017 /***********************************************************************
1018 * K32Thk1632Prolog (KERNEL32.492)
1020 void WINAPI
K32Thk1632Prolog( CONTEXT86
*context
)
1022 LPBYTE code
= (LPBYTE
)EIP_reg(context
) - 5;
1024 /* Arrrgh! SYSTHUNK.DLL just has to re-implement another method
1025 of 16->32 thunks instead of using one of the standard methods!
1026 This means that SYSTHUNK.DLL itself switches to a 32-bit stack,
1027 and does a far call to the 32-bit code segment of OLECLI32/OLESVR32.
1028 Unfortunately, our CallTo/CallFrom mechanism is therefore completely
1029 bypassed, which means it will crash the next time the 32-bit OLE
1030 code thunks down again to 16-bit (this *will* happen!).
1032 The following hack tries to recognize this situation.
1033 This is possible since the called stubs in OLECLI32/OLESVR32 all
1034 look exactly the same:
1035 00 E8xxxxxxxx call K32Thk1632Prolog
1036 05 FF55FC call [ebp-04]
1037 08 E8xxxxxxxx call K32Thk1632Epilog
1040 If we recognize this situation, we try to simulate the actions
1041 of our CallTo/CallFrom mechanism by copying the 16-bit stack
1042 to our 32-bit stack, creating a proper STACK16FRAME and
1043 updating cur_stack. */
1045 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1046 && code
[13] == 0x66 && code
[14] == 0xCB)
1048 WORD stackSel
= NtCurrentTeb()->stack_sel
;
1049 DWORD stackBase
= GetSelectorBase(stackSel
);
1051 DWORD argSize
= EBP_reg(context
) - ESP_reg(context
);
1052 char *stack16
= (char *)ESP_reg(context
) - 4;
1053 char *stack32
= (char *)NtCurrentTeb()->cur_stack
- argSize
;
1054 STACK16FRAME
*frame16
= (STACK16FRAME
*)stack16
- 1;
1056 TRACE_(thunk
)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1057 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1059 memset(frame16
, '\0', sizeof(STACK16FRAME
));
1060 frame16
->frame32
= (STACK32FRAME
*)NtCurrentTeb()->cur_stack
;
1061 frame16
->ebp
= EBP_reg(context
);
1063 memcpy(stack32
, stack16
, argSize
);
1064 NtCurrentTeb()->cur_stack
= PTR_SEG_OFF_TO_SEGPTR(stackSel
, (DWORD
)frame16
- stackBase
);
1066 ESP_reg(context
) = (DWORD
)stack32
+ 4;
1067 EBP_reg(context
) = ESP_reg(context
) + argSize
;
1069 TRACE_(thunk
)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1070 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1073 SYSLEVEL_ReleaseWin16Lock();
1076 /***********************************************************************
1077 * K32Thk1632Epilog (KERNEL32.491)
1079 void WINAPI
K32Thk1632Epilog( CONTEXT86
*context
)
1081 LPBYTE code
= (LPBYTE
)EIP_reg(context
) - 13;
1083 SYSLEVEL_RestoreWin16Lock();
1085 /* We undo the SYSTHUNK hack if necessary. See K32Thk1632Prolog. */
1087 if ( code
[5] == 0xFF && code
[6] == 0x55 && code
[7] == 0xFC
1088 && code
[13] == 0x66 && code
[14] == 0xCB)
1090 STACK16FRAME
*frame16
= (STACK16FRAME
*)PTR_SEG_TO_LIN(NtCurrentTeb()->cur_stack
);
1091 char *stack16
= (char *)(frame16
+ 1);
1092 DWORD argSize
= frame16
->ebp
- (DWORD
)stack16
;
1093 char *stack32
= (char *)frame16
->frame32
- argSize
;
1095 DWORD nArgsPopped
= ESP_reg(context
) - (DWORD
)stack32
;
1097 TRACE_(thunk
)("before SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1098 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1100 NtCurrentTeb()->cur_stack
= (DWORD
)frame16
->frame32
;
1102 ESP_reg(context
) = (DWORD
)stack16
+ nArgsPopped
;
1103 EBP_reg(context
) = frame16
->ebp
;
1105 TRACE_(thunk
)("after SYSTHUNK hack: EBP: %08lx ESP: %08lx cur_stack: %08lx\n",
1106 EBP_reg(context
), ESP_reg(context
), NtCurrentTeb()->cur_stack
);
1110 /***********************************************************************
1111 * UpdateResource32A (KERNEL32.707)
1113 BOOL WINAPI
UpdateResourceA(
1121 FIXME_(win32
)(": stub\n");
1122 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1126 /***********************************************************************
1127 * UpdateResource32W (KERNEL32.708)
1129 BOOL WINAPI
UpdateResourceW(
1137 FIXME_(win32
)(": stub\n");
1138 SetLastError(ERROR_CALL_NOT_IMPLEMENTED
);
1143 /***********************************************************************
1144 * WaitNamedPipe32A [KERNEL32.725]
1146 BOOL WINAPI
WaitNamedPipeA (LPCSTR lpNamedPipeName
, DWORD nTimeOut
)
1147 { FIXME_(win32
)("%s 0x%08lx\n",lpNamedPipeName
,nTimeOut
);
1148 SetLastError(ERROR_PIPE_NOT_CONNECTED
);
1151 /***********************************************************************
1152 * WaitNamedPipe32W [KERNEL32.726]
1154 BOOL WINAPI
WaitNamedPipeW (LPCWSTR lpNamedPipeName
, DWORD nTimeOut
)
1155 { FIXME_(win32
)("%s 0x%08lx\n",debugstr_w(lpNamedPipeName
),nTimeOut
);
1156 SetLastError(ERROR_PIPE_NOT_CONNECTED
);
1160 /*********************************************************************
1161 * PK16FNF [KERNEL32.91]
1163 * This routine fills in the supplied 13-byte (8.3 plus terminator)
1164 * string buffer with the 8.3 filename of a recently loaded 16-bit
1165 * module. It is unknown exactly what modules trigger this
1166 * mechanism or what purpose this serves. Win98 Explorer (and
1167 * probably also Win95 with IE 4 shell integration) calls this
1168 * several times during initialization.
1170 * FIXME: find out what this really does and make it work.
1172 void WINAPI
PK16FNF(LPSTR strPtr
)
1174 FIXME_(win32
)("(%p): stub\n", strPtr
);
1176 /* fill in a fake filename that'll be easy to recognize */
1177 lstrcpyA(strPtr
, "WINESTUB.FIX");