Home | History | Annotate | Download | only in tools
      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   description = __doc__
     38   epilog = 'Example: sel_ldr.py my_nexe.nexe'
     39   parser = optparse.OptionParser(usage, description=description, epilog=epilog)
     40   parser.add_option('-v', '--verbose', action='store_true',
     41                     help='Verbose output')
     42   parser.add_option('-d', '--debug', action='store_true',
     43                     help='Enable debug stub')
     44   parser.add_option('--debug-libs', action='store_true',
     45                     help='For dynamic executables, reference debug '
     46                          'libraries rather then release')
     47   options, args = parser.parse_args(argv)
     48   if not args:
     49     parser.error('No executable file specified')
     50 
     51   nexe = args[0]
     52   if options.verbose:
     53     Log.verbose = True
     54 
     55   osname = getos.GetPlatform()
     56   if not os.path.exists(nexe):
     57     raise Error('executable not found: %s' % nexe)
     58   if not os.path.isfile(nexe):
     59     raise Error('not a file: %s' % nexe)
     60 
     61   arch, dynamic = create_nmf.ParseElfHeader(nexe)
     62   if osname == 'mac' and arch == 'x86-64':
     63     raise Error('Running of x86-64 executables is not supported on mac')
     64 
     65   if arch == 'arm':
     66     raise Error('Cannot run ARM executables under sel_ldr')
     67 
     68   arch_suffix = arch.replace('-', '_')
     69 
     70   sel_ldr = os.path.join(SCRIPT_DIR, 'sel_ldr_%s' % arch_suffix)
     71   irt = os.path.join(SCRIPT_DIR, 'irt_core_%s.nexe' % arch_suffix)
     72   if osname == 'win':
     73     sel_ldr += '.exe'
     74   Log('ROOT    = %s' % NACL_SDK_ROOT)
     75   Log('SEL_LDR = %s' % sel_ldr)
     76   Log('IRT     = %s' % irt)
     77   cmd = [sel_ldr, '-a', '-B', irt, '-l', os.devnull]
     78 
     79   if options.debug:
     80     cmd.append('-g')
     81 
     82   if osname == 'linux':
     83     helper = os.path.join(SCRIPT_DIR, 'nacl_helper_bootstrap_%s' % arch_suffix)
     84     Log('HELPER  = %s' % helper)
     85     cmd.insert(0, helper)
     86 
     87   if dynamic:
     88     if options.debug_libs:
     89       libpath = os.path.join(NACL_SDK_ROOT, 'lib',
     90                             'glibc_%s' % arch_suffix, 'Debug')
     91     else:
     92       libpath = os.path.join(NACL_SDK_ROOT, 'lib',
     93                             'glibc_%s' % arch_suffix, 'Release')
     94     toolchain = '%s_x86_glibc' % osname
     95     sdk_lib_dir = os.path.join(NACL_SDK_ROOT, 'toolchain',
     96                                toolchain, 'x86_64-nacl')
     97     if arch == 'x86-64':
     98       sdk_lib_dir = os.path.join(sdk_lib_dir, 'lib')
     99     else:
    100       sdk_lib_dir = os.path.join(sdk_lib_dir, 'lib32')
    101     ldso = os.path.join(sdk_lib_dir, 'runnable-ld.so')
    102     cmd.append(ldso)
    103     Log('LD.SO   = %s' % ldso)
    104     libpath += ':' + sdk_lib_dir
    105     cmd.append('--library-path')
    106     cmd.append(libpath)
    107 
    108 
    109   cmd += args
    110   Log(cmd)
    111   rtn = subprocess.call(cmd)
    112   return rtn
    113 
    114 
    115 if __name__ == '__main__':
    116   try:
    117     sys.exit(main(sys.argv[1:]))
    118   except Error as e:
    119     sys.stderr.write(str(e) + '\n')
    120     sys.exit(1)
    121