4 * Copyright 1996 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "wine/winbase16.h"
27 #include "builtin16.h"
32 #include "stackframe.h"
33 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(module);
40 void *module_start; /* 32-bit address of the module data */
41 int module_size; /* Size of the module data */
42 void *code_start; /* 32-bit address of DLL code */
43 void *data_start; /* 32-bit address of DLL data */
44 const char *owner; /* 32-bit dll that contains this dll */
45 const void *rsrc; /* resources data */
46 } BUILTIN16_DESCRIPTOR;
48 /* Table of all built-in DLLs */
52 static const BUILTIN16_DESCRIPTOR *builtin_dlls[MAX_DLLS];
56 /* patch all the flat cs references of the code segment if necessary */
57 inline static void patch_code_segment( void *code_segment )
60 CALLFROM16 *call = code_segment;
61 if (call->flatcs == __get_cs()) return; /* nothing to patch */
62 while (call->pushl == 0x68)
64 call->flatcs = __get_cs();
71 /***********************************************************************
72 * BUILTIN_DoLoadModule16
74 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule.
76 static HMODULE16 BUILTIN_DoLoadModule16( const BUILTIN16_DESCRIPTOR *descr )
80 SEGTABLEENTRY *pSegTable;
83 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
84 descr->module_size, 0, WINE_LDT_FLAGS_DATA );
85 if (!hModule) return 0;
86 FarSetOwner16( hModule, hModule );
88 pModule = (NE_MODULE *)GlobalLock16( hModule );
89 pModule->self = hModule;
90 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
91 pModule->hRsrcMap = (void *)descr->rsrc;
93 /* Allocate the code segment */
95 pSegTable = NE_SEG_TABLE( pModule );
96 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
97 pSegTable->minsize, hModule,
98 WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT );
99 if (!pSegTable->hSeg) return 0;
100 patch_code_segment( descr->code_start );
103 /* Allocate the data segment */
105 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
106 minsize += pModule->heap_size;
107 if (minsize > 0x10000) minsize = 0x10000;
108 pSegTable->hSeg = GlobalAlloc16( GMEM_FIXED, minsize );
109 if (!pSegTable->hSeg) return 0;
110 FarSetOwner16( pSegTable->hSeg, hModule );
111 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
112 descr->data_start, pSegTable->minsize);
113 if (pModule->heap_size)
114 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
115 pSegTable->minsize, minsize );
117 if (descr->rsrc) NE_InitResourceHandler(hModule);
119 NE_RegisterModule( pModule );
121 /* make sure the 32-bit library containing this one is loaded too */
122 LoadLibraryA( descr->owner );
128 /***********************************************************************
131 * Find a descriptor in the list
133 static const BUILTIN16_DESCRIPTOR *find_dll_descr( const char *dllname )
136 for (i = 0; i < nb_dlls; i++)
138 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
139 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
140 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
141 BYTE *name_table = (BYTE *)pModule + pModule->name_table;
143 /* check the dll file name */
144 if (!FILE_strcasecmp( pOfs->szPathName, dllname ))
146 /* check the dll module name (without extension) */
147 if (!FILE_strncasecmp( dllname, name_table+1, *name_table ) &&
148 !strcmp( dllname + *name_table, ".dll" ))
155 /***********************************************************************
158 * Load a built-in module.
160 HMODULE16 BUILTIN_LoadModule( LPCSTR name )
162 const BUILTIN16_DESCRIPTOR *descr;
163 char dllname[20], *p;
166 /* Fix the name in case we have a full path and extension */
168 if ((p = strrchr( name, '\\' ))) name = p + 1;
169 if ((p = strrchr( name, '/' ))) name = p + 1;
171 if (strlen(name) >= sizeof(dllname)-4) return (HMODULE16)2;
173 strcpy( dllname, name );
174 p = strrchr( dllname, '.' );
175 if (!p) strcat( dllname, ".dll" );
176 for (p = dllname; *p; p++) *p = FILE_tolower(*p);
178 if ((descr = find_dll_descr( dllname )))
179 return BUILTIN_DoLoadModule16( descr );
181 if ((handle = BUILTIN32_dlopen( dllname )))
183 if ((descr = find_dll_descr( dllname )))
184 return BUILTIN_DoLoadModule16( descr );
186 ERR( "loaded .so but dll %s still not found\n", dllname );
187 BUILTIN32_dlclose( handle );
194 /***********************************************************************
195 * __wine_register_dll_16 (KERNEL32.@)
197 * Register a built-in DLL descriptor.
199 void __wine_register_dll_16( const BUILTIN16_DESCRIPTOR *descr )
201 assert( nb_dlls < MAX_DLLS );
202 builtin_dlls[nb_dlls++] = descr;