OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 """Dump functions called by static intializers in a Linux Release binary. | 6 """Dump functions called by static intializers in a Linux Release binary. |
7 | 7 |
8 Usage example: | 8 Usage example: |
9 tools/linux/dump-static-intializers.py out/Release/chrome | 9 tools/linux/dump-static-intializers.py out/Release/chrome |
10 | 10 |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 stdout=subprocess.PIPE) | 86 stdout=subprocess.PIPE) |
87 candidate = filename | 87 candidate = filename |
88 for line in gitgrep.stdout: | 88 for line in gitgrep.stdout: |
89 if candidate != filename: # More than one candidate; return bare filename. | 89 if candidate != filename: # More than one candidate; return bare filename. |
90 return filename | 90 return filename |
91 candidate = line.strip() | 91 candidate = line.strip() |
92 return candidate | 92 return candidate |
93 | 93 |
94 # Regex matching nm output for the symbols we're interested in. | 94 # Regex matching nm output for the symbols we're interested in. |
95 # Example line: | 95 # Example line: |
96 # 0000000001919920 0000000000000008 b _ZN12_GLOBAL__N_119g_nine_box_prelightE | 96 # 0000000001919920 0000000000000008 b _ZN12_GLOBAL__N_119g_nine_box_prelightE |
Lei Zhang
2012/01/31 20:57:46
can you fix this incorrect comment while you're at
Tyler Breisacher (Chromium)
2012/01/31 22:01:07
Done.
| |
97 nm_re = re.compile(r'(\S+) (\S+) t _GLOBAL__I_(.*)') | 97 nm_re = re.compile(r'(\S+) (\S+) t _GLOBAL__I_(.*)') |
98 def ParseNm(binary): | 98 def ParseNm(binary): |
99 """Given a binary, yield static initializers as (start, size, file) pairs.""" | 99 """Given a binary, yield static initializers as (file, start, size) tuples.""" |
100 | 100 |
101 nm = subprocess.Popen(['nm', '-S', binary], stdout=subprocess.PIPE) | 101 nm = subprocess.Popen(['nm', '-S', binary], stdout=subprocess.PIPE) |
102 for line in nm.stdout: | 102 for line in nm.stdout: |
103 match = nm_re.match(line) | 103 match = nm_re.match(line) |
104 if match: | 104 if match: |
105 addr, size, filename = match.groups() | 105 addr, size, filename = match.groups() |
106 yield int(addr, 16), int(size, 16), filename | 106 yield filename, int(addr, 16), int(size, 16) |
107 | |
108 | 107 |
109 # Regex matching objdump output for the symbols we're interested in. | 108 # Regex matching objdump output for the symbols we're interested in. |
110 # Example line: | 109 # Example line: |
111 # 12354ab: (disassembly, including <FunctionReference>) | 110 # 12354ab: (disassembly, including <FunctionReference>) |
112 disassembly_re = re.compile(r'^\s+[0-9a-f]+:.*<(\S+)>') | 111 disassembly_re = re.compile(r'^\s+[0-9a-f]+:.*<(\S+)>') |
113 def ExtractSymbolReferences(binary, start, end): | 112 def ExtractSymbolReferences(binary, start, end): |
114 """Given a span of addresses, yields symbol references from disassembly.""" | 113 """Given a span of addresses, returns symbol references from disassembly.""" |
115 cmd = ['objdump', binary, '--disassemble', | 114 cmd = ['objdump', binary, '--disassemble', |
116 '--start-address=0x%x' % start, '--stop-address=0x%x' % end] | 115 '--start-address=0x%x' % start, '--stop-address=0x%x' % end] |
117 objdump = subprocess.Popen(cmd, stdout=subprocess.PIPE) | 116 objdump = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
118 | 117 |
119 refs = set() | 118 refs = set() |
120 for line in objdump.stdout: | 119 for line in objdump.stdout: |
121 if '__static_initialization_and_destruction' in line: | 120 if '__static_initialization_and_destruction' in line: |
122 raise RuntimeError, ('code mentions ' | 121 raise RuntimeError, ('code mentions ' |
123 '__static_initialization_and_destruction; ' | 122 '__static_initialization_and_destruction; ' |
124 'did you accidentally run this on a Debug binary?') | 123 'did you accidentally run this on a Debug binary?') |
125 match = disassembly_re.search(line) | 124 match = disassembly_re.search(line) |
126 if match: | 125 if match: |
127 (ref,) = match.groups() | 126 (ref,) = match.groups() |
128 if ref.startswith('.LC') or ref.startswith('_DYNAMIC'): | 127 if ref.startswith('.LC') or ref.startswith('_DYNAMIC'): |
129 # Ignore these, they are uninformative. | 128 # Ignore these, they are uninformative. |
130 continue | 129 continue |
131 if ref.startswith('_GLOBAL__I_'): | 130 if ref.startswith('_GLOBAL__I_'): |
132 # Probably a relative jump within this function. | 131 # Probably a relative jump within this function. |
133 continue | 132 continue |
134 refs.add(ref) | 133 refs.add(ref) |
135 continue | |
136 | 134 |
137 for ref in sorted(refs): | 135 return sorted(refs) |
138 yield ref | |
139 | |
140 | 136 |
141 def main(): | 137 def main(): |
142 parser = optparse.OptionParser(usage='%prog filename') | 138 parser = optparse.OptionParser(usage='%prog [option] filename') |
143 parser.add_option('-i', '--instances', dest='calculate_instances', | 139 parser.add_option('-d', '--diffable', dest='diffable', |
144 action='store_true', default=False, | 140 action='store_true', default=False, |
145 help='Only print out the number of static initializers') | 141 help='Prints the filename on each line, for more easily ' |
142 'diff-able output.') | |
146 opts, args = parser.parse_args() | 143 opts, args = parser.parse_args() |
147 if len(args) != 1: | 144 if len(args) != 1: |
148 parser.error('missing filename argument') | 145 parser.error('missing filename argument') |
149 return 1 | 146 return 1 |
150 binary = args[0] | 147 binary = args[0] |
151 | 148 |
152 demangler = Demangler() | 149 demangler = Demangler() |
153 static_initializers_count = 0 | 150 file_count = 0 |
154 for addr, size, filename in ParseNm(binary): | 151 initializer_count = 0 |
152 | |
153 files = ParseNm(binary) | |
154 if (opts.diffable): | |
Evan Martin
2012/01/31 19:49:19
No parens around this
Tyler Breisacher (Chromium)
2012/01/31 22:01:07
Done.
| |
155 files = sorted(files) | |
156 for filename, addr, size in files: | |
155 if size == 2: | 157 if size == 2: |
156 # gcc generates a two-byte 'repz retq' initializer when there is nothing | 158 # gcc generates a two-byte 'repz retq' initializer when there is nothing |
157 # to do. jyasskin tells me this is fixed in gcc 4.6. | 159 # to do. jyasskin tells me this is fixed in gcc 4.6. |
158 # Two bytes is too small to do anything, so just ignore it. | |
159 continue | 160 continue |
160 | 161 |
161 if (opts.calculate_instances): | 162 file_count += 1 |
162 static_initializers_count += 1 | |
163 continue | |
164 | 163 |
165 ref_output = '' | 164 ref_output = [] |
166 qualified_filename = QualifyFilenameAsProto(filename) | 165 qualified_filename = QualifyFilenameAsProto(filename) |
167 for ref in ExtractSymbolReferences(binary, addr, addr+size): | 166 for ref in ExtractSymbolReferences(binary, addr, addr+size): |
167 initializer_count += 1 | |
168 | |
168 ref = demangler.Demangle(ref) | 169 ref = demangler.Demangle(ref) |
169 if qualified_filename == filename: | 170 if qualified_filename == filename: |
170 qualified_filename = QualifyFilename(filename, ref) | 171 qualified_filename = QualifyFilename(filename, ref) |
171 if ref in NOTES: | 172 if ref in NOTES: |
172 ref_output = ref_output + ' %s [%s]\n' % (ref, NOTES[ref]) | 173 ref_output.append(' %s [%s]' % (ref, NOTES[ref])) |
173 else: | 174 else: |
174 ref_output = ref_output + ' ' + ref + '\n' | 175 ref_output.append(' ' + ref) |
175 print '%s (initializer offset 0x%x size 0x%x)' % (qualified_filename, | |
176 addr, size) | |
177 print ref_output | |
178 | 176 |
179 if opts.calculate_instances: | 177 if opts.diffable: |
180 print static_initializers_count | 178 print '\n'.join(qualified_filename + r for r in ref_output) |
179 else: | |
180 print '%s (initializer offset 0x%x size 0x%x)' % (qualified_filename, | |
181 addr, size) | |
182 print '\n'.join(ref_output) + '\n' | |
183 | |
184 print 'Found %d static initializers in %d files.' % (initializer_count, | |
185 file_count) | |
186 | |
181 return 0 | 187 return 0 |
182 | 188 |
183 | |
184 if '__main__' == __name__: | 189 if '__main__' == __name__: |
185 sys.exit(main()) | 190 sys.exit(main()) |
OLD | NEW |