Release 971221
[wine/multimedia.git] / relay32 / builtin32.c
blob8bdf3eb73b2112d5baf4c6fbf273cddbd0443fe3
1 /*
2 * Win32 builtin functions
4 * Copyright 1997 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include "builtin32.h"
11 #include "module.h"
12 #include "task.h"
13 #include "process.h"
14 #include "stddebug.h"
15 #include "debug.h"
17 typedef struct
19 BYTE call; /* 0xe8 call callfrom32 (relative) */
20 DWORD callfrom32 WINE_PACKED; /* RELAY_CallFrom32 relative addr */
21 BYTE ret; /* 0xc2 ret $n or 0xc3 ret */
22 WORD args; /* nb of args to remove from the stack */
23 } DEBUG_ENTRY_POINT;
25 typedef struct
27 BYTE pushl; /* 0x68 pushl $func_to_call */
28 DWORD func WINE_PACKED; /* func to call */
29 BYTE jmp; /* 0xe9 jmp CALL32_Regs (relative) */
30 DWORD call32_regs WINE_PACKED; /* CALL32_Regs relative addr */
31 WORD nop; /* 0x9090 nop;nop */
32 } REG_ENTRY_POINT;
34 typedef struct
36 const BUILTIN32_DESCRIPTOR *descr; /* DLL descriptor */
37 DEBUG_ENTRY_POINT *dbg_funcs; /* Relay debugging functions table*/
38 BOOL32 used; /* Used by default */
39 } BUILTIN32_DLL;
42 extern const BUILTIN32_DESCRIPTOR ADVAPI32_Descriptor;
43 extern const BUILTIN32_DESCRIPTOR COMCTL32_Descriptor;
44 extern const BUILTIN32_DESCRIPTOR COMDLG32_Descriptor;
45 extern const BUILTIN32_DESCRIPTOR CRTDLL_Descriptor;
46 extern const BUILTIN32_DESCRIPTOR DCIMAN32_Descriptor;
47 extern const BUILTIN32_DESCRIPTOR GDI32_Descriptor;
48 extern const BUILTIN32_DESCRIPTOR KERNEL32_Descriptor;
49 extern const BUILTIN32_DESCRIPTOR LZ32_Descriptor;
50 extern const BUILTIN32_DESCRIPTOR MPR_Descriptor;
51 extern const BUILTIN32_DESCRIPTOR MSVFW32_Descriptor;
52 extern const BUILTIN32_DESCRIPTOR NTDLL_Descriptor;
53 extern const BUILTIN32_DESCRIPTOR OLE32_Descriptor;
54 extern const BUILTIN32_DESCRIPTOR OLECLI32_Descriptor;
55 extern const BUILTIN32_DESCRIPTOR OLESVR32_Descriptor;
56 extern const BUILTIN32_DESCRIPTOR SHELL32_Descriptor;
57 extern const BUILTIN32_DESCRIPTOR TAPI32_Descriptor;
58 extern const BUILTIN32_DESCRIPTOR USER32_Descriptor;
59 extern const BUILTIN32_DESCRIPTOR VERSION_Descriptor;
60 extern const BUILTIN32_DESCRIPTOR W32SKRNL_Descriptor;
61 extern const BUILTIN32_DESCRIPTOR WINMM_Descriptor;
62 extern const BUILTIN32_DESCRIPTOR WINSPOOL_Descriptor;
63 extern const BUILTIN32_DESCRIPTOR WOW32_Descriptor;
64 extern const BUILTIN32_DESCRIPTOR WSOCK32_Descriptor;
66 static BUILTIN32_DLL BuiltinDLLs[] =
68 { &ADVAPI32_Descriptor, NULL, TRUE },
69 { &COMCTL32_Descriptor, NULL, FALSE },
70 { &COMDLG32_Descriptor, NULL, TRUE },
71 { &CRTDLL_Descriptor, NULL, TRUE },
72 { &DCIMAN32_Descriptor, NULL, TRUE },
73 { &GDI32_Descriptor, NULL, TRUE },
74 { &KERNEL32_Descriptor, NULL, TRUE },
75 { &LZ32_Descriptor, NULL, TRUE },
76 { &MPR_Descriptor, NULL, TRUE },
77 { &MSVFW32_Descriptor, NULL, TRUE },
78 { &NTDLL_Descriptor, NULL, TRUE },
79 { &OLE32_Descriptor, NULL, FALSE },
80 { &OLECLI32_Descriptor, NULL, FALSE },
81 { &OLESVR32_Descriptor, NULL, FALSE },
82 { &SHELL32_Descriptor, NULL, TRUE },
83 { &TAPI32_Descriptor, NULL, TRUE },
84 { &USER32_Descriptor, NULL, TRUE },
85 { &VERSION_Descriptor, NULL, TRUE },
86 { &W32SKRNL_Descriptor, NULL, TRUE },
87 { &WINMM_Descriptor, NULL, TRUE },
88 { &WINSPOOL_Descriptor, NULL, TRUE },
89 { &WOW32_Descriptor, NULL, TRUE },
90 { &WSOCK32_Descriptor, NULL, TRUE },
91 /* Last entry */
92 { NULL, NULL, FALSE }
96 /***********************************************************************
97 * BUILTIN32_DoLoadModule
99 * Load a built-in Win32 module. Helper function for BUILTIN32_LoadModule.
101 static HMODULE32 BUILTIN32_DoLoadModule( BUILTIN32_DLL *dll )
103 extern void RELAY_CallFrom32();
104 extern void CALL32_Regs();
106 HMODULE16 hModule;
107 NE_MODULE *pModule;
108 OFSTRUCT ofs;
109 IMAGE_DATA_DIRECTORY *dir;
110 IMAGE_DOS_HEADER *dos;
111 IMAGE_NT_HEADERS *nt;
112 IMAGE_SECTION_HEADER *sec;
113 IMAGE_EXPORT_DIRECTORY *exp;
114 LPVOID *funcs;
115 LPSTR *names;
116 DEBUG_ENTRY_POINT *debug;
117 REG_ENTRY_POINT *regs;
118 PE_MODREF *pem;
119 INT32 i, size;
120 BYTE *addr;
122 /* Allocate the module */
124 size = (sizeof(IMAGE_DOS_HEADER)
125 + sizeof(IMAGE_NT_HEADERS)
126 + 2 * sizeof(IMAGE_SECTION_HEADER)
127 + sizeof(IMAGE_EXPORT_DIRECTORY)
128 + dll->descr->nb_funcs * sizeof(LPVOID)
129 + dll->descr->nb_names * sizeof(LPSTR)
130 + dll->descr->nb_reg_funcs * sizeof(REG_ENTRY_POINT));
131 #ifdef __i386__
132 if (debugging_relay)
133 size += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
134 #endif
135 addr = VirtualAlloc( NULL, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
136 if (!addr) return 0;
137 dos = (IMAGE_DOS_HEADER *)addr;
138 nt = (IMAGE_NT_HEADERS *)(dos + 1);
139 sec = (IMAGE_SECTION_HEADER *)(nt + 1);
140 exp = (IMAGE_EXPORT_DIRECTORY *)(sec + 2);
141 funcs = (LPVOID *)(exp + 1);
142 names = (LPSTR *)(funcs + dll->descr->nb_funcs);
143 regs = (REG_ENTRY_POINT *)(names + dll->descr->nb_names);
144 debug = (DEBUG_ENTRY_POINT *)(regs + dll->descr->nb_reg_funcs);
146 /* Build the DOS and NT headers */
148 dos->e_magic = IMAGE_DOS_SIGNATURE;
149 dos->e_lfanew = sizeof(*dos);
151 nt->Signature = IMAGE_NT_SIGNATURE;
152 nt->FileHeader.Machine = IMAGE_FILE_MACHINE_I386;
153 nt->FileHeader.NumberOfSections = 2; /* exports + code */
154 nt->FileHeader.SizeOfOptionalHeader = sizeof(nt->OptionalHeader);
155 nt->FileHeader.Characteristics = IMAGE_FILE_DLL;
157 nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR_MAGIC;
158 nt->OptionalHeader.SizeOfCode = 0x1000;
159 nt->OptionalHeader.SizeOfInitializedData = 0;
160 nt->OptionalHeader.SizeOfUninitializedData = 0;
161 nt->OptionalHeader.ImageBase = (DWORD)addr;
162 nt->OptionalHeader.SectionAlignment = 0x1000;
163 nt->OptionalHeader.FileAlignment = 0x1000;
164 nt->OptionalHeader.MajorOperatingSystemVersion = 1;
165 nt->OptionalHeader.MinorOperatingSystemVersion = 0;
166 nt->OptionalHeader.MajorSubsystemVersion = 4;
167 nt->OptionalHeader.MinorSubsystemVersion = 0;
168 nt->OptionalHeader.SizeOfImage = size;
169 nt->OptionalHeader.SizeOfHeaders = (BYTE *)exp - addr;
170 nt->OptionalHeader.NumberOfRvaAndSizes = IMAGE_NUMBEROF_DIRECTORY_ENTRIES;
172 /* Build the export directory */
174 dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
175 dir->VirtualAddress = (BYTE *)exp - addr;
176 dir->Size = sizeof(*exp)
177 + dll->descr->nb_funcs * sizeof(LPVOID)
178 + dll->descr->nb_names * sizeof(LPSTR);
180 /* Build the exports section */
182 strcpy( sec->Name, ".edata" );
183 sec->Misc.VirtualSize = dir->Size;
184 sec->VirtualAddress = (BYTE *)exp - addr;
185 sec->SizeOfRawData = dir->Size;
186 sec->PointerToRawData = (BYTE *)exp - addr;
187 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
188 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ |
189 IMAGE_SCN_MEM_WRITE);
191 /* Build the code section */
193 sec++;
194 strcpy( sec->Name, ".code" );
195 sec->SizeOfRawData = dll->descr->nb_reg_funcs * sizeof(REG_ENTRY_POINT);
196 #ifdef __i386__
197 if (debugging_relay)
198 sec->SizeOfRawData += dll->descr->nb_funcs * sizeof(DEBUG_ENTRY_POINT);
199 #endif
200 sec->Misc.VirtualSize = sec->SizeOfRawData;
201 sec->VirtualAddress = (BYTE *)regs - addr;
202 sec->PointerToRawData = (BYTE *)regs - addr;
203 sec->Characteristics = (IMAGE_SCN_CNT_INITIALIZED_DATA |
204 IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
206 /* Build the exports section data */
208 exp->Name = (BYTE *)dll->descr->name - addr; /*??*/
209 exp->Base = dll->descr->base;
210 exp->NumberOfFunctions = dll->descr->nb_funcs;
211 exp->NumberOfNames = dll->descr->nb_names;
212 exp->AddressOfFunctions = (LPDWORD *)((BYTE *)funcs - addr);
213 exp->AddressOfNames = (LPDWORD *)((BYTE *)names - addr);
214 exp->AddressOfNameOrdinals = (LPWORD *)((BYTE *)dll->descr->ordinals - addr);
216 /* Build the funcs table */
218 if (debugging_relay) dll->dbg_funcs = debug;
219 for (i = 0; i < dll->descr->nb_funcs; i++, funcs++, debug++)
221 BYTE args = dll->descr->args[i];
222 if (!dll->descr->functions[i]) continue;
223 #ifdef __i386__
224 switch(args)
226 case 0xfe: /* register func */
227 regs->pushl = 0x68;
228 regs->func = (DWORD)dll->descr->functions[i];
229 regs->jmp = 0xe9;
230 regs->call32_regs = (DWORD)CALL32_Regs - (DWORD)&regs->nop;
231 regs->nop = 0x9090;
232 if (debugging_relay)
234 debug->call = 0xe8;
235 debug->callfrom32 = (DWORD)regs - (DWORD)&debug->ret;
236 debug->ret = 0x90; /* nop */
237 debug->args = 0;
238 *funcs = (LPVOID)((BYTE *)debug - addr);
240 else *funcs = (LPVOID)((BYTE *)regs - addr);
241 regs++;
242 break;
243 case 0xff: /* stub or extern */
244 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
245 break;
246 default: /* normal function (stdcall or cdecl) */
247 if (debugging_relay)
249 debug->call = 0xe8;
250 debug->callfrom32 = (DWORD)RELAY_CallFrom32 -
251 (DWORD)&debug->ret;
252 debug->ret = (args & 0x80) ? 0xc3 : 0xc2; /*ret/ret $n*/
253 debug->args = (args & 0x7f) * sizeof(int);
254 *funcs = (LPVOID)((BYTE *)debug - addr);
256 else
257 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
258 break;
260 #else /* __i386__ */
261 *funcs = (LPVOID)((BYTE *)dll->descr->functions[i] - addr);
262 #endif /* __i386__ */
265 /* Build the names table */
267 for (i = 0; i < exp->NumberOfNames; i++, names++)
268 if (dll->descr->names[i])
269 *names = (LPSTR)((BYTE *)dll->descr->names[i] - addr);
271 /* Create a modref */
273 pem = (PE_MODREF *)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
274 sizeof(*pem) );
275 pem->module = (HMODULE32)addr;
276 pem->pe_export = exp;
277 pem->next = pCurrentProcess->modref_list;
278 pCurrentProcess->modref_list = pem;
280 /* Create a Win16 dummy module */
282 sprintf( ofs.szPathName, "%s.DLL", dll->descr->name );
283 hModule = MODULE_CreateDummyModule( &ofs );
284 pModule = (NE_MODULE *)GlobalLock16( hModule );
285 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
286 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
287 pModule->module32 = (HMODULE32)addr;
288 return pModule->module32;
292 /***********************************************************************
293 * BUILTIN32_LoadModule
295 * Load a built-in module. If the 'force' parameter is FALSE, we only
296 * load the module if it has not been disabled via the -dll option.
298 HMODULE32 BUILTIN32_LoadModule( LPCSTR name, BOOL32 force )
300 BUILTIN32_DLL *table;
301 char dllname[16], *p;
303 /* Fix the name in case we have a full path and extension */
305 if ((p = strrchr( name, '\\' ))) name = p + 1;
306 lstrcpyn32A( dllname, name, sizeof(dllname) );
307 if ((p = strrchr( dllname, '.' ))) *p = '\0';
309 for (table = BuiltinDLLs; table->descr; table++)
310 if (!lstrcmpi32A( table->descr->name, dllname )) break;
311 if (!table->descr) return 0;
312 if (!table->used && !force) return 0;
314 return BUILTIN32_DoLoadModule( table );
318 /***********************************************************************
319 * BUILTIN32_GetEntryPoint
321 * Return the name of the DLL entry point corresponding
322 * to a relay entry point address. This is used only by relay debugging.
324 * This function _must_ return the real entry point to call
325 * after the debug info is printed.
327 ENTRYPOINT32 BUILTIN32_GetEntryPoint( char *buffer, void *relay,
328 unsigned int *typemask )
330 BUILTIN32_DLL *dll;
331 int ordinal, i;
333 /* First find the module */
335 for (dll = BuiltinDLLs; dll->descr; dll++)
336 if (((void *)dll->dbg_funcs <= relay) &&
337 ((void *)(dll->dbg_funcs + dll->descr->nb_funcs) > relay))
338 break;
339 assert(dll->descr);
341 /* Now find the function */
343 ordinal = ((DWORD)relay-(DWORD)dll->dbg_funcs) / sizeof(DEBUG_ENTRY_POINT);
344 for (i = 0; i < dll->descr->nb_names; i++)
345 if (dll->descr->ordinals[i] == ordinal) break;
346 assert( i < dll->descr->nb_names );
348 sprintf( buffer, "%s.%d: %s", dll->descr->name, ordinal + dll->descr->base,
349 dll->descr->names[i] );
350 *typemask = dll->descr->argtypes[ordinal];
351 return dll->descr->functions[ordinal];
355 /***********************************************************************
356 * BUILTIN32_Unimplemented
358 * This function is called for unimplemented 32-bit entry points (declared
359 * as 'stub' in the spec file).
361 void BUILTIN32_Unimplemented( const BUILTIN32_DESCRIPTOR *descr, int ordinal )
363 const char *func_name = "???";
364 int i;
366 __RESTORE_ES; /* Just in case */
368 for (i = 0; i < descr->nb_names; i++)
369 if (descr->ordinals[i] + descr->base == ordinal) break;
370 if (i < descr->nb_names) func_name = descr->names[i];
372 fprintf( stderr, "No handler for Win32 routine %s.%d: %s",
373 descr->name, ordinal, func_name );
374 #ifdef __GNUC__
375 fprintf( stderr, " (called from %p)", __builtin_return_address(1) );
376 #endif
377 fprintf( stderr, "\n" );
378 TASK_KillCurrentTask(1);