Brijesh-Thakkar opened a new pull request, #20761:
URL: https://github.com/apache/nuttx/pull/20761

   *Note: Please adhere to [Contributing 
Guidelines](https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md).*
   
   ## Summary
   
   Fixes #18393.
   
   On Ubuntu 20.04 (gcc 10.5, binutils 2.34), the sim link fails with:
   
       /usr/bin/ld: cannot find -lgcc_s
       /usr/bin/ld: cannot find -lgcc_s
       collect2: error: ld returned 1 exit status
   
   Root cause: the sim build passed `-Wl,-no-pie`. That flag only reaches `ld`, 
so the gcc driver still sets up a PIE link (`Scrt1.o`, `crtbeginS.o`). On this 
toolchain, `ld` then searches only for `libgcc_s.a`, which Ubuntu does not 
ship. NuttX never names `-lgcc_s` itself; the gcc driver adds it. The same 
failure reproduces outside NuttX:
   
       gcc hello.c                # ok
       gcc -no-pie hello.c        # ok
       gcc -Wl,-no-pie hello.c    # cannot find -lgcc_s (x2)
   
   The make build was already fixed by fd907e6ef4 ("sim: pass no-pie through 
gcc driver"). The CMake build still used `-Wl,-no-pie`. On focal it fails even 
earlier, in CMake's compiler check at configure time.
   
   This PR has two commits:
   
   1. **arch/sim: resolve host symbols eagerly on Linux in CMake.**
      Adds `-Wl,-z,now`, which make has had since ad182bcb4b but CMake never 
got. Without it, once the binary is truly non-PIE, early host calls made with 
simulated interrupts masked fault inside the lazy binder:
   
          _dl_fixup <- _dl_runtime_resolve_xsavec <- up_irq_save <- 
host_allocheap <- nx_start
   
      The same binary boots with `LD_BIND_NOW=1`. This commit comes first so 
the series stays bisectable.
   
   2. **arch/sim: pass no-pie through the gcc driver in CMake.**
      `add_link_options(-Wl,-no-pie)` becomes `add_link_options(-no-pie)`, 
mirroring fd907e6ef4 for make.
   
   ## Impact
   
   - **Affected:** CMake sim builds on Linux only. The make build is unchanged.
   - **`-no-pie` change:** reaches only x86_64 Linux non-m32 builds, through 
the existing `elseif(NOT APPLE AND NOT SIM_HOST_ARM64)` guard. It does not 
affect macOS, arm64 or m32.
   - **`-z now`:** applies to all Linux hosts (`SIM_HOST_LINUX`), including 
aarch64. This matches make's `ifeq ($(CONFIG_HOST_LINUX),y)` exactly.
   - **No effect on:** hardware targets, documentation or the public API.
   
   ## Testing
   
   Hosts:
   - Ubuntu 20.04 container: gcc 10.5.0-1ubuntu1~20.04, binutils 2.34, cmake 
from pip. NuttX needs cmake 3.20 or later, and focal ships 3.16.3.
   - Native: Ubuntu 26.04, gcc 15.2, ld 2.46.
   
   | Environment | Build | Boot | hello | ostest | poweroff |
   |---|---|---|---|---|---|
   | focal gcc 10.5, CMake sim:nsh | pass | pass | pass | exit status 0 | pass |
   | focal gcc 10.5, make sim:nsh | pass | pass | pass | exit status 0 | pass |
   | focal gcc 10.5, CMake sim:nsh with CONFIG_SIM_M32=y | pass | pass | pass | 
exit status 0 | pass |
   | focal gcc 10.5, CMake sim:romfs | pass | pass | pass | n/a (not in 
defconfig) | pass |
   | native gcc 15, CMake sim:nsh | pass | pass | pass | exit status 0 | pass |
   | native gcc 15, make sim:nsh | pass | pass | pass | exit status 0 | pass |
   
   Before (master, focal, CMake configure fails in the compiler check):
   
       cc -Wl,-no-pie -Wl,--gc-sections -Wl,-Ttext-segment=0x40000000 
-Wl,-z,noexecstack ... -o cmTC_...
       /usr/bin/ld: cannot find -lgcc_s
       /usr/bin/ld: cannot find -lgcc_s
   
   After (focal, final link succeeds):
   
       cc -Wl,--gc-sections -no-pie -Wl,-Ttext-segment=0x40000000 
-Wl,-z,noexecstack -Wl,-z,now -T nuttx.ld @CMakeFiles/nuttx.rsp -o nuttx
   
   `./tools/checkpatch.sh -c -m -g upstream/master..HEAD` passes on both 
commits.
   
   Not tested: macOS, arm64 and the reporters' WSL2 setup. The existing guards 
keep the `-no-pie` change away from macOS and arm64.
   
   Full nsh/ostest session logs and the gdb backtrace are available on request.
   
   ## Known issues / out of scope
   
   clang 10 CMake on focal still fails with `-lgcc_s`. On master it fails at 
configure. With this PR, configure passes, but the `clang -r ... nuttx.rel` 
partial link then fails with `cannot find -lgcc_s`. That link does not use the 
flags changed here, so I'll handle it separately.
   
   Investigation, reproduction and verification were assisted by Claude Code. I 
reviewed and tested the final changes.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to