More work, Dump more tables, Documentation Update
authorMiguel de Icaza <miguel@gnome.org>
Wed, 4 Jul 2001 23:41:47 +0000 (23:41 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Wed, 4 Jul 2001 23:41:47 +0000 (23:41 -0000)
svn path=/trunk/mono/; revision=51

doc/contributing
mono/dis/ChangeLog
mono/dis/dump.c
mono/dis/dump.h
mono/dis/get.c
mono/dis/get.h
mono/dis/main.c
mono/metadata/ChangeLog
mono/metadata/metadata.h
web/contributing

index f826e7bc3250fc1e156ef6c0d3d8c71a4df35da5..ca4d53e8f0f487dbc43b6a19da19c221802b05d9 100644 (file)
@@ -3,3 +3,11 @@
        Links to Documentation.
        Links to Class Library.
        Links to ...
+
+* Special note
+
+       If you have looked at Microsoft's implementation of .NET or
+       their shared source code, you will not be able to contribute
+       to Mono.
+
+       Please, follow care when reading code
index 6ebaa6b868c005283d37d4ae005599b4d7ef9c9c..42269edf3f2aa5f5cbe7de3bf487078d514fff35 100644 (file)
@@ -1,3 +1,10 @@
+2001-07-04  Miguel de Icaza  <miguel@ximian.com>
+
+       
+       * get.c (get_methodref_signature): Implement.
+
+       * dump.c (dump_table_memberref): Add memberref dumping.
+
 Tue,  3 Jul 2001 18:32:10 +0200 Paolo Molaro <lupus@ximian.com>
 
        * main.c: return on failure.
index 89644d34d663e24380541bbb0c831e5b78b965d8..2e0829c77bdee00d87930d702f909d29c1ed5fdc 100644 (file)
 #include "dump.h"
 #include "get.h"
 
+void
+dump_table_assembly (metadata_t *m)
+{
+       metadata_tableinfo_t *t = &m->tables [META_TABLE_ASSEMBLY];
+       guint32 cols [9];
+       const char *ptr;
+       int len;
+
+       expand (t, 0, cols, CSIZE (cols));
+       fprintf (output, "Assembly Table\n");
+
+       fprintf (output, "Name:          %s\n", mono_metadata_string_heap (m, cols [7]));
+       fprintf (output, "Hash Algoritm: 0x%08x\n", cols [0]);
+       fprintf (output, "Version:       %d.%d.%d.%d\n", cols [1], cols [2], cols [3], cols [4]);
+       fprintf (output, "Flags:         0x%08x\n", cols [5]);
+       fprintf (output, "PublicKey:     BlobPtr (0x%08x)\n", cols [6]);
+
+       ptr = mono_metadata_blob_heap (m, cols [6]);
+       ptr = get_encoded_value (ptr, &len);
+       if (len > 0){
+               fprintf (output, "\tDump:");
+               hex_dump (ptr, 0, len);
+               fprintf (output, "\n");
+       } else
+               fprintf (output, "\tZero sized public key\n");
+       
+       fprintf (output, "Culture:       %s\n", mono_metadata_string_heap (m, cols [8]));
+       fprintf (output, "\n");
+}
+
 void
 dump_table_typeref (metadata_t *m)
 {
@@ -58,12 +88,23 @@ dump_table_assemblyref (metadata_t *m)
        fprintf (output, "AssemblyRef Table\n");
        
        for (i = 0; i < t->rows; i++){
+               const char *ptr;
+               int len;
                guint32 cols [9];
 
                expand (t, i, cols, CSIZE (cols));
-               fprintf (output, "%d: %d.%d.%d.%d %s\n", i,
+               fprintf (output, "%d: Version=%d.%d.%d.%d\n\tName=%s\n", i,
                         cols [0], cols [1], cols [2], cols [3],
                         mono_metadata_string_heap (m, cols [6]));
+               ptr = mono_metadata_blob_heap (m, cols [6]);
+               ptr = get_encoded_value (ptr, &len);
+               if (len > 0){
+                       fprintf (output, "\tPublic Key:");
+                       hex_dump (ptr, 0, len);
+                       fprintf (output, "\n");
+               } else
+                       fprintf (output, "\tZero sized public key\n");
+               
        }
        fprintf (output, "\n");
 }
@@ -107,3 +148,88 @@ dump_table_field (metadata_t *m)
        }
        fprintf (output, "\n");
 }
+
+void
+dump_table_memberref (metadata_t *m)
+{
+       metadata_tableinfo_t *t = &m->tables [META_TABLE_MEMBERREF];
+       int i, kind, idx;
+       char *ks, *x, *xx;
+
+       fprintf (output, "MemberRef Table (0..%d)\n", t->rows);
+
+       for (i = 0; i < t->rows; i++){
+               guint32 cols [3];
+
+               expand (t, i, cols, CSIZE (cols));
+               
+               kind = cols [0] & 7;
+               idx = cols [0] >> 3;
+
+               x = g_strdup ("UNHANDLED CASE");
+               
+               switch (kind){
+               case 0:
+                       ks = "TypeDef"; break;
+               case 1:
+                       ks = "TypeRef";
+                       xx = get_typeref (m, idx);
+                       x = g_strconcat (xx, ".", mono_metadata_string_heap (m, cols [1]), NULL);
+                       g_free (xx);
+                       break;
+               case 2:
+                       ks = "ModuleRef"; break;
+               case 3:
+                       ks = "MethodDef"; break;
+               case 4:
+                       ks = "TypeSpec"; break;
+               default:
+                       g_error ("Unknown tag: %d\n", kind);
+               }
+               
+               fprintf (output, "%d: %s[%d] %s\n\tResolved: %s\n\tSignature: %s\n\t\n",
+                        i,
+                        ks, idx,
+                        mono_metadata_string_heap (m, cols [1]),
+                        x ? x : "",
+                        get_methodref_signature (m, cols [2]));
+
+               if (x)
+                       g_free (x);
+       }
+}
+
+void
+dump_table_class_layout (metadata_t *m)
+{
+       metadata_tableinfo_t *t = &m->tables [META_TABLE_CLASSLAYOUT];
+       int i;
+       fprintf (output, "ClassLayout Table (0..%d)\n", t->rows);
+
+       for (i = 0; i < t->rows; i++){
+               guint32 cols [3];
+               
+               expand (t, i, cols, CSIZE (cols));
+
+               fprintf (stderr, "%d: PackingSize=%d  ClassSize=%d  Parent=%s\n",
+                        i, cols [0], cols [1], get_typedef (m, cols [2]));
+       }
+}
+
+void
+dump_table_constant (metadata_t *m)
+{
+       metadata_tableinfo_t *t = &m->tables [META_TABLE_CONSTANT];
+       int i;
+       fprintf (output, "Constant Table (0..%d)\n", t->rows);
+
+       for (i = 0; i < t->rows; i++){
+               guint32 cols [4];
+               
+               expand (t, i, cols, CSIZE (cols));
+
+               fprintf (stderr, "%d: Parent=0x%08x %s\n",
+                        i, cols [2], get_constant (m, (ElementTypeEnum) cols [0], cols [3]));
+       }
+       
+}
index f34a4bc4f8551bc5f8dfa0399648c2ba7de79bfb..c3d6a9d598e7e0559e4b3a1469a26f8ae86aecb4 100644 (file)
@@ -1,7 +1,11 @@
 extern FILE *output;
 
-void dump_table_typeref     (metadata_t *m);
-void dump_table_typedef     (metadata_t *m);
-void dump_table_assemblyref (metadata_t *m);
-void dump_table_param       (metadata_t *m);
-void dump_table_field       (metadata_t *m);
+void dump_table_assembly     (metadata_t *m);
+void dump_table_assemblyref  (metadata_t *m);
+void dump_table_class_layout (metadata_t *m);
+void dump_table_constant     (metadata_t *m);
+void dump_table_field        (metadata_t *m);
+void dump_table_memberref    (metadata_t *m);
+void dump_table_param        (metadata_t *m);
+void dump_table_typedef      (metadata_t *m);
+void dump_table_typeref      (metadata_t *m);
index 251e304e9272be57524b7305770002b250ac3df6..7325b2d71b3cfea487f83115faf1e3d883343675 100644 (file)
@@ -410,6 +410,27 @@ get_field_signature (metadata_t *m, guint32 blob_signature)
        return res;
 }
 
+ElementTypeEnum
+get_field_literal_type (metadata_t *m, guint32 blob_signature)
+{
+       const char *ptr = mono_metadata_blob_heap (m, blob_signature);
+       int len;
+       char *allocated_modifier_string;
+       
+       ptr = get_encoded_value (ptr, &len);
+
+       /* FIELD is 0x06 */
+       g_assert (*ptr == 0x06);
+       ptr++; len--;
+       
+       ptr = get_custom_mod (m, ptr, &allocated_modifier_string);
+       if (allocated_modifier_string)
+               g_free (allocated_modifier_string);
+
+       return (ElementTypeEnum) *ptr;
+       
+}
+
 /**
  * decode_literal:
  * @m: metadata context
@@ -597,3 +618,141 @@ get_blob_encoded_size (const char *xptr, int *size)
 
        return (char *) ptr;
 }
+
+/**
+ * Returns a stringifed representation of a MethodRefSig (22.2.2)
+ */
+char *
+get_methodref_signature (metadata_t *m, guint32 blob_signature)
+{
+       GString *res = g_string_new ("");
+       const char *ptr = mono_metadata_blob_heap (m, blob_signature);
+       char *allocated_ret_type, *s;
+       gboolean seen_vararg = 0;
+       int param_count, signature_len;
+       int i;
+       
+       ptr = get_encoded_value (ptr, &signature_len);
+
+       if (*ptr & 0x20){
+               if (*ptr & 0x40)
+                       g_string_append (res, "explicit-this ");
+               else
+                       g_string_append (res, "has-this ");
+       }
+
+       if (*ptr & 0x05)
+               seen_vararg = 1;
+
+       ptr++;
+       ptr = get_encoded_value (ptr, &param_count);
+       ptr = get_ret_type (m, ptr, &allocated_ret_type);
+
+       g_string_append (res, allocated_ret_type);
+       g_string_append (res, " (");
+       
+       /*
+        * param_count describes parameters *before* and *after*
+        * the vararg sentinel
+        */
+       for (i = 0; i < param_count; i++){
+               char *param = NULL;
+               
+               /*
+                * If ptr is a SENTINEL
+                */
+               if (*ptr == 0x41){
+                       g_string_append (res, " varargs ");
+                       continue;
+               }
+
+               ptr = get_param (m, ptr, &param);
+               g_string_append (res, param);
+               if (i+1 != param_count)
+                       g_string_append (res, ", ");
+               g_free (param);
+       }
+       g_string_append (res, ")");
+       
+       /*
+        * cleanup and return
+        */
+       g_free (allocated_ret_type);
+       s = res->str;
+       g_string_free (res, FALSE);
+       return s;
+}
+
+/**
+ * get_constant:
+ * @m: metadata context
+ * @blob_index: index into the blob where the constant is stored
+ *
+ * Returns: An allocated value representing a stringified version of the
+ * constant.
+ */
+char *
+get_constant (metadata_t *m, ElementTypeEnum t, guint32 blob_index)
+{
+       const char *ptr = mono_metadata_blob_heap (m, blob_index);
+       int len;
+       
+       ptr = get_encoded_value (ptr, &len);
+       
+       switch (t){
+       case ELEMENT_TYPE_BOOLEAN:
+               return g_strdup_printf ("%s", *ptr ? "true" : "false");
+               
+       case ELEMENT_TYPE_CHAR:
+               return g_strdup_printf ("%c", *ptr);
+               
+       case ELEMENT_TYPE_U1:
+               return g_strdup_printf ("0x%02x", (int) (*ptr));
+               break;
+               
+       case ELEMENT_TYPE_I2:
+               return g_strdup_printf ("%d", (int) (*(gint16 *) ptr));
+               
+       case ELEMENT_TYPE_I4:
+               return g_strdup_printf ("%d", *(gint32 *) ptr);
+               
+       case ELEMENT_TYPE_I8:
+               /*
+                * FIXME: This is not endian portable, does only 
+                * matter for debugging, but still.
+                */
+               return g_strdup_printf ("0x%08x%08x", *(guint32 *) ptr, *(guint32 *) (ptr + 4));
+               
+       case ELEMENT_TYPE_U8:
+               return g_strdup_printf ("0x%08x%08x", *(guint32 *) ptr, *(guint32 *) (ptr + 4));                
+       case ELEMENT_TYPE_R4:
+               return g_strdup_printf ("%g", (double) (* (float *) ptr));
+               
+       case ELEMENT_TYPE_R8:
+               return g_strdup_printf ("%g", * (double *) ptr);
+               
+       case ELEMENT_TYPE_STRING:
+               return "FIXME: Decode string constants!";
+               
+       case ELEMENT_TYPE_CLASS:
+               return g_strdup ("CLASS CONSTANT.  MUST BE ZERO");
+               
+               /*
+                * These are non CLS compliant:
+                */
+       case ELEMENT_TYPE_I1:
+               return g_strdup_printf ("%d", (int) *ptr);
+
+       case ELEMENT_TYPE_U2:
+               return g_strdup_printf ("0x%04x", (unsigned int) (*(guint16 *) ptr));
+               
+       case ELEMENT_TYPE_U4:
+               return g_strdup_printf ("0x%04x", (unsigned int) (*(guint32 *) ptr));
+               
+       default:
+               g_error ("Unknown ELEMENT_TYPE (%d) on constant at Blob index (0x%08x)\n",
+                        (int) *ptr, blob_index);
+               return g_strdup_printf ("Unknown");
+       }
+
+}
index 78e50181398589644662058cdcb7ddcebf84fa5b..d8becb8d1d4aaf5b9c6995709cb1f433d3813ff3 100644 (file)
@@ -2,15 +2,18 @@
 /*
  * These return allocated strings
  */
-char *get_typedef         (metadata_t *m, int idx);
-char *get_module          (metadata_t *m, int idx);
-char *get_assemblyref     (metadata_t *m, int idx);
-char *get_typeref         (metadata_t *m, int idx);
-char *get_typedef_or_ref  (metadata_t *m, guint32 dor_token);
-char *get_field_signature (metadata_t *m, guint32 blob_signature);
-char *decode_literal      (metadata_t *m, guint32 token);
-char *param_flags         (guint32 f);
-char *field_flags         (guint32 f);
+char *get_typedef             (metadata_t *m, int idx);
+char *get_module              (metadata_t *m, int idx);
+char *get_assemblyref         (metadata_t *m, int idx);
+char *get_typeref             (metadata_t *m, int idx);
+char *get_typedef_or_ref      (metadata_t *m, guint32 dor_token);
+char *get_field_signature     (metadata_t *m, guint32 blob_signature);
+char *decode_literal          (metadata_t *m, guint32 token);
+char *param_flags             (guint32 f);
+char *field_flags             (guint32 f);
+char *get_methodref_signature (metadata_t *m, guint32 blob_signature);
+char *get_constant            (metadata_t *m, ElementTypeEnum t, guint32 blob_index);
+
 
 /*
  * These functions are used during the decoding of streams in the
@@ -33,7 +36,9 @@ const char *get_ret_type               (metadata_t *m, const char *ptr,
                                        char **ret_type);
 const char *get_param                  (metadata_t *m, const char *ptr,
                                        char **retval);
-
 const char *get_blob_encoded_size      (const char *ptr, int *size);
 
 void expand (metadata_tableinfo_t *t, int idx, guint32 *res, int res_size);
+
+
+ElementTypeEnum get_field_literal_type (metadata_t *m, guint32 blob_signature);
index 6da2b25a3cd5fe0c3c3c72d76c7e2b3398356cbe..03e15a398d114d6bfa72bde9c1fd536bf08363ee 100644 (file)
@@ -9,8 +9,8 @@
  * TODO:
  *   Investigate how interface inheritance works and how it should be dumped.
  *   Structs are not being labeled as `valuetype' classes
- *   Support CustomMods.
- *
+ *   
+ *   How are fields with literals mapped to constants?
  */
 #include <config.h>
 #include <stdio.h>
@@ -175,7 +175,12 @@ dis_field_list (metadata_t *m, guint32 start, guint32 end)
                flags = field_flags (cols [0]);
                
                if (cols [0] & FIELD_ATTRIBUTE_LITERAL){
-                       char *lit = decode_literal (m, cols [2]);
+                       ElementTypeEnum type;
+                       char *lit;
+                       
+                       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]); */
                        
                        fprintf (output, "    .field %s %s %s = ",
                                 flags, sig,
@@ -509,6 +514,23 @@ dis_types (metadata_t *m, cli_image_info_t *ii)
                dis_type (m, ii, i);
 }
 
+struct {
+       char *name;
+       int table;
+       void (*dumper) (metadata_t *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 },
+       { NULL, -1 }
+};
+
 /**
  * disassemble_file:
  * @file: file containing CIL code.
@@ -534,25 +556,7 @@ disassemble_file (const char *file)
        m = &ii->cli_metadata;
        
        if (dump_table != -1){
-               switch (dump_table){
-               case META_TABLE_TYPEDEF:
-                       dump_table_typedef (m);
-                       break;
-               case META_TABLE_TYPEREF:
-                       dump_table_typeref (m);
-                       break;
-               case META_TABLE_ASSEMBLYREF:
-                       dump_table_assemblyref (m);
-                       break;
-               case META_TABLE_PARAM:
-                       dump_table_param (m);
-                       break;
-               case META_TABLE_FIELD:
-                       dump_table_field (m);
-                       break;
-               default:
-                       g_error ("Internal error");
-               }
+               (*table_list [dump_table].dumper) (m);
        } else {
                dump_header_data (ass);
                
@@ -567,7 +571,7 @@ disassemble_file (const char *file)
 static void
 usage (void)
 {
-       fprintf (stderr, "Usage is: monodis [--typeref][--typedef][--assemblyref] file ..\n");
+       fprintf (stderr, "Usage is: monodis [--typeref][--typedef][--assemblyref][--param][--fields][--memberref] file ..\n");
        exit (1);
 }
 
@@ -575,7 +579,7 @@ int
 main (int argc, char *argv [])
 {
        GList *input_files = NULL, *l;
-       int i;
+       int i, j;
 
        output = stdout;
        for (i = 1; i < argc; i++){
@@ -586,16 +590,9 @@ main (int argc, char *argv [])
                                dump_header_data_p = TRUE;
                        else if (strcmp (argv [i], "--help") == 0)
                                usage ();
-                       else if (strcmp (argv [i], "--typeref") == 0)
-                               dump_table = META_TABLE_TYPEREF;
-                       else if (strcmp (argv [i], "--typedef") == 0)
-                               dump_table = META_TABLE_TYPEDEF;
-                       else if (strcmp (argv [i], "--assemblyref") == 0)
-                               dump_table = META_TABLE_ASSEMBLYREF;
-                       else if (strcmp (argv [i], "--param") == 0)
-                               dump_table = META_TABLE_PARAM;
-                       else if (strcmp (argv [i], "--fields") == 0)
-                               dump_table = META_TABLE_FIELD;
+                       for (j = 0; table_list [j].name != NULL; j++)
+                               if (strcmp (argv [i], table_list [j].name) == 0)
+                                       dump_table = j;
                } else
                        input_files = g_list_append (input_files, argv [i]);
        }
index 4855e323c1c1e75a9b8c4a858751c0d187253eb7..a95367676d688267e21de4707a0509af56b6545f 100644 (file)
@@ -1,3 +1,7 @@
+2001-07-04  Miguel de Icaza  <miguel@ximian.com>
+
+       * metadata.h: Add a couple of macros to manipulate tokens. 
+
 Tue Jul  3 18:33:32 CEST 2001 Paolo Molaro <lupus@ximian.com>
 
        * assembly.c: g_free(ii->cli_sections) (and avoid double free of
index c2d8af6c3367834e4dcdd361f932ca8c461e15eb..1215556454a88d7857b081fb29743cb0d42f87d8 100644 (file)
@@ -202,3 +202,26 @@ typedef struct {
 
 MonoMetaMethodHeader *mono_metadata_parse_mh (const char *ptr);
 void                  mono_metadata_free_mh  (MonoMetaMethodHeader *mh);
+
+/*
+ * Makes a token based on a table and an index
+ */
+#define mono_metadata_make_token(table,idx) (((table) << 24)| idx)
+
+/*
+ * Returns the table index that this token encodes.
+ */
+#define mono_metadata_token_table(token) ((token) >> 24)
+
+/*
+ * Returns the index that a token refers to
+ */
+#define mono_metadata_token_index(token) ((token & 0xffffff))
+
+
+/*
+ * FIXME: put all of the table codes here
+ */
+enum {
+       TOKEN_TABLE_XXX = 0
+} MonoMetadataTableCodes;
index f826e7bc3250fc1e156ef6c0d3d8c71a4df35da5..ca4d53e8f0f487dbc43b6a19da19c221802b05d9 100644 (file)
@@ -3,3 +3,11 @@
        Links to Documentation.
        Links to Class Library.
        Links to ...
+
+* Special note
+
+       If you have looked at Microsoft's implementation of .NET or
+       their shared source code, you will not be able to contribute
+       to Mono.
+
+       Please, follow care when reading code