]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
testsuite: Fix build when linking with lld
authorLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 3 Jan 2025 19:01:14 +0000 (13:01 -0600)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Sat, 4 Jan 2025 04:44:18 +0000 (22:44 -0600)
When building with clang and linking with lld there is an issue with
keeping the kmod_tests sections: On ELF targets, __attribute__((used))
prevents compiler discarding, but does not affect linker --gc-sections,
according to https://lld.llvm.org/ELF/start-stop-gc.

Make sure the _start/_stop symbols are not weak and add the "retain"
attribute.

Closes: https://github.com/kmod-project/kmod/issues/269
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/274
testsuite/testsuite.h

index 161817e7a669e7f197e3ef2c2dd584d4edef6bab..9486622ac4ded61dbbbf4ebe197c083deae23ea8 100644 (file)
@@ -126,19 +126,20 @@ int test_run(const struct test *t);
        } while (false)
 
 /* Test definitions */
-#define DEFINE_TEST_WITH_FUNC(_name, _func, ...)                             \
-       static const struct test UNIQ(s##_name)                              \
-               __attribute__((used, section("kmod_tests"), aligned(8))) = { \
-                       .name = #_name, .func = _func, ##__VA_ARGS__         \
-               };
+// clang-format off: At least up to version 18, it just makes a mess with _Pragma()
+#define DEFINE_TEST_WITH_FUNC(_name, _func, ...)                                         \
+       _Pragma("GCC diagnostic ignored \"-Wattributes\"")                               \
+       static const struct test UNIQ(s##_name)                                          \
+       __attribute__((retain, used, section("kmod_tests"), aligned(8))) = {             \
+               .name = #_name, .func = _func, ##__VA_ARGS__                             \
+       };
+// clang-format on
 
 #define DEFINE_TEST(_name, ...) DEFINE_TEST_WITH_FUNC(_name, _name, __VA_ARGS__)
 
 #define TESTSUITE_MAIN()                                                                 \
-       extern struct test __start_kmod_tests[]                                          \
-               __attribute__((weak, visibility("hidden")));                             \
-       extern struct test __stop_kmod_tests[]                                           \
-               __attribute__((weak, visibility("hidden")));                             \
+       extern struct test __start_kmod_tests[] __attribute__((visibility("hidden")));   \
+       extern struct test __stop_kmod_tests[] __attribute__((visibility("hidden")));    \
        int main(int argc, char *argv[])                                                 \
        {                                                                                \
                const struct test *t;                                                    \