Skip to content

Commit

Permalink
LoongArch: Replace -ffreestanding with finer-grained -fno-builtin's
Browse files Browse the repository at this point in the history
As explained by Nick in the original issue: the kernel usually does a
good job of providing library helpers that have similar semantics as
their ordinary userspace libc equivalents, but -ffreestanding disables
such libcall optimization and other related features in the compiler,
which can lead to unexpected things such as CONFIG_FORTIFY_SOURCE not
working (!).

As it turns out to be the case, only the memory operations really need
to be prevented from expansion by the compiler, and this is doable with
finer-grained -fno-builtin-* toggles. So only disable memcpy, memmove
and memset, while leaving other builtins enabled, to fix source
fortification among others.

Closes: ClangBuiltLinux#1897
Reported-by: Nathan Chancellor <[email protected]>
Suggested-by: Nick Desaulniers <[email protected]>
Signed-off-by: WANG Xuerui <[email protected]>
  • Loading branch information
xen0n authored and intel-lab-lkp committed Aug 6, 2023
1 parent f0ab9f3 commit 8d37a2f
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion arch/loongarch/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ KBUILD_CFLAGS_KERNEL += -fPIE
LDFLAGS_vmlinux += -static -pie --no-dynamic-linker -z notext
endif

cflags-y += -ffreestanding
# Make sure the memory libcalls are not expanded by the compiler, for better
# control over unaligned accesses with respect to CONFIG_ARCH_STRICT_ALIGN,
# and also for avoiding https://gcc.gnu.org/PR109465.
#
# The overly broad -ffreestanding is undesirable as it disables *all* libcall
# handling, that unfortunately includes proper FORTIFY_SOURCE instrumentation.
cflags-y += -fno-builtin-memcpy -fno-builtin-memmove -fno-builtin-memset

cflags-y += $(call cc-option, -mno-check-zero-division)

load-y = 0x9000000000200000
Expand Down

0 comments on commit 8d37a2f

Please sign in to comment.