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 #ifdef HAVE_SYS_TYPES_H
29 # include <sys/types.h>
34 #include "wine/winbase16.h"
44 #include "wine/debug.h"
45 #include "wine/unicode.h"
46 #include "wine/server.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(module);
49 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
52 /****************************************************************************
53 * DisableThreadLibraryCalls (KERNEL32.@)
55 * Inform the module loader that thread notifications are not required for a dll.
58 * hModule [I] Module handle to skip calls for
61 * Success: TRUE. Thread attach and detach notifications will not be sent
63 * Failure: FALSE. Use GetLastError() to determine the cause.
66 * This is typically called from the dll entry point of a dll during process
67 * attachment, for dlls that do not need to process thread notifications.
69 BOOL WINAPI DisableThreadLibraryCalls( HMODULE hModule )
71 NTSTATUS nts = LdrDisableThreadCalloutsForDll( hModule );
72 if (nts == STATUS_SUCCESS) return TRUE;
74 SetLastError( RtlNtStatusToDosError( nts ) );
79 /* Check whether a file is an OS/2 or a very old Windows executable
80 * by testing on import of KERNEL.
82 * FIXME: is reading the module imports the only way of discerning
83 * old Windows binaries from OS/2 ones ? At least it seems so...
85 static enum binary_type MODULE_Decide_OS2_OldWin(HANDLE hfile, const IMAGE_DOS_HEADER *mz,
86 const IMAGE_OS2_HEADER *ne)
88 DWORD currpos = SetFilePointer( hfile, 0, NULL, SEEK_CUR);
89 enum binary_type ret = BINARY_OS216;
95 /* read modref table */
96 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_modtab, NULL, SEEK_SET ) == -1)
97 || (!(modtab = HeapAlloc( GetProcessHeap(), 0, ne->ne_cmod*sizeof(WORD))))
98 || (!(ReadFile(hfile, modtab, ne->ne_cmod*sizeof(WORD), &len, NULL)))
99 || (len != ne->ne_cmod*sizeof(WORD)) )
102 /* read imported names table */
103 if ( (SetFilePointer( hfile, mz->e_lfanew + ne->ne_imptab, NULL, SEEK_SET ) == -1)
104 || (!(nametab = HeapAlloc( GetProcessHeap(), 0, ne->ne_enttab - ne->ne_imptab)))
105 || (!(ReadFile(hfile, nametab, ne->ne_enttab - ne->ne_imptab, &len, NULL)))
106 || (len != ne->ne_enttab - ne->ne_imptab) )
109 for (i=0; i < ne->ne_cmod; i++)
111 LPSTR module = &nametab[modtab[i]];
112 TRACE("modref: %.*s\n", module[0], &module[1]);
113 if (!(strncmp(&module[1], "KERNEL", module[0])))
114 { /* very old Windows file */
115 MESSAGE("This seems to be a very old (pre-3.0) Windows executable. Expect crashes, especially if this is a real-mode binary !\n");
122 ERR("Hmm, an error occurred. Is this binary file broken ?\n");
125 HeapFree( GetProcessHeap(), 0, modtab);
126 HeapFree( GetProcessHeap(), 0, nametab);
127 SetFilePointer( hfile, currpos, NULL, SEEK_SET); /* restore filepos */
131 /***********************************************************************
132 * MODULE_GetBinaryType
134 enum binary_type MODULE_GetBinaryType( HANDLE hfile )
140 unsigned char magic[4];
141 unsigned char ignored[12];
147 unsigned long cputype;
148 unsigned long cpusubtype;
149 unsigned long filetype;
157 /* Seek to the start of the file and read the header information. */
158 if (SetFilePointer( hfile, 0, NULL, SEEK_SET ) == -1)
159 return BINARY_UNKNOWN;
160 if (!ReadFile( hfile, &header, sizeof(header), &len, NULL ) || len != sizeof(header))
161 return BINARY_UNKNOWN;
163 if (!memcmp( header.elf.magic, "\177ELF", 4 ))
165 /* FIXME: we don't bother to check byte order, architecture, etc. */
166 switch(header.elf.type)
168 case 2: return BINARY_UNIX_EXE;
169 case 3: return BINARY_UNIX_LIB;
171 return BINARY_UNKNOWN;
174 /* Mach-o File with Endian set to Big Endian or Little Endian*/
175 if (header.macho.magic == 0xfeedface || header.macho.magic == 0xecafdeef)
177 switch(header.macho.filetype)
179 case 0x8: /* MH_BUNDLE */ return BINARY_UNIX_LIB;
181 return BINARY_UNKNOWN;
184 /* Not ELF, try DOS */
186 if (header.mz.e_magic == IMAGE_DOS_SIGNATURE)
188 /* We do have a DOS image so we will now try to seek into
189 * the file by the amount indicated by the field
190 * "Offset to extended header" and read in the
191 * "magic" field information at that location.
192 * This will tell us if there is more header information
195 /* But before we do we will make sure that header
196 * structure encompasses the "Offset to extended header"
199 if (header.mz.e_lfanew < sizeof(IMAGE_DOS_HEADER))
201 if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
203 if (!ReadFile( hfile, magic, sizeof(magic), &len, NULL ) || len != sizeof(magic))
206 /* Reading the magic field succeeded so
207 * we will try to determine what type it is.
209 if (!memcmp( magic, "PE\0\0", 4 ))
211 IMAGE_FILE_HEADER FileHeader;
213 if (ReadFile( hfile, &FileHeader, sizeof(FileHeader), &len, NULL ) && len == sizeof(FileHeader))
215 if (FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
216 return BINARY_PE_EXE;
221 if (!memcmp( 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.
229 if ( SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) != -1
230 && ReadFile( hfile, &ne, sizeof(ne), &len, NULL )
231 && len == sizeof(ne) )
233 switch ( ne.ne_exetyp )
235 case 2: return BINARY_WIN16;
236 case 5: return BINARY_DOS;
237 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ne);
240 /* Couldn't read header, so abort. */
244 /* Unknown extended header, but this file is nonetheless DOS-executable. */
248 return BINARY_UNKNOWN;
251 /***********************************************************************
252 * GetBinaryTypeW [KERNEL32.@]
254 * Determine whether a file is executable, and if so, what kind.
257 * lpApplicationName [I] Path of the file to check
258 * lpBinaryType [O] Destination for the binary type
261 * TRUE, if the file is an executable, in which case lpBinaryType is set.
262 * FALSE, if the file is not an executable or if the function fails.
265 * The type of executable is a property that determines which subsytem an
266 * executable file runs under. lpBinaryType can be set to one of the following
268 * SCS_32BIT_BINARY: A Win32 based application
269 * SCS_DOS_BINARY: An MS-Dos based application
270 * SCS_WOW_BINARY: A Win16 based application
271 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
272 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
273 * SCS_OS216_BINARY: A 16bit OS/2 based application
275 * To find the binary type, this function reads in the files header information.
276 * If extended header information is not present it will assume that the file
277 * is a DOS executable. If extended header information is present it will
278 * determine if the file is a 16 or 32 bit Windows executable by checking the
279 * flags in the header.
281 * ".com" and ".pif" files are only recognized by their file name extension,
282 * as per native Windows.
284 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
289 TRACE("%s\n", debugstr_w(lpApplicationName) );
293 if ( lpApplicationName == NULL || lpBinaryType == NULL )
296 /* Open the file indicated by lpApplicationName for reading.
298 hfile = CreateFileW( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
299 NULL, OPEN_EXISTING, 0, 0 );
300 if ( hfile == INVALID_HANDLE_VALUE )
305 switch(MODULE_GetBinaryType( hfile ))
309 static const WCHAR comW[] = { '.','C','O','M',0 };
310 static const WCHAR pifW[] = { '.','P','I','F',0 };
313 /* try to determine from file name */
314 ptr = strrchrW( lpApplicationName, '.' );
316 if (!strcmpiW( ptr, comW ))
318 *lpBinaryType = SCS_DOS_BINARY;
321 else if (!strcmpiW( ptr, pifW ))
323 *lpBinaryType = SCS_PIF_BINARY;
330 *lpBinaryType = SCS_32BIT_BINARY;
334 *lpBinaryType = SCS_WOW_BINARY;
338 *lpBinaryType = SCS_OS216_BINARY;
342 *lpBinaryType = SCS_DOS_BINARY;
345 case BINARY_UNIX_EXE:
346 case BINARY_UNIX_LIB:
351 CloseHandle( hfile );
355 /***********************************************************************
356 * GetBinaryTypeA [KERNEL32.@]
357 * GetBinaryType [KERNEL32.@]
359 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
361 ANSI_STRING app_nameA;
364 TRACE("%s\n", debugstr_a(lpApplicationName));
368 if ( lpApplicationName == NULL || lpBinaryType == NULL )
371 RtlInitAnsiString(&app_nameA, lpApplicationName);
372 status = RtlAnsiStringToUnicodeString(&NtCurrentTeb()->StaticUnicodeString,
375 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType);
377 SetLastError(RtlNtStatusToDosError(status));
382 /***********************************************************************
383 * GetModuleHandleA (KERNEL32.@)
384 * GetModuleHandle32 (KERNEL.488)
386 * Get the handle of a dll loaded into the process address space.
389 * module [I] Name of the dll
392 * Success: A handle to the loaded dll.
393 * Failure: A NULL handle. Use GetLastError() to determine the cause.
395 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
401 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
403 RtlCreateUnicodeStringFromAsciiz(&wstr, module);
404 nts = LdrGetDllHandle(0, 0, &wstr, &ret);
405 RtlFreeUnicodeString( &wstr );
406 if (nts != STATUS_SUCCESS)
409 SetLastError( RtlNtStatusToDosError( nts ) );
414 /***********************************************************************
415 * GetModuleHandleW (KERNEL32.@)
417 * Unicode version of GetModuleHandleA.
419 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
425 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
427 RtlInitUnicodeString( &wstr, module );
428 nts = LdrGetDllHandle( 0, 0, &wstr, &ret);
429 if (nts != STATUS_SUCCESS)
431 SetLastError( RtlNtStatusToDosError( nts ) );
438 /***********************************************************************
439 * GetModuleFileNameA (KERNEL32.@)
440 * GetModuleFileName32 (KERNEL.487)
442 * Get the file name of a loaded module from its handle.
445 * Success: The length of the file name, excluding the terminating NUL.
446 * Failure: 0. Use GetLastError() to determine the cause.
449 * This function always returns the long path of hModule (as opposed to
450 * GetModuleFileName16() which returns short paths when the modules version
453 DWORD WINAPI GetModuleFileNameA(
454 HMODULE hModule, /* [in] Module handle (32 bit) */
455 LPSTR lpFileName, /* [out] Destination for file name */
456 DWORD size ) /* [in] Size of lpFileName in characters */
458 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
462 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
465 GetModuleFileNameW( hModule, filenameW, size );
466 WideCharToMultiByte( CP_ACP, 0, filenameW, -1, lpFileName, size, NULL, NULL );
467 HeapFree( GetProcessHeap(), 0, filenameW );
468 return strlen( lpFileName );
471 /***********************************************************************
472 * GetModuleFileNameW (KERNEL32.@)
474 * Unicode version of GetModuleFileNameA.
476 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
482 LdrLockLoaderLock( 0, NULL, &magic );
483 if (!hModule && !(NtCurrentTeb()->tibflags & TEBF_WIN32))
485 /* 16-bit task - get current NE module name */
486 NE_MODULE *pModule = NE_GetPtr( GetCurrentTask() );
489 WCHAR path[MAX_PATH];
491 MultiByteToWideChar( CP_ACP, 0, NE_MODULE_NAME(pModule), -1, path, MAX_PATH );
492 GetLongPathNameW(path, lpFileName, size);
500 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
501 nts = LdrFindEntryForAddress( hModule, &pldr );
502 if (nts == STATUS_SUCCESS) lstrcpynW(lpFileName, pldr->FullDllName.Buffer, size);
503 else SetLastError( RtlNtStatusToDosError( nts ) );
506 LdrUnlockLoaderLock( 0, magic );
508 TRACE( "%s\n", debugstr_w(lpFileName) );
509 return strlenW(lpFileName);
513 /***********************************************************************
514 * get_dll_system_path
516 static const WCHAR *get_dll_system_path(void)
525 exe_name = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer;
526 if (!(p = strrchrW( exe_name, '\\' ))) p = exe_name;
527 /* include trailing backslash only on drive root */
528 if (p == exe_name + 2 && exe_name[1] == ':') p++;
530 len += GetSystemDirectoryW( NULL, 0 );
531 len += GetWindowsDirectoryW( NULL, 0 );
532 path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
533 memcpy( path, exe_name, (p - exe_name) * sizeof(WCHAR) );
534 p = path + (p - exe_name);
538 GetSystemDirectoryW( p, path + len - p);
541 GetWindowsDirectoryW( p, path + len - p);
547 /******************************************************************
550 * Compute the load path to use for a given dll.
551 * Returned pointer must be freed by caller.
553 static WCHAR *get_dll_load_path( LPCWSTR module )
555 static const WCHAR pathW[] = {'P','A','T','H',0};
557 const WCHAR *system_path = get_dll_system_path();
558 const WCHAR *mod_end = NULL;
559 UNICODE_STRING name, value;
561 int len = 0, path_len = 0;
563 /* adjust length for module name */
568 if ((p = strrchrW( mod_end, '\\' ))) mod_end = p;
569 if ((p = strrchrW( mod_end, '/' ))) mod_end = p;
570 if (mod_end == module + 2 && module[1] == ':') mod_end++;
571 if (mod_end == module && module[0] && module[1] == ':') mod_end += 2;
572 len += (mod_end - module);
573 system_path = strchrW( system_path, ';' );
575 len += strlenW( system_path ) + 2;
577 /* get the PATH variable */
579 RtlInitUnicodeString( &name, pathW );
581 value.MaximumLength = 0;
583 if (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
584 path_len = value.Length;
586 if (!(ret = HeapAlloc( GetProcessHeap(), 0, path_len + len * sizeof(WCHAR) ))) return NULL;
590 memcpy( ret, module, (mod_end - module) * sizeof(WCHAR) );
591 p += (mod_end - module);
593 strcpyW( p, system_path );
597 value.MaximumLength = path_len;
599 while (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
603 /* grow the buffer and retry */
604 path_len = value.Length;
605 if (!(new_ptr = HeapReAlloc( GetProcessHeap(), 0, ret, path_len + len * sizeof(WCHAR) )))
607 HeapFree( GetProcessHeap(), 0, ret );
610 value.Buffer = new_ptr + (value.Buffer - ret);
611 value.MaximumLength = path_len;
614 value.Buffer[value.Length / sizeof(WCHAR)] = 0;
619 /******************************************************************
620 * MODULE_InitLoadPath
622 * Create the initial dll load path.
624 void MODULE_InitLoadPath(void)
626 WCHAR *path = get_dll_load_path( NULL );
627 RtlInitUnicodeString( &NtCurrentTeb()->Peb->ProcessParameters->DllPath, path );
631 /******************************************************************
632 * load_library_as_datafile
634 static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod)
636 static const WCHAR dotDLL[] = {'.','d','l','l',0};
638 WCHAR filenameW[MAX_PATH];
639 HANDLE hFile = INVALID_HANDLE_VALUE;
645 if (SearchPathW( NULL, (LPCWSTR)name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]),
648 hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ,
649 NULL, OPEN_EXISTING, 0, 0 );
651 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
653 mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
654 CloseHandle( hFile );
655 if (!mapping) return FALSE;
657 module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
658 CloseHandle( mapping );
659 if (!module) return FALSE;
661 /* make sure it's a valid PE file */
662 if (!RtlImageNtHeader(module))
664 UnmapViewOfFile( module );
667 *hmod = (HMODULE)((char *)module + 1); /* set low bit of handle to indicate datafile module */
672 /******************************************************************
675 * Helper for LoadLibraryExA/W.
677 static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags )
683 if (flags & LOAD_LIBRARY_AS_DATAFILE)
685 /* The method in load_library_as_datafile allows searching for the
686 * 'native' libraries only
688 if (load_library_as_datafile( libname->Buffer, &hModule )) return hModule;
689 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
690 /* Fallback to normal behaviour */
693 load_path = get_dll_load_path( flags & LOAD_WITH_ALTERED_SEARCH_PATH ? libname->Buffer : NULL );
694 nts = LdrLoadDll( load_path, flags, libname, &hModule );
695 HeapFree( GetProcessHeap(), 0, load_path );
696 if (nts != STATUS_SUCCESS)
699 SetLastError( RtlNtStatusToDosError( nts ) );
705 /******************************************************************
706 * LoadLibraryExA (KERNEL32.@)
708 * Load a dll file into the process address space.
711 * libname [I] Name of the file to load
712 * hfile [I] Reserved, must be 0.
713 * flags [I] Flags for loading the dll
716 * Success: A handle to the loaded dll.
717 * Failure: A NULL handle. Use GetLastError() to determine the cause.
720 * The HFILE parameter is not used and marked reserved in the SDK. I can
721 * only guess that it should force a file to be mapped, but I rather
722 * ignore the parameter because it would be extremely difficult to
723 * integrate this with different types of module representations.
725 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
732 SetLastError(ERROR_INVALID_PARAMETER);
735 RtlCreateUnicodeStringFromAsciiz( &wstr, libname );
736 hModule = load_library( &wstr, flags );
737 RtlFreeUnicodeString( &wstr );
741 /***********************************************************************
742 * LoadLibraryExW (KERNEL32.@)
744 * Unicode version of LoadLibraryExA.
746 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags)
752 SetLastError(ERROR_INVALID_PARAMETER);
755 RtlInitUnicodeString( &wstr, libnameW );
756 return load_library( &wstr, flags );
759 /***********************************************************************
760 * LoadLibraryA (KERNEL32.@)
762 * Load a dll file into the process address space.
765 * libname [I] Name of the file to load
768 * Success: A handle to the loaded dll.
769 * Failure: A NULL handle. Use GetLastError() to determine the cause.
772 * See LoadLibraryExA().
774 HMODULE WINAPI LoadLibraryA(LPCSTR libname)
776 return LoadLibraryExA(libname, 0, 0);
779 /***********************************************************************
780 * LoadLibraryW (KERNEL32.@)
782 * Unicode version of LoadLibraryA.
784 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
786 return LoadLibraryExW(libnameW, 0, 0);
789 /***********************************************************************
790 * FreeLibrary (KERNEL32.@)
791 * FreeLibrary32 (KERNEL.486)
793 * Free a dll loaded into the process address space.
796 * hLibModule [I] Handle to the dll returned by LoadLibraryA().
799 * Success: TRUE. The dll is removed if it is not still in use.
800 * Failure: FALSE. Use GetLastError() to determine the cause.
802 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
809 SetLastError( ERROR_INVALID_HANDLE );
813 if ((ULONG_PTR)hLibModule & 1)
815 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
816 char *ptr = (char *)hLibModule - 1;
817 UnmapViewOfFile( ptr );
821 if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE;
822 else SetLastError( RtlNtStatusToDosError( nts ) );
827 /***********************************************************************
828 * GetProcAddress (KERNEL32.@)
830 * Find the address of an exported symbol in a loaded dll.
833 * hModule [I] Handle to the dll returned by LoadLibraryA().
834 * function [I] Name of the symbol, or an integer ordinal number < 16384
837 * Success: A pointer to the symbol in the process address space.
838 * Failure: NULL. Use GetLastError() to determine the cause.
840 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
845 if (HIWORD(function))
849 RtlInitAnsiString( &str, function );
850 nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp );
853 nts = LdrGetProcedureAddress( hModule, NULL, (DWORD)function, (void**)&fp );
854 if (nts != STATUS_SUCCESS)
856 SetLastError( RtlNtStatusToDosError( nts ) );
862 /***********************************************************************
863 * GetProcAddress32 (KERNEL.453)
865 * Find the address of an exported symbol in a loaded dll.
868 * hModule [I] Handle to the dll returned by LoadLibraryA().
869 * function [I] Name of the symbol, or an integer ordinal number < 16384
872 * Success: A pointer to the symbol in the process address space.
873 * Failure: NULL. Use GetLastError() to determine the cause.
875 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
877 /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */
878 return GetProcAddress( hModule, function );