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/utils/mono-logger.h>
50 #include "mono/utils/mono-compiler.h"
57 #define SHARED_EXT ".dll"
58 #elif defined(__ppc__) && defined(__MACH__)
59 #define SHARED_EXT ".dylib"
61 #define SHARED_EXT ".so"
64 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
65 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
67 typedef struct MonoAotModule {
69 /* Optimization flags used to compile the module */
71 /* Pointer to the Global Offset Table */
74 GHashTable *name_cache;
75 MonoAssemblyName *image_names;
77 MonoImage **image_table;
78 guint32 image_table_len;
88 guint8 *plt_jump_table;
89 guint32 plt_jump_table_size;
90 guint32 *code_offsets;
92 guint32 *method_info_offsets;
94 guint32 *got_info_offsets;
96 guint32 *ex_info_offsets;
97 guint32 *method_order;
98 guint32 *method_order_end;
100 guint32 *class_info_offsets;
101 guint32 *methods_loaded;
102 guint16 *class_name_table;
105 static GHashTable *aot_modules;
106 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
107 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
108 static CRITICAL_SECTION aot_mutex;
111 * Disabling this will make a copy of the loaded code and use the copy instead
112 * of the original. This will place the caller and the callee close to each
113 * other in memory, possibly improving cache behavior. Since the original
114 * code is in copy-on-write memory, this will not increase the memory usage
117 static gboolean use_loaded_code = TRUE;
120 * Whenever to AOT compile loaded assemblies on demand and store them in
121 * a cache under $HOME/.mono/aot-cache.
123 static gboolean use_aot_cache = FALSE;
126 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
127 * use_aot_cache is TRUE.
129 static gboolean spawn_compiler = TRUE;
132 static gint32 mono_last_aot_method = -1;
134 static gboolean make_unreadable = FALSE;
135 static guint32 n_pagefaults = 0;
136 static guint32 name_table_accesses = 0;
138 /* Used to speed-up find_aot_module () */
139 static gsize aot_code_low_addr = (gssize)-1;
140 static gsize aot_code_high_addr = 0;
143 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info);
146 init_plt (MonoAotModule *info);
148 static inline gboolean
149 is_got_patch (MonoJumpInfoType patch_type)
153 #elif defined(__i386__)
160 /*****************************************************/
162 /*****************************************************/
165 load_image (MonoAotModule *module, int index)
167 MonoAssembly *assembly;
168 MonoImageOpenStatus status;
170 g_assert (index < module->image_table_len);
172 if (module->image_table [index])
173 return module->image_table [index];
174 if (module->out_of_date)
177 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
179 module->out_of_date = TRUE;
183 if (strcmp (assembly->image->guid, module->image_guids [index])) {
184 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);
185 module->out_of_date = TRUE;
189 module->image_table [index] = assembly->image;
190 return assembly->image;
195 decode_value (guint8 *ptr, guint8 **rptr)
200 if ((b & 0x80) == 0){
203 } else if ((b & 0x40) == 0){
204 len = ((b & 0x3f) << 8 | ptr [1]);
206 } else if (b != 0xff) {
207 len = ((b & 0x1f) << 24) |
214 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
220 //printf ("DECODE: %d.\n", len);
225 decode_klass_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
229 guint32 token, rank, image_index;
231 token = decode_value (buf, &buf);
236 image_index = decode_value (buf, &buf);
237 image = load_image (module, image_index);
240 if (mono_metadata_token_table (token) == 0) {
241 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
242 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
243 klass = mono_class_get (image, token);
245 g_assert (mono_metadata_token_table (token) == MONO_TABLE_TYPEDEF);
246 token = MONO_TOKEN_TYPE_DEF + decode_value (buf, &buf);
247 rank = decode_value (buf, &buf);
248 if (token == MONO_TOKEN_TYPE_DEF) {
250 token = MONO_TOKEN_TYPE_DEF + decode_value (buf, &buf);
251 klass = mono_class_get (image, token);
253 klass = mono_array_class_get (klass, rank);
255 rank = decode_value (buf, &buf);
256 klass = mono_array_class_get (klass, rank);
258 klass = mono_class_get (image, token);
260 klass = mono_array_class_get (klass, rank);
264 mono_class_init (klass);
270 static MonoClassField*
271 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
273 MonoClass *klass = decode_klass_info (module, buf, &buf);
279 token = MONO_TOKEN_FIELD_DEF + decode_value (buf, &buf);
283 return mono_class_get_field (klass, token);
286 static inline MonoImage*
287 decode_method_ref (MonoAotModule *module, guint32 *token, guint8 *buf, guint8 **endbuf)
289 guint32 image_index, value;
292 value = decode_value (buf, &buf);
294 image_index = value >> 24;
295 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
297 if (image_index == 255) {
299 image_index = decode_value (buf, &buf);
300 *token = decode_value (buf, &buf);
303 image = load_image (module, image_index);
312 make_writable (guint8* addr, guint32 len)
314 #ifndef PLATFORM_WIN32
318 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
319 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
320 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
325 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
331 create_cache_structure (void)
337 home = g_get_home_dir ();
341 tmp = g_build_filename (home, ".mono", NULL);
342 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
343 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
344 #ifdef PLATFORM_WIN32
347 err = mkdir (tmp, 0777);
350 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
356 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
357 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
358 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
359 #ifdef PLATFORM_WIN32
362 err = mkdir (tmp, 0777);
365 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
374 * load_aot_module_from_cache:
376 * Experimental code to AOT compile loaded assemblies on demand.
379 * - Add environment variable MONO_AOT_CACHE_OPTIONS
380 * - Add options for controlling the cache size
381 * - Handle full cache by deleting old assemblies lru style
382 * - Add options for excluding assemblies during development
383 * - Maybe add a threshold after an assembly is AOT compiled
384 * - invoking a new mono process is a security risk
385 * - recompile the AOT module if one of its dependencies changes
388 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
390 char *fname, *cmd, *tmp2, *aot_options;
399 if (assembly->image->dynamic)
402 create_cache_structure ();
404 home = g_get_home_dir ();
406 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
407 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
411 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
412 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
415 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
417 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
419 aot_options = g_strdup_printf ("outfile=%s", fname);
421 if (spawn_compiler) {
422 /* FIXME: security */
423 /* FIXME: Has to pass the assembly loading path to the child process */
424 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
426 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
428 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__powerpc__)
430 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
431 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
433 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
440 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
442 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
444 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
448 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
450 g_free (aot_options);
457 load_aot_module (MonoAssembly *assembly, gpointer user_data)
461 gboolean usable = TRUE;
462 char *saved_guid = NULL;
463 char *aot_version = NULL;
464 char *opt_flags = NULL;
465 gpointer *plt_jump_table_addr = NULL;
466 guint32 *plt_jump_table_size = NULL;
467 gpointer *got_addr = NULL;
468 gpointer *got = NULL;
469 guint32 *got_size_ptr = NULL;
472 if (mono_compile_aot)
475 if (assembly->aot_module)
477 * Already loaded. This can happen because the assembly loading code might invoke
478 * the assembly load hooks multiple times for the same assembly.
483 assembly->aot_module = load_aot_module_from_cache (assembly, &aot_name);
486 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
488 assembly->aot_module = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
490 if (!assembly->aot_module) {
491 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
496 if (!assembly->aot_module) {
501 mono_dl_symbol (assembly->aot_module, "mono_assembly_guid", (gpointer *) &saved_guid);
502 mono_dl_symbol (assembly->aot_module, "mono_aot_version", (gpointer *) &aot_version);
503 mono_dl_symbol (assembly->aot_module, "mono_aot_opt_flags", (gpointer *)&opt_flags);
505 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
506 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);
510 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
511 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
518 mono_dl_close (assembly->aot_module);
519 assembly->aot_module = NULL;
523 mono_dl_symbol (assembly->aot_module, "got_addr", (gpointer *)&got_addr);
525 got = (gpointer*)*got_addr;
527 mono_dl_symbol (assembly->aot_module, "got_size", (gpointer *)&got_size_ptr);
528 g_assert (got_size_ptr);
530 info = g_new0 (MonoAotModule, 1);
531 info->aot_name = aot_name;
533 info->got_size = *got_size_ptr;
534 info->got [0] = assembly->image;
536 sscanf (opt_flags, "%d", &info->opts);
538 /* Read image table */
540 guint32 table_len, i;
543 mono_dl_symbol (assembly->aot_module, "mono_image_table", (gpointer *)&table);
546 table_len = *(guint32*)table;
547 table += sizeof (guint32);
548 info->image_table = g_new0 (MonoImage*, table_len);
549 info->image_names = g_new0 (MonoAssemblyName, table_len);
550 info->image_guids = g_new0 (char*, table_len);
551 info->image_table_len = table_len;
552 for (i = 0; i < table_len; ++i) {
553 MonoAssemblyName *aname = &(info->image_names [i]);
555 aname->name = g_strdup (table);
556 table += strlen (table) + 1;
557 info->image_guids [i] = g_strdup (table);
558 table += strlen (table) + 1;
560 aname->culture = g_strdup (table);
561 table += strlen (table) + 1;
562 memcpy (aname->public_key_token, table, strlen (table) + 1);
563 table += strlen (table) + 1;
565 table = ALIGN_PTR_TO (table, 8);
566 aname->flags = *(guint32*)table;
568 aname->major = *(guint32*)table;
570 aname->minor = *(guint32*)table;
572 aname->build = *(guint32*)table;
574 aname->revision = *(guint32*)table;
579 /* Read method and method_info tables */
580 mono_dl_symbol (assembly->aot_module, "method_offsets", (gpointer*)&info->code_offsets);
581 mono_dl_symbol (assembly->aot_module, "methods", (gpointer*)&info->code);
582 mono_dl_symbol (assembly->aot_module, "methods_end", (gpointer*)&info->code_end);
583 mono_dl_symbol (assembly->aot_module, "method_info_offsets", (gpointer*)&info->method_info_offsets);
584 mono_dl_symbol (assembly->aot_module, "method_info", (gpointer*)&info->method_info);
585 mono_dl_symbol (assembly->aot_module, "ex_info_offsets", (gpointer*)&info->ex_info_offsets);
586 mono_dl_symbol (assembly->aot_module, "ex_info", (gpointer*)&info->ex_info);
587 mono_dl_symbol (assembly->aot_module, "method_order", (gpointer*)&info->method_order);
588 mono_dl_symbol (assembly->aot_module, "method_order_end", (gpointer*)&info->method_order_end);
589 mono_dl_symbol (assembly->aot_module, "class_info", (gpointer*)&info->class_info);
590 mono_dl_symbol (assembly->aot_module, "class_info_offsets", (gpointer*)&info->class_info_offsets);
591 mono_dl_symbol (assembly->aot_module, "class_name_table", (gpointer *)&info->class_name_table);
592 mono_dl_symbol (assembly->aot_module, "got_info", (gpointer*)&info->got_info);
593 mono_dl_symbol (assembly->aot_module, "got_info_offsets", (gpointer*)&info->got_info_offsets);
594 mono_dl_symbol (assembly->aot_module, "mem_end", (gpointer*)&info->mem_end);
596 info->mem_begin = info->code;
598 mono_dl_symbol (assembly->aot_module, "plt", (gpointer*)&info->plt);
599 mono_dl_symbol (assembly->aot_module, "plt_end", (gpointer*)&info->plt_end);
600 mono_dl_symbol (assembly->aot_module, "plt_info", (gpointer*)&info->plt_info);
602 mono_dl_symbol (assembly->aot_module, "plt_jump_table_addr", (gpointer *)&plt_jump_table_addr);
603 g_assert (plt_jump_table_addr);
604 info->plt_jump_table = (guint8*)*plt_jump_table_addr;
605 g_assert (info->plt_jump_table);
607 mono_dl_symbol (assembly->aot_module, "plt_jump_table_size", (gpointer *)&plt_jump_table_size);
608 g_assert (plt_jump_table_size);
609 info->plt_jump_table_size = *plt_jump_table_size;
611 if (make_unreadable) {
612 #ifndef PLATFORM_WIN32
617 addr = info->mem_begin;
618 len = info->mem_end - info->mem_begin;
620 /* Round down in both directions to avoid modifying data which is not ours */
621 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
622 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
623 err = mprotect (page_start, pages * PAGESIZE, 0);
630 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)info->code);
631 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)info->code_end);
633 g_hash_table_insert (aot_modules, assembly, info);
636 mono_jit_info_add_aot_module (assembly->image, info->code, info->code_end);
639 * Since we store methoddef and classdef tokens when referring to methods/classes in
640 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
641 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
642 * non-lazily, since we can't handle out-of-date errors later.
644 for (i = 0; i < info->image_table_len; ++i)
645 load_image (info, i);
647 if (info->out_of_date)
648 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);
650 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
656 InitializeCriticalSection (&aot_mutex);
657 aot_modules = g_hash_table_new (NULL, NULL);
659 mono_install_assembly_load_hook (load_aot_module, NULL);
661 if (getenv ("MONO_LASTAOT"))
662 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
663 if (getenv ("MONO_AOT_CACHE"))
664 use_aot_cache = TRUE;
668 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
672 info->vtable_size = decode_value (buf, &buf);
673 if (info->vtable_size == -1)
676 flags = decode_value (buf, &buf);
677 info->ghcimpl = (flags >> 0) & 0x1;
678 info->has_finalize = (flags >> 1) & 0x1;
679 info->has_cctor = (flags >> 2) & 0x1;
680 info->has_nested_classes = (flags >> 3) & 0x1;
681 info->blittable = (flags >> 4) & 0x1;
682 info->has_references = (flags >> 5) & 0x1;
683 info->has_static_refs = (flags >> 6) & 0x1;
684 info->no_special_static_fields = (flags >> 7) & 0x1;
686 if (info->has_cctor) {
687 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, buf, &buf);
691 if (info->has_finalize) {
692 info->finalize_image = decode_method_ref (module, &info->finalize_token, buf, &buf);
693 if (!info->finalize_image)
697 info->instance_size = decode_value (buf, &buf);
698 info->class_size = decode_value (buf, &buf);
699 info->packing_size = decode_value (buf, &buf);
700 info->min_align = decode_value (buf, &buf);
708 mono_aot_init_vtable (MonoVTable *vtable)
711 MonoAotModule *aot_module;
712 MonoClass *klass = vtable->klass;
714 MonoCachedClassInfo class_info;
717 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !klass->image->assembly->aot_module)
722 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
728 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
731 err = decode_cached_class_info (aot_module, &class_info, p, &p);
737 //printf ("VT0: %s.%s %d\n", klass->name_space, klass->name, vtable_size);
738 for (i = 0; i < class_info.vtable_size; ++i) {
739 guint32 image_index, token, value;
741 #ifndef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
745 vtable->vtable [i] = 0;
747 value = decode_value (p, &p);
751 image_index = value >> 24;
752 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
754 image = load_image (aot_module, image_index);
760 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
761 vtable->vtable [i] = mono_create_jit_trampoline_from_token (image, token);
763 m = mono_get_method (image, token, NULL);
766 //printf ("M: %d %p %s\n", i, &(vtable->vtable [i]), mono_method_full_name (m, TRUE));
767 vtable->vtable [i] = mono_create_jit_trampoline (m);
777 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
779 MonoAotModule *aot_module;
783 if (klass->rank || !klass->image->assembly->aot_module)
788 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, klass->image->assembly);
794 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
796 err = decode_cached_class_info (aot_module, res, p, &p);
808 * mono_aot_get_class_from_name:
810 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
811 * using a cache stored in the AOT file.
812 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
814 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
818 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
820 MonoAotModule *aot_module;
821 guint16 *table, *entry;
824 char full_name_buf [1024];
826 const char *name2, *name_space2;
828 guint32 cols [MONO_TYPEDEF_SIZE];
829 GHashTable *nspace_table;
836 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, image->assembly);
837 if (!aot_module || !aot_module->class_name_table) {
844 /* First look in the cache */
845 if (!aot_module->name_cache)
846 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
847 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
849 *klass = g_hash_table_lookup (nspace_table, name);
856 table_size = aot_module->class_name_table [0];
857 table = aot_module->class_name_table + 1;
859 if (name_space [0] == '\0')
860 full_name = g_strdup_printf ("%s", name);
862 if (strlen (name_space) + strlen (name) < 1000) {
863 sprintf (full_name_buf, "%s.%s", name_space, name);
864 full_name = full_name_buf;
866 full_name = g_strdup_printf ("%s.%s", name_space, name);
869 hash = g_str_hash (full_name) % table_size;
870 if (full_name != full_name_buf)
873 entry = &table [hash * 2];
875 if (entry [0] != 0) {
876 t = &image->tables [MONO_TABLE_TYPEDEF];
879 guint32 index = entry [0];
880 guint32 next = entry [1];
881 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
883 name_table_accesses ++;
885 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
887 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
888 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
890 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
892 *klass = mono_class_get (image, token);
897 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
899 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
900 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
902 g_hash_table_insert (nspace_table, (char*)name2, *klass);
909 entry = &table [next * 2];
922 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
923 MonoMethod *method, guint8* ex_info, guint8 *code)
927 guint code_len, used_int_regs;
929 MonoMethodHeader *header;
931 header = mono_method_get_header (method);
933 /* Load the method info from the AOT file */
936 code_len = decode_value (p, &p);
937 used_int_regs = decode_value (p, &p);
939 /* Exception table */
940 if (header->num_clauses) {
942 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses));
943 jinfo->num_clauses = header->num_clauses;
945 for (i = 0; i < header->num_clauses; ++i) {
946 MonoExceptionClause *ec = &header->clauses [i];
947 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
949 ei->flags = ec->flags;
950 ei->exvar_offset = decode_value (p, &p);
952 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
953 ei->data.filter = code + decode_value (p, &p);
955 ei->data.catch_class = ec->data.catch_class;
957 ei->try_start = code + decode_value (p, &p);
958 ei->try_end = code + decode_value (p, &p);
959 ei->handler_start = code + decode_value (p, &p);
963 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo));
965 jinfo->code_size = code_len;
966 jinfo->used_regs = used_int_regs;
967 jinfo->method = method;
968 jinfo->code_start = code;
969 jinfo->domain_neutral = 0;
971 /* Load debug info */
972 buf_len = decode_value (p, &p);
973 mono_debug_add_aot_method (domain, method, code, p, buf_len);
979 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
981 MonoAssembly *ass = image->assembly;
982 MonoDl *module = ass->aot_module;
983 int pos, left, right, offset, offset1, offset2, last_offset, new_offset, page_index, method_index, table_len;
985 MonoAotModule *aot_module;
988 guint8 *code, *ex_info;
989 guint32 *table, *ptr;
995 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
997 if (domain != mono_get_root_domain ())
1001 offset = (guint8*)addr - aot_module->code;
1003 /* First search through the index */
1004 ptr = aot_module->method_order;
1009 if (*ptr == 0xffffff)
1013 while (*ptr != 0xffffff) {
1014 guint32 method_index = ptr [0];
1015 new_offset = aot_module->code_offsets [method_index];
1017 if (offset >= last_offset && offset < new_offset) {
1023 last_offset = new_offset;
1027 /* Skip rest of index */
1028 while (*ptr != 0xffffff)
1033 table_len = aot_module->method_order_end - table;
1035 g_assert (table <= aot_module->method_order_end);
1038 left = (page_index * 1024);
1039 right = left + 1024;
1041 if (right > table_len)
1044 offset1 = aot_module->code_offsets [table [left]];
1045 g_assert (offset1 <= offset);
1047 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1050 //printf ("Not found in index: 0x%x\n", offset);
1055 /* Binary search inside the method_order table to find the method */
1057 pos = (left + right) / 2;
1059 g_assert (table + pos <= aot_module->method_order_end);
1061 //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]]);
1063 offset1 = aot_module->code_offsets [table [pos]];
1064 if (table + pos + 1 >= aot_module->method_order_end)
1065 offset2 = aot_module->code_end - aot_module->code;
1067 offset2 = aot_module->code_offsets [table [pos + 1]];
1069 if (offset < offset1)
1071 else if (offset >= offset2)
1077 method_index = table [pos];
1079 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1080 method = mono_get_method (image, token, NULL);
1085 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1087 code = &aot_module->code [aot_module->code_offsets [method_index]];
1088 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1090 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1092 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1093 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1095 /* Add it to the normal JitInfo tables */
1096 mono_jit_info_table_add (domain, jinfo);
1102 decode_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1110 case MONO_PATCH_INFO_METHOD:
1111 case MONO_PATCH_INFO_METHODCONST:
1112 case MONO_PATCH_INFO_METHOD_JUMP: {
1115 image = decode_method_ref (aot_module, &token, buf, &buf);
1119 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1120 if ((ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1121 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1122 ji->type = MONO_PATCH_INFO_ABS;
1125 ji->data.method = mono_get_method (image, token, NULL);
1126 g_assert (ji->data.method);
1127 mono_class_init (ji->data.method->klass);
1130 ji->data.method = mono_get_method (image, token, NULL);
1131 g_assert (ji->data.method);
1132 mono_class_init (ji->data.method->klass);
1137 case MONO_PATCH_INFO_WRAPPER: {
1138 guint32 wrapper_type;
1140 wrapper_type = decode_value (p, &p);
1142 switch (wrapper_type) {
1143 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
1144 guint32 image_index, token, value;
1146 value = decode_value (p, &p);
1147 image_index = value >> 24;
1148 token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1150 image = load_image (aot_module, image_index);
1153 ji->data.method = mono_get_method (image, token, NULL);
1154 g_assert (ji->data.method);
1155 mono_class_init (ji->data.method->klass);
1157 ji->type = MONO_PATCH_INFO_METHOD;
1158 ji->data.method = mono_marshal_get_remoting_invoke_with_check (ji->data.method);
1161 case MONO_WRAPPER_PROXY_ISINST: {
1162 MonoClass *klass = decode_klass_info (aot_module, p, &p);
1165 ji->type = MONO_PATCH_INFO_METHOD;
1166 ji->data.method = mono_marshal_get_proxy_cancast (klass);
1169 case MONO_WRAPPER_LDFLD:
1170 case MONO_WRAPPER_LDFLDA:
1171 case MONO_WRAPPER_STFLD:
1172 case MONO_WRAPPER_LDFLD_REMOTE:
1173 case MONO_WRAPPER_STFLD_REMOTE:
1174 case MONO_WRAPPER_ISINST: {
1175 MonoClass *klass = decode_klass_info (aot_module, p, &p);
1178 ji->type = MONO_PATCH_INFO_METHOD;
1179 if (wrapper_type == MONO_WRAPPER_LDFLD)
1180 ji->data.method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
1181 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
1182 ji->data.method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
1183 else if (wrapper_type == MONO_WRAPPER_STFLD)
1184 ji->data.method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
1185 else if (wrapper_type == MONO_WRAPPER_LDFLD_REMOTE)
1186 ji->data.method = mono_marshal_get_ldfld_remote_wrapper (klass);
1187 else if (wrapper_type == MONO_WRAPPER_STFLD_REMOTE)
1188 ji->data.method = mono_marshal_get_stfld_remote_wrapper (klass);
1189 else if (wrapper_type == MONO_WRAPPER_ISINST)
1190 ji->data.method = mono_marshal_get_isinst (klass);
1192 g_assert_not_reached ();
1195 case MONO_WRAPPER_STELEMREF:
1196 ji->type = MONO_PATCH_INFO_METHOD;
1197 ji->data.method = mono_marshal_get_stelemref ();
1200 g_assert_not_reached ();
1204 case MONO_PATCH_INFO_INTERNAL_METHOD: {
1205 guint32 len = decode_value (p, &p);
1207 ji->data.name = (char*)p;
1211 case MONO_PATCH_INFO_VTABLE:
1212 case MONO_PATCH_INFO_CLASS:
1213 case MONO_PATCH_INFO_IID:
1214 case MONO_PATCH_INFO_ADJUSTED_IID:
1215 *got_offset = decode_value (p, &p);
1217 if (aot_module->got [*got_offset]) {
1218 /* Already loaded */
1219 //printf ("HIT!\n");
1221 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1222 ji->data.klass = decode_klass_info (aot_module, tmp, &tmp);
1223 if (!ji->data.klass)
1227 case MONO_PATCH_INFO_CLASS_INIT:
1228 ji->data.klass = decode_klass_info (aot_module, p, &p);
1229 if (!ji->data.klass)
1232 case MONO_PATCH_INFO_IMAGE:
1233 ji->data.image = load_image (aot_module, decode_value (p, &p));
1234 if (!ji->data.image)
1237 case MONO_PATCH_INFO_FIELD:
1238 case MONO_PATCH_INFO_SFLDA:
1239 *got_offset = decode_value (p, &p);
1241 if (aot_module->got [*got_offset]) {
1242 /* Already loaded */
1243 //printf ("HIT2!\n");
1245 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1246 ji->data.field = decode_field_info (aot_module, tmp, &tmp);
1247 if (!ji->data.field)
1251 case MONO_PATCH_INFO_SWITCH:
1252 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1253 ji->data.table->table_size = decode_value (p, &p);
1254 table = g_new (gpointer, ji->data.table->table_size);
1255 ji->data.table->table = (MonoBasicBlock**)table;
1256 for (i = 0; i < ji->data.table->table_size; i++)
1257 table [i] = (gpointer)(gssize)decode_value (p, &p);
1259 case MONO_PATCH_INFO_R4: {
1262 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1263 val = decode_value (p, &p);
1264 *(float*)ji->data.target = *(float*)&val;
1267 case MONO_PATCH_INFO_R8: {
1270 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1272 val [0] = decode_value (p, &p);
1273 val [1] = decode_value (p, &p);
1274 *(double*)ji->data.target = *(double*)val;
1277 case MONO_PATCH_INFO_LDSTR:
1278 image = load_image (aot_module, decode_value (p, &p));
1281 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1283 case MONO_PATCH_INFO_RVA:
1284 case MONO_PATCH_INFO_DECLSEC:
1285 case MONO_PATCH_INFO_LDTOKEN:
1286 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1287 *got_offset = decode_value (p, &p);
1289 if (aot_module->got [*got_offset]) {
1290 /* Already loaded */
1292 guint8 *tmp = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1293 image = load_image (aot_module, decode_value (tmp, &tmp));
1296 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (tmp, &tmp));
1299 case MONO_PATCH_INFO_EXC_NAME:
1300 ji->data.klass = decode_klass_info (aot_module, p, &p);
1301 if (!ji->data.klass)
1303 ji->data.name = ji->data.klass->name;
1305 case MONO_PATCH_INFO_METHOD_REL:
1306 ji->data.offset = decode_value (p, &p);
1309 g_warning ("unhandled type %d", ji->type);
1310 g_assert_not_reached ();
1321 static MonoJumpInfo*
1322 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1323 guint32 got_index, guint32 **got_slots,
1324 guint8 *buf, guint8 **endbuf)
1326 MonoJumpInfo *patches;
1327 MonoJumpInfo *patch_info = NULL;
1329 guint32 last_offset;
1334 /* First load the type + offset table */
1336 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1338 for (pindex = 0; pindex < n_patches; ++pindex) {
1339 MonoJumpInfo *ji = &patches [pindex];
1344 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1345 ji->next = patch_info;
1349 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1350 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1352 /* Then load the other data */
1353 for (pindex = 0; pindex < n_patches; ++pindex) {
1354 MonoJumpInfo *ji = &patches [pindex];
1356 if (!decode_patch_info (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1359 if ((*got_slots) [pindex] == 0xffffffff)
1360 (*got_slots) [pindex] = got_index ++;
1367 g_free (*got_slots);
1374 mono_aot_get_method_inner (MonoDomain *domain, MonoMethod *method)
1376 MonoClass *klass = method->klass;
1377 MonoAssembly *ass = klass->image->assembly;
1378 MonoDl *module = ass->aot_module;
1379 guint32 method_index = mono_metadata_token_index (method->token) - 1;
1380 guint8 *code, *info;
1381 MonoAotModule *aot_module;
1389 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1392 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1393 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1394 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1395 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1398 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1400 g_assert (klass->inited);
1402 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1403 /* Non shared AOT code can't be used in other appdomains */
1406 if (aot_module->out_of_date)
1409 if (aot_module->code_offsets [method_index] == 0xffffffff) {
1410 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1411 char *full_name = mono_method_full_name (method, TRUE);
1412 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1418 code = &aot_module->code [aot_module->code_offsets [method_index]];
1419 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1421 if (!aot_module->methods_loaded)
1422 aot_module->methods_loaded = g_new0 (guint32, klass->image->tables [MONO_TABLE_METHOD].rows + 1);
1424 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1427 if (mono_last_aot_method != -1) {
1428 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1431 if (mono_jit_stats.methods_aot == mono_last_aot_method)
1432 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
1435 return mono_aot_load_method (domain, aot_module, method, code, info);
1439 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info)
1441 MonoClass *klass = method->klass;
1442 MonoJumpInfo *patch_info = NULL;
1444 int i, pindex, got_index = 0, n_patches, used_strings;
1445 gboolean non_got_patches, keep_patches = TRUE;
1446 guint32 method_index = mono_metadata_token_index (method->token) - 1;
1447 guint8 *p, *ex_info;
1448 MonoJitInfo *jinfo = NULL;
1451 decode_klass_info (aot_module, p, &p);
1453 if (!use_loaded_code) {
1457 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1458 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1461 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1462 memcpy (code2, code, jinfo->code_size);
1463 mono_arch_flush_icache (code2, jinfo->code_size);
1467 if (aot_module->opts & MONO_OPT_SHARED)
1468 used_strings = decode_value (p, &p);
1472 for (i = 0; i < used_strings; i++) {
1473 guint token = decode_value (p, &p);
1474 mono_ldstr (mono_get_root_domain (), klass->image, mono_metadata_token_index (token));
1477 if (aot_module->opts & MONO_OPT_SHARED)
1478 keep_patches = FALSE;
1480 n_patches = decode_value (p, &p);
1482 keep_patches = FALSE;
1485 MonoJumpInfo *patches;
1491 mp = mono_mempool_new ();
1493 got_index = decode_value (p, &p);
1495 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1496 if (patches == NULL)
1499 /* Do this outside the lock to avoid deadlocks */
1501 non_got_patches = FALSE;
1502 for (pindex = 0; pindex < n_patches; ++pindex) {
1503 MonoJumpInfo *ji = &patches [pindex];
1505 if (is_got_patch (ji->type)) {
1506 if (!aot_module->got [got_slots [pindex]])
1507 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
1508 ji->type = MONO_PATCH_INFO_NONE;
1511 non_got_patches = TRUE;
1513 if (non_got_patches) {
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 mono_arch_flush_icache (code, jinfo->code_size);
1520 make_writable (code, jinfo->code_size);
1521 mono_arch_patch_code (method, domain, code, patch_info, TRUE);
1528 mono_mempool_destroy (mp);
1531 mono_jit_stats.methods_aot++;
1533 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1534 char *full_name = mono_method_full_name (method, TRUE);
1537 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1538 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1541 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);
1545 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1547 init_plt (aot_module);
1552 /* FIXME: The space in domain->mp is wasted */
1553 if (aot_module->opts & MONO_OPT_SHARED)
1554 /* No need to cache patches */
1555 mono_mempool_destroy (mp);
1564 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1569 code = mono_aot_get_method_inner (domain, method);
1576 mono_aot_get_method_from_token_inner (MonoDomain *domain, MonoImage *image, guint32 token, MonoClass **klass)
1578 MonoAssembly *ass = image->assembly;
1580 int i, method_index, pindex, got_index, n_patches, used_strings;
1581 gboolean keep_patches = TRUE;
1583 MonoDl *module = ass->aot_module;
1584 guint8 *code = NULL;
1586 MonoAotModule *aot_module;
1593 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1596 aot_module = (MonoAotModule*) g_hash_table_lookup (aot_modules, ass);
1598 if (domain != mono_get_root_domain ())
1601 if (aot_module->out_of_date)
1604 if (aot_module->code_offsets [mono_metadata_token_index (token) - 1] == 0xffffffff) {
1608 method_index = mono_metadata_token_index (token) - 1;
1609 code = &aot_module->code [aot_module->code_offsets [method_index]];
1610 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1612 if (!aot_module->methods_loaded)
1613 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1615 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1618 if (mono_last_aot_method != -1) {
1619 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1622 if (mono_jit_stats.methods_aot == mono_last_aot_method) {
1623 MonoMethod *method = mono_get_method (image, token, NULL);
1624 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1629 *klass = decode_klass_info (aot_module, p, &p);
1631 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1632 MonoMethod *method = mono_get_method (image, token, NULL);
1633 char *full_name = mono_method_full_name (method, TRUE);
1634 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p %p\n", full_name, code, info);
1638 if (aot_module->opts & MONO_OPT_SHARED)
1639 used_strings = decode_value (p, &p);
1643 for (i = 0; i < used_strings; i++) {
1644 guint string_token = decode_value (p, &p);
1645 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (string_token));
1648 if (aot_module->opts & MONO_OPT_SHARED)
1649 keep_patches = FALSE;
1651 keep_patches = FALSE;
1653 n_patches = decode_value (p, &p);
1656 MonoJumpInfo *patches;
1662 mp = mono_mempool_new ();
1664 got_index = decode_value (p, &p);
1666 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1667 if (patches == NULL)
1670 /* Do this outside the lock to avoid deadlocks */
1673 for (pindex = 0; pindex < n_patches; ++pindex) {
1674 MonoJumpInfo *ji = &patches [pindex];
1676 if (is_got_patch (ji->type)) {
1677 if (!aot_module->got [got_slots [pindex]])
1678 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (NULL, domain, code, ji, TRUE);
1679 ji->type = MONO_PATCH_INFO_NONE;
1688 mono_mempool_destroy (mp);
1691 mono_jit_stats.methods_aot++;
1693 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
1695 init_plt (aot_module);
1700 /* FIXME: The space in domain->mp is wasted */
1701 if (aot_module->opts & MONO_OPT_SHARED)
1702 /* No need to cache patches */
1703 mono_mempool_destroy (mp);
1709 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
1713 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
1719 res = mono_aot_get_method_from_token_inner (domain, image, token, &klass);
1726 mono_runtime_class_init (mono_class_vtable (domain, klass));
1734 } IsGotEntryUserData;
1737 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
1739 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
1740 MonoAotModule *aot_module = (MonoAotModule*)value;
1742 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
1747 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
1749 IsGotEntryUserData user_data;
1754 user_data.addr = addr;
1755 user_data.res = FALSE;
1757 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
1760 return user_data.res;
1765 MonoAotModule *module;
1766 } FindAotModuleUserData;
1769 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
1771 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
1772 MonoAotModule *aot_module = (MonoAotModule*)value;
1774 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
1775 data->module = aot_module;
1778 static inline MonoAotModule*
1779 find_aot_module (guint8 *code)
1781 FindAotModuleUserData user_data;
1786 /* Reading these need no locking */
1787 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
1790 user_data.addr = code;
1791 user_data.module = NULL;
1794 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
1797 return user_data.module;
1801 * mono_aot_set_make_unreadable:
1803 * Set whenever to make all mmaped memory unreadable. In conjuction with a
1804 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
1807 mono_aot_set_make_unreadable (gboolean unreadable)
1809 make_unreadable = unreadable;
1813 MonoAotModule *module;
1818 find_map (gpointer key, gpointer value, gpointer user_data)
1820 MonoAotModule *module = (MonoAotModule*)value;
1821 FindMapUserData *data = (FindMapUserData*)user_data;
1824 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
1825 data->module = module;
1828 static MonoAotModule*
1829 find_module_for_addr (void *ptr)
1831 FindMapUserData data;
1833 if (!make_unreadable)
1837 data.ptr = (guint8*)ptr;
1840 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
1847 * mono_aot_is_pagefault:
1849 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
1850 * within memory allocated by this module.
1853 mono_aot_is_pagefault (void *ptr)
1855 if (!make_unreadable)
1858 return find_module_for_addr (ptr) != NULL;
1862 * mono_aot_handle_pagefault:
1864 * Handle a pagefault caused by an unreadable page by making it readable again.
1867 mono_aot_handle_pagefault (void *ptr)
1869 #ifndef PLATFORM_WIN32
1870 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
1874 res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
1875 g_assert (res == 0);
1886 printf ("\nNative stacktrace:\n\n");
1888 size = backtrace (array, 256);
1889 names = backtrace_symbols (array, size);
1890 for (i =0; i < size; ++i) {
1891 printf ("\t%s\n", names [i]);
1901 * mono_aot_plt_resolve:
1903 * This function is called by the entries in the PLT to resolve the actual method that
1904 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
1907 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
1909 #ifdef MONO_ARCH_HAVE_PIC_AOT
1910 guint8 *p, *target, *plt_entry;
1912 MonoAotModule *module = (MonoAotModule*)aot_module;
1915 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
1917 p = &module->plt_info [plt_info_offset];
1919 ji.type = decode_value (p, &p);
1921 res = decode_patch_info (module, NULL, &ji, p, &p, NULL);
1922 // FIXME: Error handling (how ?)
1925 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
1927 /* Patch the PLT entry with target which might be the actual method not a trampoline */
1928 plt_entry = mono_aot_get_plt_entry (code);
1929 g_assert (plt_entry);
1930 mono_arch_patch_plt_entry (plt_entry, target);
1934 g_assert_not_reached ();
1942 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
1943 * method in the module is loaded to avoid committing memory by writing to it.
1944 * LOCKING: Assumes the AOT lock is held.
1947 init_plt (MonoAotModule *info)
1949 #ifdef MONO_ARCH_HAVE_PIC_AOT
1951 guint8 *buf = info->plt;
1953 #if defined(__x86_64__)
1958 if (info->plt_inited)
1961 tramp = mono_arch_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
1964 /* Initialize the first PLT entry */
1965 make_writable (info->plt, info->plt_end - info->plt);
1966 x86_jump_code (buf, tramp);
1967 #elif defined(__x86_64__)
1969 * Initialize the entries in the plt_jump_table to point to the default targets.
1971 n_entries = info->plt_jump_table_size / sizeof (gpointer);
1973 /* The first entry points to the AOT trampoline */
1974 ((gpointer*)info->plt_jump_table)[0] = tramp;
1975 for (i = 1; i < n_entries; ++i)
1976 /* Each PLT entry is 16 bytes long, the default entry begins at offset 6 */
1977 ((gpointer*)info->plt_jump_table)[i] = info->plt + (i * 16) + 6;
1979 g_assert_not_reached ();
1982 info->plt_inited = TRUE;
1987 * mono_aot_get_plt_entry:
1989 * Return the address of the PLT entry called by the code at CODE if exists.
1992 mono_aot_get_plt_entry (guint8 *code)
1994 MonoAotModule *aot_module = find_aot_module (code);
1999 #if defined(__i386__) || defined(__x86_64__)
2000 if (code [-5] == 0xe8) {
2001 guint32 disp = *(guint32*)(code - 4);
2002 guint8 *target = code + disp;
2004 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2013 * mono_aot_get_n_pagefaults:
2015 * Return the number of times handle_pagefault is called.
2018 mono_aot_get_n_pagefaults (void)
2020 return n_pagefaults;
2027 mono_aot_init (void)
2032 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2038 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2044 mono_aot_init_vtable (MonoVTable *vtable)
2050 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2056 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2062 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2068 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2074 mono_aot_is_pagefault (void *ptr)
2080 mono_aot_set_make_unreadable (gboolean unreadable)
2085 mono_aot_get_n_pagefaults (void)
2091 mono_aot_handle_pagefault (void *ptr)
2096 mono_aot_get_plt_entry (guint8 *code)
2102 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)