blob: 9d4c90f8369eae045fafb0df22b5248280ce65df [file] [log] [blame]
sewardjd3645802010-06-13 22:13:58 +00001#! @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
njnea2d6fd2010-07-01 00:20:20 +000015# alternative address', so these link_tool_exe_*.in scripts handle
16# the per-platform hoop-jumping.
sewardjd3645802010-06-13 22:13:58 +000017#
18# What we get passed here is:
19# first arg
20# the alternative load address
21# all the rest of the args
Elliott Hughesed398002017-06-21 14:41:24 -070022# the gcc invocation to do the final link, that
sewardjd3645802010-06-13 22:13:58 +000023# 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#
tom04ce9f52013-03-28 15:53:21 +000030# Linux specific complications:
sewardjd3645802010-06-13 22:13:58 +000031#
32# - need to support both old GNU ld and gold: use -Ttext= to
sewardje4bc7032013-03-22 11:49:46 +000033# set the text segment address if that is all we have. We really
34# need -Ttext-segment. Otherwise with GNU ld sections or notes
tom04ce9f52013-03-28 15:53:21 +000035# (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 ..].
sewardjd3645802010-06-13 22:13:58 +000046#
47# So: what we actually do:
48#
49# pass the specified command to the linker as-is, except, add
sewardje4bc7032013-03-22 11:49:46 +000050# "-static" and "-Ttext[-segment]=<argv[1]>" to it.
florian09501202012-09-15 19:31:07 +000051# 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.
sewardjd3645802010-06-13 22:13:58 +000056#
57
58use warnings;
59use strict;
60
61# expect at least: alt-load-address gcc -o foo bar.o
njn7e15bc32010-06-22 06:45:44 +000062die "Not enough arguments"
sewardjd3645802010-06-13 22:13:58 +000063 if (($#ARGV + 1) < 5);
64
65my $ala = $ARGV[0];
florian09501202012-09-15 19:31:07 +000066shift; # Remove $ala from @ARGV
sewardjd3645802010-06-13 22:13:58 +000067
68# check for plausible-ish alt load address
69die "Bogus alt-load address"
70 if (length($ala) < 3 || index($ala, "0x") != 0);
71
Elliott Hughesa0664b92017-04-18 17:46:52 -070072my $cmd = join(" ", @ARGV, "-static -Wl,@FLAG_T_TEXT@=$ala");
sewardjd3645802010-06-13 22:13:58 +000073
njn7e15bc32010-06-22 06:45:44 +000074#print "link_tool_exe_linux: $cmd\n";
sewardjd3645802010-06-13 22:13:58 +000075
76
77# Execute the command:
florian09501202012-09-15 19:31:07 +000078my $r = system($cmd);
sewardjd3645802010-06-13 22:13:58 +000079
80if ($r == 0) {
81 exit 0;
82} else {
83 exit 1;
84}