2008-08-20 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / aot-runtime.c
1 /*
2  * aot.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  */
10
11 #include "config.h"
12 #include <sys/types.h>
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <fcntl.h>
17 #include <string.h>
18 #ifndef PLATFORM_WIN32
19 #include <sys/mman.h>
20 #else
21 #include <winsock2.h>
22 #include <windows.h>
23 #endif
24
25 #ifdef HAVE_EXECINFO_H
26 #include <execinfo.h>
27 #endif
28
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <limits.h>    /* for PAGESIZE */
32 #ifndef PAGESIZE
33 #define PAGESIZE 4096
34 #endif
35
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h>  /* for WIFEXITED, WEXITSTATUS */
38 #endif
39
40 #include <mono/metadata/tabledefs.h>
41 #include <mono/metadata/class.h>
42 #include <mono/metadata/object.h>
43 #include <mono/metadata/tokentype.h>
44 #include <mono/metadata/appdomain.h>
45 #include <mono/metadata/debug-helpers.h>
46 #include <mono/metadata/assembly.h>
47 #include <mono/metadata/metadata-internals.h>
48 #include <mono/metadata/marshal.h>
49 #include <mono/metadata/gc-internal.h>
50 #include <mono/utils/mono-logger.h>
51 #include "mono/utils/mono-compiler.h"
52
53 #include "mini.h"
54 #include "version.h"
55
56 #ifndef DISABLE_AOT
57
58 #ifdef PLATFORM_WIN32
59 #define SHARED_EXT ".dll"
60 #elif (defined(__ppc__) || defined(__ppc64__)) || defined(__MACH__)
61 #define SHARED_EXT ".dylib"
62 #else
63 #define SHARED_EXT ".so"
64 #endif
65
66 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
67 #define ROUND_DOWN(VALUE,SIZE)  ((VALUE) & ~((SIZE) - 1))
68
69 typedef struct MonoAotModule {
70         char *aot_name;
71         /* Optimization flags used to compile the module */
72         guint32 opts;
73         /* Pointer to the Global Offset Table */
74         gpointer *got;
75         guint32 got_size;
76         GHashTable *name_cache;
77         GHashTable *wrappers;
78         MonoAssemblyName *image_names;
79         char **image_guids;
80         MonoAssembly *assembly;
81         MonoImage **image_table;
82         guint32 image_table_len;
83         gboolean out_of_date;
84         gboolean plt_inited;
85         guint8 *mem_begin;
86         guint8 *mem_end;
87         guint8 *code;
88         guint8 *code_end;
89         guint8 *plt;
90         guint8 *plt_end;
91         guint8 *plt_info;
92         guint8 *plt_jump_table;
93         guint32 plt_jump_table_size;
94         guint32 *code_offsets;
95         guint8 *method_info;
96         guint32 *method_info_offsets;
97         guint8 *got_info;
98         guint32 *got_info_offsets;
99         guint8 *ex_info;
100         guint32 *ex_info_offsets;
101         guint32 *method_order;
102         guint32 *method_order_end;
103         guint8 *class_info;
104         guint32 *class_info_offsets;
105         guint32 *methods_loaded;
106         guint16 *class_name_table;
107         guint8 *wrapper_info;
108         guint8 *trampolines;
109         guint32 num_trampolines, first_trampoline_got_offset, trampoline_index;
110         gpointer *globals;
111         MonoDl *sofile;
112 } MonoAotModule;
113
114 static GHashTable *aot_modules;
115 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
116 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
117 static CRITICAL_SECTION aot_mutex;
118
119 /* 
120  * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
121  * AOT modules registered by mono_aot_register_module ().
122  */
123 static GHashTable *static_aot_modules;
124
125 /*
126  * Disabling this will make a copy of the loaded code and use the copy instead 
127  * of the original. This will place the caller and the callee close to each 
128  * other in memory, possibly improving cache behavior. Since the original
129  * code is in copy-on-write memory, this will not increase the memory usage
130  * of the runtime.
131  */
132 static gboolean use_loaded_code = TRUE;
133
134 /*
135  * Whenever to AOT compile loaded assemblies on demand and store them in
136  * a cache under $HOME/.mono/aot-cache.
137  */
138 static gboolean use_aot_cache = FALSE;
139
140 /*
141  * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
142  * use_aot_cache is TRUE.
143  */
144 static gboolean spawn_compiler = TRUE;
145
146 /* For debugging */
147 static gint32 mono_last_aot_method = -1;
148
149 static gboolean make_unreadable = FALSE;
150 static guint32 n_pagefaults = 0;
151 static guint32 name_table_accesses = 0;
152
153 /* Used to speed-up find_aot_module () */
154 static gsize aot_code_low_addr = (gssize)-1;
155 static gsize aot_code_high_addr = 0;
156
157 /* Used to communicate with mono_aot_register_globals () */
158 static guint32 globals_tls_id = -1;
159
160 static gpointer
161 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info, int method_index);
162
163 static void
164 init_plt (MonoAotModule *info);
165
166 static MonoJumpInfo*
167 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches, 
168                                  guint32 got_index, guint32 **got_slots, 
169                                  guint8 *buf, guint8 **endbuf);
170
171 static inline gboolean 
172 is_got_patch (MonoJumpInfoType patch_type)
173 {
174         return TRUE;
175 }
176
177 /*****************************************************/
178 /*                 AOT RUNTIME                       */
179 /*****************************************************/
180
181 static MonoImage *
182 load_image (MonoAotModule *module, int index)
183 {
184         MonoAssembly *assembly;
185         MonoImageOpenStatus status;
186
187         g_assert (index < module->image_table_len);
188
189         if (module->image_table [index])
190                 return module->image_table [index];
191         if (module->out_of_date)
192                 return NULL;
193
194         assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
195         if (!assembly) {
196                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", module->aot_name, module->image_names [index].name);
197                 module->out_of_date = TRUE;
198                 return NULL;
199         }
200
201         if (strcmp (assembly->image->guid, module->image_guids [index])) {
202                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", module->aot_name, module->image_names [index].name);
203                 module->out_of_date = TRUE;
204                 return NULL;
205         }
206
207         module->image_table [index] = assembly->image;
208         return assembly->image;
209 }
210
211
212 static inline gint32
213 decode_value (guint8 *ptr, guint8 **rptr)
214 {
215         guint8 b = *ptr;
216         gint32 len;
217         
218         if ((b & 0x80) == 0){
219                 len = b;
220                 ++ptr;
221         } else if ((b & 0x40) == 0){
222                 len = ((b & 0x3f) << 8 | ptr [1]);
223                 ptr += 2;
224         } else if (b != 0xff) {
225                 len = ((b & 0x1f) << 24) |
226                         (ptr [1] << 16) |
227                         (ptr [2] << 8) |
228                         ptr [3];
229                 ptr += 4;
230         }
231         else {
232                 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
233                 ptr += 5;
234         }
235         if (rptr)
236                 *rptr = ptr;
237
238         //printf ("DECODE: %d.\n", len);
239         return len;
240 }
241
242 static MonoMethod*
243 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
244
245 static MonoClass*
246 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
247
248 static MonoGenericInst*
249 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
250 {
251         int type_argc, i;
252         MonoType **type_argv;
253         MonoGenericInst *inst;
254         guint8 *p = buf;
255
256         type_argc = decode_value (p, &p);
257         type_argv = g_new0 (MonoType*, type_argc);
258
259         for (i = 0; i < type_argc; ++i) {
260                 MonoClass *pclass = decode_klass_ref (module, p, &p);
261                 if (!pclass) {
262                         g_free (type_argv);
263                         return NULL;
264                 }
265                 type_argv [i] = &pclass->byval_arg;
266         }
267
268         inst = mono_metadata_get_generic_inst (type_argc, type_argv);
269         g_free (type_argv);
270
271         *endbuf = p;
272
273         return inst;
274 }
275
276 static MonoClass*
277 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
278 {
279         MonoImage *image;
280         MonoClass *klass, *eklass;
281         guint32 token, rank;
282         guint8 *p = buf;
283
284         token = decode_value (p, &p);
285         if (token == 0) {
286                 *endbuf = p;
287                 return NULL;
288         }
289         if (mono_metadata_token_table (token) == 0) {
290                 image = load_image (module, decode_value (p, &p));
291                 if (!image)
292                         return NULL;
293                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
294         } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
295                 if (token == MONO_TOKEN_TYPE_SPEC) {
296                         MonoTypeEnum type = decode_value (p, &p);
297
298                         if (type == MONO_TYPE_GENERICINST) {
299                                 MonoClass *gclass;
300                                 MonoGenericContext ctx;
301                                 MonoType *type;
302
303                                 gclass = decode_klass_ref (module, p, &p);
304                                 g_assert (gclass->generic_container);
305
306                                 memset (&ctx, 0, sizeof (ctx));
307                                 ctx.class_inst = decode_generic_inst (module, p, &p);
308                                 if (!ctx.class_inst)
309                                         return NULL;
310                                 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
311                                 klass = mono_class_from_mono_type (type);
312                                 mono_metadata_free_type (type);
313                         } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
314                                 MonoType *t;
315                                 gboolean is_method;
316                                 MonoGenericContainer *container;
317
318                                 // FIXME: Maybe use types directly to avoid
319                                 // the overhead of creating MonoClass-es
320
321                                 // FIXME: Memory management
322                                 t = g_new0 (MonoType, 1);
323                                 t->type = type;
324                                 t->data.generic_param = g_new0 (MonoGenericParam, 1);
325                                 t->data.generic_param->num = decode_value (p, &p);
326                                 t->data.generic_param->name = "T";
327
328                                 is_method = decode_value (p, &p);
329                                 if (is_method) {
330                                         MonoMethod *method_def = decode_method_ref_2 (module, p, &p);
331
332                                         if (!method_def) {
333                                                 g_free (t->data.generic_param);
334                                                 g_free (t);
335                                                 return NULL;
336                                         }
337
338                                         container = mono_method_get_generic_container (method_def);
339                                 } else {
340                                         MonoClass *class_def = decode_klass_ref (module, p, &p);
341                                         
342                                         if (!class_def) {
343                                                 g_free (t->data.generic_param);
344                                                 g_free (t);
345                                                 return NULL;
346                                         }
347
348                                         container = class_def->generic_container;
349                                 }
350
351                                 g_assert (container);
352                                 t->data.generic_param->owner = container;
353
354                                 klass = mono_class_from_mono_type (t);
355                         } else {
356                                 g_assert_not_reached ();
357                         }
358                 } else {
359                         image = load_image (module, decode_value (p, &p));
360                         if (!image)
361                                 return NULL;
362                         klass = mono_class_get (image, token);
363                 }
364         } else if (token == MONO_TOKEN_TYPE_DEF) {
365                 /* Array */
366                 image = load_image (module, decode_value (p, &p));
367                 if (!image)
368                         return NULL;
369                 rank = decode_value (p, &p);
370                 eklass = decode_klass_ref (module, p, &p);
371                 klass = mono_array_class_get (eklass, rank);
372         } else {
373                 g_assert_not_reached ();
374         }
375         g_assert (klass);
376         mono_class_init (klass);
377
378         *endbuf = p;
379         return klass;
380 }
381
382 static MonoClassField*
383 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
384 {
385         MonoClass *klass = decode_klass_ref (module, buf, &buf);
386         guint32 token;
387         guint8 *p = buf;
388
389         if (!klass)
390                 return NULL;
391
392         token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
393
394         *endbuf = p;
395
396         return mono_class_get_field (klass, token);
397 }
398
399 /*
400  * decode_method_ref:
401  *
402  *   Decode a method reference, and return its image and token. This avoids loading
403  * metadata for the method if the caller does not need it. If the method has no token,
404  * then it is loaded from metadata and METHOD is set to the method instance.
405  */
406 static inline MonoImage*
407 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, guint8 *buf, guint8 **endbuf)
408 {
409         guint32 image_index, value;
410         MonoImage *image;
411         guint8 *p = buf;
412
413         if (method)
414                 *method = NULL;
415
416         value = decode_value (p, &p);
417         image_index = value >> 24;
418
419         if (image_index == 253) {
420                 /* Wrapper */
421                 guint32 wrapper_type;
422
423                 wrapper_type = decode_value (p, &p);
424
425                 /* Doesn't matter */
426                 image = mono_defaults.corlib;
427
428                 switch (wrapper_type) {
429                 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
430                         MonoMethod *m = decode_method_ref_2 (module, p, &p);
431
432                         if (!m)
433                                 return NULL;
434                         mono_class_init (m->klass);
435                         *method = mono_marshal_get_remoting_invoke_with_check (m);
436                         break;
437                 }
438                 case MONO_WRAPPER_PROXY_ISINST: {
439                         MonoClass *klass = decode_klass_ref (module, p, &p);
440                         if (!klass)
441                                 return NULL;
442                         *method = mono_marshal_get_proxy_cancast (klass);
443                         break;
444                 }
445                 case MONO_WRAPPER_LDFLD:
446                 case MONO_WRAPPER_LDFLDA:
447                 case MONO_WRAPPER_STFLD:
448                 case MONO_WRAPPER_ISINST: {
449                         MonoClass *klass = decode_klass_ref (module, p, &p);
450                         if (!klass)
451                                 return NULL;
452                         if (wrapper_type == MONO_WRAPPER_LDFLD)
453                                 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
454                         else if (wrapper_type == MONO_WRAPPER_LDFLDA)
455                                 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
456                         else if (wrapper_type == MONO_WRAPPER_STFLD)
457                                 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
458                         else if (wrapper_type == MONO_WRAPPER_ISINST)
459                                 *method = mono_marshal_get_isinst (klass);
460                         else
461                                 g_assert_not_reached ();
462                         break;
463                 }
464                 case MONO_WRAPPER_LDFLD_REMOTE:
465                         *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
466                         break;
467                 case MONO_WRAPPER_STFLD_REMOTE:
468                         *method = mono_marshal_get_stfld_remote_wrapper (NULL);
469                         break;
470                 case MONO_WRAPPER_ALLOC: {
471                         int atype = decode_value (p, &p);
472
473                         *method = mono_gc_get_managed_allocator_by_type (atype);
474                         break;
475                 }
476                 case MONO_WRAPPER_STELEMREF:
477                         *method = mono_marshal_get_stelemref ();
478                         break;
479                 case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
480                         MonoMethod *m = decode_method_ref_2 (module, p, &p);
481
482                         if (!m)
483                                 return NULL;
484                         *method = mono_marshal_get_static_rgctx_invoke (m);
485                         break;
486                 }
487                 default:
488                         g_assert_not_reached ();
489                 }
490         } else if (image_index == 255) {
491                 /* Methodspec */
492                 image_index = decode_value (p, &p);
493                 *token = decode_value (p, &p);
494
495                 image = load_image (module, image_index);
496                 if (!image)
497                         return NULL;
498         } else if (image_index == 254) {
499                 /* Method on generic instance */
500                 MonoClass *klass;
501                 MonoGenericContext ctx;
502                 gboolean has_class_inst, has_method_inst;
503
504                 /* 
505                  * These methods do not have a token which resolves them, so we 
506                  * resolve them immediately.
507                  */
508                 klass = decode_klass_ref (module, p, &p);
509                 if (!klass)
510                         return NULL;
511
512                 image_index = decode_value (p, &p);
513                 *token = decode_value (p, &p);
514
515                 image = load_image (module, image_index);
516                 if (!image)
517                         return NULL;
518
519                 *method = mono_get_method_full (image, *token, NULL, NULL);
520                 if (!(*method))
521                         return NULL;
522
523                 memset (&ctx, 0, sizeof (ctx));
524
525                 if (FALSE && klass->generic_class) {
526                         ctx.class_inst = klass->generic_class->context.class_inst;
527                         ctx.method_inst = NULL;
528  
529                         *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
530                 }                       
531
532                 memset (&ctx, 0, sizeof (ctx));
533
534                 // FIXME: Memory management
535                 has_class_inst = decode_value (p, &p);
536                 if (has_class_inst) {
537                         ctx.class_inst = decode_generic_inst (module, p, &p);
538                         if (!ctx.class_inst)
539                                 return NULL;
540                 }
541                 has_method_inst = decode_value (p, &p);
542                 if (has_method_inst) {
543                         ctx.method_inst = decode_generic_inst (module, p, &p);
544                         if (!ctx.method_inst)
545                                 return NULL;
546                 }
547
548                 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
549         } else {
550                 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
551
552                 image = load_image (module, image_index);
553                 if (!image)
554                         return NULL;
555         }
556
557         *endbuf = p;
558
559         return image;
560 }
561
562 /*
563  * decode_method_ref_2:
564  *
565  *   Similar to decode_method_ref, but resolve and return the method itself.
566  */
567 static MonoMethod*
568 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
569 {
570         MonoMethod *method;
571         guint32 token;
572         MonoImage *image = decode_method_ref (module, &token, &method, buf, endbuf);
573
574         if (method)
575                 return method;
576         if (!image)
577                 return NULL;
578         return mono_get_method (image, token, NULL);
579 }
580
581 G_GNUC_UNUSED
582 static void
583 make_writable (guint8* addr, guint32 len)
584 {
585 #ifndef PLATFORM_WIN32
586         guint8 *page_start;
587         int pages, err;
588
589         if (mono_aot_only)
590                 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
591
592         page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
593         pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
594         err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
595         g_assert (err == 0);
596 #else
597         {
598                 DWORD oldp;
599                 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
600         }
601 #endif
602 }
603
604 static void
605 create_cache_structure (void)
606 {
607         const char *home;
608         char *tmp;
609         int err;
610
611         home = g_get_home_dir ();
612         if (!home)
613                 return;
614
615         tmp = g_build_filename (home, ".mono", NULL);
616         if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
617                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
618 #ifdef PLATFORM_WIN32
619                 err = mkdir (tmp);
620 #else
621                 err = mkdir (tmp, 0777);
622 #endif
623                 if (err) {
624                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
625                         g_free (tmp);
626                         return;
627                 }
628         }
629         g_free (tmp);
630         tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
631         if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
632                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
633 #ifdef PLATFORM_WIN32
634                 err = mkdir (tmp);
635 #else
636                 err = mkdir (tmp, 0777);
637 #endif
638                 if (err) {
639                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
640                         g_free (tmp);
641                         return;
642                 }
643         }
644         g_free (tmp);
645 }
646
647 /*
648  * load_aot_module_from_cache:
649  *
650  *  Experimental code to AOT compile loaded assemblies on demand. 
651  *
652  * FIXME: 
653  * - Add environment variable MONO_AOT_CACHE_OPTIONS
654  * - Add options for controlling the cache size
655  * - Handle full cache by deleting old assemblies lru style
656  * - Add options for excluding assemblies during development
657  * - Maybe add a threshold after an assembly is AOT compiled
658  * - invoking a new mono process is a security risk
659  * - recompile the AOT module if one of its dependencies changes
660  */
661 static MonoDl*
662 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
663 {
664         char *fname, *cmd, *tmp2, *aot_options;
665         const char *home;
666         MonoDl *module;
667         gboolean res;
668         gchar *out, *err;
669         gint exit_status;
670
671         *aot_name = NULL;
672
673         if (assembly->image->dynamic)
674                 return NULL;
675
676         create_cache_structure ();
677
678         home = g_get_home_dir ();
679
680         tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
681         fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
682         *aot_name = fname;
683         g_free (tmp2);
684
685         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
686         module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
687
688         if (!module) {
689                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
690
691                 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
692
693                 aot_options = g_strdup_printf ("outfile=%s", fname);
694
695                 if (spawn_compiler) {
696                         /* FIXME: security */
697                         /* FIXME: Has to pass the assembly loading path to the child process */
698                         cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
699
700                         res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
701
702 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
703                         if (res) {
704                                 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
705                                         mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
706                                 else
707                                         mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
708                                 g_free (out);
709                                 g_free (err);
710                         }
711 #endif
712                         g_free (cmd);
713                 } else {
714                         res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
715                         if (!res) {
716                                 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
717                         } else {
718                                 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
719                         }
720                 }
721
722                 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
723
724                 g_free (aot_options);
725         }
726
727         return module;
728 }
729
730 static void
731 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
732 {
733         if (globals) {
734                 int i = 0;
735
736                 *value = NULL;
737                 for (i = 0; globals [i]; i+= 2) {
738                         if (strcmp (globals [i], name) == 0) {
739                                 *value = globals [i + 1];
740                                 break;
741                         }
742                 }
743         } else {
744                 mono_dl_symbol (module, name, value);
745         }
746 }
747
748 static void
749 load_aot_module (MonoAssembly *assembly, gpointer user_data)
750 {
751         char *aot_name;
752         MonoAotModule *amodule;
753         MonoDl *sofile;
754         gboolean usable = TRUE;
755         char *saved_guid = NULL;
756         char *aot_version = NULL;
757         char *runtime_version;
758         char *opt_flags = NULL;
759         gpointer *globals;
760         gboolean full_aot = FALSE;
761         gpointer *plt_jump_table_addr = NULL;
762         guint32 *plt_jump_table_size = NULL;
763         guint32 *trampolines_info = NULL;
764         gpointer *got_addr = NULL;
765         gpointer *got = NULL;
766         guint32 *got_size_ptr = NULL;
767         int i;
768
769         if (mono_compile_aot)
770                 return;
771
772         if (assembly->image->aot_module)
773                 /* 
774                  * Already loaded. This can happen because the assembly loading code might invoke
775                  * the assembly load hooks multiple times for the same assembly.
776                  */
777                 return;
778
779         if (assembly->image->dynamic)
780                 return;
781
782         mono_aot_lock ();
783         if (static_aot_modules)
784                 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
785         else
786                 globals = NULL;
787         mono_aot_unlock ();
788
789         if (globals) {
790                 /* Statically linked AOT module */
791                 sofile = NULL;
792                 aot_name = g_strdup_printf ("%s", assembly->aname.name);
793                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
794         } else {
795                 TlsSetValue (globals_tls_id, NULL);
796
797                 if (use_aot_cache)
798                         sofile = load_aot_module_from_cache (assembly, &aot_name);
799                 else {
800                         char *err;
801                         aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
802
803                         sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
804
805                         if (!sofile) {
806                                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
807                                 g_free (err);
808                         }
809                 }
810
811                 /*
812                  * If the image was compiled in no-dlsym mode, it contains no global symbols,
813                  * instead it contains an ELF ctor function which is called by dlopen () which 
814                  * in turn calls mono_aot_register_globals () to register a table which contains
815                  * the name and address of the globals.
816                  */
817                 globals = TlsGetValue (globals_tls_id);
818                 TlsSetValue (globals_tls_id, NULL);
819         }
820
821         if (!sofile && !globals) {
822                 if (mono_aot_only) {
823                         fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
824                         exit (1);
825                 }
826                 g_free (aot_name);
827                 return;
828         }
829
830         find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
831         find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
832         find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
833         find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
834
835         if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
836                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s has wrong file format version (expected %s got %s)\n", aot_name, MONO_AOT_FILE_VERSION, aot_version);
837                 usable = FALSE;
838         }
839         else {
840                 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
841                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
842                         usable = FALSE;
843                 }
844         }
845
846         if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, FULL_VERSION)))) {
847                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled against runtime version %s while this runtime has version %s.\n", aot_name, runtime_version, FULL_VERSION);
848                 usable = FALSE;
849         }
850
851         {
852                 char *full_aot_str;
853
854                 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
855
856                 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
857                         full_aot = TRUE;
858         }
859
860         if (mono_aot_only && !full_aot) {
861                 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
862                 exit (1);
863         }
864         if (!mono_aot_only && full_aot) {
865                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
866                 usable = FALSE;
867         }
868
869         if (!usable) {
870                 if (mono_aot_only) {
871                         fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
872                         exit (1);
873                 }
874                 g_free (aot_name);
875                 if (sofile)
876                         mono_dl_close (sofile);
877                 assembly->image->aot_module = NULL;
878                 return;
879         }
880
881         find_symbol (sofile, globals, "got_addr", (gpointer *)&got_addr);
882         g_assert (got_addr);
883         got = (gpointer*)*got_addr;
884         g_assert (got);
885         find_symbol (sofile, globals, "got_size", (gpointer *)&got_size_ptr);
886         g_assert (got_size_ptr);
887
888         amodule = g_new0 (MonoAotModule, 1);
889         amodule->aot_name = aot_name;
890         amodule->assembly = assembly;
891         amodule->got = got;
892         amodule->got_size = *got_size_ptr;
893         amodule->got [0] = assembly->image;
894         amodule->globals = globals;
895         amodule->sofile = sofile;
896
897         sscanf (opt_flags, "%d", &amodule->opts);               
898
899         /* Read image table */
900         {
901                 guint32 table_len, i;
902                 char *table = NULL;
903
904                 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
905                 g_assert (table);
906
907                 table_len = *(guint32*)table;
908                 table += sizeof (guint32);
909                 amodule->image_table = g_new0 (MonoImage*, table_len);
910                 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
911                 amodule->image_guids = g_new0 (char*, table_len);
912                 amodule->image_table_len = table_len;
913                 for (i = 0; i < table_len; ++i) {
914                         MonoAssemblyName *aname = &(amodule->image_names [i]);
915
916                         aname->name = g_strdup (table);
917                         table += strlen (table) + 1;
918                         amodule->image_guids [i] = g_strdup (table);
919                         table += strlen (table) + 1;
920                         if (table [0] != 0)
921                                 aname->culture = g_strdup (table);
922                         table += strlen (table) + 1;
923                         memcpy (aname->public_key_token, table, strlen (table) + 1);
924                         table += strlen (table) + 1;                    
925
926                         table = ALIGN_PTR_TO (table, 8);
927                         aname->flags = *(guint32*)table;
928                         table += 4;
929                         aname->major = *(guint32*)table;
930                         table += 4;
931                         aname->minor = *(guint32*)table;
932                         table += 4;
933                         aname->build = *(guint32*)table;
934                         table += 4;
935                         aname->revision = *(guint32*)table;
936                         table += 4;
937                 }
938         }
939
940         /* Read method and method_info tables */
941         find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
942         find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
943         find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
944         find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
945         find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
946         find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
947         find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
948         find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
949         find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
950         find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
951         find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
952         find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
953         find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
954         find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
955         find_symbol (sofile, globals, "wrapper_info", (gpointer*)&amodule->wrapper_info);
956         find_symbol (sofile, globals, "trampolines", (gpointer*)&amodule->trampolines);
957         find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
958
959         amodule->mem_begin = amodule->code;
960
961         find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
962         find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
963         find_symbol (sofile, globals, "plt_info", (gpointer*)&amodule->plt_info);
964
965         find_symbol (sofile, globals, "plt_jump_table_addr", (gpointer *)&plt_jump_table_addr);
966         g_assert (plt_jump_table_addr);
967         amodule->plt_jump_table = (guint8*)*plt_jump_table_addr;
968         g_assert (amodule->plt_jump_table);
969
970         find_symbol (sofile, globals, "plt_jump_table_size", (gpointer *)&plt_jump_table_size);
971         g_assert (plt_jump_table_size);
972         amodule->plt_jump_table_size = *plt_jump_table_size;
973
974         find_symbol (sofile, globals, "trampolines_info", (gpointer *)&trampolines_info);
975         if (trampolines_info) {
976                 amodule->num_trampolines = trampolines_info [0];
977                 amodule->first_trampoline_got_offset = trampolines_info [1];
978         }       
979
980         if (make_unreadable) {
981 #ifndef PLATFORM_WIN32
982                 guint8 *addr;
983                 guint8 *page_start;
984                 int pages, err, len;
985
986                 addr = amodule->mem_begin;
987                 len = amodule->mem_end - amodule->mem_begin;
988
989                 /* Round down in both directions to avoid modifying data which is not ours */
990                 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
991                 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
992                 err = mprotect (page_start, pages * PAGESIZE, 0);
993                 g_assert (err == 0);
994 #endif
995         }
996
997         mono_aot_lock ();
998
999         aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1000         aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1001
1002         g_hash_table_insert (aot_modules, assembly, amodule);
1003         mono_aot_unlock ();
1004
1005         mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1006
1007         assembly->image->aot_module = amodule;
1008
1009         /*
1010          * Since we store methoddef and classdef tokens when referring to methods/classes in
1011          * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1012          * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1013          * non-lazily, since we can't handle out-of-date errors later.
1014          */
1015         for (i = 0; i < amodule->image_table_len; ++i)
1016                 load_image (amodule, i);
1017
1018         if (amodule->out_of_date) {
1019                 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);
1020                 if (mono_aot_only) {
1021                         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);
1022                         exit (1);
1023                 }
1024         }
1025         else
1026                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1027 }
1028
1029 /*
1030  * mono_aot_register_globals:
1031  *
1032  *   This is called by the ctor function in AOT images compiled with the
1033  * 'no-dlsym' option.
1034  */
1035 void
1036 mono_aot_register_globals (gpointer *globals)
1037 {
1038         TlsSetValue (globals_tls_id, globals);
1039 }
1040
1041 /*
1042  * mono_aot_register_module:
1043  *
1044  *   This should be called by embedding code to register AOT modules statically linked
1045  * into the executable. AOT_INFO should be the value of the 
1046  * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1047  */
1048 void
1049 mono_aot_register_module (gpointer *aot_info)
1050 {
1051         gpointer *globals;
1052         char *aname;
1053
1054         globals = aot_info;
1055         g_assert (globals);
1056
1057         /* Determine the assembly name */
1058         find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1059         g_assert (aname);
1060
1061         /* This could be called before startup */
1062         if (aot_modules)
1063                 mono_aot_lock ();
1064
1065         if (!static_aot_modules)
1066                 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1067
1068         g_hash_table_insert (static_aot_modules, aname, globals);
1069
1070         if (aot_modules)
1071                 mono_aot_unlock ();
1072 }
1073
1074 void
1075 mono_aot_init (void)
1076 {
1077         InitializeCriticalSection (&aot_mutex);
1078         aot_modules = g_hash_table_new (NULL, NULL);
1079         globals_tls_id = TlsAlloc ();
1080
1081         mono_install_assembly_load_hook (load_aot_module, NULL);
1082
1083         if (getenv ("MONO_LASTAOT"))
1084                 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
1085         if (getenv ("MONO_AOT_CACHE"))
1086                 use_aot_cache = TRUE;
1087 }
1088
1089 static gboolean
1090 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1091 {
1092         guint32 flags;
1093
1094         info->vtable_size = decode_value (buf, &buf);
1095         if (info->vtable_size == -1)
1096                 /* Generic type */
1097                 return FALSE;
1098         flags = decode_value (buf, &buf);
1099         info->ghcimpl = (flags >> 0) & 0x1;
1100         info->has_finalize = (flags >> 1) & 0x1;
1101         info->has_cctor = (flags >> 2) & 0x1;
1102         info->has_nested_classes = (flags >> 3) & 0x1;
1103         info->blittable = (flags >> 4) & 0x1;
1104         info->has_references = (flags >> 5) & 0x1;
1105         info->has_static_refs = (flags >> 6) & 0x1;
1106         info->no_special_static_fields = (flags >> 7) & 0x1;
1107
1108         if (info->has_cctor) {
1109                 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, buf, &buf);
1110                 if (!cctor_image)
1111                         return FALSE;
1112         }
1113         if (info->has_finalize) {
1114                 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, buf, &buf);
1115                 if (!info->finalize_image)
1116                         return FALSE;
1117         }
1118
1119         info->instance_size = decode_value (buf, &buf);
1120         info->class_size = decode_value (buf, &buf);
1121         info->packing_size = decode_value (buf, &buf);
1122         info->min_align = decode_value (buf, &buf);
1123
1124         *endbuf = buf;
1125
1126         return TRUE;
1127 }       
1128
1129 gpointer
1130 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1131 {
1132         int i;
1133         MonoClass *klass = vtable->klass;
1134         MonoAotModule *aot_module = klass->image->aot_module;
1135         guint8 *info, *p;
1136         MonoCachedClassInfo class_info;
1137         gboolean err;
1138         guint32 token;
1139         MonoImage *image;
1140
1141         if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1142                 return NULL;
1143
1144         info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1145         p = info;
1146
1147         err = decode_cached_class_info (aot_module, &class_info, p, &p);
1148         if (!err)
1149                 return NULL;
1150
1151         for (i = 0; i < slot; ++i)
1152                 decode_method_ref (aot_module, &token, NULL, p, &p);
1153
1154         image = decode_method_ref (aot_module, &token, NULL, p, &p);
1155         if (!image)
1156                 return NULL;
1157
1158         return mono_aot_get_method_from_token (domain, image, token);
1159 }
1160
1161 gboolean
1162 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1163 {
1164         MonoAotModule *aot_module = klass->image->aot_module;
1165         guint8 *p;
1166         gboolean err;
1167
1168         if (klass->rank || !aot_module)
1169                 return FALSE;
1170
1171         p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1172
1173         err = decode_cached_class_info (aot_module, res, p, &p);
1174         if (!err)
1175                 return FALSE;
1176
1177         return TRUE;
1178 }
1179
1180 /**
1181  * mono_aot_get_class_from_name:
1182  *
1183  *  Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1184  * using a cache stored in the AOT file.
1185  * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1186  *
1187  * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was 
1188  * found.
1189  */
1190 gboolean
1191 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1192 {
1193         MonoAotModule *aot_module = image->aot_module;
1194         guint16 *table, *entry;
1195         guint16 table_size;
1196         guint32 hash;
1197         char full_name_buf [1024];
1198         char *full_name;
1199         const char *name2, *name_space2;
1200         MonoTableInfo  *t;
1201         guint32 cols [MONO_TYPEDEF_SIZE];
1202         GHashTable *nspace_table;
1203
1204         if (!aot_module || !aot_module->class_name_table)
1205                 return FALSE;
1206
1207         mono_aot_lock ();
1208
1209         *klass = NULL;
1210
1211         /* First look in the cache */
1212         if (!aot_module->name_cache)
1213                 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1214         nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1215         if (nspace_table) {
1216                 *klass = g_hash_table_lookup (nspace_table, name);
1217                 if (*klass) {
1218                         mono_aot_unlock ();
1219                         return TRUE;
1220                 }
1221         }
1222
1223         table_size = aot_module->class_name_table [0];
1224         table = aot_module->class_name_table + 1;
1225
1226         if (name_space [0] == '\0')
1227                 full_name = g_strdup_printf ("%s", name);
1228         else {
1229                 if (strlen (name_space) + strlen (name) < 1000) {
1230                         sprintf (full_name_buf, "%s.%s", name_space, name);
1231                         full_name = full_name_buf;
1232                 } else {
1233                         full_name = g_strdup_printf ("%s.%s", name_space, name);
1234                 }
1235         }
1236         hash = g_str_hash (full_name) % table_size;
1237         if (full_name != full_name_buf)
1238                 g_free (full_name);
1239
1240         entry = &table [hash * 2];
1241
1242         if (entry [0] != 0) {
1243                 t = &image->tables [MONO_TABLE_TYPEDEF];
1244
1245                 while (TRUE) {
1246                         guint32 index = entry [0];
1247                         guint32 next = entry [1];
1248                         guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1249
1250                         name_table_accesses ++;
1251
1252                         mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1253
1254                         name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1255                         name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1256
1257                         if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1258                                 mono_aot_unlock ();
1259                                 *klass = mono_class_get (image, token);
1260
1261                                 /* Add to cache */
1262                                 if (*klass) {
1263                                         mono_aot_lock ();
1264                                         nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1265                                         if (!nspace_table) {
1266                                                 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1267                                                 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1268                                         }
1269                                         g_hash_table_insert (nspace_table, (char*)name2, *klass);
1270                                         mono_aot_unlock ();
1271                                 }
1272                                 return TRUE;
1273                         }
1274
1275                         if (next != 0) {
1276                                 entry = &table [next * 2];
1277                         } else {
1278                                 break;
1279                         }
1280                 }
1281         }
1282
1283         mono_aot_unlock ();
1284         
1285         return TRUE;
1286 }
1287
1288 static MonoJitInfo*
1289 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain, 
1290                                                          MonoMethod *method, guint8* ex_info, guint8 *code)
1291 {
1292         int i, buf_len;
1293         MonoJitInfo *jinfo;
1294         guint code_len, used_int_regs;
1295         gboolean has_generic_jit_info;
1296         guint8 *p;
1297         MonoMethodHeader *header;
1298         int generic_info_size;
1299
1300         header = mono_method_get_header (method);
1301
1302         /* Load the method info from the AOT file */
1303
1304         p = ex_info;
1305         code_len = decode_value (p, &p);
1306         used_int_regs = decode_value (p, &p);
1307         has_generic_jit_info = decode_value (p, &p);
1308         if (has_generic_jit_info)
1309                 generic_info_size = sizeof (MonoGenericJitInfo);
1310         else
1311                 generic_info_size = 0;
1312
1313         /* Exception table */
1314         if (header && header->num_clauses) {
1315                 jinfo = 
1316                         mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
1317                 jinfo->num_clauses = header->num_clauses;
1318
1319                 for (i = 0; i < header->num_clauses; ++i) {
1320                         MonoExceptionClause *ec = &header->clauses [i];                         
1321                         MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1322
1323                         ei->flags = ec->flags;
1324                         ei->exvar_offset = decode_value (p, &p);
1325
1326                         if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1327                                 ei->data.filter = code + decode_value (p, &p);
1328                         else
1329                                 ei->data.catch_class = ec->data.catch_class;
1330
1331                         ei->try_start = code + decode_value (p, &p);
1332                         ei->try_end = code + decode_value (p, &p);
1333                         ei->handler_start = code + decode_value (p, &p);
1334                 }
1335         }
1336         else
1337                 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + generic_info_size);
1338
1339         jinfo->code_size = code_len;
1340         jinfo->used_regs = used_int_regs;
1341         jinfo->method = method;
1342         jinfo->code_start = code;
1343         jinfo->domain_neutral = 0;
1344
1345         if (has_generic_jit_info) {
1346                 MonoGenericJitInfo *gi;
1347
1348                 jinfo->has_generic_jit_info = 1;
1349
1350                 gi = mono_jit_info_get_generic_jit_info (jinfo);
1351                 g_assert (gi);
1352
1353                 gi->has_this = decode_value (p, &p);
1354                 gi->this_reg = decode_value (p, &p);
1355                 gi->this_offset = decode_value (p, &p);
1356
1357                 /* This currently contains no data */
1358                 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1359
1360                 jinfo->method = decode_method_ref_2 (aot_module, p, &p);
1361         }
1362
1363         /* Load debug info */
1364         buf_len = decode_value (p, &p);
1365         mono_debug_add_aot_method (domain, method, code, p, buf_len);
1366         
1367         return jinfo;
1368 }
1369
1370 MonoJitInfo *
1371 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1372 {
1373         int pos, left, right, offset, offset1, offset2, last_offset, new_offset, page_index, method_index, table_len;
1374         guint32 token;
1375         MonoAotModule *aot_module = image->aot_module;
1376         MonoMethod *method;
1377         MonoJitInfo *jinfo;
1378         guint8 *code, *ex_info;
1379         guint32 *table, *ptr;
1380         gboolean found;
1381
1382         if (!aot_module)
1383                 return NULL;
1384
1385         if (domain != mono_get_root_domain ())
1386                 /* FIXME: */
1387                 return NULL;
1388
1389         offset = (guint8*)addr - aot_module->code;
1390
1391         /* First search through the index */
1392         ptr = aot_module->method_order;
1393         last_offset = 0;
1394         page_index = 0;
1395         found = FALSE;
1396
1397         if (*ptr == 0xffffff)
1398                 return NULL;
1399         ptr ++;
1400
1401         while (*ptr != 0xffffff) {
1402                 guint32 method_index = ptr [0];
1403                 new_offset = aot_module->code_offsets [method_index];
1404
1405                 if (offset >= last_offset && offset < new_offset) {
1406                         found = TRUE;
1407                         break;
1408                 }
1409
1410                 ptr ++;
1411                 last_offset = new_offset;
1412                 page_index ++;
1413         }
1414
1415         /* Skip rest of index */
1416         while (*ptr != 0xffffff)
1417                 ptr ++;
1418         ptr ++;
1419
1420         table = ptr;
1421         table_len = aot_module->method_order_end - table;
1422
1423         g_assert (table <= aot_module->method_order_end);
1424
1425         if (found) {
1426                 left = (page_index * 1024);
1427                 right = left + 1024;
1428
1429                 if (right > table_len)
1430                         right = table_len;
1431
1432                 offset1 = aot_module->code_offsets [table [left]];
1433                 g_assert (offset1 <= offset);
1434
1435                 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1436         }
1437         else {
1438                 //printf ("Not found in index: 0x%x\n", offset);
1439                 left = 0;
1440                 right = table_len;
1441         }
1442
1443         /* Binary search inside the method_order table to find the method */
1444         while (TRUE) {
1445                 pos = (left + right) / 2;
1446
1447                 g_assert (table + pos <= aot_module->method_order_end);
1448
1449                 //printf ("Pos: %5d < %5d < %5d Offset: 0x%05x < 0x%05x < 0x%05x\n", left, pos, right, aot_module->code_offsets [table [left]], offset, aot_module->code_offsets [table [right]]);
1450
1451                 offset1 = aot_module->code_offsets [table [pos]];
1452                 if (table + pos + 1 >= aot_module->method_order_end)
1453                         offset2 = aot_module->code_end - aot_module->code;
1454                 else
1455                         offset2 = aot_module->code_offsets [table [pos + 1]];
1456
1457                 if (offset < offset1)
1458                         right = pos;
1459                 else if (offset >= offset2)
1460                         left = pos + 1;
1461                 else
1462                         break;
1463         }
1464
1465         method_index = table [pos];
1466
1467         /* Might be a wrapper */
1468         if (aot_module->wrappers) {
1469                 mono_aot_lock ();
1470                 method = g_hash_table_lookup (aot_module->wrappers, GUINT_TO_POINTER (method_index));
1471                 mono_aot_unlock ();
1472         } else {
1473                 method = NULL;
1474         }
1475
1476         if (!method) {
1477                 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1478                 method = mono_get_method (image, token, NULL);
1479         }
1480
1481         /* FIXME: */
1482         g_assert (method);
1483
1484         //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1485
1486         code = &aot_module->code [aot_module->code_offsets [method_index]];
1487         ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1488
1489         jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1490
1491         g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1492         g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1493
1494         /* Add it to the normal JitInfo tables */
1495         mono_jit_info_table_add (domain, jinfo);
1496         
1497         return jinfo;
1498 }
1499
1500 /* Keep it in sync with the version in aot-compiler.c */
1501 static inline gboolean
1502 is_shared_got_patch (MonoJumpInfo *patch_info)
1503 {
1504         switch (patch_info->type) {
1505         case MONO_PATCH_INFO_VTABLE:
1506         case MONO_PATCH_INFO_CLASS:
1507         case MONO_PATCH_INFO_IID:
1508         case MONO_PATCH_INFO_ADJUSTED_IID:
1509         case MONO_PATCH_INFO_FIELD:
1510         case MONO_PATCH_INFO_SFLDA:
1511         case MONO_PATCH_INFO_DECLSEC:
1512         case MONO_PATCH_INFO_LDTOKEN:
1513         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1514         case MONO_PATCH_INFO_RVA:
1515         case MONO_PATCH_INFO_METHODCONST:
1516                 return TRUE;
1517         default:
1518                 return FALSE;
1519         }
1520 }
1521
1522 static gboolean
1523 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1524 {
1525         guint8 *p = buf;
1526         gpointer *table;
1527         MonoImage *image;
1528         int i;
1529
1530         switch (ji->type) {
1531         case MONO_PATCH_INFO_METHOD:
1532         case MONO_PATCH_INFO_METHOD_JUMP:
1533         case MONO_PATCH_INFO_ICALL_ADDR:
1534         case MONO_PATCH_INFO_METHOD_RGCTX: {
1535                 guint32 token;
1536                 MonoMethod *method;
1537
1538                 image = decode_method_ref (aot_module, &token, &method, p, &p);
1539                 if (!image)
1540                         goto cleanup;
1541
1542 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1543                 if (!method && !mono_aot_only && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1544                         ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1545                         ji->type = MONO_PATCH_INFO_ABS;
1546                 }
1547                 else {
1548                         if (method)
1549                                 ji->data.method = method;
1550                         else
1551                                 ji->data.method = mono_get_method (image, token, NULL);
1552                         g_assert (ji->data.method);
1553                         mono_class_init (ji->data.method->klass);
1554                 }
1555 #else
1556                 ji->data.method = mono_get_method (image, token, NULL);
1557                 g_assert (ji->data.method);
1558                 mono_class_init (ji->data.method->klass);
1559 #endif
1560
1561                 break;
1562         }
1563         case MONO_PATCH_INFO_INTERNAL_METHOD:
1564         case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1565                 guint32 len = decode_value (p, &p);
1566
1567                 ji->data.name = (char*)p;
1568                 p += len + 1;
1569                 break;
1570         }
1571         case MONO_PATCH_INFO_METHODCONST:
1572                 /* Shared */
1573                 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1574                 if (!ji->data.method)
1575                         goto cleanup;
1576                 break;
1577         case MONO_PATCH_INFO_VTABLE:
1578         case MONO_PATCH_INFO_CLASS:
1579         case MONO_PATCH_INFO_IID:
1580         case MONO_PATCH_INFO_ADJUSTED_IID:
1581                 /* Shared */
1582                 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1583                 if (!ji->data.klass)
1584                         goto cleanup;
1585                 break;
1586         case MONO_PATCH_INFO_CLASS_INIT:
1587         case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1588                 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1589                 if (!ji->data.klass)
1590                         goto cleanup;
1591                 break;
1592         case MONO_PATCH_INFO_IMAGE:
1593                 ji->data.image = load_image (aot_module, decode_value (p, &p));
1594                 if (!ji->data.image)
1595                         goto cleanup;
1596                 break;
1597         case MONO_PATCH_INFO_FIELD:
1598         case MONO_PATCH_INFO_SFLDA:
1599                 /* Shared */
1600                 ji->data.field = decode_field_info (aot_module, p, &p);
1601                 if (!ji->data.field)
1602                         goto cleanup;
1603                 break;
1604         case MONO_PATCH_INFO_SWITCH:
1605                 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1606                 ji->data.table->table_size = decode_value (p, &p);
1607                 table = g_new (gpointer, ji->data.table->table_size);
1608                 ji->data.table->table = (MonoBasicBlock**)table;
1609                 for (i = 0; i < ji->data.table->table_size; i++)
1610                         table [i] = (gpointer)(gssize)decode_value (p, &p);
1611                 break;
1612         case MONO_PATCH_INFO_R4: {
1613                 guint32 val;
1614                 
1615                 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1616                 val = decode_value (p, &p);
1617                 *(float*)ji->data.target = *(float*)&val;
1618                 break;
1619         }
1620         case MONO_PATCH_INFO_R8: {
1621                 guint32 val [2];
1622
1623                 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1624
1625                 val [0] = decode_value (p, &p);
1626                 val [1] = decode_value (p, &p);
1627                 *(double*)ji->data.target = *(double*)val;
1628                 break;
1629         }
1630         case MONO_PATCH_INFO_LDSTR:
1631                 image = load_image (aot_module, decode_value (p, &p));
1632                 if (!image)
1633                         goto cleanup;
1634                 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1635                 break;
1636         case MONO_PATCH_INFO_RVA:
1637         case MONO_PATCH_INFO_DECLSEC:
1638         case MONO_PATCH_INFO_LDTOKEN:
1639         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1640                 /* Shared */
1641                 image = load_image (aot_module, decode_value (p, &p));
1642                 if (!image)
1643                         goto cleanup;
1644                 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1645                 break;
1646         case MONO_PATCH_INFO_EXC_NAME:
1647                 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1648                 if (!ji->data.klass)
1649                         goto cleanup;
1650                 ji->data.name = ji->data.klass->name;
1651                 break;
1652         case MONO_PATCH_INFO_METHOD_REL:
1653                 ji->data.offset = decode_value (p, &p);
1654                 break;
1655         case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1656         case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1657                 break;
1658         case MONO_PATCH_INFO_RGCTX_FETCH: {
1659                 gboolean res;
1660                 MonoJumpInfoRgctxEntry *entry;
1661
1662                 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1663                 entry->method = decode_method_ref_2 (aot_module, p, &p);
1664                 entry->in_mrgctx = decode_value (p, &p);
1665                 entry->info_type = decode_value (p, &p);
1666                 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1667                 entry->data->type = decode_value (p, &p);
1668                 
1669                 res = decode_patch (aot_module, mp, entry->data, p, &p);
1670                 if (!res)
1671                         goto cleanup;
1672                 ji->data.rgctx_entry = entry;
1673                 break;
1674         }
1675         default:
1676                 g_warning ("unhandled type %d", ji->type);
1677                 g_assert_not_reached ();
1678         }
1679
1680         *endbuf = p;
1681
1682         return TRUE;
1683
1684  cleanup:
1685         return FALSE;
1686 }
1687
1688 static gboolean
1689 decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1690 {
1691         guint8 *p = buf;
1692         guint8 *shared_p;
1693         gboolean res;
1694
1695         if (is_shared_got_patch (ji)) {
1696                 *got_offset = decode_value (p, &p);
1697
1698                 if (aot_module->got [*got_offset]) {
1699                         /* Already loaded */
1700                         //printf ("HIT!\n");
1701                 } else {
1702                         shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1703
1704                         res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1705                         if (!res)
1706                                 return FALSE;
1707                 }
1708         } else {
1709                 res = decode_patch (aot_module, mp, ji, p, &p);
1710                 if (!res)
1711                         return FALSE;
1712         }
1713
1714         *endbuf = p;
1715         return TRUE;
1716 }
1717
1718 static MonoJumpInfo*
1719 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches, 
1720                                  guint32 got_index, guint32 **got_slots, 
1721                                  guint8 *buf, guint8 **endbuf)
1722 {
1723         MonoJumpInfo *patches;
1724         MonoJumpInfo *patch_info = NULL;
1725         int pindex;
1726         guint32 last_offset;
1727         guint8 *p;
1728
1729         p = buf;
1730
1731         /* First load the type + offset table */
1732         last_offset = 0;
1733         patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1734
1735         for (pindex = 0; pindex < n_patches; ++pindex) {                
1736                 MonoJumpInfo *ji = &patches [pindex];
1737
1738                 ji->type = *p;
1739                 p ++;
1740
1741                 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1742                 ji->next = patch_info;
1743                 patch_info = ji;
1744         }
1745
1746         *got_slots = g_malloc (sizeof (guint32) * n_patches);
1747         memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1748
1749         /* Then load the other data */
1750         for (pindex = 0; pindex < n_patches; ++pindex) {
1751                 MonoJumpInfo *ji = &patches [pindex];
1752
1753                 if (!decode_got_entry (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1754                         goto cleanup;
1755
1756                 if ((*got_slots) [pindex] == 0xffffffff)
1757                         (*got_slots) [pindex] = got_index ++;
1758         }
1759
1760         *endbuf = p;
1761         return patches;
1762
1763  cleanup:
1764         g_free (*got_slots);
1765         *got_slots = NULL;
1766
1767         return NULL;
1768 }
1769
1770 static void
1771 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1772 {
1773         /*
1774          * Jump addresses cannot be patched by the trampoline code since it
1775          * does not have access to the caller's address. Instead, we collect
1776          * the addresses of the GOT slots pointing to a method, and patch
1777          * them after the method has been compiled.
1778          */
1779         MonoJitDomainInfo *info = jit_domain_info (domain);
1780         GSList *list;
1781                 
1782         mono_domain_lock (domain);
1783         if (!info->jump_target_got_slot_hash)
1784                 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1785         list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1786         list = g_slist_prepend (list, got_slot);
1787         g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
1788         mono_domain_unlock (domain);
1789 }
1790
1791 gpointer
1792 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1793 {
1794         MonoClass *klass = method->klass;
1795         guint32 method_index = mono_metadata_token_index (method->token) - 1;
1796         guint8 *code, *info;
1797         MonoAotModule *aot_module = klass->image->aot_module;
1798
1799         if (!aot_module)
1800                 return NULL;
1801
1802         if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1803                 return NULL;
1804
1805         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1806                 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1807                 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1808                 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1809                 return NULL;
1810
1811         g_assert (klass->inited);
1812
1813         if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1814                 /* Non shared AOT code can't be used in other appdomains */
1815                 return NULL;
1816
1817         if (aot_module->out_of_date)
1818                 return NULL;
1819
1820         if (method->is_inflated) {
1821                 if (!mono_method_is_generic_sharable_impl (method, FALSE))
1822                         return NULL;
1823                 method = mono_method_get_declaring_generic_method (method);
1824         }
1825
1826         if (!method->token) {
1827                 char *full_name;
1828                 char *p;
1829
1830                 if (!method->wrapper_type || !aot_module->wrapper_info)
1831                         return NULL;
1832
1833                 /* Try to find the wrapper among the wrapper info */
1834                 full_name = mono_method_full_name (method, TRUE);
1835                 p = (char*)aot_module->wrapper_info;
1836                 while (*p) {
1837                         char *end;
1838
1839                         end = p + strlen (p) + 1;
1840                         end = ALIGN_PTR_TO (end, 4);
1841                         method_index = *(guint32*)end;
1842                         end += 4;
1843                         if (strcmp (full_name, p) == 0)
1844                                 break;
1845                         p = end;
1846                 }
1847                 g_free (full_name);
1848
1849                 if (!(*p))
1850                         /* Not found */
1851                         return NULL;
1852
1853                 /* Needed by find_jit_info */
1854                 mono_aot_lock ();
1855                 if (!aot_module->wrappers)
1856                         aot_module->wrappers = g_hash_table_new (NULL, NULL);
1857                 g_hash_table_insert (aot_module->wrappers, GUINT_TO_POINTER (method_index), method);
1858                 mono_aot_unlock ();
1859         }
1860
1861         if (aot_module->code_offsets [method_index] == 0xffffffff) {
1862                 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1863                         char *full_name = mono_method_full_name (method, TRUE);
1864                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1865                         g_free (full_name);
1866                 }
1867                 return NULL;
1868         }
1869
1870         code = &aot_module->code [aot_module->code_offsets [method_index]];
1871         info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1872
1873         mono_aot_lock ();
1874         if (!aot_module->methods_loaded)
1875                 aot_module->methods_loaded = g_new0 (guint32, klass->image->tables [MONO_TABLE_METHOD].rows + 1);
1876         mono_aot_unlock ();
1877
1878         if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1879                 return code;
1880
1881         if (mono_last_aot_method != -1) {
1882                 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1883                                 return NULL;
1884                 else
1885                         if (mono_jit_stats.methods_aot == mono_last_aot_method)
1886                                 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
1887         }
1888
1889         return mono_aot_load_method (domain, aot_module, method, code, info, method_index);
1890 }
1891
1892 static gpointer
1893 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info, int method_index)
1894 {
1895         MonoClass *klass = method->klass;
1896         MonoJumpInfo *patch_info = NULL;
1897         MonoMemPool *mp;
1898         int i, pindex, got_index = 0, n_patches, used_strings;
1899         gboolean non_got_patches, keep_patches = TRUE;
1900         guint8 *p, *ex_info;
1901         MonoJitInfo *jinfo = NULL;
1902
1903         p = info;
1904         decode_klass_ref (aot_module, p, &p);
1905
1906         if (!use_loaded_code) {
1907                 guint8 *code2;
1908
1909                 if (!jinfo) {
1910                         ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1911                         jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1912                 }
1913
1914                 mono_domain_lock (domain);
1915                 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1916                 mono_domain_unlock (domain);
1917                 memcpy (code2, code, jinfo->code_size);
1918                 mono_arch_flush_icache (code2, jinfo->code_size);
1919                 code = code2;
1920         }
1921
1922         if (aot_module->opts & MONO_OPT_SHARED)
1923                 used_strings = decode_value (p, &p);
1924         else
1925                 used_strings = 0;
1926
1927         for (i = 0; i < used_strings; i++) {
1928                 guint token = decode_value (p, &p);
1929                 mono_ldstr (mono_get_root_domain (), klass->image, mono_metadata_token_index (token));
1930         }
1931
1932         if (aot_module->opts & MONO_OPT_SHARED) 
1933                 keep_patches = FALSE;
1934
1935         n_patches = decode_value (p, &p);
1936
1937         keep_patches = FALSE;
1938
1939         if (n_patches) {
1940                 MonoJumpInfo *patches;
1941                 guint32 *got_slots;
1942
1943                 if (keep_patches)
1944                         mp = domain->mp;
1945                 else
1946                         mp = mono_mempool_new ();
1947
1948                 got_index = decode_value (p, &p);
1949
1950                 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1951                 if (patches == NULL)
1952                         goto cleanup;
1953
1954                 non_got_patches = FALSE;
1955                 for (pindex = 0; pindex < n_patches; ++pindex) {
1956                         MonoJumpInfo *ji = &patches [pindex];
1957
1958                         if (is_got_patch (ji->type)) {
1959                                 if (!aot_module->got [got_slots [pindex]]) {
1960                                         aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
1961                                         if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
1962                                                 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
1963                                 }
1964                                 ji->type = MONO_PATCH_INFO_NONE;
1965                         }
1966                         else
1967                                 non_got_patches = TRUE;
1968                 }
1969                 if (non_got_patches) {
1970                         if (!jinfo) {
1971                                 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1972                                 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1973                         }
1974
1975                         mono_arch_flush_icache (code, jinfo->code_size);
1976                         make_writable (code, jinfo->code_size);
1977                         mono_arch_patch_code (method, domain, code, patch_info, TRUE);
1978                 }
1979
1980                 g_free (got_slots);
1981
1982                 if (!keep_patches)
1983                         mono_mempool_destroy (mp);
1984         }
1985
1986         mono_aot_lock ();
1987
1988         mono_jit_stats.methods_aot++;
1989
1990         if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1991                 char *full_name = mono_method_full_name (method, TRUE);
1992
1993                 if (!jinfo) {
1994                         ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1995                         jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1996                 }
1997
1998                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p - %p %p\n", full_name, code, code + jinfo->code_size, info);
1999                 g_free (full_name);
2000         }
2001
2002         aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2003
2004         init_plt (aot_module);
2005
2006         mono_aot_unlock ();
2007
2008         return code;
2009
2010  cleanup:
2011         /* FIXME: The space in domain->mp is wasted */  
2012         if (aot_module->opts & MONO_OPT_SHARED)
2013                 /* No need to cache patches */
2014                 mono_mempool_destroy (mp);
2015
2016         if (jinfo)
2017                 g_free (jinfo);
2018
2019         return NULL;
2020 }
2021
2022 /**
2023  * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2024  * method.
2025  */
2026 gpointer
2027 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2028 {
2029         MonoMemPool *mp;
2030         int i, method_index, pindex, got_index, n_patches, used_strings;
2031         gboolean keep_patches = TRUE;
2032         guint8 *p;
2033         guint8 *code = NULL;
2034         guint8 *info;
2035         MonoAotModule *aot_module = image->aot_module;
2036         MonoClass *klass;
2037
2038         if (!aot_module)
2039                 return NULL;
2040
2041         if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2042                 return NULL;
2043
2044         if (domain != mono_get_root_domain ())
2045                 return NULL;
2046
2047         if (aot_module->out_of_date)
2048                 return NULL;
2049
2050         if (aot_module->code_offsets [mono_metadata_token_index (token) - 1] == 0xffffffff) {
2051                 return NULL;
2052         }
2053
2054         method_index = mono_metadata_token_index (token) - 1;
2055         code = &aot_module->code [aot_module->code_offsets [method_index]];
2056         info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
2057
2058         mono_aot_lock ();
2059         if (!aot_module->methods_loaded)
2060                 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
2061         mono_aot_unlock ();
2062
2063         if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2064                 return code;
2065
2066         if (mono_last_aot_method != -1) {
2067                 if (mono_jit_stats.methods_aot > mono_last_aot_method)
2068                                 return NULL;
2069                 else
2070                         if (mono_jit_stats.methods_aot == mono_last_aot_method) {
2071                                 MonoMethod *method = mono_get_method (image, token, NULL);
2072                                 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
2073                         }
2074         }
2075
2076         p = info;
2077         klass = decode_klass_ref (aot_module, p, &p);
2078
2079         if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2080                 MonoMethod *method = mono_get_method (image, token, NULL);
2081                 char *full_name = mono_method_full_name (method, TRUE);
2082                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p %p\n", full_name, code, info);
2083                 g_free (full_name);
2084         }
2085
2086         if (aot_module->opts & MONO_OPT_SHARED)
2087                 used_strings = decode_value (p, &p);
2088         else
2089                 used_strings = 0;
2090
2091         for (i = 0; i < used_strings; i++) {
2092                 guint string_token = decode_value (p, &p);
2093                 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (string_token));
2094         }
2095
2096         if (aot_module->opts & MONO_OPT_SHARED) 
2097                 keep_patches = FALSE;
2098
2099         keep_patches = FALSE;
2100
2101         n_patches = decode_value (p, &p);
2102
2103         if (n_patches) {
2104                 MonoJumpInfo *patches;
2105                 guint32 *got_slots;
2106
2107                 if (keep_patches)
2108                         mp = domain->mp;
2109                 else
2110                         mp = mono_mempool_new ();
2111
2112                 got_index = decode_value (p, &p);
2113
2114                 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
2115                 if (patches == NULL)
2116                         goto cleanup;
2117
2118                 for (pindex = 0; pindex < n_patches; ++pindex) {
2119                         MonoJumpInfo *ji = &patches [pindex];
2120
2121                         if (is_got_patch (ji->type)) {
2122                                 if (!aot_module->got [got_slots [pindex]]) {
2123                                         aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (NULL, domain, code, ji, TRUE);
2124                                         if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2125                                                 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
2126                                 }
2127                                 ji->type = MONO_PATCH_INFO_NONE;
2128                         }
2129                 }
2130
2131                 g_free (got_slots);
2132
2133                 if (!keep_patches)
2134                         mono_mempool_destroy (mp);
2135         }
2136
2137         mono_jit_stats.methods_aot++;
2138
2139         mono_aot_lock ();
2140
2141         aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2142
2143         init_plt (aot_module);
2144
2145         mono_aot_unlock ();
2146
2147         if (klass)
2148                 mono_runtime_class_init (mono_class_vtable (domain, klass));
2149
2150         return code;
2151
2152  cleanup:
2153         /* FIXME: The space in domain->mp is wasted */  
2154         if (aot_module->opts & MONO_OPT_SHARED)
2155                 /* No need to cache patches */
2156                 mono_mempool_destroy (mp);
2157
2158         return NULL;
2159 }
2160
2161 typedef struct {
2162         guint8 *addr;
2163         gboolean res;
2164 } IsGotEntryUserData;
2165
2166 static void
2167 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2168 {
2169         IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2170         MonoAotModule *aot_module = (MonoAotModule*)value;
2171
2172         if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
2173                 data->res = TRUE;
2174 }
2175
2176 gboolean
2177 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2178 {
2179         IsGotEntryUserData user_data;
2180
2181         if (!aot_modules)
2182                 return FALSE;
2183
2184         user_data.addr = addr;
2185         user_data.res = FALSE;
2186         mono_aot_lock ();
2187         g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2188         mono_aot_unlock ();
2189         
2190         return user_data.res;
2191 }
2192
2193 typedef struct {
2194         guint8 *addr;
2195         MonoAotModule *module;
2196 } FindAotModuleUserData;
2197
2198 static void
2199 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2200 {
2201         FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2202         MonoAotModule *aot_module = (MonoAotModule*)value;
2203
2204         if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2205                 data->module = aot_module;
2206 }
2207
2208 static inline MonoAotModule*
2209 find_aot_module (guint8 *code)
2210 {
2211         FindAotModuleUserData user_data;
2212
2213         if (!aot_modules)
2214                 return NULL;
2215
2216         /* Reading these need no locking */
2217         if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2218                 return NULL;
2219
2220         user_data.addr = code;
2221         user_data.module = NULL;
2222                 
2223         mono_aot_lock ();
2224         g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2225         mono_aot_unlock ();
2226         
2227         return user_data.module;
2228 }
2229
2230 /*
2231  * mono_aot_set_make_unreadable:
2232  *
2233  *   Set whenever to make all mmaped memory unreadable. In conjuction with a
2234  * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
2235  */
2236 void
2237 mono_aot_set_make_unreadable (gboolean unreadable)
2238 {
2239         make_unreadable = unreadable;
2240 }
2241
2242 typedef struct {
2243         MonoAotModule *module;
2244         guint8 *ptr;
2245 } FindMapUserData;
2246
2247 static void
2248 find_map (gpointer key, gpointer value, gpointer user_data)
2249 {
2250         MonoAotModule *module = (MonoAotModule*)value;
2251         FindMapUserData *data = (FindMapUserData*)user_data;
2252
2253         if (!data->module)
2254                 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
2255                         data->module = module;
2256 }
2257
2258 static MonoAotModule*
2259 find_module_for_addr (void *ptr)
2260 {
2261         FindMapUserData data;
2262
2263         if (!make_unreadable)
2264                 return NULL;
2265
2266         data.module = NULL;
2267         data.ptr = (guint8*)ptr;
2268
2269         mono_aot_lock ();
2270         g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
2271         mono_aot_unlock ();
2272
2273         return data.module;
2274 }
2275
2276 /*
2277  * mono_aot_is_pagefault:
2278  *
2279  *   Should be called from a SIGSEGV signal handler to find out whenever @ptr is
2280  * within memory allocated by this module.
2281  */
2282 gboolean
2283 mono_aot_is_pagefault (void *ptr)
2284 {
2285         if (!make_unreadable)
2286                 return FALSE;
2287
2288         return find_module_for_addr (ptr) != NULL;
2289 }
2290
2291 /*
2292  * mono_aot_handle_pagefault:
2293  *
2294  *   Handle a pagefault caused by an unreadable page by making it readable again.
2295  */
2296 void
2297 mono_aot_handle_pagefault (void *ptr)
2298 {
2299 #ifndef PLATFORM_WIN32
2300         guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
2301         int res;
2302
2303         mono_aot_lock ();
2304         res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
2305         g_assert (res == 0);
2306
2307         n_pagefaults ++;
2308         mono_aot_unlock ();
2309
2310 #if 0
2311  {
2312         void *array [256];
2313         char **names;
2314         int i, size;
2315
2316         printf ("\nNative stacktrace:\n\n");
2317
2318         size = backtrace (array, 256);
2319         names = backtrace_symbols (array, size);
2320         for (i =0; i < size; ++i) {
2321                 printf ("\t%s\n", names [i]);
2322         }
2323         free (names);
2324  }
2325 #endif
2326
2327 #endif
2328 }
2329
2330 /*
2331  * mono_aot_plt_resolve:
2332  *
2333  *   This function is called by the entries in the PLT to resolve the actual method that
2334  * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2335  */
2336 gpointer
2337 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2338 {
2339 #ifdef MONO_ARCH_AOT_SUPPORTED
2340         guint8 *p, *target, *plt_entry;
2341         MonoJumpInfo ji;
2342         MonoAotModule *module = (MonoAotModule*)aot_module;
2343         gboolean res;
2344         MonoMemPool *mp;
2345
2346         //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2347
2348         p = &module->plt_info [plt_info_offset];
2349
2350         ji.type = decode_value (p, &p);
2351
2352         mp = mono_mempool_new_size (512);
2353         res = decode_patch (module, mp, &ji, p, &p);
2354         // FIXME: Error handling (how ?)
2355         g_assert (res);
2356
2357         target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2358
2359         mono_mempool_destroy (mp);
2360
2361         /* Patch the PLT entry with target which might be the actual method not a trampoline */
2362         plt_entry = mono_aot_get_plt_entry (code);
2363         g_assert (plt_entry);
2364         mono_arch_patch_plt_entry (plt_entry, target);
2365
2366         return target;
2367 #else
2368         g_assert_not_reached ();
2369         return NULL;
2370 #endif
2371 }
2372
2373 /**
2374  * init_plt:
2375  *
2376  *   Initialize the PLT table of the AOT module. Called lazily when the first AOT
2377  * method in the module is loaded to avoid committing memory by writing to it.
2378  * LOCKING: Assumes the AOT lock is held.
2379  */
2380 static void
2381 init_plt (MonoAotModule *info)
2382 {
2383 #ifdef MONO_ARCH_AOT_SUPPORTED
2384 #ifdef __i386__
2385         guint8 *buf = info->plt;
2386 #elif defined(__x86_64__)
2387         int i, n_entries;
2388 #elif defined(__arm__)
2389         int i, n_entries;
2390 #endif
2391         gpointer tramp;
2392
2393         if (info->plt_inited)
2394                 return;
2395
2396         tramp = mono_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2397
2398 #ifdef __i386__
2399         /* Initialize the first PLT entry */
2400         make_writable (info->plt, info->plt_end - info->plt);
2401         x86_jump_code (buf, tramp);
2402 #elif defined(__x86_64__) || defined(__arm__)
2403         /*
2404          * Initialize the entries in the plt_jump_table to point to the default targets.
2405          */
2406          n_entries = info->plt_jump_table_size / sizeof (gpointer);
2407
2408          /* The first entry points to the AOT trampoline */
2409          ((gpointer*)info->plt_jump_table)[0] = tramp;
2410          for (i = 1; i < n_entries; ++i)
2411                  /* All the default entries point to the first entry */
2412                  ((gpointer*)info->plt_jump_table)[i] = info->plt;
2413 #else
2414         g_assert_not_reached ();
2415 #endif
2416
2417         info->plt_inited = TRUE;
2418 #endif
2419 }
2420
2421 /*
2422  * mono_aot_get_plt_entry:
2423  *
2424  *   Return the address of the PLT entry called by the code at CODE if exists.
2425  */
2426 guint8*
2427 mono_aot_get_plt_entry (guint8 *code)
2428 {
2429         MonoAotModule *aot_module = find_aot_module (code);
2430 #if defined(__arm__)
2431         guint32 ins;
2432 #endif
2433
2434         if (!aot_module)
2435                 return NULL;
2436
2437 #if defined(__i386__) || defined(__x86_64__)
2438         if (code [-5] == 0xe8) {
2439                 guint32 disp = *(guint32*)(code - 4);
2440                 guint8 *target = code + disp;
2441
2442                 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2443                         return target;
2444         }
2445 #elif defined(__arm__)
2446         ins = ((guint32*)(gpointer)code) [-1];
2447
2448         /* Should be a 'bl' */
2449         if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2450                 gint32 disp = ((gint32)ins) & 0xffffff;
2451                 guint8 *target = code - 4 + 8 + (disp * 4);
2452
2453                 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2454                         return target;
2455         }               
2456 #else
2457         g_assert_not_reached ();
2458 #endif
2459
2460         return NULL;
2461 }
2462
2463 /*
2464  * mono_aot_get_plt_info_offset:
2465  *
2466  *   Return the PLT info offset belonging to the plt entry called by CODE.
2467  */
2468 guint32
2469 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2470 {
2471         guint8 *plt_entry = mono_aot_get_plt_entry (code);
2472
2473         g_assert (plt_entry);
2474
2475         /* The offset is embedded inside the code after the plt entry */
2476 #if defined(__i386__)
2477         return *(guint32*)(plt_entry + 5);
2478 #elif defined(__x86_64__)
2479         return *(guint32*)(plt_entry + 6);
2480 #elif defined(__arm__)
2481         /* The offset is stored as the 5th word of the plt entry */
2482         return ((guint32*)plt_entry) [4];
2483 #else
2484         g_assert_not_reached ();
2485         return 0;
2486 #endif
2487 }
2488
2489 static gpointer
2490 load_named_code (MonoAotModule *amodule, const char *name)
2491 {
2492         char *symbol;
2493         guint8 *p;
2494         int n_patches, got_index, pindex;
2495         MonoMemPool *mp;
2496         gpointer code;
2497
2498         /* Load the code */
2499
2500         symbol = g_strdup_printf ("%s", name);
2501         find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2502         g_free (symbol);
2503         g_assert (code);
2504
2505         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2506
2507         /* Load info */
2508
2509         symbol = g_strdup_printf ("%s_p", name);
2510         find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2511         g_free (symbol);
2512         if (!p)
2513                 /* Nothing to patch */
2514                 return code;
2515
2516         /* Similar to mono_aot_load_method () */
2517
2518         n_patches = decode_value (p, &p);
2519
2520         if (n_patches) {
2521                 MonoJumpInfo *patches;
2522                 guint32 *got_slots;
2523
2524                 mp = mono_mempool_new ();
2525
2526                 got_index = decode_value (p, &p);
2527
2528                 patches = load_patch_info (amodule, mp, n_patches, got_index, &got_slots, p, &p);
2529                 g_assert (patches);
2530
2531                 for (pindex = 0; pindex < n_patches; ++pindex) {
2532                         MonoJumpInfo *ji = &patches [pindex];
2533                         gpointer target;
2534
2535                         /*
2536                          * When this code is executed, the runtime may not yet initalized, so
2537                          * resolve the patch info by hand.
2538                          */
2539                         if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2540                                 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2541                                         target = mono_get_lmf_addr;
2542                                 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2543                                         target = mono_thread_force_interruption_checkpoint;
2544                                 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2545                                         target = mono_exception_from_token;
2546                                 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2547                                         target = mono_get_throw_exception ();
2548 #ifdef __x86_64__
2549                                 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2550                                         target = mono_amd64_throw_exception;
2551 #endif
2552 #ifdef __arm__
2553                                 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2554                                         target = mono_arm_throw_exception;
2555 #endif
2556                                 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2557                                         int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2558                                         target = (gpointer)mono_get_trampoline_func (tramp_type2);
2559                                 } else {
2560                                         fprintf (stderr, "%s\n", ji->data.name);
2561                                         g_assert_not_reached ();
2562                                         target = NULL;
2563                                 }
2564                         } else {
2565                                 /* Hopefully the code doesn't have patches which need method or 
2566                                  * domain to be set.
2567                                  */
2568                                 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2569                         }
2570
2571                         amodule->got [got_slots [pindex]] = target;
2572                 }
2573
2574                 g_free (got_slots);
2575
2576                 mono_mempool_destroy (mp);
2577         }
2578
2579         return code;
2580 }
2581
2582 /*
2583  * Return the piece of code identified by NAME from the mscorlib AOT file.
2584  */
2585 gpointer
2586 mono_aot_get_named_code (const char *name)
2587 {
2588         MonoImage *image;
2589         MonoAotModule *amodule;
2590
2591         image = mono_defaults.corlib;
2592         g_assert (image);
2593
2594         amodule = image->aot_module;
2595         g_assert (amodule);
2596
2597         return load_named_code (amodule, name);
2598 }
2599
2600 /*
2601  * Return a specific trampoline from the AOT file.
2602  */
2603 gpointer
2604 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2605 {
2606         MonoAotModule *amodule;
2607         int index, tramp_size;
2608         guint8 *code, *tramp;
2609         static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2610
2611         /* Currently, we keep all trampolines in the mscorlib AOT image */
2612         image = mono_defaults.corlib;
2613         g_assert (image);
2614
2615         mono_aot_lock ();
2616
2617         amodule = image->aot_module;
2618         g_assert (amodule);
2619
2620         if (amodule->trampoline_index == amodule->num_trampolines)
2621                 g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_trampolines);
2622
2623         index = amodule->trampoline_index ++;
2624
2625         mono_aot_unlock ();
2626
2627         if (!generic_trampolines [tramp_type]) {
2628                 char *symbol;
2629
2630                 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2631                 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2632                 g_free (symbol);
2633         }
2634
2635         tramp = generic_trampolines [tramp_type];
2636         g_assert (tramp);
2637
2638         amodule->got [amodule->first_trampoline_got_offset + (index *2)] = tramp;
2639         amodule->got [amodule->first_trampoline_got_offset + (index *2) + 1] = arg1;
2640
2641 #ifdef __x86_64__
2642         tramp_size = 16;
2643 #elif defined(__arm__)
2644         tramp_size = 28;
2645 #else
2646         tramp_size = -1;
2647         g_assert_not_reached ();
2648 #endif
2649
2650         code = amodule->trampolines + (index * tramp_size);
2651         if (code_len)
2652                 *code_len = tramp_size;
2653
2654         return code;
2655 }
2656
2657 gpointer
2658 mono_aot_get_unbox_trampoline (MonoMethod *method)
2659 {
2660         guint32 method_index = mono_metadata_token_index (method->token) - 1;
2661         MonoAotModule *amodule;
2662         char *symbol;
2663         gpointer code;
2664
2665         amodule = method->klass->image->aot_module;
2666         g_assert (amodule);
2667
2668         symbol = g_strdup_printf ("unbox_trampoline_%d", method_index);
2669         code = load_named_code (amodule, symbol);
2670         g_free (symbol);
2671         return code;
2672 }
2673
2674 /*
2675  * mono_aot_get_n_pagefaults:
2676  *
2677  *   Return the number of times handle_pagefault is called.
2678  */
2679 guint32
2680 mono_aot_get_n_pagefaults (void)
2681 {
2682         return n_pagefaults;
2683 }
2684
2685 #else
2686 /* AOT disabled */
2687
2688 void
2689 mono_aot_init (void)
2690 {
2691 }
2692
2693 gpointer
2694 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2695 {
2696         return NULL;
2697 }
2698
2699 gboolean
2700 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2701 {
2702         return FALSE;
2703 }
2704
2705 gboolean
2706 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2707 {
2708         return FALSE;
2709 }
2710
2711 gboolean
2712 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2713 {
2714         return FALSE;
2715 }
2716
2717 MonoJitInfo *
2718 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2719 {
2720         return NULL;
2721 }
2722
2723 gpointer
2724 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2725 {
2726         return NULL;
2727 }
2728
2729 gboolean
2730 mono_aot_is_pagefault (void *ptr)
2731 {
2732         return FALSE;
2733 }
2734
2735 void
2736 mono_aot_set_make_unreadable (gboolean unreadable)
2737 {
2738 }
2739
2740 guint32
2741 mono_aot_get_n_pagefaults (void)
2742 {
2743         return 0;
2744 }
2745
2746 void
2747 mono_aot_handle_pagefault (void *ptr)
2748 {
2749 }
2750
2751 guint8*
2752 mono_aot_get_plt_entry (guint8 *code)
2753 {
2754         return NULL;
2755 }
2756
2757 gpointer
2758 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2759 {
2760         return NULL;
2761 }
2762
2763 gpointer
2764 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2765 {
2766         return NULL;
2767 }
2768
2769 gpointer
2770 mono_aot_create_specific_trampolines (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2771 {
2772         g_assert_not_reached ();
2773         return NULL;
2774 }
2775
2776 gpointer
2777 mono_aot_get_named_code (char *name)
2778 {
2779         g_assert_not_reached ();
2780         return NULL;
2781 }
2782
2783 gpointer
2784 mono_aot_get_unbox_trampoline (MonoMethod *method)
2785 {
2786         g_assert_not_reached ();
2787         return NULL;
2788 }
2789
2790 #endif