Example #1
0
def main(argv):
  env.update(EXTRA_ENV)
  driver_tools.ParseArgs(argv, StripPatterns)
  inputs = env.get('INPUTS')
  output = env.getone('OUTPUT')

  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 driver_tools.IsBitcode(f):
      driver_tools.RunWithEnv('${RUN_OPT}', input=f, output=f_output)
      if env.getbool('DO_WRAP'):
        driver_tools.WrapBitcode(f_output)
    elif driver_tools.IsELF(f):
      driver_tools.RunWithEnv('${RUN_STRIP}', input=f, output=f_output)
    else:
      Log.Fatal('%s: File is neither ELF nor bitcode', pathtools.touser(f))
  return 0
def main(argv):
  env.update(EXTRA_ENV)
  driver_tools.ParseArgs(argv, StripPatterns)
  inputs = env.get('INPUTS')
  output = env.getone('OUTPUT')

  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 driver_tools.IsBitcode(f):
      driver_tools.RunWithEnv('${RUN_OPT}', input=f, output=f_output)
      if env.getbool('DO_WRAP'):
        driver_tools.WrapBitcode(f_output)
    elif driver_tools.IsELF(f) or driver_tools.IsNativeArchive(f):
      driver_tools.RunWithEnv('${RUN_STRIP}', input=f, output=f_output)
    elif driver_tools.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 #3
0
def RunLDSandboxed():
  CheckTranslatorPrerequisites()
  # The "main" input file is the application's combined object file.
  all_inputs = env.get('inputs')

  main_input = env.getone('LLC_TRANSLATED_FILE')
  if not main_input:
    Log.Fatal("Sandboxed LD requires one shm input file")

  outfile = env.getone('output')

  files = LinkerFiles(all_inputs)
  ld_flags = env.get('LD_FLAGS')

  script = MakeSelUniversalScriptForLD(ld_flags,
                                       main_input,
                                       files,
                                       outfile)


  Run('${SEL_UNIVERSAL_PREFIX} ${SEL_UNIVERSAL} ' +
      '${SEL_UNIVERSAL_FLAGS} -- ${LD_SB}',
      stdin_contents=script,
      # stdout/stderr will be automatically dumped
      # upon failure
      redirect_stderr=subprocess.PIPE,
      redirect_stdout=subprocess.PIPE)
def main(argv):
  env.update(EXTRA_ENV)

  ParseArgs(argv, LDPatterns)

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

  if output == '':
    output = pathtools.normalize('a.out')

  # Expand all parameters
  # This resolves -lfoo into actual filenames,
  # and expands linker scripts into command-line arguments.
  inputs = ldtools.ExpandInputs(inputs,
                                env.get('SEARCH_DIRS'),
                                env.getbool('STATIC'),
                                ldtools.LibraryTypes.NATIVE)

  env.push()
  env.set('inputs', *inputs)
  env.set('output', output)

  if env.getbool('SANDBOXED'):
    RunLDSandboxed()
  else:
    Run('${RUN_LD}')
  env.pop()
  # only reached in case of no errors
  return 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)
    if filetype.IsLLVMBitcode(infile):
      # Hack to support newlib build.
      # Newlib determines whether the toolchain supports .init_array, etc., by
      # compiling a small test and looking for a specific section tidbit using
      # "readelf -S". Since pnacl compiles to bitcode, readelf isn't available.
      # (there is a line: "if ${READELF} -S conftest | grep -e INIT_ARRAY"
      # in newlib's configure file).
      # TODO(sehr): we may want to implement a whole readelf on bitcode.
      flags = env.get('FLAGS')
      if len(flags) == 1 and flags[0] == '-S':
        print 'INIT_ARRAY'
        return 0
      Log.Fatal('Cannot handle pnacl-readelf %s' % str(argv))
      return 1
    driver_tools.Run('"${READELF}" ${FLAGS} ${input}')
    env.pop()

  # only reached in case of no errors
  return 0
Example #6
0
def DoTranslate(infile, outfile):
  args = env.get('TRANSLATE_FLAGS')
  args += ['-Wl,'+s for s in env.get('LD_FLAGS_NATIVE')]
  if infile:
    args += [infile]
  args += [s for s in env.get('NATIVE_OBJECTS')]
  args += ['-o', outfile]
  RunDriver('translate', args)
Example #7
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 #8
0
def DoTranslate(infile, outfile):
  args = env.get('TRANSLATE_FLAGS')
  args += ['-Wl,'+s for s in env.get('LD_FLAGS_NATIVE')]
  if infile:
    args += [infile]
  args += ['-Wl,'+s if ldtools.IsFlag(s) else s
           for s in env.get('NATIVE_OBJECTS')]
  args += ['-o', outfile]
  RunDriver('translate', args)
def DoTranslate(infile, outfile):
  args = env.get('TRANSLATE_FLAGS')
  args += ['-Wl,'+s for s in env.get('LD_FLAGS_NATIVE')]
  if infile:
    args += [infile]
  args += ['-Wl,'+s if ldtools.IsFlag(s) else s
           for s in env.get('NATIVE_OBJECTS')]
  args += ['-o', outfile]
  RunDriver('pnacl-translate', args)
Example #10
0
def main(argv):
  env.update(EXTRA_ENV)

  ParseArgs(argv, LDPatterns)

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

  if output == '':
    output = pathtools.normalize('a.out')

  # As we will modify the output file in-place for non-SFI, we output
  # the file to a temporary file first and then rename it. Otherwise,
  # build systems such as make assume the output file is ready even
  # if the last build failed during the in-place update.
  tmp_output = output + '.tmp'

  # Expand all parameters
  # This resolves -lfoo into actual filenames,
  # and expands linker scripts into command-line arguments.
  inputs = ldtools.ExpandInputs(inputs,
                                env.get('SEARCH_DIRS'),
                                True,
                                ldtools.LibraryTypes.NATIVE)

  env.push()
  env.set('inputs', *inputs)
  env.set('output', tmp_output)

  if env.getbool('SANDBOXED'):
    RunLDSandboxed()
  else:
    Run('${RUN_LD}')

  if env.getbool('NONSFI_NACL'):
    # Remove PT_INTERP in non-SFI binaries as we never use host's
    # dynamic linker/loader.
    #
    # This is necessary otherwise we get a statically linked
    # executable that is not directly runnable by Linux, because Linux
    # tries to load the non-existent file that PT_INTERP points to.
    #
    # This is fairly hacky.  It would be better if the linker provided
    # an option for omitting PT_INTERP (e.g. "--dynamic-linker ''").
    RemoveInterpProgramHeader(tmp_output)
  if driver_tools.IsWindowsPython() and os.path.exists(output):
    # On Windows (but not on Unix), the os.rename() call would fail if the
    # output file already exists.
    os.remove(output)
  os.rename(tmp_output, output)
  env.pop()
  # only reached in case of no errors
  return 0
Example #11
0
def RunLD(infile, outfile):
    inputs = env.get('INPUTS')
    if infile:
        inputs = ListReplace(inputs, '__BITCODE__',
                             '--llc-translated-file=' + infile)
    ToggleDefaultCommandlineLD(inputs, infile)
    env.set('ld_inputs', *inputs)
    args = env.get('LD_ARGS') + ['-o', outfile]
    if not env.getbool('SHARED') and env.getbool('STDLIB'):
        args += env.get('LD_ARGS_ENTRY')
    args += env.get('LD_FLAGS')
    driver_tools.RunDriver('nativeld', args)
Example #12
0
def main(argv):
    env.update(EXTRA_ENV)

    ParseArgs(argv, LDPatterns)

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

    if output == '':
        output = pathtools.normalize('a.out')

    # As we will modify the output file in-place for non-SFI, we output
    # the file to a temporary file first and then rename it. Otherwise,
    # build systems such as make assume the output file is ready even
    # if the last build failed during the in-place update.
    tmp_output = output + '.tmp'

    # Expand all parameters
    # This resolves -lfoo into actual filenames,
    # and expands linker scripts into command-line arguments.
    inputs = ldtools.ExpandInputs(inputs, env.get('SEARCH_DIRS'), True,
                                  ldtools.LibraryTypes.NATIVE)

    env.push()
    env.set('inputs', *inputs)
    env.set('output', tmp_output)

    if env.getbool('SANDBOXED'):
        RunLDSandboxed()
    else:
        Run('${RUN_LD}')

    if env.getbool('NONSFI_NACL'):
        # Remove PT_INTERP in non-SFI binaries as we never use host's
        # dynamic linker/loader.
        #
        # This is necessary otherwise we get a statically linked
        # executable that is not directly runnable by Linux, because Linux
        # tries to load the non-existent file that PT_INTERP points to.
        #
        # This is fairly hacky.  It would be better if the linker provided
        # an option for omitting PT_INTERP (e.g. "--dynamic-linker ''").
        RemoveInterpProgramHeader(tmp_output)
    if driver_tools.IsWindowsPython() and os.path.exists(output):
        # On Windows (but not on Unix), the os.rename() call would fail if the
        # output file already exists.
        os.remove(output)
    os.rename(tmp_output, output)
    env.pop()
    # only reached in case of no errors
    return 0
Example #13
0
def BuildOverrideCompilerCommandLine(is_pnacl, use_sz):
  if use_sz:
    # Subzero doesn't allow -mcpu=X tuning, only -mattr=X.
    extra_flags = env.get('SZ_FLAGS_EXTRA')
  else:
    extra_flags = env.get('LLC_FLAGS_EXTRA')
    # The mcpu is not part of the default flags, so append that too.
    mcpu = env.getone('LLC_MCPU')
    if mcpu:
      extra_flags.append(mcpu)
  if not is_pnacl:
    extra_flags.append('-bitcode-format=llvm')
  return extra_flags
Example #14
0
def RunLD(infile, outfile):
    inputs = env.get("INPUTS")
    if infile:
        inputs = ListReplace(inputs, "__BITCODE__", "--llc-translated-file=" + infile)
    ToggleDefaultCommandlineLD(inputs, infile)
    env.set("ld_inputs", *inputs)
    args = env.get("LD_ARGS") + ["-o", outfile]
    if not env.getbool("SHARED") and env.getbool("STDLIB"):
        args += env.get("LD_ARGS_ENTRY")
    args += env.get("LD_FLAGS")
    # If there is bitcode, there is also a metadata file.
    if infile and env.getbool("USE_META"):
        args += ["--metadata", "%s.meta" % infile]
    driver_tools.RunDriver("nativeld", args)
Example #15
0
def RunLD(infile, outfile):
  inputs = env.get('INPUTS')
  if infile:
    # Put llc-translated-file at the beginning of the inputs so that it will
    # pull in all needed symbols from any native archives that may also be
    # in the input list. This is in case there are any mixed groups of bitcode
    # and native archives in the link (as is the case with irt_browser_lib)
    inputs.remove('__BITCODE__')
    inputs = ['--llc-translated-file=' + infile] + inputs
  env.set('ld_inputs', *inputs)
  args = env.get('LD_ARGS') + ['-o', outfile]
  if env.getbool('USE_STDLIB'):
    args += env.get('LD_ARGS_ENTRY')
  args += env.get('LD_FLAGS')
  driver_tools.RunDriver('nativeld', args)
def RunLD(infile, outfile):
  inputs = env.get('INPUTS')
  if infile:
    # Put llc-translated-file at the beginning of the inputs so that it will
    # pull in all needed symbols from any native archives that may also be
    # in the input list. This is in case there are any mixed groups of bitcode
    # and native archives in the link (as is the case with irt_browser_lib)
    inputs.remove('__BITCODE__')
    inputs = ['--llc-translated-file=' + infile] + inputs
  env.set('ld_inputs', *inputs)
  args = env.get('LD_ARGS') + ['-o', outfile]
  if env.getbool('USE_STDLIB'):
    args += env.get('LD_ARGS_ENTRY')
  args += env.get('LD_FLAGS')
  driver_tools.RunDriver('nativeld', args)
Example #17
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 driver_tools.IsLLVMBitcode(infile):
            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 driver_tools.IsELF(infile):
            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 = driver_tools.DriverOpen(output, 'w')
                driver_tools.Run('${OBJDUMP} ${FLAGS} ${input}',
                                 redirect_stdout=fp)
                driver_tools.DriverClose(fp)
        else:
            Log.Fatal('Unknown file type')
        env.pop()
    # only reached in case of no errors
    return 0
Example #18
0
def RunDriver(invocation, args, suppress_inherited_arch_args=False):
  """
  RunDriver() is used to invoke "driver" tools, e.g.
  those prefixed  with "pnacl-"

  It automatically appends some additional flags to the invocation
  which were inherited from the current invocation.
  Those flags were preserved by ParseArgs
  """

  if isinstance(args, str):
    args = shell.split(env.eval(args))

  module_name = 'pnacl-%s' % invocation
  script = env.eval('${DRIVER_BIN}/%s' % module_name)
  script = shell.unescape(script)

  inherited_driver_args = env.get('INHERITED_DRIVER_ARGS')
  if suppress_inherited_arch_args:
    inherited_driver_args = FilterOutArchArgs(inherited_driver_args)

  script = pathtools.tosys(script)
  cmd = [script] + args + inherited_driver_args
  Log.Info('Driver invocation: %s', repr(cmd))

  module = __import__(module_name)
  # Save the environment, reset the environment, run
  # the driver module, and then restore the environment.
  env.push()
  env.reset()
  DriverMain(module, cmd)
  env.pop()
Example #19
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 #20
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'):
        # Just copy the input file to the output file.
        if f_input != f_output:
            shutil.copyfile(f_input, f_output)
        return 0

    # Transform the file, and convert it to a PNaCl bitcode file.
    driver_tools.RunWithEnv(' '.join(['${RUN_OPT}', '--bitcode-format=pnacl']),
                            input=inputs[0],
                            output=f_output)
    return 0
Example #21
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 #22
0
 def Func(infile, outfile):
   filtered_list = [pass_option for pass_option in pass_list
                    if pass_option not in env.get('LLVM_PASSES_TO_DISABLE')]
   # Do not serialize use lists into the (non-finalized) pexe. See
   # https://code.google.com/p/nativeclient/issues/detail?id=4190
   filtered_list.append('-preserve-bc-uselistorder=false')
   RunDriver('pnacl-opt', filtered_list + [infile, '-o', outfile])
Example #23
0
def RunDriver(module_name, args, suppress_inherited_arch_args=False):
  """
  RunDriver() is used to invoke "driver" tools, e.g.
  those prefixed  with "pnacl-"

  It automatically appends some additional flags to the invocation
  which were inherited from the current invocation.
  Those flags were preserved by ParseArgs
  """

  if isinstance(args, str):
    args = shell.split(env.eval(args))

  script = env.eval('${DRIVER_BIN}/%s' % module_name)
  script = shell.unescape(script)

  inherited_driver_args = env.get('INHERITED_DRIVER_ARGS')
  if suppress_inherited_arch_args:
    inherited_driver_args = FilterOutArchArgs(inherited_driver_args)

  script = pathtools.tosys(script)
  cmd = [script] + args + inherited_driver_args
  Log.Info('Driver invocation: %s', repr(cmd))

  module = __import__(module_name)
  # Save the environment, reset the environment, run
  # the driver module, and then restore the environment.
  env.push()
  env.reset()
  DriverMain(module, cmd)
  env.pop()
Example #24
0
def RunLD(infile, outfile):
  inputs = env.get('INPUTS')
  if infile:
    inputs = ListReplace(inputs,
                         '__BITCODE__',
                         '--llc-translated-file=' + infile)
  ToggleDefaultCommandlineLD(inputs, infile)
  env.set('ld_inputs', *inputs)
  args = env.get('LD_ARGS') + ['-o', outfile]
  if not env.getbool('SHARED') and env.getbool('STDLIB'):
    args += env.get('LD_ARGS_ENTRY')
  args += env.get('LD_FLAGS')
  # If there is bitcode, there is also a metadata file.
  if infile and env.getbool('USE_META'):
    args += ['--metadata', '%s.meta' % infile]
  driver_tools.RunDriver('nativeld', args)
Example #25
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 #26
0
def RequiresNonStandardLLCCommandline():
  extra_flags = env.get('LLC_FLAGS_EXTRA')
  if extra_flags != []:
    reason = 'Has additional llc flags: %s' % extra_flags
    return (reason, True)

  return (None, False)
Example #27
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
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)
  # Compress the result if requested.
  if env.getbool('COMPRESS'):
    driver_tools.RunDriver('compress', [f_output])
  return 0
Example #29
0
 def Func(infile, outfile):
   filtered_list = [pass_option for pass_option in pass_list
                    if pass_option not in env.get('LLVM_PASSES_TO_DISABLE')]
   # Do not serialize use lists into the (non-finalized) pexe. See
   # https://code.google.com/p/nativeclient/issues/detail?id=4190
   filtered_list.append('-preserve-bc-uselistorder=false')
   RunDriver('pnacl-opt', filtered_list + [infile, '-o', outfile])
Example #30
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
def RequiresNonStandardLLCCommandline():
  extra_flags = env.get('LLC_FLAGS_EXTRA')
  if extra_flags != []:
    reason = 'Has additional llc flags: %s' % extra_flags
    return (reason, True)

  return (None, False)
Example #32
0
def RunCC(infile, output, mode):
    intype = FileType(infile)
    typespec = FileTypeToGCCType(intype)
    include_cxx_headers = (env.get("LANGUAGE") == "CXX") or (intype == "c++")
    env.setbool("INCLUDE_CXX_HEADERS", include_cxx_headers)
    if IsStdinInput(infile):
        infile = "-"
    RunWithEnv("${RUN_CC}", infile=infile, output=output, mode=mode, typespec=typespec)
def BuildOverrideCompilerCommandLine(is_pnacl, use_sz):
  if use_sz:
    # Subzero doesn't allow -mcpu=X tuning, only -mattr=X.
    extra_flags = env.get('SZ_FLAGS_EXTRA')
  else:
    extra_flags = env.get('LLC_FLAGS_EXTRA')
    # The mcpu is not part of the default flags, so append that too.
    mcpu = env.getone('LLC_MCPU')
    if mcpu:
      extra_flags.append(mcpu)
  if not is_pnacl:
    extra_flags.append('-bitcode-format=llvm')
  # command_line is a NUL (\x00) terminated sequence.
  kTerminator = '\0'
  command_line = kTerminator.join(extra_flags) + kTerminator
  command_line_escaped = command_line.replace(kTerminator, '\\x00')
  return len(command_line), command_line_escaped
Example #34
0
def RunTranslate(infile, output, mode):
  if not env.getbool('ALLOW_TRANSLATE'):
    Log.Fatal('%s: Trying to convert bitcode to an object file before '
              'bitcode linking. This is supposed to wait until '
              'translation. Use --pnacl-allow-translate to override.',
              pathtools.touser(infile))
  args = env.get('TRANSLATE_FLAGS') + [mode, infile, '-o', output]
  if env.getbool('PIC'):
    args += ['-fPIC']
  RunDriver('translate', args)
Example #35
0
def RunTranslate(infile, output, mode):
  if not env.getbool('ALLOW_TRANSLATE'):
    Log.Fatal('%s: Trying to convert bitcode to an object file before '
              'bitcode linking. This is supposed to wait until '
              'translation. Use --pnacl-allow-translate to override.',
              pathtools.touser(infile))
  args = env.get('TRANSLATE_FLAGS') + [mode, infile, '-o', output]
  if env.getbool('PIC'):
    args += ['-fPIC']
  RunDriver('translate', args)
Example #36
0
def GetActualFilePathAndType(shortname):
    actual_lib_path = ldtools.FindFile([shortname], env.get('SEARCH_DIRS'),
                                       ldtools.LibraryTypes.ANY)
    if actual_lib_path == None:
        Log.Warning('Could not find path of lib: %s, assuming it is native',
                    shortname)
        file_type = 'so'
    else:
        file_type = FileType(actual_lib_path)
    return actual_lib_path, file_type
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;
Example #38
0
def RunCC(infile, output, mode):
  intype = filetype.FileType(infile)
  typespec = filetype.FileTypeToGCCType(intype)
  include_cxx_headers = (env.get('LANGUAGE') == 'CXX') or (intype == 'c++')
  env.setbool('INCLUDE_CXX_HEADERS', include_cxx_headers)
  if IsStdinInput(infile):
    infile = '-'
  RunWithEnv("${RUN_CC}", infile=infile, output=output,
                          mode=mode,
                          typespec=typespec)
Example #39
0
def RunCC(infile, output, mode):
  intype = filetype.FileType(infile)
  typespec = filetype.FileTypeToGCCType(intype)
  include_cxx_headers = (env.get('LANGUAGE') == 'CXX') or (intype == 'c++')
  env.setbool('INCLUDE_CXX_HEADERS', include_cxx_headers)
  if IsStdinInput(infile):
    infile = '-'
  RunWithEnv("${RUN_CC}", infile=infile, output=output,
                          mode=mode,
                          typespec=typespec)
Example #40
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
Example #41
0
def RequiresNonStandardLLCCommandline():
    if env.getbool("FAST_TRANSLATION"):
        return ("FAST_TRANSLATION", True)

    extra_flags = env.get("LLC_FLAGS_EXTRA")
    if extra_flags != []:
        reason = "Has additional llc flags: %s" % extra_flags
        return (reason, True)

    return (None, False)
Example #42
0
def BuildOverrideLLCCommandLine():
  extra_flags = env.get('LLC_FLAGS_EXTRA')
  # The mcpu is not part of the default flags, so append that too.
  mcpu = env.getone('LLC_MCPU')
  if mcpu:
    extra_flags.append(mcpu)
  # command_line is a NUL (\x00) terminated sequence.
  kTerminator = '\0'
  command_line = kTerminator.join(extra_flags) + kTerminator
  command_line_escaped = command_line.replace(kTerminator, '\\x00')
  return len(command_line), command_line_escaped
def BuildOverrideLLCCommandLine():
  extra_flags = env.get('LLC_FLAGS_EXTRA')
  # The mcpu is not part of the default flags, so append that too.
  mcpu = env.getone('LLC_MCPU')
  if mcpu:
    extra_flags.append(mcpu)
  # command_line is a NUL (\x00) terminated sequence.
  kTerminator = '\0'
  command_line = kTerminator.join(extra_flags) + kTerminator
  command_line_escaped = command_line.replace(kTerminator, '\\x00')
  return len(command_line), command_line_escaped
Example #44
0
def RunCC(infile, output, mode, emit_llvm_flag='-emit-llvm'):
  intype = filetype.FileType(infile)
  typespec = filetype.FileTypeToGCCType(intype)
  include_cxx_headers = ((env.get('LANGUAGE') == 'CXX') or
                         (intype in ('c++', 'c++-header')))
  env.setbool('INCLUDE_CXX_HEADERS', include_cxx_headers)
  if IsStdinInput(infile):
    infile = '-'
  RunWithEnv("${RUN_CC}", infile=infile, output=output,
                          emit_llvm_flag=emit_llvm_flag, mode=mode,
                          typespec=typespec)
Example #45
0
def GetActualFilePathAndType(shortname):
  actual_lib_path = ldtools.FindFile([shortname],
                                     env.get('SEARCH_DIRS'),
                                     ldtools.LibraryTypes.ANY)
  if actual_lib_path == None:
    Log.Warning('Could not find path of lib: %s, assuming it is native',
                shortname)
    file_type = 'so'
  else:
    file_type = FileType(actual_lib_path)
  return actual_lib_path, file_type
Example #46
0
def RunCC(infile, output, mode, emit_llvm_flag='-emit-llvm'):
  intype = filetype.FileType(infile)
  typespec = filetype.FileTypeToGCCType(intype)
  include_cxx_headers = ((env.get('LANGUAGE') == 'CXX') or
                         (intype in ('c++', 'c++-header')))
  env.setbool('INCLUDE_CXX_HEADERS', include_cxx_headers)
  if IsStdinInput(infile):
    infile = '-'
  RunWithEnv("${RUN_CC}", infile=infile, output=output,
                          emit_llvm_flag=emit_llvm_flag, mode=mode,
                          typespec=typespec)
Example #47
0
def BuildDynamicExecutable():
  inputs = Env.get('INPUTS')
  if len(inputs) != 1:
    Log.Fatal('You must specify exactly on input for a dynamic executable.')

  pexe_command = ProcessBuildCommand(BUILD_COMMAND_PEXE_FROM_BC)
  DumpCommand("@bc->pexe", pexe_command)
  driver_tools.RunDriver('clang', pexe_command, suppress_arch=True)

  nexe_command = ProcessBuildCommand(BUILD_COMMAND_NEXE_FROM_PEXE)
  DumpCommand("@pexe->nexe", nexe_command)
  driver_tools.RunDriver('translate', nexe_command)
Example #48
0
def RunTranslate(infile, output, mode):
    if not env.getbool("ALLOW_TRANSLATE"):
        Log.Fatal(
            "%s: Trying to convert bitcode to an object file before "
            "bitcode linking. This is supposed to wait until "
            "translation. Use --pnacl-allow-translate to override.",
            pathtools.touser(infile),
        )
    args = env.get("TRANSLATE_FLAGS") + [mode, infile, "-o", output]
    if env.getbool("PIC"):
        args += ["-fPIC"]
    RunDriver("translate", args)
def FindBaseHost(tool):
  """ Find the base directory for host binaries (i.e. llvm/binutils) """
  if env.has('BPREFIXES'):
    for prefix in env.get('BPREFIXES'):
      if os.path.exists(pathtools.join(prefix, 'bin',
                                       tool + env.getone('EXEC_EXT'))):
        return prefix

  base_pnacl = FindBasePNaCl()
  if not pathtools.exists(pathtools.join(base_pnacl, 'bin',
                          tool + env.getone('EXEC_EXT'))):
    Log.Fatal('Could not find PNaCl host directory for ' + tool)
  return base_pnacl
Example #50
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 #51
0
def RunLDSandboxed():
    if not env.getbool('USE_STDLIB'):
        Log.Fatal('-nostdlib is not supported by the sandboxed translator')
    CheckTranslatorPrerequisites()
    # The "main" input file is the application's combined object file.
    all_inputs = env.get('inputs')

    main_input = env.getone('LLC_TRANSLATED_FILE')
    if not main_input:
        Log.Fatal("Sandboxed LD requires one shm input file")

    outfile = env.getone('output')

    modules = int(env.getone('SPLIT_MODULE'))
    if modules > 1:
        first_mainfile = all_inputs.index(main_input)
        first_extra = all_inputs.index(main_input) + modules
        # Just the split module files
        llc_outputs = all_inputs[first_mainfile:first_extra]
        # everything else
        all_inputs = all_inputs[:first_mainfile] + all_inputs[first_extra:]
    else:
        llc_outputs = [main_input]

    files = LinkerFiles(all_inputs)
    ld_flags = env.get('LD_FLAGS')

    script = MakeSelUniversalScriptForLD(ld_flags, llc_outputs, files, outfile)

    Run(
        '${SEL_UNIVERSAL_PREFIX} ${SEL_UNIVERSAL} ' +
        '${SEL_UNIVERSAL_FLAGS} -- ${LD_SB}',
        stdin_contents=script,
        # stdout/stderr will be automatically dumped
        # upon failure
        redirect_stderr=subprocess.PIPE,
        redirect_stdout=subprocess.PIPE)
Example #52
0
def RunLDSandboxed():
    CheckTranslatorPrerequisites()
    # The "main" input file is the application's combined object file.
    all_inputs = env.get('inputs')

    main_input = env.getone('LLC_TRANSLATED_FILE')
    if not main_input:
        Log.Fatal("Sandboxed LD requires one shm input file")

    outfile = env.getone('output')

    files = LinkerFiles(all_inputs)
    ld_flags = env.get('LD_FLAGS')

    script = MakeSelUniversalScriptForLD(ld_flags, main_input, files, outfile)

    Run(
        '${SEL_UNIVERSAL_PREFIX} ${SEL_UNIVERSAL} ' +
        '${SEL_UNIVERSAL_FLAGS} -- ${LD_SB}',
        stdin_contents=script,
        # stdout/stderr will be automatically dumped
        # upon failure
        redirect_stderr=subprocess.PIPE,
        redirect_stdout=subprocess.PIPE)
Example #53
0
def main(argv):
    if len(argv) == 0:
        print get_help(argv)
        return 1

    env.update(EXTRA_ENV)
    driver_tools.ParseArgs(argv, PATTERNS)

    # Note: --plugin must come after the command flags, but before the filename.
    #       (definitely confirmed that it cannot go before the command)
    #       for now assume command is just the very first args
    args = env.get('ARGS')
    command = args.pop(0)
    env.set('COMMAND', command)
    env.set('ARGS', *args)
    driver_tools.Run('"${AR}" ${COMMAND} --plugin=${GOLD_PLUGIN_SO} ${ARGS}')
    # only reached in case of no errors
    return 0
Example #54
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

  Compress(f_input, f_output)
  return 0
Example #55
0
def main(argv):
  env.update(EXTRA_ENV)
  driver_tools.ParseArgs(argv, ASPatterns)
  arch = driver_tools.GetArch()

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

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

  num_inputs = len(inputs)
  if num_inputs > 1:
    Log.Fatal('Expecting exactly one input file')
  elif num_inputs == 1:
    the_input = inputs[0]
  else:
    # stdin
    the_input = '-'


  if arch:
    output_type = 'o'
  else:
    output_type = 'po'

  if output == '':
    output = 'a.out'

  env.push()
  env.set('input', the_input)
  env.set('output', output)

  if output_type == 'po':
    # .ll to .po
    driver_tools.Run("${RUN_LLVM_AS}")
  else:
    # .s to .o
    driver_tools.Run("${RUN_LLVM_MC}")
  env.pop()
  # only reached in case of no errors
  return 0
Example #56
0
def BuildSharedLib():
  # Shared libs must be built PIC
  Env.set('GENERATE_PIC', '1')
  inputs = Env.get('INPUTS')
  if len(inputs) == 0:
    Log.Fatal('No input specified.')
  if not Env.getbool('GENERATE_PIC'):
    Log.Fatal('Shared libs must be build in pic mode. Use -fPIC')
  if Env.getone('SONAME_FLAG') == '':
    Log.Fatal('Shared libs must be given a soname.')

  pso_command = ProcessBuildCommand(BUILD_COMMAND_PSO_FROM_BC)
  DumpCommand("@bc->pso", pso_command)
  # suppress_arch is needed to prevent clang to inherit the
  # -arch argument
  driver_tools.RunDriver('clang', pso_command, suppress_arch=True)

  so_command = ProcessBuildCommand(BUILD_COMMAND_SO_FROM_PSO)
  DumpCommand("@pso->so", so_command)
  driver_tools.RunDriver('translate', so_command)
Example #57
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 #58
0
def RunLDSandboxed():
    if not env.getbool('USE_STDLIB'):
        Log.Fatal('-nostdlib is not supported by the sandboxed translator')
    CheckTranslatorPrerequisites()
    # The "main" input file is the application's combined object file.
    all_inputs = env.get('inputs')

    main_input = env.getone('LLC_TRANSLATED_FILE')
    if not main_input:
        Log.Fatal("Sandboxed LD requires one shm input file")

    outfile = env.getone('output')

    modules = int(env.getone('SPLIT_MODULE'))
    assert modules >= 1
    first_mainfile = all_inputs.index(main_input)
    first_extra = all_inputs.index(main_input) + modules
    # Have a list of just the split module files.
    llc_outputs = all_inputs[first_mainfile:first_extra]
    # Have a list of everything else.
    other_inputs = all_inputs[:first_mainfile] + all_inputs[first_extra:]

    native_libs_dirname = pathtools.tosys(GetNativeLibsDirname(other_inputs))
    command = [driver_tools.SelLdrCommand(), '-a']  # Allow file access
    driver_tools.AddListToEnv(command, 'NACL_IRT_PNACL_TRANSLATOR_LINK_INPUT',
                              llc_outputs)
    command.extend([
        '-E',
        'NACL_IRT_PNACL_TRANSLATOR_LINK_OUTPUT=%s ' % outfile, '-E',
        'NACL_IRT_OPEN_RESOURCE_BASE=%s' % native_libs_dirname, '-E',
        'NACL_IRT_OPEN_RESOURCE_REMAP=%s' %
        'libpnacl_irt_shim.a:libpnacl_irt_shim_dummy.a', '--', '${LD_SB}'
    ])
    Run(
        ' '.join(command),
        # stdout/stderr will be automatically dumped
        # upon failure
        redirect_stderr=subprocess.PIPE,
        redirect_stdout=subprocess.PIPE)