Skip to content

Commit 1d949ee

Browse files
committed
cmake: llvm: use lld
Use lld linker instead of ld. Signed-off-by: Anas Nashif <[email protected]>
1 parent 22c1c50 commit 1d949ee

File tree

3 files changed

+151
-1
lines changed

3 files changed

+151
-1
lines changed

cmake/linker/lld/target.cmake

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
find_program(CMAKE_LINKER ld.lld )
4+
5+
set_ifndef(LINKERFLAGPREFIX -Wl)
6+
7+
# Run $LINKER_SCRIPT file through the C preprocessor, producing ${linker_script_gen}
8+
# NOTE: ${linker_script_gen} will be produced at build-time; not at configure-time
9+
macro(configure_linker_script linker_script_gen linker_pass_define)
10+
set(extra_dependencies ${ARGN})
11+
12+
# Different generators deal with depfiles differently.
13+
if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
14+
# Note that the IMPLICIT_DEPENDS option is currently supported only
15+
# for Makefile generators and will be ignored by other generators.
16+
set(linker_script_dep IMPLICIT_DEPENDS C ${LINKER_SCRIPT})
17+
elseif(CMAKE_GENERATOR STREQUAL "Ninja")
18+
# Using DEPFILE with other generators than Ninja is an error.
19+
set(linker_script_dep DEPFILE ${PROJECT_BINARY_DIR}/${linker_script_gen}.dep)
20+
else()
21+
# TODO: How would the linker script dependencies work for non-linker
22+
# script generators.
23+
message(STATUS "Warning; this generator is not well supported. The
24+
Linker script may not be regenerated when it should.")
25+
set(linker_script_dep "")
26+
endif()
27+
28+
zephyr_get_include_directories_for_lang(C current_includes)
29+
get_filename_component(base_name ${CMAKE_CURRENT_BINARY_DIR} NAME)
30+
get_property(current_defines GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES)
31+
32+
add_custom_command(
33+
OUTPUT ${linker_script_gen}
34+
DEPENDS
35+
${LINKER_SCRIPT}
36+
${extra_dependencies}
37+
# NB: 'linker_script_dep' will use a keyword that ends 'DEPENDS'
38+
${linker_script_dep}
39+
COMMAND ${CMAKE_C_COMPILER}
40+
-x assembler-with-cpp
41+
${NOSYSDEF_CFLAG}
42+
-MD -MF ${linker_script_gen}.dep -MT ${base_name}/${linker_script_gen}
43+
-D_LINKER
44+
-D_ASMLANGUAGE
45+
${current_includes}
46+
${current_defines}
47+
${linker_pass_define}
48+
-E ${LINKER_SCRIPT}
49+
-P # Prevent generation of debug `#line' directives.
50+
-o ${linker_script_gen}
51+
VERBATIM
52+
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
53+
COMMAND_EXPAND_LISTS
54+
)
55+
endmacro()
56+
57+
# Force symbols to be entered in the output file as undefined symbols
58+
function(toolchain_ld_force_undefined_symbols)
59+
foreach(symbol ${ARGN})
60+
zephyr_link_libraries(${LINKERFLAGPREFIX},-u,${symbol})
61+
endforeach()
62+
endfunction()
63+
64+
# Link a target to given libraries with toolchain-specific argument order
65+
#
66+
# Usage:
67+
# toolchain_ld_link_elf(
68+
# TARGET_ELF <target_elf>
69+
# OUTPUT_MAP <output_map_file_of_target>
70+
# LIBRARIES_PRE_SCRIPT [libraries_pre_script]
71+
# LINKER_SCRIPT <linker_script>
72+
# LIBRARIES_POST_SCRIPT [libraries_post_script]
73+
# DEPENDENCIES [dependencies]
74+
# )
75+
function(toolchain_ld_link_elf)
76+
cmake_parse_arguments(
77+
TOOLCHAIN_LD_LINK_ELF # prefix of output variables
78+
"" # list of names of the boolean arguments
79+
"TARGET_ELF;OUTPUT_MAP;LINKER_SCRIPT" # list of names of scalar arguments
80+
"LIBRARIES_PRE_SCRIPT;LIBRARIES_POST_SCRIPT;DEPENDENCIES" # list of names of list arguments
81+
${ARGN} # input args to parse
82+
)
83+
84+
target_link_libraries(
85+
${TOOLCHAIN_LD_LINK_ELF_TARGET_ELF}
86+
${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_PRE_SCRIPT}
87+
${TOPT}
88+
${TOOLCHAIN_LD_LINK_ELF_LINKER_SCRIPT}
89+
${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_POST_SCRIPT}
90+
91+
${LINKERFLAGPREFIX},-Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
92+
${LINKERFLAGPREFIX},--whole-archive
93+
${ZEPHYR_LIBS_PROPERTY}
94+
${LINKERFLAGPREFIX},--no-whole-archive
95+
kernel
96+
$<TARGET_OBJECTS:${OFFSETS_LIB}>
97+
${LIB_INCLUDE_DIR}
98+
-L${PROJECT_BINARY_DIR}
99+
${TOOLCHAIN_LIBS}
100+
101+
${TOOLCHAIN_LD_LINK_ELF_DEPENDENCIES}
102+
)
103+
endfunction(toolchain_ld_link_elf)
104+
105+
106+
# Load toolchain_ld-family macros
107+
include(${ZEPHYR_BASE}/cmake/linker/ld/target_base.cmake)
108+
include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_baremetal.cmake)
109+
include(${ZEPHYR_BASE}/cmake/linker/ld/target_cpp.cmake)
110+
include(${ZEPHYR_BASE}/cmake/linker/ld/target_relocation.cmake)
111+
include(${ZEPHYR_BASE}/cmake/linker/ld/target_configure.cmake)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
# See root CMakeLists.txt for description and expectations of these macros
4+
5+
macro(toolchain_ld_baremetal)
6+
7+
# LINKERFLAGPREFIX comes from linker/ld/target.cmake
8+
zephyr_ld_options(
9+
-nostdlib
10+
-static
11+
${LINKERFLAGPREFIX},-X
12+
${LINKERFLAGPREFIX},-N
13+
)
14+
15+
# Funny thing is if this is set to =error, some architectures will
16+
# skip this flag even though the compiler flag check passes
17+
# (e.g. ARC and Xtensa). So warning should be the default for now.
18+
#
19+
# Skip this for native application as Zephyr only provides
20+
# additions to the host toolchain linker script. The relocation
21+
# sections (.rel*) requires us to override those provided
22+
# by host toolchain. As we can't account for all possible
23+
# combination of compiler and linker on all machines used
24+
# for development, it is better to turn this off.
25+
#
26+
# CONFIG_LINKER_ORPHAN_SECTION_PLACE is to place the orphan sections
27+
# without any warnings or errors, which is the default behavior.
28+
# So there is no need to explicitly set a linker flag.
29+
if(CONFIG_LINKER_ORPHAN_SECTION_WARN)
30+
zephyr_ld_options(
31+
${LINKERFLAGPREFIX},--orphan-handling=warn
32+
)
33+
elseif(CONFIG_LINKER_ORPHAN_SECTION_ERROR)
34+
zephyr_ld_options(
35+
${LINKERFLAGPREFIX},--orphan-handling=error
36+
)
37+
endif()
38+
39+
endmacro()

cmake/toolchain/llvm/generic.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ endif()
99
set(LLVM_TOOLCHAIN_PATH ${CLANG_ROOT_DIR} CACHE PATH "clang install directory")
1010

1111
set(COMPILER clang)
12-
set(LINKER ld) # TODO: Use lld eventually rather than GNU ld
12+
set(LINKER lld)
1313
set(BINTOOLS llvm)
1414

1515
if("${ARCH}" STREQUAL "arm")

0 commit comments

Comments
 (0)