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
34 #ifdef HAVE_SYS_WAIT_H
35 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
38 #ifdef HAVE_DL_ITERATE_PHDR
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-mmap.h>
56 #include "mono/utils/mono-compiler.h"
57 #include <mono/utils/mono-counters.h>
65 #define SHARED_EXT ".dll"
66 #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
67 #define SHARED_EXT ".dylib"
69 #define SHARED_EXT ".so"
72 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
73 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
75 typedef struct MonoAotModule {
77 /* Pointer to the Global Offset Table */
79 GHashTable *name_cache;
80 GHashTable *extra_methods;
81 /* Maps methods to their code */
82 GHashTable *method_to_code;
83 /* Maps pointers into the method info to the methods themselves */
84 GHashTable *method_ref_to_method;
85 MonoAssemblyName *image_names;
87 MonoAssembly *assembly;
88 MonoImage **image_table;
89 guint32 image_table_len;
100 /* This contains <offset, index> pairs sorted by offset */
101 /* This is needed because LLVM emitted methods can be in any order */
102 gint32 *sorted_code_offsets;
103 guint32 *method_info_offsets;
104 guint32 *got_info_offsets;
105 guint32 *ex_info_offsets;
106 guint32 *class_info_offsets;
107 guint32 *methods_loaded;
108 guint16 *class_name_table;
109 guint32 *extra_method_table;
110 guint32 *extra_method_info_offsets;
113 /* Points to the GNU .eh_frame_hdr section, if it exists */
114 guint8 *eh_frame_hdr;
116 /* Points to the .ARM.exidx section, if it exists */
118 guint32 arm_exidx_size;
120 /* Points to the trampolines */
121 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
122 /* The first unused trampoline of each kind */
123 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
125 MonoAotFileInfo info;
131 static GHashTable *aot_modules;
132 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
133 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
134 static CRITICAL_SECTION aot_mutex;
137 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
138 * AOT modules registered by mono_aot_register_module ().
140 static GHashTable *static_aot_modules;
143 * Maps MonoJitInfo* to the aot module they belong to, this can be different
144 * from ji->method->klass->image's aot module for generic instances.
146 static GHashTable *ji_to_amodule;
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;
171 init_plt (MonoAotModule *info);
173 /*****************************************************/
175 /*****************************************************/
178 load_image (MonoAotModule *module, int index)
180 MonoAssembly *assembly;
181 MonoImageOpenStatus status;
183 g_assert (index < module->image_table_len);
185 if (module->image_table [index])
186 return module->image_table [index];
187 if (module->out_of_date)
190 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
192 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);
193 module->out_of_date = TRUE;
197 if (strcmp (assembly->image->guid, module->image_guids [index])) {
198 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);
199 module->out_of_date = TRUE;
203 module->image_table [index] = assembly->image;
204 return assembly->image;
209 decode_value (guint8 *ptr, guint8 **rptr)
214 if ((b & 0x80) == 0){
217 } else if ((b & 0x40) == 0){
218 len = ((b & 0x3f) << 8 | ptr [1]);
220 } else if (b != 0xff) {
221 len = ((b & 0x1f) << 24) |
228 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
234 //printf ("DECODE: %d.\n", len);
239 * mono_aot_get_method:
241 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
245 mono_aot_get_offset (guint32 *table, int index)
247 int i, group, ngroups, index_entry_size;
248 int start_offset, offset, noffsets, group_size;
249 guint8 *data_start, *p;
250 guint32 *index32 = NULL;
251 guint16 *index16 = NULL;
253 noffsets = table [0];
254 group_size = table [1];
256 index_entry_size = table [3];
257 group = index / group_size;
259 if (index_entry_size == 2) {
260 index16 = (guint16*)&table [4];
261 data_start = (guint8*)&index16 [ngroups];
262 p = data_start + index16 [group];
264 index32 = (guint32*)&table [4];
265 data_start = (guint8*)&index32 [ngroups];
266 p = data_start + index32 [group];
269 /* offset will contain the value of offsets [group * group_size] */
270 offset = start_offset = decode_value (p, &p);
271 for (i = group * group_size + 1; i <= index; ++i) {
272 offset += decode_value (p, &p);
275 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
281 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
284 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
286 static MonoGenericInst*
287 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
290 MonoType **type_argv;
291 MonoGenericInst *inst;
294 type_argc = decode_value (p, &p);
295 type_argv = g_new0 (MonoType*, type_argc);
297 for (i = 0; i < type_argc; ++i) {
298 MonoClass *pclass = decode_klass_ref (module, p, &p);
303 type_argv [i] = &pclass->byval_arg;
306 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
315 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
317 gboolean has_class_inst, has_method_inst;
320 has_class_inst = decode_value (p, &p);
321 if (has_class_inst) {
322 ctx->class_inst = decode_generic_inst (module, p, &p);
323 if (!ctx->class_inst)
326 has_method_inst = decode_value (p, &p);
327 if (has_method_inst) {
328 ctx->method_inst = decode_generic_inst (module, p, &p);
329 if (!ctx->method_inst)
338 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
341 MonoClass *klass, *eklass;
345 token = decode_value (p, &p);
350 if (mono_metadata_token_table (token) == 0) {
351 image = load_image (module, decode_value (p, &p));
354 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
355 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
356 if (token == MONO_TOKEN_TYPE_SPEC) {
357 MonoTypeEnum type = decode_value (p, &p);
359 if (type == MONO_TYPE_GENERICINST) {
361 MonoGenericContext ctx;
364 gclass = decode_klass_ref (module, p, &p);
367 g_assert (gclass->generic_container);
369 memset (&ctx, 0, sizeof (ctx));
370 ctx.class_inst = decode_generic_inst (module, p, &p);
373 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
374 klass = mono_class_from_mono_type (type);
375 mono_metadata_free_type (type);
376 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
378 MonoGenericContainer *container;
380 int num = decode_value (p, &p);
381 gboolean is_method = decode_value (p, &p);
384 MonoMethod *method_def;
385 g_assert (type == MONO_TYPE_MVAR);
386 method_def = decode_method_ref_2 (module, p, &p);
390 container = mono_method_get_generic_container (method_def);
392 MonoClass *class_def;
393 g_assert (type == MONO_TYPE_VAR);
394 class_def = decode_klass_ref (module, p, &p);
398 container = class_def->generic_container;
401 g_assert (container);
403 // FIXME: Memory management
404 t = g_new0 (MonoType, 1);
406 t->data.generic_param = mono_generic_container_get_param (container, num);
408 // FIXME: Maybe use types directly to avoid
409 // the overhead of creating MonoClass-es
410 klass = mono_class_from_mono_type (t);
414 g_assert_not_reached ();
417 image = load_image (module, decode_value (p, &p));
420 klass = mono_class_get (image, token);
422 } else if (token == MONO_TOKEN_TYPE_DEF) {
424 image = load_image (module, decode_value (p, &p));
427 rank = decode_value (p, &p);
428 eklass = decode_klass_ref (module, p, &p);
429 klass = mono_array_class_get (eklass, rank);
431 g_assert_not_reached ();
439 static MonoClassField*
440 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
442 MonoClass *klass = decode_klass_ref (module, buf, &buf);
449 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
453 return mono_class_get_field (klass, token);
457 * can_method_ref_match_method:
459 * Determine if calling decode_method_ref_2 on P could return the same method as
460 * METHOD. This is an optimization to avoid calling decode_method_ref_2 () which
461 * would create MonoMethods which are not needed etc.
464 can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
467 guint32 image_index, value;
469 /* Keep this in sync with decode_method_ref () */
470 value = decode_value (p, &p);
471 image_index = value >> 24;
473 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
474 guint32 wrapper_type;
476 if (!method->wrapper_type)
479 wrapper_type = decode_value (p, &p);
481 if (method->wrapper_type != wrapper_type)
483 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
485 } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
486 if (method->wrapper_type)
496 * Decode a method reference, and return its image and token. This avoids loading
497 * metadata for the method if the caller does not need it. If the method has no token,
498 * then it is loaded from metadata and METHOD is set to the method instance.
501 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
503 guint32 image_index, value;
504 MonoImage *image = NULL;
509 if (no_aot_trampoline)
510 *no_aot_trampoline = FALSE;
512 value = decode_value (p, &p);
513 image_index = value >> 24;
515 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
516 if (no_aot_trampoline)
517 *no_aot_trampoline = TRUE;
518 value = decode_value (p, &p);
519 image_index = value >> 24;
522 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
523 guint32 wrapper_type;
525 wrapper_type = decode_value (p, &p);
528 image = mono_defaults.corlib;
530 switch (wrapper_type) {
531 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
532 MonoMethod *m = decode_method_ref_2 (module, p, &p);
536 mono_class_init (m->klass);
537 *method = mono_marshal_get_remoting_invoke_with_check (m);
540 case MONO_WRAPPER_PROXY_ISINST: {
541 MonoClass *klass = decode_klass_ref (module, p, &p);
544 *method = mono_marshal_get_proxy_cancast (klass);
547 case MONO_WRAPPER_LDFLD:
548 case MONO_WRAPPER_LDFLDA:
549 case MONO_WRAPPER_STFLD:
550 case MONO_WRAPPER_ISINST: {
551 MonoClass *klass = decode_klass_ref (module, p, &p);
554 if (wrapper_type == MONO_WRAPPER_LDFLD)
555 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
556 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
557 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
558 else if (wrapper_type == MONO_WRAPPER_STFLD)
559 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
560 else if (wrapper_type == MONO_WRAPPER_ISINST)
561 *method = mono_marshal_get_isinst (klass);
563 g_assert_not_reached ();
566 case MONO_WRAPPER_LDFLD_REMOTE:
567 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
569 case MONO_WRAPPER_STFLD_REMOTE:
570 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
572 case MONO_WRAPPER_ALLOC: {
573 int atype = decode_value (p, &p);
575 *method = mono_gc_get_managed_allocator_by_type (atype);
578 case MONO_WRAPPER_STELEMREF:
579 *method = mono_marshal_get_stelemref ();
581 case MONO_WRAPPER_SYNCHRONIZED: {
582 MonoMethod *m = decode_method_ref_2 (module, p, &p);
586 *method = mono_marshal_get_synchronized_wrapper (m);
589 case MONO_WRAPPER_UNKNOWN: {
590 MonoMethodDesc *desc;
591 MonoMethod *orig_method;
592 int subtype = decode_value (p, &p);
594 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
595 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
596 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
597 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
599 g_assert_not_reached ();
600 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
601 g_assert (orig_method);
602 mono_method_desc_free (desc);
603 *method = mono_monitor_get_fast_path (orig_method);
606 case MONO_WRAPPER_RUNTIME_INVOKE: {
608 MonoMethod *m = decode_method_ref_2 (module, p, &p);
612 *method = mono_marshal_get_runtime_invoke (m, FALSE);
616 g_assert_not_reached ();
618 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
619 /* Can't decode these */
620 g_assert_not_reached ();
621 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
622 image_index = decode_value (p, &p);
623 *token = decode_value (p, &p);
625 image = load_image (module, image_index);
628 } else if (image_index == MONO_AOT_METHODREF_GINST) {
630 MonoGenericContext ctx;
633 * These methods do not have a token which resolves them, so we
634 * resolve them immediately.
636 klass = decode_klass_ref (module, p, &p);
640 image_index = decode_value (p, &p);
641 *token = decode_value (p, &p);
643 image = load_image (module, image_index);
647 *method = mono_get_method_full (image, *token, NULL, NULL);
651 memset (&ctx, 0, sizeof (ctx));
653 if (FALSE && klass->generic_class) {
654 ctx.class_inst = klass->generic_class->context.class_inst;
655 ctx.method_inst = NULL;
657 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
660 memset (&ctx, 0, sizeof (ctx));
662 if (!decode_generic_context (module, &ctx, p, &p))
665 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
666 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
670 klass = decode_klass_ref (module, p, &p);
673 method_type = decode_value (p, &p);
675 switch (method_type) {
677 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
680 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
683 *method = mono_class_get_method_from_name (klass, "Get", -1);
686 *method = mono_class_get_method_from_name (klass, "Address", -1);
689 *method = mono_class_get_method_from_name (klass, "Set", -1);
692 g_assert_not_reached ();
695 g_assert (image_index < MONO_AOT_METHODREF_MIN);
696 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
698 image = load_image (module, image_index);
709 * decode_method_ref_2:
711 * Similar to decode_method_ref, but resolve and return the method itself.
714 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
718 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
724 method = mono_get_method (image, token, NULL);
730 make_writable (guint8* addr, guint32 len)
736 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
738 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1));
739 pages = (addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ();
741 err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_READ | MONO_MMAP_WRITE | MONO_MMAP_EXEC);
746 create_cache_structure (void)
752 home = g_get_home_dir ();
756 tmp = g_build_filename (home, ".mono", NULL);
757 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
758 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
762 err = mkdir (tmp, 0777);
765 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
771 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
772 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
773 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
777 err = mkdir (tmp, 0777);
780 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
789 * load_aot_module_from_cache:
791 * Experimental code to AOT compile loaded assemblies on demand.
794 * - Add environment variable MONO_AOT_CACHE_OPTIONS
795 * - Add options for controlling the cache size
796 * - Handle full cache by deleting old assemblies lru style
797 * - Add options for excluding assemblies during development
798 * - Maybe add a threshold after an assembly is AOT compiled
799 * - invoking a new mono process is a security risk
800 * - recompile the AOT module if one of its dependencies changes
803 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
805 char *fname, *cmd, *tmp2, *aot_options;
814 if (assembly->image->dynamic)
817 create_cache_structure ();
819 home = g_get_home_dir ();
821 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
822 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
826 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
827 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
830 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
832 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
834 aot_options = g_strdup_printf ("outfile=%s", fname);
836 if (spawn_compiler) {
837 /* FIXME: security */
838 /* FIXME: Has to pass the assembly loading path to the child process */
839 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
841 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
843 #if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
845 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
846 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
848 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
855 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
857 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
859 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
863 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
865 g_free (aot_options);
872 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
876 guint16 *table, *entry;
880 /* The first entry points to the hash */
884 table_size = table [0];
887 hash = mono_aot_str_hash (name) % table_size;
889 entry = &table [hash * 2];
891 /* Search the hash for the index into the globals table */
893 while (entry [0] != 0) {
894 guint32 index = entry [0] - 1;
895 guint32 next = entry [1];
897 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
899 if (!strcmp (globals [index * 2], name)) {
900 global_index = index;
905 entry = &table [next * 2];
911 if (global_index != -1)
912 *value = globals [global_index * 2 + 1];
916 mono_dl_symbol (module, name, value);
920 #ifndef SHT_ARM_EXIDX
921 #define SHT_ARM_EXIDX 0x70000001
924 #ifdef HAVE_DL_ITERATE_PHDR
926 dl_callback (struct dl_phdr_info *info, size_t size, void *data)
929 MonoAotModule *amodule = data;
931 if (!strcmp (amodule->aot_name, info->dlpi_name)) {
932 for (j = 0; j < info->dlpi_phnum; j++) {
933 if (info->dlpi_phdr [j].p_type == PT_GNU_EH_FRAME)
934 amodule->eh_frame_hdr = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
935 if (info->dlpi_phdr [j].p_type == SHT_ARM_EXIDX) {
936 amodule->arm_exidx = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
937 amodule->arm_exidx_size = info->dlpi_phdr [j].p_filesz;
948 load_aot_module (MonoAssembly *assembly, gpointer user_data)
951 MonoAotModule *amodule;
953 gboolean usable = TRUE;
954 char *saved_guid = NULL;
955 char *aot_version = NULL;
956 char *runtime_version, *build_info;
957 char *opt_flags = NULL;
959 gboolean full_aot = FALSE;
960 MonoAotFileInfo *file_info = NULL;
964 if (mono_compile_aot)
967 if (assembly->image->aot_module)
969 * Already loaded. This can happen because the assembly loading code might invoke
970 * the assembly load hooks multiple times for the same assembly.
974 if (assembly->image->dynamic)
977 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
981 if (static_aot_modules)
982 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
988 /* Statically linked AOT module */
990 aot_name = g_strdup_printf ("%s", assembly->aname.name);
991 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
994 sofile = load_aot_module_from_cache (assembly, &aot_name);
997 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
999 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1002 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
1008 if (!sofile && !globals) {
1009 if (mono_aot_only) {
1010 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1017 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
1018 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
1019 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
1020 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
1021 find_symbol (sofile, globals, "mono_aot_got_addr", (gpointer *)&got_addr);
1023 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
1024 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);
1028 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
1029 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
1034 build_info = mono_get_runtime_build_info ();
1035 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
1036 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);
1039 g_free (build_info);
1041 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
1042 g_assert (file_info);
1044 full_aot = ((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1046 if (mono_aot_only && !full_aot) {
1047 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
1050 if (!mono_aot_only && full_aot) {
1051 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
1055 if ((((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1056 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with LLVM.\n", aot_name);
1061 if (mono_aot_only) {
1062 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
1067 mono_dl_close (sofile);
1068 assembly->image->aot_module = NULL;
1072 amodule = g_new0 (MonoAotModule, 1);
1073 amodule->aot_name = aot_name;
1074 amodule->assembly = assembly;
1076 memcpy (&amodule->info, file_info, sizeof (*file_info));
1078 amodule->got = *got_addr;
1079 amodule->got [0] = assembly->image;
1080 amodule->globals = globals;
1081 amodule->sofile = sofile;
1082 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1084 /* Read image table */
1086 guint32 table_len, i;
1089 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
1092 table_len = *(guint32*)table;
1093 table += sizeof (guint32);
1094 amodule->image_table = g_new0 (MonoImage*, table_len);
1095 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1096 amodule->image_guids = g_new0 (char*, table_len);
1097 amodule->image_table_len = table_len;
1098 for (i = 0; i < table_len; ++i) {
1099 MonoAssemblyName *aname = &(amodule->image_names [i]);
1101 aname->name = g_strdup (table);
1102 table += strlen (table) + 1;
1103 amodule->image_guids [i] = g_strdup (table);
1104 table += strlen (table) + 1;
1106 aname->culture = g_strdup (table);
1107 table += strlen (table) + 1;
1108 memcpy (aname->public_key_token, table, strlen (table) + 1);
1109 table += strlen (table) + 1;
1111 table = ALIGN_PTR_TO (table, 8);
1112 aname->flags = *(guint32*)table;
1114 aname->major = *(guint32*)table;
1116 aname->minor = *(guint32*)table;
1118 aname->build = *(guint32*)table;
1120 aname->revision = *(guint32*)table;
1125 /* Read method and method_info tables */
1126 find_symbol (sofile, globals, "code_offsets", (gpointer*)&amodule->code_offsets);
1127 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
1128 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
1129 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
1130 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
1131 find_symbol (sofile, globals, "blob", (gpointer*)&amodule->blob);
1132 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
1133 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
1134 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
1135 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1136 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1137 find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC]));
1138 find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX]));
1139 find_symbol (sofile, globals, "imt_thunks", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK]));
1140 find_symbol (sofile, globals, "unwind_info", (gpointer)&amodule->unwind_info);
1141 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1143 amodule->mem_begin = amodule->code;
1145 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1146 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1148 if (make_unreadable) {
1149 #ifndef TARGET_WIN32
1152 int pages, err, len;
1154 addr = amodule->mem_begin;
1155 len = amodule->mem_end - amodule->mem_begin;
1157 /* Round down in both directions to avoid modifying data which is not ours */
1158 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1159 pages = ((addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ()) - 1;
1160 err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_NONE);
1161 g_assert (err == 0);
1167 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1168 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1170 g_hash_table_insert (aot_modules, assembly, amodule);
1173 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1175 assembly->image->aot_module = amodule;
1177 #ifdef HAVE_DL_ITERATE_PHDR
1178 /* Lookup the address of the .eh_frame_hdr () section if available */
1179 dl_iterate_phdr (dl_callback, amodule);
1182 if (mono_aot_only) {
1183 if (mono_defaults.corlib) {
1184 /* The second got slot contains the mscorlib got addr */
1185 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1187 amodule->got [1] = mscorlib_amodule->got;
1189 amodule->got [1] = amodule->got;
1194 * Since we store methoddef and classdef tokens when referring to methods/classes in
1195 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1196 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1197 * non-lazily, since we can't handle out-of-date errors later.
1199 for (i = 0; i < amodule->image_table_len; ++i)
1200 load_image (amodule, i);
1202 if (amodule->out_of_date) {
1203 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);
1204 if (mono_aot_only) {
1205 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);
1210 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1214 * mono_aot_register_globals:
1216 * This is called by the ctor function in AOT images compiled with the
1217 * 'no-dlsym' option.
1220 mono_aot_register_globals (gpointer *globals)
1222 g_assert_not_reached ();
1226 * mono_aot_register_module:
1228 * This should be called by embedding code to register AOT modules statically linked
1229 * into the executable. AOT_INFO should be the value of the
1230 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1233 mono_aot_register_module (gpointer *aot_info)
1241 /* Determine the assembly name */
1242 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1245 /* This could be called before startup */
1249 if (!static_aot_modules)
1250 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1252 g_hash_table_insert (static_aot_modules, aname, globals);
1259 mono_aot_init (void)
1261 InitializeCriticalSection (&aot_mutex);
1262 aot_modules = g_hash_table_new (NULL, NULL);
1264 mono_install_assembly_load_hook (load_aot_module, NULL);
1266 if (g_getenv ("MONO_LASTAOT"))
1267 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1268 if (g_getenv ("MONO_AOT_CACHE"))
1269 use_aot_cache = TRUE;
1273 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1277 info->vtable_size = decode_value (buf, &buf);
1278 if (info->vtable_size == -1)
1281 flags = decode_value (buf, &buf);
1282 info->ghcimpl = (flags >> 0) & 0x1;
1283 info->has_finalize = (flags >> 1) & 0x1;
1284 info->has_cctor = (flags >> 2) & 0x1;
1285 info->has_nested_classes = (flags >> 3) & 0x1;
1286 info->blittable = (flags >> 4) & 0x1;
1287 info->has_references = (flags >> 5) & 0x1;
1288 info->has_static_refs = (flags >> 6) & 0x1;
1289 info->no_special_static_fields = (flags >> 7) & 0x1;
1290 info->is_generic_container = (flags >> 8) & 0x1;
1292 if (info->has_cctor) {
1293 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1297 if (info->has_finalize) {
1298 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1299 if (!info->finalize_image)
1303 info->instance_size = decode_value (buf, &buf);
1304 info->class_size = decode_value (buf, &buf);
1305 info->packing_size = decode_value (buf, &buf);
1306 info->min_align = decode_value (buf, &buf);
1314 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1317 MonoClass *klass = vtable->klass;
1318 MonoAotModule *aot_module = klass->image->aot_module;
1320 MonoCachedClassInfo class_info;
1324 gboolean no_aot_trampoline;
1326 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1329 info = &aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1332 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1336 for (i = 0; i < slot; ++i)
1337 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1339 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1342 if (no_aot_trampoline)
1345 if (mono_metadata_token_index (token) == 0)
1348 return mono_aot_get_method_from_token (domain, image, token);
1352 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1354 MonoAotModule *aot_module = klass->image->aot_module;
1358 if (klass->rank || !aot_module)
1361 p = (guint8*)&aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1363 err = decode_cached_class_info (aot_module, res, p, &p);
1371 * mono_aot_get_class_from_name:
1373 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1374 * using a cache stored in the AOT file.
1375 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1377 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1381 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1383 MonoAotModule *aot_module = image->aot_module;
1384 guint16 *table, *entry;
1387 char full_name_buf [1024];
1389 const char *name2, *name_space2;
1391 guint32 cols [MONO_TYPEDEF_SIZE];
1392 GHashTable *nspace_table;
1394 if (!aot_module || !aot_module->class_name_table)
1401 /* First look in the cache */
1402 if (!aot_module->name_cache)
1403 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1404 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1406 *klass = g_hash_table_lookup (nspace_table, name);
1413 table_size = aot_module->class_name_table [0];
1414 table = aot_module->class_name_table + 1;
1416 if (name_space [0] == '\0')
1417 full_name = g_strdup_printf ("%s", name);
1419 if (strlen (name_space) + strlen (name) < 1000) {
1420 sprintf (full_name_buf, "%s.%s", name_space, name);
1421 full_name = full_name_buf;
1423 full_name = g_strdup_printf ("%s.%s", name_space, name);
1426 hash = mono_aot_str_hash (full_name) % table_size;
1427 if (full_name != full_name_buf)
1430 entry = &table [hash * 2];
1432 if (entry [0] != 0) {
1433 t = &image->tables [MONO_TABLE_TYPEDEF];
1436 guint32 index = entry [0];
1437 guint32 next = entry [1];
1438 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1440 name_table_accesses ++;
1442 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1444 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1445 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1447 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1449 *klass = mono_class_get (image, token);
1454 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1455 if (!nspace_table) {
1456 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1457 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1459 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1466 entry = &table [next * 2];
1478 #define DW_EH_PE_omit 0xff
1479 #define DW_EH_PE_uleb128 0x01
1480 #define DW_EH_PE_udata2 0x02
1481 #define DW_EH_PE_udata4 0x03
1482 #define DW_EH_PE_udata8 0x04
1483 #define DW_EH_PE_sleb128 0x09
1484 #define DW_EH_PE_sdata2 0x0A
1485 #define DW_EH_PE_sdata4 0x0B
1486 #define DW_EH_PE_sdata8 0x0C
1488 #define DW_EH_PE_absptr 0x00
1489 #define DW_EH_PE_pcrel 0x10
1490 #define DW_EH_PE_datarel 0x30
1491 #define DW_EH_PE_omit 0xff
1496 guint8 eh_frame_ptr_enc;
1497 guint8 fde_count_enc;
1505 * Decode the exception handling information in the .eh_frame section of the AOT
1506 * file belong to CODE, and construct a MonoJitInfo structure from it.
1507 * LOCKING: Acquires the domain lock.
1509 static G_GNUC_UNUSED void
1510 decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
1511 MonoMethod *method, guint8 *code, MonoJitInfo *jinfo)
1515 guint8 *eh_frame, *unwind_info;
1516 guint32 eh_frame_ptr;
1519 int i, pos, left, right, offset, offset1, offset2;
1520 guint32 unw_len, code_len;
1521 MonoJitExceptionInfo *ei;
1524 g_assert (amodule->eh_frame_hdr);
1526 // http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
1527 hdr = (eh_frame_hdr*)amodule->eh_frame_hdr;
1528 g_assert (hdr->version == 1);
1529 g_assert (hdr->eh_frame_ptr_enc == (DW_EH_PE_pcrel | DW_EH_PE_sdata4));
1530 g_assert (hdr->fde_count_enc == DW_EH_PE_udata4);
1531 g_assert (hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4));
1534 eh_frame_ptr = *(guint32*)p;
1536 fde_count = *(guint32*)p;
1540 /* Binary search in the table to find the entry for code */
1541 offset = code - amodule->eh_frame_hdr;
1546 pos = (left + right) / 2;
1548 offset1 = table [(pos * 2)];
1549 if (pos + 1 == fde_count)
1551 offset2 = amodule->code_end - amodule->code;
1553 offset2 = table [(pos + 1) * 2];
1555 if (offset < offset1)
1557 else if (offset >= offset2)
1563 g_assert (code >= amodule->eh_frame_hdr + table [(pos * 2)]);
1564 if (pos < fde_count)
1565 g_assert (code < amodule->eh_frame_hdr + table [(pos * 2) + 2]);
1567 eh_frame = amodule->eh_frame_hdr + table [(pos * 2) + 1];
1569 unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len);
1571 jinfo->code_size = code_len;
1572 jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
1573 jinfo->method = method;
1574 jinfo->code_start = code;
1575 jinfo->domain_neutral = 0;
1576 /* This signals that used_regs points to a normal cached unwind info */
1577 jinfo->from_aot = 0;
1579 g_assert (ei_len == jinfo->num_clauses);
1580 for (i = 0; i < jinfo->num_clauses; ++i) {
1581 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
1583 jei->try_start = ei [i].try_start;
1584 jei->try_end = ei [i].try_end;
1585 jei->handler_start = ei [i].handler_start;
1591 /* The offsets in the table are 31 bits long, have to extend them to 32 */
1592 #define EXTEND_PREL31(val) ((((gint32)(val)) << 1) >> 1)
1594 static inline guint32
1595 decode_uleb128 (guint8 *buf, guint8 **endbuf)
1605 res = res | (((int)(b & 0x7f)) << shift);
1617 decode_arm_eh_ops (guint8 *unwind_ops, int nops)
1619 int i, vsp_reg, vsp_offset;
1621 gint32 *reg_offsets;
1624 * Have to convert the ARM unwind info into DWARF unwind info.
1625 * The ARM unwind info specifies a simple set of instructions which need to be
1626 * executed during unwinding. It manipulates a virtual stack pointer (vsp). The
1627 * connection with DWARF unwind info is the following: after all ARM unwind
1628 * opcodes have been executed, the stack should be completely unwound, i.e.
1629 * vsp == DWARF CFA. This allows us to construct the DWARF opcodes corresponding
1630 * to the ARM opcodes.
1631 * The ARM unwind info is not instruction precise, i. e. it can't handle
1632 * async exceptions etc.
1634 /* The reg used to compute the initial value of vsp */
1635 vsp_reg = ARMREG_SP;
1636 /* The offset between vsp_reg and the CFA */
1639 /* The register save offsets from the initial value of vsp */
1640 reg_offsets = g_new0 (gint32, 16);
1641 for (i = 0; i < 16; ++i)
1642 reg_offsets [i] = -1;
1644 /* section 9.3 in the ehabi doc */
1645 for (i = 0; i < nops; ++i) {
1646 guint8 op = unwind_ops [i];
1648 if ((op >> 6) == 0) {
1649 /* vsp = vsp + (xxxxxx << 2) + 4. */
1650 vsp_offset += ((op & 0x3f) << 2) + 4;
1651 } else if ((op >> 6) == 1) {
1652 /* vsp = vsp - (xxxxxx << 2) - 4. */
1653 vsp_offset -= ((op & 0x3f) << 2) + 4;
1654 } else if (op == 0xb2) {
1655 /* vsp = vsp = vsp + 0x204 + (uleb128 << 2) */
1656 guint8 *p = unwind_ops + i + 1;
1657 guint32 v = decode_uleb128 (p, &p);
1659 vsp_offset += 0x204 + (v << 2);
1660 i = (p - unwind_ops) - 1;
1661 } else if (op >= 0x80 && op <= 0x8f) {
1667 g_assert (i + 1 < nops);
1668 op2 = unwind_ops [i + 1];
1671 for (j = 0; j < 8; ++j)
1672 if (op2 & (0x1 << j))
1673 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j));
1674 for (j = 0; j < 4; ++j)
1675 if (op & (0x1 << j))
1676 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R12 + j));
1679 for (j = 0; j < g_slist_length (regs); ++j)
1680 reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4);
1682 vsp_offset += g_slist_length (regs) * 4;
1684 g_slist_free (regs);
1687 } else if (op >= 0xa8 && op <= 0xaf) {
1691 /* pop r4-r[4 + nnn], r14 */
1694 for (j = 0; j <= (op & 0x7); ++j)
1695 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j));
1696 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R14));
1698 for (j = 0; j < g_slist_length (regs); ++j)
1699 reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4);
1701 vsp_offset += g_slist_length (regs) * 4;
1703 g_slist_free (regs);
1704 } else if (op == 0xb0) {
1707 } else if (op >= 0x90 && op <= 0x9f && op != 0x9d && op != 0x9f) {
1714 for (j = 0; j < nops; ++j)
1715 printf ("%x ", unwind_ops [j]);
1716 printf (" / %d\n", i);
1717 g_assert_not_reached ();
1723 /* vsp_reg + vsp_offset = CFA */
1724 mono_add_unwind_op_def_cfa (ops, (guint8*)NULL, (guint8*)NULL, vsp_reg, vsp_offset);
1726 for (i = 0; i < 16; ++i) {
1727 if (reg_offsets [i] != -1)
1728 /* The reg is saved at vsp_reg + reg_offset [i] == CFA - (vsp_offset - reg_offset [i]) */
1729 mono_add_unwind_op_offset (ops, (guint8*)NULL, (guint8*)NULL, i, - (vsp_offset - reg_offsets [i]));
1738 * Decode the exception handling information in the .ARM.exidx section of the AOT
1739 * file belong to CODE, and construct a MonoJitInfo structure from it.
1740 * LOCKING: Acquires the domain lock.
1743 decode_arm_exidx (MonoAotModule *amodule, MonoDomain *domain,
1744 MonoMethod *method, guint8 *code, guint32 code_len, MonoJitInfo *jinfo)
1747 guint8 *base, *code1, *code2;
1748 int i, pos, left, right, offset, offset1, offset2, count, nwords, nops;
1750 guint8 unwind_ops [64];
1752 guint8 *unwind_info;
1755 g_assert (amodule->arm_exidx);
1757 table = (guint32*)amodule->arm_exidx;
1760 * The table format is described in:
1761 * infocenter.arm.com/help/topic/com.arm.doc.../IHI0038A_ehabi.pdf
1764 base = amodule->arm_exidx;
1765 count = amodule->arm_exidx_size / 8;
1767 /* Binary search in the table to find the entry for code */
1768 offset = code - base;
1773 pos = (left + right) / 2;
1778 offset1 = EXTEND_PREL31 (table [(pos * 2)]);
1779 code1 = (guint8*)&(table [pos * 2]) + offset1;
1780 if (pos + 1 == count)
1782 offset2 = amodule->code_end - amodule->code;
1784 offset2 = EXTEND_PREL31 (table [(pos + 1) * 2]);
1785 code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2;
1789 else if (code >= code2)
1795 if (code >= code1) {
1797 * The linker might merge duplicate unwind table entries, so
1798 * offset1 and offset2 might point to another method, but this is not a problem.
1800 code1 = (guint8*)&(table [pos * 2]) + offset1;
1801 code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2;
1803 g_assert (code >= code1);
1805 g_assert (code < code2);
1807 entry = table [(pos * 2) + 1];
1809 /* inline entry, compact model, personality routine 0 */
1810 if ((entry & 0xff000000) == 0x80000000) {
1812 unwind_ops [0] = (entry & 0x00ff0000) >> 16;
1813 unwind_ops [1] = (entry & 0x0000ff00) >> 8;
1814 unwind_ops [2] = (entry & 0x000000ff) >> 0;
1816 ops = decode_arm_eh_ops (unwind_ops, nops);
1817 } else if ((entry & 0x80000000) == 0) {
1818 /* non-inline entry */
1819 guint8 *data = (guint8*)&table [(pos * 2) + 1] + EXTEND_PREL31 (entry);
1821 entry = ((guint32*)data) [0];
1823 /* compact model, personality routine 1 */
1824 g_assert ((entry & 0xff000000) == 0x81000000);
1826 nwords = (entry & 0x00ff0000) >> 16;
1827 nops = nwords * 4 + 2;
1828 g_assert (nops < 64);
1830 unwind_ops [0] = (entry & 0x0000ff00) >> 8;
1831 unwind_ops [1] = (entry & 0x000000ff) >> 0;
1833 for (i = 0; i < nwords; ++i) {
1834 entry = ((guint32*)data) [1 + i];
1835 unwind_ops [(i * 4) + 2] = (entry & 0xff000000) >> 24;
1836 unwind_ops [(i * 4) + 2 + 1] = (entry & 0x00ff0000) >> 16;
1837 unwind_ops [(i * 4) + 2 + 2] = (entry & 0x0000ff00) >> 8;
1838 unwind_ops [(i * 4) + 2 + 3] = (entry & 0x000000ff) >> 0;
1841 ops = decode_arm_eh_ops (unwind_ops, nops);
1846 unwind_info = mono_unwind_ops_encode (ops, &unw_len);
1848 /* The method has no unwind info */
1853 jinfo->code_size = code_len;
1854 jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
1855 jinfo->method = method;
1856 jinfo->code_start = code;
1857 jinfo->domain_neutral = 0;
1858 /* This signals that used_regs points to a normal cached unwind info */
1859 jinfo->from_aot = 0;
1864 * LOCKING: Acquires the domain lock.
1867 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
1868 MonoMethod *method, guint8* ex_info, guint8 *addr,
1869 guint8 *code, guint32 code_len)
1873 guint used_int_regs, flags;
1874 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points;
1877 int generic_info_size;
1879 /* Load the method info from the AOT file */
1882 flags = decode_value (p, &p);
1883 has_generic_jit_info = (flags & 1) != 0;
1884 has_dwarf_unwind_info = (flags & 2) != 0;
1885 has_clauses = (flags & 4) != 0;
1886 has_seq_points = (flags & 8) != 0;
1887 from_llvm = (flags & 16) != 0;
1888 if (has_dwarf_unwind_info) {
1891 offset = decode_value (p, &p);
1892 g_assert (offset < (1 << 30));
1893 used_int_regs = offset;
1895 used_int_regs = decode_value (p, &p);
1897 if (has_generic_jit_info)
1898 generic_info_size = sizeof (MonoGenericJitInfo);
1900 generic_info_size = 0;
1902 /* Exception table */
1904 int num_clauses = decode_value (p, &p);
1907 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size);
1908 jinfo->num_clauses = num_clauses;
1910 for (i = 0; i < num_clauses; ++i) {
1911 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1913 ei->flags = decode_value (p, &p);
1914 ei->exvar_offset = decode_value (p, &p);
1916 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1917 ei->data.filter = code + decode_value (p, &p);
1919 if (decode_value (p, &p))
1920 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1923 ei->try_start = code + decode_value (p, &p);
1924 ei->try_end = code + decode_value (p, &p);
1925 ei->handler_start = code + decode_value (p, &p);
1929 jinfo = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + generic_info_size);
1933 /* LLVM compiled method */
1934 /* The info is in the .eh_frame section */
1936 decode_arm_exidx (amodule, domain, method, code, code_len, jinfo);
1938 decode_eh_frame (amodule, domain, method, code, jinfo);
1941 jinfo->code_size = code_len;
1942 jinfo->used_regs = used_int_regs;
1943 jinfo->method = method;
1944 jinfo->code_start = code;
1945 jinfo->domain_neutral = 0;
1946 jinfo->from_aot = 1;
1949 if (has_generic_jit_info) {
1950 MonoGenericJitInfo *gi;
1952 jinfo->has_generic_jit_info = 1;
1954 gi = mono_jit_info_get_generic_jit_info (jinfo);
1957 gi->has_this = decode_value (p, &p);
1958 gi->this_reg = decode_value (p, &p);
1959 gi->this_offset = decode_value (p, &p);
1961 /* This currently contains no data */
1962 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1964 jinfo->method = decode_method_ref_2 (amodule, p, &p);
1967 if (has_seq_points) {
1968 MonoSeqPointInfo *seq_points;
1969 int il_offset, native_offset, last_il_offset, last_native_offset, j;
1971 int len = decode_value (p, &p);
1973 seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
1974 last_il_offset = last_native_offset = 0;
1975 for (i = 0; i < len; ++i) {
1976 SeqPoint *sp = &seq_points->seq_points [i];
1977 il_offset = last_il_offset + decode_value (p, &p);
1978 native_offset = last_native_offset + decode_value (p, &p);
1980 sp->il_offset = il_offset;
1981 sp->native_offset = native_offset;
1983 sp->next_len = decode_value (p, &p);
1984 sp->next = g_new (int, sp->next_len);
1985 for (j = 0; j < sp->next_len; ++j)
1986 sp->next [j] = decode_value (p, &p);
1988 last_il_offset = il_offset;
1989 last_native_offset = native_offset;
1992 mono_domain_lock (domain);
1993 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
1994 mono_domain_unlock (domain);
1997 /* Load debug info */
1998 buf_len = decode_value (p, &p);
1999 mono_debug_add_aot_method (domain, method, code, p, buf_len);
2001 if (amodule != jinfo->method->klass->image->aot_module) {
2004 ji_to_amodule = g_hash_table_new (NULL, NULL);
2005 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
2013 * mono_aot_get_unwind_info:
2015 * Return a pointer to the DWARF unwind info belonging to JI.
2018 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2020 MonoAotModule *amodule = ji->method->klass->image->aot_module;
2022 guint8 *code = ji->code_start;
2025 g_assert (ji->from_aot);
2027 if (!(code >= amodule->code && code <= amodule->code_end)) {
2028 /* ji belongs to a different aot module than amodule */
2030 g_assert (ji_to_amodule);
2031 amodule = g_hash_table_lookup (ji_to_amodule, ji);
2033 g_assert (code >= amodule->code && code <= amodule->code_end);
2036 p = amodule->unwind_info + ji->used_regs;
2037 *unwind_info_len = decode_value (p, &p);
2042 compare_ints (const void *a, const void *b)
2044 return *(gint32*)a - *(gint32*)b;
2048 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2050 int pos, left, right, offset, offset1, offset2, code_len;
2051 int method_index, table_len, is_wrapper;
2053 MonoAotModule *amodule = image->aot_module;
2056 guint8 *code, *ex_info, *p;
2058 int nmethods = amodule->info.nmethods;
2059 gint32 *code_offsets;
2065 if (domain != mono_get_root_domain ())
2069 offset = (guint8*)addr - amodule->code;
2071 /* Compute a sorted table mapping code offsets to method indexes. */
2072 if (!amodule->sorted_code_offsets) {
2073 code_offsets = g_new0 (gint32, nmethods * 2);
2074 for (i = 0; i < nmethods; ++i) {
2075 code_offsets [(i * 2)] = amodule->code_offsets [i];
2076 code_offsets [(i *2) + 1] = i;
2078 /* FIXME: Use a merge sort as this is mostly sorted */
2079 qsort (code_offsets, nmethods, sizeof (gint32) * 2, compare_ints);
2080 for (i = 0; i < nmethods -1; ++i)
2081 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
2083 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
2084 /* Somebody got in before us */
2085 g_free (code_offsets);
2088 code_offsets = amodule->sorted_code_offsets;
2090 /* Binary search in the sorted_code_offsets table */
2094 pos = (left + right) / 2;
2096 offset1 = code_offsets [(pos * 2)];
2097 if (pos + 1 == nmethods)
2098 offset2 = amodule->code_end - amodule->code;
2100 offset2 = code_offsets [(pos + 1) * 2];
2102 if (offset < offset1)
2104 else if (offset >= offset2)
2110 g_assert (offset >= code_offsets [(pos * 2)]);
2111 if (pos + 1 < nmethods)
2112 g_assert (offset < code_offsets [((pos + 1) * 2)]);
2113 method_index = code_offsets [(pos * 2) + 1];
2115 code = &amodule->code [amodule->code_offsets [method_index]];
2116 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
2118 if (pos == nmethods - 1)
2119 code_len = amodule->code_end - code;
2121 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
2123 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
2125 /* Might be a wrapper/extra method */
2126 if (amodule->extra_methods) {
2128 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
2135 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
2137 * This is hit for extra methods which are called directly, so they are
2138 * not in amodule->extra_methods.
2140 table_len = amodule->extra_method_info_offsets [0];
2141 table = amodule->extra_method_info_offsets + 1;
2148 pos = ((left + right) / 2);
2150 g_assert (pos < table_len);
2152 if (table [pos * 2] < method_index)
2154 else if (table [pos * 2] > method_index)
2160 p = amodule->blob + table [(pos * 2) + 1];
2161 is_wrapper = decode_value (p, &p);
2162 g_assert (!is_wrapper);
2163 method = decode_method_ref_2 (amodule, p, &p);
2166 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
2167 method = mono_get_method (image, token, NULL);
2174 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
2176 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
2178 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
2179 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
2181 /* Add it to the normal JitInfo tables */
2182 mono_jit_info_table_add (domain, jinfo);
2188 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
2196 case MONO_PATCH_INFO_METHOD:
2197 case MONO_PATCH_INFO_METHOD_JUMP:
2198 case MONO_PATCH_INFO_ICALL_ADDR:
2199 case MONO_PATCH_INFO_METHOD_RGCTX: {
2202 gboolean no_aot_trampoline;
2204 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
2208 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
2209 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (image, token));
2210 ji->type = MONO_PATCH_INFO_ABS;
2214 ji->data.method = method;
2216 ji->data.method = mono_get_method (image, token, NULL);
2217 g_assert (ji->data.method);
2218 mono_class_init (ji->data.method->klass);
2222 case MONO_PATCH_INFO_INTERNAL_METHOD:
2223 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2224 guint32 len = decode_value (p, &p);
2226 ji->data.name = (char*)p;
2230 case MONO_PATCH_INFO_METHODCONST:
2232 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
2233 if (!ji->data.method)
2236 case MONO_PATCH_INFO_VTABLE:
2237 case MONO_PATCH_INFO_CLASS:
2238 case MONO_PATCH_INFO_IID:
2239 case MONO_PATCH_INFO_ADJUSTED_IID:
2241 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2242 if (!ji->data.klass)
2245 case MONO_PATCH_INFO_CLASS_INIT:
2246 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2247 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2248 if (!ji->data.klass)
2251 case MONO_PATCH_INFO_IMAGE:
2252 ji->data.image = load_image (aot_module, decode_value (p, &p));
2253 if (!ji->data.image)
2256 case MONO_PATCH_INFO_FIELD:
2257 case MONO_PATCH_INFO_SFLDA:
2259 ji->data.field = decode_field_info (aot_module, p, &p);
2260 if (!ji->data.field)
2263 case MONO_PATCH_INFO_SWITCH:
2264 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
2265 ji->data.table->table_size = decode_value (p, &p);
2266 table = g_new (gpointer, ji->data.table->table_size);
2267 ji->data.table->table = (MonoBasicBlock**)table;
2268 for (i = 0; i < ji->data.table->table_size; i++)
2269 table [i] = (gpointer)(gssize)decode_value (p, &p);
2271 case MONO_PATCH_INFO_R4: {
2274 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
2275 val = decode_value (p, &p);
2276 *(float*)ji->data.target = *(float*)&val;
2279 case MONO_PATCH_INFO_R8: {
2283 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
2285 val [0] = decode_value (p, &p);
2286 val [1] = decode_value (p, &p);
2287 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
2288 *(double*)ji->data.target = *(double*)&v;
2291 case MONO_PATCH_INFO_LDSTR:
2292 image = load_image (aot_module, decode_value (p, &p));
2295 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
2297 case MONO_PATCH_INFO_RVA:
2298 case MONO_PATCH_INFO_DECLSEC:
2299 case MONO_PATCH_INFO_LDTOKEN:
2300 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2302 image = load_image (aot_module, decode_value (p, &p));
2305 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
2307 ji->data.token->has_context = decode_value (p, &p);
2308 if (ji->data.token->has_context) {
2309 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
2314 case MONO_PATCH_INFO_EXC_NAME:
2315 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2316 if (!ji->data.klass)
2318 ji->data.name = ji->data.klass->name;
2320 case MONO_PATCH_INFO_METHOD_REL:
2321 ji->data.offset = decode_value (p, &p);
2323 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2324 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2325 case MONO_PATCH_INFO_MONITOR_ENTER:
2326 case MONO_PATCH_INFO_MONITOR_EXIT:
2328 case MONO_PATCH_INFO_RGCTX_FETCH: {
2330 MonoJumpInfoRgctxEntry *entry;
2332 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2333 entry->method = decode_method_ref_2 (aot_module, p, &p);
2334 entry->in_mrgctx = decode_value (p, &p);
2335 entry->info_type = decode_value (p, &p);
2336 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2337 entry->data->type = decode_value (p, &p);
2339 res = decode_patch (aot_module, mp, entry->data, p, &p);
2342 ji->data.rgctx_entry = entry;
2345 case MONO_PATCH_INFO_SEQ_POINT_INFO:
2347 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
2348 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
2350 imt_tramp->method = decode_method_ref_2 (aot_module, p, &p);
2351 imt_tramp->vt_offset = decode_value (p, &p);
2353 ji->data.imt_tramp = imt_tramp;
2357 g_warning ("unhandled type %d", ji->type);
2358 g_assert_not_reached ();
2369 static MonoJumpInfo*
2370 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
2371 guint32 **got_slots,
2372 guint8 *buf, guint8 **endbuf)
2374 MonoJumpInfo *patches;
2380 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
2382 *got_slots = g_malloc (sizeof (guint32) * n_patches);
2384 for (pindex = 0; pindex < n_patches; ++pindex) {
2385 MonoJumpInfo *ji = &patches [pindex];
2390 got_offset = decode_value (p, &p);
2392 if (aot_module->got [got_offset]) {
2393 /* Already loaded */
2394 //printf ("HIT!\n");
2396 shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
2398 ji->type = decode_value (shared_p, &shared_p);
2400 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
2405 (*got_slots) [pindex] = got_offset;
2412 g_free (*got_slots);
2419 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
2422 * Jump addresses cannot be patched by the trampoline code since it
2423 * does not have access to the caller's address. Instead, we collect
2424 * the addresses of the GOT slots pointing to a method, and patch
2425 * them after the method has been compiled.
2427 MonoJitDomainInfo *info = domain_jit_info (domain);
2430 mono_domain_lock (domain);
2431 if (!info->jump_target_got_slot_hash)
2432 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
2433 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
2434 list = g_slist_prepend (list, got_slot);
2435 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
2436 mono_domain_unlock (domain);
2442 * Load the method identified by METHOD_INDEX from the AOT image. Return a
2443 * pointer to the native code of the method, or NULL if not found.
2444 * METHOD might not be set if the caller only has the image/token info.
2447 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
2450 gboolean from_plt = method == NULL;
2452 int i, pindex, n_patches, used_strings;
2453 gboolean keep_patches = TRUE;
2455 MonoJitInfo *jinfo = NULL;
2456 guint8 *code, *info;
2458 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2461 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
2462 /* Non shared AOT code can't be used in other appdomains */
2465 if (amodule->out_of_date)
2468 if (amodule->code_offsets [method_index] == 0xffffffff) {
2469 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2473 method = mono_get_method (image, token, NULL);
2474 full_name = mono_method_full_name (method, TRUE);
2475 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2481 code = &amodule->code [amodule->code_offsets [method_index]];
2483 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
2486 if (!amodule->methods_loaded)
2487 amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods + 1);
2490 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2493 if (mono_last_aot_method != -1) {
2494 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
2496 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
2498 printf ("LAST AOT METHOD: %s%s%s.%s.\n", method->klass->name_space, method->klass->name_space [0] ? "." : "", method->klass->name, method->name);
2500 printf ("LAST AOT METHOD: %p %d\n", code, method_index);
2507 klass = method->klass;
2508 decode_klass_ref (amodule, p, &p);
2510 klass = decode_klass_ref (amodule, p, &p);
2513 if (amodule->info.opts & MONO_OPT_SHARED)
2514 used_strings = decode_value (p, &p);
2518 for (i = 0; i < used_strings; i++) {
2519 guint token = decode_value (p, &p);
2520 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2523 if (amodule->info.opts & MONO_OPT_SHARED)
2524 keep_patches = FALSE;
2526 n_patches = decode_value (p, &p);
2528 keep_patches = FALSE;
2531 MonoJumpInfo *patches;
2537 mp = mono_mempool_new ();
2539 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2540 if (patches == NULL)
2543 for (pindex = 0; pindex < n_patches; ++pindex) {
2544 MonoJumpInfo *ji = &patches [pindex];
2546 if (!amodule->got [got_slots [pindex]]) {
2547 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2548 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2549 amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
2550 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2551 register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
2553 ji->type = MONO_PATCH_INFO_NONE;
2559 mono_mempool_destroy (mp);
2562 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2566 method = mono_get_method (image, token, NULL);
2568 full_name = mono_method_full_name (method, TRUE);
2571 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
2573 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);
2579 mono_jit_stats.methods_aot++;
2581 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2585 if (method && method->wrapper_type)
2586 g_hash_table_insert (amodule->method_to_code, method, code);
2590 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
2594 method = mono_get_method (image, token, NULL);
2597 mono_profiler_method_jit (method);
2598 jinfo = mono_jit_info_table_find (domain, (char*)code);
2600 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
2603 if (from_plt && klass && !klass->generic_container)
2604 mono_runtime_class_init (mono_class_vtable (domain, klass));
2609 /* FIXME: The space in domain->mp is wasted */
2610 if (amodule->info.opts & MONO_OPT_SHARED)
2611 /* No need to cache patches */
2612 mono_mempool_destroy (mp);
2621 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name)
2623 guint32 table_size, entry_size, hash;
2624 guint32 *table, *entry;
2626 static guint32 n_extra_decodes;
2631 table_size = amodule->extra_method_table [0];
2632 table = amodule->extra_method_table + 1;
2635 hash = mono_aot_method_hash (method) % table_size;
2637 entry = &table [hash * entry_size];
2644 guint32 key = entry [0];
2645 guint32 value = entry [1];
2646 guint32 next = entry [entry_size - 1];
2649 int is_wrapper_name;
2651 p = amodule->blob + key;
2652 is_wrapper_name = decode_value (p, &p);
2653 if (is_wrapper_name) {
2654 int wrapper_type = decode_value (p, &p);
2655 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2659 } else if (can_method_ref_match_method (amodule, p, method)) {
2661 if (!amodule->method_ref_to_method)
2662 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2663 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2667 m = decode_method_ref_2 (amodule, p, &p);
2670 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2679 /* Special case: wrappers of shared generic methods */
2680 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2681 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2682 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2683 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2685 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2691 /* Methods decoded needlessly */
2694 printf ("%d %s %s\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
2700 entry = &table [next * entry_size];
2709 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2711 g_ptr_array_add ((GPtrArray*)user_data, value);
2715 * find_extra_method:
2717 * Try finding METHOD in the extra_method table in all AOT images.
2718 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2719 * module where the method was found.
2722 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2729 if (method->wrapper_type)
2730 name = mono_aot_wrapper_name (method);
2732 /* Try the method's module first */
2733 *out_amodule = method->klass->image->aot_module;
2734 index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name);
2735 if (index != 0xffffff) {
2741 * Try all other modules.
2742 * This is needed because generic instances klass->image points to the image
2743 * containing the generic definition, but the native code is generated to the
2744 * AOT image which contains the reference.
2747 /* Make a copy to avoid doing the search inside the aot lock */
2748 modules = g_ptr_array_new ();
2750 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2754 for (i = 0; i < modules->len; ++i) {
2755 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2757 if (amodule != method->klass->image->aot_module)
2758 index = find_extra_method_in_amodule (amodule, method, name);
2759 if (index != 0xffffff) {
2760 *out_amodule = amodule;
2765 g_ptr_array_free (modules, TRUE);
2772 * mono_aot_get_method:
2774 * Return a pointer to the AOTed native code for METHOD if it can be found,
2776 * On platforms with function pointers, this doesn't return a function pointer.
2779 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2781 MonoClass *klass = method->klass;
2782 guint32 method_index;
2783 MonoAotModule *amodule = klass->image->aot_module;
2789 if (amodule->out_of_date)
2792 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2793 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2794 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2795 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2799 * Use the original method instead of its invoke-with-check wrapper.
2800 * This is not a problem when using full-aot, since it doesn't support
2803 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
2804 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
2806 g_assert (klass->inited);
2808 /* Find method index */
2809 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2810 method = mono_method_get_declaring_generic_method (method);
2811 method_index = mono_metadata_token_index (method->token) - 1;
2812 } else if (method->is_inflated || !method->token) {
2813 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2815 code = g_hash_table_lookup (amodule->method_to_code, method);
2820 method_index = find_extra_method (method, &amodule);
2822 * Special case the ICollection<T> wrappers for arrays, as they cannot
2823 * be statically enumerated, and each wrapper ends up calling the same
2826 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
2827 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
2829 code = mono_aot_get_method (domain, m);
2831 if (mono_method_needs_static_rgctx_invoke (m, FALSE))
2832 code = mono_create_static_rgctx_trampoline (m, code);
2839 * Special case Array.GetGenericValueImpl which is a generic icall.
2840 * Generic sharing currently can't handle it, but the icall returns data using
2841 * an out parameter, so the managed-to-native wrappers can share the same code.
2843 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
2845 MonoGenericContext ctx;
2846 MonoType *args [16];
2848 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
2849 /* Avoid recursion */
2852 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
2855 memset (&ctx, 0, sizeof (ctx));
2856 args [0] = &mono_defaults.object_class->byval_arg;
2857 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2859 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2862 * Get the code for the <object> instantiation which should be emitted into
2863 * the mscorlib aot image by the AOT compiler.
2865 code = mono_aot_get_method (domain, m);
2870 if (method_index == 0xffffff) {
2871 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2874 full_name = mono_method_full_name (method, TRUE);
2875 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2881 if (method_index == 0xffffff)
2884 /* Needed by find_jit_info */
2886 if (!amodule->extra_methods)
2887 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2888 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2892 method_index = mono_metadata_token_index (method->token) - 1;
2895 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2899 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2903 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2905 MonoAotModule *aot_module = image->aot_module;
2911 method_index = mono_metadata_token_index (token) - 1;
2913 return load_method (domain, aot_module, image, NULL, token, method_index);
2919 } IsGotEntryUserData;
2922 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2924 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2925 MonoAotModule *aot_module = (MonoAotModule*)value;
2927 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
2932 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2934 IsGotEntryUserData user_data;
2939 user_data.addr = addr;
2940 user_data.res = FALSE;
2942 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2945 return user_data.res;
2950 MonoAotModule *module;
2951 } FindAotModuleUserData;
2954 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2956 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2957 MonoAotModule *aot_module = (MonoAotModule*)value;
2959 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2960 data->module = aot_module;
2963 static inline MonoAotModule*
2964 find_aot_module (guint8 *code)
2966 FindAotModuleUserData user_data;
2971 /* Reading these need no locking */
2972 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2975 user_data.addr = code;
2976 user_data.module = NULL;
2979 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2982 return user_data.module;
2986 * mono_aot_plt_resolve:
2988 * This function is called by the entries in the PLT to resolve the actual method that
2989 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2992 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2994 #ifdef MONO_ARCH_AOT_SUPPORTED
2995 guint8 *p, *target, *plt_entry;
2997 MonoAotModule *module = (MonoAotModule*)aot_module;
2998 gboolean res, no_ftnptr = FALSE;
3001 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
3003 p = &module->blob [plt_info_offset];
3005 ji.type = decode_value (p, &p);
3007 mp = mono_mempool_new_size (512);
3008 res = decode_patch (module, mp, &ji, p, &p);
3009 // FIXME: Error handling (how ?)
3013 * Avoid calling resolve_patch_target in the full-aot case if possible, since
3014 * it would create a trampoline, and we don't need that.
3015 * We could do this only if the method does not need the special handling
3016 * in mono_magic_trampoline ().
3018 if (mono_aot_only && ji.type == MONO_PATCH_INFO_METHOD && !ji.data.method->is_generic && !mono_method_check_context_used (ji.data.method) && !(ji.data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) &&
3019 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
3020 target = mono_jit_compile_method (ji.data.method);
3023 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
3027 * The trampoline expects us to return a function descriptor on platforms which use
3028 * it, but resolve_patch_target returns a direct function pointer for some type of
3029 * patches, so have to translate between the two.
3030 * FIXME: Clean this up, but how ?
3032 if (ji.type == MONO_PATCH_INFO_ABS || ji.type == MONO_PATCH_INFO_INTERNAL_METHOD || ji.type == MONO_PATCH_INFO_CLASS_INIT || ji.type == MONO_PATCH_INFO_ICALL_ADDR || ji.type == MONO_PATCH_INFO_JIT_ICALL_ADDR || ji.type == MONO_PATCH_INFO_RGCTX_FETCH) {
3033 /* These should already have a function descriptor */
3034 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3035 g_assert (((gpointer*)target) [2] == 0);
3037 } else if (!no_ftnptr) {
3038 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3039 g_assert (((gpointer*)target) [2] != 0);
3041 target = mono_create_ftnptr (mono_domain_get (), target);
3044 mono_mempool_destroy (mp);
3046 /* Patch the PLT entry with target which might be the actual method not a trampoline */
3047 plt_entry = mono_aot_get_plt_entry (code);
3048 g_assert (plt_entry);
3049 mono_arch_patch_plt_entry (plt_entry, module->got, NULL, target);
3053 g_assert_not_reached ();
3061 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
3062 * method in the module is loaded to avoid committing memory by writing to it.
3063 * LOCKING: Assumes the AOT lock is held.
3066 init_plt (MonoAotModule *amodule)
3068 #ifndef MONO_CROSS_COMPILE
3070 #ifdef MONO_ARCH_AOT_SUPPORTED
3072 guint8 *buf = amodule->plt;
3073 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
3079 if (amodule->plt_inited)
3082 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
3085 /* Initialize the first PLT entry */
3086 make_writable (amodule->plt, amodule->plt_end - amodule->plt);
3087 x86_jump_code (buf, tramp);
3088 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
3090 * Initialize the PLT entries in the GOT to point to the default targets.
3093 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
3094 plt_0 = mono_create_ftnptr (mono_domain_get (), amodule->plt);
3095 /* The first entry points to the AOT trampoline */
3096 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base] = tramp;
3097 for (i = 1; i < amodule->info.plt_size; ++i)
3098 /* All the default entries point to the first entry */
3099 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = plt_0;
3101 g_assert_not_reached ();
3104 amodule->plt_inited = TRUE;
3107 #endif /* MONO_CROSS_COMPILE */
3111 * mono_aot_get_plt_entry:
3113 * Return the address of the PLT entry called by the code at CODE if exists.
3116 mono_aot_get_plt_entry (guint8 *code)
3118 MonoAotModule *aot_module = find_aot_module (code);
3119 #if defined(__arm__) || defined(__mono_ppc__)
3126 #if defined(__i386__) || defined(__x86_64__)
3127 if (code [-5] == 0xe8) {
3128 guint32 disp = *(guint32*)(code - 4);
3129 guint8 *target = code + disp;
3131 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3134 #elif defined(__arm__)
3135 ins = ((guint32*)(gpointer)code) [-1];
3137 /* Should be a 'bl' */
3138 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
3139 gint32 disp = ((gint32)ins) & 0xffffff;
3140 guint8 *target = code - 4 + 8 + (disp * 4);
3142 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3145 #elif defined(__mono_ppc__)
3146 /* Should be a bl */
3147 ins = ((guint32*)(gpointer)code) [-1];
3149 if ((ins >> 26 == 18) && ((ins & 1) == 1) && ((ins & 2) == 0)) {
3150 gint32 disp = (((gint32)ins) >> 2) & 0xffffff;
3151 guint8 *target = code - 4 + (disp * 4);
3153 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3157 g_assert_not_reached ();
3164 * mono_aot_get_plt_info_offset:
3166 * Return the PLT info offset belonging to the plt entry called by CODE.
3169 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3171 guint8 *plt_entry = mono_aot_get_plt_entry (code);
3173 g_assert (plt_entry);
3175 /* The offset is embedded inside the code after the plt entry */
3176 #if defined(__i386__)
3177 return *(guint32*)(plt_entry + 5);
3178 #elif defined(__x86_64__)
3179 return *(guint32*)(plt_entry + 6);
3180 #elif defined(__arm__)
3181 /* The offset is stored as the 4th word of the plt entry */
3182 return ((guint32*)plt_entry) [3];
3183 #elif defined(__mono_ppc__)
3184 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3185 return ((guint32*)plt_entry) [8];
3187 return ((guint32*)plt_entry) [6];
3190 g_assert_not_reached ();
3196 mono_create_ftnptr_malloc (guint8 *code)
3198 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3199 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
3201 ftnptr->code = code;
3214 * Load the function named NAME from the aot image.
3217 load_function (MonoAotModule *amodule, const char *name)
3221 int n_patches, pindex;
3227 symbol = g_strdup_printf ("%s", name);
3228 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
3231 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
3233 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
3237 symbol = g_strdup_printf ("%s_p", name);
3238 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
3241 /* Nothing to patch */
3244 p = amodule->blob + *(guint32*)p;
3246 /* Similar to mono_aot_load_method () */
3248 n_patches = decode_value (p, &p);
3251 MonoJumpInfo *patches;
3254 mp = mono_mempool_new ();
3256 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
3259 for (pindex = 0; pindex < n_patches; ++pindex) {
3260 MonoJumpInfo *ji = &patches [pindex];
3263 if (amodule->got [got_slots [pindex]])
3267 * When this code is executed, the runtime may not be initalized yet, so
3268 * resolve the patch info by hand.
3270 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3271 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
3272 target = mono_get_lmf_addr;
3273 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
3274 target = mono_thread_force_interruption_checkpoint;
3275 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
3276 target = mono_exception_from_token;
3277 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
3278 target = mono_get_throw_exception ();
3280 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
3281 target = mono_amd64_throw_exception;
3284 } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
3285 target = mono_amd64_get_original_ip;
3288 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
3289 target = mono_arm_throw_exception;
3290 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
3291 target = mono_arm_throw_exception_by_token;
3294 } else if (!strcmp (ji->data.name, "mono_ppc_throw_exception")) {
3295 target = mono_ppc_throw_exception;
3297 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
3298 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
3299 target = (gpointer)mono_get_trampoline_func (tramp_type2);
3300 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
3301 /* atoll is needed because the the offset is unsigned */
3305 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
3306 g_assert (res == 1);
3307 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
3308 target = mono_create_ftnptr_malloc (target);
3309 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
3310 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
3311 target = mono_create_ftnptr_malloc (target);
3312 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
3313 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
3314 target = mono_create_ftnptr_malloc (target);
3315 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
3316 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
3317 target = mono_create_ftnptr_malloc (target);
3318 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
3319 target = mono_thread_get_and_clear_pending_exception;
3321 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
3322 g_assert_not_reached ();
3326 /* Hopefully the code doesn't have patches which need method or
3329 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
3333 amodule->got [got_slots [pindex]] = target;
3338 mono_mempool_destroy (mp);
3345 * Return the piece of code identified by NAME from the mscorlib AOT file.
3346 * On ppc64, this returns a function descriptor.
3349 mono_aot_get_named_code (const char *name)
3352 MonoAotModule *amodule;
3354 image = mono_defaults.corlib;
3357 amodule = image->aot_module;
3360 return mono_create_ftnptr_malloc (load_function (amodule, name));
3363 /* Return a given kind of trampoline */
3365 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
3367 MonoAotModule *amodule;
3368 int index, tramp_size;
3371 /* Currently, we keep all trampolines in the mscorlib AOT image */
3372 image = mono_defaults.corlib;
3377 amodule = image->aot_module;
3380 *out_amodule = amodule;
3382 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
3383 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
3385 index = amodule->trampoline_index [tramp_type] ++;
3389 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
3391 tramp_size = amodule->info.trampoline_size [tramp_type];
3394 *out_tramp_size = tramp_size;
3396 return amodule->trampolines [tramp_type] + (index * tramp_size);
3400 * Return a specific trampoline from the AOT file.
3403 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3405 MonoAotModule *amodule;
3406 guint32 got_offset, tramp_size;
3407 guint8 *code, *tramp;
3408 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
3409 static gboolean inited;
3410 static guint32 num_trampolines;
3416 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
3425 if (!generic_trampolines [tramp_type]) {
3428 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
3429 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
3433 tramp = generic_trampolines [tramp_type];
3436 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
3438 amodule->got [got_offset] = tramp;
3439 amodule->got [got_offset + 1] = arg1;
3442 *code_len = tramp_size;
3448 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3450 MonoAotModule *amodule;
3454 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
3456 amodule->got [got_offset] = ctx;
3457 amodule->got [got_offset + 1] = addr;
3459 /* The caller expects an ftnptr */
3460 return mono_create_ftnptr (mono_domain_get (), code);
3464 mono_aot_get_unbox_trampoline (MonoMethod *method)
3466 guint32 method_index = mono_metadata_token_index (method->token) - 1;
3467 MonoAotModule *amodule;
3471 if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
3472 guint32 index = find_extra_method (method, &amodule);
3473 g_assert (index != 0xffffff);
3475 symbol = g_strdup_printf ("ut_e_%d", index);
3477 amodule = method->klass->image->aot_module;
3480 symbol = g_strdup_printf ("ut_%d", method_index);
3482 code = load_function (amodule, symbol);
3485 /* The caller expects an ftnptr */
3486 return mono_create_ftnptr (mono_domain_get (), code);
3490 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3495 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
3496 code = load_function (mono_defaults.corlib->aot_module, symbol);
3502 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3508 MonoAotModule *amodule;
3510 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
3512 /* Save the entries into an array */
3513 buf = mono_domain_alloc (domain, (count + 1) * 2 * sizeof (gpointer));
3514 for (i = 0; i < count; ++i) {
3515 MonoIMTCheckItem *item = imt_entries [i];
3517 g_assert (item->key);
3519 g_assert (!item->has_target_code);
3521 buf [(i * 2)] = item->key;
3522 buf [(i * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
3524 buf [(count * 2)] = NULL;
3525 buf [(count * 2) + 1] = fail_tramp;
3527 amodule->got [got_offset] = buf;
3536 mono_aot_init (void)
3541 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3547 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3553 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3559 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
3565 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
3571 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3577 mono_aot_get_plt_entry (guint8 *code)
3583 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3589 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
3595 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3597 g_assert_not_reached ();
3603 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3605 g_assert_not_reached ();
3610 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3612 g_assert_not_reached ();
3617 mono_aot_get_named_code (const char *name)
3619 g_assert_not_reached ();
3624 mono_aot_get_unbox_trampoline (MonoMethod *method)
3626 g_assert_not_reached ();
3631 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3633 g_assert_not_reached ();
3638 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3640 g_assert_not_reached ();
3645 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3647 g_assert_not_reached ();