1 # a waf tool to extract symbols from object files or libraries
2 # using nm, producing a set of exposed defined/undefined symbols
4 import Utils, Build, subprocess, Logs
5 from samba_wildcard import fake_build_environment
6 from samba_utils import *
8 # these are the data structures used in symbols.py:
10 # bld.env.symbol_map : dictionary mapping public symbol names to list of
11 # subsystem names where that symbol exists
13 # t.in_library : list of libraries that t is in
15 # bld.env.public_symbols: set of public symbols for each subsystem
16 # bld.env.used_symbols : set of used symbols for each subsystem
18 # bld.env.syslib_symbols: dictionary mapping system library name to set of symbols
21 # LOCAL_CACHE(bld, 'TARGET_TYPE') : dictionary mapping subsystem name to target type
23 def symbols_extract(objfiles, dynamic=False):
24 '''extract symbols from objfile, returning a dictionary containing
25 the set of undefined and public symbols for each file'''
31 # needed for some .so files
35 nmpipe = subprocess.Popen(cmd, stdout=subprocess.PIPE).stdout
36 if len(objfiles) == 1:
37 filename = objfiles[0]
38 ret[filename] = { "PUBLIC": set(), "UNDEFINED" : set()}
42 if line.endswith(':'):
44 ret[filename] = { "PUBLIC": set(), "UNDEFINED" : set() }
46 cols = line.split(" ")
49 # see if the line starts with an address
56 if symbol_type in "BDGTRVWSi":
58 ret[filename]["PUBLIC"].add(symbol)
59 elif symbol_type in "U":
60 ret[filename]["UNDEFINED"].add(symbol)
66 if name.find(".objlist") != -1:
71 def find_syslib_path(bld, libname, deps):
72 '''find the path to the syslib we will link against'''
73 # the strategy is to use the targets that depend on the library, and run ldd
74 # on it to find the real location of the library that is used
76 linkpath = deps[0].link_task.outputs[0].abspath(bld.env)
78 if libname == "python":
79 libname += bld.env.PYTHON_VERSION
83 lddpipe = subprocess.Popen(['ldd', linkpath], stdout=subprocess.PIPE).stdout
86 cols = line.split(" ")
87 if len(cols) < 3 or cols[1] != "=>":
89 if cols[0].startswith("lib%s." % libname.lower()):
91 if cols[0].startswith("libc."):
93 bld.env.libc_path = cols[2]
97 def build_symbol_sets(bld, tgt_list):
98 '''build the public_symbols and undefined_symbols attributes for each target'''
100 objlist = [] # list of object file
101 objmap = {} # map from object filename to target (subsystem) name
105 t.public_symbols = set()
106 t.undefined_symbols = set()
107 t.used_symbols = set()
108 for tsk in getattr(t, 'compiled_tasks', []):
109 for output in tsk.outputs:
110 objpath = output.abspath(bld.env)
111 objlist.append(objpath)
114 symbols = symbols_extract(objlist)
117 t.public_symbols = t.public_symbols.union(symbols[obj]["PUBLIC"])
118 t.undefined_symbols = t.undefined_symbols.union(symbols[obj]["UNDEFINED"])
119 t.used_symbols = t.used_symbols.union(symbols[obj]["UNDEFINED"])
121 t.undefined_symbols = t.undefined_symbols.difference(t.public_symbols)
123 # and the reverse map of public symbols to subsystem name
124 bld.env.symbol_map = {}
127 for s in t.public_symbols:
128 if not s in bld.env.symbol_map:
129 bld.env.symbol_map[s] = []
130 bld.env.symbol_map[s].append(real_name(t.sname))
132 targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
134 bld.env.public_symbols = {}
136 name = real_name(t.sname)
137 if name in bld.env.public_symbols:
138 bld.env.public_symbols[name] = bld.env.public_symbols[name].union(t.public_symbols)
140 bld.env.public_symbols[name] = t.public_symbols
141 if t.samba_type == 'LIBRARY':
142 for dep in t.add_objects:
143 t2 = bld.name_to_obj(dep, bld.env)
144 bld.ASSERT(t2 is not None, "Library '%s' has unknown dependency '%s'" % (name, dep))
145 bld.env.public_symbols[name] = bld.env.public_symbols[name].union(t2.public_symbols)
147 bld.env.used_symbols = {}
149 name = real_name(t.sname)
150 if name in bld.env.used_symbols:
151 bld.env.used_symbols[name] = bld.env.used_symbols[name].union(t.used_symbols)
153 bld.env.used_symbols[name] = t.used_symbols
154 if t.samba_type == 'LIBRARY':
155 for dep in t.add_objects:
156 t2 = bld.name_to_obj(dep, bld.env)
157 bld.ASSERT(t2 is not None, "Library '%s' has unknown dependency '%s'" % (name, dep))
158 bld.env.used_symbols[name] = bld.env.used_symbols[name].union(t2.used_symbols)
161 def build_syslib_sets(bld, tgt_list):
162 '''build the public_symbols for all syslibs'''
164 # work out what syslibs we depend on, and what targets those are used in
168 if getattr(t, 'uselib', []) and t.samba_type in [ 'LIBRARY', 'BINARY', 'PYTHON' ]:
170 if lib in ['PYEMBED', 'PYEXT']:
172 if not lib in syslibs:
174 syslibs[lib].append(t)
176 # work out the paths to each syslib
179 path = find_syslib_path(bld, lib, syslibs[lib])
181 Logs.warn("Unable to find syslib path for %s" % lib)
183 syslib_paths.append(path)
184 objmap[path] = lib.lower()
187 syslib_paths.append(bld.env.libc_path)
188 objmap[bld.env.libc_path] = 'c'
190 symbols = symbols_extract(syslib_paths, dynamic=True)
192 # keep a map of syslib names to public symbols
193 bld.env.syslib_symbols = {}
195 bld.env.syslib_symbols[lib] = symbols[lib]["PUBLIC"]
197 # add to the map of symbols to dependencies
199 for sym in symbols[lib]["PUBLIC"]:
200 if not sym in bld.env.symbol_map:
201 bld.env.symbol_map[sym] = []
202 bld.env.symbol_map[sym].append(objmap[lib])
204 # keep the libc symbols as well, as these are useful for some of the
206 bld.env.libc_symbols = symbols[bld.env.libc_path]["PUBLIC"]
208 # add to the combined map of dependency name to public_symbols
209 for lib in bld.env.syslib_symbols:
210 bld.env.public_symbols[objmap[lib]] = bld.env.syslib_symbols[lib]
212 def build_autodeps(bld, t):
213 '''build the set of dependencies for a target'''
215 name = real_name(t.sname)
217 targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
219 for sym in t.undefined_symbols:
220 if sym in t.public_symbols:
222 if sym in bld.env.symbol_map:
223 depname = bld.env.symbol_map[sym]
224 if depname == [ name ]:
225 # self dependencies aren't interesting
227 if t.in_library == depname:
228 # no need to depend on the library we are part of
230 if depname[0] in ['c', 'python']:
231 # these don't go into autodeps
233 if targets[depname[0]] in [ 'SYSLIB' ]:
236 t2 = bld.name_to_obj(depname[0], bld.env)
237 if len(t2.in_library) != 1:
240 if t2.in_library == t.in_library:
241 # if we're part of the same library, we don't need to autodep
243 deps.add(t2.in_library[0])
247 def build_library_names(bld, tgt_list):
248 '''add a in_library attribute to all targets that are part of a library'''
253 if t.samba_type in [ 'LIBRARY' ]:
254 for obj in t.samba_deps_extended:
255 t2 = bld.name_to_obj(obj, bld.env)
256 if t2 and t2.samba_type in [ 'SUBSYSTEM', 'ASN1' ]:
257 if not t.sname in t2.in_library:
258 t2.in_library.append(t.sname)
261 def check_library_deps(bld, t):
262 '''check that all the autodeps that have mutual dependency of this
263 target are in the same library as the target'''
265 name = real_name(t.sname)
267 if len(t.in_library) > 1:
268 Logs.warn("WARNING: Target '%s' in multiple libraries: %s" % (t.sname, t.in_library))
270 for dep in t.autodeps:
271 t2 = bld.name_to_obj(dep, bld.env)
274 for dep2 in t2.autodeps:
275 if dep2 == name and t.in_library != t2.in_library:
276 Logs.warn("WARNING: mutual dependency %s <=> %s" % (name, real_name(t2.sname)))
277 Logs.warn("Libraries should match. %s != %s" % (t.in_library, t2.in_library))
278 # raise Utils.WafError("illegal mutual dependency")
281 def check_syslib_collisions(bld, tgt_list):
282 '''check if a target has any symbol collisions with a syslib
284 We do not want any code in Samba to use a symbol name from a
285 system library. The chance of that causing problems is just too
286 high. Note that libreplace uses a rep_XX approach of renaming
292 for lib in bld.env.syslib_symbols:
293 common = t.public_symbols.intersection(bld.env.syslib_symbols[lib])
295 Logs.error("ERROR: Target '%s' has symbols '%s' which is also in syslib '%s'" % (t.sname, common, lib))
298 raise Utils.WafError("symbols in common with system libraries")
301 def check_dependencies(bld, t):
302 '''check for depenencies that should be changed'''
304 if bld.name_to_obj(t.sname + ".objlist", bld.env):
307 targets = LOCAL_CACHE(bld, 'TARGET_TYPE')
309 remaining = t.undefined_symbols.copy()
310 remaining = remaining.difference(t.public_symbols)
312 sname = real_name(t.sname)
314 deps = set(t.samba_deps)
315 for d in t.samba_deps:
316 if targets[d] in [ 'EMPTY', 'DISABLED', 'SYSLIB' ]:
318 bld.ASSERT(d in bld.env.public_symbols, "Failed to find symbol list for dependency '%s'" % d)
319 diff = remaining.intersection(bld.env.public_symbols[d])
320 if not diff and targets[sname] != 'LIBRARY':
321 Logs.info("Target '%s' has no dependency on %s" % (sname, d))
323 remaining = remaining.difference(diff)
325 t.unsatisfied_symbols = set()
327 for sym in remaining:
328 if sym in bld.env.symbol_map:
329 dep = bld.env.symbol_map[sym]
330 if not dep[0] in needed:
331 needed[dep[0]] = set()
332 needed[dep[0]].add(sym)
334 t.unsatisfied_symbols.add(sym)
337 Logs.info("Target '%s' should add dep '%s' for symbols %s" % (sname, dep, " ".join(needed[dep])))
341 def check_syslib_dependencies(bld, t):
342 '''check for syslib depenencies'''
344 if bld.name_to_obj(t.sname + ".objlist", bld.env):
347 sname = real_name(t.sname)
351 features = TO_LIST(t.features)
352 if 'pyembed' in features or 'pyext' in features:
353 t.unsatisfied_symbols = t.unsatisfied_symbols.difference(bld.env.public_symbols['python'])
356 for sym in t.unsatisfied_symbols:
357 if sym in bld.env.symbol_map:
358 dep = bld.env.symbol_map[sym][0]
361 if not dep in needed:
368 Logs.info("Target '%s' should add syslib dep '%s' for symbols %s" % (sname, dep, " ".join(needed[dep])))
371 debug("deps: Target '%s' has unsatisfied symbols: %s" % (sname, " ".join(remaining)))
375 def symbols_symbolcheck(task):
376 '''check the internal dependency lists'''
378 tgt_list = get_tgt_list(bld)
380 build_symbol_sets(bld, tgt_list)
381 build_library_names(bld, tgt_list)
385 if getattr(t, 'source', ''):
386 build_autodeps(bld, t)
389 check_dependencies(bld, t)
392 check_library_deps(bld, t)
394 def symbols_syslibcheck(task):
395 '''check the syslib dependencies'''
397 tgt_list = get_tgt_list(bld)
399 build_syslib_sets(bld, tgt_list)
400 check_syslib_collisions(bld, tgt_list)
403 check_syslib_dependencies(bld, t)
406 def check_why_needed(bld, target, subsystem):
407 """check why 'target' needs to link to 'subsystem'"""
408 Logs.info("Checking why %s needs to link to %s" % (target, subsystem))
409 if not target in bld.env.used_symbols:
410 Logs.warn("unable to find target %s in used_symbols dict" % target)
412 if not subsystem in bld.env.public_symbols:
413 Logs.warn("unable to find subsystem %s in public_symbols dict" % subsystem)
415 overlap = bld.env.used_symbols[target].intersection(bld.env.public_symbols[subsystem])
417 Logs.info("target %s doesn't use any public symbols from %s" % (target, subsystem))
419 Logs.info("target %s uses %s from %s" % (target, overlap, subsystem))
423 def symbols_dupcheck(task):
424 '''check for symbols defined in two different subsystems'''
426 tgt_list = get_tgt_list(bld)
428 Logs.info("Checking for duplicate symbols")
429 for sym in bld.env.symbol_map:
430 subsystems = bld.env.symbol_map[sym]
431 if len(subsystems) == 1:
433 Logs.info("symbol %s appears in %s" % (sym, subsystems))
435 # use this type of call to find why a library is needed
436 check_why_needed(bld, 'smbd/smbd', 'gensec')
439 def SYMBOL_CHECK(bld):
440 '''check our dependency lists'''
441 if Options.options.SYMBOLCHECK:
442 bld.SET_BUILD_GROUP('symbolcheck')
443 task = bld(rule=symbols_symbolcheck, always=True, name='symbol checking')
446 bld.SET_BUILD_GROUP('syslibcheck')
447 task = bld(rule=symbols_syslibcheck, always=True, name='syslib checking')
450 bld.SET_BUILD_GROUP('syslibcheck')
451 task = bld(rule=symbols_dupcheck, always=True, name='symbol duplicate checking')
454 Build.BuildContext.SYMBOL_CHECK = SYMBOL_CHECK