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)
444 assembly->aot_module = load_aot_module_from_cache (assembly, &aot_name);
446 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
448 assembly->aot_module = g_module_open (aot_name, G_MODULE_BIND_LAZY);
450 if (!assembly->aot_module) {
451 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, g_module_error ());
455 if (!assembly->aot_module) {
460 g_module_symbol (assembly->aot_module, "mono_assembly_guid", (gpointer *) &saved_guid);
461 g_module_symbol (assembly->aot_module, "mono_aot_version", (gpointer *) &aot_version);
462 g_module_symbol (assembly->aot_module, "mono_aot_opt_flags", (gpointer *)&opt_flags);
464 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
465 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);
469 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
470 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
477 g_module_close (assembly->aot_module);
478 assembly->aot_module = NULL;
482 g_module_symbol (assembly->aot_module, "got_addr", (gpointer *)&got_addr);
484 got = (gpointer*)*got_addr;
486 g_module_symbol (assembly->aot_module, "got_size", (gpointer *)&got_size_ptr);
487 g_assert (got_size_ptr);
489 info = g_new0 (MonoAotModule, 1);
490 info->aot_name = aot_name;
492 info->got_size = *got_size_ptr;
493 info->got [0] = assembly->image;
495 sscanf (opt_flags, "%d", &info->opts);
497 /* Read image table */
499 guint32 table_len, i;
502 g_module_symbol (assembly->aot_module, "mono_image_table", (gpointer *)&table);
505 table_len = *(guint32*)table;
506 table += sizeof (guint32);
507 info->image_table = g_new0 (MonoImage*, table_len);
508 info->image_names = g_new0 (MonoAssemblyName, table_len);
509 info->image_guids = g_new0 (char*, table_len);
510 info->image_table_len = table_len;
511 for (i = 0; i < table_len; ++i) {
512 MonoAssemblyName *aname = &(info->image_names [i]);
514 aname->name = g_strdup (table);
515 table += strlen (table) + 1;
516 info->image_guids [i] = g_strdup (table);
517 table += strlen (table) + 1;
519 aname->culture = g_strdup (table);
520 table += strlen (table) + 1;
521 memcpy (aname->public_key_token, table, strlen (table) + 1);
522 table += strlen (table) + 1;
524 table = ALIGN_PTR_TO (table, 8);
525 aname->flags = *(guint32*)table;
527 aname->major = *(guint32*)table;
529 aname->minor = *(guint32*)table;
531 aname->build = *(guint32*)table;
533 aname->revision = *(guint32*)table;
538 /* Read method and method_info tables */
539 g_module_symbol (assembly->aot_module, "method_offsets", (gpointer*)&info->code_offsets);
540 g_module_symbol (assembly->aot_module, "methods", (gpointer*)&info->code);
541 g_module_symbol (assembly->aot_module, "methods_end", (gpointer*)&info->code_end);
542 g_module_symbol (assembly->aot_module, "method_info_offsets", (gpointer*)&info->method_info_offsets);
543 g_module_symbol (assembly->aot_module, "method_info", (gpointer*)&info->method_info);
544 g_module_symbol (assembly->aot_module, "ex_info_offsets", (gpointer*)&info->ex_info_offsets);
545 g_module_symbol (assembly->aot_module, "ex_info", (gpointer*)&info->ex_info);
546 g_module_symbol (assembly->aot_module, "method_order", (gpointer*)&info->method_order);
547 g_module_symbol (assembly->aot_module, "method_order_end", (gpointer*)&info->method_order_end);
548 g_module_symbol (assembly->aot_module, "class_info", (gpointer*)&info->class_info);
549 g_module_symbol (assembly->aot_module, "class_info_offsets", (gpointer*)&info->class_info_offsets);
550 g_module_symbol (assembly->aot_module, "class_name_table", (gpointer *)&info->class_name_table);
551 g_module_symbol (assembly->aot_module, "got_info", (gpointer*)&info->got_info);
552 g_module_symbol (assembly->aot_module, "got_info_offsets", (gpointer*)&info->got_info_offsets);
553 g_module_symbol (assembly->aot_module, "mem_end", (gpointer*)&info->mem_end);
555 info->mem_begin = info->code;
557 g_module_symbol (assembly->aot_module, "plt", (gpointer*)&info->plt);
558 g_module_symbol (assembly->aot_module, "plt_end", (gpointer*)&info->plt_end);
559 g_module_symbol (assembly->aot_module, "plt_info", (gpointer*)&info->plt_info);
561 g_module_symbol (assembly->aot_module, "plt_jump_table_addr", (gpointer *)&plt_jump_table_addr);
562 g_assert (plt_jump_table_addr);
563 info->plt_jump_table = (guint8*)*plt_jump_table_addr;
564 g_assert (info->plt_jump_table);
566 g_module_symbol (assembly->aot_module, "plt_jump_table_size", (gpointer *)&plt_jump_table_size);
567 g_assert (plt_jump_table_size);
568 info->plt_jump_table_size = *plt_jump_table_size;
570 if (make_unreadable) {
571 #ifndef PLATFORM_WIN32
576 addr = info->mem_begin;
577 len = info->mem_end - info->mem_begin;
579 /* Round down in both directions to avoid modifying data which is not ours */
580 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
581 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
582 err = mprotect (page_start, pages * PAGESIZE, 0);
589 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)info->code);
590 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)info->code_end);
592 g_hash_table_insert (aot_modules, assembly, info);
595 mono_jit_info_add_aot_module (assembly->image, info->code, info->code_end);
597 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
603 InitializeCriticalSection (&aot_mutex);
604 aot_modules = g_hash_table_new (NULL, NULL);
606 mono_install_assembly_load_hook (load_aot_module, NULL);
608 if (getenv ("MONO_LASTAOT"))
609 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
610 if (getenv ("MONO_AOT_CACHE"))
611 use_aot_cache = TRUE;
615 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
619 info->vtable_size = decode_value (buf, &buf);
620 flags = decode_value (buf, &buf);
621 info->ghcimpl = (flags >> 0) & 0x1;
622 info->has_finalize = (flags >> 1) & 0x1;
623 info->has_cctor = (flags >> 2) & 0x1;
624 info->has_nested_classes = (flags >> 3) & 0x1;
625 info->blittable = (flags >> 4) & 0x1;
626 info->has_references = (flags >> 5) & 0x1;
627 info->has_static_refs = (flags >> 6) & 0x1;
628 info->no_special_static_fields = (flags >> 7) & 0x1;
630 if (info->has_cctor) {
631 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, buf, &buf);
635 if (info->has_finalize) {
636 info->finalize_image = decode_method_ref (module, &info->finalize_token, buf, &buf);
637 if (!info->finalize_image)
641 info->instance_size = decode_value (buf, &buf);
642 info->class_size = decode_value (buf, &buf);
643 info->packing_size = decode_value (buf, &buf);
644 info->min_align = decode_value (buf, &buf);
652 mono_aot_init_vtable (MonoVTable *vtable)
655 MonoAotModule *aot_module;
656 MonoClass *klass = vtable->klass;
658 MonoCachedClassInfo class_info;
661 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !klass->image->assembly->aot_module)
666 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
672 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
675 err = decode_cached_class_info (aot_module, &class_info, p, &p);
681 //printf ("VT0: %s.%s %d\n", klass->name_space, klass->name, vtable_size);
682 for (i = 0; i < class_info.vtable_size; ++i) {
683 guint32 image_index, token, value;
685 #ifndef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
689 vtable->vtable [i] = 0;
691 value = decode_value (p, &p);
695 image_index = value >> 24;
696 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
698 image = load_image (aot_module, image_index);
704 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
705 vtable->vtable [i] = mono_create_jit_trampoline_from_token (image, token);
707 m = mono_get_method (image, token, NULL);
710 //printf ("M: %d %p %s\n", i, &(vtable->vtable [i]), mono_method_full_name (m, TRUE));
711 vtable->vtable [i] = mono_create_jit_trampoline (m);
721 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
723 MonoAotModule *aot_module;
727 if (klass->rank || !klass->image->assembly->aot_module)
732 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
738 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
740 err = decode_cached_class_info (aot_module, res, p, &p);
752 * mono_aot_get_class_from_name:
754 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
755 * using a cache stored in the AOT file.
756 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
758 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
762 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
764 MonoAotModule *aot_module;
765 guint32 *table, *entry;
766 guint32 table_size, hash;
768 const char *name2, *name_space2;
770 guint32 cols [MONO_TYPEDEF_SIZE];
777 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, image->assembly);
778 if (!aot_module || !aot_module->class_name_table) {
785 table_size = aot_module->class_name_table [0];
786 table = aot_module->class_name_table + 1;
788 if (name_space [0] == '\0')
789 full_name = g_strdup_printf ("%s", name);
791 full_name = g_strdup_printf ("%s.%s", name_space, name);
792 hash = g_str_hash (full_name) % table_size;
795 entry = &table [hash * 2];
797 if (entry [0] != 0) {
798 t = &image->tables [MONO_TABLE_TYPEDEF];
801 guint32 token = entry [0];
802 guint32 next = entry [1];
803 guint32 index = mono_metadata_token_index (token);
805 name_table_accesses ++;
807 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
809 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
810 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
812 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
814 *klass = mono_class_get (image, token);
819 entry = &table [next * 2];
832 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
833 MonoMethod *method, guint8* ex_info, guint8 *code)
837 guint code_len, used_int_regs;
839 MonoMethodHeader *header;
841 header = mono_method_get_header (method);
843 /* Load the method info from the AOT file */
846 code_len = decode_value (p, &p);
847 used_int_regs = decode_value (p, &p);
849 /* Exception table */
850 if (header->num_clauses) {
852 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses));
853 jinfo->num_clauses = header->num_clauses;
855 for (i = 0; i < header->num_clauses; ++i) {
856 MonoExceptionClause *ec = &header->clauses [i];
857 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
859 ei->flags = ec->flags;
860 ei->exvar_offset = decode_value (p, &p);
862 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
863 ei->data.filter = code + decode_value (p, &p);
865 ei->data.catch_class = ec->data.catch_class;
867 ei->try_start = code + decode_value (p, &p);
868 ei->try_end = code + decode_value (p, &p);
869 ei->handler_start = code + decode_value (p, &p);
873 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
875 jinfo->code_size = code_len;
876 jinfo->used_regs = used_int_regs;
877 jinfo->method = method;
878 jinfo->code_start = code;
879 jinfo->domain_neutral = 0;
881 /* Load debug info */
882 buf_len = decode_value (p, &p);
883 mono_debug_add_aot_method (domain, method, code, p, buf_len);
889 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
891 MonoAssembly *ass = image->assembly;
892 GModule *module = ass->aot_module;
893 int pos, left, right, offset, offset1, offset2, last_offset, new_offset, page_index, method_index, table_len;
895 MonoAotModule *aot_module;
898 guint8 *code, *ex_info;
899 guint32 *table, *ptr;
905 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
907 if (domain != mono_get_root_domain ())
911 offset = (guint8*)addr - aot_module->code;
913 /* First search through the index */
914 ptr = aot_module->method_order;
919 if (*ptr == 0xffffff)
923 while (*ptr != 0xffffff) {
924 guint32 method_index = ptr [0];
925 new_offset = aot_module->code_offsets [method_index];
927 if (offset >= last_offset && offset < new_offset) {
933 last_offset = new_offset;
937 /* Skip rest of index */
938 while (*ptr != 0xffffff)
943 table_len = aot_module->method_order_end - table;
945 g_assert (table <= aot_module->method_order_end);
948 left = (page_index * 1024);
951 if (right > table_len)
954 offset1 = aot_module->code_offsets [table [left]];
955 g_assert (offset1 <= offset);
957 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
960 //printf ("Not found in index: 0x%x\n", offset);
965 /* Binary search inside the method_order table to find the method */
967 pos = (left + right) / 2;
969 g_assert (table + pos <= aot_module->method_order_end);
971 //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]]);
973 offset1 = aot_module->code_offsets [table [pos]];
974 if (table + pos + 1 >= aot_module->method_order_end)
975 offset2 = aot_module->code_end - aot_module->code;
977 offset2 = aot_module->code_offsets [table [pos + 1]];
979 if (offset < offset1)
981 else if (offset >= offset2)
987 method_index = table [pos];
989 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
990 method = mono_get_method (image, token, NULL);
995 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
997 code = &aot_module->code [aot_module->code_offsets [method_index]];
998 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1000 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1002 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1003 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1005 /* Add it to the normal JitInfo tables */
1006 mono_jit_info_table_add (domain, jinfo);
1012 decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1020 case MONO_PATCH_INFO_METHOD:
1021 case MONO_PATCH_INFO_METHODCONST:
1022 case MONO_PATCH_INFO_METHOD_JUMP: {
1023 guint32 image_index, token, value;
1025 value = decode_value (p, &p);
1026 image_index = value >> 24;
1027 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1029 image = load_image (aot_module, image_index);
1033 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1034 if (ji->type == MONO_PATCH_INFO_METHOD) {
1035 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1036 ji->type = MONO_PATCH_INFO_ABS;
1039 ji->data.method = mono_get_method (image, token, NULL);
1040 g_assert (ji->data.method);
1041 mono_class_init (ji->data.method->klass);
1044 ji->data.method = mono_get_method (image, token, NULL);
1045 g_assert (ji->data.method);
1046 mono_class_init (ji->data.method->klass);
1051 case MONO_PATCH_INFO_WRAPPER: {
1052 guint32 wrapper_type;
1054 wrapper_type = decode_value (p, &p);
1056 switch (wrapper_type) {
1057 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
1058 guint32 image_index, token, value;
1060 value = decode_value (p, &p);
1061 image_index = value >> 24;
1062 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1064 image = load_image (aot_module, image_index);
1067 ji->data.method = mono_get_method (image, token, NULL);
1068 g_assert (ji->data.method);
1069 mono_class_init (ji->data.method->klass);
1071 ji->type = MONO_PATCH_INFO_METHOD;
1072 ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
1075 case MONO_WRAPPER_PROXY_ISINST: {
1076 MonoClass *klass = decode_klass_info (aot_module, p, &p);
1079 ji->type = MONO_PATCH_INFO_METHOD;
1080 ji->data.method = mono_marshal_get_proxy_cancast (klass);
1083 case MONO_WRAPPER_LDFLD:
1084 case MONO_WRAPPER_LDFLDA:
1085 case MONO_WRAPPER_STFLD:
1086 case MONO_WRAPPER_LDFLD_REMOTE:
1087 case MONO_WRAPPER_STFLD_REMOTE:
1088 case MONO_WRAPPER_ISINST: {
1089 MonoClass *klass = decode_klass_info (aot_module, p, &p);
1092 ji->type = MONO_PATCH_INFO_METHOD;
1093 if (wrapper_type == MONO_WRAPPER_LDFLD)
1094 ji->data.method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
1095 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
1096 ji->data.method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
1097 else if (wrapper_type == MONO_WRAPPER_STFLD)
1098 ji->data.method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
1099 else if (wrapper_type == MONO_WRAPPER_LDFLD_REMOTE)
1100 ji->data.method = mono_marshal_get_ldfld_remote_wrapper (klass);
1101 else if (wrapper_type == MONO_WRAPPER_STFLD_REMOTE)
1102 ji->data.method = mono_marshal_get_stfld_remote_wrapper (klass);
1103 else if (wrapper_type == MONO_WRAPPER_ISINST)
1104 ji->data.method = mono_marshal_get_isinst (klass);
1106 g_assert_not_reached ();
1109 case MONO_WRAPPER_STELEMREF:
1110 ji->type = MONO_PATCH_INFO_METHOD;
1111 ji->data.method = mono_marshal_get_stelemref ();
1114 g_assert_not_reached ();
1118 case MONO_PATCH_INFO_INTERNAL_METHOD: {
1119 guint32 len = decode_value (p, &p);
1121 ji->data.name = (char*)p;
1125 case MONO_PATCH_INFO_VTABLE:
1126 case MONO_PATCH_INFO_CLASS:
1127 case MONO_PATCH_INFO_IID:
1128 case MONO_PATCH_INFO_ADJUSTED_IID:
1129 *got_offset = decode_value (p, &p);
1131 if (aot_module->got [*got_offset]) {
1132 /* Already loaded */
1133 //printf ("HIT!\n");
1135 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1136 ji->data.klass = decode_klass_info (aot_module, tmp, &tmp);
1137 if (!ji->data.klass)
1141 case MONO_PATCH_INFO_CLASS_INIT:
1142 ji->data.klass = decode_klass_info (aot_module, p, &p);
1143 if (!ji->data.klass)
1146 case MONO_PATCH_INFO_IMAGE:
1147 ji->data.image = load_image (aot_module, decode_value (p, &p));
1148 if (!ji->data.image)
1151 case MONO_PATCH_INFO_FIELD:
1152 case MONO_PATCH_INFO_SFLDA:
1153 *got_offset = decode_value (p, &p);
1155 if (aot_module->got [*got_offset]) {
1156 /* Already loaded */
1157 //printf ("HIT2!\n");
1159 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1160 ji->data.field = decode_field_info (aot_module, tmp, &tmp);
1161 if (!ji->data.field)
1165 case MONO_PATCH_INFO_SWITCH:
1166 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1167 ji->data.table->table_size = decode_value (p, &p);
1168 table = g_new (gpointer, ji->data.table->table_size);
1169 ji->data.table->table = (MonoBasicBlock**)table;
1170 for (i = 0; i < ji->data.table->table_size; i++)
1171 table [i] = (gpointer)(gssize)decode_value (p, &p);
1173 case MONO_PATCH_INFO_R4: {
1176 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1177 val = decode_value (p, &p);
1178 *(float*)ji->data.target = *(float*)&val;
1181 case MONO_PATCH_INFO_R8: {
1184 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1186 val [0] = decode_value (p, &p);
1187 val [1] = decode_value (p, &p);
1188 *(double*)ji->data.target = *(double*)val;
1191 case MONO_PATCH_INFO_LDSTR:
1192 image = load_image (aot_module, decode_value (p, &p));
1195 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1197 case MONO_PATCH_INFO_DECLSEC:
1198 case MONO_PATCH_INFO_LDTOKEN:
1199 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1200 *got_offset = decode_value (p, &p);
1202 if (aot_module->got [*got_offset]) {
1203 /* Already loaded */
1205 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1206 image = load_image (aot_module, decode_value (tmp, &tmp));
1209 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (tmp, &tmp));
1212 case MONO_PATCH_INFO_EXC_NAME:
1213 ji->data.klass = decode_klass_info (aot_module, p, &p);
1214 if (!ji->data.klass)
1216 ji->data.name = ji->data.klass->name;
1218 case MONO_PATCH_INFO_METHOD_REL:
1219 ji->data.offset = decode_value (p, &p);
1222 g_warning ("unhandled type %d", ji->type);
1223 g_assert_not_reached ();
1234 static MonoJumpInfo*
1235 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1236 guint32 got_index, guint32 **got_slots,
1237 guint8 *buf, guint8 **endbuf)
1239 MonoJumpInfo *patches;
1240 MonoJumpInfo *patch_info = NULL;
1242 guint32 last_offset;
1247 /* First load the type + offset table */
1249 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1251 for (pindex = 0; pindex < n_patches; ++pindex) {
1252 MonoJumpInfo *ji = &patches [pindex];
1257 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1258 ji->next = patch_info;
1262 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1263 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1265 /* Then load the other data */
1266 for (pindex = 0; pindex < n_patches; ++pindex) {
1267 MonoJumpInfo *ji = &patches [pindex];
1269 if (!decode_patch_info (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1272 if ((*got_slots) [pindex] == 0xffffffff)
1273 (*got_slots) [pindex] = got_index ++;
1280 g_free (*got_slots);
1286 static MonoJitInfo *
1287 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
1289 MonoClass *klass = method->klass;
1290 MonoAssembly *ass = klass->image->assembly;
1291 GModule *module = ass->aot_module;
1292 guint8 *code, *info, *ex_info;
1293 MonoAotModule *aot_module;
1301 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1304 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1305 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1306 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1307 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1310 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1312 g_assert (klass->inited);
1314 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1315 /* Non shared AOT code can't be used in other appdomains */
1318 if (aot_module->out_of_date)
1321 if (aot_module->code_offsets [mono_metadata_token_index (method->token) - 1] == 0xffffffff) {
1322 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1323 char *full_name = mono_method_full_name (method, TRUE);
1324 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1330 code = &aot_module->code [aot_module->code_offsets [mono_metadata_token_index (method->token) - 1]];
1331 info = &aot_module->method_info [aot_module->method_info_offsets [mono_metadata_token_index (method->token) - 1]];
1332 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1334 if (mono_last_aot_method != -1) {
1335 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1338 if (mono_jit_stats.methods_aot == mono_last_aot_method)
1339 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
1342 return mono_aot_load_method (domain, aot_module, method, code, info, ex_info);
1346 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info, guint8* ex_info)
1348 MonoClass *klass = method->klass;
1349 MonoJumpInfo *patch_info = NULL;
1352 int i, pindex, got_index = 0, n_patches, used_strings;
1353 gboolean non_got_patches, keep_patches = TRUE;
1356 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1359 decode_klass_info (aot_module, p, &p);
1361 if (!use_loaded_code) {
1363 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1364 memcpy (code2, code, jinfo->code_size);
1365 mono_arch_flush_icache (code2, jinfo->code_size);
1369 if (aot_module->opts & MONO_OPT_SHARED)
1370 used_strings = decode_value (p, &p);
1374 for (i = 0; i < used_strings; i++) {
1375 guint token = decode_value (p, &p);
1376 mono_ldstr (mono_get_root_domain (), klass->image, mono_metadata_token_index (token));
1379 if (aot_module->opts & MONO_OPT_SHARED)
1380 keep_patches = FALSE;
1382 n_patches = decode_value (p, &p);
1384 keep_patches = FALSE;
1387 MonoJumpInfo *patches;
1393 mp = mono_mempool_new ();
1395 got_index = decode_value (p, &p);
1397 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1398 if (patches == NULL)
1401 /* Do this outside the lock to avoid deadlocks */
1403 non_got_patches = FALSE;
1404 for (pindex = 0; pindex < n_patches; ++pindex) {
1405 MonoJumpInfo *ji = &patches [pindex];
1407 if (is_got_patch (ji->type)) {
1408 if (!aot_module->got [got_slots [pindex]])
1409 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
1410 ji->type = MONO_PATCH_INFO_NONE;
1413 non_got_patches = TRUE;
1415 if (non_got_patches) {
1416 mono_arch_flush_icache (code, jinfo->code_size);
1417 make_writable (code, jinfo->code_size);
1418 mono_arch_patch_code (method, domain, code, patch_info, TRUE);
1425 mono_mempool_destroy (mp);
1428 mono_jit_stats.methods_aot++;
1430 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1431 char *full_name = mono_method_full_name (method, TRUE);
1432 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);
1436 init_plt (aot_module);
1441 /* FIXME: The space in domain->mp is wasted */
1442 if (aot_module->opts & MONO_OPT_SHARED)
1443 /* No need to cache patches */
1444 mono_mempool_destroy (mp);
1450 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1455 info = mono_aot_get_method_inner (domain, method);
1458 /* Do this outside the lock */
1460 mono_jit_info_table_add (domain, info);
1468 mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guint32 token, MonoClass **klass)
1470 MonoAssembly *ass = image->assembly;
1472 int i, method_index, pindex, got_index, n_patches, used_strings;
1473 gboolean keep_patches = TRUE;
1475 GModule *module = ass->aot_module;
1476 guint8 *code = NULL;
1478 MonoAotModule *aot_module;
1485 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1488 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1490 if (domain != mono_get_root_domain ())
1493 if (aot_module->out_of_date)
1496 if (aot_module->code_offsets [mono_metadata_token_index (token) - 1] == 0xffffffff) {
1500 method_index = mono_metadata_token_index (token) - 1;
1501 code = &aot_module->code [aot_module->code_offsets [method_index]];
1502 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1504 if (!aot_module->methods_loaded)
1505 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1507 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1510 if (mono_last_aot_method != -1) {
1511 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1514 if (mono_jit_stats.methods_aot == mono_last_aot_method) {
1515 MonoMethod *method = mono_get_method (image, token, NULL);
1516 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1521 *klass = decode_klass_info (aot_module, p, &p);
1523 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1524 MonoMethod *method = mono_get_method (image, token, NULL);
1525 char *full_name = mono_method_full_name (method, TRUE);
1526 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p %p\n", full_name, code, info);
1530 if (aot_module->opts & MONO_OPT_SHARED)
1531 used_strings = decode_value (p, &p);
1535 for (i = 0; i < used_strings; i++) {
1536 guint string_token = decode_value (p, &p);
1537 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (string_token));
1540 if (aot_module->opts & MONO_OPT_SHARED)
1541 keep_patches = FALSE;
1543 keep_patches = FALSE;
1545 n_patches = decode_value (p, &p);
1548 MonoJumpInfo *patches;
1554 mp = mono_mempool_new ();
1556 got_index = decode_value (p, &p);
1558 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1559 if (patches == NULL)
1562 /* Do this outside the lock to avoid deadlocks */
1565 for (pindex = 0; pindex < n_patches; ++pindex) {
1566 MonoJumpInfo *ji = &patches [pindex];
1568 if (is_got_patch (ji->type)) {
1569 if (!aot_module->got [got_slots [pindex]])
1570 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (NULL, domain, code, ji, TRUE);
1571 ji->type = MONO_PATCH_INFO_NONE;
1580 mono_mempool_destroy (mp);
1583 mono_jit_stats.methods_aot++;
1585 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1587 init_plt (aot_module);
1592 /* FIXME: The space in domain->mp is wasted */
1593 if (aot_module->opts & MONO_OPT_SHARED)
1594 /* No need to cache patches */
1595 mono_mempool_destroy (mp);
1601 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
1605 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1611 res = mono_aot_get_method_from_token_inner (domain, image, token, &klass);
1618 mono_runtime_class_init (mono_class_vtable (domain, klass));
1626 } IsGotEntryUserData;
1629 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
1631 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
1632 MonoAotModule *aot_module = (MonoAotModule*)value;
1634 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
1639 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1641 IsGotEntryUserData user_data;
1646 user_data.addr = addr;
1647 user_data.res = FALSE;
1649 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
1652 return user_data.res;
1657 MonoAotModule *module;
1658 } FindAotModuleUserData;
1661 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
1663 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
1664 MonoAotModule *aot_module = (MonoAotModule*)value;
1666 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
1667 data->module = aot_module;
1670 static inline MonoAotModule*
1671 find_aot_module (guint8 *code)
1673 FindAotModuleUserData user_data;
1678 /* Reading these need no locking */
1679 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
1682 user_data.addr = code;
1683 user_data.module = NULL;
1686 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
1689 return user_data.module;
1693 * mono_aot_set_make_unreadable:
1695 * Set whenever to make all mmaped memory unreadable. In conjuction with a
1696 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
1699 mono_aot_set_make_unreadable (gboolean unreadable)
1701 make_unreadable = unreadable;
1705 MonoAotModule *module;
1710 find_map (gpointer key, gpointer value, gpointer user_data)
1712 MonoAotModule *module = (MonoAotModule*)value;
1713 FindMapUserData *data = (FindMapUserData*)user_data;
1716 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
1717 data->module = module;
1720 static MonoAotModule*
1721 find_module_for_addr (void *ptr)
1723 FindMapUserData data;
1725 if (!make_unreadable)
1729 data.ptr = (guint8*)ptr;
1732 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
1739 * mono_aot_is_pagefault:
1741 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
1742 * within memory allocated by this module.
1745 mono_aot_is_pagefault (void *ptr)
1747 if (!make_unreadable)
1750 return find_module_for_addr (ptr) != NULL;
1754 * mono_aot_handle_pagefault:
1756 * Handle a pagefault caused by an unreadable page by making it readable again.
1759 mono_aot_handle_pagefault (void *ptr)
1761 #ifndef PLATFORM_WIN32
1762 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
1766 res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
1767 g_assert (res == 0);
1777 * This function is called by the entries in the PLT to resolve the actual method that
1778 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
1781 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
1783 #ifdef MONO_ARCH_HAVE_PIC_AOT
1784 guint8 *p, *target, *plt_entry;
1786 MonoAotModule *module = (MonoAotModule*)aot_module;
1789 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
1791 p = &module->plt_info [plt_info_offset];
1793 ji.type = decode_value (p, &p);
1795 res = decode_patch_info (module, NULL, &ji, p, &p, NULL);
1796 // FIXME: Error handling (how ?)
1799 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
1801 /* Patch the PLT entry with target which might be the actual method not a trampoline */
1802 plt_entry = mono_aot_get_plt_entry (code);
1803 g_assert (plt_entry);
1804 mono_arch_patch_plt_entry (plt_entry, target);
1808 g_assert_not_reached ();
1816 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
1817 * method in the module is loaded to avoid committing memory by writing to it.
1818 * LOCKING: Assumes the AOT lock is held.
1821 init_plt (MonoAotModule *info)
1823 #ifdef MONO_ARCH_HAVE_PIC_AOT
1825 guint8 *buf = info->plt;
1827 #if defined(__x86_64__)
1832 if (info->plt_inited)
1835 tramp = mono_arch_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
1838 /* Initialize the first PLT entry */
1839 make_writable (info->plt, info->plt_end - info->plt);
1840 x86_jump_code (buf, tramp);
1841 #elif defined(__x86_64__)
1843 * Initialize the entries in the plt_jump_table to point to the default targets.
1845 n_entries = info->plt_jump_table_size / sizeof (gpointer);
1847 /* The first entry points to the AOT trampoline */
1848 ((gpointer*)info->plt_jump_table)[0] = tramp;
1849 for (i = 1; i < n_entries; ++i)
1850 /* Each PLT entry is 16 bytes long, the default entry begins at offset 6 */
1851 ((gpointer*)info->plt_jump_table)[i] = info->plt + (i * 16) + 6;
1853 g_assert_not_reached ();
1856 info->plt_inited = TRUE;
1861 * mono_aot_get_plt_entry:
1863 * Return the address of the PLT entry called by the code at CODE if exists.
1866 mono_aot_get_plt_entry (guint8 *code)
1868 MonoAotModule *aot_module = find_aot_module (code);
1873 #if defined(__i386__) || defined(__x86_64__)
1874 if (code [-5] == 0xe8) {
1875 guint32 disp = *(guint32*)(code - 4);
1876 guint8 *target = code + disp;
1878 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
1887 * mono_aot_get_n_pagefaults:
1889 * Return the number of times handle_pagefault is called.
1892 mono_aot_get_n_pagefaults (void)
1894 return n_pagefaults;
1901 mono_aot_init (void)
1906 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1912 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1918 mono_aot_init_vtable (MonoVTable *vtable)
1924 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1930 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1936 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1942 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1948 mono_aot_is_pagefault (void *ptr)
1954 mono_aot_set_make_unreadable (gboolean unreadable)
1959 mono_aot_get_n_pagefaults (void)
1965 mono_aot_handle_pagefault (void *ptr)
1970 mono_aot_get_plt_entry (guint8 *code)