wined3d: Use dst_fbo to do the depth blit.
[wine] / libs / wine / loader.c
1 /*
2  * Win32 builtin dlls support
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <assert.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <limits.h>
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/mman.h>
34 #endif
35 #ifdef HAVE_SYS_RESOURCE_H
36 # include <sys/resource.h>
37 #endif
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wine/library.h"
47
48 #ifdef __APPLE__
49 #include <crt_externs.h>
50 #define environ (*_NSGetEnviron())
51 #else
52 extern char **environ;
53 #endif
54
55 /* argc/argv for the Windows application */
56 int __wine_main_argc = 0;
57 char **__wine_main_argv = NULL;
58 WCHAR **__wine_main_wargv = NULL;
59 char **__wine_main_environ = NULL;
60
61 struct dll_path_context
62 {
63     unsigned int index; /* current index in the dll path list */
64     char *buffer;       /* buffer used for storing path names */
65     char *name;         /* start of file name part in buffer (including leading slash) */
66     int   namelen;      /* length of file name without .so extension */
67     int   win16;        /* 16-bit dll search */
68 };
69
70 #define MAX_DLLS 100
71
72 static struct
73 {
74     const IMAGE_NT_HEADERS *nt;           /* NT header */
75     const char             *filename;     /* DLL file name */
76 } builtin_dlls[MAX_DLLS];
77
78 static int nb_dlls;
79
80 static const IMAGE_NT_HEADERS *main_exe;
81
82 static load_dll_callback_t load_dll_callback;
83
84 static const char *build_dir;
85 static const char *default_dlldir;
86 static const char **dll_paths;
87 static unsigned int nb_dll_paths;
88 static int dll_path_maxlen;
89
90 extern void mmap_init(void);
91 extern const char *get_dlldir( const char **default_dlldir );
92
93 /* build the dll load path from the WINEDLLPATH variable */
94 static void build_dll_path(void)
95 {
96     int len, count = 0;
97     char *p, *path = getenv( "WINEDLLPATH" );
98     const char *dlldir = get_dlldir( &default_dlldir );
99
100     if (path)
101     {
102         /* count how many path elements we need */
103         path = strdup(path);
104         p = path;
105         while (*p)
106         {
107             while (*p == ':') p++;
108             if (!*p) break;
109             count++;
110             while (*p && *p != ':') p++;
111         }
112     }
113
114     dll_paths = malloc( (count+2) * sizeof(*dll_paths) );
115     nb_dll_paths = 0;
116
117     if (dlldir)
118     {
119         dll_path_maxlen = strlen(dlldir);
120         dll_paths[nb_dll_paths++] = dlldir;
121     }
122     else if ((build_dir = wine_get_build_dir()))
123     {
124         dll_path_maxlen = strlen(build_dir) + sizeof("/programs");
125     }
126
127     if (count)
128     {
129         p = path;
130         while (*p)
131         {
132             while (*p == ':') *p++ = 0;
133             if (!*p) break;
134             dll_paths[nb_dll_paths] = p;
135             while (*p && *p != ':') p++;
136             if (p - dll_paths[nb_dll_paths] > dll_path_maxlen)
137                 dll_path_maxlen = p - dll_paths[nb_dll_paths];
138             nb_dll_paths++;
139         }
140     }
141
142     /* append default dll dir (if not empty) to path */
143     if ((len = strlen(default_dlldir)) > 0)
144     {
145         if (len > dll_path_maxlen) dll_path_maxlen = len;
146         dll_paths[nb_dll_paths++] = default_dlldir;
147     }
148 }
149
150 /* check if a given file can be opened */
151 static inline int file_exists( const char *name )
152 {
153     int fd = open( name, O_RDONLY );
154     if (fd != -1) close( fd );
155     return (fd != -1);
156 }
157
158 static inline char *prepend( char *buffer, const char *str, size_t len )
159 {
160     return memcpy( buffer - len, str, len );
161 }
162
163 /* get a filename from the next entry in the dll path */
164 static char *next_dll_path( struct dll_path_context *context )
165 {
166     unsigned int index = context->index++;
167     int namelen = context->namelen;
168     char *path = context->name;
169
170     switch(index)
171     {
172     case 0:  /* try programs dir for .exe files */
173         if (!context->win16 && namelen > 4 && !memcmp( context->name + namelen - 4, ".exe", 4 ))
174         {
175             path = prepend( path, context->name, namelen - 4 );
176             path = prepend( path, "/programs", sizeof("/programs") - 1 );
177             path = prepend( path, build_dir, strlen(build_dir) );
178             return path;
179         }
180         context->index++;
181         /* fall through */
182     case 1:  /* try dlls dir with subdir prefix */
183         if (namelen > 4 && !memcmp( context->name + namelen - 4, ".dll", 4 )) namelen -= 4;
184         if (!context->win16) path = prepend( path, context->name, namelen );
185         path = prepend( path, "/dlls", sizeof("/dlls") - 1 );
186         path = prepend( path, build_dir, strlen(build_dir) );
187         return path;
188     default:
189         index -= 2;
190         if (index < nb_dll_paths)
191             return prepend( context->name, dll_paths[index], strlen( dll_paths[index] ));
192         break;
193     }
194     return NULL;
195 }
196
197
198 /* get a filename from the first entry in the dll path */
199 static char *first_dll_path( const char *name, int win16, struct dll_path_context *context )
200 {
201     char *p;
202     int namelen = strlen( name );
203     const char *ext = win16 ? "16" : ".so";
204
205     context->buffer = malloc( dll_path_maxlen + 2 * namelen + strlen(ext) + 3 );
206     context->index = build_dir ? 0 : 2;  /* if no build dir skip all the build dir magic cases */
207     context->name = context->buffer + dll_path_maxlen + namelen + 1;
208     context->namelen = namelen + 1;
209     context->win16 = win16;
210
211     /* store the name at the end of the buffer, followed by extension */
212     p = context->name;
213     *p++ = '/';
214     memcpy( p, name, namelen );
215     strcpy( p + namelen, ext );
216     return next_dll_path( context );
217 }
218
219
220 /* free the dll path context created by first_dll_path */
221 static inline void free_dll_path( struct dll_path_context *context )
222 {
223     free( context->buffer );
224 }
225
226
227 /* open a library for a given dll, searching in the dll path
228  * 'name' must be the Windows dll name (e.g. "kernel32.dll") */
229 static void *dlopen_dll( const char *name, char *error, int errorsize,
230                          int test_only, int *exists )
231 {
232     struct dll_path_context context;
233     char *path;
234     void *ret = NULL;
235
236     *exists = 0;
237     for (path = first_dll_path( name, 0, &context ); path; path = next_dll_path( &context ))
238     {
239         if (!test_only && (ret = wine_dlopen( path, RTLD_NOW, error, errorsize ))) break;
240         if ((*exists = file_exists( path ))) break; /* exists but cannot be loaded, return the error */
241     }
242     free_dll_path( &context );
243     return ret;
244 }
245
246
247 /* adjust an array of pointers to make them into RVAs */
248 static inline void fixup_rva_ptrs( void *array, BYTE *base, unsigned int count )
249 {
250     void **src = (void **)array;
251     DWORD *dst = (DWORD *)array;
252     while (count--)
253     {
254         *dst++ = *src ? (BYTE *)*src - base : 0;
255         src++;
256     }
257 }
258
259 /* fixup an array of RVAs by adding the specified delta */
260 static inline void fixup_rva_dwords( DWORD *ptr, int delta, unsigned int count )
261 {
262     while (count--)
263     {
264         if (*ptr) *ptr += delta;
265         ptr++;
266     }
267 }
268
269
270 /* fixup RVAs in the import directory */
271 static void fixup_imports( IMAGE_IMPORT_DESCRIPTOR *dir, BYTE *base, int delta )
272 {
273     UINT_PTR *ptr;
274
275     while (dir->Name)
276     {
277         fixup_rva_dwords( &dir->u.OriginalFirstThunk, delta, 1 );
278         fixup_rva_dwords( &dir->Name, delta, 1 );
279         fixup_rva_dwords( &dir->FirstThunk, delta, 1 );
280         ptr = (UINT_PTR *)(base + dir->FirstThunk);
281         while (*ptr)
282         {
283             if (!(*ptr & IMAGE_ORDINAL_FLAG)) *ptr += delta;
284             ptr++;
285         }
286         dir++;
287     }
288 }
289
290
291 /* fixup RVAs in the export directory */
292 static void fixup_exports( IMAGE_EXPORT_DIRECTORY *dir, BYTE *base, int delta )
293 {
294     fixup_rva_dwords( &dir->Name, delta, 1 );
295     fixup_rva_dwords( &dir->AddressOfFunctions, delta, 1 );
296     fixup_rva_dwords( &dir->AddressOfNames, delta, 1 );
297     fixup_rva_dwords( &dir->AddressOfNameOrdinals, delta, 1 );
298     fixup_rva_dwords( (DWORD *)(base + dir->AddressOfNames), delta, dir->NumberOfNames );
299     fixup_rva_ptrs( (base + dir->AddressOfFunctions), base, dir->NumberOfFunctions );
300 }
301
302
303 /* fixup RVAs in the resource directory */
304 static void fixup_resources( IMAGE_RESOURCE_DIRECTORY *dir, BYTE *root, int delta )
305 {
306     IMAGE_RESOURCE_DIRECTORY_ENTRY *entry;
307     int i;
308
309     entry = (IMAGE_RESOURCE_DIRECTORY_ENTRY *)(dir + 1);
310     for (i = 0; i < dir->NumberOfNamedEntries + dir->NumberOfIdEntries; i++, entry++)
311     {
312         void *ptr = root + entry->u2.s3.OffsetToDirectory;
313         if (entry->u2.s3.DataIsDirectory) fixup_resources( ptr, root, delta );
314         else
315         {
316             IMAGE_RESOURCE_DATA_ENTRY *data = ptr;
317             fixup_rva_dwords( &data->OffsetToData, delta, 1 );
318         }
319     }
320 }
321
322
323 /* map a builtin dll in memory and fixup RVAs */
324 static void *map_dll( const IMAGE_NT_HEADERS *nt_descr )
325 {
326 #ifdef HAVE_MMAP
327     IMAGE_DATA_DIRECTORY *dir;
328     IMAGE_DOS_HEADER *dos;
329     IMAGE_NT_HEADERS *nt;
330     IMAGE_SECTION_HEADER *sec;
331     BYTE *addr;
332     DWORD code_start, data_start, data_end;
333     const size_t page_size = getpagesize();
334     const size_t page_mask = page_size - 1;
335     int delta, nb_sections = 2;  /* code + data */
336     unsigned int i;
337
338     size_t size = (sizeof(IMAGE_DOS_HEADER)
339                    + sizeof(IMAGE_NT_HEADERS)
340                    + nb_sections * sizeof(IMAGE_SECTION_HEADER));
341
342     assert( size <= page_size );
343
344     /* module address must be aligned on 64K boundary */
345     addr = (BYTE *)((nt_descr->OptionalHeader.ImageBase + 0xffff) & ~0xffff);
346     if (wine_anon_mmap( addr, page_size, PROT_READ|PROT_WRITE, MAP_FIXED ) != addr) return NULL;
347
348     dos    = (IMAGE_DOS_HEADER *)addr;
349     nt     = (IMAGE_NT_HEADERS *)(dos + 1);
350     sec    = (IMAGE_SECTION_HEADER *)(nt + 1);
351
352     /* Build the DOS and NT headers */
353
354     dos->e_magic    = IMAGE_DOS_SIGNATURE;
355     dos->e_cblp     = 0x90;
356     dos->e_cp       = 3;
357     dos->e_cparhdr  = (sizeof(*dos)+0xf)/0x10;
358     dos->e_minalloc = 0;
359     dos->e_maxalloc = 0xffff;
360     dos->e_ss       = 0x0000;
361     dos->e_sp       = 0x00b8;
362     dos->e_lfarlc   = sizeof(*dos);
363     dos->e_lfanew   = sizeof(*dos);
364
365     *nt = *nt_descr;
366
367     delta      = (const BYTE *)nt_descr - addr;
368     code_start = page_size;
369     data_start = delta & ~page_mask;
370     data_end   = (nt->OptionalHeader.SizeOfImage + delta + page_mask) & ~page_mask;
371
372     fixup_rva_ptrs( &nt->OptionalHeader.AddressOfEntryPoint, addr, 1 );
373
374     nt->FileHeader.NumberOfSections                = nb_sections;
375     nt->OptionalHeader.BaseOfCode                  = code_start;
376 #ifndef _WIN64
377     nt->OptionalHeader.BaseOfData                  = data_start;
378 #endif
379     nt->OptionalHeader.SizeOfCode                  = data_start - code_start;
380     nt->OptionalHeader.SizeOfInitializedData       = data_end - data_start;
381     nt->OptionalHeader.SizeOfUninitializedData     = 0;
382     nt->OptionalHeader.SizeOfImage                 = data_end;
383     nt->OptionalHeader.ImageBase                   = (ULONG_PTR)addr;
384
385     /* Build the code section */
386
387     memcpy( sec->Name, ".text", sizeof(".text") );
388     sec->SizeOfRawData = data_start - code_start;
389     sec->Misc.VirtualSize = sec->SizeOfRawData;
390     sec->VirtualAddress   = code_start;
391     sec->PointerToRawData = code_start;
392     sec->Characteristics  = (IMAGE_SCN_CNT_CODE | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ);
393     sec++;
394
395     /* Build the data section */
396
397     memcpy( sec->Name, ".data", sizeof(".data") );
398     sec->SizeOfRawData = data_end - data_start;
399     sec->Misc.VirtualSize = sec->SizeOfRawData;
400     sec->VirtualAddress   = data_start;
401     sec->PointerToRawData = data_start;
402     sec->Characteristics  = (IMAGE_SCN_CNT_INITIALIZED_DATA |
403                              IMAGE_SCN_MEM_WRITE | IMAGE_SCN_MEM_READ);
404     sec++;
405
406     for (i = 0; i < nt->OptionalHeader.NumberOfRvaAndSizes; i++)
407         fixup_rva_dwords( &nt->OptionalHeader.DataDirectory[i].VirtualAddress, delta, 1 );
408
409     /* Build the import directory */
410
411     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_IMPORT_DIRECTORY];
412     if (dir->Size)
413     {
414         IMAGE_IMPORT_DESCRIPTOR *imports = (void *)(addr + dir->VirtualAddress);
415         fixup_imports( imports, addr, delta );
416     }
417
418     /* Build the resource directory */
419
420     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_RESOURCE_DIRECTORY];
421     if (dir->Size)
422     {
423         void *ptr = (void *)(addr + dir->VirtualAddress);
424         fixup_resources( ptr, ptr, delta );
425     }
426
427     /* Build the export directory */
428
429     dir = &nt->OptionalHeader.DataDirectory[IMAGE_FILE_EXPORT_DIRECTORY];
430     if (dir->Size)
431     {
432         IMAGE_EXPORT_DIRECTORY *exports = (void *)(addr + dir->VirtualAddress);
433         fixup_exports( exports, addr, delta );
434     }
435     return addr;
436 #else  /* HAVE_MMAP */
437     return NULL;
438 #endif  /* HAVE_MMAP */
439 }
440
441
442 /***********************************************************************
443  *           __wine_dll_register
444  *
445  * Register a built-in DLL descriptor.
446  */
447 void __wine_dll_register( const IMAGE_NT_HEADERS *header, const char *filename )
448 {
449     if (load_dll_callback) load_dll_callback( map_dll(header), filename );
450     else
451     {
452         if (!(header->FileHeader.Characteristics & IMAGE_FILE_DLL))
453             main_exe = header;
454         else
455         {
456             assert( nb_dlls < MAX_DLLS );
457             builtin_dlls[nb_dlls].nt = header;
458             builtin_dlls[nb_dlls].filename = filename;
459             nb_dlls++;
460         }
461     }
462 }
463
464
465 /***********************************************************************
466  *           wine_dll_set_callback
467  *
468  * Set the callback function for dll loading, and call it
469  * for all dlls that were implicitly loaded already.
470  */
471 void wine_dll_set_callback( load_dll_callback_t load )
472 {
473     int i;
474     load_dll_callback = load;
475     for (i = 0; i < nb_dlls; i++)
476     {
477         const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
478         if (!nt) continue;
479         builtin_dlls[i].nt = NULL;
480         load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
481     }
482     nb_dlls = 0;
483     if (main_exe) load_dll_callback( map_dll(main_exe), "" );
484 }
485
486
487 /***********************************************************************
488  *           wine_dll_load
489  *
490  * Load a builtin dll.
491  */
492 void *wine_dll_load( const char *filename, char *error, int errorsize, int *file_exists )
493 {
494     int i;
495
496     /* callback must have been set already */
497     assert( load_dll_callback );
498
499     /* check if we have it in the list */
500     /* this can happen when initializing pre-loaded dlls in wine_dll_set_callback */
501     for (i = 0; i < nb_dlls; i++)
502     {
503         if (!builtin_dlls[i].nt) continue;
504         if (!strcmp( builtin_dlls[i].filename, filename ))
505         {
506             const IMAGE_NT_HEADERS *nt = builtin_dlls[i].nt;
507             builtin_dlls[i].nt = NULL;
508             load_dll_callback( map_dll(nt), builtin_dlls[i].filename );
509             *file_exists = 1;
510             return (void *)1;
511         }
512     }
513     return dlopen_dll( filename, error, errorsize, 0, file_exists );
514 }
515
516
517 /***********************************************************************
518  *           wine_dll_unload
519  *
520  * Unload a builtin dll.
521  */
522 void wine_dll_unload( void *handle )
523 {
524     if (handle != (void *)1)
525         wine_dlclose( handle, NULL, 0 );
526 }
527
528
529 /***********************************************************************
530  *           wine_dll_load_main_exe
531  *
532  * Try to load the .so for the main exe.
533  */
534 void *wine_dll_load_main_exe( const char *name, char *error, int errorsize,
535                               int test_only, int *file_exists )
536 {
537     return dlopen_dll( name, error, errorsize, test_only, file_exists );
538 }
539
540
541 /***********************************************************************
542  *           wine_dll_enum_load_path
543  *
544  * Enumerate the dll load path.
545  */
546 const char *wine_dll_enum_load_path( unsigned int index )
547 {
548     if (index >= nb_dll_paths) return NULL;
549     return dll_paths[index];
550 }
551
552
553 /***********************************************************************
554  *           wine_dll_get_owner
555  *
556  * Retrieve the name of the 32-bit owner dll for a 16-bit dll.
557  * Return 0 if OK, -1 on error.
558  */
559 int wine_dll_get_owner( const char *name, char *buffer, int size, int *exists )
560 {
561     int ret = -1;
562     char *path;
563     struct dll_path_context context;
564
565     *exists = 0;
566
567     for (path = first_dll_path( name, 1, &context ); path; path = next_dll_path( &context ))
568     {
569         int fd = open( path, O_RDONLY );
570         if (fd != -1)
571         {
572             int res = read( fd, buffer, size - 1 );
573             while (res > 0 && (buffer[res-1] == '\n' || buffer[res-1] == '\r')) res--;
574             buffer[res] = 0;
575             close( fd );
576             *exists = 1;
577             ret = 0;
578             break;
579         }
580     }
581     free_dll_path( &context );
582     return ret;
583 }
584
585
586 /***********************************************************************
587  *           set_max_limit
588  *
589  * Set a user limit to the maximum allowed value.
590  */
591 static void set_max_limit( int limit )
592 {
593 #ifdef HAVE_SETRLIMIT
594     struct rlimit rlimit;
595
596     if (!getrlimit( limit, &rlimit ))
597     {
598         rlimit.rlim_cur = rlimit.rlim_max;
599         if (setrlimit( limit, &rlimit ) != 0)
600         {
601 #if defined(__APPLE__) && defined(RLIMIT_NOFILE) && defined(OPEN_MAX)
602             /* On Leopard, setrlimit(RLIMIT_NOFILE, ...) fails on attempts to set
603              * rlim_cur above OPEN_MAX (even if rlim_max > OPEN_MAX). */
604             if (limit == RLIMIT_NOFILE && rlimit.rlim_cur > OPEN_MAX)
605             {
606                 rlimit.rlim_cur = OPEN_MAX;
607                 setrlimit( limit, &rlimit );
608             }
609 #endif
610         }
611     }
612 #endif
613 }
614
615
616 /***********************************************************************
617  *           wine_init
618  *
619  * Main Wine initialisation.
620  */
621 void wine_init( int argc, char *argv[], char *error, int error_size )
622 {
623     struct dll_path_context context;
624     char *path;
625     void *ntdll = NULL;
626     void (*init_func)(void);
627
628     /* force a few limits that are set too low on some platforms */
629 #ifdef RLIMIT_NOFILE
630     set_max_limit( RLIMIT_NOFILE );
631 #endif
632 #ifdef RLIMIT_AS
633     set_max_limit( RLIMIT_AS );
634 #endif
635
636     wine_init_argv0_path( argv[0] );
637     build_dll_path();
638     __wine_main_argc = argc;
639     __wine_main_argv = argv;
640     __wine_main_environ = environ;
641     mmap_init();
642
643     for (path = first_dll_path( "ntdll.dll", 0, &context ); path; path = next_dll_path( &context ))
644     {
645         if ((ntdll = wine_dlopen( path, RTLD_NOW, error, error_size )))
646         {
647             /* if we didn't use the default dll dir, remove it from the search path */
648             if (default_dlldir[0] && context.index < nb_dll_paths + 2) nb_dll_paths--;
649             break;
650         }
651     }
652     free_dll_path( &context );
653
654     if (!ntdll) return;
655     if (!(init_func = wine_dlsym( ntdll, "__wine_process_init", error, error_size ))) return;
656     init_func();
657 }
658
659
660 /*
661  * These functions provide wrappers around dlopen() and associated
662  * functions.  They work around a bug in glibc 2.1.x where calling
663  * a dl*() function after a previous dl*() function has failed
664  * without a dlerror() call between the two will cause a crash.
665  * They all take a pointer to a buffer that
666  * will receive the error description (from dlerror()).  This
667  * parameter may be NULL if the error description is not required.
668  */
669
670 #ifndef RTLD_FIRST
671 #define RTLD_FIRST 0
672 #endif
673
674 /***********************************************************************
675  *              wine_dlopen
676  */
677 void *wine_dlopen( const char *filename, int flag, char *error, size_t errorsize )
678 {
679 #ifdef HAVE_DLOPEN
680     void *ret;
681     const char *s;
682
683 #ifdef __APPLE__
684     /* the Mac OS loader pretends to be able to load PE files, so avoid them here */
685     unsigned char magic[2];
686     int fd = open( filename, O_RDONLY );
687     if (fd != -1)
688     {
689         if (pread( fd, magic, 2, 0 ) == 2 && magic[0] == 'M' && magic[1] == 'Z')
690         {
691             static const char msg[] = "MZ format";
692             size_t len = min( errorsize, sizeof(msg) );
693             memcpy( error, msg, len );
694             error[len - 1] = 0;
695             close( fd );
696             return NULL;
697         }
698         close( fd );
699     }
700 #endif
701     dlerror(); dlerror();
702 #ifdef __sun
703     if (strchr( filename, ':' ))
704     {
705         char path[PATH_MAX];
706         /* Solaris' brain damaged dlopen() treats ':' as a path separator */
707         realpath( filename, path );
708         ret = dlopen( path, flag | RTLD_FIRST );
709     }
710     else
711 #endif
712     ret = dlopen( filename, flag | RTLD_FIRST );
713     s = dlerror();
714     if (error && errorsize)
715     {
716         if (s)
717         {
718             size_t len = strlen(s);
719             if (len >= errorsize) len = errorsize - 1;
720             memcpy( error, s, len );
721             error[len] = 0;
722         }
723         else error[0] = 0;
724     }
725     dlerror();
726     return ret;
727 #else
728     if (error)
729     {
730         static const char msg[] = "dlopen interface not detected by configure";
731         size_t len = min( errorsize, sizeof(msg) );
732         memcpy( error, msg, len );
733         error[len - 1] = 0;
734     }
735     return NULL;
736 #endif
737 }
738
739 /***********************************************************************
740  *              wine_dlsym
741  */
742 void *wine_dlsym( void *handle, const char *symbol, char *error, size_t errorsize )
743 {
744 #ifdef HAVE_DLOPEN
745     void *ret;
746     const char *s;
747     dlerror(); dlerror();
748     ret = dlsym( handle, symbol );
749     s = dlerror();
750     if (error && errorsize)
751     {
752         if (s)
753         {
754             size_t len = strlen(s);
755             if (len >= errorsize) len = errorsize - 1;
756             memcpy( error, s, len );
757             error[len] = 0;
758         }
759         else error[0] = 0;
760     }
761     dlerror();
762     return ret;
763 #else
764     if (error)
765     {
766         static const char msg[] = "dlopen interface not detected by configure";
767         size_t len = min( errorsize, sizeof(msg) );
768         memcpy( error, msg, len );
769         error[len - 1] = 0;
770     }
771     return NULL;
772 #endif
773 }
774
775 /***********************************************************************
776  *              wine_dlclose
777  */
778 int wine_dlclose( void *handle, char *error, size_t errorsize )
779 {
780 #ifdef HAVE_DLOPEN
781     int ret;
782     const char *s;
783     dlerror(); dlerror();
784     ret = dlclose( handle );
785     s = dlerror();
786     if (error && errorsize)
787     {
788         if (s)
789         {
790             size_t len = strlen(s);
791             if (len >= errorsize) len = errorsize - 1;
792             memcpy( error, s, len );
793             error[len] = 0;
794         }
795         else error[0] = 0;
796     }
797     dlerror();
798     return ret;
799 #else
800     if (error)
801     {
802         static const char msg[] = "dlopen interface not detected by configure";
803         size_t len = min( errorsize, sizeof(msg) );
804         memcpy( error, msg, len );
805         error[len - 1] = 0;
806     }
807     return 1;
808 #endif
809 }