From nobody Tue Feb 10 04:02:28 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 277C3EB64D7 for ; Tue, 20 Jun 2023 07:47:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231163AbjFTHrg (ORCPT ); Tue, 20 Jun 2023 03:47:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229849AbjFTHr1 (ORCPT ); Tue, 20 Jun 2023 03:47:27 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 173E31A5; Tue, 20 Jun 2023 00:47:22 -0700 (PDT) Received: from loongson.cn (unknown [113.200.148.30]) by gateway (Coremail) with SMTP id _____8Cxd+mJWZFkohoHAA--.12661S3; Tue, 20 Jun 2023 15:47:21 +0800 (CST) Received: from bogon.localdomain (unknown [113.200.148.30]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxK8pvWZFkJGYhAA--.19652S5; Tue, 20 Jun 2023 15:47:19 +0800 (CST) From: Youling Tang To: Josh Poimboeuf , Peter Zijlstra , Huacai Chen , madvenka@linux.microsoft.com Cc: chenzhongjin@huawei.com, WANG Xuerui , Xi Ruoyao , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, tangyouling00@gmail.com, youling.tang@outlook.com Subject: [RFC PATCH v1 03/23] objtool: Move decode_instructions() to a separate file Date: Tue, 20 Jun 2023 15:46:29 +0800 Message-Id: <1687247209-31676-4-git-send-email-tangyouling@loongson.cn> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1687247209-31676-1-git-send-email-tangyouling@loongson.cn> References: <1687247209-31676-1-git-send-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I7IYx2IY67AKxVW5JVW7JwCI42IY6xIIjxv20xvEc7CjxVAFwI0_Gr0_Cr1lIxAIcVCF04 k26cxKx2IYs7xG6r1j6r1xMIIF0xvEx4A2jsIE14v26r4j6F4UMIIF0xvEx4A2jsIEc7Cj xVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07j6rWOUUUUU= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: "Madhavan T. Venkataraman" check.c implements static stack validation. But decode_instructions() which resides in it can be shared with other types of validation. E.g., dynamic FP validation. Move the function to its own file - decode.c. Signed-off-by: Madhavan T. Venkataraman --- tools/objtool/Build | 1 + tools/objtool/check.c | 127 +----------------------- tools/objtool/decode.c | 136 ++++++++++++++++++++++++++ tools/objtool/include/objtool/check.h | 2 + tools/objtool/include/objtool/insn.h | 2 + 5 files changed, 142 insertions(+), 126 deletions(-) create mode 100644 tools/objtool/decode.c diff --git a/tools/objtool/Build b/tools/objtool/Build index c04e36267379..64ccae49cd5f 100644 --- a/tools/objtool/Build +++ b/tools/objtool/Build @@ -7,6 +7,7 @@ objtool-y +=3D special.o objtool-y +=3D builtin-check.o objtool-y +=3D cfi.o objtool-y +=3D insn.o +objtool-y +=3D decode.o objtool-y +=3D elf.o objtool-y +=3D objtool.o =20 diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 619f7467e39c..26ed9b0b8f49 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -167,134 +167,9 @@ static bool dead_end_function(struct objtool_file *fi= le, struct symbol *func) return __dead_end_function(file, func, 0); } =20 -static unsigned long nr_insns; +unsigned long nr_insns; static unsigned long nr_insns_visited; =20 -/* - * Call the arch-specific instruction decoder for all the instructions and= add - * them to the global instruction list. - */ -static int decode_instructions(struct objtool_file *file) -{ - struct section *sec; - struct symbol *func; - unsigned long offset; - struct instruction *insn; - int ret; - - for_each_sec(file, sec) { - struct instruction *insns =3D NULL; - u8 prev_len =3D 0; - u8 idx =3D 0; - - if (!(sec->sh.sh_flags & SHF_EXECINSTR)) - continue; - - if (strcmp(sec->name, ".altinstr_replacement") && - strcmp(sec->name, ".altinstr_aux") && - strncmp(sec->name, ".discard.", 9)) - sec->text =3D true; - - if (!strcmp(sec->name, ".noinstr.text") || - !strcmp(sec->name, ".entry.text") || - !strcmp(sec->name, ".cpuidle.text") || - !strncmp(sec->name, ".text.__x86.", 12)) - sec->noinstr =3D true; - - /* - * .init.text code is ran before userspace and thus doesn't - * strictly need retpolines, except for modules which are - * loaded late, they very much do need retpoline in their - * .init.text - */ - if (!strcmp(sec->name, ".init.text") && !opts.module) - sec->init =3D true; - - for (offset =3D 0; offset < sec->sh.sh_size; offset +=3D insn->len) { - if (!insns || idx =3D=3D INSN_CHUNK_MAX) { - insns =3D calloc(sizeof(*insn), INSN_CHUNK_SIZE); - if (!insns) { - WARN("malloc failed"); - return -1; - } - idx =3D 0; - } else { - idx++; - } - insn =3D &insns[idx]; - insn->idx =3D idx; - - INIT_LIST_HEAD(&insn->call_node); - insn->sec =3D sec; - insn->offset =3D offset; - insn->prev_len =3D prev_len; - - ret =3D arch_decode_instruction(file, sec, offset, - sec->sh.sh_size - offset, - insn); - if (ret) - return ret; - - prev_len =3D insn->len; - - /* - * By default, "ud2" is a dead end unless otherwise - * annotated, because GCC 7 inserts it for certain - * divide-by-zero cases. - */ - if (insn->type =3D=3D INSN_BUG) - insn->dead_end =3D true; - - hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offse= t)); - nr_insns++; - } - -// printf("%s: last chunk used: %d\n", sec->name, (int)idx); - - list_for_each_entry(func, &sec->symbol_list, list) { - if (func->type !=3D STT_NOTYPE && func->type !=3D STT_FUNC) - continue; - - if (func->offset =3D=3D sec->sh.sh_size) { - /* Heuristic: likely an "end" symbol */ - if (func->type =3D=3D STT_NOTYPE) - continue; - WARN("%s(): STT_FUNC at end of section", - func->name); - return -1; - } - - if (func->return_thunk || func->alias !=3D func) - continue; - - if (!find_insn(file, sec, func->offset)) { - WARN("%s(): can't find starting instruction", - func->name); - return -1; - } - - sym_for_each_insn(file, func, insn) { - insn->sym =3D func; - if (func->type =3D=3D STT_FUNC && - insn->type =3D=3D INSN_ENDBR && - list_empty(&insn->call_node)) { - if (insn->offset =3D=3D func->offset) { - list_add_tail(&insn->call_node, &file->endbr_list); - file->nr_endbr++; - } else { - file->nr_endbr_int++; - } - } - } - } - } - - if (opts.stats) - printf("nr_insns: %lu\n", nr_insns); - - return 0; -} - /* * Read the pv_ops[] .data table to find the static initialized values. */ diff --git a/tools/objtool/decode.c b/tools/objtool/decode.c new file mode 100644 index 000000000000..f369412ffce1 --- /dev/null +++ b/tools/objtool/decode.c @@ -0,0 +1,136 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2015-2017 Josh Poimboeuf + */ +#include + +#include +#include +#include +#include + +/* + * Call the arch-specific instruction decoder for all the instructions and= add + * them to the global instruction list. + */ +int decode_instructions(struct objtool_file *file) +{ + struct section *sec; + struct symbol *func; + unsigned long offset; + struct instruction *insn; + int ret; + + for_each_sec(file, sec) { + struct instruction *insns =3D NULL; + u8 prev_len =3D 0; + u8 idx =3D 0; + + if (!(sec->sh.sh_flags & SHF_EXECINSTR)) + continue; + + if (strcmp(sec->name, ".altinstr_replacement") && + strcmp(sec->name, ".altinstr_aux") && + strncmp(sec->name, ".discard.", 9)) + sec->text =3D true; + + if (!strcmp(sec->name, ".noinstr.text") || + !strcmp(sec->name, ".entry.text") || + !strcmp(sec->name, ".cpuidle.text") || + !strncmp(sec->name, ".text.__x86.", 12)) + sec->noinstr =3D true; + + /* + * .init.text code is ran before userspace and thus doesn't + * strictly need retpolines, except for modules which are + * loaded late, they very much do need retpoline in their + * .init.text + */ + if (!strcmp(sec->name, ".init.text") && !opts.module) + sec->init =3D true; + + for (offset =3D 0; offset < sec->sh.sh_size; offset +=3D insn->len) { + if (!insns || idx =3D=3D INSN_CHUNK_MAX) { + insns =3D calloc(sizeof(*insn), INSN_CHUNK_SIZE); + if (!insns) { + WARN("malloc failed"); + return -1; + } + idx =3D 0; + } else { + idx++; + } + insn =3D &insns[idx]; + insn->idx =3D idx; + + INIT_LIST_HEAD(&insn->call_node); + insn->sec =3D sec; + insn->offset =3D offset; + insn->prev_len =3D prev_len; + + ret =3D arch_decode_instruction(file, sec, offset, + sec->sh.sh_size - offset, + insn); + if (ret) + return ret; + + prev_len =3D insn->len; + + /* + * By default, "ud2" is a dead end unless otherwise + * annotated, because GCC 7 inserts it for certain + * divide-by-zero cases. + */ + if (insn->type =3D=3D INSN_BUG) + insn->dead_end =3D true; + + hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offse= t)); + nr_insns++; + } + +// printf("%s: last chunk used: %d\n", sec->name, (int)idx); + + list_for_each_entry(func, &sec->symbol_list, list) { + if (func->type !=3D STT_NOTYPE && func->type !=3D STT_FUNC) + continue; + + if (func->offset =3D=3D sec->sh.sh_size) { + /* Heuristic: likely an "end" symbol */ + if (func->type =3D=3D STT_NOTYPE) + continue; + WARN("%s(): STT_FUNC at end of section", + func->name); + return -1; + } + + if (func->return_thunk || func->alias !=3D func) + continue; + + if (!find_insn(file, sec, func->offset)) { + WARN("%s(): can't find starting instruction", + func->name); + return -1; + } + + sym_for_each_insn(file, func, insn) { + insn->sym =3D func; + if (func->type =3D=3D STT_FUNC && + insn->type =3D=3D INSN_ENDBR && + list_empty(&insn->call_node)) { + if (insn->offset =3D=3D func->offset) { + list_add_tail(&insn->call_node, &file->endbr_list); + file->nr_endbr++; + } else { + file->nr_endbr_int++; + } + } + } + } + } + + if (opts.stats) + printf("nr_insns: %lu\n", nr_insns); + + return 0; +} + diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/= objtool/check.h index 450ebc092b1f..34898364bf03 100644 --- a/tools/objtool/include/objtool/check.h +++ b/tools/objtool/include/objtool/check.h @@ -31,4 +31,6 @@ struct alt_group { #define VISITED_BRANCH_MASK 0x03 #define VISITED_ENTRY 0x04 =20 +extern unsigned long nr_insns; + #endif /* _CHECK_H */ diff --git a/tools/objtool/include/objtool/insn.h b/tools/objtool/include/o= bjtool/insn.h index edd46b5ea1e4..92f8f1ff6c09 100644 --- a/tools/objtool/include/objtool/insn.h +++ b/tools/objtool/include/objtool/insn.h @@ -127,6 +127,8 @@ bool is_first_func_insn(struct objtool_file *file, struct instruction *insn, struct symbol *sym); =20 =20 +int decode_instructions(struct objtool_file *file); + #define for_each_insn(file, insn) \ for (struct section *__sec, *__fake =3D (struct section *)1; \ __fake; __fake =3D NULL) \ --=20 2.39.2