Skip to content

Commit 04d706b

Browse files
dcpleungnashif
authored andcommitted
linker: add macros so iterable sections can be garbage collected
The iterable section macros Z_ITERABLE_SECTION_ROM()/_RAM() uses Z_LINK_ITERABLE() which does KEEP() on all symbols. This results in all symbols under those sections being kept in final image even though these symbols are not referred directly from the code. This adds the new set of macros which does not force KEEP() on the symbols, and allows symbols to be garbage collected. The existing macros are kept as-is so as not to change their behaviors. Signed-off-by: Daniel Leung <[email protected]>
1 parent 0fe62c6 commit 04d706b

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

include/linker/linker-defs.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,72 @@
4242
KEEP(*(SORT_BY_NAME(._##struct_type.static.*))); \
4343
_CONCAT(_##struct_type, _list_end) = .
4444

45+
#define Z_LINK_ITERABLE_GC_ALLOWED(struct_type) \
46+
_CONCAT(_##struct_type, _list_start) = .; \
47+
*(SORT_BY_NAME(._##struct_type.static.*)); \
48+
_CONCAT(_##struct_type, _list_end) = .
49+
4550
/* Define an output section which will set up an iterable area
4651
* of equally-sized data structures. For use with Z_STRUCT_SECTION_ITERABLE.
4752
* Input sections will be sorted by name, per ld's SORT_BY_NAME.
4853
*
4954
* This macro should be used for read-only data.
55+
*
56+
* Note that this keeps the symbols in the image even though
57+
* they are not being directly referenced. Use this when symbols
58+
* are indirectly referenced by iterating through the section.
5059
*/
5160
#define Z_ITERABLE_SECTION_ROM(struct_type, subalign) \
5261
SECTION_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
5362
{ \
5463
Z_LINK_ITERABLE(struct_type); \
5564
} GROUP_LINK_IN(ROMABLE_REGION)
5665

66+
/* Define an output section which will set up an iterable area
67+
* of equally-sized data structures. For use with Z_STRUCT_SECTION_ITERABLE.
68+
* Input sections will be sorted by name, per ld's SORT_BY_NAME.
69+
*
70+
* This macro should be used for read-only data.
71+
*
72+
* Note that the symbols within the section can be garbage collected.
73+
*/
74+
#define Z_ITERABLE_SECTION_ROM_GC_ALLOWED(struct_type, subalign) \
75+
SECTION_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
76+
{ \
77+
Z_LINK_ITERABLE_GC_ALLOWED(struct_type); \
78+
} GROUP_LINK_IN(ROMABLE_REGION)
79+
5780
/* Define an output section which will set up an iterable area
5881
* of equally-sized data structures. For use with Z_STRUCT_SECTION_ITERABLE.
5982
* Input sections will be sorted by name, per ld's SORT_BY_NAME.
6083
*
6184
* This macro should be used for read-write data that is modified at runtime.
85+
*
86+
* Note that this keeps the symbols in the image even though
87+
* they are not being directly referenced. Use this when symbols
88+
* are indirectly referenced by iterating through the section.
6289
*/
6390
#define Z_ITERABLE_SECTION_RAM(struct_type, subalign) \
6491
SECTION_DATA_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
6592
{ \
6693
Z_LINK_ITERABLE(struct_type); \
6794
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
6895

96+
97+
/* Define an output section which will set up an iterable area
98+
* of equally-sized data structures. For use with Z_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 the symbols within the section can be garbage collected.
104+
*/
105+
#define Z_ITERABLE_SECTION_RAM_GC_ALLOWED(struct_type, subalign) \
106+
SECTION_DATA_PROLOGUE(struct_type##_area,,SUBALIGN(subalign)) \
107+
{ \
108+
Z_LINK_ITERABLE_GC_ALLOWED(struct_type); \
109+
} GROUP_DATA_LINK_IN(RAMABLE_REGION, ROMABLE_REGION)
110+
69111
/*
70112
* generate a symbol to mark the start of the objects array for
71113
* the specified object and level, then link all of those objects

0 commit comments

Comments
 (0)