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 /**
36  * @brief Define a read-only iterable section output.
37  *
38  * @details
39  * Define an output section which will set up an iterable area
40  * of equally-sized data structures. For use with STRUCT_SECTION_ITERABLE().
41  * Input sections will be sorted by name, per ld's SORT_BY_NAME.
42  *
43  * This macro should be used for read-only data.
44  *
45  * Note that this keeps the symbols in the image even though
46  * they are not being directly referenced. Use this when symbols
47  * are indirectly referenced by iterating through the section.
48  */
49 #define ITERABLE_SECTION_ROM(struct_type, subalign) \
50 	SECTION_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
51 	{ \
52 		Z_LINK_ITERABLE(struct_type); \
53 	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
54 
55 /**
56  * @brief Define a read-only iterable section output, sorted numerically.
57  *
58  * This version of ITERABLE_SECTION_ROM() sorts the entries numerically, that
59  * is, `SECNAME_10` will come after `SECNAME_2`. `_` separator is required, and
60  * up to 2 numeric digits are handled (0-99).
61  *
62  * @see ITERABLE_SECTION_ROM()
63  */
64 #define ITERABLE_SECTION_ROM_NUMERIC(struct_type, subalign) \
65 	SECTION_PROLOGUE(struct_type##_area, EMPTY, SUBALIGN(subalign)) \
66 	{ \
67 		Z_LINK_ITERABLE_NUMERIC(struct_type); \
68 	} GROUP_ROM_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
69 
70 /**
71  * @brief Define a garbage collectable read-only iterable section output.
72  *
73  * @details
74  * Define an output section which will set up an iterable area
75  * of equally-sized data structures. For use with STRUCT_SECTION_ITERABLE().
76  * Input sections will be sorted by name, per ld's SORT_BY_NAME.
77  *
78  * This macro should be used for read-only data.
79  *
80  * Note that the symbols within the section can be garbage collected.
81  */
82 #define ITERABLE_SECTION_ROM_GC_ALLOWED(struct_type, subalign) \
83 	SECTION_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
84 	{ \
85 		Z_LINK_ITERABLE_GC_ALLOWED(struct_type); \
86 	} GROUP_LINK_IN(ROMABLE_REGION)
87 
88 /**
89  * @brief Define a read-write iterable section output.
90  *
91  * @details
92  * Define an output section which will set up an iterable area
93  * of equally-sized data structures. For use with STRUCT_SECTION_ITERABLE().
94  * Input sections will be sorted by name, per ld's SORT_BY_NAME.
95  *
96  * This macro should be used for read-write data that is modified at runtime.
97  *
98  * Note that this keeps the symbols in the image even though
99  * they are not being directly referenced. Use this when symbols
100  * are indirectly referenced by iterating through the section.
101  */
102 #define ITERABLE_SECTION_RAM(struct_type, subalign) \
103 	SECTION_DATA_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
104 	{ \
105 		Z_LINK_ITERABLE(struct_type); \
106 	} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
107 
108 /**
109  * @brief Define a read-write iterable section output, sorted numerically.
110  *
111  * This version of ITERABLE_SECTION_RAM() sorts the entries numerically, that
112  * is, `SECNAME10` will come after `SECNAME2`. Up to 2 numeric digits are
113  * handled (0-99).
114  *
115  * @see ITERABLE_SECTION_RAM()
116  */
117 #define ITERABLE_SECTION_RAM_NUMERIC(struct_type, subalign) \
118 	SECTION_PROLOGUE(struct_type##_area, EMPTY, SUBALIGN(subalign)) \
119 	{ \
120 		Z_LINK_ITERABLE_NUMERIC(struct_type); \
121 	} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
122 
123 /**
124  * @brief Define a garbage collectable read-write iterable section output.
125  *
126  * @details
127  * Define an output section which will set up an iterable area
128  * of equally-sized data structures. For use with STRUCT_SECTION_ITERABLE().
129  * Input sections will be sorted by name, per ld's SORT_BY_NAME.
130  *
131  * This macro should be used for read-write data that is modified at runtime.
132  *
133  * Note that the symbols within the section can be garbage collected.
134  */
135 #define ITERABLE_SECTION_RAM_GC_ALLOWED(struct_type, subalign) \
136 	SECTION_DATA_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
137 	{ \
138 		Z_LINK_ITERABLE_GC_ALLOWED(struct_type); \
139 	} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
140 
141 /**
142  * @}
143  */ /* end of struct_section_apis */
144 
145 #endif /* INCLUDE_ZEPHYR_LINKER_ITERABLE_SECTIONS_H_ */
146