Blame SOURCES/check-debug-symbols.py

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