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
33 #include <limits.h> /* for PAGESIZE */
38 #ifdef HAVE_SYS_WAIT_H
39 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
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-compiler.h"
63 #define SHARED_EXT ".dll"
64 #elif (defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)
65 #define SHARED_EXT ".dylib"
67 #define SHARED_EXT ".so"
70 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
71 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
73 typedef struct MonoAotModule {
75 /* Optimization flags used to compile the module */
77 /* Pointer to the Global Offset Table */
79 guint32 got_size, plt_size;
80 GHashTable *name_cache;
81 GHashTable *extra_methods;
82 /* Maps methods to their code */
83 GHashTable *method_to_code;
84 /* Maps pointers into the method info to the methods themselves */
85 GHashTable *method_ref_to_method;
86 MonoAssemblyName *image_names;
88 MonoAssembly *assembly;
89 MonoImage **image_table;
90 guint32 image_table_len;
99 guint32 plt_got_offset_base;
100 guint32 *code_offsets;
102 guint32 *method_info_offsets;
104 guint32 *got_info_offsets;
106 guint32 *ex_info_offsets;
107 guint32 *method_order;
108 guint32 *method_order_end;
110 guint32 *class_info_offsets;
111 guint32 *methods_loaded;
112 guint16 *class_name_table;
113 guint32 *extra_method_table;
114 guint32 *extra_method_info_offsets;
115 guint8 *extra_method_info;
117 guint32 num_trampolines, trampoline_got_offset_base, trampoline_index;
118 guint32 specific_trampoline_size;
123 /* This structure is stored in the AOT file */
124 typedef struct MonoAotFileInfo
126 guint32 plt_got_offset_base;
127 guint32 trampoline_got_offset_base;
128 guint32 num_trampolines;
131 guint32 specific_trampoline_size;
135 static GHashTable *aot_modules;
136 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
137 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
138 static CRITICAL_SECTION aot_mutex;
141 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
142 * AOT modules registered by mono_aot_register_module ().
144 static GHashTable *static_aot_modules;
147 * Disabling this will make a copy of the loaded code and use the copy instead
148 * of the original. This will place the caller and the callee close to each
149 * other in memory, possibly improving cache behavior. Since the original
150 * code is in copy-on-write memory, this will not increase the memory usage
153 static gboolean use_loaded_code = TRUE;
156 * Whenever to AOT compile loaded assemblies on demand and store them in
157 * a cache under $HOME/.mono/aot-cache.
159 static gboolean use_aot_cache = FALSE;
162 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
163 * use_aot_cache is TRUE.
165 static gboolean spawn_compiler = TRUE;
168 static gint32 mono_last_aot_method = -1;
170 static gboolean make_unreadable = FALSE;
171 static guint32 name_table_accesses = 0;
173 /* Used to speed-up find_aot_module () */
174 static gsize aot_code_low_addr = (gssize)-1;
175 static gsize aot_code_high_addr = 0;
177 /* Used to communicate with mono_aot_register_globals () */
178 static guint32 globals_tls_id = -1;
181 init_plt (MonoAotModule *info);
184 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
185 guint32 got_index, guint32 **got_slots,
186 guint8 *buf, guint8 **endbuf);
188 /*****************************************************/
190 /*****************************************************/
193 load_image (MonoAotModule *module, int index)
195 MonoAssembly *assembly;
196 MonoImageOpenStatus status;
198 g_assert (index < module->image_table_len);
200 if (module->image_table [index])
201 return module->image_table [index];
202 if (module->out_of_date)
205 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
207 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", module->aot_name, module->image_names [index].name);
208 module->out_of_date = TRUE;
212 if (strcmp (assembly->image->guid, module->image_guids [index])) {
213 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", module->aot_name, module->image_names [index].name);
214 module->out_of_date = TRUE;
218 module->image_table [index] = assembly->image;
219 return assembly->image;
224 decode_value (guint8 *ptr, guint8 **rptr)
229 if ((b & 0x80) == 0){
232 } else if ((b & 0x40) == 0){
233 len = ((b & 0x3f) << 8 | ptr [1]);
235 } else if (b != 0xff) {
236 len = ((b & 0x1f) << 24) |
243 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
249 //printf ("DECODE: %d.\n", len);
254 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
257 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
259 static MonoGenericInst*
260 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
263 MonoType **type_argv;
264 MonoGenericInst *inst;
267 type_argc = decode_value (p, &p);
268 type_argv = g_new0 (MonoType*, type_argc);
270 for (i = 0; i < type_argc; ++i) {
271 MonoClass *pclass = decode_klass_ref (module, p, &p);
276 type_argv [i] = &pclass->byval_arg;
279 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
288 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
290 gboolean has_class_inst, has_method_inst;
293 has_class_inst = decode_value (p, &p);
294 if (has_class_inst) {
295 ctx->class_inst = decode_generic_inst (module, p, &p);
296 if (!ctx->class_inst)
299 has_method_inst = decode_value (p, &p);
300 if (has_method_inst) {
301 ctx->method_inst = decode_generic_inst (module, p, &p);
302 if (!ctx->method_inst)
311 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
314 MonoClass *klass, *eklass;
318 token = decode_value (p, &p);
323 if (mono_metadata_token_table (token) == 0) {
324 image = load_image (module, decode_value (p, &p));
327 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
328 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
329 if (token == MONO_TOKEN_TYPE_SPEC) {
330 MonoTypeEnum type = decode_value (p, &p);
332 if (type == MONO_TYPE_GENERICINST) {
334 MonoGenericContext ctx;
337 gclass = decode_klass_ref (module, p, &p);
338 g_assert (gclass->generic_container);
340 memset (&ctx, 0, sizeof (ctx));
341 ctx.class_inst = decode_generic_inst (module, p, &p);
344 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
345 klass = mono_class_from_mono_type (type);
346 mono_metadata_free_type (type);
347 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
349 MonoGenericContainer *container;
351 int num = decode_value (p, &p);
352 gboolean is_method = decode_value (p, &p);
355 MonoMethod *method_def;
356 g_assert (type == MONO_TYPE_MVAR);
357 method_def = decode_method_ref_2 (module, p, &p);
361 container = mono_method_get_generic_container (method_def);
363 MonoClass *class_def;
364 g_assert (type == MONO_TYPE_VAR);
365 class_def = decode_klass_ref (module, p, &p);
369 container = class_def->generic_container;
372 g_assert (container);
374 // FIXME: Memory management
375 t = g_new0 (MonoType, 1);
377 t->data.generic_param = mono_generic_container_get_param (container, num);
379 // FIXME: Maybe use types directly to avoid
380 // the overhead of creating MonoClass-es
381 klass = mono_class_from_mono_type (t);
385 g_assert_not_reached ();
388 image = load_image (module, decode_value (p, &p));
391 klass = mono_class_get (image, token);
393 } else if (token == MONO_TOKEN_TYPE_DEF) {
395 image = load_image (module, decode_value (p, &p));
398 rank = decode_value (p, &p);
399 eklass = decode_klass_ref (module, p, &p);
400 klass = mono_array_class_get (eklass, rank);
402 g_assert_not_reached ();
405 mono_class_init (klass);
411 static MonoClassField*
412 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
414 MonoClass *klass = decode_klass_ref (module, buf, &buf);
421 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
425 return mono_class_get_field (klass, token);
429 * can_method_ref_match_method:
431 * Determine if calling decode_method_ref_2 on P could return the same method as
432 * METHOD. This is an optimization to avoid calling decode_method_ref_2 () which
433 * would create MonoMethods which are not needed etc.
436 can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
439 guint32 image_index, value;
441 /* Keep this in sync with decode_method_ref () */
442 value = decode_value (p, &p);
443 image_index = value >> 24;
445 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
446 guint32 wrapper_type;
448 if (!method->wrapper_type)
451 wrapper_type = decode_value (p, &p);
453 if (method->wrapper_type != wrapper_type)
455 } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
456 if (method->wrapper_type)
466 * Decode a method reference, and return its image and token. This avoids loading
467 * metadata for the method if the caller does not need it. If the method has no token,
468 * then it is loaded from metadata and METHOD is set to the method instance.
471 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
473 guint32 image_index, value;
474 MonoImage *image = NULL;
479 if (no_aot_trampoline)
480 *no_aot_trampoline = FALSE;
482 value = decode_value (p, &p);
483 image_index = value >> 24;
485 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
486 if (no_aot_trampoline)
487 *no_aot_trampoline = TRUE;
488 value = decode_value (p, &p);
489 image_index = value >> 24;
492 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
493 guint32 wrapper_type;
495 wrapper_type = decode_value (p, &p);
498 image = mono_defaults.corlib;
500 switch (wrapper_type) {
501 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
502 MonoMethod *m = decode_method_ref_2 (module, p, &p);
506 mono_class_init (m->klass);
507 *method = mono_marshal_get_remoting_invoke_with_check (m);
510 case MONO_WRAPPER_PROXY_ISINST: {
511 MonoClass *klass = decode_klass_ref (module, p, &p);
514 *method = mono_marshal_get_proxy_cancast (klass);
517 case MONO_WRAPPER_LDFLD:
518 case MONO_WRAPPER_LDFLDA:
519 case MONO_WRAPPER_STFLD:
520 case MONO_WRAPPER_ISINST: {
521 MonoClass *klass = decode_klass_ref (module, p, &p);
524 if (wrapper_type == MONO_WRAPPER_LDFLD)
525 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
526 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
527 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
528 else if (wrapper_type == MONO_WRAPPER_STFLD)
529 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
530 else if (wrapper_type == MONO_WRAPPER_ISINST)
531 *method = mono_marshal_get_isinst (klass);
533 g_assert_not_reached ();
536 case MONO_WRAPPER_LDFLD_REMOTE:
537 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
539 case MONO_WRAPPER_STFLD_REMOTE:
540 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
542 case MONO_WRAPPER_ALLOC: {
543 int atype = decode_value (p, &p);
545 *method = mono_gc_get_managed_allocator_by_type (atype);
548 case MONO_WRAPPER_STELEMREF:
549 *method = mono_marshal_get_stelemref ();
551 case MONO_WRAPPER_STATIC_RGCTX_INVOKE: {
552 MonoMethod *m = decode_method_ref_2 (module, p, &p);
556 *method = mono_marshal_get_static_rgctx_invoke (m);
559 case MONO_WRAPPER_UNKNOWN: {
560 MonoMethodDesc *desc;
561 MonoMethod *orig_method;
562 int subtype = decode_value (p, &p);
564 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
565 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
566 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
567 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
569 g_assert_not_reached ();
570 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
571 g_assert (orig_method);
572 mono_method_desc_free (desc);
573 *method = mono_monitor_get_fast_path (orig_method);
577 g_assert_not_reached ();
579 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
580 /* Can't decode these */
581 g_assert_not_reached ();
582 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
583 image_index = decode_value (p, &p);
584 *token = decode_value (p, &p);
586 image = load_image (module, image_index);
589 } else if (image_index == MONO_AOT_METHODREF_GINST) {
591 MonoGenericContext ctx;
594 * These methods do not have a token which resolves them, so we
595 * resolve them immediately.
597 klass = decode_klass_ref (module, p, &p);
601 image_index = decode_value (p, &p);
602 *token = decode_value (p, &p);
604 image = load_image (module, image_index);
608 *method = mono_get_method_full (image, *token, NULL, NULL);
612 memset (&ctx, 0, sizeof (ctx));
614 if (FALSE && klass->generic_class) {
615 ctx.class_inst = klass->generic_class->context.class_inst;
616 ctx.method_inst = NULL;
618 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
621 memset (&ctx, 0, sizeof (ctx));
623 if (!decode_generic_context (module, &ctx, p, &p))
626 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
627 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
631 klass = decode_klass_ref (module, p, &p);
634 method_type = decode_value (p, &p);
636 switch (method_type) {
638 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
641 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
644 *method = mono_class_get_method_from_name (klass, "Get", -1);
647 *method = mono_class_get_method_from_name (klass, "Address", -1);
650 *method = mono_class_get_method_from_name (klass, "Set", -1);
653 g_assert_not_reached ();
656 g_assert (image_index < MONO_AOT_METHODREF_MIN);
657 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
659 image = load_image (module, image_index);
670 * decode_method_ref_2:
672 * Similar to decode_method_ref, but resolve and return the method itself.
675 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
679 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
685 return mono_get_method (image, token, NULL);
690 make_writable (guint8* addr, guint32 len)
692 #ifndef PLATFORM_WIN32
697 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
699 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1));
700 pages = (addr + len - page_start + PAGESIZE - 1) / PAGESIZE;
701 err = mprotect (page_start, pages * PAGESIZE, PROT_READ | PROT_WRITE | PROT_EXEC);
706 g_assert (VirtualProtect (addr, len, PAGE_EXECUTE_READWRITE, &oldp) != 0);
712 create_cache_structure (void)
718 home = g_get_home_dir ();
722 tmp = g_build_filename (home, ".mono", NULL);
723 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
724 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
725 #ifdef PLATFORM_WIN32
728 err = mkdir (tmp, 0777);
731 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
737 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
738 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
739 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
740 #ifdef PLATFORM_WIN32
743 err = mkdir (tmp, 0777);
746 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
755 * load_aot_module_from_cache:
757 * Experimental code to AOT compile loaded assemblies on demand.
760 * - Add environment variable MONO_AOT_CACHE_OPTIONS
761 * - Add options for controlling the cache size
762 * - Handle full cache by deleting old assemblies lru style
763 * - Add options for excluding assemblies during development
764 * - Maybe add a threshold after an assembly is AOT compiled
765 * - invoking a new mono process is a security risk
766 * - recompile the AOT module if one of its dependencies changes
769 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
771 char *fname, *cmd, *tmp2, *aot_options;
780 if (assembly->image->dynamic)
783 create_cache_structure ();
785 home = g_get_home_dir ();
787 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
788 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
792 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
793 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
796 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
798 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
800 aot_options = g_strdup_printf ("outfile=%s", fname);
802 if (spawn_compiler) {
803 /* FIXME: security */
804 /* FIXME: Has to pass the assembly loading path to the child process */
805 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
807 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
809 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
811 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
812 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
814 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
821 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
823 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
825 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
829 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
831 g_free (aot_options);
838 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
844 for (i = 0; globals [i]; i+= 2) {
845 if (strcmp (globals [i], name) == 0) {
846 *value = globals [i + 1];
851 mono_dl_symbol (module, name, value);
856 load_aot_module (MonoAssembly *assembly, gpointer user_data)
859 MonoAotModule *amodule;
861 gboolean usable = TRUE;
862 char *saved_guid = NULL;
863 char *aot_version = NULL;
864 char *runtime_version, *build_info;
865 char *opt_flags = NULL;
867 gboolean full_aot = FALSE;
868 MonoAotFileInfo *file_info = NULL;
871 if (mono_compile_aot)
874 if (assembly->image->aot_module)
876 * Already loaded. This can happen because the assembly loading code might invoke
877 * the assembly load hooks multiple times for the same assembly.
881 if (assembly->image->dynamic)
884 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
888 if (static_aot_modules)
889 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
895 /* Statically linked AOT module */
897 aot_name = g_strdup_printf ("%s", assembly->aname.name);
898 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
900 TlsSetValue (globals_tls_id, NULL);
903 sofile = load_aot_module_from_cache (assembly, &aot_name);
906 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
908 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
911 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
917 * If the image was compiled in no-dlsym mode, it contains no global symbols,
918 * instead it contains an ELF ctor function which is called by dlopen () which
919 * in turn calls mono_aot_register_globals () to register a table which contains
920 * the name and address of the globals.
922 globals = TlsGetValue (globals_tls_id);
923 TlsSetValue (globals_tls_id, NULL);
926 if (!sofile && !globals) {
928 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
935 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
936 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
937 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
938 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
940 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
941 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);
945 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
946 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
951 build_info = mono_get_runtime_build_info ();
952 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
953 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);
961 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
963 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
967 if (mono_aot_only && !full_aot) {
968 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
971 if (!mono_aot_only && full_aot) {
972 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
978 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
983 mono_dl_close (sofile);
984 assembly->image->aot_module = NULL;
988 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
989 g_assert (file_info);
991 amodule = g_new0 (MonoAotModule, 1);
992 amodule->aot_name = aot_name;
993 amodule->assembly = assembly;
994 amodule->plt_got_offset_base = file_info->plt_got_offset_base;
995 amodule->num_trampolines = file_info->num_trampolines;
996 amodule->trampoline_got_offset_base = file_info->trampoline_got_offset_base;
997 amodule->got_size = file_info->got_size;
998 amodule->plt_size = file_info->plt_size;
999 amodule->specific_trampoline_size = file_info->specific_trampoline_size;
1000 amodule->got = file_info->got;
1001 amodule->got [0] = assembly->image;
1002 amodule->globals = globals;
1003 amodule->sofile = sofile;
1004 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1006 sscanf (opt_flags, "%d", &amodule->opts);
1008 /* Read image table */
1010 guint32 table_len, i;
1013 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
1016 table_len = *(guint32*)table;
1017 table += sizeof (guint32);
1018 amodule->image_table = g_new0 (MonoImage*, table_len);
1019 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1020 amodule->image_guids = g_new0 (char*, table_len);
1021 amodule->image_table_len = table_len;
1022 for (i = 0; i < table_len; ++i) {
1023 MonoAssemblyName *aname = &(amodule->image_names [i]);
1025 aname->name = g_strdup (table);
1026 table += strlen (table) + 1;
1027 amodule->image_guids [i] = g_strdup (table);
1028 table += strlen (table) + 1;
1030 aname->culture = g_strdup (table);
1031 table += strlen (table) + 1;
1032 memcpy (aname->public_key_token, table, strlen (table) + 1);
1033 table += strlen (table) + 1;
1035 table = ALIGN_PTR_TO (table, 8);
1036 aname->flags = *(guint32*)table;
1038 aname->major = *(guint32*)table;
1040 aname->minor = *(guint32*)table;
1042 aname->build = *(guint32*)table;
1044 aname->revision = *(guint32*)table;
1049 /* Read method and method_info tables */
1050 find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
1051 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
1052 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
1053 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
1054 find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
1055 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
1056 find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
1057 find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
1058 find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
1059 find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
1060 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
1061 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
1062 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
1063 find_symbol (sofile, globals, "extra_method_info", (gpointer *)&amodule->extra_method_info);
1064 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1065 find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
1066 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1067 find_symbol (sofile, globals, "trampolines", (gpointer*)&amodule->trampolines);
1068 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1070 amodule->mem_begin = amodule->code;
1072 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1073 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1075 if (make_unreadable) {
1076 #ifndef PLATFORM_WIN32
1079 int pages, err, len;
1081 addr = amodule->mem_begin;
1082 len = amodule->mem_end - amodule->mem_begin;
1084 /* Round down in both directions to avoid modifying data which is not ours */
1085 page_start = (guint8 *) (((gssize) (addr)) & ~ (PAGESIZE - 1)) + PAGESIZE;
1086 pages = ((addr + len - page_start + PAGESIZE - 1) / PAGESIZE) - 1;
1087 err = mprotect (page_start, pages * PAGESIZE, 0);
1088 g_assert (err == 0);
1094 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1095 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1097 g_hash_table_insert (aot_modules, assembly, amodule);
1100 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1102 assembly->image->aot_module = amodule;
1105 * Since we store methoddef and classdef tokens when referring to methods/classes in
1106 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1107 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1108 * non-lazily, since we can't handle out-of-date errors later.
1110 for (i = 0; i < amodule->image_table_len; ++i)
1111 load_image (amodule, i);
1113 if (amodule->out_of_date) {
1114 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);
1115 if (mono_aot_only) {
1116 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);
1121 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1125 * mono_aot_register_globals:
1127 * This is called by the ctor function in AOT images compiled with the
1128 * 'no-dlsym' option.
1131 mono_aot_register_globals (gpointer *globals)
1133 TlsSetValue (globals_tls_id, globals);
1137 * mono_aot_register_module:
1139 * This should be called by embedding code to register AOT modules statically linked
1140 * into the executable. AOT_INFO should be the value of the
1141 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1144 mono_aot_register_module (gpointer *aot_info)
1152 /* Determine the assembly name */
1153 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1156 /* This could be called before startup */
1160 if (!static_aot_modules)
1161 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1163 g_hash_table_insert (static_aot_modules, aname, globals);
1170 mono_aot_init (void)
1172 InitializeCriticalSection (&aot_mutex);
1173 aot_modules = g_hash_table_new (NULL, NULL);
1174 globals_tls_id = TlsAlloc ();
1176 mono_install_assembly_load_hook (load_aot_module, NULL);
1178 if (getenv ("MONO_LASTAOT"))
1179 mono_last_aot_method = atoi (getenv ("MONO_LASTAOT"));
1180 if (getenv ("MONO_AOT_CACHE"))
1181 use_aot_cache = TRUE;
1185 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1189 info->vtable_size = decode_value (buf, &buf);
1190 if (info->vtable_size == -1)
1193 flags = decode_value (buf, &buf);
1194 info->ghcimpl = (flags >> 0) & 0x1;
1195 info->has_finalize = (flags >> 1) & 0x1;
1196 info->has_cctor = (flags >> 2) & 0x1;
1197 info->has_nested_classes = (flags >> 3) & 0x1;
1198 info->blittable = (flags >> 4) & 0x1;
1199 info->has_references = (flags >> 5) & 0x1;
1200 info->has_static_refs = (flags >> 6) & 0x1;
1201 info->no_special_static_fields = (flags >> 7) & 0x1;
1203 if (info->has_cctor) {
1204 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1208 if (info->has_finalize) {
1209 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1210 if (!info->finalize_image)
1214 info->instance_size = decode_value (buf, &buf);
1215 info->class_size = decode_value (buf, &buf);
1216 info->packing_size = decode_value (buf, &buf);
1217 info->min_align = decode_value (buf, &buf);
1225 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1228 MonoClass *klass = vtable->klass;
1229 MonoAotModule *aot_module = klass->image->aot_module;
1231 MonoCachedClassInfo class_info;
1235 gboolean no_aot_trampoline;
1237 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1240 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1243 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1247 for (i = 0; i < slot; ++i)
1248 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1250 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1253 if (no_aot_trampoline)
1256 if (mono_metadata_token_index (token) == 0)
1259 return mono_aot_get_method_from_token (domain, image, token);
1263 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1265 MonoAotModule *aot_module = klass->image->aot_module;
1269 if (klass->rank || !aot_module)
1272 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1274 err = decode_cached_class_info (aot_module, res, p, &p);
1282 * mono_aot_get_class_from_name:
1284 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1285 * using a cache stored in the AOT file.
1286 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1288 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1292 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1294 MonoAotModule *aot_module = image->aot_module;
1295 guint16 *table, *entry;
1298 char full_name_buf [1024];
1300 const char *name2, *name_space2;
1302 guint32 cols [MONO_TYPEDEF_SIZE];
1303 GHashTable *nspace_table;
1305 if (!aot_module || !aot_module->class_name_table)
1312 /* First look in the cache */
1313 if (!aot_module->name_cache)
1314 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1315 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1317 *klass = g_hash_table_lookup (nspace_table, name);
1324 table_size = aot_module->class_name_table [0];
1325 table = aot_module->class_name_table + 1;
1327 if (name_space [0] == '\0')
1328 full_name = g_strdup_printf ("%s", name);
1330 if (strlen (name_space) + strlen (name) < 1000) {
1331 sprintf (full_name_buf, "%s.%s", name_space, name);
1332 full_name = full_name_buf;
1334 full_name = g_strdup_printf ("%s.%s", name_space, name);
1337 hash = g_str_hash (full_name) % table_size;
1338 if (full_name != full_name_buf)
1341 entry = &table [hash * 2];
1343 if (entry [0] != 0) {
1344 t = &image->tables [MONO_TABLE_TYPEDEF];
1347 guint32 index = entry [0];
1348 guint32 next = entry [1];
1349 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1351 name_table_accesses ++;
1353 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1355 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1356 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1358 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1360 *klass = mono_class_get (image, token);
1365 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1366 if (!nspace_table) {
1367 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1368 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1370 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1377 entry = &table [next * 2];
1390 * LOCKING: Acquires the domain lock.
1393 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
1394 MonoMethod *method, guint8* ex_info, guint8 *code)
1398 guint code_len, used_int_regs, flags;
1399 gboolean has_generic_jit_info, has_dwarf_unwind_info;
1400 guint8 *p, *unwind_info_block;
1401 MonoMethodHeader *header;
1402 int generic_info_size, unwind_info_len;
1404 header = mono_method_get_header (method);
1406 /* Load the method info from the AOT file */
1409 code_len = decode_value (p, &p);
1410 flags = decode_value (p, &p);
1411 has_generic_jit_info = (flags & 1) != 0;
1412 has_dwarf_unwind_info = (flags & 2) != 0;
1413 unwind_info_block = p;
1414 if (has_dwarf_unwind_info) {
1417 unwind_info_len = decode_value (p, &p);
1418 offset = unwind_info_block - (guint8*)aot_module->ex_info;
1419 g_assert (offset > 0 && offset < (1 << 30));
1420 used_int_regs = offset;
1421 p += unwind_info_len;
1423 used_int_regs = decode_value (p, &p);
1425 if (has_generic_jit_info)
1426 generic_info_size = sizeof (MonoGenericJitInfo);
1428 generic_info_size = 0;
1430 /* Exception table */
1431 if (header && header->num_clauses) {
1433 mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
1434 jinfo->num_clauses = header->num_clauses;
1436 for (i = 0; i < header->num_clauses; ++i) {
1437 MonoExceptionClause *ec = &header->clauses [i];
1438 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1440 ei->flags = ec->flags;
1441 ei->exvar_offset = decode_value (p, &p);
1443 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1444 ei->data.filter = code + decode_value (p, &p);
1446 ei->data.catch_class = ec->data.catch_class;
1448 ei->try_start = code + decode_value (p, &p);
1449 ei->try_end = code + decode_value (p, &p);
1450 ei->handler_start = code + decode_value (p, &p);
1454 jinfo = mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + generic_info_size);
1457 jinfo->code_size = code_len;
1458 jinfo->used_regs = used_int_regs;
1459 jinfo->method = method;
1460 jinfo->code_start = code;
1461 jinfo->domain_neutral = 0;
1462 jinfo->from_aot = 1;
1464 if (has_generic_jit_info) {
1465 MonoGenericJitInfo *gi;
1467 jinfo->has_generic_jit_info = 1;
1469 gi = mono_jit_info_get_generic_jit_info (jinfo);
1472 gi->has_this = decode_value (p, &p);
1473 gi->this_reg = decode_value (p, &p);
1474 gi->this_offset = decode_value (p, &p);
1476 /* This currently contains no data */
1477 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1479 jinfo->method = decode_method_ref_2 (aot_module, p, &p);
1482 /* Load debug info */
1483 buf_len = decode_value (p, &p);
1484 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1490 * mono_aot_get_unwind_info:
1492 * Return a pointer to the DWARF unwind info belonging to JI.
1495 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
1497 MonoAotModule *amodule = ji->method->klass->image->aot_module;
1501 g_assert (ji->from_aot);
1503 p = amodule->ex_info + ji->used_regs;
1504 *unwind_info_len = decode_value (p, &p);
1509 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1512 int pos, left, right, offset, offset1, offset2, last_offset, new_offset;
1513 int page_index, method_index, table_len, is_wrapper;
1515 MonoAotModule *amodule = image->aot_module;
1518 guint8 *code, *ex_info, *p;
1519 guint32 *table, *ptr;
1525 if (domain != mono_get_root_domain ())
1529 offset = (guint8*)addr - amodule->code;
1531 /* First search through the index */
1532 ptr = amodule->method_order;
1537 if (*ptr == 0xffffff)
1541 while (*ptr != 0xffffff) {
1542 guint32 method_index = ptr [0];
1543 new_offset = amodule->code_offsets [method_index];
1545 if (offset >= last_offset && offset < new_offset) {
1551 last_offset = new_offset;
1555 /* Skip rest of index */
1556 while (*ptr != 0xffffff)
1561 table_len = amodule->method_order_end - table;
1563 g_assert (table <= amodule->method_order_end);
1566 left = (page_index * 1024);
1567 right = left + 1024;
1569 if (right > table_len)
1572 offset1 = amodule->code_offsets [table [left]];
1573 g_assert (offset1 <= offset);
1575 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1578 //printf ("Not found in index: 0x%x\n", offset);
1583 /* Binary search inside the method_order table to find the method */
1585 pos = (left + right) / 2;
1587 g_assert (table + pos <= amodule->method_order_end);
1589 //printf ("Pos: %5d < %5d < %5d Offset: 0x%05x < 0x%05x < 0x%05x\n", left, pos, right, amodule->code_offsets [table [left]], offset, amodule->code_offsets [table [right]]);
1591 offset1 = amodule->code_offsets [table [pos]];
1592 if (table + pos + 1 >= amodule->method_order_end)
1593 offset2 = amodule->code_end - amodule->code;
1595 offset2 = amodule->code_offsets [table [pos + 1]];
1597 if (offset < offset1)
1599 else if (offset >= offset2)
1605 method_index = table [pos];
1607 /* Might be a wrapper/extra method */
1608 if (amodule->extra_methods) {
1610 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
1617 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
1619 * This is hit for extra methods which are called directly, so they are
1620 * not in amodule->extra_methods.
1622 table_len = amodule->extra_method_info_offsets [0];
1623 table = amodule->extra_method_info_offsets + 1;
1630 pos = ((left + right) / 2);
1632 g_assert (pos < table_len);
1634 if (table [pos * 2] < method_index)
1636 else if (table [pos * 2] > method_index)
1642 p = amodule->extra_method_info + table [(pos * 2) + 1];
1643 is_wrapper = decode_value (p, &p);
1644 g_assert (!is_wrapper);
1645 method = decode_method_ref_2 (amodule, p, &p);
1648 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1649 method = mono_get_method (image, token, NULL);
1656 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1658 code = &amodule->code [amodule->code_offsets [method_index]];
1659 ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
1661 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code);
1663 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1664 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1666 /* Add it to the normal JitInfo tables */
1667 mono_jit_info_table_add (domain, jinfo);
1672 /* Keep it in sync with the version in aot-compiler.c */
1673 static inline gboolean
1674 is_shared_got_patch (MonoJumpInfo *patch_info)
1676 switch (patch_info->type) {
1677 case MONO_PATCH_INFO_VTABLE:
1678 case MONO_PATCH_INFO_CLASS:
1679 case MONO_PATCH_INFO_IID:
1680 case MONO_PATCH_INFO_ADJUSTED_IID:
1681 case MONO_PATCH_INFO_FIELD:
1682 case MONO_PATCH_INFO_SFLDA:
1683 case MONO_PATCH_INFO_DECLSEC:
1684 case MONO_PATCH_INFO_LDTOKEN:
1685 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1686 case MONO_PATCH_INFO_RVA:
1687 case MONO_PATCH_INFO_METHODCONST:
1688 case MONO_PATCH_INFO_IMAGE:
1696 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1704 case MONO_PATCH_INFO_METHOD:
1705 case MONO_PATCH_INFO_METHOD_JUMP:
1706 case MONO_PATCH_INFO_ICALL_ADDR:
1707 case MONO_PATCH_INFO_METHOD_RGCTX: {
1710 gboolean no_aot_trampoline;
1712 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
1716 #ifdef MONO_ARCH_HAVE_CREATE_TRAMPOLINE_FROM_TOKEN
1717 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1718 ji->data.target = mono_create_jit_trampoline_from_token (image, token);
1719 ji->type = MONO_PATCH_INFO_ABS;
1723 ji->data.method = method;
1725 ji->data.method = mono_get_method (image, token, NULL);
1726 g_assert (ji->data.method);
1727 mono_class_init (ji->data.method->klass);
1730 ji->data.method = mono_get_method (image, token, NULL);
1731 g_assert (ji->data.method);
1732 mono_class_init (ji->data.method->klass);
1737 case MONO_PATCH_INFO_INTERNAL_METHOD:
1738 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1739 guint32 len = decode_value (p, &p);
1741 ji->data.name = (char*)p;
1745 case MONO_PATCH_INFO_METHODCONST:
1747 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1748 if (!ji->data.method)
1751 case MONO_PATCH_INFO_VTABLE:
1752 case MONO_PATCH_INFO_CLASS:
1753 case MONO_PATCH_INFO_IID:
1754 case MONO_PATCH_INFO_ADJUSTED_IID:
1756 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1757 if (!ji->data.klass)
1760 case MONO_PATCH_INFO_CLASS_INIT:
1761 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1762 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1763 if (!ji->data.klass)
1766 case MONO_PATCH_INFO_IMAGE:
1767 ji->data.image = load_image (aot_module, decode_value (p, &p));
1768 if (!ji->data.image)
1771 case MONO_PATCH_INFO_FIELD:
1772 case MONO_PATCH_INFO_SFLDA:
1774 ji->data.field = decode_field_info (aot_module, p, &p);
1775 if (!ji->data.field)
1778 case MONO_PATCH_INFO_SWITCH:
1779 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1780 ji->data.table->table_size = decode_value (p, &p);
1781 table = g_new (gpointer, ji->data.table->table_size);
1782 ji->data.table->table = (MonoBasicBlock**)table;
1783 for (i = 0; i < ji->data.table->table_size; i++)
1784 table [i] = (gpointer)(gssize)decode_value (p, &p);
1786 case MONO_PATCH_INFO_R4: {
1789 ji->data.target = mono_mempool_alloc0 (mp, sizeof (float));
1790 val = decode_value (p, &p);
1791 *(float*)ji->data.target = *(float*)&val;
1794 case MONO_PATCH_INFO_R8: {
1797 ji->data.target = mono_mempool_alloc0 (mp, sizeof (double));
1799 val [0] = decode_value (p, &p);
1800 val [1] = decode_value (p, &p);
1801 *(double*)ji->data.target = *(double*)val;
1804 case MONO_PATCH_INFO_LDSTR:
1805 image = load_image (aot_module, decode_value (p, &p));
1808 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1810 case MONO_PATCH_INFO_RVA:
1811 case MONO_PATCH_INFO_DECLSEC:
1812 case MONO_PATCH_INFO_LDTOKEN:
1813 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1815 image = load_image (aot_module, decode_value (p, &p));
1818 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1820 ji->data.token->has_context = decode_value (p, &p);
1821 if (ji->data.token->has_context) {
1822 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
1827 case MONO_PATCH_INFO_EXC_NAME:
1828 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1829 if (!ji->data.klass)
1831 ji->data.name = ji->data.klass->name;
1833 case MONO_PATCH_INFO_METHOD_REL:
1834 ji->data.offset = decode_value (p, &p);
1836 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1837 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1838 case MONO_PATCH_INFO_MONITOR_ENTER:
1839 case MONO_PATCH_INFO_MONITOR_EXIT:
1841 case MONO_PATCH_INFO_RGCTX_FETCH: {
1843 MonoJumpInfoRgctxEntry *entry;
1845 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1846 entry->method = decode_method_ref_2 (aot_module, p, &p);
1847 entry->in_mrgctx = decode_value (p, &p);
1848 entry->info_type = decode_value (p, &p);
1849 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1850 entry->data->type = decode_value (p, &p);
1852 res = decode_patch (aot_module, mp, entry->data, p, &p);
1855 ji->data.rgctx_entry = entry;
1859 g_warning ("unhandled type %d", ji->type);
1860 g_assert_not_reached ();
1874 * Decode a reference to a GOT entry. GOT_OFFSET is set to the index of the got
1875 * entry. If that got entry is not already filled out, then JI is filled out with
1876 * the information required to resolve the value of the GOT entry.
1877 * FIXME: Clean up this confusing API.
1880 decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
1886 if (is_shared_got_patch (ji)) {
1887 *got_offset = decode_value (p, &p);
1889 if (aot_module->got [*got_offset]) {
1890 /* Already loaded */
1891 //printf ("HIT!\n");
1893 shared_p = aot_module->got_info + aot_module->got_info_offsets [*got_offset];
1895 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1900 res = decode_patch (aot_module, mp, ji, p, &p);
1909 static MonoJumpInfo*
1910 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1911 guint32 got_index, guint32 **got_slots,
1912 guint8 *buf, guint8 **endbuf)
1914 MonoJumpInfo *patches;
1915 MonoJumpInfo *patch_info = NULL;
1917 guint32 last_offset;
1922 /* First load the type + offset table */
1924 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1926 for (pindex = 0; pindex < n_patches; ++pindex) {
1927 MonoJumpInfo *ji = &patches [pindex];
1932 //printf ("T: %d O: %d.\n", ji->type, ji->ip.i);
1933 ji->next = patch_info;
1937 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1938 memset (*got_slots, 0xff, sizeof (guint32) * n_patches);
1940 /* Then load the other data */
1941 for (pindex = 0; pindex < n_patches; ++pindex) {
1942 MonoJumpInfo *ji = &patches [pindex];
1944 if (!decode_got_entry (aot_module, mp, ji, p, &p, (*got_slots) + pindex))
1947 if ((*got_slots) [pindex] == 0xffffffff)
1948 (*got_slots) [pindex] = got_index ++;
1955 g_free (*got_slots);
1962 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1965 * Jump addresses cannot be patched by the trampoline code since it
1966 * does not have access to the caller's address. Instead, we collect
1967 * the addresses of the GOT slots pointing to a method, and patch
1968 * them after the method has been compiled.
1970 MonoJitDomainInfo *info = domain_jit_info (domain);
1973 mono_domain_lock (domain);
1974 if (!info->jump_target_got_slot_hash)
1975 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1976 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1977 list = g_slist_prepend (list, got_slot);
1978 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
1979 mono_domain_unlock (domain);
1985 * Load the method identified by METHOD_INDEX from the AOT image. Return a
1986 * pointer to the native code of the method, or NULL if not found.
1987 * METHOD might not be set if the caller only has the image/token info.
1990 load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
1993 gboolean from_plt = method == NULL;
1995 int i, pindex, got_index = 0, n_patches, used_strings;
1996 gboolean keep_patches = TRUE;
1997 guint8 *p, *ex_info;
1998 MonoJitInfo *jinfo = NULL;
1999 guint8 *code, *info;
2001 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2004 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
2005 /* Non shared AOT code can't be used in other appdomains */
2008 if (aot_module->out_of_date)
2011 if (aot_module->code_offsets [method_index] == 0xffffffff) {
2012 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2016 method = mono_get_method (image, token, NULL);
2017 full_name = mono_method_full_name (method, TRUE);
2018 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2024 code = &aot_module->code [aot_module->code_offsets [method_index]];
2025 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
2028 if (!aot_module->methods_loaded)
2029 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
2032 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2035 if (mono_last_aot_method != -1) {
2036 if (mono_jit_stats.methods_aot > mono_last_aot_method)
2039 if (method && mono_jit_stats.methods_aot == mono_last_aot_method)
2040 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
2046 klass = method->klass;
2047 decode_klass_ref (aot_module, p, &p);
2049 klass = decode_klass_ref (aot_module, p, &p);
2052 if (!use_loaded_code) {
2056 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [mono_metadata_token_index (token) - 1]];
2057 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2060 code2 = mono_domain_code_reserve (domain, jinfo->code_size);
2061 memcpy (code2, code, jinfo->code_size);
2062 mono_arch_flush_icache (code2, jinfo->code_size);
2066 if (aot_module->opts & MONO_OPT_SHARED)
2067 used_strings = decode_value (p, &p);
2071 for (i = 0; i < used_strings; i++) {
2072 guint token = decode_value (p, &p);
2073 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2076 if (aot_module->opts & MONO_OPT_SHARED)
2077 keep_patches = FALSE;
2079 n_patches = decode_value (p, &p);
2081 keep_patches = FALSE;
2084 MonoJumpInfo *patches;
2090 mp = mono_mempool_new ();
2092 got_index = decode_value (p, &p);
2094 patches = load_patch_info (aot_module, mp, n_patches, got_index, &got_slots, p, &p);
2095 if (patches == NULL)
2098 for (pindex = 0; pindex < n_patches; ++pindex) {
2099 MonoJumpInfo *ji = &patches [pindex];
2101 if (!aot_module->got [got_slots [pindex]]) {
2102 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2103 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2104 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
2106 ji->type = MONO_PATCH_INFO_NONE;
2112 mono_mempool_destroy (mp);
2115 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2119 method = mono_get_method (image, token, NULL);
2121 full_name = mono_method_full_name (method, TRUE);
2124 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
2125 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2128 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);
2134 mono_jit_stats.methods_aot++;
2136 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2138 init_plt (aot_module);
2140 if (method && method->wrapper_type)
2141 g_hash_table_insert (aot_module->method_to_code, method, code);
2145 if (from_plt && klass && !klass->generic_container)
2146 mono_runtime_class_init (mono_class_vtable (domain, klass));
2151 /* FIXME: The space in domain->mp is wasted */
2152 if (aot_module->opts & MONO_OPT_SHARED)
2153 /* No need to cache patches */
2154 mono_mempool_destroy (mp);
2163 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
2165 guint32 table_size, entry_size, hash;
2166 guint32 *table, *entry;
2174 table_size = amodule->extra_method_table [0];
2175 table = amodule->extra_method_table + 1;
2178 if (method->wrapper_type) {
2179 name = mono_aot_wrapper_name (method);
2182 hash = mono_aot_method_hash (method) % table_size;
2184 entry = &table [hash * entry_size];
2191 guint32 key = entry [0];
2192 guint32 value = entry [1];
2193 guint32 next = entry [entry_size - 1];
2196 int is_wrapper_name;
2198 p = amodule->extra_method_info + key;
2199 is_wrapper_name = decode_value (p, &p);
2200 if (is_wrapper_name) {
2201 int wrapper_type = decode_value (p, &p);
2202 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2206 } else if (can_method_ref_match_method (amodule, p, method)) {
2209 if (!amodule->method_ref_to_method)
2210 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2211 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2215 m = decode_method_ref_2 (amodule, p, &p);
2218 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2224 printf ("%d %s %s\n", num_checks, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
2233 entry = &table [next * entry_size];
2243 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2245 g_ptr_array_add ((GPtrArray*)user_data, value);
2249 * find_extra_method:
2251 * Try finding METHOD in the extra_method table in all AOT images.
2252 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2253 * module where the method was found.
2256 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2262 /* Try the method's module first */
2263 *out_amodule = method->klass->image->aot_module;
2264 index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
2265 if (index != 0xffffff)
2269 * Try all other modules.
2270 * This is needed because generic instances klass->image points to the image
2271 * containing the generic definition, but the native code is generated to the
2272 * AOT image which contains the reference.
2275 /* Make a copy to avoid doing the search inside the aot lock */
2276 modules = g_ptr_array_new ();
2278 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2282 for (i = 0; i < modules->len; ++i) {
2283 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2285 if (amodule != method->klass->image->aot_module)
2286 index = find_extra_method_in_amodule (amodule, method);
2287 if (index != 0xffffff) {
2288 *out_amodule = amodule;
2293 g_ptr_array_free (modules, TRUE);
2299 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2301 MonoClass *klass = method->klass;
2302 guint32 method_index;
2303 MonoAotModule *amodule = klass->image->aot_module;
2309 if (amodule->out_of_date)
2312 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2313 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2314 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2315 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2318 g_assert (klass->inited);
2320 /* Find method index */
2321 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2322 method = mono_method_get_declaring_generic_method (method);
2323 method_index = mono_metadata_token_index (method->token) - 1;
2324 } else if (method->is_inflated || !method->token) {
2325 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2327 code = g_hash_table_lookup (amodule->method_to_code, method);
2332 method_index = find_extra_method (method, &amodule);
2333 if (method_index == 0xffffff) {
2334 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2337 full_name = mono_method_full_name (method, TRUE);
2338 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2344 if (method_index == 0xffffff)
2347 /* Needed by find_jit_info */
2349 if (!amodule->extra_methods)
2350 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2351 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2355 method_index = mono_metadata_token_index (method->token) - 1;
2358 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2362 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2366 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2368 MonoAotModule *aot_module = image->aot_module;
2374 method_index = mono_metadata_token_index (token) - 1;
2376 return load_method (domain, aot_module, image, NULL, token, method_index);
2382 } IsGotEntryUserData;
2385 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2387 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2388 MonoAotModule *aot_module = (MonoAotModule*)value;
2390 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->got_size)))
2395 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2397 IsGotEntryUserData user_data;
2402 user_data.addr = addr;
2403 user_data.res = FALSE;
2405 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2408 return user_data.res;
2413 MonoAotModule *module;
2414 } FindAotModuleUserData;
2417 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2419 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2420 MonoAotModule *aot_module = (MonoAotModule*)value;
2422 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2423 data->module = aot_module;
2426 static inline MonoAotModule*
2427 find_aot_module (guint8 *code)
2429 FindAotModuleUserData user_data;
2434 /* Reading these need no locking */
2435 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2438 user_data.addr = code;
2439 user_data.module = NULL;
2442 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2445 return user_data.module;
2449 * mono_aot_plt_resolve:
2451 * This function is called by the entries in the PLT to resolve the actual method that
2452 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2455 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2457 #ifdef MONO_ARCH_AOT_SUPPORTED
2458 guint8 *p, *target, *plt_entry;
2460 MonoAotModule *module = (MonoAotModule*)aot_module;
2464 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2466 p = &module->got_info [plt_info_offset];
2468 ji.type = decode_value (p, &p);
2470 mp = mono_mempool_new_size (512);
2471 res = decode_patch (module, mp, &ji, p, &p);
2472 // FIXME: Error handling (how ?)
2475 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2477 mono_mempool_destroy (mp);
2479 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2480 plt_entry = mono_aot_get_plt_entry (code);
2481 g_assert (plt_entry);
2482 mono_arch_patch_plt_entry (plt_entry, target);
2486 g_assert_not_reached ();
2494 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2495 * method in the module is loaded to avoid committing memory by writing to it.
2496 * LOCKING: Assumes the AOT lock is held.
2499 init_plt (MonoAotModule *info)
2501 #ifdef MONO_ARCH_AOT_SUPPORTED
2503 guint8 *buf = info->plt;
2504 #elif defined(__x86_64__) || defined(__arm__)
2509 if (info->plt_inited)
2512 tramp = mono_create_specific_trampoline (info, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2515 /* Initialize the first PLT entry */
2516 make_writable (info->plt, info->plt_end - info->plt);
2517 x86_jump_code (buf, tramp);
2518 #elif defined(__x86_64__) || defined(__arm__)
2520 * Initialize the PLT entries in the GOT to point to the default targets.
2523 /* The first entry points to the AOT trampoline */
2524 ((gpointer*)info->got)[info->plt_got_offset_base] = tramp;
2525 for (i = 1; i < info->plt_size; ++i)
2526 /* All the default entries point to the first entry */
2527 ((gpointer*)info->got)[info->plt_got_offset_base + i] = info->plt;
2529 g_assert_not_reached ();
2532 info->plt_inited = TRUE;
2537 * mono_aot_get_plt_entry:
2539 * Return the address of the PLT entry called by the code at CODE if exists.
2542 mono_aot_get_plt_entry (guint8 *code)
2544 MonoAotModule *aot_module = find_aot_module (code);
2545 #if defined(__arm__)
2552 #if defined(__i386__) || defined(__x86_64__)
2553 if (code [-5] == 0xe8) {
2554 guint32 disp = *(guint32*)(code - 4);
2555 guint8 *target = code + disp;
2557 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2560 #elif defined(__arm__)
2561 ins = ((guint32*)(gpointer)code) [-1];
2563 /* Should be a 'bl' */
2564 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2565 gint32 disp = ((gint32)ins) & 0xffffff;
2566 guint8 *target = code - 4 + 8 + (disp * 4);
2568 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2572 g_assert_not_reached ();
2579 * mono_aot_get_plt_info_offset:
2581 * Return the PLT info offset belonging to the plt entry called by CODE.
2584 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2586 guint8 *plt_entry = mono_aot_get_plt_entry (code);
2588 g_assert (plt_entry);
2590 /* The offset is embedded inside the code after the plt entry */
2591 #if defined(__i386__)
2592 return *(guint32*)(plt_entry + 5);
2593 #elif defined(__x86_64__)
2594 return *(guint32*)(plt_entry + 6);
2595 #elif defined(__arm__)
2596 /* The offset is stored as the 5th word of the plt entry */
2597 return ((guint32*)plt_entry) [4];
2599 g_assert_not_reached ();
2605 load_named_code (MonoAotModule *amodule, const char *name)
2609 int n_patches, got_index, pindex;
2615 symbol = g_strdup_printf ("%s", name);
2616 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2619 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
2621 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2625 symbol = g_strdup_printf ("%s_p", name);
2626 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2629 /* Nothing to patch */
2632 /* Similar to mono_aot_load_method () */
2634 n_patches = decode_value (p, &p);
2637 MonoJumpInfo *patches;
2640 mp = mono_mempool_new ();
2642 got_index = decode_value (p, &p);
2644 patches = load_patch_info (amodule, mp, n_patches, got_index, &got_slots, p, &p);
2647 for (pindex = 0; pindex < n_patches; ++pindex) {
2648 MonoJumpInfo *ji = &patches [pindex];
2651 if (amodule->got [got_slots [pindex]])
2655 * When this code is executed, the runtime may not yet initalized, so
2656 * resolve the patch info by hand.
2658 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2659 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2660 target = mono_get_lmf_addr;
2661 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2662 target = mono_thread_force_interruption_checkpoint;
2663 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2664 target = mono_exception_from_token;
2665 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2666 target = mono_get_throw_exception ();
2668 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2669 target = mono_amd64_throw_exception;
2672 } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
2673 target = mono_amd64_get_original_ip;
2676 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2677 target = mono_arm_throw_exception;
2678 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
2679 target = mono_arm_throw_exception_by_token;
2681 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2682 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2683 target = (gpointer)mono_get_trampoline_func (tramp_type2);
2684 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
2685 /* atoll is needed because the the offset is unsigned */
2689 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
2690 g_assert (res == 1);
2691 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
2692 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
2693 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
2694 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
2695 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
2696 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
2697 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
2698 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
2699 target = mono_thread_get_and_clear_pending_exception;
2701 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
2702 g_assert_not_reached ();
2706 /* Hopefully the code doesn't have patches which need method or
2709 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2713 amodule->got [got_slots [pindex]] = target;
2718 mono_mempool_destroy (mp);
2725 * Return the piece of code identified by NAME from the mscorlib AOT file.
2728 mono_aot_get_named_code (const char *name)
2731 MonoAotModule *amodule;
2733 image = mono_defaults.corlib;
2736 amodule = image->aot_module;
2739 return load_named_code (amodule, name);
2743 * Return a specific trampoline from the AOT file.
2746 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2748 MonoAotModule *amodule;
2749 int index, tramp_size;
2750 guint8 *code, *tramp;
2751 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2753 /* Currently, we keep all trampolines in the mscorlib AOT image */
2754 image = mono_defaults.corlib;
2759 amodule = image->aot_module;
2762 if (amodule->trampoline_index == amodule->num_trampolines)
2763 g_error ("Ran out of trampolines in '%s' (%d)\n", image->name, amodule->num_trampolines);
2765 index = amodule->trampoline_index ++;
2769 if (!generic_trampolines [tramp_type]) {
2772 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2773 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2777 tramp = generic_trampolines [tramp_type];
2780 amodule->got [amodule->trampoline_got_offset_base + (index *2)] = tramp;
2781 amodule->got [amodule->trampoline_got_offset_base + (index *2) + 1] = arg1;
2783 tramp_size = amodule->specific_trampoline_size;
2785 code = amodule->trampolines + (index * tramp_size);
2787 *code_len = tramp_size;
2793 mono_aot_get_unbox_trampoline (MonoMethod *method)
2795 guint32 method_index = mono_metadata_token_index (method->token) - 1;
2796 MonoAotModule *amodule;
2800 if (method->is_inflated) {
2801 guint32 index = find_extra_method (method, &amodule);
2803 g_assert (index != 0xffffff);
2805 symbol = g_strdup_printf ("ut_e_%d", index);
2807 amodule = method->klass->image->aot_module;
2810 symbol = g_strdup_printf ("ut_%d", method_index);
2812 code = load_named_code (amodule, symbol);
2818 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2823 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
2824 code = load_named_code (mono_defaults.corlib->aot_module, symbol);
2833 mono_aot_init (void)
2838 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2844 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2850 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2856 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2862 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2868 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2874 mono_aot_get_plt_entry (guint8 *code)
2880 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2886 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2892 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2894 g_assert_not_reached ();
2900 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2902 g_assert_not_reached ();
2907 mono_aot_get_named_code (const char *name)
2909 g_assert_not_reached ();
2914 mono_aot_get_unbox_trampoline (MonoMethod *method)
2916 g_assert_not_reached ();
2921 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2923 g_assert_not_reached ();