4 * Copyright 1996 Alexandre Julliard
12 #include "wine/winbase16.h"
13 #include "wine/winestring.h"
14 #include "builtin16.h"
15 #include "builtin32.h"
20 #include "stackframe.h"
23 #include "debugtools.h"
26 DEFAULT_DEBUG_CHANNEL(module);
30 LPVOID res_start; /* address of resource data */
32 DWORD res_size; /* size of resource data */
36 /* Table of all built-in DLLs */
40 static const BUILTIN16_DESCRIPTOR *builtin_dlls[MAX_DLLS];
44 /***********************************************************************
45 * BUILTIN_DoLoadModule16
47 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule.
49 static HMODULE16 BUILTIN_DoLoadModule16( const BUILTIN16_DESCRIPTOR *descr )
53 SEGTABLEENTRY *pSegTable;
55 const BUILTIN16_RESOURCE *rsrc = descr->rsrc;
59 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
60 descr->module_size, 0,
61 FALSE, FALSE, FALSE, NULL );
62 if (!hModule) return 0;
63 FarSetOwner16( hModule, hModule );
65 pModule = (NE_MODULE *)GlobalLock16( hModule );
71 hModule = GLOBAL_Alloc( GMEM_MOVEABLE,
72 descr->module_size + rsrc->res_size,
73 0, FALSE, FALSE, FALSE );
74 if (!hModule) return 0;
75 FarSetOwner16( hModule, hModule );
77 pModule = (NE_MODULE *)GlobalLock16( hModule );
78 res_off = ((NE_MODULE *)descr->module_start)->res_table;
80 memcpy( (LPBYTE)pModule, descr->module_start, res_off );
81 memcpy( (LPBYTE)pModule + res_off, rsrc->res_start, rsrc->res_size );
82 memcpy( (LPBYTE)pModule + res_off + rsrc->res_size,
83 (LPBYTE)descr->module_start + res_off, descr->module_size - res_off );
85 /* Have to fix up various pModule-based near pointers. Ugh! */
86 pModule->name_table += rsrc->res_size;
87 pModule->modref_table += rsrc->res_size;
88 pModule->import_table += rsrc->res_size;
89 pModule->entry_table += rsrc->res_size;
91 for ( bundle = (ET_BUNDLE *)((LPBYTE)pModule + pModule->entry_table);
93 bundle = (ET_BUNDLE *)((LPBYTE)pModule + bundle->next) )
94 bundle->next += rsrc->res_size;
96 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
97 pModule->hRsrcMap = rsrc->res_start;
99 pModule->self = hModule;
101 TRACE( "Built-in %s: hmodule=%04x\n", descr->name, hModule );
103 /* Allocate the code segment */
105 pSegTable = NE_SEG_TABLE( pModule );
106 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
107 pSegTable->minsize, hModule,
108 TRUE, TRUE, FALSE, NULL );
109 if (!pSegTable->hSeg) return 0;
112 /* Allocate the data segment */
114 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
115 minsize += pModule->heap_size;
116 if (minsize > 0x10000) minsize = 0x10000;
117 pSegTable->hSeg = GLOBAL_Alloc( GMEM_FIXED, minsize,
118 hModule, FALSE, FALSE, FALSE );
119 if (!pSegTable->hSeg) return 0;
120 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
121 descr->data_start, pSegTable->minsize);
122 if (pModule->heap_size)
123 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
124 pSegTable->minsize, minsize );
127 NE_InitResourceHandler(hModule);
129 NE_RegisterModule( pModule );
134 /***********************************************************************
137 * Load a built-in module.
139 HMODULE16 BUILTIN_LoadModule( LPCSTR name )
141 char dllname[16], *p;
145 /* Fix the name in case we have a full path and extension */
147 if ((p = strrchr( name, '\\' ))) name = p + 1;
148 lstrcpynA( dllname, name, sizeof(dllname) );
149 p = strrchr( dllname, '.' );
151 if (!p) strcat( dllname, ".dll" );
153 for (i = 0; i < nb_dlls; i++)
155 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
156 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
157 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
158 if (!strcasecmp( pOfs->szPathName, dllname ))
159 return BUILTIN_DoLoadModule16( descr );
162 if ((handle = BUILTIN32_dlopen( dllname )))
164 for (i = 0; i < nb_dlls; i++)
166 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
167 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
168 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
169 if (!strcasecmp( pOfs->szPathName, dllname ))
170 return BUILTIN_DoLoadModule16( descr );
172 ERR( "loaded .so but dll %s still not found\n", dllname );
173 BUILTIN32_dlclose( handle );
180 /***********************************************************************
181 * BUILTIN_GetEntryPoint16
183 * Return the ordinal, name, and type info corresponding to a CS:IP address.
184 * This is used only by relay debugging.
186 LPCSTR BUILTIN_GetEntryPoint16( STACK16FRAME *frame, LPSTR name, WORD *pOrd )
194 if (!(pModule = NE_GetPtr( FarGetOwner16( GlobalHandle16( frame->module_cs ) ))))
199 bundle = (ET_BUNDLE *)((BYTE *)pModule + pModule->entry_table);
202 entry = (ET_ENTRY *)((BYTE *)bundle+6);
203 for (i = bundle->first + 1; i <= bundle->last; i++)
205 if ((entry->offs < frame->entry_ip)
206 && (entry->segnum == 1) /* code segment ? */
207 && (entry->offs >= max_offset))
209 max_offset = entry->offs;
214 } while ( (bundle->next)
215 && (bundle = (ET_BUNDLE *)((BYTE *)pModule+bundle->next)));
217 /* Search for the name in the resident names table */
218 /* (built-in modules have no non-resident table) */
220 p = (BYTE *)pModule + pModule->name_table;
223 p += *p + 1 + sizeof(WORD);
224 if (*(WORD *)(p + *p + 1) == *pOrd) break;
227 sprintf( name, "%.*s.%d: %.*s",
228 *((BYTE *)pModule + pModule->name_table),
229 (char *)pModule + pModule->name_table + 1,
230 *pOrd, *p, (char *)(p + 1) );
232 /* Retrieve type info string */
233 return *(LPCSTR *)((LPBYTE)PTR_SEG_OFF_TO_LIN( frame->module_cs, frame->callfrom_ip ) + 4);
237 /***********************************************************************
238 * BUILTIN_RegisterDLL
240 * Register a built-in DLL descriptor.
242 void BUILTIN_RegisterDLL( const BUILTIN16_DESCRIPTOR *descr )
244 assert( nb_dlls < MAX_DLLS );
245 builtin_dlls[nb_dlls++] = descr;