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);
128 mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass)
131 MonoTableInfo *tables = image->tables;
133 guint32 nindex, class;
136 guint32 idx = mono_metadata_token_index (token);
138 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, MONO_MEMBERREF_SIZE);
139 nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
140 class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
142 fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
144 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
145 mono_metadata_decode_blob_size (ptr, &ptr);
146 /* we may want to check the signature here... */
149 case MEMBERREF_PARENT_TYPEREF:
150 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
152 g_warning ("Missing field %s in typeref index %d", fname, nindex);
155 mono_class_init (klass);
158 return mono_class_get_field_from_name (klass, fname);
165 find_method (MonoClass *klass, const char* name, MonoMethodSignature *sig)
169 /* mostly dumb search for now */
170 for (i = 0; i < klass->method.count; ++i) {
171 MonoMethod *m = klass->methods [i];
172 if (!strcmp (name, m->name)) {
173 if (mono_metadata_signature_equal (sig, m->signature))
177 klass = klass->parent;
184 method_from_memberref (MonoImage *image, guint32 idx)
188 MonoTableInfo *tables = image->tables;
190 guint32 nindex, class;
192 MonoMethodSignature *sig;
195 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], idx-1, cols, 3);
196 nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
197 class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
198 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
199 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
201 mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
203 ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
204 mono_metadata_decode_blob_size (ptr, &ptr);
205 sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
208 case MEMBERREF_PARENT_TYPEREF:
209 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
211 g_warning ("Missing method %s in typeref index %d", mname, nindex);
212 mono_metadata_free_method_signature (sig);
215 mono_class_init (klass);
216 method = find_method (klass, mname, sig);
218 g_warning ("Missing method %s in typeref index %d", mname, nindex);
219 mono_metadata_free_method_signature (sig);
221 case MEMBERREF_PARENT_TYPESPEC: {
222 guint32 bcols [MONO_TYPESPEC_SIZE];
227 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
228 bcols, MONO_TYPESPEC_SIZE);
229 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
230 len = mono_metadata_decode_value (ptr, &ptr);
231 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
233 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY)
234 g_assert_not_reached ();
236 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
237 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_SPEC | nindex);
238 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
239 result->signature = sig;
240 result->name = mname;
242 if (!strcmp (mname, ".ctor")) {
243 /* we special-case this in the runtime. */
248 if (!strcmp (mname, "Set")) {
249 g_assert (sig->hasthis);
250 g_assert (type->data.array->rank + 1 == sig->param_count);
252 result->addr = mono_lookup_internal_call(result);
256 if (!strcmp (mname, "Get")) {
257 g_assert (sig->hasthis);
258 g_assert (type->data.array->rank == sig->param_count);
260 result->addr = mono_lookup_internal_call(result);
264 if (!strcmp (mname, "Address")) {
265 g_assert (sig->hasthis);
266 g_assert (type->data.array->rank == sig->param_count);
268 result->addr = mono_lookup_internal_call(result);
272 g_assert_not_reached ();
276 g_assert_not_reached ();
283 fill_pinvoke_info (MonoImage *image, MonoMethodPInvoke *piinfo, int idx)
285 MonoMethod *mh = &piinfo->method;
286 MonoTableInfo *tables = image->tables;
287 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
288 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
291 const char *import = NULL;
292 const char *scope = NULL;
297 for (i = 0; i < im->rows; i++) {
299 mono_metadata_decode_row (im, i, im_cols, 4);
301 if ((im_cols[1] >> 1) == idx + 1) {
303 import = mono_metadata_string_heap (image, im_cols [2]);
305 mono_metadata_decode_row (mr, im_cols [3] - 1, mr_cols,
308 scope = mono_metadata_string_heap (image, mr_cols [0]);
312 piinfo->piflags = im_cols [0];
314 g_assert (import && scope);
316 scope = mono_map_dll (scope);
317 full_name = g_module_build_path (NULL, scope);
318 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
322 if (!(gmodule=g_module_open (scope, G_MODULE_BIND_LAZY))) {
323 g_warning ("Failed to load library %s (%s)", full_name, scope);
330 g_module_symbol (gmodule, import, &mh->addr);
333 g_warning ("Failed to load function %s from %s", import, scope);
337 mh->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
341 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
344 int table = mono_metadata_token_table (token);
345 int idx = mono_metadata_token_index (token);
346 MonoTableInfo *tables = image->tables;
347 const char *loc, *sig = NULL;
349 guint32 cols [MONO_TYPEDEF_SIZE];
351 if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
354 if (table != MONO_TABLE_METHOD) {
355 if (table != MONO_TABLE_MEMBERREF)
356 g_print("got wrong token: 0x%08x\n", token);
357 g_assert (table == MONO_TABLE_MEMBERREF);
358 result = method_from_memberref (image, idx);
359 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
363 mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
365 if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
366 (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
367 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
369 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
372 result->klass = klass;
373 result->flags = cols [2];
374 result->iflags = cols [1];
375 result->name = mono_metadata_string_heap (image, cols [3]);
377 if (!sig) /* already taken from the methodref */
378 sig = mono_metadata_blob_heap (image, cols [4]);
379 size = mono_metadata_decode_blob_size (sig, &sig);
380 result->signature = mono_metadata_parse_method_signature (image, 0, sig, NULL);
382 if (!result->klass) {
383 guint32 type = mono_metadata_typedef_from_method (image, token);
384 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
387 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
388 result->addr = mono_lookup_internal_call (result);
389 result->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
390 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
391 fill_pinvoke_info (image, (MonoMethodPInvoke *)result, idx - 1);
393 /* if this is a methodref from another module/assembly, this fails */
394 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
396 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
397 !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
399 ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
403 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
409 mono_free_method (MonoMethod *method)
411 mono_metadata_free_method_signature (method->signature);
412 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
413 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
414 g_free (piinfo->code);
415 } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
416 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
423 mono_method_get_param_names (MonoMethod *method, const char **names)
426 MonoClass *klass = method->klass;
427 MonoTableInfo *methodt = &klass->image->tables [MONO_TABLE_METHOD];
428 MonoTableInfo *paramt = &klass->image->tables [MONO_TABLE_PARAM];
430 if (!method->signature->param_count)
432 for (i = 0; i < method->signature->param_count; ++i)
435 mono_class_init (klass);
439 for (i = 0; i < klass->method.count; ++i) {
440 if (method == klass->methods [i]) {
441 guint32 idx = klass->method.first + i;
442 guint32 cols [MONO_PARAM_SIZE];
443 guint param_index = mono_metadata_decode_row_col (methodt, idx, MONO_METHOD_PARAMLIST);
445 if (idx + 1 < methodt->rows)
446 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
448 lastp = paramt->rows;
449 for (i = param_index; i < lastp; ++i) {
450 mono_metadata_decode_row (paramt, i -1, cols, MONO_PARAM_SIZE);
451 if (cols [MONO_PARAM_SEQUENCE]) /* skip return param spec */
452 names [cols [MONO_PARAM_SEQUENCE] - 1] = mono_metadata_string_heap (klass->image, cols [MONO_PARAM_NAME]);