Commit 9a9e252d authored by Torsten Rasmussen's avatar Torsten Rasmussen Committed by Carles Cufi
Browse files

cmake: linker: move toolchain_ld_<base|cpp> to linker flag property



Transition the linker flag for the toolchain_ld_base and
toolchain_ld_cpp macros to linker flag properties.
This work follows the toolchain abstraction started in PR#24851.

toolchain_ld_base() was intended for base linker flags, but has slowly
become a 'set-any-linker-flag-here' and thus having several
`if(<check>)` or `<func>_ifdef(<check> ...)`.

Move the check to the top-level Zephyr CMakeLists.txt file, so that it
becomes cleaner which part is responsible for setting a value, and then
move the actual value (the linker flag) definition to the linker flag
property location.

It also helps adding support for new linkers, as it becomes clearer
which linker flags Zephyr always expects, for example `base` and
`cpp_base`, as well as those settings which are targeting for a given
purpose, such as linker sort alignment.

It also makes it clearer when those are used, for example in top-level
CMakeLists.txt with CONFIG_LINKER_SORT_BY_ALIGNMENT compared to this
information being buried in a linker support macro.

Signed-off-by: default avatarTorsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
parent d6348919
Loading
Loading
Loading
Loading
+21 −3
Original line number Diff line number Diff line
@@ -363,6 +363,20 @@ zephyr_compile_options(
# @Intent: Set fundamental linker specific flags
toolchain_ld_base()

if(DEFINED TOOLCHAIN_LD_FLAGS)
  zephyr_ld_options(${TOOLCHAIN_LD_FLAGS})
endif()

zephyr_link_libraries(PROPERTY base)

zephyr_link_libraries_ifndef(CONFIG_LINKER_USE_RELAX PROPERTY no_relax)

zephyr_link_libraries_ifdef(CONFIG_LINKER_USE_RELAX PROPERTY relax)

# Sort the common symbols and each input section by alignment
# in descending order to minimize padding between these symbols.
zephyr_link_libraries_ifdef(CONFIG_LINKER_SORT_BY_ALIGNMENT PROPERTY sort_alignment)

toolchain_ld_force_undefined_symbols(
  _OffsetAbsSyms
  _ConfigAbsSyms
@@ -373,11 +387,15 @@ if(NOT CONFIG_NATIVE_BUILD)
  toolchain_ld_baremetal()
endif()

if(CONFIG_CPP AND NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY)
if(CONFIG_CPP)
  if(NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY)
    # @Intent: Set linker specific flags for C++
    toolchain_ld_cpp()
  endif()

  zephyr_link_libraries(PROPERTY cpp_base)
endif()

# @Intent: Add the basic toolchain warning flags
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,warning_base>>)
zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,warning_base>>)
+6 −0
Original line number Diff line number Diff line
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: Apache-2.0

set_property(TARGET linker PROPERTY cpp_base -Hcplus)

# Extra warnings options for twister run
set_property(TARGET linker PROPERTY warnings_as_errors -Wl,--fatal-warnings)

check_set_linker_property(TARGET linker PROPERTY sort_alignment -Wl,--sort-section=alignment)
+0 −9
Original line number Diff line number Diff line
@@ -159,12 +159,6 @@ endmacro()

# base linker options
macro(toolchain_ld_base)
  # 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()

# generate linker script snippets from configure files
@@ -188,9 +182,6 @@ endmacro()

# link C++ libraries
macro(toolchain_ld_cpp)
  zephyr_link_libraries(
    -Hcplus
  )
endmacro()

# use linker for relocation
+7 −1
Original line number Diff line number Diff line
@@ -6,4 +6,10 @@ elseif(CONFIG_COVERAGE_NATIVE_SOURCE)
endif()

# Extra warnings options for twister run
set_property(TARGET linker PROPERTY ld_extra_warning_options -Wl,--fatal-warnings)
set_property(TARGET linker PROPERTY ld_extra_warning_options ${LINKERFLAGPREFIX},--fatal-warnings)

# GNU ld and LLVM lld complains when used with llvm/clang:
#   error: section: init_array is not contiguous with other relro sections
#
# So do not create RELRO program header.
set_property(TARGET linker APPEND PROPERTY cpp_base ${LINKERFLAGPREFIX},-z,norelro)
+20 −0
Original line number Diff line number Diff line
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: Apache-2.0

check_set_linker_property(TARGET linker PROPERTY base
                          ${LINKERFLAGPREFIX},--gc-sections
                          ${LINKERFLAGPREFIX},--build-id=none
)

if(NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY AND NOT CONFIG_EXTERNAL_MODULE_LIBCPP)
  set_property(TARGET linker PROPERTY cpp_base -lstdc++)
endif()

check_set_linker_property(TARGET linker PROPERTY memusage "${LINKERFLAGPREFIX},--print-memory-usage")

# -no-pie is not supported until binutils 2.37.
@@ -14,6 +27,13 @@ set_property(TARGET linker PROPERTY partial_linking "-r")

set_property(TARGET linker PROPERTY lto_arguments -flto -fno-ipa-sra -ffunction-sections -fdata-sections)

check_set_linker_property(TARGET linker PROPERTY no_relax ${LINKERFLAGPREFIX},--no-relax)

check_set_linker_property(TARGET linker PROPERTY sort_alignment
                          ${LINKERFLAGPREFIX},--sort-common=descending
                          ${LINKERFLAGPREFIX},--sort-section=alignment
)

# Some linker flags might not be purely ld specific, but a combination of
# linker and compiler, such as:
# --coverage for clang
Loading