Index: pnacl/driver/pnacl-meta.py |
diff --git a/pnacl/driver/pnacl-meta.py b/pnacl/driver/pnacl-meta.py |
index 19487ffb6877a9ddee3ea63155a68bd320414518..059b71a747d66e8e5c51d866c4c84fcc1d2adb63 100755 |
--- a/pnacl/driver/pnacl-meta.py |
+++ b/pnacl/driver/pnacl-meta.py |
@@ -1,36 +1,113 @@ |
#!/usr/bin/python |
-# Copyright (c) 2011 The Native Client Authors. All rights reserved. |
+# Copyright (c) 2012 The Native Client Authors. All rights reserved. |
# Use of this source code is governed by a BSD-style license that can be |
# found in the LICENSE file. |
# |
# IMPORTANT NOTE: If you make local mods to this file, you must run: |
-# % tools/llvm/utman.sh driver |
+# % pnacl/build.sh driver |
# in order for them to take effect in the scons build. This command |
# updates the copy in the toolchain/ tree. |
# |
-# Show the PNaCl metadata of a bitcode file |
+# Show the PNaCl metadata of a bitcode file (which mirror ELF information). |
from driver_tools import * |
from driver_env import env |
from driver_log import Log, DriverExit |
+K_NEEDS_KEY = 'NeedsLibrary' |
+ |
+def OutputLddFormat(input_file, metadata): |
+ ''' Dump NEEDED dependencies in the ldd style output format. |
+ lib1 => path1 (0xaddress). |
+ However, we use dummy paths and addresses. |
+ ''' |
+ needed = metadata[K_NEEDS_KEY] |
+ dummy_path = '/dummy_path' |
+ for (index, lib) in enumerate(needed): |
+ dummy_address = 0xdeadbeef + index |
+ print '\t%s => %s/%s (0x%x)' % (lib, |
+ dummy_path, |
+ lib, |
+ dummy_address) |
+ |
+ |
+def OutputObjdumpFormat(input_file, metadata): |
+ '''Dump NEEDED dependencies in the objdump -p output format. |
+ Most of the objdump -p output will be faked. |
+ Only the NEEDED entries will be reliable. |
+ ''' |
+ needed = metadata[K_NEEDS_KEY] |
+ print '%s: file format le32-bitcode' % (pathtools.touser(input_file)) |
+ needed_string = '\n'.join( |
+ [ ' NEEDED\t\t%s' % libname for libname in needed ]) |
+ print ''' |
+Program Header: |
+ No program header info. |
+ |
+Dynamic Section: |
+%s |
+ Other bits not available. |
+ |
+Version References: |
+ No version info. |
+ |
+''' % needed_string |
+ |
+ |
+def OutputPnaclFormat(input_file, metadata): |
+ '''Dump NEEDED dependencies in the custom pnacl output format. |
+ ''' |
+ print pathtools.touser(input_file) + ":" |
+ for k, v in metadata.iteritems(): |
+ if isinstance(v, list): |
+ v = "[ " + ', '.join(v) + " ]" |
+ print " %-12s: %s" % (k, v) |
+ |
+ |
+''' Supported output formats { mode : (description, outputter_function) } ''' |
+OUTPUT_FORMATS = { |
+ 'ldd' : |
+ ('ldd-style output format (with dummy load addresses / file paths).', |
+ OutputLddFormat), |
+ 'objdump' : |
+ ('objdump -p style output format (NEEDED xxx)', |
+ OutputObjdumpFormat), |
+ 'pnacl' : |
+ ('custom pnacl output format', |
+ OutputPnaclFormat), |
+} |
+ |
EXTRA_ENV = { |
'INPUTS' : '', |
+ 'OUTPUT_FORMAT' : 'pnacl', |
} |
env.update(EXTRA_ENV) |
META_PATTERNS = [ |
+ ( '--format=(.*)', "env.set('OUTPUT_FORMAT', $0)"), |
( '(.*)', "env.append('INPUTS', pathtools.normalize($0))"), |
] |
def Usage(): |
- print "Usage: pnacl-meta [files...]" |
+ print "Usage: pnacl-meta [files...] [--format={ldd, objdump, pnacl}]" |
print "Show the PNaCl-specific metadata of a bitcode file" |
+ |
+def CheckOutputFormat(out_format): |
+ if out_format not in OUTPUT_FORMATS.keys(): |
+ possible_formats = '\n'.join([ ('\t%s:\t%s' % (key, desc)) |
+ for (key, (desc, _)) |
+ in OUTPUT_FORMATS.items() ]) |
+ Log.Fatal('--format is currently %s.\nIt must be one of the following:\n%s', |
+ out_format, possible_formats) |
+ _, outputter = OUTPUT_FORMATS[out_format] |
+ return outputter |
+ |
def main(argv): |
ParseArgs(argv, META_PATTERNS) |
- |
+ out_format = env.getone('OUTPUT_FORMAT') |
+ outputter = CheckOutputFormat(out_format) |
inputs = env.get('INPUTS') |
if not inputs: |
@@ -41,11 +118,7 @@ def main(argv): |
if not IsBitcode(f): |
Log.Fatal("%s: File is not bitcode", pathtools.touser(f)) |
metadata = GetBitcodeMetadata(f) |
- print pathtools.touser(f) + ":" |
- for k, v in metadata.iteritems(): |
- if isinstance(v, list): |
- v = "[ " + ', '.join(v) + " ]" |
- print " %-12s: %s" % (k, v) |
+ outputter(f, metadata) |
return 0 |