Blame check-debug-symbols.py

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