Remove files from here
[mono.git] / mono / dis / main.c
index 51458a77b8a03c5fbecc8d315321fe4be63a5ec1..c555a549e5f4b0b5aa1572a029b9542fd8cf6ad4 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <glib.h>
+#include <stdlib.h>
+#include <errno.h>
 #include "meta.h"
 #include "util.h"
 #include "dump.h"
 #include "get.h"
 #include "dis-cil.h"
+#include <mono/metadata/loader.h>
+#include <mono/metadata/assembly.h>
+#include <mono/metadata/appdomain.h>
 
 FILE *output;
 
 /* True if you want to get a dump of the header data */
 gboolean dump_header_data_p = FALSE;
 
+gboolean substitute_with_mscorlib_p = FALSE;
+
 int dump_table = -1;
 
 static void
-dump_header_data (MonoAssembly *ass)
+dump_header_data (MonoImage *img)
 {
        if (!dump_header_data_p)
                return;
@@ -41,54 +48,69 @@ dump_header_data (MonoAssembly *ass)
 }
 
 static void
-dis_directive_assembly (metadata_t *m)
+dump_cattrs (MonoImage *m, guint32 token, const char *indent)
+{
+       GList *tmp, *list;
+
+       list = dis_get_custom_attrs (m, token);
+       for (tmp = list; tmp; tmp = tmp->next) {
+               fprintf (output, "%s%s\n", indent, (char*)tmp->data);
+               g_free (tmp->data);
+       }
+       g_list_free (list);
+}
+
+static void
+dis_directive_assembly (MonoImage *m)
 {
-       metadata_tableinfo_t *t  = &m->tables [META_TABLE_ASSEMBLY];
-       guint32 cols [9];
+       MonoTableInfo *t  = &m->tables [MONO_TABLE_ASSEMBLY];
+       guint32 cols [MONO_ASSEMBLY_SIZE];
        
        if (t->base == NULL)
                return;
 
-       expand (t, 0, cols, CSIZE (cols));
+       mono_metadata_decode_row (t, 0, cols, MONO_ASSEMBLY_SIZE);
        
+       fprintf (output, ".assembly '%s'\n{\n",
+                mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_NAME]));
+       dump_cattrs (m, MONO_TOKEN_ASSEMBLY | 1, "  ");
        fprintf (output,
-                ".assembly %s\n"
-                "{\n"
                 "  .hash algorithm 0x%08x\n"
-                "  .ver  %d.%d.%d.%d"
+                "  .ver  %d:%d:%d:%d"
                 "%s %s"
                 "%s"
                 "\n"
                 "}\n",
-                mono_metadata_string_heap (m, cols [7]),
-                cols [0],
-                cols [1], cols [2], cols [3], cols [4],
-                cols [8] ? "\n  .locale" : "",
-                cols [8] ? mono_metadata_string_heap (m, cols [8]) : "",
-                cols [6] ? "\n  .publickey" : ""
+                cols [MONO_ASSEMBLY_HASH_ALG],
+                cols [MONO_ASSEMBLY_MAJOR_VERSION], cols [MONO_ASSEMBLY_MINOR_VERSION], 
+                cols [MONO_ASSEMBLY_BUILD_NUMBER], cols [MONO_ASSEMBLY_REV_NUMBER],
+                cols [MONO_ASSEMBLY_CULTURE] ? "\n  .locale" : "",
+                cols [MONO_ASSEMBLY_CULTURE] ? mono_metadata_string_heap (m, cols [MONO_ASSEMBLY_CULTURE]) : "",
+                cols [MONO_ASSEMBLY_PUBLIC_KEY] ? "\n  .publickey" : ""
                );
 }
 
 static void
-dis_directive_assemblyref (metadata_t *m)
+dis_directive_assemblyref (MonoImage *m)
 {
-       metadata_tableinfo_t *t = &m->tables [META_TABLE_ASSEMBLYREF];
-       guint32 cols [9];
+       MonoTableInfo *t = &m->tables [MONO_TABLE_ASSEMBLYREF];
+       guint32 cols [MONO_ASSEMBLYREF_SIZE];
        int i;
        
        if (t->base == NULL)
                return;
 
        for (i = 0; i < t->rows; i++){
-               expand (t, i, cols, CSIZE (cols));
+               mono_metadata_decode_row (t, i, cols, MONO_ASSEMBLYREF_SIZE);
 
                fprintf (output,
                         ".assembly extern %s\n"
                         "{\n"
-                        "  .ver %d.%d.%d.%d\n"
+                        "  .ver %d:%d:%d:%d\n"
                         "}\n",
-                        mono_metadata_string_heap (m, cols [6]),
-                        cols [0], cols [1], cols [2], cols [3]
+                        mono_metadata_string_heap (m, cols [MONO_ASSEMBLYREF_NAME]),
+                        cols [MONO_ASSEMBLYREF_MAJOR_VERSION], cols [MONO_ASSEMBLYREF_MINOR_VERSION], 
+                        cols [MONO_ASSEMBLYREF_BUILD_NUMBER], cols [MONO_ASSEMBLYREF_REV_NUMBER]
                        );
        }
 }
@@ -158,10 +180,12 @@ typedef_flags (guint32 flags)
  * This routine displays all the decoded fields from @start to @end
  */
 static void
-dis_field_list (metadata_t *m, guint32 start, guint32 end)
+dis_field_list (MonoImage *m, guint32 start, guint32 end)
 {
-       metadata_tableinfo_t *t = &m->tables [META_TABLE_FIELD];
-       guint32 cols [3];
+       MonoTableInfo *t = &m->tables [MONO_TABLE_FIELD];
+       guint32 cols [MONO_FIELD_SIZE];
+       char rva_desc [32];
+       guint32 rva;
        int i;
 
        if (end > t->rows + 1) {
@@ -170,31 +194,48 @@ dis_field_list (metadata_t *m, guint32 start, guint32 end)
        }
                        
        for (i = start; i < end; i++){
-               char *sig, *flags;
+               char *sig, *flags, *attrs = NULL;
+               guint32 field_offset = -1;
                
-               expand (t, i, cols, CSIZE (cols));
-               sig = get_field_signature (m, cols [2]);
-               flags = field_flags (cols [0]);
+               mono_metadata_decode_row (t, i, cols, MONO_FIELD_SIZE);
+               sig = get_field_signature (m, cols [MONO_FIELD_SIGNATURE]);
+               flags = field_flags (cols [MONO_FIELD_FLAGS]);
+
+               if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_HAS_FIELD_RVA) {
+                       mono_metadata_field_info (m, i, NULL, &rva, NULL);
+                       g_snprintf (rva_desc, sizeof (rva_desc), " at D_%08x", rva);
+               } else {
+                       rva_desc [0] = 0;
+               }
                
-               if (cols [0] & FIELD_ATTRIBUTE_LITERAL){
-                       ElementTypeEnum type;
+               mono_metadata_field_info (m, i, &field_offset, NULL, NULL);
+               if (field_offset != -1)
+                       attrs = g_strdup_printf ("[%d]", field_offset);
+               if (cols [MONO_FIELD_FLAGS] & FIELD_ATTRIBUTE_LITERAL){
                        char *lit;
+                       guint32 const_cols [MONO_CONSTANT_SIZE];
+                       guint32 crow;
                        
-                       type = get_field_literal_type (m, cols [2]);
-                       lit = g_strdup ("FIXME:Do-not-know-how-to-get-this-from-the-constants-table");
-                       /* get_constant (m, type, cols [2]); */
+                       if ((crow = mono_metadata_get_constant_index (m, MONO_TOKEN_FIELD_DEF | (i+1)))) {
+                               mono_metadata_decode_row (&m->tables [MONO_TABLE_CONSTANT], crow-1, const_cols, MONO_CONSTANT_SIZE);
+                               lit = get_constant (m, const_cols [MONO_CONSTANT_TYPE], const_cols [MONO_CONSTANT_VALUE]);
+                       } else {
+                               lit = g_strdup ("not found");
+                       }
                        
                        fprintf (output, "    .field %s %s %s = ",
                                 flags, sig,
-                                mono_metadata_string_heap (m, cols [1]));
+                                mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]));
                        fprintf (output, "%s\n", lit);
                        g_free (lit);
-               } else 
-                       fprintf (output, "    .field %s %s %s\n",
-                                flags, sig,
-                                mono_metadata_string_heap (m, cols [1]));
+               } else
+                       fprintf (output, "    .field %s %s %s %s%s\n",
+                                attrs? attrs: "", flags, sig,
+                                mono_metadata_string_heap (m, cols [MONO_FIELD_NAME]), rva_desc);
+               g_free (attrs);
                g_free (flags);
                g_free (sig);
+               dump_cattrs (m, MONO_TOKEN_FIELD_DEF | (i + 1), "    ");
        }
 }
 
@@ -218,10 +259,10 @@ static map_t method_flags_map [] = {
        { METHOD_ATTRIBUTE_ABSTRACT,            "abstract " },
        { METHOD_ATTRIBUTE_SPECIAL_NAME,        "specialname " },
        { METHOD_ATTRIBUTE_RT_SPECIAL_NAME,     "rtspecialname " },
-       { METHOD_ATTRIBUTE_PINVOKE_IMPL,        "pinvokeimpl " }, 
        { METHOD_ATTRIBUTE_UNMANAGED_EXPORT,    "export " },
        { METHOD_ATTRIBUTE_HAS_SECURITY,        "hassecurity" },
        { METHOD_ATTRIBUTE_REQUIRE_SEC_OBJECT,  "requiresecobj" },
+       { METHOD_ATTRIBUTE_PINVOKE_IMPL,        "pinvokeimpl " }, 
        { 0, NULL }
 };
 
@@ -246,6 +287,52 @@ method_flags (guint32 f)
        return s;
 }
 
+static map_t pinvoke_flags_map [] = {
+       { PINVOKE_ATTRIBUTE_NO_MANGLE ,            "nomangle " },
+       { PINVOKE_ATTRIBUTE_SUPPORTS_LAST_ERROR,   "lasterr " },
+       { 0, NULL }
+};
+
+static map_t pinvoke_call_conv_map [] = {
+       { PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI,      "winapi " },
+       { PINVOKE_ATTRIBUTE_CALL_CONV_CDECL,       "cdecl " },
+       { PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL,     "stdcall " },
+       { PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL,    "thiscall " },
+       { PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL,    "fastcall " },
+       { 0, NULL }
+};
+
+static map_t pinvoke_char_set_map [] = {
+       { PINVOKE_ATTRIBUTE_CHAR_SET_NOT_SPEC,     "" },
+       { PINVOKE_ATTRIBUTE_CHAR_SET_ANSI,         "ansi " },
+       { PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE ,     "unicode " },
+       { PINVOKE_ATTRIBUTE_CHAR_SET_AUTO,         "autochar " },
+       { 0, NULL }
+};
+
+/**
+ * pinvoke_flags:
+ *
+ * Returns a stringified version of the Method's pinvoke flags
+ */
+static char *
+pinvoke_flags (guint32 f)
+{
+       GString *str = g_string_new ("");
+       int cset = f & PINVOKE_ATTRIBUTE_CHAR_SET_MASK;
+       int cconv = f & PINVOKE_ATTRIBUTE_CALL_CONV_MASK;
+       char *s;
+       
+       g_string_append (str, map (cset, pinvoke_char_set_map));
+       g_string_append (str, map (cconv, pinvoke_call_conv_map));
+       g_string_append (str, flags (f, pinvoke_flags_map));
+
+       s = g_strdup(str->str);
+       g_string_free (str, FALSE);
+
+       return s;
+}
+
 static map_t method_impl_map [] = {
        { METHOD_IMPL_ATTRIBUTE_IL,              "cil " },
        { METHOD_IMPL_ATTRIBUTE_NATIVE,          "native " },
@@ -287,63 +374,46 @@ method_impl_flags (guint32 f)
 }
 
 static void
-dis_locals (metadata_t *m, guint32 token
+dis_locals (MonoImage *m, MonoMethodHeader *mh
 {
-       metadata_tableinfo_t *t = &m->tables [META_TABLE_STANDALONESIG];
-       const char *ptr;
-       guint32 cols[1];
-       int len=0, i, bsize;
-
-       expand (t, (token&0xffffff)-1, cols, CSIZE(cols));
-       ptr = mono_metadata_blob_heap (m, cols[0]);
-       ptr = get_blob_encoded_size (ptr, &bsize);
-       if (*ptr != 0x07)
-                       g_warning("wrong signature for locals blob");
-       ptr++;
-       ptr = get_encoded_value (ptr, &len);
-       fprintf(output, "\t.locals ( // %d\n", len);
-       for (i=0; i < len; ++i) {
-               int val;
-               char * desc = NULL;
-               const char *p = ptr;
-               ptr = get_encoded_value (ptr, &val);
-               if (val == ELEMENT_TYPE_PINNED) {
-                       fprintf(output, "//pinned\n");
-                       p = ptr;
-                       ptr = get_encoded_value (ptr, &val);
-               }
-               if (val == ELEMENT_TYPE_BYREF) {
-                       fprintf(output, "// byref\n");
-                       p = ptr;
-               }
-               ptr = get_type(m, p, &desc);
-               fprintf(output, "\t\t%s\tV_%d\n", desc, i);
+       int i;
+
+       fprintf(output, "\t.locals %s(\n", mh->init_locals ? "init " : "");
+       for (i=0; i < mh->num_locals; ++i) {
+               char * desc;
+               if (i)
+                       fprintf(output, ",\n");
+               /* print also byref and pinned attributes */
+               desc = dis_stringify_type (m, mh->locals[i]);
+               fprintf(output, "\t\t%s\tV_%d", desc, i);
                g_free(desc);
        }
-       fprintf(output, "\t)\n");
+       fprintf(output, ")\n");
 }
 
 static void
-dis_code (metadata_t *m, cli_image_info_t *ii, guint32 rva)
+dis_code (MonoImage *m, guint32 rva)
 {
-       MonoMetaMethodHeader *mh;
-       const char *ptr = cli_rva_map (ii, rva);
-       char *loc;
+       MonoMethodHeader *mh;
+       MonoCLIImageInfo *ii = m->image_info;
+       const char *ptr = mono_cli_rva_map (ii, rva);
+       const char *loc;
 
        if (rva == 0)
                return;
 
-       mh = mono_metadata_parse_mh (ptr);
-       loc = mono_metadata_locate_token (m, ii->cli_cli_header.ch_entry_point);
-       if (rva == read32(loc))
-               fprintf (output, "\t.entrypoint\n");
+       mh = mono_metadata_parse_mh (m, ptr);
+       if (ii->cli_cli_header.ch_entry_point){
+               loc = mono_metadata_locate_token (m, ii->cli_cli_header.ch_entry_point);
+               if (rva == read32 (loc))
+                       fprintf (output, "\t.entrypoint\n");
+       }
+       
+       fprintf (output, "\t// Code size %d (0x%x)\n", mh->code_size, mh->code_size);
        fprintf (output, "\t.maxstack %d\n", mh->max_stack);
-       fprintf (output, "\t// Code size=%d (0x%x)\n", mh->code_size, mh->code_size);
-       printf ("\t// Values Code Size=%d/0x%x\n\t// LocalTok=%x\n\n",
-               mh->code_size, mh->code_size, mh->local_var_sig_tok);
-       if (mh->local_var_sig_tok)
-               dis_locals (m, mh->local_var_sig_tok);
-       dissasemble_cil (m, mh->code, mh->code_size);
+       if (mh->num_locals)
+               dis_locals (m, mh);
+       dissasemble_cil (m, mh);
        
 /*
   hex_dump (mh->code, 0, mh->code_size);
@@ -353,57 +423,39 @@ dis_code (metadata_t *m, cli_image_info_t *ii, guint32 rva)
        mono_metadata_free_mh (mh);
 }
 
-typedef struct {
-       char  flags;
-       char *ret_type;
-       int   param_count;
-       char **param;
-} MethodSignature;
-
-/**
- * parse_method_signature:
- * @m: metadata context 
- * @blob_signature: pointer to the signature in the Blob heap
- *
- * 22.2.1: MethodDefSig.  
- *
- * Returns the parsed information in the MethodSignature structure
- * needs to be deallocated with free_method_signature().
- */
-static MethodSignature *
-parse_method_signature (metadata_t *m, guint32 blob_signature)
+static char *
+pinvoke_info (MonoImage *m, guint32 mindex)
 {
-       const char *ptr = mono_metadata_blob_heap (m, blob_signature);
-       MethodSignature *ms = g_new0 (MethodSignature, 1);
-       int i, len;
+       MonoTableInfo *im = &m->tables [MONO_TABLE_IMPLMAP];
+       MonoTableInfo *mr = &m->tables [MONO_TABLE_MODULEREF];
+       guint32 im_cols [MONO_IMPLMAP_SIZE];
+       guint32 mr_cols [MONO_MODULEREF_SIZE];
+       const char *import, *scope;
+       char *flags;
+       int i;
 
-       ptr = get_encoded_value (ptr, &len);
-       fprintf (output, "     // SIG: ");
-       hex_dump (ptr, 0, -len);
-       fprintf (output, "\n");
-       
-       ms->flags = *ptr++;
+       for (i = 0; i < im->rows; i++) {
 
-       ptr = get_encoded_value (ptr, &ms->param_count);
-       ptr = get_ret_type (m, ptr, &ms->ret_type);
-       ms->param = g_new (char *, ms->param_count);
-       
-       for (i = 0; i < ms->param_count; i++)
-               ptr = get_param (m, ptr, &(ms->param [i]));
+               mono_metadata_decode_row (im, i, im_cols, MONO_IMPLMAP_SIZE);
 
-       return ms;
-}
+               if ((im_cols [MONO_IMPLMAP_MEMBER] >> 1) == mindex + 1) {
 
-static void
-free_method_signature (MethodSignature *ms)
-{
-       int i;
-       
-       for (i = 0; i < ms->param_count; i++)
-               g_free (ms->param [i]);
-       g_free (ms->param);
-       g_free (ms->ret_type);
-       g_free (ms);
+                       flags = pinvoke_flags (im_cols [MONO_IMPLMAP_FLAGS]);
+
+                       import = mono_metadata_string_heap (m, im_cols [MONO_IMPLMAP_NAME]);
+
+                       mono_metadata_decode_row (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, 
+                                                 mr_cols, MONO_MODULEREF_SIZE);
+
+                       scope = mono_metadata_string_heap (m, mr_cols [MONO_MODULEREF_NAME]);
+                               
+                       return g_strdup_printf ("(%s as %s %s)", scope, import,
+                                               flags);
+                       g_free (flags);
+               }
+       }
+
+       return NULL;
 }
 
 /**
@@ -415,13 +467,10 @@ free_method_signature (MethodSignature *ms)
  * This routine displays the methods in the Method Table from @start to @end
  */
 static void
-dis_method_list (metadata_t *m, cli_image_info_t *ii, guint32 start, guint32 end)
+dis_method_list (MonoImage *m, guint32 start, guint32 end)
 {
-       metadata_tableinfo_t *t = &m->tables [META_TABLE_METHOD];
-       metadata_tableinfo_t *p = &m->tables [META_TABLE_PARAM];
-       guint32 cols [6];
-       guint32 cols_next [6];
-       guint32 param_cols [3];
+       MonoTableInfo *t = &m->tables [MONO_TABLE_METHOD];
+       guint32 cols [MONO_METHOD_SIZE];
        int i;
 
        if (end > t->rows){
@@ -431,51 +480,257 @@ dis_method_list (metadata_t *m, cli_image_info_t *ii, guint32 start, guint32 end
        }
 
        for (i = start; i < end; i++){
-               MethodSignature *ms;
+               MonoMethodSignature *ms;
                char *flags, *impl_flags;
+               const char *sig;
+               char *sig_str;
                
-               expand (t, i, cols, CSIZE (cols));
-               expand (t, i + 1, cols_next, CSIZE (cols_next));
+               mono_metadata_decode_row (t, i, cols, MONO_METHOD_SIZE);
 
-               flags = method_flags (cols [2]);
-               impl_flags = method_impl_flags (cols [1]);
+               flags = method_flags (cols [MONO_METHOD_FLAGS]);
+               impl_flags = method_impl_flags (cols [MONO_METHOD_IMPLFLAGS]);
 
-               ms = parse_method_signature (m, cols [4]);
+               sig = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
+               mono_metadata_decode_blob_size (sig, &sig);
+               ms = mono_metadata_parse_method_signature (m, 1, sig, &sig);
+               sig_str = dis_stringify_method_signature (m, ms, i + 1);
                        
-               fprintf (output,
-                        "    .method %s\n",
-                        flags);
-               fprintf (output,
-                        "           %s %s",
-                        ms->ret_type,
-                        mono_metadata_string_heap (m, cols [3]));
-               if (ms->param_count > 0){
-                       int i;
-
-                       fprintf (output, "(\n");
-                       for (i = 0; i < ms->param_count; i++){
-                               char *pf;
-                               
-                               expand (p, i, param_cols, CSIZE (param_cols));
-                               pf = param_flags (param_cols [0]);
-                               fprintf (
-                                       output, "\t\t%s %s %s%s", pf, ms->param [i],
-                                       mono_metadata_string_heap (m, param_cols [2]),
-                                       (i+1 == ms->param_count) ? ")" : ",\n");
-
-                               g_free (pf);
-                       }
-                               
-               }
+               fprintf (output, "    // method line %d\n", i + 1);
+               fprintf (output, "    .method %s", flags);
+
+               if (cols [MONO_METHOD_FLAGS] & METHOD_ATTRIBUTE_PINVOKE_IMPL)
+                       fprintf (output, "%s", pinvoke_info (m, i));
+
+               fprintf (output, "\n           %s", sig_str);
                fprintf (output, " %s\n", impl_flags);
                g_free (flags);
                g_free (impl_flags);
                
                fprintf (output, "    {\n");
-               fprintf (output, "        // Method begins at RVA 0x%x\n", cols [0]);
-               dis_code (m, ii, cols [0]);
-               fprintf (output, "    }\n\n");
-               free_method_signature (ms);
+               dump_cattrs (m, MONO_TOKEN_METHOD_DEF | (i + 1), "        ");
+               /* FIXME: need to sump also param custom attributes */
+               fprintf (output, "        // Method begins at RVA 0x%x\n", cols [MONO_METHOD_RVA]);
+               dis_code (m, cols [MONO_METHOD_RVA]);
+               fprintf (output, "    } // end of method %s\n\n", sig_str);
+               mono_metadata_free_method_signature (ms);
+               g_free (sig_str);
+       }
+}
+
+typedef struct {
+       MonoTableInfo *t;
+       guint32 col_idx;
+       guint32 idx;
+       guint32 result;
+} plocator_t;
+
+static int
+table_locator (const void *a, const void *b)
+{
+       plocator_t *loc = (plocator_t *) a;
+       const char *bb = (const char *) b;
+       guint32 table_index = (bb - loc->t->base) / loc->t->row_size;
+       guint32 col;
+       
+       col = mono_metadata_decode_row_col (loc->t, table_index, loc->col_idx);
+
+       if (loc->idx == col) {
+               loc->result = table_index;
+               return 0;
+       }
+       if (loc->idx < col)
+               return -1;
+       else 
+               return 1;
+}
+
+static void
+dis_property_methods (MonoImage *m, guint32 prop)
+{
+       guint start, end;
+       MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
+       guint32 cols [MONO_METHOD_SEMA_SIZE];
+       char *sig;
+       const char *type[] = {NULL, ".set", ".get", NULL, ".other"};
+
+       start = mono_metadata_methods_from_property (m, prop, &end);
+       while (start < end) {
+               mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
+               sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD]);
+               fprintf (output, "\t\t%s %s\n", type [cols [MONO_METHOD_SEMA_SEMANTICS]], sig);
+               g_free (sig);
+               ++start;
+       }
+}
+
+static char*
+dis_property_signature (MonoImage *m, guint32 prop_idx)
+{
+       MonoTableInfo *propt = &m->tables [MONO_TABLE_PROPERTY];
+       const char *ptr;
+       guint32 pcount, i;
+       guint32 cols [MONO_PROPERTY_SIZE];
+       MonoType *type;
+       MonoType *param;
+       char *blurb;
+       const char *name;
+       int prop_flags;
+       GString *res = g_string_new ("");
+
+       mono_metadata_decode_row (propt, prop_idx, cols, MONO_PROPERTY_SIZE);
+       name = mono_metadata_string_heap (m, cols [MONO_PROPERTY_NAME]);
+       prop_flags = cols [MONO_PROPERTY_FLAGS];
+       ptr = mono_metadata_blob_heap (m, cols [MONO_PROPERTY_TYPE]);
+       mono_metadata_decode_blob_size (ptr, &ptr);
+       /* ECMA claims 0x08 ... */
+       if (*ptr != 0x28 && *ptr != 0x08)
+               g_warning("incorrect signature in propert blob: 0x%x", *ptr);
+       ptr++;
+       pcount = mono_metadata_decode_value (ptr, &ptr);
+       type = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
+       blurb = dis_stringify_type (m, type);
+       if (prop_flags & 0x0200)
+               g_string_append (res, "special ");
+       if (prop_flags & 0x0400)
+               g_string_append (res, "runtime ");
+       if (prop_flags & 0x1000)
+               g_string_append (res, "hasdefault ");
+       g_string_sprintfa (res, "%s %s (", blurb, name);
+       g_free (blurb);
+       mono_metadata_free_type (type);
+       for (i = 0; i < pcount; i++) {
+               if (i)
+                       g_string_append (res, ", ");
+               param = mono_metadata_parse_param (m, ptr, &ptr);
+               blurb = dis_stringify_param (m, param);
+               g_string_append (res, blurb);
+               mono_metadata_free_type (param);
+               g_free (blurb);
+       }
+       g_string_append_c (res, ')');
+       blurb = res->str;
+       g_string_free (res, FALSE);
+       return blurb;
+
+}
+
+static void
+dis_property_list (MonoImage *m, guint32 typedef_row)
+{
+       guint start, end, i;
+       start = mono_metadata_properties_from_typedef (m, typedef_row, &end);
+
+       for (i = start; i < end; ++i) {
+               char *sig = dis_property_signature (m, i);
+               fprintf (output, "\t.property %s\n\t{\n", sig);
+               dump_cattrs (m, MONO_TOKEN_PROPERTY | (i + 1), "\t\t");
+               dis_property_methods (m, i);
+               fprintf (output, "\t}\n");
+               g_free (sig);
+       }
+}
+
+static char*
+dis_event_signature (MonoImage *m, guint32 event_idx)
+{
+       MonoTableInfo *et = &m->tables [MONO_TABLE_EVENT];
+       const char *name;
+       char *type, *res;
+       guint32 cols [MONO_EVENT_SIZE];
+       
+       mono_metadata_decode_row (et, event_idx, cols, MONO_EVENT_SIZE);
+       name = mono_metadata_string_heap (m, cols [MONO_EVENT_NAME]);
+       type = get_typedef_or_ref (m, cols [MONO_EVENT_TYPE]);
+
+       res = g_strdup_printf ("%s %s", type, name);
+       g_free (type);
+       return res;
+}
+
+static void
+dis_event_methods (MonoImage *m, guint32 event)
+{
+       guint start, end;
+       MonoTableInfo *msemt = &m->tables [MONO_TABLE_METHODSEMANTICS];
+       guint32 cols [MONO_METHOD_SEMA_SIZE];
+       char *sig;
+       const char *type;
+
+       start = mono_metadata_methods_from_event (m, event, &end);
+       while (start < end) {
+               mono_metadata_decode_row (msemt, start, cols, MONO_METHOD_SEMA_SIZE);
+               sig = dis_stringify_method_signature (m, NULL, cols [MONO_METHOD_SEMA_METHOD]);
+               switch (cols [MONO_METHOD_SEMA_SEMANTICS]) {
+               case METHOD_SEMANTIC_OTHER:
+                       type = ".other"; break;
+               case METHOD_SEMANTIC_ADD_ON:
+                       type = ".addon"; break;
+               case METHOD_SEMANTIC_REMOVE_ON:
+                       type = ".removeon"; break;
+               case METHOD_SEMANTIC_FIRE:
+                       type = ".fire"; break;
+               default:
+                       break;
+               }
+               fprintf (output, "\t\t%s %s\n", type, sig);
+               g_free (sig);
+               ++start;
+       }
+}
+
+static void
+dis_event_list (MonoImage *m, guint32 typedef_row)
+{
+       guint start, end, i;
+       start = mono_metadata_events_from_typedef (m, typedef_row, &end);
+
+       for (i = start; i < end; ++i) {
+               char *sig = dis_event_signature (m, i);
+               fprintf (output, "\t.event %s\n\t{\n", sig);
+               dump_cattrs (m, MONO_TOKEN_EVENT | (i + 1), "\t\t");
+               dis_event_methods (m, i);
+               fprintf (output, "\t}\n");
+               g_free (sig);
+       }
+}
+
+static void
+dis_interfaces (MonoImage *m, guint32 typedef_row)
+{
+       plocator_t loc;
+       guint start;
+       guint32 cols [MONO_INTERFACEIMPL_SIZE];
+       char *intf;
+       MonoTableInfo *table = &m->tables [MONO_TABLE_INTERFACEIMPL];
+
+       if (!table->base)
+               return;
+
+       loc.t = table;
+       loc.col_idx = MONO_INTERFACEIMPL_CLASS;
+       loc.idx = typedef_row;
+
+       if (!bsearch (&loc, table->base, table->rows, table->row_size, table_locator))
+               return;
+
+       start = loc.result;
+       /*
+        * We may end up in the middle of the rows... 
+        */
+       while (start > 0) {
+               if (loc.idx == mono_metadata_decode_row_col (table, start - 1, MONO_INTERFACEIMPL_CLASS))
+                       start--;
+               else
+                       break;
+       }
+       while (start < table->rows) {
+               mono_metadata_decode_row (table, start, cols, MONO_INTERFACEIMPL_SIZE);
+               if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
+                       break;
+               intf = get_typedef_or_ref (m, cols [MONO_INTERFACEIMPL_INTERFACE]);
+               fprintf (output, "  \timplements %s\n", intf);
+               g_free (intf);
+               ++start;
        }
 }
 
@@ -487,64 +742,73 @@ dis_method_list (metadata_t *m, cli_image_info_t *ii, guint32 start, guint32 end
  * Disassembles the type whose index in the TypeDef table is @n.
  */
 static void
-dis_type (metadata_t *m, cli_image_info_t *ii, int n)
+dis_type (MonoImage *m, int n)
 {
-       metadata_tableinfo_t *t = &m->tables [META_TABLE_TYPEDEF];
-       guint32 cols [6];
-       guint32 cols_next [6];
-       const char *name;
+       MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
+       guint32 cols [MONO_TYPEDEF_SIZE];
+       guint32 cols_next [MONO_TYPEDEF_SIZE];
+       const char *name, *nspace;
+       guint32 packing_size, class_size;
        gboolean next_is_valid, last;
        
-       expand (t, n, cols, CSIZE (cols));
+       mono_metadata_decode_row (t, n, cols, MONO_TYPEDEF_SIZE);
 
-       if (t->rows > n+1){
-               expand (t, n + 1, cols_next, CSIZE (cols_next));
+       if (t->rows > n + 1) {
+               mono_metadata_decode_row (t, n + 1, cols_next, MONO_TYPEDEF_SIZE);
                next_is_valid = 1;
        } else
                next_is_valid = 0;
 
-       fprintf (output, ".namespace %s\n{\n", mono_metadata_string_heap (m, cols [2]));
-       name = mono_metadata_string_heap (m, cols [1]);
+       nspace = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE]);
+       if (*nspace)
+               fprintf (output, ".namespace %s\n{\n", nspace);
+       name = mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME]);
 
-       if ((cols [0] & TYPE_ATTRIBUTE_CLASS_SEMANTIC_MASK) == TYPE_ATTRIBUTE_CLASS){
-               char *base = get_typedef_or_ref (m, cols [3]);
-               fprintf (output, "  .class %s%s\n", typedef_flags (cols [0]), name);
+       if ((cols [MONO_TYPEDEF_FLAGS] & TYPE_ATTRIBUTE_CLASS_SEMANTIC_MASK) == TYPE_ATTRIBUTE_CLASS){
+               char *base = get_typedef_or_ref (m, cols [MONO_TYPEDEF_EXTENDS]);
+               fprintf (output, "  .class %s%s\n", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), name);
                fprintf (output, "  \textends %s\n", base);
                g_free (base);
        } else
-               fprintf (output, "  .class interface %s%s\n", typedef_flags (cols [0]), name);
+               fprintf (output, "  .class interface %s%s\n", typedef_flags (cols [MONO_TYPEDEF_FLAGS]), name);
        
+       dis_interfaces (m, n + 1);
        fprintf (output, "  {\n");
+       dump_cattrs (m, MONO_TOKEN_TYPE_DEF | (n + 1), "    ");
 
+       if (mono_metadata_packing_from_typedef (m, n + 1, &packing_size, &class_size)) {
+               fprintf (output, "    .pack %d\n", packing_size);
+               fprintf (output, "    .size %d\n", class_size);
+       }
        /*
         * The value in the table is always valid, we know we have fields
         * if the value stored is different than the next record.
         */
 
        if (next_is_valid)
-               last = cols_next [4] - 1;
+               last = cols_next [MONO_TYPEDEF_FIELD_LIST] - 1;
        else
-               last = m->tables [META_TABLE_FIELD].rows;
+               last = m->tables [MONO_TABLE_FIELD].rows;
                        
-       /*if (cols [4] != cols_next [4] && cols_next [4] != 0)
-               dis_field_list (m, cols [4] - 1, last);*/
-       if (cols[4] && cols[4] <= m->tables [META_TABLE_FIELD].rows)
-               dis_field_list (m, cols [4] - 1, last);
-       /*fprintf (output, "cols[4] -> %d   cols_next[4] -> %d   last -> %d  rows -> %d\n", cols[4], cols_next[4], last, m->tables [META_TABLE_FIELD].rows);*/
+       if (cols [MONO_TYPEDEF_FIELD_LIST] && cols [MONO_TYPEDEF_FIELD_LIST] <= m->tables [MONO_TABLE_FIELD].rows)
+               dis_field_list (m, cols [MONO_TYPEDEF_FIELD_LIST] - 1, last);
        fprintf (output, "\n");
 
        if (next_is_valid)
-               last = cols_next [5] - 1;
+               last = cols_next [MONO_TYPEDEF_METHOD_LIST] - 1;
        else
-               last = m->tables [META_TABLE_METHOD].rows;
+               last = m->tables [MONO_TABLE_METHOD].rows;
        
-       /*if (cols [4] != cols_next [5] && cols_next [5] != 0)
-               dis_method_list (m, ii, cols [5] - 1, last);*/
-       /*fprintf (output, "method(%d): cols[5] -> %d   cols_next[5] -> %d   last -> %d  rows -> %d\n", next_is_valid, cols[5], cols_next[5], last, m->tables [META_TABLE_METHOD].rows);*/
-       if (cols [5] < m->tables [META_TABLE_METHOD].rows)
-               dis_method_list (m, ii, cols [5]-1, last);
+       if (cols [MONO_TYPEDEF_METHOD_LIST] && cols [MONO_TYPEDEF_METHOD_LIST] <= m->tables [MONO_TABLE_METHOD].rows)
+               dis_method_list (m, cols [MONO_TYPEDEF_METHOD_LIST] - 1, last);
+
+       dis_property_list (m, n);
+       dis_event_list (m, n);
 
-       fprintf (output, "  }\n}\n\n");
+       fprintf (output, "  } // end of type %s%s%s\n", nspace, *nspace? ".": "", name);
+       if (*nspace)
+               fprintf (output, "}\n");
+       fprintf (output, "\n");
 }
 
 /**
@@ -554,34 +818,81 @@ dis_type (metadata_t *m, cli_image_info_t *ii, int n)
  * disassembles all types in the @m context
  */
 static void
-dis_types (metadata_t *m, cli_image_info_t *ii)
+dis_types (MonoImage *m)
 {
-       metadata_tableinfo_t *t = &m->tables [META_TABLE_TYPEDEF];
+       MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
        int i;
 
        for (i = 1; i < t->rows; i++)
-               dis_type (m, ii, i);
+               dis_type (m, i);
+}
+
+/**
+ * dis_data:
+ * @m: metadata context
+ *
+ * disassembles all data blobs references in the FieldRVA table in the @m context
+ */
+static void
+dis_data (MonoImage *m)
+{
+       MonoTableInfo *t = &m->tables [MONO_TABLE_FIELDRVA];
+       MonoTableInfo *ft = &m->tables [MONO_TABLE_FIELD];
+       int i, b;
+       const char *rva, *sig;
+       guint32 align, size;
+       guint32 cols [MONO_FIELD_RVA_SIZE];
+       MonoType *type;
+
+       for (i = 0; i < t->rows; i++) {
+               mono_metadata_decode_row (t, i, cols, MONO_FIELD_RVA_SIZE);
+               rva = mono_cli_rva_map (m->image_info, cols [MONO_FIELD_RVA_RVA]);
+               sig = mono_metadata_blob_heap (m, mono_metadata_decode_row_col (ft, cols [MONO_FIELD_RVA_FIELD] -1, MONO_FIELD_SIGNATURE));
+               mono_metadata_decode_value (sig, &sig);
+               /* FIELD signature == 0x06 */
+               g_assert (*sig == 0x06);
+               type = mono_metadata_parse_field_type (m, 0, sig + 1, &sig);
+               mono_class_init (mono_class_from_mono_type (type));
+               size = mono_class_value_size (mono_class_from_mono_type (type), &align);
+               fprintf (output, ".data D_%08x = bytearray (", cols [MONO_FIELD_RVA_RVA]);
+               for (b = 0; b < size; ++b) {
+                       if (!(b % 16))
+                               fprintf (output, "\n\t");
+                       fprintf (output, " %02x", rva [b] & 0xff);
+               }
+               fprintf (output, ") // size: %d\n", size);
+       }
 }
 
 struct {
-       char *name;
+       const char *name;
        int table;
-       void (*dumper) (metadata_t *m);
+       void (*dumper) (MonoImage *m);
 } table_list [] = {
-       { "--assembly",    META_TABLE_ASSEMBLY,    dump_table_assembly },
-       { "--assemblyref", META_TABLE_ASSEMBLYREF, dump_table_assemblyref },
-       { "--fields",      META_TABLE_FIELD,       dump_table_field },
-       { "--memberref",   META_TABLE_MEMBERREF,   dump_table_memberref },
-       { "--param",       META_TABLE_PARAM,       dump_table_param },
-       { "--typedef",     META_TABLE_TYPEDEF,     dump_table_typedef },
-       { "--typeref",     META_TABLE_TYPEREF,     dump_table_typeref },
-       { "--classlayout", META_TABLE_CLASSLAYOUT, dump_table_class_layout },
-       { "--constant",    META_TABLE_CONSTANT,    dump_table_constant },
-       { "--property",    META_TABLE_PROPERTY,    dump_table_property },
-       { "--event",       META_TABLE_EVENT,       dump_table_event },
-       { "--file",        META_TABLE_FILE,        dump_table_file },
-       { "--moduleref",   META_TABLE_MODULEREF,   dump_table_moduleref },
-       { "--method",      META_TABLE_METHOD,      dump_table_method },
+       { "--assembly",    MONO_TABLE_ASSEMBLY,    dump_table_assembly },
+       { "--assemblyref", MONO_TABLE_ASSEMBLYREF, dump_table_assemblyref },
+       { "--fields",      MONO_TABLE_FIELD,       dump_table_field },
+       { "--marshal",     MONO_TABLE_FIELDMARSHAL,     dump_table_field_marshal },
+       { "--memberref",   MONO_TABLE_MEMBERREF,   dump_table_memberref },
+       { "--param",       MONO_TABLE_PARAM,       dump_table_param },
+       { "--typedef",     MONO_TABLE_TYPEDEF,     dump_table_typedef },
+       { "--typeref",     MONO_TABLE_TYPEREF,     dump_table_typeref },
+       { "--exported",    MONO_TABLE_EXPORTEDTYPE,     dump_table_exported },
+       { "--nested",      MONO_TABLE_NESTEDCLASS, dump_table_nestedclass },
+       { "--interface",   MONO_TABLE_INTERFACEIMPL,     dump_table_interfaceimpl },
+       { "--classlayout", MONO_TABLE_CLASSLAYOUT, dump_table_class_layout },
+       { "--constant",    MONO_TABLE_CONSTANT,    dump_table_constant },
+       { "--customattr",  MONO_TABLE_CUSTOMATTRIBUTE,    dump_table_customattr },
+       { "--property",    MONO_TABLE_PROPERTY,    dump_table_property },
+       { "--propertymap", MONO_TABLE_PROPERTYMAP, dump_table_property_map },
+       { "--event",       MONO_TABLE_EVENT,       dump_table_event },
+       { "--file",        MONO_TABLE_FILE,        dump_table_file },
+       { "--moduleref",   MONO_TABLE_MODULEREF,   dump_table_moduleref },
+       { "--module",      MONO_TABLE_MODULE,      dump_table_module },
+       { "--method",      MONO_TABLE_METHOD,      dump_table_method },
+       { "--methodimpl",  MONO_TABLE_METHODIMPL,  dump_table_methodimpl },
+       { "--methodsem",   MONO_TABLE_METHODSEMANTICS,      dump_table_methodsem },
+       { "--manifest",    MONO_TABLE_MANIFESTRESOURCE,     dump_table_manifest },
        { NULL, -1 }
 };
 
@@ -594,12 +905,9 @@ struct {
 static void
 disassemble_file (const char *file)
 {
-       enum MonoAssemblyOpenStatus status;
        MonoAssembly *ass;
-       cli_image_info_t *ii;
-       metadata_t *m;
-
-       fprintf (output, "// Disassembling %s\n", file);
+       MonoImageOpenStatus status;
+       MonoImage *img;
 
        ass = mono_assembly_open (file, &status);
        if (ass == NULL){
@@ -607,26 +915,37 @@ disassemble_file (const char *file)
                return;
        }
 
-       ii = ass->image_info;
-       m = &ii->cli_metadata;
-       
+       img = ass->image;
+
        if (dump_table != -1){
-               (*table_list [dump_table].dumper) (m);
+               (*table_list [dump_table].dumper) (img);
        } else {
-               dump_header_data (ass);
+               dump_header_data (img);
                
-               dis_directive_assemblyref (m);
-               dis_directive_assembly (m);
-               dis_types (m, ii);
+               dis_directive_assemblyref (img);
+               dis_directive_assembly (img);
+               dis_types (img);
+               dis_data (img);
        }
        
-       mono_assembly_close (ass);
+       mono_image_close (img);
 }
 
 static void
 usage (void)
 {
-       fprintf (stderr, "Usage is: monodis [--typeref][--typedef][--assemblyref][--param][--fields][--memberref] file ..\n");
+       GString *args = g_string_new ("[--output=filename] [--help] [--mscorlib] ");
+       int i;
+       
+       for (i = 0; table_list [i].name != NULL; i++){
+               g_string_append (args, "[");
+               g_string_append (args, table_list [i].name);
+               g_string_append (args, "] ");
+               if (((i-2) % 5) == 0)
+                       g_string_append_c (args, '\n');
+       }
+       fprintf (stderr,
+                "Usage is: monodis %s file ..\n", args->str);
        exit (1);
 }
 
@@ -643,11 +962,25 @@ main (int argc, char *argv [])
                                usage ();
                        else if (argv [i][1] == 'd')
                                dump_header_data_p = TRUE;
-                       else if (strcmp (argv [i], "--help") == 0)
+                       else if (strcmp (argv [i], "--mscorlib") == 0) {
+                               substitute_with_mscorlib_p = TRUE;
+                               continue;
+                       } else if (strncmp (argv [i], "--output=", 9) == 0) {
+                               output = fopen (argv [i]+9, "w");
+                               if (output == NULL) {
+                                       fprintf (stderr, "Can't open output file `%s': %s\n",
+                                                argv [i]+9, strerror (errno));
+                                       exit (1);
+                               }
+                               continue;
+                       } else if (strcmp (argv [i], "--help") == 0)
                                usage ();
-                       for (j = 0; table_list [j].name != NULL; j++)
+                       for (j = 0; table_list [j].name != NULL; j++) {
                                if (strcmp (argv [i], table_list [j].name) == 0)
                                        dump_table = j;
+                       }
+                       if (dump_table < 0)
+                               usage ();
                } else
                        input_files = g_list_append (input_files, argv [i]);
        }
@@ -655,6 +988,8 @@ main (int argc, char *argv [])
        if (input_files == NULL)
                usage ();
        
+       mono_init (argv [0]);
+
        for (l = input_files; l; l = l->next)
                disassemble_file (l->data);