From 5bad38935238da045d9fd467eae9dfd958deea34 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 17 Sep 2026 14:17:22 +0200 Subject: build/x86_64: add missing -ffreestanding flag Enable the '-ffreestanding' flag in the x86_64 toolchain file to inform the compiler about the fact that it cannot rely on the presence of a standard library implementation, thereby suppressing some optimizations designed for regular user applications. GCC supports a number of clever optimizations, some of which rely on the presence of a standard library. One of them is "-ftree-loop-distribute-patterns". This optimization performs detection of certain loop patterns, replacing them with library calls when applicable. This is problematic for cases in which we implement the standard library functionality. Consider, a schematic, `memset``: ```c++ void memset(void * d, int v, size_t n) { for(auto i = 0uz; i < n: ++i) { d[i] = static_cast(v); } } ``` GCC recognizes this loop, and similar versions of it, as an implementation of `memset`. In a hosted implementation, this is an important optimization, since a standard library may be able to provide a specifically optimized version of `memset``. So the compiler replaces this `memset` loop with a call to `memset`, since it is not aware of the fact we are currently implementing `memset`. Interestingly, tail recursion optimization eliminates the call, transforming it into a jump to the start of `memset`. This leads to execution getting stuck inside `memset` with no way of exiting and no stack usage increase, thus causing an infinite lockup. While we could suppress the specific optimization, it is more effective to tell the compiler "why" it can emit a call to memset in those cases. --- cmake/Platforms/x86_64.cmake | 1 + 1 file changed, 1 insertion(+) (limited to 'cmake/Platforms/x86_64.cmake') diff --git a/cmake/Platforms/x86_64.cmake b/cmake/Platforms/x86_64.cmake index 76ba0509..2cbf18b1 100644 --- a/cmake/Platforms/x86_64.cmake +++ b/cmake/Platforms/x86_64.cmake @@ -22,6 +22,7 @@ set(CMAKE_CXX_FLAGS_INIT "-fno-use-cxa-atexit" "-ffunction-sections" "-fdata-sections" + "-ffreestanding" ) list(JOIN CMAKE_CXX_FLAGS_INIT " " CMAKE_CXX_FLAGS_INIT) -- cgit v1.2.3