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 <adithya.nagaraj.baglody@intel.com>
This commit is contained in:
Adithya Baglody 2018-11-13 15:34:02 +05:30 committed by Anas Nashif
commit 62e152a8f0
3 changed files with 44 additions and 6 deletions

View file

@ -776,7 +776,9 @@ if(CONFIG_APPLICATION_MEMORY)
) )
endif() # 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) construct_add_custom_command_for_linker_pass(linker custom_command)
add_custom_command( add_custom_command(
@ -788,6 +790,7 @@ add_custom_target(
DEPENDS DEPENDS
${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
${APP_SMEM_DEP} ${APP_SMEM_DEP}
${CODE_RELOCATION_DEP}
linker.cmd linker.cmd
offsets_h offsets_h
) )
@ -851,6 +854,28 @@ if(CONFIG_GEN_ISR_TABLES)
set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c) set_property(GLOBAL APPEND PROPERTY GENERATED_KERNEL_SOURCE_FILES isr_tables.c)
endif() 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
$<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
-d ${APPLICATION_BINARY_DIR}
-i '$<TARGET_PROPERTY:code_data_relocation_target,COMPILE_DEFINITIONS>'
-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) if(CONFIG_ARM AND CONFIG_USERSPACE)
set(GEN_PRIV_STACKS $ENV{ZEPHYR_BASE}/scripts/gen_priv_stacks.py) set(GEN_PRIV_STACKS $ENV{ZEPHYR_BASE}/scripts/gen_priv_stacks.py)
set(PROCESS_PRIV_STACKS_GPERF $ENV{ZEPHYR_BASE}/scripts/process_gperf.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 linker_app_sizing.cmd
offsets_h offsets_h
${APP_SMEM_DEP} ${APP_SMEM_DEP}
${CODE_RELOCATION_DEP}
) )
set_property(TARGET set_property(TARGET
@ -1152,9 +1178,9 @@ if(CONFIG_CPU_HAS_MPU AND CONFIG_USERSPACE)
# other ELF files are built # other ELF files are built
set(GEN_APP_ALIGN $ENV{ZEPHYR_BASE}/scripts/gen_alignment_script.py) set(GEN_APP_ALIGN $ENV{ZEPHYR_BASE}/scripts/gen_alignment_script.py)
add_executable( app_sizing_prebuilt misc/empty_file.c) 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) 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( add_custom_command(
TARGET app_sizing_prebuilt TARGET app_sizing_prebuilt
@ -1177,6 +1203,7 @@ if(CONFIG_ARM)
linker_priv_stacks_script linker_priv_stacks_script
DEPENDS DEPENDS
${ALIGN_SIZING_DEP} ${APP_SMEM_DEP} ${ALIGN_SIZING_DEP} ${APP_SMEM_DEP}
${CODE_RELOCATION_DEP}
linker_priv_stacks.cmd linker_priv_stacks.cmd
offsets_h offsets_h
) )
@ -1189,7 +1216,7 @@ if(CONFIG_ARM)
set(PRIV_STACK_LIB priv_stacks_output_obj_renamed_lib) set(PRIV_STACK_LIB priv_stacks_output_obj_renamed_lib)
add_executable( priv_stacks_prebuilt misc/empty_file.c) 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) 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) add_dependencies( priv_stacks_prebuilt ${ALIGN_SIZING_DEP} linker_priv_stacks_script offsets)
endif() endif()
@ -1198,7 +1225,7 @@ endif()
# FIXME: Is there any way to get rid of empty_file.c? # FIXME: Is there any way to get rid of empty_file.c?
add_executable( zephyr_prebuilt misc/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) 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) 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 linker_pass_final_script
DEPENDS DEPENDS
${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP}
${CODE_RELOCATION_DEP}
zephyr_prebuilt zephyr_prebuilt
linker_pass_final.cmd linker_pass_final.cmd
offsets_h offsets_h
@ -1229,7 +1257,7 @@ if(GKOF OR GKSF)
) )
add_executable( kernel_elf misc/empty_file.c ${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) 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) add_dependencies( kernel_elf ${ALIGN_SIZING_DEP} ${PRIV_STACK_DEP} linker_pass_final_script)
else() else()

View file

@ -62,6 +62,8 @@ set(__build_dir ${CMAKE_CURRENT_BINARY_DIR}/zephyr)
set(PROJECT_BINARY_DIR ${__build_dir}) 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 # 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 # due in part to how Zephyr is organized and in part to it not fitting well
# with cross compilation. # with cross compilation.

View file

@ -692,6 +692,14 @@ function(zephyr_check_compiler_flag lang option check)
endif() endif()
endfunction() 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 # 2. Kconfig-aware extensions
######################################################## ########################################################