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 if (SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) == -1)
197 if (!ReadFile( hfile, magic, sizeof(magic), &len, NULL ) || len != sizeof(magic))
200 /* Reading the magic field succeeded so
201 * we will try to determine what type it is.
203 if (!memcmp( magic, "PE\0\0", 4 ))
205 IMAGE_FILE_HEADER FileHeader;
207 if (ReadFile( hfile, &FileHeader, sizeof(FileHeader), &len, NULL ) && len == sizeof(FileHeader))
209 if (FileHeader.Characteristics & IMAGE_FILE_DLL) return BINARY_PE_DLL;
210 return BINARY_PE_EXE;
215 if (!memcmp( magic, "NE", 2 ))
217 /* This is a Windows executable (NE) header. This can
218 * mean either a 16-bit OS/2 or a 16-bit Windows or even a
219 * DOS program (running under a DOS extender). To decide
220 * which, we'll have to read the NE header.
223 if ( SetFilePointer( hfile, header.mz.e_lfanew, NULL, SEEK_SET ) != -1
224 && ReadFile( hfile, &ne, sizeof(ne), &len, NULL )
225 && len == sizeof(ne) )
227 switch ( ne.ne_exetyp )
229 case 2: return BINARY_WIN16;
230 case 5: return BINARY_DOS;
231 default: return MODULE_Decide_OS2_OldWin(hfile, &header.mz, &ne);
234 /* Couldn't read header, so abort. */
238 /* Unknown extended header, but this file is nonetheless DOS-executable. */
242 return BINARY_UNKNOWN;
245 /***********************************************************************
246 * GetBinaryTypeW [KERNEL32.@]
248 * Determine whether a file is executable, and if so, what kind.
251 * lpApplicationName [I] Path of the file to check
252 * lpBinaryType [O] Destination for the binary type
255 * TRUE, if the file is an executable, in which case lpBinaryType is set.
256 * FALSE, if the file is not an executable or if the function fails.
259 * The type of executable is a property that determines which subsytem an
260 * executable file runs under. lpBinaryType can be set to one of the following
262 * SCS_32BIT_BINARY: A Win32 based application
263 * SCS_DOS_BINARY: An MS-Dos based application
264 * SCS_WOW_BINARY: A Win16 based application
265 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
266 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
267 * SCS_OS216_BINARY: A 16bit OS/2 based application
269 * To find the binary type, this function reads in the files header information.
270 * If extended header information is not present it will assume that the file
271 * is a DOS executable. If extended header information is present it will
272 * determine if the file is a 16 or 32 bit Windows executable by checking the
273 * flags in the header.
275 * ".com" and ".pif" files are only recognized by their file name extension,
276 * as per native Windows.
278 BOOL WINAPI GetBinaryTypeW( LPCWSTR lpApplicationName, LPDWORD lpBinaryType )
283 TRACE("%s\n", debugstr_w(lpApplicationName) );
287 if ( lpApplicationName == NULL || lpBinaryType == NULL )
290 /* Open the file indicated by lpApplicationName for reading.
292 hfile = CreateFileW( lpApplicationName, GENERIC_READ, FILE_SHARE_READ,
293 NULL, OPEN_EXISTING, 0, 0 );
294 if ( hfile == INVALID_HANDLE_VALUE )
299 switch(MODULE_GetBinaryType( hfile ))
303 static const WCHAR comW[] = { '.','C','O','M',0 };
304 static const WCHAR pifW[] = { '.','P','I','F',0 };
307 /* try to determine from file name */
308 ptr = strrchrW( lpApplicationName, '.' );
310 if (!strcmpiW( ptr, comW ))
312 *lpBinaryType = SCS_DOS_BINARY;
315 else if (!strcmpiW( ptr, pifW ))
317 *lpBinaryType = SCS_PIF_BINARY;
324 *lpBinaryType = SCS_32BIT_BINARY;
328 *lpBinaryType = SCS_WOW_BINARY;
332 *lpBinaryType = SCS_OS216_BINARY;
336 *lpBinaryType = SCS_DOS_BINARY;
339 case BINARY_UNIX_EXE:
340 case BINARY_UNIX_LIB:
345 CloseHandle( hfile );
349 /***********************************************************************
350 * GetBinaryTypeA [KERNEL32.@]
351 * GetBinaryType [KERNEL32.@]
353 BOOL WINAPI GetBinaryTypeA( LPCSTR lpApplicationName, LPDWORD lpBinaryType )
355 ANSI_STRING app_nameA;
358 TRACE("%s\n", debugstr_a(lpApplicationName));
362 if ( lpApplicationName == NULL || lpBinaryType == NULL )
365 RtlInitAnsiString(&app_nameA, lpApplicationName);
366 status = RtlAnsiStringToUnicodeString(&NtCurrentTeb()->StaticUnicodeString,
369 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString.Buffer, lpBinaryType);
371 SetLastError(RtlNtStatusToDosError(status));
376 /***********************************************************************
377 * GetModuleHandleA (KERNEL32.@)
378 * GetModuleHandle32 (KERNEL.488)
380 * Get the handle of a dll loaded into the process address space.
383 * module [I] Name of the dll
386 * Success: A handle to the loaded dll.
387 * Failure: A NULL handle. Use GetLastError() to determine the cause.
389 HMODULE WINAPI GetModuleHandleA(LPCSTR module)
395 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
397 RtlCreateUnicodeStringFromAsciiz(&wstr, module);
398 nts = LdrGetDllHandle(0, 0, &wstr, &ret);
399 RtlFreeUnicodeString( &wstr );
400 if (nts != STATUS_SUCCESS)
403 SetLastError( RtlNtStatusToDosError( nts ) );
408 /***********************************************************************
409 * GetModuleHandleW (KERNEL32.@)
411 * Unicode version of GetModuleHandleA.
413 HMODULE WINAPI GetModuleHandleW(LPCWSTR module)
419 if (!module) return NtCurrentTeb()->Peb->ImageBaseAddress;
421 RtlInitUnicodeString( &wstr, module );
422 nts = LdrGetDllHandle( 0, 0, &wstr, &ret);
423 if (nts != STATUS_SUCCESS)
425 SetLastError( RtlNtStatusToDosError( nts ) );
432 /***********************************************************************
433 * GetModuleFileNameA (KERNEL32.@)
434 * GetModuleFileName32 (KERNEL.487)
436 * Get the file name of a loaded module from its handle.
439 * Success: The length of the file name, excluding the terminating NUL.
440 * Failure: 0. Use GetLastError() to determine the cause.
443 * This function always returns the long path of hModule (as opposed to
444 * GetModuleFileName16() which returns short paths when the modules version
447 DWORD WINAPI GetModuleFileNameA(
448 HMODULE hModule, /* [in] Module handle (32 bit) */
449 LPSTR lpFileName, /* [out] Destination for file name */
450 DWORD size ) /* [in] Size of lpFileName in characters */
452 LPWSTR filenameW = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) );
456 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
459 GetModuleFileNameW( hModule, filenameW, size );
460 WideCharToMultiByte( CP_ACP, 0, filenameW, -1, lpFileName, size, NULL, NULL );
461 HeapFree( GetProcessHeap(), 0, filenameW );
462 return strlen( lpFileName );
465 /***********************************************************************
466 * GetModuleFileNameW (KERNEL32.@)
468 * Unicode version of GetModuleFileNameA.
470 DWORD WINAPI GetModuleFileNameW( HMODULE hModule, LPWSTR lpFileName, DWORD size )
476 LdrLockLoaderLock( 0, NULL, &magic );
477 if (!hModule && !(NtCurrentTeb()->tibflags & TEBF_WIN32))
479 /* 16-bit task - get current NE module name */
480 NE_MODULE *pModule = NE_GetPtr( GetCurrentTask() );
483 WCHAR path[MAX_PATH];
485 MultiByteToWideChar( CP_ACP, 0, NE_MODULE_NAME(pModule), -1, path, MAX_PATH );
486 GetLongPathNameW(path, lpFileName, size);
494 if (!hModule) hModule = NtCurrentTeb()->Peb->ImageBaseAddress;
495 nts = LdrFindEntryForAddress( hModule, &pldr );
496 if (nts == STATUS_SUCCESS) lstrcpynW(lpFileName, pldr->FullDllName.Buffer, size);
497 else SetLastError( RtlNtStatusToDosError( nts ) );
500 LdrUnlockLoaderLock( 0, magic );
502 TRACE( "%s\n", debugstr_w(lpFileName) );
503 return strlenW(lpFileName);
507 /***********************************************************************
508 * get_dll_system_path
510 static const WCHAR *get_dll_system_path(void)
519 exe_name = NtCurrentTeb()->Peb->ProcessParameters->ImagePathName.Buffer;
520 if (!(p = strrchrW( exe_name, '\\' ))) p = exe_name;
521 /* include trailing backslash only on drive root */
522 if (p == exe_name + 2 && exe_name[1] == ':') p++;
524 len += GetSystemDirectoryW( NULL, 0 );
525 len += GetWindowsDirectoryW( NULL, 0 );
526 path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
527 memcpy( path, exe_name, (p - exe_name) * sizeof(WCHAR) );
528 p = path + (p - exe_name);
532 GetSystemDirectoryW( p, path + len - p);
535 GetWindowsDirectoryW( p, path + len - p);
541 /******************************************************************
544 * Compute the load path to use for a given dll.
545 * Returned pointer must be freed by caller.
547 static WCHAR *get_dll_load_path( LPCWSTR module )
549 static const WCHAR pathW[] = {'P','A','T','H',0};
551 const WCHAR *system_path = get_dll_system_path();
552 const WCHAR *mod_end = NULL;
553 UNICODE_STRING name, value;
555 int len = 0, path_len = 0;
557 /* adjust length for module name */
562 if ((p = strrchrW( mod_end, '\\' ))) mod_end = p;
563 if ((p = strrchrW( mod_end, '/' ))) mod_end = p;
564 if (mod_end == module + 2 && module[1] == ':') mod_end++;
565 if (mod_end == module && module[0] && module[1] == ':') mod_end += 2;
566 len += (mod_end - module);
567 system_path = strchrW( system_path, ';' );
569 len += strlenW( system_path ) + 2;
571 /* get the PATH variable */
573 RtlInitUnicodeString( &name, pathW );
575 value.MaximumLength = 0;
577 if (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
578 path_len = value.Length;
580 if (!(ret = HeapAlloc( GetProcessHeap(), 0, path_len + len * sizeof(WCHAR) ))) return NULL;
584 memcpy( ret, module, (mod_end - module) * sizeof(WCHAR) );
585 p += (mod_end - module);
587 strcpyW( p, system_path );
591 value.MaximumLength = path_len;
593 while (RtlQueryEnvironmentVariable_U( NULL, &name, &value ) == STATUS_BUFFER_TOO_SMALL)
597 /* grow the buffer and retry */
598 path_len = value.Length;
599 if (!(new_ptr = HeapReAlloc( GetProcessHeap(), 0, ret, path_len + len * sizeof(WCHAR) )))
601 HeapFree( GetProcessHeap(), 0, ret );
604 value.Buffer = new_ptr + (value.Buffer - ret);
605 value.MaximumLength = path_len;
608 value.Buffer[value.Length / sizeof(WCHAR)] = 0;
613 /******************************************************************
614 * MODULE_InitLoadPath
616 * Create the initial dll load path.
618 void MODULE_InitLoadPath(void)
620 WCHAR *path = get_dll_load_path( NULL );
621 RtlInitUnicodeString( &NtCurrentTeb()->Peb->ProcessParameters->DllPath, path );
625 /******************************************************************
626 * load_library_as_datafile
628 static BOOL load_library_as_datafile( LPCWSTR name, HMODULE* hmod)
630 static const WCHAR dotDLL[] = {'.','d','l','l',0};
632 WCHAR filenameW[MAX_PATH];
633 HANDLE hFile = INVALID_HANDLE_VALUE;
639 if (SearchPathW( NULL, (LPCWSTR)name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]),
642 hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ,
643 NULL, OPEN_EXISTING, 0, 0 );
645 if (hFile == INVALID_HANDLE_VALUE) return FALSE;
647 mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
648 CloseHandle( hFile );
649 if (!mapping) return FALSE;
651 module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
652 CloseHandle( mapping );
653 if (!module) return FALSE;
655 /* make sure it's a valid PE file */
656 if (!RtlImageNtHeader(module))
658 UnmapViewOfFile( module );
661 *hmod = (HMODULE)((char *)module + 1); /* set low bit of handle to indicate datafile module */
666 /******************************************************************
669 * Helper for LoadLibraryExA/W.
671 static HMODULE load_library( const UNICODE_STRING *libname, DWORD flags )
677 if (flags & LOAD_LIBRARY_AS_DATAFILE)
679 /* The method in load_library_as_datafile allows searching for the
680 * 'native' libraries only
682 if (load_library_as_datafile( libname->Buffer, &hModule )) return hModule;
683 flags |= DONT_RESOLVE_DLL_REFERENCES; /* Just in case */
684 /* Fallback to normal behaviour */
687 load_path = get_dll_load_path( flags & LOAD_WITH_ALTERED_SEARCH_PATH ? libname->Buffer : NULL );
688 nts = LdrLoadDll( load_path, flags, libname, &hModule );
689 HeapFree( GetProcessHeap(), 0, load_path );
690 if (nts != STATUS_SUCCESS)
693 SetLastError( RtlNtStatusToDosError( nts ) );
699 /******************************************************************
700 * LoadLibraryExA (KERNEL32.@)
702 * Load a dll file into the process address space.
705 * libname [I] Name of the file to load
706 * hfile [I] Reserved, must be 0.
707 * flags [I] Flags for loading the dll
710 * Success: A handle to the loaded dll.
711 * Failure: A NULL handle. Use GetLastError() to determine the cause.
714 * The HFILE parameter is not used and marked reserved in the SDK. I can
715 * only guess that it should force a file to be mapped, but I rather
716 * ignore the parameter because it would be extremely difficult to
717 * integrate this with different types of module representations.
719 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
726 SetLastError(ERROR_INVALID_PARAMETER);
729 RtlCreateUnicodeStringFromAsciiz( &wstr, libname );
730 hModule = load_library( &wstr, flags );
731 RtlFreeUnicodeString( &wstr );
735 /***********************************************************************
736 * LoadLibraryExW (KERNEL32.@)
738 * Unicode version of LoadLibraryExA.
740 HMODULE WINAPI LoadLibraryExW(LPCWSTR libnameW, HANDLE hfile, DWORD flags)
746 SetLastError(ERROR_INVALID_PARAMETER);
749 RtlInitUnicodeString( &wstr, libnameW );
750 return load_library( &wstr, flags );
753 /***********************************************************************
754 * LoadLibraryA (KERNEL32.@)
756 * Load a dll file into the process address space.
759 * libname [I] Name of the file to load
762 * Success: A handle to the loaded dll.
763 * Failure: A NULL handle. Use GetLastError() to determine the cause.
766 * See LoadLibraryExA().
768 HMODULE WINAPI LoadLibraryA(LPCSTR libname)
770 return LoadLibraryExA(libname, 0, 0);
773 /***********************************************************************
774 * LoadLibraryW (KERNEL32.@)
776 * Unicode version of LoadLibraryA.
778 HMODULE WINAPI LoadLibraryW(LPCWSTR libnameW)
780 return LoadLibraryExW(libnameW, 0, 0);
783 /***********************************************************************
784 * FreeLibrary (KERNEL32.@)
785 * FreeLibrary32 (KERNEL.486)
787 * Free a dll loaded into the process address space.
790 * hLibModule [I] Handle to the dll returned by LoadLibraryA().
793 * Success: TRUE. The dll is removed if it is not still in use.
794 * Failure: FALSE. Use GetLastError() to determine the cause.
796 BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
803 SetLastError( ERROR_INVALID_HANDLE );
807 if ((ULONG_PTR)hLibModule & 1)
809 /* this is a LOAD_LIBRARY_AS_DATAFILE module */
810 char *ptr = (char *)hLibModule - 1;
811 UnmapViewOfFile( ptr );
815 if ((nts = LdrUnloadDll( hLibModule )) == STATUS_SUCCESS) retv = TRUE;
816 else SetLastError( RtlNtStatusToDosError( nts ) );
821 /***********************************************************************
822 * GetProcAddress (KERNEL32.@)
824 * Find the address of an exported symbol in a loaded dll.
827 * hModule [I] Handle to the dll returned by LoadLibraryA().
828 * function [I] Name of the symbol, or an integer ordinal number < 16384
831 * Success: A pointer to the symbol in the process address space.
832 * Failure: NULL. Use GetLastError() to determine the cause.
834 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
839 if (HIWORD(function))
843 RtlInitAnsiString( &str, function );
844 nts = LdrGetProcedureAddress( hModule, &str, 0, (void**)&fp );
847 nts = LdrGetProcedureAddress( hModule, NULL, (DWORD)function, (void**)&fp );
848 if (nts != STATUS_SUCCESS)
850 SetLastError( RtlNtStatusToDosError( nts ) );
856 /***********************************************************************
857 * GetProcAddress32 (KERNEL.453)
859 * Find the address of an exported symbol in a loaded dll.
862 * hModule [I] Handle to the dll returned by LoadLibraryA().
863 * function [I] Name of the symbol, or an integer ordinal number < 16384
866 * Success: A pointer to the symbol in the process address space.
867 * Failure: NULL. Use GetLastError() to determine the cause.
869 FARPROC WINAPI GetProcAddress32_16( HMODULE hModule, LPCSTR function )
871 /* FIXME: we used to disable snoop when returning proc for Win16 subsystem */
872 return GetProcAddress( hModule, function );