3 # This script handles linking the tool executables on Linux,
4 # statically and at an alternative load address.
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.
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.
18 # What we get passed here is:
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
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.
30 # Linux specific complications:
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.
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
47 # So: what we actually do:
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.
61 # expect at least: alt-load-address gcc -o foo bar.o
62 die "Not enough arguments"
63 if (($#ARGV + 1) < 5);
66 shift; # Remove $ala from @ARGV
68 # check for plausible-ish alt load address
69 die "Bogus alt-load address"
70 if (length($ala) < 3 || index($ala, "0x") != 0);
72 my $cmd = join(" ", @ARGV, "-static -Wl,@FLAG_T_TEXT@=$ala");
74 #print "link_tool_exe_linux: $cmd\n";
77 # Execute the command: