From nobody Tue Dec 16 12:42:39 2025 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 9E1502676CD; Fri, 9 May 2025 20:17:56 +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=1746821876; cv=none; b=W+WuJAu0W/Wo2nUsbNyrJh1ejn+++cvVhO/hPoL39thBjZQZIxSW87pE2IlJGvj/o8KHzo9zx0MH+Fh/Um56bylsrqiEVjqrXA1cFVibb/Iu/Gl2yuisPCx1TXRi/UBaN37930SEG/u3TPstuKylwpXP0QOXaNX8Ux4Z9mV8+a8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746821876; c=relaxed/simple; bh=VqG6g//chpaS3lqzskCigvBXnHLMqfE7+a/jSGlD9vg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ANvuxjYWdhtMXdZSjaknxVrLBJmcy+8g0t4Bq4/hDnF4ONHoFqKCCLUK+XrW1kVtIm8NY9ON8CSICZspMpWtO95mk363V6TlkRwa0vt5J20n0MPZjlgZg3AWfKau2AKRWb76uQXyRGzDJ+mBjG4ywGdvoRqqEsSm4fcswUCXWH4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rMb7fGMp; 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="rMb7fGMp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 007B7C4CEEE; Fri, 9 May 2025 20:17:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1746821876; bh=VqG6g//chpaS3lqzskCigvBXnHLMqfE7+a/jSGlD9vg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rMb7fGMpYwhyePF081JqcTo/4y8jWmBr5IW0ozT1/Jn080uC6Nj6OXhvwjchbxF5A 1G/Z1H5d9dklMfp2pYNJZkAE15gOuqKSloRB1FgbY7lK/Ve18HbvZTwttbEGPD2DMr XRri7hQvCT0CGqpey/D+KS2iCoy2lm3EjWOdIVa15+TXr0sQpsa9h2X76rLgG3BVCP f/YJVBW9rPYZxqEO2QY5+OMJ7zqNI84GyBwuTl9Pjtze3iS+/fcc2EC014yGXf7yWb j4D3aBCGjwA78/PcciWAzlZTN+TMXnaAOKgGYVjqRjNx2J+cT8jM/Fnc1jI/HHUNa2 HhQiApwzac5Qw== From: Josh Poimboeuf To: x86@kernel.org Cc: linux-kernel@vger.kernel.org, Petr Mladek , Miroslav Benes , Joe Lawrence , live-patching@vger.kernel.org, Song Liu , laokz , Jiri Kosina , Marcos Paulo de Souza , Weinan Liu , Fazla Mehrab , Chen Zhongjin , Puranjay Mohan Subject: [PATCH v2 18/62] objtool: Fix x86 addend calculation Date: Fri, 9 May 2025 13:16:42 -0700 Message-ID: <8064f40394e9f0438a36f53f54e3b56f8e5b5365.1746821544.git.jpoimboe@kernel.org> X-Mailer: git-send-email 2.49.0 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" 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/loo= ngarch/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 *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(reloc); } =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 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"); } =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 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) } } =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 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 =3D arch_jump_destination(insn); } else if (reloc->sym->type =3D=3D STT_SECTION) { dest_sec =3D reloc->sym->sec; - dest_off =3D arch_dest_reloc_offset(reloc_addend(reloc)); + dest_off =3D arch_insn_adjusted_addend(insn, reloc); } else if (reloc->sym->retpoline_thunk) { ret =3D add_retpoline_call(file, insn); if (ret) @@ -1672,7 +1672,7 @@ static int add_call_destinations(struct objtool_file = *file) } =20 } else if (reloc->sym->type =3D=3D STT_SECTION) { - 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", @@ -3348,7 +3348,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; @@ -4396,12 +4396,7 @@ static int validate_ibt_insn(struct objtool_file *fi= le, struct instruction *insn reloc_offset(reloc) + 1, (insn->offset + insn->len) - (reloc_offset(reloc) + 1))) { =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 01ef6f415adf..cd1776c35b13 100644 --- a/tools/objtool/include/objtool/arch.h +++ b/tools/objtool/include/objtool/arch.h @@ -83,7 +83,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.49.0