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?
26 #include <mono/metadata/loader.h>
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/appdomain.h>
30 static void setup_filter (MonoImage *image);
31 static gboolean should_include_type (int idx);
32 static gboolean should_include_method (int idx);
33 static gboolean should_include_field (int idx);
37 /* True if you want to get a dump of the header data */
38 gboolean dump_header_data_p = FALSE;
40 gboolean substitute_with_mscorlib_p = FALSE;
45 dump_header_data (MonoImage *img)
47 if (!dump_header_data_p)
51 "// Ximian's CIL disassembler, version 1.0\n"
52 "// Copyright (C) 2001 Ximian, Inc.\n\n");
56 dump_cattrs_list (GList *list, const char *indent)
60 for (tmp = list; tmp; tmp = tmp->next) {
61 fprintf (output, "%s%s\n", indent, (char*)tmp->data);
68 dump_cattrs (MonoImage *m, guint32 token, const char *indent)
72 list = dis_get_custom_attrs (m, token);
73 dump_cattrs_list (list, indent);
77 get_il_security_action (int val)
82 case SECURITY_ACTION_DEMAND:
84 case SECURITY_ACTION_ASSERT:
86 case SECURITY_ACTION_DENY:
88 case SECURITY_ACTION_PERMITONLY:
90 case SECURITY_ACTION_LINKDEMAND:
92 case SECURITY_ACTION_INHERITDEMAND:
93 return "inheritcheck";
94 case SECURITY_ACTION_REQMIN:
96 case SECURITY_ACTION_REQOPT:
98 case SECURITY_ACTION_REQREFUSE:
100 /* Special actions (for non CAS permissions) */
101 case SECURITY_ACTION_NONCASDEMAND:
102 return "noncasdemand";
103 case SECURITY_ACTION_NONCASLINKDEMAND:
104 return "noncaslinkdemand";
105 case SECURITY_ACTION_NONCASINHERITANCE:
106 return "noncasinheritance";
107 /* Fx 2.0 actions (for both CAS and non-CAS permissions) */
108 case SECURITY_ACTION_LINKDEMANDCHOICE:
109 return "linkdemandor";
110 case SECURITY_ACTION_INHERITDEMANDCHOICE:
111 return "inheritancedemandor";
112 case SECURITY_ACTION_DEMANDCHOICE:
115 g_snprintf (buf, sizeof (buf), "0x%04X", val);
120 #define OBJECT_TYPE_TYPEDEF 0
121 #define OBJECT_TYPE_METHODDEF 1
122 #define OBJECT_TYPE_ASSEMBLYDEF 2
125 dump_declarative_security (MonoImage *m, guint32 objectType, guint32 token, const char *indent)
127 MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
128 guint32 cols [MONO_DECL_SECURITY_SIZE];
131 const char *blob, *action;
133 for (i = 1; i <= t->rows; i++) {
134 mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
135 blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
136 len = mono_metadata_decode_blob_size (blob, &blob);
137 action = get_il_security_action (cols [MONO_DECL_SECURITY_ACTION]);
138 idx = cols [MONO_DECL_SECURITY_PARENT];
139 if (((idx & MONO_HAS_DECL_SECURITY_MASK) == objectType) && ((idx >> MONO_HAS_DECL_SECURITY_BITS) == token)) {
140 char *dump = data_dump (blob, len, indent);
141 fprintf (output, "%s.permissionset %s = %s", indent, action, dump);
148 dis_directive_assembly (MonoImage *m)
150 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
151 guint32 cols [MONO_ASSEMBLY_SIZE];
156 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
158 fprintf (output, ".assembly '%s'\n{\n",
159 mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
160 dump_cattrs (m, MONO_TOKEN_ASSEMBLY | 1, " ");
161 dump_declarative_security (m, OBJECT_TYPE_ASSEMBLYDEF, 1, " ");
163 " .hash algorithm 0x%08x\n"
164 " .ver %d:%d:%d:%d\n",
165 cols [MONO_ASSEMBLY_HASH_ALG],
166 cols [MONO_ASSEMBLY_MAJOR_VERSION], cols [MONO_ASSEMBLY_MINOR_VERSION],
167 cols [MONO_ASSEMBLY_BUILD_NUMBER], cols [MONO_ASSEMBLY_REV_NUMBER]);
168 if (cols [MONO_ASSEMBLY_CULTURE])
169 fprintf (output, " .locale %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]));
170 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
171 const char* b = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
172 int len = mono_metadata_decode_blob_size (b, &b);
173 char *dump = data_dump (b, len, "\t\t");
174 fprintf (output, " .publickey =%s", dump);
177 fprintf (output, "}\n");
181 dis_directive_assemblyref (MonoImage *m)
183 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
184 guint32 cols [MONO_ASSEMBLYREF_SIZE];
190 for (i = 0; i < t->rows; i++){
193 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
195 esc = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
198 ".assembly extern %s\n"
200 " .ver %d:%d:%d:%d\n",
202 cols [MONO_ASSEMBLYREF_MAJOR_VERSION], cols [MONO_ASSEMBLYREF_MINOR_VERSION],
203 cols [MONO_ASSEMBLYREF_BUILD_NUMBER], cols [MONO_ASSEMBLYREF_REV_NUMBER]
205 if (cols [MONO_ASSEMBLYREF_CULTURE]){
206 fprintf (output, " .locale %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_CULTURE]));
208 if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]){
209 const char* b = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLYREF_PUBLIC_KEY]);
210 int len = mono_metadata_decode_blob_size (b, &b);
211 char *dump = data_dump (b, len, "\t\t");
212 fprintf (output, " .publickeytoken =%s", dump);
215 fprintf (output, "}\n");
221 dis_directive_module (MonoImage *m)
223 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
226 for (i = 0; i < t->rows; i++){
227 guint32 cols [MONO_MODULE_SIZE];
231 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
233 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
234 ename = get_escaped_name (name);
235 guid = get_guid (m, cols [MONO_MODULE_MVID]);
236 fprintf (output, ".module %s // GUID = %s\n\n", ename, guid);
239 dump_cattrs (m, MONO_TOKEN_MODULE | (i + 1), "");
244 dis_directive_moduleref (MonoImage *m)
246 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
249 for (i = 0; i < t->rows; i++){
250 guint32 cols [MONO_MODULEREF_SIZE];
253 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
255 name = mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]);
256 fprintf (output, ".module extern %s\n", name);
262 dis_directive_file (MonoImage *m)
264 MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
268 entry_point = mono_image_get_entry_point (m);
270 for (i = 0; i < t->rows; i++){
271 guint32 cols [MONO_FILE_SIZE];
272 const char *name, *hash;
275 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
277 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
279 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
280 len = mono_metadata_decode_blob_size (hash, &hash);
282 fprintf (output, ".file %s%s .hash = (", name,
283 cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA ? " nometadata" : "");
285 for (j = 0; j < len; ++j)
286 fprintf (output, " %02X", hash [j] & 0xff);
288 token = mono_metadata_make_token (MONO_TABLE_FILE, i + 1);
289 fprintf (output, " )%s\n", (token == entry_point) ? " .entrypoint" : "");
294 static dis_map_t visibility_map [] = {
295 { TYPE_ATTRIBUTE_NOT_PUBLIC, "private " },
296 { TYPE_ATTRIBUTE_PUBLIC, "public " },
297 { TYPE_ATTRIBUTE_NESTED_PUBLIC, "nested public " },
298 { TYPE_ATTRIBUTE_NESTED_PRIVATE, "nested private " },
299 { TYPE_ATTRIBUTE_NESTED_FAMILY, "nested family " },
300 { TYPE_ATTRIBUTE_NESTED_ASSEMBLY, "nested assembly " },
301 { TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM, "nested famandassem " },
302 { TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM, "nested famorassem " },
306 static dis_map_t layout_map [] = {
307 { TYPE_ATTRIBUTE_AUTO_LAYOUT, "auto " },
308 { TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT, "sequential " },
309 { TYPE_ATTRIBUTE_EXPLICIT_LAYOUT, "explicit " },
313 static dis_map_t format_map [] = {
314 { TYPE_ATTRIBUTE_ANSI_CLASS, "ansi " },
315 { TYPE_ATTRIBUTE_UNICODE_CLASS, "unicode " },
316 { TYPE_ATTRIBUTE_AUTO_CLASS, "auto " },
321 typedef_flags (guint32 flags)
323 static char buffer [1024];
324 int visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
325 int layout = flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
326 int format = flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK;
330 strcat (buffer, map (visibility, visibility_map));
331 strcat (buffer, map (layout, layout_map));
332 strcat (buffer, map (format, format_map));
334 if (flags & TYPE_ATTRIBUTE_ABSTRACT)
335 strcat (buffer, "abstract ");
336 if (flags & TYPE_ATTRIBUTE_SEALED)
337 strcat (buffer, "sealed ");
338 if (flags & TYPE_ATTRIBUTE_SPECIAL_NAME)
339 strcat (buffer, "special-name ");
340 if (flags & TYPE_ATTRIBUTE_IMPORT)
341 strcat (buffer, "import ");
342 if (flags & TYPE_ATTRIBUTE_SERIALIZABLE)
343 strcat (buffer, "serializable ");
344 if (flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
345 strcat (buffer, "beforefieldinit ");
352 * @m: metadata context
353 * @start: starting index into the Field Table.
354 * @end: ending index into Field table.
356 * This routine displays all the decoded fields from @start to @end
359 dis_field_list (MonoImage *m, guint32 start, guint32 end)
361 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
362 guint32 cols [MONO_FIELD_SIZE];
368 if (end > t->rows + 1) {
369 g_warning ("ERROR index out of range in fields");
373 for (i = start; i < end; i++){
374 char *sig, *flags, *attrs = NULL;
375 guint32 field_offset = -1;
377 if (!should_include_field (i + 1))
379 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
380 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
381 flags = field_flags (cols [MONO_FIELD_FLAGS]);
383 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
384 mono_metadata_field_info (m, i, NULL, &rva, NULL);
385 g_snprintf (rva_desc, sizeof (rva_desc), " at D_%08x", rva);
390 mono_metadata_field_info (m, i, &field_offset, NULL, NULL);
391 if (field_offset != -1)
392 attrs = g_strdup_printf ("[%d]", field_offset);
393 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
394 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_LITERAL){
396 guint32 const_cols [MONO_CONSTANT_SIZE];
399 if ((crow = mono_metadata_get_constant_index (m, MONO_TOKEN_FIELD_DEF | (i+1), 0))) {
400 mono_metadata_decode_row (&m->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
401 lit = get_constant (m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
403 lit = g_strdup ("not found");
407 fprintf (output, " .field %s %s %s = ",
409 fprintf (output, "%s\n", lit);
412 fprintf (output, " .field %s %s %s %s%s\n",
413 attrs? attrs: "", flags, sig, esname, rva_desc);
418 dump_cattrs (m, MONO_TOKEN_FIELD_DEF | (i + 1), " ");
422 static dis_map_t method_access_map [] = {
423 { METHOD_ATTRIBUTE_COMPILER_CONTROLLED, "privatescope " },
424 { METHOD_ATTRIBUTE_PRIVATE, "private " },
425 { METHOD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
426 { METHOD_ATTRIBUTE_ASSEM, "assembly " },
427 { METHOD_ATTRIBUTE_FAMILY, "family " },
428 { METHOD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
429 { METHOD_ATTRIBUTE_PUBLIC, "public " },
433 static dis_map_t method_flags_map [] = {
434 { METHOD_ATTRIBUTE_STATIC, "static " },
435 { METHOD_ATTRIBUTE_FINAL, "final " },
436 { METHOD_ATTRIBUTE_VIRTUAL, "virtual " },
437 { METHOD_ATTRIBUTE_HIDE_BY_SIG, "hidebysig " },
438 { METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK, "newslot " },
439 { METHOD_ATTRIBUTE_ABSTRACT, "abstract " },
440 { METHOD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
441 { METHOD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
442 { METHOD_ATTRIBUTE_UNMANAGED_EXPORT, "export " },
443 /* MS ilasm doesn't compile this statement - is must be added automagically when permissionset are present */
444 /* { METHOD_ATTRIBUTE_HAS_SECURITY, "hassecurity" }, */
445 { METHOD_ATTRIBUTE_REQUIRE_SEC_OBJECT, "requiresecobj" },
446 { METHOD_ATTRIBUTE_PINVOKE_IMPL, "pinvokeimpl " },
453 * Returns a stringified version of the Method's flags
456 method_flags (guint32 f)
458 GString *str = g_string_new ("");
459 int access = f & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK;
462 g_string_append (str, map (access, method_access_map));
463 g_string_append (str, flags (f, method_flags_map));
466 g_string_free (str, FALSE);
471 static dis_map_t pinvoke_flags_map [] = {
472 { PINVOKE_ATTRIBUTE_NO_MANGLE , "nomangle " },
473 { PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR, "lasterr " },
474 { PINVOKE_ATTRIBUTE_BEST_FIT_ENABLED, "bestfit:on" },
475 { PINVOKE_ATTRIBUTE_BEST_FIT_DISABLED, "bestfit:off" },
476 { PINVOKE_ATTRIBUTE_THROW_ON_UNMAPPABLE_ENABLED, "charmaperror:on" },
477 { PINVOKE_ATTRIBUTE_THROW_ON_UNMAPPABLE_DISABLED, "charmaperror:off" },
481 static dis_map_t pinvoke_call_conv_map [] = {
482 { PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI, "winapi " },
483 { PINVOKE_ATTRIBUTE_CALL_CONV_CDECL, "cdecl " },
484 { PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL, "stdcall " },
485 { PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL, "thiscall " },
486 { PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL, "fastcall " },
490 static dis_map_t pinvoke_char_set_map [] = {
491 { PINVOKE_ATTRIBUTE_CHAR_SET_NOT_SPEC, "" },
492 { PINVOKE_ATTRIBUTE_CHAR_SET_ANSI, "ansi " },
493 { PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE , "unicode " },
494 { PINVOKE_ATTRIBUTE_CHAR_SET_AUTO, "autochar " },
501 * Returns a stringified version of the Method's pinvoke flags
504 pinvoke_flags (guint32 f)
506 GString *str = g_string_new ("");
507 int cset = f & PINVOKE_ATTRIBUTE_CHAR_SET_MASK;
508 int cconv = f & PINVOKE_ATTRIBUTE_CALL_CONV_MASK;
511 g_string_append (str, map (cset, pinvoke_char_set_map));
512 g_string_append (str, map (cconv, pinvoke_call_conv_map));
513 g_string_append (str, flags (f, pinvoke_flags_map));
515 s = g_strdup(str->str);
516 g_string_free (str, FALSE);
521 static dis_map_t method_impl_map [] = {
522 { METHOD_IMPL_ATTRIBUTE_IL, "cil " },
523 { METHOD_IMPL_ATTRIBUTE_NATIVE, "native " },
524 { METHOD_IMPL_ATTRIBUTE_OPTIL, "optil " },
525 { METHOD_IMPL_ATTRIBUTE_RUNTIME, "runtime " },
529 static dis_map_t managed_type_map [] = {
530 { METHOD_IMPL_ATTRIBUTE_UNMANAGED, "unmanaged " },
531 { METHOD_IMPL_ATTRIBUTE_MANAGED, "managed " },
535 static dis_map_t managed_impl_flags [] = {
536 { METHOD_IMPL_ATTRIBUTE_FORWARD_REF, "fwdref " },
537 { METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG, "preservesig " },
538 { METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL, "internalcall " },
539 { METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED, "synchronized " },
540 { METHOD_IMPL_ATTRIBUTE_NOINLINING, "noinline " },
545 method_impl_flags (guint32 f)
547 GString *str = g_string_new ("");
549 int code_type = f & METHOD_IMPL_ATTRIBUTE_CODE_TYPE_MASK;
550 int managed_type = f & METHOD_IMPL_ATTRIBUTE_MANAGED_MASK;
552 g_string_append (str, map (code_type, method_impl_map));
553 g_string_append (str, map (managed_type, managed_type_map));
554 g_string_append (str, flags (f, managed_impl_flags));
557 g_string_free (str, FALSE);
562 dis_locals (MonoImage *m, MonoMethodHeader *mh, const char *ptr)
567 unsigned char flags = *(const unsigned char *) ptr;
568 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
570 guint32 local_var_sig_tok, max_stack, code_size, init_locals;
573 g_assert (format == METHOD_HEADER_FAT_FORMAT);
574 fat_flags = read16 (ptr);
576 hsize = (fat_flags >> 12) & 0xf;
577 max_stack = read16 (ptr);
579 code_size = read32 (ptr);
581 local_var_sig_tok = read32 (ptr);
584 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
589 fprintf(output, "\t.locals /*%08x*/ %s(\n",
590 local_var_sig_tok, init_locals ? "init " : "");
592 fprintf(output, "\t.locals %s(\n", mh->init_locals ? "init " : "");
594 for (i=0; i < mh->num_locals; ++i) {
597 fprintf(output, ",\n");
598 /* print also byref and pinned attributes */
599 desc = dis_stringify_type (m, mh->locals[i]);
600 fprintf(output, "\t\t%s\tV_%d", desc, i);
603 fprintf(output, ")\n");
607 dis_code (MonoImage *m, guint32 token, guint32 rva)
609 MonoMethodHeader *mh;
610 const char *ptr = mono_image_rva_map (m, rva);
618 override = get_method_override (m, token);
620 fprintf (output, "\t.override %s\n", override);
624 mh = mono_metadata_parse_mh (m, ptr);
625 if ((entry_point = mono_image_get_entry_point (m))){
626 loc = mono_metadata_locate_token (m, entry_point);
627 if (rva == read32 (loc))
628 fprintf (output, "\t.entrypoint\n");
631 fprintf (output, "\t// Code size %d (0x%x)\n", mh->code_size, mh->code_size);
632 fprintf (output, "\t.maxstack %d\n", mh->max_stack);
634 dis_locals (m, mh, ptr);
635 dissasemble_cil (m, mh);
638 hex_dump (mh->code, 0, mh->code_size);
639 printf ("\nAfter the code\n");
640 hex_dump (mh->code + mh->code_size, 0, 64);
642 mono_metadata_free_mh (mh);
646 pinvoke_info (MonoImage *m, guint32 mindex)
648 MonoTableInfo *im = &m->tables [MONO_TABLE_IMPLMAP];
649 MonoTableInfo *mr = &m->tables [MONO_TABLE_MODULEREF];
650 guint32 im_cols [MONO_IMPLMAP_SIZE];
651 guint32 mr_cols [MONO_MODULEREF_SIZE];
652 const char *import, *scope;
656 for (i = 0; i < im->rows; i++) {
658 mono_metadata_decode_row (im, i, im_cols, MONO_IMPLMAP_SIZE);
660 if ((im_cols [MONO_IMPLMAP_MEMBER] >> 1) == mindex + 1) {
662 flags = pinvoke_flags (im_cols [MONO_IMPLMAP_FLAGS]);
664 import = mono_metadata_string_heap (m, im_cols [MONO_IMPLMAP_NAME]);
666 mono_metadata_decode_row (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1,
667 mr_cols, MONO_MODULEREF_SIZE);
669 scope = mono_metadata_string_heap (m, mr_cols [MONO_MODULEREF_NAME]);
671 return g_strdup_printf ("(\"%s\" as \"%s\" %s)", scope, import,
681 dump_cattrs_for_method_params (MonoImage *m, guint32 midx, MonoMethodSignature *sig) {
682 MonoTableInfo *methodt;
683 MonoTableInfo *paramt;
684 guint param_index, lastp, i;
686 methodt = &m->tables [MONO_TABLE_METHOD];
687 paramt = &m->tables [MONO_TABLE_PARAM];
688 param_index = mono_metadata_decode_row_col (methodt, midx, MONO_METHOD_PARAMLIST);
689 if (midx + 1 < methodt->rows)
690 lastp = mono_metadata_decode_row_col (methodt, midx + 1, MONO_METHOD_PARAMLIST);
692 lastp = paramt->rows + 1;
693 for (i = param_index; i < lastp; ++i) {
696 guint32 param_cols [MONO_PARAM_SIZE];
699 list = dis_get_custom_attrs (m, MONO_TOKEN_PARAM_DEF | i);
701 mono_metadata_decode_row (paramt, i-1, param_cols, MONO_PARAM_SIZE);
702 if (!(param_cols[MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_DEFAULT)) {
704 fprintf (output, "\t.param [%d]\n", param_cols[MONO_PARAM_SEQUENCE]);
706 fprintf (output, "\t.param [%d] = ", param_cols[MONO_PARAM_SEQUENCE]);
708 if ((crow = mono_metadata_get_constant_index(m, MONO_TOKEN_PARAM_DEF | i, 0))) {
709 guint32 const_cols [MONO_CONSTANT_SIZE];
710 mono_metadata_decode_row( &m->tables[MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
711 lit = get_constant(m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
714 lit = g_strdup ("not found");
716 fprintf(output, "%s\n", lit);
719 dump_cattrs_list (list, "\t");
725 * @m: metadata context
726 * @start: starting index into the Method Table.
727 * @end: ending index into Method table.
729 * This routine displays the methods in the Method Table from @start to @end
732 dis_method_list (const char *klass_name, MonoImage *m, guint32 start, guint32 end)
734 MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
735 guint32 cols [MONO_METHOD_SIZE];
739 fprintf (output, "ERROR index out of range in methods");
744 for (i = start; i < end; i++){
745 MonoMethodSignature *ms;
746 char *flags, *impl_flags;
751 if (!should_include_method (i + 1))
753 mono_metadata_decode_row (t, i, cols, MONO_METHOD_SIZE);
755 flags = method_flags (cols [MONO_METHOD_FLAGS]);
756 impl_flags = method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
758 sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
759 mono_metadata_decode_blob_size (sig, &sig);
760 ms = mono_metadata_parse_method_signature (m, i + 1, sig, &sig);
761 sig_str = dis_stringify_method_signature (m, ms, i + 1, FALSE);
763 fprintf (output, " // method line %d\n", i + 1);
764 fprintf (output, " .method %s", flags);
766 if ((cols [MONO_METHOD_FLAGS] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (cols [MONO_METHOD_RVA] == 0)) {
767 gchar *pi = pinvoke_info (m, i);
769 fprintf (output, "%s", pi);
774 fprintf (output, "\n %s", sig_str);
775 fprintf (output, " %s\n", impl_flags);
779 token = MONO_TOKEN_METHOD_DEF | (i + 1);
781 fprintf (output, " {\n");
782 dump_cattrs (m, token, " ");
783 dump_cattrs_for_method_params (m, i, ms);
784 /* FIXME: need to sump also param custom attributes */
785 fprintf (output, " // Method begins at RVA 0x%x\n", cols [MONO_METHOD_RVA]);
786 dump_declarative_security (m, OBJECT_TYPE_METHODDEF, i + 1, " ");
787 if (cols [MONO_METHOD_IMPLFLAGS] & METHOD_IMPL_ATTRIBUTE_NATIVE)
788 fprintf (output, " // Disassembly of native methods is not supported\n");
790 dis_code (m, token, cols [MONO_METHOD_RVA]);
791 fprintf (output, " } // end of method %s::%s\n\n", klass_name, sig_str);
792 mono_metadata_free_method_signature (ms);
805 table_locator (const void *a, const void *b)
807 plocator_t *loc = (plocator_t *) a;
808 const char *bb = (const char *) b;
809 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
812 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
814 if (loc->idx == col) {
815 loc->result = table_index;
825 dis_property_methods (MonoImage *m, guint32 prop)
828 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
829 guint32 cols [MONO_METHOD_SEMA_SIZE];
831 const char *type[] = {NULL, ".set", ".get", NULL, ".other"};
833 start = mono_metadata_methods_from_property (m, prop, &end);
834 for (; start < end; ++start) {
835 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
836 if (!should_include_method (cols [MONO_METHOD_SEMA_METHOD]))
838 sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD], TRUE);
839 fprintf (output, "\t\t%s %s\n", type [cols [MONO_METHOD_SEMA_SEMANTICS]], sig);
844 dis_property_signature (MonoImage *m, guint32 prop_idx)
846 MonoTableInfo *propt = &m->tables [MONO_TABLE_PROPERTY];
849 guint32 cols [MONO_PROPERTY_SIZE];
855 GString *res = g_string_new ("");
857 mono_metadata_decode_row (propt, prop_idx, cols, MONO_PROPERTY_SIZE);
858 name = mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]);
859 prop_flags = cols [MONO_PROPERTY_FLAGS];
860 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
861 mono_metadata_decode_blob_size (ptr, &ptr);
862 /* ECMA claims 0x08 ... */
863 if (*ptr != 0x28 && *ptr != 0x08)
864 g_warning("incorrect signature in propert blob: 0x%x", *ptr);
866 pcount = mono_metadata_decode_value (ptr, &ptr);
867 type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
868 blurb = dis_stringify_type (m, type);
869 if (prop_flags & 0x0200)
870 g_string_append (res, "specialname ");
871 if (prop_flags & 0x0400)
872 g_string_append (res, "rtspecialname ");
873 qk = get_escaped_name (name);
874 g_string_sprintfa (res, "%s %s (", blurb, qk);
877 mono_metadata_free_type (type);
878 for (i = 0; i < pcount; i++) {
880 g_string_append (res, ", ");
881 param = mono_metadata_parse_param (m, ptr, &ptr);
882 blurb = dis_stringify_param (m, param);
883 g_string_append (res, blurb);
884 mono_metadata_free_type (param);
887 g_string_append_c (res, ')');
889 g_string_free (res, FALSE);
895 dis_property_list (MonoImage *m, guint32 typedef_row)
898 start = mono_metadata_properties_from_typedef (m, typedef_row, &end);
900 for (i = start; i < end; ++i) {
901 char *sig = dis_property_signature (m, i);
902 fprintf (output, "\t.property %s\n\t{\n", sig);
903 dump_cattrs (m, MONO_TOKEN_PROPERTY | (i + 1), "\t\t");
904 dis_property_methods (m, i);
905 fprintf (output, "\t}\n");
911 dis_event_signature (MonoImage *m, guint32 event_idx)
913 MonoTableInfo *et = &m->tables [MONO_TABLE_EVENT];
914 char *type, *result, *esname;
915 guint32 cols [MONO_EVENT_SIZE];
917 GString *res = g_string_new ("");
919 mono_metadata_decode_row (et, event_idx, cols, MONO_EVENT_SIZE);
920 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]));
921 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE]);
922 event_flags = cols [MONO_EVENT_FLAGS];
924 if (event_flags & 0x0200)
925 g_string_append (res, "specialname ");
926 if (event_flags & 0x0400)
927 g_string_append (res, "rtspecialname ");
928 g_string_sprintfa (res, "%s %s", type, esname);
933 g_string_free (res, FALSE);
938 dis_event_methods (MonoImage *m, guint32 event)
941 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
942 guint32 cols [MONO_METHOD_SEMA_SIZE];
944 const char *type = "";
946 start = mono_metadata_methods_from_event (m, event, &end);
947 for (; start < end; ++start) {
948 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
949 if (!should_include_method (cols [MONO_METHOD_SEMA_METHOD]))
951 sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD], TRUE);
952 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
953 case METHOD_SEMANTIC_OTHER:
954 type = ".other"; break;
955 case METHOD_SEMANTIC_ADD_ON:
956 type = ".addon"; break;
957 case METHOD_SEMANTIC_REMOVE_ON:
958 type = ".removeon"; break;
959 case METHOD_SEMANTIC_FIRE:
960 type = ".fire"; break;
964 fprintf (output, "\t\t%s %s\n", type, sig);
970 dis_event_list (MonoImage *m, guint32 typedef_row)
973 start = mono_metadata_events_from_typedef (m, typedef_row, &end);
975 for (i = start; i < end; ++i) {
976 char *sig = dis_event_signature (m, i);
977 fprintf (output, "\t.event %s\n\t{\n", sig);
978 dump_cattrs (m, MONO_TOKEN_EVENT | (i + 1), "\t\t");
979 dis_event_methods (m, i);
980 fprintf (output, "\t}\n");
986 dis_interfaces (MonoImage *m, guint32 typedef_row)
990 gboolean first_interface = 1;
991 guint32 cols [MONO_INTERFACEIMPL_SIZE];
993 MonoTableInfo *table = &m->tables [MONO_TABLE_INTERFACEIMPL];
999 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
1000 loc.idx = typedef_row;
1002 if (!bsearch (&loc, table->base, table->rows, table->row_size, table_locator))
1007 * We may end up in the middle of the rows...
1010 if (loc.idx == mono_metadata_decode_row_col (table, start - 1, MONO_INTERFACEIMPL_CLASS))
1015 while (start < table->rows) {
1016 mono_metadata_decode_row (table, start, cols, MONO_INTERFACEIMPL_SIZE);
1017 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
1019 intf = get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE]);
1020 if (first_interface) {
1021 fprintf (output, " \timplements %s", intf);
1022 first_interface = 0;
1024 fprintf (output, ", %s", intf);
1032 * dis_generic_param_and_constraints:
1033 * @m: metadata context
1034 * @table_type: Type of table (0 for typedef, 1 for methoddef)
1035 * @row: Row in table
1037 * Dissasembles the generic parameters for this type or method, also
1038 * returns an allocated GString containing the generic constraints NULL
1039 * if their are no generic constraints.
1042 dis_generic_param_and_constraints (MonoImage *m, int table_type, guint32 typedef_row)
1044 MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAM];
1045 MonoTableInfo *ct = &m->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
1046 GString* cnst_block = NULL;
1047 guint32 cols [MONO_GENERICPARAM_SIZE];
1048 guint32 ccols [MONO_GENPARCONSTRAINT_SIZE];
1049 int i, own_tok, table, idx, found_count, cnst_start, cnst_ind;
1051 g_assert (table_type != MONO_TYPEORMETHOD_TYPE || table_type != MONO_TYPEORMETHOD_METHOD);
1053 found_count = cnst_start = 0;
1054 for (i = 1; i <= t->rows; i++) {
1055 mono_metadata_decode_row (t, i-1, cols, MONO_GENERICPARAM_SIZE);
1056 own_tok = cols [MONO_GENERICPARAM_OWNER];
1057 table = own_tok & MONO_TYPEORMETHOD_MASK;
1058 idx = own_tok >> MONO_TYPEORMETHOD_BITS;
1060 if (table != table_type || idx != typedef_row)
1063 if (found_count == 0)
1064 fprintf (output, "<");
1066 fprintf (output, ", ");
1068 for (cnst_ind = cnst_start; cnst_ind < ct->rows; cnst_ind++) {
1070 mono_metadata_decode_row (ct, cnst_ind, ccols, MONO_GENPARCONSTRAINT_SIZE);
1071 if (ccols [MONO_GENPARCONSTRAINT_GENERICPAR] != i)
1073 if (cnst_block == NULL)
1074 cnst_block = g_string_new ("");
1075 sig = get_typedef_or_ref (m, ccols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
1076 fprintf (output, "(%s) ", sig);
1078 cnst_start = cnst_ind;
1081 fprintf (output, "%s", mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
1087 fprintf (output, ">");
1094 * @m: metadata context
1095 * @n: index of type to disassemble
1097 * Disassembles the type whose index in the TypeDef table is @n.
1100 dis_type (MonoImage *m, int n)
1102 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1103 GString *cnst_block = NULL;
1104 guint32 cols [MONO_TYPEDEF_SIZE];
1105 guint32 cols_next [MONO_TYPEDEF_SIZE];
1106 const char *name, *nspace;
1108 guint32 packing_size, class_size;
1109 gboolean next_is_valid, last;
1112 if (!should_include_type (n + 1))
1114 mono_metadata_decode_row (t, n, cols, MONO_TYPEDEF_SIZE);
1116 if (t->rows > n + 1) {
1117 mono_metadata_decode_row (t, n + 1, cols_next, MONO_TYPEDEF_SIZE);
1122 name = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]);
1123 nspace = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
1125 fprintf (output, ".namespace %s\n{\n", nspace);
1127 esname = get_escaped_name (name);
1128 if ((cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_CLASS_SEMANTIC_MASK) == TYPE_ATTRIBUTE_CLASS){
1129 fprintf (output, " .class %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
1131 cnst_block = dis_generic_param_and_constraints (m, MONO_TYPEORMETHOD_TYPE, n+1);
1132 fprintf (output, "\n");
1133 if (cols [MONO_TYPEDEF_EXTENDS]) {
1134 char *base = get_typedef_or_ref (m, cols [MONO_TYPEDEF_EXTENDS]);
1135 fprintf (output, " \textends %s\n", base);
1139 fprintf (output, " .class interface %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
1141 cnst_block = dis_generic_param_and_constraints (m, MONO_TYPEORMETHOD_TYPE, n+1);
1142 fprintf (output, "\n");
1146 dis_interfaces (m, n + 1);
1147 fprintf (output, " {\n");
1149 fprintf (output, "%s", cnst_block->str);
1150 g_string_free (cnst_block, TRUE);
1152 dump_cattrs (m, MONO_TOKEN_TYPE_DEF | (n + 1), " ");
1153 dump_declarative_security (m, OBJECT_TYPE_TYPEDEF, (n + 1), " ");
1155 if (mono_metadata_packing_from_typedef (m, n + 1, &packing_size, &class_size)) {
1156 fprintf (output, " .pack %d\n", packing_size);
1157 fprintf (output, " .size %d\n", class_size);
1160 * The value in the table is always valid, we know we have fields
1161 * if the value stored is different than the next record.
1165 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1167 last = m->tables [MONO_TABLE_FIELD].rows;
1169 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
1170 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last);
1171 fprintf (output, "\n");
1174 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1176 last = m->tables [MONO_TABLE_METHOD].rows;
1178 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
1179 dis_method_list (name, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last);
1181 dis_property_list (m, n);
1182 dis_event_list (m, n);
1184 t = &m->tables [MONO_TABLE_NESTEDCLASS];
1185 nested = mono_metadata_nesting_typedef (m, n + 1, 1);
1187 dis_type (m, mono_metadata_decode_row_col (t, nested - 1, MONO_NESTED_CLASS_NESTED) - 1);
1188 nested = mono_metadata_nesting_typedef (m, n + 1, nested + 1);
1191 fprintf (output, " } // end of class %s%s%s\n", nspace, *nspace? ".": "", name);
1193 fprintf (output, "}\n");
1194 fprintf (output, "\n");
1200 * @m: metadata context
1202 * disassembles all the global fields and methods
1205 dis_globals (MonoImage *m)
1207 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1208 guint32 cols [MONO_TYPEDEF_SIZE];
1209 guint32 cols_next [MONO_TYPEDEF_SIZE];
1210 gboolean next_is_valid, last;
1213 name = g_strdup ("<Module>");
1215 mono_metadata_decode_row (t, 0, cols, MONO_TYPEDEF_SIZE);
1218 mono_metadata_decode_row (t, 1, cols_next, MONO_TYPEDEF_SIZE);
1224 * The value in the table is always valid, we know we have fields
1225 * if the value stored is different than the next record.
1229 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1231 last = m->tables [MONO_TABLE_FIELD].rows;
1233 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
1234 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last);
1235 fprintf (output, "\n");
1238 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1240 last = m->tables [MONO_TABLE_METHOD].rows;
1242 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
1243 dis_method_list (name, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last);
1249 * @m: metadata context
1251 * disassembles all types in the @m context
1254 dis_types (MonoImage *m)
1256 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1262 for (i = 1; i < t->rows; i++) {
1263 flags = mono_metadata_decode_row_col (t, i, MONO_TYPEDEF_FLAGS);
1264 flags &= TYPE_ATTRIBUTE_VISIBILITY_MASK;
1265 if (flags == TYPE_ATTRIBUTE_PUBLIC || flags == TYPE_ATTRIBUTE_NOT_PUBLIC)
1272 * @m: metadata context
1274 * disassembles all data blobs references in the FieldRVA table in the @m context
1277 dis_data (MonoImage *m)
1279 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
1280 MonoTableInfo *ft = &m->tables [MONO_TABLE_FIELD];
1282 const char *rva, *sig;
1283 guint32 align, size;
1284 guint32 cols [MONO_FIELD_RVA_SIZE];
1287 for (i = 0; i < t->rows; i++) {
1288 mono_metadata_decode_row (t, i, cols, MONO_FIELD_RVA_SIZE);
1289 rva = mono_image_rva_map (m, cols [MONO_FIELD_RVA_RVA]);
1290 sig = mono_metadata_blob_heap (m, mono_metadata_decode_row_col (ft, cols [MONO_FIELD_RVA_FIELD] -1, MONO_FIELD_SIGNATURE));
1291 mono_metadata_decode_value (sig, &sig);
1292 /* FIELD signature == 0x06 */
1293 g_assert (*sig == 0x06);
1294 type = mono_metadata_parse_field_type (m, 0, sig + 1, &sig);
1295 mono_class_init (mono_class_from_mono_type (type));
1296 size = mono_type_size (type, &align);
1297 fprintf (output, ".data D_%08x = bytearray (", cols [MONO_FIELD_RVA_RVA]);
1298 for (b = 0; b < size; ++b) {
1300 fprintf (output, "\n\t");
1301 fprintf (output, " %02X", rva [b] & 0xff);
1303 fprintf (output, ") // size: %d\n", size);
1310 void (*dumper) (MonoImage *m);
1312 { "--assembly", MONO_TABLE_ASSEMBLY, dump_table_assembly },
1313 { "--assemblyref", MONO_TABLE_ASSEMBLYREF, dump_table_assemblyref },
1314 { "--classlayout", MONO_TABLE_CLASSLAYOUT, dump_table_class_layout },
1315 { "--constant", MONO_TABLE_CONSTANT, dump_table_constant },
1316 { "--customattr", MONO_TABLE_CUSTOMATTRIBUTE, dump_table_customattr },
1317 { "--declsec", MONO_TABLE_DECLSECURITY, dump_table_declsec },
1318 { "--event", MONO_TABLE_EVENT, dump_table_event },
1319 { "--exported", MONO_TABLE_EXPORTEDTYPE, dump_table_exported },
1320 { "--fields", MONO_TABLE_FIELD, dump_table_field },
1321 { "--file", MONO_TABLE_FILE, dump_table_file },
1322 { "--genericpar", MONO_TABLE_GENERICPARAM, dump_table_genericpar },
1323 { "--interface", MONO_TABLE_INTERFACEIMPL, dump_table_interfaceimpl },
1324 { "--manifest", MONO_TABLE_MANIFESTRESOURCE, dump_table_manifest },
1325 { "--marshal", MONO_TABLE_FIELDMARSHAL, dump_table_field_marshal },
1326 { "--memberref", MONO_TABLE_MEMBERREF, dump_table_memberref },
1327 { "--method", MONO_TABLE_METHOD, dump_table_method },
1328 { "--methodimpl", MONO_TABLE_METHODIMPL, dump_table_methodimpl },
1329 { "--methodsem", MONO_TABLE_METHODSEMANTICS, dump_table_methodsem },
1330 { "--methodspec", MONO_TABLE_METHODSPEC, dump_table_methodspec },
1331 { "--moduleref", MONO_TABLE_MODULEREF, dump_table_moduleref },
1332 { "--module", MONO_TABLE_MODULE, dump_table_module },
1333 { "--nested", MONO_TABLE_NESTEDCLASS, dump_table_nestedclass },
1334 { "--param", MONO_TABLE_PARAM, dump_table_param },
1335 { "--parconst", MONO_TABLE_GENERICPARAMCONSTRAINT, dump_table_parconstraint },
1336 { "--property", MONO_TABLE_PROPERTY, dump_table_property },
1337 { "--propertymap", MONO_TABLE_PROPERTYMAP, dump_table_property_map },
1338 { "--typedef", MONO_TABLE_TYPEDEF, dump_table_typedef },
1339 { "--typeref", MONO_TABLE_TYPEREF, dump_table_typeref },
1340 { "--typespec", MONO_TABLE_TYPESPEC, dump_table_typespec },
1341 { "--implmap", MONO_TABLE_IMPLMAP, dump_table_implmap },
1342 { "--standalonesig", MONO_TABLE_STANDALONESIG, dump_table_standalonesig },
1343 { "--blob", 0, dump_stream_blob },
1349 * @file: file containing CIL code.
1351 * Disassembles the @file file.
1354 disassemble_file (const char *file)
1357 MonoImageOpenStatus status;
1360 ass = mono_assembly_open (file, &status);
1362 fprintf (stderr, "Error while trying to process %s\n", file);
1370 if (dump_table != -1){
1371 (*table_list [dump_table].dumper) (img);
1373 dump_header_data (img);
1375 dis_directive_assemblyref (img);
1376 dis_directive_assembly (img);
1377 dis_directive_file (img);
1378 dis_directive_module (img);
1379 dis_directive_moduleref (img);
1384 mono_image_close (img);
1398 TableFilter methods;
1401 static GList *filter_list = NULL;
1402 static ImageFilter *cur_filter = NULL;
1405 setup_filter (MonoImage *image)
1407 ImageFilter *ifilter;
1409 const char *name = mono_image_get_name (image);
1411 for (item = filter_list; item; item = item->next) {
1412 ifilter = item->data;
1413 if (strcmp (ifilter->name, name) == 0) {
1414 cur_filter = ifilter;
1422 int_cmp (const void *e1, const void *e2)
1430 table_includes (TableFilter *tf, int idx)
1434 return bsearch (&idx, tf->elems, tf->count, sizeof (int), int_cmp) != NULL;
1438 should_include_type (int idx)
1442 return table_includes (&cur_filter->types, idx);
1446 should_include_method (int idx)
1450 return table_includes (&cur_filter->methods, idx);
1454 should_include_field (int idx)
1458 return table_includes (&cur_filter->fields, idx);
1462 add_filter (const char *name)
1464 ImageFilter *ifilter;
1467 for (item = filter_list; item; item = item->next) {
1468 ifilter = item->data;
1469 if (strcmp (ifilter->name, name) == 0)
1472 ifilter = g_new0 (ImageFilter, 1);
1473 ifilter->name = g_strdup (name);
1474 filter_list = g_list_prepend (filter_list, ifilter);
1479 add_item (TableFilter *tf, int val)
1481 if (tf->count >= tf->size) {
1484 tf->elems = g_malloc (sizeof (int) * tf->size);
1487 tf->elems = g_realloc (tf->elems, sizeof (int) * tf->size);
1490 tf->elems [tf->count++] = val;
1494 sort_filter_elems (void)
1496 ImageFilter *ifilter;
1499 for (item = filter_list; item; item = item->next) {
1500 ifilter = item->data;
1501 qsort (ifilter->types.elems, ifilter->types.count, sizeof (int), int_cmp);
1502 qsort (ifilter->fields.elems, ifilter->fields.count, sizeof (int), int_cmp);
1503 qsort (ifilter->methods.elems, ifilter->methods.count, sizeof (int), int_cmp);
1508 load_filter (const char* filename)
1512 char *p, *s, *endptr;
1514 ImageFilter *ifilter = NULL;
1517 if (!(file = fopen (filename, "r"))) {
1518 g_print ("Cannot open filter file '%s'\n", filename);
1521 while (fgets (buf, sizeof (buf), file) != NULL) {
1524 while (*s && g_ascii_isspace (*s)) ++s;
1530 p = strchr (s, ']');
1532 g_error ("No matching ']' in filter at line %d\n", line);
1534 ifilter = add_filter (s + 1);
1538 g_error ("Invalid format in filter at line %d\n", line);
1539 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1540 g_error ("Invalid type number in filter at line %d\n", line);
1541 add_item (&ifilter->types, value);
1545 g_error ("Invalid format in filter at line %d\n", line);
1546 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1547 g_error ("Invalid method number in filter at line %d\n", line);
1548 add_item (&ifilter->methods, value);
1552 g_error ("Invalid format in filter at line %d\n", line);
1553 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1554 g_error ("Invalid field number in filter at line %d\n", line);
1555 add_item (&ifilter->fields, value);
1558 g_error ("Invalid format in filter at line %d\n", line);
1562 sort_filter_elems ();
1568 GString *args = g_string_new ("[--output=filename] [--filter=filename] [--help] [--mscorlib]\n");
1571 for (i = 0; table_list [i].name != NULL; i++){
1572 g_string_append (args, "[");
1573 g_string_append (args, table_list [i].name);
1574 g_string_append (args, "] ");
1575 if (((i-2) % 5) == 0)
1576 g_string_append_c (args, '\n');
1579 "Usage is: monodis %s file ..\n", args->str);
1584 main (int argc, char *argv [])
1586 GList *input_files = NULL, *l;
1591 for (i = 1; i < argc; i++){
1592 if (argv [i][0] == '-'){
1593 if (argv [i][1] == 'h')
1595 else if (argv [i][1] == 'd')
1596 dump_header_data_p = TRUE;
1597 else if (strcmp (argv [i], "--mscorlib") == 0) {
1598 substitute_with_mscorlib_p = TRUE;
1600 } else if (strcmp (argv [i], "--show-method-tokens") == 0) {
1601 show_method_tokens = TRUE;
1603 } else if (strcmp (argv [i], "--show-tokens") == 0) {
1606 } else if (strncmp (argv [i], "--output=", 9) == 0) {
1607 output = fopen (argv [i]+9, "w");
1608 if (output == NULL) {
1609 fprintf (stderr, "Can't open output file `%s': %s\n",
1610 argv [i]+9, strerror (errno));
1614 } else if (strncmp (argv [i], "--filter=", 9) == 0) {
1615 load_filter (argv [i]+9);
1617 } else if (strcmp (argv [i], "--help") == 0)
1619 for (j = 0; table_list [j].name != NULL; j++) {
1620 if (strcmp (argv [i], table_list [j].name) == 0)
1626 input_files = g_list_append (input_files, argv [i]);
1629 if (input_files == NULL)
1633 * If we just have one file, use the corlib version it requires.
1635 if (!input_files->next) {
1636 char *filename = input_files->data;
1638 mono_init_from_assembly (argv [0], filename);
1639 disassemble_file (filename);
1641 mono_init (argv [0]);
1643 for (l = input_files; l; l = l->next)
1644 disassemble_file (l->data);