Blame SOURCES/check-debug-symbols.py

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