BUILTIN32_LoadLibraryExA: set larger size for internal buffer names.
[wine.git] / relay32 / builtin32.c
blobec7b8f66fcac2592f5dae5593baf7d39c6647318
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 <stdarg.h>
27 #include <stdio.h>
28 #include <ctype.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_MMAN_H
31 #include <sys/mman.h>
32 #endif
34 #include "ntstatus.h"
35 #include "windef.h"
36 #include "winbase.h"
37 #include "wine/winbase16.h"
38 #include "wine/library.h"
39 #include "module.h"
40 #include "file.h"
41 #include "ntdll_misc.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(module);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
48 static HMODULE main_module;
49 static NTSTATUS last_status; /* use to gather all errors in callback */
52 /***********************************************************************
53 * load_library
55 * Load a library in memory; callback function for wine_dll_register
57 static void load_library( void *base, const char *filename )
59 UNICODE_STRING wstr;
60 HMODULE module = (HMODULE)base, ret;
61 IMAGE_NT_HEADERS *nt;
62 WINE_MODREF *wm;
63 char *fullname;
64 DWORD len;
66 if (!base)
68 ERR("could not map image for %s\n", filename ? filename : "main exe" );
69 return;
71 if (!(nt = RtlImageNtHeader( module )))
73 ERR( "bad module for %s\n", filename ? filename : "main exe" );
74 last_status = STATUS_INVALID_IMAGE_FORMAT;
75 return;
78 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
80 /* if we already have an executable, ignore this one */
81 if (!main_module) main_module = module;
82 return; /* don't create the modref here, will be done later on */
85 RtlCreateUnicodeStringFromAsciiz(&wstr, filename);
86 if (LdrGetDllHandle(0, 0, &wstr, &ret) == STATUS_SUCCESS)
87 MESSAGE( "Warning: loading builtin %s, but native version already present. "
88 "Expect trouble.\n", filename );
89 RtlFreeUnicodeString( &wstr );
91 len = GetSystemDirectoryA( NULL, 0 );
92 if (!(fullname = RtlAllocateHeap( ntdll_get_process_heap(), 0, len + strlen(filename) + 1 )))
94 ERR( "can't load %s\n", filename );
95 last_status = STATUS_NO_MEMORY;
96 return;
98 GetSystemDirectoryA( fullname, len );
99 strcat( fullname, "\\" );
100 strcat( fullname, filename );
102 /* Create 32-bit MODREF */
103 if (!(wm = PE_CreateModule( module, fullname, 0, 0, TRUE )))
105 ERR( "can't load %s\n", filename );
106 RtlFreeHeap( ntdll_get_process_heap(), 0, fullname );
107 last_status = STATUS_NO_MEMORY;
108 return;
110 TRACE( "loaded %s %p %p\n", fullname, wm, module );
111 RtlFreeHeap( ntdll_get_process_heap(), 0, fullname );
113 /* setup relay debugging entry points */
114 if (TRACE_ON(relay)) RELAY_SetupDLL( (void *)module );
118 /***********************************************************************
119 * BUILTIN32_LoadLibraryExA
121 * Partly copied from the original PE_ version.
124 NTSTATUS BUILTIN32_LoadLibraryExA(LPCSTR path, DWORD flags, WINE_MODREF** pwm)
126 char error[256], dllname[MAX_PATH], *p;
127 int file_exists;
128 LPCSTR name;
129 void *handle;
131 /* Fix the name in case we have a full path and extension */
132 name = path;
133 if ((p = strrchr( name, '\\' ))) name = p + 1;
134 if ((p = strrchr( name, '/' ))) name = p + 1;
136 if (strlen(name) >= sizeof(dllname)-4) return STATUS_NO_SUCH_FILE;
138 strcpy( dllname, name );
139 p = strrchr( dllname, '.' );
140 if (!p) strcat( dllname, ".dll" );
141 for (p = dllname; *p; p++) *p = FILE_tolower(*p);
143 last_status = STATUS_SUCCESS;
144 /* load_library will modify last_status. Note also that load_library can be
145 * called several times, if the .so file we're loading has dependencies.
146 * last_status will gather all the errors we may get while loading all these
147 * libraries
149 if (!(handle = wine_dll_load( dllname, error, sizeof(error), &file_exists )))
151 if (!file_exists)
153 /* The file does not exist -> WARN() */
154 WARN("cannot open .so lib for builtin %s: %s\n", name, error);
155 return STATUS_NO_SUCH_FILE;
157 /* ERR() for all other errors (missing functions, ...) */
158 ERR("failed to load .so lib for builtin %s: %s\n", name, error );
159 return STATUS_PROCEDURE_NOT_FOUND;
161 if (last_status != STATUS_SUCCESS) return last_status;
163 if (!((*pwm) = MODULE_FindModule( path ))) *pwm = MODULE_FindModule( dllname );
164 if (!*pwm)
166 ERR( "loaded .so but dll %s still not found - 16-bit dll or version conflict.\n", dllname );
167 /* wine_dll_unload( handle );*/
168 return STATUS_INVALID_IMAGE_FORMAT;
170 (*pwm)->dlhandle = handle;
171 return STATUS_SUCCESS;
174 /***********************************************************************
175 * BUILTIN32_Init
177 * Initialize loading callbacks and return HMODULE of main exe.
178 * 'main' is the main exe in case it was already loaded from a PE file.
180 HMODULE BUILTIN32_LoadExeModule( HMODULE main )
182 main_module = main;
183 last_status = STATUS_SUCCESS;
184 wine_dll_set_callback( load_library );
185 if (!main_module)
186 MESSAGE( "No built-in EXE module loaded! Did you create a .spec file?\n" );
187 if (last_status != STATUS_SUCCESS)
188 MESSAGE( "Error while processing initial modules\n");
190 return main_module;