Guido van Rossum | d8eb211 | 1998-08-04 17:57:28 +0000 | [diff] [blame] | 1 | #! /bin/env python |
| 2 | """ Dump data about a Metrowerks object file. |
| 3 | |
| 4 | Based on reverse-engineering the library file format, since the docs are |
| 5 | wrong. |
| 6 | |
| 7 | Copyright (C) 1997 Chris Herborth (chrish@qnx.com) |
| 8 | """ |
| 9 | |
| 10 | # ---------------------------------------------------------------------- |
| 11 | # Standard modules |
| 12 | import sys, getopt, string, time |
| 13 | |
| 14 | # ---------------------------------------------------------------------- |
| 15 | # Extra goodies |
| 16 | from dumpar import mk_long, str2hex, print_offset, get_string |
| 17 | |
| 18 | # ---------------------------------------------------------------------- |
| 19 | def mk_short( str ): |
| 20 | """ convert a 2-byte string into a number |
| 21 | |
| 22 | Assumes big-endian! |
| 23 | """ |
| 24 | if len( str ) < 2: |
| 25 | raise ValueError, "str must be 2 bytes long" |
| 26 | |
| 27 | num = ord( str[1] ) |
| 28 | num = num + ord( str[0] ) * 0x100 |
| 29 | |
| 30 | return num |
| 31 | |
| 32 | # ---------------------------------------------------------------------- |
| 33 | def usage(): |
| 34 | """ Display a usage message and exit. |
| 35 | """ |
| 36 | print "dumpo [-v] object1 [object2 ... objectn]" |
| 37 | print |
| 38 | print "Attempt to display some useful information about the contents" |
| 39 | print "of the given Metrowerks object file(s)." |
| 40 | print |
| 41 | print "-v Be verbose (displays offsets along with the data)" |
| 42 | raise SystemExit |
| 43 | |
| 44 | # ---------------------------------------------------------------------- |
| 45 | def dump_o( file, verbose ): |
| 46 | """ dump information about a Metrowerks object file |
| 47 | |
| 48 | Note that there is more info there, 6 more quads before the file name. |
| 49 | """ |
| 50 | offset = 0 |
| 51 | |
| 52 | print "Dumping object:", file |
| 53 | |
| 54 | # Attempt to read the data. |
| 55 | try: |
| 56 | data = open( file ).read() |
| 57 | except IOError, retval: |
| 58 | print "*** Unable to open file %s: %s" % ( file, retval[1] ) |
| 59 | return |
| 60 | |
| 61 | # Check the magic number. |
| 62 | if verbose: |
| 63 | print_offset( offset ) |
| 64 | print "Magic:", |
| 65 | magic = data[offset:offset + 8] |
| 66 | print "'%s'" % ( magic ) |
| 67 | if magic != "MWOBPPC ": |
| 68 | print "*** Invalid magic number!" |
| 69 | return |
| 70 | |
| 71 | offset = offset + 8 |
| 72 | |
| 73 | # version |
| 74 | if verbose: |
| 75 | print_offset( offset ) |
| 76 | print "version:", mk_long( data[offset:offset + 4] ) |
| 77 | offset = offset + 4 |
| 78 | |
| 79 | # flags |
| 80 | if verbose: |
| 81 | print_offset( offset ) |
| 82 | print "flags:", str2hex( data[offset:offset + 4] ) |
| 83 | offset = offset + 4 |
| 84 | |
| 85 | # code size |
| 86 | if verbose: |
| 87 | print_offset( offset ) |
| 88 | print "code size:", mk_long( data[offset:offset + 4] ) |
| 89 | offset = offset + 4 |
| 90 | |
| 91 | # data size |
| 92 | if verbose: |
| 93 | print_offset( offset ) |
| 94 | print "data size:", mk_long( data[offset:offset + 4] ) |
| 95 | offset = offset + 4 |
| 96 | |
| 97 | # ---------------------------------------------------------------------- |
| 98 | def main(): |
| 99 | """ mainline |
| 100 | """ |
| 101 | |
| 102 | # Set up some defaults |
| 103 | be_verbose = 0 |
| 104 | |
| 105 | # First, check the command-line arguments |
| 106 | try: |
| 107 | opt, args = getopt.getopt( sys.argv[1:], "vh?" ) |
| 108 | except getopt.error: |
| 109 | print "*** Error parsing command-line options!" |
| 110 | usage() |
| 111 | |
| 112 | for o in opt: |
| 113 | if o[0] == "-h" or o[0] == "-?": |
| 114 | usage() |
| 115 | elif o[0] == "-v": |
| 116 | be_verbose = 1 |
| 117 | else: |
| 118 | print "*** Unknown command-line option!" |
| 119 | usage() |
| 120 | |
| 121 | # Now we can attempt to dump info about the arguments. |
| 122 | for obj in args: |
| 123 | dump_o( obj, be_verbose ) |
| 124 | |
| 125 | if __name__ == "__main__": |
| 126 | main() |