Pablo Greco ceb317
#!/usr/bin/python3
Pablo Greco ceb317
#
Pablo Greco ceb317
# check-kabi - Red Hat kABI reference checking tool
Pablo Greco ceb317
#
Pablo Greco ceb317
# We use this script to check against reference Module.kabi files.
Pablo Greco ceb317
#
Pablo Greco ceb317
# Author: Jon Masters <jcm@redhat.com>
Pablo Greco ceb317
# Copyright (C) 2007-2009 Red Hat, Inc.
Pablo Greco ceb317
#
Pablo Greco ceb317
# This software may be freely redistributed under the terms of the GNU
Pablo Greco ceb317
# General Public License (GPL).
Pablo Greco ceb317
Pablo Greco ceb317
# Changelog:
Pablo Greco ceb317
#
Pablo Greco ceb317
# 2018/06/01 - Update for python3 by Petr Oros.
Pablo Greco ceb317
# 2009/08/15 - Updated for use in RHEL6.
Pablo Greco ceb317
# 2007/06/13 - Initial rewrite in python by Jon Masters.
Pablo Greco ceb317
Pablo Greco ceb317
__author__ = "Jon Masters <jcm@redhat.com>"
Pablo Greco ceb317
__version__ = "2.0"
Pablo Greco ceb317
__date__ = "2009/08/15"
Pablo Greco ceb317
__copyright__ = "Copyright (C) 2007-2009 Red Hat, Inc"
Pablo Greco ceb317
__license__ = "GPL"
Pablo Greco ceb317
Pablo Greco ceb317
import getopt
Pablo Greco ceb317
import string
Pablo Greco ceb317
import sys
Pablo Greco ceb317
Pablo Greco ceb317
true = 1
Pablo Greco ceb317
false = 0
Pablo Greco ceb317
Pablo Greco ceb317
Pablo Greco ceb317
def load_symvers(symvers, filename):
Pablo Greco ceb317
    """Load a Module.symvers file."""
Pablo Greco ceb317
Pablo Greco ceb317
    symvers_file = open(filename, "r")
Pablo Greco ceb317
Pablo Greco ceb317
    while true:
Pablo Greco ceb317
        in_line = symvers_file.readline()
Pablo Greco ceb317
        if in_line == "":
Pablo Greco ceb317
            break
Pablo Greco ceb317
        if in_line == "\n":
Pablo Greco ceb317
            continue
Pablo Greco ceb317
        checksum, symbol, directory, type = in_line.split()
Pablo Greco ceb317
Pablo Greco ceb317
        symvers[symbol] = in_line[0:-1]
Pablo Greco ceb317
Pablo Greco ceb317
Pablo Greco ceb317
def load_kabi(kabi, filename):
Pablo Greco ceb317
    """Load a Module.kabi file."""
Pablo Greco ceb317
Pablo Greco ceb317
    kabi_file = open(filename, "r")
Pablo Greco ceb317
Pablo Greco ceb317
    while true:
Pablo Greco ceb317
        in_line = kabi_file.readline()
Pablo Greco ceb317
        if in_line == "":
Pablo Greco ceb317
            break
Pablo Greco ceb317
        if in_line == "\n":
Pablo Greco ceb317
            continue
Pablo Greco ceb317
        checksum, symbol, directory, type = in_line.split()
Pablo Greco ceb317
Pablo Greco ceb317
        kabi[symbol] = in_line[0:-1]
Pablo Greco ceb317
Pablo Greco ceb317
Pablo Greco ceb317
def check_kabi(symvers, kabi):
Pablo Greco ceb317
    """Check Module.kabi and Module.symvers files."""
Pablo Greco ceb317
Pablo Greco ceb317
    fail = 0
Pablo Greco ceb317
    warn = 0
Pablo Greco ceb317
    changed_symbols = []
Pablo Greco ceb317
    moved_symbols = []
Pablo Greco ceb317
Pablo Greco ceb317
    for symbol in kabi:
Pablo Greco ceb317
        abi_hash, abi_sym, abi_dir, abi_type = kabi[symbol].split()
Pablo Greco ceb317
        if symbol in symvers:
Pablo Greco ceb317
            sym_hash, sym_sym, sym_dir, sym_type = symvers[symbol].split()
Pablo Greco ceb317
            if abi_hash != sym_hash:
Pablo Greco ceb317
                fail = 1
Pablo Greco ceb317
                changed_symbols.append(symbol)
Pablo Greco ceb317
Pablo Greco ceb317
            if abi_dir != sym_dir:
Pablo Greco ceb317
                warn = 1
Pablo Greco ceb317
                moved_symbols.append(symbol)
Pablo Greco ceb317
        else:
Pablo Greco ceb317
            fail = 1
Pablo Greco ceb317
            changed_symbols.append(symbol)
Pablo Greco ceb317
Pablo Greco ceb317
    if fail:
Pablo Greco ceb317
        print("*** ERROR - ABI BREAKAGE WAS DETECTED ***")
Pablo Greco ceb317
        print("")
Pablo Greco ceb317
        print("The following symbols have been changed (this will cause an ABI breakage):")
Pablo Greco ceb317
        print("")
Pablo Greco ceb317
        for symbol in changed_symbols:
Pablo Greco ceb317
            print(symbol)
Pablo Greco ceb317
        print("")
Pablo Greco ceb317
Pablo Greco ceb317
    if warn:
Pablo Greco ceb317
        print("*** WARNING - ABI SYMBOLS MOVED ***")
Pablo Greco ceb317
        print("")
Pablo Greco ceb317
        print("The following symbols moved (typically caused by moving a symbol from being")
Pablo Greco ceb317
        print("provided by the kernel vmlinux out to a loadable module):")
Pablo Greco ceb317
        print("")
Pablo Greco ceb317
        for symbol in moved_symbols:
Pablo Greco ceb317
            print(symbol)
Pablo Greco ceb317
        print("")
Pablo Greco ceb317
Pablo Greco ceb317
    """Halt the build, if we got errors and/or warnings. In either case,
Pablo Greco ceb317
       double-checkig is required to avoid introducing / concealing
Pablo Greco ceb317
       KABI inconsistencies."""
Pablo Greco ceb317
    if fail or warn:
Pablo Greco ceb317
        sys.exit(1)
Pablo Greco ceb317
    sys.exit(0)
Pablo Greco ceb317
Pablo Greco ceb317
Pablo Greco ceb317
def usage():
Pablo Greco ceb317
    print("""
Pablo Greco ceb317
check-kabi: check Module.kabi and Module.symvers files.
Pablo Greco ceb317
Pablo Greco ceb317
    check-kabi [ -k Module.kabi ] [ -s Module.symvers ]
Pablo Greco ceb317
Pablo Greco ceb317
""")
Pablo Greco ceb317
Pablo Greco ceb317
Pablo Greco ceb317
if __name__ == "__main__":
Pablo Greco ceb317
Pablo Greco ceb317
    symvers_file = ""
Pablo Greco ceb317
    kabi_file = ""
Pablo Greco ceb317
Pablo Greco ceb317
    opts, args = getopt.getopt(sys.argv[1:], 'hk:s:')
Pablo Greco ceb317
Pablo Greco ceb317
    for o, v in opts:
Pablo Greco ceb317
        if o == "-s":
Pablo Greco ceb317
            symvers_file = v
Pablo Greco ceb317
        if o == "-h":
Pablo Greco ceb317
            usage()
Pablo Greco ceb317
            sys.exit(0)
Pablo Greco ceb317
        if o == "-k":
Pablo Greco ceb317
            kabi_file = v
Pablo Greco ceb317
Pablo Greco ceb317
    if (symvers_file == "") or (kabi_file == ""):
Pablo Greco ceb317
        usage()
Pablo Greco ceb317
        sys.exit(1)
Pablo Greco ceb317
Pablo Greco ceb317
    symvers = {}
Pablo Greco ceb317
    kabi = {}
Pablo Greco ceb317
Pablo Greco ceb317
    load_symvers(symvers, symvers_file)
Pablo Greco ceb317
    load_kabi(kabi, kabi_file)
Pablo Greco ceb317
    check_kabi(symvers, kabi)