Index: chrome/tools/build/win/syzygy_reorder.py |
diff --git a/chrome/tools/build/win/syzygy_reorder.py b/chrome/tools/build/win/syzygy_reorder.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8ddef1046d3289d0809bf067f258442d73e405d1 |
--- /dev/null |
+++ b/chrome/tools/build/win/syzygy_reorder.py |
@@ -0,0 +1,110 @@ |
+#!/usr/bin/python |
+# Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""A utility script to help building Syzygy-reordered Chrome binaries.""" |
+ |
+import logging |
+import optparse |
+import os |
+import subprocess |
+import sys |
+ |
+ |
+_SRC_DIR = os.path.abspath( |
+ os.path.join(os.path.dirname(__file__), '../../../..')) |
robertshield
2011/11/28 01:16:38
seems like this should be an input option
Sigurður Ásgeirsson
2011/11/28 15:02:10
It's only used to calculate the path to the defaul
|
+ |
+# The default relink executable to use to reorder binaries. |
+_DEFAULT_RELINKER = os.path.join(_SRC_DIR, |
+ 'third_party/syzygy/binaries/exe/relink.exe') |
+ |
+# A list of tuples, where each tuple names a binary and its associated PDB file. |
+_BINARIES = [('chrome.dll', 'chrome_dll.pdb'),] |
robertshield
2011/11/28 01:16:38
feels like this should be specified in a .gyp file
Sigurður Ásgeirsson
2011/11/28 15:02:10
This is true. However, I was modelling this from t
|
+ |
+_LOGGER = logging.getLogger() |
+ |
+# We use the same seed for all random reorderings to get a deterministic build. |
+_RANDOM_SEED = 1347344 |
robertshield
2011/11/28 01:16:38
this is not a comment, but http://xkcd.com/221/
Sigurður Ásgeirsson
2011/11/28 15:02:10
:)
|
+ |
+ |
+def _Shell(*cmd, **kw): |
+ """Shells out to "cmd". Returns a tuple of cmd's stdout, stderr.""" |
+ _LOGGER.info('Running command "%s".', cmd) |
+ prog = subprocess.Popen(cmd, **kw) |
+ |
+ stdout, stderr = prog.communicate() |
+ if prog.returncode != 0: |
+ raise RuntimeError('Command "%s" returned %d.' % (cmd, prog.returncode)) |
+ |
+ return stdout, stderr |
+ |
+ |
+def _ReorderBinary(relink_exe, binary, symbol, input_dir, output_dir): |
+ """Reorders the binary found in input_dir, and writes the resultant |
+ reordered binary and symbol files to output_dir. |
+ |
+ If a file named <binary>-order.json exists in the input directory, imposes |
+ that order on the output binaries, otherwise orders them randomly. |
+ """ |
+ cmd = [relink_exe, |
+ '--verbose', |
+ '--input-dll=%s' % os.path.join(input_dir, binary), |
+ '--input-pdb=%s' % os.path.join(input_dir, symbol), |
+ '--output-dll=%s' % os.path.join(output_dir, binary), |
+ '--output-pdb=%s' % os.path.join(output_dir, symbol),] |
+ |
+ # Check whether there's an order file available for the binary. |
+ order_file = os.path.join(input_dir, '%s-order.json' % binary) |
+ if os.path.exists(order_file): |
+ # The ordering file exists, let's use that. |
+ _LOGGER.info('Reordering "%s" according to "%s".', binary, order_file) |
+ cmd.append('--order-file=%s' % order_file) |
+ else: |
+ # No ordering file, we randomize the output. |
robertshield
2011/11/28 01:16:38
Is the reordered path soon expected to be the defa
Sigurður Ásgeirsson
2011/11/28 15:02:10
Good question - I figure once we turn this on for
|
+ _LOGGER.info('Randomly reordering "%s"', binary) |
+ cmd.append('--seed=%d' % _RANDOM_SEED) |
+ |
+ return _Shell(*cmd) |
+ |
+ |
+def main(options): |
+ logging.basicConfig(level=logging.INFO) |
+ |
+ # Make sure the destination directory exists. |
+ if not os.path.isdir(options.destination_dir): |
+ _LOGGER.info('Creating destination directory "%s".', |
+ options.destination_dir) |
+ os.makedirs(options.destination_dir) |
+ |
+ for (binary, symbol) in _BINARIES: |
+ # Reorder the binaries into the destination directory, one by one. |
+ _ReorderBinary(options.relinker, |
+ binary, |
+ symbol, |
+ options.output_dir, |
+ options.destination_dir) |
+ |
+ |
+def _ParseOptions(): |
+ option_parser = optparse.OptionParser() |
+ option_parser.add_option('-o', '--output_dir', |
+ help='Output directory where the original binaries are built.') |
+ option_parser.add_option('--relinker', default=_DEFAULT_RELINKER, |
+ help='Relinker exectuable to use, defaults to "%s"' % _DEFAULT_RELINKER) |
+ option_parser.add_option('-d', '--destination_dir', |
+ help='Destination directory for reordered files, defaults to ' |
+ 'the subdirectory "reordered" in the output_dir.') |
+ options, args = option_parser.parse_args() |
+ |
+ if not options.output_dir: |
+ option_parser.error('You must provide an output dir.') |
+ |
+ if not options.destination_dir: |
+ options.destination_dir = os.path.join(options.output_dir, 'reordered') |
+ |
+ return options |
+ |
+ |
+if '__main__' == __name__: |
+ sys.exit(main(_ParseOptions())) |