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.
6 """Check that symbols are ordered into a binary as they appear in the orderfile.
12 import patch_orderfile
13 import symbol_extractor
16 _MAX_WARNINGS_TO_PRINT
= 200
19 def _CountMisorderedSymbols(symbols
, symbol_infos
):
20 """Count the number of misordered symbols, and log them.
23 symbols: ordered sequence of symbols from the orderfile
24 symbol_infos: ordered list of SymbolInfo from the binary
27 (misordered_pairs_count, matched_symbols_count, unmatched_symbols_count)
29 name_to_symbol_info
= symbol_extractor
.CreateNameToSymbolInfo(symbol_infos
)
30 matched_symbol_infos
= []
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
])
40 if missing_count
< _MAX_WARNINGS_TO_PRINT
:
41 logging
.warning('Symbol "%s" is in the orderfile, not in the binary' %
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
))
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
)))
57 previous_symbol_info
= symbol_info
58 return (misordered_count
, len(matched_symbol_infos
), missing_count
)
62 if len(sys
.argv
) != 4 and len(sys
.argv
) != 3:
63 logging
.error('Usage: check_orderfile.py binary orderfile [threshold]')
65 (binary_filename
, orderfile_filename
) = sys
.argv
[1:3]
67 if len(sys
.argv
) == 4:
68 threshold
= int(sys
.argv
[3])
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
74 (misordered_pairs_count
, _
, _
) = _CountMisorderedSymbols(
75 symbols
, symbol_infos
)
76 return misordered_pairs_count
> threshold
79 if __name__
== '__main__':
80 logging
.basicConfig(level
=logging
.INFO
)