From e3e82b36576c67e84b298073502fc016b39d09b5 Mon Sep 17 00:00:00 2001 From: Olaf Barthel Date: Wed, 9 Mar 2005 21:07:25 +0000 Subject: [PATCH] - Thanks to Joerg Strohmayer, the GCC library build now manages to invoke the library's constructor/destructor functions in a very particular order. That way, you can use constructor/destructor functions in your own code and not have them clash with the library's own functions. git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14871 87f5fb63-7c3d-0410-a384-fd976d0f7a62 --- library/GNUmakefile.os4 | 4 ++-- library/changes | 6 ++++++ library/crtbegin.c | 43 +++++++++++++++++------------------------ library/crtend.c | 7 ++++--- library/macros.h | 33 ++++++++++++++++++++++++------- 5 files changed, 56 insertions(+), 37 deletions(-) diff --git a/library/GNUmakefile.os4 b/library/GNUmakefile.os4 index 4862ffe..a12ec7d 100644 --- a/library/GNUmakefile.os4 +++ b/library/GNUmakefile.os4 @@ -1,5 +1,5 @@ # -# $Id: GNUmakefile.os4,v 1.38 2005-03-09 16:56:04 obarthel Exp $ +# $Id: GNUmakefile.os4,v 1.39 2005-03-09 21:07:20 obarthel Exp $ # # :ts=8 # @@ -113,7 +113,7 @@ WARNINGS = \ INCLUDES = -Iinclude -I. -I$(SDK_INCLUDE) OPTIONS = -D__THREAD_SAFE -DNDEBUG -DUSE_64_BIT_INTS -D__USE_INLINE__ -Wa,-mregnames OPTIMIZE = -O -fomit-frame-pointer -funroll-loops -DEBUG = -g2 +#DEBUG = -g CFLAGS = $(WARNINGS) $(OPTIMIZE) $(DEBUG) $(OPTIONS) $(CODE_TYPE) $(INCLUDES) diff --git a/library/changes b/library/changes index 2685281..da97d02 100644 --- a/library/changes +++ b/library/changes @@ -40,6 +40,12 @@ - Moved a few __delete_semaphore() calls into conditional compilation sections where they should have been in the first place. +- Thanks to Joerg Strohmayer, the GCC library build now manages to + invoke the library's constructor/destructor functions in a + very particular order. That way, you can use constructor/destructor + functions in your own code and not have them clash with the library's + own functions. + c.lib 1.189 (5.3.2005) diff --git a/library/crtbegin.c b/library/crtbegin.c index 8277358..a69a329 100644 --- a/library/crtbegin.c +++ b/library/crtbegin.c @@ -1,5 +1,5 @@ /* - * $Id: crtbegin.c,v 1.4 2005-02-25 10:14:21 obarthel Exp $ + * $Id: crtbegin.c,v 1.5 2005-03-09 21:07:25 obarthel Exp $ * * :ts=4 * @@ -25,22 +25,24 @@ * with a NULL pointer entry and is put at the end of the sections. This way, the init * code can find the global constructor/destructor pointers */ -static void (*__CTOR_LIST__[1]) (void) __attribute__((section(".ctors"))) = { (void *)-1 }; -static void (*__DTOR_LIST__[1]) (void) __attribute__((section(".dtors"))) = { (void *)-1 }; +static void (*__CTOR_LIST__[1]) (void) __attribute__(( used, section(".ctors"), aligned(sizeof(void (*)(void))) )); +static void (*__DTOR_LIST__[1]) (void) __attribute__(( used, section(".dtors"), aligned(sizeof(void (*)(void))) )); /****************************************************************************/ STATIC VOID _do_ctors(void) { - void (**pFuncPtr)(void); + int num_ctors; + int i; - /* Skip the first entry in the list (it's -1 anyway) */ - pFuncPtr = __CTOR_LIST__ + 1; + num_ctors = 0; - /* Call all constructors in forward order */ - while (*pFuncPtr != NULL) - (**pFuncPtr++)(); + for(i = 1 ; __CTOR_LIST__[i] != NULL ; i++) + num_ctors++; + + for(i = 0 ; i < num_ctors ; i++) + __CTOR_LIST__[num_ctors - i](); } /****************************************************************************/ @@ -48,25 +50,16 @@ _do_ctors(void) STATIC VOID _do_dtors(void) { - ULONG i = (ULONG)__DTOR_LIST__[0]; - void (**pFuncPtr)(void); + int num_dtors; + int i; - if (i == ~0UL) - { - /* Find the end of the destructors list */ - i = 1; + num_dtors = 0; - while (__DTOR_LIST__[i] != NULL) - i++; + for(i = 1 ; __DTOR_LIST__[i] != NULL ; i++) + num_dtors++; - /* We're at the NULL entry now. Go back by one */ - i--; - } - - /* Call all destructors in reverse order */ - pFuncPtr = __DTOR_LIST__ + i; - while (i-- > 0) - (**pFuncPtr--)(); + for(i = 0 ; i < num_dtors ; i++) + __DTOR_LIST__[i+1](); } /****************************************************************************/ diff --git a/library/crtend.c b/library/crtend.c index 321336a..78fde73 100644 --- a/library/crtend.c +++ b/library/crtend.c @@ -1,5 +1,5 @@ /* - * $Id: crtend.c,v 1.1.1.1 2004-07-26 16:30:22 obarthel Exp $ + * $Id: crtend.c,v 1.2 2005-03-09 21:07:25 obarthel Exp $ * * :ts=4 * @@ -10,9 +10,10 @@ /****************************************************************************/ -static volatile void (*__CTOR_LIST__[1]) (void) __attribute__((used,section(".ctors"))) = { (void *)0 }; -static volatile void (*__DTOR_LIST__[1]) (void) __attribute__((used,section(".dtors"))) = { (void *)0 }; +static void (*__CTOR_LIST__[1]) (void) __attribute__((used, section(".ctors"), aligned(sizeof(void (*)(void))) )); +static void (*__DTOR_LIST__[1]) (void) __attribute__((used, section(".dtors"), aligned(sizeof(void (*)(void))) )); /****************************************************************************/ #endif /*__amigaos4__ */ + diff --git a/library/macros.h b/library/macros.h index df29887..d5f8cb7 100644 --- a/library/macros.h +++ b/library/macros.h @@ -1,5 +1,5 @@ /* - * $Id: macros.h,v 1.9 2005-02-25 10:14:21 obarthel Exp $ + * $Id: macros.h,v 1.10 2005-03-09 21:07:25 obarthel Exp $ * * :ts=4 * @@ -163,12 +163,31 @@ #endif /* __SASC */ #ifdef __GNUC__ -#define CLIB_CONSTRUCTOR(name) STATIC VOID __attribute__((constructor)) name##_ctor(void) -#define CLIB_DESTRUCTOR(name) STATIC VOID __attribute__((destructor)) name##_dtor(void) -#define PROFILE_CONSTRUCTOR(name) STATIC VOID __attribute__((constructor)) name##_ctor(void) -#define PROFILE_DESTRUCTOR(name) STATIC VOID __attribute__((destructor)) name##_dtor(void) -#define CONSTRUCTOR_SUCCEED() return -#define CONSTRUCTOR_FAIL() exit(RETURN_FAIL) /* ZZZ not a nice thing to do; fix the constructor invocation code! */ +#define CLIB_CONSTRUCTOR(name) \ + STATIC VOID __attribute__((used)) name##_ctor(void); \ + STATIC VOID (*__name##_ctor)(void) __attribute__((used,section(".ctors.00500"))) = name##_ctor; \ + STATIC VOID name##_ctor(void) + +#define CLIB_DESTRUCTOR(name) \ + STATIC VOID __attribute__((used)) name##_dtor(void); \ + STATIC VOID (*__name##_dtor)(void) __attribute__((used,section(".dtors.00500"))) = name##_dtor; \ + STATIC VOID name##_dtor(void) + +#define PROFILE_CONSTRUCTOR(name) \ + STATIC VOID __attribute__((used)) name##_ctor(void); \ + STATIC VOID (*__name##_ctor)(void) __attribute__((used,section(".ctors.00150"))) = name##_ctor; \ + STATIC VOID name##_ctor(void) + +#define PROFILE_DESTRUCTOR(name) \ + STATIC VOID __attribute__((used)) name##_dtor(void); \ + STATIC VOID (*__name##_dtor)(void) __attribute__((used,section(".dtors.00150"))) = name##_dtor; \ + STATIC VOID name##_ctor(void) + +#define CONSTRUCTOR_SUCCEED() \ + return + +#define CONSTRUCTOR_FAIL() \ + exit(RETURN_FAIL) /* ZZZ not a nice thing to do; fix the constructor invocation code! */ #endif /* __GNUC__ */ /****************************************************************************/