Blame buildtools/wafsamba/symbols.py

rpm-build 95f51c
# a waf tool to extract symbols from object files or libraries
rpm-build 95f51c
# using nm, producing a set of exposed defined/undefined symbols
rpm-build 95f51c
rpm-build 95f51c
import os, re, subprocess
rpm-build 95f51c
from waflib import Utils, Build, Options, Logs, Errors
rpm-build 95f51c
from waflib.Logs import debug
rpm-build 95f51c
from samba_utils import TO_LIST, LOCAL_CACHE, get_tgt_list
rpm-build 95f51c
rpm-build 95f51c
# these are the data structures used in symbols.py:
rpm-build 95f51c
#
rpm-build 95f51c
# bld.env.symbol_map : dictionary mapping public symbol names to list of
rpm-build 95f51c
#                      subsystem names where that symbol exists
rpm-build 95f51c
#
rpm-build 95f51c
# t.in_library       : list of libraries that t is in
rpm-build 95f51c
#
rpm-build 95f51c
# bld.env.public_symbols: set of public symbols for each subsystem
rpm-build 95f51c
# bld.env.used_symbols  : set of used symbols for each subsystem
rpm-build 95f51c
#
rpm-build 95f51c
# bld.env.syslib_symbols: dictionary mapping system library name to set of symbols
rpm-build 95f51c
#                         for that library
rpm-build 95f51c
# bld.env.library_dict  : dictionary mapping built library paths to subsystem names
rpm-build 95f51c
#
rpm-build 95f51c
# LOCAL_CACHE(bld, 'TARGET_TYPE') : dictionary mapping subsystem name to target type
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def symbols_extract(bld, objfiles, dynamic=False):
rpm-build 95f51c
    '''extract symbols from objfile, returning a dictionary containing
rpm-build 95f51c
       the set of undefined and public symbols for each file'''
rpm-build 95f51c
rpm-build 95f51c
    ret = {}
rpm-build 95f51c
rpm-build 95f51c
    # see if we can get some results from the nm cache
rpm-build 95f51c
    if not bld.env.nm_cache:
rpm-build 95f51c
        bld.env.nm_cache = {}
rpm-build 95f51c
rpm-build 95f51c
    objfiles = set(objfiles).copy()
rpm-build 95f51c
rpm-build 95f51c
    remaining = set()
rpm-build 95f51c
    for obj in objfiles:
rpm-build 95f51c
        if obj in bld.env.nm_cache:
rpm-build 95f51c
            ret[obj] = bld.env.nm_cache[obj].copy()
rpm-build 95f51c
        else:
rpm-build 95f51c
            remaining.add(obj)
rpm-build 95f51c
    objfiles = remaining
rpm-build 95f51c
rpm-build 95f51c
    if len(objfiles) == 0:
rpm-build 95f51c
        return ret
rpm-build 95f51c
rpm-build 95f51c
    cmd = ["nm"]
rpm-build 95f51c
    if dynamic:
rpm-build 95f51c
        # needed for some .so files
rpm-build 95f51c
        cmd.append("-D")
rpm-build 95f51c
    cmd.extend(list(objfiles))
rpm-build 95f51c
rpm-build 95f51c
    nmpipe = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
rpm-build 95f51c
    if len(objfiles) == 1:
rpm-build 95f51c
        filename = list(objfiles)[0]
rpm-build 95f51c
        ret[filename] = { "PUBLIC": set(), "UNDEFINED" : set()}
rpm-build 95f51c
rpm-build 95f51c
    for line in nmpipe:
rpm-build 95f51c
        line = line.strip()
rpm-build 95f51c
        if line.endswith(b':'):
rpm-build 95f51c
            filename = line[:-1]
rpm-build 95f51c
            ret[filename] = { "PUBLIC": set(), "UNDEFINED" : set() }
rpm-build 95f51c
            continue
rpm-build 95f51c
        cols = line.split(b" ")
rpm-build 95f51c
        if cols == [b'']:
rpm-build 95f51c
            continue
rpm-build 95f51c
        # see if the line starts with an address
rpm-build 95f51c
        if len(cols) == 3:
rpm-build 95f51c
            symbol_type = cols[1]
rpm-build 95f51c
            symbol = cols[2]
rpm-build 95f51c
        else:
rpm-build 95f51c
            symbol_type = cols[0]
rpm-build 95f51c
            symbol = cols[1]
rpm-build 95f51c
        if symbol_type in b"BDGTRVWSi":
rpm-build 95f51c
            # its a public symbol
rpm-build 95f51c
            ret[filename]["PUBLIC"].add(symbol)
rpm-build 95f51c
        elif symbol_type in b"U":
rpm-build 95f51c
            ret[filename]["UNDEFINED"].add(symbol)
rpm-build 95f51c
rpm-build 95f51c
    # add to the cache
rpm-build 95f51c
    for obj in objfiles:
rpm-build 95f51c
        if obj in ret:
rpm-build 95f51c
            bld.env.nm_cache[obj] = ret[obj].copy()
rpm-build 95f51c
        else:
rpm-build 95f51c
            bld.env.nm_cache[obj] = { "PUBLIC": set(), "UNDEFINED" : set() }
rpm-build 95f51c
rpm-build 95f51c
    return ret
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def real_name(name):
rpm-build 95f51c
    if name.find(".objlist") != -1:
rpm-build 95f51c
        name = name[:-8]
rpm-build 95f51c
    return name
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def find_ldd_path(bld, libname, binary):
rpm-build 95f51c
    '''find the path to the syslib we will link against'''
rpm-build 95f51c
    ret = None
rpm-build 95f51c
    if not bld.env.syslib_paths:
rpm-build 95f51c
        bld.env.syslib_paths = {}
rpm-build 95f51c
    if libname in bld.env.syslib_paths:
rpm-build 95f51c
        return bld.env.syslib_paths[libname]
rpm-build 95f51c
rpm-build 95f51c
    lddpipe = subprocess.Popen(['ldd', binary], stdout=subprocess.PIPE).stdout
rpm-build 95f51c
    for line in lddpipe:
rpm-build 95f51c
        line = line.strip()
rpm-build 95f51c
        cols = line.split(b" ")
rpm-build 95f51c
        if len(cols) < 3 or cols[1] != b"=>":
rpm-build 95f51c
            continue
rpm-build 95f51c
        if cols[0].startswith(b"libc."):
rpm-build 95f51c
            # save this one too
rpm-build 95f51c
            bld.env.libc_path = cols[2]
rpm-build 95f51c
        if cols[0].startswith(libname):
rpm-build 95f51c
            ret = cols[2]
rpm-build 95f51c
    bld.env.syslib_paths[libname] = ret
rpm-build 95f51c
    return ret
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
# some regular expressions for parsing readelf output
rpm-build 95f51c
re_sharedlib = re.compile(b'Shared library: \[(.*)\]')
rpm-build 95f51c
# output from readelf could be `Library rpath` or `Libray runpath`
rpm-build 95f51c
re_rpath     = re.compile(b'Library (rpath|runpath): \[(.*)\]')
rpm-build 95f51c
rpm-build 95f51c
def get_libs(bld, binname):
rpm-build 95f51c
    '''find the list of linked libraries for any binary or library
rpm-build 95f51c
    binname is the path to the binary/library on disk
rpm-build 95f51c
rpm-build 95f51c
    We do this using readelf instead of ldd as we need to avoid recursing
rpm-build 95f51c
    into system libraries
rpm-build 95f51c
    '''
rpm-build 95f51c
rpm-build 95f51c
    # see if we can get the result from the ldd cache
rpm-build 95f51c
    if not bld.env.lib_cache:
rpm-build 95f51c
        bld.env.lib_cache = {}
rpm-build 95f51c
    if binname in bld.env.lib_cache:
rpm-build 95f51c
        return bld.env.lib_cache[binname].copy()
rpm-build 95f51c
rpm-build 95f51c
    rpath = []
rpm-build 95f51c
    libs = set()
rpm-build 95f51c
rpm-build 95f51c
    elfpipe = subprocess.Popen(['readelf', '--dynamic', binname], stdout=subprocess.PIPE).stdout
rpm-build 95f51c
    for line in elfpipe:
rpm-build 95f51c
        m = re_sharedlib.search(line)
rpm-build 95f51c
        if m:
rpm-build 95f51c
            libs.add(m.group(1))
rpm-build 95f51c
        m = re_rpath.search(line)
rpm-build 95f51c
        if m:
rpm-build 95f51c
            # output from Popen is always bytestr even in py3
rpm-build 95f51c
            rpath.extend(m.group(2).split(b":"))
rpm-build 95f51c
rpm-build 95f51c
    ret = set()
rpm-build 95f51c
    for lib in libs:
rpm-build 95f51c
        found = False
rpm-build 95f51c
        for r in rpath:
rpm-build 95f51c
            path = os.path.join(r, lib)
rpm-build 95f51c
            if os.path.exists(path):
rpm-build 95f51c
                ret.add(os.path.realpath(path))
rpm-build 95f51c
                found = True
rpm-build 95f51c
                break
rpm-build 95f51c
        if not found:
rpm-build 95f51c
            # we didn't find this lib using rpath. It is probably a system
rpm-build 95f51c
            # library, so to find the path to it we either need to use ldd
rpm-build 95f51c
            # or we need to start parsing /etc/ld.so.conf* ourselves. We'll
rpm-build 95f51c
            # use ldd for now, even though it is slow
rpm-build 95f51c
            path = find_ldd_path(bld, lib, binname)
rpm-build 95f51c
            if path:
rpm-build 95f51c
                ret.add(os.path.realpath(path))
rpm-build 95f51c
rpm-build 95f51c
    bld.env.lib_cache[binname] = ret.copy()
rpm-build 95f51c
rpm-build 95f51c
    return ret
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def get_libs_recursive(bld, binname, seen):
rpm-build 95f51c
    '''find the recursive list of linked libraries for any binary or library
rpm-build 95f51c
    binname is the path to the binary/library on disk. seen is a set used
rpm-build 95f51c
    to prevent loops
rpm-build 95f51c
    '''
rpm-build 95f51c
    if binname in seen:
rpm-build 95f51c
        return set()
rpm-build 95f51c
    ret = get_libs(bld, binname)
rpm-build 95f51c
    seen.add(binname)
rpm-build 95f51c
    for lib in ret:
rpm-build 95f51c
        # we don't want to recurse into system libraries. If a system
rpm-build 95f51c
        # library that we use (eg. libcups) happens to use another library
rpm-build 95f51c
        # (such as libkrb5) which contains common symbols with our own
rpm-build 95f51c
        # libraries, then that is not an error
rpm-build 95f51c
        if lib in bld.env.library_dict:
rpm-build 95f51c
            ret = ret.union(get_libs_recursive(bld, lib, seen))
rpm-build 95f51c
    return ret
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def find_syslib_path(bld, libname, deps):
rpm-build 95f51c
    '''find the path to the syslib we will link against'''
rpm-build 95f51c
    # the strategy is to use the targets that depend on the library, and run ldd
rpm-build 95f51c
    # on it to find the real location of the library that is used
rpm-build 95f51c
rpm-build 95f51c
    linkpath = deps[0].link_task.outputs[0].abspath(bld.env)
rpm-build 95f51c
rpm-build 95f51c
    if libname == "python":
rpm-build 95f51c
        libname += bld.env.PYTHON_VERSION
rpm-build 95f51c
rpm-build 95f51c
    return find_ldd_path(bld, "lib%s" % libname.lower(), linkpath)
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def build_symbol_sets(bld, tgt_list):
rpm-build 95f51c
    '''build the public_symbols and undefined_symbols attributes for each target'''
rpm-build 95f51c
rpm-build 95f51c
    if bld.env.public_symbols:
rpm-build 95f51c
        return
rpm-build 95f51c
rpm-build 95f51c
    objlist = []  # list of object file
rpm-build 95f51c
    objmap = {}   # map from object filename to target (subsystem) name
rpm-build 95f51c
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        t.public_symbols = set()
rpm-build 95f51c
        t.undefined_symbols = set()
rpm-build 95f51c
        t.used_symbols = set()
rpm-build 95f51c
        for tsk in getattr(t, 'compiled_tasks', []):
rpm-build 95f51c
            for output in tsk.outputs:
rpm-build 95f51c
                objpath = output.abspath(bld.env)
rpm-build 95f51c
                objlist.append(objpath)
rpm-build 95f51c
                objmap[objpath] = t
rpm-build 95f51c
rpm-build 95f51c
    symbols = symbols_extract(bld, objlist)
rpm-build 95f51c
    for obj in objlist:
rpm-build 95f51c
        t = objmap[obj]
rpm-build 95f51c
        t.public_symbols = t.public_symbols.union(symbols[obj]["PUBLIC"])
rpm-build 95f51c
        t.undefined_symbols = t.undefined_symbols.union(symbols[obj]["UNDEFINED"])
rpm-build 95f51c
        t.used_symbols = t.used_symbols.union(symbols[obj]["UNDEFINED"])
rpm-build 95f51c
rpm-build 95f51c
    t.undefined_symbols = t.undefined_symbols.difference(t.public_symbols)
rpm-build 95f51c
rpm-build 95f51c
    # and the reverse map of public symbols to subsystem name
rpm-build 95f51c
    bld.env.symbol_map = {}
rpm-build 95f51c
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        for s in t.public_symbols:
rpm-build 95f51c
            if not s in bld.env.symbol_map:
rpm-build 95f51c
                bld.env.symbol_map[s] = []
rpm-build 95f51c
            bld.env.symbol_map[s].append(real_name(t.sname))
rpm-build 95f51c
rpm-build 95f51c
    targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
rpm-build 95f51c
rpm-build 95f51c
    bld.env.public_symbols = {}
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        name = real_name(t.sname)
rpm-build 95f51c
        if name in bld.env.public_symbols:
rpm-build 95f51c
            bld.env.public_symbols[name] = bld.env.public_symbols[name].union(t.public_symbols)
rpm-build 95f51c
        else:
rpm-build 95f51c
            bld.env.public_symbols[name] = t.public_symbols
rpm-build 95f51c
        if t.samba_type == 'LIBRARY':
rpm-build 95f51c
            for dep in t.add_objects:
rpm-build 95f51c
                t2 = bld.get_tgen_by_name(dep)
rpm-build 95f51c
                bld.ASSERT(t2 is not None, "Library '%s' has unknown dependency '%s'" % (name, dep))
rpm-build 95f51c
                bld.env.public_symbols[name] = bld.env.public_symbols[name].union(t2.public_symbols)
rpm-build 95f51c
rpm-build 95f51c
    bld.env.used_symbols = {}
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        name = real_name(t.sname)
rpm-build 95f51c
        if name in bld.env.used_symbols:
rpm-build 95f51c
            bld.env.used_symbols[name] = bld.env.used_symbols[name].union(t.used_symbols)
rpm-build 95f51c
        else:
rpm-build 95f51c
            bld.env.used_symbols[name] = t.used_symbols
rpm-build 95f51c
        if t.samba_type == 'LIBRARY':
rpm-build 95f51c
            for dep in t.add_objects:
rpm-build 95f51c
                t2 = bld.get_tgen_by_name(dep)
rpm-build 95f51c
                bld.ASSERT(t2 is not None, "Library '%s' has unknown dependency '%s'" % (name, dep))
rpm-build 95f51c
                bld.env.used_symbols[name] = bld.env.used_symbols[name].union(t2.used_symbols)
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def build_library_dict(bld, tgt_list):
rpm-build 95f51c
    '''build the library_dict dictionary'''
rpm-build 95f51c
rpm-build 95f51c
    if bld.env.library_dict:
rpm-build 95f51c
        return
rpm-build 95f51c
rpm-build 95f51c
    bld.env.library_dict = {}
rpm-build 95f51c
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        if t.samba_type in [ 'LIBRARY', 'PYTHON' ]:
rpm-build 95f51c
            linkpath = os.path.realpath(t.link_task.outputs[0].abspath(bld.env))
rpm-build 95f51c
            bld.env.library_dict[linkpath] = t.sname
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def build_syslib_sets(bld, tgt_list):
rpm-build 95f51c
    '''build the public_symbols for all syslibs'''
rpm-build 95f51c
rpm-build 95f51c
    if bld.env.syslib_symbols:
rpm-build 95f51c
        return
rpm-build 95f51c
rpm-build 95f51c
    # work out what syslibs we depend on, and what targets those are used in
rpm-build 95f51c
    syslibs = {}
rpm-build 95f51c
    objmap = {}
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        if getattr(t, 'uselib', []) and t.samba_type in [ 'LIBRARY', 'BINARY', 'PYTHON' ]:
rpm-build 95f51c
            for lib in t.uselib:
rpm-build 95f51c
                if lib in ['PYEMBED', 'PYEXT']:
rpm-build 95f51c
                    lib = "python"
rpm-build 95f51c
                if not lib in syslibs:
rpm-build 95f51c
                    syslibs[lib] = []
rpm-build 95f51c
                syslibs[lib].append(t)
rpm-build 95f51c
rpm-build 95f51c
    # work out the paths to each syslib
rpm-build 95f51c
    syslib_paths = []
rpm-build 95f51c
    for lib in syslibs:
rpm-build 95f51c
        path = find_syslib_path(bld, lib, syslibs[lib])
rpm-build 95f51c
        if path is None:
rpm-build 95f51c
            Logs.warn("Unable to find syslib path for %s" % lib)
rpm-build 95f51c
        if path is not None:
rpm-build 95f51c
            syslib_paths.append(path)
rpm-build 95f51c
            objmap[path] = lib.lower()
rpm-build 95f51c
rpm-build 95f51c
    # add in libc
rpm-build 95f51c
    syslib_paths.append(bld.env.libc_path)
rpm-build 95f51c
    objmap[bld.env.libc_path] = 'c'
rpm-build 95f51c
rpm-build 95f51c
    symbols = symbols_extract(bld, syslib_paths, dynamic=True)
rpm-build 95f51c
rpm-build 95f51c
    # keep a map of syslib names to public symbols
rpm-build 95f51c
    bld.env.syslib_symbols = {}
rpm-build 95f51c
    for lib in symbols:
rpm-build 95f51c
        bld.env.syslib_symbols[lib] = symbols[lib]["PUBLIC"]
rpm-build 95f51c
rpm-build 95f51c
    # add to the map of symbols to dependencies
rpm-build 95f51c
    for lib in symbols:
rpm-build 95f51c
        for sym in symbols[lib]["PUBLIC"]:
rpm-build 95f51c
            if not sym in bld.env.symbol_map:
rpm-build 95f51c
                bld.env.symbol_map[sym] = []
rpm-build 95f51c
            bld.env.symbol_map[sym].append(objmap[lib])
rpm-build 95f51c
rpm-build 95f51c
    # keep the libc symbols as well, as these are useful for some of the
rpm-build 95f51c
    # sanity checks
rpm-build 95f51c
    bld.env.libc_symbols = symbols[bld.env.libc_path]["PUBLIC"]
rpm-build 95f51c
rpm-build 95f51c
    # add to the combined map of dependency name to public_symbols
rpm-build 95f51c
    for lib in bld.env.syslib_symbols:
rpm-build 95f51c
        bld.env.public_symbols[objmap[lib]] = bld.env.syslib_symbols[lib]
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def build_autodeps(bld, t):
rpm-build 95f51c
    '''build the set of dependencies for a target'''
rpm-build 95f51c
    deps = set()
rpm-build 95f51c
    name = real_name(t.sname)
rpm-build 95f51c
rpm-build 95f51c
    targets    = LOCAL_CACHE(bld, 'TARGET_TYPE')
rpm-build 95f51c
rpm-build 95f51c
    for sym in t.undefined_symbols:
rpm-build 95f51c
        if sym in t.public_symbols:
rpm-build 95f51c
            continue
rpm-build 95f51c
        if sym in bld.env.symbol_map:
rpm-build 95f51c
            depname = bld.env.symbol_map[sym]
rpm-build 95f51c
            if depname == [ name ]:
rpm-build 95f51c
                # self dependencies aren't interesting
rpm-build 95f51c
                continue
rpm-build 95f51c
            if t.in_library == depname:
rpm-build 95f51c
                # no need to depend on the library we are part of
rpm-build 95f51c
                continue
rpm-build 95f51c
            if depname[0] in ['c', 'python']:
rpm-build 95f51c
                # these don't go into autodeps
rpm-build 95f51c
                continue
rpm-build 95f51c
            if targets[depname[0]] in [ 'SYSLIB' ]:
rpm-build 95f51c
                deps.add(depname[0])
rpm-build 95f51c
                continue
rpm-build 95f51c
            t2 = bld.get_tgen_by_name(depname[0])
rpm-build 95f51c
            if len(t2.in_library) != 1:
rpm-build 95f51c
                deps.add(depname[0])
rpm-build 95f51c
                continue
rpm-build 95f51c
            if t2.in_library == t.in_library:
rpm-build 95f51c
                # if we're part of the same library, we don't need to autodep
rpm-build 95f51c
                continue
rpm-build 95f51c
            deps.add(t2.in_library[0])
rpm-build 95f51c
    t.autodeps = deps
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def build_library_names(bld, tgt_list):
rpm-build 95f51c
    '''add a in_library attribute to all targets that are part of a library'''
rpm-build 95f51c
rpm-build 95f51c
    if bld.env.done_build_library_names:
rpm-build 95f51c
        return
rpm-build 95f51c
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        t.in_library = []
rpm-build 95f51c
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        if t.samba_type in [ 'LIBRARY' ]:
rpm-build 95f51c
            for obj in t.samba_deps_extended:
rpm-build 95f51c
                t2 = bld.get_tgen_by_name(obj)
rpm-build 95f51c
                if t2 and t2.samba_type in [ 'SUBSYSTEM', 'ASN1' ]:
rpm-build 95f51c
                    if not t.sname in t2.in_library:
rpm-build 95f51c
                        t2.in_library.append(t.sname)
rpm-build 95f51c
    bld.env.done_build_library_names = True
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def check_library_deps(bld, t):
rpm-build 95f51c
    '''check that all the autodeps that have mutual dependency of this
rpm-build 95f51c
    target are in the same library as the target'''
rpm-build 95f51c
rpm-build 95f51c
    name = real_name(t.sname)
rpm-build 95f51c
rpm-build 95f51c
    if len(t.in_library) > 1:
rpm-build 95f51c
        Logs.warn("WARNING: Target '%s' in multiple libraries: %s" % (t.sname, t.in_library))
rpm-build 95f51c
rpm-build 95f51c
    for dep in t.autodeps:
rpm-build 95f51c
        t2 = bld.get_tgen_by_name(dep)
rpm-build 95f51c
        if t2 is None:
rpm-build 95f51c
            continue
rpm-build 95f51c
        for dep2 in t2.autodeps:
rpm-build 95f51c
            if dep2 == name and t.in_library != t2.in_library:
rpm-build 95f51c
                Logs.warn("WARNING: mutual dependency %s <=> %s" % (name, real_name(t2.sname)))
rpm-build 95f51c
                Logs.warn("Libraries should match. %s != %s" % (t.in_library, t2.in_library))
rpm-build 95f51c
                # raise Errors.WafError("illegal mutual dependency")
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def check_syslib_collisions(bld, tgt_list):
rpm-build 95f51c
    '''check if a target has any symbol collisions with a syslib
rpm-build 95f51c
rpm-build 95f51c
    We do not want any code in Samba to use a symbol name from a
rpm-build 95f51c
    system library. The chance of that causing problems is just too
rpm-build 95f51c
    high. Note that libreplace uses a rep_XX approach of renaming
rpm-build 95f51c
    symbols via macros
rpm-build 95f51c
    '''
rpm-build 95f51c
rpm-build 95f51c
    has_error = False
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        for lib in bld.env.syslib_symbols:
rpm-build 95f51c
            common = t.public_symbols.intersection(bld.env.syslib_symbols[lib])
rpm-build 95f51c
            if common:
rpm-build 95f51c
                Logs.error("ERROR: Target '%s' has symbols '%s' which is also in syslib '%s'" % (t.sname, common, lib))
rpm-build 95f51c
                has_error = True
rpm-build 95f51c
    if has_error:
rpm-build 95f51c
        raise Errors.WafError("symbols in common with system libraries")
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def check_dependencies(bld, t):
rpm-build 95f51c
    '''check for depenencies that should be changed'''
rpm-build 95f51c
rpm-build 95f51c
    if bld.get_tgen_by_name(t.sname + ".objlist"):
rpm-build 95f51c
        return
rpm-build 95f51c
rpm-build 95f51c
    targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
rpm-build 95f51c
rpm-build 95f51c
    remaining = t.undefined_symbols.copy()
rpm-build 95f51c
    remaining = remaining.difference(t.public_symbols)
rpm-build 95f51c
rpm-build 95f51c
    sname = real_name(t.sname)
rpm-build 95f51c
rpm-build 95f51c
    deps = set(t.samba_deps)
rpm-build 95f51c
    for d in t.samba_deps:
rpm-build 95f51c
        if targets[d] in [ 'EMPTY', 'DISABLED', 'SYSLIB', 'GENERATOR' ]:
rpm-build 95f51c
            continue
rpm-build 95f51c
        bld.ASSERT(d in bld.env.public_symbols, "Failed to find symbol list for dependency '%s'" % d)
rpm-build 95f51c
        diff = remaining.intersection(bld.env.public_symbols[d])
rpm-build 95f51c
        if not diff and targets[sname] != 'LIBRARY':
rpm-build 95f51c
            Logs.info("Target '%s' has no dependency on %s" % (sname, d))
rpm-build 95f51c
        else:
rpm-build 95f51c
            remaining = remaining.difference(diff)
rpm-build 95f51c
rpm-build 95f51c
    t.unsatisfied_symbols = set()
rpm-build 95f51c
    needed = {}
rpm-build 95f51c
    for sym in remaining:
rpm-build 95f51c
        if sym in bld.env.symbol_map:
rpm-build 95f51c
            dep = bld.env.symbol_map[sym]
rpm-build 95f51c
            if not dep[0] in needed:
rpm-build 95f51c
                needed[dep[0]] = set()
rpm-build 95f51c
            needed[dep[0]].add(sym)
rpm-build 95f51c
        else:
rpm-build 95f51c
            t.unsatisfied_symbols.add(sym)
rpm-build 95f51c
rpm-build 95f51c
    for dep in needed:
rpm-build 95f51c
        Logs.info("Target '%s' should add dep '%s' for symbols %s" % (sname, dep, " ".join(needed[dep])))
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def check_syslib_dependencies(bld, t):
rpm-build 95f51c
    '''check for syslib depenencies'''
rpm-build 95f51c
rpm-build 95f51c
    if bld.get_tgen_by_name(t.sname + ".objlist"):
rpm-build 95f51c
        return
rpm-build 95f51c
rpm-build 95f51c
    sname = real_name(t.sname)
rpm-build 95f51c
rpm-build 95f51c
    remaining = set()
rpm-build 95f51c
rpm-build 95f51c
    features = TO_LIST(t.features)
rpm-build 95f51c
    if 'pyembed' in features or 'pyext' in features:
rpm-build 95f51c
        if 'python' in bld.env.public_symbols:
rpm-build 95f51c
            t.unsatisfied_symbols = t.unsatisfied_symbols.difference(bld.env.public_symbols['python'])
rpm-build 95f51c
rpm-build 95f51c
    needed = {}
rpm-build 95f51c
    for sym in t.unsatisfied_symbols:
rpm-build 95f51c
        if sym in bld.env.symbol_map:
rpm-build 95f51c
            dep = bld.env.symbol_map[sym][0]
rpm-build 95f51c
            if dep == 'c':
rpm-build 95f51c
                continue
rpm-build 95f51c
            if not dep in needed:
rpm-build 95f51c
                needed[dep] = set()
rpm-build 95f51c
            needed[dep].add(sym)
rpm-build 95f51c
        else:
rpm-build 95f51c
            remaining.add(sym)
rpm-build 95f51c
rpm-build 95f51c
    for dep in needed:
rpm-build 95f51c
        Logs.info("Target '%s' should add syslib dep '%s' for symbols %s" % (sname, dep, " ".join(needed[dep])))
rpm-build 95f51c
rpm-build 95f51c
    if remaining:
rpm-build 95f51c
        debug("deps: Target '%s' has unsatisfied symbols: %s" % (sname, " ".join(remaining)))
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def symbols_symbolcheck(task):
rpm-build 95f51c
    '''check the internal dependency lists'''
rpm-build 95f51c
    bld = task.env.bld
rpm-build 95f51c
    tgt_list = get_tgt_list(bld)
rpm-build 95f51c
rpm-build 95f51c
    build_symbol_sets(bld, tgt_list)
rpm-build 95f51c
    build_library_names(bld, tgt_list)
rpm-build 95f51c
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        t.autodeps = set()
rpm-build 95f51c
        if getattr(t, 'source', ''):
rpm-build 95f51c
            build_autodeps(bld, t)
rpm-build 95f51c
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        check_dependencies(bld, t)
rpm-build 95f51c
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        check_library_deps(bld, t)
rpm-build 95f51c
rpm-build 95f51c
def symbols_syslibcheck(task):
rpm-build 95f51c
    '''check the syslib dependencies'''
rpm-build 95f51c
    bld = task.env.bld
rpm-build 95f51c
    tgt_list = get_tgt_list(bld)
rpm-build 95f51c
rpm-build 95f51c
    build_syslib_sets(bld, tgt_list)
rpm-build 95f51c
    check_syslib_collisions(bld, tgt_list)
rpm-build 95f51c
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        check_syslib_dependencies(bld, t)
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def symbols_whyneeded(task):
rpm-build 95f51c
    """check why 'target' needs to link to 'subsystem'"""
rpm-build 95f51c
    bld = task.env.bld
rpm-build 95f51c
    tgt_list = get_tgt_list(bld)
rpm-build 95f51c
rpm-build 95f51c
    why = Options.options.WHYNEEDED.split(":")
rpm-build 95f51c
    if len(why) != 2:
rpm-build 95f51c
        raise Errors.WafError("usage: WHYNEEDED=TARGET:DEPENDENCY")
rpm-build 95f51c
    target = why[0]
rpm-build 95f51c
    subsystem = why[1]
rpm-build 95f51c
rpm-build 95f51c
    build_symbol_sets(bld, tgt_list)
rpm-build 95f51c
    build_library_names(bld, tgt_list)
rpm-build 95f51c
    build_syslib_sets(bld, tgt_list)
rpm-build 95f51c
rpm-build 95f51c
    Logs.info("Checking why %s needs to link to %s" % (target, subsystem))
rpm-build 95f51c
    if not target in bld.env.used_symbols:
rpm-build 95f51c
        Logs.warn("unable to find target '%s' in used_symbols dict" % target)
rpm-build 95f51c
        return
rpm-build 95f51c
    if not subsystem in bld.env.public_symbols:
rpm-build 95f51c
        Logs.warn("unable to find subsystem '%s' in public_symbols dict" % subsystem)
rpm-build 95f51c
        return
rpm-build 95f51c
    overlap = bld.env.used_symbols[target].intersection(bld.env.public_symbols[subsystem])
rpm-build 95f51c
    if not overlap:
rpm-build 95f51c
        Logs.info("target '%s' doesn't use any public symbols from '%s'" % (target, subsystem))
rpm-build 95f51c
    else:
rpm-build 95f51c
        Logs.info("target '%s' uses symbols %s from '%s'" % (target, overlap, subsystem))
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def report_duplicate(bld, binname, sym, libs, fail_on_error):
rpm-build 95f51c
    '''report duplicated symbols'''
rpm-build 95f51c
    if sym in ['_init', '_fini', '_edata', '_end', '__bss_start']:
rpm-build 95f51c
        return
rpm-build 95f51c
    libnames = []
rpm-build 95f51c
    for lib in libs:
rpm-build 95f51c
        if lib in bld.env.library_dict:
rpm-build 95f51c
            libnames.append(bld.env.library_dict[lib])
rpm-build 95f51c
        else:
rpm-build 95f51c
            libnames.append(lib)
rpm-build 95f51c
    if fail_on_error:
rpm-build 95f51c
        raise Errors.WafError("%s: Symbol %s linked in multiple libraries %s" % (binname, sym, libnames))
rpm-build 95f51c
    else:
rpm-build 95f51c
        print("%s: Symbol %s linked in multiple libraries %s" % (binname, sym, libnames))
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def symbols_dupcheck_binary(bld, binname, fail_on_error):
rpm-build 95f51c
    '''check for duplicated symbols in one binary'''
rpm-build 95f51c
rpm-build 95f51c
    libs = get_libs_recursive(bld, binname, set())
rpm-build 95f51c
    symlist = symbols_extract(bld, libs, dynamic=True)
rpm-build 95f51c
rpm-build 95f51c
    symmap = {}
rpm-build 95f51c
    for libpath in symlist:
rpm-build 95f51c
        for sym in symlist[libpath]['PUBLIC']:
rpm-build 95f51c
            if sym == '_GLOBAL_OFFSET_TABLE_':
rpm-build 95f51c
                continue
rpm-build 95f51c
            if not sym in symmap:
rpm-build 95f51c
                symmap[sym] = set()
rpm-build 95f51c
            symmap[sym].add(libpath)
rpm-build 95f51c
    for sym in symmap:
rpm-build 95f51c
        if len(symmap[sym]) > 1:
rpm-build 95f51c
            for libpath in symmap[sym]:
rpm-build 95f51c
                if libpath in bld.env.library_dict:
rpm-build 95f51c
                    report_duplicate(bld, binname, sym, symmap[sym], fail_on_error)
rpm-build 95f51c
                    break
rpm-build 95f51c
rpm-build 95f51c
def symbols_dupcheck(task, fail_on_error=False):
rpm-build 95f51c
    '''check for symbols defined in two different subsystems'''
rpm-build 95f51c
    bld = task.env.bld
rpm-build 95f51c
    tgt_list = get_tgt_list(bld)
rpm-build 95f51c
rpm-build 95f51c
    targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
rpm-build 95f51c
rpm-build 95f51c
    build_library_dict(bld, tgt_list)
rpm-build 95f51c
    for t in tgt_list:
rpm-build 95f51c
        if t.samba_type == 'BINARY':
rpm-build 95f51c
            binname = os.path.relpath(t.link_task.outputs[0].abspath(bld.env), os.getcwd())
rpm-build 95f51c
            symbols_dupcheck_binary(bld, binname, fail_on_error)
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def symbols_dupcheck_fatal(task):
rpm-build 95f51c
    '''check for symbols defined in two different subsystems (and fail if duplicates are found)'''
rpm-build 95f51c
    symbols_dupcheck(task, fail_on_error=True)
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
def SYMBOL_CHECK(bld):
rpm-build 95f51c
    '''check our dependency lists'''
rpm-build 95f51c
    if Options.options.SYMBOLCHECK:
rpm-build 95f51c
        bld.SET_BUILD_GROUP('symbolcheck')
rpm-build 95f51c
        task = bld(rule=symbols_symbolcheck, always=True, name='symbol checking')
rpm-build 95f51c
        task.env.bld = bld
rpm-build 95f51c
rpm-build 95f51c
        bld.SET_BUILD_GROUP('syslibcheck')
rpm-build 95f51c
        task = bld(rule=symbols_syslibcheck, always=True, name='syslib checking')
rpm-build 95f51c
        task.env.bld = bld
rpm-build 95f51c
rpm-build 95f51c
        bld.SET_BUILD_GROUP('syslibcheck')
rpm-build 95f51c
        task = bld(rule=symbols_dupcheck, always=True, name='symbol duplicate checking')
rpm-build 95f51c
        task.env.bld = bld
rpm-build 95f51c
rpm-build 95f51c
    if Options.options.WHYNEEDED:
rpm-build 95f51c
        bld.SET_BUILD_GROUP('syslibcheck')
rpm-build 95f51c
        task = bld(rule=symbols_whyneeded, always=True, name='check why a dependency is needed')
rpm-build 95f51c
        task.env.bld = bld
rpm-build 95f51c
rpm-build 95f51c
rpm-build 95f51c
Build.BuildContext.SYMBOL_CHECK = SYMBOL_CHECK
rpm-build 95f51c
rpm-build 95f51c
def DUP_SYMBOL_CHECK(bld):
rpm-build 95f51c
    if Options.options.DUP_SYMBOLCHECK and bld.env.DEVELOPER:
rpm-build 95f51c
        '''check for duplicate symbols'''
rpm-build 95f51c
        bld.SET_BUILD_GROUP('syslibcheck')
rpm-build 95f51c
        task = bld(rule=symbols_dupcheck_fatal, always=True, name='symbol duplicate checking')
rpm-build 95f51c
        task.env.bld = bld
rpm-build 95f51c
rpm-build 95f51c
Build.BuildContext.DUP_SYMBOL_CHECK = DUP_SYMBOL_CHECK