From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 86A5A14F9FD for ; Fri, 22 Nov 2024 04:50:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251029; cv=none; b=YtsNemOgj/Dk5OGTVdWFv/2z2HHKLD6/gIfHOF1QdWpP21AfUgpXG9XqqOOkWYVI+d5fEgg9A3bIblejP9ghr5BmR7k7wDY/XVPvtmcPGzEh795Xbvxo7sAph2LgbBxe8RPtcfdvXgIEthoK5AZeYZSudwb71JbdOPxVd5svjnE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251029; c=relaxed/simple; bh=WQBjV7XvQFc38Q87Jv1cXvfn27Td+pGBvefYsoaVrgk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PZmEm1D71J7r2SywkhZpVeFGq5hNBcvObrFtZRxTDzyOsR5ig+vlAeVuyAbhxn2vkBnohziMbNi/m9Ua2BzcSJCedoHbs7BuymFnHZpeul8/2ttyZY1Vexir39BmQ3k+2c4er4bi2VkAXYKS7WK5Y0zVDgKRphLgxDU4fwL7cgM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8AxEK+KDUBn87xFAA--.32039S3; Fri, 22 Nov 2024 12:50:18 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S3; Fri, 22 Nov 2024 12:50:17 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 01/10] objtool: Handle various symbol types of rodata Date: Fri, 22 Nov 2024 12:49:56 +0800 Message-ID: <20241122045005.14617-2-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S3 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxJFWDKF4kAF4kGFWfGw18Xrc_yoW5Ww4xpF sxG3y5Kr45Xr1fWws7tFW0gF9xGws7WrnrJ39rKr1rA3sIqF1rKFWSkr1ay3WDGr1Fva17 XF1Y9ry7uF4UA3gCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU90b4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_JFI_Gr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Jr0_Gr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r12 6r1DMcIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_ Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_JFI_Gr1lIxAI cVC0I7IYx2IY6xkF7I0E14v26r1j6r4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E87Iv6xkF7I0E14v26r1j6r4UYxBIdaVFxhVj vjDU0xZFpf9x07j5xhLUUUUU= Content-Type: text/plain; charset="utf-8" In the relocation section ".rela.rodata" of each .o file compiled with LoongArch toolchain, there are various symbol types such as STT_NOTYPE, STT_OBJECT, STT_FUNC in addition to the usual STT_SECTION, it needs to use reloc symbol offset instead of reloc addend to find the destination instruction in find_jump_table() and add_jump_table(). This is preparation for later patch on LoongArch, there is no effect for the other archs with this patch. Signed-off-by: Tiezhu Yang --- tools/objtool/check.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 6604f5d038aa..9601235e908d 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -2079,6 +2079,7 @@ static int add_jump_table(struct objtool_file *file, = struct instruction *insn, unsigned int prev_offset =3D 0; struct reloc *reloc =3D table; struct alternative *alt; + unsigned long offset; =20 /* * Each @reloc is a switch table relocation which points to the target @@ -2094,12 +2095,19 @@ static int add_jump_table(struct objtool_file *file= , struct instruction *insn, if (prev_offset && reloc_offset(reloc) !=3D prev_offset + 8) break; =20 + if (reloc->sym->type =3D=3D STT_SECTION) { + /* Addend field in the relocation entry associated with the symbol */ + offset =3D reloc_addend(reloc); + } else { + /* The address of the symbol in the relocation entry */ + offset =3D reloc->sym->offset; + } + /* Detect function pointers from contiguous objects: */ - if (reloc->sym->sec =3D=3D pfunc->sec && - reloc_addend(reloc) =3D=3D pfunc->offset) + if (reloc->sym->sec =3D=3D pfunc->sec && offset =3D=3D pfunc->offset) break; =20 - dest_insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); + dest_insn =3D find_insn(file, reloc->sym->sec, offset); if (!dest_insn) break; =20 @@ -2137,6 +2145,7 @@ static struct reloc *find_jump_table(struct objtool_f= ile *file, { struct reloc *table_reloc; struct instruction *dest_insn, *orig_insn =3D insn; + unsigned long offset; =20 /* * Backward search using the @first_jump_src links, these help avoid @@ -2160,7 +2169,16 @@ static struct reloc *find_jump_table(struct objtool_= file *file, table_reloc =3D arch_find_switch_table(file, insn); if (!table_reloc) continue; - dest_insn =3D find_insn(file, table_reloc->sym->sec, reloc_addend(table_= reloc)); + + if (table_reloc->sym->type =3D=3D STT_SECTION) { + /* Addend field in the relocation entry associated with the symbol */ + offset =3D reloc_addend(table_reloc); + } else { + /* The address of the symbol in the relocation entry */ + offset =3D table_reloc->sym->offset; + } + + dest_insn =3D find_insn(file, table_reloc->sym->sec, offset); if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != =3D func) continue; =20 --=20 2.42.0 From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 869AC2309A3 for ; Fri, 22 Nov 2024 04:50:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251029; cv=none; b=OY0l9hO8lg2U2pAYr7iRLmNlpukR+Fnk1xKyl+tO3qOgmF2vFNoG5/pFO1h6H2RMKrvcxzjrWEYKsvObs6PrnwhM6fC/wGOkSMIpwCI2k2vy3PuYjcJ1pR9RxPq29NHss7TbPFuxVxDyipR4FfpyaHzIPhkdgO+khl9nl1u8Kdw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251029; c=relaxed/simple; bh=vOolnWiiYZj7d9EwA89wL2WhopR966rxwOdfgnLlMsQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uS3TcReyfbSqh0g48dF5Lem1UXfbvqotYyq6hHl7GKNIH8VnJiIuxH5a3KNK3iFqoM6zXXaJpcaB900rlKNazJ+5S8M6yBBH3Xl2PrRecU/ZDRwg7XjpmumkISO0ZQkHS/o7vV7lPBFPfelz4sq8xLU1jU2gYX1XG1XNSVUyvdI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8DxQK+KDUBn97xFAA--.46944S3; Fri, 22 Nov 2024 12:50:18 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S4; Fri, 22 Nov 2024 12:50:18 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 02/10] objtool: Handle special cases of dead end insn Date: Fri, 22 Nov 2024 12:49:57 +0800 Message-ID: <20241122045005.14617-3-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S4 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoW7CF4kuryfGr4DKr17uw47ZFc_yoW8CF48pF Z8Cay5Jr4DXr4xW3W3Wr4rCr93Kw4fArWUXryfJ3yUK3s8Xwn5t3Z7tay2yas8Cw13W3W2 vFs0k34UuF1UArcCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Yb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q 6rW5McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_ Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Gr0_Xr1lIxAI cVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIev Ja73UjIFyTuYvjxU4AhLUUUUU Content-Type: text/plain; charset="utf-8" There are some "unreachable instruction" objtool warnings when compling with Clang on LoongArch, this is because the "break" instruction is set as dead end due to its type is INSN_BUG in decode_instructions() at the beginning, and it does not set insn->dead_end of the "break" instruction as false after checking ".rela.discard.reachable" in add_dead_ends(), so the next instruction of "break" is marked as unreachable. Actually, it can find the reachable instruction after parsing the section ".rela.discard.reachable", in some cases, the "break" instruction may not be the first previous instruction with scheduling by Machine Instruction Scheduler of LLVM, it should find more times and then set insn->dead_end of the "break" instruction as false. This is preparation for later patch on LoongArch, there is no effect for the other archs with this patch. Signed-off-by: Tiezhu Yang --- tools/objtool/check.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 9601235e908d..191950551352 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -22,6 +22,10 @@ #include #include =20 +#ifndef EM_LOONGARCH +#define EM_LOONGARCH 258 +#endif + struct alternative { struct alternative *next; struct instruction *insn; @@ -711,6 +715,18 @@ static int add_dead_ends(struct objtool_file *file) } =20 insn->dead_end =3D false; + + /* Handle the special cases compiled with Clang on LoongArch */ + if (file->elf->ehdr.e_machine =3D=3D EM_LOONGARCH && + reloc->sym->type =3D=3D STT_SECTION) { + while (insn && insn_func(insn)) { + insn =3D prev_insn_same_sym(file, insn); + if (insn && insn->dead_end) { + insn->dead_end =3D false; + break; + } + } + } } =20 return 0; --=20 2.42.0 From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 129771527A7 for ; Fri, 22 Nov 2024 04:51:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251103; cv=none; b=L3VixPwhYbzc0nuodbfV039oIr/2Gv8OUq/J2oDfxeY3yCo/dxyynAhW9T0GxezLWD2ZlP7dLiGGWuJp1T3HQpEa8qUsSrOWH14eNAcsZzii8ZDSaT+07XyDfsFZn/XBuj6DeeGIkk9px0DrU/Tp2vrnh8PXliSgzEzSbSJpffE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251103; c=relaxed/simple; bh=sUA2iDpfr8XXjasNoatf2r79oIC71AizCNh+S9ZSSqU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mLMAuaOEw00Z+tFz7d80/olsZ2swlcAaLR+B8OumzuLdonSmyDrm1IHJpoWKPgWwojrPfhIfj4AkEJr1jijlRL1KSTjFsLhOohMvAcp6ckNNxY5DtK005tJfcD+XRzwbKvQvMhhqWJzt32MpE2ET4iDubhGlrt0vppHvp6nBpZc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8BxIK+LDUBn+7xFAA--.47117S3; Fri, 22 Nov 2024 12:50:19 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S5; Fri, 22 Nov 2024 12:50:18 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 03/10] objtool: Handle different entry size of rodata Date: Fri, 22 Nov 2024 12:49:58 +0800 Message-ID: <20241122045005.14617-4-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S5 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoW7ur1fCr4kWw48AFW5ur1xJFc_yoW8Aw48pF 43C3yrtr4Fqr17W3ZrKan5urZxCw4Igry8JFZ7try8twsFqr1rtFZak3s0kFWUXr9Yg3Wf Zr90yry8uF4DA3gCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Yb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q 6rW5McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_ Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Gr0_Xr1lIxAI cVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIev Ja73UjIFyTuYvjxU4AhLUUUUU Content-Type: text/plain; charset="utf-8" In the most cases, the entry size of rodata is 8 bytes because the relocation type is 64 bit, but when compling with Clang on LoongArch, there exists 32 bit relocation type, the entry size of rodata should be 4 bytes in this case. This is preparation for later patch on LoongArch, there is no effect for the other archs with this patch. Signed-off-by: Tiezhu Yang --- tools/objtool/check.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 191950551352..19d1263e64e4 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -26,6 +26,10 @@ #define EM_LOONGARCH 258 #endif =20 +#ifndef R_LARCH_32_PCREL +#define R_LARCH_32_PCREL 99 +#endif + struct alternative { struct alternative *next; struct instruction *insn; @@ -2096,6 +2100,7 @@ static int add_jump_table(struct objtool_file *file, = struct instruction *insn, struct reloc *reloc =3D table; struct alternative *alt; unsigned long offset; + unsigned long rodata_entry_size; =20 /* * Each @reloc is a switch table relocation which points to the target @@ -2107,8 +2112,15 @@ static int add_jump_table(struct objtool_file *file,= struct instruction *insn, if (reloc !=3D table && reloc =3D=3D next_table) break; =20 + /* Handle the special cases compiled with Clang on LoongArch */ + if (file->elf->ehdr.e_machine =3D=3D EM_LOONGARCH && + reloc_type(reloc) =3D=3D R_LARCH_32_PCREL) + rodata_entry_size =3D 4; + else + rodata_entry_size =3D 8; + /* Make sure the table entries are consecutive: */ - if (prev_offset && reloc_offset(reloc) !=3D prev_offset + 8) + if (prev_offset && reloc_offset(reloc) !=3D prev_offset + rodata_entry_s= ize) break; =20 if (reloc->sym->type =3D=3D STT_SECTION) { --=20 2.42.0 From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1E8D11632E8 for ; Fri, 22 Nov 2024 04:51:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251104; cv=none; b=TLuIAAI7YDg8htpkJHn0TNSilxCd55YcInzgIRrpLM8WqNeuYRdW4aPDjRxfYl6O6oU/JLbARBd2FmBOF9avh9iPRY0I9pky3PJTPNeppVutL8PdwxlSlEt0eCmQ3fOO743FQCJ+l9Gj6pfEwqp9suFj25iFZC05hK8oP4/JTDg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251104; c=relaxed/simple; bh=sXJC7+BIzrkdr2c8xg26yJGufdpB5vL4s7bxiuKEKkk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QiRYQDzcOsqlfSlqNFUZ42y5/plzyL6fn8xd7JQU0VnC1fsy+t2Id0WR7HQil+se30JkLPnoXc/XR/tLt24yDhR5LeaSKyuZG9dTZWy2usDGmEBnRAgfcdw9bjsGqqJy1t+ZImBD1UDFU6O/0KFv1EjeaNjp2nMHRZXCbM/Fmvo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8BxPOKMDUBn_7xFAA--.5104S3; Fri, 22 Nov 2024 12:50:20 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S6; Fri, 22 Nov 2024 12:50:19 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 04/10] objtool: Handle PC relative relocation type Date: Fri, 22 Nov 2024 12:49:59 +0800 Message-ID: <20241122045005.14617-5-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S6 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoW7uFW7tr1fWw4DuF1kWFW7Awc_yoW8Jw4xpF 9xuw45KrsI9r17WwsrKa1kWrW5Cws7WryxJrW0kryrAF9xZF1rKay2ka15Ca1UXw1Fva1I vFyYkFy7Aa1DA3gCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Yb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q 6rW5McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_ Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Gr0_Xr1lIxAI cVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIev Ja73UjIFyTuYvjxU4AhLUUUUU Content-Type: text/plain; charset="utf-8" When compling with Clang on LoongArch, there exists 32 bit PC relative relocation type, it needs to get the offset with "S + A - PC" according to the spec of "ELF for the LoongArch Architecture". This is preparation for later patch on LoongArch, there is no effect for the other archs with this patch. Link: https://github.com/loongson/la-abi-specs/blob/release/laelf.adoc Signed-off-by: Tiezhu Yang --- tools/objtool/check.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 19d1263e64e4..8733ca620cca 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -2126,6 +2126,11 @@ static int add_jump_table(struct objtool_file *file,= struct instruction *insn, if (reloc->sym->type =3D=3D STT_SECTION) { /* Addend field in the relocation entry associated with the symbol */ offset =3D reloc_addend(reloc); + /* Handle the special cases compiled with Clang on LoongArch */ + if (file->elf->ehdr.e_machine =3D=3D EM_LOONGARCH && + reloc_type(reloc) =3D=3D R_LARCH_32_PCREL) + offset =3D reloc->sym->offset + reloc_addend(reloc) - + (reloc_offset(reloc) - reloc_offset(table)); } else { /* The address of the symbol in the relocation entry */ offset =3D reloc->sym->offset; --=20 2.42.0 From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 18C5016130B for ; Fri, 22 Nov 2024 04:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251031; cv=none; b=KppAihdqU4uaWQTQTRxyoEYHSVqIF1SK5TGxsgvS/mrMYnlhrtiOq+3p7BfVj572QgCjziNUg/zMJSQHVnvIf6qwTRfPCATMLLF3yZISRbW6TG/J21Vsfo6T+PYVY/WP+L9HYrV0HHQg/8e17yqBQG1P16fElaWaaYx9kRKG9Ug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251031; c=relaxed/simple; bh=c0YOBx3w5JSgJYW2hQNaJGpMTxwwOSL9sdUCHCSGEdA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XkX4u7hw2VmmAxuNgilyrtPUtvxKbKcZ7bpIeCmOV0b+17EX8DQGXYGz6sNu1dGpwn47vGvae0FZgkc4ANU4PZfDcDHpXdXm2jDY3p2Pf77G+uIirx/CDYBYx8NZTM/aYGNC2frtAQ+6YmmvugPMxEEXeUQnLeEWm3W+7yWypKk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8BxuuGNDUBnA71FAA--.7777S3; Fri, 22 Nov 2024 12:50:21 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S7; Fri, 22 Nov 2024 12:50:20 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 05/10] objtool: Handle unreachable entry of rodata Date: Fri, 22 Nov 2024 12:50:00 +0800 Message-ID: <20241122045005.14617-6-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S7 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoW7Kry8Jw4xGr18ZF13XF15WrX_yoW8JF47pF n8Ww4jkr4vqryIk3ZrKa10gry3Gan7Gry8Jr92yr4UA3sxXrn8tFWSka1avay5Wa1ru3WI vFWY9rWUuF4qyagCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Yb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r106r15M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q 6rW5McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_ Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Xr0_Ar1lIxAI cVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIev Ja73UjIFyTuYvjxU4miiDUUUU Content-Type: text/plain; charset="utf-8" When compling with Clang on LoongArch, there exists unreachable entry of rodata which points to a position after the function return instruction, this is generated by compiler to fill the non-existent switch case, just skip the entry when parsing the relocation section of rodata. This is preparation for later patch on LoongArch, there is no effect for the other archs with this patch. Signed-off-by: Tiezhu Yang --- tools/objtool/check.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 8733ca620cca..b21e47d8d3d1 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -2144,6 +2144,13 @@ static int add_jump_table(struct objtool_file *file,= struct instruction *insn, if (!dest_insn) break; =20 + /* Handle the special cases compiled with Clang on LoongArch */ + if (file->elf->ehdr.e_machine =3D=3D EM_LOONGARCH && reloc->sym->type = =3D=3D STT_SECTION && + (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc !=3D pfunc)) { + prev_offset =3D reloc_offset(reloc); + continue; + } + /* Make sure the destination is in the same function: */ if (!insn_func(dest_insn) || insn_func(dest_insn)->pfunc !=3D pfunc) break; --=20 2.42.0 From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3BAC71632E6 for ; Fri, 22 Nov 2024 04:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251031; cv=none; b=RjaU4c5ZxmZccdstAfMZfsVN365ff/zdFTYQSKm7SWfDlghQ5Jb+g7ct4O4HRwqBlJAsoFEgpVMGwPj5Aaj/e05yPyldF75yimYTNkl/OO8Yh7/Soi1MHNMTrOpOAyNOpE0/kETcmAVen4WT0rdgC1nqkNTllYPYwlGH5kSuYj0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251031; c=relaxed/simple; bh=pZll4EsN3g1CnxWFPtzbSrUz+zKAT53GwKlq67qeI8E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VjFSkLTatqjcWt7ArQtQAOtZ3Kxv1p0zewTQHIvEWAq5ISyEpvs0Zr7UTOHv8xHFME96g4t/cG5P14cuqlUU8WFnlJ3IH05QKVYRye3LMOWfKzlJTht3kr6r3t+24Eu8bvVhYqmgvLCfdFN9+wR/myAD5B64hc8Bj3ZLNfDBlGc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8BxYa+NDUBnB71FAA--.6023S3; Fri, 22 Nov 2024 12:50:21 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S8; Fri, 22 Nov 2024 12:50:21 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 06/10] objtool: Handle unsorted table offset of rodata Date: Fri, 22 Nov 2024 12:50:01 +0800 Message-ID: <20241122045005.14617-7-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S8 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoW7Cw18uFWfGrWfWFWfJr4kKrX_yoW8uF4xpF 15C3y8KrWYg34rKwnrt3WFg3y5Gw47GryxJr1xtry0q3sFvrnxtFZaka42kFWDX3y5W3WI va9xKry8CFs2yFXCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Yb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q 6rW5McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_ Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Xr0_Ar1lIxAI cVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIev Ja73UjIFyTuYvjxU4AhLUUUUU Content-Type: text/plain; charset="utf-8" When compling with Clang on LoongArch, there are unsorted table offset of rodata if there exist many jump tables, it will get the wrong table end and find the wrong jump destination instructions in add_jump_table(), so it needs to check the table size of rodata to break the process when parsing the relocation section of rodata. This is preparation for later patch on LoongArch, there is no effect for the other archs with this patch. Signed-off-by: Tiezhu Yang --- tools/objtool/check.c | 7 +++++++ tools/objtool/include/objtool/check.h | 1 + 2 files changed, 8 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index b21e47d8d3d1..3b2d94c90011 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -2101,6 +2101,7 @@ static int add_jump_table(struct objtool_file *file, = struct instruction *insn, struct alternative *alt; unsigned long offset; unsigned long rodata_entry_size; + unsigned long rodata_table_size =3D insn->table_size; =20 /* * Each @reloc is a switch table relocation which points to the target @@ -2112,6 +2113,12 @@ static int add_jump_table(struct objtool_file *file,= struct instruction *insn, if (reloc !=3D table && reloc =3D=3D next_table) break; =20 + /* Handle the special cases compiled with Clang on LoongArch */ + if (file->elf->ehdr.e_machine =3D=3D EM_LOONGARCH && + reloc->sym->type =3D=3D STT_SECTION && reloc !=3D table && + reloc_offset(reloc) =3D=3D reloc_offset(table) + rodata_table_size) + break; + /* Handle the special cases compiled with Clang on LoongArch */ if (file->elf->ehdr.e_machine =3D=3D EM_LOONGARCH && reloc_type(reloc) =3D=3D R_LARCH_32_PCREL) diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/= objtool/check.h index daa46f1f0965..11aafcc7b0c7 100644 --- a/tools/objtool/include/objtool/check.h +++ b/tools/objtool/include/objtool/check.h @@ -46,6 +46,7 @@ struct instruction { struct section *sec; unsigned long offset; unsigned long immediate; + unsigned long table_size; =20 u8 len; u8 prev_len; --=20 2.42.0 From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1E95615FD16 for ; Fri, 22 Nov 2024 04:51:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251105; cv=none; b=T4XaLp9jgUGjVTJ+4SpNwNckhaUE8Z0mqg375LqNPom9B4pY/hi78F4ko8PfdY8837Pc/DWMPSS0zcnCM1LvyppIQxgtQgB+oPZmoPi2gJ5NnnGZlZIkWiR2o2SKRDEL7CjMPj2dvMvMeZge6U9bFtpvSt9bwajd8Mmpe7OrzAk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251105; c=relaxed/simple; bh=U76I/hWykKRbyzMCtFBw6tfSwAXietSBOB/FdHQh8KY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=juAowMlZKYiWrBnE88ZQuF219QAkKkMuxS7bXiR4GfC+sO/KNqelD7Q7aBHeDqR4k7JnYv9rOGtmvqKeuUIKxqezxSk58DCTMO5V7cOKm2cJtFIJ10QaAsPD8LatfEAZzwhs8KuEn9ptLia3DHBosh2IXgDwIXlRZIxXTcRXbdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8Bx++GNDUBnDL1FAA--.4891S3; Fri, 22 Nov 2024 12:50:21 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S9; Fri, 22 Nov 2024 12:50:21 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 07/10] objtool/LoongArch: Get each table size of rodata Date: Fri, 22 Nov 2024 12:50:02 +0800 Message-ID: <20241122045005.14617-8-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S9 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxZryxuFyUJFWftF4Duw47Jrc_yoW5KF48pF 95C3s8trWYqFy5WF47AF4SyrW3Jw48WryxGr9rG3WFy343tr9Yqay2qasIk3Z5JrZ8Cr1S qFWrKr1xuF4kArXCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Yb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVWxJVW8Jr1l84ACjcxK6I8E87Iv6xkF7I0E14v2 6r4UJVWxJr1ln4kS14v26r1Y6r17M2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q 6rW5McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1l4IxYO2xFxVAFwI0_ Jrv_JF1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8GjcxK67AKxVWUGVWUWwC2zVAF1V AY17CE14v26r126r1DMIIYrxkI7VAKI48JMIIF0xvE2Ix0cI8IcVAFwI0_Xr0_Ar1lIxAI cVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCwCI42 IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIev Ja73UjIFyTuYvjxU4AhLUUUUU Content-Type: text/plain; charset="utf-8" When compling with Clang on LoongArch, there are unsorted table offset of rodata if there exist many jump tables, it will get the wrong table end and find the wrong jump destination instructions in add_jump_table(). Sort the rodata table offset by parsing ".rela.discard.tablejump_annotate" and then get each table size of rodata corresponded with each table jump instruction, it is used to check the table end and will break the process when parsing ".rela.rodata" to avoid getting the wrong jump destination instructions. Signed-off-by: Tiezhu Yang --- tools/objtool/arch/loongarch/special.c | 72 ++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/lo= ongarch/special.c index 9bba1e9318e0..454bd01226a4 100644 --- a/tools/objtool/arch/loongarch/special.c +++ b/tools/objtool/arch/loongarch/special.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include +#include =20 bool arch_support_alt_relocation(struct special_alt *special_alt, struct instruction *insn, @@ -8,6 +9,77 @@ bool arch_support_alt_relocation(struct special_alt *speci= al_alt, return false; } =20 +struct table_info { + struct list_head jump_info; + unsigned long insn_offset; + unsigned long rodata_offset; +}; + +static void get_rodata_table_size_by_table_annotate(struct objtool_file *f= ile, + struct instruction *insn) +{ + struct section *rsec; + struct reloc *reloc; + struct list_head table_list; + struct table_info *orig_table; + struct table_info *next_table; + unsigned long tmp_insn_offset; + unsigned long tmp_rodata_offset; + + rsec =3D find_section_by_name(file->elf, ".rela.discard.tablejump_annotat= e"); + if (!rsec) + return; + + INIT_LIST_HEAD(&table_list); + + for_each_reloc(rsec, reloc) { + if (reloc->sym->type !=3D STT_SECTION) + return; + + orig_table =3D malloc(sizeof(struct table_info)); + if (!orig_table) { + WARN("malloc failed"); + return; + } + + orig_table->insn_offset =3D reloc_addend(reloc); + reloc++; + orig_table->rodata_offset =3D reloc_addend(reloc); + + list_add_tail(&orig_table->jump_info, &table_list); + + if (reloc_idx(reloc) + 1 =3D=3D sec_num_entries(rsec)) + break; + } + + list_for_each_entry(orig_table, &table_list, jump_info) { + next_table =3D list_next_entry(orig_table, jump_info); + list_for_each_entry_from(next_table, &table_list, jump_info) { + if (next_table->rodata_offset < orig_table->rodata_offset) { + tmp_insn_offset =3D next_table->insn_offset; + tmp_rodata_offset =3D next_table->rodata_offset; + next_table->insn_offset =3D orig_table->insn_offset; + next_table->rodata_offset =3D orig_table->rodata_offset; + orig_table->insn_offset =3D tmp_insn_offset; + orig_table->rodata_offset =3D tmp_rodata_offset; + } + } + } + + list_for_each_entry(orig_table, &table_list, jump_info) { + if (insn->offset =3D=3D orig_table->insn_offset) { + next_table =3D list_next_entry(orig_table, jump_info); + if (&next_table->jump_info =3D=3D &table_list) + break; + + while (next_table->rodata_offset =3D=3D orig_table->rodata_offset) + next_table =3D list_next_entry(next_table, jump_info); + + insn->table_size =3D next_table->rodata_offset - orig_table->rodata_off= set; + } + } +} + struct reloc *arch_find_switch_table(struct objtool_file *file, struct instruction *insn) { --=20 2.42.0 From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 86B3215531B for ; Fri, 22 Nov 2024 04:50:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251029; cv=none; b=iJD1MCkZ5PvRLgpNG6mih0WB/k0af4ixTmQc3O0FvwhjmVd2HqXM7Wit1ZTBEbtKSsweuPL2oDhnAQuywASVNicc3mT5WhsyS+J9hlcpHTICeb1gIAH1u79r+Rzgp46zLnviROmkFEzVOWEhrt9/nqb00tlGnNiYiHzezwamZfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251029; c=relaxed/simple; bh=D1q4dogKpbEe70M0j/62mC1peBu7voO7u8TUcPSfQ74=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=TxDl4EJYIjjjujlatCk+esK0P3mxjLIQJxl4AX1HT7juqQuqiV9gvLbPLvjd1h3DMr7sV2FGJswmyLfJH4gWJfO5iLm7dmOjdc6p+RUDR6zLtleq6X8HWoH/GI0ee/mvoi+9dET5vE+H672bcEuLIxguQIMMT+OOurrQF0GVdcA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8CxSOGQDUBnEb1FAA--.5038S3; Fri, 22 Nov 2024 12:50:24 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S10; Fri, 22 Nov 2024 12:50:21 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 08/10] objtool/LoongArch: Add support for switch table Date: Fri, 22 Nov 2024 12:50:03 +0800 Message-ID: <20241122045005.14617-9-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S10 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCw4rWw18Wr1fGr1kCw4kuFX_yoWrXFWDpF W3C345Kr45XryfWw43tFW09ryakws3WFy7JrZrGr95Aw42qF1rtanayr9IyFn5Ars0yr4I qr4fKrW8CF4DAabCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWr XVW3AwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v2 6r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17 CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26ryj6F1UMIIF 0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU0E38UUUUUU== Content-Type: text/plain; charset="utf-8" The objtool program needs to analysis the control flow of each object file generated by compiler toolchain, it needs to know all the locations that a branch instruction may jump into, if a jump table is used, objtool has to correlate the jump instruction with the table. On x86 which is the only port supported by objtool before LoongArch, there is a relocation on the jump instruction and to the table directly. But on LoongArch, the relocation is on some kind of instruction prior to the jump instruction, and also with scheduling it is not easy to tell the offset of that instruction from the jump instruction. Furthermore, because LoongArch has -fsection-anchors (often enabled at -O1 or above) the relocation may actually points to a section anchor instead of the table itself. The good news is that after continuous analysis and discussions, at last a GCC patch "LoongArch: Add support to annotate tablejump" and a Clang patch "[LoongArch] Add options for annotate tablejump" have been merged into the upstream mainline, the compiler changes make life much easier for switch table support of objtool on LoongArch. By now, there is an additional section ".discard.tablejump_annotate" to store the jump info as pairs of addresses, each pair contains the address of jump instruction and the address of jump table. In order to find switch table, it is easy to parse the relocation section ".rela.discard.tablejump_annotate" to get table_sec and table_offset, the rest process is somehow like x86. Link: https://gcc.gnu.org/git/?p=3Dgcc.git;a=3Dcommit;h=3D0ee028f55640 Link: https://github.com/llvm/llvm-project/commit/4c2c17756739 Signed-off-by: Tiezhu Yang --- tools/objtool/arch/loongarch/special.c | 60 +++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/lo= ongarch/special.c index 454bd01226a4..366517deb35b 100644 --- a/tools/objtool/arch/loongarch/special.c +++ b/tools/objtool/arch/loongarch/special.c @@ -1,4 +1,5 @@ // SPDX-License-Identifier: GPL-2.0-or-later +#include #include #include =20 @@ -80,8 +81,65 @@ static void get_rodata_table_size_by_table_annotate(stru= ct objtool_file *file, } } =20 +static struct reloc *find_reloc_by_table_annotate(struct objtool_file *fil= e, + struct instruction *insn) +{ + struct section *rsec; + struct reloc *reloc; + unsigned long offset; + + rsec =3D find_section_by_name(file->elf, ".rela.discard.tablejump_annotat= e"); + if (!rsec) + return NULL; + + for_each_reloc(rsec, reloc) { + if (reloc->sym->sec->rodata) + continue; + + if (strcmp(insn->sec->name, reloc->sym->sec->name)) + continue; + + if (reloc->sym->type =3D=3D STT_SECTION) + offset =3D reloc_addend(reloc); + else + offset =3D reloc->sym->offset; + + if (insn->offset =3D=3D offset) { + get_rodata_table_size_by_table_annotate(file, insn); + reloc++; + return reloc; + } + } + + return NULL; +} + struct reloc *arch_find_switch_table(struct objtool_file *file, struct instruction *insn) { - return NULL; + struct reloc *annotate_reloc; + struct reloc *rodata_reloc; + struct section *table_sec; + unsigned long table_offset; + + annotate_reloc =3D find_reloc_by_table_annotate(file, insn); + if (!annotate_reloc) + return NULL; + + table_sec =3D annotate_reloc->sym->sec; + if (annotate_reloc->sym->type =3D=3D STT_SECTION) + table_offset =3D reloc_addend(annotate_reloc); + else + table_offset =3D annotate_reloc->sym->offset; + + /* + * Each table entry has a rela associated with it. The rela + * should reference text in the same function as the original + * instruction. + */ + rodata_reloc =3D find_reloc_by_dest(file->elf, table_sec, table_offset); + if (!rodata_reloc) + return NULL; + + return rodata_reloc; } --=20 2.42.0 From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D9BDE15B13C for ; Fri, 22 Nov 2024 04:50:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251030; cv=none; b=K2kGSuWwqGGfqz4XxP7MGdKj+Z6jjyc9fC+DDrsvTtsjzIsfo4XBUbNdFuo1/2ebrbjiklQDmm0Jty61IUA726ROUyHgv+0PH2XEzHMTHNjV1/EpfWvUAKzNyv+SKNghNpZGNTx98irQ3xRbRtAg3GMWNNUlR60itjwla6eGH5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251030; c=relaxed/simple; bh=vo+nyHm4J5Y/nSI2tKzgttln0FQjBwZYWDnBlKp3wlU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Xn9UcFgIvSIkAgx6NDezFXjpivRxBVYsM6A8SPFZA8CHwI5mKTwRfiD99ux9CSQa05Q/YaJotDImkNESVlc/UElvm2THzBp057eO/pqujF6yNhulvn3RZQ8AoqIA5gBtvPXmQAwOwLyDdQNjMd5RJVYNeU34Glwo1I6H7tWfalo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8Dx2uGQDUBnFb1FAA--.7363S3; Fri, 22 Nov 2024 12:50:24 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S11; Fri, 22 Nov 2024 12:50:24 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 09/10] objtool/LoongArch: Add support for goto table Date: Fri, 22 Nov 2024 12:50:04 +0800 Message-ID: <20241122045005.14617-10-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S11 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCw4rWw18Wr1fGr1kCw4kuFX_yoWrGryfpF W3C345Kr45Xr4Sgr47tay09ryaya1IgFy7GrWDt34rAw43Xr1rtF1SqF9IyFy8urZ5Ar4I qr4rKr48CF4DAacCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWr XVW3AwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v2 6r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17 CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26ryj6F1UMIIF 0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU0yxRDUUUUU== Content-Type: text/plain; charset="utf-8" The objtool program needs to analysis the control flow of each object file generated by compiler toolchain, it needs to know all the locations that a branch instruction may jump into, if a jump table is used, objtool has to correlate the jump instruction with the table. On x86 which is the only port supported by objtool before LoongArch, there is a relocation on the jump instruction and to the table directly. But on LoongArch, the relocation is on some kind of instruction prior to the jump instruction, and also with scheduling it is not easy to tell the offset of that instruction from the jump instruction. Furthermore, because LoongArch has -fsection-anchors (often enabled at -O1 or above) the relocation may actually points to a section anchor instead of the table itself. For the jump table of switch cases, a GCC patch "LoongArch: Add support to annotate tablejump" and a Clang patch "[LoongArch] Add options for annotate tablejump" have been merged into the upstream mainline, it can parse the additional section ".discard.tablejump_annotate" which stores the jump info as pairs of addresses, each pair contains the address of jump instruction and the address of jump table. For the jump table of computed gotos, it is indeed not easy to implement in the compiler, especially if there is more than one computed goto in a function such as ___bpf_prog_run(). objdump kernel/bpf/core.o shows that there are many table jump instructions in ___bpf_prog_run(), but there are no relocations on the table jump instructions and to the table directly on LoongArch. Without the help of compiler, in order to figure out the address of goto table for the special case of ___bpf_prog_run(), since the instruction sequence is relatively single and stable, it makes sense to add a helper find_reloc_of_rodata_c_jump_table() to find the relocation which points to the section ".rodata..c_jump_table". If find_reloc_by_table_annotate() failed, it means there is no relocation info of switch table address in ".rela.discard.tablejump_annotate", then objtool may find the relocation info of goto table ".rodata..c_jump_table" with find_reloc_of_rodata_c_jump_table(). Signed-off-by: Tiezhu Yang --- tools/objtool/arch/loongarch/special.c | 28 ++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/lo= ongarch/special.c index 366517deb35b..c5f4217df3a7 100644 --- a/tools/objtool/arch/loongarch/special.c +++ b/tools/objtool/arch/loongarch/special.c @@ -114,6 +114,27 @@ static struct reloc *find_reloc_by_table_annotate(stru= ct objtool_file *file, return NULL; } =20 +static struct reloc *find_reloc_of_rodata_c_jump_table(struct section *sec, + unsigned long offset) +{ + struct section *rsec; + struct reloc *reloc; + + rsec =3D sec->rsec; + if (!rsec) + return NULL; + + for_each_reloc(rsec, reloc) { + if (reloc_offset(reloc) > offset) + break; + + if (!strncmp(reloc->sym->sec->name, ".rodata..c_jump_table", 21)) + return reloc; + } + + return NULL; +} + struct reloc *arch_find_switch_table(struct objtool_file *file, struct instruction *insn) { @@ -123,8 +144,11 @@ struct reloc *arch_find_switch_table(struct objtool_fi= le *file, unsigned long table_offset; =20 annotate_reloc =3D find_reloc_by_table_annotate(file, insn); - if (!annotate_reloc) - return NULL; + if (!annotate_reloc) { + annotate_reloc =3D find_reloc_of_rodata_c_jump_table(insn->sec, insn->of= fset); + if (!annotate_reloc) + return NULL; + } =20 table_sec =3D annotate_reloc->sym->sec; if (annotate_reloc->sym->type =3D=3D STT_SECTION) --=20 2.42.0 From nobody Sat Nov 23 16:23:14 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F3B4B15FD16 for ; Fri, 22 Nov 2024 04:50:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251029; cv=none; b=XWg32I7sirfUlSpYwZCkDQ26oQ2Tuqv3sZBBjdkCSKEgbcYuXk4l4R4Cnx9lYtjJ2dJ5SImjbH8qxlqovlu69843kBNGfxtLDrHbWhVwlOz3GXJIe4VUQ2241Cb5VN+ORuU13aIf19bBUZLttVbdNMxV8xx4gZfySzdMUbLIC1g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1732251029; c=relaxed/simple; bh=hpEJE6452kJP+gGj5WwMyHNX5Dc1z8fXf0Rr7Lf363M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NkC/CM2mxuRESs4N8yMxIfbREW3awn/z9zTzdFy5Xc3E2rnYx0gtO3QAubgl40jvijNGuN6EvgmUqmyhgAsCWim/GSvTeezh3n2YK24puavWCoQk4G1K0q434bFCgH/z15H4P1VlzbPIqr1JWfmLLTty04FBcbQXMjFxiX2/Fww= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8CxSOGRDUBnGb1FAA--.5039S3; Fri, 22 Nov 2024 12:50:25 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMCxdcCIDUBnIJJiAA--.33733S12; Fri, 22 Nov 2024 12:50:24 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v4 10/10] LoongArch: Enable jump table for objtool Date: Fri, 22 Nov 2024 12:50:05 +0800 Message-ID: <20241122045005.14617-11-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241122045005.14617-1-yangtiezhu@loongson.cn> References: <20241122045005.14617-1-yangtiezhu@loongson.cn> 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 X-CM-TRANSID: qMiowMCxdcCIDUBnIJJiAA--.33733S12 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoW7uFWDuryUCw13Ww1kuryUJwc_yoW8Wr45pw s7Zr4kKw4kXF4vqrZrJrWSgrZ8trnrKr43WF429a4kArW7Z345Zr48ta9rW3WUCws5JrWI qFWfGa4avFWUJ3XCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Ib4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Ar0_tr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIEc7CjxVAF wI0_Gr1j6F4UJwAaw2AFwI0_Jrv_JF1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqjxCEc2 xF0cIa020Ex4CE44I27wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_ Wrv_ZF1lYx0Ex4A2jsIE14v26r4j6F4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x 0EwIxGrwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwCFI7km07C267AK xVWUXVWUAwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVW7JVWDJwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Cr0_Gr1UMIIF0xvE42xK8VAvwI8IcIk0rVWUJVWUCw CI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E14v26r4j6r4UJbIYCTnI WIevJa73UjIFyTuYvjxUVCD7UUUUU Content-Type: text/plain; charset="utf-8" For now, it is time to remove -fno-jump-tables to enable jump table for objtool if the compiler has -mannotate-tablejump, otherwise it is better to remain -fno-jump-tables to keep compatibility with the older compilers. Signed-off-by: Tiezhu Yang --- arch/loongarch/Kconfig | 3 +++ arch/loongarch/Makefile | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index bb35c34f86d2..500ee9b2cd88 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -284,6 +284,9 @@ config AS_HAS_LBT_EXTENSION config AS_HAS_LVZ_EXTENSION def_bool $(as-instr,hvcl 0) =20 +config CC_HAS_ANNOTATE_TABLEJUMP + def_bool $(cc-option,-mannotate-tablejump) + menu "Kernel type and options" =20 source "kernel/Kconfig.hz" diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile index ae3f80622f4c..6b84e633b8ce 100644 --- a/arch/loongarch/Makefile +++ b/arch/loongarch/Makefile @@ -101,8 +101,12 @@ KBUILD_AFLAGS +=3D $(call cc-option,-mthin-add-sub) = $(call cc-option,-Wa$(comma) KBUILD_CFLAGS +=3D $(call cc-option,-mthin-add-sub) $(call cc-option,-Wa= $(comma)-mthin-add-sub) =20 ifdef CONFIG_OBJTOOL +ifdef CONFIG_CC_HAS_ANNOTATE_TABLEJUMP +KBUILD_CFLAGS +=3D $(call cc-option,-mannotate-tablejump) +else KBUILD_CFLAGS +=3D -fno-jump-tables endif +endif =20 KBUILD_RUSTFLAGS +=3D --target=3Dloongarch64-unknown-none-softfloat KBUILD_RUSTFLAGS_KERNEL +=3D -Zdirect-access-external-data=3Dyes --=20 2.42.0