From nobody Mon Feb 9 09:01:31 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 29FA2EB64DC for ; Tue, 11 Jul 2023 16:09:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233044AbjGKQJD (ORCPT ); Tue, 11 Jul 2023 12:09:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232559AbjGKQI4 (ORCPT ); Tue, 11 Jul 2023 12:08:56 -0400 Received: from pegase1.c-s.fr (pegase1.c-s.fr [93.17.236.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1704E10EA for ; Tue, 11 Jul 2023 09:08:55 -0700 (PDT) Received: from localhost (mailhub3.si.c-s.fr [192.168.12.233]) by localhost (Postfix) with ESMTP id 4R0m5D0nkTz9sFT; Tue, 11 Jul 2023 18:08:48 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 9wCSTBZkkZoi; Tue, 11 Jul 2023 18:08:48 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 4R0m5C2NQTz9sFD; Tue, 11 Jul 2023 18:08:47 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 4DAF58B77A; Tue, 11 Jul 2023 18:08:47 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id u9tlFbpuG58J; Tue, 11 Jul 2023 18:08:47 +0200 (CEST) Received: from PO20335.IDSI0.si.c-s.fr (unknown [192.168.233.184]) by messagerie.si.c-s.fr (Postfix) with ESMTP id C99388B763; Tue, 11 Jul 2023 18:08:46 +0200 (CEST) Received: from PO20335.IDSI0.si.c-s.fr (localhost [127.0.0.1]) by PO20335.IDSI0.si.c-s.fr (8.17.1/8.16.1) with ESMTPS id 36BG8jU73696836 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 11 Jul 2023 18:08:45 +0200 Received: (from chleroy@localhost) by PO20335.IDSI0.si.c-s.fr (8.17.1/8.17.1/Submit) id 36BG8jnx3696835; Tue, 11 Jul 2023 18:08:45 +0200 X-Authentication-Warning: PO20335.IDSI0.si.c-s.fr: chleroy set sender to christophe.leroy@csgroup.eu using -f From: Christophe Leroy To: Michael Ellerman , Nicholas Piggin , Josh Poimboeuf , Peter Zijlstra , Sathvika Vasireddy , Naveen N Rao Cc: Christophe Leroy , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: [PATCH v4 09/15] objtool: Find end of switch table directly Date: Tue, 11 Jul 2023 18:08:35 +0200 Message-ID: <0465b564b9ac13b801fcd00b8395214050dc0d83.1689091394.git.christophe.leroy@csgroup.eu> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1689091706; l=3695; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=XTf90OFsPllQCIzbIedSyHe0CqF3MMBuqTFyPQCeHPM=; b=+b6uGgE+bj1HDE28CR5o2blLXJm7eAOWbdzQOfKHLBsbneDL3YVxpbv5H7EqG7iFgDiCanATi KhbORFM7AG6BbrkfkcF3++jcBjno+dBirbu5w7L9WX+WDBU6EpGzkez X-Developer-Key: i=christophe.leroy@csgroup.eu; a=ed25519; pk=HIzTzUj91asvincQGOFx6+ZF5AoUuP9GdOtQChs7Mm0= Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" At the time being, the end of a switch table can only be known once the start of the following switch table has ben located. This is a problem when switch tables are nested because until the first switch table is properly added, the second one cannot be located as a the backward walk will abut on the dynamic switch of the previous one. So perform a first forward walk in the code in order to locate all possible relocations to switch tables and build a local table with those relocations. Later on once one switch table is found, go through this local table to know where next switch table starts. Signed-off-by: Christophe Leroy --- tools/objtool/check.c | 62 ++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 16 deletions(-) diff --git a/tools/objtool/check.c b/tools/objtool/check.c index be413c578588..361c832aefc8 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -2094,14 +2094,30 @@ static struct reloc *find_jump_table(struct objtool= _file *file, return NULL; } =20 +static struct reloc *find_next_table(struct instruction *insn, + struct reloc **table, unsigned int size) +{ + unsigned long offset =3D reloc_offset(insn_jump_table(insn)); + int i; + struct reloc *reloc =3D NULL; + + for (i =3D 0; i < size; i++) { + if (reloc_offset(table[i]) > offset && + (!reloc || reloc_offset(table[i]) < reloc_offset(reloc))) + reloc =3D table[i]; + } + return reloc; +} + /* * First pass: Mark the head of each jump table so that in the next pass, * we know when a given jump table ends and the next one starts. */ static int mark_add_func_jump_tables(struct objtool_file *file, - struct symbol *func) + struct symbol *func, + struct reloc **table, unsigned int size) { - struct instruction *insn, *last =3D NULL, *insn_t1 =3D NULL, *insn_t2; + struct instruction *insn, *last =3D NULL; struct reloc *reloc; int ret =3D 0; =20 @@ -2132,23 +2148,11 @@ static int mark_add_func_jump_tables(struct objtool= _file *file, else continue; =20 - if (!insn_t1) { - insn_t1 =3D insn; - continue; - } - - insn_t2 =3D insn; - - ret =3D add_jump_table(file, insn_t1, insn_jump_table(insn_t2)); + ret =3D add_jump_table(file, insn, find_next_table(insn, table, size)); if (ret) return ret; - - insn_t1 =3D insn_t2; } =20 - if (insn_t1) - ret =3D add_jump_table(file, insn_t1, NULL); - return ret; } =20 @@ -2161,15 +2165,41 @@ static int add_jump_table_alts(struct objtool_file = *file) { struct symbol *func; int ret; + struct instruction *insn; + unsigned int size =3D 0, i =3D 0; + struct reloc **table =3D NULL; =20 if (!file->rodata) return 0; =20 + for_each_insn(file, insn) { + struct instruction *dest_insn; + struct reloc *reloc; + + func =3D insn_func(insn) ? insn_func(insn)->pfunc : NULL; + reloc =3D arch_find_switch_table(file, insn, NULL); + /* + * Each table entry has a rela associated with it. The rela + * should reference text in the same function as the original + * instruction. + */ + if (!reloc) + continue; + dest_insn =3D find_insn(file, reloc->sym->sec, reloc_addend(reloc)); + if (!dest_insn || !insn_func(dest_insn) || insn_func(dest_insn)->pfunc != =3D func) + continue; + if (i =3D=3D size) { + size +=3D 1024; + table =3D realloc(table, size * sizeof(*table)); + } + table[i++] =3D reloc; + } + for_each_sym(file, func) { if (func->type !=3D STT_FUNC) continue; =20 - ret =3D mark_add_func_jump_tables(file, func); + ret =3D mark_add_func_jump_tables(file, func, table, i); if (ret) return ret; } --=20 2.41.0