This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH][MSP430] Remove .init/.fini sections from CRT library code


The attached patch remove the .init/.fini sections from crt0/crtn. Detailed
explanation on the changes is in the commit message.

Successfully regtested the GCC/G++ testsuites for msp430-elf using the latest
GCC and binutils. 

The attached "gcc.patch" patch to GCC is required before building. It enables
"initfini_array" (--enable-initfini-array) by default for msp430, and also makes
the crtn.o file an optional part of ENDFILE_SPEC (since this patch removes that
file).
If the attached newlib patch is accepted I will apply "gcc.patch" to GCC.

If the newlib patch is acceptable, I would appreciate if someone would commit it
for me, as I do not have write access.

Thanks,
Jozef
>From e5bac1bb00ac0f5f4b499ce9eb3e1d50337199d5 Mon Sep 17 00:00:00 2001
From: Jozef Lawrynowicz <jozef.l@mittosystems.com>
Date: Wed, 24 Jul 2019 10:45:49 +0100
Subject: [PATCH] MSP430: Remove .init/.fini sections

The .init/.fini sections are not required for msp430-elf, and add unnecessary
code bloat to the CRT library. These sections are specified as "unused" by
the MSP430 EABI.

.init existed to call __crt0_run_{init,preinit}_array which run through
the functions in .{init,preinit}_array.
__crt0_run_{init,preinit}_array are already dynamically included like the
other crt0 functions, so these can be placed before the call to main,
which ensures they are still called if needed.
With these functions moved, .init has no purpose and can be removed.

.fini existed to call __crt0_run_fini_array.
However, the "__msp430_fini" symbol which marks the start of .fini has
never been used, so no termination routines have ever been run for
msp430. On returning from main(), _exit() is called which just loops
forever.
So there is no current expectation that __crt0_run_fini_array will
get called by the CRT code. Further work is to ensure functions
registered with atexit can be optionally called during program termination,
and then __crt0_run_fini_array can be registered with atexit during
program initialization.

The mechanisms for supporting the "-minrt" option have also been removed.
"-minrt" enabled a "minimum runtime environment" by removing calls to
functions which run global static initializers and constructors. Since
this behaviour is now dynamic, and these functions are only included
when needed, the minrt versions of the CRT object files are no longer
required.
---
 libgloss/msp430/Makefile.in     |   9 +-
 libgloss/msp430/crt0.S          | 165 ++++++++++++--------------------
 libgloss/msp430/crtn.S          |  41 --------
 libgloss/msp430/msp430-sim.ld   |   2 -
 libgloss/msp430/msp430xl-sim.ld |   2 -
 5 files changed, 62 insertions(+), 157 deletions(-)
 delete mode 100644 libgloss/msp430/crtn.S

diff --git a/libgloss/msp430/Makefile.in b/libgloss/msp430/Makefile.in
index 77c9b8b21..59c11a9a9 100644
--- a/libgloss/msp430/Makefile.in
+++ b/libgloss/msp430/Makefile.in
@@ -61,7 +61,7 @@ SCRIPTS  = $(srcdir)/msp430-sim.ld
 SCRIPTS += $(srcdir)/msp430xl-sim.ld
 SCRIPTS += $(srcdir)/intr_vectors.ld
 
-CRT = gcrt0.o crt0.o crt0-minrt.o crtn.o crtn-minrt.o
+CRT = gcrt0.o crt0.o
 SIM_BSP = libsim.a
 LIB_NOSYS = libnosys.a
 LIB_CRT = libcrt.a
@@ -85,7 +85,6 @@ CRT_OBJS = \
 	crt_movedata.o \
 	crt_move_highdata.o \
 	crt_main.o \
-	crt_main_minrt.o \
 	crt_callexit.o \
 	crt_run_init_array.o \
 	crt_run_preinit_array.o \
@@ -102,12 +101,6 @@ all: $(CRT) $(SIM_BSP) $(LIB_NOSYS) $(LIB_CRT) copy_scripts_to_objdir
 crt_%.o : crt0.S
 	$(CC) -DL$* -Wa,-gdwarf2 -Wa,-I$(srcdir) $(CFLAGS_FOR_TARGET) $(INCLUDES) $(CFLAGS) -c $< -o $@
 
-crt0-minrt.o : crt0.S
-	$(CC) -DL0 -DMINRT -Wa,-gdwarf2 -Wa,-I$(srcdir) $(CFLAGS_FOR_TARGET) $(INCLUDES) $(CFLAGS) -c $< -o $@
-
-crtn-minrt.o : crtn.S
-	$(CC) -DL0 -DMINRT -Wa,-gdwarf2 -Wa,-I$(srcdir) $(CFLAGS_FOR_TARGET) $(INCLUDES) $(CFLAGS) -c $< -o $@
-
 # Override .S.o rule to pass assembler debugging flags
 .S.o:
 	$(CC) -DL0 -Wa,-gdwarf2 -Wa,-I$(srcdir) $(CFLAGS_FOR_TARGET) $(INCLUDES) $(CFLAGS) -c $<
diff --git a/libgloss/msp430/crt0.S b/libgloss/msp430/crt0.S
index 42464ddbe..47f2ed783 100644
--- a/libgloss/msp430/crt0.S
+++ b/libgloss/msp430/crt0.S
@@ -52,28 +52,25 @@ __msp430_resetvec_hook:
 
 START_CRT_FUNC 0000 start
 	.refsym	__msp430_resetvec_hook
-#ifdef MINRT
-	.refsym	__crt0_call_just_main
-#else
-	.refsym	__crt0_call_init_then_main
-#endif
+	.refsym	__crt0_call_main
 	mov_	#__stack, R1
 
 END_CRT_FUNC	start
 #endif
 
-;; Some of the CRT functions below will only be present in the final linked
-;; executable if the assembler decides they are needed.  It will only define
-;; the symbol necessary to prevent them being garbage collected by the linker
-;; if the file being assembled has a specific section.
-;; The CRT functions this applies to are:
-;; init_bss, movedata, move_highdata, init_highbss, run_init_array,
-;; run_preinit_array, run_fini_array and run_array.
+;; The CRT functions below will only be present in the final linked
+;; executable if the assembler decides they are needed.  The assembler will
+;; only define the symbol necessary to prevent them being garbage collected
+;; by the linker if the file being assembled has a specific section,
+;; or some other criteria is met.
+;; The exception to this is __crt0_call_exit. GCC will include this function
+;; if it detects that main() has an epilogue. For example, if main() has a
+;; while(1) loop at the end, GCC will not generate an epilogue (since it won't
+;; return) and __crt0_call_exit won't be included.
 
 #if Lbss
-;; Note - this section is only included in the startup code of the
-;; application if it is needed.  It is responsible for initializing
-;; the contents of the .bss section.
+;; This function is responsible for initializing the contents of the
+;; .bss section.
 
 START_CRT_FUNC 0100 init_bss
 
@@ -91,9 +88,8 @@ END_CRT_FUNC	init_bss
 
 #ifdef __MSP430X_LARGE__
 #if Lhigh_bss
-;; Note - this section is only included in the startup code of the
-;; application if it is needed.  It is responsible for initializing
-;; the contents of the .upper.bss section.
+;; This function is responsible for initializing the contents of the
+;; .upper.bss section.
 
 START_CRT_FUNC 0200 init_highbss
 	
@@ -112,8 +108,7 @@ END_CRT_FUNC	init_highbss
 
 
 #if Lmovedata
-;; Note - this section is only included in the startup code of the
-;; application if it is needed.  It is responsible for copying the
+;; This function is responsible for copying the
 ;; contents of the .data section from its load address (in ROM) to
 ;; its run-time address (in RAM).
 
@@ -136,8 +131,7 @@ END_CRT_FUNC	movedata
 
 #ifdef __MSP430X_LARGE__
 #if Lmove_highdata
-;; Note - this section is only included in the startup code of the application
-;; if it is needed.  It is responsible either for making sure that the
+;; This function is responsible for making sure that the
 ;; contents of the .upper.data section have their correct startup values.
 ;; If a copy of the .upper.data section is stored in ROM then this means
 ;; copying the contents into HIFRAM.  If a copy of .upper.data is stored in a
@@ -175,44 +169,62 @@ END_CRT_FUNC	move_highdata
 #endif /* Lmove_highdata */
 #endif /* __MSP430X_LARGE__ */
 
+#if Lrun_preinit_array
+;; This function is responsible for setting up the arguments
+;; required for __crt0_run_array, to run the functions in .preinit_array.
+START_CRT_FUNC 0500 run_preinit_array
+
+	mov_	#__preinit_array_start, R4
+	mov_	#__preinit_array_end, R5
+	mov_	#PTRsz, R6
+	call_	#__crt0_run_array
 
-#if Lmain_minrt
-;; Note - this section is only included in the startup code of the
-;; application if it is needed.  It is responsible for just calling
-;; main.  No initialization code is called first, and main is not
-;; expected to return.
+END_CRT_FUNC	run_preinit_array
+#endif /* Lrun_preinit_array */
 
-START_CRT_FUNC 0600 call_just_main
+#if Lrun_init_array
+;; This function is responsible for setting up the arguments
+;; required for __crt0_run_array, to run the functions in .init_array.
+START_CRT_FUNC 0600 run_init_array
 
-	clr.w	R12		; Set argc == 0
-	call_	#main
-END_CRT_FUNC	call_just_main
-#endif /* Lmain_minrt */
+	mov_	#__init_array_start, R4
+	mov_	#__init_array_end, R5
+	mov_	#PTRsz, R6
+	call_	#__crt0_run_array
 
+END_CRT_FUNC	run_init_array
+#endif /* Lrun_init_array */
 
-#if Lmain
-;; Note - this section is only included in the startup code of the
-;; application if it is needed.  It is responsible for calling the
-;; initialization code - constructors, etc - and then main.  If main
-;; returns then the following section should be present to catch it.
+;; FIXME: There are currently no program termination routines executed for
+;; msp430.
+#if 0
+#if Lrun_fini_array
+;; Ensure global C++ destructors in .fini_array are called on exit
+;; by registering __crt0_run_fini_array with atexit.
+START_CRT_FUNC 0700 register_fini_array
+
+	mov_	#__crt0_run_fini_array, R12
+	call_	#atexit
+
+END_CRT_FUNC	register_fini_array
+#endif /* Lrun_fini_array */
+#endif /* 0 */
 
-START_CRT_FUNC 0700 call_init_then_main
+#if Lmain
+;; This function is always included and calls main().
 
-	call_	#__msp430_init
+START_CRT_FUNC 0800 call_main
 
 	clr.w	R12		; Set argc == 0
 	call_	#main
 
-END_CRT_FUNC	call_init_then_main
+END_CRT_FUNC	call_main
 #endif /* Lmain */
 
-
 #if Lcallexit
-;; Note - this section is only included in the startup code of the
-;; application if it is needed.  It is responsible for calling exit
-;; once main has finished.
+;; This function is responsible for calling exit once main has finished.
 
-START_CRT_FUNC 0800 call_exit
+START_CRT_FUNC 0900 call_exit
 
 	call_	#_exit
 
@@ -221,46 +233,15 @@ END_CRT_FUNC	call_exit
 
 ;----------------------------------------
 
-#ifndef MINRT
-
-#if Lrun_preinit_array
-;; Note - this section is only included in the startup code of the application
-;; if it is needed.  It is responsible for setting up the arguments
-;; required for __crt0_run_array, to run the functions in .preinit_array.
-START_CRT_FUNC 0910 run_preinit_array
-
-	mov_	#__preinit_array_start, R4
-	mov_	#__preinit_array_end, R5
-	mov_	#PTRsz, R6
-	br_	#__crt0_run_array
-
-END_CRT_FUNC	run_preinit_array
-#endif /* Lrun_preinit_array */
-
-#if Lrun_init_array
-;; Note - this section is only included in the startup code of the application
-;; if it is needed.  It is responsible for setting up the arguments
-;; required for __crt0_run_array, to run the functions in .init_array.
-START_CRT_FUNC 0920 run_init_array
-
-	mov_	#__init_array_start, R4
-	mov_	#__init_array_end, R5
-	mov_	#PTRsz, R6
-	br_	#__crt0_run_array
-
-END_CRT_FUNC	run_init_array
-#endif /* Lrun_init_array */
-
 #if Lrun_fini_array
-;; Note - this section is only included in the startup code of the application
-;; if it is needed.  It is responsible for setting up the arguments
+;; This function is responsible for setting up the arguments
 ;; required for __crt0_run_array, to run the functions in .fini_array.
-START_CRT_FUNC 0930 run_fini_array
+START_CRT_FUNC 1000 run_fini_array
 
 	mov_	#__fini_array_start, R4
 	mov_	#__fini_array_end, R5
 	mov_	#-PTRsz, R6
-	br_	#__crt0_run_array
+	call_	#__crt0_run_array
 
 END_CRT_FUNC	run_fini_array
 #endif /* Lrun_fini_array */
@@ -268,7 +249,7 @@ END_CRT_FUNC	run_fini_array
 #if Lrun_array
 ;; Note - this section is only included in the startup code of the application
 ;; if it is needed by one of the above run_*_array functions.
-START_CRT_FUNC 0980 run_array
+START_CRT_FUNC 1100 run_array
 
 	cmp_	R4, R5
 	jeq	_msp430_run_done
@@ -282,27 +263,3 @@ END_CRT_FUNC	run_array
 _msp430_run_done:
 	ret_
 #endif /* Lrun_array */
-
-;----------------------------------------
-#if L0
-
-	.section	.init,"ax"
-
-	.global __msp430_init
-__msp430_init:
-
-	.section	.fini,"ax"
-
-	.global __msp430_fini
-__msp430_fini:
-	call_	#__crt0_run_fini_array
-	
-;; If this function is not defined externally, we don't need it to do
-;; anything.
-	.text
-	.weak __crt0_run_fini_array
-__crt0_run_fini_array:
-	ret_
-
-#endif
-#endif /* not MINRT */
diff --git a/libgloss/msp430/crtn.S b/libgloss/msp430/crtn.S
deleted file mode 100644
index 110fc3090..000000000
--- a/libgloss/msp430/crtn.S
+++ /dev/null
@@ -1,41 +0,0 @@
-/* Copyright (c) 2013 Red Hat, Inc. All rights reserved.
-
-   This copyrighted material is made available to anyone wishing to use, modify,
-   copy, or redistribute it subject to the terms and conditions of the BSD
-   License.   This program is distributed in the hope that it will be useful,
-   but WITHOUT ANY WARRANTY expressed or implied, including the implied warranties
-   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  A copy of this license
-   is available at http://www.opensource.org/licenses. Any Red Hat trademarks that
-   are incorporated in the source code or documentation are not subject to the BSD
-   License and may only be used or replicated with the express permission of
-   Red Hat, Inc.
-*/
-
-#include "memmodel.h"
-
-#ifndef MINRT
-	.section	.init,"ax"
-	call_	#__crt0_run_preinit_array
-	call_	#__crt0_run_init_array
-	ret_
-	.global	__msp430_init_end
-__msp430_init_end:
-
-	.section	.fini,"ax"
-
-	ret_
-	.global	__msp430_fini_end
-__msp430_fini_end:
-
-	.text
-;; If these functions are not defined externally, we don't need them to do
-;; anything.
-	.balign 2
-	.weak __crt0_run_preinit_array
-	.weak __crt0_run_init_array
-__crt0_run_preinit_array:
-__crt0_run_init_array:
-	ret_
-
-
-#endif
diff --git a/libgloss/msp430/msp430-sim.ld b/libgloss/msp430/msp430-sim.ld
index 283127465..fadd137e8 100644
--- a/libgloss/msp430/msp430-sim.ld
+++ b/libgloss/msp430/msp430-sim.ld
@@ -106,8 +106,6 @@ SECTIONS
     PROVIDE (_etext = .);
     PROVIDE (etext = .);
     . = ALIGN(2);
-    KEEP (*(.init))
-    KEEP (*(.fini))
     KEEP (*(.tm_clone_table))
   } > RAM
 
diff --git a/libgloss/msp430/msp430xl-sim.ld b/libgloss/msp430/msp430xl-sim.ld
index cc451b853..125b7d897 100644
--- a/libgloss/msp430/msp430xl-sim.ld
+++ b/libgloss/msp430/msp430xl-sim.ld
@@ -387,8 +387,6 @@ SECTIONS
     PROVIDE (_etext = .);
     PROVIDE (etext = .);
     . = ALIGN(2);
-    KEEP (*(.init))
-    KEEP (*(.fini))
     KEEP (*(.tm_clone_table))
   } > ROM
 
-- 
2.17.1

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 446a852ffe1..17ed1b876a1 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -2503,6 +2503,13 @@ msp430*-*-*)
 	cxx_target_objs="msp430-c.o"
 	tmake_file="${tmake_file} msp430/t-msp430"
 	extra_gcc_objs="driver-msp430.o"
+	# Enable .init_array unless it has been explicitly disabled (e.g. if
+	# the user is building using an old version of newlib.
+	# The MSPABI mandates .init_array, and the Newlib CRT code since
+	# mid-2019 expects it.
+	if test x${disable_initfini_array} != xyes; then
+		gcc_cv_initfini_array=yes
+	fi
 	;;
 nds32*-*-*)
 	target_cpu_default="0"
diff --git a/gcc/config/msp430/msp430.h b/gcc/config/msp430/msp430.h
index 1288b1a263d..ca7cf20e1d7 100644
--- a/gcc/config/msp430/msp430.h
+++ b/gcc/config/msp430/msp430.h
@@ -49,7 +49,7 @@ extern bool msp430x;
 
 /* -lgcc is included because crtend.o needs __mspabi_func_epilog_1.  */
 #undef  ENDFILE_SPEC
-#define ENDFILE_SPEC "%{!minrt:crtend.o%s} %{minrt:crtn-minrt.o%s}%{!minrt:crtn.o%s} -lgcc"
+#define ENDFILE_SPEC "%{!minrt:crtend.o%s} %{minrt:%:if-exists(crtn-minrt.o%s)}%{!minrt:%:if-exists(crtn.o%s)} -lgcc"
 
 #define ASM_SPEC "-mP " /* Enable polymorphic instructions.  */ \
   "%{mcpu=*:-mcpu=%*}%{!mcpu=*:%{mmcu=*:-mmcu=%*}} " /* Pass the CPU type on to the assembler.  */ \

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]