Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client / pnacl / driver / pnacl-ld.py
1 #!/usr/bin/python
2 # Copyright (c) 2012 The Native Client 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 from driver_tools import ArchMerge, DriverChain, GetArch, \
7     ParseArgs, ParseTriple, RunDriver, RunWithEnv, SetArch, \
8     SetExecutableMode, TempNameGen, UnrecognizedOption
9 from driver_env import env
10 from driver_log import Log
11 import filetype
12 import ldtools
13 import pathtools
14
15 EXTRA_ENV = {
16   'ALLOW_NATIVE': '0', # Allow LD args which will change the behavior
17                        # of native linking. This must be accompanied by
18                        # -arch to produce a .nexe.
19   'USE_IRT': '1',  # Use stable IRT interfaces.
20
21   'INPUTS'   : '',
22   'OUTPUT'   : '',
23
24   'STATIC'   : '0',
25   'PIC'      : '0',
26   'USE_STDLIB': '1',
27   'RELOCATABLE': '0',
28   'SONAME'   : '',
29
30   'STRIP_MODE' : 'none',
31
32   'STRIP_FLAGS'      : '${STRIP_FLAGS_%STRIP_MODE%}',
33   'STRIP_FLAGS_all'  : '-s',
34   'STRIP_FLAGS_debug': '-S',
35
36   'OPT_INLINE_THRESHOLD': '100',
37   'OPT_LEVEL': '',  # Default opt is 0, but we need to know if it's explicitly
38                     # requested or not, since we don't want to propagate
39                     # the value to TRANSLATE_FLAGS if it wasn't explicitly set.
40   'OPT_LTO_FLAGS': '-std-link-opts -disable-internalize',
41   'OPT_FLAGS': '${#OPT_LEVEL && !OPT_LEVEL == 0 ? ${OPT_LTO_FLAGS}} ' +
42                '-inline-threshold=${OPT_INLINE_THRESHOLD} ',
43
44   'TRANSLATE_FLAGS': '${PIC ? -fPIC} ${!USE_STDLIB ? -nostdlib} ' +
45                      '${#SONAME ? -Wl,--soname=${SONAME}} ' +
46                      '${#OPT_LEVEL ? -O${OPT_LEVEL}} ' +
47                      '--allow-llvm-bitcode-input ' +
48                      '${CXX_EH_MODE==zerocost ? --pnacl-allow-zerocost-eh} ' +
49                      '${TRANSLATE_FLAGS_USER}',
50
51   # Extra pnacl-translate flags specified by the user using -Wt
52   'TRANSLATE_FLAGS_USER': '',
53
54   'GOLD_PLUGIN_ARGS': '-plugin=${GOLD_PLUGIN_SO} ' +
55                       '-plugin-opt=emit-llvm',
56
57   'LD_FLAGS'       : '-nostdlib ${@AddPrefix:-L:SEARCH_DIRS} ' +
58                      '${STATIC ? -static} ' +
59                      '${RELOCATABLE ? -relocatable} ' +
60                      '${#SONAME ? --soname=${SONAME}}',
61
62   # Flags for native linking.
63   # Only allowed if ALLOW_NATIVE is true.
64   'LD_FLAGS_NATIVE': '',
65
66   'SEARCH_DIRS'        : '${SEARCH_DIRS_USER} ${SEARCH_DIRS_BUILTIN}',
67   'SEARCH_DIRS_USER'   : '',
68
69   # Standard Library Directories
70   'SEARCH_DIRS_BUILTIN': '${USE_STDLIB ? ' +
71                          '  ${BASE_USR}/local/lib/ ' +
72                          '  ${BASE_USR}/lib/ ' +
73                          '  ${BASE_SDK}/lib/ ' +
74                          '  ${BASE_LIB}/ ' +
75                          '}',
76
77   'BCLD_OFORMAT'               : '${BCLD_OFORMAT_%ARCH%}',
78   'BCLD_OFORMAT_ARM'           : 'elf32-littlearm',
79   'BCLD_OFORMAT_X8632'         : 'elf32-i386-nacl',
80   'BCLD_OFORMAT_X8664'         : 'elf64-x86-64-nacl',
81   'BCLD_OFORMAT_MIPS32'        : 'elf32-tradlittlemips-nacl',
82   'BCLD_OFORMAT_ARM_NONSFI'    : 'elf32-littlearm',
83   'BCLD_OFORMAT_X8632_NONSFI'  : 'elf32-i386-nacl',
84
85   'BCLD_ALLOW_UNRESOLVED'  :
86     # The following functions are implemented in the native support library.
87     # Before a .pexe is produced, they get rewritten to intrinsic calls.
88     # However, this rewriting happens after bitcode linking - so gold has
89     # to be told that these are allowed to remain unresolved.
90     '--allow-unresolved=memcpy '
91     '--allow-unresolved=memset '
92     '--allow-unresolved=memmove '
93     '--allow-unresolved=setjmp '
94     '--allow-unresolved=longjmp '
95     # These TLS layout functions are either defined by the ExpandTls
96     # pass or (for non-ABI-stable code only) by PNaCl's native support
97     # code.
98     '--allow-unresolved=__nacl_tp_tls_offset '
99     '--allow-unresolved=__nacl_tp_tdb_offset '
100     # __nacl_get_arch() is for non-ABI-stable code only.
101     '--allow-unresolved=__nacl_get_arch '
102     '${CXX_EH_MODE==sjlj ? '
103       # These symbols are defined by libsupc++ and the PNaClSjLjEH
104       # pass generates references to them.
105       '--undefined=__pnacl_eh_stack '
106       '--undefined=__pnacl_eh_resume '
107       # These symbols are defined by the PNaClSjLjEH pass and
108       # libsupc++ refers to them.
109       '--allow-unresolved=__pnacl_eh_type_table '
110       '--allow-unresolved=__pnacl_eh_action_table '
111       '--allow-unresolved=__pnacl_eh_filter_table} '
112     # For exception-handling enabled tests.
113     '${CXX_EH_MODE==zerocost ? '
114       '--allow-unresolved=_Unwind_Backtrace '
115       '--allow-unresolved=_Unwind_DeleteException '
116       '--allow-unresolved=_Unwind_GetCFA '
117       '--allow-unresolved=_Unwind_GetDataRelBase '
118       '--allow-unresolved=_Unwind_GetGR '
119       '--allow-unresolved=_Unwind_GetIP '
120       '--allow-unresolved=_Unwind_GetIPInfo '
121       '--allow-unresolved=_Unwind_GetLanguageSpecificData '
122       '--allow-unresolved=_Unwind_GetRegionStart '
123       '--allow-unresolved=_Unwind_GetTextRelBase '
124       '--allow-unresolved=_Unwind_PNaClSetResult0 '
125       '--allow-unresolved=_Unwind_PNaClSetResult1 '
126       '--allow-unresolved=_Unwind_RaiseException '
127       '--allow-unresolved=_Unwind_Resume '
128       '--allow-unresolved=_Unwind_Resume_or_Rethrow '
129       '--allow-unresolved=_Unwind_SetGR '
130       '--allow-unresolved=_Unwind_SetIP}',
131
132   'BCLD_FLAGS':
133     '--oformat ${BCLD_OFORMAT} ' +
134     '${!RELOCATABLE ? --undef-sym-check ${BCLD_ALLOW_UNRESOLVED}} ' +
135     '${GOLD_PLUGIN_ARGS} ${LD_FLAGS}',
136   'RUN_BCLD': ('${LD} ${BCLD_FLAGS} ${inputs} -o ${output}'),
137
138   'CXX_EH_MODE': 'none',
139   'ALLOW_NEXE_BUILD_ID': '0',
140   'DISABLE_ABI_CHECK': '0',
141   'LLVM_PASSES_TO_DISABLE': '',
142   'RUN_PASSES_SEPARATELY': '0',
143 }
144
145 def AddToBCLinkFlags(*args):
146   env.append('LD_FLAGS', *args)
147
148 def AddToNativeFlags(*args):
149   env.append('LD_FLAGS_NATIVE', *args)
150
151 def AddToBothFlags(*args):
152   env.append('LD_FLAGS', *args)
153   env.append('LD_FLAGS_NATIVE', *args)
154
155 def SetLibTarget(*args):
156   arch = ParseTriple(args[0])
157   if arch != 'le32':
158     env.set('BCLIB_ARCH', arch)
159
160 def IsPortable():
161   return env.getone('BCLIB_ARCH') == ''
162
163 LDPatterns = [
164   ( '--pnacl-allow-native', "env.set('ALLOW_NATIVE', '1')"),
165   ( '--noirt',              "env.set('USE_IRT', '0')"),
166   ( '--pnacl-exceptions=(none|sjlj|zerocost)', "env.set('CXX_EH_MODE', $0)"),
167   # TODO(mseaborn): Remove "--pnacl-allow-exceptions", which is
168   # superseded by "--pnacl-exceptions".
169   ( '--pnacl-allow-exceptions', "env.set('CXX_EH_MODE', 'zerocost')"),
170   ( '(--pnacl-allow-nexe-build-id)', "env.set('ALLOW_NEXE_BUILD_ID', '1')"),
171   ( '--pnacl-disable-abi-check', "env.set('DISABLE_ABI_CHECK', '1')"),
172   # "--pnacl-disable-pass" allows an ABI simplification pass to be
173   # disabled if it is causing problems.  These passes are generally
174   # required for ABI-stable pexes but can be omitted when the PNaCl
175   # toolchain is used for producing native nexes.
176   ( '--pnacl-disable-pass=(.+)', "env.append('LLVM_PASSES_TO_DISABLE', $0)"),
177   ( '--pnacl-run-passes-separately', "env.set('RUN_PASSES_SEPARATELY', '1')"),
178   ( ('-target', '(.+)'), SetLibTarget),
179   ( ('--target=(.+)'), SetLibTarget),
180
181   ( '-o(.+)',          "env.set('OUTPUT', pathtools.normalize($0))"),
182   ( ('-o', '(.+)'),    "env.set('OUTPUT', pathtools.normalize($0))"),
183   ( ('--output', '(.+)'), "env.set('OUTPUT', pathtools.normalize($0))"),
184
185   ( '-static',         "env.set('STATIC', '1')"),
186   ( '-nostdlib',       "env.set('USE_STDLIB', '0')"),
187
188   ( '-r',              "env.set('RELOCATABLE', '1')"),
189   ( '-relocatable',    "env.set('RELOCATABLE', '1')"),
190   ( '-i',              "env.set('RELOCATABLE', '1')"),
191
192   ( ('-L', '(.+)'),
193     "env.append('SEARCH_DIRS_USER', pathtools.normalize($0))\n"),
194   ( '-L(.+)',
195     "env.append('SEARCH_DIRS_USER', pathtools.normalize($0))\n"),
196   ( ('--library-path', '(.+)'),
197     "env.append('SEARCH_DIRS_USER', pathtools.normalize($0))\n"),
198
199   # -rpath and -rpath-link are only relevant to dynamic linking.
200   # Ignore them for compatibility with build scripts that expect to be
201   # able to pass them.
202   ( ('(-rpath)','(.*)'), ""),
203   ( ('(-rpath)=(.*)'), ""),
204   ( ('(-rpath-link)','(.*)'), ""),
205   ( ('(-rpath-link)=(.*)'), ""),
206
207   # This overrides the builtin linker script.
208   ( ('(-T)', '(.*)'),    AddToNativeFlags),
209
210   # TODO(pdox): Allow setting an alternative _start symbol in bitcode
211   ( ('(-e)','(.*)'),     AddToBothFlags),
212
213   # TODO(pdox): Support GNU versioning.
214   ( '(--version-script=.*)', ""),
215
216   # Flags to pass to the native linker.
217   ( '-Wn,(.*)', "env.append('LD_FLAGS_NATIVE', *($0.split(',')))"),
218   ( ('(-Ttext-segment=.*)'), AddToNativeFlags),
219   ( ('(-Trodata-segment=.*)'), AddToNativeFlags),
220   ( ('(--section-start)', '(.+)'), AddToNativeFlags),
221   ( ('(--build-id)'), AddToNativeFlags),
222
223   # Flags to pass to translate
224   ( '-Wt,(.*)', "env.append('TRANSLATE_FLAGS_USER', *($0.split(',')))"),
225
226   # NOTE: -export-dynamic doesn't actually do anything to the bitcode link
227   # right now.  This is just in case we do want to record that in metadata
228   # eventually, and have that influence the native linker flags.
229   ( '(-export-dynamic)', AddToBCLinkFlags),
230
231   ( '-?-soname=(.*)',             "env.set('SONAME', $0)"),
232   ( ('-?-soname', '(.*)'),        "env.set('SONAME', $0)"),
233
234   ( '(-M)',                       AddToBCLinkFlags),
235   ( '(--print-map)',              AddToBCLinkFlags),
236   ( '(-t)',                       AddToBCLinkFlags),
237   ( '(--trace)',                  AddToBCLinkFlags),
238   ( ('(-y)','(.*)'),              AddToBCLinkFlags),
239   ( ('(-defsym)','(.*)'),         AddToBCLinkFlags),
240
241   ( '-melf_nacl',            "env.set('ARCH', 'X8632')"),
242   ( ('-m','elf_nacl'),       "env.set('ARCH', 'X8632')"),
243   ( '-melf64_nacl',          "env.set('ARCH', 'X8664')"),
244   ( ('-m','elf64_nacl'),     "env.set('ARCH', 'X8664')"),
245   ( '-marmelf_nacl',         "env.set('ARCH', 'ARM')"),
246   ( ('-m','armelf_nacl'),    "env.set('ARCH', 'ARM')"),
247   ( '-mmipselelf_nacl',      "env.set('ARCH', 'MIPS32')"),
248   ( ('-m','mipselelf_nacl'), "env.set('ARCH', 'MIPS32')"),
249
250   ( ('(-?-wrap)', '(.+)'), AddToBCLinkFlags),
251   ( ('(-?-wrap=.+)'),      AddToBCLinkFlags),
252
253   # NOTE: For scons tests, the code generation fPIC flag is used with pnacl-ld.
254   ( '-fPIC',               "env.set('PIC', '1')"),
255
256   # This controls LTO optimization.
257   # opt does not support -Os but internally it is identical to -O2
258   # opt also does not support -O4 but -O4 is how you ask clang for LTO, so we
259   # can support it as well
260   ( '-Os',                 "env.set('OPT_LEVEL', '2')"),
261   ( '-O([0-3])',           "env.set('OPT_LEVEL', $0)"),
262   ( '-O([0-9]+)',          "env.set('OPT_LEVEL', '3')"),
263
264   ( '(-translate-fast)',   "env.append('TRANSLATE_FLAGS', $0)"),
265
266   ( '-s',                  "env.set('STRIP_MODE', 'all')"),
267   ( '--strip-all',         "env.set('STRIP_MODE', 'all')"),
268   ( '-S',                  "env.set('STRIP_MODE', 'debug')"),
269   ( '--strip-debug',       "env.set('STRIP_MODE', 'debug')"),
270
271   ( '-g', ""),
272
273   # Inputs and options that need to be kept in order
274   ( '(-l.*)',              "env.append('INPUTS', $0)"),
275   ( ('(-l)','(.*)'),       "env.append('INPUTS', $0+$1)"),
276   ( ('--library', '(.*)'), "env.append('INPUTS', '-l'+$0)"),
277
278   ( '(--no-as-needed)',    "env.append('INPUTS', $0)"),
279   ( '(--as-needed)',       "env.append('INPUTS', $0)"),
280   ( '(--start-group)',     "env.append('INPUTS', $0)"),
281   ( '(--end-group)',       "env.append('INPUTS', $0)"),
282   ( '(-Bstatic)',          "env.append('INPUTS', $0)"),
283   ( '(-Bdynamic)',          "env.append('INPUTS', $0)"),
284   ( '(--(no-)?whole-archive)', "env.append('INPUTS', $0)"),
285   ( '(--undefined=.*)',    "env.append('INPUTS', $0)"),
286   ( ('(-u)','(.*)'),       "env.append('INPUTS', $0+$1)"),
287   ( '(-u.*)',              "env.append('INPUTS', $0)"),
288   ( '(-.*)',               UnrecognizedOption),
289   ( '(.*)',                "env.append('INPUTS', pathtools.normalize($0))"),
290 ]
291
292 def main(argv):
293   env.update(EXTRA_ENV)
294   ParseArgs(argv, LDPatterns)
295   # If the user passed -arch, then they want native output.
296   arch_flag_given = GetArch() is not None
297
298   # Both LD_FLAGS_NATIVE and TRANSLATE_FLAGS_USER affect
299   # the translation process. If they are non-empty,
300   # then --pnacl-allow-native must be given.
301   allow_native = env.getbool('ALLOW_NATIVE')
302   native_flags = env.get('LD_FLAGS_NATIVE') + env.get('TRANSLATE_FLAGS_USER')
303   if len(native_flags) > 0:
304     if not allow_native:
305       flagstr = ' '.join(native_flags)
306       Log.Fatal('"%s" affects translation. '
307                 'To allow, specify --pnacl-allow-native' % flagstr)
308
309   if env.getbool('ALLOW_NATIVE') and not arch_flag_given:
310       Log.Fatal("--pnacl-allow-native given, but translation "
311                 "is not happening (missing -arch?)")
312
313   # Overriding the lib target uses native-flavored bitcode libs rather than the
314   # portable bitcode libs. It is currently only tested/supported for
315   # building the IRT.
316   if not IsPortable():
317     env.set('BASE_USR', "${BASE_USR_ARCH}")
318     env.set('BASE_LIB', "${BASE_LIB_ARCH}")
319
320   if env.getbool('RELOCATABLE'):
321     env.set('STATIC', '0')
322
323   inputs = env.get('INPUTS')
324   output = env.getone('OUTPUT')
325
326   if output == '':
327     output = pathtools.normalize('a.out')
328
329   if not arch_flag_given:
330     # If -arch is not given, assume X86-32.
331     # This is because gold requires an arch (even for bitcode linking).
332     SetArch('X8632')
333   assert(GetArch() is not None)
334
335   inputs = FixPrivateLibs(inputs)
336
337   # Expand all parameters
338   # This resolves -lfoo into actual filenames,
339   # and expands linker scripts into command-line arguments.
340   inputs = ldtools.ExpandInputs(inputs,
341                                 env.get('SEARCH_DIRS'),
342                                 env.getbool('STATIC'),
343                                 # Once all glibc bitcode link is purely
344                                 # bitcode (e.g., even libc_nonshared.a)
345                                 # we may be able to restrict this more.
346                                 # This is also currently used by
347                                 # pnacl_generate_pexe=0 with glibc,
348                                 # for user libraries.
349                                 ldtools.LibraryTypes.ANY)
350
351   # Make sure the inputs have matching arch.
352   CheckInputsArch(inputs)
353
354   regular_inputs, native_objects = SplitLinkLine(inputs)
355
356   if env.getbool('RELOCATABLE'):
357     bitcode_type = 'po'
358     native_type = 'o'
359   else:
360     bitcode_type = 'pexe'
361     native_type = 'nexe'
362
363   if native_objects and not allow_native:
364     argstr = ' '.join(native_objects)
365     Log.Fatal("Native objects '%s' detected in the link. "
366               "To allow, specify --pnacl-allow-native" % argstr)
367
368   tng = TempNameGen([], output)
369
370   # Do the bitcode link.
371   if HasBitcodeInputs(inputs):
372     chain = DriverChain(inputs, output, tng)
373     chain.add(LinkBC, 'pre_opt.' + bitcode_type)
374
375     # Some ABI simplification passes assume the whole program is
376     # available (e.g. -expand-varargs, -nacl-expand-ctors and
377     # -nacl-expand-tls).  While we could try running a subset of
378     # simplification passes when linking native objects, we don't
379     # do this because it complicates testing.  For example,
380     # it requires '-expand-constant-expr' to be able to handle
381     # 'landingpad' instructions.
382     # However, if we aren't using biased bitcode, then at least -expand-byval
383     # must be run to work with the PPAPI shim calling convention.
384     # This assumes that PPAPI does not use var-args, so passes like
385     # -expand-varargs and other calling-convention-changing passes are
386     # not needed.
387     abi_simplify = (env.getbool('STATIC') and
388                     len(native_objects) == 0 and
389                     env.getone('CXX_EH_MODE') != 'zerocost' and
390                     not env.getbool('ALLOW_NEXE_BUILD_ID') and
391                     IsPortable())
392     still_need_expand_byval = IsPortable()
393
394     # A list of groups of args. Each group should contain a pass to run
395     # along with relevant flags that go with that pass.
396     opt_args = []
397     if abi_simplify:
398       pre_simplify = ['-pnacl-abi-simplify-preopt']
399       if env.getone('CXX_EH_MODE') == 'sjlj':
400         pre_simplify += ['-enable-pnacl-sjlj-eh']
401       else:
402         assert env.getone('CXX_EH_MODE') == 'none'
403       opt_args.append(pre_simplify)
404     elif env.getone('CXX_EH_MODE') != 'zerocost':
405       # '-lowerinvoke' prevents use of C++ exception handling, which
406       # is not yet supported in the PNaCl ABI.  '-simplifycfg' removes
407       # landingpad blocks made unreachable by '-lowerinvoke'.
408       #
409       # We run this in order to remove 'resume' instructions,
410       # otherwise these are translated to calls to _Unwind_Resume(),
411       # which will not be available at native link time.
412       opt_args.append(['-lowerinvoke', '-simplifycfg'])
413
414     if env.getone('OPT_LEVEL') != '' and env.getone('OPT_LEVEL') != '0':
415       opt_args.append(env.get('OPT_FLAGS'))
416     if env.getone('STRIP_MODE') != 'none':
417       opt_args.append(env.get('STRIP_FLAGS'))
418
419     if abi_simplify:
420       post_simplify = ['-pnacl-abi-simplify-postopt']
421       if not env.getbool('DISABLE_ABI_CHECK'):
422         post_simplify += [
423             '-verify-pnaclabi-module',
424             '-verify-pnaclabi-functions',
425             # A flag for the above -verify-pnaclabi-* passes.
426             '-pnaclabi-allow-debug-metadata']
427       opt_args.append(post_simplify)
428     elif still_need_expand_byval:
429       # We may still need -expand-byval to match the PPAPI shim
430       # calling convention.
431       opt_args.append(['-expand-byval'])
432     if len(opt_args) != 0:
433       if env.getbool('RUN_PASSES_SEPARATELY'):
434         for i, group in enumerate(opt_args):
435           chain.add(DoLLVMPasses(group),
436                     'simplify_%d.%s' % (i, bitcode_type))
437       else:
438         flattened_opt_args = [flag for group in opt_args for flag in group]
439         chain.add(DoLLVMPasses(flattened_opt_args),
440                   'simplify_and_opt.' + bitcode_type)
441   else:
442     chain = DriverChain('', output, tng)
443
444   # If -arch is also specified, invoke pnacl-translate afterwards.
445   if arch_flag_given:
446     env.set('NATIVE_OBJECTS', *native_objects)
447     chain.add(DoTranslate, native_type)
448
449   chain.run()
450
451   if bitcode_type == 'pexe' and not arch_flag_given:
452     # Mark .pexe files as executable.
453     # Some versions of 'configure' expect this.
454     SetExecutableMode(output)
455   return 0
456
457 def FixPrivateLibs(user_libs):
458   """If not using the IRT or if private libraries are used:
459     - Place private libraries that can coexist before their public
460       equivalent (keep both);
461     - Replace public libraries that can't coexist with their private
462       equivalent.
463
464   This occurs before path resolution (important because public/private
465   libraries aren't always colocated) and assumes that -l:libfoo.a syntax
466   isn't used by the driver for relevant libraries.
467   """
468   special_libs = {
469       # Public library name: (private library name, can coexist?)
470       '-lnacl': ('-lnacl_sys_private', True),
471       '-lpthread': ('-lpthread_private', False),
472       }
473   private_libs = [v[0] for v in special_libs.values()]
474   public_libs = special_libs.keys()
475   private_lib_for = lambda user_lib: special_libs[user_lib][0]
476   can_coexist = lambda user_lib: special_libs[user_lib][1]
477
478   no_irt = not env.getbool('USE_IRT')
479   uses_private_libs = set(user_libs) & set(private_libs)
480
481   if not (no_irt or uses_private_libs):
482     return user_libs
483
484   result_libs = []
485   for user_lib in user_libs:
486     if user_lib in public_libs:
487       result_libs.append(private_lib_for(user_lib))
488       if can_coexist(user_lib):
489         result_libs.append(user_lib)
490     else:
491       result_libs.append(user_lib)
492   return result_libs
493
494 def SplitLinkLine(inputs):
495   """ Split the input list into bitcode and native objects (.o, .a)
496   """
497   normal = []
498   native = []
499   # Group flags need special handling because they need to go into the right
500   # list based on the type of the inputs in the group. If the group has both
501   # native and bitcode files (which is unfortunately the case for
502   # irt_browser_lib) then the group flags need to go in both lists.
503   if '--start-group' in inputs:
504     start_group = inputs.index('--start-group')
505     # Start with the inputs before the first group
506     normal, native = SplitLinkLine(inputs[:start_group])
507     try:
508       end_group = inputs.index('--end-group')
509     except ValueError:
510       Log.Fatal("Found --start-group without matching --end-group")
511     # Add the contents of the group together with the --{start,end}-group flags
512     norm_group, native_group = SplitLinkLine(inputs[start_group + 1:end_group])
513     if len(norm_group) > 0:
514       normal.extend(['--start-group'] + norm_group + ['--end-group'])
515     if len(native_group) > 0:
516       native.extend(['--start-group'] + native_group + ['--end-group'])
517     # Add the inputs after the first group
518     norm_last, native_last = SplitLinkLine(inputs[end_group + 1:])
519     return normal + norm_last, native + native_last
520
521   # If no groups, split the inputs based on their type.
522   for f in inputs:
523     if ldtools.IsFlag(f):
524       normal.append(f)
525     elif filetype.IsNativeArchive(f) or filetype.IsNativeObject(f):
526       native.append(f)
527     else:
528       normal.append(f)
529   return (normal, native)
530
531 def HasBitcodeInputs(inputs):
532   for f in inputs:
533     if ldtools.IsFlag(f):
534       continue
535     elif filetype.IsLLVMBitcode(f) or filetype.IsBitcodeArchive(f):
536       return True
537   return False
538
539 def CheckInputsArch(inputs):
540   count = 0
541   for f in inputs:
542     if ldtools.IsFlag(f):
543       continue
544     elif filetype.IsLLVMBitcode(f) or filetype.IsBitcodeArchive(f):
545       pass
546     elif filetype.IsNative(f):
547       ArchMerge(f, True)
548     else:
549       Log.Fatal("%s: Unexpected type of file for linking (%s)",
550                 pathtools.touser(f), filetype.FileType(f))
551     count += 1
552
553   if count == 0:
554     Log.Fatal("no input files")
555
556 def DoLLVMPasses(pass_list):
557   def Func(infile, outfile):
558     filtered_list = [pass_option for pass_option in pass_list
559                      if pass_option not in env.get('LLVM_PASSES_TO_DISABLE')]
560     RunDriver('pnacl-opt', filtered_list + [infile, '-o', outfile])
561   return Func
562
563 def DoTranslate(infile, outfile):
564   args = env.get('TRANSLATE_FLAGS')
565   args += ['-Wl,'+s for s in env.get('LD_FLAGS_NATIVE')]
566   if infile:
567     args += [infile]
568   args += ['-Wl,'+s if ldtools.IsFlag(s) else s
569            for s in env.get('NATIVE_OBJECTS')]
570   args += ['-o', outfile]
571   RunDriver('pnacl-translate', args)
572
573 def LinkBC(inputs, output):
574   '''Input: a bunch of bc/o/lib input files
575      Output: a combined & optimized bitcode file
576   '''
577   # Produce combined bitcode file
578   RunWithEnv('${RUN_BCLD}',
579              inputs=inputs,
580              output=output)
581
582 def get_help(unused_argv):
583   return """Usage: pnacl-ld [options] <input files> -o <output>
584
585 Bitcode linker for PNaCl.  Similar to the binutils "ld" tool,
586 but links bitcode instead of native code.  Supports many of the
587 "ld" flags. Below are a subset of them.
588
589 OPTIONS:
590
591   -o <file>                   Set output file name
592   -l LIBNAME                  Search for library LIBNAME
593   -L DIRECTORY, --library-path DIRECTORY
594                               Add DIRECTORY to library search path
595
596   -r, -relocatable            Generate relocatable output
597
598   -O<opt-level>               Optimize output file
599   -M, --print-map             Print map file on standard output
600   --whole-archive             Include all objects from following archives
601   --no-whole-archive          Turn off --whole-archive
602   -s, --strip-all             Strip all symbols
603   -S, --strip-debug           Strip debugging symbols
604   -u SYM, --undefined=SYM     Start with undefined reference to SYM
605
606   -help | -h                  Output this help.
607 """