Skip to content

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

Closed

Description

@casaroli

Description / Steps to reproduce the issue

Description / Steps to reproduce the issue

CONFIG_PIC does not appear to be wired up correctly in the CMake build. I
found three related problems while comparing it against the Makefile build, and
I am raising them together because fixing any one of them in isolation looks
wrong without a decision on the others. I have a one-line change for the first,
but it is not obviously the right thing to do on its own — hence an issue rather
than a PR.

1. ELF applications get two contradictory flags for the same register

arch/arm/src/cmake/elf.cmake:30:

nuttx_elf_compile_options_ifdef(CONFIG_PIC --fixed-r10 -mpic-register=r10)

These say opposite things about r10. -mpic-register=r10 tells the compiler
r10 holds the module's data base; --fixed-r10 tells it r10 is unavailable.
GCC rejects the combination outright once -fpic is in effect:

$ arm-none-eabi-gcc -mcpu=cortex-m55 -mthumb -fpic -msingle-pic-base \
      --fixed-r10 -mpic-register=r10 -c t.c -o t.o
cc1: error: unable to use 'r10' for PIC register

It is not currently fatal only because of problem 2 below — without -fpic the
same pair merely warns:

$ arm-none-eabi-gcc -mcpu=cortex-m55 -mthumb --fixed-r10 -mpic-register=r10 \
      -c t.c -o t.o
cc1: warning: '-mpic-register=' is useless without '-fpic'

The Makefile build keeps the two apart deliberately: it puts --fixed-r10 in
CFLAGS (base firmware) and filters it back out of CPICFLAGS/CELFFLAGS
(modules). The CMake build applies both to the module side.

To reproduce, on master:

$ cmake -B build -DBOARD_CONFIG=mps3-an547:picostest -GNinja \
        -DCMAKE_EXPORT_COMPILE_COMMANDS=1
$ grep -o -- "--fixed-r10 [^ ]*" build/compile_commands.json | head -1
--fixed-r10 -mpic-register=r10

$ ninja arch/arm/src/common/CMakeFiles/STARTUP_OBJS.dir/crt0.c.o
[1/1] Building C object arch/arm/src/common/CMakeFiles/STARTUP_OBJS.dir/crt0.c.o
cc1: warning: '-mpic-register=' is useless without '-fpic'

(mps3-an547:picostest is the only defconfig in the tree with CONFIG_PIC=y.)

2. ELF applications are not built position-independent at all

The warning above is the symptom: nothing gives these targets -fpic under
CONFIG_PIC. In the CMake build PICFLAGS is applied only under
CONFIG_BUILD_PIC (arch/arm/src/cmake/gcc.cmake:243-248), which is a
different feature — a position-independent kernel, using r9.

The Makefile build does give them -fpic, via

CPICFLAGS = $(ARCHPICFLAGS) $(CFLAGS)

where ARCHPICFLAGS is -fpic -msingle-pic-base -mpic-register=r10. So the
two build systems disagree about whether a CONFIG_PIC ELF application is
position-independent, and the CMake side looks incomplete rather than
deliberately different.

3. The base firmware never reserves r10

CONFIG_PIC appears in exactly two places in the whole CMake build, both in
elf.cmake, both on the module side:

$ grep -rn "CONFIG_PIC" --include="*.cmake" --include="CMakeLists.txt" . \
    | grep -v BUILD_PIC
arch/arm/src/cmake/elf.cmake:30:nuttx_elf_compile_options_ifdef(CONFIG_PIC --fixed-r10 -mpic-register=r10)
arch/arm/src/cmake/elf.cmake:33:  CONFIG_PIC --unresolved-symbols=ignore-in-object-files --emit-relocs)

Nothing adds --fixed-r10 to the base firmware, so the firmware is free to
allocate r10 — the register a PIC module reaches its own data through. A base
firmware routine that calls back into module code (qsort() with a module
comparison function is the standard case) then arrives with a bad data base.
That is the same defect the Makefile side has, which I have raised separately
in the PR "arch/arm: stop boards silently discarding --fixed-r10".

Why I have not simply sent a patch

Removing --fixed-r10 from elf.cmake:30 is a one-liner and I have it ready.
On its own, though:

  • it fixes no observable failure, because problem 2 keeps the error latent;
  • it does not silence the warning, which comes from the -mpic-register=r10
    that remains (verified by building crt0.c with the change applied);
  • it leaves problem 3 — the defect that actually corrupts a callback —
    untouched.

Fixing problem 3 centrally is also not a one-liner. The obvious approach,
add_compile_options(--fixed-r10) under CONFIG_PIC, does not work: directory
scoped options reach the ELF application targets too, recreating exactly the
rejected combination from problem 1. I confirmed that scoping behaviour with a
minimal CMake project — a directory-level option appears on a target that also
carries its own target_compile_options. Doing it properly needs either a
kernel-scoped option list (the tree has NUTTX_ELF_APP_COMPILE_OPTIONS and
NUTTX_MOD_APP_COMPILE_OPTIONS, but no firmware-only equivalent) or a generator
expression keyed on a per-target marker property.

So the real question is what CONFIG_PIC is intended to mean in the CMake
build, which someone who knows that design should answer before flags are moved
around. I am happy to write the patch once there is a direction.

On which OS does this issue occur?

[OS: Mac]

What is the version of your OS?

26.5.1 (Darwin 25.5.0, arm64)

NuttX Version

master (7df7c6ee)

Issue Architecture

[Arch: arm]

Issue Area

[Area: Build System]

Host information

macOS 26.5.1 (Darwin 25.5.0, arm64)
cmake 4.3.4, ninja 1.13.1
Arm GNU Toolchain 15.2.Rel1 (arm-none-eabi-gcc 15.2.1)

Verification

  • I have verified before submitting the report.

Activity

  1. added
    Type: Bug
    on Jul 23, 2026
  2. added
    Arch: arm
    OS: Mac
    on Jul 23, 2026
  3. alisha-1000 commented on Sep 14, 2026

    @alisha-1000

    Hi! I’d like to investigate this issue. I’ll first review the existing CMake PIC handling and compare it with the Makefile implementation to better understand the intended target scoping for CONFIG_PIC. If there is a preferred direction for separating the firmware and ELF application flags, I’d be happy to work on a PR.

  4. casaroli commented on Oct 8, 2026

    @casaroli
    ContributorAuthor

    Status, to give a starting point:

    1. Contradictory register flags: fixed. !arch/arm: Use r9 as the PIC base register, and bring mknxflat in-tree #19600 moved the PIC base register to r9, and the elseif(CONFIG_PIC) branch of arch/arm/src/cmake/elf.cmake now gives ELF applications only -mpic-register=r9 (no --fixed-r10). The title still says r10; since !arch/arm: Use r9 as the PIC base register, and bring mknxflat in-tree #19600 the register is r9.

    Two parts are still open, both in the CMake build only. The Make build (arch/arm/src/common/Toolchain.defs) already does both and is the reference.

    1. ELF applications get no -fpic. The elseif(CONFIG_PIC) branch in elf.cmake passes only -mpic-register=r9, so GCC warns "-mpic-register= is useless without -fpic". Make adds $(PICFLAGS) -mpic-register=r9 to CELFFLAGS/CXXELFFLAGS. PICFLAGS (-fpic -fPIE -mno-pic-data-is-text-relative -msingle-pic-base) is already defined in arch/arm/src/cmake/gcc.cmake, so the fix there is probably nuttx_elf_compile_options(${PICFLAGS} -mpic-register=r9).

    2. The base firmware does not reserve r9. Make adds --fixed-r9 to ARCHCFLAGS under CONFIG_PIC and filters it back out of the ELF and module flags. CMake has nothing like this for a flat build: NUTTX_KERNEL_COMPILE_OPTIONS is applied only to split kernel libraries (cmake/nuttx_add_library.cmake). The flag must reach the firmware and libc but not the ELF applications, otherwise GCC rejects the pair again ("unable to use 'r9' for PIC register"). One possible way is a generator expression keyed on a property that only ELF targets have. ELF_BINARY in cmake/nuttx_add_application.cmake may work, but check that every ELF path sets it. This part matters when firmware code calls back into module code, for example qsort() with a comparator in the module.

    To test: cmake -B build -DBOARD_CONFIG=mps3-an547:picostest -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=1. picostest is the only defconfig with CONFIG_PIC=y. In compile_commands.json, firmware objects should have --fixed-r9, and ELF application objects should have the PICFLAGS, -mpic-register=r9 and no --fixed-r9. The build should not print the "useless without -fpic" warning.

  5. added a commit that references this issue on Oct 17, 2026
    68b1a86
  6. Swatantra-66 commented on Oct 17, 2026

    @Swatantra-66
    Contributor

    Hi @casaroli,

    I have submitted a fix for this in #20711:

    1. Passes ${PICFLAGS} -mpic-register=r9 to nuttx_elf_compile_options in arch/arm/src/cmake/elf.cmake under CONFIG_PIC, ensuring ELF applications receive position-independent flags and silencing the compiler warning.
    2. Caches PICFLAGS in gcc.cmake and reserves r9 (-ffixed-r9) in the base firmware under CONFIG_PIC across GCC and Clang via a generator expression targeting non-ELF objects.
    3. Sets ELF_BINARY TRUE on STARTUP_OBJS (crt0.c) in arch/arm/src/common/CMakeLists.txt so startup code retains access to r9.
  7. added 2 commits that reference this issue on Oct 20, 2026
    28b7eee
    d4acc1f
  8. added a commit that references this issue on Oct 26, 2026
    9327c72
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Arch: armArea: Build systemOS: MacType: Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions