2 * loader.c: Image Loader
5 * Paolo Molaro (lupus@ximian.com)
6 * Miguel de Icaza (miguel@ximian.com)
7 * Patrik Torstensson (patrik.torstensson@labs2.com)
9 * (C) 2001 Ximian, Inc.
11 * This file is used by the interpreter and the JIT engine to locate
12 * assemblies. Used to load AssemblyRef and later to resolve various
16 * This should keep track of the assembly versions that we are loading.
25 #include <mono/metadata/metadata.h>
26 #include <mono/metadata/image.h>
27 #include <mono/metadata/assembly.h>
28 #include <mono/metadata/tokentype.h>
29 #include <mono/metadata/cil-coff.h>
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/loader.h>
32 #include <mono/metadata/class.h>
33 #include <mono/metadata/debug-helpers.h>
35 static gboolean dummy_icall = TRUE;
37 MonoDefaults mono_defaults;
40 #define mono_map_dll(name) (name)
42 static const char *dll_map[] = {
45 "cygwin1.dll", "libc.so.6",
50 mono_map_dll (const char *name)
55 if (!strcmp (dll_map [i], name))
56 return dll_map [i + 1];
64 static GHashTable *icall_hash = NULL;
67 mono_add_internal_call (const char *name, gconstpointer method)
71 icall_hash = g_hash_table_new (g_str_hash , g_str_equal);
74 g_hash_table_insert (icall_hash, g_strdup (name), method);
78 ves_icall_dummy (void)
80 g_warning ("the mono runtime is not initialized");
81 g_assert_not_reached ();
85 mono_lookup_internal_call (MonoMethod *method)
92 return ves_icall_dummy;
95 g_warning ("can't resolve internal call, method is null");
99 g_warning ("icall_hash not initialized");
100 g_assert_not_reached ();
103 name = g_strconcat (method->klass->name_space, ".", method->klass->name, "::", method->name, NULL);
104 if (!(res = g_hash_table_lookup (icall_hash, name))) {
105 // trying to resolve with full signature
108 tmpsig = mono_signature_get_desc(method->signature, TRUE);
109 name = g_strconcat (method->klass->name_space, ".", method->klass->name, "::", method->name, "(", tmpsig, ")", NULL);
110 if (!(res = g_hash_table_lookup (icall_hash, name))) {
111 g_warning ("cant resolve internal call to \"%s\" (tested without signature also)", name);
112 g_print ("\nYour mono runtime and corlib are out of sync.\n");
113 g_print ("When you update one from cvs you need to update, compile and install\nthe other too.\n");
114 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");
115 g_print ("If you see other errors or faults after this message they are probably related\n");
116 g_print ("and you need to fix your mono install first.\n");
133 mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass)
136 MonoTableInfo *tables = image->tables;
138 guint32 nindex, class;
141 guint32 idx = mono_metadata_token_index (token);
143 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
144 nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
145 class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
147 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
149 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
150 mono_metadata_decode_blob_size (ptr, &ptr);
151 /* we may want to check the signature here... */
154 case MEMBERREF_PARENT_TYPEREF:
155 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
157 g_warning ("Missing field %s in typeref index %d", fname, nindex);
160 mono_class_init (klass);
163 return mono_class_get_field_from_name (klass, fname);
170 find_method (MonoClass *klass, const char* name, MonoMethodSignature *sig)
174 /* mostly dumb search for now */
175 for (i = 0; i < klass->method.count; ++i) {
176 MonoMethod *m = klass->methods [i];
177 if (!strcmp (name, m->name)) {
178 if (mono_metadata_signature_equal (sig, m->signature))
182 klass = klass->parent;
189 method_from_memberref (MonoImage *image, guint32 idx)
193 MonoTableInfo *tables = image->tables;
195 guint32 nindex, class;
197 MonoMethodSignature *sig;
200 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
201 nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
202 class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
203 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
204 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
206 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
208 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
209 mono_metadata_decode_blob_size (ptr, &ptr);
210 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
213 case MEMBERREF_PARENT_TYPEREF:
214 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
216 g_warning ("Missing method %s in typeref index %d", mname, nindex);
217 mono_metadata_free_method_signature (sig);
220 mono_class_init (klass);
221 method = find_method (klass, mname, sig);
223 g_warning ("Missing method %s in typeref index %d", mname, nindex);
224 mono_metadata_free_method_signature (sig);
226 case MEMBERREF_PARENT_TYPESPEC: {
227 guint32 bcols [MONO_TYPESPEC_SIZE];
232 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
233 bcols, MONO_TYPESPEC_SIZE);
234 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
235 len = mono_metadata_decode_value (ptr, &ptr);
236 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
238 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY)
239 g_assert_not_reached ();
241 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
242 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
243 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
244 result->signature = sig;
245 result->name = mname;
247 if (!strcmp (mname, ".ctor")) {
248 /* we special-case this in the runtime. */
253 if (!strcmp (mname, "Set")) {
254 g_assert (sig->hasthis);
255 g_assert (type->data.array->rank + 1 == sig->param_count);
257 result->addr = mono_lookup_internal_call(result);
261 if (!strcmp (mname, "Get")) {
262 g_assert (sig->hasthis);
263 g_assert (type->data.array->rank == sig->param_count);
265 result->addr = mono_lookup_internal_call(result);
269 if (!strcmp (mname, "Address")) {
270 g_assert (sig->hasthis);
271 g_assert (type->data.array->rank == sig->param_count);
273 result->addr = mono_lookup_internal_call(result);
277 g_assert_not_reached ();
281 g_assert_not_reached ();
288 fill_pinvoke_info (MonoImage *image, MonoMethodPInvoke *piinfo, int idx)
290 MonoMethod *mh = &piinfo->method;
291 MonoTableInfo *tables = image->tables;
292 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
293 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
296 const char *import = NULL;
297 const char *scope = NULL;
302 for (i = 0; i < im->rows; i++) {
304 mono_metadata_decode_row (im, i, im_cols, 4);
306 if ((im_cols[1] >> 1) == idx + 1) {
308 import = mono_metadata_string_heap (image, im_cols [2]);
310 mono_metadata_decode_row (mr, im_cols [3] - 1, mr_cols,
313 scope = mono_metadata_string_heap (image, mr_cols [0]);
317 piinfo->piflags = im_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);
327 if (!(gmodule=g_module_open (scope, G_MODULE_BIND_LAZY))) {
328 g_warning ("Failed to load library %s (%s)", full_name, scope);
335 g_module_symbol (gmodule, import, &mh->addr);
338 g_warning ("Failed to load function %s from %s", import, scope);
342 mh->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
346 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
349 int table = mono_metadata_token_table (token);
350 int idx = mono_metadata_token_index (token);
351 MonoTableInfo *tables = image->tables;
352 const char *loc, *sig = NULL;
354 guint32 cols [MONO_TYPEDEF_SIZE];
356 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
359 if (table != MONO_TABLE_METHOD) {
360 if (table != MONO_TABLE_MEMBERREF)
361 g_print("got wrong token: 0x%08x\n", token);
362 g_assert (table == MONO_TABLE_MEMBERREF);
363 result = method_from_memberref (image, idx);
364 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
368 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
370 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
371 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
372 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
374 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
377 result->klass = klass;
378 result->flags = cols [2];
379 result->iflags = cols [1];
380 result->name = mono_metadata_string_heap (image, cols [3]);
382 if (!sig) /* already taken from the methodref */
383 sig = mono_metadata_blob_heap (image, cols [4]);
384 size = mono_metadata_decode_blob_size (sig, &sig);
385 result->signature = mono_metadata_parse_method_signature (image, 0, sig, NULL);
387 if (!result->klass) {
388 guint32 type = mono_metadata_typedef_from_method (image, token);
389 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
392 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
393 result->addr = mono_lookup_internal_call (result);
394 result->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
395 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
396 fill_pinvoke_info (image, (MonoMethodPInvoke *)result, idx - 1);
398 /* if this is a methodref from another module/assembly, this fails */
399 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
401 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
402 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
404 ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
408 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
414 mono_free_method (MonoMethod *method)
416 mono_metadata_free_method_signature (method->signature);
417 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
418 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
419 g_free (piinfo->code);
420 } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
421 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
428 mono_method_get_param_names (MonoMethod *method, const char **names)
431 MonoClass *klass = method->klass;
432 MonoTableInfo *methodt = &klass->image->tables [MONO_TABLE_METHOD];
433 MonoTableInfo *paramt = &klass->image->tables [MONO_TABLE_PARAM];
435 if (!method->signature->param_count)
437 for (i = 0; i < method->signature->param_count; ++i)
440 mono_class_init (klass);
444 for (i = 0; i < klass->method.count; ++i) {
445 if (method == klass->methods [i]) {
446 guint32 idx = klass->method.first + i;
447 guint32 cols [MONO_PARAM_SIZE];
448 guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
450 if (idx + 1 < methodt->rows)
451 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
453 lastp = paramt->rows;
454 for (i = param_index; i < lastp; ++i) {
455 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
456 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
457 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);