[tip: x86/build] x86/build: Check whether the compiler is sane

From: tip-bot2 for Borislav Petkov
Date: Mon Apr 27 2020 - 07:37:55 EST


The following commit has been merged into the x86/build branch of tip:

Commit-ID: 73da86741e7f75c9f1b5c8a2660c90aa41840972
Gitweb: https://git.kernel.org/tip/73da86741e7f75c9f1b5c8a2660c90aa41840972
Author: Borislav Petkov <bp@xxxxxxx>
AuthorDate: Thu, 23 Apr 2020 19:28:28 +02:00
Committer: Borislav Petkov <bp@xxxxxxx>
CommitterDate: Mon, 27 Apr 2020 13:31:57 +02:00

x86/build: Check whether the compiler is sane

Add a check script to verify whether the compiler is sane. This is
x86-only for now and checks one thing only but should be useful for more
checks in the future.

Suggested-by: Martin LiÅka <mliska@xxxxxxx>
Signed-off-by: Borislav Petkov <bp@xxxxxxx>
Reviewed-by: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>
Tested-by: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>
Link: https://lkml.kernel.org/r/20200423173058.GE26021@xxxxxxx
---
arch/x86/Makefile | 4 ++++
scripts/x86-check-compiler.sh | 9 +++++++++
2 files changed, 13 insertions(+)
create mode 100755 scripts/x86-check-compiler.sh

diff --git a/arch/x86/Makefile b/arch/x86/Makefile
index 00e378d..38d3eec 100644
--- a/arch/x86/Makefile
+++ b/arch/x86/Makefile
@@ -1,6 +1,10 @@
# SPDX-License-Identifier: GPL-2.0
# Unified Makefile for i386 and x86_64

+# Check the compiler
+sane_compiler := $(shell $(srctree)/scripts/x86-check-compiler.sh $(CC))
+$(if $(sane_compiler),$(error $(CC) check failed. Aborting),)
+
# select defconfig based on actual architecture
ifeq ($(ARCH),x86)
ifeq ($(shell uname -m),x86_64)
diff --git a/scripts/x86-check-compiler.sh b/scripts/x86-check-compiler.sh
new file mode 100755
index 0000000..b2b5b54
--- /dev/null
+++ b/scripts/x86-check-compiler.sh
@@ -0,0 +1,9 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+
+# Check whether the compiler tail-call optimizes across an asm() statement.
+# Fail the build if it does.
+
+echo "int foo(int a); int bar(int a) { int r = foo(a); asm(\"\"); return r; }" |\
+ $* -O2 -x c -c -S - -o - 2>/dev/null |\
+ grep -E "^[[:blank:]]+jmp[[:blank:]]+.*"