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(__powerpc__) || 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;
77 GHashTable *extra_methods;
78 /* Maps methods to their code */
79 GHashTable *method_to_code;
80 MonoAssemblyName *image_names;
82 MonoAssembly *assembly;
83 MonoImage **image_table;
84 guint32 image_table_len;
94 guint8 *plt_jump_table;
95 guint32 plt_jump_table_size;
96 guint32 *code_offsets;
98 guint32 *method_info_offsets;
100 guint32 *got_info_offsets;
102 guint32 *ex_info_offsets;
103 guint32 *method_order;
104 guint32 *method_order_end;
106 guint32 *class_info_offsets;
107 guint32 *methods_loaded;
108 guint16 *class_name_table;
109 guint32 *extra_method_table;
110 guint8 *extra_method_info;
112 guint32 num_trampolines, first_trampoline_got_offset, trampoline_index;
117 static GHashTable *aot_modules;
118 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
119 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
120 static CRITICAL_SECTION aot_mutex;
123 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
124 * AOT modules registered by mono_aot_register_module ().
126 static GHashTable *static_aot_modules;
129 * Disabling this will make a copy of the loaded code and use the copy instead
130 * of the original. This will place the caller and the callee close to each
131 * other in memory, possibly improving cache behavior. Since the original
132 * code is in copy-on-write memory, this will not increase the memory usage
135 static gboolean use_loaded_code = TRUE;
138 * Whenever to AOT compile loaded assemblies on demand and store them in
139 * a cache under $HOME/.mono/aot-cache.
141 static gboolean use_aot_cache = FALSE;
144 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
145 * use_aot_cache is TRUE.
147 static gboolean spawn_compiler = TRUE;
150 static gint32 mono_last_aot_method = -1;
152 static gboolean make_unreadable = FALSE;
153 static guint32 n_pagefaults = 0;
154 static guint32 name_table_accesses = 0;
156 /* Used to speed-up find_aot_module () */
157 static gsize aot_code_low_addr = (gssize)-1;
158 static gsize aot_code_high_addr = 0;
160 /* Used to communicate with mono_aot_register_globals () */
161 static guint32 globals_tls_id = -1;
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_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
279 gboolean has_class_inst, has_method_inst;
282 has_class_inst = decode_value (p, &p);
283 if (has_class_inst) {
284 ctx->class_inst = decode_generic_inst (module, p, &p);
285 if (!ctx->class_inst)
288 has_method_inst = decode_value (p, &p);
289 if (has_method_inst) {
290 ctx->method_inst = decode_generic_inst (module, p, &p);
291 if (!ctx->method_inst)
300 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
303 MonoClass *klass, *eklass;
307 token = decode_value (p, &p);
312 if (mono_metadata_token_table (token) == 0) {
313 image = load_image (module, decode_value (p, &p));
316 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
317 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
318 if (token == MONO_TOKEN_TYPE_SPEC) {
319 MonoTypeEnum type = decode_value (p, &p);
321 if (type == MONO_TYPE_GENERICINST) {
323 MonoGenericContext ctx;
326 gclass = decode_klass_ref (module, p, &p);
327 g_assert (gclass->generic_container);
329 memset (&ctx, 0, sizeof (ctx));
330 ctx.class_inst = decode_generic_inst (module, p, &p);
333 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
334 klass = mono_class_from_mono_type (type);
335 mono_metadata_free_type (type);
336 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
339 MonoGenericContainer *container;
341 // FIXME: Maybe use types directly to avoid
342 // the overhead of creating MonoClass-es
344 // FIXME: Memory management
345 t = g_new0 (MonoType, 1);
347 t->data.generic_param = g_new0 (MonoGenericParam, 1);
348 t->data.generic_param->num = decode_value (p, &p);
349 t->data.generic_param->name = "T";
351 is_method = decode_value (p, &p);
353 MonoMethod *method_def = decode_method_ref_2 (module, p, &p);
356 g_free (t->data.generic_param);
361 container = mono_method_get_generic_container (method_def);
363 MonoClass *class_def = decode_klass_ref (module, p, &p);
366 g_free (t->data.generic_param);
371 container = class_def->generic_container;
374 g_assert (container);
375 t->data.generic_param->owner = container;
377 klass = mono_class_from_mono_type (t);
379 g_assert_not_reached ();
382 image = load_image (module, decode_value (p, &p));
385 klass = mono_class_get (image, token);
387 } else if (token == MONO_TOKEN_TYPE_DEF) {
389 image = load_image (module, decode_value (p, &p));
392 rank = decode_value (p, &p);
393 eklass = decode_klass_ref (module, p, &p);
394 klass = mono_array_class_get (eklass, rank);
396 g_assert_not_reached ();
399 mono_class_init (klass);
405 static MonoClassField*
406 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
408 MonoClass *klass = decode_klass_ref (module, buf, &buf);
415 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
419 return mono_class_get_field (klass, token);
425 * Decode a method reference, and return its image and token. This avoids loading
426 * metadata for the method if the caller does not need it. If the method has no token,
427 * then it is loaded from metadata and METHOD is set to the method instance.
430 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
432 guint32 image_index, value;
438 if (no_aot_trampoline)
439 *no_aot_trampoline = FALSE;
441 value = decode_value (p, &p);
442 image_index = value >> 24;
444 if (image_index == 252) {
445 if (no_aot_trampoline)
446 *no_aot_trampoline = TRUE;
447 value = decode_value (p, &p);
448 image_index = value >> 24;
451 if (image_index == 253) {
453 guint32 wrapper_type;
455 wrapper_type = decode_value (p, &p);
458 image = mono_defaults.corlib;
460 switch (wrapper_type) {
461 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
462 MonoMethod *m = decode_method_ref_2 (module, p, &p);
466 mono_class_init (m->klass);
467 *method = mono_marshal_get_remoting_invoke_with_check (m);
470 case MONO_WRAPPER_PROXY_ISINST: {
471 MonoClass *klass = decode_klass_ref (module, p, &p);
474 *method = mono_marshal_get_proxy_cancast (klass);
477 case MONO_WRAPPER_LDFLD:
478 case MONO_WRAPPER_LDFLDA:
479 case MONO_WRAPPER_STFLD:
480 case MONO_WRAPPER_ISINST: {
481 MonoClass *klass = decode_klass_ref (module, p, &p);
484 if (wrapper_type == MONO_WRAPPER_LDFLD)
485 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
486 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
487 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
488 else if (wrapper_type == MONO_WRAPPER_STFLD)
489 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
490 else if (wrapper_type == MONO_WRAPPER_ISINST)
491 *method = mono_marshal_get_isinst (klass);
493 g_assert_not_reached ();
496 case MONO_WRAPPER_LDFLD_REMOTE:
497 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
499 case MONO_WRAPPER_STFLD_REMOTE:
500 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
502 case MONO_WRAPPER_ALLOC: {
503 int atype = decode_value (p, &p);
505 *method = mono_gc_get_managed_allocator_by_type (atype);
508 case MONO_WRAPPER_STELEMREF:
509 *method = mono_marshal_get_stelemref ();
511 case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
512 MonoMethod *m = decode_method_ref_2 (module, p, &p);
516 *method = mono_marshal_get_static_rgctx_invoke (m);
520 g_assert_not_reached ();
522 } else if (image_index == 255) {
524 image_index = decode_value (p, &p);
525 *token = decode_value (p, &p);
527 image = load_image (module, image_index);
530 } else if (image_index == 254) {
531 /* Method on generic instance */
533 MonoGenericContext ctx;
536 * These methods do not have a token which resolves them, so we
537 * resolve them immediately.
539 klass = decode_klass_ref (module, p, &p);
543 image_index = decode_value (p, &p);
544 *token = decode_value (p, &p);
546 image = load_image (module, image_index);
550 *method = mono_get_method_full (image, *token, NULL, NULL);
554 memset (&ctx, 0, sizeof (ctx));
556 if (FALSE && klass->generic_class) {
557 ctx.class_inst = klass->generic_class->context.class_inst;
558 ctx.method_inst = NULL;
560 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
563 memset (&ctx, 0, sizeof (ctx));
565 if (!decode_generic_context (module, &ctx, p, &p))
568 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
570 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
572 image = load_image (module, image_index);
583 * decode_method_ref_2:
585 * Similar to decode_method_ref, but resolve and return the method itself.
588 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
592 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
598 return mono_get_method (image, token, NULL);
603 make_writable (guint8* addr, guint32 len)
605 #ifndef PLATFORM_WIN32
610 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
612 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
613 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
614 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
619 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
625 create_cache_structure (void)
631 home = g_get_home_dir ();
635 tmp = g_build_filename (home, ".mono", NULL);
636 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
637 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
638 #ifdef PLATFORM_WIN32
641 err = mkdir (tmp, 0777);
644 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
650 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
651 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
652 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
653 #ifdef PLATFORM_WIN32
656 err = mkdir (tmp, 0777);
659 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
668 * load_aot_module_from_cache:
670 * Experimental code to AOT compile loaded assemblies on demand.
673 * - Add environment variable MONO_AOT_CACHE_OPTIONS
674 * - Add options for controlling the cache size
675 * - Handle full cache by deleting old assemblies lru style
676 * - Add options for excluding assemblies during development
677 * - Maybe add a threshold after an assembly is AOT compiled
678 * - invoking a new mono process is a security risk
679 * - recompile the AOT module if one of its dependencies changes
682 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
684 char *fname, *cmd, *tmp2, *aot_options;
693 if (assembly->image->dynamic)
696 create_cache_structure ();
698 home = g_get_home_dir ();
700 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
701 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
705 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
706 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
709 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
711 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
713 aot_options = g_strdup_printf ("outfile=%s", fname);
715 if (spawn_compiler) {
716 /* FIXME: security */
717 /* FIXME: Has to pass the assembly loading path to the child process */
718 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
720 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
722 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
724 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
725 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
727 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
734 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
736 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
738 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
742 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
744 g_free (aot_options);
751 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
757 for (i = 0; globals [i]; i+= 2) {
758 if (strcmp (globals [i], name) == 0) {
759 *value = globals [i + 1];
764 mono_dl_symbol (module, name, value);
769 load_aot_module (MonoAssembly *assembly, gpointer user_data)
772 MonoAotModule *amodule;
774 gboolean usable = TRUE;
775 char *saved_guid = NULL;
776 char *aot_version = NULL;
777 char *runtime_version;
778 char *opt_flags = NULL;
780 gboolean full_aot = FALSE;
781 gpointer *plt_jump_table_addr = NULL;
782 guint32 *plt_jump_table_size = NULL;
783 guint32 *trampolines_info = NULL;
784 gpointer *got_addr = NULL;
785 gpointer *got = NULL;
786 guint32 *got_size_ptr = NULL;
789 if (mono_compile_aot)
792 if (assembly->image->aot_module)
794 * Already loaded. This can happen because the assembly loading code might invoke
795 * the assembly load hooks multiple times for the same assembly.
799 if (assembly->image->dynamic)
803 if (static_aot_modules)
804 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
810 /* Statically linked AOT module */
812 aot_name = g_strdup_printf ("%s", assembly->aname.name);
813 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
815 TlsSetValue (globals_tls_id, NULL);
818 sofile = load_aot_module_from_cache (assembly, &aot_name);
821 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
823 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
826 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
832 * If the image was compiled in no-dlsym mode, it contains no global symbols,
833 * instead it contains an ELF ctor function which is called by dlopen () which
834 * in turn calls mono_aot_register_globals () to register a table which contains
835 * the name and address of the globals.
837 globals = TlsGetValue (globals_tls_id);
838 TlsSetValue (globals_tls_id, NULL);
841 if (!sofile && !globals) {
843 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
850 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
851 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
852 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
853 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
855 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
856 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);
860 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
861 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
866 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, FULL_VERSION)))) {
867 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);
874 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
876 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
880 if (mono_aot_only && !full_aot) {
881 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
884 if (!mono_aot_only && full_aot) {
885 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
891 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
896 mono_dl_close (sofile);
897 assembly->image->aot_module = NULL;
901 find_symbol (sofile, globals, "got_addr", (gpointer *)&got_addr);
903 got = (gpointer*)*got_addr;
905 find_symbol (sofile, globals, "got_size", (gpointer *)&got_size_ptr);
906 g_assert (got_size_ptr);
908 amodule = g_new0 (MonoAotModule, 1);
909 amodule->aot_name = aot_name;
910 amodule->assembly = assembly;
912 amodule->got_size = *got_size_ptr;
913 amodule->got [0] = assembly->image;
914 amodule->globals = globals;
915 amodule->sofile = sofile;
916 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
918 sscanf (opt_flags, "%d", &amodule->opts);
920 /* Read image table */
922 guint32 table_len, i;
925 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
928 table_len = *(guint32*)table;
929 table += sizeof (guint32);
930 amodule->image_table = g_new0 (MonoImage*, table_len);
931 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
932 amodule->image_guids = g_new0 (char*, table_len);
933 amodule->image_table_len = table_len;
934 for (i = 0; i < table_len; ++i) {
935 MonoAssemblyName *aname = &(amodule->image_names [i]);
937 aname->name = g_strdup (table);
938 table += strlen (table) + 1;
939 amodule->image_guids [i] = g_strdup (table);
940 table += strlen (table) + 1;
942 aname->culture = g_strdup (table);
943 table += strlen (table) + 1;
944 memcpy (aname->public_key_token, table, strlen (table) + 1);
945 table += strlen (table) + 1;
947 table = ALIGN_PTR_TO (table, 8);
948 aname->flags = *(guint32*)table;
950 aname->major = *(guint32*)table;
952 aname->minor = *(guint32*)table;
954 aname->build = *(guint32*)table;
956 aname->revision = *(guint32*)table;
961 /* Read method and method_info tables */
962 find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
963 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
964 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
965 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
966 find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
967 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
968 find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
969 find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
970 find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
971 find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
972 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
973 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
974 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
975 find_symbol (sofile, globals, "extra_method_info", (gpointer *)&amodule->extra_method_info);
976 find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
977 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
978 find_symbol (sofile, globals, "trampolines", (gpointer*)&amodule->trampolines);
979 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
981 amodule->mem_begin = amodule->code;
983 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
984 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
985 find_symbol (sofile, globals, "plt_info", (gpointer*)&amodule->plt_info);
987 find_symbol (sofile, globals, "plt_jump_table_addr", (gpointer *)&plt_jump_table_addr);
988 g_assert (plt_jump_table_addr);
989 amodule->plt_jump_table = (guint8*)*plt_jump_table_addr;
990 g_assert (amodule->plt_jump_table);
992 find_symbol (sofile, globals, "plt_jump_table_size", (gpointer *)&plt_jump_table_size);
993 g_assert (plt_jump_table_size);
994 amodule->plt_jump_table_size = *plt_jump_table_size;
996 find_symbol (sofile, globals, "trampolines_info", (gpointer *)&trampolines_info);
997 if (trampolines_info) {
998 amodule->num_trampolines = trampolines_info [0];
999 amodule->first_trampoline_got_offset = trampolines_info [1];
1002 if (make_unreadable) {
1003 #ifndef PLATFORM_WIN32
1006 int pages, err, len;
1008 addr = amodule->mem_begin;
1009 len = amodule->mem_end - amodule->mem_begin;
1011 /* Round down in both directions to avoid modifying data which is not ours */
1012 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
1013 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
1014 err = mprotect (page_start, pages * PAGESIZE, 0);
1015 g_assert (err == 0);
1021 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1022 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1024 g_hash_table_insert (aot_modules, assembly, amodule);
1027 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1029 assembly->image->aot_module = amodule;
1032 * Since we store methoddef and classdef tokens when referring to methods/classes in
1033 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1034 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1035 * non-lazily, since we can't handle out-of-date errors later.
1037 for (i = 0; i < amodule->image_table_len; ++i)
1038 load_image (amodule, i);
1040 if (amodule->out_of_date) {
1041 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);
1042 if (mono_aot_only) {
1043 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);
1048 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1052 * mono_aot_register_globals:
1054 * This is called by the ctor function in AOT images compiled with the
1055 * 'no-dlsym' option.
1058 mono_aot_register_globals (gpointer *globals)
1060 TlsSetValue (globals_tls_id, globals);
1064 * mono_aot_register_module:
1066 * This should be called by embedding code to register AOT modules statically linked
1067 * into the executable. AOT_INFO should be the value of the
1068 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1071 mono_aot_register_module (gpointer *aot_info)
1079 /* Determine the assembly name */
1080 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1083 /* This could be called before startup */
1087 if (!static_aot_modules)
1088 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1090 g_hash_table_insert (static_aot_modules, aname, globals);
1097 mono_aot_init (void)
1099 InitializeCriticalSection (&aot_mutex);
1100 aot_modules = g_hash_table_new (NULL, NULL);
1101 globals_tls_id = TlsAlloc ();
1103 mono_install_assembly_load_hook (load_aot_module, NULL);
1105 if (getenv ("MONO_LASTAOT"))
1106 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
1107 if (getenv ("MONO_AOT_CACHE"))
1108 use_aot_cache = TRUE;
1112 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1116 info->vtable_size = decode_value (buf, &buf);
1117 if (info->vtable_size == -1)
1120 flags = decode_value (buf, &buf);
1121 info->ghcimpl = (flags >> 0) & 0x1;
1122 info->has_finalize = (flags >> 1) & 0x1;
1123 info->has_cctor = (flags >> 2) & 0x1;
1124 info->has_nested_classes = (flags >> 3) & 0x1;
1125 info->blittable = (flags >> 4) & 0x1;
1126 info->has_references = (flags >> 5) & 0x1;
1127 info->has_static_refs = (flags >> 6) & 0x1;
1128 info->no_special_static_fields = (flags >> 7) & 0x1;
1130 if (info->has_cctor) {
1131 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1135 if (info->has_finalize) {
1136 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1137 if (!info->finalize_image)
1141 info->instance_size = decode_value (buf, &buf);
1142 info->class_size = decode_value (buf, &buf);
1143 info->packing_size = decode_value (buf, &buf);
1144 info->min_align = decode_value (buf, &buf);
1152 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1155 MonoClass *klass = vtable->klass;
1156 MonoAotModule *aot_module = klass->image->aot_module;
1158 MonoCachedClassInfo class_info;
1162 gboolean no_aot_trampoline;
1164 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1167 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1170 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1174 for (i = 0; i < slot; ++i)
1175 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1177 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1180 if (no_aot_trampoline)
1183 if (mono_metadata_token_index (token) == 0)
1186 return mono_aot_get_method_from_token (domain, image, token);
1190 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1192 MonoAotModule *aot_module = klass->image->aot_module;
1196 if (klass->rank || !aot_module)
1199 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1201 err = decode_cached_class_info (aot_module, res, p, &p);
1209 * mono_aot_get_class_from_name:
1211 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1212 * using a cache stored in the AOT file.
1213 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1215 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1219 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1221 MonoAotModule *aot_module = image->aot_module;
1222 guint16 *table, *entry;
1225 char full_name_buf [1024];
1227 const char *name2, *name_space2;
1229 guint32 cols [MONO_TYPEDEF_SIZE];
1230 GHashTable *nspace_table;
1232 if (!aot_module || !aot_module->class_name_table)
1239 /* First look in the cache */
1240 if (!aot_module->name_cache)
1241 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1242 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1244 *klass = g_hash_table_lookup (nspace_table, name);
1251 table_size = aot_module->class_name_table [0];
1252 table = aot_module->class_name_table + 1;
1254 if (name_space [0] == '\0')
1255 full_name = g_strdup_printf ("%s", name);
1257 if (strlen (name_space) + strlen (name) < 1000) {
1258 sprintf (full_name_buf, "%s.%s", name_space, name);
1259 full_name = full_name_buf;
1261 full_name = g_strdup_printf ("%s.%s", name_space, name);
1264 hash = g_str_hash (full_name) % table_size;
1265 if (full_name != full_name_buf)
1268 entry = &table [hash * 2];
1270 if (entry [0] != 0) {
1271 t = &image->tables [MONO_TABLE_TYPEDEF];
1274 guint32 index = entry [0];
1275 guint32 next = entry [1];
1276 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1278 name_table_accesses ++;
1280 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1282 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1283 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1285 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1287 *klass = mono_class_get (image, token);
1292 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1293 if (!nspace_table) {
1294 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1295 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1297 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1304 entry = &table [next * 2];
1317 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
1318 MonoMethod *method, guint8* ex_info, guint8 *code)
1322 guint code_len, used_int_regs;
1323 gboolean has_generic_jit_info;
1325 MonoMethodHeader *header;
1326 int generic_info_size;
1328 header = mono_method_get_header (method);
1330 /* Load the method info from the AOT file */
1333 code_len = decode_value (p, &p);
1334 used_int_regs = decode_value (p, &p);
1335 has_generic_jit_info = decode_value (p, &p);
1336 if (has_generic_jit_info)
1337 generic_info_size = sizeof (MonoGenericJitInfo);
1339 generic_info_size = 0;
1341 /* Exception table */
1342 if (header && header->num_clauses) {
1344 mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
1345 jinfo->num_clauses = header->num_clauses;
1347 for (i = 0; i < header->num_clauses; ++i) {
1348 MonoExceptionClause *ec = &header->clauses [i];
1349 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1351 ei->flags = ec->flags;
1352 ei->exvar_offset = decode_value (p, &p);
1354 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1355 ei->data.filter = code + decode_value (p, &p);
1357 ei->data.catch_class = ec->data.catch_class;
1359 ei->try_start = code + decode_value (p, &p);
1360 ei->try_end = code + decode_value (p, &p);
1361 ei->handler_start = code + decode_value (p, &p);
1365 jinfo = mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + generic_info_size);
1367 jinfo->code_size = code_len;
1368 jinfo->used_regs = used_int_regs;
1369 jinfo->method = method;
1370 jinfo->code_start = code;
1371 jinfo->domain_neutral = 0;
1373 if (has_generic_jit_info) {
1374 MonoGenericJitInfo *gi;
1376 jinfo->has_generic_jit_info = 1;
1378 gi = mono_jit_info_get_generic_jit_info (jinfo);
1381 gi->has_this = decode_value (p, &p);
1382 gi->this_reg = decode_value (p, &p);
1383 gi->this_offset = decode_value (p, &p);
1385 /* This currently contains no data */
1386 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1388 jinfo->method = decode_method_ref_2 (aot_module, p, &p);
1391 /* Load debug info */
1392 buf_len = decode_value (p, &p);
1393 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1399 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1401 int pos, left, right, offset, offset1, offset2, last_offset, new_offset, page_index, method_index, table_len;
1403 MonoAotModule *aot_module = image->aot_module;
1406 guint8 *code, *ex_info;
1407 guint32 *table, *ptr;
1413 if (domain != mono_get_root_domain ())
1417 offset = (guint8*)addr - aot_module->code;
1419 /* First search through the index */
1420 ptr = aot_module->method_order;
1425 if (*ptr == 0xffffff)
1429 while (*ptr != 0xffffff) {
1430 guint32 method_index = ptr [0];
1431 new_offset = aot_module->code_offsets [method_index];
1433 if (offset >= last_offset && offset < new_offset) {
1439 last_offset = new_offset;
1443 /* Skip rest of index */
1444 while (*ptr != 0xffffff)
1449 table_len = aot_module->method_order_end - table;
1451 g_assert (table <= aot_module->method_order_end);
1454 left = (page_index * 1024);
1455 right = left + 1024;
1457 if (right > table_len)
1460 offset1 = aot_module->code_offsets [table [left]];
1461 g_assert (offset1 <= offset);
1463 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1466 //printf ("Not found in index: 0x%x\n", offset);
1471 /* Binary search inside the method_order table to find the method */
1473 pos = (left + right) / 2;
1475 g_assert (table + pos <= aot_module->method_order_end);
1477 //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]]);
1479 offset1 = aot_module->code_offsets [table [pos]];
1480 if (table + pos + 1 >= aot_module->method_order_end)
1481 offset2 = aot_module->code_end - aot_module->code;
1483 offset2 = aot_module->code_offsets [table [pos + 1]];
1485 if (offset < offset1)
1487 else if (offset >= offset2)
1493 method_index = table [pos];
1495 /* Might be a wrapper/extra method */
1496 if (aot_module->extra_methods) {
1498 method = g_hash_table_lookup (aot_module->extra_methods, GUINT_TO_POINTER (method_index));
1506 * This only works for methods which are in the METHOD table, it can't
1507 * handle generic instances/wrappers. This is only a problem for direct
1508 * calls, since those bypass mono_get_aot_method (), so we can't add the
1509 * method to aot_module->extra_methods.
1511 g_assert (method_index < image->tables [MONO_TABLE_METHOD].rows);
1512 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1513 method = mono_get_method (image, token, NULL);
1519 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1521 code = &aot_module->code [aot_module->code_offsets [method_index]];
1522 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
1524 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1526 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1527 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1529 /* Add it to the normal JitInfo tables */
1530 mono_jit_info_table_add (domain, jinfo);
1535 /* Keep it in sync with the version in aot-compiler.c */
1536 static inline gboolean
1537 is_shared_got_patch (MonoJumpInfo *patch_info)
1539 switch (patch_info->type) {
1540 case MONO_PATCH_INFO_VTABLE:
1541 case MONO_PATCH_INFO_CLASS:
1542 case MONO_PATCH_INFO_IID:
1543 case MONO_PATCH_INFO_ADJUSTED_IID:
1544 case MONO_PATCH_INFO_FIELD:
1545 case MONO_PATCH_INFO_SFLDA:
1546 case MONO_PATCH_INFO_DECLSEC:
1547 case MONO_PATCH_INFO_LDTOKEN:
1548 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1549 case MONO_PATCH_INFO_RVA:
1550 case MONO_PATCH_INFO_METHODCONST:
1558 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1566 case MONO_PATCH_INFO_METHOD:
1567 case MONO_PATCH_INFO_METHOD_JUMP:
1568 case MONO_PATCH_INFO_ICALL_ADDR:
1569 case MONO_PATCH_INFO_METHOD_RGCTX: {
1572 gboolean no_aot_trampoline;
1574 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
1578 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1579 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1580 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1581 ji->type = MONO_PATCH_INFO_ABS;
1585 ji->data.method = method;
1587 ji->data.method = mono_get_method (image, token, NULL);
1588 g_assert (ji->data.method);
1589 mono_class_init (ji->data.method->klass);
1592 ji->data.method = mono_get_method (image, token, NULL);
1593 g_assert (ji->data.method);
1594 mono_class_init (ji->data.method->klass);
1599 case MONO_PATCH_INFO_INTERNAL_METHOD:
1600 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1601 guint32 len = decode_value (p, &p);
1603 ji->data.name = (char*)p;
1607 case MONO_PATCH_INFO_METHODCONST:
1609 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1610 if (!ji->data.method)
1613 case MONO_PATCH_INFO_VTABLE:
1614 case MONO_PATCH_INFO_CLASS:
1615 case MONO_PATCH_INFO_IID:
1616 case MONO_PATCH_INFO_ADJUSTED_IID:
1618 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1619 if (!ji->data.klass)
1622 case MONO_PATCH_INFO_CLASS_INIT:
1623 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1624 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1625 if (!ji->data.klass)
1628 case MONO_PATCH_INFO_IMAGE:
1629 ji->data.image = load_image (aot_module, decode_value (p, &p));
1630 if (!ji->data.image)
1633 case MONO_PATCH_INFO_FIELD:
1634 case MONO_PATCH_INFO_SFLDA:
1636 ji->data.field = decode_field_info (aot_module, p, &p);
1637 if (!ji->data.field)
1640 case MONO_PATCH_INFO_SWITCH:
1641 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1642 ji->data.table->table_size = decode_value (p, &p);
1643 table = g_new (gpointer, ji->data.table->table_size);
1644 ji->data.table->table = (MonoBasicBlock**)table;
1645 for (i = 0; i < ji->data.table->table_size; i++)
1646 table [i] = (gpointer)(gssize)decode_value (p, &p);
1648 case MONO_PATCH_INFO_R4: {
1651 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1652 val = decode_value (p, &p);
1653 *(float*)ji->data.target = *(float*)&val;
1656 case MONO_PATCH_INFO_R8: {
1659 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1661 val [0] = decode_value (p, &p);
1662 val [1] = decode_value (p, &p);
1663 *(double*)ji->data.target = *(double*)val;
1666 case MONO_PATCH_INFO_LDSTR:
1667 image = load_image (aot_module, decode_value (p, &p));
1670 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1672 case MONO_PATCH_INFO_RVA:
1673 case MONO_PATCH_INFO_DECLSEC:
1674 case MONO_PATCH_INFO_LDTOKEN:
1675 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1677 image = load_image (aot_module, decode_value (p, &p));
1680 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1682 ji->data.token->has_context = decode_value (p, &p);
1683 if (ji->data.token->has_context) {
1684 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
1689 case MONO_PATCH_INFO_EXC_NAME:
1690 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1691 if (!ji->data.klass)
1693 ji->data.name = ji->data.klass->name;
1695 case MONO_PATCH_INFO_METHOD_REL:
1696 ji->data.offset = decode_value (p, &p);
1698 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1699 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1701 case MONO_PATCH_INFO_RGCTX_FETCH: {
1703 MonoJumpInfoRgctxEntry *entry;
1705 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1706 entry->method = decode_method_ref_2 (aot_module, p, &p);
1707 entry->in_mrgctx = decode_value (p, &p);
1708 entry->info_type = decode_value (p, &p);
1709 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1710 entry->data->type = decode_value (p, &p);
1712 res = decode_patch (aot_module, mp, entry->data, p, &p);
1715 ji->data.rgctx_entry = entry;
1719 g_warning ("unhandled type %d", ji->type);
1720 g_assert_not_reached ();
1732 decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1738 if (is_shared_got_patch (ji)) {
1739 *got_offset = decode_value (p, &p);
1741 if (aot_module->got [*got_offset]) {
1742 /* Already loaded */
1743 //printf ("HIT!\n");
1745 shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1747 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1752 res = decode_patch (aot_module, mp, ji, p, &p);
1761 static MonoJumpInfo*
1762 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1763 guint32 got_index, guint32 **got_slots,
1764 guint8 *buf, guint8 **endbuf)
1766 MonoJumpInfo *patches;
1767 MonoJumpInfo *patch_info = NULL;
1769 guint32 last_offset;
1774 /* First load the type + offset table */
1776 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1778 for (pindex = 0; pindex < n_patches; ++pindex) {
1779 MonoJumpInfo *ji = &patches [pindex];
1784 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1785 ji->next = patch_info;
1789 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1790 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1792 /* Then load the other data */
1793 for (pindex = 0; pindex < n_patches; ++pindex) {
1794 MonoJumpInfo *ji = &patches [pindex];
1796 if (!decode_got_entry (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1799 if ((*got_slots) [pindex] == 0xffffffff)
1800 (*got_slots) [pindex] = got_index ++;
1807 g_free (*got_slots);
1814 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1817 * Jump addresses cannot be patched by the trampoline code since it
1818 * does not have access to the caller's address. Instead, we collect
1819 * the addresses of the GOT slots pointing to a method, and patch
1820 * them after the method has been compiled.
1822 MonoJitDomainInfo *info = domain_jit_info (domain);
1825 mono_domain_lock (domain);
1826 if (!info->jump_target_got_slot_hash)
1827 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1828 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1829 list = g_slist_prepend (list, got_slot);
1830 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
1831 mono_domain_unlock (domain);
1835 * find_extra_method:
1837 * Try finding METHOD in the extra_method table in the AOT image.
1838 * Return its method index, or 0xffffff if not found.
1841 find_extra_method (MonoDomain *domain, MonoMethod *method)
1843 MonoClass *klass = method->klass;
1844 MonoAotModule *aot_module = klass->image->aot_module;
1845 guint32 table_size, entry_size, hash;
1846 guint32 *table, *entry;
1847 char *full_name = NULL;
1852 table_size = aot_module->extra_method_table [0];
1853 table = aot_module->extra_method_table + 1;
1856 if (method->wrapper_type) {
1857 /* FIXME: This is a hack to work around the fact that runtime invoke wrappers get assigned to some random class */
1858 if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1859 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1860 full_name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
1863 full_name = mono_method_full_name (method, TRUE);
1867 if (method->wrapper_type)
1868 hash = g_str_hash (method->name) % table_size;
1870 hash = 0 % table_size;
1872 entry = &table [hash * entry_size];
1874 if (entry [0] != 0) {
1876 guint32 key = entry [0];
1877 guint32 value = entry [1];
1878 guint32 next = entry [entry_size - 1];
1883 // FIXME: Avoid fully decoding the method ref
1884 p = aot_module->extra_method_info + key;
1885 is_wrapper = decode_value (p, &p);
1886 if (method->wrapper_type && is_wrapper) {
1887 if (!strcmp (full_name, (char*)p))
1890 m = decode_method_ref_2 (aot_module, p, &p);
1896 entry = &table [next * entry_size];
1909 * Load the method identified by METHOD_INDEX from the AOT image. Return a
1910 * pointer to the native code of the method, or NULL if not found.
1911 * METHOD might not be set if the caller only has the image/token info.
1914 load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
1917 MonoJumpInfo *patch_info = NULL;
1919 int i, pindex, got_index = 0, n_patches, used_strings;
1920 gboolean non_got_patches, keep_patches = TRUE;
1921 guint8 *p, *ex_info;
1922 MonoJitInfo *jinfo = NULL;
1923 guint8 *code, *info;
1925 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1928 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1929 /* Non shared AOT code can't be used in other appdomains */
1932 if (aot_module->out_of_date)
1935 if (aot_module->code_offsets [method_index] == 0xffffffff) {
1936 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1940 method = mono_get_method (image, token, NULL);
1941 full_name = mono_method_full_name (method, TRUE);
1942 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1948 code = &aot_module->code [aot_module->code_offsets [method_index]];
1949 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1952 if (!aot_module->methods_loaded)
1953 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1956 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1959 if (mono_last_aot_method != -1) {
1960 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1963 if (method && mono_jit_stats.methods_aot == mono_last_aot_method)
1964 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1970 klass = method->klass;
1971 decode_klass_ref (aot_module, p, &p);
1973 klass = decode_klass_ref (aot_module, p, &p);
1976 if (!use_loaded_code) {
1980 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (token) - 1]];
1981 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1984 mono_domain_lock (domain);
1985 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1986 mono_domain_unlock (domain);
1987 memcpy (code2, code, jinfo->code_size);
1988 mono_arch_flush_icache (code2, jinfo->code_size);
1992 if (aot_module->opts & MONO_OPT_SHARED)
1993 used_strings = decode_value (p, &p);
1997 for (i = 0; i < used_strings; i++) {
1998 guint token = decode_value (p, &p);
1999 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2002 if (aot_module->opts & MONO_OPT_SHARED)
2003 keep_patches = FALSE;
2005 n_patches = decode_value (p, &p);
2007 keep_patches = FALSE;
2010 MonoJumpInfo *patches;
2016 mp = mono_mempool_new ();
2018 got_index = decode_value (p, &p);
2020 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
2021 if (patches == NULL)
2024 non_got_patches = FALSE;
2025 for (pindex = 0; pindex < n_patches; ++pindex) {
2026 MonoJumpInfo *ji = &patches [pindex];
2028 if (is_got_patch (ji->type)) {
2029 if (!aot_module->got [got_slots [pindex]]) {
2030 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2031 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2032 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
2034 ji->type = MONO_PATCH_INFO_NONE;
2037 non_got_patches = TRUE;
2039 if (non_got_patches) {
2041 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (token) - 1]];
2042 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2045 mono_arch_flush_icache (code, jinfo->code_size);
2046 make_writable (code, jinfo->code_size);
2047 mono_arch_patch_code (method, domain, code, patch_info, TRUE);
2053 mono_mempool_destroy (mp);
2058 mono_jit_stats.methods_aot++;
2060 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2061 char *full_name = mono_method_full_name (method, TRUE);
2064 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
2065 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2068 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);
2072 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2074 init_plt (aot_module);
2076 if (method && method->wrapper_type)
2077 g_hash_table_insert (aot_module->method_to_code, method, code);
2081 if (!method && klass)
2082 mono_runtime_class_init (mono_class_vtable (domain, klass));
2087 /* FIXME: The space in domain->mp is wasted */
2088 if (aot_module->opts & MONO_OPT_SHARED)
2089 /* No need to cache patches */
2090 mono_mempool_destroy (mp);
2099 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2101 MonoClass *klass = method->klass;
2102 guint32 method_index;
2103 MonoAotModule *aot_module = klass->image->aot_module;
2109 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2110 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2111 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2112 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2115 g_assert (klass->inited);
2117 /* Find method index */
2118 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2119 method = mono_method_get_declaring_generic_method (method);
2120 method_index = mono_metadata_token_index (method->token) - 1;
2121 } else if (method->is_inflated || !method->token) {
2122 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2124 code = g_hash_table_lookup (aot_module->method_to_code, method);
2129 method_index = find_extra_method (domain, method);
2130 if (method_index == 0xffffff)
2133 /* Needed by find_jit_info */
2135 if (!aot_module->extra_methods)
2136 aot_module->extra_methods = g_hash_table_new (NULL, NULL);
2137 g_hash_table_insert (aot_module->extra_methods, GUINT_TO_POINTER (method_index), method);
2141 method_index = mono_metadata_token_index (method->token) - 1;
2144 return load_method (domain, aot_module, klass->image, method, method->token, method_index);
2148 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2152 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2154 MonoAotModule *aot_module = image->aot_module;
2160 method_index = mono_metadata_token_index (token) - 1;
2162 return load_method (domain, aot_module, image, NULL, token, method_index);
2168 } IsGotEntryUserData;
2171 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2173 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2174 MonoAotModule *aot_module = (MonoAotModule*)value;
2176 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
2181 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2183 IsGotEntryUserData user_data;
2188 user_data.addr = addr;
2189 user_data.res = FALSE;
2191 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2194 return user_data.res;
2199 MonoAotModule *module;
2200 } FindAotModuleUserData;
2203 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2205 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2206 MonoAotModule *aot_module = (MonoAotModule*)value;
2208 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2209 data->module = aot_module;
2212 static inline MonoAotModule*
2213 find_aot_module (guint8 *code)
2215 FindAotModuleUserData user_data;
2220 /* Reading these need no locking */
2221 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2224 user_data.addr = code;
2225 user_data.module = NULL;
2228 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2231 return user_data.module;
2235 * mono_aot_set_make_unreadable:
2237 * Set whenever to make all mmaped memory unreadable. In conjuction with a
2238 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
2241 mono_aot_set_make_unreadable (gboolean unreadable)
2243 make_unreadable = unreadable;
2247 MonoAotModule *module;
2252 find_map (gpointer key, gpointer value, gpointer user_data)
2254 MonoAotModule *module = (MonoAotModule*)value;
2255 FindMapUserData *data = (FindMapUserData*)user_data;
2258 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
2259 data->module = module;
2262 static MonoAotModule*
2263 find_module_for_addr (void *ptr)
2265 FindMapUserData data;
2267 if (!make_unreadable)
2271 data.ptr = (guint8*)ptr;
2274 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
2281 * mono_aot_is_pagefault:
2283 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
2284 * within memory allocated by this module.
2287 mono_aot_is_pagefault (void *ptr)
2289 if (!make_unreadable)
2292 return find_module_for_addr (ptr) != NULL;
2296 * mono_aot_handle_pagefault:
2298 * Handle a pagefault caused by an unreadable page by making it readable again.
2301 mono_aot_handle_pagefault (void *ptr)
2303 #ifndef PLATFORM_WIN32
2304 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), PAGESIZE);
2308 res = mprotect (start, PAGESIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
2309 g_assert (res == 0);
2320 printf ("\nNative stacktrace:\n\n");
2322 size = backtrace (array, 256);
2323 names = backtrace_symbols (array, size);
2324 for (i =0; i < size; ++i) {
2325 printf ("\t%s\n", names [i]);
2335 * mono_aot_plt_resolve:
2337 * This function is called by the entries in the PLT to resolve the actual method that
2338 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2341 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2343 #ifdef MONO_ARCH_AOT_SUPPORTED
2344 guint8 *p, *target, *plt_entry;
2346 MonoAotModule *module = (MonoAotModule*)aot_module;
2350 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2352 p = &module->plt_info [plt_info_offset];
2354 ji.type = decode_value (p, &p);
2356 mp = mono_mempool_new_size (512);
2357 res = decode_patch (module, mp, &ji, p, &p);
2358 // FIXME: Error handling (how ?)
2361 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2363 mono_mempool_destroy (mp);
2365 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2366 plt_entry = mono_aot_get_plt_entry (code);
2367 g_assert (plt_entry);
2368 mono_arch_patch_plt_entry (plt_entry, target);
2372 g_assert_not_reached ();
2380 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2381 * method in the module is loaded to avoid committing memory by writing to it.
2382 * LOCKING: Assumes the AOT lock is held.
2385 init_plt (MonoAotModule *info)
2387 #ifdef MONO_ARCH_AOT_SUPPORTED
2389 guint8 *buf = info->plt;
2390 #elif defined(__x86_64__)
2392 #elif defined(__arm__)
2397 if (info->plt_inited)
2400 tramp = mono_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2403 /* Initialize the first PLT entry */
2404 make_writable (info->plt, info->plt_end - info->plt);
2405 x86_jump_code (buf, tramp);
2406 #elif defined(__x86_64__) || defined(__arm__)
2408 * Initialize the entries in the plt_jump_table to point to the default targets.
2410 n_entries = info->plt_jump_table_size / sizeof (gpointer);
2412 /* The first entry points to the AOT trampoline */
2413 ((gpointer*)info->plt_jump_table)[0] = tramp;
2414 for (i = 1; i < n_entries; ++i)
2415 /* All the default entries point to the first entry */
2416 ((gpointer*)info->plt_jump_table)[i] = info->plt;
2418 g_assert_not_reached ();
2421 info->plt_inited = TRUE;
2426 * mono_aot_get_plt_entry:
2428 * Return the address of the PLT entry called by the code at CODE if exists.
2431 mono_aot_get_plt_entry (guint8 *code)
2433 MonoAotModule *aot_module = find_aot_module (code);
2434 #if defined(__arm__)
2441 #if defined(__i386__) || defined(__x86_64__)
2442 if (code [-5] == 0xe8) {
2443 guint32 disp = *(guint32*)(code - 4);
2444 guint8 *target = code + disp;
2446 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2449 #elif defined(__arm__)
2450 ins = ((guint32*)(gpointer)code) [-1];
2452 /* Should be a 'bl' */
2453 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2454 gint32 disp = ((gint32)ins) & 0xffffff;
2455 guint8 *target = code - 4 + 8 + (disp * 4);
2457 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2461 g_assert_not_reached ();
2468 * mono_aot_get_plt_info_offset:
2470 * Return the PLT info offset belonging to the plt entry called by CODE.
2473 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2475 guint8 *plt_entry = mono_aot_get_plt_entry (code);
2477 g_assert (plt_entry);
2479 /* The offset is embedded inside the code after the plt entry */
2480 #if defined(__i386__)
2481 return *(guint32*)(plt_entry + 5);
2482 #elif defined(__x86_64__)
2483 return *(guint32*)(plt_entry + 6);
2484 #elif defined(__arm__)
2485 /* The offset is stored as the 5th word of the plt entry */
2486 return ((guint32*)plt_entry) [4];
2488 g_assert_not_reached ();
2494 load_named_code (MonoAotModule *amodule, const char *name)
2498 int n_patches, got_index, pindex;
2504 symbol = g_strdup_printf ("%s", name);
2505 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2508 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
2510 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2514 symbol = g_strdup_printf ("%s_p", name);
2515 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2518 /* Nothing to patch */
2521 /* Similar to mono_aot_load_method () */
2523 n_patches = decode_value (p, &p);
2526 MonoJumpInfo *patches;
2529 mp = mono_mempool_new ();
2531 got_index = decode_value (p, &p);
2533 patches = load_patch_info (amodule, mp, n_patches, got_index, &got_slots, p, &p);
2536 for (pindex = 0; pindex < n_patches; ++pindex) {
2537 MonoJumpInfo *ji = &patches [pindex];
2541 * When this code is executed, the runtime may not yet initalized, so
2542 * resolve the patch info by hand.
2544 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2545 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2546 target = mono_get_lmf_addr;
2547 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2548 target = mono_thread_force_interruption_checkpoint;
2549 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2550 target = mono_exception_from_token;
2551 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2552 target = mono_get_throw_exception ();
2554 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2555 target = mono_amd64_throw_exception;
2558 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2559 target = mono_arm_throw_exception;
2561 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2562 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2563 target = (gpointer)mono_get_trampoline_func (tramp_type2);
2564 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
2565 guint32 slot = atoi (ji->data.name + strlen ("specific_trampoline_lazy_fetch_"));
2566 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
2568 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
2569 g_assert_not_reached ();
2573 /* Hopefully the code doesn't have patches which need method or
2576 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2579 amodule->got [got_slots [pindex]] = target;
2584 mono_mempool_destroy (mp);
2591 * Return the piece of code identified by NAME from the mscorlib AOT file.
2594 mono_aot_get_named_code (const char *name)
2597 MonoAotModule *amodule;
2599 image = mono_defaults.corlib;
2602 amodule = image->aot_module;
2605 return load_named_code (amodule, name);
2609 * Return a specific trampoline from the AOT file.
2612 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2614 MonoAotModule *amodule;
2615 int index, tramp_size;
2616 guint8 *code, *tramp;
2617 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2619 /* Currently, we keep all trampolines in the mscorlib AOT image */
2620 image = mono_defaults.corlib;
2625 amodule = image->aot_module;
2628 if (amodule->trampoline_index == amodule->num_trampolines)
2629 g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_trampolines);
2631 index = amodule->trampoline_index ++;
2635 if (!generic_trampolines [tramp_type]) {
2638 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2639 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2643 tramp = generic_trampolines [tramp_type];
2646 amodule->got [amodule->first_trampoline_got_offset + (index *2)] = tramp;
2647 amodule->got [amodule->first_trampoline_got_offset + (index *2) + 1] = arg1;
2651 #elif defined(__arm__)
2655 g_assert_not_reached ();
2658 code = amodule->trampolines + (index * tramp_size);
2660 *code_len = tramp_size;
2666 mono_aot_get_unbox_trampoline (MonoMethod *method)
2668 guint32 method_index = mono_metadata_token_index (method->token) - 1;
2669 MonoAotModule *amodule;
2673 amodule = method->klass->image->aot_module;
2676 symbol = g_strdup_printf ("unbox_trampoline_%d", method_index);
2677 code = load_named_code (amodule, symbol);
2683 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2688 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
2689 code = load_named_code (mono_defaults.corlib->aot_module, symbol);
2695 * mono_aot_get_n_pagefaults:
2697 * Return the number of times handle_pagefault is called.
2700 mono_aot_get_n_pagefaults (void)
2702 return n_pagefaults;
2709 mono_aot_init (void)
2714 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2720 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2726 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2732 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2738 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2744 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2750 mono_aot_is_pagefault (void *ptr)
2756 mono_aot_set_make_unreadable (gboolean unreadable)
2761 mono_aot_get_n_pagefaults (void)
2767 mono_aot_handle_pagefault (void *ptr)
2772 mono_aot_get_plt_entry (guint8 *code)
2778 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2784 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2790 mono_aot_create_specific_trampolines (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2792 g_assert_not_reached ();
2797 mono_aot_get_named_code (char *name)
2799 g_assert_not_reached ();
2804 mono_aot_get_unbox_trampoline (MonoMethod *method)
2806 g_assert_not_reached ();