Skip to content

arch/sim: rename nuttx libc memchr to avoid host glibc collision#18887

Merged
anchao merged 1 commit into
apache:masterfrom
cjj66619:patch-1
May 18, 2026
Merged

arch/sim: rename nuttx libc memchr to avoid host glibc collision#18887
anchao merged 1 commit into
apache:masterfrom
cjj66619:patch-1

Conversation

@cjj66619
Copy link
Copy Markdown
Contributor

The sim/src/nuttx-names.in symbol-rename list is the mechanism that keeps every nuttx libc function used inside nuttx.rel from clashing with the same-named function in host glibc when the sim executable is finally linked. The list already covers ~200 symbols (memcpy, strlen, strcat, strchr, ...) but memchr was missing.

On x86_64 Linux hosts the omission has no visible effect because host glibc dispatches memchr through an IFUNC resolver (__memchr_ifunc) that the static libc.a path does not eagerly pull in for typical sim links. On HOST_ARM64 (Ubuntu 20.04 aarch64, glibc 2.31 .. 2.41), however, the final cc/ld invocation in the sim Makefile drags libc.a(memchr.o) into the link, which then collides with nuttx libc's lib_memchr.o that has already been folded into nuttx.rel:

/usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libc.a(memchr.o):
    in function `__memchr_ifunc':
    (.text+0x0): multiple definition of `memchr';
    nuttx.rel:libs/libc/string/lib_memchr.c:55: first defined here

Add memchr to the rename list (placed in alphabetical position between malloc_usable_size and memcpy). After this fix sim:nsh builds cleanly on aarch64 Linux hosts (verified on NVIDIA Jetson Orin L4T Ubuntu 20.04 + Raspberry Pi 4B Debian 13 trixie). The behaviour on x86_64 / macOS / Cygwin hosts is unchanged because those targets either never hit the collision or use the underscore-prefixed variant gated by NXSYMBOLS macro definition in the same file (lines 26-31).

Summary

arch/sim/src/nuttx-names.in is the rename-list that prevents nuttx libc symbols (folded into nuttx.rel) from clashing with the same-named symbols pulled in from host glibc during the final sim link. The list already covers ~200 symbols (memcpy, strlen, strcat, strchr, ...) but memchr is missing, causing the sim build to fail with a multiple-definition error on Linux aarch64 hosts.

Impact

  • Affects sim:nsh, sim:nsh2, sim:smp, sim:bluetooth, sim:binder, sim:matter and any other sim defconfig that includes nuttx libc on an aarch64 Linux host.
  • Independent of CPU architecture in principle, but the actual collision only manifests on aarch64 because x86_64 glibc dispatches memchr through an IFUNC resolver placed in a different static-archive section.
  • Concrete environments reproduced: Ubuntu 20.04 L4T (Jetson Orin) and Debian 13 trixie (Raspberry Pi 4B).

Root Cause

  1. libs/libc/string/Make.defs:58 unconditionally compiles lib_memchr.c.
  2. arch/sim/src/Makefile:436-439 folds lib_memchr.o into nuttx.rel.
  3. arch/sim/src/Makefile:441 rewrites symbols in nuttx.rel via objcopy --redefine-syms=nuttx-names.dat — but nuttx-names.in has no NXSYMBOLS(memchr), so nuttx's memchr keeps its original name.
  4. arch/sim/src/Makefile:464-465 runs the final cc ... nuttx.rel ... -lc .... gcc on aarch64 Linux pulls in libc.a(memchr.o) (__memchr_ifunc), which collides with nuttx's own memchr:
/usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/.../libc.a(memchr.o):
  in function `__memchr_ifunc':
  (.text+0x0): multiple definition of `memchr';
  nuttx.rel:libs/libc/string/lib_memchr.c:55: first defined here

Fix

Single-line addition (between malloc_usable_size and memcpy, preserving alphabetical order):

+NXSYMBOLS(memchr)

After preprocessing this expands to either memchr NXmemchr (Linux) or _memchr _NXmemchr (macOS / Cygwin decorated), matching the existing convention.

Test results

Verified on:

Platform Status
NVIDIA Jetson Orin (Ubuntu 20.04 L4T aarch64, GCC 9.4.0, glibc 2.31) sim:nsh clean build + NSH prompt + ostest 38 stages PASS
Raspberry Pi 4B (Debian 13 trixie aarch64, glibc 2.41) Jetson binary cross-runs ✅ (same ABI)

ostest_main: Exiting with status 0 on both platforms. Detailed reproduction log + bytecount diff in test-report.md.

Companion patch

This patch alone is not sufficient to build sim:nsh on Ubuntu aarch64 hosts. A second small fix in boards/sim/sim/sim/scripts/Make.defs (gate the existing -no-pie block on !CONFIG_HOST_ARM64) is also required. See companion PR: boards/sim: skip -no-pie on HOST_ARM64 to fix sim:nsh link on aarch64 host.

The two PRs are independently mergeable, but on aarch64 you need both to get a working sim binary.

checkpatch

tools/checkpatch.sh -f arch/sim/src/nuttx-names.in → ✔️ All checks pass.

The sim/src/nuttx-names.in symbol-rename list is the mechanism that
keeps every nuttx libc function used inside nuttx.rel from clashing
with the same-named function in host glibc when the sim executable
is finally linked.  The list already covers ~200 symbols (memcpy,
strlen, strcat, strchr, ...) but memchr was missing.

On x86_64 Linux hosts the omission has no visible effect because
host glibc dispatches memchr through an IFUNC resolver
(__memchr_ifunc) that the static libc.a path does not eagerly pull
in for typical sim links.  On HOST_ARM64 (Ubuntu 20.04 aarch64,
glibc 2.31 .. 2.41), however, the final cc/ld invocation in the
sim Makefile drags libc.a(memchr.o) into the link, which then
collides with nuttx libc's lib_memchr.o that has already been
folded into nuttx.rel:

    /usr/bin/ld: /usr/lib/gcc/aarch64-linux-gnu/9/../../../aarch64-linux-gnu/libc.a(memchr.o):
        in function `__memchr_ifunc':
        (.text+0x0): multiple definition of `memchr';
        nuttx.rel:libs/libc/string/lib_memchr.c:55: first defined here

Add memchr to the rename list (placed in alphabetical position
between malloc_usable_size and memcpy).  After this fix sim:nsh
builds cleanly on aarch64 Linux hosts (verified on NVIDIA Jetson
Orin L4T Ubuntu 20.04 + Raspberry Pi 4B Debian 13 trixie).  The
behaviour on x86_64 / macOS / Cygwin hosts is unchanged because
those targets either never hit the collision or use the
underscore-prefixed variant gated by NXSYMBOLS macro definition
in the same file (lines 26-31).

Signed-off-by: Jinji Cui <113000688+cjj66619@users.noreply.github.com>
@github-actions github-actions Bot added Arch: simulator Issues related to the SIMulator Size: XS The size of the change in this PR is very small labels May 16, 2026
@anchao anchao merged commit 1ae4393 into apache:master May 18, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: simulator Issues related to the SIMulator Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants