OLD | NEW |
---|---|
(Empty) | |
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 with maintaining DLL import lists.""" | |
M-A Ruel
2013/03/26 12:59:14
"""Helps maintaining DLL import lists."""
but I'd
Sigurður Ásgeirsson
2013/03/26 15:35:35
The "how" is in the script's usage string.
| |
7 import ast | |
8 import optparse | |
9 import re | |
10 import sys | |
11 | |
12 | |
13 _EXPORT_RE = re.compile(""" | |
M-A Ruel
2013/03/26 12:59:14
It should be a r"""
otherwise you'll get caught b
Sigurður Ásgeirsson
2013/03/26 15:35:35
Done.
| |
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. | |
18 """, re.VERBOSE) | |
19 | |
20 | |
21 _USAGE = """\ | |
22 Usage: %prog [options] [master-file] | |
23 | |
24 This script filters a list of exports from a DLL, generated from something | |
25 like the following command line: | |
26 | |
27 C:\> dumpbin /exports dllname.dllname | |
28 | |
29 against a master list of imports built from e.g. | |
30 | |
31 C:\> dumpbin /exports importlibname.lib | |
32 | |
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 | |
35 import libraries. | |
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 | |
38 function. | |
39 """ | |
40 | |
41 def _ReadMasterFile(master_file): | |
42 # Slurp the master file. | |
M-A Ruel
2013/03/26 12:59:14
with open(master_file) as f:
master_exports = as
Sigurður Ásgeirsson
2013/03/26 15:35:35
I generally prefer readability to terseness, do yo
M-A Ruel
2013/03/26 15:44:37
This is intermediate python but I don't really min
| |
43 master_exports = ast.literal_eval(open(master_file).read()) | |
44 master_mapping = {} | |
45 for export in master_exports: | |
46 name = export.split('@')[0] | |
47 master_mapping[name] = export | |
48 | |
49 return master_mapping | |
50 | |
51 | |
52 def main(): | |
53 parser = optparse.OptionParser(usage=_USAGE) | |
54 parser.add_option('-r', '--reverse', | |
55 action='store_true', | |
56 help='Reverse the matching, e.g. return the functions ' | |
57 'in the master list that aren\'t in the input.') | |
58 | |
59 options, args = parser.parse_args() | |
60 if len(args) != 1: | |
61 parser.error('Must provide a master file.') | |
62 return 1 | |
63 | |
64 master_mapping = _ReadMasterFile(args[0]) | |
65 | |
66 found_exports = [] | |
67 for line in sys.stdin: | |
68 match = _EXPORT_RE.match(line) | |
69 if match: | |
70 export_name = master_mapping.get(match.group('name'), None) | |
71 if export_name: | |
72 found_exports.append(export_name) | |
73 | |
74 if options.reverse: | |
75 # Invert the found_exports list. | |
76 found_exports = list(set(master_mapping.values()) - set(found_exports)) | |
M-A Ruel
2013/03/26 12:59:14
not necessary to convert back into a list.
Sigurður Ásgeirsson
2013/03/26 15:35:35
Done.
| |
77 | |
78 # Sort the found exports for tidy output. | |
79 '\n'.join(sorted(found_exports)) | |
M-A Ruel
2013/03/26 12:59:14
Did you intent to print it?
Then return 0
Sigurður Ásgeirsson
2013/03/26 15:35:35
Done.
| |
80 | |
81 | |
82 if __name__ == '__main__': | |
83 sys.exit(main()) | |
OLD | NEW |