unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] mtrace: Fix output with PIE and ASLR [BZ #22716]
@ 2021-07-22 13:24 Siddhesh Poyarekar via Libc-alpha
  2021-07-22 14:12 ` John Ogness via Libc-alpha
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Siddhesh Poyarekar via Libc-alpha @ 2021-07-22 13:24 UTC (permalink / raw)
  To: libc-alpha; +Cc: John Ogness

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 <john.ogness@linutronix.de>
---
 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 (<LOCS>) {
 	    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 = <ADDR>;
 	    chomp $line;
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-08-12  4:36 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-22 13:24 [PATCH] mtrace: Fix output with PIE and ASLR [BZ #22716] Siddhesh Poyarekar via Libc-alpha
2021-07-22 14:12 ` John Ogness via Libc-alpha
2021-07-22 14:31   ` Siddhesh Poyarekar via Libc-alpha
2021-07-22 14:45     ` John Ogness via Libc-alpha
2021-07-22 15:17       ` Siddhesh Poyarekar via Libc-alpha
2021-07-22 15:19       ` Siddhesh Poyarekar via Libc-alpha
2021-08-11  7:41 ` [PING][PATCH] " Siddhesh Poyarekar via Libc-alpha
2021-08-12  4:15 ` [PATCH] " DJ Delorie via Libc-alpha
2021-08-12  4:33   ` Siddhesh Poyarekar via Libc-alpha
2021-08-12  4:35     ` DJ Delorie via Libc-alpha

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).