kbuild: add CLANG_TRIPLE to prevent clang from compiling with wrong --target
RFC PATCH 1/1 kbuild: add CLANG_TRIPLE to prevent clang from compiling with wrong --target
Kernel compiled with tool chain CROSS_COMPILE=aarch64-linux-android- will panic during the startup phase.
Clang's --target option comes from $(CROSS_COMPILE). At the time -fstack-protector-strong is enabled, and compiled with command:
make CC=clang HOSTCC=clang ARCH=arm64 CROSS_COMPILE=aarch64-linux-android-
clang will insert code like:
mrs x8, TPIDR_EL0 //default value is zero str x8, [sp] ldr x8, [x8, #40] //access addr 0x40
instead of the code that accesses __stack_chk_guard to get the canary, which will cause the kernel to crash due to 0x40 address access.
This patch (from android) is used to remind the user that current tool chain cannot be used as the "--target" of clang, the user should specify an additional "--target" through CLANG_TRIPLE.
--- Makefile | 6 +++++- scripts/clang-android.sh | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100755 scripts/clang-android.sh
Makefile +5 -1
@@ -586,7 +586,11 @@ CC_VERSION_TEXT = $(subst $(pound),,$(shell $(CC) --version 2>/dev/null | head - ifneq ($(findstring clang,$(CC_VERSION_TEXT)),) ifneq ($(CROSS_COMPILE),) -CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%)) +CLANG_TRIPLE ?= $(CROSS_COMPILE) +CLANG_FLAGS += --target=$(notdir $(CLANG_TRIPLE:%-=%)) +ifeq ($(shell $(srctree)/scripts/clang-android.sh $(CC) $(CLANG_FLAGS)), y) +$(error "Clang with Android --target detected. Did you specify CLANG_TRIPLE?") +endif endif ifeq ($(LLVM_IAS),1) CLANG_FLAGS += -integrated-as
scripts/clang-android.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +$* -dM -E - </dev/null 2>&1 | grep -q __ANDROID__ && echo "y"
Signature · 1 line
2.7.4
Quoted 13 lines · ashimida
Kernel compiled with tool chain CROSS_COMPILE=aarch64-linux-android- will panic during the startup phase.
Clang's --target option comes from $(CROSS_COMPILE). At the time -fstack-protector-strong is enabled, and compiled with command:
make CC=clang HOSTCC=clang ARCH=arm64 CROSS_COMPILE=aarch64-linux-android-clang will insert code like:
mrs x8, TPIDR_EL0 //default value is zero str x8, [sp] ldr x8, [x8, #40] //access addr 0x40
Quoted 7 lines
instead of the code that accesses
__stack_chk_guardto get the canary, which will cause the kernel to crash due to0x40address access.This patch (from android) is used to remind the user that current tool chain cannot be used as the "--target" of clang, the user should specify an additional "--target" through
CLANG_TRIPLE.
Hi Ashimida,
Thanks for sending this patch; I recognize it from Android, which we had to carry for years due to:
1. reliance on GNU as ie. "GAS"
2. not distributing binary prefixes of GNU binutils with a target triple that clang recognized. (ie. Android's binutils were prefixed aarch64-linux-android- while Clang expected something more like aarch64-linux-gnu for --target=)
We solved this by working out the issues in clang's assembler. With LLVM=1 LLVM_IAS=1, we no longer rely on GNU binutils, and no longer need such patch. You'll find it's been dropped from Android Common Kernels now. With mainline, LLVM_IAS=1 is now the default when building with LLVM=1, and CROSS_COMPILE is now inferred from ARCH for LLVM=1 as well.
So all you should need is:
$ ARCH=arm64 make LLVM=1 -j$(nproc)
Is there a reason why the above doesn't work for you? I do not wish to see this patch upstream (or downstream; it should be unnecessary).
Quoted 38 lines
Signed-off-by ashimida <[email protected]>--- Makefile | 6 +++++- scripts/clang-android.sh | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100755 scripts/clang-android.shMakefile +5
-1@@ -586,7 +586,11 @@ CC_VERSION_TEXT = $(subst $(pound),,$(shell $(CC) --version 2>/dev/null | head - ifneq ($(findstring clang,$(CC_VERSION_TEXT)),) ifneq ($(CROSS_COMPILE),) -CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%)) +CLANG_TRIPLE ?= $(CROSS_COMPILE) +CLANG_FLAGS += --target=$(notdir $(CLANG_TRIPLE:%-=%)) +ifeq ($(shell $(srctree)/scripts/clang-android.sh $(CC) $(CLANG_FLAGS)), y) +$(error "Clang with Android --target detected. Did you specify CLANG_TRIPLE?") +endif endif ifeq ($(LLVM_IAS),1) CLANG_FLAGS += -integrated-asscripts/clang-android.sh new file +4
-1@@ -0,0 +1,4 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +$* -dM -E - </dev/null 2>&1 | grep -q __ANDROID__ && echo "y" --2.7.4
Signature · 2 lines
Thanks, ~Nick Desaulniers
Hi Desaulniers,
I now understand the meaning here, the command works fine for me.
Thank you very much for the reply!
Quoted 84 lines · Nick Desaulniers
Quoted 13 lines · ashimida
Kernel compiled with tool chain CROSS_COMPILE=aarch64-linux-android- will panic during the startup phase.
Clang's --target option comes from $(CROSS_COMPILE). At the time -fstack-protector-strong is enabled, and compiled with command:
make CC=clang HOSTCC=clang ARCH=arm64 CROSS_COMPILE=aarch64-linux-android-clang will insert code like:
mrs x8, TPIDR_EL0 //default value is zero str x8, [sp] ldr x8, [x8, #40] //access addr 0x40Quoted 7 lines
instead of the code that accesses
__stack_chk_guardto get the canary, which will cause the kernel to crash due to0x40address access.This patch (from android) is used to remind the user that current tool chain cannot be used as the "--target" of clang, the user should specify an additional "--target" through
CLANG_TRIPLE.Hi Ashimida,
Thanks for sending this patch; I recognize it from Android, which we had to carry for years due to:
1. reliance on GNUasie. "GAS"
2. not distributing binary prefixes of GNU binutils with a target triple that clang recognized. (ie. Android's binutils were prefixed aarch64-linux-android- while Clang expected something more like aarch64-linux-gnu for --target=)We solved this by working out the issues in clang's assembler. With LLVM=1 LLVM_IAS=1, we no longer rely on GNU binutils, and no longer need such patch. You'll find it's been dropped from Android Common Kernels now. With mainline, LLVM_IAS=1 is now the default when building with LLVM=1, and
CROSS_COMPILEis now inferred from ARCH for LLVM=1 as well.So all you should need is:
$ ARCH=arm64 make LLVM=1 -j$(nproc)Is there a reason why the above doesn't work for you? I do not wish to see this patch upstream (or downstream; it should be unnecessary).
Quoted 38 lines
Signed-off-by ashimida <[email protected]>--- Makefile | 6 +++++- scripts/clang-android.sh | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-)create mode 100755
scripts/clang-android.shMakefile +5
-1@@ -586,7 +586,11 @@ CC_VERSION_TEXT = $(subst $(pound),,$(shell $(CC) --version 2>/dev/null | head - ifneq ($(findstring clang,$(CC_VERSION_TEXT)),) ifneq ($(CROSS_COMPILE),) -CLANG_FLAGS += --target=$(notdir $(CROSS_COMPILE:%-=%)) +CLANG_TRIPLE ?= $(CROSS_COMPILE) +CLANG_FLAGS += --target=$(notdir $(CLANG_TRIPLE:%-=%)) +ifeq ($(shell $(srctree)/scripts/clang-android.sh $(CC) $(CLANG_FLAGS)), y) +$(error "Clang with Android --target detected. Did you specify CLANG_TRIPLE?") +endif endif ifeq ($(LLVM_IAS),1) CLANG_FLAGS += -integrated-asscripts/clang-android.sh new file +4
-1@@ -0,0 +1,4 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 + +$* -dM -E - </dev/null 2>&1 | grep -q __ANDROID__ && echo "y" --2.7.4