2 * dump.c: Dumping routines for the disassembler.
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
19 #include "mono/metadata/loader.h"
20 #include "mono/metadata/class.h"
21 #include "mono/metadata/class-internals.h"
22 #include "mono/utils/mono-compiler.h"
27 extern int isinf (double);
32 #if defined(__native_client__) && defined(__GLIBC__)
33 volatile int __nacl_thread_suspension_needed = 0;
34 void __nacl_suspend_thread_if_needed() {}
38 dump_table_assembly (MonoImage *m)
40 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
41 guint32 cols [MONO_ASSEMBLY_SIZE];
45 fprintf (output, "Assembly Table\n");
50 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
52 fprintf (output, "Name: %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
53 fprintf (output, "Hash Algoritm: 0x%08x\n", cols [MONO_ASSEMBLY_HASH_ALG]);
54 fprintf (output, "Version: %d.%d.%d.%d\n", cols [MONO_ASSEMBLY_MAJOR_VERSION],
55 cols [MONO_ASSEMBLY_MINOR_VERSION],
56 cols [MONO_ASSEMBLY_BUILD_NUMBER],
57 cols [MONO_ASSEMBLY_REV_NUMBER]);
58 fprintf (output, "Flags: 0x%08x\n", cols [MONO_ASSEMBLY_FLAGS]);
59 fprintf (output, "PublicKey: BlobPtr (0x%08x)\n", cols [MONO_ASSEMBLY_PUBLIC_KEY]);
61 ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
62 len = mono_metadata_decode_value (ptr, &ptr);
64 fprintf (output, "\tDump:");
65 hex_dump (ptr, 0, len);
66 fprintf (output, "\n");
68 fprintf (output, "\tZero sized public key\n");
70 fprintf (output, "Culture: %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]));
71 fprintf (output, "\n");
75 dump_table_typeref (MonoImage *m)
77 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEREF];
80 fprintf (output, "Typeref Table\n");
82 for (i = 1; i <= t->rows; i++){
83 char *s = get_typeref (m, i);
85 fprintf (output, "%d: %s\n", i, s);
88 fprintf (output, "\n");
92 dump_table_typedef (MonoImage *m)
94 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
97 fprintf (output, "Typedef Table\n");
99 for (i = 1; i <= t->rows; i++){
100 char *s = get_typedef (m, i);
101 guint32 cols [MONO_TYPEDEF_SIZE];
103 mono_metadata_decode_row (&m->tables [MONO_TABLE_TYPEDEF], i - 1, cols, MONO_TYPEDEF_SIZE);
105 fprintf (output, "%d: %s (flist=%d, mlist=%d, flags=0x%x, extends=0x%x)\n", i, s,
106 cols [MONO_TYPEDEF_FIELD_LIST], cols [MONO_TYPEDEF_METHOD_LIST],
107 cols [MONO_TYPEDEF_FLAGS], cols [MONO_TYPEDEF_EXTENDS]);
110 fprintf (output, "\n");
114 dump_table_typespec (MonoImage *m)
116 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPESPEC];
119 fprintf (output, "Typespec Table\n");
121 for (i = 1; i <= t->rows; i++){
122 char *typespec = get_typespec (m, i, TRUE, NULL);
124 fprintf (output, "%d: %s\n", i, typespec);
127 fprintf (output, "\n");
131 dump_table_assemblyref (MonoImage *m)
133 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
136 fprintf (output, "AssemblyRef Table\n");
138 for (i = 0; i < t->rows; i++){
141 guint32 cols [MONO_ASSEMBLYREF_SIZE];
143 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
144 fprintf (output, "%d: Version=%d.%d.%d.%d\n\tName=%s\n", i + 1,
145 cols [MONO_ASSEMBLYREF_MAJOR_VERSION],
146 cols [MONO_ASSEMBLYREF_MINOR_VERSION],
147 cols [MONO_ASSEMBLYREF_BUILD_NUMBER],
148 cols [MONO_ASSEMBLYREF_REV_NUMBER],
149 mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
150 fprintf (output, "\tFlags=0x%08x\n", cols [MONO_ASSEMBLYREF_FLAGS]);
151 ptr = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLYREF_PUBLIC_KEY]);
152 len = mono_metadata_decode_value (ptr, &ptr);
154 fprintf (output, "\tPublic Key:");
155 hex_dump (ptr, 0, len);
156 fprintf (output, "\n");
158 fprintf (output, "\tZero sized public key\n");
161 fprintf (output, "\n");
165 dump_table_param (MonoImage *m)
167 MonoTableInfo *t = &m->tables [MONO_TABLE_PARAM];
170 fprintf (output, "Param Table\n");
172 for (i = 0; i < t->rows; i++){
173 guint32 cols [MONO_PARAM_SIZE];
175 mono_metadata_decode_row (t, i, cols, MONO_PARAM_SIZE);
176 fprintf (output, "%d: 0x%04x %d %s\n",
178 cols [MONO_PARAM_FLAGS], cols [MONO_PARAM_SEQUENCE],
179 mono_metadata_string_heap (m, cols [MONO_PARAM_NAME]));
181 fprintf (output, "\n");
185 dump_table_field (MonoImage *m)
187 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
188 MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
189 MonoTableInfo *fl = &m->tables [MONO_TABLE_FIELDLAYOUT];
190 MonoTableInfo *rva = &m->tables [MONO_TABLE_FIELDRVA];
191 int i, current_type, offset_row, rva_row;
192 guint32 first_m, last_m;
194 fprintf (output, "Field Table (1..%d)\n", t->rows);
196 rva_row = offset_row = current_type = 1;
197 last_m = first_m = 1;
198 for (i = 1; i <= t->rows; i++){
199 guint32 cols [MONO_FIELD_SIZE];
203 * Find the next type.
205 while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_FIELD_LIST))) {
209 fprintf (output, "########## %s.%s\n",
210 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
211 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
214 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_SIZE);
215 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE], NULL);
216 flags = field_flags (cols [MONO_FIELD_FLAGS]);
217 fprintf (output, "%d: %s %s: %s\n",
220 mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]),
224 if (offset_row <= fl->rows && (mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_FIELD) == i)) {
225 fprintf (output, "\texplicit offset: %d\n", mono_metadata_decode_row_col (fl, offset_row - 1, MONO_FIELD_LAYOUT_OFFSET));
228 if (rva_row <= rva->rows && (mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_FIELD) == i)) {
229 fprintf (output, "\trva: %d\n", mono_metadata_decode_row_col (rva, rva_row - 1, MONO_FIELD_RVA_RVA));
233 fprintf (output, "\n");
237 dump_table_memberref (MonoImage *m)
239 MonoTableInfo *t = &m->tables [MONO_TABLE_MEMBERREF];
243 const char *blob, *ks = NULL;
245 fprintf (output, "MemberRef Table (1..%d)\n", t->rows);
247 for (i = 0; i < t->rows; i++){
248 guint32 cols [MONO_MEMBERREF_SIZE];
250 mono_metadata_decode_row (t, i, cols, MONO_MEMBERREF_SIZE);
252 kind = cols [MONO_MEMBERREF_CLASS] & 7;
253 idx = cols [MONO_MEMBERREF_CLASS] >> 3;
255 x = g_strdup ("UNHANDLED CASE");
260 xx = get_typedef (m, idx);
261 x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
266 xx = get_typeref (m, idx);
267 x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
271 ks = "ModuleRef"; break;
274 x = get_methoddef (m, idx);
278 xx = get_typespec (m, idx, FALSE, NULL);
279 x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]), NULL);
283 g_error ("Unknown tag: %d\n", kind);
285 blob = mono_metadata_blob_heap (m, cols [MONO_MEMBERREF_SIGNATURE]);
286 mono_metadata_decode_blob_size (blob, &blob);
287 if (*blob == 0x6) { /* it's a field */
288 sig = get_field_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
290 sig = get_methodref_signature (m, cols [MONO_MEMBERREF_SIGNATURE], NULL);
292 fprintf (output, "%d: %s[%d] %s\n\tResolved: %s\n\tSignature: %s\n\t\n",
295 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]),
306 dump_table_class_layout (MonoImage *m)
308 MonoTableInfo *t = &m->tables [MONO_TABLE_CLASSLAYOUT];
310 fprintf (output, "ClassLayout Table (1..%d)\n", t->rows);
312 for (i = 0; i < t->rows; i++){
313 guint32 cols [MONO_CLASS_LAYOUT_SIZE];
315 mono_metadata_decode_row (t, i, cols, MONO_CLASS_LAYOUT_SIZE);
317 fprintf (output, "%d: PackingSize=%d ClassSize=%d Parent=%s\n",
318 i + 1, cols [MONO_CLASS_LAYOUT_PACKING_SIZE],
319 cols [MONO_CLASS_LAYOUT_CLASS_SIZE],
320 get_typedef (m, cols [MONO_CLASS_LAYOUT_PARENT]));
325 dump_table_constant (MonoImage *m)
327 MonoTableInfo *t = &m->tables [MONO_TABLE_CONSTANT];
329 const char *desc [] = {
335 fprintf (output, "Constant Table (1..%d)\n", t->rows);
337 for (i = 0; i < t->rows; i++){
338 guint32 cols [MONO_CONSTANT_SIZE];
340 mono_metadata_decode_row (t, i, cols, MONO_CONSTANT_SIZE);
341 parent = desc [cols [MONO_CONSTANT_PARENT] & MONO_HASCONSTANT_MASK];
343 fprintf (output, "%d: Parent= %s: %d %s\n",
344 i + 1, parent, cols [MONO_CONSTANT_PARENT] >> MONO_HASCONSTANT_BITS,
345 get_constant (m, (MonoTypeEnum) cols [MONO_CONSTANT_TYPE], cols [MONO_CONSTANT_VALUE]));
351 dump_table_property_map (MonoImage *m)
353 MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTYMAP];
357 fprintf (output, "Property Map Table (1..%d)\n", t->rows);
359 for (i = 0; i < t->rows; i++){
360 guint32 cols [MONO_PROPERTY_MAP_SIZE];
362 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_MAP_SIZE);
363 s = get_typedef (m, cols [MONO_PROPERTY_MAP_PARENT]);
364 fprintf (output, "%d: %s (%d) %d\n", i + 1, s, cols [MONO_PROPERTY_MAP_PARENT], cols [MONO_PROPERTY_MAP_PROPERTY_LIST]);
370 dump_table_property (MonoImage *m)
372 MonoTableInfo *t = &m->tables [MONO_TABLE_PROPERTY];
377 fprintf (output, "Property Table (1..%d)\n", t->rows);
379 for (i = 0; i < t->rows; i++){
380 guint32 cols [MONO_PROPERTY_SIZE];
385 mono_metadata_decode_row (t, i, cols, MONO_PROPERTY_SIZE);
387 prop_flags = cols [MONO_PROPERTY_FLAGS];
388 if (prop_flags & 0x0200)
389 strcat (flags, "special ");
390 if (prop_flags & 0x0400)
391 strcat (flags, "runtime ");
392 if (prop_flags & 0x1000)
393 strcat (flags, "hasdefault ");
395 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
396 bsize = mono_metadata_decode_blob_size (ptr, &ptr);
397 /* ECMA claims 0x08 ... */
398 if (*ptr != 0x28 && *ptr != 0x08)
399 g_warning("incorrect signature in propert blob: 0x%x", *ptr);
401 pcount = mono_metadata_decode_value (ptr, &ptr);
402 ptr = get_type (m, ptr, &type, FALSE, NULL);
403 fprintf (output, "%d: %s %s (",
404 i + 1, type, mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]));
407 for (j = 0; j < pcount; j++){
408 ptr = get_param (m, ptr, &type, NULL);
409 fprintf (output, "%s%s", j > 0? ", " : "",type);
412 fprintf (output, ") %s\n", flags);
417 dump_table_event (MonoImage *m)
419 MonoTableInfo *t = &m->tables [MONO_TABLE_EVENT];
421 fprintf (output, "Event Table (1..%d)\n", t->rows);
423 for (i = 0; i < t->rows; i++){
424 guint32 cols [MONO_EVENT_SIZE];
428 mono_metadata_decode_row (t, i, cols, MONO_EVENT_SIZE);
430 name = mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]);
431 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE], NULL);
432 fprintf (output, "%d: %s %s %s\n", i + 1, type, name,
433 cols [MONO_EVENT_FLAGS] & 0x200 ? "specialname " : "");
440 dump_table_file (MonoImage *m)
442 MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
444 fprintf (output, "File Table (1..%d)\n", t->rows);
446 for (i = 0; i < t->rows; i++){
447 guint32 cols [MONO_FILE_SIZE];
448 const char *name, *hash;
450 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
452 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
453 fprintf (output, "%d: %s %s [", i + 1, name,
454 cols [MONO_FILE_FLAGS] & 0x1 ? "nometadata" : "containsmetadata");
455 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
456 len = mono_metadata_decode_blob_size (hash, &hash);
457 for (j = 0; j < len; ++j)
458 fprintf (output, "%s%02X", j? " ": "", hash [j] & 0xff);
459 fprintf (output, "]\n");
465 get_manifest_implementation (MonoImage *m, guint32 idx)
468 const char* table = "";
470 return g_strdup ("current module");
471 row = idx >> MONO_IMPLEMENTATION_BITS;
472 switch (idx & MONO_IMPLEMENTATION_MASK) {
473 case MONO_IMPLEMENTATION_FILE:
476 case MONO_IMPLEMENTATION_ASSEMBLYREF:
477 table = "assemblyref";
479 case MONO_IMPLEMENTATION_EXP_TYPE:
480 table = "exportedtype";
483 g_assert_not_reached ();
485 return g_strdup_printf ("%s %d", table, row);
489 get_manifest_flags (guint32 mf)
493 case 1: return "public";
494 case 2: return "private";
501 dump_table_manifest (MonoImage *m)
503 MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
505 fprintf (output, "Manifestresource Table (1..%d)\n", t->rows);
507 for (i = 0; i < t->rows; i++){
508 guint32 cols [MONO_MANIFEST_SIZE];
509 const char *name, *mf;
512 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
514 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
515 mf = get_manifest_flags (cols [MONO_MANIFEST_FLAGS]);
516 impl = get_manifest_implementation (m, cols [MONO_MANIFEST_IMPLEMENTATION]);
517 fprintf (output, "%d: %s '%s' at offset %u in %s\n", i + 1, mf, name, cols [MONO_MANIFEST_OFFSET], impl);
524 dump_table_moduleref (MonoImage *m)
526 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
528 fprintf (output, "ModuleRef Table (1..%d)\n", t->rows);
530 for (i = 0; i < t->rows; i++){
531 guint32 cols [MONO_MODULEREF_SIZE];
534 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
536 name = mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]);
537 fprintf (output, "%d: %s\n", i + 1, name);
543 dump_table_module (MonoImage *m)
545 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
547 fprintf (output, "Module Table (1..%d)\n", t->rows);
549 for (i = 0; i < t->rows; i++){
550 guint32 cols [MONO_MODULE_SIZE];
554 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
556 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
557 guid = get_guid (m, cols [MONO_MODULE_MVID]);
558 fprintf (output, "%d: %s %d %s\n", i + 1, name, cols [MONO_MODULE_MVID], guid);
563 dump_table_method (MonoImage *m)
565 MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
566 MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];
568 guint32 first_m, last_m;
569 /* Generic container for Type & method */
570 MonoGenericContainer *type_container = NULL, *method_container = NULL;
572 fprintf (output, "Method Table (1..%d)\n", t->rows);
575 last_m = first_m = 1;
576 for (i = 1; i <= t->rows; i++){
577 guint32 cols [MONO_METHOD_SIZE];
578 char *sig, *impl_flags;
580 MonoMethodSignature *method;
583 * Find the next type.
585 while (current_type <= td->rows && i >= (last_m = mono_metadata_decode_row_col (td, current_type - 1, MONO_TYPEDEF_METHOD_LIST))) {
589 fprintf (output, "########## %s.%s\n",
590 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAMESPACE)),
591 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, current_type - 2, MONO_TYPEDEF_NAME)));
593 type_container = mono_metadata_load_generic_params (m, MONO_TOKEN_TYPE_DEF | (current_type - 1), NULL);
595 mono_metadata_load_generic_param_constraints (m, MONO_TOKEN_TYPE_DEF | (current_type - 1), type_container);
598 method_container = mono_metadata_load_generic_params (m, MONO_TOKEN_METHOD_DEF | i, type_container);
599 if (method_container)
600 mono_metadata_load_generic_param_constraints (m, MONO_TOKEN_METHOD_DEF | i, method_container);
601 mono_metadata_decode_table_row (m, MONO_TABLE_METHOD, i - 1, cols, MONO_METHOD_SIZE);
602 sigblob = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
603 mono_metadata_decode_blob_size (sigblob, &sigblob);
604 method = mono_metadata_parse_method_signature_full (m, method_container ? method_container : type_container, i, sigblob, &sigblob);
605 sig = dis_stringify_method_signature (m, method, i, method_container ? method_container : type_container, FALSE);
606 impl_flags = get_method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
607 fprintf (output, "%d: %s (param: %d impl_flags: %s)\n", i, sig, cols [MONO_METHOD_PARAMLIST], impl_flags);
610 mono_metadata_free_method_signature (method);
616 dump_table_implmap (MonoImage *m)
618 MonoTableInfo *t = &m->tables [MONO_TABLE_IMPLMAP];
619 MonoTableInfo *td = &m->tables [MONO_TABLE_MODULEREF];
622 fprintf (output, "ImplMap Table (1..%d)\n", t->rows);
624 for (i = 1; i <= t->rows; i++){
625 guint32 cols [MONO_IMPLMAP_SIZE];
628 mono_metadata_decode_row (t, i - 1, cols, MONO_IMPLMAP_SIZE);
630 method = get_method (m, MONO_TOKEN_METHOD_DEF | (cols [MONO_IMPLMAP_MEMBER] >> MONO_MEMBERFORWD_BITS), NULL);
632 fprintf (output, "%d: %s %d (%s %s)\n", i,
634 cols [MONO_IMPLMAP_FLAGS],
635 mono_metadata_string_heap (m, cols [MONO_IMPLMAP_NAME]),
636 mono_metadata_string_heap (m, mono_metadata_decode_row_col (td, cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME)));
641 dump_table_fieldrva (MonoImage *m)
643 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
646 fprintf (output, "FieldRVA Table (1..%d)\n", t->rows);
648 for (i = 1; i <= t->rows; i++){
649 guint32 cols [MONO_FIELD_RVA_SIZE];
651 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_RVA_SIZE);
652 fprintf (output, "%d: Field %d: %x\n", i, cols [MONO_FIELD_RVA_FIELD], cols [MONO_FIELD_RVA_RVA]);
657 dump_table_methodimpl (MonoImage *m)
659 MonoTableInfo *t = &m->tables [MONO_TABLE_METHODIMPL];
660 /*MonoTableInfo *td = &m->tables [MONO_TABLE_TYPEDEF];*/
663 fprintf (output, "MethodImpl Table (1..%d)\n", t->rows);
665 for (i = 1; i <= t->rows; i++){
666 guint32 cols [MONO_METHODIMPL_SIZE];
667 char *class, *impl, *decl;
669 mono_metadata_decode_row (t, i - 1, cols, MONO_METHODIMPL_SIZE);
670 class = get_typedef (m, cols [MONO_METHODIMPL_CLASS]);
671 impl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_BODY]), NULL);
672 decl = get_method (m, method_dor_to_token (cols [MONO_METHODIMPL_DECLARATION]), NULL);
673 fprintf (output, "%d: %s\n\tdecl: %s\n\timpl: %s\n", i, class, decl, impl);
681 static dis_map_t semantics_map [] = {
692 dump_table_methodsem (MonoImage *m)
694 MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSEMANTICS];
695 int i, is_property, index;
696 const char *semantics;
698 fprintf (output, "Method Semantics Table (1..%d)\n", t->rows);
699 for (i = 1; i <= t->rows; i++){
700 guint32 cols [MONO_METHOD_SEMA_SIZE];
702 mono_metadata_decode_row (t, i - 1, cols, MONO_METHOD_SEMA_SIZE);
703 semantics = flags (cols [MONO_METHOD_SEMA_SEMANTICS], semantics_map);
704 is_property = cols [MONO_METHOD_SEMA_ASSOCIATION] & MONO_HAS_SEMANTICS_MASK;
705 index = cols [MONO_METHOD_SEMA_ASSOCIATION] >> MONO_HAS_SEMANTICS_BITS;
706 fprintf (output, "%d: [%d] %s method: %d %s %d\n", i, cols [MONO_METHOD_SEMA_ASSOCIATION], semantics,
707 cols [MONO_METHOD_SEMA_METHOD] - 1,
708 is_property? "property" : "event",
714 dump_table_interfaceimpl (MonoImage *m)
716 MonoTableInfo *t = &m->tables [MONO_TABLE_INTERFACEIMPL];
719 fprintf (output, "Interface Implementation Table (1..%d)\n", t->rows);
720 for (i = 1; i <= t->rows; i++) {
721 guint32 cols [MONO_INTERFACEIMPL_SIZE];
723 mono_metadata_decode_row (t, i - 1, cols, MONO_INTERFACEIMPL_SIZE);
724 fprintf (output, "%d: %s implements %s\n", i,
725 get_typedef (m, cols [MONO_INTERFACEIMPL_CLASS]),
726 get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE], NULL));
731 has_cattr_get_table (MonoImage *m, guint32 val)
733 guint32 t = val & MONO_CUSTOM_ATTR_MASK;
734 guint32 index = val >> MONO_CUSTOM_ATTR_BITS;
738 case MONO_CUSTOM_ATTR_METHODDEF:
741 case MONO_CUSTOM_ATTR_FIELDDEF:
744 case MONO_CUSTOM_ATTR_TYPEREF:
747 case MONO_CUSTOM_ATTR_TYPEDEF:
750 case MONO_CUSTOM_ATTR_PARAMDEF:
753 case MONO_CUSTOM_ATTR_INTERFACE:
754 table = "InterfaceImpl";
756 case MONO_CUSTOM_ATTR_MEMBERREF:
759 case MONO_CUSTOM_ATTR_MODULE:
762 case MONO_CUSTOM_ATTR_PERMISSION:
763 table = "DeclSecurity?";
765 case MONO_CUSTOM_ATTR_PROPERTY:
768 case MONO_CUSTOM_ATTR_EVENT:
771 case MONO_CUSTOM_ATTR_SIGNATURE:
772 table = "StandAloneSignature";
774 case MONO_CUSTOM_ATTR_MODULEREF:
777 case MONO_CUSTOM_ATTR_TYPESPEC:
780 case MONO_CUSTOM_ATTR_ASSEMBLY:
783 case MONO_CUSTOM_ATTR_ASSEMBLYREF:
784 table = "AssemblyRef";
786 case MONO_CUSTOM_ATTR_FILE:
789 case MONO_CUSTOM_ATTR_EXP_TYPE:
790 table = "ExportedType";
792 case MONO_CUSTOM_ATTR_MANIFEST:
795 case MONO_CUSTOM_ATTR_GENERICPAR:
796 table = "GenericParam";
803 * FIXME: we should decode the index into something more uman-friendly.
805 return g_strdup_printf ("%s: %d", table, index);
809 custom_attr_params (MonoImage *m, MonoMethodSignature* sig, const char* value)
811 int len, i, slen, type;
814 const char *p = value;
816 len = mono_metadata_decode_value (p, &p);
817 if (len < 2 || read16 (p) != 0x0001) /* Prolog */
818 return g_strdup ("");
822 res = g_string_new ("");
823 for (i = 0; i < sig->param_count; ++i) {
825 g_string_append (res, ", ");
826 type = sig->params [i]->type;
830 g_string_append_printf (res, "%d", (unsigned int)*p);
834 g_string_append_printf (res, "%d", *p);
837 case MONO_TYPE_BOOLEAN:
838 g_string_append_printf (res, "%s", *p?"true":"false");
842 g_string_append_printf (res, "'%c'", read16 (p));
846 g_string_append_printf (res, "%d", read16 (p));
850 g_string_append_printf (res, "%d", (gint16)read16 (p));
854 g_string_append_printf (res, "%d", read32 (p));
858 g_string_append_printf (res, "%d", (gint32)read32 (p));
862 g_string_append_printf (res, "%lld", (long long)read64 (p));
866 g_string_append_printf (res, "%lld", (long long)read64 (p));
875 g_string_append_printf (res, "(00 00 80 ff)"); /* negative infinity */
877 g_string_append_printf (res, "(00 00 80 7f)"); /* positive infinity */
878 else if (isnan (val))
879 g_string_append_printf (res, "(00 00 c0 ff)"); /* NaN */
881 g_string_append_printf (res, "%g", val);
892 g_string_append_printf (res, "(00 00 00 00 00 00 f0 ff)"); /* negative infinity */
894 g_string_append_printf (res, "(00 00 00 00 00 00 f0 7f)"); /* positive infinity */
895 else if (isnan (val))
896 g_string_append_printf (res, "(00 00 00 00 00 00 f8 ff)"); /* NaN */
898 g_string_append_printf (res, "%g", val);
902 case MONO_TYPE_VALUETYPE:
903 if (mono_class_is_enum (sig->params [i]->data.klass)) {
904 type = mono_class_enum_basetype (sig->params [i]->data.klass)->type;
907 g_warning ("generic valutype not handled in custom attr value decoding");
910 case MONO_TYPE_CLASS: /* It must be a Type: check? */
911 case MONO_TYPE_STRING:
912 if (*p == (char)0xff) {
913 g_string_append (res, "null");
917 slen = mono_metadata_decode_value (p, &p);
918 g_string_append_c (res, '"');
919 g_string_append (res, p);
920 g_string_append_c (res, '"');
924 g_warning ("Type %02x not handled in custom attr value decoding", sig->params [i]->type);
930 g_string_append_printf (res, " %d named args: (", slen);
931 slen = len - (p - value) + 1;
932 for (i = 0; i < slen; ++i) {
933 g_string_append_printf (res, " %02X", (p [i] & 0xff));
935 g_string_append_c (res, ')');
938 g_string_free (res, FALSE);
943 dump_table_customattr (MonoImage *m)
945 MonoTableInfo *t = &m->tables [MONO_TABLE_CUSTOMATTRIBUTE];
948 fprintf (output, "Custom Attributes Table (1..%d)\n", t->rows);
949 for (i = 1; i <= t->rows; i++) {
950 guint32 cols [MONO_CUSTOM_ATTR_SIZE];
957 mono_metadata_decode_row (t, i - 1, cols, MONO_CUSTOM_ATTR_SIZE);
958 desc = has_cattr_get_table (m, cols [MONO_CUSTOM_ATTR_PARENT]);
959 mtoken = cols [MONO_CUSTOM_ATTR_TYPE] >> MONO_CUSTOM_ATTR_TYPE_BITS;
960 switch (cols [MONO_CUSTOM_ATTR_TYPE] & MONO_CUSTOM_ATTR_TYPE_MASK) {
961 case MONO_CUSTOM_ATTR_TYPE_METHODDEF:
962 mtoken |= MONO_TOKEN_METHOD_DEF;
964 case MONO_CUSTOM_ATTR_TYPE_MEMBERREF:
965 mtoken |= MONO_TOKEN_MEMBER_REF;
968 g_warning ("Unknown table for custom attr type %08x", cols [MONO_CUSTOM_ATTR_TYPE]);
971 method = get_method (m, mtoken, NULL);
972 meth = mono_get_method (m, mtoken, NULL);
973 params = custom_attr_params (m, mono_method_signature (meth), mono_metadata_blob_heap (m, cols [MONO_CUSTOM_ATTR_VALUE]));
974 fprintf (output, "%d: %s: %s [%s]\n", i, desc, method, params);
982 dump_table_nestedclass (MonoImage *m)
984 MonoTableInfo *t = &m->tables [MONO_TABLE_NESTEDCLASS];
985 guint32 cols [MONO_NESTED_CLASS_SIZE];
987 char *nested, *nesting;
988 fprintf (output, "NestedClass Table (1..%d)\n", t->rows);
990 for (i = 1; i <= t->rows; i++){
991 mono_metadata_decode_row (t, i - 1, cols, MONO_NESTED_CLASS_SIZE);
992 nested = get_typedef (m, cols [MONO_NESTED_CLASS_NESTED]);
993 nesting = get_typedef (m, cols [MONO_NESTED_CLASS_ENCLOSING]);
994 fprintf (output, "%d: %d %d: %s in %s\n", i,
995 cols [MONO_NESTED_CLASS_NESTED],
996 cols [MONO_NESTED_CLASS_ENCLOSING], nested, nesting);
1004 dump_table_exported (MonoImage *m)
1006 MonoTableInfo *t = &m->tables [MONO_TABLE_EXPORTEDTYPE];
1007 guint32 cols [MONO_EXP_TYPE_SIZE];
1009 const char *name, *nspace;
1011 guint32 index, flags;
1012 fprintf (output, "ExportedType Table (1..%d)\n", t->rows);
1014 for (i = 1; i <= t->rows; i++) {
1015 mono_metadata_decode_row (t, i - 1, cols, MONO_EXP_TYPE_SIZE);
1016 name = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAME]);
1017 nspace = mono_metadata_string_heap (m, cols [MONO_EXP_TYPE_NAMESPACE]);
1018 impl = get_manifest_implementation (m, cols [MONO_EXP_TYPE_IMPLEMENTATION]);
1019 index = cols [MONO_EXP_TYPE_TYPEDEF];
1020 flags = cols [MONO_EXP_TYPE_FLAGS];
1021 fprintf (output, "%d: %s%s%s is in %s, index=%x, flags=0x%x\n", i, nspace, *nspace ? "." : "", name, impl, index, flags);
1028 dump_blob (MonoImage *m, const char* blob)
1032 bsize = mono_metadata_decode_blob_size (blob, &blob);
1034 for (j = 0; j < bsize; j++) {
1035 fprintf (output, "%02x ", blob [j] & 0xff);
1040 dump_table_field_marshal (MonoImage *m)
1042 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDMARSHAL];
1043 guint32 cols [MONO_FIELD_MARSHAL_SIZE];
1044 int i, is_field, idx;
1048 fprintf (output, "FieldMarshal Table (1..%d)\n", t->rows);
1050 for (i = 1; i <= t->rows; i++) {
1051 mono_metadata_decode_row (t, i - 1, cols, MONO_FIELD_MARSHAL_SIZE);
1052 blob = mono_metadata_blob_heap (m, cols [MONO_FIELD_MARSHAL_NATIVE_TYPE]);
1053 native = get_marshal_info (m, blob);
1054 is_field = (cols [MONO_FIELD_MARSHAL_PARENT] & MONO_HAS_FIELD_MARSHAL_MASK) == MONO_HAS_FIELD_MARSHAL_FIELDSREF;
1055 idx = cols [MONO_FIELD_MARSHAL_PARENT] >> MONO_HAS_FIELD_MARSHAL_BITS;
1056 fprintf (output, "%d: (0x%04x) %s %d: %s\n", i, cols [MONO_FIELD_MARSHAL_PARENT], is_field? "Field" : "Param", idx, native);
1057 fprintf (output, "\tblob encoding: ");
1058 dump_blob (m, blob);
1059 fprintf (output, "\n");
1066 get_security_action (int val) {
1067 static char buf [32];
1070 case SECURITY_ACTION_DEMAND:
1072 case SECURITY_ACTION_ASSERT:
1074 case SECURITY_ACTION_DENY:
1076 case SECURITY_ACTION_PERMITONLY:
1077 return "PermitOnly";
1078 case SECURITY_ACTION_LINKDEMAND:
1079 return "LinkDemand";
1080 case SECURITY_ACTION_INHERITDEMAND:
1081 return "InheritanceDemand";
1082 case SECURITY_ACTION_REQMIN:
1083 return "RequestMinimum";
1084 case SECURITY_ACTION_REQOPT:
1085 return "RequestOptional";
1086 case SECURITY_ACTION_REQREFUSE:
1087 return "RequestRefuse";
1088 /* Special actions (for non CAS permissions) */
1089 case SECURITY_ACTION_NONCASDEMAND:
1090 return "NonCasDemand";
1091 case SECURITY_ACTION_NONCASLINKDEMAND:
1092 return "NonCasLinkDemand";
1093 case SECURITY_ACTION_NONCASINHERITANCE:
1094 return "NonCasInheritance";
1095 /* Fx 2.0 actions (for both CAS and non-CAS permissions) */
1096 case SECURITY_ACTION_LINKDEMANDCHOICE:
1097 return "LinkDemandChoice";
1098 case SECURITY_ACTION_INHERITDEMANDCHOICE:
1099 return "InheritanceDemandChoice";
1100 case SECURITY_ACTION_DEMANDCHOICE:
1101 return "DemandChoice";
1103 g_snprintf (buf, sizeof (buf), "0x%04X", val);
1109 dump_table_declsec (MonoImage *m)
1111 MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
1112 guint32 cols [MONO_DECL_SECURITY_SIZE];
1115 const char *blob, *action;
1116 const char* parent[] = {
1117 "TypeDef", "MethodDef", "Assembly", ""
1120 fprintf (output, "DeclSecurity Table (1..%d)\n", t->rows);
1122 for (i = 1; i <= t->rows; i++) {
1123 mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
1124 blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
1125 len = mono_metadata_decode_blob_size (blob, &blob);
1126 action = get_security_action (cols [MONO_DECL_SECURITY_ACTION]);
1127 idx = cols [MONO_DECL_SECURITY_PARENT];
1128 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");
1131 if (blob [0] == MONO_DECLSEC_FORMAT_20) {
1132 /* 2.0 declarative security format */
1133 char *declsec = dump_declsec_entry20 (m, blob, "\t");
1134 fprintf (output, "%s", declsec);
1137 /* 1.0 declarative security format - Unicode XML */
1138 for (idx = 0; idx < len; ++idx)
1139 fprintf (output, "%c", blob [idx]);
1141 fprintf (output, "\n");
1146 dump_table_genericpar (MonoImage *m)
1148 MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAM];
1149 guint32 cols [MONO_GENERICPARAM_SIZE];
1152 fprintf (output, "GenericParameters (1..%d)\n", t->rows);
1154 for (i = 1; i <= t->rows; i++) {
1156 mono_metadata_decode_row (t, i - 1, cols, MONO_GENERICPARAM_SIZE);
1158 // sig = get_type_or_methdef (m, cols [MONO_GENERICPARAM_OWNER]);
1159 sig = g_strdup_printf ("%x", cols [MONO_GENERICPARAM_OWNER]);
1160 fprintf (output, "%d: %d, flags=%d, owner=%s %s\n", i,
1161 cols [MONO_GENERICPARAM_NUMBER],
1162 cols [MONO_GENERICPARAM_FLAGS], sig,
1163 mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
1169 dump_table_methodspec (MonoImage *m)
1171 MonoTableInfo *t = &m->tables [MONO_TABLE_METHODSPEC];
1172 guint32 cols [MONO_METHODSPEC_SIZE];
1175 fprintf (output, "MethodSpec (1..%d)\n", t->rows);
1177 for (i = 1; i <= t->rows; i++) {
1182 mono_metadata_decode_row (t, i - 1, cols, MONO_METHODSPEC_SIZE);
1184 /* build a methodspec token to get the method */
1185 token = MONO_TOKEN_METHOD_SPEC | i;
1186 method = get_method (m, token, NULL);
1188 sig = get_method_type_param (m, cols [MONO_METHODSPEC_SIGNATURE], NULL);
1189 fprintf (output, "%d: %s, %s\n", i, method, sig);
1196 dump_table_parconstraint (MonoImage *m)
1198 MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
1199 guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
1202 fprintf (output, "Generic Param Constraint (1..%d)\n", t->rows);
1204 for (i = 1; i <= t->rows; i++) {
1206 mono_metadata_decode_row (t, i - 1, cols, MONO_GENPARCONSTRAINT_SIZE);
1208 // sig = get_typedef_or_ref (m, cols [MONO_GENPARCONSTRAINT_CONSTRAINT], NULL);
1209 sig = g_strdup_printf ("%x", cols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
1210 fprintf (output, "%d: gen-par=%d, Constraint=%s\n", i,
1211 cols [MONO_GENPARCONSTRAINT_GENERICPAR], sig);
1217 dump_stream_blob (MonoImage *m)
1221 fprintf (output, "Blob heap contents\n");
1223 for (i = 0; i < m->heap_blob.size; i++) {
1226 fprintf (output, "\n");
1227 else if ((i % 8) == 0)
1228 fprintf (output, "- ");
1230 fprintf (output, "%02x ", m->heap_blob.data [i] & 0xff);
1233 fprintf (output, "\n");
1237 dump_stream_strings (MonoImage *m)
1241 fprintf (output, "Strings heap contents\n");
1243 for (i = 0; i < m->heap_strings.size; ) {
1244 const char *str = mono_metadata_string_heap (m, i);
1245 fprintf (output, "%02x: \"%s\"\n", i, str);
1246 i += strlen (str) + 1;
1251 dump_stream_us (MonoImage *m)
1255 fprintf (output, "User Strings heap contents\n");
1257 for (i = 0; i < m->heap_us.size; ) {
1258 const char *us_ptr = mono_metadata_user_string (m, i);
1259 int len = mono_metadata_decode_blob_size (us_ptr, (const char**)&us_ptr);
1261 char *str = get_encoded_user_string_or_bytearray ((const guchar*)us_ptr, len);
1262 fprintf (output, "%02x: %s\n", i, str);
1269 dump_table_standalonesig (MonoImage *m)
1271 MonoTableInfo *t = &m->tables [MONO_TABLE_STANDALONESIG];
1272 guint32 cols [MONO_STAND_ALONE_SIGNATURE_SIZE];
1275 fprintf (output, "Stand alone signature (1..%d)\n", t->rows);
1277 for (i = 1; i <= t->rows; i++) {
1278 const char *locals_ptr;
1281 mono_metadata_decode_row (t, i - 1, cols, MONO_STAND_ALONE_SIGNATURE_SIZE);
1283 locals_ptr = mono_metadata_blob_heap (m, cols [MONO_STAND_ALONE_SIGNATURE]);
1284 bsize = mono_metadata_decode_blob_size (locals_ptr, &locals_ptr);
1286 fprintf (output, "%d: blob[0x%x] = ", i, cols [MONO_STAND_ALONE_SIGNATURE]);
1288 for (j = 0; j < bsize; j++) {
1289 fprintf (output, "%02x ", locals_ptr [j] & 0xff);
1291 fprintf (output, "\n");
1296 dump_table_ptr (MonoImage *m, int table, const char *name)
1298 MonoTableInfo *t = &m->tables [table];
1302 fprintf (output, "%s (1..%d)\n", name, t->rows);
1304 for (i = 1; i <= t->rows; i++) {
1305 mono_metadata_decode_row (t, i - 1, cols, 1);
1307 fprintf (output, "%d: %d\n", i, cols [0]);
1312 dump_table_methodptr (MonoImage *m)
1314 dump_table_ptr (m, MONO_TABLE_METHOD_POINTER, "Method Ptr");
1318 dump_table_fieldptr (MonoImage *m)
1320 dump_table_ptr (m, MONO_TABLE_FIELD_POINTER, "Field Ptr");
1324 dump_table_paramptr (MonoImage *m)
1326 dump_table_ptr (m, MONO_TABLE_PARAM_POINTER, "Param Ptr");
1330 dump_table_eventptr (MonoImage *m)
1332 dump_table_ptr (m, MONO_TABLE_EVENT_POINTER, "Event Ptr");
1336 dump_table_propertyptr (MonoImage *m)
1338 dump_table_ptr (m, MONO_TABLE_PROPERTY_POINTER, "Property Ptr");