1# SPDX-License-Identifier: Apache-2.0
2
3# See root CMakeLists.txt for description and expectations of these macros
4
5macro(toolchain_ld_base)
6
7  if(NOT PROPERTY_LINKER_SCRIPT_DEFINES)
8    set_property(GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES -D__LLD_LINKER_CMD__)
9  endif()
10
11  # TOOLCHAIN_LD_FLAGS comes from compiler/clang/target.cmake
12  # LINKERFLAGPREFIX comes from linker/lld/target.cmake
13  zephyr_ld_options(
14    ${TOOLCHAIN_LD_FLAGS}
15  )
16
17  zephyr_ld_options(
18    ${LINKERFLAGPREFIX},--gc-sections
19    ${LINKERFLAGPREFIX},--build-id=none
20  )
21
22  # Sort each input section by alignment.
23  zephyr_ld_option_ifdef(
24    CONFIG_LINKER_SORT_BY_ALIGNMENT
25    ${LINKERFLAGPREFIX},--sort-section=alignment
26  )
27
28  if (NOT CONFIG_LINKER_USE_RELAX)
29    zephyr_ld_options(
30      ${LINKERFLAGPREFIX},--no-relax
31    )
32  endif()
33
34  if(CONFIG_CPP)
35    # LLVM lld complains:
36    #   error: section: init_array is not contiguous with other relro sections
37    #
38    # So do not create RELRO program header.
39    zephyr_link_libraries(
40      -Wl,-z,norelro
41    )
42  endif()
43
44  if(CONFIG_LIBGCC_RTLIB)
45    set(runtime_lib "libgcc")
46  elseif(CONFIG_COMPILER_RT_RTLIB)
47    set(runtime_lib "compiler_rt")
48  endif()
49
50  zephyr_link_libraries(
51    --config ${ZEPHYR_BASE}/cmake/toolchain/llvm/clang_${runtime_lib}.cfg
52  )
53endmacro()
54