From nobody Sun Feb 8 14:07:04 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8331F1D61AE; Tue, 3 Sep 2024 04:00:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725336040; cv=none; b=byUzh6dmoFUz5Pr3vKMKjfNjCr2sPEjojhO0QRZ8agM7D5Iy6TExyWbd4J6ArqDe9yt5mRt0j8AnaIHzyZRm4+JeAgowtsPPiqVj0KR2kBFIApHveVD7hT0b3BrBahKeoVF8/7oIDdJUmIwfq9AhLXIb2gYIbREcxcrugndzXUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1725336040; c=relaxed/simple; bh=67oHwTiAtqRuquV0JYsJeyGvcVHWHwzQnwQ9KJdp+kE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Liik7keeTtXYGNZyeKPCr+rlxrTZ223voELXymz4nj2sc5eu/w/kpCEdnuWcfRk+gKJhULMJ8f5SR+Yb+02fGYOnKSml1xa7tmHp/7le9+J/naBWzpzPJseeHjVM8Ykc5BsGs3qa5HSnLKa+ZKrhncAsVKEgx7gpumu3q24EbMU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=R3zgjR+2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="R3zgjR+2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9034FC4CECA; Tue, 3 Sep 2024 04:00:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725336039; bh=67oHwTiAtqRuquV0JYsJeyGvcVHWHwzQnwQ9KJdp+kE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R3zgjR+2Ti2BsDWbzA/ISFoOjQbmnSfmLVRUfUa7lSkG7jbTec1f8PV35TNTNWbcW +hD3eylt46m0LdYgBYr4AwRYEVuHz2qI+RRavUE5rUrDedM0RA7bkiWRjGmjv7Oq8y RHfNqSzNKYbw6m2NY/8emLNTBwnmxiJgq5ykm1J1bQSS4w8wOTWrYEABHAjlrQiXxC goXp87tJkWpDNrEsizZD0nhv2wxsaiYMz5H+2axjv9Bum3DDNTagH4in2TdOxlviwI pt4OzfjkqFzT6yg+OGdJ0dhu9tsG5Eap7CpOeMzxXatzg8hnnRutBj7N6PRrO8SaQP hOGHZatboFHZw== From: Josh Poimboeuf To: live-patching@vger.kernel.org Cc: linux-kernel@vger.kernel.org, x86@kernel.org, Miroslav Benes , Petr Mladek , Joe Lawrence , Jiri Kosina , Peter Zijlstra , Marcos Paulo de Souza , Song Liu Subject: [RFC 21/31] objtool: Fix x86 addend calcuation Date: Mon, 2 Sep 2024 21:00:04 -0700 Message-ID: <43433a745f6db5afb513d015a6181bc40be12b4f.1725334260.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- 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/loo= ngarch/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 *i= nsn) return insn->offset + (insn->immediate << 2); } =20 -unsigned long arch_dest_reloc_offset(int addend) +s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *relo= c) { - return addend; + return reloc_addend(addend); } =20 bool arch_pc_relative_reloc(struct reloc *reloc) diff --git a/tools/objtool/arch/powerpc/decode.c b/tools/objtool/arch/power= pc/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"); } =20 -unsigned long arch_dest_reloc_offset(int addend) +s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *relo= c) { - return addend; + return reloc_addend(reloc); } =20 bool arch_callee_saved_reg(unsigned char reg) diff --git a/tools/objtool/arch/x86/decode.c b/tools/objtool/arch/x86/decod= e.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) } } =20 -unsigned long arch_dest_reloc_offset(int addend) +s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *relo= c) { - return addend + 4; + s64 addend =3D reloc_addend(reloc); + + switch (reloc_type(reloc)) { + case R_X86_64_PC32: + case R_X86_64_PLT32: + addend +=3D insn->offset + insn->len - reloc_offset(reloc); + break; + default: + break; + } + + return addend; } =20 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 =3D reloc->sym->sec; dest_off =3D reloc->sym->sym.st_value + - arch_dest_reloc_offset(reloc_addend(reloc)); + arch_insn_adjusted_addend(insn, reloc); } else { /* External symbol (UNDEF) */ dest_sec =3D NULL; @@ -1609,7 +1609,7 @@ static void add_call_destinations(struct objtool_file= *file) ERROR_INSN(insn, "unsupported call to non-function"); =20 } else if (is_section_symbol(reloc->sym)) { - dest_off =3D arch_dest_reloc_offset(reloc_addend(reloc)); + dest_off =3D arch_insn_adjusted_addend(insn, reloc); dest =3D 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, s= truct instruction *insn) if (!reloc || strcmp(reloc->sym->name, "pv_ops")) return false; =20 - idx =3D (arch_dest_reloc_offset(reloc_addend(reloc)) / sizeof(void *)); + idx =3D (arch_insn_adjusted_addend(insn, reloc) / sizeof(void *)); =20 if (file->pv_ops[idx].clean) return true; @@ -4070,12 +4070,7 @@ static int validate_ibt_insn(struct objtool_file *fi= le, struct instruction *insn if (reloc->sym->static_call_tramp) continue; =20 - off =3D reloc->sym->offset; - if (reloc_type(reloc) =3D=3D R_X86_64_PC32 || - reloc_type(reloc) =3D=3D R_X86_64_PLT32) - off +=3D arch_dest_reloc_offset(reloc_addend(reloc)); - else - off +=3D reloc_addend(reloc); + off =3D reloc->sym->offset + arch_insn_adjusted_addend(insn, reloc); =20 dest =3D find_insn(file, reloc->sym->sec, off); if (!dest) diff --git a/tools/objtool/include/objtool/arch.h b/tools/objtool/include/o= bjtool/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); =20 unsigned long arch_jump_destination(struct instruction *insn); =20 -unsigned long arch_dest_reloc_offset(int addend); +s64 arch_insn_adjusted_addend(struct instruction *insn, struct reloc *relo= c); =20 const char *arch_nop_insn(int len); const char *arch_ret_insn(int len); --=20 2.45.2