Skip to content

include/nuttx: Add link-time iterable sections infrastructure - #19927

Open
JorgeGzm wants to merge 1 commit into
apache:masterfrom
JorgeGzm:iterable_sections
Open

include/nuttx: Add link-time iterable sections infrastructure#19927
JorgeGzm wants to merge 1 commit into
apache:masterfrom
JorgeGzm:iterable_sections

Conversation

@JorgeGzm

Copy link
Copy Markdown
Contributor

Summary

This PR adds link-time registration of struct instances ("iterable
sections"), modeled after the Zephyr STRUCT_SECTION_* mechanism: an
instance defined with STRUCT_SECTION_ITERABLE in any compilation unit
is collected by the linker into a contiguous, name-sorted array that can
be iterated like a plain C array. No runtime registration calls, no
central list to maintain, zero RAM overhead for the registry.

  • include/nuttx/iterable_sections.h: the C API
    (STRUCT_SECTION_ITERABLE/FOREACH/GET/COUNT).
  • include/nuttx/linker/iterable_sections.ld and
    common-rom.ld/common-ram.ld: central linker fragments. A board
    opts in by adding two #include lines to its linker script (they are
    CPP-preprocessed via ARCHSCRIPT, with upstream precedent in
    qemu-armv7a and nRF52/91 boards). Each subsystem block inside the
    fragments is guarded by its Kconfig option, so they expand to nothing
    on configurations that do not use them: a no-op for every existing
    board and config.
  • Documentation/components/iterable_sections.rst: full documentation,
    including how to add a new iterable type.

In the future, a common include of these fragments by the board linker
scripts can be considered: beyond the zbus port that uses this mechanism
today, it would open the same advantage to other routines that need
static registries (driver tables, init sequences, test registration),
with no per-board work.

Split out of #19916 (first user: the zbus message bus port,
apache/nuttx-apps#3743).

Impact

  • Existing boards/configs: none (new files only; nothing in-tree
    references them yet, and the fragments are Kconfig-guarded no-ops).
  • Users: new documented component for any subsystem that needs
    static registration.
  • Security / compatibility: no runtime code.

Testing

Host: Ubuntu 24.04.4 x86_64, arm-none-eabi-gcc 13.2.1 (GNU ld 2.41).
Target: linum-stm32h753bi (STM32H753BI).

  • Stock linum-stm32h753bi:nsh build: unchanged, no iterable sections
    in the map (no-op proof).
  • Full stack with the zbus companion PRs on hardware: the linker
    collects the three zbus sections name-sorted in flash and the 16-test
    zbus cmocka suite passes twice in the same boot (logs in the companion
    PRs).
  • tools/checkpatch.sh -c -u -m -g (nxstyle + codespell, same flags as
    CI): all checks pass. sphinx-build -W on this branch alone: clean.

Add generic support for link-time registration of struct instances,
modeled after the Zephyr STRUCT_SECTION_* mechanism:

- include/nuttx/iterable_sections.h: STRUCT_SECTION_ITERABLE/FOREACH/
  GET/COUNT macros placing instances in name-sorted linker sections
  delimited by _<type>_list_start/_end symbols.
- include/nuttx/linker/iterable_sections.ld: ITERABLE_SECTION() macro
  emitting the KEEP + SORT_BY_NAME collection statements (linker
  scripts in ARCHSCRIPT are CPP-preprocessed).
- include/nuttx/linker/common-rom.ld / common-ram.ld: central
  aggregators meant to be included by board linker scripts (inside
  .text and .data respectively).  Subsystem blocks are registered here
  guarded by their Kconfig options, so the fragments expand to nothing
  on configurations that do not use them; the first registered block is
  the zbus message bus (CONFIG_ZBUS, from nuttx-apps), mirroring how
  the Zephyr common-rom.ld carries the per-subsystem guarded blocks.
- Documentation/components/iterable_sections.rst.

First user: the Zephyr zbus message bus port (apps/system/zbus in
nuttx-apps); its board integration comes in a companion PR.

Signed-off-by: Jorge Guzman <jorge.gzm@gmail.com>
@JorgeGzm
JorgeGzm requested a review from raiden00pl as a code owner August 21, 2026 19:14
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium labels Aug 21, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Documentation Improvements or additions to documentation Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant