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