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 """Help maintaining DLL import lists."""
13 _EXPORT_RE = re.compile(r"""
14 ^\s*(?P<ordinal>[0-9]+) # The ordinal field.
15 \s+(?P<hint>[0-9A-F]+) # The hint field.
16 \s(?P<rva>........) # The RVA field.
17 \s+(?P<name>[^ ]+) # And finally the name we're really after.
22 Usage: %prog [options] [master-file]
24 This script filters a list of exports from a DLL, generated from something
25 like the following command line:
27 C:\> dumpbin /exports user32.dll
29 against a master list of imports built from e.g.
31 C:\> dumpbin /exports user32.lib
33 The point of this is to trim non-public exports from the list, and to
34 normalize the names to their stdcall-mangled form for the generation of
36 Note that the export names from the latter incanatation are stdcall-mangled,
37 e.g. they are suffixed with "@" and the number of argument bytes to the
41 def _ReadMasterFile(master_file):
42 # Slurp the master file.
43 with open(master_file) as f:
44 master_exports = ast.literal_eval(f.read())
47 for export in master_exports:
48 name = export.split('@')[0]
49 master_mapping[name] = export
55 parser = optparse.OptionParser(usage=_USAGE)
56 parser.add_option('-r', '--reverse',
58 help='Reverse the matching, e.g. return the functions '
59 'in the master list that aren\'t in the input.')
61 options, args = parser.parse_args()
63 parser.error('Must provide a master file.')
65 master_mapping = _ReadMasterFile(args[0])
68 for line in sys.stdin:
69 match = _EXPORT_RE.match(line)
71 export_name = master_mapping.get(match.group('name'), None)
73 found_exports.append(export_name)
76 # Invert the found_exports list.
77 found_exports = set(master_mapping.values()) - set(found_exports)
79 # Sort the found exports for tidy output.
80 print '\n'.join(sorted(found_exports))
84 if __name__ == '__main__':