| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Script to verify a Portable Executable's dependencies. | 6 """Script to verify a Portable Executable's dependencies. |
| 7 | 7 |
| 8 Analyzes the input portable executable (a DLL or an EXE for example), extracts | 8 Analyzes the input portable executable (a DLL or an EXE for example), extracts |
| 9 its imports and confirms that its dependencies haven't changed. This is for | 9 its imports and confirms that its dependencies haven't changed. This is for |
| 10 regression testing. | 10 regression testing. |
| 11 | 11 |
| 12 Returns 0 if the list matches. | 12 Returns 0 if the list matches. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 33 def __str__(self): | 33 def __str__(self): |
| 34 return self.message | 34 return self.message |
| 35 | 35 |
| 36 | 36 |
| 37 def RunSystemCommand(cmd): | 37 def RunSystemCommand(cmd): |
| 38 try: | 38 try: |
| 39 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] | 39 return subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] |
| 40 except: | 40 except: |
| 41 raise Error("Failed to execute: " + cmd) | 41 raise Error("Failed to execute: " + cmd) |
| 42 | 42 |
| 43 |
| 43 def RunDumpbin(binary_file): | 44 def RunDumpbin(binary_file): |
| 44 """Runs dumpbin and parses its output. | 45 """Runs dumpbin and parses its output. |
| 45 | 46 |
| 46 Args: binary_file: the binary to analyze | 47 Args: binary_file: the binary to analyze |
| 47 Returns: a tuple of the dependencies and the delay-load dependencies | 48 Returns: a tuple of the dependencies and the delay-load dependencies |
| 48 | 49 |
| 49 The output of dumpbin that we will be parsing looks like this: | 50 The output of dumpbin that we will be parsing looks like this: |
| 50 -- | 51 -- |
| 51 <blah blah> | 52 <blah blah> |
| 52 | 53 |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 expected_dependents, | 190 expected_dependents, |
| 190 list_file) | 191 list_file) |
| 191 delayed_result = Diff(pe_name, | 192 delayed_result = Diff(pe_name, |
| 192 "delay loaded dll", | 193 "delay loaded dll", |
| 193 delay_loaded, | 194 delay_loaded, |
| 194 expected_delay_loaded, | 195 expected_delay_loaded, |
| 195 list_file) | 196 list_file) |
| 196 return max(deps_result, delayed_result) | 197 return max(deps_result, delayed_result) |
| 197 | 198 |
| 198 | 199 |
| 199 def main(options, args): | 200 def main(): |
| 200 # PE means portable executable. It's any .DLL, .EXE, .SYS, .AX, etc. | 201 # PE means portable executable. It's any .DLL, .EXE, .SYS, .AX, etc. |
| 202 usage = "usage: %prog [options] input output" |
| 203 option_parser = optparse.OptionParser(usage=usage) |
| 204 option_parser.add_option("-d", |
| 205 "--debug", |
| 206 dest="debug", |
| 207 action="store_true", |
| 208 default=False, |
| 209 help="Display debugging information") |
| 210 options, args = option_parser.parse_args() |
| 211 if len(args) != 2: |
| 212 option_parser.error("Incorrect number of arguments") |
| 201 pe_name = args[0] | 213 pe_name = args[0] |
| 202 deps_file = args[1] | 214 deps_file = args[1] |
| 203 dependents, delay_loaded = RunDumpbin(pe_name) | 215 dependents, delay_loaded = RunDumpbin(pe_name) |
| 204 if options.debug: | 216 if options.debug: |
| 205 print "Dependents:" | 217 print "Dependents:" |
| 206 print "\n".join(dependents) | 218 print "\n".join(dependents) |
| 207 print "Delayloaded:" | 219 print "Delayloaded:" |
| 208 print "\n".join(delay_loaded) | 220 print "\n".join(delay_loaded) |
| 209 return VerifyDependents(pe_name, dependents, delay_loaded, deps_file, | 221 return VerifyDependents(pe_name, dependents, delay_loaded, deps_file, |
| 210 options.debug) | 222 options.debug) |
| 211 | 223 |
| 212 | 224 |
| 213 if '__main__' == __name__: | 225 if '__main__' == __name__: |
| 214 usage = "usage: %prog [options] input output" | 226 sys.exit(main()) |
| 215 option_parser = optparse.OptionParser(usage = usage) | |
| 216 option_parser.add_option("-d", | |
| 217 "--debug", | |
| 218 dest="debug", | |
| 219 action="store_true", | |
| 220 default=False, | |
| 221 help="Display debugging information") | |
| 222 options, args = option_parser.parse_args() | |
| 223 if len(args) != 2: | |
| 224 option_parser.error("Incorrect number of arguments") | |
| 225 sys.exit(main(options, args)) | |
| OLD | NEW |