Fix problems with overlong directory names: phase #1
[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 #include <unistd.h>
14 #include <fcntl.h>
15 #include <string.h>
16 #ifndef PLATFORM_WIN32
17 #include <sys/mman.h>
18 #else
19 #include <winsock2.h>
20 #include <windows.h>
21 #endif
22
23 #ifdef HAVE_EXECINFO_H
24 #include <execinfo.h>
25 #endif
26
27 #include <errno.h>
28 #include <sys/stat.h>
29 #include <limits.h>    /* for PAGESIZE */
30 #ifndef PAGESIZE
31 #define PAGESIZE 4096
32 #endif
33
34 #include <mono/metadata/tabledefs.h>
35 #include <mono/metadata/class.h>
36 #include <mono/metadata/object.h>
37 #include <mono/metadata/tokentype.h>
38 #include <mono/metadata/appdomain.h>
39 #include <mono/metadata/debug-helpers.h>
40 #include <mono/metadata/assembly.h>
41 #include <mono/metadata/metadata-internals.h>
42 #include <mono/metadata/marshal.h>
43 #include <mono/utils/mono-logger.h>
44 #include "mono/utils/mono-compiler.h"
45
46 #include "mini.h"
47
48 #ifndef DISABLE_AOT
49
50 #ifdef PLATFORM_WIN32
51 #define SHARED_EXT ".dll"
52 #elif defined(__ppc__) && defined(__MACH__)
53 #define SHARED_EXT ".dylib"
54 #else
55 #define SHARED_EXT ".so"
56 #endif
57
58 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
59 #define ROUND_DOWN(VALUE,SIZE)  ((VALUE) & ~((SIZE) - 1))
60
61 typedef struct MonoAotModule {
62         char *aot_name;
63         /* Optimization flags used to compile the module */
64         guint32 opts;
65         /* Pointer to the Global Offset Table */
66         gpointer *got;
67         guint32 got_size;
68         MonoAssemblyName *image_names;
69         char **image_guids;
70         MonoImage **image_table;
71         guint32 image_table_len;
72         gboolean out_of_date;
73         gboolean plt_inited;
74         guint8 *mem_begin;
75         guint8 *mem_end;
76         guint8 *code;
77         guint8 *code_end;
78         guint8 *plt;
79         guint8 *plt_end;
80         guint8 *plt_info;
81         guint8 *plt_jump_table;
82         guint32 plt_jump_table_size;
83         guint32 *code_offsets;
84         guint8 *method_info;
85         guint32 *method_info_offsets;
86         guint8 *got_info;
87         guint32 *got_info_offsets;
88         guint8 *ex_info;
89         guint32 *ex_info_offsets;
90         guint32 *method_order;
91         guint32 *method_order_end;
92         guint8 *class_info;
93         guint32 *class_info_offsets;
94         guint32 *methods_loaded;
95         guint16 *class_name_table;
96 } MonoAotModule;
97
98 static GHashTable *aot_modules;
99 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
100 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
101 static CRITICAL_SECTION aot_mutex;
102
103 /*
104  * Disabling this will make a copy of the loaded code and use the copy instead 
105  * of the original. This will place the caller and the callee close to each 
106  * other in memory, possibly improving cache behavior. Since the original
107  * code is in copy-on-write memory, this will not increase the memory usage
108  * of the runtime.
109  */
110 static gboolean use_loaded_code = TRUE;
111
112 /*
113  * Whenever to AOT compile loaded assemblies on demand and store them in
114  * a cache under $HOME/.mono/aot-cache.
115  */
116 static gboolean use_aot_cache = FALSE;
117
118 /*
119  * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
120  * use_aot_cache is TRUE.
121  */
122 static gboolean spawn_compiler = TRUE;
123
124 /* For debugging */
125 static gint32 mono_last_aot_method = -1;
126
127 static gboolean make_unreadable = FALSE;
128 static guint32 n_pagefaults = 0;
129 static guint32 name_table_accesses = 0;
130
131 /* Used to speed-up find_aot_module () */
132 static gsize aot_code_low_addr = (gssize)-1;
133 static gsize aot_code_high_addr = 0;
134
135 static gpointer
136 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info);
137
138 static void
139 init_plt (MonoAotModule *info);
140
141 static inline gboolean 
142 is_got_patch (MonoJumpInfoType patch_type)
143 {
144 #ifdef __x86_64__
145         return TRUE;
146 #elif defined(__i386__)
147         return TRUE;
148 #else
149         return FALSE;
150 #endif
151 }
152
153 /*****************************************************/
154 /*                 AOT RUNTIME                       */
155 /*****************************************************/
156
157 static MonoImage *
158 load_image (MonoAotModule *module, int index)
159 {
160         MonoAssembly *assembly;
161         MonoImageOpenStatus status;
162
163         g_assert (index < module->image_table_len);
164
165         if (module->image_table [index])
166                 return module->image_table [index];
167         if (module->out_of_date)
168                 return NULL;
169
170         assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
171         if (!assembly) {
172                 module->out_of_date = TRUE;
173                 return NULL;
174         }
175
176         if (strcmp (assembly->image->guid, module->image_guids [index])) {
177                 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);
178                 module->out_of_date = TRUE;
179                 return NULL;
180         }
181
182         module->image_table [index] = assembly->image;
183         return assembly->image;
184 }
185
186
187 static inline gint32
188 decode_value (guint8 *ptr, guint8 **rptr)
189 {
190         guint8 b = *ptr;
191         gint32 len;
192         
193         if ((b & 0x80) == 0){
194                 len = b;
195                 ++ptr;
196         } else if ((b & 0x40) == 0){
197                 len = ((b & 0x3f) << 8 | ptr [1]);
198                 ptr += 2;
199         } else if (b != 0xff) {
200                 len = ((b & 0x1f) << 24) |
201                         (ptr [1] << 16) |
202                         (ptr [2] << 8) |
203                         ptr [3];
204                 ptr += 4;
205         }
206         else {
207                 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
208                 ptr += 5;
209         }
210         if (rptr)
211                 *rptr = ptr;
212
213         //printf ("DECODE: %d.\n", len);
214         return len;
215 }
216
217 static MonoClass*
218 decode_klass_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
219 {
220         MonoImage *image;
221         MonoClass *klass;
222         guint32 token, rank, image_index;
223
224         token = decode_value (buf, &buf);
225         if (token == 0) {
226                 *endbuf = buf;
227                 return NULL;
228         }
229         image_index = decode_value (buf, &buf);
230         image = load_image (module, image_index);
231         if (!image)
232                 return NULL;
233         if (mono_metadata_token_code (token) == 0) {
234                 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
235         } else {
236                 token = MONO_TOKEN_TYPE_DEF + decode_value (buf, &buf);
237                 rank = decode_value (buf, &buf);
238                 klass = mono_class_get (image, token);
239                 g_assert (klass);
240                 klass = mono_array_class_get (klass, rank);
241         }
242         g_assert (klass);
243         mono_class_init (klass);
244
245         *endbuf = buf;
246         return klass;
247 }
248
249 static MonoClassField*
250 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
251 {
252         MonoClass *klass = decode_klass_info (module, buf, &buf);
253         guint32 token;
254
255         if (!klass)
256                 return NULL;
257
258         token = MONO_TOKEN_FIELD_DEF + decode_value (buf, &buf);
259
260         *endbuf = buf;
261
262         return mono_class_get_field (klass, token);
263 }
264
265 static inline MonoImage*
266 decode_method_ref (MonoAotModule *module, guint32 *token, guint8 *buf, guint8 **endbuf)
267 {
268         guint32 image_index, value;
269         MonoImage *image;
270
271         value = decode_value (buf, &buf);
272         *endbuf = buf;
273         image_index = value >> 24;
274         *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
275
276         image = load_image (module, image_index);
277         if (!image)
278                 return NULL;
279         else
280                 return image;
281 }
282
283 G_GNUC_UNUSED
284 static void
285 make_writable (guint8* addr, guint32 len)
286 {
287 #ifndef PLATFORM_WIN32
288         guint8 *page_start;
289         int pages, err;
290
291         page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
292         pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
293         err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
294         g_assert (err == 0);
295 #else
296         {
297                 DWORD oldp;
298                 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
299         }
300 #endif
301 }
302
303 static void
304 create_cache_structure (void)
305 {
306         const char *home;
307         char *tmp;
308         int err;
309
310         home = g_get_home_dir ();
311         if (!home)
312                 return;
313
314         tmp = g_build_filename (home, ".mono", NULL);
315         if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
316                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
317 #ifdef PLATFORM_WIN32
318                 err = mkdir (tmp);
319 #else
320                 err = mkdir (tmp, 0777);
321 #endif
322                 if (err) {
323                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
324                         g_free (tmp);
325                         return;
326                 }
327         }
328         g_free (tmp);
329         tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
330         if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
331                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
332 #ifdef PLATFORM_WIN32
333                 err = mkdir (tmp);
334 #else
335                 err = mkdir (tmp, 0777);
336 #endif
337                 if (err) {
338                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
339                         g_free (tmp);
340                         return;
341                 }
342         }
343         g_free (tmp);
344 }
345
346 /*
347  * load_aot_module_from_cache:
348  *
349  *  Experimental code to AOT compile loaded assemblies on demand. 
350  *
351  * FIXME: 
352  * - Add environment variable MONO_AOT_CACHE_OPTIONS
353  * - Add options for controlling the cache size
354  * - Handle full cache by deleting old assemblies lru style
355  * - Add options for excluding assemblies during development
356  * - Maybe add a threshold after an assembly is AOT compiled
357  * - invoking a new mono process is a security risk
358  * - recompile the AOT module if one of its dependencies changes
359  */
360 static GModule*
361 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
362 {
363         char *fname, *cmd, *tmp2, *aot_options;
364         const char *home;
365         GModule *module;
366         gboolean res;
367         gchar *out, *err;
368         gint exit_status;
369
370         *aot_name = NULL;
371
372         if (assembly->image->dynamic)
373                 return NULL;
374
375         create_cache_structure ();
376
377         home = g_get_home_dir ();
378
379         tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
380         fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
381         *aot_name = fname;
382         g_free (tmp2);
383
384         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
385         module = g_module_open (fname, G_MODULE_BIND_LAZY);     
386
387         if (!module) {
388                 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
389
390                 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
391
392                 aot_options = g_strdup_printf ("outfile=%s", fname);
393
394                 if (spawn_compiler) {
395                         /* FIXME: security */
396                         /* FIXME: Has to pass the assembly loading path to the child process */
397                         cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
398
399                         res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
400
401 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__powerpc__)
402                         if (res) {
403                                 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
404                                         mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
405                                 else
406                                         mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
407                                 g_free (out);
408                                 g_free (err);
409                         }
410 #endif
411                         g_free (cmd);
412                 } else {
413                         res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
414                         if (!res) {
415                                 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
416                         } else {
417                                 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
418                         }
419                 }
420
421                 module = g_module_open (fname, G_MODULE_BIND_LAZY);     
422
423                 g_free (aot_options);
424         }
425
426         return module;
427 }
428
429 static void
430 load_aot_module (MonoAssembly *assembly, gpointer user_data)
431 {
432         char *aot_name;
433         MonoAotModule *info;
434         gboolean usable = TRUE;
435         char *saved_guid = NULL;
436         char *aot_version = NULL;
437         char *opt_flags = NULL;
438         gpointer *plt_jump_table_addr = NULL;
439         guint32 *plt_jump_table_size = NULL;
440         gpointer *got_addr = NULL;
441         gpointer *got = NULL;
442         guint32 *got_size_ptr = NULL;
443
444         if (mono_compile_aot)
445                 return;
446
447         if (assembly->aot_module)
448                 /* 
449                  * Already loaded. This can happen because the assembly loading code might invoke
450                  * the assembly load hooks multiple times for the same assembly.
451                  */
452                 return;
453
454         if (use_aot_cache)
455                 assembly->aot_module = load_aot_module_from_cache (assembly, &aot_name);
456         else {
457                 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
458
459                 assembly->aot_module = g_module_open (aot_name, G_MODULE_BIND_LAZY);
460
461                 if (!assembly->aot_module) {
462                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, g_module_error ());
463                 }
464         }
465
466         if (!assembly->aot_module) {
467                 g_free (aot_name);
468                 return;
469         }
470
471         g_module_symbol (assembly->aot_module, "mono_assembly_guid", (gpointer *) &saved_guid);
472         g_module_symbol (assembly->aot_module, "mono_aot_version", (gpointer *) &aot_version);
473         g_module_symbol (assembly->aot_module, "mono_aot_opt_flags", (gpointer *)&opt_flags);
474
475         if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
476                 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);
477                 usable = FALSE;
478         }
479         else {
480                 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
481                         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
482                         usable = FALSE;
483                 }
484         }
485
486         if (!usable) {
487                 g_free (aot_name);
488                 g_module_close (assembly->aot_module);
489                 assembly->aot_module = NULL;
490                 return;
491         }
492
493         g_module_symbol (assembly->aot_module, "got_addr", (gpointer *)&got_addr);
494         g_assert (got_addr);
495         got = (gpointer*)*got_addr;
496         g_assert (got);
497         g_module_symbol (assembly->aot_module, "got_size", (gpointer *)&got_size_ptr);
498         g_assert (got_size_ptr);
499
500         info = g_new0 (MonoAotModule, 1);
501         info->aot_name = aot_name;
502         info->got = got;
503         info->got_size = *got_size_ptr;
504         info->got [0] = assembly->image;
505
506         sscanf (opt_flags, "%d", &info->opts);
507
508         /* Read image table */
509         {
510                 guint32 table_len, i;
511                 char *table = NULL;
512
513                 g_module_symbol (assembly->aot_module, "mono_image_table", (gpointer *)&table);
514                 g_assert (table);
515
516                 table_len = *(guint32*)table;
517                 table += sizeof (guint32);
518                 info->image_table = g_new0 (MonoImage*, table_len);
519                 info->image_names = g_new0 (MonoAssemblyName, table_len);
520                 info->image_guids = g_new0 (char*, table_len);
521                 info->image_table_len = table_len;
522                 for (i = 0; i < table_len; ++i) {
523                         MonoAssemblyName *aname = &(info->image_names [i]);
524
525                         aname->name = g_strdup (table);
526                         table += strlen (table) + 1;
527                         info->image_guids [i] = g_strdup (table);
528                         table += strlen (table) + 1;
529                         if (table [0] != 0)
530                                 aname->culture = g_strdup (table);
531                         table += strlen (table) + 1;
532                         memcpy (aname->public_key_token, table, strlen (table) + 1);
533                         table += strlen (table) + 1;                    
534
535                         table = ALIGN_PTR_TO (table, 8);
536                         aname->flags = *(guint32*)table;
537                         table += 4;
538                         aname->major = *(guint32*)table;
539                         table += 4;
540                         aname->minor = *(guint32*)table;
541                         table += 4;
542                         aname->build = *(guint32*)table;
543                         table += 4;
544                         aname->revision = *(guint32*)table;
545                         table += 4;
546                 }
547         }
548
549         /* Read method and method_info tables */
550         g_module_symbol (assembly->aot_module, "method_offsets", (gpointer*)&info->code_offsets);
551         g_module_symbol (assembly->aot_module, "methods", (gpointer*)&info->code);
552         g_module_symbol (assembly->aot_module, "methods_end", (gpointer*)&info->code_end);
553         g_module_symbol (assembly->aot_module, "method_info_offsets", (gpointer*)&info->method_info_offsets);
554         g_module_symbol (assembly->aot_module, "method_info", (gpointer*)&info->method_info);
555         g_module_symbol (assembly->aot_module, "ex_info_offsets", (gpointer*)&info->ex_info_offsets);
556         g_module_symbol (assembly->aot_module, "ex_info", (gpointer*)&info->ex_info);
557         g_module_symbol (assembly->aot_module, "method_order", (gpointer*)&info->method_order);
558         g_module_symbol (assembly->aot_module, "method_order_end", (gpointer*)&info->method_order_end);
559         g_module_symbol (assembly->aot_module, "class_info", (gpointer*)&info->class_info);
560         g_module_symbol (assembly->aot_module, "class_info_offsets", (gpointer*)&info->class_info_offsets);
561         g_module_symbol (assembly->aot_module, "class_name_table", (gpointer *)&info->class_name_table);
562         g_module_symbol (assembly->aot_module, "got_info", (gpointer*)&info->got_info);
563         g_module_symbol (assembly->aot_module, "got_info_offsets", (gpointer*)&info->got_info_offsets);
564         g_module_symbol (assembly->aot_module, "mem_end", (gpointer*)&info->mem_end);
565
566         info->mem_begin = info->code;
567
568         g_module_symbol (assembly->aot_module, "plt", (gpointer*)&info->plt);
569         g_module_symbol (assembly->aot_module, "plt_end", (gpointer*)&info->plt_end);
570         g_module_symbol (assembly->aot_module, "plt_info", (gpointer*)&info->plt_info);
571
572         g_module_symbol (assembly->aot_module, "plt_jump_table_addr", (gpointer *)&plt_jump_table_addr);
573         g_assert (plt_jump_table_addr);
574         info->plt_jump_table = (guint8*)*plt_jump_table_addr;
575         g_assert (info->plt_jump_table);
576
577         g_module_symbol (assembly->aot_module, "plt_jump_table_size", (gpointer *)&plt_jump_table_size);
578         g_assert (plt_jump_table_size);
579         info->plt_jump_table_size = *plt_jump_table_size;
580
581         if (make_unreadable) {
582 #ifndef PLATFORM_WIN32
583                 guint8 *addr;
584                 guint8 *page_start;
585                 int pages, err, len;
586
587                 addr = info->mem_begin;
588                 len = info->mem_end - info->mem_begin;
589
590                 /* Round down in both directions to avoid modifying data which is not ours */
591                 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
592                 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
593                 err = mprotect (page_start, pages * PAGESIZE, 0);
594                 g_assert (err == 0);
595 #endif
596         }
597
598         mono_aot_lock ();
599
600         aot_code_low_addr = MIN (aot_code_low_addr, (gsize)info->code);
601         aot_code_high_addr = MAX (aot_code_high_addr, (gsize)info->code_end);
602
603         g_hash_table_insert (aot_modules, assembly, info);
604         mono_aot_unlock ();
605
606         mono_jit_info_add_aot_module (assembly->image, info->code, info->code_end);
607
608         mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
609 }
610
611 void
612 mono_aot_init (void)
613 {
614         InitializeCriticalSection (&aot_mutex);
615         aot_modules = g_hash_table_new (NULL, NULL);
616
617         mono_install_assembly_load_hook (load_aot_module, NULL);
618
619         if (getenv ("MONO_LASTAOT"))
620                 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
621         if (getenv ("MONO_AOT_CACHE"))
622                 use_aot_cache = TRUE;
623 }
624
625 static gboolean
626 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
627 {
628         guint32 flags;
629
630         info->vtable_size = decode_value (buf, &buf);
631         flags = decode_value (buf, &buf);
632         info->ghcimpl = (flags >> 0) & 0x1;
633         info->has_finalize = (flags >> 1) & 0x1;
634         info->has_cctor = (flags >> 2) & 0x1;
635         info->has_nested_classes = (flags >> 3) & 0x1;
636         info->blittable = (flags >> 4) & 0x1;
637         info->has_references = (flags >> 5) & 0x1;
638         info->has_static_refs = (flags >> 6) & 0x1;
639         info->no_special_static_fields = (flags >> 7) & 0x1;
640
641         if (info->has_cctor) {
642                 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, buf, &buf);
643                 if (!cctor_image)
644                         return FALSE;
645         }
646         if (info->has_finalize) {
647                 info->finalize_image = decode_method_ref (module, &info->finalize_token, buf, &buf);
648                 if (!info->finalize_image)
649                         return FALSE;
650         }
651
652         info->instance_size = decode_value (buf, &buf);
653         info->class_size = decode_value (buf, &buf);
654         info->packing_size = decode_value (buf, &buf);
655         info->min_align = decode_value (buf, &buf);
656
657         *endbuf = buf;
658
659         return TRUE;
660 }       
661
662 gboolean
663 mono_aot_init_vtable (MonoVTable *vtable)
664 {
665         int i;
666         MonoAotModule *aot_module;
667         MonoClass *klass = vtable->klass;
668         guint8 *info, *p;
669         MonoCachedClassInfo class_info;
670         gboolean err;
671
672         if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !klass->image->assembly->aot_module)
673                 return FALSE;
674
675         mono_aot_lock ();
676
677         aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
678         if (!aot_module) {
679                 mono_aot_unlock ();
680                 return FALSE;
681         }
682
683         info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
684         p = info;
685
686         err = decode_cached_class_info (aot_module, &class_info, p, &p);
687         if (!err) {
688                 mono_aot_unlock ();
689                 return FALSE;
690         }
691
692         //printf ("VT0: %s.%s %d\n", klass->name_space, klass->name, vtable_size);
693         for (i = 0; i < class_info.vtable_size; ++i) {
694                 guint32 image_index, token, value;
695                 MonoImage *image;
696 #ifndef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
697                 MonoMethod *m;
698 #endif
699
700                 vtable->vtable [i] = 0;
701
702                 value = decode_value (p, &p);
703                 if (!value)
704                         continue;
705
706                 image_index = value >> 24;
707                 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
708
709                 image = load_image (aot_module, image_index);
710                 if (!image) {
711                         mono_aot_unlock ();
712                         return FALSE;
713                 }
714
715 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
716                 vtable->vtable [i] = mono_create_jit_trampoline_from_token (image, token);
717 #else
718                 m = mono_get_method (image, token, NULL);
719                 g_assert (m);
720
721                 //printf ("M: %d %p %s\n", i, &(vtable->vtable [i]), mono_method_full_name (m, TRUE));
722                 vtable->vtable [i] = mono_create_jit_trampoline (m);
723 #endif
724         }
725
726         mono_aot_unlock ();
727
728         return TRUE;
729 }
730
731 gboolean
732 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
733 {
734         MonoAotModule *aot_module;
735         guint8 *p;
736         gboolean err;
737
738         if (klass->rank || !klass->image->assembly->aot_module)
739                 return FALSE;
740
741         mono_aot_lock ();
742
743         aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
744         if (!aot_module) {
745                 mono_aot_unlock ();
746                 return FALSE;
747         }
748
749         p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
750
751         err = decode_cached_class_info (aot_module, res, p, &p);
752         if (!err) {
753                 mono_aot_unlock ();
754                 return FALSE;
755         }
756
757         mono_aot_unlock ();
758
759         return TRUE;
760 }
761
762 /**
763  * mono_aot_get_class_from_name:
764  *
765  *  Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
766  * using a cache stored in the AOT file.
767  * Stores the resulting class in *KLASS if found, stores NULL otherwise.
768  *
769  * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was 
770  * found.
771  */
772 gboolean
773 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
774 {
775         MonoAotModule *aot_module;
776         guint16 *table, *entry;
777         guint16 table_size;
778         guint32 hash;
779         char full_name_buf [1024];
780         char *full_name;
781         const char *name2, *name_space2;
782         MonoTableInfo  *t;
783         guint32 cols [MONO_TYPEDEF_SIZE];
784
785         if (!aot_modules)
786                 return FALSE;
787
788         mono_aot_lock ();
789
790         aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, image->assembly);
791         if (!aot_module || !aot_module->class_name_table) {
792                 mono_aot_unlock ();
793                 return FALSE;
794         }
795
796         *klass = NULL;
797
798         table_size = aot_module->class_name_table [0];
799         table = aot_module->class_name_table + 1;
800
801         if (name_space [0] == '\0')
802                 full_name = g_strdup_printf ("%s", name);
803         else {
804                 if (strlen (name_space) + strlen (name) < 1000) {
805                         sprintf (full_name_buf, "%s.%s", name_space, name);
806                         full_name = full_name_buf;
807                 } else {
808                         full_name = g_strdup_printf ("%s.%s", name_space, name);
809                 }
810         }
811         hash = g_str_hash (full_name) % table_size;
812         if (full_name != full_name_buf)
813                 g_free (full_name);
814
815         entry = &table [hash * 2];
816
817         if (entry [0] != 0) {
818                 t = &image->tables [MONO_TABLE_TYPEDEF];
819
820                 while (TRUE) {
821                         guint32 index = entry [0];
822                         guint32 next = entry [1];
823                         guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
824
825                         name_table_accesses ++;
826
827                         mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
828
829                         name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
830                         name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
831
832                         if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
833                                 mono_aot_unlock ();
834                                 *klass = mono_class_get (image, token);
835                                 return TRUE;
836                         }
837
838                         if (next != 0) {
839                                 entry = &table [next * 2];
840                         } else {
841                                 break;
842                         }
843                 }
844         }
845
846         mono_aot_unlock ();
847         
848         return TRUE;
849 }
850
851 static MonoJitInfo*
852 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain, 
853                                                          MonoMethod *method, guint8* ex_info, guint8 *code)
854 {
855         int i, buf_len;
856         MonoJitInfo *jinfo;
857         guint code_len, used_int_regs;
858         guint8 *p;
859         MonoMethodHeader *header;
860
861         header = mono_method_get_header (method);
862
863         /* Load the method info from the AOT file */
864
865         p = ex_info;
866         code_len = decode_value (p, &p);
867         used_int_regs = decode_value (p, &p);
868
869         /* Exception table */
870         if (header->num_clauses) {
871                 jinfo = 
872                         mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses));
873                 jinfo->num_clauses = header->num_clauses;
874
875                 for (i = 0; i < header->num_clauses; ++i) {
876                         MonoExceptionClause *ec = &header->clauses [i];                         
877                         MonoJitExceptionInfo *ei = &jinfo->clauses [i];
878
879                         ei->flags = ec->flags;
880                         ei->exvar_offset = decode_value (p, &p);
881
882                         if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
883                                 ei->data.filter = code + decode_value (p, &p);
884                         else
885                                 ei->data.catch_class = ec->data.catch_class;
886
887                         ei->try_start = code + decode_value (p, &p);
888                         ei->try_end = code + decode_value (p, &p);
889                         ei->handler_start = code + decode_value (p, &p);
890                 }
891         }
892         else
893                 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
894
895         jinfo->code_size = code_len;
896         jinfo->used_regs = used_int_regs;
897         jinfo->method = method;
898         jinfo->code_start = code;
899         jinfo->domain_neutral = 0;
900
901         /* Load debug info */
902         buf_len = decode_value (p, &p);
903         mono_debug_add_aot_method (domain, method, code, p, buf_len);
904         
905         return jinfo;
906 }
907
908 MonoJitInfo *
909 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
910 {
911         MonoAssembly *ass = image->assembly;
912         GModule *module = ass->aot_module;
913         int pos, left, right, offset, offset1, offset2, last_offset, new_offset, page_index, method_index, table_len;
914         guint32 token;
915         MonoAotModule *aot_module;
916         MonoMethod *method;
917         MonoJitInfo *jinfo;
918         guint8 *code, *ex_info;
919         guint32 *table, *ptr;
920         gboolean found;
921
922         if (!module)
923                 return NULL;
924
925         aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
926
927         if (domain != mono_get_root_domain ())
928                 /* FIXME: */
929                 return NULL;
930
931         offset = (guint8*)addr - aot_module->code;
932
933         /* First search through the index */
934         ptr = aot_module->method_order;
935         last_offset = 0;
936         page_index = 0;
937         found = FALSE;
938
939         if (*ptr == 0xffffff)
940                 return NULL;
941         ptr ++;
942
943         while (*ptr != 0xffffff) {
944                 guint32 method_index = ptr [0];
945                 new_offset = aot_module->code_offsets [method_index];
946
947                 if (offset >= last_offset && offset < new_offset) {
948                         found = TRUE;
949                         break;
950                 }
951
952                 ptr ++;
953                 last_offset = new_offset;
954                 page_index ++;
955         }
956
957         /* Skip rest of index */
958         while (*ptr != 0xffffff)
959                 ptr ++;
960         ptr ++;
961
962         table = ptr;
963         table_len = aot_module->method_order_end - table;
964
965         g_assert (table <= aot_module->method_order_end);
966
967         if (found) {
968                 left = (page_index * 1024);
969                 right = left + 1024;
970
971                 if (right > table_len)
972                         right = table_len;
973
974                 offset1 = aot_module->code_offsets [table [left]];
975                 g_assert (offset1 <= offset);
976
977                 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
978         }
979         else {
980                 //printf ("Not found in index: 0x%x\n", offset);
981                 left = 0;
982                 right = table_len;
983         }
984
985         /* Binary search inside the method_order table to find the method */
986         while (TRUE) {
987                 pos = (left + right) / 2;
988
989                 g_assert (table + pos <= aot_module->method_order_end);
990
991                 //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]]);
992
993                 offset1 = aot_module->code_offsets [table [pos]];
994                 if (table + pos + 1 >= aot_module->method_order_end)
995                         offset2 = aot_module->code_end - aot_module->code;
996                 else
997                         offset2 = aot_module->code_offsets [table [pos + 1]];
998
999                 if (offset < offset1)
1000                         right = pos;
1001                 else if (offset >= offset2)
1002                         left = pos + 1;
1003                 else
1004                         break;
1005         }
1006
1007         method_index = table [pos];
1008
1009         token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1010         method = mono_get_method (image, token, NULL);
1011
1012         /* FIXME: */
1013         g_assert (method);
1014
1015         //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1016
1017         code = &aot_module->code [aot_module->code_offsets [method_index]];
1018         ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1019
1020         jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1021
1022         g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1023         g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1024
1025         /* Add it to the normal JitInfo tables */
1026         mono_jit_info_table_add (domain, jinfo);
1027         
1028         return jinfo;
1029 }
1030
1031 static gboolean
1032 decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1033 {
1034         guint8 *p = buf;
1035         gpointer *table;
1036         MonoImage *image;
1037         int i;
1038
1039         switch (ji->type) {
1040         case MONO_PATCH_INFO_METHOD:
1041         case MONO_PATCH_INFO_METHODCONST:
1042         case MONO_PATCH_INFO_METHOD_JUMP: {
1043                 guint32 image_index, token, value;
1044
1045                 value = decode_value (p, &p);
1046                 image_index = value >> 24;
1047                 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1048
1049                 image = load_image (aot_module, image_index);
1050                 if (!image)
1051                         goto cleanup;
1052
1053 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1054                 if (ji->type == MONO_PATCH_INFO_METHOD) {
1055                         ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1056                         ji->type = MONO_PATCH_INFO_ABS;
1057                 }
1058                 else {
1059                         ji->data.method = mono_get_method (image, token, NULL);
1060                         g_assert (ji->data.method);
1061                         mono_class_init (ji->data.method->klass);
1062                 }
1063 #else
1064                 ji->data.method = mono_get_method (image, token, NULL);
1065                 g_assert (ji->data.method);
1066                 mono_class_init (ji->data.method->klass);
1067 #endif
1068
1069                 break;
1070         }
1071         case MONO_PATCH_INFO_WRAPPER: {
1072                 guint32 wrapper_type;
1073
1074                 wrapper_type = decode_value (p, &p);
1075
1076                 switch (wrapper_type) {
1077                 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
1078                         guint32 image_index, token, value;
1079
1080                         value = decode_value (p, &p);
1081                         image_index = value >> 24;
1082                         token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1083
1084                         image = load_image (aot_module, image_index);
1085                         if (!image)
1086                                 goto cleanup;
1087                         ji->data.method = mono_get_method (image, token, NULL);
1088                         g_assert (ji->data.method);
1089                         mono_class_init (ji->data.method->klass);
1090
1091                         ji->type = MONO_PATCH_INFO_METHOD;
1092                         ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
1093                         break;
1094                 }
1095                 case MONO_WRAPPER_PROXY_ISINST: {
1096                         MonoClass *klass = decode_klass_info (aot_module, p, &p);
1097                         if (!klass)
1098                                 goto cleanup;
1099                         ji->type = MONO_PATCH_INFO_METHOD;
1100                         ji->data.method = mono_marshal_get_proxy_cancast (klass);
1101                         break;
1102                 }
1103                 case MONO_WRAPPER_LDFLD:
1104                 case MONO_WRAPPER_LDFLDA:
1105                 case MONO_WRAPPER_STFLD:
1106                 case MONO_WRAPPER_LDFLD_REMOTE:
1107                 case MONO_WRAPPER_STFLD_REMOTE:
1108                 case MONO_WRAPPER_ISINST: {
1109                         MonoClass *klass = decode_klass_info (aot_module, p, &p);
1110                         if (!klass)
1111                                 goto cleanup;
1112                         ji->type = MONO_PATCH_INFO_METHOD;
1113                         if (wrapper_type == MONO_WRAPPER_LDFLD)
1114                                 ji->data.method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
1115                         else if (wrapper_type == MONO_WRAPPER_LDFLDA)
1116                                 ji->data.method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
1117                         else if (wrapper_type == MONO_WRAPPER_STFLD)
1118                                 ji->data.method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
1119                         else if (wrapper_type == MONO_WRAPPER_LDFLD_REMOTE)
1120                                 ji->data.method = mono_marshal_get_ldfld_remote_wrapper (klass);
1121                         else if (wrapper_type == MONO_WRAPPER_STFLD_REMOTE)
1122                                 ji->data.method = mono_marshal_get_stfld_remote_wrapper (klass);
1123                         else if (wrapper_type == MONO_WRAPPER_ISINST)
1124                                 ji->data.method = mono_marshal_get_isinst (klass);
1125                         else
1126                                 g_assert_not_reached ();
1127                         break;
1128                 }
1129                 case MONO_WRAPPER_STELEMREF:
1130                         ji->type = MONO_PATCH_INFO_METHOD;
1131                         ji->data.method = mono_marshal_get_stelemref ();
1132                         break;
1133                 default:
1134                         g_assert_not_reached ();
1135                 }
1136                 break;
1137         }
1138         case MONO_PATCH_INFO_INTERNAL_METHOD: {
1139                 guint32 len = decode_value (p, &p);
1140
1141                 ji->data.name = (char*)p;
1142                 p += len + 1;
1143                 break;
1144         }
1145         case MONO_PATCH_INFO_VTABLE:
1146         case MONO_PATCH_INFO_CLASS:
1147         case MONO_PATCH_INFO_IID:
1148         case MONO_PATCH_INFO_ADJUSTED_IID:
1149                 *got_offset = decode_value (p, &p);
1150
1151                 if (aot_module->got [*got_offset]) {
1152                         /* Already loaded */
1153                         //printf ("HIT!\n");
1154                 } else {
1155                         guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1156                         ji->data.klass = decode_klass_info (aot_module, tmp, &tmp);
1157                         if (!ji->data.klass)
1158                                 goto cleanup;
1159                 }
1160                 break;
1161         case MONO_PATCH_INFO_CLASS_INIT:
1162                 ji->data.klass = decode_klass_info (aot_module, p, &p);
1163                 if (!ji->data.klass)
1164                         goto cleanup;
1165                 break;
1166         case MONO_PATCH_INFO_IMAGE:
1167                 ji->data.image = load_image (aot_module, decode_value (p, &p));
1168                 if (!ji->data.image)
1169                         goto cleanup;
1170                 break;
1171         case MONO_PATCH_INFO_FIELD:
1172         case MONO_PATCH_INFO_SFLDA:
1173                 *got_offset = decode_value (p, &p);
1174
1175                 if (aot_module->got [*got_offset]) {
1176                         /* Already loaded */
1177                         //printf ("HIT2!\n");
1178                 } else {
1179                         guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1180                         ji->data.field = decode_field_info (aot_module, tmp, &tmp);
1181                         if (!ji->data.field)
1182                                 goto cleanup;
1183                 }
1184                 break;
1185         case MONO_PATCH_INFO_SWITCH:
1186                 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1187                 ji->data.table->table_size = decode_value (p, &p);
1188                 table = g_new (gpointer, ji->data.table->table_size);
1189                 ji->data.table->table = (MonoBasicBlock**)table;
1190                 for (i = 0; i < ji->data.table->table_size; i++)
1191                         table [i] = (gpointer)(gssize)decode_value (p, &p);
1192                 break;
1193         case MONO_PATCH_INFO_R4: {
1194                 guint32 val;
1195                 
1196                 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1197                 val = decode_value (p, &p);
1198                 *(float*)ji->data.target = *(float*)&val;
1199                 break;
1200         }
1201         case MONO_PATCH_INFO_R8: {
1202                 guint32 val [2];
1203
1204                 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1205
1206                 val [0] = decode_value (p, &p);
1207                 val [1] = decode_value (p, &p);
1208                 *(double*)ji->data.target = *(double*)val;
1209                 break;
1210         }
1211         case MONO_PATCH_INFO_LDSTR:
1212                 image = load_image (aot_module, decode_value (p, &p));
1213                 if (!image)
1214                         goto cleanup;
1215                 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1216                 break;
1217         case MONO_PATCH_INFO_DECLSEC:
1218         case MONO_PATCH_INFO_LDTOKEN:
1219         case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1220                 *got_offset = decode_value (p, &p);
1221
1222                 if (aot_module->got [*got_offset]) {
1223                         /* Already loaded */
1224                 } else {
1225                         guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1226                         image = load_image (aot_module, decode_value (tmp, &tmp));
1227                         if (!image)
1228                                 goto cleanup;
1229                         ji->data.token = mono_jump_info_token_new (mp, image, decode_value (tmp, &tmp));
1230                 }
1231                 break;
1232         case MONO_PATCH_INFO_EXC_NAME:
1233                 ji->data.klass = decode_klass_info (aot_module, p, &p);
1234                 if (!ji->data.klass)
1235                         goto cleanup;
1236                 ji->data.name = ji->data.klass->name;
1237                 break;
1238         case MONO_PATCH_INFO_METHOD_REL:
1239                 ji->data.offset = decode_value (p, &p);
1240                 break;
1241         default:
1242                 g_warning ("unhandled type %d", ji->type);
1243                 g_assert_not_reached ();
1244         }
1245
1246         *endbuf = p;
1247
1248         return TRUE;
1249
1250  cleanup:
1251         return FALSE;
1252 }
1253
1254 static MonoJumpInfo*
1255 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches, 
1256                                  guint32 got_index, guint32 **got_slots, 
1257                                  guint8 *buf, guint8 **endbuf)
1258 {
1259         MonoJumpInfo *patches;
1260         MonoJumpInfo *patch_info = NULL;
1261         int pindex;
1262         guint32 last_offset;
1263         guint8 *p;
1264
1265         p = buf;
1266
1267         /* First load the type + offset table */
1268         last_offset = 0;
1269         patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1270
1271         for (pindex = 0; pindex < n_patches; ++pindex) {                
1272                 MonoJumpInfo *ji = &patches [pindex];
1273
1274                 ji->type = *p;
1275                 p ++;
1276
1277                 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1278                 ji->next = patch_info;
1279                 patch_info = ji;
1280         }
1281
1282         *got_slots = g_malloc (sizeof (guint32) * n_patches);
1283         memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1284
1285         /* Then load the other data */
1286         for (pindex = 0; pindex < n_patches; ++pindex) {
1287                 MonoJumpInfo *ji = &patches [pindex];
1288
1289                 if (!decode_patch_info (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1290                         goto cleanup;
1291
1292                 if ((*got_slots) [pindex] == 0xffffffff)
1293                         (*got_slots) [pindex] = got_index ++;
1294         }
1295
1296         *endbuf = p;
1297         return patches;
1298
1299  cleanup:
1300         g_free (*got_slots);
1301         *got_slots = NULL;
1302
1303         return NULL;
1304 }
1305  
1306 static gpointer
1307 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
1308 {
1309         MonoClass *klass = method->klass;
1310         MonoAssembly *ass = klass->image->assembly;
1311         GModule *module = ass->aot_module;
1312         guint32 method_index = mono_metadata_token_index (method->token) - 1;
1313         guint8 *code, *info;
1314         MonoAotModule *aot_module;
1315
1316         if (!module)
1317                 return NULL;
1318
1319         if (!method->token)
1320                 return NULL;
1321
1322         if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1323                 return NULL;
1324
1325         if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1326                 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1327                 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1328                 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1329                 return NULL;
1330         
1331         aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1332
1333         g_assert (klass->inited);
1334
1335         if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1336                 /* Non shared AOT code can't be used in other appdomains */
1337                 return NULL;
1338
1339         if (aot_module->out_of_date)
1340                 return NULL;
1341
1342         if (aot_module->code_offsets [method_index] == 0xffffffff) {
1343                 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1344                         char *full_name = mono_method_full_name (method, TRUE);
1345                         mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1346                         g_free (full_name);
1347                 }
1348                 return NULL;
1349         }
1350
1351         code = &aot_module->code [aot_module->code_offsets [method_index]];
1352         info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1353
1354         if (!aot_module->methods_loaded)
1355                 aot_module->methods_loaded = g_new0 (guint32, klass->image->tables [MONO_TABLE_METHOD].rows + 1);
1356
1357         if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1358                 return code;
1359
1360         if (mono_last_aot_method != -1) {
1361                 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1362                                 return NULL;
1363                 else
1364                         if (mono_jit_stats.methods_aot == mono_last_aot_method)
1365                                 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
1366         }
1367
1368         return mono_aot_load_method (domain, aot_module, method, code, info);
1369 }
1370
1371 static gpointer
1372 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info)
1373 {
1374         MonoClass *klass = method->klass;
1375         MonoJumpInfo *patch_info = NULL;
1376         MonoMemPool *mp;
1377         int i, pindex, got_index = 0, n_patches, used_strings;
1378         gboolean non_got_patches, keep_patches = TRUE;
1379         guint32 method_index = mono_metadata_token_index (method->token) - 1;
1380         guint8 *p, *ex_info;
1381         MonoJitInfo *jinfo = NULL;
1382
1383         p = info;
1384         decode_klass_info (aot_module, p, &p);
1385
1386         if (!use_loaded_code) {
1387                 guint8 *code2;
1388
1389                 if (!jinfo) {
1390                         ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1391                         jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1392                 }
1393
1394                 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1395                 memcpy (code2, code, jinfo->code_size);
1396                 mono_arch_flush_icache (code2, jinfo->code_size);
1397                 code = code2;
1398         }
1399
1400         if (aot_module->opts & MONO_OPT_SHARED)
1401                 used_strings = decode_value (p, &p);
1402         else
1403                 used_strings = 0;
1404
1405         for (i = 0; i < used_strings; i++) {
1406                 guint token = decode_value (p, &p);
1407                 mono_ldstr (mono_get_root_domain (), klass->image, mono_metadata_token_index (token));
1408         }
1409
1410         if (aot_module->opts & MONO_OPT_SHARED) 
1411                 keep_patches = FALSE;
1412
1413         n_patches = decode_value (p, &p);
1414
1415         keep_patches = FALSE;
1416
1417         if (n_patches) {
1418                 MonoJumpInfo *patches;
1419                 guint32 *got_slots;
1420
1421                 if (keep_patches)
1422                         mp = domain->mp;
1423                 else
1424                         mp = mono_mempool_new ();
1425
1426                 got_index = decode_value (p, &p);
1427
1428                 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1429                 if (patches == NULL)
1430                         goto cleanup;
1431
1432                 /* Do this outside the lock to avoid deadlocks */
1433                 mono_aot_unlock ();
1434                 non_got_patches = FALSE;
1435                 for (pindex = 0; pindex < n_patches; ++pindex) {
1436                         MonoJumpInfo *ji = &patches [pindex];
1437
1438                         if (is_got_patch (ji->type)) {
1439                                 if (!aot_module->got [got_slots [pindex]])
1440                                         aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
1441                                 ji->type = MONO_PATCH_INFO_NONE;
1442                         }
1443                         else
1444                                 non_got_patches = TRUE;
1445                 }
1446                 if (non_got_patches) {
1447                         if (!jinfo) {
1448                                 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1449                                 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1450                         }
1451
1452                         mono_arch_flush_icache (code, jinfo->code_size);
1453                         make_writable (code, jinfo->code_size);
1454                         mono_arch_patch_code (method, domain, code, patch_info, TRUE);
1455                 }
1456                 mono_aot_lock ();
1457
1458                 g_free (got_slots);
1459
1460                 if (!keep_patches)
1461                         mono_mempool_destroy (mp);
1462         }
1463
1464         mono_jit_stats.methods_aot++;
1465
1466         if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1467                 char *full_name = mono_method_full_name (method, TRUE);
1468
1469                 if (!jinfo) {
1470                         ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1471                         jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1472                 }
1473
1474                 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);
1475                 g_free (full_name);
1476         }
1477
1478         aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1479
1480         init_plt (aot_module);
1481
1482         return code;
1483
1484  cleanup:
1485         /* FIXME: The space in domain->mp is wasted */  
1486         if (aot_module->opts & MONO_OPT_SHARED)
1487                 /* No need to cache patches */
1488                 mono_mempool_destroy (mp);
1489
1490         if (jinfo)
1491                 g_free (jinfo);
1492
1493         return NULL;
1494 }
1495
1496 gpointer
1497 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1498 {
1499         gpointer code;
1500
1501         mono_aot_lock ();
1502         code = mono_aot_get_method_inner (domain, method);
1503         mono_aot_unlock ();
1504
1505         return code;
1506 }
1507
1508 static gpointer
1509 mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guint32 token, MonoClass **klass)
1510 {
1511         MonoAssembly *ass = image->assembly;
1512         MonoMemPool *mp;
1513         int i, method_index, pindex, got_index, n_patches, used_strings;
1514         gboolean keep_patches = TRUE;
1515         guint8 *p;
1516         GModule *module = ass->aot_module;
1517         guint8 *code = NULL;
1518         guint8 *info;
1519         MonoAotModule *aot_module;
1520
1521         *klass = NULL;
1522
1523         if (!module)
1524                 return NULL;
1525
1526         if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1527                 return NULL;
1528
1529         aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1530
1531         if (domain != mono_get_root_domain ())
1532                 return NULL;
1533
1534         if (aot_module->out_of_date)
1535                 return NULL;
1536
1537         if (aot_module->code_offsets [mono_metadata_token_index (token) - 1] == 0xffffffff) {
1538                 return NULL;
1539         }
1540
1541         method_index = mono_metadata_token_index (token) - 1;
1542         code = &aot_module->code [aot_module->code_offsets [method_index]];
1543         info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1544
1545         if (!aot_module->methods_loaded)
1546                 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1547
1548         if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1549                 return code;
1550
1551         if (mono_last_aot_method != -1) {
1552                 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1553                                 return NULL;
1554                 else
1555                         if (mono_jit_stats.methods_aot == mono_last_aot_method) {
1556                                 MonoMethod *method = mono_get_method (image, token, NULL);
1557                                 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1558                         }
1559         }
1560
1561         p = info;
1562         *klass = decode_klass_info (aot_module, p, &p);
1563
1564         if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1565                 MonoMethod *method = mono_get_method (image, token, NULL);
1566                 char *full_name = mono_method_full_name (method, TRUE);
1567                 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p %p\n", full_name, code, info);
1568                 g_free (full_name);
1569         }
1570
1571         if (aot_module->opts & MONO_OPT_SHARED)
1572                 used_strings = decode_value (p, &p);
1573         else
1574                 used_strings = 0;
1575
1576         for (i = 0; i < used_strings; i++) {
1577                 guint string_token = decode_value (p, &p);
1578                 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (string_token));
1579         }
1580
1581         if (aot_module->opts & MONO_OPT_SHARED) 
1582                 keep_patches = FALSE;
1583
1584         keep_patches = FALSE;
1585
1586         n_patches = decode_value (p, &p);
1587
1588         if (n_patches) {
1589                 MonoJumpInfo *patches;
1590                 guint32 *got_slots;
1591
1592                 if (keep_patches)
1593                         mp = domain->mp;
1594                 else
1595                         mp = mono_mempool_new ();
1596
1597                 got_index = decode_value (p, &p);
1598
1599                 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1600                 if (patches == NULL)
1601                         goto cleanup;
1602
1603                 /* Do this outside the lock to avoid deadlocks */
1604                 mono_aot_unlock ();
1605
1606                 for (pindex = 0; pindex < n_patches; ++pindex) {
1607                         MonoJumpInfo *ji = &patches [pindex];
1608
1609                         if (is_got_patch (ji->type)) {
1610                                 if (!aot_module->got [got_slots [pindex]])
1611                                         aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (NULL, domain, code, ji, TRUE);
1612                                 ji->type = MONO_PATCH_INFO_NONE;
1613                         }
1614                 }
1615
1616                 mono_aot_lock ();
1617
1618                 g_free (got_slots);
1619
1620                 if (!keep_patches)
1621                         mono_mempool_destroy (mp);
1622         }
1623
1624         mono_jit_stats.methods_aot++;
1625
1626         aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1627
1628         init_plt (aot_module);
1629
1630         return code;
1631
1632  cleanup:
1633         /* FIXME: The space in domain->mp is wasted */  
1634         if (aot_module->opts & MONO_OPT_SHARED)
1635                 /* No need to cache patches */
1636                 mono_mempool_destroy (mp);
1637
1638         return NULL;
1639 }
1640
1641 /**
1642  * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
1643  * method.
1644  */
1645 gpointer
1646 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1647 {
1648         gpointer res;   
1649         MonoClass *klass;
1650
1651         mono_aot_lock ();
1652         res = mono_aot_get_method_from_token_inner (domain, image, token, &klass);
1653         mono_aot_unlock ();
1654
1655         if (!res)
1656                 return NULL;
1657
1658         if (klass)
1659                 mono_runtime_class_init (mono_class_vtable (domain, klass));
1660
1661         return res;
1662 }
1663
1664 typedef struct {
1665         guint8 *addr;
1666         gboolean res;
1667 } IsGotEntryUserData;
1668
1669 static void
1670 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
1671 {
1672         IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
1673         MonoAotModule *aot_module = (MonoAotModule*)value;
1674
1675         if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
1676                 data->res = TRUE;
1677 }
1678
1679 gboolean
1680 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1681 {
1682         IsGotEntryUserData user_data;
1683
1684         if (!aot_modules)
1685                 return FALSE;
1686
1687         user_data.addr = addr;
1688         user_data.res = FALSE;
1689         mono_aot_lock ();
1690         g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
1691         mono_aot_unlock ();
1692         
1693         return user_data.res;
1694 }
1695
1696 typedef struct {
1697         guint8 *addr;
1698         MonoAotModule *module;
1699 } FindAotModuleUserData;
1700
1701 static void
1702 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
1703 {
1704         FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
1705         MonoAotModule *aot_module = (MonoAotModule*)value;
1706
1707         if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
1708                 data->module = aot_module;
1709 }
1710
1711 static inline MonoAotModule*
1712 find_aot_module (guint8 *code)
1713 {
1714         FindAotModuleUserData user_data;
1715
1716         if (!aot_modules)
1717                 return NULL;
1718
1719         /* Reading these need no locking */
1720         if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
1721                 return NULL;
1722
1723         user_data.addr = code;
1724         user_data.module = NULL;
1725                 
1726         mono_aot_lock ();
1727         g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
1728         mono_aot_unlock ();
1729         
1730         return user_data.module;
1731 }
1732
1733 /*
1734  * mono_aot_set_make_unreadable:
1735  *
1736  *   Set whenever to make all mmaped memory unreadable. In conjuction with a
1737  * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
1738  */
1739 void
1740 mono_aot_set_make_unreadable (gboolean unreadable)
1741 {
1742         make_unreadable = unreadable;
1743 }
1744
1745 typedef struct {
1746         MonoAotModule *module;
1747         guint8 *ptr;
1748 } FindMapUserData;
1749
1750 static void
1751 find_map (gpointer key, gpointer value, gpointer user_data)
1752 {
1753         MonoAotModule *module = (MonoAotModule*)value;
1754         FindMapUserData *data = (FindMapUserData*)user_data;
1755
1756         if (!data->module)
1757                 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
1758                         data->module = module;
1759 }
1760
1761 static MonoAotModule*
1762 find_module_for_addr (void *ptr)
1763 {
1764         FindMapUserData data;
1765
1766         if (!make_unreadable)
1767                 return NULL;
1768
1769         data.module = NULL;
1770         data.ptr = (guint8*)ptr;
1771
1772         mono_aot_lock ();
1773         g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
1774         mono_aot_unlock ();
1775
1776         return data.module;
1777 }
1778
1779 /*
1780  * mono_aot_is_pagefault:
1781  *
1782  *   Should be called from a SIGSEGV signal handler to find out whenever @ptr is
1783  * within memory allocated by this module.
1784  */
1785 gboolean
1786 mono_aot_is_pagefault (void *ptr)
1787 {
1788         if (!make_unreadable)
1789                 return FALSE;
1790
1791         return find_module_for_addr (ptr) != NULL;
1792 }
1793
1794 /*
1795  * mono_aot_handle_pagefault:
1796  *
1797  *   Handle a pagefault caused by an unreadable page by making it readable again.
1798  */
1799 void
1800 mono_aot_handle_pagefault (void *ptr)
1801 {
1802 #ifndef PLATFORM_WIN32
1803         guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
1804         int res;
1805
1806         mono_aot_lock ();
1807         res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
1808         g_assert (res == 0);
1809
1810         n_pagefaults ++;
1811         mono_aot_unlock ();
1812
1813 #if 0
1814  {
1815         void *array [256];
1816         char **names;
1817         int i, size;
1818
1819         printf ("\nNative stacktrace:\n\n");
1820
1821         size = backtrace (array, 256);
1822         names = backtrace_symbols (array, size);
1823         for (i =0; i < size; ++i) {
1824                 printf ("\t%s\n", names [i]);
1825         }
1826         free (names);
1827  }
1828 #endif
1829
1830 #endif
1831 }
1832
1833 /*
1834  * aot_dyn_resolve:
1835  *
1836  *   This function is called by the entries in the PLT to resolve the actual method that
1837  * needs to be called. It returns a trampoline to the method and patches the PLT entry.
1838  */
1839 gpointer
1840 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
1841 {
1842 #ifdef MONO_ARCH_HAVE_PIC_AOT
1843         guint8 *p, *target, *plt_entry;
1844         MonoJumpInfo ji;
1845         MonoAotModule *module = (MonoAotModule*)aot_module;
1846         gboolean res;
1847
1848         //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
1849
1850         p = &module->plt_info [plt_info_offset];
1851
1852         ji.type = decode_value (p, &p);
1853
1854         res = decode_patch_info (module, NULL, &ji, p, &p, NULL);
1855         // FIXME: Error handling (how ?)
1856         g_assert (res);
1857
1858         target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
1859
1860         /* Patch the PLT entry with target which might be the actual method not a trampoline */
1861         plt_entry = mono_aot_get_plt_entry (code);
1862         g_assert (plt_entry);
1863         mono_arch_patch_plt_entry (plt_entry, target);
1864
1865         return target;
1866 #else
1867         g_assert_not_reached ();
1868         return NULL;
1869 #endif
1870 }
1871
1872 /**
1873  * init_plt:
1874  *
1875  *   Initialize the PLT table of the AOT module. Called lazily when the first AOT
1876  * method in the module is loaded to avoid committing memory by writing to it.
1877  * LOCKING: Assumes the AOT lock is held.
1878  */
1879 static void
1880 init_plt (MonoAotModule *info)
1881 {
1882 #ifdef MONO_ARCH_HAVE_PIC_AOT
1883 #ifdef __i386__
1884         guint8 *buf = info->plt;
1885 #endif
1886 #if defined(__x86_64__)
1887         int i, n_entries;
1888 #endif
1889         gpointer tramp;
1890
1891         if (info->plt_inited)
1892                 return;
1893
1894         tramp = mono_arch_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
1895
1896 #ifdef __i386__
1897         /* Initialize the first PLT entry */
1898         make_writable (info->plt, info->plt_end - info->plt);
1899         x86_jump_code (buf, tramp);
1900 #elif defined(__x86_64__)
1901         /*
1902          * Initialize the entries in the plt_jump_table to point to the default targets.
1903          */
1904          n_entries = info->plt_jump_table_size / sizeof (gpointer);
1905
1906          /* The first entry points to the AOT trampoline */
1907          ((gpointer*)info->plt_jump_table)[0] = tramp;
1908          for (i = 1; i < n_entries; ++i)
1909                  /* Each PLT entry is 16 bytes long, the default entry begins at offset 6 */
1910                  ((gpointer*)info->plt_jump_table)[i] = info->plt + (i * 16) + 6;
1911 #else
1912         g_assert_not_reached ();
1913 #endif
1914
1915         info->plt_inited = TRUE;
1916 #endif
1917 }
1918
1919 /*
1920  * mono_aot_get_plt_entry:
1921  *
1922  *   Return the address of the PLT entry called by the code at CODE if exists.
1923  */
1924 guint8*
1925 mono_aot_get_plt_entry (guint8 *code)
1926 {
1927         MonoAotModule *aot_module = find_aot_module (code);
1928
1929         if (!aot_module)
1930                 return NULL;
1931
1932 #if defined(__i386__) || defined(__x86_64__)
1933         if (code [-5] == 0xe8) {
1934                 guint32 disp = *(guint32*)(code - 4);
1935                 guint8 *target = code + disp;
1936
1937                 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
1938                         return target;
1939         }
1940 #endif
1941
1942         return NULL;
1943 }
1944
1945 /*
1946  * mono_aot_get_n_pagefaults:
1947  *
1948  *   Return the number of times handle_pagefault is called.
1949  */
1950 guint32
1951 mono_aot_get_n_pagefaults (void)
1952 {
1953         return n_pagefaults;
1954 }
1955
1956 #else
1957 /* AOT disabled */
1958
1959 void
1960 mono_aot_init (void)
1961 {
1962 }
1963
1964 MonoJitInfo*
1965 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1966 {
1967         return NULL;
1968 }
1969
1970 gboolean
1971 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1972 {
1973         return FALSE;
1974 }
1975
1976 gboolean
1977 mono_aot_init_vtable (MonoVTable *vtable)
1978 {
1979         return FALSE;
1980 }
1981
1982 gboolean
1983 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1984 {
1985         return FALSE;
1986 }
1987
1988 gboolean
1989 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1990 {
1991         return FALSE;
1992 }
1993
1994 MonoJitInfo *
1995 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1996 {
1997         return NULL;
1998 }
1999
2000 gpointer
2001 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2002 {
2003         return NULL;
2004 }
2005
2006 gboolean
2007 mono_aot_is_pagefault (void *ptr)
2008 {
2009         return FALSE;
2010 }
2011
2012 void
2013 mono_aot_set_make_unreadable (gboolean unreadable)
2014 {
2015 }
2016
2017 guint32
2018 mono_aot_get_n_pagefaults (void)
2019 {
2020         return 0;
2021 }
2022
2023 void
2024 mono_aot_handle_pagefault (void *ptr)
2025 {
2026 }
2027
2028 guint8*
2029 mono_aot_get_plt_entry (guint8 *code)
2030 {
2031         return NULL;
2032 }
2033
2034 #endif