From 1beb65cdfc4ba10f8d57091fd1a4acd95d39784c Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 21 May 2023 12:11:20 +0200 Subject: Fix compilation with older SDK Do not use SIZE_T which is not defined in older SDK. There is only size_t type, so use it instead. Do not use IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR which is not defined in older SDK. Use IMAGE_NUMBEROF_DIRECTORY_ENTRIES macro for checking if directory index is valid. In all SDKs is DataDirectory[] array size defined from IMAGE_NUMBEROF_DIRECTORY_ENTRIES macro. Cast members in IMAGE_EXPORT_DIRECTORY and IMAGE_DIRECTORY_ENTRY_IMPORT to DWORD as in older SDK they are defined as PDWORD and compiler throws error 'cannot add two pointers'. --- src/dlfcn.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/dlfcn.c b/src/dlfcn.c index 1402df2..da9a37a 100644 --- a/src/dlfcn.c +++ b/src/dlfcn.c @@ -252,7 +252,7 @@ static HMODULE MyGetModuleHandleFromAddress( const void *addr ) HMODULE kernel32; HMODULE hModule; MEMORY_BASIC_INFORMATION info; - SIZE_T sLen; + size_t sLen; if( !failed && GetModuleHandleExAPtr == NULL ) { @@ -615,7 +615,7 @@ static BOOL get_image_section( HMODULE module, int index, void **ptr, DWORD *siz if( optionalHeader->Magic != IMAGE_NT_OPTIONAL_HDR_MAGIC ) return FALSE; - if( index < 0 || index > IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR ) + if( index < 0 || index >= IMAGE_NUMBEROF_DIRECTORY_ENTRIES ) return FALSE; if( optionalHeader->DataDirectory[index].Size == 0 || optionalHeader->DataDirectory[index].VirtualAddress == 0 ) @@ -636,9 +636,9 @@ static const char *get_export_symbol_name( HMODULE module, IMAGE_EXPORT_DIRECTOR void *candidateAddr = NULL; int candidateIndex = -1; BYTE *base = (BYTE *) module; - DWORD *functionAddressesOffsets = (DWORD *) (base + ied->AddressOfFunctions); - DWORD *functionNamesOffsets = (DWORD *) (base + ied->AddressOfNames); - USHORT *functionNameOrdinalsIndexes = (USHORT *) (base + ied->AddressOfNameOrdinals); + DWORD *functionAddressesOffsets = (DWORD *) (base + (DWORD) ied->AddressOfFunctions); + DWORD *functionNamesOffsets = (DWORD *) (base + (DWORD) ied->AddressOfNames); + USHORT *functionNameOrdinalsIndexes = (USHORT *) (base + (DWORD) ied->AddressOfNameOrdinals); for( i = 0; i < ied->NumberOfFunctions; i++ ) { @@ -666,7 +666,7 @@ static const char *get_export_symbol_name( HMODULE module, IMAGE_EXPORT_DIRECTOR static BOOL is_valid_address( const void *addr ) { MEMORY_BASIC_INFORMATION info; - SIZE_T result; + size_t result; if( addr == NULL ) return FALSE; @@ -852,7 +852,7 @@ int dladdr( const void *addr, Dl_info *info ) if( iid == NULL || iid->Characteristics == 0 || iid->FirstThunk == 0 ) return 0; - iat = (void *)( (BYTE *) hModule + iid->FirstThunk ); + iat = (void *)( (BYTE *) hModule + (DWORD) iid->FirstThunk ); /* We assume that in this case iid and iat's are in linear order */ iatSize = iidSize - (DWORD) ( (BYTE *) iat - (BYTE *) iid ); } -- cgit v1.2.3-55-g6feb From 236d0f5e3db04e52ffd865de98ad80c6766707d3 Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 21 May 2023 14:23:11 +0200 Subject: Fix compilation with old MSVC compiler Old pre-4.0 MSVC does not support _ReturnAddress() intrinsic. Provide for it simple implementation via inline assembly. --- src/dlfcn.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/dlfcn.c b/src/dlfcn.c index da9a37a..8dc0359 100644 --- a/src/dlfcn.c +++ b/src/dlfcn.c @@ -47,9 +47,21 @@ typedef ULONG ULONG_PTR; #endif #ifdef _MSC_VER +#if _MSC_VER >= 1000 /* https://docs.microsoft.com/en-us/cpp/intrinsics/returnaddress */ #pragma intrinsic( _ReturnAddress ) #else +/* On older version read return address from the value on stack pointer + 4 of + * the caller. Caller stack pointer is stored in EBP register but only when + * the EBP register is not optimized out. Usage of _alloca() function prevent + * EBP register optimization. Read value of EBP + 4 via inline assembly. And + * because inline assembly does not have a return value, put it into naked + * function which does not have prologue and epilogue and preserve registers. + */ +__declspec( naked ) static void *_ReturnAddress( void ) { __asm mov eax, [ebp+4] __asm ret } +#define _ReturnAddress( ) ( _alloca(1), _ReturnAddress( ) ) +#endif +#else /* https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html */ #ifndef _ReturnAddress #define _ReturnAddress( ) ( __builtin_extract_return_addr( __builtin_return_address( 0 ) ) ) -- cgit v1.2.3-55-g6feb From 519400bf75ec7846e50a70f620d6dd4d570b258d Mon Sep 17 00:00:00 2001 From: Pali Rohár Date: Sun, 21 May 2023 14:26:47 +0200 Subject: Fix compilation with the first NT SDK First NT SDK available in the first 32-bit MSVC NT compiler does not provide some defines. Add them for compatibility. --- src/dlfcn.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/dlfcn.c b/src/dlfcn.c index 8dc0359..c4563d2 100644 --- a/src/dlfcn.c +++ b/src/dlfcn.c @@ -45,6 +45,19 @@ typedef ULONG ULONG_PTR; #ifndef GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT #define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 0x2 #endif +#ifndef IMAGE_NT_OPTIONAL_HDR_MAGIC +#ifdef _WIN64 +#define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x20b +#else +#define IMAGE_NT_OPTIONAL_HDR_MAGIC 0x10b +#endif +#endif +#ifndef IMAGE_DIRECTORY_ENTRY_IAT +#define IMAGE_DIRECTORY_ENTRY_IAT 12 +#endif +#ifndef LOAD_WITH_ALTERED_SEARCH_PATH +#define LOAD_WITH_ALTERED_SEARCH_PATH 0x8 +#endif #ifdef _MSC_VER #if _MSC_VER >= 1000 -- cgit v1.2.3-55-g6feb