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