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?
27 #include <mono/metadata/class-internals.h>
28 #include <mono/metadata/object-internals.h>
29 #include <mono/metadata/loader.h>
30 #include <mono/metadata/assembly.h>
31 #include <mono/metadata/appdomain.h>
33 static void setup_filter (MonoImage *image);
34 static gboolean should_include_type (int idx);
35 static gboolean should_include_method (int idx);
36 static gboolean should_include_field (int idx);
40 /* True if you want to get a dump of the header data */
41 gboolean dump_header_data_p = FALSE;
43 /* True if you want to get forward declarations */
44 gboolean dump_forward_decls = FALSE;
46 /* True if you want to dump managed resources as files */
47 gboolean dump_managed_resources = FALSE;
49 gboolean substitute_with_mscorlib_p = FALSE;
54 dump_header_data (MonoImage *img)
56 if (!dump_header_data_p)
60 "// Ximian's CIL disassembler, version 1.0\n"
61 "// Copyright (C) 2001 Ximian, Inc.\n\n");
65 dump_cattrs_list (GList *list, const char *indent)
69 for (tmp = list; tmp; tmp = tmp->next) {
70 fprintf (output, "%s%s\n", indent, (char*)tmp->data);
77 dump_cattrs (MonoImage *m, guint32 token, const char *indent)
81 list = dis_get_custom_attrs (m, token);
82 dump_cattrs_list (list, indent);
86 get_il_security_action (int val)
91 case SECURITY_ACTION_DEMAND:
93 case SECURITY_ACTION_ASSERT:
95 case SECURITY_ACTION_DENY:
97 case SECURITY_ACTION_PERMITONLY:
99 case SECURITY_ACTION_LINKDEMAND:
101 case SECURITY_ACTION_INHERITDEMAND:
102 return "inheritcheck";
103 case SECURITY_ACTION_REQMIN:
105 case SECURITY_ACTION_REQOPT:
107 case SECURITY_ACTION_REQREFUSE:
109 /* Special actions (for non CAS permissions) */
110 case SECURITY_ACTION_NONCASDEMAND:
111 return "noncasdemand";
112 case SECURITY_ACTION_NONCASLINKDEMAND:
113 return "noncaslinkdemand";
114 case SECURITY_ACTION_NONCASINHERITANCE:
115 return "noncasinheritance";
116 /* Fx 2.0 actions (for both CAS and non-CAS permissions) */
117 case SECURITY_ACTION_LINKDEMANDCHOICE:
118 return "linkdemandor";
119 case SECURITY_ACTION_INHERITDEMANDCHOICE:
120 return "inheritancedemandor";
121 case SECURITY_ACTION_DEMANDCHOICE:
124 g_snprintf (buf, sizeof (buf), "0x%04X", val);
129 #define OBJECT_TYPE_TYPEDEF 0
130 #define OBJECT_TYPE_METHODDEF 1
131 #define OBJECT_TYPE_ASSEMBLYDEF 2
134 dump_declarative_security (MonoImage *m, guint32 objectType, guint32 token, const char *indent)
136 MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
137 guint32 cols [MONO_DECL_SECURITY_SIZE];
140 const char *blob, *action;
142 for (i = 1; i <= t->rows; i++) {
143 mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
144 blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
145 len = mono_metadata_decode_blob_size (blob, &blob);
146 action = get_il_security_action (cols [MONO_DECL_SECURITY_ACTION]);
147 idx = cols [MONO_DECL_SECURITY_PARENT];
148 if (((idx & MONO_HAS_DECL_SECURITY_MASK) == objectType) && ((idx >> MONO_HAS_DECL_SECURITY_BITS) == token)) {
149 char *dump = data_dump (blob, len, indent);
150 fprintf (output, "%s.permissionset %s = %s", indent, action, dump);
157 dis_directive_assembly (MonoImage *m)
159 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
160 guint32 cols [MONO_ASSEMBLY_SIZE];
165 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
167 fprintf (output, ".assembly '%s'\n{\n",
168 mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
169 dump_cattrs (m, MONO_TOKEN_ASSEMBLY | 1, " ");
170 dump_declarative_security (m, OBJECT_TYPE_ASSEMBLYDEF, 1, " ");
172 " .hash algorithm 0x%08x\n"
173 " .ver %d:%d:%d:%d\n",
174 cols [MONO_ASSEMBLY_HASH_ALG],
175 cols [MONO_ASSEMBLY_MAJOR_VERSION], cols [MONO_ASSEMBLY_MINOR_VERSION],
176 cols [MONO_ASSEMBLY_BUILD_NUMBER], cols [MONO_ASSEMBLY_REV_NUMBER]);
177 if (cols [MONO_ASSEMBLY_CULTURE])
178 fprintf (output, " .locale %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]));
179 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
180 const char* b = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
181 int len = mono_metadata_decode_blob_size (b, &b);
182 char *dump = data_dump (b, len, "\t\t");
183 fprintf (output, " .publickey =%s", dump);
186 fprintf (output, "}\n");
190 dis_directive_assemblyref (MonoImage *m)
192 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
193 guint32 cols [MONO_ASSEMBLYREF_SIZE];
199 for (i = 0; i < t->rows; i++){
202 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
204 esc = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
207 ".assembly extern %s\n"
209 " .ver %d:%d:%d:%d\n",
211 cols [MONO_ASSEMBLYREF_MAJOR_VERSION], cols [MONO_ASSEMBLYREF_MINOR_VERSION],
212 cols [MONO_ASSEMBLYREF_BUILD_NUMBER], cols [MONO_ASSEMBLYREF_REV_NUMBER]
214 if (cols [MONO_ASSEMBLYREF_CULTURE]){
215 fprintf (output, " .locale %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_CULTURE]));
217 if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]){
218 const char* b = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLYREF_PUBLIC_KEY]);
219 int len = mono_metadata_decode_blob_size (b, &b);
220 char *dump = data_dump (b, len, "\t\t");
221 fprintf (output, " .publickeytoken =%s", dump);
224 fprintf (output, "}\n");
230 dis_directive_module (MonoImage *m)
232 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
235 for (i = 0; i < t->rows; i++){
236 guint32 cols [MONO_MODULE_SIZE];
240 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
242 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
243 ename = get_escaped_name (name);
244 guid = get_guid (m, cols [MONO_MODULE_MVID]);
245 fprintf (output, ".module %s // GUID = %s\n\n", ename, guid);
248 dump_cattrs (m, MONO_TOKEN_MODULE | (i + 1), "");
253 dis_directive_moduleref (MonoImage *m)
255 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
258 for (i = 0; i < t->rows; i++){
259 guint32 cols [MONO_MODULEREF_SIZE];
262 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
264 name = mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]);
265 fprintf (output, ".module extern %s\n", name);
271 dis_directive_file (MonoImage *m)
273 MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
277 entry_point = mono_image_get_entry_point (m);
279 for (i = 0; i < t->rows; i++){
280 guint32 cols [MONO_FILE_SIZE];
281 const char *name, *hash;
284 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
286 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
288 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
289 len = mono_metadata_decode_blob_size (hash, &hash);
291 fprintf (output, ".file %s%s .hash = (",
292 cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA ? "nometadata " : "", name);
294 for (j = 0; j < len; ++j)
295 fprintf (output, " %02X", hash [j] & 0xff);
297 token = mono_metadata_make_token (MONO_TABLE_FILE, i + 1);
298 fprintf (output, " )%s\n", (token == entry_point) ? " .entrypoint" : "");
304 dis_directive_mresource (MonoImage *m)
306 MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
309 for (i = 0; i < t->rows; i++){
310 guint32 cols [MONO_MANIFEST_SIZE];
312 guint32 impl, idx, name_token;
314 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
316 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
318 fprintf (output, ".mresource %s '%s'\n", (cols [MONO_MANIFEST_FLAGS] & MANIFEST_RESOURCE_VISIBILITY_MASK) == (MANIFEST_RESOURCE_PUBLIC) ? "public" : "private", name);
319 fprintf (output, "{\n");
320 impl = cols [MONO_MANIFEST_IMPLEMENTATION];
322 idx = impl >> MONO_IMPLEMENTATION_BITS;
323 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
324 name_token = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_FILE], idx - 1, MONO_FILE_NAME);
326 fprintf (output, " .file '%s' at 0x0\n", mono_metadata_string_heap (m, name_token));
328 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
329 name_token = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_ASSEMBLYREF], idx - 1, MONO_ASSEMBLYREF_NAME);
330 fprintf (output, " .assembly extern '%s'\n", mono_metadata_string_heap (m, name_token));
333 fprintf (output, "}\n");
338 static dis_map_t visibility_map [] = {
339 { TYPE_ATTRIBUTE_NOT_PUBLIC, "private " },
340 { TYPE_ATTRIBUTE_PUBLIC, "public " },
341 { TYPE_ATTRIBUTE_NESTED_PUBLIC, "nested public " },
342 { TYPE_ATTRIBUTE_NESTED_PRIVATE, "nested private " },
343 { TYPE_ATTRIBUTE_NESTED_FAMILY, "nested family " },
344 { TYPE_ATTRIBUTE_NESTED_ASSEMBLY, "nested assembly " },
345 { TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM, "nested famandassem " },
346 { TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM, "nested famorassem " },
350 static dis_map_t layout_map [] = {
351 { TYPE_ATTRIBUTE_AUTO_LAYOUT, "auto " },
352 { TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT, "sequential " },
353 { TYPE_ATTRIBUTE_EXPLICIT_LAYOUT, "explicit " },
357 static dis_map_t format_map [] = {
358 { TYPE_ATTRIBUTE_ANSI_CLASS, "ansi " },
359 { TYPE_ATTRIBUTE_UNICODE_CLASS, "unicode " },
360 { TYPE_ATTRIBUTE_AUTO_CLASS, "auto " },
365 typedef_flags (guint32 flags)
367 static char buffer [1024];
368 int visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
369 int layout = flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
370 int format = flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK;
374 strcat (buffer, map (visibility, visibility_map));
375 strcat (buffer, map (layout, layout_map));
376 strcat (buffer, map (format, format_map));
378 if (flags & TYPE_ATTRIBUTE_ABSTRACT)
379 strcat (buffer, "abstract ");
380 if (flags & TYPE_ATTRIBUTE_SEALED)
381 strcat (buffer, "sealed ");
382 if (flags & TYPE_ATTRIBUTE_SPECIAL_NAME)
383 strcat (buffer, "special-name ");
384 if (flags & TYPE_ATTRIBUTE_IMPORT)
385 strcat (buffer, "import ");
386 if (flags & TYPE_ATTRIBUTE_SERIALIZABLE)
387 strcat (buffer, "serializable ");
388 if (flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
389 strcat (buffer, "beforefieldinit ");
396 * @m: metadata context
397 * @start: starting index into the Field Table.
398 * @end: ending index into Field table.
400 * This routine displays all the decoded fields from @start to @end
403 dis_field_list (MonoImage *m, guint32 start, guint32 end, MonoGenericContext *context)
405 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
406 guint32 cols [MONO_FIELD_SIZE];
412 if (end > t->rows + 1) {
413 g_warning ("ERROR index out of range in fields");
417 for (i = start; i < end; i++){
418 char *sig, *flags, *attrs = NULL;
419 guint32 field_offset = -1;
421 if (!should_include_field (i + 1))
423 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
424 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE], context);
425 flags = field_flags (cols [MONO_FIELD_FLAGS]);
427 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
428 mono_metadata_field_info (m, i, NULL, &rva, NULL);
429 g_snprintf (rva_desc, sizeof (rva_desc), " at D_%08x", rva);
434 mono_metadata_field_info (m, i, &field_offset, NULL, NULL);
435 if (field_offset != -1)
436 attrs = g_strdup_printf ("[%d]", field_offset);
437 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
438 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_DEFAULT){
440 guint32 const_cols [MONO_CONSTANT_SIZE];
443 if ((crow = mono_metadata_get_constant_index (m, MONO_TOKEN_FIELD_DEF | (i+1), 0))) {
444 mono_metadata_decode_row (&m->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
445 lit = get_constant (m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
447 lit = g_strdup ("not found");
451 fprintf (output, " .field %s %s %s = ",
453 fprintf (output, "%s\n", lit);
456 fprintf (output, " .field %s %s %s %s%s\n",
457 attrs? attrs: "", flags, sig, esname, rva_desc);
462 dump_cattrs (m, MONO_TOKEN_FIELD_DEF | (i + 1), " ");
466 static dis_map_t method_access_map [] = {
467 { METHOD_ATTRIBUTE_COMPILER_CONTROLLED, "privatescope " },
468 { METHOD_ATTRIBUTE_PRIVATE, "private " },
469 { METHOD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
470 { METHOD_ATTRIBUTE_ASSEM, "assembly " },
471 { METHOD_ATTRIBUTE_FAMILY, "family " },
472 { METHOD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
473 { METHOD_ATTRIBUTE_PUBLIC, "public " },
477 static dis_map_t method_flags_map [] = {
478 { METHOD_ATTRIBUTE_STATIC, "static " },
479 { METHOD_ATTRIBUTE_FINAL, "final " },
480 { METHOD_ATTRIBUTE_VIRTUAL, "virtual " },
481 { METHOD_ATTRIBUTE_HIDE_BY_SIG, "hidebysig " },
482 { METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK, "newslot " },
483 { METHOD_ATTRIBUTE_ABSTRACT, "abstract " },
484 { METHOD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
485 { METHOD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
486 { METHOD_ATTRIBUTE_UNMANAGED_EXPORT, "export " },
487 /* MS ilasm doesn't compile this statement - is must be added automagically when permissionset are present */
488 /* { METHOD_ATTRIBUTE_HAS_SECURITY, "hassecurity" }, */
489 { METHOD_ATTRIBUTE_REQUIRE_SEC_OBJECT, "requiresecobj" },
490 { METHOD_ATTRIBUTE_PINVOKE_IMPL, "pinvokeimpl " },
497 * Returns a stringified version of the Method's flags
500 method_flags (guint32 f)
502 GString *str = g_string_new ("");
503 int access = f & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK;
506 g_string_append (str, map (access, method_access_map));
507 g_string_append (str, flags (f, method_flags_map));
510 g_string_free (str, FALSE);
515 static dis_map_t pinvoke_flags_map [] = {
516 { PINVOKE_ATTRIBUTE_NO_MANGLE , "nomangle " },
517 { PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR, "lasterr " },
518 { PINVOKE_ATTRIBUTE_BEST_FIT_ENABLED, "bestfit:on" },
519 { PINVOKE_ATTRIBUTE_BEST_FIT_DISABLED, "bestfit:off" },
520 { PINVOKE_ATTRIBUTE_THROW_ON_UNMAPPABLE_ENABLED, "charmaperror:on" },
521 { PINVOKE_ATTRIBUTE_THROW_ON_UNMAPPABLE_DISABLED, "charmaperror:off" },
525 static dis_map_t pinvoke_call_conv_map [] = {
526 { PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI, "winapi " },
527 { PINVOKE_ATTRIBUTE_CALL_CONV_CDECL, "cdecl " },
528 { PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL, "stdcall " },
529 { PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL, "thiscall " },
530 { PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL, "fastcall " },
534 static dis_map_t pinvoke_char_set_map [] = {
535 { PINVOKE_ATTRIBUTE_CHAR_SET_NOT_SPEC, "" },
536 { PINVOKE_ATTRIBUTE_CHAR_SET_ANSI, "ansi " },
537 { PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE , "unicode " },
538 { PINVOKE_ATTRIBUTE_CHAR_SET_AUTO, "autochar " },
545 * Returns a stringified version of the Method's pinvoke flags
548 pinvoke_flags (guint32 f)
550 GString *str = g_string_new ("");
551 int cset = f & PINVOKE_ATTRIBUTE_CHAR_SET_MASK;
552 int cconv = f & PINVOKE_ATTRIBUTE_CALL_CONV_MASK;
555 g_string_append (str, map (cset, pinvoke_char_set_map));
556 g_string_append (str, map (cconv, pinvoke_call_conv_map));
557 g_string_append (str, flags (f, pinvoke_flags_map));
559 s = g_strdup(str->str);
560 g_string_free (str, FALSE);
565 static dis_map_t method_impl_map [] = {
566 { METHOD_IMPL_ATTRIBUTE_IL, "cil " },
567 { METHOD_IMPL_ATTRIBUTE_NATIVE, "native " },
568 { METHOD_IMPL_ATTRIBUTE_OPTIL, "optil " },
569 { METHOD_IMPL_ATTRIBUTE_RUNTIME, "runtime " },
573 static dis_map_t managed_type_map [] = {
574 { METHOD_IMPL_ATTRIBUTE_UNMANAGED, "unmanaged " },
575 { METHOD_IMPL_ATTRIBUTE_MANAGED, "managed " },
579 static dis_map_t managed_impl_flags [] = {
580 { METHOD_IMPL_ATTRIBUTE_FORWARD_REF, "fwdref " },
581 { METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG, "preservesig " },
582 { METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL, "internalcall " },
583 { METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED, "synchronized " },
584 { METHOD_IMPL_ATTRIBUTE_NOINLINING, "noinline " },
589 method_impl_flags (guint32 f)
591 GString *str = g_string_new ("");
593 int code_type = f & METHOD_IMPL_ATTRIBUTE_CODE_TYPE_MASK;
594 int managed_type = f & METHOD_IMPL_ATTRIBUTE_MANAGED_MASK;
596 g_string_append (str, map (code_type, method_impl_map));
597 g_string_append (str, map (managed_type, managed_type_map));
598 g_string_append (str, flags (f, managed_impl_flags));
601 g_string_free (str, FALSE);
606 dis_locals (MonoImage *m, MonoMethodHeader *mh, const char *ptr)
611 unsigned char flags = *(const unsigned char *) ptr;
612 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
614 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
617 g_assert (format == METHOD_HEADER_FAT_FORMAT);
618 fat_flags = read16 (ptr);
620 hsize = (fat_flags >> 12) & 0xf;
621 max_stack = read16 (ptr);
623 code_size = read32 (ptr);
625 local_var_sig_tok = read32 (ptr);
628 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
633 fprintf(output, "\t.locals /*%08x*/ %s(\n",
634 local_var_sig_tok, init_locals ? "init " : "");
636 fprintf(output, "\t.locals %s(\n", mh->init_locals ? "init " : "");
638 for (i=0; i < mh->num_locals; ++i) {
641 fprintf(output, ",\n");
642 /* print also byref and pinned attributes */
643 desc = dis_stringify_type (m, mh->locals[i], TRUE);
644 fprintf(output, "\t\t%s\tV_%d", desc, i);
647 fprintf(output, ")\n");
651 dis_code (MonoImage *m, guint32 token, guint32 rva, MonoGenericContext *context)
653 MonoMethodHeader *mh;
654 const char *ptr = mono_image_rva_map (m, rva);
662 override = get_method_override (m, token, context);
664 fprintf (output, "\t.override %s\n", override);
668 mh = mono_metadata_parse_mh_full (m, context, ptr);
669 if ((entry_point = mono_image_get_entry_point (m))){
670 loc = mono_metadata_locate_token (m, entry_point);
671 if (rva == read32 (loc))
672 fprintf (output, "\t.entrypoint\n");
675 fprintf (output, "\t// Code size %d (0x%x)\n", mh->code_size, mh->code_size);
676 fprintf (output, "\t.maxstack %d\n", mh->max_stack);
678 dis_locals (m, mh, ptr);
679 dissasemble_cil (m, mh, context);
682 hex_dump (mh->code, 0, mh->code_size);
683 printf ("\nAfter the code\n");
684 hex_dump (mh->code + mh->code_size, 0, 64);
686 mono_metadata_free_mh (mh);
690 pinvoke_info (MonoImage *m, guint32 mindex)
692 MonoTableInfo *im = &m->tables [MONO_TABLE_IMPLMAP];
693 MonoTableInfo *mr = &m->tables [MONO_TABLE_MODULEREF];
694 guint32 im_cols [MONO_IMPLMAP_SIZE];
695 guint32 mr_cols [MONO_MODULEREF_SIZE];
696 const char *import, *scope;
700 for (i = 0; i < im->rows; i++) {
702 mono_metadata_decode_row (im, i, im_cols, MONO_IMPLMAP_SIZE);
704 if ((im_cols [MONO_IMPLMAP_MEMBER] >> 1) == mindex + 1) {
706 flags = pinvoke_flags (im_cols [MONO_IMPLMAP_FLAGS]);
708 import = mono_metadata_string_heap (m, im_cols [MONO_IMPLMAP_NAME]);
710 mono_metadata_decode_row (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1,
711 mr_cols, MONO_MODULEREF_SIZE);
713 scope = mono_metadata_string_heap (m, mr_cols [MONO_MODULEREF_NAME]);
715 return g_strdup_printf ("(\"%s\" as \"%s\" %s)", scope, import,
725 dump_cattrs_for_method_params (MonoImage *m, guint32 midx, MonoMethodSignature *sig) {
726 MonoTableInfo *methodt;
727 MonoTableInfo *paramt;
728 guint param_index, lastp, i;
730 methodt = &m->tables [MONO_TABLE_METHOD];
731 paramt = &m->tables [MONO_TABLE_PARAM];
732 param_index = mono_metadata_decode_row_col (methodt, midx, MONO_METHOD_PARAMLIST);
733 if (midx + 1 < methodt->rows)
734 lastp = mono_metadata_decode_row_col (methodt, midx + 1, MONO_METHOD_PARAMLIST);
736 lastp = paramt->rows + 1;
737 for (i = param_index; i < lastp; ++i) {
740 guint32 param_cols [MONO_PARAM_SIZE];
743 list = dis_get_custom_attrs (m, MONO_TOKEN_PARAM_DEF | i);
745 mono_metadata_decode_row (paramt, i-1, param_cols, MONO_PARAM_SIZE);
746 if (!(param_cols[MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_DEFAULT)) {
748 fprintf (output, "\t.param [%d]\n", param_cols[MONO_PARAM_SEQUENCE]);
750 fprintf (output, "\t.param [%d] = ", param_cols[MONO_PARAM_SEQUENCE]);
752 if ((crow = mono_metadata_get_constant_index(m, MONO_TOKEN_PARAM_DEF | i, 0))) {
753 guint32 const_cols [MONO_CONSTANT_SIZE];
754 mono_metadata_decode_row( &m->tables[MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
755 lit = get_constant(m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
758 lit = g_strdup ("not found");
760 fprintf(output, "%s\n", lit);
763 dump_cattrs_list (list, "\t");
769 * @m: metadata context
770 * @start: starting index into the Method Table.
771 * @end: ending index into Method table.
773 * This routine displays the methods in the Method Table from @start to @end
776 dis_method_list (const char *klass_name, MonoImage *m, guint32 start, guint32 end, MonoGenericContext *context)
778 MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
779 guint32 cols [MONO_METHOD_SIZE];
783 fprintf (output, "ERROR index out of range in methods");
788 for (i = start; i < end; i++){
789 MonoMethodSignature *ms;
790 MonoGenericContainer *container;
791 MonoGenericContext *method_context = context;
792 char *flags, *impl_flags;
797 if (!should_include_method (i + 1))
799 mono_metadata_decode_row (t, i, cols, MONO_METHOD_SIZE);
801 flags = method_flags (cols [MONO_METHOD_FLAGS]);
802 impl_flags = method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
804 sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
805 mono_metadata_decode_blob_size (sig, &sig);
807 container = mono_metadata_load_generic_params (
808 m, MONO_TOKEN_METHOD_DEF | (i + 1), context ? context->container : NULL);
810 method_context = (MonoGenericContext *) container;
812 ms = mono_metadata_parse_method_signature_full (m, method_context, i + 1, sig, &sig);
813 sig_str = dis_stringify_method_signature (m, ms, i + 1, method_context, FALSE);
815 fprintf (output, " // method line %d\n", i + 1);
816 fprintf (output, " .method %s", flags);
818 if ((cols [MONO_METHOD_FLAGS] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (cols [MONO_METHOD_RVA] == 0)) {
819 gchar *pi = pinvoke_info (m, i);
821 fprintf (output, "%s", pi);
826 fprintf (output, "\n %s", sig_str);
827 fprintf (output, " %s\n", impl_flags);
831 token = MONO_TOKEN_METHOD_DEF | (i + 1);
833 fprintf (output, " {\n");
834 dump_cattrs (m, token, " ");
835 dump_cattrs_for_method_params (m, i, ms);
836 /* FIXME: need to sump also param custom attributes */
837 fprintf (output, " // Method begins at RVA 0x%x\n", cols [MONO_METHOD_RVA]);
838 dump_declarative_security (m, OBJECT_TYPE_METHODDEF, i + 1, " ");
839 if (cols [MONO_METHOD_IMPLFLAGS] & METHOD_IMPL_ATTRIBUTE_NATIVE)
840 fprintf (output, " // Disassembly of native methods is not supported\n");
842 dis_code (m, token, cols [MONO_METHOD_RVA], method_context);
843 fprintf (output, " } // end of method %s::%s\n\n", klass_name, sig_str);
844 mono_metadata_free_method_signature (ms);
857 table_locator (const void *a, const void *b)
859 plocator_t *loc = (plocator_t *) a;
860 const char *bb = (const char *) b;
861 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
864 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
866 if (loc->idx == col) {
867 loc->result = table_index;
877 dis_property_methods (MonoImage *m, guint32 prop, MonoGenericContext *context)
880 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
881 guint32 cols [MONO_METHOD_SEMA_SIZE];
883 const char *type[] = {NULL, ".set", ".get", NULL, ".other"};
885 start = mono_metadata_methods_from_property (m, prop, &end);
886 for (; start < end; ++start) {
887 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
888 if (!should_include_method (cols [MONO_METHOD_SEMA_METHOD]))
890 sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD], context, TRUE);
891 fprintf (output, "\t\t%s %s\n", type [cols [MONO_METHOD_SEMA_SEMANTICS]], sig);
896 dis_property_signature (MonoImage *m, guint32 prop_idx, MonoGenericContext *context)
898 MonoTableInfo *propt = &m->tables [MONO_TABLE_PROPERTY];
901 guint32 cols [MONO_PROPERTY_SIZE];
907 GString *res = g_string_new ("");
909 mono_metadata_decode_row (propt, prop_idx, cols, MONO_PROPERTY_SIZE);
910 name = mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]);
911 prop_flags = cols [MONO_PROPERTY_FLAGS];
912 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
913 mono_metadata_decode_blob_size (ptr, &ptr);
914 /* ECMA claims 0x08 ... */
915 if (*ptr != 0x28 && *ptr != 0x08)
916 g_warning("incorrect signature in propert blob: 0x%x", *ptr);
918 pcount = mono_metadata_decode_value (ptr, &ptr);
919 type = mono_metadata_parse_type_full (m, context, MONO_PARSE_TYPE, 0, ptr, &ptr);
920 blurb = dis_stringify_type (m, type, TRUE);
921 if (prop_flags & 0x0200)
922 g_string_append (res, "specialname ");
923 if (prop_flags & 0x0400)
924 g_string_append (res, "rtspecialname ");
925 qk = get_escaped_name (name);
926 g_string_sprintfa (res, "%s %s (", blurb, qk);
929 mono_metadata_free_type (type);
930 for (i = 0; i < pcount; i++) {
932 g_string_append (res, ", ");
933 param = mono_metadata_parse_type_full (m, context, MONO_PARSE_PARAM, 0, ptr, &ptr);
934 blurb = dis_stringify_param (m, param);
935 g_string_append (res, blurb);
936 mono_metadata_free_type (param);
939 g_string_append_c (res, ')');
941 g_string_free (res, FALSE);
947 dis_property_list (MonoImage *m, guint32 typedef_row, MonoGenericContext *context)
950 start = mono_metadata_properties_from_typedef (m, typedef_row, &end);
952 for (i = start; i < end; ++i) {
953 char *sig = dis_property_signature (m, i, context);
954 fprintf (output, "\t.property %s\n\t{\n", sig);
955 dump_cattrs (m, MONO_TOKEN_PROPERTY | (i + 1), "\t\t");
956 dis_property_methods (m, i, context);
957 fprintf (output, "\t}\n");
963 dis_event_signature (MonoImage *m, guint32 event_idx, MonoGenericContext *context)
965 MonoTableInfo *et = &m->tables [MONO_TABLE_EVENT];
966 char *type, *result, *esname;
967 guint32 cols [MONO_EVENT_SIZE];
969 GString *res = g_string_new ("");
971 mono_metadata_decode_row (et, event_idx, cols, MONO_EVENT_SIZE);
972 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]));
973 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE], context);
974 event_flags = cols [MONO_EVENT_FLAGS];
976 if (event_flags & 0x0200)
977 g_string_append (res, "specialname ");
978 if (event_flags & 0x0400)
979 g_string_append (res, "rtspecialname ");
980 g_string_sprintfa (res, "%s %s", type, esname);
985 g_string_free (res, FALSE);
990 dis_event_methods (MonoImage *m, guint32 event, MonoGenericContext *context)
993 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
994 guint32 cols [MONO_METHOD_SEMA_SIZE];
996 const char *type = "";
998 start = mono_metadata_methods_from_event (m, event, &end);
999 for (; start < end; ++start) {
1000 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
1001 if (!should_include_method (cols [MONO_METHOD_SEMA_METHOD]))
1003 sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD], context, TRUE);
1004 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1005 case METHOD_SEMANTIC_OTHER:
1006 type = ".other"; break;
1007 case METHOD_SEMANTIC_ADD_ON:
1008 type = ".addon"; break;
1009 case METHOD_SEMANTIC_REMOVE_ON:
1010 type = ".removeon"; break;
1011 case METHOD_SEMANTIC_FIRE:
1012 type = ".fire"; break;
1016 fprintf (output, "\t\t%s %s\n", type, sig);
1022 dis_event_list (MonoImage *m, guint32 typedef_row, MonoGenericContext *context)
1024 guint start, end, i;
1025 start = mono_metadata_events_from_typedef (m, typedef_row, &end);
1027 for (i = start; i < end; ++i) {
1028 char *sig = dis_event_signature (m, i, context);
1029 fprintf (output, "\t.event %s\n\t{\n", sig);
1030 dump_cattrs (m, MONO_TOKEN_EVENT | (i + 1), "\t\t");
1031 dis_event_methods (m, i, context);
1032 fprintf (output, "\t}\n");
1038 dis_interfaces (MonoImage *m, guint32 typedef_row, MonoGenericContext *context)
1042 gboolean first_interface = 1;
1043 guint32 cols [MONO_INTERFACEIMPL_SIZE];
1045 MonoTableInfo *table = &m->tables [MONO_TABLE_INTERFACEIMPL];
1051 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
1052 loc.idx = typedef_row;
1054 if (!bsearch (&loc, table->base, table->rows, table->row_size, table_locator))
1059 * We may end up in the middle of the rows...
1062 if (loc.idx == mono_metadata_decode_row_col (table, start - 1, MONO_INTERFACEIMPL_CLASS))
1067 while (start < table->rows) {
1068 mono_metadata_decode_row (table, start, cols, MONO_INTERFACEIMPL_SIZE);
1069 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
1071 intf = get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE], context);
1072 if (first_interface) {
1073 fprintf (output, " \timplements %s", intf);
1074 first_interface = 0;
1076 fprintf (output, ", %s", intf);
1085 * @m: metadata context
1086 * @n: index of type to disassemble
1087 * @is_nested: nested type ?
1088 * @forward: forward declarations?
1090 * Disassembles the type whose index in the TypeDef table is @n.
1093 dis_type (MonoImage *m, int n, int is_nested, int forward)
1095 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1096 guint32 cols [MONO_TYPEDEF_SIZE];
1097 guint32 cols_next [MONO_TYPEDEF_SIZE];
1098 const char *name, *nspace;
1099 char *esname, *param;
1100 MonoGenericContainer *container;
1101 guint32 packing_size, class_size;
1102 gboolean next_is_valid, last;
1105 if (!should_include_type (n + 1))
1107 mono_metadata_decode_row (t, n, cols, MONO_TYPEDEF_SIZE);
1109 if (t->rows > n + 1) {
1110 mono_metadata_decode_row (t, n + 1, cols_next, MONO_TYPEDEF_SIZE);
1115 name = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]);
1116 nspace = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
1117 if (*nspace && !is_nested)
1118 fprintf (output, ".namespace %s\n{\n", nspace);
1120 container = mono_metadata_load_generic_params (m, MONO_TOKEN_TYPE_DEF | (n + 1), NULL);
1122 esname = get_escaped_name (name);
1123 if ((cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_CLASS_SEMANTIC_MASK) == TYPE_ATTRIBUTE_CLASS){
1124 fprintf (output, " .class %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
1126 param = get_generic_param (m, container);
1128 fprintf (output, param);
1131 fprintf (output, "\n");
1132 if (cols [MONO_TYPEDEF_EXTENDS]) {
1133 char *base = get_typedef_or_ref (
1134 m, cols [MONO_TYPEDEF_EXTENDS], (MonoGenericContext *) container);
1135 fprintf (output, " \textends %s\n", base);
1139 fprintf (output, " .class interface %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
1141 param = get_generic_param (m, container);
1143 fprintf (output, param);
1146 fprintf (output, "\n");
1150 dis_interfaces (m, n + 1, (MonoGenericContext *) container);
1151 fprintf (output, " {\n");
1153 dump_cattrs (m, MONO_TOKEN_TYPE_DEF | (n + 1), " ");
1154 dump_declarative_security (m, OBJECT_TYPE_TYPEDEF, (n + 1), " ");
1156 if (mono_metadata_packing_from_typedef (m, n + 1, &packing_size, &class_size)) {
1157 fprintf (output, " .pack %d\n", packing_size);
1158 fprintf (output, " .size %d\n", class_size);
1161 * The value in the table is always valid, we know we have fields
1162 * if the value stored is different than the next record.
1166 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1168 last = m->tables [MONO_TABLE_FIELD].rows;
1170 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
1171 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last, (MonoGenericContext *) container);
1172 fprintf (output, "\n");
1175 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1177 last = m->tables [MONO_TABLE_METHOD].rows;
1179 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
1180 dis_method_list (name, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last, (MonoGenericContext *) container);
1182 dis_property_list (m, n, (MonoGenericContext *) container);
1183 dis_event_list (m, n, (MonoGenericContext *) container);
1186 t = &m->tables [MONO_TABLE_NESTEDCLASS];
1187 nested = mono_metadata_nesting_typedef (m, n + 1, 1);
1189 dis_type (m, mono_metadata_decode_row_col (t, nested - 1, MONO_NESTED_CLASS_NESTED) - 1, 1, forward);
1190 nested = mono_metadata_nesting_typedef (m, n + 1, nested + 1);
1193 fprintf (output, " } // end of class %s%s%s\n", nspace, *nspace? ".": "", name);
1194 if (*nspace && !is_nested)
1195 fprintf (output, "}\n");
1196 fprintf (output, "\n");
1202 * @m: metadata context
1204 * disassembles all the global fields and methods
1207 dis_globals (MonoImage *m)
1209 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1210 guint32 cols [MONO_TYPEDEF_SIZE];
1211 guint32 cols_next [MONO_TYPEDEF_SIZE];
1212 gboolean next_is_valid, last;
1215 name = g_strdup ("<Module>");
1217 mono_metadata_decode_row (t, 0, cols, MONO_TYPEDEF_SIZE);
1220 mono_metadata_decode_row (t, 1, cols_next, MONO_TYPEDEF_SIZE);
1226 * The value in the table is always valid, we know we have fields
1227 * if the value stored is different than the next record.
1231 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1233 last = m->tables [MONO_TABLE_FIELD].rows;
1235 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
1236 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last, NULL);
1237 fprintf (output, "\n");
1240 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1242 last = m->tables [MONO_TABLE_METHOD].rows;
1244 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
1245 dis_method_list (name, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last, NULL);
1250 dis_mresource (MonoImage *m)
1252 MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
1255 for (i = 0; i < t->rows; i++){
1256 guint32 cols [MONO_MANIFEST_SIZE];
1257 const char *name, *res;
1261 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
1262 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
1264 if (! (res = mono_image_get_resource (m, cols [MONO_MANIFEST_OFFSET], &size)))
1267 if ( (fp = fopen (name, "ab")) ) {
1268 if (ftell (fp) == 0)
1269 fwrite (res, size, 1, fp);
1271 g_warning ("Error creating managed resource - %s : File already exists.", name);
1275 g_warning ("Error creating managed resource - %s : %s", name, g_strerror (errno));
1281 * @m: metadata context
1283 * disassembles all types in the @m context
1286 dis_types (MonoImage *m, int forward)
1288 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1294 for (i = 1; i < t->rows; i++) {
1295 flags = mono_metadata_decode_row_col (t, i, MONO_TYPEDEF_FLAGS);
1296 flags &= TYPE_ATTRIBUTE_VISIBILITY_MASK;
1297 if (flags == TYPE_ATTRIBUTE_PUBLIC || flags == TYPE_ATTRIBUTE_NOT_PUBLIC)
1298 dis_type (m, i, 0, forward);
1304 * @m: metadata context
1306 * disassembles all data blobs references in the FieldRVA table in the @m context
1309 dis_data (MonoImage *m)
1311 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
1312 MonoTableInfo *ft = &m->tables [MONO_TABLE_FIELD];
1314 const char *rva, *sig;
1315 guint32 align, size;
1316 guint32 cols [MONO_FIELD_RVA_SIZE];
1319 for (i = 0; i < t->rows; i++) {
1320 mono_metadata_decode_row (t, i, cols, MONO_FIELD_RVA_SIZE);
1321 rva = mono_image_rva_map (m, cols [MONO_FIELD_RVA_RVA]);
1322 sig = mono_metadata_blob_heap (m, mono_metadata_decode_row_col (ft, cols [MONO_FIELD_RVA_FIELD] -1, MONO_FIELD_SIGNATURE));
1323 mono_metadata_decode_value (sig, &sig);
1324 /* FIELD signature == 0x06 */
1325 g_assert (*sig == 0x06);
1326 type = mono_metadata_parse_field_type (m, 0, sig + 1, &sig);
1327 mono_class_init (mono_class_from_mono_type (type));
1328 size = mono_type_size (type, &align);
1329 fprintf (output, ".data D_%08x = bytearray (", cols [MONO_FIELD_RVA_RVA]);
1330 for (b = 0; b < size; ++b) {
1332 fprintf (output, "\n\t");
1333 fprintf (output, " %02X", rva [b] & 0xff);
1335 fprintf (output, ") // size: %d\n", size);
1342 void (*dumper) (MonoImage *m);
1344 { "--assembly", MONO_TABLE_ASSEMBLY, dump_table_assembly },
1345 { "--assemblyref", MONO_TABLE_ASSEMBLYREF, dump_table_assemblyref },
1346 { "--classlayout", MONO_TABLE_CLASSLAYOUT, dump_table_class_layout },
1347 { "--constant", MONO_TABLE_CONSTANT, dump_table_constant },
1348 { "--customattr", MONO_TABLE_CUSTOMATTRIBUTE, dump_table_customattr },
1349 { "--declsec", MONO_TABLE_DECLSECURITY, dump_table_declsec },
1350 { "--event", MONO_TABLE_EVENT, dump_table_event },
1351 { "--exported", MONO_TABLE_EXPORTEDTYPE, dump_table_exported },
1352 { "--fields", MONO_TABLE_FIELD, dump_table_field },
1353 { "--file", MONO_TABLE_FILE, dump_table_file },
1354 { "--genericpar", MONO_TABLE_GENERICPARAM, dump_table_genericpar },
1355 { "--interface", MONO_TABLE_INTERFACEIMPL, dump_table_interfaceimpl },
1356 { "--manifest", MONO_TABLE_MANIFESTRESOURCE, dump_table_manifest },
1357 { "--marshal", MONO_TABLE_FIELDMARSHAL, dump_table_field_marshal },
1358 { "--memberref", MONO_TABLE_MEMBERREF, dump_table_memberref },
1359 { "--method", MONO_TABLE_METHOD, dump_table_method },
1360 { "--methodimpl", MONO_TABLE_METHODIMPL, dump_table_methodimpl },
1361 { "--methodsem", MONO_TABLE_METHODSEMANTICS, dump_table_methodsem },
1362 { "--methodspec", MONO_TABLE_METHODSPEC, dump_table_methodspec },
1363 { "--moduleref", MONO_TABLE_MODULEREF, dump_table_moduleref },
1364 { "--module", MONO_TABLE_MODULE, dump_table_module },
1365 { "--nested", MONO_TABLE_NESTEDCLASS, dump_table_nestedclass },
1366 { "--param", MONO_TABLE_PARAM, dump_table_param },
1367 { "--parconst", MONO_TABLE_GENERICPARAMCONSTRAINT, dump_table_parconstraint },
1368 { "--property", MONO_TABLE_PROPERTY, dump_table_property },
1369 { "--propertymap", MONO_TABLE_PROPERTYMAP, dump_table_property_map },
1370 { "--typedef", MONO_TABLE_TYPEDEF, dump_table_typedef },
1371 { "--typeref", MONO_TABLE_TYPEREF, dump_table_typeref },
1372 { "--typespec", MONO_TABLE_TYPESPEC, dump_table_typespec },
1373 { "--implmap", MONO_TABLE_IMPLMAP, dump_table_implmap },
1374 { "--standalonesig", MONO_TABLE_STANDALONESIG, dump_table_standalonesig },
1375 { "--blob", 0, dump_stream_blob },
1381 * @file: file containing CIL code.
1383 * Disassembles the @file file.
1386 disassemble_file (const char *file)
1389 MonoImageOpenStatus status;
1392 ass = mono_assembly_open (file, &status);
1394 fprintf (stderr, "Error while trying to process %s\n", file);
1402 if (dump_table != -1){
1403 (*table_list [dump_table].dumper) (img);
1405 dump_header_data (img);
1407 dis_directive_assemblyref (img);
1408 dis_directive_assembly (img);
1409 dis_directive_file (img);
1410 dis_directive_mresource (img);
1411 dis_directive_module (img);
1412 dis_directive_moduleref (img);
1413 if (dump_managed_resources)
1414 dis_mresource (img);
1415 if (dump_forward_decls) {
1416 fprintf (output, "// *************** Forward Declarations for Classes ***************\n\n");
1418 fprintf (output, "// *************** End-Of Forward Declarations for Classes ***************\n\n");
1424 mono_image_close (img);
1438 TableFilter methods;
1441 static GList *filter_list = NULL;
1442 static ImageFilter *cur_filter = NULL;
1445 setup_filter (MonoImage *image)
1447 ImageFilter *ifilter;
1449 const char *name = mono_image_get_name (image);
1451 for (item = filter_list; item; item = item->next) {
1452 ifilter = item->data;
1453 if (strcmp (ifilter->name, name) == 0) {
1454 cur_filter = ifilter;
1462 int_cmp (const void *e1, const void *e2)
1470 table_includes (TableFilter *tf, int idx)
1474 return bsearch (&idx, tf->elems, tf->count, sizeof (int), int_cmp) != NULL;
1478 should_include_type (int idx)
1482 return table_includes (&cur_filter->types, idx);
1486 should_include_method (int idx)
1490 return table_includes (&cur_filter->methods, idx);
1494 should_include_field (int idx)
1498 return table_includes (&cur_filter->fields, idx);
1502 add_filter (const char *name)
1504 ImageFilter *ifilter;
1507 for (item = filter_list; item; item = item->next) {
1508 ifilter = item->data;
1509 if (strcmp (ifilter->name, name) == 0)
1512 ifilter = g_new0 (ImageFilter, 1);
1513 ifilter->name = g_strdup (name);
1514 filter_list = g_list_prepend (filter_list, ifilter);
1519 add_item (TableFilter *tf, int val)
1521 if (tf->count >= tf->size) {
1524 tf->elems = g_malloc (sizeof (int) * tf->size);
1527 tf->elems = g_realloc (tf->elems, sizeof (int) * tf->size);
1530 tf->elems [tf->count++] = val;
1534 sort_filter_elems (void)
1536 ImageFilter *ifilter;
1539 for (item = filter_list; item; item = item->next) {
1540 ifilter = item->data;
1541 qsort (ifilter->types.elems, ifilter->types.count, sizeof (int), int_cmp);
1542 qsort (ifilter->fields.elems, ifilter->fields.count, sizeof (int), int_cmp);
1543 qsort (ifilter->methods.elems, ifilter->methods.count, sizeof (int), int_cmp);
1548 load_filter (const char* filename)
1552 char *p, *s, *endptr;
1554 ImageFilter *ifilter = NULL;
1557 if (!(file = fopen (filename, "r"))) {
1558 g_print ("Cannot open filter file '%s'\n", filename);
1561 while (fgets (buf, sizeof (buf), file) != NULL) {
1564 while (*s && g_ascii_isspace (*s)) ++s;
1570 p = strchr (s, ']');
1572 g_error ("No matching ']' in filter at line %d\n", line);
1574 ifilter = add_filter (s + 1);
1578 g_error ("Invalid format in filter at line %d\n", line);
1579 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1580 g_error ("Invalid type number in filter at line %d\n", line);
1581 add_item (&ifilter->types, value);
1585 g_error ("Invalid format in filter at line %d\n", line);
1586 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1587 g_error ("Invalid method number in filter at line %d\n", line);
1588 add_item (&ifilter->methods, value);
1592 g_error ("Invalid format in filter at line %d\n", line);
1593 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1594 g_error ("Invalid field number in filter at line %d\n", line);
1595 add_item (&ifilter->fields, value);
1598 g_error ("Invalid format in filter at line %d\n", line);
1602 sort_filter_elems ();
1607 try_load_from (MonoAssembly **assembly, const gchar *path1, const gchar *path2,
1608 const gchar *path3, const gchar *path4, gboolean refonly)
1613 fullpath = g_build_filename (path1, path2, path3, path4, NULL);
1614 if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR))
1615 *assembly = mono_assembly_open_full (fullpath, NULL, refonly);
1618 return (*assembly != NULL);
1621 static MonoAssembly *
1622 real_load (gchar **search_path, const gchar *culture, const gchar *name, gboolean refonly)
1624 MonoAssembly *result = NULL;
1627 const gchar *local_culture;
1630 if (!culture || *culture == '\0') {
1633 local_culture = culture;
1636 filename = g_strconcat (name, ".dll", NULL);
1637 len = strlen (filename);
1639 for (path = search_path; *path; path++) {
1641 continue; /* Ignore empty ApplicationBase */
1643 /* See test cases in bug #58992 and bug #57710 */
1644 /* 1st try: [culture]/[name].dll (culture may be empty) */
1645 strcpy (filename + len - 4, ".dll");
1646 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
1649 /* 2nd try: [culture]/[name].exe (culture may be empty) */
1650 strcpy (filename + len - 4, ".exe");
1651 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
1654 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
1655 strcpy (filename + len - 4, ".dll");
1656 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
1659 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
1660 strcpy (filename + len - 4, ".exe");
1661 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
1670 * Try to load referenced assemblies from assemblies_path.
1672 static MonoAssembly *
1673 monodis_preload (MonoAssemblyName *aname,
1674 gchar **assemblies_path,
1677 MonoAssembly *result = NULL;
1678 gboolean refonly = GPOINTER_TO_UINT (user_data);
1680 if (assemblies_path && assemblies_path [0] != NULL) {
1681 result = real_load (assemblies_path, aname->culture, aname->name, refonly);
1691 GString *args = g_string_new ("[--output=filename] [--filter=filename] [--help] [--mscorlib]\n");
1694 for (i = 0; table_list [i].name != NULL; i++){
1695 g_string_append (args, "[");
1696 g_string_append (args, table_list [i].name);
1697 g_string_append (args, "] ");
1698 if (((i-2) % 5) == 0)
1699 g_string_append_c (args, '\n');
1701 g_string_append (args, "[--forward-decls]\n[--mresources]");
1703 "monodis -- Mono Common Intermediate Language Dissassembler\n"
1704 "Usage is: monodis %s file ..\n", args->str);
1709 main (int argc, char *argv [])
1711 GList *input_files = NULL, *l;
1716 for (i = 1; i < argc; i++){
1717 if (argv [i][0] == '-'){
1718 if (argv [i][1] == 'h')
1720 else if (argv [i][1] == 'd')
1721 dump_header_data_p = TRUE;
1722 else if (strcmp (argv [i], "--mscorlib") == 0) {
1723 substitute_with_mscorlib_p = TRUE;
1725 } else if (strcmp (argv [i], "--show-method-tokens") == 0) {
1726 show_method_tokens = TRUE;
1728 } else if (strcmp (argv [i], "--show-tokens") == 0) {
1731 } else if (strncmp (argv [i], "--output=", 9) == 0) {
1732 output = fopen (argv [i]+9, "w");
1733 if (output == NULL) {
1734 fprintf (stderr, "Can't open output file `%s': %s\n",
1735 argv [i]+9, strerror (errno));
1739 } else if (strncmp (argv [i], "--filter=", 9) == 0) {
1740 load_filter (argv [i]+9);
1742 } else if (strcmp (argv [i], "--forward-decls") == 0) {
1743 dump_forward_decls = TRUE;
1745 } else if (strcmp (argv [i], "--mresources") == 0) {
1746 dump_managed_resources = TRUE;
1748 } else if (strcmp (argv [i], "--help") == 0)
1750 for (j = 0; table_list [j].name != NULL; j++) {
1751 if (strcmp (argv [i], table_list [j].name) == 0)
1757 input_files = g_list_append (input_files, argv [i]);
1760 if (input_files == NULL)
1764 * If we just have one file, use the corlib version it requires.
1766 if (!input_files->next) {
1767 char *filename = input_files->data;
1769 mono_init_from_assembly (argv [0], filename);
1771 mono_install_assembly_preload_hook (monodis_preload, GUINT_TO_POINTER (FALSE));
1773 disassemble_file (filename);
1775 mono_init (argv [0]);
1777 for (l = input_files; l; l = l->next)
1778 disassemble_file (l->data);