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.array_class = mono_class_from_name (
72 mono_defaults.corlib, "System", "Array");
73 g_assert (mono_defaults.array_class != 0);
75 mono_defaults.char_class = mono_class_from_name (
76 mono_defaults.corlib, "System", "Char");
77 g_assert (mono_defaults.char_class != 0);
79 mono_defaults.string_class = mono_class_from_name (
80 mono_defaults.corlib, "System", "String");
81 g_assert (mono_defaults.string_class != 0);
85 static GHashTable *icall_hash = NULL;
88 mono_add_internal_call (const char *name, gpointer method)
91 icall_hash = g_hash_table_new (g_str_hash , g_str_equal);
93 g_hash_table_insert (icall_hash, g_strdup (name), method);
97 mono_lookup_internal_call (const char *name)
102 g_warning ("icall_hash not initialized");
103 g_assert_not_reached ();
106 if (!(res = g_hash_table_lookup (icall_hash, name))) {
107 g_warning ("cant resolve internal call to \"%s\"", name);
108 g_assert_not_reached ();
115 method_from_memberref (MonoImage *image, guint32 index)
119 MonoTableInfo *tables = image->tables;
121 guint32 nindex, class, i;
122 const char *mname, *name, *nspace;
123 MonoMethodSignature *sig;
126 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], index-1, cols, 3);
127 nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
128 class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
129 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
130 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
132 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
134 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
135 mono_metadata_decode_blob_size (ptr, &ptr);
136 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
139 case MEMBERREF_PARENT_TYPEREF: {
140 guint32 scopeindex, scopetable;
142 mono_metadata_decode_row (&tables [MONO_TABLE_TYPEREF], nindex-1, cols, MONO_TYPEREF_SIZE);
143 scopeindex = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
144 scopetable = cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK;
145 /*g_print ("typeref: 0x%x 0x%x %s.%s\n", scopetable, scopeindex,
146 mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAMESPACE]),
147 mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAME]));*/
148 switch (scopetable) {
149 case RESOLTION_SCOPE_ASSEMBLYREF:
151 * To find the method we have the following info:
152 * *) name and namespace of the class from the TYPEREF table
153 * *) name and signature of the method from the MEMBERREF table
155 nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
156 name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
158 /* this will triggered by references to mscorlib */
159 g_assert (image->references [scopeindex-1] != NULL);
161 mimage = image->references [scopeindex-1]->image;
163 klass = mono_class_from_name (mimage, nspace, name);
164 mono_class_metadata_init (klass);
166 /* mostly dumb search for now */
167 for (i = 0; i < klass->method.count; ++i) {
168 MonoMethod *m = klass->methods [i];
169 if (!strcmp (mname, m->name)) {
170 if (mono_metadata_signature_equal (image, sig, mimage, m->signature)) {
171 mono_metadata_free_method_signature (sig);
176 g_warning ("can't find method %s.%s::%s", nspace, name, mname);
177 g_assert_not_reached ();
180 g_assert_not_reached ();
184 case MEMBERREF_PARENT_TYPESPEC: {
185 guint32 bcols [MONO_TYPESPEC_SIZE];
190 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
191 bcols, MONO_TYPESPEC_SIZE);
192 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
193 len = mono_metadata_decode_value (ptr, &ptr);
194 type = mono_metadata_parse_type (image, ptr, &ptr);
196 if (type->type != MONO_TYPE_ARRAY)
197 g_assert_not_reached ();
199 result = (MonoMethod *)g_new0 (MonoMethod, 1);
200 result->klass = mono_defaults.array_class;
201 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
202 result->signature = sig;
204 if (!strcmp (mname, ".ctor")) {
205 g_assert (sig->hasthis);
206 if (type->data.array->rank == sig->param_count) {
207 result->addr = mono_lookup_internal_call ("__array_ctor");
209 } else if ((type->data.array->rank * 2) == sig->param_count) {
210 result->addr = mono_lookup_internal_call ("__array_bound_ctor");
213 g_assert_not_reached ();
216 if (!strcmp (mname, "Set")) {
217 g_assert (sig->hasthis);
218 g_assert (type->data.array->rank + 1 == sig->param_count);
220 result->addr = mono_lookup_internal_call ("__array_Set");
224 if (!strcmp (mname, "Get")) {
225 g_assert (sig->hasthis);
226 g_assert (type->data.array->rank == sig->param_count);
228 result->addr = mono_lookup_internal_call ("__array_Get");
232 g_assert_not_reached ();
236 g_assert_not_reached ();
243 ves_map_ffi_type (MonoType *type)
247 switch (type->type) {
249 rettype = &ffi_type_sint8;
251 case MONO_TYPE_BOOLEAN:
253 rettype = &ffi_type_uint8;
256 rettype = &ffi_type_sint16;
260 rettype = &ffi_type_uint16;
263 rettype = &ffi_type_sint32;
266 rettype = &ffi_type_sint32;
269 rettype = &ffi_type_float;
272 rettype = &ffi_type_double;
274 case MONO_TYPE_STRING:
275 rettype = &ffi_type_pointer;
278 rettype = &ffi_type_void;
281 g_warning ("not implemented");
282 g_assert_not_reached ();
289 fill_pinvoke_info (MonoImage *image, MonoMethodPInvoke *piinfo, int index)
291 MonoMethod *mh = &piinfo->method;
292 MonoTableInfo *tables = image->tables;
293 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
294 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
297 const char *import = NULL;
298 const char *scope = NULL;
301 ffi_type **args, *rettype;
304 for (i = 0; i < im->rows; i++) {
306 mono_metadata_decode_row (im, i, im_cols, 4);
308 if ((im_cols[1] >> 1) == index + 1) {
310 import = mono_metadata_string_heap (image, im_cols [2]);
312 mono_metadata_decode_row (mr, im_cols [3] - 1, mr_cols,
315 scope = mono_metadata_string_heap (image, mr_cols [0]);
319 g_assert (import && scope);
321 scope = mono_map_dll (scope);
322 full_name = g_module_build_path (NULL, scope);
323 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
328 piinfo->cif = g_new (ffi_cif , 1);
329 piinfo->piflags = im_cols [0];
331 g_module_symbol (gmodule, import, &mh->addr);
335 acount = mh->signature->param_count;
337 args = g_new (ffi_type *, acount);
339 for (i = 0; i < acount; i++)
340 args[i] = ves_map_ffi_type (mh->signature->params [i]->type);
342 rettype = ves_map_ffi_type (mh->signature->ret->type);
344 if (!ffi_prep_cif (piinfo->cif, FFI_DEFAULT_ABI, acount, rettype,
346 g_warning ("prepare pinvoke failed");
347 g_assert_not_reached ();
352 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
355 int table = mono_metadata_token_table (token);
356 int index = mono_metadata_token_index (token);
357 MonoTableInfo *tables = image->tables;
358 const char *loc, *sig = NULL;
361 guint32 cols [MONO_TYPEDEF_SIZE];
363 if (table == MONO_TABLE_METHOD && (result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
366 if (table != MONO_TABLE_METHOD) {
367 g_assert (table == MONO_TABLE_MEMBERREF);
368 return method_from_memberref (image, index);
371 mono_metadata_decode_row (&tables [table], index - 1, cols, 6);
373 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
374 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
375 MonoAssembly *corlib;
377 guint32 tdcols [MONO_TYPEDEF_SIZE];
379 tdef = mono_metadata_typedef_from_method (image, index - 1) - 1;
381 mono_metadata_decode_row (t, tdef, tdcols, MONO_TYPEDEF_SIZE);
383 name = g_strconcat (mono_metadata_string_heap (image, tdcols [MONO_TYPEDEF_NAMESPACE]), ".",
384 mono_metadata_string_heap (image, tdcols [MONO_TYPEDEF_NAME]), "::",
385 mono_metadata_string_heap (image, cols [MONO_METHOD_NAME]), NULL);
387 corlib = mono_assembly_open (CORLIB_NAME, NULL, NULL);
389 /* all internal calls must be inside corlib */
390 g_assert (corlib->image == image);
392 result = (MonoMethod *)g_new0 (MonoMethod, 1);
394 result->addr = mono_lookup_internal_call (name);
398 g_assert (result->addr != NULL);
400 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
402 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
405 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
408 result->klass = klass;
409 result->flags = cols [2];
410 result->iflags = cols [1];
411 result->name = mono_metadata_string_heap (image, cols [3]);
413 if (!sig) /* already taken from the methodref */
414 sig = mono_metadata_blob_heap (image, cols [4]);
415 size = mono_metadata_decode_blob_size (sig, &sig);
416 result->signature = mono_metadata_parse_method_signature (image, 0, sig, NULL);
418 if (result->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
419 fill_pinvoke_info (image, (MonoMethodPInvoke *)result,
421 } else if (!(result->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
422 /* if this is a methodref from another module/assembly, this fails */
423 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
425 if (!result->klass) {
426 guint32 type = mono_metadata_typedef_from_method (image, token);
427 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
432 ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
435 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
441 mono_free_method (MonoMethod *method)
443 mono_metadata_free_method_signature (method->signature);
444 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
445 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
446 g_free (piinfo->cif->arg_types);
447 g_free (piinfo->cif);
448 } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
449 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);