Portability fix.
[wine/wine64.git] / if1632 / builtin.c
blob0dca8a61e17dbef4ef00276fc88198b53f43da4b
1 /*
2 * Built-in modules
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
21 #include <assert.h>
22 #include <ctype.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "winbase.h"
26 #include "wine/winbase16.h"
27 #include "builtin16.h"
28 #include "global.h"
29 #include "file.h"
30 #include "module.h"
31 #include "miscemu.h"
32 #include "stackframe.h"
33 #include "wine/library.h"
34 #include "wine/debug.h"
35 #include "toolhelp.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(module);
39 typedef struct
41 void *module_start; /* 32-bit address of the module data */
42 int module_size; /* Size of the module data */
43 void *code_start; /* 32-bit address of DLL code */
44 void *data_start; /* 32-bit address of DLL data */
45 const char *owner; /* 32-bit dll that contains this dll */
46 const void *rsrc; /* resources data */
47 } BUILTIN16_DESCRIPTOR;
49 /* Table of all built-in DLLs */
51 #define MAX_DLLS 50
53 static const BUILTIN16_DESCRIPTOR *builtin_dlls[MAX_DLLS];
54 static int nb_dlls;
57 /* patch all the flat cs references of the code segment if necessary */
58 inline static void patch_code_segment( void *code_segment )
60 #ifdef __i386__
61 CALLFROM16 *call = code_segment;
62 if (call->flatcs == wine_get_cs()) return; /* nothing to patch */
63 while (call->pushl == 0x68)
65 call->flatcs = wine_get_cs();
66 call++;
68 #endif
72 /***********************************************************************
73 * BUILTIN_DoLoadModule16
75 * Load a built-in Win16 module. Helper function for BUILTIN_LoadModule.
77 static HMODULE16 BUILTIN_DoLoadModule16( const BUILTIN16_DESCRIPTOR *descr )
79 NE_MODULE *pModule;
80 int minsize;
81 SEGTABLEENTRY *pSegTable;
82 HMODULE16 hModule;
84 hModule = GLOBAL_CreateBlock( GMEM_MOVEABLE, descr->module_start,
85 descr->module_size, 0, WINE_LDT_FLAGS_DATA );
86 if (!hModule) return 0;
87 FarSetOwner16( hModule, hModule );
89 pModule = (NE_MODULE *)GlobalLock16( hModule );
90 pModule->self = hModule;
91 /* NOTE: (Ab)use the hRsrcMap parameter for resource data pointer */
92 pModule->hRsrcMap = (void *)descr->rsrc;
94 /* Allocate the code segment */
96 pSegTable = NE_SEG_TABLE( pModule );
97 pSegTable->hSeg = GLOBAL_CreateBlock( GMEM_FIXED, descr->code_start,
98 pSegTable->minsize, hModule,
99 WINE_LDT_FLAGS_CODE|WINE_LDT_FLAGS_32BIT );
100 if (!pSegTable->hSeg) return 0;
101 patch_code_segment( descr->code_start );
102 pSegTable++;
104 /* Allocate the data segment */
106 minsize = pSegTable->minsize ? pSegTable->minsize : 0x10000;
107 minsize += pModule->heap_size;
108 if (minsize > 0x10000) minsize = 0x10000;
109 pSegTable->hSeg = GlobalAlloc16( GMEM_FIXED, minsize );
110 if (!pSegTable->hSeg) return 0;
111 FarSetOwner16( pSegTable->hSeg, hModule );
112 if (pSegTable->minsize) memcpy( GlobalLock16( pSegTable->hSeg ),
113 descr->data_start, pSegTable->minsize);
114 if (pModule->heap_size)
115 LocalInit16( GlobalHandleToSel16(pSegTable->hSeg),
116 pSegTable->minsize, minsize );
118 if (descr->rsrc) NE_InitResourceHandler(hModule);
120 NE_RegisterModule( pModule );
122 /* make sure the 32-bit library containing this one is loaded too */
123 LoadLibraryA( descr->owner );
125 return hModule;
129 /***********************************************************************
130 * find_dll_descr
132 * Find a descriptor in the list
134 static const BUILTIN16_DESCRIPTOR *find_dll_descr( const char *dllname )
136 int i;
137 for (i = 0; i < nb_dlls; i++)
139 const BUILTIN16_DESCRIPTOR *descr = builtin_dlls[i];
140 NE_MODULE *pModule = (NE_MODULE *)descr->module_start;
141 OFSTRUCT *pOfs = (OFSTRUCT *)((LPBYTE)pModule + pModule->fileinfo);
142 BYTE *name_table = (BYTE *)pModule + pModule->name_table;
144 /* check the dll file name */
145 if (!FILE_strcasecmp( pOfs->szPathName, dllname ))
146 return descr;
147 /* check the dll module name (without extension) */
148 if (!FILE_strncasecmp( dllname, name_table+1, *name_table ) &&
149 !strcmp( dllname + *name_table, ".dll" ))
150 return descr;
152 return NULL;
156 /***********************************************************************
157 * BUILTIN_LoadModule
159 * Load a built-in module.
161 HMODULE16 BUILTIN_LoadModule( LPCSTR name )
163 const BUILTIN16_DESCRIPTOR *descr;
164 char dllname[20], *p;
165 void *handle;
167 /* Fix the name in case we have a full path and extension */
169 if ((p = strrchr( name, '\\' ))) name = p + 1;
170 if ((p = strrchr( name, '/' ))) name = p + 1;
172 if (strlen(name) >= sizeof(dllname)-4) return (HMODULE16)2;
174 strcpy( dllname, name );
175 p = strrchr( dllname, '.' );
176 if (!p) strcat( dllname, ".dll" );
177 for (p = dllname; *p; p++) *p = FILE_tolower(*p);
179 if ((descr = find_dll_descr( dllname )))
180 return BUILTIN_DoLoadModule16( descr );
182 if ((handle = BUILTIN32_dlopen( dllname )))
184 if ((descr = find_dll_descr( dllname )))
185 return BUILTIN_DoLoadModule16( descr );
187 ERR( "loaded .so but dll %s still not found\n", dllname );
188 BUILTIN32_dlclose( handle );
191 return (HMODULE16)2;
195 /***********************************************************************
196 * __wine_register_dll_16 (KERNEL32.@)
198 * Register a built-in DLL descriptor.
200 void __wine_register_dll_16( const BUILTIN16_DESCRIPTOR *descr )
202 assert( nb_dlls < MAX_DLLS );
203 builtin_dlls[nb_dlls++] = descr;