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