>
> 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).

I have seen __CTOR_END__ used and counted backwards by many crt startups.
example https://code.woboq.org/gcc/libgcc/config/m32r/initfini.c.html
The only difference here is that is doesn't do a compare with __CTOR_LIST__
and looks for the -1 marker at the beginning.
I could do that instead if this resolves the same concern?
I would prefer faster startup times instead of iterating through the list
up and back.
By definition the linker script should also provide __CTOR_END__ as
described here
https://sourceware.org/binutils/docs-2.25/ld/Output-Section-Keywords.html

Attached is a txt version.

On Fri, Aug 18, 2017 at 8:10 PM, Martin Storsjö <[email protected]> wrote:

> On Fri, 18 Aug 2017, Martell Malone wrote:
>
> Marin, please review :)
>>
>> commit 726ed8e9b9eea9a2c62c46108da9e014b85dca45
>> Author: Martell Malone <[email protected]>
>> 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
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
>
From 726ed8e9b9eea9a2c62c46108da9e014b85dca45 Mon Sep 17 00:00:00 2001
From: Martell Malone <[email protected]>
Date: Fri, 18 Aug 2017 19:59:20 +0100
Subject: [PATCH] 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;
 
 void
-- 
2.14.1

------------------------------------------------------------------------------
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
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to