[RFC 21/31] objtool: Fix x86 addend calcuation

Josh Poimboeuf
2024-09-03T04:00:40Z

arch_dest_reloc_offset() hard-codes the addend adjustment to 4, which
isn't always true. In fact it's dependent on the instruction itself.

Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
---
tools/objtool/arch/loongarch/decode.c | 4 ++--
tools/objtool/arch/powerpc/decode.c | 4 ++--
tools/objtool/arch/x86/decode.c | 15 +++++++++++++--
tools/objtool/check.c | 13 ++++---------
tools/objtool/include/objtool/arch.h | 2 +-
5 files changed, 22 insertions(+), 16 deletions(-)

diff --git a/tools/objtool/arch/loongarch/decode.c b/tools/objtool/arch/loongarch/decode.c
index ef09996c772e..b5d44d7bce4e 100644
--- a/tools/objtool/arch/loongarch/decode.c
+++ b/tools/objtool/arch/loongarch/decode.c
@@ -20,9 +20,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(addend);
}

bool arch_pc_relative_reloc(struct reloc *reloc)
diff --git a/tools/objtool/arch/powerpc/decode.c b/tools/objtool/arch/powerpc/decode.c
index 29e05ad1b8b6..11e59065f1dc 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -14,9 +14,9 @@ int arch_ftrace_match(const 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)
diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c
index 72d55dcd3d7f..afebd67d9b9d 100644
--- a/tools/objtool/arch/x86/decode.c
+++ b/tools/objtool/arch/x86/decode.c
@@ -67,9 +67,20 @@ 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);
+
+ switch (reloc_type(reloc)) {
+ case R_X86_64_PC32:
+ case R_X86_64_PLT32:
+ addend += insn->offset + insn->len - reloc_offset(reloc);
+ break;
+ default:
+ break;
+ }
+
+ return addend;
}

unsigned long arch_jump_destination(struct instruction *insn)
diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index 236dc7871f01..3c8d0903dfa7 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1461,7 +1461,7 @@ static void add_jump_destinations(struct objtool_file *file)
} else if (sym_has_section(reloc->sym)) {
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 {
/* External symbol (UNDEF) */
dest_sec = NULL;
@@ -1609,7 +1609,7 @@ static void add_call_destinations(struct objtool_file *file)
ERROR_INSN(insn, "unsupported call to non-function");

} else if (is_section_symbol(reloc->sym)) {
- 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",
@@ -3119,7 +3119,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;
@@ -4070,12 +4070,7 @@ static int validate_ibt_insn(struct objtool_file *file, struct instruction *insn
if (reloc->sym->static_call_tramp)
continue;

- 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)
diff --git a/tools/objtool/include/objtool/arch.h b/tools/objtool/include/objtool/arch.h
index f48f5109abb1..14911fdfdc8f 100644
--- a/tools/objtool/include/objtool/arch.h
+++ b/tools/objtool/include/objtool/arch.h
@@ -81,7 +81,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);
--
2.45.2