Re: [PATCH v2 18/62] objtool: Fix x86 addend calculation

2025-06-04 Thread Josh Poimboeuf
On Mon, May 26, 2025 at 12:23:15PM +0200, Peter Zijlstra wrote:
> On Fri, May 09, 2025 at 01:16:42PM -0700, Josh Poimboeuf wrote:
> > On x86, arch_dest_reloc_offset() hardcodes the addend adjustment to
> > four, but the actual adjustment depends on the relocation type.  Fix
> > that.
> 
> > +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;
> >  }
> 
> Should this not be something like:
> 
> s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *reloc)
> {
>   s64 addend = reloc_addend(reloc);
> 
>   if (arch_pc_relative_reloc(reloc))
>   addend += insn->offset + insn->len - reloc_offset(reloc);
> 
>   return addend;
> }
> 
> instead?
> 
> AFAIU arch_pc_relative_reloc() is the exact same set of relocations.

Yeah that's better, thanks.

-- 
Josh



Re: [PATCH v2 18/62] objtool: Fix x86 addend calculation

2025-05-26 Thread Peter Zijlstra
On Fri, May 09, 2025 at 01:16:42PM -0700, Josh Poimboeuf wrote:
> On x86, arch_dest_reloc_offset() hardcodes the addend adjustment to
> four, but the actual adjustment depends on the relocation type.  Fix
> that.

> +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;
>  }

Should this not be something like:

s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *reloc)
{
s64 addend = reloc_addend(reloc);

if (arch_pc_relative_reloc(reloc))
addend += insn->offset + insn->len - reloc_offset(reloc);

return addend;
}

instead?

AFAIU arch_pc_relative_reloc() is the exact same set of relocations.



[PATCH v2 18/62] objtool: Fix x86 addend calculation

2025-05-09 Thread Josh Poimboeuf
On x86, arch_dest_reloc_offset() hardcodes the addend adjustment to
four, but the actual adjustment depends on the relocation type.  Fix
that.

Signed-off-by: Josh Poimboeuf 
---
 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 b6fdc68053cc..330671d88c59 100644
--- a/tools/objtool/arch/loongarch/decode.c
+++ b/tools/objtool/arch/loongarch/decode.c
@@ -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)
diff --git a/tools/objtool/arch/powerpc/decode.c 
b/tools/objtool/arch/powerpc/decode.c
index c851c51d4bd3..9b17885e6cba 100644
--- a/tools/objtool/arch/powerpc/decode.c
+++ b/tools/objtool/arch/powerpc/decode.c
@@ -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)
diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decode.c
index 331b9a744410..771ad24e49ee 100644
--- a/tools/objtool/arch/x86/decode.c
+++ b/tools/objtool/arch/x86/decode.c
@@ -68,9 +68,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 3a411064fa34..ea4e0facd21b 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -1502,7 +1502,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) {
ret = add_retpoline_call(file, insn);
if (ret)
@@ -1672,7 +1672,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",
@@ -3348,7 +3348,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)
diff --git a/tools/objtool/include/objtool/arch.h 
b/tools/objtool/include/objtool/arch.h
index 01e