1#! @PERL@
2
3# This script handles linking the tool executables on Linux,
4# statically and at an alternative load address.
5#
6# Linking statically sidesteps all sorts of complications to do with
7# having two copies of the dynamic linker (valgrind's and the
8# client's) coexisting in the same process.  The alternative load
9# address is needed because Valgrind itself will load the client at
10# whatever address it specifies, which is almost invariably the
11# default load address.  Hence we can't allow Valgrind itself (viz,
12# the tool executable) to be loaded at that address.
13#
14# Unfortunately there's no standard way to do 'static link at
15# alternative address', so these link_tool_exe_*.in scripts handle
16# the per-platform hoop-jumping.
17#
18# What we get passed here is:
19#   first arg
20#      the alternative load address
21#   all the rest of the args
22#      the gcc invocation to do the final link, that
23#      the build system would have done, left to itself
24#
25# We just let the script 'die' if something is wrong, rather than do
26# proper error reporting.  We don't expect the users to run this
27# directly.  It is only run as part of the build process, with
28# carefully constrained inputs.
29#
30# Linux specific complications:
31#
32# - need to support both old GNU ld and gold: use -Ttext= to
33#   set the text segment address if that is all we have. We really
34#   need -Ttext-segment. Otherwise with GNU ld sections or notes
35#   (like the build-id) don't get at the desired address. But older
36#   linkers only know about -Ttext, not -Ttext-segment. So configure
37#   checks for us and sets FLAG_T_TEXT.
38#
39# - If all we have is -Ttext, then we need to pass --build-id=none
40#   (that is, -Wl,--build-id=none to gcc) if it accepts it, to ensure
41#   the linker doesn't add a notes section which ends up at the default
42#   load address and so defeats our attempts to keep that address clear
43#   for the client.  However, older linkers don't support this flag,
44#   so it is tested for by configure.in and is shipped to us as part of
45#   argv[2 ..].
46#
47# So: what we actually do:
48#
49#   pass the specified command to the linker as-is, except, add
50#   "-static" and "-Ttext[-segment]=<argv[1]>" to it.
51#   Previously we did this by adding these options after the first
52#   word of the rest of the arguments, which works in the common case
53#   when it's something like "gcc". But the linker invocation itself
54#   might be multiple words, say if it's "ccache gcc". So we now put
55#   the new options at the end instead.
56#
57
58use warnings;
59use strict;
60
61# expect at least: alt-load-address gcc -o foo bar.o
62die "Not enough arguments"
63    if (($#ARGV + 1) < 5);
64
65my $ala = $ARGV[0];
66shift; # Remove $ala from @ARGV
67
68# check for plausible-ish alt load address
69die "Bogus alt-load address"
70    if (length($ala) < 3 || index($ala, "0x") != 0);
71
72my $cmd = join(" ", @ARGV, "-static -Wl,@FLAG_T_TEXT@=$ala");
73
74#print "link_tool_exe_linux: $cmd\n";
75
76
77# Execute the command:
78my $r = system($cmd);
79
80if ($r == 0) {
81    exit 0;
82} else {
83    exit 1;
84}
85