4 * Copyright 1995 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>
32 #include "wine/winbase16.h"
40 #include "kernel_private.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
44 #include "wine/server.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(module);
47 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
50 /****************************************************************************
51 * DisableThreadLibraryCalls (KERNEL32.@)
53 * Inform the module loader that thread notifications are not required for a dll.
56 * hModule [I] Module handle to skip calls for
59 * Success: TRUE. Thread attach and detach notifications will not be sent
61 * Failure: FALSE. Use GetLastError() to determine the cause.
64 * This is typically called from the dll entry point of a dll during process
65 * attachment, for dlls that do not need to process thread notifications.
67 BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
69 NTSTATUS nts = LdrDisableThreadCalloutsForDll( hModule );
70 if (nts == STATUS_SUCCESS) return TRUE;
72 SetLastError( RtlNtStatusToDosError( nts ) );
77 /* Check whether a file is an OS/2 or a very old Windows executable
78 * by testing on import of KERNEL.
80 * FIXME: is reading the module imports the only way of discerning
81 * old Windows binaries from OS/2 ones ? At least it seems so...
83 static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz,
84 const IMAGE_OS2_HEADER *ne)
86 DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
87 enum binary_type ret = BINARY_OS216;
93 /* read modref table */
94 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
95 || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
96 || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
97 || (len != ne->ne_cmod*sizeof(WORD)) )
100 /* read imported names table */
101 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
102 || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
103 || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
104 || (len != ne->ne_enttab - ne->ne_imptab) )
107 for (i=0; i < ne->ne_cmod; i++)
109 LPSTR module = &nametab[modtab[i]];
110 TRACE("modref: %.*s\n", module[0], &module[1]);
111 if (!(strncmp(&module[1], "KERNEL", module[0])))
112 { /* very old Windows file */
113 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
120 ERR("Hmm, an error occurred. Is this binary file broken?\n");
123 HeapFree( GetProcessHeap(), 0, modtab);
124 HeapFree( GetProcessHeap(), 0, nametab);
125 SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
129 /***********************************************************************
130 * MODULE_GetBinaryType
132 enum binary_type MODULE_GetBinaryType( HANDLE hfile, void **res_start, void **res_end )
138 unsigned char magic[4];
139 unsigned char ignored[12];
145 unsigned long cputype;
146 unsigned long cpusubtype;
147 unsigned long filetype;
154 /* Seek to the start of the file and read the header information. */
155 if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1)
156 return BINARY_UNKNOWN;
157 if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header))
158 return BINARY_UNKNOWN;
160 if (!memcmp( header.elf.magic, "\177ELF", 4 ))
162 /* FIXME: we don't bother to check byte order, architecture, etc. */
163 switch(header.elf.type)
165 case 2: return BINARY_UNIX_EXE;
166 case 3: return BINARY_UNIX_LIB;
168 return BINARY_UNKNOWN;
171 /* Mach-o File with Endian set to Big Endian or Little Endian */
172 if (header.macho.magic == 0xfeedface || header.macho.magic == 0xecafdeef)
174 switch(header.macho.filetype)
176 case 0x8: /* MH_BUNDLE */ return BINARY_UNIX_LIB;
178 return BINARY_UNKNOWN;
181 /* Not ELF, try DOS */
183 if (header.mz.e_magic == IMAGE_DOS_SIGNATURE)
187 IMAGE_OS2_HEADER os2;
191 /* We do have a DOS image so we will now try to seek into
192 * the file by the amount indicated by the field
193 * "Offset to extended header" and read in the
194 * "magic" field information at that location.
195 * This will tell us if there is more header information
198 if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
200 if (!ReadFile( hfile, &ext_header, sizeof(ext_header), &len, NULL ) || len < 4)
203 /* Reading the magic field succeeded so
204 * we will try to determine what type it is.
206 if (!memcmp( &ext_header.nt.Signature, "PE\0\0", 4 ))
208 if (len >= sizeof(ext_header.nt.FileHeader))
210 if (len < sizeof(ext_header.nt)) /* clear remaining part of header if missing */
211 memset( (char *)&ext_header.nt + len, 0, sizeof(ext_header.nt) - len );
212 if (res_start) *res_start = (void *)ext_header.nt.OptionalHeader.ImageBase;
213 if (res_end) *res_end = (void *)(ext_header.nt.OptionalHeader.ImageBase +
214 ext_header.nt.OptionalHeader.SizeOfImage);
215 if (ext_header.nt.FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
216 return BINARY_PE_EXE;
221 if (!memcmp( &ext_header.os2.ne_magic, "NE", 2 ))
223 /* This is a Windows executable (NE) header. This can
224 * mean either a 16-bit OS/2 or a 16-bit Windows or even a
225 * DOS program (running under a DOS extender). To decide
226 * which, we'll have to read the NE header.
228 if (len >= sizeof(ext_header.os2))
230 switch ( ext_header.os2.ne_exetyp )
232 case 1: return BINARY_OS216; /* OS/2 */
233 case 2: return BINARY_WIN16; /* Windows */
234 case 3: return BINARY_DOS; /* European MS-DOS 4.x */
235 case 4: return BINARY_WIN16; /* Windows 386; FIXME: is this 32bit??? */
236 case 5: return BINARY_DOS; /* BOSS, Borland Operating System Services */
237 /* other types, e.g. 0 is: "unknown" */
238 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ext_header.os2);
241 /* Couldn't read header, so abort. */
245 /* Unknown extended header, but this file is nonetheless DOS-executable. */
249 return BINARY_UNKNOWN;
252 /***********************************************************************
253 * GetBinaryTypeW [KERNEL32.@]
255 * Determine whether a file is executable, and if so, what kind.
258 * lpApplicationName [I] Path of the file to check
259 * lpBinaryType [O] Destination for the binary type
262 * TRUE, if the file is an executable, in which case lpBinaryType is set.
263 * FALSE, if the file is not an executable or if the function fails.
266 * The type of executable is a property that determines which subsytem an
267 * executable file runs under. lpBinaryType can be set to one of the following
269 * SCS_32BIT_BINARY: A Win32 based application
270 * SCS_DOS_BINARY: An MS-Dos based application
271 * SCS_WOW_BINARY: A Win16 based application
272 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
273 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
274 * SCS_OS216_BINARY: A 16bit OS/2 based application
276 * To find the binary type, this function reads in the files header information.
277 * If extended header information is not present it will assume that the file
278 * is a DOS executable. If extended header information is present it will
279 * determine if the file is a 16 or 32 bit Windows executable by checking the
280 * flags in the header.
282 * ".com" and ".pif" files are only recognized by their file name extension,
283 * as per native Windows.
285 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
290 TRACE("%s\n", debugstr_w(lpApplicationName) );
294 if ( lpApplicationName == NULL || lpBinaryType == NULL )
297 /* Open the file indicated by lpApplicationName for reading.
299 hfile = CreateFileW( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
300 NULL, OPEN_EXISTING, 0, 0 );
301 if ( hfile == INVALID_HANDLE_VALUE )
306 switch(MODULE_GetBinaryType( hfile, NULL, NULL ))
310 static const WCHAR comW[] = { '.','C','O','M',0 };
311 static const WCHAR pifW[] = { '.','P','I','F',0 };
314 /* try to determine from file name */
315 ptr = strrchrW( lpApplicationName, '.' );
317 if (!strcmpiW( ptr, comW ))
319 *lpBinaryType = SCS_DOS_BINARY;
322 else if (!strcmpiW( ptr, pifW ))
324 *lpBinaryType = SCS_PIF_BINARY;
331 *lpBinaryType = SCS_32BIT_BINARY;
335 *lpBinaryType = SCS_WOW_BINARY;
339 *lpBinaryType = SCS_OS216_BINARY;
343 *lpBinaryType = SCS_DOS_BINARY;
346 case BINARY_UNIX_EXE:
347 case BINARY_UNIX_LIB:
352 CloseHandle( hfile );
356 /***********************************************************************
357 * GetBinaryTypeA [KERNEL32.@]
358 * GetBinaryType [KERNEL32.@]
360 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
362 ANSI_STRING app_nameA;
365 TRACE("%s\n", debugstr_a(lpApplicationName));
369 if ( lpApplicationName == NULL || lpBinaryType == NULL )
372 RtlInitAnsiString(&app_nameA, lpApplicationName);
373 status = RtlAnsiStringToUnicodeString(&NtCurrentTeb()->StaticUnicodeString,
376 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType);
378 SetLastError(RtlNtStatusToDosError(status));
383 /***********************************************************************
384 * GetModuleHandleA (KERNEL32.@)
385 * GetModuleHandle32 (KERNEL.488)
387 * Get the handle of a dll loaded into the process address space.
390 * module [I] Name of the dll
393 * Success: A handle to the loaded dll.
394 * Failure: A NULL handle. Use GetLastError() to determine the cause.
396 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
400 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
401 if (!(moduleW = FILE_name_AtoW( module, FALSE ))) return 0;
402 return GetModuleHandleW( moduleW );
405 /***********************************************************************
406 * GetModuleHandleW (KERNEL32.@)
408 * Unicode version of GetModuleHandleA.
410 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
416 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
418 RtlInitUnicodeString( &wstr, module );
419 nts = LdrGetDllHandle( 0, 0, &wstr, &ret);
420 if (nts != STATUS_SUCCESS)
422 SetLastError( RtlNtStatusToDosError( nts ) );
429 /***********************************************************************
430 * GetModuleFileNameA (KERNEL32.@)
431 * GetModuleFileName32 (KERNEL.487)
433 * Get the file name of a loaded module from its handle.
436 * Success: The length of the file name, excluding the terminating NUL.
437 * Failure: 0. Use GetLastError() to determine the cause.
440 * This function always returns the long path of hModule (as opposed to
441 * GetModuleFileName16() which returns short paths when the modules version
443 * The function doesn't write a terminating '\0' if the buffer is too
446 DWORD WINAPI GetModuleFileNameA(
447 HMODULE hModule, /* [in] Module handle (32 bit) */
448 LPSTR lpFileName, /* [out] Destination for file name */
449 DWORD size ) /* [in] Size of lpFileName in characters */
451 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
456 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
459 if ((len = GetModuleFileNameW( hModule, filenameW, size )))
461 len = FILE_name_WtoA( filenameW, len, lpFileName, size );
462 if (len < size) lpFileName[len] = '\0';
464 HeapFree( GetProcessHeap(), 0, filenameW );
468 /***********************************************************************
469 * GetModuleFileNameW (KERNEL32.@)
471 * Unicode version of GetModuleFileNameA.
473 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
475 ULONG magic, len = 0;
478 WIN16_SUBSYSTEM_TIB *win16_tib;
480 if (!hModule && ((win16_tib = NtCurrentTeb()->Tib.SubSystemTib)) && win16_tib->exe_name)
482 len = min(size, win16_tib->exe_name->Length / sizeof(WCHAR));
483 memcpy( lpFileName, win16_tib->exe_name->Buffer, len * sizeof(WCHAR) );
484 if (len < size) lpFileName[len] = '\0';
488 LdrLockLoaderLock( 0, NULL, &magic );
490 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
491 nts = LdrFindEntryForAddress( hModule, &pldr );
492 if (nts == STATUS_SUCCESS)
494 len = min(size, pldr->FullDllName.Length / sizeof(WCHAR));
495 memcpy(lpFileName, pldr->FullDllName.Buffer, len * sizeof(WCHAR));
496 if (len < size) lpFileName[len] = '\0';
498 else SetLastError( RtlNtStatusToDosError( nts ) );
500 LdrUnlockLoaderLock( 0, magic );
502 TRACE( "%s\n", debugstr_wn(lpFileName, len) );
507 /***********************************************************************
508 * get_dll_system_path
510 static const WCHAR *get_dll_system_path(void)
512 static WCHAR *cached_path;
519 len += GetSystemDirectoryW( NULL, 0 );
520 len += GetWindowsDirectoryW( NULL, 0 );
521 p = path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
524 GetSystemDirectoryW( p, path + len - p);
527 GetWindowsDirectoryW( p, path + len - p);
534 /******************************************************************
535 * MODULE_get_dll_load_path
537 * Compute the load path to use for a given dll.
538 * Returned pointer must be freed by caller.
540 WCHAR *MODULE_get_dll_load_path( LPCWSTR module )
542 static const WCHAR pathW[] = {'P','A','T','H',0};
544 const WCHAR *system_path = get_dll_system_path();
545 const WCHAR *mod_end = NULL;
546 UNICODE_STRING name, value;
548 int len = 0, path_len = 0;
550 /* adjust length for module name */
552 if (!module) module = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer;
556 if ((p = strrchrW( mod_end, '\\' ))) mod_end = p;
557 if ((p = strrchrW( mod_end, '/' ))) mod_end = p;
558 if (mod_end == module + 2 && module[1] == ':') mod_end++;
559 if (mod_end == module && module[0] && module[1] == ':') mod_end += 2;
560 len += (mod_end - module) + 1;
562 len += strlenW( system_path ) + 2;
564 /* get the PATH variable */
566 RtlInitUnicodeString( &name, pathW );
568 value.MaximumLength = 0;
570 if (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
571 path_len = value.Length;
573 if (!(ret = HeapAlloc( GetProcessHeap(), 0, path_len + len * sizeof(WCHAR) ))) return NULL;
577 memcpy( ret, module, (mod_end - module) * sizeof(WCHAR) );
578 p += (mod_end - module);
581 strcpyW( p, system_path );
585 value.MaximumLength = path_len;
587 while (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
591 /* grow the buffer and retry */
592 path_len = value.Length;
593 if (!(new_ptr = HeapReAlloc( GetProcessHeap(), 0, ret, path_len + len * sizeof(WCHAR) )))
595 HeapFree( GetProcessHeap(), 0, ret );
598 value.Buffer = new_ptr + (value.Buffer - ret);
599 value.MaximumLength = path_len;
602 value.Buffer[value.Length / sizeof(WCHAR)] = 0;
607 /******************************************************************
608 * load_library_as_datafile
610 static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod)
612 static const WCHAR dotDLL[] = {'.','d','l','l',0};
614 WCHAR filenameW[MAX_PATH];
615 HANDLE hFile = INVALID_HANDLE_VALUE;
621 if (SearchPathW( NULL, (LPCWSTR)name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]),
624 hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ,
625 NULL, OPEN_EXISTING, 0, 0 );
627 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
629 mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
630 CloseHandle( hFile );
631 if (!mapping) return FALSE;
633 module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
634 CloseHandle( mapping );
635 if (!module) return FALSE;
637 /* make sure it's a valid PE file */
638 if (!RtlImageNtHeader(module))
640 UnmapViewOfFile( module );
643 *hmod = (HMODULE)((char *)module + 1); /* set low bit of handle to indicate datafile module */
648 /******************************************************************
651 * Helper for LoadLibraryExA/W.
653 static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags )
659 if (flags & LOAD_LIBRARY_AS_DATAFILE)
661 /* The method in load_library_as_datafile allows searching for the
662 * 'native' libraries only
664 if (load_library_as_datafile( libname->Buffer, &hModule )) return hModule;
665 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
666 /* Fallback to normal behaviour */
669 load_path = MODULE_get_dll_load_path( flags & LOAD_WITH_ALTERED_SEARCH_PATH ? libname->Buffer : NULL );
670 nts = LdrLoadDll( load_path, flags, libname, &hModule );
671 HeapFree( GetProcessHeap(), 0, load_path );
672 if (nts != STATUS_SUCCESS)
675 SetLastError( RtlNtStatusToDosError( nts ) );
681 /******************************************************************
682 * LoadLibraryExA (KERNEL32.@)
684 * Load a dll file into the process address space.
687 * libname [I] Name of the file to load
688 * hfile [I] Reserved, must be 0.
689 * flags [I] Flags for loading the dll
692 * Success: A handle to the loaded dll.
693 * Failure: A NULL handle. Use GetLastError() to determine the cause.
696 * The HFILE parameter is not used and marked reserved in the SDK. I can
697 * only guess that it should force a file to be mapped, but I rather
698 * ignore the parameter because it would be extremely difficult to
699 * integrate this with different types of module representations.
701 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
705 if (!(libnameW = FILE_name_AtoW( libname, FALSE ))) return 0;
706 return LoadLibraryExW( libnameW, hfile, flags );
709 /***********************************************************************
710 * LoadLibraryExW (KERNEL32.@)
712 * Unicode version of LoadLibraryExA.
714 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags)
720 SetLastError(ERROR_INVALID_PARAMETER);
723 RtlInitUnicodeString( &wstr, libnameW );
724 return load_library( &wstr, flags );
727 /***********************************************************************
728 * LoadLibraryA (KERNEL32.@)
730 * Load a dll file into the process address space.
733 * libname [I] Name of the file to load
736 * Success: A handle to the loaded dll.
737 * Failure: A NULL handle. Use GetLastError() to determine the cause.
740 * See LoadLibraryExA().
742 HMODULE WINAPI LoadLibraryA(LPCSTR libname)
744 return LoadLibraryExA(libname, 0, 0);
747 /***********************************************************************
748 * LoadLibraryW (KERNEL32.@)
750 * Unicode version of LoadLibraryA.
752 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
754 return LoadLibraryExW(libnameW, 0, 0);
757 /***********************************************************************
758 * FreeLibrary (KERNEL32.@)
759 * FreeLibrary32 (KERNEL.486)
761 * Free a dll loaded into the process address space.
764 * hLibModule [I] Handle to the dll returned by LoadLibraryA().
767 * Success: TRUE. The dll is removed if it is not still in use.
768 * Failure: FALSE. Use GetLastError() to determine the cause.
770 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
777 SetLastError( ERROR_INVALID_HANDLE );
781 if ((ULONG_PTR)hLibModule & 1)
783 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
784 char *ptr = (char *)hLibModule - 1;
785 UnmapViewOfFile( ptr );
789 if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE;
790 else SetLastError( RtlNtStatusToDosError( nts ) );
795 /***********************************************************************
796 * GetProcAddress (KERNEL32.@)
798 * Find the address of an exported symbol in a loaded dll.
801 * hModule [I] Handle to the dll returned by LoadLibraryA().
802 * function [I] Name of the symbol, or an integer ordinal number < 16384
805 * Success: A pointer to the symbol in the process address space.
806 * Failure: NULL. Use GetLastError() to determine the cause.
808 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
813 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
815 if (HIWORD(function))
819 RtlInitAnsiString( &str, function );
820 nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp );
823 nts = LdrGetProcedureAddress( hModule, NULL, (DWORD)function, (void**)&fp );
824 if (nts != STATUS_SUCCESS)
826 SetLastError( RtlNtStatusToDosError( nts ) );
832 /***********************************************************************
833 * GetProcAddress32 (KERNEL.453)
835 * Find the address of an exported symbol in a loaded dll.
838 * hModule [I] Handle to the dll returned by LoadLibraryA().
839 * function [I] Name of the symbol, or an integer ordinal number < 16384
842 * Success: A pointer to the symbol in the process address space.
843 * Failure: NULL. Use GetLastError() to determine the cause.
845 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
847 /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */
848 return GetProcAddress( hModule, function );