def main(argv): env.update(EXTRA_ENV) ParseArgs(argv, LDPatterns) # If the user passed -arch, then they want native output. arch_flag_given = GetArch() is not None # Both LD_FLAGS_NATIVE and TRANSLATE_FLAGS_USER affect # the translation process. If they are non-empty, # then --pnacl-allow-native must be given. allow_native = env.getbool('ALLOW_NATIVE') native_flags = env.get('LD_FLAGS_NATIVE') + env.get('TRANSLATE_FLAGS_USER') if len(native_flags) > 0: if not allow_native: flagstr = ' '.join(native_flags) Log.Fatal('"%s" affects translation. ' 'To allow, specify --pnacl-allow-native' % flagstr) if allow_native: if not arch_flag_given: Log.Fatal("--pnacl-allow-native given, but translation " "is not happening (missing -arch?)") if env.getbool('SHARED'): Log.Fatal('Native shared libraries are not supported with pnacl-ld.') if env.getbool('SHARED'): if env.getbool('RELOCATABLE'): Log.Fatal('-r/-relocatable and -shared may not be passed together.') env.set('RELOCATABLE', '1') # Overriding the lib target uses native-flavored bitcode libs rather than the # portable bitcode libs. It is currently only tested/supported for # building the IRT. if not IsPortable(): env.set('BASE_USR', "${BASE_USR_ARCH}") env.set('BASE_LIB', "${BASE_LIB_ARCH}") if env.getbool('RELOCATABLE'): env.set('STATIC', '0') inputs = env.get('INPUTS') output = env.getone('OUTPUT') if output == '': output = pathtools.normalize('a.out') if not arch_flag_given: # If -arch is not given, assume X86-32. # This is because gold requires an arch (even for bitcode linking). SetArch('X8632') assert(GetArch() is not None) inputs = FixPrivateLibs(inputs) # 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'), # Once all glibc bitcode link is purely # bitcode (e.g., even libc_nonshared.a) # we may be able to restrict this more. # This is also currently used by # pnacl_generate_pexe=0 with glibc, # for user libraries. ldtools.LibraryTypes.ANY) plls, inputs = FilterPlls(inputs) plls = [os.path.basename(pll) for pll in plls] if not env.getbool('SHARED') and plls != []: Log.Fatal('Passing a PLL to the linker requires the "-shared" option.') # Make sure the inputs have matching arch. CheckInputsArch(inputs) regular_inputs, native_objects = SplitLinkLine(inputs) if env.getbool('RELOCATABLE'): bitcode_type = 'po' native_type = 'o' else: bitcode_type = 'pexe' native_type = 'nexe' if native_objects and not allow_native: argstr = ' '.join(native_objects) Log.Fatal("Native objects '%s' detected in the link. " "To allow, specify --pnacl-allow-native" % argstr) tng = TempNameGen([], output) # Do the bitcode link. if HasBitcodeInputs(inputs): chain = DriverChain(inputs, output, tng) chain.add(LinkBC, 'pre_opt.' + bitcode_type) # Some ABI simplification passes assume the whole program is # available (e.g. -expand-varargs, -nacl-expand-ctors and # -nacl-expand-tls). While we could try running a subset of # simplification passes when linking native objects, we don't # do this because it complicates testing. For example, # it requires '-expand-constant-expr' to be able to handle # 'landingpad' instructions. # However, if we aren't using biased bitcode, then at least -expand-byval # must be run to work with the PPAPI shim calling convention, and # -expand-varargs is needed because after LLVM 3.5 the x86-32 backend does # not expand the llvm.va_arg intrinsic correctly. # (see https://code.google.com/p/nativeclient/issues/detail?id=3913#c24) abi_simplify = (env.getbool('STATIC') and len(native_objects) == 0 and not env.getbool('ALLOW_NEXE_BUILD_ID') and IsPortable()) still_need_expand_byval = IsPortable() and env.getbool('STATIC') still_need_expand_varargs = (still_need_expand_byval and len(native_objects) == 0) # A list of groups of args. Each group should contain a pass to run # along with relevant flags that go with that pass. opt_args = [] if env.getbool('SHARED'): pre_simplify_shared = [ # The following is a subset of "-pnacl-abi-simplify-preopt". We don't # want to run the full "-pnacl-abi-simplify-preopt" because it # internalizes symbols that we want to export via "-convert-to-pso". '-nacl-global-cleanup', '-expand-varargs', '-rewrite-pnacl-library-calls', '-rewrite-llvm-intrinsic-calls', '-convert-to-pso', ] # ConvertToPso takes a list of comma-separated PLL dependencies as an # argument. if plls != []: pre_simplify_shared += ['-convert-to-pso-deps=' + ','.join(plls)] opt_args.append(pre_simplify_shared) # Post-opt is required, since '-convert-to-pso' adds metadata which must # be simplified before finalization. Additionally, all functions must be # simplified in the post-opt passes. abi_simplify = True elif abi_simplify: pre_simplify = ['-pnacl-abi-simplify-preopt'] if env.getone('CXX_EH_MODE') == 'sjlj': pre_simplify += ['-enable-pnacl-sjlj-eh'] else: assert env.getone('CXX_EH_MODE') == 'none' opt_args.append(pre_simplify) else: # '-lowerinvoke' prevents use of C++ exception handling, which # is not yet supported in the PNaCl ABI. '-simplifycfg' removes # landingpad blocks made unreachable by '-lowerinvoke'. # # We run this in order to remove 'resume' instructions, # otherwise these are translated to calls to _Unwind_Resume(), # which will not be available at native link time. opt_args.append(['-lowerinvoke', '-simplifycfg']) if still_need_expand_varargs: opt_args.append(['-expand-varargs']) if env.getone('OPT_LEVEL') != '' and env.getone('OPT_LEVEL') != '0': opt_args.append(env.get('OPT_FLAGS')) if env.getone('STRIP_MODE') != 'none': opt_args.append(env.get('STRIP_FLAGS')) if abi_simplify: post_simplify = ['-pnacl-abi-simplify-postopt'] if not env.getbool('DISABLE_ABI_CHECK'): post_simplify += [ '-verify-pnaclabi-module', '-verify-pnaclabi-functions', # A flag for the above -verify-pnaclabi-* passes. '-pnaclabi-allow-debug-metadata'] opt_args.append(post_simplify) elif still_need_expand_byval: # We may still need -expand-byval to match the PPAPI shim # calling convention. opt_args.append(['-expand-byval']) if len(opt_args) != 0: if env.getbool('RUN_PASSES_SEPARATELY'): for i, group in enumerate(opt_args): chain.add(DoLLVMPasses(group), 'simplify_%d.%s' % (i, bitcode_type)) else: flattened_opt_args = [flag for group in opt_args for flag in group] chain.add(DoLLVMPasses(flattened_opt_args), 'simplify_and_opt.' + bitcode_type) else: chain = DriverChain('', output, tng) if env.getbool('FINALIZE'): chain.add(DoFinalize, 'finalize.' + bitcode_type) # If -arch is also specified, invoke pnacl-translate afterwards. if arch_flag_given: env.set('NATIVE_OBJECTS', *native_objects) chain.add(DoTranslate, native_type) chain.run() if bitcode_type == 'pexe' and not arch_flag_given: # Mark .pexe files as executable. # Some versions of 'configure' expect this. SetExecutableMode(output) return 0
def main(argv): env.update(EXTRA_ENV) ParseArgs(argv, LDPatterns) # If the user passed -arch, then they want native output. arch_flag_given = GetArch() is not None # Both LD_FLAGS_NATIVE and TRANSLATE_FLAGS_USER affect # the translation process. If they are non-empty, # then --pnacl-allow-native must be given. allow_native = env.getbool('ALLOW_NATIVE') native_flags = env.get('LD_FLAGS_NATIVE') + env.get('TRANSLATE_FLAGS_USER') if len(native_flags) > 0: if not allow_native: flagstr = ' '.join(native_flags) Log.Fatal('"%s" affects translation. ' 'To allow, specify --pnacl-allow-native' % flagstr) if env.getbool('ALLOW_NATIVE') and not arch_flag_given: Log.Fatal("--pnacl-allow-native given, but translation " "is not happening (missing -arch?)") # Overriding the lib target uses native-flavored bitcode libs rather than the # portable bitcode libs. It is currently only tested/supported for # building the IRT. if not IsPortable(): env.set('BASE_USR', "${BASE_USR_ARCH}") env.set('BASE_LIB', "${BASE_LIB_ARCH}") if env.getbool('RELOCATABLE'): env.set('STATIC', '0') inputs = env.get('INPUTS') output = env.getone('OUTPUT') if output == '': output = pathtools.normalize('a.out') if not arch_flag_given: # If -arch is not given, assume X86-32. # This is because gold requires an arch (even for bitcode linking). SetArch('X8632') assert(GetArch() is not None) inputs = FixPrivateLibs(inputs) # 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'), # Once all glibc bitcode link is purely # bitcode (e.g., even libc_nonshared.a) # we may be able to restrict this more. # This is also currently used by # pnacl_generate_pexe=0 with glibc, # for user libraries. ldtools.LibraryTypes.ANY) # Make sure the inputs have matching arch. CheckInputsArch(inputs) regular_inputs, native_objects = SplitLinkLine(inputs) if env.getbool('RELOCATABLE'): bitcode_type = 'po' native_type = 'o' else: bitcode_type = 'pexe' native_type = 'nexe' if native_objects and not allow_native: argstr = ' '.join(native_objects) Log.Fatal("Native objects '%s' detected in the link. " "To allow, specify --pnacl-allow-native" % argstr) tng = TempNameGen([], output) # Do the bitcode link. if HasBitcodeInputs(inputs): chain = DriverChain(inputs, output, tng) chain.add(LinkBC, 'pre_opt.' + bitcode_type) # Some ABI simplification passes assume the whole program is # available (e.g. -expand-varargs, -nacl-expand-ctors and # -nacl-expand-tls). While we could try running a subset of # simplification passes when linking native objects, we don't # do this because it complicates testing. For example, # it requires '-expand-constant-expr' to be able to handle # 'landingpad' instructions. # However, if we aren't using biased bitcode, then at least -expand-byval # must be run to work with the PPAPI shim calling convention, and # -expand-varargs is needed because after LLVM 3.5 the x86-32 backend does # not expand the llvm.va_arg intrinsic correctly. # (see https://code.google.com/p/nativeclient/issues/detail?id=3913#c24) abi_simplify = (env.getbool('STATIC') and len(native_objects) == 0 and not env.getbool('ALLOW_NEXE_BUILD_ID') and IsPortable()) still_need_expand_byval = IsPortable() and env.getbool('STATIC') still_need_expand_varargs = (still_need_expand_byval and len(native_objects) == 0) # A list of groups of args. Each group should contain a pass to run # along with relevant flags that go with that pass. opt_args = [] if abi_simplify: pre_simplify = ['-pnacl-abi-simplify-preopt'] if env.getone('CXX_EH_MODE') == 'sjlj': pre_simplify += ['-enable-pnacl-sjlj-eh'] else: assert env.getone('CXX_EH_MODE') == 'none' opt_args.append(pre_simplify) else: # '-lowerinvoke' prevents use of C++ exception handling, which # is not yet supported in the PNaCl ABI. '-simplifycfg' removes # landingpad blocks made unreachable by '-lowerinvoke'. # # We run this in order to remove 'resume' instructions, # otherwise these are translated to calls to _Unwind_Resume(), # which will not be available at native link time. opt_args.append(['-lowerinvoke', '-simplifycfg']) if still_need_expand_varargs: opt_args.append(['-expand-varargs']) if env.getone('OPT_LEVEL') != '' and env.getone('OPT_LEVEL') != '0': opt_args.append(env.get('OPT_FLAGS')) if env.getone('STRIP_MODE') != 'none': opt_args.append(env.get('STRIP_FLAGS')) if abi_simplify: post_simplify = ['-pnacl-abi-simplify-postopt'] if not env.getbool('DISABLE_ABI_CHECK'): post_simplify += [ '-verify-pnaclabi-module', '-verify-pnaclabi-functions', # A flag for the above -verify-pnaclabi-* passes. '-pnaclabi-allow-debug-metadata'] opt_args.append(post_simplify) elif still_need_expand_byval: # We may still need -expand-byval to match the PPAPI shim # calling convention. opt_args.append(['-expand-byval']) if len(opt_args) != 0: if env.getbool('RUN_PASSES_SEPARATELY'): for i, group in enumerate(opt_args): chain.add(DoLLVMPasses(group), 'simplify_%d.%s' % (i, bitcode_type)) else: flattened_opt_args = [flag for group in opt_args for flag in group] chain.add(DoLLVMPasses(flattened_opt_args), 'simplify_and_opt.' + bitcode_type) else: chain = DriverChain('', output, tng) if env.getbool('FINALIZE'): chain.add(DoFinalize, 'finalize.' + bitcode_type) # If -arch is also specified, invoke pnacl-translate afterwards. if arch_flag_given: env.set('NATIVE_OBJECTS', *native_objects) chain.add(DoTranslate, native_type) chain.run() if bitcode_type == 'pexe' and not arch_flag_given: # Mark .pexe files as executable. # Some versions of 'configure' expect this. SetExecutableMode(output) return 0
def main(argv): env.update(EXTRA_ENV) ParseArgs(argv, LDPatterns) # If the user passed -arch, then they want native output. arch_flag_given = GetArch() is not None # Both LD_FLAGS_NATIVE and TRANSLATE_FLAGS_USER affect # the translation process. If they are non-empty, # then --pnacl-allow-native must be given. allow_native = env.getbool('ALLOW_NATIVE') native_flags = env.get('LD_FLAGS_NATIVE') + env.get('TRANSLATE_FLAGS_USER') if len(native_flags) > 0: if not allow_native: flagstr = ' '.join(native_flags) Log.Fatal('"%s" affects translation. ' 'To allow, specify --pnacl-allow-native' % flagstr) if env.getbool('ALLOW_NATIVE') and not arch_flag_given: Log.Fatal("--pnacl-allow-native given, but translation " "is not happening (missing -arch?)") # Overriding the lib target uses native-flavored bitcode libs rather than the # portable bitcode libs. It is currently only tested/supported for # building the IRT. if not IsPortable(): env.set('BASE_USR', "${BASE_USR_ARCH}") env.set('BASE_LIB', "${BASE_LIB_ARCH}") if env.getbool('RELOCATABLE'): env.set('STATIC', '0') inputs = env.get('INPUTS') output = env.getone('OUTPUT') if output == '': output = pathtools.normalize('a.out') if not arch_flag_given: # If -arch is not given, assume X86-32. # This is because gold requires an arch (even for bitcode linking). SetArch('X8632') assert(GetArch() is not None) inputs = FixPrivateLibs(inputs) # 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'), # Once all glibc bitcode link is purely # bitcode (e.g., even libc_nonshared.a) # we may be able to restrict this more. # This is also currently used by # pnacl_generate_pexe=0 with glibc, # for user libraries. ldtools.LibraryTypes.ANY) # Make sure the inputs have matching arch. CheckInputsArch(inputs) regular_inputs, native_objects = SplitLinkLine(inputs) if env.getbool('RELOCATABLE'): bitcode_type = 'po' native_type = 'o' else: bitcode_type = 'pexe' native_type = 'nexe' if native_objects and not allow_native: argstr = ' '.join(native_objects) Log.Fatal("Native objects '%s' detected in the link. " "To allow, specify --pnacl-allow-native" % argstr) tng = TempNameGen([], output) # Do the bitcode link. if HasBitcodeInputs(inputs): chain = DriverChain(inputs, output, tng) chain.add(LinkBC, 'pre_opt.' + bitcode_type) # Some ABI simplification passes assume the whole program is # available (e.g. -expand-varargs, -nacl-expand-ctors and # -nacl-expand-tls). While we could try running a subset of # simplification passes when linking native objects, we don't # do this because it complicates testing. For example, # it requires '-expand-constant-expr' to be able to handle # 'landingpad' instructions. # However, if we aren't using biased bitcode, then at least -expand-byval # must be run to work with the PPAPI shim calling convention. # This assumes that PPAPI does not use var-args, so passes like # -expand-varargs and other calling-convention-changing passes are # not needed. abi_simplify = (env.getbool('STATIC') and len(native_objects) == 0 and env.getone('CXX_EH_MODE') != 'zerocost' and not env.getbool('ALLOW_NEXE_BUILD_ID') and IsPortable()) still_need_expand_byval = IsPortable() abi_simplify_opts = [] if env.getone('CXX_EH_MODE') == 'sjlj': abi_simplify_opts += ['-enable-pnacl-sjlj-eh'] preopt_passes = [] if abi_simplify: preopt_passes += ['-pnacl-abi-simplify-preopt'] + abi_simplify_opts elif env.getone('CXX_EH_MODE') != 'zerocost': # '-lowerinvoke' prevents use of C++ exception handling, which # is not yet supported in the PNaCl ABI. '-simplifycfg' removes # landingpad blocks made unreachable by '-lowerinvoke'. # # We run this in order to remove 'resume' instructions, # otherwise these are translated to calls to _Unwind_Resume(), # which will not be available at native link time. preopt_passes += ['-lowerinvoke', '-simplifycfg'] if len(preopt_passes) != 0: chain.add(DoLLVMPasses(preopt_passes), 'simplify_preopt.' + bitcode_type) if env.getone('OPT_LEVEL') != '' and env.getone('OPT_LEVEL') != '0': chain.add(DoLTO, 'opt.' + bitcode_type) elif env.getone('STRIP_MODE') != 'none': chain.add(DoStrip, 'stripped.' + bitcode_type) postopt_passes = [] if abi_simplify: postopt_passes = ['-pnacl-abi-simplify-postopt'] + abi_simplify_opts if not env.getbool('DISABLE_ABI_CHECK'): postopt_passes += [ '-verify-pnaclabi-module', '-verify-pnaclabi-functions', # A flag for the above -verify-pnaclabi-* passes. '-pnaclabi-allow-debug-metadata'] if env.getbool('ALLOW_DEV_INTRINSICS'): # A flag for the above -verify-pnaclabi-* passes. postopt_passes += ['-pnaclabi-allow-dev-intrinsics'] elif still_need_expand_byval: # We may still need -expand-byval to match the PPAPI shim # calling convention. postopt_passes = ['-expand-byval'] if len(postopt_passes) != 0: chain.add(DoLLVMPasses(postopt_passes), 'simplify_postopt.' + bitcode_type) else: chain = DriverChain('', output, tng) # If -arch is also specified, invoke pnacl-translate afterwards. if arch_flag_given: env.set('NATIVE_OBJECTS', *native_objects) chain.add(DoTranslate, native_type) chain.run() if bitcode_type == 'pexe' and not arch_flag_given: # Mark .pexe files as executable. # Some versions of 'configure' expect this. SetExecutableMode(output) return 0