ld.bfd: ensure that ld.bfd is preferred over ld.

Fixes: #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>
This commit is contained in:
Torsten Rasmussen 2021-05-26 14:23:48 +02:00 committed by Kumar Gala
commit 39f06e0195

View file

@ -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)
@ -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}