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.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 guint32 *method_info_offsets;
104 guint32 *got_info_offsets;
105 guint32 *ex_info_offsets;
106 guint32 *class_info_offsets;
107 guint32 *methods_loaded;
108 guint16 *class_name_table;
109 guint32 *extra_method_table;
110 guint32 *extra_method_info_offsets;
113 /* Points to the GNU .eh_frame_hdr section, if it exists */
114 guint8 *eh_frame_hdr;
116 /* Points to the .ARM.exidx section, if it exists */
118 guint32 arm_exidx_size;
120 /* Points to the trampolines */
121 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
122 /* The first unused trampoline of each kind */
123 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
125 MonoAotFileInfo info;
131 static GHashTable *aot_modules;
132 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
133 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
134 static CRITICAL_SECTION aot_mutex;
137 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
138 * AOT modules registered by mono_aot_register_module ().
140 static GHashTable *static_aot_modules;
143 * Maps MonoJitInfo* to the aot module they belong to, this can be different
144 * from ji->method->klass->image's aot module for generic instances.
146 static GHashTable *ji_to_amodule;
149 * Whenever to AOT compile loaded assemblies on demand and store them in
150 * a cache under $HOME/.mono/aot-cache.
152 static gboolean use_aot_cache = FALSE;
155 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
156 * use_aot_cache is TRUE.
158 static gboolean spawn_compiler = TRUE;
161 static gint32 mono_last_aot_method = -1;
163 static gboolean make_unreadable = FALSE;
164 static guint32 name_table_accesses = 0;
165 static guint32 n_pagefaults = 0;
167 /* Used to speed-up find_aot_module () */
168 static gsize aot_code_low_addr = (gssize)-1;
169 static gsize aot_code_high_addr = 0;
172 init_plt (MonoAotModule *info);
174 /*****************************************************/
176 /*****************************************************/
181 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
182 * found, and sets the loader error if SET_ERROR is TRUE.
185 load_image (MonoAotModule *amodule, int index, gboolean set_error)
187 MonoAssembly *assembly;
188 MonoImageOpenStatus status;
190 g_assert (index < amodule->image_table_len);
192 if (amodule->image_table [index])
193 return amodule->image_table [index];
194 if (amodule->out_of_date)
197 assembly = mono_assembly_load (&amodule->image_names [index], NULL, &status);
199 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);
200 amodule->out_of_date = TRUE;
203 char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
204 mono_loader_set_error_assembly_load (full_name, FALSE);
210 if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
211 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);
212 amodule->out_of_date = TRUE;
216 amodule->image_table [index] = assembly->image;
217 return assembly->image;
221 decode_value (guint8 *ptr, guint8 **rptr)
226 if ((b & 0x80) == 0){
229 } else if ((b & 0x40) == 0){
230 len = ((b & 0x3f) << 8 | ptr [1]);
232 } else if (b != 0xff) {
233 len = ((b & 0x1f) << 24) |
240 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
246 //printf ("DECODE: %d.\n", len);
251 * mono_aot_get_method:
253 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
257 mono_aot_get_offset (guint32 *table, int index)
259 int i, group, ngroups, index_entry_size;
260 int start_offset, offset, noffsets, group_size;
261 guint8 *data_start, *p;
262 guint32 *index32 = NULL;
263 guint16 *index16 = NULL;
265 noffsets = table [0];
266 group_size = table [1];
268 index_entry_size = table [3];
269 group = index / group_size;
271 if (index_entry_size == 2) {
272 index16 = (guint16*)&table [4];
273 data_start = (guint8*)&index16 [ngroups];
274 p = data_start + index16 [group];
276 index32 = (guint32*)&table [4];
277 data_start = (guint8*)&index32 [ngroups];
278 p = data_start + index32 [group];
281 /* offset will contain the value of offsets [group * group_size] */
282 offset = start_offset = decode_value (p, &p);
283 for (i = group * group_size + 1; i <= index; ++i) {
284 offset += decode_value (p, &p);
287 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
293 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
296 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
298 static MonoGenericInst*
299 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
302 MonoType **type_argv;
303 MonoGenericInst *inst;
306 type_argc = decode_value (p, &p);
307 type_argv = g_new0 (MonoType*, type_argc);
309 for (i = 0; i < type_argc; ++i) {
310 MonoClass *pclass = decode_klass_ref (module, p, &p);
315 type_argv [i] = &pclass->byval_arg;
318 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
327 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
329 gboolean has_class_inst, has_method_inst;
332 has_class_inst = decode_value (p, &p);
333 if (has_class_inst) {
334 ctx->class_inst = decode_generic_inst (module, p, &p);
335 if (!ctx->class_inst)
338 has_method_inst = decode_value (p, &p);
339 if (has_method_inst) {
340 ctx->method_inst = decode_generic_inst (module, p, &p);
341 if (!ctx->method_inst)
350 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
353 MonoClass *klass, *eklass;
357 token = decode_value (p, &p);
362 if (mono_metadata_token_table (token) == 0) {
363 image = load_image (module, decode_value (p, &p), TRUE);
366 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
367 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
368 if (token == MONO_TOKEN_TYPE_SPEC) {
369 MonoTypeEnum type = decode_value (p, &p);
371 if (type == MONO_TYPE_GENERICINST) {
373 MonoGenericContext ctx;
376 gclass = decode_klass_ref (module, p, &p);
379 g_assert (gclass->generic_container);
381 memset (&ctx, 0, sizeof (ctx));
382 ctx.class_inst = decode_generic_inst (module, p, &p);
385 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
386 klass = mono_class_from_mono_type (type);
387 mono_metadata_free_type (type);
388 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
390 MonoGenericContainer *container;
392 int num = decode_value (p, &p);
393 gboolean is_method = decode_value (p, &p);
396 MonoMethod *method_def;
397 g_assert (type == MONO_TYPE_MVAR);
398 method_def = decode_method_ref_2 (module, p, &p);
402 container = mono_method_get_generic_container (method_def);
404 MonoClass *class_def;
405 g_assert (type == MONO_TYPE_VAR);
406 class_def = decode_klass_ref (module, p, &p);
410 container = class_def->generic_container;
413 g_assert (container);
415 // FIXME: Memory management
416 t = g_new0 (MonoType, 1);
418 t->data.generic_param = mono_generic_container_get_param (container, num);
420 // FIXME: Maybe use types directly to avoid
421 // the overhead of creating MonoClass-es
422 klass = mono_class_from_mono_type (t);
426 g_assert_not_reached ();
429 image = load_image (module, decode_value (p, &p), TRUE);
432 klass = mono_class_get (image, token);
434 } else if (token == MONO_TOKEN_TYPE_DEF) {
436 image = load_image (module, decode_value (p, &p), TRUE);
439 rank = decode_value (p, &p);
440 eklass = decode_klass_ref (module, p, &p);
441 klass = mono_array_class_get (eklass, rank);
443 g_assert_not_reached ();
451 static MonoClassField*
452 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
454 MonoClass *klass = decode_klass_ref (module, buf, &buf);
461 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
465 return mono_class_get_field (klass, token);
469 * can_method_ref_match_method:
471 * Determine if calling decode_method_ref_2 on P could return the same method as
472 * METHOD. This is an optimization to avoid calling decode_method_ref_2 () which
473 * would create MonoMethods which are not needed etc.
476 can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
479 guint32 image_index, value;
481 /* Keep this in sync with decode_method_ref () */
482 value = decode_value (p, &p);
483 image_index = value >> 24;
485 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
486 guint32 wrapper_type;
488 if (!method->wrapper_type)
491 wrapper_type = decode_value (p, &p);
493 if (method->wrapper_type != wrapper_type)
495 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
497 } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
498 if (method->wrapper_type)
508 * Decode a method reference, and return its image and token. This avoids loading
509 * metadata for the method if the caller does not need it. If the method has no token,
510 * then it is loaded from metadata and METHOD is set to the method instance.
513 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
515 guint32 image_index, value;
516 MonoImage *image = NULL;
521 if (no_aot_trampoline)
522 *no_aot_trampoline = FALSE;
524 value = decode_value (p, &p);
525 image_index = value >> 24;
527 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
528 if (no_aot_trampoline)
529 *no_aot_trampoline = TRUE;
530 value = decode_value (p, &p);
531 image_index = value >> 24;
534 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
535 guint32 wrapper_type;
537 wrapper_type = decode_value (p, &p);
540 image = mono_defaults.corlib;
542 switch (wrapper_type) {
543 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
544 MonoMethod *m = decode_method_ref_2 (module, p, &p);
548 mono_class_init (m->klass);
549 *method = mono_marshal_get_remoting_invoke_with_check (m);
552 case MONO_WRAPPER_PROXY_ISINST: {
553 MonoClass *klass = decode_klass_ref (module, p, &p);
556 *method = mono_marshal_get_proxy_cancast (klass);
559 case MONO_WRAPPER_LDFLD:
560 case MONO_WRAPPER_LDFLDA:
561 case MONO_WRAPPER_STFLD:
562 case MONO_WRAPPER_ISINST: {
563 MonoClass *klass = decode_klass_ref (module, p, &p);
566 if (wrapper_type == MONO_WRAPPER_LDFLD)
567 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
568 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
569 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
570 else if (wrapper_type == MONO_WRAPPER_STFLD)
571 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
572 else if (wrapper_type == MONO_WRAPPER_ISINST)
573 *method = mono_marshal_get_isinst (klass);
575 g_assert_not_reached ();
578 case MONO_WRAPPER_LDFLD_REMOTE:
579 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
581 case MONO_WRAPPER_STFLD_REMOTE:
582 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
584 case MONO_WRAPPER_ALLOC: {
585 int atype = decode_value (p, &p);
587 *method = mono_gc_get_managed_allocator_by_type (atype);
590 case MONO_WRAPPER_WRITE_BARRIER:
591 *method = mono_gc_get_write_barrier ();
593 case MONO_WRAPPER_STELEMREF:
594 *method = mono_marshal_get_stelemref ();
596 case MONO_WRAPPER_SYNCHRONIZED: {
597 MonoMethod *m = decode_method_ref_2 (module, p, &p);
601 *method = mono_marshal_get_synchronized_wrapper (m);
604 case MONO_WRAPPER_UNKNOWN: {
605 MonoMethodDesc *desc;
606 MonoMethod *orig_method;
607 int subtype = decode_value (p, &p);
609 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
610 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
611 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
612 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
614 g_assert_not_reached ();
615 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
616 g_assert (orig_method);
617 mono_method_desc_free (desc);
618 *method = mono_monitor_get_fast_path (orig_method);
621 case MONO_WRAPPER_RUNTIME_INVOKE: {
623 MonoMethod *m = decode_method_ref_2 (module, p, &p);
627 *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 *method = mono_marshal_get_array_address (rank, elem_size);
639 g_assert_not_reached ();
644 g_assert_not_reached ();
646 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
647 /* Can't decode these */
648 g_assert_not_reached ();
649 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
650 image_index = decode_value (p, &p);
651 *token = decode_value (p, &p);
653 image = load_image (module, image_index, TRUE);
656 } else if (image_index == MONO_AOT_METHODREF_GINST) {
658 MonoGenericContext ctx;
661 * These methods do not have a token which resolves them, so we
662 * resolve them immediately.
664 klass = decode_klass_ref (module, p, &p);
668 image_index = decode_value (p, &p);
669 *token = decode_value (p, &p);
671 image = load_image (module, image_index, TRUE);
675 *method = mono_get_method_full (image, *token, NULL, NULL);
679 memset (&ctx, 0, sizeof (ctx));
681 if (FALSE && klass->generic_class) {
682 ctx.class_inst = klass->generic_class->context.class_inst;
683 ctx.method_inst = NULL;
685 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
688 memset (&ctx, 0, sizeof (ctx));
690 if (!decode_generic_context (module, &ctx, p, &p))
693 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
694 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
698 klass = decode_klass_ref (module, p, &p);
701 method_type = decode_value (p, &p);
703 switch (method_type) {
705 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
708 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
711 *method = mono_class_get_method_from_name (klass, "Get", -1);
714 *method = mono_class_get_method_from_name (klass, "Address", -1);
717 *method = mono_class_get_method_from_name (klass, "Set", -1);
720 g_assert_not_reached ();
723 g_assert (image_index < MONO_AOT_METHODREF_MIN);
724 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
726 image = load_image (module, image_index, TRUE);
737 * decode_method_ref_2:
739 * Similar to decode_method_ref, but resolve and return the method itself.
742 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
746 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
752 method = mono_get_method (image, token, NULL);
758 make_writable (guint8* addr, guint32 len)
764 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
766 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1));
767 pages = (addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ();
769 err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_READ | MONO_MMAP_WRITE | MONO_MMAP_EXEC);
774 create_cache_structure (void)
780 home = g_get_home_dir ();
784 tmp = g_build_filename (home, ".mono", NULL);
785 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
786 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
790 err = mkdir (tmp, 0777);
793 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
799 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
800 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
801 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
805 err = mkdir (tmp, 0777);
808 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
817 * load_aot_module_from_cache:
819 * Experimental code to AOT compile loaded assemblies on demand.
822 * - Add environment variable MONO_AOT_CACHE_OPTIONS
823 * - Add options for controlling the cache size
824 * - Handle full cache by deleting old assemblies lru style
825 * - Add options for excluding assemblies during development
826 * - Maybe add a threshold after an assembly is AOT compiled
827 * - invoking a new mono process is a security risk
828 * - recompile the AOT module if one of its dependencies changes
831 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
833 char *fname, *cmd, *tmp2, *aot_options;
842 if (assembly->image->dynamic)
845 create_cache_structure ();
847 home = g_get_home_dir ();
849 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
850 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
854 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
855 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
858 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
860 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
862 aot_options = g_strdup_printf ("outfile=%s", fname);
864 if (spawn_compiler) {
865 /* FIXME: security */
866 /* FIXME: Has to pass the assembly loading path to the child process */
867 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
869 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
871 #if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
873 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
874 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
876 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
883 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
885 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
887 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
891 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
893 g_free (aot_options);
900 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
904 guint16 *table, *entry;
908 /* The first entry points to the hash */
912 table_size = table [0];
915 hash = mono_metadata_str_hash (name) % table_size;
917 entry = &table [hash * 2];
919 /* Search the hash for the index into the globals table */
921 while (entry [0] != 0) {
922 guint32 index = entry [0] - 1;
923 guint32 next = entry [1];
925 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
927 if (!strcmp (globals [index * 2], name)) {
928 global_index = index;
933 entry = &table [next * 2];
939 if (global_index != -1)
940 *value = globals [global_index * 2 + 1];
944 mono_dl_symbol (module, name, value);
948 #ifndef SHT_ARM_EXIDX
949 #define SHT_ARM_EXIDX 0x70000001
952 #ifdef HAVE_DL_ITERATE_PHDR
954 dl_callback (struct dl_phdr_info *info, size_t size, void *data)
957 MonoAotModule *amodule = data;
959 if (!strcmp (amodule->aot_name, info->dlpi_name)) {
960 for (j = 0; j < info->dlpi_phnum; j++) {
961 if (info->dlpi_phdr [j].p_type == PT_GNU_EH_FRAME)
962 amodule->eh_frame_hdr = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
963 if (info->dlpi_phdr [j].p_type == SHT_ARM_EXIDX) {
964 amodule->arm_exidx = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
965 amodule->arm_exidx_size = info->dlpi_phdr [j].p_filesz;
976 load_aot_module (MonoAssembly *assembly, gpointer user_data)
979 MonoAotModule *amodule;
981 gboolean usable = TRUE;
982 char *saved_guid = NULL;
983 char *aot_version = NULL;
984 char *runtime_version, *build_info;
985 char *opt_flags = NULL;
987 gboolean full_aot = FALSE;
988 MonoAotFileInfo *file_info = NULL;
992 if (mono_compile_aot)
995 if (assembly->image->aot_module)
997 * Already loaded. This can happen because the assembly loading code might invoke
998 * the assembly load hooks multiple times for the same assembly.
1002 if (assembly->image->dynamic)
1005 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
1009 if (static_aot_modules)
1010 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
1016 /* Statically linked AOT module */
1018 aot_name = g_strdup_printf ("%s", assembly->aname.name);
1019 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
1022 sofile = load_aot_module_from_cache (assembly, &aot_name);
1025 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
1027 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1030 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
1036 if (!sofile && !globals) {
1037 if (mono_aot_only) {
1038 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1045 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
1046 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
1047 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
1048 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
1049 find_symbol (sofile, globals, "mono_aot_got_addr", (gpointer *)&got_addr);
1051 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
1052 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);
1056 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
1057 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
1062 build_info = mono_get_runtime_build_info ();
1063 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
1064 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);
1067 g_free (build_info);
1069 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
1070 g_assert (file_info);
1072 full_aot = ((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1074 if (mono_aot_only && !full_aot) {
1075 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
1078 if (!mono_aot_only && full_aot) {
1079 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
1083 if ((((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1084 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with LLVM.\n", aot_name);
1089 if (mono_aot_only) {
1090 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
1095 mono_dl_close (sofile);
1096 assembly->image->aot_module = NULL;
1100 amodule = g_new0 (MonoAotModule, 1);
1101 amodule->aot_name = aot_name;
1102 amodule->assembly = assembly;
1104 memcpy (&amodule->info, file_info, sizeof (*file_info));
1106 amodule->got = *got_addr;
1107 amodule->got [0] = assembly->image;
1108 amodule->globals = globals;
1109 amodule->sofile = sofile;
1110 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1112 /* Read image table */
1114 guint32 table_len, i;
1117 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
1120 table_len = *(guint32*)table;
1121 table += sizeof (guint32);
1122 amodule->image_table = g_new0 (MonoImage*, table_len);
1123 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1124 amodule->image_guids = g_new0 (char*, table_len);
1125 amodule->image_table_len = table_len;
1126 for (i = 0; i < table_len; ++i) {
1127 MonoAssemblyName *aname = &(amodule->image_names [i]);
1129 aname->name = g_strdup (table);
1130 table += strlen (table) + 1;
1131 amodule->image_guids [i] = g_strdup (table);
1132 table += strlen (table) + 1;
1134 aname->culture = g_strdup (table);
1135 table += strlen (table) + 1;
1136 memcpy (aname->public_key_token, table, strlen (table) + 1);
1137 table += strlen (table) + 1;
1139 table = ALIGN_PTR_TO (table, 8);
1140 aname->flags = *(guint32*)table;
1142 aname->major = *(guint32*)table;
1144 aname->minor = *(guint32*)table;
1146 aname->build = *(guint32*)table;
1148 aname->revision = *(guint32*)table;
1153 /* Read method and method_info tables */
1154 find_symbol (sofile, globals, "code_offsets", (gpointer*)&amodule->code_offsets);
1155 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
1156 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
1157 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
1158 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
1159 find_symbol (sofile, globals, "blob", (gpointer*)&amodule->blob);
1160 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
1161 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
1162 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
1163 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1164 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1165 find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC]));
1166 find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX]));
1167 find_symbol (sofile, globals, "imt_thunks", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK]));
1168 find_symbol (sofile, globals, "unwind_info", (gpointer)&amodule->unwind_info);
1169 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1171 amodule->mem_begin = amodule->code;
1173 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1174 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1176 if (make_unreadable) {
1177 #ifndef TARGET_WIN32
1179 guint8 *page_start, *page_end;
1182 addr = amodule->mem_begin;
1183 len = amodule->mem_end - amodule->mem_begin;
1185 /* Round down in both directions to avoid modifying data which is not ours */
1186 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1187 page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
1188 if (page_end > page_start) {
1189 err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
1190 g_assert (err == 0);
1197 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1198 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1200 g_hash_table_insert (aot_modules, assembly, amodule);
1203 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1205 assembly->image->aot_module = amodule;
1207 #ifdef HAVE_DL_ITERATE_PHDR
1208 /* Lookup the address of the .eh_frame_hdr () section if available */
1209 dl_iterate_phdr (dl_callback, amodule);
1212 if (mono_aot_only) {
1213 if (mono_defaults.corlib) {
1214 /* The second got slot contains the mscorlib got addr */
1215 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1217 amodule->got [1] = mscorlib_amodule->got;
1219 amodule->got [1] = amodule->got;
1224 * Since we store methoddef and classdef tokens when referring to methods/classes in
1225 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1226 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1227 * non-lazily, since we can't handle out-of-date errors later.
1228 * The cached class info also depends on the exact assemblies.
1230 for (i = 0; i < amodule->image_table_len; ++i)
1231 load_image (amodule, i, FALSE);
1233 if (amodule->out_of_date) {
1234 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);
1235 if (mono_aot_only) {
1236 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);
1241 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1245 * mono_aot_register_globals:
1247 * This is called by the ctor function in AOT images compiled with the
1248 * 'no-dlsym' option.
1251 mono_aot_register_globals (gpointer *globals)
1253 g_assert_not_reached ();
1257 * mono_aot_register_module:
1259 * This should be called by embedding code to register AOT modules statically linked
1260 * into the executable. AOT_INFO should be the value of the
1261 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1264 mono_aot_register_module (gpointer *aot_info)
1272 /* Determine the assembly name */
1273 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1276 /* This could be called before startup */
1280 if (!static_aot_modules)
1281 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1283 g_hash_table_insert (static_aot_modules, aname, globals);
1290 mono_aot_init (void)
1292 InitializeCriticalSection (&aot_mutex);
1293 aot_modules = g_hash_table_new (NULL, NULL);
1295 mono_install_assembly_load_hook (load_aot_module, NULL);
1297 if (g_getenv ("MONO_LASTAOT"))
1298 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1299 if (g_getenv ("MONO_AOT_CACHE"))
1300 use_aot_cache = TRUE;
1304 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1308 info->vtable_size = decode_value (buf, &buf);
1309 if (info->vtable_size == -1)
1312 flags = decode_value (buf, &buf);
1313 info->ghcimpl = (flags >> 0) & 0x1;
1314 info->has_finalize = (flags >> 1) & 0x1;
1315 info->has_cctor = (flags >> 2) & 0x1;
1316 info->has_nested_classes = (flags >> 3) & 0x1;
1317 info->blittable = (flags >> 4) & 0x1;
1318 info->has_references = (flags >> 5) & 0x1;
1319 info->has_static_refs = (flags >> 6) & 0x1;
1320 info->no_special_static_fields = (flags >> 7) & 0x1;
1321 info->is_generic_container = (flags >> 8) & 0x1;
1323 if (info->has_cctor) {
1324 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1328 if (info->has_finalize) {
1329 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1330 if (!info->finalize_image)
1334 info->instance_size = decode_value (buf, &buf);
1335 info->class_size = decode_value (buf, &buf);
1336 info->packing_size = decode_value (buf, &buf);
1337 info->min_align = decode_value (buf, &buf);
1345 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1348 MonoClass *klass = vtable->klass;
1349 MonoAotModule *aot_module = klass->image->aot_module;
1351 MonoCachedClassInfo class_info;
1355 gboolean no_aot_trampoline;
1357 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1360 info = &aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1363 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1367 for (i = 0; i < slot; ++i)
1368 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1370 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1373 if (no_aot_trampoline)
1376 if (mono_metadata_token_index (token) == 0)
1379 return mono_aot_get_method_from_token (domain, image, token);
1383 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1385 MonoAotModule *aot_module = klass->image->aot_module;
1389 if (klass->rank || !aot_module)
1392 p = (guint8*)&aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1394 err = decode_cached_class_info (aot_module, res, p, &p);
1402 * mono_aot_get_class_from_name:
1404 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1405 * using a cache stored in the AOT file.
1406 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1408 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1412 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1414 MonoAotModule *aot_module = image->aot_module;
1415 guint16 *table, *entry;
1418 char full_name_buf [1024];
1420 const char *name2, *name_space2;
1422 guint32 cols [MONO_TYPEDEF_SIZE];
1423 GHashTable *nspace_table;
1425 if (!aot_module || !aot_module->class_name_table)
1432 /* First look in the cache */
1433 if (!aot_module->name_cache)
1434 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1435 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1437 *klass = g_hash_table_lookup (nspace_table, name);
1444 table_size = aot_module->class_name_table [0];
1445 table = aot_module->class_name_table + 1;
1447 if (name_space [0] == '\0')
1448 full_name = g_strdup_printf ("%s", name);
1450 if (strlen (name_space) + strlen (name) < 1000) {
1451 sprintf (full_name_buf, "%s.%s", name_space, name);
1452 full_name = full_name_buf;
1454 full_name = g_strdup_printf ("%s.%s", name_space, name);
1457 hash = mono_metadata_str_hash (full_name) % table_size;
1458 if (full_name != full_name_buf)
1461 entry = &table [hash * 2];
1463 if (entry [0] != 0) {
1464 t = &image->tables [MONO_TABLE_TYPEDEF];
1467 guint32 index = entry [0];
1468 guint32 next = entry [1];
1469 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1471 name_table_accesses ++;
1473 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1475 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1476 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1478 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1480 *klass = mono_class_get (image, token);
1485 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1486 if (!nspace_table) {
1487 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1488 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1490 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1497 entry = &table [next * 2];
1509 #define DW_EH_PE_omit 0xff
1510 #define DW_EH_PE_uleb128 0x01
1511 #define DW_EH_PE_udata2 0x02
1512 #define DW_EH_PE_udata4 0x03
1513 #define DW_EH_PE_udata8 0x04
1514 #define DW_EH_PE_sleb128 0x09
1515 #define DW_EH_PE_sdata2 0x0A
1516 #define DW_EH_PE_sdata4 0x0B
1517 #define DW_EH_PE_sdata8 0x0C
1519 #define DW_EH_PE_absptr 0x00
1520 #define DW_EH_PE_pcrel 0x10
1521 #define DW_EH_PE_datarel 0x30
1522 #define DW_EH_PE_omit 0xff
1527 guint8 eh_frame_ptr_enc;
1528 guint8 fde_count_enc;
1536 * Decode the exception handling information in the .eh_frame section of the AOT
1537 * file belong to CODE, and construct a MonoJitInfo structure from it.
1538 * LOCKING: Acquires the domain lock.
1540 static G_GNUC_UNUSED MonoJitInfo*
1541 decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
1542 MonoMethod *method, guint8 *code, MonoJitInfo *orig_jinfo,
1547 guint8 *eh_frame, *unwind_info;
1548 guint32 eh_frame_ptr;
1551 int i, pos, left, right, offset, offset1, offset2;
1552 guint32 unw_len, code_len;
1553 MonoJitExceptionInfo *ei;
1555 gpointer *type_info;
1558 g_assert (amodule->eh_frame_hdr);
1560 // http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
1561 hdr = (eh_frame_hdr*)amodule->eh_frame_hdr;
1562 g_assert (hdr->version == 1);
1563 g_assert (hdr->eh_frame_ptr_enc == (DW_EH_PE_pcrel | DW_EH_PE_sdata4));
1564 g_assert (hdr->fde_count_enc == DW_EH_PE_udata4);
1565 g_assert (hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4));
1568 eh_frame_ptr = *(guint32*)p;
1570 fde_count = *(guint32*)p;
1574 /* Binary search in the table to find the entry for code */
1575 offset = code - amodule->eh_frame_hdr;
1580 pos = (left + right) / 2;
1582 offset1 = table [(pos * 2)];
1583 if (pos + 1 == fde_count)
1585 offset2 = amodule->code_end - amodule->code;
1587 offset2 = table [(pos + 1) * 2];
1589 if (offset < offset1)
1591 else if (offset >= offset2)
1597 g_assert (code >= amodule->eh_frame_hdr + table [(pos * 2)]);
1598 if (pos < fde_count)
1599 g_assert (code < amodule->eh_frame_hdr + table [(pos * 2) + 2]);
1601 eh_frame = amodule->eh_frame_hdr + table [(pos * 2) + 1];
1603 unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len, &type_info);
1606 * LLVM might represent one IL region with multiple regions, so have to
1607 * allocate a new JI.
1611 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * ei_len) + extra_size);
1616 jinfo->code_size = code_len;
1617 jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
1618 jinfo->method = method;
1619 jinfo->code_start = code;
1620 jinfo->domain_neutral = 0;
1621 /* This signals that used_regs points to a normal cached unwind info */
1622 jinfo->from_aot = 0;
1623 jinfo->num_clauses = ei_len;
1625 for (i = 0; i < ei_len; ++i) {
1627 * orig_jinfo contains the original IL exception info saved by the AOT
1628 * compiler, we have to combine that with the information produced by LLVM
1630 /* The type_info entries contain IL clause indexes */
1631 int clause_index = *(gint32*)type_info [i];
1632 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
1633 MonoJitExceptionInfo *orig_jei = &orig_jinfo->clauses [clause_index];
1635 g_assert (clause_index < orig_jinfo->num_clauses);
1636 jei->flags = orig_jei->flags;
1637 jei->data.catch_class = orig_jei->data.catch_class;
1639 jei->try_start = ei [i].try_start;
1640 jei->try_end = ei [i].try_end;
1641 jei->handler_start = ei [i].handler_start;
1649 /* The offsets in the table are 31 bits long, have to extend them to 32 */
1650 #define EXTEND_PREL31(val) ((((gint32)(val)) << 1) >> 1)
1652 static inline guint32
1653 decode_uleb128 (guint8 *buf, guint8 **endbuf)
1663 res = res | (((int)(b & 0x7f)) << shift);
1675 decode_arm_eh_ops (guint8 *unwind_ops, int nops)
1677 int i, vsp_reg, vsp_offset;
1679 gint32 *reg_offsets;
1682 * Have to convert the ARM unwind info into DWARF unwind info.
1683 * The ARM unwind info specifies a simple set of instructions which need to be
1684 * executed during unwinding. It manipulates a virtual stack pointer (vsp). The
1685 * connection with DWARF unwind info is the following: after all ARM unwind
1686 * opcodes have been executed, the stack should be completely unwound, i.e.
1687 * vsp == DWARF CFA. This allows us to construct the DWARF opcodes corresponding
1688 * to the ARM opcodes.
1689 * The ARM unwind info is not instruction precise, i. e. it can't handle
1690 * async exceptions etc.
1692 /* The reg used to compute the initial value of vsp */
1693 vsp_reg = ARMREG_SP;
1694 /* The offset between vsp_reg and the CFA */
1697 /* The register save offsets from the initial value of vsp */
1698 reg_offsets = g_new0 (gint32, 16);
1699 for (i = 0; i < 16; ++i)
1700 reg_offsets [i] = -1;
1702 /* section 9.3 in the ehabi doc */
1703 for (i = 0; i < nops; ++i) {
1704 guint8 op = unwind_ops [i];
1706 if ((op >> 6) == 0) {
1707 /* vsp = vsp + (xxxxxx << 2) + 4. */
1708 vsp_offset += ((op & 0x3f) << 2) + 4;
1709 } else if ((op >> 6) == 1) {
1710 /* vsp = vsp - (xxxxxx << 2) - 4. */
1711 vsp_offset -= ((op & 0x3f) << 2) + 4;
1712 } else if (op == 0xb2) {
1713 /* vsp = vsp = vsp + 0x204 + (uleb128 << 2) */
1714 guint8 *p = unwind_ops + i + 1;
1715 guint32 v = decode_uleb128 (p, &p);
1717 vsp_offset += 0x204 + (v << 2);
1718 i = (p - unwind_ops) - 1;
1719 } else if (op >= 0x80 && op <= 0x8f) {
1725 g_assert (i + 1 < nops);
1726 op2 = unwind_ops [i + 1];
1729 for (j = 0; j < 8; ++j)
1730 if (op2 & (0x1 << j))
1731 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j));
1732 for (j = 0; j < 4; ++j)
1733 if (op & (0x1 << j))
1734 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R12 + j));
1737 for (j = 0; j < g_slist_length (regs); ++j)
1738 reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4);
1740 vsp_offset += g_slist_length (regs) * 4;
1742 g_slist_free (regs);
1745 } else if (op >= 0xa8 && op <= 0xaf) {
1749 /* pop r4-r[4 + nnn], r14 */
1752 for (j = 0; j <= (op & 0x7); ++j)
1753 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j));
1754 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R14));
1756 for (j = 0; j < g_slist_length (regs); ++j)
1757 reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4);
1759 vsp_offset += g_slist_length (regs) * 4;
1761 g_slist_free (regs);
1762 } else if (op == 0xb0) {
1765 } else if (op >= 0x90 && op <= 0x9f && op != 0x9d && op != 0x9f) {
1772 for (j = 0; j < nops; ++j)
1773 printf ("%x ", unwind_ops [j]);
1774 printf (" / %d\n", i);
1775 g_assert_not_reached ();
1781 /* vsp_reg + vsp_offset = CFA */
1782 mono_add_unwind_op_def_cfa (ops, (guint8*)NULL, (guint8*)NULL, vsp_reg, vsp_offset);
1784 for (i = 0; i < 16; ++i) {
1785 if (reg_offsets [i] != -1)
1786 /* The reg is saved at vsp_reg + reg_offset [i] == CFA - (vsp_offset - reg_offset [i]) */
1787 mono_add_unwind_op_offset (ops, (guint8*)NULL, (guint8*)NULL, i, - (vsp_offset - reg_offsets [i]));
1796 * Decode the exception handling information in the .ARM.exidx section of the AOT
1797 * file belong to CODE, and construct a MonoJitInfo structure from it.
1798 * LOCKING: Acquires the domain lock.
1801 decode_arm_exidx (MonoAotModule *amodule, MonoDomain *domain,
1802 MonoMethod *method, guint8 *code, guint32 code_len, MonoJitInfo *jinfo)
1805 guint8 *base, *code1, *code2;
1806 int i, pos, left, right, offset, offset1, offset2, count, nwords, nops;
1808 guint8 unwind_ops [64];
1810 guint8 *unwind_info;
1813 g_assert (amodule->arm_exidx);
1815 table = (guint32*)amodule->arm_exidx;
1818 * The table format is described in:
1819 * infocenter.arm.com/help/topic/com.arm.doc.../IHI0038A_ehabi.pdf
1822 base = amodule->arm_exidx;
1823 count = amodule->arm_exidx_size / 8;
1825 /* Binary search in the table to find the entry for code */
1826 offset = code - base;
1831 pos = (left + right) / 2;
1836 offset1 = EXTEND_PREL31 (table [(pos * 2)]);
1837 code1 = (guint8*)&(table [pos * 2]) + offset1;
1838 if (pos + 1 == count)
1840 offset2 = amodule->code_end - amodule->code;
1842 offset2 = EXTEND_PREL31 (table [(pos + 1) * 2]);
1843 code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2;
1847 else if (code >= code2)
1853 if (code >= code1) {
1855 * The linker might merge duplicate unwind table entries, so
1856 * offset1 and offset2 might point to another method, but this is not a problem.
1858 code1 = (guint8*)&(table [pos * 2]) + offset1;
1859 code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2;
1861 g_assert (code >= code1);
1863 g_assert (code < code2);
1865 entry = table [(pos * 2) + 1];
1867 /* inline entry, compact model, personality routine 0 */
1868 if ((entry & 0xff000000) == 0x80000000) {
1870 unwind_ops [0] = (entry & 0x00ff0000) >> 16;
1871 unwind_ops [1] = (entry & 0x0000ff00) >> 8;
1872 unwind_ops [2] = (entry & 0x000000ff) >> 0;
1874 ops = decode_arm_eh_ops (unwind_ops, nops);
1875 } else if ((entry & 0x80000000) == 0) {
1876 /* non-inline entry */
1877 guint8 *data = (guint8*)&table [(pos * 2) + 1] + EXTEND_PREL31 (entry);
1879 entry = ((guint32*)data) [0];
1881 /* compact model, personality routine 1 */
1882 g_assert ((entry & 0xff000000) == 0x81000000);
1884 nwords = (entry & 0x00ff0000) >> 16;
1885 nops = nwords * 4 + 2;
1886 g_assert (nops < 64);
1888 unwind_ops [0] = (entry & 0x0000ff00) >> 8;
1889 unwind_ops [1] = (entry & 0x000000ff) >> 0;
1891 for (i = 0; i < nwords; ++i) {
1892 entry = ((guint32*)data) [1 + i];
1893 unwind_ops [(i * 4) + 2] = (entry & 0xff000000) >> 24;
1894 unwind_ops [(i * 4) + 2 + 1] = (entry & 0x00ff0000) >> 16;
1895 unwind_ops [(i * 4) + 2 + 2] = (entry & 0x0000ff00) >> 8;
1896 unwind_ops [(i * 4) + 2 + 3] = (entry & 0x000000ff) >> 0;
1899 ops = decode_arm_eh_ops (unwind_ops, nops);
1904 unwind_info = mono_unwind_ops_encode (ops, &unw_len);
1906 /* The method has no unwind info */
1911 jinfo->code_size = code_len;
1912 jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
1913 jinfo->method = method;
1914 jinfo->code_start = code;
1915 jinfo->domain_neutral = 0;
1916 /* This signals that used_regs points to a normal cached unwind info */
1917 jinfo->from_aot = 0;
1922 * LOCKING: Acquires the domain lock.
1925 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
1926 MonoMethod *method, guint8* ex_info, guint8 *addr,
1927 guint8 *code, guint32 code_len)
1931 guint used_int_regs, flags;
1932 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points;
1935 int generic_info_size;
1937 /* Load the method info from the AOT file */
1940 flags = decode_value (p, &p);
1941 has_generic_jit_info = (flags & 1) != 0;
1942 has_dwarf_unwind_info = (flags & 2) != 0;
1943 has_clauses = (flags & 4) != 0;
1944 has_seq_points = (flags & 8) != 0;
1945 from_llvm = (flags & 16) != 0;
1946 if (has_dwarf_unwind_info) {
1949 offset = decode_value (p, &p);
1950 g_assert (offset < (1 << 30));
1951 used_int_regs = offset;
1953 used_int_regs = decode_value (p, &p);
1955 if (has_generic_jit_info)
1956 generic_info_size = sizeof (MonoGenericJitInfo);
1958 generic_info_size = 0;
1960 /* Exception table */
1962 int num_clauses = decode_value (p, &p);
1965 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size);
1966 jinfo->num_clauses = num_clauses;
1968 for (i = 0; i < num_clauses; ++i) {
1969 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1971 ei->flags = decode_value (p, &p);
1974 if (decode_value (p, &p))
1975 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1977 /* The rest of the info is in the DWARF EH section */
1981 ei->exvar_offset = decode_value (p, &p);
1983 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1984 ei->data.filter = code + decode_value (p, &p);
1986 if (decode_value (p, &p))
1987 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1990 ei->try_start = code + decode_value (p, &p);
1991 ei->try_end = code + decode_value (p, &p);
1992 ei->handler_start = code + decode_value (p, &p);
1996 jinfo = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + generic_info_size);
2000 /* LLVM compiled method */
2001 /* The info is in the .eh_frame section */
2003 decode_arm_exidx (amodule, domain, method, code, code_len, jinfo);
2005 jinfo = decode_eh_frame (amodule, domain, method, code, jinfo, generic_info_size);
2007 jinfo->from_llvm = 1;
2009 jinfo->code_size = code_len;
2010 jinfo->used_regs = used_int_regs;
2011 jinfo->method = method;
2012 jinfo->code_start = code;
2013 jinfo->domain_neutral = 0;
2014 jinfo->from_aot = 1;
2017 if (has_generic_jit_info) {
2018 MonoGenericJitInfo *gi;
2020 jinfo->has_generic_jit_info = 1;
2022 gi = mono_jit_info_get_generic_jit_info (jinfo);
2025 gi->has_this = decode_value (p, &p);
2026 gi->this_reg = decode_value (p, &p);
2027 gi->this_offset = decode_value (p, &p);
2029 /* This currently contains no data */
2030 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
2032 jinfo->method = decode_method_ref_2 (amodule, p, &p);
2035 if (has_seq_points) {
2036 MonoSeqPointInfo *seq_points;
2037 int il_offset, native_offset, last_il_offset, last_native_offset, j;
2039 int len = decode_value (p, &p);
2041 seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
2042 seq_points->len = len;
2043 last_il_offset = last_native_offset = 0;
2044 for (i = 0; i < len; ++i) {
2045 SeqPoint *sp = &seq_points->seq_points [i];
2046 il_offset = last_il_offset + decode_value (p, &p);
2047 native_offset = last_native_offset + decode_value (p, &p);
2049 sp->il_offset = il_offset;
2050 sp->native_offset = native_offset;
2052 sp->next_len = decode_value (p, &p);
2053 sp->next = g_new (int, sp->next_len);
2054 for (j = 0; j < sp->next_len; ++j)
2055 sp->next [j] = decode_value (p, &p);
2057 last_il_offset = il_offset;
2058 last_native_offset = native_offset;
2061 mono_domain_lock (domain);
2062 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
2063 mono_domain_unlock (domain);
2066 /* Load debug info */
2067 buf_len = decode_value (p, &p);
2068 mono_debug_add_aot_method (domain, method, code, p, buf_len);
2070 if (amodule != jinfo->method->klass->image->aot_module) {
2073 ji_to_amodule = g_hash_table_new (NULL, NULL);
2074 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
2082 * mono_aot_get_unwind_info:
2084 * Return a pointer to the DWARF unwind info belonging to JI.
2087 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2089 MonoAotModule *amodule = ji->method->klass->image->aot_module;
2091 guint8 *code = ji->code_start;
2094 g_assert (ji->from_aot);
2096 if (!(code >= amodule->code && code <= amodule->code_end)) {
2097 /* ji belongs to a different aot module than amodule */
2099 g_assert (ji_to_amodule);
2100 amodule = g_hash_table_lookup (ji_to_amodule, ji);
2102 g_assert (code >= amodule->code && code <= amodule->code_end);
2106 p = amodule->unwind_info + ji->used_regs;
2107 *unwind_info_len = decode_value (p, &p);
2112 compare_ints (const void *a, const void *b)
2114 return *(gint32*)a - *(gint32*)b;
2118 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2120 int pos, left, right, offset, offset1, offset2, code_len;
2121 int method_index, table_len, is_wrapper;
2123 MonoAotModule *amodule = image->aot_module;
2126 guint8 *code, *ex_info, *p;
2128 int nmethods = amodule->info.nmethods;
2129 gint32 *code_offsets;
2135 if (domain != mono_get_root_domain ())
2139 offset = (guint8*)addr - amodule->code;
2141 /* Compute a sorted table mapping code offsets to method indexes. */
2142 if (!amodule->sorted_code_offsets) {
2143 code_offsets = g_new0 (gint32, nmethods * 2);
2144 for (i = 0; i < nmethods; ++i) {
2145 code_offsets [(i * 2)] = amodule->code_offsets [i];
2146 code_offsets [(i *2) + 1] = i;
2148 /* FIXME: Use a merge sort as this is mostly sorted */
2149 qsort (code_offsets, nmethods, sizeof (gint32) * 2, compare_ints);
2150 for (i = 0; i < nmethods -1; ++i)
2151 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
2153 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
2154 /* Somebody got in before us */
2155 g_free (code_offsets);
2158 code_offsets = amodule->sorted_code_offsets;
2160 /* Binary search in the sorted_code_offsets table */
2164 pos = (left + right) / 2;
2166 offset1 = code_offsets [(pos * 2)];
2167 if (pos + 1 == nmethods)
2168 offset2 = amodule->code_end - amodule->code;
2170 offset2 = code_offsets [(pos + 1) * 2];
2172 if (offset < offset1)
2174 else if (offset >= offset2)
2180 g_assert (offset >= code_offsets [(pos * 2)]);
2181 if (pos + 1 < nmethods)
2182 g_assert (offset < code_offsets [((pos + 1) * 2)]);
2183 method_index = code_offsets [(pos * 2) + 1];
2185 code = &amodule->code [amodule->code_offsets [method_index]];
2186 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
2188 if (pos == nmethods - 1)
2189 code_len = amodule->code_end - code;
2191 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
2193 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
2195 /* Might be a wrapper/extra method */
2196 if (amodule->extra_methods) {
2198 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
2205 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
2207 * This is hit for extra methods which are called directly, so they are
2208 * not in amodule->extra_methods.
2210 table_len = amodule->extra_method_info_offsets [0];
2211 table = amodule->extra_method_info_offsets + 1;
2218 pos = ((left + right) / 2);
2220 g_assert (pos < table_len);
2222 if (table [pos * 2] < method_index)
2224 else if (table [pos * 2] > method_index)
2230 p = amodule->blob + table [(pos * 2) + 1];
2231 is_wrapper = decode_value (p, &p);
2232 g_assert (!is_wrapper);
2233 method = decode_method_ref_2 (amodule, p, &p);
2236 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
2237 method = mono_get_method (image, token, NULL);
2244 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
2246 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
2248 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
2249 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
2251 /* Add it to the normal JitInfo tables */
2252 mono_jit_info_table_add (domain, jinfo);
2258 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
2266 case MONO_PATCH_INFO_METHOD:
2267 case MONO_PATCH_INFO_METHOD_JUMP:
2268 case MONO_PATCH_INFO_ICALL_ADDR:
2269 case MONO_PATCH_INFO_METHOD_RGCTX: {
2272 gboolean no_aot_trampoline;
2274 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
2278 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
2279 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (image, token));
2280 ji->type = MONO_PATCH_INFO_ABS;
2284 ji->data.method = method;
2286 ji->data.method = mono_get_method (image, token, NULL);
2287 g_assert (ji->data.method);
2288 mono_class_init (ji->data.method->klass);
2292 case MONO_PATCH_INFO_INTERNAL_METHOD:
2293 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2294 guint32 len = decode_value (p, &p);
2296 ji->data.name = (char*)p;
2300 case MONO_PATCH_INFO_METHODCONST:
2302 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
2303 if (!ji->data.method)
2306 case MONO_PATCH_INFO_VTABLE:
2307 case MONO_PATCH_INFO_CLASS:
2308 case MONO_PATCH_INFO_IID:
2309 case MONO_PATCH_INFO_ADJUSTED_IID:
2311 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2312 if (!ji->data.klass)
2315 case MONO_PATCH_INFO_CLASS_INIT:
2316 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2317 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2318 if (!ji->data.klass)
2321 case MONO_PATCH_INFO_IMAGE:
2322 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
2323 if (!ji->data.image)
2326 case MONO_PATCH_INFO_FIELD:
2327 case MONO_PATCH_INFO_SFLDA:
2329 ji->data.field = decode_field_info (aot_module, p, &p);
2330 if (!ji->data.field)
2333 case MONO_PATCH_INFO_SWITCH:
2334 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
2335 ji->data.table->table_size = decode_value (p, &p);
2336 table = g_new (gpointer, ji->data.table->table_size);
2337 ji->data.table->table = (MonoBasicBlock**)table;
2338 for (i = 0; i < ji->data.table->table_size; i++)
2339 table [i] = (gpointer)(gssize)decode_value (p, &p);
2341 case MONO_PATCH_INFO_R4: {
2344 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
2345 val = decode_value (p, &p);
2346 *(float*)ji->data.target = *(float*)&val;
2349 case MONO_PATCH_INFO_R8: {
2353 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
2355 val [0] = decode_value (p, &p);
2356 val [1] = decode_value (p, &p);
2357 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
2358 *(double*)ji->data.target = *(double*)&v;
2361 case MONO_PATCH_INFO_LDSTR:
2362 image = load_image (aot_module, decode_value (p, &p), TRUE);
2365 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
2367 case MONO_PATCH_INFO_RVA:
2368 case MONO_PATCH_INFO_DECLSEC:
2369 case MONO_PATCH_INFO_LDTOKEN:
2370 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2372 image = load_image (aot_module, decode_value (p, &p), TRUE);
2375 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
2377 ji->data.token->has_context = decode_value (p, &p);
2378 if (ji->data.token->has_context) {
2379 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
2384 case MONO_PATCH_INFO_EXC_NAME:
2385 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2386 if (!ji->data.klass)
2388 ji->data.name = ji->data.klass->name;
2390 case MONO_PATCH_INFO_METHOD_REL:
2391 ji->data.offset = decode_value (p, &p);
2393 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2394 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2395 case MONO_PATCH_INFO_MONITOR_ENTER:
2396 case MONO_PATCH_INFO_MONITOR_EXIT:
2398 case MONO_PATCH_INFO_RGCTX_FETCH: {
2400 MonoJumpInfoRgctxEntry *entry;
2402 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2403 entry->method = decode_method_ref_2 (aot_module, p, &p);
2404 entry->in_mrgctx = decode_value (p, &p);
2405 entry->info_type = decode_value (p, &p);
2406 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2407 entry->data->type = decode_value (p, &p);
2409 res = decode_patch (aot_module, mp, entry->data, p, &p);
2412 ji->data.rgctx_entry = entry;
2415 case MONO_PATCH_INFO_SEQ_POINT_INFO:
2417 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
2418 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
2420 imt_tramp->method = decode_method_ref_2 (aot_module, p, &p);
2421 imt_tramp->vt_offset = decode_value (p, &p);
2423 ji->data.imt_tramp = imt_tramp;
2427 g_warning ("unhandled type %d", ji->type);
2428 g_assert_not_reached ();
2439 static MonoJumpInfo*
2440 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
2441 guint32 **got_slots,
2442 guint8 *buf, guint8 **endbuf)
2444 MonoJumpInfo *patches;
2450 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
2452 *got_slots = g_malloc (sizeof (guint32) * n_patches);
2454 for (pindex = 0; pindex < n_patches; ++pindex) {
2455 MonoJumpInfo *ji = &patches [pindex];
2460 got_offset = decode_value (p, &p);
2462 if (aot_module->got [got_offset]) {
2463 /* Already loaded */
2464 //printf ("HIT!\n");
2466 shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
2468 ji->type = decode_value (shared_p, &shared_p);
2470 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
2475 (*got_slots) [pindex] = got_offset;
2482 g_free (*got_slots);
2489 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
2492 * Jump addresses cannot be patched by the trampoline code since it
2493 * does not have access to the caller's address. Instead, we collect
2494 * the addresses of the GOT slots pointing to a method, and patch
2495 * them after the method has been compiled.
2497 MonoJitDomainInfo *info = domain_jit_info (domain);
2500 mono_domain_lock (domain);
2501 if (!info->jump_target_got_slot_hash)
2502 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
2503 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
2504 list = g_slist_prepend (list, got_slot);
2505 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
2506 mono_domain_unlock (domain);
2512 * Load the method identified by METHOD_INDEX from the AOT image. Return a
2513 * pointer to the native code of the method, or NULL if not found.
2514 * METHOD might not be set if the caller only has the image/token info.
2517 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
2520 gboolean from_plt = method == NULL;
2522 int i, pindex, n_patches, used_strings;
2523 gboolean keep_patches = TRUE;
2525 MonoJitInfo *jinfo = NULL;
2526 guint8 *code, *info;
2528 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2531 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
2532 /* Non shared AOT code can't be used in other appdomains */
2535 if (amodule->out_of_date)
2538 if (amodule->code_offsets [method_index] == 0xffffffff) {
2539 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2543 method = mono_get_method (image, token, NULL);
2544 full_name = mono_method_full_name (method, TRUE);
2545 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2551 code = &amodule->code [amodule->code_offsets [method_index]];
2553 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
2556 if (!amodule->methods_loaded)
2557 amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods + 1);
2560 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2563 if (mono_last_aot_method != -1) {
2564 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
2566 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
2568 method = mono_get_method (image, token, NULL);
2570 char *name = mono_method_full_name (method, TRUE);
2571 printf ("LAST AOT METHOD: %s.\n", name);
2574 printf ("LAST AOT METHOD: %p %d\n", code, method_index);
2582 klass = method->klass;
2583 decode_klass_ref (amodule, p, &p);
2585 klass = decode_klass_ref (amodule, p, &p);
2588 if (amodule->info.opts & MONO_OPT_SHARED)
2589 used_strings = decode_value (p, &p);
2593 for (i = 0; i < used_strings; i++) {
2594 guint token = decode_value (p, &p);
2595 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2598 if (amodule->info.opts & MONO_OPT_SHARED)
2599 keep_patches = FALSE;
2601 n_patches = decode_value (p, &p);
2603 keep_patches = FALSE;
2606 MonoJumpInfo *patches;
2612 mp = mono_mempool_new ();
2614 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2615 if (patches == NULL)
2618 for (pindex = 0; pindex < n_patches; ++pindex) {
2619 MonoJumpInfo *ji = &patches [pindex];
2621 if (!amodule->got [got_slots [pindex]]) {
2622 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2623 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2624 amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
2625 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2626 register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
2628 ji->type = MONO_PATCH_INFO_NONE;
2634 mono_mempool_destroy (mp);
2637 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2641 method = mono_get_method (image, token, NULL);
2643 full_name = mono_method_full_name (method, TRUE);
2646 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
2648 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);
2654 mono_jit_stats.methods_aot++;
2656 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2660 if (method && method->wrapper_type)
2661 g_hash_table_insert (amodule->method_to_code, method, code);
2665 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
2669 method = mono_get_method (image, token, NULL);
2672 mono_profiler_method_jit (method);
2673 jinfo = mono_jit_info_table_find (domain, (char*)code);
2675 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
2678 if (from_plt && klass && !klass->generic_container)
2679 mono_runtime_class_init (mono_class_vtable (domain, klass));
2684 /* FIXME: The space in domain->mp is wasted */
2685 if (amodule->info.opts & MONO_OPT_SHARED)
2686 /* No need to cache patches */
2687 mono_mempool_destroy (mp);
2696 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name)
2698 guint32 table_size, entry_size, hash;
2699 guint32 *table, *entry;
2701 static guint32 n_extra_decodes;
2706 table_size = amodule->extra_method_table [0];
2707 table = amodule->extra_method_table + 1;
2710 hash = mono_aot_method_hash (method) % table_size;
2712 entry = &table [hash * entry_size];
2719 guint32 key = entry [0];
2720 guint32 value = entry [1];
2721 guint32 next = entry [entry_size - 1];
2724 int is_wrapper_name;
2726 p = amodule->blob + key;
2727 is_wrapper_name = decode_value (p, &p);
2728 if (is_wrapper_name) {
2729 int wrapper_type = decode_value (p, &p);
2730 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2734 } else if (can_method_ref_match_method (amodule, p, method)) {
2736 if (!amodule->method_ref_to_method)
2737 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2738 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2742 m = decode_method_ref_2 (amodule, p, &p);
2745 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2754 /* Special case: wrappers of shared generic methods */
2755 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2756 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2757 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2758 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2760 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2766 /* Methods decoded needlessly */
2769 printf ("%d %s %s\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
2775 entry = &table [next * entry_size];
2784 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2786 g_ptr_array_add ((GPtrArray*)user_data, value);
2790 * find_extra_method:
2792 * Try finding METHOD in the extra_method table in all AOT images.
2793 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2794 * module where the method was found.
2797 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2804 if (method->wrapper_type)
2805 name = mono_aot_wrapper_name (method);
2807 /* Try the method's module first */
2808 *out_amodule = method->klass->image->aot_module;
2809 index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name);
2810 if (index != 0xffffff) {
2816 * Try all other modules.
2817 * This is needed because generic instances klass->image points to the image
2818 * containing the generic definition, but the native code is generated to the
2819 * AOT image which contains the reference.
2822 /* Make a copy to avoid doing the search inside the aot lock */
2823 modules = g_ptr_array_new ();
2825 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2829 for (i = 0; i < modules->len; ++i) {
2830 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2832 if (amodule != method->klass->image->aot_module)
2833 index = find_extra_method_in_amodule (amodule, method, name);
2834 if (index != 0xffffff) {
2835 *out_amodule = amodule;
2840 g_ptr_array_free (modules, TRUE);
2847 * mono_aot_get_method:
2849 * Return a pointer to the AOTed native code for METHOD if it can be found,
2851 * On platforms with function pointers, this doesn't return a function pointer.
2854 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2856 MonoClass *klass = method->klass;
2857 guint32 method_index;
2858 MonoAotModule *amodule = klass->image->aot_module;
2864 if (amodule->out_of_date)
2867 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2868 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2869 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2870 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2874 * Use the original method instead of its invoke-with-check wrapper.
2875 * This is not a problem when using full-aot, since it doesn't support
2878 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
2879 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
2881 g_assert (klass->inited);
2883 /* Find method index */
2884 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2885 method = mono_method_get_declaring_generic_method (method);
2886 method_index = mono_metadata_token_index (method->token) - 1;
2887 } else if (method->is_inflated || !method->token) {
2888 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2890 code = g_hash_table_lookup (amodule->method_to_code, method);
2895 method_index = find_extra_method (method, &amodule);
2897 * Special case the ICollection<T> wrappers for arrays, as they cannot
2898 * be statically enumerated, and each wrapper ends up calling the same
2901 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
2902 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
2904 code = mono_aot_get_method (domain, m);
2906 if (mono_method_needs_static_rgctx_invoke (m, FALSE)) {
2907 code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code));
2908 /* The call above returns an ftnptr */
2909 code = mono_get_addr_from_ftnptr (code);
2917 * Special case Array.GetGenericValueImpl which is a generic icall.
2918 * Generic sharing currently can't handle it, but the icall returns data using
2919 * an out parameter, so the managed-to-native wrappers can share the same code.
2921 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
2923 MonoGenericContext ctx;
2924 MonoType *args [16];
2926 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
2927 /* Avoid recursion */
2930 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
2933 memset (&ctx, 0, sizeof (ctx));
2934 args [0] = &mono_defaults.object_class->byval_arg;
2935 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2937 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2940 * Get the code for the <object> instantiation which should be emitted into
2941 * the mscorlib aot image by the AOT compiler.
2943 code = mono_aot_get_method (domain, m);
2948 if (method_index == 0xffffff) {
2949 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2952 full_name = mono_method_full_name (method, TRUE);
2953 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2959 if (method_index == 0xffffff)
2962 /* Needed by find_jit_info */
2964 if (!amodule->extra_methods)
2965 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2966 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2970 method_index = mono_metadata_token_index (method->token) - 1;
2973 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2977 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2981 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2983 MonoAotModule *aot_module = image->aot_module;
2989 method_index = mono_metadata_token_index (token) - 1;
2991 return load_method (domain, aot_module, image, NULL, token, method_index);
2997 } IsGotEntryUserData;
3000 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
3002 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
3003 MonoAotModule *aot_module = (MonoAotModule*)value;
3005 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
3010 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3012 IsGotEntryUserData user_data;
3017 user_data.addr = addr;
3018 user_data.res = FALSE;
3020 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
3023 return user_data.res;
3028 MonoAotModule *module;
3029 } FindAotModuleUserData;
3032 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
3034 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
3035 MonoAotModule *aot_module = (MonoAotModule*)value;
3037 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
3038 data->module = aot_module;
3041 static inline MonoAotModule*
3042 find_aot_module (guint8 *code)
3044 FindAotModuleUserData user_data;
3049 /* Reading these need no locking */
3050 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
3053 user_data.addr = code;
3054 user_data.module = NULL;
3057 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
3060 return user_data.module;
3064 * mono_aot_plt_resolve:
3066 * This function is called by the entries in the PLT to resolve the actual method that
3067 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
3068 * Returns NULL if the something cannot be loaded.
3071 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3073 #ifdef MONO_ARCH_AOT_SUPPORTED
3074 guint8 *p, *target, *plt_entry;
3076 MonoAotModule *module = (MonoAotModule*)aot_module;
3077 gboolean res, no_ftnptr = FALSE;
3080 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
3082 p = &module->blob [plt_info_offset];
3084 ji.type = decode_value (p, &p);
3086 mp = mono_mempool_new_size (512);
3087 res = decode_patch (module, mp, &ji, p, &p);
3090 mono_mempool_destroy (mp);
3095 * Avoid calling resolve_patch_target in the full-aot case if possible, since
3096 * it would create a trampoline, and we don't need that.
3097 * We could do this only if the method does not need the special handling
3098 * in mono_magic_trampoline ().
3100 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) &&
3101 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
3102 target = mono_jit_compile_method (ji.data.method);
3105 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
3109 * The trampoline expects us to return a function descriptor on platforms which use
3110 * it, but resolve_patch_target returns a direct function pointer for some type of
3111 * patches, so have to translate between the two.
3112 * FIXME: Clean this up, but how ?
3114 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) {
3115 /* These should already have a function descriptor */
3116 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3117 /* Our function descriptors have a 0 environment, gcc created ones don't */
3118 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
3119 g_assert (((gpointer*)target) [2] == 0);
3122 } else if (!no_ftnptr) {
3123 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3124 g_assert (((gpointer*)target) [2] != 0);
3126 target = mono_create_ftnptr (mono_domain_get (), target);
3129 mono_mempool_destroy (mp);
3131 /* Patch the PLT entry with target which might be the actual method not a trampoline */
3132 plt_entry = mono_aot_get_plt_entry (code);
3133 g_assert (plt_entry);
3134 mono_arch_patch_plt_entry (plt_entry, module->got, NULL, target);
3138 g_assert_not_reached ();
3146 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
3147 * method in the module is loaded to avoid committing memory by writing to it.
3148 * LOCKING: Assumes the AOT lock is held.
3151 init_plt (MonoAotModule *amodule)
3153 #ifndef MONO_CROSS_COMPILE
3155 #ifdef MONO_ARCH_AOT_SUPPORTED
3157 guint8 *buf = amodule->plt;
3158 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
3164 if (amodule->plt_inited)
3167 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
3170 /* Initialize the first PLT entry */
3171 make_writable (amodule->plt, amodule->plt_end - amodule->plt);
3172 x86_jump_code (buf, tramp);
3173 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
3175 * Initialize the PLT entries in the GOT to point to the default targets.
3178 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
3179 plt_0 = mono_create_ftnptr (mono_domain_get (), amodule->plt);
3180 /* The first entry points to the AOT trampoline */
3181 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base] = tramp;
3182 for (i = 1; i < amodule->info.plt_size; ++i)
3183 /* All the default entries point to the first entry */
3184 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = plt_0;
3186 g_assert_not_reached ();
3189 amodule->plt_inited = TRUE;
3192 #endif /* MONO_CROSS_COMPILE */
3196 * mono_aot_get_plt_entry:
3198 * Return the address of the PLT entry called by the code at CODE if exists.
3201 mono_aot_get_plt_entry (guint8 *code)
3203 MonoAotModule *aot_module = find_aot_module (code);
3204 #if defined(__arm__) || defined(__mono_ppc__)
3211 #if defined(__i386__) || defined(__x86_64__)
3212 if (code [-5] == 0xe8) {
3213 guint32 disp = *(guint32*)(code - 4);
3214 guint8 *target = code + disp;
3216 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3219 #elif defined(__arm__)
3220 ins = ((guint32*)(gpointer)code) [-1];
3222 /* Should be a 'bl' */
3223 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
3224 gint32 disp = ((gint32)ins) & 0xffffff;
3225 guint8 *target = code - 4 + 8 + (disp * 4);
3227 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3230 #elif defined(__mono_ppc__)
3231 /* Should be a bl */
3232 ins = ((guint32*)(gpointer)code) [-1];
3234 if ((ins >> 26 == 18) && ((ins & 1) == 1) && ((ins & 2) == 0)) {
3235 gint32 disp = (((gint32)ins) >> 2) & 0xffffff;
3236 guint8 *target = code - 4 + (disp * 4);
3238 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3242 g_assert_not_reached ();
3249 * mono_aot_get_plt_info_offset:
3251 * Return the PLT info offset belonging to the plt entry called by CODE.
3254 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3256 guint8 *plt_entry = mono_aot_get_plt_entry (code);
3258 g_assert (plt_entry);
3260 /* The offset is embedded inside the code after the plt entry */
3261 #if defined(__i386__)
3262 return *(guint32*)(plt_entry + 5);
3263 #elif defined(__x86_64__)
3264 return *(guint32*)(plt_entry + 6);
3265 #elif defined(__arm__)
3266 /* The offset is stored as the 4th word of the plt entry */
3267 return ((guint32*)plt_entry) [3];
3268 #elif defined(__mono_ppc__)
3269 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3270 return ((guint32*)plt_entry) [8];
3272 return ((guint32*)plt_entry) [6];
3275 g_assert_not_reached ();
3281 mono_create_ftnptr_malloc (guint8 *code)
3283 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3284 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
3286 ftnptr->code = code;
3299 * Load the function named NAME from the aot image.
3302 load_function (MonoAotModule *amodule, const char *name)
3306 int n_patches, pindex;
3312 symbol = g_strdup_printf ("%s", name);
3313 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
3316 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
3318 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
3322 symbol = g_strdup_printf ("%s_p", name);
3323 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
3326 /* Nothing to patch */
3329 p = amodule->blob + *(guint32*)p;
3331 /* Similar to mono_aot_load_method () */
3333 n_patches = decode_value (p, &p);
3336 MonoJumpInfo *patches;
3339 mp = mono_mempool_new ();
3341 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
3344 for (pindex = 0; pindex < n_patches; ++pindex) {
3345 MonoJumpInfo *ji = &patches [pindex];
3348 if (amodule->got [got_slots [pindex]])
3352 * When this code is executed, the runtime may not be initalized yet, so
3353 * resolve the patch info by hand.
3355 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3356 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
3357 target = mono_get_lmf_addr;
3358 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
3359 target = mono_thread_force_interruption_checkpoint;
3360 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
3361 target = mono_exception_from_token;
3362 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
3363 target = mono_get_throw_exception ();
3365 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
3366 target = mono_amd64_throw_exception;
3369 } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
3370 target = mono_amd64_get_original_ip;
3373 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
3374 target = mono_arm_throw_exception;
3375 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
3376 target = mono_arm_throw_exception_by_token;
3379 } else if (!strcmp (ji->data.name, "mono_ppc_throw_exception")) {
3380 target = mono_ppc_throw_exception;
3382 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
3383 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
3384 target = (gpointer)mono_get_trampoline_func (tramp_type2);
3385 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
3386 /* atoll is needed because the the offset is unsigned */
3390 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
3391 g_assert (res == 1);
3392 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
3393 target = mono_create_ftnptr_malloc (target);
3394 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
3395 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
3396 target = mono_create_ftnptr_malloc (target);
3397 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
3398 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
3399 target = mono_create_ftnptr_malloc (target);
3400 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
3401 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
3402 target = mono_create_ftnptr_malloc (target);
3403 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
3404 target = mono_thread_get_and_clear_pending_exception;
3406 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
3407 g_assert_not_reached ();
3411 /* Hopefully the code doesn't have patches which need method or
3414 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
3418 amodule->got [got_slots [pindex]] = target;
3423 mono_mempool_destroy (mp);
3430 * Return the piece of code identified by NAME from the mscorlib AOT file.
3431 * On ppc64, this returns a function descriptor.
3434 mono_aot_get_named_code (const char *name)
3437 MonoAotModule *amodule;
3439 image = mono_defaults.corlib;
3442 amodule = image->aot_module;
3445 return mono_create_ftnptr_malloc (load_function (amodule, name));
3448 /* Return a given kind of trampoline */
3450 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
3452 MonoAotModule *amodule;
3453 int index, tramp_size;
3456 /* Currently, we keep all trampolines in the mscorlib AOT image */
3457 image = mono_defaults.corlib;
3462 amodule = image->aot_module;
3465 *out_amodule = amodule;
3467 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
3468 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
3470 index = amodule->trampoline_index [tramp_type] ++;
3474 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
3476 tramp_size = amodule->info.trampoline_size [tramp_type];
3479 *out_tramp_size = tramp_size;
3481 return amodule->trampolines [tramp_type] + (index * tramp_size);
3485 * Return a specific trampoline from the AOT file.
3488 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3490 MonoAotModule *amodule;
3491 guint32 got_offset, tramp_size;
3492 guint8 *code, *tramp;
3493 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
3494 static gboolean inited;
3495 static guint32 num_trampolines;
3501 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
3510 if (!generic_trampolines [tramp_type]) {
3513 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
3514 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
3518 tramp = generic_trampolines [tramp_type];
3521 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
3523 amodule->got [got_offset] = tramp;
3524 amodule->got [got_offset + 1] = arg1;
3527 *code_len = tramp_size;
3533 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3535 MonoAotModule *amodule;
3539 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
3541 amodule->got [got_offset] = ctx;
3542 amodule->got [got_offset + 1] = addr;
3544 /* The caller expects an ftnptr */
3545 return mono_create_ftnptr (mono_domain_get (), code);
3549 mono_aot_get_unbox_trampoline (MonoMethod *method)
3551 guint32 method_index = mono_metadata_token_index (method->token) - 1;
3552 MonoAotModule *amodule;
3556 if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
3557 guint32 index = find_extra_method (method, &amodule);
3558 g_assert (index != 0xffffff);
3560 symbol = g_strdup_printf ("ut_e_%d", index);
3562 amodule = method->klass->image->aot_module;
3565 symbol = g_strdup_printf ("ut_%d", method_index);
3567 code = load_function (amodule, symbol);
3570 /* The caller expects an ftnptr */
3571 return mono_create_ftnptr (mono_domain_get (), code);
3575 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3580 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
3581 code = load_function (mono_defaults.corlib->aot_module, symbol);
3583 /* The caller expects an ftnptr */
3584 return mono_create_ftnptr (mono_domain_get (), code);
3588 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3594 MonoAotModule *amodule;
3596 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
3598 /* Save the entries into an array */
3599 buf = mono_domain_alloc (domain, (count + 1) * 2 * sizeof (gpointer));
3600 for (i = 0; i < count; ++i) {
3601 MonoIMTCheckItem *item = imt_entries [i];
3603 g_assert (item->key);
3605 g_assert (!item->has_target_code);
3607 buf [(i * 2)] = item->key;
3608 buf [(i * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
3610 buf [(count * 2)] = NULL;
3611 buf [(count * 2) + 1] = fail_tramp;
3613 amodule->got [got_offset] = buf;
3619 * mono_aot_set_make_unreadable:
3621 * Set whenever to make all mmaped memory unreadable. In conjuction with a
3622 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
3625 mono_aot_set_make_unreadable (gboolean unreadable)
3629 make_unreadable = unreadable;
3631 if (make_unreadable && !inited) {
3632 mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
3637 MonoAotModule *module;
3642 find_map (gpointer key, gpointer value, gpointer user_data)
3644 MonoAotModule *module = (MonoAotModule*)value;
3645 FindMapUserData *data = (FindMapUserData*)user_data;
3648 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
3649 data->module = module;
3652 static MonoAotModule*
3653 find_module_for_addr (void *ptr)
3655 FindMapUserData data;
3657 if (!make_unreadable)
3661 data.ptr = (guint8*)ptr;
3664 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
3671 * mono_aot_is_pagefault:
3673 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
3674 * within memory allocated by this module.
3677 mono_aot_is_pagefault (void *ptr)
3679 if (!make_unreadable)
3683 * Not signal safe, but SIGSEGV's are synchronous, and
3684 * this is only turned on by a MONO_DEBUG option.
3686 return find_module_for_addr (ptr) != NULL;
3690 * mono_aot_handle_pagefault:
3692 * Handle a pagefault caused by an unreadable page by making it readable again.
3695 mono_aot_handle_pagefault (void *ptr)
3697 #ifndef PLATFORM_WIN32
3698 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
3702 res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
3703 g_assert (res == 0);
3714 mono_aot_init (void)
3719 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3725 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3731 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3737 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
3743 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
3749 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3755 mono_aot_get_plt_entry (guint8 *code)
3761 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3767 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
3773 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3775 g_assert_not_reached ();
3781 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3783 g_assert_not_reached ();
3788 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3790 g_assert_not_reached ();
3795 mono_aot_get_named_code (const char *name)
3797 g_assert_not_reached ();
3802 mono_aot_get_unbox_trampoline (MonoMethod *method)
3804 g_assert_not_reached ();
3809 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3811 g_assert_not_reached ();
3816 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3818 g_assert_not_reached ();
3823 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3825 g_assert_not_reached ();