sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 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 |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 15 | # alternative address', so these link_tool_exe_*.in scripts handle |
| 16 | # the per-platform hoop-jumping. |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 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 |
njn | 7e15bc3 | 2010-06-22 06:45:44 +0000 | [diff] [blame] | 54 | die "Not enough arguments" |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 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 | my $cmd="$cc -static -Wl,-Ttext=$ala"; |
| 72 | |
| 73 | # Add the rest of the parameters |
| 74 | foreach my $n (2 .. $#ARGV) { |
| 75 | $cmd = "$cmd $ARGV[$n]"; |
| 76 | } |
| 77 | |
njn | 7e15bc3 | 2010-06-22 06:45:44 +0000 | [diff] [blame] | 78 | #print "link_tool_exe_linux: $cmd\n"; |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 79 | |
| 80 | |
| 81 | # Execute the command: |
| 82 | my $r = system("$cmd"); |
| 83 | |
| 84 | if ($r == 0) { |
| 85 | exit 0; |
| 86 | } else { |
| 87 | exit 1; |
| 88 | } |