Blame SOURCES/check-debug-symbols.py

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