OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 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 |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """Check that symbols are ordered into a binary as they appear in the orderfile. |
| 7 """ |
| 8 |
| 9 import logging |
| 10 import sys |
| 11 |
| 12 import patch_orderfile |
| 13 import symbol_extractor |
| 14 |
| 15 |
| 16 _MAX_WARNINGS_TO_PRINT = 200 |
| 17 |
| 18 |
| 19 def _CountMisorderedSymbols(symbols, symbol_infos): |
| 20 """Count the number of misordered symbols, and log them. |
| 21 |
| 22 Args: |
| 23 symbols: ordered sequence of symbols from the orderfile |
| 24 symbol_infos: ordered list of SymbolInfo from the binary |
| 25 |
| 26 Returns: |
| 27 (misordered_pairs_count, matched_symbols_count, unmatched_symbols_count) |
| 28 """ |
| 29 name_to_symbol_info = symbol_extractor.CreateNameToSymbolInfo(symbol_infos) |
| 30 matched_symbol_infos = [] |
| 31 missing_count = 0 |
| 32 misordered_count = 0 |
| 33 |
| 34 # Find the SymbolInfo matching the orderfile symbols in the binary. |
| 35 for symbol in symbols: |
| 36 if symbol in name_to_symbol_info: |
| 37 matched_symbol_infos.append(name_to_symbol_info[symbol]) |
| 38 else: |
| 39 missing_count += 1 |
| 40 if missing_count < _MAX_WARNINGS_TO_PRINT: |
| 41 logging.warning('Symbol "%s" is in the orderfile, not in the binary' % |
| 42 symbol) |
| 43 logging.info('%d matched symbols, %d un-matched (Only the first %d unmatched' |
| 44 ' symbols are shown)' % ( |
| 45 len(matched_symbol_infos), missing_count, |
| 46 _MAX_WARNINGS_TO_PRINT)) |
| 47 |
| 48 # In the order of the orderfile, find all the symbols that are at an offset |
| 49 # smaller than their immediate predecessor, and record the pair. |
| 50 previous_symbol_info = symbol_extractor.SymbolInfo( |
| 51 name='', offset=-1, size=0, section='') |
| 52 for symbol_info in matched_symbol_infos: |
| 53 if symbol_info.offset < previous_symbol_info.offset: |
| 54 logging.warning("Misordered pair: %s - %s" % ( |
| 55 str(previous_symbol_info), str(symbol_info))) |
| 56 misordered_count += 1 |
| 57 previous_symbol_info = symbol_info |
| 58 return (misordered_count, len(matched_symbol_infos), missing_count) |
| 59 |
| 60 |
| 61 def main(): |
| 62 if len(sys.argv) != 4 and len(sys.argv) != 3: |
| 63 logging.error('Usage: check_orderfile.py binary orderfile [threshold]') |
| 64 return 1 |
| 65 (binary_filename, orderfile_filename) = sys.argv[1:3] |
| 66 threshold = 0 |
| 67 if len(sys.argv) == 4: |
| 68 threshold = int(sys.argv[3]) |
| 69 |
| 70 symbols = patch_orderfile.GetSymbolsFromOrderfile(orderfile_filename) |
| 71 symbol_infos = symbol_extractor.SymbolInfosFromBinary(binary_filename) |
| 72 # Missing symbols is not an error since some of them can be eliminated through |
| 73 # inlining. |
| 74 (misordered_pairs_count, _, _) = _CountMisorderedSymbols( |
| 75 symbols, symbol_infos) |
| 76 return misordered_pairs_count > threshold |
| 77 |
| 78 |
| 79 if __name__ == '__main__': |
| 80 logging.basicConfig(level=logging.INFO) |
| 81 sys.exit(main()) |
OLD | NEW |