Skip to content

arch/arm/cmake: support CONFIG_PIC with PIC flags and reserved r9 - #20711

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/cmake-arm-pic-flags
Oct 26, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/cmake-arm-pic-flags

Conversation

@Swatantra-66

@Swatantra-66 Swatantra-66 commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

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 when CONFIG_PIC=y:

  1. Position-Independent Flags for ELF Applications:
    In arch/arm/src/cmake/elf.cmake, the elseif(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=r9 to nuttx_elf_compile_options (with PICFLAGS provided by gcc.cmake), matching CELFFLAGS in arch/arm/src/common/Toolchain.defs.

  2. Reserve r9 in Base Firmware:
    Under CONFIG_PIC, base firmware routines could clobber r9. 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-r9 to base firmware across GCC and Clang via:
    add_compile_options($<$<NOT:$<BOOL:$<TARGET_PROPERTY:ELF_APP>>>:-ffixed-r9>)
    in arch/arm/src/cmake/gcc.cmake and arch/arm/src/cmake/clang.cmake, reserving r9 for firmware while excluding ELF applications.

  3. Dedicated Target Property ELF_APP:
    In cmake/nuttx_add_application.cmake and arch/arm/src/common/CMakeLists.txt, set ELF_APP TRUE on ELF applications (NOT DYNLIB_ELF_MODE) and on STARTUP_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 misusing ELF_BINARY on STARTUP_OBJS.

  4. Preserve DYNLIB Options:
    In cmake/nuttx_add_application.cmake, guarded NUTTX_ELF_APP_COMPILE_OPTIONS with NOT DYNLIB_ELF_MODE so shared libraries are built with NUTTX_MOD_APP_COMPILE_OPTIONS without receiving application PIC flags (-msingle-pic-base).

Impact

  • Build system: Brings CMake ARM build under CONFIG_PIC=y into functional parity with the Makefile build (arch/arm/src/common/Toolchain.defs).
  • Compiler output: Silences the '-mpic-register=' is useless without '-fpic' warning when compiling crt0.c and ELF applications.
  • Runtime correctness: Protects PIC module callbacks from data base register corruption when invoked by base firmware routines.
  • Compatibility: Strictly adheres to standard GCC and Clang -ffixed-reg syntax without affecting kernel modules or DYNLIB shared libraries.

Testing

  • Verified codebase style:
    ./tools/checkpatch.sh -u: 0 errors, 0 warnings.
  • Target configuration:
    mps3-an547:picostest (CONFIG_PIC=y) with arm-gnu-toolchain 13.2.Rel1 checked via compile_commands.json:
Objects master this PR
firmware (1152) no -ffixed-r9 -ffixed-r9
ELF applications hello + ostest (35) -mpic-register=r9 only PICFLAGS -mpic-register=r9, no -ffixed-r9
crt0.c -mpic-register=r9 only PICFLAGS -mpic-register=r9, no -ffixed-r9
"useless without -fpic" warnings 36 0
  • Verified ELF hello relocations now only contain R_ARM_GOT_BREL, matching Make PIC module behavior.

Fixes #19509

@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Size: S The size of the change in this PR is small labels Oct 17, 2026
@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@Swatantra-66
Swatantra-66 force-pushed the fix/cmake-arm-pic-flags branch from 780a05b to 68b1a86 Compare October 17, 2026 08:08
@Swatantra-66 Swatantra-66 changed the title arch/arm/cmake: support CONFIG_PIC with position-independent flags and reserved r9 arch/arm/cmake: support CONFIG_PIC with PIC flags and reserved r9 Oct 17, 2026
@casaroli

Copy link
Copy Markdown
Contributor

Thank you, this fixes the two open parts of #19509. I built mps3-an547:picostest with CMake on master e29a0b2 and on this PR (arm-gnu-toolchain 13.2.Rel1) and checked compile_commands.json:

Objects master this PR
firmware (1152) no -ffixed-r9 -ffixed-r9
ELF applications hello + ostest (35) -mpic-register=r9 only PICFLAGS -mpic-register=r9, no -ffixed-r9
crt0.c -mpic-register=r9 only PICFLAGS -mpic-register=r9, no -ffixed-r9
"useless without -fpic" warnings 36 0

The ELF hello now has only R_ARM_GOT_BREL relocations, as a Make PIC module has.

Note for the test: on apps master, examples/hello and testing/ostest have no MODULE in their CMakeLists, so with CMake they are built into the firmware, and no ELF application gets these flags at all. I used apache/nuttx-apps#3883 for hello and added MODULE ${CONFIG_TESTING_OSTEST} to ostest locally. Also, the CMake build of picostest does not link on master or with this PR (undefined reference to g_exports from nsh_init.c, CONFIG_NSH_SYMTAB); that is not caused by this PR.

Three requests:

  1. PICFLAGS does not need the cache entry or the fallback. elf.cmake already sees the normal variable from gcc.cmake: I removed CACHE INTERNAL and replaced the fallback with message(FATAL_ERROR), and the configure passed with the same flags. With Clang the fallback gives GCC-only options (-mno-pic-data-is-text-relative; Clang also has no -mpic-register). Please keep the one set(PICFLAGS ...) in gcc.cmake as it was.

  2. ELF_BINARY marks more targets than the ELF applications: nuttx_add_module() sets it on kernel modules, and nuttx_add_application() sets it on DYNLIB shared libraries too. So these lose -ffixed-r9. The Make build keeps --fixed-r9 for them (CMODULEFLAGS comes from CFLAGS, which has ARCHCFLAGS += --fixed-r9). Also, with CONFIG_MODULES a DYNLIB library gets NUTTX_ELF_APP_COMPILE_OPTIONS, so it now gets PICFLAGS with -msingle-pic-base and would read its data through the r9 of the program that calls it; Make compiles shared libraries with CMODULEFLAGS, without PICFLAGS. Could you use a separate target property for "compiled as a PIC ELF application" (set for the ELF application targets and for STARTUP_OBJS) and key the generator expression on it?

  3. That also avoids ELF_BINARY TRUE on STARTUP_OBJS: everywhere else ELF_BINARY holds the path of the ELF file (nuttx_add_symtab() passes it to nm), so a boolean there is easy to misread.

Could you also add to "Testing" what you checked (the flags per kind of object, and the warning count)?

@Swatantra-66
Swatantra-66 force-pushed the fix/cmake-arm-pic-flags branch from 68b1a86 to 28b7eee Compare October 20, 2026 10:55
@Swatantra-66
Swatantra-66 requested a review from anchao as a code owner October 20, 2026 10:55
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

Thank you for the testing and review @casaroli!

I have updated the PR to address all three points:

  1. PICFLAGS Scope: Reverted set(PICFLAGS ...) in gcc.cmake to a normal directory-scoped variable and removed the fallback definition in elf.cmake.
  2. Dedicated ELF_APP Property: Replaced ELF_BINARY with a dedicated property ELF_APP set only for ELF applications (NOT DYNLIB_ELF_MODE) in nuttx_add_application.cmake and on STARTUP_OBJS (crt0.c) in arch/arm/src/common/CMakeLists.txt. Base firmware, kernel modules (nuttx_add_module), and DYNLIB shared libraries now cleanly retain -ffixed-r9.
  3. DYNLIB Compile Options: Guarded NUTTX_ELF_APP_COMPILE_OPTIONS with NOT DYNLIB_ELF_MODE so shared libraries are built with NUTTX_MOD_APP_COMPILE_OPTIONS (matching Make CMODULEFLAGS) and do not receive -msingle-pic-base.
  4. Testing: Added your compile_commands.json test breakdown, flags verification, and 0-warning count to the commit message and PR description.

Comment thread cmake/nuttx_add_application.cmake Resolved
Comment thread cmake/nuttx_add_application.cmake Resolved
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]>
@Swatantra-66
Swatantra-66 force-pushed the fix/cmake-arm-pic-flags branch from 28b7eee to d4acc1f Compare October 22, 2026 04:25
@Swatantra-66

Copy link
Copy Markdown
Contributor Author

@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/

@xiaoxiang781216
xiaoxiang781216 merged commit 9327c72 into apache:master Oct 26, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] CMake build: CONFIG_PIC gives ELF applications contradictory register flags and no -fpic, and never reserves r10 in the base firmware

4 participants