aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2023-05-21 14:23:11 +0200
committerPali Rohár <pali.rohar@gmail.com>2023-05-21 14:23:11 +0200
commit236d0f5e3db04e52ffd865de98ad80c6766707d3 (patch)
tree92bbc1b9a493e93cce734653593a28e704869ea1 /src
parent1beb65cdfc4ba10f8d57091fd1a4acd95d39784c (diff)
downloaddlfcn-win32-236d0f5e3db04e52ffd865de98ad80c6766707d3.tar.gz
dlfcn-win32-236d0f5e3db04e52ffd865de98ad80c6766707d3.tar.bz2
dlfcn-win32-236d0f5e3db04e52ffd865de98ad80c6766707d3.zip
Fix compilation with old MSVC compiler
Old pre-4.0 MSVC does not support _ReturnAddress() intrinsic. Provide for it simple implementation via inline assembly.
Diffstat (limited to 'src')
-rw-r--r--src/dlfcn.c12
1 files changed, 12 insertions, 0 deletions
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;
47#endif 47#endif
48 48
49#ifdef _MSC_VER 49#ifdef _MSC_VER
50#if _MSC_VER >= 1000
50/* https://docs.microsoft.com/en-us/cpp/intrinsics/returnaddress */ 51/* https://docs.microsoft.com/en-us/cpp/intrinsics/returnaddress */
51#pragma intrinsic( _ReturnAddress ) 52#pragma intrinsic( _ReturnAddress )
52#else 53#else
54/* On older version read return address from the value on stack pointer + 4 of
55 * the caller. Caller stack pointer is stored in EBP register but only when
56 * the EBP register is not optimized out. Usage of _alloca() function prevent
57 * EBP register optimization. Read value of EBP + 4 via inline assembly. And
58 * because inline assembly does not have a return value, put it into naked
59 * function which does not have prologue and epilogue and preserve registers.
60 */
61__declspec( naked ) static void *_ReturnAddress( void ) { __asm mov eax, [ebp+4] __asm ret }
62#define _ReturnAddress( ) ( _alloca(1), _ReturnAddress( ) )
63#endif
64#else
53/* https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html */ 65/* https://gcc.gnu.org/onlinedocs/gcc/Return-Address.html */
54#ifndef _ReturnAddress 66#ifndef _ReturnAddress
55#define _ReturnAddress( ) ( __builtin_extract_return_addr( __builtin_return_address( 0 ) ) ) 67#define _ReturnAddress( ) ( __builtin_extract_return_addr( __builtin_return_address( 0 ) ) )