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>
32 static char *dll_map[] = {
35 "cygwin1.dll", "libc.so.6",
40 mono_map_dll (const char *name)
45 if (!strcmp (dll_map [i], name))
46 return dll_map [i + 1];
54 mono_typedef_from_name (MonoImage *image, const char *name,
55 const char *nspace, guint32 *mlist)
57 MonoMetadata *m = &image->metadata;
58 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
60 guint32 cols [MONO_TYPEDEF_SIZE];
62 for (i=0; i < t->rows; ++i) {
63 mono_metadata_decode_row (t, i, cols, MONO_TYPEDEF_SIZE);
64 if (strcmp (name, mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAME])) == 0
65 && strcmp (nspace, mono_metadata_string_heap (m, cols [MONO_TYPEDEF_NAMESPACE])) == 0) {
67 *mlist = cols [MONO_TYPEDEF_METHOD_LIST];
68 return MONO_TOKEN_TYPE_DEF | (i + 1);
71 g_assert_not_reached ();
78 static MonoImage *corlib = NULL;
80 enum MonoImageOpenStatus status = MONO_IMAGE_OK;
83 ass = mono_assembly_open (CORLIB_NAME, NULL, &status);
84 g_assert (status == MONO_IMAGE_OK);
85 g_assert (ass != NULL);
86 g_assert (ass->image != NULL);
95 * mono_get_array_class_info:
96 * @ttoken: pointer to location to store type definition token
97 * @cl: pointer where image will be stored
99 * This routine locates information about the System.Array class. A reference
100 * to the image containing the class is returned in @cl. The type definition
101 * token is returned in @ttoken.
104 mono_get_array_class_info (guint *ttoken, MonoImage **cl)
106 static guint32 arr_token = 0;
107 static MonoImage *corlib;
110 corlib = mono_get_corlib ();
111 arr_token = mono_typedef_from_name (corlib, "Array", "System", NULL);
112 g_assert (arr_token != 0);
120 * mono_get_string_class_info:
121 * @ttoken: pointer to location to store type definition token
122 * @cl: pointer where image will be stored
124 * This routine locates information about the System.String class. A reference
125 * to the image containing the class is returned in @cl. The type definition
126 * token is returned in @ttoken.
128 * Returns: the method definition token for System.String::.ctor (char *)
132 mono_get_string_class_info (guint *ttoken, MonoImage **cl)
134 static guint32 ctor = 0, tt = 0;
135 static MonoImage *corlib;
138 guint32 cols [MAX (MONO_TYPEDEF_SIZE, MONO_METHOD_SIZE)];
139 guint32 ncols [MONO_TYPEDEF_SIZE];
140 guint32 i, first = 0, last = 0;
141 const char *name, *nspace;
149 *cl = corlib = mono_get_corlib ();
151 m = &corlib->metadata;
152 t = &m->tables [MONO_TABLE_TYPEDEF];
154 for (i = 0; i < t->rows; i++) {
155 mono_metadata_decode_row (t, i, cols, MONO_TYPEDEF_SIZE);
156 name = mono_metadata_string_heap (m, cols[1]);
157 nspace = mono_metadata_string_heap (m, cols[2]);
159 if (((cols [0] & TYPE_ATTRIBUTE_CLASS_SEMANTIC_MASK) == TYPE_ATTRIBUTE_CLASS) &&
160 !strcmp (nspace, "System") && !strcmp (name, "String")) {
162 *ttoken = tt = MONO_TOKEN_TYPE_DEF | (i + 1);
164 first = cols [5] - 1;
166 if (i + 1 < t->rows) {
167 mono_metadata_decode_row (t, i + 1, ncols,
169 last = ncols [5] - 1;
171 last = m->tables [MONO_TABLE_METHOD].rows;
176 g_assert (last - first > 0);
178 t = &m->tables [MONO_TABLE_METHOD];
179 g_assert (last < t->rows);
181 for (i = first; i < last; i++) {
184 guint8 sig[] = { 0x20, 0x01, 0x01, 0x0f, 0x03 };
185 mono_metadata_decode_row (t, i, cols, MONO_METHOD_SIZE);
187 if (!strcmp (mono_metadata_string_heap (m, cols [3]),
189 (cols [2] & METHOD_ATTRIBUTE_SPECIAL_NAME)) {
191 ptr = mono_metadata_blob_heap (m, cols [4]);
192 len = mono_metadata_decode_value (ptr, &ptr);
194 if (len == 5 && !memcmp (ptr, sig, len)) {
195 ctor = MONO_TOKEN_METHOD_DEF | (i + 1);
206 method_from_memberref (MonoImage *image, guint32 index)
209 MonoMetadata *m = &image->metadata;
210 MonoTableInfo *tables = m->tables;
212 guint32 nindex, class, i;
213 const char *mname, *name, *nspace;
214 MonoMethodSignature *sig, *msig;
217 mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], index-1, cols, 3);
218 nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
219 class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
220 /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
221 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
223 mname = mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]);
225 ptr = mono_metadata_blob_heap (m, cols [MONO_MEMBERREF_SIGNATURE]);
226 mono_metadata_decode_blob_size (ptr, &ptr);
227 sig = mono_metadata_parse_method_signature (m, 0, ptr, NULL);
230 case MEMBERREF_PARENT_TYPEREF: {
231 guint32 scopeindex, scopetable;
233 mono_metadata_decode_row (&tables [MONO_TABLE_TYPEREF], nindex-1, cols, MONO_TYPEREF_SIZE);
234 scopeindex = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
235 scopetable = cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK;
236 /*g_print ("typeref: 0x%x 0x%x %s.%s\n", scopetable, scopeindex,
237 mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAMESPACE]),
238 mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAME]));*/
239 switch (scopetable) {
240 case RESOLTION_SCOPE_ASSEMBLYREF:
242 * To find the method we have the following info:
243 * *) name and namespace of the class from the TYPEREF table
244 * *) name and signature of the method from the MEMBERREF table
246 nspace = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAMESPACE]);
247 name = mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAME]);
249 /* this will triggered by references to mscorlib */
250 g_assert (image->references [scopeindex-1] != NULL);
252 mimage = image->references [scopeindex-1]->image;
254 m = &mimage->metadata;
255 tables = &m->tables [MONO_TABLE_METHOD];
256 mono_typedef_from_name (mimage, name, nspace, &i);
257 /* mostly dumb search for now */
258 for (i--; i < tables->rows; ++i) {
260 mono_metadata_decode_row (tables, i, cols, MONO_METHOD_SIZE);
262 if (!strcmp (mname, mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]))) {
264 ptr = mono_metadata_blob_heap (m, cols [MONO_METHOD_SIGNATURE]);
265 mono_metadata_decode_blob_size (ptr, &ptr);
266 msig = mono_metadata_parse_method_signature (m, 1, ptr, NULL);
268 if (mono_metadata_signature_equal (&image->metadata, sig,
269 &mimage->metadata, msig)) {
270 mono_metadata_free_method_signature (sig);
271 mono_metadata_free_method_signature (msig);
272 return mono_get_method (mimage, MONO_TOKEN_METHOD_DEF | (i + 1));
276 g_warning ("cant find method %s.%s::%s",nspace, name, mname);
277 g_assert_not_reached ();
280 g_assert_not_reached ();
284 case MEMBERREF_PARENT_TYPESPEC: {
285 guint32 bcols [MONO_TYPESPEC_SIZE];
290 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1,
291 bcols, MONO_TYPESPEC_SIZE);
292 ptr = mono_metadata_blob_heap (m, bcols [MONO_TYPESPEC_SIGNATURE]);
293 len = mono_metadata_decode_value (ptr, &ptr);
294 type = mono_metadata_parse_type (m, ptr, &ptr);
296 if (type->type != MONO_TYPE_ARRAY)
297 g_assert_not_reached ();
299 result = (MonoMethod *)g_new0 (MonoMethod, 1);
300 result->image = image;
301 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
302 result->signature = sig;
304 if (!strcmp (mname, ".ctor")) {
305 g_assert (sig->hasthis);
306 if (type->data.array->rank == sig->param_count) {
307 result->addr = mono_lookup_internal_call ("__array_ctor");
309 } else if ((type->data.array->rank * 2) == sig->param_count) {
310 result->addr = mono_lookup_internal_call ("__array_bound_ctor");
313 g_assert_not_reached ();
316 if (!strcmp (mname, "Set")) {
317 g_assert (sig->hasthis);
318 g_assert (type->data.array->rank + 1 == sig->param_count);
320 result->addr = mono_lookup_internal_call ("__array_Set");
324 if (!strcmp (mname, "Get")) {
325 g_assert (sig->hasthis);
326 g_assert (type->data.array->rank == sig->param_count);
328 result->addr = mono_lookup_internal_call ("__array_Get");
332 g_assert_not_reached ();
336 g_assert_not_reached ();
343 ves_map_ffi_type (MonoType *type)
347 switch (type->type) {
349 rettype = &ffi_type_sint8;
351 case MONO_TYPE_BOOLEAN:
353 rettype = &ffi_type_uint8;
356 rettype = &ffi_type_sint16;
360 rettype = &ffi_type_uint16;
363 rettype = &ffi_type_sint32;
366 rettype = &ffi_type_sint32;
369 rettype = &ffi_type_float;
372 rettype = &ffi_type_double;
374 case MONO_TYPE_STRING:
375 rettype = &ffi_type_pointer;
378 rettype = &ffi_type_void;
381 g_warning ("not implemented");
382 g_assert_not_reached ();
389 fill_pinvoke_info (MonoImage *image, MonoMethodPInvoke *piinfo, int index)
391 MonoMethod *mh = &piinfo->method;
392 MonoTableInfo *tables = image->metadata.tables;
393 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
394 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
397 const char *import = NULL;
398 const char *scope = NULL;
401 ffi_type **args, *rettype;
404 for (i = 0; i < im->rows; i++) {
406 mono_metadata_decode_row (im, i, im_cols, 4);
408 if ((im_cols[1] >> 1) == index + 1) {
410 import = mono_metadata_string_heap (&image->metadata,
413 mono_metadata_decode_row (mr, im_cols [3] - 1, mr_cols,
416 scope = mono_metadata_string_heap (&image->metadata,
421 g_assert (import && scope);
423 scope = mono_map_dll (scope);
424 full_name = g_module_build_path (NULL, scope);
425 gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
430 piinfo->cif = g_new (ffi_cif , 1);
431 piinfo->piflags = im_cols [0];
433 g_module_symbol (gmodule, import, &mh->addr);
437 acount = mh->signature->param_count;
439 args = g_new (ffi_type *, acount);
441 for (i = 0; i < acount; i++)
442 args[i] = ves_map_ffi_type (mh->signature->params [i]->type);
444 rettype = ves_map_ffi_type (mh->signature->ret->type);
446 if (!ffi_prep_cif (piinfo->cif, FFI_DEFAULT_ABI, acount, rettype,
448 g_warning ("prepare pinvoke failed");
449 g_assert_not_reached ();
454 mono_get_method (MonoImage *image, guint32 token)
457 MonoMetadata *m = &image->metadata;
458 int table = mono_metadata_token_table (token);
459 int index = mono_metadata_token_index (token);
460 MonoTableInfo *tables = m->tables;
461 const char *loc, *sig = NULL;
464 guint32 cols[MONO_TYPEDEF_SIZE];
466 if (table == MONO_TABLE_METHOD && (result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
469 if (table != MONO_TABLE_METHOD) {
470 g_assert (table == MONO_TABLE_MEMBERREF);
471 return method_from_memberref (image, index);
474 mono_metadata_decode_row (&tables [table], index - 1, cols, 6);
476 if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
477 MonoTableInfo *t = &m->tables [MONO_TABLE_TYPEDEF];
478 MonoAssembly *corlib;
480 guint32 tdcols [MONO_TYPEDEF_SIZE];
482 tdef = mono_metadata_typedef_from_method (m, index - 1) - 1;
484 mono_metadata_decode_row (t, tdef, tdcols, MONO_TYPEDEF_SIZE);
486 name = g_strconcat (mono_metadata_string_heap (m, tdcols [MONO_TYPEDEF_NAMESPACE]), ".",
487 mono_metadata_string_heap (m, tdcols [MONO_TYPEDEF_NAME]), "::",
488 mono_metadata_string_heap (m, cols [MONO_METHOD_NAME]), NULL);
490 corlib = mono_assembly_open (CORLIB_NAME, NULL, NULL);
492 /* all internal calls must be inside corlib */
493 g_assert (corlib->image == image);
495 result = (MonoMethod *)g_new0 (MonoMethod, 1);
497 result->addr = mono_lookup_internal_call (name);
501 g_assert (result->addr != NULL);
503 } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
505 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
508 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
511 result->image = image;
512 result->flags = cols [2];
513 result->iflags = cols [1];
514 result->name = mono_metadata_string_heap (m, cols [3]);
516 if (!sig) /* already taken from the methodref */
517 sig = mono_metadata_blob_heap (m, cols [4]);
518 size = mono_metadata_decode_blob_size (sig, &sig);
519 result->signature = mono_metadata_parse_method_signature (m, 0, sig, NULL);
521 if (result->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
522 fill_pinvoke_info (image, (MonoMethodPInvoke *)result,
524 } else if (!(result->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
525 /* if this is a methodref from another module/assembly, this fails */
526 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
528 ((MonoMethodNormal *)result)->header =
529 mono_metadata_parse_mh (m, loc);
532 g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
538 mono_free_method (MonoMethod *method)
540 mono_metadata_free_method_signature (method->signature);
541 if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
542 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
543 g_free (piinfo->cif->arg_types);
544 g_free (piinfo->cif);
545 } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
546 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);