(mono_runtime_delegate_invoke): impl.
[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  *   Patrik Torstensson (patrik.torstensson@labs2.com)
8  *
9  * (C) 2001 Ximian, Inc.
10  *
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
13  * kinds of `Refs'.
14  *
15  * TODO:
16  *   This should keep track of the assembly versions that we are loading.
17  *
18  */
19 #include <config.h>
20 #include <glib.h>
21 #include <gmodule.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
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>
34
35 static gboolean dummy_icall = TRUE;
36
37 MonoDefaults mono_defaults;
38
39 #ifdef __CYGWIN__
40 #define mono_map_dll(name) (name)
41 #else
42 static const char *dll_map[] = {
43         "libc", "libc.so.6",
44         "libm", "libm.so.6",
45         "cygwin1.dll", "libc.so.6", 
46         NULL, NULL
47 };
48
49 static const char *
50 mono_map_dll (const char *name)
51 {
52         int i = 0;
53
54         while (dll_map [i]) {
55                 if (!strcmp (dll_map [i], name))
56                         return  dll_map [i + 1];
57                 i += 2;
58         }
59
60         return name;
61 }
62 #endif
63
64 static GHashTable *icall_hash = NULL;
65
66 void
67 mono_add_internal_call (const char *name, gconstpointer method)
68 {
69         if (!icall_hash) {
70                 dummy_icall = FALSE;
71                 icall_hash = g_hash_table_new (g_str_hash , g_str_equal);
72         }
73
74         g_hash_table_insert (icall_hash, g_strdup (name), method);
75 }
76
77 static void
78 ves_icall_dummy (void)
79 {
80         g_warning ("the mono runtime is not initialized");
81         g_assert_not_reached ();
82 }
83
84 gpointer
85 mono_lookup_internal_call (MonoMethod *method)
86 {
87         char *name;
88         char *tmpsig;
89         gpointer res;
90
91         if (dummy_icall)
92                 return ves_icall_dummy;
93
94         if (!method) {
95                 g_warning ("can't resolve internal call, method is null");
96         }
97
98         if (!icall_hash) {
99                 g_warning ("icall_hash not initialized");
100                 g_assert_not_reached ();
101         }
102
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
106                 g_free (name);
107         
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");
117
118                         g_free (name);
119                         g_free (tmpsig);
120
121                         return NULL;
122                 }
123
124                 g_free(tmpsig);
125         }
126
127         g_free (name);
128
129         return res;
130 }
131
132 MonoClassField*
133 mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass)
134 {
135         MonoClass *klass;
136         MonoTableInfo *tables = image->tables;
137         guint32 cols[6];
138         guint32 nindex, class;
139         const char *fname;
140         const char *ptr;
141         guint32 idx = mono_metadata_token_index (token);
142
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;
146
147         fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
148         
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... */
152
153         switch (class) {
154         case MEMBERREF_PARENT_TYPEREF:
155                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
156                 if (!klass) {
157                         g_warning ("Missing field %s in typeref index %d", fname, nindex);
158                         return NULL;
159                 }
160                 mono_class_init (klass);
161                 if (retklass)
162                         *retklass = klass;
163                 return mono_class_get_field_from_name (klass, fname);
164         default:
165                 return NULL;
166         }
167 }
168
169 static MonoMethod *
170 find_method (MonoClass *klass, const char* name, MonoMethodSignature *sig)
171 {
172         int i;
173         while (klass) {
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))
179                                         return m;
180                         }
181                 }
182                 klass = klass->parent;
183         }
184         return NULL;
185
186 }
187
188 static MonoMethod *
189 method_from_memberref (MonoImage *image, guint32 idx)
190 {
191         MonoClass *klass;
192         MonoMethod *method;
193         MonoTableInfo *tables = image->tables;
194         guint32 cols[6];
195         guint32 nindex, class;
196         const char *mname;
197         MonoMethodSignature *sig;
198         const char *ptr;
199
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]));*/
205
206         mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
207         
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);
211
212         switch (class) {
213         case MEMBERREF_PARENT_TYPEREF:
214                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
215                 if (!klass) {
216                         g_warning ("Missing method %s in typeref index %d", mname, nindex);
217                         mono_metadata_free_method_signature (sig);
218                         return NULL;
219                 }
220                 mono_class_init (klass);
221                 method = find_method (klass, mname, sig);
222                 if (!method)
223                         g_warning ("Missing method %s in typeref index %d", mname, nindex);
224                 mono_metadata_free_method_signature (sig);
225                 return method;
226         case MEMBERREF_PARENT_TYPESPEC: {
227                 guint32 bcols [MONO_TYPESPEC_SIZE];
228                 guint32 len;
229                 MonoType *type;
230                 MonoMethod *result;
231
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);
237
238                 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY)
239                         g_assert_not_reached ();                
240
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;
246
247                 if (!strcmp (mname, ".ctor")) {
248                         /* we special-case this in the runtime. */
249                         result->addr = NULL;
250                         return result;
251                 }
252                 
253                 if (!strcmp (mname, "Set")) {
254                         g_assert (sig->hasthis);
255                         g_assert (type->data.array->rank + 1 == sig->param_count);
256
257                         result->addr = mono_lookup_internal_call(result);
258                         return result;
259                 }
260
261                 if (!strcmp (mname, "Get")) {
262                         g_assert (sig->hasthis);
263                         g_assert (type->data.array->rank == sig->param_count);
264
265                         result->addr = mono_lookup_internal_call(result);
266                         return result;
267                 }
268
269                 if (!strcmp (mname, "Address")) {
270                         g_assert (sig->hasthis);
271                         g_assert (type->data.array->rank == sig->param_count);
272
273                         result->addr = mono_lookup_internal_call(result);
274                         return result;
275                 }
276
277                 g_assert_not_reached ();
278                 break;
279         }
280         default:
281                 g_assert_not_reached ();
282         }
283
284         return NULL;
285 }
286
287 static void
288 fill_pinvoke_info (MonoImage *image, MonoMethodPInvoke *piinfo, int idx)
289 {
290         MonoMethod *mh = &piinfo->method;
291         MonoTableInfo *tables = image->tables;
292         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
293         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
294         guint32 im_cols [4];
295         guint32 mr_cols [1];
296         const char *import = NULL;
297         const char *scope = NULL;
298         char *full_name;
299         GModule *gmodule;
300         int i;
301
302         for (i = 0; i < im->rows; i++) {
303                         
304                 mono_metadata_decode_row (im, i, im_cols, 4);
305
306                 if ((im_cols[1] >> 1) == idx + 1) {
307
308                         import = mono_metadata_string_heap (image, im_cols [2]);
309
310                         mono_metadata_decode_row (mr, im_cols [3] - 1, mr_cols,
311                                                   1);
312                         
313                         scope = mono_metadata_string_heap (image, mr_cols [0]);
314                 }
315         }
316
317         piinfo->piflags = im_cols [0];
318
319         g_assert (import && scope);
320
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);
324
325         mh->addr = NULL;
326         if (!gmodule) {
327                 if (!(gmodule=g_module_open (scope, G_MODULE_BIND_LAZY))) {
328                         g_warning ("Failed to load library %s (%s)", full_name, scope);
329                         g_free (full_name);
330                         return;
331                 }
332         }
333         g_free (full_name);
334
335         g_module_symbol (gmodule, import, &mh->addr); 
336
337         if (!mh->addr) {
338                 g_warning ("Failed to load function %s from %s", import, scope);
339                 return;
340         }
341
342         mh->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
343 }
344
345 MonoMethod *
346 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
347 {
348         MonoMethod *result;
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;
353         int size;
354         guint32 cols [MONO_TYPEDEF_SIZE];
355
356         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
357                         return result;
358
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);
365                 return result;
366         }
367
368         mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
369
370         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
371             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
372                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
373         else 
374                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
375         
376         result->slot = -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]);
381
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);
386
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);
390         }
391
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);
397         } else {
398                 /* if this is a methodref from another module/assembly, this fails */
399                 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
400
401                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
402                                         !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
403                         g_assert (loc);
404                         ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
405                 }
406         }
407
408         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
409
410         return result;
411 }
412
413 void
414 mono_free_method  (MonoMethod *method)
415 {
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);
422         }
423
424         g_free (method);
425 }
426
427 void
428 mono_method_get_param_names (MonoMethod *method, const char **names)
429 {
430         int i, lastp;
431         MonoClass *klass = method->klass;
432         MonoTableInfo *methodt = &klass->image->tables [MONO_TABLE_METHOD];
433         MonoTableInfo *paramt = &klass->image->tables [MONO_TABLE_PARAM];
434
435         if (!method->signature->param_count)
436                 return;
437         for (i = 0; i < method->signature->param_count; ++i)
438                 names [i] = "";
439
440         mono_class_init (klass);
441         if (!klass->methods)
442                 return;
443
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);
449
450                         if (idx + 1 < methodt->rows)
451                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
452                         else
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]);
458                         }
459                         return;
460                 }
461         }
462 }