From 62e152a8f03245961807d6abadc626b6beaef3f7 Mon Sep 17 00:00:00 2001 From: Adithya Baglody Date: Tue, 13 Nov 2018 15:34:02 +0530 Subject: [PATCH] cmake: Added rule and helper functions for code relocation. This patch creates a rule in the cmake to trigger the generation of linker_relocate.ld and code_relocation.c files. The linker_relocate.ld will create appropriate sections and will link the required functions or variables from all the selected files. The code_relocation.c will have code that is needed for initializing data sections and copy of text sections(if XIP). Also this will contain code that is needed for zeroing of bss. The procedure to invoke this feature is: 1. Enable CONFIG_CODE_RELOCATION in the prj.conf 2. Inside CMakeList.txt in the project we need to mention all the files that needs to get relocated. zephyr_kernel_code_relocate(src/*.c SRAM2) Where the first argument is the file/files and the second argument is the memory where it has be placed. NOTE: The file argument supports glob expressions. NOTE: Step 2 can be done as many times as required. And relative paths can be given here. Signed-off-by: Adithya Baglody --- CMakeLists.txt | 40 +++++++++++++++++++++++++++++++------ cmake/app/boilerplate.cmake | 2 ++ cmake/extensions.cmake | 8 ++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a615372775..4d1c3cd0970 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -776,7 +776,9 @@ if(CONFIG_APPLICATION_MEMORY) ) endif() # CONFIG_APPLICATION_MEMORY - +if (CONFIG_CODE_DATA_RELOCATION) + set(CODE_RELOCATION_DEP code_relocation_source_lib) +endif() # CONFIG_CODE_DATA_RELOCATION construct_add_custom_command_for_linker_pass(linker custom_command) add_custom_command( @@ -788,6 +790,7 @@ add_custom_target( DEPENDS ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} ${APP_SMEM_DEP} + ${CODE_RELOCATION_DEP} linker.cmd offsets_h ) @@ -851,6 +854,28 @@ if(CONFIG_GEN_ISR_TABLES) set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c) endif() +if(CONFIG_CODE_DATA_RELOCATION) + set(MEM_RELOCATAION_LD "${PROJECT_BINARY_DIR}/include/generated/linker_relocate.ld") + set(MEM_RELOCATAION_CODE "${PROJECT_BINARY_DIR}/code_relocation.c") + + add_custom_command( + OUTPUT ${MEM_RELOCATAION_CODE} ${MEM_RELOCATAION_LD} + COMMAND + ${PYTHON_EXECUTABLE} + ${ZEPHYR_BASE}/scripts/gen_relocate_app.py + $<$:--verbose> + -d ${APPLICATION_BINARY_DIR} + -i '$' + -o ${MEM_RELOCATAION_LD} + -c ${MEM_RELOCATAION_CODE} + DEPENDS app kernel ${ZEPHYR_LIBS_PROPERTY} + ) + + add_library(code_relocation_source_lib STATIC ${MEM_RELOCATAION_CODE}) + target_link_libraries(code_relocation_source_lib zephyr_interface) + +endif() + if(CONFIG_ARM AND CONFIG_USERSPACE) set(GEN_PRIV_STACKS $ENV{ZEPHYR_BASE}/scripts/gen_priv_stacks.py) set(PROCESS_PRIV_STACKS_GPERF $ENV{ZEPHYR_BASE}/scripts/process_gperf.py) @@ -1138,6 +1163,7 @@ if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE) linker_app_sizing.cmd offsets_h ${APP_SMEM_DEP} + ${CODE_RELOCATION_DEP} ) set_property(TARGET @@ -1152,9 +1178,9 @@ if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE) # other ELF files are built set(GEN_APP_ALIGN $ENV{ZEPHYR_BASE}/scripts/gen_alignment_script.py) add_executable( app_sizing_prebuilt misc/empty_file.c) - target_link_libraries(app_sizing_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_app_sizing.cmd ${zephyr_lnk}) + target_link_libraries(app_sizing_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_app_sizing.cmd ${zephyr_lnk} ${CODE_RELOCATION_DEP}) set_property(TARGET app_sizing_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_app_sizing.cmd) - add_dependencies( app_sizing_prebuilt linker_app_sizing_script offsets ) + add_dependencies( app_sizing_prebuilt linker_app_sizing_script offsets ${CODE_RELOCATION_DEP} ) add_custom_command( TARGET app_sizing_prebuilt @@ -1177,6 +1203,7 @@ if(CONFIG_ARM) linker_priv_stacks_script DEPENDS ${ALIGN_SIZING_DEP} ${APP_SMEM_DEP} + ${CODE_RELOCATION_DEP} linker_priv_stacks.cmd offsets_h ) @@ -1189,7 +1216,7 @@ if(CONFIG_ARM) set(PRIV_STACK_LIB priv_stacks_output_obj_renamed_lib) add_executable( priv_stacks_prebuilt misc/empty_file.c) - target_link_libraries(priv_stacks_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd ${zephyr_lnk}) + target_link_libraries(priv_stacks_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd ${zephyr_lnk} ${CODE_RELOCATION_DEP}) set_property(TARGET priv_stacks_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_priv_stacks.cmd) add_dependencies( priv_stacks_prebuilt ${ALIGN_SIZING_DEP} linker_priv_stacks_script offsets) endif() @@ -1198,7 +1225,7 @@ endif() # FIXME: Is there any way to get rid of empty_file.c? add_executable( zephyr_prebuilt misc/empty_file.c) -target_link_libraries(zephyr_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker.cmd ${PRIV_STACK_LIB} ${zephyr_lnk}) +target_link_libraries(zephyr_prebuilt ${TOPT} ${PROJECT_BINARY_DIR}/linker.cmd ${PRIV_STACK_LIB} ${zephyr_lnk} ${CODE_RELOCATION_DEP}) set_property(TARGET zephyr_prebuilt PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker.cmd) add_dependencies( zephyr_prebuilt ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_script offsets) @@ -1218,6 +1245,7 @@ if(GKOF OR GKSF) linker_pass_final_script DEPENDS ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} + ${CODE_RELOCATION_DEP} zephyr_prebuilt linker_pass_final.cmd offsets_h @@ -1229,7 +1257,7 @@ if(GKOF OR GKSF) ) add_executable( kernel_elf misc/empty_file.c ${GKSF}) - target_link_libraries(kernel_elf ${GKOF} ${TOPT} ${PROJECT_BINARY_DIR}/linker_pass_final.cmd ${zephyr_lnk}) + target_link_libraries(kernel_elf ${GKOF} ${TOPT} ${PROJECT_BINARY_DIR}/linker_pass_final.cmd ${zephyr_lnk} ${CODE_RELOCATION_DEP}) set_property(TARGET kernel_elf PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/linker_pass_final.cmd) add_dependencies( kernel_elf ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_pass_final_script) else() diff --git a/cmake/app/boilerplate.cmake b/cmake/app/boilerplate.cmake index 2c616330bdb..5dee461a735 100644 --- a/cmake/app/boilerplate.cmake +++ b/cmake/app/boilerplate.cmake @@ -62,6 +62,8 @@ set(__build_dir ${CMAKE_CURRENT_BINARY_DIR}/zephyr) set(PROJECT_BINARY_DIR ${__build_dir}) +add_custom_target(code_data_relocation_target) + # CMake's 'project' concept has proven to not be very useful for Zephyr # due in part to how Zephyr is organized and in part to it not fitting well # with cross compilation. diff --git a/cmake/extensions.cmake b/cmake/extensions.cmake index 3bbfd9304c8..bfc6d6168dc 100644 --- a/cmake/extensions.cmake +++ b/cmake/extensions.cmake @@ -692,6 +692,14 @@ function(zephyr_check_compiler_flag lang option check) endif() endfunction() +# Helper function for CONFIG_CODE_DATA_RELOCATION +# Call this function with 2 arguments file and then memory location +function(zephyr_code_relocate file location) + set_property(TARGET code_data_relocation_target + APPEND PROPERTY COMPILE_DEFINITIONS + "${location}:${CMAKE_CURRENT_SOURCE_DIR}/${file}") +endfunction() + ######################################################## # 2. Kconfig-aware extensions ########################################################