OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 | |
3 # Copyright 2016 The Chromium Authors. All rights reserved. | |
4 # Use of this source code is governed by a BSD-style license that can be | |
5 # found in the LICENSE file. | |
6 | |
7 """ | |
8 Prints the contents of the __DATA,__mod_init_func section of a Mach-O image. | |
9 | |
10 Usage: | |
11 tools/mac/show_mod_init_func.py out/gn/Chromium\ Framework.unstripped | |
12 | |
13 This is meant to be used on a Mach-O executable. If a dSYM is present, use | |
14 dump-static-initializers.py instead. | |
15 """ | |
16 | |
17 import optparse | |
18 import subprocess | |
19 import sys | |
20 | |
21 | |
22 def ShowModuleInitializers(binary): | |
23 """Gathers the module initializers for |binary| and symbolizes the addresses. | |
24 """ | |
25 initializers = GetModuleInitializers(binary) | |
26 if not initializers: | |
27 # atos will do work even if there are no addresses, so bail early. | |
28 return | |
29 symbols = SymbolizeAddresses(binary, initializers) | |
30 | |
31 print binary | |
32 for initializer in zip(initializers, symbols): | |
33 print '%s @ %s' % initializer | |
34 | |
35 | |
36 def GetModuleInitializers(binary): | |
37 """Parses the __DATA,__mod_init_func segment of |binary| and returns a list | |
38 of string hexadecimal addresses of the module initializers. | |
39 """ | |
40 # The -v flag will display the addresses in a usable form (as opposed to | |
41 # just its on-disk little-endian byte representation). | |
42 otool = ['otool', '-v', '-s', '__DATA', '__mod_init_func', binary] | |
43 lines = subprocess.check_output(otool).strip().split('\n') | |
44 if len(lines) == 1: | |
Mark Mentovai
2016/07/19 17:31:15
I don’t think you need this. lines[2:] will be []
Robert Sesek
2016/07/19 18:02:22
You're right. I always forget that [2:] doesn't co
| |
45 # Just the "path/to/binary:" header means no module initializers. | |
46 return [] | |
47 else: | |
48 # Skip the first two header lines and then get the address of the | |
49 # initializer in the second column. The first address is the address | |
50 # of the initializer pointer. | |
51 # out/gn/Chromium Framework.unstripped: | |
52 # Contents of (__DATA,__mod_init_func) section | |
53 # 0x0000000008761498 0x000000000385d120 | |
54 return [line.split(' ')[1] for line in lines[2:]] | |
55 | |
56 | |
57 def SymbolizeAddresses(binary, addresses): | |
58 """Given a |binary| and a list of |addresses|, symbolizes them using atos. | |
59 """ | |
60 atos = ['atos', '-o', binary] + addresses | |
Mark Mentovai
2016/07/19 17:31:15
If there are a lot of addresses, you could run out
Robert Sesek
2016/07/19 18:02:22
There are only ever a few static initializers so I
| |
61 lines = subprocess.check_output(atos).strip().split('\n') | |
62 return lines | |
63 | |
64 | |
65 def Main(): | |
66 parser = optparse.OptionParser(usage='%prog filename') | |
67 opts, args = parser.parse_args() | |
68 if len(args) != 1: | |
69 parser.error('missing binary filename') | |
70 return 1 | |
71 | |
72 ShowModuleInitializers(args[0]) | |
73 return 0 | |
74 | |
75 if __name__ == '__main__': | |
76 sys.exit(Main()) | |
OLD | NEW |