Re: [PATCH 0/4] -ffreestanding/-fno-builtin-* patches

From: Arvind Sankar
Date: Tue Aug 18 2020 - 18:25:51 EST


On Mon, Aug 17, 2020 at 03:02:08PM -0700, Nick Desaulniers wrote:
> -ffreestanding typically inhibits "libcall optimizations" where calls to
> certain library functions can be replaced by the compiler in certain
> cases to calls to other library functions that may be more efficient.
> This can be problematic for embedded targets that don't provide full
> libc implementations.
>
> -ffreestanding inhibits all such optimizations, which is the safe
> choice, but generally we want the optimizations that are performed. The
> Linux kernel does implement a fair amount of libc routines. Instead of
> -ffreestanding (which makes more sense in smaller images like kexec's
> purgatory image), prefer -fno-builtin-* flags to disable the compiler
> from emitting calls to functions which may not be defined.
>
> If you see a linkage failure due to a missing symbol that's typically
> defined in a libc, and not explicitly called from the source code, then
> the compiler may have done such a transform. You can either implement
> such a function (ie. in lib/string.c) or disable the transform outright
> via -fno-builtin-* flag (where * is the name of the library routine, ie.
> -fno-builtin-bcmp).
>
> Patch 1 unbreaks the build with ToT clang, which has been red all
> weekend, by adding -fno-builtin-stpcpy.
> Patch 2 is a revert but adds -fno-builtin-bcmp.
> Patch 3 does the same for x86 purgatory.
> Patch 4 removes -ffreestanding from i386.
>
> The first patch makes sense for Kbuild, the second maybe akpm@, the
> third and forth for x86. Not sure who should pick up the series (they
> can be merged out of order, technically) but I really need the first
> patch soon. The 3 latter patches are cleanups.
>
> Nick Desaulniers (4):
> Makefile: add -fno-builtin-stpcpy
> Revert "lib/string.c: implement a basic bcmp"
> x86/boot: use -fno-builtin-bcmp
> x86: don't build CONFIG_X86_32 as -ffreestanding
>
> Makefile | 7 +++++++
> arch/x86/Makefile | 3 ---
> arch/x86/boot/Makefile | 1 +
> arch/x86/boot/string.c | 8 --------
> include/linux/string.h | 3 ---
> lib/string.c | 20 --------------------
> 6 files changed, 8 insertions(+), 34 deletions(-)
>
> --
> 2.28.0.220.ged08abb693-goog
>

Another thing that needs to be fixed is that at least lib/string.c needs
to be compiled with -ffreestanding.

gcc-10 optimizes the generic memset implementation in there into a call
to memset. Now that's on x86 which doesn't use the generic
implementation, but this is just waiting to bite us.

https://godbolt.org/z/6EhG15