From patchwork Wed Aug 28 01:50:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Clevenger X-Patchwork-Id: 13780441 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F1C09C5474A for ; Wed, 28 Aug 2024 03:15:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:MIME-Version:Content-Type: Content-Transfer-Encoding:References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=EHWC+SfKQZU4x5+3U1K0Xq7drTMGvU/j7ZyAQp0cv5g=; b=0Po79qCNFhobOjzb4lEIGL/QkX TQi/PxmxgOU2+iO1wE6YdKxUMVrZ8Ojxh/TGVuKYG0MM7Awt+vzIZej+vBRXe9Uwufj1oo+CG6PuK dz1Mo1tOQivyc+r6e/QEEdJZMLslcqcfr7OB0raJqXyn60GPMbyhqE+0zH00HdsucVmLPfYWuOhL2 nv2GIFwdYzTBi7KQZmR/TN8InE+tYU0ErmXJn82xjFhVaN0Br1XD5R9DsmlTuefg4/R8H/ndfxE3y sJGeoaPIMzWkmpuOKS1pKWxi1ZAY8Ex7o3xdACDtMltPZ6+nQJ88Bc7vzBGVV0kLwrC6qgWXNUvqv qjwQ5cdw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sj99m-0000000Db2e-0cc9; Wed, 28 Aug 2024 03:15:38 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sj89v-0000000DRo4-3wWg for linux-arm-kernel@bombadil.infradead.org; Wed, 28 Aug 2024 02:11:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=MIME-Version:Content-Type: Content-Transfer-Encoding:References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Sender:Reply-To:Content-ID:Content-Description; bh=EHWC+SfKQZU4x5+3U1K0Xq7drTMGvU/j7ZyAQp0cv5g=; b=jelF+vwAS0v8/jRtQ7+vlB+cyH vDQn1QRSHiVuIvYM87yJYOY9tYx03ta55PLgFTnsy4hKpUZEZGs1HixgpeTKI5CBylHKXWwx0BX8h AVRTUmV9CQVzFyEc7KM2GbM8Mz7xw+kpRk2J7GLDI4UKFSEcyfpG0oLB7dwBrXHgM4LjzGTAfWP/L iyk9XEj1lgpgnu1cZYFesNRcu3ZxHFwtS/Ff7wNca2uFibqe4sWB5olrWBojPa5Wyup/3QhlbUJUZ ywFQ+Md/E2Aisxc66USj8wgFrXEjcMZD4lqFq3lJtQw7ZhVRwCS0iIciLvVd/WMyktCx+d17FbgWc 5YEQVkRw==; Received: from mail-centralusazlp170110009.outbound.protection.outlook.com ([2a01:111:f403:c111::9] helo=DM5PR21CU001.outbound.protection.outlook.com) by desiato.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sj7pk-0000000Azbp-1ZUw for linux-arm-kernel@lists.infradead.org; Wed, 28 Aug 2024 01:50:54 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=RNnpJ8Ll8UadOdmDHFXbNPz9UbOXZUrJgMf98MM0VpUj/R7n9NdC30nUUKJI1+o4rS6adzW3MhAcCJGDlzetbMNGm1jRURk01/j4wAz1rSBwD7sgH3lI3HlVNetHNWOyaGKH90WCvVKWlpNJnQlqjecozwkeJqaXIOqM4rQuDMZt8eQag9AiAAfF/YK21QCqGg5jzxn+IdSFvJwZvq7MZnsFkcEuALpfi7Ty65ZvkrRRA/je1sG5wkbs3hX7q+0teWMkQ6r7hF3IsFyKhrGevvW4qwdoBSLQdb7MyTgoDhM5evxzErlq+zOeGJxVs6U9A6LA0YoO1fxuuELE7oI39w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=EHWC+SfKQZU4x5+3U1K0Xq7drTMGvU/j7ZyAQp0cv5g=; b=Pae/J5dVy+dpZE1eeNkTOkRRQigQrx+67mIn6t679UqezTjWm/7J82hgWtsB7IULjGNRpWRilTyFju6N+eBTM/77WN2lQoZi11Oh/UzxW5Ceq94jwwC5moMTGyYdKUI4SSsqVMQLnd88KaruD3N62jskNjnD+kfwnAa5JCQH5O+H9VggfpyMiBz1U1N5gSdWb9KwB5IlCFWm8xbL+yuWS+ovZK8dKcr+68YCb7aSY2jFj83NnRN5u/f3NfsHiR/3qYEFgCFNpaKR0YdaXXXhHa2a1ieRVv2If2n2QBup3k8c4qe4EuNrW4c3NPV6W0ovdw60aSdWVeL6RvtLQ+Gh4w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=EHWC+SfKQZU4x5+3U1K0Xq7drTMGvU/j7ZyAQp0cv5g=; b=PZOVqYIyvSrt2oIUgzKdcvidec5pmQfu7T6Kn6rHUNihTbQUcRWtPsGBMi3ox8bSaCgJB87QUk5pLF+SZ1WZs3pR9XmLxQdPhLxxq9B/LuCP7z3bhbKmLrTsIfWkgXxVXkrrUzJMaPVPoKA47JczxicWYOUWBRyUa3c+7ZJobE8= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from PH7PR01MB8146.prod.exchangelabs.com (2603:10b6:510:2bd::18) by LV2PR01MB7576.prod.exchangelabs.com (2603:10b6:408:17c::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.21; Wed, 28 Aug 2024 01:50:38 +0000 Received: from PH7PR01MB8146.prod.exchangelabs.com ([fe80::2972:642:93d1:e9d4]) by PH7PR01MB8146.prod.exchangelabs.com ([fe80::2972:642:93d1:e9d4%4]) with mapi id 15.20.7897.021; Wed, 28 Aug 2024 01:50:38 +0000 From: Steve Clevenger To: james.clark@linaro.org, mike.leach@linaro.org Cc: suzuki.poulose@arm.com, leo.yan@arm.com, ilkka@os.amperecomputing.com, coresight@lists.linaro.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH V3 1/4] Add dso__is_pie call to identify ELF PIE Date: Tue, 27 Aug 2024 19:50:16 -0600 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: X-ClientProxiedBy: BY1P220CA0004.NAMP220.PROD.OUTLOOK.COM (2603:10b6:a03:59d::15) To PH7PR01MB8146.prod.exchangelabs.com (2603:10b6:510:2bd::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH7PR01MB8146:EE_|LV2PR01MB7576:EE_ X-MS-Office365-Filtering-Correlation-Id: d59bf81e-5ff6-49b9-d65b-08dcc703d0e3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|366016|376014|52116014|38350700014; X-Microsoft-Antispam-Message-Info: Iqae7ms85OcDHJCZLgD1Ql7B+BtW5yVTX3fOJ6aqn0lgm/XspWDkv4ORIkQwnzXiUeBNVil460f7QfGSvc1czhWl0AHlwlTcUkDeD2Z6udTl3QfbOK1hEr/AmFz6aFEbCJCu+B/rr08T1tCDGIb2z8xZ9wJSgthMIOrz/W0MX44xohV0CFd2k6dvyLLLF85GUQwGGftroD1BhbITqfug0iekwYk3bsO5CztEsMcloqPV/aSLKe2v7O/wSTYLCsYH0tsYKWtUtQ6k7IwAHfUj122kUeSwsxxuyHsAKbs6lfwtZ1EwGQMDiDhKEmJ0KvV89bLKRdaM+P5zmsCuPjtp0jxkfZHRdQalHlj77hD+DD9w+lP7xUgCrBLBec8WDrddtIS3W705ZXsUSaU5vFmuLhuMdf3JMbBpsYmxLz8aBE4p9atfeFOwxEbzYnyUj+2hqgf+jeCbaS4lSCAHF/Q0PmdquaAGIfaJWala8NHddFb6WTv0bDO0x17etPYoTxd1FQqQF82YIsUr1mHTSyctiCTzWUpb+Ymwympo+mhZ0uBM4p1QOvzNkZd4NSadY+Rfb1qUyn7DSXx0n3dhZAezGZqWPcKI5v7CW0PMe2LQ0U2jCSZvV04vCatlOaz99/Xw7taCeXe4LbDHCDOk6HURaYjGewhLGJKN5VEwvOdw6We44Fjm2LGZDOjiq9Cj/Ex34F6fpHGFA/JaVC8/P0BiWH1+cJVzlGZY5W4hSk4CkP8hP1UcpRUUit21aQbi0N3VJ7xMsETS74Faun9IUm99rr32X5VOxKTff3rjA9jlTjG3wDy9pto+zYLRzkGFQa0oTKbK4b/38xndbI5sNCdRCRVphlMMHW12Lf9yB9NZRAxBDjx0y9vj+9r/aEYKPsHZzUupcl87+4yRi974aomL4EC869XW0TFo6irZ+LuqEk5ooPNJYhb8Jcm5rF116EAfwiq8PdPnhjRBO0WkBMHZoCVgdw7k2mKlAj9b2FInY/+2ERQS7Cle7nLeDn/qAKRxoWiDdV4aM4J/D6eBX8WodLHKuyaA+gUmjg4B2NFbJ2t5/m0bZd21F+q5s+HIxI4o8xDMLbIhxV1s8HT9dmtadsoRz/PmggXeqmzPDE5445imqvBlm8H0qXYBWj42dSidemHyL5w8rDCl2UlY/qnA3YlfWXfcx0uRn5dpYL6Fu2Uzvr67F5EsUhTs3GA43mc4EIWP5uDXlmAtq+KngmqCMMDsU3d2YBesTJskgZUf1pSif3X+D6bYrN42rmv30NlAGa3A2g6Dg1KaO2/zTQu0uZpfNZbugt8WwN3fZ3wG1lZ3u1QrKKNTW74MyUHc9NmCRIax8u/CrKhsOxjsf4UTdnhMqou3nvxYMSlBjuQj/skjUSOua2CPRCJ6A1F3TUnzM2XCwJCXdKiIuO3kjyo4QA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PH7PR01MB8146.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230040)(1800799024)(366016)(376014)(52116014)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: DKPf+3YsXlXSldmD8SJZxDVayeuPZpmdt4inuTK+wXuZFwgSpzfjcnEwM+QWpjGNgaSJRdRCYXmv+1Rc5xZTm42EjdUz8rdRLwPORWZLbqhAmLu1Cm8sTCEdKsxEZ+idPcBi6aiorMWFU5hgguNh/1PxyJ9HmPOg22pg47k9a7z5zLsvswVN4JMQ8Igdwow9lzH9BjpHIkL31Hv7KF7hAvfv1zOr71796FYugzOZmklDbncJ02JXijfH7ohSzVhf+42Y4s3FXjAx910KGVggTLcChSedChawXtLvY+OCfMbUZEHj+Y1LQZW+fIRfjwRy8wMb4tRXpYjL/luwMNXllrUfhWQzaXOekN5xY3fqvslkbNaQsmmFUpydK+nNuv92fOzRyMp5x5fAXCxcPLRHQmQEahGZarvS3wPcc0357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aeOI5FXMlUPU8bjzcMgBVZG40Cs6b6TYbLP0KKl7xcl9xLYLQA7+gOPl05VPzc7jhYSyxPvGWai7yZLYv8SHdWoFNQb1+SUJdspj7gQY7Zc/lPuQ6RKdT3UXO5VF2gvL29oiZ95JyqDb+vasPrZjBX16a5qCidy/0aW5rx4u7 X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: d59bf81e-5ff6-49b9-d65b-08dcc703d0e3 X-MS-Exchange-CrossTenant-AuthSource: PH7PR01MB8146.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Aug 2024 01:50:38.4135 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: siugeZ5m5vVKzOByMeKIzbN0IV4/r2QCp8jBB1BTnxBIshYQZD0dFcH/IYK81QW6QMk+UBvOn1hILaS7saYvUPe5Ky8wGJ0XpwHQ+JM9BbJlAq9Y11In2oDsvSj3Sc52 X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV2PR01MB7576 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240828_025052_707366_08D49861 X-CRM114-Status: GOOD ( 14.27 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add dso__is_pie global to read the .dynamic section DT_FLAGS_1 entry for the DF_1_PIE flag. This identifies position executable code. Signed-off-by: Steve Clevenger --- tools/perf/util/symbol-elf.c | 60 ++++++++++++++++++++++++++++++++++++ tools/perf/util/symbol.h | 1 + 2 files changed, 61 insertions(+) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index e398abfd13a0..0e49c1345a67 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -662,6 +662,66 @@ static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf, return err; } +/* + * Check dynamic section DT_FLAGS_1 for a Position Independent + * Executable (PIE). + */ +bool dso__is_pie(struct dso *dso) +{ + Elf *elf = NULL; + Elf_Scn *scn = NULL; + GElf_Ehdr ehdr; + GElf_Shdr shdr; + bool is_pie = false; + char dso_path[PATH_MAX]; + int fd = -1; + + if (!dso || (elf_version(EV_CURRENT) == EV_NONE)) + goto exit; // false + + dso__build_id_filename(dso, dso_path, sizeof(dso_path), false); + + fd = open(dso_path, O_RDONLY); + + if (fd < 0) { + pr_debug("%s: cannot read cached %s.\n", __func__, dso_path); + goto exit; // false + } + + elf = elf_begin(fd, ELF_C_READ, NULL); + gelf_getehdr(elf, &ehdr); + + if (ehdr.e_type == ET_DYN) { + Elf_Data *data; + GElf_Dyn *entry; + int n_entries = shdr.sh_size / sizeof(GElf_Dyn); + + scn = elf_section_by_name(elf, &ehdr, &shdr, ".dynamic", NULL); + if (!scn) + goto exit; // false + + data = (Elf_Data *) elf_getdata(scn, NULL); + if (!data || !data->d_buf) + goto exit; // false + + // check DT_FLAGS_1 + for (int i = 0; i < n_entries; i++) { + entry = ((GElf_Dyn *) data->d_buf) + i; + if (entry->d_tag == DT_FLAGS_1) { + if ((entry->d_un.d_val & DF_1_PIE) != 0) { + is_pie = true; + break; + } + } + } // end for + } + + elf_end(elf); + close(fd); +exit: + return is_pie; +} + /* * We need to check if we have a .dynsym, so that we can handle the * .plt, synthesizing its symbols, that aren't on the symtabs (be it diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 3fb5d146d9b1..33ea2596ce31 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -127,6 +127,7 @@ void dso__insert_symbol(struct dso *dso, struct symbol *sym); void dso__delete_symbol(struct dso *dso, struct symbol *sym); +bool dso__is_pie(struct dso *dso); struct symbol *dso__find_symbol(struct dso *dso, u64 addr); struct symbol *dso__find_symbol_nocache(struct dso *dso, u64 addr);