update copyrights
[mono.git] / mono / mini / aot-runtime.c
1 /*
2  * aot-runtime.c: mono Ahead of Time compiler
3  *
4  * Author:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Zoltan Varga (vargaz@gmail.com)
7  *
8  * (C) 2002 Ximian, Inc.
9  * Copyright 2003-2011 Novell, Inc.
10  * Copyright 2011 Xamarin, Inc.
11  */
12
13 #include "config.h"
14 #include <sys/types.h>
15 #ifdef HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19 #include <string.h>
20 #ifdef HAVE_SYS_MMAN_H
21 #include <sys/mman.h>
22 #endif
23
24 #if HOST_WIN32
25 #include <winsock2.h>
26 #include <windows.h>
27 #endif
28
29 #ifdef HAVE_EXECINFO_H
30 #include <execinfo.h>
31 #endif
32
33 #include <errno.h>
34 #include <sys/stat.h>
35
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h>  /* for WIFEXITED, WEXITSTATUS */
38 #endif
39
40 #ifdef HAVE_DL_ITERATE_PHDR
41 #include <link.h>
42 #endif
43
44 #include <mono/metadata/tabledefs.h>
45 #include <mono/metadata/class.h>
46 #include <mono/metadata/object.h>
47 #include <mono/metadata/tokentype.h>
48 #include <mono/metadata/appdomain.h>
49 #include <mono/metadata/debug-helpers.h>
50 #include <mono/metadata/assembly.h>
51 #include <mono/metadata/metadata-internals.h>
52 #include <mono/metadata/marshal.h>
53 #include <mono/metadata/gc-internal.h>
54 #include <mono/metadata/monitor.h>
55 #include <mono/metadata/threads-types.h>
56 #include <mono/metadata/mono-endian.h>
57 #include <mono/utils/mono-logger-internal.h>
58 #include <mono/utils/mono-mmap.h>
59 #include "mono/utils/mono-compiler.h"
60 #include <mono/utils/mono-counters.h>
61
62 #include "mini.h"
63 #include "version.h"
64
65 #ifndef DISABLE_AOT
66
67 #ifdef TARGET_WIN32
68 #define SHARED_EXT ".dll"
69 #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
70 #define SHARED_EXT ".dylib"
71 #elif defined(__APPLE__) && defined(TARGET_X86) && !defined(__native_client_codegen__)
72 #define SHARED_EXT ".dylib"
73 #else
74 #define SHARED_EXT ".so"
75 #endif
76
77 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
78 #define ROUND_DOWN(VALUE,SIZE)  ((VALUE) & ~((SIZE) - 1))
79
80 typedef struct MonoAotModule {
81         char *aot_name;
82         /* Pointer to the Global Offset Table */
83         gpointer *got;
84         GHashTable *name_cache;
85         GHashTable *extra_methods;
86         /* Maps methods to their code */
87         GHashTable *method_to_code;
88         /* Maps pointers into the method info to the methods themselves */
89         GHashTable *method_ref_to_method;
90         MonoAssemblyName *image_names;
91         char **image_guids;
92         MonoAssembly *assembly;
93         MonoImage **image_table;
94         guint32 image_table_len;
95         gboolean out_of_date;
96         gboolean plt_inited;
97         guint8 *mem_begin;
98         guint8 *mem_end;
99         guint8 *code;
100         guint8 *code_end;
101         guint8 *plt;
102         guint8 *plt_end;
103         guint8 *blob;
104         gint32 *code_offsets;
105         /* This contains <offset, index> pairs sorted by offset */
106         /* This is needed because LLVM emitted methods can be in any order */
107         gint32 *sorted_code_offsets;
108         gint32 sorted_code_offsets_len;
109         guint32 *method_info_offsets;
110         guint32 *got_info_offsets;
111         guint32 *ex_info_offsets;
112         guint32 *class_info_offsets;
113         guint32 *methods_loaded;
114         guint16 *class_name_table;
115         guint32 *extra_method_table;
116         guint32 *extra_method_info_offsets;
117         guint8 *unwind_info;
118         guint8 *thumb_end;
119
120         /* Points to the mono EH data created by LLVM */
121         guint8 *mono_eh_frame;
122
123         /* Points to the trampolines */
124         guint8 *trampolines [MONO_AOT_TRAMP_NUM];
125         /* The first unused trampoline of each kind */
126         guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
127
128         MonoAotFileInfo info;
129
130         gpointer *globals;
131         MonoDl *sofile;
132 } MonoAotModule;
133
134 static GHashTable *aot_modules;
135 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
136 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
137 static CRITICAL_SECTION aot_mutex;
138
139 /* 
140  * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
141  * AOT modules registered by mono_aot_register_module ().
142  */
143 static GHashTable *static_aot_modules;
144
145 /*
146  * Maps MonoJitInfo* to the aot module they belong to, this can be different
147  * from ji->method->klass->image's aot module for generic instances.
148  */
149 static GHashTable *ji_to_amodule;
150
151 /*
152  * Whenever to AOT compile loaded assemblies on demand and store them in
153  * a cache under $HOME/.mono/aot-cache.
154  */
155 static gboolean use_aot_cache = FALSE;
156
157 /*
158  * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
159  * use_aot_cache is TRUE.
160  */
161 static gboolean spawn_compiler = TRUE;
162
163 /* For debugging */
164 static gint32 mono_last_aot_method = -1;
165
166 static gboolean make_unreadable = FALSE;
167 static guint32 name_table_accesses = 0;
168 static guint32 n_pagefaults = 0;
169
170 /* Used to speed-up find_aot_module () */
171 static gsize aot_code_low_addr = (gssize)-1;
172 static gsize aot_code_high_addr = 0;
173
174 static GHashTable *aot_jit_icall_hash;
175
176 static void
177 init_plt (MonoAotModule *info);
178
179 /*****************************************************/
180 /*                 AOT RUNTIME                       */
181 /*****************************************************/
182
183 /*
184  * load_image:
185  *
186  *   Load one of the images referenced by AMODULE. Returns NULL if the image is not
187  * found, and sets the loader error if SET_ERROR is TRUE.
188  */
189 static MonoImage *
190 load_image (MonoAotModule *amodule, int index, gboolean set_error)
191 {
192         MonoAssembly *assembly;
193         MonoImageOpenStatus status;
194
195         g_assert (index < amodule->image_table_len);
196
197         if (amodule->image_table [index])
198                 return amodule->image_table [index];
199         if (amodule->out_of_date)
200                 return NULL;
201
202         assembly = mono_assembly_load (&amodule->image_names [index], amodule->assembly->basedir, &status);
203         if (!assembly) {
204                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", amodule->aot_name, amodule->image_names [index].name);
205                 amodule->out_of_date = TRUE;
206
207                 if (set_error) {
208                         char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
209                         mono_loader_set_error_assembly_load (full_name, FALSE);
210                         g_free (full_name);
211                 }
212                 return NULL;
213         }
214
215         if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
216                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable (GUID of dependent assembly %s doesn't match (expected '%s', got '%s').\n", amodule->aot_name, amodule->image_names [index].name, amodule->image_guids [index], assembly->image->guid);
217                 amodule->out_of_date = TRUE;
218                 return NULL;
219         }
220
221         amodule->image_table [index] = assembly->image;
222         return assembly->image;
223 }
224
225 static inline gint32
226 decode_value (guint8 *ptr, guint8 **rptr)
227 {
228         guint8 b = *ptr;
229         gint32 len;
230         
231         if ((b & 0x80) == 0){
232                 len = b;
233                 ++ptr;
234         } else if ((b & 0x40) == 0){
235                 len = ((b & 0x3f) << 8 | ptr [1]);
236                 ptr += 2;
237         } else if (b != 0xff) {
238                 len = ((b & 0x1f) << 24) |
239                         (ptr [1] << 16) |
240                         (ptr [2] << 8) |
241                         ptr [3];
242                 ptr += 4;
243         }
244         else {
245                 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
246                 ptr += 5;
247         }
248         if (rptr)
249                 *rptr = ptr;
250
251         //printf ("DECODE: %d.\n", len);
252         return len;
253 }
254
255 /*
256  * mono_aot_get_method:
257  *
258  *   Decode an offset table emitted by emit_offset_table (), returning the INDEXth
259  * entry.
260  */
261 static guint32
262 mono_aot_get_offset (guint32 *table, int index)
263 {
264         int i, group, ngroups, index_entry_size;
265         int start_offset, offset, noffsets, group_size;
266         guint8 *data_start, *p;
267         guint32 *index32 = NULL;
268         guint16 *index16 = NULL;
269         
270         noffsets = table [0];
271         group_size = table [1];
272         ngroups = table [2];
273         index_entry_size = table [3];
274         group = index / group_size;
275
276         if (index_entry_size == 2) {
277                 index16 = (guint16*)&table [4];
278                 data_start = (guint8*)&index16 [ngroups];
279                 p = data_start + index16 [group];
280         } else {
281                 index32 = (guint32*)&table [4];
282                 data_start = (guint8*)&index32 [ngroups];
283                 p = data_start + index32 [group];
284         }
285
286         /* offset will contain the value of offsets [group * group_size] */
287         offset = start_offset = decode_value (p, &p);
288         for (i = group * group_size + 1; i <= index; ++i) {
289                 offset += decode_value (p, &p);
290         }
291
292         //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
293
294         return offset;
295 }
296
297 static MonoMethod*
298 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
299
300 static MonoClass*
301 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
302
303 static MonoGenericInst*
304 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
305 {
306         int type_argc, i;
307         MonoType **type_argv;
308         MonoGenericInst *inst;
309         guint8 *p = buf;
310
311         type_argc = decode_value (p, &p);
312         type_argv = g_new0 (MonoType*, type_argc);
313
314         for (i = 0; i < type_argc; ++i) {
315                 MonoClass *pclass = decode_klass_ref (module, p, &p);
316                 if (!pclass) {
317                         g_free (type_argv);
318                         return NULL;
319                 }
320                 type_argv [i] = &pclass->byval_arg;
321         }
322
323         inst = mono_metadata_get_generic_inst (type_argc, type_argv);
324         g_free (type_argv);
325
326         *endbuf = p;
327
328         return inst;
329 }
330
331 static gboolean
332 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
333 {
334         gboolean has_class_inst, has_method_inst;
335         guint8 *p = buf;
336
337         has_class_inst = decode_value (p, &p);
338         if (has_class_inst) {
339                 ctx->class_inst = decode_generic_inst (module, p, &p);
340                 if (!ctx->class_inst)
341                         return FALSE;
342         }
343         has_method_inst = decode_value (p, &p);
344         if (has_method_inst) {
345                 ctx->method_inst = decode_generic_inst (module, p, &p);
346                 if (!ctx->method_inst)
347                         return FALSE;
348         }
349
350         *endbuf = p;
351         return TRUE;
352 }
353
354 static MonoClass*
355 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
356 {
357         MonoImage *image;
358         MonoClass *klass, *eklass;
359         guint32 token, rank;
360         guint8 *p = buf;
361
362         token = decode_value (p, &p);
363         if (token == 0) {
364                 *endbuf = p;
365                 return NULL;
366         }
367         if (mono_metadata_token_table (token) == 0) {
368                 image = load_image (module, decode_value (p, &p), TRUE);
369                 if (!image)
370                         return NULL;
371                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
372         } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
373                 if (token == MONO_TOKEN_TYPE_SPEC) {
374                         MonoTypeEnum type = decode_value (p, &p);
375
376                         if (type == MONO_TYPE_GENERICINST) {
377                                 MonoClass *gclass;
378                                 MonoGenericContext ctx;
379                                 MonoType *type;
380
381                                 gclass = decode_klass_ref (module, p, &p);
382                                 if (!gclass)
383                                         return NULL;
384                                 g_assert (gclass->generic_container);
385
386                                 memset (&ctx, 0, sizeof (ctx));
387                                 ctx.class_inst = decode_generic_inst (module, p, &p);
388                                 if (!ctx.class_inst)
389                                         return NULL;
390                                 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
391                                 klass = mono_class_from_mono_type (type);
392                                 mono_metadata_free_type (type);
393                         } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
394                                 MonoType *t;
395                                 MonoGenericContainer *container;
396
397                                 int num = decode_value (p, &p);
398                                 gboolean is_method = decode_value (p, &p);
399
400                                 if (is_method) {
401                                         MonoMethod *method_def;
402                                         g_assert (type == MONO_TYPE_MVAR);
403                                         method_def = decode_resolve_method_ref (module, p, &p);
404                                         if (!method_def)
405                                                 return NULL;
406
407                                         container = mono_method_get_generic_container (method_def);
408                                 } else {
409                                         MonoClass *class_def;
410                                         g_assert (type == MONO_TYPE_VAR);
411                                         class_def = decode_klass_ref (module, p, &p);
412                                         if (!class_def)
413                                                 return NULL;
414
415                                         container = class_def->generic_container;
416                                 }
417
418                                 g_assert (container);
419
420                                 // FIXME: Memory management
421                                 t = g_new0 (MonoType, 1);
422                                 t->type = type;
423                                 t->data.generic_param = mono_generic_container_get_param (container, num);
424
425                                 // FIXME: Maybe use types directly to avoid
426                                 // the overhead of creating MonoClass-es
427                                 klass = mono_class_from_mono_type (t);
428
429                                 g_free (t);
430                         } else {
431                                 g_assert_not_reached ();
432                         }
433                 } else {
434                         image = load_image (module, decode_value (p, &p), TRUE);
435                         if (!image)
436                                 return NULL;
437                         klass = mono_class_get (image, token);
438                 }
439         } else if (token == MONO_TOKEN_TYPE_DEF) {
440                 /* Array */
441                 image = load_image (module, decode_value (p, &p), TRUE);
442                 if (!image)
443                         return NULL;
444                 rank = decode_value (p, &p);
445                 eklass = decode_klass_ref (module, p, &p);
446                 klass = mono_array_class_get (eklass, rank);
447         } else {
448                 g_assert_not_reached ();
449         }
450         g_assert (klass);
451
452         *endbuf = p;
453         return klass;
454 }
455
456 static MonoClassField*
457 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
458 {
459         MonoClass *klass = decode_klass_ref (module, buf, &buf);
460         guint32 token;
461         guint8 *p = buf;
462
463         if (!klass)
464                 return NULL;
465
466         token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
467
468         *endbuf = p;
469
470         return mono_class_get_field (klass, token);
471 }
472
473 /* Stores information returned by decode_method_ref () */
474 typedef struct {
475         MonoImage *image;
476         guint32 token;
477         MonoMethod *method;
478         gboolean no_aot_trampoline;
479 } MethodRef;
480
481 /*
482  * decode_method_ref_with_target:
483  *
484  *   Decode a method reference, storing the image/token into a MethodRef structure.
485  * This avoids loading metadata for the method if the caller does not need it. If the method has
486  * no token, then it is loaded from metadata and ref->method is set to the method instance.
487  * If TARGET is non-NULL, abort decoding if it can be determined that the decoded method couldn't resolve to TARGET, and return FALSE.
488  */
489 static gboolean
490 decode_method_ref_with_target (MonoAotModule *module, MethodRef *ref, MonoMethod *target, guint8 *buf, guint8 **endbuf)
491 {
492         guint32 image_index, value;
493         MonoImage *image = NULL;
494         guint8 *p = buf;
495
496         memset (ref, 0, sizeof (MethodRef));
497
498         value = decode_value (p, &p);
499         image_index = value >> 24;
500
501         if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
502                 ref->no_aot_trampoline = TRUE;
503                 value = decode_value (p, &p);
504                 image_index = value >> 24;
505         }
506
507         if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
508                 if (target && target->wrapper_type)
509                         return FALSE;
510         }
511
512         if (image_index == MONO_AOT_METHODREF_WRAPPER) {
513                 guint32 wrapper_type;
514
515                 wrapper_type = decode_value (p, &p);
516
517                 if (target && target->wrapper_type != wrapper_type)
518                         return FALSE;
519
520                 /* Doesn't matter */
521                 image = mono_defaults.corlib;
522
523                 switch (wrapper_type) {
524                 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
525                         MonoMethod *m = decode_resolve_method_ref (module, p, &p);
526
527                         if (!m)
528                                 return FALSE;
529                         mono_class_init (m->klass);
530                         ref->method = mono_marshal_get_remoting_invoke_with_check (m);
531                         break;
532                 }
533                 case MONO_WRAPPER_PROXY_ISINST: {
534                         MonoClass *klass = decode_klass_ref (module, p, &p);
535                         if (!klass)
536                                 return FALSE;
537                         ref->method = mono_marshal_get_proxy_cancast (klass);
538                         break;
539                 }
540                 case MONO_WRAPPER_LDFLD:
541                 case MONO_WRAPPER_LDFLDA:
542                 case MONO_WRAPPER_STFLD:
543                 case MONO_WRAPPER_ISINST: {
544                         MonoClass *klass = decode_klass_ref (module, p, &p);
545                         if (!klass)
546                                 return FALSE;
547                         if (wrapper_type == MONO_WRAPPER_LDFLD)
548                                 ref->method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
549                         else if (wrapper_type == MONO_WRAPPER_LDFLDA)
550                                 ref->method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
551                         else if (wrapper_type == MONO_WRAPPER_STFLD)
552                                 ref->method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
553                         else if (wrapper_type == MONO_WRAPPER_ISINST)
554                                 ref->method = mono_marshal_get_isinst (klass);
555                         else
556                                 g_assert_not_reached ();
557                         break;
558                 }
559                 case MONO_WRAPPER_LDFLD_REMOTE:
560                         ref->method = mono_marshal_get_ldfld_remote_wrapper (NULL);
561                         break;
562                 case MONO_WRAPPER_STFLD_REMOTE:
563                         ref->method = mono_marshal_get_stfld_remote_wrapper (NULL);
564                         break;
565                 case MONO_WRAPPER_ALLOC: {
566                         int atype = decode_value (p, &p);
567
568                         ref->method = mono_gc_get_managed_allocator_by_type (atype);
569                         g_assert (ref->method);
570                         break;
571                 }
572                 case MONO_WRAPPER_WRITE_BARRIER:
573                         ref->method = mono_gc_get_write_barrier ();
574                         break;
575                 case MONO_WRAPPER_STELEMREF:
576                         ref->method = mono_marshal_get_stelemref ();
577                         break;
578                 case MONO_WRAPPER_SYNCHRONIZED: {
579                         MonoMethod *m = decode_resolve_method_ref (module, p, &p);
580
581                         if (!m)
582                                 return FALSE;
583                         ref->method = mono_marshal_get_synchronized_wrapper (m);
584                         break;
585                 }
586                 case MONO_WRAPPER_UNKNOWN: {
587                         MonoMethodDesc *desc;
588                         MonoMethod *orig_method;
589                         int subtype = decode_value (p, &p);
590
591                         if (subtype == MONO_AOT_WRAPPER_PTR_TO_STRUCTURE || subtype == MONO_AOT_WRAPPER_STRUCTURE_TO_PTR) {
592                                 MonoClass *klass = decode_klass_ref (module, p, &p);
593                                 
594                                 if (!klass)
595                                         return FALSE;
596
597                                 g_assert (target);
598                                 if (klass != target->klass)
599                                         return FALSE;
600
601                                 if (subtype == MONO_AOT_WRAPPER_PTR_TO_STRUCTURE) {
602                                         if (strcmp (target->name, "PtrToStructure"))
603                                                 return FALSE;
604                                         ref->method = mono_marshal_get_ptr_to_struct (klass);
605                                 } else {
606                                         if (strcmp (target->name, "StructureToPtr"))
607                                                 return FALSE;
608                                         ref->method = mono_marshal_get_struct_to_ptr (klass);
609                                 }
610                         } else {
611                                 if (subtype == MONO_AOT_WRAPPER_MONITOR_ENTER)
612                                         desc = mono_method_desc_new ("Monitor:Enter", FALSE);
613                                 else if (subtype == MONO_AOT_WRAPPER_MONITOR_EXIT)
614                                         desc = mono_method_desc_new ("Monitor:Exit", FALSE);
615                                 else if (subtype == MONO_AOT_WRAPPER_MONITOR_ENTER_V4)
616                                         desc = mono_method_desc_new ("Monitor:Enter(object,bool&)", FALSE);
617                                 else
618                                         g_assert_not_reached ();
619                                 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
620                                 g_assert (orig_method);
621                                 mono_method_desc_free (desc);
622                                 ref->method = mono_monitor_get_fast_path (orig_method);
623                         }
624                         break;
625                 }
626                 case MONO_WRAPPER_RUNTIME_INVOKE: {
627                         /* Direct wrapper */
628                         MonoMethod *m = decode_resolve_method_ref (module, p, &p);
629
630                         if (!m)
631                                 return FALSE;
632                         ref->method = mono_marshal_get_runtime_invoke (m, FALSE);
633                         break;
634                 }
635                 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
636                         int subtype = decode_value (p, &p);
637
638                         if (subtype == MONO_AOT_WRAPPER_ELEMENT_ADDR) {
639                                 int rank = decode_value (p, &p);
640                                 int elem_size = decode_value (p, &p);
641
642                                 ref->method = mono_marshal_get_array_address (rank, elem_size);
643                         } else {
644                                 g_assert_not_reached ();
645                         }
646                         break;
647                 }
648                 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
649                         MonoMethod *m = decode_resolve_method_ref (module, p, &p);
650
651                         if (!m)
652                                 return FALSE;
653
654                         /* This should only happen when looking for an extra method */
655                         g_assert (target);
656                         if (mono_marshal_method_from_wrapper (target) == m)
657                                 ref->method = target;
658                         else
659                                 return FALSE;
660                         break;
661                 }
662                 case MONO_WRAPPER_CASTCLASS: {
663                         int subtype = decode_value (p, &p);
664
665                         if (subtype == MONO_AOT_WRAPPER_CASTCLASS_WITH_CACHE)
666                                 ref->method = mono_marshal_get_castclass_with_cache ();
667                         else if (subtype == MONO_AOT_WRAPPER_ISINST_WITH_CACHE)
668                                 ref->method = mono_marshal_get_isinst_with_cache ();
669                         else
670                                 g_assert_not_reached ();
671                         break;
672                 }
673                 default:
674                         g_assert_not_reached ();
675                 }
676         } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
677                 if (target)
678                         return FALSE;
679                 /* Can't decode these */
680                 g_assert_not_reached ();
681         } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
682                 image_index = decode_value (p, &p);
683                 ref->token = decode_value (p, &p);
684
685                 image = load_image (module, image_index, TRUE);
686                 if (!image)
687                         return FALSE;
688         } else if (image_index == MONO_AOT_METHODREF_GINST) {
689                 MonoClass *klass;
690                 MonoGenericContext ctx;
691
692                 /* 
693                  * These methods do not have a token which resolves them, so we 
694                  * resolve them immediately.
695                  */
696                 klass = decode_klass_ref (module, p, &p);
697                 if (!klass)
698                         return FALSE;
699
700                 if (target && target->klass != klass)
701                         return FALSE;
702
703                 image_index = decode_value (p, &p);
704                 ref->token = decode_value (p, &p);
705
706                 image = load_image (module, image_index, TRUE);
707                 if (!image)
708                         return FALSE;
709
710                 ref->method = mono_get_method_full (image, ref->token, NULL, NULL);
711                 if (!ref->method)
712                         return FALSE;
713
714                 memset (&ctx, 0, sizeof (ctx));
715
716                 if (FALSE && klass->generic_class) {
717                         ctx.class_inst = klass->generic_class->context.class_inst;
718                         ctx.method_inst = NULL;
719  
720                         ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
721                 }                       
722
723                 memset (&ctx, 0, sizeof (ctx));
724
725                 if (!decode_generic_context (module, &ctx, p, &p))
726                         return FALSE;
727
728                 ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
729         } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
730                 MonoClass *klass;
731                 int method_type;
732
733                 klass = decode_klass_ref (module, p, &p);
734                 if (!klass)
735                         return FALSE;
736                 method_type = decode_value (p, &p);
737                 switch (method_type) {
738                 case 0:
739                         ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
740                         break;
741                 case 1:
742                         ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
743                         break;
744                 case 2:
745                         ref->method = mono_class_get_method_from_name (klass, "Get", -1);
746                         break;
747                 case 3:
748                         ref->method = mono_class_get_method_from_name (klass, "Address", -1);
749                         break;
750                 case 4:
751                         ref->method = mono_class_get_method_from_name (klass, "Set", -1);
752                         break;
753                 default:
754                         g_assert_not_reached ();
755                 }
756         } else {
757                 g_assert (image_index < MONO_AOT_METHODREF_MIN);
758                 ref->token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
759
760                 image = load_image (module, image_index, TRUE);
761                 if (!image)
762                         return FALSE;
763         }
764
765         *endbuf = p;
766
767         ref->image = image;
768
769         return TRUE;
770 }
771
772 static gboolean
773 decode_method_ref (MonoAotModule *module, MethodRef *ref, guint8 *buf, guint8 **endbuf)
774 {
775         return decode_method_ref_with_target (module, ref, NULL, buf, endbuf);
776 }
777
778 /*
779  * decode_resolve_method_ref_with_target:
780  *
781  *   Similar to decode_method_ref, but resolve and return the method itself.
782  */
783 static MonoMethod*
784 decode_resolve_method_ref_with_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
785 {
786         MethodRef ref;
787         gboolean res;
788
789         res = decode_method_ref_with_target (module, &ref, target, buf, endbuf);
790         if (!res)
791                 return NULL;
792         if (ref.method)
793                 return ref.method;
794         if (!ref.image)
795                 return NULL;
796         return mono_get_method (ref.image, ref.token, NULL);
797 }
798
799 static MonoMethod*
800 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
801 {
802         return decode_resolve_method_ref_with_target (module, NULL, buf, endbuf);
803 }
804
805 static void
806 create_cache_structure (void)
807 {
808         const char *home;
809         char *tmp;
810         int err;
811
812         home = g_get_home_dir ();
813         if (!home)
814                 return;
815
816         tmp = g_build_filename (home, ".mono", NULL);
817         if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
818                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
819 #ifdef HOST_WIN32
820                 err = mkdir (tmp);
821 #else
822                 err = mkdir (tmp, 0777);
823 #endif
824                 if (err) {
825                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
826                         g_free (tmp);
827                         return;
828                 }
829         }
830         g_free (tmp);
831         tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
832         if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
833                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
834 #ifdef HOST_WIN32
835                 err = mkdir (tmp);
836 #else
837                 err = mkdir (tmp, 0777);
838 #endif
839                 if (err) {
840                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
841                         g_free (tmp);
842                         return;
843                 }
844         }
845         g_free (tmp);
846 }
847
848 /*
849  * load_aot_module_from_cache:
850  *
851  *  Experimental code to AOT compile loaded assemblies on demand. 
852  *
853  * FIXME: 
854  * - Add environment variable MONO_AOT_CACHE_OPTIONS
855  * - Add options for controlling the cache size
856  * - Handle full cache by deleting old assemblies lru style
857  * - Add options for excluding assemblies during development
858  * - Maybe add a threshold after an assembly is AOT compiled
859  * - invoking a new mono process is a security risk
860  * - recompile the AOT module if one of its dependencies changes
861  */
862 static MonoDl*
863 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
864 {
865         char *fname, *cmd, *tmp2, *aot_options;
866         const char *home;
867         MonoDl *module;
868         gboolean res;
869         gchar *out, *err;
870         gint exit_status;
871
872         *aot_name = NULL;
873
874         if (assembly->image->dynamic)
875                 return NULL;
876
877         create_cache_structure ();
878
879         home = g_get_home_dir ();
880
881         tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
882         fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
883         *aot_name = fname;
884         g_free (tmp2);
885
886         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
887         module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
888
889         if (!module) {
890                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
891
892                 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
893
894                 aot_options = g_strdup_printf ("outfile=%s", fname);
895
896                 if (spawn_compiler) {
897                         /* FIXME: security */
898                         /* FIXME: Has to pass the assembly loading path to the child process */
899                         cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
900
901                         res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
902
903 #if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
904                         if (res) {
905                                 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
906                                         mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
907                                 else
908                                         mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
909                                 g_free (out);
910                                 g_free (err);
911                         }
912 #endif
913                         g_free (cmd);
914                 } else {
915                         res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
916                         if (!res) {
917                                 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
918                         } else {
919                                 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
920                         }
921                 }
922
923                 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
924
925                 g_free (aot_options);
926         }
927
928         return module;
929 }
930
931 static void
932 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
933 {
934         if (globals) {
935                 int global_index;
936                 guint16 *table, *entry;
937                 guint16 table_size;
938                 guint32 hash;           
939
940                 /* The first entry points to the hash */
941                 table = globals [0];
942                 globals ++;
943
944                 table_size = table [0];
945                 table ++;
946
947                 hash = mono_metadata_str_hash (name) % table_size;
948
949                 entry = &table [hash * 2];
950
951                 /* Search the hash for the index into the globals table */
952                 global_index = -1;
953                 while (entry [0] != 0) {
954                         guint32 index = entry [0] - 1;
955                         guint32 next = entry [1];
956
957                         //printf ("X: %s %s\n", (char*)globals [index * 2], name);
958
959                         if (!strcmp (globals [index * 2], name)) {
960                                 global_index = index;
961                                 break;
962                         }
963
964                         if (next != 0) {
965                                 entry = &table [next * 2];
966                         } else {
967                                 break;
968                         }
969                 }
970
971                 if (global_index != -1)
972                         *value = globals [global_index * 2 + 1];
973                 else
974                         *value = NULL;
975         } else {
976                 char *err = mono_dl_symbol (module, name, value);
977
978                 if (err)
979                         g_free (err);
980         }
981 }
982
983 static gboolean
984 check_usable (MonoAssembly *assembly, MonoAotFileInfo *info, char **out_msg)
985 {
986         char *build_info;
987         char *msg = NULL;
988         gboolean usable = TRUE;
989         gboolean full_aot;
990         guint8 *blob;
991
992         if (strcmp (assembly->image->guid, info->assembly_guid)) {
993                 msg = g_strdup_printf ("doesn't match assembly");
994                 usable = FALSE;
995         }
996
997         build_info = mono_get_runtime_build_info ();
998         if (strlen (info->runtime_version) > 0 && strcmp (info->runtime_version, build_info)) {
999                 msg = g_strdup_printf ("compiled against runtime version '%s' while this runtime has version '%s'", info->runtime_version, build_info);
1000                 usable = FALSE;
1001         }
1002         g_free (build_info);
1003
1004         full_aot = info->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1005
1006         if (mono_aot_only && !full_aot) {
1007                 msg = g_strdup_printf ("not compiled with --aot=full");
1008                 usable = FALSE;
1009         }
1010         if (!mono_aot_only && full_aot) {
1011                 msg = g_strdup_printf ("compiled with --aot=full");
1012                 usable = FALSE;
1013         }
1014 #ifdef TARGET_ARM
1015         /* mono_arch_find_imt_method () requires this */
1016         if ((info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1017                 msg = g_strdup_printf ("compiled against LLVM");
1018                 usable = FALSE;
1019         }
1020         if (!(info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && mono_use_llvm) {
1021                 msg = g_strdup_printf ("not compiled against LLVM");
1022                 usable = FALSE;
1023         }
1024 #endif
1025         if (mini_get_debug_options ()->mdb_optimizations && !(info->flags & MONO_AOT_FILE_FLAG_DEBUG) && !full_aot) {
1026                 msg = g_strdup_printf ("not compiled for debugging");
1027                 usable = FALSE;
1028         }
1029
1030         blob = info->blob;
1031
1032         if (info->gc_name_index != -1) {
1033                 char *gc_name = (char*)&blob [info->gc_name_index];
1034                 const char *current_gc_name = mono_gc_get_gc_name ();
1035
1036                 if (strcmp (current_gc_name, gc_name) != 0) {
1037                         msg = g_strdup_printf ("compiled against GC %s, while the current runtime uses GC %s.\n", gc_name, current_gc_name);
1038                         usable = FALSE;
1039                 }
1040         }
1041
1042         *out_msg = msg;
1043         return usable;
1044 }
1045
1046 static void
1047 load_aot_module (MonoAssembly *assembly, gpointer user_data)
1048 {
1049         char *aot_name;
1050         MonoAotModule *amodule;
1051         MonoDl *sofile;
1052         gboolean usable = TRUE;
1053         char *version_symbol = NULL;
1054         char *msg = NULL;
1055         gpointer *globals = NULL;
1056         MonoAotFileInfo *info = NULL;
1057         int i, version;
1058         guint8 *blob;
1059         gboolean do_load_image = TRUE;
1060
1061         if (mono_compile_aot)
1062                 return;
1063
1064         if (assembly->image->aot_module)
1065                 /* 
1066                  * Already loaded. This can happen because the assembly loading code might invoke
1067                  * the assembly load hooks multiple times for the same assembly.
1068                  */
1069                 return;
1070
1071         if (assembly->image->dynamic)
1072                 return;
1073
1074         if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
1075                 return;
1076
1077         mono_aot_lock ();
1078         if (static_aot_modules)
1079                 info = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
1080         else
1081                 info = NULL;
1082         mono_aot_unlock ();
1083
1084         if (info) {
1085                 /* Statically linked AOT module */
1086                 sofile = NULL;
1087                 aot_name = g_strdup_printf ("%s", assembly->aname.name);
1088                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
1089                 globals = info->globals;
1090         } else {
1091                 if (use_aot_cache)
1092                         sofile = load_aot_module_from_cache (assembly, &aot_name);
1093                 else {
1094                         char *err;
1095                         aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
1096
1097                         sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1098
1099                         if (!sofile) {
1100                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
1101                                 g_free (err);
1102                         }
1103                 }
1104         }
1105
1106         if (!sofile && !globals) {
1107                 if (mono_aot_only) {
1108                         fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1109                         exit (1);
1110                 }
1111                 g_free (aot_name);
1112                 return;
1113         }
1114
1115         if (!info) {
1116                 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &version_symbol);
1117                 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&info);
1118         }
1119
1120         if (version_symbol) {
1121                 /* Old file format */
1122                 version = atoi (version_symbol);
1123         } else {
1124                 g_assert (info);
1125                 version = info->version;
1126         }
1127
1128         if (version != MONO_AOT_FILE_VERSION) {
1129                 msg = g_strdup_printf ("wrong file format version (expected %d got %d)", MONO_AOT_FILE_VERSION, version);
1130                 usable = FALSE;
1131         } else {
1132                 usable = check_usable (assembly, info, &msg);
1133         }
1134
1135         if (!usable) {
1136                 if (mono_aot_only) {
1137                         fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode: %s.\n", aot_name, msg);
1138                         exit (1);
1139                 } else {
1140                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable: %s.\n", aot_name, msg);
1141                 }
1142                 g_free (msg);
1143                 g_free (aot_name);
1144                 if (sofile)
1145                         mono_dl_close (sofile);
1146                 assembly->image->aot_module = NULL;
1147                 return;
1148         }
1149
1150         /* Sanity check */
1151         g_assert (info->double_align == __alignof__ (double));
1152         g_assert (info->long_align == __alignof__ (gint64));
1153
1154         blob = info->blob;
1155
1156         amodule = g_new0 (MonoAotModule, 1);
1157         amodule->aot_name = aot_name;
1158         amodule->assembly = assembly;
1159
1160         memcpy (&amodule->info, info, sizeof (*info));
1161
1162         amodule->got = amodule->info.got;
1163         amodule->got [0] = assembly->image;
1164         amodule->globals = globals;
1165         amodule->sofile = sofile;
1166         amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1167         amodule->blob = blob;
1168
1169         /* Read image table */
1170         {
1171                 guint32 table_len, i;
1172                 char *table = NULL;
1173
1174                 table = info->image_table;
1175                 g_assert (table);
1176
1177                 table_len = *(guint32*)table;
1178                 table += sizeof (guint32);
1179                 amodule->image_table = g_new0 (MonoImage*, table_len);
1180                 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1181                 amodule->image_guids = g_new0 (char*, table_len);
1182                 amodule->image_table_len = table_len;
1183                 for (i = 0; i < table_len; ++i) {
1184                         MonoAssemblyName *aname = &(amodule->image_names [i]);
1185
1186                         aname->name = g_strdup (table);
1187                         table += strlen (table) + 1;
1188                         amodule->image_guids [i] = g_strdup (table);
1189                         table += strlen (table) + 1;
1190                         if (table [0] != 0)
1191                                 aname->culture = g_strdup (table);
1192                         table += strlen (table) + 1;
1193                         memcpy (aname->public_key_token, table, strlen (table) + 1);
1194                         table += strlen (table) + 1;                    
1195
1196                         table = ALIGN_PTR_TO (table, 8);
1197                         aname->flags = *(guint32*)table;
1198                         table += 4;
1199                         aname->major = *(guint32*)table;
1200                         table += 4;
1201                         aname->minor = *(guint32*)table;
1202                         table += 4;
1203                         aname->build = *(guint32*)table;
1204                         table += 4;
1205                         aname->revision = *(guint32*)table;
1206                         table += 4;
1207                 }
1208         }
1209
1210         amodule->code_offsets = info->code_offsets;
1211         amodule->code = info->methods;
1212 #ifdef TARGET_ARM
1213         /* Mask out thumb interop bit */
1214         amodule->code = (void*)((mgreg_t)amodule->code & ~1);
1215 #endif
1216         amodule->code_end = info->methods_end;
1217         amodule->method_info_offsets = info->method_info_offsets;
1218         amodule->ex_info_offsets = info->ex_info_offsets;
1219         amodule->class_info_offsets = info->class_info_offsets;
1220         amodule->class_name_table = info->class_name_table;
1221         amodule->extra_method_table = info->extra_method_table;
1222         amodule->extra_method_info_offsets = info->extra_method_info_offsets;
1223         amodule->got_info_offsets = info->got_info_offsets;
1224         amodule->unwind_info = info->unwind_info;
1225         amodule->mem_end = info->mem_end;
1226         amodule->mem_begin = amodule->code;
1227         amodule->plt = info->plt;
1228         amodule->plt_end = info->plt_end;
1229         amodule->mono_eh_frame = info->mono_eh_frame;
1230         amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC] = info->specific_trampolines;
1231         amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = info->static_rgctx_trampolines;
1232         amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK] = info->imt_thunks;
1233         amodule->thumb_end = info->thumb_end;
1234
1235         if (make_unreadable) {
1236 #ifndef TARGET_WIN32
1237                 guint8 *addr;
1238                 guint8 *page_start, *page_end;
1239                 int err, len;
1240
1241                 addr = amodule->mem_begin;
1242                 len = amodule->mem_end - amodule->mem_begin;
1243
1244                 /* Round down in both directions to avoid modifying data which is not ours */
1245                 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1246                 page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
1247                 if (page_end > page_start) {
1248                         err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
1249                         g_assert (err == 0);
1250                 }
1251 #endif
1252         }
1253
1254         mono_aot_lock ();
1255
1256         aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1257         aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1258
1259         g_hash_table_insert (aot_modules, assembly, amodule);
1260         mono_aot_unlock ();
1261
1262         mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1263
1264         assembly->image->aot_module = amodule;
1265
1266         if (mono_aot_only) {
1267                 if (mono_defaults.corlib) {
1268                         /* The second got slot contains the mscorlib got addr */
1269                         MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1270
1271                         amodule->got [1] = mscorlib_amodule->got;
1272                 } else {
1273                         amodule->got [1] = amodule->got;
1274                 }
1275         }
1276
1277         if (mono_gc_is_moving ()) {
1278                 MonoJumpInfo ji;
1279
1280                 memset (&ji, 0, sizeof (ji));
1281                 ji.type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
1282
1283                 amodule->got [2] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
1284         }
1285
1286         /*
1287          * Since we store methoddef and classdef tokens when referring to methods/classes in
1288          * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1289          * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1290          * non-lazily, since we can't handle out-of-date errors later.
1291          * The cached class info also depends on the exact assemblies.
1292          */
1293 #if defined(__native_client__)
1294         /* TODO: Don't 'load_image' on mscorlib due to a */
1295         /* recursive loading problem.  This should be    */
1296         /* removed if mscorlib is loaded from disk.      */
1297         if (strncmp(assembly->aname.name, "mscorlib", 8)) {
1298                 do_load_image = TRUE;
1299         } else {
1300                 do_load_image = FALSE;
1301         }
1302 #endif
1303         if (do_load_image) {
1304                 for (i = 0; i < amodule->image_table_len; ++i)
1305                         load_image (amodule, i, FALSE);
1306         }
1307
1308         if (amodule->out_of_date) {
1309                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name);
1310                 if (mono_aot_only) {
1311                         fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode because a dependency cannot be found or it is out of date.\n", aot_name);
1312                         exit (1);
1313                 }
1314         }
1315         else
1316                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1317 }
1318
1319 /*
1320  * mono_aot_register_globals:
1321  *
1322  *   This is called by the ctor function in AOT images compiled with the
1323  * 'no-dlsym' option.
1324  */
1325 void
1326 mono_aot_register_globals (gpointer *globals)
1327 {
1328         g_assert_not_reached ();
1329 }
1330
1331 /*
1332  * mono_aot_register_module:
1333  *
1334  *   This should be called by embedding code to register AOT modules statically linked
1335  * into the executable. AOT_INFO should be the value of the 
1336  * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1337  */
1338 void
1339 mono_aot_register_module (gpointer *aot_info)
1340 {
1341         gpointer *globals;
1342         char *aname;
1343         MonoAotFileInfo *info = (gpointer)aot_info;
1344
1345         g_assert (info->version == MONO_AOT_FILE_VERSION);
1346
1347         globals = info->globals;
1348         g_assert (globals);
1349
1350         aname = info->assembly_name;
1351
1352         /* This could be called before startup */
1353         if (aot_modules)
1354                 mono_aot_lock ();
1355
1356         if (!static_aot_modules)
1357                 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1358
1359         g_hash_table_insert (static_aot_modules, aname, info);
1360
1361         if (aot_modules)
1362                 mono_aot_unlock ();
1363 }
1364
1365 void
1366 mono_aot_init (void)
1367 {
1368         InitializeCriticalSection (&aot_mutex);
1369         aot_modules = g_hash_table_new (NULL, NULL);
1370
1371         mono_install_assembly_load_hook (load_aot_module, NULL);
1372
1373         if (g_getenv ("MONO_LASTAOT"))
1374                 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1375         if (g_getenv ("MONO_AOT_CACHE"))
1376                 use_aot_cache = TRUE;
1377 }
1378
1379 void
1380 mono_aot_cleanup (void)
1381 {
1382         if (aot_jit_icall_hash)
1383                 g_hash_table_destroy (aot_jit_icall_hash);
1384         if (aot_modules)
1385                 g_hash_table_destroy (aot_modules);
1386 }
1387
1388 static gboolean
1389 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1390 {
1391         guint32 flags;
1392         MethodRef ref;
1393         gboolean res;
1394
1395         info->vtable_size = decode_value (buf, &buf);
1396         if (info->vtable_size == -1)
1397                 /* Generic type */
1398                 return FALSE;
1399         flags = decode_value (buf, &buf);
1400         info->ghcimpl = (flags >> 0) & 0x1;
1401         info->has_finalize = (flags >> 1) & 0x1;
1402         info->has_cctor = (flags >> 2) & 0x1;
1403         info->has_nested_classes = (flags >> 3) & 0x1;
1404         info->blittable = (flags >> 4) & 0x1;
1405         info->has_references = (flags >> 5) & 0x1;
1406         info->has_static_refs = (flags >> 6) & 0x1;
1407         info->no_special_static_fields = (flags >> 7) & 0x1;
1408         info->is_generic_container = (flags >> 8) & 0x1;
1409
1410         if (info->has_cctor) {
1411                 res = decode_method_ref (module, &ref, buf, &buf);
1412                 if (!res)
1413                         return FALSE;
1414                 info->cctor_token = ref.token;
1415         }
1416         if (info->has_finalize) {
1417                 res = decode_method_ref (module, &ref, buf, &buf);
1418                 if (!res)
1419                         return FALSE;
1420                 info->finalize_image = ref.image;
1421                 info->finalize_token = ref.token;
1422         }
1423
1424         info->instance_size = decode_value (buf, &buf);
1425         info->class_size = decode_value (buf, &buf);
1426         info->packing_size = decode_value (buf, &buf);
1427         info->min_align = decode_value (buf, &buf);
1428
1429         *endbuf = buf;
1430
1431         return TRUE;
1432 }       
1433
1434 gpointer
1435 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1436 {
1437         int i;
1438         MonoClass *klass = vtable->klass;
1439         MonoAotModule *amodule = klass->image->aot_module;
1440         guint8 *info, *p;
1441         MonoCachedClassInfo class_info;
1442         gboolean err;
1443         MethodRef ref;
1444         gboolean res;
1445
1446         if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !amodule)
1447                 return NULL;
1448
1449         info = &amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1450         p = info;
1451
1452         err = decode_cached_class_info (amodule, &class_info, p, &p);
1453         if (!err)
1454                 return NULL;
1455
1456         for (i = 0; i < slot; ++i)
1457                 decode_method_ref (amodule, &ref, p, &p);
1458
1459         res = decode_method_ref (amodule, &ref, p, &p);
1460         if (!res)
1461                 return NULL;
1462         if (ref.no_aot_trampoline)
1463                 return NULL;
1464
1465         if (mono_metadata_token_index (ref.token) == 0)
1466                 return NULL;
1467
1468         return mono_aot_get_method_from_token (domain, ref.image, ref.token);
1469 }
1470
1471 gboolean
1472 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1473 {
1474         MonoAotModule *amodule = klass->image->aot_module;
1475         guint8 *p;
1476         gboolean err;
1477
1478         if (klass->rank || !amodule)
1479                 return FALSE;
1480
1481         p = (guint8*)&amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1482
1483         err = decode_cached_class_info (amodule, res, p, &p);
1484         if (!err)
1485                 return FALSE;
1486
1487         return TRUE;
1488 }
1489
1490 /**
1491  * mono_aot_get_class_from_name:
1492  *
1493  *  Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1494  * using a cache stored in the AOT file.
1495  * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1496  *
1497  * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was 
1498  * found.
1499  */
1500 gboolean
1501 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1502 {
1503         MonoAotModule *amodule = image->aot_module;
1504         guint16 *table, *entry;
1505         guint16 table_size;
1506         guint32 hash;
1507         char full_name_buf [1024];
1508         char *full_name;
1509         const char *name2, *name_space2;
1510         MonoTableInfo  *t;
1511         guint32 cols [MONO_TYPEDEF_SIZE];
1512         GHashTable *nspace_table;
1513
1514         if (!amodule || !amodule->class_name_table)
1515                 return FALSE;
1516
1517         mono_aot_lock ();
1518
1519         *klass = NULL;
1520
1521         /* First look in the cache */
1522         if (!amodule->name_cache)
1523                 amodule->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1524         nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
1525         if (nspace_table) {
1526                 *klass = g_hash_table_lookup (nspace_table, name);
1527                 if (*klass) {
1528                         mono_aot_unlock ();
1529                         return TRUE;
1530                 }
1531         }
1532
1533         table_size = amodule->class_name_table [0];
1534         table = amodule->class_name_table + 1;
1535
1536         if (name_space [0] == '\0')
1537                 full_name = g_strdup_printf ("%s", name);
1538         else {
1539                 if (strlen (name_space) + strlen (name) < 1000) {
1540                         sprintf (full_name_buf, "%s.%s", name_space, name);
1541                         full_name = full_name_buf;
1542                 } else {
1543                         full_name = g_strdup_printf ("%s.%s", name_space, name);
1544                 }
1545         }
1546         hash = mono_metadata_str_hash (full_name) % table_size;
1547         if (full_name != full_name_buf)
1548                 g_free (full_name);
1549
1550         entry = &table [hash * 2];
1551
1552         if (entry [0] != 0) {
1553                 t = &image->tables [MONO_TABLE_TYPEDEF];
1554
1555                 while (TRUE) {
1556                         guint32 index = entry [0];
1557                         guint32 next = entry [1];
1558                         guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1559
1560                         name_table_accesses ++;
1561
1562                         mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1563
1564                         name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1565                         name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1566
1567                         if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1568                                 mono_aot_unlock ();
1569                                 *klass = mono_class_get (image, token);
1570
1571                                 /* Add to cache */
1572                                 if (*klass) {
1573                                         mono_aot_lock ();
1574                                         nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
1575                                         if (!nspace_table) {
1576                                                 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1577                                                 g_hash_table_insert (amodule->name_cache, (char*)name_space2, nspace_table);
1578                                         }
1579                                         g_hash_table_insert (nspace_table, (char*)name2, *klass);
1580                                         mono_aot_unlock ();
1581                                 }
1582                                 return TRUE;
1583                         }
1584
1585                         if (next != 0) {
1586                                 entry = &table [next * 2];
1587                         } else {
1588                                 break;
1589                         }
1590                 }
1591         }
1592
1593         mono_aot_unlock ();
1594         
1595         return TRUE;
1596 }
1597
1598 /*
1599  * decode_mono_eh_frame:
1600  *
1601  *   Decode the EH information emitted by our modified LLVM compiler and construct a
1602  * MonoJitInfo structure from it.
1603  * LOCKING: Acquires the domain lock.
1604  */
1605 static MonoJitInfo*
1606 decode_llvm_mono_eh_frame (MonoAotModule *amodule, MonoDomain *domain, 
1607                                                    MonoMethod *method, guint8 *code, 
1608                                                    MonoJitExceptionInfo *clauses, int num_clauses,
1609                                                    int extra_size, GSList **nesting,
1610                                                    int *this_reg, int *this_offset)
1611 {
1612         guint8 *p;
1613         guint8 *fde, *cie, *code_start, *code_end;
1614         int version, fde_count;
1615         gint32 *table;
1616         int i, j, pos, left, right, offset, offset1, offset2, code_len;
1617         MonoJitExceptionInfo *ei;
1618         guint32 fde_len, ei_len, nested_len, nindex;
1619         gpointer *type_info;
1620         MonoJitInfo *jinfo;
1621         MonoLLVMFDEInfo info;
1622
1623         g_assert (amodule->mono_eh_frame);
1624
1625         p = amodule->mono_eh_frame;
1626
1627         /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
1628
1629         /* Header */
1630         version = *p;
1631         g_assert (version == 1);
1632         p ++;
1633         p = ALIGN_PTR_TO (p, 4);
1634
1635         fde_count = *(guint32*)p;
1636         p += 4;
1637         table = (gint32*)p;
1638
1639         /* There is +1 entry in the table */
1640         cie = p + ((fde_count + 1) * 8);
1641
1642         /* Binary search in the table to find the entry for code */
1643         offset = code - amodule->mono_eh_frame;
1644
1645         left = 0;
1646         right = fde_count;
1647         while (TRUE) {
1648                 pos = (left + right) / 2;
1649
1650                 offset1 = table [(pos * 2)];
1651                 if (pos + 1 == fde_count)
1652                         /* FIXME: */
1653                         offset2 = amodule->code_end - amodule->code;
1654                 else
1655                         offset2 = table [(pos + 1) * 2];
1656
1657                 if (offset < offset1)
1658                         right = pos;
1659                 else if (offset >= offset2)
1660                         left = pos + 1;
1661                 else
1662                         break;
1663         }
1664
1665         code_start = amodule->mono_eh_frame + table [(pos * 2)];
1666         /* This won't overflow because there is +1 entry in the table */
1667         code_end = amodule->mono_eh_frame + table [(pos * 2) + 2];
1668         code_len = code_end - code_start;
1669
1670         g_assert (code >= code_start && code < code_end);
1671
1672         fde = amodule->mono_eh_frame + table [(pos * 2) + 1];   
1673         /* This won't overflow because there is +1 entry in the table */
1674         fde_len = table [(pos * 2) + 2 + 1] - table [(pos * 2) + 1];
1675
1676         mono_unwind_decode_llvm_mono_fde (fde, fde_len, cie, code_start, &info);
1677         ei = info.ex_info;
1678         ei_len = info.ex_info_len;
1679         type_info = info.type_info;
1680         *this_reg = info.this_reg;
1681         *this_offset = info.this_offset;
1682
1683         /* Count number of nested clauses */
1684         nested_len = 0;
1685         for (i = 0; i < ei_len; ++i) {
1686                 /* This might be unaligned */
1687                 gint32 cindex1 = read32 (type_info [i]);
1688                 GSList *l;
1689
1690                 for (l = nesting [cindex1]; l; l = l->next) {
1691                         gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
1692
1693                         for (j = 0; j < ei_len; ++j) {
1694                                 gint32 cindex2 = read32 (type_info [j]);
1695
1696                                 if (cindex2 == nesting_cindex)
1697                                         nested_len ++;
1698                         }
1699                 }
1700         }
1701
1702         /*
1703          * LLVM might represent one IL region with multiple regions, so have to
1704          * allocate a new JI.
1705          */
1706         jinfo = 
1707                 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * (ei_len + nested_len)) + extra_size);
1708
1709         jinfo->code_size = code_len;
1710         jinfo->used_regs = mono_cache_unwind_info (info.unw_info, info.unw_info_len);
1711         jinfo->method = method;
1712         jinfo->code_start = code;
1713         jinfo->domain_neutral = 0;
1714         /* This signals that used_regs points to a normal cached unwind info */
1715         jinfo->from_aot = 0;
1716         jinfo->num_clauses = ei_len + nested_len;
1717
1718         for (i = 0; i < ei_len; ++i) {
1719                 /*
1720                  * orig_jinfo contains the original IL exception info saved by the AOT
1721                  * compiler, we have to combine that with the information produced by LLVM
1722                  */
1723                 /* The type_info entries contain IL clause indexes */
1724                 int clause_index = read32 (type_info [i]);
1725                 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
1726                 MonoJitExceptionInfo *orig_jei = &clauses [clause_index];
1727
1728                 g_assert (clause_index < num_clauses);
1729                 jei->flags = orig_jei->flags;
1730                 jei->data.catch_class = orig_jei->data.catch_class;
1731
1732                 jei->try_start = ei [i].try_start;
1733                 jei->try_end = ei [i].try_end;
1734                 jei->handler_start = ei [i].handler_start;
1735
1736                 /* Make sure we transition to thumb when a handler starts */
1737                 if (amodule->thumb_end && (guint8*)jei->handler_start < amodule->thumb_end)
1738                         jei->handler_start = (void*)((mgreg_t)jei->handler_start + 1);
1739         }
1740
1741         /* See exception_cb () in mini-llvm.c as to why this is needed */
1742         nindex = ei_len;
1743         for (i = 0; i < ei_len; ++i) {
1744                 gint32 cindex1 = read32 (type_info [i]);
1745                 GSList *l;
1746
1747                 for (l = nesting [cindex1]; l; l = l->next) {
1748                         gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
1749
1750                         for (j = 0; j < ei_len; ++j) {
1751                                 gint32 cindex2 = read32 (type_info [j]);
1752
1753                                 if (cindex2 == nesting_cindex) {
1754                                         /* 
1755                                          * The try interval comes from the nested clause, everything else from the
1756                                          * nesting clause.
1757                                          */
1758                                         memcpy (&jinfo->clauses [nindex], &jinfo->clauses [j], sizeof (MonoJitExceptionInfo));
1759                                         jinfo->clauses [nindex].try_start = jinfo->clauses [i].try_start;
1760                                         jinfo->clauses [nindex].try_end = jinfo->clauses [i].try_end;
1761                                         nindex ++;
1762                                 }
1763                         }
1764                 }
1765         }
1766         g_assert (nindex == ei_len + nested_len);
1767
1768         return jinfo;
1769 }
1770
1771 /*
1772  * LOCKING: Acquires the domain lock.
1773  */
1774 static MonoJitInfo*
1775 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain, 
1776                                                          MonoMethod *method, guint8* ex_info, guint8 *addr,
1777                                                          guint8 *code, guint32 code_len)
1778 {
1779         int i, buf_len, num_clauses;
1780         MonoJitInfo *jinfo;
1781         guint used_int_regs, flags;
1782         gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points, has_try_block_holes;
1783         gboolean from_llvm, has_gc_map;
1784         guint8 *p;
1785         int generic_info_size, try_holes_info_size, num_holes, this_reg = 0, this_offset = 0;
1786
1787         /* Load the method info from the AOT file */
1788
1789         p = ex_info;
1790         flags = decode_value (p, &p);
1791         has_generic_jit_info = (flags & 1) != 0;
1792         has_dwarf_unwind_info = (flags & 2) != 0;
1793         has_clauses = (flags & 4) != 0;
1794         has_seq_points = (flags & 8) != 0;
1795         from_llvm = (flags & 16) != 0;
1796         has_try_block_holes = (flags & 32) != 0;
1797         has_gc_map = (flags & 64) != 0;
1798
1799         if (has_dwarf_unwind_info) {
1800                 guint32 offset;
1801
1802                 offset = decode_value (p, &p);
1803                 g_assert (offset < (1 << 30));
1804                 used_int_regs = offset;
1805         } else {
1806                 used_int_regs = decode_value (p, &p);
1807         }
1808         if (has_generic_jit_info)
1809                 generic_info_size = sizeof (MonoGenericJitInfo);
1810         else
1811                 generic_info_size = 0;
1812
1813         if (has_try_block_holes) {
1814                 num_holes = decode_value (p, &p);
1815                 try_holes_info_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
1816         } else {
1817                 num_holes = try_holes_info_size = 0;
1818         }
1819         /* Exception table */
1820         if (has_clauses)
1821                 num_clauses = decode_value (p, &p);
1822         else
1823                 num_clauses = 0;
1824
1825         if (from_llvm) {
1826                 MonoJitExceptionInfo *clauses;
1827                 GSList **nesting;
1828
1829                 /*
1830                  * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
1831                  * section.
1832                  */
1833                 clauses = g_new0 (MonoJitExceptionInfo, num_clauses);
1834                 nesting = g_new0 (GSList*, num_clauses);
1835
1836                 for (i = 0; i < num_clauses; ++i) {
1837                         MonoJitExceptionInfo *ei = &clauses [i];
1838
1839                         ei->flags = decode_value (p, &p);
1840
1841                         if (decode_value (p, &p))
1842                                 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1843
1844                         /* Read the list of nesting clauses */
1845                         while (TRUE) {
1846                                 int nesting_index = decode_value (p, &p);
1847                                 if (nesting_index == -1)
1848                                         break;
1849                                 nesting [i] = g_slist_prepend (nesting [i], GINT_TO_POINTER (nesting_index));
1850                         }
1851                 }
1852
1853                 jinfo = decode_llvm_mono_eh_frame (amodule, domain, method, code, clauses, num_clauses, generic_info_size + try_holes_info_size, nesting, &this_reg, &this_offset);
1854                 jinfo->from_llvm = 1;
1855
1856                 g_free (clauses);
1857                 for (i = 0; i < num_clauses; ++i)
1858                         g_slist_free (nesting [i]);
1859                 g_free (nesting);
1860         } else {
1861                 jinfo = 
1862                         mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size + try_holes_info_size);
1863                 jinfo->num_clauses = num_clauses;
1864
1865                 for (i = 0; i < jinfo->num_clauses; ++i) {
1866                         MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1867
1868                         ei->flags = decode_value (p, &p);
1869
1870                         ei->exvar_offset = decode_value (p, &p);
1871
1872                         if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
1873                                 ei->data.filter = code + decode_value (p, &p);
1874                         else {
1875                                 if (decode_value (p, &p))
1876                                         ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1877                         }
1878
1879                         ei->try_start = code + decode_value (p, &p);
1880                         ei->try_end = code + decode_value (p, &p);
1881                         ei->handler_start = code + decode_value (p, &p);
1882                 }
1883
1884                 jinfo->code_size = code_len;
1885                 jinfo->used_regs = used_int_regs;
1886                 jinfo->method = method;
1887                 jinfo->code_start = code;
1888                 jinfo->domain_neutral = 0;
1889                 jinfo->from_aot = 1;
1890         }
1891
1892         if (has_generic_jit_info) {
1893                 MonoGenericJitInfo *gi;
1894
1895                 jinfo->has_generic_jit_info = 1;
1896
1897                 gi = mono_jit_info_get_generic_jit_info (jinfo);
1898                 g_assert (gi);
1899
1900                 if (from_llvm) {
1901                         gi->has_this = this_reg != -1;
1902                         gi->this_reg = this_reg;
1903                         gi->this_offset = this_offset;
1904                 } else {
1905                         gi->has_this = decode_value (p, &p);
1906                         gi->this_reg = decode_value (p, &p);
1907                         gi->this_offset = decode_value (p, &p);
1908                 }
1909
1910                 /* This currently contains no data */
1911                 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1912
1913                 jinfo->method = decode_resolve_method_ref (amodule, p, &p);
1914         }
1915
1916         if (has_try_block_holes) {
1917                 MonoTryBlockHoleTableJitInfo *table;
1918
1919                 jinfo->has_try_block_holes = 1;
1920
1921                 table = mono_jit_info_get_try_block_hole_table_info (jinfo);
1922                 g_assert (table);
1923
1924                 table->num_holes = (guint16)num_holes;
1925                 for (i = 0; i < num_holes; ++i) {
1926                         MonoTryBlockHoleJitInfo *hole = &table->holes [i];
1927                         hole->clause = decode_value (p, &p);
1928                         hole->length = decode_value (p, &p);
1929                         hole->offset = decode_value (p, &p);
1930                 }
1931         }
1932
1933         if (has_seq_points) {
1934                 MonoSeqPointInfo *seq_points;
1935                 int il_offset, native_offset, last_il_offset, last_native_offset, j;
1936
1937                 int len = decode_value (p, &p);
1938
1939                 seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
1940                 seq_points->len = len;
1941                 last_il_offset = last_native_offset = 0;
1942                 for (i = 0; i < len; ++i) {
1943                         SeqPoint *sp = &seq_points->seq_points [i];
1944                         il_offset = last_il_offset + decode_value (p, &p);
1945                         native_offset = last_native_offset + decode_value (p, &p);
1946
1947                         sp->il_offset = il_offset;
1948                         sp->native_offset = native_offset;
1949                         
1950                         sp->next_len = decode_value (p, &p);
1951                         sp->next = g_new (int, sp->next_len);
1952                         for (j = 0; j < sp->next_len; ++j)
1953                                 sp->next [j] = decode_value (p, &p);
1954
1955                         last_il_offset = il_offset;
1956                         last_native_offset = native_offset;
1957                 }
1958
1959                 mono_domain_lock (domain);
1960                 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
1961                 mono_domain_unlock (domain);
1962         }
1963
1964         /* Load debug info */
1965         buf_len = decode_value (p, &p);
1966         mono_debug_add_aot_method (domain, method, code, p, buf_len);
1967         p += buf_len;
1968
1969         if (has_gc_map) {
1970                 int map_size = decode_value (p, &p);
1971                 /* The GC map requires 4 bytes of alignment */
1972                 while ((guint64)(gsize)p % 4)
1973                         p ++;           
1974                 jinfo->gc_info = p;
1975                 p += map_size;
1976         }
1977
1978         if (amodule != jinfo->method->klass->image->aot_module) {
1979                 mono_aot_lock ();
1980                 if (!ji_to_amodule)
1981                         ji_to_amodule = g_hash_table_new (NULL, NULL);
1982                 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
1983                 mono_aot_unlock ();             
1984         }
1985
1986         return jinfo;
1987 }
1988
1989 /*
1990  * mono_aot_get_unwind_info:
1991  *
1992  *   Return a pointer to the DWARF unwind info belonging to JI.
1993  */
1994 guint8*
1995 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
1996 {
1997         MonoAotModule *amodule = ji->method->klass->image->aot_module;
1998         guint8 *p;
1999         guint8 *code = ji->code_start;
2000
2001         g_assert (amodule);
2002         g_assert (ji->from_aot);
2003
2004         if (!(code >= amodule->code && code <= amodule->code_end)) {
2005                 /* ji belongs to a different aot module than amodule */
2006                 mono_aot_lock ();
2007                 g_assert (ji_to_amodule);
2008                 amodule = g_hash_table_lookup (ji_to_amodule, ji);
2009                 g_assert (amodule);
2010                 g_assert (code >= amodule->code && code <= amodule->code_end);
2011                 mono_aot_unlock ();
2012         }
2013
2014         p = amodule->unwind_info + ji->used_regs;
2015         *unwind_info_len = decode_value (p, &p);
2016         return p;
2017 }
2018
2019 static G_GNUC_UNUSED int
2020 compare_ints (const void *a, const void *b)
2021 {
2022         return *(gint32*)a - *(gint32*)b;
2023 }
2024
2025 static void
2026 msort_code_offsets_internal (gint32 *array, int lo, int hi, gint32 *scratch)
2027 {
2028         int mid = (lo + hi) / 2;
2029         int i, t_lo, t_hi;
2030
2031         if (lo >= hi)
2032                 return;
2033
2034         if (hi - lo < 32) {
2035                 for (i = lo; i < hi; ++i)
2036                         if (array [(i * 2)] > array [(i * 2) + 2])
2037                                 break;
2038                 if (i == hi)
2039                         /* Already sorted */
2040                         return;
2041         }
2042
2043         msort_code_offsets_internal (array, lo, mid, scratch);
2044         msort_code_offsets_internal (array, mid + 1, hi, scratch);
2045
2046         if (array [mid * 2] < array [(mid + 1) * 2])
2047                 return;
2048
2049         /* Merge */
2050         t_lo = lo;
2051         t_hi = mid + 1;
2052         for (i = lo; i <= hi; i ++) {
2053                 if (t_lo <= mid && ((t_hi > hi) || array [t_lo * 2] < array [t_hi * 2])) {
2054                         scratch [(i * 2)] = array [t_lo * 2];
2055                         scratch [(i * 2) + 1] = array [(t_lo *2) + 1];
2056                         t_lo ++;
2057                 } else {
2058                         scratch [(i * 2)] = array [t_hi * 2];
2059                         scratch [(i * 2) + 1] = array [(t_hi *2) + 1];
2060                         t_hi ++;
2061                 }
2062         }
2063         for (i = lo; i <= hi; ++i) {
2064                 array [(i * 2)] = scratch [i * 2];
2065                 array [(i * 2) + 1] = scratch [(i * 2) + 1];
2066         }
2067 }
2068
2069 static void
2070 msort_code_offsets (gint32 *array, int len)
2071 {
2072         gint32 *scratch;
2073
2074         scratch = g_new (gint32, len * 2);
2075         msort_code_offsets_internal (array, 0, len - 1, scratch);
2076         g_free (scratch);
2077 }
2078
2079 MonoJitInfo *
2080 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2081 {
2082         int pos, left, right, offset, offset1, offset2, code_len;
2083         int method_index, table_len, is_wrapper;
2084         guint32 token;
2085         MonoAotModule *amodule = image->aot_module;
2086         MonoMethod *method;
2087         MonoJitInfo *jinfo;
2088         guint8 *code, *ex_info, *p;
2089         guint32 *table;
2090         int nmethods = amodule->info.nmethods;
2091         gint32 *code_offsets;
2092         int offsets_len, i;
2093
2094         if (!amodule)
2095                 return NULL;
2096
2097         if (domain != mono_get_root_domain ())
2098                 /* FIXME: */
2099                 return NULL;
2100
2101         offset = (guint8*)addr - amodule->code;
2102
2103         /* Compute a sorted table mapping code offsets to method indexes. */
2104         if (!amodule->sorted_code_offsets) {
2105                 code_offsets = g_new0 (gint32, nmethods * 2);
2106                 offsets_len = 0;
2107                 for (i = 0; i < nmethods; ++i) {
2108                         /* Skip the -1 entries to speed up sorting */
2109                         if (amodule->code_offsets [i] == 0xffffffff)
2110                                 continue;
2111                         code_offsets [(offsets_len * 2)] = amodule->code_offsets [i];
2112                         code_offsets [(offsets_len *2) + 1] = i;
2113                         offsets_len ++;
2114                 }
2115                 /* Use a merge sort as this is mostly sorted */
2116                 msort_code_offsets (code_offsets, offsets_len);
2117                 //qsort (code_offsets, offsets_len, sizeof (gint32) * 2, compare_ints);
2118                 for (i = 0; i < offsets_len -1; ++i)
2119                         g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
2120
2121                 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
2122                         /* Somebody got in before us */
2123                         g_free (code_offsets);
2124                 amodule->sorted_code_offsets_len = offsets_len;
2125         }
2126
2127         code_offsets = amodule->sorted_code_offsets;
2128         offsets_len = amodule->sorted_code_offsets_len;
2129
2130         /* Binary search in the sorted_code_offsets table */
2131         left = 0;
2132         right = offsets_len;
2133         while (TRUE) {
2134                 pos = (left + right) / 2;
2135
2136                 offset1 = code_offsets [(pos * 2)];
2137                 if (pos + 1 == offsets_len)
2138                         offset2 = amodule->code_end - amodule->code;
2139                 else
2140                         offset2 = code_offsets [(pos + 1) * 2];
2141
2142                 if (offset < offset1)
2143                         right = pos;
2144                 else if (offset >= offset2)
2145                         left = pos + 1;
2146                 else
2147                         break;
2148         }
2149
2150         g_assert (offset >= code_offsets [(pos * 2)]);
2151         if (pos + 1 < offsets_len)
2152                 g_assert (offset < code_offsets [((pos + 1) * 2)]);
2153         method_index = code_offsets [(pos * 2) + 1];
2154
2155         code = &amodule->code [amodule->code_offsets [method_index]];
2156         ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
2157
2158         if (pos == offsets_len - 1)
2159                 code_len = amodule->code_end - code;
2160         else
2161                 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
2162
2163         g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
2164
2165         /* Might be a wrapper/extra method */
2166         if (amodule->extra_methods) {
2167                 mono_aot_lock ();
2168                 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
2169                 mono_aot_unlock ();
2170         } else {
2171                 method = NULL;
2172         }
2173
2174         if (!method) {
2175                 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
2176                         /* 
2177                          * This is hit for extra methods which are called directly, so they are
2178                          * not in amodule->extra_methods.
2179                          */
2180                         table_len = amodule->extra_method_info_offsets [0];
2181                         table = amodule->extra_method_info_offsets + 1;
2182                         left = 0;
2183                         right = table_len;
2184                         pos = 0;
2185
2186                         /* Binary search */
2187                         while (TRUE) {
2188                                 pos = ((left + right) / 2);
2189
2190                                 g_assert (pos < table_len);
2191
2192                                 if (table [pos * 2] < method_index)
2193                                         left = pos + 1;
2194                                 else if (table [pos * 2] > method_index)
2195                                         right = pos;
2196                                 else
2197                                         break;
2198                         }
2199
2200                         p = amodule->blob + table [(pos * 2) + 1];
2201                         is_wrapper = decode_value (p, &p);
2202                         g_assert (!is_wrapper);
2203                         method = decode_resolve_method_ref (amodule, p, &p);
2204                         g_assert (method);
2205                 } else {
2206                         token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
2207                         method = mono_get_method (image, token, NULL);
2208                 }
2209         }
2210
2211         /* FIXME: */
2212         g_assert (method);
2213
2214         //printf ("F: %s\n", mono_method_full_name (method, TRUE));
2215         
2216         jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
2217
2218         g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
2219         g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
2220
2221         /* Add it to the normal JitInfo tables */
2222         mono_jit_info_table_add (domain, jinfo);
2223         
2224         return jinfo;
2225 }
2226
2227 static gboolean
2228 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
2229 {
2230         guint8 *p = buf;
2231         gpointer *table;
2232         MonoImage *image;
2233         int i;
2234
2235         switch (ji->type) {
2236         case MONO_PATCH_INFO_METHOD:
2237         case MONO_PATCH_INFO_METHOD_JUMP:
2238         case MONO_PATCH_INFO_ICALL_ADDR:
2239         case MONO_PATCH_INFO_METHOD_RGCTX: {
2240                 MethodRef ref;
2241                 gboolean res;
2242
2243                 res = decode_method_ref (aot_module, &ref, p, &p);
2244                 if (!res)
2245                         goto cleanup;
2246
2247                 if (!ref.method && !mono_aot_only && !ref.no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (ref.token) == MONO_TABLE_METHOD)) {
2248                         ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (ref.image, ref.token));
2249                         ji->type = MONO_PATCH_INFO_ABS;
2250                 }
2251                 else {
2252                         if (ref.method)
2253                                 ji->data.method = ref.method;
2254                         else
2255                                 ji->data.method = mono_get_method (ref.image, ref.token, NULL);
2256                         g_assert (ji->data.method);
2257                         mono_class_init (ji->data.method->klass);
2258                 }
2259                 break;
2260         }
2261         case MONO_PATCH_INFO_INTERNAL_METHOD:
2262         case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2263                 guint32 len = decode_value (p, &p);
2264
2265                 ji->data.name = (char*)p;
2266                 p += len + 1;
2267                 break;
2268         }
2269         case MONO_PATCH_INFO_METHODCONST:
2270                 /* Shared */
2271                 ji->data.method = decode_resolve_method_ref (aot_module, p, &p);
2272                 if (!ji->data.method)
2273                         goto cleanup;
2274                 break;
2275         case MONO_PATCH_INFO_VTABLE:
2276         case MONO_PATCH_INFO_CLASS:
2277         case MONO_PATCH_INFO_IID:
2278         case MONO_PATCH_INFO_ADJUSTED_IID:
2279                 /* Shared */
2280                 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2281                 if (!ji->data.klass)
2282                         goto cleanup;
2283                 break;
2284         case MONO_PATCH_INFO_CLASS_INIT:
2285         case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2286                 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2287                 if (!ji->data.klass)
2288                         goto cleanup;
2289                 break;
2290         case MONO_PATCH_INFO_IMAGE:
2291                 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
2292                 if (!ji->data.image)
2293                         goto cleanup;
2294                 break;
2295         case MONO_PATCH_INFO_FIELD:
2296         case MONO_PATCH_INFO_SFLDA:
2297                 /* Shared */
2298                 ji->data.field = decode_field_info (aot_module, p, &p);
2299                 if (!ji->data.field)
2300                         goto cleanup;
2301                 break;
2302         case MONO_PATCH_INFO_SWITCH:
2303                 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
2304                 ji->data.table->table_size = decode_value (p, &p);
2305                 table = mono_domain_alloc (mono_domain_get (), sizeof (gpointer) * ji->data.table->table_size);
2306                 ji->data.table->table = (MonoBasicBlock**)table;
2307                 for (i = 0; i < ji->data.table->table_size; i++)
2308                         table [i] = (gpointer)(gssize)decode_value (p, &p);
2309                 break;
2310         case MONO_PATCH_INFO_R4: {
2311                 guint32 val;
2312                 
2313                 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
2314                 val = decode_value (p, &p);
2315                 *(float*)ji->data.target = *(float*)&val;
2316                 break;
2317         }
2318         case MONO_PATCH_INFO_R8: {
2319                 guint32 val [2];
2320                 guint64 v;
2321
2322                 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
2323
2324                 val [0] = decode_value (p, &p);
2325                 val [1] = decode_value (p, &p);
2326                 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
2327                 *(double*)ji->data.target = *(double*)&v;
2328                 break;
2329         }
2330         case MONO_PATCH_INFO_LDSTR:
2331                 image = load_image (aot_module, decode_value (p, &p), TRUE);
2332                 if (!image)
2333                         goto cleanup;
2334                 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
2335                 break;
2336         case MONO_PATCH_INFO_RVA:
2337         case MONO_PATCH_INFO_DECLSEC:
2338         case MONO_PATCH_INFO_LDTOKEN:
2339         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2340                 /* Shared */
2341                 image = load_image (aot_module, decode_value (p, &p), TRUE);
2342                 if (!image)
2343                         goto cleanup;
2344                 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
2345
2346                 ji->data.token->has_context = decode_value (p, &p);
2347                 if (ji->data.token->has_context) {
2348                         gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
2349                         if (!res)
2350                                 goto cleanup;
2351                 }
2352                 break;
2353         case MONO_PATCH_INFO_EXC_NAME:
2354                 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2355                 if (!ji->data.klass)
2356                         goto cleanup;
2357                 ji->data.name = ji->data.klass->name;
2358                 break;
2359         case MONO_PATCH_INFO_METHOD_REL:
2360                 ji->data.offset = decode_value (p, &p);
2361                 break;
2362         case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2363         case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2364         case MONO_PATCH_INFO_MONITOR_ENTER:
2365         case MONO_PATCH_INFO_MONITOR_EXIT:
2366         case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
2367         case MONO_PATCH_INFO_CASTCLASS_CACHE:
2368                 break;
2369         case MONO_PATCH_INFO_RGCTX_FETCH: {
2370                 gboolean res;
2371                 MonoJumpInfoRgctxEntry *entry;
2372
2373                 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2374                 entry->method = decode_resolve_method_ref (aot_module, p, &p);
2375                 entry->in_mrgctx = decode_value (p, &p);
2376                 entry->info_type = decode_value (p, &p);
2377                 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2378                 entry->data->type = decode_value (p, &p);
2379                 
2380                 res = decode_patch (aot_module, mp, entry->data, p, &p);
2381                 if (!res)
2382                         goto cleanup;
2383                 ji->data.rgctx_entry = entry;
2384                 break;
2385         }
2386         case MONO_PATCH_INFO_SEQ_POINT_INFO:
2387                 break;
2388         case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
2389                 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
2390
2391                 imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
2392                 imt_tramp->vt_offset = decode_value (p, &p);
2393                 
2394                 ji->data.imt_tramp = imt_tramp;
2395                 break;
2396         }
2397         default:
2398                 g_warning ("unhandled type %d", ji->type);
2399                 g_assert_not_reached ();
2400         }
2401
2402         *endbuf = p;
2403
2404         return TRUE;
2405
2406  cleanup:
2407         return FALSE;
2408 }
2409
2410 static MonoJumpInfo*
2411 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches, 
2412                                  guint32 **got_slots, 
2413                                  guint8 *buf, guint8 **endbuf)
2414 {
2415         MonoJumpInfo *patches;
2416         int pindex;
2417         guint8 *p;
2418
2419         p = buf;
2420
2421         patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
2422
2423         *got_slots = g_malloc (sizeof (guint32) * n_patches);
2424
2425         for (pindex = 0; pindex < n_patches; ++pindex) {
2426                 MonoJumpInfo *ji = &patches [pindex];
2427                 guint8 *shared_p;
2428                 gboolean res;
2429                 guint32 got_offset;
2430
2431                 got_offset = decode_value (p, &p);
2432
2433                 if (aot_module->got [got_offset]) {
2434                         /* Already loaded */
2435                         //printf ("HIT!\n");
2436                 } else {
2437                         shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
2438
2439                         ji->type = decode_value (shared_p, &shared_p);
2440
2441                         res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
2442                         if (!res)
2443                                 goto cleanup;
2444                 }
2445
2446                 (*got_slots) [pindex] = got_offset;
2447         }
2448
2449         *endbuf = p;
2450         return patches;
2451
2452  cleanup:
2453         g_free (*got_slots);
2454         *got_slots = NULL;
2455
2456         return NULL;
2457 }
2458
2459 static void
2460 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
2461 {
2462         /*
2463          * Jump addresses cannot be patched by the trampoline code since it
2464          * does not have access to the caller's address. Instead, we collect
2465          * the addresses of the GOT slots pointing to a method, and patch
2466          * them after the method has been compiled.
2467          */
2468         MonoJitDomainInfo *info = domain_jit_info (domain);
2469         GSList *list;
2470                 
2471         mono_domain_lock (domain);
2472         if (!info->jump_target_got_slot_hash)
2473                 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
2474         list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
2475         list = g_slist_prepend (list, got_slot);
2476         g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
2477         mono_domain_unlock (domain);
2478 }
2479
2480 /*
2481  * load_method:
2482  *
2483  *   Load the method identified by METHOD_INDEX from the AOT image. Return a
2484  * pointer to the native code of the method, or NULL if not found.
2485  * METHOD might not be set if the caller only has the image/token info.
2486  */
2487 static gpointer
2488 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
2489 {
2490         MonoClass *klass;
2491         gboolean from_plt = method == NULL;
2492         MonoMemPool *mp;
2493         int i, pindex, n_patches, used_strings;
2494         gboolean keep_patches = TRUE;
2495         guint8 *p;
2496         MonoJitInfo *jinfo = NULL;
2497         guint8 *code, *info;
2498
2499         if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2500                 return NULL;
2501
2502         if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
2503                 /* Non shared AOT code can't be used in other appdomains */
2504                 return NULL;
2505
2506         if (amodule->out_of_date)
2507                 return NULL;
2508
2509         if (amodule->code_offsets [method_index] == 0xffffffff) {
2510                 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2511                         char *full_name;
2512
2513                         if (!method)
2514                                 method = mono_get_method (image, token, NULL);
2515                         full_name = mono_method_full_name (method, TRUE);
2516                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2517                         g_free (full_name);
2518                 }
2519                 return NULL;
2520         }
2521
2522         code = &amodule->code [amodule->code_offsets [method_index]];
2523
2524         info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
2525
2526         if (amodule->thumb_end && code < amodule->thumb_end) {
2527                 /* Convert this into a thumb address */
2528                 g_assert ((amodule->code_offsets [method_index] & 0x1) == 0);
2529                 code = &amodule->code [amodule->code_offsets [method_index] + 1];
2530         }
2531
2532         mono_aot_lock ();
2533         if (!amodule->methods_loaded)
2534                 amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods / 32 + 1);
2535         mono_aot_unlock ();
2536
2537         if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2538                 return code;
2539
2540         if (mono_last_aot_method != -1) {
2541                 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
2542                                 return NULL;
2543                 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
2544                         if (!method)
2545                                 method = mono_get_method (image, token, NULL);
2546                         if (method) {
2547                                 char *name = mono_method_full_name (method, TRUE);
2548                                 printf ("LAST AOT METHOD: %s.\n", name);
2549                                 g_free (name);
2550                         } else {
2551                                 printf ("LAST AOT METHOD: %p %d\n", code, method_index);
2552                         }
2553                 }
2554         }
2555
2556         p = info;
2557
2558         if (method) {
2559                 klass = method->klass;
2560                 decode_klass_ref (amodule, p, &p);
2561         } else {
2562                 klass = decode_klass_ref (amodule, p, &p);
2563         }
2564
2565         if (amodule->info.opts & MONO_OPT_SHARED)
2566                 used_strings = decode_value (p, &p);
2567         else
2568                 used_strings = 0;
2569
2570         for (i = 0; i < used_strings; i++) {
2571                 guint token = decode_value (p, &p);
2572                 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2573         }
2574
2575         if (amodule->info.opts & MONO_OPT_SHARED)       
2576                 keep_patches = FALSE;
2577
2578         n_patches = decode_value (p, &p);
2579
2580         keep_patches = FALSE;
2581
2582         if (n_patches) {
2583                 MonoJumpInfo *patches;
2584                 guint32 *got_slots;
2585
2586                 if (keep_patches)
2587                         mp = domain->mp;
2588                 else
2589                         mp = mono_mempool_new ();
2590
2591                 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2592                 if (patches == NULL)
2593                         goto cleanup;
2594
2595                 for (pindex = 0; pindex < n_patches; ++pindex) {
2596                         MonoJumpInfo *ji = &patches [pindex];
2597
2598                         if (!amodule->got [got_slots [pindex]]) {
2599                                 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2600                                 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2601                                         amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
2602                                 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2603                                         register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
2604                         }
2605                         ji->type = MONO_PATCH_INFO_NONE;
2606                 }
2607
2608                 g_free (got_slots);
2609
2610                 if (!keep_patches)
2611                         mono_mempool_destroy (mp);
2612         }
2613
2614         if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2615                 char *full_name;
2616
2617                 if (!method)
2618                         method = mono_get_method (image, token, NULL);
2619
2620                 full_name = mono_method_full_name (method, TRUE);
2621
2622                 if (!jinfo)
2623                         jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
2624
2625                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND method %s [%p - %p %p]\n", full_name, code, code + jinfo->code_size, info);
2626                 g_free (full_name);
2627         }
2628
2629         mono_aot_lock ();
2630
2631         InterlockedIncrement (&mono_jit_stats.methods_aot);
2632
2633         amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2634
2635         init_plt (amodule);
2636
2637         if (method && method->wrapper_type)
2638                 g_hash_table_insert (amodule->method_to_code, method, code);
2639
2640         mono_aot_unlock ();
2641
2642         if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
2643                 MonoJitInfo *jinfo;
2644
2645                 if (!method) {
2646                         method = mono_get_method (image, token, NULL);
2647                         g_assert (method);
2648                 }
2649                 mono_profiler_method_jit (method);
2650                 jinfo = mono_jit_info_table_find (domain, (char*)code);
2651                 g_assert (jinfo);
2652                 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
2653         }
2654
2655         if (from_plt && klass && !klass->generic_container)
2656                 mono_runtime_class_init (mono_class_vtable (domain, klass));
2657
2658         return code;
2659
2660  cleanup:
2661         /* FIXME: The space in domain->mp is wasted */  
2662         if (amodule->info.opts & MONO_OPT_SHARED)
2663                 /* No need to cache patches */
2664                 mono_mempool_destroy (mp);
2665
2666         if (jinfo)
2667                 g_free (jinfo);
2668
2669         return NULL;
2670 }
2671
2672 static guint32
2673 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name)
2674 {
2675         guint32 table_size, entry_size, hash;
2676         guint32 *table, *entry;
2677         guint32 index;
2678         static guint32 n_extra_decodes;
2679
2680         if (!amodule)
2681                 return 0xffffff;
2682
2683         table_size = amodule->extra_method_table [0];
2684         table = amodule->extra_method_table + 1;
2685         entry_size = 3;
2686
2687         hash = mono_aot_method_hash (method) % table_size;
2688
2689         entry = &table [hash * entry_size];
2690
2691         if (entry [0] == 0)
2692                 return 0xffffff;
2693
2694         index = 0xffffff;
2695         while (TRUE) {
2696                 guint32 key = entry [0];
2697                 guint32 value = entry [1];
2698                 guint32 next = entry [entry_size - 1];
2699                 MonoMethod *m;
2700                 guint8 *p;
2701                 int is_wrapper_name;
2702
2703                 p = amodule->blob + key;
2704                 is_wrapper_name = decode_value (p, &p);
2705                 if (is_wrapper_name) {
2706                         int wrapper_type = decode_value (p, &p);
2707                         if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2708                                 index = value;
2709                                 break;
2710                         }
2711                 } else {
2712                         guint8 *orig_p = p;
2713
2714                         mono_aot_lock ();
2715                         if (!amodule->method_ref_to_method)
2716                                 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2717                         m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2718                         mono_aot_unlock ();
2719                         if (!m) {
2720                                 m = decode_resolve_method_ref_with_target (amodule, method, p, &p);
2721                                 if (m) {
2722                                         mono_aot_lock ();
2723                                         g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2724                                         mono_aot_unlock ();
2725                                 }
2726                         }
2727                         if (m == method) {
2728                                 index = value;
2729                                 break;
2730                         }
2731
2732                         /* Special case: wrappers of shared generic methods */
2733                         if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2734                                 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2735                                 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2736                                 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2737
2738                                 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2739                                         index = value;
2740                                         break;
2741                                 }
2742                         }
2743
2744                         /* Methods decoded needlessly */
2745                         if (m) {
2746                                 //printf ("%d %s %s %p\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE), orig_p);
2747                                 n_extra_decodes ++;
2748                         }
2749                 }
2750
2751                 if (next != 0)
2752                         entry = &table [next * entry_size];
2753                 else
2754                         break;
2755         }
2756
2757         return index;
2758 }
2759
2760 static void
2761 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2762 {
2763         g_ptr_array_add ((GPtrArray*)user_data, value);
2764 }
2765
2766 /*
2767  * find_extra_method:
2768  *
2769  *   Try finding METHOD in the extra_method table in all AOT images.
2770  * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2771  * module where the method was found.
2772  */
2773 static guint32
2774 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2775 {
2776         guint32 index;
2777         GPtrArray *modules;
2778         int i;
2779         char *name = NULL;
2780
2781         if (method->wrapper_type)
2782                 name = mono_aot_wrapper_name (method);
2783
2784         /* Try the method's module first */
2785         *out_amodule = method->klass->image->aot_module;
2786         index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name);
2787         if (index != 0xffffff) {
2788                 g_free (name);
2789                 return index;
2790         }
2791
2792         /* 
2793          * Try all other modules.
2794          * This is needed because generic instances klass->image points to the image
2795          * containing the generic definition, but the native code is generated to the
2796          * AOT image which contains the reference.
2797          */
2798
2799         /* Make a copy to avoid doing the search inside the aot lock */
2800         modules = g_ptr_array_new ();
2801         mono_aot_lock ();
2802         g_hash_table_foreach (aot_modules, add_module_cb, modules);
2803         mono_aot_unlock ();
2804
2805         index = 0xffffff;
2806         for (i = 0; i < modules->len; ++i) {
2807                 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2808
2809                 if (amodule != method->klass->image->aot_module)
2810                         index = find_extra_method_in_amodule (amodule, method, name);
2811                 if (index != 0xffffff) {
2812                         *out_amodule = amodule;
2813                         break;
2814                 }
2815         }
2816         
2817         g_ptr_array_free (modules, TRUE);
2818
2819         g_free (name);
2820         return index;
2821 }
2822
2823 /*
2824  * mono_aot_get_method:
2825  *
2826  *   Return a pointer to the AOTed native code for METHOD if it can be found,
2827  * NULL otherwise.
2828  * On platforms with function pointers, this doesn't return a function pointer.
2829  */
2830 gpointer
2831 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2832 {
2833         MonoClass *klass = method->klass;
2834         guint32 method_index;
2835         MonoAotModule *amodule = klass->image->aot_module;
2836         guint8 *code;
2837
2838         if (!amodule)
2839                 return NULL;
2840
2841         if (amodule->out_of_date)
2842                 return NULL;
2843
2844         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2845                 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2846                 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2847                 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2848                 return NULL;
2849
2850         /*
2851          * Use the original method instead of its invoke-with-check wrapper.
2852          * This is not a problem when using full-aot, since it doesn't support
2853          * remoting.
2854          */
2855         if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
2856                 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
2857
2858         g_assert (klass->inited);
2859
2860         /* Find method index */
2861         if (method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE)) {
2862                 /* 
2863                  * For generic methods, we store the fully shared instance in place of the
2864                  * original method.
2865                  */
2866                 method = mono_method_get_declaring_generic_method (method);
2867                 method_index = mono_metadata_token_index (method->token) - 1;
2868         } else if (method->is_inflated || !method->token) {
2869                 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2870                 mono_aot_lock ();
2871                 code = g_hash_table_lookup (amodule->method_to_code, method);
2872                 mono_aot_unlock ();
2873                 if (code)
2874                         return code;
2875
2876                 method_index = find_extra_method (method, &amodule);
2877                 /*
2878                  * Special case the ICollection<T> wrappers for arrays, as they cannot
2879                  * be statically enumerated, and each wrapper ends up calling the same
2880                  * method in Array.
2881                  */
2882                 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
2883                         MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
2884
2885                         code = mono_aot_get_method (domain, m);
2886                         if (code) {
2887                                 if (mono_method_needs_static_rgctx_invoke (m, FALSE)) {
2888                                         code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code));
2889                                         /* The call above returns an ftnptr */
2890                                         code = mono_get_addr_from_ftnptr (code);
2891                                 }
2892
2893                                 return code;
2894                         }
2895                 }
2896
2897                 /*
2898                  * Special case Array.GetGenericValueImpl which is a generic icall.
2899                  * Generic sharing currently can't handle it, but the icall returns data using
2900                  * an out parameter, so the managed-to-native wrappers can share the same code.
2901                  */
2902                 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
2903                         MonoMethod *m;
2904                         MonoGenericContext ctx;
2905                         MonoType *args [16];
2906
2907                         if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
2908                                 /* Avoid recursion */
2909                                 return NULL;
2910
2911                         m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
2912                         g_assert (m);
2913
2914                         memset (&ctx, 0, sizeof (ctx));
2915                         args [0] = &mono_defaults.object_class->byval_arg;
2916                         ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2917
2918                         m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2919
2920                         /* 
2921                          * Get the code for the <object> instantiation which should be emitted into
2922                          * the mscorlib aot image by the AOT compiler.
2923                          */
2924                         code = mono_aot_get_method (domain, m);
2925                         if (code)
2926                                 return code;
2927                 }
2928
2929                 /* Same for CompareExchange<T> and Exchange<T> */
2930                 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass->image == mono_defaults.corlib && !strcmp (method->klass->name_space, "System.Threading") && !strcmp (method->klass->name, "Interlocked") && (!strcmp (method->name, "CompareExchange") || !strcmp (method->name, "Exchange")) && MONO_TYPE_IS_REFERENCE (mono_method_signature (method)->params [1])) {
2931                         MonoMethod *m;
2932                         MonoGenericContext ctx;
2933                         MonoType *args [16];
2934                         gpointer iter = NULL;
2935
2936                         while ((m = mono_class_get_methods (method->klass, &iter))) {
2937                                 if (mono_method_signature (m)->generic_param_count && !strcmp (m->name, method->name))
2938                                         break;
2939                         }
2940                         g_assert (m);
2941
2942                         memset (&ctx, 0, sizeof (ctx));
2943                         args [0] = &mono_defaults.object_class->byval_arg;
2944                         ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2945
2946                         m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2947
2948                         /* Avoid recursion */
2949                         if (method == m)
2950                                 return NULL;
2951
2952                         /* 
2953                          * Get the code for the <object> instantiation which should be emitted into
2954                          * the mscorlib aot image by the AOT compiler.
2955                          */
2956                         code = mono_aot_get_method (domain, m);
2957                         if (code)
2958                                 return code;
2959                 }
2960
2961                 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, TRUE)) {
2962                         /* Partial sharing */
2963                         method_index = find_extra_method (mini_get_shared_method (method), &amodule);
2964                 }
2965
2966                 if (method_index == 0xffffff) {
2967                         if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2968                                 char *full_name;
2969
2970                                 full_name = mono_method_full_name (method, TRUE);
2971                                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2972                                 g_free (full_name);
2973                         }
2974                         return NULL;
2975                 }
2976
2977                 if (method_index == 0xffffff)
2978                         return NULL;
2979
2980                 /* Needed by find_jit_info */
2981                 mono_aot_lock ();
2982                 if (!amodule->extra_methods)
2983                         amodule->extra_methods = g_hash_table_new (NULL, NULL);
2984                 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2985                 mono_aot_unlock ();
2986         } else {
2987                 /* Common case */
2988                 method_index = mono_metadata_token_index (method->token) - 1;
2989         }
2990
2991         return load_method (domain, amodule, klass->image, method, method->token, method_index);
2992 }
2993
2994 /**
2995  * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2996  * method.
2997  */
2998 gpointer
2999 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3000 {
3001         MonoAotModule *aot_module = image->aot_module;
3002         int method_index;
3003
3004         if (!aot_module)
3005                 return NULL;
3006
3007         method_index = mono_metadata_token_index (token) - 1;
3008
3009         return load_method (domain, aot_module, image, NULL, token, method_index);
3010 }
3011
3012 typedef struct {
3013         guint8 *addr;
3014         gboolean res;
3015 } IsGotEntryUserData;
3016
3017 static void
3018 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
3019 {
3020         IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
3021         MonoAotModule *aot_module = (MonoAotModule*)value;
3022
3023         if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
3024                 data->res = TRUE;
3025 }
3026
3027 gboolean
3028 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3029 {
3030         IsGotEntryUserData user_data;
3031
3032         if (!aot_modules)
3033                 return FALSE;
3034
3035         user_data.addr = addr;
3036         user_data.res = FALSE;
3037         mono_aot_lock ();
3038         g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
3039         mono_aot_unlock ();
3040         
3041         return user_data.res;
3042 }
3043
3044 typedef struct {
3045         guint8 *addr;
3046         MonoAotModule *module;
3047 } FindAotModuleUserData;
3048
3049 static void
3050 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
3051 {
3052         FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
3053         MonoAotModule *aot_module = (MonoAotModule*)value;
3054
3055         if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
3056                 data->module = aot_module;
3057 }
3058
3059 static inline MonoAotModule*
3060 find_aot_module (guint8 *code)
3061 {
3062         FindAotModuleUserData user_data;
3063
3064         if (!aot_modules)
3065                 return NULL;
3066
3067         /* Reading these need no locking */
3068         if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
3069                 return NULL;
3070
3071         user_data.addr = code;
3072         user_data.module = NULL;
3073                 
3074         mono_aot_lock ();
3075         g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
3076         mono_aot_unlock ();
3077         
3078         return user_data.module;
3079 }
3080
3081 void
3082 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
3083 {
3084         /*
3085          * Since AOT code is only used in the root domain, 
3086          * mono_domain_get () != mono_get_root_domain () means the calling method
3087          * is AppDomain:InvokeInDomain, so this is the same check as in 
3088          * mono_method_same_domain () but without loading the metadata for the method.
3089          */
3090         if (mono_domain_get () == mono_get_root_domain ())
3091                 mono_arch_patch_plt_entry (code, got, regs, addr);
3092 }
3093
3094 /*
3095  * mono_aot_plt_resolve:
3096  *
3097  *   This function is called by the entries in the PLT to resolve the actual method that
3098  * needs to be called. It returns a trampoline to the method and patches the PLT entry.
3099  * Returns NULL if the something cannot be loaded.
3100  */
3101 gpointer
3102 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3103 {
3104 #ifdef MONO_ARCH_AOT_SUPPORTED
3105         guint8 *p, *target, *plt_entry;
3106         MonoJumpInfo ji;
3107         MonoAotModule *module = (MonoAotModule*)aot_module;
3108         gboolean res, no_ftnptr = FALSE;
3109         MonoMemPool *mp;
3110
3111         //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
3112
3113         p = &module->blob [plt_info_offset];
3114
3115         ji.type = decode_value (p, &p);
3116
3117         mp = mono_mempool_new_size (512);
3118         res = decode_patch (module, mp, &ji, p, &p);
3119
3120         if (!res) {
3121                 mono_mempool_destroy (mp);
3122                 return NULL;
3123         }
3124
3125         /* 
3126          * Avoid calling resolve_patch_target in the full-aot case if possible, since
3127          * it would create a trampoline, and we don't need that.
3128          * We could do this only if the method does not need the special handling
3129          * in mono_magic_trampoline ().
3130          */
3131         if (mono_aot_only && ji.type == MONO_PATCH_INFO_METHOD && !ji.data.method->is_generic && !mono_method_check_context_used (ji.data.method) && !(ji.data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) &&
3132                 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
3133                 target = mono_jit_compile_method (ji.data.method);
3134                 no_ftnptr = TRUE;
3135         } else {
3136                 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
3137         }
3138
3139         /*
3140          * The trampoline expects us to return a function descriptor on platforms which use
3141          * it, but resolve_patch_target returns a direct function pointer for some type of
3142          * patches, so have to translate between the two.
3143          * FIXME: Clean this up, but how ?
3144          */
3145         if (ji.type == MONO_PATCH_INFO_ABS || ji.type == MONO_PATCH_INFO_INTERNAL_METHOD || ji.type == MONO_PATCH_INFO_CLASS_INIT || ji.type == MONO_PATCH_INFO_ICALL_ADDR || ji.type == MONO_PATCH_INFO_JIT_ICALL_ADDR || ji.type == MONO_PATCH_INFO_RGCTX_FETCH) {
3146                 /* These should already have a function descriptor */
3147 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3148                 /* Our function descriptors have a 0 environment, gcc created ones don't */
3149                 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
3150                         g_assert (((gpointer*)target) [2] == 0);
3151 #endif
3152                 /* Empty */
3153         } else if (!no_ftnptr) {
3154 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3155                 g_assert (((gpointer*)target) [2] != 0);
3156 #endif
3157                 target = mono_create_ftnptr (mono_domain_get (), target);
3158         }
3159
3160         mono_mempool_destroy (mp);
3161
3162         /* Patch the PLT entry with target which might be the actual method not a trampoline */
3163         plt_entry = mono_aot_get_plt_entry (code);
3164         g_assert (plt_entry);
3165         mono_aot_patch_plt_entry (plt_entry, module->got, NULL, target);
3166
3167         return target;
3168 #else
3169         g_assert_not_reached ();
3170         return NULL;
3171 #endif
3172 }
3173
3174 /**
3175  * init_plt:
3176  *
3177  *   Initialize the PLT table of the AOT module. Called lazily when the first AOT
3178  * method in the module is loaded to avoid committing memory by writing to it.
3179  * LOCKING: Assumes the AOT lock is held.
3180  */
3181 static void
3182 init_plt (MonoAotModule *amodule)
3183 {
3184         int i;
3185         gpointer tramp;
3186
3187         if (amodule->plt_inited)
3188                 return;
3189
3190         tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
3191
3192         /*
3193          * Initialize the PLT entries in the GOT to point to the default targets.
3194          */
3195
3196         tramp = mono_create_ftnptr (mono_domain_get (), tramp);
3197          for (i = 1; i < amodule->info.plt_size; ++i)
3198                  /* All the default entries point to the AOT trampoline */
3199                  ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = tramp;
3200
3201         amodule->plt_inited = TRUE;
3202 }
3203
3204 /*
3205  * mono_aot_get_plt_entry:
3206  *
3207  *   Return the address of the PLT entry called by the code at CODE if exists.
3208  */
3209 guint8*
3210 mono_aot_get_plt_entry (guint8 *code)
3211 {
3212         MonoAotModule *amodule = find_aot_module (code);
3213         guint8 *target = NULL;
3214
3215         if (!amodule)
3216                 return NULL;
3217
3218 #ifdef TARGET_ARM
3219         if (amodule->thumb_end && code < amodule->thumb_end) {
3220                 return mono_arm_get_thumb_plt_entry (code);
3221         }
3222 #endif
3223
3224 #ifdef MONO_ARCH_AOT_SUPPORTED
3225         target = mono_arch_get_call_target (code);
3226 #else
3227         g_assert_not_reached ();
3228 #endif
3229
3230         if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
3231                 return target;
3232         else
3233                 return NULL;
3234 }
3235
3236 /*
3237  * mono_aot_get_plt_info_offset:
3238  *
3239  *   Return the PLT info offset belonging to the plt entry called by CODE.
3240  */
3241 guint32
3242 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3243 {
3244         guint8 *plt_entry = mono_aot_get_plt_entry (code);
3245
3246         g_assert (plt_entry);
3247
3248         /* The offset is embedded inside the code after the plt entry */
3249 #ifdef MONO_ARCH_AOT_SUPPORTED
3250         return mono_arch_get_plt_info_offset (plt_entry, regs, code);
3251 #else
3252         g_assert_not_reached ();
3253         return 0;
3254 #endif
3255 }
3256
3257 static gpointer
3258 mono_create_ftnptr_malloc (guint8 *code)
3259 {
3260 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3261         MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
3262
3263         ftnptr->code = code;
3264         ftnptr->toc = NULL;
3265         ftnptr->env = NULL;
3266
3267         return ftnptr;
3268 #else
3269         return code;
3270 #endif
3271 }
3272
3273 /*
3274  * mono_aot_register_jit_icall:
3275  *
3276  *   Register a JIT icall which is called by trampolines in full-aot mode. This should
3277  * be called from mono_arch_init () during startup.
3278  */
3279 void
3280 mono_aot_register_jit_icall (const char *name, gpointer addr)
3281 {
3282         /* No need for locking */
3283         if (!aot_jit_icall_hash)
3284                 aot_jit_icall_hash = g_hash_table_new (g_str_hash, g_str_equal);
3285         g_hash_table_insert (aot_jit_icall_hash, (char*)name, addr);
3286 }
3287
3288 /*
3289  * load_function:
3290  *
3291  *   Load the function named NAME from the aot image. 
3292  */
3293 static gpointer
3294 load_function (MonoAotModule *amodule, const char *name)
3295 {
3296         char *symbol;
3297         guint8 *p;
3298         int n_patches, pindex;
3299         MonoMemPool *mp;
3300         gpointer code;
3301
3302         /* Load the code */
3303
3304         symbol = g_strdup_printf ("%s", name);
3305         find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
3306         g_free (symbol);
3307         if (!code)
3308                 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
3309
3310         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
3311
3312         /* Load info */
3313
3314         symbol = g_strdup_printf ("%s_p", name);
3315         find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
3316         g_free (symbol);
3317         if (!p)
3318                 /* Nothing to patch */
3319                 return code;
3320
3321         p = amodule->blob + *(guint32*)p;
3322
3323         /* Similar to mono_aot_load_method () */
3324
3325         n_patches = decode_value (p, &p);
3326
3327         if (n_patches) {
3328                 MonoJumpInfo *patches;
3329                 guint32 *got_slots;
3330
3331                 mp = mono_mempool_new ();
3332
3333                 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
3334                 g_assert (patches);
3335
3336                 for (pindex = 0; pindex < n_patches; ++pindex) {
3337                         MonoJumpInfo *ji = &patches [pindex];
3338                         gpointer target;
3339
3340                         if (amodule->got [got_slots [pindex]])
3341                                 continue;
3342
3343                         /*
3344                          * When this code is executed, the runtime may not be initalized yet, so
3345                          * resolve the patch info by hand.
3346                          */
3347                         if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3348                                 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
3349                                         target = mono_get_lmf_addr;
3350                                 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
3351                                         target = mono_thread_force_interruption_checkpoint;
3352                                 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
3353                                         target = mono_exception_from_token;
3354                                 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
3355                                         target = mono_get_throw_exception ();
3356                                 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
3357                                         int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
3358                                         target = (gpointer)mono_get_trampoline_func (tramp_type2);
3359                                 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
3360                                         /* atoll is needed because the the offset is unsigned */
3361                                         guint32 slot;
3362                                         int res;
3363
3364                                         res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
3365                                         g_assert (res == 1);
3366                                         target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
3367                                         target = mono_create_ftnptr_malloc (target);
3368                                 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
3369                                         target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
3370                                         target = mono_create_ftnptr_malloc (target);
3371                                 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
3372                                         target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
3373                                         target = mono_create_ftnptr_malloc (target);
3374                                 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
3375                                         target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
3376                                         target = mono_create_ftnptr_malloc (target);
3377                                 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
3378                                         target = mono_thread_get_and_clear_pending_exception;
3379                                 } else if (strstr (ji->data.name, "generic_trampoline_")) {
3380                                         target = mono_aot_get_trampoline (ji->data.name);
3381                                 } else if (aot_jit_icall_hash && g_hash_table_lookup (aot_jit_icall_hash, ji->data.name)) {
3382                                         /* Registered by mono_arch_init () */
3383                                         target = g_hash_table_lookup (aot_jit_icall_hash, ji->data.name);
3384                                 } else {
3385                                         fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
3386                                         g_assert_not_reached ();
3387                                         target = NULL;
3388                                 }
3389                         } else {
3390                                 /* Hopefully the code doesn't have patches which need method or 
3391                                  * domain to be set.
3392                                  */
3393                                 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
3394                                 g_assert (target);
3395                         }
3396
3397                         amodule->got [got_slots [pindex]] = target;
3398                 }
3399
3400                 g_free (got_slots);
3401
3402                 mono_mempool_destroy (mp);
3403         }
3404
3405         return code;
3406 }
3407
3408 /*
3409  * Return the trampoline identified by NAME from the mscorlib AOT file.
3410  * On ppc64, this returns a function descriptor.
3411  */
3412 gpointer
3413 mono_aot_get_trampoline (const char *name)
3414 {
3415         MonoImage *image;
3416         MonoAotModule *amodule;
3417
3418         image = mono_defaults.corlib;
3419         g_assert (image);
3420
3421         amodule = image->aot_module;
3422         g_assert (amodule);
3423
3424         return mono_create_ftnptr_malloc (load_function (amodule, name));
3425 }
3426
3427 /* Return a given kind of trampoline */
3428 static gpointer
3429 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
3430 {
3431         MonoAotModule *amodule;
3432         int index, tramp_size;
3433         MonoImage *image;
3434
3435         /* Currently, we keep all trampolines in the mscorlib AOT image */
3436         image = mono_defaults.corlib;
3437         g_assert (image);
3438
3439         mono_aot_lock ();
3440
3441         amodule = image->aot_module;
3442         g_assert (amodule);
3443
3444         *out_amodule = amodule;
3445
3446         if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
3447                 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
3448
3449         index = amodule->trampoline_index [tramp_type] ++;
3450
3451         mono_aot_unlock ();
3452
3453         *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
3454
3455         tramp_size = amodule->info.trampoline_size [tramp_type];
3456
3457         if (out_tramp_size)
3458                 *out_tramp_size = tramp_size;
3459
3460         return amodule->trampolines [tramp_type] + (index * tramp_size);
3461 }
3462
3463 /*
3464  * Return a specific trampoline from the AOT file.
3465  */
3466 gpointer
3467 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3468 {
3469         MonoAotModule *amodule;
3470         guint32 got_offset, tramp_size;
3471         guint8 *code, *tramp;
3472         static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
3473         static gboolean inited;
3474         static guint32 num_trampolines;
3475
3476         if (!inited) {
3477                 mono_aot_lock ();
3478
3479                 if (!inited) {
3480                         mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
3481                         inited = TRUE;
3482                 }
3483
3484                 mono_aot_unlock ();
3485         }
3486
3487         num_trampolines ++;
3488
3489         if (!generic_trampolines [tramp_type]) {
3490                 char *symbol;
3491
3492                 symbol = mono_get_generic_trampoline_name (tramp_type);
3493                 generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
3494                 g_free (symbol);
3495         }
3496
3497         tramp = generic_trampolines [tramp_type];
3498         g_assert (tramp);
3499
3500         code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
3501
3502         amodule->got [got_offset] = tramp;
3503         amodule->got [got_offset + 1] = arg1;
3504
3505         if (code_len)
3506                 *code_len = tramp_size;
3507
3508         return code;
3509 }
3510
3511 gpointer
3512 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3513 {
3514         MonoAotModule *amodule;
3515         guint8 *code;
3516         guint32 got_offset;
3517
3518         code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
3519
3520         amodule->got [got_offset] = ctx;
3521         amodule->got [got_offset + 1] = addr; 
3522
3523         /* The caller expects an ftnptr */
3524         return mono_create_ftnptr (mono_domain_get (), code);
3525 }
3526
3527 gpointer
3528 mono_aot_get_unbox_trampoline (MonoMethod *method)
3529 {
3530         guint32 method_index = mono_metadata_token_index (method->token) - 1;
3531         MonoAotModule *amodule;
3532         char *symbol;
3533         gpointer code;
3534
3535         if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
3536                 guint32 index = find_extra_method (method, &amodule);
3537                 g_assert (index != 0xffffff);
3538                 
3539                 symbol = g_strdup_printf ("ut_%d", index);
3540         } else {
3541                 amodule = method->klass->image->aot_module;
3542                 g_assert (amodule);
3543
3544                 symbol = g_strdup_printf ("ut_%d", method_index);
3545         }
3546         code = load_function (amodule, symbol);
3547         g_free (symbol);
3548
3549         /* The caller expects an ftnptr */
3550         return mono_create_ftnptr (mono_domain_get (), code);
3551 }
3552
3553 gpointer
3554 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3555 {
3556         char *symbol;
3557         gpointer code;
3558
3559         symbol = mono_get_rgctx_fetch_trampoline_name (slot);
3560         code = load_function (mono_defaults.corlib->aot_module, symbol);
3561         g_free (symbol);
3562         /* The caller expects an ftnptr */
3563         return mono_create_ftnptr (mono_domain_get (), code);
3564 }
3565
3566 gpointer
3567 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3568 {
3569         guint32 got_offset;
3570         gpointer code;
3571         gpointer *buf;
3572         int i, index, real_count;
3573         MonoAotModule *amodule;
3574
3575         code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
3576
3577         real_count = 0;
3578         for (i = 0; i < count; ++i) {
3579                 MonoIMTCheckItem *item = imt_entries [i];
3580
3581                 if (item->is_equals)
3582                         real_count ++;
3583         }
3584
3585         /* Save the entries into an array */
3586         buf = mono_domain_alloc (domain, (real_count + 1) * 2 * sizeof (gpointer));
3587         index = 0;
3588         for (i = 0; i < count; ++i) {
3589                 MonoIMTCheckItem *item = imt_entries [i];               
3590
3591                 if (!item->is_equals)
3592                         continue;
3593
3594                 g_assert (item->key);
3595
3596                 buf [(index * 2)] = item->key;
3597                 if (item->has_target_code) {
3598                         gpointer *p = mono_domain_alloc (domain, sizeof (gpointer));
3599                         *p = item->value.target_code;
3600                         buf [(index * 2) + 1] = p;
3601                 } else {
3602                         buf [(index * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
3603                 }
3604                 index ++;
3605         }
3606         buf [(index * 2)] = NULL;
3607         buf [(index * 2) + 1] = fail_tramp;
3608         
3609         amodule->got [got_offset] = buf;
3610
3611         return code;
3612 }
3613  
3614 /*
3615  * mono_aot_set_make_unreadable:
3616  *
3617  *   Set whenever to make all mmaped memory unreadable. In conjuction with a
3618  * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
3619  */
3620 void
3621 mono_aot_set_make_unreadable (gboolean unreadable)
3622 {
3623         static int inited;
3624
3625         make_unreadable = unreadable;
3626
3627         if (make_unreadable && !inited) {
3628                 mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
3629         }               
3630 }
3631
3632 typedef struct {
3633         MonoAotModule *module;
3634         guint8 *ptr;
3635 } FindMapUserData;
3636
3637 static void
3638 find_map (gpointer key, gpointer value, gpointer user_data)
3639 {
3640         MonoAotModule *module = (MonoAotModule*)value;
3641         FindMapUserData *data = (FindMapUserData*)user_data;
3642
3643         if (!data->module)
3644                 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
3645                         data->module = module;
3646 }
3647
3648 static MonoAotModule*
3649 find_module_for_addr (void *ptr)
3650 {
3651         FindMapUserData data;
3652
3653         if (!make_unreadable)
3654                 return NULL;
3655
3656         data.module = NULL;
3657         data.ptr = (guint8*)ptr;
3658
3659         mono_aot_lock ();
3660         g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
3661         mono_aot_unlock ();
3662
3663         return data.module;
3664 }
3665
3666 /*
3667  * mono_aot_is_pagefault:
3668  *
3669  *   Should be called from a SIGSEGV signal handler to find out whenever @ptr is
3670  * within memory allocated by this module.
3671  */
3672 gboolean
3673 mono_aot_is_pagefault (void *ptr)
3674 {
3675         if (!make_unreadable)
3676                 return FALSE;
3677
3678         /* 
3679          * Not signal safe, but SIGSEGV's are synchronous, and
3680          * this is only turned on by a MONO_DEBUG option.
3681          */
3682         return find_module_for_addr (ptr) != NULL;
3683 }
3684
3685 /*
3686  * mono_aot_handle_pagefault:
3687  *
3688  *   Handle a pagefault caused by an unreadable page by making it readable again.
3689  */
3690 void
3691 mono_aot_handle_pagefault (void *ptr)
3692 {
3693 #ifndef PLATFORM_WIN32
3694         guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
3695         int res;
3696
3697         mono_aot_lock ();
3698         res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
3699         g_assert (res == 0);
3700
3701         n_pagefaults ++;
3702         mono_aot_unlock ();
3703 #endif
3704 }
3705
3706 #else
3707 /* AOT disabled */
3708
3709 void
3710 mono_aot_init (void)
3711 {
3712 }
3713
3714 gpointer
3715 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3716 {
3717         return NULL;
3718 }
3719
3720 gboolean
3721 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3722 {
3723         return FALSE;
3724 }
3725
3726 gboolean
3727 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3728 {
3729         return FALSE;
3730 }
3731
3732 gboolean
3733 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
3734 {
3735         return FALSE;
3736 }
3737
3738 MonoJitInfo *
3739 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
3740 {
3741         return NULL;
3742 }
3743
3744 gpointer
3745 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3746 {
3747         return NULL;
3748 }
3749
3750 guint8*
3751 mono_aot_get_plt_entry (guint8 *code)
3752 {
3753         return NULL;
3754 }
3755
3756 gpointer
3757 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3758 {
3759         return NULL;
3760 }
3761
3762 void
3763 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
3764 {
3765 }
3766
3767 gpointer
3768 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
3769 {
3770         return NULL;
3771 }
3772
3773 guint32
3774 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3775 {
3776         g_assert_not_reached ();
3777
3778         return 0;
3779 }
3780
3781 gpointer
3782 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3783 {
3784         g_assert_not_reached ();
3785         return NULL;
3786 }
3787
3788 gpointer
3789 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3790 {
3791         g_assert_not_reached ();
3792         return NULL;
3793 }
3794
3795 gpointer
3796 mono_aot_get_trampoline (const char *name)
3797 {
3798         g_assert_not_reached ();
3799         return NULL;
3800 }
3801
3802 gpointer
3803 mono_aot_get_unbox_trampoline (MonoMethod *method)
3804 {
3805         g_assert_not_reached ();
3806         return NULL;
3807 }
3808
3809 gpointer
3810 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3811 {
3812         g_assert_not_reached ();
3813         return NULL;
3814 }
3815
3816 gpointer
3817 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3818 {
3819         g_assert_not_reached ();
3820         return NULL;
3821 }       
3822
3823 guint8*
3824 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3825 {
3826         g_assert_not_reached ();
3827         return NULL;
3828 }
3829
3830 void
3831 mono_aot_register_jit_icall (const char *name, gpointer addr)
3832 {
3833 }
3834
3835 #endif