objtool: Fixup alternate feature relative addresses
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
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 | 15 +- tools/objtool/arch/powerpc/special.c | 451 ++++++++++++++++++++++ tools/objtool/builtin-check.c | 2 + tools/objtool/check.c | 57 ++- tools/objtool/elf.c | 4 + tools/objtool/include/objtool/builtin.h | 1 + tools/objtool/include/objtool/special.h | 56 +++ tools/objtool/special.c | 29 ++ 16 files changed, 637 insertions(+), 30 deletions(-)
Signature · 1 line
2.43.0
PATCH 2/6 objtool: Set ELF_F_LAYOUT flag to preserve vmlinux segment layout
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
@@ -1162,6 +1162,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.43.0
PATCH 3/6 objtool: Fix "can't find starting instruction" warnings on vmlinux
In reply to Sathvika VasireddyObjtool 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 | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-)
tools/objtool/check.c +12 -4
@@ -412,7 +412,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) {
@@ -494,17 +494,25 @@ static int decode_instructions(struct objtool_file *file)if (func->embedded_insn || func->alias != 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.43.0
PATCH 1/6 objtool/powerpc: Add build-time fixup of alternate feature branch targets
In reply to Sathvika VasireddyPowerPC __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 | 15 +- tools/objtool/arch/powerpc/special.c | 451 ++++++++++++++++++++++++ tools/objtool/builtin-check.c | 2 + tools/objtool/check.c | 29 +- tools/objtool/elf.c | 1 + tools/objtool/include/objtool/builtin.h | 1 + tools/objtool/include/objtool/special.h | 56 +++ tools/objtool/special.c | 29 ++ 8 files changed, 582 insertions(+), 2 deletions(-)
tools/objtool/arch/powerpc/decode.c +14 -1
@@ -59,13 +59,26 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *secenum insn_type typ;unsigned long imm;u32 ins; +unsigned int aa; ins =bswap_if_needed (file->elf, *(u32 *)(sec->data->d_buf + offset)); opcode = ins >>26 ; typ =INSN_OTHER ; imm =0 ; + aa = ins &2 ;switch (opcode) { +case 16 : +if (ins &1 ) + typ =INSN_OTHER ; +else + typ =INSN_JUMP_CONDITIONAL ; + imm = ins &0xfffc ; +if (imm &0x8000 ) + imm -=0x10000 ; + insn->immediate = imm | aa; +break ; +case 18 :/* b[l][a] */ if (ins ==0x48000005 )/* bl .+4 */ typ =INSN_OTHER ;
@@ -77,7 +90,7 @@ int arch_decode_instruction(struct objtool_file *file, const struct section *sec imm = ins &0x3fffffc ;if (imm &0x2000000 ) imm -=0x4000000 ; - imm |= ins &2 ;/* AA flag */ + insn->immediate = imm | aa;break ; }
tools/objtool/arch/powerpc/special.c +451 -0
461 lines not on this page · raw message
tools/objtool/builtin-check.c +2 -0
@@ -78,6 +78,7 @@ static const struct option check_options[] = {OPT_STRING_OPTARG ('d' ,"disas" , &opts.disas,"function-pattern" ,"disassemble functions" ,"*" ),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 ('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"),
@@ -181,6 +182,7 @@ static bool opts_valid(void) opts.disas || opts.hack_jump_label || opts.hack_noinstr || + opts.ftr_fixup || opts.ibt || opts.mcount || opts.noabs ||
tools/objtool/check.c +28 -1
@@ -1636,9 +1636,11 @@ static int add_jump_destinations(struct objtool_file *file) dest_off == func->offset + func->len)continue ; +ERROR_INSN (insn,"can't find jump dest instruction at %s" , -offstr (dest_sec, dest_off)); +offstr (dest_sec, dest_off));return -1 ; + }if (!dest_sym ||is_sec_sym (dest_sym)) {
@@ -5021,6 +5023,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/elf.c +1 -0
@@ -1156,6 +1156,7 @@ struct elf *elf_open_read(const char *name, int flags) cmd =ELF_C_WRITE ; elf->elf =elf_begin (elf->fd, cmd,NULL ); +if (!elf->elf) {ERROR_ELF ("elf_begin" ); goto err;
tools/objtool/include/objtool/builtin.h +1 -0
@@ -13,6 +13,7 @@ struct opts {bool checksum;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
@@ -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.43.0
PATCH 5/6 kbuild: Add objtool integration for PowerPC feature fixups
In reply to Sathvika VasireddyAdd 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
@@ -1478,6 +1478,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
@@ -179,10 +179,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
@@ -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.43.0
PATCH 6/6 powerpc: Enable build-time feature fixup processing by default
In reply to Sathvika VasireddyEnable 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
@@ -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
@@ -105,6 +105,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
@@ -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
@@ -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
@@ -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
@@ -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.43.0
PATCH 4/6 objtool/powerpc: Skip jump destination analysis and unnanotated intra-function call warnings for --ftr-fixup
In reply to Sathvika VasireddyObjtool 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.+0x4c00000000002a618: 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 | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
tools/objtool/check.c +9 -5
@@ -1644,7 +1644,6 @@ static int add_jump_destinations(struct objtool_file *file) dest_off == func->offset + func->len)continue ; -ERROR_INSN (insn,"can't find jump dest instruction at %s" ,offstr (dest_sec, dest_off));return -1 ;
@@ -1727,8 +1726,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))) {
@@ -2681,8 +2683,10 @@ static int decode_sections(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.43.0
Christophe Leroy (CS GROUP)
reviewerLe 05/05/2026 à 10:46, Sathvika Vasireddy a écrit :
Quoted 7 lines
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.
What are the changes since last RFC https://lore.kernel.org/linuxppc-dev/[email protected]/
Quoted 37 lines
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: SetELF_F_LAYOUTflag to preserve vmlinux segment layout
objtool: Fix "can't find starting instruction" warnings on vmlinux objtool/powerpc: Skip jump destination analysis and unnanotatedintra-function call warnings for --ftr-fixupkbuild: Add objtool integration for PowerPC feature fixups
powerpc: Enable build-time feature fixup processing by defaultMakefile | 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 | 15 +- tools/objtool/arch/powerpc/special.c | 451 ++++++++++++++++++++++ tools/objtool/builtin-check.c | 2 + tools/objtool/check.c | 57 ++- tools/objtool/elf.c | 4 + tools/objtool/include/objtool/builtin.h | 1 + tools/objtool/include/objtool/special.h | 56 +++ tools/objtool/special.c | 29 ++ 16 files changed, 637 insertions(+), 30 deletions(-)
Peter Zijlstra
maintainerQuoted 22 lines · Sathvika Vasireddy
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: SetELF_F_LAYOUTflag to preserve vmlinux segment layout
objtool: Fix "can't find starting instruction" warnings on vmlinux objtool/powerpc: Skip jump destination analysis and unnanotated
Quoted 3 lines
intra-function call warnings for --ftr-fixupkbuild: Add objtool integration for PowerPC feature fixups
powerpc: Enable build-time feature fixup processing by default
Ooh, so 2/3 are because you're not running objtool on vmlinux.o, but you're running it later in the link stage?
AFAICT ELF_F_LAYOUT should not hurt the normal objtool run, but it shouldn't be needed either, because we run much earlier, before sections are placed.
And that also explains why you're the first to have trouble with 'can't find starting instruction'. vmlinux.o will have unplaced sections that are all relative 0.
Hmm... perhaps this ftr fixup should be its own command, like klp. Not munged into check thing. Josh?
Peter Zijlstra
maintainerRe: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
In reply to Sathvika Vasireddytools/objtool/arch/powerpc/decode.c int arch_decode_instruction(struct objtool_file *file, const struct section *sec · line 71
Quoted 2 lines · Sathvika Vasireddy
tools/objtool/arch/powerpc/decode.c +1
-0switch (opcode) { +case 16 :
Like case 18 below, this wants a comment describing which instruction this is; bclr ?
Quoted 13 lines
tools/objtool/arch/powerpc/decode.c +10
-0+if (ins &1 ) + typ =INSN_OTHER ; +else + typ =INSN_JUMP_CONDITIONAL ; + imm = ins &0xfffc ; +if (imm &0x8000 ) + imm -=0x10000 ; + insn->immediate = imm | aa; +break ; +case 18 :/* b[l][a] */ if (ins ==0x48000005 )/* bl .+4 */ typ =INSN_OTHER ;
Peter Zijlstra
maintainerRe: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
Quoted 6 lines · Peter Zijlstra
tools/objtool/arch/powerpc/decode.c int arch_decode_instruction(struct objtool_file *file, const struct section *sec · line 71
Quoted 2 lines · Sathvika Vasireddy
tools/objtool/arch/powerpc/decode.c +1
-0switch (opcode) { +case 16 :Like case 18 below, this wants a comment describing which instruction this is; bclr ?
Also, isn't that typically used for return, suggesting at least something should have INSN_RETURN on?
Quoted 13 lines
Quoted 13 lines
tools/objtool/arch/powerpc/decode.c +10
-0+if (ins &1 ) + typ =INSN_OTHER ; +else + typ =INSN_JUMP_CONDITIONAL ; + imm = ins &0xfffc ; +if (imm &0x8000 ) + imm -=0x10000 ; + insn->immediate = imm | aa; +break ; +case 18 :/* b[l][a] */ if (ins ==0x48000005 )/* bl .+4 */ typ =INSN_OTHER ;
Christophe Leroy (CS GROUP)
reviewerRe: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
Le 05/05/2026 à 16:49, Peter Zijlstra a écrit :
Quoted 7 lines · Peter Zijlstra
Quoted 6 lines · Sathvika Vasireddy
tools/objtool/arch/powerpc/decode.c int arch_decode_instruction(struct objtool_file *file, const struct section *sec · line 71
Quoted 2 lines
switch (opcode) {tools/objtool/arch/powerpc/decode.c +1
-0+case 16 :Like case 18 below, this wants a comment describing which instruction this is; bclr ?
No, it is bc/bca/bcl/bcla, it is quite similar to type 18 which is b/ba/bl/bla, but that's a conditional branch to a 14-bit offset while type 18 is a non-conditional branch to a 24-bit offset (10 bits are the conditional in the bc, hence the reduced offset size).
Quoted 4 lines
Also, isn't that typically used for return, suggesting at least something should have
INSN_RETURNon?
bclr (which is the return INSN_RETURN) has type 19
By the way you can have a look at https://patchwork.ozlabs.org/project/linuxppc-dev/patch/bfa8364da047d8610a09458a1cd924a0566aedbb.1736955567.git.christophe.leroy@csgroup.eu/
That patch has all the objtool decoding. By the way objtool is missing a INSN_CONDITIONAL_RETURN, also see https://patchwork.ozlabs.org/project/linuxppc-dev/patch/537e5d8f181b1f1c2b8918f1aefa1dba3f972c03.1736955567.git.christophe.leroy@csgroup.eu/
Quoted 13 lines
Quoted 13 lines
Quoted 13 lines
tools/objtool/arch/powerpc/decode.c +10
-0+if (ins &1 ) + typ =INSN_OTHER ; +else + typ =INSN_JUMP_CONDITIONAL ; + imm = ins &0xfffc ; +if (imm &0x8000 ) + imm -=0x10000 ; + insn->immediate = imm | aa; +break ; +case 18 :/* b[l][a] */ if (ins ==0x48000005 )/* bl .+4 */ typ =INSN_OTHER ;
Re: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
In reply to Peter ZijlstraHi!
Quoted 6 lines · Peter Zijlstra
tools/objtool/arch/powerpc/decode.c int arch_decode_instruction(struct objtool_file *file, const struct section *sec · line 71
Quoted 2 lines · Sathvika Vasireddy
tools/objtool/arch/powerpc/decode.c +1
-0switch (opcode) { +case 16 :Like case 18 below, this wants a comment describing which instruction this is; bclr ?
Yes. It is 19/16, b[c]lr (primary opcode 19, secondary opcode 16).
Where is it described what INSN_RETURN actually means for objtool? Not in the header file :-(
For PowerPC, all three of b[c]lr[l], b[c]ctr[l], and b[c]tar[l] are indirect jumps, to an address in a register. They do absolutely nothing typically done in a function epilog, like "return" insns in some other archs do. But the BH field ("branch hint") in the instruction helps predict the control flow involving the insn as something in a call and return pattern :-)
Segher
Peter Zijlstra
maintainerRe: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
Quoted 10 lines · Segher Boessenkool
Hi!
Quoted 6 lines · Peter Zijlstra
tools/objtool/arch/powerpc/decode.c int arch_decode_instruction(struct objtool_file *file, const struct section *sec · line 71
Quoted 2 lines · Sathvika Vasireddy
tools/objtool/arch/powerpc/decode.c +1
-0switch (opcode) { +case 16 :Like case 18 below, this wants a comment describing which instruction this is; bclr ?
Quoted 4 lines
Yes. It is 19/16, b[c]lr (primary opcode 19, secondary opcode 16).
Where is it described what
INSN_RETURNactually means for objtool? Not in the header file :-(
Yeah, nowhere much I'm afraid, it is very much organic growth that is firmly rooted in x86.
RETURN, along with sibling/tail CALLs validate things like the stack frame being in identical state as on function entry and a few other sanity checks (DF flag not set, no uaccess).
There is also a pile of hacks around the whole return thunk mitigation thing. But that might be less relevant for other archs.
Peter Zijlstra
maintainerRe: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
In reply to Christophe Leroy (CS GROUP)Quoted 3 lines · Christophe Leroy (CS GROUP)
bclr (which is the return
INSN_RETURN) has type 19By the way you can have a look at https://patchwork.ozlabs.org/project/linuxppc-dev/patch/bfa8364da047d8610a09458a1cd924a0566aedbb.1736955567.git.christophe.leroy@csgroup.eu/
That is indeed more; isn't bcl something like COND_CALL ? (another one of them things we don't have).
Quoted 2 lines
That patch has all the objtool decoding. By the way objtool is missing a
INSN_CONDITIONAL_RETURN, also see https://patchwork.ozlabs.org/project/linuxppc-dev/patch/537e5d8f181b1f1c2b8918f1aefa1dba3f972c03.1736955567.git.christophe.leroy@csgroup.eu/
Right, that is not something x86 has, but I don't see a reason we can't add that. With return thunks, Clang (and I've heard GCC is also considering this) does something very close to conditional return.
With return thunks, regular RET instruction is replaced with a JMP, or rather Jcc in this specific case, to the out of line __x86_return_thunk symbol. Objtool currently treats that as a conditional sibling/tail call, which isn't wrong.
Anyway, with that patch, I think validate_insn() wants something like so added:
case INSN_COND_RETURN: validate_return(func, insn, statep); break;
That way we do the return checks, but don't terminate the control flow. After all, when the condition is taken, we had better have the stack frame in the same state etc.
Re: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
In reply to Peter ZijlstraHi!
Quoted 14 lines · Peter Zijlstra
Quoted 8 lines · Segher Boessenkool
Quoted 6 lines · Peter Zijlstra
tools/objtool/arch/powerpc/decode.c int arch_decode_instruction(struct objtool_file *file, const struct section *sec · line 71
Quoted 2 lines · Sathvika Vasireddy
tools/objtool/arch/powerpc/decode.c +1
-0switch (opcode) { +case 16 :Like case 18 below, this wants a comment describing which instruction this is; bclr ?
Quoted 4 lines
Yes. It is 19/16, b[c]lr (primary opcode 19, secondary opcode 16).
Where is it described what
INSN_RETURNactually means for objtool? Not in the header file :-(
Quoted 6 lines
Yeah, nowhere much I'm afraid, it is very much organic growth that is firmly rooted in x86.
RETURN, along with sibling/tail CALLs validate things like the stack frame being in identical state as on function entry and a few other sanity checks (DF flag not set, no uaccess).
Huh.
On function entry, there is *no* accessible stack frame, on our ABIs (typically you can still access your parent's frame of course, but then you first need to find out who your parent is, etc.) All stack frames are always set up by separate store instructions. We are a RISC architecture after all (POWER means "Performance Optimisation With Enhanced RISC"). So objtool checks if we actually tore down all stack frames? What a very useful thing to do.
Stack frames are a software concept in the first place, it has nothing to do with the hardware, *at all*. This is a bit different on archs that *actually* have such a thing as a frame pointer, that don't emulate it using a GPR (or something in memory!)
Quoted 2 lines
There is also a pile of hacks around the whole return thunk mitigation thing. But that might be less relevant for other archs.
I don't really want to think about it :-) Horrors!
There are many other archs where all (or almost all, "all normal", call/ return sequences use a "link register", often called exactly that. It's the modern consensus to design call/return around that, I'd say even. It would be nice if this abstraction worked well ;-)
Segher
Peter Zijlstra
maintainerRe: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
Quoted 34 lines · Segher Boessenkool
Hi!
Quoted 14 lines · Peter Zijlstra
Quoted 8 lines · Segher Boessenkool
Quoted 6 lines · Peter Zijlstra
tools/objtool/arch/powerpc/decode.c int arch_decode_instruction(struct objtool_file *file, const struct section *sec · line 71
Quoted 2 lines · Sathvika Vasireddy
tools/objtool/arch/powerpc/decode.c +1
-0switch (opcode) { +case 16 :Like case 18 below, this wants a comment describing which instruction this is; bclr ?
Quoted 4 lines
Yes. It is 19/16, b[c]lr (primary opcode 19, secondary opcode 16).
Where is it described what
INSN_RETURNactually means for objtool? Not in the header file :-(Quoted 6 lines
Yeah, nowhere much I'm afraid, it is very much organic growth that is firmly rooted in x86.
RETURN, along with sibling/tail CALLs validate things like the stack frame being in identical state as on function entry and a few other sanity checks (DF flag not set, no uaccess).
Huh.
On function entry, there is *no* accessible stack frame, on our ABIs (typically you can still access your parent's frame of course, but then you first need to find out who your parent is, etc.) All stack frames are always set up by separate store instructions. We are a RISC architecture after all (POWER means "Performance Optimisation With Enhanced RISC"). So objtool checks if we actually tore down all stack frames? What a very useful thing to do.
Quoted 4 lines
Stack frames are a software concept in the first place, it has nothing to do with the hardware, *at all*. This is a bit different on archs that *actually* have such a thing as a frame pointer, that don't emulate it using a GPR (or something in memory!)
So, remember that objtool was born to generate ORC stack unwind data. It needs to track the stack state at every instruction to accomplish this.
One of the basic sanity checks it does is ensuring there is no lingering stack state on RETURN -- this would obviously cause the caller/returned-to context to get a wee bit upset.
And yes, I realize this might sound quite mad to a compiler person. But remember that we have a ton of ASM (inline and otherwise) mixed in with our C code. And while you can add DWARF CFI to ASM, this has historically been a bit of a trainwreck, not in the least because the annotations got wrong and nothing warned about it until the unwinder would explode.
Objtool solves all of that, by doing build time reconstruction of the control flow and stack state it guarantees everything is consistent at build time. Additionally it can deal with fun things like alternatives / static branches etc.
It can deal with both -fframe-pointer and -fomit-frame-pointer (on x86). When the frame pointer reg (BP) is used, it also validates this is done correctly.
This has caught many whoopsies over the years.
Then because this thing is parsing object files and decoding control flow, features were added, like the uaccess scope checks.
Quoted 9 lines
Quoted 2 lines
There is also a pile of hacks around the whole return thunk mitigation thing. But that might be less relevant for other archs.
I don't really want to think about it :-) Horrors!
There are many other archs where all (or almost all, "all normal", call/ return sequences use a "link register", often called exactly that. It's the modern consensus to design call/return around that, I'd say even. It would be nice if this abstraction worked well ;-)
Agreed. Its just that this thing has a very strong x86 flavour per its legacy ;-) We'll get it sorted.
Re: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
In reply to Peter ZijlstraQuoted 7 lines · Peter Zijlstra
Quoted 3 lines · Christophe Leroy (CS GROUP)
bclr (which is the return
INSN_RETURN) has type 19By the way you can have a look at https://patchwork.ozlabs.org/project/linuxppc-dev/patch/bfa8364da047d8610a09458a1cd924a0566aedbb.1736955567.git.christophe.leroy@csgroup.eu/
That is indeed more; isn't bcl something like
COND_CALL? (another one of them things we don't have).
Yeah, all of bc[l][a] are conditional branches. "a" (AA=1) means the branch target is an absolute address (in the top or bottom 8kB of the address space), and "l" (LK=1) means set LR to the NIA ("next instruction address") before doing the branch.
The BO field describes the condition. 0x14 means "always". (But there also are primary opcodes for b[l][a], with a bigger target field, 24 bits instead of the 14 bits of bc[l][a]; you do not have the BI and BO fields then of course) ("branch index" and "branch option" btw).
Quoted 6 lines
Quoted 2 lines
That patch has all the objtool decoding. By the way objtool is missing a
INSN_CONDITIONAL_RETURN, also see https://patchwork.ozlabs.org/project/linuxppc-dev/patch/537e5d8f181b1f1c2b8918f1aefa1dba3f972c03.1736955567.git.christophe.leroy@csgroup.eu/Right, that is not something x86 has, but I don't see a reason we can't add that. With return thunks, Clang (and I've heard GCC is also considering this) does something very close to conditional return.
A conditional blr does not map well to most compiled languages. It is nice for hand-written machine code though :-)
For compiled code you almost always want to make some of the associated stack frame maintenance conditional as well (because the ABIs require this, if nothing else!)
I guess we could add some special-case stuff for it, maybe a peephole, but we probably would have to write a whole new pass for it, stuff with branch instructions always requires special checks :-p
Quoted 3 lines
That way we do the return checks, but don't terminate the control flow. After all, when the condition is taken, we had better have the stack frame in the same state etc.
Ugh, mixed abstractions again :-(
Segher
Re: [PATCH v1 1/6] objtool/powerpc: Add build-time fixup of alternate feature branch targets
In reply to Peter ZijlstraHi!
Quoted 11 lines · Peter Zijlstra
Quoted 10 lines · Segher Boessenkool
Huh.
On function entry, there is *no* accessible stack frame, on our ABIs (typically you can still access your parent's frame of course, but then you first need to find out who your parent is, etc.) All stack frames are always set up by separate store instructions. We are a RISC architecture after all (POWER means "Performance Optimisation With Enhanced RISC"). So objtool checks if we actually tore down all stack frames? What a very useful thing to do.
Quoted 8 lines
Quoted 4 lines
Stack frames are a software concept in the first place, it has nothing to do with the hardware, *at all*. This is a bit different on archs that *actually* have such a thing as a frame pointer, that don't emulate it using a GPR (or something in memory!)
So, remember that objtool was born to generate ORC stack unwind data. It needs to track the stack state at every instruction to accomplish this.
Yup.
Quoted 5 lines
One of the basic sanity checks it does is ensuring there is no lingering stack state on RETURN -- this would obviously cause the caller/returned-to context to get a wee bit upset.
And yes, I realize this might sound quite mad to a compiler person. But
It doesn't sound weird *at all*, we have to jump through many hoops in the compiler as well as in the ABIs to make this work at all.
Quoted 5 lines
remember that we have a ton of ASM (inline and otherwise) mixed in with our C code. And while you can add DWARF CFI to ASM, this has historically been a bit of a trainwreck, not in the least because the annotations got wrong and nothing warned about it until the unwinder would explode.
Typically, someone who writes inline assembler code cannot write CFI statements properly *at all*, he/she just doesn't have all the necessary information! Only the compiler does. It can put all kinds of stuff on the stack for example. Writing CFI statements manually is really forr
someone writing machine code, or actual assembler code perhaps.
Quoted 7 lines
Quoted 4 lines
There are many other archs where all (or almost all, "all normal", call/ return sequences use a "link register", often called exactly that. It's the modern consensus to design call/return around that, I'd say even. It would be nice if this abstraction worked well ;-)
Agreed. Its just that this thing has a very strong x86 flavour per its legacy ;-) We'll get it sorted.
Thanks for the care!
Segher
Quoted 34 lines · Peter Zijlstra
Quoted 22 lines · Sathvika Vasireddy
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: SetELF_F_LAYOUTflag to preserve vmlinux segment layout
objtool: Fix "can't find starting instruction" warnings on vmlinux objtool/powerpc: Skip jump destination analysis and unnanotatedQuoted 3 lines
intra-function call warnings for --ftr-fixupkbuild: Add objtool integration for PowerPC feature fixups
powerpc: Enable build-time feature fixup processing by defaultOoh, so 2/3 are because you're not running objtool on vmlinux.o, but you're running it later in the link stage?
AFAICT
ELF_F_LAYOUTshould not hurt the normal objtool run, but it shouldn't be needed either, because we run much earlier, before sections are placed.
Quoted 6 lines
And that also explains why you're the first to have trouble with 'can't find starting instruction'. vmlinux.o will have unplaced sections that are all relative 0.
Hmm... perhaps this ftr fixup should be its own command, like klp. Not munged into check thing. Josh?
Hi Peter,
As the original author for the patch series will not be working on this feature anymore, I'd like to take over and move this feature forward.
Your point about only using objtool at object stage, makes sense. Would you prefer if I rework this as a saperate tool then?
Regards
~Shivang