Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 136 lines (118 sloc) 3.331 kb
a4dd2e5 @Benabik Toy Disassembler
Benabik authored
1 /* Basic disassembler to explore the Packfile PMCs */
2
3 function get_packfile(string filename) {
4 var packfile = new 'Packfile';
5
6 var file = open(filename);
7 file.encoding('binary');
8 packfile.unpack(string(file.readall()));
9 file.close();
10
11 return packfile;
12 }
13
14 function print_header(var packfile) {
15 // Pull it into a single hash for simplicity
16 var info = new 'Hash';
17 for( string key in [
18 'wordsize', 'byteorder', 'fptype', 'uuid_type',
19 'version_major', 'version_minor', 'version_patch',
20 'bytecode_major', 'bytecode_minor'
21 ] ) {
22 info[key] = int(packfile[key]);
23 }
24 info['uuid'] = string(packfile['uuid']);
25
26 say( 'Core Version : ', info['version_major'], '.',
27 info['version_minor'], '.', info['version_patch'] );
28 say( 'Bytecode Version: ', info['bytecode_major'],
29 '.', info['bytecode_minor'] );
30 say( 'UUID (type ', info['uuid_type'], ') : ', info['uuid'] );
31 say( 'Wordsize ', info['wordsize'],
32 ' Byteorder ', info['byteorder'],
33 ' Floattype ', info['fptype'] );
34 }
35
36 function bytecode_segment(var segment) {
37 say( ' Opmap:' );
38 // XXX: PMCs missing oplib information
39 var opmap = segment.opmap();
40 int i, size = opmap;
41 for (i = 0; i < size; i++)
42 say( ' ', i, ': ', opmap[i] );
43
44 // TODO: disassemble integers into bytecode information
45 // XXX: Useful information in op_info_table, missing from PMCs
46 // - opcount, types, jump
47 say( ' Bytecode:' );
48 print( ' ' );
49 size = elements(segment);
50 for (i = 0; i < size; i++)
51 print( segment[i], ', ' );
52 say();
53 }
54
55 function constant_table(var segment) {
56 int i, size;
57
58 size = segment.num_count();
59 say( ' ', size, ' numeric constants:' );
60 for (i = 0; i < size; i++)
61 say( ' ', i, ': ', float(segment[i]) );
62
63 size = segment.str_count();
64 say( ' ', size, ' string constants:' );
65 for (i = 0; i < size; i++) {
66 string s = segment[i];
67 say( ' ', i, ' (', encoding_name(s), '): ', s );
68 }
69
70 // XXX: This appears to be WRONG!
71 size = segment.num_count();
72 say( ' ', size, ' pmc constants:' );
73 for (i = 0; i < size; i++) {
74 var c = segment[i];
75 say( ' ', i, ' (', typeof(c), '): ', c );
76 }
77 }
78
79 function debug_segment(var segment) {
80 int size = segment;
81 say( ' ', size, ' debug mappings:' );
82 for (int i = 0; i < size; i++) {
83 // XXX: Seems to output line for filename?
84 string file = segment[i];
85 int line = var(segment[i]);
86 say( ' ', i, ': ', file, ' ', line );
87 }
88 }
89
90 function main[main](var argv) {
91 // Process arguments
92 string progname = argv[0];
93 if (int(argv) != 2) {
94 cry('Usage: ', progname, ' <PBC file>');
95 exit(1);
96 }
97 string filename = argv[1];
98
99 // Get a Packfile
100 var packfile;
101 try {
102 packfile = get_packfile(filename);
103 } catch (e) {
104 cry( progname, ': Error reading packfile ', filename );
105 cry(e);
106 exit(1);
107 }
108
109 // Basic information
110 print_header(packfile);
111
112 // Directory
113 var directory = packfile.get_directory();
114 say();
115 say( elements(directory), ' segments:') ;
116 for( var segname in directory ) {
117 var segment = directory[segname];
118 string type = typeof(segment);
119
120 say( segname );
121 switch(type) {
122 case 'PackfileBytecodeSegment':
123 bytecode_segment(segment);
124 break;
125 case 'PackfileConstantTable':
126 constant_table(segment);
127 break;
128 case 'PackfileDebug':
129 debug_segment(segment);
130 break;
131 default:
132 say( ' Unknown segment type ', type );
133 }
134 }
135 }
Something went wrong with that request. Please try again.