2 * Win32 builtin dlls support
4 * Copyright 2000 Alexandre Julliard
13 #include <sys/types.h>
15 #ifdef HAVE_SYS_MMAN_H
23 #include "wine/library.h"
24 #include "wine/port.h"
30 const IMAGE_NT_HEADERS
*nt
; /* NT header */
31 const char *filename
; /* DLL file name */
32 } builtin_dlls
[MAX_DLLS
];
36 static const IMAGE_NT_HEADERS
*main_exe
;
38 static load_dll_callback_t load_dll_callback
;
40 static char **dll_paths
;
41 static int nb_dll_paths
;
42 static int dll_path_maxlen
;
46 /* build the dll load path from the WINEDLLPATH variable */
47 static void build_dll_path(void)
50 char *p
, *path
= getenv( "WINEDLLPATH" );
58 while (*p
== ':') p
++;
61 while (*p
&& *p
!= ':') p
++;
64 dll_paths
= malloc( count
* sizeof(*dll_paths
) );
69 while (*p
== ':') *p
++ = 0;
71 dll_paths
[nb_dll_paths
] = p
;
72 while (*p
&& *p
!= ':') p
++;
73 if (p
- dll_paths
[nb_dll_paths
] > dll_path_maxlen
)
74 dll_path_maxlen
= p
- dll_paths
[nb_dll_paths
];
80 /* open a library for a given dll, searching in the dll path
81 * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
82 static void *dlopen_dll( const char *name
)
85 int i
, namelen
= strlen(name
);
86 char *buffer
, *p
, *ext
;
89 if (!init_done
) build_dll_path();
91 /* clear dlerror to avoid glibc bug */
94 buffer
= malloc( dll_path_maxlen
+ namelen
+ 8 );
96 /* store the name at the end of the buffer, prefixed by /lib and followed by .so */
97 p
= buffer
+ dll_path_maxlen
;
98 memcpy( p
, "/lib", 4 );
100 memcpy( p
, name
, namelen
+1 );
101 ext
= strrchr( p
, '.' );
103 /* check for .dll or .exe extension to remove */
104 if (ext
&& (!strcmp( ext
, ".dll" ) || !strcmp( ext
, ".exe" ))) p
= ext
;
105 memcpy( p
, ".so", 4 );
107 for (i
= 0; i
< nb_dll_paths
; i
++)
109 int len
= strlen(dll_paths
[i
]);
110 char *p
= buffer
+ dll_path_maxlen
- len
;
111 memcpy( p
, dll_paths
[i
], len
);
112 if ((ret
= dlopen( p
, RTLD_NOW
))) break;
113 dlerror(); /* clear dlerror to avoid glibc bug */
116 /* now try the default dlopen search path */
117 if (!ret
) ret
= dlopen( buffer
+ dll_path_maxlen
+ 1, RTLD_NOW
);
126 /* adjust an array of pointers to make them into RVAs */
127 static inline void fixup_rva_ptrs( void *array
, void *base
, int count
)
129 void **ptr
= (void **)array
;
132 if (*ptr
) *ptr
= (void *)((char *)*ptr
- (char *)base
);
138 /* fixup RVAs in the resource directory */
139 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY
*dir
, char *root
, void *base
)
141 IMAGE_RESOURCE_DIRECTORY_ENTRY
*entry
;
144 entry
= (IMAGE_RESOURCE_DIRECTORY_ENTRY
*)(dir
+ 1);
145 for (i
= 0; i
< dir
->NumberOfNamedEntries
+ dir
->NumberOfIdEntries
; i
++, entry
++)
147 void *ptr
= root
+ entry
->u2
.s2
.OffsetToDirectory
;
148 if (entry
->u2
.s2
.DataIsDirectory
) fixup_resources( ptr
, root
, base
);
151 IMAGE_RESOURCE_DATA_ENTRY
*data
= ptr
;
152 fixup_rva_ptrs( &data
->OffsetToData
, base
, 1 );
158 /* map a builtin dll in memory and fixup RVAs */
159 static void *map_dll( const IMAGE_NT_HEADERS
*nt_descr
)
161 IMAGE_DATA_DIRECTORY
*dir
;
162 IMAGE_DOS_HEADER
*dos
;
163 IMAGE_NT_HEADERS
*nt
;
164 IMAGE_SECTION_HEADER
*sec
;
165 BYTE
*addr
, *code_start
, *data_start
;
166 size_t page_size
= getpagesize();
167 int nb_sections
= 2; /* code + data */
169 size_t size
= (sizeof(IMAGE_DOS_HEADER
)
170 + sizeof(IMAGE_NT_HEADERS
)
171 + nb_sections
* sizeof(IMAGE_SECTION_HEADER
));
173 assert( size
<= page_size
);
175 if (nt_descr
->OptionalHeader
.ImageBase
)
177 addr
= wine_anon_mmap( (void *)nt_descr
->OptionalHeader
.ImageBase
,
178 page_size
, PROT_READ
|PROT_WRITE
, MAP_FIXED
);
179 if (addr
!= (BYTE
*)nt_descr
->OptionalHeader
.ImageBase
) return NULL
;
183 /* this will leak memory; but it should never happen */
184 addr
= wine_anon_mmap( NULL
, page_size
, PROT_READ
|PROT_WRITE
, 0 );
185 if (addr
== (BYTE
*)-1) return NULL
;
188 dos
= (IMAGE_DOS_HEADER
*)addr
;
189 nt
= (IMAGE_NT_HEADERS
*)(dos
+ 1);
190 sec
= (IMAGE_SECTION_HEADER
*)(nt
+ 1);
191 code_start
= addr
+ page_size
;
194 data_start
= code_start
+ page_size
;
196 /* Build the DOS and NT headers */
198 dos
->e_magic
= IMAGE_DOS_SIGNATURE
;
199 dos
->e_lfanew
= sizeof(*dos
);
203 nt
->FileHeader
.NumberOfSections
= nb_sections
;
204 nt
->OptionalHeader
.SizeOfCode
= data_start
- code_start
;
205 nt
->OptionalHeader
.SizeOfInitializedData
= 0;
206 nt
->OptionalHeader
.SizeOfUninitializedData
= 0;
207 nt
->OptionalHeader
.ImageBase
= (DWORD
)addr
;
209 fixup_rva_ptrs( &nt
->OptionalHeader
.AddressOfEntryPoint
, addr
, 1 );
211 /* Build the code section */
213 strcpy( sec
->Name
, ".text" );
214 sec
->SizeOfRawData
= data_start
- code_start
;
215 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
216 sec
->VirtualAddress
= code_start
- addr
;
217 sec
->PointerToRawData
= code_start
- addr
;
218 sec
->Characteristics
= (IMAGE_SCN_CNT_CODE
| IMAGE_SCN_MEM_EXECUTE
| IMAGE_SCN_MEM_READ
);
221 /* Build the data section */
223 strcpy( sec
->Name
, ".data" );
224 sec
->SizeOfRawData
= 0;
225 sec
->Misc
.VirtualSize
= sec
->SizeOfRawData
;
226 sec
->VirtualAddress
= data_start
- addr
;
227 sec
->PointerToRawData
= data_start
- addr
;
228 sec
->Characteristics
= (IMAGE_SCN_CNT_INITIALIZED_DATA
|
229 IMAGE_SCN_MEM_WRITE
| IMAGE_SCN_MEM_READ
);
232 /* Build the import directory */
234 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_IMPORT_DIRECTORY
];
237 IMAGE_IMPORT_DESCRIPTOR
*imports
= (void *)dir
->VirtualAddress
;
238 fixup_rva_ptrs( &dir
->VirtualAddress
, addr
, 1 );
239 /* we can fixup everything at once since we only have pointers and 0 values */
240 fixup_rva_ptrs( imports
, addr
, dir
->Size
/ sizeof(void*) );
243 /* Build the resource directory */
245 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_RESOURCE_DIRECTORY
];
248 void *ptr
= (void *)dir
->VirtualAddress
;
249 fixup_rva_ptrs( &dir
->VirtualAddress
, addr
, 1 );
250 fixup_resources( ptr
, ptr
, addr
);
253 /* Build the export directory */
255 dir
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_FILE_EXPORT_DIRECTORY
];
258 IMAGE_EXPORT_DIRECTORY
*exports
= (void *)dir
->VirtualAddress
;
259 fixup_rva_ptrs( &dir
->VirtualAddress
, addr
, 1 );
260 fixup_rva_ptrs( (void *)exports
->AddressOfFunctions
, addr
, exports
->NumberOfFunctions
);
261 fixup_rva_ptrs( (void *)exports
->AddressOfNames
, addr
, exports
->NumberOfNames
);
262 fixup_rva_ptrs( &exports
->Name
, addr
, 1 );
263 fixup_rva_ptrs( &exports
->AddressOfFunctions
, addr
, 1 );
264 fixup_rva_ptrs( &exports
->AddressOfNames
, addr
, 1 );
265 fixup_rva_ptrs( &exports
->AddressOfNameOrdinals
, addr
, 1 );
271 /***********************************************************************
272 * __wine_dll_register
274 * Register a built-in DLL descriptor.
276 void __wine_dll_register( const IMAGE_NT_HEADERS
*header
, const char *filename
)
278 if (load_dll_callback
) load_dll_callback( map_dll(header
), filename
);
281 if (!(header
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
285 assert( nb_dlls
< MAX_DLLS
);
286 builtin_dlls
[nb_dlls
].nt
= header
;
287 builtin_dlls
[nb_dlls
].filename
= filename
;
294 /***********************************************************************
295 * wine_dll_set_callback
297 * Set the callback function for dll loading, and call it
298 * for all dlls that were implicitly loaded already.
300 void wine_dll_set_callback( load_dll_callback_t load
)
303 load_dll_callback
= load
;
304 for (i
= 0; i
< nb_dlls
; i
++)
306 const IMAGE_NT_HEADERS
*nt
= builtin_dlls
[i
].nt
;
308 builtin_dlls
[i
].nt
= NULL
;
309 load_dll_callback( map_dll(nt
), builtin_dlls
[i
].filename
);
312 if (main_exe
) load_dll_callback( map_dll(main_exe
), "" );
316 /***********************************************************************
319 * Load a builtin dll.
321 void *wine_dll_load( const char *filename
)
325 /* callback must have been set already */
326 assert( load_dll_callback
);
328 /* check if we have it in the list */
329 /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
330 for (i
= 0; i
< nb_dlls
; i
++)
332 if (!builtin_dlls
[i
].nt
) continue;
333 if (!strcmp( builtin_dlls
[i
].filename
, filename
))
335 const IMAGE_NT_HEADERS
*nt
= builtin_dlls
[i
].nt
;
336 builtin_dlls
[i
].nt
= NULL
;
337 load_dll_callback( map_dll(nt
), builtin_dlls
[i
].filename
);
341 return dlopen_dll( filename
);
345 /***********************************************************************
348 * Unload a builtin dll.
350 void wine_dll_unload( void *handle
)
353 if (handle
!= (void *)1) dlclose( handle
);
358 /***********************************************************************
359 * wine_dll_load_main_exe
361 * Try to load the .so for the main exe, optionally searching for it in PATH.
362 * Note: dlerror() is cleared before returning because of a glibc bug.
364 void *wine_dll_load_main_exe( const char *name
, int search_path
)
368 const char *path
= NULL
;
369 if (search_path
) path
= getenv( "PATH" );
373 /* no path, try only the specified name */
374 dlerror(); /* clear dlerror to avoid glibc bug */
375 ret
= dlopen( name
, RTLD_NOW
);
379 char buffer
[128], *tmp
= buffer
;
380 size_t namelen
= strlen(name
);
381 size_t pathlen
= strlen(path
);
383 if (namelen
+ pathlen
+ 2 > sizeof(buffer
)) tmp
= malloc( namelen
+ pathlen
+ 2 );
386 char *basename
= tmp
+ pathlen
;
388 strcpy( basename
+ 1, name
);
392 const char *p
= strchr( path
, ':' );
393 if (!p
) p
= path
+ strlen(path
);
394 if ((len
= p
- path
) > 0)
396 memcpy( basename
- len
, path
, len
);
397 dlerror(); /* clear dlerror to avoid glibc bug */
398 if ((ret
= dlopen( basename
- len
, RTLD_NOW
))) break;
403 if (tmp
!= buffer
) free( tmp
);
406 if (!ret
) dlerror(); /* clear dlerror to avoid glibc bug */
407 #endif /* HAVE_DL_API */