Blame SOURCES/check-debug-symbols.py

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