Blame SOURCES/check-debug-symbols.py

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