From patchwork Thu Aug 12 09:07:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 44652 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 076163839833 for ; Thu, 12 Aug 2021 09:08:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 076163839833 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1628759314; bh=XsTCoDZP0SUHWtaZ+8XttL8zTykR27bjOfsKbUtx80Y=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=GjLVVDVxHGRH9COFrMVyVhNajA1+hojWCAmwDufAIZ9uBkc+Rmdjotc8YiXlV89bU kHiIaNpxPkGOy+eEjgvz6/w5ejlgT49BO3VYBRvMfKosFHxdCH4SMlACi7W5dId7P6 905teNM8TZC2+O5JBScOyqM7vAWGoc8Fb5qolMyU= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from dragonfly.birch.relay.mailchannels.net (dragonfly.birch.relay.mailchannels.net [23.83.209.51]) by sourceware.org (Postfix) with ESMTPS id 945BE38618AD for ; Thu, 12 Aug 2021 09:08:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 945BE38618AD X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 45103362825; Thu, 12 Aug 2021 09:08:00 +0000 (UTC) Received: from pdx1-sub0-mail-a43.g.dreamhost.com (100-96-99-6.trex.outbound.svc.cluster.local [100.96.99.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 0DB7736277B; Thu, 12 Aug 2021 09:07:59 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a43.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.99.6 (trex/6.3.3); Thu, 12 Aug 2021 09:08:00 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Suffer-Power: 67edd38f2e75a9c1_1628759279991_1697624450 X-MC-Loop-Signature: 1628759279991:2910666269 X-MC-Ingress-Time: 1628759279991 Received: from pdx1-sub0-mail-a43.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a43.g.dreamhost.com (Postfix) with ESMTP id CAC717E69D; Thu, 12 Aug 2021 02:07:58 -0700 (PDT) Received: from rhbox.redhat.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a43.g.dreamhost.com (Postfix) with ESMTPSA id 72D5B7E6A8; Thu, 12 Aug 2021 02:07:53 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a43 To: libc-alpha@sourceware.org Subject: [PATCH v3] mtrace: Fix output with PIE and ASLR [BZ #22716] Date: Thu, 12 Aug 2021 14:37:46 +0530 Message-Id: <20210812090746.612090-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3493.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Cc: schwab@linux-m68k.org, John Ogness Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" Record only the relative address of the caller in mtrace file. Use LD_TRACE_PRELINKING to get the executable as well as binary vs executable load offsets so that we may compute a base to add to the relative address in the mtrace file. This allows us to get a valid address to pass to addr2line in all cases. Co-authored-by: John Ogness --- Changes from v2: - Don't use "" Changes from v1: - Set LD_TRACE_PRELINK to an empty string so that it doesn't match any files and add a comment explaining it. - Formatting change in mtrace-impl.c malloc/mtrace-impl.c | 6 +++--- malloc/mtrace.pl | 15 +++++++-------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/malloc/mtrace-impl.c b/malloc/mtrace-impl.c index 9dffddfe2d..e7ace7f6fd 100644 --- a/malloc/mtrace-impl.c +++ b/malloc/mtrace-impl.c @@ -67,9 +67,9 @@ tr_where (const void *caller, Dl_info *info) offset); } - fprintf (mallstream, "@ %s%s%s[%p] ", info->dli_fname ? : "", - info->dli_fname ? ":" : "", - buf, caller); + fprintf (mallstream, "@ %s%s%s[0x%" PRIxPTR "] ", + info->dli_fname ? : "", info->dli_fname ? ":" : "", buf, + caller - info->dli_fbase); } else fprintf (mallstream, "@ [%p] ", caller); diff --git a/malloc/mtrace.pl b/malloc/mtrace.pl index 6f49c8338d..b1073a1931 100644 --- a/malloc/mtrace.pl +++ b/malloc/mtrace.pl @@ -75,11 +75,15 @@ if ($#ARGV == 0) { } else { $prog = "./$binary"; } - if (open (LOCS, "env LD_TRACE_LOADED_OBJECTS=1 $prog |")) { + # Set the environment variable LD_TRACE_PRELINKING to an empty string so + # that we trigger tracing but do not match with the executable or any of + # its dependencies. + if (open (LOCS, "env LD_TRACE_PRELINKING= $prog |")) { while () { chop; - if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) { + if (/^.*=> (.*) \((0x[0123456789abcdef]*), (0x[0123456789abcdef]*).*/) { $locs{$1} = $2; + $rel{$1} = hex($2) - hex($3); } } close (LOCS); @@ -110,12 +114,7 @@ sub location { my $addr = $2; my $searchaddr; return $cache{$addr} if (exists $cache{$addr}); - if ($locs{$prog} ne "") { - $searchaddr = sprintf "%#x", $addr - $locs{$prog}; - } else { - $searchaddr = $addr; - $prog = $binary; - } + $searchaddr = sprintf "%#x", hex($addr) + $rel{$prog}; if ($binary ne "" && open (ADDR, "addr2line -e $prog $searchaddr|")) { my $line = ; chomp $line;