pengqianheng / rpms / kernel

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