2004-01-17 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"; break;
242                 case 1:
243                         ks = "TypeRef";
244                         xx = get_typeref (m, idx);
245                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
246                         g_free (xx);
247                         break;
248                 case 2:
249                         ks = "ModuleRef"; break;
250                 case 3:
251                         ks = "MethodDef"; break;
252                 case 4:
253                         ks = "TypeSpec";
254                         xx = get_typespec (m, idx);
255                         x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
256                         g_free (xx);
257                         break;
258                 default:
259                         g_error ("Unknown tag: %d\n", kind);
260                 }
261                 blob = mono_metadata_blob_heap (m, cols [MONO_MEMBERREF_SIGNATURE]);
262                 mono_metadata_decode_blob_size (blob, &blob);
263                 if (*blob == 0x6) { /* it's a field */
264                         sig = get_field_signature (m, cols [MONO_MEMBERREF_SIGNATURE]);
265                 } else {
266                         sig = get_methodref_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
267                 }
268                 fprintf (output, "%d: %s[%d] %s\n\tResolved: %s\n\tSignature: %s\n\t\n",
269                          i + 1,
270                          ks, idx,
271                          mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]),
272                          x ? x : "",
273                          sig);
274
275                 if (x)
276                         g_free (x);
277                 g_free (sig);
278         }
279 }
280
281 void
282 dump_table_class_layout (MonoImage *m)
283 {
284         MonoTableInfo *t = &m->tables [MONO_TABLE_CLASSLAYOUT];
285         int i;
286         fprintf (output, "ClassLayout Table (1..%d)\n", t->rows);
287
288         for (i = 0; i < t->rows; i++){
289                 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
290                 
291                 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
292
293                 fprintf (output, "%d: PackingSize=%d  ClassSize=%d  Parent=%s\n",
294                          i + 1, cols [MONO_CLASS_LAYOUT_PACKING_SIZE], 
295                          cols [MONO_CLASS_LAYOUT_CLASS_SIZE], 
296                          get_typedef (m, cols [MONO_CLASS_LAYOUT_PARENT]));
297         }
298 }
299
300 void
301 dump_table_constant (MonoImage *m)
302 {
303         MonoTableInfo *t = &m->tables [MONO_TABLE_CONSTANT];
304         int i;
305         const char *desc [] = {
306                 "Field",
307                 "Param",
308                 "Property",
309                 ""
310         };
311         fprintf (output, "Constant Table (1..%d)\n", t->rows);
312
313         for (i = 0; i < t->rows; i++){
314                 guint32 cols [MONO_CONSTANT_SIZE];
315                 const char *parent = desc [cols [MONO_CONSTANT_PARENT] & HASCONSTANT_MASK];
316                 
317                 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
318
319                 fprintf (output, "%d: Parent= %s: %d %s\n",
320                          i + 1, parent, cols [MONO_CONSTANT_PARENT] >> HASCONSTANT_BITS, 
321                          get_constant (m, (MonoTypeEnum) cols [MONO_CONSTANT_TYPE], cols [MONO_CONSTANT_VALUE]));
322         }
323         
324 }
325
326 void
327 dump_table_property_map (MonoImage *m)
328 {
329         MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTYMAP];
330         int i;
331         char *s;
332         
333         fprintf (output, "Property Map Table (1..%d)\n", t->rows);
334         
335         for (i = 0; i < t->rows; i++){
336                 guint32 cols [MONO_PROPERTY_MAP_SIZE];
337                 
338                 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_MAP_SIZE);
339                 s = get_typedef (m, cols [MONO_PROPERTY_MAP_PARENT]);
340                 fprintf (output, "%d: %s (%d) %d\n", i + 1, s, cols [MONO_PROPERTY_MAP_PARENT], cols [MONO_PROPERTY_MAP_PROPERTY_LIST]);
341                 g_free (s);
342         }
343 }
344
345 void
346 dump_table_property (MonoImage *m)
347 {
348         MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTY];
349         int i, j, pcount;
350         const char *ptr;
351         char flags[128];
352
353         fprintf (output, "Property Table (1..%d)\n", t->rows);
354
355         for (i = 0; i < t->rows; i++){
356                 guint32 cols [MONO_PROPERTY_SIZE];
357                 char *type;
358                 int bsize;
359                 int prop_flags;
360                 
361                 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_SIZE);
362                 flags [0] = 0;
363                 prop_flags = cols [MONO_PROPERTY_FLAGS];
364                 if (prop_flags & 0x0200)
365                         strcat (flags, "special ");
366                 if (prop_flags & 0x0400)
367                         strcat (flags, "runtime ");
368                 if (prop_flags & 0x1000)
369                         strcat (flags, "hasdefault ");
370
371                 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
372                 bsize = mono_metadata_decode_blob_size (ptr, &ptr);
373                 /* ECMA claims 0x08 ... */
374                 if (*ptr != 0x28 && *ptr != 0x08)
375                                 g_warning("incorrect signature in propert blob: 0x%x", *ptr);
376                 ptr++;
377                 pcount = mono_metadata_decode_value (ptr, &ptr);
378                 ptr = get_type (m, ptr, &type);
379                 fprintf (output, "%d: %s %s (",
380                          i + 1, type, mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]));
381                 g_free (type);
382
383                 for (j = 0; j < pcount; j++){
384                                 ptr = get_param (m, ptr, &type);
385                                 fprintf (output, "%s%s", j > 0? ", " : "",type);
386                                 g_free (type);
387                 }
388                 fprintf (output, ") %s\n", flags);
389         }
390 }
391
392 void
393 dump_table_event (MonoImage *m)
394 {
395         MonoTableInfo *t = &m->tables [MONO_TABLE_EVENT];
396         int i;
397         fprintf (output, "Event Table (1..%d)\n", t->rows);
398
399         for (i = 0; i < t->rows; i++){
400                 guint32 cols [MONO_EVENT_SIZE];
401                 const char *name;
402                 char *type;
403                 
404                 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
405
406                 name = mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]);
407                 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE]);
408                 fprintf (output, "%d: %s %s %s\n", i + 1, type, name,
409                          cols [MONO_EVENT_FLAGS] & 0x200 ? "specialname " : "");
410                 g_free (type);
411         }
412         
413 }
414
415 void
416 dump_table_file (MonoImage *m)
417 {
418         MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
419         int i, j, len;
420         fprintf (output, "File Table (1..%d)\n", t->rows);
421
422         for (i = 0; i < t->rows; i++){
423                 guint32 cols [MONO_FILE_SIZE];
424                 const char *name, *hash;
425                 
426                 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
427
428                 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
429                 fprintf (output, "%d: %s %s [", i + 1, name, 
430                                 cols [MONO_FILE_FLAGS] & 0x1 ? "nometadata" : "containsmetadata");
431                 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
432                 len = mono_metadata_decode_blob_size (hash, &hash);
433                 for (j = 0; j < len; ++j)
434                         fprintf (output, "%s%02X", j? " ": "", hash [j] & 0xff);
435                 fprintf (output, "]\n");
436         }
437         
438 }
439
440 static char*
441 get_manifest_implementation (MonoImage *m, guint32 idx)
442 {
443         guint32 row;
444         const char* table = "";
445         if (!idx)
446                 return g_strdup ("current module");
447         row = idx >> IMPLEMENTATION_BITS;
448         switch (idx & IMPLEMENTATION_MASK) {
449         case IMPLEMENTATION_FILE:
450                 table = "file";
451                 break;
452         case IMPLEMENTATION_ASSEMBLYREF:
453                 table = "assemblyref";
454                 break;
455         case IMPLEMENTATION_EXP_TYPE:
456                 table = "exportedtype";
457                 break;
458         default:
459                 g_assert_not_reached ();
460         }
461         return g_strdup_printf ("%s %d", table, row);
462 }
463
464 static const char*
465 get_manifest_flags (guint32 mf)
466 {
467         mf &= 3;
468         switch (mf) {
469         case 1: return "public";
470         case 2: return "private";
471         default:
472                 return "";
473         }
474 }
475
476 void
477 dump_table_manifest (MonoImage *m)
478 {
479         MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
480         int i;
481         fprintf (output, "Manifestresource Table (1..%d)\n", t->rows);
482
483         for (i = 0; i < t->rows; i++){
484                 guint32 cols [MONO_MANIFEST_SIZE];
485                 const char *name, *mf;
486                 char *impl;
487                 
488                 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
489
490                 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
491                 mf = get_manifest_flags (cols [MONO_MANIFEST_FLAGS]);
492                 impl = get_manifest_implementation (m, cols [MONO_MANIFEST_IMPLEMENTATION]);
493                 fprintf (output, "%d: %s '%s' at offset %u in %s\n", i + 1, mf, name, cols [MONO_MANIFEST_OFFSET], impl);
494                 g_free (impl);
495         }
496         
497 }
498
499 void
500 dump_table_moduleref (MonoImage *m)
501 {
502         MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
503         int i;
504         fprintf (output, "ModuleRef Table (1..%d)\n", t->rows);
505
506         for (i = 0; i < t->rows; i++){
507                 guint32 cols [MONO_MODULEREF_SIZE];
508                 const char *name;
509                 
510                 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
511
512                 name = mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]);
513                 fprintf (output, "%d: %s\n", i + 1, name);
514         }
515         
516 }
517
518 void
519 dump_table_module (MonoImage *m)
520 {
521         MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
522         int i;
523         fprintf (output, "ModuleRef Table (1..%d)\n", t->rows);
524
525         for (i = 0; i < t->rows; i++){
526                 guint32 cols [MONO_MODULE_SIZE];
527                 const char *name;
528                 char *guid;
529                 
530                 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
531
532                 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
533                 guid = get_guid (m, cols [MONO_MODULE_MVID]);
534                 fprintf (output, "%d: %s %d %s\n", i + 1, name, cols [MONO_MODULE_MVID], guid);
535         }       
536 }
537
538 void
539 dump_table_method (MonoImage *m)
540 {
541         MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
542         MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
543         int i, current_type;
544         guint32 first_m, last_m;
545         fprintf (output, "Method Table (1..%d)\n", t->rows);
546
547         current_type = 1;
548         last_m = first_m = 1;
549         for (i = 1; i <= t->rows; i++){
550                 guint32 cols [MONO_METHOD_SIZE];
551                 char *sig;
552                 const char *sigblob;
553                 MonoMethodSignature *method;
554
555                 /*
556                  * Find the next type.
557                  */
558                 while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_METHOD_LIST))) {
559                         current_type++;
560                 }
561                 if (i == first_m) {
562                         fprintf (output, "########## %s.%s\n",
563                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
564                                 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
565                         first_m = last_m;
566                 }
567                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHOD_SIZE);
568                 sigblob = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
569                 mono_metadata_decode_blob_size (sigblob, &sigblob);
570                 method = mono_metadata_parse_method_signature (m, i, sigblob, &sigblob);
571                 sig = dis_stringify_method_signature (m, method, i, FALSE);
572                 fprintf (output, "%d: %s (param: %d)\n", i, sig, cols [MONO_METHOD_PARAMLIST]);
573                 g_free (sig);
574                 mono_metadata_free_method_signature (method);
575         }
576         
577 }
578
579 void
580 dump_table_implmap (MonoImage *m)
581 {
582         MonoTableInfo *t = &m->tables [MONO_TABLE_IMPLMAP];
583         MonoTableInfo *td = &m->tables [MONO_TABLE_MODULEREF];
584         int i;
585
586         fprintf (output, "ImplMap Table (1..%d)\n", t->rows);
587
588         for (i = 1; i <= t->rows; i++){
589                 guint32 cols [MONO_IMPLMAP_SIZE];
590                 char *method;
591
592                 mono_metadata_decode_row (t, i - 1, cols, MONO_IMPLMAP_SIZE);
593
594                 method = get_method (m, MONO_TOKEN_METHOD_DEF | (cols [MONO_IMPLMAP_MEMBER] >> MEMBERFORWD_BITS));
595                 
596                 fprintf (output, "%d: %s %d (%s %s)\n", i, 
597                                  method,
598                                  cols [MONO_IMPLMAP_FLAGS], 
599                                  mono_metadata_string_heap (m, cols [MONO_IMPLMAP_NAME]),
600                                  mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME)));
601         }
602 }
603
604 static guint32
605 method_dor_to_token (guint32 idx) {
606         switch (idx & METHODDEFORREF_MASK) {
607         case METHODDEFORREF_METHODDEF:
608                 return MONO_TOKEN_METHOD_DEF | (idx >> METHODDEFORREF_BITS);
609         case METHODDEFORREF_METHODREF:
610                 return MONO_TOKEN_MEMBER_REF | (idx >> METHODDEFORREF_BITS);
611         }
612         return -1;
613 }
614
615 void
616 dump_table_methodimpl (MonoImage *m)
617 {
618         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODIMPL];
619         /*MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];*/
620         int i;
621
622         fprintf (output, "MethodImpl Table (1..%d)\n", t->rows);
623
624         for (i = 1; i <= t->rows; i++){
625                 guint32 cols [MONO_METHODIMPL_SIZE];
626                 char *class, *impl, *decl;
627
628                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHODIMPL_SIZE);
629                 class = get_typedef (m, cols [MONO_METHODIMPL_CLASS]);
630                 impl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_BODY]));
631                 decl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_DECLARATION]));
632                 fprintf (output, "%d: %s\n\tdecl: %s\n\timpl: %s\n", i, class, decl, impl);
633                 g_free (class);
634                 g_free (impl);
635                 g_free (decl);
636         }
637         
638 }
639
640 static map_t semantics_map [] = {
641                 {1, "setter"},
642                 {2, "getter"},
643                 {4, "other"},
644                 {8, "add-on"},
645                 {0x10, "remove-on"},
646                 {0x20, "fire"},
647                 {0, NULL},
648 };
649
650 void
651 dump_table_methodsem (MonoImage *m)
652 {
653         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSEMANTICS];
654         int i, is_property, index;
655         const char *semantics;
656         
657         fprintf (output, "Method Semantics Table (1..%d)\n", t->rows);
658         for (i = 1; i <= t->rows; i++){
659                 guint32 cols [MONO_METHOD_SEMA_SIZE];
660                 
661                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHOD_SEMA_SIZE);
662                 semantics = flags (cols [MONO_METHOD_SEMA_SEMANTICS], semantics_map);
663                 is_property = cols [MONO_METHOD_SEMA_ASSOCIATION] & HAS_SEMANTICS_MASK;
664                 index = cols [MONO_METHOD_SEMA_ASSOCIATION] >> HAS_SEMANTICS_BITS;
665                 fprintf (output, "%d: [%d] %s method: %d %s %d\n", i, cols [MONO_METHOD_SEMA_ASSOCIATION], semantics,
666                                                 cols [MONO_METHOD_SEMA_METHOD] - 1, 
667                                                 is_property? "property" : "event",
668                                                 index);
669         }
670 }
671
672 void 
673 dump_table_interfaceimpl (MonoImage *m)
674 {
675         MonoTableInfo *t = &m->tables [MONO_TABLE_INTERFACEIMPL];
676         int i;
677
678         fprintf (output, "Interface Implementation Table (1..%d)\n", t->rows);
679         for (i = 1; i <= t->rows; i++) {
680                 guint32 cols [MONO_INTERFACEIMPL_SIZE];
681                 
682                 mono_metadata_decode_row (t, i - 1, cols, MONO_INTERFACEIMPL_SIZE);
683                 fprintf (output, "%d: %s implements %s\n", i,
684                         get_typedef (m, cols [MONO_INTERFACEIMPL_CLASS]),
685                         get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE]));
686         }
687 }
688
689 static char*
690 has_cattr_get_table (MonoImage *m, guint32 val)
691 {
692         guint32 t = val & CUSTOM_ATTR_MASK;
693         guint32 index = val >> CUSTOM_ATTR_BITS;
694         const char *table;
695
696         switch (t) {
697         case CUSTOM_ATTR_METHODDEF:
698                 table = "MethodDef";
699                 break;
700         case CUSTOM_ATTR_FIELDDEF:
701                 table = "FieldDef";
702                 break;
703         case CUSTOM_ATTR_TYPEREF:
704                 table = "TypeRef";
705                 break;
706         case CUSTOM_ATTR_TYPEDEF:
707                 table = "TypeDef";
708                 break;
709         case CUSTOM_ATTR_PARAMDEF:
710                 table = "Param";
711                 break;
712         case CUSTOM_ATTR_INTERFACE:
713                 table = "InterfaceImpl";
714                 break;
715         case CUSTOM_ATTR_MEMBERREF:
716                 table = "MemberRef";
717                 break;
718         case CUSTOM_ATTR_MODULE:
719                 table = "Module";
720                 break;
721         case CUSTOM_ATTR_PERMISSION:
722                 table = "DeclSecurity?";
723                 break;
724         case CUSTOM_ATTR_PROPERTY:
725                 table = "Property";
726                 break;
727         case CUSTOM_ATTR_EVENT:
728                 table = "Event";
729                 break;
730         case CUSTOM_ATTR_SIGNATURE:
731                 table = "StandAloneSignature";
732                 break;
733         case CUSTOM_ATTR_MODULEREF:
734                 table = "ModuleRef";
735                 break;
736         case CUSTOM_ATTR_TYPESPEC:
737                 table = "TypeSpec";
738                 break;
739         case CUSTOM_ATTR_ASSEMBLY:
740                 table = "Assembly";
741                 break;
742         case CUSTOM_ATTR_ASSEMBLYREF:
743                 table = "AssemblyRef";
744                 break;
745         case CUSTOM_ATTR_FILE:
746                 table = "File";
747                 break;
748         case CUSTOM_ATTR_EXP_TYPE:
749                 table = "ExportedType";
750                 break;
751         case CUSTOM_ATTR_MANIFEST:
752                 table = "Manifest";
753                 break;
754         default:
755                 table = "Unknown";
756                 break;
757         }
758         /*
759          * FIXME: we should decode the index into something more uman-friendly.
760          */
761         return g_strdup_printf ("%s: %d", table, index);
762 }
763
764 static char*
765 custom_attr_params (MonoImage *m, MonoMethodSignature* sig, const char* value)
766 {
767         int len, i, slen, type;
768         GString *res;
769         char *s;
770         const char *p = value;
771
772         len = mono_metadata_decode_value (p, &p);
773         if (len < 2 || read16 (p) != 0x0001) /* Prolog */
774                 return g_strdup ("");
775
776         /* skip prolog */
777         p += 2;
778         res = g_string_new ("");
779         for (i = 0; i < sig->param_count; ++i) {
780                 if (i != 0)
781                         g_string_append (res, ", ");
782                 type = sig->params [i]->type;
783 handle_enum:
784                 switch (type) {
785                 case MONO_TYPE_U1:
786                         g_string_sprintfa (res, "%d", (unsigned int)*p);
787                         ++p;
788                         break;
789                 case MONO_TYPE_I1:
790                         g_string_sprintfa (res, "%d", *p);
791                         ++p;
792                         break;
793                 case MONO_TYPE_BOOLEAN:
794                         g_string_sprintfa (res, "%s", *p?"true":"false");
795                         ++p;
796                         break;
797                 case MONO_TYPE_CHAR:
798                         g_string_sprintfa (res, "'%c'", read16 (p));
799                         p += 2;
800                         break;
801                 case MONO_TYPE_U2:
802                         g_string_sprintfa (res, "%d", read16 (p));
803                         p += 2;
804                         break;
805                 case MONO_TYPE_I2:
806                         g_string_sprintfa (res, "%d", (gint16)read16 (p));
807                         p += 2;
808                         break;
809                 case MONO_TYPE_U4:
810                         g_string_sprintfa (res, "%d", read32 (p));
811                         p += 4;
812                         break;
813                 case MONO_TYPE_I4:
814                         g_string_sprintfa (res, "%d", (gint32)read32 (p));
815                         p += 4;
816                         break;
817                 case MONO_TYPE_U8:
818                         g_string_sprintfa (res, "%lld", read64 (p));
819                         p += 8;
820                         break;
821                 case MONO_TYPE_I8:
822                         g_string_sprintfa (res, "%lld", (gint64)read64 (p));
823                         p += 8;
824                         break;
825                 case MONO_TYPE_R4: {
826                         float val;
827                         readr4 (p, &val);
828                         g_string_sprintfa (res, "%g", val);
829                         p += 4;
830                         break;
831                 }
832                 case MONO_TYPE_R8: {
833                         double val;
834                         readr8 (p, &val);
835                         g_string_sprintfa (res, "%g", val);
836                         p += 8;
837                         break;
838                 }
839                 case MONO_TYPE_VALUETYPE:
840                         if (sig->params [i]->data.klass->enumtype) {
841                                 type = sig->params [i]->data.klass->enum_basetype->type;
842                                 goto handle_enum;
843                         } else {
844                                 g_warning ("generic valutype not handled in custom attr value decoding");
845                         }
846                         break;
847                 case MONO_TYPE_CLASS: /* It must be a Type: check? */
848                 case MONO_TYPE_STRING:
849                         if (*p == (char)0xff) {
850                                 g_string_append (res, "null");
851                                 p++;
852                                 break;
853                         }
854                         slen = mono_metadata_decode_value (p, &p);
855                         g_string_append_c (res, '"');
856                         g_string_append (res, p);
857                         g_string_append_c (res, '"');
858                         p += slen;
859                         break;
860                 default:
861                         g_warning ("Type %02x not handled in custom attr value decoding", sig->params [i]->type);
862                         break;
863                 }
864         }
865         slen = read16 (p);
866         if (slen) {
867                 g_string_sprintfa (res, " %d named args: (", slen);
868                 slen = len - (p - value) + 1;
869                 for (i = 0; i < slen; ++i) {
870                         g_string_sprintfa (res, " %02X", (p [i] & 0xff));
871                 }
872                 g_string_append_c (res, ')');
873         }
874         s = res->str;
875         g_string_free (res, FALSE);
876         return s;
877 }
878
879 void
880 dump_table_customattr (MonoImage *m)
881 {
882         MonoTableInfo *t = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
883         int i;
884
885         fprintf (output, "Custom Attributes Table (1..%d)\n", t->rows);
886         for (i = 1; i <= t->rows; i++) {
887                 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
888                 guint32 mtoken;
889                 char * desc;
890                 char *method;
891                 char *params;
892                 MonoMethod *meth;
893                 
894                 mono_metadata_decode_row (t, i - 1, cols, MONO_CUSTOM_ATTR_SIZE);
895                 desc = has_cattr_get_table (m, cols [MONO_CUSTOM_ATTR_PARENT]);
896                 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> CUSTOM_ATTR_TYPE_BITS;
897                 switch (cols [MONO_CUSTOM_ATTR_TYPE] & CUSTOM_ATTR_TYPE_MASK) {
898                 case CUSTOM_ATTR_TYPE_METHODDEF:
899                         mtoken |= MONO_TOKEN_METHOD_DEF;
900                         break;
901                 case CUSTOM_ATTR_TYPE_MEMBERREF:
902                         mtoken |= MONO_TOKEN_MEMBER_REF;
903                         break;
904                 default:
905                         g_warning ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
906                         break;
907                 }
908                 method = get_method (m, mtoken);
909                 meth = mono_get_method (m, mtoken, NULL);
910                 params = custom_attr_params (m, meth->signature, mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]));
911                 fprintf (output, "%d: %s: %s [%s]\n", i, desc, method, params);
912                 g_free (desc);
913                 g_free (method);
914                 g_free (params);
915         }
916 }
917
918 void
919 dump_table_nestedclass (MonoImage *m)
920 {
921         MonoTableInfo *t = &m->tables [MONO_TABLE_NESTEDCLASS];
922         guint32 cols [MONO_NESTED_CLASS_SIZE];
923         int i;
924         char *nested, *nesting;
925         fprintf (output, "NestedClass Table (1..%d)\n", t->rows);
926
927         for (i = 1; i <= t->rows; i++){
928                 mono_metadata_decode_row (t, i - 1, cols, MONO_NESTED_CLASS_SIZE);
929                 nested = get_typedef (m, cols [MONO_NESTED_CLASS_NESTED]);
930                 nesting = get_typedef (m, cols [MONO_NESTED_CLASS_ENCLOSING]);
931                 fprintf (output, "%d: %d %d: %s in %s\n", i,
932                                 cols [MONO_NESTED_CLASS_NESTED], 
933                                 cols [MONO_NESTED_CLASS_ENCLOSING], nested, nesting);
934                 g_free (nested);
935                 g_free (nesting);
936         }
937         
938 }
939
940 void
941 dump_table_exported (MonoImage *m)
942 {
943         MonoTableInfo *t = &m->tables [MONO_TABLE_EXPORTEDTYPE];
944         guint32 cols [MONO_EXP_TYPE_SIZE];
945         int i;
946         const char *name, *nspace;
947         char *impl;
948         guint32 index;
949         fprintf (output, "ExportedType Table (1..%d)\n", t->rows);
950
951         for (i = 1; i <= t->rows; i++) {
952                 mono_metadata_decode_row (t, i - 1, cols, MONO_EXP_TYPE_SIZE);
953                 name = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAME]);
954                 nspace = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAMESPACE]);
955                 impl = get_manifest_implementation (m, cols [MONO_EXP_TYPE_IMPLEMENTATION]);
956                 index = cols [MONO_EXP_TYPE_TYPEDEF];
957                 fprintf (output, "%d: %s%s%s is in %s, token %x\n", i, nspace, *nspace ? "." : "", name, impl, index);
958                 g_free (impl);
959         }
960         
961 }
962
963 void
964 dump_table_field_marshal (MonoImage *m)
965 {
966         MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDMARSHAL];
967         guint32 cols [MONO_FIELD_MARSHAL_SIZE];
968         int i, is_field, idx;
969         const char *blob;
970         char *native;
971         
972         fprintf (output, "FieldMarshal Table (1..%d)\n", t->rows);
973
974         for (i = 1; i <= t->rows; i++) {
975                 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_MARSHAL_SIZE);
976                 blob = mono_metadata_blob_heap (m, cols [MONO_FIELD_MARSHAL_NATIVE_TYPE]);
977                 native = get_marshal_info (m, blob);
978                 is_field = (cols [MONO_FIELD_MARSHAL_PARENT] & HAS_FIELD_MARSHAL_MASK) == HAS_FIELD_MARSHAL_FIELDSREF;
979                 idx = cols [MONO_FIELD_MARSHAL_PARENT] >> HAS_FIELD_MARSHAL_BITS;
980                 fprintf (output, "%d: (0x%04x) %s %d: %s\n", i, cols [MONO_FIELD_MARSHAL_PARENT], is_field? "Field" : "Param", idx, native);
981                 g_free (native);
982         }
983         
984 }
985
986 static const char*
987 get_security_action (int val) {
988         static char buf [32];
989
990         switch (val) {
991         case SECURITY_ACTION_DEMAND:
992                 return "Demand";
993         case SECURITY_ACTION_ASSERT:
994                 return "Assert";
995         case SECURITY_ACTION_DENY:
996                 return "Deny";
997         case SECURITY_ACTION_PERMITONLY:
998                 return "PermitOnly";
999         case SECURITY_ACTION_LINKDEMAND:
1000                 return "LinkDemand";
1001         case SECURITY_ACTION_INHERITDEMAND:
1002                 return "InheritanceDemand";
1003         case SECURITY_ACTION_REQMIN:
1004                 return "RequestMinimum";
1005         case SECURITY_ACTION_REQOPT:
1006                 return "RequestOptional";
1007         case SECURITY_ACTION_REQREFUSE:
1008                 return "RequestRefuse";
1009         default:
1010                 g_snprintf (buf, sizeof (buf), "0x%04X", val);
1011                 return buf;
1012         }
1013 }
1014
1015 void 
1016 dump_table_declsec (MonoImage *m)
1017 {
1018         MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
1019         guint32 cols [MONO_DECL_SECURITY_SIZE];
1020         int i, len;
1021         guint32 idx;
1022         const char *blob, *action;
1023         const char* parent[] = {
1024                 "TypeDef", "MethodDef", "Assembly", ""
1025         };
1026         
1027         fprintf (output, "DeclSecurity Table (1..%d)\n", t->rows);
1028
1029         for (i = 1; i <= t->rows; i++) {
1030                 mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
1031                 blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
1032                 len = mono_metadata_decode_blob_size (blob, &blob);
1033                 action = get_security_action (cols [MONO_DECL_SECURITY_ACTION]);
1034                 idx = cols [MONO_DECL_SECURITY_PARENT];
1035                 fprintf (output, "%d: %s on %s %d%s", i, action, parent [idx & HAS_DECL_SECURITY_MASK], idx >> HAS_DECL_SECURITY_BITS, len? ":\n\t":"\n");
1036                 if (!len)
1037                         continue;
1038                 for (idx = 0; idx < len; ++idx)
1039                         fprintf (output, "%c", blob [idx]);
1040                 fprintf (output, "\n");
1041         }
1042 }
1043
1044 void 
1045 dump_table_genericpar (MonoImage *m)
1046 {
1047         MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAM];
1048         guint32 cols [MONO_GENERICPARAM_SIZE];
1049         int i;
1050
1051         fprintf (output, "GenericParameters (1..%d)\n", t->rows);
1052         
1053         for (i = 1; i <= t->rows; i++) {
1054                 char *sig;
1055                 mono_metadata_decode_row (t, i - 1, cols, MONO_GENERICPARAM_SIZE);
1056
1057                 sig = get_type_or_methdef (m, cols [MONO_GENERICPARAM_OWNER]);
1058                 fprintf (output, "%d: %d, flags=%d, owner=%s %s\n", i,
1059                          cols [MONO_GENERICPARAM_NUMBER],
1060                          cols [MONO_GENERICPARAM_FLAGS], sig,
1061                          mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
1062                 g_free (sig);
1063         }
1064 }
1065
1066 void
1067 dump_table_methodspec (MonoImage *m)
1068 {
1069         MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSPEC];
1070         guint32 cols [MONO_METHODSPEC_SIZE];
1071         int i;
1072         
1073         fprintf (output, "MethodSpec (1..%d)\n", t->rows);
1074
1075         for (i = 1; i <= t->rows; i++) {
1076                 char *sig;
1077                 char *method;
1078                 guint32 token;
1079                 
1080                 mono_metadata_decode_row (t, i - 1, cols, MONO_METHODSPEC_SIZE);
1081
1082                 // build a methodspec token to get the method
1083                 token = MONO_TOKEN_METHOD_SPEC | i;
1084                 method = get_method (m, token);
1085                 
1086                 sig = get_method_type_param (m, cols [MONO_METHODSPEC_SIGNATURE]);
1087                 fprintf (output, "%d: %s, %s\n", i, method, sig);
1088                 g_free (sig);
1089                 g_free (method);
1090         }
1091 }
1092
1093 void
1094 dump_table_parconstraint (MonoImage *m)
1095 {
1096         MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
1097         guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
1098         int i;
1099         
1100         fprintf (output, "Generic Param Constraint (1..%d)\n", t->rows);
1101
1102         for (i = 1; i <= t->rows; i++) {
1103                 char *sig;
1104                 mono_metadata_decode_row (t, i - 1, cols, MONO_GENPARCONSTRAINT_SIZE);
1105
1106                 sig = get_typedef_or_ref (m, cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
1107                 fprintf (output, "%d: gen-par=%d, Constraint=%s\n", i,
1108                          cols [MONO_GENPARCONSTRAINT_GENERICPAR], sig);
1109                 g_free (sig);
1110         }
1111 }
1112