OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Copyright (c) 2008 The Chromium Authors. All rights reserved. | |
4 # 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 |
5 # found in the LICENSE file. | 4 # found in the LICENSE file. |
6 | 5 |
7 """This will retrieve a PDBs "fingerprint" from it's corresponding executable | 6 """This will retrieve a PDBs "fingerprint" from it's corresponding executable |
8 image (.dll or .exe). This is used when retrieving the PDB from the symbol | 7 image (.dll or .exe). This is used when retrieving the PDB from the symbol |
9 server. The .pdb (or cab compressed .pd_) is expected at a path like: | 8 server. The .pdb (or cab compressed .pd_) is expected at a path like: |
10 foo.pdb/FINGERPRINT/foo.pdb | 9 foo.pdb/FINGERPRINT/foo.pdb |
11 | 10 |
12 We can retrieve the same information from the .PDB file itself, but this file | 11 We can retrieve the same information from the .PDB file itself, but this file |
13 format is much more difficult and undocumented. Instead, we can look at the | 12 format is much more difficult and undocumented. Instead, we can look at the |
14 DLL's reference to the PDB, and use that to retrieve the information.""" | 13 DLL's reference to the PDB, and use that to retrieve the information.""" |
15 | 14 |
16 import sys | 15 import sys |
17 import pefile | 16 import pefile |
18 | 17 |
| 18 |
19 __CV_INFO_PDB70_format__ = ('CV_INFO_PDB70', | 19 __CV_INFO_PDB70_format__ = ('CV_INFO_PDB70', |
20 ('4s,CvSignature', '16s,Signature', 'L,Age')) | 20 ('4s,CvSignature', '16s,Signature', 'L,Age')) |
21 | 21 |
22 __GUID_format__ = ('GUID', | 22 __GUID_format__ = ('GUID', |
23 ('L,Data1', 'H,Data2', 'H,Data3', '8s,Data4')) | 23 ('L,Data1', 'H,Data2', 'H,Data3', '8s,Data4')) |
24 | 24 |
| 25 |
25 def GetPDBInfoFromImg(filename): | 26 def GetPDBInfoFromImg(filename): |
26 """Returns the PDB fingerprint and the pdb filename given an image file""" | 27 """Returns the PDB fingerprint and the pdb filename given an image file""" |
27 | 28 |
28 pe = pefile.PE(filename) | 29 pe = pefile.PE(filename) |
29 | 30 |
30 for dbg in pe.DIRECTORY_ENTRY_DEBUG: | 31 for dbg in pe.DIRECTORY_ENTRY_DEBUG: |
31 if dbg.struct.Type == 2: # IMAGE_DEBUG_TYPE_CODEVIEW | 32 if dbg.struct.Type == 2: # IMAGE_DEBUG_TYPE_CODEVIEW |
32 off = dbg.struct.AddressOfRawData | 33 off = dbg.struct.AddressOfRawData |
33 size = dbg.struct.SizeOfData | 34 size = dbg.struct.SizeOfData |
34 data = pe.get_memory_mapped_image()[off:off+size] | 35 data = pe.get_memory_mapped_image()[off:off+size] |
35 | 36 |
36 cv = pefile.Structure(__CV_INFO_PDB70_format__) | 37 cv = pefile.Structure(__CV_INFO_PDB70_format__) |
37 cv.__unpack__(data) | 38 cv.__unpack__(data) |
38 cv.PdbFileName = data[cv.sizeof():] | 39 cv.PdbFileName = data[cv.sizeof():] |
39 guid = pefile.Structure(__GUID_format__) | 40 guid = pefile.Structure(__GUID_format__) |
40 guid.__unpack__(cv.Signature) | 41 guid.__unpack__(cv.Signature) |
41 guid.Data4_0 = ''.join("%02X" % ord(x) for x in guid.Data4[0:2]) | 42 guid.Data4_0 = ''.join("%02X" % ord(x) for x in guid.Data4[0:2]) |
42 guid.Data4_1 = ''.join("%02X" % ord(x) for x in guid.Data4[2:]) | 43 guid.Data4_1 = ''.join("%02X" % ord(x) for x in guid.Data4[2:]) |
43 | 44 |
44 return ("%08X%04X%04X%s%s%d" % ( | 45 return ("%08X%04X%04X%s%s%d" % ( |
45 guid.Data1, guid.Data2, guid.Data3, | 46 guid.Data1, guid.Data2, guid.Data3, |
46 guid.Data4_0, guid.Data4_1, cv.Age), | 47 guid.Data4_0, guid.Data4_1, cv.Age), |
47 cv.PdbFileName.split('\x00', 1)[0]) | 48 cv.PdbFileName.split('\x00', 1)[0]) |
48 | 49 |
49 break | 50 break |
50 | 51 |
51 if __name__ == '__main__': | 52 |
| 53 def main(): |
52 if len(sys.argv) != 2: | 54 if len(sys.argv) != 2: |
53 print "usage: file.dll" | 55 print "usage: file.dll" |
54 sys.exit(1) | 56 return 1 |
55 | 57 |
56 (fingerprint, file) = GetPDBInfoFromImg(sys.argv[1]) | 58 (fingerprint, filename) = GetPDBInfoFromImg(sys.argv[1]) |
57 print "%s %s" % (fingerprint, file) | 59 print "%s %s" % (fingerprint, filename) |
| 60 return 0 |
| 61 |
| 62 |
| 63 if __name__ == '__main__': |
| 64 sys.exit(main()) |
OLD | NEW |