CMake: Fix three checks if building with -flto

In CMake, check_c_source_compiles() always links too. With
link-time optimization, unused functions may get omitted if
main() doesn't depend on them. Consider the following which
tries to check if somefunction() is available when <someheader.h>
has been included:

    #include <someheader.h>
    int foo(void) { return somefunction(); }
    int main(void) { return 0; }

LTO may omit foo() completely because the program as a whole doesn't
need it and then the program will link even if the symbol somefunction
isn't available in libc or other library being linked in, and then
the test may pass when it shouldn't.

What happens if <someheader.h> doesn't declare somefunction()?
Shouldn't the test fail in the compilation phase already? It should
but many compilers don't follow the C99 and later standards that
prohibit implicit function declarations. Instead such compilers
assume that somefunction() exists, compilation succeeds (with a
warning), and then linker with LTO omits the call to somefunction().

Change the tests so that they are part of main(). If compiler accepts
implicitly declared functions, LTO cannot omit them because it has to
assume that they might have side effects and thus linking will fail.
On the other hand, if the functions/intrinsics being used are supported,
they might get optimized away but in that case it's fine because they
really are supported.

It is fine to use __attribute__((target(...))) for main(). At least
it works with GCC 4.9 to 14.1 on x86-64.

Reported-by: Sam James <sam@gentoo.org>
This commit is contained in:
Lasse Collin 2024-06-24 22:41:10 +03:00
parent 78e882205e
commit 114cba69db
1 changed files with 8 additions and 11 deletions

View File

@ -1271,12 +1271,12 @@ calculation (with runtime detection) if supported by the compiler" ON)
&& !defined(__EDG__) && !defined(__EDG__)
__attribute__((__target__(\"ssse3,sse4.1,pclmul\"))) __attribute__((__target__(\"ssse3,sse4.1,pclmul\")))
#endif #endif
__m128i my_clmul(__m128i a) int main(void)
{ {
const __m128i b = _mm_set_epi64x(1, 2); __m128i a = _mm_set_epi64x(1, 2);
return _mm_clmulepi64_si128(a, b, 0); a = _mm_clmulepi64_si128(a, a, 0);
return 0;
} }
int main(void) { return 0; }
" "
HAVE_USABLE_CLMUL) HAVE_USABLE_CLMUL)
tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL) tuklib_add_definition_if(liblzma HAVE_USABLE_CLMUL)
@ -1301,11 +1301,10 @@ if(XZ_ARM64_CRC32)
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__) #if (defined(__GNUC__) || defined(__clang__)) && !defined(__EDG__)
__attribute__((__target__(\"+crc\"))) __attribute__((__target__(\"+crc\")))
#endif #endif
uint32_t my_crc(uint32_t a, uint64_t b) int main(void)
{ {
return __crc32d(a, b); return __crc32d(1, 2) != 0;
} }
int main(void) { return 0; }
" "
HAVE_ARM64_CRC32) HAVE_ARM64_CRC32)
@ -1773,16 +1772,14 @@ if(NOT SANDBOX_FOUND AND XZ_SANDBOX MATCHES "^auto$|^landlock$")
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/prctl.h> #include <sys/prctl.h>
void my_sandbox(void) int main(void)
{ {
(void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0); (void)prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0);
(void)SYS_landlock_create_ruleset; (void)SYS_landlock_create_ruleset;
(void)SYS_landlock_restrict_self; (void)SYS_landlock_restrict_self;
(void)LANDLOCK_CREATE_RULESET_VERSION; (void)LANDLOCK_CREATE_RULESET_VERSION;
return; return 0;
} }
int main(void) { return 0; }
" "
HAVE_LINUX_LANDLOCK) HAVE_LINUX_LANDLOCK)