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