Repository navigation
arch/arm/cmake: support CONFIG_PIC with PIC flags and reserved r9 - #20711
Conversation
|
780a05b to
68b1a86
Compare
|
Thank you, this fixes the two open parts of #19509. I built
The ELF Note for the test: on apps master, Three requests:
Could you also add to "Testing" what you checked (the flags per kind of object, and the warning count)? |
68b1a86 to
28b7eee
Compare
|
Thank you for the testing and review @casaroli! I have updated the PR to address all three points:
|
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 apache#19509 Signed-off-by: Swatantra Yadav <[email protected]>
28b7eee to
d4acc1f
Compare
|
@xiaoxiang781216 , I have reverted all changes to cmake/nuttx_add_application.cmake. Instead of modifying common application logic or checking DYNLIB_ELF_MODE, we now use the existing ELF_BINARY target property to distinguish base firmware from ELF applications when reserving r9. This keeps the patch strictly scoped inside arch/arm/ |
Summary
Resolves the remaining open items in issue #19509 ("[BUG] CMake build: CONFIG_PIC gives ELF applications contradictory register flags and no -fpic, and never reserves r10 in the base firmware"):
Following PR #19600 which transitioned the PIC base register to
r9, two issues remained in the CMake build whenCONFIG_PIC=y:Position-Independent Flags for ELF Applications:
In
arch/arm/src/cmake/elf.cmake, theelseif(CONFIG_PIC)branch previously passed only-mpic-register=r9, causing the compiler warning:cc1: warning: '-mpic-register=' is useless without '-fpic'This patch passes
${PICFLAGS} -mpic-register=r9tonuttx_elf_compile_options(withPICFLAGSprovided bygcc.cmake), matchingCELFFLAGSinarch/arm/src/common/Toolchain.defs.Reserve r9 in Base Firmware:
Under
CONFIG_PIC, base firmware routines could clobberr9. When firmware calls back into module code (e.g.qsort()with a module comparator), the module's data base was corrupted.This patch adds
-ffixed-r9to base firmware across GCC and Clang via:add_compile_options($<$<NOT:$<BOOL:$<TARGET_PROPERTY:ELF_APP>>>:-ffixed-r9>)in
arch/arm/src/cmake/gcc.cmakeandarch/arm/src/cmake/clang.cmake, reservingr9for firmware while excluding ELF applications.Dedicated Target Property
ELF_APP:In
cmake/nuttx_add_application.cmakeandarch/arm/src/common/CMakeLists.txt, setELF_APP TRUEon ELF applications (NOT DYNLIB_ELF_MODE) and onSTARTUP_OBJS(crt0.c). This ensures only ELF application targets omit-ffixed-r9, while kernel modules (nuttx_add_module) and DYNLIB shared libraries retain-ffixed-r9(matching Make). It also avoids misusingELF_BINARYonSTARTUP_OBJS.Preserve DYNLIB Options:
In
cmake/nuttx_add_application.cmake, guardedNUTTX_ELF_APP_COMPILE_OPTIONSwithNOT DYNLIB_ELF_MODEso shared libraries are built withNUTTX_MOD_APP_COMPILE_OPTIONSwithout receiving application PIC flags (-msingle-pic-base).Impact
CONFIG_PIC=yinto functional parity with the Makefile build (arch/arm/src/common/Toolchain.defs).'-mpic-register=' is useless without '-fpic'warning when compilingcrt0.cand ELF applications.-ffixed-regsyntax without affecting kernel modules or DYNLIB shared libraries.Testing
./tools/checkpatch.sh -u: 0 errors, 0 warnings.mps3-an547:picostest(CONFIG_PIC=y) witharm-gnu-toolchain 13.2.Rel1checked viacompile_commands.json:-ffixed-r9-ffixed-r9-mpic-register=r9only-mpic-register=r9, no-ffixed-r9crt0.c-mpic-register=r9only-mpic-register=r9, no-ffixed-r9hellorelocations now only containR_ARM_GOT_BREL, matching Make PIC module behavior.Fixes #19509