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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <sys/types.h>
29 #define WIN32_NO_STATUS
34 #include "kernel_private.h"
37 #include "wine/exception.h"
38 #include "wine/list.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(module
);
45 /****************************************************************************
46 * GetDllDirectoryA (KERNEL32.@)
48 DWORD WINAPI
GetDllDirectoryA( DWORD buf_len
, LPSTR buffer
)
56 str
.MaximumLength
= sizeof(data
);
60 status
= LdrGetDllDirectory( &str
);
61 if (status
!= STATUS_BUFFER_TOO_SMALL
) break;
62 if (str
.Buffer
!= data
) HeapFree( GetProcessHeap(), 0, str
.Buffer
);
63 str
.MaximumLength
= str
.Length
;
64 if (!(str
.Buffer
= HeapAlloc( GetProcessHeap(), 0, str
.MaximumLength
)))
66 status
= STATUS_NO_MEMORY
;
71 if (!set_ntstatus( status
)) return 0;
73 len
= FILE_name_WtoA( str
.Buffer
, str
.Length
/ sizeof(WCHAR
), NULL
, 0 );
74 if (buffer
&& buf_len
> len
)
76 FILE_name_WtoA( str
.Buffer
, -1, buffer
, buf_len
);
80 len
++; /* for terminating null */
81 if (buffer
) *buffer
= 0;
83 if (str
.Buffer
!= data
) HeapFree( GetProcessHeap(), 0, str
.Buffer
);
88 /****************************************************************************
89 * GetDllDirectoryW (KERNEL32.@)
91 DWORD WINAPI
GetDllDirectoryW( DWORD buf_len
, LPWSTR buffer
)
97 str
.MaximumLength
= min( buf_len
, UNICODE_STRING_MAX_CHARS
) * sizeof(WCHAR
);
98 status
= LdrGetDllDirectory( &str
);
99 if (status
== STATUS_BUFFER_TOO_SMALL
) status
= STATUS_SUCCESS
;
100 if (!set_ntstatus( status
)) return 0;
101 return str
.Length
/ sizeof(WCHAR
);
105 /****************************************************************************
106 * SetDllDirectoryA (KERNEL32.@)
108 BOOL WINAPI
SetDllDirectoryA( LPCSTR dir
)
113 if (dir
&& !(dirW
= FILE_name_AtoW( dir
, TRUE
))) return FALSE
;
114 ret
= SetDllDirectoryW( dirW
);
115 HeapFree( GetProcessHeap(), 0, dirW
);
120 /****************************************************************************
121 * SetDllDirectoryW (KERNEL32.@)
123 BOOL WINAPI
SetDllDirectoryW( LPCWSTR dir
)
127 RtlInitUnicodeString( &str
, dir
);
128 return set_ntstatus( LdrSetDllDirectory( &str
));
132 /***********************************************************************
133 * GetBinaryTypeW [KERNEL32.@]
135 * Determine whether a file is executable, and if so, what kind.
138 * lpApplicationName [I] Path of the file to check
139 * lpBinaryType [O] Destination for the binary type
142 * TRUE, if the file is an executable, in which case lpBinaryType is set.
143 * FALSE, if the file is not an executable or if the function fails.
146 * The type of executable is a property that determines which subsystem an
147 * executable file runs under. lpBinaryType can be set to one of the following
149 * SCS_32BIT_BINARY: A Win32 based application
150 * SCS_64BIT_BINARY: A Win64 based application
151 * SCS_DOS_BINARY: An MS-Dos based application
152 * SCS_WOW_BINARY: A Win16 based application
153 * SCS_PIF_BINARY: A PIF file that executes an MS-Dos based app
154 * SCS_POSIX_BINARY: A POSIX based application ( Not implemented )
155 * SCS_OS216_BINARY: A 16bit OS/2 based application
157 * To find the binary type, this function reads in the files header information.
158 * If extended header information is not present it will assume that the file
159 * is a DOS executable. If extended header information is present it will
160 * determine if the file is a 16, 32 or 64 bit Windows executable by checking the
161 * flags in the header.
163 * ".com" and ".pif" files are only recognized by their file name extension,
164 * as per native Windows.
166 BOOL WINAPI
GetBinaryTypeW( LPCWSTR name
, LPDWORD type
)
168 HANDLE hfile
, mapping
;
172 TRACE("%s\n", debugstr_w(name
) );
174 if (type
== NULL
) return FALSE
;
176 hfile
= CreateFileW( name
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0 );
177 if ( hfile
== INVALID_HANDLE_VALUE
)
180 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
,
181 NULL
, NULL
, PAGE_READONLY
, SEC_IMAGE
, hfile
);
182 CloseHandle( hfile
);
188 SECTION_IMAGE_INFORMATION info
;
190 status
= NtQuerySection( mapping
, SectionImageInformation
, &info
, sizeof(info
), NULL
);
191 CloseHandle( mapping
);
192 if (status
) return FALSE
;
193 switch (info
.Machine
)
195 case IMAGE_FILE_MACHINE_I386
:
196 case IMAGE_FILE_MACHINE_ARM
:
197 case IMAGE_FILE_MACHINE_THUMB
:
198 case IMAGE_FILE_MACHINE_ARMNT
:
199 case IMAGE_FILE_MACHINE_POWERPC
:
200 *type
= SCS_32BIT_BINARY
;
202 case IMAGE_FILE_MACHINE_AMD64
:
203 case IMAGE_FILE_MACHINE_ARM64
:
204 *type
= SCS_64BIT_BINARY
;
209 case STATUS_INVALID_IMAGE_WIN_16
:
210 *type
= SCS_WOW_BINARY
;
212 case STATUS_INVALID_IMAGE_WIN_32
:
213 *type
= SCS_32BIT_BINARY
;
215 case STATUS_INVALID_IMAGE_WIN_64
:
216 *type
= SCS_64BIT_BINARY
;
218 case STATUS_INVALID_IMAGE_NE_FORMAT
:
219 *type
= SCS_OS216_BINARY
;
221 case STATUS_INVALID_IMAGE_PROTECT
:
222 *type
= SCS_DOS_BINARY
;
224 case STATUS_INVALID_IMAGE_NOT_MZ
:
225 if ((ptr
= wcsrchr( name
, '.' )))
227 if (!wcsicmp( ptr
, L
".com" ))
229 *type
= SCS_DOS_BINARY
;
232 if (!wcsicmp( ptr
, L
".pif" ))
234 *type
= SCS_PIF_BINARY
;
244 /***********************************************************************
245 * GetBinaryTypeA [KERNEL32.@]
246 * GetBinaryType [KERNEL32.@]
248 * See GetBinaryTypeW.
250 BOOL WINAPI
GetBinaryTypeA( LPCSTR lpApplicationName
, LPDWORD lpBinaryType
)
252 ANSI_STRING app_nameA
;
254 TRACE("%s\n", debugstr_a(lpApplicationName
));
258 if ( lpApplicationName
== NULL
|| lpBinaryType
== NULL
)
261 RtlInitAnsiString(&app_nameA
, lpApplicationName
);
262 if (!set_ntstatus( RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString
,
263 &app_nameA
, FALSE
)))
265 return GetBinaryTypeW(NtCurrentTeb()->StaticUnicodeString
.Buffer
, lpBinaryType
);
268 /***********************************************************************
269 * GetProcAddress (KERNEL32.@)
271 * Find the address of an exported symbol in a loaded dll.
274 * hModule [I] Handle to the dll returned by LoadLibraryA().
275 * function [I] Name of the symbol, or an integer ordinal number < 16384
278 * Success: A pointer to the symbol in the process address space.
279 * Failure: NULL. Use GetLastError() to determine the cause.
281 FARPROC
get_proc_address( HMODULE hModule
, LPCSTR function
)
285 if (!hModule
) hModule
= NtCurrentTeb()->Peb
->ImageBaseAddress
;
287 if ((ULONG_PTR
)function
>> 16)
291 RtlInitAnsiString( &str
, function
);
292 if (!set_ntstatus( LdrGetProcedureAddress( hModule
, &str
, 0, (void**)&fp
))) return NULL
;
295 if (!set_ntstatus( LdrGetProcedureAddress( hModule
, NULL
, LOWORD(function
), (void**)&fp
)))
303 * Work around a Delphi bug on x86_64. When delay loading a symbol,
304 * Delphi saves rcx, rdx, r8 and r9 to the stack. It then calls
305 * GetProcAddress(), pops the saved registers and calls the function.
306 * This works fine if all of the parameters are ints. However, since
307 * it does not save xmm0 - 3, it relies on GetProcAddress() preserving
308 * these registers if the function takes floating point parameters.
309 * This wrapper saves xmm0 - 3 to the stack.
311 extern FARPROC
get_proc_address_wrapper( HMODULE module
, LPCSTR function
);
313 __ASM_GLOBAL_FUNC( get_proc_address_wrapper
,
315 __ASM_SEH(".seh_pushreg %rbp\n\t")
316 __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
317 __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
319 __ASM_SEH(".seh_setframe %rbp,0\n\t")
320 __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
321 __ASM_SEH(".seh_endprologue\n\t")
322 "subq $0x60,%rsp\n\t"
324 "movaps %xmm0,0x20(%rsp)\n\t"
325 "movaps %xmm1,0x30(%rsp)\n\t"
326 "movaps %xmm2,0x40(%rsp)\n\t"
327 "movaps %xmm3,0x50(%rsp)\n\t"
328 "call " __ASM_NAME("get_proc_address") "\n\t"
329 "movaps 0x50(%rsp), %xmm3\n\t"
330 "movaps 0x40(%rsp), %xmm2\n\t"
331 "movaps 0x30(%rsp), %xmm1\n\t"
332 "movaps 0x20(%rsp), %xmm0\n\t"
333 "leaq 0(%rbp),%rsp\n\t"
334 __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
336 __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
337 __ASM_CFI(".cfi_same_value %rbp\n\t")
339 #else /* __x86_64__ */
341 static inline FARPROC
get_proc_address_wrapper( HMODULE module
, LPCSTR function
)
343 return get_proc_address( module
, function
);
346 #endif /* __x86_64__ */
348 FARPROC WINAPI
GetProcAddress( HMODULE hModule
, LPCSTR function
)
350 return get_proc_address_wrapper( hModule
, function
);