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/utils/mono-logger-internal.h>
55 #include <mono/utils/mono-mmap.h>
56 #include "mono/utils/mono-compiler.h"
57 #include <mono/utils/mono-counters.h>
65 #define SHARED_EXT ".dll"
66 #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
67 #define SHARED_EXT ".dylib"
69 #define SHARED_EXT ".so"
72 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
73 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
75 typedef struct MonoAotModule {
77 /* Pointer to the Global Offset Table */
79 GHashTable *name_cache;
80 GHashTable *extra_methods;
81 /* Maps methods to their code */
82 GHashTable *method_to_code;
83 /* Maps pointers into the method info to the methods themselves */
84 GHashTable *method_ref_to_method;
85 MonoAssemblyName *image_names;
87 MonoAssembly *assembly;
88 MonoImage **image_table;
89 guint32 image_table_len;
100 /* This contains <offset, index> pairs sorted by offset */
101 /* This is needed because LLVM emitted methods can be in any order */
102 gint32 *sorted_code_offsets;
103 gint32 sorted_code_offsets_len;
104 guint32 *method_info_offsets;
105 guint32 *got_info_offsets;
106 guint32 *ex_info_offsets;
107 guint32 *class_info_offsets;
108 guint32 *methods_loaded;
109 guint16 *class_name_table;
110 guint32 *extra_method_table;
111 guint32 *extra_method_info_offsets;
114 /* Points to the GNU .eh_frame_hdr section, if it exists */
115 guint8 *eh_frame_hdr;
117 /* Points to the trampolines */
118 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
119 /* The first unused trampoline of each kind */
120 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
122 MonoAotFileInfo info;
128 static GHashTable *aot_modules;
129 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
130 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
131 static CRITICAL_SECTION aot_mutex;
134 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
135 * AOT modules registered by mono_aot_register_module ().
137 static GHashTable *static_aot_modules;
140 * Maps MonoJitInfo* to the aot module they belong to, this can be different
141 * from ji->method->klass->image's aot module for generic instances.
143 static GHashTable *ji_to_amodule;
146 * Whenever to AOT compile loaded assemblies on demand and store them in
147 * a cache under $HOME/.mono/aot-cache.
149 static gboolean use_aot_cache = FALSE;
152 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
153 * use_aot_cache is TRUE.
155 static gboolean spawn_compiler = TRUE;
158 static gint32 mono_last_aot_method = -1;
160 static gboolean make_unreadable = FALSE;
161 static guint32 name_table_accesses = 0;
162 static guint32 n_pagefaults = 0;
164 /* Used to speed-up find_aot_module () */
165 static gsize aot_code_low_addr = (gssize)-1;
166 static gsize aot_code_high_addr = 0;
169 init_plt (MonoAotModule *info);
171 /*****************************************************/
173 /*****************************************************/
178 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
179 * found, and sets the loader error if SET_ERROR is TRUE.
182 load_image (MonoAotModule *amodule, int index, gboolean set_error)
184 MonoAssembly *assembly;
185 MonoImageOpenStatus status;
187 g_assert (index < amodule->image_table_len);
189 if (amodule->image_table [index])
190 return amodule->image_table [index];
191 if (amodule->out_of_date)
194 assembly = mono_assembly_load (&amodule->image_names [index], NULL, &status);
196 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);
197 amodule->out_of_date = TRUE;
200 char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
201 mono_loader_set_error_assembly_load (full_name, FALSE);
207 if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
208 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);
209 amodule->out_of_date = TRUE;
213 amodule->image_table [index] = assembly->image;
214 return assembly->image;
218 decode_value (guint8 *ptr, guint8 **rptr)
223 if ((b & 0x80) == 0){
226 } else if ((b & 0x40) == 0){
227 len = ((b & 0x3f) << 8 | ptr [1]);
229 } else if (b != 0xff) {
230 len = ((b & 0x1f) << 24) |
237 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
243 //printf ("DECODE: %d.\n", len);
248 * mono_aot_get_method:
250 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
254 mono_aot_get_offset (guint32 *table, int index)
256 int i, group, ngroups, index_entry_size;
257 int start_offset, offset, noffsets, group_size;
258 guint8 *data_start, *p;
259 guint32 *index32 = NULL;
260 guint16 *index16 = NULL;
262 noffsets = table [0];
263 group_size = table [1];
265 index_entry_size = table [3];
266 group = index / group_size;
268 if (index_entry_size == 2) {
269 index16 = (guint16*)&table [4];
270 data_start = (guint8*)&index16 [ngroups];
271 p = data_start + index16 [group];
273 index32 = (guint32*)&table [4];
274 data_start = (guint8*)&index32 [ngroups];
275 p = data_start + index32 [group];
278 /* offset will contain the value of offsets [group * group_size] */
279 offset = start_offset = decode_value (p, &p);
280 for (i = group * group_size + 1; i <= index; ++i) {
281 offset += decode_value (p, &p);
284 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
290 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
293 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
295 static MonoGenericInst*
296 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
299 MonoType **type_argv;
300 MonoGenericInst *inst;
303 type_argc = decode_value (p, &p);
304 type_argv = g_new0 (MonoType*, type_argc);
306 for (i = 0; i < type_argc; ++i) {
307 MonoClass *pclass = decode_klass_ref (module, p, &p);
312 type_argv [i] = &pclass->byval_arg;
315 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
324 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
326 gboolean has_class_inst, has_method_inst;
329 has_class_inst = decode_value (p, &p);
330 if (has_class_inst) {
331 ctx->class_inst = decode_generic_inst (module, p, &p);
332 if (!ctx->class_inst)
335 has_method_inst = decode_value (p, &p);
336 if (has_method_inst) {
337 ctx->method_inst = decode_generic_inst (module, p, &p);
338 if (!ctx->method_inst)
347 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
350 MonoClass *klass, *eklass;
354 token = decode_value (p, &p);
359 if (mono_metadata_token_table (token) == 0) {
360 image = load_image (module, decode_value (p, &p), TRUE);
363 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
364 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
365 if (token == MONO_TOKEN_TYPE_SPEC) {
366 MonoTypeEnum type = decode_value (p, &p);
368 if (type == MONO_TYPE_GENERICINST) {
370 MonoGenericContext ctx;
373 gclass = decode_klass_ref (module, p, &p);
376 g_assert (gclass->generic_container);
378 memset (&ctx, 0, sizeof (ctx));
379 ctx.class_inst = decode_generic_inst (module, p, &p);
382 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
383 klass = mono_class_from_mono_type (type);
384 mono_metadata_free_type (type);
385 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
387 MonoGenericContainer *container;
389 int num = decode_value (p, &p);
390 gboolean is_method = decode_value (p, &p);
393 MonoMethod *method_def;
394 g_assert (type == MONO_TYPE_MVAR);
395 method_def = decode_resolve_method_ref (module, p, &p);
399 container = mono_method_get_generic_container (method_def);
401 MonoClass *class_def;
402 g_assert (type == MONO_TYPE_VAR);
403 class_def = decode_klass_ref (module, p, &p);
407 container = class_def->generic_container;
410 g_assert (container);
412 // FIXME: Memory management
413 t = g_new0 (MonoType, 1);
415 t->data.generic_param = mono_generic_container_get_param (container, num);
417 // FIXME: Maybe use types directly to avoid
418 // the overhead of creating MonoClass-es
419 klass = mono_class_from_mono_type (t);
423 g_assert_not_reached ();
426 image = load_image (module, decode_value (p, &p), TRUE);
429 klass = mono_class_get (image, token);
431 } else if (token == MONO_TOKEN_TYPE_DEF) {
433 image = load_image (module, decode_value (p, &p), TRUE);
436 rank = decode_value (p, &p);
437 eklass = decode_klass_ref (module, p, &p);
438 klass = mono_array_class_get (eklass, rank);
440 g_assert_not_reached ();
448 static MonoClassField*
449 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
451 MonoClass *klass = decode_klass_ref (module, buf, &buf);
458 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
462 return mono_class_get_field (klass, token);
466 * can_method_ref_match_method:
468 * Determine if calling decode_resolve_method_ref on P could return the same method as
469 * METHOD. This is an optimization to avoid calling decode_resolve_method_ref () which
470 * would create MonoMethods which are not needed etc.
473 can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
476 guint32 image_index, value;
478 /* Keep this in sync with decode_method_ref () */
479 value = decode_value (p, &p);
480 image_index = value >> 24;
482 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
483 guint32 wrapper_type;
485 if (!method->wrapper_type)
488 wrapper_type = decode_value (p, &p);
490 if (method->wrapper_type != wrapper_type)
492 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
494 } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
495 if (method->wrapper_type)
505 * Decode a method reference, and return its image and token. This avoids loading
506 * metadata for the method if the caller does not need it. If the method has no token,
507 * then it is loaded from metadata and METHOD is set to the method instance.
510 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
512 guint32 image_index, value;
513 MonoImage *image = NULL;
518 if (no_aot_trampoline)
519 *no_aot_trampoline = FALSE;
521 value = decode_value (p, &p);
522 image_index = value >> 24;
524 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
525 if (no_aot_trampoline)
526 *no_aot_trampoline = TRUE;
527 value = decode_value (p, &p);
528 image_index = value >> 24;
531 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
532 guint32 wrapper_type;
534 wrapper_type = decode_value (p, &p);
537 image = mono_defaults.corlib;
539 switch (wrapper_type) {
540 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
541 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
545 mono_class_init (m->klass);
546 *method = mono_marshal_get_remoting_invoke_with_check (m);
549 case MONO_WRAPPER_PROXY_ISINST: {
550 MonoClass *klass = decode_klass_ref (module, p, &p);
553 *method = mono_marshal_get_proxy_cancast (klass);
556 case MONO_WRAPPER_LDFLD:
557 case MONO_WRAPPER_LDFLDA:
558 case MONO_WRAPPER_STFLD:
559 case MONO_WRAPPER_ISINST: {
560 MonoClass *klass = decode_klass_ref (module, p, &p);
563 if (wrapper_type == MONO_WRAPPER_LDFLD)
564 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
565 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
566 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
567 else if (wrapper_type == MONO_WRAPPER_STFLD)
568 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
569 else if (wrapper_type == MONO_WRAPPER_ISINST)
570 *method = mono_marshal_get_isinst (klass);
572 g_assert_not_reached ();
575 case MONO_WRAPPER_LDFLD_REMOTE:
576 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
578 case MONO_WRAPPER_STFLD_REMOTE:
579 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
581 case MONO_WRAPPER_ALLOC: {
582 int atype = decode_value (p, &p);
584 *method = mono_gc_get_managed_allocator_by_type (atype);
587 case MONO_WRAPPER_WRITE_BARRIER:
588 *method = mono_gc_get_write_barrier ();
590 case MONO_WRAPPER_STELEMREF:
591 *method = mono_marshal_get_stelemref ();
593 case MONO_WRAPPER_SYNCHRONIZED: {
594 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
598 *method = mono_marshal_get_synchronized_wrapper (m);
601 case MONO_WRAPPER_UNKNOWN: {
602 MonoMethodDesc *desc;
603 MonoMethod *orig_method;
604 int subtype = decode_value (p, &p);
606 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
607 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
608 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
609 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
611 g_assert_not_reached ();
612 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
613 g_assert (orig_method);
614 mono_method_desc_free (desc);
615 *method = mono_monitor_get_fast_path (orig_method);
618 case MONO_WRAPPER_RUNTIME_INVOKE: {
620 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
624 *method = mono_marshal_get_runtime_invoke (m, FALSE);
627 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
628 int subtype = decode_value (p, &p);
630 if (subtype == MONO_AOT_WRAPPER_ELEMENT_ADDR) {
631 int rank = decode_value (p, &p);
632 int elem_size = decode_value (p, &p);
634 *method = mono_marshal_get_array_address (rank, elem_size);
636 g_assert_not_reached ();
641 g_assert_not_reached ();
643 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
644 /* Can't decode these */
645 g_assert_not_reached ();
646 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
647 image_index = decode_value (p, &p);
648 *token = decode_value (p, &p);
650 image = load_image (module, image_index, TRUE);
653 } else if (image_index == MONO_AOT_METHODREF_GINST) {
655 MonoGenericContext ctx;
658 * These methods do not have a token which resolves them, so we
659 * resolve them immediately.
661 klass = decode_klass_ref (module, p, &p);
665 image_index = decode_value (p, &p);
666 *token = decode_value (p, &p);
668 image = load_image (module, image_index, TRUE);
672 *method = mono_get_method_full (image, *token, NULL, NULL);
676 memset (&ctx, 0, sizeof (ctx));
678 if (FALSE && klass->generic_class) {
679 ctx.class_inst = klass->generic_class->context.class_inst;
680 ctx.method_inst = NULL;
682 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
685 memset (&ctx, 0, sizeof (ctx));
687 if (!decode_generic_context (module, &ctx, p, &p))
690 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
691 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
695 klass = decode_klass_ref (module, p, &p);
698 method_type = decode_value (p, &p);
700 switch (method_type) {
702 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
705 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
708 *method = mono_class_get_method_from_name (klass, "Get", -1);
711 *method = mono_class_get_method_from_name (klass, "Address", -1);
714 *method = mono_class_get_method_from_name (klass, "Set", -1);
717 g_assert_not_reached ();
720 g_assert (image_index < MONO_AOT_METHODREF_MIN);
721 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
723 image = load_image (module, image_index, TRUE);
734 * decode_resolve_method_ref:
736 * Similar to decode_method_ref, but resolve and return the method itself.
739 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
743 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
749 method = mono_get_method (image, token, NULL);
754 create_cache_structure (void)
760 home = g_get_home_dir ();
764 tmp = g_build_filename (home, ".mono", NULL);
765 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
766 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
770 err = mkdir (tmp, 0777);
773 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
779 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
780 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
781 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
785 err = mkdir (tmp, 0777);
788 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
797 * load_aot_module_from_cache:
799 * Experimental code to AOT compile loaded assemblies on demand.
802 * - Add environment variable MONO_AOT_CACHE_OPTIONS
803 * - Add options for controlling the cache size
804 * - Handle full cache by deleting old assemblies lru style
805 * - Add options for excluding assemblies during development
806 * - Maybe add a threshold after an assembly is AOT compiled
807 * - invoking a new mono process is a security risk
808 * - recompile the AOT module if one of its dependencies changes
811 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
813 char *fname, *cmd, *tmp2, *aot_options;
822 if (assembly->image->dynamic)
825 create_cache_structure ();
827 home = g_get_home_dir ();
829 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
830 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
834 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
835 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
838 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
840 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
842 aot_options = g_strdup_printf ("outfile=%s", fname);
844 if (spawn_compiler) {
845 /* FIXME: security */
846 /* FIXME: Has to pass the assembly loading path to the child process */
847 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
849 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
851 #if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
853 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
854 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
856 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
863 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
865 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
867 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
871 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
873 g_free (aot_options);
880 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
884 guint16 *table, *entry;
888 /* The first entry points to the hash */
892 table_size = table [0];
895 hash = mono_metadata_str_hash (name) % table_size;
897 entry = &table [hash * 2];
899 /* Search the hash for the index into the globals table */
901 while (entry [0] != 0) {
902 guint32 index = entry [0] - 1;
903 guint32 next = entry [1];
905 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
907 if (!strcmp (globals [index * 2], name)) {
908 global_index = index;
913 entry = &table [next * 2];
919 if (global_index != -1)
920 *value = globals [global_index * 2 + 1];
924 char *err = mono_dl_symbol (module, name, value);
931 #if defined(HAVE_DL_ITERATE_PHDR) && defined(PT_GNU_EH_FRAME)
933 dl_callback (struct dl_phdr_info *info, size_t size, void *data)
936 MonoAotModule *amodule = data;
938 if (!strcmp (amodule->aot_name, info->dlpi_name)) {
939 for (j = 0; j < info->dlpi_phnum; j++) {
940 if (info->dlpi_phdr [j].p_type == PT_GNU_EH_FRAME)
941 amodule->eh_frame_hdr = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
951 load_aot_module (MonoAssembly *assembly, gpointer user_data)
954 MonoAotModule *amodule;
956 gboolean usable = TRUE;
957 char *saved_guid = NULL;
958 char *aot_version = NULL;
959 char *runtime_version, *build_info;
960 char *opt_flags = NULL;
962 gboolean full_aot = FALSE;
963 MonoAotFileInfo *file_info = NULL;
967 if (mono_compile_aot)
970 if (assembly->image->aot_module)
972 * Already loaded. This can happen because the assembly loading code might invoke
973 * the assembly load hooks multiple times for the same assembly.
977 if (assembly->image->dynamic)
980 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
984 if (static_aot_modules)
985 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
991 /* Statically linked AOT module */
993 aot_name = g_strdup_printf ("%s", assembly->aname.name);
994 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
997 sofile = load_aot_module_from_cache (assembly, &aot_name);
1000 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
1002 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1005 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
1011 if (!sofile && !globals) {
1012 if (mono_aot_only) {
1013 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1020 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
1021 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
1022 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
1023 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
1024 find_symbol (sofile, globals, "mono_aot_got_addr", (gpointer *)&got_addr);
1026 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
1027 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s has wrong file format version (expected %s got %s)\n", aot_name, MONO_AOT_FILE_VERSION, aot_version);
1031 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
1032 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
1037 build_info = mono_get_runtime_build_info ();
1038 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
1039 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled against runtime version '%s' while this runtime has version '%s'.\n", aot_name, runtime_version, build_info);
1042 g_free (build_info);
1044 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
1045 g_assert (file_info);
1047 full_aot = ((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1049 if (mono_aot_only && !full_aot) {
1050 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
1053 if (!mono_aot_only && full_aot) {
1054 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
1058 if ((((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1059 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with LLVM.\n", aot_name);
1064 if (mono_aot_only) {
1065 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
1070 mono_dl_close (sofile);
1071 assembly->image->aot_module = NULL;
1075 amodule = g_new0 (MonoAotModule, 1);
1076 amodule->aot_name = aot_name;
1077 amodule->assembly = assembly;
1079 memcpy (&amodule->info, file_info, sizeof (*file_info));
1081 amodule->got = *got_addr;
1082 amodule->got [0] = assembly->image;
1083 amodule->globals = globals;
1084 amodule->sofile = sofile;
1085 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1087 /* Read image table */
1089 guint32 table_len, i;
1092 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
1095 table_len = *(guint32*)table;
1096 table += sizeof (guint32);
1097 amodule->image_table = g_new0 (MonoImage*, table_len);
1098 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1099 amodule->image_guids = g_new0 (char*, table_len);
1100 amodule->image_table_len = table_len;
1101 for (i = 0; i < table_len; ++i) {
1102 MonoAssemblyName *aname = &(amodule->image_names [i]);
1104 aname->name = g_strdup (table);
1105 table += strlen (table) + 1;
1106 amodule->image_guids [i] = g_strdup (table);
1107 table += strlen (table) + 1;
1109 aname->culture = g_strdup (table);
1110 table += strlen (table) + 1;
1111 memcpy (aname->public_key_token, table, strlen (table) + 1);
1112 table += strlen (table) + 1;
1114 table = ALIGN_PTR_TO (table, 8);
1115 aname->flags = *(guint32*)table;
1117 aname->major = *(guint32*)table;
1119 aname->minor = *(guint32*)table;
1121 aname->build = *(guint32*)table;
1123 aname->revision = *(guint32*)table;
1128 /* Read method and method_info tables */
1129 find_symbol (sofile, globals, "code_offsets", (gpointer*)&amodule->code_offsets);
1130 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
1131 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
1132 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
1133 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
1134 find_symbol (sofile, globals, "blob", (gpointer*)&amodule->blob);
1135 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
1136 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
1137 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
1138 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1139 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1140 find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC]));
1141 find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX]));
1142 find_symbol (sofile, globals, "imt_thunks", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK]));
1143 find_symbol (sofile, globals, "unwind_info", (gpointer)&amodule->unwind_info);
1144 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1146 amodule->mem_begin = amodule->code;
1148 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1149 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1151 if (make_unreadable) {
1152 #ifndef TARGET_WIN32
1154 guint8 *page_start, *page_end;
1157 addr = amodule->mem_begin;
1158 len = amodule->mem_end - amodule->mem_begin;
1160 /* Round down in both directions to avoid modifying data which is not ours */
1161 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1162 page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
1163 if (page_end > page_start) {
1164 err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
1165 g_assert (err == 0);
1172 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1173 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1175 g_hash_table_insert (aot_modules, assembly, amodule);
1178 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1180 assembly->image->aot_module = amodule;
1182 #if defined(HAVE_DL_ITERATE_PHDR) && defined(PT_GNU_EH_FRAME)
1183 /* Lookup the address of the .eh_frame_hdr () section if available */
1184 dl_iterate_phdr (dl_callback, amodule);
1187 if (mono_aot_only) {
1188 if (mono_defaults.corlib) {
1189 /* The second got slot contains the mscorlib got addr */
1190 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1192 amodule->got [1] = mscorlib_amodule->got;
1194 amodule->got [1] = amodule->got;
1199 * Since we store methoddef and classdef tokens when referring to methods/classes in
1200 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1201 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1202 * non-lazily, since we can't handle out-of-date errors later.
1203 * The cached class info also depends on the exact assemblies.
1205 for (i = 0; i < amodule->image_table_len; ++i)
1206 load_image (amodule, i, FALSE);
1208 if (amodule->out_of_date) {
1209 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);
1210 if (mono_aot_only) {
1211 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);
1216 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1220 * mono_aot_register_globals:
1222 * This is called by the ctor function in AOT images compiled with the
1223 * 'no-dlsym' option.
1226 mono_aot_register_globals (gpointer *globals)
1228 g_assert_not_reached ();
1232 * mono_aot_register_module:
1234 * This should be called by embedding code to register AOT modules statically linked
1235 * into the executable. AOT_INFO should be the value of the
1236 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1239 mono_aot_register_module (gpointer *aot_info)
1247 /* Determine the assembly name */
1248 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1251 /* This could be called before startup */
1255 if (!static_aot_modules)
1256 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1258 g_hash_table_insert (static_aot_modules, aname, globals);
1265 mono_aot_init (void)
1267 InitializeCriticalSection (&aot_mutex);
1268 aot_modules = g_hash_table_new (NULL, NULL);
1270 mono_install_assembly_load_hook (load_aot_module, NULL);
1272 if (g_getenv ("MONO_LASTAOT"))
1273 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1274 if (g_getenv ("MONO_AOT_CACHE"))
1275 use_aot_cache = TRUE;
1279 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1283 info->vtable_size = decode_value (buf, &buf);
1284 if (info->vtable_size == -1)
1287 flags = decode_value (buf, &buf);
1288 info->ghcimpl = (flags >> 0) & 0x1;
1289 info->has_finalize = (flags >> 1) & 0x1;
1290 info->has_cctor = (flags >> 2) & 0x1;
1291 info->has_nested_classes = (flags >> 3) & 0x1;
1292 info->blittable = (flags >> 4) & 0x1;
1293 info->has_references = (flags >> 5) & 0x1;
1294 info->has_static_refs = (flags >> 6) & 0x1;
1295 info->no_special_static_fields = (flags >> 7) & 0x1;
1296 info->is_generic_container = (flags >> 8) & 0x1;
1298 if (info->has_cctor) {
1299 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1303 if (info->has_finalize) {
1304 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1305 if (!info->finalize_image)
1309 info->instance_size = decode_value (buf, &buf);
1310 info->class_size = decode_value (buf, &buf);
1311 info->packing_size = decode_value (buf, &buf);
1312 info->min_align = decode_value (buf, &buf);
1320 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1323 MonoClass *klass = vtable->klass;
1324 MonoAotModule *aot_module = klass->image->aot_module;
1326 MonoCachedClassInfo class_info;
1330 gboolean no_aot_trampoline;
1332 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1335 info = &aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1338 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1342 for (i = 0; i < slot; ++i)
1343 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1345 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1348 if (no_aot_trampoline)
1351 if (mono_metadata_token_index (token) == 0)
1354 return mono_aot_get_method_from_token (domain, image, token);
1358 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1360 MonoAotModule *aot_module = klass->image->aot_module;
1364 if (klass->rank || !aot_module)
1367 p = (guint8*)&aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1369 err = decode_cached_class_info (aot_module, res, p, &p);
1377 * mono_aot_get_class_from_name:
1379 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1380 * using a cache stored in the AOT file.
1381 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1383 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1387 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1389 MonoAotModule *aot_module = image->aot_module;
1390 guint16 *table, *entry;
1393 char full_name_buf [1024];
1395 const char *name2, *name_space2;
1397 guint32 cols [MONO_TYPEDEF_SIZE];
1398 GHashTable *nspace_table;
1400 if (!aot_module || !aot_module->class_name_table)
1407 /* First look in the cache */
1408 if (!aot_module->name_cache)
1409 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1410 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1412 *klass = g_hash_table_lookup (nspace_table, name);
1419 table_size = aot_module->class_name_table [0];
1420 table = aot_module->class_name_table + 1;
1422 if (name_space [0] == '\0')
1423 full_name = g_strdup_printf ("%s", name);
1425 if (strlen (name_space) + strlen (name) < 1000) {
1426 sprintf (full_name_buf, "%s.%s", name_space, name);
1427 full_name = full_name_buf;
1429 full_name = g_strdup_printf ("%s.%s", name_space, name);
1432 hash = mono_metadata_str_hash (full_name) % table_size;
1433 if (full_name != full_name_buf)
1436 entry = &table [hash * 2];
1438 if (entry [0] != 0) {
1439 t = &image->tables [MONO_TABLE_TYPEDEF];
1442 guint32 index = entry [0];
1443 guint32 next = entry [1];
1444 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1446 name_table_accesses ++;
1448 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1450 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1451 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1453 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1455 *klass = mono_class_get (image, token);
1460 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1461 if (!nspace_table) {
1462 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1463 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1465 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1472 entry = &table [next * 2];
1484 #define DW_EH_PE_omit 0xff
1485 #define DW_EH_PE_uleb128 0x01
1486 #define DW_EH_PE_udata2 0x02
1487 #define DW_EH_PE_udata4 0x03
1488 #define DW_EH_PE_udata8 0x04
1489 #define DW_EH_PE_sleb128 0x09
1490 #define DW_EH_PE_sdata2 0x0A
1491 #define DW_EH_PE_sdata4 0x0B
1492 #define DW_EH_PE_sdata8 0x0C
1494 #define DW_EH_PE_absptr 0x00
1495 #define DW_EH_PE_pcrel 0x10
1496 #define DW_EH_PE_datarel 0x30
1497 #define DW_EH_PE_omit 0xff
1502 guint8 eh_frame_ptr_enc;
1503 guint8 fde_count_enc;
1511 * Decode the exception handling information in the .eh_frame section of the AOT
1512 * file belong to CODE, and construct a MonoJitInfo structure from it.
1513 * LOCKING: Acquires the domain lock.
1515 static G_GNUC_UNUSED MonoJitInfo*
1516 decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
1517 MonoMethod *method, guint8 *code, MonoJitInfo *orig_jinfo,
1522 guint8 *eh_frame, *unwind_info;
1523 guint32 eh_frame_ptr;
1526 int i, pos, left, right, offset, offset1, offset2;
1527 guint32 unw_len, code_len;
1528 MonoJitExceptionInfo *ei;
1530 gpointer *type_info;
1533 g_assert (amodule->eh_frame_hdr);
1535 // http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
1536 hdr = (eh_frame_hdr*)amodule->eh_frame_hdr;
1537 g_assert (hdr->version == 1);
1538 g_assert (hdr->eh_frame_ptr_enc == (DW_EH_PE_pcrel | DW_EH_PE_sdata4));
1539 g_assert (hdr->fde_count_enc == DW_EH_PE_udata4);
1540 g_assert (hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4));
1543 eh_frame_ptr = *(guint32*)p;
1545 fde_count = *(guint32*)p;
1549 /* Binary search in the table to find the entry for code */
1550 offset = code - amodule->eh_frame_hdr;
1555 pos = (left + right) / 2;
1557 offset1 = table [(pos * 2)];
1558 if (pos + 1 == fde_count)
1560 offset2 = amodule->code_end - amodule->code;
1562 offset2 = table [(pos + 1) * 2];
1564 if (offset < offset1)
1566 else if (offset >= offset2)
1572 g_assert (code >= amodule->eh_frame_hdr + table [(pos * 2)]);
1573 if (pos < fde_count)
1574 g_assert (code < amodule->eh_frame_hdr + table [(pos * 2) + 2]);
1576 eh_frame = amodule->eh_frame_hdr + table [(pos * 2) + 1];
1578 unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len, &type_info);
1581 * LLVM might represent one IL region with multiple regions, so have to
1582 * allocate a new JI.
1586 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * ei_len) + extra_size);
1591 jinfo->code_size = code_len;
1592 jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
1593 jinfo->method = method;
1594 jinfo->code_start = code;
1595 jinfo->domain_neutral = 0;
1596 /* This signals that used_regs points to a normal cached unwind info */
1597 jinfo->from_aot = 0;
1598 jinfo->num_clauses = ei_len;
1600 for (i = 0; i < ei_len; ++i) {
1602 * orig_jinfo contains the original IL exception info saved by the AOT
1603 * compiler, we have to combine that with the information produced by LLVM
1605 /* The type_info entries contain IL clause indexes */
1606 int clause_index = *(gint32*)type_info [i];
1607 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
1608 MonoJitExceptionInfo *orig_jei = &orig_jinfo->clauses [clause_index];
1610 g_assert (clause_index < orig_jinfo->num_clauses);
1611 jei->flags = orig_jei->flags;
1612 jei->data.catch_class = orig_jei->data.catch_class;
1614 jei->try_start = ei [i].try_start;
1615 jei->try_end = ei [i].try_end;
1616 jei->handler_start = ei [i].handler_start;
1623 * LOCKING: Acquires the domain lock.
1626 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
1627 MonoMethod *method, guint8* ex_info, guint8 *addr,
1628 guint8 *code, guint32 code_len)
1632 guint used_int_regs, flags;
1633 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points, has_try_block_holes;
1636 int generic_info_size, try_holes_info_size, num_holes;
1638 /* Load the method info from the AOT file */
1641 flags = decode_value (p, &p);
1642 has_generic_jit_info = (flags & 1) != 0;
1643 has_dwarf_unwind_info = (flags & 2) != 0;
1644 has_clauses = (flags & 4) != 0;
1645 has_seq_points = (flags & 8) != 0;
1646 from_llvm = (flags & 16) != 0;
1647 has_try_block_holes = (flags & 32) != 0;
1649 if (has_dwarf_unwind_info) {
1652 offset = decode_value (p, &p);
1653 g_assert (offset < (1 << 30));
1654 used_int_regs = offset;
1656 used_int_regs = decode_value (p, &p);
1658 if (has_generic_jit_info)
1659 generic_info_size = sizeof (MonoGenericJitInfo);
1661 generic_info_size = 0;
1663 if (has_try_block_holes) {
1664 num_holes = decode_value (p, &p);
1665 try_holes_info_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
1667 num_holes = try_holes_info_size = 0;
1669 /* Exception table */
1671 int num_clauses = decode_value (p, &p);
1674 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size + try_holes_info_size);
1675 jinfo->num_clauses = num_clauses;
1677 for (i = 0; i < num_clauses; ++i) {
1678 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1680 ei->flags = decode_value (p, &p);
1683 if (decode_value (p, &p))
1684 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1686 /* The rest of the info is in the DWARF EH section */
1690 ei->exvar_offset = decode_value (p, &p);
1692 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
1693 ei->data.filter = code + decode_value (p, &p);
1695 if (decode_value (p, &p))
1696 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1699 ei->try_start = code + decode_value (p, &p);
1700 ei->try_end = code + decode_value (p, &p);
1701 ei->handler_start = code + decode_value (p, &p);
1705 jinfo = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + generic_info_size + try_holes_info_size);
1709 /* LLVM compiled method */
1710 /* The info is in the .eh_frame section */
1711 jinfo = decode_eh_frame (amodule, domain, method, code, jinfo, generic_info_size);
1712 jinfo->from_llvm = 1;
1714 jinfo->code_size = code_len;
1715 jinfo->used_regs = used_int_regs;
1716 jinfo->method = method;
1717 jinfo->code_start = code;
1718 jinfo->domain_neutral = 0;
1719 jinfo->from_aot = 1;
1722 if (has_generic_jit_info) {
1723 MonoGenericJitInfo *gi;
1725 jinfo->has_generic_jit_info = 1;
1727 gi = mono_jit_info_get_generic_jit_info (jinfo);
1730 gi->has_this = decode_value (p, &p);
1731 gi->this_reg = decode_value (p, &p);
1732 gi->this_offset = decode_value (p, &p);
1734 /* This currently contains no data */
1735 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1737 jinfo->method = decode_resolve_method_ref (amodule, p, &p);
1740 if (has_try_block_holes) {
1741 MonoTryBlockHoleTableJitInfo *table;
1743 jinfo->has_try_block_holes = 1;
1745 table = mono_jit_info_get_try_block_hole_table_info (jinfo);
1748 table->num_holes = (guint16)num_holes;
1749 for (i = 0; i < num_holes; ++i) {
1750 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
1751 hole->clause = decode_value (p, &p);
1752 hole->length = decode_value (p, &p);
1753 hole->offset = decode_value (p, &p);
1757 if (has_seq_points) {
1758 MonoSeqPointInfo *seq_points;
1759 int il_offset, native_offset, last_il_offset, last_native_offset, j;
1761 int len = decode_value (p, &p);
1763 seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
1764 seq_points->len = len;
1765 last_il_offset = last_native_offset = 0;
1766 for (i = 0; i < len; ++i) {
1767 SeqPoint *sp = &seq_points->seq_points [i];
1768 il_offset = last_il_offset + decode_value (p, &p);
1769 native_offset = last_native_offset + decode_value (p, &p);
1771 sp->il_offset = il_offset;
1772 sp->native_offset = native_offset;
1774 sp->next_len = decode_value (p, &p);
1775 sp->next = g_new (int, sp->next_len);
1776 for (j = 0; j < sp->next_len; ++j)
1777 sp->next [j] = decode_value (p, &p);
1779 last_il_offset = il_offset;
1780 last_native_offset = native_offset;
1783 mono_domain_lock (domain);
1784 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
1785 mono_domain_unlock (domain);
1788 /* Load debug info */
1789 buf_len = decode_value (p, &p);
1790 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1792 if (amodule != jinfo->method->klass->image->aot_module) {
1795 ji_to_amodule = g_hash_table_new (NULL, NULL);
1796 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
1804 * mono_aot_get_unwind_info:
1806 * Return a pointer to the DWARF unwind info belonging to JI.
1809 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
1811 MonoAotModule *amodule = ji->method->klass->image->aot_module;
1813 guint8 *code = ji->code_start;
1816 g_assert (ji->from_aot);
1818 if (!(code >= amodule->code && code <= amodule->code_end)) {
1819 /* ji belongs to a different aot module than amodule */
1821 g_assert (ji_to_amodule);
1822 amodule = g_hash_table_lookup (ji_to_amodule, ji);
1824 g_assert (code >= amodule->code && code <= amodule->code_end);
1828 p = amodule->unwind_info + ji->used_regs;
1829 *unwind_info_len = decode_value (p, &p);
1833 static G_GNUC_UNUSED int
1834 compare_ints (const void *a, const void *b)
1836 return *(gint32*)a - *(gint32*)b;
1840 msort_code_offsets_internal (gint32 *array, int lo, int hi, gint32 *scratch)
1842 int mid = (lo + hi) / 2;
1849 for (i = lo; i < hi; ++i)
1850 if (array [(i * 2)] > array [(i * 2) + 2])
1853 /* Already sorted */
1857 msort_code_offsets_internal (array, lo, mid, scratch);
1858 msort_code_offsets_internal (array, mid + 1, hi, scratch);
1860 if (array [mid * 2] < array [(mid + 1) * 2])
1866 for (i = lo; i <= hi; i ++) {
1867 if (t_lo <= mid && ((t_hi > hi) || array [t_lo * 2] < array [t_hi * 2])) {
1868 scratch [(i * 2)] = array [t_lo * 2];
1869 scratch [(i * 2) + 1] = array [(t_lo *2) + 1];
1872 scratch [(i * 2)] = array [t_hi * 2];
1873 scratch [(i * 2) + 1] = array [(t_hi *2) + 1];
1877 for (i = lo; i <= hi; ++i) {
1878 array [(i * 2)] = scratch [i * 2];
1879 array [(i * 2) + 1] = scratch [(i * 2) + 1];
1884 msort_code_offsets (gint32 *array, int len)
1888 scratch = g_new (gint32, len * 2);
1889 msort_code_offsets_internal (array, 0, len - 1, scratch);
1894 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1896 int pos, left, right, offset, offset1, offset2, code_len;
1897 int method_index, table_len, is_wrapper;
1899 MonoAotModule *amodule = image->aot_module;
1902 guint8 *code, *ex_info, *p;
1904 int nmethods = amodule->info.nmethods;
1905 gint32 *code_offsets;
1911 if (domain != mono_get_root_domain ())
1915 offset = (guint8*)addr - amodule->code;
1917 /* Compute a sorted table mapping code offsets to method indexes. */
1918 if (!amodule->sorted_code_offsets) {
1920 code_offsets = g_new0 (gint32, nmethods * 2);
1922 for (i = 0; i < nmethods; ++i) {
1923 /* Skip the -1 entries to speed up sorting */
1924 if (amodule->code_offsets [i] == 0xffffffff)
1926 code_offsets [(offsets_len * 2)] = amodule->code_offsets [i];
1927 code_offsets [(offsets_len *2) + 1] = i;
1930 /* Use a merge sort as this is mostly sorted */
1931 msort_code_offsets (code_offsets, offsets_len);
1932 //qsort (code_offsets, offsets_len, sizeof (gint32) * 2, compare_ints);
1933 for (i = 0; i < offsets_len -1; ++i)
1934 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
1936 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
1937 /* Somebody got in before us */
1938 g_free (code_offsets);
1939 amodule->sorted_code_offsets_len = offsets_len;
1942 code_offsets = amodule->sorted_code_offsets;
1943 offsets_len = amodule->sorted_code_offsets_len;
1945 /* Binary search in the sorted_code_offsets table */
1947 right = offsets_len;
1949 pos = (left + right) / 2;
1951 offset1 = code_offsets [(pos * 2)];
1952 if (pos + 1 == offsets_len)
1953 offset2 = amodule->code_end - amodule->code;
1955 offset2 = code_offsets [(pos + 1) * 2];
1957 if (offset < offset1)
1959 else if (offset >= offset2)
1965 g_assert (offset >= code_offsets [(pos * 2)]);
1966 if (pos + 1 < offsets_len)
1967 g_assert (offset < code_offsets [((pos + 1) * 2)]);
1968 method_index = code_offsets [(pos * 2) + 1];
1970 code = &amodule->code [amodule->code_offsets [method_index]];
1971 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
1973 if (pos == offsets_len - 1)
1974 code_len = amodule->code_end - code;
1976 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
1978 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
1980 /* Might be a wrapper/extra method */
1981 if (amodule->extra_methods) {
1983 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
1990 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
1992 * This is hit for extra methods which are called directly, so they are
1993 * not in amodule->extra_methods.
1995 table_len = amodule->extra_method_info_offsets [0];
1996 table = amodule->extra_method_info_offsets + 1;
2003 pos = ((left + right) / 2);
2005 g_assert (pos < table_len);
2007 if (table [pos * 2] < method_index)
2009 else if (table [pos * 2] > method_index)
2015 p = amodule->blob + table [(pos * 2) + 1];
2016 is_wrapper = decode_value (p, &p);
2017 g_assert (!is_wrapper);
2018 method = decode_resolve_method_ref (amodule, p, &p);
2021 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
2022 method = mono_get_method (image, token, NULL);
2029 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
2031 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
2033 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
2034 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
2036 /* Add it to the normal JitInfo tables */
2037 mono_jit_info_table_add (domain, jinfo);
2043 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
2051 case MONO_PATCH_INFO_METHOD:
2052 case MONO_PATCH_INFO_METHOD_JUMP:
2053 case MONO_PATCH_INFO_ICALL_ADDR:
2054 case MONO_PATCH_INFO_METHOD_RGCTX: {
2057 gboolean no_aot_trampoline;
2059 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
2063 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
2064 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (image, token));
2065 ji->type = MONO_PATCH_INFO_ABS;
2069 ji->data.method = method;
2071 ji->data.method = mono_get_method (image, token, NULL);
2072 g_assert (ji->data.method);
2073 mono_class_init (ji->data.method->klass);
2077 case MONO_PATCH_INFO_INTERNAL_METHOD:
2078 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2079 guint32 len = decode_value (p, &p);
2081 ji->data.name = (char*)p;
2085 case MONO_PATCH_INFO_METHODCONST:
2087 ji->data.method = decode_resolve_method_ref (aot_module, p, &p);
2088 if (!ji->data.method)
2091 case MONO_PATCH_INFO_VTABLE:
2092 case MONO_PATCH_INFO_CLASS:
2093 case MONO_PATCH_INFO_IID:
2094 case MONO_PATCH_INFO_ADJUSTED_IID:
2096 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2097 if (!ji->data.klass)
2100 case MONO_PATCH_INFO_CLASS_INIT:
2101 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2102 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2103 if (!ji->data.klass)
2106 case MONO_PATCH_INFO_IMAGE:
2107 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
2108 if (!ji->data.image)
2111 case MONO_PATCH_INFO_FIELD:
2112 case MONO_PATCH_INFO_SFLDA:
2114 ji->data.field = decode_field_info (aot_module, p, &p);
2115 if (!ji->data.field)
2118 case MONO_PATCH_INFO_SWITCH:
2119 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
2120 ji->data.table->table_size = decode_value (p, &p);
2121 table = mono_domain_alloc (mono_domain_get (), sizeof (gpointer) * ji->data.table->table_size);
2122 ji->data.table->table = (MonoBasicBlock**)table;
2123 for (i = 0; i < ji->data.table->table_size; i++)
2124 table [i] = (gpointer)(gssize)decode_value (p, &p);
2126 case MONO_PATCH_INFO_R4: {
2129 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
2130 val = decode_value (p, &p);
2131 *(float*)ji->data.target = *(float*)&val;
2134 case MONO_PATCH_INFO_R8: {
2138 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
2140 val [0] = decode_value (p, &p);
2141 val [1] = decode_value (p, &p);
2142 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
2143 *(double*)ji->data.target = *(double*)&v;
2146 case MONO_PATCH_INFO_LDSTR:
2147 image = load_image (aot_module, decode_value (p, &p), TRUE);
2150 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
2152 case MONO_PATCH_INFO_RVA:
2153 case MONO_PATCH_INFO_DECLSEC:
2154 case MONO_PATCH_INFO_LDTOKEN:
2155 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2157 image = load_image (aot_module, decode_value (p, &p), TRUE);
2160 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
2162 ji->data.token->has_context = decode_value (p, &p);
2163 if (ji->data.token->has_context) {
2164 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
2169 case MONO_PATCH_INFO_EXC_NAME:
2170 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2171 if (!ji->data.klass)
2173 ji->data.name = ji->data.klass->name;
2175 case MONO_PATCH_INFO_METHOD_REL:
2176 ji->data.offset = decode_value (p, &p);
2178 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2179 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2180 case MONO_PATCH_INFO_MONITOR_ENTER:
2181 case MONO_PATCH_INFO_MONITOR_EXIT:
2183 case MONO_PATCH_INFO_RGCTX_FETCH: {
2185 MonoJumpInfoRgctxEntry *entry;
2187 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2188 entry->method = decode_resolve_method_ref (aot_module, p, &p);
2189 entry->in_mrgctx = decode_value (p, &p);
2190 entry->info_type = decode_value (p, &p);
2191 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2192 entry->data->type = decode_value (p, &p);
2194 res = decode_patch (aot_module, mp, entry->data, p, &p);
2197 ji->data.rgctx_entry = entry;
2200 case MONO_PATCH_INFO_SEQ_POINT_INFO:
2202 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
2203 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
2205 imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
2206 imt_tramp->vt_offset = decode_value (p, &p);
2208 ji->data.imt_tramp = imt_tramp;
2212 g_warning ("unhandled type %d", ji->type);
2213 g_assert_not_reached ();
2224 static MonoJumpInfo*
2225 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
2226 guint32 **got_slots,
2227 guint8 *buf, guint8 **endbuf)
2229 MonoJumpInfo *patches;
2235 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
2237 *got_slots = g_malloc (sizeof (guint32) * n_patches);
2239 for (pindex = 0; pindex < n_patches; ++pindex) {
2240 MonoJumpInfo *ji = &patches [pindex];
2245 got_offset = decode_value (p, &p);
2247 if (aot_module->got [got_offset]) {
2248 /* Already loaded */
2249 //printf ("HIT!\n");
2251 shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
2253 ji->type = decode_value (shared_p, &shared_p);
2255 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
2260 (*got_slots) [pindex] = got_offset;
2267 g_free (*got_slots);
2274 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
2277 * Jump addresses cannot be patched by the trampoline code since it
2278 * does not have access to the caller's address. Instead, we collect
2279 * the addresses of the GOT slots pointing to a method, and patch
2280 * them after the method has been compiled.
2282 MonoJitDomainInfo *info = domain_jit_info (domain);
2285 mono_domain_lock (domain);
2286 if (!info->jump_target_got_slot_hash)
2287 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
2288 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
2289 list = g_slist_prepend (list, got_slot);
2290 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
2291 mono_domain_unlock (domain);
2297 * Load the method identified by METHOD_INDEX from the AOT image. Return a
2298 * pointer to the native code of the method, or NULL if not found.
2299 * METHOD might not be set if the caller only has the image/token info.
2302 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
2305 gboolean from_plt = method == NULL;
2307 int i, pindex, n_patches, used_strings;
2308 gboolean keep_patches = TRUE;
2310 MonoJitInfo *jinfo = NULL;
2311 guint8 *code, *info;
2313 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2316 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
2317 /* Non shared AOT code can't be used in other appdomains */
2320 if (amodule->out_of_date)
2323 if (amodule->code_offsets [method_index] == 0xffffffff) {
2324 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2328 method = mono_get_method (image, token, NULL);
2329 full_name = mono_method_full_name (method, TRUE);
2330 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2336 code = &amodule->code [amodule->code_offsets [method_index]];
2338 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
2341 if (!amodule->methods_loaded)
2342 amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods + 1);
2345 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2348 if (mono_last_aot_method != -1) {
2349 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
2351 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
2353 method = mono_get_method (image, token, NULL);
2355 char *name = mono_method_full_name (method, TRUE);
2356 printf ("LAST AOT METHOD: %s.\n", name);
2359 printf ("LAST AOT METHOD: %p %d\n", code, method_index);
2367 klass = method->klass;
2368 decode_klass_ref (amodule, p, &p);
2370 klass = decode_klass_ref (amodule, p, &p);
2373 if (amodule->info.opts & MONO_OPT_SHARED)
2374 used_strings = decode_value (p, &p);
2378 for (i = 0; i < used_strings; i++) {
2379 guint token = decode_value (p, &p);
2380 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2383 if (amodule->info.opts & MONO_OPT_SHARED)
2384 keep_patches = FALSE;
2386 n_patches = decode_value (p, &p);
2388 keep_patches = FALSE;
2391 MonoJumpInfo *patches;
2397 mp = mono_mempool_new ();
2399 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2400 if (patches == NULL)
2403 for (pindex = 0; pindex < n_patches; ++pindex) {
2404 MonoJumpInfo *ji = &patches [pindex];
2406 if (!amodule->got [got_slots [pindex]]) {
2407 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2408 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2409 amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
2410 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2411 register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
2413 ji->type = MONO_PATCH_INFO_NONE;
2419 mono_mempool_destroy (mp);
2422 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2426 method = mono_get_method (image, token, NULL);
2428 full_name = mono_method_full_name (method, TRUE);
2431 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
2433 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);
2439 mono_jit_stats.methods_aot++;
2441 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2445 if (method && method->wrapper_type)
2446 g_hash_table_insert (amodule->method_to_code, method, code);
2450 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
2454 method = mono_get_method (image, token, NULL);
2457 mono_profiler_method_jit (method);
2458 jinfo = mono_jit_info_table_find (domain, (char*)code);
2460 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
2463 if (from_plt && klass && !klass->generic_container)
2464 mono_runtime_class_init (mono_class_vtable (domain, klass));
2469 /* FIXME: The space in domain->mp is wasted */
2470 if (amodule->info.opts & MONO_OPT_SHARED)
2471 /* No need to cache patches */
2472 mono_mempool_destroy (mp);
2481 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name)
2483 guint32 table_size, entry_size, hash;
2484 guint32 *table, *entry;
2486 static guint32 n_extra_decodes;
2491 table_size = amodule->extra_method_table [0];
2492 table = amodule->extra_method_table + 1;
2495 hash = mono_aot_method_hash (method) % table_size;
2497 entry = &table [hash * entry_size];
2504 guint32 key = entry [0];
2505 guint32 value = entry [1];
2506 guint32 next = entry [entry_size - 1];
2509 int is_wrapper_name;
2511 p = amodule->blob + key;
2512 is_wrapper_name = decode_value (p, &p);
2513 if (is_wrapper_name) {
2514 int wrapper_type = decode_value (p, &p);
2515 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2519 } else if (can_method_ref_match_method (amodule, p, method)) {
2521 if (!amodule->method_ref_to_method)
2522 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2523 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2527 m = decode_resolve_method_ref (amodule, p, &p);
2530 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2539 /* Special case: wrappers of shared generic methods */
2540 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2541 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2542 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2543 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2545 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2551 /* Methods decoded needlessly */
2554 printf ("%d %s %s\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
2560 entry = &table [next * entry_size];
2569 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2571 g_ptr_array_add ((GPtrArray*)user_data, value);
2575 * find_extra_method:
2577 * Try finding METHOD in the extra_method table in all AOT images.
2578 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2579 * module where the method was found.
2582 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2589 if (method->wrapper_type)
2590 name = mono_aot_wrapper_name (method);
2592 /* Try the method's module first */
2593 *out_amodule = method->klass->image->aot_module;
2594 index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name);
2595 if (index != 0xffffff) {
2601 * Try all other modules.
2602 * This is needed because generic instances klass->image points to the image
2603 * containing the generic definition, but the native code is generated to the
2604 * AOT image which contains the reference.
2607 /* Make a copy to avoid doing the search inside the aot lock */
2608 modules = g_ptr_array_new ();
2610 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2614 for (i = 0; i < modules->len; ++i) {
2615 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2617 if (amodule != method->klass->image->aot_module)
2618 index = find_extra_method_in_amodule (amodule, method, name);
2619 if (index != 0xffffff) {
2620 *out_amodule = amodule;
2625 g_ptr_array_free (modules, TRUE);
2632 * mono_aot_get_method:
2634 * Return a pointer to the AOTed native code for METHOD if it can be found,
2636 * On platforms with function pointers, this doesn't return a function pointer.
2639 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2641 MonoClass *klass = method->klass;
2642 guint32 method_index;
2643 MonoAotModule *amodule = klass->image->aot_module;
2649 if (amodule->out_of_date)
2652 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2653 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2654 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2655 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2659 * Use the original method instead of its invoke-with-check wrapper.
2660 * This is not a problem when using full-aot, since it doesn't support
2663 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
2664 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
2666 g_assert (klass->inited);
2668 /* Find method index */
2669 if (method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE)) {
2671 * For generic methods, we store the fully shared instance in place of the
2674 method = mono_method_get_declaring_generic_method (method);
2675 method_index = mono_metadata_token_index (method->token) - 1;
2676 } else if (method->is_inflated || !method->token) {
2677 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2679 code = g_hash_table_lookup (amodule->method_to_code, method);
2684 method_index = find_extra_method (method, &amodule);
2686 * Special case the ICollection<T> wrappers for arrays, as they cannot
2687 * be statically enumerated, and each wrapper ends up calling the same
2690 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
2691 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
2693 code = mono_aot_get_method (domain, m);
2695 if (mono_method_needs_static_rgctx_invoke (m, FALSE)) {
2696 code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code));
2697 /* The call above returns an ftnptr */
2698 code = mono_get_addr_from_ftnptr (code);
2706 * Special case Array.GetGenericValueImpl which is a generic icall.
2707 * Generic sharing currently can't handle it, but the icall returns data using
2708 * an out parameter, so the managed-to-native wrappers can share the same code.
2710 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
2712 MonoGenericContext ctx;
2713 MonoType *args [16];
2715 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
2716 /* Avoid recursion */
2719 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
2722 memset (&ctx, 0, sizeof (ctx));
2723 args [0] = &mono_defaults.object_class->byval_arg;
2724 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2726 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2729 * Get the code for the <object> instantiation which should be emitted into
2730 * the mscorlib aot image by the AOT compiler.
2732 code = mono_aot_get_method (domain, m);
2737 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, TRUE)) {
2738 /* Partial sharing */
2739 method_index = find_extra_method (mini_get_shared_method (method), &amodule);
2742 if (method_index == 0xffffff) {
2743 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2746 full_name = mono_method_full_name (method, TRUE);
2747 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2753 if (method_index == 0xffffff)
2756 /* Needed by find_jit_info */
2758 if (!amodule->extra_methods)
2759 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2760 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2764 method_index = mono_metadata_token_index (method->token) - 1;
2767 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2771 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2775 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2777 MonoAotModule *aot_module = image->aot_module;
2783 method_index = mono_metadata_token_index (token) - 1;
2785 return load_method (domain, aot_module, image, NULL, token, method_index);
2791 } IsGotEntryUserData;
2794 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2796 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2797 MonoAotModule *aot_module = (MonoAotModule*)value;
2799 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
2804 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2806 IsGotEntryUserData user_data;
2811 user_data.addr = addr;
2812 user_data.res = FALSE;
2814 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2817 return user_data.res;
2822 MonoAotModule *module;
2823 } FindAotModuleUserData;
2826 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2828 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2829 MonoAotModule *aot_module = (MonoAotModule*)value;
2831 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2832 data->module = aot_module;
2835 static inline MonoAotModule*
2836 find_aot_module (guint8 *code)
2838 FindAotModuleUserData user_data;
2843 /* Reading these need no locking */
2844 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2847 user_data.addr = code;
2848 user_data.module = NULL;
2851 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2854 return user_data.module;
2858 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
2861 * Since AOT code is only used in the root domain,
2862 * mono_domain_get () != mono_get_root_domain () means the calling method
2863 * is AppDomain:InvokeInDomain, so this is the same check as in
2864 * mono_method_same_domain () but without loading the metadata for the method.
2866 if (mono_domain_get () == mono_get_root_domain ())
2867 mono_arch_patch_plt_entry (code, got, regs, addr);
2871 * mono_aot_plt_resolve:
2873 * This function is called by the entries in the PLT to resolve the actual method that
2874 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2875 * Returns NULL if the something cannot be loaded.
2878 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2880 #ifdef MONO_ARCH_AOT_SUPPORTED
2881 guint8 *p, *target, *plt_entry;
2883 MonoAotModule *module = (MonoAotModule*)aot_module;
2884 gboolean res, no_ftnptr = FALSE;
2887 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2889 p = &module->blob [plt_info_offset];
2891 ji.type = decode_value (p, &p);
2893 mp = mono_mempool_new_size (512);
2894 res = decode_patch (module, mp, &ji, p, &p);
2897 mono_mempool_destroy (mp);
2902 * Avoid calling resolve_patch_target in the full-aot case if possible, since
2903 * it would create a trampoline, and we don't need that.
2904 * We could do this only if the method does not need the special handling
2905 * in mono_magic_trampoline ().
2907 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) &&
2908 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
2909 target = mono_jit_compile_method (ji.data.method);
2912 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2916 * The trampoline expects us to return a function descriptor on platforms which use
2917 * it, but resolve_patch_target returns a direct function pointer for some type of
2918 * patches, so have to translate between the two.
2919 * FIXME: Clean this up, but how ?
2921 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) {
2922 /* These should already have a function descriptor */
2923 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2924 /* Our function descriptors have a 0 environment, gcc created ones don't */
2925 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
2926 g_assert (((gpointer*)target) [2] == 0);
2929 } else if (!no_ftnptr) {
2930 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2931 g_assert (((gpointer*)target) [2] != 0);
2933 target = mono_create_ftnptr (mono_domain_get (), target);
2936 mono_mempool_destroy (mp);
2938 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2939 plt_entry = mono_aot_get_plt_entry (code);
2940 g_assert (plt_entry);
2941 mono_aot_patch_plt_entry (plt_entry, module->got, NULL, target);
2945 g_assert_not_reached ();
2953 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2954 * method in the module is loaded to avoid committing memory by writing to it.
2955 * LOCKING: Assumes the AOT lock is held.
2958 init_plt (MonoAotModule *amodule)
2963 if (amodule->plt_inited)
2966 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2969 * Initialize the PLT entries in the GOT to point to the default targets.
2972 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
2973 for (i = 1; i < amodule->info.plt_size; ++i)
2974 /* All the default entries point to the AOT trampoline */
2975 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = tramp;
2977 amodule->plt_inited = TRUE;
2981 * mono_aot_get_plt_entry:
2983 * Return the address of the PLT entry called by the code at CODE if exists.
2986 mono_aot_get_plt_entry (guint8 *code)
2988 MonoAotModule *aot_module = find_aot_module (code);
2993 #ifdef MONO_ARCH_AOT_SUPPORTED
2995 guint8 *target = mono_arch_get_call_target (code);
2997 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3001 g_assert_not_reached ();
3008 * mono_aot_get_plt_info_offset:
3010 * Return the PLT info offset belonging to the plt entry called by CODE.
3013 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3015 guint8 *plt_entry = mono_aot_get_plt_entry (code);
3017 g_assert (plt_entry);
3019 /* The offset is embedded inside the code after the plt entry */
3020 #ifdef MONO_ARCH_AOT_SUPPORTED
3021 return mono_arch_get_plt_info_offset (plt_entry, regs, code);
3023 g_assert_not_reached ();
3029 mono_create_ftnptr_malloc (guint8 *code)
3031 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3032 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
3034 ftnptr->code = code;
3044 static GHashTable *aot_jit_icall_hash;
3047 * mono_aot_register_jit_icall:
3049 * Register a JIT icall which is called by trampolines in full-aot mode. This should
3050 * be called from mono_arch_init () during startup.
3053 mono_aot_register_jit_icall (const char *name, gpointer addr)
3055 /* No need for locking */
3056 if (!aot_jit_icall_hash)
3057 aot_jit_icall_hash = g_hash_table_new (g_str_hash, g_str_equal);
3058 g_hash_table_insert (aot_jit_icall_hash, (char*)name, addr);
3064 * Load the function named NAME from the aot image.
3067 load_function (MonoAotModule *amodule, const char *name)
3071 int n_patches, pindex;
3077 symbol = g_strdup_printf ("%s", name);
3078 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
3081 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
3083 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
3087 symbol = g_strdup_printf ("%s_p", name);
3088 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
3091 /* Nothing to patch */
3094 p = amodule->blob + *(guint32*)p;
3096 /* Similar to mono_aot_load_method () */
3098 n_patches = decode_value (p, &p);
3101 MonoJumpInfo *patches;
3104 mp = mono_mempool_new ();
3106 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
3109 for (pindex = 0; pindex < n_patches; ++pindex) {
3110 MonoJumpInfo *ji = &patches [pindex];
3113 if (amodule->got [got_slots [pindex]])
3117 * When this code is executed, the runtime may not be initalized yet, so
3118 * resolve the patch info by hand.
3120 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3121 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
3122 target = mono_get_lmf_addr;
3123 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
3124 target = mono_thread_force_interruption_checkpoint;
3125 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
3126 target = mono_exception_from_token;
3127 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
3128 target = mono_get_throw_exception ();
3129 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
3130 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
3131 target = (gpointer)mono_get_trampoline_func (tramp_type2);
3132 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
3133 /* atoll is needed because the the offset is unsigned */
3137 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
3138 g_assert (res == 1);
3139 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
3140 target = mono_create_ftnptr_malloc (target);
3141 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
3142 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
3143 target = mono_create_ftnptr_malloc (target);
3144 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
3145 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
3146 target = mono_create_ftnptr_malloc (target);
3147 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
3148 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
3149 target = mono_create_ftnptr_malloc (target);
3150 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
3151 target = mono_thread_get_and_clear_pending_exception;
3152 } else if (strstr (ji->data.name, "generic_trampoline_monitor_enter")) {
3155 symbol = g_strdup_printf ("generic_trampoline_%d", MONO_TRAMPOLINE_MONITOR_ENTER);
3156 target = mono_aot_get_trampoline (symbol);
3158 } else if (strstr (ji->data.name, "generic_trampoline_monitor_exit")) {
3161 symbol = g_strdup_printf ("generic_trampoline_%d", MONO_TRAMPOLINE_MONITOR_EXIT);
3162 target = mono_aot_get_trampoline (symbol);
3164 } else if (strstr (ji->data.name, "generic_trampoline_generic_class_init")) {
3167 symbol = g_strdup_printf ("generic_trampoline_%d", MONO_TRAMPOLINE_GENERIC_CLASS_INIT);
3168 target = mono_aot_get_trampoline (symbol);
3170 } else if (aot_jit_icall_hash && g_hash_table_lookup (aot_jit_icall_hash, ji->data.name)) {
3171 /* Registered by mono_arch_init () */
3172 target = g_hash_table_lookup (aot_jit_icall_hash, ji->data.name);
3174 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
3175 g_assert_not_reached ();
3179 /* Hopefully the code doesn't have patches which need method or
3182 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
3186 amodule->got [got_slots [pindex]] = target;
3191 mono_mempool_destroy (mp);
3198 * Return the trampoline identified by NAME from the mscorlib AOT file.
3199 * On ppc64, this returns a function descriptor.
3202 mono_aot_get_trampoline (const char *name)
3205 MonoAotModule *amodule;
3207 image = mono_defaults.corlib;
3210 amodule = image->aot_module;
3213 return mono_create_ftnptr_malloc (load_function (amodule, name));
3216 /* Return a given kind of trampoline */
3218 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
3220 MonoAotModule *amodule;
3221 int index, tramp_size;
3224 /* Currently, we keep all trampolines in the mscorlib AOT image */
3225 image = mono_defaults.corlib;
3230 amodule = image->aot_module;
3233 *out_amodule = amodule;
3235 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
3236 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
3238 index = amodule->trampoline_index [tramp_type] ++;
3242 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
3244 tramp_size = amodule->info.trampoline_size [tramp_type];
3247 *out_tramp_size = tramp_size;
3249 return amodule->trampolines [tramp_type] + (index * tramp_size);
3253 * Return a specific trampoline from the AOT file.
3256 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3258 MonoAotModule *amodule;
3259 guint32 got_offset, tramp_size;
3260 guint8 *code, *tramp;
3261 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
3262 static gboolean inited;
3263 static guint32 num_trampolines;
3269 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
3278 if (!generic_trampolines [tramp_type]) {
3281 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
3282 generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
3286 tramp = generic_trampolines [tramp_type];
3289 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
3291 amodule->got [got_offset] = tramp;
3292 amodule->got [got_offset + 1] = arg1;
3295 *code_len = tramp_size;
3301 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3303 MonoAotModule *amodule;
3307 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
3309 amodule->got [got_offset] = ctx;
3310 amodule->got [got_offset + 1] = addr;
3312 /* The caller expects an ftnptr */
3313 return mono_create_ftnptr (mono_domain_get (), code);
3317 mono_aot_get_unbox_trampoline (MonoMethod *method)
3319 guint32 method_index = mono_metadata_token_index (method->token) - 1;
3320 MonoAotModule *amodule;
3324 if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
3325 guint32 index = find_extra_method (method, &amodule);
3326 g_assert (index != 0xffffff);
3328 symbol = g_strdup_printf ("ut_e_%d", index);
3330 amodule = method->klass->image->aot_module;
3333 symbol = g_strdup_printf ("ut_%d", method_index);
3335 code = load_function (amodule, symbol);
3338 /* The caller expects an ftnptr */
3339 return mono_create_ftnptr (mono_domain_get (), code);
3343 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3348 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
3349 code = load_function (mono_defaults.corlib->aot_module, symbol);
3351 /* The caller expects an ftnptr */
3352 return mono_create_ftnptr (mono_domain_get (), code);
3356 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3362 MonoAotModule *amodule;
3364 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
3366 /* Save the entries into an array */
3367 buf = mono_domain_alloc (domain, (count + 1) * 2 * sizeof (gpointer));
3368 for (i = 0; i < count; ++i) {
3369 MonoIMTCheckItem *item = imt_entries [i];
3371 g_assert (item->key);
3373 g_assert (!item->has_target_code);
3375 buf [(i * 2)] = item->key;
3376 buf [(i * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
3378 buf [(count * 2)] = NULL;
3379 buf [(count * 2) + 1] = fail_tramp;
3381 amodule->got [got_offset] = buf;
3387 * mono_aot_set_make_unreadable:
3389 * Set whenever to make all mmaped memory unreadable. In conjuction with a
3390 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
3393 mono_aot_set_make_unreadable (gboolean unreadable)
3397 make_unreadable = unreadable;
3399 if (make_unreadable && !inited) {
3400 mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
3405 MonoAotModule *module;
3410 find_map (gpointer key, gpointer value, gpointer user_data)
3412 MonoAotModule *module = (MonoAotModule*)value;
3413 FindMapUserData *data = (FindMapUserData*)user_data;
3416 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
3417 data->module = module;
3420 static MonoAotModule*
3421 find_module_for_addr (void *ptr)
3423 FindMapUserData data;
3425 if (!make_unreadable)
3429 data.ptr = (guint8*)ptr;
3432 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
3439 * mono_aot_is_pagefault:
3441 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
3442 * within memory allocated by this module.
3445 mono_aot_is_pagefault (void *ptr)
3447 if (!make_unreadable)
3451 * Not signal safe, but SIGSEGV's are synchronous, and
3452 * this is only turned on by a MONO_DEBUG option.
3454 return find_module_for_addr (ptr) != NULL;
3458 * mono_aot_handle_pagefault:
3460 * Handle a pagefault caused by an unreadable page by making it readable again.
3463 mono_aot_handle_pagefault (void *ptr)
3465 #ifndef PLATFORM_WIN32
3466 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
3470 res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
3471 g_assert (res == 0);
3482 mono_aot_init (void)
3487 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3493 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3499 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3505 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
3511 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
3517 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3523 mono_aot_get_plt_entry (guint8 *code)
3529 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3535 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
3540 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
3546 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3548 g_assert_not_reached ();
3554 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3556 g_assert_not_reached ();
3561 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3563 g_assert_not_reached ();
3568 mono_aot_get_trampoline (const char *name)
3570 g_assert_not_reached ();
3575 mono_aot_get_unbox_trampoline (MonoMethod *method)
3577 g_assert_not_reached ();
3582 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3584 g_assert_not_reached ();
3589 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3591 g_assert_not_reached ();
3596 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3598 g_assert_not_reached ();
3603 mono_aot_register_jit_icall (const char *name, gpointer addr)