xref: /aosp_15_r20/external/angle/build/win/reorder-imports.py (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*8975f5c5SAndroid Build Coastguard Worker# Copyright 2014 The Chromium Authors
3*8975f5c5SAndroid Build Coastguard Worker# Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker# found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker
6*8975f5c5SAndroid Build Coastguard Workerimport glob
7*8975f5c5SAndroid Build Coastguard Workerimport optparse
8*8975f5c5SAndroid Build Coastguard Workerimport os
9*8975f5c5SAndroid Build Coastguard Workerimport shutil
10*8975f5c5SAndroid Build Coastguard Workerimport sys
11*8975f5c5SAndroid Build Coastguard Worker
12*8975f5c5SAndroid Build Coastguard Workersys.path.insert(
13*8975f5c5SAndroid Build Coastguard Worker    0,
14*8975f5c5SAndroid Build Coastguard Worker    os.path.join(os.path.dirname(__file__), '..', '..', 'third_party',
15*8975f5c5SAndroid Build Coastguard Worker                 'pefile_py3'))
16*8975f5c5SAndroid Build Coastguard Workerimport pefile
17*8975f5c5SAndroid Build Coastguard Worker
18*8975f5c5SAndroid Build Coastguard Workerdef reorder_imports(input_dir, output_dir, architecture):
19*8975f5c5SAndroid Build Coastguard Worker  """Swap chrome_elf.dll to be the first import of chrome.exe.
20*8975f5c5SAndroid Build Coastguard Worker  Also copy over any related files that might be needed
21*8975f5c5SAndroid Build Coastguard Worker  (pdbs, manifests etc.).
22*8975f5c5SAndroid Build Coastguard Worker  """
23*8975f5c5SAndroid Build Coastguard Worker  # TODO(thakis): See if there is a reliable way to write the
24*8975f5c5SAndroid Build Coastguard Worker  # correct executable in the first place, so that this script
25*8975f5c5SAndroid Build Coastguard Worker  # only needs to verify that and not write a whole new exe.
26*8975f5c5SAndroid Build Coastguard Worker
27*8975f5c5SAndroid Build Coastguard Worker  input_image = os.path.join(input_dir, 'chrome.exe')
28*8975f5c5SAndroid Build Coastguard Worker  output_image = os.path.join(output_dir, 'chrome.exe')
29*8975f5c5SAndroid Build Coastguard Worker
30*8975f5c5SAndroid Build Coastguard Worker  # pefile mmap()s the whole executable, and then parses parts of
31*8975f5c5SAndroid Build Coastguard Worker  # it into python data structures for ease of processing.
32*8975f5c5SAndroid Build Coastguard Worker  # To write the file again, only the mmap'd data is written back,
33*8975f5c5SAndroid Build Coastguard Worker  # so modifying the parsed python objects generally has no effect.
34*8975f5c5SAndroid Build Coastguard Worker  # However, parsed raw data ends up in pe.Structure instances,
35*8975f5c5SAndroid Build Coastguard Worker  # and these all get serialized back when the file gets written.
36*8975f5c5SAndroid Build Coastguard Worker  # So things that are in a Structure must have their data set
37*8975f5c5SAndroid Build Coastguard Worker  # through the Structure, while other data must bet set through
38*8975f5c5SAndroid Build Coastguard Worker  # the set_bytes_*() methods.
39*8975f5c5SAndroid Build Coastguard Worker  pe = pefile.PE(input_image, fast_load=True)
40*8975f5c5SAndroid Build Coastguard Worker  if architecture == 'x64' or architecture == 'arm64':
41*8975f5c5SAndroid Build Coastguard Worker    assert pe.PE_TYPE == pefile.OPTIONAL_HEADER_MAGIC_PE_PLUS
42*8975f5c5SAndroid Build Coastguard Worker  else:
43*8975f5c5SAndroid Build Coastguard Worker    assert pe.PE_TYPE == pefile.OPTIONAL_HEADER_MAGIC_PE
44*8975f5c5SAndroid Build Coastguard Worker
45*8975f5c5SAndroid Build Coastguard Worker  pe.parse_data_directories(directories=[
46*8975f5c5SAndroid Build Coastguard Worker      pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_IMPORT']])
47*8975f5c5SAndroid Build Coastguard Worker
48*8975f5c5SAndroid Build Coastguard Worker  found_elf = False
49*8975f5c5SAndroid Build Coastguard Worker  for i, peimport in enumerate(pe.DIRECTORY_ENTRY_IMPORT):
50*8975f5c5SAndroid Build Coastguard Worker    if peimport.dll.lower() == b'chrome_elf.dll':
51*8975f5c5SAndroid Build Coastguard Worker      assert not found_elf, 'only one chrome_elf.dll import expected'
52*8975f5c5SAndroid Build Coastguard Worker      found_elf = True
53*8975f5c5SAndroid Build Coastguard Worker      if i > 0:
54*8975f5c5SAndroid Build Coastguard Worker        swap = pe.DIRECTORY_ENTRY_IMPORT[0]
55*8975f5c5SAndroid Build Coastguard Worker
56*8975f5c5SAndroid Build Coastguard Worker        # Morally we want to swap peimport.struct and swap.struct here,
57*8975f5c5SAndroid Build Coastguard Worker        # but the pe module doesn't expose a public method on Structure
58*8975f5c5SAndroid Build Coastguard Worker        # to get all data of a Structure without explicitly listing all
59*8975f5c5SAndroid Build Coastguard Worker        # field names.
60*8975f5c5SAndroid Build Coastguard Worker        # NB: OriginalFirstThunk and Characteristics are an union both at
61*8975f5c5SAndroid Build Coastguard Worker        # offset 0, handling just one of them is enough.
62*8975f5c5SAndroid Build Coastguard Worker        peimport.struct.OriginalFirstThunk, swap.struct.OriginalFirstThunk = \
63*8975f5c5SAndroid Build Coastguard Worker            swap.struct.OriginalFirstThunk, peimport.struct.OriginalFirstThunk
64*8975f5c5SAndroid Build Coastguard Worker        peimport.struct.TimeDateStamp, swap.struct.TimeDateStamp = \
65*8975f5c5SAndroid Build Coastguard Worker            swap.struct.TimeDateStamp, peimport.struct.TimeDateStamp
66*8975f5c5SAndroid Build Coastguard Worker        peimport.struct.ForwarderChain, swap.struct.ForwarderChain = \
67*8975f5c5SAndroid Build Coastguard Worker            swap.struct.ForwarderChain, peimport.struct.ForwarderChain
68*8975f5c5SAndroid Build Coastguard Worker        peimport.struct.Name, swap.struct.Name = \
69*8975f5c5SAndroid Build Coastguard Worker            swap.struct.Name, peimport.struct.Name
70*8975f5c5SAndroid Build Coastguard Worker        peimport.struct.FirstThunk, swap.struct.FirstThunk = \
71*8975f5c5SAndroid Build Coastguard Worker            swap.struct.FirstThunk, peimport.struct.FirstThunk
72*8975f5c5SAndroid Build Coastguard Worker  assert found_elf, 'chrome_elf.dll import not found'
73*8975f5c5SAndroid Build Coastguard Worker
74*8975f5c5SAndroid Build Coastguard Worker  pe.write(filename=output_image)
75*8975f5c5SAndroid Build Coastguard Worker
76*8975f5c5SAndroid Build Coastguard Worker  for fname in glob.iglob(os.path.join(input_dir, 'chrome.exe.*')):
77*8975f5c5SAndroid Build Coastguard Worker    shutil.copy(fname, os.path.join(output_dir, os.path.basename(fname)))
78*8975f5c5SAndroid Build Coastguard Worker  return 0
79*8975f5c5SAndroid Build Coastguard Worker
80*8975f5c5SAndroid Build Coastguard Worker
81*8975f5c5SAndroid Build Coastguard Workerdef main(argv):
82*8975f5c5SAndroid Build Coastguard Worker  usage = 'reorder_imports.py -i <input_dir> -o <output_dir> -a <target_arch>'
83*8975f5c5SAndroid Build Coastguard Worker  parser = optparse.OptionParser(usage=usage)
84*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('-i', '--input', help='reorder chrome.exe in DIR',
85*8975f5c5SAndroid Build Coastguard Worker      metavar='DIR')
86*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('-o', '--output', help='write new chrome.exe to DIR',
87*8975f5c5SAndroid Build Coastguard Worker      metavar='DIR')
88*8975f5c5SAndroid Build Coastguard Worker  parser.add_option('-a', '--arch', help='architecture of build (optional)',
89*8975f5c5SAndroid Build Coastguard Worker      default='ia32')
90*8975f5c5SAndroid Build Coastguard Worker  opts, args = parser.parse_args()
91*8975f5c5SAndroid Build Coastguard Worker
92*8975f5c5SAndroid Build Coastguard Worker  if not opts.input or not opts.output:
93*8975f5c5SAndroid Build Coastguard Worker    parser.error('Please provide and input and output directory')
94*8975f5c5SAndroid Build Coastguard Worker  return reorder_imports(opts.input, opts.output, opts.arch)
95*8975f5c5SAndroid Build Coastguard Worker
96*8975f5c5SAndroid Build Coastguard Workerif __name__ == "__main__":
97*8975f5c5SAndroid Build Coastguard Worker  sys.exit(main(sys.argv[1:]))
98