Blame SOURCES/check-debug-symbols.py

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