blob: 6de2562f4560bd54339d53ab47bf3bf410dd7bbe [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
22# the gcc invokation 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.
34#
35# - need to pass --build-id=none (that is, -Wl,--build-id=none to
36# gcc) if it accepts it, to ensure the linker doesn't add a
37# notes section which ends up at the default load address and
38# so defeats our attempts to keep that address clear for the
39# client. However, older linkers don't support this flag, so it
40# is tested for by configure.in and is shipped to us as part of
41# argv[2 ..].
42#
43#
44# So: what we actually do:
45#
46# pass the specified command to the linker as-is, except, add
47# "-static" and "-Ttext=<argv[1]>" to it.
48#
49
50use warnings;
51use strict;
52
53# expect at least: alt-load-address gcc -o foo bar.o
njn7e15bc32010-06-22 06:45:44 +000054die "Not enough arguments"
sewardjd3645802010-06-13 22:13:58 +000055 if (($#ARGV + 1) < 5);
56
57my $ala = $ARGV[0];
58
59# check for plausible-ish alt load address
60die "Bogus alt-load address"
61 if (length($ala) < 3 || index($ala, "0x") != 0);
62
63# The cc invokation to do the final link
64my $cc = $ARGV[1];
65
66# and the 'restargs' are argv[2 ..]
67
68# so, build up the complete command here:
69# 'cc' -static -Ttext='ala' 'restargs'
70
71my $cmd="$cc -static -Wl,-Ttext=$ala";
72
73# Add the rest of the parameters
74foreach my $n (2 .. $#ARGV) {
75 $cmd = "$cmd $ARGV[$n]";
76}
77
njn7e15bc32010-06-22 06:45:44 +000078#print "link_tool_exe_linux: $cmd\n";
sewardjd3645802010-06-13 22:13:58 +000079
80
81# Execute the command:
82my $r = system("$cmd");
83
84if ($r == 0) {
85 exit 0;
86} else {
87 exit 1;
88}