From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7814) id 02DE13847802; Fri, 3 Sep 2021 17:03:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 02DE13847802 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Fangrui Song To: glibc-cvs@sourceware.org Subject: [glibc/maskray/unnest] mtrace: Fix output with PIE and ASLR [BZ #22716] X-Act-Checkin: glibc X-Git-Author: Siddhesh Poyarekar X-Git-Refname: refs/heads/maskray/unnest X-Git-Oldrev: 78c9ec9000f873abe7a15a91b87080a2e4308260 X-Git-Newrev: f2e33c3268db9adf8e57e991676ed0d5ac74e8a8 Message-Id: <20210903170330.02DE13847802@sourceware.org> Date: Fri, 3 Sep 2021 17:03:29 +0000 (GMT) X-BeenThere: glibc-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Glibc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 Sep 2021 17:03:30 -0000 https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=f2e33c3268db9adf8e57e991676ed0d5ac74e8a8 commit f2e33c3268db9adf8e57e991676ed0d5ac74e8a8 Author: Siddhesh Poyarekar Date: Mon Aug 23 08:11:54 2021 +0530 mtrace: Fix output with PIE and ASLR [BZ #22716] 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. Fixes BZ #22716. Co-authored-by: John Ogness Reviewed-by: Andreas Schwab Reviewed-by: DJ Delorie Diff: --- 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 83008ca18f..f5f19c265c 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 ? : "", - 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;