From patchwork Tue Aug 20 22:11:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Clevenger X-Patchwork-Id: 13770612 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 307BAC3DA4A for ; Tue, 20 Aug 2024 22:16: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=LWT9byCzx1h/F2NimXQpIKNzwc0SlRD8qgdi/yxbFJo=; b=dMYlBdo8gBvM07SFrF86dnt8oG 1suaRPa9YGPv/eX/PYjBTb1C4/UatyHT9vZeLMUA7YRBJK+2JvWFXmTIsMzqK0LdO9wN+J7Pe0n3o z8av2ctxOnPqNlSISLCjAPyM5MCif8p/pdJl2a0YHITdxn3ovrHOAB8OdTxJPburJOyQlGXrv74W/ MKYuTdzjLGFlx6/fl+tqXkMJ1VirppGuo92R8y5goYkY8U9kgf0TFzwfSrfjTtyjU+KrH0DSC1v0z hGRj5i7b3m0yasQIR5qavFGqVL/iNDVHhIHMDZMGfdXYS2uOKfr7n57JINSe6FV7/kdDaJLfZctXH 5E6nuOsQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgX9S-00000006n2m-2xCq; Tue, 20 Aug 2024 22:16:30 +0000 Received: from mail-westusazlp170100001.outbound.protection.outlook.com ([2a01:111:f403:c000::1] helo=BYAPR05CU005.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sgX67-00000006mCF-2orG for linux-arm-kernel@lists.infradead.org; Tue, 20 Aug 2024 22:13:04 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=uYET6vm6D4XTyJbO3QxC9F8b28LtmXnbuW0+4DQphz9aA9NBfO4Hi8iMQL87+qrb+2EhB9ILqZmRHyER/Xn6zYZMWZDSR58Ax+hL8qMGcoHagPAZswy2+0m86EaNRm1XURF24zGtPbf+wybAqZMappGWISLYdXvs619RkwPAtLeZe/8sSJLHMbl3t0yBmF21c4LtL18I2w4DTiB36RQGaX5t3T1o7bS8isF0q+JyUkLsGeVAR2qPTiJRRZRTG0L1ClNHZk6GIaAYnYzbah6Y88IiEAiSBzSn2Uaj0FBwIGrS5SJ9phO2cG9C4TGov+910GsKkaDOJ7cMnHFqac5kFg== 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=LWT9byCzx1h/F2NimXQpIKNzwc0SlRD8qgdi/yxbFJo=; b=antKFXNbJfSv2xn7KidxmB/n5TTRGc/oGF10ukOi48T0E1WsOPRD43VViHGg51kxuXi/1kvA3h7mUVHbhzmqmb8JYoUjP1gNm4Iq3IqO2ddGz4aEYL82WYLlTw0Nsx/JUapkrnun3UEDcfwm8s/X1bf7/igTJEtIAa/RQBU5kE7Yy72n/FTaJCnRmn583nJYIvwHVORAy1AUMBqYwMxTawgEa2YR221Xt4z0nCDO1I2z8OuhEHefdUFI2AEZ7TjmtOXLrjFLq6UCRcq19BlZ04aTEHd5djFxhyyw3MxaXVVirdZRs7xfx09x2kKa4LAbHRk3cSCcC4N4cDO/TmL3dA== 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=LWT9byCzx1h/F2NimXQpIKNzwc0SlRD8qgdi/yxbFJo=; b=aNWwGz/mmId69mq+fClgh1WX/CCHCYNAeb1dCy20xSFgoQyQ0yMZiClTfN5rN9TdwuAhV5UAp7ECknP5y5LIgMXY7bxeMbnMqOSttpMjlOTT6fDcynqPA41jXQYvjdiY7ey7KhS8gvRDFvTuNNrMbUQR5vWoPEuoswX3En7sV5E= 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 CYYPR01MB8601.prod.exchangelabs.com (2603:10b6:930:c5::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7875.21; Tue, 20 Aug 2024 22:12:56 +0000 Received: from PH7PR01MB8146.prod.exchangelabs.com ([fe80::2972:642:93d1:e9d4]) by PH7PR01MB8146.prod.exchangelabs.com ([fe80::2972:642:93d1:e9d4%6]) with mapi id 15.20.7897.014; Tue, 20 Aug 2024 22:12:56 +0000 From: Steve Clevenger To: james.clark@arm.com, mike.leach@linaro.org Cc: suzuki.poulose@arm.com, leo.yan@linaro.org, ilkka@os.ampercomputing.com, coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, "steve.c.clevenger.ampere" Subject: [PATCH 1/5] Add dso__is_pie call to identify ELF PIE Date: Tue, 20 Aug 2024 16:11:35 -0600 Message-Id: <323e931fe9f8f080eb0dfc2e29d112dd7edf1fb2.1724104248.git.scclevenger@os.amperecomputing.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: X-ClientProxiedBy: YQXP288CA0033.CANP288.PROD.OUTLOOK.COM (2603:10b6:c00:41::30) To PH7PR01MB8146.prod.exchangelabs.com (2603:10b6:510:2bd::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH7PR01MB8146:EE_|CYYPR01MB8601:EE_ X-MS-Office365-Filtering-Correlation-Id: fc0c151b-ba9d-4e45-63a6-08dcc1653ead X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|366016|1800799024|52116014|38350700014; X-Microsoft-Antispam-Message-Info: Wsir/4LbK40O9nFlVkRK6ONIHNQ/EQqVsWqm9Qx3lU5IPQvTwHvjyraGrtydMFNn1t8rzso9b0ntrESsTl8jpMi/W2TObJCspV4jRhW2vLviNyK1Xsk8FoQbXT0s4RHMiTQJAVhDq7ZxQo5Xqlp5JeNeKAbLJL29vvIqVDl1TFolHU/NtffZWNVHhp2h0NIMYGvM9MC0iZvbU5fiW8zyJJHnf+RxWWKBCf8FIxPj5qIl9QTeXEQHLJo/V07degdBDB/dy77s2UZaC9OvIv86msf36/ZKfoE15DxrFrzAVki2ygKl8hk4IgHyDz9qU9sq1c+KUVCrLJeEb+0g0d5I4AEWtYBXxGeH8+LmFgIYU3D9hL9z8W+E4qon7wBOG3VrPLOr6HeVQP4rADgLzTkHihseRkyFk/iIYCmVEXAZ1Y0Z+vTfU0c0kPNGw+ltAM91tkrrXIyXX/u/GBrRUMzPXZ/lKaph4sxeRguZZ7uhalRNOAvn5JHprG0dV+piVZu4RAwYQHkdq2tX+BZFV6krKnsWYaWjmqp5z5RjWzMw+tFiCV2j2XGE+M0Sihh5rbQnBGs/3R5nZPRpubBnENJatuRQuq9RefAvTCZGYJIBvFCQcqFliHz2bO203inyThtDEuMxmwwdJXKoniYUkhRN8hbVuVgjveYh7TOPVp68vrQcCwNxfZXuHtEY5um3NiFojvq0bVPe0M0FOKsqa9ynC5UP656MQKJgeBJ3fogpX/gIVJa7BCQ2uk21yD2x533RUxwKNcd7J88Ks6OFGSnWsblJF6gCRWQDBs3Am2wYEDAAMGDEESxiJP/FHaDqkyqhMFb43Nmognda7teNZMSMLfTVn4Uk6TJfF3Rv8aUes/LhWk4J8etfEXW8e5pgupjlQ4zSkW0Vl7rh0mcJbYHU0FhdFYUGLOUW2tWtcu4KeQOEPuTdjdlZAWzKKbjglsu8lB1k1C3eTCWbuf4m8N3RaAn4v5+/iGqM+OCX/Mhqr4HZ0WC3ka0v+hNqeJkOvDJf5YkDVTfqQb8GNAUEOrpxeWcXmkIVYqauG/9Hv2PHQ6b1fCAZFJomaGQyaHDBF6XhWkziGp5/WsHOmWVBJcK/ZBALvnBdaw7PU2oc1QMKpLe/gIQTSW9IRsIPMfngj/3C2HsoWrokhxUt7GSV+CIvedrDbd10qF0KBkkJo6mswTIxTbGD74Gf95up6pmi6X4C1csbXzDXxI52IUhEzkQD6DabIsUj3lj6PO6ehXt5TO9WM1LRMLP7fnafL6utfBGR+PQEAiTEOqDrxjUHmckGZ2FdzJ4On+DbclgZY76AxT/r+8IK6aqnMnU9PsqwX6Gx93FHpfEFgnynTQWWyKstJy0HXxT4Ex7Rp8rXaRVdcPCf57ho35DrQIVpe34IHBhTcWeY/6z5a7xFaMbye0pRkg== 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)(376014)(366016)(1800799024)(52116014)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: vp9Ky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QCD13kWWDGv4NrU26lb2juH4J+oVLeZ4Wdr4SJb2OmTxrI3pmtnpKJxFggg6DrtGXPDUiz8f/83GcrKyZXCnRuSlq43qJoF4gMNKB1uITI6QnGFD9HGi+QiR22+1dtzXoOcWqW+U8XqouskZxVuzZ/++yQ1DXk9oNh/ZAyQRswMmWhv2Rv7RS6yhcq6DZ4xvRUKpAOq++PZIjcNnNEkp3ZbzhVYwVPDdF/Jhjw6Y4Jf04KBXP/isMfKDAJibdn4gp/L9WAXbbK9RN9D/sM2lHAUdGzvhgDI6gnlJHmEBjOrRzkFJt7RsTDhnjIU54UsSkAclVbs+Z8SVs8LON9an/bto3IhXpmQZdk8I0BO9eYdT8CknWq8JMaHlgBsut2fVcrynnOmxDpoNv/HcnGzpa+A8q2bDxbU5ivpbc/eM361i0yFbnOxNrzcOAlaFIbBuoUoEt1Qv2wRdMcJUsd7xLPHp2wsOTEbfzRz8IDW0nvFKNggjHKcqqTsKJqbs/o05TiFSRFYzzI6oiaqp2cIx7N8S7f7wVeDC2ucLkFE+y/52tCZVMkeL2N+cJYl6diJIY0md7KEeWcRgA X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: fc0c151b-ba9d-4e45-63a6-08dcc1653ead X-MS-Exchange-CrossTenant-AuthSource: PH7PR01MB8146.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Aug 2024 22:12:56.8273 (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: 0O0fNvdJdjQlD/XRgVWiDhzcSJrxUbO0lTYxd7CmebRuooqtckvxMuLgfgkIPT36YEpYovlRhYZ8VEf7c4+uHJgOhCWVkcZ1LB0/7yb5g2E6kdWH8WFQ555nGsKa9xo6 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CYYPR01MB8601 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240820_151303_749539_44CDE811 X-CRM114-Status: GOOD ( 13.34 ) 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 From: "steve.c.clevenger.ampere" 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.c.clevenger.ampere Reviewed-by: Leo Yan --- tools/perf/util/symbol-elf.c | 55 ++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index e398abfd13a0..1d4bd222b314 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -662,6 +662,61 @@ 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)) + return is_pie; // 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); + return is_pie; // false + } + + elf = elf_begin(fd, ELF_C_READ, NULL); + gelf_getehdr(elf, &ehdr); + + if (ehdr.e_type == ET_DYN) { + scn = elf_section_by_name(elf, &ehdr, &shdr, ".dynamic", NULL); + if (scn) { // check DT_FLAGS_1 + Elf_Data *data; + GElf_Dyn *entry; + int n_entries = shdr.sh_size / sizeof(GElf_Dyn); + + data = (Elf_Data *) elf_getdata(scn, NULL); + 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); + + 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