|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# This script extracts symbols from an LLVM bitcode file, and uses |
| 4 | +# them to generate a whitelist of symbols that should be compiled with |
| 5 | +# Emterpreter. |
| 6 | +# |
| 7 | +# There are two sets of regular expressions loaded from JSON files: |
| 8 | +# "include" expressions are used to find functions that should be |
| 9 | +# Emterpreted, and "exclude" expressions are used to prune out |
| 10 | +# functions that are too greedily included. |
| 11 | + |
| 12 | +import sys |
| 13 | +import os |
| 14 | +import subprocess |
| 15 | +import re |
| 16 | +import json |
| 17 | + |
| 18 | +# Get any relevant info from the environment |
| 19 | +# ------------------------------------------ |
| 20 | + |
| 21 | +env_verbose = os.getenv('V', '0').strip() |
| 22 | +env_nm = os.getenv('NM', 'llvm-nm') |
| 23 | + |
| 24 | +# Separate out separate elements of command line |
| 25 | +nm = env_nm.split() |
| 26 | + |
| 27 | +# Verbosity |
| 28 | +verbose = (env_verbose.strip() is not '0') |
| 29 | + |
| 30 | +# Process command line options |
| 31 | +# ---------------------------- |
| 32 | +# |
| 33 | +# Each option absorbs all subsequent arguments up to the next option. |
| 34 | +# Options are identified by the fact they start with "--". |
| 35 | + |
| 36 | +option = None |
| 37 | +options = {} |
| 38 | +for arg in sys.argv[1:]: |
| 39 | + if arg.startswith('--'): |
| 40 | + option = arg[2:] |
| 41 | + options[option] = [] |
| 42 | + else: |
| 43 | + if option is None: |
| 44 | + print('ERROR: unrecognized option \'{}\''.format(arg)) |
| 45 | + sys.exit(1) |
| 46 | + options[option].append(arg) |
| 47 | + |
| 48 | +# Generate include/exclude predicate |
| 49 | +# ---------------------------------- |
| 50 | + |
| 51 | +def build_regexp_from_json_files(paths): |
| 52 | + expressions = [] |
| 53 | + for p in paths: |
| 54 | + with file(p) as fp: |
| 55 | + expressions += json.load(fp) |
| 56 | + if len(expressions) is 0: |
| 57 | + return None |
| 58 | + return '(' + '|'.join(expressions) + ')' |
| 59 | + |
| 60 | +exclude_re = None |
| 61 | +if 'exclude' in options: |
| 62 | + exclude_re = build_regexp_from_json_files(options['exclude']) |
| 63 | +if exclude_re is not None: |
| 64 | + exclude_re = re.compile(exclude_re) |
| 65 | + |
| 66 | +include_re = None |
| 67 | +if 'include' in options: |
| 68 | + include_re = build_regexp_from_json_files(options['include']) |
| 69 | +if include_re is not None: |
| 70 | + include_re = re.compile(include_re) |
| 71 | + |
| 72 | +def is_emterpreted(symbol): |
| 73 | + if include_re is not None: |
| 74 | + if include_re.search(symbol) is None: |
| 75 | + return False |
| 76 | + if exclude_re is not None: |
| 77 | + if exclude_re.search(symbol) is not None: |
| 78 | + return False |
| 79 | + return True |
| 80 | + |
| 81 | +# Generate emterpreter whitelist |
| 82 | +# ------------------------------ |
| 83 | + |
| 84 | +# Run llvm-nm and yield symbol names from its standard output |
| 85 | +def iter_archive_symbols(archive): |
| 86 | + command = nm + ['-B', archive] |
| 87 | + if verbose: |
| 88 | + print(' '.join(command)) |
| 89 | + output = subprocess.check_output(command) |
| 90 | + for line in output.splitlines(): |
| 91 | + line = line.strip() |
| 92 | + |
| 93 | + if len(line) is 0: |
| 94 | + # Empty line |
| 95 | + continue |
| 96 | + |
| 97 | + if line.endswith(':'): |
| 98 | + # This line names a code object file (foo.o), so ignore it |
| 99 | + continue |
| 100 | + |
| 101 | + # Split the line into elements. The layout of the line should |
| 102 | + # be something like: "[<address>] <type> <name>" |
| 103 | + line = line.split() |
| 104 | + |
| 105 | + # Only "text" symbols, i.e. code defined in the current |
| 106 | + # object, should be emterpreted. |
| 107 | + if len(line) < 3 or line[1].lower() != 't': |
| 108 | + continue |
| 109 | + |
| 110 | + # Put an "_" before each symbol to get the name as generated |
| 111 | + # by emscripten |
| 112 | + yield ('_' + line[2]) |
| 113 | + |
| 114 | + |
| 115 | +# Generate list of all symbols that should be whitelisted |
| 116 | +object_path = options['input'][0] |
| 117 | +whitelist_symbols = [] |
| 118 | +blacklist_symbols = [] |
| 119 | +for symbol in iter_archive_symbols(object_path): |
| 120 | + if is_emterpreted(symbol): |
| 121 | + whitelist_symbols.append(symbol) |
| 122 | + else: |
| 123 | + blacklist_symbols.append(symbol) |
| 124 | + |
| 125 | +print("Compiling {} functions".format(len(blacklist_symbols))) |
| 126 | +print("Emterpreting {} functions".format(len(whitelist_symbols))) |
| 127 | + |
| 128 | +# Put into a JSON file |
| 129 | +if len(options['output']) > 0: |
| 130 | + json_path = options['output'][0] |
| 131 | + with file(json_path, 'w') as fp: |
| 132 | + json.dump(whitelist_symbols, fp, indent=4, separators=(',', ': ')) |
| 133 | +if len(options['output']) > 1: |
| 134 | + json_path = options['output'][1] |
| 135 | + with file(json_path, 'w') as fp: |
| 136 | + json.dump(blacklist_symbols, fp, indent=4, separators=(',', ': ')) |
0 commit comments