[MSP430,GAS] Define symbols for functions to run through .{preinit,init,fini}_array sections

Message ID 20190418010213.3e72e2c5@jozef-kubuntu
State New
Headers show
Series
  • [MSP430,GAS] Define symbols for functions to run through .{preinit,init,fini}_array sections
Related show

Commit Message

Jozef Lawrynowicz April 18, 2019, 12:02 a.m.
Many of the MSP430 crt functions (e.g. to initialize bss) are linked
"dynamically", based on symbols defined in the program.
The GNU assembler defines the symbols corresponding to the crt
functions by examining the section names in the input file.

If GCC has been configured with --enable-initfini-array, then
.init_array and .fini_array will hold pointers to global
constructors/destructors. These sections can also hold functions that
need to be executed for other purposes.

The attached patch extends the assembler so it will define the
__crt0_run_{preinit,init,fini}_array symbols when
the .{preinit,init,fini}_array sections exist.

An accompanying patch to Newlib which adds the __crt0_run_*_array functions
will be submitted shortly.

Successfully regtested the DejaGNU GCC testsuite using the binutils and newlib
changes together with GCC trunk configured with --enable-initfini-array.

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

Thanks,
Jozef

Comments

Nick Clifton April 18, 2019, 12:17 p.m. | #1
Hi Jozef,

> If the patch is acceptable, I would appreciate if someone would commit it for

> me, as I do not have write access.


Approved and applied.

By the way - would you be interested in having write-after-approval access to 
the repository ?

Cheers
  Nick
Jozef Lawrynowicz April 22, 2019, 4:08 p.m. | #2
Hi Nick,

> > If the patch is acceptable, I would appreciate if someone would commit it for

> > me, as I do not have write access.  

> 

> Approved and applied.


Thanks for reviewing all those patches.

> By the way - would you be interested in having write-after-approval access to 

> the repository ?


Yes, definitely :)

Thanks,
Jozef

Patch

From c26b4d7713cb652c685e671270c0738f7cb9f12b Mon Sep 17 00:00:00 2001
From: Jozef Lawrynowicz <jozef.l@mittosystems.com>
Date: Wed, 17 Apr 2019 22:21:10 +0100
Subject: [PATCH] MSP430 Assembler: Define symbols for functions to run through
 .{preinit,init,fini}_array sections

gas/ChangeLog:

2019-04-18  Jozef Lawrynowicz  <jozef.l@mittosystems.com>

	* config/tc-msp430.c (msp430_make_init_symbols): Define
	__crt0_run_{preinit,init,fini}_array symbols if
	.{preinit,init,fini}_array sections exist.
	* testsuite/gas/msp430/fini-array.d: New test.
	* testsuite/gas/msp430/init-array.d: New test.
	* testsuite/gas/msp430/preinit-array.d: New test.
	* testsuite/gas/msp430/fini-array.s: New test source.
	* testsuite/gas/msp430/init-array.s: New test source.
	* testsuite/gas/msp430/preinit-array.s: New test source.
	* testsuite/gas/msp430/msp430.exp: Add new tests to driver.
---
 gas/config/tc-msp430.c                   | 26 ++++++++++++++++++++++++
 gas/testsuite/gas/msp430/fini-array.d    |  7 +++++++
 gas/testsuite/gas/msp430/fini-array.s    | 22 ++++++++++++++++++++
 gas/testsuite/gas/msp430/init-array.d    |  7 +++++++
 gas/testsuite/gas/msp430/init-array.s    | 22 ++++++++++++++++++++
 gas/testsuite/gas/msp430/msp430.exp      |  3 +++
 gas/testsuite/gas/msp430/preinit-array.d |  7 +++++++
 gas/testsuite/gas/msp430/preinit-array.s | 22 ++++++++++++++++++++
 8 files changed, 116 insertions(+)
 create mode 100644 gas/testsuite/gas/msp430/fini-array.d
 create mode 100644 gas/testsuite/gas/msp430/fini-array.s
 create mode 100644 gas/testsuite/gas/msp430/init-array.d
 create mode 100644 gas/testsuite/gas/msp430/init-array.s
 create mode 100644 gas/testsuite/gas/msp430/preinit-array.d
 create mode 100644 gas/testsuite/gas/msp430/preinit-array.s

diff --git a/gas/config/tc-msp430.c b/gas/config/tc-msp430.c
index 7868331bb9..d2d531b51e 100644
--- a/gas/config/tc-msp430.c
+++ b/gas/config/tc-msp430.c
@@ -1520,6 +1520,32 @@  msp430_make_init_symbols (const char * name)
       || strncmp (name, ".either.bss", 11) == 0
       || upper_data_region_in_use)
     (void) symbol_find_or_make ("__crt0_init_highbss");
+
+  /* The following symbols are for the crt0 functions that run through
+     the different .*_array sections and call the functions placed there.
+     - init_array stores global static C++ constructors to run before main.
+     - preinit_array is not expected to ever be used for MSP430.
+     GCC only places initialization functions for runtime "sanitizers"
+     (i.e. {a,l,t,u}san) and "virtual table verification" in preinit_array.
+     - fini_array stores global static C++ destructors to run after calling
+     exit() or returning from main.
+     __crt0_run_array is required to actually call the functions in the above
+     arrays.  */
+  if (strncmp (name, ".init_array", 11) == 0)
+    {
+      (void) symbol_find_or_make ("__crt0_run_init_array");
+      (void) symbol_find_or_make ("__crt0_run_array");
+    }
+  else if (strncmp (name, ".preinit_array", 14) == 0)
+    {
+      (void) symbol_find_or_make ("__crt0_run_preinit_array");
+      (void) symbol_find_or_make ("__crt0_run_array");
+    }
+  else if (strncmp (name, ".fini_array", 11) == 0)
+    {
+      (void) symbol_find_or_make ("__crt0_run_fini_array");
+      (void) symbol_find_or_make ("__crt0_run_array");
+    }
 }
 
 static void
diff --git a/gas/testsuite/gas/msp430/fini-array.d b/gas/testsuite/gas/msp430/fini-array.d
new file mode 100644
index 0000000000..c9cf572b67
--- /dev/null
+++ b/gas/testsuite/gas/msp430/fini-array.d
@@ -0,0 +1,7 @@ 
+#objdump: -t
+#name: Check symbols to run .fini_array functions have been defined
+#...
+.*__crt0_run_fini_array.*
+#...
+.*__crt0_run_array.*
+#pass
diff --git a/gas/testsuite/gas/msp430/fini-array.s b/gas/testsuite/gas/msp430/fini-array.s
new file mode 100644
index 0000000000..54d1ad22a4
--- /dev/null
+++ b/gas/testsuite/gas/msp430/fini-array.s
@@ -0,0 +1,22 @@ 
+	.file	"main.c"
+  .section  .fini_array,"aw"
+  .balign 2
+  .short  42
+.text
+	.balign 2
+	.global	main
+	.type	main, @function
+main:
+; start of function
+; framesize_regs:     0
+; framesize_locals:   0
+; framesize_outgoing: 0
+; framesize:          0
+; elim ap -> fp       2
+; elim fp -> sp       0
+; saved regs:(none)
+	; start of prologue
+	; end of prologue
+.L2:
+	BR	#.L2
+	.size	main, .-main
diff --git a/gas/testsuite/gas/msp430/init-array.d b/gas/testsuite/gas/msp430/init-array.d
new file mode 100644
index 0000000000..d3fa5979c8
--- /dev/null
+++ b/gas/testsuite/gas/msp430/init-array.d
@@ -0,0 +1,7 @@ 
+#objdump: -t
+#name: Check symbols to run .init_array functions have been defined
+#...
+.*__crt0_run_init_array.*
+#...
+.*__crt0_run_array.*
+#pass
diff --git a/gas/testsuite/gas/msp430/init-array.s b/gas/testsuite/gas/msp430/init-array.s
new file mode 100644
index 0000000000..b049ec4498
--- /dev/null
+++ b/gas/testsuite/gas/msp430/init-array.s
@@ -0,0 +1,22 @@ 
+	.file	"main.c"
+  .section  .init_array,"aw"
+  .balign 2
+  .short  42
+.text
+	.balign 2
+	.global	main
+	.type	main, @function
+main:
+; start of function
+; framesize_regs:     0
+; framesize_locals:   0
+; framesize_outgoing: 0
+; framesize:          0
+; elim ap -> fp       2
+; elim fp -> sp       0
+; saved regs:(none)
+	; start of prologue
+	; end of prologue
+.L2:
+	BR	#.L2
+	.size	main, .-main
diff --git a/gas/testsuite/gas/msp430/msp430.exp b/gas/testsuite/gas/msp430/msp430.exp
index 0f7805da3e..9c7a1975f9 100644
--- a/gas/testsuite/gas/msp430/msp430.exp
+++ b/gas/testsuite/gas/msp430/msp430.exp
@@ -43,4 +43,7 @@  if [expr [istarget "msp430-*-*"]]  then {
     run_dump_test "nop-unknown-intr-430"
     run_dump_test "nop-unknown-intr-430x"
     run_dump_test "nop-unknown-intr-430x-ignore"
+    run_dump_test "preinit-array"
+    run_dump_test "init-array"
+    run_dump_test "fini-array"
 }
diff --git a/gas/testsuite/gas/msp430/preinit-array.d b/gas/testsuite/gas/msp430/preinit-array.d
new file mode 100644
index 0000000000..8401adcd06
--- /dev/null
+++ b/gas/testsuite/gas/msp430/preinit-array.d
@@ -0,0 +1,7 @@ 
+#objdump: -t
+#name: Check symbols to run .preinit_array functions have been defined
+#...
+.*__crt0_run_preinit_array.*
+#...
+.*__crt0_run_array.*
+#pass
diff --git a/gas/testsuite/gas/msp430/preinit-array.s b/gas/testsuite/gas/msp430/preinit-array.s
new file mode 100644
index 0000000000..563326aef8
--- /dev/null
+++ b/gas/testsuite/gas/msp430/preinit-array.s
@@ -0,0 +1,22 @@ 
+	.file	"main.c"
+  .section  .preinit_array,"aw"
+  .balign 2
+  .short  42
+.text
+	.balign 2
+	.global	main
+	.type	main, @function
+main:
+; start of function
+; framesize_regs:     0
+; framesize_locals:   0
+; framesize_outgoing: 0
+; framesize:          0
+; elim ap -> fp       2
+; elim fp -> sp       0
+; saved regs:(none)
+	; start of prologue
+	; end of prologue
+.L2:
+	BR	#.L2
+	.size	main, .-main
-- 
2.17.1