mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@kernel.org>
To: x86@kernel.org
Cc: linux-kernel@vger.kernel.org,
	Peter Zijlstra <peterz@infradead.org>,
	Gary Guo <gary@garyguo.net>,
	rust-for-linux@vger.kernel.org, Ard Biesheuvel <ardb@kernel.org>,
	Miguel Ojeda <ojeda@kernel.org>,
	Nathan Chancellor <nathan@kernel.org>,
	Nicolas Schier <nsc@kernel.org>,
	linux-kbuild@vger.kernel.org, Huacai Chen <chenhuacai@kernel.org>
Subject: [PATCH v3 22/28] objtool: Add options to write/read exported noreturns to/from a file
Date: Mon, 21 Sep 2026 15:31:23 -0700	[thread overview]
Message-ID: <ee9a89f3c8990d2af8f5f8345680a1223e1639f0.1790028654.git.jpoimboe@kernel.org> (raw)
In-Reply-To: <cover.1790028654.git.jpoimboe@kernel.org>

Add --noreturns-write which can be used to write vmlinux's exported
noreturns to a file, and --noreturn-read which can be used to read them
when running objtool on a module.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
 tools/objtool/builtin-check.c           |  12 ++
 tools/objtool/check.c                   | 143 +++++++++++++++++++++++-
 tools/objtool/include/objtool/builtin.h |   2 +
 tools/objtool/include/objtool/elf.h     |   1 +
 4 files changed, 157 insertions(+), 1 deletion(-)

diff --git a/tools/objtool/builtin-check.c b/tools/objtool/builtin-check.c
index 75b11dc85010e..c4c4210f84662 100644
--- a/tools/objtool/builtin-check.c
+++ b/tools/objtool/builtin-check.c
@@ -101,6 +101,8 @@ static const struct option check_options[] = {
 	OPT_BOOLEAN(0,		 "module", &opts.module, "object is part of a kernel module"),
 	OPT_BOOLEAN(0,		 "mnop", &opts.mnop, "nop out mcount call sites"),
 	OPT_BOOLEAN(0,		 "no-unreachable", &opts.no_unreachable, "skip 'unreachable instruction' warnings"),
+	OPT_STRING(0,		 "noreturns-read", &opts.noreturns_read, "file", "read exported noreturns from a file"),
+	OPT_STRING(0,		 "noreturns-write", &opts.noreturns_write, "file", "write exported noreturns to a file"),
 	OPT_STRING('o',		 "output", &opts.output, "file", "output file name"),
 	OPT_BOOLEAN(0,		 "sec-address", &opts.sec_address, "print section addresses in warnings"),
 	OPT_BOOLEAN(0,		 "stats", &opts.stats, "print statistics"),
@@ -180,6 +182,16 @@ static bool opts_valid(void)
 		return false;
 	}
 
+	if (opts.noreturns_write && !opts.link) {
+		ERROR("--noreturns-write requires --link");
+		return false;
+	}
+
+	if (opts.noreturns_write && !opts.stackval && !opts.orc && !opts.uaccess) {
+		ERROR("--noreturns-write requires --stackval, --orc, or --uaccess");
+		return false;
+	}
+
 	if (opts.disas			||
 	    opts.hack_jump_label	||
 	    opts.hack_noinstr		||
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 41df5f608535f..29ef2dc678833 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -341,6 +341,120 @@ static void detect_noreturns(struct objtool_file *file)
 	}
 }
 
+static bool noreturns_changed(const char *buf, size_t len)
+{
+	bool changed = true;
+	char *old = NULL;
+	FILE *fp;
+
+	fp = fopen(opts.noreturns_write, "r");
+	if (!fp)
+		return true;
+
+	old = malloc(len + 1);
+	if (!old)
+		goto out;
+
+	if (fread(old, 1, len + 1, fp) != len)
+		goto out;
+
+	if (memcmp(old, buf, len))
+		goto out;
+
+	changed = false;
+
+out:
+	free(old);
+	fclose(fp);
+	return changed;
+}
+
+/*
+ * This is called for vmlinux.o.  Write the vmlinux.o noreturn list to a file
+ * so it can be read in module objtool runs by read_noreturns().
+ */
+static int write_noreturns(struct objtool_file *file)
+{
+	struct symbol *func;
+	char *buf, *pos;
+	size_t len = 0;
+	FILE *fp;
+
+	for_each_sym(file->elf, func) {
+		if (is_noreturn(func) && func->exported)
+			len += strlen(func->name) + 1;
+	}
+
+	buf = malloc(len + 1);
+	if (!buf) {
+		ERROR_GLIBC("malloc");
+		return -1;
+	}
+
+	pos = buf;
+	for_each_sym(file->elf, func) {
+		if (is_noreturn(func) && func->exported)
+			pos += sprintf(pos, "%s\n", func->name);
+	}
+
+	/*
+	 * Only write the file when the contents have change to avoid relinking
+	 * all the modules unnecessarily.
+	 */
+	if (!noreturns_changed(buf, len)) {
+		free(buf);
+		return 0;
+	}
+
+	fp = fopen(opts.noreturns_write, "w");
+	if (!fp) {
+		ERROR_GLIBC("fopen");
+		return -1;
+	}
+
+	if (fwrite(buf, 1, len, fp) != len) {
+		ERROR_GLIBC("fwrite");
+		return -1;
+	}
+
+	free(buf);
+
+	if (fclose(fp)) {
+		ERROR_GLIBC("fclose");
+		return -1;
+	}
+
+	return 0;
+}
+
+/*
+ * This is called for modules.  Read the noreturn list generated by the
+ * vmlinux.o pass and mark the corresponding undefined symbols noreturn.
+ */
+static int read_noreturns(struct objtool_file *file)
+{
+	char line[SYM_NAME_LEN];
+	struct symbol *func;
+	FILE *fp;
+
+	fp = fopen(opts.noreturns_read, "r");
+	if (!fp) {
+		ERROR("can't open '%s'", opts.noreturns_read);
+		return -1;
+	}
+
+	while (fgets(line, sizeof(line), fp)) {
+		line[strcspn(line, "\n")] = '\0';
+
+		func = find_global_symbol_by_name(file->elf, line);
+		if (func && is_undef_sym(func))
+			func->_noreturn = 1;
+	}
+
+	fclose(fp);
+	return 0;
+}
+
 static void init_cfi_state(struct cfi_state *cfi)
 {
 	int i;
@@ -2238,6 +2352,19 @@ static int add_jump_table_alts(struct objtool_file *file)
 	return 0;
 }
 
+static void read_exports(struct objtool_file *file)
+{
+	struct section *sec;
+	struct reloc *reloc;
+
+	sec = find_section_by_name(file->elf, ".export_symbol");
+	if (!sec || !sec->rsec)
+		return;
+
+	for_each_reloc(sec->rsec, reloc)
+		reloc->sym->exported = 1;
+}
+
 static void set_func_state(struct cfi_state *state)
 {
 	state->cfa = initial_func_cfi.cfa;
@@ -2722,8 +2849,10 @@ int decode_file(struct objtool_file *file)
 	 * both validate_functions() and validate_noinstr_sections() -- and by
 	 * validate_unret().
 	 */
-	if (validate_branch_enabled() || opts.noinstr || opts.unret)
+	if (validate_branch_enabled() || opts.noinstr || opts.unret) {
+		read_exports(file);
 		detect_noreturns(file);
+	}
 
 	if (read_unwind_hints(file))
 		return -1;
@@ -4889,6 +5018,12 @@ int check(struct objtool_file *file)
 		objtool_disas_ctx = disas_ctx;
 	}
 
+	if (opts.noreturns_read) {
+		ret = read_noreturns(file);
+		if (ret)
+			goto out;
+	}
+
 	ret = decode_file(file);
 	if (ret)
 		goto out;
@@ -4996,6 +5131,12 @@ int check(struct objtool_file *file)
 			goto out;
 	}
 
+	if (opts.noreturns_write && !opts.dryrun) {
+		ret = write_noreturns(file);
+		if (ret)
+			goto out;
+	}
+
 	if (opts.stats) {
 		printf("nr_insns_visited: %ld\n", nr_insns_visited);
 		printf("nr_cfi: %ld\n", nr_cfi);
diff --git a/tools/objtool/include/objtool/builtin.h b/tools/objtool/include/objtool/builtin.h
index 349690bb1c50e..fb33d57a54c95 100644
--- a/tools/objtool/include/objtool/builtin.h
+++ b/tools/objtool/include/objtool/builtin.h
@@ -41,6 +41,8 @@ struct opts {
 	bool mnop;
 	bool module;
 	bool no_unreachable;
+	const char *noreturns_read;
+	const char *noreturns_write;
 	const char *output;
 	bool sec_address;
 	bool stats;
diff --git a/tools/objtool/include/objtool/elf.h b/tools/objtool/include/objtool/elf.h
index 0e3593d993ac8..adf6e1322c6df 100644
--- a/tools/objtool/include/objtool/elf.h
+++ b/tools/objtool/include/objtool/elf.h
@@ -100,6 +100,7 @@ struct symbol {
 	u8 fake		     : 1;
 	u8 _noreturn         : 1;
 	u8 ignore_noreturn   : 1;
+	u8 exported          : 1;
 	struct list_head pv_target;
 	struct reloc *relocs;
 	struct section *group_sec;
-- 
2.55.0


  parent reply	other threads:[~2026-09-21 22:31 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-21 22:31 [PATCH v3 00/28] objtool: dynamically detect noreturns Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 01/28] objtool: Remove obsolete noreturns.h entries Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 02/28] kbuild: Add CONFIG_OBJTOOL_DEFERRED Josh Poimboeuf
2026-09-24 11:39   ` Nicolas Schier
2026-09-21 22:31 ` [PATCH v3 03/28] kbuild: Add CONFIG_OBJTOOL_CONTROL_FLOW Josh Poimboeuf
2026-09-24 11:40   ` Nicolas Schier
2026-09-21 22:31 ` [PATCH v3 04/28] objtool: Fix dead end detection for sibling calls Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 05/28] objtool: Refactor the noreturn/dead-end detection Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 06/28] objtool: Ignore traps after noreturn calls in STT_CODE Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 07/28] objtool: Make .discard.stack_frame_non_standard non-allocatable Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 08/28] efi/libstub: Drop .discard.addressable from the stub objects Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 09/28] efi/loongarch: Mark loongarch efi_boot_kernel() non-standard for objtool Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 10/28] objtool: Add ANNOTATE_IGNORE_NORETURN() Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 11/28] LoongArch: Annotate reboot and kexec paths as returnable Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 12/28] kbuild: Defer running objtool to link time for all CFG features Josh Poimboeuf
2026-09-24 11:48   ` Nicolas Schier
2026-09-21 22:31 ` [PATCH v3 13/28] rust: Annotate the intrinsic stubs as returnable Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 14/28] panic: Mark abort() __noreturn Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 15/28] x86/xen: Ignore noreturn status of weak mem_map_via_hcall() Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 16/28] objtool: Detect noreturns in weak functions Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 17/28] x86/entry: Make rewind_stack_and_make_dead() a real function Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 18/28] x86/xen: Make xen_cpu_bringup_again() " Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 19/28] x86/xen: Make xen_start_kernel() noreturn Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 20/28] x86/boot: Rework how pi startup symbols get exposed to vmlinux Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 21/28] objtool: Fix noreturn detection for non-sibling jumps to SYM_CODE Josh Poimboeuf
2026-09-21 22:31 ` Josh Poimboeuf [this message]
2026-09-21 22:31 ` [PATCH v3 23/28] kbuild: Do the per-module objtool pass right before linking Josh Poimboeuf
2026-09-24 11:22   ` Nicolas Schier
2026-09-21 22:31 ` [PATCH v3 24/28] kbuild: Generate the noreturn list and validate modules against it Josh Poimboeuf
2026-09-24 12:18   ` Nicolas Schier
2026-09-21 22:31 ` [PATCH v3 25/28] objtool: Add ANNOTATE_EXPORTED_NORETURN() Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 26/28] objtool: Annotate all module-exported noreturns and remove noreturns.h Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 27/28] objtool: Warn about missing/stale ANNOTATE_EXPORTED_NORETURN() usage Josh Poimboeuf
2026-09-21 22:31 ` [PATCH v3 28/28] fixup! objtool: Add options to write/read exported noreturns to/from a file Josh Poimboeuf

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=ee9a89f3c8990d2af8f5f8345680a1223e1639f0.1790028654.git.jpoimboe@kernel.org \
    --to=jpoimboe@kernel.org \
    --cc=ardb@kernel.org \
    --cc=chenhuacai@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=nsc@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=x86@kernel.org \
    /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

all inboxes | Powered by JetHome®