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