m10k / rpms / dotnet3.0

Forked from rpms/dotnet3.0 4 years ago
Clone

Blame SOURCES/check-debug-symbols.py

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