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.
9 * Copyright 2003-2011 Novell, Inc.
10 * Copyright 2011 Xamarin, Inc.
14 #include <sys/types.h>
20 #ifdef HAVE_SYS_MMAN_H
29 #ifdef HAVE_EXECINFO_H
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
40 #include <mono/metadata/abi-details.h>
41 #include <mono/metadata/tabledefs.h>
42 #include <mono/metadata/class.h>
43 #include <mono/metadata/object.h>
44 #include <mono/metadata/tokentype.h>
45 #include <mono/metadata/appdomain.h>
46 #include <mono/metadata/debug-helpers.h>
47 #include <mono/metadata/assembly.h>
48 #include <mono/metadata/metadata-internals.h>
49 #include <mono/metadata/marshal.h>
50 #include <mono/metadata/gc-internal.h>
51 #include <mono/metadata/monitor.h>
52 #include <mono/metadata/threads-types.h>
53 #include <mono/metadata/mono-endian.h>
54 #include <mono/utils/mono-logger-internal.h>
55 #include <mono/utils/mono-mmap.h>
56 #include "mono/utils/mono-compiler.h"
57 #include <mono/utils/mono-counters.h>
58 #include <mono/utils/mono-digest.h>
61 #include "seq-points.h"
63 #include "debugger-agent.h"
68 #define ENABLE_AOT_CACHE
71 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
72 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
73 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
80 typedef struct MonoAotModule {
82 /* Pointer to the Global Offset Table */
85 GHashTable *name_cache;
86 GHashTable *extra_methods;
87 /* Maps methods to their code */
88 GHashTable *method_to_code;
89 /* Maps pointers into the method info to the methods themselves */
90 GHashTable *method_ref_to_method;
91 MonoAssemblyName *image_names;
93 MonoAssembly *assembly;
94 MonoImage **image_table;
95 guint32 image_table_len;
100 /* Points to either the start of JIT compiler or LLVM compiled code */
102 guint8 *jit_code_start;
103 guint8 *jit_code_end;
104 guint8 *llvm_code_start;
105 guint8 *llvm_code_end;
109 gint32 *code_offsets;
110 gpointer *method_addresses;
111 /* This contains <offset, index> pairs sorted by offset */
112 /* This is needed because LLVM emitted methods can be in any order */
113 gint32 *sorted_code_offsets;
114 gint32 sorted_code_offsets_len;
115 guint32 *method_info_offsets;
116 guint32 *ex_info_offsets;
117 guint32 *class_info_offsets;
118 guint32 *methods_loaded;
119 guint16 *class_name_table;
120 guint32 *extra_method_table;
121 guint32 *extra_method_info_offsets;
122 guint32 *unbox_trampolines;
123 guint32 *unbox_trampolines_end;
124 guint32 *unbox_trampoline_addresses;
128 /* Points to the mono EH data created by LLVM */
129 guint8 *mono_eh_frame;
131 /* Points to the trampolines */
132 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
133 /* The first unused trampoline of each kind */
134 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
136 gboolean use_page_trampolines;
138 MonoAotFileInfo info;
143 JitInfoMap *async_jit_info_table;
149 unsigned char *trampolines;
150 unsigned char *trampolines_end;
153 static GHashTable *aot_modules;
154 #define mono_aot_lock() mono_mutex_lock (&aot_mutex)
155 #define mono_aot_unlock() mono_mutex_unlock (&aot_mutex)
156 static mono_mutex_t aot_mutex;
159 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
160 * AOT modules registered by mono_aot_register_module ().
162 static GHashTable *static_aot_modules;
165 * Maps MonoJitInfo* to the aot module they belong to, this can be different
166 * from ji->method->klass->image's aot module for generic instances.
168 static GHashTable *ji_to_amodule;
171 * Whenever to AOT compile loaded assemblies on demand and store them in
174 static gboolean enable_aot_cache = FALSE;
176 static gboolean mscorlib_aot_loaded;
179 static gint32 mono_last_aot_method = -1;
181 static gboolean make_unreadable = FALSE;
182 static guint32 name_table_accesses = 0;
183 static guint32 n_pagefaults = 0;
185 /* Used to speed-up find_aot_module () */
186 static gsize aot_code_low_addr = (gssize)-1;
187 static gsize aot_code_high_addr = 0;
190 static gint32 async_jit_info_size;
192 static GHashTable *aot_jit_icall_hash;
195 #define USE_PAGE_TRAMPOLINES ((MonoAotModule*)mono_defaults.corlib->aot_module)->use_page_trampolines
197 #define USE_PAGE_TRAMPOLINES 0
200 #define mono_aot_page_lock() mono_mutex_lock (&aot_page_mutex)
201 #define mono_aot_page_unlock() mono_mutex_unlock (&aot_page_mutex)
202 static mono_mutex_t aot_page_mutex;
205 init_plt (MonoAotModule *info);
208 compute_llvm_code_range (MonoAotModule *amodule, guint8 **code_start, guint8 **code_end);
210 /*****************************************************/
212 /*****************************************************/
215 amodule_lock (MonoAotModule *amodule)
217 mono_mutex_lock (&amodule->mutex);
221 amodule_unlock (MonoAotModule *amodule)
223 mono_mutex_unlock (&amodule->mutex);
229 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
230 * found, and sets the loader error if SET_ERROR is TRUE.
233 load_image (MonoAotModule *amodule, int index, gboolean set_error)
235 MonoAssembly *assembly;
236 MonoImageOpenStatus status;
238 g_assert (index < amodule->image_table_len);
240 if (amodule->image_table [index])
241 return amodule->image_table [index];
242 if (amodule->out_of_date)
245 assembly = mono_assembly_load (&amodule->image_names [index], amodule->assembly->basedir, &status);
247 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: module %s is unusable because dependency %s is not found.\n", amodule->aot_name, amodule->image_names [index].name);
248 amodule->out_of_date = TRUE;
251 char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
252 mono_loader_set_error_assembly_load (full_name, FALSE);
258 if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
259 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: module %s is unusable (GUID of dependent assembly %s doesn't match (expected '%s', got '%s').\n", amodule->aot_name, amodule->image_names [index].name, amodule->image_guids [index], assembly->image->guid);
260 amodule->out_of_date = TRUE;
264 amodule->image_table [index] = assembly->image;
265 return assembly->image;
269 decode_value (guint8 *ptr, guint8 **rptr)
274 if ((b & 0x80) == 0){
277 } else if ((b & 0x40) == 0){
278 len = ((b & 0x3f) << 8 | ptr [1]);
280 } else if (b != 0xff) {
281 len = ((b & 0x1f) << 24) |
288 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
294 //printf ("DECODE: %d.\n", len);
299 * mono_aot_get_offset:
301 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
305 mono_aot_get_offset (guint32 *table, int index)
307 int i, group, ngroups, index_entry_size;
308 int start_offset, offset, group_size;
309 guint8 *data_start, *p;
310 guint32 *index32 = NULL;
311 guint16 *index16 = NULL;
313 /* noffsets = table [0]; */
314 group_size = table [1];
316 index_entry_size = table [3];
317 group = index / group_size;
319 if (index_entry_size == 2) {
320 index16 = (guint16*)&table [4];
321 data_start = (guint8*)&index16 [ngroups];
322 p = data_start + index16 [group];
324 index32 = (guint32*)&table [4];
325 data_start = (guint8*)&index32 [ngroups];
326 p = data_start + index32 [group];
329 /* offset will contain the value of offsets [group * group_size] */
330 offset = start_offset = decode_value (p, &p);
331 for (i = group * group_size + 1; i <= index; ++i) {
332 offset += decode_value (p, &p);
335 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
341 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
344 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
347 decode_type (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
349 static MonoGenericInst*
350 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
353 MonoType **type_argv;
354 MonoGenericInst *inst;
357 type_argc = decode_value (p, &p);
358 type_argv = g_new0 (MonoType*, type_argc);
360 for (i = 0; i < type_argc; ++i) {
361 MonoClass *pclass = decode_klass_ref (module, p, &p);
366 type_argv [i] = &pclass->byval_arg;
369 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
378 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
385 argc = decode_value (p, &p);
388 ctx->class_inst = decode_generic_inst (module, p, &p);
389 if (!ctx->class_inst)
393 argc = decode_value (p, &p);
396 ctx->method_inst = decode_generic_inst (module, p, &p);
397 if (!ctx->method_inst)
406 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
410 MonoClass *klass = NULL, *eklass;
411 guint32 token, rank, idx;
415 reftype = decode_value (p, &p);
422 case MONO_AOT_TYPEREF_TYPEDEF_INDEX:
423 idx = decode_value (p, &p);
424 image = load_image (module, 0, TRUE);
427 klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF + idx, &error);
428 g_assert (mono_error_ok (&error));
430 case MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE:
431 idx = decode_value (p, &p);
432 image = load_image (module, decode_value (p, &p), TRUE);
435 klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF + idx, &error);
436 g_assert (mono_error_ok (&error));
438 case MONO_AOT_TYPEREF_TYPESPEC_TOKEN:
439 token = decode_value (p, &p);
440 image = module->assembly->image;
443 klass = mono_class_get_checked (image, token, &error);
444 g_assert (mono_error_ok (&error));
446 case MONO_AOT_TYPEREF_GINST: {
448 MonoGenericContext ctx;
451 gclass = decode_klass_ref (module, p, &p);
454 g_assert (gclass->generic_container);
456 memset (&ctx, 0, sizeof (ctx));
457 ctx.class_inst = decode_generic_inst (module, p, &p);
460 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
461 klass = mono_class_from_mono_type (type);
462 mono_metadata_free_type (type);
465 case MONO_AOT_TYPEREF_VAR: {
467 MonoGenericContainer *container = NULL;
468 int type = decode_value (p, &p);
469 int num = decode_value (p, &p);
470 gboolean has_container = decode_value (p, &p);
471 MonoType *gshared_constraint = NULL;
472 char *par_name = NULL;
475 gboolean is_method = decode_value (p, &p);
478 MonoMethod *method_def;
479 g_assert (type == MONO_TYPE_MVAR);
480 method_def = decode_resolve_method_ref (module, p, &p);
484 container = mono_method_get_generic_container (method_def);
486 MonoClass *class_def;
487 g_assert (type == MONO_TYPE_VAR);
488 class_def = decode_klass_ref (module, p, &p);
492 container = class_def->generic_container;
495 gboolean has_gshared_constraint = decode_value (p, &p);
496 if (has_gshared_constraint) {
499 gshared_constraint = decode_type (module, p, &p);
500 if (!gshared_constraint)
503 len = decode_value (p, &p);
505 par_name = mono_image_alloc (module->assembly->image, len + 1);
506 memcpy (par_name, p, len);
508 par_name [len] = '\0';
513 t = g_new0 (MonoType, 1);
516 t->data.generic_param = mono_generic_container_get_param (container, num);
517 g_assert (gshared_constraint == NULL);
520 MonoGenericParam *par = (MonoGenericParam*)mono_image_alloc0 (module->assembly->image, sizeof (MonoGenericParamFull));
522 par->gshared_constraint = gshared_constraint;
524 par->image = mono_defaults.corlib;
525 t->data.generic_param = par;
527 ((MonoGenericParamFull*)par)->info.name = par_name;
530 // FIXME: Maybe use types directly to avoid
531 // the overhead of creating MonoClass-es
532 klass = mono_class_from_mono_type (t);
537 case MONO_AOT_TYPEREF_ARRAY:
539 rank = decode_value (p, &p);
540 eklass = decode_klass_ref (module, p, &p);
541 klass = mono_array_class_get (eklass, rank);
543 case MONO_AOT_TYPEREF_PTR: {
546 t = decode_type (module, p, &p);
549 klass = mono_class_from_mono_type (t);
553 case MONO_AOT_TYPEREF_BLOB_INDEX: {
554 guint32 offset = decode_value (p, &p);
557 p2 = module->blob + offset;
558 klass = decode_klass_ref (module, p2, &p2);
562 g_assert_not_reached ();
565 //printf ("BLA: %s\n", mono_type_full_name (&klass->byval_arg));
570 static MonoClassField*
571 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
573 MonoClass *klass = decode_klass_ref (module, buf, &buf);
580 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
584 return mono_class_get_field (klass, token);
588 * Parse a MonoType encoded by encode_type () in aot-compiler.c. Return malloc-ed
592 decode_type (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
597 t = g_malloc0 (sizeof (MonoType));
600 if (*p == MONO_TYPE_PINNED) {
603 } else if (*p == MONO_TYPE_BYREF) {
616 case MONO_TYPE_BOOLEAN:
630 case MONO_TYPE_STRING:
631 case MONO_TYPE_OBJECT:
632 case MONO_TYPE_TYPEDBYREF:
634 case MONO_TYPE_VALUETYPE:
635 case MONO_TYPE_CLASS:
636 t->data.klass = decode_klass_ref (module, p, &p);
638 case MONO_TYPE_SZARRAY:
639 t->data.klass = decode_klass_ref (module, p, &p);
645 t->data.type = decode_type (module, p, &p);
647 case MONO_TYPE_GENERICINST: {
649 MonoGenericContext ctx;
653 gclass = decode_klass_ref (module, p, &p);
656 g_assert (gclass->generic_container);
658 memset (&ctx, 0, sizeof (ctx));
659 ctx.class_inst = decode_generic_inst (module, p, &p);
662 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
663 klass = mono_class_from_mono_type (type);
664 t->data.generic_class = klass->generic_class;
667 case MONO_TYPE_ARRAY: {
668 MonoArrayType *array;
671 // FIXME: memory management
672 array = g_new0 (MonoArrayType, 1);
673 array->eklass = decode_klass_ref (module, p, &p);
676 array->rank = decode_value (p, &p);
677 array->numsizes = decode_value (p, &p);
680 array->sizes = g_malloc0 (sizeof (int) * array->numsizes);
681 for (i = 0; i < array->numsizes; ++i)
682 array->sizes [i] = decode_value (p, &p);
684 array->numlobounds = decode_value (p, &p);
685 if (array->numlobounds)
686 array->lobounds = g_malloc0 (sizeof (int) * array->numlobounds);
687 for (i = 0; i < array->numlobounds; ++i)
688 array->lobounds [i] = decode_value (p, &p);
689 t->data.array = array;
693 case MONO_TYPE_MVAR: {
694 MonoClass *klass = decode_klass_ref (module, p, &p);
697 t->data.generic_param = klass->byval_arg.data.generic_param;
701 g_assert_not_reached ();
709 // FIXME: Error handling, memory management
711 static MonoMethodSignature*
712 decode_signature_with_target (MonoAotModule *module, MonoMethodSignature *target, guint8 *buf, guint8 **endbuf)
714 MonoMethodSignature *sig;
716 int i, param_count, call_conv;
718 gboolean hasthis, explicit_this, has_gen_params;
722 has_gen_params = (flags & 0x10) != 0;
723 hasthis = (flags & 0x20) != 0;
724 explicit_this = (flags & 0x40) != 0;
725 call_conv = flags & 0x0F;
728 /* gen_param_count = */ decode_value (p, &p);
729 param_count = decode_value (p, &p);
730 if (target && param_count != target->param_count)
732 sig = g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE + param_count * sizeof (MonoType *));
733 sig->param_count = param_count;
734 sig->sentinelpos = -1;
735 sig->hasthis = hasthis;
736 sig->explicit_this = explicit_this;
737 sig->call_convention = call_conv;
738 sig->param_count = param_count;
739 sig->ret = decode_type (module, p, &p);
740 for (i = 0; i < param_count; ++i) {
741 if (*p == MONO_TYPE_SENTINEL) {
742 g_assert (sig->call_convention == MONO_CALL_VARARG);
743 sig->sentinelpos = i;
746 sig->params [i] = decode_type (module, p, &p);
749 if (sig->call_convention == MONO_CALL_VARARG && sig->sentinelpos == -1)
750 sig->sentinelpos = sig->param_count;
757 static MonoMethodSignature*
758 decode_signature (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
760 return decode_signature_with_target (module, NULL, buf, endbuf);
764 sig_matches_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
766 MonoMethodSignature *sig;
770 sig = decode_signature_with_target (module, mono_method_signature (target), p, &p);
771 res = sig && mono_metadata_signature_equal (mono_method_signature (target), sig);
777 /* Stores information returned by decode_method_ref () */
782 gboolean no_aot_trampoline;
786 * decode_method_ref_with_target:
788 * Decode a method reference, storing the image/token into a MethodRef structure.
789 * This avoids loading metadata for the method if the caller does not need it. If the method has
790 * no token, then it is loaded from metadata and ref->method is set to the method instance.
791 * If TARGET is non-NULL, abort decoding if it can be determined that the decoded method
792 * couldn't resolve to TARGET, and return FALSE.
793 * There are some kinds of method references which only support a non-null TARGET.
794 * This means that its not possible to decode this into a method, only to check
795 * that the method reference matches a given method. This is normally not a problem
796 * as these wrappers only occur in the extra_methods table, where we already have
797 * a method we want to lookup.
800 decode_method_ref_with_target (MonoAotModule *module, MethodRef *ref, MonoMethod *target, guint8 *buf, guint8 **endbuf)
802 guint32 image_index, value;
803 MonoImage *image = NULL;
806 memset (ref, 0, sizeof (MethodRef));
808 value = decode_value (p, &p);
809 image_index = value >> 24;
811 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
812 ref->no_aot_trampoline = TRUE;
813 value = decode_value (p, &p);
814 image_index = value >> 24;
817 if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
818 if (target && target->wrapper_type)
822 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
823 guint32 wrapper_type;
825 wrapper_type = decode_value (p, &p);
827 if (target && target->wrapper_type != wrapper_type)
831 image = mono_defaults.corlib;
833 switch (wrapper_type) {
834 #ifndef DISABLE_REMOTING
835 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
836 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
840 mono_class_init (m->klass);
841 ref->method = mono_marshal_get_remoting_invoke_with_check (m);
844 case MONO_WRAPPER_PROXY_ISINST: {
845 MonoClass *klass = decode_klass_ref (module, p, &p);
848 ref->method = mono_marshal_get_proxy_cancast (klass);
851 case MONO_WRAPPER_LDFLD:
852 case MONO_WRAPPER_LDFLDA:
853 case MONO_WRAPPER_STFLD:
854 case MONO_WRAPPER_ISINST: {
855 MonoClass *klass = decode_klass_ref (module, p, &p);
858 if (wrapper_type == MONO_WRAPPER_LDFLD)
859 ref->method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
860 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
861 ref->method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
862 else if (wrapper_type == MONO_WRAPPER_STFLD)
863 ref->method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
864 else if (wrapper_type == MONO_WRAPPER_ISINST)
865 ref->method = mono_marshal_get_isinst (klass);
867 g_assert_not_reached ();
870 case MONO_WRAPPER_LDFLD_REMOTE:
871 ref->method = mono_marshal_get_ldfld_remote_wrapper (NULL);
873 case MONO_WRAPPER_STFLD_REMOTE:
874 ref->method = mono_marshal_get_stfld_remote_wrapper (NULL);
877 case MONO_WRAPPER_ALLOC: {
878 int atype = decode_value (p, &p);
880 ref->method = mono_gc_get_managed_allocator_by_type (atype);
882 fprintf (stderr, "Error: No managed allocator, but we need one for AOT.\nAre you using non-standard GC options?\n");
887 case MONO_WRAPPER_WRITE_BARRIER:
888 ref->method = mono_gc_get_write_barrier ();
890 case MONO_WRAPPER_STELEMREF: {
891 int subtype = decode_value (p, &p);
893 if (subtype == WRAPPER_SUBTYPE_NONE) {
894 ref->method = mono_marshal_get_stelemref ();
895 } else if (subtype == WRAPPER_SUBTYPE_VIRTUAL_STELEMREF) {
899 kind = decode_value (p, &p);
901 /* Can't decode this */
904 if (target->wrapper_type == MONO_WRAPPER_STELEMREF) {
905 info = mono_marshal_get_wrapper_info (target);
908 if (info->subtype == subtype && info->d.virtual_stelemref.kind == kind)
909 ref->method = target;
916 g_assert_not_reached ();
920 case MONO_WRAPPER_SYNCHRONIZED: {
921 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
925 ref->method = mono_marshal_get_synchronized_wrapper (m);
928 case MONO_WRAPPER_UNKNOWN: {
929 int subtype = decode_value (p, &p);
931 if (subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE || subtype == WRAPPER_SUBTYPE_STRUCTURE_TO_PTR) {
932 MonoClass *klass = decode_klass_ref (module, p, &p);
939 if (klass != target->klass)
942 if (subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE) {
943 if (strcmp (target->name, "PtrToStructure"))
945 ref->method = mono_marshal_get_ptr_to_struct (klass);
947 if (strcmp (target->name, "StructureToPtr"))
949 ref->method = mono_marshal_get_struct_to_ptr (klass);
951 } else if (subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
952 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
956 ref->method = mono_marshal_get_synchronized_inner_wrapper (m);
957 } else if (subtype == WRAPPER_SUBTYPE_ARRAY_ACCESSOR) {
958 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
962 ref->method = mono_marshal_get_array_accessor_wrapper (m);
963 } else if (subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN) {
964 ref->method = mono_marshal_get_gsharedvt_in_wrapper ();
965 } else if (subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT) {
966 ref->method = mono_marshal_get_gsharedvt_out_wrapper ();
968 g_assert_not_reached ();
972 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
973 int subtype = decode_value (p, &p);
975 if (subtype == WRAPPER_SUBTYPE_ELEMENT_ADDR) {
976 int rank = decode_value (p, &p);
977 int elem_size = decode_value (p, &p);
979 ref->method = mono_marshal_get_array_address (rank, elem_size);
980 } else if (subtype == WRAPPER_SUBTYPE_STRING_CTOR) {
984 m = decode_resolve_method_ref (module, p, &p);
990 g_assert (target->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED);
992 info = mono_marshal_get_wrapper_info (target);
993 if (info && info->subtype == subtype && info->d.string_ctor.method == m)
994 ref->method = target;
1000 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
1002 int subtype = decode_value (p, &p);
1005 if (subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER) {
1010 if (strcmp (target->name, name) != 0)
1012 ref->method = target;
1014 m = decode_resolve_method_ref (module, p, &p);
1019 /* This should only happen when looking for an extra method */
1022 if (mono_marshal_method_from_wrapper (target) == m)
1023 ref->method = target;
1029 case MONO_WRAPPER_CASTCLASS: {
1030 int subtype = decode_value (p, &p);
1032 if (subtype == WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE)
1033 ref->method = mono_marshal_get_castclass_with_cache ();
1034 else if (subtype == WRAPPER_SUBTYPE_ISINST_WITH_CACHE)
1035 ref->method = mono_marshal_get_isinst_with_cache ();
1037 g_assert_not_reached ();
1040 case MONO_WRAPPER_RUNTIME_INVOKE: {
1041 int subtype = decode_value (p, &p);
1046 if (subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC) {
1047 if (strcmp (target->name, "runtime_invoke_dynamic") != 0)
1049 ref->method = target;
1050 } else if (subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT) {
1051 /* Direct wrapper */
1052 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
1056 ref->method = mono_marshal_get_runtime_invoke (m, FALSE);
1057 } else if (subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL) {
1058 /* Virtual direct wrapper */
1059 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
1063 ref->method = mono_marshal_get_runtime_invoke (m, TRUE);
1065 MonoMethodSignature *sig;
1068 sig = decode_signature_with_target (module, NULL, p, &p);
1069 info = mono_marshal_get_wrapper_info (target);
1072 if (info->subtype != subtype)
1074 g_assert (info->d.runtime_invoke.sig);
1075 if (mono_metadata_signature_equal (sig, info->d.runtime_invoke.sig))
1076 ref->method = target;
1082 case MONO_WRAPPER_DELEGATE_INVOKE:
1083 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
1084 case MONO_WRAPPER_DELEGATE_END_INVOKE: {
1085 gboolean is_inflated = decode_value (p, &p);
1086 WrapperSubtype subtype;
1090 MonoMethod *invoke, *wrapper;
1092 klass = decode_klass_ref (module, p, &p);
1096 switch (wrapper_type) {
1097 case MONO_WRAPPER_DELEGATE_INVOKE:
1098 invoke = mono_get_delegate_invoke (klass);
1099 wrapper = mono_marshal_get_delegate_invoke (invoke, NULL);
1101 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
1102 invoke = mono_get_delegate_begin_invoke (klass);
1103 wrapper = mono_marshal_get_delegate_begin_invoke (invoke);
1105 case MONO_WRAPPER_DELEGATE_END_INVOKE:
1106 invoke = mono_get_delegate_end_invoke (klass);
1107 wrapper = mono_marshal_get_delegate_end_invoke (invoke);
1110 g_assert_not_reached ();
1113 if (target && wrapper != target)
1115 ref->method = wrapper;
1118 * These wrappers are associated with a signature, not with a method.
1119 * Since we can't decode them into methods, they need a target method.
1124 if (wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
1127 subtype = decode_value (p, &p);
1128 info = mono_marshal_get_wrapper_info (target);
1130 if (info->subtype != subtype)
1133 if (subtype != WRAPPER_SUBTYPE_NONE)
1137 if (sig_matches_target (module, target, p, &p))
1138 ref->method = target;
1144 case MONO_WRAPPER_NATIVE_TO_MANAGED: {
1148 m = decode_resolve_method_ref (module, p, &p);
1151 klass = decode_klass_ref (module, p, &p);
1154 ref->method = mono_marshal_get_managed_wrapper (m, klass, 0);
1158 g_assert_not_reached ();
1160 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
1161 image_index = decode_value (p, &p);
1162 ref->token = decode_value (p, &p);
1164 image = load_image (module, image_index, TRUE);
1167 } else if (image_index == MONO_AOT_METHODREF_GINST) {
1170 MonoGenericContext ctx;
1173 * These methods do not have a token which resolves them, so we
1174 * resolve them immediately.
1176 klass = decode_klass_ref (module, p, &p);
1180 if (target && target->klass != klass)
1183 image_index = decode_value (p, &p);
1184 ref->token = decode_value (p, &p);
1186 image = load_image (module, image_index, TRUE);
1190 ref->method = mono_get_method_full (image, ref->token, NULL, NULL);
1194 memset (&ctx, 0, sizeof (ctx));
1196 if (FALSE && klass->generic_class) {
1197 ctx.class_inst = klass->generic_class->context.class_inst;
1198 ctx.method_inst = NULL;
1200 ref->method = mono_class_inflate_generic_method_full_checked (ref->method, klass, &ctx, &error);
1201 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
1204 memset (&ctx, 0, sizeof (ctx));
1206 if (!decode_generic_context (module, &ctx, p, &p))
1209 ref->method = mono_class_inflate_generic_method_full_checked (ref->method, klass, &ctx, &error);
1210 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
1211 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
1215 klass = decode_klass_ref (module, p, &p);
1218 method_type = decode_value (p, &p);
1219 switch (method_type) {
1221 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
1224 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
1227 ref->method = mono_class_get_method_from_name (klass, "Get", -1);
1230 ref->method = mono_class_get_method_from_name (klass, "Address", -1);
1233 ref->method = mono_class_get_method_from_name (klass, "Set", -1);
1236 g_assert_not_reached ();
1239 if (image_index == MONO_AOT_METHODREF_LARGE_IMAGE_INDEX) {
1240 image_index = decode_value (p, &p);
1241 value = decode_value (p, &p);
1244 ref->token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1246 image = load_image (module, image_index, TRUE);
1259 decode_method_ref (MonoAotModule *module, MethodRef *ref, guint8 *buf, guint8 **endbuf)
1261 return decode_method_ref_with_target (module, ref, NULL, buf, endbuf);
1265 * decode_resolve_method_ref_with_target:
1267 * Similar to decode_method_ref, but resolve and return the method itself.
1270 decode_resolve_method_ref_with_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
1275 res = decode_method_ref_with_target (module, &ref, target, buf, endbuf);
1282 return mono_get_method (ref.image, ref.token, NULL);
1286 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
1288 return decode_resolve_method_ref_with_target (module, NULL, buf, endbuf);
1291 #ifdef ENABLE_AOT_CACHE
1297 * - Add options for controlling the cache size
1298 * - Handle full cache by deleting old assemblies lru style
1299 * - Maybe add a threshold after an assembly is AOT compiled
1300 * - Add options for enabling this for specific main assemblies
1303 /* The cache directory */
1304 static char *cache_dir;
1306 /* The number of assemblies AOTed in this run */
1307 static int cache_count;
1309 /* Whenever to AOT in-process */
1310 static gboolean in_process;
1313 collect_assemblies (gpointer data, gpointer user_data)
1315 MonoAssembly *ass = data;
1316 GSList **l = user_data;
1318 *l = g_slist_prepend (*l, ass);
1321 #define SHA1_DIGEST_LENGTH 20
1324 * get_aot_config_hash:
1326 * Return a hash for all the version information an AOT module depends on.
1328 static G_GNUC_UNUSED char*
1329 get_aot_config_hash (MonoAssembly *assembly)
1332 GSList *l, *assembly_list = NULL;
1335 guint8 digest [SHA1_DIGEST_LENGTH];
1338 build_info = mono_get_runtime_build_info ();
1340 s = g_string_new (build_info);
1342 mono_assembly_foreach (collect_assemblies, &assembly_list);
1345 * The assembly list includes the current assembly as well, no need
1348 for (l = assembly_list; l; l = l->next) {
1349 MonoAssembly *ass = l->data;
1351 g_string_append (s, "_");
1352 g_string_append (s, ass->aname.name);
1353 g_string_append (s, "_");
1354 g_string_append (s, ass->image->guid);
1357 for (i = 0; i < s->len; ++i) {
1358 if (!isalnum (s->str [i]) && s->str [i] != '-')
1362 mono_sha1_get_digest ((guint8*)s->str, s->len, digest);
1364 digest_str = g_malloc0 ((SHA1_DIGEST_LENGTH * 2) + 1);
1365 for (i = 0; i < SHA1_DIGEST_LENGTH; ++i)
1366 sprintf (digest_str + (i * 2), "%02x", digest [i]);
1368 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: file dependencies: %s, hash %s", s->str, digest_str);
1370 g_string_free (s, TRUE);
1376 aot_cache_init (void)
1380 enable_aot_cache = TRUE;
1385 * aot_cache_load_module:
1387 * Load the AOT image corresponding to ASSEMBLY from the aot cache, AOTing it if neccessary.
1390 aot_cache_load_module (MonoAssembly *assembly, char **aot_name)
1392 MonoAotCacheConfig *config;
1394 char *fname, *tmp2, *aot_options, *failure_fname;
1406 if (image_is_dynamic (assembly->image))
1409 /* Check in the list of assemblies enabled for aot caching */
1410 config = mono_get_aot_cache_config ();
1414 MonoDomain *domain = mono_domain_get ();
1415 MonoAssembly *entry_assembly = domain->entry_assembly;
1417 // FIXME: This cannot be used for mscorlib during startup, since entry_assembly is not set yet
1418 for (l = config->apps; l; l = l->next) {
1421 if ((entry_assembly && !strcmp (entry_assembly->aname.name, n)) || (!entry_assembly && !strcmp (assembly->aname.name, n)))
1429 for (l = config->assemblies; l; l = l->next) {
1432 if (!strcmp (assembly->aname.name, n))
1442 home = g_get_home_dir ();
1445 cache_dir = g_strdup_printf ("%s/Library/Caches/mono/aot-cache", home);
1446 if (!g_file_test (cache_dir, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_DIR))
1447 g_mkdir_with_parents (cache_dir, 0777);
1451 * The same assembly can be used in multiple configurations, i.e. multiple
1452 * versions of the runtime, with multiple versions of dependent assemblies etc.
1453 * To handle this, we compute a version string containing all this information, hash it,
1454 * and use the hash as a filename suffix.
1456 hash = get_aot_config_hash (assembly);
1458 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, hash, MONO_SOLIB_EXT);
1459 fname = g_build_filename (cache_dir, tmp2, NULL);
1463 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: loading from cache: '%s'.", fname);
1464 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
1467 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: found in cache: '%s'.", fname);
1471 if (!strcmp (assembly->aname.name, "mscorlib") && !mscorlib_aot_loaded)
1473 * Can't AOT this during startup, so we AOT it when called later from
1474 * mono_aot_get_method ().
1478 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: not found.");
1480 /* Only AOT one assembly per run to avoid slowing down execution too much */
1481 if (cache_count > 0)
1485 /* Check for previous failure */
1486 failure_fname = g_strdup_printf ("%s.failure", fname);
1487 failure_file = fopen (failure_fname, "r");
1489 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: assembly '%s' previously failed to compile '%s' ('%s')... ", assembly->image->name, fname, failure_fname);
1490 g_free (failure_fname);
1493 g_free (failure_fname);
1494 fclose (failure_file);
1497 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: compiling assembly '%s', logfile: '%s.log'... ", assembly->image->name, fname);
1500 * We need to invoke the AOT compiler here. There are multiple approaches:
1501 * - spawn a new runtime process. This can be hard when running with mkbundle, and
1502 * its hard to make the new process load the same set of assemblies.
1503 * - doing it in-process. This exposes the current process to bugs/leaks/side effects of
1505 * - fork a new process and do the work there.
1508 aot_options = g_strdup_printf ("outfile=%s,internal-logfile=%s.log%s%s", fname, fname, config->aot_options ? "," : "", config->aot_options ? config->aot_options : "");
1509 /* Maybe due this in another thread ? */
1510 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
1512 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: compilation failed.");
1513 failure_fname = g_strdup_printf ("%s.failure", fname);
1514 failure_file = fopen (failure_fname, "a+");
1515 fclose (failure_file);
1516 g_free (failure_fname);
1518 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: compilation succeeded.");
1522 * - Avoid waiting for the aot process to finish ?
1523 * (less overhead, but multiple processes could aot the same assembly at the same time)
1532 logfile_name = g_strdup_printf ("%s/aot.log", cache_dir);
1533 logfile = fopen (logfile_name, "a+");
1534 g_free (logfile_name);
1536 dup2 (fileno (logfile), 1);
1537 dup2 (fileno (logfile), 2);
1539 aot_options = g_strdup_printf ("outfile=%s", fname);
1540 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
1548 waitpid (pid, &exit_status, 0);
1549 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
1550 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: failed.");
1552 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: succeeded.");
1556 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
1564 aot_cache_init (void)
1569 aot_cache_load_module (MonoAssembly *assembly, char **aot_name)
1577 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
1581 guint16 *table, *entry;
1584 char *symbol = (char*)name;
1587 symbol = g_strdup_printf ("_%s", name);
1590 /* The first entry points to the hash */
1591 table = globals [0];
1594 table_size = table [0];
1597 hash = mono_metadata_str_hash (symbol) % table_size;
1599 entry = &table [hash * 2];
1601 /* Search the hash for the index into the globals table */
1603 while (entry [0] != 0) {
1604 guint32 index = entry [0] - 1;
1605 guint32 next = entry [1];
1607 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
1609 if (!strcmp (globals [index * 2], symbol)) {
1610 global_index = index;
1615 entry = &table [next * 2];
1621 if (global_index != -1)
1622 *value = globals [global_index * 2 + 1];
1629 char *err = mono_dl_symbol (module, name, value);
1637 check_usable (MonoAssembly *assembly, MonoAotFileInfo *info, char **out_msg)
1641 gboolean usable = TRUE;
1644 guint32 excluded_cpu_optimizations;
1646 if (strcmp (assembly->image->guid, info->assembly_guid)) {
1647 msg = g_strdup_printf ("doesn't match assembly");
1651 build_info = mono_get_runtime_build_info ();
1652 if (strlen (info->runtime_version) > 0 && strcmp (info->runtime_version, build_info)) {
1653 msg = g_strdup_printf ("compiled against runtime version '%s' while this runtime has version '%s'", info->runtime_version, build_info);
1656 g_free (build_info);
1658 full_aot = info->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1660 if (mono_aot_only && !full_aot) {
1661 msg = g_strdup_printf ("not compiled with --aot=full");
1664 if (!mono_aot_only && full_aot) {
1665 msg = g_strdup_printf ("compiled with --aot=full");
1669 /* mono_arch_find_imt_method () requires this */
1670 if ((info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1671 msg = g_strdup_printf ("compiled against LLVM");
1674 if (!(info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && mono_use_llvm) {
1675 msg = g_strdup_printf ("not compiled against LLVM");
1679 if (mini_get_debug_options ()->mdb_optimizations && !(info->flags & MONO_AOT_FILE_FLAG_DEBUG) && !full_aot) {
1680 msg = g_strdup_printf ("not compiled for debugging");
1684 mono_arch_cpu_optimizations (&excluded_cpu_optimizations);
1685 if (info->opts & excluded_cpu_optimizations) {
1686 msg = g_strdup_printf ("compiled with unsupported CPU optimizations");
1690 if (!mono_aot_only && (info->simd_opts & ~mono_arch_cpu_enumerate_simd_versions ())) {
1691 msg = g_strdup_printf ("compiled with unsupported SIMD extensions");
1697 if (info->gc_name_index != -1) {
1698 char *gc_name = (char*)&blob [info->gc_name_index];
1699 const char *current_gc_name = mono_gc_get_gc_name ();
1701 if (strcmp (current_gc_name, gc_name) != 0) {
1702 msg = g_strdup_printf ("compiled against GC %s, while the current runtime uses GC %s.\n", gc_name, current_gc_name);
1712 * TABLE should point to a table of call instructions. Return the address called by the INDEXth entry.
1715 get_call_table_entry (void *table, int index)
1717 #if defined(TARGET_ARM)
1722 ins_addr = (guint32*)table + index;
1724 if ((ins >> ARMCOND_SHIFT) == ARMCOND_NV) {
1726 offset = (((int)(((ins & 0xffffff) << 1) | ((ins >> 24) & 0x1))) << 7) >> 7;
1727 return (char*)ins_addr + (offset * 2) + 8 + 1;
1729 offset = (((int)ins & 0xffffff) << 8) >> 8;
1730 return (char*)ins_addr + (offset * 4) + 8;
1732 #elif defined(TARGET_ARM64)
1733 return mono_arch_get_call_target ((guint8*)table + (index * 4) + 4);
1734 #elif defined(TARGET_X86) || defined(TARGET_AMD64)
1735 /* The callee expects an ip which points after the call */
1736 return mono_arch_get_call_target ((guint8*)table + (index * 5) + 5);
1738 g_assert_not_reached ();
1744 load_aot_module (MonoAssembly *assembly, gpointer user_data)
1747 MonoAotModule *amodule;
1749 gboolean usable = TRUE;
1750 char *version_symbol = NULL;
1752 gpointer *globals = NULL;
1753 MonoAotFileInfo *info = NULL;
1756 gboolean do_load_image = TRUE;
1757 int align_double, align_int64;
1759 if (mono_compile_aot)
1762 if (assembly->image->aot_module)
1764 * Already loaded. This can happen because the assembly loading code might invoke
1765 * the assembly load hooks multiple times for the same assembly.
1769 if (image_is_dynamic (assembly->image) || assembly->ref_only)
1772 if (mono_security_cas_enabled ())
1776 if (static_aot_modules)
1777 info = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
1785 /* Statically linked AOT module */
1786 aot_name = g_strdup_printf ("%s", assembly->aname.name);
1787 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
1788 globals = info->globals;
1790 if (enable_aot_cache)
1791 sofile = aot_cache_load_module (assembly, &aot_name);
1794 aot_name = g_strdup_printf ("%s%s", assembly->image->name, MONO_SOLIB_EXT);
1796 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1799 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module '%s' not found: %s\n", aot_name, err);
1802 aot_name = g_strdup_printf ("%s/mono/aot-cache/%s/%s%s", mono_assembly_getrootdir(), ARCHITECTURE, g_path_get_basename (assembly->image->name), MONO_SOLIB_EXT);
1803 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1805 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module '%s' not found: %s\n", aot_name, err);
1813 if (!sofile && !globals) {
1814 if (mono_aot_only && assembly->image->tables [MONO_TABLE_METHOD].rows) {
1815 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1823 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &version_symbol);
1824 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&info);
1827 if (version_symbol) {
1828 /* Old file format */
1829 version = atoi (version_symbol);
1832 version = info->version;
1835 if (version != MONO_AOT_FILE_VERSION) {
1836 msg = g_strdup_printf ("wrong file format version (expected %d got %d)", MONO_AOT_FILE_VERSION, version);
1839 usable = check_usable (assembly, info, &msg);
1843 if (mono_aot_only) {
1844 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode: %s.\n", aot_name, msg);
1847 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: module %s is unusable: %s.\n", aot_name, msg);
1852 mono_dl_close (sofile);
1853 assembly->image->aot_module = NULL;
1857 #if defined (TARGET_ARM) && defined (TARGET_MACH)
1862 memset (&t, 0, sizeof (MonoType));
1863 t.type = MONO_TYPE_R8;
1864 mono_type_size (&t, &align);
1865 align_double = align;
1867 memset (&t, 0, sizeof (MonoType));
1868 t.type = MONO_TYPE_I8;
1869 align_int64 = align;
1872 align_double = MONO_ABI_ALIGNOF (double);
1873 align_int64 = MONO_ABI_ALIGNOF (gint64);
1877 g_assert (info->double_align == align_double);
1878 g_assert (info->long_align == align_int64);
1879 g_assert (info->generic_tramp_num == MONO_TRAMPOLINE_NUM);
1883 amodule = g_new0 (MonoAotModule, 1);
1884 amodule->aot_name = aot_name;
1885 amodule->assembly = assembly;
1887 memcpy (&amodule->info, info, sizeof (*info));
1889 amodule->got = amodule->info.got;
1890 amodule->llvm_got = amodule->info.llvm_got;
1891 amodule->globals = globals;
1892 amodule->sofile = sofile;
1893 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1894 amodule->blob = blob;
1896 mono_mutex_init_recursive (&amodule->mutex);
1898 /* Read image table */
1900 guint32 table_len, i;
1903 table = info->image_table;
1906 table_len = *(guint32*)table;
1907 table += sizeof (guint32);
1908 amodule->image_table = g_new0 (MonoImage*, table_len);
1909 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1910 amodule->image_guids = g_new0 (char*, table_len);
1911 amodule->image_table_len = table_len;
1912 for (i = 0; i < table_len; ++i) {
1913 MonoAssemblyName *aname = &(amodule->image_names [i]);
1915 aname->name = g_strdup (table);
1916 table += strlen (table) + 1;
1917 amodule->image_guids [i] = g_strdup (table);
1918 table += strlen (table) + 1;
1920 aname->culture = g_strdup (table);
1921 table += strlen (table) + 1;
1922 memcpy (aname->public_key_token, table, strlen (table) + 1);
1923 table += strlen (table) + 1;
1925 table = ALIGN_PTR_TO (table, 8);
1926 aname->flags = *(guint32*)table;
1928 aname->major = *(guint32*)table;
1930 aname->minor = *(guint32*)table;
1932 aname->build = *(guint32*)table;
1934 aname->revision = *(guint32*)table;
1939 amodule->method_addresses = info->method_addresses;
1940 amodule->code = info->methods;
1942 /* Mask out thumb interop bit */
1943 amodule->code = (void*)((mgreg_t)amodule->code & ~1);
1945 amodule->jit_code_start = info->jit_code_start;
1946 amodule->jit_code_end = info->jit_code_end;
1947 amodule->method_info_offsets = info->method_info_offsets;
1948 amodule->ex_info_offsets = info->ex_info_offsets;
1949 amodule->class_info_offsets = info->class_info_offsets;
1950 amodule->class_name_table = info->class_name_table;
1951 amodule->extra_method_table = info->extra_method_table;
1952 amodule->extra_method_info_offsets = info->extra_method_info_offsets;
1953 amodule->unbox_trampolines = info->unbox_trampolines;
1954 amodule->unbox_trampolines_end = info->unbox_trampolines_end;
1955 amodule->unbox_trampoline_addresses = info->unbox_trampoline_addresses;
1956 amodule->unwind_info = info->unwind_info;
1957 amodule->mem_end = info->mem_end;
1958 amodule->mem_begin = amodule->code;
1959 amodule->plt = info->plt;
1960 amodule->plt_end = info->plt_end;
1961 amodule->mono_eh_frame = info->mono_eh_frame;
1962 amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC] = info->specific_trampolines;
1963 amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = info->static_rgctx_trampolines;
1964 amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK] = info->imt_thunks;
1965 amodule->trampolines [MONO_AOT_TRAMP_GSHAREDVT_ARG] = info->gsharedvt_arg_trampolines;
1966 amodule->thumb_end = info->thumb_end;
1968 /* Compute code_offsets from the method addresses */
1969 amodule->code_offsets = g_malloc0 (amodule->info.nmethods * sizeof (gint32));
1970 for (i = 0; i < amodule->info.nmethods; ++i) {
1971 /* method_addresses () contains a table of branches, since the ios linker can update those correctly */
1974 addr = get_call_table_entry (amodule->method_addresses, i);
1976 if (addr == amodule->method_addresses)
1977 amodule->code_offsets [i] = 0xffffffff;
1979 amodule->code_offsets [i] = (char*)addr - (char*)amodule->code;
1982 if (make_unreadable) {
1983 #ifndef TARGET_WIN32
1985 guint8 *page_start, *page_end;
1988 addr = amodule->mem_begin;
1989 len = amodule->mem_end - amodule->mem_begin;
1991 /* Round down in both directions to avoid modifying data which is not ours */
1992 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1993 page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
1994 if (page_end > page_start) {
1995 err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
1996 g_assert (err == 0);
2001 /* Compute the boundaries of LLVM code */
2002 if (info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM)
2003 compute_llvm_code_range (amodule, &amodule->llvm_code_start, &amodule->llvm_code_end);
2007 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->jit_code_start);
2008 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->jit_code_end);
2009 if (amodule->llvm_code_start) {
2010 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->llvm_code_start);
2011 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->llvm_code_end);
2014 g_hash_table_insert (aot_modules, assembly, amodule);
2017 mono_jit_info_add_aot_module (assembly->image, amodule->jit_code_start, amodule->jit_code_end);
2018 if (amodule->llvm_code_start)
2019 mono_jit_info_add_aot_module (assembly->image, amodule->llvm_code_start, amodule->llvm_code_end);
2021 assembly->image->aot_module = amodule;
2023 amodule->got [0] = assembly->image;
2025 if (mono_aot_only) {
2027 find_symbol (amodule->sofile, amodule->globals, "specific_trampolines_page", (gpointer *)&code);
2028 amodule->use_page_trampolines = code != NULL;
2029 /*g_warning ("using page trampolines: %d", amodule->use_page_trampolines);*/
2030 if (mono_defaults.corlib) {
2031 /* The second got slot contains the mscorlib got addr */
2032 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
2034 amodule->got [1] = mscorlib_amodule->got;
2036 amodule->got [1] = amodule->got;
2040 if (mono_gc_is_moving ()) {
2043 memset (&ji, 0, sizeof (ji));
2044 ji.type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
2045 amodule->got [2] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
2047 memset (&ji, 0, sizeof (ji));
2048 ji.type = MONO_PATCH_INFO_GC_NURSERY_START;
2049 amodule->got [3] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
2052 if (amodule->llvm_got) {
2053 amodule->llvm_got [0] = amodule->got [0];
2054 amodule->llvm_got [1] = amodule->got [1];
2055 amodule->llvm_got [2] = amodule->got [2];
2056 amodule->llvm_got [3] = amodule->got [3];
2060 * Since we store methoddef and classdef tokens when referring to methods/classes in
2061 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
2062 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
2063 * non-lazily, since we can't handle out-of-date errors later.
2064 * The cached class info also depends on the exact assemblies.
2066 #if defined(__native_client__)
2067 /* TODO: Don't 'load_image' on mscorlib due to a */
2068 /* recursive loading problem. This should be */
2069 /* removed if mscorlib is loaded from disk. */
2070 if (strncmp(assembly->aname.name, "mscorlib", 8)) {
2071 do_load_image = TRUE;
2073 do_load_image = FALSE;
2076 if (do_load_image) {
2077 for (i = 0; i < amodule->image_table_len; ++i)
2078 load_image (amodule, i, FALSE);
2081 if (amodule->out_of_date) {
2082 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);
2083 if (mono_aot_only) {
2084 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);
2089 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: loaded AOT Module for %s.\n", assembly->image->name);
2093 * mono_aot_register_globals:
2095 * This is called by the ctor function in AOT images compiled with the
2096 * 'no-dlsym' option.
2099 mono_aot_register_globals (gpointer *globals)
2101 g_assert_not_reached ();
2105 * mono_aot_register_module:
2107 * This should be called by embedding code to register AOT modules statically linked
2108 * into the executable. AOT_INFO should be the value of the
2109 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
2112 mono_aot_register_module (gpointer *aot_info)
2116 MonoAotFileInfo *info = (gpointer)aot_info;
2118 g_assert (info->version == MONO_AOT_FILE_VERSION);
2120 globals = info->globals;
2123 aname = info->assembly_name;
2125 /* This could be called before startup */
2129 if (!static_aot_modules)
2130 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
2132 g_hash_table_insert (static_aot_modules, aname, info);
2139 mono_aot_init (void)
2141 mono_mutex_init_recursive (&aot_mutex);
2142 mono_mutex_init_recursive (&aot_page_mutex);
2143 aot_modules = g_hash_table_new (NULL, NULL);
2145 #ifndef __native_client__
2146 mono_install_assembly_load_hook (load_aot_module, NULL);
2148 mono_counters_register ("Async JIT info size", MONO_COUNTER_INT|MONO_COUNTER_JIT, &async_jit_info_size);
2150 if (g_getenv ("MONO_LASTAOT"))
2151 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
2156 mono_aot_cleanup (void)
2158 if (aot_jit_icall_hash)
2159 g_hash_table_destroy (aot_jit_icall_hash);
2161 g_hash_table_destroy (aot_modules);
2165 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
2171 info->vtable_size = decode_value (buf, &buf);
2172 if (info->vtable_size == -1)
2175 flags = decode_value (buf, &buf);
2176 info->ghcimpl = (flags >> 0) & 0x1;
2177 info->has_finalize = (flags >> 1) & 0x1;
2178 info->has_cctor = (flags >> 2) & 0x1;
2179 info->has_nested_classes = (flags >> 3) & 0x1;
2180 info->blittable = (flags >> 4) & 0x1;
2181 info->has_references = (flags >> 5) & 0x1;
2182 info->has_static_refs = (flags >> 6) & 0x1;
2183 info->no_special_static_fields = (flags >> 7) & 0x1;
2184 info->is_generic_container = (flags >> 8) & 0x1;
2186 if (info->has_cctor) {
2187 res = decode_method_ref (module, &ref, buf, &buf);
2190 info->cctor_token = ref.token;
2192 if (info->has_finalize) {
2193 res = decode_method_ref (module, &ref, buf, &buf);
2196 info->finalize_image = ref.image;
2197 info->finalize_token = ref.token;
2200 info->instance_size = decode_value (buf, &buf);
2201 info->class_size = decode_value (buf, &buf);
2202 info->packing_size = decode_value (buf, &buf);
2203 info->min_align = decode_value (buf, &buf);
2211 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2214 MonoClass *klass = vtable->klass;
2215 MonoAotModule *amodule = klass->image->aot_module;
2217 MonoCachedClassInfo class_info;
2222 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !amodule)
2225 info = &amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
2228 err = decode_cached_class_info (amodule, &class_info, p, &p);
2232 for (i = 0; i < slot; ++i)
2233 decode_method_ref (amodule, &ref, p, &p);
2235 res = decode_method_ref (amodule, &ref, p, &p);
2238 if (ref.no_aot_trampoline)
2241 if (mono_metadata_token_index (ref.token) == 0 || mono_metadata_token_table (ref.token) != MONO_TABLE_METHOD)
2244 return mono_aot_get_method_from_token (domain, ref.image, ref.token);
2248 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2250 MonoAotModule *amodule = klass->image->aot_module;
2254 if (klass->rank || !amodule)
2257 p = (guint8*)&amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
2259 err = decode_cached_class_info (amodule, res, p, &p);
2267 * mono_aot_get_class_from_name:
2269 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
2270 * using a cache stored in the AOT file.
2271 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
2273 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
2277 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2279 MonoAotModule *amodule = image->aot_module;
2280 guint16 *table, *entry;
2283 char full_name_buf [1024];
2285 const char *name2, *name_space2;
2287 guint32 cols [MONO_TYPEDEF_SIZE];
2288 GHashTable *nspace_table;
2290 if (!amodule || !amodule->class_name_table)
2293 amodule_lock (amodule);
2297 /* First look in the cache */
2298 if (!amodule->name_cache)
2299 amodule->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
2300 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
2302 *klass = g_hash_table_lookup (nspace_table, name);
2304 amodule_unlock (amodule);
2309 table_size = amodule->class_name_table [0];
2310 table = amodule->class_name_table + 1;
2312 if (name_space [0] == '\0')
2313 full_name = g_strdup_printf ("%s", name);
2315 if (strlen (name_space) + strlen (name) < 1000) {
2316 sprintf (full_name_buf, "%s.%s", name_space, name);
2317 full_name = full_name_buf;
2319 full_name = g_strdup_printf ("%s.%s", name_space, name);
2322 hash = mono_metadata_str_hash (full_name) % table_size;
2323 if (full_name != full_name_buf)
2326 entry = &table [hash * 2];
2328 if (entry [0] != 0) {
2329 t = &image->tables [MONO_TABLE_TYPEDEF];
2332 guint32 index = entry [0];
2333 guint32 next = entry [1];
2334 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
2336 name_table_accesses ++;
2338 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
2340 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2341 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2343 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
2345 amodule_unlock (amodule);
2346 *klass = mono_class_get_checked (image, token, &error);
2347 if (!mono_error_ok (&error))
2348 mono_error_cleanup (&error); /* FIXME don't swallow the error */
2352 amodule_lock (amodule);
2353 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
2354 if (!nspace_table) {
2355 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
2356 g_hash_table_insert (amodule->name_cache, (char*)name_space2, nspace_table);
2358 g_hash_table_insert (nspace_table, (char*)name2, *klass);
2359 amodule_unlock (amodule);
2365 entry = &table [next * 2];
2372 amodule_unlock (amodule);
2377 /* Compute the boundaries of the LLVM code for AMODULE. */
2379 compute_llvm_code_range (MonoAotModule *amodule, guint8 **code_start, guint8 **code_end)
2382 int version, fde_count;
2385 g_assert (amodule->mono_eh_frame);
2387 p = amodule->mono_eh_frame;
2389 /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
2393 g_assert (version == 3);
2396 p = ALIGN_PTR_TO (p, 4);
2398 fde_count = *(guint32*)p;
2402 if (fde_count > 1) {
2403 /* mono_aot_personality () */
2404 g_assert (table [0] == -1);
2405 *code_start = amodule->code + amodule->code_offsets [table [2]];
2406 *code_end = amodule->code + amodule->code_offsets [table [(fde_count - 1) * 2]] + table [fde_count * 2];
2414 * decode_mono_eh_frame:
2416 * Decode the EH information emitted by our modified LLVM compiler and construct a
2417 * MonoJitInfo structure from it.
2418 * LOCKING: Acquires the domain lock.
2421 decode_llvm_mono_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
2422 MonoMethod *method, guint8 *code,
2423 MonoJitExceptionInfo *clauses, int num_clauses,
2424 MonoJitInfoFlags flags,
2426 int *this_reg, int *this_offset)
2429 guint8 *fde, *cie, *code_start, *code_end;
2430 int version, fde_count;
2432 int i, j, pos, left, right, offset, offset1, offset2, code_len;
2433 MonoJitExceptionInfo *ei;
2434 guint32 fde_len, ei_len, nested_len, nindex;
2435 gpointer *type_info;
2437 MonoLLVMFDEInfo info;
2439 g_assert (amodule->mono_eh_frame);
2441 p = amodule->mono_eh_frame;
2443 /* p points to data emitted by LLVM in DwarfMonoException::EmitMonoEHFrame () */
2447 g_assert (version == 3);
2449 /* func_encoding = *p; */
2451 p = ALIGN_PTR_TO (p, 4);
2453 fde_count = *(guint32*)p;
2457 /* There is +1 entry in the table */
2458 cie = p + ((fde_count + 1) * 8);
2460 /* Binary search in the table to find the entry for code */
2461 offset = code - amodule->code;
2465 pos = (left + right) / 2;
2467 /* The table contains method index/fde offset pairs */
2468 g_assert (table [(pos * 2)] != -1);
2469 offset1 = amodule->code_offsets [table [(pos * 2)]];
2470 if (pos + 1 == fde_count) {
2471 offset2 = amodule->llvm_code_end - amodule->llvm_code_start;
2473 g_assert (table [(pos + 1) * 2] != -1);
2474 offset2 = amodule->code_offsets [table [(pos + 1) * 2]];
2477 if (offset < offset1)
2479 else if (offset >= offset2)
2485 code_start = amodule->code + amodule->code_offsets [table [(pos * 2)]];
2486 if (pos + 1 == fde_count) {
2487 /* The +1 entry in the table contains the length of the last method */
2488 int len = table [(pos + 1) * 2];
2489 code_end = code_start + len;
2491 code_end = amodule->code + amodule->code_offsets [table [(pos + 1) * 2]];
2493 code_len = code_end - code_start;
2495 g_assert (code >= code_start && code < code_end);
2497 if (amodule->thumb_end && (guint8*)code_start < amodule->thumb_end)
2498 /* Clear thumb flag */
2499 code_start = (guint8*)(((mgreg_t)code_start) & ~1);
2501 fde = amodule->mono_eh_frame + table [(pos * 2) + 1];
2502 /* This won't overflow because there is +1 entry in the table */
2503 fde_len = table [(pos * 2) + 2 + 1] - table [(pos * 2) + 1];
2505 mono_unwind_decode_llvm_mono_fde (fde, fde_len, cie, code_start, &info);
2507 ei_len = info.ex_info_len;
2508 type_info = info.type_info;
2509 *this_reg = info.this_reg;
2510 *this_offset = info.this_offset;
2512 /* Count number of nested clauses */
2514 for (i = 0; i < ei_len; ++i) {
2515 /* This might be unaligned */
2516 gint32 cindex1 = read32 (type_info [i]);
2519 for (l = nesting [cindex1]; l; l = l->next) {
2520 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
2522 for (j = 0; j < ei_len; ++j) {
2523 gint32 cindex2 = read32 (type_info [j]);
2525 if (cindex2 == nesting_cindex)
2532 * LLVM might represent one IL region with multiple regions, so have to
2533 * allocate a new JI.
2536 mono_domain_alloc0_lock_free (domain, mono_jit_info_size (flags, ei_len + nested_len, 0));
2537 mono_jit_info_init (jinfo, method, code, code_len, flags, ei_len + nested_len, 0);
2539 jinfo->unwind_info = mono_cache_unwind_info (info.unw_info, info.unw_info_len);
2540 /* This signals that unwind_info points to a normal cached unwind info */
2541 jinfo->from_aot = 0;
2542 jinfo->from_llvm = 1;
2544 for (i = 0; i < ei_len; ++i) {
2546 * clauses contains the original IL exception info saved by the AOT
2547 * compiler, we have to combine that with the information produced by LLVM
2549 /* The type_info entries contain IL clause indexes */
2550 int clause_index = read32 (type_info [i]);
2551 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
2552 MonoJitExceptionInfo *orig_jei = &clauses [clause_index];
2554 g_assert (clause_index < num_clauses);
2555 jei->flags = orig_jei->flags;
2556 jei->data.catch_class = orig_jei->data.catch_class;
2558 jei->try_start = ei [i].try_start;
2559 jei->try_end = ei [i].try_end;
2560 jei->handler_start = ei [i].handler_start;
2561 jei->clause_index = clause_index;
2563 /* Make sure we transition to thumb when a handler starts */
2564 if (amodule->thumb_end && (guint8*)jei->handler_start < amodule->thumb_end)
2565 jei->handler_start = (void*)((mgreg_t)jei->handler_start + 1);
2568 /* See exception_cb () in mini-llvm.c as to why this is needed */
2570 for (i = 0; i < ei_len; ++i) {
2571 gint32 cindex1 = read32 (type_info [i]);
2574 for (l = nesting [cindex1]; l; l = l->next) {
2575 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
2577 for (j = 0; j < ei_len; ++j) {
2578 gint32 cindex2 = read32 (type_info [j]);
2580 if (cindex2 == nesting_cindex) {
2581 memcpy (&jinfo->clauses [nindex], &jinfo->clauses [j], sizeof (MonoJitExceptionInfo));
2582 jinfo->clauses [nindex].try_start = jinfo->clauses [i].try_start;
2583 jinfo->clauses [nindex].try_end = jinfo->clauses [i].try_end;
2584 jinfo->clauses [nindex].handler_start = jinfo->clauses [i].handler_start;
2585 jinfo->clauses [nindex].exvar_offset = jinfo->clauses [i].exvar_offset;
2591 g_assert (nindex == ei_len + nested_len);
2597 alloc0_jit_info_data (MonoDomain *domain, int size, gboolean async_context)
2601 if (async_context) {
2602 res = mono_domain_alloc0_lock_free (domain, size);
2603 InterlockedExchangeAdd (&async_jit_info_size, size);
2605 res = mono_domain_alloc0 (domain, size);
2611 * LOCKING: Acquires the domain lock.
2612 * In async context, this is async safe.
2615 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
2616 MonoMethod *method, guint8* ex_info, guint8 *addr,
2617 guint8 *code, guint32 code_len)
2619 int i, buf_len, num_clauses, len;
2621 MonoJitInfoFlags flags = JIT_INFO_NONE;
2622 guint unwind_info, eflags;
2623 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points, has_try_block_holes, has_arch_eh_jit_info;
2624 gboolean from_llvm, has_gc_map;
2626 int try_holes_info_size, num_holes;
2627 int this_reg = 0, this_offset = 0;
2630 /* Load the method info from the AOT file */
2631 async = mono_thread_info_is_async_context ();
2634 eflags = decode_value (p, &p);
2635 has_generic_jit_info = (eflags & 1) != 0;
2636 has_dwarf_unwind_info = (eflags & 2) != 0;
2637 has_clauses = (eflags & 4) != 0;
2638 has_seq_points = (eflags & 8) != 0;
2639 from_llvm = (eflags & 16) != 0;
2640 has_try_block_holes = (eflags & 32) != 0;
2641 has_gc_map = (eflags & 64) != 0;
2642 has_arch_eh_jit_info = (eflags & 128) != 0;
2644 if (has_dwarf_unwind_info) {
2645 unwind_info = decode_value (p, &p);
2646 g_assert (unwind_info < (1 << 30));
2648 unwind_info = decode_value (p, &p);
2650 if (has_generic_jit_info)
2651 flags |= JIT_INFO_HAS_GENERIC_JIT_INFO;
2653 if (has_try_block_holes) {
2654 num_holes = decode_value (p, &p);
2655 flags |= JIT_INFO_HAS_TRY_BLOCK_HOLES;
2656 try_holes_info_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
2658 num_holes = try_holes_info_size = 0;
2661 if (has_arch_eh_jit_info) {
2662 flags |= JIT_INFO_HAS_ARCH_EH_INFO;
2663 /* Overwrite the original code_len which includes alignment padding */
2664 code_len = decode_value (p, &p);
2667 /* Exception table */
2669 num_clauses = decode_value (p, &p);
2674 MonoJitExceptionInfo *clauses;
2681 * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
2684 clauses = g_new0 (MonoJitExceptionInfo, num_clauses);
2685 nesting = g_new0 (GSList*, num_clauses);
2687 for (i = 0; i < num_clauses; ++i) {
2688 MonoJitExceptionInfo *ei = &clauses [i];
2690 ei->flags = decode_value (p, &p);
2692 if (decode_value (p, &p))
2693 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
2695 /* Read the list of nesting clauses */
2697 int nesting_index = decode_value (p, &p);
2698 if (nesting_index == -1)
2700 nesting [i] = g_slist_prepend (nesting [i], GINT_TO_POINTER (nesting_index));
2704 jinfo = decode_llvm_mono_eh_frame (amodule, domain, method, code, clauses, num_clauses, flags, nesting, &this_reg, &this_offset);
2707 for (i = 0; i < num_clauses; ++i)
2708 g_slist_free (nesting [i]);
2711 len = mono_jit_info_size (flags, num_clauses, num_holes);
2712 jinfo = alloc0_jit_info_data (domain, len, async);
2713 mono_jit_info_init (jinfo, method, code, code_len, flags, num_clauses, num_holes);
2715 for (i = 0; i < jinfo->num_clauses; ++i) {
2716 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
2718 ei->flags = decode_value (p, &p);
2720 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
2721 /* Not used for catch clauses */
2722 if (ei->flags != MONO_EXCEPTION_CLAUSE_NONE)
2723 ei->exvar_offset = decode_value (p, &p);
2725 ei->exvar_offset = decode_value (p, &p);
2728 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
2729 ei->data.filter = code + decode_value (p, &p);
2731 int len = decode_value (p, &p);
2737 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
2741 ei->try_start = code + decode_value (p, &p);
2742 ei->try_end = code + decode_value (p, &p);
2743 ei->handler_start = code + decode_value (p, &p);
2746 jinfo->unwind_info = unwind_info;
2747 jinfo->domain_neutral = 0;
2748 jinfo->from_aot = 1;
2751 if (has_try_block_holes) {
2752 MonoTryBlockHoleTableJitInfo *table;
2754 g_assert (jinfo->has_try_block_holes);
2756 table = mono_jit_info_get_try_block_hole_table_info (jinfo);
2759 table->num_holes = (guint16)num_holes;
2760 for (i = 0; i < num_holes; ++i) {
2761 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
2762 hole->clause = decode_value (p, &p);
2763 hole->length = decode_value (p, &p);
2764 hole->offset = decode_value (p, &p);
2768 if (has_arch_eh_jit_info) {
2769 MonoArchEHJitInfo *eh_info;
2771 g_assert (jinfo->has_arch_eh_info);
2773 eh_info = mono_jit_info_get_arch_eh_info (jinfo);
2774 eh_info->stack_size = decode_value (p, &p);
2775 eh_info->epilog_size = decode_value (p, &p);
2779 /* The rest is not needed in async mode */
2780 jinfo->async = TRUE;
2781 jinfo->d.aot_info = amodule;
2786 if (has_generic_jit_info) {
2787 MonoGenericJitInfo *gi;
2790 g_assert (jinfo->has_generic_jit_info);
2792 gi = mono_jit_info_get_generic_jit_info (jinfo);
2795 gi->nlocs = decode_value (p, &p);
2797 gi->locations = alloc0_jit_info_data (domain, gi->nlocs * sizeof (MonoDwarfLocListEntry), async);
2798 for (i = 0; i < gi->nlocs; ++i) {
2799 MonoDwarfLocListEntry *entry = &gi->locations [i];
2801 entry->is_reg = decode_value (p, &p);
2802 entry->reg = decode_value (p, &p);
2804 entry->offset = decode_value (p, &p);
2806 entry->from = decode_value (p, &p);
2807 entry->to = decode_value (p, &p);
2812 gi->has_this = this_reg != -1;
2813 gi->this_reg = this_reg;
2814 gi->this_offset = this_offset;
2816 gi->has_this = decode_value (p, &p);
2817 gi->this_reg = decode_value (p, &p);
2818 gi->this_offset = decode_value (p, &p);
2822 len = decode_value (p, &p);
2826 jinfo->d.method = decode_resolve_method_ref (amodule, p, &p);
2828 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
2829 if (decode_value (p, &p)) {
2832 MonoGenericSharingContext *gsctx = gi->generic_sharing_context;
2834 n = decode_value (p, &p);
2836 gsctx->var_is_vt = alloc0_jit_info_data (domain, sizeof (gboolean) * n, async);
2837 for (i = 0; i < n; ++i)
2838 gsctx->var_is_vt [i] = decode_value (p, &p);
2840 n = decode_value (p, &p);
2842 gsctx->mvar_is_vt = alloc0_jit_info_data (domain, sizeof (gboolean) * n, async);
2843 for (i = 0; i < n; ++i)
2844 gsctx->mvar_is_vt [i] = decode_value (p, &p);
2849 if (method && has_seq_points) {
2850 MonoSeqPointInfo *seq_points;
2852 p += seq_point_info_read (&seq_points, p, FALSE);
2854 mono_domain_lock (domain);
2855 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
2856 mono_domain_unlock (domain);
2859 /* Load debug info */
2860 buf_len = decode_value (p, &p);
2862 mono_debug_add_aot_method (domain, method, code, p, buf_len);
2866 int map_size = decode_value (p, &p);
2867 /* The GC map requires 4 bytes of alignment */
2868 while ((guint64)(gsize)p % 4)
2874 if (amodule != jinfo->d.method->klass->image->aot_module) {
2877 ji_to_amodule = g_hash_table_new (NULL, NULL);
2878 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
2886 amodule_contains_code_addr (MonoAotModule *amodule, guint8 *code)
2888 return (code >= amodule->jit_code_start && code <= amodule->jit_code_end) ||
2889 (code >= amodule->llvm_code_start && code <= amodule->llvm_code_end);
2893 * mono_aot_get_unwind_info:
2895 * Return a pointer to the DWARF unwind info belonging to JI.
2898 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2900 MonoAotModule *amodule;
2902 guint8 *code = ji->code_start;
2905 amodule = ji->d.aot_info;
2907 amodule = jinfo_get_method (ji)->klass->image->aot_module;
2909 g_assert (ji->from_aot);
2911 if (!amodule_contains_code_addr (amodule, code)) {
2912 /* ji belongs to a different aot module than amodule */
2914 g_assert (ji_to_amodule);
2915 amodule = g_hash_table_lookup (ji_to_amodule, ji);
2917 g_assert (amodule_contains_code_addr (amodule, code));
2921 p = amodule->unwind_info + ji->unwind_info;
2922 *unwind_info_len = decode_value (p, &p);
2926 static G_GNUC_UNUSED int
2927 compare_ints (const void *a, const void *b)
2929 return *(gint32*)a - *(gint32*)b;
2933 msort_code_offsets_internal (gint32 *array, int lo, int hi, gint32 *scratch)
2935 int mid = (lo + hi) / 2;
2942 for (i = lo; i < hi; ++i)
2943 if (array [(i * 2)] > array [(i * 2) + 2])
2946 /* Already sorted */
2950 msort_code_offsets_internal (array, lo, mid, scratch);
2951 msort_code_offsets_internal (array, mid + 1, hi, scratch);
2953 if (array [mid * 2] < array [(mid + 1) * 2])
2959 for (i = lo; i <= hi; i ++) {
2960 if (t_lo <= mid && ((t_hi > hi) || array [t_lo * 2] < array [t_hi * 2])) {
2961 scratch [(i * 2)] = array [t_lo * 2];
2962 scratch [(i * 2) + 1] = array [(t_lo *2) + 1];
2965 scratch [(i * 2)] = array [t_hi * 2];
2966 scratch [(i * 2) + 1] = array [(t_hi *2) + 1];
2970 for (i = lo; i <= hi; ++i) {
2971 array [(i * 2)] = scratch [i * 2];
2972 array [(i * 2) + 1] = scratch [(i * 2) + 1];
2977 msort_code_offsets (gint32 *array, int len)
2981 scratch = g_new (gint32, len * 2);
2982 msort_code_offsets_internal (array, 0, len - 1, scratch);
2987 * mono_aot_find_jit_info:
2989 * In async context, the resulting MonoJitInfo will not have its method field set, and it will not be added
2990 * to the jit info tables.
2991 * FIXME: Large sizes in the lock free allocator
2994 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2996 int pos, left, right, offset, offset1, offset2, code_len;
2997 int method_index, table_len;
2999 MonoAotModule *amodule = image->aot_module;
3000 MonoMethod *method = NULL;
3002 guint8 *code, *ex_info, *p;
3005 gint32 *code_offsets;
3012 nmethods = amodule->info.nmethods;
3014 if (domain != mono_get_root_domain ())
3018 if (!amodule_contains_code_addr (amodule, addr))
3021 async = mono_thread_info_is_async_context ();
3023 offset = (guint8*)addr - amodule->code;
3025 /* Compute a sorted table mapping code offsets to method indexes. */
3026 if (!amodule->sorted_code_offsets) {
3028 code_offsets = g_new0 (gint32, nmethods * 2);
3030 for (i = 0; i < nmethods; ++i) {
3031 /* Skip the -1 entries to speed up sorting */
3032 if (amodule->code_offsets [i] == 0xffffffff)
3034 code_offsets [(offsets_len * 2)] = amodule->code_offsets [i];
3035 code_offsets [(offsets_len *2) + 1] = i;
3038 /* Use a merge sort as this is mostly sorted */
3039 msort_code_offsets (code_offsets, offsets_len);
3040 //qsort (code_offsets, offsets_len, sizeof (gint32) * 2, compare_ints);
3041 for (i = 0; i < offsets_len -1; ++i)
3042 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
3044 amodule->sorted_code_offsets_len = offsets_len;
3045 mono_memory_barrier ();
3046 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
3047 /* Somebody got in before us */
3048 g_free (code_offsets);
3051 code_offsets = amodule->sorted_code_offsets;
3052 offsets_len = amodule->sorted_code_offsets_len;
3054 /* Binary search in the sorted_code_offsets table */
3056 right = offsets_len;
3058 pos = (left + right) / 2;
3060 offset1 = code_offsets [(pos * 2)];
3061 if (pos + 1 == offsets_len) {
3062 if (amodule->code + offset1 >= amodule->jit_code_start && amodule->code + offset1 < amodule->jit_code_end)
3063 offset2 = amodule->jit_code_end - amodule->code;
3065 offset2 = amodule->llvm_code_end - amodule->code;
3067 offset2 = code_offsets [(pos + 1) * 2];
3070 if (offset < offset1)
3072 else if (offset >= offset2)
3078 g_assert (offset >= code_offsets [(pos * 2)]);
3079 if (pos + 1 < offsets_len)
3080 g_assert (offset < code_offsets [((pos + 1) * 2)]);
3081 method_index = code_offsets [(pos * 2) + 1];
3083 /* In async mode, jinfo is not added to the normal jit info table, so have to cache it ourselves */
3085 JitInfoMap *table = amodule->async_jit_info_table;
3089 len = table [0].method_index;
3090 for (i = 1; i < len; ++i) {
3091 if (table [i].method_index == method_index)
3092 return table [i].jinfo;
3097 code = &amodule->code [amodule->code_offsets [method_index]];
3098 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
3100 if (pos == offsets_len - 1) {
3101 if (code >= amodule->jit_code_start && code < amodule->jit_code_end)
3102 code_len = amodule->jit_code_end - code;
3104 code_len = amodule->llvm_code_end - code;
3106 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
3109 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
3111 /* Might be a wrapper/extra method */
3113 if (amodule->extra_methods) {
3114 amodule_lock (amodule);
3115 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
3116 amodule_unlock (amodule);
3122 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
3124 * This is hit for extra methods which are called directly, so they are
3125 * not in amodule->extra_methods.
3127 table_len = amodule->extra_method_info_offsets [0];
3128 table = amodule->extra_method_info_offsets + 1;
3135 pos = ((left + right) / 2);
3137 g_assert (pos < table_len);
3139 if (table [pos * 2] < method_index)
3141 else if (table [pos * 2] > method_index)
3147 p = amodule->blob + table [(pos * 2) + 1];
3148 method = decode_resolve_method_ref (amodule, p, &p);
3150 /* Happens when a random address is passed in which matches a not-yey called wrapper encoded using its name */
3153 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
3154 method = mono_get_method (image, token, NULL);
3161 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
3163 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
3165 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
3166 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
3168 /* Add it to the normal JitInfo tables */
3170 JitInfoMap *old_table, *new_table;
3174 * Use a simple inmutable table with linear search to cache async jit info entries.
3175 * This assumes that the number of entries is small.
3178 /* Copy the table, adding a new entry at the end */
3179 old_table = amodule->async_jit_info_table;
3181 len = old_table[0].method_index;
3184 new_table = alloc0_jit_info_data (domain, (len + 1) * sizeof (JitInfoMap), async);
3186 memcpy (new_table, old_table, len * sizeof (JitInfoMap));
3187 new_table [0].method_index = len + 1;
3188 new_table [len].method_index = method_index;
3189 new_table [len].jinfo = jinfo;
3191 mono_memory_barrier ();
3192 if (InterlockedCompareExchangePointer ((gpointer)&amodule->async_jit_info_table, new_table, old_table) == old_table)
3196 mono_jit_info_table_add (domain, jinfo);
3203 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
3211 case MONO_PATCH_INFO_METHOD:
3212 case MONO_PATCH_INFO_METHOD_JUMP:
3213 case MONO_PATCH_INFO_ICALL_ADDR:
3214 case MONO_PATCH_INFO_METHOD_RGCTX:
3215 case MONO_PATCH_INFO_METHOD_CODE_SLOT: {
3219 res = decode_method_ref (aot_module, &ref, p, &p);
3223 if (!ref.method && !mono_aot_only && !ref.no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (ref.token) == MONO_TABLE_METHOD)) {
3224 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (ref.image, ref.token));
3225 ji->type = MONO_PATCH_INFO_ABS;
3229 ji->data.method = ref.method;
3231 ji->data.method = mono_get_method (ref.image, ref.token, NULL);
3232 g_assert (ji->data.method);
3233 mono_class_init (ji->data.method->klass);
3237 case MONO_PATCH_INFO_INTERNAL_METHOD:
3238 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
3239 guint32 len = decode_value (p, &p);
3241 ji->data.name = (char*)p;
3245 case MONO_PATCH_INFO_METHODCONST:
3247 ji->data.method = decode_resolve_method_ref (aot_module, p, &p);
3248 if (!ji->data.method)
3251 case MONO_PATCH_INFO_VTABLE:
3252 case MONO_PATCH_INFO_CLASS:
3253 case MONO_PATCH_INFO_IID:
3254 case MONO_PATCH_INFO_ADJUSTED_IID:
3255 case MONO_PATCH_INFO_CLASS_INIT:
3257 ji->data.klass = decode_klass_ref (aot_module, p, &p);
3258 if (!ji->data.klass)
3261 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
3262 ji->data.del_tramp = mono_mempool_alloc0 (mp, sizeof (MonoDelegateClassMethodPair));
3263 ji->data.del_tramp->klass = decode_klass_ref (aot_module, p, &p);
3264 if (!ji->data.del_tramp->klass)
3266 if (decode_value (p, &p)) {
3267 ji->data.del_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
3268 if (!ji->data.del_tramp->method)
3271 ji->data.del_tramp->virtual = decode_value (p, &p) ? TRUE : FALSE;
3273 case MONO_PATCH_INFO_IMAGE:
3274 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
3275 if (!ji->data.image)
3278 case MONO_PATCH_INFO_FIELD:
3279 case MONO_PATCH_INFO_SFLDA:
3281 ji->data.field = decode_field_info (aot_module, p, &p);
3282 if (!ji->data.field)
3285 case MONO_PATCH_INFO_SWITCH:
3286 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
3287 ji->data.table->table_size = decode_value (p, &p);
3288 table = mono_domain_alloc (mono_domain_get (), sizeof (gpointer) * ji->data.table->table_size);
3289 ji->data.table->table = (MonoBasicBlock**)table;
3290 for (i = 0; i < ji->data.table->table_size; i++)
3291 table [i] = (gpointer)(gssize)decode_value (p, &p);
3293 case MONO_PATCH_INFO_R4: {
3296 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
3297 val = decode_value (p, &p);
3298 *(float*)ji->data.target = *(float*)&val;
3301 case MONO_PATCH_INFO_R8: {
3305 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
3307 val [0] = decode_value (p, &p);
3308 val [1] = decode_value (p, &p);
3309 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
3310 *(double*)ji->data.target = *(double*)&v;
3313 case MONO_PATCH_INFO_LDSTR:
3314 image = load_image (aot_module, decode_value (p, &p), TRUE);
3317 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
3319 case MONO_PATCH_INFO_RVA:
3320 case MONO_PATCH_INFO_DECLSEC:
3321 case MONO_PATCH_INFO_LDTOKEN:
3322 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3324 image = load_image (aot_module, decode_value (p, &p), TRUE);
3327 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
3329 ji->data.token->has_context = decode_value (p, &p);
3330 if (ji->data.token->has_context) {
3331 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
3336 case MONO_PATCH_INFO_EXC_NAME:
3337 ji->data.klass = decode_klass_ref (aot_module, p, &p);
3338 if (!ji->data.klass)
3340 ji->data.name = ji->data.klass->name;
3342 case MONO_PATCH_INFO_METHOD_REL:
3343 ji->data.offset = decode_value (p, &p);
3345 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
3346 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
3347 case MONO_PATCH_INFO_MONITOR_ENTER:
3348 case MONO_PATCH_INFO_MONITOR_ENTER_V4:
3349 case MONO_PATCH_INFO_MONITOR_EXIT:
3350 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
3351 case MONO_PATCH_INFO_GC_NURSERY_START:
3352 case MONO_PATCH_INFO_JIT_TLS_ID:
3354 case MONO_PATCH_INFO_CASTCLASS_CACHE:
3355 ji->data.index = decode_value (p, &p);
3357 case MONO_PATCH_INFO_RGCTX_FETCH: {
3359 MonoJumpInfoRgctxEntry *entry;
3360 guint32 offset, val;
3363 offset = decode_value (p, &p);
3364 val = decode_value (p, &p);
3366 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3367 p2 = aot_module->blob + offset;
3368 entry->method = decode_resolve_method_ref (aot_module, p2, &p2);
3369 entry->in_mrgctx = ((val & 1) > 0) ? TRUE : FALSE;
3370 entry->info_type = (val >> 1) & 0xff;
3371 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3372 entry->data->type = (val >> 9) & 0xff;
3374 res = decode_patch (aot_module, mp, entry->data, p, &p);
3377 ji->data.rgctx_entry = entry;
3380 case MONO_PATCH_INFO_SEQ_POINT_INFO:
3382 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
3383 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
3385 imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
3386 imt_tramp->vt_offset = decode_value (p, &p);
3388 ji->data.imt_tramp = imt_tramp;
3391 case MONO_PATCH_INFO_SIGNATURE:
3392 ji->data.target = decode_signature (aot_module, p, &p);
3394 case MONO_PATCH_INFO_TLS_OFFSET:
3395 ji->data.target = GINT_TO_POINTER (decode_value (p, &p));
3397 case MONO_PATCH_INFO_GSHAREDVT_CALL: {
3398 MonoJumpInfoGSharedVtCall *info = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoGSharedVtCall));
3399 info->sig = decode_signature (aot_module, p, &p);
3400 g_assert (info->sig);
3401 info->method = decode_resolve_method_ref (aot_module, p, &p);
3402 g_assert (info->method);
3404 ji->data.target = info;
3407 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
3408 MonoGSharedVtMethodInfo *info = mono_mempool_alloc0 (mp, sizeof (MonoGSharedVtMethodInfo));
3411 info->method = decode_resolve_method_ref (aot_module, p, &p);
3412 g_assert (info->method);
3413 info->num_entries = decode_value (p, &p);
3414 info->count_entries = info->num_entries;
3415 info->entries = mono_mempool_alloc0 (mp, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->num_entries);
3416 for (i = 0; i < info->num_entries; ++i) {
3417 MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
3419 template->info_type = decode_value (p, &p);
3420 switch (mini_rgctx_info_type_to_patch_info_type (template->info_type)) {
3421 case MONO_PATCH_INFO_CLASS: {
3422 MonoClass *klass = decode_klass_ref (aot_module, p, &p);
3425 template->data = &klass->byval_arg;
3428 case MONO_PATCH_INFO_FIELD:
3429 template->data = decode_field_info (aot_module, p, &p);
3430 if (!template->data)
3434 g_assert_not_reached ();
3438 ji->data.target = info;
3441 case MONO_PATCH_INFO_LDSTR_LIT: {
3442 int len = decode_value (p, &p);
3445 s = mono_mempool_alloc0 (mp, len + 1);
3446 memcpy (s, p, len + 1);
3449 ji->data.target = s;
3452 case MONO_PATCH_INFO_VIRT_METHOD: {
3453 MonoJumpInfoVirtMethod *info = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoVirtMethod));
3455 info->klass = decode_klass_ref (aot_module, p, &p);
3456 g_assert (info->klass);
3457 info->method = decode_resolve_method_ref (aot_module, p, &p);
3458 g_assert (info->method);
3460 ji->data.target = info;
3463 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG:
3466 g_warning ("unhandled type %d", ji->type);
3467 g_assert_not_reached ();
3478 static MonoJumpInfo*
3479 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
3480 gboolean llvm, guint32 **got_slots,
3481 guint8 *buf, guint8 **endbuf)
3483 MonoJumpInfo *patches;
3487 guint32 *got_offsets;
3492 got = aot_module->llvm_got;
3493 got_offsets = aot_module->info.llvm_got_info_offsets;
3495 got = aot_module->got;
3496 got_offsets = aot_module->info.got_info_offsets;
3499 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
3501 *got_slots = g_malloc (sizeof (guint32) * n_patches);
3503 for (pindex = 0; pindex < n_patches; ++pindex) {
3504 MonoJumpInfo *ji = &patches [pindex];
3509 got_offset = decode_value (p, &p);
3511 shared_p = aot_module->blob + mono_aot_get_offset (got_offsets, got_offset);
3513 ji->type = decode_value (shared_p, &shared_p);
3515 /* See load_method () for SFLDA */
3516 if (got [got_offset] && ji->type != MONO_PATCH_INFO_SFLDA) {
3517 /* Already loaded */
3519 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
3524 (*got_slots) [pindex] = got_offset;
3531 g_free (*got_slots);
3538 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
3541 * Jump addresses cannot be patched by the trampoline code since it
3542 * does not have access to the caller's address. Instead, we collect
3543 * the addresses of the GOT slots pointing to a method, and patch
3544 * them after the method has been compiled.
3546 MonoJitDomainInfo *info = domain_jit_info (domain);
3549 mono_domain_lock (domain);
3550 if (!info->jump_target_got_slot_hash)
3551 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
3552 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
3553 list = g_slist_prepend (list, got_slot);
3554 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
3555 mono_domain_unlock (domain);
3561 * Load the method identified by METHOD_INDEX from the AOT image. Return a
3562 * pointer to the native code of the method, or NULL if not found.
3563 * METHOD might not be set if the caller only has the image/token info.
3566 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
3569 gboolean from_plt = method == NULL;
3571 int i, pindex, n_patches, used_strings;
3572 gboolean keep_patches = TRUE;
3574 MonoJitInfo *jinfo = NULL;
3575 guint8 *code, *info;
3577 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE) {
3579 /* The caller cannot handle this */
3580 g_assert_not_reached ();
3584 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
3585 /* Non shared AOT code can't be used in other appdomains */
3588 if (amodule->out_of_date)
3591 if (amodule->code_offsets [method_index] == 0xffffffff) {
3592 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
3596 method = mono_get_method (image, token, NULL);
3597 full_name = mono_method_full_name (method, TRUE);
3598 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: NOT FOUND: %s.", full_name);
3604 code = &amodule->code [amodule->code_offsets [method_index]];
3606 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
3608 if (!amodule->methods_loaded) {
3609 amodule_lock (amodule);
3610 if (!amodule->methods_loaded) {
3613 loaded = g_new0 (guint32, amodule->info.nmethods / 32 + 1);
3614 mono_memory_barrier ();
3615 amodule->methods_loaded = loaded;
3617 amodule_unlock (amodule);
3620 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
3623 if (mono_last_aot_method != -1) {
3624 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
3626 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
3628 method = mono_get_method (image, token, NULL);
3630 char *name = mono_method_full_name (method, TRUE);
3631 g_print ("LAST AOT METHOD: %s.\n", name);
3634 g_print ("LAST AOT METHOD: %p %d\n", code, method_index);
3642 klass = method->klass;
3643 decode_klass_ref (amodule, p, &p);
3645 klass = decode_klass_ref (amodule, p, &p);
3648 if (amodule->info.opts & MONO_OPT_SHARED)
3649 used_strings = decode_value (p, &p);
3653 for (i = 0; i < used_strings; i++) {
3654 guint token = decode_value (p, &p);
3655 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
3658 if (amodule->info.opts & MONO_OPT_SHARED)
3659 keep_patches = FALSE;
3661 n_patches = decode_value (p, &p);
3663 keep_patches = FALSE;
3666 MonoJumpInfo *patches;
3674 mp = mono_mempool_new ();
3676 if ((gpointer)code >= amodule->info.jit_code_start && (gpointer)code <= amodule->info.jit_code_end) {
3681 got = amodule->llvm_got;
3685 patches = load_patch_info (amodule, mp, n_patches, llvm, &got_slots, p, &p);
3686 if (patches == NULL)
3689 for (pindex = 0; pindex < n_patches; ++pindex) {
3690 MonoJumpInfo *ji = &patches [pindex];
3694 * For SFLDA, we need to call resolve_patch_target () since the GOT slot could have
3695 * been initialized by load_method () for a static cctor before the cctor has
3696 * finished executing (#23242).
3698 if (!got [got_slots [pindex]] || ji->type == MONO_PATCH_INFO_SFLDA) {
3699 addr = mono_resolve_patch_target (method, domain, code, ji, TRUE);
3700 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
3701 addr = mono_create_ftnptr (domain, addr);
3702 mono_memory_barrier ();
3703 got [got_slots [pindex]] = addr;
3704 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
3705 register_jump_target_got_slot (domain, ji->data.method, &(got [got_slots [pindex]]));
3707 ji->type = MONO_PATCH_INFO_NONE;
3713 mono_mempool_destroy (mp);
3716 if (mini_get_debug_options ()->load_aot_jit_info_eagerly)
3717 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
3719 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
3723 method = mono_get_method (image, token, NULL);
3725 full_name = mono_method_full_name (method, TRUE);
3728 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
3730 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: FOUND method %s [%p - %p %p]", full_name, code, code + jinfo->code_size, info);
3734 amodule_lock (amodule);
3736 InterlockedIncrement (&mono_jit_stats.methods_aot);
3738 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
3742 if (method && method->wrapper_type)
3743 g_hash_table_insert (amodule->method_to_code, method, code);
3745 amodule_unlock (amodule);
3747 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
3751 method = mono_get_method (image, token, NULL);
3754 mono_profiler_method_jit (method);
3755 jinfo = mono_jit_info_table_find (domain, (char*)code);
3757 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
3760 if (from_plt && klass && !klass->generic_container)
3761 mono_runtime_class_init (mono_class_vtable (domain, klass));
3766 /* FIXME: The space in domain->mp is wasted */
3767 if (amodule->info.opts & MONO_OPT_SHARED)
3768 /* No need to cache patches */
3769 mono_mempool_destroy (mp);
3778 find_aot_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
3780 guint32 table_size, entry_size, hash;
3781 guint32 *table, *entry;
3783 static guint32 n_extra_decodes;
3785 if (!amodule || amodule->out_of_date)
3788 table_size = amodule->extra_method_table [0];
3789 table = amodule->extra_method_table + 1;
3792 hash = mono_aot_method_hash (method) % table_size;
3794 entry = &table [hash * entry_size];
3801 guint32 key = entry [0];
3802 guint32 value = entry [1];
3803 guint32 next = entry [entry_size - 1];
3807 p = amodule->blob + key;
3810 amodule_lock (amodule);
3811 if (!amodule->method_ref_to_method)
3812 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
3813 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
3814 amodule_unlock (amodule);
3816 m = decode_resolve_method_ref_with_target (amodule, method, p, &p);
3818 amodule_lock (amodule);
3819 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
3820 amodule_unlock (amodule);
3828 /* Special case: wrappers of shared generic methods */
3829 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
3830 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
3831 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
3832 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
3834 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
3840 /* Methods decoded needlessly */
3842 //printf ("%d %s %s %p\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE), orig_p);
3847 entry = &table [next * entry_size];
3856 add_module_cb (gpointer key, gpointer value, gpointer user_data)
3858 g_ptr_array_add ((GPtrArray*)user_data, value);
3864 * Try finding METHOD in the extra_method table in all AOT images.
3865 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
3866 * module where the method was found.
3869 find_aot_method (MonoMethod *method, MonoAotModule **out_amodule)
3875 /* Try the method's module first */
3876 *out_amodule = method->klass->image->aot_module;
3877 index = find_aot_method_in_amodule (method->klass->image->aot_module, method);
3878 if (index != 0xffffff)
3882 * Try all other modules.
3883 * This is needed because generic instances klass->image points to the image
3884 * containing the generic definition, but the native code is generated to the
3885 * AOT image which contains the reference.
3888 /* Make a copy to avoid doing the search inside the aot lock */
3889 modules = g_ptr_array_new ();
3891 g_hash_table_foreach (aot_modules, add_module_cb, modules);
3895 for (i = 0; i < modules->len; ++i) {
3896 MonoAotModule *amodule = g_ptr_array_index (modules, i);
3898 if (amodule != method->klass->image->aot_module)
3899 index = find_aot_method_in_amodule (amodule, method);
3900 if (index != 0xffffff) {
3901 *out_amodule = amodule;
3906 g_ptr_array_free (modules, TRUE);
3912 mono_aot_find_method_index (MonoMethod *method)
3914 MonoAotModule *out_amodule;
3915 return find_aot_method (method, &out_amodule);
3919 * mono_aot_get_method:
3921 * Return a pointer to the AOTed native code for METHOD if it can be found,
3923 * On platforms with function pointers, this doesn't return a function pointer.
3926 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3928 MonoClass *klass = method->klass;
3929 guint32 method_index;
3930 MonoAotModule *amodule = klass->image->aot_module;
3933 if (enable_aot_cache && !amodule && domain->entry_assembly && klass->image == mono_defaults.corlib) {
3934 /* This cannot be AOTed during startup, so do it now */
3935 if (!mscorlib_aot_loaded) {
3936 mscorlib_aot_loaded = TRUE;
3937 load_aot_module (klass->image->assembly, NULL);
3938 amodule = klass->image->aot_module;
3945 if (amodule->out_of_date)
3948 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3949 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3950 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3951 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
3955 * Use the original method instead of its invoke-with-check wrapper.
3956 * This is not a problem when using full-aot, since it doesn't support
3959 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
3960 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
3962 g_assert (klass->inited);
3964 /* Find method index */
3965 method_index = 0xffffff;
3966 if (method->is_inflated && !method->wrapper_type && mono_method_is_generic_sharable_full (method, FALSE, FALSE, FALSE)) {
3968 * For generic methods, we store the fully shared instance in place of the
3971 method = mono_method_get_declaring_generic_method (method);
3972 method_index = mono_metadata_token_index (method->token) - 1;
3973 } else if (method->is_inflated || !method->token) {
3974 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
3975 amodule_lock (amodule);
3976 code = g_hash_table_lookup (amodule->method_to_code, method);
3977 amodule_unlock (amodule);
3981 method_index = find_aot_method (method, &amodule);
3983 * Special case the ICollection<T> wrappers for arrays, as they cannot
3984 * be statically enumerated, and each wrapper ends up calling the same
3987 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
3988 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
3990 code = mono_aot_get_method (domain, m);
3996 * Special case Array.GetGenericValueImpl which is a generic icall.
3997 * Generic sharing currently can't handle it, but the icall returns data using
3998 * an out parameter, so the managed-to-native wrappers can share the same code.
4000 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
4003 MonoGenericContext ctx;
4004 MonoType *args [16];
4006 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
4007 /* Avoid recursion */
4010 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
4013 memset (&ctx, 0, sizeof (ctx));
4014 args [0] = &mono_defaults.object_class->byval_arg;
4015 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
4017 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m, &ctx, &error), TRUE, TRUE);
4018 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4021 * Get the code for the <object> instantiation which should be emitted into
4022 * the mscorlib aot image by the AOT compiler.
4024 code = mono_aot_get_method (domain, m);
4029 /* Same for CompareExchange<T> and Exchange<T> */
4030 /* Same for Volatile.Read<T>/Write<T> */
4031 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass->image == mono_defaults.corlib &&
4032 ((!strcmp (method->klass->name_space, "System.Threading") && !strcmp (method->klass->name, "Interlocked") && (!strcmp (method->name, "CompareExchange") || !strcmp (method->name, "Exchange")) && MONO_TYPE_IS_REFERENCE (mono_method_signature (method)->params [1])) ||
4033 (!strcmp (method->klass->name_space, "System.Threading") && !strcmp (method->klass->name, "Volatile") && (!strcmp (method->name, "Read") && MONO_TYPE_IS_REFERENCE (mono_method_signature (method)->ret))) ||
4034 (!strcmp (method->klass->name_space, "System.Threading") && !strcmp (method->klass->name, "Volatile") && (!strcmp (method->name, "Write") && MONO_TYPE_IS_REFERENCE (mono_method_signature (method)->params [1]))))) {
4037 MonoGenericContext ctx;
4038 MonoType *args [16];
4039 gpointer iter = NULL;
4041 while ((m = mono_class_get_methods (method->klass, &iter))) {
4042 if (mono_method_signature (m)->generic_param_count && !strcmp (m->name, method->name))
4047 memset (&ctx, 0, sizeof (ctx));
4048 args [0] = &mono_defaults.object_class->byval_arg;
4049 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
4051 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m, &ctx, &error), TRUE, TRUE);
4052 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4054 /* Avoid recursion */
4059 * Get the code for the <object> instantiation which should be emitted into
4060 * the mscorlib aot image by the AOT compiler.
4062 code = mono_aot_get_method (domain, m);
4067 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_full (method, FALSE, TRUE, FALSE)) {
4068 /* Partial sharing */
4071 shared = mini_get_shared_method (method);
4072 method_index = find_aot_method (shared, &amodule);
4073 if (method_index != 0xffffff)
4077 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) {
4079 /* Use the all-vt shared method since this is what was AOTed */
4080 method_index = find_aot_method (mini_get_shared_method_full (method, TRUE, TRUE), &amodule);
4081 if (method_index != 0xffffff)
4082 method = mini_get_shared_method_full (method, TRUE, FALSE);
4085 if (method_index == 0xffffff) {
4086 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
4089 full_name = mono_method_full_name (method, TRUE);
4090 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.", full_name);
4096 if (method_index == 0xffffff)
4099 /* Needed by find_jit_info */
4100 amodule_lock (amodule);
4101 if (!amodule->extra_methods)
4102 amodule->extra_methods = g_hash_table_new (NULL, NULL);
4103 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
4104 amodule_unlock (amodule);
4107 method_index = mono_metadata_token_index (method->token) - 1;
4110 return load_method (domain, amodule, klass->image, method, method->token, method_index);
4114 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
4118 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
4120 MonoAotModule *aot_module = image->aot_module;
4126 method_index = mono_metadata_token_index (token) - 1;
4128 return load_method (domain, aot_module, image, NULL, token, method_index);
4134 } IsGotEntryUserData;
4137 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
4139 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
4140 MonoAotModule *aot_module = (MonoAotModule*)value;
4142 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
4147 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
4149 IsGotEntryUserData user_data;
4154 user_data.addr = addr;
4155 user_data.res = FALSE;
4157 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
4160 return user_data.res;
4165 MonoAotModule *module;
4166 } FindAotModuleUserData;
4169 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
4171 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
4172 MonoAotModule *aot_module = (MonoAotModule*)value;
4174 if (amodule_contains_code_addr (aot_module, data->addr))
4175 data->module = aot_module;
4178 static inline MonoAotModule*
4179 find_aot_module (guint8 *code)
4181 FindAotModuleUserData user_data;
4186 /* Reading these need no locking */
4187 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
4190 user_data.addr = code;
4191 user_data.module = NULL;
4194 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
4197 return user_data.module;
4201 mono_aot_patch_plt_entry (guint8 *code, guint8 *plt_entry, gpointer *got, mgreg_t *regs, guint8 *addr)
4203 MonoAotModule *amodule;
4206 * Since AOT code is only used in the root domain,
4207 * mono_domain_get () != mono_get_root_domain () means the calling method
4208 * is AppDomain:InvokeInDomain, so this is the same check as in
4209 * mono_method_same_domain () but without loading the metadata for the method.
4211 if (mono_domain_get () == mono_get_root_domain ()) {
4213 amodule = find_aot_module (code);
4217 mono_arch_patch_plt_entry (plt_entry, got, regs, addr);
4222 * mono_aot_plt_resolve:
4224 * This function is called by the entries in the PLT to resolve the actual method that
4225 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
4226 * Returns NULL if the something cannot be loaded.
4229 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
4231 #ifdef MONO_ARCH_AOT_SUPPORTED
4232 guint8 *p, *target, *plt_entry;
4234 MonoAotModule *module = (MonoAotModule*)aot_module;
4235 gboolean res, no_ftnptr = FALSE;
4237 gboolean using_gsharedvt = FALSE;
4239 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
4241 p = &module->blob [plt_info_offset];
4243 ji.type = decode_value (p, &p);
4245 mp = mono_mempool_new_size (512);
4246 res = decode_patch (module, mp, &ji, p, &p);
4249 mono_mempool_destroy (mp);
4253 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
4254 using_gsharedvt = TRUE;
4258 * Avoid calling resolve_patch_target in the full-aot case if possible, since
4259 * it would create a trampoline, and we don't need that.
4260 * We could do this only if the method does not need the special handling
4261 * in mono_magic_trampoline ().
4263 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) &&
4264 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE) && !using_gsharedvt) {
4265 target = mono_jit_compile_method (ji.data.method);
4268 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
4272 * The trampoline expects us to return a function descriptor on platforms which use
4273 * it, but resolve_patch_target returns a direct function pointer for some type of
4274 * patches, so have to translate between the two.
4275 * FIXME: Clean this up, but how ?
4277 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) {
4278 /* These should already have a function descriptor */
4279 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4280 /* Our function descriptors have a 0 environment, gcc created ones don't */
4281 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
4282 g_assert (((gpointer*)target) [2] == 0);
4285 } else if (!no_ftnptr) {
4286 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4287 g_assert (((gpointer*)target) [2] != 0);
4289 target = mono_create_ftnptr (mono_domain_get (), target);
4292 mono_mempool_destroy (mp);
4294 /* Patch the PLT entry with target which might be the actual method not a trampoline */
4295 plt_entry = mono_aot_get_plt_entry (code);
4296 g_assert (plt_entry);
4297 mono_aot_patch_plt_entry (code, plt_entry, module->got, NULL, target);
4301 g_assert_not_reached ();
4309 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
4310 * method in the module is loaded to avoid committing memory by writing to it.
4311 * LOCKING: Assumes the AMODULE lock is held.
4314 init_plt (MonoAotModule *amodule)
4319 if (amodule->plt_inited)
4322 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
4325 * Initialize the PLT entries in the GOT to point to the default targets.
4328 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
4329 for (i = 1; i < amodule->info.plt_size; ++i)
4330 /* All the default entries point to the AOT trampoline */
4331 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = tramp;
4333 amodule->plt_inited = TRUE;
4337 * mono_aot_get_plt_entry:
4339 * Return the address of the PLT entry called by the code at CODE if exists.
4342 mono_aot_get_plt_entry (guint8 *code)
4344 MonoAotModule *amodule = find_aot_module (code);
4345 guint8 *target = NULL;
4351 if (amodule->thumb_end) {
4352 if (code >= amodule->llvm_code_start && code < amodule->llvm_code_end)
4353 return mono_arm_get_thumb_plt_entry (code);
4357 #ifdef MONO_ARCH_AOT_SUPPORTED
4358 target = mono_arch_get_call_target (code);
4360 g_assert_not_reached ();
4364 while (target != NULL) {
4365 if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
4368 // Add 4 since mono_arch_get_call_target assumes we're passing
4369 // the instruction after the actual branch instruction.
4370 target = mono_arch_get_call_target (target + 4);
4375 if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
4383 * mono_aot_get_plt_info_offset:
4385 * Return the PLT info offset belonging to the plt entry called by CODE.
4388 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
4390 guint8 *plt_entry = mono_aot_get_plt_entry (code);
4392 g_assert (plt_entry);
4394 /* The offset is embedded inside the code after the plt entry */
4395 #ifdef MONO_ARCH_AOT_SUPPORTED
4396 return mono_arch_get_plt_info_offset (plt_entry, regs, code);
4398 g_assert_not_reached ();
4404 mono_create_ftnptr_malloc (guint8 *code)
4406 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4407 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
4409 ftnptr->code = code;
4420 * mono_aot_register_jit_icall:
4422 * Register a JIT icall which is called by trampolines in full-aot mode. This should
4423 * be called from mono_arch_init () during startup.
4426 mono_aot_register_jit_icall (const char *name, gpointer addr)
4428 /* No need for locking */
4429 if (!aot_jit_icall_hash)
4430 aot_jit_icall_hash = g_hash_table_new (g_str_hash, g_str_equal);
4431 g_hash_table_insert (aot_jit_icall_hash, (char*)name, addr);
4435 * load_function_full:
4437 * Load the function named NAME from the aot image.
4440 load_function_full (MonoAotModule *amodule, const char *name, MonoTrampInfo **out_tinfo)
4444 int n_patches, pindex;
4447 guint32 info_offset;
4451 symbol = g_strdup_printf ("%s", name);
4452 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
4455 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
4457 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: FOUND function '%s' in AOT file '%s'.", name, amodule->aot_name);
4461 symbol = g_strdup_printf ("%s_p", name);
4462 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
4465 /* Nothing to patch */
4468 info_offset = *(guint32*)p;
4470 MonoTrampInfo *tinfo;
4471 guint32 code_size, uw_info_len, uw_offset;
4473 /* Construct a MonoTrampInfo from the data in the AOT image */
4475 p += sizeof (guint32);
4476 code_size = *(guint32*)p;
4477 p += sizeof (guint32);
4478 uw_offset = *(guint32*)p;
4479 uw_info = amodule->unwind_info + uw_offset;
4480 uw_info_len = decode_value (uw_info, &uw_info);
4482 tinfo = g_new0 (MonoTrampInfo, 1);
4484 tinfo->code_size = code_size;
4485 tinfo->uw_info = uw_info;
4486 tinfo->uw_info_len = uw_info_len;
4491 p = amodule->blob + info_offset;
4493 /* Similar to mono_aot_load_method () */
4495 n_patches = decode_value (p, &p);
4498 MonoJumpInfo *patches;
4501 mp = mono_mempool_new ();
4503 patches = load_patch_info (amodule, mp, n_patches, FALSE, &got_slots, p, &p);
4506 for (pindex = 0; pindex < n_patches; ++pindex) {
4507 MonoJumpInfo *ji = &patches [pindex];
4510 if (amodule->got [got_slots [pindex]])
4514 * When this code is executed, the runtime may not be initalized yet, so
4515 * resolve the patch info by hand.
4517 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
4518 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
4519 target = mono_get_lmf_addr;
4520 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
4521 target = mono_thread_force_interruption_checkpoint;
4522 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
4523 target = mono_exception_from_token;
4524 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
4525 target = mono_get_throw_exception ();
4526 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
4527 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
4528 target = (gpointer)mono_get_trampoline_func (tramp_type2);
4529 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
4530 /* atoll is needed because the the offset is unsigned */
4534 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
4535 g_assert (res == 1);
4536 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
4537 target = mono_create_ftnptr_malloc (target);
4538 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
4539 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
4540 target = mono_create_ftnptr_malloc (target);
4541 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter_v4")) {
4542 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER_V4, mono_get_root_domain (), NULL);
4543 target = mono_create_ftnptr_malloc (target);
4544 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
4545 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
4546 target = mono_create_ftnptr_malloc (target);
4547 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
4548 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
4549 target = mono_create_ftnptr_malloc (target);
4550 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
4551 target = mono_thread_get_and_clear_pending_exception;
4552 } else if (!strcmp (ji->data.name, "debugger_agent_single_step_from_context")) {
4553 target = debugger_agent_single_step_from_context;
4554 } else if (!strcmp (ji->data.name, "debugger_agent_breakpoint_from_context")) {
4555 target = debugger_agent_breakpoint_from_context;
4556 } else if (strstr (ji->data.name, "generic_trampoline_")) {
4557 target = mono_aot_get_trampoline (ji->data.name);
4558 } else if (aot_jit_icall_hash && g_hash_table_lookup (aot_jit_icall_hash, ji->data.name)) {
4559 /* Registered by mono_arch_init () */
4560 target = g_hash_table_lookup (aot_jit_icall_hash, ji->data.name);
4562 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
4563 g_assert_not_reached ();
4567 /* Hopefully the code doesn't have patches which need method or
4570 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
4574 amodule->got [got_slots [pindex]] = target;
4579 mono_mempool_destroy (mp);
4586 load_function (MonoAotModule *amodule, const char *name)
4588 return load_function_full (amodule, name, NULL);
4592 * Return the trampoline identified by NAME from the mscorlib AOT file.
4593 * On ppc64, this returns a function descriptor.
4596 mono_aot_get_trampoline_full (const char *name, MonoTrampInfo **out_tinfo)
4599 MonoAotModule *amodule;
4601 image = mono_defaults.corlib;
4604 amodule = image->aot_module;
4607 return mono_create_ftnptr_malloc (load_function_full (amodule, name, out_tinfo));
4611 mono_aot_get_trampoline (const char *name)
4613 return mono_aot_get_trampoline_full (name, NULL);
4617 #include <mach/mach.h>
4619 static TrampolinePage* trampoline_pages [MONO_AOT_TRAMP_NUM];
4621 static unsigned char*
4622 get_new_trampoline_from_page (int tramp_type)
4624 MonoAotModule *amodule;
4626 TrampolinePage *page;
4629 vm_address_t addr, taddr;
4631 vm_prot_t prot, max_prot;
4632 int psize, specific_trampoline_size;
4633 unsigned char *code;
4635 specific_trampoline_size = 2 * sizeof (gpointer);
4637 mono_aot_page_lock ();
4638 page = trampoline_pages [tramp_type];
4639 if (page && page->trampolines < page->trampolines_end) {
4640 code = page->trampolines;
4641 page->trampolines += specific_trampoline_size;
4642 mono_aot_page_unlock ();
4645 mono_aot_page_unlock ();
4646 psize = mono_pagesize ();
4647 /* the trampoline template page is in the mscorlib module */
4648 image = mono_defaults.corlib;
4651 amodule = image->aot_module;
4654 g_assert (amodule->info.tramp_page_size == psize);
4656 if (tramp_type == MONO_AOT_TRAMP_SPECIFIC)
4657 tpage = load_function (amodule, "specific_trampolines_page");
4658 else if (tramp_type == MONO_AOT_TRAMP_STATIC_RGCTX)
4659 tpage = load_function (amodule, "rgctx_trampolines_page");
4660 else if (tramp_type == MONO_AOT_TRAMP_IMT_THUNK)
4661 tpage = load_function (amodule, "imt_trampolines_page");
4662 else if (tramp_type == MONO_AOT_TRAMP_GSHAREDVT_ARG)
4663 tpage = load_function (amodule, "gsharedvt_arg_trampolines_page");
4665 g_error ("Incorrect tramp type for trampolines page");
4667 /*g_warning ("loaded trampolines page at %x", tpage);*/
4669 /* avoid the unlikely case of looping forever */
4672 while (page == NULL && count-- > 0) {
4674 /* allocate two contiguous pages of memory: the first page will contain the data (like a local constant pool)
4675 * while the second will contain the trampolines.
4677 ret = vm_allocate (mach_task_self (), &addr, psize * 2, VM_FLAGS_ANYWHERE);
4678 if (ret != KERN_SUCCESS) {
4679 g_error ("Cannot allocate memory for trampolines: %d", ret);
4682 /*g_warning ("allocated trampoline double page at %x", addr);*/
4683 /* replace the second page with a remapped trampoline page */
4684 taddr = addr + psize;
4685 vm_deallocate (mach_task_self (), taddr, psize);
4686 ret = vm_remap (mach_task_self (), &taddr, psize, 0, FALSE, mach_task_self(), (vm_address_t)tpage, FALSE, &prot, &max_prot, VM_INHERIT_SHARE);
4687 if (ret != KERN_SUCCESS) {
4688 /* someone else got the page, try again */
4689 vm_deallocate (mach_task_self (), addr, psize);
4692 /*g_warning ("remapped trampoline page at %x", taddr);*/
4694 mono_aot_page_lock ();
4695 page = trampoline_pages [tramp_type];
4696 /* some other thread already allocated, so use that to avoid wasting memory */
4697 if (page && page->trampolines < page->trampolines_end) {
4698 code = page->trampolines;
4699 page->trampolines += specific_trampoline_size;
4700 mono_aot_page_unlock ();
4701 vm_deallocate (mach_task_self (), addr, psize);
4702 vm_deallocate (mach_task_self (), taddr, psize);
4705 page = (TrampolinePage*)addr;
4706 page->next = trampoline_pages [tramp_type];
4707 trampoline_pages [tramp_type] = page;
4708 page->trampolines = (void*)(taddr + amodule->info.tramp_page_code_offsets [tramp_type]);
4709 page->trampolines_end = (void*)(taddr + psize - 64);
4710 code = page->trampolines;
4711 page->trampolines += specific_trampoline_size;
4712 mono_aot_page_unlock ();
4715 g_error ("Cannot allocate more trampoline pages: %d", ret);
4720 static unsigned char*
4721 get_new_trampoline_from_page (int tramp_type)
4723 g_error ("Page trampolines not supported.");
4730 get_new_specific_trampoline_from_page (gpointer tramp, gpointer arg)
4735 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_SPECIFIC);
4737 data = (gpointer*)((char*)code - mono_pagesize ());
4740 /*g_warning ("new trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
4746 get_new_rgctx_trampoline_from_page (gpointer tramp, gpointer arg)
4751 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_STATIC_RGCTX);
4753 data = (gpointer*)((char*)code - mono_pagesize ());
4756 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
4762 get_new_imt_trampoline_from_page (gpointer arg)
4767 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_IMT_THUNK);
4769 data = (gpointer*)((char*)code - mono_pagesize ());
4771 /*g_warning ("new imt trampoline at %p for data %p, (stored at %p)", code, arg, data);*/
4777 get_new_gsharedvt_arg_trampoline_from_page (gpointer tramp, gpointer arg)
4782 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_GSHAREDVT_ARG);
4784 data = (gpointer*)((char*)code - mono_pagesize ());
4787 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
4791 /* Return a given kind of trampoline */
4793 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
4795 MonoAotModule *amodule;
4796 int index, tramp_size;
4799 /* Currently, we keep all trampolines in the mscorlib AOT image */
4800 image = mono_defaults.corlib;
4805 amodule = image->aot_module;
4808 *out_amodule = amodule;
4811 #define MONOTOUCH_TRAMPOLINES_ERROR ". See http://docs.xamarin.com/ios/troubleshooting for instructions on how to fix this condition."
4813 #define MONOTOUCH_TRAMPOLINES_ERROR ""
4815 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type]) {
4816 g_error ("Ran out of trampolines of type %d in '%s' (%d)%s\n",
4817 tramp_type, image->name, amodule->info.num_trampolines [tramp_type], MONOTOUCH_TRAMPOLINES_ERROR);
4819 index = amodule->trampoline_index [tramp_type] ++;
4823 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
4825 tramp_size = amodule->info.trampoline_size [tramp_type];
4828 *out_tramp_size = tramp_size;
4830 return amodule->trampolines [tramp_type] + (index * tramp_size);
4834 * Return a specific trampoline from the AOT file.
4837 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
4839 MonoAotModule *amodule;
4840 guint32 got_offset, tramp_size;
4841 guint8 *code, *tramp;
4842 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
4843 static gboolean inited;
4844 static guint32 num_trampolines;
4850 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
4859 if (!generic_trampolines [tramp_type]) {
4862 symbol = mono_get_generic_trampoline_name (tramp_type);
4863 generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
4867 tramp = generic_trampolines [tramp_type];
4870 if (USE_PAGE_TRAMPOLINES) {
4871 code = get_new_specific_trampoline_from_page (tramp, arg1);
4874 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
4876 amodule->got [got_offset] = tramp;
4877 amodule->got [got_offset + 1] = arg1;
4881 *code_len = tramp_size;
4887 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
4889 MonoAotModule *amodule;
4893 if (USE_PAGE_TRAMPOLINES) {
4894 code = get_new_rgctx_trampoline_from_page (addr, ctx);
4896 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
4898 amodule->got [got_offset] = ctx;
4899 amodule->got [got_offset + 1] = addr;
4902 /* The caller expects an ftnptr */
4903 return mono_create_ftnptr (mono_domain_get (), code);
4907 mono_aot_get_unbox_trampoline (MonoMethod *method)
4909 guint32 method_index = mono_metadata_token_index (method->token) - 1;
4910 MonoAotModule *amodule;
4912 guint32 *ut, *ut_end, *entry;
4913 int low, high, entry_index = 0;
4915 if (method->is_inflated && !mono_method_is_generic_sharable_full (method, FALSE, FALSE, FALSE)) {
4916 method_index = find_aot_method (method, &amodule);
4917 if (method_index == 0xffffff && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) {
4918 MonoMethod *shared = mini_get_shared_method_full (method, TRUE, TRUE);
4919 method_index = find_aot_method (shared, &amodule);
4921 g_assert (method_index != 0xffffff);
4923 amodule = method->klass->image->aot_module;
4927 ut = amodule->unbox_trampolines;
4928 ut_end = amodule->unbox_trampolines_end;
4930 /* Do a binary search in the sorted table */
4933 high = (ut_end - ut);
4934 while (low < high) {
4935 entry_index = (low + high) / 2;
4936 entry = &ut [entry_index];
4937 if (entry [0] < method_index) {
4938 low = entry_index + 1;
4939 } else if (entry [0] > method_index) {
4946 code = get_call_table_entry (amodule->unbox_trampoline_addresses, entry_index);
4949 /* The caller expects an ftnptr */
4950 return mono_create_ftnptr (mono_domain_get (), code);
4954 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
4958 MonoAotModule *amodule = mono_defaults.corlib->aot_module;
4959 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
4960 static int count = 0;
4963 if (index >= amodule->info.num_rgctx_fetch_trampolines) {
4964 static gpointer addr;
4968 * Use the general version of the rgctx fetch trampoline. It receives a pair of <slot, trampoline> in the rgctx arg reg.
4971 addr = load_function (amodule, "rgctx_fetch_trampoline_general");
4972 info = mono_domain_alloc0 (mono_get_root_domain (), sizeof (gpointer) * 2);
4973 info [0] = GUINT_TO_POINTER (slot);
4974 info [1] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
4975 code = mono_aot_get_static_rgctx_trampoline (info, addr);
4976 return mono_create_ftnptr (mono_domain_get (), code);
4979 symbol = mono_get_rgctx_fetch_trampoline_name (slot);
4980 code = load_function (mono_defaults.corlib->aot_module, symbol);
4982 /* The caller expects an ftnptr */
4983 return mono_create_ftnptr (mono_domain_get (), code);
4987 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
4992 int i, index, real_count;
4993 MonoAotModule *amodule;
4996 for (i = 0; i < count; ++i) {
4997 MonoIMTCheckItem *item = imt_entries [i];
4999 if (item->is_equals)
5003 /* Save the entries into an array */
5004 buf = mono_domain_alloc (domain, (real_count + 1) * 2 * sizeof (gpointer));
5006 for (i = 0; i < count; ++i) {
5007 MonoIMTCheckItem *item = imt_entries [i];
5009 if (!item->is_equals)
5012 g_assert (item->key);
5014 buf [(index * 2)] = item->key;
5015 if (item->has_target_code) {
5016 gpointer *p = mono_domain_alloc (domain, sizeof (gpointer));
5017 *p = item->value.target_code;
5018 buf [(index * 2) + 1] = p;
5020 buf [(index * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
5024 buf [(index * 2)] = NULL;
5025 buf [(index * 2) + 1] = fail_tramp;
5027 if (USE_PAGE_TRAMPOLINES) {
5028 code = get_new_imt_trampoline_from_page (buf);
5030 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
5032 amodule->got [got_offset] = buf;
5039 mono_aot_get_gsharedvt_arg_trampoline (gpointer arg, gpointer addr)
5041 MonoAotModule *amodule;
5045 if (USE_PAGE_TRAMPOLINES) {
5046 code = get_new_gsharedvt_arg_trampoline_from_page (addr, arg);
5048 code = get_numerous_trampoline (MONO_AOT_TRAMP_GSHAREDVT_ARG, 2, &amodule, &got_offset, NULL);
5050 amodule->got [got_offset] = arg;
5051 amodule->got [got_offset + 1] = addr;
5054 /* The caller expects an ftnptr */
5055 return mono_create_ftnptr (mono_domain_get (), code);
5059 * mono_aot_set_make_unreadable:
5061 * Set whenever to make all mmaped memory unreadable. In conjuction with a
5062 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
5065 mono_aot_set_make_unreadable (gboolean unreadable)
5069 make_unreadable = unreadable;
5071 if (make_unreadable && !inited) {
5072 mono_counters_register ("AOT: pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
5077 MonoAotModule *module;
5082 find_map (gpointer key, gpointer value, gpointer user_data)
5084 MonoAotModule *module = (MonoAotModule*)value;
5085 FindMapUserData *data = (FindMapUserData*)user_data;
5088 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
5089 data->module = module;
5092 static MonoAotModule*
5093 find_module_for_addr (void *ptr)
5095 FindMapUserData data;
5097 if (!make_unreadable)
5101 data.ptr = (guint8*)ptr;
5104 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
5111 * mono_aot_is_pagefault:
5113 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
5114 * within memory allocated by this module.
5117 mono_aot_is_pagefault (void *ptr)
5119 if (!make_unreadable)
5123 * Not signal safe, but SIGSEGV's are synchronous, and
5124 * this is only turned on by a MONO_DEBUG option.
5126 return find_module_for_addr (ptr) != NULL;
5130 * mono_aot_handle_pagefault:
5132 * Handle a pagefault caused by an unreadable page by making it readable again.
5135 mono_aot_handle_pagefault (void *ptr)
5137 #ifndef PLATFORM_WIN32
5138 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
5142 res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
5143 g_assert (res == 0);
5154 mono_aot_init (void)
5159 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
5165 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
5171 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
5177 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
5183 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
5189 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
5195 mono_aot_get_plt_entry (guint8 *code)
5201 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
5207 mono_aot_patch_plt_entry (guint8 *code, guint8 *plt_entry, gpointer *got, mgreg_t *regs, guint8 *addr)
5212 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
5218 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
5220 g_assert_not_reached ();
5226 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
5228 g_assert_not_reached ();
5233 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
5235 g_assert_not_reached ();
5240 mono_aot_get_trampoline (const char *name)
5242 g_assert_not_reached ();
5247 mono_aot_get_unbox_trampoline (MonoMethod *method)
5249 g_assert_not_reached ();
5254 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
5256 g_assert_not_reached ();
5261 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
5263 g_assert_not_reached ();
5268 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
5270 g_assert_not_reached ();
5275 mono_aot_register_jit_icall (const char *name, gpointer addr)