[PATCH] mtrace: Fix output with PIE and ASLR [BZ #22716]

John Ogness john.ogness@linutronix.de
Thu Jul 22 14:12:35 GMT 2021


Hi Siddhesh,

Thanks for pushing this much needed fix. Some comments from me below.

On 2021-07-22, Siddhesh Poyarekar <siddhesh@sourceware.org> wrote:
> 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.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]*).*/) {

AFAIK you are only interested in @l_map_start of the link map. That is
the first argument.

>  		$locs{$1} = $2;
> +		$rel{$1} = hex($2) - hex($3);

Subtracting @l_addr from @l_map_start will probably always result in
0. Why should @l_addr be interesting for mtrace?

I recommend:

+	    if (/^.*=> (.*) .(0x[0123456789abcdef]*),.*/) {
 		$locs{$1} = $2;
+		$rel{$1} = hex($2);

>  	    }
>  	}
>  	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};

And then $rel would need to be subtracted, not added:

+	$searchaddr = sprintf "%#x", hex($addr) - $rel{$prog};

John Ogness


More information about the Libc-alpha mailing list