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 """Help maintaining DLL import lists.""" 7 import ast 8 import optparse 9 import re 10 import sys 11 12 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. 18 """, re.VERBOSE) 19 20 21 _USAGE = r"""\ 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 user32.dll 28 29 against a master list of imports built from e.g. 30 31 C:\> dumpbin /exports user32.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. 43 with open(master_file) as f: 44 master_exports = ast.literal_eval(f.read()) 45 46 master_mapping = {} 47 for export in master_exports: 48 name = export.split('@')[0] 49 master_mapping[name] = export 50 51 return master_mapping 52 53 54 def main(): 55 parser = optparse.OptionParser(usage=_USAGE) 56 parser.add_option('-r', '--reverse', 57 action='store_true', 58 help='Reverse the matching, e.g. return the functions ' 59 'in the master list that aren\'t in the input.') 60 61 options, args = parser.parse_args() 62 if len(args) != 1: 63 parser.error('Must provide a master file.') 64 65 master_mapping = _ReadMasterFile(args[0]) 66 67 found_exports = [] 68 for line in sys.stdin: 69 match = _EXPORT_RE.match(line) 70 if match: 71 export_name = master_mapping.get(match.group('name'), None) 72 if export_name: 73 found_exports.append(export_name) 74 75 if options.reverse: 76 # Invert the found_exports list. 77 found_exports = set(master_mapping.values()) - set(found_exports) 78 79 # Sort the found exports for tidy output. 80 print '\n'.join(sorted(found_exports)) 81 return 0 82 83 84 if __name__ == '__main__': 85 sys.exit(main()) 86