From 5d3975ab89e623af16573345ec0d6d665612fd1b Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Tue, 19 Mar 2013 17:53:07 +0100 Subject: Add support for static constructors and destructors --- lib/cm3/vector.c | 15 +++++++++++++++ lib/efm32/efm32g/libopencm3_efm32g.ld | 23 +++++++++++++++++++++++ lib/efm32/efm32gg/libopencm3_efm32gg.ld | 23 +++++++++++++++++++++++ lib/efm32/efm32lg/libopencm3_efm32lg.ld | 23 +++++++++++++++++++++++ lib/efm32/efm32tg/libopencm3_efm32tg.ld | 23 +++++++++++++++++++++++ lib/lm3s/libopencm3_lm3s.ld | 23 +++++++++++++++++++++++ lib/lpc13xx/libopencm3_lpc13xx.ld | 23 +++++++++++++++++++++++ lib/lpc17xx/libopencm3_lpc17xx.ld | 23 +++++++++++++++++++++++ lib/lpc43xx/libopencm3_lpc43xx.ld | 23 +++++++++++++++++++++++ lib/lpc43xx/libopencm3_lpc43xx_rom_to_ram.ld | 23 +++++++++++++++++++++++ lib/stm32/f1/libopencm3_stm32f1.ld | 23 +++++++++++++++++++++++ lib/stm32/f2/libopencm3_stm32f2.ld | 23 +++++++++++++++++++++++ lib/stm32/f4/libopencm3_stm32f4.ld | 23 +++++++++++++++++++++++ lib/stm32/l1/libopencm3_stm32l1.ld | 23 +++++++++++++++++++++++ 14 files changed, 314 insertions(+) diff --git a/lib/cm3/vector.c b/lib/cm3/vector.c index 2706b6d..43e8917 100644 --- a/lib/cm3/vector.c +++ b/lib/cm3/vector.c @@ -29,6 +29,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 blocking_handler(void); @@ -55,6 +59,7 @@ vector_table_t vector_table = { void WEAK __attribute__ ((naked)) reset_handler(void) { volatile unsigned *src, *dest; + funcp_t *fp; for (src = &_data_loadaddr, dest = &_data; dest < &_edata; src++, dest++) *dest = *src; @@ -62,11 +67,21 @@ void WEAK __attribute__ ((naked)) 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)(); + /* might be provided by platform specific vector.c */ pre_main(); /* 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/efm32/efm32g/libopencm3_efm32g.ld b/lib/efm32/efm32g/libopencm3_efm32g.ld index b18da7f..87d6ee6 100644 --- a/lib/efm32/efm32g/libopencm3_efm32g.ld +++ b/lib/efm32/efm32g/libopencm3_efm32g.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/efm32/efm32gg/libopencm3_efm32gg.ld b/lib/efm32/efm32gg/libopencm3_efm32gg.ld index b18da7f..87d6ee6 100644 --- a/lib/efm32/efm32gg/libopencm3_efm32gg.ld +++ b/lib/efm32/efm32gg/libopencm3_efm32gg.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/efm32/efm32lg/libopencm3_efm32lg.ld b/lib/efm32/efm32lg/libopencm3_efm32lg.ld index b18da7f..87d6ee6 100644 --- a/lib/efm32/efm32lg/libopencm3_efm32lg.ld +++ b/lib/efm32/efm32lg/libopencm3_efm32lg.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/efm32/efm32tg/libopencm3_efm32tg.ld b/lib/efm32/efm32tg/libopencm3_efm32tg.ld index b18da7f..87d6ee6 100644 --- a/lib/efm32/efm32tg/libopencm3_efm32tg.ld +++ b/lib/efm32/efm32tg/libopencm3_efm32tg.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/libopencm3_lm3s.ld b/lib/lm3s/libopencm3_lm3s.ld index ceb391a..6c8c7f2 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/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/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/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/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/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/l1/libopencm3_stm32l1.ld b/lib/stm32/l1/libopencm3_stm32l1.ld index 9d165f6..3fc2ccb 100644 --- a/lib/stm32/l1/libopencm3_stm32l1.ld +++ b/lib/stm32/l1/libopencm3_stm32l1.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 -- cgit v1.2.3