nuttx/include/nuttx/linker/iterable_sections.ld
Jorge Guzman 7cd511fed2 include/nuttx: Add link-time iterable sections infrastructure
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/DECLARE/
  FOREACH/GET/COUNT macros placing instances in name-sorted linker
  sections delimited by _<type>_list_start/_end symbols (attributes
  through the nuttx/compiler.h macros; FOREACH takes a caller-declared
  iterator, like list_for_every_entry).
- 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); subsystems register their sections
  here guarded by their Kconfig options, so the fragments expand to
  nothing on configurations that do not use them.
- CONFIG_ITERABLE_SECTIONS_LINKER_INSERT + include/nuttx/linker/
  common-insert.ld (added before the board script by tools/Config.mk and
  by the top-level CMakeLists.txt): optional zero-touch mode that
  supplements the board script through GNU ld INSERT AFTER, collecting
  the subsystems' ITERABLE_SECTION blocks in one output section; the
  common-rom.ld/common-ram.ld fragments expand to nothing in that mode.
  See the option help for the constraints.
- 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>
2026-08-27 01:04:05 +08:00

43 lines
1.8 KiB
Text

/****************************************************************************
* include/nuttx/linker/iterable_sections.ld
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/* CPP macro emitting the linker statements that collect one iterable
* section (see include/nuttx/iterable_sections.h). This file is meant to
* be included from linker scripts that are preprocessed with CPP (the
* ARCHSCRIPT .tmp rule).
*
* The macro must be expanded INSIDE an output section (e.g. .text or
* .data). KEEP() protects the entries from --gc-sections and
* SORT_BY_NAME() defines the iteration order.
*/
#ifndef __INCLUDE_NUTTX_LINKER_ITERABLE_SECTIONS_LD
#define __INCLUDE_NUTTX_LINKER_ITERABLE_SECTIONS_LD
#define ITERABLE_SECTION(name) \
. = ALIGN(4); \
_##name##_list_start = .; \
KEEP(*(SORT_BY_NAME(._##name.static.*))); \
_##name##_list_end = .; \
. = ALIGN(4);
#endif /* __INCLUDE_NUTTX_LINKER_ITERABLE_SECTIONS_LD */