Release 20030408.
[wine/gsoc-2012-control.git] / relay32 / builtin32.c
blob3bcbb1be76b939f1bd5c17be7e00d4c9f7416b82
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <sys/types.h>
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
33 #include "windef.h"
34 #include "wine/winbase16.h"
35 #include "wine/library.h"
36 #include "module.h"
37 #include "file.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 static HMODULE main_module;
46 static NTSTATUS last_status; /* use to gather all errors in callback */
48 /***********************************************************************
49 * BUILTIN32_dlopen
51 * The loader critical section must be locked while calling this function
53 NTSTATUS BUILTIN32_dlopen( const char *name, void** handle)
55 char error[256];
57 last_status = STATUS_SUCCESS;
58 /* load_library will modify last_status. Note also that load_library can be
59 * called several times, if the .so file we're loading has dependencies.
60 * last_status will gather all the errors we may get while loading all these
61 * libraries
63 if (!(*handle = wine_dll_load( name, error, sizeof(error) )))
65 if (strstr(error, "cannot open") || strstr(error, "open failed") ||
66 (strstr(error, "Shared object") && strstr(error, "not found"))) {
67 /* The file does not exist -> WARN() */
68 WARN("cannot open .so lib for builtin %s: %s\n", name, error);
69 last_status = STATUS_NO_SUCH_FILE;
70 } else {
71 /* ERR() for all other errors (missing functions, ...) */
72 ERR("failed to load .so lib for builtin %s: %s\n", name, error );
73 last_status = STATUS_PROCEDURE_NOT_FOUND;
76 return last_status;
80 /***********************************************************************
81 * load_library
83 * Load a library in memory; callback function for wine_dll_register
85 static void load_library( void *base, const char *filename )
87 UNICODE_STRING wstr;
88 HMODULE module = (HMODULE)base, ret;
89 IMAGE_NT_HEADERS *nt;
90 WINE_MODREF *wm;
91 char *fullname;
92 DWORD len;
94 if (!base)
96 ERR("could not map image for %s\n", filename ? filename : "main exe" );
97 return;
99 if (!(nt = RtlImageNtHeader( module )))
101 ERR( "bad module for %s\n", filename ? filename : "main exe" );
102 last_status = STATUS_INVALID_IMAGE_FORMAT;
103 return;
106 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
108 /* if we already have an executable, ignore this one */
109 if (!main_module) main_module = module;
110 return; /* don't create the modref here, will be done later on */
113 RtlCreateUnicodeStringFromAsciiz(&wstr, filename);
114 if (LdrGetDllHandle(0, 0, &wstr, &ret) == STATUS_SUCCESS)
115 MESSAGE( "Warning: loading builtin %s, but native version already present. "
116 "Expect trouble.\n", filename );
117 RtlFreeUnicodeString( &wstr );
119 len = GetSystemDirectoryA( NULL, 0 );
120 if (!(fullname = RtlAllocateHeap( ntdll_get_process_heap(), 0, len + strlen(filename) + 1 )))
122 ERR( "can't load %s\n", filename );
123 last_status = STATUS_NO_MEMORY;
124 return;
126 GetSystemDirectoryA( fullname, len );
127 strcat( fullname, "\\" );
128 strcat( fullname, filename );
130 /* Create 32-bit MODREF */
131 if (!(wm = PE_CreateModule( module, fullname, 0, 0, TRUE )))
133 ERR( "can't load %s\n", filename );
134 RtlFreeHeap( ntdll_get_process_heap(), 0, fullname );
135 last_status = STATUS_NO_MEMORY;
136 return;
138 TRACE( "loaded %s %p %p\n", fullname, wm, module );
139 RtlFreeHeap( ntdll_get_process_heap(), 0, fullname );
141 /* setup relay debugging entry points */
142 if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
146 /***********************************************************************
147 * BUILTIN32_LoadLibraryExA
149 * Partly copied from the original PE_ version.
152 NTSTATUS BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags, WINE_MODREF** pwm)
154 char dllname[20], *p;
155 LPCSTR name;
156 void *handle;
157 NTSTATUS nts;
159 /* Fix the name in case we have a full path and extension */
160 name = path;
161 if ((p = strrchr( name, '\\' ))) name = p + 1;
162 if ((p = strrchr( name, '/' ))) name = p + 1;
164 if (strlen(name) >= sizeof(dllname)-4) return STATUS_NO_SUCH_FILE;
166 strcpy( dllname, name );
167 p = strrchr( dllname, '.' );
168 if (!p) strcat( dllname, ".dll" );
169 for (p = dllname; *p; p++) *p = FILE_tolower(*p);
171 if ((nts = BUILTIN32_dlopen( dllname, &handle )) != STATUS_SUCCESS)
172 return nts;
174 if (!((*pwm) = MODULE_FindModule( path ))) *pwm = MODULE_FindModule( dllname );
175 if (!*pwm)
177 ERR( "loaded .so but dll %s still not found - 16-bit dll or version conflict.\n", dllname );
178 /* wine_dll_unload( handle );*/
179 return STATUS_INVALID_IMAGE_FORMAT;
181 (*pwm)->dlhandle = handle;
182 return STATUS_SUCCESS;
185 /***********************************************************************
186 * BUILTIN32_Init
188 * Initialize loading callbacks and return HMODULE of main exe.
189 * 'main' is the main exe in case it was already loaded from a PE file.
191 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
193 main_module = main;
194 last_status = STATUS_SUCCESS;
195 wine_dll_set_callback( load_library );
196 if (!main_module)
197 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
198 if (last_status != STATUS_SUCCESS)
199 MESSAGE( "Error while processing initial modules\n");
201 return main_module;