From patchwork Thu Sep 5 00:11:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Clevenger X-Patchwork-Id: 13791560 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 F13B0CD4F22 for ; Thu, 5 Sep 2024 00:16:21 +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=QoI+6VwUPZbAITKOFRSU33xXl7 BxGwcfBV2qYkg9CLH6JGpf5mz2w8WE7BEpG6R5+rpfTauuUTuI5bg+MIfGs5fOr4w91lpfTBwsgfn 73cClCkJASNHrT7d4dpZ92cLidlpW+BGWonA3Dbmogb0FAh8SkasUGE+NF8mZZpp8KAmn5YQxjspu BOKixB10mNw+Uy2NnVYUlVqHiU3Fe0I01sdCLMLOJifK15/WmGQaalZcvSH5QL+a97jmIOUnzrfqC S7j0zBtkqYUX4vM2MqRkDP4luViC2NX1exakMnTKvUfX89IXzojTElKCGpzMnU+IZNHkG8MXUjDSG V8wL9UWw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sm0AV-00000006Swo-35mm; Thu, 05 Sep 2024 00:16:11 +0000 Received: from mail-centralusazlp170110009.outbound.protection.outlook.com ([2a01:111:f403:c111::9] helo=DM5PR21CU001.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sm06r-00000006SFz-1G6c for linux-arm-kernel@lists.infradead.org; Thu, 05 Sep 2024 00:12:26 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=C5dj34qvGecfwJJAmbmEyOVi86yMOZ4yQZx4XMpiNQksGgYI+W9rH0cNkd8xhBbMkAaFcv85trNDYR7L2q0u94BxgZpowhbhHiIjRdgWPtimddh+9tEyZ0Dl+ODbGSr6wHmZoXj8bPUK6+g615w/xQ9kaxDC0RNo4jwlS9CARQ8NTMwuAVoOpN3UcnMKk4v+o5ak+z7OFml+7Zb7tAzO/dHf6vs2xl1Zi/6+vEIPz/qwQd/DY1sgNbND32xtsMRQIqKDIPJ9OwvN1/97rTO5XxlNwAbFIBVWbKPwegvSbgUaHuq8waeqnlkxM8iCoLBu5cLalwfZxwaSmdO3OSf/og== 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=IqLujuhuI1Zg8PLE9bHDjYF177N/UjqRsVME3kLgTJCnN64pYuNKKKdmXhvYVdAwb8QhDF9PAUnmBnivithjXIgD+yLyi9NhaCC1uQWLxKxWFPqNMIIeHd9u/jrm6oJ25w3LmkaEUKsg3r1WEs7X7K4o93oWlF67oef6wO2aXpWRB/it+RIRR36cHyM/EHZO00e8W5TWBDf3o9FcNsi/DI/24yyYaPBwr11YhuoojEVKysJgirYPSu8YPExzM5rhBvx51txuoRZoU8q9I6XjDFVphUk2EV6hEXE+b03LQ2xA9hvEroxljckEpbOu3Ky3a2xLpEfRFOjyI/S5Mb2IYg== 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=Qr2k9yInUMe5eQydNrNRvtNEqRTrr29+sfvek5xQ8mKV9P0CkHwZ8OvvGuTG5h1B4fgtEHWJncRGB7qge4yyGXLeAzoRjib98bWCKVkyo6PvUMJf6QEOqostj8EJI8NExkETw/B2+FEIBZt5M4MVfeuAQB2j01HkHlrODp+gJS0= 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 MW6PR01MB8365.prod.exchangelabs.com (2603:10b6:303:248::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.28; Thu, 5 Sep 2024 00:12:14 +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; Thu, 5 Sep 2024 00:12:14 +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 V6 1/4] Add dso__is_pie call to identify ELF PIE Date: Wed, 4 Sep 2024 18:11:32 -0600 Message-Id: <85d8de9558414879d19bd0c530f94a8002836166.1725493961.git.scclevenger@os.amperecomputing.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: X-ClientProxiedBy: CH2PR07CA0050.namprd07.prod.outlook.com (2603:10b6:610:5b::24) To PH7PR01MB8146.prod.exchangelabs.com (2603:10b6:510:2bd::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH7PR01MB8146:EE_|MW6PR01MB8365:EE_ X-MS-Office365-Filtering-Correlation-Id: b00457f8-59ea-44e8-6259-08dccd3f6518 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|366016|52116014|376014|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: jb6j8aI1kb6zgQSrQg//pmOK7lyXLI2Ah0udNcudKlLa/zfdC+TOckxJzO3CaqBo4H9Pkmlkk4vsRXxomM52zZIWhvsQafd9oSOlkq9/QT4iJiebhyWgwvCkyJXsPaurHuCvOPzoxLput6U/Mjw2TldmPyBhbToS9Iw7oHE/S8ypUBJ7KD7mYtM0AsrTKI74IrL+IA7QyiFnpsDPw51TTkeLP6pxxtQJGLJOMGDgp5w1AQhPiyMgp7+nbnsqJXGchiF4GftbyeaNUW53ea8KguadodZigXUYfqm1m6dd5CC8JTOjDFRsml571cq7qMpV2I4o3kWP7esH5EinaGdc9yiIfqbTb1cXDHJxL17JT+C3Y0iLGro1E3rRBXsn7aJm/RzXf4SfU72wBZhQ8KhEwXwgonY1YMrB5sg9i3Z3qEfvQJbHgcC9VK/946WhW+1Q2gwlx/JT5yvUyghV82SOOiKNntCfyJ30ZFCyb1cM2FIWNHy3vcjOdDv27nRAg8w+F9hPISYlP12Epq+ez8Z7XpPI0Vx2oiqaYkC7Xv5wDqgDOXZDjBj5cp9/+LQdwty2RFVJfI0yeMXaBZtjYiXvcVAHEjPs0H3TA0zyCjulF8e20HIJKBUzzpYlLwA+Q7FMlfMeF1PXsTSTie4X2rPMWp4mOIFQ4XoMRxXRuhUPVTuTgfqf4c8qX5VRDpJH2uy4N1U+wLrqybuFXFFrU7mC2sgcdFrkQo+RVgIhx6KpHv4vwA0bcRIdamrsbuDo3pEi7q8amsudEWmzQGaUw+PCj8J7BQ+HW0KKndXRmTr86Mf59YmPVBPZwY79cAlQlAnyCc8w+03Klh9dWhRq6xceEA9STsnEw6omJisAwapOkWrSXWPyxYb8OiGchLBXU2Q+AhxH/nZfTrDVdUsjeFYfh9G4ud8UtuDFC7J1ClIHgMZibxTp5avWzKW9Oh5Xaxbrz8In6YJzuvUjBL2SbWtbdrDH/RKrVPUMEe1ojDtAOuKsmG8aXcsa7nh5igy1VEOI21HlgtUCMkaRc6rRr4rm80lOWtreFE8m0yWB+Pe8NfZM9FvFqgnCKUkiz2XLw6e1KxmvZkT/5txf/XgP1kAK08QUBn/xPp/hrE4vmDwu3ppYQdY98BCZmzJyKKmSxc+Z6SovvvLLI+inupwSMDVx++8ihvcStswfCtWkQyt8d2sHemSKV/+umcWue6D51exUz24YF2qY129hGNif8uPaM35SeZfvt9Obmw+DG40g4P0b8hQMwc0Y3w3NxKsTv8QjNPYEGg5mpRzWUFpnBwwvosy5UFU+k69pxjTVhw49eauwCxnuYn9ae24mwFFdgcLpOajPYK58tsnnOoUSSydsugToHlUaUCPaP8R67EX1xut2L5Uv9Q0blJd5mL4J1KUZXVrmeUuMfh9AazqOmbE9UA== 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)(52116014)(376014)(1800799024)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 7+C+qApnfjGLLFFwZug8LFB+Ef17AjPMbc37N2wCgjswLkhoG//VqC5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T2cggcueOxJDZrn1k+IpvCWsosIpkgmIdUERLEYLzd3nCIlGn1Nzt/fjJm2RoBuXiAhkJ3LsLAoGFBmXgTHo2DunjNt80uIq6uKkKar9+TpIPOcm7MtpGbQQDgDFPB5sEPVgarYtJHC6rpGBQUMW2VBE1jIr8CeE7o0aVpxVKCn007wKZ/738fZ1OE2niBrifJsi1e7palLWh38ZSiXWgN04cX5iH73QeiKAXSiY7kjULB0dYHfjmCblM6Rq8T6vXCjLM2UL0EKtFWhN33y0McH15OKfksFtkfcuPuXZKFfjTvcf2uEPjppAmq3fs1EoQJ7vueVDtQ/k7JxTldQWoh0heY5f6i4GKSl2Ks3Dn5o6wF0uqkZL64EiE1cxa+mNrwvTGAMknN+kXKTQVejHU42d1sY2H7WJ7oPSCEF1f3cD0CsPH21oF5cHZGWSUloh8E/aWHt/wDIS2Vk2pfkYQFClb6p6dNgi8TQPtxRXidtMYla5oiyh6aSSn2In4pWBGqZxTWc7D3 X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: b00457f8-59ea-44e8-6259-08dccd3f6518 X-MS-Exchange-CrossTenant-AuthSource: PH7PR01MB8146.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Sep 2024 00:12:14.3440 (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: NCvd+ugdMMIRPFPXeCUPDcdYdXk4B219vQeXxtpKf1A2rFmmbHa38sm2/GF0PzK80a50D80GT4Kiz3quFL9a2Pg1ueJFTh0fCuO0CEpn5WUXJgvVjCIVE0bQcPTbqpHH X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR01MB8365 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240904_171225_381727_3EC9F55B X-CRM114-Status: GOOD ( 13.94 ) 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);