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 |
| 45 # Skip the first two header lines and then get the address of the |
| 46 # initializer in the second column. The first address is the address |
| 47 # of the initializer pointer. |
| 48 # out/gn/Chromium Framework.unstripped: |
| 49 # Contents of (__DATA,__mod_init_func) section |
| 50 # 0x0000000008761498 0x000000000385d120 |
| 51 return [line.split(' ')[1] for line in lines[2:]] |
| 52 |
| 53 |
| 54 def SymbolizeAddresses(binary, addresses): |
| 55 """Given a |binary| and a list of |addresses|, symbolizes them using atos. |
| 56 """ |
| 57 atos = ['atos', '-o', binary] + addresses |
| 58 lines = subprocess.check_output(atos).strip().split('\n') |
| 59 return lines |
| 60 |
| 61 |
| 62 def Main(): |
| 63 parser = optparse.OptionParser(usage='%prog filename') |
| 64 opts, args = parser.parse_args() |
| 65 if len(args) != 1: |
| 66 parser.error('missing binary filename') |
| 67 return 1 |
| 68 |
| 69 ShowModuleInitializers(args[0]) |
| 70 return 0 |
| 71 |
| 72 if __name__ == '__main__': |
| 73 sys.exit(Main()) |
OLD | NEW |