Blame SOURCES/check-debug-symbols.py

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