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 |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame] | 22 | # the gcc invocation to do the final link, that |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 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 | # |
tom | 04ce9f5 | 2013-03-28 15:53:21 +0000 | [diff] [blame] | 30 | # Linux specific complications: |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 31 | # |
| 32 | # - need to support both old GNU ld and gold: use -Ttext= to |
sewardj | e4bc703 | 2013-03-22 11:49:46 +0000 | [diff] [blame] | 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 |
tom | 04ce9f5 | 2013-03-28 15:53:21 +0000 | [diff] [blame] | 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 ..]. |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 46 | # |
| 47 | # So: what we actually do: |
| 48 | # |
| 49 | # pass the specified command to the linker as-is, except, add |
sewardj | e4bc703 | 2013-03-22 11:49:46 +0000 | [diff] [blame] | 50 | # "-static" and "-Ttext[-segment]=<argv[1]>" to it. |
florian | 0950120 | 2012-09-15 19:31:07 +0000 | [diff] [blame] | 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. |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 56 | # |
| 57 | |
| 58 | use warnings; |
| 59 | use strict; |
| 60 | |
| 61 | # expect at least: alt-load-address gcc -o foo bar.o |
njn | 7e15bc3 | 2010-06-22 06:45:44 +0000 | [diff] [blame] | 62 | die "Not enough arguments" |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 63 | if (($#ARGV + 1) < 5); |
| 64 | |
| 65 | my $ala = $ARGV[0]; |
florian | 0950120 | 2012-09-15 19:31:07 +0000 | [diff] [blame] | 66 | shift; # Remove $ala from @ARGV |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 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 | |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 72 | my $cmd = join(" ", @ARGV, "-static -Wl,@FLAG_T_TEXT@=$ala"); |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 73 | |
njn | 7e15bc3 | 2010-06-22 06:45:44 +0000 | [diff] [blame] | 74 | #print "link_tool_exe_linux: $cmd\n"; |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 75 | |
| 76 | |
| 77 | # Execute the command: |
florian | 0950120 | 2012-09-15 19:31:07 +0000 | [diff] [blame] | 78 | my $r = system($cmd); |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 79 | |
| 80 | if ($r == 0) { |
| 81 | exit 0; |
| 82 | } else { |
| 83 | exit 1; |
| 84 | } |