Add SDL.NET bindings, reorganize
[mono.git] / mono / dis / dump.c
1 /*
2  * dump.c: Dumping routines for the disassembler.
3  *
4  * Author:
5  *   Miguel de Icaza (miguel@ximian.com)
6  *
7  * (C) 2001 Ximian, Inc.
8  */
9 #include <config.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <glib.h>
13 #include "meta.h"
14 #include "util.h"
15 #include "dump.h"
16 #include "get.h"
17 #include "mono/metadata/loader.h"
18 #include "mono/metadata/class.h"
19
20 void
21 dump_table_assembly (MonoImage *m)
22 {
23         MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
24         guint32 cols [MONO_ASSEMBLY_SIZE];
25         const char *ptr;
26         int len;
27
28         mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
29         fprintf (output, "Assembly Table\n");
30
31         fprintf (output, "Name:          %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
32         fprintf (output, "Hash Algoritm: 0x%08x\n", cols [MONO_ASSEMBLY_HASH_ALG]);
33         fprintf (output, "Version:       %d.%d.%d.%d\n", cols [MONO_ASSEMBLY_MAJOR_VERSION], 
34                                         cols [MONO_ASSEMBLY_MINOR_VERSION], 
35                                         cols [MONO_ASSEMBLY_BUILD_NUMBER], 
36                                         cols [MONO_ASSEMBLY_REV_NUMBER]);
37         fprintf (output, "Flags:         0x%08x\n", cols [MONO_ASSEMBLY_FLAGS]);
38         fprintf (output, "PublicKey:     BlobPtr (0x%08x)\n", cols [MONO_ASSEMBLY_PUBLIC_KEY]);
39
40         ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
41         len = mono_metadata_decode_value (ptr, &ptr);
42         if (len > 0){
43                 fprintf (output, "\tDump:");
44                 hex_dump (ptr, 0, len);
45                 fprintf (output, "\n");
46         } else
47                 fprintf (output, "\tZero sized public key\n");
48         
49         fprintf (output, "Culture:       %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]));
50         fprintf (output, "\n");
51 }
52
53 void
54 dump_table_typeref (MonoImage *m)
55 {
56         MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEREF];
57         int i;
58
59         fprintf (output, "Typeref Table\n");
60         
61         for (i = 1; i <= t->rows; i++){
62                 char *s = get_typeref (m, i);
63                 
64                 fprintf (output, "%d: %s\n", i, s);
65                 g_free (s);
66         }
67         fprintf (output, "\n");
68 }
69
70 void
71 dump_table_typedef (MonoImage *m)
72 {
73         MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
74         int i;
75
76         fprintf (output, "Typedef Table\n");
77         
78         for (i = 1; i <= t->rows; i++){
79                 char *s = get_typedef (m, i);
80                 guint32 cols [MONO_TYPEDEF_SIZE];
81
82                 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], i - 1, cols, MONO_TYPEDEF_SIZE);
83
84                 fprintf (output, "%d: %s (flist=%d, mlist=%d, flags=0x%x, extends=0x%x)\n", i, s, 
85                                         cols [MONO_TYPEDEF_FIELD_LIST], cols [MONO_TYPEDEF_METHOD_LIST],
86                                         cols [MONO_TYPEDEF_FLAGS], cols [MONO_TYPEDEF_EXTENDS]);
87                 g_free (s);
88         }
89         fprintf (output, "\n");
90 }
91
92 void
93 dump_table_assemblyref (MonoImage *m)
94 {
95         MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
96         int i;
97
98         fprintf (output, "AssemblyRef Table\n");
99         
100         for (i = 0; i < t->rows; i++){
101                 const char *ptr;
102                 int len;
103                 guint32 cols [MONO_ASSEMBLYREF_SIZE];
104
105                 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
106                 fprintf (output, "%d: Version=%d.%d.%d.%d\n\tName=%s\n", i + 1,
107                          cols [MONO_ASSEMBLYREF_MAJOR_VERSION], 
108                          cols [MONO_ASSEMBLYREF_MINOR_VERSION], 
109                          cols [MONO_ASSEMBLYREF_BUILD_NUMBER], 
110                          cols [MONO_ASSEMBLYREF_REV_NUMBER],
111                          mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
112                 ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLYREF_PUBLIC_KEY]);
113                 len = mono_metadata_decode_value (ptr, &ptr);
114                 if (len > 0){
115                         fprintf (output, "\tPublic Key:");
116                         hex_dump (ptr, 0, len);
117                         fprintf (output, "\n");
118                 } else
119                         fprintf (output, "\tZero sized public key\n");
120                 
121         }
122         fprintf (output, "\n");
123 }
124
125 void
126 dump_table_param (MonoImage *m)
127 {
128         MonoTableInfo *t = &m->tables [MONO_TABLE_PARAM];
129         int i;
130
131         fprintf (output, "Param Table\n");
132         
133         for (i = 0; i < t->rows; i++){
134                 guint32 cols [MONO_PARAM_SIZE];
135
136                 mono_metadata_decode_row (t, i, cols, CSIZE (cols));
137                 fprintf (output, "%d: 0x%04x %d %s\n",
138                          i + 1,
139                          cols [MONO_PARAM_FLAGS], cols [MONO_PARAM_SEQUENCE], 
140                          mono_metadata_string_heap (m, cols [MONO_PARAM_NAME]));
141         }
142         fprintf (output, "\n");
143 }
144
145 void
146 dump_table_field (MonoImage *m)
147 {
148         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
149         MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
150         MonoTableInfo *fl = &m->tables [MONO_TABLE_FIELDLAYOUT];
151         MonoTableInfo *rva = &m->tables [MONO_TABLE_FIELDRVA];
152         int i, current_type, offset_row, rva_row;
153         guint32 first_m, last_m;
154
155         fprintf (output, "Field Table (1..%d)\n", t->rows);
156         
157         rva_row = offset_row = current_type = 1;
158         last_m = first_m = 1;
159         for (i = 1; i <= t->rows; i++){
160                 guint32 cols [MONO_FIELD_SIZE];
161                 char *sig, *flags;
162
163                 /*
164                  * Find the next type.
165                  */
166                 while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_FIELD_LIST))) {
167                         current_type++;
168                 }
169                 if (i == first_m) {
170                         fprintf (output, "########## %s.%s\n",
171                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
172                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
173                         first_m = last_m;
174                 }
175                 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_SIZE);
176                 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
177                 flags = field_flags (cols [MONO_FIELD_FLAGS]);
178                 fprintf (output, "%d: %s %s: %s\n",
179                          i,
180                          sig,
181                          mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]),
182                          flags);
183                 g_free (sig);
184                 g_free (flags);
185                 if (offset_row <= fl->rows && (mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_FIELD) == i)) {
186                         fprintf (output, "\texplicit offset: %d\n", mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_OFFSET));
187                         offset_row ++;
188                 }
189                 if (rva_row <= rva->rows && (mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_FIELD) == i)) {
190                         fprintf (output, "\trva: %d\n", mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_RVA));
191                         rva_row ++;
192                 }
193         }
194         fprintf (output, "\n");
195 }
196
197 void
198 dump_table_memberref (MonoImage *m)
199 {
200         MonoTableInfo *t = &m->tables [MONO_TABLE_MEMBERREF];
201         int i, kind, idx;
202         char *x, *xx;
203         char *sig;
204         const char *blob, *ks;
205
206         fprintf (output, "MemberRef Table (1..%d)\n", t->rows);
207
208         for (i = 0; i < t->rows; i++){
209                 guint32 cols [MONO_MEMBERREF_SIZE];
210
211                 mono_metadata_decode_row (t, i, cols, MONO_MEMBERREF_SIZE);
212                 
213                 kind = cols [MONO_MEMBERREF_CLASS] & 7;
214                 idx = cols [MONO_MEMBERREF_CLASS] >> 3;
215
216                 x = g_strdup ("UNHANDLED CASE");
217                 
218                 switch (kind){
219                 case 0:
220                         ks = "TypeDef"; break;
221                 case 1:
222                         ks = "TypeRef";
223                         xx = get_typeref (m, idx);
224                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
225                         g_free (xx);
226                         break;
227                 case 2:
228                         ks = "ModuleRef"; break;
229                 case 3:
230                         ks = "MethodDef"; break;
231                 case 4:
232                         ks = "TypeSpec";
233                         xx = get_typespec (m, idx);
234                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
235                         g_free (xx);
236                         break;
237                 default:
238                         g_error ("Unknown tag: %d\n", kind);
239                 }
240                 blob = mono_metadata_blob_heap (m, cols [MONO_MEMBERREF_SIGNATURE]);
241                 mono_metadata_decode_blob_size (blob, &blob);
242                 if (*blob == 0x6) { /* it's a field */
243                         sig = get_field_signature (m, cols [MONO_MEMBERREF_SIGNATURE]);
244                 } else {
245                         sig = get_methodref_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
246                 }
247                 fprintf (output, "%d: %s[%d] %s\n\tResolved: %s\n\tSignature: %s\n\t\n",
248                          i + 1,
249                          ks, idx,
250                          mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]),
251                          x ? x : "",
252                          sig);
253
254                 if (x)
255                         g_free (x);
256                 g_free (sig);
257         }
258 }
259
260 void
261 dump_table_class_layout (MonoImage *m)
262 {
263         MonoTableInfo *t = &m->tables [MONO_TABLE_CLASSLAYOUT];
264         int i;
265         fprintf (output, "ClassLayout Table (1..%d)\n", t->rows);
266
267         for (i = 0; i < t->rows; i++){
268                 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
269                 
270                 mono_metadata_decode_row (t, i, cols, CSIZE (cols));
271
272                 fprintf (output, "%d: PackingSize=%d  ClassSize=%d  Parent=%s\n",
273                          i + 1, cols [MONO_CLASS_LAYOUT_PACKING_SIZE], 
274                          cols [MONO_CLASS_LAYOUT_CLASS_SIZE], 
275                          get_typedef (m, cols [MONO_CLASS_LAYOUT_PARENT]));
276         }
277 }
278
279 void
280 dump_table_constant (MonoImage *m)
281 {
282         MonoTableInfo *t = &m->tables [MONO_TABLE_CONSTANT];
283         int i;
284         const char *desc [] = {
285                 "Field",
286                 "Param",
287                 "Property",
288                 ""
289         };
290         fprintf (output, "Constant Table (1..%d)\n", t->rows);
291
292         for (i = 0; i < t->rows; i++){
293                 guint32 cols [MONO_CONSTANT_SIZE];
294                 const char *parent = desc [cols [MONO_CONSTANT_PARENT] & HASCONSTANT_MASK];
295                 
296                 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
297
298                 fprintf (output, "%d: Parent= %s: %d %s\n",
299                          i + 1, parent, cols [MONO_CONSTANT_PARENT] >> HASCONSTANT_BITS, 
300                          get_constant (m, (MonoTypeEnum) cols [MONO_CONSTANT_TYPE], cols [MONO_CONSTANT_VALUE]));
301         }
302         
303 }
304
305 void
306 dump_table_property_map (MonoImage *m)
307 {
308         MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTYMAP];
309         int i;
310         char *s;
311         
312         fprintf (output, "Property Map Table (1..%d)\n", t->rows);
313         
314         for (i = 0; i < t->rows; i++){
315                 guint32 cols [MONO_PROPERTY_MAP_SIZE];
316                 
317                 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_MAP_SIZE);
318                 s = get_typedef (m, cols [MONO_PROPERTY_MAP_PARENT]);
319                 fprintf (output, "%d: %s %d\n", i + 1, s, cols [MONO_PROPERTY_MAP_PROPERTY_LIST]);
320                 g_free (s);
321         }
322 }
323
324 void
325 dump_table_property (MonoImage *m)
326 {
327         MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTY];
328         int i, j, pcount;
329         const char *ptr;
330         char flags[128];
331
332         fprintf (output, "Property Table (1..%d)\n", t->rows);
333
334         for (i = 0; i < t->rows; i++){
335                 guint32 cols [MONO_PROPERTY_SIZE];
336                 char *type;
337                 int bsize;
338                 int prop_flags;
339                 
340                 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_SIZE);
341                 flags [0] = 0;
342                 prop_flags = cols [MONO_PROPERTY_FLAGS];
343                 if (prop_flags & 0x0200)
344                         strcat (flags, "special ");
345                 if (prop_flags & 0x0400)
346                         strcat (flags, "runtime ");
347                 if (prop_flags & 0x1000)
348                         strcat (flags, "hasdefault ");
349
350                 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
351                 bsize = mono_metadata_decode_blob_size (ptr, &ptr);
352                 /* ECMA claims 0x08 ... */
353                 if (*ptr != 0x28 && *ptr != 0x08)
354                                 g_warning("incorrect signature in propert blob: 0x%x", *ptr);
355                 ptr++;
356                 pcount = mono_metadata_decode_value (ptr, &ptr);
357                 ptr = get_type (m, ptr, &type);
358                 fprintf (output, "%d: %s %s (",
359                          i + 1, type, mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]));
360                 g_free (type);
361
362                 for (j = 0; j < pcount; j++){
363                                 ptr = get_param (m, ptr, &type);
364                                 fprintf (output, "%s%s", j > 0? ", " : "",type);
365                                 g_free (type);
366                 }
367                 fprintf (output, ") %s\n", flags);
368         }
369 }
370
371 void
372 dump_table_event (MonoImage *m)
373 {
374         MonoTableInfo *t = &m->tables [MONO_TABLE_EVENT];
375         int i;
376         fprintf (output, "Event Table (1..%d)\n", t->rows);
377
378         for (i = 0; i < t->rows; i++){
379                 guint32 cols [MONO_EVENT_SIZE];
380                 const char *name;
381                 char *type;
382                 
383                 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
384
385                 name = mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]);
386                 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE]);
387                 fprintf (output, "%d: %s %s %s\n", i + 1, type, name,
388                          cols [MONO_EVENT_FLAGS] & 0x200 ? "specialname " : "");
389                 g_free (type);
390         }
391         
392 }
393
394 void
395 dump_table_file (MonoImage *m)
396 {
397         MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
398         int i, j, len;
399         fprintf (output, "File Table (1..%d)\n", t->rows);
400
401         for (i = 0; i < t->rows; i++){
402                 guint32 cols [MONO_FILE_SIZE];
403                 const char *name, *hash;
404                 
405                 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
406
407                 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
408                 fprintf (output, "%d: %s %s [", i + 1, name, 
409                                 cols [MONO_FILE_FLAGS] & 0x1 ? "nometadata" : "containsmetadata");
410                 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
411                 len = mono_metadata_decode_blob_size (hash, &hash);
412                 for (j = 0; j < len; ++j)
413                         fprintf (output, "%s%02X", j? " ": "", hash [j] & 0xff);
414                 fprintf (output, "]\n");
415         }
416         
417 }
418
419 static char*
420 get_manifest_implementation (MonoImage *m, guint32 idx)
421 {
422         guint32 row;
423         const char* table = "";
424         if (!idx)
425                 return g_strdup ("current module");
426         row = idx >> IMPLEMENTATION_BITS;
427         switch (idx & IMPLEMENTATION_MASK) {
428         case IMPLEMENTATION_FILE:
429                 table = "file";
430                 break;
431         case IMPLEMENTATION_ASSEMBLYREF:
432                 table = "assemblyref";
433                 break;
434         case IMPLEMENTATION_EXP_TYPE:
435         default:
436                 g_assert_not_reached ();
437         }
438         return g_strdup_printf ("%s %d", table, row);
439 }
440
441 static const char*
442 get_manifest_flags (guint32 mf)
443 {
444         mf &= 3;
445         switch (mf) {
446         case 1: return "public";
447         case 2: return "private";
448         default:
449                 return "";
450         }
451 }
452
453 void
454 dump_table_manifest (MonoImage *m)
455 {
456         MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
457         int i;
458         fprintf (output, "Manifestresource Table (1..%d)\n", t->rows);
459
460         for (i = 0; i < t->rows; i++){
461                 guint32 cols [MONO_MANIFEST_SIZE];
462                 const char *name, *mf;
463                 char *impl;
464                 
465                 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
466
467                 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
468                 mf = get_manifest_flags (cols [MONO_MANIFEST_FLAGS]);
469                 impl = get_manifest_implementation (m, cols [MONO_MANIFEST_IMPLEMENTATION]);
470                 fprintf (output, "%d: %s '%s' at offset %u in %s\n", i + 1, mf, name, cols [MONO_MANIFEST_OFFSET], impl);
471                 g_free (impl);
472         }
473         
474 }
475
476 void
477 dump_table_moduleref (MonoImage *m)
478 {
479         MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
480         int i;
481         fprintf (output, "ModuleRef Table (1..%d)\n", t->rows);
482
483         for (i = 0; i < t->rows; i++){
484                 guint32 cols [MONO_MODULEREF_SIZE];
485                 const char *name;
486                 
487                 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
488
489                 name = mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]);
490                 fprintf (output, "%d: %s\n", i + 1, name);
491         }
492         
493 }
494
495 void
496 dump_table_module (MonoImage *m)
497 {
498         MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
499         int i;
500         fprintf (output, "ModuleRef Table (1..%d)\n", t->rows);
501
502         for (i = 0; i < t->rows; i++){
503                 guint32 cols [MONO_MODULE_SIZE];
504                 const char *name;
505                 char *guid;
506                 
507                 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
508
509                 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
510                 guid = get_guid (m, cols [MONO_MODULE_MVID]);
511                 fprintf (output, "%d: %s %d %s\n", i + 1, name, cols [MONO_MODULE_MVID], guid);
512         }
513         
514 }
515
516 void
517 dump_table_method (MonoImage *m)
518 {
519         MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
520         MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
521         int i, current_type;
522         guint32 first_m, last_m;
523         fprintf (output, "Method Table (1..%d)\n", t->rows);
524
525         current_type = 1;
526         last_m = first_m = 1;
527         for (i = 1; i <= t->rows; i++){
528                 guint32 cols [MONO_METHOD_SIZE];
529                 char *sig;
530                 const char *sigblob;
531                 MonoMethodSignature *method;
532
533                 /*
534                  * Find the next type.
535                  */
536                 while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_METHOD_LIST))) {
537                         current_type++;
538                 }
539                 if (i == first_m) {
540                         fprintf (output, "########## %s.%s\n",
541                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
542                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
543                         first_m = last_m;
544                 }
545                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHOD_SIZE);
546                 sigblob = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
547                 mono_metadata_decode_blob_size (sigblob, &sigblob);
548                 method = mono_metadata_parse_method_signature (m, i, sigblob, &sigblob);
549                 sig = dis_stringify_method_signature (m, method, i);
550                 fprintf (output, "%d: %s (param: %d)\n", i, sig, cols [MONO_METHOD_PARAMLIST]);
551                 g_free (sig);
552                 mono_metadata_free_method_signature (method);
553         }
554         
555 }
556
557 static guint32
558 method_dor_to_token (guint32 idx) {
559         switch (idx & METHODDEFORREF_MASK) {
560         case METHODDEFORREF_METHODDEF:
561                 return MONO_TOKEN_METHOD_DEF | (idx >> METHODDEFORREF_BITS);
562         case METHODDEFORREF_METHODREF:
563                 return MONO_TOKEN_MEMBER_REF | (idx >> METHODDEFORREF_BITS);
564         }
565         return -1;
566 }
567
568 void
569 dump_table_methodimpl (MonoImage *m)
570 {
571         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODIMPL];
572         MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
573         int i;
574
575         fprintf (output, "MethodImpl Table (1..%d)\n", t->rows);
576
577         for (i = 1; i <= t->rows; i++){
578                 guint32 cols [MONO_METHODIMPL_SIZE];
579                 char *class, *impl, *decl;
580
581                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHODIMPL_SIZE);
582                 class = get_typedef (m, cols [MONO_METHODIMPL_CLASS]);
583                 impl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_BODY]));
584                 decl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_DECLARATION]));
585                 fprintf (output, "%d: %s\n\tdecl: %s\n\timpl: %s\n", i, class, decl, impl);
586                 g_free (class);
587                 g_free (impl);
588                 g_free (decl);
589         }
590         
591 }
592
593 static map_t semantics_map [] = {
594                 {1, "setter"},
595                 {2, "getter"},
596                 {4, "other"},
597                 {8, "add-on"},
598                 {0x10, "remove-on"},
599                 {0x20, "fire"},
600                 {0, NULL},
601 };
602
603 void
604 dump_table_methodsem (MonoImage *m)
605 {
606         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSEMANTICS];
607         int i, is_property, index;
608         const char *semantics;
609         
610         fprintf (output, "Method Semantics Table (1..%d)\n", t->rows);
611         for (i = 1; i <= t->rows; i++){
612                 guint32 cols [MONO_METHOD_SEMA_SIZE];
613                 
614                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHOD_SEMA_SIZE);
615                 semantics = flags (cols [MONO_METHOD_SEMA_SEMANTICS], semantics_map);
616                 is_property = cols [MONO_METHOD_SEMA_ASSOCIATION] & HAS_SEMANTICS_MASK;
617                 index = cols [MONO_METHOD_SEMA_ASSOCIATION] >> HAS_SEMANTICS_BITS;
618                 fprintf (output, "%d: [%d] %s method: %d %s %d\n", i, cols [MONO_METHOD_SEMA_ASSOCIATION], semantics,
619                                                 cols [MONO_METHOD_SEMA_METHOD] - 1, 
620                                                 is_property? "property" : "event",
621                                                 index);
622         }
623 }
624
625 void 
626 dump_table_interfaceimpl (MonoImage *m)
627 {
628         MonoTableInfo *t = &m->tables [MONO_TABLE_INTERFACEIMPL];
629         int i;
630
631         fprintf (output, "Interface Implementation Table (1..%d)\n", t->rows);
632         for (i = 1; i <= t->rows; i++) {
633                 guint32 cols [MONO_INTERFACEIMPL_SIZE];
634                 
635                 mono_metadata_decode_row (t, i - 1, cols, MONO_INTERFACEIMPL_SIZE);
636                 fprintf (output, "%d: %s implements %s\n", i,
637                         get_typedef (m, cols [MONO_INTERFACEIMPL_CLASS]),
638                         get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE]));
639         }
640 }
641
642 static char*
643 has_cattr_get_table (MonoImage *m, guint32 val)
644 {
645         guint32 t = val & CUSTOM_ATTR_MASK;
646         guint32 index = val >> CUSTOM_ATTR_BITS;
647         const char *table;
648
649         switch (t) {
650         case CUSTOM_ATTR_METHODDEF:
651                 table = "MethodDef";
652                 break;
653         case CUSTOM_ATTR_FIELDDEF:
654                 table = "FieldDef";
655                 break;
656         case CUSTOM_ATTR_TYPEREF:
657                 table = "TypeRef";
658                 break;
659         case CUSTOM_ATTR_TYPEDEF:
660                 table = "TypeDef";
661                 break;
662         case CUSTOM_ATTR_PARAMDEF:
663                 table = "Param";
664                 break;
665         case CUSTOM_ATTR_INTERFACE:
666                 table = "InterfaceImpl";
667                 break;
668         case CUSTOM_ATTR_MEMBERREF:
669                 table = "MemberRef";
670                 break;
671         case CUSTOM_ATTR_MODULE:
672                 table = "Module";
673                 break;
674         case CUSTOM_ATTR_PERMISSION:
675                 table = "DeclSecurity?";
676                 break;
677         case CUSTOM_ATTR_PROPERTY:
678                 table = "Property";
679                 break;
680         case CUSTOM_ATTR_EVENT:
681                 table = "Event";
682                 break;
683         case CUSTOM_ATTR_SIGNATURE:
684                 table = "StandAloneSignature";
685                 break;
686         case CUSTOM_ATTR_MODULEREF:
687                 table = "ModuleRef";
688                 break;
689         case CUSTOM_ATTR_TYPESPEC:
690                 table = "TypeSpec";
691                 break;
692         case CUSTOM_ATTR_ASSEMBLY:
693                 table = "Assembly";
694                 break;
695         case CUSTOM_ATTR_ASSEMBLYREF:
696                 table = "AssemblyRef";
697                 break;
698         case CUSTOM_ATTR_FILE:
699                 table = "File";
700                 break;
701         case CUSTOM_ATTR_EXP_TYPE:
702                 table = "ExportedType";
703                 break;
704         case CUSTOM_ATTR_MANIFEST:
705                 table = "Manifest";
706                 break;
707         default:
708                 table = "Unknown";
709                 break;
710         }
711         /*
712          * FIXME: we should decode the index into something more uman-friendly.
713          */
714         return g_strdup_printf ("%s: %d", table, index);
715 }
716
717 static char*
718 custom_attr_params (MonoImage *m, MonoMethodSignature* sig, const char* value)
719 {
720         int len, i, slen, type;
721         GString *res;
722         char *s;
723         const char *p = value;
724
725         len = mono_metadata_decode_value (p, &p);
726         if (len < 2 || read16 (p) != 0x0001) /* Prolog */
727                 return g_strdup ("");
728
729         /* skip prolog */
730         p += 2;
731         res = g_string_new ("");
732         for (i = 0; i < sig->param_count; ++i) {
733                 if (i != 0)
734                         g_string_append (res, ", ");
735                 type = sig->params [i]->type;
736 handle_enum:
737                 switch (type) {
738                 case MONO_TYPE_U1:
739                         g_string_sprintfa (res, "%d", (unsigned int)*p);
740                         ++p;
741                         break;
742                 case MONO_TYPE_I1:
743                         g_string_sprintfa (res, "%d", *p);
744                         ++p;
745                         break;
746                 case MONO_TYPE_BOOLEAN:
747                         g_string_sprintfa (res, "%s", *p?"true":"false");
748                         ++p;
749                         break;
750                 case MONO_TYPE_CHAR:
751                         g_string_sprintfa (res, "'%c'", read16 (p));
752                         p += 2;
753                         break;
754                 case MONO_TYPE_U2:
755                         g_string_sprintfa (res, "%d", read16 (p));
756                         p += 2;
757                         break;
758                 case MONO_TYPE_I2:
759                         g_string_sprintfa (res, "%d", (gint16)read16 (p));
760                         p += 2;
761                         break;
762                 case MONO_TYPE_U4:
763                         g_string_sprintfa (res, "%d", read32 (p));
764                         p += 4;
765                         break;
766                 case MONO_TYPE_I4:
767                         g_string_sprintfa (res, "%d", (gint32)read32 (p));
768                         p += 4;
769                         break;
770                 case MONO_TYPE_U8:
771                         g_string_sprintfa (res, "%lld", read64 (p));
772                         p += 8;
773                         break;
774                 case MONO_TYPE_I8:
775                         g_string_sprintfa (res, "%lld", (gint64)read64 (p));
776                         p += 8;
777                         break;
778                 case MONO_TYPE_R4: {
779                         float val;
780                         readr4 (p, &val);
781                         g_string_sprintfa (res, "%g", val);
782                         p += 4;
783                         break;
784                 }
785                 case MONO_TYPE_R8: {
786                         double val;
787                         readr8 (p, &val);
788                         g_string_sprintfa (res, "%g", val);
789                         p += 8;
790                         break;
791                 }
792                 case MONO_TYPE_VALUETYPE:
793                         if (sig->params [i]->data.klass->enumtype) {
794                                 type = sig->params [i]->data.klass->enum_basetype->type;
795                                 goto handle_enum;
796                         } else {
797                                 g_warning ("generic valutype not handled in custom attr value decoding");
798                         }
799                         break;
800                 case MONO_TYPE_CLASS: /* It must be a Type: check? */
801                 case MONO_TYPE_STRING:
802                         slen = mono_metadata_decode_value (p, &p);
803                         g_string_append_c (res, '"');
804                         g_string_append (res, p);
805                         g_string_append_c (res, '"');
806                         p += slen;
807                         break;
808                 default:
809                         g_warning ("Type %02x not handled in custom attr value decoding", sig->params [i]->type);
810                         break;
811                 }
812         }
813         slen = read16 (p);
814         if (slen) {
815                 g_string_sprintfa (res, " %d named args: (", slen);
816                 slen = len - (p - value) + 1;
817                 for (i = 0; i < slen; ++i) {
818                         g_string_sprintfa (res, " %02X", (p [i] & 0xff));
819                 }
820                 g_string_append_c (res, ')');
821         }
822         s = res->str;
823         g_string_free (res, FALSE);
824         return s;
825 }
826
827 void
828 dump_table_customattr (MonoImage *m)
829 {
830         MonoTableInfo *t = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
831         int i;
832
833         fprintf (output, "Custom Attributes Table (1..%d)\n", t->rows);
834         for (i = 1; i <= t->rows; i++) {
835                 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
836                 guint32 mtoken;
837                 char * desc;
838                 char *method;
839                 char *params;
840                 MonoMethod *meth;
841                 
842                 mono_metadata_decode_row (t, i - 1, cols, MONO_CUSTOM_ATTR_SIZE);
843                 desc = has_cattr_get_table (m, cols [MONO_CUSTOM_ATTR_PARENT]);
844                 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> CUSTOM_ATTR_TYPE_BITS;
845                 switch (cols [MONO_CUSTOM_ATTR_TYPE] & CUSTOM_ATTR_TYPE_MASK) {
846                 case CUSTOM_ATTR_TYPE_METHODDEF:
847                         mtoken |= MONO_TOKEN_METHOD_DEF;
848                         break;
849                 case CUSTOM_ATTR_TYPE_MEMBERREF:
850                         mtoken |= MONO_TOKEN_MEMBER_REF;
851                         break;
852                 default:
853                         g_warning ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
854                         break;
855                 }
856                 method = get_method (m, mtoken);
857                 meth = mono_get_method (m, mtoken, NULL);
858                 params = custom_attr_params (m, meth->signature, mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]));
859                 fprintf (output, "%d: %s: %s [%s]\n", i, desc, method, params);
860                 g_free (desc);
861                 g_free (method);
862                 g_free (params);
863         }
864 }
865
866 void
867 dump_table_nestedclass (MonoImage *m)
868 {
869         MonoTableInfo *t = &m->tables [MONO_TABLE_NESTEDCLASS];
870         guint32 cols [MONO_NESTED_CLASS_SIZE];
871         int i;
872         char *nested, *nesting;
873         fprintf (output, "NestedClass Table (1..%d)\n", t->rows);
874
875         for (i = 1; i <= t->rows; i++){
876                 mono_metadata_decode_row (t, i - 1, cols, MONO_NESTED_CLASS_SIZE);
877                 nested = get_typedef (m, cols [MONO_NESTED_CLASS_NESTED]);
878                 nesting = get_typedef (m, cols [MONO_NESTED_CLASS_ENCLOSING]);
879                 fprintf (output, "%d: %d %d: %s in %s\n", i,
880                                 cols [MONO_NESTED_CLASS_NESTED], 
881                                 cols [MONO_NESTED_CLASS_ENCLOSING], nested, nesting);
882                 g_free (nested);
883                 g_free (nesting);
884         }
885         
886 }
887
888 void
889 dump_table_exported (MonoImage *m)
890 {
891         MonoTableInfo *t = &m->tables [MONO_TABLE_EXPORTEDTYPE];
892         guint32 cols [MONO_EXP_TYPE_SIZE];
893         int i;
894         const char *name, *nspace;
895         fprintf (output, "ExportedType Table (1..%d)\n", t->rows);
896
897         for (i = 1; i <= t->rows; i++) {
898                 mono_metadata_decode_row (t, i - 1, cols, MONO_EXP_TYPE_SIZE);
899                 name = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAME]);
900                 nspace = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAMESPACE]);
901                 fprintf (output, "%d: %s %s\n", i, name, nspace);
902         }
903         
904 }
905
906 void
907 dump_table_field_marshal (MonoImage *m)
908 {
909         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDMARSHAL];
910         guint32 cols [MONO_FIELD_MARSHAL_SIZE];
911         int i, is_field, idx;
912         const char *blob;
913         char *native;
914         
915         fprintf (output, "FieldMarshal Table (1..%d)\n", t->rows);
916
917         for (i = 1; i <= t->rows; i++) {
918                 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_MARSHAL_SIZE);
919                 blob = mono_metadata_blob_heap (m, cols [MONO_FIELD_MARSHAL_NATIVE_TYPE]);
920                 native = get_marshal_info (m, blob);
921                 is_field = (cols [MONO_FIELD_MARSHAL_PARENT] & HAS_FIELD_MARSHAL_MASK) == HAS_FIELD_MARSHAL_FIELDSREF;
922                 idx = cols [MONO_FIELD_MARSHAL_PARENT] >> HAS_FIELD_MARSHAL_BITS;
923                 fprintf (output, "%d: (0x%04x) %s %d: %s\n", i, cols [MONO_FIELD_MARSHAL_PARENT], is_field? "Field" : "Param", idx, native);
924                 g_free (native);
925         }
926         
927 }
928
929 static const char*
930 get_security_action (int val) {
931         static char buf [32];
932
933         switch (val) {
934         case SECURITY_ACTION_DEMAND:
935                 return "Demand";
936         case SECURITY_ACTION_ASSERT:
937                 return "Assert";
938         case SECURITY_ACTION_DENY:
939                 return "Deny";
940         case SECURITY_ACTION_PERMITONLY:
941                 return "PermitOnly";
942         case SECURITY_ACTION_LINKDEMAND:
943                 return "LinkDemand";
944         case SECURITY_ACTION_INHERITDEMAND:
945                 return "InheritanceDemand";
946         case SECURITY_ACTION_REQMIN:
947                 return "RequestMinimum";
948         case SECURITY_ACTION_REQOPT:
949                 return "RequestOptional";
950         case SECURITY_ACTION_REQREFUSE:
951                 return "RequestRefuse";
952         default:
953                 g_snprintf (buf, sizeof (buf), "0x%04X", val);
954                 return buf;
955         }
956 }
957
958 void 
959 dump_table_declsec (MonoImage *m)
960 {
961         MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
962         guint32 cols [MONO_DECL_SECURITY_SIZE];
963         int i, len;
964         guint32 idx;
965         const char *blob, *action;
966         const char* parent[] = {
967                 "TypeDef", "MethodDef", "Assembly", ""
968         };
969         
970         fprintf (output, "DeclSecurity Table (1..%d)\n", t->rows);
971
972         for (i = 1; i <= t->rows; i++) {
973                 mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
974                 blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
975                 len = mono_metadata_decode_blob_size (blob, &blob);
976                 action = get_security_action (cols [MONO_DECL_SECURITY_ACTION]);
977                 idx = cols [MONO_DECL_SECURITY_PARENT];
978                 fprintf (output, "%d: %s on %s %d%s", i, action, parent [idx & HAS_DECL_SECURITY_MASK], idx >> HAS_DECL_SECURITY_BITS, len? ":\n\t":"\n");
979                 if (!len)
980                         continue;
981                 for (idx = 0; idx < len; ++idx)
982                         fprintf (output, "%c", blob [idx]);
983                 fprintf (output, "\n");
984         }
985 }
986