Linux cryptographic layer development
 help / color / mirror / Atom feed
From: Dylan Hatch <dylanbhatch@google.com>
To: Catalin Marinas <catalin.marinas@arm.com>,
	Will Deacon <will@kernel.org>,
	 Mark Rutland <mark.rutland@arm.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>,
	"David S . Miller" <davem@davemloft.net>,
	 Nathan Chancellor <nathan@kernel.org>,
	Nicolas Schier <nsc@kernel.org>,
	 Nick Desaulniers <ndesaulniers@google.com>,
	Bill Wendling <morbo@google.com>,
	 Justin Stitt <justinstitt@google.com>,
	Weinan Liu <wnliu@google.com>,  Jens Remus <jremus@linux.ibm.com>,
	linux-arm-kernel@lists.infradead.org,
	 linux-kernel@vger.kernel.org, linux-kbuild@vger.kernel.org,
	 linux-crypto@vger.kernel.org, llvm@lists.linux.dev,
	 Josh Poimboeuf <jpoimboe@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	 Ard Biesheuvel <ardb@kernel.org>,
	Indu Bhagat <ibhagatgnu@gmail.com>,
	 Prasanna Kumar T S M <ptsm@linux.microsoft.com>,
	live-patching@vger.kernel.org,
	 Dylan Hatch <dylanbhatch@google.com>
Subject: [RFC PATCH 01/12] arm64, unwind: build with .eh_frame and add gen_lr_unwind skeleton
Date: Tue, 17 Nov 2026 04:34:53 +0000	[thread overview]
Message-ID: <20261117043504.1021063-2-dylanbhatch@google.com> (raw)
In-Reply-To: <20261117043504.1021063-1-dylanbhatch@google.com>

Add CONFIG_ARM64_LR_UNWIND to build the kernel with .eh_frame unwind
tables while discarding .eh_frame in the final vmlinux and module linker
scripts unless CONFIG_UNWIND_PATCH_PAC_INTO_SCS is enabled. Wire up a
skeleton scripts/gen_lr_unwind host tool invoked on vmlinux.o.

Signed-off-by: Dylan Hatch <dylanbhatch@google.com>
---
 MAINTAINERS                         |   1 +
 arch/arm64/Kconfig                  |  11 +++
 arch/arm64/include/asm/module.lds.h |   2 +-
 arch/arm64/kernel/vmlinux.lds.S     |  25 ++++-
 scripts/.gitignore                  |   1 +
 scripts/Makefile                    |   2 +
 scripts/Makefile.vmlinux_o          |   8 +-
 scripts/gen_lr_unwind.c             | 137 ++++++++++++++++++++++++++++
 scripts/link-vmlinux.sh             |   2 +-
 9 files changed, 185 insertions(+), 4 deletions(-)
 create mode 100644 scripts/gen_lr_unwind.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315065d..c7c8673291702 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4047,6 +4047,7 @@ F:	include/linux/arm-rmi-cmds.h
 F:	include/linux/arm-rsi-cmds.h
 F:	include/linux/arm-smccc-rmi.h
 F:	include/linux/arm-smccc-rsi.h
+F:	scripts/gen_lr_unwind.c
 F:	tools/testing/selftests/arm64/
 X:	arch/arm64/boot/dts/
 X:	arch/arm64/configs/defconfig
diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig
index 1075bc6618330..aa09c7ea61be3 100644
--- a/arch/arm64/Kconfig
+++ b/arch/arm64/Kconfig
@@ -2506,6 +2506,17 @@ config UNWIND_PATCH_PAC_INTO_SCS
 	select UNWIND_TABLES
 	select DYNAMIC_SCS
 
+config ARM64_LR_UNWIND
+	bool "Generate exception-boundary unwind tables from .eh_frame"
+	default y
+	select UNWIND_TABLES
+	help
+	  Generate compact .lr_unwind_fde and .lr_unwind_ranges sections at
+	  build time from DWARF .eh_frame unwind information to determine how
+	  to unwind across exception boundaries (whether the return address is
+	  live in x30, saved in the frame record at [x29], or saved in the
+	  frame record at an SP-relative offset).
+
 config ARM64_CONTPTE
 	bool "Contiguous PTE mappings for user memory" if EXPERT
 	depends on TRANSPARENT_HUGEPAGE
diff --git a/arch/arm64/include/asm/module.lds.h b/arch/arm64/include/asm/module.lds.h
index 603f92a8a73ed..84e5fa0250435 100644
--- a/arch/arm64/include/asm/module.lds.h
+++ b/arch/arm64/include/asm/module.lds.h
@@ -4,7 +4,7 @@ SECTIONS {
 	.text.ftrace_trampoline 0 : { BYTE(0) }
 	.init.text.ftrace_trampoline 0 : { BYTE(0) }
 
-#ifdef CONFIG_UNWIND_TABLES
+#ifdef CONFIG_UNWIND_PATCH_PAC_INTO_SCS
 	/*
 	 * Currently, we only use unwind info at module load time, so we can
 	 * put it into the .init allocation.
diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.lds.S
index 1869b44ae1909..b8f2963d10030 100644
--- a/arch/arm64/kernel/vmlinux.lds.S
+++ b/arch/arm64/kernel/vmlinux.lds.S
@@ -144,15 +144,35 @@ jiffies = jiffies_64;
 #define TRAMP_TEXT
 #endif
 
-#ifdef CONFIG_UNWIND_TABLES
+#ifdef CONFIG_UNWIND_PATCH_PAC_INTO_SCS
 #define UNWIND_DATA_SECTIONS				\
 	.eh_frame : {					\
 		__pi___eh_frame_start = .;		\
 		*(.eh_frame)				\
 		__pi___eh_frame_end = .;		\
 	}
+#define EH_FRAME_DISCARDS
 #else
 #define UNWIND_DATA_SECTIONS
+#define EH_FRAME_DISCARDS	*(.eh_frame)
+#endif
+
+#ifdef CONFIG_ARM64_LR_UNWIND
+#define LR_UNWIND_SECTIONS				\
+	. = ALIGN(4);					\
+	.lr_unwind_fde : {				\
+		__start_lr_unwind_fde = .;		\
+		KEEP(*(.lr_unwind_fde))			\
+		__stop_lr_unwind_fde = .;		\
+	}						\
+	. = ALIGN(2);					\
+	.lr_unwind_ranges : {				\
+		__start_lr_unwind_ranges = .;		\
+		KEEP(*(.lr_unwind_ranges))		\
+		__stop_lr_unwind_ranges = .;		\
+	}
+#else
+#define LR_UNWIND_SECTIONS
 #endif
 
 /*
@@ -184,6 +204,7 @@ SECTIONS
 		*(.interp .dynamic)
 		*(.dynsym .dynstr .hash .gnu.hash)
 		*(.ARM.attributes)
+		EH_FRAME_DISCARDS
 	}
 
 	. = KIMAGE_VADDR;
@@ -212,6 +233,8 @@ SECTIONS
 	/* everything from this point to __init_begin will be marked RO NX */
 	RO_DATA(PAGE_SIZE)
 
+	LR_UNWIND_SECTIONS
+
 	HYPERVISOR_RODATA_SECTIONS
 
 #ifdef CONFIG_NVHE_EL2_TRACING
diff --git a/scripts/.gitignore b/scripts/.gitignore
index 7004565439107..a56a49fee309c 100644
--- a/scripts/.gitignore
+++ b/scripts/.gitignore
@@ -1,6 +1,7 @@
 # SPDX-License-Identifier: GPL-2.0-only
 /asn1_compiler
 /gen_init_cpio
+/gen_lr_unwind
 /gen_packed_field_checks
 /generate_rust_target
 /insert-sys-cert
diff --git a/scripts/Makefile b/scripts/Makefile
index d7fbb7ca098b1..cb5af9339e4ac 100644
--- a/scripts/Makefile
+++ b/scripts/Makefile
@@ -12,6 +12,7 @@ hostprogs-always-$(CONFIG_SYSTEM_EXTRA_CERTIFICATE)	+= insert-sys-cert
 hostprogs-always-$(CONFIG_RUST_KERNEL_DOCTESTS)		+= rustdoc_test_builder
 hostprogs-always-$(CONFIG_RUST_KERNEL_DOCTESTS)		+= rustdoc_test_gen
 hostprogs-always-$(CONFIG_TRACEPOINTS)			+= tracepoint-update
+hostprogs-always-$(CONFIG_ARM64_LR_UNWIND)		+= gen_lr_unwind
 
 kallsyms-objs := kallsyms.o kallsyms-sysmap.o elf-parse.o
 sorttable-objs := sorttable.o elf-parse.o
@@ -31,6 +32,7 @@ rustdoc_test_builder-rust := y
 rustdoc_test_gen-rust := y
 
 HOSTCFLAGS_tracepoint-update.o = -I$(srctree)/tools/include
+HOSTCFLAGS_gen_lr_unwind.o = -I$(srctree)/tools/include
 HOSTCFLAGS_kallsyms.o = -I$(srctree)/tools/include
 HOSTCFLAGS_kallsyms-sysmap.o = -I$(srctree)/tools/include
 HOSTCFLAGS_elf-parse.o = -I$(srctree)/tools/include
diff --git a/scripts/Makefile.vmlinux_o b/scripts/Makefile.vmlinux_o
index 0b37bd5dc3a49..362382ec58b15 100644
--- a/scripts/Makefile.vmlinux_o
+++ b/scripts/Makefile.vmlinux_o
@@ -57,6 +57,10 @@ objtool-args = $(vmlinux-objtool-args-y) --link
 
 vmlinux-o-ld-args-$(CONFIG_BUILTIN_MODULE_RANGES)	+= -Map=$@.map
 
+gen_lr_unwind := $(objtree)/scripts/gen_lr_unwind
+cmd_gen_lr_unwind = $(if $(CONFIG_ARM64_LR_UNWIND), ; $(gen_lr_unwind) $@)
+cmd_gen_lr_unwinddep = $(if $(CONFIG_ARM64_LR_UNWIND), { echo ; echo '$@: $$(wildcard $(gen_lr_unwind))' ; } >> $(dot-target).cmd)
+
 quiet_cmd_ld_vmlinux.o = LD      $@
       cmd_ld_vmlinux.o = \
 	$(LD) ${KBUILD_LDFLAGS} -r -o $@ \
@@ -64,13 +68,15 @@ quiet_cmd_ld_vmlinux.o = LD      $@
 	$(addprefix -T , $(initcalls-lds)) \
 	--whole-archive vmlinux.a --no-whole-archive \
 	--start-group $(KBUILD_VMLINUX_LIBS) --end-group \
-	$(cmd_objtool)
+	$(cmd_objtool) \
+	$(cmd_gen_lr_unwind)
 
 cmd_check_function_names = $(srctree)/scripts/check-function-names.sh $@
 
 define rule_ld_vmlinux.o
 	$(call cmd_and_savecmd,ld_vmlinux.o)
 	$(call cmd,gen_objtooldep)
+	$(call cmd,gen_lr_unwinddep)
 	$(call cmd,check_function_names)
 endef
 
diff --git a/scripts/gen_lr_unwind.c b/scripts/gen_lr_unwind.c
new file mode 100644
index 0000000000000..1b52662838653
--- /dev/null
+++ b/scripts/gen_lr_unwind.c
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Generate .lr_unwind_fde and .lr_unwind_ranges sections from .eh_frame
+ * for arm64 exception-boundary unwinding.
+ */
+
+#include <elf.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+struct lr_unwind_ctx {
+	const void	*map;
+	size_t		map_size;
+
+	const Elf64_Shdr *eh_sec;
+	const Elf64_Shdr *rela_eh_sec;
+	const Elf64_Shdr *fde_sec;
+	unsigned int	symtab_shndx;
+};
+
+static int parse_eh_frame(struct lr_unwind_ctx *ctx)
+{
+	/* TODO: Parse CIEs/FDEs from .eh_frame and .rela.eh_frame */
+	(void)ctx;
+	return 0;
+}
+
+static int emit_lr_unwind_sections(const char *fname, struct lr_unwind_ctx *ctx,
+				   unsigned int orig_shnum,
+				   unsigned int shstrndx)
+{
+	/* TODO: Emit .lr_unwind_fde, .rela.lr_unwind_fde, and .lr_unwind_ranges */
+	(void)fname;
+	(void)ctx;
+	(void)orig_shnum;
+	(void)shstrndx;
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	struct lr_unwind_ctx ctx = { 0 };
+	unsigned int shnum, shstrndx;
+	const Elf64_Ehdr *ehdr;
+	const Elf64_Shdr *shdrs;
+	const char *shstrtab;
+	struct stat sb;
+	int fd, ret = 1;
+
+	if (argc != 2) {
+		fprintf(stderr, "Usage: %s <file>\n", argv[0]);
+		return 1;
+	}
+
+	fd = open(argv[1], O_RDONLY);
+	if (fd < 0) {
+		fprintf(stderr, "gen_lr_unwind: cannot open %s: %s\n",
+			argv[1], strerror(errno));
+		return 1;
+	}
+	if (fstat(fd, &sb) < 0 || (size_t)sb.st_size < sizeof(Elf64_Ehdr)) {
+		fprintf(stderr, "gen_lr_unwind: invalid file %s\n", argv[1]);
+		close(fd);
+		return 1;
+	}
+
+	ctx.map_size = sb.st_size;
+	ctx.map = mmap(NULL, ctx.map_size, PROT_READ, MAP_PRIVATE, fd, 0);
+	close(fd);
+	if (ctx.map == MAP_FAILED) {
+		fprintf(stderr, "gen_lr_unwind: mmap failed on %s: %s\n",
+			argv[1], strerror(errno));
+		return 1;
+	}
+
+	ehdr = ctx.map;
+	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
+	    ehdr->e_ident[EI_CLASS] != ELFCLASS64 ||
+	    ehdr->e_ident[EI_DATA] != ELFDATA2LSB ||
+	    ehdr->e_type != ET_REL ||
+	    ehdr->e_machine != EM_AARCH64) {
+		fprintf(stderr, "gen_lr_unwind: %s is not an AArch64 ET_REL ELF\n",
+			argv[1]);
+		goto out;
+	}
+
+	shdrs = ctx.map + ehdr->e_shoff;
+	shnum = ehdr->e_shnum == SHN_UNDEF ? shdrs[0].sh_size : ehdr->e_shnum;
+	shstrndx = ehdr->e_shstrndx == SHN_XINDEX ?
+		   shdrs[0].sh_link : ehdr->e_shstrndx;
+	shstrtab = ctx.map + shdrs[shstrndx].sh_offset;
+
+	for (unsigned int i = 0; i < shnum; i++) {
+		const char *name = shstrtab + shdrs[i].sh_name;
+
+		if (!strcmp(name, ".lr_unwind_fde"))
+			ctx.fde_sec = &shdrs[i];
+		else if (!strcmp(name, ".eh_frame") ||
+			 !strcmp(name, ".init.eh_frame"))
+			ctx.eh_sec = &shdrs[i];
+		else if (!strcmp(name, ".rela.eh_frame") ||
+			 !strcmp(name, ".rela.init.eh_frame"))
+			ctx.rela_eh_sec = &shdrs[i];
+		else if (!strcmp(name, ".symtab"))
+			ctx.symtab_shndx = i;
+	}
+
+	if (ctx.fde_sec) {
+		ret = 0;
+		goto out;
+	}
+
+	if (!ctx.eh_sec || !ctx.rela_eh_sec || !ctx.symtab_shndx) {
+		ret = 0;
+		goto out;
+	}
+
+	if (!parse_eh_frame(&ctx) &&
+	    !emit_lr_unwind_sections(argv[1], &ctx, shnum, shstrndx))
+		ret = 0;
+	else
+		fprintf(stderr, "gen_lr_unwind: failed to generate .lr_unwind for %s\n",
+			argv[1]);
+
+out:
+	if (ctx.map)
+		munmap((void *)ctx.map, ctx.map_size);
+	return ret;
+}
diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh
index d5710e900bd40..9c8bfb6ff05e0 100755
--- a/scripts/link-vmlinux.sh
+++ b/scripts/link-vmlinux.sh
@@ -61,7 +61,7 @@ vmlinux_link()
 	shift
 
 	if is_enabled CONFIG_LTO_CLANG || is_enabled CONFIG_X86_KERNEL_IBT ||
-	   is_enabled CONFIG_KLP_BUILD; then
+	   is_enabled CONFIG_KLP_BUILD || is_enabled CONFIG_ARM64_LR_UNWIND; then
 		# Use vmlinux.o instead of performing the slow LTO link again.
 		objs=vmlinux.o
 		libs=
-- 
2.56.0.679.gc527c65306-goog


  reply	other threads:[~2026-11-17  4:35 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17  4:34 [RFC PATCH 00/12] arm64, unwind: add .lr_unwind unwinder for reliable stacktrace Dylan Hatch
2026-11-17  4:34 ` Dylan Hatch [this message]
2026-11-17  4:34 ` [RFC PATCH 02/12] arm64: factor .eh_frame parser out of patch-scs.c into <asm/eh_frame.h> Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 03/12] arm64: eh_frame: track CFA and x29/x30 register state in CFI parser Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 04/12] arm64, unwind: parse .eh_frame into .lr_unwind entries in gen_lr_unwind Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 05/12] scripts/gen_lr_unwind: emit .lr_unwind_fde and .lr_unwind_ranges Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 06/12] scripts/gen_lr_unwind: add --dump option to inspect .lr_unwind tables Dylan Hatch
2026-11-17  4:34 ` [RFC PATCH 07/12] scripts/sorttable: sort .lr_unwind_fde by start_offset Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 08/12] arm64, unwind: add in-kernel .lr_unwind lookup and validation Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 09/12] arm64/module, unwind: add .lr_unwind support for modules Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 10/12] arm64: entry: add unwind info for call_on_irq_stack() Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 11/12] arm64, crypto/lib: annotate leaf functions with CFI info Dylan Hatch
2026-11-17  4:35 ` [RFC PATCH 12/12] unwind: arm64: use .lr_unwind to unwind interrupt frames Dylan Hatch

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117043504.1021063-2-dylanbhatch@google.com \
    --to=dylanbhatch@google.com \
    --cc=ardb@kernel.org \
    --cc=catalin.marinas@arm.com \
    --cc=davem@davemloft.net \
    --cc=herbert@gondor.apana.org.au \
    --cc=ibhagatgnu@gmail.com \
    --cc=jpoimboe@kernel.org \
    --cc=jremus@linux.ibm.com \
    --cc=justinstitt@google.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-crypto@vger.kernel.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=mark.rutland@arm.com \
    --cc=morbo@google.com \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=nsc@kernel.org \
    --cc=ptsm@linux.microsoft.com \
    --cc=rostedt@goodmis.org \
    --cc=will@kernel.org \
    --cc=wnliu@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox