zephyr/cmake/linker/ld/target_baremetal.cmake
Yuval Peress a81f8af701 build: Fix native_posix build on GNU GCC
Update -fno-pie to only be added when relevant. This fixes native_posix
builds on Debian hosts.

* Debian and other Linux distributions have ld with --warn-textrel as
default.
* The option generates a warning when linking on x86.
* The warning make scripts/twister fail because we treat all warnings
as error.
* The warning is issued when no PIE objects are linked against PIE
library.
* PIE (Position Independent Executable) or not can be seen with the
following command.
* When you want to generate non PIE, link with `-no-pie.`

Non PIE

  $ readelf -l build/zephyr/zephyr.elf | grep 'Elf file type is'
  Elf file type is EXEC (Executable file)

PIE

  $ readelf -l /lib/x86_64-linux-gnu/libc.so.6 | \
  grep 'Elf file type is'
  Elf file type is DYN (Shared object file)

Issue #35244

Signed-off-by: Yuval Peress <peress@google.com>
2021-10-05 05:44:32 -04:00

40 lines
1.3 KiB
CMake

# SPDX-License-Identifier: Apache-2.0
# See root CMakeLists.txt for description and expectations of these macros
macro(toolchain_ld_baremetal)
# LINKERFLAGPREFIX comes from linker/ld/target.cmake
zephyr_ld_options(
-nostdlib
-static
${LINKERFLAGPREFIX},-X
${LINKERFLAGPREFIX},-N
)
# Funny thing is if this is set to =error, some architectures will
# skip this flag even though the compiler flag check passes
# (e.g. ARC and Xtensa). So warning should be the default for now.
#
# Skip this for native application as Zephyr only provides
# additions to the host toolchain linker script. The relocation
# sections (.rel*) requires us to override those provided
# by host toolchain. As we can't account for all possible
# combination of compiler and linker on all machines used
# for development, it is better to turn this off.
#
# CONFIG_LINKER_ORPHAN_SECTION_PLACE is to place the orphan sections
# without any warnings or errors, which is the default behavior.
# So there is no need to explicitly set a linker flag.
if(CONFIG_LINKER_ORPHAN_SECTION_WARN)
zephyr_ld_options(
${LINKERFLAGPREFIX},--orphan-handling=warn
)
elseif(CONFIG_LINKER_ORPHAN_SECTION_ERROR)
zephyr_ld_options(
${LINKERFLAGPREFIX},--orphan-handling=error
)
endif()
endmacro()