Skip to content

Commit

Permalink
ld.bfd: ensure that ld.bfd is preferred over ld.
Browse files Browse the repository at this point in the history
Fixes: zephyrproject-rtos#32237

When building for native_posix, then host tools are used.
This means that gcc will link using `/usr/bin/ld` per default.

If ld points to lld, then linking will fail.

This commit will first look for ld.bfd, and if found then use
-fuse-ld=bfd for linking. If ld.bfd is not found, then ld is used as
fallback as that will be assumed to be the best working candidate.

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
  • Loading branch information
tejlmand committed May 26, 2021
1 parent f451cd3 commit 91a77a9
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cmake/linker/ld/target.cmake
@@ -1,6 +1,15 @@
# SPDX-License-Identifier: Apache-2.0

find_program(CMAKE_LINKER ${CROSS_COMPILE}ld PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
if(DEFINED TOOLCHAIN_HOME)
# When Toolchain home is defined, then we are cross-compiling, so only look
# for linker in that path, else we are using host tools.
set(LD_SEARCH_PATH PATHS ${TOOLCHAIN_HOME} NO_DEFAULT_PATH)
endif()

find_program(CMAKE_LINKER ${CROSS_COMPILE}ld.bfd ${LD_SEARCH_PATH})
if(NOT CMAKE_LINKER)
find_program(CMAKE_LINKER ${CROSS_COMPILE}ld ${LD_SEARCH_PATH})
endif()

set_ifndef(LINKERFLAGPREFIX -Wl)

Expand Down Expand Up @@ -82,9 +91,15 @@ function(toolchain_ld_link_elf)
${ARGN} # input args to parse
)

if(${CMAKE_LINKER} STREQUAL "${CROSS_COMPILE}ld.bfd")
# ld.bfd was found so let's explicitly use that for linking, see #32237
set(use_linker "-fuse-ld=bfd")
endif()

target_link_libraries(
${TOOLCHAIN_LD_LINK_ELF_TARGET_ELF}
${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_PRE_SCRIPT}
${use_linker}
${TOPT}
${TOOLCHAIN_LD_LINK_ELF_LINKER_SCRIPT}
${TOOLCHAIN_LD_LINK_ELF_LIBRARIES_POST_SCRIPT}
Expand Down

0 comments on commit 91a77a9

Please sign in to comment.