sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 1 | #! @PERL@ |
| 2 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 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. |
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 | # |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 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 | # |
| 31 | # So: what we actually do is: |
| 32 | # |
| 33 | # Look at the specified gcc invokation. Ignore all parts of it except |
| 34 | # the *.a, *.o and -o outfile parts. Wrap them up in a new command |
| 35 | # which looks (eg) as follows: |
| 36 | # |
| 37 | # (64-bit): |
| 38 | # |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 39 | # /usr/bin/ld -static -arch x86_64 -macosx_version_min 10.6 \ |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 40 | # -o memcheck-amd64-darwin -u __start -e __start \ |
| 41 | # -image_base 0x138000000 -stack_addr 0x13c000000 \ |
| 42 | # -stack_size 0x800000 \ |
| 43 | # memcheck_amd*.o \ |
| 44 | # ../coregrind/libcoregrind-amd64-darwin.a \ |
| 45 | # ../VEX/libvex-amd64-darwin.a |
| 46 | # |
| 47 | # (32-bit) |
| 48 | # |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 49 | # /usr/bin/ld -static -arch i386 -macosx_version_min 10.6 \ |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 50 | # -o memcheck-x86-darwin -u __start -e __start \ |
| 51 | # -image_base 0x38000000 -stack_addr 0x3c000000 \ |
| 52 | # -stack_size 0x800000 \ |
| 53 | # memcheck_x86*.o \ |
| 54 | # ../coregrind/libcoregrind-x86-darwin.a \ |
| 55 | # ../VEX/libvex-x86-darwin.a |
| 56 | # |
| 57 | # The addresses shown above will actually work, although "for real" we |
| 58 | # of course need to take it from argv[1]. In these examples the stack |
| 59 | # is placed 64M after the executable start. It is probably safer to |
| 60 | # place it 64M before the executable's start point, so the executable |
| 61 | # + data + bss can grow arbitrarily in future without colliding with |
| 62 | # the stack. |
| 63 | # |
| 64 | # There's one more twist: we need to know the word size of the |
| 65 | # executable for which we are linking. We need to know this because |
| 66 | # we must tell the linker that, by handing it either "-arch x86_64" or |
| 67 | # "-arch i386". Fortunately we can figure this out by scanning the |
| 68 | # gcc invokation, which itself must contain either "-arch x86_64" or |
| 69 | # "-arch i386". |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 70 | |
| 71 | use warnings; |
| 72 | use strict; |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 73 | # we need to be able to do 64-bit arithmetic: |
| 74 | use Math::BigInt; |
| 75 | |
| 76 | |
| 77 | # User configurable constants: how far before the exe should we |
| 78 | # place the stack? |
| 79 | my $TX_STACK_OFFSET_BEFORE_TEXT = 64 * 1024 * 1024; |
| 80 | |
| 81 | # and how big should the stack be? |
| 82 | my $TX_STACK_SIZE = 8 * 1024 * 1024; |
| 83 | |
| 84 | |
| 85 | # string -> bool |
| 86 | sub is_dota_or_doto($) |
| 87 | { |
| 88 | my ($str) = @_; |
| 89 | if ($str =~ /.\.a$/ || $str =~ /.\.o$/) { |
| 90 | return 1; |
| 91 | } else { |
| 92 | return 0; |
| 93 | } |
| 94 | } |
| 95 | |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 96 | |
| 97 | # expect at least: alt-load-address gcc -o foo bar.o |
njn | 7e15bc3 | 2010-06-22 06:45:44 +0000 | [diff] [blame] | 98 | die "Not enough arguments" |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 99 | if (($#ARGV + 1) < 5); |
| 100 | |
sewardj | f0aedc4 | 2012-07-20 16:46:54 +0000 | [diff] [blame] | 101 | my $ala = $ARGV[0]; # the load address to use |
| 102 | my $cc = $ARGV[1]; # the C compiler in use |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 103 | |
| 104 | # check for plausible-ish alt load address |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 105 | die "Bogus alt-load address (1)" |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 106 | if (length($ala) < 3 || index($ala, "0x") != 0); |
| 107 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 108 | die "Bogus alt-load address (2)" |
| 109 | if ($ala !~ /^0x[0-9a-fA-F]+$/); |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 110 | |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 111 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 112 | # get hold of the outfile name (following "-o") |
| 113 | my $outname = ""; |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 114 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 115 | foreach my $n (2 .. $#ARGV - 1) { |
| 116 | my $str = $ARGV[$n]; |
| 117 | if ($str eq "-o" && $outname eq "") { |
| 118 | $outname = $ARGV[$n + 1]; |
| 119 | } |
| 120 | } |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 121 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 122 | die "Can't find '-o outfilename' in command line" |
| 123 | if ($outname eq ""); |
| 124 | |
| 125 | |
| 126 | # get hold of the string following "-arch" |
| 127 | my $archstr = ""; |
| 128 | |
| 129 | foreach my $n (2 .. $#ARGV - 1) { |
| 130 | my $str = $ARGV[$n]; |
| 131 | if ($str eq "-arch" && $archstr eq "") { |
| 132 | $archstr = $ARGV[$n + 1]; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | die "Can't find '-arch archstr' in command line" |
| 137 | if ($archstr eq ""); |
| 138 | |
| 139 | |
| 140 | # build the command line |
| 141 | my $cmd = "/usr/bin/ld"; |
| 142 | |
| 143 | $cmd = "$cmd -static"; |
sewardj | f0aedc4 | 2012-07-20 16:46:54 +0000 | [diff] [blame] | 144 | |
| 145 | # If we're building with clang (viz, the C compiler as specified |
| 146 | # by the 2nd arg ends in "clang"), we also need -new_linker. See |
| 147 | # https://bugs.kde.org/show_bug.cgi?id=295427 |
| 148 | if ("$cc" =~ /clang$/) { |
| 149 | $cmd = "$cmd -new_linker"; |
| 150 | } |
| 151 | |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 152 | $cmd = "$cmd -arch $archstr"; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame^] | 153 | $cmd = "$cmd -macosx_version_min 10.6"; |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 154 | $cmd = "$cmd -o $outname"; |
| 155 | $cmd = "$cmd -u __start -e __start"; |
| 156 | |
| 157 | my $stack_addr = Math::BigInt->new( $ala ) - $TX_STACK_OFFSET_BEFORE_TEXT; |
| 158 | my $stack_addr_str = $stack_addr->as_hex(); |
| 159 | my $stack_size_str = Math::BigInt::as_hex($TX_STACK_SIZE); |
| 160 | |
| 161 | $cmd = "$cmd -image_base $ala"; |
| 162 | $cmd = "$cmd -stack_addr $stack_addr_str"; |
| 163 | $cmd = "$cmd -stack_size $stack_size_str"; |
| 164 | |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 165 | foreach my $n (2 .. $#ARGV) { |
njn | ea2d6fd | 2010-07-01 00:20:20 +0000 | [diff] [blame] | 166 | my $str = $ARGV[$n]; |
| 167 | if (is_dota_or_doto($str)) { |
| 168 | $cmd = "$cmd $str"; |
| 169 | } |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 170 | } |
| 171 | |
sewardj | 08f5a27 | 2011-04-06 11:17:16 +0000 | [diff] [blame] | 172 | print "link_tool_exe_darwin: $cmd\n"; |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 173 | |
| 174 | # Execute the command: |
| 175 | my $r = system("$cmd"); |
| 176 | |
sewardj | 08f5a27 | 2011-04-06 11:17:16 +0000 | [diff] [blame] | 177 | if ($r != 0) { |
| 178 | exit 1; |
sewardj | d364580 | 2010-06-13 22:13:58 +0000 | [diff] [blame] | 179 | } |
sewardj | 08f5a27 | 2011-04-06 11:17:16 +0000 | [diff] [blame] | 180 | |
| 181 | |
| 182 | # and now kludge the tool exe |
| 183 | # see bug 267997 |
| 184 | |
| 185 | $cmd = "../coregrind/fixup_macho_loadcmds"; |
| 186 | $cmd = "$cmd $stack_addr_str $stack_size_str $outname"; |
| 187 | |
| 188 | print "link_tool_exe_darwin: $cmd\n"; |
| 189 | |
sewardj | f952035 | 2011-04-11 22:14:03 +0000 | [diff] [blame] | 190 | $r = system("$cmd"); |
sewardj | 08f5a27 | 2011-04-06 11:17:16 +0000 | [diff] [blame] | 191 | |
| 192 | if ($r != 0) { |
| 193 | exit 1; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | exit 0; |