Example #1
0
def main(argv):
    env.update(EXTRA_ENV)
    driver_tools.ParseArgs(argv, DISPatterns)

    inputs = env.get('INPUTS')
    output = env.getone('OUTPUT')

    if len(inputs) == 0:
        Log.Fatal("No input files given")

    if len(inputs) > 1 and output != '':
        Log.Fatal("Cannot have -o with multiple inputs")

    for infile in inputs:
        env.push()
        env.set('input', infile)
        env.set('output', output)

        # When we output to stdout, set redirect_stdout and set log_stdout
        # to False to bypass the driver's line-by-line handling of stdout
        # which is extremely slow when you have a lot of output

        if (filetype.IsLLVMBitcode(infile) or filetype.IsPNaClBitcode(infile)):
            bitcodetype = 'PNaCl' if filetype.IsPNaClBitcode(
                infile) else 'LLVM'
            format = bitcodetype.lower()

            if env.has('FILE_TYPE'):
                sys.stdout.write('%s: %s bitcode\n' % (infile, bitcodetype))
                continue
            env.append('FLAGS', '-bitcode-format=' + format)
            if output == '':
                # LLVM by default outputs to a file if -o is missing
                # Let's instead output to stdout
                env.set('output', '-')
                env.append('FLAGS', '-f')
            driver_tools.Run('${LLVM_DIS} ${FLAGS} ${input} -o ${output}')
        elif filetype.IsELF(infile):
            if env.has('FILE_TYPE'):
                sys.stdout.write('%s: ELF\n' % infile)
                continue
            flags = env.get('FLAGS')
            if len(flags) == 0:
                env.append('FLAGS', '-d')
            if output == '':
                # objdump to stdout
                driver_tools.Run('"${OBJDUMP}" ${FLAGS} ${input}')
            else:
                # objdump always outputs to stdout, and doesn't recognize -o
                # Let's add this feature to be consistent.
                fp = DriverOpen(output, 'w')
                driver_tools.Run('${OBJDUMP} ${FLAGS} ${input}',
                                 redirect_stdout=fp)
                DriverClose(fp)
        else:
            Log.Fatal('Unknown file type')
        env.pop()
    # only reached in case of no errors
    return 0
Example #2
0
 def test_inplace_finalize(self):
   if not driver_test_utils.CanRunHost():
     return
   ll, bc = self.getFakeLLAndBitcodeFile()
   self.assertTrue(filetype.FileType(bc.name) == 'po')
   self.assertTrue(filetype.IsLLVMBitcode(bc.name))
   self.assertFalse(filetype.IsPNaClBitcode(bc.name))
   driver_tools.RunDriver('finalize', [bc.name])
   self.assertFalse(filetype.IsLLVMBitcode(bc.name))
   self.assertTrue(filetype.IsPNaClBitcode(bc.name))
   self.assertTrue(filetype.FileType(bc.name) == 'pexe')
Example #3
0
def RunSandboxedCompiler(use_sz):
  driver_tools.CheckTranslatorPrerequisites()
  infile = env.getone('input')
  is_pnacl = filetype.IsPNaClBitcode(infile)
  if not is_pnacl and not env.getbool('ALLOW_LLVM_BITCODE_INPUT'):
    Log.Fatal('Translator expects finalized PNaCl bitcode. '
              'Pass --allow-llvm-bitcode-input to override.')
  threads = int(env.getone('SPLIT_MODULE'))
  command = [driver_tools.SelLdrCommand(),
             '-a', # Allow file access
             '-E NACL_IRT_PNACL_TRANSLATOR_COMPILE_INPUT=%s' % infile]
  driver_tools.AddListToEnv(command, 'NACL_IRT_PNACL_TRANSLATOR_COMPILE_OUTPUT',
                            GetObjectFiles(use_sz))
  driver_tools.AddListToEnv(command, 'NACL_IRT_PNACL_TRANSLATOR_COMPILE_ARG',
                            BuildOverrideCompilerCommandLine(is_pnacl, use_sz))
  command.extend(['-E NACL_IRT_PNACL_TRANSLATOR_COMPILE_THREADS=%d' % threads,
                  '--'])
  if use_sz:
    command.append('${PNACL_SZ_SB}')
  else:
    command.append('${LLC_SB}')
  driver_tools.Run(' '.join(command),
                   # stdout/stderr will be automatically dumped
                   # upon failure
                   redirect_stderr=subprocess.PIPE,
                   redirect_stdout=subprocess.PIPE)
Example #4
0
def main(argv):
    env.update(EXTRA_ENV)
    driver_tools.ParseArgs(argv, PATTERNS)

    inputs = env.get('INPUTS')

    if len(inputs) == 0:
        Log.Fatal("No input files given")

    for infile in inputs:
        driver_tools.CheckPathLength(infile)
        env.push()
        env.set('input', infile)

        # For frozen PNaCl bitcode, use 'llvm-nm -bitcode-format=pnacl'. For all
        # other formats, use the binutils nm with our gold plugin.
        # Update: llvm-nm -bitcode-format=pnacl is currently disabled.
        if filetype.IsPNaClBitcode(infile):
            Log.Fatal(
                'nm on finalized bitcode is currently disabled.\n'
                'See: https://code.google.com/p/nativeclient/issues/detail?id=3993'
            )
        else:
            env.set('TOOLNAME', '${NM}')
            env.append('FLAGS', '--plugin=${GOLD_PLUGIN_SO}')

        driver_tools.Run('"${TOOLNAME}" ${FLAGS} ${input}')
        env.pop()

    # only reached in case of no errors
    return 0
Example #5
0
def main(argv):
    env.update(EXTRA_ENV)
    driver_tools.ParseArgs(argv, PATTERNS)

    inputs = env.get('INPUTS')

    if len(inputs) == 0:
        Log.Fatal("No input files given")

    for infile in inputs:
        env.push()
        env.set('input', infile)

        # For frozen PNaCl bitcode, use 'llvm-nm -bitcode-format=pnacl'. For all
        # other formats, use the binutils nm with our gold plugin.
        if filetype.IsPNaClBitcode(infile):
            env.set('TOOLNAME', '${LLVM_NM}')
            env.append('FLAGS', '-bitcode-format=pnacl')
        else:
            env.set('TOOLNAME', '${NM}')
            env.append('FLAGS', '--plugin=${GOLD_PLUGIN_SO}')

        driver_tools.Run('"${TOOLNAME}" ${FLAGS} ${input}')
        env.pop()

    # only reached in case of no errors
    return 0
Example #6
0
def main(argv):
  env.update(EXTRA_ENV)
  driver_tools.ParseArgs(argv, PrepPatterns)

  inputs = env.get('INPUTS')
  output = env.getone('OUTPUT')

  if len(inputs) != 1:
    Log.Fatal('Can only have one input')
  f_input = inputs[0]

  # Allow in-place file changes if output isn't specified..
  if output != '':
    f_output = output
  else:
    f_output = f_input

  if env.getbool('DISABLE_FINALIZE') or filetype.IsPNaClBitcode(f_input):
    # Just copy the input file to the output file.
    if f_input != f_output:
      shutil.copyfile(f_input, f_output)
    return 0

  opt_flags = ['-disable-opt', '-strip', '-strip-metadata',
               '--bitcode-format=pnacl', f_input, '-o', f_output]
  # Transform the file, and convert it to a PNaCl bitcode file.
  driver_tools.RunDriver('opt', opt_flags)
  return 0
Example #7
0
def main(argv):
    env.update(EXTRA_ENV)
    driver_tools.ParseArgs(argv, PATTERNS)

    args = env.get('ARGS')
    input = pathtools.normalize(args[-1])
    if filetype.IsPNaClBitcode(input):
        env.append('ARGS', '--bitcode-format=pnacl')
    driver_tools.Run('"${PNACL_ABICHECK}" ${ARGS}')
    return 0
 def test_finalize(self):
   """ Test that pnacl-ld will finalize the pexe when requested"""
   if not driver_test_utils.CanRunHost():
     return
   bitcode = self.getBitcode()
   with self.getTemp(suffix='.pexe') as temp_output:
     driver_tools.RunDriver(
       'pnacl-ld', ['--finalize', bitcode.name, '-o', temp_output.name])
     self.assertFalse(filetype.IsLLVMBitcode(temp_output.name))
     self.assertTrue(filetype.IsPNaClBitcode(temp_output.name))
Example #9
0
 def test_finalize_keep_syms(self):
     """Test that finalize is still able to create a pexe w/ -no-strip-syms."""
     if not driver_test_utils.CanRunHost():
         return
     bc = self.getBCWithDebug()
     self.assertTrue(filetype.FileType(bc.name) == 'po')
     self.assertTrue(filetype.IsLLVMBitcode(bc.name))
     self.assertFalse(filetype.IsPNaClBitcode(bc.name))
     driver_tools.RunDriver('pnacl-finalize', [bc.name, '--no-strip-syms'])
     self.assertFalse(filetype.IsLLVMBitcode(bc.name))
     self.assertTrue(filetype.IsPNaClBitcode(bc.name))
     self.assertTrue(filetype.FileType(bc.name) == 'pexe')
     # Use pnacl-dis instead of llvm-nm, since llvm-nm won't know how to
     # handle finalized bitcode for now:
     # https://code.google.com/p/nativeclient/issues/detail?id=3993
     with self.getTemp(suffix='.ll') as temp_ll:
         driver_tools.RunDriver('pnacl-dis', [bc.name, '-o', temp_ll.name])
         with open(temp_ll.name, 'r') as dis_file:
             file_contents = dis_file.read()
             self.assertTrue(re.search(r'define .*@baz', file_contents))
             self.assertTrue(re.search(r'define .*@foo', file_contents))
Example #10
0
def RunCompiler(infile, outfile, outfiletype, use_sz):
  env.push()
  env.setmany(input=infile, output=outfile, outfiletype=outfiletype)
  if env.getbool('SANDBOXED'):
    RunSandboxedCompiler(use_sz)
  else:
    args = ["${RUN_SZ}" if use_sz else "${RUN_LLC}"]
    if filetype.IsPNaClBitcode(infile):
      args.append("-bitcode-format=pnacl")
    elif filetype.IsLLVMBitcode(infile):
      if not env.getbool('ALLOW_LLVM_BITCODE_INPUT'):
        Log.Fatal('Translator expects finalized PNaCl bitcode. '
                  'Pass --allow-llvm-bitcode-input to override.')
    driver_tools.Run(' '.join(args))
  env.pop()
  return 0
def RunLLCSandboxed():
    driver_tools.CheckTranslatorPrerequisites()
    infile = env.getone('input')
    outfile = env.getone('output')
    if not filetype.IsPNaClBitcode(infile):
        Log.Fatal('Input to sandboxed translator must be PNaCl bitcode')
    script = MakeSelUniversalScriptForLLC(infile, outfile)
    command = (
        '${SEL_UNIVERSAL_PREFIX} ${SEL_UNIVERSAL} ${SEL_UNIVERSAL_FLAGS} '
        '-- ${LLC_SB}')
    driver_tools.Run(
        command,
        stdin_contents=script,
        # stdout/stderr will be automatically dumped
        # upon failure
        redirect_stderr=subprocess.PIPE,
        redirect_stdout=subprocess.PIPE)
Example #12
0
    def getFakePexe(self, finalized=True):
        # Even --dry-run requires a file to exist, so make a fake pexe.
        # It even cares that the file is really bitcode.
        with self.getTemp(suffix='.ll', close=False) as t:
            with self.getTemp(suffix='.pexe') as p:
                t.write('''
define i32 @_start() {
  ret i32 0
}
''')
                t.close()
                driver_tools.RunDriver('as', [t.name, '-o', p.name])
                if finalized:
                    driver_tools.RunDriver('finalize', [p.name])
                    self.assertTrue(filetype.IsPNaClBitcode(p.name))
                else:
                    self.assertTrue(filetype.IsLLVMBitcode(p.name))
                return p
Example #13
0
def RunLLCSandboxed():
    driver_tools.CheckTranslatorPrerequisites()
    infile = env.getone('input')
    outfile = env.getone('output')
    is_pnacl = filetype.IsPNaClBitcode(infile)
    if not is_pnacl and not env.getbool('ALLOW_LLVM_BITCODE_INPUT'):
        Log.Fatal('Translator expects finalized PNaCl bitcode. '
                  'Pass --allow-llvm-bitcode-input to override.')
    script = MakeSelUniversalScriptForLLC(infile, outfile, is_pnacl)
    command = (
        '${SEL_UNIVERSAL_PREFIX} ${SEL_UNIVERSAL} ${SEL_UNIVERSAL_FLAGS} '
        '-- ${LLC_SB}')
    driver_tools.Run(
        command,
        stdin_contents=script,
        # stdout/stderr will be automatically dumped
        # upon failure
        redirect_stderr=subprocess.PIPE,
        redirect_stdout=subprocess.PIPE)
Example #14
0
def RunLLC(infile, outfile, outfiletype):
  env.push()
  env.setmany(input=infile, output=outfile, outfiletype=outfiletype)
  if env.getbool('SANDBOXED'):
    is_shared, soname, needed = RunLLCSandboxed()
    # Ignore is_shared, soname, and needed for now, since we aren't
    # dealing with bitcode shared libraries.
    env.pop()
  else:
    args = ["${RUN_LLC}"]
    if filetype.IsPNaClBitcode(infile):
      args.append("-bitcode-format=pnacl")
    elif filetype.IsLLVMBitcode(infile):
      if not env.getbool('ALLOW_LLVM_BITCODE_INPUT'):
        Log.Fatal('Translator expects finalized PNaCl bitcode. '
                  'Pass --allow-llvm-bitcode-input to override.')
    driver_tools.Run(' '.join(args))
    env.pop()
  return 0
Example #15
0
def main(argv):
    env.update(EXTRA_ENV)
    driver_tools.ParseArgs(argv, PrepPatterns)

    inputs = env.get('INPUTS')
    output = env.getone('OUTPUT')

    for path in inputs + [output]:
        driver_tools.CheckPathLength(path)

    if len(inputs) != 1:
        Log.Fatal('Can only have one input')
    f_input = inputs[0]

    # Allow in-place file changes if output isn't specified..
    if output != '':
        f_output = output
    else:
        f_output = f_input

    if env.getbool('DISABLE_FINALIZE') or filetype.IsPNaClBitcode(f_input):
        # Just copy the input file to the output file.
        if f_input != f_output:
            shutil.copyfile(f_input, f_output)
        return 0

    opt_flags = [
        '-disable-opt', '-strip-metadata', '-strip-module-flags',
        '--bitcode-format=pnacl', f_input, '-o', f_output
    ]
    if env.getbool('DISABLE_STRIP_SYMS'):
        opt_flags += ['-strip-debug']
    else:
        opt_flags += ['-strip']
    # Transform the file, and convert it to a PNaCl bitcode file.
    driver_tools.RunDriver('pnacl-opt', opt_flags)
    # Compress the result if requested.
    if env.getbool('COMPRESS'):
        driver_tools.RunDriver('pnacl-compress', [f_output])
    return 0
Example #16
0
def main(argv):
    env.update(EXTRA_ENV)
    driver_tools.ParseArgs(argv, StripPatterns)
    inputs = env.get('INPUTS')
    output = env.getone('OUTPUT')
    for path in inputs + [output]:
        driver_tools.CheckPathLength(path)

    if len(inputs) > 1 and output != '':
        Log.Fatal('Cannot have -o with multiple inputs')

    if '--info' in env.get('STRIP_FLAGS'):
        code, _, _ = driver_tools.Run('${STRIP} ${STRIP_FLAGS}')
        return code

    for f in inputs:
        if output != '':
            f_output = output
        else:
            f_output = f
        if filetype.IsPNaClBitcode(f):
            # PNaCl-format bitcode has no symbols, i.e. it is already stripped.
            if f != f_output:
                shutil.copyfile(f, f_output)
        elif filetype.IsLLVMBitcode(f):
            driver_tools.RunWithEnv('${RUN_OPT}', input=f, output=f_output)
        elif filetype.IsELF(f) or filetype.IsNativeArchive(f):
            driver_tools.RunWithEnv('${RUN_STRIP}', input=f, output=f_output)
        elif filetype.IsBitcodeArchive(f):
            # The strip tool supports native archives, but it does not support the
            # LLVM gold plugin so cannot handle bitcode.  There is also no bitcode
            # tool like opt that support archives.
            Log.Fatal('%s: strip does not support bitcode archives',
                      pathtools.touser(f))
        else:
            Log.Fatal('%s: File is neither ELF, nor bitcode',
                      pathtools.touser(f))
    return 0
Example #17
0
def RunLLCSandboxed():
  driver_tools.CheckTranslatorPrerequisites()
  infile = env.getone('input')
  outfile = env.getone('output')
  if not filetype.IsPNaClBitcode(infile):
    Log.Fatal('Input to sandboxed translator must be PNaCl bitcode')
  script = MakeSelUniversalScriptForLLC(infile, outfile)
  command = ('${SEL_UNIVERSAL_PREFIX} ${SEL_UNIVERSAL} ${SEL_UNIVERSAL_FLAGS} '
    '-- ${LLC_SB}')
  _, stdout, _  = driver_tools.Run(command,
                                stdin_contents=script,
                                # stdout/stderr will be automatically dumped
                                # upon failure
                                redirect_stderr=subprocess.PIPE,
                                redirect_stdout=subprocess.PIPE)
  # Get the values returned from the llc RPC to use in input to ld
  is_shared = re.search(r'output\s+0:\s+i\(([0|1])\)', stdout).group(1)
  is_shared = (is_shared == '1')
  soname = re.search(r'output\s+1:\s+s\("(.*)"\)', stdout).group(1)
  needed_str = re.search(r'output\s+2:\s+s\("(.*)"\)', stdout).group(1)
  # If the delimiter changes, this line needs to change
  needed_libs = [ lib for lib in needed_str.split(r'\n') if lib]
  return is_shared, soname, needed_libs
def main(argv):
    env.update(EXTRA_ENV)
    driver_tools.ParseArgs(argv, TranslatorPatterns)
    driver_tools.GetArch(required=True)

    inputs = env.get('INPUTS')
    output = env.getone('OUTPUT')

    if len(inputs) == 0:
        Log.Fatal("No input files")

    if output == '':
        Log.Fatal("Please specify output file with -o")

    # Find the bitcode file on the command line.
    bcfiles = [
        f for f in inputs
        if not ldtools.IsFlag(f) and (filetype.IsPNaClBitcode(
            f) or filetype.IsLLVMBitcode(f) or filetype.FileType(f) == 'll')
    ]
    if len(bcfiles) > 1:
        Log.Fatal('Expecting at most 1 bitcode file')
    elif len(bcfiles) == 1:
        bcfile = bcfiles[0]
    else:
        bcfile = None

    # If there's a bitcode file, translate it now.
    tng = driver_tools.TempNameGen(inputs + bcfiles, output)
    output_type = env.getone('OUTPUT_TYPE')
    if bcfile:
        sfile = None
        if output_type == 's':
            sfile = output

        ofile = None
        if output_type == 'o':
            ofile = output
        elif output_type != 's':
            ofile = tng.TempNameForInput(bcfile, 'o')

        if sfile:
            RunLLC(bcfile, sfile, outfiletype='asm')
            if ofile:
                RunAS(sfile, ofile)
        else:
            RunLLC(bcfile, ofile, outfiletype='obj')
    else:
        ofile = None

    # If we've been told to stop after translation, stop now.
    if output_type in ('o', 's'):
        return 0

    # Replace the bitcode file with __BITCODE__ in the input list
    if bcfile:
        inputs = ListReplace(inputs, bcfile, '__BITCODE__')
        env.set('INPUTS', *inputs)

    if env.getone('ARCH') == 'LINUX_X8632':
        RunHostLD(ofile, output)
    else:
        RunLD(ofile, output)
    return 0
Example #19
0
def main(argv):
  env.update(EXTRA_ENV)
  driver_tools.ParseArgs(argv, TranslatorPatterns)
  driver_tools.GetArch(required = True)
  SetUpArch()
  SetUpLinkOptions()

  # Now commit to whether or not Subzero is used.
  use_sz = env.getbool('USE_SZ') and not env.getbool('SZ_UNSUPPORTED')

  inputs = env.get('INPUTS')
  output = env.getone('OUTPUT')

  if len(inputs) == 0:
    Log.Fatal("No input files")
  for path in inputs:
    driver_tools.CheckPathLength(path)

  if output == '':
    Log.Fatal("Please specify output file with -o")

  # Find the bitcode file on the command line.
  bcfiles = [f for f in inputs
             if not ldtools.IsFlag(f) and
               (filetype.IsPNaClBitcode(f)
                or filetype.IsLLVMBitcode(f)
                or filetype.FileType(f) == 'll')]
  if len(bcfiles) > 1:
    Log.Fatal('Expecting at most 1 bitcode file')
  elif len(bcfiles) == 1:
    bcfile = bcfiles[0]
  else:
    bcfile = None

  if ((env.getbool('ALLOW_LLVM_BITCODE_INPUT') or
       env.getone('TARGET_OS') != 'nacl' or
       env.getbool('USE_EMULATOR')) and
      env.getone('SPLIT_MODULE') == 'auto'):
    # When llvm input is allowed, the pexe may not be ABI-stable, so do not
    # split it.  Non-ABI-stable pexes may have symbol naming and visibility
    # issues that the current splitting scheme doesn't account for.
    #
    # For now, also do not enable multi-threaded translation when TARGET_OS !=
    # 'nacl', since in these cases we will be using the host toolchain's
    # linker.
    #
    # The x86->arm emulator is very flaky when threading is used, so don't
    # do module splitting when using it.
    env.set('SPLIT_MODULE', 'seq')
  # Do not set -streaming-bitcode for sandboxed mode, because it is already
  # in the default command line.
  if not env.getbool('SANDBOXED') and env.getbool('STREAM_BITCODE'):
    env.append('LLC_FLAGS_EXTRA', '-streaming-bitcode')

  if env.getone('SPLIT_MODULE') == 'seq':
    env.set('SPLIT_MODULE', '1')
    env.set('SZ_THREADS', '0')
  elif env.getone('SPLIT_MODULE') == 'auto':
    try:
      num_modules = min(4, multiprocessing.cpu_count())
    except NotImplementedError:
      num_modules = 2
    env.set('SPLIT_MODULE', str(num_modules))
    env.set('SZ_THREADS', str(num_modules))
  else:
    num_modules = int(env.getone('SPLIT_MODULE'))
    if num_modules < 1:
      Log.Fatal('Value given for -split-module must be > 0')
    env.set('SPLIT_MODULE', str(num_modules))
    env.set('SZ_THREADS', str(num_modules))

  modules = env.getone('SPLIT_MODULE')
  module_sched = env.getone('SPLIT_MODULE_SCHED')
  sz_threads = env.getone('SZ_THREADS')
  # TODO(dschuff,jvoung): No need to specify -split-module=X since the IPC
  # already has a parameter for the number of threads and modules.
  env.append('LLC_FLAGS_EXTRA', '-split-module=' + modules)
  env.append('LD_FLAGS', '-split-module=' + ('1' if use_sz else modules))
  env.append('LLC_FLAGS_EXTRA', '-split-module-sched=' + module_sched)
  # In sandboxed mode, the IPC already has a parameter for the number
  # of threads, so no need to specify that again via argv[].
  if not env.getbool('SANDBOXED'):
    env.append('SZ_FLAGS_EXTRA', '--threads=' + sz_threads)

  # If there's a bitcode file, translate it now.
  tng = driver_tools.TempNameGen(inputs + bcfiles, output)
  output_type = env.getone('OUTPUT_TYPE')
  if bcfile:
    sfile = None
    if output_type == 's':
      sfile = output

    ofile = None
    if output_type == 'o':
      ofile = output
    elif output_type != 's':
      ofile = tng.TempNameForInput(bcfile, 'o')

    if sfile:
      RunCompiler(bcfile, sfile, outfiletype='asm', use_sz=use_sz)
      if ofile:
        RunAS(sfile, ofile)
    else:
      RunCompiler(bcfile, ofile, outfiletype='obj', use_sz=use_sz)
  else:
    ofile = None

  # If we've been told to stop after translation, stop now.
  if output_type in ('o','s'):
    return 0

  if use_sz:
    # Reset SPLIT_MODULE to 1 to fall back to normal linking behavior.
    env.set('SPLIT_MODULE', '1')

  # Replace the bitcode file with __BITCODE__ in the input list
  if bcfile:
    inputs = ListReplace(inputs, bcfile, '__BITCODE__')
    env.set('INPUTS', *inputs)
  if int(env.getone('SPLIT_MODULE')) > 1:
    modules = int(env.getone('SPLIT_MODULE'))
    for i in range(1, modules):
      filename = ofile + '.module%d' % i
      TempFiles.add(filename)
      env.append('INPUTS', filename)

  if env.getone('TARGET_OS') != 'nacl':
    RunHostLD(ofile, output)
  else:
    RunLD(ofile, output)
  return 0
Example #20
0
def main(argv):
    env.update(EXTRA_ENV)
    driver_tools.ParseArgs(argv, TranslatorPatterns)
    driver_tools.GetArch(required=True)

    inputs = env.get('INPUTS')
    output = env.getone('OUTPUT')

    if len(inputs) == 0:
        Log.Fatal("No input files")

    if output == '':
        Log.Fatal("Please specify output file with -o")

    # Find the bitcode file on the command line.
    bcfiles = [
        f for f in inputs
        if not ldtools.IsFlag(f) and (filetype.IsPNaClBitcode(
            f) or filetype.IsLLVMBitcode(f) or filetype.FileType(f) == 'll')
    ]
    if len(bcfiles) > 1:
        Log.Fatal('Expecting at most 1 bitcode file')
    elif len(bcfiles) == 1:
        bcfile = bcfiles[0]
    else:
        bcfile = None

    if not env.getbool('SPLIT_MODULE'):
        try:
            env.set('SPLIT_MODULE', str(min(4, multiprocessing.cpu_count())))
        except NotImplementedError:
            env.set('SPLIT_MODULE', '2')
    elif int(env.getone('SPLIT_MODULE')) < 1:
        Log.Fatal('Value given for -split-module must be > 0')
    if (env.getbool('ALLOW_LLVM_BITCODE_INPUT')
            or env.getone('ARCH') == 'X8632_LINUX'
            or env.getbool('USE_EMULATOR')):
        # When llvm input is allowed, the pexe may not be ABI-stable, so do not
        # split it. For now also do not support threading non-SFI baremetal mode.
        # Non-ABI-stable pexes may have symbol naming and visibility issues that the
        # current splitting scheme doesn't account for.
        # If the link would require a non-standard command line, do not split the
        # modules because the sandboxed linker doesn't support that combination.
        # The x86->arm emulator is very flaky when threading is used, so don't
        # do module splitting when using it.
        env.set('SPLIT_MODULE', '1')
    else:
        modules = env.getone('SPLIT_MODULE')
        if modules != '1':
            env.append('LLC_FLAGS_EXTRA', '-split-module=' + modules)
            env.append('LD_FLAGS', '-split-module=' + modules)
        if not env.getbool('SANDBOXED') and env.getbool('STREAM_BITCODE'):
            # Do not set -streaming-bitcode for sandboxed mode, because it is already
            # in the default command line.
            env.append('LLC_FLAGS_EXTRA', '-streaming-bitcode')

    # If there's a bitcode file, translate it now.
    tng = driver_tools.TempNameGen(inputs + bcfiles, output)
    output_type = env.getone('OUTPUT_TYPE')
    if bcfile:
        sfile = None
        if output_type == 's':
            sfile = output

        ofile = None
        if output_type == 'o':
            ofile = output
        elif output_type != 's':
            ofile = tng.TempNameForInput(bcfile, 'o')

        if sfile:
            RunLLC(bcfile, sfile, outfiletype='asm')
            if ofile:
                RunAS(sfile, ofile)
        else:
            RunLLC(bcfile, ofile, outfiletype='obj')
    else:
        ofile = None

    # If we've been told to stop after translation, stop now.
    if output_type in ('o', 's'):
        return 0

    # Replace the bitcode file with __BITCODE__ in the input list
    if bcfile:
        inputs = ListReplace(inputs, bcfile, '__BITCODE__')
        env.set('INPUTS', *inputs)
    if int(env.getone('SPLIT_MODULE')) > 1:
        modules = int(env.getone('SPLIT_MODULE'))
        for i in range(1, modules):
            filename = ofile + '.module%d' % i
            TempFiles.add(filename)
            env.append('INPUTS', filename)

    if env.getone('ARCH') == 'X8632_LINUX':
        RunHostLD(ofile, output)
    else:
        RunLD(ofile, output)
    return 0
Example #21
0
def main(argv):
  env.update(EXTRA_ENV)
  driver_tools.ParseArgs(argv, TranslatorPatterns)
  if env.getbool('SHARED'):
    Log.Fatal('Not handling SHARED')

  driver_tools.GetArch(required = True)

  inputs = env.get('INPUTS')
  output = env.getone('OUTPUT')

  if len(inputs) == 0:
    Log.Fatal("No input files")

  if output == '':
    Log.Fatal("Please specify output file with -o")

  # Find the bitcode file on the command line.
  bcfiles = [f for f in inputs
             if not ldtools.IsFlag(f) and
               (filetype.IsPNaClBitcode(f)
                or filetype.IsLLVMBitcode(f)
                or filetype.FileType(f) == 'll')]
  if len(bcfiles) > 1:
    Log.Fatal('Expecting at most 1 bitcode file')
  elif len(bcfiles) == 1:
    bcfile = bcfiles[0]
  else:
    bcfile = None

  # If there's a bitcode file, translate it now.
  tng = driver_tools.TempNameGen(inputs + bcfiles, output)
  output_type = env.getone('OUTPUT_TYPE')
  if bcfile:
    sfile = None
    if output_type == 's':
      sfile = output
    elif env.getbool('FORCE_INTERMEDIATE_S'):
      sfile = tng.TempNameForInput(bcfile, 's')

    ofile = None
    if output_type == 'o':
      ofile = output
    elif output_type != 's':
      ofile = tng.TempNameForInput(bcfile, 'o')

    if sfile:
      RunLLC(bcfile, sfile, outfiletype='asm')
      if ofile:
        RunAS(sfile, ofile)
    else:
      RunLLC(bcfile, ofile, outfiletype='obj')
  else:
    ofile = None

  # If we've been told to stop after translation, stop now.
  if output_type in ('o','s'):
    return 0

  # Replace the bitcode file with __BITCODE__ in the input list
  if bcfile:
    inputs = ListReplace(inputs, bcfile, '__BITCODE__')
    env.set('INPUTS', *inputs)

  # Determine the output type, in this order of precedence:
  # 1) Output type can be specified on command-line (-S, -c, -static)
  #    -S and -c are handled above by the check that output_type in ('o', 's').
  # 2) Otherwise, assume static nexe output.
  if env.getbool('STATIC'):
    output_type = 'nexe'
  else:
    # Until we stabilize the ABI for shared libraries,
    # assume that pnacl-translate only handles pexes -> nexes,
    # to avoid a dependency on bitcode metadata.
    output_type = 'nexe'
    env.set('STATIC', '1')

  assert output_type in ('so','nexe')
  if env.getone('ARCH') == 'LINUX_X8632':
    RunHostLD(ofile, output)
  else:
    RunLD(ofile, output)
  return 0