xref: /openbmc/qemu/scripts/undefsym.py (revision b15e402f)
1#!/usr/bin/env python3
2
3# Before a shared module's DSO is produced, a static library is built for it
4# and passed to this script.  The script generates -Wl,-u options to force
5# the inclusion of symbol from libqemuutil.a if the shared modules need them,
6# This is necessary because the modules may use functions not needed by the
7# executable itself, which would cause the function to not be linked in.
8# Then the DSO loading would fail because of the missing symbol.
9
10
11import sys
12import subprocess
13
14def filter_lines_set(stdout, from_staticlib):
15    linesSet = set()
16    for line in stdout.splitlines():
17        tokens = line.split(b' ')
18        if len(tokens) >= 1:
19            if len(tokens) > 1:
20                if from_staticlib and tokens[1] == b'U':
21                    continue
22                if not from_staticlib and tokens[1] != b'U':
23                    continue
24            new_line = b'-Wl,-u,' + tokens[0]
25            if not new_line in linesSet:
26                linesSet.add(new_line)
27    return linesSet
28
29def main(args):
30    if len(args) <= 3:
31        sys.exit(0)
32
33    nm = args[1]
34    staticlib = args[2]
35    pc = subprocess.run([nm, "-P", "-g", staticlib], stdout=subprocess.PIPE)
36    if pc.returncode != 0:
37        sys.exit(1)
38    staticlib_syms = filter_lines_set(pc.stdout, True)
39
40    shared_modules = args[3:]
41    pc = subprocess.run([nm, "-P", "-g"] + shared_modules, stdout=subprocess.PIPE)
42    if pc.returncode != 0:
43        sys.exit(1)
44    modules_undef_syms = filter_lines_set(pc.stdout, False)
45    lines = sorted(staticlib_syms.intersection(modules_undef_syms))
46    sys.stdout.buffer.write(b'\n'.join(lines))
47
48if __name__ == "__main__":
49    main(sys.argv)
50