From e2d8daae1616f1dd84adb625098a9796b448f471 Mon Sep 17 00:00:00 2001 From: Guennadi Liakhovetski Date: Wed, 30 Oct 2024 14:49:16 +0200 Subject: [PATCH] LLEXT: Xtensa: add a Kconfig option to enable -fPIC Currently when building LLEXT for Xtensa we use the -fPIC compiler option, but this cannot be used when using detached sections in extensions. Add a Kconfig option to switch between the two compilation modes and switch -fPIC off when building relocatable (partially linked) ELF binaries. Signed-off-by: Guennadi Liakhovetski --- cmake/compiler/gcc/target_xtensa.cmake | 17 ++++++++++++++--- cmake/compiler/xt-clang/target.cmake | 17 ++++++++++++++--- subsys/llext/Kconfig | 10 ++++++++++ 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/cmake/compiler/gcc/target_xtensa.cmake b/cmake/compiler/gcc/target_xtensa.cmake index 5321204c2df..0b398023f67 100644 --- a/cmake/compiler/gcc/target_xtensa.cmake +++ b/cmake/compiler/gcc/target_xtensa.cmake @@ -3,8 +3,6 @@ # Flags not supported by llext linker # (regexps are supported and match whole word) set(LLEXT_REMOVE_FLAGS - -fno-pic - -fno-pie -ffunction-sections -fdata-sections -g.* @@ -14,7 +12,20 @@ set(LLEXT_REMOVE_FLAGS # Flags to be added to llext code compilation set(LLEXT_APPEND_FLAGS - -fPIC -nostdlib -nodefaultlibs ) + +if(CONFIG_LLEXT_BUILD_PIC) +set(LLEXT_REMOVE_FLAGS ${LLEXT_REMOVE_FLAGS} + -fno-pic + -fno-pie +) +set(LLEXT_APPEND_FLAGS ${LLEXT_APPEND_FLAGS} + -fPIC +) +else() +set(LLEXT_APPEND_FLAGS ${LLEXT_APPEND_FLAGS} + -ffreestanding +) +endif() diff --git a/cmake/compiler/xt-clang/target.cmake b/cmake/compiler/xt-clang/target.cmake index 41a72aa782e..32def2e5b8f 100644 --- a/cmake/compiler/xt-clang/target.cmake +++ b/cmake/compiler/xt-clang/target.cmake @@ -5,8 +5,6 @@ include(${ZEPHYR_BASE}/cmake/compiler/xcc/target.cmake) # Flags not supported by llext linker # (regexps are supported and match whole word) set(LLEXT_REMOVE_FLAGS - -fno-pic - -fno-pie -ffunction-sections -fdata-sections -g.* @@ -16,7 +14,20 @@ set(LLEXT_REMOVE_FLAGS # Flags to be added to llext code compilation set(LLEXT_APPEND_FLAGS - -fPIC -nostdlib -nodefaultlibs ) + +if(CONFIG_LLEXT_BUILD_PIC) +set(LLEXT_REMOVE_FLAGS ${LLEXT_REMOVE_FLAGS} + -fno-pic + -fno-pie +) +set(LLEXT_APPEND_FLAGS ${LLEXT_APPEND_FLAGS} + -fPIC +) +else() +set(LLEXT_APPEND_FLAGS ${LLEXT_APPEND_FLAGS} + -ffreestanding +) +endif() diff --git a/subsys/llext/Kconfig b/subsys/llext/Kconfig index 696620b16d8..55800dcd0cf 100644 --- a/subsys/llext/Kconfig +++ b/subsys/llext/Kconfig @@ -49,6 +49,16 @@ config LLEXT_HEAP_SIZE help Heap size in kilobytes available to llext for dynamic allocation +config LLEXT_BUILD_PIC + bool "Use -fPIC when building LLEXT" + depends on XTENSA + default y if LLEXT_TYPE_ELF_SHAREDLIB + help + By default LLEXT compilation is performed with -fno-pic -fno-pie compiler + flags. Some platforms can benefit from using -fPIC instead, in which case + most internal linking is performed by the linker at build time. Select "y" + to make use of that advantage. + config LLEXT_SHELL bool "llext shell commands" depends on SHELL