For some targets __dso_handle will never be used, and its definition in crtstuff.c can cause a domino effect resulting in the size of the final executable increasing much more than just the size of this piece of data.
For msp430, CRT functions to initialize global data are only included if there is global data to initialize and it is more than feasible to write functional programs which do not use any global data. In these cases, the definition of __dso_handle will cause code size to increase by around 100 bytes as library code to initialize data is linked into the executable. Removing __dso_handle can therefore save on code size. This does require the target to NOT use __cxa_atexit, so either TARGET_CXX_USE_ATEXIT_FOR_CXA_ATEXIT must return true or -fno-use-cxa-atexit must be used as a target flag when building GCC. This is because __cxa_atexit includes functionality to unload dynamic shared objects and so cp/decl.c will create a reference to __dso_handle to facilitate this in programs with static destructors.
>From 7bc0971d2936ebe71e7b7d3d805cf1bbf9f9f5af Mon Sep 17 00:00:00 2001 From: Jozef Lawrynowicz <joze...@mittosystems.com> Date: Mon, 4 Nov 2019 17:38:13 +0000 Subject: [PATCH 3/3] libgcc: Implement TARGET_LIBGCC_REMOVE_DSO_HANDLE gcc/ChangeLog: 2019-11-06 Jozef Lawrynowicz <joze...@mittosystems.com> * doc/tm.texi: Regenerate. * doc/tm.texi.in: Define TARGET_LIBGCC_REMOVE_DSO_HANDLE. libgcc/ChangeLog: 2019-11-06 Jozef Lawrynowicz <joze...@mittosystems.com> * crtstuff.c: Don't declare __dso_handle if TARGET_LIBGCC_REMOVE_DSO_HANDLE is defined. --- gcc/doc/tm.texi | 11 +++++++++++ gcc/doc/tm.texi.in | 11 +++++++++++ libgcc/crtstuff.c | 2 ++ 3 files changed, 24 insertions(+) diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index cd9aed9874f..89ef0a8e616 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -11425,6 +11425,17 @@ from shared libraries (DLLs). You need not define this macro if it would always evaluate to zero. @end defmac +@defmac TARGET_LIBGCC_REMOVE_DSO_HANDLE +Define this macro if, for targets where dynamic shared objects cannot be used, +the declaration of @samp{__dso_handle} in libgcc/crtstuff.c can be removed. + +If this macro is defined, __cxa_atexit must be disabled at GCC configure time, +otherwise references to __dso_handle will be created when the middle-end +creates destructors for static objects. + +This macro is undefined by default. +@end defmac + @deftypefn {Target Hook} {rtx_insn *} TARGET_MD_ASM_ADJUST (vec<rtx>& @var{outputs}, vec<rtx>& @var{inputs}, vec<const char *>& @var{constraints}, vec<rtx>& @var{clobbers}, HARD_REG_SET& @var{clobbered_regs}) This target hook may add @dfn{clobbers} to @var{clobbers} and @var{clobbered_regs} for any hard regs the port wishes to automatically diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index 2739e9ceec5..3a211a7658a 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -7853,6 +7853,17 @@ from shared libraries (DLLs). You need not define this macro if it would always evaluate to zero. @end defmac +@defmac TARGET_LIBGCC_REMOVE_DSO_HANDLE +Define this macro if, for targets where dynamic shared objects cannot be used, +the declaration of @samp{__dso_handle} in libgcc/crtstuff.c can be removed. + +If this macro is defined, __cxa_atexit must be disabled at GCC configure time, +otherwise references to __dso_handle will be created when the middle-end +creates destructors for static objects. + +This macro is undefined by default. +@end defmac + @hook TARGET_MD_ASM_ADJUST @defmac MATH_LIBRARY diff --git a/libgcc/crtstuff.c b/libgcc/crtstuff.c index 0b0a0b865fe..d1d17f959d3 100644 --- a/libgcc/crtstuff.c +++ b/libgcc/crtstuff.c @@ -335,6 +335,7 @@ register_tm_clones (void) in one DSO or the main program is not used in another object. The dynamic linker takes care of this. */ +#ifndef TARGET_LIBGCC_REMOVE_DSO_HANDLE #ifdef TARGET_LIBGCC_SDATA_SECTION extern void *__dso_handle __attribute__ ((__section__ (TARGET_LIBGCC_SDATA_SECTION))); #endif @@ -346,6 +347,7 @@ void *__dso_handle = &__dso_handle; #else void *__dso_handle = 0; #endif +#endif /* TARGET_LIBGCC_REMOVE_DSO_HANDLE */ /* The __cxa_finalize function may not be available so we use only a weak declaration. */ -- 2.17.1