nuttx/libs/libc/stdlib
Marco Casaroli 43694933ce libc, sched: Resolve FDPIC descriptors at module callback entry points.
The base firmware and an FDPIC module disagree about what a function
pointer is.  Firmware is not built FDPIC, so to it a pointer is a code
address and it branches there.  A module passes the address of a two word
descriptor instead, because its code and data are placed independently and
a bare code address would leave the callee unable to find its own data.  A
firmware routine that takes a callback therefore branches into the
module's data segment and faults.

So the ten entry points that can be handed a callback by a module resolve
the descriptor before storing or branching to it: qsort, bsearch,
pthread_create, signal, sigaction, task_create and task_create_with_stack,
task_spawn, pthread_once, scandir, and mq_notify and timer_create with
SIGEV_THREAD.

Which one resolves matters as much as that one does.  Resolving twice would
take an already resolved code address for a descriptor and read two words
from the instruction stream, so each pointer is resolved exactly once, at
the outermost point that sees it.  signal() passes its argument through
untouched because sigaction() and then nxsig_action() will resolve it,
which covers a module calling sigaction() directly as well.  qsort() is
split so that the public entry resolves and the recursive implementation
does not.  scandir() resolves its filter but not its comparison function,
which it hands to qsort().

Whether a caller is a module at all is asked of the PIC base register,
which up_initial_state() sets only for a task that has a D-Space.  A plain
kernel task therefore reads zero and is left alone.

SIGEV_THREAD is the case the register cannot answer, because the callback
runs later on a work queue worker that carries no module's base at all.
The base is captured instead when the notification is registered, in the
module's own context, and installed around the call.

All of it is behind CONFIG_FDPIC, which defaults off.  Built for
mps3-an547:picostest both ways; with it off the entry points compile to
what they were.

Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
2026-09-25 10:46:48 -03:00
..
CMakeLists.txt libc: improve libc dependencies in case of LIBM_NONE 2025-12-14 17:57:27 -03:00
Kconfig stdlib/rand: replace weak LCG with xorshift32 PRNG 2026-07-03 15:08:12 +08:00
lib_abort.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_abs.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_aligned_alloc.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_arc4random.c libc: Fix lib_arc4random.c:111:(.text.arc4random_buf+0x26): undefined reference to `clock_systime_ticks' 2024-10-28 09:23:19 +01:00
lib_atexit.c libc/atexit: honor registrations made during exit processing 2026-09-14 18:52:46 -03:00
lib_atof.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_atoi.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_atol.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_atoll.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_bsearch.c libc, sched: Resolve FDPIC descriptors at module callback entry points. 2026-09-25 10:46:48 -03:00
lib_checkbase.c libs/libc: support explicit bases up to 36 2026-08-01 20:08:32 +08:00
lib_div.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_exit.c Revert "libs/libc/stdlib/lib_exit.c: fix multiple definition of __dso_handle in msys2" 2025-07-31 22:10:36 +08:00
lib_getprogname.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_itoa.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_labs.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_ldiv.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_llabs.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_lldiv.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_mblen.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_mbstowcs.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_mbtowc.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_mkdtemp.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_mkstemp.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_mktemp.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_openpty.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_posix_memalign.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_ptsname.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_ptsnamer.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_qsort.c libc, sched: Resolve FDPIC descriptors at module callback entry points. 2026-09-25 10:46:48 -03:00
lib_rand.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_rand48.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_reallocarray.c style: fix spelling in code comments and strings 2025-05-23 10:48:41 +08:00
lib_realpath.c fs: rename PSEUDOFS_SOFTLINKS to FS_LINKS 2026-08-27 01:12:33 +08:00
lib_srand.c stdlib/rand: replace weak LCG with xorshift32 PRNG 2026-07-03 15:08:12 +08:00
lib_strtol.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_strtold.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_strtoll.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_strtoul.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_strtoull.c !compiler: drop CONFIG_HAVE_LONG_LONG and require long long support 2026-05-19 16:21:28 +08:00
lib_unlockpt.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_valloc.c libc: remove reference to non-existent readme 2025-10-11 17:07:51 -04:00
lib_wcstombs.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
lib_wctomb.c libs/libc: migrate to SPDX identifier 2024-10-01 12:25:52 +08:00
Make.defs libc: improve libc dependencies in case of LIBM_NONE 2025-12-14 17:57:27 -03:00