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 if bld
.env
.public_symbols
:
103 objlist
= [] # list of object file
104 objmap
= {} # map from object filename to target (subsystem) name
107 t
.public_symbols
= set()
108 t
.undefined_symbols
= set()
109 t
.used_symbols
= set()
110 for tsk
in getattr(t
, 'compiled_tasks', []):
111 for output
in tsk
.outputs
:
112 objpath
= output
.abspath(bld
.env
)
113 objlist
.append(objpath
)
116 symbols
= symbols_extract(objlist
)
119 t
.public_symbols
= t
.public_symbols
.union(symbols
[obj
]["PUBLIC"])
120 t
.undefined_symbols
= t
.undefined_symbols
.union(symbols
[obj
]["UNDEFINED"])
121 t
.used_symbols
= t
.used_symbols
.union(symbols
[obj
]["UNDEFINED"])
123 t
.undefined_symbols
= t
.undefined_symbols
.difference(t
.public_symbols
)
125 # and the reverse map of public symbols to subsystem name
126 bld
.env
.symbol_map
= {}
129 for s
in t
.public_symbols
:
130 if not s
in bld
.env
.symbol_map
:
131 bld
.env
.symbol_map
[s
] = []
132 bld
.env
.symbol_map
[s
].append(real_name(t
.sname
))
134 targets
= LOCAL_CACHE(bld
, 'TARGET_TYPE')
136 bld
.env
.public_symbols
= {}
138 name
= real_name(t
.sname
)
139 if name
in bld
.env
.public_symbols
:
140 bld
.env
.public_symbols
[name
] = bld
.env
.public_symbols
[name
].union(t
.public_symbols
)
142 bld
.env
.public_symbols
[name
] = t
.public_symbols
143 if t
.samba_type
== 'LIBRARY':
144 for dep
in t
.add_objects
:
145 t2
= bld
.name_to_obj(dep
, bld
.env
)
146 bld
.ASSERT(t2
is not None, "Library '%s' has unknown dependency '%s'" % (name
, dep
))
147 bld
.env
.public_symbols
[name
] = bld
.env
.public_symbols
[name
].union(t2
.public_symbols
)
149 bld
.env
.used_symbols
= {}
151 name
= real_name(t
.sname
)
152 if name
in bld
.env
.used_symbols
:
153 bld
.env
.used_symbols
[name
] = bld
.env
.used_symbols
[name
].union(t
.used_symbols
)
155 bld
.env
.used_symbols
[name
] = t
.used_symbols
156 if t
.samba_type
== 'LIBRARY':
157 for dep
in t
.add_objects
:
158 t2
= bld
.name_to_obj(dep
, bld
.env
)
159 bld
.ASSERT(t2
is not None, "Library '%s' has unknown dependency '%s'" % (name
, dep
))
160 bld
.env
.used_symbols
[name
] = bld
.env
.used_symbols
[name
].union(t2
.used_symbols
)
163 def build_syslib_sets(bld
, tgt_list
):
164 '''build the public_symbols for all syslibs'''
166 if bld
.env
.syslib_symbols
:
169 # work out what syslibs we depend on, and what targets those are used in
173 if getattr(t
, 'uselib', []) and t
.samba_type
in [ 'LIBRARY', 'BINARY', 'PYTHON' ]:
175 if lib
in ['PYEMBED', 'PYEXT']:
177 if not lib
in syslibs
:
179 syslibs
[lib
].append(t
)
181 # work out the paths to each syslib
184 path
= find_syslib_path(bld
, lib
, syslibs
[lib
])
186 Logs
.warn("Unable to find syslib path for %s" % lib
)
188 syslib_paths
.append(path
)
189 objmap
[path
] = lib
.lower()
192 syslib_paths
.append(bld
.env
.libc_path
)
193 objmap
[bld
.env
.libc_path
] = 'c'
195 symbols
= symbols_extract(syslib_paths
, dynamic
=True)
197 # keep a map of syslib names to public symbols
198 bld
.env
.syslib_symbols
= {}
200 bld
.env
.syslib_symbols
[lib
] = symbols
[lib
]["PUBLIC"]
202 # add to the map of symbols to dependencies
204 for sym
in symbols
[lib
]["PUBLIC"]:
205 if not sym
in bld
.env
.symbol_map
:
206 bld
.env
.symbol_map
[sym
] = []
207 bld
.env
.symbol_map
[sym
].append(objmap
[lib
])
209 # keep the libc symbols as well, as these are useful for some of the
211 bld
.env
.libc_symbols
= symbols
[bld
.env
.libc_path
]["PUBLIC"]
213 # add to the combined map of dependency name to public_symbols
214 for lib
in bld
.env
.syslib_symbols
:
215 bld
.env
.public_symbols
[objmap
[lib
]] = bld
.env
.syslib_symbols
[lib
]
218 def build_autodeps(bld
, t
):
219 '''build the set of dependencies for a target'''
221 name
= real_name(t
.sname
)
223 targets
= LOCAL_CACHE(bld
, 'TARGET_TYPE')
225 for sym
in t
.undefined_symbols
:
226 if sym
in t
.public_symbols
:
228 if sym
in bld
.env
.symbol_map
:
229 depname
= bld
.env
.symbol_map
[sym
]
230 if depname
== [ name
]:
231 # self dependencies aren't interesting
233 if t
.in_library
== depname
:
234 # no need to depend on the library we are part of
236 if depname
[0] in ['c', 'python']:
237 # these don't go into autodeps
239 if targets
[depname
[0]] in [ 'SYSLIB' ]:
242 t2
= bld
.name_to_obj(depname
[0], bld
.env
)
243 if len(t2
.in_library
) != 1:
246 if t2
.in_library
== t
.in_library
:
247 # if we're part of the same library, we don't need to autodep
249 deps
.add(t2
.in_library
[0])
253 def build_library_names(bld
, tgt_list
):
254 '''add a in_library attribute to all targets that are part of a library'''
256 if bld
.env
.done_build_library_names
:
263 if t
.samba_type
in [ 'LIBRARY' ]:
264 for obj
in t
.samba_deps_extended
:
265 t2
= bld
.name_to_obj(obj
, bld
.env
)
266 if t2
and t2
.samba_type
in [ 'SUBSYSTEM', 'ASN1' ]:
267 if not t
.sname
in t2
.in_library
:
268 t2
.in_library
.append(t
.sname
)
269 bld
.env
.done_build_library_names
= True
272 def check_library_deps(bld
, t
):
273 '''check that all the autodeps that have mutual dependency of this
274 target are in the same library as the target'''
276 name
= real_name(t
.sname
)
278 if len(t
.in_library
) > 1:
279 Logs
.warn("WARNING: Target '%s' in multiple libraries: %s" % (t
.sname
, t
.in_library
))
281 for dep
in t
.autodeps
:
282 t2
= bld
.name_to_obj(dep
, bld
.env
)
285 for dep2
in t2
.autodeps
:
286 if dep2
== name
and t
.in_library
!= t2
.in_library
:
287 Logs
.warn("WARNING: mutual dependency %s <=> %s" % (name
, real_name(t2
.sname
)))
288 Logs
.warn("Libraries should match. %s != %s" % (t
.in_library
, t2
.in_library
))
289 # raise Utils.WafError("illegal mutual dependency")
292 def check_syslib_collisions(bld
, tgt_list
):
293 '''check if a target has any symbol collisions with a syslib
295 We do not want any code in Samba to use a symbol name from a
296 system library. The chance of that causing problems is just too
297 high. Note that libreplace uses a rep_XX approach of renaming
303 for lib
in bld
.env
.syslib_symbols
:
304 common
= t
.public_symbols
.intersection(bld
.env
.syslib_symbols
[lib
])
306 Logs
.error("ERROR: Target '%s' has symbols '%s' which is also in syslib '%s'" % (t
.sname
, common
, lib
))
309 raise Utils
.WafError("symbols in common with system libraries")
312 def check_dependencies(bld
, t
):
313 '''check for depenencies that should be changed'''
315 if bld
.name_to_obj(t
.sname
+ ".objlist", bld
.env
):
318 targets
= LOCAL_CACHE(bld
, 'TARGET_TYPE')
320 remaining
= t
.undefined_symbols
.copy()
321 remaining
= remaining
.difference(t
.public_symbols
)
323 sname
= real_name(t
.sname
)
325 deps
= set(t
.samba_deps
)
326 for d
in t
.samba_deps
:
327 if targets
[d
] in [ 'EMPTY', 'DISABLED', 'SYSLIB' ]:
329 bld
.ASSERT(d
in bld
.env
.public_symbols
, "Failed to find symbol list for dependency '%s'" % d
)
330 diff
= remaining
.intersection(bld
.env
.public_symbols
[d
])
331 if not diff
and targets
[sname
] != 'LIBRARY':
332 Logs
.info("Target '%s' has no dependency on %s" % (sname
, d
))
334 remaining
= remaining
.difference(diff
)
336 t
.unsatisfied_symbols
= set()
338 for sym
in remaining
:
339 if sym
in bld
.env
.symbol_map
:
340 dep
= bld
.env
.symbol_map
[sym
]
341 if not dep
[0] in needed
:
342 needed
[dep
[0]] = set()
343 needed
[dep
[0]].add(sym
)
345 t
.unsatisfied_symbols
.add(sym
)
348 Logs
.info("Target '%s' should add dep '%s' for symbols %s" % (sname
, dep
, " ".join(needed
[dep
])))
352 def check_syslib_dependencies(bld
, t
):
353 '''check for syslib depenencies'''
355 if bld
.name_to_obj(t
.sname
+ ".objlist", bld
.env
):
358 sname
= real_name(t
.sname
)
362 features
= TO_LIST(t
.features
)
363 if 'pyembed' in features
or 'pyext' in features
:
364 t
.unsatisfied_symbols
= t
.unsatisfied_symbols
.difference(bld
.env
.public_symbols
['python'])
367 for sym
in t
.unsatisfied_symbols
:
368 if sym
in bld
.env
.symbol_map
:
369 dep
= bld
.env
.symbol_map
[sym
][0]
372 if not dep
in needed
:
379 Logs
.info("Target '%s' should add syslib dep '%s' for symbols %s" % (sname
, dep
, " ".join(needed
[dep
])))
382 debug("deps: Target '%s' has unsatisfied symbols: %s" % (sname
, " ".join(remaining
)))
386 def symbols_symbolcheck(task
):
387 '''check the internal dependency lists'''
389 tgt_list
= get_tgt_list(bld
)
391 build_symbol_sets(bld
, tgt_list
)
392 build_library_names(bld
, tgt_list
)
396 if getattr(t
, 'source', ''):
397 build_autodeps(bld
, t
)
400 check_dependencies(bld
, t
)
403 check_library_deps(bld
, t
)
405 def symbols_syslibcheck(task
):
406 '''check the syslib dependencies'''
408 tgt_list
= get_tgt_list(bld
)
410 build_syslib_sets(bld
, tgt_list
)
411 check_syslib_collisions(bld
, tgt_list
)
414 check_syslib_dependencies(bld
, t
)
417 def symbols_whyneeded(task
):
418 """check why 'target' needs to link to 'subsystem'"""
420 tgt_list
= get_tgt_list(bld
)
422 why
= Options
.options
.WHYNEEDED
.split(":")
424 raise Utils
.WafError("usage: WHYNEEDED=TARGET:DEPENDENCY")
428 build_symbol_sets(bld
, tgt_list
)
429 build_library_names(bld
, tgt_list
)
430 build_syslib_sets(bld
, tgt_list
)
432 Logs
.info("Checking why %s needs to link to %s" % (target
, subsystem
))
433 if not target
in bld
.env
.used_symbols
:
434 Logs
.warn("unable to find target '%s' in used_symbols dict" % target
)
436 if not subsystem
in bld
.env
.public_symbols
:
437 Logs
.warn("unable to find subsystem '%s' in public_symbols dict" % subsystem
)
439 overlap
= bld
.env
.used_symbols
[target
].intersection(bld
.env
.public_symbols
[subsystem
])
441 Logs
.info("target '%s' doesn't use any public symbols from '%s'" % (target
, subsystem
))
443 Logs
.info("target '%s' uses symbols %s from '%s'" % (target
, overlap
, subsystem
))
447 def symbols_dupcheck(task
):
448 '''check for symbols defined in two different subsystems'''
450 tgt_list
= get_tgt_list(bld
)
452 targets
= LOCAL_CACHE(bld
, 'TARGET_TYPE')
454 Logs
.info("Checking for duplicate symbols")
455 for sym
in bld
.env
.symbol_map
:
456 subsystems
= set(bld
.env
.symbol_map
[sym
])
457 if len(subsystems
) == 1:
460 if sym
in ['main', '_init', '_fini', 'init_samba_module', 'samba_init_module', 'ldb_init_module' ]:
461 # these are expected to be in many subsystems
464 # if all of them are in system libraries, we can ignore them. This copes
465 # with the duplication between libc, libpthread and libattr
468 if s
!= 'c' and (not s
in targets
or targets
[s
] != 'SYSLIB'):
472 Logs
.info("symbol %s appears in %s" % (sym
, subsystems
))
475 def SYMBOL_CHECK(bld
):
476 '''check our dependency lists'''
477 if Options
.options
.SYMBOLCHECK
:
478 bld
.SET_BUILD_GROUP('symbolcheck')
479 task
= bld(rule
=symbols_symbolcheck
, always
=True, name
='symbol checking')
482 bld
.SET_BUILD_GROUP('syslibcheck')
483 task
= bld(rule
=symbols_syslibcheck
, always
=True, name
='syslib checking')
486 bld
.SET_BUILD_GROUP('syslibcheck')
487 task
= bld(rule
=symbols_dupcheck
, always
=True, name
='symbol duplicate checking')
490 if Options
.options
.WHYNEEDED
:
491 bld
.SET_BUILD_GROUP('syslibcheck')
492 task
= bld(rule
=symbols_whyneeded
, always
=True, name
='check why a dependency is needed')
496 Build
.BuildContext
.SYMBOL_CHECK
= SYMBOL_CHECK