1 #!/usr/bin/env python 2 # Copyright 2013 The Chromium Authors. All rights reserved. 3 # Use of this source code is governed by a BSD-style license that can be 4 # found in the LICENSE file. 5 6 """Wrapper script for launching application within the sel_ldr. 7 """ 8 9 import optparse 10 import os 11 import subprocess 12 import sys 13 14 import create_nmf 15 import getos 16 17 SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) 18 NACL_SDK_ROOT = os.path.dirname(SCRIPT_DIR) 19 20 if sys.version_info < (2, 6, 0): 21 sys.stderr.write("python 2.6 or later is required run this script\n") 22 sys.exit(1) 23 24 25 class Error(Exception): 26 pass 27 28 29 def Log(msg): 30 if Log.verbose: 31 sys.stderr.write(str(msg) + '\n') 32 Log.verbose = False 33 34 35 def main(argv): 36 usage = 'Usage: %prog [options] <.nexe>' 37 epilog = 'Example: sel_ldr.py my_nexe.nexe' 38 parser = optparse.OptionParser(usage, description=__doc__, epilog=epilog) 39 parser.add_option('-v', '--verbose', action='store_true', 40 help='Verbose output') 41 parser.add_option('-d', '--debug', action='store_true', 42 help='Enable debug stub') 43 parser.add_option('--debug-libs', action='store_true', 44 help='For dynamic executables, reference debug ' 45 'libraries rather then release') 46 47 # To enable bash completion for this command first install optcomplete 48 # and then add this line to your .bashrc: 49 # complete -F _optcomplete sel_ldr.py 50 try: 51 import optcomplete 52 optcomplete.autocomplete(parser) 53 except ImportError: 54 pass 55 56 options, args = parser.parse_args(argv) 57 if not args: 58 parser.error('No executable file specified') 59 60 nexe = args[0] 61 if options.verbose: 62 Log.verbose = True 63 64 osname = getos.GetPlatform() 65 if not os.path.exists(nexe): 66 raise Error('executable not found: %s' % nexe) 67 if not os.path.isfile(nexe): 68 raise Error('not a file: %s' % nexe) 69 70 arch, dynamic = create_nmf.ParseElfHeader(nexe) 71 72 if arch == 'arm': 73 raise Error('Cannot run ARM executables under sel_ldr') 74 75 arch_suffix = arch.replace('-', '_') 76 77 sel_ldr = os.path.join(SCRIPT_DIR, 'sel_ldr_%s' % arch_suffix) 78 irt = os.path.join(SCRIPT_DIR, 'irt_core_%s.nexe' % arch_suffix) 79 if osname == 'win': 80 sel_ldr += '.exe' 81 Log('ROOT = %s' % NACL_SDK_ROOT) 82 Log('SEL_LDR = %s' % sel_ldr) 83 Log('IRT = %s' % irt) 84 cmd = [sel_ldr] 85 86 if osname == 'linux': 87 # Run sel_ldr under nacl_helper_bootstrap 88 helper = os.path.join(SCRIPT_DIR, 'nacl_helper_bootstrap_%s' % arch_suffix) 89 Log('HELPER = %s' % helper) 90 cmd.insert(0, helper) 91 cmd.append('--r_debug=0xXXXXXXXXXXXXXXXX') 92 cmd.append('--reserved_at_zero=0xXXXXXXXXXXXXXXXX') 93 94 cmd += ['-a', '-B', irt] 95 96 if options.debug: 97 cmd.append('-g') 98 99 if not options.verbose: 100 cmd += ['-l', os.devnull] 101 102 if dynamic: 103 if options.debug_libs: 104 libpath = os.path.join(NACL_SDK_ROOT, 'lib', 105 'glibc_%s' % arch_suffix, 'Debug') 106 else: 107 libpath = os.path.join(NACL_SDK_ROOT, 'lib', 108 'glibc_%s' % arch_suffix, 'Release') 109 toolchain = '%s_x86_glibc' % osname 110 sdk_lib_dir = os.path.join(NACL_SDK_ROOT, 'toolchain', 111 toolchain, 'x86_64-nacl') 112 if arch == 'x86-64': 113 sdk_lib_dir = os.path.join(sdk_lib_dir, 'lib') 114 else: 115 sdk_lib_dir = os.path.join(sdk_lib_dir, 'lib32') 116 ldso = os.path.join(sdk_lib_dir, 'runnable-ld.so') 117 cmd.append(ldso) 118 Log('LD.SO = %s' % ldso) 119 libpath += ':' + sdk_lib_dir 120 cmd.append('--library-path') 121 cmd.append(libpath) 122 123 124 if args: 125 # Append arguments for the executable itself. 126 cmd += args 127 128 Log(cmd) 129 rtn = subprocess.call(cmd) 130 return rtn 131 132 133 if __name__ == '__main__': 134 try: 135 sys.exit(main(sys.argv[1:])) 136 except Error as e: 137 sys.stderr.write(str(e) + '\n') 138 sys.exit(1) 139