Blame SOURCES/check-debug-symbols.py

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