samba_abi.py: avoid inefficient string concatenations
[samba.git] / buildtools / wafsamba / symbols.py
index 0d0af79d06638c29dd0bc069b5e7d402782ceee5..3eca3d46bd71cf0780b3c8e36a26b96bd3aa3b83 100644 (file)
@@ -1,9 +1,10 @@
 # a waf tool to extract symbols from object files or libraries
 # using nm, producing a set of exposed defined/undefined symbols
 
-import Utils, Build, subprocess, Logs
-from samba_wildcard import fake_build_environment
-from samba_utils import *
+import os, re, subprocess
+from waflib import Utils, Build, Options, Logs, Errors
+from waflib.Logs import debug
+from samba_utils import TO_LIST, LOCAL_CACHE, get_tgt_list, os_path_relpath
 
 # these are the data structures used in symbols.py:
 #
@@ -17,34 +18,53 @@ from samba_utils import *
 #
 # bld.env.syslib_symbols: dictionary mapping system library name to set of symbols
 #                         for that library
+# bld.env.library_dict  : dictionary mapping built library paths to subsystem names
 #
 # LOCAL_CACHE(bld, 'TARGET_TYPE') : dictionary mapping subsystem name to target type
 
-def symbols_extract(objfiles, dynamic=False):
+
+def symbols_extract(bld, objfiles, dynamic=False):
     '''extract symbols from objfile, returning a dictionary containing
        the set of undefined and public symbols for each file'''
 
     ret = {}
 
+    # see if we can get some results from the nm cache
+    if not bld.env.nm_cache:
+        bld.env.nm_cache = {}
+
+    objfiles = set(objfiles).copy()
+
+    remaining = set()
+    for obj in objfiles:
+        if obj in bld.env.nm_cache:
+            ret[obj] = bld.env.nm_cache[obj].copy()
+        else:
+            remaining.add(obj)
+    objfiles = remaining
+
+    if len(objfiles) == 0:
+        return ret
+
     cmd = ["nm"]
     if dynamic:
         # needed for some .so files
         cmd.append("-D")
-    cmd.extend(objfiles)
+    cmd.extend(list(objfiles))
 
     nmpipe = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
     if len(objfiles) == 1:
-        filename = objfiles[0]
+        filename = list(objfiles)[0]
         ret[filename] = { "PUBLIC": set(), "UNDEFINED" : set()}
 
     for line in nmpipe:
         line = line.strip()
-        if line.endswith(':'):
+        if line.endswith(b':'):
             filename = line[:-1]
             ret[filename] = { "PUBLIC": set(), "UNDEFINED" : set() }
             continue
-        cols = line.split(" ")
-        if cols == ['']:
+        cols = line.split(b" ")
+        if cols == [b'']:
             continue
         # see if the line starts with an address
         if len(cols) == 3:
@@ -53,12 +73,19 @@ def symbols_extract(objfiles, dynamic=False):
         else:
             symbol_type = cols[0]
             symbol = cols[1]
-        if symbol_type in "BDGTRVWSi":
+        if symbol_type in b"BDGTRVWSi":
             # its a public symbol
             ret[filename]["PUBLIC"].add(symbol)
-        elif symbol_type in "U":
+        elif symbol_type in b"U":
             ret[filename]["UNDEFINED"].add(symbol)
 
+    # add to the cache
+    for obj in objfiles:
+        if obj in ret:
+            bld.env.nm_cache[obj] = ret[obj].copy()
+        else:
+            bld.env.nm_cache[obj] = { "PUBLIC": set(), "UNDEFINED" : set() }
+
     return ret
 
 
@@ -68,6 +95,104 @@ def real_name(name):
     return name
 
 
+def find_ldd_path(bld, libname, binary):
+    '''find the path to the syslib we will link against'''
+    ret = None
+    if not bld.env.syslib_paths:
+        bld.env.syslib_paths = {}
+    if libname in bld.env.syslib_paths:
+        return bld.env.syslib_paths[libname]
+
+    lddpipe = subprocess.Popen(['ldd', binary], stdout=subprocess.PIPE).stdout
+    for line in lddpipe:
+        line = line.strip()
+        cols = line.split(b" ")
+        if len(cols) < 3 or cols[1] != b"=>":
+            continue
+        if cols[0].startswith(b"libc."):
+            # save this one too
+            bld.env.libc_path = cols[2]
+        if cols[0].startswith(libname):
+            ret = cols[2]
+    bld.env.syslib_paths[libname] = ret
+    return ret
+
+
+# some regular expressions for parsing readelf output
+re_sharedlib = re.compile(b'Shared library: \[(.*)\]')
+# output from readelf could be `Library rpath` or `Libray runpath`
+re_rpath     = re.compile(b'Library (rpath|runpath): \[(.*)\]')
+
+def get_libs(bld, binname):
+    '''find the list of linked libraries for any binary or library
+    binname is the path to the binary/library on disk
+
+    We do this using readelf instead of ldd as we need to avoid recursing
+    into system libraries
+    '''
+
+    # see if we can get the result from the ldd cache
+    if not bld.env.lib_cache:
+        bld.env.lib_cache = {}
+    if binname in bld.env.lib_cache:
+        return bld.env.lib_cache[binname].copy()
+
+    rpath = []
+    libs = set()
+
+    elfpipe = subprocess.Popen(['readelf', '--dynamic', binname], stdout=subprocess.PIPE).stdout
+    for line in elfpipe:
+        m = re_sharedlib.search(line)
+        if m:
+            libs.add(m.group(1))
+        m = re_rpath.search(line)
+        if m:
+            # output from Popen is always bytestr even in py3
+            rpath.extend(m.group(2).split(b":"))
+
+    ret = set()
+    for lib in libs:
+        found = False
+        for r in rpath:
+            path = os.path.join(r, lib)
+            if os.path.exists(path):
+                ret.add(os.path.realpath(path))
+                found = True
+                break
+        if not found:
+            # we didn't find this lib using rpath. It is probably a system
+            # library, so to find the path to it we either need to use ldd
+            # or we need to start parsing /etc/ld.so.conf* ourselves. We'll
+            # use ldd for now, even though it is slow
+            path = find_ldd_path(bld, lib, binname)
+            if path:
+                ret.add(os.path.realpath(path))
+
+    bld.env.lib_cache[binname] = ret.copy()
+
+    return ret
+
+
+def get_libs_recursive(bld, binname, seen):
+    '''find the recursive list of linked libraries for any binary or library
+    binname is the path to the binary/library on disk. seen is a set used
+    to prevent loops
+    '''
+    if binname in seen:
+        return set()
+    ret = get_libs(bld, binname)
+    seen.add(binname)
+    for lib in ret:
+        # we don't want to recurse into system libraries. If a system
+        # library that we use (eg. libcups) happens to use another library
+        # (such as libkrb5) which contains common symbols with our own
+        # libraries, then that is not an error
+        if lib in bld.env.library_dict:
+            ret = ret.union(get_libs_recursive(bld, lib, seen))
+    return ret
+
+
+
 def find_syslib_path(bld, libname, deps):
     '''find the path to the syslib we will link against'''
     # the strategy is to use the targets that depend on the library, and run ldd
@@ -78,20 +203,7 @@ def find_syslib_path(bld, libname, deps):
     if libname == "python":
         libname += bld.env.PYTHON_VERSION
 
-    ret = None
-
-    lddpipe = subprocess.Popen(['ldd', linkpath], stdout=subprocess.PIPE).stdout
-    for line in lddpipe:
-        line = line.strip()
-        cols = line.split(" ")
-        if len(cols) < 3 or cols[1] != "=>":
-            continue
-        if cols[0].startswith("lib%s." % libname.lower()):
-            ret = cols[2]
-        if cols[0].startswith("libc."):
-            # save this one too
-            bld.env.libc_path = cols[2]
-    return ret
+    return find_ldd_path(bld, "lib%s" % libname.lower(), linkpath)
 
 
 def build_symbol_sets(bld, tgt_list):
@@ -113,7 +225,7 @@ def build_symbol_sets(bld, tgt_list):
                 objlist.append(objpath)
                 objmap[objpath] = t
 
-    symbols = symbols_extract(objlist)
+    symbols = symbols_extract(bld, objlist)
     for obj in objlist:
         t = objmap[obj]
         t.public_symbols = t.public_symbols.union(symbols[obj]["PUBLIC"])
@@ -142,7 +254,7 @@ def build_symbol_sets(bld, tgt_list):
             bld.env.public_symbols[name] = t.public_symbols
         if t.samba_type == 'LIBRARY':
             for dep in t.add_objects:
-                t2 = bld.name_to_obj(dep, bld.env)
+                t2 = bld.get_tgen_by_name(dep)
                 bld.ASSERT(t2 is not None, "Library '%s' has unknown dependency '%s'" % (name, dep))
                 bld.env.public_symbols[name] = bld.env.public_symbols[name].union(t2.public_symbols)
 
@@ -155,11 +267,25 @@ def build_symbol_sets(bld, tgt_list):
             bld.env.used_symbols[name] = t.used_symbols
         if t.samba_type == 'LIBRARY':
             for dep in t.add_objects:
-                t2 = bld.name_to_obj(dep, bld.env)
+                t2 = bld.get_tgen_by_name(dep)
                 bld.ASSERT(t2 is not None, "Library '%s' has unknown dependency '%s'" % (name, dep))
                 bld.env.used_symbols[name] = bld.env.used_symbols[name].union(t2.used_symbols)
 
 
+def build_library_dict(bld, tgt_list):
+    '''build the library_dict dictionary'''
+
+    if bld.env.library_dict:
+        return
+
+    bld.env.library_dict = {}
+
+    for t in tgt_list:
+        if t.samba_type in [ 'LIBRARY', 'PYTHON' ]:
+            linkpath = os.path.realpath(t.link_task.outputs[0].abspath(bld.env))
+            bld.env.library_dict[linkpath] = t.sname
+
+
 def build_syslib_sets(bld, tgt_list):
     '''build the public_symbols for all syslibs'''
 
@@ -192,7 +318,7 @@ def build_syslib_sets(bld, tgt_list):
     syslib_paths.append(bld.env.libc_path)
     objmap[bld.env.libc_path] = 'c'
 
-    symbols = symbols_extract(syslib_paths, dynamic=True)
+    symbols = symbols_extract(bld, syslib_paths, dynamic=True)
 
     # keep a map of syslib names to public symbols
     bld.env.syslib_symbols = {}
@@ -239,7 +365,7 @@ def build_autodeps(bld, t):
             if targets[depname[0]] in [ 'SYSLIB' ]:
                 deps.add(depname[0])
                 continue
-            t2 = bld.name_to_obj(depname[0], bld.env)
+            t2 = bld.get_tgen_by_name(depname[0])
             if len(t2.in_library) != 1:
                 deps.add(depname[0])
                 continue
@@ -262,7 +388,7 @@ def build_library_names(bld, tgt_list):
     for t in tgt_list:
         if t.samba_type in [ 'LIBRARY' ]:
             for obj in t.samba_deps_extended:
-                t2 = bld.name_to_obj(obj, bld.env)
+                t2 = bld.get_tgen_by_name(obj)
                 if t2 and t2.samba_type in [ 'SUBSYSTEM', 'ASN1' ]:
                     if not t.sname in t2.in_library:
                         t2.in_library.append(t.sname)
@@ -279,14 +405,14 @@ def check_library_deps(bld, t):
         Logs.warn("WARNING: Target '%s' in multiple libraries: %s" % (t.sname, t.in_library))
 
     for dep in t.autodeps:
-        t2 = bld.name_to_obj(dep, bld.env)
+        t2 = bld.get_tgen_by_name(dep)
         if t2 is None:
             continue
         for dep2 in t2.autodeps:
             if dep2 == name and t.in_library != t2.in_library:
                 Logs.warn("WARNING: mutual dependency %s <=> %s" % (name, real_name(t2.sname)))
                 Logs.warn("Libraries should match. %s != %s" % (t.in_library, t2.in_library))
-                # raise Utils.WafError("illegal mutual dependency")
+                # raise Errors.WafError("illegal mutual dependency")
 
 
 def check_syslib_collisions(bld, tgt_list):
@@ -306,13 +432,13 @@ def check_syslib_collisions(bld, tgt_list):
                 Logs.error("ERROR: Target '%s' has symbols '%s' which is also in syslib '%s'" % (t.sname, common, lib))
                 has_error = True
     if has_error:
-        raise Utils.WafError("symbols in common with system libraries")
+        raise Errors.WafError("symbols in common with system libraries")
 
 
 def check_dependencies(bld, t):
     '''check for depenencies that should be changed'''
 
-    if bld.name_to_obj(t.sname + ".objlist", bld.env):
+    if bld.get_tgen_by_name(t.sname + ".objlist"):
         return
 
     targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
@@ -324,7 +450,7 @@ def check_dependencies(bld, t):
 
     deps = set(t.samba_deps)
     for d in t.samba_deps:
-        if targets[d] in [ 'EMPTY', 'DISABLED', 'SYSLIB' ]:
+        if targets[d] in [ 'EMPTY', 'DISABLED', 'SYSLIB', 'GENERATOR' ]:
             continue
         bld.ASSERT(d in bld.env.public_symbols, "Failed to find symbol list for dependency '%s'" % d)
         diff = remaining.intersection(bld.env.public_symbols[d])
@@ -352,7 +478,7 @@ def check_dependencies(bld, t):
 def check_syslib_dependencies(bld, t):
     '''check for syslib depenencies'''
 
-    if bld.name_to_obj(t.sname + ".objlist", bld.env):
+    if bld.get_tgen_by_name(t.sname + ".objlist"):
         return
 
     sname = real_name(t.sname)
@@ -361,7 +487,8 @@ def check_syslib_dependencies(bld, t):
 
     features = TO_LIST(t.features)
     if 'pyembed' in features or 'pyext' in features:
-        t.unsatisfied_symbols = t.unsatisfied_symbols.difference(bld.env.public_symbols['python'])
+        if 'python' in bld.env.public_symbols:
+            t.unsatisfied_symbols = t.unsatisfied_symbols.difference(bld.env.public_symbols['python'])
 
     needed = {}
     for sym in t.unsatisfied_symbols:
@@ -421,7 +548,7 @@ def symbols_whyneeded(task):
 
     why = Options.options.WHYNEEDED.split(":")
     if len(why) != 2:
-        raise Utils.WafError("usage: WHYNEEDED=TARGET:DEPENDENCY")
+        raise Errors.WafError("usage: WHYNEEDED=TARGET:DEPENDENCY")
     target = why[0]
     subsystem = why[1]
 
@@ -443,33 +570,60 @@ def symbols_whyneeded(task):
         Logs.info("target '%s' uses symbols %s from '%s'" % (target, overlap, subsystem))
 
 
+def report_duplicate(bld, binname, sym, libs, fail_on_error):
+    '''report duplicated symbols'''
+    if sym in ['_init', '_fini', '_edata', '_end', '__bss_start']:
+        return
+    libnames = []
+    for lib in libs:
+        if lib in bld.env.library_dict:
+            libnames.append(bld.env.library_dict[lib])
+        else:
+            libnames.append(lib)
+    if fail_on_error:
+        raise Errors.WafError("%s: Symbol %s linked in multiple libraries %s" % (binname, sym, libnames))
+    else:
+        print("%s: Symbol %s linked in multiple libraries %s" % (binname, sym, libnames))
+
+
+def symbols_dupcheck_binary(bld, binname, fail_on_error):
+    '''check for duplicated symbols in one binary'''
 
-def symbols_dupcheck(task):
+    libs = get_libs_recursive(bld, binname, set())
+    symlist = symbols_extract(bld, libs, dynamic=True)
+
+    symmap = {}
+    for libpath in symlist:
+        for sym in symlist[libpath]['PUBLIC']:
+            if sym == '_GLOBAL_OFFSET_TABLE_':
+                continue
+            if not sym in symmap:
+                symmap[sym] = set()
+            symmap[sym].add(libpath)
+    for sym in symmap:
+        if len(symmap[sym]) > 1:
+            for libpath in symmap[sym]:
+                if libpath in bld.env.library_dict:
+                    report_duplicate(bld, binname, sym, symmap[sym], fail_on_error)
+                    break
+
+def symbols_dupcheck(task, fail_on_error=False):
     '''check for symbols defined in two different subsystems'''
     bld = task.env.bld
     tgt_list = get_tgt_list(bld)
 
     targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
 
-    Logs.info("Checking for duplicate symbols")
-    for sym in bld.env.symbol_map:
-        subsystems = set(bld.env.symbol_map[sym])
-        if len(subsystems) == 1:
-            continue
+    build_library_dict(bld, tgt_list)
+    for t in tgt_list:
+        if t.samba_type == 'BINARY':
+            binname = os_path_relpath(t.link_task.outputs[0].abspath(bld.env), os.getcwd())
+            symbols_dupcheck_binary(bld, binname, fail_on_error)
 
-        if sym in ['main', '_init', '_fini', 'init_samba_module', 'samba_init_module', 'ldb_init_module' ]:
-            # these are expected to be in many subsystems
-            continue
 
-        # if all of them are in system libraries, we can ignore them. This copes
-        # with the duplication between libc, libpthread and libattr
-        all_syslib = True
-        for s in subsystems:
-            if s != 'c' and (not s in targets or targets[s] != 'SYSLIB'):
-                all_syslib = False
-        if all_syslib:
-            continue
-        Logs.info("symbol %s appears in %s" % (sym, subsystems))
+def symbols_dupcheck_fatal(task):
+    '''check for symbols defined in two different subsystems (and fail if duplicates are found)'''
+    symbols_dupcheck(task, fail_on_error=True)
 
 
 def SYMBOL_CHECK(bld):
@@ -494,3 +648,12 @@ def SYMBOL_CHECK(bld):
 
 
 Build.BuildContext.SYMBOL_CHECK = SYMBOL_CHECK
+
+def DUP_SYMBOL_CHECK(bld):
+    if Options.options.DUP_SYMBOLCHECK and bld.env.DEVELOPER:
+        '''check for duplicate symbols'''
+        bld.SET_BUILD_GROUP('syslibcheck')
+        task = bld(rule=symbols_dupcheck_fatal, always=True, name='symbol duplicate checking')
+        task.env.bld = bld
+
+Build.BuildContext.DUP_SYMBOL_CHECK = DUP_SYMBOL_CHECK