2 * main.c: Sample disassembler
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
10 * Investigate how interface inheritance works and how it should be dumped.
11 * Structs are not being labeled as `valuetype' classes
13 * How are fields with literals mapped to constants?
28 #include <mono/metadata/class-internals.h>
29 #include <mono/metadata/object-internals.h>
30 #include <mono/metadata/loader.h>
31 #include <mono/metadata/assembly.h>
32 #include <mono/metadata/appdomain.h>
34 static void setup_filter (MonoImage *image);
35 static gboolean should_include_type (int idx);
36 static gboolean should_include_method (int idx);
37 static gboolean should_include_field (int idx);
41 /* True if you want to get a dump of the header data */
42 gboolean dump_header_data_p = FALSE;
44 /* True if you want to get forward declarations */
45 gboolean dump_forward_decls = FALSE;
47 /* True if you want to dump managed resources as files */
48 gboolean dump_managed_resources = FALSE;
50 gboolean substitute_with_mscorlib_p = FALSE;
55 dump_header_data (MonoImage *img)
57 if (!dump_header_data_p)
61 "// Ximian's CIL disassembler, version 1.0\n"
62 "// Copyright (C) 2001 Ximian, Inc.\n\n");
66 dump_cattrs_list (GList *list, const char *indent)
70 for (tmp = list; tmp; tmp = tmp->next) {
71 fprintf (output, "%s%s\n", indent, (char*)tmp->data);
78 dump_cattrs (MonoImage *m, guint32 token, const char *indent)
82 list = dis_get_custom_attrs (m, token);
83 dump_cattrs_list (list, indent);
87 get_il_security_action (int val)
92 case SECURITY_ACTION_DEMAND:
94 case SECURITY_ACTION_ASSERT:
96 case SECURITY_ACTION_DENY:
98 case SECURITY_ACTION_PERMITONLY:
100 case SECURITY_ACTION_LINKDEMAND:
102 case SECURITY_ACTION_INHERITDEMAND:
103 return "inheritcheck";
104 case SECURITY_ACTION_REQMIN:
106 case SECURITY_ACTION_REQOPT:
108 case SECURITY_ACTION_REQREFUSE:
110 /* Special actions (for non CAS permissions) */
111 case SECURITY_ACTION_NONCASDEMAND:
112 return "noncasdemand";
113 case SECURITY_ACTION_NONCASLINKDEMAND:
114 return "noncaslinkdemand";
115 case SECURITY_ACTION_NONCASINHERITANCE:
116 return "noncasinheritance";
117 /* Fx 2.0 actions (for both CAS and non-CAS permissions) */
118 case SECURITY_ACTION_LINKDEMANDCHOICE:
119 return "linkdemandor";
120 case SECURITY_ACTION_INHERITDEMANDCHOICE:
121 return "inheritancedemandor";
122 case SECURITY_ACTION_DEMANDCHOICE:
125 g_snprintf (buf, sizeof (buf), "0x%04X", val);
130 #define OBJECT_TYPE_TYPEDEF 0
131 #define OBJECT_TYPE_METHODDEF 1
132 #define OBJECT_TYPE_ASSEMBLYDEF 2
135 dump_declarative_security (MonoImage *m, guint32 objectType, guint32 token, const char *indent)
137 MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
138 guint32 cols [MONO_DECL_SECURITY_SIZE];
141 const char *blob, *action;
143 for (i = 1; i <= t->rows; i++) {
144 mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
145 blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
146 len = mono_metadata_decode_blob_size (blob, &blob);
147 action = get_il_security_action (cols [MONO_DECL_SECURITY_ACTION]);
148 idx = cols [MONO_DECL_SECURITY_PARENT];
149 if (((idx & MONO_HAS_DECL_SECURITY_MASK) == objectType) && ((idx >> MONO_HAS_DECL_SECURITY_BITS) == token)) {
151 if (blob [0] == MONO_DECLSEC_FORMAT_20) {
152 /* 2.0 declarative security format */
153 dump = dump_declsec_entry20 (m, blob, indent);
154 fprintf (output, "%s.permissionset %s = %s\n", indent, action, dump);
156 /* 1.x declarative security metadata format */
157 dump = data_dump (blob, len, indent);
158 fprintf (output, "%s.permissionset %s = %s", indent, action, dump);
166 dis_directive_assembly (MonoImage *m)
168 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
169 guint32 cols [MONO_ASSEMBLY_SIZE];
174 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
176 fprintf (output, ".assembly '%s'\n{\n",
177 mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
178 dump_cattrs (m, MONO_TOKEN_ASSEMBLY | 1, " ");
179 dump_declarative_security (m, OBJECT_TYPE_ASSEMBLYDEF, 1, " ");
181 " .hash algorithm 0x%08x\n"
182 " .ver %d:%d:%d:%d\n",
183 cols [MONO_ASSEMBLY_HASH_ALG],
184 cols [MONO_ASSEMBLY_MAJOR_VERSION], cols [MONO_ASSEMBLY_MINOR_VERSION],
185 cols [MONO_ASSEMBLY_BUILD_NUMBER], cols [MONO_ASSEMBLY_REV_NUMBER]);
186 if (cols [MONO_ASSEMBLY_CULTURE])
187 fprintf (output, " .locale %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]));
188 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
189 const char* b = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
190 int len = mono_metadata_decode_blob_size (b, &b);
191 char *dump = data_dump (b, len, "\t\t");
192 fprintf (output, " .publickey =%s", dump);
195 fprintf (output, "}\n");
199 dis_directive_assemblyref (MonoImage *m)
201 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
202 guint32 cols [MONO_ASSEMBLYREF_SIZE];
208 for (i = 0; i < t->rows; i++){
211 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
213 esc = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
216 ".assembly extern %s\n"
218 " .ver %d:%d:%d:%d\n",
220 cols [MONO_ASSEMBLYREF_MAJOR_VERSION], cols [MONO_ASSEMBLYREF_MINOR_VERSION],
221 cols [MONO_ASSEMBLYREF_BUILD_NUMBER], cols [MONO_ASSEMBLYREF_REV_NUMBER]
223 dump_cattrs (m, MONO_TOKEN_ASSEMBLY_REF | (i + 1), " ");
224 if (cols [MONO_ASSEMBLYREF_CULTURE]){
225 fprintf (output, " .locale %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_CULTURE]));
227 if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]){
228 const char* b = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLYREF_PUBLIC_KEY]);
229 int len = mono_metadata_decode_blob_size (b, &b);
230 char *dump = data_dump (b, len, "\t\t");
231 fprintf (output, " .publickeytoken =%s", dump);
234 fprintf (output, "}\n");
240 dis_directive_module (MonoImage *m)
242 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
245 for (i = 0; i < t->rows; i++){
246 guint32 cols [MONO_MODULE_SIZE];
250 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
252 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
253 ename = get_escaped_name (name);
254 guid = get_guid (m, cols [MONO_MODULE_MVID]);
255 fprintf (output, ".module %s // GUID = %s\n\n", ename, guid);
258 dump_cattrs (m, MONO_TOKEN_MODULE | (i + 1), "");
263 dis_directive_moduleref (MonoImage *m)
265 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
268 for (i = 0; i < t->rows; i++){
269 guint32 cols [MONO_MODULEREF_SIZE];
271 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
273 fprintf (output, ".module extern '%s'\n", mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]));
279 dis_nt_header (MonoImage *m)
281 MonoCLIImageInfo *image_info = m->image_info;
282 if (image_info && image_info->cli_header.nt.pe_stack_reserve != 0x100000)
283 fprintf (output, ".stackreserve 0x%x\n", image_info->cli_header.nt.pe_stack_reserve);
287 dis_directive_file (MonoImage *m)
289 MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
293 entry_point = mono_image_get_entry_point (m);
295 for (i = 0; i < t->rows; i++){
296 guint32 cols [MONO_FILE_SIZE];
297 const char *name, *hash;
300 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
302 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
304 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
305 len = mono_metadata_decode_blob_size (hash, &hash);
307 fprintf (output, ".file %s%s .hash = (",
308 cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA ? "nometadata " : "", name);
310 for (j = 0; j < len; ++j)
311 fprintf (output, " %02X", hash [j] & 0xff);
313 token = mono_metadata_make_token (MONO_TABLE_FILE, i + 1);
314 fprintf (output, " )%s\n", (token == entry_point) ? " .entrypoint" : "");
320 dis_directive_mresource (MonoImage *m)
322 MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
325 for (i = 0; i < t->rows; i++){
326 guint32 cols [MONO_MANIFEST_SIZE];
328 guint32 impl, idx, name_token;
330 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
332 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
334 fprintf (output, ".mresource %s '%s'\n", (cols [MONO_MANIFEST_FLAGS] & MANIFEST_RESOURCE_VISIBILITY_MASK) == (MANIFEST_RESOURCE_PUBLIC) ? "public" : "private", name);
335 fprintf (output, "{\n");
336 impl = cols [MONO_MANIFEST_IMPLEMENTATION];
338 idx = impl >> MONO_IMPLEMENTATION_BITS;
339 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
340 name_token = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_FILE], idx - 1, MONO_FILE_NAME);
342 fprintf (output, " .file '%s' at 0x0\n", mono_metadata_string_heap (m, name_token));
344 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
345 name_token = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_ASSEMBLYREF], idx - 1, MONO_ASSEMBLYREF_NAME);
346 fprintf (output, " .assembly extern '%s'\n", mono_metadata_string_heap (m, name_token));
349 fprintf (output, "}\n");
354 static dis_map_t visibility_map [] = {
355 { TYPE_ATTRIBUTE_NOT_PUBLIC, "private " },
356 { TYPE_ATTRIBUTE_PUBLIC, "public " },
357 { TYPE_ATTRIBUTE_NESTED_PUBLIC, "nested public " },
358 { TYPE_ATTRIBUTE_NESTED_PRIVATE, "nested private " },
359 { TYPE_ATTRIBUTE_NESTED_FAMILY, "nested family " },
360 { TYPE_ATTRIBUTE_NESTED_ASSEMBLY, "nested assembly " },
361 { TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM, "nested famandassem " },
362 { TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM, "nested famorassem " },
366 static dis_map_t layout_map [] = {
367 { TYPE_ATTRIBUTE_AUTO_LAYOUT, "auto " },
368 { TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT, "sequential " },
369 { TYPE_ATTRIBUTE_EXPLICIT_LAYOUT, "explicit " },
373 static dis_map_t format_map [] = {
374 { TYPE_ATTRIBUTE_ANSI_CLASS, "ansi " },
375 { TYPE_ATTRIBUTE_UNICODE_CLASS, "unicode " },
376 { TYPE_ATTRIBUTE_AUTO_CLASS, "auto " },
381 typedef_flags (guint32 flags)
383 static char buffer [1024];
384 int visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
385 int layout = flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
386 int format = flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK;
390 strcat (buffer, map (visibility, visibility_map));
391 strcat (buffer, map (layout, layout_map));
392 strcat (buffer, map (format, format_map));
394 if (flags & TYPE_ATTRIBUTE_ABSTRACT)
395 strcat (buffer, "abstract ");
396 if (flags & TYPE_ATTRIBUTE_SEALED)
397 strcat (buffer, "sealed ");
398 if (flags & TYPE_ATTRIBUTE_SPECIAL_NAME)
399 strcat (buffer, "specialname ");
400 if (flags & TYPE_ATTRIBUTE_IMPORT)
401 strcat (buffer, "import ");
402 if (flags & TYPE_ATTRIBUTE_SERIALIZABLE)
403 strcat (buffer, "serializable ");
404 if (flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
405 strcat (buffer, "beforefieldinit ");
406 if (flags & TYPE_ATTRIBUTE_FORWARDER)
407 strcat (buffer, "forwarder ");
414 * @m: metadata context
415 * @start: starting index into the Field Table.
416 * @end: ending index into Field table.
418 * This routine displays all the decoded fields from @start to @end
421 dis_field_list (MonoImage *m, guint32 start, guint32 end, MonoGenericContext *context)
423 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
424 guint32 cols [MONO_FIELD_SIZE];
430 if (end > t->rows + 1) {
431 g_warning ("ERROR index out of range in fields");
435 for (i = start; i < end; i++){
436 char *sig, *flags, *attrs = NULL;
437 char *marshal_str = NULL;
438 guint32 field_offset = -1;
440 if (!should_include_field (i + 1))
442 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
443 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE], context);
444 flags = field_flags (cols [MONO_FIELD_FLAGS]);
446 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL) {
448 MonoMarshalSpec *spec;
450 tp = mono_metadata_get_marshal_info (m, i, TRUE);
451 spec = mono_metadata_parse_marshal_spec (m, tp);
452 marshal_str = dis_stringify_marshal_spec (spec);
455 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
456 mono_metadata_field_info (m, i, NULL, &rva, NULL);
457 g_snprintf (rva_desc, sizeof (rva_desc), " at D_%08x", rva);
462 mono_metadata_field_info (m, i, &field_offset, NULL, NULL);
463 if (field_offset != -1)
464 attrs = g_strdup_printf ("[%d]", field_offset);
465 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
466 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_DEFAULT){
468 guint32 const_cols [MONO_CONSTANT_SIZE];
471 if ((crow = mono_metadata_get_constant_index (m, MONO_TOKEN_FIELD_DEF | (i+1), 0))) {
472 mono_metadata_decode_row (&m->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
473 lit = get_constant (m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
475 lit = g_strdup ("not found");
478 fprintf (output, " .field %s%s%s %s = ",
479 flags, marshal_str ? marshal_str : " ", sig, esname);
480 fprintf (output, "%s\n", lit);
483 fprintf (output, " .field %s %s%s%s %s%s\n",
484 attrs? attrs: "", flags, marshal_str ? marshal_str : " ", sig, esname, rva_desc);
487 g_free (marshal_str);
490 dump_cattrs (m, MONO_TOKEN_FIELD_DEF | (i + 1), " ");
494 static dis_map_t method_access_map [] = {
495 { METHOD_ATTRIBUTE_COMPILER_CONTROLLED, "privatescope " },
496 { METHOD_ATTRIBUTE_PRIVATE, "private " },
497 { METHOD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
498 { METHOD_ATTRIBUTE_ASSEM, "assembly " },
499 { METHOD_ATTRIBUTE_FAMILY, "family " },
500 { METHOD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
501 { METHOD_ATTRIBUTE_PUBLIC, "public " },
505 static dis_map_t method_flags_map [] = {
506 { METHOD_ATTRIBUTE_STATIC, "static " },
507 { METHOD_ATTRIBUTE_FINAL, "final " },
508 { METHOD_ATTRIBUTE_VIRTUAL, "virtual " },
509 { METHOD_ATTRIBUTE_HIDE_BY_SIG, "hidebysig " },
510 { METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK, "newslot " },
511 { METHOD_ATTRIBUTE_ABSTRACT, "abstract " },
512 { METHOD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
513 { METHOD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
514 { METHOD_ATTRIBUTE_UNMANAGED_EXPORT, "export " },
515 /* MS ilasm doesn't compile this statement - is must be added automagically when permissionset are present */
516 /* { METHOD_ATTRIBUTE_HAS_SECURITY, "hassecurity" }, */
517 { METHOD_ATTRIBUTE_REQUIRE_SEC_OBJECT, "requiresecobj" },
518 { METHOD_ATTRIBUTE_PINVOKE_IMPL, "pinvokeimpl " },
525 * Returns a stringified version of the Method's flags
528 method_flags (guint32 f)
530 GString *str = g_string_new ("");
531 int access = f & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK;
534 g_string_append (str, map (access, method_access_map));
535 g_string_append (str, flags (f, method_flags_map));
538 g_string_free (str, FALSE);
543 static dis_map_t pinvoke_flags_map [] = {
544 { PINVOKE_ATTRIBUTE_NO_MANGLE , "nomangle " },
545 { PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR, "lasterr " },
546 { PINVOKE_ATTRIBUTE_BEST_FIT_ENABLED, "bestfit:on" },
547 { PINVOKE_ATTRIBUTE_BEST_FIT_DISABLED, "bestfit:off" },
548 { PINVOKE_ATTRIBUTE_THROW_ON_UNMAPPABLE_ENABLED, "charmaperror:on" },
549 { PINVOKE_ATTRIBUTE_THROW_ON_UNMAPPABLE_DISABLED, "charmaperror:off" },
553 static dis_map_t pinvoke_call_conv_map [] = {
554 { PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI, "winapi " },
555 { PINVOKE_ATTRIBUTE_CALL_CONV_CDECL, "cdecl " },
556 { PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL, "stdcall " },
557 { PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL, "thiscall " },
558 { PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL, "fastcall " },
563 static dis_map_t pinvoke_char_set_map [] = {
564 { PINVOKE_ATTRIBUTE_CHAR_SET_NOT_SPEC, "" },
565 { PINVOKE_ATTRIBUTE_CHAR_SET_ANSI, "ansi " },
566 { PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE , "unicode " },
567 { PINVOKE_ATTRIBUTE_CHAR_SET_AUTO, "autochar " },
574 * Returns a stringified version of the Method's pinvoke flags
577 pinvoke_flags (guint32 f)
579 GString *str = g_string_new ("");
580 int cset = f & PINVOKE_ATTRIBUTE_CHAR_SET_MASK;
581 int cconv = f & PINVOKE_ATTRIBUTE_CALL_CONV_MASK;
584 g_string_append (str, map (cset, pinvoke_char_set_map));
585 g_string_append (str, map (cconv, pinvoke_call_conv_map));
586 g_string_append (str, flags (f, pinvoke_flags_map));
588 s = g_strdup(str->str);
589 g_string_free (str, FALSE);
594 static dis_map_t method_impl_map [] = {
595 { METHOD_IMPL_ATTRIBUTE_IL, "cil " },
596 { METHOD_IMPL_ATTRIBUTE_NATIVE, "native " },
597 { METHOD_IMPL_ATTRIBUTE_OPTIL, "optil " },
598 { METHOD_IMPL_ATTRIBUTE_RUNTIME, "runtime " },
602 static dis_map_t managed_type_map [] = {
603 { METHOD_IMPL_ATTRIBUTE_UNMANAGED, "unmanaged " },
604 { METHOD_IMPL_ATTRIBUTE_MANAGED, "managed " },
608 static dis_map_t managed_impl_flags [] = {
609 { METHOD_IMPL_ATTRIBUTE_FORWARD_REF, "fwdref " },
610 { METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG, "preservesig " },
611 { METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL, "internalcall " },
612 { METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED, "synchronized " },
613 { METHOD_IMPL_ATTRIBUTE_NOINLINING, "noinlining " },
618 method_impl_flags (guint32 f)
620 GString *str = g_string_new ("");
622 int code_type = f & METHOD_IMPL_ATTRIBUTE_CODE_TYPE_MASK;
623 int managed_type = f & METHOD_IMPL_ATTRIBUTE_MANAGED_MASK;
625 g_string_append (str, map (code_type, method_impl_map));
626 g_string_append (str, map (managed_type, managed_type_map));
627 g_string_append (str, flags (f, managed_impl_flags));
630 g_string_free (str, FALSE);
635 dis_locals (MonoImage *m, MonoMethodHeader *mh, const char *ptr)
640 unsigned char flags = *(const unsigned char *) ptr;
641 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
643 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
646 g_assert (format == METHOD_HEADER_FAT_FORMAT);
647 fat_flags = read16 (ptr);
649 hsize = (fat_flags >> 12) & 0xf;
650 max_stack = read16 (ptr);
652 code_size = read32 (ptr);
654 local_var_sig_tok = read32 (ptr);
657 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
662 fprintf(output, "\t.locals /*%08x*/ %s(\n",
663 local_var_sig_tok, init_locals ? "init " : "");
665 fprintf(output, "\t.locals %s(\n", mh->init_locals ? "init " : "");
667 for (i=0; i < mh->num_locals; ++i) {
670 fprintf(output, ",\n");
671 /* print also byref and pinned attributes */
672 desc = dis_stringify_type (m, mh->locals[i], TRUE);
673 fprintf(output, "\t\t%s\tV_%d", desc, i);
676 fprintf(output, ")\n");
680 dis_code (MonoImage *m, guint32 token, guint32 rva, MonoGenericContext *context)
682 MonoMethodHeader *mh;
683 const char *ptr = mono_image_rva_map (m, rva);
691 override = get_method_override (m, token, context);
693 fprintf (output, "\t.override %s\n", override);
697 mh = mono_metadata_parse_mh_full (m, context ? context->container : NULL, ptr);
698 if ((entry_point = mono_image_get_entry_point (m))){
699 loc = mono_metadata_locate_token (m, entry_point);
700 if (rva == read32 (loc))
701 fprintf (output, "\t.entrypoint\n");
704 fprintf (output, "\t// Code size %d (0x%x)\n", mh->code_size, mh->code_size);
705 fprintf (output, "\t.maxstack %d\n", mh->max_stack);
707 dis_locals (m, mh, ptr);
708 disassemble_cil (m, mh, context);
711 hex_dump (mh->code, 0, mh->code_size);
712 printf ("\nAfter the code\n");
713 hex_dump (mh->code + mh->code_size, 0, 64);
715 mono_metadata_free_mh (mh);
719 pinvoke_info (MonoImage *m, guint32 mindex)
721 MonoTableInfo *im = &m->tables [MONO_TABLE_IMPLMAP];
722 MonoTableInfo *mr = &m->tables [MONO_TABLE_MODULEREF];
723 guint32 im_cols [MONO_IMPLMAP_SIZE];
724 guint32 mr_cols [MONO_MODULEREF_SIZE];
725 const char *import, *scope;
729 for (i = 0; i < im->rows; i++) {
731 mono_metadata_decode_row (im, i, im_cols, MONO_IMPLMAP_SIZE);
733 if ((im_cols [MONO_IMPLMAP_MEMBER] >> 1) == mindex + 1) {
735 flags = pinvoke_flags (im_cols [MONO_IMPLMAP_FLAGS]);
737 import = mono_metadata_string_heap (m, im_cols [MONO_IMPLMAP_NAME]);
739 mono_metadata_decode_row (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1,
740 mr_cols, MONO_MODULEREF_SIZE);
742 scope = mono_metadata_string_heap (m, mr_cols [MONO_MODULEREF_NAME]);
744 return g_strdup_printf ("(\"%s\" as \"%s\" %s)", scope, import,
754 * dump_cattrs_for_type_params
757 * @token: TypeOrMethodDef token, owner for GenericParam
759 * Dumps the custom attributes for @token's type parameters
762 dump_cattrs_for_type_params (MonoImage *m, guint32 token, const char *indent)
764 MonoTableInfo *tdef = &m->tables [MONO_TABLE_GENERICPARAM];
765 guint32 cols [MONO_GENERICPARAM_SIZE];
766 guint32 owner = 0, i;
769 if (! (i = mono_metadata_get_generic_param_row (m, token, &owner)))
772 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
774 list = dis_get_custom_attrs (m, mono_metadata_make_token (MONO_TABLE_GENERICPARAM, i));
776 fprintf (output, "%s.param type %s\n", indent, mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
777 dump_cattrs_list (list, indent);
780 if (++i > tdef->rows)
782 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
783 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
787 dump_cattrs_for_method_params (MonoImage *m, guint32 midx, MonoMethodSignature *sig) {
788 MonoTableInfo *methodt;
789 MonoTableInfo *paramt;
790 guint param_index, lastp, i;
792 methodt = &m->tables [MONO_TABLE_METHOD];
793 paramt = &m->tables [MONO_TABLE_PARAM];
794 param_index = mono_metadata_decode_row_col (methodt, midx, MONO_METHOD_PARAMLIST);
795 if (midx + 1 < methodt->rows)
796 lastp = mono_metadata_decode_row_col (methodt, midx + 1, MONO_METHOD_PARAMLIST);
798 lastp = paramt->rows + 1;
799 for (i = param_index; i < lastp; ++i) {
802 guint32 param_cols [MONO_PARAM_SIZE];
805 list = dis_get_custom_attrs (m, MONO_TOKEN_PARAM_DEF | i);
807 mono_metadata_decode_row (paramt, i-1, param_cols, MONO_PARAM_SIZE);
808 if (!(param_cols[MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_DEFAULT)) {
810 fprintf (output, "\t.param [%d]\n", param_cols[MONO_PARAM_SEQUENCE]);
812 fprintf (output, "\t.param [%d] = ", param_cols[MONO_PARAM_SEQUENCE]);
814 if ((crow = mono_metadata_get_constant_index(m, MONO_TOKEN_PARAM_DEF | i, 0))) {
815 guint32 const_cols [MONO_CONSTANT_SIZE];
816 mono_metadata_decode_row( &m->tables[MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
817 lit = get_constant(m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
820 lit = g_strdup ("not found");
822 fprintf(output, "%s\n", lit);
825 dump_cattrs_list (list, "\t");
831 * @m: metadata context
832 * @start: starting index into the Method Table.
833 * @end: ending index into Method table.
835 * This routine displays the methods in the Method Table from @start to @end
838 dis_method_list (const char *klass_name, MonoImage *m, guint32 start, guint32 end, MonoGenericContext *context)
840 MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
841 guint32 cols [MONO_METHOD_SIZE];
845 fprintf (output, "ERROR index out of range in methods");
850 for (i = start; i < end; i++){
851 MonoMethodSignature *ms;
852 MonoGenericContainer *container;
853 MonoGenericContext *method_context = context;
854 char *flags, *impl_flags;
855 const char *sig, *method_name;
859 if (!should_include_method (i + 1))
861 mono_metadata_decode_row (t, i, cols, MONO_METHOD_SIZE);
863 flags = method_flags (cols [MONO_METHOD_FLAGS]);
864 impl_flags = method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
866 sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
867 mono_metadata_decode_blob_size (sig, &sig);
869 container = mono_metadata_load_generic_params (
870 m, MONO_TOKEN_METHOD_DEF | (i + 1), context ? context->container : NULL);
872 mono_metadata_load_generic_param_constraints (
873 m, MONO_TOKEN_METHOD_DEF | (i + 1), container);
874 method_context = (MonoGenericContext *) container;
877 ms = mono_metadata_parse_method_signature_full (m, method_context ? method_context->container : NULL, i + 1, sig, &sig);
878 sig_str = dis_stringify_method_signature (m, ms, i + 1, method_context, FALSE);
879 method_name = mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]);
881 fprintf (output, " // method line %d\n", i + 1);
882 fprintf (output, " .method %s", flags);
884 if ((cols [MONO_METHOD_FLAGS] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (cols [MONO_METHOD_RVA] == 0)) {
885 gchar *pi = pinvoke_info (m, i);
887 fprintf (output, "%s", pi);
892 fprintf (output, "\n %s", sig_str);
893 fprintf (output, " %s\n", impl_flags);
897 token = MONO_TOKEN_METHOD_DEF | (i + 1);
899 fprintf (output, " {\n");
900 dump_cattrs (m, token, " ");
901 dump_cattrs_for_type_params (m, MONO_TOKEN_METHOD_DEF | (i + 1), " ");
902 dump_cattrs_for_method_params (m, i, ms);
904 fprintf (output, " // Method begins at RVA 0x%x\n", cols [MONO_METHOD_RVA]);
905 dump_declarative_security (m, OBJECT_TYPE_METHODDEF, i + 1, " ");
906 if (cols [MONO_METHOD_IMPLFLAGS] & METHOD_IMPL_ATTRIBUTE_NATIVE)
907 fprintf (output, " // Disassembly of native methods is not supported\n");
909 dis_code (m, token, cols [MONO_METHOD_RVA], method_context);
911 fprintf (output, " } // end of method %s::%s\n\n", klass_name, method_name);
913 fprintf (output, " } // end of global method %s\n\n", method_name);
914 mono_metadata_free_method_signature (ms);
927 table_locator (const void *a, const void *b)
929 plocator_t *loc = (plocator_t *) a;
930 const char *bb = (const char *) b;
931 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
934 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
936 if (loc->idx == col) {
937 loc->result = table_index;
947 dis_property_methods (MonoImage *m, guint32 prop, MonoGenericContext *context)
950 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
951 guint32 cols [MONO_METHOD_SEMA_SIZE];
953 const char *type[] = {NULL, ".set", ".get", NULL, ".other"};
955 start = mono_metadata_methods_from_property (m, prop, &end);
956 for (; start < end; ++start) {
957 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
958 if (!should_include_method (cols [MONO_METHOD_SEMA_METHOD]))
960 sig = dis_stringify_method_signature_full (m, NULL, cols [MONO_METHOD_SEMA_METHOD], context, TRUE, FALSE);
961 fprintf (output, "\t\t%s %s\n", type [cols [MONO_METHOD_SEMA_SEMANTICS]], sig);
966 dis_property_signature (MonoImage *m, guint32 prop_idx, MonoGenericContext *context)
968 MonoTableInfo *propt = &m->tables [MONO_TABLE_PROPERTY];
971 guint32 cols [MONO_PROPERTY_SIZE];
977 GString *res = g_string_new ("");
979 mono_metadata_decode_row (propt, prop_idx, cols, MONO_PROPERTY_SIZE);
980 name = mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]);
981 prop_flags = cols [MONO_PROPERTY_FLAGS];
982 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
983 mono_metadata_decode_blob_size (ptr, &ptr);
985 g_warning("incorrect signature in property blob: 0x%x", *ptr);
987 g_string_append (res, "instance ");
989 pcount = mono_metadata_decode_value (ptr, &ptr);
990 type = mono_metadata_parse_type_full (m, context ? context->container : NULL, MONO_PARSE_TYPE, 0, ptr, &ptr);
991 blurb = dis_stringify_type (m, type, TRUE);
992 if (prop_flags & 0x0200)
993 g_string_append (res, "specialname ");
994 if (prop_flags & 0x0400)
995 g_string_append (res, "rtspecialname ");
996 qk = get_escaped_name (name);
997 g_string_sprintfa (res, "%s %s (", blurb, qk);
1000 mono_metadata_free_type (type);
1001 for (i = 0; i < pcount; i++) {
1003 g_string_append (res, ", ");
1004 param = mono_metadata_parse_type_full (m, context ? context->container : NULL, MONO_PARSE_PARAM, 0, ptr, &ptr);
1005 blurb = dis_stringify_param (m, param);
1006 g_string_append (res, blurb);
1007 mono_metadata_free_type (param);
1010 g_string_append_c (res, ')');
1012 g_string_free (res, FALSE);
1018 dis_property_list (MonoImage *m, guint32 typedef_row, MonoGenericContext *context)
1020 guint start, end, i;
1021 start = mono_metadata_properties_from_typedef (m, typedef_row, &end);
1023 for (i = start; i < end; ++i) {
1024 char *sig = dis_property_signature (m, i, context);
1025 fprintf (output, "\t.property %s\n\t{\n", sig);
1026 dump_cattrs (m, MONO_TOKEN_PROPERTY | (i + 1), "\t\t");
1027 dis_property_methods (m, i, context);
1028 fprintf (output, "\t}\n");
1034 dis_event_signature (MonoImage *m, guint32 event_idx, MonoGenericContext *context)
1036 MonoTableInfo *et = &m->tables [MONO_TABLE_EVENT];
1037 char *type, *result, *esname;
1038 guint32 cols [MONO_EVENT_SIZE];
1040 GString *res = g_string_new ("");
1042 mono_metadata_decode_row (et, event_idx, cols, MONO_EVENT_SIZE);
1043 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]));
1044 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE], context);
1045 event_flags = cols [MONO_EVENT_FLAGS];
1047 if (event_flags & 0x0200)
1048 g_string_append (res, "specialname ");
1049 if (event_flags & 0x0400)
1050 g_string_append (res, "rtspecialname ");
1051 g_string_sprintfa (res, "%s %s", type, esname);
1056 g_string_free (res, FALSE);
1061 dis_event_methods (MonoImage *m, guint32 event, MonoGenericContext *context)
1064 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
1065 guint32 cols [MONO_METHOD_SEMA_SIZE];
1067 const char *type = "";
1069 start = mono_metadata_methods_from_event (m, event, &end);
1070 for (; start < end; ++start) {
1071 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
1072 if (!should_include_method (cols [MONO_METHOD_SEMA_METHOD]))
1074 sig = dis_stringify_method_signature_full (m, NULL, cols [MONO_METHOD_SEMA_METHOD], context, TRUE, FALSE);
1075 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1076 case METHOD_SEMANTIC_OTHER:
1077 type = ".other"; break;
1078 case METHOD_SEMANTIC_ADD_ON:
1079 type = ".addon"; break;
1080 case METHOD_SEMANTIC_REMOVE_ON:
1081 type = ".removeon"; break;
1082 case METHOD_SEMANTIC_FIRE:
1083 type = ".fire"; break;
1087 fprintf (output, "\t\t%s %s\n", type, sig);
1093 dis_event_list (MonoImage *m, guint32 typedef_row, MonoGenericContext *context)
1095 guint start, end, i;
1096 start = mono_metadata_events_from_typedef (m, typedef_row, &end);
1098 for (i = start; i < end; ++i) {
1099 char *sig = dis_event_signature (m, i, context);
1100 fprintf (output, "\t.event %s\n\t{\n", sig);
1101 dump_cattrs (m, MONO_TOKEN_EVENT | (i + 1), "\t\t");
1102 dis_event_methods (m, i, context);
1103 fprintf (output, "\t}\n");
1109 dis_interfaces (MonoImage *m, guint32 typedef_row, MonoGenericContext *context)
1113 gboolean first_interface = 1;
1114 guint32 cols [MONO_INTERFACEIMPL_SIZE];
1116 MonoTableInfo *table = &m->tables [MONO_TABLE_INTERFACEIMPL];
1122 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
1123 loc.idx = typedef_row;
1125 if (!bsearch (&loc, table->base, table->rows, table->row_size, table_locator))
1130 * We may end up in the middle of the rows...
1133 if (loc.idx == mono_metadata_decode_row_col (table, start - 1, MONO_INTERFACEIMPL_CLASS))
1138 while (start < table->rows) {
1139 mono_metadata_decode_row (table, start, cols, MONO_INTERFACEIMPL_SIZE);
1140 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
1142 intf = get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE], context);
1143 if (first_interface) {
1144 fprintf (output, " \timplements %s", intf);
1145 first_interface = 0;
1147 fprintf (output, ", %s", intf);
1156 * @m: metadata context
1157 * @n: index of type to disassemble
1158 * @is_nested: nested type ?
1159 * @forward: forward declarations?
1161 * Disassembles the type whose index in the TypeDef table is @n.
1164 dis_type (MonoImage *m, int n, int is_nested, int forward)
1166 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1167 guint32 cols [MONO_TYPEDEF_SIZE];
1168 guint32 cols_next [MONO_TYPEDEF_SIZE];
1169 const char *name, *nspace;
1170 char *esname, *param;
1171 MonoGenericContainer *container;
1172 guint32 packing_size, class_size;
1173 gboolean next_is_valid, last;
1176 if (!should_include_type (n + 1))
1178 mono_metadata_decode_row (t, n, cols, MONO_TYPEDEF_SIZE);
1180 if (t->rows > n + 1) {
1181 mono_metadata_decode_row (t, n + 1, cols_next, MONO_TYPEDEF_SIZE);
1186 name = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]);
1187 nspace = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
1188 if (*nspace && !is_nested)
1189 fprintf (output, ".namespace %s\n{\n", nspace);
1191 container = mono_metadata_load_generic_params (m, MONO_TOKEN_TYPE_DEF | (n + 1), NULL);
1193 mono_metadata_load_generic_param_constraints (m, MONO_TOKEN_TYPE_DEF | (n + 1), container);
1195 esname = get_escaped_name (name);
1196 if ((cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_CLASS_SEMANTIC_MASK) == TYPE_ATTRIBUTE_CLASS){
1197 fprintf (output, " .class %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
1199 param = get_generic_param (m, container);
1201 fprintf (output, param);
1204 fprintf (output, "\n");
1205 if (cols [MONO_TYPEDEF_EXTENDS]) {
1206 char *base = get_typedef_or_ref (
1207 m, cols [MONO_TYPEDEF_EXTENDS], (MonoGenericContext *) container);
1208 fprintf (output, " \textends %s\n", base);
1212 fprintf (output, " .class interface %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
1214 param = get_generic_param (m, container);
1216 fprintf (output, param);
1219 fprintf (output, "\n");
1223 dis_interfaces (m, n + 1, (MonoGenericContext *) container);
1224 fprintf (output, " {\n");
1226 dump_cattrs (m, MONO_TOKEN_TYPE_DEF | (n + 1), " ");
1227 dump_cattrs_for_type_params (m, MONO_TOKEN_TYPE_DEF | (n + 1), " ");
1228 dump_declarative_security (m, OBJECT_TYPE_TYPEDEF, (n + 1), " ");
1230 if (mono_metadata_packing_from_typedef (m, n + 1, &packing_size, &class_size)) {
1231 fprintf (output, " .pack %d\n", packing_size);
1232 fprintf (output, " .size %d\n", class_size);
1235 * The value in the table is always valid, we know we have fields
1236 * if the value stored is different than the next record.
1240 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1242 last = m->tables [MONO_TABLE_FIELD].rows;
1244 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
1245 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last, (MonoGenericContext *) container);
1246 fprintf (output, "\n");
1249 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1251 last = m->tables [MONO_TABLE_METHOD].rows;
1253 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
1254 dis_method_list (name, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last, (MonoGenericContext *) container);
1256 dis_property_list (m, n, (MonoGenericContext *) container);
1257 dis_event_list (m, n, (MonoGenericContext *) container);
1260 t = &m->tables [MONO_TABLE_NESTEDCLASS];
1261 nested = mono_metadata_nesting_typedef (m, n + 1, 1);
1263 dis_type (m, mono_metadata_decode_row_col (t, nested - 1, MONO_NESTED_CLASS_NESTED) - 1, 1, forward);
1264 nested = mono_metadata_nesting_typedef (m, n + 1, nested + 1);
1267 fprintf (output, " } // end of class %s%s%s\n", nspace, *nspace? ".": "", name);
1268 if (*nspace && !is_nested)
1269 fprintf (output, "}\n");
1270 fprintf (output, "\n");
1276 * @m: metadata context
1278 * disassembles all the global fields and methods
1281 dis_globals (MonoImage *m)
1283 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1284 guint32 cols [MONO_TYPEDEF_SIZE];
1285 guint32 cols_next [MONO_TYPEDEF_SIZE];
1286 gboolean next_is_valid, last;
1288 mono_metadata_decode_row (t, 0, cols, MONO_TYPEDEF_SIZE);
1291 mono_metadata_decode_row (t, 1, cols_next, MONO_TYPEDEF_SIZE);
1297 * The value in the table is always valid, we know we have fields
1298 * if the value stored is different than the next record.
1302 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1304 last = m->tables [MONO_TABLE_FIELD].rows;
1306 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
1307 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last, NULL);
1308 fprintf (output, "\n");
1311 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1313 last = m->tables [MONO_TABLE_METHOD].rows;
1315 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
1316 dis_method_list (NULL, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last, NULL);
1321 dis_mresource (MonoImage *m)
1323 MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
1326 for (i = 0; i < t->rows; i++){
1327 guint32 cols [MONO_MANIFEST_SIZE];
1328 const char *name, *res;
1332 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
1333 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
1335 if (! (res = mono_image_get_resource (m, cols [MONO_MANIFEST_OFFSET], &size)))
1338 if ( (fp = fopen (name, "ab")) ) {
1339 if (ftell (fp) == 0)
1340 fwrite (res, size, 1, fp);
1342 g_warning ("Error creating managed resource - %s : File already exists.", name);
1346 g_warning ("Error creating managed resource - %s : %s", name, g_strerror (errno));
1352 * @m: metadata context
1354 * disassembles all types in the @m context
1357 dis_types (MonoImage *m, int forward)
1359 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1365 for (i = 1; i < t->rows; i++) {
1366 flags = mono_metadata_decode_row_col (t, i, MONO_TYPEDEF_FLAGS);
1367 flags &= TYPE_ATTRIBUTE_VISIBILITY_MASK;
1368 if (flags == TYPE_ATTRIBUTE_PUBLIC || flags == TYPE_ATTRIBUTE_NOT_PUBLIC)
1369 dis_type (m, i, 0, forward);
1375 * @m: metadata context
1377 * disassembles all data blobs references in the FieldRVA table in the @m context
1380 dis_data (MonoImage *m)
1382 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
1383 MonoTableInfo *ft = &m->tables [MONO_TABLE_FIELD];
1385 const char *rva, *sig;
1388 guint32 cols [MONO_FIELD_RVA_SIZE];
1391 for (i = 0; i < t->rows; i++) {
1392 mono_metadata_decode_row (t, i, cols, MONO_FIELD_RVA_SIZE);
1393 rva = mono_image_rva_map (m, cols [MONO_FIELD_RVA_RVA]);
1394 sig = mono_metadata_blob_heap (m, mono_metadata_decode_row_col (ft, cols [MONO_FIELD_RVA_FIELD] -1, MONO_FIELD_SIGNATURE));
1395 mono_metadata_decode_value (sig, &sig);
1396 /* FIELD signature == 0x06 */
1397 g_assert (*sig == 0x06);
1398 type = mono_metadata_parse_field_type (m, 0, sig + 1, &sig);
1399 mono_class_init (mono_class_from_mono_type (type));
1400 size = mono_type_size (type, &align);
1401 fprintf (output, ".data D_%08x = bytearray (", cols [MONO_FIELD_RVA_RVA]);
1402 for (b = 0; b < size; ++b) {
1404 fprintf (output, "\n\t");
1405 fprintf (output, " %02X", rva [b] & 0xff);
1407 fprintf (output, ") // size: %d\n", size);
1414 void (*dumper) (MonoImage *m);
1416 { "--assembly", MONO_TABLE_ASSEMBLY, dump_table_assembly },
1417 { "--assemblyref", MONO_TABLE_ASSEMBLYREF, dump_table_assemblyref },
1418 { "--classlayout", MONO_TABLE_CLASSLAYOUT, dump_table_class_layout },
1419 { "--constant", MONO_TABLE_CONSTANT, dump_table_constant },
1420 { "--customattr", MONO_TABLE_CUSTOMATTRIBUTE, dump_table_customattr },
1421 { "--declsec", MONO_TABLE_DECLSECURITY, dump_table_declsec },
1422 { "--event", MONO_TABLE_EVENT, dump_table_event },
1423 { "--exported", MONO_TABLE_EXPORTEDTYPE, dump_table_exported },
1424 { "--fields", MONO_TABLE_FIELD, dump_table_field },
1425 { "--file", MONO_TABLE_FILE, dump_table_file },
1426 { "--genericpar", MONO_TABLE_GENERICPARAM, dump_table_genericpar },
1427 { "--interface", MONO_TABLE_INTERFACEIMPL, dump_table_interfaceimpl },
1428 { "--manifest", MONO_TABLE_MANIFESTRESOURCE, dump_table_manifest },
1429 { "--marshal", MONO_TABLE_FIELDMARSHAL, dump_table_field_marshal },
1430 { "--memberref", MONO_TABLE_MEMBERREF, dump_table_memberref },
1431 { "--method", MONO_TABLE_METHOD, dump_table_method },
1432 { "--methodimpl", MONO_TABLE_METHODIMPL, dump_table_methodimpl },
1433 { "--methodsem", MONO_TABLE_METHODSEMANTICS, dump_table_methodsem },
1434 { "--methodspec", MONO_TABLE_METHODSPEC, dump_table_methodspec },
1435 { "--moduleref", MONO_TABLE_MODULEREF, dump_table_moduleref },
1436 { "--module", MONO_TABLE_MODULE, dump_table_module },
1437 { "--mresources", 0, dis_mresource },
1438 { "--nested", MONO_TABLE_NESTEDCLASS, dump_table_nestedclass },
1439 { "--param", MONO_TABLE_PARAM, dump_table_param },
1440 { "--parconst", MONO_TABLE_GENERICPARAMCONSTRAINT, dump_table_parconstraint },
1441 { "--property", MONO_TABLE_PROPERTY, dump_table_property },
1442 { "--propertymap", MONO_TABLE_PROPERTYMAP, dump_table_property_map },
1443 { "--typedef", MONO_TABLE_TYPEDEF, dump_table_typedef },
1444 { "--typeref", MONO_TABLE_TYPEREF, dump_table_typeref },
1445 { "--typespec", MONO_TABLE_TYPESPEC, dump_table_typespec },
1446 { "--implmap", MONO_TABLE_IMPLMAP, dump_table_implmap },
1447 { "--standalonesig", MONO_TABLE_STANDALONESIG, dump_table_standalonesig },
1448 { "--blob", 0, dump_stream_blob },
1454 * @file: file containing CIL code.
1456 * Disassembles the @file file.
1459 disassemble_file (const char *file)
1462 MonoImageOpenStatus status;
1465 ass = mono_assembly_open (file, &status);
1467 fprintf (stderr, "Error while trying to process %s\n", file);
1475 if (dump_table != -1){
1476 (*table_list [dump_table].dumper) (img);
1478 dump_header_data (img);
1480 dis_directive_assemblyref (img);
1481 dis_directive_assembly (img);
1482 dis_directive_file (img);
1483 dis_directive_mresource (img);
1484 dis_directive_module (img);
1485 dis_directive_moduleref (img);
1486 dis_nt_header (img);
1487 if (dump_managed_resources)
1488 dis_mresource (img);
1489 if (dump_forward_decls) {
1490 fprintf (output, "// *************** Forward Declarations for Classes ***************\n\n");
1492 fprintf (output, "// *************** End-Of Forward Declarations for Classes ***************\n\n");
1498 mono_image_close (img);
1512 TableFilter methods;
1515 static GList *filter_list = NULL;
1516 static ImageFilter *cur_filter = NULL;
1519 setup_filter (MonoImage *image)
1521 ImageFilter *ifilter;
1523 const char *name = mono_image_get_name (image);
1525 for (item = filter_list; item; item = item->next) {
1526 ifilter = item->data;
1527 if (strcmp (ifilter->name, name) == 0) {
1528 cur_filter = ifilter;
1536 int_cmp (const void *e1, const void *e2)
1544 table_includes (TableFilter *tf, int idx)
1548 return bsearch (&idx, tf->elems, tf->count, sizeof (int), int_cmp) != NULL;
1552 should_include_type (int idx)
1556 return table_includes (&cur_filter->types, idx);
1560 should_include_method (int idx)
1564 return table_includes (&cur_filter->methods, idx);
1568 should_include_field (int idx)
1572 return table_includes (&cur_filter->fields, idx);
1576 add_filter (const char *name)
1578 ImageFilter *ifilter;
1581 for (item = filter_list; item; item = item->next) {
1582 ifilter = item->data;
1583 if (strcmp (ifilter->name, name) == 0)
1586 ifilter = g_new0 (ImageFilter, 1);
1587 ifilter->name = g_strdup (name);
1588 filter_list = g_list_prepend (filter_list, ifilter);
1593 add_item (TableFilter *tf, int val)
1595 if (tf->count >= tf->size) {
1598 tf->elems = g_malloc (sizeof (int) * tf->size);
1601 tf->elems = g_realloc (tf->elems, sizeof (int) * tf->size);
1604 tf->elems [tf->count++] = val;
1608 sort_filter_elems (void)
1610 ImageFilter *ifilter;
1613 for (item = filter_list; item; item = item->next) {
1614 ifilter = item->data;
1615 qsort (ifilter->types.elems, ifilter->types.count, sizeof (int), int_cmp);
1616 qsort (ifilter->fields.elems, ifilter->fields.count, sizeof (int), int_cmp);
1617 qsort (ifilter->methods.elems, ifilter->methods.count, sizeof (int), int_cmp);
1622 load_filter (const char* filename)
1626 char *p, *s, *endptr;
1628 ImageFilter *ifilter = NULL;
1631 if (!(file = fopen (filename, "r"))) {
1632 g_print ("Cannot open filter file '%s'\n", filename);
1635 while (fgets (buf, sizeof (buf), file) != NULL) {
1638 while (*s && g_ascii_isspace (*s)) ++s;
1644 p = strchr (s, ']');
1646 g_error ("No matching ']' in filter at line %d\n", line);
1648 ifilter = add_filter (s + 1);
1652 g_error ("Invalid format in filter at line %d\n", line);
1653 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1654 g_error ("Invalid type number in filter at line %d\n", line);
1655 add_item (&ifilter->types, value);
1659 g_error ("Invalid format in filter at line %d\n", line);
1660 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1661 g_error ("Invalid method number in filter at line %d\n", line);
1662 add_item (&ifilter->methods, value);
1666 g_error ("Invalid format in filter at line %d\n", line);
1667 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1668 g_error ("Invalid field number in filter at line %d\n", line);
1669 add_item (&ifilter->fields, value);
1672 g_error ("Invalid format in filter at line %d\n", line);
1676 sort_filter_elems ();
1681 try_load_from (MonoAssembly **assembly, const gchar *path1, const gchar *path2,
1682 const gchar *path3, const gchar *path4, gboolean refonly)
1687 fullpath = g_build_filename (path1, path2, path3, path4, NULL);
1688 if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR))
1689 *assembly = mono_assembly_open_full (fullpath, NULL, refonly);
1692 return (*assembly != NULL);
1695 static MonoAssembly *
1696 real_load (gchar **search_path, const gchar *culture, const gchar *name, gboolean refonly)
1698 MonoAssembly *result = NULL;
1701 const gchar *local_culture;
1704 if (!culture || *culture == '\0') {
1707 local_culture = culture;
1710 filename = g_strconcat (name, ".dll", NULL);
1711 len = strlen (filename);
1713 for (path = search_path; *path; path++) {
1715 continue; /* Ignore empty ApplicationBase */
1717 /* See test cases in bug #58992 and bug #57710 */
1718 /* 1st try: [culture]/[name].dll (culture may be empty) */
1719 strcpy (filename + len - 4, ".dll");
1720 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
1723 /* 2nd try: [culture]/[name].exe (culture may be empty) */
1724 strcpy (filename + len - 4, ".exe");
1725 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
1728 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
1729 strcpy (filename + len - 4, ".dll");
1730 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
1733 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
1734 strcpy (filename + len - 4, ".exe");
1735 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
1744 * Try to load referenced assemblies from assemblies_path.
1746 static MonoAssembly *
1747 monodis_preload (MonoAssemblyName *aname,
1748 gchar **assemblies_path,
1751 MonoAssembly *result = NULL;
1752 gboolean refonly = GPOINTER_TO_UINT (user_data);
1754 if (assemblies_path && assemblies_path [0] != NULL) {
1755 result = real_load (assemblies_path, aname->culture, aname->name, refonly);
1761 static GList *loaded_assemblies = NULL;
1764 monodis_assembly_load_hook (MonoAssembly *assembly, gpointer user_data)
1766 loaded_assemblies = g_list_prepend (loaded_assemblies, assembly);
1769 static MonoAssembly *
1770 monodis_assembly_search_hook (MonoAssemblyName *aname, gpointer user_data)
1774 for (tmp = loaded_assemblies; tmp; tmp = tmp->next) {
1775 MonoAssembly *ass = tmp->data;
1776 if (mono_assembly_names_equal (aname, &ass->aname))
1785 GString *args = g_string_new ("[--output=filename] [--filter=filename] [--help] [--mscorlib]\n");
1788 for (i = 0; table_list [i].name != NULL; i++){
1789 g_string_append (args, "[");
1790 g_string_append (args, table_list [i].name);
1791 g_string_append (args, "] ");
1792 if (((i-2) % 5) == 0)
1793 g_string_append_c (args, '\n');
1795 g_string_append (args, "[--forward-decls]");
1797 "monodis -- Mono Common Intermediate Language Disassembler\n"
1798 "Usage is: monodis %s file ..\n", args->str);
1803 main (int argc, char *argv [])
1805 GList *input_files = NULL, *l;
1810 for (i = 1; i < argc; i++){
1811 if (argv [i][0] == '-'){
1812 if (argv [i][1] == 'h')
1814 else if (argv [i][1] == 'd')
1815 dump_header_data_p = TRUE;
1816 else if (strcmp (argv [i], "--mscorlib") == 0) {
1817 substitute_with_mscorlib_p = TRUE;
1819 } else if (strcmp (argv [i], "--show-method-tokens") == 0) {
1820 show_method_tokens = TRUE;
1822 } else if (strcmp (argv [i], "--show-tokens") == 0) {
1825 } else if (strncmp (argv [i], "--output=", 9) == 0) {
1826 output = fopen (argv [i]+9, "w");
1827 if (output == NULL) {
1828 fprintf (stderr, "Can't open output file `%s': %s\n",
1829 argv [i]+9, strerror (errno));
1832 dump_managed_resources = TRUE;
1834 } else if (strncmp (argv [i], "--filter=", 9) == 0) {
1835 load_filter (argv [i]+9);
1837 } else if (strcmp (argv [i], "--forward-decls") == 0) {
1838 dump_forward_decls = TRUE;
1840 } else if (strcmp (argv [i], "--help") == 0)
1842 for (j = 0; table_list [j].name != NULL; j++) {
1843 if (strcmp (argv [i], table_list [j].name) == 0)
1849 input_files = g_list_append (input_files, argv [i]);
1852 if (input_files == NULL)
1855 mono_install_assembly_load_hook (monodis_assembly_load_hook, NULL);
1856 mono_install_assembly_search_hook (monodis_assembly_search_hook, NULL);
1859 * If we just have one file, use the corlib version it requires.
1861 if (!input_files->next) {
1862 char *filename = input_files->data;
1864 mono_init_from_assembly (argv [0], filename);
1866 mono_install_assembly_preload_hook (monodis_preload, GUINT_TO_POINTER (FALSE));
1868 disassemble_file (filename);
1870 mono_init (argv [0]);
1872 for (l = input_files; l; l = l->next)
1873 disassemble_file (l->data);