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.
8 from driver_env import env
9 from driver_log import Log, DriverOpen, DriverClose
19 ( '--file-type', "env.set('FILE_TYPE', '1')"),
20 ( ('-o','(.*)'), "env.set('OUTPUT', pathtools.normalize($0))"),
21 ( '(-.*)', "env.append('FLAGS', $0)"),
22 ( '(.*)', "env.append('INPUTS', pathtools.normalize($0))"),
27 driver_tools.ParseArgs(argv, DISPatterns)
29 inputs = env.get('INPUTS')
30 output = env.getone('OUTPUT')
32 for path in inputs + [output]:
33 driver_tools.CheckPathLength(path)
36 Log.Fatal("No input files given")
38 if len(inputs) > 1 and output != '':
39 Log.Fatal("Cannot have -o with multiple inputs")
43 env.set('input', infile)
44 env.set('output', output)
46 # When we output to stdout, set redirect_stdout and set log_stdout
47 # to False to bypass the driver's line-by-line handling of stdout
48 # which is extremely slow when you have a lot of output
50 if (filetype.IsLLVMBitcode(infile) or
51 filetype.IsPNaClBitcode(infile)):
52 bitcodetype = 'PNaCl' if filetype.IsPNaClBitcode(infile) else 'LLVM'
53 format = bitcodetype.lower()
55 if env.has('FILE_TYPE'):
56 sys.stdout.write('%s: %s bitcode\n' % (infile, bitcodetype))
58 env.append('FLAGS', '-bitcode-format=' + format)
60 # LLVM by default outputs to a file if -o is missing
61 # Let's instead output to stdout
62 env.set('output', '-')
63 env.append('FLAGS', '-f')
64 driver_tools.Run('${LLVM_DIS} ${FLAGS} ${input} -o ${output}')
65 elif filetype.IsELF(infile):
66 if env.has('FILE_TYPE'):
67 sys.stdout.write('%s: ELF\n' % infile)
69 flags = env.get('FLAGS')
71 env.append('FLAGS', '-d')
74 driver_tools.Run('"${OBJDUMP}" ${FLAGS} ${input}')
76 # objdump always outputs to stdout, and doesn't recognize -o
77 # Let's add this feature to be consistent.
78 fp = DriverOpen(output, 'w')
79 driver_tools.Run('${OBJDUMP} ${FLAGS} ${input}', redirect_stdout=fp)
82 Log.Fatal('Unknown file type')
84 # only reached in case of no errors
87 def get_help(unused_argv):
88 return """Usage: pnacl-dis [options] <input binary file> -o <output.txt>
90 Disassembler for PNaCl. Converts either bitcode to text or
91 native code to assembly. For native code, this just a wrapper around objdump
92 so this accepts the usual objdump flags.
95 -o <file> Output to file
96 -help | -h Output this help.
97 --file-type Detect and print the file type for each of the input files.