xref: /openbmc/qemu/scripts/undefsym.py (revision 05caa062)
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) >= 2:
19            if from_staticlib and tokens[1] == b'U':
20                continue
21            if not from_staticlib and tokens[1] != b'U':
22                continue
23            new_line = b'-Wl,-u,' + tokens[0]
24            if not new_line in linesSet:
25                linesSet.add(new_line)
26    return linesSet
27
28def main(args):
29    if len(args) <= 3:
30        sys.exit(0)
31
32    nm = args[1]
33    staticlib = args[2]
34    pc = subprocess.run([nm, "-P", "-g", staticlib], stdout=subprocess.PIPE)
35    if pc.returncode != 0:
36        sys.exit(1)
37    staticlib_syms = filter_lines_set(pc.stdout, True)
38
39    shared_modules = args[3:]
40    pc = subprocess.run([nm, "-P", "-g"] + shared_modules, stdout=subprocess.PIPE)
41    if pc.returncode != 0:
42        sys.exit(1)
43    modules_undef_syms = filter_lines_set(pc.stdout, False)
44    lines = sorted(staticlib_syms.intersection(modules_undef_syms))
45    sys.stdout.buffer.write(b'\n'.join(lines))
46
47if __name__ == "__main__":
48    main(sys.argv)
49