diff --git a/include/zephyr/toolchain/common.h b/include/zephyr/toolchain/common.h index f050aa74784..a90ccaf73d7 100644 --- a/include/zephyr/toolchain/common.h +++ b/include/zephyr/toolchain/common.h @@ -385,6 +385,15 @@ #define STRUCT_SECTION_ITERABLE(struct_type, varname) \ STRUCT_SECTION_ITERABLE_ALTERNATE(struct_type, struct_type, varname) +/** + * @brief Defines a new element for an iterable section with a custom name. + * + * The name can be used to customize how iterable section entries are sorted. + * @see STRUCT_SECTION_ITERABLE() + */ +#define STRUCT_SECTION_ITERABLE_NAMED(struct_type, name, varname) \ + TYPE_SECTION_ITERABLE(struct struct_type, varname, struct_type, name) + /** * @brief Iterate over a specified iterable section (alternate). * diff --git a/tests/misc/iterable_sections/CMakeLists.txt b/tests/misc/iterable_sections/CMakeLists.txt index aae6f0deb21..ab2ae06b44e 100644 --- a/tests/misc/iterable_sections/CMakeLists.txt +++ b/tests/misc/iterable_sections/CMakeLists.txt @@ -10,7 +10,9 @@ target_sources(app PRIVATE ${app_sources}) zephyr_linker_sources(DATA_SECTIONS sections-ram.ld) zephyr_iterable_section(NAME test_ram GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4) zephyr_iterable_section(NAME test_ram2 GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4) +zephyr_iterable_section(NAME test_ram_named GROUP DATA_REGION ${XIP_ALIGN_WITH_INPUT} SUBALIGN 4) zephyr_linker_sources(SECTIONS sections-rom.ld) zephyr_iterable_section(NAME test_rom KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4) zephyr_iterable_section(NAME test_rom2 KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4) +zephyr_iterable_section(NAME test_rom_named KVMA RAM_REGION GROUP RODATA_REGION SUBALIGN 4) diff --git a/tests/misc/iterable_sections/sections-ram.ld b/tests/misc/iterable_sections/sections-ram.ld index 4badcbb9e6e..28a02b4bc4d 100644 --- a/tests/misc/iterable_sections/sections-ram.ld +++ b/tests/misc/iterable_sections/sections-ram.ld @@ -1,2 +1,3 @@ ITERABLE_SECTION_RAM(test_ram, 4) ITERABLE_SECTION_RAM(test_ram2, 4) +ITERABLE_SECTION_RAM(test_ram_named, 4) diff --git a/tests/misc/iterable_sections/sections-rom.ld b/tests/misc/iterable_sections/sections-rom.ld index 434bf1606bf..5c8f7203a1a 100644 --- a/tests/misc/iterable_sections/sections-rom.ld +++ b/tests/misc/iterable_sections/sections-rom.ld @@ -1,2 +1,3 @@ ITERABLE_SECTION_ROM(test_rom, 4) ITERABLE_SECTION_ROM(test_rom2, 4) +ITERABLE_SECTION_ROM(test_rom_named, 4) diff --git a/tests/misc/iterable_sections/src/main.c b/tests/misc/iterable_sections/src/main.c index caba125e0b2..f004ee99c67 100644 --- a/tests/misc/iterable_sections/src/main.c +++ b/tests/misc/iterable_sections/src/main.c @@ -10,6 +10,10 @@ struct test_ram { int i; }; +struct test_ram_named { + int i; +}; + #define CHECK_BIT 0x80 /* declare in random order to check that the linker is sorting by name */ @@ -23,6 +27,12 @@ STRUCT_SECTION_ITERABLE(test_ram, ram1) = {0x01}; /* iterable section items can also be static */ static const STRUCT_SECTION_ITERABLE_ALTERNATE(test_ram2, test_ram, ram5) = {RAM_EXPECT}; +/* declare in random order to check that the linker is sorting by custom name */ +const STRUCT_SECTION_ITERABLE_NAMED(test_ram_named, A, ram6) = {0x01}; +const STRUCT_SECTION_ITERABLE_NAMED(test_ram_named, C, ram7) = {0x03}; +const STRUCT_SECTION_ITERABLE_NAMED(test_ram_named, D, ram8) = {0x04}; +const STRUCT_SECTION_ITERABLE_NAMED(test_ram_named, B, ram9) = {0x02}; + /** * * @brief Test iterable in read write section. @@ -54,12 +64,23 @@ ZTEST(iterable_sections, test_ram) } zassert_equal(out, RAM_EXPECT, "Check value incorrect (got: 0x%08x)", out); + + out = 0; + STRUCT_SECTION_FOREACH(test_ram_named, t) { + out = (out << 8) | t->i; + } + + zassert_equal(out, RAM_EXPECT, "Check value incorrect (got: 0x%x)", out); } struct test_rom { int i; }; +struct test_rom_named { + int i; +}; + /* declare in random order to check that the linker is sorting by name */ const STRUCT_SECTION_ITERABLE(test_rom, rom1) = {0x10}; const STRUCT_SECTION_ITERABLE(test_rom, rom3) = {0x30}; @@ -71,6 +92,12 @@ const STRUCT_SECTION_ITERABLE(test_rom, rom2) = {0x20}; /* iterable section items can also be static */ static const STRUCT_SECTION_ITERABLE_ALTERNATE(test_rom2, test_rom, rom5) = {ROM_EXPECT}; +/* declare in random order to check that the linker is sorting by custom name */ +const STRUCT_SECTION_ITERABLE_NAMED(test_rom_named, A, rom6) = {0x10}; +const STRUCT_SECTION_ITERABLE_NAMED(test_rom_named, C, rom7) = {0x30}; +const STRUCT_SECTION_ITERABLE_NAMED(test_rom_named, D, rom8) = {0x40}; +const STRUCT_SECTION_ITERABLE_NAMED(test_rom_named, B, rom9) = {0x20}; + /** * * @brief Test iterable in read only section. @@ -92,6 +119,14 @@ ZTEST(iterable_sections, test_rom) } zassert_equal(out, ROM_EXPECT, "Check value incorrect (got: 0x%x)", out); + + out = 0; + STRUCT_SECTION_FOREACH(test_rom_named, t) { + out = (out << 8) | t->i; + } + + zassert_equal(out, ROM_EXPECT, "Check value incorrect (got: 0x%x)", out); + } ZTEST_SUITE(iterable_sections, NULL, NULL, NULL, NULL, NULL);