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 #ifdef HAVE_SYS_MMAN_H
27 #ifdef HAVE_EXECINFO_H
33 #include <limits.h> /* for PAGESIZE */
38 #ifdef HAVE_SYS_WAIT_H
39 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
42 #include <mono/metadata/tabledefs.h>
43 #include <mono/metadata/class.h>
44 #include <mono/metadata/object.h>
45 #include <mono/metadata/tokentype.h>
46 #include <mono/metadata/appdomain.h>
47 #include <mono/metadata/debug-helpers.h>
48 #include <mono/metadata/assembly.h>
49 #include <mono/metadata/metadata-internals.h>
50 #include <mono/metadata/marshal.h>
51 #include <mono/metadata/gc-internal.h>
52 #include <mono/metadata/monitor.h>
53 #include <mono/metadata/threads-types.h>
54 #include <mono/utils/mono-logger.h>
55 #include "mono/utils/mono-compiler.h"
63 #define SHARED_EXT ".dll"
64 #elif (defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)
65 #define SHARED_EXT ".dylib"
67 #define SHARED_EXT ".so"
70 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
71 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
73 typedef struct MonoAotModule {
75 /* Optimization flags used to compile the module */
77 /* Pointer to the Global Offset Table */
79 guint32 got_size, plt_size;
80 GHashTable *name_cache;
81 GHashTable *extra_methods;
82 /* Maps methods to their code */
83 GHashTable *method_to_code;
84 MonoAssemblyName *image_names;
86 MonoAssembly *assembly;
87 MonoImage **image_table;
88 guint32 image_table_len;
97 guint32 plt_got_offset_base;
98 guint32 *code_offsets;
100 guint32 *method_info_offsets;
102 guint32 *got_info_offsets;
104 guint32 *ex_info_offsets;
105 guint32 *method_order;
106 guint32 *method_order_end;
108 guint32 *class_info_offsets;
109 guint32 *methods_loaded;
110 guint16 *class_name_table;
111 guint32 *extra_method_table;
112 guint32 *extra_method_info_offsets;
113 guint8 *extra_method_info;
115 guint32 num_trampolines, trampoline_got_offset_base, trampoline_index;
120 /* This structure is stored in the AOT file */
121 typedef struct MonoAotFileInfo
123 guint32 plt_got_offset_base;
124 guint32 trampoline_got_offset_base;
125 guint32 num_trampolines;
131 static GHashTable *aot_modules;
132 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
133 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
134 static CRITICAL_SECTION aot_mutex;
137 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
138 * AOT modules registered by mono_aot_register_module ().
140 static GHashTable *static_aot_modules;
143 * Disabling this will make a copy of the loaded code and use the copy instead
144 * of the original. This will place the caller and the callee close to each
145 * other in memory, possibly improving cache behavior. Since the original
146 * code is in copy-on-write memory, this will not increase the memory usage
149 static gboolean use_loaded_code = TRUE;
152 * Whenever to AOT compile loaded assemblies on demand and store them in
153 * a cache under $HOME/.mono/aot-cache.
155 static gboolean use_aot_cache = FALSE;
158 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
159 * use_aot_cache is TRUE.
161 static gboolean spawn_compiler = TRUE;
164 static gint32 mono_last_aot_method = -1;
166 static gboolean make_unreadable = FALSE;
167 static guint32 name_table_accesses = 0;
169 /* Used to speed-up find_aot_module () */
170 static gsize aot_code_low_addr = (gssize)-1;
171 static gsize aot_code_high_addr = 0;
173 /* Used to communicate with mono_aot_register_globals () */
174 static guint32 globals_tls_id = -1;
177 init_plt (MonoAotModule *info);
180 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
181 guint32 got_index, guint32 **got_slots,
182 guint8 *buf, guint8 **endbuf);
184 /*****************************************************/
186 /*****************************************************/
189 load_image (MonoAotModule *module, int index)
191 MonoAssembly *assembly;
192 MonoImageOpenStatus status;
194 g_assert (index < module->image_table_len);
196 if (module->image_table [index])
197 return module->image_table [index];
198 if (module->out_of_date)
201 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
203 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);
204 module->out_of_date = TRUE;
208 if (strcmp (assembly->image->guid, module->image_guids [index])) {
209 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);
210 module->out_of_date = TRUE;
214 module->image_table [index] = assembly->image;
215 return assembly->image;
220 decode_value (guint8 *ptr, guint8 **rptr)
225 if ((b & 0x80) == 0){
228 } else if ((b & 0x40) == 0){
229 len = ((b & 0x3f) << 8 | ptr [1]);
231 } else if (b != 0xff) {
232 len = ((b & 0x1f) << 24) |
239 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
245 //printf ("DECODE: %d.\n", len);
250 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
253 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
255 static MonoGenericInst*
256 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
259 MonoType **type_argv;
260 MonoGenericInst *inst;
263 type_argc = decode_value (p, &p);
264 type_argv = g_new0 (MonoType*, type_argc);
266 for (i = 0; i < type_argc; ++i) {
267 MonoClass *pclass = decode_klass_ref (module, p, &p);
272 type_argv [i] = &pclass->byval_arg;
275 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
284 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
286 gboolean has_class_inst, has_method_inst;
289 has_class_inst = decode_value (p, &p);
290 if (has_class_inst) {
291 ctx->class_inst = decode_generic_inst (module, p, &p);
292 if (!ctx->class_inst)
295 has_method_inst = decode_value (p, &p);
296 if (has_method_inst) {
297 ctx->method_inst = decode_generic_inst (module, p, &p);
298 if (!ctx->method_inst)
307 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
310 MonoClass *klass, *eklass;
314 token = decode_value (p, &p);
319 if (mono_metadata_token_table (token) == 0) {
320 image = load_image (module, decode_value (p, &p));
323 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
324 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
325 if (token == MONO_TOKEN_TYPE_SPEC) {
326 MonoTypeEnum type = decode_value (p, &p);
328 if (type == MONO_TYPE_GENERICINST) {
330 MonoGenericContext ctx;
333 gclass = decode_klass_ref (module, p, &p);
334 g_assert (gclass->generic_container);
336 memset (&ctx, 0, sizeof (ctx));
337 ctx.class_inst = decode_generic_inst (module, p, &p);
340 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
341 klass = mono_class_from_mono_type (type);
342 mono_metadata_free_type (type);
343 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
346 MonoGenericContainer *container;
348 // FIXME: Maybe use types directly to avoid
349 // the overhead of creating MonoClass-es
351 // FIXME: Memory management
352 t = g_new0 (MonoType, 1);
354 t->data.generic_param = g_new0 (MonoGenericParam, 1);
355 t->data.generic_param->num = decode_value (p, &p);
356 t->data.generic_param->name = "T";
358 is_method = decode_value (p, &p);
360 MonoMethod *method_def = decode_method_ref_2 (module, p, &p);
363 g_free (t->data.generic_param);
368 container = mono_method_get_generic_container (method_def);
370 MonoClass *class_def = decode_klass_ref (module, p, &p);
373 g_free (t->data.generic_param);
378 container = class_def->generic_container;
381 g_assert (container);
382 t->data.generic_param->owner = container;
384 klass = mono_class_from_mono_type (t);
386 g_assert_not_reached ();
389 image = load_image (module, decode_value (p, &p));
392 klass = mono_class_get (image, token);
394 } else if (token == MONO_TOKEN_TYPE_DEF) {
396 image = load_image (module, decode_value (p, &p));
399 rank = decode_value (p, &p);
400 eklass = decode_klass_ref (module, p, &p);
401 klass = mono_array_class_get (eklass, rank);
403 g_assert_not_reached ();
406 mono_class_init (klass);
412 static MonoClassField*
413 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
415 MonoClass *klass = decode_klass_ref (module, buf, &buf);
422 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
426 return mono_class_get_field (klass, token);
432 * Decode a method reference, and return its image and token. This avoids loading
433 * metadata for the method if the caller does not need it. If the method has no token,
434 * then it is loaded from metadata and METHOD is set to the method instance.
437 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
439 guint32 image_index, value;
445 if (no_aot_trampoline)
446 *no_aot_trampoline = FALSE;
448 value = decode_value (p, &p);
449 image_index = value >> 24;
451 if (image_index == 252) {
452 if (no_aot_trampoline)
453 *no_aot_trampoline = TRUE;
454 value = decode_value (p, &p);
455 image_index = value >> 24;
458 if (image_index == 253) {
460 guint32 wrapper_type;
462 wrapper_type = decode_value (p, &p);
465 image = mono_defaults.corlib;
467 switch (wrapper_type) {
468 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
469 MonoMethod *m = decode_method_ref_2 (module, p, &p);
473 mono_class_init (m->klass);
474 *method = mono_marshal_get_remoting_invoke_with_check (m);
477 case MONO_WRAPPER_PROXY_ISINST: {
478 MonoClass *klass = decode_klass_ref (module, p, &p);
481 *method = mono_marshal_get_proxy_cancast (klass);
484 case MONO_WRAPPER_LDFLD:
485 case MONO_WRAPPER_LDFLDA:
486 case MONO_WRAPPER_STFLD:
487 case MONO_WRAPPER_ISINST: {
488 MonoClass *klass = decode_klass_ref (module, p, &p);
491 if (wrapper_type == MONO_WRAPPER_LDFLD)
492 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
493 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
494 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
495 else if (wrapper_type == MONO_WRAPPER_STFLD)
496 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
497 else if (wrapper_type == MONO_WRAPPER_ISINST)
498 *method = mono_marshal_get_isinst (klass);
500 g_assert_not_reached ();
503 case MONO_WRAPPER_LDFLD_REMOTE:
504 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
506 case MONO_WRAPPER_STFLD_REMOTE:
507 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
509 case MONO_WRAPPER_ALLOC: {
510 int atype = decode_value (p, &p);
512 *method = mono_gc_get_managed_allocator_by_type (atype);
515 case MONO_WRAPPER_STELEMREF:
516 *method = mono_marshal_get_stelemref ();
518 case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
519 MonoMethod *m = decode_method_ref_2 (module, p, &p);
523 *method = mono_marshal_get_static_rgctx_invoke (m);
526 case MONO_WRAPPER_UNKNOWN: {
527 MonoMethodDesc *desc;
528 MonoMethod *orig_method;
529 int subtype = decode_value (p, &p);
531 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
532 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
533 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
534 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
536 g_assert_not_reached ();
537 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
538 g_assert (orig_method);
539 mono_method_desc_free (desc);
540 *method = mono_monitor_get_fast_path (orig_method);
544 g_assert_not_reached ();
546 } else if (image_index == 255) {
548 image_index = decode_value (p, &p);
549 *token = decode_value (p, &p);
551 image = load_image (module, image_index);
554 } else if (image_index == 254) {
555 /* Method on generic instance */
557 MonoGenericContext ctx;
560 * These methods do not have a token which resolves them, so we
561 * resolve them immediately.
563 klass = decode_klass_ref (module, p, &p);
567 image_index = decode_value (p, &p);
568 *token = decode_value (p, &p);
570 image = load_image (module, image_index);
574 *method = mono_get_method_full (image, *token, NULL, NULL);
578 memset (&ctx, 0, sizeof (ctx));
580 if (FALSE && klass->generic_class) {
581 ctx.class_inst = klass->generic_class->context.class_inst;
582 ctx.method_inst = NULL;
584 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
587 memset (&ctx, 0, sizeof (ctx));
589 if (!decode_generic_context (module, &ctx, p, &p))
592 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
594 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
596 image = load_image (module, image_index);
607 * decode_method_ref_2:
609 * Similar to decode_method_ref, but resolve and return the method itself.
612 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
616 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
622 return mono_get_method (image, token, NULL);
627 make_writable (guint8* addr, guint32 len)
629 #ifndef PLATFORM_WIN32
634 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
636 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
637 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
638 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
643 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
649 create_cache_structure (void)
655 home = g_get_home_dir ();
659 tmp = g_build_filename (home, ".mono", NULL);
660 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
661 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
662 #ifdef PLATFORM_WIN32
665 err = mkdir (tmp, 0777);
668 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
674 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
675 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
676 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
677 #ifdef PLATFORM_WIN32
680 err = mkdir (tmp, 0777);
683 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
692 * load_aot_module_from_cache:
694 * Experimental code to AOT compile loaded assemblies on demand.
697 * - Add environment variable MONO_AOT_CACHE_OPTIONS
698 * - Add options for controlling the cache size
699 * - Handle full cache by deleting old assemblies lru style
700 * - Add options for excluding assemblies during development
701 * - Maybe add a threshold after an assembly is AOT compiled
702 * - invoking a new mono process is a security risk
703 * - recompile the AOT module if one of its dependencies changes
706 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
708 char *fname, *cmd, *tmp2, *aot_options;
717 if (assembly->image->dynamic)
720 create_cache_structure ();
722 home = g_get_home_dir ();
724 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
725 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
729 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
730 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
733 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
735 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
737 aot_options = g_strdup_printf ("outfile=%s", fname);
739 if (spawn_compiler) {
740 /* FIXME: security */
741 /* FIXME: Has to pass the assembly loading path to the child process */
742 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
744 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
746 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
748 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
749 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
751 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
758 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
760 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
762 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
766 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
768 g_free (aot_options);
775 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
781 for (i = 0; globals [i]; i+= 2) {
782 if (strcmp (globals [i], name) == 0) {
783 *value = globals [i + 1];
788 mono_dl_symbol (module, name, value);
793 load_aot_module (MonoAssembly *assembly, gpointer user_data)
796 MonoAotModule *amodule;
798 gboolean usable = TRUE;
799 char *saved_guid = NULL;
800 char *aot_version = NULL;
801 char *runtime_version, *build_info;
802 char *opt_flags = NULL;
804 gboolean full_aot = FALSE;
805 MonoAotFileInfo *file_info = NULL;
808 if (mono_compile_aot)
811 if (assembly->image->aot_module)
813 * Already loaded. This can happen because the assembly loading code might invoke
814 * the assembly load hooks multiple times for the same assembly.
818 if (assembly->image->dynamic)
821 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
825 if (static_aot_modules)
826 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
832 /* Statically linked AOT module */
834 aot_name = g_strdup_printf ("%s", assembly->aname.name);
835 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
837 TlsSetValue (globals_tls_id, NULL);
840 sofile = load_aot_module_from_cache (assembly, &aot_name);
843 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
845 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
848 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
854 * If the image was compiled in no-dlsym mode, it contains no global symbols,
855 * instead it contains an ELF ctor function which is called by dlopen () which
856 * in turn calls mono_aot_register_globals () to register a table which contains
857 * the name and address of the globals.
859 globals = TlsGetValue (globals_tls_id);
860 TlsSetValue (globals_tls_id, NULL);
863 if (!sofile && !globals) {
865 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
872 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
873 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
874 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
875 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
877 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
878 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);
882 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
883 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
888 build_info = mono_get_runtime_build_info ();
889 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
890 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, build_info);
898 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
900 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
904 if (mono_aot_only && !full_aot) {
905 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
908 if (!mono_aot_only && full_aot) {
909 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
915 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
920 mono_dl_close (sofile);
921 assembly->image->aot_module = NULL;
925 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
926 g_assert (file_info);
928 amodule = g_new0 (MonoAotModule, 1);
929 amodule->aot_name = aot_name;
930 amodule->assembly = assembly;
931 amodule->plt_got_offset_base = file_info->plt_got_offset_base;
932 amodule->num_trampolines = file_info->num_trampolines;
933 amodule->trampoline_got_offset_base = file_info->trampoline_got_offset_base;
934 amodule->got_size = file_info->got_size;
935 amodule->plt_size = file_info->plt_size;
936 amodule->got = file_info->got;
937 amodule->got [0] = assembly->image;
938 amodule->globals = globals;
939 amodule->sofile = sofile;
940 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
942 sscanf (opt_flags, "%d", &amodule->opts);
944 /* Read image table */
946 guint32 table_len, i;
949 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
952 table_len = *(guint32*)table;
953 table += sizeof (guint32);
954 amodule->image_table = g_new0 (MonoImage*, table_len);
955 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
956 amodule->image_guids = g_new0 (char*, table_len);
957 amodule->image_table_len = table_len;
958 for (i = 0; i < table_len; ++i) {
959 MonoAssemblyName *aname = &(amodule->image_names [i]);
961 aname->name = g_strdup (table);
962 table += strlen (table) + 1;
963 amodule->image_guids [i] = g_strdup (table);
964 table += strlen (table) + 1;
966 aname->culture = g_strdup (table);
967 table += strlen (table) + 1;
968 memcpy (aname->public_key_token, table, strlen (table) + 1);
969 table += strlen (table) + 1;
971 table = ALIGN_PTR_TO (table, 8);
972 aname->flags = *(guint32*)table;
974 aname->major = *(guint32*)table;
976 aname->minor = *(guint32*)table;
978 aname->build = *(guint32*)table;
980 aname->revision = *(guint32*)table;
985 /* Read method and method_info tables */
986 find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
987 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
988 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
989 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
990 find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
991 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
992 find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
993 find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
994 find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
995 find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
996 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
997 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
998 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
999 find_symbol (sofile, globals, "extra_method_info", (gpointer *)&amodule->extra_method_info);
1000 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1001 find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
1002 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1003 find_symbol (sofile, globals, "trampolines", (gpointer*)&amodule->trampolines);
1004 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1006 amodule->mem_begin = amodule->code;
1008 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1009 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1011 if (make_unreadable) {
1012 #ifndef PLATFORM_WIN32
1015 int pages, err, len;
1017 addr = amodule->mem_begin;
1018 len = amodule->mem_end - amodule->mem_begin;
1020 /* Round down in both directions to avoid modifying data which is not ours */
1021 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
1022 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
1023 err = mprotect (page_start, pages * PAGESIZE, 0);
1024 g_assert (err == 0);
1030 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1031 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1033 g_hash_table_insert (aot_modules, assembly, amodule);
1036 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1038 assembly->image->aot_module = amodule;
1041 * Since we store methoddef and classdef tokens when referring to methods/classes in
1042 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1043 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1044 * non-lazily, since we can't handle out-of-date errors later.
1046 for (i = 0; i < amodule->image_table_len; ++i)
1047 load_image (amodule, i);
1049 if (amodule->out_of_date) {
1050 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);
1051 if (mono_aot_only) {
1052 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);
1057 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1061 * mono_aot_register_globals:
1063 * This is called by the ctor function in AOT images compiled with the
1064 * 'no-dlsym' option.
1067 mono_aot_register_globals (gpointer *globals)
1069 TlsSetValue (globals_tls_id, globals);
1073 * mono_aot_register_module:
1075 * This should be called by embedding code to register AOT modules statically linked
1076 * into the executable. AOT_INFO should be the value of the
1077 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1080 mono_aot_register_module (gpointer *aot_info)
1088 /* Determine the assembly name */
1089 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1092 /* This could be called before startup */
1096 if (!static_aot_modules)
1097 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1099 g_hash_table_insert (static_aot_modules, aname, globals);
1106 mono_aot_init (void)
1108 InitializeCriticalSection (&aot_mutex);
1109 aot_modules = g_hash_table_new (NULL, NULL);
1110 globals_tls_id = TlsAlloc ();
1112 mono_install_assembly_load_hook (load_aot_module, NULL);
1114 if (getenv ("MONO_LASTAOT"))
1115 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
1116 if (getenv ("MONO_AOT_CACHE"))
1117 use_aot_cache = TRUE;
1121 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1125 info->vtable_size = decode_value (buf, &buf);
1126 if (info->vtable_size == -1)
1129 flags = decode_value (buf, &buf);
1130 info->ghcimpl = (flags >> 0) & 0x1;
1131 info->has_finalize = (flags >> 1) & 0x1;
1132 info->has_cctor = (flags >> 2) & 0x1;
1133 info->has_nested_classes = (flags >> 3) & 0x1;
1134 info->blittable = (flags >> 4) & 0x1;
1135 info->has_references = (flags >> 5) & 0x1;
1136 info->has_static_refs = (flags >> 6) & 0x1;
1137 info->no_special_static_fields = (flags >> 7) & 0x1;
1139 if (info->has_cctor) {
1140 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1144 if (info->has_finalize) {
1145 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1146 if (!info->finalize_image)
1150 info->instance_size = decode_value (buf, &buf);
1151 info->class_size = decode_value (buf, &buf);
1152 info->packing_size = decode_value (buf, &buf);
1153 info->min_align = decode_value (buf, &buf);
1161 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1164 MonoClass *klass = vtable->klass;
1165 MonoAotModule *aot_module = klass->image->aot_module;
1167 MonoCachedClassInfo class_info;
1171 gboolean no_aot_trampoline;
1173 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1176 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1179 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1183 for (i = 0; i < slot; ++i)
1184 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1186 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1189 if (no_aot_trampoline)
1192 if (mono_metadata_token_index (token) == 0)
1195 return mono_aot_get_method_from_token (domain, image, token);
1199 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1201 MonoAotModule *aot_module = klass->image->aot_module;
1205 if (klass->rank || !aot_module)
1208 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1210 err = decode_cached_class_info (aot_module, res, p, &p);
1218 * mono_aot_get_class_from_name:
1220 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1221 * using a cache stored in the AOT file.
1222 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1224 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1228 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1230 MonoAotModule *aot_module = image->aot_module;
1231 guint16 *table, *entry;
1234 char full_name_buf [1024];
1236 const char *name2, *name_space2;
1238 guint32 cols [MONO_TYPEDEF_SIZE];
1239 GHashTable *nspace_table;
1241 if (!aot_module || !aot_module->class_name_table)
1248 /* First look in the cache */
1249 if (!aot_module->name_cache)
1250 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1251 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1253 *klass = g_hash_table_lookup (nspace_table, name);
1260 table_size = aot_module->class_name_table [0];
1261 table = aot_module->class_name_table + 1;
1263 if (name_space [0] == '\0')
1264 full_name = g_strdup_printf ("%s", name);
1266 if (strlen (name_space) + strlen (name) < 1000) {
1267 sprintf (full_name_buf, "%s.%s", name_space, name);
1268 full_name = full_name_buf;
1270 full_name = g_strdup_printf ("%s.%s", name_space, name);
1273 hash = g_str_hash (full_name) % table_size;
1274 if (full_name != full_name_buf)
1277 entry = &table [hash * 2];
1279 if (entry [0] != 0) {
1280 t = &image->tables [MONO_TABLE_TYPEDEF];
1283 guint32 index = entry [0];
1284 guint32 next = entry [1];
1285 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1287 name_table_accesses ++;
1289 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1291 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1292 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1294 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1296 *klass = mono_class_get (image, token);
1301 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1302 if (!nspace_table) {
1303 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1304 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1306 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1313 entry = &table [next * 2];
1326 * LOCKING: Acquires the domain lock.
1329 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
1330 MonoMethod *method, guint8* ex_info, guint8 *code)
1334 guint code_len, used_int_regs, flags;
1335 gboolean has_generic_jit_info, has_dwarf_unwind_info;
1336 guint8 *p, *unwind_info_block;
1337 MonoMethodHeader *header;
1338 int generic_info_size, unwind_info_len;
1340 header = mono_method_get_header (method);
1342 /* Load the method info from the AOT file */
1345 code_len = decode_value (p, &p);
1346 flags = decode_value (p, &p);
1347 has_generic_jit_info = (flags & 1) != 0;
1348 has_dwarf_unwind_info = (flags & 2) != 0;
1349 unwind_info_block = p;
1350 if (has_dwarf_unwind_info) {
1353 unwind_info_len = decode_value (p, &p);
1354 offset = unwind_info_block - (guint8*)aot_module->ex_info;
1355 g_assert (offset > 0 && offset < (1 << 30));
1356 used_int_regs = offset;
1357 p += unwind_info_len;
1359 used_int_regs = decode_value (p, &p);
1361 if (has_generic_jit_info)
1362 generic_info_size = sizeof (MonoGenericJitInfo);
1364 generic_info_size = 0;
1366 /* Exception table */
1367 if (header && header->num_clauses) {
1368 mono_domain_lock (domain);
1370 mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
1371 jinfo->num_clauses = header->num_clauses;
1372 mono_domain_unlock (domain);
1374 for (i = 0; i < header->num_clauses; ++i) {
1375 MonoExceptionClause *ec = &header->clauses [i];
1376 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1378 ei->flags = ec->flags;
1379 ei->exvar_offset = decode_value (p, &p);
1381 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1382 ei->data.filter = code + decode_value (p, &p);
1384 ei->data.catch_class = ec->data.catch_class;
1386 ei->try_start = code + decode_value (p, &p);
1387 ei->try_end = code + decode_value (p, &p);
1388 ei->handler_start = code + decode_value (p, &p);
1392 mono_domain_lock (domain);
1393 jinfo = mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + generic_info_size);
1394 mono_domain_unlock (domain);
1397 jinfo->code_size = code_len;
1398 jinfo->used_regs = used_int_regs;
1399 jinfo->method = method;
1400 jinfo->code_start = code;
1401 jinfo->domain_neutral = 0;
1402 jinfo->from_aot = 1;
1404 if (has_generic_jit_info) {
1405 MonoGenericJitInfo *gi;
1407 jinfo->has_generic_jit_info = 1;
1409 gi = mono_jit_info_get_generic_jit_info (jinfo);
1412 gi->has_this = decode_value (p, &p);
1413 gi->this_reg = decode_value (p, &p);
1414 gi->this_offset = decode_value (p, &p);
1416 /* This currently contains no data */
1417 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1419 jinfo->method = decode_method_ref_2 (aot_module, p, &p);
1422 /* Load debug info */
1423 buf_len = decode_value (p, &p);
1424 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1430 * mono_aot_get_unwind_info:
1432 * Return a pointer to the DWARF unwind info belonging to JI.
1435 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
1437 MonoAotModule *amodule = ji->method->klass->image->aot_module;
1441 g_assert (ji->from_aot);
1443 p = amodule->ex_info + ji->used_regs;
1444 *unwind_info_len = decode_value (p, &p);
1449 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1452 int pos, left, right, offset, offset1, offset2, last_offset, new_offset;
1453 int page_index, method_index, table_len, is_wrapper;
1455 MonoAotModule *amodule = image->aot_module;
1458 guint8 *code, *ex_info, *p;
1459 guint32 *table, *ptr;
1465 if (domain != mono_get_root_domain ())
1469 offset = (guint8*)addr - amodule->code;
1471 /* First search through the index */
1472 ptr = amodule->method_order;
1477 if (*ptr == 0xffffff)
1481 while (*ptr != 0xffffff) {
1482 guint32 method_index = ptr [0];
1483 new_offset = amodule->code_offsets [method_index];
1485 if (offset >= last_offset && offset < new_offset) {
1491 last_offset = new_offset;
1495 /* Skip rest of index */
1496 while (*ptr != 0xffffff)
1501 table_len = amodule->method_order_end - table;
1503 g_assert (table <= amodule->method_order_end);
1506 left = (page_index * 1024);
1507 right = left + 1024;
1509 if (right > table_len)
1512 offset1 = amodule->code_offsets [table [left]];
1513 g_assert (offset1 <= offset);
1515 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1518 //printf ("Not found in index: 0x%x\n", offset);
1523 /* Binary search inside the method_order table to find the method */
1525 pos = (left + right) / 2;
1527 g_assert (table + pos <= amodule->method_order_end);
1529 //printf ("Pos: %5d < %5d < %5d Offset: 0x%05x < 0x%05x < 0x%05x\n", left, pos, right, amodule->code_offsets [table [left]], offset, amodule->code_offsets [table [right]]);
1531 offset1 = amodule->code_offsets [table [pos]];
1532 if (table + pos + 1 >= amodule->method_order_end)
1533 offset2 = amodule->code_end - amodule->code;
1535 offset2 = amodule->code_offsets [table [pos + 1]];
1537 if (offset < offset1)
1539 else if (offset >= offset2)
1545 method_index = table [pos];
1547 /* Might be a wrapper/extra method */
1548 if (amodule->extra_methods) {
1550 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
1557 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
1559 * This is hit for extra methods which are called directly, so they are
1560 * not in amodule->extra_methods.
1562 table_len = amodule->extra_method_info_offsets [0];
1563 table = amodule->extra_method_info_offsets + 1;
1570 pos = ((left + right) / 2);
1572 g_assert (pos < table_len);
1574 if (table [pos * 2] < method_index)
1576 else if (table [pos * 2] > method_index)
1582 p = amodule->extra_method_info + table [(pos * 2) + 1];
1583 is_wrapper = decode_value (p, &p);
1584 g_assert (!is_wrapper);
1585 method = decode_method_ref_2 (amodule, p, &p);
1588 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1589 method = mono_get_method (image, token, NULL);
1596 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1598 code = &amodule->code [amodule->code_offsets [method_index]];
1599 ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
1601 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code);
1603 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1604 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1606 /* Add it to the normal JitInfo tables */
1607 mono_jit_info_table_add (domain, jinfo);
1612 /* Keep it in sync with the version in aot-compiler.c */
1613 static inline gboolean
1614 is_shared_got_patch (MonoJumpInfo *patch_info)
1616 switch (patch_info->type) {
1617 case MONO_PATCH_INFO_VTABLE:
1618 case MONO_PATCH_INFO_CLASS:
1619 case MONO_PATCH_INFO_IID:
1620 case MONO_PATCH_INFO_ADJUSTED_IID:
1621 case MONO_PATCH_INFO_FIELD:
1622 case MONO_PATCH_INFO_SFLDA:
1623 case MONO_PATCH_INFO_DECLSEC:
1624 case MONO_PATCH_INFO_LDTOKEN:
1625 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1626 case MONO_PATCH_INFO_RVA:
1627 case MONO_PATCH_INFO_METHODCONST:
1635 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1643 case MONO_PATCH_INFO_METHOD:
1644 case MONO_PATCH_INFO_METHOD_JUMP:
1645 case MONO_PATCH_INFO_ICALL_ADDR:
1646 case MONO_PATCH_INFO_METHOD_RGCTX: {
1649 gboolean no_aot_trampoline;
1651 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
1655 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1656 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1657 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1658 ji->type = MONO_PATCH_INFO_ABS;
1662 ji->data.method = method;
1664 ji->data.method = mono_get_method (image, token, NULL);
1665 g_assert (ji->data.method);
1666 mono_class_init (ji->data.method->klass);
1669 ji->data.method = mono_get_method (image, token, NULL);
1670 g_assert (ji->data.method);
1671 mono_class_init (ji->data.method->klass);
1676 case MONO_PATCH_INFO_INTERNAL_METHOD:
1677 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1678 guint32 len = decode_value (p, &p);
1680 ji->data.name = (char*)p;
1684 case MONO_PATCH_INFO_METHODCONST:
1686 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1687 if (!ji->data.method)
1690 case MONO_PATCH_INFO_VTABLE:
1691 case MONO_PATCH_INFO_CLASS:
1692 case MONO_PATCH_INFO_IID:
1693 case MONO_PATCH_INFO_ADJUSTED_IID:
1695 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1696 if (!ji->data.klass)
1699 case MONO_PATCH_INFO_CLASS_INIT:
1700 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1701 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1702 if (!ji->data.klass)
1705 case MONO_PATCH_INFO_IMAGE:
1706 ji->data.image = load_image (aot_module, decode_value (p, &p));
1707 if (!ji->data.image)
1710 case MONO_PATCH_INFO_FIELD:
1711 case MONO_PATCH_INFO_SFLDA:
1713 ji->data.field = decode_field_info (aot_module, p, &p);
1714 if (!ji->data.field)
1717 case MONO_PATCH_INFO_SWITCH:
1718 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1719 ji->data.table->table_size = decode_value (p, &p);
1720 table = g_new (gpointer, ji->data.table->table_size);
1721 ji->data.table->table = (MonoBasicBlock**)table;
1722 for (i = 0; i < ji->data.table->table_size; i++)
1723 table [i] = (gpointer)(gssize)decode_value (p, &p);
1725 case MONO_PATCH_INFO_R4: {
1728 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1729 val = decode_value (p, &p);
1730 *(float*)ji->data.target = *(float*)&val;
1733 case MONO_PATCH_INFO_R8: {
1736 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1738 val [0] = decode_value (p, &p);
1739 val [1] = decode_value (p, &p);
1740 *(double*)ji->data.target = *(double*)val;
1743 case MONO_PATCH_INFO_LDSTR:
1744 image = load_image (aot_module, decode_value (p, &p));
1747 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1749 case MONO_PATCH_INFO_RVA:
1750 case MONO_PATCH_INFO_DECLSEC:
1751 case MONO_PATCH_INFO_LDTOKEN:
1752 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1754 image = load_image (aot_module, decode_value (p, &p));
1757 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1759 ji->data.token->has_context = decode_value (p, &p);
1760 if (ji->data.token->has_context) {
1761 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
1766 case MONO_PATCH_INFO_EXC_NAME:
1767 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1768 if (!ji->data.klass)
1770 ji->data.name = ji->data.klass->name;
1772 case MONO_PATCH_INFO_METHOD_REL:
1773 ji->data.offset = decode_value (p, &p);
1775 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1776 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1777 case MONO_PATCH_INFO_MONITOR_ENTER:
1778 case MONO_PATCH_INFO_MONITOR_EXIT:
1780 case MONO_PATCH_INFO_RGCTX_FETCH: {
1782 MonoJumpInfoRgctxEntry *entry;
1784 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1785 entry->method = decode_method_ref_2 (aot_module, p, &p);
1786 entry->in_mrgctx = decode_value (p, &p);
1787 entry->info_type = decode_value (p, &p);
1788 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1789 entry->data->type = decode_value (p, &p);
1791 res = decode_patch (aot_module, mp, entry->data, p, &p);
1794 ji->data.rgctx_entry = entry;
1798 g_warning ("unhandled type %d", ji->type);
1799 g_assert_not_reached ();
1811 decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1817 if (is_shared_got_patch (ji)) {
1818 *got_offset = decode_value (p, &p);
1820 if (aot_module->got [*got_offset]) {
1821 /* Already loaded */
1822 //printf ("HIT!\n");
1824 shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1826 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1831 res = decode_patch (aot_module, mp, ji, p, &p);
1840 static MonoJumpInfo*
1841 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1842 guint32 got_index, guint32 **got_slots,
1843 guint8 *buf, guint8 **endbuf)
1845 MonoJumpInfo *patches;
1846 MonoJumpInfo *patch_info = NULL;
1848 guint32 last_offset;
1853 /* First load the type + offset table */
1855 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1857 for (pindex = 0; pindex < n_patches; ++pindex) {
1858 MonoJumpInfo *ji = &patches [pindex];
1863 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1864 ji->next = patch_info;
1868 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1869 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1871 /* Then load the other data */
1872 for (pindex = 0; pindex < n_patches; ++pindex) {
1873 MonoJumpInfo *ji = &patches [pindex];
1875 if (!decode_got_entry (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1878 if ((*got_slots) [pindex] == 0xffffffff)
1879 (*got_slots) [pindex] = got_index ++;
1886 g_free (*got_slots);
1893 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1896 * Jump addresses cannot be patched by the trampoline code since it
1897 * does not have access to the caller's address. Instead, we collect
1898 * the addresses of the GOT slots pointing to a method, and patch
1899 * them after the method has been compiled.
1901 MonoJitDomainInfo *info = domain_jit_info (domain);
1904 mono_domain_lock (domain);
1905 if (!info->jump_target_got_slot_hash)
1906 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1907 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1908 list = g_slist_prepend (list, got_slot);
1909 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
1910 mono_domain_unlock (domain);
1916 * Load the method identified by METHOD_INDEX from the AOT image. Return a
1917 * pointer to the native code of the method, or NULL if not found.
1918 * METHOD might not be set if the caller only has the image/token info.
1921 load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
1924 gboolean from_plt = method == NULL;
1926 int i, pindex, got_index = 0, n_patches, used_strings;
1927 gboolean keep_patches = TRUE;
1928 guint8 *p, *ex_info;
1929 MonoJitInfo *jinfo = NULL;
1930 guint8 *code, *info;
1932 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1935 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1936 /* Non shared AOT code can't be used in other appdomains */
1939 if (aot_module->out_of_date)
1942 if (aot_module->code_offsets [method_index] == 0xffffffff) {
1943 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1947 method = mono_get_method (image, token, NULL);
1948 full_name = mono_method_full_name (method, TRUE);
1949 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1955 code = &aot_module->code [aot_module->code_offsets [method_index]];
1956 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1959 if (!aot_module->methods_loaded)
1960 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1963 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1966 if (mono_last_aot_method != -1) {
1967 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1970 if (method && mono_jit_stats.methods_aot == mono_last_aot_method)
1971 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1977 klass = method->klass;
1978 decode_klass_ref (aot_module, p, &p);
1980 klass = decode_klass_ref (aot_module, p, &p);
1983 if (!use_loaded_code) {
1987 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (token) - 1]];
1988 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1991 mono_domain_lock (domain);
1992 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1993 mono_domain_unlock (domain);
1994 memcpy (code2, code, jinfo->code_size);
1995 mono_arch_flush_icache (code2, jinfo->code_size);
1999 if (aot_module->opts & MONO_OPT_SHARED)
2000 used_strings = decode_value (p, &p);
2004 for (i = 0; i < used_strings; i++) {
2005 guint token = decode_value (p, &p);
2006 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2009 if (aot_module->opts & MONO_OPT_SHARED)
2010 keep_patches = FALSE;
2012 n_patches = decode_value (p, &p);
2014 keep_patches = FALSE;
2017 MonoJumpInfo *patches;
2023 mp = mono_mempool_new ();
2025 got_index = decode_value (p, &p);
2027 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
2028 if (patches == NULL)
2031 for (pindex = 0; pindex < n_patches; ++pindex) {
2032 MonoJumpInfo *ji = &patches [pindex];
2034 if (!aot_module->got [got_slots [pindex]]) {
2035 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2036 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2037 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
2039 ji->type = MONO_PATCH_INFO_NONE;
2045 mono_mempool_destroy (mp);
2048 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2052 method = mono_get_method (image, token, NULL);
2054 full_name = mono_method_full_name (method, TRUE);
2057 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
2058 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2061 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);
2067 mono_jit_stats.methods_aot++;
2069 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2071 init_plt (aot_module);
2073 if (method && method->wrapper_type)
2074 g_hash_table_insert (aot_module->method_to_code, method, code);
2078 if (from_plt && klass && !klass->generic_container)
2079 mono_runtime_class_init (mono_class_vtable (domain, klass));
2084 /* FIXME: The space in domain->mp is wasted */
2085 if (aot_module->opts & MONO_OPT_SHARED)
2086 /* No need to cache patches */
2087 mono_mempool_destroy (mp);
2096 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
2098 guint32 table_size, entry_size, hash;
2099 guint32 *table, *entry;
2100 char *full_name = NULL;
2105 table_size = amodule->extra_method_table [0];
2106 table = amodule->extra_method_table + 1;
2109 if (method->wrapper_type) {
2110 /* FIXME: This is a hack to work around the fact that runtime invoke wrappers get assigned to some random class */
2111 if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
2112 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
2113 full_name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
2115 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
2116 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
2117 full_name = g_strdup_printf ("(wrapper delegate-invoke):%s (%s)", method->name, tmpsig);
2119 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_BEGIN_INVOKE) {
2120 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
2121 full_name = g_strdup_printf ("(wrapper delegate-begin-invoke):%s (%s)", method->name, tmpsig);
2123 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_END_INVOKE) {
2124 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
2125 full_name = g_strdup_printf ("(wrapper delegate-end-invoke):%s (%s)", method->name, tmpsig);
2128 full_name = mono_method_full_name (method, TRUE);
2132 if (method->wrapper_type)
2133 hash = g_str_hash (method->name) % table_size;
2135 hash = 0 % table_size;
2137 entry = &table [hash * entry_size];
2139 if (entry [0] != 0) {
2141 guint32 key = entry [0];
2142 guint32 value = entry [1];
2143 guint32 next = entry [entry_size - 1];
2148 // FIXME: Avoid fully decoding the method ref
2149 p = amodule->extra_method_info + key;
2150 is_wrapper = decode_value (p, &p);
2151 if (method->wrapper_type && is_wrapper) {
2152 if (!strcmp (full_name, (char*)p))
2155 m = decode_method_ref_2 (amodule, p, &p);
2161 entry = &table [next * entry_size];
2172 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2174 g_ptr_array_add ((GPtrArray*)user_data, value);
2178 * find_extra_method:
2180 * Try finding METHOD in the extra_method table in all AOT images.
2181 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2182 * module where the method was found.
2185 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2191 /* Try the method's module first */
2192 *out_amodule = method->klass->image->aot_module;
2193 index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
2194 if (index != 0xffffff)
2198 * Try all other modules.
2199 * This is needed because generic instances klass->image points to the image
2200 * containing the generic definition, but the native code is generated to the
2201 * AOT image which contains the reference.
2204 /* Make a copy to avoid doing the search inside the aot lock */
2205 modules = g_ptr_array_new ();
2207 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2211 for (i = 0; i < modules->len; ++i) {
2212 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2214 if (amodule != method->klass->image->aot_module)
2215 index = find_extra_method_in_amodule (amodule, method);
2216 if (index != 0xffffff) {
2217 *out_amodule = amodule;
2222 g_ptr_array_free (modules, TRUE);
2228 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2230 MonoClass *klass = method->klass;
2231 guint32 method_index;
2232 MonoAotModule *amodule = klass->image->aot_module;
2238 if (amodule->out_of_date)
2241 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2242 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2243 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2244 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2247 g_assert (klass->inited);
2249 /* Find method index */
2250 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2251 method = mono_method_get_declaring_generic_method (method);
2252 method_index = mono_metadata_token_index (method->token) - 1;
2253 } else if (method->is_inflated || !method->token) {
2254 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2256 code = g_hash_table_lookup (amodule->method_to_code, method);
2261 method_index = find_extra_method (method, &amodule);
2262 if (method_index == 0xffffff) {
2263 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2266 full_name = mono_method_full_name (method, TRUE);
2267 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2273 if (method_index == 0xffffff)
2276 /* Needed by find_jit_info */
2278 if (!amodule->extra_methods)
2279 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2280 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2284 method_index = mono_metadata_token_index (method->token) - 1;
2287 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2291 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2295 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2297 MonoAotModule *aot_module = image->aot_module;
2303 method_index = mono_metadata_token_index (token) - 1;
2305 return load_method (domain, aot_module, image, NULL, token, method_index);
2311 } IsGotEntryUserData;
2314 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2316 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2317 MonoAotModule *aot_module = (MonoAotModule*)value;
2319 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
2324 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2326 IsGotEntryUserData user_data;
2331 user_data.addr = addr;
2332 user_data.res = FALSE;
2334 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2337 return user_data.res;
2342 MonoAotModule *module;
2343 } FindAotModuleUserData;
2346 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2348 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2349 MonoAotModule *aot_module = (MonoAotModule*)value;
2351 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2352 data->module = aot_module;
2355 static inline MonoAotModule*
2356 find_aot_module (guint8 *code)
2358 FindAotModuleUserData user_data;
2363 /* Reading these need no locking */
2364 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2367 user_data.addr = code;
2368 user_data.module = NULL;
2371 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2374 return user_data.module;
2378 * mono_aot_plt_resolve:
2380 * This function is called by the entries in the PLT to resolve the actual method that
2381 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2384 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2386 #ifdef MONO_ARCH_AOT_SUPPORTED
2387 guint8 *p, *target, *plt_entry;
2389 MonoAotModule *module = (MonoAotModule*)aot_module;
2393 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2395 p = &module->got_info [plt_info_offset];
2397 ji.type = decode_value (p, &p);
2399 mp = mono_mempool_new_size (512);
2400 res = decode_patch (module, mp, &ji, p, &p);
2401 // FIXME: Error handling (how ?)
2404 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2406 mono_mempool_destroy (mp);
2408 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2409 plt_entry = mono_aot_get_plt_entry (code);
2410 g_assert (plt_entry);
2411 mono_arch_patch_plt_entry (plt_entry, target);
2415 g_assert_not_reached ();
2423 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2424 * method in the module is loaded to avoid committing memory by writing to it.
2425 * LOCKING: Assumes the AOT lock is held.
2428 init_plt (MonoAotModule *info)
2430 #ifdef MONO_ARCH_AOT_SUPPORTED
2432 guint8 *buf = info->plt;
2433 #elif defined(__x86_64__) || defined(__arm__)
2438 if (info->plt_inited)
2441 tramp = mono_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2444 /* Initialize the first PLT entry */
2445 make_writable (info->plt, info->plt_end - info->plt);
2446 x86_jump_code (buf, tramp);
2447 #elif defined(__x86_64__) || defined(__arm__)
2449 * Initialize the PLT entries in the GOT to point to the default targets.
2452 /* The first entry points to the AOT trampoline */
2453 ((gpointer*)info->got)[info->plt_got_offset_base] = tramp;
2454 for (i = 1; i < info->plt_size; ++i)
2455 /* All the default entries point to the first entry */
2456 ((gpointer*)info->got)[info->plt_got_offset_base + i] = info->plt;
2458 g_assert_not_reached ();
2461 info->plt_inited = TRUE;
2466 * mono_aot_get_plt_entry:
2468 * Return the address of the PLT entry called by the code at CODE if exists.
2471 mono_aot_get_plt_entry (guint8 *code)
2473 MonoAotModule *aot_module = find_aot_module (code);
2474 #if defined(__arm__)
2481 #if defined(__i386__) || defined(__x86_64__)
2482 if (code [-5] == 0xe8) {
2483 guint32 disp = *(guint32*)(code - 4);
2484 guint8 *target = code + disp;
2486 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2489 #elif defined(__arm__)
2490 ins = ((guint32*)(gpointer)code) [-1];
2492 /* Should be a 'bl' */
2493 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2494 gint32 disp = ((gint32)ins) & 0xffffff;
2495 guint8 *target = code - 4 + 8 + (disp * 4);
2497 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2501 g_assert_not_reached ();
2508 * mono_aot_get_plt_info_offset:
2510 * Return the PLT info offset belonging to the plt entry called by CODE.
2513 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2515 guint8 *plt_entry = mono_aot_get_plt_entry (code);
2517 g_assert (plt_entry);
2519 /* The offset is embedded inside the code after the plt entry */
2520 #if defined(__i386__)
2521 return *(guint32*)(plt_entry + 5);
2522 #elif defined(__x86_64__)
2523 return *(guint32*)(plt_entry + 6);
2524 #elif defined(__arm__)
2525 /* The offset is stored as the 5th word of the plt entry */
2526 return ((guint32*)plt_entry) [4];
2528 g_assert_not_reached ();
2534 load_named_code (MonoAotModule *amodule, const char *name)
2538 int n_patches, got_index, pindex;
2544 symbol = g_strdup_printf ("%s", name);
2545 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2548 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
2550 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2554 symbol = g_strdup_printf ("%s_p", name);
2555 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2558 /* Nothing to patch */
2561 /* Similar to mono_aot_load_method () */
2563 n_patches = decode_value (p, &p);
2566 MonoJumpInfo *patches;
2569 mp = mono_mempool_new ();
2571 got_index = decode_value (p, &p);
2573 patches = load_patch_info (amodule, mp, n_patches, got_index, &got_slots, p, &p);
2576 for (pindex = 0; pindex < n_patches; ++pindex) {
2577 MonoJumpInfo *ji = &patches [pindex];
2581 * When this code is executed, the runtime may not yet initalized, so
2582 * resolve the patch info by hand.
2584 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2585 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2586 target = mono_get_lmf_addr;
2587 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2588 target = mono_thread_force_interruption_checkpoint;
2589 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2590 target = mono_exception_from_token;
2591 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2592 target = mono_get_throw_exception ();
2594 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2595 target = mono_amd64_throw_exception;
2598 } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
2599 target = mono_amd64_get_original_ip;
2602 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2603 target = mono_arm_throw_exception;
2605 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2606 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2607 target = (gpointer)mono_get_trampoline_func (tramp_type2);
2608 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
2609 /* atoll is needed because the the offset is unsigned */
2613 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
2614 g_assert (res == 1);
2615 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
2616 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
2617 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
2618 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
2619 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
2620 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
2621 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
2622 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
2623 target = mono_thread_get_and_clear_pending_exception;
2625 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
2626 g_assert_not_reached ();
2630 /* Hopefully the code doesn't have patches which need method or
2633 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2636 amodule->got [got_slots [pindex]] = target;
2641 mono_mempool_destroy (mp);
2648 * Return the piece of code identified by NAME from the mscorlib AOT file.
2651 mono_aot_get_named_code (const char *name)
2654 MonoAotModule *amodule;
2656 image = mono_defaults.corlib;
2659 amodule = image->aot_module;
2662 return load_named_code (amodule, name);
2666 * Return a specific trampoline from the AOT file.
2669 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2671 MonoAotModule *amodule;
2672 int index, tramp_size;
2673 guint8 *code, *tramp;
2674 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2676 /* Currently, we keep all trampolines in the mscorlib AOT image */
2677 image = mono_defaults.corlib;
2682 amodule = image->aot_module;
2685 if (amodule->trampoline_index == amodule->num_trampolines)
2686 g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_trampolines);
2688 index = amodule->trampoline_index ++;
2692 if (!generic_trampolines [tramp_type]) {
2695 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2696 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2700 tramp = generic_trampolines [tramp_type];
2703 amodule->got [amodule->trampoline_got_offset_base + (index *2)] = tramp;
2704 amodule->got [amodule->trampoline_got_offset_base + (index *2) + 1] = arg1;
2708 #elif defined(__arm__)
2712 g_assert_not_reached ();
2715 code = amodule->trampolines + (index * tramp_size);
2717 *code_len = tramp_size;
2723 mono_aot_get_unbox_trampoline (MonoMethod *method)
2725 guint32 method_index = mono_metadata_token_index (method->token) - 1;
2726 MonoAotModule *amodule;
2730 amodule = method->klass->image->aot_module;
2733 symbol = g_strdup_printf ("unbox_trampoline_%d", method_index);
2734 code = load_named_code (amodule, symbol);
2740 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2745 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
2746 code = load_named_code (mono_defaults.corlib->aot_module, symbol);
2755 mono_aot_init (void)
2760 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2766 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2772 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2778 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2784 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2790 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2796 mono_aot_get_plt_entry (guint8 *code)
2802 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2808 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2814 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2816 g_assert_not_reached ();
2822 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2824 g_assert_not_reached ();
2829 mono_aot_get_named_code (const char *name)
2831 g_assert_not_reached ();
2836 mono_aot_get_unbox_trampoline (MonoMethod *method)
2838 g_assert_not_reached ();
2843 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2845 g_assert_not_reached ();