2 * loader.c: Image Loader
5 * Paolo Molaro (lupus@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
8 * (C) 2001 Ximian, Inc.
10 * This file is used by the interpreter and the JIT engine to locate
11 * assemblies. Used to load AssemblyRef and later to resolve various
15 * This should keep track of the assembly versions that we are loading.
23 #include <mono/metadata/metadata.h>
24 #include <mono/metadata/image.h>
25 #include <mono/metadata/assembly.h>
26 #include <mono/metadata/tokentype.h>
27 #include <mono/metadata/cil-coff.h>
28 #include <mono/metadata/tabledefs.h>
29 #include <mono/metadata/loader.h>
30 #include <mono/metadata/class.h>
32 MonoDefaults mono_defaults;
34 static char *dll_map[] = {
37 "cygwin1.dll", "libc.so.6",
42 mono_map_dll (const char *name)
47 if (!strcmp (dll_map [i], name))
48 return dll_map [i + 1];
58 static gboolean initialized = FALSE;
60 enum MonoImageOpenStatus status = MONO_IMAGE_OK;
66 ass = mono_assembly_open (CORLIB_NAME, NULL, &status);
67 g_assert (status == MONO_IMAGE_OK);
68 g_assert (ass != NULL);
69 mono_defaults.corlib = ass->image;
71 mono_defaults.object_class = mono_class_from_name (
72 mono_defaults.corlib, "System", "Object");
73 g_assert (mono_defaults.object_class != 0);
75 mono_defaults.boolean_class = mono_class_from_name (
76 mono_defaults.corlib, "System", "Boolean");
77 g_assert (mono_defaults.boolean_class != 0);
79 mono_defaults.byte_class = mono_class_from_name (
80 mono_defaults.corlib, "System", "Byte");
81 g_assert (mono_defaults.byte_class != 0);
83 mono_defaults.sbyte_class = mono_class_from_name (
84 mono_defaults.corlib, "System", "SByte");
85 g_assert (mono_defaults.sbyte_class != 0);
87 mono_defaults.int16_class = mono_class_from_name (
88 mono_defaults.corlib, "System", "Int16");
89 g_assert (mono_defaults.int16_class != 0);
91 mono_defaults.uint16_class = mono_class_from_name (
92 mono_defaults.corlib, "System", "UInt16");
93 g_assert (mono_defaults.uint16_class != 0);
95 mono_defaults.int32_class = mono_class_from_name (
96 mono_defaults.corlib, "System", "Int32");
97 g_assert (mono_defaults.int32_class != 0);
99 mono_defaults.uint32_class = mono_class_from_name (
100 mono_defaults.corlib, "System", "UInt32");
101 g_assert (mono_defaults.uint32_class != 0);
103 mono_defaults.uint_class = mono_class_from_name (
104 mono_defaults.corlib, "System", "UIntPtr");
105 g_assert (mono_defaults.uint_class != 0);
107 mono_defaults.int_class = mono_class_from_name (
108 mono_defaults.corlib, "System", "IntPtr");
109 g_assert (mono_defaults.int_class != 0);
111 mono_defaults.int64_class = mono_class_from_name (
112 mono_defaults.corlib, "System", "Int64");
113 g_assert (mono_defaults.int64_class != 0);
115 mono_defaults.uint64_class = mono_class_from_name (
116 mono_defaults.corlib, "System", "UInt64");
117 g_assert (mono_defaults.uint64_class != 0);
119 mono_defaults.single_class = mono_class_from_name (
120 mono_defaults.corlib, "System", "Single");
121 g_assert (mono_defaults.single_class != 0);
123 mono_defaults.double_class = mono_class_from_name (
124 mono_defaults.corlib, "System", "Double");
125 g_assert (mono_defaults.double_class != 0);
127 mono_defaults.char_class = mono_class_from_name (
128 mono_defaults.corlib, "System", "Char");
129 g_assert (mono_defaults.char_class != 0);
131 mono_defaults.string_class = mono_class_from_name (
132 mono_defaults.corlib, "System", "String");
133 g_assert (mono_defaults.string_class != 0);
135 mono_defaults.enum_class = mono_class_from_name (
136 mono_defaults.corlib, "System", "Enum");
137 g_assert (mono_defaults.enum_class != 0);
139 mono_defaults.array_class = mono_class_from_name (
140 mono_defaults.corlib, "System", "Array");
141 g_assert (mono_defaults.array_class != 0);
144 static GHashTable *icall_hash = NULL;
147 mono_add_internal_call (const char *name, gpointer method)
150 icall_hash = g_hash_table_new (g_str_hash , g_str_equal);
152 g_hash_table_insert (icall_hash, g_strdup (name), method);
156 mono_lookup_internal_call (const char *name)
161 g_warning ("icall_hash not initialized");
162 g_assert_not_reached ();
165 if (!(res = g_hash_table_lookup (icall_hash, name))) {
166 g_warning ("cant resolve internal call to \"%s\"", name);
167 g_assert_not_reached ();
174 method_from_memberref (MonoImage *image, guint32 index)
178 MonoTableInfo *tables = image->tables;
180 guint32 nindex, class, i;
181 const char *mname, *name, *nspace;
182 MonoMethodSignature *sig;
185 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], index-1, cols, 3);
186 nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
187 class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
188 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
189 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
191 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
193 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
194 mono_metadata_decode_blob_size (ptr, &ptr);
195 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
198 case MEMBERREF_PARENT_TYPEREF: {
199 guint32 scopeindex, scopetable;
201 mono_metadata_decode_row (&tables [MONO_TABLE_TYPEREF], nindex-1, cols, MONO_TYPEREF_SIZE);
202 scopeindex = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
203 scopetable = cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK;
204 /*g_print ("typeref: 0x%x 0x%x %s.%s\n", scopetable, scopeindex,
205 mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAMESPACE]),
206 mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAME]));*/
207 switch (scopetable) {
208 case RESOLTION_SCOPE_ASSEMBLYREF:
210 * To find the method we have the following info:
211 * *) name and namespace of the class from the TYPEREF table
212 * *) name and signature of the method from the MEMBERREF table
214 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
215 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
217 /* this will triggered by references to mscorlib */
218 if (image->references [scopeindex-1] == NULL)
219 g_error ("Reference to mscorlib? Probably need to implement %s.%s::%s in corlib", nspace, name, mname);
221 mimage = image->references [scopeindex-1]->image;
223 klass = mono_class_from_name (mimage, nspace, name);
224 mono_class_metadata_init (klass);
226 /* mostly dumb search for now */
227 for (i = 0; i < klass->method.count; ++i) {
228 MonoMethod *m = klass->methods [i];
229 if (!strcmp (mname, m->name)) {
230 if (mono_metadata_signature_equal (image, sig, mimage, m->signature)) {
231 mono_metadata_free_method_signature (sig);
236 g_warning ("can't find method %s.%s::%s", nspace, name, mname);
237 g_assert_not_reached ();
240 g_assert_not_reached ();
244 case MEMBERREF_PARENT_TYPESPEC: {
245 guint32 bcols [MONO_TYPESPEC_SIZE];
250 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
251 bcols, MONO_TYPESPEC_SIZE);
252 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
253 len = mono_metadata_decode_value (ptr, &ptr);
254 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
256 if (type->type != MONO_TYPE_ARRAY)
257 g_assert_not_reached ();
259 result = (MonoMethod *)g_new0 (MonoMethod, 1);
260 result->klass = mono_defaults.array_class;
261 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
262 result->signature = sig;
264 if (!strcmp (mname, ".ctor")) {
265 g_assert (sig->hasthis);
266 if (type->data.array->rank == sig->param_count) {
267 result->addr = mono_lookup_internal_call ("__array_ctor");
269 } else if ((type->data.array->rank * 2) == sig->param_count) {
270 result->addr = mono_lookup_internal_call ("__array_bound_ctor");
273 g_assert_not_reached ();
276 if (!strcmp (mname, "Set")) {
277 g_assert (sig->hasthis);
278 g_assert (type->data.array->rank + 1 == sig->param_count);
280 result->addr = mono_lookup_internal_call ("__array_Set");
284 if (!strcmp (mname, "Get")) {
285 g_assert (sig->hasthis);
286 g_assert (type->data.array->rank == sig->param_count);
288 result->addr = mono_lookup_internal_call ("__array_Get");
292 g_assert_not_reached ();
296 g_assert_not_reached ();
303 ves_map_ffi_type (MonoType *type)
307 switch (type->type) {
309 rettype = &ffi_type_sint8;
311 case MONO_TYPE_BOOLEAN:
313 rettype = &ffi_type_uint8;
316 rettype = &ffi_type_sint16;
320 rettype = &ffi_type_uint16;
323 rettype = &ffi_type_sint32;
326 rettype = &ffi_type_sint32;
329 rettype = &ffi_type_sint;
332 rettype = &ffi_type_sint;
335 rettype = &ffi_type_sint64;
338 rettype = &ffi_type_pointer;
341 rettype = &ffi_type_float;
344 rettype = &ffi_type_double;
346 case MONO_TYPE_STRING:
347 rettype = &ffi_type_pointer;
350 rettype = &ffi_type_void;
353 g_warning ("not implemented 0x%02x", type->type);
354 g_assert_not_reached ();
361 fill_pinvoke_info (MonoImage *image, MonoMethodPInvoke *piinfo, int index)
363 MonoMethod *mh = &piinfo->method;
364 MonoTableInfo *tables = image->tables;
365 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
366 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
369 const char *import = NULL;
370 const char *scope = NULL;
373 ffi_type **args, *rettype;
376 for (i = 0; i < im->rows; i++) {
378 mono_metadata_decode_row (im, i, im_cols, 4);
380 if ((im_cols[1] >> 1) == index + 1) {
382 import = mono_metadata_string_heap (image, im_cols [2]);
384 mono_metadata_decode_row (mr, im_cols [3] - 1, mr_cols,
387 scope = mono_metadata_string_heap (image, mr_cols [0]);
391 g_assert (import && scope);
393 scope = mono_map_dll (scope);
394 full_name = g_module_build_path (NULL, scope);
395 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
398 g_error ("Failed to load library %s (%s)", full_name, scope);
401 piinfo->cif = g_new (ffi_cif , 1);
402 piinfo->piflags = im_cols [0];
404 g_module_symbol (gmodule, import, &mh->addr);
408 acount = mh->signature->param_count;
410 args = g_new (ffi_type *, acount);
412 for (i = 0; i < acount; i++)
413 args[i] = ves_map_ffi_type (mh->signature->params [i]);
415 rettype = ves_map_ffi_type (mh->signature->ret);
417 if (!ffi_prep_cif (piinfo->cif, FFI_DEFAULT_ABI, acount, rettype,
419 g_warning ("prepare pinvoke failed");
420 g_assert_not_reached ();
425 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
428 int table = mono_metadata_token_table (token);
429 int index = mono_metadata_token_index (token);
430 MonoTableInfo *tables = image->tables;
431 const char *loc, *sig = NULL;
434 guint32 cols [MONO_TYPEDEF_SIZE];
436 if (table == MONO_TABLE_METHOD && (result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
439 if (table != MONO_TABLE_METHOD) {
440 g_assert (table == MONO_TABLE_MEMBERREF);
441 return method_from_memberref (image, index);
444 mono_metadata_decode_row (&tables [table], index - 1, cols, 6);
446 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
447 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
448 MonoAssembly *corlib;
450 guint32 tdcols [MONO_TYPEDEF_SIZE];
452 tdef = mono_metadata_typedef_from_method (image, index - 1) - 1;
454 mono_metadata_decode_row (t, tdef, tdcols, MONO_TYPEDEF_SIZE);
456 name = g_strconcat (mono_metadata_string_heap (image, tdcols [MONO_TYPEDEF_NAMESPACE]), ".",
457 mono_metadata_string_heap (image, tdcols [MONO_TYPEDEF_NAME]), "::",
458 mono_metadata_string_heap (image, cols [MONO_METHOD_NAME]), NULL);
460 corlib = mono_assembly_open (CORLIB_NAME, NULL, NULL);
462 /* all internal calls must be inside corlib */
463 g_assert (corlib->image == image);
465 result = (MonoMethod *)g_new0 (MonoMethod, 1);
467 result->addr = mono_lookup_internal_call (name);
471 g_assert (result->addr != NULL);
473 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
475 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
478 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
481 result->klass = klass;
482 result->flags = cols [2];
483 result->iflags = cols [1];
484 result->name = mono_metadata_string_heap (image, cols [3]);
486 if (!sig) /* already taken from the methodref */
487 sig = mono_metadata_blob_heap (image, cols [4]);
488 size = mono_metadata_decode_blob_size (sig, &sig);
489 result->signature = mono_metadata_parse_method_signature (image, 0, sig, NULL);
491 if (!result->klass) {
492 guint32 type = mono_metadata_typedef_from_method (image, token);
493 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
496 if (result->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
497 fill_pinvoke_info (image, (MonoMethodPInvoke *)result,
499 } else if (!(result->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
500 /* if this is a methodref from another module/assembly, this fails */
501 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
503 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
505 ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
509 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
515 mono_free_method (MonoMethod *method)
517 mono_metadata_free_method_signature (method->signature);
518 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
519 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
520 g_free (piinfo->cif->arg_types);
521 g_free (piinfo->cif);
522 } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
523 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);