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;
166 /* Used to speed-up find_aot_module () */
167 static gsize aot_code_low_addr = (gssize)-1;
168 static gsize aot_code_high_addr = 0;
171 init_plt (MonoAotModule *info);
173 /*****************************************************/
175 /*****************************************************/
180 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
181 * found, and sets the loader error if SET_ERROR is TRUE.
184 load_image (MonoAotModule *amodule, int index, gboolean set_error)
186 MonoAssembly *assembly;
187 MonoImageOpenStatus status;
189 g_assert (index < amodule->image_table_len);
191 if (amodule->image_table [index])
192 return amodule->image_table [index];
193 if (amodule->out_of_date)
196 assembly = mono_assembly_load (&amodule->image_names [index], NULL, &status);
198 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);
199 amodule->out_of_date = TRUE;
202 char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
203 mono_loader_set_error_assembly_load (full_name, FALSE);
209 if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
210 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);
211 amodule->out_of_date = TRUE;
215 amodule->image_table [index] = assembly->image;
216 return assembly->image;
220 decode_value (guint8 *ptr, guint8 **rptr)
225 if ((b & 0x80) == 0){
228 } else if ((b & 0x40) == 0){
229 len = ((b & 0x3f) << 8 | ptr [1]);
231 } else if (b != 0xff) {
232 len = ((b & 0x1f) << 24) |
239 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
245 //printf ("DECODE: %d.\n", len);
250 * mono_aot_get_method:
252 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
256 mono_aot_get_offset (guint32 *table, int index)
258 int i, group, ngroups, index_entry_size;
259 int start_offset, offset, noffsets, group_size;
260 guint8 *data_start, *p;
261 guint32 *index32 = NULL;
262 guint16 *index16 = NULL;
264 noffsets = table [0];
265 group_size = table [1];
267 index_entry_size = table [3];
268 group = index / group_size;
270 if (index_entry_size == 2) {
271 index16 = (guint16*)&table [4];
272 data_start = (guint8*)&index16 [ngroups];
273 p = data_start + index16 [group];
275 index32 = (guint32*)&table [4];
276 data_start = (guint8*)&index32 [ngroups];
277 p = data_start + index32 [group];
280 /* offset will contain the value of offsets [group * group_size] */
281 offset = start_offset = decode_value (p, &p);
282 for (i = group * group_size + 1; i <= index; ++i) {
283 offset += decode_value (p, &p);
286 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
292 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
295 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
297 static MonoGenericInst*
298 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
301 MonoType **type_argv;
302 MonoGenericInst *inst;
305 type_argc = decode_value (p, &p);
306 type_argv = g_new0 (MonoType*, type_argc);
308 for (i = 0; i < type_argc; ++i) {
309 MonoClass *pclass = decode_klass_ref (module, p, &p);
314 type_argv [i] = &pclass->byval_arg;
317 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
326 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
328 gboolean has_class_inst, has_method_inst;
331 has_class_inst = decode_value (p, &p);
332 if (has_class_inst) {
333 ctx->class_inst = decode_generic_inst (module, p, &p);
334 if (!ctx->class_inst)
337 has_method_inst = decode_value (p, &p);
338 if (has_method_inst) {
339 ctx->method_inst = decode_generic_inst (module, p, &p);
340 if (!ctx->method_inst)
349 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
352 MonoClass *klass, *eklass;
356 token = decode_value (p, &p);
361 if (mono_metadata_token_table (token) == 0) {
362 image = load_image (module, decode_value (p, &p), TRUE);
365 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
366 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
367 if (token == MONO_TOKEN_TYPE_SPEC) {
368 MonoTypeEnum type = decode_value (p, &p);
370 if (type == MONO_TYPE_GENERICINST) {
372 MonoGenericContext ctx;
375 gclass = decode_klass_ref (module, p, &p);
378 g_assert (gclass->generic_container);
380 memset (&ctx, 0, sizeof (ctx));
381 ctx.class_inst = decode_generic_inst (module, p, &p);
384 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
385 klass = mono_class_from_mono_type (type);
386 mono_metadata_free_type (type);
387 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
389 MonoGenericContainer *container;
391 int num = decode_value (p, &p);
392 gboolean is_method = decode_value (p, &p);
395 MonoMethod *method_def;
396 g_assert (type == MONO_TYPE_MVAR);
397 method_def = decode_method_ref_2 (module, p, &p);
401 container = mono_method_get_generic_container (method_def);
403 MonoClass *class_def;
404 g_assert (type == MONO_TYPE_VAR);
405 class_def = decode_klass_ref (module, p, &p);
409 container = class_def->generic_container;
412 g_assert (container);
414 // FIXME: Memory management
415 t = g_new0 (MonoType, 1);
417 t->data.generic_param = mono_generic_container_get_param (container, num);
419 // FIXME: Maybe use types directly to avoid
420 // the overhead of creating MonoClass-es
421 klass = mono_class_from_mono_type (t);
425 g_assert_not_reached ();
428 image = load_image (module, decode_value (p, &p), TRUE);
431 klass = mono_class_get (image, token);
433 } else if (token == MONO_TOKEN_TYPE_DEF) {
435 image = load_image (module, decode_value (p, &p), TRUE);
438 rank = decode_value (p, &p);
439 eklass = decode_klass_ref (module, p, &p);
440 klass = mono_array_class_get (eklass, rank);
442 g_assert_not_reached ();
450 static MonoClassField*
451 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
453 MonoClass *klass = decode_klass_ref (module, buf, &buf);
460 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
464 return mono_class_get_field (klass, token);
468 * can_method_ref_match_method:
470 * Determine if calling decode_method_ref_2 on P could return the same method as
471 * METHOD. This is an optimization to avoid calling decode_method_ref_2 () which
472 * would create MonoMethods which are not needed etc.
475 can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
478 guint32 image_index, value;
480 /* Keep this in sync with decode_method_ref () */
481 value = decode_value (p, &p);
482 image_index = value >> 24;
484 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
485 guint32 wrapper_type;
487 if (!method->wrapper_type)
490 wrapper_type = decode_value (p, &p);
492 if (method->wrapper_type != wrapper_type)
494 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
496 } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
497 if (method->wrapper_type)
507 * Decode a method reference, and return its image and token. This avoids loading
508 * metadata for the method if the caller does not need it. If the method has no token,
509 * then it is loaded from metadata and METHOD is set to the method instance.
512 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
514 guint32 image_index, value;
515 MonoImage *image = NULL;
520 if (no_aot_trampoline)
521 *no_aot_trampoline = FALSE;
523 value = decode_value (p, &p);
524 image_index = value >> 24;
526 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
527 if (no_aot_trampoline)
528 *no_aot_trampoline = TRUE;
529 value = decode_value (p, &p);
530 image_index = value >> 24;
533 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
534 guint32 wrapper_type;
536 wrapper_type = decode_value (p, &p);
539 image = mono_defaults.corlib;
541 switch (wrapper_type) {
542 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
543 MonoMethod *m = decode_method_ref_2 (module, p, &p);
547 mono_class_init (m->klass);
548 *method = mono_marshal_get_remoting_invoke_with_check (m);
551 case MONO_WRAPPER_PROXY_ISINST: {
552 MonoClass *klass = decode_klass_ref (module, p, &p);
555 *method = mono_marshal_get_proxy_cancast (klass);
558 case MONO_WRAPPER_LDFLD:
559 case MONO_WRAPPER_LDFLDA:
560 case MONO_WRAPPER_STFLD:
561 case MONO_WRAPPER_ISINST: {
562 MonoClass *klass = decode_klass_ref (module, p, &p);
565 if (wrapper_type == MONO_WRAPPER_LDFLD)
566 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
567 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
568 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
569 else if (wrapper_type == MONO_WRAPPER_STFLD)
570 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
571 else if (wrapper_type == MONO_WRAPPER_ISINST)
572 *method = mono_marshal_get_isinst (klass);
574 g_assert_not_reached ();
577 case MONO_WRAPPER_LDFLD_REMOTE:
578 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
580 case MONO_WRAPPER_STFLD_REMOTE:
581 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
583 case MONO_WRAPPER_ALLOC: {
584 int atype = decode_value (p, &p);
586 *method = mono_gc_get_managed_allocator_by_type (atype);
589 case MONO_WRAPPER_STELEMREF:
590 *method = mono_marshal_get_stelemref ();
592 case MONO_WRAPPER_SYNCHRONIZED: {
593 MonoMethod *m = decode_method_ref_2 (module, p, &p);
597 *method = mono_marshal_get_synchronized_wrapper (m);
600 case MONO_WRAPPER_UNKNOWN: {
601 MonoMethodDesc *desc;
602 MonoMethod *orig_method;
603 int subtype = decode_value (p, &p);
605 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
606 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
607 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
608 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
610 g_assert_not_reached ();
611 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
612 g_assert (orig_method);
613 mono_method_desc_free (desc);
614 *method = mono_monitor_get_fast_path (orig_method);
617 case MONO_WRAPPER_RUNTIME_INVOKE: {
619 MonoMethod *m = decode_method_ref_2 (module, p, &p);
623 *method = mono_marshal_get_runtime_invoke (m, FALSE);
626 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
627 int subtype = decode_value (p, &p);
629 if (subtype == MONO_AOT_WRAPPER_ELEMENT_ADDR) {
630 int rank = decode_value (p, &p);
631 int elem_size = decode_value (p, &p);
633 *method = mono_marshal_get_array_address (rank, elem_size);
635 g_assert_not_reached ();
640 g_assert_not_reached ();
642 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
643 /* Can't decode these */
644 g_assert_not_reached ();
645 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
646 image_index = decode_value (p, &p);
647 *token = decode_value (p, &p);
649 image = load_image (module, image_index, TRUE);
652 } else if (image_index == MONO_AOT_METHODREF_GINST) {
654 MonoGenericContext ctx;
657 * These methods do not have a token which resolves them, so we
658 * resolve them immediately.
660 klass = decode_klass_ref (module, p, &p);
664 image_index = decode_value (p, &p);
665 *token = decode_value (p, &p);
667 image = load_image (module, image_index, TRUE);
671 *method = mono_get_method_full (image, *token, NULL, NULL);
675 memset (&ctx, 0, sizeof (ctx));
677 if (FALSE && klass->generic_class) {
678 ctx.class_inst = klass->generic_class->context.class_inst;
679 ctx.method_inst = NULL;
681 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
684 memset (&ctx, 0, sizeof (ctx));
686 if (!decode_generic_context (module, &ctx, p, &p))
689 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
690 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
694 klass = decode_klass_ref (module, p, &p);
697 method_type = decode_value (p, &p);
699 switch (method_type) {
701 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
704 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
707 *method = mono_class_get_method_from_name (klass, "Get", -1);
710 *method = mono_class_get_method_from_name (klass, "Address", -1);
713 *method = mono_class_get_method_from_name (klass, "Set", -1);
716 g_assert_not_reached ();
719 g_assert (image_index < MONO_AOT_METHODREF_MIN);
720 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
722 image = load_image (module, image_index, TRUE);
733 * decode_method_ref_2:
735 * Similar to decode_method_ref, but resolve and return the method itself.
738 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
742 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
748 method = mono_get_method (image, token, NULL);
754 make_writable (guint8* addr, guint32 len)
760 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
762 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1));
763 pages = (addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ();
765 err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_READ | MONO_MMAP_WRITE | MONO_MMAP_EXEC);
770 create_cache_structure (void)
776 home = g_get_home_dir ();
780 tmp = g_build_filename (home, ".mono", NULL);
781 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
782 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
786 err = mkdir (tmp, 0777);
789 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
795 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
796 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
797 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
801 err = mkdir (tmp, 0777);
804 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
813 * load_aot_module_from_cache:
815 * Experimental code to AOT compile loaded assemblies on demand.
818 * - Add environment variable MONO_AOT_CACHE_OPTIONS
819 * - Add options for controlling the cache size
820 * - Handle full cache by deleting old assemblies lru style
821 * - Add options for excluding assemblies during development
822 * - Maybe add a threshold after an assembly is AOT compiled
823 * - invoking a new mono process is a security risk
824 * - recompile the AOT module if one of its dependencies changes
827 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
829 char *fname, *cmd, *tmp2, *aot_options;
838 if (assembly->image->dynamic)
841 create_cache_structure ();
843 home = g_get_home_dir ();
845 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
846 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
850 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
851 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
854 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
856 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
858 aot_options = g_strdup_printf ("outfile=%s", fname);
860 if (spawn_compiler) {
861 /* FIXME: security */
862 /* FIXME: Has to pass the assembly loading path to the child process */
863 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
865 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
867 #if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
869 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
870 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
872 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
879 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
881 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
883 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
887 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
889 g_free (aot_options);
896 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
900 guint16 *table, *entry;
904 /* The first entry points to the hash */
908 table_size = table [0];
911 hash = mono_metadata_str_hash (name) % table_size;
913 entry = &table [hash * 2];
915 /* Search the hash for the index into the globals table */
917 while (entry [0] != 0) {
918 guint32 index = entry [0] - 1;
919 guint32 next = entry [1];
921 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
923 if (!strcmp (globals [index * 2], name)) {
924 global_index = index;
929 entry = &table [next * 2];
935 if (global_index != -1)
936 *value = globals [global_index * 2 + 1];
940 mono_dl_symbol (module, name, value);
944 #ifndef SHT_ARM_EXIDX
945 #define SHT_ARM_EXIDX 0x70000001
948 #ifdef HAVE_DL_ITERATE_PHDR
950 dl_callback (struct dl_phdr_info *info, size_t size, void *data)
953 MonoAotModule *amodule = data;
955 if (!strcmp (amodule->aot_name, info->dlpi_name)) {
956 for (j = 0; j < info->dlpi_phnum; j++) {
957 if (info->dlpi_phdr [j].p_type == PT_GNU_EH_FRAME)
958 amodule->eh_frame_hdr = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
959 if (info->dlpi_phdr [j].p_type == SHT_ARM_EXIDX) {
960 amodule->arm_exidx = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
961 amodule->arm_exidx_size = info->dlpi_phdr [j].p_filesz;
972 load_aot_module (MonoAssembly *assembly, gpointer user_data)
975 MonoAotModule *amodule;
977 gboolean usable = TRUE;
978 char *saved_guid = NULL;
979 char *aot_version = NULL;
980 char *runtime_version, *build_info;
981 char *opt_flags = NULL;
983 gboolean full_aot = FALSE;
984 MonoAotFileInfo *file_info = NULL;
988 if (mono_compile_aot)
991 if (assembly->image->aot_module)
993 * Already loaded. This can happen because the assembly loading code might invoke
994 * the assembly load hooks multiple times for the same assembly.
998 if (assembly->image->dynamic)
1001 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
1005 if (static_aot_modules)
1006 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
1012 /* Statically linked AOT module */
1014 aot_name = g_strdup_printf ("%s", assembly->aname.name);
1015 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
1018 sofile = load_aot_module_from_cache (assembly, &aot_name);
1021 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
1023 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1026 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
1032 if (!sofile && !globals) {
1033 if (mono_aot_only) {
1034 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1041 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
1042 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
1043 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
1044 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
1045 find_symbol (sofile, globals, "mono_aot_got_addr", (gpointer *)&got_addr);
1047 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
1048 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);
1052 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
1053 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
1058 build_info = mono_get_runtime_build_info ();
1059 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
1060 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);
1063 g_free (build_info);
1065 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
1066 g_assert (file_info);
1068 full_aot = ((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1070 if (mono_aot_only && !full_aot) {
1071 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
1074 if (!mono_aot_only && full_aot) {
1075 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
1079 if ((((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1080 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with LLVM.\n", aot_name);
1085 if (mono_aot_only) {
1086 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
1091 mono_dl_close (sofile);
1092 assembly->image->aot_module = NULL;
1096 amodule = g_new0 (MonoAotModule, 1);
1097 amodule->aot_name = aot_name;
1098 amodule->assembly = assembly;
1100 memcpy (&amodule->info, file_info, sizeof (*file_info));
1102 amodule->got = *got_addr;
1103 amodule->got [0] = assembly->image;
1104 amodule->globals = globals;
1105 amodule->sofile = sofile;
1106 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1108 /* Read image table */
1110 guint32 table_len, i;
1113 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
1116 table_len = *(guint32*)table;
1117 table += sizeof (guint32);
1118 amodule->image_table = g_new0 (MonoImage*, table_len);
1119 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1120 amodule->image_guids = g_new0 (char*, table_len);
1121 amodule->image_table_len = table_len;
1122 for (i = 0; i < table_len; ++i) {
1123 MonoAssemblyName *aname = &(amodule->image_names [i]);
1125 aname->name = g_strdup (table);
1126 table += strlen (table) + 1;
1127 amodule->image_guids [i] = g_strdup (table);
1128 table += strlen (table) + 1;
1130 aname->culture = g_strdup (table);
1131 table += strlen (table) + 1;
1132 memcpy (aname->public_key_token, table, strlen (table) + 1);
1133 table += strlen (table) + 1;
1135 table = ALIGN_PTR_TO (table, 8);
1136 aname->flags = *(guint32*)table;
1138 aname->major = *(guint32*)table;
1140 aname->minor = *(guint32*)table;
1142 aname->build = *(guint32*)table;
1144 aname->revision = *(guint32*)table;
1149 /* Read method and method_info tables */
1150 find_symbol (sofile, globals, "code_offsets", (gpointer*)&amodule->code_offsets);
1151 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
1152 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
1153 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
1154 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
1155 find_symbol (sofile, globals, "blob", (gpointer*)&amodule->blob);
1156 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
1157 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
1158 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
1159 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1160 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1161 find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC]));
1162 find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX]));
1163 find_symbol (sofile, globals, "imt_thunks", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK]));
1164 find_symbol (sofile, globals, "unwind_info", (gpointer)&amodule->unwind_info);
1165 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1167 amodule->mem_begin = amodule->code;
1169 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1170 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1172 if (make_unreadable) {
1173 #ifndef TARGET_WIN32
1176 int pages, err, len;
1178 addr = amodule->mem_begin;
1179 len = amodule->mem_end - amodule->mem_begin;
1181 /* Round down in both directions to avoid modifying data which is not ours */
1182 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1183 pages = ((addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ()) - 1;
1184 err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_NONE);
1185 g_assert (err == 0);
1191 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1192 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1194 g_hash_table_insert (aot_modules, assembly, amodule);
1197 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1199 assembly->image->aot_module = amodule;
1201 #ifdef HAVE_DL_ITERATE_PHDR
1202 /* Lookup the address of the .eh_frame_hdr () section if available */
1203 dl_iterate_phdr (dl_callback, amodule);
1206 if (mono_aot_only) {
1207 if (mono_defaults.corlib) {
1208 /* The second got slot contains the mscorlib got addr */
1209 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1211 amodule->got [1] = mscorlib_amodule->got;
1213 amodule->got [1] = amodule->got;
1218 * Since we store methoddef and classdef tokens when referring to methods/classes in
1219 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1220 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1221 * non-lazily, since we can't handle out-of-date errors later.
1222 * The cached class info also depends on the exact assemblies.
1224 for (i = 0; i < amodule->image_table_len; ++i)
1225 load_image (amodule, i, FALSE);
1227 if (amodule->out_of_date) {
1228 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);
1229 if (mono_aot_only) {
1230 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);
1235 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1239 * mono_aot_register_globals:
1241 * This is called by the ctor function in AOT images compiled with the
1242 * 'no-dlsym' option.
1245 mono_aot_register_globals (gpointer *globals)
1247 g_assert_not_reached ();
1251 * mono_aot_register_module:
1253 * This should be called by embedding code to register AOT modules statically linked
1254 * into the executable. AOT_INFO should be the value of the
1255 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1258 mono_aot_register_module (gpointer *aot_info)
1266 /* Determine the assembly name */
1267 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1270 /* This could be called before startup */
1274 if (!static_aot_modules)
1275 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1277 g_hash_table_insert (static_aot_modules, aname, globals);
1284 mono_aot_init (void)
1286 InitializeCriticalSection (&aot_mutex);
1287 aot_modules = g_hash_table_new (NULL, NULL);
1289 mono_install_assembly_load_hook (load_aot_module, NULL);
1291 if (g_getenv ("MONO_LASTAOT"))
1292 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1293 if (g_getenv ("MONO_AOT_CACHE"))
1294 use_aot_cache = TRUE;
1298 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1302 info->vtable_size = decode_value (buf, &buf);
1303 if (info->vtable_size == -1)
1306 flags = decode_value (buf, &buf);
1307 info->ghcimpl = (flags >> 0) & 0x1;
1308 info->has_finalize = (flags >> 1) & 0x1;
1309 info->has_cctor = (flags >> 2) & 0x1;
1310 info->has_nested_classes = (flags >> 3) & 0x1;
1311 info->blittable = (flags >> 4) & 0x1;
1312 info->has_references = (flags >> 5) & 0x1;
1313 info->has_static_refs = (flags >> 6) & 0x1;
1314 info->no_special_static_fields = (flags >> 7) & 0x1;
1315 info->is_generic_container = (flags >> 8) & 0x1;
1317 if (info->has_cctor) {
1318 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1322 if (info->has_finalize) {
1323 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1324 if (!info->finalize_image)
1328 info->instance_size = decode_value (buf, &buf);
1329 info->class_size = decode_value (buf, &buf);
1330 info->packing_size = decode_value (buf, &buf);
1331 info->min_align = decode_value (buf, &buf);
1339 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1342 MonoClass *klass = vtable->klass;
1343 MonoAotModule *aot_module = klass->image->aot_module;
1345 MonoCachedClassInfo class_info;
1349 gboolean no_aot_trampoline;
1351 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1354 info = &aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1357 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1361 for (i = 0; i < slot; ++i)
1362 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1364 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1367 if (no_aot_trampoline)
1370 if (mono_metadata_token_index (token) == 0)
1373 return mono_aot_get_method_from_token (domain, image, token);
1377 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1379 MonoAotModule *aot_module = klass->image->aot_module;
1383 if (klass->rank || !aot_module)
1386 p = (guint8*)&aot_module->blob [mono_aot_get_offset (aot_module->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1388 err = decode_cached_class_info (aot_module, res, p, &p);
1396 * mono_aot_get_class_from_name:
1398 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1399 * using a cache stored in the AOT file.
1400 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1402 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1406 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1408 MonoAotModule *aot_module = image->aot_module;
1409 guint16 *table, *entry;
1412 char full_name_buf [1024];
1414 const char *name2, *name_space2;
1416 guint32 cols [MONO_TYPEDEF_SIZE];
1417 GHashTable *nspace_table;
1419 if (!aot_module || !aot_module->class_name_table)
1426 /* First look in the cache */
1427 if (!aot_module->name_cache)
1428 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1429 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1431 *klass = g_hash_table_lookup (nspace_table, name);
1438 table_size = aot_module->class_name_table [0];
1439 table = aot_module->class_name_table + 1;
1441 if (name_space [0] == '\0')
1442 full_name = g_strdup_printf ("%s", name);
1444 if (strlen (name_space) + strlen (name) < 1000) {
1445 sprintf (full_name_buf, "%s.%s", name_space, name);
1446 full_name = full_name_buf;
1448 full_name = g_strdup_printf ("%s.%s", name_space, name);
1451 hash = mono_metadata_str_hash (full_name) % table_size;
1452 if (full_name != full_name_buf)
1455 entry = &table [hash * 2];
1457 if (entry [0] != 0) {
1458 t = &image->tables [MONO_TABLE_TYPEDEF];
1461 guint32 index = entry [0];
1462 guint32 next = entry [1];
1463 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1465 name_table_accesses ++;
1467 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1469 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1470 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1472 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1474 *klass = mono_class_get (image, token);
1479 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1480 if (!nspace_table) {
1481 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1482 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1484 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1491 entry = &table [next * 2];
1503 #define DW_EH_PE_omit 0xff
1504 #define DW_EH_PE_uleb128 0x01
1505 #define DW_EH_PE_udata2 0x02
1506 #define DW_EH_PE_udata4 0x03
1507 #define DW_EH_PE_udata8 0x04
1508 #define DW_EH_PE_sleb128 0x09
1509 #define DW_EH_PE_sdata2 0x0A
1510 #define DW_EH_PE_sdata4 0x0B
1511 #define DW_EH_PE_sdata8 0x0C
1513 #define DW_EH_PE_absptr 0x00
1514 #define DW_EH_PE_pcrel 0x10
1515 #define DW_EH_PE_datarel 0x30
1516 #define DW_EH_PE_omit 0xff
1521 guint8 eh_frame_ptr_enc;
1522 guint8 fde_count_enc;
1530 * Decode the exception handling information in the .eh_frame section of the AOT
1531 * file belong to CODE, and construct a MonoJitInfo structure from it.
1532 * LOCKING: Acquires the domain lock.
1534 static G_GNUC_UNUSED void
1535 decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
1536 MonoMethod *method, guint8 *code, MonoJitInfo *jinfo)
1540 guint8 *eh_frame, *unwind_info;
1541 guint32 eh_frame_ptr;
1544 int i, pos, left, right, offset, offset1, offset2;
1545 guint32 unw_len, code_len;
1546 MonoJitExceptionInfo *ei;
1549 g_assert (amodule->eh_frame_hdr);
1551 // http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
1552 hdr = (eh_frame_hdr*)amodule->eh_frame_hdr;
1553 g_assert (hdr->version == 1);
1554 g_assert (hdr->eh_frame_ptr_enc == (DW_EH_PE_pcrel | DW_EH_PE_sdata4));
1555 g_assert (hdr->fde_count_enc == DW_EH_PE_udata4);
1556 g_assert (hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4));
1559 eh_frame_ptr = *(guint32*)p;
1561 fde_count = *(guint32*)p;
1565 /* Binary search in the table to find the entry for code */
1566 offset = code - amodule->eh_frame_hdr;
1571 pos = (left + right) / 2;
1573 offset1 = table [(pos * 2)];
1574 if (pos + 1 == fde_count)
1576 offset2 = amodule->code_end - amodule->code;
1578 offset2 = table [(pos + 1) * 2];
1580 if (offset < offset1)
1582 else if (offset >= offset2)
1588 g_assert (code >= amodule->eh_frame_hdr + table [(pos * 2)]);
1589 if (pos < fde_count)
1590 g_assert (code < amodule->eh_frame_hdr + table [(pos * 2) + 2]);
1592 eh_frame = amodule->eh_frame_hdr + table [(pos * 2) + 1];
1594 unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len, NULL);
1596 jinfo->code_size = code_len;
1597 jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
1598 jinfo->method = method;
1599 jinfo->code_start = code;
1600 jinfo->domain_neutral = 0;
1601 /* This signals that used_regs points to a normal cached unwind info */
1602 jinfo->from_aot = 0;
1604 g_assert (ei_len == jinfo->num_clauses);
1605 for (i = 0; i < jinfo->num_clauses; ++i) {
1606 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
1608 jei->try_start = ei [i].try_start;
1609 jei->try_end = ei [i].try_end;
1610 jei->handler_start = ei [i].handler_start;
1616 /* The offsets in the table are 31 bits long, have to extend them to 32 */
1617 #define EXTEND_PREL31(val) ((((gint32)(val)) << 1) >> 1)
1619 static inline guint32
1620 decode_uleb128 (guint8 *buf, guint8 **endbuf)
1630 res = res | (((int)(b & 0x7f)) << shift);
1642 decode_arm_eh_ops (guint8 *unwind_ops, int nops)
1644 int i, vsp_reg, vsp_offset;
1646 gint32 *reg_offsets;
1649 * Have to convert the ARM unwind info into DWARF unwind info.
1650 * The ARM unwind info specifies a simple set of instructions which need to be
1651 * executed during unwinding. It manipulates a virtual stack pointer (vsp). The
1652 * connection with DWARF unwind info is the following: after all ARM unwind
1653 * opcodes have been executed, the stack should be completely unwound, i.e.
1654 * vsp == DWARF CFA. This allows us to construct the DWARF opcodes corresponding
1655 * to the ARM opcodes.
1656 * The ARM unwind info is not instruction precise, i. e. it can't handle
1657 * async exceptions etc.
1659 /* The reg used to compute the initial value of vsp */
1660 vsp_reg = ARMREG_SP;
1661 /* The offset between vsp_reg and the CFA */
1664 /* The register save offsets from the initial value of vsp */
1665 reg_offsets = g_new0 (gint32, 16);
1666 for (i = 0; i < 16; ++i)
1667 reg_offsets [i] = -1;
1669 /* section 9.3 in the ehabi doc */
1670 for (i = 0; i < nops; ++i) {
1671 guint8 op = unwind_ops [i];
1673 if ((op >> 6) == 0) {
1674 /* vsp = vsp + (xxxxxx << 2) + 4. */
1675 vsp_offset += ((op & 0x3f) << 2) + 4;
1676 } else if ((op >> 6) == 1) {
1677 /* vsp = vsp - (xxxxxx << 2) - 4. */
1678 vsp_offset -= ((op & 0x3f) << 2) + 4;
1679 } else if (op == 0xb2) {
1680 /* vsp = vsp = vsp + 0x204 + (uleb128 << 2) */
1681 guint8 *p = unwind_ops + i + 1;
1682 guint32 v = decode_uleb128 (p, &p);
1684 vsp_offset += 0x204 + (v << 2);
1685 i = (p - unwind_ops) - 1;
1686 } else if (op >= 0x80 && op <= 0x8f) {
1692 g_assert (i + 1 < nops);
1693 op2 = unwind_ops [i + 1];
1696 for (j = 0; j < 8; ++j)
1697 if (op2 & (0x1 << j))
1698 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j));
1699 for (j = 0; j < 4; ++j)
1700 if (op & (0x1 << j))
1701 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R12 + j));
1704 for (j = 0; j < g_slist_length (regs); ++j)
1705 reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4);
1707 vsp_offset += g_slist_length (regs) * 4;
1709 g_slist_free (regs);
1712 } else if (op >= 0xa8 && op <= 0xaf) {
1716 /* pop r4-r[4 + nnn], r14 */
1719 for (j = 0; j <= (op & 0x7); ++j)
1720 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R4 + j));
1721 regs = g_slist_append (regs, GUINT_TO_POINTER (ARMREG_R14));
1723 for (j = 0; j < g_slist_length (regs); ++j)
1724 reg_offsets [GPOINTER_TO_UINT (g_slist_nth (regs, j)->data)] = vsp_offset + (j * 4);
1726 vsp_offset += g_slist_length (regs) * 4;
1728 g_slist_free (regs);
1729 } else if (op == 0xb0) {
1732 } else if (op >= 0x90 && op <= 0x9f && op != 0x9d && op != 0x9f) {
1739 for (j = 0; j < nops; ++j)
1740 printf ("%x ", unwind_ops [j]);
1741 printf (" / %d\n", i);
1742 g_assert_not_reached ();
1748 /* vsp_reg + vsp_offset = CFA */
1749 mono_add_unwind_op_def_cfa (ops, (guint8*)NULL, (guint8*)NULL, vsp_reg, vsp_offset);
1751 for (i = 0; i < 16; ++i) {
1752 if (reg_offsets [i] != -1)
1753 /* The reg is saved at vsp_reg + reg_offset [i] == CFA - (vsp_offset - reg_offset [i]) */
1754 mono_add_unwind_op_offset (ops, (guint8*)NULL, (guint8*)NULL, i, - (vsp_offset - reg_offsets [i]));
1763 * Decode the exception handling information in the .ARM.exidx section of the AOT
1764 * file belong to CODE, and construct a MonoJitInfo structure from it.
1765 * LOCKING: Acquires the domain lock.
1768 decode_arm_exidx (MonoAotModule *amodule, MonoDomain *domain,
1769 MonoMethod *method, guint8 *code, guint32 code_len, MonoJitInfo *jinfo)
1772 guint8 *base, *code1, *code2;
1773 int i, pos, left, right, offset, offset1, offset2, count, nwords, nops;
1775 guint8 unwind_ops [64];
1777 guint8 *unwind_info;
1780 g_assert (amodule->arm_exidx);
1782 table = (guint32*)amodule->arm_exidx;
1785 * The table format is described in:
1786 * infocenter.arm.com/help/topic/com.arm.doc.../IHI0038A_ehabi.pdf
1789 base = amodule->arm_exidx;
1790 count = amodule->arm_exidx_size / 8;
1792 /* Binary search in the table to find the entry for code */
1793 offset = code - base;
1798 pos = (left + right) / 2;
1803 offset1 = EXTEND_PREL31 (table [(pos * 2)]);
1804 code1 = (guint8*)&(table [pos * 2]) + offset1;
1805 if (pos + 1 == count)
1807 offset2 = amodule->code_end - amodule->code;
1809 offset2 = EXTEND_PREL31 (table [(pos + 1) * 2]);
1810 code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2;
1814 else if (code >= code2)
1820 if (code >= code1) {
1822 * The linker might merge duplicate unwind table entries, so
1823 * offset1 and offset2 might point to another method, but this is not a problem.
1825 code1 = (guint8*)&(table [pos * 2]) + offset1;
1826 code2 = (guint8*)&(table [(pos + 1) * 2]) + offset2;
1828 g_assert (code >= code1);
1830 g_assert (code < code2);
1832 entry = table [(pos * 2) + 1];
1834 /* inline entry, compact model, personality routine 0 */
1835 if ((entry & 0xff000000) == 0x80000000) {
1837 unwind_ops [0] = (entry & 0x00ff0000) >> 16;
1838 unwind_ops [1] = (entry & 0x0000ff00) >> 8;
1839 unwind_ops [2] = (entry & 0x000000ff) >> 0;
1841 ops = decode_arm_eh_ops (unwind_ops, nops);
1842 } else if ((entry & 0x80000000) == 0) {
1843 /* non-inline entry */
1844 guint8 *data = (guint8*)&table [(pos * 2) + 1] + EXTEND_PREL31 (entry);
1846 entry = ((guint32*)data) [0];
1848 /* compact model, personality routine 1 */
1849 g_assert ((entry & 0xff000000) == 0x81000000);
1851 nwords = (entry & 0x00ff0000) >> 16;
1852 nops = nwords * 4 + 2;
1853 g_assert (nops < 64);
1855 unwind_ops [0] = (entry & 0x0000ff00) >> 8;
1856 unwind_ops [1] = (entry & 0x000000ff) >> 0;
1858 for (i = 0; i < nwords; ++i) {
1859 entry = ((guint32*)data) [1 + i];
1860 unwind_ops [(i * 4) + 2] = (entry & 0xff000000) >> 24;
1861 unwind_ops [(i * 4) + 2 + 1] = (entry & 0x00ff0000) >> 16;
1862 unwind_ops [(i * 4) + 2 + 2] = (entry & 0x0000ff00) >> 8;
1863 unwind_ops [(i * 4) + 2 + 3] = (entry & 0x000000ff) >> 0;
1866 ops = decode_arm_eh_ops (unwind_ops, nops);
1871 unwind_info = mono_unwind_ops_encode (ops, &unw_len);
1873 /* The method has no unwind info */
1878 jinfo->code_size = code_len;
1879 jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
1880 jinfo->method = method;
1881 jinfo->code_start = code;
1882 jinfo->domain_neutral = 0;
1883 /* This signals that used_regs points to a normal cached unwind info */
1884 jinfo->from_aot = 0;
1889 * LOCKING: Acquires the domain lock.
1892 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
1893 MonoMethod *method, guint8* ex_info, guint8 *addr,
1894 guint8 *code, guint32 code_len)
1898 guint used_int_regs, flags;
1899 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points;
1902 int generic_info_size;
1904 /* Load the method info from the AOT file */
1907 flags = decode_value (p, &p);
1908 has_generic_jit_info = (flags & 1) != 0;
1909 has_dwarf_unwind_info = (flags & 2) != 0;
1910 has_clauses = (flags & 4) != 0;
1911 has_seq_points = (flags & 8) != 0;
1912 from_llvm = (flags & 16) != 0;
1913 if (has_dwarf_unwind_info) {
1916 offset = decode_value (p, &p);
1917 g_assert (offset < (1 << 30));
1918 used_int_regs = offset;
1920 used_int_regs = decode_value (p, &p);
1922 if (has_generic_jit_info)
1923 generic_info_size = sizeof (MonoGenericJitInfo);
1925 generic_info_size = 0;
1927 /* Exception table */
1929 int num_clauses = decode_value (p, &p);
1932 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size);
1933 jinfo->num_clauses = num_clauses;
1935 for (i = 0; i < num_clauses; ++i) {
1936 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1938 ei->flags = decode_value (p, &p);
1939 ei->exvar_offset = decode_value (p, &p);
1941 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1942 ei->data.filter = code + decode_value (p, &p);
1944 if (decode_value (p, &p))
1945 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1948 ei->try_start = code + decode_value (p, &p);
1949 ei->try_end = code + decode_value (p, &p);
1950 ei->handler_start = code + decode_value (p, &p);
1954 jinfo = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + generic_info_size);
1958 /* LLVM compiled method */
1959 /* The info is in the .eh_frame section */
1961 decode_arm_exidx (amodule, domain, method, code, code_len, jinfo);
1963 decode_eh_frame (amodule, domain, method, code, jinfo);
1965 jinfo->from_llvm = 1;
1967 jinfo->code_size = code_len;
1968 jinfo->used_regs = used_int_regs;
1969 jinfo->method = method;
1970 jinfo->code_start = code;
1971 jinfo->domain_neutral = 0;
1972 jinfo->from_aot = 1;
1975 if (has_generic_jit_info) {
1976 MonoGenericJitInfo *gi;
1978 jinfo->has_generic_jit_info = 1;
1980 gi = mono_jit_info_get_generic_jit_info (jinfo);
1983 gi->has_this = decode_value (p, &p);
1984 gi->this_reg = decode_value (p, &p);
1985 gi->this_offset = decode_value (p, &p);
1987 /* This currently contains no data */
1988 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1990 jinfo->method = decode_method_ref_2 (amodule, p, &p);
1993 if (has_seq_points) {
1994 MonoSeqPointInfo *seq_points;
1995 int il_offset, native_offset, last_il_offset, last_native_offset, j;
1997 int len = decode_value (p, &p);
1999 seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
2000 last_il_offset = last_native_offset = 0;
2001 for (i = 0; i < len; ++i) {
2002 SeqPoint *sp = &seq_points->seq_points [i];
2003 il_offset = last_il_offset + decode_value (p, &p);
2004 native_offset = last_native_offset + decode_value (p, &p);
2006 sp->il_offset = il_offset;
2007 sp->native_offset = native_offset;
2009 sp->next_len = decode_value (p, &p);
2010 sp->next = g_new (int, sp->next_len);
2011 for (j = 0; j < sp->next_len; ++j)
2012 sp->next [j] = decode_value (p, &p);
2014 last_il_offset = il_offset;
2015 last_native_offset = native_offset;
2018 mono_domain_lock (domain);
2019 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
2020 mono_domain_unlock (domain);
2023 /* Load debug info */
2024 buf_len = decode_value (p, &p);
2025 mono_debug_add_aot_method (domain, method, code, p, buf_len);
2027 if (amodule != jinfo->method->klass->image->aot_module) {
2030 ji_to_amodule = g_hash_table_new (NULL, NULL);
2031 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
2039 * mono_aot_get_unwind_info:
2041 * Return a pointer to the DWARF unwind info belonging to JI.
2044 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
2046 MonoAotModule *amodule = ji->method->klass->image->aot_module;
2048 guint8 *code = ji->code_start;
2051 g_assert (ji->from_aot);
2053 if (!(code >= amodule->code && code <= amodule->code_end)) {
2054 /* ji belongs to a different aot module than amodule */
2056 g_assert (ji_to_amodule);
2057 amodule = g_hash_table_lookup (ji_to_amodule, ji);
2059 g_assert (code >= amodule->code && code <= amodule->code_end);
2062 p = amodule->unwind_info + ji->used_regs;
2063 *unwind_info_len = decode_value (p, &p);
2068 compare_ints (const void *a, const void *b)
2070 return *(gint32*)a - *(gint32*)b;
2074 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2076 int pos, left, right, offset, offset1, offset2, code_len;
2077 int method_index, table_len, is_wrapper;
2079 MonoAotModule *amodule = image->aot_module;
2082 guint8 *code, *ex_info, *p;
2084 int nmethods = amodule->info.nmethods;
2085 gint32 *code_offsets;
2091 if (domain != mono_get_root_domain ())
2095 offset = (guint8*)addr - amodule->code;
2097 /* Compute a sorted table mapping code offsets to method indexes. */
2098 if (!amodule->sorted_code_offsets) {
2099 code_offsets = g_new0 (gint32, nmethods * 2);
2100 for (i = 0; i < nmethods; ++i) {
2101 code_offsets [(i * 2)] = amodule->code_offsets [i];
2102 code_offsets [(i *2) + 1] = i;
2104 /* FIXME: Use a merge sort as this is mostly sorted */
2105 qsort (code_offsets, nmethods, sizeof (gint32) * 2, compare_ints);
2106 for (i = 0; i < nmethods -1; ++i)
2107 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
2109 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
2110 /* Somebody got in before us */
2111 g_free (code_offsets);
2114 code_offsets = amodule->sorted_code_offsets;
2116 /* Binary search in the sorted_code_offsets table */
2120 pos = (left + right) / 2;
2122 offset1 = code_offsets [(pos * 2)];
2123 if (pos + 1 == nmethods)
2124 offset2 = amodule->code_end - amodule->code;
2126 offset2 = code_offsets [(pos + 1) * 2];
2128 if (offset < offset1)
2130 else if (offset >= offset2)
2136 g_assert (offset >= code_offsets [(pos * 2)]);
2137 if (pos + 1 < nmethods)
2138 g_assert (offset < code_offsets [((pos + 1) * 2)]);
2139 method_index = code_offsets [(pos * 2) + 1];
2141 code = &amodule->code [amodule->code_offsets [method_index]];
2142 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
2144 if (pos == nmethods - 1)
2145 code_len = amodule->code_end - code;
2147 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
2149 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
2151 /* Might be a wrapper/extra method */
2152 if (amodule->extra_methods) {
2154 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
2161 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
2163 * This is hit for extra methods which are called directly, so they are
2164 * not in amodule->extra_methods.
2166 table_len = amodule->extra_method_info_offsets [0];
2167 table = amodule->extra_method_info_offsets + 1;
2174 pos = ((left + right) / 2);
2176 g_assert (pos < table_len);
2178 if (table [pos * 2] < method_index)
2180 else if (table [pos * 2] > method_index)
2186 p = amodule->blob + table [(pos * 2) + 1];
2187 is_wrapper = decode_value (p, &p);
2188 g_assert (!is_wrapper);
2189 method = decode_method_ref_2 (amodule, p, &p);
2192 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
2193 method = mono_get_method (image, token, NULL);
2200 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
2202 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
2204 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
2205 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
2207 /* Add it to the normal JitInfo tables */
2208 mono_jit_info_table_add (domain, jinfo);
2214 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
2222 case MONO_PATCH_INFO_METHOD:
2223 case MONO_PATCH_INFO_METHOD_JUMP:
2224 case MONO_PATCH_INFO_ICALL_ADDR:
2225 case MONO_PATCH_INFO_METHOD_RGCTX: {
2228 gboolean no_aot_trampoline;
2230 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
2234 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
2235 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (image, token));
2236 ji->type = MONO_PATCH_INFO_ABS;
2240 ji->data.method = method;
2242 ji->data.method = mono_get_method (image, token, NULL);
2243 g_assert (ji->data.method);
2244 mono_class_init (ji->data.method->klass);
2248 case MONO_PATCH_INFO_INTERNAL_METHOD:
2249 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2250 guint32 len = decode_value (p, &p);
2252 ji->data.name = (char*)p;
2256 case MONO_PATCH_INFO_METHODCONST:
2258 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
2259 if (!ji->data.method)
2262 case MONO_PATCH_INFO_VTABLE:
2263 case MONO_PATCH_INFO_CLASS:
2264 case MONO_PATCH_INFO_IID:
2265 case MONO_PATCH_INFO_ADJUSTED_IID:
2267 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2268 if (!ji->data.klass)
2271 case MONO_PATCH_INFO_CLASS_INIT:
2272 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2273 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2274 if (!ji->data.klass)
2277 case MONO_PATCH_INFO_IMAGE:
2278 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
2279 if (!ji->data.image)
2282 case MONO_PATCH_INFO_FIELD:
2283 case MONO_PATCH_INFO_SFLDA:
2285 ji->data.field = decode_field_info (aot_module, p, &p);
2286 if (!ji->data.field)
2289 case MONO_PATCH_INFO_SWITCH:
2290 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
2291 ji->data.table->table_size = decode_value (p, &p);
2292 table = g_new (gpointer, ji->data.table->table_size);
2293 ji->data.table->table = (MonoBasicBlock**)table;
2294 for (i = 0; i < ji->data.table->table_size; i++)
2295 table [i] = (gpointer)(gssize)decode_value (p, &p);
2297 case MONO_PATCH_INFO_R4: {
2300 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
2301 val = decode_value (p, &p);
2302 *(float*)ji->data.target = *(float*)&val;
2305 case MONO_PATCH_INFO_R8: {
2309 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
2311 val [0] = decode_value (p, &p);
2312 val [1] = decode_value (p, &p);
2313 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
2314 *(double*)ji->data.target = *(double*)&v;
2317 case MONO_PATCH_INFO_LDSTR:
2318 image = load_image (aot_module, decode_value (p, &p), TRUE);
2321 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
2323 case MONO_PATCH_INFO_RVA:
2324 case MONO_PATCH_INFO_DECLSEC:
2325 case MONO_PATCH_INFO_LDTOKEN:
2326 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2328 image = load_image (aot_module, decode_value (p, &p), TRUE);
2331 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
2333 ji->data.token->has_context = decode_value (p, &p);
2334 if (ji->data.token->has_context) {
2335 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
2340 case MONO_PATCH_INFO_EXC_NAME:
2341 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2342 if (!ji->data.klass)
2344 ji->data.name = ji->data.klass->name;
2346 case MONO_PATCH_INFO_METHOD_REL:
2347 ji->data.offset = decode_value (p, &p);
2349 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2350 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2351 case MONO_PATCH_INFO_MONITOR_ENTER:
2352 case MONO_PATCH_INFO_MONITOR_EXIT:
2354 case MONO_PATCH_INFO_RGCTX_FETCH: {
2356 MonoJumpInfoRgctxEntry *entry;
2358 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2359 entry->method = decode_method_ref_2 (aot_module, p, &p);
2360 entry->in_mrgctx = decode_value (p, &p);
2361 entry->info_type = decode_value (p, &p);
2362 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2363 entry->data->type = decode_value (p, &p);
2365 res = decode_patch (aot_module, mp, entry->data, p, &p);
2368 ji->data.rgctx_entry = entry;
2371 case MONO_PATCH_INFO_SEQ_POINT_INFO:
2373 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
2374 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
2376 imt_tramp->method = decode_method_ref_2 (aot_module, p, &p);
2377 imt_tramp->vt_offset = decode_value (p, &p);
2379 ji->data.imt_tramp = imt_tramp;
2383 g_warning ("unhandled type %d", ji->type);
2384 g_assert_not_reached ();
2395 static MonoJumpInfo*
2396 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
2397 guint32 **got_slots,
2398 guint8 *buf, guint8 **endbuf)
2400 MonoJumpInfo *patches;
2406 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
2408 *got_slots = g_malloc (sizeof (guint32) * n_patches);
2410 for (pindex = 0; pindex < n_patches; ++pindex) {
2411 MonoJumpInfo *ji = &patches [pindex];
2416 got_offset = decode_value (p, &p);
2418 if (aot_module->got [got_offset]) {
2419 /* Already loaded */
2420 //printf ("HIT!\n");
2422 shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
2424 ji->type = decode_value (shared_p, &shared_p);
2426 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
2431 (*got_slots) [pindex] = got_offset;
2438 g_free (*got_slots);
2445 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
2448 * Jump addresses cannot be patched by the trampoline code since it
2449 * does not have access to the caller's address. Instead, we collect
2450 * the addresses of the GOT slots pointing to a method, and patch
2451 * them after the method has been compiled.
2453 MonoJitDomainInfo *info = domain_jit_info (domain);
2456 mono_domain_lock (domain);
2457 if (!info->jump_target_got_slot_hash)
2458 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
2459 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
2460 list = g_slist_prepend (list, got_slot);
2461 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
2462 mono_domain_unlock (domain);
2468 * Load the method identified by METHOD_INDEX from the AOT image. Return a
2469 * pointer to the native code of the method, or NULL if not found.
2470 * METHOD might not be set if the caller only has the image/token info.
2473 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
2476 gboolean from_plt = method == NULL;
2478 int i, pindex, n_patches, used_strings;
2479 gboolean keep_patches = TRUE;
2481 MonoJitInfo *jinfo = NULL;
2482 guint8 *code, *info;
2484 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2487 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
2488 /* Non shared AOT code can't be used in other appdomains */
2491 if (amodule->out_of_date)
2494 if (amodule->code_offsets [method_index] == 0xffffffff) {
2495 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2499 method = mono_get_method (image, token, NULL);
2500 full_name = mono_method_full_name (method, TRUE);
2501 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2507 code = &amodule->code [amodule->code_offsets [method_index]];
2509 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
2512 if (!amodule->methods_loaded)
2513 amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods + 1);
2516 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2519 if (mono_last_aot_method != -1) {
2520 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
2522 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
2524 method = mono_get_method (image, token, NULL);
2526 char *name = mono_method_full_name (method, TRUE);
2527 printf ("LAST AOT METHOD: %s.\n", name);
2530 printf ("LAST AOT METHOD: %p %d\n", code, method_index);
2538 klass = method->klass;
2539 decode_klass_ref (amodule, p, &p);
2541 klass = decode_klass_ref (amodule, p, &p);
2544 if (amodule->info.opts & MONO_OPT_SHARED)
2545 used_strings = decode_value (p, &p);
2549 for (i = 0; i < used_strings; i++) {
2550 guint token = decode_value (p, &p);
2551 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2554 if (amodule->info.opts & MONO_OPT_SHARED)
2555 keep_patches = FALSE;
2557 n_patches = decode_value (p, &p);
2559 keep_patches = FALSE;
2562 MonoJumpInfo *patches;
2568 mp = mono_mempool_new ();
2570 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2571 if (patches == NULL)
2574 for (pindex = 0; pindex < n_patches; ++pindex) {
2575 MonoJumpInfo *ji = &patches [pindex];
2577 if (!amodule->got [got_slots [pindex]]) {
2578 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2579 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2580 amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
2581 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2582 register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
2584 ji->type = MONO_PATCH_INFO_NONE;
2590 mono_mempool_destroy (mp);
2593 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2597 method = mono_get_method (image, token, NULL);
2599 full_name = mono_method_full_name (method, TRUE);
2602 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
2604 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);
2610 mono_jit_stats.methods_aot++;
2612 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2616 if (method && method->wrapper_type)
2617 g_hash_table_insert (amodule->method_to_code, method, code);
2621 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
2625 method = mono_get_method (image, token, NULL);
2628 mono_profiler_method_jit (method);
2629 jinfo = mono_jit_info_table_find (domain, (char*)code);
2631 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
2634 if (from_plt && klass && !klass->generic_container)
2635 mono_runtime_class_init (mono_class_vtable (domain, klass));
2640 /* FIXME: The space in domain->mp is wasted */
2641 if (amodule->info.opts & MONO_OPT_SHARED)
2642 /* No need to cache patches */
2643 mono_mempool_destroy (mp);
2652 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name)
2654 guint32 table_size, entry_size, hash;
2655 guint32 *table, *entry;
2657 static guint32 n_extra_decodes;
2662 table_size = amodule->extra_method_table [0];
2663 table = amodule->extra_method_table + 1;
2666 hash = mono_aot_method_hash (method) % table_size;
2668 entry = &table [hash * entry_size];
2675 guint32 key = entry [0];
2676 guint32 value = entry [1];
2677 guint32 next = entry [entry_size - 1];
2680 int is_wrapper_name;
2682 p = amodule->blob + key;
2683 is_wrapper_name = decode_value (p, &p);
2684 if (is_wrapper_name) {
2685 int wrapper_type = decode_value (p, &p);
2686 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2690 } else if (can_method_ref_match_method (amodule, p, method)) {
2692 if (!amodule->method_ref_to_method)
2693 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2694 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2698 m = decode_method_ref_2 (amodule, p, &p);
2701 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2710 /* Special case: wrappers of shared generic methods */
2711 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2712 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2713 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2714 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2716 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2722 /* Methods decoded needlessly */
2725 printf ("%d %s %s\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
2731 entry = &table [next * entry_size];
2740 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2742 g_ptr_array_add ((GPtrArray*)user_data, value);
2746 * find_extra_method:
2748 * Try finding METHOD in the extra_method table in all AOT images.
2749 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2750 * module where the method was found.
2753 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2760 if (method->wrapper_type)
2761 name = mono_aot_wrapper_name (method);
2763 /* Try the method's module first */
2764 *out_amodule = method->klass->image->aot_module;
2765 index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name);
2766 if (index != 0xffffff) {
2772 * Try all other modules.
2773 * This is needed because generic instances klass->image points to the image
2774 * containing the generic definition, but the native code is generated to the
2775 * AOT image which contains the reference.
2778 /* Make a copy to avoid doing the search inside the aot lock */
2779 modules = g_ptr_array_new ();
2781 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2785 for (i = 0; i < modules->len; ++i) {
2786 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2788 if (amodule != method->klass->image->aot_module)
2789 index = find_extra_method_in_amodule (amodule, method, name);
2790 if (index != 0xffffff) {
2791 *out_amodule = amodule;
2796 g_ptr_array_free (modules, TRUE);
2803 * mono_aot_get_method:
2805 * Return a pointer to the AOTed native code for METHOD if it can be found,
2807 * On platforms with function pointers, this doesn't return a function pointer.
2810 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2812 MonoClass *klass = method->klass;
2813 guint32 method_index;
2814 MonoAotModule *amodule = klass->image->aot_module;
2820 if (amodule->out_of_date)
2823 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2824 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2825 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2826 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2830 * Use the original method instead of its invoke-with-check wrapper.
2831 * This is not a problem when using full-aot, since it doesn't support
2834 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
2835 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
2837 g_assert (klass->inited);
2839 /* Find method index */
2840 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2841 method = mono_method_get_declaring_generic_method (method);
2842 method_index = mono_metadata_token_index (method->token) - 1;
2843 } else if (method->is_inflated || !method->token) {
2844 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2846 code = g_hash_table_lookup (amodule->method_to_code, method);
2851 method_index = find_extra_method (method, &amodule);
2853 * Special case the ICollection<T> wrappers for arrays, as they cannot
2854 * be statically enumerated, and each wrapper ends up calling the same
2857 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
2858 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
2860 code = mono_aot_get_method (domain, m);
2862 if (mono_method_needs_static_rgctx_invoke (m, FALSE)) {
2863 code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code));
2864 /* The call above returns an ftnptr */
2865 code = mono_get_addr_from_ftnptr (code);
2873 * Special case Array.GetGenericValueImpl which is a generic icall.
2874 * Generic sharing currently can't handle it, but the icall returns data using
2875 * an out parameter, so the managed-to-native wrappers can share the same code.
2877 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
2879 MonoGenericContext ctx;
2880 MonoType *args [16];
2882 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
2883 /* Avoid recursion */
2886 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
2889 memset (&ctx, 0, sizeof (ctx));
2890 args [0] = &mono_defaults.object_class->byval_arg;
2891 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2893 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2896 * Get the code for the <object> instantiation which should be emitted into
2897 * the mscorlib aot image by the AOT compiler.
2899 code = mono_aot_get_method (domain, m);
2904 if (method_index == 0xffffff) {
2905 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2908 full_name = mono_method_full_name (method, TRUE);
2909 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2915 if (method_index == 0xffffff)
2918 /* Needed by find_jit_info */
2920 if (!amodule->extra_methods)
2921 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2922 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2926 method_index = mono_metadata_token_index (method->token) - 1;
2929 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2933 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2937 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2939 MonoAotModule *aot_module = image->aot_module;
2945 method_index = mono_metadata_token_index (token) - 1;
2947 return load_method (domain, aot_module, image, NULL, token, method_index);
2953 } IsGotEntryUserData;
2956 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2958 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2959 MonoAotModule *aot_module = (MonoAotModule*)value;
2961 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
2966 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2968 IsGotEntryUserData user_data;
2973 user_data.addr = addr;
2974 user_data.res = FALSE;
2976 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2979 return user_data.res;
2984 MonoAotModule *module;
2985 } FindAotModuleUserData;
2988 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2990 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2991 MonoAotModule *aot_module = (MonoAotModule*)value;
2993 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2994 data->module = aot_module;
2997 static inline MonoAotModule*
2998 find_aot_module (guint8 *code)
3000 FindAotModuleUserData user_data;
3005 /* Reading these need no locking */
3006 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
3009 user_data.addr = code;
3010 user_data.module = NULL;
3013 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
3016 return user_data.module;
3020 * mono_aot_plt_resolve:
3022 * This function is called by the entries in the PLT to resolve the actual method that
3023 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
3024 * Returns NULL if the something cannot be loaded.
3027 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3029 #ifdef MONO_ARCH_AOT_SUPPORTED
3030 guint8 *p, *target, *plt_entry;
3032 MonoAotModule *module = (MonoAotModule*)aot_module;
3033 gboolean res, no_ftnptr = FALSE;
3036 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
3038 p = &module->blob [plt_info_offset];
3040 ji.type = decode_value (p, &p);
3042 mp = mono_mempool_new_size (512);
3043 res = decode_patch (module, mp, &ji, p, &p);
3046 mono_mempool_destroy (mp);
3051 * Avoid calling resolve_patch_target in the full-aot case if possible, since
3052 * it would create a trampoline, and we don't need that.
3053 * We could do this only if the method does not need the special handling
3054 * in mono_magic_trampoline ().
3056 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) &&
3057 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
3058 target = mono_jit_compile_method (ji.data.method);
3061 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
3065 * The trampoline expects us to return a function descriptor on platforms which use
3066 * it, but resolve_patch_target returns a direct function pointer for some type of
3067 * patches, so have to translate between the two.
3068 * FIXME: Clean this up, but how ?
3070 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) {
3071 /* These should already have a function descriptor */
3072 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3073 /* Our function descriptors have a 0 environment, gcc created ones don't */
3074 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
3075 g_assert (((gpointer*)target) [2] == 0);
3078 } else if (!no_ftnptr) {
3079 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3080 g_assert (((gpointer*)target) [2] != 0);
3082 target = mono_create_ftnptr (mono_domain_get (), target);
3085 mono_mempool_destroy (mp);
3087 /* Patch the PLT entry with target which might be the actual method not a trampoline */
3088 plt_entry = mono_aot_get_plt_entry (code);
3089 g_assert (plt_entry);
3090 mono_arch_patch_plt_entry (plt_entry, module->got, NULL, target);
3094 g_assert_not_reached ();
3102 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
3103 * method in the module is loaded to avoid committing memory by writing to it.
3104 * LOCKING: Assumes the AOT lock is held.
3107 init_plt (MonoAotModule *amodule)
3109 #ifndef MONO_CROSS_COMPILE
3111 #ifdef MONO_ARCH_AOT_SUPPORTED
3113 guint8 *buf = amodule->plt;
3114 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
3120 if (amodule->plt_inited)
3123 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
3126 /* Initialize the first PLT entry */
3127 make_writable (amodule->plt, amodule->plt_end - amodule->plt);
3128 x86_jump_code (buf, tramp);
3129 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
3131 * Initialize the PLT entries in the GOT to point to the default targets.
3134 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
3135 plt_0 = mono_create_ftnptr (mono_domain_get (), amodule->plt);
3136 /* The first entry points to the AOT trampoline */
3137 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base] = tramp;
3138 for (i = 1; i < amodule->info.plt_size; ++i)
3139 /* All the default entries point to the first entry */
3140 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = plt_0;
3142 g_assert_not_reached ();
3145 amodule->plt_inited = TRUE;
3148 #endif /* MONO_CROSS_COMPILE */
3152 * mono_aot_get_plt_entry:
3154 * Return the address of the PLT entry called by the code at CODE if exists.
3157 mono_aot_get_plt_entry (guint8 *code)
3159 MonoAotModule *aot_module = find_aot_module (code);
3160 #if defined(__arm__) || defined(__mono_ppc__)
3167 #if defined(__i386__) || defined(__x86_64__)
3168 if (code [-5] == 0xe8) {
3169 guint32 disp = *(guint32*)(code - 4);
3170 guint8 *target = code + disp;
3172 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3175 #elif defined(__arm__)
3176 ins = ((guint32*)(gpointer)code) [-1];
3178 /* Should be a 'bl' */
3179 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
3180 gint32 disp = ((gint32)ins) & 0xffffff;
3181 guint8 *target = code - 4 + 8 + (disp * 4);
3183 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3186 #elif defined(__mono_ppc__)
3187 /* Should be a bl */
3188 ins = ((guint32*)(gpointer)code) [-1];
3190 if ((ins >> 26 == 18) && ((ins & 1) == 1) && ((ins & 2) == 0)) {
3191 gint32 disp = (((gint32)ins) >> 2) & 0xffffff;
3192 guint8 *target = code - 4 + (disp * 4);
3194 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3198 g_assert_not_reached ();
3205 * mono_aot_get_plt_info_offset:
3207 * Return the PLT info offset belonging to the plt entry called by CODE.
3210 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3212 guint8 *plt_entry = mono_aot_get_plt_entry (code);
3214 g_assert (plt_entry);
3216 /* The offset is embedded inside the code after the plt entry */
3217 #if defined(__i386__)
3218 return *(guint32*)(plt_entry + 5);
3219 #elif defined(__x86_64__)
3220 return *(guint32*)(plt_entry + 6);
3221 #elif defined(__arm__)
3222 /* The offset is stored as the 4th word of the plt entry */
3223 return ((guint32*)plt_entry) [3];
3224 #elif defined(__mono_ppc__)
3225 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3226 return ((guint32*)plt_entry) [8];
3228 return ((guint32*)plt_entry) [6];
3231 g_assert_not_reached ();
3237 mono_create_ftnptr_malloc (guint8 *code)
3239 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3240 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
3242 ftnptr->code = code;
3255 * Load the function named NAME from the aot image.
3258 load_function (MonoAotModule *amodule, const char *name)
3262 int n_patches, pindex;
3268 symbol = g_strdup_printf ("%s", name);
3269 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
3272 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
3274 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
3278 symbol = g_strdup_printf ("%s_p", name);
3279 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
3282 /* Nothing to patch */
3285 p = amodule->blob + *(guint32*)p;
3287 /* Similar to mono_aot_load_method () */
3289 n_patches = decode_value (p, &p);
3292 MonoJumpInfo *patches;
3295 mp = mono_mempool_new ();
3297 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
3300 for (pindex = 0; pindex < n_patches; ++pindex) {
3301 MonoJumpInfo *ji = &patches [pindex];
3304 if (amodule->got [got_slots [pindex]])
3308 * When this code is executed, the runtime may not be initalized yet, so
3309 * resolve the patch info by hand.
3311 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3312 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
3313 target = mono_get_lmf_addr;
3314 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
3315 target = mono_thread_force_interruption_checkpoint;
3316 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
3317 target = mono_exception_from_token;
3318 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
3319 target = mono_get_throw_exception ();
3321 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
3322 target = mono_amd64_throw_exception;
3325 } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
3326 target = mono_amd64_get_original_ip;
3329 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
3330 target = mono_arm_throw_exception;
3331 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
3332 target = mono_arm_throw_exception_by_token;
3335 } else if (!strcmp (ji->data.name, "mono_ppc_throw_exception")) {
3336 target = mono_ppc_throw_exception;
3338 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
3339 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
3340 target = (gpointer)mono_get_trampoline_func (tramp_type2);
3341 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
3342 /* atoll is needed because the the offset is unsigned */
3346 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
3347 g_assert (res == 1);
3348 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
3349 target = mono_create_ftnptr_malloc (target);
3350 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
3351 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
3352 target = mono_create_ftnptr_malloc (target);
3353 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
3354 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
3355 target = mono_create_ftnptr_malloc (target);
3356 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
3357 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
3358 target = mono_create_ftnptr_malloc (target);
3359 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
3360 target = mono_thread_get_and_clear_pending_exception;
3362 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
3363 g_assert_not_reached ();
3367 /* Hopefully the code doesn't have patches which need method or
3370 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
3374 amodule->got [got_slots [pindex]] = target;
3379 mono_mempool_destroy (mp);
3386 * Return the piece of code identified by NAME from the mscorlib AOT file.
3387 * On ppc64, this returns a function descriptor.
3390 mono_aot_get_named_code (const char *name)
3393 MonoAotModule *amodule;
3395 image = mono_defaults.corlib;
3398 amodule = image->aot_module;
3401 return mono_create_ftnptr_malloc (load_function (amodule, name));
3404 /* Return a given kind of trampoline */
3406 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
3408 MonoAotModule *amodule;
3409 int index, tramp_size;
3412 /* Currently, we keep all trampolines in the mscorlib AOT image */
3413 image = mono_defaults.corlib;
3418 amodule = image->aot_module;
3421 *out_amodule = amodule;
3423 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
3424 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
3426 index = amodule->trampoline_index [tramp_type] ++;
3430 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
3432 tramp_size = amodule->info.trampoline_size [tramp_type];
3435 *out_tramp_size = tramp_size;
3437 return amodule->trampolines [tramp_type] + (index * tramp_size);
3441 * Return a specific trampoline from the AOT file.
3444 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3446 MonoAotModule *amodule;
3447 guint32 got_offset, tramp_size;
3448 guint8 *code, *tramp;
3449 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
3450 static gboolean inited;
3451 static guint32 num_trampolines;
3457 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
3466 if (!generic_trampolines [tramp_type]) {
3469 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
3470 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
3474 tramp = generic_trampolines [tramp_type];
3477 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
3479 amodule->got [got_offset] = tramp;
3480 amodule->got [got_offset + 1] = arg1;
3483 *code_len = tramp_size;
3489 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3491 MonoAotModule *amodule;
3495 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
3497 amodule->got [got_offset] = ctx;
3498 amodule->got [got_offset + 1] = addr;
3500 /* The caller expects an ftnptr */
3501 return mono_create_ftnptr (mono_domain_get (), code);
3505 mono_aot_get_unbox_trampoline (MonoMethod *method)
3507 guint32 method_index = mono_metadata_token_index (method->token) - 1;
3508 MonoAotModule *amodule;
3512 if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
3513 guint32 index = find_extra_method (method, &amodule);
3514 g_assert (index != 0xffffff);
3516 symbol = g_strdup_printf ("ut_e_%d", index);
3518 amodule = method->klass->image->aot_module;
3521 symbol = g_strdup_printf ("ut_%d", method_index);
3523 code = load_function (amodule, symbol);
3526 /* The caller expects an ftnptr */
3527 return mono_create_ftnptr (mono_domain_get (), code);
3531 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3536 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
3537 code = load_function (mono_defaults.corlib->aot_module, symbol);
3539 /* The caller expects an ftnptr */
3540 return mono_create_ftnptr (mono_domain_get (), code);
3544 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3550 MonoAotModule *amodule;
3552 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
3554 /* Save the entries into an array */
3555 buf = mono_domain_alloc (domain, (count + 1) * 2 * sizeof (gpointer));
3556 for (i = 0; i < count; ++i) {
3557 MonoIMTCheckItem *item = imt_entries [i];
3559 g_assert (item->key);
3561 g_assert (!item->has_target_code);
3563 buf [(i * 2)] = item->key;
3564 buf [(i * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
3566 buf [(count * 2)] = NULL;
3567 buf [(count * 2) + 1] = fail_tramp;
3569 amodule->got [got_offset] = buf;
3578 mono_aot_init (void)
3583 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3589 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3595 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3601 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
3607 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
3613 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3619 mono_aot_get_plt_entry (guint8 *code)
3625 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3631 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
3637 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3639 g_assert_not_reached ();
3645 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3647 g_assert_not_reached ();
3652 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3654 g_assert_not_reached ();
3659 mono_aot_get_named_code (const char *name)
3661 g_assert_not_reached ();
3666 mono_aot_get_unbox_trampoline (MonoMethod *method)
3668 g_assert_not_reached ();
3673 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3675 g_assert_not_reached ();
3680 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3682 g_assert_not_reached ();
3687 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3689 g_assert_not_reached ();