On Fri, 18 Aug 2017, Martell Malone wrote:

Marin, please review :)

commit 726ed8e9b9eea9a2c62c46108da9e014b85dca45
Author: Martell Malone <martellmal...@gmail.com>
Date:   Fri Aug 18 19:59:20 2017 +0100

   crt: Handle .ctors and .dtors within mingw-w64

   When building with clang we currently assume you will be
   linking with llvm's lld or a bleeding edge binutils.
   In future this will become the default method once
   binutils support has settled and bumped a few versions.

diff --git a/mingw-w64-crt/crt/crtdll.c b/mingw-w64-crt/crt/crtdll.c
index 07a18408..85035d49 100644
--- a/mingw-w64-crt/crt/crtdll.c
+++ b/mingw-w64-crt/crt/crtdll.c
@@ -40,6 +40,13 @@ extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z[];
extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a[];
extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z[];

+#ifdef __clang__
+__attribute__ (( __section__ (".ctors"), __used__ ,
aligned(sizeof(void *)))) const void * __CTOR_LIST__ = (void *) -1;
+__attribute__ (( __section__ (".dtors"), __used__ ,
aligned(sizeof(void *)))) const void * __DTOR_LIST__ = (void *) -1;
+__attribute__ (( __section__ (".ctors$zzz"), __used__ ,
aligned(sizeof(void *)))) const void * __CTOR_END__ = (void *) 0;
+__attribute__ (( __section__ (".dtors$zzz"), __used__ ,
aligned(sizeof(void *)))) const void * __DTOR_END__ = (void *) 0;
+#endif
+
/* TLS initialization hook.  */
extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback;

diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index ae37e0fe..e3a84784 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -60,6 +60,13 @@ extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z[];
extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a[];
extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z[];

+#ifdef __clang__
+__attribute__ (( __section__ (".ctors"), __used__ ,
aligned(sizeof(void *)))) const void * __CTOR_LIST__ = (void *) -1;
+__attribute__ (( __section__ (".dtors"), __used__ ,
aligned(sizeof(void *)))) const void * __DTOR_LIST__ = (void *) -1;
+__attribute__ (( __section__ (".ctors$zzz"), __used__ ,
aligned(sizeof(void *)))) const void * __CTOR_END__ = (void *) 0;
+__attribute__ (( __section__ (".dtors$zzz"), __used__ ,
aligned(sizeof(void *)))) const void * __DTOR_END__ = (void *) 0;
+#endif
+
/* TLS initialization hook.  */
extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback;

diff --git a/mingw-w64-crt/crt/gccmain.c b/mingw-w64-crt/crt/gccmain.c
index fc0e3500..bf035d77 100644
--- a/mingw-w64-crt/crt/gccmain.c
+++ b/mingw-w64-crt/crt/gccmain.c
@@ -16,6 +16,31 @@ void __do_global_dtors (void);
void __do_global_ctors (void);
void __main (void);

+#ifdef __clang__
+extern func_ptr __CTOR_END__[];
+extern func_ptr __DTOR_END__[];
+
+void __do_global_dtors (void)
+{
+  static func_ptr *p = __DTOR_LIST__ + 1;
+  while(p < __DTOR_END__) {
+    if (*p) (*(p)) ();
+      p++;
+  }
+}
+
+void __do_global_ctors (void)
+{
+  static func_ptr *p = __CTOR_END__ - 1;
+  while(p > __CTOR_LIST__) {
+    if (*p) (*(p)) ();
+      p--;
+  }
+  atexit (__do_global_dtors);
+}
+
+#else
+
void
__do_global_dtors (void)
{
@@ -47,6 +72,8 @@ __do_global_ctors (void)
  atexit (__do_global_dtors);
}

+#endif
+
static int initialized = 0;


LGTM, although it might be good to adjust it in the way David suggested (avoiding the direct comparisons with *_END__, which is technically undefined behaviour, and just checking the sentinel element value instead).

// Martin

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to