2 * aot-runtime.c: mono Ahead of Time compiler
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2002 Ximian, Inc.
12 #include <sys/types.h>
18 #ifdef HAVE_SYS_MMAN_H
27 #ifdef HAVE_EXECINFO_H
34 #ifdef HAVE_SYS_WAIT_H
35 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
38 #ifdef HAVE_DL_ITERATE_PHDR
42 #include <mono/metadata/tabledefs.h>
43 #include <mono/metadata/class.h>
44 #include <mono/metadata/object.h>
45 #include <mono/metadata/tokentype.h>
46 #include <mono/metadata/appdomain.h>
47 #include <mono/metadata/debug-helpers.h>
48 #include <mono/metadata/assembly.h>
49 #include <mono/metadata/metadata-internals.h>
50 #include <mono/metadata/marshal.h>
51 #include <mono/metadata/gc-internal.h>
52 #include <mono/metadata/monitor.h>
53 #include <mono/metadata/threads-types.h>
54 #include <mono/metadata/mono-endian.h>
55 #include <mono/utils/mono-logger-internal.h>
56 #include <mono/utils/mono-mmap.h>
57 #include "mono/utils/mono-compiler.h"
58 #include <mono/utils/mono-counters.h>
66 #define SHARED_EXT ".dll"
67 #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
68 #define SHARED_EXT ".dylib"
69 #elif defined(__APPLE__) && defined(TARGET_X86) && !defined(__native_client_codegen__)
70 #define SHARED_EXT ".dylib"
72 #define SHARED_EXT ".so"
75 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
76 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
78 typedef struct MonoAotModule {
80 /* Pointer to the Global Offset Table */
82 GHashTable *name_cache;
83 GHashTable *extra_methods;
84 /* Maps methods to their code */
85 GHashTable *method_to_code;
86 /* Maps pointers into the method info to the methods themselves */
87 GHashTable *method_ref_to_method;
88 MonoAssemblyName *image_names;
90 MonoAssembly *assembly;
91 MonoImage **image_table;
92 guint32 image_table_len;
102 gint32 *code_offsets;
103 /* This contains <offset, index> pairs sorted by offset */
104 /* This is needed because LLVM emitted methods can be in any order */
105 gint32 *sorted_code_offsets;
106 gint32 sorted_code_offsets_len;
107 guint32 *method_info_offsets;
108 guint32 *got_info_offsets;
109 guint32 *ex_info_offsets;
110 guint32 *class_info_offsets;
111 guint32 *methods_loaded;
112 guint16 *class_name_table;
113 guint32 *extra_method_table;
114 guint32 *extra_method_info_offsets;
118 /* Points to the mono EH data created by LLVM */
119 guint8 *mono_eh_frame;
121 /* Points to the trampolines */
122 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
123 /* The first unused trampoline of each kind */
124 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
126 MonoAotFileInfo info;
132 static GHashTable *aot_modules;
133 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
134 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
135 static CRITICAL_SECTION aot_mutex;
138 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
139 * AOT modules registered by mono_aot_register_module ().
141 static GHashTable *static_aot_modules;
144 * Maps MonoJitInfo* to the aot module they belong to, this can be different
145 * from ji->method->klass->image's aot module for generic instances.
147 static GHashTable *ji_to_amodule;
150 * Whenever to AOT compile loaded assemblies on demand and store them in
151 * a cache under $HOME/.mono/aot-cache.
153 static gboolean use_aot_cache = FALSE;
156 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
157 * use_aot_cache is TRUE.
159 static gboolean spawn_compiler = TRUE;
162 static gint32 mono_last_aot_method = -1;
164 static gboolean make_unreadable = FALSE;
165 static guint32 name_table_accesses = 0;
166 static guint32 n_pagefaults = 0;
168 /* Used to speed-up find_aot_module () */
169 static gsize aot_code_low_addr = (gssize)-1;
170 static gsize aot_code_high_addr = 0;
172 static GHashTable *aot_jit_icall_hash;
175 init_plt (MonoAotModule *info);
177 /*****************************************************/
179 /*****************************************************/
184 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
185 * found, and sets the loader error if SET_ERROR is TRUE.
188 load_image (MonoAotModule *amodule, int index, gboolean set_error)
190 MonoAssembly *assembly;
191 MonoImageOpenStatus status;
193 g_assert (index < amodule->image_table_len);
195 if (amodule->image_table [index])
196 return amodule->image_table [index];
197 if (amodule->out_of_date)
200 assembly = mono_assembly_load (&amodule->image_names [index], amodule->assembly->basedir, &status);
202 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);
203 amodule->out_of_date = TRUE;
206 char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
207 mono_loader_set_error_assembly_load (full_name, FALSE);
213 if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
214 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", amodule->aot_name, amodule->image_names [index].name);
215 amodule->out_of_date = TRUE;
219 amodule->image_table [index] = assembly->image;
220 return assembly->image;
224 decode_value (guint8 *ptr, guint8 **rptr)
229 if ((b & 0x80) == 0){
232 } else if ((b & 0x40) == 0){
233 len = ((b & 0x3f) << 8 | ptr [1]);
235 } else if (b != 0xff) {
236 len = ((b & 0x1f) << 24) |
243 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
249 //printf ("DECODE: %d.\n", len);
254 * mono_aot_get_method:
256 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
260 mono_aot_get_offset (guint32 *table, int index)
262 int i, group, ngroups, index_entry_size;
263 int start_offset, offset, noffsets, group_size;
264 guint8 *data_start, *p;
265 guint32 *index32 = NULL;
266 guint16 *index16 = NULL;
268 noffsets = table [0];
269 group_size = table [1];
271 index_entry_size = table [3];
272 group = index / group_size;
274 if (index_entry_size == 2) {
275 index16 = (guint16*)&table [4];
276 data_start = (guint8*)&index16 [ngroups];
277 p = data_start + index16 [group];
279 index32 = (guint32*)&table [4];
280 data_start = (guint8*)&index32 [ngroups];
281 p = data_start + index32 [group];
284 /* offset will contain the value of offsets [group * group_size] */
285 offset = start_offset = decode_value (p, &p);
286 for (i = group * group_size + 1; i <= index; ++i) {
287 offset += decode_value (p, &p);
290 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
296 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
299 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
301 static MonoGenericInst*
302 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
305 MonoType **type_argv;
306 MonoGenericInst *inst;
309 type_argc = decode_value (p, &p);
310 type_argv = g_new0 (MonoType*, type_argc);
312 for (i = 0; i < type_argc; ++i) {
313 MonoClass *pclass = decode_klass_ref (module, p, &p);
318 type_argv [i] = &pclass->byval_arg;
321 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
330 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
332 gboolean has_class_inst, has_method_inst;
335 has_class_inst = decode_value (p, &p);
336 if (has_class_inst) {
337 ctx->class_inst = decode_generic_inst (module, p, &p);
338 if (!ctx->class_inst)
341 has_method_inst = decode_value (p, &p);
342 if (has_method_inst) {
343 ctx->method_inst = decode_generic_inst (module, p, &p);
344 if (!ctx->method_inst)
353 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
356 MonoClass *klass, *eklass;
360 token = decode_value (p, &p);
365 if (mono_metadata_token_table (token) == 0) {
366 image = load_image (module, decode_value (p, &p), TRUE);
369 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
370 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
371 if (token == MONO_TOKEN_TYPE_SPEC) {
372 MonoTypeEnum type = decode_value (p, &p);
374 if (type == MONO_TYPE_GENERICINST) {
376 MonoGenericContext ctx;
379 gclass = decode_klass_ref (module, p, &p);
382 g_assert (gclass->generic_container);
384 memset (&ctx, 0, sizeof (ctx));
385 ctx.class_inst = decode_generic_inst (module, p, &p);
388 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
389 klass = mono_class_from_mono_type (type);
390 mono_metadata_free_type (type);
391 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
393 MonoGenericContainer *container;
395 int num = decode_value (p, &p);
396 gboolean is_method = decode_value (p, &p);
399 MonoMethod *method_def;
400 g_assert (type == MONO_TYPE_MVAR);
401 method_def = decode_resolve_method_ref (module, p, &p);
405 container = mono_method_get_generic_container (method_def);
407 MonoClass *class_def;
408 g_assert (type == MONO_TYPE_VAR);
409 class_def = decode_klass_ref (module, p, &p);
413 container = class_def->generic_container;
416 g_assert (container);
418 // FIXME: Memory management
419 t = g_new0 (MonoType, 1);
421 t->data.generic_param = mono_generic_container_get_param (container, num);
423 // FIXME: Maybe use types directly to avoid
424 // the overhead of creating MonoClass-es
425 klass = mono_class_from_mono_type (t);
429 g_assert_not_reached ();
432 image = load_image (module, decode_value (p, &p), TRUE);
435 klass = mono_class_get (image, token);
437 } else if (token == MONO_TOKEN_TYPE_DEF) {
439 image = load_image (module, decode_value (p, &p), TRUE);
442 rank = decode_value (p, &p);
443 eklass = decode_klass_ref (module, p, &p);
444 klass = mono_array_class_get (eklass, rank);
446 g_assert_not_reached ();
454 static MonoClassField*
455 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
457 MonoClass *klass = decode_klass_ref (module, buf, &buf);
464 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
468 return mono_class_get_field (klass, token);
471 /* Stores information returned by decode_method_ref () */
476 gboolean no_aot_trampoline;
480 * decode_method_ref_with_target:
482 * Decode a method reference, storing the image/token into a MethodRef structure.
483 * This avoids loading metadata for the method if the caller does not need it. If the method has
484 * no token, then it is loaded from metadata and ref->method is set to the method instance.
485 * If TARGET is non-NULL, abort decoding if it can be determined that the decoded method couldn't resolve to TARGET, and return FALSE.
488 decode_method_ref_with_target (MonoAotModule *module, MethodRef *ref, MonoMethod *target, guint8 *buf, guint8 **endbuf)
490 guint32 image_index, value;
491 MonoImage *image = NULL;
494 memset (ref, 0, sizeof (MethodRef));
496 value = decode_value (p, &p);
497 image_index = value >> 24;
499 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
500 ref->no_aot_trampoline = TRUE;
501 value = decode_value (p, &p);
502 image_index = value >> 24;
505 if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
506 if (target && target->wrapper_type)
510 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
511 guint32 wrapper_type;
513 wrapper_type = decode_value (p, &p);
515 if (target && target->wrapper_type != wrapper_type)
519 image = mono_defaults.corlib;
521 switch (wrapper_type) {
522 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
523 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
527 mono_class_init (m->klass);
528 ref->method = mono_marshal_get_remoting_invoke_with_check (m);
531 case MONO_WRAPPER_PROXY_ISINST: {
532 MonoClass *klass = decode_klass_ref (module, p, &p);
535 ref->method = mono_marshal_get_proxy_cancast (klass);
538 case MONO_WRAPPER_LDFLD:
539 case MONO_WRAPPER_LDFLDA:
540 case MONO_WRAPPER_STFLD:
541 case MONO_WRAPPER_ISINST: {
542 MonoClass *klass = decode_klass_ref (module, p, &p);
545 if (wrapper_type == MONO_WRAPPER_LDFLD)
546 ref->method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
547 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
548 ref->method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
549 else if (wrapper_type == MONO_WRAPPER_STFLD)
550 ref->method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
551 else if (wrapper_type == MONO_WRAPPER_ISINST)
552 ref->method = mono_marshal_get_isinst (klass);
554 g_assert_not_reached ();
557 case MONO_WRAPPER_LDFLD_REMOTE:
558 ref->method = mono_marshal_get_ldfld_remote_wrapper (NULL);
560 case MONO_WRAPPER_STFLD_REMOTE:
561 ref->method = mono_marshal_get_stfld_remote_wrapper (NULL);
563 case MONO_WRAPPER_ALLOC: {
564 int atype = decode_value (p, &p);
566 ref->method = mono_gc_get_managed_allocator_by_type (atype);
569 case MONO_WRAPPER_WRITE_BARRIER:
570 ref->method = mono_gc_get_write_barrier ();
572 case MONO_WRAPPER_STELEMREF:
573 ref->method = mono_marshal_get_stelemref ();
575 case MONO_WRAPPER_SYNCHRONIZED: {
576 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
580 ref->method = mono_marshal_get_synchronized_wrapper (m);
583 case MONO_WRAPPER_UNKNOWN: {
584 MonoMethodDesc *desc;
585 MonoMethod *orig_method;
586 int subtype = decode_value (p, &p);
588 if (subtype == MONO_AOT_WRAPPER_PTR_TO_STRUCTURE || subtype == MONO_AOT_WRAPPER_STRUCTURE_TO_PTR) {
589 MonoClass *klass = decode_klass_ref (module, p, &p);
595 if (klass != target->klass)
598 if (subtype == MONO_AOT_WRAPPER_PTR_TO_STRUCTURE) {
599 if (strcmp (target->name, "PtrToStructure"))
601 ref->method = mono_marshal_get_ptr_to_struct (klass);
603 if (strcmp (target->name, "StructureToPtr"))
605 ref->method = mono_marshal_get_struct_to_ptr (klass);
608 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
609 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
610 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
611 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
613 g_assert_not_reached ();
614 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
615 g_assert (orig_method);
616 mono_method_desc_free (desc);
617 ref->method = mono_monitor_get_fast_path (orig_method);
621 case MONO_WRAPPER_RUNTIME_INVOKE: {
623 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
627 ref->method = mono_marshal_get_runtime_invoke (m, FALSE);
630 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
631 int subtype = decode_value (p, &p);
633 if (subtype == MONO_AOT_WRAPPER_ELEMENT_ADDR) {
634 int rank = decode_value (p, &p);
635 int elem_size = decode_value (p, &p);
637 ref->method = mono_marshal_get_array_address (rank, elem_size);
639 g_assert_not_reached ();
643 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
644 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
649 /* This should only happen when looking for an extra method */
651 if (mono_marshal_method_from_wrapper (target) == m)
652 ref->method = target;
658 g_assert_not_reached ();
660 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
663 /* Can't decode these */
664 g_assert_not_reached ();
665 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
666 image_index = decode_value (p, &p);
667 ref->token = decode_value (p, &p);
669 image = load_image (module, image_index, TRUE);
672 } else if (image_index == MONO_AOT_METHODREF_GINST) {
674 MonoGenericContext ctx;
677 * These methods do not have a token which resolves them, so we
678 * resolve them immediately.
680 klass = decode_klass_ref (module, p, &p);
684 if (target && target->klass != klass)
687 image_index = decode_value (p, &p);
688 ref->token = decode_value (p, &p);
690 image = load_image (module, image_index, TRUE);
694 ref->method = mono_get_method_full (image, ref->token, NULL, NULL);
698 memset (&ctx, 0, sizeof (ctx));
700 if (FALSE && klass->generic_class) {
701 ctx.class_inst = klass->generic_class->context.class_inst;
702 ctx.method_inst = NULL;
704 ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
707 memset (&ctx, 0, sizeof (ctx));
709 if (!decode_generic_context (module, &ctx, p, &p))
712 ref->method = mono_class_inflate_generic_method_full (ref->method, klass, &ctx);
713 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
717 klass = decode_klass_ref (module, p, &p);
720 method_type = decode_value (p, &p);
721 switch (method_type) {
723 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
726 ref->method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
729 ref->method = mono_class_get_method_from_name (klass, "Get", -1);
732 ref->method = mono_class_get_method_from_name (klass, "Address", -1);
735 ref->method = mono_class_get_method_from_name (klass, "Set", -1);
738 g_assert_not_reached ();
741 g_assert (image_index < MONO_AOT_METHODREF_MIN);
742 ref->token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
744 image = load_image (module, image_index, TRUE);
757 decode_method_ref (MonoAotModule *module, MethodRef *ref, guint8 *buf, guint8 **endbuf)
759 return decode_method_ref_with_target (module, ref, NULL, buf, endbuf);
763 * decode_resolve_method_ref_with_target:
765 * Similar to decode_method_ref, but resolve and return the method itself.
768 decode_resolve_method_ref_with_target (MonoAotModule *module, MonoMethod *target, guint8 *buf, guint8 **endbuf)
773 res = decode_method_ref_with_target (module, &ref, target, buf, endbuf);
780 return mono_get_method (ref.image, ref.token, NULL);
784 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
786 return decode_resolve_method_ref_with_target (module, NULL, buf, endbuf);
790 create_cache_structure (void)
796 home = g_get_home_dir ();
800 tmp = g_build_filename (home, ".mono", NULL);
801 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
802 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
806 err = mkdir (tmp, 0777);
809 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
815 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
816 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
817 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
821 err = mkdir (tmp, 0777);
824 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
833 * load_aot_module_from_cache:
835 * Experimental code to AOT compile loaded assemblies on demand.
838 * - Add environment variable MONO_AOT_CACHE_OPTIONS
839 * - Add options for controlling the cache size
840 * - Handle full cache by deleting old assemblies lru style
841 * - Add options for excluding assemblies during development
842 * - Maybe add a threshold after an assembly is AOT compiled
843 * - invoking a new mono process is a security risk
844 * - recompile the AOT module if one of its dependencies changes
847 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
849 char *fname, *cmd, *tmp2, *aot_options;
858 if (assembly->image->dynamic)
861 create_cache_structure ();
863 home = g_get_home_dir ();
865 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
866 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
870 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
871 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
874 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
876 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
878 aot_options = g_strdup_printf ("outfile=%s", fname);
880 if (spawn_compiler) {
881 /* FIXME: security */
882 /* FIXME: Has to pass the assembly loading path to the child process */
883 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
885 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
887 #if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
889 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
890 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
892 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
899 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
901 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
903 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
907 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
909 g_free (aot_options);
916 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
920 guint16 *table, *entry;
924 /* The first entry points to the hash */
928 table_size = table [0];
931 hash = mono_metadata_str_hash (name) % table_size;
933 entry = &table [hash * 2];
935 /* Search the hash for the index into the globals table */
937 while (entry [0] != 0) {
938 guint32 index = entry [0] - 1;
939 guint32 next = entry [1];
941 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
943 if (!strcmp (globals [index * 2], name)) {
944 global_index = index;
949 entry = &table [next * 2];
955 if (global_index != -1)
956 *value = globals [global_index * 2 + 1];
960 char *err = mono_dl_symbol (module, name, value);
968 check_usable (MonoAssembly *assembly, MonoAotFileInfo *info, char **out_msg)
972 gboolean usable = TRUE;
976 if (strcmp (assembly->image->guid, info->assembly_guid)) {
977 msg = g_strdup_printf ("doesn't match assembly");
981 build_info = mono_get_runtime_build_info ();
982 if (strlen (info->runtime_version) > 0 && strcmp (info->runtime_version, build_info)) {
983 msg = g_strdup_printf ("compiled against runtime version '%s' while this runtime has version '%s'", info->runtime_version, build_info);
988 full_aot = info->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
990 if (mono_aot_only && !full_aot) {
991 msg = g_strdup_printf ("not compiled with --aot=full");
994 if (!mono_aot_only && full_aot) {
995 msg = g_strdup_printf ("compiled with --aot=full");
999 /* mono_arch_find_imt_method () requires this */
1000 if ((info->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1001 msg = g_strdup_printf ("compiled against LLVM");
1005 if (mini_get_debug_options ()->mdb_optimizations && !(info->flags & MONO_AOT_FILE_FLAG_DEBUG) && !full_aot) {
1006 msg = g_strdup_printf ("not compiled for debugging");
1012 if (info->gc_name_index != -1) {
1013 char *gc_name = (char*)&blob [info->gc_name_index];
1014 const char *current_gc_name = mono_gc_get_gc_name ();
1016 if (strcmp (current_gc_name, gc_name) != 0) {
1017 msg = g_strdup_printf ("compiled against GC %s, while the current runtime uses GC %s.\n", gc_name, current_gc_name);
1027 load_aot_module (MonoAssembly *assembly, gpointer user_data)
1030 MonoAotModule *amodule;
1032 gboolean usable = TRUE;
1033 char *version_symbol = NULL;
1036 MonoAotFileInfo *info = NULL;
1039 gboolean do_load_image = TRUE;
1041 if (mono_compile_aot)
1044 if (assembly->image->aot_module)
1046 * Already loaded. This can happen because the assembly loading code might invoke
1047 * the assembly load hooks multiple times for the same assembly.
1051 if (assembly->image->dynamic)
1054 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
1058 if (static_aot_modules)
1059 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
1065 /* Statically linked AOT module */
1067 aot_name = g_strdup_printf ("%s", assembly->aname.name);
1068 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
1071 sofile = load_aot_module_from_cache (assembly, &aot_name);
1074 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
1076 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1079 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
1085 if (!sofile && !globals) {
1086 if (mono_aot_only) {
1087 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1094 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &version_symbol);
1095 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&info);
1097 if (version_symbol) {
1098 /* Old file format */
1099 version = atoi (version_symbol);
1102 version = info->version;
1105 if (version != MONO_AOT_FILE_VERSION) {
1106 msg = g_strdup_printf ("wrong file format version (expected %d got %d)", MONO_AOT_FILE_VERSION, version);
1109 usable = check_usable (assembly, info, &msg);
1113 if (mono_aot_only) {
1114 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode: %s.\n", aot_name, msg);
1117 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable: %s.\n", aot_name, msg);
1122 mono_dl_close (sofile);
1123 assembly->image->aot_module = NULL;
1129 amodule = g_new0 (MonoAotModule, 1);
1130 amodule->aot_name = aot_name;
1131 amodule->assembly = assembly;
1133 memcpy (&amodule->info, info, sizeof (*info));
1135 amodule->got = amodule->info.got;
1136 amodule->got [0] = assembly->image;
1137 amodule->globals = globals;
1138 amodule->sofile = sofile;
1139 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1140 amodule->blob = blob;
1142 /* Read image table */
1144 guint32 table_len, i;
1147 table = info->image_table;
1150 table_len = *(guint32*)table;
1151 table += sizeof (guint32);
1152 amodule->image_table = g_new0 (MonoImage*, table_len);
1153 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1154 amodule->image_guids = g_new0 (char*, table_len);
1155 amodule->image_table_len = table_len;
1156 for (i = 0; i < table_len; ++i) {
1157 MonoAssemblyName *aname = &(amodule->image_names [i]);
1159 aname->name = g_strdup (table);
1160 table += strlen (table) + 1;
1161 amodule->image_guids [i] = g_strdup (table);
1162 table += strlen (table) + 1;
1164 aname->culture = g_strdup (table);
1165 table += strlen (table) + 1;
1166 memcpy (aname->public_key_token, table, strlen (table) + 1);
1167 table += strlen (table) + 1;
1169 table = ALIGN_PTR_TO (table, 8);
1170 aname->flags = *(guint32*)table;
1172 aname->major = *(guint32*)table;
1174 aname->minor = *(guint32*)table;
1176 aname->build = *(guint32*)table;
1178 aname->revision = *(guint32*)table;
1183 amodule->code_offsets = info->code_offsets;
1184 amodule->code = info->methods;
1186 /* Mask out thumb interop bit */
1187 amodule->code = (void*)((mgreg_t)amodule->code & ~1);
1189 amodule->code_end = info->methods_end;
1190 amodule->method_info_offsets = info->method_info_offsets;
1191 amodule->ex_info_offsets = info->ex_info_offsets;
1192 amodule->class_info_offsets = info->class_info_offsets;
1193 amodule->class_name_table = info->class_name_table;
1194 amodule->extra_method_table = info->extra_method_table;
1195 amodule->extra_method_info_offsets = info->extra_method_info_offsets;
1196 amodule->got_info_offsets = info->got_info_offsets;
1197 amodule->unwind_info = info->unwind_info;
1198 amodule->mem_end = info->mem_end;
1199 amodule->mem_begin = amodule->code;
1200 amodule->plt = info->plt;
1201 amodule->plt_end = info->plt_end;
1202 amodule->mono_eh_frame = info->mono_eh_frame;
1203 amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC] = info->specific_trampolines;
1204 amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = info->static_rgctx_trampolines;
1205 amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK] = info->imt_thunks;
1206 amodule->thumb_end = info->thumb_end;
1208 if (make_unreadable) {
1209 #ifndef TARGET_WIN32
1211 guint8 *page_start, *page_end;
1214 addr = amodule->mem_begin;
1215 len = amodule->mem_end - amodule->mem_begin;
1217 /* Round down in both directions to avoid modifying data which is not ours */
1218 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1219 page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
1220 if (page_end > page_start) {
1221 err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
1222 g_assert (err == 0);
1229 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1230 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1232 g_hash_table_insert (aot_modules, assembly, amodule);
1235 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1237 assembly->image->aot_module = amodule;
1239 if (mono_aot_only) {
1240 if (mono_defaults.corlib) {
1241 /* The second got slot contains the mscorlib got addr */
1242 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1244 amodule->got [1] = mscorlib_amodule->got;
1246 amodule->got [1] = amodule->got;
1250 if (mono_gc_is_moving ()) {
1253 memset (&ji, 0, sizeof (ji));
1254 ji.type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
1256 amodule->got [2] = mono_resolve_patch_target (NULL, mono_get_root_domain (), NULL, &ji, FALSE);
1260 * Since we store methoddef and classdef tokens when referring to methods/classes in
1261 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1262 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1263 * non-lazily, since we can't handle out-of-date errors later.
1264 * The cached class info also depends on the exact assemblies.
1266 #if defined(__native_client__)
1267 /* TODO: Don't 'load_image' on mscorlib due to a */
1268 /* recursive loading problem. This should be */
1269 /* removed if mscorlib is loaded from disk. */
1270 if (strncmp(assembly->aname.name, "mscorlib", 8)) {
1271 do_load_image = TRUE;
1273 do_load_image = FALSE;
1276 if (do_load_image) {
1277 for (i = 0; i < amodule->image_table_len; ++i)
1278 load_image (amodule, i, FALSE);
1281 if (amodule->out_of_date) {
1282 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);
1283 if (mono_aot_only) {
1284 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);
1289 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1293 * mono_aot_register_globals:
1295 * This is called by the ctor function in AOT images compiled with the
1296 * 'no-dlsym' option.
1299 mono_aot_register_globals (gpointer *globals)
1301 g_assert_not_reached ();
1305 * mono_aot_register_module:
1307 * This should be called by embedding code to register AOT modules statically linked
1308 * into the executable. AOT_INFO should be the value of the
1309 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1312 mono_aot_register_module (gpointer *aot_info)
1320 /* Determine the assembly name */
1321 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1324 /* This could be called before startup */
1328 if (!static_aot_modules)
1329 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1331 g_hash_table_insert (static_aot_modules, aname, globals);
1338 mono_aot_init (void)
1340 InitializeCriticalSection (&aot_mutex);
1341 aot_modules = g_hash_table_new (NULL, NULL);
1343 mono_install_assembly_load_hook (load_aot_module, NULL);
1345 if (g_getenv ("MONO_LASTAOT"))
1346 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1347 if (g_getenv ("MONO_AOT_CACHE"))
1348 use_aot_cache = TRUE;
1352 mono_aot_cleanup (void)
1354 if (aot_jit_icall_hash)
1355 g_hash_table_destroy (aot_jit_icall_hash);
1357 g_hash_table_destroy (aot_modules);
1361 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1367 info->vtable_size = decode_value (buf, &buf);
1368 if (info->vtable_size == -1)
1371 flags = decode_value (buf, &buf);
1372 info->ghcimpl = (flags >> 0) & 0x1;
1373 info->has_finalize = (flags >> 1) & 0x1;
1374 info->has_cctor = (flags >> 2) & 0x1;
1375 info->has_nested_classes = (flags >> 3) & 0x1;
1376 info->blittable = (flags >> 4) & 0x1;
1377 info->has_references = (flags >> 5) & 0x1;
1378 info->has_static_refs = (flags >> 6) & 0x1;
1379 info->no_special_static_fields = (flags >> 7) & 0x1;
1380 info->is_generic_container = (flags >> 8) & 0x1;
1382 if (info->has_cctor) {
1383 res = decode_method_ref (module, &ref, buf, &buf);
1386 info->cctor_token = ref.token;
1388 if (info->has_finalize) {
1389 res = decode_method_ref (module, &ref, buf, &buf);
1392 info->finalize_image = ref.image;
1393 info->finalize_token = ref.token;
1396 info->instance_size = decode_value (buf, &buf);
1397 info->class_size = decode_value (buf, &buf);
1398 info->packing_size = decode_value (buf, &buf);
1399 info->min_align = decode_value (buf, &buf);
1407 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1410 MonoClass *klass = vtable->klass;
1411 MonoAotModule *amodule = klass->image->aot_module;
1413 MonoCachedClassInfo class_info;
1418 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !amodule)
1421 info = &amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1424 err = decode_cached_class_info (amodule, &class_info, p, &p);
1428 for (i = 0; i < slot; ++i)
1429 decode_method_ref (amodule, &ref, p, &p);
1431 res = decode_method_ref (amodule, &ref, p, &p);
1434 if (ref.no_aot_trampoline)
1437 if (mono_metadata_token_index (ref.token) == 0)
1440 return mono_aot_get_method_from_token (domain, ref.image, ref.token);
1444 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1446 MonoAotModule *amodule = klass->image->aot_module;
1450 if (klass->rank || !amodule)
1453 p = (guint8*)&amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1455 err = decode_cached_class_info (amodule, res, p, &p);
1463 * mono_aot_get_class_from_name:
1465 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1466 * using a cache stored in the AOT file.
1467 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1469 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1473 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1475 MonoAotModule *amodule = image->aot_module;
1476 guint16 *table, *entry;
1479 char full_name_buf [1024];
1481 const char *name2, *name_space2;
1483 guint32 cols [MONO_TYPEDEF_SIZE];
1484 GHashTable *nspace_table;
1486 if (!amodule || !amodule->class_name_table)
1493 /* First look in the cache */
1494 if (!amodule->name_cache)
1495 amodule->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1496 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
1498 *klass = g_hash_table_lookup (nspace_table, name);
1505 table_size = amodule->class_name_table [0];
1506 table = amodule->class_name_table + 1;
1508 if (name_space [0] == '\0')
1509 full_name = g_strdup_printf ("%s", name);
1511 if (strlen (name_space) + strlen (name) < 1000) {
1512 sprintf (full_name_buf, "%s.%s", name_space, name);
1513 full_name = full_name_buf;
1515 full_name = g_strdup_printf ("%s.%s", name_space, name);
1518 hash = mono_metadata_str_hash (full_name) % table_size;
1519 if (full_name != full_name_buf)
1522 entry = &table [hash * 2];
1524 if (entry [0] != 0) {
1525 t = &image->tables [MONO_TABLE_TYPEDEF];
1528 guint32 index = entry [0];
1529 guint32 next = entry [1];
1530 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1532 name_table_accesses ++;
1534 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1536 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1537 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1539 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1541 *klass = mono_class_get (image, token);
1546 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
1547 if (!nspace_table) {
1548 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1549 g_hash_table_insert (amodule->name_cache, (char*)name_space2, nspace_table);
1551 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1558 entry = &table [next * 2];
1571 * decode_mono_eh_frame:
1573 * Decode the EH information emitted by our modified LLVM compiler and construct a
1574 * MonoJitInfo structure from it.
1575 * LOCKING: Acquires the domain lock.
1578 decode_llvm_mono_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
1579 MonoMethod *method, guint8 *code,
1580 MonoJitExceptionInfo *clauses, int num_clauses,
1581 int extra_size, GSList **nesting,
1582 int *this_reg, int *this_offset)
1585 guint8 *fde, *cie, *code_start, *code_end;
1586 int version, fde_count;
1588 int i, j, pos, left, right, offset, offset1, offset2, code_len;
1589 MonoJitExceptionInfo *ei;
1590 guint32 fde_len, ei_len, nested_len, nindex;
1591 gpointer *type_info;
1593 MonoLLVMFDEInfo info;
1595 g_assert (amodule->mono_eh_frame);
1597 p = amodule->mono_eh_frame;
1599 /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
1603 g_assert (version == 1);
1605 p = ALIGN_PTR_TO (p, 4);
1607 fde_count = *(guint32*)p;
1611 /* There is +1 entry in the table */
1612 cie = p + ((fde_count + 1) * 8);
1614 /* Binary search in the table to find the entry for code */
1615 offset = code - amodule->mono_eh_frame;
1620 pos = (left + right) / 2;
1622 offset1 = table [(pos * 2)];
1623 if (pos + 1 == fde_count)
1625 offset2 = amodule->code_end - amodule->code;
1627 offset2 = table [(pos + 1) * 2];
1629 if (offset < offset1)
1631 else if (offset >= offset2)
1637 code_start = amodule->mono_eh_frame + table [(pos * 2)];
1638 /* This won't overflow because there is +1 entry in the table */
1639 code_end = amodule->mono_eh_frame + table [(pos * 2) + 2];
1640 code_len = code_end - code_start;
1642 g_assert (code >= code_start && code < code_end);
1644 fde = amodule->mono_eh_frame + table [(pos * 2) + 1];
1645 /* This won't overflow because there is +1 entry in the table */
1646 fde_len = table [(pos * 2) + 2 + 1] - table [(pos * 2) + 1];
1648 mono_unwind_decode_llvm_mono_fde (fde, fde_len, cie, code_start, &info);
1650 ei_len = info.ex_info_len;
1651 type_info = info.type_info;
1652 *this_reg = info.this_reg;
1653 *this_offset = info.this_offset;
1655 /* Count number of nested clauses */
1657 for (i = 0; i < ei_len; ++i) {
1658 /* This might be unaligned */
1659 gint32 cindex1 = read32 (type_info [i]);
1662 for (l = nesting [cindex1]; l; l = l->next) {
1663 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
1665 for (j = 0; j < ei_len; ++j) {
1666 gint32 cindex2 = read32 (type_info [j]);
1668 if (cindex2 == nesting_cindex)
1675 * LLVM might represent one IL region with multiple regions, so have to
1676 * allocate a new JI.
1679 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * (ei_len + nested_len)) + extra_size);
1681 jinfo->code_size = code_len;
1682 jinfo->used_regs = mono_cache_unwind_info (info.unw_info, info.unw_info_len);
1683 jinfo->method = method;
1684 jinfo->code_start = code;
1685 jinfo->domain_neutral = 0;
1686 /* This signals that used_regs points to a normal cached unwind info */
1687 jinfo->from_aot = 0;
1688 jinfo->num_clauses = ei_len + nested_len;
1690 for (i = 0; i < ei_len; ++i) {
1692 * orig_jinfo contains the original IL exception info saved by the AOT
1693 * compiler, we have to combine that with the information produced by LLVM
1695 /* The type_info entries contain IL clause indexes */
1696 int clause_index = read32 (type_info [i]);
1697 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
1698 MonoJitExceptionInfo *orig_jei = &clauses [clause_index];
1700 g_assert (clause_index < num_clauses);
1701 jei->flags = orig_jei->flags;
1702 jei->data.catch_class = orig_jei->data.catch_class;
1704 jei->try_start = ei [i].try_start;
1705 jei->try_end = ei [i].try_end;
1706 jei->handler_start = ei [i].handler_start;
1708 /* Make sure we transition to thumb when a handler starts */
1709 if (amodule->thumb_end && (guint8*)jei->handler_start < amodule->thumb_end)
1710 jei->handler_start = (void*)((mgreg_t)jei->handler_start + 1);
1713 /* See exception_cb () in mini-llvm.c as to why this is needed */
1715 for (i = 0; i < ei_len; ++i) {
1716 gint32 cindex1 = read32 (type_info [i]);
1719 for (l = nesting [cindex1]; l; l = l->next) {
1720 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
1722 for (j = 0; j < ei_len; ++j) {
1723 gint32 cindex2 = read32 (type_info [j]);
1725 if (cindex2 == nesting_cindex) {
1727 * The try interval comes from the nested clause, everything else from the
1730 memcpy (&jinfo->clauses [nindex], &jinfo->clauses [j], sizeof (MonoJitExceptionInfo));
1731 jinfo->clauses [nindex].try_start = jinfo->clauses [i].try_start;
1732 jinfo->clauses [nindex].try_end = jinfo->clauses [i].try_end;
1738 g_assert (nindex == ei_len + nested_len);
1744 * LOCKING: Acquires the domain lock.
1747 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
1748 MonoMethod *method, guint8* ex_info, guint8 *addr,
1749 guint8 *code, guint32 code_len)
1751 int i, buf_len, num_clauses;
1753 guint used_int_regs, flags;
1754 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points, has_try_block_holes;
1755 gboolean from_llvm, has_gc_map;
1757 int generic_info_size, try_holes_info_size, num_holes, this_reg = 0, this_offset = 0;
1759 /* Load the method info from the AOT file */
1762 flags = decode_value (p, &p);
1763 has_generic_jit_info = (flags & 1) != 0;
1764 has_dwarf_unwind_info = (flags & 2) != 0;
1765 has_clauses = (flags & 4) != 0;
1766 has_seq_points = (flags & 8) != 0;
1767 from_llvm = (flags & 16) != 0;
1768 has_try_block_holes = (flags & 32) != 0;
1769 has_gc_map = (flags & 64) != 0;
1771 if (has_dwarf_unwind_info) {
1774 offset = decode_value (p, &p);
1775 g_assert (offset < (1 << 30));
1776 used_int_regs = offset;
1778 used_int_regs = decode_value (p, &p);
1780 if (has_generic_jit_info)
1781 generic_info_size = sizeof (MonoGenericJitInfo);
1783 generic_info_size = 0;
1785 if (has_try_block_holes) {
1786 num_holes = decode_value (p, &p);
1787 try_holes_info_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
1789 num_holes = try_holes_info_size = 0;
1791 /* Exception table */
1793 num_clauses = decode_value (p, &p);
1798 MonoJitExceptionInfo *clauses;
1802 * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
1805 clauses = g_new0 (MonoJitExceptionInfo, num_clauses);
1806 nesting = g_new0 (GSList*, num_clauses);
1808 for (i = 0; i < num_clauses; ++i) {
1809 MonoJitExceptionInfo *ei = &clauses [i];
1811 ei->flags = decode_value (p, &p);
1813 if (decode_value (p, &p))
1814 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1816 /* Read the list of nesting clauses */
1818 int nesting_index = decode_value (p, &p);
1819 if (nesting_index == -1)
1821 nesting [i] = g_slist_prepend (nesting [i], GINT_TO_POINTER (nesting_index));
1825 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);
1826 jinfo->from_llvm = 1;
1829 for (i = 0; i < num_clauses; ++i)
1830 g_slist_free (nesting [i]);
1834 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size + try_holes_info_size);
1835 jinfo->num_clauses = num_clauses;
1837 for (i = 0; i < jinfo->num_clauses; ++i) {
1838 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1840 ei->flags = decode_value (p, &p);
1842 ei->exvar_offset = decode_value (p, &p);
1844 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
1845 ei->data.filter = code + decode_value (p, &p);
1847 if (decode_value (p, &p))
1848 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1851 ei->try_start = code + decode_value (p, &p);
1852 ei->try_end = code + decode_value (p, &p);
1853 ei->handler_start = code + decode_value (p, &p);
1856 jinfo->code_size = code_len;
1857 jinfo->used_regs = used_int_regs;
1858 jinfo->method = method;
1859 jinfo->code_start = code;
1860 jinfo->domain_neutral = 0;
1861 jinfo->from_aot = 1;
1864 if (has_generic_jit_info) {
1865 MonoGenericJitInfo *gi;
1867 jinfo->has_generic_jit_info = 1;
1869 gi = mono_jit_info_get_generic_jit_info (jinfo);
1873 gi->has_this = this_reg != -1;
1874 gi->this_reg = this_reg;
1875 gi->this_offset = this_offset;
1877 gi->has_this = decode_value (p, &p);
1878 gi->this_reg = decode_value (p, &p);
1879 gi->this_offset = decode_value (p, &p);
1882 /* This currently contains no data */
1883 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1885 jinfo->method = decode_resolve_method_ref (amodule, p, &p);
1888 if (has_try_block_holes) {
1889 MonoTryBlockHoleTableJitInfo *table;
1891 jinfo->has_try_block_holes = 1;
1893 table = mono_jit_info_get_try_block_hole_table_info (jinfo);
1896 table->num_holes = (guint16)num_holes;
1897 for (i = 0; i < num_holes; ++i) {
1898 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
1899 hole->clause = decode_value (p, &p);
1900 hole->length = decode_value (p, &p);
1901 hole->offset = decode_value (p, &p);
1905 if (has_seq_points) {
1906 MonoSeqPointInfo *seq_points;
1907 int il_offset, native_offset, last_il_offset, last_native_offset, j;
1909 int len = decode_value (p, &p);
1911 seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
1912 seq_points->len = len;
1913 last_il_offset = last_native_offset = 0;
1914 for (i = 0; i < len; ++i) {
1915 SeqPoint *sp = &seq_points->seq_points [i];
1916 il_offset = last_il_offset + decode_value (p, &p);
1917 native_offset = last_native_offset + decode_value (p, &p);
1919 sp->il_offset = il_offset;
1920 sp->native_offset = native_offset;
1922 sp->next_len = decode_value (p, &p);
1923 sp->next = g_new (int, sp->next_len);
1924 for (j = 0; j < sp->next_len; ++j)
1925 sp->next [j] = decode_value (p, &p);
1927 last_il_offset = il_offset;
1928 last_native_offset = native_offset;
1931 mono_domain_lock (domain);
1932 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
1933 mono_domain_unlock (domain);
1936 /* Load debug info */
1937 buf_len = decode_value (p, &p);
1938 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1942 int map_size = decode_value (p, &p);
1943 /* The GC map requires 4 bytes of alignment */
1944 while ((guint64)(gsize)p % 4)
1950 if (amodule != jinfo->method->klass->image->aot_module) {
1953 ji_to_amodule = g_hash_table_new (NULL, NULL);
1954 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
1962 * mono_aot_get_unwind_info:
1964 * Return a pointer to the DWARF unwind info belonging to JI.
1967 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
1969 MonoAotModule *amodule = ji->method->klass->image->aot_module;
1971 guint8 *code = ji->code_start;
1974 g_assert (ji->from_aot);
1976 if (!(code >= amodule->code && code <= amodule->code_end)) {
1977 /* ji belongs to a different aot module than amodule */
1979 g_assert (ji_to_amodule);
1980 amodule = g_hash_table_lookup (ji_to_amodule, ji);
1982 g_assert (code >= amodule->code && code <= amodule->code_end);
1986 p = amodule->unwind_info + ji->used_regs;
1987 *unwind_info_len = decode_value (p, &p);
1991 static G_GNUC_UNUSED int
1992 compare_ints (const void *a, const void *b)
1994 return *(gint32*)a - *(gint32*)b;
1998 msort_code_offsets_internal (gint32 *array, int lo, int hi, gint32 *scratch)
2000 int mid = (lo + hi) / 2;
2007 for (i = lo; i < hi; ++i)
2008 if (array [(i * 2)] > array [(i * 2) + 2])
2011 /* Already sorted */
2015 msort_code_offsets_internal (array, lo, mid, scratch);
2016 msort_code_offsets_internal (array, mid + 1, hi, scratch);
2018 if (array [mid * 2] < array [(mid + 1) * 2])
2024 for (i = lo; i <= hi; i ++) {
2025 if (t_lo <= mid && ((t_hi > hi) || array [t_lo * 2] < array [t_hi * 2])) {
2026 scratch [(i * 2)] = array [t_lo * 2];
2027 scratch [(i * 2) + 1] = array [(t_lo *2) + 1];
2030 scratch [(i * 2)] = array [t_hi * 2];
2031 scratch [(i * 2) + 1] = array [(t_hi *2) + 1];
2035 for (i = lo; i <= hi; ++i) {
2036 array [(i * 2)] = scratch [i * 2];
2037 array [(i * 2) + 1] = scratch [(i * 2) + 1];
2042 msort_code_offsets (gint32 *array, int len)
2046 scratch = g_new (gint32, len * 2);
2047 msort_code_offsets_internal (array, 0, len - 1, scratch);
2052 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2054 int pos, left, right, offset, offset1, offset2, code_len;
2055 int method_index, table_len, is_wrapper;
2057 MonoAotModule *amodule = image->aot_module;
2060 guint8 *code, *ex_info, *p;
2062 int nmethods = amodule->info.nmethods;
2063 gint32 *code_offsets;
2069 if (domain != mono_get_root_domain ())
2073 offset = (guint8*)addr - amodule->code;
2075 /* Compute a sorted table mapping code offsets to method indexes. */
2076 if (!amodule->sorted_code_offsets) {
2077 code_offsets = g_new0 (gint32, nmethods * 2);
2079 for (i = 0; i < nmethods; ++i) {
2080 /* Skip the -1 entries to speed up sorting */
2081 if (amodule->code_offsets [i] == 0xffffffff)
2083 code_offsets [(offsets_len * 2)] = amodule->code_offsets [i];
2084 code_offsets [(offsets_len *2) + 1] = i;
2087 /* Use a merge sort as this is mostly sorted */
2088 msort_code_offsets (code_offsets, offsets_len);
2089 //qsort (code_offsets, offsets_len, sizeof (gint32) * 2, compare_ints);
2090 for (i = 0; i < offsets_len -1; ++i)
2091 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
2093 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
2094 /* Somebody got in before us */
2095 g_free (code_offsets);
2096 amodule->sorted_code_offsets_len = offsets_len;
2099 code_offsets = amodule->sorted_code_offsets;
2100 offsets_len = amodule->sorted_code_offsets_len;
2102 /* Binary search in the sorted_code_offsets table */
2104 right = offsets_len;
2106 pos = (left + right) / 2;
2108 offset1 = code_offsets [(pos * 2)];
2109 if (pos + 1 == offsets_len)
2110 offset2 = amodule->code_end - amodule->code;
2112 offset2 = code_offsets [(pos + 1) * 2];
2114 if (offset < offset1)
2116 else if (offset >= offset2)
2122 g_assert (offset >= code_offsets [(pos * 2)]);
2123 if (pos + 1 < offsets_len)
2124 g_assert (offset < code_offsets [((pos + 1) * 2)]);
2125 method_index = code_offsets [(pos * 2) + 1];
2127 code = &amodule->code [amodule->code_offsets [method_index]];
2128 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
2130 if (pos == offsets_len - 1)
2131 code_len = amodule->code_end - code;
2133 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
2135 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
2137 /* Might be a wrapper/extra method */
2138 if (amodule->extra_methods) {
2140 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
2147 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
2149 * This is hit for extra methods which are called directly, so they are
2150 * not in amodule->extra_methods.
2152 table_len = amodule->extra_method_info_offsets [0];
2153 table = amodule->extra_method_info_offsets + 1;
2160 pos = ((left + right) / 2);
2162 g_assert (pos < table_len);
2164 if (table [pos * 2] < method_index)
2166 else if (table [pos * 2] > method_index)
2172 p = amodule->blob + table [(pos * 2) + 1];
2173 is_wrapper = decode_value (p, &p);
2174 g_assert (!is_wrapper);
2175 method = decode_resolve_method_ref (amodule, p, &p);
2178 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
2179 method = mono_get_method (image, token, NULL);
2186 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
2188 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
2190 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
2191 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
2193 /* Add it to the normal JitInfo tables */
2194 mono_jit_info_table_add (domain, jinfo);
2200 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
2208 case MONO_PATCH_INFO_METHOD:
2209 case MONO_PATCH_INFO_METHOD_JUMP:
2210 case MONO_PATCH_INFO_ICALL_ADDR:
2211 case MONO_PATCH_INFO_METHOD_RGCTX: {
2215 res = decode_method_ref (aot_module, &ref, p, &p);
2219 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)) {
2220 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (ref.image, ref.token));
2221 ji->type = MONO_PATCH_INFO_ABS;
2225 ji->data.method = ref.method;
2227 ji->data.method = mono_get_method (ref.image, ref.token, NULL);
2228 g_assert (ji->data.method);
2229 mono_class_init (ji->data.method->klass);
2233 case MONO_PATCH_INFO_INTERNAL_METHOD:
2234 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2235 guint32 len = decode_value (p, &p);
2237 ji->data.name = (char*)p;
2241 case MONO_PATCH_INFO_METHODCONST:
2243 ji->data.method = decode_resolve_method_ref (aot_module, p, &p);
2244 if (!ji->data.method)
2247 case MONO_PATCH_INFO_VTABLE:
2248 case MONO_PATCH_INFO_CLASS:
2249 case MONO_PATCH_INFO_IID:
2250 case MONO_PATCH_INFO_ADJUSTED_IID:
2252 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2253 if (!ji->data.klass)
2256 case MONO_PATCH_INFO_CLASS_INIT:
2257 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2258 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2259 if (!ji->data.klass)
2262 case MONO_PATCH_INFO_IMAGE:
2263 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
2264 if (!ji->data.image)
2267 case MONO_PATCH_INFO_FIELD:
2268 case MONO_PATCH_INFO_SFLDA:
2270 ji->data.field = decode_field_info (aot_module, p, &p);
2271 if (!ji->data.field)
2274 case MONO_PATCH_INFO_SWITCH:
2275 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
2276 ji->data.table->table_size = decode_value (p, &p);
2277 table = mono_domain_alloc (mono_domain_get (), sizeof (gpointer) * ji->data.table->table_size);
2278 ji->data.table->table = (MonoBasicBlock**)table;
2279 for (i = 0; i < ji->data.table->table_size; i++)
2280 table [i] = (gpointer)(gssize)decode_value (p, &p);
2282 case MONO_PATCH_INFO_R4: {
2285 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
2286 val = decode_value (p, &p);
2287 *(float*)ji->data.target = *(float*)&val;
2290 case MONO_PATCH_INFO_R8: {
2294 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
2296 val [0] = decode_value (p, &p);
2297 val [1] = decode_value (p, &p);
2298 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
2299 *(double*)ji->data.target = *(double*)&v;
2302 case MONO_PATCH_INFO_LDSTR:
2303 image = load_image (aot_module, decode_value (p, &p), TRUE);
2306 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
2308 case MONO_PATCH_INFO_RVA:
2309 case MONO_PATCH_INFO_DECLSEC:
2310 case MONO_PATCH_INFO_LDTOKEN:
2311 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2313 image = load_image (aot_module, decode_value (p, &p), TRUE);
2316 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
2318 ji->data.token->has_context = decode_value (p, &p);
2319 if (ji->data.token->has_context) {
2320 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
2325 case MONO_PATCH_INFO_EXC_NAME:
2326 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2327 if (!ji->data.klass)
2329 ji->data.name = ji->data.klass->name;
2331 case MONO_PATCH_INFO_METHOD_REL:
2332 ji->data.offset = decode_value (p, &p);
2334 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2335 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2336 case MONO_PATCH_INFO_MONITOR_ENTER:
2337 case MONO_PATCH_INFO_MONITOR_EXIT:
2338 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
2340 case MONO_PATCH_INFO_RGCTX_FETCH: {
2342 MonoJumpInfoRgctxEntry *entry;
2344 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2345 entry->method = decode_resolve_method_ref (aot_module, p, &p);
2346 entry->in_mrgctx = decode_value (p, &p);
2347 entry->info_type = decode_value (p, &p);
2348 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2349 entry->data->type = decode_value (p, &p);
2351 res = decode_patch (aot_module, mp, entry->data, p, &p);
2354 ji->data.rgctx_entry = entry;
2357 case MONO_PATCH_INFO_SEQ_POINT_INFO:
2359 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
2360 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
2362 imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
2363 imt_tramp->vt_offset = decode_value (p, &p);
2365 ji->data.imt_tramp = imt_tramp;
2369 g_warning ("unhandled type %d", ji->type);
2370 g_assert_not_reached ();
2381 static MonoJumpInfo*
2382 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
2383 guint32 **got_slots,
2384 guint8 *buf, guint8 **endbuf)
2386 MonoJumpInfo *patches;
2392 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
2394 *got_slots = g_malloc (sizeof (guint32) * n_patches);
2396 for (pindex = 0; pindex < n_patches; ++pindex) {
2397 MonoJumpInfo *ji = &patches [pindex];
2402 got_offset = decode_value (p, &p);
2404 if (aot_module->got [got_offset]) {
2405 /* Already loaded */
2406 //printf ("HIT!\n");
2408 shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
2410 ji->type = decode_value (shared_p, &shared_p);
2412 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
2417 (*got_slots) [pindex] = got_offset;
2424 g_free (*got_slots);
2431 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
2434 * Jump addresses cannot be patched by the trampoline code since it
2435 * does not have access to the caller's address. Instead, we collect
2436 * the addresses of the GOT slots pointing to a method, and patch
2437 * them after the method has been compiled.
2439 MonoJitDomainInfo *info = domain_jit_info (domain);
2442 mono_domain_lock (domain);
2443 if (!info->jump_target_got_slot_hash)
2444 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
2445 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
2446 list = g_slist_prepend (list, got_slot);
2447 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
2448 mono_domain_unlock (domain);
2454 * Load the method identified by METHOD_INDEX from the AOT image. Return a
2455 * pointer to the native code of the method, or NULL if not found.
2456 * METHOD might not be set if the caller only has the image/token info.
2459 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
2462 gboolean from_plt = method == NULL;
2464 int i, pindex, n_patches, used_strings;
2465 gboolean keep_patches = TRUE;
2467 MonoJitInfo *jinfo = NULL;
2468 guint8 *code, *info;
2470 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2473 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
2474 /* Non shared AOT code can't be used in other appdomains */
2477 if (amodule->out_of_date)
2480 if (amodule->code_offsets [method_index] == 0xffffffff) {
2481 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2485 method = mono_get_method (image, token, NULL);
2486 full_name = mono_method_full_name (method, TRUE);
2487 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2493 code = &amodule->code [amodule->code_offsets [method_index]];
2495 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
2497 if (amodule->thumb_end && code < amodule->thumb_end) {
2498 /* Convert this into a thumb address */
2499 g_assert ((amodule->code_offsets [method_index] & 0x1) == 0);
2500 code = &amodule->code [amodule->code_offsets [method_index] + 1];
2504 if (!amodule->methods_loaded)
2505 amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods + 1);
2508 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2511 if (mono_last_aot_method != -1) {
2512 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
2514 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
2516 method = mono_get_method (image, token, NULL);
2518 char *name = mono_method_full_name (method, TRUE);
2519 printf ("LAST AOT METHOD: %s.\n", name);
2522 printf ("LAST AOT METHOD: %p %d\n", code, method_index);
2530 klass = method->klass;
2531 decode_klass_ref (amodule, p, &p);
2533 klass = decode_klass_ref (amodule, p, &p);
2536 if (amodule->info.opts & MONO_OPT_SHARED)
2537 used_strings = decode_value (p, &p);
2541 for (i = 0; i < used_strings; i++) {
2542 guint token = decode_value (p, &p);
2543 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2546 if (amodule->info.opts & MONO_OPT_SHARED)
2547 keep_patches = FALSE;
2549 n_patches = decode_value (p, &p);
2551 keep_patches = FALSE;
2554 MonoJumpInfo *patches;
2560 mp = mono_mempool_new ();
2562 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2563 if (patches == NULL)
2566 for (pindex = 0; pindex < n_patches; ++pindex) {
2567 MonoJumpInfo *ji = &patches [pindex];
2569 if (!amodule->got [got_slots [pindex]]) {
2570 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2571 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2572 amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
2573 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2574 register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
2576 ji->type = MONO_PATCH_INFO_NONE;
2582 mono_mempool_destroy (mp);
2585 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2589 method = mono_get_method (image, token, NULL);
2591 full_name = mono_method_full_name (method, TRUE);
2594 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
2596 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND AOT compiled code for %s %p - %p %p\n", full_name, code, code + jinfo->code_size, info);
2602 mono_jit_stats.methods_aot++;
2604 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2608 if (method && method->wrapper_type)
2609 g_hash_table_insert (amodule->method_to_code, method, code);
2613 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
2617 method = mono_get_method (image, token, NULL);
2620 mono_profiler_method_jit (method);
2621 jinfo = mono_jit_info_table_find (domain, (char*)code);
2623 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
2626 if (from_plt && klass && !klass->generic_container)
2627 mono_runtime_class_init (mono_class_vtable (domain, klass));
2632 /* FIXME: The space in domain->mp is wasted */
2633 if (amodule->info.opts & MONO_OPT_SHARED)
2634 /* No need to cache patches */
2635 mono_mempool_destroy (mp);
2644 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name)
2646 guint32 table_size, entry_size, hash;
2647 guint32 *table, *entry;
2649 static guint32 n_extra_decodes;
2654 table_size = amodule->extra_method_table [0];
2655 table = amodule->extra_method_table + 1;
2658 hash = mono_aot_method_hash (method) % table_size;
2660 entry = &table [hash * entry_size];
2667 guint32 key = entry [0];
2668 guint32 value = entry [1];
2669 guint32 next = entry [entry_size - 1];
2672 int is_wrapper_name;
2674 p = amodule->blob + key;
2675 is_wrapper_name = decode_value (p, &p);
2676 if (is_wrapper_name) {
2677 int wrapper_type = decode_value (p, &p);
2678 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2686 if (!amodule->method_ref_to_method)
2687 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2688 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2691 m = decode_resolve_method_ref_with_target (amodule, method, p, &p);
2694 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2703 /* Special case: wrappers of shared generic methods */
2704 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2705 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2706 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2707 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2709 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2715 /* Methods decoded needlessly */
2717 //printf ("%d %s %s %p\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE), orig_p);
2723 entry = &table [next * entry_size];
2732 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2734 g_ptr_array_add ((GPtrArray*)user_data, value);
2738 * find_extra_method:
2740 * Try finding METHOD in the extra_method table in all AOT images.
2741 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2742 * module where the method was found.
2745 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2752 if (method->wrapper_type)
2753 name = mono_aot_wrapper_name (method);
2755 /* Try the method's module first */
2756 *out_amodule = method->klass->image->aot_module;
2757 index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name);
2758 if (index != 0xffffff) {
2764 * Try all other modules.
2765 * This is needed because generic instances klass->image points to the image
2766 * containing the generic definition, but the native code is generated to the
2767 * AOT image which contains the reference.
2770 /* Make a copy to avoid doing the search inside the aot lock */
2771 modules = g_ptr_array_new ();
2773 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2777 for (i = 0; i < modules->len; ++i) {
2778 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2780 if (amodule != method->klass->image->aot_module)
2781 index = find_extra_method_in_amodule (amodule, method, name);
2782 if (index != 0xffffff) {
2783 *out_amodule = amodule;
2788 g_ptr_array_free (modules, TRUE);
2795 * mono_aot_get_method:
2797 * Return a pointer to the AOTed native code for METHOD if it can be found,
2799 * On platforms with function pointers, this doesn't return a function pointer.
2802 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2804 MonoClass *klass = method->klass;
2805 guint32 method_index;
2806 MonoAotModule *amodule = klass->image->aot_module;
2812 if (amodule->out_of_date)
2815 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2816 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2817 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2818 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2822 * Use the original method instead of its invoke-with-check wrapper.
2823 * This is not a problem when using full-aot, since it doesn't support
2826 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
2827 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
2829 g_assert (klass->inited);
2831 /* Find method index */
2832 if (method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE)) {
2834 * For generic methods, we store the fully shared instance in place of the
2837 method = mono_method_get_declaring_generic_method (method);
2838 method_index = mono_metadata_token_index (method->token) - 1;
2839 } else if (method->is_inflated || !method->token) {
2840 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2842 code = g_hash_table_lookup (amodule->method_to_code, method);
2847 method_index = find_extra_method (method, &amodule);
2849 * Special case the ICollection<T> wrappers for arrays, as they cannot
2850 * be statically enumerated, and each wrapper ends up calling the same
2853 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
2854 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
2856 code = mono_aot_get_method (domain, m);
2858 if (mono_method_needs_static_rgctx_invoke (m, FALSE)) {
2859 code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code));
2860 /* The call above returns an ftnptr */
2861 code = mono_get_addr_from_ftnptr (code);
2869 * Special case Array.GetGenericValueImpl which is a generic icall.
2870 * Generic sharing currently can't handle it, but the icall returns data using
2871 * an out parameter, so the managed-to-native wrappers can share the same code.
2873 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
2875 MonoGenericContext ctx;
2876 MonoType *args [16];
2878 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
2879 /* Avoid recursion */
2882 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
2885 memset (&ctx, 0, sizeof (ctx));
2886 args [0] = &mono_defaults.object_class->byval_arg;
2887 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2889 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2892 * Get the code for the <object> instantiation which should be emitted into
2893 * the mscorlib aot image by the AOT compiler.
2895 code = mono_aot_get_method (domain, m);
2900 /* Same for CompareExchange<T> */
2901 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")) {
2903 MonoGenericContext ctx;
2904 MonoType *args [16];
2905 gpointer iter = NULL;
2907 while ((m = mono_class_get_methods (method->klass, &iter))) {
2908 if (mono_method_signature (m)->generic_param_count && !strcmp (m->name, "CompareExchange"))
2913 memset (&ctx, 0, sizeof (ctx));
2914 args [0] = &mono_defaults.object_class->byval_arg;
2915 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2917 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2919 /* Avoid recursion */
2924 * Get the code for the <object> instantiation which should be emitted into
2925 * the mscorlib aot image by the AOT compiler.
2927 code = mono_aot_get_method (domain, m);
2932 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, TRUE)) {
2933 /* Partial sharing */
2934 method_index = find_extra_method (mini_get_shared_method (method), &amodule);
2937 if (method_index == 0xffffff) {
2938 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2941 full_name = mono_method_full_name (method, TRUE);
2942 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2948 if (method_index == 0xffffff)
2951 /* Needed by find_jit_info */
2953 if (!amodule->extra_methods)
2954 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2955 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2959 method_index = mono_metadata_token_index (method->token) - 1;
2962 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2966 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2970 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2972 MonoAotModule *aot_module = image->aot_module;
2978 method_index = mono_metadata_token_index (token) - 1;
2980 return load_method (domain, aot_module, image, NULL, token, method_index);
2986 } IsGotEntryUserData;
2989 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2991 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2992 MonoAotModule *aot_module = (MonoAotModule*)value;
2994 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
2999 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3001 IsGotEntryUserData user_data;
3006 user_data.addr = addr;
3007 user_data.res = FALSE;
3009 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
3012 return user_data.res;
3017 MonoAotModule *module;
3018 } FindAotModuleUserData;
3021 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
3023 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
3024 MonoAotModule *aot_module = (MonoAotModule*)value;
3026 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
3027 data->module = aot_module;
3030 static inline MonoAotModule*
3031 find_aot_module (guint8 *code)
3033 FindAotModuleUserData user_data;
3038 /* Reading these need no locking */
3039 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
3042 user_data.addr = code;
3043 user_data.module = NULL;
3046 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
3049 return user_data.module;
3053 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
3056 * Since AOT code is only used in the root domain,
3057 * mono_domain_get () != mono_get_root_domain () means the calling method
3058 * is AppDomain:InvokeInDomain, so this is the same check as in
3059 * mono_method_same_domain () but without loading the metadata for the method.
3061 if (mono_domain_get () == mono_get_root_domain ())
3062 mono_arch_patch_plt_entry (code, got, regs, addr);
3066 * mono_aot_plt_resolve:
3068 * This function is called by the entries in the PLT to resolve the actual method that
3069 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
3070 * Returns NULL if the something cannot be loaded.
3073 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3075 #ifdef MONO_ARCH_AOT_SUPPORTED
3076 guint8 *p, *target, *plt_entry;
3078 MonoAotModule *module = (MonoAotModule*)aot_module;
3079 gboolean res, no_ftnptr = FALSE;
3082 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
3084 p = &module->blob [plt_info_offset];
3086 ji.type = decode_value (p, &p);
3088 mp = mono_mempool_new_size (512);
3089 res = decode_patch (module, mp, &ji, p, &p);
3092 mono_mempool_destroy (mp);
3097 * Avoid calling resolve_patch_target in the full-aot case if possible, since
3098 * it would create a trampoline, and we don't need that.
3099 * We could do this only if the method does not need the special handling
3100 * in mono_magic_trampoline ().
3102 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) &&
3103 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
3104 target = mono_jit_compile_method (ji.data.method);
3107 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
3111 * The trampoline expects us to return a function descriptor on platforms which use
3112 * it, but resolve_patch_target returns a direct function pointer for some type of
3113 * patches, so have to translate between the two.
3114 * FIXME: Clean this up, but how ?
3116 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) {
3117 /* These should already have a function descriptor */
3118 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3119 /* Our function descriptors have a 0 environment, gcc created ones don't */
3120 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
3121 g_assert (((gpointer*)target) [2] == 0);
3124 } else if (!no_ftnptr) {
3125 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3126 g_assert (((gpointer*)target) [2] != 0);
3128 target = mono_create_ftnptr (mono_domain_get (), target);
3131 mono_mempool_destroy (mp);
3133 /* Patch the PLT entry with target which might be the actual method not a trampoline */
3134 plt_entry = mono_aot_get_plt_entry (code);
3135 g_assert (plt_entry);
3136 mono_aot_patch_plt_entry (plt_entry, module->got, NULL, target);
3140 g_assert_not_reached ();
3148 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
3149 * method in the module is loaded to avoid committing memory by writing to it.
3150 * LOCKING: Assumes the AOT lock is held.
3153 init_plt (MonoAotModule *amodule)
3158 if (amodule->plt_inited)
3161 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
3164 * Initialize the PLT entries in the GOT to point to the default targets.
3167 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
3168 for (i = 1; i < amodule->info.plt_size; ++i)
3169 /* All the default entries point to the AOT trampoline */
3170 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = tramp;
3172 amodule->plt_inited = TRUE;
3176 * mono_aot_get_plt_entry:
3178 * Return the address of the PLT entry called by the code at CODE if exists.
3181 mono_aot_get_plt_entry (guint8 *code)
3183 MonoAotModule *amodule = find_aot_module (code);
3184 guint8 *target = NULL;
3190 if (amodule->thumb_end && code < amodule->thumb_end) {
3191 return mono_arm_get_thumb_plt_entry (code);
3195 #ifdef MONO_ARCH_AOT_SUPPORTED
3196 target = mono_arch_get_call_target (code);
3198 g_assert_not_reached ();
3201 if ((target >= (guint8*)(amodule->plt)) && (target < (guint8*)(amodule->plt_end)))
3208 * mono_aot_get_plt_info_offset:
3210 * Return the PLT info offset belonging to the plt entry called by CODE.
3213 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3215 guint8 *plt_entry = mono_aot_get_plt_entry (code);
3217 g_assert (plt_entry);
3219 /* The offset is embedded inside the code after the plt entry */
3220 #ifdef MONO_ARCH_AOT_SUPPORTED
3221 return mono_arch_get_plt_info_offset (plt_entry, regs, code);
3223 g_assert_not_reached ();
3229 mono_create_ftnptr_malloc (guint8 *code)
3231 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3232 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
3234 ftnptr->code = code;
3245 * mono_aot_register_jit_icall:
3247 * Register a JIT icall which is called by trampolines in full-aot mode. This should
3248 * be called from mono_arch_init () during startup.
3251 mono_aot_register_jit_icall (const char *name, gpointer addr)
3253 /* No need for locking */
3254 if (!aot_jit_icall_hash)
3255 aot_jit_icall_hash = g_hash_table_new (g_str_hash, g_str_equal);
3256 g_hash_table_insert (aot_jit_icall_hash, (char*)name, addr);
3262 * Load the function named NAME from the aot image.
3265 load_function (MonoAotModule *amodule, const char *name)
3269 int n_patches, pindex;
3275 symbol = g_strdup_printf ("%s", name);
3276 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
3279 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
3281 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
3285 symbol = g_strdup_printf ("%s_p", name);
3286 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
3289 /* Nothing to patch */
3292 p = amodule->blob + *(guint32*)p;
3294 /* Similar to mono_aot_load_method () */
3296 n_patches = decode_value (p, &p);
3299 MonoJumpInfo *patches;
3302 mp = mono_mempool_new ();
3304 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
3307 for (pindex = 0; pindex < n_patches; ++pindex) {
3308 MonoJumpInfo *ji = &patches [pindex];
3311 if (amodule->got [got_slots [pindex]])
3315 * When this code is executed, the runtime may not be initalized yet, so
3316 * resolve the patch info by hand.
3318 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3319 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
3320 target = mono_get_lmf_addr;
3321 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
3322 target = mono_thread_force_interruption_checkpoint;
3323 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
3324 target = mono_exception_from_token;
3325 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
3326 target = mono_get_throw_exception ();
3327 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
3328 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
3329 target = (gpointer)mono_get_trampoline_func (tramp_type2);
3330 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
3331 /* atoll is needed because the the offset is unsigned */
3335 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
3336 g_assert (res == 1);
3337 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
3338 target = mono_create_ftnptr_malloc (target);
3339 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
3340 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
3341 target = mono_create_ftnptr_malloc (target);
3342 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
3343 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
3344 target = mono_create_ftnptr_malloc (target);
3345 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
3346 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
3347 target = mono_create_ftnptr_malloc (target);
3348 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
3349 target = mono_thread_get_and_clear_pending_exception;
3350 } else if (strstr (ji->data.name, "generic_trampoline_")) {
3351 target = mono_aot_get_trampoline (ji->data.name);
3352 } else if (aot_jit_icall_hash && g_hash_table_lookup (aot_jit_icall_hash, ji->data.name)) {
3353 /* Registered by mono_arch_init () */
3354 target = g_hash_table_lookup (aot_jit_icall_hash, ji->data.name);
3356 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
3357 g_assert_not_reached ();
3361 /* Hopefully the code doesn't have patches which need method or
3364 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
3368 amodule->got [got_slots [pindex]] = target;
3373 mono_mempool_destroy (mp);
3380 * Return the trampoline identified by NAME from the mscorlib AOT file.
3381 * On ppc64, this returns a function descriptor.
3384 mono_aot_get_trampoline (const char *name)
3387 MonoAotModule *amodule;
3389 image = mono_defaults.corlib;
3392 amodule = image->aot_module;
3395 return mono_create_ftnptr_malloc (load_function (amodule, name));
3398 /* Return a given kind of trampoline */
3400 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
3402 MonoAotModule *amodule;
3403 int index, tramp_size;
3406 /* Currently, we keep all trampolines in the mscorlib AOT image */
3407 image = mono_defaults.corlib;
3412 amodule = image->aot_module;
3415 *out_amodule = amodule;
3417 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
3418 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
3420 index = amodule->trampoline_index [tramp_type] ++;
3424 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
3426 tramp_size = amodule->info.trampoline_size [tramp_type];
3429 *out_tramp_size = tramp_size;
3431 return amodule->trampolines [tramp_type] + (index * tramp_size);
3435 * Return a specific trampoline from the AOT file.
3438 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3440 MonoAotModule *amodule;
3441 guint32 got_offset, tramp_size;
3442 guint8 *code, *tramp;
3443 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
3444 static gboolean inited;
3445 static guint32 num_trampolines;
3451 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
3460 if (!generic_trampolines [tramp_type]) {
3463 symbol = mono_get_generic_trampoline_name (tramp_type);
3464 generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
3468 tramp = generic_trampolines [tramp_type];
3471 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
3473 amodule->got [got_offset] = tramp;
3474 amodule->got [got_offset + 1] = arg1;
3477 *code_len = tramp_size;
3483 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3485 MonoAotModule *amodule;
3489 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
3491 amodule->got [got_offset] = ctx;
3492 amodule->got [got_offset + 1] = addr;
3494 /* The caller expects an ftnptr */
3495 return mono_create_ftnptr (mono_domain_get (), code);
3499 mono_aot_get_unbox_trampoline (MonoMethod *method)
3501 guint32 method_index = mono_metadata_token_index (method->token) - 1;
3502 MonoAotModule *amodule;
3506 if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
3507 guint32 index = find_extra_method (method, &amodule);
3508 g_assert (index != 0xffffff);
3510 symbol = g_strdup_printf ("ut_e_%d", index);
3512 amodule = method->klass->image->aot_module;
3515 symbol = g_strdup_printf ("ut_%d", method_index);
3517 code = load_function (amodule, symbol);
3520 /* The caller expects an ftnptr */
3521 return mono_create_ftnptr (mono_domain_get (), code);
3525 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3530 symbol = mono_get_rgctx_fetch_trampoline_name (slot);
3531 code = load_function (mono_defaults.corlib->aot_module, symbol);
3533 /* The caller expects an ftnptr */
3534 return mono_create_ftnptr (mono_domain_get (), code);
3538 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3544 MonoAotModule *amodule;
3546 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
3548 /* Save the entries into an array */
3549 buf = mono_domain_alloc (domain, (count + 1) * 2 * sizeof (gpointer));
3550 for (i = 0; i < count; ++i) {
3551 MonoIMTCheckItem *item = imt_entries [i];
3553 g_assert (item->key);
3555 g_assert (!item->has_target_code);
3557 buf [(i * 2)] = item->key;
3558 buf [(i * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
3560 buf [(count * 2)] = NULL;
3561 buf [(count * 2) + 1] = fail_tramp;
3563 amodule->got [got_offset] = buf;
3569 * mono_aot_set_make_unreadable:
3571 * Set whenever to make all mmaped memory unreadable. In conjuction with a
3572 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
3575 mono_aot_set_make_unreadable (gboolean unreadable)
3579 make_unreadable = unreadable;
3581 if (make_unreadable && !inited) {
3582 mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
3587 MonoAotModule *module;
3592 find_map (gpointer key, gpointer value, gpointer user_data)
3594 MonoAotModule *module = (MonoAotModule*)value;
3595 FindMapUserData *data = (FindMapUserData*)user_data;
3598 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
3599 data->module = module;
3602 static MonoAotModule*
3603 find_module_for_addr (void *ptr)
3605 FindMapUserData data;
3607 if (!make_unreadable)
3611 data.ptr = (guint8*)ptr;
3614 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
3621 * mono_aot_is_pagefault:
3623 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
3624 * within memory allocated by this module.
3627 mono_aot_is_pagefault (void *ptr)
3629 if (!make_unreadable)
3633 * Not signal safe, but SIGSEGV's are synchronous, and
3634 * this is only turned on by a MONO_DEBUG option.
3636 return find_module_for_addr (ptr) != NULL;
3640 * mono_aot_handle_pagefault:
3642 * Handle a pagefault caused by an unreadable page by making it readable again.
3645 mono_aot_handle_pagefault (void *ptr)
3647 #ifndef PLATFORM_WIN32
3648 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
3652 res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
3653 g_assert (res == 0);
3664 mono_aot_init (void)
3669 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3675 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3681 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3687 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
3693 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
3699 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3705 mono_aot_get_plt_entry (guint8 *code)
3711 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3717 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
3722 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
3728 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3730 g_assert_not_reached ();
3736 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3738 g_assert_not_reached ();
3743 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3745 g_assert_not_reached ();
3750 mono_aot_get_trampoline (const char *name)
3752 g_assert_not_reached ();
3757 mono_aot_get_unbox_trampoline (MonoMethod *method)
3759 g_assert_not_reached ();
3764 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3766 g_assert_not_reached ();
3771 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3773 g_assert_not_reached ();
3778 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3780 g_assert_not_reached ();
3785 mono_aot_register_jit_icall (const char *name, gpointer addr)