From 3062999e97581e1ee1e2ec205b328491df7074d7 Mon Sep 17 00:00:00 2001 From: GustavWi Date: Wed, 10 Dec 2014 13:40:17 +0100 Subject: [PATCH 1/2] Separated the c data initialization and c++ to call mbed_sdk_init before c++ data. So that the c variables are updated before the c++ objects are initialized. Had to rename the cmain override in RTX_CM_lib.h to __iar_program_start because there can not be two __cmain overrides in the project. The ewps had to be updated to add the linker command --skip_dynamic_initialization which stalls the c++ data init until __iar_dynamic_initialization which is called right after mbed_sdk_init. --- .../mbed/targets/cmsis/TOOLCHAIN_IAR/cmain.s | 81 +++++++++++++++++++ .../rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h | 19 ++++- workspace_tools/export/iar_arch_pro.ewp.tmpl | 4 +- workspace_tools/export/iar_k20d50m.ewp.tmpl | 4 +- workspace_tools/export/iar_k22f.ewp.tmpl | 4 +- workspace_tools/export/iar_k64f.ewp.tmpl | 4 +- workspace_tools/export/iar_kl05z.ewp.tmpl | 4 +- workspace_tools/export/iar_kl25z.ewp.tmpl | 4 +- workspace_tools/export/iar_kl46z.ewp.tmpl | 4 +- workspace_tools/export/iar_lpc1114.ewp.tmpl | 4 +- workspace_tools/export/iar_lpc11u24.ewp.tmpl | 4 +- .../export/iar_lpc11u35_401.ewp.tmpl | 4 +- .../export/iar_lpc11u35_501.ewp.tmpl | 4 +- workspace_tools/export/iar_lpc1347.ewp.tmpl | 4 +- workspace_tools/export/iar_lpc1549.ewp.tmpl | 4 +- workspace_tools/export/iar_lpc1768.ewp.tmpl | 4 +- workspace_tools/export/iar_lpc4088.ewp.tmpl | 4 +- workspace_tools/export/iar_lpc812.ewp.tmpl | 4 +- .../export/iar_lpccappuccino.ewp.tmpl | 4 +- .../export/iar_mts_mdot_f405rg.ewp.tmpl | 4 +- .../export/iar_nucleo_f030r8.ewp.tmpl | 4 +- .../export/iar_nucleo_f072rb.ewp.tmpl | 4 +- .../export/iar_nucleo_f091rc.ewp.tmpl | 4 +- .../export/iar_nucleo_f103rb.ewp.tmpl | 4 +- .../export/iar_nucleo_f302r8.ewp.tmpl | 4 +- .../export/iar_nucleo_f303re.ewp.tmpl | 4 +- .../export/iar_nucleo_f334r8.ewp.tmpl | 4 +- .../export/iar_nucleo_f401re.ewp.tmpl | 4 +- .../export/iar_nucleo_f411re.ewp.tmpl | 4 +- .../export/iar_nucleo_l053r8.ewp.tmpl | 4 +- .../export/iar_nucleo_l152re.ewp.tmpl | 4 +- workspace_tools/export/iar_stm32f407.ewp.tmpl | 4 +- .../export/iar_ublox_c027.ewp.tmpl | 4 +- workspace_tools/toolchains/iar.py | 2 +- 34 files changed, 160 insertions(+), 66 deletions(-) create mode 100644 libraries/mbed/targets/cmsis/TOOLCHAIN_IAR/cmain.s diff --git a/libraries/mbed/targets/cmsis/TOOLCHAIN_IAR/cmain.s b/libraries/mbed/targets/cmsis/TOOLCHAIN_IAR/cmain.s new file mode 100644 index 0000000000..7395d3fa57 --- /dev/null +++ b/libraries/mbed/targets/cmsis/TOOLCHAIN_IAR/cmain.s @@ -0,0 +1,81 @@ +/************************************************** + * + * Part two of the system initialization code, contains C-level + * initialization, thumb-2 only variant. + * + * Copyright 2006 IAR Systems. All rights reserved. + * + * $Revision: 59783 $ + * + **************************************************/ + + +; -------------------------------------------------- +; Module ?cmain, C-level initialization. +; + + + SECTION SHT$$PREINIT_ARRAY:CONST:NOROOT(2) + SECTION SHT$$INIT_ARRAY:CONST:NOROOT(2) + + SECTION .text:CODE:NOROOT(2) + + PUBLIC __cmain + ;; Keep ?main for legacy reasons, it is accessed in countless instances of cstartup.s around the world... + PUBLIC ?main + EXTWEAK __iar_data_init3 + EXTWEAK __iar_argc_argv + EXTERN __low_level_init + EXTERN __call_ctors + EXTERN main + EXTERN exit + EXTERN __iar_dynamic_initialization + EXTERN mbed_sdk_init + EXTERN SystemInit + + THUMB +__cmain: +?main: + +; Initialize segments. +; __segment_init and __low_level_init are assumed to use the same +; instruction set and to be reachable by BL from the ICODE segment +; (it is safest to link them in segment ICODE). + + FUNCALL __cmain, __low_level_init + bl __low_level_init + cmp r0,#0 + beq ?l1 + FUNCALL __cmain, __iar_data_init3 + bl __iar_data_init3 + MOVS r0,#0 ; No parameters + FUNCALL __cmain, mbed_sdk_init + BL mbed_sdk_init + MOVS r0,#0 ; No parameters + FUNCALL __cmain, __iar_dynamic_initialization + BL __iar_dynamic_initialization ; C++ dynamic initialization + +?l1: + REQUIRE ?l3 + + SECTION .text:CODE:NOROOT(2) + + PUBLIC _main + PUBLIC _call_main + THUMB + +__iar_init$$done: ; Copy initialization is done + +?l3: +_call_main: + MOVS r0,#0 ; No parameters + FUNCALL __cmain, __iar_argc_argv + BL __iar_argc_argv ; Maybe setup command line + + FUNCALL __cmain, main + BL main +_main: + FUNCALL __cmain, exit + BL exit + + END diff --git a/libraries/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h b/libraries/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h index d3abb20148..c36f806d73 100755 --- a/libraries/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h +++ b/libraries/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h @@ -436,21 +436,34 @@ __attribute__((naked)) void software_init_hook (void) { #elif defined (__ICCARM__) +extern void* __vector_table; extern int __low_level_init(void); extern void __iar_data_init3(void); +extern __weak void __iar_init_core( void ); +extern __weak void __iar_init_vfp( void ); +extern void __iar_dynamic_initialization(void); +extern void mbed_sdk_init(void); extern void exit(int arg); -__noreturn __stackless void __cmain(void) { +#pragma required=__vector_table +void __iar_program_start( void ) +{ + __iar_init_core(); + __iar_init_vfp(); + int a; if (__low_level_init() != 0) { __iar_data_init3(); - } + mbed_sdk_init(); + __iar_dynamic_initialization(); + } osKernelInitialize(); set_main_stack(); osThreadCreate(&os_thread_def_main, NULL); a = osKernelStart(); - exit(a); + exit(a); + } #endif diff --git a/workspace_tools/export/iar_arch_pro.ewp.tmpl b/workspace_tools/export/iar_arch_pro.ewp.tmpl index 6eb8e14536..96900d1d69 100644 --- a/workspace_tools/export/iar_arch_pro.ewp.tmpl +++ b/workspace_tools/export/iar_arch_pro.ewp.tmpl @@ -722,11 +722,11 @@