Implement partial generic methods
[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 <math.h>
14 #include "meta.h"
15 #include "util.h"
16 #include "dump.h"
17 #include "get.h"
18 #include "declsec.h"
19 #include "mono/metadata/loader.h"
20 #include "mono/metadata/class.h"
21 #include "mono/metadata/class-internals.h"
22 #include "mono/utils/mono-compiler.h"
23
24 #ifndef HAVE_ISINF
25
26 #ifdef HAVE_IEEEFP_H
27 extern int isinf (double);
28 #endif
29
30 #endif
31
32 void
33 dump_table_assembly (MonoImage *m)
34 {
35         MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
36         guint32 cols [MONO_ASSEMBLY_SIZE];
37         const char *ptr;
38         int len;
39
40         fprintf (output, "Assembly Table\n");
41
42         if (!t->rows)
43                 return;
44
45         mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
46
47         fprintf (output, "Name:          %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
48         fprintf (output, "Hash Algoritm: 0x%08x\n", cols [MONO_ASSEMBLY_HASH_ALG]);
49         fprintf (output, "Version:       %d.%d.%d.%d\n", cols [MONO_ASSEMBLY_MAJOR_VERSION], 
50                                         cols [MONO_ASSEMBLY_MINOR_VERSION], 
51                                         cols [MONO_ASSEMBLY_BUILD_NUMBER], 
52                                         cols [MONO_ASSEMBLY_REV_NUMBER]);
53         fprintf (output, "Flags:         0x%08x\n", cols [MONO_ASSEMBLY_FLAGS]);
54         fprintf (output, "PublicKey:     BlobPtr (0x%08x)\n", cols [MONO_ASSEMBLY_PUBLIC_KEY]);
55
56         ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
57         len = mono_metadata_decode_value (ptr, &ptr);
58         if (len > 0){
59                 fprintf (output, "\tDump:");
60                 hex_dump (ptr, 0, len);
61                 fprintf (output, "\n");
62         } else
63                 fprintf (output, "\tZero sized public key\n");
64         
65         fprintf (output, "Culture:       %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]));
66         fprintf (output, "\n");
67 }
68
69 void
70 dump_table_typeref (MonoImage *m)
71 {
72         MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEREF];
73         int i;
74
75         fprintf (output, "Typeref Table\n");
76         
77         for (i = 1; i <= t->rows; i++){
78                 char *s = get_typeref (m, i);
79                 
80                 fprintf (output, "%d: %s\n", i, s);
81                 g_free (s);
82         }
83         fprintf (output, "\n");
84 }
85
86 void
87 dump_table_typedef (MonoImage *m)
88 {
89         MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
90         int i;
91
92         fprintf (output, "Typedef Table\n");
93         
94         for (i = 1; i <= t->rows; i++){
95                 char *s = get_typedef (m, i);
96                 guint32 cols [MONO_TYPEDEF_SIZE];
97
98                 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], i - 1, cols, MONO_TYPEDEF_SIZE);
99
100                 fprintf (output, "%d: %s (flist=%d, mlist=%d, flags=0x%x, extends=0x%x)\n", i, s, 
101                                         cols [MONO_TYPEDEF_FIELD_LIST], cols [MONO_TYPEDEF_METHOD_LIST],
102                                         cols [MONO_TYPEDEF_FLAGS], cols [MONO_TYPEDEF_EXTENDS]);
103                 g_free (s);
104         }
105         fprintf (output, "\n");
106 }
107
108 void
109 dump_table_typespec (MonoImage *m)
110 {
111         MonoTableInfo *t = &m->tables [MONO_TABLE_TYPESPEC];
112         int i;
113
114         fprintf (output, "Typespec Table\n");
115         
116         for (i = 1; i <= t->rows; i++){         
117                 char *typespec = get_typespec (m, i, TRUE, NULL);
118
119                 fprintf (output, "%d: %s\n", i, typespec);
120                 g_free (typespec);
121         }
122         fprintf (output, "\n");
123 }
124
125 void
126 dump_table_assemblyref (MonoImage *m)
127 {
128         MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
129         int i;
130
131         fprintf (output, "AssemblyRef Table\n");
132         
133         for (i = 0; i < t->rows; i++){
134                 const char *ptr;
135                 int len;
136                 guint32 cols [MONO_ASSEMBLYREF_SIZE];
137
138                 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
139                 fprintf (output, "%d: Version=%d.%d.%d.%d\n\tName=%s\n", i + 1,
140                          cols [MONO_ASSEMBLYREF_MAJOR_VERSION], 
141                          cols [MONO_ASSEMBLYREF_MINOR_VERSION], 
142                          cols [MONO_ASSEMBLYREF_BUILD_NUMBER], 
143                          cols [MONO_ASSEMBLYREF_REV_NUMBER],
144                          mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
145                 fprintf (output, "\tFlags=0x%08x\n", cols [MONO_ASSEMBLYREF_FLAGS]);
146                 ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLYREF_PUBLIC_KEY]);
147                 len = mono_metadata_decode_value (ptr, &ptr);
148                 if (len > 0){
149                         fprintf (output, "\tPublic Key:");
150                         hex_dump (ptr, 0, len);
151                         fprintf (output, "\n");
152                 } else
153                         fprintf (output, "\tZero sized public key\n");
154                 
155         }
156         fprintf (output, "\n");
157 }
158
159 void
160 dump_table_param (MonoImage *m)
161 {
162         MonoTableInfo *t = &m->tables [MONO_TABLE_PARAM];
163         int i;
164
165         fprintf (output, "Param Table\n");
166         
167         for (i = 0; i < t->rows; i++){
168                 guint32 cols [MONO_PARAM_SIZE];
169
170                 mono_metadata_decode_row (t, i, cols, MONO_PARAM_SIZE);
171                 fprintf (output, "%d: 0x%04x %d %s\n",
172                          i + 1,
173                          cols [MONO_PARAM_FLAGS], cols [MONO_PARAM_SEQUENCE], 
174                          mono_metadata_string_heap (m, cols [MONO_PARAM_NAME]));
175         }
176         fprintf (output, "\n");
177 }
178
179 void
180 dump_table_field (MonoImage *m)
181 {
182         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
183         MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
184         MonoTableInfo *fl = &m->tables [MONO_TABLE_FIELDLAYOUT];
185         MonoTableInfo *rva = &m->tables [MONO_TABLE_FIELDRVA];
186         int i, current_type, offset_row, rva_row;
187         guint32 first_m, last_m;
188
189         fprintf (output, "Field Table (1..%d)\n", t->rows);
190         
191         rva_row = offset_row = current_type = 1;
192         last_m = first_m = 1;
193         for (i = 1; i <= t->rows; i++){
194                 guint32 cols [MONO_FIELD_SIZE];
195                 char *sig, *flags;
196
197                 /*
198                  * Find the next type.
199                  */
200                 while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_FIELD_LIST))) {
201                         current_type++;
202                 }
203                 if (i == first_m) {
204                         fprintf (output, "########## %s.%s\n",
205                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
206                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
207                         first_m = last_m;
208                 }
209                 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_SIZE);
210                 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE], NULL);
211                 flags = field_flags (cols [MONO_FIELD_FLAGS]);
212                 fprintf (output, "%d: %s %s: %s\n",
213                          i,
214                          sig,
215                          mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]),
216                          flags);
217                 g_free (sig);
218                 g_free (flags);
219                 if (offset_row <= fl->rows && (mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_FIELD) == i)) {
220                         fprintf (output, "\texplicit offset: %d\n", mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_OFFSET));
221                         offset_row ++;
222                 }
223                 if (rva_row <= rva->rows && (mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_FIELD) == i)) {
224                         fprintf (output, "\trva: %d\n", mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_RVA));
225                         rva_row ++;
226                 }
227         }
228         fprintf (output, "\n");
229 }
230
231 void
232 dump_table_memberref (MonoImage *m)
233 {
234         MonoTableInfo *t = &m->tables [MONO_TABLE_MEMBERREF];
235         int i, kind, idx;
236         char *x, *xx;
237         char *sig;
238         const char *blob, *ks = NULL;
239
240         fprintf (output, "MemberRef Table (1..%d)\n", t->rows);
241
242         for (i = 0; i < t->rows; i++){
243                 guint32 cols [MONO_MEMBERREF_SIZE];
244
245                 mono_metadata_decode_row (t, i, cols, MONO_MEMBERREF_SIZE);
246                 
247                 kind = cols [MONO_MEMBERREF_CLASS] & 7;
248                 idx = cols [MONO_MEMBERREF_CLASS] >> 3;
249
250                 x = g_strdup ("UNHANDLED CASE");
251                 
252                 switch (kind){
253                 case 0:
254                         ks = "TypeDef";
255                         xx = get_typedef (m, idx);
256                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
257                         g_free (xx);
258                         break;
259                 case 1:
260                         ks = "TypeRef";
261                         xx = get_typeref (m, idx);
262                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
263                         g_free (xx);
264                         break;
265                 case 2:
266                         ks = "ModuleRef"; break;
267                 case 3:
268                         ks = "MethodDef";
269                         x = get_methoddef (m, idx);
270                         break;
271                 case 4:
272                         ks = "TypeSpec";
273                         xx = get_typespec (m, idx, FALSE, NULL);
274                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
275                         g_free (xx);
276                         break;
277                 default:
278                         g_error ("Unknown tag: %d\n", kind);
279                 }
280                 blob = mono_metadata_blob_heap (m, cols [MONO_MEMBERREF_SIGNATURE]);
281                 mono_metadata_decode_blob_size (blob, &blob);
282                 if (*blob == 0x6) { /* it's a field */
283                         sig = get_field_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
284                 } else {
285                         sig = get_methodref_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
286                 }
287                 fprintf (output, "%d: %s[%d] %s\n\tResolved: %s\n\tSignature: %s\n\t\n",
288                          i + 1,
289                          ks, idx,
290                          mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]),
291                          x ? x : "",
292                          sig);
293
294                 if (x)
295                         g_free (x);
296                 g_free (sig);
297         }
298 }
299
300 void
301 dump_table_class_layout (MonoImage *m)
302 {
303         MonoTableInfo *t = &m->tables [MONO_TABLE_CLASSLAYOUT];
304         int i;
305         fprintf (output, "ClassLayout Table (1..%d)\n", t->rows);
306
307         for (i = 0; i < t->rows; i++){
308                 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
309                 
310                 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
311
312                 fprintf (output, "%d: PackingSize=%d  ClassSize=%d  Parent=%s\n",
313                          i + 1, cols [MONO_CLASS_LAYOUT_PACKING_SIZE], 
314                          cols [MONO_CLASS_LAYOUT_CLASS_SIZE], 
315                          get_typedef (m, cols [MONO_CLASS_LAYOUT_PARENT]));
316         }
317 }
318
319 void
320 dump_table_constant (MonoImage *m)
321 {
322         MonoTableInfo *t = &m->tables [MONO_TABLE_CONSTANT];
323         int i;
324         const char *desc [] = {
325                 "Field",
326                 "Param",
327                 "Property",
328                 ""
329         };
330         fprintf (output, "Constant Table (1..%d)\n", t->rows);
331
332         for (i = 0; i < t->rows; i++){
333                 guint32 cols [MONO_CONSTANT_SIZE];
334                 const char *parent;
335                 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
336                 parent = desc [cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK];
337
338                 fprintf (output, "%d: Parent= %s: %d %s\n",
339                          i + 1, parent, cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS, 
340                          get_constant (m, (MonoTypeEnum) cols [MONO_CONSTANT_TYPE], cols [MONO_CONSTANT_VALUE]));
341         }
342         
343 }
344
345 void
346 dump_table_property_map (MonoImage *m)
347 {
348         MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTYMAP];
349         int i;
350         char *s;
351         
352         fprintf (output, "Property Map Table (1..%d)\n", t->rows);
353         
354         for (i = 0; i < t->rows; i++){
355                 guint32 cols [MONO_PROPERTY_MAP_SIZE];
356                 
357                 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_MAP_SIZE);
358                 s = get_typedef (m, cols [MONO_PROPERTY_MAP_PARENT]);
359                 fprintf (output, "%d: %s (%d) %d\n", i + 1, s, cols [MONO_PROPERTY_MAP_PARENT], cols [MONO_PROPERTY_MAP_PROPERTY_LIST]);
360                 g_free (s);
361         }
362 }
363
364 void
365 dump_table_property (MonoImage *m)
366 {
367         MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTY];
368         int i, j, pcount;
369         const char *ptr;
370         char flags[128];
371
372         fprintf (output, "Property Table (1..%d)\n", t->rows);
373
374         for (i = 0; i < t->rows; i++){
375                 guint32 cols [MONO_PROPERTY_SIZE];
376                 char *type;
377                 int bsize;
378                 int prop_flags;
379                 
380                 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_SIZE);
381                 flags [0] = 0;
382                 prop_flags = cols [MONO_PROPERTY_FLAGS];
383                 if (prop_flags & 0x0200)
384                         strcat (flags, "special ");
385                 if (prop_flags & 0x0400)
386                         strcat (flags, "runtime ");
387                 if (prop_flags & 0x1000)
388                         strcat (flags, "hasdefault ");
389
390                 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
391                 bsize = mono_metadata_decode_blob_size (ptr, &ptr);
392                 /* ECMA claims 0x08 ... */
393                 if (*ptr != 0x28 && *ptr != 0x08)
394                         g_warning("incorrect signature in propert blob: 0x%x", *ptr);
395                 ptr++;
396                 pcount = mono_metadata_decode_value (ptr, &ptr);
397                 ptr = get_type (m, ptr, &type, FALSE, NULL);
398                 fprintf (output, "%d: %s %s (",
399                          i + 1, type, mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]));
400                 g_free (type);
401
402                 for (j = 0; j < pcount; j++){
403                         ptr = get_param (m, ptr, &type, NULL);
404                         fprintf (output, "%s%s", j > 0? ", " : "",type);
405                         g_free (type);
406                 }
407                 fprintf (output, ") %s\n", flags);
408         }
409 }
410
411 void
412 dump_table_event (MonoImage *m)
413 {
414         MonoTableInfo *t = &m->tables [MONO_TABLE_EVENT];
415         int i;
416         fprintf (output, "Event Table (1..%d)\n", t->rows);
417
418         for (i = 0; i < t->rows; i++){
419                 guint32 cols [MONO_EVENT_SIZE];
420                 const char *name;
421                 char *type;
422                 
423                 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
424
425                 name = mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]);
426                 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE], NULL);
427                 fprintf (output, "%d: %s %s %s\n", i + 1, type, name,
428                          cols [MONO_EVENT_FLAGS] & 0x200 ? "specialname " : "");
429                 g_free (type);
430         }
431         
432 }
433
434 void
435 dump_table_file (MonoImage *m)
436 {
437         MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
438         int i, j, len;
439         fprintf (output, "File Table (1..%d)\n", t->rows);
440
441         for (i = 0; i < t->rows; i++){
442                 guint32 cols [MONO_FILE_SIZE];
443                 const char *name, *hash;
444                 
445                 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
446
447                 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
448                 fprintf (output, "%d: %s %s [", i + 1, name, 
449                                 cols [MONO_FILE_FLAGS] & 0x1 ? "nometadata" : "containsmetadata");
450                 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
451                 len = mono_metadata_decode_blob_size (hash, &hash);
452                 for (j = 0; j < len; ++j)
453                         fprintf (output, "%s%02X", j? " ": "", hash [j] & 0xff);
454                 fprintf (output, "]\n");
455         }
456         
457 }
458
459 static char*
460 get_manifest_implementation (MonoImage *m, guint32 idx)
461 {
462         guint32 row;
463         const char* table = "";
464         if (!idx)
465                 return g_strdup ("current module");
466         row = idx >> MONO_IMPLEMENTATION_BITS;
467         switch (idx & MONO_IMPLEMENTATION_MASK) {
468         case MONO_IMPLEMENTATION_FILE:
469                 table = "file";
470                 break;
471         case MONO_IMPLEMENTATION_ASSEMBLYREF:
472                 table = "assemblyref";
473                 break;
474         case MONO_IMPLEMENTATION_EXP_TYPE:
475                 table = "exportedtype";
476                 break;
477         default:
478                 g_assert_not_reached ();
479         }
480         return g_strdup_printf ("%s %d", table, row);
481 }
482
483 static const char*
484 get_manifest_flags (guint32 mf)
485 {
486         mf &= 3;
487         switch (mf) {
488         case 1: return "public";
489         case 2: return "private";
490         default:
491                 return "";
492         }
493 }
494
495 void
496 dump_table_manifest (MonoImage *m)
497 {
498         MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
499         int i;
500         fprintf (output, "Manifestresource Table (1..%d)\n", t->rows);
501
502         for (i = 0; i < t->rows; i++){
503                 guint32 cols [MONO_MANIFEST_SIZE];
504                 const char *name, *mf;
505                 char *impl;
506                 
507                 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
508
509                 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
510                 mf = get_manifest_flags (cols [MONO_MANIFEST_FLAGS]);
511                 impl = get_manifest_implementation (m, cols [MONO_MANIFEST_IMPLEMENTATION]);
512                 fprintf (output, "%d: %s '%s' at offset %u in %s\n", i + 1, mf, name, cols [MONO_MANIFEST_OFFSET], impl);
513                 g_free (impl);
514         }
515         
516 }
517
518 void
519 dump_table_moduleref (MonoImage *m)
520 {
521         MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
522         int i;
523         fprintf (output, "ModuleRef Table (1..%d)\n", t->rows);
524
525         for (i = 0; i < t->rows; i++){
526                 guint32 cols [MONO_MODULEREF_SIZE];
527                 const char *name;
528                 
529                 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
530
531                 name = mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]);
532                 fprintf (output, "%d: %s\n", i + 1, name);
533         }
534         
535 }
536
537 void
538 dump_table_module (MonoImage *m)
539 {
540         MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
541         int i;
542         fprintf (output, "Module Table (1..%d)\n", t->rows);
543
544         for (i = 0; i < t->rows; i++){
545                 guint32 cols [MONO_MODULE_SIZE];
546                 const char *name;
547                 char *guid;
548                 
549                 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
550
551                 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
552                 guid = get_guid (m, cols [MONO_MODULE_MVID]);
553                 fprintf (output, "%d: %s %d %s\n", i + 1, name, cols [MONO_MODULE_MVID], guid);
554         }       
555 }
556
557 void
558 dump_table_method (MonoImage *m)
559 {
560         MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
561         MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
562         int i, current_type;
563         guint32 first_m, last_m;
564         /* Generic container for Type & method */
565         MonoGenericContainer *type_container = NULL, *method_container = NULL;
566
567         fprintf (output, "Method Table (1..%d)\n", t->rows);
568
569         current_type = 1;
570         last_m = first_m = 1;
571         for (i = 1; i <= t->rows; i++){
572                 guint32 cols [MONO_METHOD_SIZE];
573                 char *sig, *impl_flags;
574                 const char *sigblob;
575                 MonoMethodSignature *method;
576
577                 /*
578                  * Find the next type.
579                  */
580                 while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_METHOD_LIST))) {
581                         current_type++;
582                 }
583                 if (i == first_m) {
584                         fprintf (output, "########## %s.%s\n",
585                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
586                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
587                         first_m = last_m;
588                         type_container = mono_metadata_load_generic_params (m, MONO_TOKEN_TYPE_DEF | (current_type - 1), NULL);
589                         if (type_container)
590                                 mono_metadata_load_generic_param_constraints (m, MONO_TOKEN_TYPE_DEF | (current_type - 1), type_container);
591                 }
592
593                 method_container = mono_metadata_load_generic_params (m, MONO_TOKEN_METHOD_DEF | i, type_container);
594                 if (method_container)
595                         mono_metadata_load_generic_param_constraints (m, MONO_TOKEN_METHOD_DEF | i, method_container);
596                 mono_metadata_decode_table_row (m, MONO_TABLE_METHOD, i - 1, cols, MONO_METHOD_SIZE);
597                 sigblob = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
598                 mono_metadata_decode_blob_size (sigblob, &sigblob);
599                 method = mono_metadata_parse_method_signature_full (m, method_container ? method_container : type_container, i, sigblob, &sigblob);
600                 sig = dis_stringify_method_signature (m, method, i, method_container ? method_container : type_container, FALSE);
601                 impl_flags = get_method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
602                 fprintf (output, "%d: %s (param: %d impl_flags: %s)\n", i, sig, cols [MONO_METHOD_PARAMLIST], impl_flags);
603                 g_free (sig);
604                 g_free (impl_flags);
605                 mono_metadata_free_method_signature (method);
606         }
607         
608 }
609
610 void
611 dump_table_implmap (MonoImage *m)
612 {
613         MonoTableInfo *t = &m->tables [MONO_TABLE_IMPLMAP];
614         MonoTableInfo *td = &m->tables [MONO_TABLE_MODULEREF];
615         int i;
616
617         fprintf (output, "ImplMap Table (1..%d)\n", t->rows);
618
619         for (i = 1; i <= t->rows; i++){
620                 guint32 cols [MONO_IMPLMAP_SIZE];
621                 char *method;
622
623                 mono_metadata_decode_row (t, i - 1, cols, MONO_IMPLMAP_SIZE);
624
625                 method = get_method (m, MONO_TOKEN_METHOD_DEF | (cols [MONO_IMPLMAP_MEMBER] >> MONO_MEMBERFORWD_BITS), NULL);
626                 
627                 fprintf (output, "%d: %s %d (%s %s)\n", i, 
628                                  method,
629                                  cols [MONO_IMPLMAP_FLAGS], 
630                                  mono_metadata_string_heap (m, cols [MONO_IMPLMAP_NAME]),
631                                  mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME)));
632         }
633 }
634
635 void
636 dump_table_fieldrva  (MonoImage *m)
637 {
638         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
639         int i;
640
641         fprintf (output, "FieldRVA Table (1..%d)\n", t->rows);
642
643         for (i = 1; i <= t->rows; i++){
644                 guint32 cols [MONO_FIELD_RVA_SIZE];
645
646                 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_RVA_SIZE);
647                 fprintf (output, "%d: Field %d: %x\n", i, cols [MONO_FIELD_RVA_FIELD], cols [MONO_FIELD_RVA_RVA]);
648         }
649 }
650
651 void
652 dump_table_methodimpl (MonoImage *m)
653 {
654         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODIMPL];
655         /*MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];*/
656         int i;
657
658         fprintf (output, "MethodImpl Table (1..%d)\n", t->rows);
659
660         for (i = 1; i <= t->rows; i++){
661                 guint32 cols [MONO_METHODIMPL_SIZE];
662                 char *class, *impl, *decl;
663
664                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHODIMPL_SIZE);
665                 class = get_typedef (m, cols [MONO_METHODIMPL_CLASS]);
666                 impl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_BODY]), NULL);
667                 decl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_DECLARATION]), NULL);
668                 fprintf (output, "%d: %s\n\tdecl: %s\n\timpl: %s\n", i, class, decl, impl);
669                 g_free (class);
670                 g_free (impl);
671                 g_free (decl);
672         }
673         
674 }
675
676 static dis_map_t semantics_map [] = {
677                 {1, "setter"},
678                 {2, "getter"},
679                 {4, "other"},
680                 {8, "add-on"},
681                 {0x10, "remove-on"},
682                 {0x20, "fire"},
683                 {0, NULL},
684 };
685
686 void
687 dump_table_methodsem (MonoImage *m)
688 {
689         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSEMANTICS];
690         int i, is_property, index;
691         const char *semantics;
692         
693         fprintf (output, "Method Semantics Table (1..%d)\n", t->rows);
694         for (i = 1; i <= t->rows; i++){
695                 guint32 cols [MONO_METHOD_SEMA_SIZE];
696                 
697                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHOD_SEMA_SIZE);
698                 semantics = flags (cols [MONO_METHOD_SEMA_SEMANTICS], semantics_map);
699                 is_property = cols [MONO_METHOD_SEMA_ASSOCIATION] & MONO_HAS_SEMANTICS_MASK;
700                 index = cols [MONO_METHOD_SEMA_ASSOCIATION] >> MONO_HAS_SEMANTICS_BITS;
701                 fprintf (output, "%d: [%d] %s method: %d %s %d\n", i, cols [MONO_METHOD_SEMA_ASSOCIATION], semantics,
702                                                 cols [MONO_METHOD_SEMA_METHOD] - 1, 
703                                                 is_property? "property" : "event",
704                                                 index);
705         }
706 }
707
708 void 
709 dump_table_interfaceimpl (MonoImage *m)
710 {
711         MonoTableInfo *t = &m->tables [MONO_TABLE_INTERFACEIMPL];
712         int i;
713
714         fprintf (output, "Interface Implementation Table (1..%d)\n", t->rows);
715         for (i = 1; i <= t->rows; i++) {
716                 guint32 cols [MONO_INTERFACEIMPL_SIZE];
717                 
718                 mono_metadata_decode_row (t, i - 1, cols, MONO_INTERFACEIMPL_SIZE);
719                 fprintf (output, "%d: %s implements %s\n", i,
720                          get_typedef (m, cols [MONO_INTERFACEIMPL_CLASS]),
721                          get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE], NULL));
722         }
723 }
724
725 static char*
726 has_cattr_get_table (MonoImage *m, guint32 val)
727 {
728         guint32 t = val & MONO_CUSTOM_ATTR_MASK;
729         guint32 index = val >> MONO_CUSTOM_ATTR_BITS;
730         const char *table;
731
732         switch (t) {
733         case MONO_CUSTOM_ATTR_METHODDEF:
734                 table = "MethodDef";
735                 break;
736         case MONO_CUSTOM_ATTR_FIELDDEF:
737                 table = "FieldDef";
738                 break;
739         case MONO_CUSTOM_ATTR_TYPEREF:
740                 table = "TypeRef";
741                 break;
742         case MONO_CUSTOM_ATTR_TYPEDEF:
743                 table = "TypeDef";
744                 break;
745         case MONO_CUSTOM_ATTR_PARAMDEF:
746                 table = "Param";
747                 break;
748         case MONO_CUSTOM_ATTR_INTERFACE:
749                 table = "InterfaceImpl";
750                 break;
751         case MONO_CUSTOM_ATTR_MEMBERREF:
752                 table = "MemberRef";
753                 break;
754         case MONO_CUSTOM_ATTR_MODULE:
755                 table = "Module";
756                 break;
757         case MONO_CUSTOM_ATTR_PERMISSION:
758                 table = "DeclSecurity?";
759                 break;
760         case MONO_CUSTOM_ATTR_PROPERTY:
761                 table = "Property";
762                 break;
763         case MONO_CUSTOM_ATTR_EVENT:
764                 table = "Event";
765                 break;
766         case MONO_CUSTOM_ATTR_SIGNATURE:
767                 table = "StandAloneSignature";
768                 break;
769         case MONO_CUSTOM_ATTR_MODULEREF:
770                 table = "ModuleRef";
771                 break;
772         case MONO_CUSTOM_ATTR_TYPESPEC:
773                 table = "TypeSpec";
774                 break;
775         case MONO_CUSTOM_ATTR_ASSEMBLY:
776                 table = "Assembly";
777                 break;
778         case MONO_CUSTOM_ATTR_ASSEMBLYREF:
779                 table = "AssemblyRef";
780                 break;
781         case MONO_CUSTOM_ATTR_FILE:
782                 table = "File";
783                 break;
784         case MONO_CUSTOM_ATTR_EXP_TYPE:
785                 table = "ExportedType";
786                 break;
787         case MONO_CUSTOM_ATTR_MANIFEST:
788                 table = "Manifest";
789                 break;
790         case MONO_CUSTOM_ATTR_GENERICPAR:
791                 table = "GenericParam";
792                 break;
793         default:
794                 table = "Unknown";
795                 break;
796         }
797         /*
798          * FIXME: we should decode the index into something more uman-friendly.
799          */
800         return g_strdup_printf ("%s: %d", table, index);
801 }
802
803 static char*
804 custom_attr_params (MonoImage *m, MonoMethodSignature* sig, const char* value)
805 {
806         int len, i, slen, type;
807         GString *res;
808         char *s;
809         const char *p = value;
810
811         len = mono_metadata_decode_value (p, &p);
812         if (len < 2 || read16 (p) != 0x0001) /* Prolog */
813                 return g_strdup ("");
814
815         /* skip prolog */
816         p += 2;
817         res = g_string_new ("");
818         for (i = 0; i < sig->param_count; ++i) {
819                 if (i != 0)
820                         g_string_append (res, ", ");
821                 type = sig->params [i]->type;
822 handle_enum:
823                 switch (type) {
824                 case MONO_TYPE_U1:
825                         g_string_append_printf (res, "%d", (unsigned int)*p);
826                         ++p;
827                         break;
828                 case MONO_TYPE_I1:
829                         g_string_append_printf (res, "%d", *p);
830                         ++p;
831                         break;
832                 case MONO_TYPE_BOOLEAN:
833                         g_string_append_printf (res, "%s", *p?"true":"false");
834                         ++p;
835                         break;
836                 case MONO_TYPE_CHAR:
837                         g_string_append_printf (res, "'%c'", read16 (p));
838                         p += 2;
839                         break;
840                 case MONO_TYPE_U2:
841                         g_string_append_printf (res, "%d", read16 (p));
842                         p += 2;
843                         break;
844                 case MONO_TYPE_I2:
845                         g_string_append_printf (res, "%d", (gint16)read16 (p));
846                         p += 2;
847                         break;
848                 case MONO_TYPE_U4:
849                         g_string_append_printf (res, "%d", read32 (p));
850                         p += 4;
851                         break;
852                 case MONO_TYPE_I4:
853                         g_string_append_printf (res, "%d", (gint32)read32 (p));
854                         p += 4;
855                         break;
856                 case MONO_TYPE_U8:
857                         g_string_append_printf (res, "%lld", (long long)read64 (p));
858                         p += 8;
859                         break;
860                 case MONO_TYPE_I8:
861                         g_string_append_printf (res, "%lld", (long long)read64 (p));
862                         p += 8;
863                         break;
864                 case MONO_TYPE_R4: {
865                         float val;
866                         int inf;
867                         readr4 (p, &val);
868                         inf = isinf (val);
869                         if (inf == -1) 
870                                 g_string_append_printf (res, "(00 00 80 ff)"); /* negative infinity */
871                         else if (inf == 1)
872                                 g_string_append_printf (res, "(00 00 80 7f)"); /* positive infinity */
873                         else if (isnan (val))
874                                 g_string_append_printf (res, "(00 00 c0 ff)"); /* NaN */
875                         else
876                                 g_string_append_printf (res, "%g", val);
877                         p += 4;
878                         break;
879                 }
880                 case MONO_TYPE_R8: {
881                         double val;
882                         int inf;
883                         
884                         readr8 (p, &val);
885                         inf = isinf (val);
886                         if (inf == -1) 
887                                 g_string_append_printf (res, "(00 00 00 00 00 00 f0 ff)"); /* negative infinity */
888                         else if (inf == 1)
889                                 g_string_append_printf (res, "(00 00 00 00 00 00 f0 7f)"); /* positive infinity */
890                         else if (isnan (val))
891                                 g_string_append_printf (res, "(00 00 00 00 00 00 f8 ff)"); /* NaN */
892                         else
893                                 g_string_append_printf (res, "%g", val);
894                         p += 8;
895                         break;
896                 }
897                 case MONO_TYPE_VALUETYPE:
898                         if (mono_class_is_enum (sig->params [i]->data.klass)) {
899                                 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
900                                 goto handle_enum;
901                         } else {
902                                 g_warning ("generic valutype not handled in custom attr value decoding");
903                         }
904                         break;
905                 case MONO_TYPE_CLASS: /* It must be a Type: check? */
906                 case MONO_TYPE_STRING:
907                         if (*p == (char)0xff) {
908                                 g_string_append (res, "null");
909                                 p++;
910                                 break;
911                         }
912                         slen = mono_metadata_decode_value (p, &p);
913                         g_string_append_c (res, '"');
914                         g_string_append (res, p);
915                         g_string_append_c (res, '"');
916                         p += slen;
917                         break;
918                 default:
919                         g_warning ("Type %02x not handled in custom attr value decoding", sig->params [i]->type);
920                         break;
921                 }
922         }
923         slen = read16 (p);
924         if (slen) {
925                 g_string_append_printf (res, " %d named args: (", slen);
926                 slen = len - (p - value) + 1;
927                 for (i = 0; i < slen; ++i) {
928                         g_string_append_printf (res, " %02X", (p [i] & 0xff));
929                 }
930                 g_string_append_c (res, ')');
931         }
932         s = res->str;
933         g_string_free (res, FALSE);
934         return s;
935 }
936
937 void
938 dump_table_customattr (MonoImage *m)
939 {
940         MonoTableInfo *t = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
941         int i;
942
943         fprintf (output, "Custom Attributes Table (1..%d)\n", t->rows);
944         for (i = 1; i <= t->rows; i++) {
945                 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
946                 guint32 mtoken;
947                 char * desc;
948                 char *method;
949                 char *params;
950                 MonoMethod *meth;
951                 
952                 mono_metadata_decode_row (t, i - 1, cols, MONO_CUSTOM_ATTR_SIZE);
953                 desc = has_cattr_get_table (m, cols [MONO_CUSTOM_ATTR_PARENT]);
954                 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> MONO_CUSTOM_ATTR_TYPE_BITS;
955                 switch (cols [MONO_CUSTOM_ATTR_TYPE] & MONO_CUSTOM_ATTR_TYPE_MASK) {
956                 case MONO_CUSTOM_ATTR_TYPE_METHODDEF:
957                         mtoken |= MONO_TOKEN_METHOD_DEF;
958                         break;
959                 case MONO_CUSTOM_ATTR_TYPE_MEMBERREF:
960                         mtoken |= MONO_TOKEN_MEMBER_REF;
961                         break;
962                 default:
963                         g_warning ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
964                         break;
965                 }
966                 method = get_method (m, mtoken, NULL);
967                 meth = mono_get_method (m, mtoken, NULL);
968                 params = custom_attr_params (m, mono_method_signature (meth), mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]));
969                 fprintf (output, "%d: %s: %s [%s]\n", i, desc, method, params);
970                 g_free (desc);
971                 g_free (method);
972                 g_free (params);
973         }
974 }
975
976 void
977 dump_table_nestedclass (MonoImage *m)
978 {
979         MonoTableInfo *t = &m->tables [MONO_TABLE_NESTEDCLASS];
980         guint32 cols [MONO_NESTED_CLASS_SIZE];
981         int i;
982         char *nested, *nesting;
983         fprintf (output, "NestedClass Table (1..%d)\n", t->rows);
984
985         for (i = 1; i <= t->rows; i++){
986                 mono_metadata_decode_row (t, i - 1, cols, MONO_NESTED_CLASS_SIZE);
987                 nested = get_typedef (m, cols [MONO_NESTED_CLASS_NESTED]);
988                 nesting = get_typedef (m, cols [MONO_NESTED_CLASS_ENCLOSING]);
989                 fprintf (output, "%d: %d %d: %s in %s\n", i,
990                                 cols [MONO_NESTED_CLASS_NESTED], 
991                                 cols [MONO_NESTED_CLASS_ENCLOSING], nested, nesting);
992                 g_free (nested);
993                 g_free (nesting);
994         }
995         
996 }
997
998 void
999 dump_table_exported (MonoImage *m)
1000 {
1001         MonoTableInfo *t = &m->tables [MONO_TABLE_EXPORTEDTYPE];
1002         guint32 cols [MONO_EXP_TYPE_SIZE];
1003         int i;
1004         const char *name, *nspace;
1005         char *impl;
1006         guint32 index, flags;
1007         fprintf (output, "ExportedType Table (1..%d)\n", t->rows);
1008
1009         for (i = 1; i <= t->rows; i++) {
1010                 mono_metadata_decode_row (t, i - 1, cols, MONO_EXP_TYPE_SIZE);
1011                 name = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAME]);
1012                 nspace = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAMESPACE]);
1013                 impl = get_manifest_implementation (m, cols [MONO_EXP_TYPE_IMPLEMENTATION]);
1014                 index = cols [MONO_EXP_TYPE_TYPEDEF];
1015                 flags = cols [MONO_EXP_TYPE_FLAGS];
1016                 fprintf (output, "%d: %s%s%s is in %s, index=%x, flags=0x%x\n", i, nspace, *nspace ? "." : "", name, impl, index, flags);
1017                 g_free (impl);
1018         }
1019         
1020 }
1021
1022 static void
1023 dump_blob (MonoImage *m, const char* blob)
1024 {
1025         int j, bsize;
1026
1027         bsize = mono_metadata_decode_blob_size (blob, &blob);
1028
1029         for (j = 0; j < bsize; j++) {
1030                 fprintf (output, "%02x ", blob [j] & 0xff);
1031         }
1032 }
1033
1034 void
1035 dump_table_field_marshal (MonoImage *m)
1036 {
1037         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDMARSHAL];
1038         guint32 cols [MONO_FIELD_MARSHAL_SIZE];
1039         int i, is_field, idx;
1040         const char *blob;
1041         char *native;
1042         
1043         fprintf (output, "FieldMarshal Table (1..%d)\n", t->rows);
1044
1045         for (i = 1; i <= t->rows; i++) {
1046                 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_MARSHAL_SIZE);
1047                 blob = mono_metadata_blob_heap (m, cols [MONO_FIELD_MARSHAL_NATIVE_TYPE]);
1048                 native = get_marshal_info (m, blob);
1049                 is_field = (cols [MONO_FIELD_MARSHAL_PARENT] & MONO_HAS_FIELD_MARSHAL_MASK) == MONO_HAS_FIELD_MARSHAL_FIELDSREF;
1050                 idx = cols [MONO_FIELD_MARSHAL_PARENT] >> MONO_HAS_FIELD_MARSHAL_BITS;
1051                 fprintf (output, "%d: (0x%04x) %s %d: %s\n", i, cols [MONO_FIELD_MARSHAL_PARENT], is_field? "Field" : "Param", idx, native);
1052                 fprintf (output, "\tblob encoding: ");
1053                 dump_blob (m, blob);
1054                 fprintf (output, "\n");
1055                 g_free (native);
1056         }
1057         
1058 }
1059
1060 static const char*
1061 get_security_action (int val) {
1062         static char buf [32];
1063
1064         switch (val) {
1065         case SECURITY_ACTION_DEMAND:
1066                 return "Demand";
1067         case SECURITY_ACTION_ASSERT:
1068                 return "Assert";
1069         case SECURITY_ACTION_DENY:
1070                 return "Deny";
1071         case SECURITY_ACTION_PERMITONLY:
1072                 return "PermitOnly";
1073         case SECURITY_ACTION_LINKDEMAND:
1074                 return "LinkDemand";
1075         case SECURITY_ACTION_INHERITDEMAND:
1076                 return "InheritanceDemand";
1077         case SECURITY_ACTION_REQMIN:
1078                 return "RequestMinimum";
1079         case SECURITY_ACTION_REQOPT:
1080                 return "RequestOptional";
1081         case SECURITY_ACTION_REQREFUSE:
1082                 return "RequestRefuse";
1083         /* Special actions (for non CAS permissions) */
1084         case SECURITY_ACTION_NONCASDEMAND:
1085                 return "NonCasDemand";
1086         case SECURITY_ACTION_NONCASLINKDEMAND:
1087                 return "NonCasLinkDemand";
1088         case SECURITY_ACTION_NONCASINHERITANCE:
1089                 return "NonCasInheritance";
1090         /* Fx 2.0 actions (for both CAS and non-CAS permissions) */
1091         case SECURITY_ACTION_LINKDEMANDCHOICE:
1092                 return "LinkDemandChoice";
1093         case SECURITY_ACTION_INHERITDEMANDCHOICE:
1094                 return "InheritanceDemandChoice";
1095         case SECURITY_ACTION_DEMANDCHOICE:
1096                 return "DemandChoice";
1097         default:
1098                 g_snprintf (buf, sizeof (buf), "0x%04X", val);
1099                 return buf;
1100         }
1101 }
1102
1103 void 
1104 dump_table_declsec (MonoImage *m)
1105 {
1106         MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
1107         guint32 cols [MONO_DECL_SECURITY_SIZE];
1108         int i, len;
1109         guint32 idx;
1110         const char *blob, *action;
1111         const char* parent[] = {
1112                 "TypeDef", "MethodDef", "Assembly", ""
1113         };
1114         
1115         fprintf (output, "DeclSecurity Table (1..%d)\n", t->rows);
1116
1117         for (i = 1; i <= t->rows; i++) {
1118                 mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
1119                 blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
1120                 len = mono_metadata_decode_blob_size (blob, &blob);
1121                 action = get_security_action (cols [MONO_DECL_SECURITY_ACTION]);
1122                 idx = cols [MONO_DECL_SECURITY_PARENT];
1123                 fprintf (output, "%d: %s on %s %d%s", i, action, parent [idx & MONO_HAS_DECL_SECURITY_MASK], idx >> MONO_HAS_DECL_SECURITY_BITS, len? ":\n\t":"\n");
1124                 if (!len)
1125                         continue;
1126                 if (blob [0] == MONO_DECLSEC_FORMAT_20) {
1127                         /* 2.0 declarative security format */
1128                         char *declsec = dump_declsec_entry20 (m, blob, "\t");
1129                         fprintf (output, "%s", declsec);
1130                         g_free (declsec);
1131                 } else {
1132                         /* 1.0 declarative security format - Unicode XML */
1133                         for (idx = 0; idx < len; ++idx)
1134                                 fprintf (output, "%c", blob [idx]);
1135                 }
1136                 fprintf (output, "\n");
1137         }
1138 }
1139
1140 void 
1141 dump_table_genericpar (MonoImage *m)
1142 {
1143         MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAM];
1144         guint32 cols [MONO_GENERICPARAM_SIZE];
1145         int i;
1146
1147         fprintf (output, "GenericParameters (1..%d)\n", t->rows);
1148         
1149         for (i = 1; i <= t->rows; i++) {
1150                 char *sig;
1151                 mono_metadata_decode_row (t, i - 1, cols, MONO_GENERICPARAM_SIZE);
1152
1153                 // sig = get_type_or_methdef (m, cols [MONO_GENERICPARAM_OWNER]);
1154                 sig = g_strdup_printf ("%x", cols [MONO_GENERICPARAM_OWNER]);
1155                 fprintf (output, "%d: %d, flags=%d, owner=%s %s\n", i,
1156                          cols [MONO_GENERICPARAM_NUMBER],
1157                          cols [MONO_GENERICPARAM_FLAGS], sig,
1158                          mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
1159                 g_free (sig);
1160         }
1161 }
1162
1163 void
1164 dump_table_methodspec (MonoImage *m)
1165 {
1166         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSPEC];
1167         guint32 cols [MONO_METHODSPEC_SIZE];
1168         int i;
1169         
1170         fprintf (output, "MethodSpec (1..%d)\n", t->rows);
1171
1172         for (i = 1; i <= t->rows; i++) {
1173                 char *sig;
1174                 char *method;
1175                 guint32 token;
1176                 
1177                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHODSPEC_SIZE);
1178
1179                 /* build a methodspec token to get the method */
1180                 token = MONO_TOKEN_METHOD_SPEC | i;
1181                 method = get_method (m, token, NULL);
1182                 
1183                 sig = get_method_type_param (m, cols [MONO_METHODSPEC_SIGNATURE], NULL);
1184                 fprintf (output, "%d: %s, %s\n", i, method, sig);
1185                 g_free (sig);
1186                 g_free (method);
1187         }
1188 }
1189
1190 void
1191 dump_table_parconstraint (MonoImage *m)
1192 {
1193         MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
1194         guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
1195         int i;
1196         
1197         fprintf (output, "Generic Param Constraint (1..%d)\n", t->rows);
1198
1199         for (i = 1; i <= t->rows; i++) {
1200                 char *sig;
1201                 mono_metadata_decode_row (t, i - 1, cols, MONO_GENPARCONSTRAINT_SIZE);
1202
1203                 // sig = get_typedef_or_ref (m, cols [MONO_GENPARCONSTRAINT_CONSTRAINT], NULL);
1204                 sig = g_strdup_printf ("%x", cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
1205                 fprintf (output, "%d: gen-par=%d, Constraint=%s\n", i,
1206                          cols [MONO_GENPARCONSTRAINT_GENERICPAR], sig);
1207                 g_free (sig);
1208         }
1209 }
1210
1211 void
1212 dump_stream_blob (MonoImage *m)
1213 {
1214         int i;
1215
1216         fprintf (output, "Blob heap contents\n");
1217
1218         for (i = 0; i < m->heap_blob.size; i++) {
1219                 if (i > 0) {
1220                         if ((i % 16) == 0)
1221                                 fprintf (output, "\n");
1222                         else if ((i % 8) == 0)
1223                                 fprintf (output, "- ");
1224                 }
1225                 fprintf (output, "%02x ", m->heap_blob.data [i] & 0xff);
1226         }
1227
1228         fprintf (output, "\n");
1229 }
1230
1231 void
1232 dump_stream_strings (MonoImage *m)
1233 {
1234         guint32 i;
1235
1236         fprintf (output, "Strings heap contents\n");
1237
1238         for (i = 0; i < m->heap_strings.size; ) {
1239                 const char *str = mono_metadata_string_heap (m, i);
1240                 fprintf (output, "%02x: \"%s\"\n", i, str);
1241                 i += strlen (str) + 1;
1242         }
1243 }
1244
1245 void
1246 dump_stream_us (MonoImage *m)
1247 {
1248         guint32 i;
1249
1250         fprintf (output, "User Strings heap contents\n");
1251
1252         for (i = 0; i < m->heap_us.size; ) {
1253                 const char *us_ptr = mono_metadata_user_string (m, i);
1254                 int len = mono_metadata_decode_blob_size (us_ptr, (const char**)&us_ptr);
1255
1256                 char *str = get_encoded_user_string_or_bytearray ((const guchar*)us_ptr, len);
1257                 fprintf (output, "%02x: %s\n", i, str);
1258                 g_free (str);
1259                 i += len + 1;
1260         }
1261 }
1262
1263 void
1264 dump_table_standalonesig (MonoImage *m)
1265 {
1266         MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1267         guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1268         int i;
1269         
1270         fprintf (output, "Stand alone signature (1..%d)\n", t->rows);
1271
1272         for (i = 1; i <= t->rows; i++) {
1273                 const char *locals_ptr;
1274                 int j, bsize;
1275
1276                 mono_metadata_decode_row (t, i - 1, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
1277
1278                 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1279                 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1280
1281                 fprintf (output, "%d: blob[0x%x] = ", i, cols [MONO_STAND_ALONE_SIGNATURE]);
1282
1283                 for (j = 0; j < bsize; j++) {
1284                         fprintf (output, "%02x ", locals_ptr [j] & 0xff);
1285                 }
1286                 fprintf (output, "\n");
1287         }
1288 }
1289
1290 static void
1291 dump_table_ptr (MonoImage *m, int table, const char *name)
1292 {
1293         MonoTableInfo *t = &m->tables [table];
1294         guint32 cols [1];
1295         int i;
1296         
1297         fprintf (output, "%s (1..%d)\n", name, t->rows);
1298
1299         for (i = 1; i <= t->rows; i++) {
1300                 mono_metadata_decode_row (t, i - 1, cols, 1);
1301
1302                 fprintf (output, "%d: %d\n", i, cols [0]);
1303         }
1304 }
1305
1306 void
1307 dump_table_methodptr (MonoImage *m)
1308 {
1309         dump_table_ptr (m, MONO_TABLE_METHOD_POINTER, "Method Ptr");
1310 }
1311
1312 void
1313 dump_table_fieldptr (MonoImage *m)
1314 {
1315         dump_table_ptr (m, MONO_TABLE_FIELD_POINTER, "Field Ptr");
1316 }
1317
1318 void
1319 dump_table_paramptr (MonoImage *m)
1320 {
1321         dump_table_ptr (m, MONO_TABLE_PARAM_POINTER, "Param Ptr");
1322 }
1323
1324 void
1325 dump_table_eventptr (MonoImage *m)
1326 {
1327         dump_table_ptr (m, MONO_TABLE_EVENT_POINTER, "Event Ptr");
1328 }
1329
1330 void
1331 dump_table_propertyptr (MonoImage *m)
1332 {
1333         dump_table_ptr (m, MONO_TABLE_PROPERTY_POINTER, "Property Ptr");
1334 }