OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | 2 # Copyright 2015 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 """Utilities to get and manipulate symbols from a binary.""" | 6 """Utilities to get and manipulate symbols from a binary.""" |
7 | 7 |
8 import collections | 8 import collections |
9 import os | 9 import os |
10 import re | 10 import re |
11 import subprocess | 11 import subprocess |
12 import sys | 12 import sys |
13 | 13 |
14 sys.path.insert( | 14 sys.path.insert( |
15 0, os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | 15 0, os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
16 'third_party', 'android_platform', 'development', | 16 'third_party', 'android_platform', 'development', |
17 'scripts')) | 17 'scripts')) |
18 import symbol | 18 import symbol |
19 | 19 |
20 | 20 |
21 # TODO(lizeb): Change symbol.ARCH to the proper value when "arm" is no longer | |
22 # the only possible value. | |
23 _OBJDUMP_BINARY = symbol.ToolPath('objdump') | |
24 | |
25 | |
26 SymbolInfo = collections.namedtuple('SymbolInfo', ('name', 'offset', 'size', | 21 SymbolInfo = collections.namedtuple('SymbolInfo', ('name', 'offset', 'size', |
27 'section')) | 22 'section')) |
28 | 23 |
| 24 def SetArchitecture(arch): |
| 25 """Set the architecture for binaries to be symbolized.""" |
| 26 symbol.ARCH = arch |
| 27 |
29 | 28 |
30 def _FromObjdumpLine(line): | 29 def _FromObjdumpLine(line): |
31 """Create a SymbolInfo by parsing a properly formatted objdump output line. | 30 """Create a SymbolInfo by parsing a properly formatted objdump output line. |
32 | 31 |
33 Args: | 32 Args: |
34 line: line from objdump | 33 line: line from objdump |
35 | 34 |
36 Returns: | 35 Returns: |
37 An instance of SymbolInfo if the line represents a symbol, None otherwise. | 36 An instance of SymbolInfo if the line represents a symbol, None otherwise. |
38 """ | 37 """ |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 73 |
75 def SymbolInfosFromBinary(binary_filename): | 74 def SymbolInfosFromBinary(binary_filename): |
76 """Runs objdump to get all the symbols from a binary. | 75 """Runs objdump to get all the symbols from a binary. |
77 | 76 |
78 Args: | 77 Args: |
79 binary_filename: path to the binary. | 78 binary_filename: path to the binary. |
80 | 79 |
81 Returns: | 80 Returns: |
82 A list of SymbolInfo from the binary. | 81 A list of SymbolInfo from the binary. |
83 """ | 82 """ |
84 command = (_OBJDUMP_BINARY, '-t', '-w', binary_filename) | 83 command = (symbol.ToolPath('objdump'), '-t', '-w', binary_filename) |
85 p = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE) | 84 p = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE) |
86 try: | 85 try: |
87 result = _SymbolInfosFromStream(p.stdout) | 86 result = _SymbolInfosFromStream(p.stdout) |
88 return result | 87 return result |
89 finally: | 88 finally: |
90 p.wait() | 89 p.wait() |
91 | 90 |
92 | 91 |
93 def GroupSymbolInfosByOffset(symbol_infos): | 92 def GroupSymbolInfosByOffset(symbol_infos): |
94 """Create a dict {offset: [symbol_info1, ...], ...}. | 93 """Create a dict {offset: [symbol_info1, ...], ...}. |
(...skipping 16 matching lines...) Expand all Loading... |
111 def CreateNameToSymbolInfo(symbol_infos): | 110 def CreateNameToSymbolInfo(symbol_infos): |
112 """Create a dict {name: symbol_info, ...}. | 111 """Create a dict {name: symbol_info, ...}. |
113 | 112 |
114 Args: | 113 Args: |
115 symbol_infos: iterable of SymbolInfo instances | 114 symbol_infos: iterable of SymbolInfo instances |
116 | 115 |
117 Returns: | 116 Returns: |
118 a dict {name: symbol_info, ...} | 117 a dict {name: symbol_info, ...} |
119 """ | 118 """ |
120 return {symbol_info.name: symbol_info for symbol_info in symbol_infos} | 119 return {symbol_info.name: symbol_info for symbol_info in symbol_infos} |
OLD | NEW |