2 * Win32 builtin functions
4 * Copyright 1997 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
22 #include "wine/port.h"
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
34 #include "wine/winbase16.h"
35 #include "wine/library.h"
38 #include "ntdll_misc.h"
39 #include "wine/server.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(module
);
43 WINE_DECLARE_DEBUG_CHANNEL(relay
);
45 extern void RELAY_SetupDLL( const char *module
);
47 static HMODULE main_module
;
48 static NTSTATUS last_status
; /* use to gather all errors in callback */
50 /***********************************************************************
53 * The loader critical section must be locked while calling this function
55 NTSTATUS
BUILTIN32_dlopen( const char *name
, void** handle
)
59 last_status
= STATUS_SUCCESS
;
60 /* load_library will modify last_status. Note also that load_library can be
61 * called several times, if the .so file we're loading has dependencies.
62 * last_status will gather all the errors we may get while loading all these
65 if (!(*handle
= wine_dll_load( name
, error
, sizeof(error
) )))
67 if (strstr(error
, "cannot open") || strstr(error
, "open failed") ||
68 (strstr(error
, "Shared object") && strstr(error
, "not found"))) {
69 /* The file does not exist -> WARN() */
70 WARN("cannot open .so lib for builtin %s: %s\n", name
, error
);
71 last_status
= STATUS_NO_SUCH_FILE
;
73 /* ERR() for all other errors (missing functions, ...) */
74 ERR("failed to load .so lib for builtin %s: %s\n", name
, error
);
75 last_status
= STATUS_PROCEDURE_NOT_FOUND
;
82 /***********************************************************************
85 * Load a library in memory; callback function for wine_dll_register
87 static void load_library( void *base
, const char *filename
)
90 HMODULE module
= (HMODULE
)base
, ret
;
98 ERR("could not map image for %s\n", filename
? filename
: "main exe" );
101 if (!(nt
= RtlImageNtHeader( module
)))
103 ERR( "bad module for %s\n", filename
? filename
: "main exe" );
104 last_status
= STATUS_INVALID_IMAGE_FORMAT
;
108 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
110 /* if we already have an executable, ignore this one */
111 if (!main_module
) main_module
= module
;
112 return; /* don't create the modref here, will be done later on */
115 RtlCreateUnicodeStringFromAsciiz(&wstr
, filename
);
116 if (LdrGetDllHandle(0, 0, &wstr
, &ret
) == STATUS_SUCCESS
)
117 MESSAGE( "Warning: loading builtin %s, but native version already present. "
118 "Expect trouble.\n", filename
);
119 RtlFreeUnicodeString( &wstr
);
121 len
= GetSystemDirectoryA( NULL
, 0 );
122 if (!(fullname
= RtlAllocateHeap( ntdll_get_process_heap(), 0, len
+ strlen(filename
) + 1 )))
124 ERR( "can't load %s\n", filename
);
125 last_status
= STATUS_NO_MEMORY
;
128 GetSystemDirectoryA( fullname
, len
);
129 strcat( fullname
, "\\" );
130 strcat( fullname
, filename
);
132 /* Create 32-bit MODREF */
133 if (!(wm
= PE_CreateModule( module
, fullname
, 0, 0, TRUE
)))
135 ERR( "can't load %s\n", filename
);
136 RtlFreeHeap( ntdll_get_process_heap(), 0, fullname
);
137 last_status
= STATUS_NO_MEMORY
;
140 TRACE( "loaded %s %p %p\n", fullname
, wm
, module
);
141 RtlFreeHeap( ntdll_get_process_heap(), 0, fullname
);
143 /* setup relay debugging entry points */
144 if (TRACE_ON(relay
)) RELAY_SetupDLL( (void *)module
);
148 /***********************************************************************
149 * BUILTIN32_LoadLibraryExA
151 * Partly copied from the original PE_ version.
154 NTSTATUS
BUILTIN32_LoadLibraryExA(LPCSTR path
, DWORD flags
, WINE_MODREF
** pwm
)
156 char dllname
[20], *p
;
161 /* Fix the name in case we have a full path and extension */
163 if ((p
= strrchr( name
, '\\' ))) name
= p
+ 1;
164 if ((p
= strrchr( name
, '/' ))) name
= p
+ 1;
166 if (strlen(name
) >= sizeof(dllname
)-4) return STATUS_NO_SUCH_FILE
;
168 strcpy( dllname
, name
);
169 p
= strrchr( dllname
, '.' );
170 if (!p
) strcat( dllname
, ".dll" );
171 for (p
= dllname
; *p
; p
++) *p
= FILE_tolower(*p
);
173 if ((nts
= BUILTIN32_dlopen( dllname
, &handle
)) != STATUS_SUCCESS
)
176 if (!((*pwm
) = MODULE_FindModule( path
))) *pwm
= MODULE_FindModule( dllname
);
179 ERR( "loaded .so but dll %s still not found - 16-bit dll or version conflict.\n", dllname
);
180 /* wine_dll_unload( handle );*/
181 return STATUS_INVALID_IMAGE_FORMAT
;
183 (*pwm
)->dlhandle
= handle
;
184 return STATUS_SUCCESS
;
187 /***********************************************************************
190 * Initialize loading callbacks and return HMODULE of main exe.
191 * 'main' is the main exe in case it was already loaded from a PE file.
193 HMODULE
BUILTIN32_LoadExeModule( HMODULE main
)
196 last_status
= STATUS_SUCCESS
;
197 wine_dll_set_callback( load_library
);
199 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
200 if (last_status
!= STATUS_SUCCESS
)
201 MESSAGE( "Error while processing initial modules\n");