Blame SOURCES/check-debug-symbols.py

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