2 * aot.c: mono Ahead of Time compiler
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2002 Ximian, Inc.
12 #include <sys/types.h>
16 #ifndef PLATFORM_WIN32
25 #include <limits.h> /* for PAGESIZE */
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/class.h>
32 #include <mono/metadata/object.h>
33 #include <mono/metadata/tokentype.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/metadata/assembly.h>
37 #include <mono/metadata/metadata-internals.h>
38 #include <mono/metadata/marshal.h>
39 #include <mono/utils/mono-logger.h>
40 #include "mono/utils/mono-compiler.h"
47 #define SHARED_EXT ".dll"
48 #elif defined(__ppc__) && defined(__MACH__)
49 #define SHARED_EXT ".dylib"
51 #define SHARED_EXT ".so"
54 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
55 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
57 typedef struct MonoAotModule {
59 /* Optimization flags used to compile the module */
61 /* Pointer to the Global Offset Table */
64 MonoAssemblyName *image_names;
66 MonoImage **image_table;
67 guint32 image_table_len;
77 guint8 *plt_jump_table;
78 guint32 plt_jump_table_size;
79 guint32 *code_offsets;
81 guint32 *method_info_offsets;
83 guint32 *got_info_offsets;
85 guint32 *ex_info_offsets;
86 guint32 *method_order;
87 guint32 *method_order_end;
89 guint32 *class_info_offsets;
90 guint32 *methods_loaded;
91 guint32 *class_name_table;
94 static GHashTable *aot_modules;
95 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
96 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
97 static CRITICAL_SECTION aot_mutex;
100 * Disabling this will make a copy of the loaded code and use the copy instead
101 * of the original. This will place the caller and the callee close to each
102 * other in memory, possibly improving cache behavior. Since the original
103 * code is in copy-on-write memory, this will not increase the memory usage
106 static gboolean use_loaded_code = TRUE;
109 * Whenever to AOT compile loaded assemblies on demand and store them in
110 * a cache under $HOME/.mono/aot-cache.
112 static gboolean use_aot_cache = FALSE;
115 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
116 * use_aot_cache is TRUE.
118 static gboolean spawn_compiler = TRUE;
121 static gint32 mono_last_aot_method = -1;
123 static gboolean make_unreadable = FALSE;
124 static guint32 n_pagefaults = 0;
125 static guint32 name_table_accesses = 0;
127 /* Used to speed-up find_aot_module () */
128 static gsize aot_code_low_addr = (gssize)-1;
129 static gsize aot_code_high_addr = 0;
132 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info, guint8 *ex_info);
135 init_plt (MonoAotModule *info);
138 is_got_patch (MonoJumpInfoType patch_type)
142 #elif defined(__i386__)
149 /*****************************************************/
151 /*****************************************************/
154 load_image (MonoAotModule *module, int index)
156 MonoAssembly *assembly;
157 MonoImageOpenStatus status;
159 g_assert (index < module->image_table_len);
161 if (module->image_table [index])
162 return module->image_table [index];
163 if (module->out_of_date)
166 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
168 module->out_of_date = TRUE;
172 if (strcmp (assembly->image->guid, module->image_guids [index])) {
173 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);
174 module->out_of_date = TRUE;
178 module->image_table [index] = assembly->image;
179 return assembly->image;
184 decode_value (guint8 *ptr, guint8 **rptr)
189 if ((b & 0x80) == 0){
192 } else if ((b & 0x40) == 0){
193 len = ((b & 0x3f) << 8 | ptr [1]);
195 } else if (b != 0xff) {
196 len = ((b & 0x1f) << 24) |
203 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
209 //printf ("DECODE: %d.\n", len);
214 decode_klass_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
218 guint32 token, rank, image_index;
220 token = decode_value (buf, &buf);
225 image_index = decode_value (buf, &buf);
226 image = load_image (module, image_index);
229 if (mono_metadata_token_code (token) == 0) {
230 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
232 token = MONO_TOKEN_TYPE_DEF + decode_value (buf, &buf);
233 rank = decode_value (buf, &buf);
234 klass = mono_class_get (image, token);
236 klass = mono_array_class_get (klass, rank);
239 mono_class_init (klass);
245 static MonoClassField*
246 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
248 MonoClass *klass = decode_klass_info (module, buf, &buf);
254 token = MONO_TOKEN_FIELD_DEF + decode_value (buf, &buf);
258 return mono_class_get_field (klass, token);
261 static inline MonoImage*
262 decode_method_ref (MonoAotModule *module, guint32 *token, guint8 *buf, guint8 **endbuf)
264 guint32 image_index, value;
267 value = decode_value (buf, &buf);
269 image_index = value >> 24;
270 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
272 image = load_image (module, image_index);
281 make_writable (guint8* addr, guint32 len)
283 #ifndef PLATFORM_WIN32
287 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
288 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
289 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
294 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
300 create_cache_structure (void)
306 home = g_get_home_dir ();
310 tmp = g_build_filename (home, ".mono", NULL);
311 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
312 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
313 #ifdef PLATFORM_WIN32
316 err = mkdir (tmp, 0777);
319 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
325 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
326 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
327 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
328 #ifdef PLATFORM_WIN32
331 err = mkdir (tmp, 0777);
334 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
343 * load_aot_module_from_cache:
345 * Experimental code to AOT compile loaded assemblies on demand.
348 * - Add environment variable MONO_AOT_CACHE_OPTIONS
349 * - Add options for controlling the cache size
350 * - Handle full cache by deleting old assemblies lru style
351 * - Add options for excluding assemblies during development
352 * - Maybe add a threshold after an assembly is AOT compiled
353 * - invoking a new mono process is a security risk
354 * - recompile the AOT module if one of its dependencies changes
357 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
359 char *fname, *cmd, *tmp2, *aot_options;
368 if (assembly->image->dynamic)
371 create_cache_structure ();
373 home = g_get_home_dir ();
375 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
376 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
380 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
381 module = g_module_open (fname, G_MODULE_BIND_LAZY);
384 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
386 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
388 aot_options = g_strdup_printf ("outfile=%s", fname);
390 if (spawn_compiler) {
391 /* FIXME: security */
392 /* FIXME: Has to pass the assembly loading path to the child process */
393 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
395 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
397 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__powerpc__)
399 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
400 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
402 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
409 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
411 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
413 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
417 module = g_module_open (fname, G_MODULE_BIND_LAZY);
419 g_free (aot_options);
426 load_aot_module (MonoAssembly *assembly, gpointer user_data)
430 gboolean usable = TRUE;
431 char *saved_guid = NULL;
432 char *aot_version = NULL;
433 char *opt_flags = NULL;
434 gpointer *plt_jump_table_addr = NULL;
435 guint32 *plt_jump_table_size = NULL;
436 gpointer *got_addr = NULL;
437 gpointer *got = NULL;
438 guint32 *got_size_ptr = NULL;
440 if (mono_compile_aot)
443 if (assembly->aot_module)
445 * Already loaded. This can happen because the assembly loading code might invoke
446 * the assembly load hooks multiple times for the same assembly.
451 assembly->aot_module = load_aot_module_from_cache (assembly, &aot_name);
453 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
455 assembly->aot_module = g_module_open (aot_name, G_MODULE_BIND_LAZY);
457 if (!assembly->aot_module) {
458 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, g_module_error ());
462 if (!assembly->aot_module) {
467 g_module_symbol (assembly->aot_module, "mono_assembly_guid", (gpointer *) &saved_guid);
468 g_module_symbol (assembly->aot_module, "mono_aot_version", (gpointer *) &aot_version);
469 g_module_symbol (assembly->aot_module, "mono_aot_opt_flags", (gpointer *)&opt_flags);
471 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
472 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);
476 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
477 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
484 g_module_close (assembly->aot_module);
485 assembly->aot_module = NULL;
489 g_module_symbol (assembly->aot_module, "got_addr", (gpointer *)&got_addr);
491 got = (gpointer*)*got_addr;
493 g_module_symbol (assembly->aot_module, "got_size", (gpointer *)&got_size_ptr);
494 g_assert (got_size_ptr);
496 info = g_new0 (MonoAotModule, 1);
497 info->aot_name = aot_name;
499 info->got_size = *got_size_ptr;
500 info->got [0] = assembly->image;
502 sscanf (opt_flags, "%d", &info->opts);
504 /* Read image table */
506 guint32 table_len, i;
509 g_module_symbol (assembly->aot_module, "mono_image_table", (gpointer *)&table);
512 table_len = *(guint32*)table;
513 table += sizeof (guint32);
514 info->image_table = g_new0 (MonoImage*, table_len);
515 info->image_names = g_new0 (MonoAssemblyName, table_len);
516 info->image_guids = g_new0 (char*, table_len);
517 info->image_table_len = table_len;
518 for (i = 0; i < table_len; ++i) {
519 MonoAssemblyName *aname = &(info->image_names [i]);
521 aname->name = g_strdup (table);
522 table += strlen (table) + 1;
523 info->image_guids [i] = g_strdup (table);
524 table += strlen (table) + 1;
526 aname->culture = g_strdup (table);
527 table += strlen (table) + 1;
528 memcpy (aname->public_key_token, table, strlen (table) + 1);
529 table += strlen (table) + 1;
531 table = ALIGN_PTR_TO (table, 8);
532 aname->flags = *(guint32*)table;
534 aname->major = *(guint32*)table;
536 aname->minor = *(guint32*)table;
538 aname->build = *(guint32*)table;
540 aname->revision = *(guint32*)table;
545 /* Read method and method_info tables */
546 g_module_symbol (assembly->aot_module, "method_offsets", (gpointer*)&info->code_offsets);
547 g_module_symbol (assembly->aot_module, "methods", (gpointer*)&info->code);
548 g_module_symbol (assembly->aot_module, "methods_end", (gpointer*)&info->code_end);
549 g_module_symbol (assembly->aot_module, "method_info_offsets", (gpointer*)&info->method_info_offsets);
550 g_module_symbol (assembly->aot_module, "method_info", (gpointer*)&info->method_info);
551 g_module_symbol (assembly->aot_module, "ex_info_offsets", (gpointer*)&info->ex_info_offsets);
552 g_module_symbol (assembly->aot_module, "ex_info", (gpointer*)&info->ex_info);
553 g_module_symbol (assembly->aot_module, "method_order", (gpointer*)&info->method_order);
554 g_module_symbol (assembly->aot_module, "method_order_end", (gpointer*)&info->method_order_end);
555 g_module_symbol (assembly->aot_module, "class_info", (gpointer*)&info->class_info);
556 g_module_symbol (assembly->aot_module, "class_info_offsets", (gpointer*)&info->class_info_offsets);
557 g_module_symbol (assembly->aot_module, "class_name_table", (gpointer *)&info->class_name_table);
558 g_module_symbol (assembly->aot_module, "got_info", (gpointer*)&info->got_info);
559 g_module_symbol (assembly->aot_module, "got_info_offsets", (gpointer*)&info->got_info_offsets);
560 g_module_symbol (assembly->aot_module, "mem_end", (gpointer*)&info->mem_end);
562 info->mem_begin = info->code;
564 g_module_symbol (assembly->aot_module, "plt", (gpointer*)&info->plt);
565 g_module_symbol (assembly->aot_module, "plt_end", (gpointer*)&info->plt_end);
566 g_module_symbol (assembly->aot_module, "plt_info", (gpointer*)&info->plt_info);
568 g_module_symbol (assembly->aot_module, "plt_jump_table_addr", (gpointer *)&plt_jump_table_addr);
569 g_assert (plt_jump_table_addr);
570 info->plt_jump_table = (guint8*)*plt_jump_table_addr;
571 g_assert (info->plt_jump_table);
573 g_module_symbol (assembly->aot_module, "plt_jump_table_size", (gpointer *)&plt_jump_table_size);
574 g_assert (plt_jump_table_size);
575 info->plt_jump_table_size = *plt_jump_table_size;
577 if (make_unreadable) {
578 #ifndef PLATFORM_WIN32
583 addr = info->mem_begin;
584 len = info->mem_end - info->mem_begin;
586 /* Round down in both directions to avoid modifying data which is not ours */
587 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
588 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
589 err = mprotect (page_start, pages * PAGESIZE, 0);
596 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)info->code);
597 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)info->code_end);
599 g_hash_table_insert (aot_modules, assembly, info);
602 mono_jit_info_add_aot_module (assembly->image, info->code, info->code_end);
604 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
610 InitializeCriticalSection (&aot_mutex);
611 aot_modules = g_hash_table_new (NULL, NULL);
613 mono_install_assembly_load_hook (load_aot_module, NULL);
615 if (getenv ("MONO_LASTAOT"))
616 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
617 if (getenv ("MONO_AOT_CACHE"))
618 use_aot_cache = TRUE;
622 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
626 info->vtable_size = decode_value (buf, &buf);
627 flags = decode_value (buf, &buf);
628 info->ghcimpl = (flags >> 0) & 0x1;
629 info->has_finalize = (flags >> 1) & 0x1;
630 info->has_cctor = (flags >> 2) & 0x1;
631 info->has_nested_classes = (flags >> 3) & 0x1;
632 info->blittable = (flags >> 4) & 0x1;
633 info->has_references = (flags >> 5) & 0x1;
634 info->has_static_refs = (flags >> 6) & 0x1;
635 info->no_special_static_fields = (flags >> 7) & 0x1;
637 if (info->has_cctor) {
638 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, buf, &buf);
642 if (info->has_finalize) {
643 info->finalize_image = decode_method_ref (module, &info->finalize_token, buf, &buf);
644 if (!info->finalize_image)
648 info->instance_size = decode_value (buf, &buf);
649 info->class_size = decode_value (buf, &buf);
650 info->packing_size = decode_value (buf, &buf);
651 info->min_align = decode_value (buf, &buf);
659 mono_aot_init_vtable (MonoVTable *vtable)
662 MonoAotModule *aot_module;
663 MonoClass *klass = vtable->klass;
665 MonoCachedClassInfo class_info;
668 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !klass->image->assembly->aot_module)
673 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
679 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
682 err = decode_cached_class_info (aot_module, &class_info, p, &p);
688 //printf ("VT0: %s.%s %d\n", klass->name_space, klass->name, vtable_size);
689 for (i = 0; i < class_info.vtable_size; ++i) {
690 guint32 image_index, token, value;
692 #ifndef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
696 vtable->vtable [i] = 0;
698 value = decode_value (p, &p);
702 image_index = value >> 24;
703 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
705 image = load_image (aot_module, image_index);
711 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
712 vtable->vtable [i] = mono_create_jit_trampoline_from_token (image, token);
714 m = mono_get_method (image, token, NULL);
717 //printf ("M: %d %p %s\n", i, &(vtable->vtable [i]), mono_method_full_name (m, TRUE));
718 vtable->vtable [i] = mono_create_jit_trampoline (m);
728 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
730 MonoAotModule *aot_module;
734 if (klass->rank || !klass->image->assembly->aot_module)
739 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
745 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
747 err = decode_cached_class_info (aot_module, res, p, &p);
759 * mono_aot_get_class_from_name:
761 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
762 * using a cache stored in the AOT file.
763 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
765 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
769 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
771 MonoAotModule *aot_module;
772 guint32 *table, *entry;
773 guint32 table_size, hash;
774 char full_name_buf [1024];
776 const char *name2, *name_space2;
778 guint32 cols [MONO_TYPEDEF_SIZE];
785 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, image->assembly);
786 if (!aot_module || !aot_module->class_name_table) {
793 table_size = aot_module->class_name_table [0];
794 table = aot_module->class_name_table + 1;
796 if (name_space [0] == '\0')
797 full_name = g_strdup_printf ("%s", name);
799 if (strlen (name_space) + strlen (name) < 1000) {
800 sprintf (full_name_buf, "%s.%s", name_space, name);
801 full_name = full_name_buf;
803 full_name = g_strdup_printf ("%s.%s", name_space, name);
806 hash = g_str_hash (full_name) % table_size;
807 if (full_name != full_name_buf)
810 entry = &table [hash * 2];
812 if (entry [0] != 0) {
813 t = &image->tables [MONO_TABLE_TYPEDEF];
816 guint32 token = entry [0];
817 guint32 next = entry [1];
818 guint32 index = mono_metadata_token_index (token);
820 name_table_accesses ++;
822 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
824 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
825 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
827 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
829 *klass = mono_class_get (image, token);
834 entry = &table [next * 2];
847 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
848 MonoMethod *method, guint8* ex_info, guint8 *code)
852 guint code_len, used_int_regs;
854 MonoMethodHeader *header;
856 header = mono_method_get_header (method);
858 /* Load the method info from the AOT file */
861 code_len = decode_value (p, &p);
862 used_int_regs = decode_value (p, &p);
864 /* Exception table */
865 if (header->num_clauses) {
867 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses));
868 jinfo->num_clauses = header->num_clauses;
870 for (i = 0; i < header->num_clauses; ++i) {
871 MonoExceptionClause *ec = &header->clauses [i];
872 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
874 ei->flags = ec->flags;
875 ei->exvar_offset = decode_value (p, &p);
877 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
878 ei->data.filter = code + decode_value (p, &p);
880 ei->data.catch_class = ec->data.catch_class;
882 ei->try_start = code + decode_value (p, &p);
883 ei->try_end = code + decode_value (p, &p);
884 ei->handler_start = code + decode_value (p, &p);
888 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
890 jinfo->code_size = code_len;
891 jinfo->used_regs = used_int_regs;
892 jinfo->method = method;
893 jinfo->code_start = code;
894 jinfo->domain_neutral = 0;
896 /* Load debug info */
897 buf_len = decode_value (p, &p);
898 mono_debug_add_aot_method (domain, method, code, p, buf_len);
904 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
906 MonoAssembly *ass = image->assembly;
907 GModule *module = ass->aot_module;
908 int pos, left, right, offset, offset1, offset2, last_offset, new_offset, page_index, method_index, table_len;
910 MonoAotModule *aot_module;
913 guint8 *code, *ex_info;
914 guint32 *table, *ptr;
920 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
922 if (domain != mono_get_root_domain ())
926 offset = (guint8*)addr - aot_module->code;
928 /* First search through the index */
929 ptr = aot_module->method_order;
934 if (*ptr == 0xffffff)
938 while (*ptr != 0xffffff) {
939 guint32 method_index = ptr [0];
940 new_offset = aot_module->code_offsets [method_index];
942 if (offset >= last_offset && offset < new_offset) {
948 last_offset = new_offset;
952 /* Skip rest of index */
953 while (*ptr != 0xffffff)
958 table_len = aot_module->method_order_end - table;
960 g_assert (table <= aot_module->method_order_end);
963 left = (page_index * 1024);
966 if (right > table_len)
969 offset1 = aot_module->code_offsets [table [left]];
970 g_assert (offset1 <= offset);
972 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
975 //printf ("Not found in index: 0x%x\n", offset);
980 /* Binary search inside the method_order table to find the method */
982 pos = (left + right) / 2;
984 g_assert (table + pos <= aot_module->method_order_end);
986 //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]]);
988 offset1 = aot_module->code_offsets [table [pos]];
989 if (table + pos + 1 >= aot_module->method_order_end)
990 offset2 = aot_module->code_end - aot_module->code;
992 offset2 = aot_module->code_offsets [table [pos + 1]];
994 if (offset < offset1)
996 else if (offset >= offset2)
1002 method_index = table [pos];
1004 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1005 method = mono_get_method (image, token, NULL);
1010 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1012 code = &aot_module->code [aot_module->code_offsets [method_index]];
1013 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1015 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1017 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1018 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1020 /* Add it to the normal JitInfo tables */
1021 mono_jit_info_table_add (domain, jinfo);
1027 decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1035 case MONO_PATCH_INFO_METHOD:
1036 case MONO_PATCH_INFO_METHODCONST:
1037 case MONO_PATCH_INFO_METHOD_JUMP: {
1038 guint32 image_index, token, value;
1040 value = decode_value (p, &p);
1041 image_index = value >> 24;
1042 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1044 image = load_image (aot_module, image_index);
1048 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1049 if (ji->type == MONO_PATCH_INFO_METHOD) {
1050 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1051 ji->type = MONO_PATCH_INFO_ABS;
1054 ji->data.method = mono_get_method (image, token, NULL);
1055 g_assert (ji->data.method);
1056 mono_class_init (ji->data.method->klass);
1059 ji->data.method = mono_get_method (image, token, NULL);
1060 g_assert (ji->data.method);
1061 mono_class_init (ji->data.method->klass);
1066 case MONO_PATCH_INFO_WRAPPER: {
1067 guint32 wrapper_type;
1069 wrapper_type = decode_value (p, &p);
1071 switch (wrapper_type) {
1072 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
1073 guint32 image_index, token, value;
1075 value = decode_value (p, &p);
1076 image_index = value >> 24;
1077 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1079 image = load_image (aot_module, image_index);
1082 ji->data.method = mono_get_method (image, token, NULL);
1083 g_assert (ji->data.method);
1084 mono_class_init (ji->data.method->klass);
1086 ji->type = MONO_PATCH_INFO_METHOD;
1087 ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
1090 case MONO_WRAPPER_PROXY_ISINST: {
1091 MonoClass *klass = decode_klass_info (aot_module, p, &p);
1094 ji->type = MONO_PATCH_INFO_METHOD;
1095 ji->data.method = mono_marshal_get_proxy_cancast (klass);
1098 case MONO_WRAPPER_LDFLD:
1099 case MONO_WRAPPER_LDFLDA:
1100 case MONO_WRAPPER_STFLD:
1101 case MONO_WRAPPER_LDFLD_REMOTE:
1102 case MONO_WRAPPER_STFLD_REMOTE:
1103 case MONO_WRAPPER_ISINST: {
1104 MonoClass *klass = decode_klass_info (aot_module, p, &p);
1107 ji->type = MONO_PATCH_INFO_METHOD;
1108 if (wrapper_type == MONO_WRAPPER_LDFLD)
1109 ji->data.method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
1110 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
1111 ji->data.method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
1112 else if (wrapper_type == MONO_WRAPPER_STFLD)
1113 ji->data.method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
1114 else if (wrapper_type == MONO_WRAPPER_LDFLD_REMOTE)
1115 ji->data.method = mono_marshal_get_ldfld_remote_wrapper (klass);
1116 else if (wrapper_type == MONO_WRAPPER_STFLD_REMOTE)
1117 ji->data.method = mono_marshal_get_stfld_remote_wrapper (klass);
1118 else if (wrapper_type == MONO_WRAPPER_ISINST)
1119 ji->data.method = mono_marshal_get_isinst (klass);
1121 g_assert_not_reached ();
1124 case MONO_WRAPPER_STELEMREF:
1125 ji->type = MONO_PATCH_INFO_METHOD;
1126 ji->data.method = mono_marshal_get_stelemref ();
1129 g_assert_not_reached ();
1133 case MONO_PATCH_INFO_INTERNAL_METHOD: {
1134 guint32 len = decode_value (p, &p);
1136 ji->data.name = (char*)p;
1140 case MONO_PATCH_INFO_VTABLE:
1141 case MONO_PATCH_INFO_CLASS:
1142 case MONO_PATCH_INFO_IID:
1143 case MONO_PATCH_INFO_ADJUSTED_IID:
1144 *got_offset = decode_value (p, &p);
1146 if (aot_module->got [*got_offset]) {
1147 /* Already loaded */
1148 //printf ("HIT!\n");
1150 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1151 ji->data.klass = decode_klass_info (aot_module, tmp, &tmp);
1152 if (!ji->data.klass)
1156 case MONO_PATCH_INFO_CLASS_INIT:
1157 ji->data.klass = decode_klass_info (aot_module, p, &p);
1158 if (!ji->data.klass)
1161 case MONO_PATCH_INFO_IMAGE:
1162 ji->data.image = load_image (aot_module, decode_value (p, &p));
1163 if (!ji->data.image)
1166 case MONO_PATCH_INFO_FIELD:
1167 case MONO_PATCH_INFO_SFLDA:
1168 *got_offset = decode_value (p, &p);
1170 if (aot_module->got [*got_offset]) {
1171 /* Already loaded */
1172 //printf ("HIT2!\n");
1174 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1175 ji->data.field = decode_field_info (aot_module, tmp, &tmp);
1176 if (!ji->data.field)
1180 case MONO_PATCH_INFO_SWITCH:
1181 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1182 ji->data.table->table_size = decode_value (p, &p);
1183 table = g_new (gpointer, ji->data.table->table_size);
1184 ji->data.table->table = (MonoBasicBlock**)table;
1185 for (i = 0; i < ji->data.table->table_size; i++)
1186 table [i] = (gpointer)(gssize)decode_value (p, &p);
1188 case MONO_PATCH_INFO_R4: {
1191 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1192 val = decode_value (p, &p);
1193 *(float*)ji->data.target = *(float*)&val;
1196 case MONO_PATCH_INFO_R8: {
1199 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1201 val [0] = decode_value (p, &p);
1202 val [1] = decode_value (p, &p);
1203 *(double*)ji->data.target = *(double*)val;
1206 case MONO_PATCH_INFO_LDSTR:
1207 image = load_image (aot_module, decode_value (p, &p));
1210 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1212 case MONO_PATCH_INFO_DECLSEC:
1213 case MONO_PATCH_INFO_LDTOKEN:
1214 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1215 *got_offset = decode_value (p, &p);
1217 if (aot_module->got [*got_offset]) {
1218 /* Already loaded */
1220 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1221 image = load_image (aot_module, decode_value (tmp, &tmp));
1224 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (tmp, &tmp));
1227 case MONO_PATCH_INFO_EXC_NAME:
1228 ji->data.klass = decode_klass_info (aot_module, p, &p);
1229 if (!ji->data.klass)
1231 ji->data.name = ji->data.klass->name;
1233 case MONO_PATCH_INFO_METHOD_REL:
1234 ji->data.offset = decode_value (p, &p);
1237 g_warning ("unhandled type %d", ji->type);
1238 g_assert_not_reached ();
1249 static MonoJumpInfo*
1250 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1251 guint32 got_index, guint32 **got_slots,
1252 guint8 *buf, guint8 **endbuf)
1254 MonoJumpInfo *patches;
1255 MonoJumpInfo *patch_info = NULL;
1257 guint32 last_offset;
1262 /* First load the type + offset table */
1264 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1266 for (pindex = 0; pindex < n_patches; ++pindex) {
1267 MonoJumpInfo *ji = &patches [pindex];
1272 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1273 ji->next = patch_info;
1277 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1278 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1280 /* Then load the other data */
1281 for (pindex = 0; pindex < n_patches; ++pindex) {
1282 MonoJumpInfo *ji = &patches [pindex];
1284 if (!decode_patch_info (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1287 if ((*got_slots) [pindex] == 0xffffffff)
1288 (*got_slots) [pindex] = got_index ++;
1295 g_free (*got_slots);
1301 static MonoJitInfo *
1302 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
1304 MonoClass *klass = method->klass;
1305 MonoAssembly *ass = klass->image->assembly;
1306 GModule *module = ass->aot_module;
1307 guint8 *code, *info, *ex_info;
1308 MonoAotModule *aot_module;
1316 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1319 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1320 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1321 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1322 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1325 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1327 g_assert (klass->inited);
1329 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1330 /* Non shared AOT code can't be used in other appdomains */
1333 if (aot_module->out_of_date)
1336 if (aot_module->code_offsets [mono_metadata_token_index (method->token) - 1] == 0xffffffff) {
1337 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1338 char *full_name = mono_method_full_name (method, TRUE);
1339 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1345 code = &aot_module->code [aot_module->code_offsets [mono_metadata_token_index (method->token) - 1]];
1346 info = &aot_module->method_info [aot_module->method_info_offsets [mono_metadata_token_index (method->token) - 1]];
1347 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1349 if (mono_last_aot_method != -1) {
1350 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1353 if (mono_jit_stats.methods_aot == mono_last_aot_method)
1354 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
1357 return mono_aot_load_method (domain, aot_module, method, code, info, ex_info);
1361 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info, guint8* ex_info)
1363 MonoClass *klass = method->klass;
1364 MonoJumpInfo *patch_info = NULL;
1367 int i, pindex, got_index = 0, n_patches, used_strings;
1368 gboolean non_got_patches, keep_patches = TRUE;
1371 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1374 decode_klass_info (aot_module, p, &p);
1376 if (!use_loaded_code) {
1378 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1379 memcpy (code2, code, jinfo->code_size);
1380 mono_arch_flush_icache (code2, jinfo->code_size);
1384 if (aot_module->opts & MONO_OPT_SHARED)
1385 used_strings = decode_value (p, &p);
1389 for (i = 0; i < used_strings; i++) {
1390 guint token = decode_value (p, &p);
1391 mono_ldstr (mono_get_root_domain (), klass->image, mono_metadata_token_index (token));
1394 if (aot_module->opts & MONO_OPT_SHARED)
1395 keep_patches = FALSE;
1397 n_patches = decode_value (p, &p);
1399 keep_patches = FALSE;
1402 MonoJumpInfo *patches;
1408 mp = mono_mempool_new ();
1410 got_index = decode_value (p, &p);
1412 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1413 if (patches == NULL)
1416 /* Do this outside the lock to avoid deadlocks */
1418 non_got_patches = FALSE;
1419 for (pindex = 0; pindex < n_patches; ++pindex) {
1420 MonoJumpInfo *ji = &patches [pindex];
1422 if (is_got_patch (ji->type)) {
1423 if (!aot_module->got [got_slots [pindex]])
1424 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
1425 ji->type = MONO_PATCH_INFO_NONE;
1428 non_got_patches = TRUE;
1430 if (non_got_patches) {
1431 mono_arch_flush_icache (code, jinfo->code_size);
1432 make_writable (code, jinfo->code_size);
1433 mono_arch_patch_code (method, domain, code, patch_info, TRUE);
1440 mono_mempool_destroy (mp);
1443 mono_jit_stats.methods_aot++;
1445 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1446 char *full_name = mono_method_full_name (method, TRUE);
1447 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);
1451 init_plt (aot_module);
1456 /* FIXME: The space in domain->mp is wasted */
1457 if (aot_module->opts & MONO_OPT_SHARED)
1458 /* No need to cache patches */
1459 mono_mempool_destroy (mp);
1465 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1470 info = mono_aot_get_method_inner (domain, method);
1473 /* Do this outside the lock */
1475 mono_jit_info_table_add (domain, info);
1483 mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guint32 token, MonoClass **klass)
1485 MonoAssembly *ass = image->assembly;
1487 int i, method_index, pindex, got_index, n_patches, used_strings;
1488 gboolean keep_patches = TRUE;
1490 GModule *module = ass->aot_module;
1491 guint8 *code = NULL;
1493 MonoAotModule *aot_module;
1500 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1503 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1505 if (domain != mono_get_root_domain ())
1508 if (aot_module->out_of_date)
1511 if (aot_module->code_offsets [mono_metadata_token_index (token) - 1] == 0xffffffff) {
1515 method_index = mono_metadata_token_index (token) - 1;
1516 code = &aot_module->code [aot_module->code_offsets [method_index]];
1517 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1519 if (!aot_module->methods_loaded)
1520 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1522 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1525 if (mono_last_aot_method != -1) {
1526 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1529 if (mono_jit_stats.methods_aot == mono_last_aot_method) {
1530 MonoMethod *method = mono_get_method (image, token, NULL);
1531 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1536 *klass = decode_klass_info (aot_module, p, &p);
1538 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1539 MonoMethod *method = mono_get_method (image, token, NULL);
1540 char *full_name = mono_method_full_name (method, TRUE);
1541 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p %p\n", full_name, code, info);
1545 if (aot_module->opts & MONO_OPT_SHARED)
1546 used_strings = decode_value (p, &p);
1550 for (i = 0; i < used_strings; i++) {
1551 guint string_token = decode_value (p, &p);
1552 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (string_token));
1555 if (aot_module->opts & MONO_OPT_SHARED)
1556 keep_patches = FALSE;
1558 keep_patches = FALSE;
1560 n_patches = decode_value (p, &p);
1563 MonoJumpInfo *patches;
1569 mp = mono_mempool_new ();
1571 got_index = decode_value (p, &p);
1573 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1574 if (patches == NULL)
1577 /* Do this outside the lock to avoid deadlocks */
1580 for (pindex = 0; pindex < n_patches; ++pindex) {
1581 MonoJumpInfo *ji = &patches [pindex];
1583 if (is_got_patch (ji->type)) {
1584 if (!aot_module->got [got_slots [pindex]])
1585 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (NULL, domain, code, ji, TRUE);
1586 ji->type = MONO_PATCH_INFO_NONE;
1595 mono_mempool_destroy (mp);
1598 mono_jit_stats.methods_aot++;
1600 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1602 init_plt (aot_module);
1607 /* FIXME: The space in domain->mp is wasted */
1608 if (aot_module->opts & MONO_OPT_SHARED)
1609 /* No need to cache patches */
1610 mono_mempool_destroy (mp);
1616 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
1620 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1626 res = mono_aot_get_method_from_token_inner (domain, image, token, &klass);
1633 mono_runtime_class_init (mono_class_vtable (domain, klass));
1641 } IsGotEntryUserData;
1644 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
1646 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
1647 MonoAotModule *aot_module = (MonoAotModule*)value;
1649 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
1654 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1656 IsGotEntryUserData user_data;
1661 user_data.addr = addr;
1662 user_data.res = FALSE;
1664 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
1667 return user_data.res;
1672 MonoAotModule *module;
1673 } FindAotModuleUserData;
1676 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
1678 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
1679 MonoAotModule *aot_module = (MonoAotModule*)value;
1681 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
1682 data->module = aot_module;
1685 static inline MonoAotModule*
1686 find_aot_module (guint8 *code)
1688 FindAotModuleUserData user_data;
1693 /* Reading these need no locking */
1694 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
1697 user_data.addr = code;
1698 user_data.module = NULL;
1701 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
1704 return user_data.module;
1708 * mono_aot_set_make_unreadable:
1710 * Set whenever to make all mmaped memory unreadable. In conjuction with a
1711 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
1714 mono_aot_set_make_unreadable (gboolean unreadable)
1716 make_unreadable = unreadable;
1720 MonoAotModule *module;
1725 find_map (gpointer key, gpointer value, gpointer user_data)
1727 MonoAotModule *module = (MonoAotModule*)value;
1728 FindMapUserData *data = (FindMapUserData*)user_data;
1731 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
1732 data->module = module;
1735 static MonoAotModule*
1736 find_module_for_addr (void *ptr)
1738 FindMapUserData data;
1740 if (!make_unreadable)
1744 data.ptr = (guint8*)ptr;
1747 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
1754 * mono_aot_is_pagefault:
1756 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
1757 * within memory allocated by this module.
1760 mono_aot_is_pagefault (void *ptr)
1762 if (!make_unreadable)
1765 return find_module_for_addr (ptr) != NULL;
1769 * mono_aot_handle_pagefault:
1771 * Handle a pagefault caused by an unreadable page by making it readable again.
1774 mono_aot_handle_pagefault (void *ptr)
1776 #ifndef PLATFORM_WIN32
1777 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
1781 res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
1782 g_assert (res == 0);
1792 * This function is called by the entries in the PLT to resolve the actual method that
1793 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
1796 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
1798 #ifdef MONO_ARCH_HAVE_PIC_AOT
1799 guint8 *p, *target, *plt_entry;
1801 MonoAotModule *module = (MonoAotModule*)aot_module;
1804 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
1806 p = &module->plt_info [plt_info_offset];
1808 ji.type = decode_value (p, &p);
1810 res = decode_patch_info (module, NULL, &ji, p, &p, NULL);
1811 // FIXME: Error handling (how ?)
1814 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
1816 /* Patch the PLT entry with target which might be the actual method not a trampoline */
1817 plt_entry = mono_aot_get_plt_entry (code);
1818 g_assert (plt_entry);
1819 mono_arch_patch_plt_entry (plt_entry, target);
1823 g_assert_not_reached ();
1831 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
1832 * method in the module is loaded to avoid committing memory by writing to it.
1833 * LOCKING: Assumes the AOT lock is held.
1836 init_plt (MonoAotModule *info)
1838 #ifdef MONO_ARCH_HAVE_PIC_AOT
1840 guint8 *buf = info->plt;
1842 #if defined(__x86_64__)
1847 if (info->plt_inited)
1850 tramp = mono_arch_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
1853 /* Initialize the first PLT entry */
1854 make_writable (info->plt, info->plt_end - info->plt);
1855 x86_jump_code (buf, tramp);
1856 #elif defined(__x86_64__)
1858 * Initialize the entries in the plt_jump_table to point to the default targets.
1860 n_entries = info->plt_jump_table_size / sizeof (gpointer);
1862 /* The first entry points to the AOT trampoline */
1863 ((gpointer*)info->plt_jump_table)[0] = tramp;
1864 for (i = 1; i < n_entries; ++i)
1865 /* Each PLT entry is 16 bytes long, the default entry begins at offset 6 */
1866 ((gpointer*)info->plt_jump_table)[i] = info->plt + (i * 16) + 6;
1868 g_assert_not_reached ();
1871 info->plt_inited = TRUE;
1876 * mono_aot_get_plt_entry:
1878 * Return the address of the PLT entry called by the code at CODE if exists.
1881 mono_aot_get_plt_entry (guint8 *code)
1883 MonoAotModule *aot_module = find_aot_module (code);
1888 #if defined(__i386__) || defined(__x86_64__)
1889 if (code [-5] == 0xe8) {
1890 guint32 disp = *(guint32*)(code - 4);
1891 guint8 *target = code + disp;
1893 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
1902 * mono_aot_get_n_pagefaults:
1904 * Return the number of times handle_pagefault is called.
1907 mono_aot_get_n_pagefaults (void)
1909 return n_pagefaults;
1916 mono_aot_init (void)
1921 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1927 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1933 mono_aot_init_vtable (MonoVTable *vtable)
1939 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1945 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1951 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1957 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1963 mono_aot_is_pagefault (void *ptr)
1969 mono_aot_set_make_unreadable (gboolean unreadable)
1974 mono_aot_get_n_pagefaults (void)
1980 mono_aot_handle_pagefault (void *ptr)
1985 mono_aot_get_plt_entry (guint8 *code)