From patchwork Thu Jul 22 13:24:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 44454 X-Patchwork-Delegate: carlos@redhat.com 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 68358383942C for ; Thu, 22 Jul 2021 13:27:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 68358383942C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1626960422; bh=FocxAa+IrTSMqmKtkhPgUDo2WKO1iimDCNKcKNsewvY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=QUEfhxUeU8QAhvUIj4aM0vjvXjXniOQ3T/zwynqjUPi3YQN4RHGcv44RkWIKLOiKc tm13h9CXSQ131WlYtxa6EtKEGhexJHnyGS4EgBmc7P9C55IqGnc9D4D3iib421rnsA w5zhbPnUmYcA3UE4TCgzNyoQpXgKdFF8VYYay+Dc= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from camel.birch.relay.mailchannels.net (camel.birch.relay.mailchannels.net [23.83.209.29]) by sourceware.org (Postfix) with ESMTPS id 656AF3839417 for ; Thu, 22 Jul 2021 13:25:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 656AF3839417 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 C32C4122034; Thu, 22 Jul 2021 13:25:18 +0000 (UTC) Received: from pdx1-sub0-mail-a10.g.dreamhost.com (100-96-11-33.trex.outbound.svc.cluster.local [100.96.11.33]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id D67BD12294D; Thu, 22 Jul 2021 13:25:17 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a10.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.11.33 (trex/6.3.3); Thu, 22 Jul 2021 13:25:18 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Tangy-Soft: 633485b44632e208_1626960318597_3303629056 X-MC-Loop-Signature: 1626960318597:4056400479 X-MC-Ingress-Time: 1626960318597 Received: from pdx1-sub0-mail-a10.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a10.g.dreamhost.com (Postfix) with ESMTP id 83BA47EFD1; Thu, 22 Jul 2021 06:25:17 -0700 (PDT) Received: from rhbox.intra.reserved-bit.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-a10.g.dreamhost.com (Postfix) with ESMTPSA id 618B37EFC9; Thu, 22 Jul 2021 06:25:13 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a10 To: libc-alpha@sourceware.org Subject: [PATCH] mtrace: Fix output with PIE and ASLR [BZ #22716] Date: Thu, 22 Jul 2021 18:54:57 +0530 Message-Id: <20210722132457.1945231-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3494.1 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_H3, RCVD_IN_MSPIKE_WL, 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: 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 Reviewed-by: DJ Delorie --- malloc/mtrace-impl.c | 4 ++-- malloc/mtrace.pl | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/malloc/mtrace-impl.c b/malloc/mtrace-impl.c index 83008ca18f..3a65863d24 100644 --- a/malloc/mtrace-impl.c +++ b/malloc/mtrace-impl.c @@ -65,9 +65,9 @@ tr_where (const void *caller, Dl_info *info) offset); } - fprintf (mallstream, "@ %s%s%s[%p] ", info->dli_fname ? : "", + fprintf (mallstream, "@ %s%s%s[0x%" PRIxPTR "] ", info->dli_fname ? : "", info->dli_fname ? ":" : "", - buf, caller); + buf, caller - info->dli_fbase); } else fprintf (mallstream, "@ [%p] ", caller); diff --git a/malloc/mtrace.pl b/malloc/mtrace.pl index 6f49c8338d..f2570d2186 100644 --- a/malloc/mtrace.pl +++ b/malloc/mtrace.pl @@ -75,11 +75,12 @@ if ($#ARGV == 0) { } else { $prog = "./$binary"; } - if (open (LOCS, "env LD_TRACE_LOADED_OBJECTS=1 $prog |")) { + if (open (LOCS, "env LD_TRACE_PRELINKING=1 $prog |")) { while () { chop; - if (/^.*=> (.*) .(0x[0123456789abcdef]*).$/) { + if (/^.*=> (.*) \((0x[0123456789abcdef]*), (0x[0123456789abcdef]*).*/) { $locs{$1} = $2; + $rel{$1} = hex($2) - hex($3); } } close (LOCS); @@ -110,12 +111,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;