From patchwork Thu Sep 5 22:28:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Steve Clevenger X-Patchwork-Id: 13793046 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 7DB42CE7AA1 for ; Thu, 5 Sep 2024 22:32:59 +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=T0M5xsB5GHgn14az/wECWXeRLB eIymypBLNXR9aDlTANwX31z2PV4p8MNMTcX2yf3QJdn2LOdWOypHb4dwyWqHWl9L+vrPgwOpY/0Xh SvBHhS6lHSYtCNu39DsdGrh83NEsorccrOaASxzf1cDLqBJO9I+0J/1NOOlQRwGMkGNiZtmAv/OyX aYOTYBkeX8kCPW0avcYB3K9+WBgNAexIgbqCpsgNZLIEu+zdLng0v18KeNB611IR5HoWUhLEkljeB WK/TB+MML7TC+0RnXFqTpl5W3XQa8wxWIP7etPcbV9BMuKRYR7XdMxQNjIsJx+/Qn4YEHR2jWhbbn 9b70iFnw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1smL21-0000000A1lr-1gnq; Thu, 05 Sep 2024 22:32:49 +0000 Received: from mail-eastusazlp170100000.outbound.protection.outlook.com ([2a01:111:f403:c101::] helo=BL2PR02CU003.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1smKyK-0000000A1JA-34R5 for linux-arm-kernel@lists.infradead.org; Thu, 05 Sep 2024 22:29:01 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=PNM666JZBWMXNxGu1g/mcthg/LTAIgxxPvINfgLFvo+XxFnKhw8ZqEPCIHHMYgmlhhacc063GTBlj31e7znnduyiry+TizfpoaoY+W6wNtL+ao/1XzHBQ//iYr5QhUXxxRNXxJbgcJoJDdlv66/w/TzrmoaatDqFPK7TpBGFfJ8bOKeCQa2RDOfLdalsQafz63QLLFQCghaKE5uTZofB8vwV0fGsVAruKjN5bYSVt7P4COz7WwKSzU2opFo08mho5t7apABY7SopawvV1rHZvnsPgrWVqraJoXiD3W0lZwx86LYVYiLltboh4hZkV0Bt0CAUVi8XcS2aUdxFfPxu1A== 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=q5WukhleqQvn+zrcS7RkUVLMT6gOWiaM+6ttM95p6g6lzGmVDZ4ZqeRe9bcie9xwxXYnNTRRWeDt/J1kjkQSZkpdxUbqUMZZKhqZlHu7whMpt0K6mR1IFIDdCXrqiouT35crT9I/84ipLcU0TxPRQaTPb6J7O55qhCuE/BEnTobYturWNhE+E6ZAUj1jTw266aL+PLUpJ+l6SVWt1tO7WhTJFKIaQ2xAawpo+R9noDf3s5v2PUyzNDxVSqakMuRu9uXGBeDg3S3QiVuCuP/RLUPFrNvZOyQGbc9HsTNN3QQfZpDNbrBvvAEJQeK9qw9gnh/9YwL91mm2U3a22AAdXA== 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=N8DCKt3tTI3HigsvUEOv9XApol3k9sIbgT3xQrrv+cRvVh+SZtXKu83LKrehrSgLYVB/KxXbFSWUcFcRcifgc37a2bfp+7k3wJ+3qy+bss0MaoBW44xJNnsiFXFsEmsKPW4DIHRcNNofa7GLVFDqxrtVSNqFNL8oga+oKe75y00= 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 PH0PR01MB6501.prod.exchangelabs.com (2603:10b6:510:15::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7918.27; Thu, 5 Sep 2024 22:28:47 +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 22:28:47 +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 V7 1/4] Add dso__is_pie call to identify ELF PIE Date: Thu, 5 Sep 2024 16:28:28 -0600 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: X-ClientProxiedBy: CH2PR18CA0056.namprd18.prod.outlook.com (2603:10b6:610:55::36) To PH7PR01MB8146.prod.exchangelabs.com (2603:10b6:510:2bd::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PH7PR01MB8146:EE_|PH0PR01MB6501:EE_ X-MS-Office365-Filtering-Correlation-Id: 4d944949-a4bf-4a3b-ea38-08dccdfa1c11 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|52116014|366016|1800799024|38350700014; X-Microsoft-Antispam-Message-Info: p8vkYlx7S1p4Ix4veCsmfHghqHXMACBloQSTFY4ro3e7PAAW+DEyPUif9SaWiprcxu2G6NF6cEI29NP2DyJV084bnsIArWSuoFvO0VMBqdCY3ijjKAWUFCJIikOAfDu8UzNI8tkzghjBRAjQeNvTVD+v7Kc1jGAejF0xHfwNGU3ocnFE/lZ/Qyc/cVs8dydZDmGZ57K5VQBm5dSHoQ89uw3s8+I4zp9z374zX0kH26LeA0Cesg9UbNKhH324Cs/7spFLUvTZukTplGOGAhvEpui5Expuvswk1l7jVHaYWe8IY0M/V8gnBat9Da3leVLWQncUBqPM7rBoRoDW91CMXXubtvTgKXVoFLcRmHnW4/mbdJoJpV4UIHhvONYK/H/ZfV7xeGmlLgFsXbAwCfvz95iTzEmy9gSgTOMJFVWFewSNDY/64fIw9Q8xfU/7IxouNXGToFcmpyKDGrs5l719h3gN0F6aSL3EHMK15ycGcQqs4l77y/rNLc1/42cADXwsacC76WLX5oGqCTLzFCN/jeFVMNsEaxGeDJICbnhEGZDWPVX3q0D1lzuTGlby6NZl8Y9hW1RDXO8ZUonwa5WmOh/gz7KKUEPCk5MWQLGUR/KA8wtJxUesFZM1jJ06Q97js/1F/uDByRolS+gBEuJ5igKHSq52/tJ37an/V6WdDpq7G9JhVKIE6Vwb8p0F0gPEt8DLRFSsWVEY6cnSDd6Dlt53GToaiFbjHR9M4NYTOQJq4A5QuyPcJM6RCZleDsk6Lc8489M7pOMOjn4wRbCrlW4IyHMfyh9oGc5N7h9sDUFNkqBQu1zavG+pia56VEbWJtibtmOIrbFlknx+drY/EstHNLZAROkJOInCenOKm8eMhliHxiM5zJNmWNTxhwX79GK+gxHocNLpZ9EYnz+dpJ3W5p/YntLJoOo2KmWH19MeBAsKQQNgKAMKSC2HckSP/6Tw/0k2vs3sw/iBq4tTuNICcSbjgl50zY76eBaK24BCDVW6UHsdUOOqMil5pFBDg51s26eGPtiuypNF0tiaKd1oFbkUULumOE9FCQO2QZE9lISKthvdme9F62CLYaVg0A6MBOVazeWNcoCDtOURMEg+yLjMscWTulTWu4k1TRmB1nQ2X7YssvZz5suBfE0P4FYEH302VxJ3Al7L9aYRzOSO060TNsf7KWLK4UWqzFZZijP3/Py7xEyd1cjOnZFiOeheR4CA2rAlIaMFOm9ozMrXwtyK40z/fj/j/B3u27YXK5xnA11/pZU7p4p3nBjB1nYlUZrsnCMaZpyO1mvJg++wktZ4GeX8wBGUJkIMYYiqEicLUFauscFzy6i5gqFoX7gpRCEgmzu5SVWrG3uJTeMslqX6HWafU2qfRvUL52BKAh0z1N0iX8ikSt3n5OqdivnvCNY/FpgrZaXvFlnCkw== 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)(52116014)(366016)(1800799024)(38350700014);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CZrEQEC/7/IK8KS0ujj8yPoJ96uYTZfaHDIdhWbDqm2ulXOPvUz6XM/3wR9ejPD9Wr+VJRcfwW9VLt63xq6JrSQmD7lnaAlZsCwTuiBt4ryTOTvRSn/pDJMSBya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OWjqrpJop1KZtvMkvjEygmWwCbXcfGTCzrbSVHz0+bMUB8fFPOczOYU5gtzX1Vql3esvEe7VSNOJrJ7oONhFmbsAuWh7RxhIcmg6AOv/sYcK2SiZgPD1UEqnRhDO129V2ZFaaUbh0aylWDvde0Po/+grUoBRigH2gqyZ4RPlfeI0j2fgUQLEKurGuDKOvynolZrkb8GHlxHm0QSjFNJihxcp7WksyOxIBhf1rQOUS4wAjZM7AnZuz8vU2lsLj7WXsK8VbQVFNtAFumhjJkIXJu03YcF3kyoo0xM+t5BlfdWaG6z7ApCA2LxhOEQCeV3GpyjoR99usvBuRMlovfFSmRIsPazfDTNNpjrOvC3E99aHO34UuhDIi6pdu3xklceWcCNrSRC1QQ8Yml9kZXmFIl1Ou5EMGjCP6zRIo9dtmWYcB0/MR4kSjxxV6JdglqVdQIfFphXt29PgWcmhOX6TC5WuoAu84 X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: 4d944949-a4bf-4a3b-ea38-08dccdfa1c11 X-MS-Exchange-CrossTenant-AuthSource: PH7PR01MB8146.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Sep 2024 22:28:47.7185 (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: XRcAPmZM6dBmzyb4Mr0uNT49D0aJbbWAb366pfotY364bsBwG93O1jAgOZKczEHrEalybHRQnyy9xjTmIlz9/A/N+i8xF6NXpp68IDKg1cIsiionBYkF7siu1knfY1Pn X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR01MB6501 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240905_152900_782010_B78BD086 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);