diff --git a/cmake/modules/extensions.cmake b/cmake/modules/extensions.cmake index ecf47ec4196..b21614d75fb 100644 --- a/cmake/modules/extensions.cmake +++ b/cmake/modules/extensions.cmake @@ -5318,6 +5318,26 @@ function(add_llext_target target_name) add_library(${llext_lib_target} OBJECT ${source_files}) set(llext_lib_output $) + elseif(CONFIG_LLEXT_TYPE_ELF_RELOCATABLE) + + # CMake does not directly support a "RELOCATABLE" library target. + # The "SHARED" target would be similar, but that unavoidably adds + # a "-shared" flag to the linker command line which does firmly + # conflict with "-r". + # A workaround is to use an executable target and make the linker + # output a relocatable file. The output file suffix is changed so + # the result looks like the object file it actually is. + add_executable(${llext_lib_target} EXCLUDE_FROM_ALL ${source_files}) + target_link_options(${llext_lib_target} PRIVATE -r) + set_target_properties(${llext_lib_target} PROPERTIES + SUFFIX ${CMAKE_C_OUTPUT_EXTENSION}) + set(llext_lib_output $) + + # Add the llext flags to the linking step as well + target_link_options(${llext_lib_target} PRIVATE + ${LLEXT_APPEND_FLAGS} + ) + elseif(CONFIG_LLEXT_TYPE_ELF_SHAREDLIB) # Create a shared library @@ -5380,6 +5400,21 @@ function(add_llext_target target_name) DEPENDS ${llext_proc_target} ${llext_pkg_input} ) + elseif(CONFIG_LLEXT_TYPE_ELF_RELOCATABLE) + + # Need to remove just some sections from the relocatable object + # (using strip in this case would remove _all_ symbols) + add_custom_command( + OUTPUT ${llext_pkg_output} + COMMAND $ + $ + $.xt.* + $${llext_pkg_input} + $${llext_pkg_output} + $ + DEPENDS ${llext_proc_target} ${llext_pkg_input} + ) + elseif(CONFIG_LLEXT_TYPE_ELF_SHAREDLIB) # Need to strip the shared library of some sections diff --git a/subsys/llext/Kconfig b/subsys/llext/Kconfig index 4bc5e5b6136..c96017c79c9 100644 --- a/subsys/llext/Kconfig +++ b/subsys/llext/Kconfig @@ -23,6 +23,14 @@ config LLEXT_TYPE_ELF_OBJECT llext subsystem. A single compiler invocation is used to generate the object file. +config LLEXT_TYPE_ELF_RELOCATABLE + bool "Relocatable ELF file" + help + Build and expect relocatable (partially linked )files as the + binary object type for the llext subsystem. These object files + are generated by the linker by combining multiple object files + into a single one. + config LLEXT_TYPE_ELF_SHAREDLIB bool "Shared library ELF file" help