Blame SOURCES/check-debug-symbols.py

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