1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-01-31 01:47:15 +03:00

test,static-destruct: Use retain attribute to prevent linker garbage collection

Fixes: #21847
This commit is contained in:
Jan Janssen 2021-12-22 01:33:10 +01:00 committed by Yu Watanabe
parent 8470a16d6f
commit a3aff1c47f
4 changed files with 8 additions and 2 deletions

View File

@ -24,12 +24,16 @@ typedef struct StaticDestructor {
typeof(variable) *q = p; \ typeof(variable) *q = p; \
func(q); \ func(q); \
} \ } \
/* Older compilers don't know retain attribute. */ \
_Pragma("GCC diagnostic ignored \"-Wattributes\"") \
/* The actual destructor structure we place in a special section to find it */ \ /* The actual destructor structure we place in a special section to find it */ \
_section_("SYSTEMD_STATIC_DESTRUCT") \ _section_("SYSTEMD_STATIC_DESTRUCT") \
/* We pick pointer alignment, since that is apparently what gcc does for static variables */ \ /* We pick pointer alignment, since that is apparently what gcc does for static variables */ \
_alignptr_ \ _alignptr_ \
/* Make sure this is not dropped from the image because not explicitly referenced */ \ /* Make sure this is not dropped from the image because not explicitly referenced */ \
_used_ \ _used_ \
/* Prevent linker from garbage collection. */ \
_retain_ \
/* Make sure that AddressSanitizer doesn't pad this variable: we want everything in this section /* Make sure that AddressSanitizer doesn't pad this variable: we want everything in this section
* packed next to each other so that we can enumerate it. */ \ * packed next to each other so that we can enumerate it. */ \
_variable_no_sanitize_address_ \ _variable_no_sanitize_address_ \

View File

@ -13,6 +13,7 @@
#define _pure_ __attribute__((__pure__)) #define _pure_ __attribute__((__pure__))
#define _section_(x) __attribute__((__section__(x))) #define _section_(x) __attribute__((__section__(x)))
#define _packed_ __attribute__((__packed__)) #define _packed_ __attribute__((__packed__))
#define _retain_ __attribute__((__retain__))
#define _used_ __attribute__((__used__)) #define _used_ __attribute__((__used__))
#define _unused_ __attribute__((__unused__)) #define _unused_ __attribute__((__unused__))
#define _cleanup_(x) __attribute__((__cleanup__(x))) #define _cleanup_(x) __attribute__((__cleanup__(x)))

View File

@ -38,7 +38,7 @@ int bus_error_set_errnofv(sd_bus_error *e, int error, const char *format, va_lis
_Pragma("GCC diagnostic ignored \"-Wattributes\"") \ _Pragma("GCC diagnostic ignored \"-Wattributes\"") \
_section_("SYSTEMD_BUS_ERROR_MAP") \ _section_("SYSTEMD_BUS_ERROR_MAP") \
_used_ \ _used_ \
__attribute__((retain)) \ _retain_ \
_alignptr_ \ _alignptr_ \
_variable_no_sanitize_address_ _variable_no_sanitize_address_

View File

@ -57,7 +57,8 @@ typedef struct TestFunc {
/* See static-destruct.h for an explanation of how this works. */ /* See static-destruct.h for an explanation of how this works. */
#define REGISTER_TEST(func, ...) \ #define REGISTER_TEST(func, ...) \
_section_("SYSTEMD_TEST_TABLE") _alignptr_ _used_ _variable_no_sanitize_address_ \ _Pragma("GCC diagnostic ignored \"-Wattributes\"") \
_section_("SYSTEMD_TEST_TABLE") _alignptr_ _used_ _retain_ _variable_no_sanitize_address_ \
static const TestFunc UNIQ_T(static_test_table_entry, UNIQ) = { \ static const TestFunc UNIQ_T(static_test_table_entry, UNIQ) = { \
.f = (union f) &(func), \ .f = (union f) &(func), \
.name = STRINGIFY(func), \ .name = STRINGIFY(func), \