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]
120 static map_t visibility_map [] = {
121 { TYPE_ATTRIBUTE_NOT_PUBLIC, "private " },
122 { TYPE_ATTRIBUTE_PUBLIC, "public " },
123 { TYPE_ATTRIBUTE_NESTED_PUBLIC, "nested-public " },
124 { TYPE_ATTRIBUTE_NESTED_PRIVATE, "nested-private " },
125 { TYPE_ATTRIBUTE_NESTED_FAMILY, "family " },
126 { TYPE_ATTRIBUTE_NESTED_ASSEMBLY, "nested-assembly" },
127 { TYPE_ATTRIBUTE_NESTED_FAM_AND_ASSEM, "nested-fam-and-assembly" },
128 { TYPE_ATTRIBUTE_NESTED_FAM_OR_ASSEM, "nested-fam-or-assembly" },
132 static map_t layout_map [] = {
133 { TYPE_ATTRIBUTE_AUTO_LAYOUT, "auto " },
134 { TYPE_ATTRIBUTE_SEQUENTIAL_LAYOUT, "sequential " },
135 { TYPE_ATTRIBUTE_EXPLICIT_LAYOUT, "explicit " },
139 static map_t format_map [] = {
140 { TYPE_ATTRIBUTE_ANSI_CLASS, "ansi " },
141 { TYPE_ATTRIBUTE_UNICODE_CLASS, "unicode " },
142 { TYPE_ATTRIBUTE_AUTO_CLASS, "auto " },
147 typedef_flags (guint32 flags)
149 static char buffer [1024];
150 int visibility = flags & TYPE_ATTRIBUTE_VISIBILITY_MASK;
151 int layout = flags & TYPE_ATTRIBUTE_LAYOUT_MASK;
152 int format = flags & TYPE_ATTRIBUTE_STRING_FORMAT_MASK;
156 strcat (buffer, map (visibility, visibility_map));
157 strcat (buffer, map (layout, layout_map));
158 strcat (buffer, map (format, format_map));
160 if (flags & TYPE_ATTRIBUTE_ABSTRACT)
161 strcat (buffer, "abstract ");
162 if (flags & TYPE_ATTRIBUTE_SEALED)
163 strcat (buffer, "sealed ");
164 if (flags & TYPE_ATTRIBUTE_SPECIAL_NAME)
165 strcat (buffer, "special-name ");
166 if (flags & TYPE_ATTRIBUTE_IMPORT)
167 strcat (buffer, "import ");
168 if (flags & TYPE_ATTRIBUTE_SERIALIZABLE)
169 strcat (buffer, "serializable ");
170 if (flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)
171 strcat (buffer, "beforefieldinit ");
178 * @m: metadata context
179 * @start: starting index into the Field Table.
180 * @end: ending index into Field table.
182 * This routine displays all the decoded fields from @start to @end
185 dis_field_list (MonoImage *m, guint32 start, guint32 end)
187 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
188 guint32 cols [MONO_FIELD_SIZE];
193 if (end > t->rows + 1) {
194 g_warning ("ERROR index out of range in fields");
198 for (i = start; i < end; i++){
199 char *sig, *flags, *attrs = NULL;
200 guint32 field_offset = -1;
202 mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
203 sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
204 flags = field_flags (cols [MONO_FIELD_FLAGS]);
206 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
207 mono_metadata_field_info (m, i, NULL, &rva, NULL);
208 g_snprintf (rva_desc, sizeof (rva_desc), " at D_%08x", rva);
213 mono_metadata_field_info (m, i, &field_offset, NULL, NULL);
214 if (field_offset != -1)
215 attrs = g_strdup_printf ("[%d]", field_offset);
216 if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_LITERAL){
218 guint32 const_cols [MONO_CONSTANT_SIZE];
221 if ((crow = mono_metadata_get_constant_index (m, MONO_TOKEN_FIELD_DEF | (i+1)))) {
222 mono_metadata_decode_row (&m->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
223 lit = get_constant (m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
225 lit = g_strdup ("not found");
228 fprintf (output, " .field %s %s %s = ",
230 mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
231 fprintf (output, "%s\n", lit);
234 fprintf (output, " .field %s %s %s %s%s\n",
235 attrs? attrs: "", flags, sig,
236 mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]), rva_desc);
240 dump_cattrs (m, MONO_TOKEN_FIELD_DEF | (i + 1), " ");
244 static map_t method_access_map [] = {
245 { METHOD_ATTRIBUTE_COMPILER_CONTROLLED, "compilercontrolled " },
246 { METHOD_ATTRIBUTE_PRIVATE, "private " },
247 { METHOD_ATTRIBUTE_FAM_AND_ASSEM, "famandassem " },
248 { METHOD_ATTRIBUTE_ASSEM, "assembly " },
249 { METHOD_ATTRIBUTE_FAMILY, "family " },
250 { METHOD_ATTRIBUTE_FAM_OR_ASSEM, "famorassem " },
251 { METHOD_ATTRIBUTE_PUBLIC, "public " },
255 static map_t method_flags_map [] = {
256 { METHOD_ATTRIBUTE_STATIC, "static " },
257 { METHOD_ATTRIBUTE_FINAL, "final " },
258 { METHOD_ATTRIBUTE_VIRTUAL, "virtual " },
259 { METHOD_ATTRIBUTE_HIDE_BY_SIG, "hidebysig " },
260 { METHOD_ATTRIBUTE_VTABLE_LAYOUT_MASK, "newslot " },
261 { METHOD_ATTRIBUTE_ABSTRACT, "abstract " },
262 { METHOD_ATTRIBUTE_SPECIAL_NAME, "specialname " },
263 { METHOD_ATTRIBUTE_RT_SPECIAL_NAME, "rtspecialname " },
264 { METHOD_ATTRIBUTE_UNMANAGED_EXPORT, "export " },
265 { METHOD_ATTRIBUTE_HAS_SECURITY, "hassecurity" },
266 { METHOD_ATTRIBUTE_REQUIRE_SEC_OBJECT, "requiresecobj" },
267 { METHOD_ATTRIBUTE_PINVOKE_IMPL, "pinvokeimpl " },
274 * Returns a stringified version of the Method's flags
277 method_flags (guint32 f)
279 GString *str = g_string_new ("");
280 int access = f & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK;
283 g_string_append (str, map (access, method_access_map));
284 g_string_append (str, flags (f, method_flags_map));
287 g_string_free (str, FALSE);
292 static map_t pinvoke_flags_map [] = {
293 { PINVOKE_ATTRIBUTE_NO_MANGLE , "nomangle " },
294 { PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR, "lasterr " },
298 static map_t pinvoke_call_conv_map [] = {
299 { PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI, "winapi " },
300 { PINVOKE_ATTRIBUTE_CALL_CONV_CDECL, "cdecl " },
301 { PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL, "stdcall " },
302 { PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL, "thiscall " },
303 { PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL, "fastcall " },
307 static map_t pinvoke_char_set_map [] = {
308 { PINVOKE_ATTRIBUTE_CHAR_SET_NOT_SPEC, "" },
309 { PINVOKE_ATTRIBUTE_CHAR_SET_ANSI, "ansi " },
310 { PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE , "unicode " },
311 { PINVOKE_ATTRIBUTE_CHAR_SET_AUTO, "autochar " },
318 * Returns a stringified version of the Method's pinvoke flags
321 pinvoke_flags (guint32 f)
323 GString *str = g_string_new ("");
324 int cset = f & PINVOKE_ATTRIBUTE_CHAR_SET_MASK;
325 int cconv = f & PINVOKE_ATTRIBUTE_CALL_CONV_MASK;
328 g_string_append (str, map (cset, pinvoke_char_set_map));
329 g_string_append (str, map (cconv, pinvoke_call_conv_map));
330 g_string_append (str, flags (f, pinvoke_flags_map));
332 s = g_strdup(str->str);
333 g_string_free (str, FALSE);
338 static map_t method_impl_map [] = {
339 { METHOD_IMPL_ATTRIBUTE_IL, "cil " },
340 { METHOD_IMPL_ATTRIBUTE_NATIVE, "native " },
341 { METHOD_IMPL_ATTRIBUTE_OPTIL, "optil " },
342 { METHOD_IMPL_ATTRIBUTE_RUNTIME, "runtime " },
346 static map_t managed_type_map [] = {
347 { METHOD_IMPL_ATTRIBUTE_UNMANAGED, "unmanaged " },
348 { METHOD_IMPL_ATTRIBUTE_MANAGED, "managed " },
352 static map_t managed_impl_flags [] = {
353 { METHOD_IMPL_ATTRIBUTE_FORWARD_REF, "fwdref " },
354 { METHOD_IMPL_ATTRIBUTE_PRESERVE_SIG, "preservesig " },
355 { METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL, "internalcall " },
356 { METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED, "synchronized " },
357 { METHOD_IMPL_ATTRIBUTE_NOINLINING, "noinline " },
362 method_impl_flags (guint32 f)
364 GString *str = g_string_new ("");
366 int code_type = f & METHOD_IMPL_ATTRIBUTE_CODE_TYPE_MASK;
367 int managed_type = f & METHOD_IMPL_ATTRIBUTE_MANAGED_MASK;
369 g_string_append (str, map (code_type, method_impl_map));
370 g_string_append (str, map (managed_type, managed_type_map));
371 g_string_append (str, flags (f, managed_impl_flags));
374 g_string_free (str, FALSE);
379 dis_locals (MonoImage *m, MonoMethodHeader *mh)
383 fprintf(output, "\t.locals %s(\n", mh->init_locals ? "init " : "");
384 for (i=0; i < mh->num_locals; ++i) {
387 fprintf(output, ",\n");
388 /* print also byref and pinned attributes */
389 desc = dis_stringify_type (m, mh->locals[i]);
390 fprintf(output, "\t\t%s\tV_%d", desc, i);
393 fprintf(output, ")\n");
397 dis_code (MonoImage *m, guint32 rva)
399 MonoMethodHeader *mh;
400 MonoCLIImageInfo *ii = m->image_info;
401 const char *ptr = mono_cli_rva_map (ii, rva);
408 mh = mono_metadata_parse_mh (m, ptr);
409 if ((entry_point = mono_image_get_entry_point (m))){
410 loc = mono_metadata_locate_token (m, entry_point);
411 if (rva == read32 (loc))
412 fprintf (output, "\t.entrypoint\n");
415 fprintf (output, "\t// Code size %d (0x%x)\n", mh->code_size, mh->code_size);
416 fprintf (output, "\t.maxstack %d\n", mh->max_stack);
419 dissasemble_cil (m, mh);
422 hex_dump (mh->code, 0, mh->code_size);
423 printf ("\nAfter the code\n");
424 hex_dump (mh->code + mh->code_size, 0, 64);
426 mono_metadata_free_mh (mh);
430 pinvoke_info (MonoImage *m, guint32 mindex)
432 MonoTableInfo *im = &m->tables [MONO_TABLE_IMPLMAP];
433 MonoTableInfo *mr = &m->tables [MONO_TABLE_MODULEREF];
434 guint32 im_cols [MONO_IMPLMAP_SIZE];
435 guint32 mr_cols [MONO_MODULEREF_SIZE];
436 const char *import, *scope;
440 for (i = 0; i < im->rows; i++) {
442 mono_metadata_decode_row (im, i, im_cols, MONO_IMPLMAP_SIZE);
444 if ((im_cols [MONO_IMPLMAP_MEMBER] >> 1) == mindex + 1) {
446 flags = pinvoke_flags (im_cols [MONO_IMPLMAP_FLAGS]);
448 import = mono_metadata_string_heap (m, im_cols [MONO_IMPLMAP_NAME]);
450 mono_metadata_decode_row (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1,
451 mr_cols, MONO_MODULEREF_SIZE);
453 scope = mono_metadata_string_heap (m, mr_cols [MONO_MODULEREF_NAME]);
455 return g_strdup_printf ("(%s as %s %s)", scope, import,
465 cattrs_for_method (MonoImage *m, guint32 midx, MonoMethodSignature *sig) {
466 MonoTableInfo *methodt;
467 MonoTableInfo *paramt;
468 guint param_index, lastp, i, pid;
470 methodt = &m->tables [MONO_TABLE_METHOD];
471 paramt = &m->tables [MONO_TABLE_PARAM];
472 param_index = mono_metadata_decode_row_col (methodt, midx, MONO_METHOD_PARAMLIST);
473 if (midx + 1 < methodt->rows)
474 lastp = mono_metadata_decode_row_col (methodt, midx + 1, MONO_METHOD_PARAMLIST);
476 lastp = paramt->rows + 1;
477 for (i = param_index; i < lastp; ++i) {
478 pid = mono_metadata_decode_row_col (paramt, i - 1, MONO_PARAM_SEQUENCE);
479 fprintf (output, "\t.param [%d]\n", pid);
480 dump_cattrs (m, MONO_TOKEN_PARAM_DEF | i, "\t");
486 * @m: metadata context
487 * @start: starting index into the Method Table.
488 * @end: ending index into Method table.
490 * This routine displays the methods in the Method Table from @start to @end
493 dis_method_list (const char *klass_name, MonoImage *m, guint32 start, guint32 end)
495 MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
496 guint32 cols [MONO_METHOD_SIZE];
500 fprintf (output, "ERROR index out of range in methods");
505 for (i = start; i < end; i++){
506 MonoMethodSignature *ms;
507 char *flags, *impl_flags;
511 mono_metadata_decode_row (t, i, cols, MONO_METHOD_SIZE);
513 flags = method_flags (cols [MONO_METHOD_FLAGS]);
514 impl_flags = method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
516 sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
517 mono_metadata_decode_blob_size (sig, &sig);
518 ms = mono_metadata_parse_method_signature (m, i + 1, sig, &sig);
519 sig_str = dis_stringify_method_signature (m, ms, i + 1, FALSE);
521 fprintf (output, " // method line %d\n", i + 1);
522 fprintf (output, " .method %s", flags);
524 if (cols [MONO_METHOD_FLAGS] & METHOD_ATTRIBUTE_PINVOKE_IMPL)
525 fprintf (output, "%s", pinvoke_info (m, i));
527 fprintf (output, "\n %s", sig_str);
528 fprintf (output, " %s\n", impl_flags);
532 fprintf (output, " {\n");
533 dump_cattrs (m, MONO_TOKEN_METHOD_DEF | (i + 1), " ");
534 cattrs_for_method (m, i, ms);
535 /* FIXME: need to sump also param custom attributes */
536 fprintf (output, " // Method begins at RVA 0x%x\n", cols [MONO_METHOD_RVA]);
537 dis_code (m, cols [MONO_METHOD_RVA]);
538 fprintf (output, " } // end of method %s::%s\n\n", klass_name, sig_str);
539 mono_metadata_free_method_signature (ms);
552 table_locator (const void *a, const void *b)
554 plocator_t *loc = (plocator_t *) a;
555 const char *bb = (const char *) b;
556 guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
559 col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
561 if (loc->idx == col) {
562 loc->result = table_index;
572 dis_property_methods (MonoImage *m, guint32 prop)
575 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
576 guint32 cols [MONO_METHOD_SEMA_SIZE];
578 const char *type[] = {NULL, ".set", ".get", NULL, ".other"};
580 start = mono_metadata_methods_from_property (m, prop, &end);
581 while (start < end) {
582 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
583 sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD], TRUE);
584 fprintf (output, "\t\t%s %s\n", type [cols [MONO_METHOD_SEMA_SEMANTICS]], sig);
591 dis_property_signature (MonoImage *m, guint32 prop_idx)
593 MonoTableInfo *propt = &m->tables [MONO_TABLE_PROPERTY];
596 guint32 cols [MONO_PROPERTY_SIZE];
602 GString *res = g_string_new ("");
604 mono_metadata_decode_row (propt, prop_idx, cols, MONO_PROPERTY_SIZE);
605 name = mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]);
606 prop_flags = cols [MONO_PROPERTY_FLAGS];
607 ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
608 mono_metadata_decode_blob_size (ptr, &ptr);
609 /* ECMA claims 0x08 ... */
610 if (*ptr != 0x28 && *ptr != 0x08)
611 g_warning("incorrect signature in propert blob: 0x%x", *ptr);
613 pcount = mono_metadata_decode_value (ptr, &ptr);
614 type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
615 blurb = dis_stringify_type (m, type);
616 if (prop_flags & 0x0200)
617 g_string_append (res, "special ");
618 if (prop_flags & 0x0400)
619 g_string_append (res, "runtime ");
620 if (prop_flags & 0x1000)
621 g_string_append (res, "hasdefault ");
622 g_string_sprintfa (res, "%s %s (", blurb, name);
624 mono_metadata_free_type (type);
625 for (i = 0; i < pcount; i++) {
627 g_string_append (res, ", ");
628 param = mono_metadata_parse_param (m, ptr, &ptr);
629 blurb = dis_stringify_param (m, param);
630 g_string_append (res, blurb);
631 mono_metadata_free_type (param);
634 g_string_append_c (res, ')');
636 g_string_free (res, FALSE);
642 dis_property_list (MonoImage *m, guint32 typedef_row)
645 start = mono_metadata_properties_from_typedef (m, typedef_row, &end);
647 for (i = start; i < end; ++i) {
648 char *sig = dis_property_signature (m, i);
649 fprintf (output, "\t.property %s\n\t{\n", sig);
650 dump_cattrs (m, MONO_TOKEN_PROPERTY | (i + 1), "\t\t");
651 dis_property_methods (m, i);
652 fprintf (output, "\t}\n");
658 dis_event_signature (MonoImage *m, guint32 event_idx)
660 MonoTableInfo *et = &m->tables [MONO_TABLE_EVENT];
663 guint32 cols [MONO_EVENT_SIZE];
665 mono_metadata_decode_row (et, event_idx, cols, MONO_EVENT_SIZE);
666 name = mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]);
667 type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE]);
669 res = g_strdup_printf ("%s %s", type, name);
675 dis_event_methods (MonoImage *m, guint32 event)
678 MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
679 guint32 cols [MONO_METHOD_SEMA_SIZE];
683 start = mono_metadata_methods_from_event (m, event, &end);
684 while (start < end) {
685 mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
686 sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD], FALSE);
687 switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
688 case METHOD_SEMANTIC_OTHER:
689 type = ".other"; break;
690 case METHOD_SEMANTIC_ADD_ON:
691 type = ".addon"; break;
692 case METHOD_SEMANTIC_REMOVE_ON:
693 type = ".removeon"; break;
694 case METHOD_SEMANTIC_FIRE:
695 type = ".fire"; break;
699 fprintf (output, "\t\t%s %s\n", type, sig);
706 dis_event_list (MonoImage *m, guint32 typedef_row)
709 start = mono_metadata_events_from_typedef (m, typedef_row, &end);
711 for (i = start; i < end; ++i) {
712 char *sig = dis_event_signature (m, i);
713 fprintf (output, "\t.event %s\n\t{\n", sig);
714 dump_cattrs (m, MONO_TOKEN_EVENT | (i + 1), "\t\t");
715 dis_event_methods (m, i);
716 fprintf (output, "\t}\n");
722 dis_interfaces (MonoImage *m, guint32 typedef_row)
726 gboolean first_interface = 1;
727 guint32 cols [MONO_INTERFACEIMPL_SIZE];
729 MonoTableInfo *table = &m->tables [MONO_TABLE_INTERFACEIMPL];
735 loc.col_idx = MONO_INTERFACEIMPL_CLASS;
736 loc.idx = typedef_row;
738 if (!bsearch (&loc, table->base, table->rows, table->row_size, table_locator))
743 * We may end up in the middle of the rows...
746 if (loc.idx == mono_metadata_decode_row_col (table, start - 1, MONO_INTERFACEIMPL_CLASS))
751 while (start < table->rows) {
752 mono_metadata_decode_row (table, start, cols, MONO_INTERFACEIMPL_SIZE);
753 if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
755 intf = get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE]);
756 if (first_interface) {
757 fprintf (output, " \timplements %s", intf);
760 fprintf (output, ", %s", intf);
768 dis_genericparam (MonoImage *m, guint32 typedef_row)
770 MonoTableInfo *t = &m->tables [MONO_TABLE_GENERICPARAM];
771 guint32 cols [MONO_GENERICPARAM_SIZE];
772 int i, own_tok, table, idx, found_count;
775 for (i = 1; i <= t->rows; i++) {
776 mono_metadata_decode_row (t, i - 1, cols, MONO_GENERICPARAM_SIZE);
777 own_tok = cols [MONO_GENERICPARAM_OWNER];
778 table = own_tok & 0x03;
781 if (table != 0 || idx != typedef_row)
784 if (found_count == 0)
785 fprintf (output, "<%s", mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
787 fprintf (output, ", %s", mono_metadata_string_heap (m, cols [MONO_GENERICPARAM_NAME]));
793 fprintf (output, ">");
798 * @m: metadata context
799 * @n: index of type to disassemble
801 * Disassembles the type whose index in the TypeDef table is @n.
804 dis_type (MonoImage *m, int n)
806 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
807 guint32 cols [MONO_TYPEDEF_SIZE];
808 guint32 cols_next [MONO_TYPEDEF_SIZE];
809 const char *name, *nspace;
810 guint32 packing_size, class_size;
811 gboolean next_is_valid, last;
814 mono_metadata_decode_row (t, n, cols, MONO_TYPEDEF_SIZE);
816 if (t->rows > n + 1) {
817 mono_metadata_decode_row (t, n + 1, cols_next, MONO_TYPEDEF_SIZE);
822 nspace = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
824 fprintf (output, ".namespace %s\n{\n", nspace);
825 name = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]);
827 if ((cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_CLASS_SEMANTIC_MASK) == TYPE_ATTRIBUTE_CLASS){
828 fprintf (output, " .class %s%s", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), name);
829 dis_genericparam (m, n + 1);
830 fprintf (output, "\n");
831 if (cols [MONO_TYPEDEF_EXTENDS]) {
832 char *base = get_typedef_or_ref (m, cols [MONO_TYPEDEF_EXTENDS]);
833 fprintf (output, " \textends %s\n", base);
837 fprintf (output, " .class interface %s%s\n", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), name);
839 dis_interfaces (m, n + 1);
840 fprintf (output, " {\n");
841 dump_cattrs (m, MONO_TOKEN_TYPE_DEF | (n + 1), " ");
843 if (mono_metadata_packing_from_typedef (m, n + 1, &packing_size, &class_size)) {
844 fprintf (output, " .pack %d\n", packing_size);
845 fprintf (output, " .size %d\n", class_size);
848 * The value in the table is always valid, we know we have fields
849 * if the value stored is different than the next record.
853 last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
855 last = m->tables [MONO_TABLE_FIELD].rows;
857 if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
858 dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last);
859 fprintf (output, "\n");
862 last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
864 last = m->tables [MONO_TABLE_METHOD].rows;
866 if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
867 dis_method_list (name, m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last);
869 dis_property_list (m, n);
870 dis_event_list (m, n);
872 t = &m->tables [MONO_TABLE_NESTEDCLASS];
873 nested = mono_metadata_nesting_typedef (m, n + 1, 1);
875 dis_type (m, mono_metadata_decode_row_col (t, nested - 1, MONO_NESTED_CLASS_NESTED) - 1);
876 nested = mono_metadata_nesting_typedef (m, n + 1, nested + 1);
879 fprintf (output, " } // end of type %s%s%s\n", nspace, *nspace? ".": "", name);
881 fprintf (output, "}\n");
882 fprintf (output, "\n");
887 * @m: metadata context
889 * disassembles all types in the @m context
892 dis_types (MonoImage *m)
894 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
898 for (i = 1; i < t->rows; i++) {
899 flags = mono_metadata_decode_row_col (t, i, MONO_TYPEDEF_FLAGS);
900 flags &= TYPE_ATTRIBUTE_VISIBILITY_MASK;
901 if (flags == TYPE_ATTRIBUTE_PUBLIC || flags == TYPE_ATTRIBUTE_NOT_PUBLIC)
908 * @m: metadata context
910 * disassembles all data blobs references in the FieldRVA table in the @m context
913 dis_data (MonoImage *m)
915 MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
916 MonoTableInfo *ft = &m->tables [MONO_TABLE_FIELD];
918 const char *rva, *sig;
920 guint32 cols [MONO_FIELD_RVA_SIZE];
923 for (i = 0; i < t->rows; i++) {
924 mono_metadata_decode_row (t, i, cols, MONO_FIELD_RVA_SIZE);
925 rva = mono_cli_rva_map (m->image_info, cols [MONO_FIELD_RVA_RVA]);
926 sig = mono_metadata_blob_heap (m, mono_metadata_decode_row_col (ft, cols [MONO_FIELD_RVA_FIELD] -1, MONO_FIELD_SIGNATURE));
927 mono_metadata_decode_value (sig, &sig);
928 /* FIELD signature == 0x06 */
929 g_assert (*sig == 0x06);
930 type = mono_metadata_parse_field_type (m, 0, sig + 1, &sig);
931 mono_class_init (mono_class_from_mono_type (type));
932 size = mono_type_size (type, &align);
933 fprintf (output, ".data D_%08x = bytearray (", cols [MONO_FIELD_RVA_RVA]);
934 for (b = 0; b < size; ++b) {
936 fprintf (output, "\n\t");
937 fprintf (output, " %02x", rva [b] & 0xff);
939 fprintf (output, ") // size: %d\n", size);
946 void (*dumper) (MonoImage *m);
948 { "--assembly", MONO_TABLE_ASSEMBLY, dump_table_assembly },
949 { "--assemblyref", MONO_TABLE_ASSEMBLYREF, dump_table_assemblyref },
950 { "--classlayout", MONO_TABLE_CLASSLAYOUT, dump_table_class_layout },
951 { "--constant", MONO_TABLE_CONSTANT, dump_table_constant },
952 { "--customattr", MONO_TABLE_CUSTOMATTRIBUTE, dump_table_customattr },
953 { "--declsec", MONO_TABLE_DECLSECURITY, dump_table_declsec },
954 { "--event", MONO_TABLE_EVENT, dump_table_event },
955 { "--exported", MONO_TABLE_EXPORTEDTYPE, dump_table_exported },
956 { "--fields", MONO_TABLE_FIELD, dump_table_field },
957 { "--file", MONO_TABLE_FILE, dump_table_file },
958 { "--genericpar", MONO_TABLE_GENERICPARAM, dump_table_genericpar },
959 { "--interface", MONO_TABLE_INTERFACEIMPL, dump_table_interfaceimpl },
960 { "--manifest", MONO_TABLE_MANIFESTRESOURCE, dump_table_manifest },
961 { "--marshal", MONO_TABLE_FIELDMARSHAL, dump_table_field_marshal },
962 { "--memberref", MONO_TABLE_MEMBERREF, dump_table_memberref },
963 { "--method", MONO_TABLE_METHOD, dump_table_method },
964 { "--methodimpl", MONO_TABLE_METHODIMPL, dump_table_methodimpl },
965 { "--methodsem", MONO_TABLE_METHODSEMANTICS, dump_table_methodsem },
966 { "--methodspec", MONO_TABLE_METHODSPEC, dump_table_methodspec },
967 { "--moduleref", MONO_TABLE_MODULEREF, dump_table_moduleref },
968 { "--module", MONO_TABLE_MODULE, dump_table_module },
969 { "--nested", MONO_TABLE_NESTEDCLASS, dump_table_nestedclass },
970 { "--param", MONO_TABLE_PARAM, dump_table_param },
971 { "--parconst", MONO_TABLE_GENERICPARAMCONSTRAINT, dump_table_parconstraint },
972 { "--property", MONO_TABLE_PROPERTY, dump_table_property },
973 { "--propertymap", MONO_TABLE_PROPERTYMAP, dump_table_property_map },
974 { "--typedef", MONO_TABLE_TYPEDEF, dump_table_typedef },
975 { "--typeref", MONO_TABLE_TYPEREF, dump_table_typeref },
981 * @file: file containing CIL code.
983 * Disassembles the @file file.
986 disassemble_file (const char *file)
989 MonoImageOpenStatus status;
992 ass = mono_assembly_open (file, &status);
994 fprintf (stderr, "Error while trying to process %s\n", file);
1000 if (dump_table != -1){
1001 (*table_list [dump_table].dumper) (img);
1003 dump_header_data (img);
1005 dis_directive_assemblyref (img);
1006 dis_directive_assembly (img);
1011 mono_image_close (img);
1017 GString *args = g_string_new ("[--output=filename] [--help] [--mscorlib]\n");
1020 for (i = 0; table_list [i].name != NULL; i++){
1021 g_string_append (args, "[");
1022 g_string_append (args, table_list [i].name);
1023 g_string_append (args, "] ");
1024 if (((i-2) % 5) == 0)
1025 g_string_append_c (args, '\n');
1028 "Usage is: monodis %s file ..\n", args->str);
1033 main (int argc, char *argv [])
1035 GList *input_files = NULL, *l;
1039 for (i = 1; i < argc; i++){
1040 if (argv [i][0] == '-'){
1041 if (argv [i][1] == 'h')
1043 else if (argv [i][1] == 'd')
1044 dump_header_data_p = TRUE;
1045 else if (strcmp (argv [i], "--mscorlib") == 0) {
1046 substitute_with_mscorlib_p = TRUE;
1048 } else if (strncmp (argv [i], "--output=", 9) == 0) {
1049 output = fopen (argv [i]+9, "w");
1050 if (output == NULL) {
1051 fprintf (stderr, "Can't open output file `%s': %s\n",
1052 argv [i]+9, strerror (errno));
1056 } else if (strcmp (argv [i], "--help") == 0)
1058 for (j = 0; table_list [j].name != NULL; j++) {
1059 if (strcmp (argv [i], table_list [j].name) == 0)
1065 input_files = g_list_append (input_files, argv [i]);
1068 if (input_files == NULL)
1071 mono_init (argv [0]);
1073 for (l = input_files; l; l = l->next)
1074 disassemble_file (l->data);