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>
18 #ifndef PLATFORM_WIN32
25 #ifdef HAVE_EXECINFO_H
31 #include <limits.h> /* for PAGESIZE */
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
40 #include <mono/metadata/tabledefs.h>
41 #include <mono/metadata/class.h>
42 #include <mono/metadata/object.h>
43 #include <mono/metadata/tokentype.h>
44 #include <mono/metadata/appdomain.h>
45 #include <mono/metadata/debug-helpers.h>
46 #include <mono/metadata/assembly.h>
47 #include <mono/metadata/metadata-internals.h>
48 #include <mono/metadata/marshal.h>
49 #include <mono/metadata/gc-internal.h>
50 #include <mono/utils/mono-logger.h>
51 #include "mono/utils/mono-compiler.h"
59 #define SHARED_EXT ".dll"
60 #elif defined(__ppc__) && defined(__MACH__)
61 #define SHARED_EXT ".dylib"
63 #define SHARED_EXT ".so"
66 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
67 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
69 typedef struct MonoAotModule {
71 /* Optimization flags used to compile the module */
73 /* Pointer to the Global Offset Table */
76 GHashTable *name_cache;
77 MonoAssemblyName *image_names;
79 MonoImage **image_table;
80 guint32 image_table_len;
90 guint8 *plt_jump_table;
91 guint32 plt_jump_table_size;
92 guint32 *code_offsets;
94 guint32 *method_info_offsets;
96 guint32 *got_info_offsets;
98 guint32 *ex_info_offsets;
99 guint32 *method_order;
100 guint32 *method_order_end;
102 guint32 *class_info_offsets;
103 guint32 *methods_loaded;
104 guint16 *class_name_table;
107 static GHashTable *aot_modules;
108 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
109 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
110 static CRITICAL_SECTION aot_mutex;
113 * Disabling this will make a copy of the loaded code and use the copy instead
114 * of the original. This will place the caller and the callee close to each
115 * other in memory, possibly improving cache behavior. Since the original
116 * code is in copy-on-write memory, this will not increase the memory usage
119 static gboolean use_loaded_code = TRUE;
122 * Whenever to AOT compile loaded assemblies on demand and store them in
123 * a cache under $HOME/.mono/aot-cache.
125 static gboolean use_aot_cache = FALSE;
128 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
129 * use_aot_cache is TRUE.
131 static gboolean spawn_compiler = TRUE;
134 static gint32 mono_last_aot_method = -1;
136 static gboolean make_unreadable = FALSE;
137 static guint32 n_pagefaults = 0;
138 static guint32 name_table_accesses = 0;
140 /* Used to speed-up find_aot_module () */
141 static gsize aot_code_low_addr = (gssize)-1;
142 static gsize aot_code_high_addr = 0;
145 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info);
148 init_plt (MonoAotModule *info);
150 static inline gboolean
151 is_got_patch (MonoJumpInfoType patch_type)
156 /*****************************************************/
158 /*****************************************************/
161 load_image (MonoAotModule *module, int index)
163 MonoAssembly *assembly;
164 MonoImageOpenStatus status;
166 g_assert (index < module->image_table_len);
168 if (module->image_table [index])
169 return module->image_table [index];
170 if (module->out_of_date)
173 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
175 module->out_of_date = TRUE;
179 if (strcmp (assembly->image->guid, module->image_guids [index])) {
180 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);
181 module->out_of_date = TRUE;
185 module->image_table [index] = assembly->image;
186 return assembly->image;
191 decode_value (guint8 *ptr, guint8 **rptr)
196 if ((b & 0x80) == 0){
199 } else if ((b & 0x40) == 0){
200 len = ((b & 0x3f) << 8 | ptr [1]);
202 } else if (b != 0xff) {
203 len = ((b & 0x1f) << 24) |
210 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
216 //printf ("DECODE: %d.\n", len);
221 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
224 MonoClass *klass, *eklass;
227 token = decode_value (buf, &buf);
232 if (mono_metadata_token_table (token) == 0) {
233 image = load_image (module, decode_value (buf, &buf));
236 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
237 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
238 if (token == MONO_TOKEN_TYPE_SPEC) {
241 MonoGenericContext ctx;
242 MonoGenericInst inst;
244 gclass = decode_klass_ref (module, buf, &buf);
245 g_assert (gclass->generic_container);
247 memset (&ctx, 0, sizeof (ctx));
248 memset (&inst, 0, sizeof (inst));
249 ctx.class_inst = &inst;
250 inst.type_argc = decode_value (buf, &buf);
251 inst.type_argv = g_new0 (MonoType*, inst.type_argc);
252 for (i = 0; i < inst.type_argc; ++i) {
253 MonoClass *pclass = decode_klass_ref (module, buf, &buf);
255 g_free (inst.type_argv);
258 inst.type_argv [i] = &pclass->byval_arg;
260 klass = mono_class_from_mono_type (mono_class_inflate_generic_type (&gclass->byval_arg, &ctx));
262 image = load_image (module, decode_value (buf, &buf));
265 klass = mono_class_get (image, token);
267 } else if (token == MONO_TOKEN_TYPE_DEF) {
269 image = load_image (module, decode_value (buf, &buf));
272 rank = decode_value (buf, &buf);
273 eklass = decode_klass_ref (module, buf, &buf);
274 klass = mono_array_class_get (eklass, rank);
276 g_assert_not_reached ();
279 mono_class_init (klass);
285 static MonoClassField*
286 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
288 MonoClass *klass = decode_klass_ref (module, buf, &buf);
294 token = MONO_TOKEN_FIELD_DEF + decode_value (buf, &buf);
298 return mono_class_get_field (klass, token);
301 static inline MonoImage*
302 decode_method_ref (MonoAotModule *module, guint32 *token, guint8 *buf, guint8 **endbuf)
304 guint32 image_index, value;
307 value = decode_value (buf, &buf);
308 image_index = value >> 24;
309 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
311 if (image_index == 255) {
313 image_index = decode_value (buf, &buf);
314 *token = decode_value (buf, &buf);
319 image = load_image (module, image_index);
328 make_writable (guint8* addr, guint32 len)
330 #ifndef PLATFORM_WIN32
334 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
335 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
336 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
341 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
347 create_cache_structure (void)
353 home = g_get_home_dir ();
357 tmp = g_build_filename (home, ".mono", NULL);
358 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
359 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
360 #ifdef PLATFORM_WIN32
363 err = mkdir (tmp, 0777);
366 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
372 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
373 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
374 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
375 #ifdef PLATFORM_WIN32
378 err = mkdir (tmp, 0777);
381 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
390 * load_aot_module_from_cache:
392 * Experimental code to AOT compile loaded assemblies on demand.
395 * - Add environment variable MONO_AOT_CACHE_OPTIONS
396 * - Add options for controlling the cache size
397 * - Handle full cache by deleting old assemblies lru style
398 * - Add options for excluding assemblies during development
399 * - Maybe add a threshold after an assembly is AOT compiled
400 * - invoking a new mono process is a security risk
401 * - recompile the AOT module if one of its dependencies changes
404 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
406 char *fname, *cmd, *tmp2, *aot_options;
415 if (assembly->image->dynamic)
418 create_cache_structure ();
420 home = g_get_home_dir ();
422 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
423 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
427 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
428 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
431 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
433 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
435 aot_options = g_strdup_printf ("outfile=%s", fname);
437 if (spawn_compiler) {
438 /* FIXME: security */
439 /* FIXME: Has to pass the assembly loading path to the child process */
440 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
442 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
444 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__powerpc__)
446 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
447 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
449 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
456 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
458 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
460 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
464 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
466 g_free (aot_options);
473 load_aot_module (MonoAssembly *assembly, gpointer user_data)
477 gboolean usable = TRUE;
478 char *saved_guid = NULL;
479 char *aot_version = NULL;
480 char *runtime_version;
481 char *opt_flags = NULL;
482 gpointer *plt_jump_table_addr = NULL;
483 guint32 *plt_jump_table_size = NULL;
484 gpointer *got_addr = NULL;
485 gpointer *got = NULL;
486 guint32 *got_size_ptr = NULL;
489 if (mono_compile_aot)
492 if (assembly->aot_module)
494 * Already loaded. This can happen because the assembly loading code might invoke
495 * the assembly load hooks multiple times for the same assembly.
500 assembly->aot_module = load_aot_module_from_cache (assembly, &aot_name);
503 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
505 assembly->aot_module = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
507 if (!assembly->aot_module) {
508 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
513 if (!assembly->aot_module) {
518 mono_dl_symbol (assembly->aot_module, "mono_assembly_guid", (gpointer *) &saved_guid);
519 mono_dl_symbol (assembly->aot_module, "mono_aot_version", (gpointer *) &aot_version);
520 mono_dl_symbol (assembly->aot_module, "mono_aot_opt_flags", (gpointer *)&opt_flags);
521 mono_dl_symbol (assembly->aot_module, "mono_runtime_version", (gpointer *)&runtime_version);
523 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
524 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);
528 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
529 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
534 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, FULL_VERSION)))) {
535 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled against runtime version %s while this runtime has version %s.\n", aot_name, runtime_version, FULL_VERSION);
541 mono_dl_close (assembly->aot_module);
542 assembly->aot_module = NULL;
546 mono_dl_symbol (assembly->aot_module, "got_addr", (gpointer *)&got_addr);
548 got = (gpointer*)*got_addr;
550 mono_dl_symbol (assembly->aot_module, "got_size", (gpointer *)&got_size_ptr);
551 g_assert (got_size_ptr);
553 info = g_new0 (MonoAotModule, 1);
554 info->aot_name = aot_name;
556 info->got_size = *got_size_ptr;
557 info->got [0] = assembly->image;
559 sscanf (opt_flags, "%d", &info->opts);
561 /* Read image table */
563 guint32 table_len, i;
566 mono_dl_symbol (assembly->aot_module, "mono_image_table", (gpointer *)&table);
569 table_len = *(guint32*)table;
570 table += sizeof (guint32);
571 info->image_table = g_new0 (MonoImage*, table_len);
572 info->image_names = g_new0 (MonoAssemblyName, table_len);
573 info->image_guids = g_new0 (char*, table_len);
574 info->image_table_len = table_len;
575 for (i = 0; i < table_len; ++i) {
576 MonoAssemblyName *aname = &(info->image_names [i]);
578 aname->name = g_strdup (table);
579 table += strlen (table) + 1;
580 info->image_guids [i] = g_strdup (table);
581 table += strlen (table) + 1;
583 aname->culture = g_strdup (table);
584 table += strlen (table) + 1;
585 memcpy (aname->public_key_token, table, strlen (table) + 1);
586 table += strlen (table) + 1;
588 table = ALIGN_PTR_TO (table, 8);
589 aname->flags = *(guint32*)table;
591 aname->major = *(guint32*)table;
593 aname->minor = *(guint32*)table;
595 aname->build = *(guint32*)table;
597 aname->revision = *(guint32*)table;
602 /* Read method and method_info tables */
603 mono_dl_symbol (assembly->aot_module, "method_offsets", (gpointer*)&info->code_offsets);
604 mono_dl_symbol (assembly->aot_module, "methods", (gpointer*)&info->code);
605 mono_dl_symbol (assembly->aot_module, "methods_end", (gpointer*)&info->code_end);
606 mono_dl_symbol (assembly->aot_module, "method_info_offsets", (gpointer*)&info->method_info_offsets);
607 mono_dl_symbol (assembly->aot_module, "method_info", (gpointer*)&info->method_info);
608 mono_dl_symbol (assembly->aot_module, "ex_info_offsets", (gpointer*)&info->ex_info_offsets);
609 mono_dl_symbol (assembly->aot_module, "ex_info", (gpointer*)&info->ex_info);
610 mono_dl_symbol (assembly->aot_module, "method_order", (gpointer*)&info->method_order);
611 mono_dl_symbol (assembly->aot_module, "method_order_end", (gpointer*)&info->method_order_end);
612 mono_dl_symbol (assembly->aot_module, "class_info", (gpointer*)&info->class_info);
613 mono_dl_symbol (assembly->aot_module, "class_info_offsets", (gpointer*)&info->class_info_offsets);
614 mono_dl_symbol (assembly->aot_module, "class_name_table", (gpointer *)&info->class_name_table);
615 mono_dl_symbol (assembly->aot_module, "got_info", (gpointer*)&info->got_info);
616 mono_dl_symbol (assembly->aot_module, "got_info_offsets", (gpointer*)&info->got_info_offsets);
617 mono_dl_symbol (assembly->aot_module, "mem_end", (gpointer*)&info->mem_end);
619 info->mem_begin = info->code;
621 mono_dl_symbol (assembly->aot_module, "plt", (gpointer*)&info->plt);
622 mono_dl_symbol (assembly->aot_module, "plt_end", (gpointer*)&info->plt_end);
623 mono_dl_symbol (assembly->aot_module, "plt_info", (gpointer*)&info->plt_info);
625 mono_dl_symbol (assembly->aot_module, "plt_jump_table_addr", (gpointer *)&plt_jump_table_addr);
626 g_assert (plt_jump_table_addr);
627 info->plt_jump_table = (guint8*)*plt_jump_table_addr;
628 g_assert (info->plt_jump_table);
630 mono_dl_symbol (assembly->aot_module, "plt_jump_table_size", (gpointer *)&plt_jump_table_size);
631 g_assert (plt_jump_table_size);
632 info->plt_jump_table_size = *plt_jump_table_size;
634 if (make_unreadable) {
635 #ifndef PLATFORM_WIN32
640 addr = info->mem_begin;
641 len = info->mem_end - info->mem_begin;
643 /* Round down in both directions to avoid modifying data which is not ours */
644 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
645 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
646 err = mprotect (page_start, pages * PAGESIZE, 0);
653 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)info->code);
654 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)info->code_end);
656 g_hash_table_insert (aot_modules, assembly, info);
659 mono_jit_info_add_aot_module (assembly->image, info->code, info->code_end);
662 * Since we store methoddef and classdef tokens when referring to methods/classes in
663 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
664 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
665 * non-lazily, since we can't handle out-of-date errors later.
667 for (i = 0; i < info->image_table_len; ++i)
668 load_image (info, i);
670 if (info->out_of_date)
671 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name);
673 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
679 InitializeCriticalSection (&aot_mutex);
680 aot_modules = g_hash_table_new (NULL, NULL);
682 mono_install_assembly_load_hook (load_aot_module, NULL);
684 if (getenv ("MONO_LASTAOT"))
685 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
686 if (getenv ("MONO_AOT_CACHE"))
687 use_aot_cache = TRUE;
691 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
695 info->vtable_size = decode_value (buf, &buf);
696 if (info->vtable_size == -1)
699 flags = decode_value (buf, &buf);
700 info->ghcimpl = (flags >> 0) & 0x1;
701 info->has_finalize = (flags >> 1) & 0x1;
702 info->has_cctor = (flags >> 2) & 0x1;
703 info->has_nested_classes = (flags >> 3) & 0x1;
704 info->blittable = (flags >> 4) & 0x1;
705 info->has_references = (flags >> 5) & 0x1;
706 info->has_static_refs = (flags >> 6) & 0x1;
707 info->no_special_static_fields = (flags >> 7) & 0x1;
709 if (info->has_cctor) {
710 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, buf, &buf);
714 if (info->has_finalize) {
715 info->finalize_image = decode_method_ref (module, &info->finalize_token, buf, &buf);
716 if (!info->finalize_image)
720 info->instance_size = decode_value (buf, &buf);
721 info->class_size = decode_value (buf, &buf);
722 info->packing_size = decode_value (buf, &buf);
723 info->min_align = decode_value (buf, &buf);
731 mono_aot_init_vtable (MonoVTable *vtable)
733 #ifdef MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
735 MonoAotModule *aot_module;
736 MonoClass *klass = vtable->klass;
738 MonoCachedClassInfo class_info;
741 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !klass->image->assembly->aot_module)
746 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
752 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
755 err = decode_cached_class_info (aot_module, &class_info, p, &p);
763 //printf ("VT0: %s.%s %d\n", klass->name_space, klass->name, vtable_size);
764 for (i = 0; i < class_info.vtable_size; ++i) {
765 vtable->vtable [i] = mini_get_vtable_trampoline ();
775 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
778 MonoAotModule *aot_module;
779 MonoClass *klass = vtable->klass;
781 MonoCachedClassInfo class_info;
786 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !klass->image->assembly->aot_module)
791 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
797 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
800 err = decode_cached_class_info (aot_module, &class_info, p, &p);
806 for (i = 0; i < slot; ++i)
807 decode_method_ref (aot_module, &token, p, &p);
809 image = decode_method_ref (aot_module, &token, p, &p);
817 return mono_aot_get_method_from_token (domain, image, token);
821 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
823 MonoAotModule *aot_module;
827 if (klass->rank || !klass->image->assembly->aot_module)
832 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
838 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
840 err = decode_cached_class_info (aot_module, res, p, &p);
852 * mono_aot_get_class_from_name:
854 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
855 * using a cache stored in the AOT file.
856 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
858 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
862 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
864 MonoAotModule *aot_module;
865 guint16 *table, *entry;
868 char full_name_buf [1024];
870 const char *name2, *name_space2;
872 guint32 cols [MONO_TYPEDEF_SIZE];
873 GHashTable *nspace_table;
880 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, image->assembly);
881 if (!aot_module || !aot_module->class_name_table) {
888 /* First look in the cache */
889 if (!aot_module->name_cache)
890 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
891 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
893 *klass = g_hash_table_lookup (nspace_table, name);
900 table_size = aot_module->class_name_table [0];
901 table = aot_module->class_name_table + 1;
903 if (name_space [0] == '\0')
904 full_name = g_strdup_printf ("%s", name);
906 if (strlen (name_space) + strlen (name) < 1000) {
907 sprintf (full_name_buf, "%s.%s", name_space, name);
908 full_name = full_name_buf;
910 full_name = g_strdup_printf ("%s.%s", name_space, name);
913 hash = g_str_hash (full_name) % table_size;
914 if (full_name != full_name_buf)
917 entry = &table [hash * 2];
919 if (entry [0] != 0) {
920 t = &image->tables [MONO_TABLE_TYPEDEF];
923 guint32 index = entry [0];
924 guint32 next = entry [1];
925 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
927 name_table_accesses ++;
929 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
931 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
932 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
934 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
936 *klass = mono_class_get (image, token);
941 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
943 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
944 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
946 g_hash_table_insert (nspace_table, (char*)name2, *klass);
953 entry = &table [next * 2];
966 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
967 MonoMethod *method, guint8* ex_info, guint8 *code)
971 guint code_len, used_int_regs;
973 MonoMethodHeader *header;
975 header = mono_method_get_header (method);
977 /* Load the method info from the AOT file */
980 code_len = decode_value (p, &p);
981 used_int_regs = decode_value (p, &p);
983 /* Exception table */
984 if (header->num_clauses) {
986 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses));
987 jinfo->num_clauses = header->num_clauses;
989 for (i = 0; i < header->num_clauses; ++i) {
990 MonoExceptionClause *ec = &header->clauses [i];
991 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
993 ei->flags = ec->flags;
994 ei->exvar_offset = decode_value (p, &p);
996 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
997 ei->data.filter = code + decode_value (p, &p);
999 ei->data.catch_class = ec->data.catch_class;
1001 ei->try_start = code + decode_value (p, &p);
1002 ei->try_end = code + decode_value (p, &p);
1003 ei->handler_start = code + decode_value (p, &p);
1007 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
1009 jinfo->code_size = code_len;
1010 jinfo->used_regs = used_int_regs;
1011 jinfo->method = method;
1012 jinfo->code_start = code;
1013 jinfo->domain_neutral = 0;
1015 /* Load debug info */
1016 buf_len = decode_value (p, &p);
1017 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1023 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1025 MonoAssembly *ass = image->assembly;
1026 MonoDl *module = ass->aot_module;
1027 int pos, left, right, offset, offset1, offset2, last_offset, new_offset, page_index, method_index, table_len;
1029 MonoAotModule *aot_module;
1032 guint8 *code, *ex_info;
1033 guint32 *table, *ptr;
1039 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1041 if (domain != mono_get_root_domain ())
1045 offset = (guint8*)addr - aot_module->code;
1047 /* First search through the index */
1048 ptr = aot_module->method_order;
1053 if (*ptr == 0xffffff)
1057 while (*ptr != 0xffffff) {
1058 guint32 method_index = ptr [0];
1059 new_offset = aot_module->code_offsets [method_index];
1061 if (offset >= last_offset && offset < new_offset) {
1067 last_offset = new_offset;
1071 /* Skip rest of index */
1072 while (*ptr != 0xffffff)
1077 table_len = aot_module->method_order_end - table;
1079 g_assert (table <= aot_module->method_order_end);
1082 left = (page_index * 1024);
1083 right = left + 1024;
1085 if (right > table_len)
1088 offset1 = aot_module->code_offsets [table [left]];
1089 g_assert (offset1 <= offset);
1091 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1094 //printf ("Not found in index: 0x%x\n", offset);
1099 /* Binary search inside the method_order table to find the method */
1101 pos = (left + right) / 2;
1103 g_assert (table + pos <= aot_module->method_order_end);
1105 //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]]);
1107 offset1 = aot_module->code_offsets [table [pos]];
1108 if (table + pos + 1 >= aot_module->method_order_end)
1109 offset2 = aot_module->code_end - aot_module->code;
1111 offset2 = aot_module->code_offsets [table [pos + 1]];
1113 if (offset < offset1)
1115 else if (offset >= offset2)
1121 method_index = table [pos];
1123 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1124 method = mono_get_method (image, token, NULL);
1129 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1131 code = &aot_module->code [aot_module->code_offsets [method_index]];
1132 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1134 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1136 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1137 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1139 /* Add it to the normal JitInfo tables */
1140 mono_jit_info_table_add (domain, jinfo);
1146 decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1154 case MONO_PATCH_INFO_METHOD:
1155 case MONO_PATCH_INFO_METHODCONST:
1156 case MONO_PATCH_INFO_METHOD_JUMP:
1157 case MONO_PATCH_INFO_ICALL_ADDR: {
1160 image = decode_method_ref (aot_module, &token, p, &p);
1164 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1165 if ((ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1166 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1167 ji->type = MONO_PATCH_INFO_ABS;
1170 ji->data.method = mono_get_method (image, token, NULL);
1171 g_assert (ji->data.method);
1172 mono_class_init (ji->data.method->klass);
1175 ji->data.method = mono_get_method (image, token, NULL);
1176 g_assert (ji->data.method);
1177 mono_class_init (ji->data.method->klass);
1182 case MONO_PATCH_INFO_WRAPPER: {
1183 guint32 wrapper_type;
1185 wrapper_type = decode_value (p, &p);
1187 switch (wrapper_type) {
1188 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
1189 guint32 image_index, token, value;
1191 value = decode_value (p, &p);
1192 image_index = value >> 24;
1193 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1195 image = load_image (aot_module, image_index);
1198 ji->data.method = mono_get_method (image, token, NULL);
1199 g_assert (ji->data.method);
1200 mono_class_init (ji->data.method->klass);
1202 ji->type = MONO_PATCH_INFO_METHOD;
1203 ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
1206 case MONO_WRAPPER_PROXY_ISINST: {
1207 MonoClass *klass = decode_klass_ref (aot_module, p, &p);
1210 ji->type = MONO_PATCH_INFO_METHOD;
1211 ji->data.method = mono_marshal_get_proxy_cancast (klass);
1214 case MONO_WRAPPER_LDFLD:
1215 case MONO_WRAPPER_LDFLDA:
1216 case MONO_WRAPPER_STFLD:
1217 case MONO_WRAPPER_ISINST: {
1218 MonoClass *klass = decode_klass_ref (aot_module, p, &p);
1221 ji->type = MONO_PATCH_INFO_METHOD;
1222 if (wrapper_type == MONO_WRAPPER_LDFLD)
1223 ji->data.method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
1224 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
1225 ji->data.method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
1226 else if (wrapper_type == MONO_WRAPPER_STFLD)
1227 ji->data.method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
1228 else if (wrapper_type == MONO_WRAPPER_ISINST)
1229 ji->data.method = mono_marshal_get_isinst (klass);
1231 g_assert_not_reached ();
1234 case MONO_WRAPPER_LDFLD_REMOTE:
1235 ji->data.method = mono_marshal_get_ldfld_remote_wrapper (NULL);
1237 case MONO_WRAPPER_STFLD_REMOTE:
1238 ji->data.method = mono_marshal_get_stfld_remote_wrapper (NULL);
1240 case MONO_WRAPPER_ALLOC: {
1241 int atype = decode_value (p, &p);
1243 ji->type = MONO_PATCH_INFO_METHOD;
1244 ji->data.method = mono_gc_get_managed_allocator_by_type (atype);
1247 case MONO_WRAPPER_STELEMREF:
1248 ji->type = MONO_PATCH_INFO_METHOD;
1249 ji->data.method = mono_marshal_get_stelemref ();
1252 g_assert_not_reached ();
1256 case MONO_PATCH_INFO_INTERNAL_METHOD: {
1257 guint32 len = decode_value (p, &p);
1259 ji->data.name = (char*)p;
1263 case MONO_PATCH_INFO_VTABLE:
1264 case MONO_PATCH_INFO_CLASS:
1265 case MONO_PATCH_INFO_IID:
1266 case MONO_PATCH_INFO_ADJUSTED_IID:
1267 *got_offset = decode_value (p, &p);
1269 if (aot_module->got [*got_offset]) {
1270 /* Already loaded */
1271 //printf ("HIT!\n");
1273 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1274 ji->data.klass = decode_klass_ref (aot_module, tmp, &tmp);
1275 if (!ji->data.klass)
1279 case MONO_PATCH_INFO_CLASS_INIT:
1280 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1281 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1282 if (!ji->data.klass)
1285 case MONO_PATCH_INFO_IMAGE:
1286 ji->data.image = load_image (aot_module, decode_value (p, &p));
1287 if (!ji->data.image)
1290 case MONO_PATCH_INFO_FIELD:
1291 case MONO_PATCH_INFO_SFLDA:
1292 *got_offset = decode_value (p, &p);
1294 if (aot_module->got [*got_offset]) {
1295 /* Already loaded */
1296 //printf ("HIT2!\n");
1298 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1299 ji->data.field = decode_field_info (aot_module, tmp, &tmp);
1300 if (!ji->data.field)
1304 case MONO_PATCH_INFO_SWITCH:
1305 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1306 ji->data.table->table_size = decode_value (p, &p);
1307 table = g_new (gpointer, ji->data.table->table_size);
1308 ji->data.table->table = (MonoBasicBlock**)table;
1309 for (i = 0; i < ji->data.table->table_size; i++)
1310 table [i] = (gpointer)(gssize)decode_value (p, &p);
1312 case MONO_PATCH_INFO_R4: {
1315 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1316 val = decode_value (p, &p);
1317 *(float*)ji->data.target = *(float*)&val;
1320 case MONO_PATCH_INFO_R8: {
1323 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1325 val [0] = decode_value (p, &p);
1326 val [1] = decode_value (p, &p);
1327 *(double*)ji->data.target = *(double*)val;
1330 case MONO_PATCH_INFO_LDSTR:
1331 image = load_image (aot_module, decode_value (p, &p));
1334 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1336 case MONO_PATCH_INFO_RVA:
1337 case MONO_PATCH_INFO_DECLSEC:
1338 case MONO_PATCH_INFO_LDTOKEN:
1339 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1340 *got_offset = decode_value (p, &p);
1342 if (aot_module->got [*got_offset]) {
1343 /* Already loaded */
1345 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1346 image = load_image (aot_module, decode_value (tmp, &tmp));
1349 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (tmp, &tmp));
1352 case MONO_PATCH_INFO_EXC_NAME:
1353 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1354 if (!ji->data.klass)
1356 ji->data.name = ji->data.klass->name;
1358 case MONO_PATCH_INFO_METHOD_REL:
1359 ji->data.offset = decode_value (p, &p);
1362 g_warning ("unhandled type %d", ji->type);
1363 g_assert_not_reached ();
1374 static MonoJumpInfo*
1375 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1376 guint32 got_index, guint32 **got_slots,
1377 guint8 *buf, guint8 **endbuf)
1379 MonoJumpInfo *patches;
1380 MonoJumpInfo *patch_info = NULL;
1382 guint32 last_offset;
1387 /* First load the type + offset table */
1389 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1391 for (pindex = 0; pindex < n_patches; ++pindex) {
1392 MonoJumpInfo *ji = &patches [pindex];
1397 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1398 ji->next = patch_info;
1402 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1403 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1405 /* Then load the other data */
1406 for (pindex = 0; pindex < n_patches; ++pindex) {
1407 MonoJumpInfo *ji = &patches [pindex];
1409 if (!decode_patch_info (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1412 if ((*got_slots) [pindex] == 0xffffffff)
1413 (*got_slots) [pindex] = got_index ++;
1420 g_free (*got_slots);
1427 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
1429 MonoClass *klass = method->klass;
1430 MonoAssembly *ass = klass->image->assembly;
1431 MonoDl *module = ass->aot_module;
1432 guint32 method_index = mono_metadata_token_index (method->token) - 1;
1433 guint8 *code, *info;
1434 MonoAotModule *aot_module;
1442 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1445 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1446 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1447 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1448 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1451 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1453 g_assert (klass->inited);
1455 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1456 /* Non shared AOT code can't be used in other appdomains */
1459 if (aot_module->out_of_date)
1462 if (method->is_inflated) {
1463 if (!mono_method_is_generic_sharable_impl (method))
1465 method = mono_method_get_declaring_generic_method (method);
1468 if (aot_module->code_offsets [method_index] == 0xffffffff) {
1469 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1470 char *full_name = mono_method_full_name (method, TRUE);
1471 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1477 code = &aot_module->code [aot_module->code_offsets [method_index]];
1478 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1480 if (!aot_module->methods_loaded)
1481 aot_module->methods_loaded = g_new0 (guint32, klass->image->tables [MONO_TABLE_METHOD].rows + 1);
1483 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1486 if (mono_last_aot_method != -1) {
1487 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1490 if (mono_jit_stats.methods_aot == mono_last_aot_method)
1491 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
1494 return mono_aot_load_method (domain, aot_module, method, code, info);
1498 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info)
1500 MonoClass *klass = method->klass;
1501 MonoJumpInfo *patch_info = NULL;
1503 int i, pindex, got_index = 0, n_patches, used_strings;
1504 gboolean non_got_patches, keep_patches = TRUE;
1505 guint32 method_index = mono_metadata_token_index (method->token) - 1;
1506 guint8 *p, *ex_info;
1507 MonoJitInfo *jinfo = NULL;
1510 decode_klass_ref (aot_module, p, &p);
1512 if (!use_loaded_code) {
1516 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1517 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1520 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1521 memcpy (code2, code, jinfo->code_size);
1522 mono_arch_flush_icache (code2, jinfo->code_size);
1526 if (aot_module->opts & MONO_OPT_SHARED)
1527 used_strings = decode_value (p, &p);
1531 for (i = 0; i < used_strings; i++) {
1532 guint token = decode_value (p, &p);
1533 mono_ldstr (mono_get_root_domain (), klass->image, mono_metadata_token_index (token));
1536 if (aot_module->opts & MONO_OPT_SHARED)
1537 keep_patches = FALSE;
1539 n_patches = decode_value (p, &p);
1541 keep_patches = FALSE;
1544 MonoJumpInfo *patches;
1550 mp = mono_mempool_new ();
1552 got_index = decode_value (p, &p);
1554 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1555 if (patches == NULL)
1558 /* Do this outside the lock to avoid deadlocks */
1560 non_got_patches = FALSE;
1561 for (pindex = 0; pindex < n_patches; ++pindex) {
1562 MonoJumpInfo *ji = &patches [pindex];
1564 if (is_got_patch (ji->type)) {
1565 if (!aot_module->got [got_slots [pindex]])
1566 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
1567 ji->type = MONO_PATCH_INFO_NONE;
1570 non_got_patches = TRUE;
1572 if (non_got_patches) {
1574 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1575 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1578 mono_arch_flush_icache (code, jinfo->code_size);
1579 make_writable (code, jinfo->code_size);
1580 mono_arch_patch_code (method, domain, code, patch_info, TRUE);
1587 mono_mempool_destroy (mp);
1590 mono_jit_stats.methods_aot++;
1592 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1593 char *full_name = mono_method_full_name (method, TRUE);
1596 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1597 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1600 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);
1604 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1606 init_plt (aot_module);
1611 /* FIXME: The space in domain->mp is wasted */
1612 if (aot_module->opts & MONO_OPT_SHARED)
1613 /* No need to cache patches */
1614 mono_mempool_destroy (mp);
1623 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1628 code = mono_aot_get_method_inner (domain, method);
1635 mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guint32 token, MonoClass **klass)
1637 MonoAssembly *ass = image->assembly;
1639 int i, method_index, pindex, got_index, n_patches, used_strings;
1640 gboolean keep_patches = TRUE;
1642 MonoDl *module = ass->aot_module;
1643 guint8 *code = NULL;
1645 MonoAotModule *aot_module;
1652 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1655 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1657 if (domain != mono_get_root_domain ())
1660 if (aot_module->out_of_date)
1663 if (aot_module->code_offsets [mono_metadata_token_index (token) - 1] == 0xffffffff) {
1667 method_index = mono_metadata_token_index (token) - 1;
1668 code = &aot_module->code [aot_module->code_offsets [method_index]];
1669 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1671 if (!aot_module->methods_loaded)
1672 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1674 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1677 if (mono_last_aot_method != -1) {
1678 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1681 if (mono_jit_stats.methods_aot == mono_last_aot_method) {
1682 MonoMethod *method = mono_get_method (image, token, NULL);
1683 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1688 *klass = decode_klass_ref (aot_module, p, &p);
1690 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1691 MonoMethod *method = mono_get_method (image, token, NULL);
1692 char *full_name = mono_method_full_name (method, TRUE);
1693 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p %p\n", full_name, code, info);
1697 if (aot_module->opts & MONO_OPT_SHARED)
1698 used_strings = decode_value (p, &p);
1702 for (i = 0; i < used_strings; i++) {
1703 guint string_token = decode_value (p, &p);
1704 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (string_token));
1707 if (aot_module->opts & MONO_OPT_SHARED)
1708 keep_patches = FALSE;
1710 keep_patches = FALSE;
1712 n_patches = decode_value (p, &p);
1715 MonoJumpInfo *patches;
1721 mp = mono_mempool_new ();
1723 got_index = decode_value (p, &p);
1725 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1726 if (patches == NULL)
1729 /* Do this outside the lock to avoid deadlocks */
1732 for (pindex = 0; pindex < n_patches; ++pindex) {
1733 MonoJumpInfo *ji = &patches [pindex];
1735 if (is_got_patch (ji->type)) {
1736 if (!aot_module->got [got_slots [pindex]])
1737 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (NULL, domain, code, ji, TRUE);
1738 ji->type = MONO_PATCH_INFO_NONE;
1747 mono_mempool_destroy (mp);
1750 mono_jit_stats.methods_aot++;
1752 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1754 init_plt (aot_module);
1759 /* FIXME: The space in domain->mp is wasted */
1760 if (aot_module->opts & MONO_OPT_SHARED)
1761 /* No need to cache patches */
1762 mono_mempool_destroy (mp);
1768 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
1772 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1778 res = mono_aot_get_method_from_token_inner (domain, image, token, &klass);
1785 mono_runtime_class_init (mono_class_vtable (domain, klass));
1793 } IsGotEntryUserData;
1796 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
1798 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
1799 MonoAotModule *aot_module = (MonoAotModule*)value;
1801 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
1806 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1808 IsGotEntryUserData user_data;
1813 user_data.addr = addr;
1814 user_data.res = FALSE;
1816 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
1819 return user_data.res;
1824 MonoAotModule *module;
1825 } FindAotModuleUserData;
1828 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
1830 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
1831 MonoAotModule *aot_module = (MonoAotModule*)value;
1833 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
1834 data->module = aot_module;
1837 static inline MonoAotModule*
1838 find_aot_module (guint8 *code)
1840 FindAotModuleUserData user_data;
1845 /* Reading these need no locking */
1846 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
1849 user_data.addr = code;
1850 user_data.module = NULL;
1853 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
1856 return user_data.module;
1860 * mono_aot_set_make_unreadable:
1862 * Set whenever to make all mmaped memory unreadable. In conjuction with a
1863 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
1866 mono_aot_set_make_unreadable (gboolean unreadable)
1868 make_unreadable = unreadable;
1872 MonoAotModule *module;
1877 find_map (gpointer key, gpointer value, gpointer user_data)
1879 MonoAotModule *module = (MonoAotModule*)value;
1880 FindMapUserData *data = (FindMapUserData*)user_data;
1883 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
1884 data->module = module;
1887 static MonoAotModule*
1888 find_module_for_addr (void *ptr)
1890 FindMapUserData data;
1892 if (!make_unreadable)
1896 data.ptr = (guint8*)ptr;
1899 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
1906 * mono_aot_is_pagefault:
1908 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
1909 * within memory allocated by this module.
1912 mono_aot_is_pagefault (void *ptr)
1914 if (!make_unreadable)
1917 return find_module_for_addr (ptr) != NULL;
1921 * mono_aot_handle_pagefault:
1923 * Handle a pagefault caused by an unreadable page by making it readable again.
1926 mono_aot_handle_pagefault (void *ptr)
1928 #ifndef PLATFORM_WIN32
1929 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
1933 res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
1934 g_assert (res == 0);
1945 printf ("\nNative stacktrace:\n\n");
1947 size = backtrace (array, 256);
1948 names = backtrace_symbols (array, size);
1949 for (i =0; i < size; ++i) {
1950 printf ("\t%s\n", names [i]);
1960 * mono_aot_plt_resolve:
1962 * This function is called by the entries in the PLT to resolve the actual method that
1963 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
1966 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
1968 #ifdef MONO_ARCH_AOT_SUPPORTED
1969 guint8 *p, *target, *plt_entry;
1971 MonoAotModule *module = (MonoAotModule*)aot_module;
1974 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
1976 p = &module->plt_info [plt_info_offset];
1978 ji.type = decode_value (p, &p);
1980 res = decode_patch_info (module, NULL, &ji, p, &p, NULL);
1981 // FIXME: Error handling (how ?)
1984 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
1986 /* Patch the PLT entry with target which might be the actual method not a trampoline */
1987 plt_entry = mono_aot_get_plt_entry (code);
1988 g_assert (plt_entry);
1989 mono_arch_patch_plt_entry (plt_entry, target);
1993 g_assert_not_reached ();
2001 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2002 * method in the module is loaded to avoid committing memory by writing to it.
2003 * LOCKING: Assumes the AOT lock is held.
2006 init_plt (MonoAotModule *info)
2008 #ifdef MONO_ARCH_AOT_SUPPORTED
2010 guint8 *buf = info->plt;
2011 #elif defined(__x86_64__)
2013 #elif defined(__arm__)
2018 if (info->plt_inited)
2021 tramp = mono_arch_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2024 /* Initialize the first PLT entry */
2025 make_writable (info->plt, info->plt_end - info->plt);
2026 x86_jump_code (buf, tramp);
2027 #elif defined(__x86_64__)
2029 * Initialize the entries in the plt_jump_table to point to the default targets.
2031 n_entries = info->plt_jump_table_size / sizeof (gpointer);
2033 /* The first entry points to the AOT trampoline */
2034 ((gpointer*)info->plt_jump_table)[0] = tramp;
2035 for (i = 1; i < n_entries; ++i)
2036 /* Each PLT entry is 16 bytes long, the default entry begins at offset 6 */
2037 ((gpointer*)info->plt_jump_table)[i] = info->plt + (i * 16) + 6;
2038 #elif defined(__arm__)
2039 /* Initialize the first PLT entry */
2040 make_writable (info->plt, info->plt_end - info->plt);
2041 ((guint32*)info->plt)[1] = (guint32)tramp;
2043 n_entries = ((guint8*)info->plt_end - (guint8*)info->plt) / 8;
2046 * Initialize the jump targets embedded inside the PLT entries to the default
2049 for (i = 1; i < n_entries; ++i)
2050 /* Each PLT entry is 8 bytes long, the jump target is at offset 4 */
2051 /* Each default PLT target is 12 bytes long */
2052 ((guint32*)info->plt)[(i * 2) + 1] = (guint8*)info->plt_end + ((i - 1) * 12);
2054 g_assert_not_reached ();
2057 info->plt_inited = TRUE;
2062 * mono_aot_get_plt_entry:
2064 * Return the address of the PLT entry called by the code at CODE if exists.
2067 mono_aot_get_plt_entry (guint8 *code)
2069 MonoAotModule *aot_module = find_aot_module (code);
2070 #if defined(__arm__)
2077 #if defined(__i386__) || defined(__x86_64__)
2078 if (code [-5] == 0xe8) {
2079 guint32 disp = *(guint32*)(code - 4);
2080 guint8 *target = code + disp;
2082 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2085 #elif defined(__arm__)
2086 ins = ((guint32*)(gpointer)code) [-1];
2088 /* Should be a 'bl' */
2089 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2090 gint32 disp = ((gint32)ins) & 0xffffff;
2091 guint8 *target = code - 4 + 8 + (disp * 4);
2093 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2097 g_assert_not_reached ();
2104 * mono_aot_get_n_pagefaults:
2106 * Return the number of times handle_pagefault is called.
2109 mono_aot_get_n_pagefaults (void)
2111 return n_pagefaults;
2118 mono_aot_init (void)
2123 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2129 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2135 mono_aot_init_vtable (MonoVTable *vtable)
2141 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2147 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2153 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2159 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2165 mono_aot_is_pagefault (void *ptr)
2171 mono_aot_set_make_unreadable (gboolean unreadable)
2176 mono_aot_get_n_pagefaults (void)
2182 mono_aot_handle_pagefault (void *ptr)
2187 mono_aot_get_plt_entry (guint8 *code)
2193 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2199 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)