]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
objtool: Fix x86 addend calculation
authorJosh Poimboeuf <jpoimboe@kernel.org>
Wed, 17 Sep 2025 16:03:28 +0000 (09:03 -0700)
committerJosh Poimboeuf <jpoimboe@kernel.org>
Tue, 14 Oct 2025 21:45:24 +0000 (14:45 -0700)
On x86, arch_dest_reloc_offset() hardcodes the addend adjustment to
four, but the actual adjustment depends on the relocation type.  Fix
that.

Acked-by: Petr Mladek <pmladek@suse.com>
Tested-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
tools/objtool/arch/loongarch/decode.c
tools/objtool/arch/powerpc/decode.c
tools/objtool/arch/x86/decode.c
tools/objtool/check.c
tools/objtool/include/objtool/arch.h

index 2e555c4060c5e4dbcea3f661412e34e636709315..77942b927a7ac0801f16349b8f5bd171d6bcfe59 100644 (file)
@@ -17,9 +17,9 @@ unsigned long arch_jump_destination(struct instruction *insn)
        return insn->offset + (insn->immediate << 2);
 }
 
-unsigned long arch_dest_reloc_offset(int addend)
+s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *reloc)
 {
-       return addend;
+       return reloc_addend(reloc);
 }
 
 bool arch_pc_relative_reloc(struct reloc *reloc)
index c851c51d4bd35c8c05bfcb84908ab8f87819068a..9b17885e6cba66b0ff6bcbf5324ee5e8a13b023a 100644 (file)
@@ -14,9 +14,9 @@ int arch_ftrace_match(char *name)
        return !strcmp(name, "_mcount");
 }
 
-unsigned long arch_dest_reloc_offset(int addend)
+s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *reloc)
 {
-       return addend;
+       return reloc_addend(reloc);
 }
 
 bool arch_callee_saved_reg(unsigned char reg)
index 0ad5cc70ecbe74e267896962e7d4e65a970c6b0b..6742002a01f55586479ca54080d95a0147664af3 100644 (file)
@@ -68,9 +68,14 @@ bool arch_callee_saved_reg(unsigned char reg)
        }
 }
 
-unsigned long arch_dest_reloc_offset(int addend)
+s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *reloc)
 {
-       return addend + 4;
+       s64 addend = reloc_addend(reloc);
+
+       if (arch_pc_relative_reloc(reloc))
+               addend += insn->offset + insn->len - reloc_offset(reloc);
+
+       return addend;
 }
 
 unsigned long arch_jump_destination(struct instruction *insn)
index 02c3e2de85cef466fee8f376a9c1e1f8aec589c9..65eb90034d3ee3480917325997b8f498f928218e 100644 (file)
@@ -1499,7 +1499,7 @@ static int add_jump_destinations(struct objtool_file *file)
                        dest_off = arch_jump_destination(insn);
                } else if (reloc->sym->type == STT_SECTION) {
                        dest_sec = reloc->sym->sec;
-                       dest_off = arch_dest_reloc_offset(reloc_addend(reloc));
+                       dest_off = arch_insn_adjusted_addend(insn, reloc);
                } else if (reloc->sym->retpoline_thunk) {
                        if (add_retpoline_call(file, insn))
                                return -1;
@@ -1518,7 +1518,7 @@ static int add_jump_destinations(struct objtool_file *file)
                } else if (reloc->sym->sec->idx) {
                        dest_sec = reloc->sym->sec;
                        dest_off = reloc->sym->sym.st_value +
-                                  arch_dest_reloc_offset(reloc_addend(reloc));
+                                  arch_insn_adjusted_addend(insn, reloc);
                } else {
                        /* non-func asm code jumping to another file */
                        continue;
@@ -1663,7 +1663,7 @@ static int add_call_destinations(struct objtool_file *file)
                        }
 
                } else if (reloc->sym->type == STT_SECTION) {
-                       dest_off = arch_dest_reloc_offset(reloc_addend(reloc));
+                       dest_off = arch_insn_adjusted_addend(insn, reloc);
                        dest = find_call_destination(reloc->sym->sec, dest_off);
                        if (!dest) {
                                ERROR_INSN(insn, "can't find call dest symbol at %s+0x%lx",
@@ -3315,7 +3315,7 @@ static bool pv_call_dest(struct objtool_file *file, struct instruction *insn)
        if (!reloc || strcmp(reloc->sym->name, "pv_ops"))
                return false;
 
-       idx = (arch_dest_reloc_offset(reloc_addend(reloc)) / sizeof(void *));
+       idx = arch_insn_adjusted_addend(insn, reloc) / sizeof(void *);
 
        if (file->pv_ops[idx].clean)
                return true;
@@ -4396,12 +4396,7 @@ static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn
                                              reloc_offset(reloc) + 1,
                                              (insn->offset + insn->len) - (reloc_offset(reloc) + 1))) {
 
-               off = reloc->sym->offset;
-               if (reloc_type(reloc) == R_X86_64_PC32 ||
-                   reloc_type(reloc) == R_X86_64_PLT32)
-                       off += arch_dest_reloc_offset(reloc_addend(reloc));
-               else
-                       off += reloc_addend(reloc);
+               off = reloc->sym->offset + arch_insn_adjusted_addend(insn, reloc);
 
                dest = find_insn(file, reloc->sym->sec, off);
                if (!dest)
index be33c7b43180aa616a7687a7baee13dbffa15fbb..68664625a467138d524ce7a6ef7446e43d4ce782 100644 (file)
@@ -83,7 +83,7 @@ bool arch_callee_saved_reg(unsigned char reg);
 
 unsigned long arch_jump_destination(struct instruction *insn);
 
-unsigned long arch_dest_reloc_offset(int addend);
+s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *reloc);
 
 const char *arch_nop_insn(int len);
 const char *arch_ret_insn(int len);