From patchwork Mon Sep 9 21:30:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Clevenger X-Patchwork-Id: 13797613 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 6A283ECE57E for ; Mon, 9 Sep 2024 21:34:39 +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=y3Z68xW8nOXTyEOcTAZJeJM/P2J92dmdOL3G/97x0/g=; b=m6EO/gCKOCPsAGmkIPg9ndbPNs PwktfNWcab+RSHFiKQUol6mn9J2fxo3CMPLKZCHJ6P2ygL/PxttwJc697+JPo9zP7UVHJI0yXci9Y TPhbCs6T11MO96l0e5Eueys5QN1UDuRAhcDm/Ywt6aMKyvks/fRxIf5kCjsRXZONkI16kACZ6rxAI nG0x1yPqYZmwGLHODK5oWQm9BJO5R9VpgFjMXKVNfbPVWHzGmG7NYFRr95z+pyAaIXAuBkzRL7byl Byx02mGQHNM88JQJk87YqQweLRCZVoh8HGK8foEFCK5uFssc4QijMlB5IVaEKXk8ReCAdE5eae4dY DpucamEA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1snm1i-00000003NGS-0uNt; Mon, 09 Sep 2024 21:34:26 +0000 Received: from mail-dm6nam10on20719.outbound.protection.outlook.com ([2a01:111:f400:7e88::719] helo=NAM10-DM6-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1snlxp-00000003MpL-3HEF for linux-arm-kernel@lists.infradead.org; Mon, 09 Sep 2024 21:30:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=bKPl3+cMTlKpwR011bmjeKjNFCBq1r7ZwXHFwZbFX8N2rM3Kgh16KH4yyNpMe6MgT5BA1KcudthyKSJQvnC9mR4wrDnbbRIR4iRDLRuKWohKRkVZV8UodtcBl6OQfuerxtmTXVtZrBvQLSdqbBe1wgO8dnLU5ZTPEjZXE/YTP3fffZuf3Wq0BmEBao5M3NfjXMTdtLdIJATqB0wlf45cWw2sWP2v+g928xaUIHV6aAZbfGVtI4Le1t68rDsqsW0B4ctNlm4qlvpiLF6YUI2UVkWOb3DNuIf1DvZMLWgUBKOHMROEVUddw4Gh9I70Wpptkd9ZUuxdGJznShAidSvgfA== 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=y3Z68xW8nOXTyEOcTAZJeJM/P2J92dmdOL3G/97x0/g=; b=CLiQxJJE/UxQZ4a0Mxn3ilMjoOnR4STt9C9DyrHGQENa/rvBSV1SUnB1a3ZvoGonkdGLEQXjUlMYk7KN6XvdOfuB4onWwkwe36VKpvTjkzEsYhGYtB42LyzDHAVm3n+F/Whr34xuYlcpm4Y91pIjLM4rh4GlzXA5Cm7QbDMDGaffOupOYOLkytIjEYnlM3nu2G2HElkdnZjnnPwkYAUqKf2Mddiqx7oqxZAI1exP8MBvDPi8FVRbrnqdzxjpqKlZgHso4SxE0lxSqfufLDET2FwqT/N6ct0YkL34YRf1r8Mh3TNoUqHkZfMwCRwR2CxU+VIoleERu4ucTwIwNMww4g== 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=y3Z68xW8nOXTyEOcTAZJeJM/P2J92dmdOL3G/97x0/g=; b=ZSpz2NKfZkZL/ccuVCvo7VsQ65UcHuBXpX+u3Tj3aT1Y6xaGXLc5EhVPx/xV8zle0akcYs8AQM0C6MSF/plNdKuInaf8wtV/gYJW/+ZMXi0+hWtGcLhYpO3M92lnh5BwXW0mDQ3IJf3NBZZaBEvV2eYPa44rxf9LSLbu1a+UKsQ= 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 DM8PR01MB6919.prod.exchangelabs.com (2603:10b6:8:14::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7939.24; Mon, 9 Sep 2024 21:30:22 +0000 Received: from PH7PR01MB8146.prod.exchangelabs.com ([fe80::2972:642:93d1:e9d4]) by PH7PR01MB8146.prod.exchangelabs.com ([fe80::2972:642:93d1:e9d4%5]) with mapi id 15.20.7918.024; Mon, 9 Sep 2024 21:30:22 +0000 From: Steve Clevenger To: leo.yan@arm.com, james.clark@linaro.org, mike.leach@linaro.org Cc: suzuki.poulose@arm.com, ilkka@os.amperecomputing.com, coresight@lists.linaro.org, linux-perf-users@vger.kernel.org, linux-arm-kernel@lists.infradead.org Subject: [PATCH V8 1/4] Add dso__is_pie call to identify ELF PIE Date: Mon, 9 Sep 2024 15:30:01 -0600 Message-Id: <18bb5281c3648a67bff61ce44966d3dfd7c09b4f.1725667397.git.scclevenger@os.amperecomputing.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: X-ClientProxiedBy: CH3P220CA0010.NAMP220.PROD.OUTLOOK.COM (2603:10b6:610:1e8::20) To PH7PR01MB8146.prod.exchangelabs.com (2603:10b6:510:2bd::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH7PR01MB8146:EE_|DM8PR01MB6919:EE_ X-MS-Office365-Filtering-Correlation-Id: 9d3235e8-b248-44fa-8429-08dcd1169c43 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|376014|52116014|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: vlSqk8Ac1APLiKJG/bomcLetSWIiGIZtPZJ58fvlw+wWx1SCl9ZQxbNHGCAIIEnpFcF1coU6jzLVfb3EdTq6A6EVpqqagC7J5Zjd98VvfGjJVz9r9/wxiWNPNVb5Z7l3wCl3DI2mguCZ5mI3byoIsnyW1d7zR9rvYap4B+lCSoUvrLjVIYg6M/+sbklYBSpNY66NdsnE63e3B90vCatr8R6gnuHXXNAIxON6U+PYHZa8ONbpSNeTzTnfhEM06jVYyxXutyWvG1KpI+AyYstixemT8ocS+UX7m62yP2syafMivW953nJlb3lODXAmo84Z+UqgywBQGRxR9867dG1IObnC9tem9SPh5naEU/EQswQlxp52HHJJFDPWRigcrmFQjzK3iMOGYzD0lDKhaVonT41FOOQRs/+n60x4QQnF/IX0lO3jSdsx2ub26PLEylOEaeB13/ugP8KceAIlMJp9JNO4k+qWf5uTEJ3VDT98tUol53SoWueD7wlbZVwHETjoVSazmTEBdP2ryg5fvhR+A1+lGCVRZuP5bK8YEX5qJ9KnvDV+yhwSgmIqyq3sC1w7N1qPsSxfjpTmpDw93NLWLoKvpNEBRND+Jq+PV383jOvw200fWTq0XGYK2k3ttKlk9aIPccEmC3nPT2O++GeK3eUoGmk57WkpYMBBAzrXsb1isKLGAAaCw/iAJVDFbmRwt7T58n6rjZBVdhHxrYevlY/zPX/NilSXiYy1hldj8PO4JbdqalizL82+mGmwPn5lXEIOuUcbC79BG+enWkxbwgmjycbf4kXEQHDNYnCPR/wbZofiHEA7xr4RBSRhn42DRl8nPCJMl+/rqf49gcBYR1dtZNrbuIKMktFs3md43Kl2ijIPMQlQTT95gLIKU84gzQAY9+d5zr/o4P+iLC75hOwlZ3fKGlB7pUFL3Oyo4gGYarRrbytFtjL0KCj3F1IcQrzkCbAIXxQT7v8TTLkzQGk1s8AJuoMp1ZZ6U//7P9rULcr2dTkfSo4skOhZ1OkwL7/e78blAA4xGGug1FG2g/16JwEBzbSVFGZH8/VUogFd0VpR5KXfXbyJyXGBU4W/teLG/GY0BAQDCQ8+owineOqeoDERzCjxx682gcHNz2ZBySqu3PysENmeKI3izkTcu2DvYBV9jnz+ikZ2eihfqR7N3rCCzULWjNnGNJKaC2hu03eacWhHBwk7bNknFypw3yNdrM/kqVqk014cth75gX7d6v63o7wqcgEzhTMidzapUynGR3njDYheHz3m0uAMie4aUvsP++ogtqJsM92k09H6T2ST73dmrt6GFSRVOkCC4ZW81oD2Nk42hHZTmYs2ojud0InfWPj7MMmDZb/eNn0n9Mx2PNNpPBdVEhSMAHyqMP09SRfnqFUnumyhVqPdjaM7hJKdecaee1BFWSXUFg== 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)(366016)(376014)(52116014)(1800799024)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ih3RTh1rp2ITDlbgHz13WQBC7Pao1YE94RNGG7wmyMufKZrynZOVUUY+x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u3lLGixXb9OZeQbFrEsDSPSfCDqXh1c4cEMxaz22tWPqwgnVtmn/GNssj9Rbl6hhYjLk8QbZP+BFrxruMB4BpEpraaXGKx5QUrg36zYUJvxmrvFR+k9VKbzasyJ6QSviCZscp+mh8bIpkVlBN9KhvvR3KgGtYoKVbsH7AP2Dfv+oMyDPwREybhILKEQZ+619/6iNGczuiOQMhyi69XEd5t9COf3W7PE66kJwmUUXsKLSIGSSK989aXuVSneqBAGh/VCRJfRdYo8/9MgRzW+xvfQd4kYuSaAt1p/W9pmu1AlVibtN4MnzmFe3ocKUOsZ7+Tq9ltPSwTqE3Zl4tmmryXy9cVutq401fP/I/cVmHAWwuE7ovULn4C5yFH04tvb2cn5veOMFO6lvdF4hW6NhH0m3+t6fBSrczMJFYBcXx0OA8W1ZkLaivjBVbRiqM+tOG44591krhq38S029OV0ULD0LKmIaQ3TSnngHr96ePGZ1gVrgs4otyQj/sJg8CYJpzUZ/3HxO1 X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9d3235e8-b248-44fa-8429-08dcd1169c43 X-MS-Exchange-CrossTenant-AuthSource: PH7PR01MB8146.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Sep 2024 21:30:22.2061 (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: vdHEIm0DJHoBaLkBkkayqXp8MZvnwyjanJGGSxWbPLDevC4G3dL/j214oJNY9zCDoRyPyDbzU4yi2959GAhODObFmVkTDET99VmkcY7M+KszXQ6FErmR656ve1ssi6Yo X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM8PR01MB6919 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240909_143025_850743_DB4C554F X-CRM114-Status: GOOD ( 14.43 ) 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 Reviewed-by: Leo Yan --- tools/perf/util/symbol-elf.c | 61 ++++++++++++++++++++++++++++++++++++ tools/perf/util/symbol.h | 1 + 2 files changed, 62 insertions(+) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index e398abfd13a0..babe47976922 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -662,6 +662,67 @@ 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_close; // false + + data = (Elf_Data *) elf_getdata(scn, NULL); + if (!data || !data->d_buf) + goto exit_close; // 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 + } + +exit_close: + 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);