Thu Sep 27 19:52:11 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                 return NULL;
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                         /* 
249                          * FIXME: this is a workaround for the different signatures
250                          * in delegates constructors you get in user code (native int)
251                          * and in mscorlib (native unsigned int)
252                          */
253                         if (klass->parent && klass->parent->parent == mono_defaults.delegate_class) {
254                                 for (i = 0; i < klass->method.count; ++i) {
255                                         MonoMethod *m = klass->methods [i];
256                                         if (!strcmp (mname, m->name)) {
257                                                 if (!strcmp (mname, ".ctor")) {
258                                                         /* we assume signature is correct */
259                                                         mono_metadata_free_method_signature (sig);
260                                                         return m;
261                                                 }
262                                                 if (mono_metadata_signature_equal (sig, m->signature)) {
263                                                         mono_metadata_free_method_signature (sig);
264                                                         return m;
265                                                 }
266                                         }
267                                 }
268                         }
269                         /* mostly dumb search for now */
270                         for (i = 0; i < klass->method.count; ++i) {
271                                 MonoMethod *m = klass->methods [i];
272                                 if (!strcmp (mname, m->name)) {
273                                         if (mono_metadata_signature_equal (sig, m->signature)) {
274                                                 mono_metadata_free_method_signature (sig);
275                                                 return m;
276                                         }
277                                 }
278                         }
279                         g_warning ("Missing method %s.%s::%s", nspace, name, mname);
280                         mono_metadata_free_method_signature (sig);
281                         return NULL;
282                 default:
283                         mono_metadata_free_method_signature (sig);
284                         return NULL;
285                 }
286                 break;
287         }
288         case MEMBERREF_PARENT_TYPESPEC: {
289                 guint32 bcols [MONO_TYPESPEC_SIZE];
290                 guint32 len;
291                 MonoType *type;
292                 MonoMethod *result;
293
294                 mono_metadata_decode_row (&tables [MONO_TABLE_TYPESPEC], nindex - 1, 
295                                           bcols, MONO_TYPESPEC_SIZE);
296                 ptr = mono_metadata_blob_heap (image, bcols [MONO_TYPESPEC_SIGNATURE]);
297                 len = mono_metadata_decode_value (ptr, &ptr);   
298                 type = mono_metadata_parse_type (image, MONO_PARSE_TYPE, 0, ptr, &ptr);
299
300                 if (type->type != MONO_TYPE_ARRAY)
301                         g_assert_not_reached ();                
302
303                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
304                 result->klass = mono_defaults.array_class;
305                 result->iflags = METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL;
306                 result->signature = sig;
307                 result->name = mname;
308
309                 if (!strcmp (mname, ".ctor")) { 
310                         g_assert (sig->hasthis);
311                         if (type->data.array->rank == sig->param_count) {
312                                 result->addr = mono_lookup_internal_call ("__array_ctor");
313                         } else if ((type->data.array->rank * 2) == sig->param_count) {
314                                 result->addr = mono_lookup_internal_call ("__array_bound_ctor");
315                         } else 
316                                 g_assert_not_reached ();
317
318                         result->flags = METHOD_ATTRIBUTE_PINVOKE_IMPL;
319                         return result;                                          
320                 }
321
322                 if (!strcmp (mname, "Set")) {
323                         g_assert (sig->hasthis);
324                         g_assert (type->data.array->rank + 1 == sig->param_count);
325
326                         result->addr = mono_lookup_internal_call ("__array_Set");
327                         return result;
328                 }
329
330                 if (!strcmp (mname, "Get")) {
331                         g_assert (sig->hasthis);
332                         g_assert (type->data.array->rank == sig->param_count);
333
334                         result->addr = mono_lookup_internal_call ("__array_Get");
335                         return result;
336                 }
337
338                 g_assert_not_reached ();
339                 break;
340         }
341         default:
342                 g_assert_not_reached ();
343         }
344
345         return NULL;
346 }
347
348 static void
349 fill_pinvoke_info (MonoImage *image, MonoMethodPInvoke *piinfo, int index)
350 {
351         MonoMethod *mh = &piinfo->method;
352         MonoTableInfo *tables = image->tables;
353         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
354         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
355         guint32 im_cols [4];
356         guint32 mr_cols [1];
357         const char *import = NULL;
358         const char *scope = NULL;
359         char *full_name;
360         GModule *gmodule;
361         int i;
362
363         for (i = 0; i < im->rows; i++) {
364                         
365                 mono_metadata_decode_row (im, i, im_cols, 4);
366
367                 if ((im_cols[1] >> 1) == index + 1) {
368
369                         import = mono_metadata_string_heap (image, im_cols [2]);
370
371                         mono_metadata_decode_row (mr, im_cols [3] - 1, mr_cols,
372                                                   1);
373                         
374                         scope = mono_metadata_string_heap (image, mr_cols [0]);
375                 }
376         }
377
378         piinfo->piflags = im_cols [0];
379
380         g_assert (import && scope);
381
382         scope = mono_map_dll (scope);
383         full_name = g_module_build_path (NULL, scope);
384         gmodule = g_module_open (full_name, G_MODULE_BIND_LAZY);
385
386         mh->addr = NULL;
387         if (!gmodule) {
388                 g_warning ("Failed to load library %s (%s)", full_name, scope);
389                 g_free (full_name);
390                 return;
391         }
392         g_free (full_name);
393
394         g_module_symbol (gmodule, import, &mh->addr); 
395
396         if (!mh->addr) {
397                 g_warning ("Failed to load function %s from %s", import, scope);
398                 return;
399         }
400
401         mh->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
402 }
403
404 MonoMethod *
405 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
406 {
407         MonoMethod *result;
408         int table = mono_metadata_token_table (token);
409         int index = mono_metadata_token_index (token);
410         MonoTableInfo *tables = image->tables;
411         const char *loc, *sig = NULL;
412         char *name;
413         int size;
414         guint32 cols [MONO_TYPEDEF_SIZE];
415
416         if (table == MONO_TABLE_METHOD && (result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
417                         return result;
418
419         if (table != MONO_TABLE_METHOD) {
420                 g_assert (table == MONO_TABLE_MEMBERREF);
421                 return method_from_memberref (image, index);
422         }
423
424         mono_metadata_decode_row (&tables [table], index - 1, cols, 6);
425
426         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
427             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
428                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
429         else 
430                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
431         
432         result->klass = klass;
433         result->flags = cols [2];
434         result->iflags = cols [1];
435         result->name = mono_metadata_string_heap (image, cols [3]);
436
437         if (!sig) /* already taken from the methodref */
438                 sig = mono_metadata_blob_heap (image, cols [4]);
439         size = mono_metadata_decode_blob_size (sig, &sig);
440         result->signature = mono_metadata_parse_method_signature (image, 0, sig, NULL);
441
442         if (!result->klass) {
443                 guint32 type = mono_metadata_typedef_from_method (image, token);
444                 result->klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF | type);
445         }
446
447         if (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
448                 name = g_strconcat (result->klass->name_space, ".", result->klass->name, "::", 
449                                     mono_metadata_string_heap (image, cols [MONO_METHOD_NAME]), NULL);
450                 result->addr = mono_lookup_internal_call (name);
451                 g_free (name);
452                 result->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
453         } else if (cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
454                 fill_pinvoke_info (image, (MonoMethodPInvoke *)result, index - 1);
455         } else {
456                 /* if this is a methodref from another module/assembly, this fails */
457                 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
458
459                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
460                                         !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
461                         g_assert (loc);
462                         ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
463                 }
464         }
465
466         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
467
468         return result;
469 }
470
471 void
472 mono_free_method  (MonoMethod *method)
473 {
474         mono_metadata_free_method_signature (method->signature);
475         if (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) {
476                 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *)method;
477                 g_free (piinfo->code);
478         } else if (!(method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
479                 mono_metadata_free_mh (((MonoMethodNormal *)method)->header);
480         }
481
482         g_free (method);
483 }
484