2 * aot-runtime.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 /* Maps pointers into the method info to the methods themselves */
85 GHashTable *method_ref_to_method;
86 MonoAssemblyName *image_names;
88 MonoAssembly *assembly;
89 MonoImage **image_table;
90 guint32 image_table_len;
99 guint32 plt_got_offset_base;
100 guint32 *code_offsets;
102 guint32 *method_info_offsets;
104 guint32 *got_info_offsets;
106 guint32 *ex_info_offsets;
107 guint32 *method_order;
108 guint32 *method_order_end;
110 guint32 *class_info_offsets;
111 guint32 *methods_loaded;
112 guint16 *class_name_table;
113 guint32 *extra_method_table;
114 guint32 *extra_method_info_offsets;
115 guint8 *extra_method_info;
117 guint8 *specific_trampolines;
118 guint32 num_specific_trampolines, specific_trampoline_got_offset_base;
119 guint32 specific_trampoline_index, specific_trampoline_size;
121 guint8 *static_rgctx_trampolines;
122 guint32 num_static_rgctx_trampolines, static_rgctx_trampoline_got_offset_base;
123 guint32 static_rgctx_trampoline_index, static_rgctx_trampoline_size;
129 /* This structure is stored in the AOT file */
130 typedef struct MonoAotFileInfo
132 guint32 plt_got_offset_base;
135 guint32 num_specific_trampolines;
136 guint32 specific_trampoline_size;
137 guint32 specific_trampoline_got_offset_base;
138 guint32 num_static_rgctx_trampolines;
139 guint32 static_rgctx_trampoline_size;
140 guint32 static_rgctx_trampoline_got_offset_base;
144 static GHashTable *aot_modules;
145 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
146 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
147 static CRITICAL_SECTION aot_mutex;
150 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
151 * AOT modules registered by mono_aot_register_module ().
153 static GHashTable *static_aot_modules;
156 * Disabling this will make a copy of the loaded code and use the copy instead
157 * of the original. This will place the caller and the callee close to each
158 * other in memory, possibly improving cache behavior. Since the original
159 * code is in copy-on-write memory, this will not increase the memory usage
162 static gboolean use_loaded_code = TRUE;
165 * Whenever to AOT compile loaded assemblies on demand and store them in
166 * a cache under $HOME/.mono/aot-cache.
168 static gboolean use_aot_cache = FALSE;
171 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
172 * use_aot_cache is TRUE.
174 static gboolean spawn_compiler = TRUE;
177 static gint32 mono_last_aot_method = -1;
179 static gboolean make_unreadable = FALSE;
180 static guint32 name_table_accesses = 0;
182 /* Used to speed-up find_aot_module () */
183 static gsize aot_code_low_addr = (gssize)-1;
184 static gsize aot_code_high_addr = 0;
186 /* Used to communicate with mono_aot_register_globals () */
187 static guint32 globals_tls_id = -1;
190 init_plt (MonoAotModule *info);
193 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
194 guint32 got_index, guint32 **got_slots,
195 guint8 *buf, guint8 **endbuf);
197 /*****************************************************/
199 /*****************************************************/
202 load_image (MonoAotModule *module, int index)
204 MonoAssembly *assembly;
205 MonoImageOpenStatus status;
207 g_assert (index < module->image_table_len);
209 if (module->image_table [index])
210 return module->image_table [index];
211 if (module->out_of_date)
214 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
216 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);
217 module->out_of_date = TRUE;
221 if (strcmp (assembly->image->guid, module->image_guids [index])) {
222 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);
223 module->out_of_date = TRUE;
227 module->image_table [index] = assembly->image;
228 return assembly->image;
233 decode_value (guint8 *ptr, guint8 **rptr)
238 if ((b & 0x80) == 0){
241 } else if ((b & 0x40) == 0){
242 len = ((b & 0x3f) << 8 | ptr [1]);
244 } else if (b != 0xff) {
245 len = ((b & 0x1f) << 24) |
252 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
258 //printf ("DECODE: %d.\n", len);
263 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
266 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
268 static MonoGenericInst*
269 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
272 MonoType **type_argv;
273 MonoGenericInst *inst;
276 type_argc = decode_value (p, &p);
277 type_argv = g_new0 (MonoType*, type_argc);
279 for (i = 0; i < type_argc; ++i) {
280 MonoClass *pclass = decode_klass_ref (module, p, &p);
285 type_argv [i] = &pclass->byval_arg;
288 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
297 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
299 gboolean has_class_inst, has_method_inst;
302 has_class_inst = decode_value (p, &p);
303 if (has_class_inst) {
304 ctx->class_inst = decode_generic_inst (module, p, &p);
305 if (!ctx->class_inst)
308 has_method_inst = decode_value (p, &p);
309 if (has_method_inst) {
310 ctx->method_inst = decode_generic_inst (module, p, &p);
311 if (!ctx->method_inst)
320 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
323 MonoClass *klass, *eklass;
327 token = decode_value (p, &p);
332 if (mono_metadata_token_table (token) == 0) {
333 image = load_image (module, decode_value (p, &p));
336 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
337 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
338 if (token == MONO_TOKEN_TYPE_SPEC) {
339 MonoTypeEnum type = decode_value (p, &p);
341 if (type == MONO_TYPE_GENERICINST) {
343 MonoGenericContext ctx;
346 gclass = decode_klass_ref (module, p, &p);
347 g_assert (gclass->generic_container);
349 memset (&ctx, 0, sizeof (ctx));
350 ctx.class_inst = decode_generic_inst (module, p, &p);
353 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
354 klass = mono_class_from_mono_type (type);
355 mono_metadata_free_type (type);
356 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
358 MonoGenericContainer *container;
360 int num = decode_value (p, &p);
361 gboolean is_method = decode_value (p, &p);
364 MonoMethod *method_def;
365 g_assert (type == MONO_TYPE_MVAR);
366 method_def = decode_method_ref_2 (module, p, &p);
370 container = mono_method_get_generic_container (method_def);
372 MonoClass *class_def;
373 g_assert (type == MONO_TYPE_VAR);
374 class_def = decode_klass_ref (module, p, &p);
378 container = class_def->generic_container;
381 g_assert (container);
383 // FIXME: Memory management
384 t = g_new0 (MonoType, 1);
386 t->data.generic_param = mono_generic_container_get_param (container, num);
388 // FIXME: Maybe use types directly to avoid
389 // the overhead of creating MonoClass-es
390 klass = mono_class_from_mono_type (t);
394 g_assert_not_reached ();
397 image = load_image (module, decode_value (p, &p));
400 klass = mono_class_get (image, token);
402 } else if (token == MONO_TOKEN_TYPE_DEF) {
404 image = load_image (module, decode_value (p, &p));
407 rank = decode_value (p, &p);
408 eklass = decode_klass_ref (module, p, &p);
409 klass = mono_array_class_get (eklass, rank);
411 g_assert_not_reached ();
414 mono_class_init (klass);
420 static MonoClassField*
421 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
423 MonoClass *klass = decode_klass_ref (module, buf, &buf);
430 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
434 return mono_class_get_field (klass, token);
438 * can_method_ref_match_method:
440 * Determine if calling decode_method_ref_2 on P could return the same method as
441 * METHOD. This is an optimization to avoid calling decode_method_ref_2 () which
442 * would create MonoMethods which are not needed etc.
445 can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
448 guint32 image_index, value;
450 /* Keep this in sync with decode_method_ref () */
451 value = decode_value (p, &p);
452 image_index = value >> 24;
454 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
455 guint32 wrapper_type;
457 if (!method->wrapper_type)
460 wrapper_type = decode_value (p, &p);
462 if (method->wrapper_type != wrapper_type)
464 } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
465 if (method->wrapper_type)
475 * Decode a method reference, and return its image and token. This avoids loading
476 * metadata for the method if the caller does not need it. If the method has no token,
477 * then it is loaded from metadata and METHOD is set to the method instance.
480 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
482 guint32 image_index, value;
483 MonoImage *image = NULL;
488 if (no_aot_trampoline)
489 *no_aot_trampoline = FALSE;
491 value = decode_value (p, &p);
492 image_index = value >> 24;
494 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
495 if (no_aot_trampoline)
496 *no_aot_trampoline = TRUE;
497 value = decode_value (p, &p);
498 image_index = value >> 24;
501 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
502 guint32 wrapper_type;
504 wrapper_type = decode_value (p, &p);
507 image = mono_defaults.corlib;
509 switch (wrapper_type) {
510 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
511 MonoMethod *m = decode_method_ref_2 (module, p, &p);
515 mono_class_init (m->klass);
516 *method = mono_marshal_get_remoting_invoke_with_check (m);
519 case MONO_WRAPPER_PROXY_ISINST: {
520 MonoClass *klass = decode_klass_ref (module, p, &p);
523 *method = mono_marshal_get_proxy_cancast (klass);
526 case MONO_WRAPPER_LDFLD:
527 case MONO_WRAPPER_LDFLDA:
528 case MONO_WRAPPER_STFLD:
529 case MONO_WRAPPER_ISINST: {
530 MonoClass *klass = decode_klass_ref (module, p, &p);
533 if (wrapper_type == MONO_WRAPPER_LDFLD)
534 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
535 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
536 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
537 else if (wrapper_type == MONO_WRAPPER_STFLD)
538 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
539 else if (wrapper_type == MONO_WRAPPER_ISINST)
540 *method = mono_marshal_get_isinst (klass);
542 g_assert_not_reached ();
545 case MONO_WRAPPER_LDFLD_REMOTE:
546 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
548 case MONO_WRAPPER_STFLD_REMOTE:
549 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
551 case MONO_WRAPPER_ALLOC: {
552 int atype = decode_value (p, &p);
554 *method = mono_gc_get_managed_allocator_by_type (atype);
557 case MONO_WRAPPER_STELEMREF:
558 *method = mono_marshal_get_stelemref ();
560 case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
561 MonoMethod *m = decode_method_ref_2 (module, p, &p);
565 *method = mono_marshal_get_static_rgctx_invoke (m);
568 case MONO_WRAPPER_SYNCHRONIZED: {
569 MonoMethod *m = decode_method_ref_2 (module, p, &p);
573 *method = mono_marshal_get_synchronized_wrapper (m);
576 case MONO_WRAPPER_UNKNOWN: {
577 MonoMethodDesc *desc;
578 MonoMethod *orig_method;
579 int subtype = decode_value (p, &p);
581 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
582 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
583 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
584 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
586 g_assert_not_reached ();
587 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
588 g_assert (orig_method);
589 mono_method_desc_free (desc);
590 *method = mono_monitor_get_fast_path (orig_method);
594 g_assert_not_reached ();
596 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
597 /* Can't decode these */
598 g_assert_not_reached ();
599 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
600 image_index = decode_value (p, &p);
601 *token = decode_value (p, &p);
603 image = load_image (module, image_index);
606 } else if (image_index == MONO_AOT_METHODREF_GINST) {
608 MonoGenericContext ctx;
611 * These methods do not have a token which resolves them, so we
612 * resolve them immediately.
614 klass = decode_klass_ref (module, p, &p);
618 image_index = decode_value (p, &p);
619 *token = decode_value (p, &p);
621 image = load_image (module, image_index);
625 *method = mono_get_method_full (image, *token, NULL, NULL);
629 memset (&ctx, 0, sizeof (ctx));
631 if (FALSE && klass->generic_class) {
632 ctx.class_inst = klass->generic_class->context.class_inst;
633 ctx.method_inst = NULL;
635 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
638 memset (&ctx, 0, sizeof (ctx));
640 if (!decode_generic_context (module, &ctx, p, &p))
643 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
644 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
648 klass = decode_klass_ref (module, p, &p);
651 method_type = decode_value (p, &p);
653 switch (method_type) {
655 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
658 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
661 *method = mono_class_get_method_from_name (klass, "Get", -1);
664 *method = mono_class_get_method_from_name (klass, "Address", -1);
667 *method = mono_class_get_method_from_name (klass, "Set", -1);
670 g_assert_not_reached ();
673 g_assert (image_index < MONO_AOT_METHODREF_MIN);
674 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
676 image = load_image (module, image_index);
687 * decode_method_ref_2:
689 * Similar to decode_method_ref, but resolve and return the method itself.
692 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
696 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
702 return mono_get_method (image, token, NULL);
707 make_writable (guint8* addr, guint32 len)
709 #ifndef PLATFORM_WIN32
714 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
716 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
717 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
718 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
723 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
729 create_cache_structure (void)
735 home = g_get_home_dir ();
739 tmp = g_build_filename (home, ".mono", NULL);
740 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
741 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
742 #ifdef PLATFORM_WIN32
745 err = mkdir (tmp, 0777);
748 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
754 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
755 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
756 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
757 #ifdef PLATFORM_WIN32
760 err = mkdir (tmp, 0777);
763 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
772 * load_aot_module_from_cache:
774 * Experimental code to AOT compile loaded assemblies on demand.
777 * - Add environment variable MONO_AOT_CACHE_OPTIONS
778 * - Add options for controlling the cache size
779 * - Handle full cache by deleting old assemblies lru style
780 * - Add options for excluding assemblies during development
781 * - Maybe add a threshold after an assembly is AOT compiled
782 * - invoking a new mono process is a security risk
783 * - recompile the AOT module if one of its dependencies changes
786 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
788 char *fname, *cmd, *tmp2, *aot_options;
797 if (assembly->image->dynamic)
800 create_cache_structure ();
802 home = g_get_home_dir ();
804 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
805 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
809 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
810 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
813 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
815 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
817 aot_options = g_strdup_printf ("outfile=%s", fname);
819 if (spawn_compiler) {
820 /* FIXME: security */
821 /* FIXME: Has to pass the assembly loading path to the child process */
822 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
824 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
826 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
828 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
829 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
831 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
838 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
840 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
842 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
846 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
848 g_free (aot_options);
855 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
861 for (i = 0; globals [i]; i+= 2) {
862 if (strcmp (globals [i], name) == 0) {
863 *value = globals [i + 1];
868 mono_dl_symbol (module, name, value);
873 load_aot_module (MonoAssembly *assembly, gpointer user_data)
876 MonoAotModule *amodule;
878 gboolean usable = TRUE;
879 char *saved_guid = NULL;
880 char *aot_version = NULL;
881 char *runtime_version, *build_info;
882 char *opt_flags = NULL;
884 gboolean full_aot = FALSE;
885 MonoAotFileInfo *file_info = NULL;
888 if (mono_compile_aot)
891 if (assembly->image->aot_module)
893 * Already loaded. This can happen because the assembly loading code might invoke
894 * the assembly load hooks multiple times for the same assembly.
898 if (assembly->image->dynamic)
901 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
905 if (static_aot_modules)
906 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
912 /* Statically linked AOT module */
914 aot_name = g_strdup_printf ("%s", assembly->aname.name);
915 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
917 TlsSetValue (globals_tls_id, NULL);
920 sofile = load_aot_module_from_cache (assembly, &aot_name);
923 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
925 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
928 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
934 * If the image was compiled in no-dlsym mode, it contains no global symbols,
935 * instead it contains an ELF ctor function which is called by dlopen () which
936 * in turn calls mono_aot_register_globals () to register a table which contains
937 * the name and address of the globals.
939 globals = TlsGetValue (globals_tls_id);
940 TlsSetValue (globals_tls_id, NULL);
943 if (!sofile && !globals) {
945 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
952 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
953 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
954 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
955 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
957 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
958 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);
962 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
963 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
968 build_info = mono_get_runtime_build_info ();
969 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
970 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);
978 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
980 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
984 if (mono_aot_only && !full_aot) {
985 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
988 if (!mono_aot_only && full_aot) {
989 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
995 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
1000 mono_dl_close (sofile);
1001 assembly->image->aot_module = NULL;
1005 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
1006 g_assert (file_info);
1008 amodule = g_new0 (MonoAotModule, 1);
1009 amodule->aot_name = aot_name;
1010 amodule->assembly = assembly;
1011 amodule->plt_got_offset_base = file_info->plt_got_offset_base;
1012 amodule->num_specific_trampolines = file_info->num_specific_trampolines;
1013 amodule->specific_trampoline_got_offset_base = file_info->specific_trampoline_got_offset_base;
1014 amodule->specific_trampoline_size = file_info->specific_trampoline_size;
1015 amodule->got_size = file_info->got_size;
1016 amodule->plt_size = file_info->plt_size;
1017 amodule->num_static_rgctx_trampolines = file_info->num_static_rgctx_trampolines;
1018 amodule->static_rgctx_trampoline_got_offset_base = file_info->static_rgctx_trampoline_got_offset_base;
1019 amodule->static_rgctx_trampoline_size = file_info->static_rgctx_trampoline_size;
1020 amodule->got = file_info->got;
1021 amodule->got [0] = assembly->image;
1022 amodule->globals = globals;
1023 amodule->sofile = sofile;
1024 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1026 sscanf (opt_flags, "%d", &amodule->opts);
1028 /* Read image table */
1030 guint32 table_len, i;
1033 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
1036 table_len = *(guint32*)table;
1037 table += sizeof (guint32);
1038 amodule->image_table = g_new0 (MonoImage*, table_len);
1039 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1040 amodule->image_guids = g_new0 (char*, table_len);
1041 amodule->image_table_len = table_len;
1042 for (i = 0; i < table_len; ++i) {
1043 MonoAssemblyName *aname = &(amodule->image_names [i]);
1045 aname->name = g_strdup (table);
1046 table += strlen (table) + 1;
1047 amodule->image_guids [i] = g_strdup (table);
1048 table += strlen (table) + 1;
1050 aname->culture = g_strdup (table);
1051 table += strlen (table) + 1;
1052 memcpy (aname->public_key_token, table, strlen (table) + 1);
1053 table += strlen (table) + 1;
1055 table = ALIGN_PTR_TO (table, 8);
1056 aname->flags = *(guint32*)table;
1058 aname->major = *(guint32*)table;
1060 aname->minor = *(guint32*)table;
1062 aname->build = *(guint32*)table;
1064 aname->revision = *(guint32*)table;
1069 /* Read method and method_info tables */
1070 find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
1071 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
1072 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
1073 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
1074 find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
1075 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
1076 find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
1077 find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
1078 find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
1079 find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
1080 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
1081 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
1082 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
1083 find_symbol (sofile, globals, "extra_method_info", (gpointer *)&amodule->extra_method_info);
1084 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1085 find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
1086 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1087 find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&amodule->specific_trampolines);
1088 find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&amodule->static_rgctx_trampolines);
1089 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1091 amodule->mem_begin = amodule->code;
1093 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1094 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1096 if (make_unreadable) {
1097 #ifndef PLATFORM_WIN32
1100 int pages, err, len;
1102 addr = amodule->mem_begin;
1103 len = amodule->mem_end - amodule->mem_begin;
1105 /* Round down in both directions to avoid modifying data which is not ours */
1106 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
1107 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
1108 err = mprotect (page_start, pages * PAGESIZE, 0);
1109 g_assert (err == 0);
1115 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1116 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1118 g_hash_table_insert (aot_modules, assembly, amodule);
1121 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1123 assembly->image->aot_module = amodule;
1126 * Since we store methoddef and classdef tokens when referring to methods/classes in
1127 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1128 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1129 * non-lazily, since we can't handle out-of-date errors later.
1131 for (i = 0; i < amodule->image_table_len; ++i)
1132 load_image (amodule, i);
1134 if (amodule->out_of_date) {
1135 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);
1136 if (mono_aot_only) {
1137 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);
1142 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1146 * mono_aot_register_globals:
1148 * This is called by the ctor function in AOT images compiled with the
1149 * 'no-dlsym' option.
1152 mono_aot_register_globals (gpointer *globals)
1154 TlsSetValue (globals_tls_id, globals);
1158 * mono_aot_register_module:
1160 * This should be called by embedding code to register AOT modules statically linked
1161 * into the executable. AOT_INFO should be the value of the
1162 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1165 mono_aot_register_module (gpointer *aot_info)
1173 /* Determine the assembly name */
1174 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1177 /* This could be called before startup */
1181 if (!static_aot_modules)
1182 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1184 g_hash_table_insert (static_aot_modules, aname, globals);
1191 mono_aot_init (void)
1193 InitializeCriticalSection (&aot_mutex);
1194 aot_modules = g_hash_table_new (NULL, NULL);
1195 globals_tls_id = TlsAlloc ();
1197 mono_install_assembly_load_hook (load_aot_module, NULL);
1199 if (getenv ("MONO_LASTAOT"))
1200 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
1201 if (getenv ("MONO_AOT_CACHE"))
1202 use_aot_cache = TRUE;
1206 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1210 info->vtable_size = decode_value (buf, &buf);
1211 if (info->vtable_size == -1)
1214 flags = decode_value (buf, &buf);
1215 info->ghcimpl = (flags >> 0) & 0x1;
1216 info->has_finalize = (flags >> 1) & 0x1;
1217 info->has_cctor = (flags >> 2) & 0x1;
1218 info->has_nested_classes = (flags >> 3) & 0x1;
1219 info->blittable = (flags >> 4) & 0x1;
1220 info->has_references = (flags >> 5) & 0x1;
1221 info->has_static_refs = (flags >> 6) & 0x1;
1222 info->no_special_static_fields = (flags >> 7) & 0x1;
1224 if (info->has_cctor) {
1225 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1229 if (info->has_finalize) {
1230 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1231 if (!info->finalize_image)
1235 info->instance_size = decode_value (buf, &buf);
1236 info->class_size = decode_value (buf, &buf);
1237 info->packing_size = decode_value (buf, &buf);
1238 info->min_align = decode_value (buf, &buf);
1246 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1249 MonoClass *klass = vtable->klass;
1250 MonoAotModule *aot_module = klass->image->aot_module;
1252 MonoCachedClassInfo class_info;
1256 gboolean no_aot_trampoline;
1258 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1261 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1264 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1268 for (i = 0; i < slot; ++i)
1269 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1271 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1274 if (no_aot_trampoline)
1277 if (mono_metadata_token_index (token) == 0)
1280 return mono_aot_get_method_from_token (domain, image, token);
1284 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1286 MonoAotModule *aot_module = klass->image->aot_module;
1290 if (klass->rank || !aot_module)
1293 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1295 err = decode_cached_class_info (aot_module, res, p, &p);
1303 * mono_aot_get_class_from_name:
1305 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1306 * using a cache stored in the AOT file.
1307 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1309 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1313 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1315 MonoAotModule *aot_module = image->aot_module;
1316 guint16 *table, *entry;
1319 char full_name_buf [1024];
1321 const char *name2, *name_space2;
1323 guint32 cols [MONO_TYPEDEF_SIZE];
1324 GHashTable *nspace_table;
1326 if (!aot_module || !aot_module->class_name_table)
1333 /* First look in the cache */
1334 if (!aot_module->name_cache)
1335 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1336 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1338 *klass = g_hash_table_lookup (nspace_table, name);
1345 table_size = aot_module->class_name_table [0];
1346 table = aot_module->class_name_table + 1;
1348 if (name_space [0] == '\0')
1349 full_name = g_strdup_printf ("%s", name);
1351 if (strlen (name_space) + strlen (name) < 1000) {
1352 sprintf (full_name_buf, "%s.%s", name_space, name);
1353 full_name = full_name_buf;
1355 full_name = g_strdup_printf ("%s.%s", name_space, name);
1358 hash = g_str_hash (full_name) % table_size;
1359 if (full_name != full_name_buf)
1362 entry = &table [hash * 2];
1364 if (entry [0] != 0) {
1365 t = &image->tables [MONO_TABLE_TYPEDEF];
1368 guint32 index = entry [0];
1369 guint32 next = entry [1];
1370 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1372 name_table_accesses ++;
1374 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1376 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1377 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1379 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1381 *klass = mono_class_get (image, token);
1386 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1387 if (!nspace_table) {
1388 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1389 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1391 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1398 entry = &table [next * 2];
1411 * LOCKING: Acquires the domain lock.
1414 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
1415 MonoMethod *method, guint8* ex_info, guint8 *code)
1419 guint code_len, used_int_regs, flags;
1420 gboolean has_generic_jit_info, has_dwarf_unwind_info;
1421 guint8 *p, *unwind_info_block;
1422 MonoMethodHeader *header;
1423 int generic_info_size, unwind_info_len;
1425 header = mono_method_get_header (method);
1427 /* Load the method info from the AOT file */
1430 code_len = decode_value (p, &p);
1431 flags = decode_value (p, &p);
1432 has_generic_jit_info = (flags & 1) != 0;
1433 has_dwarf_unwind_info = (flags & 2) != 0;
1434 unwind_info_block = p;
1435 if (has_dwarf_unwind_info) {
1438 unwind_info_len = decode_value (p, &p);
1439 offset = unwind_info_block - (guint8*)aot_module->ex_info;
1440 g_assert (offset > 0 && offset < (1 << 30));
1441 used_int_regs = offset;
1442 p += unwind_info_len;
1444 used_int_regs = decode_value (p, &p);
1446 if (has_generic_jit_info)
1447 generic_info_size = sizeof (MonoGenericJitInfo);
1449 generic_info_size = 0;
1451 /* Exception table */
1452 if (header && header->num_clauses) {
1454 mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
1455 jinfo->num_clauses = header->num_clauses;
1457 for (i = 0; i < header->num_clauses; ++i) {
1458 MonoExceptionClause *ec = &header->clauses [i];
1459 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1461 ei->flags = ec->flags;
1462 ei->exvar_offset = decode_value (p, &p);
1464 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1465 ei->data.filter = code + decode_value (p, &p);
1467 ei->data.catch_class = ec->data.catch_class;
1469 ei->try_start = code + decode_value (p, &p);
1470 ei->try_end = code + decode_value (p, &p);
1471 ei->handler_start = code + decode_value (p, &p);
1475 jinfo = mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + generic_info_size);
1478 jinfo->code_size = code_len;
1479 jinfo->used_regs = used_int_regs;
1480 jinfo->method = method;
1481 jinfo->code_start = code;
1482 jinfo->domain_neutral = 0;
1483 jinfo->from_aot = 1;
1485 if (has_generic_jit_info) {
1486 MonoGenericJitInfo *gi;
1488 jinfo->has_generic_jit_info = 1;
1490 gi = mono_jit_info_get_generic_jit_info (jinfo);
1493 gi->has_this = decode_value (p, &p);
1494 gi->this_reg = decode_value (p, &p);
1495 gi->this_offset = decode_value (p, &p);
1497 /* This currently contains no data */
1498 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1500 jinfo->method = decode_method_ref_2 (aot_module, p, &p);
1503 /* Load debug info */
1504 buf_len = decode_value (p, &p);
1505 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1511 * mono_aot_get_unwind_info:
1513 * Return a pointer to the DWARF unwind info belonging to JI.
1516 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
1518 MonoAotModule *amodule = ji->method->klass->image->aot_module;
1522 g_assert (ji->from_aot);
1524 p = amodule->ex_info + ji->used_regs;
1525 *unwind_info_len = decode_value (p, &p);
1530 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1533 int pos, left, right, offset, offset1, offset2, last_offset, new_offset;
1534 int page_index, method_index, table_len, is_wrapper;
1536 MonoAotModule *amodule = image->aot_module;
1539 guint8 *code, *ex_info, *p;
1540 guint32 *table, *ptr;
1546 if (domain != mono_get_root_domain ())
1550 offset = (guint8*)addr - amodule->code;
1552 /* First search through the index */
1553 ptr = amodule->method_order;
1558 if (*ptr == 0xffffff)
1562 while (*ptr != 0xffffff) {
1563 guint32 method_index = ptr [0];
1564 new_offset = amodule->code_offsets [method_index];
1566 if (offset >= last_offset && offset < new_offset) {
1572 last_offset = new_offset;
1576 /* Skip rest of index */
1577 while (*ptr != 0xffffff)
1582 table_len = amodule->method_order_end - table;
1584 g_assert (table <= amodule->method_order_end);
1587 left = (page_index * 1024);
1588 right = left + 1024;
1590 if (right > table_len)
1593 offset1 = amodule->code_offsets [table [left]];
1594 g_assert (offset1 <= offset);
1596 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1599 //printf ("Not found in index: 0x%x\n", offset);
1604 /* Binary search inside the method_order table to find the method */
1606 pos = (left + right) / 2;
1608 g_assert (table + pos <= amodule->method_order_end);
1610 //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]]);
1612 offset1 = amodule->code_offsets [table [pos]];
1613 if (table + pos + 1 >= amodule->method_order_end)
1614 offset2 = amodule->code_end - amodule->code;
1616 offset2 = amodule->code_offsets [table [pos + 1]];
1618 if (offset < offset1)
1620 else if (offset >= offset2)
1626 method_index = table [pos];
1628 /* Might be a wrapper/extra method */
1629 if (amodule->extra_methods) {
1631 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
1638 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
1640 * This is hit for extra methods which are called directly, so they are
1641 * not in amodule->extra_methods.
1643 table_len = amodule->extra_method_info_offsets [0];
1644 table = amodule->extra_method_info_offsets + 1;
1651 pos = ((left + right) / 2);
1653 g_assert (pos < table_len);
1655 if (table [pos * 2] < method_index)
1657 else if (table [pos * 2] > method_index)
1663 p = amodule->extra_method_info + table [(pos * 2) + 1];
1664 is_wrapper = decode_value (p, &p);
1665 g_assert (!is_wrapper);
1666 method = decode_method_ref_2 (amodule, p, &p);
1669 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1670 method = mono_get_method (image, token, NULL);
1677 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1679 code = &amodule->code [amodule->code_offsets [method_index]];
1680 ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
1682 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code);
1684 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1685 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1687 /* Add it to the normal JitInfo tables */
1688 mono_jit_info_table_add (domain, jinfo);
1693 /* Keep it in sync with the version in aot-compiler.c */
1694 static inline gboolean
1695 is_shared_got_patch (MonoJumpInfo *patch_info)
1697 switch (patch_info->type) {
1698 case MONO_PATCH_INFO_VTABLE:
1699 case MONO_PATCH_INFO_CLASS:
1700 case MONO_PATCH_INFO_IID:
1701 case MONO_PATCH_INFO_ADJUSTED_IID:
1702 case MONO_PATCH_INFO_FIELD:
1703 case MONO_PATCH_INFO_SFLDA:
1704 case MONO_PATCH_INFO_DECLSEC:
1705 case MONO_PATCH_INFO_LDTOKEN:
1706 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1707 case MONO_PATCH_INFO_RVA:
1708 case MONO_PATCH_INFO_METHODCONST:
1709 case MONO_PATCH_INFO_IMAGE:
1717 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1725 case MONO_PATCH_INFO_METHOD:
1726 case MONO_PATCH_INFO_METHOD_JUMP:
1727 case MONO_PATCH_INFO_ICALL_ADDR:
1728 case MONO_PATCH_INFO_METHOD_RGCTX: {
1731 gboolean no_aot_trampoline;
1733 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
1737 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1738 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1739 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1740 ji->type = MONO_PATCH_INFO_ABS;
1744 ji->data.method = method;
1746 ji->data.method = mono_get_method (image, token, NULL);
1747 g_assert (ji->data.method);
1748 mono_class_init (ji->data.method->klass);
1751 ji->data.method = mono_get_method (image, token, NULL);
1752 g_assert (ji->data.method);
1753 mono_class_init (ji->data.method->klass);
1758 case MONO_PATCH_INFO_INTERNAL_METHOD:
1759 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1760 guint32 len = decode_value (p, &p);
1762 ji->data.name = (char*)p;
1766 case MONO_PATCH_INFO_METHODCONST:
1768 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1769 if (!ji->data.method)
1772 case MONO_PATCH_INFO_VTABLE:
1773 case MONO_PATCH_INFO_CLASS:
1774 case MONO_PATCH_INFO_IID:
1775 case MONO_PATCH_INFO_ADJUSTED_IID:
1777 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1778 if (!ji->data.klass)
1781 case MONO_PATCH_INFO_CLASS_INIT:
1782 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1783 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1784 if (!ji->data.klass)
1787 case MONO_PATCH_INFO_IMAGE:
1788 ji->data.image = load_image (aot_module, decode_value (p, &p));
1789 if (!ji->data.image)
1792 case MONO_PATCH_INFO_FIELD:
1793 case MONO_PATCH_INFO_SFLDA:
1795 ji->data.field = decode_field_info (aot_module, p, &p);
1796 if (!ji->data.field)
1799 case MONO_PATCH_INFO_SWITCH:
1800 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1801 ji->data.table->table_size = decode_value (p, &p);
1802 table = g_new (gpointer, ji->data.table->table_size);
1803 ji->data.table->table = (MonoBasicBlock**)table;
1804 for (i = 0; i < ji->data.table->table_size; i++)
1805 table [i] = (gpointer)(gssize)decode_value (p, &p);
1807 case MONO_PATCH_INFO_R4: {
1810 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1811 val = decode_value (p, &p);
1812 *(float*)ji->data.target = *(float*)&val;
1815 case MONO_PATCH_INFO_R8: {
1818 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1820 val [0] = decode_value (p, &p);
1821 val [1] = decode_value (p, &p);
1822 *(double*)ji->data.target = *(double*)val;
1825 case MONO_PATCH_INFO_LDSTR:
1826 image = load_image (aot_module, decode_value (p, &p));
1829 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1831 case MONO_PATCH_INFO_RVA:
1832 case MONO_PATCH_INFO_DECLSEC:
1833 case MONO_PATCH_INFO_LDTOKEN:
1834 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1836 image = load_image (aot_module, decode_value (p, &p));
1839 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1841 ji->data.token->has_context = decode_value (p, &p);
1842 if (ji->data.token->has_context) {
1843 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
1848 case MONO_PATCH_INFO_EXC_NAME:
1849 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1850 if (!ji->data.klass)
1852 ji->data.name = ji->data.klass->name;
1854 case MONO_PATCH_INFO_METHOD_REL:
1855 ji->data.offset = decode_value (p, &p);
1857 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1858 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1859 case MONO_PATCH_INFO_MONITOR_ENTER:
1860 case MONO_PATCH_INFO_MONITOR_EXIT:
1862 case MONO_PATCH_INFO_RGCTX_FETCH: {
1864 MonoJumpInfoRgctxEntry *entry;
1866 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1867 entry->method = decode_method_ref_2 (aot_module, p, &p);
1868 entry->in_mrgctx = decode_value (p, &p);
1869 entry->info_type = decode_value (p, &p);
1870 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1871 entry->data->type = decode_value (p, &p);
1873 res = decode_patch (aot_module, mp, entry->data, p, &p);
1876 ji->data.rgctx_entry = entry;
1880 g_warning ("unhandled type %d", ji->type);
1881 g_assert_not_reached ();
1895 * Decode a reference to a GOT entry. GOT_OFFSET is set to the index of the got
1896 * entry. If that got entry is not already filled out, then JI is filled out with
1897 * the information required to resolve the value of the GOT entry.
1898 * FIXME: Clean up this confusing API.
1901 decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1907 if (is_shared_got_patch (ji)) {
1908 *got_offset = decode_value (p, &p);
1910 if (aot_module->got [*got_offset]) {
1911 /* Already loaded */
1912 //printf ("HIT!\n");
1914 shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1916 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1921 res = decode_patch (aot_module, mp, ji, p, &p);
1930 static MonoJumpInfo*
1931 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1932 guint32 got_index, guint32 **got_slots,
1933 guint8 *buf, guint8 **endbuf)
1935 MonoJumpInfo *patches;
1936 MonoJumpInfo *patch_info = NULL;
1938 guint32 last_offset;
1943 /* First load the type + offset table */
1945 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1947 for (pindex = 0; pindex < n_patches; ++pindex) {
1948 MonoJumpInfo *ji = &patches [pindex];
1953 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1954 ji->next = patch_info;
1958 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1959 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1961 /* Then load the other data */
1962 for (pindex = 0; pindex < n_patches; ++pindex) {
1963 MonoJumpInfo *ji = &patches [pindex];
1965 if (!decode_got_entry (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1968 if ((*got_slots) [pindex] == 0xffffffff)
1969 (*got_slots) [pindex] = got_index ++;
1976 g_free (*got_slots);
1983 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1986 * Jump addresses cannot be patched by the trampoline code since it
1987 * does not have access to the caller's address. Instead, we collect
1988 * the addresses of the GOT slots pointing to a method, and patch
1989 * them after the method has been compiled.
1991 MonoJitDomainInfo *info = domain_jit_info (domain);
1994 mono_domain_lock (domain);
1995 if (!info->jump_target_got_slot_hash)
1996 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1997 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1998 list = g_slist_prepend (list, got_slot);
1999 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
2000 mono_domain_unlock (domain);
2006 * Load the method identified by METHOD_INDEX from the AOT image. Return a
2007 * pointer to the native code of the method, or NULL if not found.
2008 * METHOD might not be set if the caller only has the image/token info.
2011 load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
2014 gboolean from_plt = method == NULL;
2016 int i, pindex, got_index = 0, n_patches, used_strings;
2017 gboolean keep_patches = TRUE;
2018 guint8 *p, *ex_info;
2019 MonoJitInfo *jinfo = NULL;
2020 guint8 *code, *info;
2022 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2025 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
2026 /* Non shared AOT code can't be used in other appdomains */
2029 if (aot_module->out_of_date)
2032 if (aot_module->code_offsets [method_index] == 0xffffffff) {
2033 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2037 method = mono_get_method (image, token, NULL);
2038 full_name = mono_method_full_name (method, TRUE);
2039 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2045 code = &aot_module->code [aot_module->code_offsets [method_index]];
2046 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
2049 if (!aot_module->methods_loaded)
2050 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
2053 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2056 if (mono_last_aot_method != -1) {
2057 if (mono_jit_stats.methods_aot > mono_last_aot_method)
2060 if (method && mono_jit_stats.methods_aot == mono_last_aot_method)
2061 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
2067 klass = method->klass;
2068 decode_klass_ref (aot_module, p, &p);
2070 klass = decode_klass_ref (aot_module, p, &p);
2073 if (!use_loaded_code) {
2077 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (token) - 1]];
2078 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2081 code2 = mono_domain_code_reserve (domain, jinfo->code_size);
2082 memcpy (code2, code, jinfo->code_size);
2083 mono_arch_flush_icache (code2, jinfo->code_size);
2087 if (aot_module->opts & MONO_OPT_SHARED)
2088 used_strings = decode_value (p, &p);
2092 for (i = 0; i < used_strings; i++) {
2093 guint token = decode_value (p, &p);
2094 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2097 if (aot_module->opts & MONO_OPT_SHARED)
2098 keep_patches = FALSE;
2100 n_patches = decode_value (p, &p);
2102 keep_patches = FALSE;
2105 MonoJumpInfo *patches;
2111 mp = mono_mempool_new ();
2113 got_index = decode_value (p, &p);
2115 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
2116 if (patches == NULL)
2119 for (pindex = 0; pindex < n_patches; ++pindex) {
2120 MonoJumpInfo *ji = &patches [pindex];
2122 if (!aot_module->got [got_slots [pindex]]) {
2123 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2124 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2125 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
2127 ji->type = MONO_PATCH_INFO_NONE;
2133 mono_mempool_destroy (mp);
2136 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2140 method = mono_get_method (image, token, NULL);
2142 full_name = mono_method_full_name (method, TRUE);
2145 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
2146 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2149 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);
2155 mono_jit_stats.methods_aot++;
2157 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2159 init_plt (aot_module);
2161 if (method && method->wrapper_type)
2162 g_hash_table_insert (aot_module->method_to_code, method, code);
2166 if (from_plt && klass && !klass->generic_container)
2167 mono_runtime_class_init (mono_class_vtable (domain, klass));
2172 /* FIXME: The space in domain->mp is wasted */
2173 if (aot_module->opts & MONO_OPT_SHARED)
2174 /* No need to cache patches */
2175 mono_mempool_destroy (mp);
2184 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
2186 guint32 table_size, entry_size, hash;
2187 guint32 *table, *entry;
2195 table_size = amodule->extra_method_table [0];
2196 table = amodule->extra_method_table + 1;
2199 if (method->wrapper_type) {
2200 name = mono_aot_wrapper_name (method);
2203 hash = mono_aot_method_hash (method) % table_size;
2205 entry = &table [hash * entry_size];
2212 guint32 key = entry [0];
2213 guint32 value = entry [1];
2214 guint32 next = entry [entry_size - 1];
2217 int is_wrapper_name;
2219 p = amodule->extra_method_info + key;
2220 is_wrapper_name = decode_value (p, &p);
2221 if (is_wrapper_name) {
2222 int wrapper_type = decode_value (p, &p);
2223 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2227 } else if (can_method_ref_match_method (amodule, p, method)) {
2230 if (!amodule->method_ref_to_method)
2231 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2232 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2236 m = decode_method_ref_2 (amodule, p, &p);
2239 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2245 printf ("%d %s %s\n", num_checks, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
2252 /* Special case: wrappers of shared generic methods */
2253 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2254 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2255 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2256 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2258 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2266 entry = &table [next * entry_size];
2276 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2278 g_ptr_array_add ((GPtrArray*)user_data, value);
2282 * find_extra_method:
2284 * Try finding METHOD in the extra_method table in all AOT images.
2285 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2286 * module where the method was found.
2289 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2295 /* Try the method's module first */
2296 *out_amodule = method->klass->image->aot_module;
2297 index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
2298 if (index != 0xffffff)
2302 * Try all other modules.
2303 * This is needed because generic instances klass->image points to the image
2304 * containing the generic definition, but the native code is generated to the
2305 * AOT image which contains the reference.
2308 /* Make a copy to avoid doing the search inside the aot lock */
2309 modules = g_ptr_array_new ();
2311 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2315 for (i = 0; i < modules->len; ++i) {
2316 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2318 if (amodule != method->klass->image->aot_module)
2319 index = find_extra_method_in_amodule (amodule, method);
2320 if (index != 0xffffff) {
2321 *out_amodule = amodule;
2326 g_ptr_array_free (modules, TRUE);
2332 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2334 MonoClass *klass = method->klass;
2335 guint32 method_index;
2336 MonoAotModule *amodule = klass->image->aot_module;
2342 if (amodule->out_of_date)
2345 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2346 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2347 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2348 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2351 g_assert (klass->inited);
2353 /* Find method index */
2354 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2355 method = mono_method_get_declaring_generic_method (method);
2356 method_index = mono_metadata_token_index (method->token) - 1;
2357 } else if (method->is_inflated || !method->token) {
2358 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2360 code = g_hash_table_lookup (amodule->method_to_code, method);
2365 method_index = find_extra_method (method, &amodule);
2366 if (method_index == 0xffffff) {
2367 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2370 full_name = mono_method_full_name (method, TRUE);
2371 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2377 if (method_index == 0xffffff)
2380 /* Needed by find_jit_info */
2382 if (!amodule->extra_methods)
2383 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2384 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2388 method_index = mono_metadata_token_index (method->token) - 1;
2391 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2395 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2399 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2401 MonoAotModule *aot_module = image->aot_module;
2407 method_index = mono_metadata_token_index (token) - 1;
2409 return load_method (domain, aot_module, image, NULL, token, method_index);
2415 } IsGotEntryUserData;
2418 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2420 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2421 MonoAotModule *aot_module = (MonoAotModule*)value;
2423 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
2428 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2430 IsGotEntryUserData user_data;
2435 user_data.addr = addr;
2436 user_data.res = FALSE;
2438 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2441 return user_data.res;
2446 MonoAotModule *module;
2447 } FindAotModuleUserData;
2450 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2452 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2453 MonoAotModule *aot_module = (MonoAotModule*)value;
2455 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2456 data->module = aot_module;
2459 static inline MonoAotModule*
2460 find_aot_module (guint8 *code)
2462 FindAotModuleUserData user_data;
2467 /* Reading these need no locking */
2468 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2471 user_data.addr = code;
2472 user_data.module = NULL;
2475 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2478 return user_data.module;
2482 * mono_aot_plt_resolve:
2484 * This function is called by the entries in the PLT to resolve the actual method that
2485 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2488 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2490 #ifdef MONO_ARCH_AOT_SUPPORTED
2491 guint8 *p, *target, *plt_entry;
2493 MonoAotModule *module = (MonoAotModule*)aot_module;
2497 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2499 p = &module->got_info [plt_info_offset];
2501 ji.type = decode_value (p, &p);
2503 mp = mono_mempool_new_size (512);
2504 res = decode_patch (module, mp, &ji, p, &p);
2505 // FIXME: Error handling (how ?)
2508 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2510 mono_mempool_destroy (mp);
2512 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2513 plt_entry = mono_aot_get_plt_entry (code);
2514 g_assert (plt_entry);
2515 mono_arch_patch_plt_entry (plt_entry, target);
2519 g_assert_not_reached ();
2527 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2528 * method in the module is loaded to avoid committing memory by writing to it.
2529 * LOCKING: Assumes the AOT lock is held.
2532 init_plt (MonoAotModule *info)
2534 #ifdef MONO_ARCH_AOT_SUPPORTED
2536 guint8 *buf = info->plt;
2537 #elif defined(__x86_64__) || defined(__arm__)
2542 if (info->plt_inited)
2545 tramp = mono_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2548 /* Initialize the first PLT entry */
2549 make_writable (info->plt, info->plt_end - info->plt);
2550 x86_jump_code (buf, tramp);
2551 #elif defined(__x86_64__) || defined(__arm__)
2553 * Initialize the PLT entries in the GOT to point to the default targets.
2556 /* The first entry points to the AOT trampoline */
2557 ((gpointer*)info->got)[info->plt_got_offset_base] = tramp;
2558 for (i = 1; i < info->plt_size; ++i)
2559 /* All the default entries point to the first entry */
2560 ((gpointer*)info->got)[info->plt_got_offset_base + i] = info->plt;
2562 g_assert_not_reached ();
2565 info->plt_inited = TRUE;
2570 * mono_aot_get_plt_entry:
2572 * Return the address of the PLT entry called by the code at CODE if exists.
2575 mono_aot_get_plt_entry (guint8 *code)
2577 MonoAotModule *aot_module = find_aot_module (code);
2578 #if defined(__arm__)
2585 #if defined(__i386__) || defined(__x86_64__)
2586 if (code [-5] == 0xe8) {
2587 guint32 disp = *(guint32*)(code - 4);
2588 guint8 *target = code + disp;
2590 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2593 #elif defined(__arm__)
2594 ins = ((guint32*)(gpointer)code) [-1];
2596 /* Should be a 'bl' */
2597 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2598 gint32 disp = ((gint32)ins) & 0xffffff;
2599 guint8 *target = code - 4 + 8 + (disp * 4);
2601 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2605 g_assert_not_reached ();
2612 * mono_aot_get_plt_info_offset:
2614 * Return the PLT info offset belonging to the plt entry called by CODE.
2617 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2619 guint8 *plt_entry = mono_aot_get_plt_entry (code);
2621 g_assert (plt_entry);
2623 /* The offset is embedded inside the code after the plt entry */
2624 #if defined(__i386__)
2625 return *(guint32*)(plt_entry + 5);
2626 #elif defined(__x86_64__)
2627 return *(guint32*)(plt_entry + 6);
2628 #elif defined(__arm__)
2629 /* The offset is stored as the 5th word of the plt entry */
2630 return ((guint32*)plt_entry) [4];
2632 g_assert_not_reached ();
2638 load_named_code (MonoAotModule *amodule, const char *name)
2642 int n_patches, got_index, pindex;
2648 symbol = g_strdup_printf ("%s", name);
2649 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2652 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
2654 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2658 symbol = g_strdup_printf ("%s_p", name);
2659 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2662 /* Nothing to patch */
2665 /* Similar to mono_aot_load_method () */
2667 n_patches = decode_value (p, &p);
2670 MonoJumpInfo *patches;
2673 mp = mono_mempool_new ();
2675 got_index = decode_value (p, &p);
2677 patches = load_patch_info (amodule, mp, n_patches, got_index, &got_slots, p, &p);
2680 for (pindex = 0; pindex < n_patches; ++pindex) {
2681 MonoJumpInfo *ji = &patches [pindex];
2684 if (amodule->got [got_slots [pindex]])
2688 * When this code is executed, the runtime may not yet initalized, so
2689 * resolve the patch info by hand.
2691 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2692 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2693 target = mono_get_lmf_addr;
2694 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2695 target = mono_thread_force_interruption_checkpoint;
2696 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2697 target = mono_exception_from_token;
2698 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2699 target = mono_get_throw_exception ();
2701 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2702 target = mono_amd64_throw_exception;
2705 } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
2706 target = mono_amd64_get_original_ip;
2709 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2710 target = mono_arm_throw_exception;
2711 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
2712 target = mono_arm_throw_exception_by_token;
2714 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2715 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2716 target = (gpointer)mono_get_trampoline_func (tramp_type2);
2717 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
2718 /* atoll is needed because the the offset is unsigned */
2722 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
2723 g_assert (res == 1);
2724 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
2725 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
2726 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
2727 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
2728 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
2729 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
2730 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
2731 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
2732 target = mono_thread_get_and_clear_pending_exception;
2734 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
2735 g_assert_not_reached ();
2739 /* Hopefully the code doesn't have patches which need method or
2742 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2746 amodule->got [got_slots [pindex]] = target;
2751 mono_mempool_destroy (mp);
2758 * Return the piece of code identified by NAME from the mscorlib AOT file.
2761 mono_aot_get_named_code (const char *name)
2764 MonoAotModule *amodule;
2766 image = mono_defaults.corlib;
2769 amodule = image->aot_module;
2772 return load_named_code (amodule, name);
2776 * Return a specific trampoline from the AOT file.
2779 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2781 MonoAotModule *amodule;
2782 int index, tramp_size;
2783 guint8 *code, *tramp;
2784 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2786 /* Currently, we keep all trampolines in the mscorlib AOT image */
2787 image = mono_defaults.corlib;
2792 amodule = image->aot_module;
2795 if (amodule->specific_trampoline_index == amodule->num_specific_trampolines)
2796 g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_specific_trampolines);
2798 index = amodule->specific_trampoline_index ++;
2802 if (!generic_trampolines [tramp_type]) {
2805 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2806 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2810 tramp = generic_trampolines [tramp_type];
2813 amodule->got [amodule->specific_trampoline_got_offset_base + (index *2)] = tramp;
2814 amodule->got [amodule->specific_trampoline_got_offset_base + (index *2) + 1] = arg1;
2816 tramp_size = amodule->specific_trampoline_size;
2818 code = amodule->specific_trampolines + (index * tramp_size);
2820 *code_len = tramp_size;
2826 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
2828 MonoAotModule *amodule;
2829 int index, tramp_size;
2833 /* Currently, we keep all trampolines in the mscorlib AOT image */
2834 image = mono_defaults.corlib;
2839 amodule = image->aot_module;
2842 if (amodule->static_rgctx_trampoline_index == amodule->num_static_rgctx_trampolines)
2843 g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_static_rgctx_trampolines);
2845 index = amodule->static_rgctx_trampoline_index ++;
2849 amodule->got [amodule->static_rgctx_trampoline_got_offset_base + (index *2)] = ctx;
2850 amodule->got [amodule->static_rgctx_trampoline_got_offset_base + (index *2) + 1] = addr;
2852 tramp_size = amodule->static_rgctx_trampoline_size;
2854 code = amodule->static_rgctx_trampolines + (index * tramp_size);
2860 mono_aot_get_unbox_trampoline (MonoMethod *method)
2862 guint32 method_index = mono_metadata_token_index (method->token) - 1;
2863 MonoAotModule *amodule;
2867 if (method->is_inflated) {
2868 guint32 index = find_extra_method (method, &amodule);
2870 g_assert (index != 0xffffff);
2872 symbol = g_strdup_printf ("ut_e_%d", index);
2874 amodule = method->klass->image->aot_module;
2877 symbol = g_strdup_printf ("ut_%d", method_index);
2879 code = load_named_code (amodule, symbol);
2885 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2890 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
2891 code = load_named_code (mono_defaults.corlib->aot_module, symbol);
2900 mono_aot_init (void)
2905 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2911 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2917 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2923 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2929 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2935 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2941 mono_aot_get_plt_entry (guint8 *code)
2947 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2953 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2959 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2961 g_assert_not_reached ();
2967 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2969 g_assert_not_reached ();
2974 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
2976 g_assert_not_reached ();
2981 mono_aot_get_named_code (const char *name)
2983 g_assert_not_reached ();
2988 mono_aot_get_unbox_trampoline (MonoMethod *method)
2990 g_assert_not_reached ();
2995 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2997 g_assert_not_reached ();