From nobody Fri Apr 10 08:06:49 2026 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 22D3D24EF6E for ; Mon, 24 Feb 2025 13:21:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740403318; cv=none; b=qeNtOR+Pl02cP0KHjBDXCJaWGYkn3X06RFUjqekD4+5gTMYDG8wvw4jUfJhFAO9U0csBjxHMEN/YUYIjAr58ijoHVjU0knsPSbAy1UQUq8AjLBbSccZBaLdS2Zae110rFRhJ4Al+lLXoi9C8tywZJW+vqdnY4q+lOvl6DO0CNKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740403318; c=relaxed/simple; bh=4JX+l3Lgbl+ZEiHLxUH/SYwEcP5mDlrM6p749Og3ZPk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Dope1tBRUzxZz4kNnJYawY7qXTpAvzNmfJFip90o9ghdlfNQzHV9m7zgdaDx61wrjG2SMctH3UWeq+l+vW2NCqiIAmWUMFYh3+rzD35y+yAqF7xhfse7RVwjzfdV5+9bN8x3C0ceWtIuy2lj2iMBbCgAKMUG3So2Ve5OUnom4kE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=EkhPRMmM; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--ardb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="EkhPRMmM" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-38f27acb979so4443305f8f.0 for ; Mon, 24 Feb 2025 05:21:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1740403314; x=1741008114; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PgCkMehe7p3mehBNoMC8WFS/X7xwPD6PvbsrOWzFSqw=; b=EkhPRMmMsv2b8KIlYCEMfe3BnsX7f6FEXFUIkivxxq8ZWpiYW5nF5o4A1PMy3/WWzY IrCjH9Iw7yRVh+mgpvHlv/TgK5mTq1KP5bG7HWfMqUHQ/tC9IuhwdgYa0C/Z3ZYmAzhm zWhHNUvqNBuqZPJuGAilmQbI04Hotw1yBquiw+BnU4JogsuN/i/IEaEqJGviNoQxygm4 7WrnLdf4zS4rQK9/iEQvpPUlof67imQeSuYDg2/wu+pxzH/UmQki4t79I32qSOuvuknW HfbXh2qmKxUoCG4sgp+WeDr1njBeekHMMeApwpkp6X/phK3MpanSWtZmbEGHG/+/uxYi TZ1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740403314; x=1741008114; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PgCkMehe7p3mehBNoMC8WFS/X7xwPD6PvbsrOWzFSqw=; b=wJmAtnL3JUqtn6xyxhA1P9LFPlCnl1RCpid+xkWIR4kKb+Dt5qS0cBqh6tTncegXz2 xOl0mT2xB7gDh6/cv6pSVY7O/Q6YPf/CHHek/tNITNB6gWCzo0W7q73CgEEqoGxHxZXy lPuCd8+GOIfP82EtIceFn01pywIrsCCW6km66O05S9qrnR+ecFA4vKdAmpWwl9fl1jKg UormimdTYXWjOnjN1MkKyKMZjBEizHCtgzHpy8EUh43hClYmnau+LwoLVxs6z9KdfI3c 2mpwmuqW4ZD31HvjNtHdgVmHUdbj6eX23Kbd2otQMaIw7fxJRSeRuiuVu/b7jrxjhK5n Ibgg== X-Gm-Message-State: AOJu0Yz5whyd1aCHJNrfzRzUad7rU8cbI1JJIExfiznEuReRrtic3oXb wsl4l7HfwxAy2PSRx+kNOg/GEq/k/OX9iXeKnI1xvYFe79cy50+QDe93CClPo9yRm1HNTATRtcw lNepjbVDUmOZbxQ0UZLFvM285Eb8SlkVRZahF5BrvjBnGVmy6dFZ9QZvvJF4Q967BC7Q/zcN5uM 0VE+oZTCQmzGp1CQvESG0XJXfnNI+TQg== X-Google-Smtp-Source: AGHT+IEyH2XmBvCzg2ijOwcjrIaoCy2e3N1FgVWpa9pBlwZWSeWw3cgu90bvQVoFAh+RzYhSFVTp0joX X-Received: from wmbep9.prod.google.com ([2002:a05:600c:8409:b0:439:65f0:b039]) (user=ardb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:154a:b0:38f:50fd:55a8 with SMTP id ffacd0b85a97d-38f70783eeamr9304935f8f.6.1740403314458; Mon, 24 Feb 2025 05:21:54 -0800 (PST) Date: Mon, 24 Feb 2025 14:21:35 +0100 In-Reply-To: <20250224132132.1765115-6-ardb+git@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250224132132.1765115-6-ardb+git@google.com> X-Developer-Key: i=ardb@kernel.org; a=openpgp; fpr=F43D03328115A198C90016883D200E9CA6329909 X-Developer-Signature: v=1; a=openpgp-sha256; l=5903; i=ardb@kernel.org; h=from:subject; bh=l0XPdsqmrGbazu6TBBLLKaLD6j8WwrwleHZjQt9SLHk=; b=owGbwMvMwCFmkMcZplerG8N4Wi2JIX1PUcLlhUx3xDe9fZG2eUvuX8l1N7u/t61Zu09hbvnm5 VkJE6bM7ihlYRDjYJAVU2QRmP333c7TE6VqnWfJwsxhZQIZwsDFKQATEd3OyLBD/LmTf+jMnyLa wnnnIkwtXQ9dPf8+9m3Mn5nJX5ZPWx/N8L/006XoyoLw1k/r1p669uDYOd9KFY5fBTtbvYLnrdq kz8AEAA== X-Mailer: git-send-email 2.48.1.601.g30ceb7b040-goog Message-ID: <20250224132132.1765115-8-ardb+git@google.com> Subject: [RFC PATCH 2/4] Kbuild: Introduce Kconfig symbol for linking vmlinux with relocations From: Ard Biesheuvel To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, linux-kbuild@vger.kernel.org, Ard Biesheuvel , Masahiro Yamada , Ingo Molnar Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Ard Biesheuvel Some architectures build vmlinux with static relocations preserved, but strip them again from the final vmlinux image. Arch specific tools consume these static relocations in order to construct relocation tables for KASLR. The fact that vmlinux is created, consumed and subsequently updated goes against the typical, declarative paradigm used by Make, which is based on rules and dependencies. So as a first step towards cleaning this up, introduce a Kconfig symbol to declare that the arch wants to consume the static relocations emitted into vmlinux. This will be wired up further in subsequent patches. Signed-off-by: Ard Biesheuvel --- Makefile | 4 ++++ arch/Kconfig | 7 +++++++ arch/mips/Kconfig | 1 + arch/mips/Makefile | 4 ---- arch/riscv/Kconfig | 1 + arch/riscv/Makefile | 2 +- arch/s390/Kconfig | 1 + arch/s390/Makefile | 2 +- arch/x86/Kconfig | 1 + arch/x86/Makefile | 6 ------ 10 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 30dab4c8b012..a3302dce56de 100644 --- a/Makefile +++ b/Makefile @@ -1119,6 +1119,10 @@ ifdef CONFIG_LD_ORPHAN_WARN LDFLAGS_vmlinux +=3D --orphan-handling=3D$(CONFIG_LD_ORPHAN_WARN_LEVEL) endif =20 +ifneq ($(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS),) +LDFLAGS_vmlinux +=3D --emit-relocs --discard-none +endif + # Align the bit size of userspace programs with the kernel KBUILD_USERCFLAGS +=3D $(filter -m32 -m64 --target=3D%, $(KBUILD_CPPFLAGS= ) $(KBUILD_CFLAGS)) KBUILD_USERLDFLAGS +=3D $(filter -m32 -m64 --target=3D%, $(KBUILD_CPPFLAGS= ) $(KBUILD_CFLAGS)) diff --git a/arch/Kconfig b/arch/Kconfig index b8a4ff365582..101a13fcde8e 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -1695,6 +1695,13 @@ config ARCH_HAS_KERNEL_FPU_SUPPORT Architectures that select this option can run floating-point code in the kernel, as described in Documentation/core-api/floating-point.rst. =20 +config ARCH_VMLINUX_NEEDS_RELOCS + bool + help + Whether the architecture needs vmlinux to be built with static + relocations preserved. This is used by some architectures to + construct bespoke relocation tables for KASLR. + source "kernel/gcov/Kconfig" =20 source "scripts/gcc-plugins/Kconfig" diff --git a/arch/mips/Kconfig b/arch/mips/Kconfig index 1924f2d83932..5aedbd7afadb 100644 --- a/arch/mips/Kconfig +++ b/arch/mips/Kconfig @@ -2617,6 +2617,7 @@ config RELOCATABLE CPU_MIPS32_R6 || CPU_MIPS64_R6 || \ CPU_P5600 || CAVIUM_OCTEON_SOC || \ CPU_LOONGSON64 + select ARCH_VMLINUX_NEEDS_RELOCS help This builds a kernel image that retains relocation information so it can be loaded someplace besides the default 1MB. diff --git a/arch/mips/Makefile b/arch/mips/Makefile index be8cb44a89fd..d9057e29bc62 100644 --- a/arch/mips/Makefile +++ b/arch/mips/Makefile @@ -100,10 +100,6 @@ LDFLAGS_vmlinux +=3D -G 0 -static -n -nostdlib KBUILD_AFLAGS_MODULE +=3D -mlong-calls KBUILD_CFLAGS_MODULE +=3D -mlong-calls =20 -ifeq ($(CONFIG_RELOCATABLE),y) -LDFLAGS_vmlinux +=3D --emit-relocs -endif - cflags-y +=3D -ffreestanding =20 cflags-$(CONFIG_CPU_BIG_ENDIAN) +=3D -EB diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig index 7612c52e9b1e..6f5800114416 100644 --- a/arch/riscv/Kconfig +++ b/arch/riscv/Kconfig @@ -1077,6 +1077,7 @@ config RELOCATABLE bool "Build a relocatable kernel" depends on MMU && 64BIT && !XIP_KERNEL select MODULE_SECTIONS if MODULES + select ARCH_VMLINUX_NEEDS_RELOCS help This builds a kernel as a Position Independent Executable (PIE), which retains all relocation metadata required to relocate the diff --git a/arch/riscv/Makefile b/arch/riscv/Makefile index 13fbc0f94238..6ef0d10e0c50 100644 --- a/arch/riscv/Makefile +++ b/arch/riscv/Makefile @@ -8,7 +8,7 @@ =20 LDFLAGS_vmlinux :=3D -z norelro ifeq ($(CONFIG_RELOCATABLE),y) - LDFLAGS_vmlinux +=3D -shared -Bsymbolic -z notext --emit-relocs + LDFLAGS_vmlinux +=3D -shared -Bsymbolic -z notext KBUILD_CFLAGS +=3D -fPIE endif ifeq ($(CONFIG_DYNAMIC_FTRACE),y) diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig index 9c9ec08d78c7..ea67b7317138 100644 --- a/arch/s390/Kconfig +++ b/arch/s390/Kconfig @@ -630,6 +630,7 @@ endchoice =20 config RELOCATABLE def_bool y + select ARCH_VMLINUX_NEEDS_RELOCS help This builds a kernel image that retains relocation information so it can be loaded at an arbitrary address. diff --git a/arch/s390/Makefile b/arch/s390/Makefile index 5fae311203c2..d5f4be440879 100644 --- a/arch/s390/Makefile +++ b/arch/s390/Makefile @@ -15,7 +15,7 @@ KBUILD_CFLAGS_MODULE +=3D -fPIC KBUILD_AFLAGS +=3D -m64 KBUILD_CFLAGS +=3D -m64 KBUILD_CFLAGS +=3D -fPIC -LDFLAGS_vmlinux :=3D -no-pie --emit-relocs --discard-none +LDFLAGS_vmlinux :=3D -no-pie extra_tools :=3D relocs aflags_dwarf :=3D -Wa,-gdwarf-2 KBUILD_AFLAGS_DECOMPRESSOR :=3D $(CLANG_FLAGS) -m64 -D__ASSEMBLY__ diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index be2c311f5118..2005d80ff8d1 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2199,6 +2199,7 @@ config RANDOMIZE_BASE config X86_NEED_RELOCS def_bool y depends on RANDOMIZE_BASE || (X86_32 && RELOCATABLE) + select ARCH_VMLINUX_NEEDS_RELOCS =20 config PHYSICAL_ALIGN hex "Alignment value to which kernel should be aligned" diff --git a/arch/x86/Makefile b/arch/x86/Makefile index 5b773b34768d..f65ed6dcd6fb 100644 --- a/arch/x86/Makefile +++ b/arch/x86/Makefile @@ -251,12 +251,6 @@ endif =20 KBUILD_LDFLAGS +=3D -m elf_$(UTS_MACHINE) =20 -ifdef CONFIG_X86_NEED_RELOCS -LDFLAGS_vmlinux :=3D --emit-relocs --discard-none -else -LDFLAGS_vmlinux :=3D -endif - # # The 64-bit kernel must be aligned to 2MB. Pass -z max-page-size=3D0x200= 000 to # the linker to force 2MB page size regardless of the default page size us= ed --=20 2.48.1.601.g30ceb7b040-goog