Blame SOURCES/check-debug-symbols.py

74755f
#!/usr/bin/python2
74755f
74755f
"""
74755f
Check debug symbols are present in shared object and can identify
74755f
code.
74755f
74755f
It starts scanning from a directory and recursively scans all ELF
74755f
files found in it for various symbols to ensure all debuginfo is
74755f
present and nothing has been stripped.
74755f
74755f
Usage:
74755f
74755f
./check-debug-symbols /path/of/dir/to/scan/
74755f
74755f
74755f
Example:
74755f
74755f
./check-debug-symbols /usr/lib64
74755f
"""
74755f
74755f
# This technique was explained to me by Mark Wielaard (mjw).
74755f
74755f
import collections
74755f
import os
74755f
import re
74755f
import subprocess
74755f
import sys
74755f
74755f
ScanResult = collections.namedtuple('ScanResult',
74755f
                                    'file_name debug_info debug_abbrev file_symbols gnu_debuglink')
74755f
74755f
74755f
def scan_file(file):
74755f
    "Scan the provided file and return a ScanResult containing results of the scan."
74755f
74755f
    # Test for .debug_* sections in the shared object. This is the  main test.
74755f
    # Stripped objects will not contain these.
74755f
    readelf_S_result = subprocess.check_output(['eu-readelf', '-S', file])
74755f
    has_debug_info = any(line for line in readelf_S_result.split('\n') if '] .debug_info' in line)
74755f
74755f
    has_debug_abbrev = any(line for line in readelf_S_result.split('\n') if '] .debug_abbrev' in line)
74755f
74755f
    # Test FILE symbols. These will most likely be removed by anyting that
74755f
    # manipulates symbol tables because it's generally useless. So a nice test
74755f
    # that nothing has messed with symbols.
74755f
    def contains_file_symbols(line):
74755f
        parts = line.split()
74755f
        if len(parts) < 8:
74755f
            return False
74755f
        return \
74755f
            parts[2] == '0' and parts[3] == 'FILE' and parts[4] == 'LOCAL' and parts[5] == 'DEFAULT' and \
74755f
            parts[6] == 'ABS' and re.match(r'((.*/)?[-_a-zA-Z0-9]+\.(c|cc|cpp|cxx))?', parts[7])
74755f
74755f
    readelf_s_result = subprocess.check_output(["eu-readelf", '-s', file])
74755f
    has_file_symbols = any(line for line in readelf_s_result.split('\n') if contains_file_symbols(line))
74755f
74755f
    # Test that there are no .gnu_debuglink sections pointing to another
74755f
    # debuginfo file. There shouldn't be any debuginfo files, so the link makes
74755f
    # no sense either.
74755f
    has_gnu_debuglink = any(line for line in readelf_s_result.split('\n') if '] .gnu_debuglink' in line)
74755f
74755f
    return ScanResult(file, has_debug_info, has_debug_abbrev, has_file_symbols, has_gnu_debuglink)
74755f
74755f
def is_elf(file):
74755f
    result = subprocess.check_output(['file', file])
74755f
    return re.search('ELF 64-bit LSB (?:executable|shared object)', result)
74755f
74755f
def scan_file_if_sensible(file):
74755f
    if is_elf(file):
74755f
        # print(file)
74755f
        return scan_file(file)
74755f
    return None
74755f
74755f
def scan_dir(dir):
74755f
    results = []
74755f
    for root, _, files in os.walk(dir):
74755f
        for name in files:
74755f
            result = scan_file_if_sensible(os.path.join(root, name))
74755f
            if result:
74755f
                results.append(result)
74755f
    return results
74755f
74755f
def scan(file):
74755f
    file = os.path.abspath(file)
74755f
    if os.path.isdir(file):
74755f
        return scan_dir(file)
74755f
    elif os.path.isfile(file):
74755f
        return scan_file_if_sensible(file)
74755f
74755f
def is_bad_result(result):
74755f
    return not result.debug_info or not result.debug_abbrev or not result.file_symbols or result.gnu_debuglink
74755f
74755f
def print_scan_results(results, verbose):
74755f
    # print(results)
74755f
    for result in results:
74755f
        file_name = result.file_name
74755f
        found_issue = False
74755f
        if not result.debug_info:
74755f
            found_issue = True
74755f
            print('error: missing .debug_info section in ' + file_name)
74755f
        if not result.debug_abbrev:
74755f
            found_issue = True
74755f
            print('error: missing .debug_abbrev section in ' + file_name)
74755f
        if not result.file_symbols:
74755f
            found_issue = True
74755f
            print('error: missing FILE symbols in ' + file_name)
74755f
        if result.gnu_debuglink:
74755f
            found_issue = True
74755f
            print('error: unexpected .gnu_debuglink section in ' + file_name)
74755f
        if verbose and not found_issue:
74755f
            print('OK: ', file_name)
74755f
74755f
def main(args):
74755f
    verbose = False
74755f
    files = []
74755f
    for arg in args:
74755f
        if arg == '--verbose' or arg == '-v':
74755f
            verbose = True
74755f
        else:
74755f
            files.append(arg)
74755f
74755f
    results = []
74755f
    for file in files:
74755f
        results.extend(scan(file) or [])
74755f
74755f
    print_scan_results(results, verbose)
74755f
74755f
    if any(is_bad_result(result) for result in results):
74755f
        return 1
74755f
    return 0
74755f
74755f
74755f
if __name__ == '__main__':
74755f
    sys.exit(main(sys.argv[1:]))