Blame SOURCES/check-debug-symbols.py

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