This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mono / metadata / loader.c
index f9354a964d7bfb5b419694a74e968e43a2415765..81f2af3fa7c1f1a6f88e97ee4cd6d88a46a3b257 100644 (file)
 #include <mono/metadata/tokentype.h>
 #include <mono/metadata/cil-coff.h>
 #include <mono/metadata/tabledefs.h>
+#include <mono/metadata/metadata-internals.h>
 #include <mono/metadata/loader.h>
-#include <mono/metadata/class.h>
+#include <mono/metadata/class-internals.h>
 #include <mono/metadata/debug-helpers.h>
+#include <mono/metadata/reflection.h>
+#include <mono/utils/mono-logger.h>
 
-static gboolean dummy_icall = TRUE;
+static void mono_loader_wine_init   (void);
 
 MonoDefaults mono_defaults;
 
-static GHashTable *icall_hash = NULL;
+/*
+ * This lock protects the hash tables inside MonoImage used by the metadata 
+ * loading functions in class.c and loader.c.
+ */
+static CRITICAL_SECTION loader_mutex;
 
 void
-mono_add_internal_call (const char *name, gconstpointer method)
-{
-       if (!icall_hash) {
-               dummy_icall = FALSE;
-               icall_hash = g_hash_table_new (g_str_hash , g_str_equal);
-       }
-
-       g_hash_table_insert (icall_hash, g_strdup (name), method);
-}
-
-static void
-ves_icall_dummy (void)
+mono_loader_init ()
 {
-       g_warning ("the mono runtime is not initialized");
-       g_assert_not_reached ();
+       InitializeCriticalSection (&loader_mutex);
 }
 
-gpointer
-mono_lookup_internal_call (MonoMethod *method)
-{
-       char *name;
-       char *tmpsig;
-       gpointer res;
-
-       if (dummy_icall)
-               return ves_icall_dummy;
-
-       if (!method) {
-               g_warning ("can't resolve internal call, method is null");
-       }
-
-       if (!icall_hash) {
-               g_warning ("icall_hash not initialized");
-               g_assert_not_reached ();
-       }
-
-       if (*method->klass->name_space)
-               name = g_strconcat (method->klass->name_space, ".", method->klass->name, "::", method->name, NULL);
-       else
-               name = g_strconcat (method->klass->name, "::", method->name, NULL);
-       if (!(res = g_hash_table_lookup (icall_hash, name))) {
-               /* trying to resolve with full signature */
-               g_free (name);
-       
-               tmpsig = mono_signature_get_desc(method->signature, TRUE);
-               if (*method->klass->name_space)
-                       name = g_strconcat (method->klass->name_space, ".", method->klass->name, "::", method->name, "(", tmpsig, ")", NULL);
-               else
-                       name = g_strconcat (method->klass->name, "::", method->name, "(", tmpsig, ")", NULL);
-               if (!(res = g_hash_table_lookup (icall_hash, name))) {
-                       g_warning ("cant resolve internal call to \"%s\" (tested without signature also)", name);
-                       g_print ("\nYour mono runtime and corlib are out of sync.\n");
-                       g_print ("When you update one from cvs you need to update, compile and install\nthe other too.\n");
-                       g_print ("Do not report this as a bug unless you're sure you have updated correctly:\nyou probably have a broken mono install.\n");
-                       g_print ("If you see other errors or faults after this message they are probably related\n");
-                       g_print ("and you need to fix your mono install first.\n");
-
-                       g_free (name);
-                       g_free (tmpsig);
-
-                       return NULL;
-               }
-
-               g_free(tmpsig);
-       }
-
-       g_free (name);
-
-       return res;
-}
-
-MonoClassField*
-mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass)
+static MonoClassField*
+field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass,
+                     MonoGenericContext *context)
 {
        MonoClass *klass;
        MonoTableInfo *tables = image->tables;
@@ -121,9 +63,15 @@ mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass
        const char *ptr;
        guint32 idx = mono_metadata_token_index (token);
 
+       if (image->dynamic) {
+               MonoClassField *result = mono_lookup_dynamic_token (image, token);
+               *retklass = result->parent;
+               return result;
+       }
+
        mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
-       nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
-       class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
+       nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
+       class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
 
        fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
        
@@ -132,7 +80,7 @@ mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass
        /* we may want to check the signature here... */
 
        switch (class) {
-       case MEMBERREF_PARENT_TYPEREF:
+       case MONO_MEMBERREF_PARENT_TYPEREF:
                klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
                if (!klass) {
                        g_warning ("Missing field %s in typeref index %d", fname, nindex);
@@ -142,53 +90,206 @@ mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass
                if (retklass)
                        *retklass = klass;
                return mono_class_get_field_from_name (klass, fname);
+       case MONO_MEMBERREF_PARENT_TYPESPEC: {
+               /*guint32 bcols [MONO_TYPESPEC_SIZE];
+               guint32 len;
+               MonoType *type;
+
+               mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
+                                         bcols, MONO_TYPESPEC_SIZE);
+               ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
+               len = mono_metadata_decode_value (ptr, &ptr);   
+               type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
+
+               klass = mono_class_from_mono_type (type);
+               mono_class_init (klass);
+               g_print ("type in sig: %s\n", klass->name);*/
+               klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
+               mono_class_init (klass);
+               if (retklass)
+                       *retklass = klass;
+               return mono_class_get_field_from_name (klass, fname);
+       }
        default:
+               g_warning ("field load from %x", class);
                return NULL;
        }
 }
 
 MonoClassField*
-mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass)
+mono_field_from_token (MonoImage *image, guint32 token, MonoClass **retklass,
+                      MonoGenericContext *context)
 {
        MonoClass *k;
        guint32 type;
-       
+       MonoClassField *field;
+
+       if (image->dynamic) {
+               MonoClassField *result = mono_lookup_dynamic_token (image, token);
+               *retklass = result->parent;
+               return result;
+       }
+
+       mono_loader_lock ();
+       if ((field = g_hash_table_lookup (image->field_cache, GUINT_TO_POINTER (token)))) {
+               *retklass = field->parent;
+               mono_loader_unlock ();
+               return field;
+       }
+       mono_loader_unlock ();
+
        if (mono_metadata_token_table (token) == MONO_TABLE_MEMBERREF)
-               return mono_field_from_memberref (image, token, retklass);
+               field = field_from_memberref (image, token, retklass, context);
+       else {
+               type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
+               if (!type)
+                       return NULL;
+               k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
+               mono_class_init (k);
+               if (!k)
+                       return NULL;
+               if (retklass)
+                       *retklass = k;
+               field = mono_class_get_field (k, token);
+       }
 
-       type = mono_metadata_typedef_from_field (image, mono_metadata_token_index (token));
-       if (!type)
-               return NULL;
-       k = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
-       mono_class_init (k);
-       if (!k)
-               return NULL;
-       if (retklass)
-               *retklass = k;
-       return mono_class_get_field (k, token);
+       mono_loader_lock ();
+       if (!field->parent->generic_inst)
+               g_hash_table_insert (image->field_cache, GUINT_TO_POINTER (token), field);
+       mono_loader_unlock ();
+       return field;
+}
+
+static gboolean
+mono_metadata_signature_vararg_match (MonoMethodSignature *sig1, MonoMethodSignature *sig2)
+{
+       int i;
+
+       if (sig1->hasthis != sig2->hasthis ||
+           sig1->sentinelpos != sig2->sentinelpos)
+               return FALSE;
+
+       for (i = 0; i < sig1->sentinelpos; i++) { 
+               MonoType *p1 = sig1->params[i];
+               MonoType *p2 = sig2->params[i];
+               
+               /*if (p1->attrs != p2->attrs)
+                       return FALSE;
+               */
+               if (!mono_metadata_type_equal (p1, p2))
+                       return FALSE;
+       }
+
+       if (!mono_metadata_type_equal (sig1->ret, sig2->ret))
+               return FALSE;
+       return TRUE;
 }
 
 static MonoMethod *
-find_method (MonoClass *klass, const char* name, MonoMethodSignature *sig)
+find_method (MonoClass *klass, MonoClass *ic, const char* name, MonoMethodSignature *sig)
 {
        int i;
+       MonoClass *sclass = klass;
+       char *qname, *fqname;
+
+       if (ic) {
+               qname = g_strconcat (ic->name, ".", name, NULL); 
+               if (ic->name_space && ic->name_space [0])
+                       fqname = g_strconcat (ic->name_space, ".", ic->name, ".", name, NULL);
+               else
+                       fqname = NULL;
+       } else
+               qname = fqname = NULL;
+
        while (klass) {
-               /* mostly dumb search for now */
                for (i = 0; i < klass->method.count; ++i) {
                        MonoMethod *m = klass->methods [i];
-                       if (!strcmp (name, m->name)) {
+
+                       if (!((fqname && !strcmp (m->name, fqname)) ||
+                             (qname && !strcmp (m->name, qname)) || !strcmp (m->name, name)))
+                               continue;
+
+                       if (sig->call_convention == MONO_CALL_VARARG) {
+                               if (mono_metadata_signature_vararg_match (sig, m->signature))
+                                       return m;
+                       } else {
                                if (mono_metadata_signature_equal (sig, m->signature))
                                        return m;
                        }
                }
+
+               if (name [0] == '.' && (strcmp (name, ".ctor") == 0 || strcmp (name, ".cctor") == 0))
+                       break;
+
                klass = klass->parent;
        }
+
+       if (sclass->generic_inst) {
+               MonoClass *gclass;
+               MonoMethod *res;
+
+               gclass = mono_class_from_mono_type (sclass->generic_inst->generic_type);
+               mono_class_init (gclass);
+
+               res = find_method (gclass, ic, name, sig);
+               if (!res)
+                       return NULL;
+               for (i = 0; i < res->klass->method.count; ++i) {
+                       if (res == res->klass->methods [i]) {
+                               return sclass->methods [i];
+                       }
+               }
+       }
+
        return NULL;
+}
 
+/*
+ * token is the method_ref or method_def token used in a call IL instruction.
+ */
+MonoMethodSignature*
+mono_method_get_signature (MonoMethod *method, MonoImage *image, guint32 token)
+{
+       int table = mono_metadata_token_table (token);
+       int idx = mono_metadata_token_index (token);
+       guint32 cols [MONO_MEMBERREF_SIZE];
+       MonoMethodSignature *sig;
+       const char *ptr;
+
+       /* !table is for wrappers: we should really assign their own token to them */
+       if (!table || table == MONO_TABLE_METHOD)
+               return method->signature;
+
+       if (table == MONO_TABLE_METHODSPEC) {
+               g_assert (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) &&
+                         !(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) &&
+                         method->signature);
+               g_assert (method->signature->is_inflated);
+
+               return method->signature;
+       }
+
+       if (method->klass->generic_inst)
+               return method->signature;
+
+       if (image->dynamic)
+               /* FIXME: This might be incorrect for vararg methods */
+               return method->signature;
+
+       if (!(sig = g_hash_table_lookup (image->memberref_signatures, GUINT_TO_POINTER (token)))) {
+               mono_metadata_decode_row (&image->tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
+       
+               ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
+               mono_metadata_decode_blob_size (ptr, &ptr);
+               sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
+               g_hash_table_insert (image->memberref_signatures, GUINT_TO_POINTER (token), sig);
+       }
+
+       return sig;
 }
 
 static MonoMethod *
-method_from_memberref (MonoImage *image, guint32 idx)
+method_from_memberref (MonoImage *image, guint32 idx, MonoGenericContext *context)
 {
        MonoClass *klass;
        MonoMethod *method;
@@ -200,8 +301,8 @@ method_from_memberref (MonoImage *image, guint32 idx)
        const char *ptr;
 
        mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
-       nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
-       class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
+       nindex = cols [MONO_MEMBERREF_CLASS] >> MONO_MEMBERREF_PARENT_BITS;
+       class = cols [MONO_MEMBERREF_CLASS] & MONO_MEMBERREF_PARENT_MASK;
        /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
                mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
 
@@ -212,7 +313,7 @@ method_from_memberref (MonoImage *image, guint32 idx)
        sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
 
        switch (class) {
-       case MEMBERREF_PARENT_TYPEREF:
+       case MONO_MEMBERREF_PARENT_TYPEREF:
                klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
                if (!klass) {
                        g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
@@ -220,25 +321,32 @@ method_from_memberref (MonoImage *image, guint32 idx)
                        return NULL;
                }
                mono_class_init (klass);
-               method = find_method (klass, mname, sig);
+               method = find_method (klass, NULL, mname, sig);
                if (!method)
                        g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
                mono_metadata_free_method_signature (sig);
                return method;
-       case MEMBERREF_PARENT_TYPESPEC: {
+       case MONO_MEMBERREF_PARENT_TYPESPEC: {
                guint32 bcols [MONO_TYPESPEC_SIZE];
                guint32 len;
                MonoType *type;
                MonoMethod *result;
-
-               mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
-                                         bcols, MONO_TYPESPEC_SIZE);
-               ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
-               len = mono_metadata_decode_value (ptr, &ptr);   
-               type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
-
-               if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY)
-                       g_assert_not_reached ();                
+               MonoClass *klass;
+
+               klass = mono_class_get_full (image, MONO_TOKEN_TYPE_SPEC | nindex, context);
+               type = &klass->byval_arg;
+
+               if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY) {
+                       mono_class_init (klass);
+                       method = find_method (klass, NULL, mname, sig);
+                       if (!method)
+                               g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
+                       else if (klass->generic_inst && (klass != method->klass))
+                               method = mono_class_inflate_generic_method (
+                                       method, klass->generic_inst->context, klass);
+                       mono_metadata_free_method_signature (sig);
+                       return method;
+               }
 
                result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
                result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
@@ -255,37 +363,103 @@ method_from_memberref (MonoImage *image, guint32 idx)
                if (!strcmp (mname, "Set")) {
                        g_assert (sig->hasthis);
                        g_assert (type->data.array->rank + 1 == sig->param_count);
-
-                       result->addr = mono_lookup_internal_call(result);
+                       result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
+                       result->addr = NULL;
                        return result;
                }
 
                if (!strcmp (mname, "Get")) {
                        g_assert (sig->hasthis);
                        g_assert (type->data.array->rank == sig->param_count);
-
-                       result->addr = mono_lookup_internal_call(result);
+                       result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
+                       result->addr = NULL;
                        return result;
                }
 
                if (!strcmp (mname, "Address")) {
                        g_assert (sig->hasthis);
                        g_assert (type->data.array->rank == sig->param_count);
-
-                       result->addr = mono_lookup_internal_call(result);
+                       result->iflags |= METHOD_IMPL_ATTRIBUTE_RUNTIME;
+                       result->addr = NULL;
                        return result;
                }
 
                g_assert_not_reached ();
                break;
        }
+       case MONO_MEMBERREF_PARENT_TYPEDEF:
+               klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | nindex);
+               if (!klass) {
+                       g_warning ("Missing method %s in assembly %s typedef index %d", mname, image->name, nindex);
+                       mono_metadata_free_method_signature (sig);
+                       return NULL;
+               }
+               mono_class_init (klass);
+               method = find_method (klass, NULL, mname, sig);
+               if (!method)
+                       g_warning ("Missing method %s in assembly %s typeref index %d", mname, image->name, nindex);
+               mono_metadata_free_method_signature (sig);
+               return method;
+       case MONO_MEMBERREF_PARENT_METHODDEF:
+               method = mono_get_method (image, MONO_TOKEN_METHOD_DEF | nindex, NULL);
+               return method;
        default:
+               g_error ("Memberref parent unknown: class: %d, index %d", class, nindex);
                g_assert_not_reached ();
        }
 
        return NULL;
 }
 
+static MonoMethod *
+method_from_methodspec (MonoImage *image, guint32 idx)
+{
+       MonoMethod *method, *inflated;
+       MonoTableInfo *tables = image->tables;
+       MonoGenericContext *context;
+       MonoGenericMethod *gmethod;
+       const char *ptr;
+       guint32 cols [MONO_METHODSPEC_SIZE];
+       guint32 token, param_count, i;
+
+       mono_metadata_decode_row (&tables [MONO_TABLE_METHODSPEC], idx - 1, cols, MONO_METHODSPEC_SIZE);
+       token = cols [MONO_METHODSPEC_METHOD];
+       if ((token & MONO_METHODDEFORREF_MASK) == MONO_METHODDEFORREF_METHODDEF)
+               token = MONO_TOKEN_METHOD_DEF | (token >> MONO_METHODDEFORREF_BITS);
+       else
+               token = MONO_TOKEN_MEMBER_REF | (token >> MONO_METHODDEFORREF_BITS);
+
+       method = mono_get_method (image, token, NULL);
+
+       ptr = mono_metadata_blob_heap (image, cols [MONO_METHODSPEC_SIGNATURE]);
+       
+       mono_metadata_decode_value (ptr, &ptr);
+       ptr++;
+       param_count = mono_metadata_decode_value (ptr, &ptr);
+
+       gmethod = g_new0 (MonoGenericMethod, 1);
+       gmethod->mtype_argc = param_count;
+       gmethod->mtype_argv = g_new0 (MonoType *, param_count);
+       
+       for (i = 0; i < param_count; i++) {
+               gmethod->mtype_argv [i] = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
+
+               if (!gmethod->is_open)
+                       gmethod->is_open = mono_class_is_open_constructed_type (gmethod->mtype_argv [i]);
+       }
+
+       context = g_new0 (MonoGenericContext, 1);
+       context->gmethod = gmethod;
+
+       mono_stats.generics_metadata_size += sizeof (MonoGenericMethod) +
+               sizeof (MonoGenericContext) + param_count * sizeof (MonoType);
+
+       inflated = mono_class_inflate_generic_method (method, context, NULL);
+
+       context->ginst = inflated->klass->generic_inst;
+       return inflated;
+}
+
 typedef struct MonoDllMap MonoDllMap;
 
 struct MonoDllMap {
@@ -295,17 +469,24 @@ struct MonoDllMap {
        MonoDllMap *next;
 };
 
-static GHashTable *dll_map;
+static GHashTable *global_dll_map;
 
-int 
-mono_dllmap_lookup (const char *dll, const char* func, const char **rdll, const char **rfunc) {
+static int 
+mono_dllmap_lookup_hash (GHashTable *dll_map, const char *dll, const char* func, const char **rdll, const char **rfunc) {
        MonoDllMap *map, *tmp;
 
+       *rdll = dll;
+
        if (!dll_map)
                return 0;
+
+       mono_loader_lock ();
+
        map = g_hash_table_lookup (dll_map, dll);
-       if (!map)
+       if (!map) {
+               mono_loader_unlock ();
                return 0;
+       }
        *rdll = map->target? map->target: dll;
                
        for (tmp = map->next; tmp; tmp = tmp->next) {
@@ -313,19 +494,43 @@ mono_dllmap_lookup (const char *dll, const char* func, const char **rdll, const
                        *rfunc = tmp->name;
                        if (tmp->dll)
                                *rdll = tmp->dll;
+                       mono_loader_unlock ();
                        return 1;
                }
        }
        *rfunc = func;
+       mono_loader_unlock ();
        return 1;
 }
 
+static int 
+mono_dllmap_lookup (MonoImage *assembly, const char *dll, const char* func, const char **rdll, const char **rfunc)
+{
+       int res;
+       if (assembly && assembly->dll_map) {
+               res = mono_dllmap_lookup_hash (assembly->dll_map, dll, func, rdll, rfunc);
+               if (res)
+                       return res;
+       }
+       return mono_dllmap_lookup_hash (global_dll_map, dll, func, rdll, rfunc);
+}
+
 void
-mono_dllmap_insert (const char *dll, const char *func, const char *tdll, const char *tfunc) {
+mono_dllmap_insert (MonoImage *assembly, const char *dll, const char *func, const char *tdll, const char *tfunc) {
        MonoDllMap *map, *entry;
+       GHashTable *dll_map = NULL;
 
-       if (!dll_map)
-               dll_map = g_hash_table_new (g_str_hash, g_str_equal);
+       mono_loader_lock ();
+
+       if (!assembly) {
+               if (!global_dll_map)
+                       global_dll_map = g_hash_table_new (g_str_hash, g_str_equal);
+               dll_map = global_dll_map;
+       } else {
+               if (!assembly->dll_map)
+                       assembly->dll_map = g_hash_table_new (g_str_hash, g_str_equal);
+               dll_map = assembly->dll_map;
+       }
 
        map = g_hash_table_lookup (dll_map, dll);
        if (!map) {
@@ -345,10 +550,19 @@ mono_dllmap_insert (const char *dll, const char *func, const char *tdll, const c
                entry->next = map->next;
                map->next = entry;
        }
+
+       mono_loader_unlock ();
 }
 
+static void
+mono_dllmap_free (GHashTable *dll_map)
+{
+}
+
+static int wine_test_needed = 1;
+
 gpointer
-mono_lookup_pinvoke_call (MonoMethod *method)
+mono_lookup_pinvoke_call (MonoMethod *method, const char **exc_class, const char **exc_arg)
 {
        MonoImage *image = method->klass->image;
        MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
@@ -358,12 +572,19 @@ mono_lookup_pinvoke_call (MonoMethod *method)
        guint32 im_cols [MONO_IMPLMAP_SIZE];
        guint32 scope_token;
        const char *import = NULL;
-       const char *scope = NULL;
-       char *full_name;
-       GModule *gmodule;
+       const char *orig_scope;
+       const char *new_scope;
+       char *full_name, *file_name;
+       int i;
+       GModule *gmodule = NULL;
 
        g_assert (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL);
 
+       if (exc_class) {
+               *exc_class = NULL;
+               *exc_arg = NULL;
+       }
+
        if (method->addr)
                return method->addr;
        if (!piinfo->implmap_idx)
@@ -374,51 +595,158 @@ mono_lookup_pinvoke_call (MonoMethod *method)
        piinfo->piflags = im_cols [MONO_IMPLMAP_FLAGS];
        import = mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]);
        scope_token = mono_metadata_decode_row_col (mr, im_cols [MONO_IMPLMAP_SCOPE] - 1, MONO_MODULEREF_NAME);
-       scope = mono_metadata_string_heap (image, scope_token);
+       orig_scope = mono_metadata_string_heap (image, scope_token);
 
-       mono_dllmap_lookup (scope, import, &scope, &import);
+       mono_dllmap_lookup (image, orig_scope, import, &new_scope, &import);
 
-       full_name = g_module_build_path (NULL, scope);
-       gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
+       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+                       "DllImport attempting to load: '%s'.", new_scope);
 
-       if (!gmodule) {
-               if (!(gmodule=g_module_open (scope, G_MODULE_BIND_LAZY))) {
-                       g_warning ("Failed to load library %s (%s)", full_name, scope);
+#ifndef PLATFORM_WIN32
+       /*
+        * If we are P/Invoking a library from System.Windows.Forms, load Wine
+        */
+       if (wine_test_needed && strcmp (image->assembly_name, "System.Windows.Forms") == 0){
+               mono_loader_wine_init ();
+               wine_test_needed = 0;
+       }
+#endif
+       
+       /*
+        * Try loading the module using a variety of names
+        */
+       for (i = 0; i < 2; ++i) {
+               if (i == 0)
+                       /* Try the original name */
+                       file_name = g_strdup (new_scope);
+               else {
+                       /* Try trimming the .dll extension */
+                       if (strstr (new_scope, ".dll") == (new_scope + strlen (new_scope) - 4)) {
+                               file_name = g_strdup (new_scope);
+                               file_name [strlen (new_scope) - 4] = '\0';
+                       }
+                       else
+                               break;
+               }
+
+               if (!gmodule) {
+                       full_name = g_module_build_path (NULL, file_name);
+                       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+                                       "DllImport loading location: '%s'.", full_name);
+                       gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
+                       if (!gmodule) {
+                               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+                                               "DllImport error loading library: '%s'.",
+                                               g_module_error ());
+                       }
                        g_free (full_name);
-                       return NULL;
                }
+
+               if (!gmodule) {
+                       full_name = g_module_build_path (".", file_name);
+                       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+                                       "DllImport loading library: '%s'.", full_name);
+                       gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
+                       if (!gmodule) {
+                               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+                                               "DllImport error loading library '%s'.",
+                                               g_module_error ());
+                       }
+                       g_free (full_name);
+               }
+
+               if (!gmodule) {
+                       mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+                                       "DllImport loading: '%s'.", file_name);
+                       gmodule=g_module_open (file_name, G_MODULE_BIND_LAZY);
+                       if (!gmodule) {
+                               mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_DLLIMPORT,
+                                               "DllImport error loading library '%s'.",
+                                               g_module_error ());
+                       }
+               }
+
+               g_free (file_name);
+
+               if (gmodule)
+                       break;
+       }
+
+       if (!gmodule) {
+               mono_trace (G_LOG_LEVEL_WARNING, MONO_TRACE_DLLIMPORT,
+                               "DllImport unable to load library '%s'.",
+                               g_module_error ());
+
+               if (exc_class) {
+                       *exc_class = "DllNotFoundException";
+                       *exc_arg = orig_scope;
+               }
+               return NULL;
        }
-       g_free (full_name);
 
-       g_module_symbol (gmodule, import, &method->addr); 
+       if (piinfo->piflags & PINVOKE_ATTRIBUTE_NO_MANGLE) {
+               g_module_symbol (gmodule, import, &method->addr); 
+       } else {
+               char *mangled_name;
+
+               switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CHAR_SET_MASK) {
+               case PINVOKE_ATTRIBUTE_CHAR_SET_UNICODE:
+                       mangled_name = g_strconcat (import, "W", NULL);
+                       g_module_symbol (gmodule, mangled_name, &method->addr); 
+                       g_free (mangled_name);
+
+                       if (!method->addr)
+                               g_module_symbol (gmodule, import, &method->addr); 
+                       break;
+               case PINVOKE_ATTRIBUTE_CHAR_SET_AUTO:
+                       g_module_symbol (gmodule, import, &method->addr); 
+                       break;
+               case PINVOKE_ATTRIBUTE_CHAR_SET_ANSI:
+               default:
+                       mangled_name = g_strconcat (import, "A", NULL);
+                       g_module_symbol (gmodule, mangled_name, &method->addr); 
+                       g_free (mangled_name);
+
+                       if (!method->addr)
+                               g_module_symbol (gmodule, import, &method->addr); 
+                              
+                       break;                                  
+               }
+       }
 
        if (!method->addr) {
-               g_warning ("Failed to load function %s from %s", import, scope);
+               if (exc_class) {
+                       *exc_class = "EntryPointNotFoundException";
+                       *exc_arg = import;
+               }
                return NULL;
        }
        return method->addr;
 }
 
-MonoMethod *
-mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
+static MonoMethod *
+mono_get_method_from_token (MonoImage *image, guint32 token, MonoClass *klass,
+                           MonoGenericContext *context)
 {
        MonoMethod *result;
        int table = mono_metadata_token_table (token);
        int idx = mono_metadata_token_index (token);
        MonoTableInfo *tables = image->tables;
        const char *loc, *sig = NULL;
-       int size;
+       int size, i;
        guint32 cols [MONO_TYPEDEF_SIZE];
 
-       if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
-                       return result;
+       if (image->dynamic)
+               return mono_lookup_dynamic_token (image, token);
 
        if (table != MONO_TABLE_METHOD) {
+               if (table == MONO_TABLE_METHODSPEC)
+                       return method_from_methodspec (image, idx);
                if (table != MONO_TABLE_MEMBERREF)
                        g_print("got wrong token: 0x%08x\n", token);
                g_assert (table == MONO_TABLE_MEMBERREF);
-               result = method_from_memberref (image, idx);
-               g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
+               result = method_from_memberref (image, idx, context);
+
                return result;
        }
 
@@ -434,12 +762,13 @@ mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
        result->klass = klass;
        result->flags = cols [2];
        result->iflags = cols [1];
+       result->token = token;
        result->name = mono_metadata_string_heap (image, cols [3]);
 
        if (!sig) /* already taken from the methodref */
                sig = mono_metadata_blob_heap (image, cols [4]);
        size = mono_metadata_decode_blob_size (sig, &sig);
-       result->signature = mono_metadata_parse_method_signature (image, 0, sig, NULL);
+       result->signature = mono_metadata_parse_method_signature (image, idx, sig, NULL);
 
        if (!result->klass) {
                guint32 type = mono_metadata_typedef_from_method (image, token);
@@ -450,26 +779,145 @@ mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
                if (result->klass == mono_defaults.string_class && !strcmp (result->name, ".ctor"))
                        result->string_ctor = 1;
 
-               result->addr = mono_lookup_internal_call (result);
-               result->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
-       } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
-               ((MonoMethodPInvoke *)result)->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
+               result->signature->pinvoke = 1;
+       } else if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) && (!(cols [1] & METHOD_IMPL_ATTRIBUTE_NATIVE))) {
+               MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)result;
+               MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
+               MonoCallConvention conv = 0;
+
+               result->signature->pinvoke = 1;
+               piinfo->implmap_idx = mono_metadata_implmap_from_method (image, idx - 1);
+               piinfo->piflags = mono_metadata_decode_row_col (im, piinfo->implmap_idx - 1, MONO_IMPLMAP_FLAGS);
+
+               switch (piinfo->piflags & PINVOKE_ATTRIBUTE_CALL_CONV_MASK) {
+               case PINVOKE_ATTRIBUTE_CALL_CONV_WINAPI:
+                       conv = MONO_CALL_DEFAULT;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_CDECL:
+                       conv = MONO_CALL_C;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_STDCALL:
+                       conv = MONO_CALL_STDCALL;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_THISCALL:
+                       conv = MONO_CALL_THISCALL;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_FASTCALL:
+                       conv = MONO_CALL_FASTCALL;
+                       break;
+               case PINVOKE_ATTRIBUTE_CALL_CONV_GENERIC:
+               case PINVOKE_ATTRIBUTE_CALL_CONV_GENERICINST:
+               default:
+                       g_warning ("unsupported calling convention");
+                       g_assert_not_reached ();
+               }       
+               result->signature->call_convention = conv;
        } else {
+               MonoGenericParam *gen_params = NULL;
+
                /* if this is a methodref from another module/assembly, this fails */
                loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
 
+               if (result->signature->generic_param_count) {
+                       MonoMethodSignature *sig = result->signature;
+
+                       gen_params = mono_metadata_load_generic_params (image, token, NULL);
+
+                       for (i = 0; i < sig->generic_param_count; i++) {
+                               gen_params [i].method = result;
+
+                               mono_class_from_generic_parameter (
+                                       &gen_params [i], image, TRUE);
+                       }
+
+                       if (sig->ret->type == MONO_TYPE_MVAR) {
+                               int num = sig->ret->data.generic_param->num;
+                               sig->ret->data.generic_param = &gen_params [num];
+                       }
+
+                       for (i = 0; i < sig->param_count; i++) {
+                               MonoType *t = sig->params [i];
+                               if (t->type == MONO_TYPE_MVAR) {
+                                       int num = t->data.generic_param->num;
+                                       sig->params [i]->data.generic_param = &gen_params [num];
+                               }
+                       }
+               }
+
                if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
                                        !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
+                       MonoMethodNormal *mn = (MonoMethodNormal *) result;
+
                        g_assert (loc);
-                       ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
+                       mn->header = mono_metadata_parse_mh (image, loc);
+                       mn->header->gen_params = gen_params;
                }
        }
 
-       g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
+       return result;
+}
+
+MonoMethod *
+mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
+{
+       return mono_get_method_full (image, token, klass, NULL);
+}
+
+MonoMethod *
+mono_get_method_full (MonoImage *image, guint32 token, MonoClass *klass,
+                     MonoGenericContext *context)
+{
+       MonoMethod *result;
+
+       /* We do everything inside the lock to prevent creation races */
+
+       mono_loader_lock ();
+
+       if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token)))) {
+               mono_loader_unlock ();
+               return result;
+       }
+
+       result = mono_get_method_from_token (image, token, klass, context);
+
+       if (!(result && result->signature->is_inflated))
+               g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
+
+       mono_loader_unlock ();
 
        return result;
 }
 
+MonoMethod *
+mono_get_method_constrained (MonoImage *image, guint32 token, MonoClass *constrained_class,
+                            MonoGenericContext *context)
+{
+       MonoMethod *method, *result;
+       MonoClass *ic = NULL;
+       int i;
+
+       mono_loader_lock ();
+
+       method = mono_get_method_from_token (image, token, NULL, context);
+       if (!method) {
+               mono_loader_unlock ();
+               return NULL;
+       }
+
+       mono_class_init (constrained_class);
+
+       if ((constrained_class != method->klass) && (method->klass->interface_id != 0))
+               ic = method->klass;
+
+       result = find_method (constrained_class, ic, method->name, method->signature);
+       if (!result)
+               g_warning ("Missing method %s in assembly %s token %x", method->name,
+                          image->name, token);
+
+       mono_loader_unlock ();
+       return result;
+}
+
 void
 mono_free_method  (MonoMethod *method)
 {
@@ -489,18 +937,33 @@ mono_method_get_param_names (MonoMethod *method, const char **names)
 {
        int i, lastp;
        MonoClass *klass = method->klass;
-       MonoTableInfo *methodt = &klass->image->tables [MONO_TABLE_METHOD];
-       MonoTableInfo *paramt = &klass->image->tables [MONO_TABLE_PARAM];
+       MonoTableInfo *methodt;
+       MonoTableInfo *paramt;
 
        if (!method->signature->param_count)
                return;
        for (i = 0; i < method->signature->param_count; ++i)
                names [i] = "";
 
+       if (klass->generic_inst) /* copy the names later */
+               return;
+
        mono_class_init (klass);
-       if (!klass->methods)
+
+       if (klass->image->dynamic) {
+               MonoReflectionMethodAux *method_aux = 
+                       mono_g_hash_table_lookup (
+                               ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
+               if (method_aux && method_aux->param_names) {
+                       for (i = 0; i < method->signature->param_count; ++i)
+                               if (method_aux->param_names [i + 1])
+                                       names [i] = method_aux->param_names [i + 1];
+               }
                return;
+       }
 
+       methodt = &klass->image->tables [MONO_TABLE_METHOD];
+       paramt = &klass->image->tables [MONO_TABLE_PARAM];
        for (i = 0; i < klass->method.count; ++i) {
                if (method == klass->methods [i]) {
                        guint32 idx = klass->method.first + i;
@@ -510,7 +973,7 @@ mono_method_get_param_names (MonoMethod *method, const char **names)
                        if (idx + 1 < methodt->rows)
                                lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
                        else
-                               lastp = paramt->rows;
+                               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_SEQUENCE]) /* skip return param spec */
@@ -521,6 +984,113 @@ mono_method_get_param_names (MonoMethod *method, const char **names)
        }
 }
 
+void
+mono_method_get_marshal_info (MonoMethod *method, MonoMarshalSpec **mspecs)
+{
+       int i, lastp;
+       MonoClass *klass = method->klass;
+       MonoTableInfo *methodt;
+       MonoTableInfo *paramt;
+
+       for (i = 0; i < method->signature->param_count + 1; ++i)
+               mspecs [i] = NULL;
+
+       if (method->klass->image->dynamic) {
+               MonoReflectionMethodAux *method_aux = 
+                       mono_g_hash_table_lookup (
+                               ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
+               if (method_aux && method_aux->param_marshall) {
+                       MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
+                       for (i = 0; i < method->signature->param_count + 1; ++i)
+                               if (dyn_specs [i]) {
+                                       mspecs [i] = g_new0 (MonoMarshalSpec, 1);
+                                       memcpy (mspecs [i], dyn_specs [i], sizeof (MonoMarshalSpec));
+                               }
+               }
+               return;
+       }
+
+       mono_class_init (klass);
+
+       methodt = &klass->image->tables [MONO_TABLE_METHOD];
+       paramt = &klass->image->tables [MONO_TABLE_PARAM];
+
+       for (i = 0; i < klass->method.count; ++i) {
+               if (method == klass->methods [i]) {
+                       guint32 idx = klass->method.first + i;
+                       guint32 cols [MONO_PARAM_SIZE];
+                       guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
+
+                       if (idx + 1 < methodt->rows)
+                               lastp = mono_metadata_decode_row_col (methodt, idx + 1, 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] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL) {
+                                       const char *tp;
+                                       tp = mono_metadata_get_marshal_info (klass->image, i - 1, FALSE);
+                                       g_assert (tp);
+                                       mspecs [cols [MONO_PARAM_SEQUENCE]]= mono_metadata_parse_marshal_spec (klass->image, tp);
+                               }
+                       }
+
+                       return;
+               }
+       }
+}
+
+gboolean
+mono_method_has_marshal_info (MonoMethod *method)
+{
+       int i, lastp;
+       MonoClass *klass = method->klass;
+       MonoTableInfo *methodt;
+       MonoTableInfo *paramt;
+
+       if (method->klass->image->dynamic) {
+               MonoReflectionMethodAux *method_aux = 
+                       mono_g_hash_table_lookup (
+                               ((MonoDynamicImage*)method->klass->image)->method_aux_hash, method);
+               MonoMarshalSpec **dyn_specs = method_aux->param_marshall;
+               if (dyn_specs) {
+                       for (i = 0; i < method->signature->param_count + 1; ++i)
+                               if (dyn_specs [i])
+                                       return TRUE;
+               }
+               return FALSE;
+       }
+
+       mono_class_init (klass);
+
+       methodt = &klass->image->tables [MONO_TABLE_METHOD];
+       paramt = &klass->image->tables [MONO_TABLE_PARAM];
+
+       for (i = 0; i < klass->method.count; ++i) {
+               if (method == klass->methods [i]) {
+                       guint32 idx = klass->method.first + i;
+                       guint32 cols [MONO_PARAM_SIZE];
+                       guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
+
+                       if (idx + 1 < methodt->rows)
+                               lastp = mono_metadata_decode_row_col (methodt, idx + 1, 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] & PARAM_ATTRIBUTE_HAS_FIELD_MARSHAL)
+                                       return TRUE;
+                       }
+                       return FALSE;
+               }
+       }
+       return FALSE;
+}
+
 gpointer
 mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
 {
@@ -533,3 +1103,113 @@ mono_method_get_wrapper_data (MonoMethod *method, guint32 id)
 
        return l->data;
 }
+
+static void
+default_stack_walk (MonoStackWalk func, gpointer user_data) {
+       g_error ("stack walk not installed");
+}
+
+static MonoStackWalkImpl stack_walk = default_stack_walk;
+
+void
+mono_stack_walk (MonoStackWalk func, gpointer user_data)
+{
+       stack_walk (func, user_data);
+}
+
+void
+mono_install_stack_walk (MonoStackWalkImpl func)
+{
+       stack_walk = func;
+}
+
+static gboolean
+last_managed (MonoMethod *m, gint no, gint ilo, gboolean managed, gpointer data)
+{
+       MonoMethod **dest = data;
+       *dest = m;
+       /*g_print ("In %s::%s [%d] [%d]\n", m->klass->name, m->name, no, ilo);*/
+
+       return managed;
+}
+
+MonoMethod*
+mono_method_get_last_managed (void)
+{
+       MonoMethod *m = NULL;
+       stack_walk (last_managed, &m);
+       return m;
+}
+
+#ifndef PLATFORM_WIN32
+/*
+ * This routine exists to load and init Wine due to the special Wine
+ * requirements: basically the SharedWineInit must be called before
+ * any modules are dlopened or they will fail to work.
+ */
+static void
+mono_loader_wine_init ()
+{
+       GModule *module = g_module_open ("winelib.exe.so", G_MODULE_BIND_LAZY);
+       int (*shared_wine_init)();
+
+       if (module == NULL){
+               fprintf (stderr, "Could not load winelib.exe.so");
+               return;
+       }
+
+       g_module_symbol (module, "SharedWineInit", &shared_wine_init);
+       if (shared_wine_init == NULL)
+               return;
+
+       shared_wine_init ();
+
+       return;
+}
+#endif
+
+void
+mono_loader_lock (void)
+{
+       EnterCriticalSection (&loader_mutex);
+}
+
+void
+mono_loader_unlock (void)
+{
+       LeaveCriticalSection (&loader_mutex);
+}
+
+MonoMethodSignature* 
+mono_method_signature (MonoMethod *method)
+{
+       return method->signature;
+}
+
+const char*
+mono_method_get_name (MonoMethod *method)
+{
+       return method->name;
+}
+
+MonoClass*
+mono_method_get_class (MonoMethod *method)
+{
+       return method->klass;
+}
+
+guint32
+mono_method_get_token (MonoMethod *method)
+{
+       return method->token;
+}
+
+guint32
+mono_method_get_flags (MonoMethod *method, guint32 *iflags)
+{
+       if (iflags)
+               *iflags = method->iflags;
+       return method->flags;
+}
+
+