68c32e252261a5ff37ca61d52d60f02a3d7fbee8
[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
113                         g_free (name);
114                         g_free(tmpsig);
115
116                         return NULL;
117                 }
118
119                 g_free(tmpsig);
120         }
121
122         g_free (name);
123
124         return res;
125 }
126
127 MonoClassField*
128 mono_field_from_memberref (MonoImage *image, guint32 token, MonoClass **retklass)
129 {
130         MonoClass *klass;
131         MonoTableInfo *tables = image->tables;
132         guint32 cols[6];
133         guint32 nindex, class;
134         const char *fname;
135         const char *ptr;
136         guint32 idx = mono_metadata_token_index (token);
137
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;
141
142         fname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
143         
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... */
147
148         switch (class) {
149         case MEMBERREF_PARENT_TYPEREF:
150                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
151                 if (!klass) {
152                         g_warning ("Missing field %s in typeref index %d", fname, nindex);
153                         return NULL;
154                 }
155                 mono_class_init (klass);
156                 if (retklass)
157                         *retklass = klass;
158                 return mono_class_get_field_from_name (klass, fname);
159         default:
160                 return NULL;
161         }
162 }
163
164 static MonoMethod *
165 find_method (MonoClass *klass, const char* name, MonoMethodSignature *sig)
166 {
167         int i;
168         while (klass) {
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))
174                                         return m;
175                         }
176                 }
177                 klass = klass->parent;
178         }
179         return NULL;
180
181 }
182
183 static MonoMethod *
184 method_from_memberref (MonoImage *image, guint32 idx)
185 {
186         MonoClass *klass;
187         MonoMethod *method;
188         MonoTableInfo *tables = image->tables;
189         guint32 cols[6];
190         guint32 nindex, class;
191         const char *mname;
192         MonoMethodSignature *sig;
193         const char *ptr;
194
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]));*/
200
201         mname = mono_metadata_string_heap (image, cols [MONO_MEMBERREF_NAME]);
202         
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);
206
207         switch (class) {
208         case MEMBERREF_PARENT_TYPEREF:
209                 klass = mono_class_from_typeref (image, MONO_TOKEN_TYPE_REF | nindex);
210                 if (!klass) {
211                         g_warning ("Missing method %s in typeref index %d", mname, nindex);
212                         mono_metadata_free_method_signature (sig);
213                         return NULL;
214                 }
215                 mono_class_init (klass);
216                 method = find_method (klass, mname, sig);
217                 if (!method)
218                         g_warning ("Missing method %s in typeref index %d", mname, nindex);
219                 mono_metadata_free_method_signature (sig);
220                 return method;
221         case MEMBERREF_PARENT_TYPESPEC: {
222                 guint32 bcols [MONO_TYPESPEC_SIZE];
223                 guint32 len;
224                 MonoType *type;
225                 MonoMethod *result;
226
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);
232
233                 if (type->type != MONO_TYPE_ARRAY && type->type != MONO_TYPE_SZARRAY)
234                         g_assert_not_reached ();                
235
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;
241
242                 if (!strcmp (mname, ".ctor")) {
243                         /* we special-case this in the runtime. */
244                         result->addr = NULL;
245                         return result;
246                 }
247                 
248                 if (!strcmp (mname, "Set")) {
249                         g_assert (sig->hasthis);
250                         g_assert (type->data.array->rank + 1 == sig->param_count);
251
252                         result->addr = mono_lookup_internal_call(result);
253                         return result;
254                 }
255
256                 if (!strcmp (mname, "Get")) {
257                         g_assert (sig->hasthis);
258                         g_assert (type->data.array->rank == sig->param_count);
259
260                         result->addr = mono_lookup_internal_call(result);
261                         return result;
262                 }
263
264                 if (!strcmp (mname, "Address")) {
265                         g_assert (sig->hasthis);
266                         g_assert (type->data.array->rank == sig->param_count);
267
268                         result->addr = mono_lookup_internal_call(result);
269                         return result;
270                 }
271
272                 g_assert_not_reached ();
273                 break;
274         }
275         default:
276                 g_assert_not_reached ();
277         }
278
279         return NULL;
280 }
281
282 static void
283 fill_pinvoke_info (MonoImage *image, MonoMethodPInvoke *piinfo, int idx)
284 {
285         MonoMethod *mh = &piinfo->method;
286         MonoTableInfo *tables = image->tables;
287         MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
288         MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
289         guint32 im_cols [4];
290         guint32 mr_cols [1];
291         const char *import = NULL;
292         const char *scope = NULL;
293         char *full_name;
294         GModule *gmodule;
295         int i;
296
297         for (i = 0; i < im->rows; i++) {
298                         
299                 mono_metadata_decode_row (im, i, im_cols, 4);
300
301                 if ((im_cols[1] >> 1) == idx + 1) {
302
303                         import = mono_metadata_string_heap (image, im_cols [2]);
304
305                         mono_metadata_decode_row (mr, im_cols [3] - 1, mr_cols,
306                                                   1);
307                         
308                         scope = mono_metadata_string_heap (image, mr_cols [0]);
309                 }
310         }
311
312         piinfo->piflags = im_cols [0];
313
314         g_assert (import && scope);
315
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);
319
320         mh->addr = NULL;
321         if (!gmodule) {
322                 if (!(gmodule=g_module_open (scope, G_MODULE_BIND_LAZY))) {
323                         g_warning ("Failed to load library %s (%s)", full_name, scope);
324                         g_free (full_name);
325                         return;
326                 }
327         }
328         g_free (full_name);
329
330         g_module_symbol (gmodule, import, &mh->addr); 
331
332         if (!mh->addr) {
333                 g_warning ("Failed to load function %s from %s", import, scope);
334                 return;
335         }
336
337         mh->flags |= METHOD_ATTRIBUTE_PINVOKE_IMPL;
338 }
339
340 MonoMethod *
341 mono_get_method (MonoImage *image, guint32 token, MonoClass *klass)
342 {
343         MonoMethod *result;
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;
348         int size;
349         guint32 cols [MONO_TYPEDEF_SIZE];
350
351         if ((result = g_hash_table_lookup (image->method_cache, GINT_TO_POINTER (token))))
352                         return result;
353
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);
360                 return result;
361         }
362
363         mono_metadata_decode_row (&tables [table], idx - 1, cols, 6);
364
365         if ((cols [2] & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
366             (cols [1] & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))
367                 result = (MonoMethod *)g_new0 (MonoMethodPInvoke, 1);
368         else 
369                 result = (MonoMethod *)g_new0 (MonoMethodNormal, 1);
370         
371         result->slot = -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]);
376
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);
381
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);
385         }
386
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);
392         } else {
393                 /* if this is a methodref from another module/assembly, this fails */
394                 loc = mono_cli_rva_map ((MonoCLIImageInfo *)image->image_info, cols [0]);
395
396                 if (!result->klass->dummy && !(result->flags & METHOD_ATTRIBUTE_ABSTRACT) &&
397                                         !(result->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME)) {
398                         g_assert (loc);
399                         ((MonoMethodNormal *)result)->header = mono_metadata_parse_mh (image, loc);
400                 }
401         }
402
403         g_hash_table_insert (image->method_cache, GINT_TO_POINTER (token), result);
404
405         return result;
406 }
407
408 void
409 mono_free_method  (MonoMethod *method)
410 {
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);
417         }
418
419         g_free (method);
420 }
421
422 void
423 mono_method_get_param_names (MonoMethod *method, const char **names)
424 {
425         int i, lastp;
426         MonoClass *klass = method->klass;
427         MonoTableInfo *methodt = &klass->image->tables [MONO_TABLE_METHOD];
428         MonoTableInfo *paramt = &klass->image->tables [MONO_TABLE_PARAM];
429
430         if (!method->signature->param_count)
431                 return;
432         for (i = 0; i < method->signature->param_count; ++i)
433                 names [i] = "";
434
435         mono_class_init (klass);
436         if (!klass->methods)
437                 return;
438
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);
444
445                         if (idx + 1 < methodt->rows)
446                                 lastp = mono_metadata_decode_row_col (methodt, idx + 1, MONO_METHOD_PARAMLIST);
447                         else
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]);
453                         }
454                         return;
455                 }
456         }
457 }