Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | |
| 3 | import argparse |
| 4 | import os |
| 5 | import pipes |
| 6 | import re |
| 7 | import sys |
| 8 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 9 | from utils import shellcmd, FindBaseNaCl, get_sfi_string |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 10 | |
Jim Stichnoth | 0fb613f | 2014-09-16 10:19:03 -0700 | [diff] [blame] | 11 | def NewerThanOrNotThere(old_path, new_path): |
| 12 | """Returns whether old_path is newer than new_path. |
| 13 | |
| 14 | Also returns true if either path doesn't exist. |
| 15 | """ |
| 16 | if not (os.path.exists(old_path) and os.path.exists(new_path)): |
| 17 | return True |
| 18 | return os.path.getmtime(old_path) > os.path.getmtime(new_path) |
| 19 | |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 20 | def BuildRegex(patterns, syms): |
| 21 | """Build a regular expression string for inclusion or exclusion. |
| 22 | |
| 23 | Creates a regex string from an array of patterns and an array |
| 24 | of symbol names. Each element in the patterns array is either a |
| 25 | regex, or a range of entries in the symbol name array, e.g. '2:9'. |
| 26 | """ |
| 27 | pattern_list = [] |
| 28 | for pattern in patterns: |
| 29 | if pattern[0].isdigit() or pattern[0] == ':': |
| 30 | # Legitimate symbols or regexes shouldn't start with a |
| 31 | # digit or a ':', so interpret the pattern as a range. |
| 32 | interval = pattern.split(':') |
| 33 | if len(interval) == 1: |
| 34 | # Treat singleton 'n' as 'n:n+1'. |
| 35 | lower = int(interval[0]) |
| 36 | upper = lower + 1 |
| 37 | elif len(interval) == 2: |
| 38 | # Handle 'a:b', 'a:', and ':b' with suitable defaults. |
| 39 | lower = int(interval[0]) if len(interval[0]) else 0 |
| 40 | upper = int(interval[1]) if len(interval[1]) else len(syms) |
| 41 | else: |
| 42 | print 'Invalid range syntax: {p}'.format(p=pattern) |
| 43 | exit(1) |
| 44 | pattern = '$|^'.join([re.escape(p) for p in syms[lower:upper]]) |
| 45 | pattern_list.append('^' + pattern + '$') |
| 46 | return '|'.join(pattern_list) if len(pattern_list) else '^$' |
| 47 | |
| 48 | def MatchSymbol(sym, re_include, re_exclude, default_match): |
| 49 | """Match a symbol name against inclusion/exclusion rules. |
| 50 | |
| 51 | Returns True or False depending on whether the given symbol |
| 52 | matches the compiled include or exclude regexes. The default is |
| 53 | returned if neither the include nor the exclude regex matches. |
| 54 | """ |
| 55 | if re_exclude.match(sym): |
| 56 | # Always honor an explicit exclude before considering |
| 57 | # includes. |
| 58 | return False |
| 59 | if re_include.match(sym): |
| 60 | return True |
| 61 | return default_match |
| 62 | |
Jim Stichnoth | cc0ee13 | 2014-09-17 09:42:53 -0700 | [diff] [blame] | 63 | def AddOptionalArgs(argparser): |
| 64 | argparser.add_argument('--force', dest='force', action='store_true', |
| 65 | help='Force all re-translations of the pexe') |
| 66 | argparser.add_argument('--include', '-i', default=[], dest='include', |
| 67 | action='append', |
| 68 | help='Subzero symbols to include ' + |
| 69 | '(regex or line range)') |
| 70 | argparser.add_argument('--exclude', '-e', default=[], dest='exclude', |
| 71 | action='append', |
| 72 | help='Subzero symbols to exclude ' + |
| 73 | '(regex or line range)') |
| 74 | argparser.add_argument('--output', '-o', default='a.out', dest='output', |
| 75 | action='store', |
| 76 | help='Output executable. Default %(default)s.') |
| 77 | argparser.add_argument('-O', default='2', dest='optlevel', |
| 78 | choices=['m1', '-1', '0', '1', '2'], |
| 79 | help='Optimization level ' + |
| 80 | '(m1 and -1 are equivalent).' + |
| 81 | ' Default %(default)s.') |
Jim Stichnoth | d442e7e | 2015-02-12 14:01:48 -0800 | [diff] [blame] | 82 | argparser.add_argument('--filetype', default='iasm', dest='filetype', |
| 83 | choices=['obj', 'asm', 'iasm'], |
| 84 | help='Output file type. Default %(default)s.') |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 85 | argparser.add_argument('--sandbox', dest='sandbox', action='store_true', |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 86 | help='Enable sandboxing in the translator') |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 87 | argparser.add_argument('--nonsfi', dest='nonsfi', action='store_true', |
| 88 | help='Enable Non-SFI in the translator') |
John Porto | f8b4cc8 | 2015-06-09 18:06:19 -0700 | [diff] [blame] | 89 | argparser.add_argument('--enable-block-profile', |
| 90 | dest='enable_block_profile', action='store_true', |
| 91 | help='Enable basic block profiling.') |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 92 | argparser.add_argument('--target', default='x8632', dest='target', |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 93 | choices=['arm32', 'x8632', 'x8664'], |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 94 | help='Generate code for specified target.') |
Jim Stichnoth | cc0ee13 | 2014-09-17 09:42:53 -0700 | [diff] [blame] | 95 | argparser.add_argument('--verbose', '-v', dest='verbose', |
| 96 | action='store_true', |
| 97 | help='Display some extra debugging output') |
Jim Stichnoth | 89906a5 | 2014-09-18 09:43:30 -0700 | [diff] [blame] | 98 | argparser.add_argument('--sz', dest='sz_args', action='append', default=[], |
| 99 | help='Extra arguments for Subzero') |
| 100 | argparser.add_argument('--llc', dest='llc_args', action='append', |
| 101 | default=[], help='Extra arguments for llc') |
Jim Stichnoth | 42356fb | 2015-11-10 08:41:12 -0800 | [diff] [blame] | 102 | argparser.add_argument('--no-sz', dest='nosz', action='store_true', |
| 103 | help='Run only post-Subzero build steps') |
Jim Stichnoth | cc0ee13 | 2014-09-17 09:42:53 -0700 | [diff] [blame] | 104 | |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 105 | def LinkSandbox(objs, exe, target, verbose=True): |
| 106 | assert target in ('x8632', 'arm32'), \ |
| 107 | '-sandbox is not available for %s' % target |
| 108 | nacl_root = FindBaseNaCl() |
| 109 | gold = ('{root}/toolchain/linux_x86/pnacl_newlib_raw/bin/' + |
| 110 | 'le32-nacl-ld.gold').format(root=nacl_root) |
| 111 | target_lib_dir = { |
| 112 | 'arm32': 'arm', |
| 113 | 'x8632': 'x86-32', |
| 114 | }[target] |
| 115 | linklib = ('{root}/toolchain/linux_x86/pnacl_newlib_raw/translator/' + |
| 116 | '{target_dir}/lib').format(root=nacl_root, |
| 117 | target_dir=target_lib_dir) |
| 118 | shellcmd([gold, |
| 119 | '-nostdlib', |
| 120 | '--no-fix-cortex-a8', |
| 121 | '--eh-frame-hdr', |
| 122 | '-z', 'text', |
| 123 | #'-z', 'noexecstack', |
| 124 | '--build-id', |
| 125 | '--entry=__pnacl_start', |
| 126 | '-static', #'-pie', |
| 127 | '{linklib}/crtbegin.o'.format(linklib=linklib)] + |
| 128 | objs + |
| 129 | [('{root}/toolchain_build/src/subzero/build/runtime/' + |
| 130 | 'szrt_sb_{target}.o').format(root=nacl_root, target=target), |
| 131 | '{linklib}/libpnacl_irt_shim_dummy.a'.format(linklib=linklib), |
| 132 | '--start-group', |
| 133 | '{linklib}/libgcc.a'.format(linklib=linklib), |
| 134 | '{linklib}/libcrt_platform.a'.format(linklib=linklib), |
| 135 | '--end-group', |
| 136 | '{linklib}/crtend.o'.format(linklib=linklib), |
| 137 | '--undefined=_start', |
| 138 | '--defsym=__Sz_AbsoluteZero=0', |
| 139 | #'--defsym=_begin=0', |
| 140 | '-o', exe |
| 141 | ], echo=verbose) |
| 142 | |
| 143 | def LinkNonsfi(objs, exe, target, verbose=True): |
| 144 | nacl_root = FindBaseNaCl() |
| 145 | gold = ('{root}/toolchain/linux_x86/pnacl_newlib_raw/bin/' + |
| 146 | 'le32-nacl-ld.gold').format(root=nacl_root) |
| 147 | target_lib_dir = { |
| 148 | 'arm32': 'arm-nonsfi', |
| 149 | 'x8632': 'x86-32-nonsfi', |
| 150 | }[target] |
| 151 | linklib = ('{root}/toolchain/linux_x86/pnacl_newlib_raw/translator/' + |
| 152 | '{target_dir}/lib').format(root=nacl_root, |
| 153 | target_dir=target_lib_dir) |
| 154 | shellcmd([gold, |
| 155 | '-nostdlib', |
| 156 | '--no-fix-cortex-a8', |
| 157 | '--eh-frame-hdr', |
| 158 | '-z', 'text', |
| 159 | '-z', 'noexecstack', |
| 160 | '--build-id', |
| 161 | '--entry=__pnacl_start', |
| 162 | '-pie', |
| 163 | '{linklib}/crtbegin.o'.format(linklib=linklib)] + |
| 164 | objs + |
| 165 | [('{root}/toolchain_build/src/subzero/build/runtime/' + |
| 166 | 'szrt_nonsfi_{target}.o').format(root=nacl_root, target=target), |
| 167 | '{linklib}/libpnacl_irt_shim_dummy.a'.format(linklib=linklib), |
| 168 | '--start-group', |
| 169 | '{linklib}/libgcc.a'.format(linklib=linklib), |
| 170 | '{linklib}/libcrt_platform.a'.format(linklib=linklib), |
| 171 | '--end-group', |
| 172 | '{linklib}/crtend.o'.format(linklib=linklib), |
| 173 | '--undefined=_start', |
| 174 | '--defsym=__Sz_AbsoluteZero=0', |
| 175 | '--defsym=_begin=0', |
| 176 | '-o', exe |
| 177 | ], echo=verbose) |
| 178 | |
| 179 | def LinkNative(objs, exe, target, verbose=True): |
| 180 | nacl_root = FindBaseNaCl() |
| 181 | linker = { |
| 182 | 'arm32': '/usr/bin/arm-linux-gnueabihf-g++', |
| 183 | 'x8632': ('{root}/../third_party/llvm-build/Release+Asserts/bin/clang' |
| 184 | ).format(root=nacl_root), |
| 185 | 'x8664': ('{root}/../third_party/llvm-build/Release+Asserts/bin/clang' |
| 186 | ).format(root=nacl_root) |
| 187 | }[target] |
| 188 | |
| 189 | extra_linker_args = { |
| 190 | 'arm32': ['-mcpu=cortex-a9'], |
| 191 | 'x8632': ['-m32'], |
| 192 | 'x8664': ['-mx32'] |
| 193 | }[target] |
| 194 | |
| 195 | lib_dir = { |
| 196 | 'arm32': 'arm-linux', |
| 197 | 'x8632': 'x86-32-linux', |
| 198 | 'x8664': 'x86-64-linux', |
| 199 | }[target] |
| 200 | |
| 201 | shellcmd([linker] + |
| 202 | extra_linker_args + |
| 203 | objs + |
| 204 | ['-o', exe, |
| 205 | ('{root}/toolchain/linux_x86/pnacl_newlib_raw/translator/' + |
| 206 | '{lib_dir}/lib/' + |
| 207 | '{{unsandboxed_irt,irt_random,irt_query_list}}.o').format( |
| 208 | root=nacl_root, lib_dir=lib_dir), |
| 209 | ('{root}/toolchain_build/src/subzero/build/runtime/' + |
| 210 | 'szrt_native_{target}.o').format(root=nacl_root, target=target), |
| 211 | '-lm', '-lpthread', '-lrt', |
| 212 | '-Wl,--defsym=__Sz_AbsoluteZero=0' |
| 213 | ], echo=verbose) |
| 214 | |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 215 | def main(): |
| 216 | """Create a hybrid translation from Subzero and llc. |
| 217 | |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 218 | Takes a finalized pexe and builds a native executable as a hybrid of Subzero |
| 219 | and llc translated bitcode. Linker tricks are used to determine whether |
| 220 | Subzero or llc generated symbols are used, on a per-symbol basis. |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 221 | |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 222 | By default, for every symbol, its Subzero version is used. Subzero and llc |
| 223 | symbols can be selectively enabled/disabled via regular expressions on the |
| 224 | symbol name, or by ranges of lines in this program's auto-generated symbol |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 225 | file. |
| 226 | |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 227 | For each symbol, the --exclude arguments are first checked (the symbol is |
| 228 | 'rejected' on a match), followed by the --include arguments (the symbol is |
| 229 | 'accepted' on a match), followed by unconditional 'rejection'. The Subzero |
| 230 | version is used for an 'accepted' symbol, and the llc version is used for a |
| 231 | 'rejected' symbol. |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 232 | |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 233 | Each --include and --exclude argument can be a regular expression or a range |
| 234 | of lines in the symbol file. Each regular expression is wrapped inside |
| 235 | '^$', so if you want a substring match on 'foo', use '.*foo.*' instead. |
| 236 | Ranges use python-style 'first:last' notation, so e.g. use '0:10' or ':10' |
| 237 | for the first 10 lines of the file, or '1' for the second line of the file. |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 238 | |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 239 | If no --include or --exclude arguments are given, the executable is produced |
| 240 | entirely using Subzero, without using llc or linker tricks. |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 241 | |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 242 | This script uses file modification timestamps to determine whether llc and |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 243 | Subzero re-translation are needed. It checks timestamps of llc, pnacl-sz, |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 244 | and the pexe against the translated object files to determine the minimal |
| 245 | work necessary. The --force option suppresses those checks and |
| 246 | re-translates everything. |
| 247 | |
Jim Stichnoth | a2efb9d | 2015-10-30 12:02:44 -0700 | [diff] [blame] | 248 | This script expects various PNaCl and LLVM tools to be found within the |
| 249 | native_client tree. When changes are made to these tools, copy them this |
| 250 | way: |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 251 | cd native_client |
Jan Voung | 4c127ba | 2014-09-19 13:11:36 -0700 | [diff] [blame] | 252 | toolchain_build/toolchain_build_pnacl.py llvm_x86_64_linux \\ |
Jim Stichnoth | bb9d11a | 2015-06-03 00:18:14 -0700 | [diff] [blame] | 253 | --install=toolchain/linux_x86/pnacl_newlib_raw |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 254 | """ |
| 255 | argparser = argparse.ArgumentParser( |
| 256 | description=' ' + main.__doc__, |
| 257 | formatter_class=argparse.RawTextHelpFormatter) |
Jim Stichnoth | cc0ee13 | 2014-09-17 09:42:53 -0700 | [diff] [blame] | 258 | AddOptionalArgs(argparser) |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 259 | argparser.add_argument('pexe', help='Finalized pexe to translate') |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 260 | args = argparser.parse_args() |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 261 | pexe = args.pexe |
Jim Stichnoth | cc0ee13 | 2014-09-17 09:42:53 -0700 | [diff] [blame] | 262 | exe = args.output |
| 263 | ProcessPexe(args, pexe, exe) |
| 264 | |
| 265 | def ProcessPexe(args, pexe, exe): |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 266 | [pexe_base, ext] = os.path.splitext(pexe) |
| 267 | if ext != '.pexe': |
| 268 | pexe_base = pexe |
| 269 | pexe_base_unescaped = pexe_base |
| 270 | pexe_base = pipes.quote(pexe_base) |
| 271 | pexe = pipes.quote(pexe) |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 272 | |
| 273 | nacl_root = FindBaseNaCl() |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 274 | path_addition = ( |
Jim Stichnoth | bb9d11a | 2015-06-03 00:18:14 -0700 | [diff] [blame] | 275 | '{root}/toolchain/linux_x86/pnacl_newlib_raw/bin' |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 276 | ).format(root=nacl_root) |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 277 | obj_llc = pexe_base + '.llc.o' |
| 278 | obj_sz = pexe_base + '.sz.o' |
| 279 | asm_sz = pexe_base + '.sz.s' |
| 280 | obj_llc_weak = pexe_base + '.weak.llc.o' |
| 281 | obj_sz_weak = pexe_base + '.weak.sz.o' |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 282 | obj_partial = obj_sz # overridden for hybrid mode |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 283 | sym_llc = pexe_base + '.sym.llc.txt' |
| 284 | sym_sz = pexe_base + '.sym.sz.txt' |
| 285 | sym_sz_unescaped = pexe_base_unescaped + '.sym.sz.txt' |
| 286 | whitelist_sz = pexe_base + '.wl.sz.txt' |
| 287 | whitelist_sz_unescaped = pexe_base_unescaped + '.wl.sz.txt' |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 288 | pnacl_sz = ( |
| 289 | '{root}/toolchain_build/src/subzero/pnacl-sz' |
Jim Stichnoth | 0fb613f | 2014-09-16 10:19:03 -0700 | [diff] [blame] | 290 | ).format(root=nacl_root) |
Jim Stichnoth | 8c7b0a2 | 2015-03-29 15:20:37 -0700 | [diff] [blame] | 291 | llcbin = '{base}/pnacl-llc'.format(base=path_addition) |
Jim Stichnoth | a2efb9d | 2015-10-30 12:02:44 -0700 | [diff] [blame] | 292 | gold = '{base}/le32-nacl-ld.gold'.format(base=path_addition) |
| 293 | objcopy = '{base}/le32-nacl-objcopy'.format(base=path_addition) |
Jim Stichnoth | 0fb613f | 2014-09-16 10:19:03 -0700 | [diff] [blame] | 294 | opt_level = args.optlevel |
Jan Voung | 109fa15 | 2014-10-07 17:22:51 -0700 | [diff] [blame] | 295 | opt_level_map = { 'm1':'0', '-1':'0', '0':'0', '1':'1', '2':'2' } |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 296 | hybrid = args.include or args.exclude |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 297 | native = not args.sandbox and not args.nonsfi |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 298 | |
| 299 | if hybrid and (args.force or |
| 300 | NewerThanOrNotThere(pexe, obj_llc) or |
| 301 | NewerThanOrNotThere(llcbin, obj_llc)): |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 302 | arch = { |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 303 | 'arm32': 'arm' + get_sfi_string(args, 'v7', '-nonsfi', '-nonsfi'), |
| 304 | 'x8632': 'x86-32' + get_sfi_string(args, '', '-nonsfi', '-linux'), |
| 305 | 'x8664': 'x86-64' + get_sfi_string(args, '', '', '-linux') |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 306 | }[args.target] |
| 307 | |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 308 | # Only run pnacl-translate in hybrid mode. |
Jim Stichnoth | a2efb9d | 2015-10-30 12:02:44 -0700 | [diff] [blame] | 309 | shellcmd(['{base}/pnacl-translate'.format(base=path_addition), |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 310 | '-split-module=1', |
Jim Stichnoth | 89906a5 | 2014-09-18 09:43:30 -0700 | [diff] [blame] | 311 | '-ffunction-sections', |
Jim Stichnoth | 2a063e2 | 2014-10-08 11:24:51 -0700 | [diff] [blame] | 312 | '-fdata-sections', |
Jim Stichnoth | 89906a5 | 2014-09-18 09:43:30 -0700 | [diff] [blame] | 313 | '-c', |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 314 | '-arch', arch, |
Jim Stichnoth | 89906a5 | 2014-09-18 09:43:30 -0700 | [diff] [blame] | 315 | '-O' + opt_level_map[opt_level], |
| 316 | '--pnacl-driver-append-LLC_FLAGS_EXTRA=-externalize', |
| 317 | '-o', obj_llc] + |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 318 | (['--pnacl-driver-verbose'] if args.verbose else []) + |
Jim Stichnoth | 89906a5 | 2014-09-18 09:43:30 -0700 | [diff] [blame] | 319 | args.llc_args + |
| 320 | [pexe], |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 321 | echo=args.verbose) |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 322 | if native: |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 323 | shellcmd(( |
Jan Voung | 050deaa | 2015-06-12 15:12:05 -0700 | [diff] [blame] | 324 | '{objcopy} --redefine-sym _start=_user_start {obj}' |
| 325 | ).format(objcopy=objcopy, obj=obj_llc), echo=args.verbose) |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 326 | # Generate llc syms file for consistency, even though it's not used. |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 327 | shellcmd(( |
Jim Stichnoth | 0fb613f | 2014-09-16 10:19:03 -0700 | [diff] [blame] | 328 | 'nm {obj} | sed -n "s/.* [a-zA-Z] //p" > {sym}' |
| 329 | ).format(obj=obj_llc, sym=sym_llc), echo=args.verbose) |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 330 | |
| 331 | if (args.force or |
| 332 | NewerThanOrNotThere(pexe, obj_sz) or |
Jim Stichnoth | fa0cfa5 | 2015-02-26 09:42:36 -0800 | [diff] [blame] | 333 | NewerThanOrNotThere(pnacl_sz, obj_sz)): |
Jim Stichnoth | 42356fb | 2015-11-10 08:41:12 -0800 | [diff] [blame] | 334 | if not args.nosz: |
| 335 | # Run pnacl-sz regardless of hybrid mode. |
| 336 | shellcmd([pnacl_sz, |
| 337 | '-O' + opt_level, |
| 338 | '-bitcode-format=pnacl', |
| 339 | '-filetype=' + args.filetype, |
| 340 | '-o', obj_sz if args.filetype == 'obj' else asm_sz, |
| 341 | '-target=' + args.target] + |
| 342 | (['-externalize', |
| 343 | '-ffunction-sections', |
| 344 | '-fdata-sections'] if hybrid else []) + |
| 345 | (['-sandbox'] if args.sandbox else []) + |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 346 | (['-nonsfi'] if args.nonsfi else []) + |
Jim Stichnoth | 42356fb | 2015-11-10 08:41:12 -0800 | [diff] [blame] | 347 | (['-enable-block-profile'] if |
| 348 | args.enable_block_profile and not args.sandbox |
| 349 | else []) + |
| 350 | args.sz_args + |
| 351 | [pexe], |
| 352 | echo=args.verbose) |
Jim Stichnoth | d442e7e | 2015-02-12 14:01:48 -0800 | [diff] [blame] | 353 | if args.filetype != 'obj': |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 354 | triple = { |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 355 | 'arm32': 'arm' + get_sfi_string(args, '-nacl', '', ''), |
| 356 | 'x8632': 'i686' + get_sfi_string(args, '-nacl', '', ''), |
| 357 | 'x8664': 'x86_64' + |
| 358 | get_sfi_string(args, '-nacl', '-linux-gnux32', |
| 359 | '-linux-gnux32'), |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 360 | }[args.target] |
| 361 | |
Jim Stichnoth | 046ec2e | 2015-01-31 17:57:41 -0800 | [diff] [blame] | 362 | shellcmd(( |
Jim Stichnoth | 955bfdc | 2015-10-30 14:41:43 -0700 | [diff] [blame] | 363 | '{base}/llvm-mc -triple={triple} -filetype=obj -o {obj} {asm}' |
| 364 | ).format(base=path_addition, asm=asm_sz, obj=obj_sz, |
| 365 | triple=triple), |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 366 | echo=args.verbose) |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 367 | if native: |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 368 | shellcmd(( |
Jan Voung | 050deaa | 2015-06-12 15:12:05 -0700 | [diff] [blame] | 369 | '{objcopy} --redefine-sym _start=_user_start {obj}' |
| 370 | ).format(objcopy=objcopy, obj=obj_sz), echo=args.verbose) |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 371 | if hybrid: |
| 372 | shellcmd(( |
| 373 | 'nm {obj} | sed -n "s/.* [a-zA-Z] //p" > {sym}' |
| 374 | ).format(obj=obj_sz, sym=sym_sz), echo=args.verbose) |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 375 | |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 376 | if hybrid: |
| 377 | with open(sym_sz_unescaped) as f: |
| 378 | sz_syms = f.read().splitlines() |
| 379 | re_include_str = BuildRegex(args.include, sz_syms) |
| 380 | re_exclude_str = BuildRegex(args.exclude, sz_syms) |
| 381 | re_include = re.compile(re_include_str) |
| 382 | re_exclude = re.compile(re_exclude_str) |
| 383 | # If a symbol doesn't explicitly match re_include or re_exclude, |
| 384 | # the default MatchSymbol() result is True, unless some --include |
| 385 | # args are provided. |
| 386 | default_match = not args.include |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 387 | |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 388 | whitelist_has_items = False |
| 389 | with open(whitelist_sz_unescaped, 'w') as f: |
| 390 | for sym in sz_syms: |
| 391 | if MatchSymbol(sym, re_include, re_exclude, default_match): |
| 392 | f.write(sym + '\n') |
| 393 | whitelist_has_items = True |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 394 | shellcmd(( |
Jan Voung | 050deaa | 2015-06-12 15:12:05 -0700 | [diff] [blame] | 395 | '{objcopy} --weaken {obj} {weak}' |
| 396 | ).format(objcopy=objcopy, obj=obj_sz, weak=obj_sz_weak), |
| 397 | echo=args.verbose) |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 398 | if whitelist_has_items: |
| 399 | # objcopy returns an error if the --weaken-symbols file is empty. |
| 400 | shellcmd(( |
Jan Voung | 050deaa | 2015-06-12 15:12:05 -0700 | [diff] [blame] | 401 | '{objcopy} --weaken-symbols={whitelist} {obj} {weak}' |
| 402 | ).format(objcopy=objcopy, |
| 403 | whitelist=whitelist_sz, obj=obj_llc, |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 404 | weak=obj_llc_weak), |
| 405 | echo=args.verbose) |
| 406 | else: |
| 407 | shellcmd(( |
Jan Voung | 050deaa | 2015-06-12 15:12:05 -0700 | [diff] [blame] | 408 | '{objcopy} {obj} {weak}' |
| 409 | ).format(objcopy=objcopy, obj=obj_llc, weak=obj_llc_weak), |
| 410 | echo=args.verbose) |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 411 | obj_partial = pexe_base + '.o' |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 412 | ld = { |
| 413 | 'arm32': 'arm-linux-gnueabihf-ld', |
| 414 | 'x8632': 'ld', |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 415 | 'x8664': 'ld', |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 416 | }[args.target] |
| 417 | emulation = { |
| 418 | 'arm32': 'armelf_linux_eabi', |
| 419 | 'x8632': 'elf_i386', |
John Porto | 3c275ce | 2015-12-22 08:14:00 -0800 | [diff] [blame] | 420 | 'x8664': 'elf32_x86_64', |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 421 | }[args.target] |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 422 | shellcmd(( |
John Porto | afc92af | 2015-10-16 10:34:04 -0700 | [diff] [blame] | 423 | '{ld} -r -m {emulation} -o {partial} {sz} {llc}' |
| 424 | ).format(ld=ld, emulation=emulation, partial=obj_partial, |
| 425 | sz=obj_sz_weak, llc=obj_llc_weak), |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 426 | echo=args.verbose) |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 427 | shellcmd(( |
Jan Voung | 050deaa | 2015-06-12 15:12:05 -0700 | [diff] [blame] | 428 | '{objcopy} -w --localize-symbol="*" {partial}' |
| 429 | ).format(objcopy=objcopy, partial=obj_partial), |
| 430 | echo=args.verbose) |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 431 | shellcmd(( |
Jim Stichnoth | 88ab5ca | 2015-08-06 14:56:42 -0700 | [diff] [blame] | 432 | '{objcopy} --globalize-symbol={start} ' + |
| 433 | '--globalize-symbol=__Sz_block_profile_info {partial}' |
Jan Voung | 050deaa | 2015-06-12 15:12:05 -0700 | [diff] [blame] | 434 | ).format(objcopy=objcopy, partial=obj_partial, |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 435 | start=get_sfi_string(args, '_start', '_start', |
| 436 | '_user_start')), |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 437 | echo=args.verbose) |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 438 | |
| 439 | # Run the linker regardless of hybrid mode. |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 440 | if args.sandbox: |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 441 | LinkSandbox([obj_partial], exe, args.target, args.verbose) |
| 442 | elif args.nonsfi: |
| 443 | LinkNonsfi([obj_partial], exe, args.target, args.verbose) |
Jim Stichnoth | 9738a9e | 2015-02-23 16:39:06 -0800 | [diff] [blame] | 444 | else: |
Jim Stichnoth | 8ff4b28 | 2016-01-04 15:39:06 -0800 | [diff] [blame^] | 445 | LinkNative([obj_partial], exe, args.target, args.verbose) |
Jim Stichnoth | 58acae3 | 2014-11-03 12:31:28 -0800 | [diff] [blame] | 446 | |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 447 | # Put the extra verbose printing at the end. |
Jim Stichnoth | a2efb9d | 2015-10-30 12:02:44 -0700 | [diff] [blame] | 448 | if args.verbose and hybrid: |
| 449 | print 'include={regex}'.format(regex=re_include_str) |
| 450 | print 'exclude={regex}'.format(regex=re_exclude_str) |
| 451 | print 'default_match={dm}'.format(dm=default_match) |
| 452 | print 'Number of Subzero syms = {num}'.format(num=len(sz_syms)) |
Jim Stichnoth | 10ea698 | 2014-09-09 11:19:12 -0700 | [diff] [blame] | 453 | |
| 454 | if __name__ == '__main__': |
| 455 | main() |