From 00e50ff0cda40ee1736887e83465e7e5c77aaa3f Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Tue, 19 Mar 2013 18:22:15 +0100 Subject: Add support for static constructors and destructors Conflicts: lib/efm32/efm32g/libopencm3_efm32g.ld lib/efm32/efm32gg/libopencm3_efm32gg.ld lib/efm32/efm32lg/libopencm3_efm32lg.ld lib/efm32/efm32tg/libopencm3_efm32tg.ld lib/lpc17xx/vector.c lib/stm32/l1/libopencm3_stm32l1.ld --- lib/lpc17xx/libopencm3_lpc17xx.ld | 23 +++++++++++++++++++++++ lib/lpc17xx/vector.c | 15 +++++++++++++++ 2 files changed, 38 insertions(+) (limited to 'lib/lpc17xx') diff --git a/lib/lpc17xx/libopencm3_lpc17xx.ld b/lib/lpc17xx/libopencm3_lpc17xx.ld index 4e0f1df..bd0005c 100644 --- a/lib/lpc17xx/libopencm3_lpc17xx.ld +++ b/lib/lpc17xx/libopencm3_lpc17xx.ld @@ -38,6 +38,29 @@ SECTIONS . = ALIGN(4); } >rom + /* C++ Static constructors/destructors, also used for __attribute__ + * ((constructor)) and the likes */ + .preinit_array : { + . = ALIGN(4); + __preinit_array_start = .; + KEEP (*(.preinit_array)) + __preinit_array_end = .; + } >rom + .init_array : { + . = ALIGN(4); + __init_array_start = .; + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array)) + __init_array_end = .; + } >rom + .fini_array : { + . = ALIGN(4); + __fini_array_start = .; + KEEP (*(.fini_array)) + KEEP (*(SORT(.fini_array.*))) + __fini_array_end = .; + } >rom + /* * Another section used by C++ stuff, appears when using newlib with * 64bit (long long) printf support diff --git a/lib/lpc17xx/vector.c b/lib/lpc17xx/vector.c index 518f562..4cccb6e 100644 --- a/lib/lpc17xx/vector.c +++ b/lib/lpc17xx/vector.c @@ -21,6 +21,10 @@ /* Symbols exported by the linker script(s): */ extern unsigned _data_loadaddr, _data, _edata, _ebss, _stack; +typedef void (*funcp_t) (void); +extern funcp_t __preinit_array_start, __preinit_array_end; +extern funcp_t __init_array_start, __init_array_end; +extern funcp_t __fini_array_start, __fini_array_end; void main(void); void reset_handler(void); @@ -60,6 +64,7 @@ void (*const vector_table[]) (void) = { void reset_handler(void) { volatile unsigned *src, *dest; + funcp_t *fp; __asm__("MSR msp, %0" : : "r"(&_stack)); @@ -69,8 +74,18 @@ void reset_handler(void) while (dest < &_ebss) *dest++ = 0; + /* Constructors. */ + for (fp = &__preinit_array_start; fp < &__preinit_array_end; fp++) + (*fp)(); + for (fp = &__init_array_start; fp < &__init_array_end; fp++) + (*fp)(); + /* Call the application's entry point. */ main(); + + /* Destructors. */ + for (fp = &__fini_array_start; fp < &__fini_array_end; fp++) + (*fp)(); } void blocking_handler(void) -- cgit v1.2.3