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(__ppc64__)) || 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;
78 MonoAssemblyName *image_names;
80 MonoAssembly *assembly;
81 MonoImage **image_table;
82 guint32 image_table_len;
92 guint8 *plt_jump_table;
93 guint32 plt_jump_table_size;
94 guint32 *code_offsets;
96 guint32 *method_info_offsets;
98 guint32 *got_info_offsets;
100 guint32 *ex_info_offsets;
101 guint32 *method_order;
102 guint32 *method_order_end;
104 guint32 *class_info_offsets;
105 guint32 *methods_loaded;
106 guint16 *class_name_table;
107 guint8 *wrapper_info;
109 guint32 num_trampolines, first_trampoline_got_offset, trampoline_index;
114 static GHashTable *aot_modules;
115 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
116 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
117 static CRITICAL_SECTION aot_mutex;
120 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
121 * AOT modules registered by mono_aot_register_module ().
123 static GHashTable *static_aot_modules;
126 * Disabling this will make a copy of the loaded code and use the copy instead
127 * of the original. This will place the caller and the callee close to each
128 * other in memory, possibly improving cache behavior. Since the original
129 * code is in copy-on-write memory, this will not increase the memory usage
132 static gboolean use_loaded_code = TRUE;
135 * Whenever to AOT compile loaded assemblies on demand and store them in
136 * a cache under $HOME/.mono/aot-cache.
138 static gboolean use_aot_cache = FALSE;
141 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
142 * use_aot_cache is TRUE.
144 static gboolean spawn_compiler = TRUE;
147 static gint32 mono_last_aot_method = -1;
149 static gboolean make_unreadable = FALSE;
150 static guint32 n_pagefaults = 0;
151 static guint32 name_table_accesses = 0;
153 /* Used to speed-up find_aot_module () */
154 static gsize aot_code_low_addr = (gssize)-1;
155 static gsize aot_code_high_addr = 0;
157 /* Used to communicate with mono_aot_register_globals () */
158 static guint32 globals_tls_id = -1;
161 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info, int method_index);
164 init_plt (MonoAotModule *info);
167 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
168 guint32 got_index, guint32 **got_slots,
169 guint8 *buf, guint8 **endbuf);
171 static inline gboolean
172 is_got_patch (MonoJumpInfoType patch_type)
177 /*****************************************************/
179 /*****************************************************/
182 load_image (MonoAotModule *module, int index)
184 MonoAssembly *assembly;
185 MonoImageOpenStatus status;
187 g_assert (index < module->image_table_len);
189 if (module->image_table [index])
190 return module->image_table [index];
191 if (module->out_of_date)
194 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
196 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", module->aot_name, module->image_names [index].name);
197 module->out_of_date = TRUE;
201 if (strcmp (assembly->image->guid, module->image_guids [index])) {
202 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);
203 module->out_of_date = TRUE;
207 module->image_table [index] = assembly->image;
208 return assembly->image;
213 decode_value (guint8 *ptr, guint8 **rptr)
218 if ((b & 0x80) == 0){
221 } else if ((b & 0x40) == 0){
222 len = ((b & 0x3f) << 8 | ptr [1]);
224 } else if (b != 0xff) {
225 len = ((b & 0x1f) << 24) |
232 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
238 //printf ("DECODE: %d.\n", len);
243 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
246 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
248 static MonoGenericInst*
249 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
252 MonoType **type_argv;
253 MonoGenericInst *inst;
256 type_argc = decode_value (p, &p);
257 type_argv = g_new0 (MonoType*, type_argc);
259 for (i = 0; i < type_argc; ++i) {
260 MonoClass *pclass = decode_klass_ref (module, p, &p);
265 type_argv [i] = &pclass->byval_arg;
268 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
277 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
280 MonoClass *klass, *eklass;
284 token = decode_value (p, &p);
289 if (mono_metadata_token_table (token) == 0) {
290 image = load_image (module, decode_value (p, &p));
293 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
294 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
295 if (token == MONO_TOKEN_TYPE_SPEC) {
296 MonoTypeEnum type = decode_value (p, &p);
298 if (type == MONO_TYPE_GENERICINST) {
300 MonoGenericContext ctx;
303 gclass = decode_klass_ref (module, p, &p);
304 g_assert (gclass->generic_container);
306 memset (&ctx, 0, sizeof (ctx));
307 ctx.class_inst = decode_generic_inst (module, p, &p);
310 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
311 klass = mono_class_from_mono_type (type);
312 mono_metadata_free_type (type);
313 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
316 MonoGenericContainer *container;
318 // FIXME: Maybe use types directly to avoid
319 // the overhead of creating MonoClass-es
321 // FIXME: Memory management
322 t = g_new0 (MonoType, 1);
324 t->data.generic_param = g_new0 (MonoGenericParam, 1);
325 t->data.generic_param->num = decode_value (p, &p);
326 t->data.generic_param->name = "T";
328 is_method = decode_value (p, &p);
330 MonoMethod *method_def = decode_method_ref_2 (module, p, &p);
333 g_free (t->data.generic_param);
338 container = mono_method_get_generic_container (method_def);
340 MonoClass *class_def = decode_klass_ref (module, p, &p);
343 g_free (t->data.generic_param);
348 container = class_def->generic_container;
351 g_assert (container);
352 t->data.generic_param->owner = container;
354 klass = mono_class_from_mono_type (t);
356 g_assert_not_reached ();
359 image = load_image (module, decode_value (p, &p));
362 klass = mono_class_get (image, token);
364 } else if (token == MONO_TOKEN_TYPE_DEF) {
366 image = load_image (module, decode_value (p, &p));
369 rank = decode_value (p, &p);
370 eklass = decode_klass_ref (module, p, &p);
371 klass = mono_array_class_get (eklass, rank);
373 g_assert_not_reached ();
376 mono_class_init (klass);
382 static MonoClassField*
383 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
385 MonoClass *klass = decode_klass_ref (module, buf, &buf);
392 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
396 return mono_class_get_field (klass, token);
402 * Decode a method reference, and return its image and token. This avoids loading
403 * metadata for the method if the caller does not need it. If the method has no token,
404 * then it is loaded from metadata and METHOD is set to the method instance.
406 static inline MonoImage*
407 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, guint8 *buf, guint8 **endbuf)
409 guint32 image_index, value;
416 value = decode_value (p, &p);
417 image_index = value >> 24;
419 if (image_index == 253) {
421 guint32 wrapper_type;
423 wrapper_type = decode_value (p, &p);
426 image = mono_defaults.corlib;
428 switch (wrapper_type) {
429 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
430 MonoMethod *m = decode_method_ref_2 (module, p, &p);
434 mono_class_init (m->klass);
435 *method = mono_marshal_get_remoting_invoke_with_check (m);
438 case MONO_WRAPPER_PROXY_ISINST: {
439 MonoClass *klass = decode_klass_ref (module, p, &p);
442 *method = mono_marshal_get_proxy_cancast (klass);
445 case MONO_WRAPPER_LDFLD:
446 case MONO_WRAPPER_LDFLDA:
447 case MONO_WRAPPER_STFLD:
448 case MONO_WRAPPER_ISINST: {
449 MonoClass *klass = decode_klass_ref (module, p, &p);
452 if (wrapper_type == MONO_WRAPPER_LDFLD)
453 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
454 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
455 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
456 else if (wrapper_type == MONO_WRAPPER_STFLD)
457 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
458 else if (wrapper_type == MONO_WRAPPER_ISINST)
459 *method = mono_marshal_get_isinst (klass);
461 g_assert_not_reached ();
464 case MONO_WRAPPER_LDFLD_REMOTE:
465 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
467 case MONO_WRAPPER_STFLD_REMOTE:
468 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
470 case MONO_WRAPPER_ALLOC: {
471 int atype = decode_value (p, &p);
473 *method = mono_gc_get_managed_allocator_by_type (atype);
476 case MONO_WRAPPER_STELEMREF:
477 *method = mono_marshal_get_stelemref ();
479 case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
480 MonoMethod *m = decode_method_ref_2 (module, p, &p);
484 *method = mono_marshal_get_static_rgctx_invoke (m);
488 g_assert_not_reached ();
490 } else if (image_index == 255) {
492 image_index = decode_value (p, &p);
493 *token = decode_value (p, &p);
495 image = load_image (module, image_index);
498 } else if (image_index == 254) {
499 /* Method on generic instance */
501 MonoGenericContext ctx;
502 gboolean has_class_inst, has_method_inst;
505 * These methods do not have a token which resolves them, so we
506 * resolve them immediately.
508 klass = decode_klass_ref (module, p, &p);
512 image_index = decode_value (p, &p);
513 *token = decode_value (p, &p);
515 image = load_image (module, image_index);
519 *method = mono_get_method_full (image, *token, NULL, NULL);
523 memset (&ctx, 0, sizeof (ctx));
525 if (FALSE && klass->generic_class) {
526 ctx.class_inst = klass->generic_class->context.class_inst;
527 ctx.method_inst = NULL;
529 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
532 memset (&ctx, 0, sizeof (ctx));
534 // FIXME: Memory management
535 has_class_inst = decode_value (p, &p);
536 if (has_class_inst) {
537 ctx.class_inst = decode_generic_inst (module, p, &p);
541 has_method_inst = decode_value (p, &p);
542 if (has_method_inst) {
543 ctx.method_inst = decode_generic_inst (module, p, &p);
544 if (!ctx.method_inst)
548 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
550 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
552 image = load_image (module, image_index);
563 * decode_method_ref_2:
565 * Similar to decode_method_ref, but resolve and return the method itself.
568 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
572 MonoImage *image = decode_method_ref (module, &token, &method, buf, endbuf);
578 return mono_get_method (image, token, NULL);
583 make_writable (guint8* addr, guint32 len)
585 #ifndef PLATFORM_WIN32
590 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
592 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
593 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
594 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
599 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
605 create_cache_structure (void)
611 home = g_get_home_dir ();
615 tmp = g_build_filename (home, ".mono", NULL);
616 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
617 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
618 #ifdef PLATFORM_WIN32
621 err = mkdir (tmp, 0777);
624 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
630 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
631 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
632 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
633 #ifdef PLATFORM_WIN32
636 err = mkdir (tmp, 0777);
639 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
648 * load_aot_module_from_cache:
650 * Experimental code to AOT compile loaded assemblies on demand.
653 * - Add environment variable MONO_AOT_CACHE_OPTIONS
654 * - Add options for controlling the cache size
655 * - Handle full cache by deleting old assemblies lru style
656 * - Add options for excluding assemblies during development
657 * - Maybe add a threshold after an assembly is AOT compiled
658 * - invoking a new mono process is a security risk
659 * - recompile the AOT module if one of its dependencies changes
662 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
664 char *fname, *cmd, *tmp2, *aot_options;
673 if (assembly->image->dynamic)
676 create_cache_structure ();
678 home = g_get_home_dir ();
680 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
681 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
685 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
686 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
689 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
691 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
693 aot_options = g_strdup_printf ("outfile=%s", fname);
695 if (spawn_compiler) {
696 /* FIXME: security */
697 /* FIXME: Has to pass the assembly loading path to the child process */
698 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
700 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
702 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
704 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
705 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
707 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
714 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
716 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
718 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
722 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
724 g_free (aot_options);
731 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
737 for (i = 0; globals [i]; i+= 2) {
738 if (strcmp (globals [i], name) == 0) {
739 *value = globals [i + 1];
744 mono_dl_symbol (module, name, value);
749 load_aot_module (MonoAssembly *assembly, gpointer user_data)
752 MonoAotModule *amodule;
754 gboolean usable = TRUE;
755 char *saved_guid = NULL;
756 char *aot_version = NULL;
757 char *runtime_version;
758 char *opt_flags = NULL;
760 gboolean full_aot = FALSE;
761 gpointer *plt_jump_table_addr = NULL;
762 guint32 *plt_jump_table_size = NULL;
763 guint32 *trampolines_info = NULL;
764 gpointer *got_addr = NULL;
765 gpointer *got = NULL;
766 guint32 *got_size_ptr = NULL;
769 if (mono_compile_aot)
772 if (assembly->image->aot_module)
774 * Already loaded. This can happen because the assembly loading code might invoke
775 * the assembly load hooks multiple times for the same assembly.
779 if (assembly->image->dynamic)
783 if (static_aot_modules)
784 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
790 /* Statically linked AOT module */
792 aot_name = g_strdup_printf ("%s", assembly->aname.name);
793 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
795 TlsSetValue (globals_tls_id, NULL);
798 sofile = load_aot_module_from_cache (assembly, &aot_name);
801 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
803 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
806 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
812 * If the image was compiled in no-dlsym mode, it contains no global symbols,
813 * instead it contains an ELF ctor function which is called by dlopen () which
814 * in turn calls mono_aot_register_globals () to register a table which contains
815 * the name and address of the globals.
817 globals = TlsGetValue (globals_tls_id);
818 TlsSetValue (globals_tls_id, NULL);
821 if (!sofile && !globals) {
823 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
830 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
831 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
832 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
833 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
835 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
836 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);
840 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
841 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
846 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, FULL_VERSION)))) {
847 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);
854 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
856 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
860 if (mono_aot_only && !full_aot) {
861 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
864 if (!mono_aot_only && full_aot) {
865 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
871 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
876 mono_dl_close (sofile);
877 assembly->image->aot_module = NULL;
881 find_symbol (sofile, globals, "got_addr", (gpointer *)&got_addr);
883 got = (gpointer*)*got_addr;
885 find_symbol (sofile, globals, "got_size", (gpointer *)&got_size_ptr);
886 g_assert (got_size_ptr);
888 amodule = g_new0 (MonoAotModule, 1);
889 amodule->aot_name = aot_name;
890 amodule->assembly = assembly;
892 amodule->got_size = *got_size_ptr;
893 amodule->got [0] = assembly->image;
894 amodule->globals = globals;
895 amodule->sofile = sofile;
897 sscanf (opt_flags, "%d", &amodule->opts);
899 /* Read image table */
901 guint32 table_len, i;
904 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
907 table_len = *(guint32*)table;
908 table += sizeof (guint32);
909 amodule->image_table = g_new0 (MonoImage*, table_len);
910 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
911 amodule->image_guids = g_new0 (char*, table_len);
912 amodule->image_table_len = table_len;
913 for (i = 0; i < table_len; ++i) {
914 MonoAssemblyName *aname = &(amodule->image_names [i]);
916 aname->name = g_strdup (table);
917 table += strlen (table) + 1;
918 amodule->image_guids [i] = g_strdup (table);
919 table += strlen (table) + 1;
921 aname->culture = g_strdup (table);
922 table += strlen (table) + 1;
923 memcpy (aname->public_key_token, table, strlen (table) + 1);
924 table += strlen (table) + 1;
926 table = ALIGN_PTR_TO (table, 8);
927 aname->flags = *(guint32*)table;
929 aname->major = *(guint32*)table;
931 aname->minor = *(guint32*)table;
933 aname->build = *(guint32*)table;
935 aname->revision = *(guint32*)table;
940 /* Read method and method_info tables */
941 find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
942 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
943 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
944 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
945 find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
946 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
947 find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
948 find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
949 find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
950 find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
951 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
952 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
953 find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
954 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
955 find_symbol (sofile, globals, "wrapper_info", (gpointer*)&amodule->wrapper_info);
956 find_symbol (sofile, globals, "trampolines", (gpointer*)&amodule->trampolines);
957 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
959 amodule->mem_begin = amodule->code;
961 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
962 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
963 find_symbol (sofile, globals, "plt_info", (gpointer*)&amodule->plt_info);
965 find_symbol (sofile, globals, "plt_jump_table_addr", (gpointer *)&plt_jump_table_addr);
966 g_assert (plt_jump_table_addr);
967 amodule->plt_jump_table = (guint8*)*plt_jump_table_addr;
968 g_assert (amodule->plt_jump_table);
970 find_symbol (sofile, globals, "plt_jump_table_size", (gpointer *)&plt_jump_table_size);
971 g_assert (plt_jump_table_size);
972 amodule->plt_jump_table_size = *plt_jump_table_size;
974 find_symbol (sofile, globals, "trampolines_info", (gpointer *)&trampolines_info);
975 if (trampolines_info) {
976 amodule->num_trampolines = trampolines_info [0];
977 amodule->first_trampoline_got_offset = trampolines_info [1];
980 if (make_unreadable) {
981 #ifndef PLATFORM_WIN32
986 addr = amodule->mem_begin;
987 len = amodule->mem_end - amodule->mem_begin;
989 /* Round down in both directions to avoid modifying data which is not ours */
990 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
991 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
992 err = mprotect (page_start, pages * PAGESIZE, 0);
999 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1000 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1002 g_hash_table_insert (aot_modules, assembly, amodule);
1005 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1007 assembly->image->aot_module = amodule;
1010 * Since we store methoddef and classdef tokens when referring to methods/classes in
1011 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1012 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1013 * non-lazily, since we can't handle out-of-date errors later.
1015 for (i = 0; i < amodule->image_table_len; ++i)
1016 load_image (amodule, i);
1018 if (amodule->out_of_date) {
1019 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);
1020 if (mono_aot_only) {
1021 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode because a dependency cannot be found or it is out of date.\n", aot_name);
1026 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1030 * mono_aot_register_globals:
1032 * This is called by the ctor function in AOT images compiled with the
1033 * 'no-dlsym' option.
1036 mono_aot_register_globals (gpointer *globals)
1038 TlsSetValue (globals_tls_id, globals);
1042 * mono_aot_register_module:
1044 * This should be called by embedding code to register AOT modules statically linked
1045 * into the executable. AOT_INFO should be the value of the
1046 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1049 mono_aot_register_module (gpointer *aot_info)
1057 /* Determine the assembly name */
1058 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1061 /* This could be called before startup */
1065 if (!static_aot_modules)
1066 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1068 g_hash_table_insert (static_aot_modules, aname, globals);
1075 mono_aot_init (void)
1077 InitializeCriticalSection (&aot_mutex);
1078 aot_modules = g_hash_table_new (NULL, NULL);
1079 globals_tls_id = TlsAlloc ();
1081 mono_install_assembly_load_hook (load_aot_module, NULL);
1083 if (getenv ("MONO_LASTAOT"))
1084 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
1085 if (getenv ("MONO_AOT_CACHE"))
1086 use_aot_cache = TRUE;
1090 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1094 info->vtable_size = decode_value (buf, &buf);
1095 if (info->vtable_size == -1)
1098 flags = decode_value (buf, &buf);
1099 info->ghcimpl = (flags >> 0) & 0x1;
1100 info->has_finalize = (flags >> 1) & 0x1;
1101 info->has_cctor = (flags >> 2) & 0x1;
1102 info->has_nested_classes = (flags >> 3) & 0x1;
1103 info->blittable = (flags >> 4) & 0x1;
1104 info->has_references = (flags >> 5) & 0x1;
1105 info->has_static_refs = (flags >> 6) & 0x1;
1106 info->no_special_static_fields = (flags >> 7) & 0x1;
1108 if (info->has_cctor) {
1109 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, buf, &buf);
1113 if (info->has_finalize) {
1114 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, buf, &buf);
1115 if (!info->finalize_image)
1119 info->instance_size = decode_value (buf, &buf);
1120 info->class_size = decode_value (buf, &buf);
1121 info->packing_size = decode_value (buf, &buf);
1122 info->min_align = decode_value (buf, &buf);
1130 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1133 MonoClass *klass = vtable->klass;
1134 MonoAotModule *aot_module = klass->image->aot_module;
1136 MonoCachedClassInfo class_info;
1141 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1144 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1147 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1151 for (i = 0; i < slot; ++i)
1152 decode_method_ref (aot_module, &token, NULL, p, &p);
1154 image = decode_method_ref (aot_module, &token, NULL, p, &p);
1158 return mono_aot_get_method_from_token (domain, image, token);
1162 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1164 MonoAotModule *aot_module = klass->image->aot_module;
1168 if (klass->rank || !aot_module)
1171 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1173 err = decode_cached_class_info (aot_module, res, p, &p);
1181 * mono_aot_get_class_from_name:
1183 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1184 * using a cache stored in the AOT file.
1185 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1187 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1191 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1193 MonoAotModule *aot_module = image->aot_module;
1194 guint16 *table, *entry;
1197 char full_name_buf [1024];
1199 const char *name2, *name_space2;
1201 guint32 cols [MONO_TYPEDEF_SIZE];
1202 GHashTable *nspace_table;
1204 if (!aot_module || !aot_module->class_name_table)
1211 /* First look in the cache */
1212 if (!aot_module->name_cache)
1213 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1214 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1216 *klass = g_hash_table_lookup (nspace_table, name);
1223 table_size = aot_module->class_name_table [0];
1224 table = aot_module->class_name_table + 1;
1226 if (name_space [0] == '\0')
1227 full_name = g_strdup_printf ("%s", name);
1229 if (strlen (name_space) + strlen (name) < 1000) {
1230 sprintf (full_name_buf, "%s.%s", name_space, name);
1231 full_name = full_name_buf;
1233 full_name = g_strdup_printf ("%s.%s", name_space, name);
1236 hash = g_str_hash (full_name) % table_size;
1237 if (full_name != full_name_buf)
1240 entry = &table [hash * 2];
1242 if (entry [0] != 0) {
1243 t = &image->tables [MONO_TABLE_TYPEDEF];
1246 guint32 index = entry [0];
1247 guint32 next = entry [1];
1248 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1250 name_table_accesses ++;
1252 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1254 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1255 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1257 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1259 *klass = mono_class_get (image, token);
1264 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1265 if (!nspace_table) {
1266 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1267 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1269 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1276 entry = &table [next * 2];
1289 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
1290 MonoMethod *method, guint8* ex_info, guint8 *code)
1294 guint code_len, used_int_regs;
1295 gboolean has_generic_jit_info;
1297 MonoMethodHeader *header;
1298 int generic_info_size;
1300 header = mono_method_get_header (method);
1302 /* Load the method info from the AOT file */
1305 code_len = decode_value (p, &p);
1306 used_int_regs = decode_value (p, &p);
1307 has_generic_jit_info = decode_value (p, &p);
1308 if (has_generic_jit_info)
1309 generic_info_size = sizeof (MonoGenericJitInfo);
1311 generic_info_size = 0;
1313 /* Exception table */
1314 if (header && header->num_clauses) {
1316 mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
1317 jinfo->num_clauses = header->num_clauses;
1319 for (i = 0; i < header->num_clauses; ++i) {
1320 MonoExceptionClause *ec = &header->clauses [i];
1321 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1323 ei->flags = ec->flags;
1324 ei->exvar_offset = decode_value (p, &p);
1326 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1327 ei->data.filter = code + decode_value (p, &p);
1329 ei->data.catch_class = ec->data.catch_class;
1331 ei->try_start = code + decode_value (p, &p);
1332 ei->try_end = code + decode_value (p, &p);
1333 ei->handler_start = code + decode_value (p, &p);
1337 jinfo = mono_mempool_alloc0 (domain->mp, sizeof (MonoJitInfo) + generic_info_size);
1339 jinfo->code_size = code_len;
1340 jinfo->used_regs = used_int_regs;
1341 jinfo->method = method;
1342 jinfo->code_start = code;
1343 jinfo->domain_neutral = 0;
1345 if (has_generic_jit_info) {
1346 MonoGenericJitInfo *gi;
1348 jinfo->has_generic_jit_info = 1;
1350 gi = mono_jit_info_get_generic_jit_info (jinfo);
1353 gi->has_this = decode_value (p, &p);
1354 gi->this_reg = decode_value (p, &p);
1355 gi->this_offset = decode_value (p, &p);
1357 /* This currently contains no data */
1358 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1360 jinfo->method = decode_method_ref_2 (aot_module, p, &p);
1363 /* Load debug info */
1364 buf_len = decode_value (p, &p);
1365 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1371 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1373 int pos, left, right, offset, offset1, offset2, last_offset, new_offset, page_index, method_index, table_len;
1375 MonoAotModule *aot_module = image->aot_module;
1378 guint8 *code, *ex_info;
1379 guint32 *table, *ptr;
1385 if (domain != mono_get_root_domain ())
1389 offset = (guint8*)addr - aot_module->code;
1391 /* First search through the index */
1392 ptr = aot_module->method_order;
1397 if (*ptr == 0xffffff)
1401 while (*ptr != 0xffffff) {
1402 guint32 method_index = ptr [0];
1403 new_offset = aot_module->code_offsets [method_index];
1405 if (offset >= last_offset && offset < new_offset) {
1411 last_offset = new_offset;
1415 /* Skip rest of index */
1416 while (*ptr != 0xffffff)
1421 table_len = aot_module->method_order_end - table;
1423 g_assert (table <= aot_module->method_order_end);
1426 left = (page_index * 1024);
1427 right = left + 1024;
1429 if (right > table_len)
1432 offset1 = aot_module->code_offsets [table [left]];
1433 g_assert (offset1 <= offset);
1435 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1438 //printf ("Not found in index: 0x%x\n", offset);
1443 /* Binary search inside the method_order table to find the method */
1445 pos = (left + right) / 2;
1447 g_assert (table + pos <= aot_module->method_order_end);
1449 //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]]);
1451 offset1 = aot_module->code_offsets [table [pos]];
1452 if (table + pos + 1 >= aot_module->method_order_end)
1453 offset2 = aot_module->code_end - aot_module->code;
1455 offset2 = aot_module->code_offsets [table [pos + 1]];
1457 if (offset < offset1)
1459 else if (offset >= offset2)
1465 method_index = table [pos];
1467 /* Might be a wrapper */
1468 if (aot_module->wrappers) {
1470 method = g_hash_table_lookup (aot_module->wrappers, GUINT_TO_POINTER (method_index));
1477 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1478 method = mono_get_method (image, token, NULL);
1484 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1486 code = &aot_module->code [aot_module->code_offsets [method_index]];
1487 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1489 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1491 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1492 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1494 /* Add it to the normal JitInfo tables */
1495 mono_jit_info_table_add (domain, jinfo);
1500 /* Keep it in sync with the version in aot-compiler.c */
1501 static inline gboolean
1502 is_shared_got_patch (MonoJumpInfo *patch_info)
1504 switch (patch_info->type) {
1505 case MONO_PATCH_INFO_VTABLE:
1506 case MONO_PATCH_INFO_CLASS:
1507 case MONO_PATCH_INFO_IID:
1508 case MONO_PATCH_INFO_ADJUSTED_IID:
1509 case MONO_PATCH_INFO_FIELD:
1510 case MONO_PATCH_INFO_SFLDA:
1511 case MONO_PATCH_INFO_DECLSEC:
1512 case MONO_PATCH_INFO_LDTOKEN:
1513 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1514 case MONO_PATCH_INFO_RVA:
1515 case MONO_PATCH_INFO_METHODCONST:
1523 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1531 case MONO_PATCH_INFO_METHOD:
1532 case MONO_PATCH_INFO_METHOD_JUMP:
1533 case MONO_PATCH_INFO_ICALL_ADDR:
1534 case MONO_PATCH_INFO_METHOD_RGCTX: {
1538 image = decode_method_ref (aot_module, &token, &method, p, &p);
1542 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1543 if (!method && !mono_aot_only && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1544 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1545 ji->type = MONO_PATCH_INFO_ABS;
1549 ji->data.method = method;
1551 ji->data.method = mono_get_method (image, token, NULL);
1552 g_assert (ji->data.method);
1553 mono_class_init (ji->data.method->klass);
1556 ji->data.method = mono_get_method (image, token, NULL);
1557 g_assert (ji->data.method);
1558 mono_class_init (ji->data.method->klass);
1563 case MONO_PATCH_INFO_INTERNAL_METHOD:
1564 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1565 guint32 len = decode_value (p, &p);
1567 ji->data.name = (char*)p;
1571 case MONO_PATCH_INFO_METHODCONST:
1573 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1574 if (!ji->data.method)
1577 case MONO_PATCH_INFO_VTABLE:
1578 case MONO_PATCH_INFO_CLASS:
1579 case MONO_PATCH_INFO_IID:
1580 case MONO_PATCH_INFO_ADJUSTED_IID:
1582 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1583 if (!ji->data.klass)
1586 case MONO_PATCH_INFO_CLASS_INIT:
1587 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1588 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1589 if (!ji->data.klass)
1592 case MONO_PATCH_INFO_IMAGE:
1593 ji->data.image = load_image (aot_module, decode_value (p, &p));
1594 if (!ji->data.image)
1597 case MONO_PATCH_INFO_FIELD:
1598 case MONO_PATCH_INFO_SFLDA:
1600 ji->data.field = decode_field_info (aot_module, p, &p);
1601 if (!ji->data.field)
1604 case MONO_PATCH_INFO_SWITCH:
1605 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1606 ji->data.table->table_size = decode_value (p, &p);
1607 table = g_new (gpointer, ji->data.table->table_size);
1608 ji->data.table->table = (MonoBasicBlock**)table;
1609 for (i = 0; i < ji->data.table->table_size; i++)
1610 table [i] = (gpointer)(gssize)decode_value (p, &p);
1612 case MONO_PATCH_INFO_R4: {
1615 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1616 val = decode_value (p, &p);
1617 *(float*)ji->data.target = *(float*)&val;
1620 case MONO_PATCH_INFO_R8: {
1623 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1625 val [0] = decode_value (p, &p);
1626 val [1] = decode_value (p, &p);
1627 *(double*)ji->data.target = *(double*)val;
1630 case MONO_PATCH_INFO_LDSTR:
1631 image = load_image (aot_module, decode_value (p, &p));
1634 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1636 case MONO_PATCH_INFO_RVA:
1637 case MONO_PATCH_INFO_DECLSEC:
1638 case MONO_PATCH_INFO_LDTOKEN:
1639 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1641 image = load_image (aot_module, decode_value (p, &p));
1644 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1646 case MONO_PATCH_INFO_EXC_NAME:
1647 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1648 if (!ji->data.klass)
1650 ji->data.name = ji->data.klass->name;
1652 case MONO_PATCH_INFO_METHOD_REL:
1653 ji->data.offset = decode_value (p, &p);
1655 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1656 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1658 case MONO_PATCH_INFO_RGCTX_FETCH: {
1660 MonoJumpInfoRgctxEntry *entry;
1662 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1663 entry->method = decode_method_ref_2 (aot_module, p, &p);
1664 entry->in_mrgctx = decode_value (p, &p);
1665 entry->info_type = decode_value (p, &p);
1666 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1667 entry->data->type = decode_value (p, &p);
1669 res = decode_patch (aot_module, mp, entry->data, p, &p);
1672 ji->data.rgctx_entry = entry;
1676 g_warning ("unhandled type %d", ji->type);
1677 g_assert_not_reached ();
1689 decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1695 if (is_shared_got_patch (ji)) {
1696 *got_offset = decode_value (p, &p);
1698 if (aot_module->got [*got_offset]) {
1699 /* Already loaded */
1700 //printf ("HIT!\n");
1702 shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1704 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1709 res = decode_patch (aot_module, mp, ji, p, &p);
1718 static MonoJumpInfo*
1719 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1720 guint32 got_index, guint32 **got_slots,
1721 guint8 *buf, guint8 **endbuf)
1723 MonoJumpInfo *patches;
1724 MonoJumpInfo *patch_info = NULL;
1726 guint32 last_offset;
1731 /* First load the type + offset table */
1733 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1735 for (pindex = 0; pindex < n_patches; ++pindex) {
1736 MonoJumpInfo *ji = &patches [pindex];
1741 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1742 ji->next = patch_info;
1746 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1747 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1749 /* Then load the other data */
1750 for (pindex = 0; pindex < n_patches; ++pindex) {
1751 MonoJumpInfo *ji = &patches [pindex];
1753 if (!decode_got_entry (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1756 if ((*got_slots) [pindex] == 0xffffffff)
1757 (*got_slots) [pindex] = got_index ++;
1764 g_free (*got_slots);
1771 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1774 * Jump addresses cannot be patched by the trampoline code since it
1775 * does not have access to the caller's address. Instead, we collect
1776 * the addresses of the GOT slots pointing to a method, and patch
1777 * them after the method has been compiled.
1779 MonoJitDomainInfo *info = jit_domain_info (domain);
1782 mono_domain_lock (domain);
1783 if (!info->jump_target_got_slot_hash)
1784 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1785 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1786 list = g_slist_prepend (list, got_slot);
1787 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
1788 mono_domain_unlock (domain);
1792 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
1794 MonoClass *klass = method->klass;
1795 guint32 method_index = mono_metadata_token_index (method->token) - 1;
1796 guint8 *code, *info;
1797 MonoAotModule *aot_module = klass->image->aot_module;
1802 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1805 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
1806 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1807 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1808 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1811 g_assert (klass->inited);
1813 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1814 /* Non shared AOT code can't be used in other appdomains */
1817 if (aot_module->out_of_date)
1820 if (method->is_inflated) {
1821 if (!mono_method_is_generic_sharable_impl (method, FALSE))
1823 method = mono_method_get_declaring_generic_method (method);
1826 if (!method->token) {
1830 if (!method->wrapper_type || !aot_module->wrapper_info)
1833 /* Try to find the wrapper among the wrapper info */
1834 full_name = mono_method_full_name (method, TRUE);
1835 p = (char*)aot_module->wrapper_info;
1839 end = p + strlen (p) + 1;
1840 end = ALIGN_PTR_TO (end, 4);
1841 method_index = *(guint32*)end;
1843 if (strcmp (full_name, p) == 0)
1853 /* Needed by find_jit_info */
1855 if (!aot_module->wrappers)
1856 aot_module->wrappers = g_hash_table_new (NULL, NULL);
1857 g_hash_table_insert (aot_module->wrappers, GUINT_TO_POINTER (method_index), method);
1861 if (aot_module->code_offsets [method_index] == 0xffffffff) {
1862 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1863 char *full_name = mono_method_full_name (method, TRUE);
1864 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1870 code = &aot_module->code [aot_module->code_offsets [method_index]];
1871 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1874 if (!aot_module->methods_loaded)
1875 aot_module->methods_loaded = g_new0 (guint32, klass->image->tables [MONO_TABLE_METHOD].rows + 1);
1878 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1881 if (mono_last_aot_method != -1) {
1882 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1885 if (mono_jit_stats.methods_aot == mono_last_aot_method)
1886 printf ("LAST AOT METHOD: %s.%s.%s.\n", klass->name_space, klass->name, method->name);
1889 return mono_aot_load_method (domain, aot_module, method, code, info, method_index);
1893 mono_aot_load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoMethod *method, guint8 *code, guint8 *info, int method_index)
1895 MonoClass *klass = method->klass;
1896 MonoJumpInfo *patch_info = NULL;
1898 int i, pindex, got_index = 0, n_patches, used_strings;
1899 gboolean non_got_patches, keep_patches = TRUE;
1900 guint8 *p, *ex_info;
1901 MonoJitInfo *jinfo = NULL;
1904 decode_klass_ref (aot_module, p, &p);
1906 if (!use_loaded_code) {
1910 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1911 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1914 mono_domain_lock (domain);
1915 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1916 mono_domain_unlock (domain);
1917 memcpy (code2, code, jinfo->code_size);
1918 mono_arch_flush_icache (code2, jinfo->code_size);
1922 if (aot_module->opts & MONO_OPT_SHARED)
1923 used_strings = decode_value (p, &p);
1927 for (i = 0; i < used_strings; i++) {
1928 guint token = decode_value (p, &p);
1929 mono_ldstr (mono_get_root_domain (), klass->image, mono_metadata_token_index (token));
1932 if (aot_module->opts & MONO_OPT_SHARED)
1933 keep_patches = FALSE;
1935 n_patches = decode_value (p, &p);
1937 keep_patches = FALSE;
1940 MonoJumpInfo *patches;
1946 mp = mono_mempool_new ();
1948 got_index = decode_value (p, &p);
1950 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1951 if (patches == NULL)
1954 non_got_patches = FALSE;
1955 for (pindex = 0; pindex < n_patches; ++pindex) {
1956 MonoJumpInfo *ji = &patches [pindex];
1958 if (is_got_patch (ji->type)) {
1959 if (!aot_module->got [got_slots [pindex]]) {
1960 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
1961 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
1962 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
1964 ji->type = MONO_PATCH_INFO_NONE;
1967 non_got_patches = TRUE;
1969 if (non_got_patches) {
1971 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (method->token) - 1]];
1972 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1975 mono_arch_flush_icache (code, jinfo->code_size);
1976 make_writable (code, jinfo->code_size);
1977 mono_arch_patch_code (method, domain, code, patch_info, TRUE);
1983 mono_mempool_destroy (mp);
1988 mono_jit_stats.methods_aot++;
1990 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1991 char *full_name = mono_method_full_name (method, TRUE);
1994 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1995 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1998 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);
2002 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2004 init_plt (aot_module);
2011 /* FIXME: The space in domain->mp is wasted */
2012 if (aot_module->opts & MONO_OPT_SHARED)
2013 /* No need to cache patches */
2014 mono_mempool_destroy (mp);
2023 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2027 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2030 int i, method_index, pindex, got_index, n_patches, used_strings;
2031 gboolean keep_patches = TRUE;
2033 guint8 *code = NULL;
2035 MonoAotModule *aot_module = image->aot_module;
2041 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2044 if (domain != mono_get_root_domain ())
2047 if (aot_module->out_of_date)
2050 if (aot_module->code_offsets [mono_metadata_token_index (token) - 1] == 0xffffffff) {
2054 method_index = mono_metadata_token_index (token) - 1;
2055 code = &aot_module->code [aot_module->code_offsets [method_index]];
2056 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
2059 if (!aot_module->methods_loaded)
2060 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
2063 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2066 if (mono_last_aot_method != -1) {
2067 if (mono_jit_stats.methods_aot > mono_last_aot_method)
2070 if (mono_jit_stats.methods_aot == mono_last_aot_method) {
2071 MonoMethod *method = mono_get_method (image, token, NULL);
2072 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
2077 klass = decode_klass_ref (aot_module, p, &p);
2079 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2080 MonoMethod *method = mono_get_method (image, token, NULL);
2081 char *full_name = mono_method_full_name (method, TRUE);
2082 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p %p\n", full_name, code, info);
2086 if (aot_module->opts & MONO_OPT_SHARED)
2087 used_strings = decode_value (p, &p);
2091 for (i = 0; i < used_strings; i++) {
2092 guint string_token = decode_value (p, &p);
2093 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (string_token));
2096 if (aot_module->opts & MONO_OPT_SHARED)
2097 keep_patches = FALSE;
2099 keep_patches = FALSE;
2101 n_patches = decode_value (p, &p);
2104 MonoJumpInfo *patches;
2110 mp = mono_mempool_new ();
2112 got_index = decode_value (p, &p);
2114 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
2115 if (patches == NULL)
2118 for (pindex = 0; pindex < n_patches; ++pindex) {
2119 MonoJumpInfo *ji = &patches [pindex];
2121 if (is_got_patch (ji->type)) {
2122 if (!aot_module->got [got_slots [pindex]]) {
2123 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (NULL, domain, code, ji, TRUE);
2124 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2125 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
2127 ji->type = MONO_PATCH_INFO_NONE;
2134 mono_mempool_destroy (mp);
2137 mono_jit_stats.methods_aot++;
2141 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2143 init_plt (aot_module);
2148 mono_runtime_class_init (mono_class_vtable (domain, klass));
2153 /* FIXME: The space in domain->mp is wasted */
2154 if (aot_module->opts & MONO_OPT_SHARED)
2155 /* No need to cache patches */
2156 mono_mempool_destroy (mp);
2164 } IsGotEntryUserData;
2167 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2169 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2170 MonoAotModule *aot_module = (MonoAotModule*)value;
2172 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
2177 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2179 IsGotEntryUserData user_data;
2184 user_data.addr = addr;
2185 user_data.res = FALSE;
2187 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2190 return user_data.res;
2195 MonoAotModule *module;
2196 } FindAotModuleUserData;
2199 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2201 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2202 MonoAotModule *aot_module = (MonoAotModule*)value;
2204 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2205 data->module = aot_module;
2208 static inline MonoAotModule*
2209 find_aot_module (guint8 *code)
2211 FindAotModuleUserData user_data;
2216 /* Reading these need no locking */
2217 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2220 user_data.addr = code;
2221 user_data.module = NULL;
2224 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2227 return user_data.module;
2231 * mono_aot_set_make_unreadable:
2233 * Set whenever to make all mmaped memory unreadable. In conjuction with a
2234 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
2237 mono_aot_set_make_unreadable (gboolean unreadable)
2239 make_unreadable = unreadable;
2243 MonoAotModule *module;
2248 find_map (gpointer key, gpointer value, gpointer user_data)
2250 MonoAotModule *module = (MonoAotModule*)value;
2251 FindMapUserData *data = (FindMapUserData*)user_data;
2254 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
2255 data->module = module;
2258 static MonoAotModule*
2259 find_module_for_addr (void *ptr)
2261 FindMapUserData data;
2263 if (!make_unreadable)
2267 data.ptr = (guint8*)ptr;
2270 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
2277 * mono_aot_is_pagefault:
2279 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
2280 * within memory allocated by this module.
2283 mono_aot_is_pagefault (void *ptr)
2285 if (!make_unreadable)
2288 return find_module_for_addr (ptr) != NULL;
2292 * mono_aot_handle_pagefault:
2294 * Handle a pagefault caused by an unreadable page by making it readable again.
2297 mono_aot_handle_pagefault (void *ptr)
2299 #ifndef PLATFORM_WIN32
2300 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
2304 res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
2305 g_assert (res == 0);
2316 printf ("\nNative stacktrace:\n\n");
2318 size = backtrace (array, 256);
2319 names = backtrace_symbols (array, size);
2320 for (i =0; i < size; ++i) {
2321 printf ("\t%s\n", names [i]);
2331 * mono_aot_plt_resolve:
2333 * This function is called by the entries in the PLT to resolve the actual method that
2334 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2337 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2339 #ifdef MONO_ARCH_AOT_SUPPORTED
2340 guint8 *p, *target, *plt_entry;
2342 MonoAotModule *module = (MonoAotModule*)aot_module;
2346 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2348 p = &module->plt_info [plt_info_offset];
2350 ji.type = decode_value (p, &p);
2352 mp = mono_mempool_new_size (512);
2353 res = decode_patch (module, mp, &ji, p, &p);
2354 // FIXME: Error handling (how ?)
2357 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2359 mono_mempool_destroy (mp);
2361 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2362 plt_entry = mono_aot_get_plt_entry (code);
2363 g_assert (plt_entry);
2364 mono_arch_patch_plt_entry (plt_entry, target);
2368 g_assert_not_reached ();
2376 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2377 * method in the module is loaded to avoid committing memory by writing to it.
2378 * LOCKING: Assumes the AOT lock is held.
2381 init_plt (MonoAotModule *info)
2383 #ifdef MONO_ARCH_AOT_SUPPORTED
2385 guint8 *buf = info->plt;
2386 #elif defined(__x86_64__)
2388 #elif defined(__arm__)
2393 if (info->plt_inited)
2396 tramp = mono_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2399 /* Initialize the first PLT entry */
2400 make_writable (info->plt, info->plt_end - info->plt);
2401 x86_jump_code (buf, tramp);
2402 #elif defined(__x86_64__) || defined(__arm__)
2404 * Initialize the entries in the plt_jump_table to point to the default targets.
2406 n_entries = info->plt_jump_table_size / sizeof (gpointer);
2408 /* The first entry points to the AOT trampoline */
2409 ((gpointer*)info->plt_jump_table)[0] = tramp;
2410 for (i = 1; i < n_entries; ++i)
2411 /* All the default entries point to the first entry */
2412 ((gpointer*)info->plt_jump_table)[i] = info->plt;
2414 g_assert_not_reached ();
2417 info->plt_inited = TRUE;
2422 * mono_aot_get_plt_entry:
2424 * Return the address of the PLT entry called by the code at CODE if exists.
2427 mono_aot_get_plt_entry (guint8 *code)
2429 MonoAotModule *aot_module = find_aot_module (code);
2430 #if defined(__arm__)
2437 #if defined(__i386__) || defined(__x86_64__)
2438 if (code [-5] == 0xe8) {
2439 guint32 disp = *(guint32*)(code - 4);
2440 guint8 *target = code + disp;
2442 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2445 #elif defined(__arm__)
2446 ins = ((guint32*)(gpointer)code) [-1];
2448 /* Should be a 'bl' */
2449 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2450 gint32 disp = ((gint32)ins) & 0xffffff;
2451 guint8 *target = code - 4 + 8 + (disp * 4);
2453 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2457 g_assert_not_reached ();
2464 * mono_aot_get_plt_info_offset:
2466 * Return the PLT info offset belonging to the plt entry called by CODE.
2469 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2471 guint8 *plt_entry = mono_aot_get_plt_entry (code);
2473 g_assert (plt_entry);
2475 /* The offset is embedded inside the code after the plt entry */
2476 #if defined(__i386__)
2477 return *(guint32*)(plt_entry + 5);
2478 #elif defined(__x86_64__)
2479 return *(guint32*)(plt_entry + 6);
2480 #elif defined(__arm__)
2481 /* The offset is stored as the 5th word of the plt entry */
2482 return ((guint32*)plt_entry) [4];
2484 g_assert_not_reached ();
2490 load_named_code (MonoAotModule *amodule, const char *name)
2494 int n_patches, got_index, pindex;
2500 symbol = g_strdup_printf ("%s", name);
2501 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2505 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2509 symbol = g_strdup_printf ("%s_p", name);
2510 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2513 /* Nothing to patch */
2516 /* Similar to mono_aot_load_method () */
2518 n_patches = decode_value (p, &p);
2521 MonoJumpInfo *patches;
2524 mp = mono_mempool_new ();
2526 got_index = decode_value (p, &p);
2528 patches = load_patch_info (amodule, mp, n_patches, got_index, &got_slots, p, &p);
2531 for (pindex = 0; pindex < n_patches; ++pindex) {
2532 MonoJumpInfo *ji = &patches [pindex];
2536 * When this code is executed, the runtime may not yet initalized, so
2537 * resolve the patch info by hand.
2539 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2540 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2541 target = mono_get_lmf_addr;
2542 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2543 target = mono_thread_force_interruption_checkpoint;
2544 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2545 target = mono_exception_from_token;
2546 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2547 target = mono_get_throw_exception ();
2549 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2550 target = mono_amd64_throw_exception;
2553 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2554 target = mono_arm_throw_exception;
2556 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2557 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2558 target = (gpointer)mono_get_trampoline_func (tramp_type2);
2560 fprintf (stderr, "%s\n", ji->data.name);
2561 g_assert_not_reached ();
2565 /* Hopefully the code doesn't have patches which need method or
2568 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2571 amodule->got [got_slots [pindex]] = target;
2576 mono_mempool_destroy (mp);
2583 * Return the piece of code identified by NAME from the mscorlib AOT file.
2586 mono_aot_get_named_code (const char *name)
2589 MonoAotModule *amodule;
2591 image = mono_defaults.corlib;
2594 amodule = image->aot_module;
2597 return load_named_code (amodule, name);
2601 * Return a specific trampoline from the AOT file.
2604 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2606 MonoAotModule *amodule;
2607 int index, tramp_size;
2608 guint8 *code, *tramp;
2609 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2611 /* Currently, we keep all trampolines in the mscorlib AOT image */
2612 image = mono_defaults.corlib;
2617 amodule = image->aot_module;
2620 if (amodule->trampoline_index == amodule->num_trampolines)
2621 g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_trampolines);
2623 index = amodule->trampoline_index ++;
2627 if (!generic_trampolines [tramp_type]) {
2630 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2631 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2635 tramp = generic_trampolines [tramp_type];
2638 amodule->got [amodule->first_trampoline_got_offset + (index *2)] = tramp;
2639 amodule->got [amodule->first_trampoline_got_offset + (index *2) + 1] = arg1;
2643 #elif defined(__arm__)
2647 g_assert_not_reached ();
2650 code = amodule->trampolines + (index * tramp_size);
2652 *code_len = tramp_size;
2658 mono_aot_get_unbox_trampoline (MonoMethod *method)
2660 guint32 method_index = mono_metadata_token_index (method->token) - 1;
2661 MonoAotModule *amodule;
2665 amodule = method->klass->image->aot_module;
2668 symbol = g_strdup_printf ("unbox_trampoline_%d", method_index);
2669 code = load_named_code (amodule, symbol);
2675 * mono_aot_get_n_pagefaults:
2677 * Return the number of times handle_pagefault is called.
2680 mono_aot_get_n_pagefaults (void)
2682 return n_pagefaults;
2689 mono_aot_init (void)
2694 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2700 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2706 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2712 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2718 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2724 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2730 mono_aot_is_pagefault (void *ptr)
2736 mono_aot_set_make_unreadable (gboolean unreadable)
2741 mono_aot_get_n_pagefaults (void)
2747 mono_aot_handle_pagefault (void *ptr)
2752 mono_aot_get_plt_entry (guint8 *code)
2758 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2764 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2770 mono_aot_create_specific_trampolines (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2772 g_assert_not_reached ();
2777 mono_aot_get_named_code (char *name)
2779 g_assert_not_reached ();
2784 mono_aot_get_unbox_trampoline (MonoMethod *method)
2786 g_assert_not_reached ();