From nobody Sun Nov 24 11:53:53 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5BE591D5AAC for ; Tue, 5 Nov 2024 12:39:11 +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=1730810354; cv=none; b=pJ/jbpdNF+l22K77XW6/Ag9Rvm9eogFQxzNz29Nrds4zQF3OjyuCRwi87wpaYPYXCYu7R56715hnoP8YMnOf9oYCBy/LM3Y3rIRVhw6THsxzJtNC+xEQEftHy/3dC7b/bRqsq7bHq2Afs4rjTP7vsjWvnnysuxKBt2bvommE/IU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730810354; c=relaxed/simple; bh=WQBjV7XvQFc38Q87Jv1cXvfn27Td+pGBvefYsoaVrgk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QHY5yMDdk+zyCxGgXMZANAQWi9GX8cI6V8hw1rIRM5xBXgYJ+HO4kBpsMeLvgHZP7N3PJsXN0r1Vv8En02MfMwjeHSISjdXiBfNmCtDWR/U2gJVI2jmJrNzGVO4SziZ6/394e1rk7nvEY69sUbUZAs9QE0Sm+OyCE0IxKVmWoyk= 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 _____8AxquDuESpnhLUxAA--.34568S3; Tue, 05 Nov 2024 20:39:10 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMBxrsLsESpnw4lCAA--.26454S3; Tue, 05 Nov 2024 20:39:09 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/5] objtool: Handle various symbol types for rodata Date: Tue, 5 Nov 2024 20:39:02 +0800 Message-ID: <20241105123906.26072-2-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241105123906.26072-1-yangtiezhu@loongson.cn> References: <20241105123906.26072-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: qMiowMBxrsLsESpnw4lCAA--.26454S3 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxJFWDKF4kAF4kGFWfGw18Xrc_yoW5Ww4xpF sxG3y5Kr45Xr1fWws7tFW0gF9xGws7WrnrJ39rKr1rA3sIqF1rKFWSkr1ay3WDGr1Fva17 XF1Y9ry7uF4UA3gCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Yb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r106r15M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Gr0_Xr1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU tVWrXwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v2 6r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17 CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r1I6r4UMIIF 0xvE2Ix0cI8IcVCY1x0267AKxVWUJVW8JwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Jr0_Gr1lIxAIcVC2z280aVCY1x0267AKxVWUJVW8JbIYCTnIWIev Ja73UjIFyTuYvjxU4miiDUUUU 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 Sun Nov 24 11:53:53 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A72941D5AA8 for ; Tue, 5 Nov 2024 12:39:12 +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=1730810354; cv=none; b=gYrALtvdCRU8l/bxvhiIwPLkvSbRLtGW6ZAZY58d3y0WtZlENGXpRp3Zt+eg/VBEUKgYSmJM9ZKSJDvvquu7KyfjpBX1L+3cSDWUG1oWsosyoCJbro+nhTzYrV1DJ8V9Ku/HTudie2ogSLDVwkbIdZB9i11qK0lMdXnNpxRfHH4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730810354; c=relaxed/simple; bh=WuYh2QNbB1DXYMDkZ4MpKfVONFKDR2ri1ITDB4mHoqs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G8M0SVXCBz7cXBk9z8BrFaP07JsGeFhxRbAcdIsqN6Yb0SOFupMBDrgNtROl6IiOo0Euwetw+8a5JWzKnAtwYXx14S8+oDPD7XClHpBAvTSawrpxmzw+EOjfqd2zeEsZz0PQLEqE+SfOM5jXsP8EFK5UD0jIJ/LR9M8F4Bhhti8= 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 _____8AxquHuESpnirUxAA--.36195S3; Tue, 05 Nov 2024 20:39:10 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMBxrsLsESpnw4lCAA--.26454S4; Tue, 05 Nov 2024 20:39:10 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/5] objtool: Handle special cases of discard.reachable Date: Tue, 5 Nov 2024 20:39:03 +0800 Message-ID: <20241105123906.26072-3-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241105123906.26072-1-yangtiezhu@loongson.cn> References: <20241105123906.26072-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: qMiowMBxrsLsESpnw4lCAA--.26454S4 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoW7CF4kuryfGr4DKr17uw47ZFc_yoW8WF4Dpa y5Cay3Jr4DXr48W3W7Wws5WryfKw4fGrW8XryfJ34UK3s8Xwn5KFn3tay2v3Zxua1ag3Wa qFsI934UZF1DArcCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 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" 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 | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 9601235e908d..6607cd56459b 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -711,6 +711,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 Sun Nov 24 11:53:53 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CEDAB1D5AB7 for ; Tue, 5 Nov 2024 12:39:13 +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=1730810356; cv=none; b=Gd5d+ACpoX00+DJsVWFAXaQPMj6TdaG4LZ8wSfW0VyQf2ky5Xj2WtmByI+rgfKZhtATgzMfKC1KtyZIwdFX3Gw4sx5yYji2Jkgyvy7QKPCcEnucEn3fnbWfDgyXWBZvoylqzDxrtCPsRs7yiEPOD6xWEAoKbsZE0+CgTWHDJho8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730810356; c=relaxed/simple; bh=1P9y8x6CeepxT8JghLYZnzdwyIS2oAFeo31CZBM8/1I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R5+lm6G5qwa13p+R6s2eePfcb3iEeYuVOxbAtioNHI3e273DDNFpJvB+ipEf6647Ok4LDUeG4kxE2OceAU5n2U9+e7FIGE/lTAP/txVtZlUSseIQM+5D+BgKV5F5NvZxM1FIdFEtkCz9BO4rKG5gu1aIdfx9nxS+TbE0lez7Zws= 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_wESpnkrUxAA--.10283S3; Tue, 05 Nov 2024 20:39:12 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMBxrsLsESpnw4lCAA--.26454S5; Tue, 05 Nov 2024 20:39:10 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 3/5] objtool/LoongArch: Add support for switch table Date: Tue, 5 Nov 2024 20:39:04 +0800 Message-ID: <20241105123906.26072-4-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241105123906.26072-1-yangtiezhu@loongson.cn> References: <20241105123906.26072-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: qMiowMBxrsLsESpnw4lCAA--.26454S5 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCw4rWw18Wr1fGr1kCw4kuFX_yoWrGr1DpF W7C345Kr45XryfWw13ta18uryakws3WFy7Jr9xKr93Aw42qF1rtFs3tFn0yF15Crs0yr4I qr4fKr4UCF4DAabCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 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, 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. There is a GCC patch "LoongArch: Add support to annotate tablejump" which has been merged into the upstream mainline, the changes are very trivial in GCC, it makes 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 Signed-off-by: Tiezhu Yang --- tools/objtool/arch/loongarch/special.c | 59 +++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/tools/objtool/arch/loongarch/special.c b/tools/objtool/arch/lo= ongarch/special.c index 9bba1e9318e0..31e0f94e31e8 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 =20 bool arch_support_alt_relocation(struct special_alt *special_alt, @@ -8,8 +9,64 @@ bool arch_support_alt_relocation(struct special_alt *speci= al_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; + + 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) { + 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 Sun Nov 24 11:53:53 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AA6FF1D5CD7 for ; Tue, 5 Nov 2024 12:40:31 +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=1730810433; cv=none; b=Ll2ojV4HPZa7bcvDLogZbVSU9W+U6II/a0EOxUfj639H7PhMSAiSTqhhz4l+dkhGAwxU2/I8XooRTRbk3OcaMWGcl3OHgzIW88pyA+P///++6oNYELhz+3GMzFBbnGs1TF9YB62lO0FZWHZ45TDLImj6AtTIiGENO4u2CGragXI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730810433; c=relaxed/simple; bh=AJwHgU1lPUIK2aReyiUYS5P69puXM2GY8DxuNND1mWs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=C2KfdEigcwbc5UxA+uxUJ53gBxDNSxabYnfppGZEJxYFp241sFK4KzimbcjgMfEzPl5PmV/cwJFR3n+uhejupHObqSfx8L9tNvyUiSmRlNtRrIp9tLccK9GmYqJQkJsC2B15VQBgezKHC4UMpkBzpOItWn7PVkTTtlmJWLbUW5c= 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 _____8AxbeLwESpnl7UxAA--.34025S3; Tue, 05 Nov 2024 20:39:12 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMBxrsLsESpnw4lCAA--.26454S6; Tue, 05 Nov 2024 20:39:12 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 4/5] objtool/LoongArch: Add support for goto table Date: Tue, 5 Nov 2024 20:39:05 +0800 Message-ID: <20241105123906.26072-5-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241105123906.26072-1-yangtiezhu@loongson.cn> References: <20241105123906.26072-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: qMiowMBxrsLsESpnw4lCAA--.26454S6 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoWxCw4rWw18Wr1fGr1kCw4kuFX_yoWrJw1UpF W3C345Kr45Xr1fWr47tay09ryaya1Igry7GrWDt348Aw43Xr1FyF1xtF90yFW8Wrs5Ar4I qr1rKr4UCF4DAacCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r106r15M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU tVWrXwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v2 6r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17 CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26r4j6ryUMIIF 0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU8N6pPUUUUU== 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" has been merged into the upstream mainline, it makes life much easier with 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 31e0f94e31e8..fe7413bcd98b 100644 --- a/tools/objtool/arch/loongarch/special.c +++ b/tools/objtool/arch/loongarch/special.c @@ -41,6 +41,27 @@ static struct reloc *find_reloc_by_table_annotate(struct= 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) { @@ -50,8 +71,11 @@ struct reloc *arch_find_switch_table(struct objtool_file= *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 Sun Nov 24 11:53:53 2024 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6D9A71D54FE for ; Tue, 5 Nov 2024 12:40:31 +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=1730810433; cv=none; b=WlVyY/pYgPuD0mqTQBBnjaroKhljdgAU3tYuLjSjioBc/Z2fb2u+c5oG9weUUuZi7cWKu0fGPu2atDO/hyunS936NmD9nE00gy/w3JkdNcAKGx/fmqvANQXzdUbifR8EvazeAo82sl9sMypmRYIsitIlr6itQUkl8hyN217GBM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730810433; c=relaxed/simple; bh=Rwqaw/Ie96v4mk5XllciCpWVfbKETe4TlSqOSrnJlYI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kJGNuremgn+C8TnF26OppW1C+0nViGq6HKCuh5zpPqgSIikbsXofguACloyqYZS69JMrXRXWbECZivrlaXCgT6Q3Yt+3F/SnnKzd2CO/oKxrEzTbX77BzRufpg6PQWN3EYflqla880PFlGR3C9qHeJBp+16duHc2VfvuHnEI+l0= 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 _____8CxPuPxESpnm7UxAA--.33729S3; Tue, 05 Nov 2024 20:39:13 +0800 (CST) Received: from linux.localdomain (unknown [113.200.148.30]) by front1 (Coremail) with SMTP id qMiowMBxrsLsESpnw4lCAA--.26454S7; Tue, 05 Nov 2024 20:39:12 +0800 (CST) From: Tiezhu Yang To: Huacai Chen , Josh Poimboeuf , Peter Zijlstra Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org Subject: [PATCH v2 5/5] LoongArch: Enable jump table with GCC for objtool Date: Tue, 5 Nov 2024 20:39:06 +0800 Message-ID: <20241105123906.26072-6-yangtiezhu@loongson.cn> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20241105123906.26072-1-yangtiezhu@loongson.cn> References: <20241105123906.26072-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: qMiowMBxrsLsESpnw4lCAA--.26454S7 X-CM-SenderInfo: p1dqw3xlh2x3gn0dqz5rrqw2lrqou0/ X-Coremail-Antispam: 1Uk129KBj93XoW7CF48AFy5Aw17Kw13Aw4kZrc_yoW8ZF43p3 ykur1DKr4kXFs7trZrJrWSgr98Jrnrtr13WF47ta48ArW7Z345Zr4Iya9rWFy5Cws5JrWI qFWfKFyayF4UA3XCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Gr0_Cr1l84ACjcxK6I8E87Iv67AKxVW8Jr0_Cr1UM28EF7xvwVC2z280aVCY1x0267AK xVW8Jr0_Cr1UM2kKe7AKxVWUXVWUAwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07 AIYIkI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWU tVWrXwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7V AKI48JMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j6r4UMxCIbckI1I0E14v2 6r1Y6r17MI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwVAFwI0_JrI_JrWlx4CE17 CEb7AF67AKxVWUAVWUtwCIc40Y0x0EwIxGrwCI42IY6xIIjxv20xvE14v26ryj6F1UMIIF 0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4v20xvaj40_Jr0_JF4lIx AIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8JrUvcSsGvfC2 KfnxnUUI43ZEXa7IU8EeHDUUUUU== Content-Type: text/plain; charset="utf-8" For now, it is time to remove the compiler option -fno-jump-tables to enable jump table for objtool if the compiler is GCC and it has the compiler option -mannotate-tablejump, otherwise still keep the compiler option -fno-jump-tables to maintain compatibility with the older compilers. By the way, the compiler behaviors are different for various archs, there are some corner issues after removing -fno-jump-tables if the compiler is Clang, so just keep the compiler option -fno-jump-tables for Clang at present. Signed-off-by: Tiezhu Yang --- arch/loongarch/Kconfig | 3 +++ arch/loongarch/Makefile | 9 +++++++++ 2 files changed, 12 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..61484df4eccc 100644 --- a/arch/loongarch/Makefile +++ b/arch/loongarch/Makefile @@ -101,8 +101,17 @@ 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_IS_GCC +ifdef CONFIG_CC_HAS_ANNOTATE_TABLEJUMP +KBUILD_CFLAGS +=3D $(call cc-option,-mannotate-tablejump) +else KBUILD_CFLAGS +=3D -fno-jump-tables endif +endif +ifdef CONFIG_CC_IS_CLANG +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