2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
10 #include "builtin32.h"
18 BYTE call
; /* 0xe8 call callfrom32 (relative) */
19 DWORD callfrom32 WINE_PACKED
; /* RELAY_CallFrom32 relative addr */
20 BYTE ret
; /* 0xc2 ret $n or 0xc3 ret */
21 WORD args
; /* nb of args to remove from the stack */
26 BYTE pushl
; /* 0x68 pushl $func_to_call */
27 DWORD func WINE_PACKED
; /* func to call */
28 BYTE jmp
; /* 0xe9 jmp CALL32_Regs (relative) */
29 DWORD call32_regs WINE_PACKED
; /* CALL32_Regs relative addr */
30 WORD nop
; /* 0x9090 nop;nop */
35 const BUILTIN32_DESCRIPTOR
*descr
; /* DLL descriptor */
36 DEBUG_ENTRY_POINT
*dbg_funcs
; /* Relay debugging functions table*/
37 BOOL32 used
; /* Used by default */
41 extern const BUILTIN32_DESCRIPTOR ADVAPI32_Descriptor
;
42 extern const BUILTIN32_DESCRIPTOR COMCTL32_Descriptor
;
43 extern const BUILTIN32_DESCRIPTOR COMDLG32_Descriptor
;
44 extern const BUILTIN32_DESCRIPTOR CRTDLL_Descriptor
;
45 extern const BUILTIN32_DESCRIPTOR DCIMAN32_Descriptor
;
46 extern const BUILTIN32_DESCRIPTOR DDRAW_Descriptor
;
47 extern const BUILTIN32_DESCRIPTOR DINPUT_Descriptor
;
48 extern const BUILTIN32_DESCRIPTOR DPLAY_Descriptor
;
49 extern const BUILTIN32_DESCRIPTOR DSOUND_Descriptor
;
50 extern const BUILTIN32_DESCRIPTOR GDI32_Descriptor
;
51 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor
;
52 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor
;
53 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor
;
54 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor
;
55 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor
;
56 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor
;
57 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor
;
58 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor
;
59 extern const BUILTIN32_DESCRIPTOR SHELL32_Descriptor
;
60 extern const BUILTIN32_DESCRIPTOR TAPI32_Descriptor
;
61 extern const BUILTIN32_DESCRIPTOR USER32_Descriptor
;
62 extern const BUILTIN32_DESCRIPTOR VERSION_Descriptor
;
63 extern const BUILTIN32_DESCRIPTOR W32SKRNL_Descriptor
;
64 extern const BUILTIN32_DESCRIPTOR WINMM_Descriptor
;
65 extern const BUILTIN32_DESCRIPTOR WINSPOOL_Descriptor
;
66 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor
;
67 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor
;
69 static BUILTIN32_DLL BuiltinDLLs
[] =
71 { &ADVAPI32_Descriptor
, NULL
, TRUE
},
72 { &COMCTL32_Descriptor
, NULL
, FALSE
},
73 { &COMDLG32_Descriptor
, NULL
, TRUE
},
74 { &CRTDLL_Descriptor
, NULL
, TRUE
},
75 { &DCIMAN32_Descriptor
, NULL
, FALSE
},
76 { &DDRAW_Descriptor
, NULL
, TRUE
},
77 { &DINPUT_Descriptor
, NULL
, TRUE
},
78 { &DPLAY_Descriptor
, NULL
, TRUE
},
79 { &DSOUND_Descriptor
, NULL
, TRUE
},
80 { &GDI32_Descriptor
, NULL
, TRUE
},
81 { &KERNEL32_Descriptor
, NULL
, TRUE
},
82 { &LZ32_Descriptor
, NULL
, TRUE
},
83 { &MPR_Descriptor
, NULL
, TRUE
},
84 { &MSVFW32_Descriptor
, NULL
, FALSE
},
85 { &NTDLL_Descriptor
, NULL
, TRUE
},
86 { &OLE32_Descriptor
, NULL
, FALSE
},
87 { &OLECLI32_Descriptor
, NULL
, FALSE
},
88 { &OLESVR32_Descriptor
, NULL
, FALSE
},
89 { &SHELL32_Descriptor
, NULL
, TRUE
},
90 { &TAPI32_Descriptor
, NULL
, FALSE
},
91 { &USER32_Descriptor
, NULL
, TRUE
},
92 { &VERSION_Descriptor
, NULL
, TRUE
},
93 { &W32SKRNL_Descriptor
, NULL
, TRUE
},
94 { &WINMM_Descriptor
, NULL
, TRUE
},
95 { &WINSPOOL_Descriptor
, NULL
, TRUE
},
96 { &WOW32_Descriptor
, NULL
, FALSE
},
97 { &WSOCK32_Descriptor
, NULL
, TRUE
},
103 /***********************************************************************
104 * BUILTIN32_DoLoadModule
106 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadModule.
108 static HMODULE32
BUILTIN32_DoLoadModule( BUILTIN32_DLL
*dll
, PDB32
*pdb
)
110 extern void RELAY_CallFrom32();
111 extern void CALL32_Regs();
116 IMAGE_DATA_DIRECTORY
*dir
;
117 IMAGE_DOS_HEADER
*dos
;
118 IMAGE_NT_HEADERS
*nt
;
119 IMAGE_SECTION_HEADER
*sec
;
120 IMAGE_EXPORT_DIRECTORY
*exp
;
123 DEBUG_ENTRY_POINT
*debug
;
124 REG_ENTRY_POINT
*regs
;
129 /* Allocate the module */
131 size
= (sizeof(IMAGE_DOS_HEADER
)
132 + sizeof(IMAGE_NT_HEADERS
)
133 + 2 * sizeof(IMAGE_SECTION_HEADER
)
134 + sizeof(IMAGE_EXPORT_DIRECTORY
)
135 + dll
->descr
->nb_funcs
* sizeof(LPVOID
)
136 + dll
->descr
->nb_names
* sizeof(LPSTR
)
137 + dll
->descr
->nb_reg_funcs
* sizeof(REG_ENTRY_POINT
));
139 if (debugging_info(relay
))
140 size
+= dll
->descr
->nb_funcs
* sizeof(DEBUG_ENTRY_POINT
);
142 addr
= VirtualAlloc( NULL
, size
, MEM_COMMIT
, PAGE_EXECUTE_READWRITE
);
144 dos
= (IMAGE_DOS_HEADER
*)addr
;
145 nt
= (IMAGE_NT_HEADERS
*)(dos
+ 1);
146 sec
= (IMAGE_SECTION_HEADER
*)(nt
+ 1);
147 exp
= (IMAGE_EXPORT_DIRECTORY
*)(sec
+ 2);
148 funcs
= (LPVOID
*)(exp
+ 1);
149 names
= (LPSTR
*)(funcs
+ dll
->descr
->nb_funcs
);
150 regs
= (REG_ENTRY_POINT
*)(names
+ dll
->descr
->nb_names
);
151 debug
= (DEBUG_ENTRY_POINT
*)(regs
+ dll
->descr
->nb_reg_funcs
);
153 /* Build the DOS and NT headers */
155 dos
->e_magic
= IMAGE_DOS_SIGNATURE
;
156 dos
->e_lfanew
= sizeof(*dos
);
158 nt
->Signature
= IMAGE_NT_SIGNATURE
;
159 nt
->FileHeader
.Machine
= IMAGE_FILE_MACHINE_I386
;
160 nt
->FileHeader
.NumberOfSections
= 2; /* exports + code */
161 nt
->FileHeader
.SizeOfOptionalHeader
= sizeof(nt
->OptionalHeader
);
162 nt
->FileHeader
.Characteristics
= IMAGE_FILE_DLL
;
164 nt
->OptionalHeader
.Magic
= IMAGE_NT_OPTIONAL_HDR_MAGIC
;
165 nt
->OptionalHeader
.SizeOfCode
= 0x1000;
166 nt
->OptionalHeader
.SizeOfInitializedData
= 0;
167 nt
->OptionalHeader
.SizeOfUninitializedData
= 0;
168 nt
->OptionalHeader
.ImageBase
= (DWORD
)addr
;
169 nt
->OptionalHeader
.SectionAlignment
= 0x1000;
170 nt
->OptionalHeader
.FileAlignment
= 0x1000;
171 nt
->OptionalHeader
.MajorOperatingSystemVersion
= 1;
172 nt
->OptionalHeader
.MinorOperatingSystemVersion
= 0;
173 nt
->OptionalHeader
.MajorSubsystemVersion
= 4;
174 nt
->OptionalHeader
.MinorSubsystemVersion
= 0;
175 nt
->OptionalHeader
.SizeOfImage
= size
;
176 nt
->OptionalHeader
.SizeOfHeaders
= (BYTE
*)exp
- addr
;
177 nt
->OptionalHeader
.NumberOfRvaAndSizes
= IMAGE_NUMBEROF_DIRECTORY_ENTRIES
;
179 /* Build the export directory */
181 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
182 dir
->VirtualAddress
= (BYTE
*)exp
- addr
;
183 dir
->Size
= sizeof(*exp
)
184 + dll
->descr
->nb_funcs
* sizeof(LPVOID
)
185 + dll
->descr
->nb_names
* sizeof(LPSTR
);
187 /* Build the exports section */
189 strcpy( sec
->Name
, ".edata" );
190 sec
->Misc
.VirtualSize
= dir
->Size
;
191 sec
->VirtualAddress
= (BYTE
*)exp
- addr
;
192 sec
->SizeOfRawData
= dir
->Size
;
193 sec
->PointerToRawData
= (BYTE
*)exp
- addr
;
194 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
195 IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
|
196 IMAGE_SCN_MEM_WRITE
);
198 /* Build the code section */
201 strcpy( sec
->Name
, ".code" );
202 sec
->SizeOfRawData
= dll
->descr
->nb_reg_funcs
* sizeof(REG_ENTRY_POINT
);
204 if (debugging_info(relay
))
205 sec
->SizeOfRawData
+= dll
->descr
->nb_funcs
* sizeof(DEBUG_ENTRY_POINT
);
207 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
208 sec
->VirtualAddress
= (BYTE
*)regs
- addr
;
209 sec
->PointerToRawData
= (BYTE
*)regs
- addr
;
210 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
211 IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
);
213 /* Build the exports section data */
215 exp
->Name
= (BYTE
*)dll
->descr
->name
- addr
; /*??*/
216 exp
->Base
= dll
->descr
->base
;
217 exp
->NumberOfFunctions
= dll
->descr
->nb_funcs
;
218 exp
->NumberOfNames
= dll
->descr
->nb_names
;
219 exp
->AddressOfFunctions
= (LPDWORD
*)((BYTE
*)funcs
- addr
);
220 exp
->AddressOfNames
= (LPDWORD
*)((BYTE
*)names
- addr
);
221 exp
->AddressOfNameOrdinals
= (LPWORD
*)((BYTE
*)dll
->descr
->ordinals
- addr
);
223 /* Build the funcs table */
225 if (debugging_info(relay
)) dll
->dbg_funcs
= debug
;
226 for (i
= 0; i
< dll
->descr
->nb_funcs
; i
++, funcs
++, debug
++)
228 BYTE args
= dll
->descr
->args
[i
];
229 if (!dll
->descr
->functions
[i
]) continue;
233 case 0xfe: /* register func */
235 regs
->func
= (DWORD
)dll
->descr
->functions
[i
];
237 regs
->call32_regs
= (DWORD
)CALL32_Regs
- (DWORD
)®s
->nop
;
239 if (debugging_info(relay
))
242 debug
->callfrom32
= (DWORD
)regs
- (DWORD
)&debug
->ret
;
243 debug
->ret
= 0x90; /* nop */
245 *funcs
= (LPVOID
)((BYTE
*)debug
- addr
);
247 else *funcs
= (LPVOID
)((BYTE
*)regs
- addr
);
250 case 0xff: /* stub or extern */
251 *funcs
= (LPVOID
)((BYTE
*)dll
->descr
->functions
[i
] - addr
);
253 default: /* normal function (stdcall or cdecl) */
254 if (debugging_info(relay
))
257 debug
->callfrom32
= (DWORD
)RELAY_CallFrom32
-
259 debug
->ret
= (args
& 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
260 debug
->args
= (args
& 0x7f) * sizeof(int);
261 *funcs
= (LPVOID
)((BYTE
*)debug
- addr
);
264 *funcs
= (LPVOID
)((BYTE
*)dll
->descr
->functions
[i
] - addr
);
268 *funcs
= (LPVOID
)((BYTE
*)dll
->descr
->functions
[i
] - addr
);
269 #endif /* __i386__ */
272 /* Build the names table */
274 for (i
= 0; i
< exp
->NumberOfNames
; i
++, names
++)
275 if (dll
->descr
->names
[i
])
276 *names
= (LPSTR
)((BYTE
*)dll
->descr
->names
[i
] - addr
);
278 /* Create a modref */
280 pem
= (PE_MODREF
*)HeapAlloc( pdb
->heap
, HEAP_ZERO_MEMORY
, sizeof(*pem
) );
281 pem
->module
= (HMODULE32
)addr
;
282 pem
->pe_export
= exp
;
283 pem
->next
= pdb
->modref_list
;
284 pdb
->modref_list
= pem
;
286 /* Create a Win16 dummy module */
288 sprintf( ofs
.szPathName
, "%s.DLL", dll
->descr
->name
);
289 hModule
= MODULE_CreateDummyModule( &ofs
);
290 pModule
= (NE_MODULE
*)GlobalLock16( hModule
);
291 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
|
292 NE_FFLAGS_LIBMODULE
| NE_FFLAGS_WIN32
;
293 pModule
->module32
= (HMODULE32
)addr
;
294 return pModule
->module32
;
298 /***********************************************************************
299 * BUILTIN32_LoadModule
301 * Load a built-in module. If the 'force' parameter is FALSE, we only
302 * load the module if it has not been disabled via the -dll option.
304 HMODULE32
BUILTIN32_LoadModule( LPCSTR name
, BOOL32 force
, PDB32
*process
)
306 BUILTIN32_DLL
*table
;
307 char dllname
[16], *p
;
309 /* Fix the name in case we have a full path and extension */
311 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
312 lstrcpyn32A( dllname
, name
, sizeof(dllname
) );
313 if ((p
= strrchr( dllname
, '.' ))) *p
= '\0';
315 for (table
= BuiltinDLLs
; table
->descr
; table
++)
316 if (!lstrcmpi32A( table
->descr
->name
, dllname
)) break;
317 if (!table
->descr
) return 0;
318 if (!table
->used
&& !force
) return 0;
320 return BUILTIN32_DoLoadModule( table
, process
);
324 /***********************************************************************
325 * BUILTIN32_GetEntryPoint
327 * Return the name of the DLL entry point corresponding
328 * to a relay entry point address. This is used only by relay debugging.
330 * This function _must_ return the real entry point to call
331 * after the debug info is printed.
333 ENTRYPOINT32
BUILTIN32_GetEntryPoint( char *buffer
, void *relay
,
334 unsigned int *typemask
)
339 /* First find the module */
341 for (dll
= BuiltinDLLs
; dll
->descr
; dll
++)
342 if (((void *)dll
->dbg_funcs
<= relay
) &&
343 ((void *)(dll
->dbg_funcs
+ dll
->descr
->nb_funcs
) > relay
))
347 /* Now find the function */
349 ordinal
= ((DWORD
)relay
-(DWORD
)dll
->dbg_funcs
) / sizeof(DEBUG_ENTRY_POINT
);
350 for (i
= 0; i
< dll
->descr
->nb_names
; i
++)
351 if (dll
->descr
->ordinals
[i
] == ordinal
) break;
352 assert( i
< dll
->descr
->nb_names
);
354 sprintf( buffer
, "%s.%d: %s", dll
->descr
->name
, ordinal
+ dll
->descr
->base
,
355 dll
->descr
->names
[i
] );
356 *typemask
= dll
->descr
->argtypes
[ordinal
];
357 return dll
->descr
->functions
[ordinal
];
361 /***********************************************************************
362 * BUILTIN32_Unimplemented
364 * This function is called for unimplemented 32-bit entry points (declared
365 * as 'stub' in the spec file).
367 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR
*descr
, int ordinal
)
369 const char *func_name
= "???";
372 __RESTORE_ES
; /* Just in case */
374 for (i
= 0; i
< descr
->nb_names
; i
++)
375 if (descr
->ordinals
[i
] + descr
->base
== ordinal
) break;
376 if (i
< descr
->nb_names
) func_name
= descr
->names
[i
];
378 fprintf( stderr
, "No handler for Win32 routine %s.%d: %s",
379 descr
->name
, ordinal
, func_name
);
381 fprintf( stderr
, " (called from %p)", __builtin_return_address(1) );
383 fprintf( stderr
, "\n" );
384 TASK_KillCurrentTask(1);
388 /***********************************************************************
389 * BUILTIN32_EnableDLL
391 * Enable or disable a built-in DLL.
393 int BUILTIN32_EnableDLL( const char *name
, int len
, int enable
)
398 for (i
= 0, dll
= BuiltinDLLs
; dll
->descr
; dll
++)
400 if (!lstrncmpi32A( name
, dll
->descr
->name
, len
))
410 /***********************************************************************
411 * BUILTIN32_PrintDLLs
413 * Print the list of built-in DLLs that can be disabled.
415 void BUILTIN32_PrintDLLs(void)
420 fprintf(stderr
,"Available Win32 DLLs:\n");
421 for (i
= 0, dll
= BuiltinDLLs
; dll
->descr
; dll
++)
422 fprintf( stderr
, "%-9s%c", dll
->descr
->name
,
423 ((++i
) % 8) ? ' ' : '\n' );
424 fprintf(stderr
,"\n");