toolchain: add STRUCT_SECTION_ITERABLE_NAMED

Add a new API that allows to customize the name of an iterable section.
This allows to influence how section elements are sorted.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
This commit is contained in:
Gerard Marull-Paretas 2023-04-26 12:15:00 +02:00 committed by Carles Cufí
parent 7525a551cf
commit aae0fe8dab
5 changed files with 48 additions and 0 deletions

View file

@ -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).
*

View file

@ -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)

View file

@ -1,2 +1,3 @@
ITERABLE_SECTION_RAM(test_ram, 4)
ITERABLE_SECTION_RAM(test_ram2, 4)
ITERABLE_SECTION_RAM(test_ram_named, 4)

View file

@ -1,2 +1,3 @@
ITERABLE_SECTION_ROM(test_rom, 4)
ITERABLE_SECTION_ROM(test_rom2, 4)
ITERABLE_SECTION_ROM(test_rom_named, 4)

View file

@ -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);