Skip to content

Commit

Permalink
cmake: linker: make passing -no-pie configurable
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
dcpleung authored and nashif committed Mar 29, 2023
1 parent 04fd862 commit 81c3b31
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ zephyr_compile_options($<$<COMPILE_LANGUAGE:CXX>:$<TARGET_PROPERTY:compiler-cpp,

# @Intent: Do not make position independent code / executable
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.
# 'cmake -DEXTRA_CFLAGS="-Werror -Wno-deprecated-declarations" ..'
Expand Down
10 changes: 10 additions & 0 deletions cmake/linker/ld/linker_flags.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
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
# linker and compiler, such as:
# --coverage for clang
Expand Down
1 change: 0 additions & 1 deletion cmake/linker/ld/target_base.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ macro(toolchain_ld_base)
# TOOLCHAIN_LD_FLAGS comes from compiler/gcc/target.cmake
# LINKERFLAGPREFIX comes from linker/ld/target.cmake
zephyr_ld_options(
-no-pie
${TOOLCHAIN_LD_FLAGS}
)

Expand Down
4 changes: 4 additions & 0 deletions cmake/linker/linker_flags_template.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ check_set_linker_property(TARGET linker PROPERTY memusage)

# Extra warnings options for twister run
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)
2 changes: 2 additions & 0 deletions cmake/linker/lld/linker_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@

# 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)

set_property(TARGET linker PROPERTY no_position_independent "${LINKERFLAGPREFIX},--no-pie")
1 change: 0 additions & 1 deletion cmake/linker/lld/target_base.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ macro(toolchain_ld_base)
# TOOLCHAIN_LD_FLAGS comes from compiler/clang/target.cmake
# LINKERFLAGPREFIX comes from linker/lld/target.cmake
zephyr_ld_options(
-no-pie
${TOOLCHAIN_LD_FLAGS}
)

Expand Down

0 comments on commit 81c3b31

Please sign in to comment.