cmake: linker: make passing -no-pie configurable

This adds a new linker property specifically for passing
"-no-pie" to linker. Older binutils' LD (<= 2.36) do not
support this flag and will behave erratically if set. It
would parse "-no-pie" separately as "-n" and "-o-pie",
which would result in the output file being "-pie"
instead of "zephyr*.elf". Moreover, LLVM lld does not
support -no-pie but --no-pie (note the extra hyphen).
By having no-pie as a linker property, we can pass
correct no-pie flag to these linkers (or none at all).

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2023-02-24 17:39:07 -08:00 committed by Anas Nashif
commit 81c3b3152c
6 changed files with 17 additions and 2 deletions

View file

@ -378,6 +378,7 @@ zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,
# @Intent: Do not make position independent code / executable # @Intent: Do not make position independent code / executable
zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_position_independent>>) zephyr_compile_options($<$<COMPILE_LANGUAGE:C>:$<TARGET_PROPERTY:compiler,no_position_independent>>)
zephyr_link_libraries($<TARGET_PROPERTY:linker,no_position_independent>)
# Allow the user to inject options when calling cmake, e.g. # Allow the user to inject options when calling cmake, e.g.
# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..' # 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'

View file

@ -1,5 +1,15 @@
check_set_linker_property(TARGET linker PROPERTY memusage "${LINKERFLAGPREFIX},--print-memory-usage") check_set_linker_property(TARGET linker PROPERTY memusage "${LINKERFLAGPREFIX},--print-memory-usage")
# -no-pie is not supported until binutils 2.37.
# If -no-pie is passed to old binutils <= 2.36, it is parsed
# as separate arguments -n and -o, which results in output file
# called "-pie".
if("${GNULD_VERSION_STRING}" VERSION_GREATER_EQUAL 2.37)
set_property(TARGET linker PROPERTY no_position_independent "${LINKERFLAGPREFIX},-no-pie")
else()
set_property(TARGET linker PROPERTY no_position_independent)
endif()
# Some linker flags might not be purely ld specific, but a combination of # Some linker flags might not be purely ld specific, but a combination of
# linker and compiler, such as: # linker and compiler, such as:
# --coverage for clang # --coverage for clang

View file

@ -11,7 +11,6 @@ macro(toolchain_ld_base)
# TOOLCHAIN_LD_FLAGS comes from compiler/gcc/target.cmake # TOOLCHAIN_LD_FLAGS comes from compiler/gcc/target.cmake
# LINKERFLAGPREFIX comes from linker/ld/target.cmake # LINKERFLAGPREFIX comes from linker/ld/target.cmake
zephyr_ld_options( zephyr_ld_options(
-no-pie
${TOOLCHAIN_LD_FLAGS} ${TOOLCHAIN_LD_FLAGS}
) )

View file

@ -12,3 +12,7 @@ check_set_linker_property(TARGET linker PROPERTY memusage)
# Extra warnings options for twister run # Extra warnings options for twister run
set_property(TARGET linker PROPERTY warnings_as_errors) set_property(TARGET linker PROPERTY warnings_as_errors)
# Linker flag for disabling position independent binaries,
# such as, "-no-pie" for LD, and "--no-pie" for LLD.
set_property(TARGET linker PROPERTY no_position_independent)

View file

@ -3,3 +3,5 @@
# Since lld is a drop in replacement for ld, we can just use ld's flags # Since lld is a drop in replacement for ld, we can just use ld's flags
include(${ZEPHYR_BASE}/cmake/linker/ld/${COMPILER}/linker_flags.cmake OPTIONAL) include(${ZEPHYR_BASE}/cmake/linker/ld/${COMPILER}/linker_flags.cmake OPTIONAL)
set_property(TARGET linker PROPERTY no_position_independent "${LINKERFLAGPREFIX},--no-pie")

View file

@ -11,7 +11,6 @@ macro(toolchain_ld_base)
# TOOLCHAIN_LD_FLAGS comes from compiler/clang/target.cmake # TOOLCHAIN_LD_FLAGS comes from compiler/clang/target.cmake
# LINKERFLAGPREFIX comes from linker/lld/target.cmake # LINKERFLAGPREFIX comes from linker/lld/target.cmake
zephyr_ld_options( zephyr_ld_options(
-no-pie
${TOOLCHAIN_LD_FLAGS} ${TOOLCHAIN_LD_FLAGS}
) )