Skip to content

Commit

Permalink
linker: Include libkernel.a in the whole-archive when llext is enabled
Browse files Browse the repository at this point in the history
Differently from other libraries, which are included whole in the final
Zephyr ELF, libkernel.a itself isn't. Assuming this is intended to
enable optimisations (if it isn't, this patch will break things) - linker
can remove parts of the kernel that are not used by the application.

However, when considering Linkable Loadable Extensions (llext), this
optimisations can be counterproductive: for instance, syscalls that are
not used by the application won't be available for extensions. It won't
matter if someone "EXPORT_SYMBOL" for them, or even try to keep them
using LINKER_KEEP, they'll be gone.

To avoid that, this patches includes, when CONFIG_LLEXT=y, libkernel.a
inside the linker "whole-archive" block. This ends up making it consider
libkernel.a as a library whose all symbols should be kept. Note this
doesn't mean that all symbols will be there - things compiled out via
Kconfig will naturally still be out.

Signed-off-by: Ederson de Souza <[email protected]>
  • Loading branch information
edersondisouza authored and nashif committed Mar 26, 2024
1 parent a924c87 commit 321e395
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,13 @@ foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
add_dependencies(${zephyr_lib} zephyr_generated_headers)
endforeach()

if(CONFIG_LLEXT)
set(WHOLE_ARCHIVE_LIBS ${ZEPHYR_LIBS_PROPERTY} kernel)
else()
set(WHOLE_ARCHIVE_LIBS ${ZEPHYR_LIBS_PROPERTY})
set(NO_WHOLE_ARCHIVE_LIBS kernel)
endif()

get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)

if (CONFIG_CODE_DATA_RELOCATION)
Expand Down
4 changes: 2 additions & 2 deletions cmake/linker/arcmwdt/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ function(toolchain_ld_link_elf)
${LINKERFLAGPREFIX}--entry=__start
${LINKERFLAGPREFIX}--Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
${LINKERFLAGPREFIX}--whole-archive
${ZEPHYR_LIBS_PROPERTY}
${WHOLE_ARCHIVE_LIBS}
${LINKERFLAGPREFIX}--no-whole-archive
kernel
${NO_WHOLE_ARCHIVE_LIBS}
$<TARGET_OBJECTS:${OFFSETS_LIB}>
${LIB_INCLUDE_DIR}
-L${PROJECT_BINARY_DIR}
Expand Down
4 changes: 2 additions & 2 deletions cmake/linker/ld/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ function(toolchain_ld_link_elf)

${LINKERFLAGPREFIX},-Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
${LINKERFLAGPREFIX},--whole-archive
${ZEPHYR_LIBS_PROPERTY}
${WHOLE_ARCHIVE_LIBS}
${LINKERFLAGPREFIX},--no-whole-archive
kernel
${NO_WHOLE_ARCHIVE_LIBS}
$<TARGET_OBJECTS:${OFFSETS_LIB}>
${LIB_INCLUDE_DIR}
-L${PROJECT_BINARY_DIR}
Expand Down
4 changes: 2 additions & 2 deletions cmake/linker/lld/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ function(toolchain_ld_link_elf)

${LINKERFLAGPREFIX},-Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
${LINKERFLAGPREFIX},--whole-archive
${ZEPHYR_LIBS_PROPERTY}
${WHOLE_ARCHIVE_LIBS}
${LINKERFLAGPREFIX},--no-whole-archive
kernel
${NO_WHOLE_ARCHIVE_LIBS}
$<TARGET_OBJECTS:${OFFSETS_LIB}>
${LIB_INCLUDE_DIR}
-L${PROJECT_BINARY_DIR}
Expand Down
4 changes: 2 additions & 2 deletions cmake/linker/xt-ld/target.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ function(toolchain_ld_link_elf)

${LINKERFLAGPREFIX},-Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
${LINKERFLAGPREFIX},--whole-archive
${ZEPHYR_LIBS_PROPERTY}
${WHOLE_ARCHIVE_LIBS}
${LINKERFLAGPREFIX},--no-whole-archive
kernel
${NO_WHOLE_ARCHIVE_LIBS}
$<TARGET_OBJECTS:${OFFSETS_LIB}>
${LIB_INCLUDE_DIR}
-L${PROJECT_BINARY_DIR}
Expand Down
2 changes: 1 addition & 1 deletion kernel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: Apache-2.0

# kernel is a normal CMake library and not a zephyr_library because it
# should not be --whole-archive'd
# should usually not be --whole-archive'd

zephyr_syscall_header(
${ZEPHYR_BASE}/include/zephyr/device.h
Expand Down

0 comments on commit 321e395

Please sign in to comment.