Skip to content

Commit a3aff1c

Browse files
medhefgoyuwata
authored andcommitted
test,static-destruct: Use retain attribute to prevent linker garbage collection
Fixes: systemd#21847
1 parent 8470a16 commit a3aff1c

File tree

4 files changed

+8
-2
lines changed

4 files changed

+8
-2
lines changed

src/basic/static-destruct.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ typedef struct StaticDestructor {
2424
typeof(variable) *q = p; \
2525
func(q); \
2626
} \
27+
/* Older compilers don't know retain attribute. */ \
28+
_Pragma("GCC diagnostic ignored \"-Wattributes\"") \
2729
/* The actual destructor structure we place in a special section to find it */ \
2830
_section_("SYSTEMD_STATIC_DESTRUCT") \
2931
/* We pick pointer alignment, since that is apparently what gcc does for static variables */ \
3032
_alignptr_ \
3133
/* Make sure this is not dropped from the image because not explicitly referenced */ \
3234
_used_ \
35+
/* Prevent linker from garbage collection. */ \
36+
_retain_ \
3337
/* Make sure that AddressSanitizer doesn't pad this variable: we want everything in this section
3438
* packed next to each other so that we can enumerate it. */ \
3539
_variable_no_sanitize_address_ \

src/fundamental/macro-fundamental.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define _pure_ __attribute__((__pure__))
1414
#define _section_(x) __attribute__((__section__(x)))
1515
#define _packed_ __attribute__((__packed__))
16+
#define _retain_ __attribute__((__retain__))
1617
#define _used_ __attribute__((__used__))
1718
#define _unused_ __attribute__((__unused__))
1819
#define _cleanup_(x) __attribute__((__cleanup__(x)))

src/libsystemd/sd-bus/bus-error.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_lis
3838
_Pragma("GCC diagnostic ignored \"-Wattributes\"") \
3939
_section_("SYSTEMD_BUS_ERROR_MAP") \
4040
_used_ \
41-
__attribute__((retain)) \
41+
_retain_ \
4242
_alignptr_ \
4343
_variable_no_sanitize_address_
4444

src/shared/tests.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ typedef struct TestFunc {
5757

5858
/* See static-destruct.h for an explanation of how this works. */
5959
#define REGISTER_TEST(func, ...) \
60-
_section_("SYSTEMD_TEST_TABLE") _alignptr_ _used_ _variable_no_sanitize_address_ \
60+
_Pragma("GCC diagnostic ignored \"-Wattributes\"") \
61+
_section_("SYSTEMD_TEST_TABLE") _alignptr_ _used_ _retain_ _variable_no_sanitize_address_ \
6162
static const TestFunc UNIQ_T(static_test_table_entry, UNIQ) = { \
6263
.f = (union f) &(func), \
6364
.name = STRINGIFY(func), \

0 commit comments

Comments
 (0)