Can I land this in master?

Best,
Martell

On Fri, Aug 18, 2017 at 8:53 PM, Martell Malone <[email protected]>
wrote:

> I'm not so sure; a normal C struct or array would never have a global
>> symbol defined in the middle or at the end, so a smart compiler might
>> some day assume that if you take a symbol and decrement it, it's an
>> invalid pointer.  I don't really want to argue much more than that,
>> and I don't have a specific thing to point to in the C standard.
>
> I can't comment on the specifics of the spec but I would say that if this
> did happen in future then many toolchains in the wild would break with that
> optimisation, either resulting in a revert of that new pass or addition of
> an exception for __*_END__ or all those toolchains will have to be changed.
> It is highly unlikely we will ever face this problem and we won't face it
> alone regardless.
>
>
>
> On Fri, Aug 18, 2017 at 8:46 PM, Martell Malone <[email protected]>
> wrote:
>
>> Attached updated patch.
>>
>> I was itching to invert the .ctors markers so that I could do while (*p)
>> instead of while (*p != (func_ptr) -1)
>> but that would create another binutils breakage so I left it.
>>
>> Let me know how you all want to proceed :)
>>
>> On Fri, Aug 18, 2017 at 8:39 PM, David Grayson <[email protected]>
>> wrote:
>>
>>> On Fri, Aug 18, 2017 at 12:26 PM, Martin Storsjö <[email protected]>
>>> wrote:
>>> > Yup, that resolves the concern.
>>> >
>>> > As long as you just use one or the other of *_LIST__ or *_END__ in each
>>> > function and use the sentinel values instead of comparing the
>>> pointers, it
>>> > should be fine. Otherwise the compiler is free to regard the
>>> comparison as
>>> > nonsense.
>>> >
>>> > // Martin
>>>
>>> I'm not so sure; a normal C struct or array would never have a global
>>> symbol defined in the middle or at the end, so a smart compiler might
>>> some day assume that if you take a symbol and decrement it, it's an
>>> invalid pointer.  I don't really want to argue much more than that,
>>> and I don't have a specific thing to point to in the C standard.
>>>
>>> --David
>>>
>>> ------------------------------------------------------------
>>> ------------------
>>> 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 beb5ab5b7b18e82cc2d73ed23639cf9aa1a8ec6f 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..54cbf02b 100644
--- a/mingw-w64-crt/crt/gccmain.c
+++ b/mingw-w64-crt/crt/gccmain.c
@@ -28,6 +28,23 @@ __do_global_dtors (void)
     }
 }
 
+#ifdef __clang__
+extern func_ptr __CTOR_END__[];
+extern func_ptr __DTOR_END__[];
+
+void __do_global_ctors (void)
+{
+  static func_ptr *p = __CTOR_END__ - 1;
+  while (*p != (func_ptr) -1) {
+    (*(p))();
+    p--;
+  }
+  atexit (__do_global_dtors);
+}
+
+#else
+// old method that iterates the list twice because old linker scripts do not 
have __CTOR_END__
+
 void
 __do_global_ctors (void)
 {
@@ -47,6 +64,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