[runtime] Fix a crash on Windows during the cleanup phase caused by using a destroyed...
[mono-project.git] / mono / dis / main.c
blob20b8b7c9797e99e384a30f4de95aacf7fa4e106f
1 /*
2 * main.c: Sample disassembler
4 * Author:
5 * Miguel de Icaza (miguel@ximian.com)
7 * (C) 2001 Ximian, Inc.
9 * TODO:
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?
15 #include <config.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <glib.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include "meta.h"
23 #include "util.h"
24 #include "dump.h"
25 #include "get.h"
26 #include "dis-cil.h"
27 #include "declsec.h"
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>
33 #include <mono/utils/bsearch.h>
35 static void setup_filter (MonoImage *image);
36 static gboolean should_include_type (int idx);
37 static gboolean should_include_method (int idx);
38 static gboolean should_include_field (int idx);
40 FILE *output;
42 /* True if you want to get a dump of the header data */
43 gboolean dump_header_data_p = FALSE;
45 /* True if you want to get forward declarations */
46 gboolean dump_forward_decls = FALSE;
48 /* True if you want to dump managed resources as files */
49 gboolean dump_managed_resources = FALSE;
51 gboolean substitute_with_mscorlib_p = FALSE;
53 int dump_table = -1;
55 static void
56 dump_header_data (MonoImage *img)
58 if (!dump_header_data_p)
59 return;
61 fprintf (output,
62 "// Ximian's CIL disassembler, version 1.0\n"
63 "// Copyright (C) 2001 Ximian, Inc.\n\n");
66 static void
67 dump_cattrs_list (GList *list, const char *indent)
69 GList *tmp;
71 for (tmp = list; tmp; tmp = tmp->next) {
72 fprintf (output, "%s%s\n", indent, (char*)tmp->data);
73 g_free (tmp->data);
75 g_list_free (list);
78 static void
79 dump_cattrs (MonoImage *m, guint32 token, const char *indent)
81 GList *list;
83 list = dis_get_custom_attrs (m, token);
84 dump_cattrs_list (list, indent);
87 static const char*
88 get_il_security_action (int val)
90 static char buf [32];
92 switch (val) {
93 case SECURITY_ACTION_DEMAND:
94 return "demand";
95 case SECURITY_ACTION_ASSERT:
96 return "assert";
97 case SECURITY_ACTION_DENY:
98 return "deny";
99 case SECURITY_ACTION_PERMITONLY:
100 return "permitonly";
101 case SECURITY_ACTION_LINKDEMAND:
102 return "linkcheck";
103 case SECURITY_ACTION_INHERITDEMAND:
104 return "inheritcheck";
105 case SECURITY_ACTION_REQMIN:
106 return "reqmin";
107 case SECURITY_ACTION_REQOPT:
108 return "reqopt";
109 case SECURITY_ACTION_REQREFUSE:
110 return "reqrefuse";
111 /* Special actions (for non CAS permissions) */
112 case SECURITY_ACTION_NONCASDEMAND:
113 return "noncasdemand";
114 case SECURITY_ACTION_NONCASLINKDEMAND:
115 return "noncaslinkdemand";
116 case SECURITY_ACTION_NONCASINHERITANCE:
117 return "noncasinheritance";
118 /* Fx 2.0 actions (for both CAS and non-CAS permissions) */
119 case SECURITY_ACTION_LINKDEMANDCHOICE:
120 return "linkdemandor";
121 case SECURITY_ACTION_INHERITDEMANDCHOICE:
122 return "inheritancedemandor";
123 case SECURITY_ACTION_DEMANDCHOICE:
124 return "demandor";
125 default:
126 g_snprintf (buf, sizeof (buf), "0x%04X", val);
127 return buf;
131 #define OBJECT_TYPE_TYPEDEF 0
132 #define OBJECT_TYPE_METHODDEF 1
133 #define OBJECT_TYPE_ASSEMBLYDEF 2
135 static void
136 dump_declarative_security (MonoImage *m, guint32 objectType, guint32 token, const char *indent)
138 MonoTableInfo *t = &m->tables [MONO_TABLE_DECLSECURITY];
139 guint32 cols [MONO_DECL_SECURITY_SIZE];
140 int i, len;
141 guint32 idx;
142 const char *blob, *action;
144 for (i = 1; i <= t->rows; i++) {
145 mono_metadata_decode_row (t, i - 1, cols, MONO_DECL_SECURITY_SIZE);
146 blob = mono_metadata_blob_heap (m, cols [MONO_DECL_SECURITY_PERMISSIONSET]);
147 len = mono_metadata_decode_blob_size (blob, &blob);
148 action = get_il_security_action (cols [MONO_DECL_SECURITY_ACTION]);
149 idx = cols [MONO_DECL_SECURITY_PARENT];
150 if (((idx & MONO_HAS_DECL_SECURITY_MASK) == objectType) && ((idx >> MONO_HAS_DECL_SECURITY_BITS) == token)) {
151 char *dump;
152 if (blob [0] == MONO_DECLSEC_FORMAT_20) {
153 /* 2.0 declarative security format */
154 dump = dump_declsec_entry20 (m, blob, indent);
155 fprintf (output, "%s.permissionset %s = %s\n", indent, action, dump);
156 } else {
157 /* 1.x declarative security metadata format */
158 dump = data_dump (blob, len, indent);
159 fprintf (output, "%s.permissionset %s = %s", indent, action, dump);
161 g_free (dump);
166 static char *
167 assembly_flags (guint32 f)
169 if (f & ASSEMBLYREF_RETARGETABLE_FLAG)
170 return g_strdup ("retargetable ");
171 return g_strdup ("");
174 static void
175 dis_directive_assembly (MonoImage *m)
177 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
178 guint32 cols [MONO_ASSEMBLY_SIZE];
179 char *flags;
181 if (t->base == NULL)
182 return;
184 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
185 flags = assembly_flags (cols [MONO_ASSEMBLY_FLAGS]);
187 fprintf (output, ".assembly %s'%s'\n{\n",
188 flags, mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
189 dump_cattrs (m, MONO_TOKEN_ASSEMBLY | 1, " ");
190 dump_declarative_security (m, OBJECT_TYPE_ASSEMBLYDEF, 1, " ");
191 fprintf (output,
192 " .hash algorithm 0x%08x\n"
193 " .ver %d:%d:%d:%d\n",
194 cols [MONO_ASSEMBLY_HASH_ALG],
195 cols [MONO_ASSEMBLY_MAJOR_VERSION], cols [MONO_ASSEMBLY_MINOR_VERSION],
196 cols [MONO_ASSEMBLY_BUILD_NUMBER], cols [MONO_ASSEMBLY_REV_NUMBER]);
197 if (cols [MONO_ASSEMBLY_CULTURE]){
198 const char *locale = mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]);
199 glong items_read, items_written;
200 gunichar2 *render = g_utf8_to_utf16 (locale, strlen (locale), &items_read, &items_written, NULL);
201 char *dump = data_dump ((const char *) render, items_written * sizeof (gunichar2), "\t\t");
202 fprintf (output, " .locale %s\n", dump);
203 g_free (dump);
204 g_free (render);
206 } if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
207 const char* b = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
208 int len = mono_metadata_decode_blob_size (b, &b);
209 char *dump = data_dump (b, len, "\t\t");
210 fprintf (output, " .publickey =%s", dump);
211 g_free (dump);
213 fprintf (output, "}\n");
215 g_free (flags);
218 static void
219 dis_directive_assemblyref (MonoImage *m)
221 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
222 guint32 cols [MONO_ASSEMBLYREF_SIZE];
223 int i;
225 if (t->base == NULL)
226 return;
228 for (i = 0; i < t->rows; i++){
229 char *esc, *flags;
231 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
233 esc = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]));
234 flags = assembly_flags (cols [MONO_ASSEMBLYREF_FLAGS]);
236 fprintf (output,
237 ".assembly extern %s%s\n"
238 "{\n"
239 " .ver %d:%d:%d:%d\n",
240 flags,
241 esc,
242 cols [MONO_ASSEMBLYREF_MAJOR_VERSION], cols [MONO_ASSEMBLYREF_MINOR_VERSION],
243 cols [MONO_ASSEMBLYREF_BUILD_NUMBER], cols [MONO_ASSEMBLYREF_REV_NUMBER]
245 dump_cattrs (m, MONO_TOKEN_ASSEMBLY_REF | (i + 1), " ");
246 if (cols [MONO_ASSEMBLYREF_CULTURE]){
247 fprintf (output, " .locale %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_CULTURE]));
249 if (cols [MONO_ASSEMBLYREF_PUBLIC_KEY]){
250 const char* b = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLYREF_PUBLIC_KEY]);
251 int len = mono_metadata_decode_blob_size (b, &b);
252 char *dump = data_dump (b, len, "\t\t");
253 fprintf (output, " .publickeytoken =%s", dump);
254 g_free (dump);
256 fprintf (output, "}\n");
257 g_free (flags);
258 g_free (esc);
262 static void
263 dis_directive_module (MonoImage *m)
265 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
266 int i;
268 for (i = 0; i < t->rows; i++){
269 guint32 cols [MONO_MODULE_SIZE];
270 const char *name;
271 char *guid, *ename;
273 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
275 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
276 ename = get_escaped_name (name);
277 guid = get_guid (m, cols [MONO_MODULE_MVID]);
278 fprintf (output, ".module %s // GUID = %s\n\n", ename, guid);
279 g_free (ename);
281 dump_cattrs (m, MONO_TOKEN_MODULE | (i + 1), "");
285 static void
286 dis_directive_moduleref (MonoImage *m)
288 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
289 int i;
291 for (i = 0; i < t->rows; i++){
292 guint32 cols [MONO_MODULEREF_SIZE];
294 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
296 fprintf (output, ".module extern '%s'\n", mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]));
301 static void
302 dis_nt_header (MonoImage *m)
304 MonoCLIImageInfo *image_info = m->image_info;
305 if (image_info && image_info->cli_header.nt.pe_stack_reserve != 0x100000)
306 fprintf (output, ".stackreserve 0x%x\n", image_info->cli_header.nt.pe_stack_reserve);
309 static void
310 dis_directive_file (MonoImage *m)
312 MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
313 int i, j, len;
314 guint32 entry_point;
316 entry_point = mono_image_get_entry_point (m);
318 for (i = 0; i < t->rows; i++){
319 guint32 cols [MONO_FILE_SIZE];
320 const char *name, *hash;
321 guint32 token;
323 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
325 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
327 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
328 len = mono_metadata_decode_blob_size (hash, &hash);
330 fprintf (output, ".file %s%s .hash = (",
331 cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA ? "nometadata " : "", name);
333 for (j = 0; j < len; ++j)
334 fprintf (output, " %02X", hash [j] & 0xff);
336 token = mono_metadata_make_token (MONO_TABLE_FILE, i + 1);
337 fprintf (output, " )%s\n", (token == entry_point) ? " .entrypoint" : "");
342 static void
343 dis_directive_mresource (MonoImage *m)
345 MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
346 int i;
348 for (i = 0; i < t->rows; i++){
349 guint32 cols [MONO_MANIFEST_SIZE];
350 const char *name;
351 guint32 impl, idx, name_token;
353 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
355 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
357 fprintf (output, ".mresource %s '%s'\n", (cols [MONO_MANIFEST_FLAGS] & MANIFEST_RESOURCE_VISIBILITY_MASK) == (MANIFEST_RESOURCE_PUBLIC) ? "public" : "private", name);
358 fprintf (output, "{\n");
359 impl = cols [MONO_MANIFEST_IMPLEMENTATION];
360 if (impl) {
361 idx = impl >> MONO_IMPLEMENTATION_BITS;
362 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_FILE) {
363 name_token = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_FILE], idx - 1, MONO_FILE_NAME);
365 fprintf (output, " .file '%s' at 0x0\n", mono_metadata_string_heap (m, name_token));
367 if ((impl & MONO_IMPLEMENTATION_MASK) == MONO_IMPLEMENTATION_ASSEMBLYREF) {
368 name_token = mono_metadata_decode_row_col (&m->tables [MONO_TABLE_ASSEMBLYREF], idx - 1, MONO_ASSEMBLYREF_NAME);
369 fprintf (output, " .assembly extern '%s'\n", mono_metadata_string_heap (m, name_token));
372 fprintf (output, "}\n");
377 static dis_map_t visibility_map [] = {
378 { TYPE_ATTRIBUTE_NOT_PUBLIC, "private " },
379 { TYPE_ATTRIBUTE_PUBLIC, "public " },
380 { TYPE_ATTRIBUTE_NESTED_PUBLIC, "nested public " },
381 { TYPE_ATTRIBUTE_NESTED_PRIVATE, "nested private " },
382 { TYPE_ATTRIBUTE_NESTED_FAMILY, "nested family " },
383 { TYPE_ATTRIBUTE_NESTED_ASSEMBLY, "nested assembly " },
384 { TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM, "nested famandassem " },
385 { TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM, "nested famorassem " },
386 { 0, NULL }
389 static dis_map_t layout_map [] = {
390 { TYPE_ATTRIBUTE_AUTO_LAYOUT, "auto " },
391 { TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT, "sequential " },
392 { TYPE_ATTRIBUTE_EXPLICIT_LAYOUT, "explicit " },
393 { 0, NULL }
396 static dis_map_t format_map [] = {
397 { TYPE_ATTRIBUTE_ANSI_CLASS, "ansi " },
398 { TYPE_ATTRIBUTE_UNICODE_CLASS, "unicode " },
399 { TYPE_ATTRIBUTE_AUTO_CLASS, "auto " },
400 { 0, NULL }
403 static char *
404 typedef_flags (guint32 flags)
406 static char buffer [1024];
407 int visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
408 int layout = flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
409 int format = flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK;
411 buffer [0] = 0;
413 strcat (buffer, map (visibility, visibility_map));
414 strcat (buffer, map (layout, layout_map));
415 strcat (buffer, map (format, format_map));
417 if (flags & TYPE_ATTRIBUTE_ABSTRACT)
418 strcat (buffer, "abstract ");
419 if (flags & TYPE_ATTRIBUTE_SEALED)
420 strcat (buffer, "sealed ");
421 if (flags & TYPE_ATTRIBUTE_SPECIAL_NAME)
422 strcat (buffer, "specialname ");
423 if (flags & TYPE_ATTRIBUTE_IMPORT)
424 strcat (buffer, "import ");
425 if (flags & TYPE_ATTRIBUTE_SERIALIZABLE)
426 strcat (buffer, "serializable ");
427 if (flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
428 strcat (buffer, "beforefieldinit ");
429 if (flags & TYPE_ATTRIBUTE_FORWARDER)
430 strcat (buffer, "forwarder ");
432 return buffer;
436 * dis_field_list:
437 * @m: metadata context
438 * @start: starting index into the Field Table.
439 * @end: ending index into Field table.
441 * This routine displays all the decoded fields from @start to @end
443 static void
444 dis_field_list (MonoImage *m, guint32 start, guint32 end, MonoGenericContainer *container)
446 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
447 guint32 cols [MONO_FIELD_SIZE];
448 char *esname;
449 char rva_desc [32];
450 guint32 rva;
451 int i;
453 if (end > t->rows + 1) {
454 g_warning ("ERROR index out of range in fields");
455 end = t->rows;
458 for (i = start; i < end; i++){
459 char *sig, *flags, *attrs = NULL;
460 char *marshal_str = NULL;
461 guint32 field_offset = -1;
463 if (!should_include_field (i + 1))
464 continue;
465 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
466 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE], container);
467 flags = field_flags (cols [MONO_FIELD_FLAGS]);
469 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_MARSHAL) {
470 const char *tp;
471 MonoMarshalSpec *spec;
473 tp = mono_metadata_get_marshal_info (m, i, TRUE);
474 spec = mono_metadata_parse_marshal_spec (m, tp);
475 marshal_str = dis_stringify_marshal_spec (spec);
478 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
479 mono_metadata_field_info (m, i, NULL, &rva, NULL);
480 g_snprintf (rva_desc, sizeof (rva_desc), " at D_%08x", rva);
481 } else {
482 rva_desc [0] = 0;
485 mono_metadata_field_info (m, i, &field_offset, NULL, NULL);
486 if (field_offset != -1)
487 attrs = g_strdup_printf ("[%d]", field_offset);
488 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
489 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_DEFAULT){
490 char *lit;
491 guint32 const_cols [MONO_CONSTANT_SIZE];
492 guint32 crow;
494 if ((crow = mono_metadata_get_constant_index (m, MONO_TOKEN_FIELD_DEF | (i+1), 0))) {
495 mono_metadata_decode_row (&m->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
496 lit = get_constant (m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
497 } else {
498 lit = g_strdup ("not found");
501 fprintf (output, " .field %s%s%s %s = ",
502 flags, marshal_str ? marshal_str : " ", sig, esname);
503 fprintf (output, "%s\n", lit);
504 g_free (lit);
505 } else
506 fprintf (output, " .field %s %s%s%s %s%s\n",
507 attrs? attrs: "", flags, marshal_str ? marshal_str : " ", sig, esname, rva_desc);
508 g_free (attrs);
509 g_free (flags);
510 g_free (marshal_str);
511 g_free (sig);
512 g_free (esname);
513 dump_cattrs (m, MONO_TOKEN_FIELD_DEF | (i + 1), " ");
517 static dis_map_t method_access_map [] = {
518 { METHOD_ATTRIBUTE_COMPILER_CONTROLLED, "privatescope " },
519 { METHOD_ATTRIBUTE_PRIVATE, "private " },
520 { METHOD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
521 { METHOD_ATTRIBUTE_ASSEM, "assembly " },
522 { METHOD_ATTRIBUTE_FAMILY, "family " },
523 { METHOD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
524 { METHOD_ATTRIBUTE_PUBLIC, "public " },
525 { 0, NULL }
528 static dis_map_t method_flags_map [] = {
529 { METHOD_ATTRIBUTE_STATIC, "static " },
530 { METHOD_ATTRIBUTE_FINAL, "final " },
531 { METHOD_ATTRIBUTE_VIRTUAL, "virtual " },
532 { METHOD_ATTRIBUTE_HIDE_BY_SIG, "hidebysig " },
533 { METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK, "newslot " },
534 { METHOD_ATTRIBUTE_ABSTRACT, "abstract " },
535 { METHOD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
536 { METHOD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
537 { METHOD_ATTRIBUTE_UNMANAGED_EXPORT, "export " },
538 /* MS ilasm doesn't compile this statement - is must be added automagically when permissionset are present */
539 { METHOD_ATTRIBUTE_HAS_SECURITY, "" /*"hassecurity"*/ },
540 { METHOD_ATTRIBUTE_REQUIRE_SEC_OBJECT, "requiresecobj " },
541 { METHOD_ATTRIBUTE_PINVOKE_IMPL, "pinvokeimpl " },
542 { METHOD_ATTRIBUTE_STRICT, "strict " },
543 { 0, NULL }
547 * method_flags:
549 * Returns a stringified version of the Method's flags
551 static char *
552 method_flags (guint32 f)
554 GString *str = g_string_new ("");
555 int access = f & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK;
556 int rest = f & ~access;
557 char *s;
559 g_string_append (str, map (access, method_access_map));
560 g_string_append (str, flags (rest, method_flags_map));
562 s = str->str;
563 g_string_free (str, FALSE);
565 return s;
568 static dis_map_t pinvoke_flags_map [] = {
569 { PINVOKE_ATTRIBUTE_NO_MANGLE , "nomangle " },
570 { PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR, "lasterr " },
571 { PINVOKE_ATTRIBUTE_BEST_FIT_ENABLED, "bestfit:on " },
572 { PINVOKE_ATTRIBUTE_BEST_FIT_DISABLED, "bestfit:off " },
573 { PINVOKE_ATTRIBUTE_THROW_ON_UNMAPPABLE_ENABLED, "charmaperror:on " },
574 { PINVOKE_ATTRIBUTE_THROW_ON_UNMAPPABLE_DISABLED, "charmaperror:off " },
575 { 0, NULL }
578 static dis_map_t pinvoke_call_conv_map [] = {
579 { PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI, "winapi " },
580 { PINVOKE_ATTRIBUTE_CALL_CONV_CDECL, "cdecl " },
581 { PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL, "stdcall " },
582 { PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL, "thiscall " },
583 { PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL, "fastcall " },
584 { 0, "" },
585 { -1, NULL }
588 static dis_map_t pinvoke_char_set_map [] = {
589 { PINVOKE_ATTRIBUTE_CHAR_SET_NOT_SPEC, "" },
590 { PINVOKE_ATTRIBUTE_CHAR_SET_ANSI, "ansi " },
591 { PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE , "unicode " },
592 { PINVOKE_ATTRIBUTE_CHAR_SET_AUTO, "autochar " },
593 { 0, NULL }
597 * pinvoke_flags:
599 * Returns a stringified version of the Method's pinvoke flags
601 static char *
602 pinvoke_flags (guint32 f)
604 GString *str = g_string_new ("");
605 int cset = f & PINVOKE_ATTRIBUTE_CHAR_SET_MASK;
606 int cconv = f & PINVOKE_ATTRIBUTE_CALL_CONV_MASK;
607 int rest = f & ~(cset | cconv);
608 char *s;
610 g_string_append (str, map (cset, pinvoke_char_set_map));
611 g_string_append (str, map (cconv, pinvoke_call_conv_map));
612 g_string_append (str, flags (rest, pinvoke_flags_map));
614 s = g_strdup(str->str);
615 g_string_free (str, FALSE);
617 return s;
620 static void
621 dis_locals (MonoImage *m, MonoMethodHeader *mh, const char *ptr)
623 int i;
625 if (show_tokens) {
626 unsigned char flags = *(const unsigned char *) ptr;
627 unsigned char format = flags & METHOD_HEADER_FORMAT_MASK;
628 guint16 fat_flags;
629 guint32 local_var_sig_tok, init_locals;
631 g_assert (format == METHOD_HEADER_FAT_FORMAT);
632 fat_flags = read16 (ptr);
633 ptr += 2;
634 /* max_stack = read16 (ptr); */
635 ptr += 2;
636 /* code_size = read32 (ptr); */
637 ptr += 4;
638 local_var_sig_tok = read32 (ptr);
639 ptr += 4;
641 if (fat_flags & METHOD_HEADER_INIT_LOCALS)
642 init_locals = 1;
643 else
644 init_locals = 0;
646 fprintf(output, "\t.locals /*%08x*/ %s(\n",
647 local_var_sig_tok, init_locals ? "init " : "");
648 } else
649 fprintf(output, "\t.locals %s(\n", mh->init_locals ? "init " : "");
651 for (i=0; i < mh->num_locals; ++i) {
652 char * desc;
653 if (i)
654 fprintf(output, ",\n");
655 /* print also byref and pinned attributes */
656 desc = dis_stringify_type (m, mh->locals[i], TRUE);
657 fprintf(output, "\t\t%s\tV_%d", desc, i);
658 g_free(desc);
660 fprintf(output, ")\n");
663 static void
664 dis_code (MonoImage *m, guint32 token, guint32 rva, MonoGenericContainer *container)
666 MonoMethodHeader *mh;
667 const char *ptr = mono_image_rva_map (m, rva);
668 const char *loc;
669 gchar *override;
670 guint32 entry_point;
672 if (rva == 0)
673 return;
675 override = get_method_override (m, token, container);
676 if (override) {
677 fprintf (output, "\t.override %s\n", override);
678 g_free (override);
681 mh = mono_metadata_parse_mh_full (m, container, ptr);
682 entry_point = mono_image_get_entry_point (m);
683 if (entry_point && mono_metadata_token_index (entry_point) && mono_metadata_token_table (entry_point) == MONO_TABLE_METHOD) {
684 loc = mono_metadata_locate_token (m, entry_point);
685 if (rva == read32 (loc))
686 fprintf (output, "\t.entrypoint\n");
689 if (mh) {
690 fprintf (output, "\t// Code size %d (0x%x)\n", mh->code_size, mh->code_size);
691 fprintf (output, "\t.maxstack %d\n", mh->max_stack);
692 if (mh->num_locals)
693 dis_locals (m, mh, ptr);
694 disassemble_cil (m, mh, container);
696 hex_dump (mh->code, 0, mh->code_size);
697 printf ("\nAfter the code\n");
698 hex_dump (mh->code + mh->code_size, 0, 64);
700 mono_metadata_free_mh (mh);
704 static char *
705 pinvoke_info (MonoImage *m, guint32 mindex)
707 MonoTableInfo *im = &m->tables [MONO_TABLE_IMPLMAP];
708 MonoTableInfo *mr = &m->tables [MONO_TABLE_MODULEREF];
709 guint32 im_cols [MONO_IMPLMAP_SIZE];
710 guint32 mr_cols [MONO_MODULEREF_SIZE];
711 const char *import, *scope;
712 char *flags;
713 int i;
715 for (i = 0; i < im->rows; i++) {
717 mono_metadata_decode_row (im, i, im_cols, MONO_IMPLMAP_SIZE);
719 if ((im_cols [MONO_IMPLMAP_MEMBER] >> 1) == mindex + 1) {
721 flags = pinvoke_flags (im_cols [MONO_IMPLMAP_FLAGS]);
723 import = mono_metadata_string_heap (m, im_cols [MONO_IMPLMAP_NAME]);
725 mono_metadata_decode_row (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1,
726 mr_cols, MONO_MODULEREF_SIZE);
728 scope = mono_metadata_string_heap (m, mr_cols [MONO_MODULEREF_NAME]);
730 return g_strdup_printf ("(\"%s\" as \"%s\" %s)", scope, import,
731 flags);
732 g_free (flags);
736 return NULL;
740 * dump_cattrs_for_type_params
742 * @m:
743 * @token: TypeOrMethodDef token, owner for GenericParam
745 * Dumps the custom attributes for @token's type parameters
747 static void
748 dump_cattrs_for_type_params (MonoImage *m, guint32 token, const char *indent)
750 MonoTableInfo *tdef = &m->tables [MONO_TABLE_GENERICPARAM];
751 guint32 cols [MONO_GENERICPARAM_SIZE];
752 guint32 owner = 0, i;
753 GList *list = NULL;
755 if (! (i = mono_metadata_get_generic_param_row (m, token, &owner)))
756 return;
758 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
759 do {
760 list = dis_get_custom_attrs (m, mono_metadata_make_token (MONO_TABLE_GENERICPARAM, i));
761 if (list) {
762 fprintf (output, "%s.param type %s\n", indent, mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
763 dump_cattrs_list (list, indent);
766 if (++i > tdef->rows)
767 break;
768 mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
769 } while (cols [MONO_GENERICPARAM_OWNER] == owner);
772 static void
773 dump_cattrs_for_method_params (MonoImage *m, guint32 midx, MonoMethodSignature *sig) {
774 MonoTableInfo *methodt;
775 MonoTableInfo *paramt;
776 guint param_index, lastp, i;
778 methodt = &m->tables [MONO_TABLE_METHOD];
779 paramt = &m->tables [MONO_TABLE_PARAM];
780 param_index = mono_metadata_decode_row_col (methodt, midx, MONO_METHOD_PARAMLIST);
781 if (midx + 1 < methodt->rows)
782 lastp = mono_metadata_decode_row_col (methodt, midx + 1, MONO_METHOD_PARAMLIST);
783 else
784 lastp = paramt->rows + 1;
785 for (i = param_index; i < lastp; ++i) {
786 char *lit;
787 int crow;
788 guint32 param_cols [MONO_PARAM_SIZE];
789 GList *list;
791 list = dis_get_custom_attrs (m, MONO_TOKEN_PARAM_DEF | i);
793 mono_metadata_decode_row (paramt, i-1, param_cols, MONO_PARAM_SIZE);
794 if (!(param_cols[MONO_PARAM_FLAGS] & PARAM_ATTRIBUTE_HAS_DEFAULT)) {
795 if(list != NULL)
796 fprintf (output, "\t.param [%d]\n", param_cols[MONO_PARAM_SEQUENCE]);
797 } else {
798 fprintf (output, "\t.param [%d] = ", param_cols[MONO_PARAM_SEQUENCE]);
800 if ((crow = mono_metadata_get_constant_index(m, MONO_TOKEN_PARAM_DEF | i, 0))) {
801 guint32 const_cols [MONO_CONSTANT_SIZE];
802 mono_metadata_decode_row( &m->tables[MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
803 lit = get_constant(m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
805 else {
806 lit = g_strdup ("not found");
808 fprintf(output, "%s\n", lit);
809 g_free(lit);
811 dump_cattrs_list (list, "\t");
816 * dis_method_list:
817 * @m: metadata context
818 * @start: starting index into the Method Table.
819 * @end: ending index into Method table.
821 * This routine displays the methods in the Method Table from @start to @end
823 static void
824 dis_method_list (const char *klass_name, MonoImage *m, guint32 start, guint32 end, MonoGenericContainer *type_container)
826 MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
827 guint32 cols [MONO_METHOD_SIZE];
828 int i;
830 if (end > t->rows){
831 fprintf (output, "ERROR index out of range in methods");
832 /*exit (1);*/
833 end = t->rows;
836 for (i = start; i < end; i++){
837 MonoError error;
838 MonoMethodSignature *ms;
839 MonoGenericContainer *container;
840 char *flags, *impl_flags;
841 const char *sig, *method_name;
842 char *sig_str;
843 guint32 token;
845 if (!should_include_method (i + 1))
846 continue;
847 mono_metadata_decode_row (t, i, cols, MONO_METHOD_SIZE);
849 flags = method_flags (cols [MONO_METHOD_FLAGS]);
850 impl_flags = get_method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
852 sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
853 mono_metadata_decode_blob_size (sig, &sig);
855 container = mono_metadata_load_generic_params (m, MONO_TOKEN_METHOD_DEF | (i + 1), type_container);
856 if (container) {
857 MonoError error;
858 mono_metadata_load_generic_param_constraints_checked (m, MONO_TOKEN_METHOD_DEF | (i + 1), container, &error);
859 g_assert (mono_error_ok (&error)); /*FIXME don't swallow the error message*/
860 } else {
861 container = type_container;
864 ms = mono_metadata_parse_method_signature_full (m, container, i + 1, sig, &sig, &error);
865 if (ms != NULL){
866 sig_str = dis_stringify_method_signature (m, ms, i + 1, container, FALSE);
867 method_name = mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]);
868 } else {
869 sig_str = NULL;
870 method_name = g_strdup ("<NULL METHOD SIGNATURE>");
871 mono_error_cleanup (&error);
874 fprintf (output, " // method line %d\n", i + 1);
875 fprintf (output, " .method %s", flags);
877 if ((cols [MONO_METHOD_FLAGS] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (cols [MONO_METHOD_RVA] == 0)) {
878 gchar *pi = pinvoke_info (m, i);
879 if (pi) {
880 fprintf (output, "%s", pi);
881 g_free (pi);
885 fprintf (output, "\n %s", sig_str);
886 fprintf (output, " %s\n", impl_flags);
887 g_free (flags);
888 g_free (impl_flags);
890 token = MONO_TOKEN_METHOD_DEF | (i + 1);
892 fprintf (output, " {\n");
893 dump_cattrs (m, token, " ");
894 dump_cattrs_for_type_params (m, MONO_TOKEN_METHOD_DEF | (i + 1), " ");
895 dump_cattrs_for_method_params (m, i, ms);
897 fprintf (output, " // Method begins at RVA 0x%x\n", cols [MONO_METHOD_RVA]);
898 dump_declarative_security (m, OBJECT_TYPE_METHODDEF, i + 1, " ");
899 if (cols [MONO_METHOD_IMPLFLAGS] & METHOD_IMPL_ATTRIBUTE_NATIVE)
900 fprintf (output, " // Disassembly of native methods is not supported\n");
901 else
902 dis_code (m, token, cols [MONO_METHOD_RVA], container);
903 if (klass_name)
904 fprintf (output, " } // end of method %s::%s\n\n", klass_name, method_name);
905 else
906 fprintf (output, " } // end of global method %s\n\n", method_name);
907 mono_metadata_free_method_signature (ms);
908 g_free (sig_str);
912 typedef struct {
913 MonoTableInfo *t;
914 guint32 col_idx;
915 guint32 idx;
916 guint32 result;
917 } plocator_t;
919 static int
920 table_locator (const void *a, const void *b)
922 plocator_t *loc = (plocator_t *) a;
923 const char *bb = (const char *) b;
924 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
925 guint32 col;
927 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
929 if (loc->idx == col) {
930 loc->result = table_index;
931 return 0;
933 if (loc->idx < col)
934 return -1;
935 else
936 return 1;
939 static void
940 dis_property_methods (MonoImage *m, guint32 prop, MonoGenericContainer *container)
942 guint start, end;
943 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
944 guint32 cols [MONO_METHOD_SEMA_SIZE];
945 char *sig;
946 const char *type[] = {NULL, ".set", ".get", NULL, ".other"};
948 start = mono_metadata_methods_from_property (m, prop, &end);
949 for (; start < end; ++start) {
950 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
951 if (!should_include_method (cols [MONO_METHOD_SEMA_METHOD]))
952 continue;
953 sig = dis_stringify_method_signature_full (m, NULL, cols [MONO_METHOD_SEMA_METHOD], container, TRUE, FALSE);
954 fprintf (output, "\t\t%s %s\n", type [cols [MONO_METHOD_SEMA_SEMANTICS]], sig);
955 g_free (sig);
958 static char*
959 dis_property_signature (MonoImage *m, guint32 prop_idx, MonoGenericContainer *container)
961 MonoTableInfo *propt = &m->tables [MONO_TABLE_PROPERTY];
962 const char *ptr;
963 guint32 pcount, i;
964 guint32 cols [MONO_PROPERTY_SIZE];
965 MonoType *type;
966 MonoType *param;
967 char *blurb, *qk;
968 const char *name;
969 int prop_flags;
970 GString *res = g_string_new ("");
972 mono_metadata_decode_row (propt, prop_idx, cols, MONO_PROPERTY_SIZE);
973 name = mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]);
974 prop_flags = cols [MONO_PROPERTY_FLAGS];
975 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
976 mono_metadata_decode_blob_size (ptr, &ptr);
977 if (!(*ptr & 0x08))
978 g_warning("incorrect signature in property blob: 0x%x", *ptr);
979 if (*ptr & 0x20)
980 g_string_append (res, "instance ");
981 ptr++;
982 pcount = mono_metadata_decode_value (ptr, &ptr);
983 type = mono_metadata_parse_type_full (m, container, MONO_PARSE_TYPE, 0, ptr, &ptr);
984 blurb = dis_stringify_type (m, type, TRUE);
985 if (prop_flags & 0x0200)
986 g_string_append (res, "specialname ");
987 if (prop_flags & 0x0400)
988 g_string_append (res, "rtspecialname ");
989 qk = get_escaped_name (name);
990 g_string_append_printf (res, "%s %s (", blurb, qk);
991 g_free (qk);
992 g_free (blurb);
993 for (i = 0; i < pcount; i++) {
994 if (i)
995 g_string_append (res, ", ");
996 param = mono_metadata_parse_type_full (m, container, MONO_PARSE_PARAM, 0, ptr, &ptr);
997 blurb = dis_stringify_param (m, param);
998 g_string_append (res, blurb);
999 g_free (blurb);
1001 g_string_append_c (res, ')');
1002 blurb = res->str;
1003 g_string_free (res, FALSE);
1004 return blurb;
1008 static void
1009 dis_property_list (MonoImage *m, guint32 typedef_row, MonoGenericContainer *container)
1011 guint start, end, i;
1012 start = mono_metadata_properties_from_typedef (m, typedef_row, &end);
1014 for (i = start; i < end; ++i) {
1015 char *sig = dis_property_signature (m, i, container);
1016 fprintf (output, "\t.property %s\n\t{\n", sig);
1017 dump_cattrs (m, MONO_TOKEN_PROPERTY | (i + 1), "\t\t");
1018 dis_property_methods (m, i, container);
1019 fprintf (output, "\t}\n");
1020 g_free (sig);
1024 static char*
1025 dis_event_signature (MonoImage *m, guint32 event_idx, MonoGenericContainer *container)
1027 MonoTableInfo *et = &m->tables [MONO_TABLE_EVENT];
1028 char *type, *result, *esname;
1029 guint32 cols [MONO_EVENT_SIZE];
1030 int event_flags;
1031 GString *res = g_string_new ("");
1033 mono_metadata_decode_row (et, event_idx, cols, MONO_EVENT_SIZE);
1034 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]));
1035 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE], container);
1036 event_flags = cols [MONO_EVENT_FLAGS];
1038 if (event_flags & 0x0200)
1039 g_string_append (res, "specialname ");
1040 if (event_flags & 0x0400)
1041 g_string_append (res, "rtspecialname ");
1042 g_string_append_printf (res, "%s %s", type, esname);
1044 g_free (type);
1045 g_free (esname);
1046 result = res->str;
1047 g_string_free (res, FALSE);
1048 return result;
1051 static void
1052 dis_event_methods (MonoImage *m, guint32 event, MonoGenericContainer *container)
1054 guint start, end;
1055 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
1056 guint32 cols [MONO_METHOD_SEMA_SIZE];
1057 char *sig;
1058 const char *type = "";
1060 start = mono_metadata_methods_from_event (m, event, &end);
1061 for (; start < end; ++start) {
1062 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
1063 if (!should_include_method (cols [MONO_METHOD_SEMA_METHOD]))
1064 continue;
1065 sig = dis_stringify_method_signature_full (m, NULL, cols [MONO_METHOD_SEMA_METHOD], container, TRUE, FALSE);
1066 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
1067 case METHOD_SEMANTIC_OTHER:
1068 type = ".other"; break;
1069 case METHOD_SEMANTIC_ADD_ON:
1070 type = ".addon"; break;
1071 case METHOD_SEMANTIC_REMOVE_ON:
1072 type = ".removeon"; break;
1073 case METHOD_SEMANTIC_FIRE:
1074 type = ".fire"; break;
1075 default:
1076 break;
1078 fprintf (output, "\t\t%s %s\n", type, sig);
1079 g_free (sig);
1083 static void
1084 dis_event_list (MonoImage *m, guint32 typedef_row, MonoGenericContainer *container)
1086 guint start, end, i;
1087 start = mono_metadata_events_from_typedef (m, typedef_row, &end);
1089 for (i = start; i < end; ++i) {
1090 char *sig = dis_event_signature (m, i, container);
1091 fprintf (output, "\t.event %s\n\t{\n", sig);
1092 dump_cattrs (m, MONO_TOKEN_EVENT | (i + 1), "\t\t");
1093 dis_event_methods (m, i, container);
1094 fprintf (output, "\t}\n");
1095 g_free (sig);
1099 static void
1100 dis_interfaces (MonoImage *m, guint32 typedef_row, MonoGenericContainer *container)
1102 plocator_t loc;
1103 guint start;
1104 gboolean first_interface = 1;
1105 guint32 cols [MONO_INTERFACEIMPL_SIZE];
1106 char *intf;
1107 MonoTableInfo *table = &m->tables [MONO_TABLE_INTERFACEIMPL];
1109 if (!table->base)
1110 return;
1112 loc.t = table;
1113 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
1114 loc.idx = typedef_row;
1116 if (!mono_binary_search (&loc, table->base, table->rows, table->row_size, table_locator))
1117 return;
1119 start = loc.result;
1121 * We may end up in the middle of the rows...
1123 while (start > 0) {
1124 if (loc.idx == mono_metadata_decode_row_col (table, start - 1, MONO_INTERFACEIMPL_CLASS))
1125 start--;
1126 else
1127 break;
1129 while (start < table->rows) {
1130 mono_metadata_decode_row (table, start, cols, MONO_INTERFACEIMPL_SIZE);
1131 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
1132 break;
1133 intf = get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE], container);
1134 if (first_interface) {
1135 fprintf (output, " \timplements %s", intf);
1136 first_interface = 0;
1137 } else {
1138 fprintf (output, ", %s", intf);
1140 g_free (intf);
1141 ++start;
1146 * dis_type:
1147 * @m: metadata context
1148 * @n: index of type to disassemble
1149 * @is_nested: nested type ?
1150 * @forward: forward declarations?
1152 * Disassembles the type whose index in the TypeDef table is @n.
1154 static void
1155 dis_type (MonoImage *m, int n, int is_nested, int forward)
1157 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1158 guint32 cols [MONO_TYPEDEF_SIZE];
1159 guint32 cols_next [MONO_TYPEDEF_SIZE];
1160 const char *name, *nspace;
1161 char *esname, *param;
1162 MonoGenericContainer *container;
1163 guint32 packing_size, class_size;
1164 gboolean next_is_valid, last;
1165 guint32 nested;
1167 if (!should_include_type (n + 1))
1168 return;
1169 mono_metadata_decode_row (t, n, cols, MONO_TYPEDEF_SIZE);
1171 if (t->rows > n + 1) {
1172 mono_metadata_decode_row (t, n + 1, cols_next, MONO_TYPEDEF_SIZE);
1173 next_is_valid = 1;
1174 } else
1175 next_is_valid = 0;
1177 name = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]);
1178 nspace = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
1179 if (*nspace && !is_nested) {
1180 char *esnspace;
1181 esnspace = get_escaped_name (nspace);
1182 fprintf (output, ".namespace %s\n{\n", esnspace);
1183 g_free (esnspace);
1186 container = mono_metadata_load_generic_params (m, MONO_TOKEN_TYPE_DEF | (n + 1), NULL);
1187 if (container) {
1188 MonoError error;
1189 mono_metadata_load_generic_param_constraints_checked (m, MONO_TOKEN_TYPE_DEF | (n + 1), container, &error);
1190 g_assert (mono_error_ok (&error)); /*FIXME don't swallow the error message*/
1193 esname = get_escaped_name (name);
1194 if ((cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_CLASS_SEMANTIC_MASK) == TYPE_ATTRIBUTE_CLASS){
1195 fprintf (output, " .class %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
1197 param = get_generic_param (m, container);
1198 if (param) {
1199 fprintf (output, "%s", param);
1200 g_free (param);
1202 fprintf (output, "\n");
1203 if (cols [MONO_TYPEDEF_EXTENDS]) {
1204 char *base = get_typedef_or_ref (m, cols [MONO_TYPEDEF_EXTENDS], container);
1205 fprintf (output, " \textends %s\n", base);
1206 g_free (base);
1208 } else {
1209 fprintf (output, " .class interface %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
1211 param = get_generic_param (m, container);
1212 if (param) {
1213 fprintf (output, "%s", param);
1214 g_free (param);
1216 fprintf (output, "\n");
1219 g_free (esname);
1220 dis_interfaces (m, n + 1, container);
1221 fprintf (output, " {\n");
1222 if (!forward) {
1223 dump_cattrs (m, MONO_TOKEN_TYPE_DEF | (n + 1), " ");
1224 dump_cattrs_for_type_params (m, MONO_TOKEN_TYPE_DEF | (n + 1), " ");
1225 dump_declarative_security (m, OBJECT_TYPE_TYPEDEF, (n + 1), " ");
1227 if (mono_metadata_packing_from_typedef (m, n + 1, &packing_size, &class_size)) {
1228 fprintf (output, " .pack %d\n", packing_size);
1229 fprintf (output, " .size %d\n", class_size);
1232 * The value in the table is always valid, we know we have fields
1233 * if the value stored is different than the next record.
1236 if (next_is_valid)
1237 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1238 else
1239 last = m->tables [MONO_TABLE_FIELD].rows;
1241 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
1242 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last, container);
1243 fprintf (output, "\n");
1245 if (next_is_valid)
1246 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1247 else
1248 last = m->tables [MONO_TABLE_METHOD].rows;
1250 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
1251 dis_method_list (name, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last, container);
1253 dis_property_list (m, n, container);
1254 dis_event_list (m, n, container);
1257 t = &m->tables [MONO_TABLE_NESTEDCLASS];
1258 nested = mono_metadata_nesting_typedef (m, n + 1, 1);
1259 while (nested) {
1260 dis_type (m, mono_metadata_decode_row_col (t, nested - 1, MONO_NESTED_CLASS_NESTED) - 1, 1, forward);
1261 nested = mono_metadata_nesting_typedef (m, n + 1, nested + 1);
1264 fprintf (output, " } // end of class %s%s%s\n", nspace, *nspace? ".": "", name);
1265 if (*nspace && !is_nested)
1266 fprintf (output, "}\n");
1267 fprintf (output, "\n");
1272 * dis_globals
1273 * @m: metadata context
1275 * disassembles all the global fields and methods
1277 static void
1278 dis_globals (MonoImage *m)
1280 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1281 guint32 cols [MONO_TYPEDEF_SIZE];
1282 guint32 cols_next [MONO_TYPEDEF_SIZE];
1283 gboolean next_is_valid, last;
1285 mono_metadata_decode_row (t, 0, cols, MONO_TYPEDEF_SIZE);
1287 if (t->rows > 1) {
1288 mono_metadata_decode_row (t, 1, cols_next, MONO_TYPEDEF_SIZE);
1289 next_is_valid = 1;
1290 } else
1291 next_is_valid = 0;
1294 * The value in the table is always valid, we know we have fields
1295 * if the value stored is different than the next record.
1298 if (next_is_valid)
1299 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1300 else
1301 last = m->tables [MONO_TABLE_FIELD].rows;
1303 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
1304 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last, NULL);
1305 fprintf (output, "\n");
1307 if (next_is_valid)
1308 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1309 else
1310 last = m->tables [MONO_TABLE_METHOD].rows;
1312 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
1313 dis_method_list (NULL, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last, NULL);
1317 static void dis_resources_worker (MonoImage *m, gboolean just_print)
1319 MonoTableInfo *t = &m->tables [MONO_TABLE_MANIFESTRESOURCE];
1320 int i;
1322 for (i = 0; i < t->rows; i++){
1323 guint32 cols [MONO_MANIFEST_SIZE];
1324 const char *name, *res;
1325 guint32 size;
1326 FILE* fp;
1328 mono_metadata_decode_row (t, i, cols, MONO_MANIFEST_SIZE);
1329 name = mono_metadata_string_heap (m, cols [MONO_MANIFEST_NAME]);
1331 if (just_print)
1332 fprintf (output, "%8x: %s", cols [MONO_MANIFEST_OFFSET], name);
1334 if (! (res = mono_image_get_resource (m, cols [MONO_MANIFEST_OFFSET], &size))) {
1335 if (just_print)
1336 fprintf (output, " (absent from image)\n");
1337 continue;
1340 if (just_print) {
1341 fprintf (output, " (size %u)\n", size);
1342 continue;
1345 if ( (fp = fopen (name, "ab")) ) {
1346 if (ftell (fp) == 0)
1347 fwrite (res, size, 1, fp);
1348 else
1349 g_warning ("Error creating managed resource - %s : File already exists.", name);
1351 fclose (fp);
1352 } else
1353 g_warning ("Error creating managed resource - %s : %s", name, g_strerror (errno));
1357 static void
1358 dis_mresource (MonoImage *m)
1360 dis_resources_worker (m, FALSE);
1363 static void
1364 dis_presource (MonoImage *m)
1366 dis_resources_worker (m, TRUE);
1369 static char *
1370 exported_type_flags (guint32 flags)
1372 static char buffer [1024];
1373 int visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1375 buffer [0] = 0;
1377 if (flags & TYPE_ATTRIBUTE_FORWARDER) {
1378 strcat (buffer, "forwarder ");
1379 return buffer;
1382 strcat (buffer, map (visibility, visibility_map));
1383 return buffer;
1386 static char *
1387 get_escaped_fullname (MonoImage *m, guint32 nspace_idx, guint32 name_idx)
1389 const char *name, *nspace;
1390 char *fullname, *esfullname;
1392 nspace = mono_metadata_string_heap (m, nspace_idx);
1393 name = mono_metadata_string_heap (m, name_idx);
1395 fullname = g_strdup_printf ("%s%s%s", nspace, *nspace ? "." : "", name);
1396 esfullname = get_escaped_name (fullname);
1398 g_free (fullname);
1400 return esfullname;
1403 static void
1404 dis_exported_types (MonoImage *m)
1406 MonoTableInfo *t = &m->tables [MONO_TABLE_EXPORTEDTYPE];
1407 int i;
1409 for (i = 1; i <= t->rows; i++) {
1410 char *fullname;
1411 guint32 impl, idx, type_token;
1412 guint32 cols [MONO_EXP_TYPE_SIZE];
1414 mono_metadata_decode_row (t, i - 1, cols, MONO_EXP_TYPE_SIZE);
1416 fullname = get_escaped_fullname (m, cols [MONO_EXP_TYPE_NAMESPACE], cols [MONO_EXP_TYPE_NAME]);
1418 fprintf (output, "\n");
1419 fprintf (output, ".class extern %s%s\n", exported_type_flags (cols [MONO_EXP_TYPE_FLAGS]), fullname);
1420 fprintf (output, "{\n");
1422 g_free (fullname);
1424 impl = cols [MONO_EXP_TYPE_IMPLEMENTATION];
1425 if (impl) {
1426 idx = impl >> MONO_IMPLEMENTATION_BITS;
1427 switch (impl & MONO_IMPLEMENTATION_MASK) {
1428 case MONO_IMPLEMENTATION_FILE:
1429 fprintf (output, " .file '%s'\n",
1430 mono_metadata_string_heap (m, mono_metadata_decode_row_col (&m->tables [MONO_TABLE_FILE], idx - 1, MONO_FILE_NAME)));
1431 break;
1432 case MONO_IMPLEMENTATION_ASSEMBLYREF:
1433 fprintf (output, " .assembly extern '%s'\n",
1434 mono_metadata_string_heap (m, mono_metadata_decode_row_col (&m->tables [MONO_TABLE_ASSEMBLYREF], idx - 1, MONO_ASSEMBLYREF_NAME)));
1435 break;
1436 case MONO_IMPLEMENTATION_EXP_TYPE:
1437 fullname = get_escaped_fullname (
1439 mono_metadata_decode_row_col (&m->tables [MONO_TABLE_EXPORTEDTYPE], idx - 1, MONO_EXP_TYPE_NAMESPACE),
1440 mono_metadata_decode_row_col (&m->tables [MONO_TABLE_EXPORTEDTYPE], idx - 1, MONO_EXP_TYPE_NAME));
1441 fprintf (output, " .class extern %s\n", fullname);
1442 g_free (fullname);
1443 break;
1444 default:
1445 g_assert_not_reached ();
1446 break;
1450 type_token = cols [MONO_EXP_TYPE_TYPEDEF];
1451 if (type_token)
1452 fprintf (output, " .class 0x%08x\n", type_token | MONO_TOKEN_TYPE_DEF);
1454 fprintf (output, "}\n");
1459 * dis_types:
1460 * @m: metadata context
1462 * disassembles all types in the @m context
1464 static void
1465 dis_types (MonoImage *m, int forward)
1467 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1468 int i;
1469 guint32 flags;
1471 dis_globals (m);
1473 for (i = 1; i < t->rows; i++) {
1474 flags = mono_metadata_decode_row_col (t, i, MONO_TYPEDEF_FLAGS);
1475 flags &= TYPE_ATTRIBUTE_VISIBILITY_MASK;
1476 if (flags == TYPE_ATTRIBUTE_PUBLIC || flags == TYPE_ATTRIBUTE_NOT_PUBLIC)
1477 dis_type (m, i, 0, forward);
1481 static const char *
1482 get_uninitialized_data_type (guint32 size)
1484 switch (size) {
1485 case 1:
1486 return "int8";
1487 case 2:
1488 return "int16";
1489 case 4:
1490 return "int32";
1491 case 8:
1492 return "int64";
1493 default:
1494 g_error ("get_uninitialized_data_type for size: %d\n", size);
1496 return NULL;
1500 * dis_data:
1501 * @m: metadata context
1503 * disassembles all data blobs references in the FieldRVA table in the @m context
1505 static void
1506 dis_data (MonoImage *m)
1508 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
1509 MonoTableInfo *ft = &m->tables [MONO_TABLE_FIELD];
1510 int i, b;
1511 const char *rva, *sig;
1512 guint32 size;
1513 gint align;
1514 guint32 cols [MONO_FIELD_RVA_SIZE];
1515 MonoType *type;
1517 for (i = 0; i < t->rows; i++) {
1518 mono_metadata_decode_row (t, i, cols, MONO_FIELD_RVA_SIZE);
1519 rva = mono_image_rva_map (m, cols [MONO_FIELD_RVA_RVA]);
1520 sig = mono_metadata_blob_heap (m, mono_metadata_decode_row_col (ft, cols [MONO_FIELD_RVA_FIELD] -1, MONO_FIELD_SIGNATURE));
1521 mono_metadata_decode_value (sig, &sig);
1522 /* FIELD signature == 0x06 */
1523 g_assert (*sig == 0x06);
1524 type = mono_metadata_parse_field_type (m, 0, sig + 1, &sig);
1525 mono_class_init (mono_class_from_mono_type (type));
1526 size = mono_type_size (type, &align);
1528 if (rva) {
1529 fprintf (output, ".data D_%08x = bytearray (", cols [MONO_FIELD_RVA_RVA]);
1530 for (b = 0; b < size; ++b) {
1531 if (!(b % 16))
1532 fprintf (output, "\n\t");
1533 fprintf (output, " %02X", rva [b] & 0xff);
1535 fprintf (output, ") // size: %d\n", size);
1536 } else
1537 fprintf (output, ".data D_%08x = %s [%d]\n",
1538 cols [MONO_FIELD_RVA_RVA], get_uninitialized_data_type (size), size);
1542 struct {
1543 const char *name;
1544 int table;
1545 void (*dumper) (MonoImage *m);
1546 } table_list [] = {
1547 { "--assembly", MONO_TABLE_ASSEMBLY, dump_table_assembly },
1548 { "--assemblyref", MONO_TABLE_ASSEMBLYREF, dump_table_assemblyref },
1549 { "--classlayout", MONO_TABLE_CLASSLAYOUT, dump_table_class_layout },
1550 { "--constant", MONO_TABLE_CONSTANT, dump_table_constant },
1551 { "--customattr", MONO_TABLE_CUSTOMATTRIBUTE, dump_table_customattr },
1552 { "--declsec", MONO_TABLE_DECLSECURITY, dump_table_declsec },
1553 { "--event", MONO_TABLE_EVENT, dump_table_event },
1554 { "--exported", MONO_TABLE_EXPORTEDTYPE, dump_table_exported },
1555 { "--fields", MONO_TABLE_FIELD, dump_table_field },
1556 { "--file", MONO_TABLE_FILE, dump_table_file },
1557 { "--genericpar", MONO_TABLE_GENERICPARAM, dump_table_genericpar },
1558 { "--interface", MONO_TABLE_INTERFACEIMPL, dump_table_interfaceimpl },
1559 { "--manifest", MONO_TABLE_MANIFESTRESOURCE, dump_table_manifest },
1560 { "--marshal", MONO_TABLE_FIELDMARSHAL, dump_table_field_marshal },
1561 { "--memberref", MONO_TABLE_MEMBERREF, dump_table_memberref },
1562 { "--method", MONO_TABLE_METHOD, dump_table_method },
1563 { "--methodimpl", MONO_TABLE_METHODIMPL, dump_table_methodimpl },
1564 { "--methodsem", MONO_TABLE_METHODSEMANTICS, dump_table_methodsem },
1565 { "--methodspec", MONO_TABLE_METHODSPEC, dump_table_methodspec },
1566 { "--moduleref", MONO_TABLE_MODULEREF, dump_table_moduleref },
1567 { "--module", MONO_TABLE_MODULE, dump_table_module },
1568 { "--mresources", 0, dis_mresource },
1569 { "--presources", 0, dis_presource },
1570 { "--nested", MONO_TABLE_NESTEDCLASS, dump_table_nestedclass },
1571 { "--param", MONO_TABLE_PARAM, dump_table_param },
1572 { "--parconst", MONO_TABLE_GENERICPARAMCONSTRAINT, dump_table_parconstraint },
1573 { "--property", MONO_TABLE_PROPERTY, dump_table_property },
1574 { "--propertymap", MONO_TABLE_PROPERTYMAP, dump_table_property_map },
1575 { "--typedef", MONO_TABLE_TYPEDEF, dump_table_typedef },
1576 { "--typeref", MONO_TABLE_TYPEREF, dump_table_typeref },
1577 { "--typespec", MONO_TABLE_TYPESPEC, dump_table_typespec },
1578 { "--implmap", MONO_TABLE_IMPLMAP, dump_table_implmap },
1579 { "--fieldrva", MONO_TABLE_FIELDRVA, dump_table_fieldrva },
1580 { "--standalonesig", MONO_TABLE_STANDALONESIG, dump_table_standalonesig },
1581 { "--methodptr", MONO_TABLE_METHOD_POINTER, dump_table_methodptr },
1582 { "--fieldptr", MONO_TABLE_FIELD_POINTER, dump_table_fieldptr },
1583 { "--paramptr", MONO_TABLE_PARAM_POINTER, dump_table_paramptr },
1584 { "--eventptr", MONO_TABLE_EVENT_POINTER, dump_table_eventptr },
1585 { "--propertyptr", MONO_TABLE_PROPERTY_POINTER, dump_table_propertyptr },
1586 { "--blob", 0, dump_stream_blob },
1587 { "--strings", 0, dump_stream_strings },
1588 { "--userstrings", 0, dump_stream_us },
1589 { NULL, -1, }
1593 * disassemble_file:
1594 * @file: file containing CIL code.
1596 * Disassembles the @file file.
1598 static void
1599 disassemble_file (const char *file)
1601 MonoImageOpenStatus status;
1602 MonoImage *img;
1604 img = mono_image_open (file, &status);
1605 if (!img) {
1606 fprintf (stderr, "Error while trying to process %s\n", file);
1607 return;
1608 } else {
1609 /* FIXME: is this call necessary? */
1610 mono_assembly_load_from_full (img, file, &status, FALSE);
1613 setup_filter (img);
1615 if (dump_table != -1){
1616 (*table_list [dump_table].dumper) (img);
1617 } else {
1618 dump_header_data (img);
1620 dis_directive_assemblyref (img);
1621 dis_directive_assembly (img);
1622 dis_directive_file (img);
1623 dis_directive_mresource (img);
1624 dis_directive_module (img);
1625 dis_directive_moduleref (img);
1626 dis_exported_types (img);
1627 dis_nt_header (img);
1628 if (dump_managed_resources)
1629 dis_mresource (img);
1630 if (dump_forward_decls) {
1631 fprintf (output, "// *************** Forward Declarations for Classes ***************\n\n");
1632 dis_types (img, 1);
1633 fprintf (output, "// *************** End-Of Forward Declarations for Classes ***************\n\n");
1635 dis_types (img, 0);
1636 dis_data (img);
1639 mono_image_close (img);
1642 typedef struct {
1643 int size;
1644 int count;
1645 int *elems;
1646 } TableFilter;
1648 typedef struct {
1649 char *name;
1650 char *guid;
1651 TableFilter types;
1652 TableFilter fields;
1653 TableFilter methods;
1654 } ImageFilter;
1656 static GList *filter_list = NULL;
1657 static ImageFilter *cur_filter = NULL;
1659 static void
1660 setup_filter (MonoImage *image)
1662 ImageFilter *ifilter;
1663 GList *item;
1664 const char *name = mono_image_get_name (image);
1666 for (item = filter_list; item; item = item->next) {
1667 ifilter = item->data;
1668 if (strcmp (ifilter->name, name) == 0) {
1669 cur_filter = ifilter;
1670 return;
1673 cur_filter = NULL;
1676 static int
1677 int_cmp (const void *e1, const void *e2)
1679 const int *i1 = e1;
1680 const int *i2 = e2;
1681 return *i1 - *i2;
1684 static gboolean
1685 table_includes (TableFilter *tf, int idx)
1687 if (!tf->count)
1688 return FALSE;
1689 return mono_binary_search (&idx, tf->elems, tf->count, sizeof (int), int_cmp) != NULL;
1692 static gboolean
1693 should_include_type (int idx)
1695 if (!cur_filter)
1696 return TRUE;
1697 return table_includes (&cur_filter->types, idx);
1700 static gboolean
1701 should_include_method (int idx)
1703 if (!cur_filter)
1704 return TRUE;
1705 return table_includes (&cur_filter->methods, idx);
1708 static gboolean
1709 should_include_field (int idx)
1711 if (!cur_filter)
1712 return TRUE;
1713 return table_includes (&cur_filter->fields, idx);
1716 static ImageFilter*
1717 add_filter (const char *name)
1719 ImageFilter *ifilter;
1720 GList *item;
1722 for (item = filter_list; item; item = item->next) {
1723 ifilter = item->data;
1724 if (strcmp (ifilter->name, name) == 0)
1725 return ifilter;
1727 ifilter = g_new0 (ImageFilter, 1);
1728 ifilter->name = g_strdup (name);
1729 filter_list = g_list_prepend (filter_list, ifilter);
1730 return ifilter;
1733 static void
1734 add_item (TableFilter *tf, int val)
1736 if (tf->count >= tf->size) {
1737 if (!tf->size) {
1738 tf->size = 8;
1739 tf->elems = g_malloc (sizeof (int) * tf->size);
1740 } else {
1741 tf->size *= 2;
1742 tf->elems = g_realloc (tf->elems, sizeof (int) * tf->size);
1745 tf->elems [tf->count++] = val;
1748 static void
1749 sort_filter_elems (void)
1751 ImageFilter *ifilter;
1752 GList *item;
1754 for (item = filter_list; item; item = item->next) {
1755 ifilter = item->data;
1756 qsort (ifilter->types.elems, ifilter->types.count, sizeof (int), int_cmp);
1757 qsort (ifilter->fields.elems, ifilter->fields.count, sizeof (int), int_cmp);
1758 qsort (ifilter->methods.elems, ifilter->methods.count, sizeof (int), int_cmp);
1762 static void
1763 load_filter (const char* filename)
1765 FILE *file;
1766 char buf [1024];
1767 char *p, *s, *endptr;
1768 int line = 0;
1769 ImageFilter *ifilter = NULL;
1770 int value = 0;
1772 if (!(file = fopen (filename, "r"))) {
1773 g_print ("Cannot open filter file '%s'\n", filename);
1774 exit (1);
1776 while (fgets (buf, sizeof (buf), file) != NULL) {
1777 ++line;
1778 s = buf;
1779 while (*s && g_ascii_isspace (*s)) ++s;
1780 switch (*s) {
1781 case 0:
1782 case '#':
1783 break;
1784 case '[':
1785 p = strchr (s, ']');
1786 if (!p)
1787 g_error ("No matching ']' in filter at line %d\n", line);
1788 *p = 0;
1789 ifilter = add_filter (s + 1);
1790 break;
1791 case 'T':
1792 if (!ifilter)
1793 g_error ("Invalid format in filter at line %d\n", line);
1794 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1795 g_error ("Invalid type number in filter at line %d\n", line);
1796 add_item (&ifilter->types, value);
1797 break;
1798 case 'M':
1799 if (!ifilter)
1800 g_error ("Invalid format in filter at line %d\n", line);
1801 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1802 g_error ("Invalid method number in filter at line %d\n", line);
1803 add_item (&ifilter->methods, value);
1804 break;
1805 case 'F':
1806 if (!ifilter)
1807 g_error ("Invalid format in filter at line %d\n", line);
1808 if ((s [1] != ':') || !(value = strtol (s + 2, &endptr, 0)) || (endptr == s + 2))
1809 g_error ("Invalid field number in filter at line %d\n", line);
1810 add_item (&ifilter->fields, value);
1811 break;
1812 default:
1813 g_error ("Invalid format in filter at line %d\n", line);
1816 fclose (file);
1817 sort_filter_elems ();
1821 static gboolean
1822 try_load_from (MonoAssembly **assembly, const gchar *path1, const gchar *path2,
1823 const gchar *path3, const gchar *path4, gboolean refonly)
1825 gchar *fullpath;
1827 *assembly = NULL;
1828 fullpath = g_build_filename (path1, path2, path3, path4, NULL);
1829 if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR))
1830 *assembly = mono_assembly_open_full (fullpath, NULL, refonly);
1832 g_free (fullpath);
1833 return (*assembly != NULL);
1836 static MonoAssembly *
1837 real_load (gchar **search_path, const gchar *culture, const gchar *name, gboolean refonly)
1839 MonoAssembly *result = NULL;
1840 gchar **path;
1841 gchar *filename;
1842 const gchar *local_culture;
1843 gint len;
1845 if (!culture || *culture == '\0') {
1846 local_culture = "";
1847 } else {
1848 local_culture = culture;
1851 filename = g_strconcat (name, ".dll", NULL);
1852 len = strlen (filename);
1854 for (path = search_path; *path; path++) {
1855 if (**path == '\0')
1856 continue; /* Ignore empty ApplicationBase */
1858 /* See test cases in bug #58992 and bug #57710 */
1859 /* 1st try: [culture]/[name].dll (culture may be empty) */
1860 strcpy (filename + len - 4, ".dll");
1861 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
1862 break;
1864 /* 2nd try: [culture]/[name].exe (culture may be empty) */
1865 strcpy (filename + len - 4, ".exe");
1866 if (try_load_from (&result, *path, local_culture, "", filename, refonly))
1867 break;
1869 /* 3rd try: [culture]/[name]/[name].dll (culture may be empty) */
1870 strcpy (filename + len - 4, ".dll");
1871 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
1872 break;
1874 /* 4th try: [culture]/[name]/[name].exe (culture may be empty) */
1875 strcpy (filename + len - 4, ".exe");
1876 if (try_load_from (&result, *path, local_culture, name, filename, refonly))
1877 break;
1880 g_free (filename);
1881 return result;
1885 * Try to load referenced assemblies from assemblies_path.
1887 static MonoAssembly *
1888 monodis_preload (MonoAssemblyName *aname,
1889 gchar **assemblies_path,
1890 gpointer user_data)
1892 MonoAssembly *result = NULL;
1893 gboolean refonly = GPOINTER_TO_UINT (user_data);
1895 if (assemblies_path && assemblies_path [0] != NULL) {
1896 result = real_load (assemblies_path, aname->culture, aname->name, refonly);
1899 return result;
1902 static GList *loaded_assemblies = NULL;
1904 static void
1905 monodis_assembly_load_hook (MonoAssembly *assembly, gpointer user_data)
1907 loaded_assemblies = g_list_prepend (loaded_assemblies, assembly);
1910 static MonoAssembly *
1911 monodis_assembly_search_hook (MonoAssemblyName *aname, gpointer user_data)
1913 GList *tmp;
1915 for (tmp = loaded_assemblies; tmp; tmp = tmp->next) {
1916 MonoAssembly *ass = tmp->data;
1917 if (mono_assembly_names_equal (aname, &ass->aname))
1918 return ass;
1920 return NULL;
1923 static void
1924 usage (void)
1926 GString *args = g_string_new ("[--output=filename] [--filter=filename] [--help] [--mscorlib]\n");
1927 int i;
1929 for (i = 0; table_list [i].name != NULL; i++){
1930 g_string_append (args, "[");
1931 g_string_append (args, table_list [i].name);
1932 g_string_append (args, "] ");
1933 if (((i-2) % 5) == 0)
1934 g_string_append_c (args, '\n');
1936 g_string_append (args, "[--forward-decls]");
1937 fprintf (stderr,
1938 "monodis -- Mono Common Intermediate Language Disassembler\n"
1939 "Usage is: monodis %s file ..\n", args->str);
1940 exit (1);
1944 main (int argc, char *argv [])
1946 GList *input_files = NULL, *l;
1947 int i, j;
1949 output = stdout;
1950 init_key_table ();
1951 for (i = 1; i < argc; i++){
1952 if (argv [i][0] == '-'){
1953 if (argv [i][1] == 'h')
1954 usage ();
1955 else if (argv [i][1] == 'd')
1956 dump_header_data_p = TRUE;
1957 else if (strcmp (argv [i], "--mscorlib") == 0) {
1958 substitute_with_mscorlib_p = TRUE;
1959 continue;
1960 } else if (strcmp (argv [i], "--show-method-tokens") == 0) {
1961 show_method_tokens = TRUE;
1962 continue;
1963 } else if (strcmp (argv [i], "--show-tokens") == 0) {
1964 show_tokens = TRUE;
1965 continue;
1966 } else if (strncmp (argv [i], "--output=", 9) == 0) {
1967 output = fopen (argv [i]+9, "w");
1968 if (output == NULL) {
1969 fprintf (stderr, "Can't open output file `%s': %s\n",
1970 argv [i]+9, strerror (errno));
1971 exit (1);
1973 dump_managed_resources = TRUE;
1974 continue;
1975 } else if (strncmp (argv [i], "--filter=", 9) == 0) {
1976 load_filter (argv [i]+9);
1977 continue;
1978 } else if (strcmp (argv [i], "--forward-decls") == 0) {
1979 dump_forward_decls = TRUE;
1980 continue;
1981 } else if (strcmp (argv [i], "--help") == 0)
1982 usage ();
1983 for (j = 0; table_list [j].name != NULL; j++) {
1984 if (strcmp (argv [i], table_list [j].name) == 0)
1985 dump_table = j;
1987 if (dump_table < 0)
1988 usage ();
1989 } else
1990 input_files = g_list_append (input_files, argv [i]);
1993 if (input_files == NULL)
1994 usage ();
1996 mono_install_assembly_load_hook (monodis_assembly_load_hook, NULL);
1997 mono_install_assembly_search_hook (monodis_assembly_search_hook, NULL);
2000 * If we just have one file, use the corlib version it requires.
2002 if (!input_files->next) {
2003 char *filename = input_files->data;
2005 mono_init_from_assembly (argv [0], filename);
2007 mono_install_assembly_preload_hook (monodis_preload, GUINT_TO_POINTER (FALSE));
2009 disassemble_file (filename);
2010 } else {
2011 mono_init (argv [0]);
2013 for (l = input_files; l; l = l->next)
2014 disassemble_file (l->data);
2017 return 0;