Skip to contentSkip to conversation

objtool: Fixup alternate feature relative addresses

PATCH In review Shivang Upadhyay 2 participants 14 messages Sep 29 – Oct 6

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover objtool: Fixup alternate feature relative addresses

This patch series implements build-time fixup of alternate feature relative addresses for powerpc.

Previously, Nicholas Piggin proposed a build-time solution using a custom PowerPC tool [1], which provided the foundation for this approach. The current implementation leverages objtool's existing ELF parsing infrastructure to do the same.

This patchset applies atop powerpc/merge branch.

[1] Original PowerPC tool approach:

    http://patchwork.ozlabs.org/project/linuxppc-dev/patch/[email protected]/

Testing:
Build and Boot tested on ppc64le, ppc64be, and ppc32be configs.

Sathvika Vasireddy (6):
objtool/powerpc: Add build-time fixup of alternate feature branch
targets
objtool: Set ELF_F_LAYOUT flag to preserve vmlinux segment layout
objtool: Fix "can't find starting instruction" warnings on vmlinux objtool/powerpc: Skip jump destination analysis and unnanotated

    intra-function call warnings for --ftr-fixup

kbuild: Add objtool integration for PowerPC feature fixups
powerpc: Enable build-time feature fixup processing by default

--
Changes since v1: https://lore.kernel.org/all/[email protected]/
- rebase to upstream master
- 1/6: in arch_decode_instruction() added comment describing opcode 16.
- 1/6: in arch_decode_instruction() added missing logic to add AA flag to offset.

Changes since RFC-v4: https://lore.kernel.org/all/[email protected]/
- 4/6: In the instruction validation logic, update insn_call_dest() handling to allow unannotated intra-function calls when opts.ftr_fixup is enabled, and continue processing the instruction.
- 3/6: In the function processing logic, skip zero-length symbols before performing function boundary validation, as suggested by Sashiko.
- 3/6: In the function processing loop, add func_off to track the absolute/local offset and replace sym_for_each_insn() with an explicit loop.
- 1/6: In process_alt_data(), skip files that do not use opt.link.
- 1/6: In process_fixup_entries(), handle realloc() failure and return
-1` when allocation fails.
- 1/6: In check_and_flatten_fixup_entries(), remove dead code that redundantly resets the fixup entry parent.
- 1/6: In is_local_symbol(), fix the local-symbol detection logic and add
a comment explaining the new check.
- 1/6: In process_alt_relocations(), check that ftr_alt->data and ftr->data->d_buf are initialized before accessing them.
- 1/6: Remove redundant line deletions and perform minor cleanup.

Changes in V4:
- consolidate emit-relocs remove-section logic for CONFIG_ARCH_VMLINUX_NEEDS_RELOCS and CONFIG_HAVE_OBJTOOL_FTR_FIXUP configs as suggested by Nathan Chancellor.

Changes in V3:
- Split code into individual patches
- Put objtool call into cmd_link_vmlinux
- Move HAVE_OBJTOOL_FEATURE_FIXUP from arch/Kconfig to arch/powerpc/Kconfig

Changes in V2:
- Added implementation support for ppc64 LE, BE, ppc32 BE.

---

 Makefile                                  |   7 +
 arch/powerpc/Kconfig                      |   3 +
 arch/powerpc/Makefile                     |   5 +
 arch/powerpc/include/asm/feature-fixups.h |   2 +-
 arch/powerpc/kernel/vmlinux.lds.S         |   8 +-
 arch/powerpc/lib/feature-fixups.c         |  12 -
 scripts/Makefile.lib                      |   4 +-
 scripts/Makefile.vmlinux                  |  11 +-
 tools/objtool/arch/powerpc/decode.c       |  11 +
 tools/objtool/arch/powerpc/special.c      | 451 ++++++++++++++++++++++
 tools/objtool/builtin-check.c             |   2 +
 tools/objtool/check.c                     |  55 ++-
 tools/objtool/elf.c                       |   3 +
 tools/objtool/include/objtool/builtin.h   |   1 +
 tools/objtool/include/objtool/special.h   |  56 +++
 tools/objtool/special.c                   |  29 ++
 16 files changed, 632 insertions(+), 28 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/6 objtool/powerpc: Add build-time fixup of alternate feature branch targets

From: Sathvika Vasireddy <[email protected]>

PowerPC __ftr_alt* sections must currently be placed near .text because they lack the executable attribute, preventing the linker from inserting stubs. Branches in the alternate code must reach their targets directly. This can cause build failures as the kernel grows.

Fix this by processing alternate feature relocations at build time with objtool. Link with --emit-relocs to preserve relocation information, mark __ftr_alt* sections executable ("ax") so the linker can insert stubs, then use objtool --ftr-fixup to recompute branch offsets based on their runtime locations.

objtool already has ELF parsing and instruction patching infrastructure, so this avoids introducing a separate tool as was originally proposed [1].

[1]
https://lore.kernel.org/linuxppc-dev/[email protected]/

---
 tools/objtool/arch/powerpc/decode.c     |  11 +
 tools/objtool/arch/powerpc/special.c    | 451 ++++++++++++++++++++++++
 tools/objtool/builtin-check.c           |   2 +
 tools/objtool/check.c                   |  25 ++
 tools/objtool/include/objtool/builtin.h |   1 +
 tools/objtool/include/objtool/special.h |  56 +++
 tools/objtool/special.c                 |  29 ++
 7 files changed, 575 insertions(+)
tools/objtool/arch/powerpc/decode.c +11 -0
index e534ac1123b3..5fdd142f0a19 100644
@@ -66,6 +66,17 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec
 	imm = 0;
 
 	switch (opcode) {
+	case 16: /* bc[l][a] : Branch Conditional */
+		if (ins & 1)
+			typ = INSN_OTHER;
+		else
+			typ = INSN_JUMP_CONDITIONAL;
+		imm = ins & 0xfffc;
+		if (imm & 0x8000)
+			imm -= 0x10000;
+		imm |= ins & 2;	/* AA flag */
+		break;
+
 	case 18: /* b[l][a] */
 		if (ins == 0x48000005)	/* bl .+4 */
 			typ = INSN_OTHER;
tools/objtool/arch/powerpc/special.c +451 -0

461 lines not on this page · raw message

tools/objtool/builtin-check.c +2 -0
index 75b11dc85010..ed7fdd82d997 100644
@@ -77,6 +77,7 @@ static const struct option check_options[] = {
 	OPT_CALLBACK_OPTARG('h', "hacks", NULL, NULL, "jump_label,noinstr,skylake", "patch toolchain bugs/limitations", parse_hacks),
 	OPT_BOOLEAN('i',	 "ibt", &opts.ibt, "validate and annotate IBT"),
 	OPT_BOOLEAN(0,		 "klp-symids", &opts.klp_symids, "generate .klp.symids for duplicate symbol disambiguation"),
+	OPT_BOOLEAN('f',	 "ftr-fixup", &opts.ftr_fixup, "feature fixup"),
 	OPT_BOOLEAN('m',	 "mcount", &opts.mcount, "annotate mcount/fentry calls for ftrace"),
 	OPT_BOOLEAN(0,		 "noabs", &opts.noabs, "reject absolute references in allocatable sections"),
 	OPT_BOOLEAN('n',	 "noinstr", &opts.noinstr, "validate noinstr rules"),
@@ -183,6 +184,7 @@ static bool opts_valid(void)
 	if (opts.disas			||
 	    opts.hack_jump_label	||
 	    opts.hack_noinstr		||
+	    opts.ftr_fixup		||
 	    opts.ibt			||
 	    opts.klp_symids		||
 	    opts.mcount			||
tools/objtool/check.c +25 -0
index 464f6c9d9ff0..2e875ac9175e 100644
@@ -4840,6 +4840,31 @@ int check(struct objtool_file *file)
 	if (!nr_insns)
 		goto out;
 
+	if (opts.ftr_fixup) {
+		ret = process_alt_data(file);
+		if (ret < 0)
+			return ret;
+
+		ret = process_fixup_entries(file);
+		if (ret < 0)
+			return ret;
+
+		check_and_flatten_fixup_entries();
+
+		ret = process_exception_entries(file);
+		if (ret < 0)
+			return ret;
+
+		ret = process_bug_entries(file);
+		if (ret < 0)
+			return ret;
+
+		ret = process_alt_relocations(file);
+		if (ret < 0)
+			return ret;
+	}
+
+
 	if (opts.retpoline)
 		warnings += validate_retpoline(file);
 
tools/objtool/include/objtool/builtin.h +1 -0
index 349690bb1c50..159255cecc27 100644
@@ -13,6 +13,7 @@ struct opts {
 	const char *disas;
 	bool dump_orc;
 	bool hack_jump_label;
+	bool ftr_fixup;
 	bool hack_noinstr;
 	bool hack_skylake;
 	bool ibt;
tools/objtool/include/objtool/special.h +56 -0

72 lines not on this page · raw message

tools/objtool/special.c +29 -0
index 2a533afbc69a..43b60bf789dd 100644
@@ -167,3 +167,32 @@ int special_get_alts(struct elf *elf, struct list_head *alts)
 
 	return 0;
 }
+
+int __weak process_alt_data(struct objtool_file *file)
+{
+	return 0;
+}
+
+int __weak process_fixup_entries(struct objtool_file *file)
+{
+	return 0;
+}
+
+void __weak check_and_flatten_fixup_entries(void)
+{
+}
+
+int __weak process_exception_entries(struct objtool_file *file)
+{
+	return 0;
+}
+
+int __weak process_bug_entries(struct objtool_file *file)
+{
+	return 0;
+}
+
+int __weak process_alt_relocations(struct objtool_file *file)
+{
+	return 0;
+}
Signature · 1 line
2.54.0
#7 lore raw

PATCH v2 4/6 objtool/powerpc: Skip jump destination analysis and unnanotated intra-function call warnings for --ftr-fixup

In reply to Shivang Upadhyay

From: Sathvika Vasireddy <[email protected]>

Objtool is throwing unannotated intra-function call warnings when run on vmlinux with --ftr-fixup option.

One such example:

vmlinux: warning: objtool: .text+0x3d94:
unannotated intra-function call

.text + 0x3d94 = c000000000008000 + 3d94 = c0000000000081d4

c0000000000081d4: 45 24 02 48 bl c00000000002a618 <system_reset_exception+0x8>

c00000000002a610 <system_reset_exception>:
c00000000002a610: 0e 01 4c 3c addis r2,r12,270

                        c00000000002a610: R_PPC64_REL16_HA    .TOC.
c00000000002a614:       f0 6c 42 38     addi    r2,r2,27888
                        c00000000002a614: R_PPC64_REL16_LO    .TOC.+0x4

c00000000002a618: a6 02 08 7c mflr r0

This is happening because we should be looking for destination symbols that are at absolute offsets instead of relative offsets. After fixing dest_off to point to absolute offset, there are still a lot of these warnings shown.

In the above example, objtool is computing the destination offset to be c00000000002a618, which points to a completely different instruction. find_call_destination() is looking for this offset and failing. Instead, we should be looking for destination offset c00000000002a610 which points to system_reset_exception function.

Even after fixing the way destination offset is computed, and after looking for dest_off - 0x8 in cases where the original offset is not found, there are still a lot of unannotated intra-function call warnings generated. This is due to symbols that are not properly annotated.

So, for now, as a hack to curb these warnings, do not emit unannotated intra-function call warnings when objtool is run with --ftr-fixup option.

Skip add_jump_destinations() and suppress intra-function call errors in --ftr-fixup mode. The feature fixup pass only needs fixup entry tables, relocation entries from .__ftr_alternates.text, and elf_write_insn() to patch branch offsets.

---
 tools/objtool/check.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
tools/objtool/check.c +9 -4
index 7662e990cb13..907dbc8f9b71 100644
@@ -1693,8 +1693,11 @@ static int add_call_destinations(struct objtool_file *file)
 				continue;
 
 			if (!insn_call_dest(insn)) {
-				ERROR_INSN(insn, "unannotated intra-function call");
-				return -1;
+				if (!opts.ftr_fixup) {
+					ERROR_INSN(insn, "unannotated intra-function call");
+					return -1;
+				}
+				continue;
 			}
 
 			if (func && !is_func_sym(insn_call_dest(insn))) {
@@ -2661,8 +2664,10 @@ int decode_file(struct objtool_file *file)
 			return -1;
 	}
 
-	if (add_jump_destinations(file))
-		return -1;
+	if (!opts.ftr_fixup) {
+		if (add_jump_destinations(file))
+			return -1;
+	}
 
 	/*
 	 * Must be before add_call_destination(); it changes INSN_CALL to
Signature · 1 line
2.54.0
#2 lore raw

PATCH v2 2/6 objtool: Set ELF_F_LAYOUT flag to preserve vmlinux segment layout

In reply to Shivang Upadhyay

From: Sathvika Vasireddy <[email protected]>

When objtool writes changes back to vmlinux with --ftr-fixup --link, libelf recalculates the file layout and inserts padding between sections. This corrupts the ELF segment structure, causing kexec to fail with:

ELF Note corrupted !
Cannot determine the file type of vmlinux

This happens because libelf's default behavior assumes it can freely rearrange section offsets when writing, which breaks the carefully constructed vmlinux layout produced by the linker.

Set ELF_F_LAYOUT after elf_begin() to instruct libelf that the application is responsible for the file layout. This prevents libelf from inserting padding or repositioning sections while still allowing data modifications to be written back.

---
 tools/objtool/elf.c | 3 +++
 1 file changed, 3 insertions(+)
tools/objtool/elf.c +3 -0
index a791f4ea6ec1..55c20a993f5d 100644
@@ -1239,6 +1239,9 @@ struct elf *elf_open_read(const char *name, int flags)
 		goto err;
 	}
 
+	if (opts.ftr_fixup)
+		elf_flagelf(elf->elf, ELF_C_SET, ELF_F_LAYOUT);
+
 	if (!gelf_getehdr(elf->elf, &elf->ehdr)) {
 		ERROR_ELF("gelf_getehdr");
 		goto err;
Signature · 1 line
2.54.0
#4 lore raw

PATCH v2 3/6 objtool: Fix "can't find starting instruction" warnings on vmlinux

In reply to Shivang Upadhyay

From: Sathvika Vasireddy <[email protected]>

Objtool throws a lot of can't find starting instruction warnings when run on vmlinux with --ftr-fixup option.

These warnings are seen because find_insn() function looks for instructions at offsets that are relative to the start of the section. In case of individual object files (.o), there are no can't find starting instruction warnings seen because the actual offset associated with an instruction is itself a relative offset since the sections start at offset 0x0.

However, in case of vmlinux, find_insn() function fails to find instructions at the actual offset associated with an instruction since the sections in vmlinux do not start at offset 0x0. Due to this, find_insn() will look for absolute offset and not the relative offset. This is resulting in a lot of can't find starting instruction warnings when objtool is run on vmlinux.

To fix this, pass offset that is relative to the start of the section to find_insn().

find_insn() is also looking for symbols of size 0. But, objtool does not store empty STT_NOTYPE symbols in the rbtree. Due to this, for empty symbols, objtool is throwing can't find starting instruction warnings. Fix this by ignoring symbols that are of size 0 since objtool does not add them to the rbtree.

---
 tools/objtool/check.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
tools/objtool/check.c +13 -4
index 2e875ac9175e..7662e990cb13 100644
@@ -411,7 +411,7 @@ static int decode_instructions(struct objtool_file *file)
 {
 	struct section *sec;
 	struct symbol *func;
-	unsigned long offset;
+	unsigned long offset, func_off;
 	struct instruction *insn;
 
 	for_each_sec(file->elf, sec) {
@@ -493,17 +493,26 @@ static int decode_instructions(struct objtool_file *file)
 			if (func->embedded_insn || is_alias_sym(func))
 				continue;
 
-			if (!find_insn(file, sec, func->offset)) {
+			if (func->len == 0 && is_notype_sym(func))
+				continue;
+
+			func_off = opts.ftr_fixup ?
+				func->offset - sec->sh.sh_addr : func->offset;
+
+			if (!find_insn(file, sec, func_off)) {
 				ERROR("%s(): can't find starting instruction", func->name);
 				return -1;
 			}
 
-			sym_for_each_insn(file, func, insn) {
+			for (insn = find_insn(file, sec, func_off);
+				insn && insn->offset < func_off + func->len;
+				insn = next_insn_same_sec(file, insn)) {
 				insn->_sym = func;
+
 				if (is_func_sym(func) &&
 				    insn->type == INSN_ENDBR &&
 				    list_empty(&insn->call_node)) {
-					if (insn->offset == func->offset) {
+					if (insn->offset == func_off) {
 						list_add_tail(&insn->call_node, &file->endbr_list);
 						file->nr_endbr++;
 					} else {
Signature · 1 line
2.54.0
#12 lore raw

PATCH v2 5/6 kbuild: Add objtool integration for PowerPC feature fixups

In reply to Shivang Upadhyay

From: Sathvika Vasireddy <[email protected]>

Add build system support for PowerPC feature fixup processing:

- Add HAVE_OBJTOOL_FTR_FIXUP config option for architectures that support
build-time feature fixup processing
- Integrate objtool feature fixup processing into vmlinux build

---
 Makefile                 | 7 +++++++
 scripts/Makefile.lib     | 4 ++--
 scripts/Makefile.vmlinux | 3 ++-
 3 files changed, 11 insertions(+), 3 deletions(-)
Makefile +7 -0
index 6b8b812f23b9..363cad32fb26 100644
@@ -1566,6 +1566,13 @@ ifdef CONFIG_OBJTOOL
 prepare: tools/objtool
 endif
 
+# CONFIG_OBJTOOL and CONFIG_HAVE_OBJTOOL_FTR_FIXUP are unrelated, separate
+# options. It was integrated in objtool in order to borrow the elf parser,
+# but this is different from how the other objtool commands are used.
+ifdef CONFIG_HAVE_OBJTOOL_FTR_FIXUP
+prepare: tools/objtool
+endif
+
 ifdef CONFIG_BPF
 ifdef CONFIG_DEBUG_INFO_BTF
 prepare: tools/bpf/resolve_btfids
scripts/Makefile.lib +2 -2
index 0a4fdd8bd975..efcedeb207b7 100644
@@ -182,10 +182,10 @@ cpp_flags      = -Wp,-MMD,$(depfile) $(NOSTDINC_FLAGS) $(LINUXINCLUDE)     \
 
 ld_flags       = $(KBUILD_LDFLAGS) $(ldflags-y) $(LDFLAGS_$(@F))
 
-ifdef CONFIG_OBJTOOL
-
 objtool := $(objtree)/tools/objtool/objtool
 
+ifdef CONFIG_OBJTOOL
+
 objtool-args-$(CONFIG_HAVE_JUMP_LABEL_HACK)		+= --hacks=jump_label
 objtool-args-$(CONFIG_HAVE_NOINSTR_HACK)		+= --hacks=noinstr
 objtool-args-$(CONFIG_MITIGATION_CALL_DEPTH_TRACKING)	+= --hacks=skylake
scripts/Makefile.vmlinux +2 -1
index fcae1e432d9a..a1bef0638ecb 100644
@@ -65,7 +65,8 @@ ARCH_POSTLINK := $(wildcard $(srctree)/arch/$(SRCARCH)/Makefile.postlink)
 # Final link of vmlinux with optional arch pass after final link
 cmd_link_vmlinux =							\
 	$< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)" "$@";	\
-	$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true)
+	$(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true);	\
+	$(if $(CONFIG_HAVE_OBJTOOL_FTR_FIXUP), $(objtool) --ftr-fixup --link $@, true)
 
 targets += vmlinux.unstripped .vmlinux.export.o
 vmlinux.unstripped: scripts/link-vmlinux.sh vmlinux.o .vmlinux.export.o $(KBUILD_LDS) FORCE
Signature · 1 line
2.54.0
#13 lore raw

PATCH v2 6/6 powerpc: Enable build-time feature fixup processing by default

In reply to Shivang Upadhyay

From: Sathvika Vasireddy <[email protected]>

Enable HAVE_OBJTOOL_FTR_FIXUP by default on PowerPC architecture.

- Remove runtime branch translation logic from patch_alt_instruction()
- Add --emit-relocs linker flags for post-link fixup processing
- Update ftr_alt section attributes to include executable flag
- Strip the --emit-relocs relocation sections (.rel*) from the final vmlinux after processing

---
 arch/powerpc/Kconfig                      |  3 +++
 arch/powerpc/Makefile                     |  5 +++++
 arch/powerpc/include/asm/feature-fixups.h |  2 +-
 arch/powerpc/kernel/vmlinux.lds.S         |  8 ++++++--
 arch/powerpc/lib/feature-fixups.c         | 12 ------------
 scripts/Makefile.vmlinux                  |  8 ++++++--
 6 files changed, 21 insertions(+), 17 deletions(-)
arch/powerpc/Kconfig +3 -0
index 2580e27e4328..d9056f35da9f 100644
@@ -23,6 +23,9 @@ config 64BIT
 	bool
 	default y if PPC64
 
+config HAVE_OBJTOOL_FTR_FIXUP
+        def_bool y
+
 config LIVEPATCH_64
 	def_bool PPC64
 	depends on LIVEPATCH
arch/powerpc/Makefile +5 -0
index 9385db478c59..269ed76a40c6 100644
@@ -112,6 +112,11 @@ LDFLAGS_vmlinux-$(CONFIG_RELOCATABLE) := -pie --no-dynamic-linker
 LDFLAGS_vmlinux-$(CONFIG_RELOCATABLE) += -z notext
 LDFLAGS_vmlinux	:= $(LDFLAGS_vmlinux-y)
 
+# --emit-relocs required for post-link fixup of alternate feature
+# text section relocations.
+LDFLAGS_vmlinux        += --emit-relocs
+KBUILD_LDFLAGS_MODULE += --emit-relocs
+
 ifdef CONFIG_PPC64
 ifndef CONFIG_PPC_KERNEL_PCREL
 	# -mcmodel=medium breaks modules because it uses 32bit offsets from
arch/powerpc/include/asm/feature-fixups.h +1 -1
index 756a6c694018..d6ae92a292ec 100644
@@ -32,7 +32,7 @@
 
 #define FTR_SECTION_ELSE_NESTED(label)			\
 label##2:						\
-	.pushsection __ftr_alt_##label,"a";		\
+	.pushsection __ftr_alt_##label, "ax";		\
 	.align 2;					\
 label##3:
 
arch/powerpc/kernel/vmlinux.lds.S +6 -2
index 8fc11d6565bf..1a2d7c2d32f1 100644
@@ -99,8 +99,8 @@ SECTIONS
 	.text : AT(ADDR(.text) - LOAD_OFFSET) {
 		ALIGN_FUNCTION();
 #endif
-		/* careful! __ftr_alt_* sections need to be close to .text */
-		*(.text.hot .text.hot.* TEXT_MAIN .text.fixup .text.unlikely .text.unlikely.* .fixup __ftr_alt_* .ref.text);
+		*(.text.hot .text.hot.* TEXT_MAIN .text.fixup .text.unlikely
+			.text.unlikely.* .fixup .ref.text);
 		*(.tramp.ftrace.text);
 		NOINSTR_TEXT
 		SCHED_TEXT
@@ -267,6 +267,10 @@ SECTIONS
 		_einittext = .;
 	} :text
 
+	.__ftr_alternates.text : AT(ADDR(.__ftr_alternates.text) - LOAD_OFFSET) {
+		*(__ftr_alt*);
+	}
+
 	/* .exit.text is discarded at runtime, not link time,
 	 * to deal with references from __bug_table
 	 */
arch/powerpc/lib/feature-fixups.c +0 -12
index 587c8cf1230f..269e992b1631 100644
@@ -53,22 +53,10 @@ static u32 *calc_addr(struct fixup_entry *fcur, long offset)
 
 static int patch_alt_instruction(u32 *src, u32 *dest, u32 *alt_start, u32 *alt_end)
 {
-	int err;
 	ppc_inst_t instr;
 
 	instr = ppc_inst_read(src);
 
-	if (instr_is_relative_branch(ppc_inst_read(src))) {
-		u32 *target = (u32 *)branch_target(src);
-
-		/* Branch within the section doesn't need translating */
-		if (target < alt_start || target > alt_end) {
-			err = translate_branch(&instr, dest, src);
-			if (err)
-				return 1;
-		}
-	}
-
 	raw_patch_instruction(dest, instr);
 
 	return 0;
scripts/Makefile.vmlinux +6 -2
index a1bef0638ecb..66e5d58a6ce8 100644
@@ -82,11 +82,15 @@ endif
 # vmlinux
 # ---------------------------------------------------------------------------
 
+# These configurations require vmlinux.unstripped to be linked with
+# '--emit-relocs', which need to be stripped from the final vmlinux.
+uses-emit-relocs := $(or $(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS),$(CONFIG_HAVE_OBJTOOL_FTR_FIXUP))
+
 remove-section-y                                   := .modinfo
-remove-section-$(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS) += '.rel*' '!.rel*.dyn'
+remove-section-$(uses-emit-relocs)                 += '.rel*' '!.rel*.dyn'
 # for compatibility with binutils < 2.32
 # https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=c12d9fa2afe7abcbe407a00e15719e1a1350c2a7
-remove-section-$(CONFIG_ARCH_VMLINUX_NEEDS_RELOCS) += '.rel.*'
+remove-section-$(uses-emit-relocs)                 += '.rel.*'
 
 remove-symbols := -w --strip-unneeded-symbol='__mod_device_table__*'
 
Signature · 1 line
2.54.0
maintainer
#5 lore raw

Re: [PATCH v2 3/6] objtool: Fix "can't find starting instruction" warnings on vmlinux

In reply to Shivang Upadhyay
Quoted 2 lines · Shivang Upadhyay

find_insn() is also looking for symbols of size 0. But, objtool does not store empty STT_NOTYPE symbols in the rbtree.

81cf39be3559 ("objtool: Add empty symbols to the symbol tree again")

maintainer
#6 lore raw

Re: [PATCH v2 3/6] objtool: Fix "can't find starting instruction" warnings on vmlinux

In reply to Shivang Upadhyay
Quoted 12 lines · Shivang Upadhyay

From: Sathvika Vasireddy <[email protected]>

Objtool throws a lot of can't find starting instruction warnings when run on vmlinux with --ftr-fixup option.

These warnings are seen because find_insn() function looks for instructions at offsets that are relative to the start of the section. In case of individual object files (.o), there are no can't find starting instruction warnings seen because the actual offset associated with an instruction is itself a relative offset since the sections start at offset 0x0.

Quoted 6 lines

However, in case of vmlinux, find_insn() function fails to find instructions at the actual offset associated with an instruction since the sections in vmlinux do not start at offset 0x0. Due to this, find_insn() will look for absolute offset and not the relative offset. This is resulting in a lot of can't find starting instruction warnings when objtool is run on vmlinux.

I'm confused; decode_instructions() iterates the section and inserts the instructions at the exact offset it finds them at. Therefore it should be able to find them again at that same offset, irrespective of the actual start, no?

tools/objtool/check.c static int decode_instructions(struct objtool_file *file) · line 500

Quoted 5 lines
tools/objtool/check.c +5 -0
+			if (func->len == 0 && is_notype_sym(func))
+				continue;
+
+			func_off = opts.ftr_fixup ?
+				func->offset - sec->sh.sh_addr : func->offset;

function offset really should not depend on opts. It should work irrespective of any option, no?

maintainer
#8 lore raw

Re: [PATCH v2 4/6] objtool/powerpc: Skip jump destination analysis and unnanotated intra-function call warnings for --ftr-fixup

In reply to Shivang Upadhyay
Quoted 17 lines · Shivang Upadhyay

From: Sathvika Vasireddy <[email protected]>

Objtool is throwing unannotated intra-function call warnings when run on vmlinux with --ftr-fixup option.

One such example:

vmlinux: warning: objtool: .text+0x3d94:
unannotated intra-function call

.text + 0x3d94 = c000000000008000 + 3d94 = c0000000000081d4

c0000000000081d4: 45 24 02 48 bl c00000000002a618 <system_reset_exception+0x8>

c00000000002a610 <system_reset_exception>:
c00000000002a610: 0e 01 4c 3c addis r2,r12,270

Quoted 7 lines
                        c00000000002a610: R_PPC64_REL16_HA    .TOC.
c00000000002a614:       f0 6c 42 38     addi    r2,r2,27888
                        c00000000002a614: R_PPC64_REL16_LO    .TOC.+0x4

c00000000002a618: a6 02 08 7c mflr r0

This is happening because we should be looking for destination symbols that are at absolute offsets instead of relative offsets.

Again, confused. We run objtool on objects, this is pre-linking, there are no absolute offsets.

#9 lore raw

Re: [PATCH v2 4/6] objtool/powerpc: Skip jump destination analysis and unnanotated intra-function call warnings for --ftr-fixup

Quoted 9 lines · Peter Zijlstra
Quoted 9 lines

c00000000002a610 <system_reset_exception>:
c00000000002a610:       0e 01 4c 3c     addis   r2,r12,270
                         c00000000002a610: R_PPC64_REL16_HA .TOC.
c00000000002a614:       f0 6c 42 38     addi    r2,r2,27888
                         c00000000002a614: R_PPC64_REL16_LO .TOC.+0x4
c00000000002a618:       a6 02 08 7c     mflr    r0

Quoted 6 lines
Quoted 2 lines

This is happening because we should be looking for destination symbols that are at absolute offsets instead of relative offsets.

Again, confused. We run objtool on objects, this is pre-linking, there
are no absolute offsets.

Hi Peter,

This is the special requirement of patching jump assembly instructions that we need to know where the jump is performed from/to. As per 5/6 objtool is run post link on vmlinux. If handling post-link information is outside the scope of objtool, please let me know.

Regards,
~Shivang.

maintainer
#10 lore raw

Re: [PATCH v2 4/6] objtool/powerpc: Skip jump destination analysis and unnanotated intra-function call warnings for --ftr-fixup

Quoted 16 lines · Shivang Upadhyay
Quoted 9 lines · Peter Zijlstra
Quoted 9 lines

c00000000002a610 <system_reset_exception>:
c00000000002a610:       0e 01 4c 3c     addis   r2,r12,270
                         c00000000002a610: R_PPC64_REL16_HA .TOC.
c00000000002a614:       f0 6c 42 38     addi    r2,r2,27888
                         c00000000002a614: R_PPC64_REL16_LO .TOC.+0x4
c00000000002a618:       a6 02 08 7c     mflr    r0

Quoted 6 lines
Quoted 2 lines

This is happening because we should be looking for destination symbols that are at absolute offsets instead of relative offsets.

Again, confused. We run objtool on objects, this is pre-linking, there
are no absolute offsets.

Quoted 6 lines

Hi Peter,

This is the special requirement of patching jump assembly instructions that we need to know where the jump is performed from/to. As per 5/6 objtool is run post link on vmlinux. If handling post-link information is outside the scope of objtool, please let me know.

I think we can make it work; but this wasn't immediately obvious. Also I'm still struggling to understand what exactly you're doing -- I'm not well versed in the PPC details and have no idea what this ftr thing really is and your patches don't really explain anything much at all.

(Gemini is suggesting this is all somewhat similar to the x86 RIP relative fixup we do for alternatives, but your case is more 'fun').

Anyway, I would much rather objtool learns how to deal with absolute sections rather than making things depend on a 'random' ftr option. That is, if we need conditional code, have it be because the secion has non-zero address, not because ftr option or somesuch.

But noting that, can't we simply change:

  decode_instructions()
    hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset));

to take sh_addr into account? I mean, when unlinked, it'll be 0 so nothing changes, but when already linked, it'll get you the absolute value and then you don't need that fixup later on, or am I missing some details?

So from where I'm at, please:

- teach objtool about absolute sections, not ftr specials

- expand changelog to actually explain what these ftr things are and how they work and what the actual problem is we're solving, so we don't need to employ LLMs to understand patches and all that :-)

Does that sound reasonable?

maintainer
#3 lore raw

Re: [PATCH v2 2/6] objtool: Set ELF_F_LAYOUT flag to preserve vmlinux segment layout

In reply to Shivang Upadhyay
Quoted 30 lines · Shivang Upadhyay

From: Sathvika Vasireddy <[email protected]>

When objtool writes changes back to vmlinux with --ftr-fixup --link, libelf recalculates the file layout and inserts padding between sections. This corrupts the ELF segment structure, causing kexec to fail with:

ELF Note corrupted !
Cannot determine the file type of vmlinux

This happens because libelf's default behavior assumes it can freely rearrange section offsets when writing, which breaks the carefully constructed vmlinux layout produced by the linker.

Set ELF_F_LAYOUT after elf_begin() to instruct libelf that the application is responsible for the file layout. This prevents libelf from inserting padding or repositioning sections while still allowing data modifications to be written back.

---
 tools/objtool/elf.c | 3 +++
 1 file changed, 3 insertions(+)
tools/objtool/elf.c +0 -0
index a791f4ea6ec1..55c20a993f5d 100644

tools/objtool/elf.c struct elf *elf_open_read(const char *name, int flags) · line 1243

Quoted 5 lines
tools/objtool/elf.c +2 -0
@@ -1239,6 +1239,9 @@ struct elf *elf_open_read(const char *name, int flags)
 		goto err;
 	}
 
+	if (opts.ftr_fixup)
+		elf_flagelf(elf->elf, ELF_C_SET, ELF_F_LAYOUT);

So per the previous email; you need this because this is a post-link pass. As such, anything that has absolute sections would need this. So should this be conditional on that, rather than ftr?

#11 lore raw

Re: [PATCH v2 4/6] objtool/powerpc: Skip jump destination analysis and unnanotated intra-function call warnings for --ftr-fixup

In reply to Peter Zijlstra
Quoted 35 lines · Peter Zijlstra
Quoted 19 lines · Shivang Upadhyay
Quoted 9 lines · Peter Zijlstra
Quoted 9 lines

c00000000002a610 <system_reset_exception>:
c00000000002a610:       0e 01 4c 3c     addis   r2,r12,270
                         c00000000002a610: R_PPC64_REL16_HA .TOC.
c00000000002a614:       f0 6c 42 38     addi    r2,r2,27888
                         c00000000002a614: R_PPC64_REL16_LO .TOC.+0x4
c00000000002a618:       a6 02 08 7c     mflr    r0

Quoted 7 lines
Quoted 3 lines

This is happening because we should be looking for destination symbols that are at absolute offsets instead of relative offsets.

Again, confused. We run objtool on objects, this is pre-linking, there
are no absolute offsets.

Hi Peter,

Quoted 7 lines

This is the special requirement of patching jump assembly instructions
that we need to know where the jump is performed from/to. As per 5/6
objtool is run post link on vmlinux. If handling post-link information
is outside the scope of objtool, please let me know.

I think we can make it work; but this wasn't immediately obvious. Also
I'm still struggling to understand what exactly you're doing -- I'm not
well versed in the PPC details and have no idea what this ftr thing really is and your patches don't really explain anything much at all.

Quoted 3 lines

(Gemini is suggesting this is all somewhat similar to the x86 RIP relative fixup we do for alternatives, but your case is more 'fun').

Hi Peter,
Sorry for the delayed response.

I think I can explain the feature fixup mechanism and the problem we're trying to solve more clearly in the cover letter.

Here is a basic example of what we're trying to achieve.

        GEN_COMMON data_access

            ld      r4,_DSISR(r1)
            addi    r3,r1,STACK_INT_FRAME_REGS
            andis.  r0,r4,DSISR_DABRMATCH@h
            bne-    1f

        #ifdef CONFIG_PPC_64S_HASH_MMU
        BEGIN_MMU_FTR_SECTION
            bl      CFUNC(do_hash_fault)
        MMU_FTR_SECTION_ELSE
            bl      CFUNC(do_page_fault)
        ALT_MMU_FTR_SECTION_END_IFCLR(MMU_FTR_TYPE_RADIX)
        #else
            bl      CFUNC(do_page_fault)
        #endif

            b       interrupt_return_srr

This is the implementation of the data_access exception handler on ppc64le. Depending on whether the machine is using the hash or radix MMU, we need to execute a different fault handler. The choice is made at runtime based on the CPU's feature bits.

The assembly macros therefore generate code for both possible cases at build time, and the feature-fixup mechanism later patches the code so that only the appropriate path is executed.

The problem is that the linker does not know about this later patching when it resolves branch relocations.
For example, consider:

feat_loc:

    ...

BEGIN_MMU_FTR_SECTION

    ...
loc_a:
    bl      CFUNC(main_feature)
    ...

MMU_FTR_SECTION_ELSE

    ...
loc_b:
    bl      CFUNC(alt_feature)
    ...

When linking this code, the linker will encode main_feature - loc_a at loc_a and alt_feature - loc_b at loc_b at two bl instructions ablove.

However, alt_feature - loc_b is not the offset that we ultimately need from the alt branch. When the feature fixup is applied, the instruction at loc_a is replaced with the alternative instruction, so the branch needs to encode (alt_feature - loc_a) jump.

This distinction matters because the branch displacement has to fit in the range representable by the 32-bit PowerPC branch instruction. Depending on where feat_loc, main_feature, and alt_feature end up after linking, the linker-generated displacement can therefore be different from the displacement that is actually needed after the feature fixup. In some layouts this can result in either a build-time failure or, more importantly, an invalid branch at runtime.

What we're trying to do with the build-time pass is detect these cases and prepare the branch relocations for the code as it will actually exist after feature patching. At minimum, it should allow us to detect an impossible branch offset at build time rather than discovering it only after the feature fixup is applied at runtime.

Quoted 18 lines

Anyway, I would much rather objtool learns how to deal with absolute sections rather than making things depend on a 'random' ftr option. That
is, if we need conditional code, have it be because the secion has non-zero address, not because ftr option or somesuch.

But noting that, can't we simply change:

  decode_instructions()
    hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn-

Quoted 1 line

offset));

to take sh_addr into account? I mean, when unlinked, it'll be 0 so nothing changes, but when already linked, it'll get you the absolute value and then you don't need that fixup later on, or am I missing some
details?

Quoted 3 lines

So from where I'm at, please:

 - teach objtool about absolute sections, not ftr specials

Sure I will try to rework these patches towards that.

Quoted 6 lines

 - expand changelog to actually explain what these ftr things are and how they work and what the actual problem is we're solving, so we don't need to employ LLMs to understand patches and all that :-)

Does that sound reasonable?

Yes.

Thanks
~Shivang.