Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: pydir/szbuild.py

Issue 1506653002: Subzero: Add Non-SFI support for x86-32. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-subzero.git@master
Patch Set: Code review changes Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pydir/crosstest_generator.py ('k') | pydir/szbuild_spec2k.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python2 1 #!/usr/bin/env python2
2 2
3 import argparse 3 import argparse
4 import os 4 import os
5 import pipes 5 import pipes
6 import re 6 import re
7 import sys 7 import sys
8 8
9 from utils import shellcmd 9 from utils import shellcmd, FindBaseNaCl, get_sfi_string
10 from utils import FindBaseNaCl
11 10
12 def NewerThanOrNotThere(old_path, new_path): 11 def NewerThanOrNotThere(old_path, new_path):
13 """Returns whether old_path is newer than new_path. 12 """Returns whether old_path is newer than new_path.
14 13
15 Also returns true if either path doesn't exist. 14 Also returns true if either path doesn't exist.
16 """ 15 """
17 if not (os.path.exists(old_path) and os.path.exists(new_path)): 16 if not (os.path.exists(old_path) and os.path.exists(new_path)):
18 return True 17 return True
19 return os.path.getmtime(old_path) > os.path.getmtime(new_path) 18 return os.path.getmtime(old_path) > os.path.getmtime(new_path)
20 19
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 argparser.add_argument('-O', default='2', dest='optlevel', 77 argparser.add_argument('-O', default='2', dest='optlevel',
79 choices=['m1', '-1', '0', '1', '2'], 78 choices=['m1', '-1', '0', '1', '2'],
80 help='Optimization level ' + 79 help='Optimization level ' +
81 '(m1 and -1 are equivalent).' + 80 '(m1 and -1 are equivalent).' +
82 ' Default %(default)s.') 81 ' Default %(default)s.')
83 argparser.add_argument('--filetype', default='iasm', dest='filetype', 82 argparser.add_argument('--filetype', default='iasm', dest='filetype',
84 choices=['obj', 'asm', 'iasm'], 83 choices=['obj', 'asm', 'iasm'],
85 help='Output file type. Default %(default)s.') 84 help='Output file type. Default %(default)s.')
86 argparser.add_argument('--sandbox', dest='sandbox', action='store_true', 85 argparser.add_argument('--sandbox', dest='sandbox', action='store_true',
87 help='Enable sandboxing in the translator') 86 help='Enable sandboxing in the translator')
87 argparser.add_argument('--nonsfi', dest='nonsfi', action='store_true',
88 help='Enable Non-SFI in the translator')
88 argparser.add_argument('--enable-block-profile', 89 argparser.add_argument('--enable-block-profile',
89 dest='enable_block_profile', action='store_true', 90 dest='enable_block_profile', action='store_true',
90 help='Enable basic block profiling.') 91 help='Enable basic block profiling.')
91 argparser.add_argument('--target', default='x8632', dest='target', 92 argparser.add_argument('--target', default='x8632', dest='target',
92 choices=['arm32', 'x8632', 'x8664'], 93 choices=['arm32', 'x8632', 'x8664'],
93 help='Generate code for specified target.') 94 help='Generate code for specified target.')
94 argparser.add_argument('--verbose', '-v', dest='verbose', 95 argparser.add_argument('--verbose', '-v', dest='verbose',
95 action='store_true', 96 action='store_true',
96 help='Display some extra debugging output') 97 help='Display some extra debugging output')
97 argparser.add_argument('--sz', dest='sz_args', action='append', default=[], 98 argparser.add_argument('--sz', dest='sz_args', action='append', default=[],
98 help='Extra arguments for Subzero') 99 help='Extra arguments for Subzero')
99 argparser.add_argument('--llc', dest='llc_args', action='append', 100 argparser.add_argument('--llc', dest='llc_args', action='append',
100 default=[], help='Extra arguments for llc') 101 default=[], help='Extra arguments for llc')
101 argparser.add_argument('--no-sz', dest='nosz', action='store_true', 102 argparser.add_argument('--no-sz', dest='nosz', action='store_true',
102 help='Run only post-Subzero build steps') 103 help='Run only post-Subzero build steps')
103 104
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
104 def main(): 215 def main():
105 """Create a hybrid translation from Subzero and llc. 216 """Create a hybrid translation from Subzero and llc.
106 217
107 Takes a finalized pexe and builds a native executable as a hybrid of Subzero 218 Takes a finalized pexe and builds a native executable as a hybrid of Subzero
108 and llc translated bitcode. Linker tricks are used to determine whether 219 and llc translated bitcode. Linker tricks are used to determine whether
109 Subzero or llc generated symbols are used, on a per-symbol basis. 220 Subzero or llc generated symbols are used, on a per-symbol basis.
110 221
111 By default, for every symbol, its Subzero version is used. Subzero and llc 222 By default, for every symbol, its Subzero version is used. Subzero and llc
112 symbols can be selectively enabled/disabled via regular expressions on the 223 symbols can be selectively enabled/disabled via regular expressions on the
113 symbol name, or by ranges of lines in this program's auto-generated symbol 224 symbol name, or by ranges of lines in this program's auto-generated symbol
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 whitelist_sz_unescaped = pexe_base_unescaped + '.wl.sz.txt' 287 whitelist_sz_unescaped = pexe_base_unescaped + '.wl.sz.txt'
177 pnacl_sz = ( 288 pnacl_sz = (
178 '{root}/toolchain_build/src/subzero/pnacl-sz' 289 '{root}/toolchain_build/src/subzero/pnacl-sz'
179 ).format(root=nacl_root) 290 ).format(root=nacl_root)
180 llcbin = '{base}/pnacl-llc'.format(base=path_addition) 291 llcbin = '{base}/pnacl-llc'.format(base=path_addition)
181 gold = '{base}/le32-nacl-ld.gold'.format(base=path_addition) 292 gold = '{base}/le32-nacl-ld.gold'.format(base=path_addition)
182 objcopy = '{base}/le32-nacl-objcopy'.format(base=path_addition) 293 objcopy = '{base}/le32-nacl-objcopy'.format(base=path_addition)
183 opt_level = args.optlevel 294 opt_level = args.optlevel
184 opt_level_map = { 'm1':'0', '-1':'0', '0':'0', '1':'1', '2':'2' } 295 opt_level_map = { 'm1':'0', '-1':'0', '0':'0', '1':'1', '2':'2' }
185 hybrid = args.include or args.exclude 296 hybrid = args.include or args.exclude
297 native = not args.sandbox and not args.nonsfi
186 298
187 if hybrid and (args.force or 299 if hybrid and (args.force or
188 NewerThanOrNotThere(pexe, obj_llc) or 300 NewerThanOrNotThere(pexe, obj_llc) or
189 NewerThanOrNotThere(llcbin, obj_llc)): 301 NewerThanOrNotThere(llcbin, obj_llc)):
190 arch = { 302 arch = {
191 'arm32': 'armv7' if args.sandbox else 'arm-nonsfi', 303 'arm32': 'arm' + get_sfi_string(args, 'v7', '-nonsfi', '-nonsfi'),
192 'x8632': 'x86-32' if args.sandbox else 'x86-32-linux', 304 'x8632': 'x86-32' + get_sfi_string(args, '', '-nonsfi', '-linux'),
193 'x8664': 'x86-64' if args.sandbox else 'x86-64-linux', 305 'x8664': 'x86-64' + get_sfi_string(args, '', '', '-linux')
194 }[args.target] 306 }[args.target]
195 307
196 # Only run pnacl-translate in hybrid mode. 308 # Only run pnacl-translate in hybrid mode.
197 shellcmd(['{base}/pnacl-translate'.format(base=path_addition), 309 shellcmd(['{base}/pnacl-translate'.format(base=path_addition),
198 '-split-module=1', 310 '-split-module=1',
199 '-ffunction-sections', 311 '-ffunction-sections',
200 '-fdata-sections', 312 '-fdata-sections',
201 '-c', 313 '-c',
202 '-arch', arch, 314 '-arch', arch,
203 '-O' + opt_level_map[opt_level], 315 '-O' + opt_level_map[opt_level],
204 '--pnacl-driver-append-LLC_FLAGS_EXTRA=-externalize', 316 '--pnacl-driver-append-LLC_FLAGS_EXTRA=-externalize',
205 '-o', obj_llc] + 317 '-o', obj_llc] +
206 (['--pnacl-driver-verbose'] if args.verbose else []) + 318 (['--pnacl-driver-verbose'] if args.verbose else []) +
207 args.llc_args + 319 args.llc_args +
208 [pexe], 320 [pexe],
209 echo=args.verbose) 321 echo=args.verbose)
210 if not args.sandbox: 322 if native:
211 shellcmd(( 323 shellcmd((
212 '{objcopy} --redefine-sym _start=_user_start {obj}' 324 '{objcopy} --redefine-sym _start=_user_start {obj}'
213 ).format(objcopy=objcopy, obj=obj_llc), echo=args.verbose) 325 ).format(objcopy=objcopy, obj=obj_llc), echo=args.verbose)
214 # Generate llc syms file for consistency, even though it's not used. 326 # Generate llc syms file for consistency, even though it's not used.
215 shellcmd(( 327 shellcmd((
216 'nm {obj} | sed -n "s/.* [a-zA-Z] //p" > {sym}' 328 'nm {obj} | sed -n "s/.* [a-zA-Z] //p" > {sym}'
217 ).format(obj=obj_llc, sym=sym_llc), echo=args.verbose) 329 ).format(obj=obj_llc, sym=sym_llc), echo=args.verbose)
218 330
219 if (args.force or 331 if (args.force or
220 NewerThanOrNotThere(pexe, obj_sz) or 332 NewerThanOrNotThere(pexe, obj_sz) or
221 NewerThanOrNotThere(pnacl_sz, obj_sz)): 333 NewerThanOrNotThere(pnacl_sz, obj_sz)):
222 if not args.nosz: 334 if not args.nosz:
223 # Run pnacl-sz regardless of hybrid mode. 335 # Run pnacl-sz regardless of hybrid mode.
224 shellcmd([pnacl_sz, 336 shellcmd([pnacl_sz,
225 '-O' + opt_level, 337 '-O' + opt_level,
226 '-bitcode-format=pnacl', 338 '-bitcode-format=pnacl',
227 '-filetype=' + args.filetype, 339 '-filetype=' + args.filetype,
228 '-o', obj_sz if args.filetype == 'obj' else asm_sz, 340 '-o', obj_sz if args.filetype == 'obj' else asm_sz,
229 '-target=' + args.target] + 341 '-target=' + args.target] +
230 (['-externalize', 342 (['-externalize',
231 '-ffunction-sections', 343 '-ffunction-sections',
232 '-fdata-sections'] if hybrid else []) + 344 '-fdata-sections'] if hybrid else []) +
233 (['-sandbox'] if args.sandbox else []) + 345 (['-sandbox'] if args.sandbox else []) +
346 (['-nonsfi'] if args.nonsfi else []) +
234 (['-enable-block-profile'] if 347 (['-enable-block-profile'] if
235 args.enable_block_profile and not args.sandbox 348 args.enable_block_profile and not args.sandbox
236 else []) + 349 else []) +
237 args.sz_args + 350 args.sz_args +
238 [pexe], 351 [pexe],
239 echo=args.verbose) 352 echo=args.verbose)
240 if args.filetype != 'obj': 353 if args.filetype != 'obj':
241 triple = { 354 triple = {
242 'arm32': 'arm-nacl' if args.sandbox else 'arm', 355 'arm32': 'arm' + get_sfi_string(args, '-nacl', '', ''),
243 'x8632': 'i686-nacl' if args.sandbox else 'i686', 356 'x8632': 'i686' + get_sfi_string(args, '-nacl', '', ''),
244 'x8664': 'x86_64-nacl' if args.sandbox else 'x86_64-linux-gnux32', 357 'x8664': 'x86_64' +
358 get_sfi_string(args, '-nacl', '-linux-gnux32',
359 '-linux-gnux32'),
245 }[args.target] 360 }[args.target]
246 361
247 shellcmd(( 362 shellcmd((
248 '{base}/llvm-mc -triple={triple} -filetype=obj -o {obj} {asm}' 363 '{base}/llvm-mc -triple={triple} -filetype=obj -o {obj} {asm}'
249 ).format(base=path_addition, asm=asm_sz, obj=obj_sz, 364 ).format(base=path_addition, asm=asm_sz, obj=obj_sz,
250 triple=triple), 365 triple=triple),
251 echo=args.verbose) 366 echo=args.verbose)
252 if not args.sandbox: 367 if native:
253 shellcmd(( 368 shellcmd((
254 '{objcopy} --redefine-sym _start=_user_start {obj}' 369 '{objcopy} --redefine-sym _start=_user_start {obj}'
255 ).format(objcopy=objcopy, obj=obj_sz), echo=args.verbose) 370 ).format(objcopy=objcopy, obj=obj_sz), echo=args.verbose)
256 if hybrid: 371 if hybrid:
257 shellcmd(( 372 shellcmd((
258 'nm {obj} | sed -n "s/.* [a-zA-Z] //p" > {sym}' 373 'nm {obj} | sed -n "s/.* [a-zA-Z] //p" > {sym}'
259 ).format(obj=obj_sz, sym=sym_sz), echo=args.verbose) 374 ).format(obj=obj_sz, sym=sym_sz), echo=args.verbose)
260 375
261 if hybrid: 376 if hybrid:
262 with open(sym_sz_unescaped) as f: 377 with open(sym_sz_unescaped) as f:
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 sz=obj_sz_weak, llc=obj_llc_weak), 425 sz=obj_sz_weak, llc=obj_llc_weak),
311 echo=args.verbose) 426 echo=args.verbose)
312 shellcmd(( 427 shellcmd((
313 '{objcopy} -w --localize-symbol="*" {partial}' 428 '{objcopy} -w --localize-symbol="*" {partial}'
314 ).format(objcopy=objcopy, partial=obj_partial), 429 ).format(objcopy=objcopy, partial=obj_partial),
315 echo=args.verbose) 430 echo=args.verbose)
316 shellcmd(( 431 shellcmd((
317 '{objcopy} --globalize-symbol={start} ' + 432 '{objcopy} --globalize-symbol={start} ' +
318 '--globalize-symbol=__Sz_block_profile_info {partial}' 433 '--globalize-symbol=__Sz_block_profile_info {partial}'
319 ).format(objcopy=objcopy, partial=obj_partial, 434 ).format(objcopy=objcopy, partial=obj_partial,
320 start='_start' if args.sandbox else '_user_start'), 435 start=get_sfi_string(args, '_start', '_start',
436 '_user_start')),
321 echo=args.verbose) 437 echo=args.verbose)
322 438
323 # Run the linker regardless of hybrid mode. 439 # Run the linker regardless of hybrid mode.
324 if args.sandbox: 440 if args.sandbox:
325 assert args.target in ('x8632', 'arm32'), \ 441 LinkSandbox([obj_partial], exe, args.target, args.verbose)
326 '-sandbox is not available for %s' % args.target 442 elif args.nonsfi:
327 target_lib_dir = { 443 LinkNonsfi([obj_partial], exe, args.target, args.verbose)
328 'arm32': 'arm',
329 'x8632': 'x86-32',
330 }[args.target]
331 linklib = ('{root}/toolchain/linux_x86/pnacl_newlib_raw/translator/' +
332 '{target_dir}/lib').format(root=nacl_root,
333 target_dir=target_lib_dir)
334 shellcmd((
335 '{gold} -nostdlib --no-fix-cortex-a8 --eh-frame-hdr -z text ' +
336 '--build-id --entry=__pnacl_start -static ' +
337 '{linklib}/crtbegin.o {partial} ' +
338 '{root}/toolchain_build/src/subzero/build/runtime/' +
339 'szrt_sb_{target}.o ' +
340 '{linklib}/libpnacl_irt_shim_dummy.a --start-group ' +
341 '{linklib}/libgcc.a {linklib}/libcrt_platform.a ' +
342 '--end-group {linklib}/crtend.o --undefined=_start ' +
343 '--defsym=__Sz_AbsoluteZero=0 ' +
344 '-o {exe}'
345 ).format(gold=gold, linklib=linklib, partial=obj_partial, exe=exe,
346 root=nacl_root, target=args.target),
347 echo=args.verbose)
348 else: 444 else:
349 linker = { 445 LinkNative([obj_partial], exe, args.target, args.verbose)
350 'arm32': '/usr/bin/arm-linux-gnueabihf-g++',
351 'x8632': ('{root}/../third_party/llvm-build/Release+Asserts/bin/clang'
352 ).format(root=nacl_root),
353 'x8664': ('{root}/../third_party/llvm-build/Release+Asserts/bin/clang'
354 ).format(root=nacl_root)
355 }[args.target]
356
357 extra_linker_args = ' '.join({
358 'arm32': ['-mcpu=cortex-a9'],
359 'x8632': ['-m32'],
360 'x8664': ['-mx32']
361 }[args.target])
362
363 lib_dir = {
364 'arm32': 'arm-linux',
365 'x8632': 'x86-32-linux',
366 'x8664': 'x86-64-linux',
367 }[args.target]
368
369 shellcmd((
370 '{ld} {ld_extra_args} {partial} -o {exe} ' +
371 # Keep the rest of this command line (except szrt_native_x8632.o) in
372 # sync with RunHostLD() in pnacl-translate.py.
373 '{root}/toolchain/linux_x86/pnacl_newlib_raw/translator/' +
374 '{lib_dir}/lib/' +
375 '{{unsandboxed_irt,irt_random,irt_query_list}}.o ' +
376 '{root}/toolchain_build/src/subzero/build/runtime/' +
377 'szrt_native_{target}.o -lpthread -lrt ' +
378 '-Wl,--defsym=__Sz_AbsoluteZero=0'
379 ).format(ld=linker, ld_extra_args=extra_linker_args,
380 partial=obj_partial, exe=exe, root=nacl_root,
381 target=args.target, lib_dir=lib_dir),
382 echo=args.verbose)
383 446
384 # Put the extra verbose printing at the end. 447 # Put the extra verbose printing at the end.
385 if args.verbose and hybrid: 448 if args.verbose and hybrid:
386 print 'include={regex}'.format(regex=re_include_str) 449 print 'include={regex}'.format(regex=re_include_str)
387 print 'exclude={regex}'.format(regex=re_exclude_str) 450 print 'exclude={regex}'.format(regex=re_exclude_str)
388 print 'default_match={dm}'.format(dm=default_match) 451 print 'default_match={dm}'.format(dm=default_match)
389 print 'Number of Subzero syms = {num}'.format(num=len(sz_syms)) 452 print 'Number of Subzero syms = {num}'.format(num=len(sz_syms))
390 453
391 if __name__ == '__main__': 454 if __name__ == '__main__':
392 main() 455 main()
OLDNEW
« no previous file with comments | « pydir/crosstest_generator.py ('k') | pydir/szbuild_spec2k.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698