diff options
author | Silvio Traversaro <silvio@traversaro.it> | 2023-05-25 10:06:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-25 10:06:21 +0200 |
commit | f70fe95c098bea78191dc8082cba0f91c7bed5c5 (patch) | |
tree | 03ef5659536461ac15f90dd48a1c7ec9147dc77a | |
parent | 6444294ee354796536c34b54e154c0538a4d1eaf (diff) | |
parent | 519400bf75ec7846e50a70f620d6dd4d570b258d (diff) | |
download | dlfcn-win32-f70fe95c098bea78191dc8082cba0f91c7bed5c5.tar.gz dlfcn-win32-f70fe95c098bea78191dc8082cba0f91c7bed5c5.tar.bz2 dlfcn-win32-f70fe95c098bea78191dc8082cba0f91c7bed5c5.zip |
Merge pull request #108 from pali/master
Fix compilation with older SDK and older MSVC
-rw-r--r-- | src/dlfcn.c | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/src/dlfcn.c b/src/dlfcn.c index 1402df2..c4563d2 100644 --- a/src/dlfcn.c +++ b/src/dlfcn.c | |||
@@ -45,11 +45,36 @@ typedef ULONG ULONG_PTR; | |||
45 | #ifndef GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT | 45 | #ifndef GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
46 | #define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 0x2 | 46 | #define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 0x2 |
47 | #endif | 47 | #endif |
48 | #ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC | ||
49 | #ifdef _WIN64 | ||
50 | #define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x20b | ||
51 | #else | ||
52 | #define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b | ||
53 | #endif | ||
54 | #endif | ||
55 | #ifndef IMAGE_DIRECTORY_ENTRY_IAT | ||
56 | #define IMAGE_DIRECTORY_ENTRY_IAT 12 | ||
57 | #endif | ||
58 | #ifndef LOAD_WITH_ALTERED_SEARCH_PATH | ||
59 | #define LOAD_WITH_ALTERED_SEARCH_PATH 0x8 | ||
60 | #endif | ||
48 | 61 | ||
49 | #ifdef _MSC_VER | 62 | #ifdef _MSC_VER |
63 | #if _MSC_VER >= 1000 | ||
50 | /* https://docs.microsoft.com/en-us/cpp/intrinsics/returnaddress */ | 64 | /* https://docs.microsoft.com/en-us/cpp/intrinsics/returnaddress */ |
51 | #pragma intrinsic( _ReturnAddress ) | 65 | #pragma intrinsic( _ReturnAddress ) |
52 | #else | 66 | #else |
67 | /* On older version read return address from the value on stack pointer + 4 of | ||
68 | * the caller. Caller stack pointer is stored in EBP register but only when | ||
69 | * the EBP register is not optimized out. Usage of _alloca() function prevent | ||
70 | * EBP register optimization. Read value of EBP + 4 via inline assembly. And | ||
71 | * because inline assembly does not have a return value, put it into naked | ||
72 | * function which does not have prologue and epilogue and preserve registers. | ||
73 | */ | ||
74 | __declspec( naked ) static void *_ReturnAddress( void ) { __asm mov eax, [ebp+4] __asm ret } | ||
75 | #define _ReturnAddress( ) ( _alloca(1), _ReturnAddress( ) ) | ||
76 | #endif | ||
77 | #else | ||
53 | /* https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html */ | 78 | /* https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html */ |
54 | #ifndef _ReturnAddress | 79 | #ifndef _ReturnAddress |
55 | #define _ReturnAddress( ) ( __builtin_extract_return_addr( __builtin_return_address( 0 ) ) ) | 80 | #define _ReturnAddress( ) ( __builtin_extract_return_addr( __builtin_return_address( 0 ) ) ) |
@@ -252,7 +277,7 @@ static HMODULE MyGetModuleHandleFromAddress( const void *addr ) | |||
252 | HMODULE kernel32; | 277 | HMODULE kernel32; |
253 | HMODULE hModule; | 278 | HMODULE hModule; |
254 | MEMORY_BASIC_INFORMATION info; | 279 | MEMORY_BASIC_INFORMATION info; |
255 | SIZE_T sLen; | 280 | size_t sLen; |
256 | 281 | ||
257 | if( !failed && GetModuleHandleExAPtr == NULL ) | 282 | if( !failed && GetModuleHandleExAPtr == NULL ) |
258 | { | 283 | { |
@@ -615,7 +640,7 @@ static BOOL get_image_section( HMODULE module, int index, void **ptr, DWORD *siz | |||
615 | if( optionalHeader->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC ) | 640 | if( optionalHeader->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC ) |
616 | return FALSE; | 641 | return FALSE; |
617 | 642 | ||
618 | if( index < 0 || index > IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR ) | 643 | if( index < 0 || index >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ) |
619 | return FALSE; | 644 | return FALSE; |
620 | 645 | ||
621 | if( optionalHeader->DataDirectory[index].Size == 0 || optionalHeader->DataDirectory[index].VirtualAddress == 0 ) | 646 | if( optionalHeader->DataDirectory[index].Size == 0 || optionalHeader->DataDirectory[index].VirtualAddress == 0 ) |
@@ -636,9 +661,9 @@ static const char *get_export_symbol_name( HMODULE module, IMAGE_EXPORT_DIRECTOR | |||
636 | void *candidateAddr = NULL; | 661 | void *candidateAddr = NULL; |
637 | int candidateIndex = -1; | 662 | int candidateIndex = -1; |
638 | BYTE *base = (BYTE *) module; | 663 | BYTE *base = (BYTE *) module; |
639 | DWORD *functionAddressesOffsets = (DWORD *) (base + ied->AddressOfFunctions); | 664 | DWORD *functionAddressesOffsets = (DWORD *) (base + (DWORD) ied->AddressOfFunctions); |
640 | DWORD *functionNamesOffsets = (DWORD *) (base + ied->AddressOfNames); | 665 | DWORD *functionNamesOffsets = (DWORD *) (base + (DWORD) ied->AddressOfNames); |
641 | USHORT *functionNameOrdinalsIndexes = (USHORT *) (base + ied->AddressOfNameOrdinals); | 666 | USHORT *functionNameOrdinalsIndexes = (USHORT *) (base + (DWORD) ied->AddressOfNameOrdinals); |
642 | 667 | ||
643 | for( i = 0; i < ied->NumberOfFunctions; i++ ) | 668 | for( i = 0; i < ied->NumberOfFunctions; i++ ) |
644 | { | 669 | { |
@@ -666,7 +691,7 @@ static const char *get_export_symbol_name( HMODULE module, IMAGE_EXPORT_DIRECTOR | |||
666 | static BOOL is_valid_address( const void *addr ) | 691 | static BOOL is_valid_address( const void *addr ) |
667 | { | 692 | { |
668 | MEMORY_BASIC_INFORMATION info; | 693 | MEMORY_BASIC_INFORMATION info; |
669 | SIZE_T result; | 694 | size_t result; |
670 | 695 | ||
671 | if( addr == NULL ) | 696 | if( addr == NULL ) |
672 | return FALSE; | 697 | return FALSE; |
@@ -852,7 +877,7 @@ int dladdr( const void *addr, Dl_info *info ) | |||
852 | if( iid == NULL || iid->Characteristics == 0 || iid->FirstThunk == 0 ) | 877 | if( iid == NULL || iid->Characteristics == 0 || iid->FirstThunk == 0 ) |
853 | return 0; | 878 | return 0; |
854 | 879 | ||
855 | iat = (void *)( (BYTE *) hModule + iid->FirstThunk ); | 880 | iat = (void *)( (BYTE *) hModule + (DWORD) iid->FirstThunk ); |
856 | /* We assume that in this case iid and iat's are in linear order */ | 881 | /* We assume that in this case iid and iat's are in linear order */ |
857 | iatSize = iidSize - (DWORD) ( (BYTE *) iat - (BYTE *) iid ); | 882 | iatSize = iidSize - (DWORD) ( (BYTE *) iat - (BYTE *) iid ); |
858 | } | 883 | } |