2 * aot-runtime.c: mono Ahead of Time compiler
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2002 Ximian, Inc.
9 * Copyright 2003-2011 Novell, Inc.
10 * Copyright 2011 Xamarin, Inc.
14 #include <sys/types.h>
20 #ifdef HAVE_SYS_MMAN_H
29 #ifdef HAVE_EXECINFO_H
36 #ifdef HAVE_SYS_WAIT_H
37 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
40 #include <mono/metadata/abi-details.h>
41 #include <mono/metadata/tabledefs.h>
42 #include <mono/metadata/class.h>
43 #include <mono/metadata/object.h>
44 #include <mono/metadata/tokentype.h>
45 #include <mono/metadata/appdomain.h>
46 #include <mono/metadata/debug-helpers.h>
47 #include <mono/metadata/assembly.h>
48 #include <mono/metadata/metadata-internals.h>
49 #include <mono/metadata/marshal.h>
50 #include <mono/metadata/gc-internal.h>
51 #include <mono/metadata/monitor.h>
52 #include <mono/metadata/threads-types.h>
53 #include <mono/metadata/mono-endian.h>
54 #include <mono/utils/mono-logger-internal.h>
55 #include <mono/utils/mono-mmap.h>
56 #include "mono/utils/mono-compiler.h"
57 #include <mono/utils/mono-counters.h>
58 #include <mono/utils/mono-digest.h>
61 #include "seq-points.h"
67 #define ENABLE_AOT_CACHE
70 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
71 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
72 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
79 typedef struct MonoAotModule {
81 /* Pointer to the Global Offset Table */
84 GHashTable *name_cache;
85 GHashTable *extra_methods;
86 /* Maps methods to their code */
87 GHashTable *method_to_code;
88 /* Maps pointers into the method info to the methods themselves */
89 GHashTable *method_ref_to_method;
90 MonoAssemblyName *image_names;
92 MonoAssembly *assembly;
93 MonoImage **image_table;
94 guint32 image_table_len;
99 /* Points to either the start of JIT compiler or LLVM compiled code */
101 guint8 *jit_code_start;
102 guint8 *jit_code_end;
103 guint8 *llvm_code_start;
104 guint8 *llvm_code_end;
108 gint32 *code_offsets;
109 gpointer *method_addresses;
110 /* This contains <offset, index> pairs sorted by offset */
111 /* This is needed because LLVM emitted methods can be in any order */
112 gint32 *sorted_code_offsets;
113 gint32 sorted_code_offsets_len;
114 guint32 *method_info_offsets;
115 guint32 *ex_info_offsets;
116 guint32 *class_info_offsets;
117 guint32 *methods_loaded;
118 guint16 *class_name_table;
119 guint32 *extra_method_table;
120 guint32 *extra_method_info_offsets;
121 guint32 *unbox_trampolines;
122 guint32 *unbox_trampolines_end;
123 guint32 *unbox_trampoline_addresses;
127 /* Points to the mono EH data created by LLVM */
128 guint8 *mono_eh_frame;
130 /* Points to the trampolines */
131 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
132 /* The first unused trampoline of each kind */
133 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
135 gboolean use_page_trampolines;
137 MonoAotFileInfo info;
142 JitInfoMap *async_jit_info_table;
148 unsigned char *trampolines;
149 unsigned char *trampolines_end;
152 static GHashTable *aot_modules;
153 #define mono_aot_lock() mono_mutex_lock (&aot_mutex)
154 #define mono_aot_unlock() mono_mutex_unlock (&aot_mutex)
155 static mono_mutex_t aot_mutex;
158 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
159 * AOT modules registered by mono_aot_register_module ().
161 static GHashTable *static_aot_modules;
164 * Maps MonoJitInfo* to the aot module they belong to, this can be different
165 * from ji->method->klass->image's aot module for generic instances.
167 static GHashTable *ji_to_amodule;
170 * Whenever to AOT compile loaded assemblies on demand and store them in
173 static gboolean enable_aot_cache = FALSE;
175 static gboolean mscorlib_aot_loaded;
178 static gint32 mono_last_aot_method = -1;
180 static gboolean make_unreadable = FALSE;
181 static guint32 name_table_accesses = 0;
182 static guint32 n_pagefaults = 0;
184 /* Used to speed-up find_aot_module () */
185 static gsize aot_code_low_addr = (gssize)-1;
186 static gsize aot_code_high_addr = 0;
189 static gint32 async_jit_info_size;
191 static GHashTable *aot_jit_icall_hash;
194 #define USE_PAGE_TRAMPOLINES ((MonoAotModule*)mono_defaults.corlib->aot_module)->use_page_trampolines
196 #define USE_PAGE_TRAMPOLINES 0
199 #define mono_aot_page_lock() mono_mutex_lock (&aot_page_mutex)
200 #define mono_aot_page_unlock() mono_mutex_unlock (&aot_page_mutex)
201 static mono_mutex_t aot_page_mutex;
204 init_plt (MonoAotModule *info);
207 compute_llvm_code_range (MonoAotModule *amodule, guint8 **code_start, guint8 **code_end);
209 /*****************************************************/
211 /*****************************************************/
214 amodule_lock (MonoAotModule *amodule)
216 mono_mutex_lock (&amodule->mutex);
220 amodule_unlock (MonoAotModule *amodule)
222 mono_mutex_unlock (&amodule->mutex);
228 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
229 * found, and sets the loader error if SET_ERROR is TRUE.
232 load_image (MonoAotModule *amodule, int index, gboolean set_error)
234 MonoAssembly *assembly;
235 MonoImageOpenStatus status;
237 g_assert (index < amodule->image_table_len);
239 if (amodule->image_table [index])
240 return amodule->image_table [index];
241 if (amodule->out_of_date)
244 assembly = mono_assembly_load (&amodule->image_names [index], amodule->assembly->basedir, &status);
246 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);
247 amodule->out_of_date = TRUE;
250 char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
251 mono_loader_set_error_assembly_load (full_name, FALSE);
257 if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
258 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);
259 amodule->out_of_date = TRUE;
263 amodule->image_table [index] = assembly->image;
264 return assembly->image;
268 decode_value (guint8 *ptr, guint8 **rptr)
273 if ((b & 0x80) == 0){
276 } else if ((b & 0x40) == 0){
277 len = ((b & 0x3f) << 8 | ptr [1]);
279 } else if (b != 0xff) {
280 len = ((b & 0x1f) << 24) |
287 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
293 //printf ("DECODE: %d.\n", len);
298 * mono_aot_get_offset:
300 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
304 mono_aot_get_offset (guint32 *table, int index)
306 int i, group, ngroups, index_entry_size;
307 int start_offset, offset, noffsets, group_size;
308 guint8 *data_start, *p;
309 guint32 *index32 = NULL;
310 guint16 *index16 = NULL;
312 noffsets = table [0];
313 group_size = table [1];
315 index_entry_size = table [3];
316 group = index / group_size;
318 if (index_entry_size == 2) {
319 index16 = (guint16*)&table [4];
320 data_start = (guint8*)&index16 [ngroups];
321 p = data_start + index16 [group];
323 index32 = (guint32*)&table [4];
324 data_start = (guint8*)&index32 [ngroups];
325 p = data_start + index32 [group];
328 /* offset will contain the value of offsets [group * group_size] */
329 offset = start_offset = decode_value (p, &p);
330 for (i = group * group_size + 1; i <= index; ++i) {
331 offset += decode_value (p, &p);
334 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
340 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
343 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
346 decode_type (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
348 static MonoGenericInst*
349 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
352 MonoType **type_argv;
353 MonoGenericInst *inst;
356 type_argc = decode_value (p, &p);
357 type_argv = g_new0 (MonoType*, type_argc);
359 for (i = 0; i < type_argc; ++i) {
360 MonoClass *pclass = decode_klass_ref (module, p, &p);
365 type_argv [i] = &pclass->byval_arg;
368 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
377 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
384 argc = decode_value (p, &p);
387 ctx->class_inst = decode_generic_inst (module, p, &p);
388 if (!ctx->class_inst)
392 argc = decode_value (p, &p);
395 ctx->method_inst = decode_generic_inst (module, p, &p);
396 if (!ctx->method_inst)
405 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_checked (image, MONO_TOKEN_TYPE_DEF + idx, &error);
427 g_assert (mono_error_ok (&error));
429 case MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE:
430 idx = decode_value (p, &p);
431 image = load_image (module, decode_value (p, &p), TRUE);
434 klass = mono_class_get_checked (image, MONO_TOKEN_TYPE_DEF + idx, &error);
435 g_assert (mono_error_ok (&error));
437 case MONO_AOT_TYPEREF_TYPESPEC_TOKEN:
438 token = decode_value (p, &p);
439 image = module->assembly->image;
442 klass = mono_class_get_checked (image, token, &error);
443 g_assert (mono_error_ok (&error));
445 case MONO_AOT_TYPEREF_GINST: {
447 MonoGenericContext ctx;
450 gclass = decode_klass_ref (module, p, &p);
453 g_assert (gclass->generic_container);
455 memset (&ctx, 0, sizeof (ctx));
456 ctx.class_inst = decode_generic_inst (module, p, &p);
459 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
460 klass = mono_class_from_mono_type (type);
461 mono_metadata_free_type (type);
464 case MONO_AOT_TYPEREF_VAR: {
466 MonoGenericContainer *container = NULL;
467 int type = decode_value (p, &p);
468 int num = decode_value (p, &p);
469 gboolean has_container = decode_value (p, &p);
473 gboolean is_method = decode_value (p, &p);
476 MonoMethod *method_def;
477 g_assert (type == MONO_TYPE_MVAR);
478 method_def = decode_resolve_method_ref (module, p, &p);
482 container = mono_method_get_generic_container (method_def);
484 MonoClass *class_def;
485 g_assert (type == MONO_TYPE_VAR);
486 class_def = decode_klass_ref (module, p, &p);
490 container = class_def->generic_container;
493 serial = decode_value (p, &p);
496 t = g_new0 (MonoType, 1);
499 t->data.generic_param = mono_generic_container_get_param (container, num);
500 g_assert (serial == 0);
503 MonoGenericParam *par = (MonoGenericParam*)mono_image_alloc0 (module->assembly->image, sizeof (MonoGenericParamFull));
505 par->serial = serial;
507 par->image = mono_defaults.corlib;
508 t->data.generic_param = par;
511 // FIXME: Maybe use types directly to avoid
512 // the overhead of creating MonoClass-es
513 klass = mono_class_from_mono_type (t);
518 case MONO_AOT_TYPEREF_ARRAY:
520 rank = decode_value (p, &p);
521 eklass = decode_klass_ref (module, p, &p);
522 klass = mono_array_class_get (eklass, rank);
524 case MONO_AOT_TYPEREF_PTR: {
527 t = decode_type (module, p, &p);
530 klass = mono_class_from_mono_type (t);
534 case MONO_AOT_TYPEREF_BLOB_INDEX: {
535 guint32 offset = decode_value (p, &p);
538 p2 = module->blob + offset;
539 klass = decode_klass_ref (module, p2, &p2);
543 g_assert_not_reached ();
546 //printf ("BLA: %s\n", mono_type_full_name (&klass->byval_arg));
551 static MonoClassField*
552 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
554 MonoClass *klass = decode_klass_ref (module, buf, &buf);
561 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
565 return mono_class_get_field (klass, token);
569 * Parse a MonoType encoded by encode_type () in aot-compiler.c. Return malloc-ed
573 decode_type (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
578 t = g_malloc0 (sizeof (MonoType));
581 if (*p == MONO_TYPE_PINNED) {
584 } else if (*p == MONO_TYPE_BYREF) {
597 case MONO_TYPE_BOOLEAN:
611 case MONO_TYPE_STRING:
612 case MONO_TYPE_OBJECT:
613 case MONO_TYPE_TYPEDBYREF:
615 case MONO_TYPE_VALUETYPE:
616 case MONO_TYPE_CLASS:
617 t->data.klass = decode_klass_ref (module, p, &p);
619 case MONO_TYPE_SZARRAY:
620 t->data.klass = decode_klass_ref (module, p, &p);
626 t->data.type = decode_type (module, p, &p);
628 case MONO_TYPE_GENERICINST: {
630 MonoGenericContext ctx;
634 gclass = decode_klass_ref (module, p, &p);
637 g_assert (gclass->generic_container);
639 memset (&ctx, 0, sizeof (ctx));
640 ctx.class_inst = decode_generic_inst (module, p, &p);
643 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
644 klass = mono_class_from_mono_type (type);
645 t->data.generic_class = klass->generic_class;
648 case MONO_TYPE_ARRAY: {
649 MonoArrayType *array;
652 // FIXME: memory management
653 array = g_new0 (MonoArrayType, 1);
654 array->eklass = decode_klass_ref (module, p, &p);
657 array->rank = decode_value (p, &p);
658 array->numsizes = decode_value (p, &p);
661 array->sizes = g_malloc0 (sizeof (int) * array->numsizes);
662 for (i = 0; i < array->numsizes; ++i)
663 array->sizes [i] = decode_value (p, &p);
665 array->numlobounds = decode_value (p, &p);
666 if (array->numlobounds)
667 array->lobounds = g_malloc0 (sizeof (int) * array->numlobounds);
668 for (i = 0; i < array->numlobounds; ++i)
669 array->lobounds [i] = decode_value (p, &p);
670 t->data.array = array;
674 case MONO_TYPE_MVAR: {
675 MonoClass *klass = decode_klass_ref (module, p, &p);
678 t->data.generic_param = klass->byval_arg.data.generic_param;
682 g_assert_not_reached ();
690 // FIXME: Error handling, memory management
692 static MonoMethodSignature*
693 decode_signature_with_target (MonoAotModule *module, MonoMethodSignature *target, guint8 *buf, guint8 **endbuf)
695 MonoMethodSignature *sig;
697 int i, param_count, call_conv, gen_param_count = 0;
699 gboolean hasthis, explicit_this, has_gen_params;
703 has_gen_params = (flags & 0x10) != 0;
704 hasthis = (flags & 0x20) != 0;
705 explicit_this = (flags & 0x40) != 0;
706 call_conv = flags & 0x0F;
709 gen_param_count = decode_value (p, &p);
710 param_count = decode_value (p, &p);
711 if (target && param_count != target->param_count)
713 sig = g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE + param_count * sizeof (MonoType *));
714 sig->param_count = param_count;
715 sig->sentinelpos = -1;
716 sig->hasthis = hasthis;
717 sig->explicit_this = explicit_this;
718 sig->call_convention = call_conv;
719 sig->param_count = param_count;
720 sig->ret = decode_type (module, p, &p);
721 for (i = 0; i < param_count; ++i) {
722 if (*p == MONO_TYPE_SENTINEL) {
723 g_assert (sig->call_convention == MONO_CALL_VARARG);
724 sig->sentinelpos = i;
727 sig->params [i] = decode_type (module, p, &p);
730 if (sig->call_convention == MONO_CALL_VARARG && sig->sentinelpos == -1)
731 sig->sentinelpos = sig->param_count;
738 static MonoMethodSignature*
739 decode_signature (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
741 return decode_signature_with_target (module, NULL, buf, endbuf);
745 sig_matches_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
747 MonoMethodSignature *sig;
751 sig = decode_signature_with_target (module, mono_method_signature (target), p, &p);
752 res = sig && mono_metadata_signature_equal (mono_method_signature (target), sig);
758 /* Stores information returned by decode_method_ref () */
763 gboolean no_aot_trampoline;
767 * decode_method_ref_with_target:
769 * Decode a method reference, storing the image/token into a MethodRef structure.
770 * This avoids loading metadata for the method if the caller does not need it. If the method has
771 * no token, then it is loaded from metadata and ref->method is set to the method instance.
772 * If TARGET is non-NULL, abort decoding if it can be determined that the decoded method
773 * couldn't resolve to TARGET, and return FALSE.
774 * There are some kinds of method references which only support a non-null TARGET.
775 * This means that its not possible to decode this into a method, only to check
776 * that the method reference matches a given method. This is normally not a problem
777 * as these wrappers only occur in the extra_methods table, where we already have
778 * a method we want to lookup.
781 decode_method_ref_with_target (MonoAotModule *module, MethodRef *ref, MonoMethod *target, guint8 *buf, guint8 **endbuf)
783 guint32 image_index, value;
784 MonoImage *image = NULL;
787 memset (ref, 0, sizeof (MethodRef));
789 value = decode_value (p, &p);
790 image_index = value >> 24;
792 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
793 ref->no_aot_trampoline = TRUE;
794 value = decode_value (p, &p);
795 image_index = value >> 24;
798 if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
799 if (target && target->wrapper_type)
803 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
804 guint32 wrapper_type;
806 wrapper_type = decode_value (p, &p);
808 if (target && target->wrapper_type != wrapper_type)
812 image = mono_defaults.corlib;
814 switch (wrapper_type) {
815 #ifndef DISABLE_REMOTING
816 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
817 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
821 mono_class_init (m->klass);
822 ref->method = mono_marshal_get_remoting_invoke_with_check (m);
825 case MONO_WRAPPER_PROXY_ISINST: {
826 MonoClass *klass = decode_klass_ref (module, p, &p);
829 ref->method = mono_marshal_get_proxy_cancast (klass);
832 case MONO_WRAPPER_LDFLD:
833 case MONO_WRAPPER_LDFLDA:
834 case MONO_WRAPPER_STFLD:
835 case MONO_WRAPPER_ISINST: {
836 MonoClass *klass = decode_klass_ref (module, p, &p);
839 if (wrapper_type == MONO_WRAPPER_LDFLD)
840 ref->method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
841 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
842 ref->method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
843 else if (wrapper_type == MONO_WRAPPER_STFLD)
844 ref->method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
845 else if (wrapper_type == MONO_WRAPPER_ISINST)
846 ref->method = mono_marshal_get_isinst (klass);
848 g_assert_not_reached ();
851 case MONO_WRAPPER_LDFLD_REMOTE:
852 ref->method = mono_marshal_get_ldfld_remote_wrapper (NULL);
854 case MONO_WRAPPER_STFLD_REMOTE:
855 ref->method = mono_marshal_get_stfld_remote_wrapper (NULL);
858 case MONO_WRAPPER_ALLOC: {
859 int atype = decode_value (p, &p);
861 ref->method = mono_gc_get_managed_allocator_by_type (atype);
862 g_assert (ref->method);
865 case MONO_WRAPPER_WRITE_BARRIER:
866 ref->method = mono_gc_get_write_barrier ();
868 case MONO_WRAPPER_STELEMREF: {
869 int subtype = decode_value (p, &p);
871 if (subtype == WRAPPER_SUBTYPE_NONE) {
872 ref->method = mono_marshal_get_stelemref ();
873 } else if (subtype == WRAPPER_SUBTYPE_VIRTUAL_STELEMREF) {
877 kind = decode_value (p, &p);
879 /* Can't decode this */
882 if (target->wrapper_type == MONO_WRAPPER_STELEMREF) {
883 info = mono_marshal_get_wrapper_info (target);
886 if (info->subtype == subtype && info->d.virtual_stelemref.kind == kind)
887 ref->method = target;
894 g_assert_not_reached ();
898 case MONO_WRAPPER_SYNCHRONIZED: {
899 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
903 ref->method = mono_marshal_get_synchronized_wrapper (m);
906 case MONO_WRAPPER_UNKNOWN: {
907 int subtype = decode_value (p, &p);
909 if (subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE || subtype == WRAPPER_SUBTYPE_STRUCTURE_TO_PTR) {
910 MonoClass *klass = decode_klass_ref (module, p, &p);
917 if (klass != target->klass)
920 if (subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE) {
921 if (strcmp (target->name, "PtrToStructure"))
923 ref->method = mono_marshal_get_ptr_to_struct (klass);
925 if (strcmp (target->name, "StructureToPtr"))
927 ref->method = mono_marshal_get_struct_to_ptr (klass);
929 } else if (subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER) {
930 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
934 ref->method = mono_marshal_get_synchronized_inner_wrapper (m);
935 } else if (subtype == WRAPPER_SUBTYPE_ARRAY_ACCESSOR) {
936 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
940 ref->method = mono_marshal_get_array_accessor_wrapper (m);
941 } else if (subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN) {
942 ref->method = mono_marshal_get_gsharedvt_in_wrapper ();
943 } else if (subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT) {
944 ref->method = mono_marshal_get_gsharedvt_out_wrapper ();
946 g_assert_not_reached ();
950 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
951 int subtype = decode_value (p, &p);
953 if (subtype == WRAPPER_SUBTYPE_ELEMENT_ADDR) {
954 int rank = decode_value (p, &p);
955 int elem_size = decode_value (p, &p);
957 ref->method = mono_marshal_get_array_address (rank, elem_size);
958 } else if (subtype == WRAPPER_SUBTYPE_STRING_CTOR) {
962 m = decode_resolve_method_ref (module, p, &p);
968 g_assert (target->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED);
970 info = mono_marshal_get_wrapper_info (target);
971 if (info && info->subtype == subtype && info->d.string_ctor.method == m)
972 ref->method = target;
978 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
980 int subtype = decode_value (p, &p);
983 if (subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER) {
988 if (strcmp (target->name, name) != 0)
990 ref->method = target;
992 m = decode_resolve_method_ref (module, p, &p);
997 /* This should only happen when looking for an extra method */
1000 if (mono_marshal_method_from_wrapper (target) == m)
1001 ref->method = target;
1007 case MONO_WRAPPER_CASTCLASS: {
1008 int subtype = decode_value (p, &p);
1010 if (subtype == WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE)
1011 ref->method = mono_marshal_get_castclass_with_cache ();
1012 else if (subtype == WRAPPER_SUBTYPE_ISINST_WITH_CACHE)
1013 ref->method = mono_marshal_get_isinst_with_cache ();
1015 g_assert_not_reached ();
1018 case MONO_WRAPPER_RUNTIME_INVOKE: {
1019 int subtype = decode_value (p, &p);
1024 if (subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC) {
1025 if (strcmp (target->name, "runtime_invoke_dynamic") != 0)
1027 ref->method = target;
1028 } else if (subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT) {
1029 /* Direct wrapper */
1030 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
1034 ref->method = mono_marshal_get_runtime_invoke (m, FALSE);
1035 } else if (subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL) {
1036 /* Virtual direct wrapper */
1037 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
1041 ref->method = mono_marshal_get_runtime_invoke (m, TRUE);
1043 MonoMethodSignature *sig;
1046 sig = decode_signature_with_target (module, NULL, p, &p);
1047 info = mono_marshal_get_wrapper_info (target);
1050 if (info->subtype != subtype)
1052 g_assert (info->d.runtime_invoke.sig);
1053 if (mono_metadata_signature_equal (sig, info->d.runtime_invoke.sig))
1054 ref->method = target;
1060 case MONO_WRAPPER_DELEGATE_INVOKE:
1061 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
1062 case MONO_WRAPPER_DELEGATE_END_INVOKE: {
1063 gboolean is_inflated = decode_value (p, &p);
1064 WrapperSubtype subtype;
1068 MonoMethod *invoke, *wrapper;
1070 klass = decode_klass_ref (module, p, &p);
1074 switch (wrapper_type) {
1075 case MONO_WRAPPER_DELEGATE_INVOKE:
1076 invoke = mono_get_delegate_invoke (klass);
1077 wrapper = mono_marshal_get_delegate_invoke (invoke, NULL);
1079 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
1080 invoke = mono_get_delegate_begin_invoke (klass);
1081 wrapper = mono_marshal_get_delegate_begin_invoke (invoke);
1083 case MONO_WRAPPER_DELEGATE_END_INVOKE:
1084 invoke = mono_get_delegate_end_invoke (klass);
1085 wrapper = mono_marshal_get_delegate_end_invoke (invoke);
1088 g_assert_not_reached ();
1091 if (target && wrapper != target)
1093 ref->method = wrapper;
1096 * These wrappers are associated with a signature, not with a method.
1097 * Since we can't decode them into methods, they need a target method.
1102 if (wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
1105 subtype = decode_value (p, &p);
1106 info = mono_marshal_get_wrapper_info (target);
1108 if (info->subtype != subtype)
1111 if (subtype != WRAPPER_SUBTYPE_NONE)
1115 if (sig_matches_target (module, target, p, &p))
1116 ref->method = target;
1122 case MONO_WRAPPER_NATIVE_TO_MANAGED: {
1126 m = decode_resolve_method_ref (module, p, &p);
1129 klass = decode_klass_ref (module, p, &p);
1132 ref->method = mono_marshal_get_managed_wrapper (m, klass, 0);
1136 g_assert_not_reached ();
1138 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
1139 image_index = decode_value (p, &p);
1140 ref->token = decode_value (p, &p);
1142 image = load_image (module, image_index, TRUE);
1145 } else if (image_index == MONO_AOT_METHODREF_GINST) {
1148 MonoGenericContext ctx;
1151 * These methods do not have a token which resolves them, so we
1152 * resolve them immediately.
1154 klass = decode_klass_ref (module, p, &p);
1158 if (target && target->klass != klass)
1161 image_index = decode_value (p, &p);
1162 ref->token = decode_value (p, &p);
1164 image = load_image (module, image_index, TRUE);
1168 ref->method = mono_get_method_full (image, ref->token, NULL, NULL);
1172 memset (&ctx, 0, sizeof (ctx));
1174 if (FALSE && klass->generic_class) {
1175 ctx.class_inst = klass->generic_class->context.class_inst;
1176 ctx.method_inst = NULL;
1178 ref->method = mono_class_inflate_generic_method_full_checked (ref->method, klass, &ctx, &error);
1179 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
1182 memset (&ctx, 0, sizeof (ctx));
1184 if (!decode_generic_context (module, &ctx, p, &p))
1187 ref->method = mono_class_inflate_generic_method_full_checked (ref->method, klass, &ctx, &error);
1188 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
1189 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
1193 klass = decode_klass_ref (module, p, &p);
1196 method_type = decode_value (p, &p);
1197 switch (method_type) {
1199 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
1202 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
1205 ref->method = mono_class_get_method_from_name (klass, "Get", -1);
1208 ref->method = mono_class_get_method_from_name (klass, "Address", -1);
1211 ref->method = mono_class_get_method_from_name (klass, "Set", -1);
1214 g_assert_not_reached ();
1217 if (image_index == MONO_AOT_METHODREF_LARGE_IMAGE_INDEX) {
1218 image_index = decode_value (p, &p);
1219 value = decode_value (p, &p);
1222 ref->token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1224 image = load_image (module, image_index, TRUE);
1237 decode_method_ref (MonoAotModule *module, MethodRef *ref, guint8 *buf, guint8 **endbuf)
1239 return decode_method_ref_with_target (module, ref, NULL, buf, endbuf);
1243 * decode_resolve_method_ref_with_target:
1245 * Similar to decode_method_ref, but resolve and return the method itself.
1248 decode_resolve_method_ref_with_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
1253 res = decode_method_ref_with_target (module, &ref, target, buf, endbuf);
1260 return mono_get_method (ref.image, ref.token, NULL);
1264 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
1266 return decode_resolve_method_ref_with_target (module, NULL, buf, endbuf);
1269 #ifdef ENABLE_AOT_CACHE
1275 * - Add options for controlling the cache size
1276 * - Handle full cache by deleting old assemblies lru style
1277 * - Maybe add a threshold after an assembly is AOT compiled
1278 * - Add options for enabling this for specific main assemblies
1281 /* The cache directory */
1282 static char *cache_dir;
1284 /* The number of assemblies AOTed in this run */
1285 static int cache_count;
1287 /* Whenever to AOT in-process */
1288 static gboolean in_process;
1291 collect_assemblies (gpointer data, gpointer user_data)
1293 MonoAssembly *ass = data;
1294 GSList **l = user_data;
1296 *l = g_slist_prepend (*l, ass);
1299 #define SHA1_DIGEST_LENGTH 20
1302 * get_aot_config_hash:
1304 * Return a hash for all the version information an AOT module depends on.
1306 static G_GNUC_UNUSED char*
1307 get_aot_config_hash (MonoAssembly *assembly)
1310 GSList *l, *assembly_list = NULL;
1313 guint8 digest [SHA1_DIGEST_LENGTH];
1316 build_info = mono_get_runtime_build_info ();
1318 s = g_string_new (build_info);
1320 mono_assembly_foreach (collect_assemblies, &assembly_list);
1323 * The assembly list includes the current assembly as well, no need
1326 for (l = assembly_list; l; l = l->next) {
1327 MonoAssembly *ass = l->data;
1329 g_string_append (s, "_");
1330 g_string_append (s, ass->aname.name);
1331 g_string_append (s, "_");
1332 g_string_append (s, ass->image->guid);
1335 for (i = 0; i < s->len; ++i) {
1336 if (!isalnum (s->str [i]) && s->str [i] != '-')
1340 mono_sha1_get_digest ((guint8*)s->str, s->len, digest);
1342 digest_str = g_malloc0 ((SHA1_DIGEST_LENGTH * 2) + 1);
1343 for (i = 0; i < SHA1_DIGEST_LENGTH; ++i)
1344 sprintf (digest_str + (i * 2), "%02x", digest [i]);
1346 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: file dependencies: %s, hash %s", s->str, digest_str);
1348 g_string_free (s, TRUE);
1354 aot_cache_init (void)
1358 enable_aot_cache = TRUE;
1363 * aot_cache_load_module:
1365 * Load the AOT image corresponding to ASSEMBLY from the aot cache, AOTing it if neccessary.
1368 aot_cache_load_module (MonoAssembly *assembly, char **aot_name)
1370 MonoAotCacheConfig *config;
1372 char *fname, *tmp2, *aot_options, *failure_fname;
1384 if (image_is_dynamic (assembly->image))
1387 /* Check in the list of assemblies enabled for aot caching */
1388 config = mono_get_aot_cache_config ();
1392 MonoDomain *domain = mono_domain_get ();
1393 MonoAssembly *entry_assembly = domain->entry_assembly;
1395 // FIXME: This cannot be used for mscorlib during startup, since entry_assembly is not set yet
1396 for (l = config->apps; l; l = l->next) {
1399 if ((entry_assembly && !strcmp (entry_assembly->aname.name, n)) || (!entry_assembly && !strcmp (assembly->aname.name, n)))
1407 for (l = config->assemblies; l; l = l->next) {
1410 if (!strcmp (assembly->aname.name, n))
1420 home = g_get_home_dir ();
1423 cache_dir = g_strdup_printf ("%s/Library/Caches/mono/aot-cache", home);
1424 if (!g_file_test (cache_dir, G_FILE_TEST_EXISTS|G_FILE_TEST_IS_DIR))
1425 g_mkdir_with_parents (cache_dir, 0777);
1429 * The same assembly can be used in multiple configurations, i.e. multiple
1430 * versions of the runtime, with multiple versions of dependent assemblies etc.
1431 * To handle this, we compute a version string containing all this information, hash it,
1432 * and use the hash as a filename suffix.
1434 hash = get_aot_config_hash (assembly);
1436 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, hash, MONO_SOLIB_EXT);
1437 fname = g_build_filename (cache_dir, tmp2, NULL);
1441 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: loading from cache: '%s'.", fname);
1442 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
1445 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: found in cache: '%s'.", fname);
1449 if (!strcmp (assembly->aname.name, "mscorlib") && !mscorlib_aot_loaded)
1451 * Can't AOT this during startup, so we AOT it when called later from
1452 * mono_aot_get_method ().
1456 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: not found.");
1458 /* Only AOT one assembly per run to avoid slowing down execution too much */
1459 if (cache_count > 0)
1463 /* Check for previous failure */
1464 failure_fname = g_strdup_printf ("%s.failure", fname);
1465 failure_file = fopen (failure_fname, "r");
1467 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: assembly '%s' previously failed to compile '%s' ('%s')... ", assembly->image->name, fname, failure_fname);
1468 g_free (failure_fname);
1471 g_free (failure_fname);
1472 fclose (failure_file);
1475 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: compiling assembly '%s', logfile: '%s.log'... ", assembly->image->name, fname);
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%s%s", fname, fname, config->aot_options ? "," : "", config->aot_options ? config->aot_options : "");
1487 /* Maybe due this in another thread ? */
1488 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
1490 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: compilation failed.");
1491 failure_fname = g_strdup_printf ("%s.failure", fname);
1492 failure_file = fopen (failure_fname, "a+");
1493 fclose (failure_file);
1494 g_free (failure_fname);
1496 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: compilation succeeded.");
1500 * - Avoid waiting for the aot process to finish ?
1501 * (less overhead, but multiple processes could aot the same assembly at the same time)
1510 logfile_name = g_strdup_printf ("%s/aot.log", cache_dir);
1511 logfile = fopen (logfile_name, "a+");
1512 g_free (logfile_name);
1514 dup2 (fileno (logfile), 1);
1515 dup2 (fileno (logfile), 2);
1517 aot_options = g_strdup_printf ("outfile=%s", fname);
1518 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
1526 waitpid (pid, &exit_status, 0);
1527 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
1528 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: failed.");
1530 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT: succeeded.");
1534 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
1542 aot_cache_init (void)
1547 aot_cache_load_module (MonoAssembly *assembly, char **aot_name)
1555 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
1559 guint16 *table, *entry;
1562 char *symbol = (char*)name;
1565 symbol = g_strdup_printf ("_%s", name);
1568 /* The first entry points to the hash */
1569 table = globals [0];
1572 table_size = table [0];
1575 hash = mono_metadata_str_hash (symbol) % table_size;
1577 entry = &table [hash * 2];
1579 /* Search the hash for the index into the globals table */
1581 while (entry [0] != 0) {
1582 guint32 index = entry [0] - 1;
1583 guint32 next = entry [1];
1585 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
1587 if (!strcmp (globals [index * 2], symbol)) {
1588 global_index = index;
1593 entry = &table [next * 2];
1599 if (global_index != -1)
1600 *value = globals [global_index * 2 + 1];
1607 char *err = mono_dl_symbol (module, name, value);
1615 check_usable (MonoAssembly *assembly, MonoAotFileInfo *info, char **out_msg)
1619 gboolean usable = TRUE;
1622 guint32 excluded_cpu_optimizations;
1624 if (strcmp (assembly->image->guid, info->assembly_guid)) {
1625 msg = g_strdup_printf ("doesn't match assembly");
1629 build_info = mono_get_runtime_build_info ();
1630 if (strlen (info->runtime_version) > 0 && strcmp (info->runtime_version, build_info)) {
1631 msg = g_strdup_printf ("compiled against runtime version '%s' while this runtime has version '%s'", info->runtime_version, build_info);
1634 g_free (build_info);
1636 full_aot = info->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1638 if (mono_aot_only && !full_aot) {
1639 msg = g_strdup_printf ("not compiled with --aot=full");
1642 if (!mono_aot_only && full_aot) {
1643 msg = g_strdup_printf ("compiled with --aot=full");
1647 /* mono_arch_find_imt_method () requires this */
1648 if ((info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1649 msg = g_strdup_printf ("compiled against LLVM");
1652 if (!(info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && mono_use_llvm) {
1653 msg = g_strdup_printf ("not compiled against LLVM");
1657 if (mini_get_debug_options ()->mdb_optimizations && !(info->flags & MONO_AOT_FILE_FLAG_DEBUG) && !full_aot) {
1658 msg = g_strdup_printf ("not compiled for debugging");
1662 mono_arch_cpu_optimizations (&excluded_cpu_optimizations);
1663 if (info->opts & excluded_cpu_optimizations) {
1664 msg = g_strdup_printf ("compiled with unsupported CPU optimizations");
1668 if (!mono_aot_only && (info->simd_opts & ~mono_arch_cpu_enumerate_simd_versions ())) {
1669 msg = g_strdup_printf ("compiled with unsupported SIMD extensions");
1675 if (info->gc_name_index != -1) {
1676 char *gc_name = (char*)&blob [info->gc_name_index];
1677 const char *current_gc_name = mono_gc_get_gc_name ();
1679 if (strcmp (current_gc_name, gc_name) != 0) {
1680 msg = g_strdup_printf ("compiled against GC %s, while the current runtime uses GC %s.\n", gc_name, current_gc_name);
1690 * TABLE should point to a table of call instructions. Return the address called by the INDEXth entry.
1693 get_call_table_entry (void *table, int index)
1695 #if defined(TARGET_ARM)
1700 ins_addr = (guint32*)table + index;
1702 if ((ins >> ARMCOND_SHIFT) == ARMCOND_NV) {
1704 offset = (((int)(((ins & 0xffffff) << 1) | ((ins >> 24) & 0x1))) << 7) >> 7;
1705 return (char*)ins_addr + (offset * 2) + 8 + 1;
1707 offset = (((int)ins & 0xffffff) << 8) >> 8;
1708 return (char*)ins_addr + (offset * 4) + 8;
1710 #elif defined(TARGET_ARM64)
1711 return mono_arch_get_call_target ((guint8*)table + (index * 4) + 4);
1712 #elif defined(TARGET_X86) || defined(TARGET_AMD64)
1713 /* The callee expects an ip which points after the call */
1714 return mono_arch_get_call_target ((guint8*)table + (index * 5) + 5);
1716 g_assert_not_reached ();
1722 load_aot_module (MonoAssembly *assembly, gpointer user_data)
1725 MonoAotModule *amodule;
1727 gboolean usable = TRUE;
1728 char *version_symbol = NULL;
1730 gpointer *globals = NULL;
1731 MonoAotFileInfo *info = NULL;
1734 gboolean do_load_image = TRUE;
1735 int align_double, align_int64;
1737 if (mono_compile_aot)
1740 if (assembly->image->aot_module)
1742 * Already loaded. This can happen because the assembly loading code might invoke
1743 * the assembly load hooks multiple times for the same assembly.
1747 if (image_is_dynamic (assembly->image) || assembly->ref_only)
1750 if (mono_security_cas_enabled ())
1754 if (static_aot_modules)
1755 info = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
1763 /* Statically linked AOT module */
1764 aot_name = g_strdup_printf ("%s", assembly->aname.name);
1765 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
1766 globals = info->globals;
1768 if (enable_aot_cache)
1769 sofile = aot_cache_load_module (assembly, &aot_name);
1772 aot_name = g_strdup_printf ("%s%s", assembly->image->name, MONO_SOLIB_EXT);
1774 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1777 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module '%s' not found: %s\n", aot_name, err);
1780 aot_name = g_strdup_printf ("%s/mono/aot-cache/%s/%s%s", mono_assembly_getrootdir(), ARCHITECTURE, g_path_get_basename (assembly->image->name), MONO_SOLIB_EXT);
1781 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1783 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module '%s' not found: %s\n", aot_name, err);
1791 if (!sofile && !globals) {
1792 if (mono_aot_only && assembly->image->tables [MONO_TABLE_METHOD].rows) {
1793 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1801 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &version_symbol);
1802 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&info);
1805 if (version_symbol) {
1806 /* Old file format */
1807 version = atoi (version_symbol);
1810 version = info->version;
1813 if (version != MONO_AOT_FILE_VERSION) {
1814 msg = g_strdup_printf ("wrong file format version (expected %d got %d)", MONO_AOT_FILE_VERSION, version);
1817 usable = check_usable (assembly, info, &msg);
1821 if (mono_aot_only) {
1822 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode: %s.\n", aot_name, msg);
1825 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: module %s is unusable: %s.\n", aot_name, msg);
1830 mono_dl_close (sofile);
1831 assembly->image->aot_module = NULL;
1835 #if defined (TARGET_ARM) && defined (TARGET_MACH)
1840 memset (&t, 0, sizeof (MonoType));
1841 t.type = MONO_TYPE_R8;
1842 mono_type_size (&t, &align);
1843 align_double = align;
1845 memset (&t, 0, sizeof (MonoType));
1846 t.type = MONO_TYPE_I8;
1847 align_int64 = align;
1850 align_double = MONO_ABI_ALIGNOF (double);
1851 align_int64 = MONO_ABI_ALIGNOF (gint64);
1855 g_assert (info->double_align == align_double);
1856 g_assert (info->long_align == align_int64);
1857 g_assert (info->generic_tramp_num == MONO_TRAMPOLINE_NUM);
1861 amodule = g_new0 (MonoAotModule, 1);
1862 amodule->aot_name = aot_name;
1863 amodule->assembly = assembly;
1865 memcpy (&amodule->info, info, sizeof (*info));
1867 amodule->got = amodule->info.got;
1868 amodule->llvm_got = amodule->info.llvm_got;
1869 amodule->globals = globals;
1870 amodule->sofile = sofile;
1871 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1872 amodule->blob = blob;
1874 mono_mutex_init_recursive (&amodule->mutex);
1876 /* Read image table */
1878 guint32 table_len, i;
1881 table = info->image_table;
1884 table_len = *(guint32*)table;
1885 table += sizeof (guint32);
1886 amodule->image_table = g_new0 (MonoImage*, table_len);
1887 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1888 amodule->image_guids = g_new0 (char*, table_len);
1889 amodule->image_table_len = table_len;
1890 for (i = 0; i < table_len; ++i) {
1891 MonoAssemblyName *aname = &(amodule->image_names [i]);
1893 aname->name = g_strdup (table);
1894 table += strlen (table) + 1;
1895 amodule->image_guids [i] = g_strdup (table);
1896 table += strlen (table) + 1;
1898 aname->culture = g_strdup (table);
1899 table += strlen (table) + 1;
1900 memcpy (aname->public_key_token, table, strlen (table) + 1);
1901 table += strlen (table) + 1;
1903 table = ALIGN_PTR_TO (table, 8);
1904 aname->flags = *(guint32*)table;
1906 aname->major = *(guint32*)table;
1908 aname->minor = *(guint32*)table;
1910 aname->build = *(guint32*)table;
1912 aname->revision = *(guint32*)table;
1917 amodule->method_addresses = info->method_addresses;
1918 amodule->code = info->methods;
1920 /* Mask out thumb interop bit */
1921 amodule->code = (void*)((mgreg_t)amodule->code & ~1);
1923 amodule->jit_code_start = info->jit_code_start;
1924 amodule->jit_code_end = info->jit_code_end;
1925 amodule->method_info_offsets = info->method_info_offsets;
1926 amodule->ex_info_offsets = info->ex_info_offsets;
1927 amodule->class_info_offsets = info->class_info_offsets;
1928 amodule->class_name_table = info->class_name_table;
1929 amodule->extra_method_table = info->extra_method_table;
1930 amodule->extra_method_info_offsets = info->extra_method_info_offsets;
1931 amodule->unbox_trampolines = info->unbox_trampolines;
1932 amodule->unbox_trampolines_end = info->unbox_trampolines_end;
1933 amodule->unbox_trampoline_addresses = info->unbox_trampoline_addresses;
1934 amodule->unwind_info = info->unwind_info;
1935 amodule->mem_end = info->mem_end;
1936 amodule->mem_begin = amodule->code;
1937 amodule->plt = info->plt;
1938 amodule->plt_end = info->plt_end;
1939 amodule->mono_eh_frame = info->mono_eh_frame;
1940 amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC] = info->specific_trampolines;
1941 amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = info->static_rgctx_trampolines;
1942 amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK] = info->imt_thunks;
1943 amodule->trampolines [MONO_AOT_TRAMP_GSHAREDVT_ARG] = info->gsharedvt_arg_trampolines;
1944 amodule->thumb_end = info->thumb_end;
1946 /* Compute code_offsets from the method addresses */
1947 amodule->code_offsets = g_malloc0 (amodule->info.nmethods * sizeof (gint32));
1948 for (i = 0; i < amodule->info.nmethods; ++i) {
1949 /* method_addresses () contains a table of branches, since the ios linker can update those correctly */
1952 addr = get_call_table_entry (amodule->method_addresses, i);
1954 if (addr == amodule->method_addresses)
1955 amodule->code_offsets [i] = 0xffffffff;
1957 amodule->code_offsets [i] = (char*)addr - (char*)amodule->code;
1960 if (make_unreadable) {
1961 #ifndef TARGET_WIN32
1963 guint8 *page_start, *page_end;
1966 addr = amodule->mem_begin;
1967 len = amodule->mem_end - amodule->mem_begin;
1969 /* Round down in both directions to avoid modifying data which is not ours */
1970 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1971 page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
1972 if (page_end > page_start) {
1973 err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
1974 g_assert (err == 0);
1979 /* Compute the boundaries of LLVM code */
1980 if (info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM)
1981 compute_llvm_code_range (amodule, &amodule->llvm_code_start, &amodule->llvm_code_end);
1985 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->jit_code_start);
1986 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->jit_code_end);
1987 if (amodule->llvm_code_start) {
1988 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->llvm_code_start);
1989 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->llvm_code_end);
1992 g_hash_table_insert (aot_modules, assembly, amodule);
1995 mono_jit_info_add_aot_module (assembly->image, amodule->jit_code_start, amodule->jit_code_end);
1996 if (amodule->llvm_code_start)
1997 mono_jit_info_add_aot_module (assembly->image, amodule->llvm_code_start, amodule->llvm_code_end);
1999 assembly->image->aot_module = amodule;
2001 amodule->got [0] = assembly->image;
2003 if (mono_aot_only) {
2005 find_symbol (amodule->sofile, amodule->globals, "specific_trampolines_page", (gpointer *)&code);
2006 amodule->use_page_trampolines = code != NULL;
2007 /*g_warning ("using page trampolines: %d", amodule->use_page_trampolines);*/
2008 if (mono_defaults.corlib) {
2009 /* The second got slot contains the mscorlib got addr */
2010 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
2012 amodule->got [1] = mscorlib_amodule->got;
2014 amodule->got [1] = amodule->got;
2018 if (mono_gc_is_moving ()) {
2021 memset (&ji, 0, sizeof (ji));
2022 ji.type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
2024 amodule->got [2] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
2027 if (amodule->llvm_got) {
2028 amodule->llvm_got [0] = amodule->got [0];
2029 amodule->llvm_got [1] = amodule->got [1];
2030 amodule->llvm_got [2] = amodule->got [2];
2034 * Since we store methoddef and classdef tokens when referring to methods/classes in
2035 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
2036 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
2037 * non-lazily, since we can't handle out-of-date errors later.
2038 * The cached class info also depends on the exact assemblies.
2040 #if defined(__native_client__)
2041 /* TODO: Don't 'load_image' on mscorlib due to a */
2042 /* recursive loading problem. This should be */
2043 /* removed if mscorlib is loaded from disk. */
2044 if (strncmp(assembly->aname.name, "mscorlib", 8)) {
2045 do_load_image = TRUE;
2047 do_load_image = FALSE;
2050 if (do_load_image) {
2051 for (i = 0; i < amodule->image_table_len; ++i)
2052 load_image (amodule, i, FALSE);
2055 if (amodule->out_of_date) {
2056 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);
2057 if (mono_aot_only) {
2058 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);
2063 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT: loaded AOT Module for %s.\n", assembly->image->name);
2067 * mono_aot_register_globals:
2069 * This is called by the ctor function in AOT images compiled with the
2070 * 'no-dlsym' option.
2073 mono_aot_register_globals (gpointer *globals)
2075 g_assert_not_reached ();
2079 * mono_aot_register_module:
2081 * This should be called by embedding code to register AOT modules statically linked
2082 * into the executable. AOT_INFO should be the value of the
2083 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
2086 mono_aot_register_module (gpointer *aot_info)
2090 MonoAotFileInfo *info = (gpointer)aot_info;
2092 g_assert (info->version == MONO_AOT_FILE_VERSION);
2094 globals = info->globals;
2097 aname = info->assembly_name;
2099 /* This could be called before startup */
2103 if (!static_aot_modules)
2104 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
2106 g_hash_table_insert (static_aot_modules, aname, info);
2113 mono_aot_init (void)
2115 mono_mutex_init_recursive (&aot_mutex);
2116 mono_mutex_init_recursive (&aot_page_mutex);
2117 aot_modules = g_hash_table_new (NULL, NULL);
2119 #ifndef __native_client__
2120 mono_install_assembly_load_hook (load_aot_module, NULL);
2122 mono_counters_register ("Async JIT info size", MONO_COUNTER_INT|MONO_COUNTER_JIT, &async_jit_info_size);
2124 if (g_getenv ("MONO_LASTAOT"))
2125 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
2130 mono_aot_cleanup (void)
2132 if (aot_jit_icall_hash)
2133 g_hash_table_destroy (aot_jit_icall_hash);
2135 g_hash_table_destroy (aot_modules);
2139 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
2145 info->vtable_size = decode_value (buf, &buf);
2146 if (info->vtable_size == -1)
2149 flags = decode_value (buf, &buf);
2150 info->ghcimpl = (flags >> 0) & 0x1;
2151 info->has_finalize = (flags >> 1) & 0x1;
2152 info->has_cctor = (flags >> 2) & 0x1;
2153 info->has_nested_classes = (flags >> 3) & 0x1;
2154 info->blittable = (flags >> 4) & 0x1;
2155 info->has_references = (flags >> 5) & 0x1;
2156 info->has_static_refs = (flags >> 6) & 0x1;
2157 info->no_special_static_fields = (flags >> 7) & 0x1;
2158 info->is_generic_container = (flags >> 8) & 0x1;
2160 if (info->has_cctor) {
2161 res = decode_method_ref (module, &ref, buf, &buf);
2164 info->cctor_token = ref.token;
2166 if (info->has_finalize) {
2167 res = decode_method_ref (module, &ref, buf, &buf);
2170 info->finalize_image = ref.image;
2171 info->finalize_token = ref.token;
2174 info->instance_size = decode_value (buf, &buf);
2175 info->class_size = decode_value (buf, &buf);
2176 info->packing_size = decode_value (buf, &buf);
2177 info->min_align = decode_value (buf, &buf);
2185 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2188 MonoClass *klass = vtable->klass;
2189 MonoAotModule *amodule = klass->image->aot_module;
2191 MonoCachedClassInfo class_info;
2196 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !amodule)
2199 info = &amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
2202 err = decode_cached_class_info (amodule, &class_info, p, &p);
2206 for (i = 0; i < slot; ++i)
2207 decode_method_ref (amodule, &ref, p, &p);
2209 res = decode_method_ref (amodule, &ref, p, &p);
2212 if (ref.no_aot_trampoline)
2215 if (mono_metadata_token_index (ref.token) == 0 || mono_metadata_token_table (ref.token) != MONO_TABLE_METHOD)
2218 return mono_aot_get_method_from_token (domain, ref.image, ref.token);
2222 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2224 MonoAotModule *amodule = klass->image->aot_module;
2228 if (klass->rank || !amodule)
2231 p = (guint8*)&amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
2233 err = decode_cached_class_info (amodule, res, p, &p);
2241 * mono_aot_get_class_from_name:
2243 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
2244 * using a cache stored in the AOT file.
2245 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
2247 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
2251 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2253 MonoAotModule *amodule = image->aot_module;
2254 guint16 *table, *entry;
2257 char full_name_buf [1024];
2259 const char *name2, *name_space2;
2261 guint32 cols [MONO_TYPEDEF_SIZE];
2262 GHashTable *nspace_table;
2264 if (!amodule || !amodule->class_name_table)
2267 amodule_lock (amodule);
2271 /* First look in the cache */
2272 if (!amodule->name_cache)
2273 amodule->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
2274 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
2276 *klass = g_hash_table_lookup (nspace_table, name);
2278 amodule_unlock (amodule);
2283 table_size = amodule->class_name_table [0];
2284 table = amodule->class_name_table + 1;
2286 if (name_space [0] == '\0')
2287 full_name = g_strdup_printf ("%s", name);
2289 if (strlen (name_space) + strlen (name) < 1000) {
2290 sprintf (full_name_buf, "%s.%s", name_space, name);
2291 full_name = full_name_buf;
2293 full_name = g_strdup_printf ("%s.%s", name_space, name);
2296 hash = mono_metadata_str_hash (full_name) % table_size;
2297 if (full_name != full_name_buf)
2300 entry = &table [hash * 2];
2302 if (entry [0] != 0) {
2303 t = &image->tables [MONO_TABLE_TYPEDEF];
2306 guint32 index = entry [0];
2307 guint32 next = entry [1];
2308 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
2310 name_table_accesses ++;
2312 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
2314 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
2315 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
2317 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
2319 amodule_unlock (amodule);
2320 *klass = mono_class_get_checked (image, token, &error);
2321 if (!mono_error_ok (&error))
2322 mono_error_cleanup (&error); /* FIXME don't swallow the error */
2326 amodule_lock (amodule);
2327 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
2328 if (!nspace_table) {
2329 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
2330 g_hash_table_insert (amodule->name_cache, (char*)name_space2, nspace_table);
2332 g_hash_table_insert (nspace_table, (char*)name2, *klass);
2333 amodule_unlock (amodule);
2339 entry = &table [next * 2];
2346 amodule_unlock (amodule);
2351 /* Compute the boundaries of the LLVM code for AMODULE. */
2353 compute_llvm_code_range (MonoAotModule *amodule, guint8 **code_start, guint8 **code_end)
2356 int version, fde_count;
2359 g_assert (amodule->mono_eh_frame);
2361 p = amodule->mono_eh_frame;
2363 /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
2367 g_assert (version == 3);
2370 p = ALIGN_PTR_TO (p, 4);
2372 fde_count = *(guint32*)p;
2376 if (fde_count > 1) {
2377 /* mono_aot_personality () */
2378 g_assert (table [0] == -1);
2379 *code_start = amodule->code + amodule->code_offsets [table [2]];
2380 *code_end = amodule->code + amodule->code_offsets [table [(fde_count - 1) * 2]] + table [fde_count * 2];
2388 * decode_mono_eh_frame:
2390 * Decode the EH information emitted by our modified LLVM compiler and construct a
2391 * MonoJitInfo structure from it.
2392 * LOCKING: Acquires the domain lock.
2395 decode_llvm_mono_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
2396 MonoMethod *method, guint8 *code,
2397 MonoJitExceptionInfo *clauses, int num_clauses,
2398 MonoJitInfoFlags flags,
2400 int *this_reg, int *this_offset)
2403 guint8 *fde, *cie, *code_start, *code_end;
2404 int version, fde_count;
2406 int i, j, pos, left, right, offset, offset1, offset2, code_len, func_encoding;
2407 MonoJitExceptionInfo *ei;
2408 guint32 fde_len, ei_len, nested_len, nindex;
2409 gpointer *type_info;
2411 MonoLLVMFDEInfo info;
2413 g_assert (amodule->mono_eh_frame);
2415 p = amodule->mono_eh_frame;
2417 /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
2421 g_assert (version == 3);
2425 p = ALIGN_PTR_TO (p, 4);
2427 fde_count = *(guint32*)p;
2431 /* There is +1 entry in the table */
2432 cie = p + ((fde_count + 1) * 8);
2434 /* Binary search in the table to find the entry for code */
2435 offset = code - amodule->code;
2439 pos = (left + right) / 2;
2441 /* The table contains method index/fde offset pairs */
2442 g_assert (table [(pos * 2)] != -1);
2443 offset1 = amodule->code_offsets [table [(pos * 2)]];
2444 if (pos + 1 == fde_count) {
2445 offset2 = amodule->llvm_code_end - amodule->llvm_code_start;
2447 g_assert (table [(pos + 1) * 2] != -1);
2448 offset2 = amodule->code_offsets [table [(pos + 1) * 2]];
2451 if (offset < offset1)
2453 else if (offset >= offset2)
2459 code_start = amodule->code + amodule->code_offsets [table [(pos * 2)]];
2460 if (pos + 1 == fde_count) {
2461 /* The +1 entry in the table contains the length of the last method */
2462 int len = table [(pos + 1) * 2];
2463 code_end = code_start + len;
2465 code_end = amodule->code + amodule->code_offsets [table [(pos + 1) * 2]];
2467 code_len = code_end - code_start;
2469 g_assert (code >= code_start && code < code_end);
2471 if (amodule->thumb_end && (guint8*)code_start < amodule->thumb_end)
2472 /* Clear thumb flag */
2473 code_start = (guint8*)(((mgreg_t)code_start) & ~1);
2475 fde = amodule->mono_eh_frame + table [(pos * 2) + 1];
2476 /* This won't overflow because there is +1 entry in the table */
2477 fde_len = table [(pos * 2) + 2 + 1] - table [(pos * 2) + 1];
2479 mono_unwind_decode_llvm_mono_fde (fde, fde_len, cie, code_start, &info);
2481 ei_len = info.ex_info_len;
2482 type_info = info.type_info;
2483 *this_reg = info.this_reg;
2484 *this_offset = info.this_offset;
2486 /* Count number of nested clauses */
2488 for (i = 0; i < ei_len; ++i) {
2489 /* This might be unaligned */
2490 gint32 cindex1 = read32 (type_info [i]);
2493 for (l = nesting [cindex1]; l; l = l->next) {
2494 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
2496 for (j = 0; j < ei_len; ++j) {
2497 gint32 cindex2 = read32 (type_info [j]);
2499 if (cindex2 == nesting_cindex)
2506 * LLVM might represent one IL region with multiple regions, so have to
2507 * allocate a new JI.
2510 mono_domain_alloc0_lock_free (domain, mono_jit_info_size (flags, ei_len + nested_len, 0));
2511 mono_jit_info_init (jinfo, method, code, code_len, flags, ei_len + nested_len, 0);
2513 jinfo->unwind_info = mono_cache_unwind_info (info.unw_info, info.unw_info_len);
2514 /* This signals that unwind_info points to a normal cached unwind info */
2515 jinfo->from_aot = 0;
2516 jinfo->from_llvm = 1;
2518 for (i = 0; i < ei_len; ++i) {
2520 * clauses contains the original IL exception info saved by the AOT
2521 * compiler, we have to combine that with the information produced by LLVM
2523 /* The type_info entries contain IL clause indexes */
2524 int clause_index = read32 (type_info [i]);
2525 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
2526 MonoJitExceptionInfo *orig_jei = &clauses [clause_index];
2528 g_assert (clause_index < num_clauses);
2529 jei->flags = orig_jei->flags;
2530 jei->data.catch_class = orig_jei->data.catch_class;
2532 jei->try_start = ei [i].try_start;
2533 jei->try_end = ei [i].try_end;
2534 jei->handler_start = ei [i].handler_start;
2536 /* Make sure we transition to thumb when a handler starts */
2537 if (amodule->thumb_end && (guint8*)jei->handler_start < amodule->thumb_end)
2538 jei->handler_start = (void*)((mgreg_t)jei->handler_start + 1);
2541 /* See exception_cb () in mini-llvm.c as to why this is needed */
2543 for (i = 0; i < ei_len; ++i) {
2544 gint32 cindex1 = read32 (type_info [i]);
2547 for (l = nesting [cindex1]; l; l = l->next) {
2548 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
2550 for (j = 0; j < ei_len; ++j) {
2551 gint32 cindex2 = read32 (type_info [j]);
2553 if (cindex2 == nesting_cindex) {
2555 * The try interval comes from the nested clause, everything else from the
2558 memcpy (&jinfo->clauses [nindex], &jinfo->clauses [j], sizeof (MonoJitExceptionInfo));
2559 jinfo->clauses [nindex].try_start = jinfo->clauses [i].try_start;
2560 jinfo->clauses [nindex].try_end = jinfo->clauses [i].try_end;
2566 g_assert (nindex == ei_len + nested_len);
2572 alloc0_jit_info_data (MonoDomain *domain, int size, gboolean async_context)
2576 if (async_context) {
2577 res = mono_domain_alloc0_lock_free (domain, size);
2578 InterlockedExchangeAdd (&async_jit_info_size, size);
2580 res = mono_domain_alloc0 (domain, size);
2586 * LOCKING: Acquires the domain lock.
2587 * In async context, this is async safe.
2590 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
2591 MonoMethod *method, guint8* ex_info, guint8 *addr,
2592 guint8 *code, guint32 code_len)
2594 int i, buf_len, num_clauses, len;
2596 MonoJitInfoFlags flags = JIT_INFO_NONE;
2597 guint unwind_info, eflags;
2598 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points, has_try_block_holes, has_arch_eh_jit_info;
2599 gboolean from_llvm, has_gc_map;
2601 int generic_info_size, try_holes_info_size, num_holes, arch_eh_jit_info_size;
2602 int this_reg = 0, this_offset = 0;
2605 /* Load the method info from the AOT file */
2606 async = mono_thread_info_is_async_context ();
2609 eflags = decode_value (p, &p);
2610 has_generic_jit_info = (eflags & 1) != 0;
2611 has_dwarf_unwind_info = (eflags & 2) != 0;
2612 has_clauses = (eflags & 4) != 0;
2613 has_seq_points = (eflags & 8) != 0;
2614 from_llvm = (eflags & 16) != 0;
2615 has_try_block_holes = (eflags & 32) != 0;
2616 has_gc_map = (eflags & 64) != 0;
2617 has_arch_eh_jit_info = (eflags & 128) != 0;
2619 if (has_dwarf_unwind_info) {
2620 unwind_info = decode_value (p, &p);
2621 g_assert (unwind_info < (1 << 30));
2623 unwind_info = decode_value (p, &p);
2625 if (has_generic_jit_info) {
2626 flags |= JIT_INFO_HAS_GENERIC_JIT_INFO;
2627 generic_info_size = sizeof (MonoGenericJitInfo);
2629 generic_info_size = 0;
2632 if (has_try_block_holes) {
2633 num_holes = decode_value (p, &p);
2634 flags |= JIT_INFO_HAS_TRY_BLOCK_HOLES;
2635 try_holes_info_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
2637 num_holes = try_holes_info_size = 0;
2640 if (has_arch_eh_jit_info) {
2641 flags |= JIT_INFO_HAS_ARCH_EH_INFO;
2642 arch_eh_jit_info_size = sizeof (MonoArchEHJitInfo);
2643 /* Overwrite the original code_len which includes alignment padding */
2644 code_len = decode_value (p, &p);
2646 arch_eh_jit_info_size = 0;
2649 /* Exception table */
2651 num_clauses = decode_value (p, &p);
2656 MonoJitExceptionInfo *clauses;
2663 * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
2666 clauses = g_new0 (MonoJitExceptionInfo, num_clauses);
2667 nesting = g_new0 (GSList*, num_clauses);
2669 for (i = 0; i < num_clauses; ++i) {
2670 MonoJitExceptionInfo *ei = &clauses [i];
2672 ei->flags = decode_value (p, &p);
2674 if (decode_value (p, &p))
2675 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
2677 /* Read the list of nesting clauses */
2679 int nesting_index = decode_value (p, &p);
2680 if (nesting_index == -1)
2682 nesting [i] = g_slist_prepend (nesting [i], GINT_TO_POINTER (nesting_index));
2686 jinfo = decode_llvm_mono_eh_frame (amodule, domain, method, code, clauses, num_clauses, flags, nesting, &this_reg, &this_offset);
2689 for (i = 0; i < num_clauses; ++i)
2690 g_slist_free (nesting [i]);
2693 len = mono_jit_info_size (flags, num_clauses, num_holes);
2694 jinfo = alloc0_jit_info_data (domain, len, async);
2695 mono_jit_info_init (jinfo, method, code, code_len, flags, num_clauses, num_holes);
2697 for (i = 0; i < jinfo->num_clauses; ++i) {
2698 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
2700 ei->flags = decode_value (p, &p);
2702 ei->exvar_offset = decode_value (p, &p);
2704 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
2705 ei->data.filter = code + decode_value (p, &p);
2707 int len = decode_value (p, &p);
2713 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
2717 ei->try_start = code + decode_value (p, &p);
2718 ei->try_end = code + decode_value (p, &p);
2719 ei->handler_start = code + decode_value (p, &p);
2722 jinfo->unwind_info = unwind_info;
2723 jinfo->domain_neutral = 0;
2724 jinfo->from_aot = 1;
2727 if (has_try_block_holes) {
2728 MonoTryBlockHoleTableJitInfo *table;
2730 g_assert (jinfo->has_try_block_holes);
2732 table = mono_jit_info_get_try_block_hole_table_info (jinfo);
2735 table->num_holes = (guint16)num_holes;
2736 for (i = 0; i < num_holes; ++i) {
2737 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
2738 hole->clause = decode_value (p, &p);
2739 hole->length = decode_value (p, &p);
2740 hole->offset = decode_value (p, &p);
2744 if (has_arch_eh_jit_info) {
2745 MonoArchEHJitInfo *eh_info;
2747 g_assert (jinfo->has_arch_eh_info);
2749 eh_info = mono_jit_info_get_arch_eh_info (jinfo);
2750 eh_info->stack_size = decode_value (p, &p);
2751 eh_info->epilog_size = decode_value (p, &p);
2755 /* The rest is not needed in async mode */
2756 jinfo->async = TRUE;
2757 jinfo->d.aot_info = amodule;
2762 if (has_generic_jit_info) {
2763 MonoGenericJitInfo *gi;
2766 g_assert (jinfo->has_generic_jit_info);
2768 gi = mono_jit_info_get_generic_jit_info (jinfo);
2771 gi->nlocs = decode_value (p, &p);
2773 gi->locations = alloc0_jit_info_data (domain, gi->nlocs * sizeof (MonoDwarfLocListEntry), async);
2774 for (i = 0; i < gi->nlocs; ++i) {
2775 MonoDwarfLocListEntry *entry = &gi->locations [i];
2777 entry->is_reg = decode_value (p, &p);
2778 entry->reg = decode_value (p, &p);
2780 entry->offset = decode_value (p, &p);
2782 entry->from = decode_value (p, &p);
2783 entry->to = decode_value (p, &p);
2788 gi->has_this = this_reg != -1;
2789 gi->this_reg = this_reg;
2790 gi->this_offset = this_offset;
2792 gi->has_this = decode_value (p, &p);
2793 gi->this_reg = decode_value (p, &p);
2794 gi->this_offset = decode_value (p, &p);
2798 len = decode_value (p, &p);
2802 jinfo->d.method = decode_resolve_method_ref (amodule, p, &p);
2804 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
2805 if (decode_value (p, &p)) {
2808 MonoGenericSharingContext *gsctx = gi->generic_sharing_context;
2810 n = decode_value (p, &p);
2812 gsctx->var_is_vt = alloc0_jit_info_data (domain, sizeof (gboolean) * n, async);
2813 for (i = 0; i < n; ++i)
2814 gsctx->var_is_vt [i] = decode_value (p, &p);
2816 n = decode_value (p, &p);
2818 gsctx->mvar_is_vt = alloc0_jit_info_data (domain, sizeof (gboolean) * n, async);
2819 for (i = 0; i < n; ++i)
2820 gsctx->mvar_is_vt [i] = decode_value (p, &p);
2825 if (method && has_seq_points) {
2826 MonoSeqPointInfo *seq_points;
2828 p += seq_point_info_read (&seq_points, p, FALSE);
2830 mono_domain_lock (domain);
2831 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
2832 mono_domain_unlock (domain);
2835 /* Load debug info */
2836 buf_len = decode_value (p, &p);
2838 mono_debug_add_aot_method (domain, method, code, p, buf_len);
2842 int map_size = decode_value (p, &p);
2843 /* The GC map requires 4 bytes of alignment */
2844 while ((guint64)(gsize)p % 4)
2850 if (amodule != jinfo->d.method->klass->image->aot_module) {
2853 ji_to_amodule = g_hash_table_new (NULL, NULL);
2854 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
2862 amodule_contains_code_addr (MonoAotModule *amodule, guint8 *code)
2864 return (code >= amodule->jit_code_start && code <= amodule->jit_code_end) ||
2865 (code >= amodule->llvm_code_start && code <= amodule->llvm_code_end);
2869 * mono_aot_get_unwind_info:
2871 * Return a pointer to the DWARF unwind info belonging to JI.
2874 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2876 MonoAotModule *amodule;
2878 guint8 *code = ji->code_start;
2881 amodule = ji->d.aot_info;
2883 amodule = jinfo_get_method (ji)->klass->image->aot_module;
2885 g_assert (ji->from_aot);
2887 if (!amodule_contains_code_addr (amodule, code)) {
2888 /* ji belongs to a different aot module than amodule */
2890 g_assert (ji_to_amodule);
2891 amodule = g_hash_table_lookup (ji_to_amodule, ji);
2893 g_assert (amodule_contains_code_addr (amodule, code));
2897 p = amodule->unwind_info + ji->unwind_info;
2898 *unwind_info_len = decode_value (p, &p);
2902 static G_GNUC_UNUSED int
2903 compare_ints (const void *a, const void *b)
2905 return *(gint32*)a - *(gint32*)b;
2909 msort_code_offsets_internal (gint32 *array, int lo, int hi, gint32 *scratch)
2911 int mid = (lo + hi) / 2;
2918 for (i = lo; i < hi; ++i)
2919 if (array [(i * 2)] > array [(i * 2) + 2])
2922 /* Already sorted */
2926 msort_code_offsets_internal (array, lo, mid, scratch);
2927 msort_code_offsets_internal (array, mid + 1, hi, scratch);
2929 if (array [mid * 2] < array [(mid + 1) * 2])
2935 for (i = lo; i <= hi; i ++) {
2936 if (t_lo <= mid && ((t_hi > hi) || array [t_lo * 2] < array [t_hi * 2])) {
2937 scratch [(i * 2)] = array [t_lo * 2];
2938 scratch [(i * 2) + 1] = array [(t_lo *2) + 1];
2941 scratch [(i * 2)] = array [t_hi * 2];
2942 scratch [(i * 2) + 1] = array [(t_hi *2) + 1];
2946 for (i = lo; i <= hi; ++i) {
2947 array [(i * 2)] = scratch [i * 2];
2948 array [(i * 2) + 1] = scratch [(i * 2) + 1];
2953 msort_code_offsets (gint32 *array, int len)
2957 scratch = g_new (gint32, len * 2);
2958 msort_code_offsets_internal (array, 0, len - 1, scratch);
2963 * mono_aot_find_jit_info:
2965 * In async context, the resulting MonoJitInfo will not have its method field set, and it will not be added
2966 * to the jit info tables.
2967 * FIXME: Large sizes in the lock free allocator
2970 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2972 int pos, left, right, offset, offset1, offset2, code_len;
2973 int method_index, table_len;
2975 MonoAotModule *amodule = image->aot_module;
2976 MonoMethod *method = NULL;
2978 guint8 *code, *code_end, *ex_info, *p;
2981 gint32 *code_offsets;
2988 nmethods = amodule->info.nmethods;
2990 if (domain != mono_get_root_domain ())
2994 if (!amodule_contains_code_addr (amodule, addr))
2997 async = mono_thread_info_is_async_context ();
2999 offset = (guint8*)addr - amodule->code;
3001 /* Compute a sorted table mapping code offsets to method indexes. */
3002 if (!amodule->sorted_code_offsets) {
3004 code_offsets = g_new0 (gint32, nmethods * 2);
3006 for (i = 0; i < nmethods; ++i) {
3007 /* Skip the -1 entries to speed up sorting */
3008 if (amodule->code_offsets [i] == 0xffffffff)
3010 code_offsets [(offsets_len * 2)] = amodule->code_offsets [i];
3011 code_offsets [(offsets_len *2) + 1] = i;
3014 /* Use a merge sort as this is mostly sorted */
3015 msort_code_offsets (code_offsets, offsets_len);
3016 //qsort (code_offsets, offsets_len, sizeof (gint32) * 2, compare_ints);
3017 for (i = 0; i < offsets_len -1; ++i)
3018 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
3020 amodule->sorted_code_offsets_len = offsets_len;
3021 mono_memory_barrier ();
3022 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
3023 /* Somebody got in before us */
3024 g_free (code_offsets);
3027 code_offsets = amodule->sorted_code_offsets;
3028 offsets_len = amodule->sorted_code_offsets_len;
3030 code_end = amodule->jit_code_end > amodule->llvm_code_end ? amodule->jit_code_end : amodule->llvm_code_end;
3032 /* Binary search in the sorted_code_offsets table */
3034 right = offsets_len;
3036 pos = (left + right) / 2;
3038 offset1 = code_offsets [(pos * 2)];
3039 if (pos + 1 == offsets_len) {
3040 if (amodule->code + offset1 >= amodule->jit_code_start && amodule->code + offset1 < amodule->jit_code_end)
3041 offset2 = amodule->jit_code_end - amodule->code;
3043 offset2 = amodule->llvm_code_end - amodule->code;
3045 offset2 = code_offsets [(pos + 1) * 2];
3048 if (offset < offset1)
3050 else if (offset >= offset2)
3056 g_assert (offset >= code_offsets [(pos * 2)]);
3057 if (pos + 1 < offsets_len)
3058 g_assert (offset < code_offsets [((pos + 1) * 2)]);
3059 method_index = code_offsets [(pos * 2) + 1];
3061 /* In async mode, jinfo is not added to the normal jit info table, so have to cache it ourselves */
3063 JitInfoMap *table = amodule->async_jit_info_table;
3067 len = table [0].method_index;
3068 for (i = 1; i < len; ++i) {
3069 if (table [i].method_index == method_index)
3070 return table [i].jinfo;
3075 code = &amodule->code [amodule->code_offsets [method_index]];
3076 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
3078 if (pos == offsets_len - 1) {
3079 if (code >= amodule->jit_code_start && code < amodule->jit_code_end)
3080 code_len = amodule->jit_code_end - code;
3082 code_len = amodule->llvm_code_end - code;
3084 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
3087 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
3089 /* Might be a wrapper/extra method */
3091 if (amodule->extra_methods) {
3092 amodule_lock (amodule);
3093 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
3094 amodule_unlock (amodule);
3100 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
3102 * This is hit for extra methods which are called directly, so they are
3103 * not in amodule->extra_methods.
3105 table_len = amodule->extra_method_info_offsets [0];
3106 table = amodule->extra_method_info_offsets + 1;
3113 pos = ((left + right) / 2);
3115 g_assert (pos < table_len);
3117 if (table [pos * 2] < method_index)
3119 else if (table [pos * 2] > method_index)
3125 p = amodule->blob + table [(pos * 2) + 1];
3126 method = decode_resolve_method_ref (amodule, p, &p);
3128 /* Happens when a random address is passed in which matches a not-yey called wrapper encoded using its name */
3131 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
3132 method = mono_get_method (image, token, NULL);
3139 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
3141 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
3143 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
3144 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
3146 /* Add it to the normal JitInfo tables */
3148 JitInfoMap *old_table, *new_table;
3152 * Use a simple inmutable table with linear search to cache async jit info entries.
3153 * This assumes that the number of entries is small.
3156 /* Copy the table, adding a new entry at the end */
3157 old_table = amodule->async_jit_info_table;
3159 len = old_table[0].method_index;
3162 new_table = alloc0_jit_info_data (domain, (len + 1) * sizeof (JitInfoMap), async);
3164 memcpy (new_table, old_table, len * sizeof (JitInfoMap));
3165 new_table [0].method_index = len + 1;
3166 new_table [len].method_index = method_index;
3167 new_table [len].jinfo = jinfo;
3169 mono_memory_barrier ();
3170 if (InterlockedCompareExchangePointer ((gpointer)&amodule->async_jit_info_table, new_table, old_table) == old_table)
3174 mono_jit_info_table_add (domain, jinfo);
3181 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
3189 case MONO_PATCH_INFO_METHOD:
3190 case MONO_PATCH_INFO_METHOD_JUMP:
3191 case MONO_PATCH_INFO_ICALL_ADDR:
3192 case MONO_PATCH_INFO_METHOD_RGCTX:
3193 case MONO_PATCH_INFO_METHOD_CODE_SLOT: {
3197 res = decode_method_ref (aot_module, &ref, p, &p);
3201 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)) {
3202 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (ref.image, ref.token));
3203 ji->type = MONO_PATCH_INFO_ABS;
3207 ji->data.method = ref.method;
3209 ji->data.method = mono_get_method (ref.image, ref.token, NULL);
3210 g_assert (ji->data.method);
3211 mono_class_init (ji->data.method->klass);
3215 case MONO_PATCH_INFO_INTERNAL_METHOD:
3216 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
3217 guint32 len = decode_value (p, &p);
3219 ji->data.name = (char*)p;
3223 case MONO_PATCH_INFO_METHODCONST:
3225 ji->data.method = decode_resolve_method_ref (aot_module, p, &p);
3226 if (!ji->data.method)
3229 case MONO_PATCH_INFO_VTABLE:
3230 case MONO_PATCH_INFO_CLASS:
3231 case MONO_PATCH_INFO_IID:
3232 case MONO_PATCH_INFO_ADJUSTED_IID:
3233 case MONO_PATCH_INFO_CLASS_INIT:
3235 ji->data.klass = decode_klass_ref (aot_module, p, &p);
3236 if (!ji->data.klass)
3239 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
3240 ji->data.del_tramp = mono_mempool_alloc0 (mp, sizeof (MonoDelegateClassMethodPair));
3241 ji->data.del_tramp->klass = decode_klass_ref (aot_module, p, &p);
3242 if (!ji->data.del_tramp->klass)
3244 if (decode_value (p, &p)) {
3245 ji->data.del_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
3246 if (!ji->data.del_tramp->method)
3249 ji->data.del_tramp->virtual = decode_value (p, &p) ? TRUE : FALSE;
3251 case MONO_PATCH_INFO_IMAGE:
3252 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
3253 if (!ji->data.image)
3256 case MONO_PATCH_INFO_FIELD:
3257 case MONO_PATCH_INFO_SFLDA:
3259 ji->data.field = decode_field_info (aot_module, p, &p);
3260 if (!ji->data.field)
3263 case MONO_PATCH_INFO_SWITCH:
3264 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
3265 ji->data.table->table_size = decode_value (p, &p);
3266 table = mono_domain_alloc (mono_domain_get (), sizeof (gpointer) * ji->data.table->table_size);
3267 ji->data.table->table = (MonoBasicBlock**)table;
3268 for (i = 0; i < ji->data.table->table_size; i++)
3269 table [i] = (gpointer)(gssize)decode_value (p, &p);
3271 case MONO_PATCH_INFO_R4: {
3274 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
3275 val = decode_value (p, &p);
3276 *(float*)ji->data.target = *(float*)&val;
3279 case MONO_PATCH_INFO_R8: {
3283 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
3285 val [0] = decode_value (p, &p);
3286 val [1] = decode_value (p, &p);
3287 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
3288 *(double*)ji->data.target = *(double*)&v;
3291 case MONO_PATCH_INFO_LDSTR:
3292 image = load_image (aot_module, decode_value (p, &p), TRUE);
3295 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
3297 case MONO_PATCH_INFO_RVA:
3298 case MONO_PATCH_INFO_DECLSEC:
3299 case MONO_PATCH_INFO_LDTOKEN:
3300 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3302 image = load_image (aot_module, decode_value (p, &p), TRUE);
3305 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
3307 ji->data.token->has_context = decode_value (p, &p);
3308 if (ji->data.token->has_context) {
3309 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
3314 case MONO_PATCH_INFO_EXC_NAME:
3315 ji->data.klass = decode_klass_ref (aot_module, p, &p);
3316 if (!ji->data.klass)
3318 ji->data.name = ji->data.klass->name;
3320 case MONO_PATCH_INFO_METHOD_REL:
3321 ji->data.offset = decode_value (p, &p);
3323 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
3324 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
3325 case MONO_PATCH_INFO_MONITOR_ENTER:
3326 case MONO_PATCH_INFO_MONITOR_ENTER_V4:
3327 case MONO_PATCH_INFO_MONITOR_EXIT:
3328 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
3329 case MONO_PATCH_INFO_JIT_TLS_ID:
3331 case MONO_PATCH_INFO_CASTCLASS_CACHE:
3332 ji->data.index = decode_value (p, &p);
3334 case MONO_PATCH_INFO_RGCTX_FETCH: {
3336 MonoJumpInfoRgctxEntry *entry;
3337 guint32 offset, val;
3340 offset = decode_value (p, &p);
3341 val = decode_value (p, &p);
3343 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
3344 p2 = aot_module->blob + offset;
3345 entry->method = decode_resolve_method_ref (aot_module, p2, &p2);
3346 entry->in_mrgctx = ((val & 1) > 0) ? TRUE : FALSE;
3347 entry->info_type = (val >> 1) & 0xff;
3348 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
3349 entry->data->type = (val >> 9) & 0xff;
3351 res = decode_patch (aot_module, mp, entry->data, p, &p);
3354 ji->data.rgctx_entry = entry;
3357 case MONO_PATCH_INFO_SEQ_POINT_INFO:
3359 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
3360 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
3362 imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
3363 imt_tramp->vt_offset = decode_value (p, &p);
3365 ji->data.imt_tramp = imt_tramp;
3368 case MONO_PATCH_INFO_SIGNATURE:
3369 ji->data.target = decode_signature (aot_module, p, &p);
3371 case MONO_PATCH_INFO_TLS_OFFSET:
3372 ji->data.target = GINT_TO_POINTER (decode_value (p, &p));
3374 case MONO_PATCH_INFO_GSHAREDVT_CALL: {
3375 MonoJumpInfoGSharedVtCall *info = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoGSharedVtCall));
3376 info->sig = decode_signature (aot_module, p, &p);
3377 g_assert (info->sig);
3378 info->method = decode_resolve_method_ref (aot_module, p, &p);
3379 g_assert (info->method);
3381 ji->data.target = info;
3384 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
3385 MonoGSharedVtMethodInfo *info = mono_mempool_alloc0 (mp, sizeof (MonoGSharedVtMethodInfo));
3388 info->method = decode_resolve_method_ref (aot_module, p, &p);
3389 g_assert (info->method);
3390 info->num_entries = decode_value (p, &p);
3391 info->count_entries = info->num_entries;
3392 info->entries = mono_mempool_alloc0 (mp, sizeof (MonoRuntimeGenericContextInfoTemplate) * info->num_entries);
3393 for (i = 0; i < info->num_entries; ++i) {
3394 MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
3396 template->info_type = decode_value (p, &p);
3397 switch (mini_rgctx_info_type_to_patch_info_type (template->info_type)) {
3398 case MONO_PATCH_INFO_CLASS: {
3399 MonoClass *klass = decode_klass_ref (aot_module, p, &p);
3402 template->data = &klass->byval_arg;
3405 case MONO_PATCH_INFO_FIELD:
3406 template->data = decode_field_info (aot_module, p, &p);
3407 if (!template->data)
3411 g_assert_not_reached ();
3415 ji->data.target = info;
3418 case MONO_PATCH_INFO_LDSTR_LIT: {
3419 int len = decode_value (p, &p);
3422 s = mono_mempool_alloc0 (mp, len + 1);
3423 memcpy (s, p, len + 1);
3426 ji->data.target = s;
3430 g_warning ("unhandled type %d", ji->type);
3431 g_assert_not_reached ();
3442 static MonoJumpInfo*
3443 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
3444 gboolean llvm, guint32 **got_slots,
3445 guint8 *buf, guint8 **endbuf)
3447 MonoJumpInfo *patches;
3451 guint32 *got_offsets;
3456 got = aot_module->llvm_got;
3457 got_offsets = aot_module->info.llvm_got_info_offsets;
3459 got = aot_module->got;
3460 got_offsets = aot_module->info.got_info_offsets;
3463 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
3465 *got_slots = g_malloc (sizeof (guint32) * n_patches);
3467 for (pindex = 0; pindex < n_patches; ++pindex) {
3468 MonoJumpInfo *ji = &patches [pindex];
3473 got_offset = decode_value (p, &p);
3475 shared_p = aot_module->blob + mono_aot_get_offset (got_offsets, got_offset);
3477 ji->type = decode_value (shared_p, &shared_p);
3479 /* See load_method () for SFLDA */
3480 if (got [got_offset] && ji->type != MONO_PATCH_INFO_SFLDA) {
3481 /* Already loaded */
3483 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
3488 (*got_slots) [pindex] = got_offset;
3495 g_free (*got_slots);
3502 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
3505 * Jump addresses cannot be patched by the trampoline code since it
3506 * does not have access to the caller's address. Instead, we collect
3507 * the addresses of the GOT slots pointing to a method, and patch
3508 * them after the method has been compiled.
3510 MonoJitDomainInfo *info = domain_jit_info (domain);
3513 mono_domain_lock (domain);
3514 if (!info->jump_target_got_slot_hash)
3515 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
3516 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
3517 list = g_slist_prepend (list, got_slot);
3518 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
3519 mono_domain_unlock (domain);
3525 * Load the method identified by METHOD_INDEX from the AOT image. Return a
3526 * pointer to the native code of the method, or NULL if not found.
3527 * METHOD might not be set if the caller only has the image/token info.
3530 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
3533 gboolean from_plt = method == NULL;
3535 int i, pindex, n_patches, used_strings;
3536 gboolean keep_patches = TRUE;
3538 MonoJitInfo *jinfo = NULL;
3539 guint8 *code, *info;
3541 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE) {
3543 /* The caller cannot handle this */
3544 g_assert_not_reached ();
3548 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
3549 /* Non shared AOT code can't be used in other appdomains */
3552 if (amodule->out_of_date)
3555 if (amodule->code_offsets [method_index] == 0xffffffff) {
3556 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
3560 method = mono_get_method (image, token, NULL);
3561 full_name = mono_method_full_name (method, TRUE);
3562 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: NOT FOUND: %s.", full_name);
3568 code = &amodule->code [amodule->code_offsets [method_index]];
3570 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
3572 if (!amodule->methods_loaded) {
3573 amodule_lock (amodule);
3574 if (!amodule->methods_loaded) {
3577 loaded = g_new0 (guint32, amodule->info.nmethods / 32 + 1);
3578 mono_memory_barrier ();
3579 amodule->methods_loaded = loaded;
3581 amodule_unlock (amodule);
3584 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
3587 if (mono_last_aot_method != -1) {
3588 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
3590 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
3592 method = mono_get_method (image, token, NULL);
3594 char *name = mono_method_full_name (method, TRUE);
3595 g_print ("LAST AOT METHOD: %s.\n", name);
3598 g_print ("LAST AOT METHOD: %p %d\n", code, method_index);
3606 klass = method->klass;
3607 decode_klass_ref (amodule, p, &p);
3609 klass = decode_klass_ref (amodule, p, &p);
3612 if (amodule->info.opts & MONO_OPT_SHARED)
3613 used_strings = decode_value (p, &p);
3617 for (i = 0; i < used_strings; i++) {
3618 guint token = decode_value (p, &p);
3619 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
3622 if (amodule->info.opts & MONO_OPT_SHARED)
3623 keep_patches = FALSE;
3625 n_patches = decode_value (p, &p);
3627 keep_patches = FALSE;
3630 MonoJumpInfo *patches;
3638 mp = mono_mempool_new ();
3640 if ((gpointer)code >= amodule->info.jit_code_start && (gpointer)code <= amodule->info.jit_code_end) {
3645 got = amodule->llvm_got;
3649 patches = load_patch_info (amodule, mp, n_patches, llvm, &got_slots, p, &p);
3650 if (patches == NULL)
3653 for (pindex = 0; pindex < n_patches; ++pindex) {
3654 MonoJumpInfo *ji = &patches [pindex];
3658 * For SFLDA, we need to call resolve_patch_target () since the GOT slot could have
3659 * been initialized by load_method () for a static cctor before the cctor has
3660 * finished executing (#23242).
3662 if (!got [got_slots [pindex]] || ji->type == MONO_PATCH_INFO_SFLDA) {
3663 addr = mono_resolve_patch_target (method, domain, code, ji, TRUE);
3664 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
3665 addr = mono_create_ftnptr (domain, addr);
3666 mono_memory_barrier ();
3667 got [got_slots [pindex]] = addr;
3668 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
3669 register_jump_target_got_slot (domain, ji->data.method, &(got [got_slots [pindex]]));
3671 ji->type = MONO_PATCH_INFO_NONE;
3677 mono_mempool_destroy (mp);
3680 if (mini_get_debug_options ()->load_aot_jit_info_eagerly)
3681 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
3683 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
3687 method = mono_get_method (image, token, NULL);
3689 full_name = mono_method_full_name (method, TRUE);
3692 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
3694 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: FOUND method %s [%p - %p %p]", full_name, code, code + jinfo->code_size, info);
3698 amodule_lock (amodule);
3700 InterlockedIncrement (&mono_jit_stats.methods_aot);
3702 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
3706 if (method && method->wrapper_type)
3707 g_hash_table_insert (amodule->method_to_code, method, code);
3709 amodule_unlock (amodule);
3711 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
3715 method = mono_get_method (image, token, NULL);
3718 mono_profiler_method_jit (method);
3719 jinfo = mono_jit_info_table_find (domain, (char*)code);
3721 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
3724 if (from_plt && klass && !klass->generic_container)
3725 mono_runtime_class_init (mono_class_vtable (domain, klass));
3730 /* FIXME: The space in domain->mp is wasted */
3731 if (amodule->info.opts & MONO_OPT_SHARED)
3732 /* No need to cache patches */
3733 mono_mempool_destroy (mp);
3742 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
3744 guint32 table_size, entry_size, hash;
3745 guint32 *table, *entry;
3747 static guint32 n_extra_decodes;
3749 if (!amodule || amodule->out_of_date)
3752 table_size = amodule->extra_method_table [0];
3753 table = amodule->extra_method_table + 1;
3756 hash = mono_aot_method_hash (method) % table_size;
3758 entry = &table [hash * entry_size];
3765 guint32 key = entry [0];
3766 guint32 value = entry [1];
3767 guint32 next = entry [entry_size - 1];
3771 p = amodule->blob + key;
3774 amodule_lock (amodule);
3775 if (!amodule->method_ref_to_method)
3776 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
3777 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
3778 amodule_unlock (amodule);
3780 m = decode_resolve_method_ref_with_target (amodule, method, p, &p);
3782 amodule_lock (amodule);
3783 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
3784 amodule_unlock (amodule);
3792 /* Special case: wrappers of shared generic methods */
3793 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
3794 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
3795 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
3796 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
3798 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
3804 /* Methods decoded needlessly */
3806 //printf ("%d %s %s %p\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE), orig_p);
3811 entry = &table [next * entry_size];
3820 add_module_cb (gpointer key, gpointer value, gpointer user_data)
3822 g_ptr_array_add ((GPtrArray*)user_data, value);
3826 * find_extra_method:
3828 * Try finding METHOD in the extra_method table in all AOT images.
3829 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
3830 * module where the method was found.
3833 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
3839 /* Try the method's module first */
3840 *out_amodule = method->klass->image->aot_module;
3841 index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
3842 if (index != 0xffffff)
3846 * Try all other modules.
3847 * This is needed because generic instances klass->image points to the image
3848 * containing the generic definition, but the native code is generated to the
3849 * AOT image which contains the reference.
3852 /* Make a copy to avoid doing the search inside the aot lock */
3853 modules = g_ptr_array_new ();
3855 g_hash_table_foreach (aot_modules, add_module_cb, modules);
3859 for (i = 0; i < modules->len; ++i) {
3860 MonoAotModule *amodule = g_ptr_array_index (modules, i);
3862 if (amodule != method->klass->image->aot_module)
3863 index = find_extra_method_in_amodule (amodule, method);
3864 if (index != 0xffffff) {
3865 *out_amodule = amodule;
3870 g_ptr_array_free (modules, TRUE);
3876 * mono_aot_get_method:
3878 * Return a pointer to the AOTed native code for METHOD if it can be found,
3880 * On platforms with function pointers, this doesn't return a function pointer.
3883 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3885 MonoClass *klass = method->klass;
3886 guint32 method_index;
3887 MonoAotModule *amodule = klass->image->aot_module;
3890 if (enable_aot_cache && !amodule && domain->entry_assembly && klass->image == mono_defaults.corlib) {
3891 /* This cannot be AOTed during startup, so do it now */
3892 if (!mscorlib_aot_loaded) {
3893 mscorlib_aot_loaded = TRUE;
3894 load_aot_module (klass->image->assembly, NULL);
3895 amodule = klass->image->aot_module;
3902 if (amodule->out_of_date)
3905 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3906 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3907 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3908 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
3912 * Use the original method instead of its invoke-with-check wrapper.
3913 * This is not a problem when using full-aot, since it doesn't support
3916 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
3917 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
3919 g_assert (klass->inited);
3921 /* Find method index */
3922 method_index = 0xffffff;
3923 if (method->is_inflated && !method->wrapper_type && mono_method_is_generic_sharable_full (method, FALSE, FALSE, FALSE)) {
3925 * For generic methods, we store the fully shared instance in place of the
3928 method = mono_method_get_declaring_generic_method (method);
3929 method_index = mono_metadata_token_index (method->token) - 1;
3930 } else if (method->is_inflated || !method->token) {
3931 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
3932 amodule_lock (amodule);
3933 code = g_hash_table_lookup (amodule->method_to_code, method);
3934 amodule_unlock (amodule);
3938 method_index = find_extra_method (method, &amodule);
3940 * Special case the ICollection<T> wrappers for arrays, as they cannot
3941 * be statically enumerated, and each wrapper ends up calling the same
3944 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
3945 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
3947 code = mono_aot_get_method (domain, m);
3953 * Special case Array.GetGenericValueImpl which is a generic icall.
3954 * Generic sharing currently can't handle it, but the icall returns data using
3955 * an out parameter, so the managed-to-native wrappers can share the same code.
3957 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
3960 MonoGenericContext ctx;
3961 MonoType *args [16];
3963 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
3964 /* Avoid recursion */
3967 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
3970 memset (&ctx, 0, sizeof (ctx));
3971 args [0] = &mono_defaults.object_class->byval_arg;
3972 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
3974 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m, &ctx, &error), TRUE, TRUE);
3975 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
3978 * Get the code for the <object> instantiation which should be emitted into
3979 * the mscorlib aot image by the AOT compiler.
3981 code = mono_aot_get_method (domain, m);
3986 /* Same for CompareExchange<T> and Exchange<T> */
3987 /* Same for Volatile.Read<T>/Write<T> */
3988 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass->image == mono_defaults.corlib &&
3989 ((!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])) ||
3990 (!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))) ||
3991 (!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]))))) {
3994 MonoGenericContext ctx;
3995 MonoType *args [16];
3996 gpointer iter = NULL;
3998 while ((m = mono_class_get_methods (method->klass, &iter))) {
3999 if (mono_method_signature (m)->generic_param_count && !strcmp (m->name, method->name))
4004 memset (&ctx, 0, sizeof (ctx));
4005 args [0] = &mono_defaults.object_class->byval_arg;
4006 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
4008 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m, &ctx, &error), TRUE, TRUE);
4009 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4011 /* Avoid recursion */
4016 * Get the code for the <object> instantiation which should be emitted into
4017 * the mscorlib aot image by the AOT compiler.
4019 code = mono_aot_get_method (domain, m);
4024 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_full (method, FALSE, TRUE, FALSE)) {
4025 /* Partial sharing */
4028 shared = mini_get_shared_method (method);
4029 method_index = find_extra_method (shared, &amodule);
4030 if (method_index != 0xffffff)
4034 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) {
4036 /* Use the all-vt shared method since this is what was AOTed */
4037 method_index = find_extra_method (mini_get_shared_method_full (method, TRUE, TRUE), &amodule);
4038 if (method_index != 0xffffff)
4039 method = mini_get_shared_method_full (method, TRUE, FALSE);
4042 if (method_index == 0xffffff) {
4043 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
4046 full_name = mono_method_full_name (method, TRUE);
4047 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.", full_name);
4053 if (method_index == 0xffffff)
4056 /* Needed by find_jit_info */
4057 amodule_lock (amodule);
4058 if (!amodule->extra_methods)
4059 amodule->extra_methods = g_hash_table_new (NULL, NULL);
4060 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
4061 amodule_unlock (amodule);
4064 method_index = mono_metadata_token_index (method->token) - 1;
4067 return load_method (domain, amodule, klass->image, method, method->token, method_index);
4071 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
4075 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
4077 MonoAotModule *aot_module = image->aot_module;
4083 method_index = mono_metadata_token_index (token) - 1;
4085 return load_method (domain, aot_module, image, NULL, token, method_index);
4091 } IsGotEntryUserData;
4094 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
4096 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
4097 MonoAotModule *aot_module = (MonoAotModule*)value;
4099 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
4104 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
4106 IsGotEntryUserData user_data;
4111 user_data.addr = addr;
4112 user_data.res = FALSE;
4114 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
4117 return user_data.res;
4122 MonoAotModule *module;
4123 } FindAotModuleUserData;
4126 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
4128 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
4129 MonoAotModule *aot_module = (MonoAotModule*)value;
4131 if (amodule_contains_code_addr (aot_module, data->addr))
4132 data->module = aot_module;
4135 static inline MonoAotModule*
4136 find_aot_module (guint8 *code)
4138 FindAotModuleUserData user_data;
4143 /* Reading these need no locking */
4144 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
4147 user_data.addr = code;
4148 user_data.module = NULL;
4151 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
4154 return user_data.module;
4158 mono_aot_patch_plt_entry (guint8 *code, guint8 *plt_entry, gpointer *got, mgreg_t *regs, guint8 *addr)
4160 MonoAotModule *amodule;
4163 * Since AOT code is only used in the root domain,
4164 * mono_domain_get () != mono_get_root_domain () means the calling method
4165 * is AppDomain:InvokeInDomain, so this is the same check as in
4166 * mono_method_same_domain () but without loading the metadata for the method.
4168 if (mono_domain_get () == mono_get_root_domain ()) {
4170 amodule = find_aot_module (code);
4174 mono_arch_patch_plt_entry (plt_entry, got, regs, addr);
4179 * mono_aot_plt_resolve:
4181 * This function is called by the entries in the PLT to resolve the actual method that
4182 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
4183 * Returns NULL if the something cannot be loaded.
4186 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
4188 #ifdef MONO_ARCH_AOT_SUPPORTED
4189 guint8 *p, *target, *plt_entry;
4191 MonoAotModule *module = (MonoAotModule*)aot_module;
4192 gboolean res, no_ftnptr = FALSE;
4194 gboolean using_gsharedvt = FALSE;
4196 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
4198 p = &module->blob [plt_info_offset];
4200 ji.type = decode_value (p, &p);
4202 mp = mono_mempool_new_size (512);
4203 res = decode_patch (module, mp, &ji, p, &p);
4206 mono_mempool_destroy (mp);
4210 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
4211 using_gsharedvt = TRUE;
4215 * Avoid calling resolve_patch_target in the full-aot case if possible, since
4216 * it would create a trampoline, and we don't need that.
4217 * We could do this only if the method does not need the special handling
4218 * in mono_magic_trampoline ().
4220 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) &&
4221 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE) && !using_gsharedvt) {
4222 target = mono_jit_compile_method (ji.data.method);
4225 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
4229 * The trampoline expects us to return a function descriptor on platforms which use
4230 * it, but resolve_patch_target returns a direct function pointer for some type of
4231 * patches, so have to translate between the two.
4232 * FIXME: Clean this up, but how ?
4234 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) {
4235 /* These should already have a function descriptor */
4236 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4237 /* Our function descriptors have a 0 environment, gcc created ones don't */
4238 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
4239 g_assert (((gpointer*)target) [2] == 0);
4242 } else if (!no_ftnptr) {
4243 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4244 g_assert (((gpointer*)target) [2] != 0);
4246 target = mono_create_ftnptr (mono_domain_get (), target);
4249 mono_mempool_destroy (mp);
4251 /* Patch the PLT entry with target which might be the actual method not a trampoline */
4252 plt_entry = mono_aot_get_plt_entry (code);
4253 g_assert (plt_entry);
4254 mono_aot_patch_plt_entry (code, plt_entry, module->got, NULL, target);
4258 g_assert_not_reached ();
4266 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
4267 * method in the module is loaded to avoid committing memory by writing to it.
4268 * LOCKING: Assumes the AMODULE lock is held.
4271 init_plt (MonoAotModule *amodule)
4276 if (amodule->plt_inited)
4279 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
4282 * Initialize the PLT entries in the GOT to point to the default targets.
4285 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
4286 for (i = 1; i < amodule->info.plt_size; ++i)
4287 /* All the default entries point to the AOT trampoline */
4288 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = tramp;
4290 amodule->plt_inited = TRUE;
4294 * mono_aot_get_plt_entry:
4296 * Return the address of the PLT entry called by the code at CODE if exists.
4299 mono_aot_get_plt_entry (guint8 *code)
4301 MonoAotModule *amodule = find_aot_module (code);
4302 guint8 *target = NULL;
4308 if (amodule->thumb_end && code < amodule->thumb_end) {
4309 return mono_arm_get_thumb_plt_entry (code);
4313 #ifdef MONO_ARCH_AOT_SUPPORTED
4314 target = mono_arch_get_call_target (code);
4316 g_assert_not_reached ();
4320 while (target != NULL) {
4321 if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
4324 // Add 4 since mono_arch_get_call_target assumes we're passing
4325 // the instruction after the actual branch instruction.
4326 target = mono_arch_get_call_target (target + 4);
4331 if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
4339 * mono_aot_get_plt_info_offset:
4341 * Return the PLT info offset belonging to the plt entry called by CODE.
4344 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
4346 guint8 *plt_entry = mono_aot_get_plt_entry (code);
4348 g_assert (plt_entry);
4350 /* The offset is embedded inside the code after the plt entry */
4351 #ifdef MONO_ARCH_AOT_SUPPORTED
4352 return mono_arch_get_plt_info_offset (plt_entry, regs, code);
4354 g_assert_not_reached ();
4360 mono_create_ftnptr_malloc (guint8 *code)
4362 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
4363 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
4365 ftnptr->code = code;
4376 * mono_aot_register_jit_icall:
4378 * Register a JIT icall which is called by trampolines in full-aot mode. This should
4379 * be called from mono_arch_init () during startup.
4382 mono_aot_register_jit_icall (const char *name, gpointer addr)
4384 /* No need for locking */
4385 if (!aot_jit_icall_hash)
4386 aot_jit_icall_hash = g_hash_table_new (g_str_hash, g_str_equal);
4387 g_hash_table_insert (aot_jit_icall_hash, (char*)name, addr);
4391 * load_function_full:
4393 * Load the function named NAME from the aot image.
4396 load_function_full (MonoAotModule *amodule, const char *name, MonoTrampInfo **out_tinfo)
4400 int n_patches, pindex;
4403 guint32 info_offset;
4407 symbol = g_strdup_printf ("%s", name);
4408 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
4411 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
4413 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT: FOUND function '%s' in AOT file '%s'.", name, amodule->aot_name);
4417 symbol = g_strdup_printf ("%s_p", name);
4418 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
4421 /* Nothing to patch */
4424 info_offset = *(guint32*)p;
4426 MonoTrampInfo *tinfo;
4427 guint32 code_size, uw_info_len, uw_offset;
4429 /* Construct a MonoTrampInfo from the data in the AOT image */
4431 p += sizeof (guint32);
4432 code_size = *(guint32*)p;
4433 p += sizeof (guint32);
4434 uw_offset = *(guint32*)p;
4435 uw_info = amodule->unwind_info + uw_offset;
4436 uw_info_len = decode_value (uw_info, &uw_info);
4438 tinfo = g_new0 (MonoTrampInfo, 1);
4440 tinfo->code_size = code_size;
4441 tinfo->uw_info = uw_info;
4442 tinfo->uw_info_len = uw_info_len;
4447 p = amodule->blob + info_offset;
4449 /* Similar to mono_aot_load_method () */
4451 n_patches = decode_value (p, &p);
4454 MonoJumpInfo *patches;
4457 mp = mono_mempool_new ();
4459 patches = load_patch_info (amodule, mp, n_patches, FALSE, &got_slots, p, &p);
4462 for (pindex = 0; pindex < n_patches; ++pindex) {
4463 MonoJumpInfo *ji = &patches [pindex];
4466 if (amodule->got [got_slots [pindex]])
4470 * When this code is executed, the runtime may not be initalized yet, so
4471 * resolve the patch info by hand.
4473 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
4474 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
4475 target = mono_get_lmf_addr;
4476 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
4477 target = mono_thread_force_interruption_checkpoint;
4478 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
4479 target = mono_exception_from_token;
4480 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
4481 target = mono_get_throw_exception ();
4482 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
4483 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
4484 target = (gpointer)mono_get_trampoline_func (tramp_type2);
4485 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
4486 /* atoll is needed because the the offset is unsigned */
4490 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
4491 g_assert (res == 1);
4492 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
4493 target = mono_create_ftnptr_malloc (target);
4494 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
4495 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
4496 target = mono_create_ftnptr_malloc (target);
4497 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter_v4")) {
4498 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER_V4, mono_get_root_domain (), NULL);
4499 target = mono_create_ftnptr_malloc (target);
4500 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
4501 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
4502 target = mono_create_ftnptr_malloc (target);
4503 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
4504 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
4505 target = mono_create_ftnptr_malloc (target);
4506 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
4507 target = mono_thread_get_and_clear_pending_exception;
4508 } else if (strstr (ji->data.name, "generic_trampoline_")) {
4509 target = mono_aot_get_trampoline (ji->data.name);
4510 } else if (aot_jit_icall_hash && g_hash_table_lookup (aot_jit_icall_hash, ji->data.name)) {
4511 /* Registered by mono_arch_init () */
4512 target = g_hash_table_lookup (aot_jit_icall_hash, ji->data.name);
4514 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
4515 g_assert_not_reached ();
4519 /* Hopefully the code doesn't have patches which need method or
4522 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
4526 amodule->got [got_slots [pindex]] = target;
4531 mono_mempool_destroy (mp);
4538 load_function (MonoAotModule *amodule, const char *name)
4540 return load_function_full (amodule, name, NULL);
4544 * Return the trampoline identified by NAME from the mscorlib AOT file.
4545 * On ppc64, this returns a function descriptor.
4548 mono_aot_get_trampoline_full (const char *name, MonoTrampInfo **out_tinfo)
4551 MonoAotModule *amodule;
4553 image = mono_defaults.corlib;
4556 amodule = image->aot_module;
4559 return mono_create_ftnptr_malloc (load_function_full (amodule, name, out_tinfo));
4563 mono_aot_get_trampoline (const char *name)
4565 return mono_aot_get_trampoline_full (name, NULL);
4569 #include <mach/mach.h>
4571 static TrampolinePage* trampoline_pages [MONO_AOT_TRAMP_NUM];
4573 static unsigned char*
4574 get_new_trampoline_from_page (int tramp_type)
4576 MonoAotModule *amodule;
4578 TrampolinePage *page;
4581 vm_address_t addr, taddr;
4583 vm_prot_t prot, max_prot;
4584 int psize, specific_trampoline_size;
4585 unsigned char *code;
4587 specific_trampoline_size = 2 * sizeof (gpointer);
4589 mono_aot_page_lock ();
4590 page = trampoline_pages [tramp_type];
4591 if (page && page->trampolines < page->trampolines_end) {
4592 code = page->trampolines;
4593 page->trampolines += specific_trampoline_size;
4594 mono_aot_page_unlock ();
4597 mono_aot_page_unlock ();
4598 psize = mono_pagesize ();
4599 /* the trampoline template page is in the mscorlib module */
4600 image = mono_defaults.corlib;
4603 amodule = image->aot_module;
4606 g_assert (amodule->info.tramp_page_size == psize);
4608 if (tramp_type == MONO_AOT_TRAMP_SPECIFIC)
4609 tpage = load_function (amodule, "specific_trampolines_page");
4610 else if (tramp_type == MONO_AOT_TRAMP_STATIC_RGCTX)
4611 tpage = load_function (amodule, "rgctx_trampolines_page");
4612 else if (tramp_type == MONO_AOT_TRAMP_IMT_THUNK)
4613 tpage = load_function (amodule, "imt_trampolines_page");
4614 else if (tramp_type == MONO_AOT_TRAMP_GSHAREDVT_ARG)
4615 tpage = load_function (amodule, "gsharedvt_arg_trampolines_page");
4617 g_error ("Incorrect tramp type for trampolines page");
4619 /*g_warning ("loaded trampolines page at %x", tpage);*/
4621 /* avoid the unlikely case of looping forever */
4624 while (page == NULL && count-- > 0) {
4626 /* allocate two contiguous pages of memory: the first page will contain the data (like a local constant pool)
4627 * while the second will contain the trampolines.
4629 ret = vm_allocate (mach_task_self (), &addr, psize * 2, VM_FLAGS_ANYWHERE);
4630 if (ret != KERN_SUCCESS) {
4631 g_error ("Cannot allocate memory for trampolines: %d", ret);
4634 /*g_warning ("allocated trampoline double page at %x", addr);*/
4635 /* replace the second page with a remapped trampoline page */
4636 taddr = addr + psize;
4637 vm_deallocate (mach_task_self (), taddr, psize);
4638 ret = vm_remap (mach_task_self (), &taddr, psize, 0, FALSE, mach_task_self(), (vm_address_t)tpage, FALSE, &prot, &max_prot, VM_INHERIT_SHARE);
4639 if (ret != KERN_SUCCESS) {
4640 /* someone else got the page, try again */
4641 vm_deallocate (mach_task_self (), addr, psize);
4644 /*g_warning ("remapped trampoline page at %x", taddr);*/
4646 mono_aot_page_lock ();
4647 page = trampoline_pages [tramp_type];
4648 /* some other thread already allocated, so use that to avoid wasting memory */
4649 if (page && page->trampolines < page->trampolines_end) {
4650 code = page->trampolines;
4651 page->trampolines += specific_trampoline_size;
4652 mono_aot_page_unlock ();
4653 vm_deallocate (mach_task_self (), addr, psize);
4654 vm_deallocate (mach_task_self (), taddr, psize);
4657 page = (TrampolinePage*)addr;
4658 page->next = trampoline_pages [tramp_type];
4659 trampoline_pages [tramp_type] = page;
4660 page->trampolines = (void*)(taddr + amodule->info.tramp_page_code_offsets [tramp_type]);
4661 page->trampolines_end = (void*)(taddr + psize - 64);
4662 code = page->trampolines;
4663 page->trampolines += specific_trampoline_size;
4664 mono_aot_page_unlock ();
4667 g_error ("Cannot allocate more trampoline pages: %d", ret);
4672 static unsigned char*
4673 get_new_trampoline_from_page (int tramp_type)
4675 g_error ("Page trampolines not supported.");
4682 get_new_specific_trampoline_from_page (gpointer tramp, gpointer arg)
4687 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_SPECIFIC);
4689 data = (gpointer*)((char*)code - mono_pagesize ());
4692 /*g_warning ("new trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
4698 get_new_rgctx_trampoline_from_page (gpointer tramp, gpointer arg)
4703 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_STATIC_RGCTX);
4705 data = (gpointer*)((char*)code - mono_pagesize ());
4708 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
4714 get_new_imt_trampoline_from_page (gpointer arg)
4719 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_IMT_THUNK);
4721 data = (gpointer*)((char*)code - mono_pagesize ());
4723 /*g_warning ("new imt trampoline at %p for data %p, (stored at %p)", code, arg, data);*/
4729 get_new_gsharedvt_arg_trampoline_from_page (gpointer tramp, gpointer arg)
4734 code = get_new_trampoline_from_page (MONO_AOT_TRAMP_GSHAREDVT_ARG);
4736 data = (gpointer*)((char*)code - mono_pagesize ());
4739 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
4743 /* Return a given kind of trampoline */
4745 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
4747 MonoAotModule *amodule;
4748 int index, tramp_size;
4751 /* Currently, we keep all trampolines in the mscorlib AOT image */
4752 image = mono_defaults.corlib;
4757 amodule = image->aot_module;
4760 *out_amodule = amodule;
4763 #define MONOTOUCH_TRAMPOLINES_ERROR ". See http://docs.xamarin.com/ios/troubleshooting for instructions on how to fix this condition."
4765 #define MONOTOUCH_TRAMPOLINES_ERROR ""
4767 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type]) {
4768 g_error ("Ran out of trampolines of type %d in '%s' (%d)%s\n",
4769 tramp_type, image->name, amodule->info.num_trampolines [tramp_type], MONOTOUCH_TRAMPOLINES_ERROR);
4771 index = amodule->trampoline_index [tramp_type] ++;
4775 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
4777 tramp_size = amodule->info.trampoline_size [tramp_type];
4780 *out_tramp_size = tramp_size;
4782 return amodule->trampolines [tramp_type] + (index * tramp_size);
4786 * Return a specific trampoline from the AOT file.
4789 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
4791 MonoAotModule *amodule;
4792 guint32 got_offset, tramp_size;
4793 guint8 *code, *tramp;
4794 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
4795 static gboolean inited;
4796 static guint32 num_trampolines;
4802 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
4811 if (!generic_trampolines [tramp_type]) {
4814 symbol = mono_get_generic_trampoline_name (tramp_type);
4815 generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
4819 tramp = generic_trampolines [tramp_type];
4822 if (USE_PAGE_TRAMPOLINES) {
4823 code = get_new_specific_trampoline_from_page (tramp, arg1);
4826 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
4828 amodule->got [got_offset] = tramp;
4829 amodule->got [got_offset + 1] = arg1;
4833 *code_len = tramp_size;
4839 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
4841 MonoAotModule *amodule;
4845 if (USE_PAGE_TRAMPOLINES) {
4846 code = get_new_rgctx_trampoline_from_page (addr, ctx);
4848 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
4850 amodule->got [got_offset] = ctx;
4851 amodule->got [got_offset + 1] = addr;
4854 /* The caller expects an ftnptr */
4855 return mono_create_ftnptr (mono_domain_get (), code);
4859 mono_aot_get_unbox_trampoline (MonoMethod *method)
4861 guint32 method_index = mono_metadata_token_index (method->token) - 1;
4862 MonoAotModule *amodule;
4864 guint32 *ut, *ut_end, *entry;
4865 int low, high, entry_index = 0;
4867 if (method->is_inflated && !mono_method_is_generic_sharable_full (method, FALSE, FALSE, FALSE)) {
4868 method_index = find_extra_method (method, &amodule);
4869 if (method_index == 0xffffff && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) {
4870 MonoMethod *shared = mini_get_shared_method_full (method, TRUE, TRUE);
4871 method_index = find_extra_method (shared, &amodule);
4873 g_assert (method_index != 0xffffff);
4875 amodule = method->klass->image->aot_module;
4879 ut = amodule->unbox_trampolines;
4880 ut_end = amodule->unbox_trampolines_end;
4882 /* Do a binary search in the sorted table */
4885 high = (ut_end - ut);
4886 while (low < high) {
4887 entry_index = (low + high) / 2;
4888 entry = &ut [entry_index];
4889 if (entry [0] < method_index) {
4890 low = entry_index + 1;
4891 } else if (entry [0] > method_index) {
4898 code = get_call_table_entry (amodule->unbox_trampoline_addresses, entry_index);
4901 /* The caller expects an ftnptr */
4902 return mono_create_ftnptr (mono_domain_get (), code);
4906 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
4910 MonoAotModule *amodule = mono_defaults.corlib->aot_module;
4911 guint32 index = MONO_RGCTX_SLOT_INDEX (slot);
4912 static int count = 0;
4915 if (index >= amodule->info.num_rgctx_fetch_trampolines) {
4916 static gpointer addr;
4920 * Use the general version of the rgctx fetch trampoline. It receives a pair of <slot, trampoline> in the rgctx arg reg.
4923 addr = load_function (amodule, "rgctx_fetch_trampoline_general");
4924 info = mono_domain_alloc0 (mono_get_root_domain (), sizeof (gpointer) * 2);
4925 info [0] = GUINT_TO_POINTER (slot);
4926 info [1] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
4927 code = mono_aot_get_static_rgctx_trampoline (info, addr);
4928 return mono_create_ftnptr (mono_domain_get (), code);
4931 symbol = mono_get_rgctx_fetch_trampoline_name (slot);
4932 code = load_function (mono_defaults.corlib->aot_module, symbol);
4934 /* The caller expects an ftnptr */
4935 return mono_create_ftnptr (mono_domain_get (), code);
4939 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
4944 int i, index, real_count;
4945 MonoAotModule *amodule;
4948 for (i = 0; i < count; ++i) {
4949 MonoIMTCheckItem *item = imt_entries [i];
4951 if (item->is_equals)
4955 /* Save the entries into an array */
4956 buf = mono_domain_alloc (domain, (real_count + 1) * 2 * sizeof (gpointer));
4958 for (i = 0; i < count; ++i) {
4959 MonoIMTCheckItem *item = imt_entries [i];
4961 if (!item->is_equals)
4964 g_assert (item->key);
4966 buf [(index * 2)] = item->key;
4967 if (item->has_target_code) {
4968 gpointer *p = mono_domain_alloc (domain, sizeof (gpointer));
4969 *p = item->value.target_code;
4970 buf [(index * 2) + 1] = p;
4972 buf [(index * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
4976 buf [(index * 2)] = NULL;
4977 buf [(index * 2) + 1] = fail_tramp;
4979 if (USE_PAGE_TRAMPOLINES) {
4980 code = get_new_imt_trampoline_from_page (buf);
4982 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
4984 amodule->got [got_offset] = buf;
4991 mono_aot_get_gsharedvt_arg_trampoline (gpointer arg, gpointer addr)
4993 MonoAotModule *amodule;
4997 if (USE_PAGE_TRAMPOLINES) {
4998 code = get_new_gsharedvt_arg_trampoline_from_page (addr, arg);
5000 code = get_numerous_trampoline (MONO_AOT_TRAMP_GSHAREDVT_ARG, 2, &amodule, &got_offset, NULL);
5002 amodule->got [got_offset] = arg;
5003 amodule->got [got_offset + 1] = addr;
5006 /* The caller expects an ftnptr */
5007 return mono_create_ftnptr (mono_domain_get (), code);
5011 * mono_aot_set_make_unreadable:
5013 * Set whenever to make all mmaped memory unreadable. In conjuction with a
5014 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
5017 mono_aot_set_make_unreadable (gboolean unreadable)
5021 make_unreadable = unreadable;
5023 if (make_unreadable && !inited) {
5024 mono_counters_register ("AOT: pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
5029 MonoAotModule *module;
5034 find_map (gpointer key, gpointer value, gpointer user_data)
5036 MonoAotModule *module = (MonoAotModule*)value;
5037 FindMapUserData *data = (FindMapUserData*)user_data;
5040 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
5041 data->module = module;
5044 static MonoAotModule*
5045 find_module_for_addr (void *ptr)
5047 FindMapUserData data;
5049 if (!make_unreadable)
5053 data.ptr = (guint8*)ptr;
5056 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
5063 * mono_aot_is_pagefault:
5065 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
5066 * within memory allocated by this module.
5069 mono_aot_is_pagefault (void *ptr)
5071 if (!make_unreadable)
5075 * Not signal safe, but SIGSEGV's are synchronous, and
5076 * this is only turned on by a MONO_DEBUG option.
5078 return find_module_for_addr (ptr) != NULL;
5082 * mono_aot_handle_pagefault:
5084 * Handle a pagefault caused by an unreadable page by making it readable again.
5087 mono_aot_handle_pagefault (void *ptr)
5089 #ifndef PLATFORM_WIN32
5090 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
5094 res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
5095 g_assert (res == 0);
5106 mono_aot_init (void)
5111 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
5117 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
5123 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
5129 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
5135 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
5141 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
5147 mono_aot_get_plt_entry (guint8 *code)
5153 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
5159 mono_aot_patch_plt_entry (guint8 *code, guint8 *plt_entry, gpointer *got, mgreg_t *regs, guint8 *addr)
5164 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
5170 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
5172 g_assert_not_reached ();
5178 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
5180 g_assert_not_reached ();
5185 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
5187 g_assert_not_reached ();
5192 mono_aot_get_trampoline (const char *name)
5194 g_assert_not_reached ();
5199 mono_aot_get_unbox_trampoline (MonoMethod *method)
5201 g_assert_not_reached ();
5206 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
5208 g_assert_not_reached ();
5213 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
5215 g_assert_not_reached ();
5220 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
5222 g_assert_not_reached ();
5227 mono_aot_register_jit_icall (const char *name, gpointer addr)