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/lm3s/libopencm3_lm3s.ld | 23 +++++++++++++++++++++++ lib/lm3s/vector.c | 15 +++++++++++++++ lib/lpc13xx/libopencm3_lpc13xx.ld | 23 +++++++++++++++++++++++ lib/lpc17xx/libopencm3_lpc17xx.ld | 23 +++++++++++++++++++++++ lib/lpc17xx/vector.c | 15 +++++++++++++++ lib/lpc43xx/libopencm3_lpc43xx.ld | 23 +++++++++++++++++++++++ lib/lpc43xx/libopencm3_lpc43xx_rom_to_ram.ld | 23 +++++++++++++++++++++++ lib/lpc43xx/vector.c | 15 +++++++++++++++ lib/stm32/f1/libopencm3_stm32f1.ld | 23 +++++++++++++++++++++++ lib/stm32/f1/vector.c | 15 +++++++++++++++ lib/stm32/f2/libopencm3_stm32f2.ld | 23 +++++++++++++++++++++++ lib/stm32/f2/vector.c | 15 +++++++++++++++ lib/stm32/f4/libopencm3_stm32f4.ld | 23 +++++++++++++++++++++++ lib/stm32/f4/vector.c | 15 +++++++++++++++ 14 files changed, 274 insertions(+) diff --git a/lib/lm3s/libopencm3_lm3s.ld b/lib/lm3s/libopencm3_lm3s.ld index 4aaf4d9..ef32001 100644 --- a/lib/lm3s/libopencm3_lm3s.ld +++ b/lib/lm3s/libopencm3_lm3s.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/lm3s/vector.c b/lib/lm3s/vector.c index b7c92ae..43c7952 100644 --- a/lib/lm3s/vector.c +++ b/lib/lm3s/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); @@ -309,6 +313,7 @@ void (*const vector_table[]) (void) = { void reset_handler(void) { volatile unsigned *src, *dest; + funcp_t *fp; __asm__("MSR msp, %0" : : "r"(&_stack)); @@ -318,8 +323,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) diff --git a/lib/lpc13xx/libopencm3_lpc13xx.ld b/lib/lpc13xx/libopencm3_lpc13xx.ld index 4e0f1df..bd0005c 100644 --- a/lib/lpc13xx/libopencm3_lpc13xx.ld +++ b/lib/lpc13xx/libopencm3_lpc13xx.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/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) diff --git a/lib/lpc43xx/libopencm3_lpc43xx.ld b/lib/lpc43xx/libopencm3_lpc43xx.ld index 9402a54..5dad9ec 100644 --- a/lib/lpc43xx/libopencm3_lpc43xx.ld +++ b/lib/lpc43xx/libopencm3_lpc43xx.ld @@ -42,6 +42,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/lpc43xx/libopencm3_lpc43xx_rom_to_ram.ld b/lib/lpc43xx/libopencm3_lpc43xx_rom_to_ram.ld index 06f7708..39602b5 100644 --- a/lib/lpc43xx/libopencm3_lpc43xx_rom_to_ram.ld +++ b/lib/lpc43xx/libopencm3_lpc43xx_rom_to_ram.ld @@ -43,6 +43,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/lpc43xx/vector.c b/lib/lpc43xx/vector.c index 23008bc..d4aa2f5 100644 --- a/lib/lpc43xx/vector.c +++ b/lib/lpc43xx/vector.c @@ -23,6 +23,10 @@ /* Symbols exported by the linker script(s): */ extern unsigned _data_loadaddr, _data, _edata, _ebss, _stack; extern unsigned _etext_ram, _text_ram, _etext_rom; +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); @@ -165,6 +169,7 @@ void (*const vector_table[]) (void) = { void reset_handler(void) { volatile unsigned *src, *dest; + funcp_t *fp; __asm__("MSR msp, %0" : : "r"(&_stack)); @@ -192,8 +197,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) diff --git a/lib/stm32/f1/libopencm3_stm32f1.ld b/lib/stm32/f1/libopencm3_stm32f1.ld index 9d165f6..3fc2ccb 100644 --- a/lib/stm32/f1/libopencm3_stm32f1.ld +++ b/lib/stm32/f1/libopencm3_stm32f1.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/stm32/f1/vector.c b/lib/stm32/f1/vector.c index f496ae4..a010078 100644 --- a/lib/stm32/f1/vector.c +++ b/lib/stm32/f1/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); @@ -194,6 +198,7 @@ void (*const vector_table[]) (void) = { void reset_handler(void) { volatile unsigned *src, *dest; + funcp_t *fp; __asm__("MSR msp, %0" : : "r"(&_stack)); @@ -203,8 +208,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) diff --git a/lib/stm32/f2/libopencm3_stm32f2.ld b/lib/stm32/f2/libopencm3_stm32f2.ld index 9d165f6..3fc2ccb 100644 --- a/lib/stm32/f2/libopencm3_stm32f2.ld +++ b/lib/stm32/f2/libopencm3_stm32f2.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/stm32/f2/vector.c b/lib/stm32/f2/vector.c index 3429bfb..d24fcae 100644 --- a/lib/stm32/f2/vector.c +++ b/lib/stm32/f2/vector.c @@ -22,6 +22,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); @@ -221,6 +225,7 @@ void (*const vector_table[]) (void) = { void reset_handler(void) { volatile unsigned *src, *dest; + funcp_t *fp; __asm__("MSR msp, %0" : : "r"(&_stack)); @@ -230,8 +235,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) diff --git a/lib/stm32/f4/libopencm3_stm32f4.ld b/lib/stm32/f4/libopencm3_stm32f4.ld index 9d165f6..3fc2ccb 100644 --- a/lib/stm32/f4/libopencm3_stm32f4.ld +++ b/lib/stm32/f4/libopencm3_stm32f4.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/stm32/f4/vector.c b/lib/stm32/f4/vector.c index 01b5e64..16b158b 100644 --- a/lib/stm32/f4/vector.c +++ b/lib/stm32/f4/vector.c @@ -24,6 +24,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); @@ -223,6 +227,7 @@ void (*const vector_table[]) (void) = { void reset_handler(void) { volatile unsigned *src, *dest; + funcp_t *fp; __asm__("MSR msp, %0" : : "r"(&_stack)); @@ -235,8 +240,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