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