Blame SOURCES/check-debug-symbols.py

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