From patchwork Fri Mar 18 10:51:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sathvika Vasireddy X-Patchwork-Id: 1607290 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=OXQd9p66; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KKyJ53JGMz9s5V for ; Sat, 19 Mar 2022 08:46:13 +1100 (AEDT) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4KKyJ46fj3z3bVD for ; Sat, 19 Mar 2022 08:46:12 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=OXQd9p66; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linux.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=sv@linux.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=OXQd9p66; dkim-atps=neutral Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4KKgqQ6tlLz2ynp for ; Fri, 18 Mar 2022 21:53:54 +1100 (AEDT) Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 22I7vjir011346; Fri, 18 Mar 2022 10:53:32 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=1dxESL4wrWhiNiFzfmUFeh1MGvu9VOj/ATKHLuRWj7s=; b=OXQd9p66lTsez6l02+Ds20iaUihnIbI15MLXxprpeprpJrklKSanzryKs+R46Fn6qUij gGnL9AO8dIqa8RaElvnDmEappCeyPDrZn5NFOtDSpO+RerfagfXE0euRn/b5c8nBsu23 kuOlBDblfW5Iss2J7Z/fCjXIDj1DvQyMuQhNSLIsRf4D076bNjzb/ZX/0VKqzCTbxsuH KkSQo+iXtb9gNrAMAuCRt7IfCTHLhTM6hojjK+b0a+7SuT5LY4mK6kMnwHOxCceZDk3j UH27YR6rU4K/yYw6bTvbn8ObW1hD3rl3V2chH9MDJwERoAL7Q4M2jXqiyfU21yXYrQym 9A== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3ev2s51p5s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 18 Mar 2022 10:53:31 +0000 Received: from m0098399.ppops.net (m0098399.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 22IAqtK8017577; Fri, 18 Mar 2022 10:53:31 GMT Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0a-001b2d01.pphosted.com with ESMTP id 3ev2s51p5a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 18 Mar 2022 10:53:31 +0000 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 22IArRmZ026929; Fri, 18 Mar 2022 10:53:28 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma04ams.nl.ibm.com with ESMTP id 3erk595b2c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 18 Mar 2022 10:53:28 +0000 Received: from d06av24.portsmouth.uk.ibm.com (d06av24.portsmouth.uk.ibm.com [9.149.105.60]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 22IArQ8j19726780 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 18 Mar 2022 10:53:26 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6EC2C42047; Fri, 18 Mar 2022 10:53:26 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 7655E4203F; Fri, 18 Mar 2022 10:53:23 +0000 (GMT) Received: from li-c3569c4c-1ef8-11b2-a85c-ee139cda3133.ibm.com.com (unknown [9.43.86.72]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 18 Mar 2022 10:53:23 +0000 (GMT) From: Sathvika Vasireddy To: linuxppc-dev@lists.ozlabs.org Subject: [RFC PATCH 1/3] objtool: Move common code to utils.c Date: Fri, 18 Mar 2022 16:21:38 +0530 Message-Id: <20220318105140.43914-2-sv@linux.ibm.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220318105140.43914-1-sv@linux.ibm.com> References: <20220318105140.43914-1-sv@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: 28vsj1gaIiAWR3ol53GRSKjETyNUnhZe X-Proofpoint-GUID: 8aqvLo5e6oisQn81p_U3MNdYm4m88s_g X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.850,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-18_08,2022-03-15_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 spamscore=0 lowpriorityscore=0 bulkscore=0 mlxlogscore=999 suspectscore=0 malwarescore=0 clxscore=1015 mlxscore=0 adultscore=0 priorityscore=1501 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2203180058 X-Mailman-Approved-At: Sat, 19 Mar 2022 08:45:00 +1100 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peterz@infradead.org, linux-kernel@vger.kernel.org, rostedt@goodmis.org, aik@ozlabs.ru, sv@linux.ibm.com, jpoimboe@redhat.com, naveen.n.rao@linux.vnet.ibm.com Errors-To: linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" This patch moves common code to utils.c file. Code from this file will be reused across check.c and mcount.c (will be introduced in the coming patches). Also, ensure that this change works well with existing commands. Signed-off-by: Sathvika Vasireddy --- tools/objtool/Build | 1 + tools/objtool/check.c | 178 +----------------------- tools/objtool/include/objtool/check.h | 2 - tools/objtool/include/objtool/utils.h | 28 ++++ tools/objtool/orc_gen.c | 1 + tools/objtool/utils.c | 192 ++++++++++++++++++++++++++ 6 files changed, 223 insertions(+), 179 deletions(-) create mode 100644 tools/objtool/include/objtool/utils.h create mode 100644 tools/objtool/utils.c diff --git a/tools/objtool/Build b/tools/objtool/Build index b7222d5cc7bc..161fd451241a 100644 --- a/tools/objtool/Build +++ b/tools/objtool/Build @@ -12,6 +12,7 @@ objtool-y += builtin-check.o objtool-y += builtin-orc.o objtool-y += elf.o objtool-y += objtool.o +objtool-y += utils.o objtool-y += libstring.o objtool-y += libctype.o diff --git a/tools/objtool/check.c b/tools/objtool/check.c index 7c33ec67c4a9..161dd181d9d4 100644 --- a/tools/objtool/check.c +++ b/tools/objtool/check.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -33,19 +34,6 @@ static struct cfi_init_state initial_func_cfi; static struct cfi_state init_cfi; static struct cfi_state func_cfi; -struct instruction *find_insn(struct objtool_file *file, - struct section *sec, unsigned long offset) -{ - struct instruction *insn; - - hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { - if (insn->sec == sec && insn->offset == offset) - return insn; - } - - return NULL; -} - static struct instruction *next_insn_same_sec(struct objtool_file *file, struct instruction *insn) { @@ -342,86 +330,8 @@ static void *cfi_hash_alloc(unsigned long size) return cfi_hash; } -static unsigned long nr_insns; static unsigned long nr_insns_visited; -/* - * 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) { - - 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 = true; - - if (!strcmp(sec->name, ".noinstr.text") || - !strcmp(sec->name, ".entry.text")) - sec->noinstr = true; - - for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) { - insn = malloc(sizeof(*insn)); - if (!insn) { - WARN("malloc failed"); - return -1; - } - memset(insn, 0, sizeof(*insn)); - INIT_LIST_HEAD(&insn->alts); - INIT_LIST_HEAD(&insn->stack_ops); - - insn->sec = sec; - insn->offset = offset; - - ret = arch_decode_instruction(file, sec, offset, - sec->sh.sh_size - offset, - &insn->len, &insn->type, - &insn->immediate, - &insn->stack_ops); - if (ret) - goto err; - - hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); - list_add_tail(&insn->list, &file->insn_list); - nr_insns++; - } - - list_for_each_entry(func, &sec->symbol_list, list) { - if (func->type != STT_FUNC || func->alias != 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->func = func; - } - } - - if (stats) - printf("nr_insns: %lu\n", nr_insns); - - return 0; - -err: - free(insn); - return ret; -} - /* * Read the pv_ops[] .data table to find the static initialized values. */ @@ -731,49 +641,6 @@ static int create_retpoline_sites_sections(struct objtool_file *file) return 0; } -static int create_mcount_loc_sections(struct objtool_file *file) -{ - struct section *sec; - unsigned long *loc; - struct instruction *insn; - int idx; - - sec = find_section_by_name(file->elf, "__mcount_loc"); - if (sec) { - INIT_LIST_HEAD(&file->mcount_loc_list); - WARN("file already has __mcount_loc section, skipping"); - return 0; - } - - if (list_empty(&file->mcount_loc_list)) - return 0; - - idx = 0; - list_for_each_entry(insn, &file->mcount_loc_list, call_node) - idx++; - - sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx); - if (!sec) - return -1; - - idx = 0; - list_for_each_entry(insn, &file->mcount_loc_list, call_node) { - - loc = (unsigned long *)sec->data->d_buf + idx; - memset(loc, 0, sizeof(unsigned long)); - - if (elf_add_reloc_to_insn(file->elf, sec, - idx * sizeof(unsigned long), - R_X86_64_64, - insn->sec, insn->offset)) - return -1; - - idx++; - } - - return 0; -} - /* * Warnings shouldn't be reported for ignored functions. */ @@ -1013,38 +880,6 @@ __weak bool arch_is_retpoline(struct symbol *sym) return false; } -#define NEGATIVE_RELOC ((void *)-1L) - -static struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn) -{ - if (insn->reloc == NEGATIVE_RELOC) - return NULL; - - if (!insn->reloc) { - if (!file) - return NULL; - - insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec, - insn->offset, insn->len); - if (!insn->reloc) { - insn->reloc = NEGATIVE_RELOC; - return NULL; - } - } - - return insn->reloc; -} - -static void remove_insn_ops(struct instruction *insn) -{ - struct stack_op *op, *tmp; - - list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) { - list_del(&op->list); - free(op); - } -} - static void annotate_call_site(struct objtool_file *file, struct instruction *insn, bool sibling) { @@ -1256,17 +1091,6 @@ static int add_jump_destinations(struct objtool_file *file) return 0; } -static struct symbol *find_call_destination(struct section *sec, unsigned long offset) -{ - struct symbol *call_dest; - - call_dest = find_func_by_offset(sec, offset); - if (!call_dest) - call_dest = find_symbol_by_offset(sec, offset); - - return call_dest; -} - /* * Find the destination instructions for all calls. */ diff --git a/tools/objtool/include/objtool/check.h b/tools/objtool/include/objtool/check.h index 6cfff078897f..d4e378c7aa30 100644 --- a/tools/objtool/include/objtool/check.h +++ b/tools/objtool/include/objtool/check.h @@ -79,8 +79,6 @@ static inline bool is_jump(struct instruction *insn) return is_static_jump(insn) || is_dynamic_jump(insn); } -struct instruction *find_insn(struct objtool_file *file, - struct section *sec, unsigned long offset); #define for_each_insn(file, insn) \ list_for_each_entry(insn, &file->insn_list, list) diff --git a/tools/objtool/include/objtool/utils.h b/tools/objtool/include/objtool/utils.h new file mode 100644 index 000000000000..f808a66dd0a8 --- /dev/null +++ b/tools/objtool/include/objtool/utils.h @@ -0,0 +1,28 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright (C) 2017 Josh Poimboeuf + */ + +#ifndef UTILS_H +#define UTILS_H + +#include +#include +#include + +int decode_instructions(struct objtool_file *file); +struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn); +void remove_insn_ops(struct instruction *insn); +struct symbol *find_call_destination(struct section *sec, unsigned long offset); +int create_mcount_loc_sections(struct objtool_file *file); +struct instruction *find_insn(struct objtool_file *file, + struct section *sec, unsigned long offset); + +#define sym_for_each_insn(file, sym, insn) \ + for (insn = find_insn(file, sym->sec, sym->offset); \ + insn && &insn->list != &file->insn_list && \ + insn->sec == sym->sec && \ + insn->offset < sym->offset + sym->len; \ + insn = list_next_entry(insn, list)) + +#endif /* UTILS_H */ diff --git a/tools/objtool/orc_gen.c b/tools/objtool/orc_gen.c index dd3c64af9db2..383c5f2f0658 100644 --- a/tools/objtool/orc_gen.c +++ b/tools/objtool/orc_gen.c @@ -10,6 +10,7 @@ #include #include +#include #include #include diff --git a/tools/objtool/utils.c b/tools/objtool/utils.c new file mode 100644 index 000000000000..d1fc6a123a6e --- /dev/null +++ b/tools/objtool/utils.c @@ -0,0 +1,192 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2017 Josh Poimboeuf + */ + +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include + +struct instruction *find_insn(struct objtool_file *file, + struct section *sec, unsigned long offset) +{ + struct instruction *insn; + + hash_for_each_possible(file->insn_hash, insn, hash, sec_offset_hash(sec, offset)) { + if (insn->sec == sec && insn->offset == offset) + return insn; + } + + return NULL; +} + +#define NEGATIVE_RELOC ((void *)-1L) + +struct reloc *insn_reloc(struct objtool_file *file, struct instruction *insn) +{ + if (insn->reloc == NEGATIVE_RELOC) + return NULL; + + if (!insn->reloc) { + if (!file) + return NULL; + + insn->reloc = find_reloc_by_dest_range(file->elf, insn->sec, + insn->offset, insn->len); + if (!insn->reloc) { + insn->reloc = NEGATIVE_RELOC; + return NULL; + } + } + + return insn->reloc; +} +void remove_insn_ops(struct instruction *insn) +{ + struct stack_op *op, *tmp; + + list_for_each_entry_safe(op, tmp, &insn->stack_ops, list) { + list_del(&op->list); + free(op); + } +} + + +struct symbol *find_call_destination(struct section *sec, unsigned long offset) +{ + struct symbol *call_dest; + + call_dest = find_func_by_offset(sec, offset); + if (!call_dest) + call_dest = find_symbol_by_offset(sec, offset); + + return call_dest; +} + +static unsigned long nr_insns; + +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) { + + 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 = true; + + if (!strcmp(sec->name, ".noinstr.text") || + !strcmp(sec->name, ".entry.text")) + sec->noinstr = true; + + for (offset = 0; offset < sec->sh.sh_size; offset += insn->len) { + insn = malloc(sizeof(*insn)); + if (!insn) { + WARN("malloc failed"); + return -1; + } + memset(insn, 0, sizeof(*insn)); + INIT_LIST_HEAD(&insn->alts); + INIT_LIST_HEAD(&insn->stack_ops); + + insn->sec = sec; + insn->offset = offset; + + ret = arch_decode_instruction(file, sec, offset, + sec->sh.sh_size - offset, + &insn->len, &insn->type, + &insn->immediate, + &insn->stack_ops); + if (ret) + goto err; + + hash_add(file->insn_hash, &insn->hash, sec_offset_hash(sec, insn->offset)); + list_add_tail(&insn->list, &file->insn_list); + nr_insns++; + } + + list_for_each_entry(func, &sec->symbol_list, list) { + if (func->type != STT_FUNC || func->alias != 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->func = func; + } + } + + if (stats) + printf("nr_insns: %lu\n", nr_insns); + + return 0; + +err: + free(insn); + return ret; +} + +int create_mcount_loc_sections(struct objtool_file *file) +{ + struct section *sec; + unsigned long *loc; + struct instruction *insn; + int idx; + + sec = find_section_by_name(file->elf, "__mcount_loc"); + if (sec) { + INIT_LIST_HEAD(&file->mcount_loc_list); + WARN("file already has __mcount_loc section, skipping"); + return 0; + } + + if (list_empty(&file->mcount_loc_list)) + return 0; + + idx = 0; + list_for_each_entry(insn, &file->mcount_loc_list, call_node) + idx++; + + sec = elf_create_section(file->elf, "__mcount_loc", 0, sizeof(unsigned long), idx); + if (!sec) + return -1; + + idx = 0; + list_for_each_entry(insn, &file->mcount_loc_list, call_node) { + + loc = (unsigned long *)sec->data->d_buf + idx; + memset(loc, 0, sizeof(unsigned long)); + + if (elf_add_reloc_to_insn(file->elf, sec, + idx * sizeof(unsigned long), + R_X86_64_64, + insn->sec, insn->offset)) + return -1; + + idx++; + } + + return 0; +} From patchwork Fri Mar 18 10:51:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sathvika Vasireddy X-Patchwork-Id: 1607291 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=n2OL4IfY; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=2404:9400:2:0:216:3eff:fee1:b9f1; helo=lists.ozlabs.org; envelope-from=linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2404:9400:2:0:216:3eff:fee1:b9f1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KKyJt2r1mz9s5V for ; Sat, 19 Mar 2022 08:46:54 +1100 (AEDT) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4KKyJt1L26z3bhJ for ; Sat, 19 Mar 2022 08:46:54 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=n2OL4IfY; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linux.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=sv@linux.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=n2OL4IfY; dkim-atps=neutral Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4KKgqR68rrz2ynp for ; Fri, 18 Mar 2022 21:53:55 +1100 (AEDT) Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 22IA9BNS026607; Fri, 18 Mar 2022 10:53:36 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=2ew6wRg1wjtuO6uiQny5V0EzHjASVsiq/XY8gPk0vWA=; b=n2OL4IfY+xFDvIgvDLtBFipG45eV7jhvV8zaeHh90HkA0WdABw94GUjip+sfBXAX7sFC iB1SF1ibWob4Ox+IVHOygdP6TQNiW1iOFfIi4eaTHU2q/cidnLNd3EZOv3ljjTPLbvDC OPwcxE1/NmJoP31aT3mF52PN285scYjxRosCmpDmQREjXj/MKypnbv1gkhGi2lcvhItu AwG223HJk1TaJCTmcWoU5OIJo3LJUp83fJFzivDzUjPxrJfOlj4I4tZz1ymN8M23EqRI 0aGWSEh5JRKf3QMG0bi51pifIj4Vd0kIWNH+slJB8HnqyExdGSMnCupI/slfn8vXJ/xq NA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3ev04qmu1x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 18 Mar 2022 10:53:35 +0000 Received: from m0098409.ppops.net (m0098409.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 22IAi0Ib019720; Fri, 18 Mar 2022 10:53:35 GMT Received: from ppma03fra.de.ibm.com (6b.4a.5195.ip4.static.sl-reverse.com [149.81.74.107]) by mx0a-001b2d01.pphosted.com with ESMTP id 3ev04qmu1e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 18 Mar 2022 10:53:35 +0000 Received: from pps.filterd (ppma03fra.de.ibm.com [127.0.0.1]) by ppma03fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 22IArR8D024792; Fri, 18 Mar 2022 10:53:32 GMT Received: from b06cxnps4076.portsmouth.uk.ibm.com (d06relay13.portsmouth.uk.ibm.com [9.149.109.198]) by ppma03fra.de.ibm.com with ESMTP id 3erk58ubwq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 18 Mar 2022 10:53:32 +0000 Received: from d06av24.portsmouth.uk.ibm.com (d06av24.portsmouth.uk.ibm.com [9.149.105.60]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 22IArUZc14156074 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 18 Mar 2022 10:53:30 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5590442047; Fri, 18 Mar 2022 10:53:30 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 891DA42042; Fri, 18 Mar 2022 10:53:27 +0000 (GMT) Received: from li-c3569c4c-1ef8-11b2-a85c-ee139cda3133.ibm.com.com (unknown [9.43.86.72]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 18 Mar 2022 10:53:27 +0000 (GMT) From: Sathvika Vasireddy To: linuxppc-dev@lists.ozlabs.org Subject: [RFC PATCH 2/3] objtool: Enable and implement 'mcount' subcommand Date: Fri, 18 Mar 2022 16:21:39 +0530 Message-Id: <20220318105140.43914-3-sv@linux.ibm.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220318105140.43914-1-sv@linux.ibm.com> References: <20220318105140.43914-1-sv@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: 3RGrmuy0K9pOsAbWcN8jqOUXdgbA1cwi X-Proofpoint-ORIG-GUID: tEldzbnDPlSEygwEjURN2gENrvWTOj7u X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.850,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-18_08,2022-03-15_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 impostorscore=0 phishscore=0 bulkscore=0 mlxscore=0 malwarescore=0 suspectscore=0 priorityscore=1501 lowpriorityscore=0 clxscore=1015 mlxlogscore=999 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2203180055 X-Mailman-Approved-At: Sat, 19 Mar 2022 08:45:00 +1100 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peterz@infradead.org, linux-kernel@vger.kernel.org, rostedt@goodmis.org, aik@ozlabs.ru, sv@linux.ibm.com, jpoimboe@redhat.com, naveen.n.rao@linux.vnet.ibm.com Errors-To: linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" This patch adds 'mcount' as a subcommand to objtool, and enables the same for x86. objtool is built if CONFIG_FTRACE_MCOUNT_USE_OBJTOOL is selected. Additionally, architectures can select HAVE_NOP_MCOUNT if they choose to nop out mcount call sites. If that config option is selected, then --mnop is passed as an option to 'objtool mcount' Signed-off-by: Sathvika Vasireddy --- Makefile | 6 ++ arch/x86/Kconfig | 3 +- scripts/Makefile.build | 12 +++ tools/objtool/Build | 2 + tools/objtool/Makefile | 4 +- tools/objtool/builtin-mcount.c | 74 +++++++++++++ tools/objtool/include/objtool/builtin.h | 4 +- tools/objtool/include/objtool/objtool.h | 1 + tools/objtool/mcount.c | 138 ++++++++++++++++++++++++ tools/objtool/objtool.c | 1 + tools/objtool/weak.c | 5 + 11 files changed, 247 insertions(+), 3 deletions(-) create mode 100644 tools/objtool/builtin-mcount.c create mode 100644 tools/objtool/mcount.c diff --git a/Makefile b/Makefile index 55a30ca69350..316f7d08b30a 100644 --- a/Makefile +++ b/Makefile @@ -846,7 +846,9 @@ ifdef CONFIG_FTRACE_MCOUNT_USE_CC endif endif ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL + ifdef CONFIG_HAVE_NOP_MCOUNT CC_FLAGS_USING += -DCC_USING_NOP_MCOUNT + endif endif ifdef CONFIG_FTRACE_MCOUNT_USE_RECORDMCOUNT ifdef CONFIG_HAVE_C_RECORDMCOUNT @@ -1303,6 +1305,10 @@ ifdef CONFIG_STACK_VALIDATION prepare: tools/objtool endif +ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL +prepare: tools/objtool +endif + ifdef CONFIG_BPF ifdef CONFIG_DEBUG_INFO_BTF prepare: tools/bpf/resolve_btfids diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 9f5bd41bf660..eecfe588cd9f 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -185,7 +185,8 @@ config X86 select HAVE_CONTEXT_TRACKING if X86_64 select HAVE_CONTEXT_TRACKING_OFFSTACK if HAVE_CONTEXT_TRACKING select HAVE_C_RECORDMCOUNT - select HAVE_OBJTOOL_MCOUNT if STACK_VALIDATION + select HAVE_OBJTOOL_MCOUNT + select HAVE_NOP_MCOUNT if HAVE_OBJTOOL_MCOUNT select HAVE_BUILDTIME_MCOUNT_SORT select HAVE_DEBUG_KMEMLEAK select HAVE_DMA_CONTIGUOUS diff --git a/scripts/Makefile.build b/scripts/Makefile.build index a4b89b757287..6a09b4ba14a1 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -242,6 +242,18 @@ cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(o endif # CONFIG_STACK_VALIDATION +ifdef CONFIG_FTRACE_MCOUNT_USE_OBJTOOL + +objtool := $(objtree)/tools/objtool/objtool + +objtool_args := mcount +objtool_args += $(if $(CONFIG_HAVE_NOP_MCOUNT), --mnop) + +cmd_objtool = $(if $(objtool-enabled), ; $(objtool) $(objtool_args) $@) +cmd_gen_objtooldep = $(if $(objtool-enabled), { echo ; echo '$@: $$(wildcard $(objtool))' ; } >> $(dot-target).cmd) + +endif # CONFIG_FTRACE_MCOUNT_USE_OBJTOOL + ifdef CONFIG_LTO_CLANG # Skip objtool for LLVM bitcode diff --git a/tools/objtool/Build b/tools/objtool/Build index 161fd451241a..0b9b8bd0ee92 100644 --- a/tools/objtool/Build +++ b/tools/objtool/Build @@ -2,12 +2,14 @@ objtool-y += arch/$(SRCARCH)/ objtool-y += weak.o +objtool-$(SUBCMD_MCOUNT) += mcount.o objtool-$(SUBCMD_CHECK) += check.o objtool-$(SUBCMD_CHECK) += special.o objtool-$(SUBCMD_ORC) += check.o objtool-$(SUBCMD_ORC) += orc_gen.o objtool-$(SUBCMD_ORC) += orc_dump.o +objtool-y += builtin-mcount.o objtool-y += builtin-check.o objtool-y += builtin-orc.o objtool-y += elf.o diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile index 92ce4fce7bc7..8404cf696954 100644 --- a/tools/objtool/Makefile +++ b/tools/objtool/Makefile @@ -41,13 +41,15 @@ AWK = awk SUBCMD_CHECK := n SUBCMD_ORC := n +SUBCMD_MCOUNT := n ifeq ($(SRCARCH),x86) SUBCMD_CHECK := y SUBCMD_ORC := y + SUBCMD_MCOUNT := y endif -export SUBCMD_CHECK SUBCMD_ORC +export SUBCMD_CHECK SUBCMD_ORC SUBCMD_MCOUNT export srctree OUTPUT CFLAGS SRCARCH AWK include $(srctree)/tools/build/Makefile.include diff --git a/tools/objtool/builtin-mcount.c b/tools/objtool/builtin-mcount.c new file mode 100644 index 000000000000..f203d118cffa --- /dev/null +++ b/tools/objtool/builtin-mcount.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include +#include + +bool mnop; + +static const char * const mcount_usage[] = { + "objtool mcount [] file.o", + NULL, +}; + +static const char * const env_usage[] = { + "OBJTOOL_ARGS=\"\"", + NULL, +}; + +const struct option mcount_options[] = { + OPT_BOOLEAN('N', "mnop", &mnop, "nop mcount call sites"), + OPT_END(), +}; + +int cmd_parse_options_mcount(int argc, const char **argv, const char * const usage[]) +{ + const char *envv[16] = { }; + char *env; + int envc; + + env = getenv("OBJTOOL_ARGS"); + if (env) { + envv[0] = "OBJTOOL_ARGS"; + for (envc = 1; envc < ARRAY_SIZE(envv); ) { + envv[envc++] = env; + env = strchr(env, ' '); + if (!env) + break; + *env = '\0'; + env++; + } + + parse_options(envc, envv, mcount_options, env_usage, 0); + } + + argc = parse_options(argc, argv, mcount_options, usage, 0); + if (argc != 1) + usage_with_options(usage, mcount_options); + return argc; +} + +int cmd_mcount(int argc, const char **argv) +{ + const char *objname; + struct objtool_file *file; + int ret; + + argc = cmd_parse_options_mcount(argc, argv, mcount_usage); + objname = argv[0]; + + file = objtool_open_read(objname); + if (!file) + return 1; + + ret = objtool_mcount(file); + if (ret) + return ret; + + if (file->elf->changed) + return elf_write(file->elf); + + return 0; +} diff --git a/tools/objtool/include/objtool/builtin.h b/tools/objtool/include/objtool/builtin.h index 89ba869ed08f..d7ed6ff65729 100644 --- a/tools/objtool/include/objtool/builtin.h +++ b/tools/objtool/include/objtool/builtin.h @@ -9,11 +9,13 @@ extern const struct option check_options[]; extern bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, - validate_dup, vmlinux, mcount, noinstr, backup, sls; + validate_dup, vmlinux, mcount, noinstr, backup, sls, mnop; extern int cmd_parse_options(int argc, const char **argv, const char * const usage[]); +extern int cmd_parse_options_mcount(int argc, const char **argv, const char * const usage[]); extern int cmd_check(int argc, const char **argv); extern int cmd_orc(int argc, const char **argv); +extern int cmd_mcount(int argc, const char **argv); #endif /* _BUILTIN_H */ diff --git a/tools/objtool/include/objtool/objtool.h b/tools/objtool/include/objtool/objtool.h index f99fbc6078d5..edcf28d4407c 100644 --- a/tools/objtool/include/objtool/objtool.h +++ b/tools/objtool/include/objtool/objtool.h @@ -41,5 +41,6 @@ void objtool_pv_add(struct objtool_file *file, int idx, struct symbol *func); int check(struct objtool_file *file); int orc_dump(const char *objname); int orc_create(struct objtool_file *file); +int objtool_mcount(struct objtool_file *file); #endif /* _OBJTOOL_H */ diff --git a/tools/objtool/mcount.c b/tools/objtool/mcount.c new file mode 100644 index 000000000000..fc3d215671bf --- /dev/null +++ b/tools/objtool/mcount.c @@ -0,0 +1,138 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +static int classify_symbols(struct objtool_file *file) +{ + struct section *sec; + struct symbol *func; + + for_each_sec(file, sec) { + list_for_each_entry(func, &sec->symbol_list, list) { + if (func->bind != STB_GLOBAL) + continue; + if ((!strcmp(func->name, "__fentry__")) || (!strcmp(func->name, "_mcount"))) + func->fentry = true; + } + } + + return 0; +} + +static void annotate_call_site(struct objtool_file *file, + struct instruction *insn, bool sibling) +{ + struct reloc *reloc = insn_reloc(file, insn); + struct symbol *sym = insn->call_dest; + + if (!sym) + sym = reloc->sym; + + if (sym->fentry) { + if (sibling) + WARN_FUNC("Tail call to _mcount !?!?", insn->sec, insn->offset); + if (mnop) { + if (reloc) { + reloc->type = R_NONE; + elf_write_reloc(file->elf, reloc); + } + elf_write_insn(file->elf, insn->sec, + insn->offset, insn->len, + arch_nop_insn(insn->len)); + + insn->type = INSN_NOP; + } + + list_add_tail(&insn->call_node, &file->mcount_loc_list); + return; + } +} + +static void add_call_dest(struct objtool_file *file, struct instruction *insn, + struct symbol *dest, bool sibling) +{ + insn->call_dest = dest; + if (!dest) + return; + + remove_insn_ops(insn); + + annotate_call_site(file, insn, sibling); +} +static int add_call_destinations(struct objtool_file *file) +{ + struct instruction *insn; + unsigned long dest_off; + struct symbol *dest; + struct reloc *reloc; + + for_each_insn(file, insn) { + if (insn->type != INSN_CALL) + continue; + + reloc = insn_reloc(file, insn); + if (!reloc) { + dest_off = arch_jump_destination(insn); + dest = find_call_destination(insn->sec, dest_off); + + add_call_dest(file, insn, dest, false); + + + } else { + add_call_dest(file, insn, reloc->sym, false); + } + } + + return 0; +} + +static int decode_sections(struct objtool_file *file) +{ + int ret; + + ret = decode_instructions(file); + if (ret) + return ret; + + ret = classify_symbols(file); + if (ret) + return ret; + + ret = add_call_destinations(file); + if (ret) + return ret; + + return 0; +} + + +int objtool_mcount(struct objtool_file *file) +{ + int ret, warnings = 0; + + ret = decode_sections(file); + if (ret < 0) + return 0; + + ret = create_mcount_loc_sections(file); + if (ret < 0) + return 0; + warnings += ret; + return 0; +} diff --git a/tools/objtool/objtool.c b/tools/objtool/objtool.c index bdf699f6552b..e19851813d5b 100644 --- a/tools/objtool/objtool.c +++ b/tools/objtool/objtool.c @@ -37,6 +37,7 @@ static const char objtool_usage_string[] = static struct cmd_struct objtool_cmds[] = { {"check", cmd_check, "Perform stack metadata validation on an object file" }, + {"mcount", cmd_mcount, "Generate __mcount_loc section" }, {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" }, }; diff --git a/tools/objtool/weak.c b/tools/objtool/weak.c index 8314e824db4a..19b2dfcacf2e 100644 --- a/tools/objtool/weak.c +++ b/tools/objtool/weak.c @@ -29,3 +29,8 @@ int __weak orc_create(struct objtool_file *file) { UNSUPPORTED("orc"); } + +int __weak objtool_mcount(struct objtool_file *file) +{ + UNSUPPORTED("mcount subcommand"); +} From patchwork Fri Mar 18 10:51:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sathvika Vasireddy X-Patchwork-Id: 1607292 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=p8P6k4iR; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.ozlabs.org (client-ip=112.213.38.117; helo=lists.ozlabs.org; envelope-from=linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org; receiver=) Received: from lists.ozlabs.org (lists.ozlabs.org [112.213.38.117]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4KKyKg5WXQz9s5V for ; Sat, 19 Mar 2022 08:47:35 +1100 (AEDT) Received: from boromir.ozlabs.org (localhost [IPv6:::1]) by lists.ozlabs.org (Postfix) with ESMTP id 4KKyKf6ldFz3bwT for ; Sat, 19 Mar 2022 08:47:34 +1100 (AEDT) Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=p8P6k4iR; dkim-atps=neutral X-Original-To: linuxppc-dev@lists.ozlabs.org Delivered-To: linuxppc-dev@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=linux.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=sv@linux.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=p8P6k4iR; dkim-atps=neutral Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 4KKgqZ0Sn6z30CT for ; Fri, 18 Mar 2022 21:54:01 +1100 (AEDT) Received: from pps.filterd (m0098393.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 22I8Uh33023446; Fri, 18 Mar 2022 10:53:40 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=t3uvsDIdKhsiwvX0gqMiuOC7GjUgy6HC7f0j4xg3EM8=; b=p8P6k4iRoTrNUq0XjloQLFbyjXanutjpbcdsvoq2xXQTHFT6S82/x0nxAOZ+taaSA9M1 tC7Zn9H8qq4ZFxVePoUog6MSX43tIsBYsLaeCOC8zAxWQqGm3Eotrmr7rCHz+49ZoRnW FjYGPYOj8PRMn+6gKRotVjBeSLr8qXbvMGUWKlJVLby+jzXyTigi/r/aY8xcZrN2BuCU ORbLlz4xE/4TfVD7IKAXrogTm6mlQrgVQlkihyZaeJLn8VYWMcnnFKHrOrIAEJFJb+Im LLGz6kRmCf7rjORglLsLdosC1CAh5Mjw9vSI3lg/h9XIzZue3bQUXQ4TDPYrzfoT3kSj hw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3ev2b3a73d-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 18 Mar 2022 10:53:40 +0000 Received: from m0098393.ppops.net (m0098393.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 22IArP9n010239; Fri, 18 Mar 2022 10:53:39 GMT Received: from ppma06ams.nl.ibm.com (66.31.33a9.ip4.static.sl-reverse.com [169.51.49.102]) by mx0a-001b2d01.pphosted.com with ESMTP id 3ev2b3a72s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 18 Mar 2022 10:53:39 +0000 Received: from pps.filterd (ppma06ams.nl.ibm.com [127.0.0.1]) by ppma06ams.nl.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 22IAqqpc026968; Fri, 18 Mar 2022 10:53:37 GMT Received: from b06cxnps4075.portsmouth.uk.ibm.com (d06relay12.portsmouth.uk.ibm.com [9.149.109.197]) by ppma06ams.nl.ibm.com with ESMTP id 3euc6r4uc5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 18 Mar 2022 10:53:37 +0000 Received: from d06av24.portsmouth.uk.ibm.com (d06av24.portsmouth.uk.ibm.com [9.149.105.60]) by b06cxnps4075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 22IArYOu20447502 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 18 Mar 2022 10:53:34 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6D1DB42042; Fri, 18 Mar 2022 10:53:34 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A55484203F; Fri, 18 Mar 2022 10:53:31 +0000 (GMT) Received: from li-c3569c4c-1ef8-11b2-a85c-ee139cda3133.ibm.com.com (unknown [9.43.86.72]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTP; Fri, 18 Mar 2022 10:53:31 +0000 (GMT) From: Sathvika Vasireddy To: linuxppc-dev@lists.ozlabs.org Subject: [RFC PATCH 3/3] objtool/mcount: Add powerpc specific functions Date: Fri, 18 Mar 2022 16:21:40 +0530 Message-Id: <20220318105140.43914-4-sv@linux.ibm.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220318105140.43914-1-sv@linux.ibm.com> References: <20220318105140.43914-1-sv@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: Fx24Gj7hsZC2kGiRj7WKjt8Dh0XH53RG X-Proofpoint-GUID: SEaYOOXrbhIX6xp6WVgslE39kc6lFypF X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.850,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-03-18_07,2022-03-15_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 adultscore=0 phishscore=0 spamscore=0 mlxlogscore=999 clxscore=1015 suspectscore=0 malwarescore=0 mlxscore=0 impostorscore=0 bulkscore=0 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2202240000 definitions=main-2203180055 X-Mailman-Approved-At: Sat, 19 Mar 2022 08:45:00 +1100 X-BeenThere: linuxppc-dev@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux on PowerPC Developers Mail List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: peterz@infradead.org, linux-kernel@vger.kernel.org, rostedt@goodmis.org, aik@ozlabs.ru, sv@linux.ibm.com, jpoimboe@redhat.com, naveen.n.rao@linux.vnet.ibm.com Errors-To: linuxppc-dev-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Linuxppc-dev" This patch adds powerpc specific functions required for 'objtool mcount' to work, and enables mcount for ppc. Signed-off-by: Sathvika Vasireddy Signed-off-by: Christophe Leroy --- arch/powerpc/Kconfig | 1 + tools/objtool/Makefile | 4 ++ tools/objtool/arch/powerpc/Build | 1 + tools/objtool/arch/powerpc/decode.c | 51 +++++++++++++++++++ .../arch/powerpc/include/arch/cfi_regs.h | 37 ++++++++++++++ tools/objtool/arch/powerpc/include/arch/elf.h | 8 +++ tools/objtool/utils.c | 28 ++++++++-- 7 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 tools/objtool/arch/powerpc/Build create mode 100644 tools/objtool/arch/powerpc/decode.c create mode 100644 tools/objtool/arch/powerpc/include/arch/cfi_regs.h create mode 100644 tools/objtool/arch/powerpc/include/arch/elf.h diff --git a/arch/powerpc/Kconfig b/arch/powerpc/Kconfig index b779603978e1..5ddafd3ce210 100644 --- a/arch/powerpc/Kconfig +++ b/arch/powerpc/Kconfig @@ -225,6 +225,7 @@ config PPC select HAVE_MOD_ARCH_SPECIFIC select HAVE_NMI if PERF_EVENTS || (PPC64 && PPC_BOOK3S) select HAVE_OPTPROBES + select HAVE_OBJTOOL_MCOUNT select HAVE_PERF_EVENTS select HAVE_PERF_EVENTS_NMI if PPC64 select HAVE_PERF_REGS diff --git a/tools/objtool/Makefile b/tools/objtool/Makefile index 8404cf696954..06a9fcb4a0a3 100644 --- a/tools/objtool/Makefile +++ b/tools/objtool/Makefile @@ -49,6 +49,10 @@ ifeq ($(SRCARCH),x86) SUBCMD_MCOUNT := y endif +ifeq ($(SRCARCH),powerpc) + SUBCMD_MCOUNT := y +endif + export SUBCMD_CHECK SUBCMD_ORC SUBCMD_MCOUNT export srctree OUTPUT CFLAGS SRCARCH AWK include $(srctree)/tools/build/Makefile.include diff --git a/tools/objtool/arch/powerpc/Build b/tools/objtool/arch/powerpc/Build new file mode 100644 index 000000000000..3ff1f00c6a47 --- /dev/null +++ b/tools/objtool/arch/powerpc/Build @@ -0,0 +1 @@ +objtool-y += decode.o diff --git a/tools/objtool/arch/powerpc/decode.c b/tools/objtool/arch/powerpc/decode.c new file mode 100644 index 000000000000..58988f88b315 --- /dev/null +++ b/tools/objtool/arch/powerpc/decode.c @@ -0,0 +1,51 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include + +#include +#include +#include +#include +#include + +int arch_decode_instruction(struct objtool_file *file, const struct section *sec, + unsigned long offset, unsigned int maxlen, + unsigned int *len, enum insn_type *type, + unsigned long *immediate, + struct list_head *ops_list) +{ + u32 insn; + unsigned int opcode; + u64 imm; + + *immediate = imm = 0; + memcpy(&insn, sec->data->d_buf+offset, 4); + *len = 4; + *type = INSN_OTHER; + + opcode = (insn >> 26); + + switch (opcode) { + case 18: /* bl */ + *type = INSN_CALL; + break; + } + *immediate = imm; + return 0; +} + +unsigned long arch_dest_reloc_offset(int addend) +{ + return addend; +} + +unsigned long arch_jump_destination(struct instruction *insn) +{ + return insn->offset + insn->immediate; +} + +const char *arch_nop_insn(int len) +{ + return NULL; +} diff --git a/tools/objtool/arch/powerpc/include/arch/cfi_regs.h b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h new file mode 100644 index 000000000000..1369176c8a94 --- /dev/null +++ b/tools/objtool/arch/powerpc/include/arch/cfi_regs.h @@ -0,0 +1,37 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef _OBJTOOL_CFI_REGS_H +#define _OBJTOOL_CFI_REGS_H + +#define CFI_SP 1 +#define CFI_TOC 2 +#define CFI_R3 3 +#define CFI_R4 4 +#define CFI_R5 5 +#define CFI_R6 6 +#define CFI_R7 7 +#define CFI_R8 8 +#define CFI_R9 9 +#define CFI_R10 10 +#define CFI_R14 14 +#define CFI_R15 15 +#define CFI_R16 16 +#define CFI_R17 17 +#define CFI_R18 18 +#define CFI_R19 19 +#define CFI_R20 20 +#define CFI_R21 21 +#define CFI_R22 22 +#define CFI_R23 23 +#define CFI_R24 24 +#define CFI_R25 25 +#define CFI_R26 26 +#define CFI_R27 27 +#define CFI_R28 28 +#define CFI_R29 29 +#define CFI_R30 30 +#define CFI_R31 31 +#define CFI_LR 32 +#define CFI_NUM_REGS 33 + +#endif diff --git a/tools/objtool/arch/powerpc/include/arch/elf.h b/tools/objtool/arch/powerpc/include/arch/elf.h new file mode 100644 index 000000000000..3c8ebb7d2a6b --- /dev/null +++ b/tools/objtool/arch/powerpc/include/arch/elf.h @@ -0,0 +1,8 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef _OBJTOOL_ARCH_ELF +#define _OBJTOOL_ARCH_ELF + +#define R_NONE R_PPC_NONE + +#endif /* _OBJTOOL_ARCH_ELF */ diff --git a/tools/objtool/utils.c b/tools/objtool/utils.c index d1fc6a123a6e..c9c14fa0dfd7 100644 --- a/tools/objtool/utils.c +++ b/tools/objtool/utils.c @@ -179,11 +179,29 @@ int create_mcount_loc_sections(struct objtool_file *file) loc = (unsigned long *)sec->data->d_buf + idx; memset(loc, 0, sizeof(unsigned long)); - if (elf_add_reloc_to_insn(file->elf, sec, - idx * sizeof(unsigned long), - R_X86_64_64, - insn->sec, insn->offset)) - return -1; + if (file->elf->ehdr.e_machine == EM_X86_64) { + if (elf_add_reloc_to_insn(file->elf, sec, + idx * sizeof(unsigned long), + R_X86_64_64, + insn->sec, insn->offset)) + return -1; + } + + if (file->elf->ehdr.e_machine == EM_PPC64) { + if (elf_add_reloc_to_insn(file->elf, sec, + idx * sizeof(unsigned long), + R_PPC64_ADDR64, + insn->sec, insn->offset)) + return -1; + } + + if (file->elf->ehdr.e_machine == EM_PPC) { + if (elf_add_reloc_to_insn(file->elf, sec, + idx * sizeof(unsigned long), + R_PPC_ADDR32, + insn->sec, insn->offset)) + return -1; + } idx++; }