From patchwork Tue Jan 31 13:16:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 50840 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2746935wrn; Tue, 31 Jan 2023 05:22:11 -0800 (PST) X-Google-Smtp-Source: AK7set+mQDvkHYLaVdODyk/sBqSwmCgD32LAnlu1uoK3K62j0AJs+wWUNzt4Gg8Tnzyo23OuzpxB X-Received: by 2002:aa7:9883:0:b0:58b:7db6:da48 with SMTP id r3-20020aa79883000000b0058b7db6da48mr10364891pfl.20.1675171331131; Tue, 31 Jan 2023 05:22:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675171331; cv=none; d=google.com; s=arc-20160816; b=k8+Gz0q0QKjMugjbD9YrDF9qwxrDvhqjHtMVjCkpqNvwGSdhHGEud9rQdZ3Kwj+Lkh p4n8KZHlniCygtaiFoLCi5V8ziNQC/tURcrY6zsLstQ1KS5pt0flRG3RInXZdJgdaxHu /ZC66LFvSr2qbTFFvpyhbgDOygAojmBX2bDEbKnVtfzhSWeUGd2NwCngOXHYrtW1e5Z2 jNX3uox0PAElvgUqC8kB1UUJnPtzTNapJo4hdh2DMCos23Wo6rIvyYrtRHMasMghEN31 w9098xvMOWfMydCSbK+Z1LvsgWOTCaFwUM/ICIA0br1ygo/jctml5Vye5h8Up44Aiq2E g0vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=nQkMJIXpGy3aSyOYWZEVilGjNv4ugy8vRZOboJULTnE=; b=M/QLr08gZQ9tgLkcFU0boyMMeLoWm/IP5SI8eQRqiQEQ97dMJTUKAgZ2ANXNz7bb3Q KWusXeDxaWc8GaprsgZnflh/9M0kzlaD1UCmRJuZ4p/UJ0AuVuCCAiEBFpg10nd/5SFq Lr5X2/qzeaE3a8Trm/BF8Tv2w6yw4e2koH4v5zebNtOPe4O/SCXDK9EC4ckpwXgEWM1t QooClFv3azsiF3C6vwp2uYPlgiHBgxSRYRY8xNrvp7qni90fZq2JaI/aTyehwKpSUmOg Bs/G4wrqXtf7bSYyhv4osZ8uHNJuDEK6QwTK0THdJhHDVv96RUfEVuFmYFmHUzfAfJ3k LtsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VLi80FMg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id cp14-20020a056a00348e00b00593ac9adb24si9398409pfb.6.2023.01.31.05.21.56; Tue, 31 Jan 2023 05:22:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=VLi80FMg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231444AbjAaNQo (ORCPT + 99 others); Tue, 31 Jan 2023 08:16:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230008AbjAaNQn (ORCPT ); Tue, 31 Jan 2023 08:16:43 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F6B110C9; Tue, 31 Jan 2023 05:16:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675171001; x=1706707001; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bDKkyRtHwSFPxVEw6hcGJuHrCfS3E8nNGIlQ35mRFSA=; b=VLi80FMgmSaNNzfdEZPaax8C2iOYBp715qh3DrEhO66yT40bLrzDWnMO JDTj/AN3HVIjRdn1YKQg5E8A//sM8RlmKKRDOTpFyIs30RBmUclsQVB80 h4VS8AF111BZ0qjhyamUk/U9n/29jYuvjSm1Vd3SyQIngti/GyvaJnbtG aCBMjphhVqyxrJKR12IwZmNZ208J9E0h4d0UtUyMYOUtWQy8Xl2x30rBm xfQpNOH11e+BTBGhldUEy2LSWXDPciLGt1b8ZO1slegLioecedSt6wgHy sLQjE54h2mn/yzAQgOZeVHynPmU7DxDax4B8rqFY2abKK9EqTR7WS+d+W g==; X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="325534141" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="325534141" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:40 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="657864791" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="657864791" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.47.218]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:38 -0800 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH V2 1/9] perf symbols: Correct plt entry sizes for x86 Date: Tue, 31 Jan 2023 15:16:17 +0200 Message-Id: <20230131131625.6964-2-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230131131625.6964-1-adrian.hunter@intel.com> References: <20230131131625.6964-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,URI_NOVOWEL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756544453959811131?= X-GMAIL-MSGID: =?utf-8?q?1756544453959811131?= In 32-bit executables the .plt entry size can be set to 4 when it is really 16. In fact the only sizes used for x86 (32 or 64 bit) are 8 or 16, so check for those and, if not, use the alignment to choose which it is. Example on Ubuntu 22.04 gcc 11.3: Before: $ cat tstpltlib.c void fn1(void) {} void fn2(void) {} void fn3(void) {} void fn4(void) {} $ cat tstplt.c void fn1(void); void fn2(void); void fn3(void); void fn4(void); int main() { fn4(); fn1(); fn2(); fn3(); return 0; } $ gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -m32 -Wall -Wextra -shared -o libtstpltlib32.so tstpltlib.c $ gcc -m32 -Wall -Wextra -o tstplt32 tstplt.c -L . -ltstpltlib32 -Wl,-rpath=$(pwd) $ perf record -e intel_pt//u --filter 'filter main @ ./tstplt32' ./tstplt32 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.011 MB perf.data ] $ readelf -SW tstplt32 | grep 'plt\|Name' [Nr] Name Type Addr Off Size ES Flg Lk Inf Al [10] .rel.plt REL 0000041c 00041c 000028 08 AI 5 22 4 [12] .plt PROGBITS 00001030 001030 000060 04 AX 0 0 16 <- ES is 0x04, should be 0x10 [13] .plt.got PROGBITS 00001090 001090 000008 08 AX 0 0 8 $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 17894.383903029: tr strt 0 [unknown] => 565b81cd main+0x0 17894.383903029: tr end call 565b81d4 main+0x7 => 565b80d0 __x86.get_pc_thunk.bx+0x0 17894.383903031: tr strt 0 [unknown] => 565b81d9 main+0xc 17894.383903031: tr end call 565b81df main+0x12 => 565b8070 [unknown] 17894.383903032: tr strt 0 [unknown] => 565b81e4 main+0x17 17894.383903032: tr end call 565b81e4 main+0x17 => 565b8050 [unknown] 17894.383903033: tr strt 0 [unknown] => 565b81e9 main+0x1c 17894.383903033: tr end call 565b81e9 main+0x1c => 565b8080 [unknown] 17894.383903033: tr strt 0 [unknown] => 565b81ee main+0x21 17894.383903033: tr end call 565b81ee main+0x21 => 565b8060 [unknown] 17894.383903237: tr strt 0 [unknown] => 565b81f3 main+0x26 17894.383903237: tr end return 565b81fc main+0x2f => f7c21519 [unknown] After: $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 17894.383903029: tr strt 0 [unknown] => 565b81cd main+0x0 17894.383903029: tr end call 565b81d4 main+0x7 => 565b80d0 __x86.get_pc_thunk.bx+0x0 17894.383903031: tr strt 0 [unknown] => 565b81d9 main+0xc 17894.383903031: tr end call 565b81df main+0x12 => 565b8070 fn4@plt+0x0 17894.383903032: tr strt 0 [unknown] => 565b81e4 main+0x17 17894.383903032: tr end call 565b81e4 main+0x17 => 565b8050 fn1@plt+0x0 17894.383903033: tr strt 0 [unknown] => 565b81e9 main+0x1c 17894.383903033: tr end call 565b81e9 main+0x1c => 565b8080 fn2@plt+0x0 17894.383903033: tr strt 0 [unknown] => 565b81ee main+0x21 17894.383903033: tr end call 565b81ee main+0x21 => 565b8060 fn3@plt+0x0 17894.383903237: tr strt 0 [unknown] => 565b81f3 main+0x26 17894.383903237: tr end return 565b81fc main+0x2f => f7c21519 [unknown] Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index aa62735aea7b..9328c162d68f 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -360,14 +360,23 @@ static bool get_plt_sizes(struct dso *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr_plt, *plt_header_size = 128; *plt_entry_size = 32; return true; + case EM_386: + case EM_X86_64: + *plt_entry_size = shdr_plt->sh_entsize; + /* Size is 8 or 16, if not, assume alignment indicates size */ + if (*plt_entry_size != 8 && *plt_entry_size != 16) + *plt_entry_size = shdr_plt->sh_addralign == 8 ? 8 : 16; + *plt_header_size = *plt_entry_size; + break; default: /* FIXME: s390/alpha/mips/parisc/poperpc/sh/xtensa need to be checked */ *plt_header_size = shdr_plt->sh_entsize; *plt_entry_size = shdr_plt->sh_entsize; - if (*plt_entry_size) - return true; - pr_debug("Missing PLT entry size for %s\n", dso->long_name); - return false; + break; } + if (*plt_entry_size) + return true; + pr_debug("Missing PLT entry size for %s\n", dso->long_name); + return false; } /* From patchwork Tue Jan 31 13:16:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 50841 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2746968wrn; Tue, 31 Jan 2023 05:22:16 -0800 (PST) X-Google-Smtp-Source: AK7set9E5LFOS8AXFY0+Uoikgny2oOe4vwcOGeK2mrQu1OJz4n3AG8QwOsYg6YPUmyQQWbJisfX8 X-Received: by 2002:a05:6a20:1b13:b0:a5:df86:f0e1 with SMTP id ch19-20020a056a201b1300b000a5df86f0e1mr12023549pzb.16.1675171335584; Tue, 31 Jan 2023 05:22:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675171335; cv=none; d=google.com; s=arc-20160816; b=Mv3XLCNWlU2cM3vhz+lf/nb6CfqLfphPc5Yl3wr7Dwq0QDRNHuw05nGRloxTrvdk8E FL5bKDdSjw7KaTwW7J0OhsdaX1LcVx//uyhd54zsoSuQ1pq/bt12OAksZJqNMw9G+/Ov Q6cpds4s0B6tyC+qPEBl3XtCH1p22VYAdm08BKqx6koPnGwLQyI74dbQJ0HkcvTee22Q rbZirG0OSzleYvuZAId2I4wwclyuIOg5BEEILZEfZvU4Z3TLQ74/dI0VXpQX5TvgPpCw jI0vD2j6beQZ13WCJn4EYzKiunIeB6tBbQWMWYh45nhP53wALy9NY7H9eyh7/8MCaoCe QVtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XE3ULfSNGUsZw9CelUDgtjRfaz23Yrw2JMnZT0ADUvY=; b=y75Juuxx5J91aEh6AlFlH0dfwqU0mu3l/3vnWZIfGOiD0hXQVHXS+co0tsYRTH1LZA usM70X9PaO4xuRdnQ+dIokaCNKT1sxYXk5voXcKg6yD7+cxtW8KxF4Nez+N+4aHi7/Yj K2ro/igw6T8+ruClUe8gYnZDkBR564h2gruo8n9gSwKQBibWafYEugIs3zDsObkPfhBT GdO1LsHY3H3OkxIOLChxTOXSv5Kl5DmBfvslFnG41aW2LO2FUyGJ87SeBLsOdC4LNw12 2gGoSAYNUnvpYfhEQNGGLaqBRNRQJCNRTpOL9di0m0lfiAXhppAz8eljizZpFjU9D3Y8 6n/g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JCd0m3Vx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a204-20020a621ad5000000b00592591f1972si15500410pfa.64.2023.01.31.05.22.00; Tue, 31 Jan 2023 05:22:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=JCd0m3Vx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231504AbjAaNQs (ORCPT + 99 others); Tue, 31 Jan 2023 08:16:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231407AbjAaNQo (ORCPT ); Tue, 31 Jan 2023 08:16:44 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EFFC2D4D; Tue, 31 Jan 2023 05:16:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675171003; x=1706707003; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=EaD2lkLbiSUPIvN7w5iSQEWoS+/2QEnz2PV2lI9JMV8=; b=JCd0m3Vx/ANxHc1y8ubVgrCHU6eBZBlWpeM6qSJgLZLm71f0fNN4tOE4 QA4zR714uglyLZtPMMrsE7giEb3rbyDpi7sU1hLp/RhloZond35FmroIM 5Y5kK3avtILDw2WvRw3d33frOc15qnQnKJqFF+wFTgKAXE0vUazOzzFq5 IjNirywNyYBd6QNKX7J+Vrqb3QEl9CmUCPK9GAw9IORwtEWEcVKS+M/oa a7RbE7VrCfDHzKjruZXCqkrqAlkb3I2YMdivUkBjvEVkN9TaFxI/Y7Xdj Q6iGlqBfm/MXFs2kcqBlxayLWQvRsq44rLbYbGYV+sFc5HNJ8EAGgggMd A==; X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="325534148" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="325534148" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:43 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="657864800" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="657864800" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.47.218]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:40 -0800 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH V2 2/9] perf symbols: Add support for x86 .plt.sec Date: Tue, 31 Jan 2023 15:16:18 +0200 Message-Id: <20230131131625.6964-3-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230131131625.6964-1-adrian.hunter@intel.com> References: <20230131131625.6964-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,URI_NOVOWEL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756544458050762448?= X-GMAIL-MSGID: =?utf-8?q?1756544458050762448?= The section .plt.sec was originally added for MPX and was first called .plt.bnd. While MPX has been deprecated, .plt.sec is now also used for IBT. On x86_64, IBT may be enabled by default, but can be switched off using gcc option -fcf-protection=none, or switched on by -z ibt or -z ibtplt. On 32-bit, option -z ibt or -z ibtplt will enable IBT. With .plt.sec, calls are made into .plt.sec instead of .plt, so it makes more sense to put the symbols there instead of .plt. A notable difference is that .plt.sec does not have a header entry. For x86, when synthesizing symbols for plt, use offset and entry size of .plt.sec instead of .plt when there is a .plt.sec section. Example on Ubuntu 22.04 gcc 11.3: Before: $ cat tstpltlib.c void fn1(void) {} void fn2(void) {} void fn3(void) {} void fn4(void) {} $ cat tstplt.c void fn1(void); void fn2(void); void fn3(void); void fn4(void); int main() { fn4(); fn1(); fn2(); fn3(); return 0; } $ gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -Wall -Wextra -shared -o libtstpltlib.so tstpltlib.c $ gcc -Wall -Wextra -z ibt -o tstplt tstplt.c -L . -ltstpltlib -Wl,-rpath=$(pwd) $ readelf -SW tstplt | grep 'plt\|Name' [Nr] Name Type Address Off Size ES Flg Lk Inf Al [11] .rela.plt RELA 0000000000000698 000698 000060 18 AI 6 24 8 [13] .plt PROGBITS 0000000000001020 001020 000050 10 AX 0 0 16 [14] .plt.got PROGBITS 0000000000001070 001070 000010 10 AX 0 0 16 [15] .plt.sec PROGBITS 0000000000001080 001080 000040 10 AX 0 0 16 $ perf record -e intel_pt//u --filter 'filter main @ ./tstplt' ./tstplt [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.015 MB perf.data ] $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 38970.522546686: tr strt 0 [unknown] => 55fc222a81a9 main+0x0 38970.522546686: tr end call 55fc222a81b1 main+0x8 => 55fc222a80a0 [unknown] 38970.522546687: tr strt 0 [unknown] => 55fc222a81b6 main+0xd 38970.522546687: tr end call 55fc222a81b6 main+0xd => 55fc222a8080 [unknown] 38970.522546688: tr strt 0 [unknown] => 55fc222a81bb main+0x12 38970.522546688: tr end call 55fc222a81bb main+0x12 => 55fc222a80b0 [unknown] 38970.522546688: tr strt 0 [unknown] => 55fc222a81c0 main+0x17 38970.522546688: tr end call 55fc222a81c0 main+0x17 => 55fc222a8090 [unknown] 38970.522546689: tr strt 0 [unknown] => 55fc222a81c5 main+0x1c 38970.522546894: tr end return 55fc222a81cb main+0x22 => 7f3a4dc29d90 __libc_start_call_main+0x80 After: $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 38970.522546686: tr strt 0 [unknown] => 55fc222a81a9 main+0x0 38970.522546686: tr end call 55fc222a81b1 main+0x8 => 55fc222a80a0 fn4@plt+0x0 38970.522546687: tr strt 0 [unknown] => 55fc222a81b6 main+0xd 38970.522546687: tr end call 55fc222a81b6 main+0xd => 55fc222a8080 fn1@plt+0x0 38970.522546688: tr strt 0 [unknown] => 55fc222a81bb main+0x12 38970.522546688: tr end call 55fc222a81bb main+0x12 => 55fc222a80b0 fn2@plt+0x0 38970.522546688: tr strt 0 [unknown] => 55fc222a81c0 main+0x17 38970.522546688: tr end call 55fc222a81c0 main+0x17 => 55fc222a8090 fn3@plt+0x0 38970.522546689: tr strt 0 [unknown] => 55fc222a81c5 main+0x1c 38970.522546894: tr end return 55fc222a81cb main+0x22 => 7f3a4dc29d90 __libc_start_call_main+0x80 Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 9328c162d68f..bb1b5cb3ff12 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -379,6 +379,11 @@ static bool get_plt_sizes(struct dso *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr_plt, return false; } +static bool machine_is_x86(GElf_Half e_machine) +{ + return e_machine == EM_386 || e_machine == EM_X86_64; +} + /* * 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 @@ -391,8 +396,8 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) uint32_t nr_rel_entries, idx; GElf_Sym sym; u64 plt_offset, plt_header_size, plt_entry_size; - GElf_Shdr shdr_plt; - struct symbol *f; + GElf_Shdr shdr_plt, plt_sec_shdr; + struct symbol *f, *plt_sym; GElf_Shdr shdr_rel_plt, shdr_dynsym; Elf_Data *syms, *symstrs; Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; @@ -422,10 +427,23 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) return 0; /* Add a symbol for .plt header */ - f = symbol__new(shdr_plt.sh_offset, plt_header_size, STB_GLOBAL, STT_FUNC, ".plt"); - if (!f) + plt_sym = symbol__new(shdr_plt.sh_offset, plt_header_size, STB_GLOBAL, STT_FUNC, ".plt"); + if (!plt_sym) goto out_elf_end; - symbols__insert(&dso->symbols, f); + symbols__insert(&dso->symbols, plt_sym); + + /* Only x86 has .plt.sec */ + if (machine_is_x86(ehdr.e_machine) && + elf_section_by_name(elf, &ehdr, &plt_sec_shdr, ".plt.sec", NULL)) { + if (!get_plt_sizes(dso, &ehdr, &plt_sec_shdr, &plt_header_size, &plt_entry_size)) + return 0; + /* Extend .plt symbol to entire .plt */ + plt_sym->end = plt_sym->start + shdr_plt.sh_size; + /* Use .plt.sec offset */ + plt_offset = plt_sec_shdr.sh_offset; + } else { + plt_offset = shdr_plt.sh_offset + plt_header_size; + } scn_dynsym = ss->dynsym; shdr_dynsym = ss->dynshdr; @@ -474,8 +492,6 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) goto out_elf_end; nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; - plt_offset = shdr_plt.sh_offset; - plt_offset += plt_header_size; ri.is_rela = shdr_rel_plt.sh_type == SHT_RELA; From patchwork Tue Jan 31 13:16:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 50842 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2747015wrn; Tue, 31 Jan 2023 05:22:24 -0800 (PST) X-Google-Smtp-Source: AK7set9jkDL0TS7XAWRjbFeWx+VNq4jGGuI3i9Qp/qQNpb5zcQcuPRE2GoqKGt8lxWhgqsDr2NkI X-Received: by 2002:a17:903:3094:b0:196:807a:daab with SMTP id u20-20020a170903309400b00196807adaabmr6630315plc.33.1675171343996; Tue, 31 Jan 2023 05:22:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675171343; cv=none; d=google.com; s=arc-20160816; b=Q7f2b02+S+uX7Vn+K9k+aS6tpOJARGKdOqON94RTc+h9VBLKPGvUbeDSnv9PiO9R7e t6lFq3n1lirecgYs7kLh+mPHHFGKao6ZnHDhnlsmey9PgR2KinWHhcXkn4xBnJY2kcHU rYJzMi4TeVKQZ5zhLvJQiH4FA4U5HWHsMPLrNNeowG4zWl9MSgL+w6Z8VXh6Ml+Cy7LS C+mx2kULcibb8FNr7lNgFUrhHWdYEdSL6dOIK0cYzYBW+ix/6OvblO77utBc26EgCGrf eESVqkc7re7BUve/9AxkomZKsVu4vi+UER1+rbDf6VpCGjfaQmuEqo3kDxiK56nU1Gx+ rMsA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=RmTcy0JslofGdcr3zX7snMJ2uokAhiTczHZLa2tHgTs=; b=Mi3VCDcUu41d9hl4GbrpOiPi2nMH+cRtudUbvmfywNmc2HpZKL78jHp16GmcnUn04b u4QaS/6zN5gtmJmzGtZfPNESjWywbowBrBEtuiLkx/2/+8h5gQngkWMQhYT57dsoiWu8 xGZ2d4Cpxx+5Tn/xq+xScT2Ft7fo3FqiIOu8HctDd/DErHwyeDZceUtblIuS9iUK9dXU a2O7rt0+XstcdOvt64NMG2w+XZyUAz6yJhBlZPTzkvdHzT0+6hvwEPG7L0fb4O+1ZA6b OP/lubrIfg8709Gie3Gy77xZ5DCBi1mduFe1WWQvkdqTJzo84+NT9JkUvGD72F5cUhhZ 1spw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HNE4221o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f21-20020a170902f39500b0019305638dfdsi14915950ple.77.2023.01.31.05.22.09; Tue, 31 Jan 2023 05:22:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=HNE4221o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231675AbjAaNQu (ORCPT + 99 others); Tue, 31 Jan 2023 08:16:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231527AbjAaNQr (ORCPT ); Tue, 31 Jan 2023 08:16:47 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D24691AF; Tue, 31 Jan 2023 05:16:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675171005; x=1706707005; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=MSYyxIYpIcGvKGW9ILNeoVixG2YK5UrzbYtuW8oYj7s=; b=HNE4221oBCciKgKpAxUxvevqE9wPdnVdKzaThSZwUduXSVMgBd+lTc6R va+ozpL/s58Q7hRP2dHhgGwBmZx1zGu8kt41Sb1vrdPP7qbkG429TU0jC v61LadzRlN+97FTTc/VE8NFHhcAJpnCdwRonoLHi3lFr59VIZ0ftBsTJb +KrXYKVyG6N8I1OM1nxX45Dy52B4APQEesSueyuw9ur8CWVEJnCIKVfXr P+tZivxTaKYZcuWte/EmDTMwkSWnItFzykeIktKVYfbm/Xoiwm7uDgtfZ j1l4kooHANTvm+WfWZ5AsS2d3Vhm0MWMMgZ6YDBvcM4eanrm77tLHGFe6 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="325534157" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="325534157" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:45 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="657864807" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="657864807" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.47.218]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:43 -0800 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH V2 3/9] perf symbols: Sort plt relocations for x86 Date: Tue, 31 Jan 2023 15:16:19 +0200 Message-Id: <20230131131625.6964-4-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230131131625.6964-1-adrian.hunter@intel.com> References: <20230131131625.6964-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,URI_NOVOWEL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756544467429239724?= X-GMAIL-MSGID: =?utf-8?q?1756544467429239724?= For x86, with the addition of IFUNCs, relocation information becomes disordered with respect to plt. Correct that by sorting the relocations by offset. Example: Before: $ cat tstpltlib.c void fn1(void) {} void fn2(void) {} void fn3(void) {} void fn4(void) {} $ cat tstpltifunc.c #include void thing1(void) { printf("thing1\n"); } void thing2(void) { printf("thing2\n"); } typedef void (*thing_fn_t)(void); thing_fn_t thing_ifunc(void) { int x; if (x & 1) return thing2; return thing1; } void thing(void) __attribute__ ((ifunc ("thing_ifunc"))); void fn1(void); void fn2(void); void fn3(void); void fn4(void); int main() { fn4(); fn1(); thing(); fn2(); fn3(); return 0; } $ gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -Wall -Wextra -shared -o libtstpltlib.so tstpltlib.c $ gcc -Wall -Wextra -Wno-uninitialized -o tstpltifunc tstpltifunc.c -L . -ltstpltlib -Wl,-rpath="$(pwd)" $ readelf -rW tstpltifunc | grep -A99 plt Relocation section '.rela.plt' at offset 0x738 contains 8 entries: Offset Info Type Symbol's Value Symbol's Name + Addend 0000000000003f98 0000000300000007 R_X86_64_JUMP_SLOT 0000000000000000 puts@GLIBC_2.2.5 + 0 0000000000003fa8 0000000400000007 R_X86_64_JUMP_SLOT 0000000000000000 __stack_chk_fail@GLIBC_2.4 + 0 0000000000003fb0 0000000500000007 R_X86_64_JUMP_SLOT 0000000000000000 fn1 + 0 0000000000003fb8 0000000600000007 R_X86_64_JUMP_SLOT 0000000000000000 fn3 + 0 0000000000003fc0 0000000800000007 R_X86_64_JUMP_SLOT 0000000000000000 fn4 + 0 0000000000003fc8 0000000900000007 R_X86_64_JUMP_SLOT 0000000000000000 fn2 + 0 0000000000003fd0 0000000b00000007 R_X86_64_JUMP_SLOT 0000000000000000 getrandom@GLIBC_2.25 + 0 0000000000003fa0 0000000000000025 R_X86_64_IRELATIVE 125d $ perf record -e intel_pt//u --filter 'filter main @ ./tstpltifunc' ./tstpltifunc thing2 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.029 MB perf.data ] $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 20417.302513948: tr strt 0 [unknown] => 5629a74892be main+0x0 20417.302513948: tr end call 5629a74892c6 main+0x8 => 5629a7489110 fn2@plt+0x0 20417.302513949: tr strt 0 [unknown] => 5629a74892cb main+0xd 20417.302513949: tr end call 5629a74892cb main+0xd => 5629a74890f0 fn3@plt+0x0 20417.302513950: tr strt 0 [unknown] => 5629a74892d0 main+0x12 20417.302513950: tr end call 5629a74892d0 main+0x12 => 5629a74890d0 __stack_chk_fail@plt+0x0 20417.302528114: tr strt 0 [unknown] => 5629a74892d5 main+0x17 20417.302528114: tr end call 5629a74892d5 main+0x17 => 5629a7489120 getrandom@plt+0x0 20417.302528115: tr strt 0 [unknown] => 5629a74892da main+0x1c 20417.302528115: tr end call 5629a74892da main+0x1c => 5629a7489100 fn4@plt+0x0 20417.302528115: tr strt 0 [unknown] => 5629a74892df main+0x21 20417.302528115: tr end return 5629a74892e5 main+0x27 => 7ff14da29d90 __libc_start_call_main+0x80 After: $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 20417.302513948: tr strt 0 [unknown] => 5629a74892be main+0x0 20417.302513948: tr end call 5629a74892c6 main+0x8 => 5629a7489110 fn4@plt+0x0 20417.302513949: tr strt 0 [unknown] => 5629a74892cb main+0xd 20417.302513949: tr end call 5629a74892cb main+0xd => 5629a74890f0 fn1@plt+0x0 20417.302513950: tr strt 0 [unknown] => 5629a74892d0 main+0x12 20417.302513950: tr end call 5629a74892d0 main+0x12 => 5629a74890d0 offset_0x10d0@plt+0x0 20417.302528114: tr strt 0 [unknown] => 5629a74892d5 main+0x17 20417.302528114: tr end call 5629a74892d5 main+0x17 => 5629a7489120 fn2@plt+0x0 20417.302528115: tr strt 0 [unknown] => 5629a74892da main+0x1c 20417.302528115: tr end call 5629a74892da main+0x1c => 5629a7489100 fn3@plt+0x0 20417.302528115: tr strt 0 [unknown] => 5629a74892df main+0x21 20417.302528115: tr end return 5629a74892e5 main+0x27 => 7ff14da29d90 __libc_start_call_main+0x80 Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 60 ++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index bb1b5cb3ff12..07cfcf8f40e3 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -324,6 +324,8 @@ static char *demangle_sym(struct dso *dso, int kmodule, const char *elf_name) } struct rel_info { + u32 nr_entries; + u32 *sorted; bool is_rela; Elf_Data *reldata; GElf_Rela rela; @@ -332,6 +334,7 @@ struct rel_info { static u32 get_rel_symidx(struct rel_info *ri, u32 idx) { + idx = ri->sorted ? ri->sorted[idx] : idx; if (ri->is_rela) { gelf_getrela(ri->reldata, idx, &ri->rela); return GELF_R_SYM(ri->rela.r_info); @@ -340,6 +343,49 @@ static u32 get_rel_symidx(struct rel_info *ri, u32 idx) return GELF_R_SYM(ri->rel.r_info); } +static u64 get_rel_offset(struct rel_info *ri, u32 x) +{ + if (ri->is_rela) { + GElf_Rela rela; + + gelf_getrela(ri->reldata, x, &rela); + return rela.r_offset; + } else { + GElf_Rel rel; + + gelf_getrel(ri->reldata, x, &rel); + return rel.r_offset; + } +} + +static int rel_cmp(const void *a, const void *b, void *r) +{ + struct rel_info *ri = r; + u64 a_offset = get_rel_offset(ri, *(const u32 *)a); + u64 b_offset = get_rel_offset(ri, *(const u32 *)b); + + return a_offset < b_offset ? -1 : (a_offset > b_offset ? 1 : 0); +} + +static int sort_rel(struct rel_info *ri) +{ + size_t sz = sizeof(ri->sorted[0]); + u32 i; + + ri->sorted = calloc(ri->nr_entries, sz); + if (!ri->sorted) + return -1; + for (i = 0; i < ri->nr_entries; i++) + ri->sorted[i] = i; + qsort_r(ri->sorted, ri->nr_entries, sz, rel_cmp, ri); + return 0; +} + +static void exit_rel(struct rel_info *ri) +{ + free(ri->sorted); +} + static bool get_plt_sizes(struct dso *dso, GElf_Ehdr *ehdr, GElf_Shdr *shdr_plt, u64 *plt_header_size, u64 *plt_entry_size) { @@ -393,7 +439,7 @@ static bool machine_is_x86(GElf_Half e_machine) */ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) { - uint32_t nr_rel_entries, idx; + uint32_t idx; GElf_Sym sym; u64 plt_offset, plt_header_size, plt_entry_size; GElf_Shdr shdr_plt, plt_sec_shdr; @@ -491,11 +537,18 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) if (symstrs->d_size == 0) goto out_elf_end; - nr_rel_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; + ri.nr_entries = shdr_rel_plt.sh_size / shdr_rel_plt.sh_entsize; ri.is_rela = shdr_rel_plt.sh_type == SHT_RELA; - for (idx = 0; idx < nr_rel_entries; idx++) { + /* + * x86 doesn't insert IFUNC relocations in .plt order, so sort to get + * back in order. + */ + if (machine_is_x86(ehdr.e_machine) && sort_rel(&ri)) + goto out_elf_end; + + for (idx = 0; idx < ri.nr_entries; idx++) { const char *elf_name = NULL; char *demangled = NULL; @@ -523,6 +576,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) err = 0; out_elf_end: + exit_rel(&ri); if (err == 0) return nr; pr_debug("%s: problems reading %s PLT info.\n", From patchwork Tue Jan 31 13:16:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 50845 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2747176wrn; Tue, 31 Jan 2023 05:22:43 -0800 (PST) X-Google-Smtp-Source: AMrXdXvOqhbGs4ckVozIk8c4iSjH3MCYgu4x/eIejJeV+9kMPbKdozQ70gKLzoRo5tRMxCFgn9uR X-Received: by 2002:a05:6a20:5495:b0:b8:381d:6491 with SMTP id i21-20020a056a20549500b000b8381d6491mr72803985pzk.31.1675171363229; Tue, 31 Jan 2023 05:22:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675171363; cv=none; d=google.com; s=arc-20160816; b=EiyavE9n4RjC0s3g+sEaRQ6Z7064sn7IEt52BpBTgar226COChIqxycsfUu3B2eMmC SmdziH3u5BcfyxImRjyn02U+Hmx/kSrH4wLecyArQKqX2FyaFd8rP6rqOAP2FAufcZAv 5VzqLotRKblHfL5QbJK+x9djVyrJZSyNtohV7Q+qd0mLEMriEHFeb5tXu/JeTDtoxu91 LibwgcLXxJQ4X7R7jap4IXBYkX7ZTrFZAZ67k2giscaiT8exBI6tBtD0q5+fKDFOEduU HaloW2DJ4YcVgF3kVxWCgjRtLB7/HWBKMOS1fhx623sWwkpsmXBaE58yZ1dgcJ2hJlaW uq9A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=CmMuhLiuqXUsvNkbvXl0BbzLw2KQ0XXEzTTXmyZVJOI=; b=bRkELJdyGSqKNEHXEnt+v5FOOaTGBL3mQ5j1NoC9kpEHRxU63ikw+E0zZneWZ24+za s7KGQJ9ykqMp/Qfub/Y4wJIW6hMHAQJ9XkKASPiaV/8NP8ToMjEkWRuA9pImz0w+Bz7v h++otoCLRt8a6n7SgU0ziaHfxObpZy7+/p5X8qJVv/NtLVcZkZnCKpEVCMYMMnWe/w6T 4ZG+/rCkFs81SC9Hcq4fKNTgoygM1b7o5cjwH8qTGtF9PEBykPr4L5a1L3L50UaYPnPU M2XIWxQKJ7ehD6yB7wd7yLVJ1d2fy4ZgEudW8z1OXop3QQDvC/sc/ny/tpkfktMQvQXe ob4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=h4K8YnoI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v15-20020a63bf0f000000b004b2317cd02fsi15996709pgf.582.2023.01.31.05.22.29; Tue, 31 Jan 2023 05:22:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=h4K8YnoI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231781AbjAaNQy (ORCPT + 99 others); Tue, 31 Jan 2023 08:16:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230136AbjAaNQt (ORCPT ); Tue, 31 Jan 2023 08:16:49 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 49A753401A; Tue, 31 Jan 2023 05:16:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675171008; x=1706707008; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=PgoaMa5uG1on8jbmDVSgzzslCJYjltgwG8UjBjeDNrE=; b=h4K8YnoI+cdSsw9JmRrEGtkP7mLHgsKsWBl9fogKRN67MI/+uv6fJTIX 7mkLtKsVCcWWf8a46vPRz7ipgO5xgCHBqHKEU0wXTz0QDK9w0yv4O1xgN HSNBjj8AhLr+PtHdUbrRNvrr8+FN9lO1HyNQQDLg0eph5U1CiAzIceVmg WeUPo0PNkGgd2vhImCEf8xYupvmLKp6aOQrbXbbwOLGPtDDSirAlU38cr r5BK2WTiyssxb2rh1MGO2pXm4zfF3nsC2AtnP8BgpLuKoiaY8+/ieYULt e2l9rCRHO9zDN/+S2zb3UbdsMJpLAj/4s9ZnUYEJYX2EcPw76PNS6prqR w==; X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="325534169" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="325534169" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:47 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="657864813" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="657864813" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.47.218]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:45 -0800 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH V2 4/9] perf symbols: Record whether a symbol is an alias for an IFUNC symbol Date: Tue, 31 Jan 2023 15:16:20 +0200 Message-Id: <20230131131625.6964-5-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230131131625.6964-1-adrian.hunter@intel.com> References: <20230131131625.6964-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756544487064182945?= X-GMAIL-MSGID: =?utf-8?q?1756544487064182945?= To assist with synthesizing plt symbols for IFUNCs, record whether a symbol is an alias of an IFUNC symbol. Signed-off-by: Adrian Hunter --- tools/perf/util/symbol.c | 4 ++++ tools/perf/util/symbol.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index a024f06f75d8..d05727fcb30d 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -201,10 +201,14 @@ void symbols__fixup_duplicate(struct rb_root_cached *symbols) continue; if (choose_best_symbol(curr, next) == SYMBOL_A) { + if (next->type == STT_GNU_IFUNC) + curr->ifunc_alias = true; rb_erase_cached(&next->rb_node, symbols); symbol__delete(next); goto again; } else { + if (curr->type == STT_GNU_IFUNC) + next->ifunc_alias = true; nd = rb_next(&curr->rb_node); rb_erase_cached(&curr->rb_node, symbols); symbol__delete(curr); diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 2fdeb22bd02f..7558735543c2 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -64,6 +64,8 @@ struct symbol { u8 inlined:1; /** Has symbol__annotate2 been performed. */ u8 annotate2:1; + /** Symbol is an alias of an STT_GNU_IFUNC */ + u8 ifunc_alias:1; /** Architecture specific. Unused except on PPC where it holds st_other. */ u8 arch_sym; /** The name of length namelen associated with the symbol. */ From patchwork Tue Jan 31 13:16:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 50846 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2747273wrn; Tue, 31 Jan 2023 05:22:58 -0800 (PST) X-Google-Smtp-Source: AK7set+9AOAzr1NlvF0+fJbxOiJ6R+7G/zT31tzc1KNjd9GNFx4V5P3NbMDUQu5KC4iS4yiJ4xA/ X-Received: by 2002:a17:90b:1a8a:b0:22b:b3df:d970 with SMTP id ng10-20020a17090b1a8a00b0022bb3dfd970mr14142501pjb.44.1675171377906; Tue, 31 Jan 2023 05:22:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675171377; cv=none; d=google.com; s=arc-20160816; b=jbiMoni3yW6YDr8eMFXwxf4Nn/QW6UL8HEDeC6eLmZFfdfRfLnh9mm07RrOQN8KhF6 pVXvjhUZcoSY5rR+MGug2dJCFAYkyqhLawGCf0C/m0qDrRunpDwasV83pS/VoxYsEClO sXbSHxiyF8E6GE848+hcH+93Pyf/7MKzigzayDltLlNdpbZb+2lLtavs6XsdKH53tGxr 6o6t7DAvJfzWote4bVN4U+IGj1ylRHKdtBxDoqo27vpOZae+eDxlWXLd9WoxaVWKOC2x 0lpJSBO3eZf9hv1ha7VqfwKjrpCxik1WZe9RaTq/Qlv6B0L1fbQN3QCtmykuWAtzBuuE KO5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=pz/omomXowJ8tf4rJlX8y8f9vDddjIVdMIVG+xC7weM=; b=YHp6doVR8tN7c1krhv58afOEw/UZEBSBgwE2O+grZfgOA4zNm5beJ339x/g9UBL2lI GcjB3nzEjuQU94ldMKJtNx0DdU8MOS1caqeD6sIsYia9LDEfV4SiIzvgQTHrT7DcVefo he12EZHmAQj5z/kju0zLJFw09qpi6bzOAfddvL35Q7XLfHPd94pSI8iL7zYObJ5rHdOV TXaNS1pwvhMNa3tbBIQ7MTQIeiXpqWajqEJQ7BBuzQbougbIn9nXEgPSbrBu2nx1VkyK 6nz+NHaaw72spTLs5JyuWWsIB+Ppl3Z+7S1SgR5rwQBy+tksjxaXcVda5CsmWZ3wTokF yjGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lwLEejvt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x4-20020a17090a9dc400b00229311676ecsi16143992pjv.49.2023.01.31.05.22.44; Tue, 31 Jan 2023 05:22:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=lwLEejvt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231848AbjAaNQ6 (ORCPT + 99 others); Tue, 31 Jan 2023 08:16:58 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53618 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231726AbjAaNQw (ORCPT ); Tue, 31 Jan 2023 08:16:52 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 14670442EE; Tue, 31 Jan 2023 05:16:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675171011; x=1706707011; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=buNnSEAE01t7rtwc1PFmnTIysYUAolAqo25T7Ol7MAM=; b=lwLEejvtrg4vLcqc2PrdZiEWiFZ3wyst1LRkGE3YeXCPUjXEGgxwr0Do YJiG1LtqZaf1eBkdZGMF/l7drdg2lFgQp8U+IZuyemFnNVF4bSUQMSXG0 gYabLir51Cls0y4WDiw9Vb5kmVa74y+hscS+eiYCbw4sGbdaV+PBXdkI0 88V3tc0mOostwgIs8esvlWlM5BD/IT8By8xAz4QRbgjdnolrri7+idIxW Rj3s28Ea04riNUpX7hYq+TGM1kL/QdT2k4KcGfZu4z6WxXjpb5YAg1GAz THSBQf/PzTR0u3w1FqX+f13z2dXhi9tNDx5/8EhwTQGyYQTxJUY9wHAna w==; X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="325534180" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="325534180" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:50 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="657864820" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="657864820" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.47.218]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:48 -0800 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH V2 5/9] perf symbols: Add support for IFUNC symbols for x86_64 Date: Tue, 31 Jan 2023 15:16:21 +0200 Message-Id: <20230131131625.6964-6-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230131131625.6964-1-adrian.hunter@intel.com> References: <20230131131625.6964-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,URI_NOVOWEL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756544502719784168?= X-GMAIL-MSGID: =?utf-8?q?1756544502719784168?= For x86_64, the GNU linker is putting IFUNC information in the relocation addend, so use it to try to find a symbol for plt entries that refer to IFUNCs. Example: Before: $ cat tstpltlib.c void fn1(void) {} void fn2(void) {} void fn3(void) {} void fn4(void) {} $ cat tstpltifunc.c #include void thing1(void) { printf("thing1\n"); } void thing2(void) { printf("thing2\n"); } typedef void (*thing_fn_t)(void); thing_fn_t thing_ifunc(void) { int x; if (x & 1) return thing2; return thing1; } void thing(void) __attribute__ ((ifunc ("thing_ifunc"))); void fn1(void); void fn2(void); void fn3(void); void fn4(void); int main() { fn4(); fn1(); thing(); fn2(); fn3(); return 0; } $ gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -Wall -Wextra -shared -o libtstpltlib.so tstpltlib.c $ gcc -Wall -Wextra -Wno-uninitialized -o tstpltifunc tstpltifunc.c -L . -ltstpltlib -Wl,-rpath="$(pwd)" $ readelf -rW tstpltifunc | grep -A99 plt Relocation section '.rela.plt' at offset 0x738 contains 8 entries: Offset Info Type Symbol's Value Symbol's Name + Addend 0000000000003f98 0000000300000007 R_X86_64_JUMP_SLOT 0000000000000000 puts@GLIBC_2.2.5 + 0 0000000000003fa8 0000000400000007 R_X86_64_JUMP_SLOT 0000000000000000 __stack_chk_fail@GLIBC_2.4 + 0 0000000000003fb0 0000000500000007 R_X86_64_JUMP_SLOT 0000000000000000 fn1 + 0 0000000000003fb8 0000000600000007 R_X86_64_JUMP_SLOT 0000000000000000 fn3 + 0 0000000000003fc0 0000000800000007 R_X86_64_JUMP_SLOT 0000000000000000 fn4 + 0 0000000000003fc8 0000000900000007 R_X86_64_JUMP_SLOT 0000000000000000 fn2 + 0 0000000000003fd0 0000000b00000007 R_X86_64_JUMP_SLOT 0000000000000000 getrandom@GLIBC_2.25 + 0 0000000000003fa0 0000000000000025 R_X86_64_IRELATIVE 125d $ perf record -e intel_pt//u --filter 'filter main @ ./tstpltifunc' ./tstpltifunc thing2 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.016 MB perf.data ] $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 21860.073683659: tr strt 0 [unknown] => 561e212c42be main+0x0 21860.073683659: tr end call 561e212c42c6 main+0x8 => 561e212c4110 fn4@plt+0x0 21860.073683661: tr strt 0 [unknown] => 561e212c42cb main+0xd 21860.073683661: tr end call 561e212c42cb main+0xd => 561e212c40f0 fn1@plt+0x0 21860.073683661: tr strt 0 [unknown] => 561e212c42d0 main+0x12 21860.073683661: tr end call 561e212c42d0 main+0x12 => 561e212c40d0 offset_0x10d0@plt+0x0 21860.073698451: tr strt 0 [unknown] => 561e212c42d5 main+0x17 21860.073698451: tr end call 561e212c42d5 main+0x17 => 561e212c4120 fn2@plt+0x0 21860.073698451: tr strt 0 [unknown] => 561e212c42da main+0x1c 21860.073698451: tr end call 561e212c42da main+0x1c => 561e212c4100 fn3@plt+0x0 21860.073698452: tr strt 0 [unknown] => 561e212c42df main+0x21 21860.073698452: tr end return 561e212c42e5 main+0x27 => 7fb51cc29d90 __libc_start_call_main+0x80 After: $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 21860.073683659: tr strt 0 [unknown] => 561e212c42be main+0x0 21860.073683659: tr end call 561e212c42c6 main+0x8 => 561e212c4110 fn4@plt+0x0 21860.073683661: tr strt 0 [unknown] => 561e212c42cb main+0xd 21860.073683661: tr end call 561e212c42cb main+0xd => 561e212c40f0 fn1@plt+0x0 21860.073683661: tr strt 0 [unknown] => 561e212c42d0 main+0x12 21860.073683661: tr end call 561e212c42d0 main+0x12 => 561e212c40d0 thing_ifunc@plt+0x0 21860.073698451: tr strt 0 [unknown] => 561e212c42d5 main+0x17 21860.073698451: tr end call 561e212c42d5 main+0x17 => 561e212c4120 fn2@plt+0x0 21860.073698451: tr strt 0 [unknown] => 561e212c42da main+0x1c 21860.073698451: tr end call 561e212c42da main+0x1c => 561e212c4100 fn3@plt+0x0 21860.073698452: tr strt 0 [unknown] => 561e212c42df main+0x21 21860.073698452: tr end return 561e212c42e5 main+0x27 => 7fb51cc29d90 __libc_start_call_main+0x80 Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 38 +++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 07cfcf8f40e3..a002fc0bea03 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -381,6 +381,42 @@ static int sort_rel(struct rel_info *ri) return 0; } +/* + * For x86_64, the GNU linker is putting IFUNC information in the relocation + * addend. + */ +static bool addend_may_be_ifunc(GElf_Ehdr *ehdr, struct rel_info *ri) +{ + return ehdr->e_machine == EM_X86_64 && ri->is_rela && + GELF_R_TYPE(ri->rela.r_info) == R_X86_64_IRELATIVE; +} + +static bool get_ifunc_name(Elf *elf, struct dso *dso, GElf_Ehdr *ehdr, + struct rel_info *ri, char *buf, size_t buf_sz) +{ + u64 addr = ri->rela.r_addend; + struct symbol *sym; + GElf_Phdr phdr; + + if (!addend_may_be_ifunc(ehdr, ri)) + return false; + + if (elf_read_program_header(elf, addr, &phdr)) + return false; + + addr -= phdr.p_vaddr - phdr.p_offset; + + sym = dso__find_symbol_nocache(dso, addr); + + /* Expecting the address to be an IFUNC or IFUNC alias */ + if (!sym || sym->start != addr || (sym->type != STT_GNU_IFUNC && !sym->ifunc_alias)) + return false; + + snprintf(buf, buf_sz, "%s@plt", sym->name); + + return true; +} + static void exit_rel(struct rel_info *ri) { free(ri->sorted); @@ -560,7 +596,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) elf_name = demangled; if (*elf_name) snprintf(sympltname, sizeof(sympltname), "%s@plt", elf_name); - else + else if (!get_ifunc_name(elf, dso, &ehdr, &ri, sympltname, sizeof(sympltname))) snprintf(sympltname, sizeof(sympltname), "offset_%#" PRIx64 "@plt", plt_offset); free(demangled); From patchwork Tue Jan 31 13:16:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 50847 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2747339wrn; Tue, 31 Jan 2023 05:23:05 -0800 (PST) X-Google-Smtp-Source: AK7set9nn+DFplgPFbI9t+eyCCZgepypOX8G66Y7dPK6SeS5m3ANISQu9Kf5BOH2YHeoXoqbNAWO X-Received: by 2002:a17:90b:3891:b0:22b:ecec:aba6 with SMTP id mu17-20020a17090b389100b0022bececaba6mr29091646pjb.26.1675171385540; Tue, 31 Jan 2023 05:23:05 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675171385; cv=none; d=google.com; s=arc-20160816; b=Z1+ittYcTg/poNQivQSEm1D1e1Xa7dHpXqfYx/AmBYXy+cPmghGVHtc4wKvEI7G2uW fK1xZF72nyHk10IIFNrcSgOjlPan51tW8NCJHkp4xuq70AyYUZDxTBD3iDVZ1/TYTp20 dB5xPz7gPNMVHUsyQnzRg4IeqJJNXreQR/PpxsBqPsnBGtAHMvZzxz0CyDicQgthp+46 hilGipRZdZzCVA9/o0oHCU3uy4U9LbQd0hrTG66369PJTvlr/8DmN4c1wzl+AGFCCX/P HqiiUh5xQNwXWjYkMXmvftnryfW7aPWZSpLtqBXCQFtk29DWiJIFqGCteKIoo65AGHFN Tmgg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=AsL7Nr8/yMHdcVD7i6dH05sudp8BcA9/SlgLgW/fdsw=; b=CUmkfWHSVDS544L/wVpMcHNvU3O8BzEI0AlleVM5h1dKuOu9KC+aMRKb8vV+WL9m6g GN41MQdKK9gjhm+evF/3WCGrolMjyOItwJY/HdPvtQq33eIr1PuGJttnDQvQwKTwzolF Wxa/1nln9jWefhzR3/o/OeZB04uusA326JjZhJaYHZjRA2si/A0YYweLuM+3I8lNCMO3 /uqnnIQBh6n82xStoSDU41bmExdsx+fn+aOb4F90q2zPMESTjVVpEW5MyUXxA+k+eTNj q91V7UlVFBhf61DA+eGr+FfLEEwxebVv4qrtU4dbFhPmvLCfcfRzUJ+kLOxj86z4M30X Ya4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IhcUoNCc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b23-20020a17090aa59700b0022bb87f2dfdsi15534489pjq.129.2023.01.31.05.22.52; Tue, 31 Jan 2023 05:23:05 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=IhcUoNCc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231846AbjAaNRG (ORCPT + 99 others); Tue, 31 Jan 2023 08:17:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231705AbjAaNQ4 (ORCPT ); Tue, 31 Jan 2023 08:16:56 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2DF3E367DF; Tue, 31 Jan 2023 05:16:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675171013; x=1706707013; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=xv79CpMJX0N54gB4CsU1GJfMgJBj74HpxOAnf609a00=; b=IhcUoNCcvxeTkKYkaetKU6zRIrEZ2kdUNru73cPRsr76mFGWraU7pHVW Sp4/mID7U3oMUcjCId2k8/nBNYoRLtikf9HZA3MNsSRf5HnZc3Ahl2Wsi sCnzmh/f9rjKrUQZNiw9nJyfzjZPwd2/qV0MQZ3Xd/l8/vODLmnulNehn chpLda/0YFBjT/T7hEy+Ai8mZTFh+bMEr3Xj3R4lmIel1PZeyWKZq4lKL Y2bdQsgN0Lg29VvwB0urDYZPOEvQ3x2WNlub0MKE+L7wOELw0Hz0zvuji WKWS9awXRJAHz9jqF5U4jpveeaLIutanZPXXzN7Yj3A3DtkMbaz48rghj A==; X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="325534189" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="325534189" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:52 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="657864838" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="657864838" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.47.218]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:50 -0800 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH V2 6/9] perf symbols: Allow for .plt without header Date: Tue, 31 Jan 2023 15:16:22 +0200 Message-Id: <20230131131625.6964-7-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230131131625.6964-1-adrian.hunter@intel.com> References: <20230131131625.6964-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756544511046771097?= X-GMAIL-MSGID: =?utf-8?q?1756544511046771097?= A static executable can have a .plt due to the presence of IFUNCs. In that case the .plt does not have a header. Check for whether there is a header by comparing the number of entries to the number of relocation entries. Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index a002fc0bea03..8f7802097c72 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -489,6 +489,7 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) Elf *elf; int nr = 0, err = -1; struct rel_info ri = { .is_rela = false }; + bool lazy_plt; elf = ss->elf; ehdr = ss->ehdr; @@ -523,8 +524,10 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) plt_sym->end = plt_sym->start + shdr_plt.sh_size; /* Use .plt.sec offset */ plt_offset = plt_sec_shdr.sh_offset; + lazy_plt = false; } else { - plt_offset = shdr_plt.sh_offset + plt_header_size; + plt_offset = shdr_plt.sh_offset; + lazy_plt = true; } scn_dynsym = ss->dynsym; @@ -577,6 +580,17 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) ri.is_rela = shdr_rel_plt.sh_type == SHT_RELA; + if (lazy_plt) { + /* + * Assume a .plt with the same number of entries as the number + * of relocation entries is not lazy and does not have a header. + */ + if (ri.nr_entries * plt_entry_size == shdr_plt.sh_size) + dso__delete_symbol(dso, plt_sym); + else + plt_offset += plt_header_size; + } + /* * x86 doesn't insert IFUNC relocations in .plt order, so sort to get * back in order. From patchwork Tue Jan 31 13:16:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 50848 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2747552wrn; Tue, 31 Jan 2023 05:23:35 -0800 (PST) X-Google-Smtp-Source: AMrXdXvAazZsvxjmv5k/8qZJ0GQqjKZ+Gvk7cjWC64Q3CmVECI07dmD+yzRvqIrazHjwHJm6gQqX X-Received: by 2002:aa7:91c1:0:b0:58a:fddd:9b1d with SMTP id z1-20020aa791c1000000b0058afddd9b1dmr47159552pfa.10.1675171415073; Tue, 31 Jan 2023 05:23:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675171415; cv=none; d=google.com; s=arc-20160816; b=hArFzspdmIN3xN1aJeUagIepJmg4WFfVj7VTaU93oN0VPcIoHC81uCjnz/Rhy67EqQ zu3QF2V2Y9ZoWmwc6nUkovlTCM1olJhrSDHDQt4VhgbKoGHEgr/re6E7wwr5xhvlhW6h Wh3lbrQ6qUHaZVZUZerQytmasZ512JwCCxzgLJ++YPEM86cDBjw72jGrN3Y2C4oqBnri /Z/BYqfmfEHcN1wxc4HfySf4iOmuKwLpl6NZRPrU4QXbzyaCSkauO+a728IwS522keBQ JuHWFEfR97D6gaZYXZnSQ/2lQVvKurQPPpWvjXPQNh00rd7RMOxLmkDRfkpsp1nu5Eyq HzGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=oiqIlGp84MvjHPO4MdTTj8vRJGnrg2QBpF9DGrRsHEc=; b=qAFicHaFHwKjhGhmtK7hX1xFDZYCIeibmD/LXz14grTqMMUg8cGs8BbD6GTpWjW23y 9NDP2CjuYu4x3Fm4oqpfrn26K+0zG05yqoMHpjVKgNjpN8Uy4CCdgaHJIEKqmX6S9zsa CIF1KLwN/SiCXaetjDyKlsZCbgp5JmfvdVoBp1I9M1nI1Aw2cKTF7OFJYV4eVSPfRX/9 KISDP78z+r5TEELBYoIpPTdhx7kuMyaXepkiB7R+/w6qDBmpASrTbJ8GoKUILCCJNPek SusgiceW4S3UaHfyajOt7XxwOJ+Is2oPESwqkcFRLg71A/XE8MKD3G9yFsX6nw8mXQ6p xJqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WNKtOIUC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s64-20020a625e43000000b0059396b9f9bcsi9652600pfb.27.2023.01.31.05.23.22; Tue, 31 Jan 2023 05:23:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=WNKtOIUC; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231897AbjAaNRP (ORCPT + 99 others); Tue, 31 Jan 2023 08:17:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231887AbjAaNQ7 (ORCPT ); Tue, 31 Jan 2023 08:16:59 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A66482CC60; Tue, 31 Jan 2023 05:16:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675171015; x=1706707015; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=R3NgJAtWb7jXyeV9uKKrkT8nXL2hEwhW5ncNONVpuSY=; b=WNKtOIUCtc2VlDs+m4l43nzpq9bWZ6A+VXf/D89m5UVnCzTBU7YyNw3e 0fBB0cOX8jLg/8jJ7u1JKvwQwvuy5qfnu24p9cIzNZsVLwqPe8ir+ruy8 GX8kJS5T2nutgjFiJYFZ+1ydBUU2itKQUrA10zU8qOJUChjQnW6d5VRl8 jp1BAlkXg3Upg3tGoF4QKDzwFYOl3i00znJU52BkqoHhV6UgsI42vC/W/ qk3887ZicYX/hrRcsI5f1gVBVlfU/R4GlcFzOYdD+9ZL8zize9q8860xg Kn6W83pifb5EP/FoDLmAsP8T+CuI/6u49KwewcSIX7UlgE1nRvoq8aRD5 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="325534199" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="325534199" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:55 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="657864849" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="657864849" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.47.218]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:52 -0800 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH V2 7/9] perf symbols: Allow for static executables with .plt Date: Tue, 31 Jan 2023 15:16:23 +0200 Message-Id: <20230131131625.6964-8-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230131131625.6964-1-adrian.hunter@intel.com> References: <20230131131625.6964-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756544542009531762?= X-GMAIL-MSGID: =?utf-8?q?1756544542009531762?= A statically linked executable can have a .plt due to IFUNCs, in which case .symtab is used not .dynsym. Check the section header link to see if that is the case, and then use symtab instead. Example: Before: $ cat tstifunc.c #include void thing1(void) { printf("thing1\n"); } void thing2(void) { printf("thing2\n"); } typedef void (*thing_fn_t)(void); thing_fn_t thing_ifunc(void) { int x; if (x & 1) return thing2; return thing1; } void thing(void) __attribute__ ((ifunc ("thing_ifunc"))); int main() { thing(); return 0; } $ gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -static -Wall -Wextra -Wno-uninitialized -o tstifuncstatic tstifunc.c $ readelf -SW tstifuncstatic | grep 'Name\|plt\|dyn' [Nr] Name Type Address Off Size ES Flg Lk Inf Al [ 4] .rela.plt RELA 00000000004002e8 0002e8 000258 18 AI 29 20 8 [ 6] .plt PROGBITS 0000000000401020 001020 000190 00 AX 0 0 16 [20] .got.plt PROGBITS 00000000004c5000 0c4000 0000e0 08 WA 0 0 8 $ perf record -e intel_pt//u --filter 'filter main @ ./tstifuncstatic' ./tstifuncstatic thing1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.008 MB perf.data ] $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 15786.690189535: tr strt 0 [unknown] => 4017cd main+0x0 15786.690189535: tr end call 4017d5 main+0x8 => 401170 [unknown] 15786.690197660: tr strt 0 [unknown] => 4017da main+0xd 15786.690197660: tr end return 4017e0 main+0x13 => 401c1a __libc_start_call_main+0x6a After: $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 15786.690189535: tr strt 0 [unknown] => 4017cd main+0x0 15786.690189535: tr end call 4017d5 main+0x8 => 401170 thing_ifunc@plt+0x0 15786.690197660: tr strt 0 [unknown] => 4017da main+0xd 15786.690197660: tr end return 4017e0 main+0x13 => 401c1a __libc_start_call_main+0x6a Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 30 ++++++++++++++++++++---------- tools/perf/util/symsrc.h | 1 + 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 8f7802097c72..9e265a726418 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -483,7 +483,6 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) GElf_Shdr shdr_rel_plt, shdr_dynsym; Elf_Data *syms, *symstrs; Elf_Scn *scn_plt_rel, *scn_symstrs, *scn_dynsym; - size_t dynsym_idx; GElf_Ehdr ehdr; char sympltname[1024]; Elf *elf; @@ -530,13 +529,6 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) lazy_plt = true; } - scn_dynsym = ss->dynsym; - shdr_dynsym = ss->dynshdr; - dynsym_idx = ss->dynsym_idx; - - if (scn_dynsym == NULL) - return 0; - scn_plt_rel = elf_section_by_name(elf, &ehdr, &shdr_rel_plt, ".rela.plt", NULL); if (scn_plt_rel == NULL) { @@ -550,8 +542,25 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) shdr_rel_plt.sh_type != SHT_REL) return 0; - if (shdr_rel_plt.sh_link != dynsym_idx) + if (!shdr_rel_plt.sh_link) + return 0; + + if (shdr_rel_plt.sh_link == ss->dynsym_idx) { + scn_dynsym = ss->dynsym; + shdr_dynsym = ss->dynshdr; + } else if (shdr_rel_plt.sh_link == ss->symtab_idx) { + /* + * A static executable can have a .plt due to IFUNCs, in which + * case .symtab is used not .dynsym. + */ + scn_dynsym = ss->symtab; + shdr_dynsym = ss->symshdr; + } else { goto out_elf_end; + } + + if (!scn_dynsym) + return 0; /* * Fetch the relocation section to find the idxes to the GOT @@ -1077,8 +1086,9 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64); + ss->symtab_idx = 0; ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab", - NULL); + &ss->symtab_idx); if (ss->symshdr.sh_type != SHT_SYMTAB) ss->symtab = NULL; diff --git a/tools/perf/util/symsrc.h b/tools/perf/util/symsrc.h index 2665b4bde751..edf82028c9e6 100644 --- a/tools/perf/util/symsrc.h +++ b/tools/perf/util/symsrc.h @@ -26,6 +26,7 @@ struct symsrc { GElf_Shdr opdshdr; Elf_Scn *symtab; + size_t symtab_idx; GElf_Shdr symshdr; Elf_Scn *dynsym; From patchwork Tue Jan 31 13:16:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 50849 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2747556wrn; Tue, 31 Jan 2023 05:23:36 -0800 (PST) X-Google-Smtp-Source: AK7set90DYS627Zr6TCJxIPFpeXITfKCGNfkRJAwGsYs+qejVpyYQFD4X7TJv0bpVr4OCUIVLOdN X-Received: by 2002:a17:902:e48d:b0:196:768a:a9ae with SMTP id i13-20020a170902e48d00b00196768aa9aemr8364251ple.47.1675171415773; Tue, 31 Jan 2023 05:23:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675171415; cv=none; d=google.com; s=arc-20160816; b=M02zjJJ14lqcHq8ZjmrSqhK9zE2No/s7KNIT0MfZzWE5+/0VfOwVYmlW5Ny6Ex0+C4 dMlLxLqxZWg8Xm0uBceA5N2L4N1oZ8j829Xhn57xxd7ial8BW2MRQoLosj8kR6zUpSz6 Y16LqzLK7LhnBuwFfUUVph045tHyqXrHOtsfhAQw5DxUWRqWrdU/4C02iLgrHKaN69BI ZTA5hBb9D7Xm7gQN2rWsLb0pXBRUVw6AX1Sg3dC34QKgzbq4YeUPWGCjQA0SKFj4kaO1 RakmNVOR+BCwU0ZxNWjGUq0lhVQCQ4BtOr1nYg+y11TZEG2VORlq0qItioA6RzlqxFER YykA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=fR0vNSISLs2z0PmTwF1jfwk32UyM0IMlV9ht7ldmjTE=; b=i/FD302sl7U17T0KPA3t1fHN7yh+kQ0XTE3ZUvVq+rKYzX8yc9fuTKhhpvcTgweMPW A9Ho/WnRd91cqK4BJM5oVYIdxyVAdcXtubXLIWjRXCvTf0nE7Nru00/hL8v4lKrvYmls raZO1I4qYlxP/B+aMy+C1Ugzvuv9y6BNCThppd7G4mAWIYxFJMFRxz++ahIj075M65VH hCIDBF7scD88hote8/FnIYrbwP4Tf1pTjDa83yxYk8HVjnNzQ520mUA4CKQqbA6hkix6 La55a4iIOZsTh8b1qVcw016TwLjdKORpQCIWIeQ6cP6jWwiMU338sal+3LLEueIEB7JC SB9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=l8q9ApxI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z6-20020a170903018600b0019680715ea0si8297929plg.392.2023.01.31.05.23.23; Tue, 31 Jan 2023 05:23:35 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=l8q9ApxI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231868AbjAaNRS (ORCPT + 99 others); Tue, 31 Jan 2023 08:17:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54040 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231992AbjAaNRC (ORCPT ); Tue, 31 Jan 2023 08:17:02 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C81D342DE5; Tue, 31 Jan 2023 05:16:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675171018; x=1706707018; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=gSCQ96tg4l0FNHIgMJQtf3tGIwORYZgZx/efj/0fnbo=; b=l8q9ApxIiu+CUwlfKvp5I1t3eh3tu5L6J+4L5WKYemboLFPanBxdbueI hVuSvd2ey0T7TkboWRLzXUBlcs0Jalsd/VxfvDppuMwlG45cbmlab4iVa uzDJ1VampeZ+XfYOpBtqx97mrXu/krkmDIgQVssmKUOZ35ffDtl5ks7/V ASRTKcIqVNelcEBCeF3iLgG450DlJP9332lhWJWIvf1X6xE5Z/1GJPnlW VusWHSce+cuEumI6StcgMFfK/b7xq+JIztjMw8kmNteFzI7aCgbssx9Qb 7ZuhLgjh5CZWxyxS9YmhqRuDIx4r8FUfRAB9yz1Mi5fWQejycVCXaR+ly g==; X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="325534214" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="325534214" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:58 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="657864857" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="657864857" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.47.218]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:55 -0800 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH V2 8/9] perf symbols: Start adding support for .plt.got for x86 Date: Tue, 31 Jan 2023 15:16:24 +0200 Message-Id: <20230131131625.6964-9-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230131131625.6964-1-adrian.hunter@intel.com> References: <20230131131625.6964-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,URI_NOVOWEL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756544542706857158?= X-GMAIL-MSGID: =?utf-8?q?1756544542706857158?= For x86, .plt.got is used, for example, when the address is taken of a dynamically linked function. Start adding support by synthesizing a symbol for each entry. A subsequent patch will attempt to get a better name for the symbol. Example: Before: $ cat tstpltlib.c void fn1(void) {} void fn2(void) {} void fn3(void) {} void fn4(void) {} $ cat tstpltgot.c void fn1(void); void fn2(void); void fn3(void); void fn4(void); void callfn(void (*fn)(void)) { fn(); } int main() { fn4(); fn1(); callfn(fn3); fn2(); fn3(); return 0; } $ gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ gcc -Wall -Wextra -shared -o libtstpltlib.so tstpltlib.c $ gcc -Wall -Wextra -o tstpltgot tstpltgot.c -L . -ltstpltlib -Wl,-rpath="$(pwd)" $ readelf -SW tstpltgot | grep 'Name\|plt\|dyn' [Nr] Name Type Address Off Size ES Flg Lk Inf Al [ 6] .dynsym DYNSYM 00000000000003d8 0003d8 0000f0 18 A 7 1 8 [ 7] .dynstr STRTAB 00000000000004c8 0004c8 0000c6 00 A 0 0 1 [10] .rela.dyn RELA 00000000000005d8 0005d8 0000d8 18 A 6 0 8 [11] .rela.plt RELA 00000000000006b0 0006b0 000048 18 AI 6 24 8 [13] .plt PROGBITS 0000000000001020 001020 000040 10 AX 0 0 16 [14] .plt.got PROGBITS 0000000000001060 001060 000020 10 AX 0 0 16 [15] .plt.sec PROGBITS 0000000000001080 001080 000030 10 AX 0 0 16 [23] .dynamic DYNAMIC 0000000000003d90 002d90 000210 10 WA 7 0 8 $ perf record -e intel_pt//u --filter 'filter main @ ./tstpltgot , filter callfn @ ./tstpltgot' ./tstpltgot [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.011 MB perf.data ] $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 28393.810326915: tr strt 0 [unknown] => 562350baa1b2 main+0x0 28393.810326915: tr end call 562350baa1ba main+0x8 => 562350baa090 fn4@plt+0x0 28393.810326917: tr strt 0 [unknown] => 562350baa1bf main+0xd 28393.810326917: tr end call 562350baa1bf main+0xd => 562350baa080 fn1@plt+0x0 28393.810326917: tr strt 0 [unknown] => 562350baa1c4 main+0x12 28393.810326917: call 562350baa1ce main+0x1c => 562350baa199 callfn+0x0 28393.810326917: tr end call 562350baa1ad callfn+0x14 => 7f607d36110f fn3+0x0 28393.810326922: tr strt 0 [unknown] => 562350baa1af callfn+0x16 28393.810326922: return 562350baa1b1 callfn+0x18 => 562350baa1d3 main+0x21 28393.810326922: tr end call 562350baa1d3 main+0x21 => 562350baa0a0 fn2@plt+0x0 28393.810326924: tr strt 0 [unknown] => 562350baa1d8 main+0x26 28393.810326924: tr end call 562350baa1d8 main+0x26 => 562350baa060 [unknown] <- call to fn3 via .plt.got 28393.810326925: tr strt 0 [unknown] => 562350baa1dd main+0x2b 28393.810326925: tr end return 562350baa1e3 main+0x31 => 7f607d029d90 __libc_start_call_main+0x80 After: $ perf script --itrace=be --ns -F+flags,-event,+addr,-period,-comm,-tid,-cpu,-dso 28393.810326915: tr strt 0 [unknown] => 562350baa1b2 main+0x0 28393.810326915: tr end call 562350baa1ba main+0x8 => 562350baa090 fn4@plt+0x0 28393.810326917: tr strt 0 [unknown] => 562350baa1bf main+0xd 28393.810326917: tr end call 562350baa1bf main+0xd => 562350baa080 fn1@plt+0x0 28393.810326917: tr strt 0 [unknown] => 562350baa1c4 main+0x12 28393.810326917: call 562350baa1ce main+0x1c => 562350baa199 callfn+0x0 28393.810326917: tr end call 562350baa1ad callfn+0x14 => 7f607d36110f fn3+0x0 28393.810326922: tr strt 0 [unknown] => 562350baa1af callfn+0x16 28393.810326922: return 562350baa1b1 callfn+0x18 => 562350baa1d3 main+0x21 28393.810326922: tr end call 562350baa1d3 main+0x21 => 562350baa0a0 fn2@plt+0x0 28393.810326924: tr strt 0 [unknown] => 562350baa1d8 main+0x26 28393.810326924: tr end call 562350baa1d8 main+0x26 => 562350baa060 offset_0x1060@plt+0x0 28393.810326925: tr strt 0 [unknown] => 562350baa1dd main+0x2b 28393.810326925: tr end return 562350baa1e3 main+0x31 => 7f607d029d90 __libc_start_call_main+0x80 Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 9e265a726418..254116d40e59 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -466,6 +466,30 @@ static bool machine_is_x86(GElf_Half e_machine) return e_machine == EM_386 || e_machine == EM_X86_64; } +static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf, + GElf_Ehdr *ehdr, + char *buf, size_t buf_sz) +{ + struct symbol *sym; + GElf_Shdr shdr; + Elf_Scn *scn; + size_t i; + + scn = elf_section_by_name(elf, ehdr, &shdr, ".plt.got", NULL); + if (!scn || !shdr.sh_entsize) + return 0; + + for (i = 0; i < shdr.sh_size; i += shdr.sh_entsize) { + snprintf(buf, buf_sz, "offset_%#zx@plt", shdr.sh_offset + i); + sym = symbol__new(shdr.sh_offset + i, shdr.sh_entsize, STB_GLOBAL, STT_FUNC, buf); + if (!sym) + return -1; + symbols__insert(&dso->symbols, sym); + } + + return 0; +} + /* * 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 @@ -514,6 +538,11 @@ int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss) goto out_elf_end; symbols__insert(&dso->symbols, plt_sym); + /* Only x86 has .plt.got */ + if (machine_is_x86(ehdr.e_machine) && + dso__synthesize_plt_got_symbols(dso, elf, &ehdr, sympltname, sizeof(sympltname))) + goto out_elf_end; + /* Only x86 has .plt.sec */ if (machine_is_x86(ehdr.e_machine) && elf_section_by_name(elf, &ehdr, &plt_sec_shdr, ".plt.sec", NULL)) { From patchwork Tue Jan 31 13:16:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adrian Hunter X-Patchwork-Id: 50852 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp2747885wrn; Tue, 31 Jan 2023 05:24:25 -0800 (PST) X-Google-Smtp-Source: AMrXdXuqmKqcmen//fIwsl8HIN+IypvrROvNNooKR5aa2doF8WIgaaS/aTlacOYVg8AQnXkGfIrL X-Received: by 2002:a17:902:ebc9:b0:194:8260:cc50 with SMTP id p9-20020a170902ebc900b001948260cc50mr54938966plg.43.1675171465316; Tue, 31 Jan 2023 05:24:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675171465; cv=none; d=google.com; s=arc-20160816; b=zc7n8LmAo8jjkuCYB0hqaHw77fROF0/eXcM9QeqMNqCH4BGP6tDdbfJ4T9WX7/5WD0 RelqfAOV05nWNg/dnITO/FIVvlnCBFDnsWoTMdN8QwxjAcr6ppoaVPf3FEjfq7K1UQdH 0Lq1FIsPlCyPWZlPKA1mgb3T3qBeAE5yEEmtSfLhujBG/FNRIedVSTT+bGX3u75Sia3x dZpJMceEXkMTkrtAvUSlS19qx0Y3/jTBlyfY/+secWbb2mKAl3UoIFPdDkBovOHWBHk1 1cL7e0bjvEKNHbw46IWi2ERKhkDF3muwJ0of5Bla0vuADDs5UrqIFDf45uVk+wP4bsw9 CxQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:organization :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=MJaQKgjvk8ebKIgWVuekBFxyLDrGTMWU+IXt+IHdoE8=; b=kltQEKVY52Blph9r7x9Yoo6lZzKnAV2aVbnGU5pgMu8euZJN6ontTZ4ovY2mFLuL/n 8yuavlIcJzUbhcU6RxSal/u4pewfJTArjNJkbZP2D/6aUuCa+lxvXzUGS6sNk4tQuyvJ exkKuHJBf0Nconu2UcSik9s31a02bzslAROWDWnIUg0mtKv0NhA6jLlFEyd/AaMtf9IQ wDH45h6CQrktD+c4OdaJThIXKsasno4ioBtyLbGW/yGd+gBmh7SqKJoO6ytVDi4/u8Tt 2nVdrhY5PdBoRgHkvEuCo0ybQcwZYrmAFmJ9Nry9KHzWxWR1oIw8rROqdTAwSnBdObqj 7peA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XxVxCTWL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y15-20020a17090322cf00b00188ad330242si19272052plg.131.2023.01.31.05.24.12; Tue, 31 Jan 2023 05:24:25 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XxVxCTWL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230366AbjAaNRW (ORCPT + 99 others); Tue, 31 Jan 2023 08:17:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231872AbjAaNRM (ORCPT ); Tue, 31 Jan 2023 08:17:12 -0500 Received: from mga11.intel.com (mga11.intel.com [192.55.52.93]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 281841AF; Tue, 31 Jan 2023 05:17:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1675171021; x=1706707021; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=kTyPY0p6KFVL1tyOf3fj9sx3LfQomRM5zqwDxyFvz7M=; b=XxVxCTWLp1JwsVJoOtBS1NIuJfj62DlWrGgZCR8EHk46d6OqhoIwZTuZ bvL10sZjar5yvhGswruTnqs8PgQDVrzrQ4454yqfpn6ZmSWV96mkDG61W ReS8qmOBek1AJqyp9fUbGrBHIFn6Fh2HnjcLbXg5WWSQfTRKYfVo/tESC wA7gAIbEZfCBb8sLJHlU+WINo2a3XQcBnDp8N8jGFbLBowmBAxLajHy1w B8rwmtbTK1GwNCzF8jbdim8EHflIVqyl8MzS9lG27tq6a9x788fLIz6Lb znqz8JtX7jIbJwCB0czKHW+tevb7nS4ZLxetyErm74MjJszlXqpVG8ftZ A==; X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="325534225" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="325534225" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:17:00 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10606"; a="657864862" X-IronPort-AV: E=Sophos;i="5.97,261,1669104000"; d="scan'208";a="657864862" Received: from ahunter6-mobl1.ger.corp.intel.com (HELO ahunter-VirtualBox.home\044ger.corp.intel.com) ([10.252.47.218]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 31 Jan 2023 05:16:58 -0800 From: Adrian Hunter To: Arnaldo Carvalho de Melo Cc: Jiri Olsa , Namhyung Kim , Ian Rogers , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Subject: [PATCH V2 9/9] perf symbols: Get symbols for .plt.got for x86-64 Date: Tue, 31 Jan 2023 15:16:25 +0200 Message-Id: <20230131131625.6964-10-adrian.hunter@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230131131625.6964-1-adrian.hunter@intel.com> References: <20230131131625.6964-1-adrian.hunter@intel.com> MIME-Version: 1.0 Organization: Intel Finland Oy, Registered Address: PL 281, 00181 Helsinki, Business Identity Code: 0357606 - 4, Domiciled in Helsinki X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756544594403566840?= X-GMAIL-MSGID: =?utf-8?q?1756544594403566840?= For x86_64, determine a symbol for .plt.got entries. That requires computing the target offset and finding that in .rela.dyn, which in turn means .rela.dyn needs to be sorted by offset. Example: In this example, the GNU C Library is using .plt.got for malloc and free. Before: $ gcc --version gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. $ perf record -e intel_pt//u uname Linux [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.027 MB perf.data ] $ perf script --itrace=be --ns -F-event,+addr,-period,-comm,-tid,-cpu > /tmp/cmp1.txt After: $ perf script --itrace=be --ns -F-event,+addr,-period,-comm,-tid,-cpu > /tmp/cmp2.txt $ diff /tmp/cmp1.txt /tmp/cmp2.txt | head -12 15509,15510c15509,15510 < 27046.755390907: 7f0b2943e3ab _nl_normalize_codeset+0x5b (/usr/lib/x86_64-linux-gnu/libc.so.6) => 7f0b29428380 offset_0x28380@plt+0x0 (/usr/lib/x86_64-linux-gnu/libc.so.6) < 27046.755390907: 7f0b29428384 offset_0x28380@plt+0x4 (/usr/lib/x86_64-linux-gnu/libc.so.6) => 7f0b294a5120 malloc+0x0 (/usr/lib/x86_64-linux-gnu/libc.so.6) --- > 27046.755390907: 7f0b2943e3ab _nl_normalize_codeset+0x5b (/usr/lib/x86_64-linux-gnu/libc.so.6) => 7f0b29428380 malloc@plt+0x0 (/usr/lib/x86_64-linux-gnu/libc.so.6) > 27046.755390907: 7f0b29428384 malloc@plt+0x4 (/usr/lib/x86_64-linux-gnu/libc.so.6) => 7f0b294a5120 malloc+0x0 (/usr/lib/x86_64-linux-gnu/libc.so.6) 15821,15822c15821,15822 < 27046.755394865: 7f0b2943850c _nl_load_locale_from_archive+0x5bc (/usr/lib/x86_64-linux-gnu/libc.so.6) => 7f0b29428370 offset_0x28370@plt+0x0 (/usr/lib/x86_64-linux-gnu/libc.so.6) < 27046.755394865: 7f0b29428374 offset_0x28370@plt+0x4 (/usr/lib/x86_64-linux-gnu/libc.so.6) => 7f0b294a5460 cfree@GLIBC_2.2.5+0x0 (/usr/lib/x86_64-linux-gnu/libc.so.6) --- > 27046.755394865: 7f0b2943850c _nl_load_locale_from_archive+0x5bc (/usr/lib/x86_64-linux-gnu/libc.so.6) => 7f0b29428370 free@plt+0x0 (/usr/lib/x86_64-linux-gnu/libc.so.6) > 27046.755394865: 7f0b29428374 free@plt+0x4 (/usr/lib/x86_64-linux-gnu/libc.so.6) => 7f0b294a5460 cfree@GLIBC_2.2.5+0x0 (/usr/lib/x86_64-linux-gnu/libc.so.6) Signed-off-by: Adrian Hunter --- tools/perf/util/symbol-elf.c | 161 ++++++++++++++++++++++++++++++++++- 1 file changed, 157 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 254116d40e59..b149cfea73d0 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -466,28 +466,181 @@ static bool machine_is_x86(GElf_Half e_machine) return e_machine == EM_386 || e_machine == EM_X86_64; } +struct rela_dyn { + GElf_Addr offset; + u32 sym_idx; +}; + +struct rela_dyn_info { + struct dso *dso; + Elf_Data *plt_got_data; + u32 nr_entries; + struct rela_dyn *sorted; + Elf_Data *dynsym_data; + Elf_Data *dynstr_data; + Elf_Data *rela_dyn_data; +}; + +static void exit_rela_dyn(struct rela_dyn_info *di) +{ + free(di->sorted); +} + +static int cmp_offset(const void *a, const void *b) +{ + const struct rela_dyn *va = a; + const struct rela_dyn *vb = b; + + return va->offset < vb->offset ? -1 : (va->offset > vb->offset ? 1 : 0); +} + +static int sort_rela_dyn(struct rela_dyn_info *di) +{ + u32 i, n; + + di->sorted = calloc(di->nr_entries, sizeof(di->sorted[0])); + if (!di->sorted) + return -1; + + /* Get data for sorting: the offset and symbol index */ + for (i = 0, n = 0; i < di->nr_entries; i++) { + GElf_Rela rela; + u32 sym_idx; + + gelf_getrela(di->rela_dyn_data, i, &rela); + sym_idx = GELF_R_SYM(rela.r_info); + if (sym_idx) { + di->sorted[n].sym_idx = sym_idx; + di->sorted[n].offset = rela.r_offset; + n += 1; + } + } + + /* Sort by offset */ + di->nr_entries = n; + qsort(di->sorted, n, sizeof(di->sorted[0]), cmp_offset); + + return 0; +} + +static void get_rela_dyn_info(Elf *elf, GElf_Ehdr *ehdr, struct rela_dyn_info *di, Elf_Scn *scn) +{ + GElf_Shdr rela_dyn_shdr; + GElf_Shdr shdr; + + di->plt_got_data = elf_getdata(scn, NULL); + + scn = elf_section_by_name(elf, ehdr, &rela_dyn_shdr, ".rela.dyn", NULL); + if (!scn || !rela_dyn_shdr.sh_link || !rela_dyn_shdr.sh_entsize) + return; + + di->nr_entries = rela_dyn_shdr.sh_size / rela_dyn_shdr.sh_entsize; + di->rela_dyn_data = elf_getdata(scn, NULL); + + scn = elf_getscn(elf, rela_dyn_shdr.sh_link); + if (!scn || !gelf_getshdr(scn, &shdr) || !shdr.sh_link) + return; + + di->dynsym_data = elf_getdata(scn, NULL); + di->dynstr_data = elf_getdata(elf_getscn(elf, shdr.sh_link), NULL); + + if (!di->plt_got_data || !di->dynstr_data || !di->dynsym_data || !di->rela_dyn_data) + return; + + /* Sort into offset order */ + sort_rela_dyn(di); +} + +/* Get instruction displacement from a plt entry for x86_64 */ +static u32 get_x86_64_plt_disp(const u8 *p) +{ + u8 endbr64[] = {0xf3, 0x0f, 0x1e, 0xfa}; + int n = 0; + + /* Skip endbr64 */ + if (!memcmp(p, endbr64, sizeof(endbr64))) + n += sizeof(endbr64); + /* Skip bnd prefix */ + if (p[n] == 0xf2) + n += 1; + /* jmp with 4-byte displacement */ + if (p[n] == 0xff && p[n + 1] == 0x25) { + n += 2; + /* Also add offset from start of entry to end of instruction */ + return n + 4 + le32toh(*(const u32 *)(p + n)); + } + return 0; +} + +static bool get_plt_got_name(GElf_Shdr *shdr, size_t i, + struct rela_dyn_info *di, + char *buf, size_t buf_sz) +{ + struct rela_dyn vi, *vr; + const char *sym_name; + char *demangled; + GElf_Sym sym; + u32 disp; + + if (!di->sorted) + return false; + + disp = get_x86_64_plt_disp(di->plt_got_data->d_buf + i); + if (!disp) + return false; + + /* Compute target offset of the .plt.got entry */ + vi.offset = shdr->sh_offset + di->plt_got_data->d_off + i + disp; + + /* Find that offset in .rela.dyn (sorted by offset) */ + vr = bsearch(&vi, di->sorted, di->nr_entries, sizeof(di->sorted[0]), cmp_offset); + if (!vr) + return false; + + /* Get the associated symbol */ + gelf_getsym(di->dynsym_data, vr->sym_idx, &sym); + sym_name = elf_sym__name(&sym, di->dynstr_data); + demangled = demangle_sym(di->dso, 0, sym_name); + if (demangled != NULL) + sym_name = demangled; + + snprintf(buf, buf_sz, "%s@plt", sym_name); + + free(demangled); + + return *sym_name; +} + static int dso__synthesize_plt_got_symbols(struct dso *dso, Elf *elf, GElf_Ehdr *ehdr, char *buf, size_t buf_sz) { + struct rela_dyn_info di = { .dso = dso }; struct symbol *sym; GElf_Shdr shdr; Elf_Scn *scn; + int err = -1; size_t i; scn = elf_section_by_name(elf, ehdr, &shdr, ".plt.got", NULL); if (!scn || !shdr.sh_entsize) return 0; + if (ehdr->e_machine == EM_X86_64) + get_rela_dyn_info(elf, ehdr, &di, scn); + for (i = 0; i < shdr.sh_size; i += shdr.sh_entsize) { - snprintf(buf, buf_sz, "offset_%#zx@plt", shdr.sh_offset + i); + if (!get_plt_got_name(&shdr, i, &di, buf, buf_sz)) + snprintf(buf, buf_sz, "offset_%#zx@plt", shdr.sh_offset + i); sym = symbol__new(shdr.sh_offset + i, shdr.sh_entsize, STB_GLOBAL, STT_FUNC, buf); if (!sym) - return -1; + goto out; symbols__insert(&dso->symbols, sym); } - - return 0; + err = 0; +out: + exit_rela_dyn(&di); + return err; } /*