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 #ifdef HAVE_DL_ITERATE_PHDR
44 #include <mono/metadata/tabledefs.h>
45 #include <mono/metadata/class.h>
46 #include <mono/metadata/object.h>
47 #include <mono/metadata/tokentype.h>
48 #include <mono/metadata/appdomain.h>
49 #include <mono/metadata/debug-helpers.h>
50 #include <mono/metadata/assembly.h>
51 #include <mono/metadata/metadata-internals.h>
52 #include <mono/metadata/marshal.h>
53 #include <mono/metadata/gc-internal.h>
54 #include <mono/metadata/monitor.h>
55 #include <mono/metadata/threads-types.h>
56 #include <mono/metadata/mono-endian.h>
57 #include <mono/utils/mono-logger-internal.h>
58 #include <mono/utils/mono-mmap.h>
59 #include "mono/utils/mono-compiler.h"
60 #include <mono/utils/mono-counters.h>
68 #define SHARED_EXT ".dll"
69 #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
70 #define SHARED_EXT ".dylib"
71 #elif defined(__APPLE__) && defined(TARGET_X86) && !defined(__native_client_codegen__)
72 #define SHARED_EXT ".dylib"
74 #define SHARED_EXT ".so"
77 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
78 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
80 typedef struct MonoAotModule {
82 /* 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;
104 gint32 *code_offsets;
105 /* This contains <offset, index> pairs sorted by offset */
106 /* This is needed because LLVM emitted methods can be in any order */
107 gint32 *sorted_code_offsets;
108 gint32 sorted_code_offsets_len;
109 guint32 *method_info_offsets;
110 guint32 *got_info_offsets;
111 guint32 *ex_info_offsets;
112 guint32 *class_info_offsets;
113 guint32 *methods_loaded;
114 guint16 *class_name_table;
115 guint32 *extra_method_table;
116 guint32 *extra_method_info_offsets;
117 guint32 *unbox_trampolines;
118 guint32 *unbox_trampolines_end;
122 /* Points to the mono EH data created by LLVM */
123 guint8 *mono_eh_frame;
125 /* Points to the trampolines */
126 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
127 /* The first unused trampoline of each kind */
128 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
130 MonoAotFileInfo info;
136 static GHashTable *aot_modules;
137 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
138 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
139 static CRITICAL_SECTION aot_mutex;
142 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
143 * AOT modules registered by mono_aot_register_module ().
145 static GHashTable *static_aot_modules;
148 * Maps MonoJitInfo* to the aot module they belong to, this can be different
149 * from ji->method->klass->image's aot module for generic instances.
151 static GHashTable *ji_to_amodule;
154 * Whenever to AOT compile loaded assemblies on demand and store them in
155 * a cache under $HOME/.mono/aot-cache.
157 static gboolean use_aot_cache = FALSE;
160 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
161 * use_aot_cache is TRUE.
163 static gboolean spawn_compiler = TRUE;
166 static gint32 mono_last_aot_method = -1;
168 static gboolean make_unreadable = FALSE;
169 static guint32 name_table_accesses = 0;
170 static guint32 n_pagefaults = 0;
172 /* Used to speed-up find_aot_module () */
173 static gsize aot_code_low_addr = (gssize)-1;
174 static gsize aot_code_high_addr = 0;
176 static GHashTable *aot_jit_icall_hash;
179 init_plt (MonoAotModule *info);
181 /*****************************************************/
183 /*****************************************************/
188 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
189 * found, and sets the loader error if SET_ERROR is TRUE.
192 load_image (MonoAotModule *amodule, int index, gboolean set_error)
194 MonoAssembly *assembly;
195 MonoImageOpenStatus status;
197 g_assert (index < amodule->image_table_len);
199 if (amodule->image_table [index])
200 return amodule->image_table [index];
201 if (amodule->out_of_date)
204 assembly = mono_assembly_load (&amodule->image_names [index], amodule->assembly->basedir, &status);
206 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);
207 amodule->out_of_date = TRUE;
210 char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
211 mono_loader_set_error_assembly_load (full_name, FALSE);
217 if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
218 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);
219 amodule->out_of_date = TRUE;
223 amodule->image_table [index] = assembly->image;
224 return assembly->image;
228 decode_value (guint8 *ptr, guint8 **rptr)
233 if ((b & 0x80) == 0){
236 } else if ((b & 0x40) == 0){
237 len = ((b & 0x3f) << 8 | ptr [1]);
239 } else if (b != 0xff) {
240 len = ((b & 0x1f) << 24) |
247 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
253 //printf ("DECODE: %d.\n", len);
258 * mono_aot_get_method:
260 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
264 mono_aot_get_offset (guint32 *table, int index)
266 int i, group, ngroups, index_entry_size;
267 int start_offset, offset, noffsets, group_size;
268 guint8 *data_start, *p;
269 guint32 *index32 = NULL;
270 guint16 *index16 = NULL;
272 noffsets = table [0];
273 group_size = table [1];
275 index_entry_size = table [3];
276 group = index / group_size;
278 if (index_entry_size == 2) {
279 index16 = (guint16*)&table [4];
280 data_start = (guint8*)&index16 [ngroups];
281 p = data_start + index16 [group];
283 index32 = (guint32*)&table [4];
284 data_start = (guint8*)&index32 [ngroups];
285 p = data_start + index32 [group];
288 /* offset will contain the value of offsets [group * group_size] */
289 offset = start_offset = decode_value (p, &p);
290 for (i = group * group_size + 1; i <= index; ++i) {
291 offset += decode_value (p, &p);
294 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
300 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
303 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
305 static MonoGenericInst*
306 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
309 MonoType **type_argv;
310 MonoGenericInst *inst;
313 type_argc = decode_value (p, &p);
314 type_argv = g_new0 (MonoType*, type_argc);
316 for (i = 0; i < type_argc; ++i) {
317 MonoClass *pclass = decode_klass_ref (module, p, &p);
322 type_argv [i] = &pclass->byval_arg;
325 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
334 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
341 argc = decode_value (p, &p);
344 ctx->class_inst = decode_generic_inst (module, p, &p);
345 if (!ctx->class_inst)
349 argc = decode_value (p, &p);
352 ctx->method_inst = decode_generic_inst (module, p, &p);
353 if (!ctx->method_inst)
362 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
365 MonoClass *klass = NULL, *eklass;
366 guint32 token, rank, idx;
370 reftype = decode_value (p, &p);
377 case MONO_AOT_TYPEREF_TYPEDEF_INDEX:
378 idx = decode_value (p, &p);
379 image = load_image (module, 0, TRUE);
382 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + idx);
384 case MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE:
385 idx = decode_value (p, &p);
386 image = load_image (module, decode_value (p, &p), TRUE);
389 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + idx);
391 case MONO_AOT_TYPEREF_TYPESPEC_TOKEN:
392 token = decode_value (p, &p);
393 image = module->assembly->image;
396 klass = mono_class_get (image, token);
398 case MONO_AOT_TYPEREF_GINST: {
400 MonoGenericContext ctx;
403 gclass = decode_klass_ref (module, p, &p);
406 g_assert (gclass->generic_container);
408 memset (&ctx, 0, sizeof (ctx));
409 ctx.class_inst = decode_generic_inst (module, p, &p);
412 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
413 klass = mono_class_from_mono_type (type);
414 mono_metadata_free_type (type);
417 case MONO_AOT_TYPEREF_VAR: {
419 MonoGenericContainer *container;
420 int type = decode_value (p, &p);
421 int num = decode_value (p, &p);
422 gboolean is_method = decode_value (p, &p);
425 MonoMethod *method_def;
426 g_assert (type == MONO_TYPE_MVAR);
427 method_def = decode_resolve_method_ref (module, p, &p);
431 container = mono_method_get_generic_container (method_def);
433 MonoClass *class_def;
434 g_assert (type == MONO_TYPE_VAR);
435 class_def = decode_klass_ref (module, p, &p);
439 container = class_def->generic_container;
442 g_assert (container);
444 // FIXME: Memory management
445 t = g_new0 (MonoType, 1);
447 t->data.generic_param = mono_generic_container_get_param (container, num);
449 // FIXME: Maybe use types directly to avoid
450 // the overhead of creating MonoClass-es
451 klass = mono_class_from_mono_type (t);
456 case MONO_AOT_TYPEREF_ARRAY:
458 rank = decode_value (p, &p);
459 eklass = decode_klass_ref (module, p, &p);
460 klass = mono_array_class_get (eklass, rank);
462 case MONO_AOT_TYPEREF_BLOB_INDEX: {
463 guint32 offset = decode_value (p, &p);
466 p2 = module->blob + offset;
467 klass = decode_klass_ref (module, p2, &p2);
471 g_assert_not_reached ();
474 //printf ("BLA: %s\n", mono_type_full_name (&klass->byval_arg));
479 static MonoClassField*
480 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
482 MonoClass *klass = decode_klass_ref (module, buf, &buf);
489 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
493 return mono_class_get_field (klass, token);
497 * Parse a MonoType encoded by encode_type () in aot-compiler.c. Return malloc-ed
501 decode_type (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
506 t = g_malloc0 (sizeof (MonoType));
509 if (*p == MONO_TYPE_PINNED) {
512 } else if (*p == MONO_TYPE_BYREF) {
525 case MONO_TYPE_BOOLEAN:
539 case MONO_TYPE_STRING:
540 case MONO_TYPE_OBJECT:
541 case MONO_TYPE_TYPEDBYREF:
543 case MONO_TYPE_VALUETYPE:
544 case MONO_TYPE_CLASS:
545 t->data.klass = decode_klass_ref (module, p, &p);
547 case MONO_TYPE_SZARRAY:
548 t->data.klass = decode_klass_ref (module, p, &p);
554 t->data.type = decode_type (module, p, &p);
556 case MONO_TYPE_GENERICINST: {
558 MonoGenericContext ctx;
562 gclass = decode_klass_ref (module, p, &p);
565 g_assert (gclass->generic_container);
567 memset (&ctx, 0, sizeof (ctx));
568 ctx.class_inst = decode_generic_inst (module, p, &p);
571 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
572 klass = mono_class_from_mono_type (type);
573 t->data.generic_class = klass->generic_class;
577 g_assert_not_reached ();
585 // FIXME: Error handling, memory management
587 static MonoMethodSignature*
588 decode_signature_with_target (MonoAotModule *module, MonoMethodSignature *target, guint8 *buf, guint8 **endbuf)
590 MonoMethodSignature *sig;
592 int i, param_count, call_conv;
594 gboolean hasthis, explicit_this, has_gen_params;
598 has_gen_params = (flags & 0x10) != 0;
599 hasthis = (flags & 0x20) != 0;
600 explicit_this = (flags & 0x40) != 0;
601 call_conv = flags & 0x0F;
603 g_assert (!has_gen_params);
605 param_count = decode_value (p, &p);
606 if (param_count != target->param_count)
608 sig = g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE + param_count * sizeof (MonoType *));
609 sig->param_count = param_count;
610 sig->sentinelpos = -1;
611 sig->hasthis = hasthis;
612 sig->explicit_this = explicit_this;
613 sig->call_convention = call_conv;
614 sig->param_count = param_count;
615 sig->ret = decode_type (module, p, &p);
616 for (i = 0; i < param_count; ++i)
617 sig->params [i] = decode_type (module, p, &p);
625 sig_matches_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
627 MonoMethodSignature *sig;
631 sig = decode_signature_with_target (module, mono_method_signature (target), p, &p);
632 res = sig && mono_metadata_signature_equal (mono_method_signature (target), sig);
639 wrapper_name_matches_target (guint8 *p, MonoMethod *target)
641 char *name = (char*)p;
644 tname = mono_aot_wrapper_name (target);
645 if (!strcmp (name, tname))
651 /* Stores information returned by decode_method_ref () */
656 gboolean no_aot_trampoline;
660 * decode_method_ref_with_target:
662 * Decode a method reference, storing the image/token into a MethodRef structure.
663 * This avoids loading metadata for the method if the caller does not need it. If the method has
664 * no token, then it is loaded from metadata and ref->method is set to the method instance.
665 * If TARGET is non-NULL, abort decoding if it can be determined that the decoded method couldn't resolve to TARGET, and return FALSE. There are some kinds of method references
666 * which only support a non-null TARGET.
669 decode_method_ref_with_target (MonoAotModule *module, MethodRef *ref, MonoMethod *target, guint8 *buf, guint8 **endbuf)
671 guint32 image_index, value;
672 MonoImage *image = NULL;
675 memset (ref, 0, sizeof (MethodRef));
677 value = decode_value (p, &p);
678 image_index = value >> 24;
680 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
681 ref->no_aot_trampoline = TRUE;
682 value = decode_value (p, &p);
683 image_index = value >> 24;
686 if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
687 if (target && target->wrapper_type)
691 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
692 guint32 wrapper_type;
694 wrapper_type = decode_value (p, &p);
696 if (target && target->wrapper_type != wrapper_type)
700 image = mono_defaults.corlib;
702 switch (wrapper_type) {
703 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
704 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
708 mono_class_init (m->klass);
709 ref->method = mono_marshal_get_remoting_invoke_with_check (m);
712 case MONO_WRAPPER_PROXY_ISINST: {
713 MonoClass *klass = decode_klass_ref (module, p, &p);
716 ref->method = mono_marshal_get_proxy_cancast (klass);
719 case MONO_WRAPPER_LDFLD:
720 case MONO_WRAPPER_LDFLDA:
721 case MONO_WRAPPER_STFLD:
722 case MONO_WRAPPER_ISINST: {
723 MonoClass *klass = decode_klass_ref (module, p, &p);
726 if (wrapper_type == MONO_WRAPPER_LDFLD)
727 ref->method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
728 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
729 ref->method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
730 else if (wrapper_type == MONO_WRAPPER_STFLD)
731 ref->method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
732 else if (wrapper_type == MONO_WRAPPER_ISINST)
733 ref->method = mono_marshal_get_isinst (klass);
735 g_assert_not_reached ();
738 case MONO_WRAPPER_LDFLD_REMOTE:
739 ref->method = mono_marshal_get_ldfld_remote_wrapper (NULL);
741 case MONO_WRAPPER_STFLD_REMOTE:
742 ref->method = mono_marshal_get_stfld_remote_wrapper (NULL);
744 case MONO_WRAPPER_ALLOC: {
745 int atype = decode_value (p, &p);
747 ref->method = mono_gc_get_managed_allocator_by_type (atype);
748 g_assert (ref->method);
751 case MONO_WRAPPER_WRITE_BARRIER:
752 ref->method = mono_gc_get_write_barrier ();
754 case MONO_WRAPPER_STELEMREF: {
755 int subtype = decode_value (p, &p);
758 ref->method = mono_marshal_get_stelemref ();
760 g_assert (subtype == MONO_AOT_WRAPPER_BY_NAME);
762 if (wrapper_name_matches_target (p, target))
763 ref->method = target;
769 case MONO_WRAPPER_SYNCHRONIZED: {
770 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
774 ref->method = mono_marshal_get_synchronized_wrapper (m);
777 case MONO_WRAPPER_UNKNOWN: {
778 MonoMethodDesc *desc;
779 MonoMethod *orig_method;
780 int subtype = decode_value (p, &p);
782 if (subtype == MONO_AOT_WRAPPER_PTR_TO_STRUCTURE || subtype == MONO_AOT_WRAPPER_STRUCTURE_TO_PTR) {
783 MonoClass *klass = decode_klass_ref (module, p, &p);
789 if (klass != target->klass)
792 if (subtype == MONO_AOT_WRAPPER_PTR_TO_STRUCTURE) {
793 if (strcmp (target->name, "PtrToStructure"))
795 ref->method = mono_marshal_get_ptr_to_struct (klass);
797 if (strcmp (target->name, "StructureToPtr"))
799 ref->method = mono_marshal_get_struct_to_ptr (klass);
802 if (subtype == MONO_AOT_WRAPPER_MONITOR_ENTER)
803 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
804 else if (subtype == MONO_AOT_WRAPPER_MONITOR_EXIT)
805 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
806 else if (subtype == MONO_AOT_WRAPPER_MONITOR_ENTER_V4)
807 desc = mono_method_desc_new ("Monitor:Enter(object,bool&)", FALSE);
809 g_assert_not_reached ();
810 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
811 g_assert (orig_method);
812 mono_method_desc_free (desc);
813 ref->method = mono_monitor_get_fast_path (orig_method);
817 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
818 int subtype = decode_value (p, &p);
820 if (subtype == MONO_AOT_WRAPPER_ELEMENT_ADDR) {
821 int rank = decode_value (p, &p);
822 int elem_size = decode_value (p, &p);
824 ref->method = mono_marshal_get_array_address (rank, elem_size);
826 g_assert (subtype == MONO_AOT_WRAPPER_BY_NAME);
828 if (wrapper_name_matches_target (p, target))
829 ref->method = target;
835 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
837 int subtype = decode_value (p, &p);
840 if (subtype == MONO_AOT_WRAPPER_JIT_ICALL) {
844 if (strcmp (target->name, name) != 0)
846 ref->method = target;
848 m = decode_resolve_method_ref (module, p, &p);
853 /* This should only happen when looking for an extra method */
855 if (mono_marshal_method_from_wrapper (target) == m)
856 ref->method = target;
862 case MONO_WRAPPER_CASTCLASS: {
863 int subtype = decode_value (p, &p);
865 if (subtype == MONO_AOT_WRAPPER_CASTCLASS_WITH_CACHE)
866 ref->method = mono_marshal_get_castclass_with_cache ();
867 else if (subtype == MONO_AOT_WRAPPER_ISINST_WITH_CACHE)
868 ref->method = mono_marshal_get_isinst_with_cache ();
870 g_assert_not_reached ();
873 case MONO_WRAPPER_RUNTIME_INVOKE: {
874 int subtype = decode_value (p, &p);
878 if (subtype == MONO_AOT_WRAPPER_RUNTIME_INVOKE_DYNAMIC) {
879 if (strcmp (target->name, "runtime_invoke_dynamic") != 0)
881 ref->method = target;
882 } else if (subtype == MONO_AOT_WRAPPER_RUNTIME_INVOKE_DIRECT) {
884 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
888 ref->method = mono_marshal_get_runtime_invoke (m, FALSE);
889 } else if (subtype == MONO_AOT_WRAPPER_RUNTIME_INVOKE_VIRTUAL) {
890 /* Virtual direct wrapper */
891 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
895 ref->method = mono_marshal_get_runtime_invoke (m, TRUE);
897 if (sig_matches_target (module, target, p, &p))
898 ref->method = target;
904 case MONO_WRAPPER_DELEGATE_INVOKE:
905 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
906 case MONO_WRAPPER_DELEGATE_END_INVOKE: {
908 * These wrappers are associated with a signature, not with a method.
909 * Since we can't decode them into methods, they need a target method.
913 if (sig_matches_target (module, target, p, &p))
914 ref->method = target;
919 case MONO_WRAPPER_NATIVE_TO_MANAGED: {
923 m = decode_resolve_method_ref (module, p, &p);
926 klass = decode_klass_ref (module, p, &p);
929 ref->method = mono_marshal_get_managed_wrapper (m, klass, 0);
933 g_assert_not_reached ();
935 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
936 image_index = decode_value (p, &p);
937 ref->token = decode_value (p, &p);
939 image = load_image (module, image_index, TRUE);
942 } else if (image_index == MONO_AOT_METHODREF_GINST) {
944 MonoGenericContext ctx;
947 * These methods do not have a token which resolves them, so we
948 * resolve them immediately.
950 klass = decode_klass_ref (module, p, &p);
954 if (target && target->klass != klass)
957 image_index = decode_value (p, &p);
958 ref->token = decode_value (p, &p);
960 image = load_image (module, image_index, TRUE);
964 ref->method = mono_get_method_full (image, ref->token, NULL, NULL);
968 memset (&ctx, 0, sizeof (ctx));
970 if (FALSE && klass->generic_class) {
971 ctx.class_inst = klass->generic_class->context.class_inst;
972 ctx.method_inst = NULL;
974 ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
977 memset (&ctx, 0, sizeof (ctx));
979 if (!decode_generic_context (module, &ctx, p, &p))
982 ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
983 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
987 klass = decode_klass_ref (module, p, &p);
990 method_type = decode_value (p, &p);
991 switch (method_type) {
993 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
996 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
999 ref->method = mono_class_get_method_from_name (klass, "Get", -1);
1002 ref->method = mono_class_get_method_from_name (klass, "Address", -1);
1005 ref->method = mono_class_get_method_from_name (klass, "Set", -1);
1008 g_assert_not_reached ();
1011 g_assert (image_index < MONO_AOT_METHODREF_MIN);
1012 ref->token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
1014 image = load_image (module, image_index, TRUE);
1027 decode_method_ref (MonoAotModule *module, MethodRef *ref, guint8 *buf, guint8 **endbuf)
1029 return decode_method_ref_with_target (module, ref, NULL, buf, endbuf);
1033 * decode_resolve_method_ref_with_target:
1035 * Similar to decode_method_ref, but resolve and return the method itself.
1038 decode_resolve_method_ref_with_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
1043 res = decode_method_ref_with_target (module, &ref, target, buf, endbuf);
1050 return mono_get_method (ref.image, ref.token, NULL);
1054 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
1056 return decode_resolve_method_ref_with_target (module, NULL, buf, endbuf);
1060 create_cache_structure (void)
1066 home = g_get_home_dir ();
1070 tmp = g_build_filename (home, ".mono", NULL);
1071 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
1072 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
1076 err = mkdir (tmp, 0777);
1079 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
1085 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
1086 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
1087 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
1091 err = mkdir (tmp, 0777);
1094 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
1103 * load_aot_module_from_cache:
1105 * Experimental code to AOT compile loaded assemblies on demand.
1108 * - Add environment variable MONO_AOT_CACHE_OPTIONS
1109 * - Add options for controlling the cache size
1110 * - Handle full cache by deleting old assemblies lru style
1111 * - Add options for excluding assemblies during development
1112 * - Maybe add a threshold after an assembly is AOT compiled
1113 * - invoking a new mono process is a security risk
1114 * - recompile the AOT module if one of its dependencies changes
1117 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
1119 char *fname, *cmd, *tmp2, *aot_options;
1128 if (assembly->image->dynamic)
1131 create_cache_structure ();
1133 home = g_get_home_dir ();
1135 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
1136 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
1140 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
1141 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
1144 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
1146 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
1148 aot_options = g_strdup_printf ("outfile=%s", fname);
1150 if (spawn_compiler) {
1151 /* FIXME: security */
1152 /* FIXME: Has to pass the assembly loading path to the child process */
1153 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
1155 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
1157 #if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
1159 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
1160 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
1162 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
1169 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
1171 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
1173 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
1177 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
1179 g_free (aot_options);
1186 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
1190 guint16 *table, *entry;
1194 /* The first entry points to the hash */
1195 table = globals [0];
1198 table_size = table [0];
1201 hash = mono_metadata_str_hash (name) % table_size;
1203 entry = &table [hash * 2];
1205 /* Search the hash for the index into the globals table */
1207 while (entry [0] != 0) {
1208 guint32 index = entry [0] - 1;
1209 guint32 next = entry [1];
1211 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
1213 if (!strcmp (globals [index * 2], name)) {
1214 global_index = index;
1219 entry = &table [next * 2];
1225 if (global_index != -1)
1226 *value = globals [global_index * 2 + 1];
1230 char *err = mono_dl_symbol (module, name, value);
1238 check_usable (MonoAssembly *assembly, MonoAotFileInfo *info, char **out_msg)
1242 gboolean usable = TRUE;
1246 if (strcmp (assembly->image->guid, info->assembly_guid)) {
1247 msg = g_strdup_printf ("doesn't match assembly");
1251 build_info = mono_get_runtime_build_info ();
1252 if (strlen (info->runtime_version) > 0 && strcmp (info->runtime_version, build_info)) {
1253 msg = g_strdup_printf ("compiled against runtime version '%s' while this runtime has version '%s'", info->runtime_version, build_info);
1256 g_free (build_info);
1258 full_aot = info->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1260 if (mono_aot_only && !full_aot) {
1261 msg = g_strdup_printf ("not compiled with --aot=full");
1264 if (!mono_aot_only && full_aot) {
1265 msg = g_strdup_printf ("compiled with --aot=full");
1269 /* mono_arch_find_imt_method () requires this */
1270 if ((info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1271 msg = g_strdup_printf ("compiled against LLVM");
1274 if (!(info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && mono_use_llvm) {
1275 msg = g_strdup_printf ("not compiled against LLVM");
1279 if (mini_get_debug_options ()->mdb_optimizations && !(info->flags & MONO_AOT_FILE_FLAG_DEBUG) && !full_aot) {
1280 msg = g_strdup_printf ("not compiled for debugging");
1286 if (info->gc_name_index != -1) {
1287 char *gc_name = (char*)&blob [info->gc_name_index];
1288 const char *current_gc_name = mono_gc_get_gc_name ();
1290 if (strcmp (current_gc_name, gc_name) != 0) {
1291 msg = g_strdup_printf ("compiled against GC %s, while the current runtime uses GC %s.\n", gc_name, current_gc_name);
1301 load_aot_module (MonoAssembly *assembly, gpointer user_data)
1304 MonoAotModule *amodule;
1306 gboolean usable = TRUE;
1307 char *version_symbol = NULL;
1309 gpointer *globals = NULL;
1310 MonoAotFileInfo *info = NULL;
1313 gboolean do_load_image = TRUE;
1315 if (mono_compile_aot)
1318 if (assembly->image->aot_module)
1320 * Already loaded. This can happen because the assembly loading code might invoke
1321 * the assembly load hooks multiple times for the same assembly.
1325 if (assembly->image->dynamic)
1328 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
1332 if (static_aot_modules)
1333 info = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
1339 /* Statically linked AOT module */
1341 aot_name = g_strdup_printf ("%s", assembly->aname.name);
1342 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
1343 globals = info->globals;
1346 sofile = load_aot_module_from_cache (assembly, &aot_name);
1349 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
1351 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1354 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
1360 if (!sofile && !globals) {
1361 if (mono_aot_only) {
1362 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1370 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &version_symbol);
1371 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&info);
1374 if (version_symbol) {
1375 /* Old file format */
1376 version = atoi (version_symbol);
1379 version = info->version;
1382 if (version != MONO_AOT_FILE_VERSION) {
1383 msg = g_strdup_printf ("wrong file format version (expected %d got %d)", MONO_AOT_FILE_VERSION, version);
1386 usable = check_usable (assembly, info, &msg);
1390 if (mono_aot_only) {
1391 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode: %s.\n", aot_name, msg);
1394 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable: %s.\n", aot_name, msg);
1399 mono_dl_close (sofile);
1400 assembly->image->aot_module = NULL;
1405 g_assert (info->double_align == __alignof__ (double));
1406 g_assert (info->long_align == __alignof__ (gint64));
1410 amodule = g_new0 (MonoAotModule, 1);
1411 amodule->aot_name = aot_name;
1412 amodule->assembly = assembly;
1414 memcpy (&amodule->info, info, sizeof (*info));
1416 amodule->got = amodule->info.got;
1417 amodule->got [0] = assembly->image;
1418 amodule->globals = globals;
1419 amodule->sofile = sofile;
1420 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1421 amodule->blob = blob;
1423 /* Read image table */
1425 guint32 table_len, i;
1428 table = info->image_table;
1431 table_len = *(guint32*)table;
1432 table += sizeof (guint32);
1433 amodule->image_table = g_new0 (MonoImage*, table_len);
1434 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1435 amodule->image_guids = g_new0 (char*, table_len);
1436 amodule->image_table_len = table_len;
1437 for (i = 0; i < table_len; ++i) {
1438 MonoAssemblyName *aname = &(amodule->image_names [i]);
1440 aname->name = g_strdup (table);
1441 table += strlen (table) + 1;
1442 amodule->image_guids [i] = g_strdup (table);
1443 table += strlen (table) + 1;
1445 aname->culture = g_strdup (table);
1446 table += strlen (table) + 1;
1447 memcpy (aname->public_key_token, table, strlen (table) + 1);
1448 table += strlen (table) + 1;
1450 table = ALIGN_PTR_TO (table, 8);
1451 aname->flags = *(guint32*)table;
1453 aname->major = *(guint32*)table;
1455 aname->minor = *(guint32*)table;
1457 aname->build = *(guint32*)table;
1459 aname->revision = *(guint32*)table;
1464 amodule->code_offsets = info->code_offsets;
1465 amodule->code = info->methods;
1467 /* Mask out thumb interop bit */
1468 amodule->code = (void*)((mgreg_t)amodule->code & ~1);
1470 amodule->code_end = info->methods_end;
1471 amodule->method_info_offsets = info->method_info_offsets;
1472 amodule->ex_info_offsets = info->ex_info_offsets;
1473 amodule->class_info_offsets = info->class_info_offsets;
1474 amodule->class_name_table = info->class_name_table;
1475 amodule->extra_method_table = info->extra_method_table;
1476 amodule->extra_method_info_offsets = info->extra_method_info_offsets;
1477 amodule->unbox_trampolines = info->unbox_trampolines;
1478 amodule->unbox_trampolines_end = info->unbox_trampolines_end;
1479 amodule->got_info_offsets = info->got_info_offsets;
1480 amodule->unwind_info = info->unwind_info;
1481 amodule->mem_end = info->mem_end;
1482 amodule->mem_begin = amodule->code;
1483 amodule->plt = info->plt;
1484 amodule->plt_end = info->plt_end;
1485 amodule->mono_eh_frame = info->mono_eh_frame;
1486 amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC] = info->specific_trampolines;
1487 amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = info->static_rgctx_trampolines;
1488 amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK] = info->imt_thunks;
1489 amodule->thumb_end = info->thumb_end;
1491 if (make_unreadable) {
1492 #ifndef TARGET_WIN32
1494 guint8 *page_start, *page_end;
1497 addr = amodule->mem_begin;
1498 len = amodule->mem_end - amodule->mem_begin;
1500 /* Round down in both directions to avoid modifying data which is not ours */
1501 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1502 page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
1503 if (page_end > page_start) {
1504 err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
1505 g_assert (err == 0);
1512 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1513 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1515 g_hash_table_insert (aot_modules, assembly, amodule);
1518 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1520 assembly->image->aot_module = amodule;
1522 if (mono_aot_only) {
1523 if (mono_defaults.corlib) {
1524 /* The second got slot contains the mscorlib got addr */
1525 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1527 amodule->got [1] = mscorlib_amodule->got;
1529 amodule->got [1] = amodule->got;
1533 if (mono_gc_is_moving ()) {
1536 memset (&ji, 0, sizeof (ji));
1537 ji.type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
1539 amodule->got [2] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
1543 * Since we store methoddef and classdef tokens when referring to methods/classes in
1544 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1545 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1546 * non-lazily, since we can't handle out-of-date errors later.
1547 * The cached class info also depends on the exact assemblies.
1549 #if defined(__native_client__)
1550 /* TODO: Don't 'load_image' on mscorlib due to a */
1551 /* recursive loading problem. This should be */
1552 /* removed if mscorlib is loaded from disk. */
1553 if (strncmp(assembly->aname.name, "mscorlib", 8)) {
1554 do_load_image = TRUE;
1556 do_load_image = FALSE;
1559 if (do_load_image) {
1560 for (i = 0; i < amodule->image_table_len; ++i)
1561 load_image (amodule, i, FALSE);
1564 if (amodule->out_of_date) {
1565 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);
1566 if (mono_aot_only) {
1567 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);
1572 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1576 * mono_aot_register_globals:
1578 * This is called by the ctor function in AOT images compiled with the
1579 * 'no-dlsym' option.
1582 mono_aot_register_globals (gpointer *globals)
1584 g_assert_not_reached ();
1588 * mono_aot_register_module:
1590 * This should be called by embedding code to register AOT modules statically linked
1591 * into the executable. AOT_INFO should be the value of the
1592 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1595 mono_aot_register_module (gpointer *aot_info)
1599 MonoAotFileInfo *info = (gpointer)aot_info;
1601 g_assert (info->version == MONO_AOT_FILE_VERSION);
1603 globals = info->globals;
1606 aname = info->assembly_name;
1608 /* This could be called before startup */
1612 if (!static_aot_modules)
1613 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1615 g_hash_table_insert (static_aot_modules, aname, info);
1622 mono_aot_init (void)
1624 InitializeCriticalSection (&aot_mutex);
1625 aot_modules = g_hash_table_new (NULL, NULL);
1627 mono_install_assembly_load_hook (load_aot_module, NULL);
1629 if (g_getenv ("MONO_LASTAOT"))
1630 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1631 if (g_getenv ("MONO_AOT_CACHE"))
1632 use_aot_cache = TRUE;
1636 mono_aot_cleanup (void)
1638 if (aot_jit_icall_hash)
1639 g_hash_table_destroy (aot_jit_icall_hash);
1641 g_hash_table_destroy (aot_modules);
1645 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1651 info->vtable_size = decode_value (buf, &buf);
1652 if (info->vtable_size == -1)
1655 flags = decode_value (buf, &buf);
1656 info->ghcimpl = (flags >> 0) & 0x1;
1657 info->has_finalize = (flags >> 1) & 0x1;
1658 info->has_cctor = (flags >> 2) & 0x1;
1659 info->has_nested_classes = (flags >> 3) & 0x1;
1660 info->blittable = (flags >> 4) & 0x1;
1661 info->has_references = (flags >> 5) & 0x1;
1662 info->has_static_refs = (flags >> 6) & 0x1;
1663 info->no_special_static_fields = (flags >> 7) & 0x1;
1664 info->is_generic_container = (flags >> 8) & 0x1;
1666 if (info->has_cctor) {
1667 res = decode_method_ref (module, &ref, buf, &buf);
1670 info->cctor_token = ref.token;
1672 if (info->has_finalize) {
1673 res = decode_method_ref (module, &ref, buf, &buf);
1676 info->finalize_image = ref.image;
1677 info->finalize_token = ref.token;
1680 info->instance_size = decode_value (buf, &buf);
1681 info->class_size = decode_value (buf, &buf);
1682 info->packing_size = decode_value (buf, &buf);
1683 info->min_align = decode_value (buf, &buf);
1691 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1694 MonoClass *klass = vtable->klass;
1695 MonoAotModule *amodule = klass->image->aot_module;
1697 MonoCachedClassInfo class_info;
1702 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !amodule)
1705 info = &amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1708 err = decode_cached_class_info (amodule, &class_info, p, &p);
1712 for (i = 0; i < slot; ++i)
1713 decode_method_ref (amodule, &ref, p, &p);
1715 res = decode_method_ref (amodule, &ref, p, &p);
1718 if (ref.no_aot_trampoline)
1721 if (mono_metadata_token_index (ref.token) == 0)
1724 return mono_aot_get_method_from_token (domain, ref.image, ref.token);
1728 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1730 MonoAotModule *amodule = klass->image->aot_module;
1734 if (klass->rank || !amodule)
1737 p = (guint8*)&amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1739 err = decode_cached_class_info (amodule, res, p, &p);
1747 * mono_aot_get_class_from_name:
1749 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1750 * using a cache stored in the AOT file.
1751 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1753 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1757 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1759 MonoAotModule *amodule = image->aot_module;
1760 guint16 *table, *entry;
1763 char full_name_buf [1024];
1765 const char *name2, *name_space2;
1767 guint32 cols [MONO_TYPEDEF_SIZE];
1768 GHashTable *nspace_table;
1770 if (!amodule || !amodule->class_name_table)
1777 /* First look in the cache */
1778 if (!amodule->name_cache)
1779 amodule->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1780 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
1782 *klass = g_hash_table_lookup (nspace_table, name);
1789 table_size = amodule->class_name_table [0];
1790 table = amodule->class_name_table + 1;
1792 if (name_space [0] == '\0')
1793 full_name = g_strdup_printf ("%s", name);
1795 if (strlen (name_space) + strlen (name) < 1000) {
1796 sprintf (full_name_buf, "%s.%s", name_space, name);
1797 full_name = full_name_buf;
1799 full_name = g_strdup_printf ("%s.%s", name_space, name);
1802 hash = mono_metadata_str_hash (full_name) % table_size;
1803 if (full_name != full_name_buf)
1806 entry = &table [hash * 2];
1808 if (entry [0] != 0) {
1809 t = &image->tables [MONO_TABLE_TYPEDEF];
1812 guint32 index = entry [0];
1813 guint32 next = entry [1];
1814 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1816 name_table_accesses ++;
1818 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1820 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1821 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1823 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1825 *klass = mono_class_get (image, token);
1830 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
1831 if (!nspace_table) {
1832 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1833 g_hash_table_insert (amodule->name_cache, (char*)name_space2, nspace_table);
1835 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1842 entry = &table [next * 2];
1855 * decode_mono_eh_frame:
1857 * Decode the EH information emitted by our modified LLVM compiler and construct a
1858 * MonoJitInfo structure from it.
1859 * LOCKING: Acquires the domain lock.
1862 decode_llvm_mono_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
1863 MonoMethod *method, guint8 *code,
1864 MonoJitExceptionInfo *clauses, int num_clauses,
1865 int extra_size, GSList **nesting,
1866 int *this_reg, int *this_offset)
1869 guint8 *fde, *cie, *code_start, *code_end;
1870 int version, fde_count;
1872 int i, j, pos, left, right, offset, offset1, offset2, code_len;
1873 MonoJitExceptionInfo *ei;
1874 guint32 fde_len, ei_len, nested_len, nindex;
1875 gpointer *type_info;
1877 MonoLLVMFDEInfo info;
1879 g_assert (amodule->mono_eh_frame);
1881 p = amodule->mono_eh_frame;
1883 /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
1887 g_assert (version == 1);
1889 p = ALIGN_PTR_TO (p, 4);
1891 fde_count = *(guint32*)p;
1895 /* There is +1 entry in the table */
1896 cie = p + ((fde_count + 1) * 8);
1898 /* Binary search in the table to find the entry for code */
1899 offset = code - amodule->mono_eh_frame;
1904 pos = (left + right) / 2;
1906 offset1 = table [(pos * 2)];
1907 if (pos + 1 == fde_count)
1909 offset2 = amodule->code_end - amodule->code;
1911 offset2 = table [(pos + 1) * 2];
1913 if (offset < offset1)
1915 else if (offset >= offset2)
1921 code_start = amodule->mono_eh_frame + table [(pos * 2)];
1922 /* This won't overflow because there is +1 entry in the table */
1923 code_end = amodule->mono_eh_frame + table [(pos * 2) + 2];
1924 code_len = code_end - code_start;
1926 g_assert (code >= code_start && code < code_end);
1928 fde = amodule->mono_eh_frame + table [(pos * 2) + 1];
1929 /* This won't overflow because there is +1 entry in the table */
1930 fde_len = table [(pos * 2) + 2 + 1] - table [(pos * 2) + 1];
1932 mono_unwind_decode_llvm_mono_fde (fde, fde_len, cie, code_start, &info);
1934 ei_len = info.ex_info_len;
1935 type_info = info.type_info;
1936 *this_reg = info.this_reg;
1937 *this_offset = info.this_offset;
1939 /* Count number of nested clauses */
1941 for (i = 0; i < ei_len; ++i) {
1942 /* This might be unaligned */
1943 gint32 cindex1 = read32 (type_info [i]);
1946 for (l = nesting [cindex1]; l; l = l->next) {
1947 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
1949 for (j = 0; j < ei_len; ++j) {
1950 gint32 cindex2 = read32 (type_info [j]);
1952 if (cindex2 == nesting_cindex)
1959 * LLVM might represent one IL region with multiple regions, so have to
1960 * allocate a new JI.
1963 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * (ei_len + nested_len)) + extra_size);
1965 jinfo->code_size = code_len;
1966 jinfo->used_regs = mono_cache_unwind_info (info.unw_info, info.unw_info_len);
1967 jinfo->method = method;
1968 jinfo->code_start = code;
1969 jinfo->domain_neutral = 0;
1970 /* This signals that used_regs points to a normal cached unwind info */
1971 jinfo->from_aot = 0;
1972 jinfo->num_clauses = ei_len + nested_len;
1974 for (i = 0; i < ei_len; ++i) {
1976 * orig_jinfo contains the original IL exception info saved by the AOT
1977 * compiler, we have to combine that with the information produced by LLVM
1979 /* The type_info entries contain IL clause indexes */
1980 int clause_index = read32 (type_info [i]);
1981 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
1982 MonoJitExceptionInfo *orig_jei = &clauses [clause_index];
1984 g_assert (clause_index < num_clauses);
1985 jei->flags = orig_jei->flags;
1986 jei->data.catch_class = orig_jei->data.catch_class;
1988 jei->try_start = ei [i].try_start;
1989 jei->try_end = ei [i].try_end;
1990 jei->handler_start = ei [i].handler_start;
1992 /* Make sure we transition to thumb when a handler starts */
1993 if (amodule->thumb_end && (guint8*)jei->handler_start < amodule->thumb_end)
1994 jei->handler_start = (void*)((mgreg_t)jei->handler_start + 1);
1997 /* See exception_cb () in mini-llvm.c as to why this is needed */
1999 for (i = 0; i < ei_len; ++i) {
2000 gint32 cindex1 = read32 (type_info [i]);
2003 for (l = nesting [cindex1]; l; l = l->next) {
2004 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
2006 for (j = 0; j < ei_len; ++j) {
2007 gint32 cindex2 = read32 (type_info [j]);
2009 if (cindex2 == nesting_cindex) {
2011 * The try interval comes from the nested clause, everything else from the
2014 memcpy (&jinfo->clauses [nindex], &jinfo->clauses [j], sizeof (MonoJitExceptionInfo));
2015 jinfo->clauses [nindex].try_start = jinfo->clauses [i].try_start;
2016 jinfo->clauses [nindex].try_end = jinfo->clauses [i].try_end;
2022 g_assert (nindex == ei_len + nested_len);
2028 * LOCKING: Acquires the domain lock.
2031 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
2032 MonoMethod *method, guint8* ex_info, guint8 *addr,
2033 guint8 *code, guint32 code_len)
2035 int i, buf_len, num_clauses;
2037 guint used_int_regs, flags;
2038 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points, has_try_block_holes;
2039 gboolean from_llvm, has_gc_map;
2041 int generic_info_size, try_holes_info_size, num_holes, this_reg = 0, this_offset = 0;
2043 /* Load the method info from the AOT file */
2046 flags = decode_value (p, &p);
2047 has_generic_jit_info = (flags & 1) != 0;
2048 has_dwarf_unwind_info = (flags & 2) != 0;
2049 has_clauses = (flags & 4) != 0;
2050 has_seq_points = (flags & 8) != 0;
2051 from_llvm = (flags & 16) != 0;
2052 has_try_block_holes = (flags & 32) != 0;
2053 has_gc_map = (flags & 64) != 0;
2055 if (has_dwarf_unwind_info) {
2058 offset = decode_value (p, &p);
2059 g_assert (offset < (1 << 30));
2060 used_int_regs = offset;
2062 used_int_regs = decode_value (p, &p);
2064 if (has_generic_jit_info)
2065 generic_info_size = sizeof (MonoGenericJitInfo);
2067 generic_info_size = 0;
2069 if (has_try_block_holes) {
2070 num_holes = decode_value (p, &p);
2071 try_holes_info_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
2073 num_holes = try_holes_info_size = 0;
2075 /* Exception table */
2077 num_clauses = decode_value (p, &p);
2082 MonoJitExceptionInfo *clauses;
2086 * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
2089 clauses = g_new0 (MonoJitExceptionInfo, num_clauses);
2090 nesting = g_new0 (GSList*, num_clauses);
2092 for (i = 0; i < num_clauses; ++i) {
2093 MonoJitExceptionInfo *ei = &clauses [i];
2095 ei->flags = decode_value (p, &p);
2097 if (decode_value (p, &p))
2098 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
2100 /* Read the list of nesting clauses */
2102 int nesting_index = decode_value (p, &p);
2103 if (nesting_index == -1)
2105 nesting [i] = g_slist_prepend (nesting [i], GINT_TO_POINTER (nesting_index));
2109 jinfo = decode_llvm_mono_eh_frame (amodule, domain, method, code, clauses, num_clauses, generic_info_size + try_holes_info_size, nesting, &this_reg, &this_offset);
2110 jinfo->from_llvm = 1;
2113 for (i = 0; i < num_clauses; ++i)
2114 g_slist_free (nesting [i]);
2118 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size + try_holes_info_size);
2119 jinfo->num_clauses = num_clauses;
2121 for (i = 0; i < jinfo->num_clauses; ++i) {
2122 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
2124 ei->flags = decode_value (p, &p);
2126 ei->exvar_offset = decode_value (p, &p);
2128 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
2129 ei->data.filter = code + decode_value (p, &p);
2131 if (decode_value (p, &p))
2132 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
2135 ei->try_start = code + decode_value (p, &p);
2136 ei->try_end = code + decode_value (p, &p);
2137 ei->handler_start = code + decode_value (p, &p);
2140 jinfo->code_size = code_len;
2141 jinfo->used_regs = used_int_regs;
2142 jinfo->method = method;
2143 jinfo->code_start = code;
2144 jinfo->domain_neutral = 0;
2145 jinfo->from_aot = 1;
2148 if (has_generic_jit_info) {
2149 MonoGenericJitInfo *gi;
2151 jinfo->has_generic_jit_info = 1;
2153 gi = mono_jit_info_get_generic_jit_info (jinfo);
2157 gi->has_this = this_reg != -1;
2158 gi->this_reg = this_reg;
2159 gi->this_offset = this_offset;
2161 gi->has_this = decode_value (p, &p);
2162 gi->this_reg = decode_value (p, &p);
2163 gi->this_offset = decode_value (p, &p);
2166 /* This currently contains no data */
2167 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
2169 jinfo->method = decode_resolve_method_ref (amodule, p, &p);
2172 if (has_try_block_holes) {
2173 MonoTryBlockHoleTableJitInfo *table;
2175 jinfo->has_try_block_holes = 1;
2177 table = mono_jit_info_get_try_block_hole_table_info (jinfo);
2180 table->num_holes = (guint16)num_holes;
2181 for (i = 0; i < num_holes; ++i) {
2182 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
2183 hole->clause = decode_value (p, &p);
2184 hole->length = decode_value (p, &p);
2185 hole->offset = decode_value (p, &p);
2189 if (has_seq_points) {
2190 MonoSeqPointInfo *seq_points;
2191 int il_offset, native_offset, last_il_offset, last_native_offset, j;
2193 int len = decode_value (p, &p);
2195 seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
2196 seq_points->len = len;
2197 last_il_offset = last_native_offset = 0;
2198 for (i = 0; i < len; ++i) {
2199 SeqPoint *sp = &seq_points->seq_points [i];
2200 il_offset = last_il_offset + decode_value (p, &p);
2201 native_offset = last_native_offset + decode_value (p, &p);
2203 sp->il_offset = il_offset;
2204 sp->native_offset = native_offset;
2206 sp->next_len = decode_value (p, &p);
2207 sp->next = g_new (int, sp->next_len);
2208 for (j = 0; j < sp->next_len; ++j)
2209 sp->next [j] = decode_value (p, &p);
2211 last_il_offset = il_offset;
2212 last_native_offset = native_offset;
2215 mono_domain_lock (domain);
2216 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
2217 mono_domain_unlock (domain);
2220 /* Load debug info */
2221 buf_len = decode_value (p, &p);
2222 mono_debug_add_aot_method (domain, method, code, p, buf_len);
2226 int map_size = decode_value (p, &p);
2227 /* The GC map requires 4 bytes of alignment */
2228 while ((guint64)(gsize)p % 4)
2234 if (amodule != jinfo->method->klass->image->aot_module) {
2237 ji_to_amodule = g_hash_table_new (NULL, NULL);
2238 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
2246 * mono_aot_get_unwind_info:
2248 * Return a pointer to the DWARF unwind info belonging to JI.
2251 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2253 MonoAotModule *amodule = ji->method->klass->image->aot_module;
2255 guint8 *code = ji->code_start;
2258 g_assert (ji->from_aot);
2260 if (!(code >= amodule->code && code <= amodule->code_end)) {
2261 /* ji belongs to a different aot module than amodule */
2263 g_assert (ji_to_amodule);
2264 amodule = g_hash_table_lookup (ji_to_amodule, ji);
2266 g_assert (code >= amodule->code && code <= amodule->code_end);
2270 p = amodule->unwind_info + ji->used_regs;
2271 *unwind_info_len = decode_value (p, &p);
2275 static G_GNUC_UNUSED int
2276 compare_ints (const void *a, const void *b)
2278 return *(gint32*)a - *(gint32*)b;
2282 msort_code_offsets_internal (gint32 *array, int lo, int hi, gint32 *scratch)
2284 int mid = (lo + hi) / 2;
2291 for (i = lo; i < hi; ++i)
2292 if (array [(i * 2)] > array [(i * 2) + 2])
2295 /* Already sorted */
2299 msort_code_offsets_internal (array, lo, mid, scratch);
2300 msort_code_offsets_internal (array, mid + 1, hi, scratch);
2302 if (array [mid * 2] < array [(mid + 1) * 2])
2308 for (i = lo; i <= hi; i ++) {
2309 if (t_lo <= mid && ((t_hi > hi) || array [t_lo * 2] < array [t_hi * 2])) {
2310 scratch [(i * 2)] = array [t_lo * 2];
2311 scratch [(i * 2) + 1] = array [(t_lo *2) + 1];
2314 scratch [(i * 2)] = array [t_hi * 2];
2315 scratch [(i * 2) + 1] = array [(t_hi *2) + 1];
2319 for (i = lo; i <= hi; ++i) {
2320 array [(i * 2)] = scratch [i * 2];
2321 array [(i * 2) + 1] = scratch [(i * 2) + 1];
2326 msort_code_offsets (gint32 *array, int len)
2330 scratch = g_new (gint32, len * 2);
2331 msort_code_offsets_internal (array, 0, len - 1, scratch);
2336 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2338 int pos, left, right, offset, offset1, offset2, code_len;
2339 int method_index, table_len, is_wrapper;
2341 MonoAotModule *amodule = image->aot_module;
2344 guint8 *code, *ex_info, *p;
2346 int nmethods = amodule->info.nmethods;
2347 gint32 *code_offsets;
2353 if (domain != mono_get_root_domain ())
2357 offset = (guint8*)addr - amodule->code;
2359 /* Compute a sorted table mapping code offsets to method indexes. */
2360 if (!amodule->sorted_code_offsets) {
2361 code_offsets = g_new0 (gint32, nmethods * 2);
2363 for (i = 0; i < nmethods; ++i) {
2364 /* Skip the -1 entries to speed up sorting */
2365 if (amodule->code_offsets [i] == 0xffffffff)
2367 code_offsets [(offsets_len * 2)] = amodule->code_offsets [i];
2368 code_offsets [(offsets_len *2) + 1] = i;
2371 /* Use a merge sort as this is mostly sorted */
2372 msort_code_offsets (code_offsets, offsets_len);
2373 //qsort (code_offsets, offsets_len, sizeof (gint32) * 2, compare_ints);
2374 for (i = 0; i < offsets_len -1; ++i)
2375 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
2377 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
2378 /* Somebody got in before us */
2379 g_free (code_offsets);
2380 amodule->sorted_code_offsets_len = offsets_len;
2383 code_offsets = amodule->sorted_code_offsets;
2384 offsets_len = amodule->sorted_code_offsets_len;
2386 /* Binary search in the sorted_code_offsets table */
2388 right = offsets_len;
2390 pos = (left + right) / 2;
2392 offset1 = code_offsets [(pos * 2)];
2393 if (pos + 1 == offsets_len)
2394 offset2 = amodule->code_end - amodule->code;
2396 offset2 = code_offsets [(pos + 1) * 2];
2398 if (offset < offset1)
2400 else if (offset >= offset2)
2406 g_assert (offset >= code_offsets [(pos * 2)]);
2407 if (pos + 1 < offsets_len)
2408 g_assert (offset < code_offsets [((pos + 1) * 2)]);
2409 method_index = code_offsets [(pos * 2) + 1];
2411 code = &amodule->code [amodule->code_offsets [method_index]];
2412 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
2414 if (pos == offsets_len - 1)
2415 code_len = amodule->code_end - code;
2417 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
2419 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
2421 /* Might be a wrapper/extra method */
2422 if (amodule->extra_methods) {
2424 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
2431 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
2433 * This is hit for extra methods which are called directly, so they are
2434 * not in amodule->extra_methods.
2436 table_len = amodule->extra_method_info_offsets [0];
2437 table = amodule->extra_method_info_offsets + 1;
2444 pos = ((left + right) / 2);
2446 g_assert (pos < table_len);
2448 if (table [pos * 2] < method_index)
2450 else if (table [pos * 2] > method_index)
2456 p = amodule->blob + table [(pos * 2) + 1];
2457 is_wrapper = decode_value (p, &p);
2458 g_assert (!is_wrapper);
2459 method = decode_resolve_method_ref (amodule, p, &p);
2462 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
2463 method = mono_get_method (image, token, NULL);
2470 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
2472 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
2474 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
2475 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
2477 /* Add it to the normal JitInfo tables */
2478 mono_jit_info_table_add (domain, jinfo);
2484 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
2492 case MONO_PATCH_INFO_METHOD:
2493 case MONO_PATCH_INFO_METHOD_JUMP:
2494 case MONO_PATCH_INFO_ICALL_ADDR:
2495 case MONO_PATCH_INFO_METHOD_RGCTX: {
2499 res = decode_method_ref (aot_module, &ref, p, &p);
2503 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)) {
2504 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (ref.image, ref.token));
2505 ji->type = MONO_PATCH_INFO_ABS;
2509 ji->data.method = ref.method;
2511 ji->data.method = mono_get_method (ref.image, ref.token, NULL);
2512 g_assert (ji->data.method);
2513 mono_class_init (ji->data.method->klass);
2517 case MONO_PATCH_INFO_INTERNAL_METHOD:
2518 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2519 guint32 len = decode_value (p, &p);
2521 ji->data.name = (char*)p;
2525 case MONO_PATCH_INFO_METHODCONST:
2527 ji->data.method = decode_resolve_method_ref (aot_module, p, &p);
2528 if (!ji->data.method)
2531 case MONO_PATCH_INFO_VTABLE:
2532 case MONO_PATCH_INFO_CLASS:
2533 case MONO_PATCH_INFO_IID:
2534 case MONO_PATCH_INFO_ADJUSTED_IID:
2536 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2537 if (!ji->data.klass)
2540 case MONO_PATCH_INFO_CLASS_INIT:
2541 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2542 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2543 if (!ji->data.klass)
2546 case MONO_PATCH_INFO_IMAGE:
2547 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
2548 if (!ji->data.image)
2551 case MONO_PATCH_INFO_FIELD:
2552 case MONO_PATCH_INFO_SFLDA:
2554 ji->data.field = decode_field_info (aot_module, p, &p);
2555 if (!ji->data.field)
2558 case MONO_PATCH_INFO_SWITCH:
2559 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
2560 ji->data.table->table_size = decode_value (p, &p);
2561 table = mono_domain_alloc (mono_domain_get (), sizeof (gpointer) * ji->data.table->table_size);
2562 ji->data.table->table = (MonoBasicBlock**)table;
2563 for (i = 0; i < ji->data.table->table_size; i++)
2564 table [i] = (gpointer)(gssize)decode_value (p, &p);
2566 case MONO_PATCH_INFO_R4: {
2569 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
2570 val = decode_value (p, &p);
2571 *(float*)ji->data.target = *(float*)&val;
2574 case MONO_PATCH_INFO_R8: {
2578 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
2580 val [0] = decode_value (p, &p);
2581 val [1] = decode_value (p, &p);
2582 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
2583 *(double*)ji->data.target = *(double*)&v;
2586 case MONO_PATCH_INFO_LDSTR:
2587 image = load_image (aot_module, decode_value (p, &p), TRUE);
2590 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
2592 case MONO_PATCH_INFO_RVA:
2593 case MONO_PATCH_INFO_DECLSEC:
2594 case MONO_PATCH_INFO_LDTOKEN:
2595 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2597 image = load_image (aot_module, decode_value (p, &p), TRUE);
2600 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
2602 ji->data.token->has_context = decode_value (p, &p);
2603 if (ji->data.token->has_context) {
2604 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
2609 case MONO_PATCH_INFO_EXC_NAME:
2610 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2611 if (!ji->data.klass)
2613 ji->data.name = ji->data.klass->name;
2615 case MONO_PATCH_INFO_METHOD_REL:
2616 ji->data.offset = decode_value (p, &p);
2618 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2619 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2620 case MONO_PATCH_INFO_MONITOR_ENTER:
2621 case MONO_PATCH_INFO_MONITOR_EXIT:
2622 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
2623 case MONO_PATCH_INFO_CASTCLASS_CACHE:
2625 case MONO_PATCH_INFO_RGCTX_FETCH: {
2627 MonoJumpInfoRgctxEntry *entry;
2628 guint32 offset, val;
2631 offset = decode_value (p, &p);
2632 val = decode_value (p, &p);
2634 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2635 p2 = aot_module->blob + offset;
2636 entry->method = decode_resolve_method_ref (aot_module, p2, &p2);
2637 entry->in_mrgctx = ((val & 1) > 0) ? TRUE : FALSE;
2638 entry->info_type = (val >> 1) & 0xff;
2639 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2640 entry->data->type = (val >> 9) & 0xff;
2642 res = decode_patch (aot_module, mp, entry->data, p, &p);
2645 ji->data.rgctx_entry = entry;
2648 case MONO_PATCH_INFO_SEQ_POINT_INFO:
2650 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
2651 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
2653 imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
2654 imt_tramp->vt_offset = decode_value (p, &p);
2656 ji->data.imt_tramp = imt_tramp;
2660 g_warning ("unhandled type %d", ji->type);
2661 g_assert_not_reached ();
2672 static MonoJumpInfo*
2673 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
2674 guint32 **got_slots,
2675 guint8 *buf, guint8 **endbuf)
2677 MonoJumpInfo *patches;
2683 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
2685 *got_slots = g_malloc (sizeof (guint32) * n_patches);
2687 for (pindex = 0; pindex < n_patches; ++pindex) {
2688 MonoJumpInfo *ji = &patches [pindex];
2693 got_offset = decode_value (p, &p);
2695 if (aot_module->got [got_offset]) {
2696 /* Already loaded */
2697 //printf ("HIT!\n");
2699 shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
2701 ji->type = decode_value (shared_p, &shared_p);
2703 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
2708 (*got_slots) [pindex] = got_offset;
2715 g_free (*got_slots);
2722 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
2725 * Jump addresses cannot be patched by the trampoline code since it
2726 * does not have access to the caller's address. Instead, we collect
2727 * the addresses of the GOT slots pointing to a method, and patch
2728 * them after the method has been compiled.
2730 MonoJitDomainInfo *info = domain_jit_info (domain);
2733 mono_domain_lock (domain);
2734 if (!info->jump_target_got_slot_hash)
2735 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
2736 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
2737 list = g_slist_prepend (list, got_slot);
2738 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
2739 mono_domain_unlock (domain);
2745 * Load the method identified by METHOD_INDEX from the AOT image. Return a
2746 * pointer to the native code of the method, or NULL if not found.
2747 * METHOD might not be set if the caller only has the image/token info.
2750 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
2753 gboolean from_plt = method == NULL;
2755 int i, pindex, n_patches, used_strings;
2756 gboolean keep_patches = TRUE;
2758 MonoJitInfo *jinfo = NULL;
2759 guint8 *code, *info;
2761 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2764 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
2765 /* Non shared AOT code can't be used in other appdomains */
2768 if (amodule->out_of_date)
2771 if (amodule->code_offsets [method_index] == 0xffffffff) {
2772 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2776 method = mono_get_method (image, token, NULL);
2777 full_name = mono_method_full_name (method, TRUE);
2778 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2784 code = &amodule->code [amodule->code_offsets [method_index]];
2786 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
2788 if (amodule->thumb_end && code < amodule->thumb_end) {
2789 /* Convert this into a thumb address */
2790 g_assert ((amodule->code_offsets [method_index] & 0x1) == 0);
2791 code = &amodule->code [amodule->code_offsets [method_index] + 1];
2795 if (!amodule->methods_loaded)
2796 amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods / 32 + 1);
2799 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2802 if (mono_last_aot_method != -1) {
2803 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
2805 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
2807 method = mono_get_method (image, token, NULL);
2809 char *name = mono_method_full_name (method, TRUE);
2810 printf ("LAST AOT METHOD: %s.\n", name);
2813 printf ("LAST AOT METHOD: %p %d\n", code, method_index);
2821 klass = method->klass;
2822 decode_klass_ref (amodule, p, &p);
2824 klass = decode_klass_ref (amodule, p, &p);
2827 if (amodule->info.opts & MONO_OPT_SHARED)
2828 used_strings = decode_value (p, &p);
2832 for (i = 0; i < used_strings; i++) {
2833 guint token = decode_value (p, &p);
2834 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2837 if (amodule->info.opts & MONO_OPT_SHARED)
2838 keep_patches = FALSE;
2840 n_patches = decode_value (p, &p);
2842 keep_patches = FALSE;
2845 MonoJumpInfo *patches;
2851 mp = mono_mempool_new ();
2853 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2854 if (patches == NULL)
2857 for (pindex = 0; pindex < n_patches; ++pindex) {
2858 MonoJumpInfo *ji = &patches [pindex];
2860 if (!amodule->got [got_slots [pindex]]) {
2861 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2862 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2863 amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
2864 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2865 register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
2867 ji->type = MONO_PATCH_INFO_NONE;
2873 mono_mempool_destroy (mp);
2876 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2880 method = mono_get_method (image, token, NULL);
2882 full_name = mono_method_full_name (method, TRUE);
2885 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
2887 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND method %s [%p - %p %p]\n", full_name, code, code + jinfo->code_size, info);
2893 InterlockedIncrement (&mono_jit_stats.methods_aot);
2895 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2899 if (method && method->wrapper_type)
2900 g_hash_table_insert (amodule->method_to_code, method, code);
2904 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
2908 method = mono_get_method (image, token, NULL);
2911 mono_profiler_method_jit (method);
2912 jinfo = mono_jit_info_table_find (domain, (char*)code);
2914 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
2917 if (from_plt && klass && !klass->generic_container)
2918 mono_runtime_class_init (mono_class_vtable (domain, klass));
2923 /* FIXME: The space in domain->mp is wasted */
2924 if (amodule->info.opts & MONO_OPT_SHARED)
2925 /* No need to cache patches */
2926 mono_mempool_destroy (mp);
2935 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name)
2937 guint32 table_size, entry_size, hash;
2938 guint32 *table, *entry;
2940 static guint32 n_extra_decodes;
2942 if (!amodule || amodule->out_of_date)
2945 table_size = amodule->extra_method_table [0];
2946 table = amodule->extra_method_table + 1;
2949 hash = mono_aot_method_hash (method) % table_size;
2951 entry = &table [hash * entry_size];
2958 guint32 key = entry [0];
2959 guint32 value = entry [1];
2960 guint32 next = entry [entry_size - 1];
2964 p = amodule->blob + key;
2968 if (!amodule->method_ref_to_method)
2969 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2970 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2973 m = decode_resolve_method_ref_with_target (amodule, method, p, &p);
2976 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2985 /* Special case: wrappers of shared generic methods */
2986 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2987 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2988 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2989 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2991 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2997 /* Methods decoded needlessly */
2999 //printf ("%d %s %s %p\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE), orig_p);
3004 entry = &table [next * entry_size];
3013 add_module_cb (gpointer key, gpointer value, gpointer user_data)
3015 g_ptr_array_add ((GPtrArray*)user_data, value);
3019 * find_extra_method:
3021 * Try finding METHOD in the extra_method table in all AOT images.
3022 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
3023 * module where the method was found.
3026 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
3033 if (method->wrapper_type)
3034 name = mono_aot_wrapper_name (method);
3036 /* Try the method's module first */
3037 *out_amodule = method->klass->image->aot_module;
3038 index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name);
3039 if (index != 0xffffff) {
3045 * Try all other modules.
3046 * This is needed because generic instances klass->image points to the image
3047 * containing the generic definition, but the native code is generated to the
3048 * AOT image which contains the reference.
3051 /* Make a copy to avoid doing the search inside the aot lock */
3052 modules = g_ptr_array_new ();
3054 g_hash_table_foreach (aot_modules, add_module_cb, modules);
3058 for (i = 0; i < modules->len; ++i) {
3059 MonoAotModule *amodule = g_ptr_array_index (modules, i);
3061 if (amodule != method->klass->image->aot_module)
3062 index = find_extra_method_in_amodule (amodule, method, name);
3063 if (index != 0xffffff) {
3064 *out_amodule = amodule;
3069 g_ptr_array_free (modules, TRUE);
3076 * mono_aot_get_method:
3078 * Return a pointer to the AOTed native code for METHOD if it can be found,
3080 * On platforms with function pointers, this doesn't return a function pointer.
3083 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3085 MonoClass *klass = method->klass;
3086 guint32 method_index;
3087 MonoAotModule *amodule = klass->image->aot_module;
3093 if (amodule->out_of_date)
3096 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
3097 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3098 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3099 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
3103 * Use the original method instead of its invoke-with-check wrapper.
3104 * This is not a problem when using full-aot, since it doesn't support
3107 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
3108 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
3110 g_assert (klass->inited);
3112 /* Find method index */
3113 if (method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE)) {
3115 * For generic methods, we store the fully shared instance in place of the
3118 method = mono_method_get_declaring_generic_method (method);
3119 method_index = mono_metadata_token_index (method->token) - 1;
3120 } else if (method->is_inflated || !method->token) {
3121 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
3123 code = g_hash_table_lookup (amodule->method_to_code, method);
3128 method_index = find_extra_method (method, &amodule);
3130 * Special case the ICollection<T> wrappers for arrays, as they cannot
3131 * be statically enumerated, and each wrapper ends up calling the same
3134 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
3135 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
3137 code = mono_aot_get_method (domain, m);
3139 if (mono_method_needs_static_rgctx_invoke (m, FALSE)) {
3140 code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code));
3141 /* The call above returns an ftnptr */
3142 code = mono_get_addr_from_ftnptr (code);
3150 * Special case Array.GetGenericValueImpl which is a generic icall.
3151 * Generic sharing currently can't handle it, but the icall returns data using
3152 * an out parameter, so the managed-to-native wrappers can share the same code.
3154 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
3156 MonoGenericContext ctx;
3157 MonoType *args [16];
3159 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
3160 /* Avoid recursion */
3163 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
3166 memset (&ctx, 0, sizeof (ctx));
3167 args [0] = &mono_defaults.object_class->byval_arg;
3168 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
3170 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
3173 * Get the code for the <object> instantiation which should be emitted into
3174 * the mscorlib aot image by the AOT compiler.
3176 code = mono_aot_get_method (domain, m);
3181 /* Same for CompareExchange<T> and Exchange<T> */
3182 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass->image == mono_defaults.corlib && !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])) {
3184 MonoGenericContext ctx;
3185 MonoType *args [16];
3186 gpointer iter = NULL;
3188 while ((m = mono_class_get_methods (method->klass, &iter))) {
3189 if (mono_method_signature (m)->generic_param_count && !strcmp (m->name, method->name))
3194 memset (&ctx, 0, sizeof (ctx));
3195 args [0] = &mono_defaults.object_class->byval_arg;
3196 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
3198 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
3200 /* Avoid recursion */
3205 * Get the code for the <object> instantiation which should be emitted into
3206 * the mscorlib aot image by the AOT compiler.
3208 code = mono_aot_get_method (domain, m);
3213 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, TRUE)) {
3214 /* Partial sharing */
3215 method_index = find_extra_method (mini_get_shared_method (method), &amodule);
3218 if (method_index == 0xffffff) {
3219 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
3222 full_name = mono_method_full_name (method, TRUE);
3223 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
3229 if (method_index == 0xffffff)
3232 /* Needed by find_jit_info */
3234 if (!amodule->extra_methods)
3235 amodule->extra_methods = g_hash_table_new (NULL, NULL);
3236 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
3240 method_index = mono_metadata_token_index (method->token) - 1;
3243 return load_method (domain, amodule, klass->image, method, method->token, method_index);
3247 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
3251 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3253 MonoAotModule *aot_module = image->aot_module;
3259 method_index = mono_metadata_token_index (token) - 1;
3261 return load_method (domain, aot_module, image, NULL, token, method_index);
3267 } IsGotEntryUserData;
3270 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
3272 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
3273 MonoAotModule *aot_module = (MonoAotModule*)value;
3275 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
3280 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3282 IsGotEntryUserData user_data;
3287 user_data.addr = addr;
3288 user_data.res = FALSE;
3290 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
3293 return user_data.res;
3298 MonoAotModule *module;
3299 } FindAotModuleUserData;
3302 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
3304 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
3305 MonoAotModule *aot_module = (MonoAotModule*)value;
3307 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
3308 data->module = aot_module;
3311 static inline MonoAotModule*
3312 find_aot_module (guint8 *code)
3314 FindAotModuleUserData user_data;
3319 /* Reading these need no locking */
3320 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
3323 user_data.addr = code;
3324 user_data.module = NULL;
3327 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
3330 return user_data.module;
3334 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
3337 * Since AOT code is only used in the root domain,
3338 * mono_domain_get () != mono_get_root_domain () means the calling method
3339 * is AppDomain:InvokeInDomain, so this is the same check as in
3340 * mono_method_same_domain () but without loading the metadata for the method.
3342 if (mono_domain_get () == mono_get_root_domain ())
3343 mono_arch_patch_plt_entry (code, got, regs, addr);
3347 * mono_aot_plt_resolve:
3349 * This function is called by the entries in the PLT to resolve the actual method that
3350 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
3351 * Returns NULL if the something cannot be loaded.
3354 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3356 #ifdef MONO_ARCH_AOT_SUPPORTED
3357 guint8 *p, *target, *plt_entry;
3359 MonoAotModule *module = (MonoAotModule*)aot_module;
3360 gboolean res, no_ftnptr = FALSE;
3363 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
3365 p = &module->blob [plt_info_offset];
3367 ji.type = decode_value (p, &p);
3369 mp = mono_mempool_new_size (512);
3370 res = decode_patch (module, mp, &ji, p, &p);
3373 mono_mempool_destroy (mp);
3378 * Avoid calling resolve_patch_target in the full-aot case if possible, since
3379 * it would create a trampoline, and we don't need that.
3380 * We could do this only if the method does not need the special handling
3381 * in mono_magic_trampoline ().
3383 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) &&
3384 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
3385 target = mono_jit_compile_method (ji.data.method);
3388 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
3392 * The trampoline expects us to return a function descriptor on platforms which use
3393 * it, but resolve_patch_target returns a direct function pointer for some type of
3394 * patches, so have to translate between the two.
3395 * FIXME: Clean this up, but how ?
3397 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) {
3398 /* These should already have a function descriptor */
3399 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3400 /* Our function descriptors have a 0 environment, gcc created ones don't */
3401 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
3402 g_assert (((gpointer*)target) [2] == 0);
3405 } else if (!no_ftnptr) {
3406 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3407 g_assert (((gpointer*)target) [2] != 0);
3409 target = mono_create_ftnptr (mono_domain_get (), target);
3412 mono_mempool_destroy (mp);
3414 /* Patch the PLT entry with target which might be the actual method not a trampoline */
3415 plt_entry = mono_aot_get_plt_entry (code);
3416 g_assert (plt_entry);
3417 mono_aot_patch_plt_entry (plt_entry, module->got, NULL, target);
3421 g_assert_not_reached ();
3429 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
3430 * method in the module is loaded to avoid committing memory by writing to it.
3431 * LOCKING: Assumes the AOT lock is held.
3434 init_plt (MonoAotModule *amodule)
3439 if (amodule->plt_inited)
3442 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
3445 * Initialize the PLT entries in the GOT to point to the default targets.
3448 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
3449 for (i = 1; i < amodule->info.plt_size; ++i)
3450 /* All the default entries point to the AOT trampoline */
3451 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = tramp;
3453 amodule->plt_inited = TRUE;
3457 * mono_aot_get_plt_entry:
3459 * Return the address of the PLT entry called by the code at CODE if exists.
3462 mono_aot_get_plt_entry (guint8 *code)
3464 MonoAotModule *amodule = find_aot_module (code);
3465 guint8 *target = NULL;
3471 if (amodule->thumb_end && code < amodule->thumb_end) {
3472 return mono_arm_get_thumb_plt_entry (code);
3476 #ifdef MONO_ARCH_AOT_SUPPORTED
3477 target = mono_arch_get_call_target (code);
3479 g_assert_not_reached ();
3482 if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
3489 * mono_aot_get_plt_info_offset:
3491 * Return the PLT info offset belonging to the plt entry called by CODE.
3494 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3496 guint8 *plt_entry = mono_aot_get_plt_entry (code);
3498 g_assert (plt_entry);
3500 /* The offset is embedded inside the code after the plt entry */
3501 #ifdef MONO_ARCH_AOT_SUPPORTED
3502 return mono_arch_get_plt_info_offset (plt_entry, regs, code);
3504 g_assert_not_reached ();
3510 mono_create_ftnptr_malloc (guint8 *code)
3512 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3513 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
3515 ftnptr->code = code;
3526 * mono_aot_register_jit_icall:
3528 * Register a JIT icall which is called by trampolines in full-aot mode. This should
3529 * be called from mono_arch_init () during startup.
3532 mono_aot_register_jit_icall (const char *name, gpointer addr)
3534 /* No need for locking */
3535 if (!aot_jit_icall_hash)
3536 aot_jit_icall_hash = g_hash_table_new (g_str_hash, g_str_equal);
3537 g_hash_table_insert (aot_jit_icall_hash, (char*)name, addr);
3543 * Load the function named NAME from the aot image.
3546 load_function (MonoAotModule *amodule, const char *name)
3550 int n_patches, pindex;
3556 symbol = g_strdup_printf ("%s", name);
3557 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
3560 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
3562 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
3566 symbol = g_strdup_printf ("%s_p", name);
3567 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
3570 /* Nothing to patch */
3573 p = amodule->blob + *(guint32*)p;
3575 /* Similar to mono_aot_load_method () */
3577 n_patches = decode_value (p, &p);
3580 MonoJumpInfo *patches;
3583 mp = mono_mempool_new ();
3585 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
3588 for (pindex = 0; pindex < n_patches; ++pindex) {
3589 MonoJumpInfo *ji = &patches [pindex];
3592 if (amodule->got [got_slots [pindex]])
3596 * When this code is executed, the runtime may not be initalized yet, so
3597 * resolve the patch info by hand.
3599 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3600 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
3601 target = mono_get_lmf_addr;
3602 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
3603 target = mono_thread_force_interruption_checkpoint;
3604 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
3605 target = mono_exception_from_token;
3606 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
3607 target = mono_get_throw_exception ();
3608 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
3609 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
3610 target = (gpointer)mono_get_trampoline_func (tramp_type2);
3611 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
3612 /* atoll is needed because the the offset is unsigned */
3616 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
3617 g_assert (res == 1);
3618 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
3619 target = mono_create_ftnptr_malloc (target);
3620 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
3621 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
3622 target = mono_create_ftnptr_malloc (target);
3623 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
3624 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
3625 target = mono_create_ftnptr_malloc (target);
3626 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
3627 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
3628 target = mono_create_ftnptr_malloc (target);
3629 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
3630 target = mono_thread_get_and_clear_pending_exception;
3631 } else if (strstr (ji->data.name, "generic_trampoline_")) {
3632 target = mono_aot_get_trampoline (ji->data.name);
3633 } else if (aot_jit_icall_hash && g_hash_table_lookup (aot_jit_icall_hash, ji->data.name)) {
3634 /* Registered by mono_arch_init () */
3635 target = g_hash_table_lookup (aot_jit_icall_hash, ji->data.name);
3637 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
3638 g_assert_not_reached ();
3642 /* Hopefully the code doesn't have patches which need method or
3645 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
3649 amodule->got [got_slots [pindex]] = target;
3654 mono_mempool_destroy (mp);
3661 * Return the trampoline identified by NAME from the mscorlib AOT file.
3662 * On ppc64, this returns a function descriptor.
3665 mono_aot_get_trampoline (const char *name)
3668 MonoAotModule *amodule;
3670 image = mono_defaults.corlib;
3673 amodule = image->aot_module;
3676 return mono_create_ftnptr_malloc (load_function (amodule, name));
3679 /* Return a given kind of trampoline */
3681 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
3683 MonoAotModule *amodule;
3684 int index, tramp_size;
3687 /* Currently, we keep all trampolines in the mscorlib AOT image */
3688 image = mono_defaults.corlib;
3693 amodule = image->aot_module;
3696 *out_amodule = amodule;
3698 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
3699 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
3701 index = amodule->trampoline_index [tramp_type] ++;
3705 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
3707 tramp_size = amodule->info.trampoline_size [tramp_type];
3710 *out_tramp_size = tramp_size;
3712 return amodule->trampolines [tramp_type] + (index * tramp_size);
3716 * Return a specific trampoline from the AOT file.
3719 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3721 MonoAotModule *amodule;
3722 guint32 got_offset, tramp_size;
3723 guint8 *code, *tramp;
3724 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
3725 static gboolean inited;
3726 static guint32 num_trampolines;
3732 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
3741 if (!generic_trampolines [tramp_type]) {
3744 symbol = mono_get_generic_trampoline_name (tramp_type);
3745 generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
3749 tramp = generic_trampolines [tramp_type];
3752 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
3754 amodule->got [got_offset] = tramp;
3755 amodule->got [got_offset + 1] = arg1;
3758 *code_len = tramp_size;
3764 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3766 MonoAotModule *amodule;
3770 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
3772 amodule->got [got_offset] = ctx;
3773 amodule->got [got_offset + 1] = addr;
3775 /* The caller expects an ftnptr */
3776 return mono_create_ftnptr (mono_domain_get (), code);
3780 mono_aot_get_unbox_trampoline (MonoMethod *method)
3782 guint32 method_index = mono_metadata_token_index (method->token) - 1;
3783 MonoAotModule *amodule;
3785 guint32 *ut, *ut_end, *entry;
3786 int low, high, entry_index;
3788 if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
3789 method_index = find_extra_method (method, &amodule);
3790 g_assert (method_index != 0xffffff);
3792 amodule = method->klass->image->aot_module;
3796 ut = amodule->unbox_trampolines;
3797 ut_end = amodule->unbox_trampolines_end;
3799 /* Do a binary search in the sorted table */
3802 high = (ut_end - ut) / 2;
3803 while (low < high) {
3804 entry_index = (low + high) / 2;
3805 entry = &ut [(entry_index * 2)];
3806 if (entry [0] < method_index) {
3807 low = entry_index + 1;
3808 } else if (entry [0] > method_index) {
3811 code = amodule->code + entry [1];
3817 /* The caller expects an ftnptr */
3818 return mono_create_ftnptr (mono_domain_get (), code);
3822 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3827 symbol = mono_get_rgctx_fetch_trampoline_name (slot);
3828 code = load_function (mono_defaults.corlib->aot_module, symbol);
3830 /* The caller expects an ftnptr */
3831 return mono_create_ftnptr (mono_domain_get (), code);
3835 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3840 int i, index, real_count;
3841 MonoAotModule *amodule;
3843 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
3846 for (i = 0; i < count; ++i) {
3847 MonoIMTCheckItem *item = imt_entries [i];
3849 if (item->is_equals)
3853 /* Save the entries into an array */
3854 buf = mono_domain_alloc (domain, (real_count + 1) * 2 * sizeof (gpointer));
3856 for (i = 0; i < count; ++i) {
3857 MonoIMTCheckItem *item = imt_entries [i];
3859 if (!item->is_equals)
3862 g_assert (item->key);
3864 buf [(index * 2)] = item->key;
3865 if (item->has_target_code) {
3866 gpointer *p = mono_domain_alloc (domain, sizeof (gpointer));
3867 *p = item->value.target_code;
3868 buf [(index * 2) + 1] = p;
3870 buf [(index * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
3874 buf [(index * 2)] = NULL;
3875 buf [(index * 2) + 1] = fail_tramp;
3877 amodule->got [got_offset] = buf;
3883 * mono_aot_set_make_unreadable:
3885 * Set whenever to make all mmaped memory unreadable. In conjuction with a
3886 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
3889 mono_aot_set_make_unreadable (gboolean unreadable)
3893 make_unreadable = unreadable;
3895 if (make_unreadable && !inited) {
3896 mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
3901 MonoAotModule *module;
3906 find_map (gpointer key, gpointer value, gpointer user_data)
3908 MonoAotModule *module = (MonoAotModule*)value;
3909 FindMapUserData *data = (FindMapUserData*)user_data;
3912 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
3913 data->module = module;
3916 static MonoAotModule*
3917 find_module_for_addr (void *ptr)
3919 FindMapUserData data;
3921 if (!make_unreadable)
3925 data.ptr = (guint8*)ptr;
3928 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
3935 * mono_aot_is_pagefault:
3937 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
3938 * within memory allocated by this module.
3941 mono_aot_is_pagefault (void *ptr)
3943 if (!make_unreadable)
3947 * Not signal safe, but SIGSEGV's are synchronous, and
3948 * this is only turned on by a MONO_DEBUG option.
3950 return find_module_for_addr (ptr) != NULL;
3954 * mono_aot_handle_pagefault:
3956 * Handle a pagefault caused by an unreadable page by making it readable again.
3959 mono_aot_handle_pagefault (void *ptr)
3961 #ifndef PLATFORM_WIN32
3962 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
3966 res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
3967 g_assert (res == 0);
3978 mono_aot_init (void)
3983 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3989 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3995 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
4001 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
4007 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
4013 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
4019 mono_aot_get_plt_entry (guint8 *code)
4025 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
4031 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
4036 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
4042 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
4044 g_assert_not_reached ();
4050 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
4052 g_assert_not_reached ();
4057 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
4059 g_assert_not_reached ();
4064 mono_aot_get_trampoline (const char *name)
4066 g_assert_not_reached ();
4071 mono_aot_get_unbox_trampoline (MonoMethod *method)
4073 g_assert_not_reached ();
4078 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
4080 g_assert_not_reached ();
4085 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
4087 g_assert_not_reached ();
4092 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
4094 g_assert_not_reached ();
4099 mono_aot_register_jit_icall (const char *name, gpointer addr)