From patchwork Wed Aug 28 05:09:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Clevenger X-Patchwork-Id: 13780570 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 9D9DDC5472D for ; Wed, 28 Aug 2024 05:13:52 +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=uR9ij0s1UgAW3YWg38hQc+apIy 46nKy2a/AegvrEy0fBkI2E4wOZEAAXDmtZ5TemNKBVUVMDEs83EQOWHoCtqmIGrWqEbWzXyKOAskq MqDAOjwWV6GgsEUlo9ka2kps9uSLtdyQbUWpEtV7LIH4RFCaALHcaGEI2cQAxTG3tOQ66ESTMG6+e hov7050ULvtizV10pLeJSGa/2fl56C7P2HbI2DEmOG+Ekc5bm8XHtlxjGHXq2Nn9aejbKZgQb35S0 G7ZiNT2HMPMjo2BqhW3lLKIEbVQkoegRg8jCacfeY3O36XqjW5/mtv6eRmF0Ftct0jUpxZwpUIjQG naUaICbw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjB04-0000000Dsl4-0wav; Wed, 28 Aug 2024 05:13:44 +0000 Received: from mail-northcentralusazlp170100001.outbound.protection.outlook.com ([2a01:111:f403:c105::1] helo=CH1PR05CU001.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sjAwY-0000000DrsB-3CUH for linux-arm-kernel@lists.infradead.org; Wed, 28 Aug 2024 05:10:08 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=KnSpxULxQU04Lhdel1icBO3fnbwnkeDyJALl9kcYWaIq8d4ZXnUZo+ELN4tHlLyBQPa/+ZaGafbnajXLTCsJrwT3ht3FYC8SDSJsjunKc56V4IAV9wjvnjyXWKotf3TZJQ0/AJBFeePwXZYwP5eUMC5kBvTrSbNTh12XqewAKGPLMbs1vdBDduXepIRxm2NKOZ53Eivs0TqnOwHNNFS3c6reWg8u02QlhfV0v200l0bcu9LYqYBK9iVOhn0/79ZgH/9+59iBarjTfGATJ7tjkG4TQGFmjvuDJ3KqaTHstb6TZcoB6Z7FxXjCaBwb2qWs6XYScuWDXcmrNy6Jp2DWoQ== 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=v19gHaufx57UihNPjfk0U2zCVdXpU+YR+tgmJIwQBn0G2o1aT27mvCvwWVjg1k/ex/N0jXptidM5hv7l0dV0GtIg2vkhkqciIo6/v4cf52tVDpExdNBF5zcdDCjrLhFrOvAx1IQ2tO6t7YIUsClUXIlDUcELYvYV51fTiLL27gR/6SRwAOHUEQPwWzKd/yVnNWnBKX3wvw2Rr3orr2SRgbRDbV8IysQaLE1iVgOX2faDv7lilqFqbmIfIPrKqffP3HRnfF7grO69O1AbsbauFntfJBHhH6a2RmCNnQZaOmxpscg7McWWE0WN8i9qQaxesBQwWLfIkf4caOValROeng== 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=Fa8/ZLBRZbXBP1ljds9Ueq4ph+uSBWjvUEQrx8b7Pd7mPHP7GlcK6ImtJdYZwKGO8O6Jmk4gJKnb5GxosB6zaZK4Qoqp/qlIlPgxZ3Iggqrc0WwUR3w0KEKK7kvGIy9o/hQUS1OPL7TsYKfzD+1AmKT0uWNHGar+iMllNEuNb10= 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 SA0PR01MB6506.prod.exchangelabs.com (2603:10b6:806:eb::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7897.25; Wed, 28 Aug 2024 05:10:01 +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.027; Wed, 28 Aug 2024 05:10:01 +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 V4 1/4] Add dso__is_pie call to identify ELF PIE Date: Tue, 27 Aug 2024 23:09:39 -0600 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: X-ClientProxiedBy: CH5PR04CA0011.namprd04.prod.outlook.com (2603:10b6:610:1f4::27) To PH7PR01MB8146.prod.exchangelabs.com (2603:10b6:510:2bd::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH7PR01MB8146:EE_|SA0PR01MB6506:EE_ X-MS-Office365-Filtering-Correlation-Id: de0197e3-90ad-418a-50bc-08dcc71fab63 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|52116014|376014|366016|38350700014; X-Microsoft-Antispam-Message-Info: /Ywn61PDdEn9+TEnlWSwmvwKHCZFk7Yod81kWNQq4v5dJ0aNz/xTlugO/clKymUKNr8PYHVabQhA5H+06iSLiSgZlweiJI4VKAsnjJbs5JOcCQmjKjASVSf1JQTOyyHv1XaUWKJkgk7tj5aYQHY9dIDV3QT1cpdezw5DZ0tYx1MK7M5MHTGEXSBknilNNFQPDkFL01YlnqlMCl8EHANLKdM+zNYHW2G6tqIvGQF5GSP25q9WtZz3DtwynlBiptbcUOqZ+5D1B6rp4lZ5bMUgEItteWZFterVlYvzqX/IN/JS+xDRktnE15AsczVZxg6fOU01yWlTECDxPEpFPAvbUaz9/23yRRazJcbY8zkdvRAJoDHmKFKoXVmOfUN78GSf98AY1TbmwUjXhoBjUCdCIdq/5yi5RppJ1AwKAjxpOURYXk9SbkwuxfReSxVQ8o913C9zGfrK/f1jo0KVcXpNeCrCG23GaYujK1tgp8PXr0k1HwChHCgA3ydh0XXYBtx+xdDqRKX1X+iU4k47MtTeobk8hrHOjg+b4OdLeeuwmlRpv5Tx9UjIGeAOra3HrgxtX9Cujt819HIXIaJ1FXb9zgEtwSTHfP7JHiHblJAzFeN6FwyRPhdMuvFOjk42sgoIm9+I6cgG67UyT+hLzs3BgFiigFBvIxT54EVck+XdLp/r6rfH9qrNELVQ01M45mcrOghqYiqbiwrrmvcmCP44Nl1vpR3dL5uCP/vEI/GNWibTntTQOQCp1DaUS/AfTsQCCwdTIEYd5j4m+uQhYFZFvsierqL/Li5tap593O0CsO0KfIVfof8+OG/k6sxMfSx6m+K+hXiMYyrsJoWC715667qimYzHhnOfEjPMHkXzlmrHF9VgVnboejtjiIK5xDSZ4o5FDQlhziCyvhJL/8ebXL3QmS65vuigSvfEfykyeuS4AOE5OnpvLhuW3/jipkOrJ7xHeWcwIcabVQpwziRzyCGI+dXFr28FHNpkeM1GQxrcfbxg+y5t0phci/RnYaN7FFgjYc1WWEKxJ/LGFUYeLrJm6f//A71Jr5RLcBWNmad8DjosgyiIVC5RRwFi2v8qT+fWzXvxeYCGNRcU4I1eTM6PMyqVy95tmwYHuttQSmF/BlBkaOdLUKvz7D4tktaHnUlUa7p6+T7xnN13OOihPsHYOmuPInuwZgQYrux18jE7WOfhKOATdOaB9CYGYqVMzRPUttF7m6QANNv/p/m9XDj4vhwSAdqXuyDmjHvBBYqg8/G5LgSFmqduqgOdCvZGDO7YkSdBXnAOGSco04o84MytICNrlrIpYyqleFlSxznLL0U/GB2rBc94WalZFCb5W4ayatLgvsGsEjKksl/nINpF7R926ijskiQcZ8ykoCgeO6iinJSjDMF4QUL+qdVOwsvTBwPk9emeTtprSfZ0vQ== 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)(52116014)(376014)(366016)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: bDmVN00Kqk+JX7RK6z3VR+erYmQu6n27RaADqsNj51MAtjxycsws6XywB3ZKVvwN6ESded2ldkJStMcyVAZQB7asal52Z+SfTfDKEnI+l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ItMHBIT2mQelJbFHEr6fTOyqXzt10vR3DYQkfXDmDcrQIfeh9r9dPWdhe+BBt1CUe1yuL616x7WIFIeJFlvt+AByCISiyIAHpH5QKG2XhSPJdBTx0nZuczo12QgTVXD4ZiQy22BOP44PdMPq6DRjvt9WDblFPnGsKA2QiLxUcOujBI5jpKhS3TXmOAU/UIdj20EUhGZamXq8dpNQWz9+Kwt+OiKjXfekK9hM3U8ByItZrMhUg7YqdaqRgnG6gwJ9tvg+v3CtKhIKGQ7ZsSIfNGAcTL9BW4oYB10xvMUEkedOX9mIquukiorjaamgKonF15EKyzy1GXDdlxn4PcSlo01Bq3DCFf0TZet7o01pBjjw/fmEhI4TC42hfbc9SKSS4nsHQiki+7In7wFhJOBtcICPx0H6W2ZIpVl0BPIFQYhpLwl0EWEIEboSQ/2oiHu49NmwcwM93HQX8Pac9AKBSJzpH77jWad X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: de0197e3-90ad-418a-50bc-08dcc71fab63 X-MS-Exchange-CrossTenant-AuthSource: PH7PR01MB8146.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Aug 2024 05:10:01.4074 (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: z6SsIhp/2sg/izc5xVl8kmsxf6gSQpDskkBKo/vrR7XdJGERbyhXIVRTJqczGvE2NbmqIUCfIYDSDGZ9aFprdrSku9xk0mVg+pQ7tu3Et9GynDsgS3QuZAXhSeYQYSO7 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR01MB6506 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240827_221006_826506_063219C4 X-CRM114-Status: GOOD ( 14.32 ) 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 | 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);