diff --git a/CMakeLists.txt b/CMakeLists.txt index 540202fc4b6..d286637203f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -385,6 +385,28 @@ toolchain_ld_force_undefined_symbols( if(NOT CONFIG_NATIVE_BUILD) # @Intent: Set linker specific flags for bare metal target toolchain_ld_baremetal() + + zephyr_link_libraries(PROPERTY baremetal) + + # Note that some architectures will skip this flag if set to error, 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_link_libraries(PROPERTY orphan_warning) + elseif(CONFIG_LINKER_ORPHAN_SECTION_ERROR) + zephyr_link_libraries(PROPERTY orphan_error) + endif() endif() if(CONFIG_CPP) diff --git a/cmake/linker/arcmwdt/linker_flags.cmake b/cmake/linker/arcmwdt/linker_flags.cmake index bd8ec002226..63aead6e93b 100644 --- a/cmake/linker/arcmwdt/linker_flags.cmake +++ b/cmake/linker/arcmwdt/linker_flags.cmake @@ -4,6 +4,38 @@ set_property(TARGET linker PROPERTY cpp_base -Hcplus) +check_set_linker_property(TARGET linker PROPERTY baremetal + -Hlld + -Hnosdata + -Xtimer0 # to suppress the warning message + -Hnoxcheck_obj + -Hnocplus + -Hhostlib= + -Hheap=0 + -Hnoivt + -Hnocrt +) + +# There are two options: +# - We have full MWDT libc support and we link MWDT libc - this is default +# behavior and we don't need to do something for that. +# - We use minimal libc provided by Zephyr itself. In that case we must not +# link MWDT libc, but we still need to link libmw +if(CONFIG_MINIMAL_LIBC) + check_set_linker_property(TARGET linker APPEND PROPERTY baremetal + -Hnolib + -Hldopt=-lmw + ) +endif() + +check_set_linker_property(TARGET linker PROPERTY orphan_warning + ${LINKERFLAGPREFIX},--orphan-handling=warn +) + +check_set_linker_property(TARGET linker PROPERTY orphan_error + ${LINKERFLAGPREFIX},--orphan-handling=error +) + # Extra warnings options for twister run set_property(TARGET linker PROPERTY warnings_as_errors -Wl,--fatal-warnings) diff --git a/cmake/linker/arcmwdt/target.cmake b/cmake/linker/arcmwdt/target.cmake index af14f89f06f..997bc8afb0c 100644 --- a/cmake/linker/arcmwdt/target.cmake +++ b/cmake/linker/arcmwdt/target.cmake @@ -110,51 +110,6 @@ endfunction(toolchain_ld_link_elf) # linker options of temporary linkage for code generation macro(toolchain_ld_baremetal) - zephyr_ld_options( - -Hlld - -Hnosdata - -Xtimer0 # to suppress the warning message - -Hnoxcheck_obj - -Hnocplus - -Hhostlib= - -Hheap=0 - -Hnoivt - -Hnocrt - ) - - # There are two options: - # - We have full MWDT libc support and we link MWDT libc - this is default - # behavior and we don't need to do something for that. - # - We use minimal libc provided by Zephyr itself. In that case we must not - # link MWDT libc, but we still need to link libmw - if(CONFIG_MINIMAL_LIBC) - zephyr_ld_options( - -Hnolib - -Hldopt=-lmw - ) - endif() - - # 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) - message(WARNING "MWDT toolchain does not support - CONFIG_LINKER_ORPHAN_SECTION_WARN") - elseif(CONFIG_LINKER_ORPHAN_SECTION_ERROR) - zephyr_ld_options( - ${LINKERFLAGPREFIX}--orphan-handling=error) - endif() endmacro() # base linker options diff --git a/cmake/linker/ld/linker_flags.cmake b/cmake/linker/ld/linker_flags.cmake index f7d7a17743a..49bba260d3a 100644 --- a/cmake/linker/ld/linker_flags.cmake +++ b/cmake/linker/ld/linker_flags.cmake @@ -11,6 +11,21 @@ if(NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY AND NOT CONFIG_EXTERN set_property(TARGET linker PROPERTY cpp_base -lstdc++) endif() +check_set_linker_property(TARGET linker PROPERTY baremetal + -nostdlib + -static + ${LINKERFLAGPREFIX},-X + ${LINKERFLAGPREFIX},-N +) + +check_set_linker_property(TARGET linker PROPERTY orphan_warning + ${LINKERFLAGPREFIX},--orphan-handling=warn +) + +check_set_linker_property(TARGET linker PROPERTY orphan_error + ${LINKERFLAGPREFIX},--orphan-handling=error +) + check_set_linker_property(TARGET linker PROPERTY memusage "${LINKERFLAGPREFIX},--print-memory-usage") # -no-pie is not supported until binutils 2.37. diff --git a/cmake/linker/ld/target_baremetal.cmake b/cmake/linker/ld/target_baremetal.cmake index 76965ed81b2..8d01edc53a4 100644 --- a/cmake/linker/ld/target_baremetal.cmake +++ b/cmake/linker/ld/target_baremetal.cmake @@ -3,37 +3,4 @@ # 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() diff --git a/cmake/linker/linker_flags_template.cmake b/cmake/linker/linker_flags_template.cmake index 39db87e6c7f..870c597be24 100644 --- a/cmake/linker/linker_flags_template.cmake +++ b/cmake/linker/linker_flags_template.cmake @@ -11,6 +11,13 @@ set_property(TARGET linker PROPERTY base) # using C++. set_property(TARGET linker PROPERTY cpp_base) +# Base properties when building Zephyr for an embedded target (baremetal). +set_property(TARGET linker PROPERTY baremetal) + +# Property for controlling linker reporting / handling when placing orphaned sections. +set_property(TARGET linker PROPERTY orphan_warning) +set_property(TARGET linker PROPERTY orphan_error) + # coverage is a property holding the linker flag required for coverage support on the toolchain. # For example, on ld/gcc this would be: -lgcov # Set the property for the corresponding flags of the given toolchain. diff --git a/cmake/linker/lld/linker_flags.cmake b/cmake/linker/lld/linker_flags.cmake index 9183f60eadb..f6e873ad631 100644 --- a/cmake/linker/lld/linker_flags.cmake +++ b/cmake/linker/lld/linker_flags.cmake @@ -9,6 +9,11 @@ if(NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY AND NOT CONFIG_EXTERN set_property(TARGET linker PROPERTY cpp_base -lc++ ${LINKERFLAGPREFIX},-z,norelro) endif() +# Force LLVM to use built-in lld linker +if(NOT CONFIG_LLVM_USE_LD) + check_set_linker_property(TARGET linker APPEND PROPERTY baremetal -fuse-ld=lld) +endif() + set_property(TARGET linker PROPERTY no_position_independent "${LINKERFLAGPREFIX},--no-pie") set_property(TARGET linker PROPERTY lto_arguments) diff --git a/cmake/linker/lld/target_baremetal.cmake b/cmake/linker/lld/target_baremetal.cmake index 6faf8f50cac..8d01edc53a4 100644 --- a/cmake/linker/lld/target_baremetal.cmake +++ b/cmake/linker/lld/target_baremetal.cmake @@ -3,44 +3,4 @@ # See root CMakeLists.txt for description and expectations of these macros macro(toolchain_ld_baremetal) - - # LINKERFLAGPREFIX comes from linker/lld/target.cmake - zephyr_ld_options( - -nostdlib - -static - ${LINKERFLAGPREFIX},-X - ${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 - # 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() diff --git a/cmake/linker/xt-ld/linker_flags.cmake b/cmake/linker/xt-ld/linker_flags.cmake index b812ed5a1d8..7657da4a51a 100644 --- a/cmake/linker/xt-ld/linker_flags.cmake +++ b/cmake/linker/xt-ld/linker_flags.cmake @@ -11,6 +11,21 @@ if(NOT CONFIG_MINIMAL_LIBCPP AND NOT CONFIG_NATIVE_LIBRARY AND NOT CONFIG_EXTERN set_property(TARGET linker PROPERTY cpp_base -lstdc++) endif() +check_set_linker_property(TARGET linker PROPERTY baremetal + -nostdlib + -static + ${LINKERFLAGPREFIX},-X + ${LINKERFLAGPREFIX},-N +) + +check_set_linker_property(TARGET linker PROPERTY orphan_warning + ${LINKERFLAGPREFIX},--orphan-handling=warn +) + +check_set_linker_property(TARGET linker PROPERTY orphan_error + ${LINKERFLAGPREFIX},--orphan-handling=error +) + set_property(TARGET linker PROPERTY partial_linking "-r") check_set_linker_property(TARGET linker PROPERTY no_relax ${LINKERFLAGPREFIX},--no-relax)