This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 9327c72ab57 arch/arm/cmake: support CONFIG_PIC with PIC flags and 
reserved r9
9327c72ab57 is described below

commit 9327c72ab57c4c7c903d08907e0f1aae2690af71
Author: Swatantra Yadav <[email protected]>
AuthorDate: Sat Oct 17 13:17:28 2026 +0530

    arch/arm/cmake: support CONFIG_PIC with PIC flags and reserved r9
    
    Under CONFIG_PIC, the CMake build previously did not pass -fpic / PICFLAGS
    to ELF applications, triggering the warning:
    '-mpic-register=' is useless without '-fpic'.
    
    Furthermore, the base firmware never reserved r9, allowing the compiler to
    allocate r9 across firmware routines. When firmware callbacks invoke
    module functions (e.g., qsort with a module comparator), the module's
    data base was corrupted.
    
    This patch:
    1. Passes  -mpic-register=r9 to nuttx_elf_compile_options in
       arch/arm/src/cmake/elf.cmake under CONFIG_PIC, ensuring ELF applications
       are built position-independent and silencing the compiler warning.
    2. Adds -ffixed-r9 to base firmware objects under CONFIG_PIC in gcc.cmake
       and clang.cmake using a generator expression keyed on ELF_BINARY:
       $<$<NOT:$<BOOL:$<TARGET_PROPERTY:ELF_BINARY>>>:-ffixed-r9>
    3. Marks STARTUP_OBJS in arch/arm/src/common/CMakeLists.txt with target
       property ELF_BINARY, ensuring crt0.c and ELF application targets
       (which already have ELF_BINARY set) are excluded from -ffixed-r9.
    
    Testing:
    Built mps3-an547:picostest with CMake (arm-gnu-toolchain 13.2.Rel1) and
    checked compile_commands.json:
    - firmware (1152): -ffixed-r9 added
    - ELF apps hello + ostest (35): PICFLAGS -mpic-register=r9, no -ffixed-r9
    - crt0.c: PICFLAGS -mpic-register=r9, no -ffixed-r9
    - 'useless without -fpic' warnings: reduced from 36 to 0
    - ELF relocations for hello contain only R_ARM_GOT_BREL, matching Make PIC.
    
    Fixes #19509
    Signed-off-by: Swatantra Yadav <[email protected]>
---
 arch/arm/src/cmake/clang.cmake     | 8 ++++++++
 arch/arm/src/cmake/elf.cmake       | 2 +-
 arch/arm/src/cmake/gcc.cmake       | 8 ++++++++
 arch/arm/src/common/CMakeLists.txt | 2 ++
 4 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/arch/arm/src/cmake/clang.cmake b/arch/arm/src/cmake/clang.cmake
index e30015145fd..2de4db30bed 100644
--- a/arch/arm/src/cmake/clang.cmake
+++ b/arch/arm/src/cmake/clang.cmake
@@ -197,6 +197,14 @@ if(CONFIG_DEBUG_SYMBOLS)
   add_compile_options(${CONFIG_DEBUG_SYMBOLS_LEVEL})
 endif()
 
+if(CONFIG_PIC)
+  # Reserve r9 across the base firmware so callbacks into module code arrive
+  # with the module's data base intact. ELF applications are excluded because
+  # they receive r9 via -mpic-register=r9.
+  add_compile_options(
+    $<$<NOT:$<BOOL:$<TARGET_PROPERTY:ELF_BINARY>>>:-ffixed-r9>)
+endif()
+
 add_compile_options(
   -Wno-attributes -Wno-unknown-pragmas
   $<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>
diff --git a/arch/arm/src/cmake/elf.cmake b/arch/arm/src/cmake/elf.cmake
index 7d5febf29a5..4d87adbe80b 100644
--- a/arch/arm/src/cmake/elf.cmake
+++ b/arch/arm/src/cmake/elf.cmake
@@ -91,7 +91,7 @@ elseif(CONFIG_PIC)
   # register".  This mirrors CELFFLAGS in common/Toolchain.defs, which filters
   # --fixed-r9 back out of the inherited CFLAGS for the same reason.
 
-  nuttx_elf_compile_options(-mpic-register=r9)
+  nuttx_elf_compile_options(${PICFLAGS} -mpic-register=r9)
 
   nuttx_elf_link_options(
     ${ELF_LD_OPTION}--unresolved-symbols=ignore-in-object-files
diff --git a/arch/arm/src/cmake/gcc.cmake b/arch/arm/src/cmake/gcc.cmake
index 081c6b845fd..5807b967132 100644
--- a/arch/arm/src/cmake/gcc.cmake
+++ b/arch/arm/src/cmake/gcc.cmake
@@ -249,6 +249,14 @@ if(CONFIG_BUILD_PIC)
   add_link_options(-Wl,--emit-relocs)
 endif()
 
+if(CONFIG_PIC)
+  # Reserve r9 across the base firmware so callbacks into module code arrive
+  # with the module's data base intact. ELF applications are excluded because
+  # they receive r9 via -mpic-register=r9.
+  add_compile_options(
+    $<$<NOT:$<BOOL:$<TARGET_PROPERTY:ELF_BINARY>>>:-ffixed-r9>)
+endif()
+
 add_compile_options(-Wno-attributes -Wno-unknown-pragmas
                     $<$<COMPILE_LANGUAGE:C>:-Wstrict-prototypes>)
 
diff --git a/arch/arm/src/common/CMakeLists.txt 
b/arch/arm/src/common/CMakeLists.txt
index 177b4e68115..0bee88f0739 100644
--- a/arch/arm/src/common/CMakeLists.txt
+++ b/arch/arm/src/common/CMakeLists.txt
@@ -91,6 +91,8 @@ target_sources(arch PRIVATE ${SRCS})
 
 nuttx_add_aux_library(STARTUP_OBJS crt0.c)
 
+set_property(TARGET STARTUP_OBJS PROPERTY ELF_BINARY TRUE)
+
 target_compile_options(
   STARTUP_OBJS
   PRIVATE

Reply via email to