2006-08-31 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / metadata / metadata.c
index d908256b6d6511430ab84e5953adae414ea81519..8fabf0b78235bab1cbadb9d9f53b9ae410e58ee6 100644 (file)
@@ -27,11 +27,10 @@ static gboolean do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoG
 
 static gboolean do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only);
 static gboolean mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only);
+static gboolean mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only);
 static gboolean _mono_metadata_generic_class_equal (const MonoGenericClass *g1, const MonoGenericClass *g2,
                                                    gboolean signature_only);
 
-GHashTable *mono_generic_params_with_ambiguous_names;
-
 /*
  * This enumeration is used to describe the data types in the metadata
  * tables
@@ -93,7 +92,8 @@ enum {
        MONO_MT_RS_IDX
 };
 
-const static unsigned char AssemblySchema [] = {
+const static unsigned char TableSchemas [] = {
+#define ASSEMBLY_SCHEMA_OFFSET 0
        MONO_MT_UINT32,     /* "HashId" }, */
        MONO_MT_UINT16,     /* "Major" },  */
        MONO_MT_UINT16,     /* "Minor" }, */
@@ -103,22 +103,19 @@ const static unsigned char AssemblySchema [] = {
        MONO_MT_BLOB_IDX,   /* "PublicKey" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_STRING_IDX, /* "Culture" }, */
-       MONO_MT_END
-};
-       
-const static unsigned char AssemblyOSSchema [] = {
+       MONO_MT_END,
+
+#define ASSEMBLYOS_SCHEMA_OFFSET ASSEMBLY_SCHEMA_OFFSET + 10
        MONO_MT_UINT32,     /* "OSPlatformID" }, */
        MONO_MT_UINT32,     /* "OSMajor" }, */
        MONO_MT_UINT32,     /* "OSMinor" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char AssemblyProcessorSchema [] = {
+#define ASSEMBLYPROC_SCHEMA_OFFSET ASSEMBLYOS_SCHEMA_OFFSET + 4
        MONO_MT_UINT32,     /* "Processor" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char AssemblyRefSchema [] = {
+#define ASSEMBLYREF_SCHEMA_OFFSET ASSEMBLYPROC_SCHEMA_OFFSET + 2
        MONO_MT_UINT16,     /* "Major" }, */
        MONO_MT_UINT16,     /* "Minor" }, */
        MONO_MT_UINT16,     /* "Build" }, */
@@ -128,296 +125,293 @@ const static unsigned char AssemblyRefSchema [] = {
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_STRING_IDX, /* "Culture" }, */
        MONO_MT_BLOB_IDX,   /* "HashValue" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char AssemblyRefOSSchema [] = {
+#define ASSEMBLYREFOS_SCHEMA_OFFSET ASSEMBLYREF_SCHEMA_OFFSET + 10
        MONO_MT_UINT32,     /* "OSPlatformID" }, */
        MONO_MT_UINT32,     /* "OSMajorVersion" }, */
        MONO_MT_UINT32,     /* "OSMinorVersion" }, */
        MONO_MT_TABLE_IDX,  /* "AssemblyRef:AssemblyRef" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char AssemblyRefProcessorSchema [] = {
+#define ASSEMBLYREFPROC_SCHEMA_OFFSET ASSEMBLYREFOS_SCHEMA_OFFSET + 5
        MONO_MT_UINT32,     /* "Processor" }, */
        MONO_MT_TABLE_IDX,  /* "AssemblyRef:AssemblyRef" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char ClassLayoutSchema [] = {
+#define CLASS_LAYOUT_SCHEMA_OFFSET ASSEMBLYREFPROC_SCHEMA_OFFSET + 3
        MONO_MT_UINT16,     /* "PackingSize" }, */
        MONO_MT_UINT32,     /* "ClassSize" }, */
        MONO_MT_TABLE_IDX,  /* "Parent:TypeDef" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char ConstantSchema [] = {
+#define CONSTANT_SCHEMA_OFFSET CLASS_LAYOUT_SCHEMA_OFFSET + 4
        MONO_MT_UINT8,      /* "Type" }, */
        MONO_MT_UINT8,      /* "PaddingZero" }, */
        MONO_MT_CONST_IDX,  /* "Parent" }, */
        MONO_MT_BLOB_IDX,   /* "Value" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char CustomAttributeSchema [] = {
+#define CUSTOM_ATTR_SCHEMA_OFFSET CONSTANT_SCHEMA_OFFSET + 5
        MONO_MT_HASCAT_IDX, /* "Parent" }, */
        MONO_MT_CAT_IDX,    /* "Type" }, */
        MONO_MT_BLOB_IDX,   /* "Value" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char DeclSecuritySchema [] = {
+#define DECL_SEC_SCHEMA_OFFSET CUSTOM_ATTR_SCHEMA_OFFSET + 4
        MONO_MT_UINT16,     /* "Action" }, */
        MONO_MT_HASDEC_IDX, /* "Parent" }, */
        MONO_MT_BLOB_IDX,   /* "PermissionSet" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char EventMapSchema [] = {
+#define EVENTMAP_SCHEMA_OFFSET DECL_SEC_SCHEMA_OFFSET + 4
        MONO_MT_TABLE_IDX,  /* "Parent:TypeDef" }, */
        MONO_MT_TABLE_IDX,  /* "EventList:Event" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char EventSchema [] = {
+#define EVENT_SCHEMA_OFFSET EVENTMAP_SCHEMA_OFFSET + 3
        MONO_MT_UINT16,     /* "EventFlags#EventAttribute" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_TABLE_IDX,  /* "EventType" }, TypeDef or TypeRef  */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char ExportedTypeSchema [] = {
+#define EXPORTED_TYPE_SCHEMA_OFFSET EVENT_SCHEMA_OFFSET + 4
        MONO_MT_UINT32,     /* "Flags" }, */
        MONO_MT_TABLE_IDX,  /* "TypeDefId" }, */
        MONO_MT_STRING_IDX, /* "TypeName" }, */
        MONO_MT_STRING_IDX, /* "TypeNameSpace" }, */
        MONO_MT_IMPL_IDX,   /* "Implementation" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char FieldSchema [] = {
+#define FIELD_SCHEMA_OFFSET EXPORTED_TYPE_SCHEMA_OFFSET + 6
        MONO_MT_UINT16,     /* "Flags" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_BLOB_IDX,   /* "Signature" }, */
-       MONO_MT_END
-};
-const static unsigned char FieldLayoutSchema [] = {
+       MONO_MT_END,
+
+#define FIELD_LAYOUT_SCHEMA_OFFSET FIELD_SCHEMA_OFFSET + 4
        MONO_MT_UINT32,     /* "Offset" }, */
        MONO_MT_TABLE_IDX,  /* "Field:Field" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char FieldMarshalSchema [] = {
+#define FIELD_MARSHAL_SCHEMA_OFFSET FIELD_LAYOUT_SCHEMA_OFFSET + 3
        MONO_MT_HFM_IDX,    /* "Parent" }, */
        MONO_MT_BLOB_IDX,   /* "NativeType" }, */
-       MONO_MT_END
-};
-const static unsigned char FieldRVASchema [] = {
+       MONO_MT_END,
+
+#define FIELD_RVA_SCHEMA_OFFSET FIELD_MARSHAL_SCHEMA_OFFSET + 3
        MONO_MT_UINT32,     /* "RVA" }, */
        MONO_MT_TABLE_IDX,  /* "Field:Field" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char FileSchema [] = {
+#define FILE_SCHEMA_OFFSET FIELD_RVA_SCHEMA_OFFSET + 3
        MONO_MT_UINT32,     /* "Flags" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_BLOB_IDX,   /* "Value" },  */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char ImplMapSchema [] = {
+#define IMPLMAP_SCHEMA_OFFSET FILE_SCHEMA_OFFSET + 4
        MONO_MT_UINT16,     /* "MappingFlag" }, */
        MONO_MT_MF_IDX,     /* "MemberForwarded" }, */
        MONO_MT_STRING_IDX, /* "ImportName" }, */
        MONO_MT_TABLE_IDX,  /* "ImportScope:ModuleRef" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char InterfaceImplSchema [] = {
+#define IFACEMAP_SCHEMA_OFFSET IMPLMAP_SCHEMA_OFFSET + 5
        MONO_MT_TABLE_IDX,  /* "Class:TypeDef" },  */
        MONO_MT_TDOR_IDX,  /* "Interface=TypeDefOrRef" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char ManifestResourceSchema [] = {
+#define MANIFEST_SCHEMA_OFFSET IFACEMAP_SCHEMA_OFFSET + 3
        MONO_MT_UINT32,     /* "Offset" }, */
        MONO_MT_UINT32,     /* "Flags" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_IMPL_IDX,   /* "Implementation" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char MemberRefSchema [] = {
+#define MEMBERREF_SCHEMA_OFFSET MANIFEST_SCHEMA_OFFSET + 5
        MONO_MT_MRP_IDX,    /* "Class" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_BLOB_IDX,   /* "Signature" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char MethodSchema [] = {
+#define METHOD_SCHEMA_OFFSET MEMBERREF_SCHEMA_OFFSET + 4
        MONO_MT_UINT32,     /* "RVA" }, */
        MONO_MT_UINT16,     /* "ImplFlags#MethodImplAttributes" }, */
        MONO_MT_UINT16,     /* "Flags#MethodAttribute" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_BLOB_IDX,   /* "Signature" }, */
        MONO_MT_TABLE_IDX,  /* "ParamList:Param" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char MethodImplSchema [] = {
+#define METHOD_IMPL_SCHEMA_OFFSET METHOD_SCHEMA_OFFSET + 7
        MONO_MT_TABLE_IDX,  /* "Class:TypeDef" }, */
        MONO_MT_MDOR_IDX,   /* "MethodBody" }, */
        MONO_MT_MDOR_IDX,   /* "MethodDeclaration" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char MethodSemanticsSchema [] = {
+#define METHOD_SEMA_SCHEMA_OFFSET METHOD_IMPL_SCHEMA_OFFSET + 4
        MONO_MT_UINT16,     /* "MethodSemantic" }, */
        MONO_MT_TABLE_IDX,  /* "Method:Method" }, */
        MONO_MT_HS_IDX,     /* "Association" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char ModuleSchema [] = {
+#define MODULE_SCHEMA_OFFSET METHOD_SEMA_SCHEMA_OFFSET + 4
        MONO_MT_UINT16,     /* "Generation" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_GUID_IDX,   /* "MVID" }, */
        MONO_MT_GUID_IDX,   /* "EncID" }, */
        MONO_MT_GUID_IDX,   /* "EncBaseID" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char ModuleRefSchema [] = {
+#define MODULEREF_SCHEMA_OFFSET MODULE_SCHEMA_OFFSET + 6
        MONO_MT_STRING_IDX, /* "Name" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char NestedClassSchema [] = {
+#define NESTED_CLASS_SCHEMA_OFFSET MODULEREF_SCHEMA_OFFSET + 2
        MONO_MT_TABLE_IDX,  /* "NestedClass:TypeDef" }, */
        MONO_MT_TABLE_IDX,  /* "EnclosingClass:TypeDef" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char ParamSchema [] = {
+#define PARAM_SCHEMA_OFFSET NESTED_CLASS_SCHEMA_OFFSET + 3
        MONO_MT_UINT16,     /* "Flags" }, */
        MONO_MT_UINT16,     /* "Sequence" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char PropertySchema [] = {
+#define PROPERTY_SCHEMA_OFFSET PARAM_SCHEMA_OFFSET + 4
        MONO_MT_UINT16,     /* "Flags" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_BLOB_IDX,   /* "Type" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char PropertyMapSchema [] = {
+#define PROPERTY_MAP_SCHEMA_OFFSET PROPERTY_SCHEMA_OFFSET + 4
        MONO_MT_TABLE_IDX,  /* "Parent:TypeDef" }, */
        MONO_MT_TABLE_IDX,  /* "PropertyList:Property" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char StandaloneSigSchema [] = {
+#define STDALON_SIG_SCHEMA_OFFSET PROPERTY_MAP_SCHEMA_OFFSET + 3
        MONO_MT_BLOB_IDX,   /* "Signature" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char TypeDefSchema [] = {
+#define TYPEDEF_SCHEMA_OFFSET STDALON_SIG_SCHEMA_OFFSET + 2
        MONO_MT_UINT32,     /* "Flags" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_STRING_IDX, /* "Namespace" }, */
        MONO_MT_TDOR_IDX,   /* "Extends" }, */
        MONO_MT_TABLE_IDX,  /* "FieldList:Field" }, */
        MONO_MT_TABLE_IDX,  /* "MethodList:Method" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char TypeRefSchema [] = {
+#define TYPEREF_SCHEMA_OFFSET TYPEDEF_SCHEMA_OFFSET + 7
        MONO_MT_RS_IDX,     /* "ResolutionScope=ResolutionScope" }, */
        MONO_MT_STRING_IDX, /* "Name" }, */
        MONO_MT_STRING_IDX, /* "Namespace" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char TypeSpecSchema [] = {
+#define TYPESPEC_SCHEMA_OFFSET TYPEREF_SCHEMA_OFFSET + 4
        MONO_MT_BLOB_IDX,   /* "Signature" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char GenericParamSchema [] = {
+#define GENPARAM_SCHEMA_OFFSET TYPESPEC_SCHEMA_OFFSET + 2
        MONO_MT_UINT16,     /* "Number" }, */
        MONO_MT_UINT16,     /* "Flags" }, */
        MONO_MT_TABLE_IDX,  /* "Owner" },  TypeDef or MethodDef */
        MONO_MT_STRING_IDX, /* "Name" }, */
+       MONO_MT_END,
 
-       MONO_MT_END
-};
-
-const static unsigned char MethodSpecSchema [] = {
+#define METHOD_SPEC_SCHEMA_OFFSET GENPARAM_SCHEMA_OFFSET + 5
        MONO_MT_MDOR_IDX,   /* "Method" }, */
        MONO_MT_BLOB_IDX,   /* "Signature" }, */
-       MONO_MT_END
-};
+       MONO_MT_END,
 
-const static unsigned char GenericParamConstraintSchema [] = {
+#define GEN_CONSTRAINT_SCHEMA_OFFSET METHOD_SPEC_SCHEMA_OFFSET + 3
        MONO_MT_TABLE_IDX,  /* "GenericParam" }, */
        MONO_MT_TDOR_IDX,   /* "Constraint" }, */
+       MONO_MT_END,
+
+#define NULL_SCHEMA_OFFSET GEN_CONSTRAINT_SCHEMA_OFFSET + 3
        MONO_MT_END
 };
 
-const static struct {
-       const unsigned char *description;
-       const char    *name;
-} tables [] = {
-       /*  0 */ { ModuleSchema,               "Module" },
-       /*  1 */ { TypeRefSchema,              "TypeRef" },
-       /*  2 */ { TypeDefSchema,              "TypeDef" },
-       /*  3 */ { NULL,                       NULL },
-       /*  4 */ { FieldSchema,                "Field" },
-       /*  5 */ { NULL,                       NULL },
-       /*  6 */ { MethodSchema,               "Method" },
-       /*  7 */ { NULL,                       NULL },
-       /*  8 */ { ParamSchema,                "Param" },
-       /*  9 */ { InterfaceImplSchema,        "InterfaceImpl" },
-       /*  A */ { MemberRefSchema,            "MemberRef" },
-       /*  B */ { ConstantSchema,             "Constant" },
-       /*  C */ { CustomAttributeSchema,      "CustomAttribute" },
-       /*  D */ { FieldMarshalSchema,         "FieldMarshal" },
-       /*  E */ { DeclSecuritySchema,         "DeclSecurity" },
-       /*  F */ { ClassLayoutSchema,          "ClassLayout" },
-       /* 10 */ { FieldLayoutSchema,          "FieldLayout" },
-       /* 11 */ { StandaloneSigSchema,        "StandaloneSig" },
-       /* 12 */ { EventMapSchema,             "EventMap" },
-       /* 13 */ { NULL,                       NULL },
-       /* 14 */ { EventSchema,                "Event" },
-       /* 15 */ { PropertyMapSchema,          "PropertyMap" },
-       /* 16 */ { NULL,                       NULL },
-       /* 17 */ { PropertySchema,             "PropertyTable" },
-       /* 18 */ { MethodSemanticsSchema,      "MethodSemantics" },
-       /* 19 */ { MethodImplSchema,           "MethodImpl" },
-       /* 1A */ { ModuleRefSchema,            "ModuleRef" },
-       /* 1B */ { TypeSpecSchema,             "TypeSpec" },
-       /* 1C */ { ImplMapSchema,              "ImplMap" },
-       /* 1D */ { FieldRVASchema,             "FieldRVA" },
-       /* 1E */ { NULL,                       NULL },
-       /* 1F */ { NULL,                       NULL },
-       /* 20 */ { AssemblySchema,             "Assembly" },
-       /* 21 */ { AssemblyProcessorSchema,    "AssemblyProcessor" },
-       /* 22 */ { AssemblyOSSchema,           "AssemblyOS" },
-       /* 23 */ { AssemblyRefSchema,          "AssemblyRef" },
-       /* 24 */ { AssemblyRefProcessorSchema, "AssemblyRefProcessor" },
-       /* 25 */ { AssemblyRefOSSchema,        "AssemblyRefOS" },
-       /* 26 */ { FileSchema,                 "File" },
-       /* 27 */ { ExportedTypeSchema,         "ExportedType" },
-       /* 28 */ { ManifestResourceSchema,     "ManifestResource" },
-       /* 29 */ { NestedClassSchema,          "NestedClass" },
-       /* 2A */ { GenericParamSchema,         "GenericParam" },
-       /* 2B */ { MethodSpecSchema,           "MethodSpec" },
-       /* 2C */ { GenericParamConstraintSchema, "GenericParamConstraint" },
+/* Must be the same order as MONO_TABLE_* */
+const static unsigned char
+table_description [] = {
+       MODULE_SCHEMA_OFFSET,
+       TYPEREF_SCHEMA_OFFSET,
+       TYPEDEF_SCHEMA_OFFSET,
+       NULL_SCHEMA_OFFSET,
+       FIELD_SCHEMA_OFFSET,
+       NULL_SCHEMA_OFFSET,
+       METHOD_SCHEMA_OFFSET,
+       NULL_SCHEMA_OFFSET,
+       PARAM_SCHEMA_OFFSET,
+       IFACEMAP_SCHEMA_OFFSET,
+       MEMBERREF_SCHEMA_OFFSET, /* 0xa */
+       CONSTANT_SCHEMA_OFFSET,
+       CUSTOM_ATTR_SCHEMA_OFFSET,
+       FIELD_MARSHAL_SCHEMA_OFFSET,
+       DECL_SEC_SCHEMA_OFFSET,
+       CLASS_LAYOUT_SCHEMA_OFFSET,
+       FIELD_LAYOUT_SCHEMA_OFFSET, /* 0x10 */
+       STDALON_SIG_SCHEMA_OFFSET,
+       EVENTMAP_SCHEMA_OFFSET,
+       NULL_SCHEMA_OFFSET,
+       EVENT_SCHEMA_OFFSET,
+       PROPERTY_MAP_SCHEMA_OFFSET,
+       NULL_SCHEMA_OFFSET,
+       PROPERTY_SCHEMA_OFFSET,
+       METHOD_SEMA_SCHEMA_OFFSET,
+       METHOD_IMPL_SCHEMA_OFFSET,
+       MODULEREF_SCHEMA_OFFSET, /* 0x1a */
+       TYPESPEC_SCHEMA_OFFSET,
+       IMPLMAP_SCHEMA_OFFSET,
+       FIELD_RVA_SCHEMA_OFFSET,
+       NULL_SCHEMA_OFFSET,
+       NULL_SCHEMA_OFFSET,
+       ASSEMBLY_SCHEMA_OFFSET, /* 0x20 */
+       ASSEMBLYPROC_SCHEMA_OFFSET,
+       ASSEMBLYOS_SCHEMA_OFFSET,
+       ASSEMBLYREF_SCHEMA_OFFSET,
+       ASSEMBLYREFPROC_SCHEMA_OFFSET,
+       ASSEMBLYREFOS_SCHEMA_OFFSET,
+       FILE_SCHEMA_OFFSET,
+       EXPORTED_TYPE_SCHEMA_OFFSET,
+       MANIFEST_SCHEMA_OFFSET,
+       NESTED_CLASS_SCHEMA_OFFSET,
+       GENPARAM_SCHEMA_OFFSET, /* 0x2a */
+       METHOD_SPEC_SCHEMA_OFFSET,
+       GEN_CONSTRAINT_SCHEMA_OFFSET
 };
 
+#ifdef HAVE_ARRAY_ELEM_INIT
+#define MSGSTRFIELD(line) MSGSTRFIELD1(line)
+#define MSGSTRFIELD1(line) str##line
+static const struct msgstr_t {
+#define TABLEDEF(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
+#include "mono/cil/tables.def"
+#undef TABLEDEF
+} tablestr = {
+#define TABLEDEF(a,b) b,
+#include "mono/cil/tables.def"
+#undef TABLEDEF
+};
+static const gint16 tableidx [] = {
+#define TABLEDEF(a,b) [a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
+#include "mono/cil/tables.def"
+#undef TABLEDEF
+};
+
+#else
+#define TABLEDEF(a,b) b,
+static const char* const
+mono_tables_names [] = {
+#include "mono/cil/tables.def"
+       NULL
+};
+
+#endif
+
 /**
  * mono_meta_table_name:
  * @table: table index
@@ -427,10 +421,14 @@ const static struct {
 const char *
 mono_meta_table_name (int table)
 {
-       if ((table < 0) || (table > 0x2c))
+       if ((table < 0) || (table > MONO_TABLE_LAST))
                return "";
-       
-       return tables [table].name;
+
+#ifdef HAVE_ARRAY_ELEM_INIT
+       return (const char*)&tablestr + tableidx [table];
+#else
+       return mono_tables_names [table];
+#endif
 }
 
 /* The guy who wrote the spec for this should not be allowed near a
@@ -467,7 +465,7 @@ mono_metadata_compute_size (MonoImage *meta, int tableindex, guint32 *result_bit
        int size = 0, field_size = 0;
        int i, n, code;
        int shift = 0;
-       const unsigned char *description = tables [tableindex].description;
+       const unsigned char *description = TableSchemas + table_description [tableindex];
 
        for (i = 0; (code = description [i]) != MONO_MT_END; i++){
                switch (code){
@@ -1329,6 +1327,20 @@ mono_metadata_init (void)
                g_hash_table_insert (type_cache, (gpointer) &builtin_types [i], (gpointer) &builtin_types [i]);
 }
 
+/**
+ * mono_metadata_cleanup:
+ *
+ * Free all resources used by this module.
+ * This is a Mono runtime internal function.
+ */
+void
+mono_metadata_cleanup (void)
+{
+       g_hash_table_destroy (type_cache);
+       g_hash_table_destroy (generic_inst_cache);
+       g_hash_table_destroy (generic_class_cache);
+}
+
 /**
  * mono_metadata_parse_type:
  * @m: metadata context
@@ -1350,6 +1362,8 @@ mono_metadata_init (void)
  * this MonoGenericContainer.
  * This is a Mono runtime internal function.
  *
+ * LOCKING: Assumes the loader lock is held.
+ *
  * Returns: a #MonoType structure representing the decoded type.
  */
 MonoType*
@@ -1396,7 +1410,7 @@ mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, Mo
        }
 
        if (count) {
-               type = g_malloc0 (sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
+               type = mono_mempool_alloc0 (m->mempool, sizeof (MonoType) + ((gint32)count - MONO_ZERO_LEN_ARRAY) * sizeof (MonoCustomMod));
                type->num_mods = count;
                if (count > 64)
                        g_warning ("got more than 64 modifiers in type");
@@ -1441,30 +1455,49 @@ mono_metadata_parse_type_full (MonoImage *m, MonoGenericContainer *container, Mo
        if (rptr)
                *rptr = ptr;
 
-       
-       /* FIXME: remove the != MONO_PARSE_PARAM condition, this accounts for
-        * almost 10k (about 2/3rds) of all MonoType's we create.
-        */
-       if (mode != MONO_PARSE_PARAM && !type->num_mods) {
+               if (!type->num_mods) {
                /* no need to free type here, because it is on the stack */
                if ((type->type == MONO_TYPE_CLASS || type->type == MONO_TYPE_VALUETYPE) && !type->pinned && !type->attrs) {
-                       if (type->byref)
-                               return &type->data.klass->this_arg;
-                       else
-                               return &type->data.klass->byval_arg;
+                       MonoType *ret = type->byref ? &type->data.klass->this_arg : &type->data.klass->byval_arg;
+
+                       /* Consider the case:
+
+                            class Foo<T> { class Bar {} }
+                            class Test : Foo<Test>.Bar {}
+
+                          When Foo<Test> is being expanded, 'Test' isn't yet initialized.  It's actually in
+                          a really pristine state: it doesn't even know whether 'Test' is a reference or a value type.
+
+                          We ensure that the MonoClass is in a state that we can canonicalize to:
+
+                            klass->byval_arg.data.klass == klass
+                            klass->this_arg.data.klass == klass
+
+                          If we can't canonicalize 'type', it doesn't matter, since later users of 'type' will do it.
+
+                          LOCKING: even though we don't explicitly hold a lock, in the problematic case 'ret' is a field
+                                   of a MonoClass which currently holds the loader lock.  'type' is local.
+                       */
+                       if (ret->data.klass == type->data.klass)
+                               return ret;
                }
                /* No need to use locking since nobody is modifying the hash table */
                if ((cached = g_hash_table_lookup (type_cache, type)))
                        return cached;
        }
        
-       /*printf ("%x%c %s\n", type->attrs, type->pinned ? 'p' : ' ', mono_type_full_name (type));*/
+       /* printf ("%x %x %c %s\n", type->attrs, type->num_mods, type->pinned ? 'p' : ' ', mono_type_full_name (type)); */
        
-       if (type == &stype)
-               type = g_memdup (&stype, sizeof (MonoType));
+       if (type == &stype) {
+               type = mono_mempool_alloc (m->mempool, sizeof (MonoType));
+               memcpy (type, &stype, sizeof (MonoType));
+       }
        return type;
 }
 
+/*
+ * LOCKING: Assumes the loader lock is held.
+ */
 MonoType*
 mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
                          const char *ptr, const char **rptr)
@@ -1472,6 +1505,39 @@ mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
        return mono_metadata_parse_type_full (m, NULL, mode, opt_attrs, ptr, rptr);
 }
 
+/*
+ * mono_metadata_get_param_attrs:
+ *
+ *   Return the parameter attributes for the method whose MethodDef index is DEF. The 
+ * returned memory needs to be freed by the caller. If all the param attributes are
+ * 0, then NULL is returned.
+ */
+int*
+mono_metadata_get_param_attrs (MonoImage *m, int def)
+{
+       MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
+       MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
+       guint32 cols [MONO_PARAM_SIZE];
+       guint lastp, i, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
+       int *pattrs = NULL;
+
+       if (def < methodt->rows)
+               lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
+       else
+               lastp = paramt->rows + 1;
+
+       for (i = param_index; i < lastp; ++i) {
+               mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
+               if (cols [MONO_PARAM_FLAGS]) {
+                       if (!pattrs)
+                               pattrs = g_new0 (int, 1 + (lastp - param_index));
+                       pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
+               }
+       }
+
+       return pattrs;
+}
+
 /*
  * mono_metadata_parse_signature_full:
  * @image: metadata context
@@ -1480,6 +1546,8 @@ mono_metadata_parse_type (MonoImage *m, MonoParseTypeMode mode, short opt_attrs,
  *
  * Decode a method signature stored in the STANDALONESIG table
  *
+ * LOCKING: Assumes the loader lock is held.
+ *
  * Returns: a MonoMethodSignature describing the signature.
  */
 MonoMethodSignature*
@@ -1500,7 +1568,7 @@ mono_metadata_parse_signature_full (MonoImage *image, MonoGenericContainer *gene
        ptr = mono_metadata_blob_heap (image, sig);
        mono_metadata_decode_blob_size (ptr, &ptr);
 
-       return mono_metadata_parse_method_signature_full (image, generic_container, FALSE, ptr, NULL); 
+       return mono_metadata_parse_method_signature_full (image, generic_container, 0, ptr, NULL); 
 }
 
 /*
@@ -1527,6 +1595,8 @@ mono_metadata_parse_signature (MonoImage *image, guint32 token)
  * The return type and the params types need to be filled later.
  * This is a Mono runtime internal function.
  *
+ * LOCKING: Assumes the loader lock is held.
+ *
  * Returns: the new MonoMethodSignature structure.
  */
 MonoMethodSignature*
@@ -1534,8 +1604,7 @@ mono_metadata_signature_alloc (MonoImage *m, guint32 nparams)
 {
        MonoMethodSignature *sig;
 
-       /* later we want to allocate signatures with mempools */
-       sig = g_malloc0 (sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
+       sig = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodSignature) + ((gint32)nparams - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
        sig->param_count = nparams;
        sig->sentinelpos = -1;
 
@@ -1571,6 +1640,8 @@ mono_metadata_signature_dup (MonoMethodSignature *sig)
  * Decode a method signature stored at @ptr.
  * This is a Mono runtime internal function.
  *
+ * LOCKING: Assumes the loader lock is held.
+ *
  * Returns: a MonoMethodSignature describing the signature.
  */
 MonoMethodSignature *
@@ -1595,23 +1666,8 @@ mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *c
                gen_param_count = mono_metadata_decode_value (ptr, &ptr);
        param_count = mono_metadata_decode_value (ptr, &ptr);
 
-       if (def) {
-               MonoTableInfo *paramt = &m->tables [MONO_TABLE_PARAM];
-               MonoTableInfo *methodt = &m->tables [MONO_TABLE_METHOD];
-               guint32 cols [MONO_PARAM_SIZE];
-               guint lastp, param_index = mono_metadata_decode_row_col (methodt, def - 1, MONO_METHOD_PARAMLIST);
-
-               if (def < methodt->rows)
-                       lastp = mono_metadata_decode_row_col (methodt, def, MONO_METHOD_PARAMLIST);
-               else
-                       lastp = paramt->rows + 1;
-
-               pattrs = g_new0 (int, 1 + param_count);
-               for (i = param_index; i < lastp; ++i) {
-                       mono_metadata_decode_row (paramt, i - 1, cols, MONO_PARAM_SIZE);
-                       pattrs [cols [MONO_PARAM_SEQUENCE]] = cols [MONO_PARAM_FLAGS];
-               }
-       }
+       if (def)
+               pattrs = mono_metadata_get_param_attrs (m, def);
        method = mono_metadata_signature_alloc (m, param_count);
        method->hasthis = hasthis;
        method->explicit_this = explicit_this;
@@ -1632,6 +1688,8 @@ mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *c
                if (*ptr == MONO_TYPE_SENTINEL) {
                        if (method->call_convention != MONO_CALL_VARARG || def)
                                g_error ("found sentinel for methoddef or no vararg method");
+                       if (method->sentinelpos >= 0)
+                               g_error ("found sentinel twice in the same signature");
                        method->sentinelpos = i;
                        ptr++;
                }
@@ -1645,6 +1703,10 @@ mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *c
                        is_open = mono_class_is_open_constructed_type (method->params [i]);
        }
 
+       /* The sentinel could be missing if the caller does not pass any additional arguments */
+       if (!def && method->call_convention == MONO_CALL_VARARG && method->sentinelpos < 0)
+               method->sentinelpos = method->param_count;
+
        method->has_type_parameters = is_open;
 
        if (def && (method->call_convention == MONO_CALL_VARARG))
@@ -1671,6 +1733,8 @@ mono_metadata_parse_method_signature_full (MonoImage *m, MonoGenericContainer *c
  * Decode a method signature stored at @ptr.
  * This is a Mono runtime internal function.
  *
+ * LOCKING: Assumes the loader lock is held.
+ *
  * Returns: a MonoMethodSignature describing the signature.
  */
 MonoMethodSignature *
@@ -1697,8 +1761,6 @@ mono_metadata_free_method_signature (MonoMethodSignature *sig)
                if (sig->params [i])
                        mono_metadata_free_type (sig->params [i]);
        }
-
-       g_free (sig);
 }
 
 /*
@@ -1770,6 +1832,9 @@ mono_metadata_inflate_generic_inst (MonoGenericInst *ginst, MonoGenericContext *
        MonoGenericInst *nginst;
        int i;
 
+       if (!ginst->is_open)
+               return ginst;
+
        nginst = g_new0 (MonoGenericInst, 1);
        nginst->type_argc = ginst->type_argc;
        nginst->type_argv = g_new0 (MonoType*, nginst->type_argc);
@@ -1822,7 +1887,7 @@ mono_metadata_parse_generic_inst (MonoImage *m, MonoGenericContainer *container,
        return mono_metadata_lookup_generic_inst (ginst);
 }
 
-static void
+static gboolean
 do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericContainer *container,
                                      const char *ptr, const char **rptr)
 {
@@ -1849,6 +1914,8 @@ do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericC
        igclass->klass = g_new0 (MonoClass, 1);
 
        gtype = mono_metadata_parse_type (m, MONO_PARSE_TYPE, 0, ptr, &ptr);
+       if (gtype == NULL)
+               return FALSE;
        gclass->container_class = gklass = mono_class_from_mono_type (gtype);
 
        g_assert (gklass->generic_container);
@@ -1861,6 +1928,10 @@ do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericC
        if (rptr)
                *rptr = ptr;
 
+       /* If we failed to parse, return, the error has been flagged. */
+       if (gclass->inst == NULL)
+               return FALSE;
+       
        /*
         * We may be called multiple times on different metadata to create the same
         * instantiated type.  This happens for instance if we're part of a method or
@@ -1881,7 +1952,7 @@ do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericC
                g_free (gclass);
 
                type->data.generic_class = cached;
-               return;
+               return TRUE;
        } else {
                g_hash_table_insert (generic_class_cache, gclass, gclass);
 
@@ -1890,6 +1961,7 @@ do_mono_metadata_parse_generic_class (MonoType *type, MonoImage *m, MonoGenericC
                        sizeof (MonoGenericContext) +
                        gclass->inst->type_argc * sizeof (MonoType);
        }
+       return TRUE;
 }
 
 /*
@@ -1974,6 +2046,7 @@ static gboolean
 do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer *container,
                             const char *ptr, const char **rptr)
 {
+       gboolean ok = TRUE;
        type->type = mono_metadata_decode_value (ptr, &ptr);
        
        switch (type->type){
@@ -2029,7 +2102,7 @@ do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer
                type->data.generic_param = mono_metadata_parse_generic_param (m, container, type->type, ptr, &ptr);
                break;
        case MONO_TYPE_GENERICINST:
-               do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr);
+               ok = do_mono_metadata_parse_generic_class (type, m, container, ptr, &ptr);
                break;
        default:
                g_error ("type 0x%02x not handled in do_mono_metadata_parse_type", type->type);
@@ -2037,14 +2110,15 @@ do_mono_metadata_parse_type (MonoType *type, MonoImage *m, MonoGenericContainer
        
        if (rptr)
                *rptr = ptr;
-       return TRUE;
+       return ok;
 }
 
 /*
  * mono_metadata_free_type:
  * @type: type to free
  *
- * Free the memory allocated for type @type.
+ * Free the memory allocated for type @type which is assumed to be created by
+ * mono_metadata_parse_type ().
  */
 void
 mono_metadata_free_type (MonoType *type)
@@ -2073,7 +2147,8 @@ mono_metadata_free_type (MonoType *type)
                mono_metadata_free_array (type->data.array);
                break;
        }
-       g_free (type);
+
+       /* Allocated from a mempool, no need to free it */
 }
 
 #if 0
@@ -2139,7 +2214,7 @@ parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr
                        int i;
                        mh->num_clauses = is_fat ? sect_data_len / 24: sect_data_len / 12;
                        /* we could just store a pointer if we don't need to byteswap */
-                       mh->clauses = g_new0 (MonoExceptionClause, mh->num_clauses);
+                       mh->clauses = mono_mempool_alloc0 (m->mempool, sizeof (MonoExceptionClause) * mh->num_clauses);
                        for (i = 0; i < mh->num_clauses; ++i) {
                                MonoExceptionClause *ec = &mh->clauses [i];
                                guint32 tof_value;
@@ -2188,7 +2263,9 @@ parse_section_data (MonoImage *m, MonoMethodHeader *mh, const unsigned char *ptr
  * info about local variables and optional exception tables.
  * This is a Mono runtime internal function.
  *
- * Returns: a MonoMethodHeader.
+ * LOCKING: Assumes the loader lock is held.
+ *
+ * Returns: a MonoMethodHeader allocated from the image mempool.
  */
 MonoMethodHeader *
 mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, const char *ptr)
@@ -2205,7 +2282,7 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons
 
        switch (format) {
        case METHOD_HEADER_TINY_FORMAT:
-               mh = g_new0 (MonoMethodHeader, 1);
+               mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
                ptr++;
                mh->max_stack = 8;
                local_var_sig_tok = 0;
@@ -2213,7 +2290,7 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons
                mh->code = ptr;
                return mh;
        case METHOD_HEADER_TINY_FORMAT1:
-               mh = g_new0 (MonoMethodHeader, 1);
+               mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
                ptr++;
                mh->max_stack = 8;
                local_var_sig_tok = 0;
@@ -2268,7 +2345,7 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons
                        g_warning ("wrong signature for locals blob");
                locals_ptr++;
                len = mono_metadata_decode_value (locals_ptr, &locals_ptr);
-               mh = g_malloc0 (sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
+               mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (MonoType*));
                mh->num_locals = len;
                for (i = 0; i < len; ++i) {
                        mh->locals [i] = mono_metadata_parse_type_full (
@@ -2279,7 +2356,7 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons
                        }
                }
        } else {
-               mh = g_new0 (MonoMethodHeader, 1);
+               mh = mono_mempool_alloc0 (m->mempool, sizeof (MonoMethodHeader));
        }
        mh->code = code;
        mh->code_size = code_size;
@@ -2304,7 +2381,15 @@ mono_metadata_parse_mh_full (MonoImage *m, MonoGenericContainer *container, cons
 MonoMethodHeader *
 mono_metadata_parse_mh (MonoImage *m, const char *ptr)
 {
-       return mono_metadata_parse_mh_full (m, NULL, ptr);
+       MonoMethodHeader *res;
+
+       mono_loader_lock ();
+
+       res = mono_metadata_parse_mh_full (m, NULL, ptr);
+
+       mono_loader_unlock ();
+
+       return res;
 }
 
 /*
@@ -2317,11 +2402,7 @@ mono_metadata_parse_mh (MonoImage *m, const char *ptr)
 void
 mono_metadata_free_mh (MonoMethodHeader *mh)
 {
-       int i;
-       for (i = 0; i < mh->num_locals; ++i)
-               mono_metadata_free_type (mh->locals[i]);
-       g_free (mh->clauses);
-       g_free (mh);
+       /* Allocated from the mempool */
 }
 
 /*
@@ -2661,6 +2742,8 @@ mono_metadata_typedef_from_method (MonoImage *meta, guint32 index)
  * The array of interfaces that the @index typedef token implements is returned in
  * @interfaces. The number of elemnts in the array is returned in @count.
  *
+ * LOCKING: Assumes the loader lock is held.
+ *
  * Returns: TRUE on success, FALSE on failure.
  */
 gboolean
@@ -2668,7 +2751,7 @@ mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, Mono
 {
        MonoTableInfo *tdef = &meta->tables [MONO_TABLE_INTERFACEIMPL];
        locator_t loc;
-       guint32 start, i;
+       guint32 start, pos;
        guint32 cols [MONO_INTERFACEIMPL_SIZE];
        MonoClass **result;
 
@@ -2695,18 +2778,31 @@ mono_metadata_interfaces_from_typedef_full (MonoImage *meta, guint32 index, Mono
                else
                        break;
        }
-       result = NULL;
-       i = 0;
-       while (start < tdef->rows) {
-               mono_metadata_decode_row (tdef, start, cols, MONO_INTERFACEIMPL_SIZE);
+       pos = start;
+       while (pos < tdef->rows) {
+               mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
                if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
                        break;
-               result = g_renew (MonoClass*, result, i + 1);
-               result [i] = mono_class_get_full (
+               ++pos;
+       }
+
+       result = mono_mempool_alloc0 (meta->mempool, sizeof (MonoClass*) * (pos - start));
+
+       pos = start;
+       while (pos < tdef->rows) {
+               MonoClass *iface;
+               
+               mono_metadata_decode_row (tdef, pos, cols, MONO_INTERFACEIMPL_SIZE);
+               if (cols [MONO_INTERFACEIMPL_CLASS] != loc.idx)
+                       break;
+               iface = mono_class_get_full (
                        meta, mono_metadata_token_from_dor (cols [MONO_INTERFACEIMPL_INTERFACE]), context);
-               *count = ++i;
-               ++start;
+               if (iface == NULL)
+                       return FALSE;
+               result [pos - start] = iface;
+               ++pos;
        }
+       *count = pos - start;
        *interfaces = result;
        return TRUE;
 }
@@ -2717,7 +2813,9 @@ mono_metadata_interfaces_from_typedef (MonoImage *meta, guint32 index, guint *co
        MonoClass **interfaces;
        gboolean rv;
 
+       mono_loader_lock ();
        rv = mono_metadata_interfaces_from_typedef_full (meta, index, &interfaces, count, NULL);
+       mono_loader_unlock ();
        if (rv)
                return interfaces;
        else
@@ -2915,6 +3013,7 @@ mono_backtrace (int limit)
  *
  * Returns: the number of bytes required to hold an instance of this
  * type in memory
+ * FIXME: This should really use 'guint32*' for the align parameter.
  */
 int
 mono_type_size (MonoType *t, gint *align)
@@ -2988,8 +3087,8 @@ mono_type_size (MonoType *t, gint *align)
                MonoClass *container_class;
 
                gclass = mono_get_inflated_generic_class (t->data.generic_class);
-               g_assert (!gclass->generic_class.inst->is_open);
-               g_assert (!gclass->klass->generic_container);
+               // g_assert (!gclass->generic_class.inst->is_open);
+               // g_assert (!gclass->klass->generic_container);
 
                container_class = gclass->generic_class.container_class;
 
@@ -3020,6 +3119,7 @@ mono_type_size (MonoType *t, gint *align)
  *
  * Returns: the number of bytes required to hold an instance of this
  * type on the runtime stack
+ * FIXME: This should really use 'guint32*' for the align parameter.
  */
 int
 mono_type_stack_size (MonoType *t, gint *align)
@@ -3231,6 +3331,37 @@ mono_metadata_class_equal (MonoClass *c1, MonoClass *c2, gboolean signature_only
        return FALSE;
 }
 
+static gboolean
+mono_metadata_fnptr_equal (MonoMethodSignature *s1, MonoMethodSignature *s2, gboolean signature_only)
+{
+       gpointer iter1 = 0, iter2 = 0;
+
+       if (s1 == s2)
+               return TRUE;
+       if (s1->call_convention != s2->call_convention)
+               return FALSE;
+       if (s1->sentinelpos != s2->sentinelpos)
+               return FALSE;
+       if (s1->hasthis != s2->hasthis)
+               return FALSE;
+       if (s1->explicit_this != s2->explicit_this)
+               return FALSE;
+       if (! do_mono_metadata_type_equal (s1->ret, s2->ret, signature_only))
+               return FALSE;
+       if (s1->param_count != s2->param_count)
+               return FALSE;
+
+       while (TRUE) {
+               MonoType *t1 = mono_signature_get_params (s1, &iter1);
+               MonoType *t2 = mono_signature_get_params (s2, &iter2);
+
+               if (t1 == NULL || t2 == NULL)
+                       return (t1 == t2);
+               if (! do_mono_metadata_type_equal (t1, t2, signature_only))
+                       return FALSE;
+       }
+}
+
 /*
  * mono_metadata_type_equal:
  * @t1: a type
@@ -3284,6 +3415,8 @@ do_mono_metadata_type_equal (MonoType *t1, MonoType *t2, gboolean signature_only
        case MONO_TYPE_MVAR:
                return mono_metadata_generic_param_equal (
                        t1->data.generic_param, t2->data.generic_param, signature_only);
+       case MONO_TYPE_FNPTR:
+               return mono_metadata_fnptr_equal (t1->data.method, t2->data.method, signature_only);
        default:
                g_error ("implement type compare for %0x!", t1->type);
                return FALSE;
@@ -3340,6 +3473,31 @@ mono_metadata_signature_equal (MonoMethodSignature *sig1, MonoMethodSignature *s
        return TRUE;
 }
 
+/**
+ * mono_metadata_type_dup_mp:
+ * @image: image type is defined in
+ * @original: type to duplicate
+ *
+ * Returns: copy of type allocated from mempool.
+ */
+MonoType *
+mono_metadata_type_dup_mp (MonoImage *image, const MonoType *original)
+{
+       MonoType *r = NULL;
+       mono_loader_lock ();
+       r = mono_mempool_alloc0 (image->mempool, sizeof(MonoType));
+       mono_loader_unlock ();
+       *r = *original;
+       /* FIXME: we don't handle these yet because they need to duplicate memory
+        * but the current routines used are not using the mempools
+        */
+       if (original->type == MONO_TYPE_PTR || 
+               original->type == MONO_TYPE_ARRAY || 
+               original->type == MONO_TYPE_FNPTR)
+               g_assert_not_reached ();
+       return r;
+}
+
 guint
 mono_signature_hash (MonoMethodSignature *sig)
 {
@@ -3914,7 +4072,10 @@ handle_enum:
                if (mspec) {
                        switch (mspec->native) {
                        case MONO_NATIVE_BYVALARRAY:
-                               *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
+                               if ((type->data.klass->element_class == mono_defaults.char_class) && !unicode)
+                                       *conv = MONO_MARSHAL_CONV_ARRAY_BYVALCHARARRAY;
+                               else
+                                       *conv = MONO_MARSHAL_CONV_ARRAY_BYVALARRAY;
                                return MONO_NATIVE_BYVALARRAY;
                        case MONO_NATIVE_SAFEARRAY:
                                *conv = MONO_MARSHAL_CONV_ARRAY_SAVEARRAY;
@@ -4031,7 +4192,8 @@ mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod
        gint32 i, num;
        guint32 cols [MONO_METHODIMPL_SIZE];
        MonoMethod **result;
-
+       gint32 ok = TRUE;
+       
        *overrides = NULL;
        if (num_overrides)
                *num_overrides = 0;
@@ -4066,17 +4228,25 @@ mono_class_get_overrides_full (MonoImage *image, guint32 type_token, MonoMethod
        num = end - start;
        result = g_new (MonoMethod*, num * 2);
        for (i = 0; i < num; ++i) {
+               MonoMethod *method;
+
                mono_metadata_decode_row (tdef, start + i, cols, MONO_METHODIMPL_SIZE);
-               result [i * 2] = method_from_method_def_or_ref (
+               method = method_from_method_def_or_ref (
                        image, cols [MONO_METHODIMPL_DECLARATION], generic_context);
-               result [i * 2 + 1] = method_from_method_def_or_ref (
+               if (method == NULL)
+                       ok = FALSE;
+               result [i * 2] = method;
+               method = method_from_method_def_or_ref (
                        image, cols [MONO_METHODIMPL_BODY], generic_context);
+               if (method == NULL)
+                       ok = FALSE;
+               result [i * 2 + 1] = method;
        }
 
        *overrides = result;
        if (num_overrides)
                *num_overrides = num;
-       return TRUE;
+       return ok;
 }
 
 /**
@@ -4096,14 +4266,18 @@ mono_guid_to_string (const guint8 *guid)
 }
 
 static gboolean
-get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContext *context)
+get_constraints (MonoImage *image, int owner, MonoClass ***constraints, MonoGenericContainer *container)
 {
        MonoTableInfo *tdef  = &image->tables [MONO_TABLE_GENERICPARAMCONSTRAINT];
        guint32 cols [MONO_GENPARCONSTRAINT_SIZE];
        guint32 i, token, found;
        MonoClass *klass, **res;
        GList *cons = NULL, *tmp;
-       
+       MonoGenericContext *context = &container->context;
+
+       /* FIXME: !container->klass => this is probably monodis */
+       g_assert (!container->klass || context->gclass || context->gmethod);
+
        *constraints = NULL;
        found = 0;
        for (i = 0; i < tdef->rows; ++i) {
@@ -4192,8 +4366,7 @@ mono_metadata_load_generic_param_constraints (MonoImage *image, guint32 token,
        if (! (start_row = mono_metadata_get_generic_param_row (image, token, &owner)))
                return;
        for (i = 0; i < container->type_argc; i++)
-               get_constraints (image, start_row + i, &container->type_params [i].constraints,
-                                &container->context);
+               get_constraints (image, start_row + i, &container->type_params [i].constraints, container);
 }
 
 /*
@@ -4219,16 +4392,12 @@ mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericC
        guint32 i, owner = 0, n;
        MonoGenericContainer *container;
        MonoGenericParam *params;
-       gpointer *p;
-       GSList *dup_list = NULL, *l;
-       GHashTable *table = NULL;
 
        if (!(i = mono_metadata_get_generic_param_row (image, token, &owner)))
                return NULL;
        mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
        params = NULL;
        n = 0;
-       table = g_hash_table_new (g_str_hash, g_str_equal);
        container = g_new0 (MonoGenericContainer, 1);
        do {
                n++;
@@ -4239,28 +4408,12 @@ mono_metadata_load_generic_params (MonoImage *image, guint32 token, MonoGenericC
                params [n - 1].flags = cols [MONO_GENERICPARAM_FLAGS];
                params [n - 1].num = cols [MONO_GENERICPARAM_NUMBER];
                params [n - 1].name = mono_metadata_string_heap (image, cols [MONO_GENERICPARAM_NAME]);
-               if ((p = g_hash_table_lookup (table, params [n - 1].name)))
-                       dup_list = g_slist_prepend (g_slist_prepend (dup_list, GUINT_TO_POINTER (n)), p);
-               else
-                       g_hash_table_insert (table, (char*)params [n - 1].name, GUINT_TO_POINTER (n));
                params [n - 1].constraints = NULL;
                if (++i > tdef->rows)
                        break;
                mono_metadata_decode_row (tdef, i - 1, cols, MONO_GENERICPARAM_SIZE);
        } while (cols [MONO_GENERICPARAM_OWNER] == owner);
 
-       if (dup_list) {
-               if (!mono_generic_params_with_ambiguous_names)
-                       mono_generic_params_with_ambiguous_names = g_hash_table_new (NULL, NULL);
-               for (l = dup_list; l; l = l->next) {
-                       int param = GPOINTER_TO_UINT (l->data);
-                       g_hash_table_insert (mono_generic_params_with_ambiguous_names, &params [param-1], &params [param-1]);
-               }
-               g_slist_free (dup_list);
-       }
-       g_hash_table_destroy (table);
-       table = NULL;
-
        container->type_argc = n;
        container->type_params = params;
        container->parent = parent_container;