2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """A utility script to help building Syzygy-reordered Chrome binaries."""
15 # The default relink executable to use to reorder binaries.
16 _DEFAULT_RELINKER
= os
.path
.join(
17 os
.path
.join(os
.path
.dirname(__file__
), '../../../..'),
18 'third_party/syzygy/binaries/exe/relink.exe')
20 _LOGGER
= logging
.getLogger()
22 # We use the same seed for all random reorderings to get a deterministic build.
23 _RANDOM_SEED
= 1347344
26 def _Shell(*cmd
, **kw
):
27 """Shells out to "cmd". Returns a tuple of cmd's stdout, stderr."""
28 _LOGGER
.info('Running command "%s".', cmd
)
29 prog
= subprocess
.Popen(cmd
, **kw
)
31 stdout
, stderr
= prog
.communicate()
32 if prog
.returncode
!= 0:
33 raise RuntimeError('Command "%s" returned %d.' % (cmd
, prog
.returncode
))
38 def _ReorderBinary(relink_exe
, executable
, symbol
, destination_dir
):
39 """Reorders the executable found in input_dir, and writes the resultant
40 reordered executable and symbol files to destination_dir.
42 If a file named <executable>-order.json exists, imposes that order on the
43 output binaries, otherwise orders them randomly.
47 '--input-image=%s' % executable
,
48 '--input-pdb=%s' % symbol
,
49 '--output-image=%s' % os
.path
.abspath(
50 os
.path
.join(destination_dir
, os
.path
.basename(executable
))),
51 '--output-pdb=%s' % os
.path
.abspath(
52 os
.path
.join(destination_dir
, os
.path
.basename(symbol
))),]
54 # Check whether there's an order file available for the executable.
55 order_file
= '%s-order.json' % executable
56 if os
.path
.exists(order_file
):
57 # The ordering file exists, let's use that.
58 _LOGGER
.info('Reordering "%s" according to "%s".',
59 os
.path
.basename(executable
),
60 os
.path
.basename(order_file
))
61 cmd
.append('--order-file=%s' % order_file
)
63 # No ordering file, we randomize the output.
64 _LOGGER
.info('Randomly reordering "%s"', executable
)
65 cmd
.append('--seed=%d' % _RANDOM_SEED
)
71 logging
.basicConfig(level
=logging
.INFO
)
73 # Make sure the destination directory exists.
74 if not os
.path
.isdir(options
.destination_dir
):
75 _LOGGER
.info('Creating destination directory "%s".',
76 options
.destination_dir
)
77 os
.makedirs(options
.destination_dir
)
79 # Reorder the binaries into the destination directory.
80 _ReorderBinary(options
.relinker
,
81 options
.input_executable
,
83 options
.destination_dir
)
87 option_parser
= optparse
.OptionParser()
88 option_parser
.add_option('--input_executable',
89 help='The path to the input executable.')
90 option_parser
.add_option('--input_symbol',
91 help='The path to the input symbol file.')
92 option_parser
.add_option('--relinker', default
=_DEFAULT_RELINKER
,
93 help='Relinker exectuable to use, defaults to "%s"' % _DEFAULT_RELINKER
)
94 option_parser
.add_option('-d', '--destination_dir',
95 help='Destination directory for reordered files, defaults to '
96 'the subdirectory "reordered" in the output_dir.')
97 options
, args
= option_parser
.parse_args()
99 if not options
.input_executable
:
100 option_parser
.error('You must provide an input executable.')
101 if not options
.input_symbol
:
102 option_parser
.error('You must provide an input symbol file.')
104 if not options
.destination_dir
:
105 options
.destination_dir
= os
.path
.join(options
.output_dir
, 'reordered')
110 if '__main__' == __name__
:
111 sys
.exit(main(_ParseOptions()))