Blame SOURCES/check-debug-symbols.py

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