Blame SOURCES/check-debug-symbols.py

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