]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test,static-destruct: Use retain attribute to prevent linker garbage collection
authorJan Janssen <medhefgo@web.de>
Wed, 22 Dec 2021 00:33:10 +0000 (01:33 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 22 Dec 2021 03:14:08 +0000 (12:14 +0900)
Fixes: #21847
src/basic/static-destruct.h
src/fundamental/macro-fundamental.h
src/libsystemd/sd-bus/bus-error.h
src/shared/tests.h

index 7c5734d96021be2c962a9df6ea8bed638ca62162..079bc65b8207e873e1d011c2a961de870c87bf56 100644 (file)
@@ -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_                                  \
index 1f640b3ae3db52e702c1b1255b21e8a455eaa0e0..f87839d47bb56ae319c180d1b673da78e9dadb69 100644 (file)
@@ -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)))
index d981f72449ee26e07d979fe1ddb75b9ccf10e6ef..05dabdb7373e4fd22aaea94cf42539e0d197fda6 100644 (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\"")              \
         _section_("SYSTEMD_BUS_ERROR_MAP")                              \
         _used_                                                          \
-        __attribute__((retain))                                         \
+        _retain_                                                        \
         _alignptr_                                                      \
         _variable_no_sanitize_address_
 
index 95283e28297947ef9438b3bcb22f2384fb6cee3b..3b93aab498ae5bd1993d49cdebddb4a768c914b4 100644 (file)
@@ -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),                                                                \