From nobody Wed Nov 27 02:40:54 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 7F8E01E8855; Tue, 15 Oct 2024 11:39:21 +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=1728992364; cv=none; b=pvUZun5s63qp4g4iBnq+WjdSpUJ91leJsTzaFw7sTX7164EVfkz/xtr3BceQ13tnKxvLwJ854D/nEvrOT4I/2DIkwSVn+9oJ08wn9osRWusL3qaAI9e8FaKj2HcBY0K4Y4tbwXPcpvdJ9fBCG9F5LEGwfjYFSJpo3jmLdWmhLe8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728992364; c=relaxed/simple; bh=BMzaHd/KZvfQw6XtDmSYagMqPYpWkf/ORu4doTVMsno=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=L31nGz0fm39I2QC1leyIS/X2jSBZ/dr31jpkFdDRPTSiAv8xQkDHK543dyW6CN57P/b4pCPNl9kUqhhIyC7P6GtcMTbQl4Oo6v8YE+aY/mYF3uAWNDVH6u4O1clseqt581UicjLFGotGzbrzCN3FruPvoO62mBUYJ3B976glScI= 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 _____8DxfWtnVA5n7n8dAA--.42285S3; Tue, 15 Oct 2024 19:39:19 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front2 (Coremail) with SMTP id qciowMCxbcdkVA5n6E0uAA--.9583S4; Tue, 15 Oct 2024 19:39:18 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v1 2/6] objtool/LoongArch: Add support for jump table Date: Tue, 15 Oct 2024 19:39:11 +0800 Message-ID: <20241015113915.12623-3-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241015113915.12623-1-yangtiezhu@loongson.cn> References: <20241015113915.12623-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: qciowMCxbcdkVA5n6E0uAA--.9583S4 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCw4rWw4UXF1UCw1xXr4fJFc_yoW5KrWkpF W7C345Kr45XryfWw43ta18uryakws7WFy7Gry3Kr93Aw4aqF1rtFsayFn0yF15ursYyr4I qr4fKr4UCF4DAabCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU tVWrXwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v2 6r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17 CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryUMIIF 0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU8EeHDUUUUU== Content-Type: text/plain; charset="utf-8" The objtool program needs to analysis the control flow of each object file generated by compiler toolchain, 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, the relocation is directly on the jump instruction and to the table. But on LoongArch, the relocation is on some kind of instruction prior to the jump instruction, with scheduling it is not easy to tell the offset of that instruction from the jump instruction. Furthermore, because LoongArch has -fsection-anchors the relocation may actually points to a section anchor instead of the table itself. So it needs some non-trivial data flow analysis in objtool without special support of compiler. The good news is that the GCC patch "LoongArch: Add support to annotate tablejump" has been merged into the upstream mainline, the changes are very trivial in GCC, it makes life much easier for jump 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 Signed-off-by: Tiezhu Yang --- tools/objtool/arch/loongarch/special.c | 53 +++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/lo= ongarch/special.c index 9bba1e9318e0..65b1ed297d57 100644 --- a/tools/objtool/arch/loongarch/special.c +++ b/tools/objtool/arch/loongarch/special.c @@ -1,4 +1,6 @@ // SPDX-License-Identifier: GPL-2.0-or-later +#include + #include =20 bool arch_support_alt_relocation(struct special_alt *special_alt, @@ -8,8 +10,57 @@ bool arch_support_alt_relocation(struct special_alt *spec= ial_alt, return false; } =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; + + offset =3D reloc->sym->offset; + if (insn->offset =3D=3D offset) { + 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; + 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