| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2009 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 """Makes sure that all EXE and DLL files in the provided directory were built | |
| 7 correctly. | |
| 8 | |
| 9 Currently this tool will check that binaries were built with /NXCOMPAT and | |
| 10 /DYNAMICBASE set. | |
| 11 """ | |
| 12 | |
| 13 import os | |
| 14 import optparse | |
| 15 import sys | |
| 16 | |
| 17 import pefile | |
| 18 | |
| 19 PE_FILE_EXTENSIONS = ['.exe', '.dll'] | |
| 20 DYNAMICBASE_FLAG = 0x0040 | |
| 21 NXCOMPAT_FLAG = 0x0100 | |
| 22 | |
| 23 def IsPEFile(path): | |
| 24 return (os.path.isfile(path) and | |
| 25 os.path.splitext(path)[1].lower() in PE_FILE_EXTENSIONS) | |
| 26 | |
| 27 def main(options, args): | |
| 28 directory = args[0] | |
| 29 success = True | |
| 30 | |
| 31 for file in os.listdir(directory): | |
| 32 path = os.path.abspath(os.path.join(directory, file)) | |
| 33 if not IsPEFile(path): | |
| 34 continue | |
| 35 pe = pefile.PE(path, fast_load=True) | |
| 36 | |
| 37 # Check for /DYNAMICBASE. | |
| 38 if pe.OPTIONAL_HEADER.DllCharacteristics & DYNAMICBASE_FLAG: | |
| 39 if options.verbose: | |
| 40 print "Checking %s for /DYNAMICBASE... PASS" % path | |
| 41 else: | |
| 42 success = False | |
| 43 print "Checking %s for /DYNAMICBASE... FAIL" % path | |
| 44 | |
| 45 # Check for /NXCOMPAT. | |
| 46 if pe.OPTIONAL_HEADER.DllCharacteristics & NXCOMPAT_FLAG: | |
| 47 if options.verbose: | |
| 48 print "Checking %s for /NXCOMPAT... PASS" % path | |
| 49 else: | |
| 50 success = False | |
| 51 print "Checking %s for /NXCOMPAT... FAIL" % path | |
| 52 | |
| 53 if not success: | |
| 54 sys.exit(1) | |
| 55 | |
| 56 if __name__ == '__main__': | |
| 57 usage = "Usage: %prog [options] DIRECTORY" | |
| 58 option_parser = optparse.OptionParser(usage=usage) | |
| 59 option_parser.add_option("-v", "--verbose", action="store_true", | |
| 60 default=False, help="Print debug logging") | |
| 61 options, args = option_parser.parse_args() | |
| 62 if not args: | |
| 63 option_parser.print_help() | |
| 64 sys.exit(0) | |
| 65 main(options, args) | |
| OLD | NEW |