1#!/usr/bin/env python
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.
5
6"""A utility script to help building Syzygy-reordered Chrome binaries."""
7
8import logging
9import optparse
10import os
11import subprocess
12import sys
13
14
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')
19
20_LOGGER = logging.getLogger()
21
22# We use the same seed for all random reorderings to get a deterministic build.
23_RANDOM_SEED = 1347344
24
25
26def _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)
30
31  stdout, stderr = prog.communicate()
32  if prog.returncode != 0:
33    raise RuntimeError('Command "%s" returned %d.' % (cmd, prog.returncode))
34
35  return stdout, stderr
36
37
38def _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.
41
42  If a file named <executable>-order.json exists, imposes that order on the
43  output binaries, otherwise orders them randomly.
44  """
45  cmd = [relink_exe,
46         '--overwrite',
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))),]
53
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)
62  else:
63    # No ordering file, we randomize the output.
64    _LOGGER.info('Randomly reordering "%s"', executable)
65    cmd.append('--seed=%d' % _RANDOM_SEED)
66
67  return _Shell(*cmd)
68
69
70def main(options):
71  logging.basicConfig(level=logging.INFO)
72
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)
78
79  # Reorder the binaries into the destination directory.
80  _ReorderBinary(options.relinker,
81                 options.input_executable,
82                 options.input_symbol,
83                 options.destination_dir)
84
85
86def _ParseOptions():
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()
98
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.')
103
104  if not options.destination_dir:
105    options.destination_dir = os.path.join(options.output_dir, 'reordered')
106
107  return options
108
109
110if '__main__' == __name__:
111  sys.exit(main(_ParseOptions()))
112