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