This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Fwd: [PATCH] COFF Compliant .ctors and .dtors sections
- From: Martell Malone <martellmalone at gmail dot com>
- To: binutils at sourceware dot org
- Cc: Kai Tietz <ktietz70 at gmail dot com>
- Date: Sat, 5 Aug 2017 02:12:53 +0100
- Subject: Fwd: [PATCH] COFF Compliant .ctors and .dtors sections
- Authentication-results: sourceware.org; auth=none
- References: <CAOpLJ7wg8097ud3uPtAftcGq++-M=M4GdWgC0SKJo0CB5Hk27Q@mail.gmail.com>
Hi all,
I am on a quest to have mingw-w64 work across msvc link, llvm lld and
binutils ld seamlessly from the one build.
While MS have their own ctors and dtors like section it is still
possible to use our gnu style sections within the COFF spec.
The linker script currently injects code into the section with a
marker at the start which is -1 and another marker at the end of 0.
Other Linkers have no notion of this marker but it is possible to have
the crt create the markers itself.
I would like to propose the following patch to go with a pair that is
on the mingw-w64 mailing list which implements this on the crt end.
Best,
Martell
>From d69a7d065d4baed6f72c456594d47976128d1e4e Mon Sep 17 00:00:00 2001
From: Martell Malone <martellmalone@gmail.com>
Date: Sat, 5 Aug 2017 01:17:49 +0100
Subject: [PATCH] update to latest mingw-w64
diff --git a/ld/scripttempl/pe.sc b/ld/scripttempl/pe.sc
index 9f7b2b220e..ea085ceadc 100644
--- a/ld/scripttempl/pe.sc
+++ b/ld/scripttempl/pe.sc
@@ -97,10 +97,8 @@ SECTIONS
${RELOCATING+ *(.gnu.linkonce.t.*)}
${RELOCATING+*(.glue_7t)}
${RELOCATING+*(.glue_7)}
- ${CONSTRUCTING+ ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
- LONG (-1);*(.ctors); *(.ctor); *(SORT(.ctors.*)); LONG (0); }
- ${CONSTRUCTING+ ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
- LONG (-1); *(.dtors); *(.dtor); *(SORT(.dtors.*)); LONG (0); }
+ ${CONSTRUCTING+*(SORT(.ctors.*))}
+ ${CONSTRUCTING+*(SORT(.dtors.*))}
${RELOCATING+ KEEP (*(.fini))}
/* ??? Why is .gcc_exc here? */
${RELOCATING+ *(.gcc_exc)}
diff --git a/ld/scripttempl/pep.sc b/ld/scripttempl/pep.sc
index 57eb75c51c..dfd5a2c536 100644
--- a/ld/scripttempl/pep.sc
+++ b/ld/scripttempl/pep.sc
@@ -98,18 +98,8 @@ SECTIONS
${RELOCATING+*(.glue_7t)}
${RELOCATING+*(.glue_7)}
${CONSTRUCTING+. = ALIGN(8);}
- ${CONSTRUCTING+ ___CTOR_LIST__ = .; __CTOR_LIST__ = . ;
- LONG (-1); LONG (-1);
- KEEP (*(.ctors));
- KEEP (*(.ctor));
- KEEP (*(SORT(.ctors.*)));
- LONG (0); LONG (0); }
- ${CONSTRUCTING+ ___DTOR_LIST__ = .; __DTOR_LIST__ = . ;
- LONG (-1); LONG (-1);
- KEEP (*(.dtors));
- KEEP (*(.dtor));
- KEEP (*(SORT(.dtors.*)));
- LONG (0); LONG (0); }
+ ${CONSTRUCTING+*(SORT(.ctors.*))}
+ ${CONSTRUCTING+*(SORT(.dtors.*))}
${RELOCATING+ KEEP (*(.fini))}
/* ??? Why is .gcc_exc here? */
${RELOCATING+ *(.gcc_exc)}
--
2.13.4
I will also attach the mingw-w64 patch to this thread so reviewers can
see how this in full context.
>From ac04d10b33e9738afd11969bbc09e2c9077d7553 Mon Sep 17 00:00:00 2001
From: Martell Malone <martellmalone@gmail.com>
Date: Fri, 5 Aug 2016 20:09:41 -0700
Subject: [PATCH] Handle __CTOR_LIST__ within mingw-w64
diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index ae37e0fe..4e8cda5a 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -60,6 +60,11 @@ extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z[];
extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a[];
extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z[];
+__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;
+
/* 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..4363fd6a 100644
--- a/mingw-w64-crt/crt/gccmain.c
+++ b/mingw-w64-crt/crt/gccmain.c
@@ -11,6 +11,8 @@
typedef void (*func_ptr) (void);
extern func_ptr __CTOR_LIST__[];
extern func_ptr __DTOR_LIST__[];
+extern func_ptr __CTOR_END__[];
+extern func_ptr __DTOR_END__[];
void __do_global_dtors (void);
void __do_global_ctors (void);
@@ -20,30 +22,20 @@ void
__do_global_dtors (void)
{
static func_ptr *p = __DTOR_LIST__ + 1;
-
- while (*p)
- {
- (*(p)) ();
+ while(p < __DTOR_END__) {
+ if (*p) (*(p)) ();
p++;
- }
+ }
}
void
__do_global_ctors (void)
{
- unsigned long nptrs = (unsigned long) (ptrdiff_t) __CTOR_LIST__[0];
- unsigned long i;
-
- if (nptrs == (unsigned long) -1)
- {
- for (nptrs = 0; __CTOR_LIST__[nptrs + 1] != 0; nptrs++);
- }
-
- for (i = nptrs; i >= 1; i--)
- {
- __CTOR_LIST__[i] ();
- }
-
+ static func_ptr *p = __CTOR_END__ - 1;
+ while(p > __CTOR_LIST__) {
+ if (*p) (*(p)) ();
+ p--;
+ }
atexit (__do_global_dtors);
}
--
2.13.4
I also CC'd kai the author of mingw-w64.