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/metadata/monitor.h>
51 #include <mono/utils/mono-logger.h>
52 #include "mono/utils/mono-compiler.h"
60 #define SHARED_EXT ".dll"
61 #elif (defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)
62 #define SHARED_EXT ".dylib"
64 #define SHARED_EXT ".so"
67 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
68 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
70 typedef struct MonoAotModule {
72 /* Optimization flags used to compile the module */
74 /* Pointer to the Global Offset Table */
76 guint32 got_size, plt_size;
77 GHashTable *name_cache;
78 GHashTable *extra_methods;
79 /* Maps methods to their code */
80 GHashTable *method_to_code;
81 MonoAssemblyName *image_names;
83 MonoAssembly *assembly;
84 MonoImage **image_table;
85 guint32 image_table_len;
94 guint32 plt_got_offset_base;
95 guint32 *code_offsets;
97 guint32 *method_info_offsets;
99 guint32 *got_info_offsets;
101 guint32 *ex_info_offsets;
102 guint32 *method_order;
103 guint32 *method_order_end;
105 guint32 *class_info_offsets;
106 guint32 *methods_loaded;
107 guint16 *class_name_table;
108 guint32 *extra_method_table;
109 guint32 *extra_method_info_offsets;
110 guint8 *extra_method_info;
112 guint32 num_trampolines, trampoline_got_offset_base, trampoline_index;
117 /* This structure is stored in the AOT file */
118 typedef struct MonoAotFileInfo
120 guint32 plt_got_offset_base;
121 guint32 trampoline_got_offset_base;
122 guint32 num_trampolines;
128 static GHashTable *aot_modules;
129 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
130 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
131 static CRITICAL_SECTION aot_mutex;
134 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
135 * AOT modules registered by mono_aot_register_module ().
137 static GHashTable *static_aot_modules;
140 * Disabling this will make a copy of the loaded code and use the copy instead
141 * of the original. This will place the caller and the callee close to each
142 * other in memory, possibly improving cache behavior. Since the original
143 * code is in copy-on-write memory, this will not increase the memory usage
146 static gboolean use_loaded_code = TRUE;
149 * Whenever to AOT compile loaded assemblies on demand and store them in
150 * a cache under $HOME/.mono/aot-cache.
152 static gboolean use_aot_cache = FALSE;
155 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
156 * use_aot_cache is TRUE.
158 static gboolean spawn_compiler = TRUE;
161 static gint32 mono_last_aot_method = -1;
163 static gboolean make_unreadable = FALSE;
164 static guint32 name_table_accesses = 0;
166 /* Used to speed-up find_aot_module () */
167 static gsize aot_code_low_addr = (gssize)-1;
168 static gsize aot_code_high_addr = 0;
170 /* Used to communicate with mono_aot_register_globals () */
171 static guint32 globals_tls_id = -1;
174 init_plt (MonoAotModule *info);
177 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
178 guint32 got_index, guint32 **got_slots,
179 guint8 *buf, guint8 **endbuf);
181 /*****************************************************/
183 /*****************************************************/
186 load_image (MonoAotModule *module, int index)
188 MonoAssembly *assembly;
189 MonoImageOpenStatus status;
191 g_assert (index < module->image_table_len);
193 if (module->image_table [index])
194 return module->image_table [index];
195 if (module->out_of_date)
198 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
200 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);
201 module->out_of_date = TRUE;
205 if (strcmp (assembly->image->guid, module->image_guids [index])) {
206 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);
207 module->out_of_date = TRUE;
211 module->image_table [index] = assembly->image;
212 return assembly->image;
217 decode_value (guint8 *ptr, guint8 **rptr)
222 if ((b & 0x80) == 0){
225 } else if ((b & 0x40) == 0){
226 len = ((b & 0x3f) << 8 | ptr [1]);
228 } else if (b != 0xff) {
229 len = ((b & 0x1f) << 24) |
236 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
242 //printf ("DECODE: %d.\n", len);
247 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
250 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
252 static MonoGenericInst*
253 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
256 MonoType **type_argv;
257 MonoGenericInst *inst;
260 type_argc = decode_value (p, &p);
261 type_argv = g_new0 (MonoType*, type_argc);
263 for (i = 0; i < type_argc; ++i) {
264 MonoClass *pclass = decode_klass_ref (module, p, &p);
269 type_argv [i] = &pclass->byval_arg;
272 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
281 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
283 gboolean has_class_inst, has_method_inst;
286 has_class_inst = decode_value (p, &p);
287 if (has_class_inst) {
288 ctx->class_inst = decode_generic_inst (module, p, &p);
289 if (!ctx->class_inst)
292 has_method_inst = decode_value (p, &p);
293 if (has_method_inst) {
294 ctx->method_inst = decode_generic_inst (module, p, &p);
295 if (!ctx->method_inst)
304 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
307 MonoClass *klass, *eklass;
311 token = decode_value (p, &p);
316 if (mono_metadata_token_table (token) == 0) {
317 image = load_image (module, decode_value (p, &p));
320 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
321 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
322 if (token == MONO_TOKEN_TYPE_SPEC) {
323 MonoTypeEnum type = decode_value (p, &p);
325 if (type == MONO_TYPE_GENERICINST) {
327 MonoGenericContext ctx;
330 gclass = decode_klass_ref (module, p, &p);
331 g_assert (gclass->generic_container);
333 memset (&ctx, 0, sizeof (ctx));
334 ctx.class_inst = decode_generic_inst (module, p, &p);
337 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
338 klass = mono_class_from_mono_type (type);
339 mono_metadata_free_type (type);
340 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
343 MonoGenericContainer *container;
345 // FIXME: Maybe use types directly to avoid
346 // the overhead of creating MonoClass-es
348 // FIXME: Memory management
349 t = g_new0 (MonoType, 1);
351 t->data.generic_param = g_new0 (MonoGenericParam, 1);
352 t->data.generic_param->num = decode_value (p, &p);
353 t->data.generic_param->name = "T";
355 is_method = decode_value (p, &p);
357 MonoMethod *method_def = decode_method_ref_2 (module, p, &p);
360 g_free (t->data.generic_param);
365 container = mono_method_get_generic_container (method_def);
367 MonoClass *class_def = decode_klass_ref (module, p, &p);
370 g_free (t->data.generic_param);
375 container = class_def->generic_container;
378 g_assert (container);
379 t->data.generic_param->owner = container;
381 klass = mono_class_from_mono_type (t);
383 g_assert_not_reached ();
386 image = load_image (module, decode_value (p, &p));
389 klass = mono_class_get (image, token);
391 } else if (token == MONO_TOKEN_TYPE_DEF) {
393 image = load_image (module, decode_value (p, &p));
396 rank = decode_value (p, &p);
397 eklass = decode_klass_ref (module, p, &p);
398 klass = mono_array_class_get (eklass, rank);
400 g_assert_not_reached ();
403 mono_class_init (klass);
409 static MonoClassField*
410 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
412 MonoClass *klass = decode_klass_ref (module, buf, &buf);
419 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
423 return mono_class_get_field (klass, token);
429 * Decode a method reference, and return its image and token. This avoids loading
430 * metadata for the method if the caller does not need it. If the method has no token,
431 * then it is loaded from metadata and METHOD is set to the method instance.
434 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
436 guint32 image_index, value;
442 if (no_aot_trampoline)
443 *no_aot_trampoline = FALSE;
445 value = decode_value (p, &p);
446 image_index = value >> 24;
448 if (image_index == 252) {
449 if (no_aot_trampoline)
450 *no_aot_trampoline = TRUE;
451 value = decode_value (p, &p);
452 image_index = value >> 24;
455 if (image_index == 253) {
457 guint32 wrapper_type;
459 wrapper_type = decode_value (p, &p);
462 image = mono_defaults.corlib;
464 switch (wrapper_type) {
465 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
466 MonoMethod *m = decode_method_ref_2 (module, p, &p);
470 mono_class_init (m->klass);
471 *method = mono_marshal_get_remoting_invoke_with_check (m);
474 case MONO_WRAPPER_PROXY_ISINST: {
475 MonoClass *klass = decode_klass_ref (module, p, &p);
478 *method = mono_marshal_get_proxy_cancast (klass);
481 case MONO_WRAPPER_LDFLD:
482 case MONO_WRAPPER_LDFLDA:
483 case MONO_WRAPPER_STFLD:
484 case MONO_WRAPPER_ISINST: {
485 MonoClass *klass = decode_klass_ref (module, p, &p);
488 if (wrapper_type == MONO_WRAPPER_LDFLD)
489 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
490 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
491 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
492 else if (wrapper_type == MONO_WRAPPER_STFLD)
493 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
494 else if (wrapper_type == MONO_WRAPPER_ISINST)
495 *method = mono_marshal_get_isinst (klass);
497 g_assert_not_reached ();
500 case MONO_WRAPPER_LDFLD_REMOTE:
501 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
503 case MONO_WRAPPER_STFLD_REMOTE:
504 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
506 case MONO_WRAPPER_ALLOC: {
507 int atype = decode_value (p, &p);
509 *method = mono_gc_get_managed_allocator_by_type (atype);
512 case MONO_WRAPPER_STELEMREF:
513 *method = mono_marshal_get_stelemref ();
515 case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
516 MonoMethod *m = decode_method_ref_2 (module, p, &p);
520 *method = mono_marshal_get_static_rgctx_invoke (m);
523 case MONO_WRAPPER_UNKNOWN: {
524 MonoMethodDesc *desc;
525 MonoMethod *orig_method;
526 int subtype = decode_value (p, &p);
528 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
529 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
530 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
531 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
533 g_assert_not_reached ();
534 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
535 g_assert (orig_method);
536 mono_method_desc_free (desc);
537 *method = mono_monitor_get_fast_path (orig_method);
541 g_assert_not_reached ();
543 } else if (image_index == 255) {
545 image_index = decode_value (p, &p);
546 *token = decode_value (p, &p);
548 image = load_image (module, image_index);
551 } else if (image_index == 254) {
552 /* Method on generic instance */
554 MonoGenericContext ctx;
557 * These methods do not have a token which resolves them, so we
558 * resolve them immediately.
560 klass = decode_klass_ref (module, p, &p);
564 image_index = decode_value (p, &p);
565 *token = decode_value (p, &p);
567 image = load_image (module, image_index);
571 *method = mono_get_method_full (image, *token, NULL, NULL);
575 memset (&ctx, 0, sizeof (ctx));
577 if (FALSE && klass->generic_class) {
578 ctx.class_inst = klass->generic_class->context.class_inst;
579 ctx.method_inst = NULL;
581 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
584 memset (&ctx, 0, sizeof (ctx));
586 if (!decode_generic_context (module, &ctx, p, &p))
589 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
591 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
593 image = load_image (module, image_index);
604 * decode_method_ref_2:
606 * Similar to decode_method_ref, but resolve and return the method itself.
609 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
613 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
619 return mono_get_method (image, token, NULL);
624 make_writable (guint8* addr, guint32 len)
626 #ifndef PLATFORM_WIN32
631 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
633 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
634 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
635 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
640 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
646 create_cache_structure (void)
652 home = g_get_home_dir ();
656 tmp = g_build_filename (home, ".mono", NULL);
657 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
658 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
659 #ifdef PLATFORM_WIN32
662 err = mkdir (tmp, 0777);
665 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
671 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
672 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
673 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
674 #ifdef PLATFORM_WIN32
677 err = mkdir (tmp, 0777);
680 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
689 * load_aot_module_from_cache:
691 * Experimental code to AOT compile loaded assemblies on demand.
694 * - Add environment variable MONO_AOT_CACHE_OPTIONS
695 * - Add options for controlling the cache size
696 * - Handle full cache by deleting old assemblies lru style
697 * - Add options for excluding assemblies during development
698 * - Maybe add a threshold after an assembly is AOT compiled
699 * - invoking a new mono process is a security risk
700 * - recompile the AOT module if one of its dependencies changes
703 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
705 char *fname, *cmd, *tmp2, *aot_options;
714 if (assembly->image->dynamic)
717 create_cache_structure ();
719 home = g_get_home_dir ();
721 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
722 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
726 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
727 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
730 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
732 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
734 aot_options = g_strdup_printf ("outfile=%s", fname);
736 if (spawn_compiler) {
737 /* FIXME: security */
738 /* FIXME: Has to pass the assembly loading path to the child process */
739 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
741 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
743 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
745 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
746 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
748 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
755 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
757 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
759 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
763 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
765 g_free (aot_options);
772 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
778 for (i = 0; globals [i]; i+= 2) {
779 if (strcmp (globals [i], name) == 0) {
780 *value = globals [i + 1];
785 mono_dl_symbol (module, name, value);
790 load_aot_module (MonoAssembly *assembly, gpointer user_data)
793 MonoAotModule *amodule;
795 gboolean usable = TRUE;
796 char *saved_guid = NULL;
797 char *aot_version = NULL;
798 char *runtime_version, *build_info;
799 char *opt_flags = NULL;
801 gboolean full_aot = FALSE;
802 MonoAotFileInfo *file_info = NULL;
805 if (mono_compile_aot)
808 if (assembly->image->aot_module)
810 * Already loaded. This can happen because the assembly loading code might invoke
811 * the assembly load hooks multiple times for the same assembly.
815 if (assembly->image->dynamic)
818 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
822 if (static_aot_modules)
823 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
829 /* Statically linked AOT module */
831 aot_name = g_strdup_printf ("%s", assembly->aname.name);
832 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
834 TlsSetValue (globals_tls_id, NULL);
837 sofile = load_aot_module_from_cache (assembly, &aot_name);
840 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
842 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
845 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
851 * If the image was compiled in no-dlsym mode, it contains no global symbols,
852 * instead it contains an ELF ctor function which is called by dlopen () which
853 * in turn calls mono_aot_register_globals () to register a table which contains
854 * the name and address of the globals.
856 globals = TlsGetValue (globals_tls_id);
857 TlsSetValue (globals_tls_id, NULL);
860 if (!sofile && !globals) {
862 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
869 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
870 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
871 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
872 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
874 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
875 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);
879 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
880 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
885 build_info = mono_get_runtime_build_info ();
886 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
887 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);
895 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
897 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
901 if (mono_aot_only && !full_aot) {
902 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
905 if (!mono_aot_only && full_aot) {
906 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
912 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
917 mono_dl_close (sofile);
918 assembly->image->aot_module = NULL;
922 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
923 g_assert (file_info);
925 amodule = g_new0 (MonoAotModule, 1);
926 amodule->aot_name = aot_name;
927 amodule->assembly = assembly;
928 amodule->plt_got_offset_base = file_info->plt_got_offset_base;
929 amodule->num_trampolines = file_info->num_trampolines;
930 amodule->trampoline_got_offset_base = file_info->trampoline_got_offset_base;
931 amodule->got_size = file_info->got_size;
932 amodule->plt_size = file_info->plt_size;
933 amodule->got = file_info->got;
934 amodule->got [0] = assembly->image;
935 amodule->globals = globals;
936 amodule->sofile = sofile;
937 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
939 sscanf (opt_flags, "%d", &amodule->opts);
941 /* Read image table */
943 guint32 table_len, i;
946 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
949 table_len = *(guint32*)table;
950 table += sizeof (guint32);
951 amodule->image_table = g_new0 (MonoImage*, table_len);
952 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
953 amodule->image_guids = g_new0 (char*, table_len);
954 amodule->image_table_len = table_len;
955 for (i = 0; i < table_len; ++i) {
956 MonoAssemblyName *aname = &(amodule->image_names [i]);
958 aname->name = g_strdup (table);
959 table += strlen (table) + 1;
960 amodule->image_guids [i] = g_strdup (table);
961 table += strlen (table) + 1;
963 aname->culture = g_strdup (table);
964 table += strlen (table) + 1;
965 memcpy (aname->public_key_token, table, strlen (table) + 1);
966 table += strlen (table) + 1;
968 table = ALIGN_PTR_TO (table, 8);
969 aname->flags = *(guint32*)table;
971 aname->major = *(guint32*)table;
973 aname->minor = *(guint32*)table;
975 aname->build = *(guint32*)table;
977 aname->revision = *(guint32*)table;
982 /* Read method and method_info tables */
983 find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
984 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
985 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
986 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
987 find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
988 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
989 find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
990 find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
991 find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
992 find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
993 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
994 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
995 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
996 find_symbol (sofile, globals, "extra_method_info", (gpointer *)&amodule->extra_method_info);
997 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
998 find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
999 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1000 find_symbol (sofile, globals, "trampolines", (gpointer*)&amodule->trampolines);
1001 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1003 amodule->mem_begin = amodule->code;
1005 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1006 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1008 if (make_unreadable) {
1009 #ifndef PLATFORM_WIN32
1012 int pages, err, len;
1014 addr = amodule->mem_begin;
1015 len = amodule->mem_end - amodule->mem_begin;
1017 /* Round down in both directions to avoid modifying data which is not ours */
1018 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
1019 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
1020 err = mprotect (page_start, pages * PAGESIZE, 0);
1021 g_assert (err == 0);
1027 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1028 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1030 g_hash_table_insert (aot_modules, assembly, amodule);
1033 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1035 assembly->image->aot_module = amodule;
1038 * Since we store methoddef and classdef tokens when referring to methods/classes in
1039 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1040 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1041 * non-lazily, since we can't handle out-of-date errors later.
1043 for (i = 0; i < amodule->image_table_len; ++i)
1044 load_image (amodule, i);
1046 if (amodule->out_of_date) {
1047 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);
1048 if (mono_aot_only) {
1049 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);
1054 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1058 * mono_aot_register_globals:
1060 * This is called by the ctor function in AOT images compiled with the
1061 * 'no-dlsym' option.
1064 mono_aot_register_globals (gpointer *globals)
1066 TlsSetValue (globals_tls_id, globals);
1070 * mono_aot_register_module:
1072 * This should be called by embedding code to register AOT modules statically linked
1073 * into the executable. AOT_INFO should be the value of the
1074 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1077 mono_aot_register_module (gpointer *aot_info)
1085 /* Determine the assembly name */
1086 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1089 /* This could be called before startup */
1093 if (!static_aot_modules)
1094 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1096 g_hash_table_insert (static_aot_modules, aname, globals);
1103 mono_aot_init (void)
1105 InitializeCriticalSection (&aot_mutex);
1106 aot_modules = g_hash_table_new (NULL, NULL);
1107 globals_tls_id = TlsAlloc ();
1109 mono_install_assembly_load_hook (load_aot_module, NULL);
1111 if (getenv ("MONO_LASTAOT"))
1112 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
1113 if (getenv ("MONO_AOT_CACHE"))
1114 use_aot_cache = TRUE;
1118 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1122 info->vtable_size = decode_value (buf, &buf);
1123 if (info->vtable_size == -1)
1126 flags = decode_value (buf, &buf);
1127 info->ghcimpl = (flags >> 0) & 0x1;
1128 info->has_finalize = (flags >> 1) & 0x1;
1129 info->has_cctor = (flags >> 2) & 0x1;
1130 info->has_nested_classes = (flags >> 3) & 0x1;
1131 info->blittable = (flags >> 4) & 0x1;
1132 info->has_references = (flags >> 5) & 0x1;
1133 info->has_static_refs = (flags >> 6) & 0x1;
1134 info->no_special_static_fields = (flags >> 7) & 0x1;
1136 if (info->has_cctor) {
1137 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1141 if (info->has_finalize) {
1142 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1143 if (!info->finalize_image)
1147 info->instance_size = decode_value (buf, &buf);
1148 info->class_size = decode_value (buf, &buf);
1149 info->packing_size = decode_value (buf, &buf);
1150 info->min_align = decode_value (buf, &buf);
1158 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1161 MonoClass *klass = vtable->klass;
1162 MonoAotModule *aot_module = klass->image->aot_module;
1164 MonoCachedClassInfo class_info;
1168 gboolean no_aot_trampoline;
1170 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1173 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1176 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1180 for (i = 0; i < slot; ++i)
1181 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1183 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1186 if (no_aot_trampoline)
1189 if (mono_metadata_token_index (token) == 0)
1192 return mono_aot_get_method_from_token (domain, image, token);
1196 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1198 MonoAotModule *aot_module = klass->image->aot_module;
1202 if (klass->rank || !aot_module)
1205 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1207 err = decode_cached_class_info (aot_module, res, p, &p);
1215 * mono_aot_get_class_from_name:
1217 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1218 * using a cache stored in the AOT file.
1219 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1221 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1225 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1227 MonoAotModule *aot_module = image->aot_module;
1228 guint16 *table, *entry;
1231 char full_name_buf [1024];
1233 const char *name2, *name_space2;
1235 guint32 cols [MONO_TYPEDEF_SIZE];
1236 GHashTable *nspace_table;
1238 if (!aot_module || !aot_module->class_name_table)
1245 /* First look in the cache */
1246 if (!aot_module->name_cache)
1247 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1248 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1250 *klass = g_hash_table_lookup (nspace_table, name);
1257 table_size = aot_module->class_name_table [0];
1258 table = aot_module->class_name_table + 1;
1260 if (name_space [0] == '\0')
1261 full_name = g_strdup_printf ("%s", name);
1263 if (strlen (name_space) + strlen (name) < 1000) {
1264 sprintf (full_name_buf, "%s.%s", name_space, name);
1265 full_name = full_name_buf;
1267 full_name = g_strdup_printf ("%s.%s", name_space, name);
1270 hash = g_str_hash (full_name) % table_size;
1271 if (full_name != full_name_buf)
1274 entry = &table [hash * 2];
1276 if (entry [0] != 0) {
1277 t = &image->tables [MONO_TABLE_TYPEDEF];
1280 guint32 index = entry [0];
1281 guint32 next = entry [1];
1282 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1284 name_table_accesses ++;
1286 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1288 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1289 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1291 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1293 *klass = mono_class_get (image, token);
1298 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1299 if (!nspace_table) {
1300 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1301 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1303 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1310 entry = &table [next * 2];
1323 * LOCKING: Acquires the domain lock.
1326 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
1327 MonoMethod *method, guint8* ex_info, guint8 *code)
1331 guint code_len, used_int_regs;
1332 gboolean has_generic_jit_info;
1334 MonoMethodHeader *header;
1335 int generic_info_size;
1337 header = mono_method_get_header (method);
1339 /* Load the method info from the AOT file */
1342 code_len = decode_value (p, &p);
1343 used_int_regs = decode_value (p, &p);
1344 has_generic_jit_info = decode_value (p, &p);
1345 if (has_generic_jit_info)
1346 generic_info_size = sizeof (MonoGenericJitInfo);
1348 generic_info_size = 0;
1350 /* Exception table */
1351 if (header && header->num_clauses) {
1352 mono_domain_lock (domain);
1354 mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
1355 jinfo->num_clauses = header->num_clauses;
1356 mono_domain_unlock (domain);
1358 for (i = 0; i < header->num_clauses; ++i) {
1359 MonoExceptionClause *ec = &header->clauses [i];
1360 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1362 ei->flags = ec->flags;
1363 ei->exvar_offset = decode_value (p, &p);
1365 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1366 ei->data.filter = code + decode_value (p, &p);
1368 ei->data.catch_class = ec->data.catch_class;
1370 ei->try_start = code + decode_value (p, &p);
1371 ei->try_end = code + decode_value (p, &p);
1372 ei->handler_start = code + decode_value (p, &p);
1376 mono_domain_lock (domain);
1377 jinfo = mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + generic_info_size);
1378 mono_domain_unlock (domain);
1381 jinfo->code_size = code_len;
1382 jinfo->used_regs = used_int_regs;
1383 jinfo->method = method;
1384 jinfo->code_start = code;
1385 jinfo->domain_neutral = 0;
1387 if (has_generic_jit_info) {
1388 MonoGenericJitInfo *gi;
1390 jinfo->has_generic_jit_info = 1;
1392 gi = mono_jit_info_get_generic_jit_info (jinfo);
1395 gi->has_this = decode_value (p, &p);
1396 gi->this_reg = decode_value (p, &p);
1397 gi->this_offset = decode_value (p, &p);
1399 /* This currently contains no data */
1400 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1402 jinfo->method = decode_method_ref_2 (aot_module, p, &p);
1405 /* Load debug info */
1406 buf_len = decode_value (p, &p);
1407 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1413 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1416 int pos, left, right, offset, offset1, offset2, last_offset, new_offset;
1417 int page_index, method_index, table_len, is_wrapper;
1419 MonoAotModule *amodule = image->aot_module;
1422 guint8 *code, *ex_info, *p;
1423 guint32 *table, *ptr;
1429 if (domain != mono_get_root_domain ())
1433 offset = (guint8*)addr - amodule->code;
1435 /* First search through the index */
1436 ptr = amodule->method_order;
1441 if (*ptr == 0xffffff)
1445 while (*ptr != 0xffffff) {
1446 guint32 method_index = ptr [0];
1447 new_offset = amodule->code_offsets [method_index];
1449 if (offset >= last_offset && offset < new_offset) {
1455 last_offset = new_offset;
1459 /* Skip rest of index */
1460 while (*ptr != 0xffffff)
1465 table_len = amodule->method_order_end - table;
1467 g_assert (table <= amodule->method_order_end);
1470 left = (page_index * 1024);
1471 right = left + 1024;
1473 if (right > table_len)
1476 offset1 = amodule->code_offsets [table [left]];
1477 g_assert (offset1 <= offset);
1479 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1482 //printf ("Not found in index: 0x%x\n", offset);
1487 /* Binary search inside the method_order table to find the method */
1489 pos = (left + right) / 2;
1491 g_assert (table + pos <= amodule->method_order_end);
1493 //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]]);
1495 offset1 = amodule->code_offsets [table [pos]];
1496 if (table + pos + 1 >= amodule->method_order_end)
1497 offset2 = amodule->code_end - amodule->code;
1499 offset2 = amodule->code_offsets [table [pos + 1]];
1501 if (offset < offset1)
1503 else if (offset >= offset2)
1509 method_index = table [pos];
1511 /* Might be a wrapper/extra method */
1512 if (amodule->extra_methods) {
1514 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
1521 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
1523 * This is hit for extra methods which are called directly, so they are
1524 * not in amodule->extra_methods.
1526 table_len = amodule->extra_method_info_offsets [0];
1527 table = amodule->extra_method_info_offsets + 1;
1534 pos = ((left + right) / 2);
1536 g_assert (pos < table_len);
1538 if (table [pos * 2] < method_index)
1540 else if (table [pos * 2] > method_index)
1546 p = amodule->extra_method_info + table [(pos * 2) + 1];
1547 is_wrapper = decode_value (p, &p);
1548 g_assert (!is_wrapper);
1549 method = decode_method_ref_2 (amodule, p, &p);
1552 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1553 method = mono_get_method (image, token, NULL);
1560 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1562 code = &amodule->code [amodule->code_offsets [method_index]];
1563 ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
1565 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code);
1567 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1568 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1570 /* Add it to the normal JitInfo tables */
1571 mono_jit_info_table_add (domain, jinfo);
1576 /* Keep it in sync with the version in aot-compiler.c */
1577 static inline gboolean
1578 is_shared_got_patch (MonoJumpInfo *patch_info)
1580 switch (patch_info->type) {
1581 case MONO_PATCH_INFO_VTABLE:
1582 case MONO_PATCH_INFO_CLASS:
1583 case MONO_PATCH_INFO_IID:
1584 case MONO_PATCH_INFO_ADJUSTED_IID:
1585 case MONO_PATCH_INFO_FIELD:
1586 case MONO_PATCH_INFO_SFLDA:
1587 case MONO_PATCH_INFO_DECLSEC:
1588 case MONO_PATCH_INFO_LDTOKEN:
1589 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1590 case MONO_PATCH_INFO_RVA:
1591 case MONO_PATCH_INFO_METHODCONST:
1599 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1607 case MONO_PATCH_INFO_METHOD:
1608 case MONO_PATCH_INFO_METHOD_JUMP:
1609 case MONO_PATCH_INFO_ICALL_ADDR:
1610 case MONO_PATCH_INFO_METHOD_RGCTX: {
1613 gboolean no_aot_trampoline;
1615 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
1619 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1620 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1621 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1622 ji->type = MONO_PATCH_INFO_ABS;
1626 ji->data.method = method;
1628 ji->data.method = mono_get_method (image, token, NULL);
1629 g_assert (ji->data.method);
1630 mono_class_init (ji->data.method->klass);
1633 ji->data.method = mono_get_method (image, token, NULL);
1634 g_assert (ji->data.method);
1635 mono_class_init (ji->data.method->klass);
1640 case MONO_PATCH_INFO_INTERNAL_METHOD:
1641 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1642 guint32 len = decode_value (p, &p);
1644 ji->data.name = (char*)p;
1648 case MONO_PATCH_INFO_METHODCONST:
1650 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1651 if (!ji->data.method)
1654 case MONO_PATCH_INFO_VTABLE:
1655 case MONO_PATCH_INFO_CLASS:
1656 case MONO_PATCH_INFO_IID:
1657 case MONO_PATCH_INFO_ADJUSTED_IID:
1659 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1660 if (!ji->data.klass)
1663 case MONO_PATCH_INFO_CLASS_INIT:
1664 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1665 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1666 if (!ji->data.klass)
1669 case MONO_PATCH_INFO_IMAGE:
1670 ji->data.image = load_image (aot_module, decode_value (p, &p));
1671 if (!ji->data.image)
1674 case MONO_PATCH_INFO_FIELD:
1675 case MONO_PATCH_INFO_SFLDA:
1677 ji->data.field = decode_field_info (aot_module, p, &p);
1678 if (!ji->data.field)
1681 case MONO_PATCH_INFO_SWITCH:
1682 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1683 ji->data.table->table_size = decode_value (p, &p);
1684 table = g_new (gpointer, ji->data.table->table_size);
1685 ji->data.table->table = (MonoBasicBlock**)table;
1686 for (i = 0; i < ji->data.table->table_size; i++)
1687 table [i] = (gpointer)(gssize)decode_value (p, &p);
1689 case MONO_PATCH_INFO_R4: {
1692 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1693 val = decode_value (p, &p);
1694 *(float*)ji->data.target = *(float*)&val;
1697 case MONO_PATCH_INFO_R8: {
1700 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1702 val [0] = decode_value (p, &p);
1703 val [1] = decode_value (p, &p);
1704 *(double*)ji->data.target = *(double*)val;
1707 case MONO_PATCH_INFO_LDSTR:
1708 image = load_image (aot_module, decode_value (p, &p));
1711 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1713 case MONO_PATCH_INFO_RVA:
1714 case MONO_PATCH_INFO_DECLSEC:
1715 case MONO_PATCH_INFO_LDTOKEN:
1716 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1718 image = load_image (aot_module, decode_value (p, &p));
1721 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1723 ji->data.token->has_context = decode_value (p, &p);
1724 if (ji->data.token->has_context) {
1725 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
1730 case MONO_PATCH_INFO_EXC_NAME:
1731 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1732 if (!ji->data.klass)
1734 ji->data.name = ji->data.klass->name;
1736 case MONO_PATCH_INFO_METHOD_REL:
1737 ji->data.offset = decode_value (p, &p);
1739 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1740 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1741 case MONO_PATCH_INFO_MONITOR_ENTER:
1742 case MONO_PATCH_INFO_MONITOR_EXIT:
1744 case MONO_PATCH_INFO_RGCTX_FETCH: {
1746 MonoJumpInfoRgctxEntry *entry;
1748 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1749 entry->method = decode_method_ref_2 (aot_module, p, &p);
1750 entry->in_mrgctx = decode_value (p, &p);
1751 entry->info_type = decode_value (p, &p);
1752 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1753 entry->data->type = decode_value (p, &p);
1755 res = decode_patch (aot_module, mp, entry->data, p, &p);
1758 ji->data.rgctx_entry = entry;
1762 g_warning ("unhandled type %d", ji->type);
1763 g_assert_not_reached ();
1775 decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1781 if (is_shared_got_patch (ji)) {
1782 *got_offset = decode_value (p, &p);
1784 if (aot_module->got [*got_offset]) {
1785 /* Already loaded */
1786 //printf ("HIT!\n");
1788 shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1790 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1795 res = decode_patch (aot_module, mp, ji, p, &p);
1804 static MonoJumpInfo*
1805 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1806 guint32 got_index, guint32 **got_slots,
1807 guint8 *buf, guint8 **endbuf)
1809 MonoJumpInfo *patches;
1810 MonoJumpInfo *patch_info = NULL;
1812 guint32 last_offset;
1817 /* First load the type + offset table */
1819 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1821 for (pindex = 0; pindex < n_patches; ++pindex) {
1822 MonoJumpInfo *ji = &patches [pindex];
1827 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1828 ji->next = patch_info;
1832 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1833 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1835 /* Then load the other data */
1836 for (pindex = 0; pindex < n_patches; ++pindex) {
1837 MonoJumpInfo *ji = &patches [pindex];
1839 if (!decode_got_entry (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1842 if ((*got_slots) [pindex] == 0xffffffff)
1843 (*got_slots) [pindex] = got_index ++;
1850 g_free (*got_slots);
1857 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1860 * Jump addresses cannot be patched by the trampoline code since it
1861 * does not have access to the caller's address. Instead, we collect
1862 * the addresses of the GOT slots pointing to a method, and patch
1863 * them after the method has been compiled.
1865 MonoJitDomainInfo *info = domain_jit_info (domain);
1868 mono_domain_lock (domain);
1869 if (!info->jump_target_got_slot_hash)
1870 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1871 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1872 list = g_slist_prepend (list, got_slot);
1873 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
1874 mono_domain_unlock (domain);
1880 * Load the method identified by METHOD_INDEX from the AOT image. Return a
1881 * pointer to the native code of the method, or NULL if not found.
1882 * METHOD might not be set if the caller only has the image/token info.
1885 load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
1888 gboolean from_plt = method == NULL;
1890 int i, pindex, got_index = 0, n_patches, used_strings;
1891 gboolean keep_patches = TRUE;
1892 guint8 *p, *ex_info;
1893 MonoJitInfo *jinfo = NULL;
1894 guint8 *code, *info;
1896 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1899 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1900 /* Non shared AOT code can't be used in other appdomains */
1903 if (aot_module->out_of_date)
1906 if (aot_module->code_offsets [method_index] == 0xffffffff) {
1907 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1911 method = mono_get_method (image, token, NULL);
1912 full_name = mono_method_full_name (method, TRUE);
1913 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1919 code = &aot_module->code [aot_module->code_offsets [method_index]];
1920 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1923 if (!aot_module->methods_loaded)
1924 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1927 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1930 if (mono_last_aot_method != -1) {
1931 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1934 if (method && mono_jit_stats.methods_aot == mono_last_aot_method)
1935 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1941 klass = method->klass;
1942 decode_klass_ref (aot_module, p, &p);
1944 klass = decode_klass_ref (aot_module, p, &p);
1947 if (!use_loaded_code) {
1951 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (token) - 1]];
1952 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
1955 mono_domain_lock (domain);
1956 code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
1957 mono_domain_unlock (domain);
1958 memcpy (code2, code, jinfo->code_size);
1959 mono_arch_flush_icache (code2, jinfo->code_size);
1963 if (aot_module->opts & MONO_OPT_SHARED)
1964 used_strings = decode_value (p, &p);
1968 for (i = 0; i < used_strings; i++) {
1969 guint token = decode_value (p, &p);
1970 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
1973 if (aot_module->opts & MONO_OPT_SHARED)
1974 keep_patches = FALSE;
1976 n_patches = decode_value (p, &p);
1978 keep_patches = FALSE;
1981 MonoJumpInfo *patches;
1987 mp = mono_mempool_new ();
1989 got_index = decode_value (p, &p);
1991 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
1992 if (patches == NULL)
1995 for (pindex = 0; pindex < n_patches; ++pindex) {
1996 MonoJumpInfo *ji = &patches [pindex];
1998 if (!aot_module->got [got_slots [pindex]]) {
1999 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2000 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2001 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
2003 ji->type = MONO_PATCH_INFO_NONE;
2009 mono_mempool_destroy (mp);
2012 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2016 method = mono_get_method (image, token, NULL);
2018 full_name = mono_method_full_name (method, TRUE);
2021 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
2022 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2025 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);
2031 mono_jit_stats.methods_aot++;
2033 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2035 init_plt (aot_module);
2037 if (method && method->wrapper_type)
2038 g_hash_table_insert (aot_module->method_to_code, method, code);
2042 if (from_plt && klass && !klass->generic_container)
2043 mono_runtime_class_init (mono_class_vtable (domain, klass));
2048 /* FIXME: The space in domain->mp is wasted */
2049 if (aot_module->opts & MONO_OPT_SHARED)
2050 /* No need to cache patches */
2051 mono_mempool_destroy (mp);
2060 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
2062 guint32 table_size, entry_size, hash;
2063 guint32 *table, *entry;
2064 char *full_name = NULL;
2069 table_size = amodule->extra_method_table [0];
2070 table = amodule->extra_method_table + 1;
2073 if (method->wrapper_type) {
2074 /* FIXME: This is a hack to work around the fact that runtime invoke wrappers get assigned to some random class */
2075 if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
2076 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
2077 full_name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
2080 full_name = mono_method_full_name (method, TRUE);
2084 if (method->wrapper_type)
2085 hash = g_str_hash (method->name) % table_size;
2087 hash = 0 % table_size;
2089 entry = &table [hash * entry_size];
2091 if (entry [0] != 0) {
2093 guint32 key = entry [0];
2094 guint32 value = entry [1];
2095 guint32 next = entry [entry_size - 1];
2100 // FIXME: Avoid fully decoding the method ref
2101 p = amodule->extra_method_info + key;
2102 is_wrapper = decode_value (p, &p);
2103 if (method->wrapper_type && is_wrapper) {
2104 if (!strcmp (full_name, (char*)p))
2107 m = decode_method_ref_2 (amodule, p, &p);
2113 entry = &table [next * entry_size];
2124 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2126 g_ptr_array_add ((GPtrArray*)user_data, value);
2130 * find_extra_method:
2132 * Try finding METHOD in the extra_method table in all AOT images.
2133 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2134 * module where the method was found.
2137 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2143 /* Try the method's module first */
2144 *out_amodule = method->klass->image->aot_module;
2145 index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
2146 if (index != 0xffffff)
2150 * Try all other modules.
2151 * This is needed because generic instances klass->image points to the image
2152 * containing the generic definition, but the native code is generated to the
2153 * AOT image which contains the reference.
2156 /* Make a copy to avoid doing the search inside the aot lock */
2157 modules = g_ptr_array_new ();
2159 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2163 for (i = 0; i < modules->len; ++i) {
2164 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2166 if (amodule != method->klass->image->aot_module)
2167 index = find_extra_method_in_amodule (amodule, method);
2168 if (index != 0xffffff) {
2169 *out_amodule = amodule;
2174 g_ptr_array_free (modules, TRUE);
2180 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2182 MonoClass *klass = method->klass;
2183 guint32 method_index;
2184 MonoAotModule *amodule = klass->image->aot_module;
2190 if (amodule->out_of_date)
2193 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2194 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2195 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2196 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2199 g_assert (klass->inited);
2201 /* Find method index */
2202 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2203 method = mono_method_get_declaring_generic_method (method);
2204 method_index = mono_metadata_token_index (method->token) - 1;
2205 } else if (method->is_inflated || !method->token) {
2206 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2208 code = g_hash_table_lookup (amodule->method_to_code, method);
2213 method_index = find_extra_method (method, &amodule);
2214 if (method_index == 0xffffff) {
2215 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2218 full_name = mono_method_full_name (method, TRUE);
2219 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2225 if (method_index == 0xffffff)
2228 /* Needed by find_jit_info */
2230 if (!amodule->extra_methods)
2231 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2232 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2236 method_index = mono_metadata_token_index (method->token) - 1;
2239 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2243 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2247 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2249 MonoAotModule *aot_module = image->aot_module;
2255 method_index = mono_metadata_token_index (token) - 1;
2257 return load_method (domain, aot_module, image, NULL, token, method_index);
2263 } IsGotEntryUserData;
2266 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2268 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2269 MonoAotModule *aot_module = (MonoAotModule*)value;
2271 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
2276 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2278 IsGotEntryUserData user_data;
2283 user_data.addr = addr;
2284 user_data.res = FALSE;
2286 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2289 return user_data.res;
2294 MonoAotModule *module;
2295 } FindAotModuleUserData;
2298 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2300 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2301 MonoAotModule *aot_module = (MonoAotModule*)value;
2303 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2304 data->module = aot_module;
2307 static inline MonoAotModule*
2308 find_aot_module (guint8 *code)
2310 FindAotModuleUserData user_data;
2315 /* Reading these need no locking */
2316 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2319 user_data.addr = code;
2320 user_data.module = NULL;
2323 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2326 return user_data.module;
2330 * mono_aot_plt_resolve:
2332 * This function is called by the entries in the PLT to resolve the actual method that
2333 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2336 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2338 #ifdef MONO_ARCH_AOT_SUPPORTED
2339 guint8 *p, *target, *plt_entry;
2341 MonoAotModule *module = (MonoAotModule*)aot_module;
2345 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2347 p = &module->got_info [plt_info_offset];
2349 ji.type = decode_value (p, &p);
2351 mp = mono_mempool_new_size (512);
2352 res = decode_patch (module, mp, &ji, p, &p);
2353 // FIXME: Error handling (how ?)
2356 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2358 mono_mempool_destroy (mp);
2360 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2361 plt_entry = mono_aot_get_plt_entry (code);
2362 g_assert (plt_entry);
2363 mono_arch_patch_plt_entry (plt_entry, target);
2367 g_assert_not_reached ();
2375 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2376 * method in the module is loaded to avoid committing memory by writing to it.
2377 * LOCKING: Assumes the AOT lock is held.
2380 init_plt (MonoAotModule *info)
2382 #ifdef MONO_ARCH_AOT_SUPPORTED
2384 guint8 *buf = info->plt;
2385 #elif defined(__x86_64__) || defined(__arm__)
2390 if (info->plt_inited)
2393 tramp = mono_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2396 /* Initialize the first PLT entry */
2397 make_writable (info->plt, info->plt_end - info->plt);
2398 x86_jump_code (buf, tramp);
2399 #elif defined(__x86_64__) || defined(__arm__)
2401 * Initialize the PLT entries in the GOT to point to the default targets.
2404 /* The first entry points to the AOT trampoline */
2405 ((gpointer*)info->got)[info->plt_got_offset_base] = tramp;
2406 for (i = 1; i < info->plt_size; ++i)
2407 /* All the default entries point to the first entry */
2408 ((gpointer*)info->got)[info->plt_got_offset_base + i] = info->plt;
2410 g_assert_not_reached ();
2413 info->plt_inited = TRUE;
2418 * mono_aot_get_plt_entry:
2420 * Return the address of the PLT entry called by the code at CODE if exists.
2423 mono_aot_get_plt_entry (guint8 *code)
2425 MonoAotModule *aot_module = find_aot_module (code);
2426 #if defined(__arm__)
2433 #if defined(__i386__) || defined(__x86_64__)
2434 if (code [-5] == 0xe8) {
2435 guint32 disp = *(guint32*)(code - 4);
2436 guint8 *target = code + disp;
2438 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2441 #elif defined(__arm__)
2442 ins = ((guint32*)(gpointer)code) [-1];
2444 /* Should be a 'bl' */
2445 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2446 gint32 disp = ((gint32)ins) & 0xffffff;
2447 guint8 *target = code - 4 + 8 + (disp * 4);
2449 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2453 g_assert_not_reached ();
2460 * mono_aot_get_plt_info_offset:
2462 * Return the PLT info offset belonging to the plt entry called by CODE.
2465 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2467 guint8 *plt_entry = mono_aot_get_plt_entry (code);
2469 g_assert (plt_entry);
2471 /* The offset is embedded inside the code after the plt entry */
2472 #if defined(__i386__)
2473 return *(guint32*)(plt_entry + 5);
2474 #elif defined(__x86_64__)
2475 return *(guint32*)(plt_entry + 6);
2476 #elif defined(__arm__)
2477 /* The offset is stored as the 5th word of the plt entry */
2478 return ((guint32*)plt_entry) [4];
2480 g_assert_not_reached ();
2486 load_named_code (MonoAotModule *amodule, const char *name)
2490 int n_patches, got_index, pindex;
2496 symbol = g_strdup_printf ("%s", name);
2497 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2500 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
2502 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2506 symbol = g_strdup_printf ("%s_p", name);
2507 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2510 /* Nothing to patch */
2513 /* Similar to mono_aot_load_method () */
2515 n_patches = decode_value (p, &p);
2518 MonoJumpInfo *patches;
2521 mp = mono_mempool_new ();
2523 got_index = decode_value (p, &p);
2525 patches = load_patch_info (amodule, mp, n_patches, got_index, &got_slots, p, &p);
2528 for (pindex = 0; pindex < n_patches; ++pindex) {
2529 MonoJumpInfo *ji = &patches [pindex];
2533 * When this code is executed, the runtime may not yet initalized, so
2534 * resolve the patch info by hand.
2536 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2537 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2538 target = mono_get_lmf_addr;
2539 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2540 target = mono_thread_force_interruption_checkpoint;
2541 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2542 target = mono_exception_from_token;
2543 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2544 target = mono_get_throw_exception ();
2546 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2547 target = mono_amd64_throw_exception;
2550 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2551 target = mono_arm_throw_exception;
2553 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2554 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2555 target = (gpointer)mono_get_trampoline_func (tramp_type2);
2556 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
2557 /* atoll is needed because the the offset is unsigned */
2561 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
2562 g_assert (res == 1);
2563 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
2564 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
2565 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
2566 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
2567 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
2569 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
2570 g_assert_not_reached ();
2574 /* Hopefully the code doesn't have patches which need method or
2577 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2580 amodule->got [got_slots [pindex]] = target;
2585 mono_mempool_destroy (mp);
2592 * Return the piece of code identified by NAME from the mscorlib AOT file.
2595 mono_aot_get_named_code (const char *name)
2598 MonoAotModule *amodule;
2600 image = mono_defaults.corlib;
2603 amodule = image->aot_module;
2606 return load_named_code (amodule, name);
2610 * Return a specific trampoline from the AOT file.
2613 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2615 MonoAotModule *amodule;
2616 int index, tramp_size;
2617 guint8 *code, *tramp;
2618 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2620 /* Currently, we keep all trampolines in the mscorlib AOT image */
2621 image = mono_defaults.corlib;
2626 amodule = image->aot_module;
2629 if (amodule->trampoline_index == amodule->num_trampolines)
2630 g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_trampolines);
2632 index = amodule->trampoline_index ++;
2636 if (!generic_trampolines [tramp_type]) {
2639 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2640 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2644 tramp = generic_trampolines [tramp_type];
2647 amodule->got [amodule->trampoline_got_offset_base + (index *2)] = tramp;
2648 amodule->got [amodule->trampoline_got_offset_base + (index *2) + 1] = arg1;
2652 #elif defined(__arm__)
2656 g_assert_not_reached ();
2659 code = amodule->trampolines + (index * tramp_size);
2661 *code_len = tramp_size;
2667 mono_aot_get_unbox_trampoline (MonoMethod *method)
2669 guint32 method_index = mono_metadata_token_index (method->token) - 1;
2670 MonoAotModule *amodule;
2674 amodule = method->klass->image->aot_module;
2677 symbol = g_strdup_printf ("unbox_trampoline_%d", method_index);
2678 code = load_named_code (amodule, symbol);
2684 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2689 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
2690 code = load_named_code (mono_defaults.corlib->aot_module, symbol);
2699 mono_aot_init (void)
2704 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2710 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2716 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2722 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2728 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2734 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2740 mono_aot_get_plt_entry (guint8 *code)
2746 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2752 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2758 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2760 g_assert_not_reached ();
2766 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2768 g_assert_not_reached ();
2773 mono_aot_get_named_code (const char *name)
2775 g_assert_not_reached ();
2780 mono_aot_get_unbox_trampoline (MonoMethod *method)
2782 g_assert_not_reached ();
2787 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2789 g_assert_not_reached ();