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>
32 /* True if you want to get a dump of the header data */
33 gboolean dump_header_data_p = FALSE;
35 gboolean substitute_with_mscorlib_p = FALSE;
40 dump_header_data (MonoImage *img)
42 if (!dump_header_data_p)
46 "// Ximian's CIL disassembler, version 1.0\n"
47 "// Copyright (C) 2001 Ximian, Inc.\n\n");
51 dump_cattrs (MonoImage *m, guint32 token, const char *indent)
55 list = dis_get_custom_attrs (m, token);
56 for (tmp = list; tmp; tmp = tmp->next) {
57 fprintf (output, "%s%s\n", indent, (char*)tmp->data);
64 dis_directive_assembly (MonoImage *m)
66 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLY];
67 guint32 cols [MONO_ASSEMBLY_SIZE];
72 mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
74 fprintf (output, ".assembly '%s'\n{\n",
75 mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
76 dump_cattrs (m, MONO_TOKEN_ASSEMBLY | 1, " ");
78 " .hash algorithm 0x%08x\n"
79 " .ver %d:%d:%d:%d\n",
80 cols [MONO_ASSEMBLY_HASH_ALG],
81 cols [MONO_ASSEMBLY_MAJOR_VERSION], cols [MONO_ASSEMBLY_MINOR_VERSION],
82 cols [MONO_ASSEMBLY_BUILD_NUMBER], cols [MONO_ASSEMBLY_REV_NUMBER]);
83 if (cols [MONO_ASSEMBLY_CULTURE])
84 fprintf (output, " .locale %s\n", mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]));
85 if (cols [MONO_ASSEMBLY_PUBLIC_KEY]) {
86 const char* b = mono_metadata_blob_heap (m, cols [MONO_ASSEMBLY_PUBLIC_KEY]);
87 int len = mono_metadata_decode_blob_size (b, &b);
88 char *dump = data_dump (b, len, "\t\t");
89 fprintf (output, " .publickey =%s", dump);
92 fprintf (output, "}\n");
96 dis_directive_assemblyref (MonoImage *m)
98 MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
99 guint32 cols [MONO_ASSEMBLYREF_SIZE];
105 for (i = 0; i < t->rows; i++){
106 mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
109 ".assembly extern %s\n"
111 " .ver %d:%d:%d:%d\n"
113 mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]),
114 cols [MONO_ASSEMBLYREF_MAJOR_VERSION], cols [MONO_ASSEMBLYREF_MINOR_VERSION],
115 cols [MONO_ASSEMBLYREF_BUILD_NUMBER], cols [MONO_ASSEMBLYREF_REV_NUMBER]
121 dis_directive_module (MonoImage *m)
123 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULE];
126 for (i = 0; i < t->rows; i++){
127 guint32 cols [MONO_MODULE_SIZE];
131 mono_metadata_decode_row (t, i, cols, MONO_MODULE_SIZE);
133 name = mono_metadata_string_heap (m, cols [MONO_MODULE_NAME]);
134 ename = get_escaped_name (name);
135 guid = get_guid (m, cols [MONO_MODULE_MVID]);
136 fprintf (output, ".module %s // GUID = %s\n\n", ename, guid);
139 dump_cattrs (m, MONO_TOKEN_MODULE | (i + 1), "");
144 dis_directive_moduleref (MonoImage *m)
146 MonoTableInfo *t = &m->tables [MONO_TABLE_MODULEREF];
149 for (i = 0; i < t->rows; i++){
150 guint32 cols [MONO_MODULEREF_SIZE];
153 mono_metadata_decode_row (t, i, cols, MONO_MODULEREF_SIZE);
155 name = mono_metadata_string_heap (m, cols [MONO_MODULEREF_NAME]);
156 fprintf (output, ".module extern %s\n", name);
162 dis_directive_file (MonoImage *m)
164 MonoTableInfo *t = &m->tables [MONO_TABLE_FILE];
168 entry_point = mono_image_get_entry_point (m);
170 for (i = 0; i < t->rows; i++){
171 guint32 cols [MONO_FILE_SIZE];
172 const char *name, *hash;
175 mono_metadata_decode_row (t, i, cols, MONO_FILE_SIZE);
177 name = mono_metadata_string_heap (m, cols [MONO_FILE_NAME]);
179 hash = mono_metadata_blob_heap (m, cols [MONO_FILE_HASH_VALUE]);
180 len = mono_metadata_decode_blob_size (hash, &hash);
182 fprintf (output, ".file %s%s .hash = (", name,
183 cols [MONO_FILE_FLAGS] & FILE_CONTAINS_NO_METADATA ? " nometadata" : "");
185 for (j = 0; j < len; ++j)
186 fprintf (output, " %02X", hash [j] & 0xff);
188 token = mono_metadata_make_token (MONO_TABLE_FILE, i + 1);
189 fprintf (output, " )%s\n", (token == entry_point) ? " .entrypoint" : "");
194 static dis_map_t visibility_map [] = {
195 { TYPE_ATTRIBUTE_NOT_PUBLIC, "private " },
196 { TYPE_ATTRIBUTE_PUBLIC, "public " },
197 { TYPE_ATTRIBUTE_NESTED_PUBLIC, "nested public " },
198 { TYPE_ATTRIBUTE_NESTED_PRIVATE, "nested private " },
199 { TYPE_ATTRIBUTE_NESTED_FAMILY, "nested family " },
200 { TYPE_ATTRIBUTE_NESTED_ASSEMBLY, "nested assembly" },
201 { TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM, "nested famandassem" },
202 { TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM, "nested famorassem" },
206 static dis_map_t layout_map [] = {
207 { TYPE_ATTRIBUTE_AUTO_LAYOUT, "auto " },
208 { TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT, "sequential " },
209 { TYPE_ATTRIBUTE_EXPLICIT_LAYOUT, "explicit " },
213 static dis_map_t format_map [] = {
214 { TYPE_ATTRIBUTE_ANSI_CLASS, "ansi " },
215 { TYPE_ATTRIBUTE_UNICODE_CLASS, "unicode " },
216 { TYPE_ATTRIBUTE_AUTO_CLASS, "auto " },
221 typedef_flags (guint32 flags)
223 static char buffer [1024];
224 int visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
225 int layout = flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
226 int format = flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK;
230 strcat (buffer, map (visibility, visibility_map));
231 strcat (buffer, map (layout, layout_map));
232 strcat (buffer, map (format, format_map));
234 if (flags & TYPE_ATTRIBUTE_ABSTRACT)
235 strcat (buffer, "abstract ");
236 if (flags & TYPE_ATTRIBUTE_SEALED)
237 strcat (buffer, "sealed ");
238 if (flags & TYPE_ATTRIBUTE_SPECIAL_NAME)
239 strcat (buffer, "special-name ");
240 if (flags & TYPE_ATTRIBUTE_IMPORT)
241 strcat (buffer, "import ");
242 if (flags & TYPE_ATTRIBUTE_SERIALIZABLE)
243 strcat (buffer, "serializable ");
244 if (flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
245 strcat (buffer, "beforefieldinit ");
252 * @m: metadata context
253 * @start: starting index into the Field Table.
254 * @end: ending index into Field table.
256 * This routine displays all the decoded fields from @start to @end
259 dis_field_list (MonoImage *m, guint32 start, guint32 end)
261 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
262 guint32 cols [MONO_FIELD_SIZE];
268 if (end > t->rows + 1) {
269 g_warning ("ERROR index out of range in fields");
273 for (i = start; i < end; i++){
274 char *sig, *flags, *attrs = NULL;
275 guint32 field_offset = -1;
277 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
278 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
279 flags = field_flags (cols [MONO_FIELD_FLAGS]);
281 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
282 mono_metadata_field_info (m, i, NULL, &rva, NULL);
283 g_snprintf (rva_desc, sizeof (rva_desc), " at D_%08x", rva);
288 mono_metadata_field_info (m, i, &field_offset, NULL, NULL);
289 if (field_offset != -1)
290 attrs = g_strdup_printf ("[%d]", field_offset);
291 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
292 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_LITERAL){
294 guint32 const_cols [MONO_CONSTANT_SIZE];
297 if ((crow = mono_metadata_get_constant_index (m, MONO_TOKEN_FIELD_DEF | (i+1), 0))) {
298 mono_metadata_decode_row (&m->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
299 lit = get_constant (m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
301 lit = g_strdup ("not found");
305 fprintf (output, " .field %s %s %s = ",
307 fprintf (output, "%s\n", lit);
310 fprintf (output, " .field %s %s %s %s%s\n",
311 attrs? attrs: "", flags, sig, esname, rva_desc);
316 dump_cattrs (m, MONO_TOKEN_FIELD_DEF | (i + 1), " ");
320 static dis_map_t method_access_map [] = {
321 { METHOD_ATTRIBUTE_COMPILER_CONTROLLED, "compilercontrolled " },
322 { METHOD_ATTRIBUTE_PRIVATE, "private " },
323 { METHOD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
324 { METHOD_ATTRIBUTE_ASSEM, "assembly " },
325 { METHOD_ATTRIBUTE_FAMILY, "family " },
326 { METHOD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
327 { METHOD_ATTRIBUTE_PUBLIC, "public " },
331 static dis_map_t method_flags_map [] = {
332 { METHOD_ATTRIBUTE_STATIC, "static " },
333 { METHOD_ATTRIBUTE_FINAL, "final " },
334 { METHOD_ATTRIBUTE_VIRTUAL, "virtual " },
335 { METHOD_ATTRIBUTE_HIDE_BY_SIG, "hidebysig " },
336 { METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK, "newslot " },
337 { METHOD_ATTRIBUTE_ABSTRACT, "abstract " },
338 { METHOD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
339 { METHOD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
340 { METHOD_ATTRIBUTE_UNMANAGED_EXPORT, "export " },
341 { METHOD_ATTRIBUTE_HAS_SECURITY, "hassecurity" },
342 { METHOD_ATTRIBUTE_REQUIRE_SEC_OBJECT, "requiresecobj" },
343 { METHOD_ATTRIBUTE_PINVOKE_IMPL, "pinvokeimpl " },
350 * Returns a stringified version of the Method's flags
353 method_flags (guint32 f)
355 GString *str = g_string_new ("");
356 int access = f & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK;
359 g_string_append (str, map (access, method_access_map));
360 g_string_append (str, flags (f, method_flags_map));
363 g_string_free (str, FALSE);
368 static dis_map_t pinvoke_flags_map [] = {
369 { PINVOKE_ATTRIBUTE_NO_MANGLE , "nomangle " },
370 { PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR, "lasterr " },
374 static dis_map_t pinvoke_call_conv_map [] = {
375 { PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI, "winapi " },
376 { PINVOKE_ATTRIBUTE_CALL_CONV_CDECL, "cdecl " },
377 { PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL, "stdcall " },
378 { PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL, "thiscall " },
379 { PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL, "fastcall " },
383 static dis_map_t pinvoke_char_set_map [] = {
384 { PINVOKE_ATTRIBUTE_CHAR_SET_NOT_SPEC, "" },
385 { PINVOKE_ATTRIBUTE_CHAR_SET_ANSI, "ansi " },
386 { PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE , "unicode " },
387 { PINVOKE_ATTRIBUTE_CHAR_SET_AUTO, "autochar " },
394 * Returns a stringified version of the Method's pinvoke flags
397 pinvoke_flags (guint32 f)
399 GString *str = g_string_new ("");
400 int cset = f & PINVOKE_ATTRIBUTE_CHAR_SET_MASK;
401 int cconv = f & PINVOKE_ATTRIBUTE_CALL_CONV_MASK;
404 g_string_append (str, map (cset, pinvoke_char_set_map));
405 g_string_append (str, map (cconv, pinvoke_call_conv_map));
406 g_string_append (str, flags (f, pinvoke_flags_map));
408 s = g_strdup(str->str);
409 g_string_free (str, FALSE);
414 static dis_map_t method_impl_map [] = {
415 { METHOD_IMPL_ATTRIBUTE_IL, "cil " },
416 { METHOD_IMPL_ATTRIBUTE_NATIVE, "native " },
417 { METHOD_IMPL_ATTRIBUTE_OPTIL, "optil " },
418 { METHOD_IMPL_ATTRIBUTE_RUNTIME, "runtime " },
422 static dis_map_t managed_type_map [] = {
423 { METHOD_IMPL_ATTRIBUTE_UNMANAGED, "unmanaged " },
424 { METHOD_IMPL_ATTRIBUTE_MANAGED, "managed " },
428 static dis_map_t managed_impl_flags [] = {
429 { METHOD_IMPL_ATTRIBUTE_FORWARD_REF, "fwdref " },
430 { METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG, "preservesig " },
431 { METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL, "internalcall " },
432 { METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED, "synchronized " },
433 { METHOD_IMPL_ATTRIBUTE_NOINLINING, "noinline " },
438 method_impl_flags (guint32 f)
440 GString *str = g_string_new ("");
442 int code_type = f & METHOD_IMPL_ATTRIBUTE_CODE_TYPE_MASK;
443 int managed_type = f & METHOD_IMPL_ATTRIBUTE_MANAGED_MASK;
445 g_string_append (str, map (code_type, method_impl_map));
446 g_string_append (str, map (managed_type, managed_type_map));
447 g_string_append (str, flags (f, managed_impl_flags));
450 g_string_free (str, FALSE);
455 dis_locals (MonoImage *m, MonoMethodHeader *mh)
459 fprintf(output, "\t.locals %s(\n", mh->init_locals ? "init " : "");
460 for (i=0; i < mh->num_locals; ++i) {
463 fprintf(output, ",\n");
464 /* print also byref and pinned attributes */
465 desc = dis_stringify_type (m, mh->locals[i]);
466 fprintf(output, "\t\t%s\tV_%d", desc, i);
469 fprintf(output, ")\n");
473 dis_code (MonoImage *m, guint32 rva)
475 MonoMethodHeader *mh;
476 MonoCLIImageInfo *ii = m->image_info;
477 const char *ptr = mono_cli_rva_map (ii, rva);
484 mh = mono_metadata_parse_mh (m, ptr);
485 if ((entry_point = mono_image_get_entry_point (m))){
486 loc = mono_metadata_locate_token (m, entry_point);
487 if (rva == read32 (loc))
488 fprintf (output, "\t.entrypoint\n");
491 fprintf (output, "\t// Code size %d (0x%x)\n", mh->code_size, mh->code_size);
492 fprintf (output, "\t.maxstack %d\n", mh->max_stack);
495 dissasemble_cil (m, mh);
498 hex_dump (mh->code, 0, mh->code_size);
499 printf ("\nAfter the code\n");
500 hex_dump (mh->code + mh->code_size, 0, 64);
502 mono_metadata_free_mh (mh);
506 pinvoke_info (MonoImage *m, guint32 mindex)
508 MonoTableInfo *im = &m->tables [MONO_TABLE_IMPLMAP];
509 MonoTableInfo *mr = &m->tables [MONO_TABLE_MODULEREF];
510 guint32 im_cols [MONO_IMPLMAP_SIZE];
511 guint32 mr_cols [MONO_MODULEREF_SIZE];
512 const char *import, *scope;
516 for (i = 0; i < im->rows; i++) {
518 mono_metadata_decode_row (im, i, im_cols, MONO_IMPLMAP_SIZE);
520 if ((im_cols [MONO_IMPLMAP_MEMBER] >> 1) == mindex + 1) {
522 flags = pinvoke_flags (im_cols [MONO_IMPLMAP_FLAGS]);
524 import = mono_metadata_string_heap (m, im_cols [MONO_IMPLMAP_NAME]);
526 mono_metadata_decode_row (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1,
527 mr_cols, MONO_MODULEREF_SIZE);
529 scope = mono_metadata_string_heap (m, mr_cols [MONO_MODULEREF_NAME]);
531 return g_strdup_printf ("(\"%s\" as \"%s\" %s)", scope, import,
541 cattrs_for_method (MonoImage *m, guint32 midx, MonoMethodSignature *sig) {
542 MonoTableInfo *methodt;
543 MonoTableInfo *paramt;
544 guint param_index, lastp, i, pid;
546 methodt = &m->tables [MONO_TABLE_METHOD];
547 paramt = &m->tables [MONO_TABLE_PARAM];
548 param_index = mono_metadata_decode_row_col (methodt, midx, MONO_METHOD_PARAMLIST);
549 if (midx + 1 < methodt->rows)
550 lastp = mono_metadata_decode_row_col (methodt, midx + 1, MONO_METHOD_PARAMLIST);
552 lastp = paramt->rows + 1;
553 for (i = param_index; i < lastp; ++i) {
554 pid = mono_metadata_decode_row_col (paramt, i - 1, MONO_PARAM_SEQUENCE);
555 fprintf (output, "\t.param [%d]\n", pid);
556 dump_cattrs (m, MONO_TOKEN_PARAM_DEF | i, "\t");
562 * @m: metadata context
563 * @start: starting index into the Method Table.
564 * @end: ending index into Method table.
566 * This routine displays the methods in the Method Table from @start to @end
569 dis_method_list (const char *klass_name, MonoImage *m, guint32 start, guint32 end)
571 MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
572 guint32 cols [MONO_METHOD_SIZE];
576 fprintf (output, "ERROR index out of range in methods");
581 for (i = start; i < end; i++){
582 MonoMethodSignature *ms;
583 char *flags, *impl_flags;
587 mono_metadata_decode_row (t, i, cols, MONO_METHOD_SIZE);
589 flags = method_flags (cols [MONO_METHOD_FLAGS]);
590 impl_flags = method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
592 sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
593 mono_metadata_decode_blob_size (sig, &sig);
594 ms = mono_metadata_parse_method_signature (m, i + 1, sig, &sig);
595 sig_str = dis_stringify_method_signature (m, ms, i + 1, FALSE);
597 fprintf (output, " // method line %d\n", i + 1);
598 fprintf (output, " .method %s", flags);
600 if ((cols [MONO_METHOD_FLAGS] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (cols [MONO_METHOD_RVA] == 0)) {
601 gchar *pi = pinvoke_info (m, i);
603 fprintf (output, "%s", pi);
608 fprintf (output, "\n %s", sig_str);
609 fprintf (output, " %s\n", impl_flags);
613 fprintf (output, " {\n");
614 dump_cattrs (m, MONO_TOKEN_METHOD_DEF | (i + 1), " ");
615 cattrs_for_method (m, i, ms);
616 /* FIXME: need to sump also param custom attributes */
617 fprintf (output, " // Method begins at RVA 0x%x\n", cols [MONO_METHOD_RVA]);
618 if (cols [MONO_METHOD_IMPLFLAGS] & METHOD_IMPL_ATTRIBUTE_NATIVE)
619 fprintf (output, " // Disassembly of native methods is not supported\n");
621 dis_code (m, cols [MONO_METHOD_RVA]);
622 fprintf (output, " } // end of method %s::%s\n\n", klass_name, sig_str);
623 mono_metadata_free_method_signature (ms);
636 table_locator (const void *a, const void *b)
638 plocator_t *loc = (plocator_t *) a;
639 const char *bb = (const char *) b;
640 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
643 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
645 if (loc->idx == col) {
646 loc->result = table_index;
656 dis_property_methods (MonoImage *m, guint32 prop)
659 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
660 guint32 cols [MONO_METHOD_SEMA_SIZE];
662 const char *type[] = {NULL, ".set", ".get", NULL, ".other"};
664 start = mono_metadata_methods_from_property (m, prop, &end);
665 while (start < end) {
666 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
667 sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD], TRUE);
668 fprintf (output, "\t\t%s %s\n", type [cols [MONO_METHOD_SEMA_SEMANTICS]], sig);
675 dis_property_signature (MonoImage *m, guint32 prop_idx)
677 MonoTableInfo *propt = &m->tables [MONO_TABLE_PROPERTY];
680 guint32 cols [MONO_PROPERTY_SIZE];
686 GString *res = g_string_new ("");
688 mono_metadata_decode_row (propt, prop_idx, cols, MONO_PROPERTY_SIZE);
689 name = mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]);
690 prop_flags = cols [MONO_PROPERTY_FLAGS];
691 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
692 mono_metadata_decode_blob_size (ptr, &ptr);
693 /* ECMA claims 0x08 ... */
694 if (*ptr != 0x28 && *ptr != 0x08)
695 g_warning("incorrect signature in propert blob: 0x%x", *ptr);
697 pcount = mono_metadata_decode_value (ptr, &ptr);
698 type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
699 blurb = dis_stringify_type (m, type);
700 if (prop_flags & 0x0200)
701 g_string_append (res, "specialname ");
702 if (prop_flags & 0x0400)
703 g_string_append (res, "rtspecialname ");
704 g_string_sprintfa (res, "%s %s (", blurb, name);
706 mono_metadata_free_type (type);
707 for (i = 0; i < pcount; i++) {
709 g_string_append (res, ", ");
710 param = mono_metadata_parse_param (m, ptr, &ptr);
711 blurb = dis_stringify_param (m, param);
712 g_string_append (res, blurb);
713 mono_metadata_free_type (param);
716 g_string_append_c (res, ')');
718 g_string_free (res, FALSE);
724 dis_property_list (MonoImage *m, guint32 typedef_row)
727 start = mono_metadata_properties_from_typedef (m, typedef_row, &end);
729 for (i = start; i < end; ++i) {
730 char *sig = dis_property_signature (m, i);
731 fprintf (output, "\t.property %s\n\t{\n", sig);
732 dump_cattrs (m, MONO_TOKEN_PROPERTY | (i + 1), "\t\t");
733 dis_property_methods (m, i);
734 fprintf (output, "\t}\n");
740 dis_event_signature (MonoImage *m, guint32 event_idx)
742 MonoTableInfo *et = &m->tables [MONO_TABLE_EVENT];
743 char *type, *result, *esname;
744 guint32 cols [MONO_EVENT_SIZE];
746 GString *res = g_string_new ("");
748 mono_metadata_decode_row (et, event_idx, cols, MONO_EVENT_SIZE);
749 esname = get_escaped_name (mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]));
750 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE]);
751 event_flags = cols [MONO_EVENT_FLAGS];
753 if (event_flags & 0x0200)
754 g_string_append (res, "specialname ");
755 if (event_flags & 0x0400)
756 g_string_append (res, "rtspecialname ");
757 g_string_sprintfa (res, "%s %s", type, esname);
762 g_string_free (res, FALSE);
767 dis_event_methods (MonoImage *m, guint32 event)
770 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
771 guint32 cols [MONO_METHOD_SEMA_SIZE];
773 const char *type = "";
775 start = mono_metadata_methods_from_event (m, event, &end);
776 while (start < end) {
777 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
778 sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD], TRUE);
779 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
780 case METHOD_SEMANTIC_OTHER:
781 type = ".other"; break;
782 case METHOD_SEMANTIC_ADD_ON:
783 type = ".addon"; break;
784 case METHOD_SEMANTIC_REMOVE_ON:
785 type = ".removeon"; break;
786 case METHOD_SEMANTIC_FIRE:
787 type = ".fire"; break;
791 fprintf (output, "\t\t%s %s\n", type, sig);
798 dis_event_list (MonoImage *m, guint32 typedef_row)
801 start = mono_metadata_events_from_typedef (m, typedef_row, &end);
803 for (i = start; i < end; ++i) {
804 char *sig = dis_event_signature (m, i);
805 fprintf (output, "\t.event %s\n\t{\n", sig);
806 dump_cattrs (m, MONO_TOKEN_EVENT | (i + 1), "\t\t");
807 dis_event_methods (m, i);
808 fprintf (output, "\t}\n");
814 dis_interfaces (MonoImage *m, guint32 typedef_row)
818 gboolean first_interface = 1;
819 guint32 cols [MONO_INTERFACEIMPL_SIZE];
821 MonoTableInfo *table = &m->tables [MONO_TABLE_INTERFACEIMPL];
827 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
828 loc.idx = typedef_row;
830 if (!bsearch (&loc, table->base, table->rows, table->row_size, table_locator))
835 * We may end up in the middle of the rows...
838 if (loc.idx == mono_metadata_decode_row_col (table, start - 1, MONO_INTERFACEIMPL_CLASS))
843 while (start < table->rows) {
844 mono_metadata_decode_row (table, start, cols, MONO_INTERFACEIMPL_SIZE);
845 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
847 intf = get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE]);
848 if (first_interface) {
849 fprintf (output, " \timplements %s", intf);
852 fprintf (output, ", %s", intf);
860 * dis_generic_param_and_constraints:
861 * @m: metadata context
862 * @table_type: Type of table (0 for typedef, 1 for methoddef)
865 * Dissasembles the generic parameters for this type or method, also
866 * returns an allocated GString containing the generic constraints NULL
867 * if their are no generic constraints.
870 dis_generic_param_and_constraints (MonoImage *m, int table_type, guint32 typedef_row)
872 MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAM];
873 MonoTableInfo *ct = &m->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
874 GString* cnst_block = NULL;
875 guint32 cols [MONO_GENERICPARAM_SIZE];
876 guint32 ccols [MONO_GENPARCONSTRAINT_SIZE];
877 int i, own_tok, table, idx, found_count, cnst_start, cnst_ind;
879 g_assert (table_type != MONO_TYPEORMETHOD_TYPE || table_type != MONO_TYPEORMETHOD_METHOD);
881 found_count = cnst_start = 0;
882 for (i = 1; i <= t->rows; i++) {
883 mono_metadata_decode_row (t, i-1, cols, MONO_GENERICPARAM_SIZE);
884 own_tok = cols [MONO_GENERICPARAM_OWNER];
885 table = own_tok & MONO_TYPEORMETHOD_MASK;
886 idx = own_tok >> MONO_TYPEORMETHOD_BITS;
888 if (table != table_type || idx != typedef_row)
891 if (found_count == 0)
892 fprintf (output, "<%s", mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
894 fprintf (output, ", %s", mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
896 for (cnst_ind = cnst_start; cnst_ind < ct->rows; cnst_ind++) {
898 mono_metadata_decode_row (ct, cnst_ind, ccols, MONO_GENPARCONSTRAINT_SIZE);
899 if (ccols [MONO_GENPARCONSTRAINT_GENERICPAR] != i)
901 if (cnst_block == NULL)
902 cnst_block = g_string_new ("");
903 sig = get_typedef_or_ref (m, ccols [MONO_GENPARCONSTRAINT_CONSTRAINT]);
904 g_string_append_printf (cnst_block, " .constraint !%d is %s\n",
905 cols [MONO_GENERICPARAM_NUMBER], sig);
907 cnst_start = cnst_ind;
914 fprintf (output, ">");
921 * @m: metadata context
922 * @n: index of type to disassemble
924 * Disassembles the type whose index in the TypeDef table is @n.
927 dis_type (MonoImage *m, int n)
929 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
930 GString *cnst_block = NULL;
931 guint32 cols [MONO_TYPEDEF_SIZE];
932 guint32 cols_next [MONO_TYPEDEF_SIZE];
933 const char *name, *nspace;
935 guint32 packing_size, class_size;
936 gboolean next_is_valid, last;
939 mono_metadata_decode_row (t, n, cols, MONO_TYPEDEF_SIZE);
941 if (t->rows > n + 1) {
942 mono_metadata_decode_row (t, n + 1, cols_next, MONO_TYPEDEF_SIZE);
947 name = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]);
948 nspace = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
950 fprintf (output, ".namespace %s\n{\n", nspace);
952 esname = get_escaped_name (name);
953 if ((cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_CLASS_SEMANTIC_MASK) == TYPE_ATTRIBUTE_CLASS){
954 fprintf (output, " .class %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
956 cnst_block = dis_generic_param_and_constraints (m, MONO_TYPEORMETHOD_TYPE, n+1);
957 fprintf (output, "\n");
958 if (cols [MONO_TYPEDEF_EXTENDS]) {
959 char *base = get_typedef_or_ref (m, cols [MONO_TYPEDEF_EXTENDS]);
960 fprintf (output, " \textends %s\n", base);
964 fprintf (output, " .class interface %s%s\n", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), esname);
966 dis_interfaces (m, n + 1);
967 fprintf (output, " {\n");
969 fprintf (output, "%s", cnst_block->str);
970 g_string_free (cnst_block, TRUE);
972 dump_cattrs (m, MONO_TOKEN_TYPE_DEF | (n + 1), " ");
974 if (mono_metadata_packing_from_typedef (m, n + 1, &packing_size, &class_size)) {
975 fprintf (output, " .pack %d\n", packing_size);
976 fprintf (output, " .size %d\n", class_size);
979 * The value in the table is always valid, we know we have fields
980 * if the value stored is different than the next record.
984 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
986 last = m->tables [MONO_TABLE_FIELD].rows;
988 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
989 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last);
990 fprintf (output, "\n");
993 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
995 last = m->tables [MONO_TABLE_METHOD].rows;
997 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
998 dis_method_list (name, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last);
1000 dis_property_list (m, n);
1001 dis_event_list (m, n);
1003 t = &m->tables [MONO_TABLE_NESTEDCLASS];
1004 nested = mono_metadata_nesting_typedef (m, n + 1, 1);
1006 dis_type (m, mono_metadata_decode_row_col (t, nested - 1, MONO_NESTED_CLASS_NESTED) - 1);
1007 nested = mono_metadata_nesting_typedef (m, n + 1, nested + 1);
1010 fprintf (output, " } // end of class %s%s%s\n", nspace, *nspace? ".": "", name);
1012 fprintf (output, "}\n");
1013 fprintf (output, "\n");
1019 * @m: metadata context
1021 * disassembles all the global fields and methods
1024 dis_globals (MonoImage *m)
1026 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1027 guint32 cols [MONO_TYPEDEF_SIZE];
1028 guint32 cols_next [MONO_TYPEDEF_SIZE];
1029 gboolean next_is_valid, last;
1032 name = g_strdup ("<Module>");
1034 mono_metadata_decode_row (t, 0, cols, MONO_TYPEDEF_SIZE);
1037 mono_metadata_decode_row (t, 1, cols_next, MONO_TYPEDEF_SIZE);
1043 * The value in the table is always valid, we know we have fields
1044 * if the value stored is different than the next record.
1048 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
1050 last = m->tables [MONO_TABLE_FIELD].rows;
1052 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
1053 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last);
1054 fprintf (output, "\n");
1057 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
1059 last = m->tables [MONO_TABLE_METHOD].rows;
1061 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
1062 dis_method_list (name, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last);
1068 * @m: metadata context
1070 * disassembles all types in the @m context
1073 dis_types (MonoImage *m)
1075 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
1081 for (i = 1; i < t->rows; i++) {
1082 flags = mono_metadata_decode_row_col (t, i, MONO_TYPEDEF_FLAGS);
1083 flags &= TYPE_ATTRIBUTE_VISIBILITY_MASK;
1084 if (flags == TYPE_ATTRIBUTE_PUBLIC || flags == TYPE_ATTRIBUTE_NOT_PUBLIC)
1091 * @m: metadata context
1093 * disassembles all data blobs references in the FieldRVA table in the @m context
1096 dis_data (MonoImage *m)
1098 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
1099 MonoTableInfo *ft = &m->tables [MONO_TABLE_FIELD];
1101 const char *rva, *sig;
1102 guint32 align, size;
1103 guint32 cols [MONO_FIELD_RVA_SIZE];
1106 for (i = 0; i < t->rows; i++) {
1107 mono_metadata_decode_row (t, i, cols, MONO_FIELD_RVA_SIZE);
1108 rva = mono_cli_rva_map (m->image_info, cols [MONO_FIELD_RVA_RVA]);
1109 sig = mono_metadata_blob_heap (m, mono_metadata_decode_row_col (ft, cols [MONO_FIELD_RVA_FIELD] -1, MONO_FIELD_SIGNATURE));
1110 mono_metadata_decode_value (sig, &sig);
1111 /* FIELD signature == 0x06 */
1112 g_assert (*sig == 0x06);
1113 type = mono_metadata_parse_field_type (m, 0, sig + 1, &sig);
1114 mono_class_init (mono_class_from_mono_type (type));
1115 size = mono_type_size (type, &align);
1116 fprintf (output, ".data D_%08x = bytearray (", cols [MONO_FIELD_RVA_RVA]);
1117 for (b = 0; b < size; ++b) {
1119 fprintf (output, "\n\t");
1120 fprintf (output, " %02X", rva [b] & 0xff);
1122 fprintf (output, ") // size: %d\n", size);
1129 void (*dumper) (MonoImage *m);
1131 { "--assembly", MONO_TABLE_ASSEMBLY, dump_table_assembly },
1132 { "--assemblyref", MONO_TABLE_ASSEMBLYREF, dump_table_assemblyref },
1133 { "--classlayout", MONO_TABLE_CLASSLAYOUT, dump_table_class_layout },
1134 { "--constant", MONO_TABLE_CONSTANT, dump_table_constant },
1135 { "--customattr", MONO_TABLE_CUSTOMATTRIBUTE, dump_table_customattr },
1136 { "--declsec", MONO_TABLE_DECLSECURITY, dump_table_declsec },
1137 { "--event", MONO_TABLE_EVENT, dump_table_event },
1138 { "--exported", MONO_TABLE_EXPORTEDTYPE, dump_table_exported },
1139 { "--fields", MONO_TABLE_FIELD, dump_table_field },
1140 { "--file", MONO_TABLE_FILE, dump_table_file },
1141 { "--genericpar", MONO_TABLE_GENERICPARAM, dump_table_genericpar },
1142 { "--interface", MONO_TABLE_INTERFACEIMPL, dump_table_interfaceimpl },
1143 { "--manifest", MONO_TABLE_MANIFESTRESOURCE, dump_table_manifest },
1144 { "--marshal", MONO_TABLE_FIELDMARSHAL, dump_table_field_marshal },
1145 { "--memberref", MONO_TABLE_MEMBERREF, dump_table_memberref },
1146 { "--method", MONO_TABLE_METHOD, dump_table_method },
1147 { "--methodimpl", MONO_TABLE_METHODIMPL, dump_table_methodimpl },
1148 { "--methodsem", MONO_TABLE_METHODSEMANTICS, dump_table_methodsem },
1149 { "--methodspec", MONO_TABLE_METHODSPEC, dump_table_methodspec },
1150 { "--moduleref", MONO_TABLE_MODULEREF, dump_table_moduleref },
1151 { "--module", MONO_TABLE_MODULE, dump_table_module },
1152 { "--nested", MONO_TABLE_NESTEDCLASS, dump_table_nestedclass },
1153 { "--param", MONO_TABLE_PARAM, dump_table_param },
1154 { "--parconst", MONO_TABLE_GENERICPARAMCONSTRAINT, dump_table_parconstraint },
1155 { "--property", MONO_TABLE_PROPERTY, dump_table_property },
1156 { "--propertymap", MONO_TABLE_PROPERTYMAP, dump_table_property_map },
1157 { "--typedef", MONO_TABLE_TYPEDEF, dump_table_typedef },
1158 { "--typeref", MONO_TABLE_TYPEREF, dump_table_typeref },
1159 { "--typespec", MONO_TABLE_TYPESPEC, dump_table_typespec },
1160 { "--implmap", MONO_TABLE_IMPLMAP, dump_table_implmap },
1161 { "--blob", NULL, dump_stream_blob },
1167 * @file: file containing CIL code.
1169 * Disassembles the @file file.
1172 disassemble_file (const char *file)
1175 MonoImageOpenStatus status;
1178 ass = mono_assembly_open (file, &status);
1180 fprintf (stderr, "Error while trying to process %s\n", file);
1186 if (dump_table != -1){
1187 (*table_list [dump_table].dumper) (img);
1189 dump_header_data (img);
1191 dis_directive_assemblyref (img);
1192 dis_directive_assembly (img);
1193 dis_directive_file (img);
1194 dis_directive_module (img);
1195 dis_directive_moduleref (img);
1200 mono_image_close (img);
1206 GString *args = g_string_new ("[--output=filename] [--help] [--mscorlib]\n");
1209 for (i = 0; table_list [i].name != NULL; i++){
1210 g_string_append (args, "[");
1211 g_string_append (args, table_list [i].name);
1212 g_string_append (args, "] ");
1213 if (((i-2) % 5) == 0)
1214 g_string_append_c (args, '\n');
1217 "Usage is: monodis %s file ..\n", args->str);
1222 main (int argc, char *argv [])
1224 GList *input_files = NULL, *l;
1229 for (i = 1; i < argc; i++){
1230 if (argv [i][0] == '-'){
1231 if (argv [i][1] == 'h')
1233 else if (argv [i][1] == 'd')
1234 dump_header_data_p = TRUE;
1235 else if (strcmp (argv [i], "--mscorlib") == 0) {
1236 substitute_with_mscorlib_p = TRUE;
1238 } else if (strcmp (argv [i], "--show-method-tokens") == 0) {
1239 show_method_tokens = TRUE;
1241 } else if (strcmp (argv [i], "--show-tokens") == 0) {
1244 } else if (strncmp (argv [i], "--output=", 9) == 0) {
1245 output = fopen (argv [i]+9, "w");
1246 if (output == NULL) {
1247 fprintf (stderr, "Can't open output file `%s': %s\n",
1248 argv [i]+9, strerror (errno));
1252 } else if (strcmp (argv [i], "--help") == 0)
1254 for (j = 0; table_list [j].name != NULL; j++) {
1255 if (strcmp (argv [i], table_list [j].name) == 0)
1261 input_files = g_list_append (input_files, argv [i]);
1264 if (input_files == NULL)
1267 mono_init (argv [0]);
1269 for (l = input_files; l; l = l->next)
1270 disassemble_file (l->data);