From patchwork Mon Aug 26 21:35:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Clevenger X-Patchwork-Id: 13778524 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 83978C5472C for ; Mon, 26 Aug 2024 21:44:25 +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=rAERlNiW9pyPTkrb1TcDBDVT3AA+peDza7BurMjsD1M=; b=tPTktOwOcz3up6qv7VW2EAWmjA s2N0bmj493e7k1FDtXrr2iRWa6pbz4pTaPcpezWYThqeJGZcIMTDNtx0aRlBY1D4O8t2t2e7AFTam QaLtzD90j1S9e9UatK21HwWlAzFthdvtLv+GouQP6gupGRqlHbQyka7IceQfdfROdCvSpPMllhhTF 2i5bK2B9wKA17HKbc3FCltPy4ulj37PN/IIO/NljC42ot5EZnQdO2NsnhPeWIUa5z0+nKgo9RmG+w BEhrWsnMzc9Up5dPlxt4Z9pzFbFZ0a7W/jnkBbWO8EgVzjV13SQD8Kl1H3JNsRWvW8W3cjcCOfz4G 3h1xvziw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sihVW-00000008rFx-2aNN; Mon, 26 Aug 2024 21:44:14 +0000 Received: from mail-eastus2azlp170110002.outbound.protection.outlook.com ([2a01:111:f403:c110::2] helo=BN8PR05CU002.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sihO1-00000008prN-15AT for linux-arm-kernel@lists.infradead.org; Mon, 26 Aug 2024 21:36:31 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=AGC8pfOTCOtABgrtWq4AEPRbLmjHWrPZwQvYZ4t+Io2kOtYgvBH87oyPw9NkF9seq3i2oSkh38csFrys8IaJFLhYgTw5/TrFhvzqzGGDQ2V2xhFc1sc4Laqtln92B3Z6rmfRkZPp7k4uXz3wwlgWoHC/+DKK5iRrTnyO1bQ4HNfdgbK77nr9KBoBP7B0zZIe9ncbBuCaQVJKc/us5CsoAGrbnFIcw3ZktLXeZWjONP7YMl9X3e1YPX7s+FzBZsFcUfZ+RX54MHIcuorhBt2f5XK2ZiwfGnRqHfi+IneLRWrpwx5gTqyBcmqXW/crlb2aycpnr3Bhq6JSxjWsC7PoJQ== 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=rAERlNiW9pyPTkrb1TcDBDVT3AA+peDza7BurMjsD1M=; b=CUSN6slknln5Wwnji8LHc62GZLRsmrttAfGabWCN1zd6gPW11Xuw/PgLTmo+biiHNXGpPHAhhkg8PURYGUWYJ+JcSSF7k5H4Vx7GXLng3NIWuufBQAPndmxAvB7fJWNkOpiFfCfeTGdwjYOM3AdBm+34ysuCB3Re7ZULKI0UYhpJqvU2QbOPq4mpZ8tvqc5iTgogCYkubfPTlOK9b1ubZyjwgRPa95aXALYANUHoZ/LDyaiBVgxvIRhwkjCk2sYBslycfKM0g6ZbNHdHAu6gVdtAh1tNovuKphVxKrXwuFtxI0hF15C0dZv1Ka4ATccg0KX1xQbnQ81+qYry081rhQ== 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=rAERlNiW9pyPTkrb1TcDBDVT3AA+peDza7BurMjsD1M=; b=u3GcBaL+O7b1zJ0ZKdHx8UGlsA0U4JFw4kg7MVKxyyQLvNyZkm2dMof9I3bOLS3Vtmj3RL4/VEODkO2qYP8kSDZ4hddkrqaGUdjwZgR4HAuM4Zscl7zAoanF/GJUPzPJXICPsYO87sTRXlAyRdzVsNVJNz4WLJqvcrLiWwjWZhA= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from SJ2PR01MB8150.prod.exchangelabs.com (2603:10b6:a03:4ff::5) by CYYPR01MB8410.prod.exchangelabs.com (2603:10b6:930:c9::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Mon, 26 Aug 2024 21:36:24 +0000 Received: from SJ2PR01MB8150.prod.exchangelabs.com ([fe80::72ca:29db:7676:9fea]) by SJ2PR01MB8150.prod.exchangelabs.com ([fe80::72ca:29db:7676:9fea%6]) with mapi id 15.20.7897.021; Mon, 26 Aug 2024 21:36:24 +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 V2 1/5] Add dso__is_pie call to identify ELF PIE Date: Mon, 26 Aug 2024 15:35:56 -0600 Message-Id: <99684496008df1c7b5437a4c31277fa43df044cc.1724707494.git.scclevenger@os.amperecomputing.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: X-ClientProxiedBy: YQ1P288CA0024.CANP288.PROD.OUTLOOK.COM (2603:10b6:c01:9e::7) To SJ2PR01MB8150.prod.exchangelabs.com (2603:10b6:a03:4ff::5) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SJ2PR01MB8150:EE_|CYYPR01MB8410:EE_ X-MS-Office365-Filtering-Correlation-Id: 0c60fced-262a-46a0-3a0b-08dcc617227e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|52116014|1800799024|366016|38350700014; X-Microsoft-Antispam-Message-Info: MJzX9sWmPfvRHx0PVweVkQgv7RP2wZ1/CqoU4m/Twbynv8VmMK1N79furnyKWgHv36dzc3T6nHvQJ+8obgxJ3A+79EV/LhV1vmMvXvIK56II1le7L/wdZT2vCZcgz5Qs8UR2aHIEaS1U2i/9nIEOrXPZHMcGctqX702fugBxcnNqxHtF/HtfsooD0eqdTGLY3RZHfJ6EZAEKR/i8Rw3zQcM5P7Qxq7Oh1zy5mUqseN5L6jZXNCyUFc74Ec/AKHbkOhnP0oVsvNdCRQQ8nwgGdD6uqCN93rqCCEIOYvZMQDqdNjDRX3/Ph7F3D/mCuH4FTXu7m5rKbeFek5aqK6sby76CBKeg9iTJx/h+jZsSm3atZKlvJsZK3ibC+63v/i/KKw+Sg7dOE1hjzXKqUU4QZr5mY7OmMaB8x12Gk0ZdTufNVwwkVdvteXaaANpKn4XnkZMkq6c5onRCW7EbpKvcJfp2/mzw3mMZsm++OHaX9kiqkwZY7ROcXIWjCSq98aq51wU6kishZKWha/DOneqbXRvi1WvGPeV+4KRGw3/YmdHqyNaNOpPp8ONRgY5XTMdwBpSPltJexLaRpK5aTzBhLwz4iPTGk0EabmbgF2ndkCc5y4aIhRfq80sDIiPnQ22X+g8/jxPTFcCeDAr4ZGPhkCHBkPHs2yF9Uo1IoL2IL/ecwJ6I/yioyP3KJays84gJlVBw/48M8h5ZOsd5TQnTFTD4YdI163ikak9tIGRgPXjKOQOhc0k2Sl9cM2Z0wEKZiUSTPFYvwQL59C9G5mWr7RdjEzHLZHzZl2BbNdtz27TszFmUcpoCooxLLs+Mvzb8yPfRr01hxDyHqFFgojzcTTYBLr44Sb43JRyX/q6vO8g2DSGzrtdkn/+stmJGB8f20QEUNb/UNzVregJkfktsxdEgrd28BPZ2D8ylbywMSMNGP9NIp9qi1Hwr+1DOK5YsyPuofujLrfN69E1MsSIY6bkwWmXXszGgPKU2/yBZ+2vCOnxkwjkcoqHFgDPIDT+MLnbxyA4ED4cP6q/R2qjqO51o6z/Ac6+45XDps0rMohAj3BUnlt+VfVgH5gAVBJx/4KinUrA91zLMMlU/xVuQR/64gh8P91R9qHcbz25tD72vUVp36zRrKeB55c4DYnnljtQTkQs7Fe/K1QfRSgELO8wTTVr++JCagM1JWdpzC1zdQz7C9EwjRDFHNkZaIB5wcGzVc+Da8V2WBylfeO5AaJxEDLq6XeXsgoY/SZaqrJw3Qme17K2OpSZyJSw1AKJ0Bavm1qw1QAIm2Wo2pvP4vZ+B1ga5NfpDpwyv4Y6rhzTnoI6IOqZJ0VYn0hfB50M5NcPCanBw5tVJdePLfTRtaH+xGBZKPjdiK4bmkn0p6sQxqQW+6qHscNqssA/ezi+vU1h2B3ndQBUP3LtChXiJVg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SJ2PR01MB8150.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230040)(376014)(52116014)(1800799024)(366016)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: QNFZwQgeD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/JdkKp4U+m+ZIuJFqQEo9FsGmxlDPS2vEsBAdu1aPvg3L4qK4nDi/yivx/4eF2gjo0QeSVlbje0BY02OdUf5Xl5OFKSrExiUjvvPDYlR/Rt5+qmniC4uWFOYfvGrq2lDi45y2Va11JixehSEXWqVyddCyRQCuytgFoAULkqTIrEZjO4fMfgM6SOz9ITz/y8e1bPVJ5k7AlIBWWL88SJlex0iw0L7e9sNfI81IBgJylcgqJxrCYHstrvmndvxxm/mtTCcMDAnMKxm/lsAaa0QT3f8MbFhTPvPFvfdkspVRO3S1Fp5BxP44oF4F2XAkyHMncD0mMPzSjFiZ/iZ6QY9zuWJsACXf3YZjjv/4ds66TRIw5uOq/iiariGUGmZ2xenzcA8WHDTUUzIbfyS+Dxnn23bUmPi0un/m+d+2L3SAL2ugR/3j5LX5Z4SITF1xJNc+UA0o75xR7rGC7OnlWetjFEVlbEqFvJW1ZGxnxCCFy3ltbDApKC4KVqdFmeelwksrl+fEb/X4VkHA1i4WYCEE3xyomgf8FgrTVmGXNawV4vvew2gZn26v5FIDEExMukg6+Vn3B X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0c60fced-262a-46a0-3a0b-08dcc617227e X-MS-Exchange-CrossTenant-AuthSource: SJ2PR01MB8150.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 26 Aug 2024 21:36:24.6013 (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: GY+DjwMEb3xRZetddoQnBeTpYwCXYhteMheZdpDNiWKHUmBvSJgPyzw9tls5ior7h/JQDAvP0crMDz+rUPBnskirldIRdE7i84ctKjTPjbtIMU9/Ir//hT1q8tRhjneG X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR01MB8410 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240826_143629_339903_4BB0E8B8 X-CRM114-Status: GOOD ( 14.07 ) 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 Changes in V2: - Updated mailing list distribution - Decrease indentation, add null pointer checks per Leo Yan review 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 ++++++++++++++++++++++++++++++++++++ 1 file changed, 60 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