On Saturday 24 December 2022 11:48:51 LIU Hao wrote:
> 在 2022-12-24 06:14, Pali Rohár 写道:
> > I just quickly tested it only in wine. Without LTO it prints "ł$"
> > (really strange, looks like some memory problem, maybe in wine?).
> >
>
> Oops, sorry forgot that the callback for `__cxa_thread_atexit` should be
> specified with `__cdecl`, otherwise its argument (the second argument for
> `__cxa_thread_atexit`) will be passed incorrectly on i686.
>
>
> This can be rectified like this:
>
> ```
> typedef void __thiscall atexit_func(void*);
> extern int __cxa_thread_atexit(atexit_func*, void*, void*);
> extern void* __dso_handle;
> extern int puts(const char*);
>
> void
> __thiscall
> puts_wrapper(void* param)
> {
> puts(param);
> }
>
> int
> main(void)
> {
> /* print a string upon the current thread's exit. */
> __cxa_thread_atexit(puts_wrapper, "test thread atexit",
> &__dso_handle);
> }
> ```
>
Ok, without LTO this prints "test thread atexit". With LTO it prints nothing.
>
> > And with LTO it prints nothing. When I added __attribute__((used)) for
> > those 3 symbols then it started printing "ő<".
>
> So the attribute seems necessary. Patches are welcome.
This patch fixes it! And also in LTO mode it prints "test thread atexit".
diff --git a/mingw-w64-crt/crt/tls_atexit.c b/mingw-w64-crt/crt/tls_atexit.c
index f39731ad76f3..94ebe1d3ace2 100644
--- a/mingw-w64-crt/crt/tls_atexit.c
+++ b/mingw-w64-crt/crt/tls_atexit.c
@@ -158,4 +158,5 @@ static void WINAPI tls_callback(HANDLE hDllHandle, DWORD
dwReason, LPVOID __UNUS
}
}
+__attribute__((used))
_CRTALLOC(".CRT$XLB") PIMAGE_TLS_CALLBACK __xl_b = (PIMAGE_TLS_CALLBACK)
tls_callback;
diff --git a/mingw-w64-crt/crt/tlssup.c b/mingw-w64-crt/crt/tlssup.c
index 47beb27fcc89..d5d8d579efcf 100644
--- a/mingw-w64-crt/crt/tlssup.c
+++ b/mingw-w64-crt/crt/tlssup.c
@@ -44,6 +44,7 @@ _CRTALLOC(".tls$ZZZ") char *_tls_end = NULL;
_CRTALLOC(".CRT$XLA") PIMAGE_TLS_CALLBACK __xl_a = 0;
_CRTALLOC(".CRT$XLZ") PIMAGE_TLS_CALLBACK __xl_z = 0;
+__attribute__((used))
const IMAGE_TLS_DIRECTORY _tls_used = {
(ULONG_PTR) &_tls_start, (ULONG_PTR) &_tls_end,
(ULONG_PTR) &_tls_index, (ULONG_PTR) (&__xl_a+1),
@@ -101,6 +102,7 @@ __dyn_tls_init (HANDLE hDllHandle, DWORD dwReason, LPVOID
lpreserved)
}
const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback = (const
PIMAGE_TLS_CALLBACK) __dyn_tls_init;
+__attribute__((used))
_CRTALLOC(".CRT$XLC") PIMAGE_TLS_CALLBACK __xl_c = (PIMAGE_TLS_CALLBACK)
__dyn_tls_init;
int __cdecl __tlregdtor (_PVFV);
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public