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