2020-07-03 18:13:49 +08:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2023-04-26 10:30:28 +02:00
|
|
|
set_property(TARGET linker PROPERTY devices_start_symbol "_device_list_start")
|
2020-07-03 18:13:49 +08:00
|
|
|
|
2022-04-26 21:58:24 +09:00
|
|
|
find_program(CMAKE_LINKER ${CROSS_COMPILE}lldac PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
|
2020-07-03 18:13:49 +08:00
|
|
|
|
|
|
|
# the prefix to transfer linker options from compiler
|
|
|
|
set_ifndef(LINKERFLAGPREFIX -Wl,)
|
|
|
|
|
|
|
|
# Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen}
|
|
|
|
# NOTE: ${linker_script_gen} will be produced at build-time; not at configure-time
|
|
|
|
macro(configure_linker_script linker_script_gen linker_pass_define)
|
|
|
|
set(extra_dependencies ${ARGN})
|
2021-11-01 12:53:28 +01:00
|
|
|
set(template_script_defines ${linker_pass_define})
|
|
|
|
list(TRANSFORM template_script_defines PREPEND "-D")
|
2020-07-03 18:13:49 +08:00
|
|
|
|
|
|
|
# Different generators deal with depfiles differently.
|
|
|
|
if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
|
|
|
|
# Note that the IMPLICIT_DEPENDS option is currently supported only
|
|
|
|
# for Makefile generators and will be ignored by other generators.
|
|
|
|
set(linker_script_dep IMPLICIT_DEPENDS C ${LINKER_SCRIPT})
|
|
|
|
elseif(CMAKE_GENERATOR STREQUAL "Ninja")
|
|
|
|
# Using DEPFILE with other generators than Ninja is an error.
|
|
|
|
set(linker_script_dep DEPFILE ${PROJECT_BINARY_DIR}/${linker_script_gen}.dep)
|
|
|
|
else()
|
|
|
|
# TODO: How would the linker script dependencies work for non-linker
|
|
|
|
# script generators.
|
|
|
|
message(STATUS "Warning; this generator is not well supported. The
|
|
|
|
Linker script may not be regenerated when it should.")
|
|
|
|
set(linker_script_dep "")
|
|
|
|
endif()
|
|
|
|
|
2020-11-20 10:56:28 +01:00
|
|
|
zephyr_get_include_directories_for_lang(C current_includes)
|
2020-07-03 18:13:49 +08:00
|
|
|
get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES)
|
|
|
|
|
|
|
|
# the command to generate linker file from template
|
|
|
|
add_custom_command(
|
|
|
|
OUTPUT ${linker_script_gen}
|
|
|
|
DEPENDS
|
|
|
|
${LINKER_SCRIPT}
|
2021-07-12 11:16:21 -07:00
|
|
|
${AUTOCONF_H}
|
2020-07-03 18:13:49 +08:00
|
|
|
${extra_dependencies}
|
|
|
|
# NB: 'linker_script_dep' will use a keyword that ends 'DEPENDS'
|
|
|
|
${linker_script_dep}
|
|
|
|
COMMAND ${CMAKE_C_COMPILER}
|
|
|
|
-x c
|
|
|
|
${NOSYSDEF_CFLAG}
|
|
|
|
-Hnocopyr
|
2022-03-14 15:25:22 +01:00
|
|
|
-MD -MF ${linker_script_gen}.dep -MT ${linker_script_gen}
|
2020-07-03 18:13:49 +08:00
|
|
|
-D_LINKER
|
|
|
|
-D_ASMLANGUAGE
|
2021-07-12 11:16:21 -07:00
|
|
|
-imacros ${AUTOCONF_H}
|
2020-07-03 18:13:49 +08:00
|
|
|
${current_includes}
|
|
|
|
${current_defines}
|
2021-11-01 12:53:28 +01:00
|
|
|
${template_script_defines}
|
2020-07-03 18:13:49 +08:00
|
|
|
${LINKER_SCRIPT}
|
2021-03-17 11:34:38 +08:00
|
|
|
-E
|
2020-07-03 18:13:49 +08:00
|
|
|
-o ${linker_script_gen}
|
|
|
|
VERBATIM
|
|
|
|
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
2020-09-04 23:52:56 +02:00
|
|
|
COMMAND_EXPAND_LISTS
|
|
|
|
)
|
2020-07-03 18:13:49 +08:00
|
|
|
endmacro()
|
|
|
|
|
|
|
|
# Force symbols to be entered in the output file as undefined symbols
|
|
|
|
function(toolchain_ld_force_undefined_symbols)
|
|
|
|
foreach(symbol ${ARGN})
|
|
|
|
zephyr_link_libraries(${LINKERFLAGPREFIX}-u${symbol})
|
|
|
|
endforeach()
|
|
|
|
endfunction()
|
|
|
|
|
|
|
|
# Link a target to given libraries with toolchain-specific argument order
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# toolchain_ld_link_elf(
|
|
|
|
# TARGET_ELF <target_elf>
|
|
|
|
# OUTPUT_MAP <output_map_file_of_target>
|
|
|
|
# LIBRARIES_PRE_SCRIPT [libraries_pre_script]
|
|
|
|
# LINKER_SCRIPT <linker_script>
|
|
|
|
# LIBRARIES_POST_SCRIPT [libraries_post_script]
|
|
|
|
# DEPENDENCIES [dependencies]
|
|
|
|
# )
|
|
|
|
function(toolchain_ld_link_elf)
|
|
|
|
cmake_parse_arguments(
|
|
|
|
TOOLCHAIN_LD_LINK_ELF # prefix of output variables
|
|
|
|
"" # list of names of the boolean arguments
|
|
|
|
"TARGET_ELF;OUTPUT_MAP;LINKER_SCRIPT" # list of names of scalar arguments
|
|
|
|
"LIBRARIES_PRE_SCRIPT;LIBRARIES_POST_SCRIPT;DEPENDENCIES" # list of names of list arguments
|
|
|
|
${ARGN} # input args to parse
|
|
|
|
)
|
|
|
|
|
|
|
|
target_link_libraries(
|
|
|
|
${TOOLCHAIN_LD_LINK_ELF_TARGET_ELF}
|
|
|
|
${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_PRE_SCRIPT}
|
|
|
|
${LINKERFLAGPREFIX}-T${TOOLCHAIN_LD_LINK_ELF_LINKER_SCRIPT}
|
|
|
|
${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_POST_SCRIPT}
|
|
|
|
${LINKERFLAGPREFIX}--gc-sections
|
|
|
|
${LINKERFLAGPREFIX}--entry=__start
|
|
|
|
${LINKERFLAGPREFIX}--Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
|
|
|
|
${LINKERFLAGPREFIX}--whole-archive
|
linker: Include libkernel.a in the whole-archive when llext is enabled
Differently from other libraries, which are included whole in the final
Zephyr ELF, libkernel.a itself isn't. Assuming this is intended to
enable optimisations (if it isn't, this patch will break things) - linker
can remove parts of the kernel that are not used by the application.
However, when considering Linkable Loadable Extensions (llext), this
optimisations can be counterproductive: for instance, syscalls that are
not used by the application won't be available for extensions. It won't
matter if someone "EXPORT_SYMBOL" for them, or even try to keep them
using LINKER_KEEP, they'll be gone.
To avoid that, this patches includes, when CONFIG_LLEXT=y, libkernel.a
inside the linker "whole-archive" block. This ends up making it consider
libkernel.a as a library whose all symbols should be kept. Note this
doesn't mean that all symbols will be there - things compiled out via
Kconfig will naturally still be out.
Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
2024-03-21 15:50:13 -07:00
|
|
|
${WHOLE_ARCHIVE_LIBS}
|
2020-07-03 18:13:49 +08:00
|
|
|
${LINKERFLAGPREFIX}--no-whole-archive
|
linker: Include libkernel.a in the whole-archive when llext is enabled
Differently from other libraries, which are included whole in the final
Zephyr ELF, libkernel.a itself isn't. Assuming this is intended to
enable optimisations (if it isn't, this patch will break things) - linker
can remove parts of the kernel that are not used by the application.
However, when considering Linkable Loadable Extensions (llext), this
optimisations can be counterproductive: for instance, syscalls that are
not used by the application won't be available for extensions. It won't
matter if someone "EXPORT_SYMBOL" for them, or even try to keep them
using LINKER_KEEP, they'll be gone.
To avoid that, this patches includes, when CONFIG_LLEXT=y, libkernel.a
inside the linker "whole-archive" block. This ends up making it consider
libkernel.a as a library whose all symbols should be kept. Note this
doesn't mean that all symbols will be there - things compiled out via
Kconfig will naturally still be out.
Signed-off-by: Ederson de Souza <ederson.desouza@intel.com>
2024-03-21 15:50:13 -07:00
|
|
|
${NO_WHOLE_ARCHIVE_LIBS}
|
2020-07-03 18:13:49 +08:00
|
|
|
$<TARGET_OBJECTS:${OFFSETS_LIB}>
|
|
|
|
${LIB_INCLUDE_DIR}
|
|
|
|
-L${PROJECT_BINARY_DIR}
|
|
|
|
${TOOLCHAIN_LIBS}
|
|
|
|
|
|
|
|
${TOOLCHAIN_LD_LINK_ELF_DEPENDENCIES}
|
|
|
|
)
|
|
|
|
endfunction(toolchain_ld_link_elf)
|
|
|
|
|
|
|
|
# linker options of temporary linkage for code generation
|
|
|
|
macro(toolchain_ld_baremetal)
|
|
|
|
zephyr_ld_options(
|
|
|
|
-Hlld
|
|
|
|
-Hnosdata
|
|
|
|
-Xtimer0 # to suppress the warning message
|
2021-03-11 22:20:46 +03:00
|
|
|
-Hnoxcheck_obj
|
2020-07-03 18:13:49 +08:00
|
|
|
-Hnocplus
|
2021-07-09 00:57:19 +03:00
|
|
|
-Hhostlib=
|
2020-07-03 18:13:49 +08:00
|
|
|
-Hheap=0
|
|
|
|
-Hnoivt
|
2021-08-30 22:40:45 +03:00
|
|
|
-Hnocrt
|
2020-07-03 18:13:49 +08:00
|
|
|
)
|
|
|
|
|
2021-08-12 00:44:52 +03:00
|
|
|
# There are two options:
|
|
|
|
# - We have full MWDT libc support and we link MWDT libc - this is default
|
|
|
|
# behavior and we don't need to do something for that.
|
|
|
|
# - We use minimal libc provided by Zephyr itself. In that case we must not
|
|
|
|
# link MWDT libc, but we still need to link libmw
|
|
|
|
if(CONFIG_MINIMAL_LIBC)
|
|
|
|
zephyr_ld_options(
|
|
|
|
-Hnolib
|
|
|
|
-Hldopt=-lmw
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
2020-07-03 18:13:49 +08:00
|
|
|
# Funny thing is if this is set to =error, some architectures will
|
|
|
|
# skip this flag even though the compiler flag check passes
|
|
|
|
# (e.g. ARC and Xtensa). So warning should be the default for now.
|
|
|
|
#
|
|
|
|
# Skip this for native application as Zephyr only provides
|
|
|
|
# additions to the host toolchain linker script. The relocation
|
|
|
|
# sections (.rel*) requires us to override those provided
|
|
|
|
# by host toolchain. As we can't account for all possible
|
|
|
|
# combination of compiler and linker on all machines used
|
|
|
|
# for development, it is better to turn this off.
|
|
|
|
#
|
|
|
|
# CONFIG_LINKER_ORPHAN_SECTION_PLACE is to place the orphan sections
|
|
|
|
# without any warnings or errors, which is the default behavior.
|
|
|
|
# So there is no need to explicitly set a linker flag.
|
|
|
|
if(CONFIG_LINKER_ORPHAN_SECTION_WARN)
|
|
|
|
message(WARNING "MWDT toolchain does not support
|
|
|
|
CONFIG_LINKER_ORPHAN_SECTION_WARN")
|
|
|
|
elseif(CONFIG_LINKER_ORPHAN_SECTION_ERROR)
|
|
|
|
zephyr_ld_options(
|
|
|
|
${LINKERFLAGPREFIX}--orphan-handling=error)
|
|
|
|
endif()
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
# base linker options
|
|
|
|
macro(toolchain_ld_base)
|
|
|
|
if(NOT PROPERTY_LINKER_SCRIPT_DEFINES)
|
|
|
|
set_property(GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES -D__MWDT_LINKER_CMD__)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
# Sort the common symbols and each input section by alignment
|
|
|
|
# in descending order to minimize padding between these symbols.
|
|
|
|
zephyr_ld_option_ifdef(
|
|
|
|
CONFIG_LINKER_SORT_BY_ALIGNMENT
|
|
|
|
${LINKERFLAGPREFIX}--sort-section=alignment
|
|
|
|
)
|
|
|
|
endmacro()
|
|
|
|
|
2022-03-16 21:07:43 +00:00
|
|
|
# generate linker script snippets from configure files
|
2020-07-03 18:13:49 +08:00
|
|
|
macro(toolchain_ld_configure_files)
|
|
|
|
configure_file(
|
2022-03-29 13:52:59 -06:00
|
|
|
$ENV{ZEPHYR_BASE}/include/zephyr/arch/common/app_data_alignment.ld
|
2020-07-03 18:13:49 +08:00
|
|
|
${PROJECT_BINARY_DIR}/include/generated/app_data_alignment.ld)
|
|
|
|
|
|
|
|
configure_file(
|
2022-03-29 13:52:59 -06:00
|
|
|
$ENV{ZEPHYR_BASE}/include/zephyr/linker/app_smem.ld
|
2020-07-03 18:13:49 +08:00
|
|
|
${PROJECT_BINARY_DIR}/include/generated/app_smem.ld)
|
|
|
|
|
|
|
|
configure_file(
|
2022-03-29 13:52:59 -06:00
|
|
|
$ENV{ZEPHYR_BASE}/include/zephyr/linker/app_smem_aligned.ld
|
2020-07-03 18:13:49 +08:00
|
|
|
${PROJECT_BINARY_DIR}/include/generated/app_smem_aligned.ld)
|
|
|
|
|
|
|
|
configure_file(
|
2022-03-29 13:52:59 -06:00
|
|
|
$ENV{ZEPHYR_BASE}/include/zephyr/linker/app_smem_unaligned.ld
|
2020-07-03 18:13:49 +08:00
|
|
|
${PROJECT_BINARY_DIR}/include/generated/app_smem_unaligned.ld)
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
# link C++ libraries
|
|
|
|
macro(toolchain_ld_cpp)
|
|
|
|
zephyr_link_libraries(
|
2021-06-02 14:46:07 +03:00
|
|
|
-Hcplus
|
2020-07-03 18:13:49 +08:00
|
|
|
)
|
|
|
|
endmacro()
|
|
|
|
|
|
|
|
# use linker for relocation
|
|
|
|
macro(toolchain_ld_relocation)
|
|
|
|
set(MEM_RELOCATION_LD "${PROJECT_BINARY_DIR}/include/generated/linker_relocate.ld")
|
|
|
|
set(MEM_RELOCATION_SRAM_DATA_LD
|
|
|
|
"${PROJECT_BINARY_DIR}/include/generated/linker_sram_data_relocate.ld")
|
|
|
|
set(MEM_RELOCATION_SRAM_BSS_LD
|
|
|
|
"${PROJECT_BINARY_DIR}/include/generated/linker_sram_bss_relocate.ld")
|
|
|
|
set(MEM_RELOCATION_CODE "${PROJECT_BINARY_DIR}/code_relocation.c")
|
|
|
|
|
|
|
|
add_custom_command(
|
|
|
|
OUTPUT ${MEM_RELOCATION_CODE} ${MEM_RELOCATION_LD}
|
|
|
|
COMMAND
|
|
|
|
${PYTHON_EXECUTABLE}
|
2022-07-11 10:53:51 -04:00
|
|
|
${ZEPHYR_BASE}/scripts/build/gen_relocate_app.py
|
2020-07-03 18:13:49 +08:00
|
|
|
$<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:--verbose>
|
|
|
|
-d ${APPLICATION_BINARY_DIR}
|
2020-12-08 14:52:10 +01:00
|
|
|
-i \"$<TARGET_PROPERTY:code_data_relocation_target,COMPILE_DEFINITIONS>\"
|
2020-07-03 18:13:49 +08:00
|
|
|
-o ${MEM_RELOCATION_LD}
|
|
|
|
-s ${MEM_RELOCATION_SRAM_DATA_LD}
|
|
|
|
-b ${MEM_RELOCATION_SRAM_BSS_LD}
|
|
|
|
-c ${MEM_RELOCATION_CODE}
|
|
|
|
DEPENDS app kernel ${ZEPHYR_LIBS_PROPERTY}
|
|
|
|
)
|
|
|
|
|
|
|
|
add_library(code_relocation_source_lib STATIC ${MEM_RELOCATION_CODE})
|
2022-02-10 13:54:49 -05:00
|
|
|
target_include_directories(code_relocation_source_lib PRIVATE
|
|
|
|
${ZEPHYR_BASE}/kernel/include ${ARCH_DIR}/${ARCH}/include)
|
2020-07-03 18:13:49 +08:00
|
|
|
target_link_libraries(code_relocation_source_lib zephyr_interface)
|
|
|
|
endmacro()
|