Home | History | Annotate | Download | only in coregrind
      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 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 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 
     58 use warnings;
     59 use strict;
     60 
     61 # expect at least: alt-load-address gcc -o foo bar.o
     62 die "Not enough arguments"
     63     if (($#ARGV + 1) < 5);
     64 
     65 my $ala = $ARGV[0];
     66 shift; # Remove $ala from @ARGV
     67 
     68 # check for plausible-ish alt load address
     69 die "Bogus alt-load address"
     70     if (length($ala) < 3 || index($ala, "0x") != 0);
     71 
     72 # For mips32 or mips64 we need to use "--section-start=.reginfo=$ala" or
     73 # "--section-start=.MIPS.options=$ala" respectively, because "-Ttext=$ala" will
     74 # not put all the sections to the specificed address ($ala).
     75 my $x = `cat ../config.log 2>&1 | grep host_cpu= | sed "s/host_cpu='//g"`;
     76 my $arch = substr($x, 0, index($x, "'"));
     77 
     78 my $extra_args;
     79 if (($arch eq 'mips') || ($arch eq 'mipsel')
     80     || ($arch eq 'mipsisa32r2el')) {
     81    $extra_args = "-static -Wl,--section-start=.reginfo=$ala";
     82 } elsif (($arch eq 'mips64') || ($arch eq 'mips64el') ||
     83          ($arch eq 'mipsisa64el')) {
     84    $extra_args = "-static -Wl,--section-start=.MIPS.options=$ala";
     85 } else {
     86    $extra_args = "-static -Wl,@FLAG_T_TEXT@=$ala";
     87 }
     88 
     89 my $cmd = join(" ", @ARGV, $extra_args);
     90 
     91 #print "link_tool_exe_linux: $cmd\n";
     92 
     93 
     94 # Execute the command:
     95 my $r = system($cmd);
     96 
     97 if ($r == 0) {
     98     exit 0;
     99 } else {
    100     exit 1;
    101 }
    102