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