Blame SOURCES/check-debug-symbols.py

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