blob: 594646324d3c6189692178df65d5f4a31239d438 [file] [log] [blame]
#! @PERL@
# This script handles linking the tool executables on Linux,
# statically and at an alternative load address.
#
# Linking statically sidesteps all sorts of complications to do with
# having two copies of the dynamic linker (valgrind's and the
# client's) coexisting in the same process. The alternative load
# address is needed because Valgrind itself will load the client at
# whatever address it specifies, which is almost invariably the
# default load address. Hence we can't allow Valgrind itself (viz,
# the tool executable) to be loaded at that address.
#
# Unfortunately there's no standard way to do 'static link at
# alternative address', so these link_tool_exe_*.in scripts handle
# the per-platform hoop-jumping.
#
# What we get passed here is:
# first arg
# the alternative load address
# all the rest of the args
# the gcc invokation to do the final link, that
# the build system would have done, left to itself
#
# We just let the script 'die' if something is wrong, rather than do
# proper error reporting. We don't expect the users to run this
# directly. It is only run as part of the build process, with
# carefully constrained inputs.
#
# Linux specific complications:
#
# - need to support both old GNU ld and gold: use -Ttext= to
# set the text segment address if that is all we have. We really
# need -Ttext-segment. Otherwise with GNU ld sections or notes
# (like the build-id) don't get at the desired address. But older
# linkers only know about -Ttext, not -Ttext-segment. So configure
# checks for us and sets FLAG_T_TEXT.
#
# - If all we have is -Ttext, then we need to pass --build-id=none
# (that is, -Wl,--build-id=none to gcc) if it accepts it, to ensure
# the linker doesn't add a notes section which ends up at the default
# load address and so defeats our attempts to keep that address clear
# for the client. However, older linkers don't support this flag,
# so it is tested for by configure.in and is shipped to us as part of
# argv[2 ..].
#
# So: what we actually do:
#
# pass the specified command to the linker as-is, except, add
# "-static" and "-Ttext[-segment]=<argv[1]>" to it.
# Previously we did this by adding these options after the first
# word of the rest of the arguments, which works in the common case
# when it's something like "gcc". But the linker invocation itself
# might be multiple words, say if it's "ccache gcc". So we now put
# the new options at the end instead.
#
use warnings;
use strict;
# expect at least: alt-load-address gcc -o foo bar.o
die "Not enough arguments"
if (($#ARGV + 1) < 5);
my $ala = $ARGV[0];
shift; # Remove $ala from @ARGV
# check for plausible-ish alt load address
die "Bogus alt-load address"
if (length($ala) < 3 || index($ala, "0x") != 0);
# For mips32 or mips64 we need to use "--section-start=.reginfo=$ala" or
# "--section-start=.MIPS.options=$ala" respectively, because "-Ttext=$ala" will
# not put all the sections to the specificed address ($ala).
my $x = `cat ../config.log 2>&1 | grep host_cpu= | sed "s/host_cpu='//g"`;
my $arch = substr($x, 0, index($x, "'"));
my $extra_args;
if (($arch eq 'mips') || ($arch eq 'mipsel')
|| ($arch eq 'mipsisa32r2el')) {
$extra_args = "-static -Wl,--section-start=.reginfo=$ala";
} elsif (($arch eq 'mips64') || ($arch eq 'mips64el') ||
($arch eq 'mipsisa64el')) {
$extra_args = "-static -Wl,--section-start=.MIPS.options=$ala";
} else {
$extra_args = "-static -Wl,@FLAG_T_TEXT@=$ala";
}
my $cmd = join(" ", @ARGV, $extra_args);
#print "link_tool_exe_linux: $cmd\n";
# Execute the command:
my $r = system($cmd);
if ($r == 0) {
exit 0;
} else {
exit 1;
}