From a3aff1c47f61da74671fcbf9bb39fc51a461e79e Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Wed, 22 Dec 2021 01:33:10 +0100 Subject: [PATCH] test,static-destruct: Use retain attribute to prevent linker garbage collection Fixes: #21847 --- src/basic/static-destruct.h | 4 ++++ src/fundamental/macro-fundamental.h | 1 + src/libsystemd/sd-bus/bus-error.h | 2 +- src/shared/tests.h | 3 ++- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/basic/static-destruct.h b/src/basic/static-destruct.h index 7c5734d9602..079bc65b820 100644 --- a/src/basic/static-destruct.h +++ b/src/basic/static-destruct.h @@ -24,12 +24,16 @@ typedef struct StaticDestructor { typeof(variable) *q = p; \ 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 */ \ _section_("SYSTEMD_STATIC_DESTRUCT") \ /* We pick pointer alignment, since that is apparently what gcc does for static variables */ \ _alignptr_ \ /* Make sure this is not dropped from the image because not explicitly referenced */ \ _used_ \ + /* Prevent linker from garbage collection. */ \ + _retain_ \ /* 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. */ \ _variable_no_sanitize_address_ \ diff --git a/src/fundamental/macro-fundamental.h b/src/fundamental/macro-fundamental.h index 1f640b3ae3d..f87839d47bb 100644 --- a/src/fundamental/macro-fundamental.h +++ b/src/fundamental/macro-fundamental.h @@ -13,6 +13,7 @@ #define _pure_ __attribute__((__pure__)) #define _section_(x) __attribute__((__section__(x))) #define _packed_ __attribute__((__packed__)) +#define _retain_ __attribute__((__retain__)) #define _used_ __attribute__((__used__)) #define _unused_ __attribute__((__unused__)) #define _cleanup_(x) __attribute__((__cleanup__(x))) diff --git a/src/libsystemd/sd-bus/bus-error.h b/src/libsystemd/sd-bus/bus-error.h index d981f72449e..05dabdb7373 100644 --- a/src/libsystemd/sd-bus/bus-error.h +++ b/src/libsystemd/sd-bus/bus-error.h @@ -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\"") \ _section_("SYSTEMD_BUS_ERROR_MAP") \ _used_ \ - __attribute__((retain)) \ + _retain_ \ _alignptr_ \ _variable_no_sanitize_address_ diff --git a/src/shared/tests.h b/src/shared/tests.h index 95283e28297..3b93aab498a 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -57,7 +57,8 @@ typedef struct TestFunc { /* See static-destruct.h for an explanation of how this works. */ #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) = { \ .f = (union f) &(func), \ .name = STRINGIFY(func), \ -- 2.47.3