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>
66 #define ENABLE_AOT_CACHE
70 #define SHARED_EXT ".dll"
71 #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
72 #define SHARED_EXT ".dylib"
73 #elif defined(__APPLE__) && defined(TARGET_X86) && !defined(__native_client_codegen__)
74 #define SHARED_EXT ".dylib"
76 #define SHARED_EXT ".so"
79 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
80 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
81 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
88 typedef struct MonoAotModule {
90 /* Pointer to the Global Offset Table */
92 GHashTable *name_cache;
93 GHashTable *extra_methods;
94 /* Maps methods to their code */
95 GHashTable *method_to_code;
96 /* Maps pointers into the method info to the methods themselves */
97 GHashTable *method_ref_to_method;
98 MonoAssemblyName *image_names;
100 MonoAssembly *assembly;
101 MonoImage **image_table;
102 guint32 image_table_len;
103 gboolean out_of_date;
112 gint32 *code_offsets;
113 gpointer *method_addresses;
114 /* This contains <offset, index> pairs sorted by offset */
115 /* This is needed because LLVM emitted methods can be in any order */
116 gint32 *sorted_code_offsets;
117 gint32 sorted_code_offsets_len;
118 guint32 *method_info_offsets;
119 guint32 *got_info_offsets;
120 guint32 *ex_info_offsets;
121 guint32 *class_info_offsets;
122 guint32 *methods_loaded;
123 guint16 *class_name_table;
124 guint32 *extra_method_table;
125 guint32 *extra_method_info_offsets;
126 guint32 *unbox_trampolines;
127 guint32 *unbox_trampolines_end;
131 /* Points to the mono EH data created by LLVM */
132 guint8 *mono_eh_frame;
134 /* Points to the trampolines */
135 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
136 /* The first unused trampoline of each kind */
137 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
139 gboolean use_page_trampolines;
141 MonoAotFileInfo info;
146 JitInfoMap *async_jit_info_table;
152 unsigned char *trampolines;
153 unsigned char *trampolines_end;
156 static GHashTable *aot_modules;
157 #define mono_aot_lock() mono_mutex_lock (&aot_mutex)
158 #define mono_aot_unlock() mono_mutex_unlock (&aot_mutex)
159 static mono_mutex_t aot_mutex;
162 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
163 * AOT modules registered by mono_aot_register_module ().
165 static GHashTable *static_aot_modules;
168 * Maps MonoJitInfo* to the aot module they belong to, this can be different
169 * from ji->method->klass->image's aot module for generic instances.
171 static GHashTable *ji_to_amodule;
174 * Whenever to AOT compile loaded assemblies on demand and store them in
177 static gboolean enable_aot_cache = FALSE;
179 static gboolean mscorlib_aot_loaded;
182 static gint32 mono_last_aot_method = -1;
184 static gboolean make_unreadable = FALSE;
185 static guint32 name_table_accesses = 0;
186 static guint32 n_pagefaults = 0;
188 /* Used to speed-up find_aot_module () */
189 static gsize aot_code_low_addr = (gssize)-1;
190 static gsize aot_code_high_addr = 0;
193 static gint32 async_jit_info_size;
195 static GHashTable *aot_jit_icall_hash;
198 #define USE_PAGE_TRAMPOLINES ((MonoAotModule*)mono_defaults.corlib->aot_module)->use_page_trampolines
200 #define USE_PAGE_TRAMPOLINES 0
203 #define mono_aot_page_lock() mono_mutex_lock (&aot_page_mutex)
204 #define mono_aot_page_unlock() mono_mutex_unlock (&aot_page_mutex)
205 static mono_mutex_t aot_page_mutex;
208 init_plt (MonoAotModule *info);
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, noffsets, 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)
409 MonoClass *klass = NULL, *eklass;
410 guint32 token, rank, idx;
414 reftype = decode_value (p, &p);
421 case MONO_AOT_TYPEREF_TYPEDEF_INDEX:
422 idx = decode_value (p, &p);
423 image = load_image (module, 0, TRUE);
426 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + idx);
428 case MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE:
429 idx = decode_value (p, &p);
430 image = load_image (module, decode_value (p, &p), TRUE);
433 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + idx);
435 case MONO_AOT_TYPEREF_TYPESPEC_TOKEN:
436 token = decode_value (p, &p);
437 image = module->assembly->image;
440 klass = mono_class_get (image, token);
442 case MONO_AOT_TYPEREF_GINST: {
444 MonoGenericContext ctx;
447 gclass = decode_klass_ref (module, p, &p);
450 g_assert (gclass->generic_container);
452 memset (&ctx, 0, sizeof (ctx));
453 ctx.class_inst = decode_generic_inst (module, p, &p);
456 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
457 klass = mono_class_from_mono_type (type);
458 mono_metadata_free_type (type);
461 case MONO_AOT_TYPEREF_VAR: {
463 MonoGenericContainer *container = NULL;
464 int type = decode_value (p, &p);
465 int num = decode_value (p, &p);
466 gboolean has_container = decode_value (p, &p);
470 gboolean is_method = decode_value (p, &p);
473 MonoMethod *method_def;
474 g_assert (type == MONO_TYPE_MVAR);
475 method_def = decode_resolve_method_ref (module, p, &p);
479 container = mono_method_get_generic_container (method_def);
481 MonoClass *class_def;
482 g_assert (type == MONO_TYPE_VAR);
483 class_def = decode_klass_ref (module, p, &p);
487 container = class_def->generic_container;
490 serial = decode_value (p, &p);
493 t = g_new0 (MonoType, 1);
496 t->data.generic_param = mono_generic_container_get_param (container, num);
497 g_assert (serial == 0);
500 MonoGenericParam *par = (MonoGenericParam*)mono_image_alloc0 (module->assembly->image, sizeof (MonoGenericParamFull));
502 par->serial = serial;
504 par->image = mono_defaults.corlib;
505 t->data.generic_param = par;
508 // FIXME: Maybe use types directly to avoid
509 // the overhead of creating MonoClass-es
510 klass = mono_class_from_mono_type (t);
515 case MONO_AOT_TYPEREF_ARRAY:
517 rank = decode_value (p, &p);
518 eklass = decode_klass_ref (module, p, &p);
519 klass = mono_array_class_get (eklass, rank);
521 case MONO_AOT_TYPEREF_PTR: {
524 t = decode_type (module, p, &p);
527 klass = mono_class_from_mono_type (t);
531 case MONO_AOT_TYPEREF_BLOB_INDEX: {
532 guint32 offset = decode_value (p, &p);
535 p2 = module->blob + offset;
536 klass = decode_klass_ref (module, p2, &p2);
540 g_assert_not_reached ();
543 //printf ("BLA: %s\n", mono_type_full_name (&klass->byval_arg));
548 static MonoClassField*
549 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
551 MonoClass *klass = decode_klass_ref (module, buf, &buf);
558 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
562 return mono_class_get_field (klass, token);
566 * Parse a MonoType encoded by encode_type () in aot-compiler.c. Return malloc-ed
570 decode_type (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
575 t = g_malloc0 (sizeof (MonoType));
578 if (*p == MONO_TYPE_PINNED) {
581 } else if (*p == MONO_TYPE_BYREF) {
594 case MONO_TYPE_BOOLEAN:
608 case MONO_TYPE_STRING:
609 case MONO_TYPE_OBJECT:
610 case MONO_TYPE_TYPEDBYREF:
612 case MONO_TYPE_VALUETYPE:
613 case MONO_TYPE_CLASS:
614 t->data.klass = decode_klass_ref (module, p, &p);
616 case MONO_TYPE_SZARRAY:
617 t->data.klass = decode_klass_ref (module, p, &p);
623 t->data.type = decode_type (module, p, &p);
625 case MONO_TYPE_GENERICINST: {
627 MonoGenericContext ctx;
631 gclass = decode_klass_ref (module, p, &p);
634 g_assert (gclass->generic_container);
636 memset (&ctx, 0, sizeof (ctx));
637 ctx.class_inst = decode_generic_inst (module, p, &p);
640 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
641 klass = mono_class_from_mono_type (type);
642 t->data.generic_class = klass->generic_class;
645 case MONO_TYPE_ARRAY: {
646 MonoArrayType *array;
649 // FIXME: memory management
650 array = g_new0 (MonoArrayType, 1);
651 array->eklass = decode_klass_ref (module, p, &p);
654 array->rank = decode_value (p, &p);
655 array->numsizes = decode_value (p, &p);
658 array->sizes = g_malloc0 (sizeof (int) * array->numsizes);
659 for (i = 0; i < array->numsizes; ++i)
660 array->sizes [i] = decode_value (p, &p);
662 array->numlobounds = decode_value (p, &p);
663 if (array->numlobounds)
664 array->lobounds = g_malloc0 (sizeof (int) * array->numlobounds);
665 for (i = 0; i < array->numlobounds; ++i)
666 array->lobounds [i] = decode_value (p, &p);
667 t->data.array = array;
671 case MONO_TYPE_MVAR: {
672 MonoClass *klass = decode_klass_ref (module, p, &p);
675 t->data.generic_param = klass->byval_arg.data.generic_param;
679 g_assert_not_reached ();
687 // FIXME: Error handling, memory management
689 static MonoMethodSignature*
690 decode_signature_with_target (MonoAotModule *module, MonoMethodSignature *target, guint8 *buf, guint8 **endbuf)
692 MonoMethodSignature *sig;
694 int i, param_count, call_conv, gen_param_count = 0;
696 gboolean hasthis, explicit_this, has_gen_params;
700 has_gen_params = (flags & 0x10) != 0;
701 hasthis = (flags & 0x20) != 0;
702 explicit_this = (flags & 0x40) != 0;
703 call_conv = flags & 0x0F;
706 gen_param_count = decode_value (p, &p);
707 param_count = decode_value (p, &p);
708 if (target && param_count != target->param_count)
710 sig = g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE + param_count * sizeof (MonoType *));
711 sig->param_count = param_count;
712 sig->sentinelpos = -1;
713 sig->hasthis = hasthis;
714 sig->explicit_this = explicit_this;
715 sig->call_convention = call_conv;
716 sig->param_count = param_count;
717 sig->ret = decode_type (module, p, &p);
718 for (i = 0; i < param_count; ++i) {
719 if (*p == MONO_TYPE_SENTINEL) {
720 g_assert (sig->call_convention == MONO_CALL_VARARG);
721 sig->sentinelpos = i;
724 sig->params [i] = decode_type (module, p, &p);
727 if (sig->call_convention == MONO_CALL_VARARG && sig->sentinelpos == -1)
728 sig->sentinelpos = sig->param_count;
735 static MonoMethodSignature*
736 decode_signature (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
738 return decode_signature_with_target (module, NULL, buf, endbuf);
742 sig_matches_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
744 MonoMethodSignature *sig;
748 sig = decode_signature_with_target (module, mono_method_signature (target), p, &p);
749 res = sig && mono_metadata_signature_equal (mono_method_signature (target), sig);
755 /* Stores information returned by decode_method_ref () */
760 gboolean no_aot_trampoline;
764 * decode_method_ref_with_target:
766 * Decode a method reference, storing the image/token into a MethodRef structure.
767 * This avoids loading metadata for the method if the caller does not need it. If the method has
768 * no token, then it is loaded from metadata and ref->method is set to the method instance.
769 * If TARGET is non-NULL, abort decoding if it can be determined that the decoded method
770 * couldn't resolve to TARGET, and return FALSE.
771 * There are some kinds of method references which only support a non-null TARGET.
772 * This means that its not possible to decode this into a method, only to check
773 * that the method reference matches a given method. This is normally not a problem
774 * as these wrappers only occur in the extra_methods table, where we already have
775 * a method we want to lookup.
778 decode_method_ref_with_target (MonoAotModule *module, MethodRef *ref, MonoMethod *target, guint8 *buf, guint8 **endbuf)
780 guint32 image_index, value;
781 MonoImage *image = NULL;
784 memset (ref, 0, sizeof (MethodRef));
786 value = decode_value (p, &p);
787 image_index = value >> 24;
789 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
790 ref->no_aot_trampoline = TRUE;
791 value = decode_value (p, &p);
792 image_index = value >> 24;
795 if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
796 if (target && target->wrapper_type)
800 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
801 guint32 wrapper_type;
803 wrapper_type = decode_value (p, &p);
805 if (target && target->wrapper_type != wrapper_type)
809 image = mono_defaults.corlib;
811 switch (wrapper_type) {
812 #ifndef DISABLE_REMOTING
813 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
814 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
818 mono_class_init (m->klass);
819 ref->method = mono_marshal_get_remoting_invoke_with_check (m);
822 case MONO_WRAPPER_PROXY_ISINST: {
823 MonoClass *klass = decode_klass_ref (module, p, &p);
826 ref->method = mono_marshal_get_proxy_cancast (klass);
829 case MONO_WRAPPER_LDFLD:
830 case MONO_WRAPPER_LDFLDA:
831 case MONO_WRAPPER_STFLD:
832 case MONO_WRAPPER_ISINST: {
833 MonoClass *klass = decode_klass_ref (module, p, &p);
836 if (wrapper_type == MONO_WRAPPER_LDFLD)
837 ref->method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
838 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
839 ref->method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
840 else if (wrapper_type == MONO_WRAPPER_STFLD)
841 ref->method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
842 else if (wrapper_type == MONO_WRAPPER_ISINST)
843 ref->method = mono_marshal_get_isinst (klass);
845 g_assert_not_reached ();
848 case MONO_WRAPPER_LDFLD_REMOTE:
849 ref->method = mono_marshal_get_ldfld_remote_wrapper (NULL);
851 case MONO_WRAPPER_STFLD_REMOTE:
852 ref->method = mono_marshal_get_stfld_remote_wrapper (NULL);
855 case MONO_WRAPPER_ALLOC: {
856 int atype = decode_value (p, &p);
858 ref->method = mono_gc_get_managed_allocator_by_type (atype);
859 g_assert (ref->method);
862 case MONO_WRAPPER_WRITE_BARRIER:
863 ref->method = mono_gc_get_write_barrier ();
865 case MONO_WRAPPER_STELEMREF: {
866 int subtype = decode_value (p, &p);
868 if (subtype == WRAPPER_SUBTYPE_NONE) {
869 ref->method = mono_marshal_get_stelemref ();
870 } else if (subtype == WRAPPER_SUBTYPE_VIRTUAL_STELEMREF) {
874 kind = decode_value (p, &p);
876 /* Can't decode this */
879 if (target->wrapper_type == MONO_WRAPPER_STELEMREF) {
880 info = mono_marshal_get_wrapper_info (target);
883 if (info->subtype == subtype && info->d.virtual_stelemref.kind == kind)
884 ref->method = target;
891 g_assert_not_reached ();
895 case MONO_WRAPPER_SYNCHRONIZED: {
896 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
900 ref->method = mono_marshal_get_synchronized_wrapper (m);
903 case MONO_WRAPPER_UNKNOWN: {
904 MonoMethodDesc *desc;
905 MonoMethod *orig_method;
906 int subtype = decode_value (p, &p);
908 if (subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE || subtype == WRAPPER_SUBTYPE_STRUCTURE_TO_PTR) {
909 MonoClass *klass = decode_klass_ref (module, p, &p);
916 if (klass != target->klass)
919 if (subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE) {
920 if (strcmp (target->name, "PtrToStructure"))
922 ref->method = mono_marshal_get_ptr_to_struct (klass);
924 if (strcmp (target->name, "StructureToPtr"))
926 ref->method = mono_marshal_get_struct_to_ptr (klass);
928 } else if (subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
929 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
933 ref->method = mono_marshal_get_synchronized_inner_wrapper (m);
934 } else if (subtype == WRAPPER_SUBTYPE_ARRAY_ACCESSOR) {
935 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
939 ref->method = mono_marshal_get_array_accessor_wrapper (m);
940 } else if (subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN) {
941 ref->method = mono_marshal_get_gsharedvt_in_wrapper ();
942 } else if (subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT) {
943 ref->method = mono_marshal_get_gsharedvt_out_wrapper ();
945 if (subtype == WRAPPER_SUBTYPE_FAST_MONITOR_ENTER)
946 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
947 else if (subtype == WRAPPER_SUBTYPE_FAST_MONITOR_EXIT)
948 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
949 else if (subtype == WRAPPER_SUBTYPE_FAST_MONITOR_ENTER_V4)
950 desc = mono_method_desc_new ("Monitor:Enter(object,bool&)", FALSE);
952 g_assert_not_reached ();
953 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
954 g_assert (orig_method);
955 mono_method_desc_free (desc);
956 ref->method = mono_monitor_get_fast_path (orig_method);
960 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
961 int subtype = decode_value (p, &p);
963 if (subtype == WRAPPER_SUBTYPE_ELEMENT_ADDR) {
964 int rank = decode_value (p, &p);
965 int elem_size = decode_value (p, &p);
967 ref->method = mono_marshal_get_array_address (rank, elem_size);
968 } else if (subtype == WRAPPER_SUBTYPE_STRING_CTOR) {
972 m = decode_resolve_method_ref (module, p, &p);
978 g_assert (target->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED);
980 info = mono_marshal_get_wrapper_info (target);
981 if (info && info->subtype == subtype && info->d.string_ctor.method == m)
982 ref->method = target;
988 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
990 int subtype = decode_value (p, &p);
993 if (subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER) {
998 if (strcmp (target->name, name) != 0)
1000 ref->method = target;
1002 m = decode_resolve_method_ref (module, p, &p);
1007 /* This should only happen when looking for an extra method */
1010 if (mono_marshal_method_from_wrapper (target) == m)
1011 ref->method = target;
1017 case MONO_WRAPPER_CASTCLASS: {
1018 int subtype = decode_value (p, &p);
1020 if (subtype == WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE)
1021 ref->method = mono_marshal_get_castclass_with_cache ();
1022 else if (subtype == WRAPPER_SUBTYPE_ISINST_WITH_CACHE)
1023 ref->method = mono_marshal_get_isinst_with_cache ();
1025 g_assert_not_reached ();
1028 case MONO_WRAPPER_RUNTIME_INVOKE: {
1029 int subtype = decode_value (p, &p);
1034 if (subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC) {
1035 if (strcmp (target->name, "runtime_invoke_dynamic") != 0)
1037 ref->method = target;
1038 } else if (subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT) {
1039 /* Direct wrapper */
1040 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
1044 ref->method = mono_marshal_get_runtime_invoke (m, FALSE);
1045 } else if (subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL) {
1046 /* Virtual direct wrapper */
1047 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
1051 ref->method = mono_marshal_get_runtime_invoke (m, TRUE);
1053 MonoMethodSignature *sig;
1056 sig = decode_signature_with_target (module, NULL, p, &p);
1057 info = mono_marshal_get_wrapper_info (target);
1060 if (info->subtype != subtype)
1062 g_assert (info->d.runtime_invoke.sig);
1063 if (mono_metadata_signature_equal (sig, info->d.runtime_invoke.sig))
1064 ref->method = target;
1070 case MONO_WRAPPER_DELEGATE_INVOKE:
1071 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
1072 case MONO_WRAPPER_DELEGATE_END_INVOKE: {
1073 gboolean is_inflated = decode_value (p, &p);
1074 WrapperSubtype subtype;
1078 MonoMethod *invoke, *wrapper;
1080 klass = decode_klass_ref (module, p, &p);
1084 switch (wrapper_type) {
1085 case MONO_WRAPPER_DELEGATE_INVOKE:
1086 invoke = mono_get_delegate_invoke (klass);
1087 wrapper = mono_marshal_get_delegate_invoke (invoke, NULL);
1089 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
1090 invoke = mono_get_delegate_begin_invoke (klass);
1091 wrapper = mono_marshal_get_delegate_begin_invoke (invoke);
1093 case MONO_WRAPPER_DELEGATE_END_INVOKE:
1094 invoke = mono_get_delegate_end_invoke (klass);
1095 wrapper = mono_marshal_get_delegate_end_invoke (invoke);
1098 g_assert_not_reached ();
1101 if (target && wrapper != target)
1103 ref->method = wrapper;
1106 * These wrappers are associated with a signature, not with a method.
1107 * Since we can't decode them into methods, they need a target method.
1112 if (wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
1115 subtype = decode_value (p, &p);
1116 info = mono_marshal_get_wrapper_info (target);
1118 if (info->subtype != subtype)
1121 if (subtype != WRAPPER_SUBTYPE_NONE)
1125 if (sig_matches_target (module, target, p, &p))
1126 ref->method = target;
1132 case MONO_WRAPPER_NATIVE_TO_MANAGED: {
1136 m = decode_resolve_method_ref (module, p, &p);
1139 klass = decode_klass_ref (module, p, &p);
1142 ref->method = mono_marshal_get_managed_wrapper (m, klass, 0);
1146 g_assert_not_reached ();
1148 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
1149 image_index = decode_value (p, &p);
1150 ref->token = decode_value (p, &p);
1152 image = load_image (module, image_index, TRUE);
1155 } else if (image_index == MONO_AOT_METHODREF_GINST) {
1157 MonoGenericContext ctx;
1160 * These methods do not have a token which resolves them, so we
1161 * resolve them immediately.
1163 klass = decode_klass_ref (module, p, &p);
1167 if (target && target->klass != klass)
1170 image_index = decode_value (p, &p);
1171 ref->token = decode_value (p, &p);
1173 image = load_image (module, image_index, TRUE);
1177 ref->method = mono_get_method_full (image, ref->token, NULL, NULL);
1181 memset (&ctx, 0, sizeof (ctx));
1183 if (FALSE && klass->generic_class) {
1184 ctx.class_inst = klass->generic_class->context.class_inst;
1185 ctx.method_inst = NULL;
1187 ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
1190 memset (&ctx, 0, sizeof (ctx));
1192 if (!decode_generic_context (module, &ctx, p, &p))
1195 ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
1196 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
1200 klass = decode_klass_ref (module, p, &p);
1203 method_type = decode_value (p, &p);
1204 switch (method_type) {
1206 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
1209 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
1212 ref->method = mono_class_get_method_from_name (klass, "Get", -1);
1215 ref->method = mono_class_get_method_from_name (klass, "Address", -1);
1218 ref->method = mono_class_get_method_from_name (klass, "Set", -1);
1221 g_assert_not_reached ();
1224 if (image_index == MONO_AOT_METHODREF_LARGE_IMAGE_INDEX) {
1225 image_index = decode_value (p, &p);
1226 value = decode_value (p, &p);
1229 ref->token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1231 image = load_image (module, image_index, TRUE);
1244 decode_method_ref (MonoAotModule *module, MethodRef *ref, guint8 *buf, guint8 **endbuf)
1246 return decode_method_ref_with_target (module, ref, NULL, buf, endbuf);
1250 * decode_resolve_method_ref_with_target:
1252 * Similar to decode_method_ref, but resolve and return the method itself.
1255 decode_resolve_method_ref_with_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
1260 res = decode_method_ref_with_target (module, &ref, target, buf, endbuf);
1267 return mono_get_method (ref.image, ref.token, NULL);
1271 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
1273 return decode_resolve_method_ref_with_target (module, NULL, buf, endbuf);
1276 #ifdef ENABLE_AOT_CACHE
1282 * - Add options for controlling the cache size
1283 * - Handle full cache by deleting old assemblies lru style
1284 * - Maybe add a threshold after an assembly is AOT compiled
1285 * - Add options for enabling this for specific main assemblies
1288 /* The cache directory */
1289 static char *cache_dir;
1291 /* The number of assemblies AOTed in this run */
1292 static int cache_count;
1294 /* Whenever to AOT in-process */
1295 static gboolean in_process;
1298 collect_assemblies (gpointer data, gpointer user_data)
1300 MonoAssembly *ass = data;
1301 GSList **l = user_data;
1303 *l = g_slist_prepend (*l, ass);
1306 #define SHA1_DIGEST_LENGTH 20
1309 * get_aot_config_hash:
1311 * Return a hash for all the version information an AOT module depends on.
1313 static G_GNUC_UNUSED char*
1314 get_aot_config_hash (MonoAssembly *assembly)
1317 GSList *l, *assembly_list = NULL;
1320 guint8 digest [SHA1_DIGEST_LENGTH];
1323 build_info = mono_get_runtime_build_info ();
1325 s = g_string_new (build_info);
1327 mono_assembly_foreach (collect_assemblies, &assembly_list);
1330 * The assembly list includes the current assembly as well, no need
1333 for (l = assembly_list; l; l = l->next) {
1334 MonoAssembly *ass = l->data;
1336 g_string_append (s, "_");
1337 g_string_append (s, ass->aname.name);
1338 g_string_append (s, "_");
1339 g_string_append (s, ass->image->guid);
1342 for (i = 0; i < s->len; ++i) {
1343 if (!isalnum (s->str [i]) && s->str [i] != '-')
1347 mono_sha1_get_digest ((guint8*)s->str, s->len, digest);
1349 digest_str = g_malloc0 ((SHA1_DIGEST_LENGTH * 2) + 1);
1350 for (i = 0; i < SHA1_DIGEST_LENGTH; ++i)
1351 sprintf (digest_str + (i * 2), "%02x", digest [i]);
1353 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: file dependencies: %s, hash %s", s->str, digest_str);
1355 g_string_free (s, TRUE);
1361 aot_cache_init (void)
1365 enable_aot_cache = TRUE;
1370 * aot_cache_load_module:
1372 * Load the AOT image corresponding to ASSEMBLY from the aot cache, AOTing it if neccessary.
1375 aot_cache_load_module (MonoAssembly *assembly, char **aot_name)
1377 MonoAotCacheConfig *config;
1379 char *fname, *tmp2, *aot_options, *failure_fname;
1391 if (image_is_dynamic (assembly->image))
1394 /* Check in the list of assemblies enabled for aot caching */
1395 config = mono_get_aot_cache_config ();
1399 MonoDomain *domain = mono_domain_get ();
1400 MonoAssembly *entry_assembly = domain->entry_assembly;
1402 for (l = config->apps; l; l = l->next) {
1405 if ((entry_assembly && !strcmp (entry_assembly->aname.name, n)) || (!entry_assembly && !strcmp (assembly->aname.name, n)))
1413 for (l = config->assemblies; l; l = l->next) {
1416 if (!strcmp (assembly->aname.name, n))
1426 home = g_get_home_dir ();
1429 cache_dir = g_strdup_printf ("%s/Library/Caches/mono/aot-cache", home);
1430 if (!g_file_test (cache_dir, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_DIR))
1431 g_mkdir_with_parents (cache_dir, 0777);
1435 * The same assembly can be used in multiple configurations, i.e. multiple
1436 * versions of the runtime, with multiple versions of dependent assemblies etc.
1437 * To handle this, we compute a version string containing all this information, hash it,
1438 * and use the hash as a filename suffix.
1440 hash = get_aot_config_hash (assembly);
1442 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, hash, SHARED_EXT);
1443 fname = g_build_filename (cache_dir, tmp2, NULL);
1447 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: loading from cache: '%s'.", fname);
1448 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
1453 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: not found.");
1455 if (!strcmp (assembly->aname.name, "mscorlib") && !mono_defaults.corlib)
1456 /* Can't AOT this during startup */
1459 /* Only AOT one assembly per run to avoid slowing down execution too much */
1460 if (cache_count > 0)
1464 /* Check for previous failure */
1465 failure_fname = g_strdup_printf ("%s.failure", fname);
1466 failure_file = fopen (failure_fname, "r");
1467 g_free (failure_fname);
1468 if (!failure_file) {
1469 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: assembly '%s' previously failed to compile '%s' ('%s')... ", assembly->image->name, failure_fname);
1472 fclose (failure_file);
1475 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: compiling assembly '%s'... ", assembly->image->name);
1478 * We need to invoke the AOT compiler here. There are multiple approaches:
1479 * - spawn a new runtime process. This can be hard when running with mkbundle, and
1480 * its hard to make the new process load the same set of assemblies.
1481 * - doing it in-process. This exposes the current process to bugs/leaks/side effects of
1483 * - fork a new process and do the work there.
1486 aot_options = g_strdup_printf ("outfile=%s,internal-logfile=%s.log", fname, fname);
1487 /* Maybe due this in another thread ? */
1488 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
1490 failure_fname = g_strdup_printf ("%s.failure", fname);
1491 failure_file = fopen (failure_fname, "a+");
1492 fclose (failure_file);
1493 g_free (failure_fname);
1497 * - Avoid waiting for the aot process to finish ?
1498 * (less overhead, but multiple processes could aot the same assembly at the same time)
1507 logfile_name = g_strdup_printf ("%s/aot.log", cache_dir);
1508 logfile = fopen (logfile_name, "a+");
1509 g_free (logfile_name);
1511 dup2 (fileno (logfile), 1);
1512 dup2 (fileno (logfile), 2);
1514 aot_options = g_strdup_printf ("outfile=%s", fname);
1515 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
1523 waitpid (pid, &exit_status, 0);
1524 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
1525 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: failed.");
1527 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: succeeded.");
1531 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
1539 aot_cache_init (void)
1544 aot_cache_load_module (MonoAssembly *assembly, char **aot_name)
1552 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
1556 guint16 *table, *entry;
1559 char *symbol = (char*)name;
1562 symbol = g_strdup_printf ("_%s", name);
1565 /* The first entry points to the hash */
1566 table = globals [0];
1569 table_size = table [0];
1572 hash = mono_metadata_str_hash (symbol) % table_size;
1574 entry = &table [hash * 2];
1576 /* Search the hash for the index into the globals table */
1578 while (entry [0] != 0) {
1579 guint32 index = entry [0] - 1;
1580 guint32 next = entry [1];
1582 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
1584 if (!strcmp (globals [index * 2], symbol)) {
1585 global_index = index;
1590 entry = &table [next * 2];
1596 if (global_index != -1)
1597 *value = globals [global_index * 2 + 1];
1604 char *err = mono_dl_symbol (module, name, value);
1612 check_usable (MonoAssembly *assembly, MonoAotFileInfo *info, char **out_msg)
1616 gboolean usable = TRUE;
1619 guint32 excluded_cpu_optimizations;
1621 if (strcmp (assembly->image->guid, info->assembly_guid)) {
1622 msg = g_strdup_printf ("doesn't match assembly");
1626 build_info = mono_get_runtime_build_info ();
1627 if (strlen (info->runtime_version) > 0 && strcmp (info->runtime_version, build_info)) {
1628 msg = g_strdup_printf ("compiled against runtime version '%s' while this runtime has version '%s'", info->runtime_version, build_info);
1631 g_free (build_info);
1633 full_aot = info->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1635 if (mono_aot_only && !full_aot) {
1636 msg = g_strdup_printf ("not compiled with --aot=full");
1639 if (!mono_aot_only && full_aot) {
1640 msg = g_strdup_printf ("compiled with --aot=full");
1644 /* mono_arch_find_imt_method () requires this */
1645 if ((info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1646 msg = g_strdup_printf ("compiled against LLVM");
1649 if (!(info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && mono_use_llvm) {
1650 msg = g_strdup_printf ("not compiled against LLVM");
1654 if (mini_get_debug_options ()->mdb_optimizations && !(info->flags & MONO_AOT_FILE_FLAG_DEBUG) && !full_aot) {
1655 msg = g_strdup_printf ("not compiled for debugging");
1659 mono_arch_cpu_optimizations (&excluded_cpu_optimizations);
1660 if (info->opts & excluded_cpu_optimizations) {
1661 msg = g_strdup_printf ("compiled with unsupported CPU optimizations");
1665 if (!mono_aot_only && (info->simd_opts & ~mono_arch_cpu_enumerate_simd_versions ())) {
1666 msg = g_strdup_printf ("compiled with unsupported SIMD extensions");
1672 if (info->gc_name_index != -1) {
1673 char *gc_name = (char*)&blob [info->gc_name_index];
1674 const char *current_gc_name = mono_gc_get_gc_name ();
1676 if (strcmp (current_gc_name, gc_name) != 0) {
1677 msg = g_strdup_printf ("compiled against GC %s, while the current runtime uses GC %s.\n", gc_name, current_gc_name);
1686 /* This returns an interop address */
1688 get_arm_bl_target (guint32 *ins_addr)
1691 guint32 ins = *ins_addr;
1694 if ((ins >> ARMCOND_SHIFT) == ARMCOND_NV) {
1696 offset = (((int)(((ins & 0xffffff) << 1) | ((ins >> 24) & 0x1))) << 7) >> 7;
1697 return (char*)ins_addr + (offset * 2) + 8 + 1;
1699 offset = (((int)ins & 0xffffff) << 8) >> 8;
1700 return (char*)ins_addr + (offset * 4) + 8;
1702 #elif defined(TARGET_ARM64)
1703 return mono_arch_get_call_target (((guint8*)ins_addr) + 4);
1705 g_assert_not_reached ();
1711 load_aot_module (MonoAssembly *assembly, gpointer user_data)
1714 MonoAotModule *amodule;
1716 gboolean usable = TRUE;
1717 char *version_symbol = NULL;
1719 gpointer *globals = NULL;
1720 MonoAotFileInfo *info = NULL;
1723 gboolean do_load_image = TRUE;
1724 int align_double, align_int64;
1726 if (mono_compile_aot)
1729 if (assembly->image->aot_module)
1731 * Already loaded. This can happen because the assembly loading code might invoke
1732 * the assembly load hooks multiple times for the same assembly.
1736 if (image_is_dynamic (assembly->image) || assembly->ref_only)
1739 if (mono_security_cas_enabled ())
1742 if (enable_aot_cache && !strcmp (assembly->aname.name, "mscorlib") && !mono_defaults.corlib && !mono_aot_only)
1743 /* Loaded later from mono_aot_get_method () */
1747 if (static_aot_modules)
1748 info = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
1756 /* Statically linked AOT module */
1757 aot_name = g_strdup_printf ("%s", assembly->aname.name);
1758 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
1759 globals = info->globals;
1761 if (enable_aot_cache)
1762 sofile = aot_cache_load_module (assembly, &aot_name);
1765 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
1767 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1770 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module '%s' not found: %s\n", aot_name, err);
1776 if (!sofile && !globals) {
1777 if (mono_aot_only && assembly->image->tables [MONO_TABLE_METHOD].rows) {
1778 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1786 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &version_symbol);
1787 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&info);
1790 if (version_symbol) {
1791 /* Old file format */
1792 version = atoi (version_symbol);
1795 version = info->version;
1798 if (version != MONO_AOT_FILE_VERSION) {
1799 msg = g_strdup_printf ("wrong file format version (expected %d got %d)", MONO_AOT_FILE_VERSION, version);
1802 usable = check_usable (assembly, info, &msg);
1806 if (mono_aot_only) {
1807 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode: %s.\n", aot_name, msg);
1810 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: module %s is unusable: %s.\n", aot_name, msg);
1815 mono_dl_close (sofile);
1816 assembly->image->aot_module = NULL;
1820 #if defined (TARGET_ARM) && defined (TARGET_MACH)
1825 memset (&t, 0, sizeof (MonoType));
1826 t.type = MONO_TYPE_R8;
1827 mono_type_size (&t, &align);
1828 align_double = align;
1830 memset (&t, 0, sizeof (MonoType));
1831 t.type = MONO_TYPE_I8;
1832 align_int64 = align;
1835 align_double = MONO_ABI_ALIGNOF (double);
1836 align_int64 = MONO_ABI_ALIGNOF (gint64);
1840 g_assert (info->double_align == align_double);
1841 g_assert (info->long_align == align_int64);
1842 g_assert (info->generic_tramp_num == MONO_TRAMPOLINE_NUM);
1846 amodule = g_new0 (MonoAotModule, 1);
1847 amodule->aot_name = aot_name;
1848 amodule->assembly = assembly;
1850 memcpy (&amodule->info, info, sizeof (*info));
1852 amodule->got = amodule->info.got;
1853 amodule->got [0] = assembly->image;
1854 amodule->globals = globals;
1855 amodule->sofile = sofile;
1856 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1857 amodule->blob = blob;
1859 mono_mutex_init_recursive (&amodule->mutex);
1861 /* Read image table */
1863 guint32 table_len, i;
1866 table = info->image_table;
1869 table_len = *(guint32*)table;
1870 table += sizeof (guint32);
1871 amodule->image_table = g_new0 (MonoImage*, table_len);
1872 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1873 amodule->image_guids = g_new0 (char*, table_len);
1874 amodule->image_table_len = table_len;
1875 for (i = 0; i < table_len; ++i) {
1876 MonoAssemblyName *aname = &(amodule->image_names [i]);
1878 aname->name = g_strdup (table);
1879 table += strlen (table) + 1;
1880 amodule->image_guids [i] = g_strdup (table);
1881 table += strlen (table) + 1;
1883 aname->culture = g_strdup (table);
1884 table += strlen (table) + 1;
1885 memcpy (aname->public_key_token, table, strlen (table) + 1);
1886 table += strlen (table) + 1;
1888 table = ALIGN_PTR_TO (table, 8);
1889 aname->flags = *(guint32*)table;
1891 aname->major = *(guint32*)table;
1893 aname->minor = *(guint32*)table;
1895 aname->build = *(guint32*)table;
1897 aname->revision = *(guint32*)table;
1902 amodule->code_offsets = info->code_offsets;
1903 amodule->method_addresses = info->method_addresses;
1904 amodule->code = info->methods;
1906 /* Mask out thumb interop bit */
1907 amodule->code = (void*)((mgreg_t)amodule->code & ~1);
1909 amodule->code_end = info->methods_end;
1910 amodule->method_info_offsets = info->method_info_offsets;
1911 amodule->ex_info_offsets = info->ex_info_offsets;
1912 amodule->class_info_offsets = info->class_info_offsets;
1913 amodule->class_name_table = info->class_name_table;
1914 amodule->extra_method_table = info->extra_method_table;
1915 amodule->extra_method_info_offsets = info->extra_method_info_offsets;
1916 amodule->unbox_trampolines = info->unbox_trampolines;
1917 amodule->unbox_trampolines_end = info->unbox_trampolines_end;
1918 amodule->got_info_offsets = info->got_info_offsets;
1919 amodule->unwind_info = info->unwind_info;
1920 amodule->mem_end = info->mem_end;
1921 amodule->mem_begin = amodule->code;
1922 amodule->plt = info->plt;
1923 amodule->plt_end = info->plt_end;
1924 amodule->mono_eh_frame = info->mono_eh_frame;
1925 amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC] = info->specific_trampolines;
1926 amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = info->static_rgctx_trampolines;
1927 amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK] = info->imt_thunks;
1928 amodule->trampolines [MONO_AOT_TRAMP_GSHAREDVT_ARG] = info->gsharedvt_arg_trampolines;
1929 amodule->thumb_end = info->thumb_end;
1931 if (info->flags & MONO_AOT_FILE_FLAG_DIRECT_METHOD_ADDRESSES) {
1932 /* Compute code_offsets from the method addresses */
1933 amodule->code_offsets = g_malloc0 (amodule->info.nmethods * sizeof (gint32));
1934 for (i = 0; i < amodule->info.nmethods; ++i) {
1935 /* method_addresses () contains a table of branches, since the ios linker can update those correctly */
1938 #if defined(TARGET_ARM) || defined(TARGET_ARM64)
1939 addr = get_arm_bl_target ((guint32*)amodule->method_addresses + i);
1940 #elif defined(TARGET_X86) || defined(TARGET_AMD64)
1941 addr = mono_arch_get_call_target ((guint8*)amodule->method_addresses + (i * 5) + 5);
1943 g_assert_not_reached ();
1946 if (addr == amodule->method_addresses)
1947 amodule->code_offsets [i] = 0xffffffff;
1949 amodule->code_offsets [i] = (char*)addr - (char*)amodule->code;
1953 if (make_unreadable) {
1954 #ifndef TARGET_WIN32
1956 guint8 *page_start, *page_end;
1959 addr = amodule->mem_begin;
1960 len = amodule->mem_end - amodule->mem_begin;
1962 /* Round down in both directions to avoid modifying data which is not ours */
1963 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1964 page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
1965 if (page_end > page_start) {
1966 err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
1967 g_assert (err == 0);
1974 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1975 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1977 g_hash_table_insert (aot_modules, assembly, amodule);
1980 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1982 assembly->image->aot_module = amodule;
1984 if (mono_aot_only) {
1986 find_symbol (amodule->sofile, amodule->globals, "specific_trampolines_page", (gpointer *)&code);
1987 amodule->use_page_trampolines = code != NULL;
1988 /*g_warning ("using page trampolines: %d", amodule->use_page_trampolines);*/
1989 if (mono_defaults.corlib) {
1990 /* The second got slot contains the mscorlib got addr */
1991 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1993 amodule->got [1] = mscorlib_amodule->got;
1995 amodule->got [1] = amodule->got;
1999 if (mono_gc_is_moving ()) {
2002 memset (&ji, 0, sizeof (ji));
2003 ji.type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
2005 amodule->got [2] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
2009 * Since we store methoddef and classdef tokens when referring to methods/classes in
2010 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
2011 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
2012 * non-lazily, since we can't handle out-of-date errors later.
2013 * The cached class info also depends on the exact assemblies.
2015 #if defined(__native_client__)
2016 /* TODO: Don't 'load_image' on mscorlib due to a */
2017 /* recursive loading problem. This should be */
2018 /* removed if mscorlib is loaded from disk. */
2019 if (strncmp(assembly->aname.name, "mscorlib", 8)) {
2020 do_load_image = TRUE;
2022 do_load_image = FALSE;
2025 if (do_load_image) {
2026 for (i = 0; i < amodule->image_table_len; ++i)
2027 load_image (amodule, i, FALSE);
2030 if (amodule->out_of_date) {
2031 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);
2032 if (mono_aot_only) {
2033 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);
2038 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: loaded AOT Module for %s.\n", assembly->image->name);
2042 * mono_aot_register_globals:
2044 * This is called by the ctor function in AOT images compiled with the
2045 * 'no-dlsym' option.
2048 mono_aot_register_globals (gpointer *globals)
2050 g_assert_not_reached ();
2054 * mono_aot_register_module:
2056 * This should be called by embedding code to register AOT modules statically linked
2057 * into the executable. AOT_INFO should be the value of the
2058 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
2061 mono_aot_register_module (gpointer *aot_info)
2065 MonoAotFileInfo *info = (gpointer)aot_info;
2067 g_assert (info->version == MONO_AOT_FILE_VERSION);
2069 globals = info->globals;
2072 aname = info->assembly_name;
2074 /* This could be called before startup */
2078 if (!static_aot_modules)
2079 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
2081 g_hash_table_insert (static_aot_modules, aname, info);
2088 mono_aot_init (void)
2090 mono_mutex_init_recursive (&aot_mutex);
2091 mono_mutex_init_recursive (&aot_page_mutex);
2092 aot_modules = g_hash_table_new (NULL, NULL);
2094 #ifndef __native_client__
2095 mono_install_assembly_load_hook (load_aot_module, NULL);
2097 mono_counters_register ("Async JIT info size", MONO_COUNTER_INT|MONO_COUNTER_JIT, &async_jit_info_size);
2099 if (g_getenv ("MONO_LASTAOT"))
2100 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
2101 #ifdef ENABLE_AOT_CACHE
2107 mono_aot_cleanup (void)
2109 if (aot_jit_icall_hash)
2110 g_hash_table_destroy (aot_jit_icall_hash);
2112 g_hash_table_destroy (aot_modules);
2116 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
2122 info->vtable_size = decode_value (buf, &buf);
2123 if (info->vtable_size == -1)
2126 flags = decode_value (buf, &buf);
2127 info->ghcimpl = (flags >> 0) & 0x1;
2128 info->has_finalize = (flags >> 1) & 0x1;
2129 info->has_cctor = (flags >> 2) & 0x1;
2130 info->has_nested_classes = (flags >> 3) & 0x1;
2131 info->blittable = (flags >> 4) & 0x1;
2132 info->has_references = (flags >> 5) & 0x1;
2133 info->has_static_refs = (flags >> 6) & 0x1;
2134 info->no_special_static_fields = (flags >> 7) & 0x1;
2135 info->is_generic_container = (flags >> 8) & 0x1;
2137 if (info->has_cctor) {
2138 res = decode_method_ref (module, &ref, buf, &buf);
2141 info->cctor_token = ref.token;
2143 if (info->has_finalize) {
2144 res = decode_method_ref (module, &ref, buf, &buf);
2147 info->finalize_image = ref.image;
2148 info->finalize_token = ref.token;
2151 info->instance_size = decode_value (buf, &buf);
2152 info->class_size = decode_value (buf, &buf);
2153 info->packing_size = decode_value (buf, &buf);
2154 info->min_align = decode_value (buf, &buf);
2162 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2165 MonoClass *klass = vtable->klass;
2166 MonoAotModule *amodule = klass->image->aot_module;
2168 MonoCachedClassInfo class_info;
2173 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !amodule)
2176 info = &amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
2179 err = decode_cached_class_info (amodule, &class_info, p, &p);
2183 for (i = 0; i < slot; ++i)
2184 decode_method_ref (amodule, &ref, p, &p);
2186 res = decode_method_ref (amodule, &ref, p, &p);
2189 if (ref.no_aot_trampoline)
2192 if (mono_metadata_token_index (ref.token) == 0 || mono_metadata_token_table (ref.token) != MONO_TABLE_METHOD)
2195 return mono_aot_get_method_from_token (domain, ref.image, ref.token);
2199 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2201 MonoAotModule *amodule = klass->image->aot_module;
2205 if (klass->rank || !amodule)
2208 p = (guint8*)&amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
2210 err = decode_cached_class_info (amodule, res, p, &p);
2218 * mono_aot_get_class_from_name:
2220 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
2221 * using a cache stored in the AOT file.
2222 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
2224 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
2228 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2230 MonoAotModule *amodule = image->aot_module;
2231 guint16 *table, *entry;
2234 char full_name_buf [1024];
2236 const char *name2, *name_space2;
2238 guint32 cols [MONO_TYPEDEF_SIZE];
2239 GHashTable *nspace_table;
2241 if (!amodule || !amodule->class_name_table)
2244 amodule_lock (amodule);
2248 /* First look in the cache */
2249 if (!amodule->name_cache)
2250 amodule->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
2251 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
2253 *klass = g_hash_table_lookup (nspace_table, name);
2255 amodule_unlock (amodule);
2260 table_size = amodule->class_name_table [0];
2261 table = amodule->class_name_table + 1;
2263 if (name_space [0] == '\0')
2264 full_name = g_strdup_printf ("%s", name);
2266 if (strlen (name_space) + strlen (name) < 1000) {
2267 sprintf (full_name_buf, "%s.%s", name_space, name);
2268 full_name = full_name_buf;
2270 full_name = g_strdup_printf ("%s.%s", name_space, name);
2273 hash = mono_metadata_str_hash (full_name) % table_size;
2274 if (full_name != full_name_buf)
2277 entry = &table [hash * 2];
2279 if (entry [0] != 0) {
2280 t = &image->tables [MONO_TABLE_TYPEDEF];
2283 guint32 index = entry [0];
2284 guint32 next = entry [1];
2285 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
2287 name_table_accesses ++;
2289 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
2291 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2292 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2294 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
2295 amodule_unlock (amodule);
2296 *klass = mono_class_get (image, token);
2300 amodule_lock (amodule);
2301 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
2302 if (!nspace_table) {
2303 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
2304 g_hash_table_insert (amodule->name_cache, (char*)name_space2, nspace_table);
2306 g_hash_table_insert (nspace_table, (char*)name2, *klass);
2307 amodule_unlock (amodule);
2313 entry = &table [next * 2];
2320 amodule_unlock (amodule);
2326 * decode_mono_eh_frame:
2328 * Decode the EH information emitted by our modified LLVM compiler and construct a
2329 * MonoJitInfo structure from it.
2330 * LOCKING: Acquires the domain lock.
2333 decode_llvm_mono_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
2334 MonoMethod *method, guint8 *code,
2335 MonoJitExceptionInfo *clauses, int num_clauses,
2336 int extra_size, GSList **nesting,
2337 int *this_reg, int *this_offset)
2340 guint8 *fde, *cie, *code_start, *code_end;
2341 int version, fde_count;
2343 int i, j, pos, left, right, offset, offset1, offset2, code_len, func_encoding;
2344 MonoJitExceptionInfo *ei;
2345 guint32 fde_len, ei_len, nested_len, nindex;
2346 gpointer *type_info;
2348 MonoLLVMFDEInfo info;
2350 g_assert (amodule->mono_eh_frame);
2352 p = amodule->mono_eh_frame;
2354 /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
2358 g_assert (version == 3);
2362 p = ALIGN_PTR_TO (p, 4);
2364 fde_count = *(guint32*)p;
2368 /* There is +1 entry in the table */
2369 cie = p + ((fde_count + 1) * 8);
2371 /* Binary search in the table to find the entry for code */
2372 offset = code - amodule->code;
2376 pos = (left + right) / 2;
2378 /* The table contains method index/fde offset pairs */
2379 g_assert (table [(pos * 2)] != -1);
2380 offset1 = amodule->code_offsets [table [(pos * 2)]];
2381 if (pos + 1 == fde_count) {
2382 offset2 = amodule->code_end - amodule->code;
2384 g_assert (table [(pos + 1) * 2] != -1);
2385 offset2 = amodule->code_offsets [table [(pos + 1) * 2]];
2388 if (offset < offset1)
2390 else if (offset >= offset2)
2396 code_start = amodule->code + amodule->code_offsets [table [(pos * 2)]];
2397 if (pos + 1 == fde_count) {
2398 /* The +1 entry in the table contains the length of the last method */
2399 int len = table [(pos + 1) * 2];
2400 code_end = code_start + len;
2402 code_end = amodule->code + amodule->code_offsets [table [(pos + 1) * 2]];
2404 code_len = code_end - code_start;
2406 g_assert (code >= code_start && code < code_end);
2408 if (amodule->thumb_end && (guint8*)code_start < amodule->thumb_end)
2409 /* Clear thumb flag */
2410 code_start = (guint8*)(((mgreg_t)code_start) & ~1);
2412 fde = amodule->mono_eh_frame + table [(pos * 2) + 1];
2413 /* This won't overflow because there is +1 entry in the table */
2414 fde_len = table [(pos * 2) + 2 + 1] - table [(pos * 2) + 1];
2416 mono_unwind_decode_llvm_mono_fde (fde, fde_len, cie, code_start, &info);
2418 ei_len = info.ex_info_len;
2419 type_info = info.type_info;
2420 *this_reg = info.this_reg;
2421 *this_offset = info.this_offset;
2423 /* Count number of nested clauses */
2425 for (i = 0; i < ei_len; ++i) {
2426 /* This might be unaligned */
2427 gint32 cindex1 = read32 (type_info [i]);
2430 for (l = nesting [cindex1]; l; l = l->next) {
2431 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
2433 for (j = 0; j < ei_len; ++j) {
2434 gint32 cindex2 = read32 (type_info [j]);
2436 if (cindex2 == nesting_cindex)
2443 * LLVM might represent one IL region with multiple regions, so have to
2444 * allocate a new JI.
2447 mono_domain_alloc0_lock_free (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * (ei_len + nested_len)) + extra_size);
2449 jinfo->code_size = code_len;
2450 jinfo->unwind_info = mono_cache_unwind_info (info.unw_info, info.unw_info_len);
2451 jinfo->d.method = method;
2452 jinfo->code_start = code;
2453 jinfo->domain_neutral = 0;
2454 /* This signals that unwind_info points to a normal cached unwind info */
2455 jinfo->from_aot = 0;
2456 jinfo->num_clauses = ei_len + nested_len;
2458 for (i = 0; i < ei_len; ++i) {
2460 * orig_jinfo contains the original IL exception info saved by the AOT
2461 * compiler, we have to combine that with the information produced by LLVM
2463 /* The type_info entries contain IL clause indexes */
2464 int clause_index = read32 (type_info [i]);
2465 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
2466 MonoJitExceptionInfo *orig_jei = &clauses [clause_index];
2468 g_assert (clause_index < num_clauses);
2469 jei->flags = orig_jei->flags;
2470 jei->data.catch_class = orig_jei->data.catch_class;
2472 jei->try_start = ei [i].try_start;
2473 jei->try_end = ei [i].try_end;
2474 jei->handler_start = ei [i].handler_start;
2476 /* Make sure we transition to thumb when a handler starts */
2477 if (amodule->thumb_end && (guint8*)jei->handler_start < amodule->thumb_end)
2478 jei->handler_start = (void*)((mgreg_t)jei->handler_start + 1);
2481 /* See exception_cb () in mini-llvm.c as to why this is needed */
2483 for (i = 0; i < ei_len; ++i) {
2484 gint32 cindex1 = read32 (type_info [i]);
2487 for (l = nesting [cindex1]; l; l = l->next) {
2488 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
2490 for (j = 0; j < ei_len; ++j) {
2491 gint32 cindex2 = read32 (type_info [j]);
2493 if (cindex2 == nesting_cindex) {
2495 * The try interval comes from the nested clause, everything else from the
2498 memcpy (&jinfo->clauses [nindex], &jinfo->clauses [j], sizeof (MonoJitExceptionInfo));
2499 jinfo->clauses [nindex].try_start = jinfo->clauses [i].try_start;
2500 jinfo->clauses [nindex].try_end = jinfo->clauses [i].try_end;
2506 g_assert (nindex == ei_len + nested_len);
2512 alloc0_jit_info_data (MonoDomain *domain, int size, gboolean async_context)
2516 if (async_context) {
2517 res = mono_domain_alloc0_lock_free (domain, size);
2518 InterlockedExchangeAdd (&async_jit_info_size, size);
2520 res = mono_domain_alloc0 (domain, size);
2526 * LOCKING: Acquires the domain lock.
2527 * In async context, this is async safe.
2530 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
2531 MonoMethod *method, guint8* ex_info, guint8 *addr,
2532 guint8 *code, guint32 code_len)
2534 int i, buf_len, num_clauses, len;
2536 guint unwind_info, flags;
2537 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points, has_try_block_holes, has_arch_eh_jit_info;
2538 gboolean from_llvm, has_gc_map;
2540 int generic_info_size, try_holes_info_size, num_holes, arch_eh_jit_info_size;
2541 int this_reg = 0, this_offset = 0;
2544 /* Load the method info from the AOT file */
2545 async = mono_thread_info_is_async_context ();
2548 flags = decode_value (p, &p);
2549 has_generic_jit_info = (flags & 1) != 0;
2550 has_dwarf_unwind_info = (flags & 2) != 0;
2551 has_clauses = (flags & 4) != 0;
2552 has_seq_points = (flags & 8) != 0;
2553 from_llvm = (flags & 16) != 0;
2554 has_try_block_holes = (flags & 32) != 0;
2555 has_gc_map = (flags & 64) != 0;
2556 has_arch_eh_jit_info = (flags & 128) != 0;
2558 if (has_dwarf_unwind_info) {
2559 unwind_info = decode_value (p, &p);
2560 g_assert (unwind_info < (1 << 30));
2562 unwind_info = decode_value (p, &p);
2564 if (has_generic_jit_info)
2565 generic_info_size = sizeof (MonoGenericJitInfo);
2567 generic_info_size = 0;
2569 if (has_try_block_holes) {
2570 num_holes = decode_value (p, &p);
2571 try_holes_info_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
2573 num_holes = try_holes_info_size = 0;
2575 /* Exception table */
2577 num_clauses = decode_value (p, &p);
2580 if (has_arch_eh_jit_info)
2581 arch_eh_jit_info_size = sizeof (MonoArchEHJitInfo);
2583 arch_eh_jit_info_size = 0;
2586 MonoJitExceptionInfo *clauses;
2593 * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
2596 clauses = g_new0 (MonoJitExceptionInfo, num_clauses);
2597 nesting = g_new0 (GSList*, num_clauses);
2599 for (i = 0; i < num_clauses; ++i) {
2600 MonoJitExceptionInfo *ei = &clauses [i];
2602 ei->flags = decode_value (p, &p);
2604 if (decode_value (p, &p))
2605 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
2607 /* Read the list of nesting clauses */
2609 int nesting_index = decode_value (p, &p);
2610 if (nesting_index == -1)
2612 nesting [i] = g_slist_prepend (nesting [i], GINT_TO_POINTER (nesting_index));
2616 jinfo = decode_llvm_mono_eh_frame (amodule, domain, method, code, clauses, num_clauses, generic_info_size + try_holes_info_size + arch_eh_jit_info_size, nesting, &this_reg, &this_offset);
2617 jinfo->from_llvm = 1;
2620 for (i = 0; i < num_clauses; ++i)
2621 g_slist_free (nesting [i]);
2624 len = MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size + try_holes_info_size + arch_eh_jit_info_size;
2625 jinfo = alloc0_jit_info_data (domain, len, async);
2626 jinfo->num_clauses = num_clauses;
2628 for (i = 0; i < jinfo->num_clauses; ++i) {
2629 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
2631 ei->flags = decode_value (p, &p);
2633 ei->exvar_offset = decode_value (p, &p);
2635 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
2636 ei->data.filter = code + decode_value (p, &p);
2638 int len = decode_value (p, &p);
2644 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
2648 ei->try_start = code + decode_value (p, &p);
2649 ei->try_end = code + decode_value (p, &p);
2650 ei->handler_start = code + decode_value (p, &p);
2653 jinfo->code_size = code_len;
2654 jinfo->unwind_info = unwind_info;
2655 jinfo->d.method = method;
2656 jinfo->code_start = code;
2657 jinfo->domain_neutral = 0;
2658 jinfo->from_aot = 1;
2662 * Set all the 'has' flags, the mono_jit_info_get () functions depends on this to
2663 * compute the addresses of data blocks.
2665 if (has_generic_jit_info)
2666 jinfo->has_generic_jit_info = 1;
2667 if (has_arch_eh_jit_info)
2668 jinfo->has_arch_eh_info = 1;
2669 if (has_try_block_holes)
2670 jinfo->has_try_block_holes = 1;
2672 if (has_try_block_holes) {
2673 MonoTryBlockHoleTableJitInfo *table;
2675 g_assert (jinfo->has_try_block_holes);
2677 table = mono_jit_info_get_try_block_hole_table_info (jinfo);
2680 table->num_holes = (guint16)num_holes;
2681 for (i = 0; i < num_holes; ++i) {
2682 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
2683 hole->clause = decode_value (p, &p);
2684 hole->length = decode_value (p, &p);
2685 hole->offset = decode_value (p, &p);
2689 if (has_arch_eh_jit_info) {
2690 MonoArchEHJitInfo *eh_info;
2692 g_assert (jinfo->has_arch_eh_info);
2694 eh_info = mono_jit_info_get_arch_eh_info (jinfo);
2695 eh_info->stack_size = decode_value (p, &p);
2696 eh_info->epilog_size = decode_value (p, &p);
2700 /* The rest is not needed in async mode */
2701 jinfo->async = TRUE;
2702 jinfo->d.aot_info = amodule;
2707 if (has_generic_jit_info) {
2708 MonoGenericJitInfo *gi;
2711 g_assert (jinfo->has_generic_jit_info);
2713 gi = mono_jit_info_get_generic_jit_info (jinfo);
2716 gi->nlocs = decode_value (p, &p);
2718 gi->locations = alloc0_jit_info_data (domain, gi->nlocs * sizeof (MonoDwarfLocListEntry), async);
2719 for (i = 0; i < gi->nlocs; ++i) {
2720 MonoDwarfLocListEntry *entry = &gi->locations [i];
2722 entry->is_reg = decode_value (p, &p);
2723 entry->reg = decode_value (p, &p);
2725 entry->offset = decode_value (p, &p);
2727 entry->from = decode_value (p, &p);
2728 entry->to = decode_value (p, &p);
2732 gi->has_this = this_reg != -1;
2733 gi->this_reg = this_reg;
2734 gi->this_offset = this_offset;
2736 gi->has_this = decode_value (p, &p);
2737 gi->this_reg = decode_value (p, &p);
2738 gi->this_offset = decode_value (p, &p);
2742 len = decode_value (p, &p);
2746 jinfo->d.method = decode_resolve_method_ref (amodule, p, &p);
2748 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
2749 if (decode_value (p, &p)) {
2752 MonoGenericSharingContext *gsctx = gi->generic_sharing_context;
2754 n = decode_value (p, &p);
2756 gsctx->var_is_vt = alloc0_jit_info_data (domain, sizeof (gboolean) * n, async);
2757 for (i = 0; i < n; ++i)
2758 gsctx->var_is_vt [i] = decode_value (p, &p);
2760 n = decode_value (p, &p);
2762 gsctx->mvar_is_vt = alloc0_jit_info_data (domain, sizeof (gboolean) * n, async);
2763 for (i = 0; i < n; ++i)
2764 gsctx->mvar_is_vt [i] = decode_value (p, &p);
2769 if (method && has_seq_points) {
2770 MonoSeqPointInfo *seq_points;
2771 int il_offset, native_offset, last_il_offset, last_native_offset, j;
2773 int len = decode_value (p, &p);
2775 seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
2776 seq_points->len = len;
2777 last_il_offset = last_native_offset = 0;
2778 for (i = 0; i < len; ++i) {
2779 SeqPoint *sp = &seq_points->seq_points [i];
2780 il_offset = last_il_offset + decode_value (p, &p);
2781 native_offset = last_native_offset + decode_value (p, &p);
2783 sp->il_offset = il_offset;
2784 sp->native_offset = native_offset;
2786 sp->flags = decode_value (p, &p);
2787 sp->next_len = decode_value (p, &p);
2788 sp->next = g_new (int, sp->next_len);
2789 for (j = 0; j < sp->next_len; ++j)
2790 sp->next [j] = decode_value (p, &p);
2792 last_il_offset = il_offset;
2793 last_native_offset = native_offset;
2796 mono_domain_lock (domain);
2797 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
2798 mono_domain_unlock (domain);
2801 /* Load debug info */
2802 buf_len = decode_value (p, &p);
2804 mono_debug_add_aot_method (domain, method, code, p, buf_len);
2808 int map_size = decode_value (p, &p);
2809 /* The GC map requires 4 bytes of alignment */
2810 while ((guint64)(gsize)p % 4)
2816 if (amodule != jinfo->d.method->klass->image->aot_module) {
2819 ji_to_amodule = g_hash_table_new (NULL, NULL);
2820 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
2828 * mono_aot_get_unwind_info:
2830 * Return a pointer to the DWARF unwind info belonging to JI.
2833 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2835 MonoAotModule *amodule;
2837 guint8 *code = ji->code_start;
2840 amodule = ji->d.aot_info;
2842 amodule = jinfo_get_method (ji)->klass->image->aot_module;
2844 g_assert (ji->from_aot);
2846 if (!(code >= amodule->code && code <= amodule->code_end)) {
2847 /* ji belongs to a different aot module than amodule */
2849 g_assert (ji_to_amodule);
2850 amodule = g_hash_table_lookup (ji_to_amodule, ji);
2852 g_assert (code >= amodule->code && code <= amodule->code_end);
2856 p = amodule->unwind_info + ji->unwind_info;
2857 *unwind_info_len = decode_value (p, &p);
2861 static G_GNUC_UNUSED int
2862 compare_ints (const void *a, const void *b)
2864 return *(gint32*)a - *(gint32*)b;
2868 msort_code_offsets_internal (gint32 *array, int lo, int hi, gint32 *scratch)
2870 int mid = (lo + hi) / 2;
2877 for (i = lo; i < hi; ++i)
2878 if (array [(i * 2)] > array [(i * 2) + 2])
2881 /* Already sorted */
2885 msort_code_offsets_internal (array, lo, mid, scratch);
2886 msort_code_offsets_internal (array, mid + 1, hi, scratch);
2888 if (array [mid * 2] < array [(mid + 1) * 2])
2894 for (i = lo; i <= hi; i ++) {
2895 if (t_lo <= mid && ((t_hi > hi) || array [t_lo * 2] < array [t_hi * 2])) {
2896 scratch [(i * 2)] = array [t_lo * 2];
2897 scratch [(i * 2) + 1] = array [(t_lo *2) + 1];
2900 scratch [(i * 2)] = array [t_hi * 2];
2901 scratch [(i * 2) + 1] = array [(t_hi *2) + 1];
2905 for (i = lo; i <= hi; ++i) {
2906 array [(i * 2)] = scratch [i * 2];
2907 array [(i * 2) + 1] = scratch [(i * 2) + 1];
2912 msort_code_offsets (gint32 *array, int len)
2916 scratch = g_new (gint32, len * 2);
2917 msort_code_offsets_internal (array, 0, len - 1, scratch);
2922 * mono_aot_find_jit_info:
2924 * In async context, the resulting MonoJitInfo will not have its method field set, and it will not be added
2925 * to the jit info tables.
2926 * FIXME: Large sizes in the lock free allocator
2929 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2931 int pos, left, right, offset, offset1, offset2, code_len;
2932 int method_index, table_len;
2934 MonoAotModule *amodule = image->aot_module;
2935 MonoMethod *method = NULL;
2937 guint8 *code, *ex_info, *p;
2940 gint32 *code_offsets;
2947 nmethods = amodule->info.nmethods;
2949 if (domain != mono_get_root_domain ())
2953 async = mono_thread_info_is_async_context ();
2955 offset = (guint8*)addr - amodule->code;
2957 /* Compute a sorted table mapping code offsets to method indexes. */
2958 if (!amodule->sorted_code_offsets) {
2960 code_offsets = g_new0 (gint32, nmethods * 2);
2962 for (i = 0; i < nmethods; ++i) {
2963 /* Skip the -1 entries to speed up sorting */
2964 if (amodule->code_offsets [i] == 0xffffffff)
2966 code_offsets [(offsets_len * 2)] = amodule->code_offsets [i];
2967 code_offsets [(offsets_len *2) + 1] = i;
2970 /* Use a merge sort as this is mostly sorted */
2971 msort_code_offsets (code_offsets, offsets_len);
2972 //qsort (code_offsets, offsets_len, sizeof (gint32) * 2, compare_ints);
2973 for (i = 0; i < offsets_len -1; ++i)
2974 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
2976 amodule->sorted_code_offsets_len = offsets_len;
2977 mono_memory_barrier ();
2978 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
2979 /* Somebody got in before us */
2980 g_free (code_offsets);
2983 code_offsets = amodule->sorted_code_offsets;
2984 offsets_len = amodule->sorted_code_offsets_len;
2986 if (offsets_len > 0 && (offset < code_offsets [0] || offset >= (amodule->code_end - amodule->code)))
2989 /* Binary search in the sorted_code_offsets table */
2991 right = offsets_len;
2993 pos = (left + right) / 2;
2995 offset1 = code_offsets [(pos * 2)];
2996 if (pos + 1 == offsets_len)
2997 offset2 = amodule->code_end - amodule->code;
2999 offset2 = code_offsets [(pos + 1) * 2];
3001 if (offset < offset1)
3003 else if (offset >= offset2)
3009 g_assert (offset >= code_offsets [(pos * 2)]);
3010 if (pos + 1 < offsets_len)
3011 g_assert (offset < code_offsets [((pos + 1) * 2)]);
3012 method_index = code_offsets [(pos * 2) + 1];
3014 /* In async mode, jinfo is not added to the normal jit info table, so have to cache it ourselves */
3016 JitInfoMap *table = amodule->async_jit_info_table;
3020 len = table [0].method_index;
3021 for (i = 1; i < len; ++i) {
3022 if (table [i].method_index == method_index)
3023 return table [i].jinfo;
3028 code = &amodule->code [amodule->code_offsets [method_index]];
3029 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
3031 if (pos == offsets_len - 1)
3032 code_len = amodule->code_end - code;
3034 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
3036 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
3038 /* Might be a wrapper/extra method */
3040 if (amodule->extra_methods) {
3041 amodule_lock (amodule);
3042 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
3043 amodule_unlock (amodule);
3049 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
3051 * This is hit for extra methods which are called directly, so they are
3052 * not in amodule->extra_methods.
3054 table_len = amodule->extra_method_info_offsets [0];
3055 table = amodule->extra_method_info_offsets + 1;
3062 pos = ((left + right) / 2);
3064 g_assert (pos < table_len);
3066 if (table [pos * 2] < method_index)
3068 else if (table [pos * 2] > method_index)
3074 p = amodule->blob + table [(pos * 2) + 1];
3075 method = decode_resolve_method_ref (amodule, p, &p);
3077 /* Happens when a random address is passed in which matches a not-yey called wrapper encoded using its name */
3080 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
3081 method = mono_get_method (image, token, NULL);
3088 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
3090 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
3092 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
3093 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
3095 /* Add it to the normal JitInfo tables */
3097 JitInfoMap *old_table, *new_table;
3101 * Use a simple inmutable table with linear search to cache async jit info entries.
3102 * This assumes that the number of entries is small.
3105 /* Copy the table, adding a new entry at the end */
3106 old_table = amodule->async_jit_info_table;
3108 len = old_table[0].method_index;
3111 new_table = alloc0_jit_info_data (domain, (len + 1) * sizeof (JitInfoMap), async);
3113 memcpy (new_table, old_table, len * sizeof (JitInfoMap));
3114 new_table [0].method_index = len + 1;
3115 new_table [len].method_index = method_index;
3116 new_table [len].jinfo = jinfo;
3118 mono_memory_barrier ();
3119 if (InterlockedCompareExchangePointer ((gpointer)&amodule->async_jit_info_table, new_table, old_table) == old_table)
3123 mono_jit_info_table_add (domain, jinfo);
3130 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
3138 case MONO_PATCH_INFO_METHOD:
3139 case MONO_PATCH_INFO_METHOD_JUMP:
3140 case MONO_PATCH_INFO_ICALL_ADDR:
3141 case MONO_PATCH_INFO_METHOD_RGCTX:
3142 case MONO_PATCH_INFO_METHOD_CODE_SLOT: {
3146 res = decode_method_ref (aot_module, &ref, p, &p);
3150 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)) {
3151 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (ref.image, ref.token));
3152 ji->type = MONO_PATCH_INFO_ABS;
3156 ji->data.method = ref.method;
3158 ji->data.method = mono_get_method (ref.image, ref.token, NULL);
3159 g_assert (ji->data.method);
3160 mono_class_init (ji->data.method->klass);
3164 case MONO_PATCH_INFO_INTERNAL_METHOD:
3165 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
3166 guint32 len = decode_value (p, &p);
3168 ji->data.name = (char*)p;
3172 case MONO_PATCH_INFO_METHODCONST:
3174 ji->data.method = decode_resolve_method_ref (aot_module, p, &p);
3175 if (!ji->data.method)
3178 case MONO_PATCH_INFO_VTABLE:
3179 case MONO_PATCH_INFO_CLASS:
3180 case MONO_PATCH_INFO_IID:
3181 case MONO_PATCH_INFO_ADJUSTED_IID:
3182 case MONO_PATCH_INFO_CLASS_INIT:
3184 ji->data.klass = decode_klass_ref (aot_module, p, &p);
3185 if (!ji->data.klass)
3188 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
3189 ji->data.del_tramp = mono_mempool_alloc0 (mp, sizeof (MonoDelegateClassMethodPair));
3190 ji->data.del_tramp->klass = decode_klass_ref (aot_module, p, &p);
3191 if (!ji->data.del_tramp->klass)
3193 if (decode_value (p, &p)) {
3194 ji->data.del_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
3195 if (!ji->data.del_tramp->method)
3198 ji->data.del_tramp->virtual = decode_value (p, &p) ? TRUE : FALSE;
3200 case MONO_PATCH_INFO_IMAGE:
3201 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
3202 if (!ji->data.image)
3205 case MONO_PATCH_INFO_FIELD:
3206 case MONO_PATCH_INFO_SFLDA:
3208 ji->data.field = decode_field_info (aot_module, p, &p);
3209 if (!ji->data.field)
3212 case MONO_PATCH_INFO_SWITCH:
3213 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
3214 ji->data.table->table_size = decode_value (p, &p);
3215 table = mono_domain_alloc (mono_domain_get (), sizeof (gpointer) * ji->data.table->table_size);
3216 ji->data.table->table = (MonoBasicBlock**)table;
3217 for (i = 0; i < ji->data.table->table_size; i++)
3218 table [i] = (gpointer)(gssize)decode_value (p, &p);
3220 case MONO_PATCH_INFO_R4: {
3223 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
3224 val = decode_value (p, &p);
3225 *(float*)ji->data.target = *(float*)&val;
3228 case MONO_PATCH_INFO_R8: {
3232 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
3234 val [0] = decode_value (p, &p);
3235 val [1] = decode_value (p, &p);
3236 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
3237 *(double*)ji->data.target = *(double*)&v;
3240 case MONO_PATCH_INFO_LDSTR:
3241 image = load_image (aot_module, decode_value (p, &p), TRUE);
3244 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
3246 case MONO_PATCH_INFO_RVA:
3247 case MONO_PATCH_INFO_DECLSEC:
3248 case MONO_PATCH_INFO_LDTOKEN:
3249 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3251 image = load_image (aot_module, decode_value (p, &p), TRUE);
3254 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
3256 ji->data.token->has_context = decode_value (p, &p);
3257 if (ji->data.token->has_context) {
3258 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
3263 case MONO_PATCH_INFO_EXC_NAME:
3264 ji->data.klass = decode_klass_ref (aot_module, p, &p);
3265 if (!ji->data.klass)
3267 ji->data.name = ji->data.klass->name;
3269 case MONO_PATCH_INFO_METHOD_REL:
3270 ji->data.offset = decode_value (p, &p);
3272 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
3273 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
3274 case MONO_PATCH_INFO_MONITOR_ENTER:
3275 case MONO_PATCH_INFO_MONITOR_EXIT:
3276 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
3277 case MONO_PATCH_INFO_CASTCLASS_CACHE:
3278 case MONO_PATCH_INFO_JIT_TLS_ID:
3280 case MONO_PATCH_INFO_RGCTX_FETCH: {
3282 MonoJumpInfoRgctxEntry *entry;
3283 guint32 offset, val;
3286 offset = decode_value (p, &p);
3287 val = decode_value (p, &p);
3289 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3290 p2 = aot_module->blob + offset;
3291 entry->method = decode_resolve_method_ref (aot_module, p2, &p2);
3292 entry->in_mrgctx = ((val & 1) > 0) ? TRUE : FALSE;
3293 entry->info_type = (val >> 1) & 0xff;
3294 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3295 entry->data->type = (val >> 9) & 0xff;
3297 res = decode_patch (aot_module, mp, entry->data, p, &p);
3300 ji->data.rgctx_entry = entry;
3303 case MONO_PATCH_INFO_SEQ_POINT_INFO:
3305 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
3306 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
3308 imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
3309 imt_tramp->vt_offset = decode_value (p, &p);
3311 ji->data.imt_tramp = imt_tramp;
3314 case MONO_PATCH_INFO_SIGNATURE:
3315 ji->data.target = decode_signature (aot_module, p, &p);
3317 case MONO_PATCH_INFO_TLS_OFFSET:
3318 ji->data.target = GINT_TO_POINTER (decode_value (p, &p));
3320 case MONO_PATCH_INFO_GSHAREDVT_CALL: {
3321 MonoJumpInfoGSharedVtCall *info = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoGSharedVtCall));
3322 info->sig = decode_signature (aot_module, p, &p);
3323 g_assert (info->sig);
3324 info->method = decode_resolve_method_ref (aot_module, p, &p);
3325 g_assert (info->method);
3327 ji->data.target = info;
3330 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
3331 MonoGSharedVtMethodInfo *info = mono_mempool_alloc0 (mp, sizeof (MonoGSharedVtMethodInfo));
3334 info->method = decode_resolve_method_ref (aot_module, p, &p);
3335 g_assert (info->method);
3336 info->num_entries = decode_value (p, &p);
3337 info->count_entries = info->num_entries;
3338 info->entries = mono_mempool_alloc0 (mp, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->num_entries);
3339 for (i = 0; i < info->num_entries; ++i) {
3340 MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
3342 template->info_type = decode_value (p, &p);
3343 switch (mini_rgctx_info_type_to_patch_info_type (template->info_type)) {
3344 case MONO_PATCH_INFO_CLASS: {
3345 MonoClass *klass = decode_klass_ref (aot_module, p, &p);
3348 template->data = &klass->byval_arg;
3351 case MONO_PATCH_INFO_FIELD:
3352 template->data = decode_field_info (aot_module, p, &p);
3353 if (!template->data)
3357 g_assert_not_reached ();
3361 ji->data.target = info;
3365 g_warning ("unhandled type %d", ji->type);
3366 g_assert_not_reached ();
3377 static MonoJumpInfo*
3378 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
3379 guint32 **got_slots,
3380 guint8 *buf, guint8 **endbuf)
3382 MonoJumpInfo *patches;
3388 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
3390 *got_slots = g_malloc (sizeof (guint32) * n_patches);
3392 for (pindex = 0; pindex < n_patches; ++pindex) {
3393 MonoJumpInfo *ji = &patches [pindex];
3398 got_offset = decode_value (p, &p);
3400 if (aot_module->got [got_offset]) {
3401 /* Already loaded */
3402 //printf ("HIT!\n");
3404 shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
3406 ji->type = decode_value (shared_p, &shared_p);
3408 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
3413 (*got_slots) [pindex] = got_offset;
3420 g_free (*got_slots);
3427 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
3430 * Jump addresses cannot be patched by the trampoline code since it
3431 * does not have access to the caller's address. Instead, we collect
3432 * the addresses of the GOT slots pointing to a method, and patch
3433 * them after the method has been compiled.
3435 MonoJitDomainInfo *info = domain_jit_info (domain);
3438 mono_domain_lock (domain);
3439 if (!info->jump_target_got_slot_hash)
3440 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
3441 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
3442 list = g_slist_prepend (list, got_slot);
3443 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
3444 mono_domain_unlock (domain);
3450 * Load the method identified by METHOD_INDEX from the AOT image. Return a
3451 * pointer to the native code of the method, or NULL if not found.
3452 * METHOD might not be set if the caller only has the image/token info.
3455 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
3458 gboolean from_plt = method == NULL;
3460 int i, pindex, n_patches, used_strings;
3461 gboolean keep_patches = TRUE;
3463 MonoJitInfo *jinfo = NULL;
3464 guint8 *code, *info;
3466 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
3469 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
3470 /* Non shared AOT code can't be used in other appdomains */
3473 if (amodule->out_of_date)
3476 if (amodule->code_offsets [method_index] == 0xffffffff) {
3477 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
3481 method = mono_get_method (image, token, NULL);
3482 full_name = mono_method_full_name (method, TRUE);
3483 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: NOT FOUND: %s.", full_name);
3489 code = &amodule->code [amodule->code_offsets [method_index]];
3491 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
3493 if (amodule->thumb_end && code < amodule->thumb_end && ((amodule->info.flags & MONO_AOT_FILE_FLAG_DIRECT_METHOD_ADDRESSES) == 0)) {
3494 /* Convert this into a thumb address */
3495 g_assert ((amodule->code_offsets [method_index] & 0x1) == 0);
3496 code = &amodule->code [amodule->code_offsets [method_index] + 1];
3499 if (!amodule->methods_loaded) {
3500 amodule_lock (amodule);
3501 if (!amodule->methods_loaded) {
3504 loaded = g_new0 (guint32, amodule->info.nmethods / 32 + 1);
3505 mono_memory_barrier ();
3506 amodule->methods_loaded = loaded;
3508 amodule_unlock (amodule);
3511 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
3514 if (mono_last_aot_method != -1) {
3515 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
3517 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
3519 method = mono_get_method (image, token, NULL);
3521 char *name = mono_method_full_name (method, TRUE);
3522 g_print ("LAST AOT METHOD: %s.\n", name);
3525 g_print ("LAST AOT METHOD: %p %d\n", code, method_index);
3533 klass = method->klass;
3534 decode_klass_ref (amodule, p, &p);
3536 klass = decode_klass_ref (amodule, p, &p);
3539 if (amodule->info.opts & MONO_OPT_SHARED)
3540 used_strings = decode_value (p, &p);
3544 for (i = 0; i < used_strings; i++) {
3545 guint token = decode_value (p, &p);
3546 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
3549 if (amodule->info.opts & MONO_OPT_SHARED)
3550 keep_patches = FALSE;
3552 n_patches = decode_value (p, &p);
3554 keep_patches = FALSE;
3557 MonoJumpInfo *patches;
3563 mp = mono_mempool_new ();
3565 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
3566 if (patches == NULL)
3569 for (pindex = 0; pindex < n_patches; ++pindex) {
3570 MonoJumpInfo *ji = &patches [pindex];
3572 if (!amodule->got [got_slots [pindex]]) {
3573 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
3574 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
3575 amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
3576 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
3577 register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
3579 ji->type = MONO_PATCH_INFO_NONE;
3585 mono_mempool_destroy (mp);
3588 if (mini_get_debug_options ()->load_aot_jit_info_eagerly)
3589 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
3591 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
3595 method = mono_get_method (image, token, NULL);
3597 full_name = mono_method_full_name (method, TRUE);
3600 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
3602 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: FOUND method %s [%p - %p %p]", full_name, code, code + jinfo->code_size, info);
3606 amodule_lock (amodule);
3608 InterlockedIncrement (&mono_jit_stats.methods_aot);
3610 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
3614 if (method && method->wrapper_type)
3615 g_hash_table_insert (amodule->method_to_code, method, code);
3617 amodule_unlock (amodule);
3619 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
3623 method = mono_get_method (image, token, NULL);
3626 mono_profiler_method_jit (method);
3627 jinfo = mono_jit_info_table_find (domain, (char*)code);
3629 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
3632 if (from_plt && klass && !klass->generic_container)
3633 mono_runtime_class_init (mono_class_vtable (domain, klass));
3638 /* FIXME: The space in domain->mp is wasted */
3639 if (amodule->info.opts & MONO_OPT_SHARED)
3640 /* No need to cache patches */
3641 mono_mempool_destroy (mp);
3650 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
3652 guint32 table_size, entry_size, hash;
3653 guint32 *table, *entry;
3655 static guint32 n_extra_decodes;
3657 if (!amodule || amodule->out_of_date)
3660 table_size = amodule->extra_method_table [0];
3661 table = amodule->extra_method_table + 1;
3664 hash = mono_aot_method_hash (method) % table_size;
3666 entry = &table [hash * entry_size];
3673 guint32 key = entry [0];
3674 guint32 value = entry [1];
3675 guint32 next = entry [entry_size - 1];
3679 p = amodule->blob + key;
3682 amodule_lock (amodule);
3683 if (!amodule->method_ref_to_method)
3684 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
3685 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
3686 amodule_unlock (amodule);
3688 m = decode_resolve_method_ref_with_target (amodule, method, p, &p);
3690 amodule_lock (amodule);
3691 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
3692 amodule_unlock (amodule);
3700 /* Special case: wrappers of shared generic methods */
3701 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
3702 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
3703 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
3704 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
3706 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
3712 /* Methods decoded needlessly */
3714 //printf ("%d %s %s %p\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE), orig_p);
3719 entry = &table [next * entry_size];
3728 add_module_cb (gpointer key, gpointer value, gpointer user_data)
3730 g_ptr_array_add ((GPtrArray*)user_data, value);
3734 * find_extra_method:
3736 * Try finding METHOD in the extra_method table in all AOT images.
3737 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
3738 * module where the method was found.
3741 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
3747 /* Try the method's module first */
3748 *out_amodule = method->klass->image->aot_module;
3749 index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
3750 if (index != 0xffffff)
3754 * Try all other modules.
3755 * This is needed because generic instances klass->image points to the image
3756 * containing the generic definition, but the native code is generated to the
3757 * AOT image which contains the reference.
3760 /* Make a copy to avoid doing the search inside the aot lock */
3761 modules = g_ptr_array_new ();
3763 g_hash_table_foreach (aot_modules, add_module_cb, modules);
3767 for (i = 0; i < modules->len; ++i) {
3768 MonoAotModule *amodule = g_ptr_array_index (modules, i);
3770 if (amodule != method->klass->image->aot_module)
3771 index = find_extra_method_in_amodule (amodule, method);
3772 if (index != 0xffffff) {
3773 *out_amodule = amodule;
3778 g_ptr_array_free (modules, TRUE);
3784 * mono_aot_get_method:
3786 * Return a pointer to the AOTed native code for METHOD if it can be found,
3788 * On platforms with function pointers, this doesn't return a function pointer.
3791 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3793 MonoClass *klass = method->klass;
3794 guint32 method_index;
3795 MonoAotModule *amodule = klass->image->aot_module;
3798 if (enable_aot_cache && !amodule && domain->entry_assembly && klass->image == mono_defaults.corlib) {
3799 /* This cannot be AOTed during startup, so do it now */
3800 if (!mscorlib_aot_loaded) {
3801 load_aot_module (klass->image->assembly, NULL);
3802 amodule = klass->image->aot_module;
3804 mscorlib_aot_loaded = TRUE;
3810 if (amodule->out_of_date)
3813 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3814 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3815 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3816 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
3820 * Use the original method instead of its invoke-with-check wrapper.
3821 * This is not a problem when using full-aot, since it doesn't support
3824 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
3825 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
3827 g_assert (klass->inited);
3829 /* Find method index */
3830 method_index = 0xffffff;
3831 if (method->is_inflated && !method->wrapper_type && mono_method_is_generic_sharable_full (method, FALSE, FALSE, FALSE)) {
3833 * For generic methods, we store the fully shared instance in place of the
3836 method = mono_method_get_declaring_generic_method (method);
3837 method_index = mono_metadata_token_index (method->token) - 1;
3838 } else if (method->is_inflated || !method->token) {
3839 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
3840 amodule_lock (amodule);
3841 code = g_hash_table_lookup (amodule->method_to_code, method);
3842 amodule_unlock (amodule);
3846 method_index = find_extra_method (method, &amodule);
3848 * Special case the ICollection<T> wrappers for arrays, as they cannot
3849 * be statically enumerated, and each wrapper ends up calling the same
3852 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
3853 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
3855 code = mono_aot_get_method (domain, m);
3861 * Special case Array.GetGenericValueImpl which is a generic icall.
3862 * Generic sharing currently can't handle it, but the icall returns data using
3863 * an out parameter, so the managed-to-native wrappers can share the same code.
3865 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
3867 MonoGenericContext ctx;
3868 MonoType *args [16];
3870 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
3871 /* Avoid recursion */
3874 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
3877 memset (&ctx, 0, sizeof (ctx));
3878 args [0] = &mono_defaults.object_class->byval_arg;
3879 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
3881 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
3884 * Get the code for the <object> instantiation which should be emitted into
3885 * the mscorlib aot image by the AOT compiler.
3887 code = mono_aot_get_method (domain, m);
3892 /* Same for CompareExchange<T> and Exchange<T> */
3893 /* Same for Volatile.Read<T>/Write<T> */
3894 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass->image == mono_defaults.corlib &&
3895 ((!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])) ||
3896 (!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))) ||
3897 (!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]))))) {
3899 MonoGenericContext ctx;
3900 MonoType *args [16];
3901 gpointer iter = NULL;
3903 while ((m = mono_class_get_methods (method->klass, &iter))) {
3904 if (mono_method_signature (m)->generic_param_count && !strcmp (m->name, method->name))
3909 memset (&ctx, 0, sizeof (ctx));
3910 args [0] = &mono_defaults.object_class->byval_arg;
3911 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
3913 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
3915 /* Avoid recursion */
3920 * Get the code for the <object> instantiation which should be emitted into
3921 * the mscorlib aot image by the AOT compiler.
3923 code = mono_aot_get_method (domain, m);
3928 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_full (method, FALSE, TRUE, FALSE)) {
3929 /* Partial sharing */
3932 shared = mini_get_shared_method (method);
3933 method_index = find_extra_method (shared, &amodule);
3934 if (method_index != 0xffffff)
3938 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) {
3940 /* Use the all-vt shared method since this is what was AOTed */
3941 method_index = find_extra_method (mini_get_shared_method_full (method, TRUE, TRUE), &amodule);
3942 if (method_index != 0xffffff)
3943 method = mini_get_shared_method_full (method, TRUE, FALSE);
3946 if (method_index == 0xffffff) {
3947 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
3950 full_name = mono_method_full_name (method, TRUE);
3951 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.", full_name);
3957 if (method_index == 0xffffff)
3960 /* Needed by find_jit_info */
3961 amodule_lock (amodule);
3962 if (!amodule->extra_methods)
3963 amodule->extra_methods = g_hash_table_new (NULL, NULL);
3964 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
3965 amodule_unlock (amodule);
3968 method_index = mono_metadata_token_index (method->token) - 1;
3971 return load_method (domain, amodule, klass->image, method, method->token, method_index);
3975 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
3979 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3981 MonoAotModule *aot_module = image->aot_module;
3987 method_index = mono_metadata_token_index (token) - 1;
3989 return load_method (domain, aot_module, image, NULL, token, method_index);
3995 } IsGotEntryUserData;
3998 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
4000 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
4001 MonoAotModule *aot_module = (MonoAotModule*)value;
4003 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
4008 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
4010 IsGotEntryUserData user_data;
4015 user_data.addr = addr;
4016 user_data.res = FALSE;
4018 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
4021 return user_data.res;
4026 MonoAotModule *module;
4027 } FindAotModuleUserData;
4030 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
4032 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
4033 MonoAotModule *aot_module = (MonoAotModule*)value;
4035 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
4036 data->module = aot_module;
4039 static inline MonoAotModule*
4040 find_aot_module (guint8 *code)
4042 FindAotModuleUserData user_data;
4047 /* Reading these need no locking */
4048 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
4051 user_data.addr = code;
4052 user_data.module = NULL;
4055 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
4058 return user_data.module;
4062 mono_aot_patch_plt_entry (guint8 *code, guint8 *plt_entry, gpointer *got, mgreg_t *regs, guint8 *addr)
4064 MonoAotModule *amodule;
4067 * Since AOT code is only used in the root domain,
4068 * mono_domain_get () != mono_get_root_domain () means the calling method
4069 * is AppDomain:InvokeInDomain, so this is the same check as in
4070 * mono_method_same_domain () but without loading the metadata for the method.
4072 if (mono_domain_get () == mono_get_root_domain ()) {
4074 amodule = find_aot_module (code);
4078 mono_arch_patch_plt_entry (plt_entry, got, regs, addr);
4083 * mono_aot_plt_resolve:
4085 * This function is called by the entries in the PLT to resolve the actual method that
4086 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
4087 * Returns NULL if the something cannot be loaded.
4090 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
4092 #ifdef MONO_ARCH_AOT_SUPPORTED
4093 guint8 *p, *target, *plt_entry;
4095 MonoAotModule *module = (MonoAotModule*)aot_module;
4096 gboolean res, no_ftnptr = FALSE;
4098 gboolean using_gsharedvt = FALSE;
4100 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
4102 p = &module->blob [plt_info_offset];
4104 ji.type = decode_value (p, &p);
4106 mp = mono_mempool_new_size (512);
4107 res = decode_patch (module, mp, &ji, p, &p);
4110 mono_mempool_destroy (mp);
4114 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
4115 using_gsharedvt = TRUE;
4119 * Avoid calling resolve_patch_target in the full-aot case if possible, since
4120 * it would create a trampoline, and we don't need that.
4121 * We could do this only if the method does not need the special handling
4122 * in mono_magic_trampoline ().
4124 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) &&
4125 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE) && !using_gsharedvt) {
4126 target = mono_jit_compile_method (ji.data.method);
4129 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
4133 * The trampoline expects us to return a function descriptor on platforms which use
4134 * it, but resolve_patch_target returns a direct function pointer for some type of
4135 * patches, so have to translate between the two.
4136 * FIXME: Clean this up, but how ?
4138 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) {
4139 /* These should already have a function descriptor */
4140 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4141 /* Our function descriptors have a 0 environment, gcc created ones don't */
4142 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
4143 g_assert (((gpointer*)target) [2] == 0);
4146 } else if (!no_ftnptr) {
4147 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4148 g_assert (((gpointer*)target) [2] != 0);
4150 target = mono_create_ftnptr (mono_domain_get (), target);
4153 mono_mempool_destroy (mp);
4155 /* Patch the PLT entry with target which might be the actual method not a trampoline */
4156 plt_entry = mono_aot_get_plt_entry (code);
4157 g_assert (plt_entry);
4158 mono_aot_patch_plt_entry (code, plt_entry, module->got, NULL, target);
4162 g_assert_not_reached ();
4170 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
4171 * method in the module is loaded to avoid committing memory by writing to it.
4172 * LOCKING: Assumes the AMODULE lock is held.
4175 init_plt (MonoAotModule *amodule)
4180 if (amodule->plt_inited)
4183 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
4186 * Initialize the PLT entries in the GOT to point to the default targets.
4189 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
4190 for (i = 1; i < amodule->info.plt_size; ++i)
4191 /* All the default entries point to the AOT trampoline */
4192 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = tramp;
4194 amodule->plt_inited = TRUE;
4198 * mono_aot_get_plt_entry:
4200 * Return the address of the PLT entry called by the code at CODE if exists.
4203 mono_aot_get_plt_entry (guint8 *code)
4205 MonoAotModule *amodule = find_aot_module (code);
4206 guint8 *target = NULL;
4212 if (amodule->thumb_end && code < amodule->thumb_end) {
4213 return mono_arm_get_thumb_plt_entry (code);
4217 #ifdef MONO_ARCH_AOT_SUPPORTED
4218 target = mono_arch_get_call_target (code);
4220 g_assert_not_reached ();
4224 while (target != NULL) {
4225 if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
4228 // Add 4 since mono_arch_get_call_target assumes we're passing
4229 // the instruction after the actual branch instruction.
4230 target = mono_arch_get_call_target (target + 4);
4235 if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
4243 * mono_aot_get_plt_info_offset:
4245 * Return the PLT info offset belonging to the plt entry called by CODE.
4248 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
4250 guint8 *plt_entry = mono_aot_get_plt_entry (code);
4252 g_assert (plt_entry);
4254 /* The offset is embedded inside the code after the plt entry */
4255 #ifdef MONO_ARCH_AOT_SUPPORTED
4256 return mono_arch_get_plt_info_offset (plt_entry, regs, code);
4258 g_assert_not_reached ();
4264 mono_create_ftnptr_malloc (guint8 *code)
4266 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4267 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
4269 ftnptr->code = code;
4280 * mono_aot_register_jit_icall:
4282 * Register a JIT icall which is called by trampolines in full-aot mode. This should
4283 * be called from mono_arch_init () during startup.
4286 mono_aot_register_jit_icall (const char *name, gpointer addr)
4288 /* No need for locking */
4289 if (!aot_jit_icall_hash)
4290 aot_jit_icall_hash = g_hash_table_new (g_str_hash, g_str_equal);
4291 g_hash_table_insert (aot_jit_icall_hash, (char*)name, addr);
4295 * load_function_full:
4297 * Load the function named NAME from the aot image.
4300 load_function_full (MonoAotModule *amodule, const char *name, MonoTrampInfo **out_tinfo)
4304 int n_patches, pindex;
4307 guint32 info_offset;
4311 symbol = g_strdup_printf ("%s", name);
4312 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
4315 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
4317 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: FOUND function '%s' in AOT file '%s'.", name, amodule->aot_name);
4321 symbol = g_strdup_printf ("%s_p", name);
4322 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
4325 /* Nothing to patch */
4328 info_offset = *(guint32*)p;
4330 MonoTrampInfo *tinfo;
4331 guint32 code_size, uw_info_len, uw_offset;
4333 /* Construct a MonoTrampInfo from the data in the AOT image */
4335 p += sizeof (guint32);
4336 code_size = *(guint32*)p;
4337 p += sizeof (guint32);
4338 uw_offset = *(guint32*)p;
4339 uw_info = amodule->unwind_info + uw_offset;
4340 uw_info_len = decode_value (uw_info, &uw_info);
4342 tinfo = g_new0 (MonoTrampInfo, 1);
4344 tinfo->code_size = code_size;
4345 tinfo->uw_info = uw_info;
4346 tinfo->uw_info_len = uw_info_len;
4351 p = amodule->blob + info_offset;
4353 /* Similar to mono_aot_load_method () */
4355 n_patches = decode_value (p, &p);
4358 MonoJumpInfo *patches;
4361 mp = mono_mempool_new ();
4363 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
4366 for (pindex = 0; pindex < n_patches; ++pindex) {
4367 MonoJumpInfo *ji = &patches [pindex];
4370 if (amodule->got [got_slots [pindex]])
4374 * When this code is executed, the runtime may not be initalized yet, so
4375 * resolve the patch info by hand.
4377 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
4378 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
4379 target = mono_get_lmf_addr;
4380 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
4381 target = mono_thread_force_interruption_checkpoint;
4382 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
4383 target = mono_exception_from_token;
4384 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
4385 target = mono_get_throw_exception ();
4386 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
4387 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
4388 target = (gpointer)mono_get_trampoline_func (tramp_type2);
4389 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
4390 /* atoll is needed because the the offset is unsigned */
4394 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
4395 g_assert (res == 1);
4396 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
4397 target = mono_create_ftnptr_malloc (target);
4398 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
4399 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
4400 target = mono_create_ftnptr_malloc (target);
4401 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
4402 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
4403 target = mono_create_ftnptr_malloc (target);
4404 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
4405 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
4406 target = mono_create_ftnptr_malloc (target);
4407 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
4408 target = mono_thread_get_and_clear_pending_exception;
4409 } else if (strstr (ji->data.name, "generic_trampoline_")) {
4410 target = mono_aot_get_trampoline (ji->data.name);
4411 } else if (aot_jit_icall_hash && g_hash_table_lookup (aot_jit_icall_hash, ji->data.name)) {
4412 /* Registered by mono_arch_init () */
4413 target = g_hash_table_lookup (aot_jit_icall_hash, ji->data.name);
4415 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
4416 g_assert_not_reached ();
4420 /* Hopefully the code doesn't have patches which need method or
4423 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
4427 amodule->got [got_slots [pindex]] = target;
4432 mono_mempool_destroy (mp);
4439 load_function (MonoAotModule *amodule, const char *name)
4441 return load_function_full (amodule, name, NULL);
4445 * Return the trampoline identified by NAME from the mscorlib AOT file.
4446 * On ppc64, this returns a function descriptor.
4449 mono_aot_get_trampoline_full (const char *name, MonoTrampInfo **out_tinfo)
4452 MonoAotModule *amodule;
4454 image = mono_defaults.corlib;
4457 amodule = image->aot_module;
4460 return mono_create_ftnptr_malloc (load_function_full (amodule, name, out_tinfo));
4464 mono_aot_get_trampoline (const char *name)
4466 return mono_aot_get_trampoline_full (name, NULL);
4470 #include <mach/mach.h>
4472 static TrampolinePage* trampoline_pages [MONO_AOT_TRAMP_NUM];
4473 /* these sizes are for ARM code, parametrize if porting to other architectures (see arch_emit_specific_trampoline_pages)
4474 * trampoline size is assumed to be 8 bytes below as well (8 is the minimum for 32 bit archs, since we need to store
4475 * two pointers for trampoline in the data page).
4476 * the minimum for the common code must be at least sizeof(TrampolinePage), since we store the page info at the
4477 * beginning of the data page.
4479 static const int trampolines_pages_code_offsets [MONO_AOT_TRAMP_NUM] = {16, 16, 72, 16};
4481 static unsigned char*
4482 get_new_trampoline_from_page (int tramp_type)
4484 MonoAotModule *amodule;
4486 TrampolinePage *page;
4489 vm_address_t addr, taddr;
4491 vm_prot_t prot, max_prot;
4492 int psize, specific_trampoline_size;
4493 unsigned char *code;
4495 specific_trampoline_size = 2 * sizeof (gpointer);
4497 mono_aot_page_lock ();
4498 page = trampoline_pages [tramp_type];
4499 if (page && page->trampolines < page->trampolines_end) {
4500 code = page->trampolines;
4501 page->trampolines += specific_trampoline_size;
4502 mono_aot_page_unlock ();
4505 mono_aot_page_unlock ();
4506 psize = mono_pagesize ();
4507 /* the trampoline template page is in the mscorlib module */
4508 image = mono_defaults.corlib;
4511 amodule = image->aot_module;
4514 g_assert (amodule->info.tramp_page_size == psize);
4516 if (tramp_type == MONO_AOT_TRAMP_SPECIFIC)
4517 tpage = load_function (amodule, "specific_trampolines_page");
4518 else if (tramp_type == MONO_AOT_TRAMP_STATIC_RGCTX)
4519 tpage = load_function (amodule, "rgctx_trampolines_page");
4520 else if (tramp_type == MONO_AOT_TRAMP_IMT_THUNK)
4521 tpage = load_function (amodule, "imt_trampolines_page");
4522 else if (tramp_type == MONO_AOT_TRAMP_GSHAREDVT_ARG)
4523 tpage = load_function (amodule, "gsharedvt_arg_trampolines_page");
4525 g_error ("Incorrect tramp type for trampolines page");
4527 /*g_warning ("loaded trampolines page at %x", tpage);*/
4529 /* avoid the unlikely case of looping forever */
4532 while (page == NULL && count-- > 0) {
4534 /* allocate two contiguous pages of memory: the first page will contain the data (like a local constant pool)
4535 * while the second will contain the trampolines.
4537 ret = vm_allocate (mach_task_self (), &addr, psize * 2, VM_FLAGS_ANYWHERE);
4538 if (ret != KERN_SUCCESS) {
4539 g_error ("Cannot allocate memory for trampolines: %d", ret);
4542 /*g_warning ("allocated trampoline double page at %x", addr);*/
4543 /* replace the second page with a remapped trampoline page */
4544 taddr = addr + psize;
4545 vm_deallocate (mach_task_self (), taddr, psize);
4546 ret = vm_remap (mach_task_self (), &taddr, psize, 0, FALSE, mach_task_self(), (vm_address_t)tpage, FALSE, &prot, &max_prot, VM_INHERIT_SHARE);
4547 if (ret != KERN_SUCCESS) {
4548 /* someone else got the page, try again */
4549 vm_deallocate (mach_task_self (), addr, psize);
4552 /*g_warning ("remapped trampoline page at %x", taddr);*/
4554 mono_aot_page_lock ();
4555 page = trampoline_pages [tramp_type];
4556 /* some other thread already allocated, so use that to avoid wasting memory */
4557 if (page && page->trampolines < page->trampolines_end) {
4558 code = page->trampolines;
4559 page->trampolines += specific_trampoline_size;
4560 mono_aot_page_unlock ();
4561 vm_deallocate (mach_task_self (), addr, psize);
4562 vm_deallocate (mach_task_self (), taddr, psize);
4565 page = (TrampolinePage*)addr;
4566 page->next = trampoline_pages [tramp_type];
4567 trampoline_pages [tramp_type] = page;
4569 page->trampolines = (void*)(taddr + amodule->info.tramp_page_code_offsets [tramp_type]);
4571 page->trampolines = (void*)(taddr + trampolines_pages_code_offsets [tramp_type]);
4573 page->trampolines_end = (void*)(taddr + psize - 64);
4574 code = page->trampolines;
4575 page->trampolines += specific_trampoline_size;
4576 mono_aot_page_unlock ();
4579 g_error ("Cannot allocate more trampoline pages: %d", ret);
4584 static unsigned char*
4585 get_new_trampoline_from_page (int tramp_type)
4587 g_error ("Page trampolines not supported.");
4594 get_new_specific_trampoline_from_page (gpointer tramp, gpointer arg)
4599 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_SPECIFIC);
4601 data = (gpointer*)((char*)code - mono_pagesize ());
4604 /*g_warning ("new trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
4610 get_new_rgctx_trampoline_from_page (gpointer tramp, gpointer arg)
4615 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_STATIC_RGCTX);
4617 data = (gpointer*)((char*)code - mono_pagesize ());
4620 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
4626 get_new_imt_trampoline_from_page (gpointer arg)
4631 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_IMT_THUNK);
4633 data = (gpointer*)((char*)code - mono_pagesize ());
4635 /*g_warning ("new imt trampoline at %p for data %p, (stored at %p)", code, arg, data);*/
4641 get_new_gsharedvt_arg_trampoline_from_page (gpointer tramp, gpointer arg)
4646 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_GSHAREDVT_ARG);
4648 data = (gpointer*)((char*)code - mono_pagesize ());
4651 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
4655 /* Return a given kind of trampoline */
4657 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
4659 MonoAotModule *amodule;
4660 int index, tramp_size;
4663 /* Currently, we keep all trampolines in the mscorlib AOT image */
4664 image = mono_defaults.corlib;
4669 amodule = image->aot_module;
4672 *out_amodule = amodule;
4675 #define MONOTOUCH_TRAMPOLINES_ERROR ". See http://docs.xamarin.com/ios/troubleshooting for instructions on how to fix this condition."
4677 #define MONOTOUCH_TRAMPOLINES_ERROR ""
4679 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type]) {
4680 g_error ("Ran out of trampolines of type %d in '%s' (%d)%s\n",
4681 tramp_type, image->name, amodule->info.num_trampolines [tramp_type], MONOTOUCH_TRAMPOLINES_ERROR);
4683 index = amodule->trampoline_index [tramp_type] ++;
4687 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
4689 tramp_size = amodule->info.trampoline_size [tramp_type];
4692 *out_tramp_size = tramp_size;
4694 return amodule->trampolines [tramp_type] + (index * tramp_size);
4698 * Return a specific trampoline from the AOT file.
4701 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
4703 MonoAotModule *amodule;
4704 guint32 got_offset, tramp_size;
4705 guint8 *code, *tramp;
4706 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
4707 static gboolean inited;
4708 static guint32 num_trampolines;
4714 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
4723 if (!generic_trampolines [tramp_type]) {
4726 symbol = mono_get_generic_trampoline_name (tramp_type);
4727 generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
4731 tramp = generic_trampolines [tramp_type];
4734 if (USE_PAGE_TRAMPOLINES) {
4735 code = get_new_specific_trampoline_from_page (tramp, arg1);
4738 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
4740 amodule->got [got_offset] = tramp;
4741 amodule->got [got_offset + 1] = arg1;
4745 *code_len = tramp_size;
4751 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
4753 MonoAotModule *amodule;
4757 if (USE_PAGE_TRAMPOLINES) {
4758 code = get_new_rgctx_trampoline_from_page (addr, ctx);
4760 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
4762 amodule->got [got_offset] = ctx;
4763 amodule->got [got_offset + 1] = addr;
4766 /* The caller expects an ftnptr */
4767 return mono_create_ftnptr (mono_domain_get (), code);
4771 mono_aot_get_unbox_trampoline (MonoMethod *method)
4773 guint32 method_index = mono_metadata_token_index (method->token) - 1;
4774 MonoAotModule *amodule;
4776 guint32 *ut, *ut_end, *entry;
4777 int low, high, entry_index;
4779 if (method->is_inflated && !mono_method_is_generic_sharable_full (method, FALSE, FALSE, FALSE)) {
4780 method_index = find_extra_method (method, &amodule);
4781 if (method_index == 0xffffff && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) {
4782 MonoMethod *shared = mini_get_shared_method_full (method, TRUE, TRUE);
4783 method_index = find_extra_method (shared, &amodule);
4785 g_assert (method_index != 0xffffff);
4787 amodule = method->klass->image->aot_module;
4791 ut = amodule->unbox_trampolines;
4792 ut_end = amodule->unbox_trampolines_end;
4794 /* Do a binary search in the sorted table */
4797 high = (ut_end - ut) / 2;
4798 while (low < high) {
4799 entry_index = (low + high) / 2;
4800 entry = &ut [(entry_index * 2)];
4801 if (entry [0] < method_index) {
4802 low = entry_index + 1;
4803 } else if (entry [0] > method_index) {
4806 if (amodule->info.flags & MONO_AOT_FILE_FLAG_DIRECT_METHOD_ADDRESSES)
4807 code = get_arm_bl_target (entry + 1);
4809 code = amodule->code + entry [1];
4815 /* The caller expects an ftnptr */
4816 return mono_create_ftnptr (mono_domain_get (), code);
4820 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
4824 MonoAotModule *amodule = mono_defaults.corlib->aot_module;
4825 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
4826 static int count = 0;
4829 if (index >= amodule->info.num_rgctx_fetch_trampolines) {
4830 static gpointer addr;
4834 * Use the general version of the rgctx fetch trampoline. It receives a pair of <slot, trampoline> in the rgctx arg reg.
4837 addr = load_function (amodule, "rgctx_fetch_trampoline_general");
4838 info = mono_domain_alloc0 (mono_get_root_domain (), sizeof (gpointer) * 2);
4839 info [0] = GUINT_TO_POINTER (slot);
4840 info [1] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
4841 code = mono_aot_get_static_rgctx_trampoline (info, addr);
4842 return mono_create_ftnptr (mono_domain_get (), code);
4845 symbol = mono_get_rgctx_fetch_trampoline_name (slot);
4846 code = load_function (mono_defaults.corlib->aot_module, symbol);
4848 /* The caller expects an ftnptr */
4849 return mono_create_ftnptr (mono_domain_get (), code);
4853 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
4858 int i, index, real_count;
4859 MonoAotModule *amodule;
4862 for (i = 0; i < count; ++i) {
4863 MonoIMTCheckItem *item = imt_entries [i];
4865 if (item->is_equals)
4869 /* Save the entries into an array */
4870 buf = mono_domain_alloc (domain, (real_count + 1) * 2 * sizeof (gpointer));
4872 for (i = 0; i < count; ++i) {
4873 MonoIMTCheckItem *item = imt_entries [i];
4875 if (!item->is_equals)
4878 g_assert (item->key);
4880 buf [(index * 2)] = item->key;
4881 if (item->has_target_code) {
4882 gpointer *p = mono_domain_alloc (domain, sizeof (gpointer));
4883 *p = item->value.target_code;
4884 buf [(index * 2) + 1] = p;
4886 buf [(index * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
4890 buf [(index * 2)] = NULL;
4891 buf [(index * 2) + 1] = fail_tramp;
4893 if (USE_PAGE_TRAMPOLINES) {
4894 code = get_new_imt_trampoline_from_page (buf);
4896 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
4898 amodule->got [got_offset] = buf;
4905 mono_aot_get_gsharedvt_arg_trampoline (gpointer arg, gpointer addr)
4907 MonoAotModule *amodule;
4911 if (USE_PAGE_TRAMPOLINES) {
4912 code = get_new_gsharedvt_arg_trampoline_from_page (addr, arg);
4914 code = get_numerous_trampoline (MONO_AOT_TRAMP_GSHAREDVT_ARG, 2, &amodule, &got_offset, NULL);
4916 amodule->got [got_offset] = arg;
4917 amodule->got [got_offset + 1] = addr;
4920 /* The caller expects an ftnptr */
4921 return mono_create_ftnptr (mono_domain_get (), code);
4925 * mono_aot_set_make_unreadable:
4927 * Set whenever to make all mmaped memory unreadable. In conjuction with a
4928 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
4931 mono_aot_set_make_unreadable (gboolean unreadable)
4935 make_unreadable = unreadable;
4937 if (make_unreadable && !inited) {
4938 mono_counters_register ("AOT: pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
4943 MonoAotModule *module;
4948 find_map (gpointer key, gpointer value, gpointer user_data)
4950 MonoAotModule *module = (MonoAotModule*)value;
4951 FindMapUserData *data = (FindMapUserData*)user_data;
4954 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
4955 data->module = module;
4958 static MonoAotModule*
4959 find_module_for_addr (void *ptr)
4961 FindMapUserData data;
4963 if (!make_unreadable)
4967 data.ptr = (guint8*)ptr;
4970 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
4977 * mono_aot_is_pagefault:
4979 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
4980 * within memory allocated by this module.
4983 mono_aot_is_pagefault (void *ptr)
4985 if (!make_unreadable)
4989 * Not signal safe, but SIGSEGV's are synchronous, and
4990 * this is only turned on by a MONO_DEBUG option.
4992 return find_module_for_addr (ptr) != NULL;
4996 * mono_aot_handle_pagefault:
4998 * Handle a pagefault caused by an unreadable page by making it readable again.
5001 mono_aot_handle_pagefault (void *ptr)
5003 #ifndef PLATFORM_WIN32
5004 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
5008 res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
5009 g_assert (res == 0);
5020 mono_aot_init (void)
5025 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
5031 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
5037 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
5043 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
5049 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
5055 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
5061 mono_aot_get_plt_entry (guint8 *code)
5067 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
5073 mono_aot_patch_plt_entry (guint8 *code, guint8 *plt_entry, gpointer *got, mgreg_t *regs, guint8 *addr)
5078 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
5084 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
5086 g_assert_not_reached ();
5092 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
5094 g_assert_not_reached ();
5099 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
5101 g_assert_not_reached ();
5106 mono_aot_get_trampoline (const char *name)
5108 g_assert_not_reached ();
5113 mono_aot_get_unbox_trampoline (MonoMethod *method)
5115 g_assert_not_reached ();
5120 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
5122 g_assert_not_reached ();
5127 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
5129 g_assert_not_reached ();
5134 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
5136 g_assert_not_reached ();
5141 mono_aot_register_jit_icall (const char *name, gpointer addr)