|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Dynamic linking consistency checker |
| 4 | +# hacked together by _that@xda, 2014-12 to 2022-03 |
| 5 | + |
| 6 | +import os |
| 7 | +import string |
| 8 | +import argparse |
| 9 | + |
| 10 | + |
| 11 | +def ldcheck(files, libpath, printalldefined, printresolved, demangle): |
| 12 | + nmopts = "-C" if demangle else "" |
| 13 | + libs = files |
| 14 | + libswithpath = [] |
| 15 | + |
| 16 | + # find all dependencies |
| 17 | + for filename in libs: |
| 18 | + if not os.path.isfile(filename) and libpath: |
| 19 | + filename = findlibraryinpath(filename, libpath) |
| 20 | + libswithpath.append(filename) |
| 21 | + with os.popen("readelf -d {0} | grep '\(NEEDED\)' | sed -r 's/.*\[(.*)\]/\\1/'".format(filename)) as f: |
| 22 | + for line in f: |
| 23 | + dependency = line.rstrip() |
| 24 | + if dependency not in libs: |
| 25 | + libs.append(dependency) |
| 26 | + |
| 27 | + print("libs:", libswithpath) |
| 28 | + libsused = set((libswithpath[0],)) |
| 29 | + |
| 30 | + # read all defined symbols |
| 31 | + syms = {} |
| 32 | + for filename in libswithpath: |
| 33 | + for sym in readsymbols(filename, nmopts): |
| 34 | + if sym["type"] != "U": |
| 35 | + # TODO: support symbols defined by multiple libs properly, and weak symbols |
| 36 | + # TODO: more proper handling of symbol versions, see https://maskray.me/blog/2020-11-26-all-about-symbol-versioning |
| 37 | + if "@@" in sym["name"]: |
| 38 | + syms[sym["name"].replace("@@", "@")] = {"type": sym["type"], "file": filename} |
| 39 | + syms[sym["name"].split("@@")[0]] = {"type": sym["type"], "file": filename} |
| 40 | + else: |
| 41 | + syms[sym["name"]] = {"type": sym["type"], "file": filename} |
| 42 | + |
| 43 | + # resolve unresolved |
| 44 | + for filename in libswithpath: |
| 45 | + for sym in readsymbols(filename, nmopts): |
| 46 | + if sym["type"] == "U": |
| 47 | + resolved = syms.get(sym["name"]) |
| 48 | + if resolved: |
| 49 | + if filename in libsused: |
| 50 | + libsused.add(resolved["file"]) |
| 51 | + if printresolved: |
| 52 | + print("{0:25} {1:25} {2} {3}".format(filename, resolved["file"], resolved["type"], sym["name"])) |
| 53 | + else: |
| 54 | + print("{0:25} {1:25} U {2}".format(filename, "UNRESOLVED #####", sym["name"])) |
| 55 | + else: |
| 56 | + if printalldefined: |
| 57 | + print("{0:25} {1}".format(filename, sym["line"])) |
| 58 | + |
| 59 | + unused = set(libswithpath) - libsused |
| 60 | + if unused: |
| 61 | + print("unused:", unused) |
| 62 | + |
| 63 | + |
| 64 | +def findlibraryinpath(filename, searchpath): |
| 65 | + for d in searchpath.split(os.pathsep): |
| 66 | + filepath = os.path.join(d, filename) |
| 67 | + if os.path.isfile(filepath): |
| 68 | + return filepath |
| 69 | + return filename |
| 70 | + |
| 71 | + |
| 72 | +""" |
| 73 | +output format of nm: |
| 74 | +00003004 A __bss_start |
| 75 | + U __cxa_atexit |
| 76 | +""" |
| 77 | +def readsymbol(line): |
| 78 | + s = line if line[0] == " " else line.lstrip(string.hexdigits) |
| 79 | + s = s.lstrip() |
| 80 | + return {"type": s[0], "name": s[2:], "line": line} |
| 81 | + |
| 82 | + |
| 83 | +def readsymbols(filename, nmopts=""): |
| 84 | + with os.popen("nm -D {0} {1}".format(nmopts, filename)) as f: |
| 85 | + for line in f: |
| 86 | + yield readsymbol(line.rstrip()) |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + parser = argparse.ArgumentParser(description='Check dynamic linkage consistency by matching defined vs unresolved symbols.') |
| 90 | + parser.add_argument('files', metavar='FILE', nargs='+', help="a dynamically linked executable or library.") |
| 91 | + parser.add_argument('-p', '--path', help="Search path for libraries (use like LD_LIBRARY_PATH)") |
| 92 | + parser.add_argument('-r', '--resolved', action='store_true', help="Print resolved symbols. By default only unresolved symbols are printed.") |
| 93 | + parser.add_argument('-a', '--alldefined', action='store_true', help="Print all defined symbols") |
| 94 | + parser.add_argument('-d', '--demangle', action='store_true', help="Demangle C++ names") |
| 95 | + |
| 96 | + args = parser.parse_args() |
| 97 | + ldcheck(args.files, args.path, args.alldefined, args.resolved, args.demangle) |
0 commit comments