1 /*
2  * Copyright (C) 2020, Intel Corporation
3  * Copyright (C) 2023, Nordic Semiconductor ASA
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef INCLUDE_ZEPHYR_LINKER_ITERABLE_SECTIONS_H_
8 #define INCLUDE_ZEPHYR_LINKER_ITERABLE_SECTIONS_H_
9 
10 /**
11  * @addtogroup iterable_section_apis
12  * @{
13  */
14 
15 #define Z_LINK_ITERABLE(struct_type) \
16 	_CONCAT(_##struct_type, _list_start) = .; \
17 	KEEP(*(SORT_BY_NAME(._##struct_type.static.*))); \
18 	_CONCAT(_##struct_type, _list_end) = .
19 
20 #define Z_LINK_ITERABLE_NUMERIC(struct_type) \
21 	_CONCAT(_##struct_type, _list_start) = .; \
22 	KEEP(*(SORT(._##struct_type.static.*_?_*))); \
23 	KEEP(*(SORT(._##struct_type.static.*_??_*))); \
24 	_CONCAT(_##struct_type, _list_end) = .
25 
26 #define Z_LINK_ITERABLE_ALIGNED(struct_type, align) \
27 	. = ALIGN(align); \
28 	Z_LINK_ITERABLE(struct_type);
29 
30 #define Z_LINK_ITERABLE_GC_ALLOWED(struct_type) \
31 	_CONCAT(_##struct_type, _list_start) = .; \
32 	*(SORT_BY_NAME(._##struct_type.static.*)); \
33 	_CONCAT(_##struct_type, _list_end) = .
34 
35 #define Z_LINK_ITERABLE_SUBALIGN CONFIG_LINKER_ITERABLE_SUBALIGN
36 
37 /**
38  * @brief Define a read-only iterable section output.
39  *
40  * @details
41  * Define an output section which will set up an iterable area
42  * of equally-sized data structures. For use with STRUCT_SECTION_ITERABLE().
43  * Input sections will be sorted by name, per ld's SORT_BY_NAME.
44  *
45  * This macro should be used for read-only data.
46  *
47  * Note that this keeps the symbols in the image even though
48  * they are not being directly referenced. Use this when symbols
49  * are indirectly referenced by iterating through the section.
50  */
51 #define ITERABLE_SECTION_ROM(struct_type, subalign) \
52 	SECTION_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
53 	{ \
54 		Z_LINK_ITERABLE(struct_type); \
55 	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
56 
57 /**
58  * @brief Define a read-only iterable section output, sorted numerically.
59  *
60  * This version of ITERABLE_SECTION_ROM() sorts the entries numerically, that
61  * is, `SECNAME_10` will come after `SECNAME_2`. `_` separator is required, and
62  * up to 2 numeric digits are handled (0-99).
63  *
64  * @see ITERABLE_SECTION_ROM()
65  */
66 #define ITERABLE_SECTION_ROM_NUMERIC(struct_type, subalign) \
67 	SECTION_PROLOGUE(struct_type##_area, EMPTY, SUBALIGN(subalign)) \
68 	{ \
69 		Z_LINK_ITERABLE_NUMERIC(struct_type); \
70 	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
71 
72 /**
73  * @brief Define a garbage collectable read-only iterable section output.
74  *
75  * @details
76  * Define an output section which will set up an iterable area
77  * of equally-sized data structures. For use with STRUCT_SECTION_ITERABLE().
78  * Input sections will be sorted by name, per ld's SORT_BY_NAME.
79  *
80  * This macro should be used for read-only data.
81  *
82  * Note that the symbols within the section can be garbage collected.
83  */
84 #define ITERABLE_SECTION_ROM_GC_ALLOWED(struct_type, subalign) \
85 	SECTION_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
86 	{ \
87 		Z_LINK_ITERABLE_GC_ALLOWED(struct_type); \
88 	} GROUP_LINK_IN(ROMABLE_REGION)
89 
90 /**
91  * @brief Define a read-write iterable section output.
92  *
93  * @details
94  * Define an output section which will set up an iterable area
95  * of equally-sized data structures. For use with STRUCT_SECTION_ITERABLE().
96  * Input sections will be sorted by name, per ld's SORT_BY_NAME.
97  *
98  * This macro should be used for read-write data that is modified at runtime.
99  *
100  * Note that this keeps the symbols in the image even though
101  * they are not being directly referenced. Use this when symbols
102  * are indirectly referenced by iterating through the section.
103  */
104 #define ITERABLE_SECTION_RAM(struct_type, subalign) \
105 	SECTION_DATA_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
106 	{ \
107 		Z_LINK_ITERABLE(struct_type); \
108 	} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
109 
110 /**
111  * @brief Define a read-write iterable section output, sorted numerically.
112  *
113  * This version of ITERABLE_SECTION_RAM() sorts the entries numerically, that
114  * is, `SECNAME10` will come after `SECNAME2`. Up to 2 numeric digits are
115  * handled (0-99).
116  *
117  * @see ITERABLE_SECTION_RAM()
118  */
119 #define ITERABLE_SECTION_RAM_NUMERIC(struct_type, subalign) \
120 	SECTION_PROLOGUE(struct_type##_area, EMPTY, SUBALIGN(subalign)) \
121 	{ \
122 		Z_LINK_ITERABLE_NUMERIC(struct_type); \
123 	} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
124 
125 /**
126  * @brief Define a garbage collectable read-write iterable section output.
127  *
128  * @details
129  * Define an output section which will set up an iterable area
130  * of equally-sized data structures. For use with STRUCT_SECTION_ITERABLE().
131  * Input sections will be sorted by name, per ld's SORT_BY_NAME.
132  *
133  * This macro should be used for read-write data that is modified at runtime.
134  *
135  * Note that the symbols within the section can be garbage collected.
136  */
137 #define ITERABLE_SECTION_RAM_GC_ALLOWED(struct_type, subalign) \
138 	SECTION_DATA_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
139 	{ \
140 		Z_LINK_ITERABLE_GC_ALLOWED(struct_type); \
141 	} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
142 
143 /**
144  * @}
145  */ /* end of struct_section_apis */
146 
147 #endif /* INCLUDE_ZEPHYR_LINKER_ITERABLE_SECTIONS_H_ */
148