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)
734 MonoAotModule *aot_module;
735 MonoClass *klass = vtable->klass;
737 MonoCachedClassInfo class_info;
740 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !klass->image->assembly->aot_module)
745 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
751 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
754 err = decode_cached_class_info (aot_module, &class_info, p, &p);
762 #ifdef MONO_ARCH_COMMON_VTABLE_TRAMPOLINE
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: {
1159 image = decode_method_ref (aot_module, &token, p, &p);
1163 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1164 if ((ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1165 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1166 ji->type = MONO_PATCH_INFO_ABS;
1169 ji->data.method = mono_get_method (image, token, NULL);
1170 g_assert (ji->data.method);
1171 mono_class_init (ji->data.method->klass);
1174 ji->data.method = mono_get_method (image, token, NULL);
1175 g_assert (ji->data.method);
1176 mono_class_init (ji->data.method->klass);
1181 case MONO_PATCH_INFO_WRAPPER: {
1182 guint32 wrapper_type;
1184 wrapper_type = decode_value (p, &p);
1186 switch (wrapper_type) {
1187 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
1188 guint32 image_index, token, value;
1190 value = decode_value (p, &p);
1191 image_index = value >> 24;
1192 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1194 image = load_image (aot_module, image_index);
1197 ji->data.method = mono_get_method (image, token, NULL);
1198 g_assert (ji->data.method);
1199 mono_class_init (ji->data.method->klass);
1201 ji->type = MONO_PATCH_INFO_METHOD;
1202 ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
1205 case MONO_WRAPPER_PROXY_ISINST: {
1206 MonoClass *klass = decode_klass_ref (aot_module, p, &p);
1209 ji->type = MONO_PATCH_INFO_METHOD;
1210 ji->data.method = mono_marshal_get_proxy_cancast (klass);
1213 case MONO_WRAPPER_LDFLD:
1214 case MONO_WRAPPER_LDFLDA:
1215 case MONO_WRAPPER_STFLD:
1216 case MONO_WRAPPER_LDFLD_REMOTE:
1217 case MONO_WRAPPER_STFLD_REMOTE:
1218 case MONO_WRAPPER_ISINST: {
1219 MonoClass *klass = decode_klass_ref (aot_module, p, &p);
1222 ji->type = MONO_PATCH_INFO_METHOD;
1223 if (wrapper_type == MONO_WRAPPER_LDFLD)
1224 ji->data.method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
1225 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
1226 ji->data.method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
1227 else if (wrapper_type == MONO_WRAPPER_STFLD)
1228 ji->data.method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
1229 else if (wrapper_type == MONO_WRAPPER_LDFLD_REMOTE)
1230 ji->data.method = mono_marshal_get_ldfld_remote_wrapper (klass);
1231 else if (wrapper_type == MONO_WRAPPER_STFLD_REMOTE)
1232 ji->data.method = mono_marshal_get_stfld_remote_wrapper (klass);
1233 else if (wrapper_type == MONO_WRAPPER_ISINST)
1234 ji->data.method = mono_marshal_get_isinst (klass);
1236 g_assert_not_reached ();
1239 case MONO_WRAPPER_ALLOC: {
1240 int atype = decode_value (p, &p);
1242 ji->type = MONO_PATCH_INFO_METHOD;
1243 ji->data.method = mono_gc_get_managed_allocator_by_type (atype);
1246 case MONO_WRAPPER_STELEMREF:
1247 ji->type = MONO_PATCH_INFO_METHOD;
1248 ji->data.method = mono_marshal_get_stelemref ();
1251 g_assert_not_reached ();
1255 case MONO_PATCH_INFO_INTERNAL_METHOD: {
1256 guint32 len = decode_value (p, &p);
1258 ji->data.name = (char*)p;
1262 case MONO_PATCH_INFO_VTABLE:
1263 case MONO_PATCH_INFO_CLASS:
1264 case MONO_PATCH_INFO_IID:
1265 case MONO_PATCH_INFO_ADJUSTED_IID:
1266 *got_offset = decode_value (p, &p);
1268 if (aot_module->got [*got_offset]) {
1269 /* Already loaded */
1270 //printf ("HIT!\n");
1272 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1273 ji->data.klass = decode_klass_ref (aot_module, tmp, &tmp);
1274 if (!ji->data.klass)
1278 case MONO_PATCH_INFO_CLASS_INIT:
1279 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1280 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1281 if (!ji->data.klass)
1284 case MONO_PATCH_INFO_IMAGE:
1285 ji->data.image = load_image (aot_module, decode_value (p, &p));
1286 if (!ji->data.image)
1289 case MONO_PATCH_INFO_FIELD:
1290 case MONO_PATCH_INFO_SFLDA:
1291 *got_offset = decode_value (p, &p);
1293 if (aot_module->got [*got_offset]) {
1294 /* Already loaded */
1295 //printf ("HIT2!\n");
1297 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1298 ji->data.field = decode_field_info (aot_module, tmp, &tmp);
1299 if (!ji->data.field)
1303 case MONO_PATCH_INFO_SWITCH:
1304 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1305 ji->data.table->table_size = decode_value (p, &p);
1306 table = g_new (gpointer, ji->data.table->table_size);
1307 ji->data.table->table = (MonoBasicBlock**)table;
1308 for (i = 0; i < ji->data.table->table_size; i++)
1309 table [i] = (gpointer)(gssize)decode_value (p, &p);
1311 case MONO_PATCH_INFO_R4: {
1314 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1315 val = decode_value (p, &p);
1316 *(float*)ji->data.target = *(float*)&val;
1319 case MONO_PATCH_INFO_R8: {
1322 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1324 val [0] = decode_value (p, &p);
1325 val [1] = decode_value (p, &p);
1326 *(double*)ji->data.target = *(double*)val;
1329 case MONO_PATCH_INFO_LDSTR:
1330 image = load_image (aot_module, decode_value (p, &p));
1333 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1335 case MONO_PATCH_INFO_RVA:
1336 case MONO_PATCH_INFO_DECLSEC:
1337 case MONO_PATCH_INFO_LDTOKEN:
1338 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1339 *got_offset = decode_value (p, &p);
1341 if (aot_module->got [*got_offset]) {
1342 /* Already loaded */
1344 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1345 image = load_image (aot_module, decode_value (tmp, &tmp));
1348 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (tmp, &tmp));
1351 case MONO_PATCH_INFO_EXC_NAME:
1352 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1353 if (!ji->data.klass)
1355 ji->data.name = ji->data.klass->name;
1357 case MONO_PATCH_INFO_METHOD_REL:
1358 ji->data.offset = decode_value (p, &p);
1361 g_warning ("unhandled type %d", ji->type);
1362 g_assert_not_reached ();
1373 static MonoJumpInfo*
1374 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1375 guint32 got_index, guint32 **got_slots,
1376 guint8 *buf, guint8 **endbuf)
1378 MonoJumpInfo *patches;
1379 MonoJumpInfo *patch_info = NULL;
1381 guint32 last_offset;
1386 /* First load the type + offset table */
1388 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1390 for (pindex = 0; pindex < n_patches; ++pindex) {
1391 MonoJumpInfo *ji = &patches [pindex];
1396 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1397 ji->next = patch_info;
1401 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1402 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1404 /* Then load the other data */
1405 for (pindex = 0; pindex < n_patches; ++pindex) {
1406 MonoJumpInfo *ji = &patches [pindex];
1408 if (!decode_patch_info (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1411 if ((*got_slots) [pindex] == 0xffffffff)
1412 (*got_slots) [pindex] = got_index ++;
1419 g_free (*got_slots);
1426 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
1428 MonoClass *klass = method->klass;
1429 MonoAssembly *ass = klass->image->assembly;
1430 MonoDl *module = ass->aot_module;
1431 guint32 method_index = mono_metadata_token_index (method->token) - 1;
1432 guint8 *code, *info;
1433 MonoAotModule *aot_module;
1441 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1444 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1445 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1446 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1447 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1450 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1452 g_assert (klass->inited);
1454 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1455 /* Non shared AOT code can't be used in other appdomains */
1458 if (aot_module->out_of_date)
1461 if (method->is_inflated) {
1462 if (!mono_method_is_generic_sharable_impl (method))
1464 method = mono_method_get_declaring_generic_method (method);
1467 if (aot_module->code_offsets [method_index] == 0xffffffff) {
1468 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1469 char *full_name = mono_method_full_name (method, TRUE);
1470 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1476 code = &aot_module->code [aot_module->code_offsets [method_index]];
1477 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1479 if (!aot_module->methods_loaded)
1480 aot_module->methods_loaded = g_new0 (guint32, klass->image->tables [MONO_TABLE_METHOD].rows + 1);
1482 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1485 if (mono_last_aot_method != -1) {
1486 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1489 if (mono_jit_stats.methods_aot == mono_last_aot_method)
1490 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
1493 return mono_aot_load_method (domain, aot_module, method, code, info);
1497 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info)
1499 MonoClass *klass = method->klass;
1500 MonoJumpInfo *patch_info = NULL;
1502 int i, pindex, got_index = 0, n_patches, used_strings;
1503 gboolean non_got_patches, keep_patches = TRUE;
1504 guint32 method_index = mono_metadata_token_index (method->token) - 1;
1505 guint8 *p, *ex_info;
1506 MonoJitInfo *jinfo = NULL;
1509 decode_klass_ref (aot_module, p, &p);
1511 if (!use_loaded_code) {
1515 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1516 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1519 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1520 memcpy (code2, code, jinfo->code_size);
1521 mono_arch_flush_icache (code2, jinfo->code_size);
1525 if (aot_module->opts & MONO_OPT_SHARED)
1526 used_strings = decode_value (p, &p);
1530 for (i = 0; i < used_strings; i++) {
1531 guint token = decode_value (p, &p);
1532 mono_ldstr (mono_get_root_domain (), klass->image, mono_metadata_token_index (token));
1535 if (aot_module->opts & MONO_OPT_SHARED)
1536 keep_patches = FALSE;
1538 n_patches = decode_value (p, &p);
1540 keep_patches = FALSE;
1543 MonoJumpInfo *patches;
1549 mp = mono_mempool_new ();
1551 got_index = decode_value (p, &p);
1553 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1554 if (patches == NULL)
1557 /* Do this outside the lock to avoid deadlocks */
1559 non_got_patches = FALSE;
1560 for (pindex = 0; pindex < n_patches; ++pindex) {
1561 MonoJumpInfo *ji = &patches [pindex];
1563 if (is_got_patch (ji->type)) {
1564 if (!aot_module->got [got_slots [pindex]])
1565 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
1566 ji->type = MONO_PATCH_INFO_NONE;
1569 non_got_patches = TRUE;
1571 if (non_got_patches) {
1573 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1574 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1577 mono_arch_flush_icache (code, jinfo->code_size);
1578 make_writable (code, jinfo->code_size);
1579 mono_arch_patch_code (method, domain, code, patch_info, TRUE);
1586 mono_mempool_destroy (mp);
1589 mono_jit_stats.methods_aot++;
1591 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1592 char *full_name = mono_method_full_name (method, TRUE);
1595 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1596 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1599 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);
1603 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1605 init_plt (aot_module);
1610 /* FIXME: The space in domain->mp is wasted */
1611 if (aot_module->opts & MONO_OPT_SHARED)
1612 /* No need to cache patches */
1613 mono_mempool_destroy (mp);
1622 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1627 code = mono_aot_get_method_inner (domain, method);
1634 mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guint32 token, MonoClass **klass)
1636 MonoAssembly *ass = image->assembly;
1638 int i, method_index, pindex, got_index, n_patches, used_strings;
1639 gboolean keep_patches = TRUE;
1641 MonoDl *module = ass->aot_module;
1642 guint8 *code = NULL;
1644 MonoAotModule *aot_module;
1651 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1654 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1656 if (domain != mono_get_root_domain ())
1659 if (aot_module->out_of_date)
1662 if (aot_module->code_offsets [mono_metadata_token_index (token) - 1] == 0xffffffff) {
1666 method_index = mono_metadata_token_index (token) - 1;
1667 code = &aot_module->code [aot_module->code_offsets [method_index]];
1668 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1670 if (!aot_module->methods_loaded)
1671 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1673 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1676 if (mono_last_aot_method != -1) {
1677 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1680 if (mono_jit_stats.methods_aot == mono_last_aot_method) {
1681 MonoMethod *method = mono_get_method (image, token, NULL);
1682 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1687 *klass = decode_klass_ref (aot_module, p, &p);
1689 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1690 MonoMethod *method = mono_get_method (image, token, NULL);
1691 char *full_name = mono_method_full_name (method, TRUE);
1692 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p %p\n", full_name, code, info);
1696 if (aot_module->opts & MONO_OPT_SHARED)
1697 used_strings = decode_value (p, &p);
1701 for (i = 0; i < used_strings; i++) {
1702 guint string_token = decode_value (p, &p);
1703 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (string_token));
1706 if (aot_module->opts & MONO_OPT_SHARED)
1707 keep_patches = FALSE;
1709 keep_patches = FALSE;
1711 n_patches = decode_value (p, &p);
1714 MonoJumpInfo *patches;
1720 mp = mono_mempool_new ();
1722 got_index = decode_value (p, &p);
1724 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1725 if (patches == NULL)
1728 /* Do this outside the lock to avoid deadlocks */
1731 for (pindex = 0; pindex < n_patches; ++pindex) {
1732 MonoJumpInfo *ji = &patches [pindex];
1734 if (is_got_patch (ji->type)) {
1735 if (!aot_module->got [got_slots [pindex]])
1736 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (NULL, domain, code, ji, TRUE);
1737 ji->type = MONO_PATCH_INFO_NONE;
1746 mono_mempool_destroy (mp);
1749 mono_jit_stats.methods_aot++;
1751 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1753 init_plt (aot_module);
1758 /* FIXME: The space in domain->mp is wasted */
1759 if (aot_module->opts & MONO_OPT_SHARED)
1760 /* No need to cache patches */
1761 mono_mempool_destroy (mp);
1767 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
1771 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1777 res = mono_aot_get_method_from_token_inner (domain, image, token, &klass);
1784 mono_runtime_class_init (mono_class_vtable (domain, klass));
1792 } IsGotEntryUserData;
1795 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
1797 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
1798 MonoAotModule *aot_module = (MonoAotModule*)value;
1800 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
1805 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1807 IsGotEntryUserData user_data;
1812 user_data.addr = addr;
1813 user_data.res = FALSE;
1815 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
1818 return user_data.res;
1823 MonoAotModule *module;
1824 } FindAotModuleUserData;
1827 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
1829 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
1830 MonoAotModule *aot_module = (MonoAotModule*)value;
1832 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
1833 data->module = aot_module;
1836 static inline MonoAotModule*
1837 find_aot_module (guint8 *code)
1839 FindAotModuleUserData user_data;
1844 /* Reading these need no locking */
1845 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
1848 user_data.addr = code;
1849 user_data.module = NULL;
1852 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
1855 return user_data.module;
1859 * mono_aot_set_make_unreadable:
1861 * Set whenever to make all mmaped memory unreadable. In conjuction with a
1862 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
1865 mono_aot_set_make_unreadable (gboolean unreadable)
1867 make_unreadable = unreadable;
1871 MonoAotModule *module;
1876 find_map (gpointer key, gpointer value, gpointer user_data)
1878 MonoAotModule *module = (MonoAotModule*)value;
1879 FindMapUserData *data = (FindMapUserData*)user_data;
1882 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
1883 data->module = module;
1886 static MonoAotModule*
1887 find_module_for_addr (void *ptr)
1889 FindMapUserData data;
1891 if (!make_unreadable)
1895 data.ptr = (guint8*)ptr;
1898 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
1905 * mono_aot_is_pagefault:
1907 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
1908 * within memory allocated by this module.
1911 mono_aot_is_pagefault (void *ptr)
1913 if (!make_unreadable)
1916 return find_module_for_addr (ptr) != NULL;
1920 * mono_aot_handle_pagefault:
1922 * Handle a pagefault caused by an unreadable page by making it readable again.
1925 mono_aot_handle_pagefault (void *ptr)
1927 #ifndef PLATFORM_WIN32
1928 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
1932 res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
1933 g_assert (res == 0);
1944 printf ("\nNative stacktrace:\n\n");
1946 size = backtrace (array, 256);
1947 names = backtrace_symbols (array, size);
1948 for (i =0; i < size; ++i) {
1949 printf ("\t%s\n", names [i]);
1959 * mono_aot_plt_resolve:
1961 * This function is called by the entries in the PLT to resolve the actual method that
1962 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
1965 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
1967 #ifdef MONO_ARCH_AOT_SUPPORTED
1968 guint8 *p, *target, *plt_entry;
1970 MonoAotModule *module = (MonoAotModule*)aot_module;
1973 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
1975 p = &module->plt_info [plt_info_offset];
1977 ji.type = decode_value (p, &p);
1979 res = decode_patch_info (module, NULL, &ji, p, &p, NULL);
1980 // FIXME: Error handling (how ?)
1983 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
1985 /* Patch the PLT entry with target which might be the actual method not a trampoline */
1986 plt_entry = mono_aot_get_plt_entry (code);
1987 g_assert (plt_entry);
1988 mono_arch_patch_plt_entry (plt_entry, target);
1992 g_assert_not_reached ();
2000 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2001 * method in the module is loaded to avoid committing memory by writing to it.
2002 * LOCKING: Assumes the AOT lock is held.
2005 init_plt (MonoAotModule *info)
2007 #ifdef MONO_ARCH_AOT_SUPPORTED
2009 guint8 *buf = info->plt;
2010 #elif defined(__x86_64__)
2012 #elif defined(__arm__)
2017 if (info->plt_inited)
2020 tramp = mono_arch_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2023 /* Initialize the first PLT entry */
2024 make_writable (info->plt, info->plt_end - info->plt);
2025 x86_jump_code (buf, tramp);
2026 #elif defined(__x86_64__)
2028 * Initialize the entries in the plt_jump_table to point to the default targets.
2030 n_entries = info->plt_jump_table_size / sizeof (gpointer);
2032 /* The first entry points to the AOT trampoline */
2033 ((gpointer*)info->plt_jump_table)[0] = tramp;
2034 for (i = 1; i < n_entries; ++i)
2035 /* Each PLT entry is 16 bytes long, the default entry begins at offset 6 */
2036 ((gpointer*)info->plt_jump_table)[i] = info->plt + (i * 16) + 6;
2037 #elif defined(__arm__)
2038 /* Initialize the first PLT entry */
2039 make_writable (info->plt, info->plt_end - info->plt);
2040 ((guint32*)info->plt)[1] = (guint32)tramp;
2042 n_entries = ((guint8*)info->plt_end - (guint8*)info->plt) / 8;
2045 * Initialize the jump targets embedded inside the PLT entries to the default
2048 for (i = 1; i < n_entries; ++i)
2049 /* Each PLT entry is 8 bytes long, the jump target is at offset 4 */
2050 /* Each default PLT target is 12 bytes long */
2051 ((guint32*)info->plt)[(i * 2) + 1] = (guint8*)info->plt_end + ((i - 1) * 12);
2053 g_assert_not_reached ();
2056 info->plt_inited = TRUE;
2061 * mono_aot_get_plt_entry:
2063 * Return the address of the PLT entry called by the code at CODE if exists.
2066 mono_aot_get_plt_entry (guint8 *code)
2068 MonoAotModule *aot_module = find_aot_module (code);
2073 #if defined(__i386__) || defined(__x86_64__)
2074 if (code [-5] == 0xe8) {
2075 guint32 disp = *(guint32*)(code - 4);
2076 guint8 *target = code + disp;
2078 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2081 #elif defined(__arm__)
2082 guint32 ins = ((guint32*)code) [-1];
2084 /* Should be a 'bl' */
2085 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2086 gint32 disp = ((gint32)ins) & 0xffffff;
2087 guint8 *target = code - 4 + 8 + (disp * 4);
2089 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2093 g_assert_not_reached ();
2100 * mono_aot_get_n_pagefaults:
2102 * Return the number of times handle_pagefault is called.
2105 mono_aot_get_n_pagefaults (void)
2107 return n_pagefaults;
2114 mono_aot_init (void)
2119 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2125 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2131 mono_aot_init_vtable (MonoVTable *vtable)
2137 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2143 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2149 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2155 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2161 mono_aot_is_pagefault (void *ptr)
2167 mono_aot_set_make_unreadable (gboolean unreadable)
2172 mono_aot_get_n_pagefaults (void)
2178 mono_aot_handle_pagefault (void *ptr)
2183 mono_aot_get_plt_entry (guint8 *code)
2189 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2195 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)