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
23 #ifdef HAVE_EXECINFO_H
29 #include <limits.h> /* for PAGESIZE */
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"
51 #define SHARED_EXT ".dll"
52 #elif defined(__ppc__) && defined(__MACH__)
53 #define SHARED_EXT ".dylib"
55 #define SHARED_EXT ".so"
58 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
59 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
61 typedef struct MonoAotModule {
63 /* Optimization flags used to compile the module */
65 /* Pointer to the Global Offset Table */
68 MonoAssemblyName *image_names;
70 MonoImage **image_table;
71 guint32 image_table_len;
81 guint8 *plt_jump_table;
82 guint32 plt_jump_table_size;
83 guint32 *code_offsets;
85 guint32 *method_info_offsets;
87 guint32 *got_info_offsets;
89 guint32 *ex_info_offsets;
90 guint32 *method_order;
91 guint32 *method_order_end;
93 guint32 *class_info_offsets;
94 guint32 *methods_loaded;
95 guint16 *class_name_table;
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;
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
110 static gboolean use_loaded_code = TRUE;
113 * Whenever to AOT compile loaded assemblies on demand and store them in
114 * a cache under $HOME/.mono/aot-cache.
116 static gboolean use_aot_cache = FALSE;
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.
122 static gboolean spawn_compiler = TRUE;
125 static gint32 mono_last_aot_method = -1;
127 static gboolean make_unreadable = FALSE;
128 static guint32 n_pagefaults = 0;
129 static guint32 name_table_accesses = 0;
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;
136 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info);
139 init_plt (MonoAotModule *info);
141 static inline gboolean
142 is_got_patch (MonoJumpInfoType patch_type)
146 #elif defined(__i386__)
153 /*****************************************************/
155 /*****************************************************/
158 load_image (MonoAotModule *module, int index)
160 MonoAssembly *assembly;
161 MonoImageOpenStatus status;
163 g_assert (index < module->image_table_len);
165 if (module->image_table [index])
166 return module->image_table [index];
167 if (module->out_of_date)
170 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
172 module->out_of_date = TRUE;
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;
182 module->image_table [index] = assembly->image;
183 return assembly->image;
188 decode_value (guint8 *ptr, guint8 **rptr)
193 if ((b & 0x80) == 0){
196 } else if ((b & 0x40) == 0){
197 len = ((b & 0x3f) << 8 | ptr [1]);
199 } else if (b != 0xff) {
200 len = ((b & 0x1f) << 24) |
207 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
213 //printf ("DECODE: %d.\n", len);
218 decode_klass_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
222 guint32 token, rank, image_index;
224 token = decode_value (buf, &buf);
229 image_index = decode_value (buf, &buf);
230 image = load_image (module, image_index);
233 if (mono_metadata_token_code (token) == 0) {
234 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
236 token = MONO_TOKEN_TYPE_DEF + decode_value (buf, &buf);
237 rank = decode_value (buf, &buf);
238 klass = mono_class_get (image, token);
240 klass = mono_array_class_get (klass, rank);
243 mono_class_init (klass);
249 static MonoClassField*
250 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
252 MonoClass *klass = decode_klass_info (module, buf, &buf);
258 token = MONO_TOKEN_FIELD_DEF + decode_value (buf, &buf);
262 return mono_class_get_field (klass, token);
265 static inline MonoImage*
266 decode_method_ref (MonoAotModule *module, guint32 *token, guint8 *buf, guint8 **endbuf)
268 guint32 image_index, value;
271 value = decode_value (buf, &buf);
273 image_index = value >> 24;
274 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
276 image = load_image (module, image_index);
285 make_writable (guint8* addr, guint32 len)
287 #ifndef PLATFORM_WIN32
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);
298 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
304 create_cache_structure (void)
310 home = g_get_home_dir ();
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
320 err = mkdir (tmp, 0777);
323 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
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
335 err = mkdir (tmp, 0777);
338 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
347 * load_aot_module_from_cache:
349 * Experimental code to AOT compile loaded assemblies on demand.
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
361 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
363 char *fname, *cmd, *tmp2, *aot_options;
372 if (assembly->image->dynamic)
375 create_cache_structure ();
377 home = g_get_home_dir ();
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);
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);
388 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
390 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
392 aot_options = g_strdup_printf ("outfile=%s", fname);
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);
399 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
401 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__powerpc__)
403 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
404 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
406 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
413 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
415 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
417 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
421 module = g_module_open (fname, G_MODULE_BIND_LAZY);
423 g_free (aot_options);
430 load_aot_module (MonoAssembly *assembly, gpointer user_data)
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;
444 if (mono_compile_aot)
447 if (assembly->aot_module)
449 * Already loaded. This can happen because the assembly loading code might invoke
450 * the assembly load hooks multiple times for the same assembly.
455 assembly->aot_module = load_aot_module_from_cache (assembly, &aot_name);
457 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
459 assembly->aot_module = g_module_open (aot_name, G_MODULE_BIND_LAZY);
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 ());
466 if (!assembly->aot_module) {
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);
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);
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);
488 g_module_close (assembly->aot_module);
489 assembly->aot_module = NULL;
493 g_module_symbol (assembly->aot_module, "got_addr", (gpointer *)&got_addr);
495 got = (gpointer*)*got_addr;
497 g_module_symbol (assembly->aot_module, "got_size", (gpointer *)&got_size_ptr);
498 g_assert (got_size_ptr);
500 info = g_new0 (MonoAotModule, 1);
501 info->aot_name = aot_name;
503 info->got_size = *got_size_ptr;
504 info->got [0] = assembly->image;
506 sscanf (opt_flags, "%d", &info->opts);
508 /* Read image table */
510 guint32 table_len, i;
513 g_module_symbol (assembly->aot_module, "mono_image_table", (gpointer *)&table);
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]);
525 aname->name = g_strdup (table);
526 table += strlen (table) + 1;
527 info->image_guids [i] = g_strdup (table);
528 table += strlen (table) + 1;
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;
535 table = ALIGN_PTR_TO (table, 8);
536 aname->flags = *(guint32*)table;
538 aname->major = *(guint32*)table;
540 aname->minor = *(guint32*)table;
542 aname->build = *(guint32*)table;
544 aname->revision = *(guint32*)table;
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);
566 info->mem_begin = info->code;
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);
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);
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;
581 if (make_unreadable) {
582 #ifndef PLATFORM_WIN32
587 addr = info->mem_begin;
588 len = info->mem_end - info->mem_begin;
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);
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);
603 g_hash_table_insert (aot_modules, assembly, info);
606 mono_jit_info_add_aot_module (assembly->image, info->code, info->code_end);
608 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
614 InitializeCriticalSection (&aot_mutex);
615 aot_modules = g_hash_table_new (NULL, NULL);
617 mono_install_assembly_load_hook (load_aot_module, NULL);
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;
626 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
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;
641 if (info->has_cctor) {
642 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, buf, &buf);
646 if (info->has_finalize) {
647 info->finalize_image = decode_method_ref (module, &info->finalize_token, buf, &buf);
648 if (!info->finalize_image)
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);
663 mono_aot_init_vtable (MonoVTable *vtable)
666 MonoAotModule *aot_module;
667 MonoClass *klass = vtable->klass;
669 MonoCachedClassInfo class_info;
672 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !klass->image->assembly->aot_module)
677 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
683 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
686 err = decode_cached_class_info (aot_module, &class_info, p, &p);
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;
696 #ifndef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
700 vtable->vtable [i] = 0;
702 value = decode_value (p, &p);
706 image_index = value >> 24;
707 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
709 image = load_image (aot_module, image_index);
715 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
716 vtable->vtable [i] = mono_create_jit_trampoline_from_token (image, token);
718 m = mono_get_method (image, token, NULL);
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);
732 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
734 MonoAotModule *aot_module;
738 if (klass->rank || !klass->image->assembly->aot_module)
743 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
749 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
751 err = decode_cached_class_info (aot_module, res, p, &p);
763 * mono_aot_get_class_from_name:
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.
769 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
773 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
775 MonoAotModule *aot_module;
776 guint16 *table, *entry;
779 char full_name_buf [1024];
781 const char *name2, *name_space2;
783 guint32 cols [MONO_TYPEDEF_SIZE];
790 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, image->assembly);
791 if (!aot_module || !aot_module->class_name_table) {
798 table_size = aot_module->class_name_table [0];
799 table = aot_module->class_name_table + 1;
801 if (name_space [0] == '\0')
802 full_name = g_strdup_printf ("%s", name);
804 if (strlen (name_space) + strlen (name) < 1000) {
805 sprintf (full_name_buf, "%s.%s", name_space, name);
806 full_name = full_name_buf;
808 full_name = g_strdup_printf ("%s.%s", name_space, name);
811 hash = g_str_hash (full_name) % table_size;
812 if (full_name != full_name_buf)
815 entry = &table [hash * 2];
817 if (entry [0] != 0) {
818 t = &image->tables [MONO_TABLE_TYPEDEF];
821 guint32 index = entry [0];
822 guint32 next = entry [1];
823 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
825 name_table_accesses ++;
827 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
829 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
830 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
832 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
834 *klass = mono_class_get (image, token);
839 entry = &table [next * 2];
852 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
853 MonoMethod *method, guint8* ex_info, guint8 *code)
857 guint code_len, used_int_regs;
859 MonoMethodHeader *header;
861 header = mono_method_get_header (method);
863 /* Load the method info from the AOT file */
866 code_len = decode_value (p, &p);
867 used_int_regs = decode_value (p, &p);
869 /* Exception table */
870 if (header->num_clauses) {
872 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses));
873 jinfo->num_clauses = header->num_clauses;
875 for (i = 0; i < header->num_clauses; ++i) {
876 MonoExceptionClause *ec = &header->clauses [i];
877 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
879 ei->flags = ec->flags;
880 ei->exvar_offset = decode_value (p, &p);
882 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
883 ei->data.filter = code + decode_value (p, &p);
885 ei->data.catch_class = ec->data.catch_class;
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);
893 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
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;
901 /* Load debug info */
902 buf_len = decode_value (p, &p);
903 mono_debug_add_aot_method (domain, method, code, p, buf_len);
909 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
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;
915 MonoAotModule *aot_module;
918 guint8 *code, *ex_info;
919 guint32 *table, *ptr;
925 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
927 if (domain != mono_get_root_domain ())
931 offset = (guint8*)addr - aot_module->code;
933 /* First search through the index */
934 ptr = aot_module->method_order;
939 if (*ptr == 0xffffff)
943 while (*ptr != 0xffffff) {
944 guint32 method_index = ptr [0];
945 new_offset = aot_module->code_offsets [method_index];
947 if (offset >= last_offset && offset < new_offset) {
953 last_offset = new_offset;
957 /* Skip rest of index */
958 while (*ptr != 0xffffff)
963 table_len = aot_module->method_order_end - table;
965 g_assert (table <= aot_module->method_order_end);
968 left = (page_index * 1024);
971 if (right > table_len)
974 offset1 = aot_module->code_offsets [table [left]];
975 g_assert (offset1 <= offset);
977 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
980 //printf ("Not found in index: 0x%x\n", offset);
985 /* Binary search inside the method_order table to find the method */
987 pos = (left + right) / 2;
989 g_assert (table + pos <= aot_module->method_order_end);
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]]);
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;
997 offset2 = aot_module->code_offsets [table [pos + 1]];
999 if (offset < offset1)
1001 else if (offset >= offset2)
1007 method_index = table [pos];
1009 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1010 method = mono_get_method (image, token, NULL);
1015 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
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]];
1020 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1022 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1023 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1025 /* Add it to the normal JitInfo tables */
1026 mono_jit_info_table_add (domain, jinfo);
1032 decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
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;
1045 value = decode_value (p, &p);
1046 image_index = value >> 24;
1047 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1049 image = load_image (aot_module, image_index);
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;
1059 ji->data.method = mono_get_method (image, token, NULL);
1060 g_assert (ji->data.method);
1061 mono_class_init (ji->data.method->klass);
1064 ji->data.method = mono_get_method (image, token, NULL);
1065 g_assert (ji->data.method);
1066 mono_class_init (ji->data.method->klass);
1071 case MONO_PATCH_INFO_WRAPPER: {
1072 guint32 wrapper_type;
1074 wrapper_type = decode_value (p, &p);
1076 switch (wrapper_type) {
1077 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
1078 guint32 image_index, token, value;
1080 value = decode_value (p, &p);
1081 image_index = value >> 24;
1082 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1084 image = load_image (aot_module, image_index);
1087 ji->data.method = mono_get_method (image, token, NULL);
1088 g_assert (ji->data.method);
1089 mono_class_init (ji->data.method->klass);
1091 ji->type = MONO_PATCH_INFO_METHOD;
1092 ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
1095 case MONO_WRAPPER_PROXY_ISINST: {
1096 MonoClass *klass = decode_klass_info (aot_module, p, &p);
1099 ji->type = MONO_PATCH_INFO_METHOD;
1100 ji->data.method = mono_marshal_get_proxy_cancast (klass);
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);
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);
1126 g_assert_not_reached ();
1129 case MONO_WRAPPER_STELEMREF:
1130 ji->type = MONO_PATCH_INFO_METHOD;
1131 ji->data.method = mono_marshal_get_stelemref ();
1134 g_assert_not_reached ();
1138 case MONO_PATCH_INFO_INTERNAL_METHOD: {
1139 guint32 len = decode_value (p, &p);
1141 ji->data.name = (char*)p;
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);
1151 if (aot_module->got [*got_offset]) {
1152 /* Already loaded */
1153 //printf ("HIT!\n");
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)
1161 case MONO_PATCH_INFO_CLASS_INIT:
1162 ji->data.klass = decode_klass_info (aot_module, p, &p);
1163 if (!ji->data.klass)
1166 case MONO_PATCH_INFO_IMAGE:
1167 ji->data.image = load_image (aot_module, decode_value (p, &p));
1168 if (!ji->data.image)
1171 case MONO_PATCH_INFO_FIELD:
1172 case MONO_PATCH_INFO_SFLDA:
1173 *got_offset = decode_value (p, &p);
1175 if (aot_module->got [*got_offset]) {
1176 /* Already loaded */
1177 //printf ("HIT2!\n");
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)
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);
1193 case MONO_PATCH_INFO_R4: {
1196 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1197 val = decode_value (p, &p);
1198 *(float*)ji->data.target = *(float*)&val;
1201 case MONO_PATCH_INFO_R8: {
1204 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1206 val [0] = decode_value (p, &p);
1207 val [1] = decode_value (p, &p);
1208 *(double*)ji->data.target = *(double*)val;
1211 case MONO_PATCH_INFO_LDSTR:
1212 image = load_image (aot_module, decode_value (p, &p));
1215 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
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);
1222 if (aot_module->got [*got_offset]) {
1223 /* Already loaded */
1225 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1226 image = load_image (aot_module, decode_value (tmp, &tmp));
1229 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (tmp, &tmp));
1232 case MONO_PATCH_INFO_EXC_NAME:
1233 ji->data.klass = decode_klass_info (aot_module, p, &p);
1234 if (!ji->data.klass)
1236 ji->data.name = ji->data.klass->name;
1238 case MONO_PATCH_INFO_METHOD_REL:
1239 ji->data.offset = decode_value (p, &p);
1242 g_warning ("unhandled type %d", ji->type);
1243 g_assert_not_reached ();
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)
1259 MonoJumpInfo *patches;
1260 MonoJumpInfo *patch_info = NULL;
1262 guint32 last_offset;
1267 /* First load the type + offset table */
1269 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1271 for (pindex = 0; pindex < n_patches; ++pindex) {
1272 MonoJumpInfo *ji = &patches [pindex];
1277 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1278 ji->next = patch_info;
1282 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1283 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1285 /* Then load the other data */
1286 for (pindex = 0; pindex < n_patches; ++pindex) {
1287 MonoJumpInfo *ji = &patches [pindex];
1289 if (!decode_patch_info (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1292 if ((*got_slots) [pindex] == 0xffffffff)
1293 (*got_slots) [pindex] = got_index ++;
1300 g_free (*got_slots);
1307 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
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;
1322 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
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))
1331 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1333 g_assert (klass->inited);
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 */
1339 if (aot_module->out_of_date)
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);
1351 code = &aot_module->code [aot_module->code_offsets [method_index]];
1352 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1354 if (!aot_module->methods_loaded)
1355 aot_module->methods_loaded = g_new0 (guint32, klass->image->tables [MONO_TABLE_METHOD].rows + 1);
1357 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1360 if (mono_last_aot_method != -1) {
1361 if (mono_jit_stats.methods_aot > mono_last_aot_method)
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);
1368 return mono_aot_load_method (domain, aot_module, method, code, info);
1372 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info)
1374 MonoClass *klass = method->klass;
1375 MonoJumpInfo *patch_info = NULL;
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;
1384 decode_klass_info (aot_module, p, &p);
1386 if (!use_loaded_code) {
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);
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);
1400 if (aot_module->opts & MONO_OPT_SHARED)
1401 used_strings = decode_value (p, &p);
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));
1410 if (aot_module->opts & MONO_OPT_SHARED)
1411 keep_patches = FALSE;
1413 n_patches = decode_value (p, &p);
1415 keep_patches = FALSE;
1418 MonoJumpInfo *patches;
1424 mp = mono_mempool_new ();
1426 got_index = decode_value (p, &p);
1428 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1429 if (patches == NULL)
1432 /* Do this outside the lock to avoid deadlocks */
1434 non_got_patches = FALSE;
1435 for (pindex = 0; pindex < n_patches; ++pindex) {
1436 MonoJumpInfo *ji = &patches [pindex];
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;
1444 non_got_patches = TRUE;
1446 if (non_got_patches) {
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);
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);
1461 mono_mempool_destroy (mp);
1464 mono_jit_stats.methods_aot++;
1466 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1467 char *full_name = mono_method_full_name (method, TRUE);
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);
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);
1478 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1480 init_plt (aot_module);
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);
1497 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1502 code = mono_aot_get_method_inner (domain, method);
1509 mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guint32 token, MonoClass **klass)
1511 MonoAssembly *ass = image->assembly;
1513 int i, method_index, pindex, got_index, n_patches, used_strings;
1514 gboolean keep_patches = TRUE;
1516 GModule *module = ass->aot_module;
1517 guint8 *code = NULL;
1519 MonoAotModule *aot_module;
1526 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1529 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1531 if (domain != mono_get_root_domain ())
1534 if (aot_module->out_of_date)
1537 if (aot_module->code_offsets [mono_metadata_token_index (token) - 1] == 0xffffffff) {
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]];
1545 if (!aot_module->methods_loaded)
1546 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1548 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1551 if (mono_last_aot_method != -1) {
1552 if (mono_jit_stats.methods_aot > mono_last_aot_method)
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);
1562 *klass = decode_klass_info (aot_module, p, &p);
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);
1571 if (aot_module->opts & MONO_OPT_SHARED)
1572 used_strings = decode_value (p, &p);
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));
1581 if (aot_module->opts & MONO_OPT_SHARED)
1582 keep_patches = FALSE;
1584 keep_patches = FALSE;
1586 n_patches = decode_value (p, &p);
1589 MonoJumpInfo *patches;
1595 mp = mono_mempool_new ();
1597 got_index = decode_value (p, &p);
1599 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1600 if (patches == NULL)
1603 /* Do this outside the lock to avoid deadlocks */
1606 for (pindex = 0; pindex < n_patches; ++pindex) {
1607 MonoJumpInfo *ji = &patches [pindex];
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;
1621 mono_mempool_destroy (mp);
1624 mono_jit_stats.methods_aot++;
1626 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1628 init_plt (aot_module);
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);
1642 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
1646 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1652 res = mono_aot_get_method_from_token_inner (domain, image, token, &klass);
1659 mono_runtime_class_init (mono_class_vtable (domain, klass));
1667 } IsGotEntryUserData;
1670 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
1672 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
1673 MonoAotModule *aot_module = (MonoAotModule*)value;
1675 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
1680 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1682 IsGotEntryUserData user_data;
1687 user_data.addr = addr;
1688 user_data.res = FALSE;
1690 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
1693 return user_data.res;
1698 MonoAotModule *module;
1699 } FindAotModuleUserData;
1702 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
1704 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
1705 MonoAotModule *aot_module = (MonoAotModule*)value;
1707 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
1708 data->module = aot_module;
1711 static inline MonoAotModule*
1712 find_aot_module (guint8 *code)
1714 FindAotModuleUserData user_data;
1719 /* Reading these need no locking */
1720 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
1723 user_data.addr = code;
1724 user_data.module = NULL;
1727 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
1730 return user_data.module;
1734 * mono_aot_set_make_unreadable:
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.
1740 mono_aot_set_make_unreadable (gboolean unreadable)
1742 make_unreadable = unreadable;
1746 MonoAotModule *module;
1751 find_map (gpointer key, gpointer value, gpointer user_data)
1753 MonoAotModule *module = (MonoAotModule*)value;
1754 FindMapUserData *data = (FindMapUserData*)user_data;
1757 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
1758 data->module = module;
1761 static MonoAotModule*
1762 find_module_for_addr (void *ptr)
1764 FindMapUserData data;
1766 if (!make_unreadable)
1770 data.ptr = (guint8*)ptr;
1773 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
1780 * mono_aot_is_pagefault:
1782 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
1783 * within memory allocated by this module.
1786 mono_aot_is_pagefault (void *ptr)
1788 if (!make_unreadable)
1791 return find_module_for_addr (ptr) != NULL;
1795 * mono_aot_handle_pagefault:
1797 * Handle a pagefault caused by an unreadable page by making it readable again.
1800 mono_aot_handle_pagefault (void *ptr)
1802 #ifndef PLATFORM_WIN32
1803 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
1807 res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
1808 g_assert (res == 0);
1819 printf ("\nNative stacktrace:\n\n");
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]);
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.
1840 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
1842 #ifdef MONO_ARCH_HAVE_PIC_AOT
1843 guint8 *p, *target, *plt_entry;
1845 MonoAotModule *module = (MonoAotModule*)aot_module;
1848 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
1850 p = &module->plt_info [plt_info_offset];
1852 ji.type = decode_value (p, &p);
1854 res = decode_patch_info (module, NULL, &ji, p, &p, NULL);
1855 // FIXME: Error handling (how ?)
1858 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
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);
1867 g_assert_not_reached ();
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.
1880 init_plt (MonoAotModule *info)
1882 #ifdef MONO_ARCH_HAVE_PIC_AOT
1884 guint8 *buf = info->plt;
1886 #if defined(__x86_64__)
1891 if (info->plt_inited)
1894 tramp = mono_arch_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
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__)
1902 * Initialize the entries in the plt_jump_table to point to the default targets.
1904 n_entries = info->plt_jump_table_size / sizeof (gpointer);
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;
1912 g_assert_not_reached ();
1915 info->plt_inited = TRUE;
1920 * mono_aot_get_plt_entry:
1922 * Return the address of the PLT entry called by the code at CODE if exists.
1925 mono_aot_get_plt_entry (guint8 *code)
1927 MonoAotModule *aot_module = find_aot_module (code);
1932 #if defined(__i386__) || defined(__x86_64__)
1933 if (code [-5] == 0xe8) {
1934 guint32 disp = *(guint32*)(code - 4);
1935 guint8 *target = code + disp;
1937 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
1946 * mono_aot_get_n_pagefaults:
1948 * Return the number of times handle_pagefault is called.
1951 mono_aot_get_n_pagefaults (void)
1953 return n_pagefaults;
1960 mono_aot_init (void)
1965 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1971 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1977 mono_aot_init_vtable (MonoVTable *vtable)
1983 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1989 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1995 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2001 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2007 mono_aot_is_pagefault (void *ptr)
2013 mono_aot_set_make_unreadable (gboolean unreadable)
2018 mono_aot_get_n_pagefaults (void)
2024 mono_aot_handle_pagefault (void *ptr)
2029 mono_aot_get_plt_entry (guint8 *code)