Tue Sep 18 13:16:32 CEST 2001 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / loader.c
1 /*
2  * loader.c: Image Loader 
3  *
4  * Authors:
5  *   Paolo Molaro (lupus@ximian.com)
6  *   Miguel de Icaza (miguel@ximian.com)
7  *
8  * (C) 2001 Ximian, Inc.
9  *
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
12  * kinds of `Refs'.
13  *
14  * TODO:
15  *   This should keep track of the assembly versions that we are loading.
16  *
17  */
18 #include <config.h>
19 #include <glib.h>
20 #include <gmodule.h>
21 #include <stdio.h>
22 #include <string.h>
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>
31
32 static gboolean dummy_icall = TRUE;
33
34 MonoDefaults mono_defaults;
35
36 static char *dll_map[] = {
37         "libc", "libc.so.6",
38         "libm", "libm.so.6",
39         "cygwin1.dll", "libc.so.6", 
40         NULL, NULL
41 };
42
43 static const char *
44 mono_map_dll (const char *name)
45 {
46         int i = 0;
47
48         while (dll_map [i]) {
49                 if (!strcmp (dll_map [i], name))
50                         return  dll_map [i + 1];
51                 i += 2;
52         }
53
54         return name;
55 }
56
57 void
58 mono_init (void)
59 {
60         static gboolean initialized = FALSE;
61         MonoAssembly *ass;
62         enum MonoImageOpenStatus status = MONO_IMAGE_OK;
63
64         if (initialized)
65                 return;
66
67         /* find the corlib */
68         ass = mono_assembly_open (CORLIB_NAME, NULL, &status);
69         g_assert (status == MONO_IMAGE_OK);
70         g_assert (ass != NULL);
71         mono_defaults.corlib = ass->image;
72
73         mono_defaults.object_class = mono_class_from_name (
74                 mono_defaults.corlib, "System", "Object");
75         g_assert (mono_defaults.object_class != 0);
76
77         mono_defaults.void_class = mono_class_from_name (
78                 mono_defaults.corlib, "System", "Void");
79         g_assert (mono_defaults.void_class != 0);
80
81         mono_defaults.boolean_class = mono_class_from_name (
82                 mono_defaults.corlib, "System", "Boolean");
83         g_assert (mono_defaults.boolean_class != 0);
84
85         mono_defaults.byte_class = mono_class_from_name (
86                 mono_defaults.corlib, "System", "Byte");
87         g_assert (mono_defaults.byte_class != 0);
88
89         mono_defaults.sbyte_class = mono_class_from_name (
90                 mono_defaults.corlib, "System", "SByte");
91         g_assert (mono_defaults.sbyte_class != 0);
92
93         mono_defaults.int16_class = mono_class_from_name (
94                 mono_defaults.corlib, "System", "Int16");
95         g_assert (mono_defaults.int16_class != 0);
96
97         mono_defaults.uint16_class = mono_class_from_name (
98                 mono_defaults.corlib, "System", "UInt16");
99         g_assert (mono_defaults.uint16_class != 0);
100
101         mono_defaults.int32_class = mono_class_from_name (
102                 mono_defaults.corlib, "System", "Int32");
103         g_assert (mono_defaults.int32_class != 0);
104
105         mono_defaults.uint32_class = mono_class_from_name (
106                 mono_defaults.corlib, "System", "UInt32");
107         g_assert (mono_defaults.uint32_class != 0);
108
109         mono_defaults.uint_class = mono_class_from_name (
110                 mono_defaults.corlib, "System", "UIntPtr");
111         g_assert (mono_defaults.uint_class != 0);
112
113         mono_defaults.int_class = mono_class_from_name (
114                 mono_defaults.corlib, "System", "IntPtr");
115         g_assert (mono_defaults.int_class != 0);
116
117         mono_defaults.int64_class = mono_class_from_name (
118                 mono_defaults.corlib, "System", "Int64");
119         g_assert (mono_defaults.int64_class != 0);
120
121         mono_defaults.uint64_class = mono_class_from_name (
122                 mono_defaults.corlib, "System", "UInt64");
123         g_assert (mono_defaults.uint64_class != 0);
124
125         mono_defaults.single_class = mono_class_from_name (
126                 mono_defaults.corlib, "System", "Single");
127         g_assert (mono_defaults.single_class != 0);
128
129         mono_defaults.double_class = mono_class_from_name (
130                 mono_defaults.corlib, "System", "Double");
131         g_assert (mono_defaults.double_class != 0);
132
133         mono_defaults.char_class = mono_class_from_name (
134                 mono_defaults.corlib, "System", "Char");
135         g_assert (mono_defaults.char_class != 0);
136
137         mono_defaults.string_class = mono_class_from_name (
138                 mono_defaults.corlib, "System", "String");
139         g_assert (mono_defaults.string_class != 0);
140
141         mono_defaults.enum_class = mono_class_from_name (
142                 mono_defaults.corlib, "System", "Enum");
143         g_assert (mono_defaults.enum_class != 0);
144
145         mono_defaults.array_class = mono_class_from_name (
146                 mono_defaults.corlib, "System", "Array");
147         g_assert (mono_defaults.array_class != 0);
148
149         mono_defaults.delegate_class = mono_class_from_name (
150                 mono_defaults.corlib, "System", "Delegate");
151         g_assert (mono_defaults.delegate_class != 0);
152 }
153
154 static GHashTable *icall_hash = NULL;
155
156 void
157 mono_add_internal_call (const char *name, gpointer method)
158 {
159         if (!icall_hash) {
160                 dummy_icall = FALSE;
161                 icall_hash = g_hash_table_new (g_str_hash , g_str_equal);
162         }
163
164         g_hash_table_insert (icall_hash, g_strdup (name), method);
165 }
166
167 static void
168 ves_icall_dummy ()
169 {
170         g_warning ("the mono runtime is not initialized");
171         g_assert_not_reached ();
172 }
173
174 gpointer
175 mono_lookup_internal_call (const char *name)
176 {
177         gpointer res;
178
179         if (dummy_icall)
180                 return ves_icall_dummy;
181
182         if (!icall_hash) {
183                 g_warning ("icall_hash not initialized");
184                 g_assert_not_reached ();
185         }
186
187         if (!(res = g_hash_table_lookup (icall_hash, name))) {
188                 g_warning ("cant resolve internal call to \"%s\"", name);
189                 g_assert_not_reached ();
190         }
191
192         return res;
193 }
194
195 static MonoMethod *
196 method_from_memberref (MonoImage *image, guint32 index)
197 {
198         MonoImage *mimage;
199         MonoClass *klass;
200         MonoTableInfo *tables = image->tables;
201         guint32 cols[6];
202         guint32 nindex, class, i;
203         const char *mname, *name, *nspace;
204         MonoMethodSignature *sig;
205         const char *ptr;
206
207         mono_metadata_decode_row (&tables [MONO_TABLE_MEMBERREF], index-1, cols, 3);
208         nindex = cols [MONO_MEMBERREF_CLASS] >> MEMBERREF_PARENT_BITS;
209         class = cols [MONO_MEMBERREF_CLASS] & MEMBERREF_PARENT_MASK;
210         /*g_print ("methodref: 0x%x 0x%x %s\n", class, nindex,
211                 mono_metadata_string_heap (m, cols [MONO_MEMBERREF_NAME]));*/
212
213         mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
214         
215         ptr = mono_metadata_blob_heap (image, cols [MONO_MEMBERREF_SIGNATURE]);
216         mono_metadata_decode_blob_size (ptr, &ptr);
217         sig = mono_metadata_parse_method_signature (image, 0, ptr, NULL);
218
219         switch (class) {
220         case MEMBERREF_PARENT_TYPEREF: {
221                 guint32 scopeindex, scopetable;
222
223                 mono_metadata_decode_row (&tables [MONO_TABLE_TYPEREF], nindex-1, cols, MONO_TYPEREF_SIZE);
224                 scopeindex = cols [MONO_TYPEREF_SCOPE] >> RESOLTION_SCOPE_BITS;
225                 scopetable = cols [MONO_TYPEREF_SCOPE] & RESOLTION_SCOPE_MASK;
226                 /*g_print ("typeref: 0x%x 0x%x %s.%s\n", scopetable, scopeindex,
227                         mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAMESPACE]),
228                         mono_metadata_string_heap (m, cols [MONO_TYPEREF_NAME]));*/
229                 switch (scopetable) {
230                 case RESOLTION_SCOPE_ASSEMBLYREF:
231                         /*
232                          * To find the method we have the following info:
233                          * *) name and namespace of the class from the TYPEREF table
234                          * *) name and signature of the method from the MEMBERREF table
235                          */
236                         nspace = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAMESPACE]);
237                         name = mono_metadata_string_heap (image, cols [MONO_TYPEREF_NAME]);
238
239                         /* this will triggered by references to mscorlib */
240                         if (image->references [scopeindex-1] == NULL)
241                                 g_error ("Reference to mscorlib? Probably need to implement %s.%s::%s in corlib", nspace, name, mname);
242
243                         mimage = image->references [scopeindex-1]->image;
244
245                         klass = mono_class_from_name (mimage, nspace, name);
246                         mono_class_metadata_init (klass);
247
248                         /* mostly dumb search for now */
249                         for (i = 0; i < klass->method.count; ++i) {
250                                 MonoMethod *m = klass->methods [i];
251                                 if (!strcmp (mname, m->name)) {
252                                         if (mono_metadata_signature_equal (sig, m->signature)) {
253                                                 mono_metadata_free_method_signature (sig);
254                                                 return m;
255                                         }
256                                 }
257                         }
258                         g_warning ("can't find method %s.%s::%s", nspace, name, mname);
259                         g_assert_not_reached ();
260                         break;
261                 default:
262                         g_assert_not_reached ();
263                 }
264                 break;
265         }
266         case MEMBERREF_PARENT_TYPESPEC: {
267                 guint32 bcols [MONO_TYPESPEC_SIZE];
268                 guint32 len;
269                 MonoType *type;
270                 MonoMethod *result;
271
272                 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
273                                           bcols, MONO_TYPESPEC_SIZE);
274                 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
275                 len = mono_metadata_decode_value (ptr, &ptr);   
276                 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
277
278                 if (type->type != MONO_TYPE_ARRAY)
279                         g_assert_not_reached ();                
280
281                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
282                 result->klass = mono_defaults.array_class;
283                 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
284                 result->signature = sig;
285                 result->name = mname;
286
287                 if (!strcmp (mname, ".ctor")) { 
288                         g_assert (sig->hasthis);
289                         if (type->data.array->rank == sig->param_count) {
290                                 result->addr = mono_lookup_internal_call ("__array_ctor");
291                         } else if ((type->data.array->rank * 2) == sig->param_count) {
292                                 result->addr = mono_lookup_internal_call ("__array_bound_ctor");
293                         } else 
294                                 g_assert_not_reached ();
295
296                         result->flags = METHOD_ATTRIBUTE_PINVOKE_IMPL;
297                         return result;                                          
298                 }
299
300                 if (!strcmp (mname, "Set")) {
301                         g_assert (sig->hasthis);
302                         g_assert (type->data.array->rank + 1 == sig->param_count);
303
304                         result->addr = mono_lookup_internal_call ("__array_Set");
305                         return result;
306                 }
307
308                 if (!strcmp (mname, "Get")) {
309                         g_assert (sig->hasthis);
310                         g_assert (type->data.array->rank == sig->param_count);
311
312                         result->addr = mono_lookup_internal_call ("__array_Get");
313                         return result;
314                 }
315
316                 g_assert_not_reached ();
317                 break;
318         }
319         default:
320                 g_assert_not_reached ();
321         }
322
323         return NULL;
324 }
325
326 static void
327 fill_pinvoke_info (MonoImage *image, MonoMethodPInvoke *piinfo, int index)
328 {
329         MonoMethod *mh = &piinfo->method;
330         MonoTableInfo *tables = image->tables;
331         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
332         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
333         guint32 im_cols [4];
334         guint32 mr_cols [1];
335         const char *import = NULL;
336         const char *scope = NULL;
337         char *full_name;
338         GModule *gmodule;
339         int i;
340
341         for (i = 0; i < im->rows; i++) {
342                         
343                 mono_metadata_decode_row (im, i, im_cols, 4);
344
345                 if ((im_cols[1] >> 1) == index + 1) {
346
347                         import = mono_metadata_string_heap (image, im_cols [2]);
348
349                         mono_metadata_decode_row (mr, im_cols [3] - 1, mr_cols,
350                                                   1);
351                         
352                         scope = mono_metadata_string_heap (image, mr_cols [0]);
353                 }
354         }
355
356         piinfo->piflags = im_cols [0];
357
358         g_assert (import && scope);
359
360         scope = mono_map_dll (scope);
361         full_name = g_module_build_path (NULL, scope);
362         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
363
364         if (!gmodule)
365                 g_error ("Failed to load library %s (%s)", full_name, scope);
366         g_free (full_name);
367
368         g_module_symbol (gmodule, import, &mh->addr); 
369
370         if (!mh->addr)
371                 g_error ("Failed to load function %s from %s", import, scope);
372
373         mh->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
374 }
375
376 MonoMethod *
377 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
378 {
379         MonoMethod *result;
380         int table = mono_metadata_token_table (token);
381         int index = mono_metadata_token_index (token);
382         MonoTableInfo *tables = image->tables;
383         const char *loc, *sig = NULL;
384         char *name;
385         int size;
386         guint32 cols [MONO_TYPEDEF_SIZE];
387
388         if (table == MONO_TABLE_METHOD && (result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
389                         return result;
390
391         if (table != MONO_TABLE_METHOD) {
392                 g_assert (table == MONO_TABLE_MEMBERREF);
393                 return method_from_memberref (image, index);
394         }
395
396         mono_metadata_decode_row (&tables [table], index - 1, cols, 6);
397
398         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
399             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
400                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
401         else 
402                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
403         
404         result->klass = klass;
405         result->flags = cols [2];
406         result->iflags = cols [1];
407         result->name = mono_metadata_string_heap (image, cols [3]);
408
409         if (!sig) /* already taken from the methodref */
410                 sig = mono_metadata_blob_heap (image, cols [4]);
411         size = mono_metadata_decode_blob_size (sig, &sig);
412         result->signature = mono_metadata_parse_method_signature (image, 0, sig, NULL);
413
414         if (!result->klass) {
415                 guint32 type = mono_metadata_typedef_from_method (image, token);
416                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
417         }
418
419         if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
420                 MonoTableInfo *t = &image->tables [MONO_TABLE_TYPEDEF];
421                 guint32 tdef;
422                 guint32 tdcols [MONO_TYPEDEF_SIZE];
423
424                 tdef = mono_metadata_typedef_from_method (image, index - 1) - 1;
425
426                 mono_metadata_decode_row (t, tdef, tdcols, MONO_TYPEDEF_SIZE);
427
428                 name = g_strconcat (mono_metadata_string_heap (image, tdcols [MONO_TYPEDEF_NAMESPACE]), ".",
429                                     mono_metadata_string_heap (image, tdcols [MONO_TYPEDEF_NAME]), "::", 
430                                     mono_metadata_string_heap (image, cols [MONO_METHOD_NAME]), NULL);
431
432                 result->addr = mono_lookup_internal_call (name);
433
434                 g_free (name);
435
436                 g_assert (result->addr != NULL);
437
438                 result->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
439
440         } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
441                 fill_pinvoke_info (image, (MonoMethodPInvoke *)result, index - 1);
442         } else {
443                 /* if this is a methodref from another module/assembly, this fails */
444                 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
445
446                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
447                                         !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
448                         g_assert (loc);
449                         ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
450                 }
451         }
452
453         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
454
455         return result;
456 }
457
458 void
459 mono_free_method  (MonoMethod *method)
460 {
461         mono_metadata_free_method_signature (method->signature);
462         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
463                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
464                 g_free (piinfo->code);
465         } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
466                 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
467         }
468
469         g_free (method);
470 }
471