Delphi 2.0 needs to allocate a bitmap bigger than 4096 bits wide.
[wine/multimedia.git] / relay32 / builtin32.c
blob55f3fcf9a63989d966d860300a9e5b828c1f25eb
1 /*
2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include "builtin32.h"
10 #include "peexe.h"
11 #include "heap.h"
12 #include "debug.h"
14 typedef struct
16 BYTE call; /* 0xe8 call callfrom32 (relative) */
17 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
18 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
19 WORD args; /* nb of args to remove from the stack */
20 } DEBUG_ENTRY_POINT;
22 typedef struct
24 const BUILTIN32_DESCRIPTOR *descr; /* DLL descriptor */
25 BOOL32 used; /* Used by default */
26 } BUILTIN32_DLL;
29 extern const BUILTIN32_DESCRIPTOR ADVAPI32_Descriptor;
30 extern const BUILTIN32_DESCRIPTOR AVIFIL32_Descriptor;
31 extern const BUILTIN32_DESCRIPTOR COMCTL32_Descriptor;
32 extern const BUILTIN32_DESCRIPTOR COMDLG32_Descriptor;
33 extern const BUILTIN32_DESCRIPTOR CRTDLL_Descriptor;
34 extern const BUILTIN32_DESCRIPTOR DCIMAN32_Descriptor;
35 extern const BUILTIN32_DESCRIPTOR DDRAW_Descriptor;
36 extern const BUILTIN32_DESCRIPTOR DINPUT_Descriptor;
37 extern const BUILTIN32_DESCRIPTOR DPLAY_Descriptor;
38 extern const BUILTIN32_DESCRIPTOR DPLAYX_Descriptor;
39 extern const BUILTIN32_DESCRIPTOR DSOUND_Descriptor;
40 extern const BUILTIN32_DESCRIPTOR GDI32_Descriptor;
41 extern const BUILTIN32_DESCRIPTOR IMAGEHLP_Descriptor;
42 extern const BUILTIN32_DESCRIPTOR IMM32_Descriptor;
43 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor;
44 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor;
45 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor;
46 extern const BUILTIN32_DESCRIPTOR MSACM32_Descriptor;
47 extern const BUILTIN32_DESCRIPTOR MSNET32_Descriptor;
48 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
49 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
50 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
51 extern const BUILTIN32_DESCRIPTOR OLEAUT32_Descriptor;
52 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
53 extern const BUILTIN32_DESCRIPTOR OLEDLG_Descriptor;
54 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor;
55 extern const BUILTIN32_DESCRIPTOR PSAPI_Descriptor;
56 extern const BUILTIN32_DESCRIPTOR RASAPI32_Descriptor;
57 extern const BUILTIN32_DESCRIPTOR SHELL32_Descriptor;
58 extern const BUILTIN32_DESCRIPTOR TAPI32_Descriptor;
59 extern const BUILTIN32_DESCRIPTOR USER32_Descriptor;
60 extern const BUILTIN32_DESCRIPTOR VERSION_Descriptor;
61 extern const BUILTIN32_DESCRIPTOR W32SKRNL_Descriptor;
62 extern const BUILTIN32_DESCRIPTOR WINMM_Descriptor;
63 extern const BUILTIN32_DESCRIPTOR WINSPOOL_Descriptor;
64 extern const BUILTIN32_DESCRIPTOR WNASPI32_Descriptor;
65 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor;
66 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
68 static BUILTIN32_DLL BuiltinDLLs[] =
70 { &ADVAPI32_Descriptor, TRUE },
71 { &AVIFIL32_Descriptor, FALSE },
72 { &COMCTL32_Descriptor, FALSE },
73 { &COMDLG32_Descriptor, TRUE },
74 { &CRTDLL_Descriptor, TRUE },
75 { &DCIMAN32_Descriptor, FALSE },
76 { &DDRAW_Descriptor, TRUE },
77 { &DINPUT_Descriptor, TRUE },
78 { &DPLAY_Descriptor, TRUE },
79 { &DPLAYX_Descriptor, TRUE },
80 { &DSOUND_Descriptor, TRUE },
81 { &GDI32_Descriptor, TRUE },
82 { &IMAGEHLP_Descriptor, FALSE },
83 { &IMM32_Descriptor, FALSE },
84 { &KERNEL32_Descriptor, TRUE },
85 { &LZ32_Descriptor, TRUE },
86 { &MPR_Descriptor, TRUE },
87 { &MSACM32_Descriptor, FALSE },
88 { &MSNET32_Descriptor, FALSE },
89 { &MSVFW32_Descriptor, FALSE },
90 { &NTDLL_Descriptor, TRUE },
91 { &OLE32_Descriptor, FALSE },
92 { &OLEAUT32_Descriptor, FALSE },
93 { &OLECLI32_Descriptor, FALSE },
94 { &OLEDLG_Descriptor, FALSE },
95 { &OLESVR32_Descriptor, FALSE },
96 { &PSAPI_Descriptor, FALSE },
97 { &RASAPI32_Descriptor, FALSE },
98 { &SHELL32_Descriptor, TRUE },
99 { &TAPI32_Descriptor, FALSE },
100 { &USER32_Descriptor, TRUE },
101 { &VERSION_Descriptor, TRUE },
102 { &W32SKRNL_Descriptor, TRUE },
103 { &WINMM_Descriptor, TRUE },
104 { &WINSPOOL_Descriptor, TRUE },
105 { &WNASPI32_Descriptor, TRUE },
106 { &WOW32_Descriptor, TRUE },
107 { &WSOCK32_Descriptor, TRUE },
108 /* Last entry */
109 { NULL, FALSE }
113 /***********************************************************************
114 * BUILTIN32_DoLoadImage
116 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadImage.
118 static HMODULE32 BUILTIN32_DoLoadImage( BUILTIN32_DLL *dll )
120 extern void RELAY_CallFrom32();
122 IMAGE_DATA_DIRECTORY *dir;
123 IMAGE_DOS_HEADER *dos;
124 IMAGE_NT_HEADERS *nt;
125 IMAGE_SECTION_HEADER *sec;
126 IMAGE_EXPORT_DIRECTORY *exp;
127 LPVOID *funcs;
128 LPSTR *names;
129 DEBUG_ENTRY_POINT *debug;
130 INT32 i, size;
131 BYTE *addr;
133 /* Allocate the module */
135 size = (sizeof(IMAGE_DOS_HEADER)
136 + sizeof(IMAGE_NT_HEADERS)
137 + 2 * sizeof(IMAGE_SECTION_HEADER)
138 + sizeof(IMAGE_EXPORT_DIRECTORY)
139 + dll->descr->nb_funcs * sizeof(LPVOID)
140 + dll->descr->nb_names * sizeof(LPSTR));
141 #ifdef __i386__
142 if (TRACE_ON(relay))
143 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
144 #endif
145 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
146 if (!addr) return 0;
147 dos = (IMAGE_DOS_HEADER *)addr;
148 nt = (IMAGE_NT_HEADERS *)(dos + 1);
149 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
150 exp = (IMAGE_EXPORT_DIRECTORY *)(sec + 2);
151 funcs = (LPVOID *)(exp + 1);
152 names = (LPSTR *)(funcs + dll->descr->nb_funcs);
153 debug = (DEBUG_ENTRY_POINT *)(names + dll->descr->nb_names);
155 /* Build the DOS and NT headers */
157 dos->e_magic = IMAGE_DOS_SIGNATURE;
158 dos->e_lfanew = sizeof(*dos);
160 nt->Signature = IMAGE_NT_SIGNATURE;
161 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
162 nt->FileHeader.NumberOfSections = 2; /* exports + code */
163 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
164 nt->FileHeader.Characteristics = IMAGE_FILE_DLL;
166 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
167 nt->OptionalHeader.SizeOfCode = 0x1000;
168 nt->OptionalHeader.SizeOfInitializedData = 0;
169 nt->OptionalHeader.SizeOfUninitializedData = 0;
170 nt->OptionalHeader.ImageBase = (DWORD)addr;
171 nt->OptionalHeader.SectionAlignment = 0x1000;
172 nt->OptionalHeader.FileAlignment = 0x1000;
173 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
174 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
175 nt->OptionalHeader.MajorSubsystemVersion = 4;
176 nt->OptionalHeader.MinorSubsystemVersion = 0;
177 nt->OptionalHeader.SizeOfImage = size;
178 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
179 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
180 if (dll->descr->dllentrypoint)
181 nt->OptionalHeader.AddressOfEntryPoint = (DWORD)dll->descr->dllentrypoint - (DWORD)addr;
183 /* Build the export directory */
185 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
186 dir->VirtualAddress = (BYTE *)exp - addr;
187 dir->Size = sizeof(*exp)
188 + dll->descr->nb_funcs * sizeof(LPVOID)
189 + dll->descr->nb_names * sizeof(LPSTR);
191 /* Build the exports section */
193 strcpy( sec->Name, ".edata" );
194 sec->Misc.VirtualSize = dir->Size;
195 sec->VirtualAddress = (BYTE *)exp - addr;
196 sec->SizeOfRawData = dir->Size;
197 sec->PointerToRawData = (BYTE *)exp - addr;
198 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
199 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
200 IMAGE_SCN_MEM_WRITE);
202 /* Build the code section */
204 sec++;
205 strcpy( sec->Name, ".code" );
206 sec->SizeOfRawData = 0;
207 #ifdef __i386__
208 if (TRACE_ON(relay))
209 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
210 #endif
211 sec->Misc.VirtualSize = sec->SizeOfRawData;
212 sec->VirtualAddress = (BYTE *)debug - addr;
213 sec->PointerToRawData = (BYTE *)debug - addr;
214 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
215 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
217 /* Build the exports section data */
219 exp->Name = ((BYTE *)dll->descr->name) - addr; /*??*/
220 exp->Base = dll->descr->base;
221 exp->NumberOfFunctions = dll->descr->nb_funcs;
222 exp->NumberOfNames = dll->descr->nb_names;
223 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
224 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
225 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
227 /* Build the funcs table */
229 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
231 BYTE args = dll->descr->args[i];
232 int j;
234 if (!dll->descr->functions[i]) continue;
235 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
236 #ifdef __i386__
237 if (!TRACE_ON(relay)) continue;
238 for (j=0;j<dll->descr->nb_names;j++)
239 if (dll->descr->ordinals[j] == i)
240 break;
241 if (j<dll->descr->nb_names) {
242 if (dll->descr->names[j]) {
243 char buffer[200];
244 sprintf(buffer,"%s.%d: %s",dll->descr->name,i,dll->descr->names[j]);
245 if (!RELAY_ShowDebugmsgRelay(buffer))
246 continue;
249 switch(args)
251 case 0xfe: /* register func */
252 debug->call = 0xe8;
253 debug->callfrom32 = (DWORD)dll->descr->functions[i] -
254 (DWORD)&debug->ret;
255 debug->ret = 0x90; /* nop */
256 debug->args = 0;
257 *funcs = (LPVOID)((BYTE *)debug - addr);
258 break;
259 case 0xff: /* stub or extern */
260 break;
261 default: /* normal function (stdcall or cdecl) */
262 debug->call = 0xe8;
263 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
264 (DWORD)&debug->ret;
265 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
266 debug->args = (args & 0x7f) * sizeof(int);
267 *funcs = (LPVOID)((BYTE *)debug - addr);
268 break;
270 #endif /* __i386__ */
273 /* Build the names table */
275 for (i = 0; i < exp->NumberOfNames; i++, names++)
276 if (dll->descr->names[i])
277 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
279 return (HMODULE32)addr;
282 /***********************************************************************
283 * BUILTIN32_LoadImage
285 * Load a built-in module. If the 'force' parameter is FALSE, we only
286 * load the module if it has not been disabled via the -dll option.
288 HMODULE32 BUILTIN32_LoadImage( LPCSTR name, OFSTRUCT *ofs, BOOL32 force )
290 BUILTIN32_DLL *table;
291 char dllname[16], *p;
293 /* Fix the name in case we have a full path and extension */
295 if ((p = strrchr( name, '\\' ))) name = p + 1;
296 lstrcpyn32A( dllname, name, sizeof(dllname) );
297 if ((p = strrchr( dllname, '.' ))) *p = '\0';
299 for (table = BuiltinDLLs; table->descr; table++)
300 if (!lstrcmpi32A( table->descr->name, dllname )) break;
301 if (!table->descr) return 0;
302 if (!table->used)
304 if (!force) return 0;
305 table->used = TRUE; /* So next time we use it at once */
308 sprintf( ofs->szPathName, "%s.DLL", table->descr->name );
309 return BUILTIN32_DoLoadImage( table );
313 /***********************************************************************
314 * BUILTIN32_GetEntryPoint
316 * Return the name of the DLL entry point corresponding
317 * to a relay entry point address. This is used only by relay debugging.
319 * This function _must_ return the real entry point to call
320 * after the debug info is printed.
322 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
323 unsigned int *typemask )
325 BUILTIN32_DLL *dll;
326 HMODULE32 hModule;
327 int ordinal = 0, i;
329 /* First find the module */
331 for (dll = BuiltinDLLs; dll->descr; dll++)
332 if (dll->used
333 && ((hModule = GetModuleHandle32A(dll->descr->name)) != 0))
335 IMAGE_SECTION_HEADER *sec = PE_SECTIONS(hModule);
336 DEBUG_ENTRY_POINT *debug =
337 (DEBUG_ENTRY_POINT *)((DWORD)hModule + sec[1].VirtualAddress);
338 DEBUG_ENTRY_POINT *func = (DEBUG_ENTRY_POINT *)relay;
340 if (debug <= func && func < debug + dll->descr->nb_funcs)
342 ordinal = func - debug;
343 break;
347 if (!dll->descr)
348 return (ENTRYPOINT32)NULL;
350 /* Now find the function */
352 for (i = 0; i < dll->descr->nb_names; i++)
353 if (dll->descr->ordinals[i] == ordinal) break;
354 assert( i < dll->descr->nb_names );
356 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
357 dll->descr->names[i] );
358 *typemask = dll->descr->argtypes[ordinal];
359 return dll->descr->functions[ordinal];
363 /***********************************************************************
364 * BUILTIN32_Unimplemented
366 * This function is called for unimplemented 32-bit entry points (declared
367 * as 'stub' in the spec file).
369 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
371 const char *func_name = "???";
372 int i;
374 __RESTORE_ES; /* Just in case */
376 for (i = 0; i < descr->nb_names; i++)
377 if (descr->ordinals[i] + descr->base == ordinal) break;
378 if (i < descr->nb_names) func_name = descr->names[i];
380 MSG( "No handler for Win32 routine %s.%d: %s",
381 descr->name, ordinal, func_name );
382 #ifdef __GNUC__
383 MSG( " (called from %p)", __builtin_return_address(1) );
384 #endif
385 MSG( "\n" );
386 ExitProcess(1);
390 /***********************************************************************
391 * BUILTIN32_EnableDLL
393 * Enable or disable a built-in DLL.
395 int BUILTIN32_EnableDLL( const char *name, int len, int enable )
397 int i;
398 BUILTIN32_DLL *dll;
400 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
402 if (!lstrncmpi32A( name, dll->descr->name, len ))
404 dll->used = enable;
405 return TRUE;
408 return FALSE;
412 /***********************************************************************
413 * BUILTIN32_PrintDLLs
415 * Print the list of built-in DLLs that can be disabled.
417 void BUILTIN32_PrintDLLs(void)
419 int i;
420 BUILTIN32_DLL *dll;
422 MSG("Available Win32 DLLs:\n");
423 for (i = 0, dll = BuiltinDLLs; dll->descr; dll++)
424 MSG("%-9s%c", dll->descr->name,
425 ((++i) % 8) ? ' ' : '\n' );
426 MSG("\n");