"""Whole-image link check: does every program find every symbol it needs? For each ELF object under a root filesystem, follow DT_NEEDED through the libraries that root actually provides (by soname, as the dynamic linker would), gather the dynamic symbols that closure defines, and report any non-weak undefined symbol nothing defines. DT_NEEDED resolving is necessary but not sufficient -- a newer program on an older library resolves its soname and then dies on a missing symbol. This catches that, statically. python3 linkcheck.py [--quiet] """ import os, sys, struct sys.path.insert(0, os.path.dirname(__file__)) from elfsyms import symbols def needed(path): try: d = open(path, 'rb').read() except Exception: return None if d[:4] != b'\x7fELF' or len(d) < 52: return None try: sh = struct.unpack(' real path, searching the dynamic linker's directories first LIBDIRS = ['lib', 'usr/lib', 'usr/X11R6/lib', 'usr/lib/firefox-1.0'] soname = {} for ld in LIBDIRS: base = os.path.join(root, ld) if not os.path.isdir(base): continue for f in os.listdir(base): p = os.path.join(base, f) if '.so' in f and f not in soname and os.path.exists(p): soname[f] = os.path.realpath(p) if not os.path.islink(p) else os.path.join(base, os.readlink(p)) if not os.readlink(p).startswith('/') else os.path.join(root, os.readlink(p).lstrip('/')) symcache = {} def syms(p): if p not in symcache: symcache[p] = symbols(p) or (set(), set()) return symcache[p] def closure(p, seen=None): seen = seen if seen is not None else set() for lib in needed(p) or []: q = soname.get(lib) if q and q not in seen and os.path.exists(q): seen.add(q); closure(q, seen) return seen problems = {} count = 0 def etype(p): try: with open(p, 'rb') as f: h = f.read(18) return struct.unpack('