Yet another attempt at fixing CW_USEDEFAULT handling.
[wine.git] / if1632 / builtin.c
blob2f1c64b324da6de47afe005df233d5f5660e8434
1 /*
2 * Built-in modules
4 * Copyright 1996 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <ctype.h>
9 #include <string.h>
10 #include <stdio.h>
11 #include "winbase.h"
12 #include "wine/winbase16.h"
13 #include "wine/winestring.h"
14 #include "builtin16.h"
15 #include "builtin32.h"
16 #include "global.h"
17 #include "heap.h"
18 #include "module.h"
19 #include "miscemu.h"
20 #include "stackframe.h"
21 #include "user.h"
22 #include "process.h"
23 #include "task.h"
24 #include "debugtools.h"
25 #include "toolhelp.h"
27 DEFAULT_DEBUG_CHANNEL(module);
29 typedef struct
31 LPVOID res_start; /* address of resource data */
32 DWORD nr_res;
33 DWORD res_size; /* size of resource data */
34 } BUILTIN16_RESOURCE;
37 /* Table of all built-in DLLs */
39 #define MAX_DLLS 50
41 static const BUILTIN16_DESCRIPTOR *builtin_dlls[MAX_DLLS];
42 static int nb_dlls;
45 /***********************************************************************
46 * BUILTIN_DoLoadModule16
48 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule.
50 static HMODULE16 BUILTIN_DoLoadModule16( const BUILTIN16_DESCRIPTOR *descr )
52 NE_MODULE *pModule;
53 int minsize, res_off;
54 SEGTABLEENTRY *pSegTable;
55 HMODULE16 hModule;
56 const BUILTIN16_RESOURCE *rsrc = descr->rsrc;
58 if (!rsrc)
60 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
61 descr->module_size, 0,
62 FALSE, FALSE, FALSE, NULL );
63 if (!hModule) return 0;
64 FarSetOwner16( hModule, hModule );
66 pModule = (NE_MODULE *)GlobalLock16( hModule );
68 else
70 ET_BUNDLE *bundle;
72 hModule = GLOBAL_Alloc( GMEM_MOVEABLE,
73 descr->module_size + rsrc->res_size,
74 0, FALSE, FALSE, FALSE );
75 if (!hModule) return 0;
76 FarSetOwner16( hModule, hModule );
78 pModule = (NE_MODULE *)GlobalLock16( hModule );
79 res_off = ((NE_MODULE *)descr->module_start)->res_table;
81 memcpy( (LPBYTE)pModule, descr->module_start, res_off );
82 memcpy( (LPBYTE)pModule + res_off, rsrc->res_start, rsrc->res_size );
83 memcpy( (LPBYTE)pModule + res_off + rsrc->res_size,
84 (LPBYTE)descr->module_start + res_off, descr->module_size - res_off );
86 /* Have to fix up various pModule-based near pointers. Ugh! */
87 pModule->name_table += rsrc->res_size;
88 pModule->modref_table += rsrc->res_size;
89 pModule->import_table += rsrc->res_size;
90 pModule->entry_table += rsrc->res_size;
92 for ( bundle = (ET_BUNDLE *)((LPBYTE)pModule + pModule->entry_table);
93 bundle->next;
94 bundle = (ET_BUNDLE *)((LPBYTE)pModule + bundle->next) )
95 bundle->next += rsrc->res_size;
97 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
98 pModule->hRsrcMap = rsrc->res_start;
100 pModule->self = hModule;
102 TRACE( "Built-in %s: hmodule=%04x\n", descr->name, hModule );
104 /* Allocate the code segment */
106 pSegTable = NE_SEG_TABLE( pModule );
107 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
108 pSegTable->minsize, hModule,
109 TRUE, TRUE, FALSE, NULL );
110 if (!pSegTable->hSeg) return 0;
111 pSegTable++;
113 /* Allocate the data segment */
115 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
116 minsize += pModule->heap_size;
117 if (minsize > 0x10000) minsize = 0x10000;
118 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
119 hModule, FALSE, FALSE, FALSE );
120 if (!pSegTable->hSeg) return 0;
121 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
122 descr->data_start, pSegTable->minsize);
123 if (pModule->heap_size)
124 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
125 pSegTable->minsize, minsize );
127 if (rsrc)
128 NE_InitResourceHandler(hModule);
130 NE_RegisterModule( pModule );
131 return hModule;
135 /***********************************************************************
136 * BUILTIN_LoadModule
138 * Load a built-in module.
140 HMODULE16 BUILTIN_LoadModule( LPCSTR name )
142 char dllname[16], *p;
143 void *handle;
144 int i;
146 /* Fix the name in case we have a full path and extension */
148 if ((p = strrchr( name, '\\' ))) name = p + 1;
149 lstrcpynA( dllname, name, sizeof(dllname) );
150 p = strrchr( dllname, '.' );
152 if (!p) strcat( dllname, ".dll" );
154 for (i = 0; i < nb_dlls; i++)
156 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
157 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
158 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
159 if (!strcasecmp( pOfs->szPathName, dllname ))
160 return BUILTIN_DoLoadModule16( descr );
163 if ((handle = BUILTIN32_dlopen( dllname )))
165 for (i = 0; i < nb_dlls; i++)
167 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
168 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
169 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
170 if (!strcasecmp( pOfs->szPathName, dllname ))
171 return BUILTIN_DoLoadModule16( descr );
173 ERR( "loaded .so but dll %s still not found\n", dllname );
174 BUILTIN32_dlclose( handle );
177 return (HMODULE16)2;
181 /***********************************************************************
182 * BUILTIN_GetEntryPoint16
184 * Return the ordinal, name, and type info corresponding to a CS:IP address.
185 * This is used only by relay debugging.
187 LPCSTR BUILTIN_GetEntryPoint16( STACK16FRAME *frame, LPSTR name, WORD *pOrd )
189 WORD i, max_offset;
190 register BYTE *p;
191 NE_MODULE *pModule;
192 ET_BUNDLE *bundle;
193 ET_ENTRY *entry;
195 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
196 return NULL;
198 max_offset = 0;
199 *pOrd = 0;
200 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
203 entry = (ET_ENTRY *)((BYTE *)bundle+6);
204 for (i = bundle->first + 1; i <= bundle->last; i++)
206 if ((entry->offs < frame->entry_ip)
207 && (entry->segnum == 1) /* code segment ? */
208 && (entry->offs >= max_offset))
210 max_offset = entry->offs;
211 *pOrd = i;
213 entry++;
215 } while ( (bundle->next)
216 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
218 /* Search for the name in the resident names table */
219 /* (built-in modules have no non-resident table) */
221 p = (BYTE *)pModule + pModule->name_table;
222 while (*p)
224 p += *p + 1 + sizeof(WORD);
225 if (*(WORD *)(p + *p + 1) == *pOrd) break;
228 sprintf( name, "%.*s.%d: %.*s",
229 *((BYTE *)pModule + pModule->name_table),
230 (char *)pModule + pModule->name_table + 1,
231 *pOrd, *p, (char *)(p + 1) );
233 /* Retrieve type info string */
234 return *(LPCSTR *)((LPBYTE)PTR_SEG_OFF_TO_LIN( frame->module_cs, frame->callfrom_ip ) + 4);
238 /***********************************************************************
239 * BUILTIN_RegisterDLL
241 * Register a built-in DLL descriptor.
243 void BUILTIN_RegisterDLL( const BUILTIN16_DESCRIPTOR *descr )
245 assert( nb_dlls < MAX_DLLS );
246 builtin_dlls[nb_dlls++] = descr;