Blame SOURCES/check-debug-symbols.py

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