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. 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 50 use warnings; 51 use strict; 52 53 # expect at least: alt-load-address gcc -o foo bar.o 54 die "Not enough arguments" 55 if (($#ARGV + 1) < 5); 56 57 my $ala = $ARGV[0]; 58 59 # check for plausible-ish alt load address 60 die "Bogus alt-load address" 61 if (length($ala) < 3 || index($ala, "0x") != 0); 62 63 # The cc invokation to do the final link 64 my $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 71 # For mips we need to use "--section-start=.reginfo=$ala" because 72 # "--section-start=.reginfo=$ala" will put all the sections to the 73 # specificed address ($ala) 74 my $x=`$cc -v 2>&1 | grep Target | sed 's/Target: //g'`; 75 my $arch=substr($x, 0, index($x, '-')); 76 my $cmd; 77 78 if (($arch eq 'mips') || ($arch eq 'mipsel')) { 79 $cmd = "$cc -static -Wl,--section-start=.reginfo=$ala"; 80 } else { 81 $cmd = "$cc -static -Wl,-Ttext=$ala"; 82 } 83 84 # Add the rest of the parameters 85 foreach my $n (2 .. $#ARGV) { 86 $cmd = "$cmd $ARGV[$n]"; 87 } 88 89 #print "link_tool_exe_linux: $cmd\n"; 90 91 92 # Execute the command: 93 my $r = system("$cmd"); 94 95 if ($r == 0) { 96 exit 0; 97 } else { 98 exit 1; 99 } 100