From nobody Thu Feb 12 05:03:34 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 620B0C77B60 for ; Fri, 28 Apr 2023 10:02:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345872AbjD1KCi (ORCPT ); Fri, 28 Apr 2023 06:02:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346049AbjD1KCP (ORCPT ); Fri, 28 Apr 2023 06:02:15 -0400 Received: from out0-212.mail.aliyun.com (out0-212.mail.aliyun.com [140.205.0.212]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60E9B5FEA for ; Fri, 28 Apr 2023 03:02:04 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R981e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047213;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=11;SR=0;TI=SMTPD_---.STCEPrW_1682675622; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.STCEPrW_1682675622) by smtp.aliyun-inc.com; Fri, 28 Apr 2023 17:53:43 +0800 From: "Hou Wenlong" To: linux-kernel@vger.kernel.org Cc: "Thomas Garnier" , "Lai Jiangshan" , "Kees Cook" , "Hou Wenlong" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "H. Peter Anvin" Subject: [PATCH RFC 35/43] x86/pie: Build the kernel as PIE Date: Fri, 28 Apr 2023 17:51:15 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The kernel is currently build with mcmode=3Dkernel option which forces it to stay on the top 2G of the virtual address space. For PIE, use -fPIE option to build the kernel as a Position Independent Executable (PIE), which uses RIP-relative addressing and could be able to move below the top 2G. The --emit-relocs linker option was kept instead of using -pie to limit the impact on mapped sections. Any incompatible relocation will be catch by the objtool at compile time. Suggested-by: Lai Jiangshan Signed-off-by: Hou Wenlong Cc: Thomas Garnier Cc: Kees Cook --- arch/x86/Kconfig | 8 ++++++-- arch/x86/Makefile | 9 +++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 5ac5f335855e..9f8020991184 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2222,10 +2222,14 @@ config RELOCATABLE (CONFIG_PHYSICAL_START) is used as the minimum location. =20 config X86_PIE - def_bool n - depends on X86_64 + bool "Build a PIE kernel" + default n + depends on X86_64 && !XEN select OBJTOOL if HAVE_OBJTOOL select DYNAMIC_FTRACE if FUNCTION_TRACER && RETPOLINE + help + This builds a PIE kernel image that could be put at any + virtual address. =20 config RANDOMIZE_BASE bool "Randomize the address of the kernel image (KASLR)" diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 81500011396d..6631974e2003 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -160,10 +160,15 @@ else KBUILD_CFLAGS +=3D -mno-red-zone =20 ifdef CONFIG_X86_PIE - PIE_CFLAGS :=3D -include $(srctree)/include/linux/hidden.h + PIE_CFLAGS :=3D -fPIE -include $(srctree)/include/linux/hidden.h KBUILD_CFLAGS +=3D $(PIE_CFLAGS) -endif + # Relax relocation in both CFLAGS and LDFLAGS to support older com= pilers + KBUILD_CFLAGS +=3D $(call cc-option,-Wa$(comma)-mrelax-relocations= =3Dno) + LDFLAGS_vmlinux +=3D $(call ld-option,--no-relax) + KBUILD_LDFLAGS_MODULE +=3D $(call ld-option,--no-relax) +else KBUILD_CFLAGS +=3D -mcmodel=3Dkernel +endif KBUILD_RUSTFLAGS +=3D -Cno-redzone=3Dy KBUILD_RUSTFLAGS +=3D -Ccode-model=3Dkernel =20 --=20 2.31.1