On Wed, 11 Dec 2019 12:19:41 +0000
Jozef Lawrynowicz <joze...@mittosystems.com> wrote:

> On Mon, 9 Dec 2019 15:28:25 +0000
> Jozef Lawrynowicz <joze...@mittosystems.com> wrote:
> 
> > On Sat, 07 Dec 2019 11:40:33 -0700
> > Jeff Law <l...@redhat.com> wrote:
> >   
> > > On Fri, 2019-11-29 at 21:00 +0000, Jozef Lawrynowicz wrote:    
> > > > The attached patch consolidates some configuration tweaks I
> > > > previously submitted
> > > > as modifications to the msp430-elf target into a new target called
> > > > "msp430-elfbare" i.e. "bare-metal".
> > > > 
> > > > MSP430: Disable TM clone registry by default
> > > >   https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00550.html
> > > > MSP430: Disable __cxa_atexit
> > > >   https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00552.html
> > > > 
> > > > The patches tweak the CRT code to achieve the smallest possible code
> > > > size, 
> > > > and rely on some additional generic tweaks to crtstuff.c.
> > > > 
> > > > I did submit these tweaks a while ago, but I didn't get any feedback,
> > > > however even if they are acceptable I suspect it is too late for GCC-
> > > > 10 anyway:
> > > > libgcc: Dont define __do_global_dtors_aux if it will be empty
> > > >   https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00417.html
> > > > libgcc: Implement TARGET_LIBGCC_REMOVE_DSO_HANDLE
> > > >   https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00418.html
> > > > 
> > > > (The second one is a bit hacky, but without some way of removing the
> > > > __dso_handle declaration, we end up with 150 bytes of unnecessary
> > > > code in some
> > > > programs.)
> > > > 
> > > > So for this patch crtstuff.c was copied to the msp430 subdirectory
> > > > and the
> > > > changes were made to that target specific version.
> > > > 
> > > > Tiny program size can now be achieved by configuring gcc for msp430-
> > > > elfbare.
> > > > 
> > > > For example in an "empty main" program which loops forever:
> > > >   msp430-elfbare @ -Os:
> > > >      text    data     bss     dec     hex filename
> > > >        14       0       0      14       e a.out
> > > >   msp430-elf @ -Os:
> > > >      text    data     bss     dec     hex filename
> > > >       270       6       2     278     116 a.out
> > > > 
> > > > Successfully regtested msp430-elfbare vs msp430-elf.
> > > > 
> > > > Ok to apply?
> > > > 
> > > > P.S. This patch relies on the -fno-exceptions multilib patch
> > > > submitted here:
> > > > https://gcc.gnu.org/ml/gcc-patches/2019-11/msg02523.html
> > > > 
> > > > P.P.S. This requires some minor configury tweaks to Newlib and GDB of
> > > > the form:
> > > > -  msp430*-*-elf)
> > > > +  msp430-*-elf*)      
> > >     
> > > > I'll apply these changes if the patch is accepted.
> > > > From cff4611855d838315e793d45256de5fc8eeefafe Mon Sep 17 00:00:00
> > > > 2001
> > > > From: Jozef Lawrynowicz <joze...@mittosystems.com>
> > > > Date: Mon, 25 Nov 2019 19:41:05 +0000
> > > > Subject: [PATCH] MSP430: Add new msp430-elfbare target
> > > > 
> > > > contrib/ChangeLog:
> > > > 
> > > > 2019-11-29  Jozef Lawrynowicz  <joze...@mittosystems.com>
> > > > 
> > > >         * config-list.mk: Add msp430-elfbare.
> > > > 
> > > > gcc/ChangeLog:
> > > > 
> > > > 2019-11-29  Jozef Lawrynowicz  <joze...@mittosystems.com>
> > > > 
> > > >         * config.gcc: s/msp430*-*-*/msp430-*-*.
> > > >         Handle msp430-*-elfbare.
> > > >         * config/msp430/msp430-devices.c (TARGET_SUBDIR): Define.
> > > >         (_MSPMKSTR): Define.
> > > >         (__MSPMKSTR): Define.
> > > >         (rest_of_devices_path): Use TARGET_SUBDIR value in string.
> > > >         * config/msp430/msp430.c (msp430_option_override): Error if
> > > >         -fuse-cxa-atexit is used when it has been disabled at configure
> > > > time.
> > > >         * config/msp430/t-msp430: Define TARGET_SUBDIR when building
> > > >         msp430-devices.o.
> > > >         * doc/install.texi: Document msp430-*-elf and msp430-*-elfbare.
> > > >         * doc/invoke.texi: Update documentation about which path
> > > > devices.csv is
> > > >         searched for.
> > > > 
> > > > gcc/testsuite/ChangeLog:
> > > > 
> > > > 2019-11-29  Jozef Lawrynowicz  <joze...@mittosystems.com>
> > > > 
> > > >         * g++.dg/init/dso_handle1.C: Require cxa_atexit support.
> > > >         * g++.dg/init/dso_handle2.C: Likewise.
> > > >         * g++.dg/other/cxa-atexit1.C: Likewise.
> > > >         * gcc.target/msp430/msp430.exp: Update csv-using-installed.c
> > > > test to
> > > >         handle msp430-elfbare configuration.
> > > > 
> > > > libgcc/ChangeLog:
> > > > 
> > > > 2019-11-29  Jozef Lawrynowicz  <joze...@mittosystems.com>
> > > > 
> > > >         * config.host: Use t-msp430-elfbare-crtstuff Makefile fragment
> > > > when GCC
> > > >         is configured for the msp430-elfbare target.
> > > >         * config/msp430/msp430-elfbare-crtstuff.c: New file.
> > > >         * config/msp430/t-msp430: Remove Makefile rules for object
> > > > files
> > > >         built from crtstuff.c
> > > >         * config/msp430/t-msp430-crtstuff: New file.
> > > >         * config/msp430/t-msp430-elfbare-crtstuff: New file.
> > > >         * configure: Regenerate.
> > > >         * configure.ac: Disable TM clone registry by default for
> > > >         msp430-elfbare.      
> > > OK.   I probably would have tried to avoid msp430-elfbare-crtstuff, but
> > > it's not a huge wart IMHO.    
> > 
> > If we get the __dso_handle removal into the generic libgcc/crtstuff.c those
> > changes won't be necessary.  
> 
> I've attached an amended patch assuming the removal of __dso_handle
> when !DEFAULT_USE_CXA_ATEXIT is approved. Differences vs the original patch 
> are
> only in libgcc/, removed the custom crtstuff implementation.

Missed the attachment...
> 
> Thanks,
> Jozef
> > 
> > Did you get a chance to look at "Add -fno-exceptions multilib" -
> > https://gcc.gnu.org/ml/gcc-patches/2019-11/msg02523.html
> > 
> > It is the cumulative effect of these patches that gives the good code size
> > results, unfortunately without them all there isn't a significant code size
> > improvement.
> > 
> > Thanks,
> > Jozef  
> > > 
> > > Jeff    
> > > >       
> > >     
> 

>From 50d7b024bd05f94ae6b85ae776043f9c3bc86939 Mon Sep 17 00:00:00 2001
From: Jozef Lawrynowicz <joze...@mittosystems.com>
Date: Mon, 25 Nov 2019 19:41:05 +0000
Subject: [PATCH] MSP430: Add new msp430-elfbare target

contrib/ChangeLog:

2019-12-11  Jozef Lawrynowicz  <joze...@mittosystems.com>

	* config-list.mk: Add msp430-elfbare.

gcc/ChangeLog:

2019-12-11  Jozef Lawrynowicz  <joze...@mittosystems.com>

	* config.gcc: s/msp430*-*-*/msp430-*-*.
	Handle msp430-*-elfbare.
	* config/msp430/msp430-devices.c (TARGET_SUBDIR): Define.
	(_MSPMKSTR): Define.
	(__MSPMKSTR): Define.
	(rest_of_devices_path): Use TARGET_SUBDIR value in string.
	* config/msp430/msp430.c (msp430_option_override): Error if
	-fuse-cxa-atexit is used when it has been disabled at configure time.
	* config/msp430/t-msp430: Define TARGET_SUBDIR when building
	msp430-devices.o.
	* doc/install.texi: Document msp430-*-elf and msp430-*-elfbare.
	* doc/invoke.texi: Update documentation about which path devices.csv is
	searched for.

gcc/testsuite/ChangeLog:

2019-12-11  Jozef Lawrynowicz  <joze...@mittosystems.com>

	* g++.dg/init/dso_handle1.C: Require cxa_atexit support.
	* g++.dg/init/dso_handle2.C: Likewise.
	* g++.dg/other/cxa-atexit1.C: Likewise.
	* gcc.target/msp430/msp430.exp: Update csv-using-installed.c test to
	handle msp430-elfbare configuration.

libgcc/ChangeLog:

2019-12-11  Jozef Lawrynowicz  <joze...@mittosystems.com>

	* config.host: s/msp430*-*-elf/msp430-*-elf*.
	Override default "extra_parts" variable.
	* configure: Regenerate.
	* configure.ac: Disable TM clone registry by default for
	msp430-elfbare.
---
 contrib/config-list.mk                     |  2 +-
 gcc/config.gcc                             | 14 +++++++++++++-
 gcc/config/msp430/msp430-devices.c         | 17 ++++++++++++++++-
 gcc/config/msp430/msp430.c                 | 10 ++++++++++
 gcc/config/msp430/t-msp430                 |  2 +-
 gcc/doc/install.texi                       | 16 +++++++++++++++-
 gcc/doc/invoke.texi                        |  4 ++--
 gcc/testsuite/g++.dg/init/dso_handle1.C    |  1 +
 gcc/testsuite/g++.dg/init/dso_handle2.C    |  1 +
 gcc/testsuite/g++.dg/other/cxa-atexit1.C   |  1 +
 gcc/testsuite/gcc.target/msp430/msp430.exp |  8 +++++---
 libgcc/config.host                         |  4 ++--
 libgcc/configure                           |  9 +++++++++
 libgcc/configure.ac                        |  8 ++++++++
 14 files changed, 85 insertions(+), 12 deletions(-)

diff --git a/contrib/config-list.mk b/contrib/config-list.mk
index a5f5d7bbd5a..d154286a497 100644
--- a/contrib/config-list.mk
+++ b/contrib/config-list.mk
@@ -68,7 +68,7 @@ LIST = aarch64-elf aarch64-linux-gnu aarch64-rtems \
   mipsel-elf mips64-elf mips64vr-elf mips64orion-elf mips-rtems \
   mips-wrs-vxworks mipstx39-elf mmix-knuth-mmixware mn10300-elf moxie-elf \
   moxie-uclinux moxie-rtems \
-  msp430-elf \
+  msp430-elf msp430-elfbare \
   nds32le-elf nds32be-elf \
   nios2-elf nios2-linux-gnu nios2-rtems \
   nvptx-none \
diff --git a/gcc/config.gcc b/gcc/config.gcc
index 5aa0130135f..9802f436e06 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -2624,7 +2624,7 @@ mn10300-*-*)
 	use_collect2=no
 	use_gcc_stdint=wrap
 	;;
-msp430*-*-*)
+msp430-*-*)
 	tm_file="dbxelf.h elfos.h newlib-stdint.h ${tm_file}"
 	c_target_objs="msp430-c.o"
 	cxx_target_objs="msp430-c.o"
@@ -2637,6 +2637,18 @@ msp430*-*-*)
 	if test x${disable_initfini_array} != xyes; then
 		gcc_cv_initfini_array=yes
 	fi
+	case ${target} in
+	  msp430-*-elfbare)
+	    # __cxa_atexit increases code size, and we don't need to support
+	    # dynamic shared objects on MSP430, so regular Newlib atexit is a
+	    # fine replacement as it also supports registration of more than 32
+	    # functions.
+	    default_use_cxa_atexit=no
+	    # This target does not match the generic *-*-elf case above which
+	    # sets use_gcc_stdint=wrap, so explicitly set it here.
+	    use_gcc_stdint=wrap
+	    ;;
+	esac
 	;;
 nds32*-*-*)
 	target_cpu_default="0"
diff --git a/gcc/config/msp430/msp430-devices.c b/gcc/config/msp430/msp430-devices.c
index 600a111b517..b379bb2b1d4 100644
--- a/gcc/config/msp430/msp430-devices.c
+++ b/gcc/config/msp430/msp430-devices.c
@@ -71,8 +71,23 @@ msp430_dirname (char *path)
   return path;
 }
 
+/* We need to support both the msp430-elf and msp430-elfbare target aliases.
+   gcc/config/msp430/t-msp430 will define TARGET_SUBDIR to the target_subdir
+   Makefile variable, which will evaluate to the correct subdirectory that
+   needs to be searched for devices.csv.  */
+#ifndef TARGET_SUBDIR
+#define TARGET_SUBDIR msp430-elf
+#endif
+
+#define _MSPMKSTR(x) __MSPMKSTR(x)
+#define __MSPMKSTR(x) #x
+
 /* devices.csv path from the toolchain root.  */
-static const char rest_of_devices_path[] = "/msp430-elf/include/devices/";
+static const char rest_of_devices_path[] =
+  "/" _MSPMKSTR (TARGET_SUBDIR) "/include/devices/";
+
+#undef _MSPMKSTR
+#undef __MSPMKSTR
 
 /* "The default value of GCC_EXEC_PREFIX is prefix/lib/gcc". Strip lib/gcc
    from GCC_EXEC_PREFIX to get the path to the installed toolchain.  */
diff --git a/gcc/config/msp430/msp430.c b/gcc/config/msp430/msp430.c
index 4ebdcf51e8b..cce41011aef 100644
--- a/gcc/config/msp430/msp430.c
+++ b/gcc/config/msp430/msp430.c
@@ -288,6 +288,16 @@ msp430_option_override (void)
   if (TARGET_OPT_SPACE && optimize < 3)
     optimize_size = 1;
 
+#if !DEFAULT_USE_CXA_ATEXIT
+  /* For some configurations, we use atexit () instead of __cxa_atexit () by
+     default to save on code size and remove the declaration of __dso_handle
+     from the CRT library.
+     Configuring GCC with --enable-__cxa-atexit re-enables it by defining
+     DEFAULT_USE_CXA_ATEXIT to 1.  */
+  if (flag_use_cxa_atexit)
+    error ("%<-fuse-cxa-atexit%> is not supported for msp430-elf");
+#endif
+
 #ifndef HAVE_NEWLIB_NANO_FORMATTED_IO
   if (TARGET_TINY_PRINTF)
     error ("GCC must be configured with %<--enable-newlib-nano-formatted-io%> "
diff --git a/gcc/config/msp430/t-msp430 b/gcc/config/msp430/t-msp430
index e180ce3efdb..d481696220a 100644
--- a/gcc/config/msp430/t-msp430
+++ b/gcc/config/msp430/t-msp430
@@ -24,7 +24,7 @@ driver-msp430.o: $(srcdir)/config/msp430/driver-msp430.c \
 
 msp430-devices.o: $(srcdir)/config/msp430/msp430-devices.c \
 	$(CONFIG_H) $(SYSTEM_H) coretypes.h $(TM_H)
-	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) $(INCLUDES) $<
+	$(COMPILER) -c $(ALL_COMPILERFLAGS) $(ALL_CPPFLAGS) -DTARGET_SUBDIR=$(target_subdir) $(INCLUDES) $<
 
 # Enable multilibs:
 
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index 19d649ccc83..656410afff4 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -4330,10 +4330,24 @@ The moxie processor.
 <hr />
 @end html
 @anchor{msp430-x-elf}
-@heading msp430-*-elf
+@heading msp430-*-elf*
 TI MSP430 processor.
 This configuration is intended for embedded systems.
 
+@samp{msp430-*-elf} is the standard configuration with most GCC
+features enabled by default.
+
+@samp{msp430-*-elfbare} is tuned for a bare-metal environment, and disables
+features related to shared libraries and other functionality not used for
+this device.  This reduces code and data usage of the GCC libraries, resulting
+in a minimal run-time environment by default.
+
+Features disabled by default include:
+@itemize
+@item transactional memory
+@item __cxa_atexit
+@end itemize
+
 @html
 <hr />
 @end html
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index af3c7f2b910..413b7afd0b9 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -23302,8 +23302,8 @@ this directory for devices.csv.  If devices.csv is found, this directory will
 also be registered as an include path, and linker library path.  Header files
 and linker scripts in this directory can therefore be used without manually
 specifying @code{-I} and @code{-L} on the command line.
-@item The @samp{msp430-elf/include/devices} directory
-Finally, GCC will examine @samp{msp430-elf/include/devices} from the
+@item The @samp{msp430-elf@{,bare@}/include/devices} directory
+Finally, GCC will examine @samp{msp430-elf@{,bare@}/include/devices} from the
 toolchain root directory.  This directory does not exist in a default
 installation, but if the user has created it and copied @samp{devices.csv}
 there, then the MCU data will be read.  As above, this directory will
diff --git a/gcc/testsuite/g++.dg/init/dso_handle1.C b/gcc/testsuite/g++.dg/init/dso_handle1.C
index 97f67cad8f4..0377c4ef7c0 100644
--- a/gcc/testsuite/g++.dg/init/dso_handle1.C
+++ b/gcc/testsuite/g++.dg/init/dso_handle1.C
@@ -1,6 +1,7 @@
 // PR c++/17042
 // { dg-do assemble }
 /* { dg-require-weak "" } */
+// { dg-require-effective-target cxa_atexit }
 // { dg-options "-fuse-cxa-atexit" }
 
 struct A
diff --git a/gcc/testsuite/g++.dg/init/dso_handle2.C b/gcc/testsuite/g++.dg/init/dso_handle2.C
index b219dc02611..a4daaf3f7da 100644
--- a/gcc/testsuite/g++.dg/init/dso_handle2.C
+++ b/gcc/testsuite/g++.dg/init/dso_handle2.C
@@ -1,4 +1,5 @@
 // PR c++/58846
+// { dg-require-effective-target cxa_atexit }
 // { dg-options "-fuse-cxa-atexit" }
 
 extern "C" { char* __dso_handle; }
diff --git a/gcc/testsuite/g++.dg/other/cxa-atexit1.C b/gcc/testsuite/g++.dg/other/cxa-atexit1.C
index a51f3340142..b22911db2e0 100644
--- a/gcc/testsuite/g++.dg/other/cxa-atexit1.C
+++ b/gcc/testsuite/g++.dg/other/cxa-atexit1.C
@@ -1,4 +1,5 @@
 // { dg-do compile }
+// { dg-require-effective-target cxa_atexit }
 // { dg-options "-O2 -fuse-cxa-atexit" }
 
 # 1 "cxa-atexit1.C"
diff --git a/gcc/testsuite/gcc.target/msp430/msp430.exp b/gcc/testsuite/gcc.target/msp430/msp430.exp
index 37586612b9a..42dc91136c9 100644
--- a/gcc/testsuite/gcc.target/msp430/msp430.exp
+++ b/gcc/testsuite/gcc.target/msp430/msp430.exp
@@ -141,18 +141,20 @@ proc msp430_device_permutations_runtest { tests } {
 }
 
 
-# Return $TOOLCHAIN_ROOT/msp430-elf/include/devices/
+# Return $TOOLCHAIN_ROOT/$target_alias/include/devices/
+# target_alias is expected to be either msp430-elf or msp430-elfbare.
 proc get_installed_device_data_path { } {
+    global target_alias
     set compiler [lindex [regexp -all -inline {\S+} \
 	[board_info [target_info name] compiler]] 0]
     # $compiler is actually a file, but normalize will still get us the desired
     # result.
     return [file normalize \
-	"$compiler/../../msp430-elf/include/devices/devices.csv"]
+	"$compiler/../../$target_alias/include/devices/devices.csv"]
 }
 
 # If the devices.csv is installed in
-# $TOOLCHAIN_ROOT/msp430-elf/include/devices/, rename it so it doesn't
+# $TOOLCHAIN_ROOT/$target_alias/include/devices/, rename it so it doesn't
 # interfere with the hard-coded device data tests.
 proc msp430_hide_installed_devices_data { } {
     set devices_path [get_installed_device_data_path]
diff --git a/libgcc/config.host b/libgcc/config.host
index 56868719fc8..efcf5f03f50 100644
--- a/libgcc/config.host
+++ b/libgcc/config.host
@@ -1043,9 +1043,9 @@ moxie-*-elf | moxie-*-moxiebox* | moxie-*-uclinux* | moxie-*-rtems*)
 	tmake_file="$tmake_file moxie/t-moxie t-softfp-sfdf t-softfp-excl t-softfp"
 	extra_parts="$extra_parts crti.o crtn.o crtbegin.o crtend.o"
 	;;
-msp430*-*-elf)
+msp430-*-elf*)
 	tmake_file="$tm_file t-crtstuff t-fdpbit msp430/t-msp430"
-	extra_parts="$extra_parts crtbegin_no_eh.o crtend_no_eh.o"
+	extra_parts="crtbegin.o crtend.o crtbegin_no_eh.o crtend_no_eh.o"
 	extra_parts="$extra_parts libmul_none.a libmul_16.a libmul_32.a libmul_f5.a"
 	;;
 nds32*-linux*)
diff --git a/libgcc/configure b/libgcc/configure
index 117e9c97e57..97cbad3be56 100755
--- a/libgcc/configure
+++ b/libgcc/configure
@@ -4964,6 +4964,15 @@ if test "$enable_tm_clone_registry" = no; then
   use_tm_clone_registry=-DUSE_TM_CLONE_REGISTRY=0
 fi
 
+else
+
+use_tm_clone_registry=
+case $target in
+  msp430*elfbare)
+   use_tm_clone_registry=-DUSE_TM_CLONE_REGISTRY=0
+   ;;
+esac
+
 fi
 
 
diff --git a/libgcc/configure.ac b/libgcc/configure.ac
index f63c5e736e5..2d22f05486d 100644
--- a/libgcc/configure.ac
+++ b/libgcc/configure.ac
@@ -268,6 +268,14 @@ use_tm_clone_registry=
 if test "$enable_tm_clone_registry" = no; then
   use_tm_clone_registry=-DUSE_TM_CLONE_REGISTRY=0
 fi
+],
+[
+use_tm_clone_registry=
+case $target in
+  msp430*elfbare)
+   use_tm_clone_registry=-DUSE_TM_CLONE_REGISTRY=0
+   ;;
+esac
 ])
 AC_SUBST([use_tm_clone_registry])
 
-- 
2.17.1

Reply via email to