Blame SOURCES/check-debug-symbols.py

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