Skip to content

Commit

Permalink
fwd: Redefine __MCF_cxa_dtor_union for x86
Browse files Browse the repository at this point in the history
  • Loading branch information
lhmouse committed Jan 22, 2024
1 parent 12b6010 commit 743c4f4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mcfgthread/dtor_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ __MCF_C_DECLARATIONS_BEGIN
/* Define the cxa_atexit queue structure. */
struct __MCF_dtor_element
{
__MCF_cxa_dtor_cdecl* __dtor;
__MCF_cxa_dtor_union __dtor;
void* __this;
void* __dso;
};
Expand Down
23 changes: 22 additions & 1 deletion mcfgthread/fwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,28 @@ typedef void _MCF_tls_dtor(void* __ptr);
* and on the stack, to allow both `__cdecl` and `__thiscall` functions to work
* properly. The function prototype is declared for compatibility with GCC. */
typedef void __MCF_cxa_dtor_cdecl(void* __arg);
typedef __MCF_cxa_dtor_cdecl __MCF_cxa_dtor_union;

#if defined __i386__ && (defined __GNUC__ || defined __clang__)
typedef void __thiscall __MCF_cxa_dtor_thiscall(void* __arg);
typedef union __MCF_cxa_dtor_union __MCF_cxa_dtor_union;

/* Provide dual-ABI support via this union. */
union __attribute__((__transparent_union__)) __MCF_cxa_dtor_union
{
__MCF_cxa_dtor_cdecl* __c_ptr;
__MCF_cxa_dtor_thiscall* __tc_ptr;

# ifdef __cplusplus
/* `__transparent_union__` is not supported in C++, so mimic it. */
__MCF_cxa_dtor_union(__MCF_cxa_dtor_cdecl* __p) __MCF_NOEXCEPT : __c_ptr(__p) { }
__MCF_cxa_dtor_union(__MCF_cxa_dtor_stdcall* __p) __MCF_NOEXCEPT : __tc_ptr(__p) { }
# endif
};
#else
/* FIXME: MSVC doesn't seem to support `__thiscall` on non-member functions. */
typedef __MCF_cxa_dtor_cdecl __MCF_cxa_dtor_thiscall;
typedef __MCF_cxa_dtor_cdecl* __MCF_cxa_dtor_union;
#endif

/* Define the prototype for `atexit()` and `at_quick_exit()`. */
typedef void __MCF_atexit_callback(void);
Expand Down
6 changes: 3 additions & 3 deletions mcfgthread/xglobals.i
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ __MCF_seh_i386_cleanup(__MCF_seh_i386_node* __seh_node) __MCF_NOEXCEPT
* properly. */
__MCF_ALWAYS_INLINE
void
__MCF_invoke_cxa_dtor(__MCF_cxa_dtor_cdecl* __dtor, void* __arg)
__MCF_invoke_cxa_dtor(__MCF_cxa_dtor_union __dtor, void* __arg)
{
/* Parameters are `EAX`, `EDX`, `ECX`, `ESP[4]`. */
typedef void __i386_abi_dtor(int, int, void*, void*) __attribute__((__regparm__(3)));
int __eax, __edx;
__asm__ ("" : "=a"(__eax), "=d"(__edx)); /* uninitialized. */
(*(__i386_abi_dtor*)(int) __dtor) (__eax, __edx, __arg, __arg);
typedef void __i386_abi_dtor(int, int, void*, void*) __attribute__((__regparm__(3)));
(*(__i386_abi_dtor*)(int) __dtor.__c_ptr) (__eax, __edx, __arg, __arg);
}
#else
/* Otherwise, SEH is table-based. */
Expand Down

0 comments on commit 743c4f4

Please sign in to comment.