Platform: Place global variables in shared code to dedicated section

The code sharing between bootloader and runtime firmware requires to
share the global variables as well. Because the RAM area is reused by
SPE when bootloader has finished its job, therefore it must be ensured
that global variables are placed by the linker to the same place during
both linking operation (MCUboot and SPE). This location is a dedicated
section which has a fixed place at the beginning of the RAM and hence
not colliding with other sections. If code sharing is disabled then this
special section is not created.

Change-Id: I4b6c181f924c1ab8c1733b99806e824526fb820b
Signed-off-by: Tamas Ban <tamas.ban@arm.com>
diff --git a/platform/CMakeLists.txt b/platform/CMakeLists.txt
index 915307c..5009520 100755
--- a/platform/CMakeLists.txt
+++ b/platform/CMakeLists.txt
@@ -153,4 +153,5 @@
         $<$<STREQUAL:${MCUBOOT_EXECUTION_SLOT},2>:LINK_TO_SECONDARY_PARTITION>
         $<$<BOOL:${TEST_PSA_API}>:PSA_API_TEST_${TEST_PSA_API}>
         $<$<BOOL:${FORWARD_PROT_MSG}>:FORWARD_PROT_MSG=${FORWARD_PROT_MSG}>
+        $<$<BOOL:${TFM_CODE_SHARING}>:CODE_SHARING>
 )
diff --git a/platform/ext/common/armclang/tfm_common_s.sct.template b/platform/ext/common/armclang/tfm_common_s.sct.template
index 627ca06..d2e9ba7 100644
--- a/platform/ext/common/armclang/tfm_common_s.sct.template
+++ b/platform/ext/common/armclang/tfm_common_s.sct.template
@@ -141,6 +141,14 @@
      * beginning of privileged data region in multi-core topology.
      */
 #ifndef TFM_MULTI_CORE_TOPOLOGY
+#ifdef CODE_SHARING
+    /* The code sharing between bootloader and runtime requires to share the
+     * global variables.
+     */
+    TFM_SHARED_SYMBOLS +0 ALIGN 32 EMPTY SHARED_SYMBOL_AREA_SIZE {
+    }
+#endif
+
     /* Shared area between BL2 and runtime to exchange data */
     TFM_SHARED_DATA +0 ALIGN 32 OVERLAY EMPTY BOOT_TFM_SHARED_DATA_SIZE {
     }
diff --git a/platform/ext/common/gcc/tfm_common_s.ld.template b/platform/ext/common/gcc/tfm_common_s.ld.template
index d596cea..f530492 100644
--- a/platform/ext/common/gcc/tfm_common_s.ld.template
+++ b/platform/ext/common/gcc/tfm_common_s.ld.template
@@ -313,6 +313,16 @@
      * beginning of privileged data region in multi-core topology.
      */
 #ifndef TFM_MULTI_CORE_TOPOLOGY
+#ifdef CODE_SHARING
+    /* The code sharing between bootloader and runtime requires to share the
+     * global variables.
+     */
+    .TFM_SHARED_SYMBOLS : ALIGN(32)
+    {
+        . += SHARED_SYMBOL_AREA_SIZE;
+    } > RAM
+#endif
+
     /* shared_data and msp_stack are overlapping on purpose when
      * msp_stack is extended until the beginning of RAM, when shared_date
      * was read out by partitions
diff --git a/platform/ext/target/mps2/an521/armclang/mps2_an521_bl2.sct b/platform/ext/target/mps2/an521/armclang/mps2_an521_bl2.sct
index 309bddf..6a6bcd4 100644
--- a/platform/ext/target/mps2/an521/armclang/mps2_an521_bl2.sct
+++ b/platform/ext/target/mps2/an521/armclang/mps2_an521_bl2.sct
@@ -26,6 +26,16 @@
     BL2_DATA_START S_DATA_START {
     }
 
+#ifdef CODE_SHARING
+    /* The code sharing between bootloader and runtime firmware requires to
+     * share the global variables.
+     */
+    TFM_SHARED_SYMBOLS +0 ALIGN 32 SHARED_SYMBOL_AREA_SIZE {
+       *platform.* (+RW)
+       *(.rodata.memset_func)
+    }
+#endif
+
     TFM_SHARED_DATA +0 ALIGN 32 EMPTY BOOT_TFM_SHARED_DATA_SIZE {
     }
 
diff --git a/platform/ext/target/mps2/an521/gcc/mps2_an521_bl2.ld b/platform/ext/target/mps2/an521/gcc/mps2_an521_bl2.ld
index 9dfddf3..1b4b995 100644
--- a/platform/ext/target/mps2/an521/gcc/mps2_an521_bl2.ld
+++ b/platform/ext/target/mps2/an521/gcc/mps2_an521_bl2.ld
@@ -1,5 +1,5 @@
 ;/*
-; * Copyright (c) 2017-2019 Arm Limited
+; * Copyright (c) 2017-2020 Arm Limited
 ; *
 ; * Licensed under the Apache License, Version 2.0 (the "License");
 ; * you may not use this file except in compliance with the License.
@@ -89,12 +89,14 @@
     {
         . = ALIGN(4);
         __copy_table_start__ = .;
-        LONG (__etext)
-        LONG (__data_start__)
-        LONG (__data_end__ - __data_start__)
-        LONG (DEFINED(__etext2) ? __etext2 : 0)
-        LONG (DEFINED(__data2_start__) ? __data2_start__ : 0)
-        LONG (DEFINED(__data2_start__) ? __data2_end__ - __data2_start__ : 0)
+#ifdef CODE_SHARING
+        LONG (LOADADDR(.tfm_shared_symbols))
+        LONG (ADDR(.tfm_shared_symbols))
+        LONG (SIZEOF(.tfm_shared_symbols))
+#endif
+        LONG (LOADADDR(.data))
+        LONG (ADDR(.data))
+        LONG (SIZEOF(.data))
         __copy_table_end__ = .;
     } > FLASH
 
@@ -105,26 +107,35 @@
     {
         . = ALIGN(4);
         __zero_table_start__ = .;
-        LONG (__bss_start__)
-        LONG (__bss_end__ - __bss_start__)
-        LONG (DEFINED(__bss2_start__) ? __bss2_start__ : 0)
-        LONG (DEFINED(__bss2_start__) ? __bss2_end__ - __bss2_start__ : 0)
+        LONG (ADDR(.bss))
+        LONG (SIZEOF(.bss))
         __zero_table_end__ = .;
     } > FLASH
 
-    __etext = .;
+#ifdef CODE_SHARING
+    /* The code sharing between bootloader and runtime firmware requires to
+     * share the global variables. Section size must be equal with
+     * SHARED_SYMBOL_AREA_SIZE defined in region_defs.h
+     */
+    .tfm_shared_symbols :
+    {
+        *(.data.mbedtls_calloc_func)
+        *(.data.mbedtls_free_func)
+        *(.data.mbedtls_exit)
+        *(.data.memset_func)
+        . = ALIGN(SHARED_SYMBOL_AREA_SIZE);
+    } > RAM AT > FLASH
+#endif
 
-    .tfm_bl2_shared_data : ALIGN(32)
+    .tfm_bl2_shared_data :
     {
         . += BOOT_TFM_SHARED_DATA_SIZE;
     } > RAM
     Image$$SHARED_DATA$$RW$$Base = ADDR(.tfm_bl2_shared_data);
     Image$$SHARED_DATA$$RW$$Limit = ADDR(.tfm_bl2_shared_data) + SIZEOF(.tfm_bl2_shared_data);
 
-    .data : AT (__etext)
+    .data : ALIGN(4)
     {
-        __data_start__ = .;
-        *(vtable)
         *(.data*)
 
         . = ALIGN(4);
@@ -150,23 +161,18 @@
 
         KEEP(*(.jcr*))
         . = ALIGN(4);
-        /* All data end */
-        __data_end__ = .;
 
-    } > RAM
+    } > RAM AT > FLASH
     Image$$ER_DATA$$Base = ADDR(.data);
 
-    .bss :
+    .bss : ALIGN(4)
     {
-        . = ALIGN(4);
         __bss_start__ = .;
         *(.bss*)
         *(COMMON)
         . = ALIGN(4);
         __bss_end__ = .;
-    } > RAM
-
-    bss_size = __bss_end__ - __bss_start__;
+    } > RAM AT > RAM
 
     .msp_stack : ALIGN(32)
     {
diff --git a/platform/ext/target/mps2/an521/gcc/startup_cmsdk_mps2_an521_bl2.S b/platform/ext/target/mps2/an521/gcc/startup_cmsdk_mps2_an521_bl2.S
index f31522a..a841d04 100644
--- a/platform/ext/target/mps2/an521/gcc/startup_cmsdk_mps2_an521_bl2.S
+++ b/platform/ext/target/mps2/an521/gcc/startup_cmsdk_mps2_an521_bl2.S
@@ -1,5 +1,5 @@
 ;/*
-; * Copyright (c) 2009-2018 ARM Limited
+; * Copyright (c) 2009-2020 ARM Limited
 ; *
 ; * Licensed under the Apache License, Version 2.0 (the "License");
 ; * you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
 ; * This file is derivative of CMSIS V5.00 startup_ARMCM33.S
 ; */
 
+#include "tfm_plat_config.h"
+
     .syntax    unified
     .arch    armv8-m.main
 
diff --git a/platform/ext/target/mps2/an521/partition/region_defs.h b/platform/ext/target/mps2/an521/partition/region_defs.h
index 5531f73..bef2b51 100644
--- a/platform/ext/target/mps2/an521/partition/region_defs.h
+++ b/platform/ext/target/mps2/an521/partition/region_defs.h
@@ -140,11 +140,25 @@
 #define BL2_DATA_LIMIT    (BL2_DATA_START + BL2_DATA_SIZE - 1)
 #endif /* BL2 */
 
+
+
+/* Shared symbol area between bootloader and runtime firmware. Global variables
+ * in the shared code can be placed here.
+ */
+#ifdef CODE_SHARING
+#define SHARED_SYMBOL_AREA_BASE S_RAM_ALIAS_BASE
+#define SHARED_SYMBOL_AREA_SIZE 0x20
+#else
+#define SHARED_SYMBOL_AREA_BASE S_RAM_ALIAS_BASE
+#define SHARED_SYMBOL_AREA_SIZE 0x0
+#endif /* CODE_SHARING */
+
 /* Shared data area between bootloader and runtime firmware.
- * Shared data area is allocated at the beginning of the RAM, it is overlapping
+ * These areas are allocated at the beginning of the RAM, it is overlapping
  * with TF-M Secure code's MSP stack
  */
-#define BOOT_TFM_SHARED_DATA_BASE S_RAM_ALIAS_BASE
+#define BOOT_TFM_SHARED_DATA_BASE (SHARED_SYMBOL_AREA_BASE + \
+                                   SHARED_SYMBOL_AREA_SIZE)
 #define BOOT_TFM_SHARED_DATA_SIZE (0x400)
 #define BOOT_TFM_SHARED_DATA_LIMIT (BOOT_TFM_SHARED_DATA_BASE + \
                                     BOOT_TFM_SHARED_DATA_SIZE - 1)
diff --git a/platform/ext/target/musca_b1/sse_200/Device/Source/armclang/musca_bl2.sct b/platform/ext/target/musca_b1/sse_200/Device/Source/armclang/musca_bl2.sct
index 2774107..f5d3bd9 100644
--- a/platform/ext/target/musca_b1/sse_200/Device/Source/armclang/musca_bl2.sct
+++ b/platform/ext/target/musca_b1/sse_200/Device/Source/armclang/musca_bl2.sct
@@ -33,6 +33,16 @@
     BL2_DATA_START S_DATA_START {
     }
 
+#ifdef CODE_SHARING
+    /* The code sharing between bootloader and runtime firmware requires to
+     * share the global variables.
+     */
+    TFM_SHARED_SYMBOLS +0 ALIGN 32 SHARED_SYMBOL_AREA_SIZE {
+       *platform.* (+RW)
+       *(.rodata.memset_func)
+    }
+#endif
+
     TFM_SHARED_DATA +0 ALIGN 32 EMPTY BOOT_TFM_SHARED_DATA_SIZE {
     }
 
diff --git a/platform/ext/target/musca_b1/sse_200/Device/Source/gcc/musca_bl2.ld b/platform/ext/target/musca_b1/sse_200/Device/Source/gcc/musca_bl2.ld
index c1fb13a..e3dbe6c 100644
--- a/platform/ext/target/musca_b1/sse_200/Device/Source/gcc/musca_bl2.ld
+++ b/platform/ext/target/musca_b1/sse_200/Device/Source/gcc/musca_bl2.ld
@@ -107,21 +107,22 @@
     __exidx_end = .;
 
     /* To copy multiple ROM to RAM sections,
-     * define etext2/data2_start/data2_end and
      * define __STARTUP_COPY_MULTIPLE in startup_cmsdk_musca_bl2.S */
     .copy.table :
     {
         . = ALIGN(4);
         __copy_table_start__ = .;
-        LONG (__etext)
-        LONG (__data_start__)
-        LONG (__data_end__ - __data_start__)
         LONG (LOADADDR(.ER_CODE_SRAM))
         LONG (ADDR(.ER_CODE_SRAM))
         LONG (SIZEOF(.ER_CODE_SRAM))
-        LONG (DEFINED(__etext2) ? __etext2 : 0)
-        LONG (DEFINED(__data2_start__) ? __data2_start__ : 0)
-        LONG (DEFINED(__data2_start__) ? __data2_end__ - __data2_start__ : 0)
+#ifdef CODE_SHARING
+        LONG (LOADADDR(.tfm_shared_symbols))
+        LONG (ADDR(.tfm_shared_symbols))
+        LONG (SIZEOF(.tfm_shared_symbols))
+#endif
+        LONG (LOADADDR(.data))
+        LONG (ADDR(.data))
+        LONG (SIZEOF(.data))
         __copy_table_end__ = .;
     } > FLASH
 
@@ -132,26 +133,35 @@
     {
         . = ALIGN(4);
         __zero_table_start__ = .;
-        LONG (__bss_start__)
-        LONG (__bss_end__ - __bss_start__)
-        LONG (DEFINED(__bss2_start__) ? __bss2_start__ : 0)
-        LONG (DEFINED(__bss2_start__) ? __bss2_end__ - __bss2_start__ : 0)
+       LONG (ADDR(.bss))
+       LONG (SIZEOF(.bss))
         __zero_table_end__ = .;
     } > FLASH
 
-    __etext = .;
+#ifdef CODE_SHARING
+    /* The code sharing between bootloader and runtime firmware requires to
+     * share the global variables. Section size must be equal with
+     * SHARED_SYMBOL_AREA_SIZE defined in region_defs.h
+     */
+    .tfm_shared_symbols :
+    {
+        *(.data.mbedtls_calloc_func)
+        *(.data.mbedtls_free_func)
+        *(.data.mbedtls_exit)
+        *(.data.memset_func)
+        . = ALIGN(SHARED_SYMBOL_AREA_SIZE);
+    } > RAM AT > FLASH
+#endif
 
-    .tfm_bl2_shared_data : ALIGN(32)
+    .tfm_bl2_shared_data :
     {
         . += BOOT_TFM_SHARED_DATA_SIZE;
     } > RAM
     Image$$SHARED_DATA$$RW$$Base = ADDR(.tfm_bl2_shared_data);
     Image$$SHARED_DATA$$RW$$Limit = ADDR(.tfm_bl2_shared_data) + SIZEOF(.tfm_bl2_shared_data);
 
-    .data : AT (__etext)
+    .data : ALIGN(4)
     {
-        __data_start__ = .;
-        *(vtable)
         *(.data*)
 
         . = ALIGN(4);
@@ -177,13 +187,11 @@
 
         KEEP(*(.jcr*))
         . = ALIGN(4);
-        /* All data end */
-        __data_end__ = .;
 
-    } > RAM
+    } > RAM AT > FLASH
     Image$$ER_DATA$$Base = ADDR(.data);
 
-    .bss :
+    .bss : ALIGN(4)
     {
         . = ALIGN(4);
         __bss_start__ = .;
@@ -193,8 +201,6 @@
         __bss_end__ = .;
     } > RAM
 
-    bss_size = __bss_end__ - __bss_start__;
-
     .msp_stack : ALIGN(32)
     {
         . += __msp_stack_size__;
diff --git a/platform/ext/target/musca_b1/sse_200/partition/region_defs.h b/platform/ext/target/musca_b1/sse_200/partition/region_defs.h
index 1f6ce14..a362c01 100644
--- a/platform/ext/target/musca_b1/sse_200/partition/region_defs.h
+++ b/platform/ext/target/musca_b1/sse_200/partition/region_defs.h
@@ -157,11 +157,23 @@
 #define BL2_DATA_LIMIT    (BL2_DATA_START + BL2_DATA_SIZE - 1)
 #endif /* BL2 */
 
+/* Shared symbol area between bootloader and runtime firmware. Global variables
+ * in the shared code can be placed here.
+ */
+#ifdef CODE_SHARING
+#define SHARED_SYMBOL_AREA_BASE S_RAM_ALIAS_BASE
+#define SHARED_SYMBOL_AREA_SIZE 0x20
+#else
+#define SHARED_SYMBOL_AREA_BASE S_RAM_ALIAS_BASE
+#define SHARED_SYMBOL_AREA_SIZE 0x0
+#endif /* CODE_SHARING */
+
 /* Shared data area between bootloader and runtime firmware.
- * Shared data area is allocated at the beginning of the RAM, it is overlapping
+ * These areas are allocated at the beginning of the RAM, it is overlapping
  * with TF-M Secure code's MSP stack
  */
-#define BOOT_TFM_SHARED_DATA_BASE S_RAM_ALIAS_BASE
+#define BOOT_TFM_SHARED_DATA_BASE (SHARED_SYMBOL_AREA_BASE + \
+                                   SHARED_SYMBOL_AREA_SIZE)
 #define BOOT_TFM_SHARED_DATA_SIZE (0x400)
 #define BOOT_TFM_SHARED_DATA_LIMIT (BOOT_TFM_SHARED_DATA_BASE + \
                                     BOOT_TFM_SHARED_DATA_SIZE - 1)