LLVM: add built-in lld linker support for x86.

add support to use LLVM built-in lld linker to build
zephyr applications for x86 platforms.

Signed-off-by: Chen Peng1 <peng1.chen@intel.com>
This commit is contained in:
Chen Peng1 2022-02-25 16:21:27 +08:00 committed by Carles Cufí
commit d8fa857b19
7 changed files with 90 additions and 3 deletions

View file

@ -162,6 +162,13 @@ config CMAKE_LINKER_GENERATOR
endchoice endchoice
config LLVM_USE_LD
bool "LLVM use ld linker"
depends on "${ZEPHYR_TOOLCHAIN_VARIANT}" = "llvm"
default y
help
Use binutils ld linker instead of LLVM built-in lld linker.
config HAVE_CUSTOM_LINKER_SCRIPT config HAVE_CUSTOM_LINKER_SCRIPT
bool "Custom linker script provided" bool "Custom linker script provided"
help help

View file

@ -107,7 +107,7 @@ endfunction(toolchain_ld_link_elf)
# Load toolchain_ld-family macros # Load toolchain_ld-family macros
include(${ZEPHYR_BASE}/cmake/linker/ld/target_base.cmake) include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_base.cmake)
include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_baremetal.cmake) include(${ZEPHYR_BASE}/cmake/linker/${LINKER}/target_baremetal.cmake)
include(${ZEPHYR_BASE}/cmake/linker/ld/target_cpp.cmake) include(${ZEPHYR_BASE}/cmake/linker/ld/target_cpp.cmake)
include(${ZEPHYR_BASE}/cmake/linker/ld/target_relocation.cmake) include(${ZEPHYR_BASE}/cmake/linker/ld/target_relocation.cmake)

View file

@ -4,7 +4,7 @@
macro(toolchain_ld_baremetal) macro(toolchain_ld_baremetal)
# LINKERFLAGPREFIX comes from linker/ld/target.cmake # LINKERFLAGPREFIX comes from linker/lld/target.cmake
zephyr_ld_options( zephyr_ld_options(
-nostdlib -nostdlib
-static -static
@ -12,6 +12,13 @@ macro(toolchain_ld_baremetal)
${LINKERFLAGPREFIX},-N ${LINKERFLAGPREFIX},-N
) )
# Force LLVM to use built-in lld linker
if(NOT CONFIG_LLVM_USE_LD)
zephyr_ld_options(
-fuse-ld=lld
)
endif()
# Funny thing is if this is set to =error, some architectures will # Funny thing is if this is set to =error, some architectures will
# skip this flag even though the compiler flag check passes # skip this flag even though the compiler flag check passes
# (e.g. ARC and Xtensa). So warning should be the default for now. # (e.g. ARC and Xtensa). So warning should be the default for now.

View file

@ -0,0 +1,28 @@
# SPDX-License-Identifier: Apache-2.0
# See root CMakeLists.txt for description and expectations of these macros
macro(toolchain_ld_base)
if(NOT PROPERTY_LINKER_SCRIPT_DEFINES)
set_property(GLOBAL PROPERTY PROPERTY_LINKER_SCRIPT_DEFINES -D__GCC_LINKER_CMD__)
endif()
# TOOLCHAIN_LD_FLAGS comes from compiler/clang/target.cmake
# LINKERFLAGPREFIX comes from linker/lld/target.cmake
zephyr_ld_options(
${TOOLCHAIN_LD_FLAGS}
)
zephyr_ld_options(
${LINKERFLAGPREFIX},--gc-sections
${LINKERFLAGPREFIX},--build-id=none
)
# Sort each input section by alignment.
zephyr_ld_option_ifdef(
CONFIG_LINKER_SORT_BY_ALIGNMENT
${LINKERFLAGPREFIX},--sort-section=alignment
)
endmacro()

View file

@ -9,7 +9,11 @@ endif()
set(LLVM_TOOLCHAIN_PATH ${CLANG_ROOT_DIR} CACHE PATH "clang install directory") set(LLVM_TOOLCHAIN_PATH ${CLANG_ROOT_DIR} CACHE PATH "clang install directory")
set(COMPILER clang) set(COMPILER clang)
set(LINKER lld) if(CONFIG_LLVM_USE_LD)
set(LINKER ld)
else()
set(LINKER lld)
endif()
set(BINTOOLS llvm) set(BINTOOLS llvm)
if("${ARCH}" STREQUAL "arm") if("${ARCH}" STREQUAL "arm")

View file

@ -313,6 +313,15 @@ SECTIONS
/* Needs KEEP() as ENTRY() is given a physical address */ /* Needs KEEP() as ENTRY() is given a physical address */
KEEP(*(.text.__start)) KEEP(*(.text.__start))
/*
* We need these sections to extract interrupt information, but they
* will be removed with "--gc-sections" by LLVM lld, so add keep
* command to save them.
*/
#ifndef CONFIG_LLVM_USE_LD
KEEP(*(.text.irqstubs))
KEEP(*(".gnu.linkonce.t.exc_*_stub"))
#endif
*(.text) *(.text)
*(".text.*") *(".text.*")
*(.gnu.linkonce.t.*) *(.gnu.linkonce.t.*)
@ -532,7 +541,22 @@ SECTIONS
#include <zephyr/linker/debug-sections.ld> #include <zephyr/linker/debug-sections.ld>
/DISCARD/ : { *(.note.GNU-stack) } /DISCARD/ : { *(.note.GNU-stack) }
/*
* eh_frame section won't be removed even with "--gc-sections" by LLVM lld.
*/
#if !defined(CONFIG_EXCEPTIONS)
/DISCARD/ : { *(.eh_frame) }
#endif
/*
* The sections below are still treated as warnings
* with "--orphan-handling=warn" by LLVM lld.
*/
#if !defined(CONFIG_LLVM_USE_LD)
.symtab 0 : { *(.symtab) }
.strtab 0 : { *(.strtab) }
.shstrtab 0 : { *(.shstrtab) }
#endif
} }
#ifdef CONFIG_XIP #ifdef CONFIG_XIP

View file

@ -217,4 +217,21 @@ SECTIONS
*(.rel.*) *(.rel.*)
*(.rela.*) *(.rela.*)
} }
/*
* eh_frame section won't be removed even with "--gc-sections" by LLVM lld.
*/
#if !defined(CONFIG_EXCEPTIONS)
/DISCARD/ : { *(.eh_frame) }
#endif
/*
* The sections below are still treated as warnings
* with "--orphan-handling=warn" by LLVM lld.
*/
#if !defined(CONFIG_LLVM_USE_LD)
.symtab 0 : { *(.symtab) }
.strtab 0 : { *(.strtab) }
.shstrtab 0 : { *(.shstrtab) }
#endif
} }