Skip to content

Commit 321e395

Browse files
edersondisouzanashif
authored andcommitted
linker: Include libkernel.a in the whole-archive when llext is enabled
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 <ederson.desouza@intel.com>
1 parent a924c87 commit 321e395

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,13 @@ foreach(zephyr_lib ${ZEPHYR_LIBS_PROPERTY})
876876
add_dependencies(${zephyr_lib} zephyr_generated_headers)
877877
endforeach()
878878

879+
if(CONFIG_LLEXT)
880+
set(WHOLE_ARCHIVE_LIBS ${ZEPHYR_LIBS_PROPERTY} kernel)
881+
else()
882+
set(WHOLE_ARCHIVE_LIBS ${ZEPHYR_LIBS_PROPERTY})
883+
set(NO_WHOLE_ARCHIVE_LIBS kernel)
884+
endif()
885+
879886
get_property(OUTPUT_FORMAT GLOBAL PROPERTY PROPERTY_OUTPUT_FORMAT)
880887

881888
if (CONFIG_CODE_DATA_RELOCATION)

cmake/linker/arcmwdt/target.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ function(toolchain_ld_link_elf)
9797
${LINKERFLAGPREFIX}--entry=__start
9898
${LINKERFLAGPREFIX}--Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
9999
${LINKERFLAGPREFIX}--whole-archive
100-
${ZEPHYR_LIBS_PROPERTY}
100+
${WHOLE_ARCHIVE_LIBS}
101101
${LINKERFLAGPREFIX}--no-whole-archive
102-
kernel
102+
${NO_WHOLE_ARCHIVE_LIBS}
103103
$<TARGET_OBJECTS:${OFFSETS_LIB}>
104104
${LIB_INCLUDE_DIR}
105105
-L${PROJECT_BINARY_DIR}

cmake/linker/ld/target.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ function(toolchain_ld_link_elf)
134134

135135
${LINKERFLAGPREFIX},-Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
136136
${LINKERFLAGPREFIX},--whole-archive
137-
${ZEPHYR_LIBS_PROPERTY}
137+
${WHOLE_ARCHIVE_LIBS}
138138
${LINKERFLAGPREFIX},--no-whole-archive
139-
kernel
139+
${NO_WHOLE_ARCHIVE_LIBS}
140140
$<TARGET_OBJECTS:${OFFSETS_LIB}>
141141
${LIB_INCLUDE_DIR}
142142
-L${PROJECT_BINARY_DIR}

cmake/linker/lld/target.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ function(toolchain_ld_link_elf)
9494

9595
${LINKERFLAGPREFIX},-Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
9696
${LINKERFLAGPREFIX},--whole-archive
97-
${ZEPHYR_LIBS_PROPERTY}
97+
${WHOLE_ARCHIVE_LIBS}
9898
${LINKERFLAGPREFIX},--no-whole-archive
99-
kernel
99+
${NO_WHOLE_ARCHIVE_LIBS}
100100
$<TARGET_OBJECTS:${OFFSETS_LIB}>
101101
${LIB_INCLUDE_DIR}
102102
-L${PROJECT_BINARY_DIR}

cmake/linker/xt-ld/target.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ function(toolchain_ld_link_elf)
126126

127127
${LINKERFLAGPREFIX},-Map=${TOOLCHAIN_LD_LINK_ELF_OUTPUT_MAP}
128128
${LINKERFLAGPREFIX},--whole-archive
129-
${ZEPHYR_LIBS_PROPERTY}
129+
${WHOLE_ARCHIVE_LIBS}
130130
${LINKERFLAGPREFIX},--no-whole-archive
131-
kernel
131+
${NO_WHOLE_ARCHIVE_LIBS}
132132
$<TARGET_OBJECTS:${OFFSETS_LIB}>
133133
${LIB_INCLUDE_DIR}
134134
-L${PROJECT_BINARY_DIR}

kernel/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-License-Identifier: Apache-2.0
22

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

66
zephyr_syscall_header(
77
${ZEPHYR_BASE}/include/zephyr/device.h

0 commit comments

Comments
 (0)