Skip to content

Commit 5a92e46

Browse files
RobinKastbergkartben
authored andcommitted
iar: toolchain: Implement IAR static init routine
This will implement a way of doing static initialization the "IAR" way. This is done by calling __iar_data_init3 which handles all static initialization that is mentioned in the linker file "initialize by copy". Signed-off-by: Robin Kastberg <[email protected]>
1 parent 252f8fe commit 5a92e46

File tree

5 files changed

+80
-27
lines changed

5 files changed

+80
-27
lines changed

cmake/linker/iar/config_file_script.cmake

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ function(group_to_string)
333333
endif()
334334

335335
set(${STRING_STRING} "${${STRING_STRING}}\"${name}\": place in ${ILINK_CURRENT_NAME} { block ${name_clean} };\n")
336-
if(DEFINED vma AND DEFINED lma AND NOT ${noinit})
336+
if(CONFIG_IAR_ZEPHYR_INIT AND DEFINED vma AND DEFINED lma AND NOT ${noinit})
337337
set(${STRING_STRING} "${${STRING_STRING}}\"${name}_init\": place in ${lma} { block ${name_clean}_init };\n")
338338
endif()
339339

@@ -792,32 +792,51 @@ function(section_to_string)
792792
list(JOIN current_sections ", " SELECTORS)
793793
set(TEMP "${TEMP}\ndo not initialize {\n${SELECTORS}\n};")
794794
else()
795-
# Generate the _init block and the initialize manually statement.
796-
# Note that we need to have the X_init block defined even if we have
797-
# no sections, since there will come a "place in XXX" statement later.
798-
799-
# "${TEMP}" is there too keep the ';' else it will be a list
800-
string(REGEX REPLACE "(block[ \t\r\n]+)([^ \t\r\n]+)" "\\1\\2_init" INIT_TEMP "${TEMP}")
801-
string(REGEX REPLACE "(rw)([ \t\r\n]+)(section[ \t\r\n]+)([^ \t\r\n,]+)" "\\1\\2\\3\\4_init" INIT_TEMP "${INIT_TEMP}")
802-
string(REGEX REPLACE "(rw)([ \t\r\n]+)(section[ \t\r\n]+)" "ro\\2\\3" INIT_TEMP "${INIT_TEMP}")
803-
string(REGEX REPLACE "alphabetical order, " "" INIT_TEMP "${INIT_TEMP}")
804-
string(REGEX REPLACE "{ readwrite }" "{ }" INIT_TEMP "${INIT_TEMP}")
805-
806-
# If any content is marked as keep, is has to be applied to the init block
807-
# too, esp. for blocks that are not referenced (e.g. empty blocks wiht min_size)
808-
if(to_be_kept)
809-
list(APPEND to_be_kept "block ${name_clean}_init")
810-
endif()
811-
set(TEMP "${TEMP}\n${INIT_TEMP}\n")
795+
812796
if(DEFINED current_sections)
813-
set(TEMP "${TEMP}\ninitialize manually with copy friendly\n")
814-
set(TEMP "${TEMP}{\n")
815-
foreach(section ${current_sections})
816-
set(TEMP "${TEMP} ${section},\n")
817-
endforeach()
818-
set(TEMP "${TEMP}};")
819-
set(current_sections)
797+
if(CONFIG_IAR_DATA_INIT)
798+
set(TEMP "${TEMP}\ninitialize by copy\n")
799+
set(TEMP "${TEMP}{\n")
800+
foreach(section ${current_sections})
801+
set(TEMP "${TEMP} ${section},\n")
802+
endforeach()
803+
set(TEMP "${TEMP}};")
804+
805+
set(TEMP "${TEMP}\n\"${name}_init\": place in ${group_parent_lma} {\n")
806+
foreach(section ${current_sections})
807+
set(TEMP "${TEMP} ${section}_init,\n")
808+
endforeach()
809+
set(TEMP "${TEMP}};")
810+
elseif(CONFIG_IAR_ZEPHYR_INIT)
811+
# Generate the _init block and the initialize manually statement.
812+
# Note that we need to have the X_init block defined even if we have
813+
# no sections, since there will come a "place in XXX" statement later.
814+
815+
# "${TEMP}" is there too keep the ';' else it will be a list
816+
string(REGEX REPLACE "(block[ \t\r\n]+)([^ \t\r\n]+)" "\\1\\2_init" INIT_TEMP "${TEMP}")
817+
string(REGEX REPLACE "(rw)([ \t\r\n]+)(section[ \t\r\n]+)([^ \t\r\n,]+)" "\\1\\2\\3\\4_init" INIT_TEMP "${INIT_TEMP}")
818+
string(REGEX REPLACE "(rw)([ \t\r\n]+)(section[ \t\r\n]+)" "ro\\2\\3" INIT_TEMP "${INIT_TEMP}")
819+
string(REGEX REPLACE "alphabetical order, " "" INIT_TEMP "${INIT_TEMP}")
820+
string(REGEX REPLACE "{ readwrite }" "{ }" INIT_TEMP "${INIT_TEMP}")
821+
822+
# If any content is marked as keep, is has to be applied to the init block
823+
# too, esp. for blocks that are not referenced (e.g. empty blocks wiht min_size)
824+
if(to_be_kept)
825+
list(APPEND to_be_kept "block ${name_clean}_init")
826+
endif()
827+
set(TEMP "${TEMP}\n${INIT_TEMP}\n")
828+
set(TEMP "${TEMP}\ninitialize manually with copy friendly\n")
829+
set(TEMP "${TEMP}{\n")
830+
foreach(section ${current_sections})
831+
set(TEMP "${TEMP} ${section},\n")
832+
endforeach()
833+
set(TEMP "${TEMP}};")
834+
set(current_sections)
835+
endif()
820836
endif()
837+
838+
set(current_sections)
839+
821840
endif()
822841
endif()
823842

cmake/linker/iar/linker_flags.cmake

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44

55
# Override the default CMake's IAR ILINK linker signature
66

7-
string(APPEND CMAKE_C_LINK_FLAGS --no-wrap-diagnostics )
7+
string(APPEND CMAKE_C_LINK_FLAGS --no-wrap-diagnostics)
88

9+
if(CONFIG_IAR_DATA_INIT)
10+
string(APPEND CMAKE_C_LINK_FLAGS " --redirect z_data_copy=__iar_data_init3")
11+
endif()
912
foreach(lang C CXX ASM)
1013
set(commands "--log modules,libraries,initialization,redirects,sections")
1114
set(CMAKE_${lang}_LINK_EXECUTABLE

cmake/linker/iar/target.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ find_program(CMAKE_LINKER
1010
NO_DEFAULT_PATH
1111
)
1212

13+
if(CONFIG_IAR_DATA_INIT)
14+
zephyr_linker_section(NAME .iar.init_table KVMA RAM_REGION GROUP RODATA_REGION)
15+
endif()
16+
1317
add_custom_target(${IAR_LINKER})
1418
set(ILINK_THUMB_CALLS_WARNING_SUPPRESSED)
1519
set(IAR_LIB_USED)
@@ -61,6 +65,8 @@ macro(configure_linker_script linker_script_gen linker_pass_define)
6165
${STEERING_FILE_ARG}
6266
-DCONFIG_LINKER_LAST_SECTION_ID=${CONFIG_LINKER_LAST_SECTION_ID}
6367
-DCONFIG_LINKER_LAST_SECTION_ID_PATTERN=${CONFIG_LINKER_LAST_SECTION_ID_PATTERN}
68+
-DCONFIG_IAR_DATA_INIT=${CONFIG_IAR_DATA_INIT}
69+
-DCONFIG_IAR_ZEPHYR_INIT=${CONFIG_IAR_ZEPHYR_INIT}
6470
-DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/${linker_script_gen}
6571
${IAR_LIB_USED}
6672
-P ${ZEPHYR_BASE}/cmake/linker/iar/config_file_script.cmake

cmake/toolchain/iar/Kconfig

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,30 @@ choice LINKER_SCRIPT
99
default CMAKE_LINKER_GENERATOR
1010
endchoice
1111

12-
menu "IAR library options"
12+
menu "IAR options"
13+
14+
choice IAR_INIT
15+
bool "Static initialization method"
16+
depends on XIP
17+
default IAR_ZEPHYR_INIT
18+
19+
config IAR_DATA_INIT
20+
bool "IAR"
21+
select SKIP_BSS_CLEAR # IAR handles zeroing.
22+
help
23+
IAR handles initialization of static variables.
24+
Instead of `z_prep_c` calling Zephyrs `z_data_copy`
25+
we call IARs own proprietary initialization method
26+
which can save time and space.
27+
28+
config IAR_ZEPHYR_INIT
29+
bool "Zephyr"
30+
help
31+
Zephyr handles initialization of static variables.
32+
This is the regular `z_data_copy`.
33+
34+
endchoice
35+
1336

1437
config IAR_SEMIHOSTING
1538
bool "Use the IAR semihosting implementation."

scripts/ci/check_compliance.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,10 @@ def check_no_undef_outside_kconfig(self, kconf):
10711071
"HEAP_MEM_POOL_ADD_SIZE_", # Used as an option matching prefix
10721072
"HUGETLBFS", # Linux, in boards/xtensa/intel_adsp_cavs25/doc
10731073
"IAR_BUFFERED_WRITE",
1074+
"IAR_DATA_INIT",
10741075
"IAR_LIBCPP",
10751076
"IAR_SEMIHOSTING",
1077+
"IAR_ZEPHYR_INIT",
10761078
"IPC_SERVICE_ICMSG_BOND_NOTIFY_REPEAT_TO_MS", # Used in ICMsg tests for intercompatibility
10771079
# with older versions of the ICMsg.
10781080
"LIBGCC_RTLIB",

0 commit comments

Comments
 (0)