Blame SOURCES/check-debug-symbols.py

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