Blame SOURCES/check-debug-symbols.py

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