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; void On Fri, Aug 18, 2017 at 7:52 PM, Martell Malone <martellmal...@gmail.com> wrote: > In that case I will be back with a patch shortly for you to review. > It might look ugly because of a large __clang__ ifdef block but should work. > > On Fri, Aug 18, 2017 at 7:50 PM, Martin Storsjö <mar...@martin.st> wrote: >> >> On Fri, 18 Aug 2017, Martell Malone wrote: >> >>> David, I also want to remove KEEP (*(.ctors)); which seems to cause a bug >>> when linking in conjunction with KEEP (*(SORT_BY_NAME(.ctors.*))) and our >>> new mingw-w64 markers. I have details in this email on binutils ml. >>> https://sourceware.org/ml/binutils/2017-08/msg00078.html >>> I have to create a minified test case for this to get approval to remove >>> that line. >>> >>> Martin, I am fine with adding a __clang__ guard around the implementation >>> if we want to land it now rather then keeping it out of tree? >>> Kai any objections? >> >> >> That'd be great yeah. Once we can build at least simple C apps without any >> out-of-tree patches, it'll be a much lower barrier for entry for a lot of >> more people. >> >> IIRC Kai at least ok'd the patch once last year. >> >> >> // 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 > > ------------------------------------------------------------------------------ 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