Fix typo in GC_compare_and_exchange
[mono.git] / mono / mini / aot-runtime.c
blob2e74244f70d2ca10ddea8b48d7fcf88f83a07201
1 /*
2 * aot-runtime.c: mono Ahead of Time compiler
4 * Author:
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2002 Ximian, Inc.
9 */
11 #include "config.h"
12 #include <sys/types.h>
13 #ifdef HAVE_UNISTD_H
14 #include <unistd.h>
15 #endif
16 #include <fcntl.h>
17 #include <string.h>
18 #ifdef HAVE_SYS_MMAN_H
19 #include <sys/mman.h>
20 #endif
22 #if PLATFORM_WIN32
23 #include <winsock2.h>
24 #include <windows.h>
25 #endif
27 #ifdef HAVE_EXECINFO_H
28 #include <execinfo.h>
29 #endif
31 #include <errno.h>
32 #include <sys/stat.h>
34 #ifdef HAVE_SYS_WAIT_H
35 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
36 #endif
38 #include <mono/metadata/tabledefs.h>
39 #include <mono/metadata/class.h>
40 #include <mono/metadata/object.h>
41 #include <mono/metadata/tokentype.h>
42 #include <mono/metadata/appdomain.h>
43 #include <mono/metadata/debug-helpers.h>
44 #include <mono/metadata/assembly.h>
45 #include <mono/metadata/metadata-internals.h>
46 #include <mono/metadata/marshal.h>
47 #include <mono/metadata/gc-internal.h>
48 #include <mono/metadata/monitor.h>
49 #include <mono/metadata/threads-types.h>
50 #include <mono/utils/mono-logger.h>
51 #include <mono/utils/mono-mmap.h>
52 #include "mono/utils/mono-compiler.h"
53 #include <mono/utils/mono-counters.h>
55 #include "mini.h"
56 #include "version.h"
58 #ifndef DISABLE_AOT
60 #ifdef PLATFORM_WIN32
61 #define SHARED_EXT ".dll"
62 #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
63 #define SHARED_EXT ".dylib"
64 #else
65 #define SHARED_EXT ".so"
66 #endif
68 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
69 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
71 typedef struct MonoAotModule {
72 char *aot_name;
73 /* Optimization flags used to compile the module */
74 guint32 opts;
75 /* Pointer to the Global Offset Table */
76 gpointer *got;
77 GHashTable *name_cache;
78 GHashTable *extra_methods;
79 /* Maps methods to their code */
80 GHashTable *method_to_code;
81 /* Maps pointers into the method info to the methods themselves */
82 GHashTable *method_ref_to_method;
83 MonoAssemblyName *image_names;
84 char **image_guids;
85 MonoAssembly *assembly;
86 MonoImage **image_table;
87 guint32 image_table_len;
88 gboolean out_of_date;
89 gboolean plt_inited;
90 guint8 *mem_begin;
91 guint8 *mem_end;
92 guint8 *code;
93 guint8 *code_end;
94 guint8 *plt;
95 guint8 *plt_end;
96 guint32 *code_offsets;
97 guint8 *method_info;
98 guint32 *method_info_offsets;
99 guint8 *got_info;
100 guint32 *got_info_offsets;
101 guint8 *ex_info;
102 guint32 *ex_info_offsets;
103 guint32 *method_order;
104 guint32 *method_order_end;
105 guint8 *class_info;
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;
111 guint8 *extra_method_info;
112 guint8 *unwind_info;
114 /* Points to the trampolines */
115 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
116 /* The first unused trampoline of each kind */
117 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
119 MonoAotFileInfo info;
121 gpointer *globals;
122 MonoDl *sofile;
123 } MonoAotModule;
125 static GHashTable *aot_modules;
126 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
127 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
128 static CRITICAL_SECTION aot_mutex;
131 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
132 * AOT modules registered by mono_aot_register_module ().
134 static GHashTable *static_aot_modules;
137 * Whenever to AOT compile loaded assemblies on demand and store them in
138 * a cache under $HOME/.mono/aot-cache.
140 static gboolean use_aot_cache = FALSE;
143 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
144 * use_aot_cache is TRUE.
146 static gboolean spawn_compiler = TRUE;
148 /* For debugging */
149 static gint32 mono_last_aot_method = -1;
151 static gboolean make_unreadable = FALSE;
152 static guint32 name_table_accesses = 0;
154 /* Used to speed-up find_aot_module () */
155 static gsize aot_code_low_addr = (gssize)-1;
156 static gsize aot_code_high_addr = 0;
158 static void
159 init_plt (MonoAotModule *info);
161 /*****************************************************/
162 /* AOT RUNTIME */
163 /*****************************************************/
165 static MonoImage *
166 load_image (MonoAotModule *module, int index)
168 MonoAssembly *assembly;
169 MonoImageOpenStatus status;
171 g_assert (index < module->image_table_len);
173 if (module->image_table [index])
174 return module->image_table [index];
175 if (module->out_of_date)
176 return NULL;
178 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
179 if (!assembly) {
180 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);
181 module->out_of_date = TRUE;
182 return NULL;
185 if (strcmp (assembly->image->guid, module->image_guids [index])) {
186 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);
187 module->out_of_date = TRUE;
188 return NULL;
191 module->image_table [index] = assembly->image;
192 return assembly->image;
196 static inline gint32
197 decode_value (guint8 *ptr, guint8 **rptr)
199 guint8 b = *ptr;
200 gint32 len;
202 if ((b & 0x80) == 0){
203 len = b;
204 ++ptr;
205 } else if ((b & 0x40) == 0){
206 len = ((b & 0x3f) << 8 | ptr [1]);
207 ptr += 2;
208 } else if (b != 0xff) {
209 len = ((b & 0x1f) << 24) |
210 (ptr [1] << 16) |
211 (ptr [2] << 8) |
212 ptr [3];
213 ptr += 4;
215 else {
216 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
217 ptr += 5;
219 if (rptr)
220 *rptr = ptr;
222 //printf ("DECODE: %d.\n", len);
223 return len;
226 static MonoMethod*
227 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
229 static MonoClass*
230 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
232 static MonoGenericInst*
233 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
235 int type_argc, i;
236 MonoType **type_argv;
237 MonoGenericInst *inst;
238 guint8 *p = buf;
240 type_argc = decode_value (p, &p);
241 type_argv = g_new0 (MonoType*, type_argc);
243 for (i = 0; i < type_argc; ++i) {
244 MonoClass *pclass = decode_klass_ref (module, p, &p);
245 if (!pclass) {
246 g_free (type_argv);
247 return NULL;
249 type_argv [i] = &pclass->byval_arg;
252 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
253 g_free (type_argv);
255 *endbuf = p;
257 return inst;
260 static gboolean
261 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
263 gboolean has_class_inst, has_method_inst;
264 guint8 *p = buf;
266 has_class_inst = decode_value (p, &p);
267 if (has_class_inst) {
268 ctx->class_inst = decode_generic_inst (module, p, &p);
269 if (!ctx->class_inst)
270 return FALSE;
272 has_method_inst = decode_value (p, &p);
273 if (has_method_inst) {
274 ctx->method_inst = decode_generic_inst (module, p, &p);
275 if (!ctx->method_inst)
276 return FALSE;
279 *endbuf = p;
280 return TRUE;
283 static MonoClass*
284 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
286 MonoImage *image;
287 MonoClass *klass, *eklass;
288 guint32 token, rank;
289 guint8 *p = buf;
291 token = decode_value (p, &p);
292 if (token == 0) {
293 *endbuf = p;
294 return NULL;
296 if (mono_metadata_token_table (token) == 0) {
297 image = load_image (module, decode_value (p, &p));
298 if (!image)
299 return NULL;
300 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
301 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
302 if (token == MONO_TOKEN_TYPE_SPEC) {
303 MonoTypeEnum type = decode_value (p, &p);
305 if (type == MONO_TYPE_GENERICINST) {
306 MonoClass *gclass;
307 MonoGenericContext ctx;
308 MonoType *type;
310 gclass = decode_klass_ref (module, p, &p);
311 g_assert (gclass->generic_container);
313 memset (&ctx, 0, sizeof (ctx));
314 ctx.class_inst = decode_generic_inst (module, p, &p);
315 if (!ctx.class_inst)
316 return NULL;
317 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
318 klass = mono_class_from_mono_type (type);
319 mono_metadata_free_type (type);
320 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
321 MonoType *t;
322 MonoGenericContainer *container;
324 int num = decode_value (p, &p);
325 gboolean is_method = decode_value (p, &p);
327 if (is_method) {
328 MonoMethod *method_def;
329 g_assert (type == MONO_TYPE_MVAR);
330 method_def = decode_method_ref_2 (module, p, &p);
331 if (!method_def)
332 return NULL;
334 container = mono_method_get_generic_container (method_def);
335 } else {
336 MonoClass *class_def;
337 g_assert (type == MONO_TYPE_VAR);
338 class_def = decode_klass_ref (module, p, &p);
339 if (!class_def)
340 return NULL;
342 container = class_def->generic_container;
345 g_assert (container);
347 // FIXME: Memory management
348 t = g_new0 (MonoType, 1);
349 t->type = type;
350 t->data.generic_param = mono_generic_container_get_param (container, num);
352 // FIXME: Maybe use types directly to avoid
353 // the overhead of creating MonoClass-es
354 klass = mono_class_from_mono_type (t);
356 g_free (t);
357 } else {
358 g_assert_not_reached ();
360 } else {
361 image = load_image (module, decode_value (p, &p));
362 if (!image)
363 return NULL;
364 klass = mono_class_get (image, token);
366 } else if (token == MONO_TOKEN_TYPE_DEF) {
367 /* Array */
368 image = load_image (module, decode_value (p, &p));
369 if (!image)
370 return NULL;
371 rank = decode_value (p, &p);
372 eklass = decode_klass_ref (module, p, &p);
373 klass = mono_array_class_get (eklass, rank);
374 } else {
375 g_assert_not_reached ();
377 g_assert (klass);
378 mono_class_init (klass);
380 *endbuf = p;
381 return klass;
384 static MonoClassField*
385 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
387 MonoClass *klass = decode_klass_ref (module, buf, &buf);
388 guint32 token;
389 guint8 *p = buf;
391 if (!klass)
392 return NULL;
394 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
396 *endbuf = p;
398 return mono_class_get_field (klass, token);
402 * can_method_ref_match_method:
404 * Determine if calling decode_method_ref_2 on P could return the same method as
405 * METHOD. This is an optimization to avoid calling decode_method_ref_2 () which
406 * would create MonoMethods which are not needed etc.
408 static gboolean
409 can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
411 guint8 *p = buf;
412 guint32 image_index, value;
414 /* Keep this in sync with decode_method_ref () */
415 value = decode_value (p, &p);
416 image_index = value >> 24;
418 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
419 guint32 wrapper_type;
421 if (!method->wrapper_type)
422 return FALSE;
424 wrapper_type = decode_value (p, &p);
426 if (method->wrapper_type != wrapper_type)
427 return FALSE;
428 } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
429 if (method->wrapper_type)
430 return FALSE;
433 return TRUE;
437 * decode_method_ref:
439 * Decode a method reference, and return its image and token. This avoids loading
440 * metadata for the method if the caller does not need it. If the method has no token,
441 * then it is loaded from metadata and METHOD is set to the method instance.
443 static MonoImage*
444 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
446 guint32 image_index, value;
447 MonoImage *image = NULL;
448 guint8 *p = buf;
450 if (method)
451 *method = NULL;
452 if (no_aot_trampoline)
453 *no_aot_trampoline = FALSE;
455 value = decode_value (p, &p);
456 image_index = value >> 24;
458 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
459 if (no_aot_trampoline)
460 *no_aot_trampoline = TRUE;
461 value = decode_value (p, &p);
462 image_index = value >> 24;
465 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
466 guint32 wrapper_type;
468 wrapper_type = decode_value (p, &p);
470 /* Doesn't matter */
471 image = mono_defaults.corlib;
473 switch (wrapper_type) {
474 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
475 MonoMethod *m = decode_method_ref_2 (module, p, &p);
477 if (!m)
478 return NULL;
479 mono_class_init (m->klass);
480 *method = mono_marshal_get_remoting_invoke_with_check (m);
481 break;
483 case MONO_WRAPPER_PROXY_ISINST: {
484 MonoClass *klass = decode_klass_ref (module, p, &p);
485 if (!klass)
486 return NULL;
487 *method = mono_marshal_get_proxy_cancast (klass);
488 break;
490 case MONO_WRAPPER_LDFLD:
491 case MONO_WRAPPER_LDFLDA:
492 case MONO_WRAPPER_STFLD:
493 case MONO_WRAPPER_ISINST: {
494 MonoClass *klass = decode_klass_ref (module, p, &p);
495 if (!klass)
496 return NULL;
497 if (wrapper_type == MONO_WRAPPER_LDFLD)
498 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
499 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
500 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
501 else if (wrapper_type == MONO_WRAPPER_STFLD)
502 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
503 else if (wrapper_type == MONO_WRAPPER_ISINST)
504 *method = mono_marshal_get_isinst (klass);
505 else
506 g_assert_not_reached ();
507 break;
509 case MONO_WRAPPER_LDFLD_REMOTE:
510 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
511 break;
512 case MONO_WRAPPER_STFLD_REMOTE:
513 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
514 break;
515 case MONO_WRAPPER_ALLOC: {
516 int atype = decode_value (p, &p);
518 *method = mono_gc_get_managed_allocator_by_type (atype);
519 break;
521 case MONO_WRAPPER_STELEMREF:
522 *method = mono_marshal_get_stelemref ();
523 break;
524 case MONO_WRAPPER_SYNCHRONIZED: {
525 MonoMethod *m = decode_method_ref_2 (module, p, &p);
527 if (!m)
528 return NULL;
529 *method = mono_marshal_get_synchronized_wrapper (m);
530 break;
532 case MONO_WRAPPER_UNKNOWN: {
533 MonoMethodDesc *desc;
534 MonoMethod *orig_method;
535 int subtype = decode_value (p, &p);
537 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
538 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
539 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
540 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
541 else
542 g_assert_not_reached ();
543 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
544 g_assert (orig_method);
545 mono_method_desc_free (desc);
546 *method = mono_monitor_get_fast_path (orig_method);
547 break;
549 case MONO_WRAPPER_RUNTIME_INVOKE: {
550 /* Direct wrapper */
551 MonoMethod *m = decode_method_ref_2 (module, p, &p);
553 if (!m)
554 return NULL;
555 *method = mono_marshal_get_runtime_invoke (m, FALSE);
556 break;
558 default:
559 g_assert_not_reached ();
561 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
562 /* Can't decode these */
563 g_assert_not_reached ();
564 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
565 image_index = decode_value (p, &p);
566 *token = decode_value (p, &p);
568 image = load_image (module, image_index);
569 if (!image)
570 return NULL;
571 } else if (image_index == MONO_AOT_METHODREF_GINST) {
572 MonoClass *klass;
573 MonoGenericContext ctx;
576 * These methods do not have a token which resolves them, so we
577 * resolve them immediately.
579 klass = decode_klass_ref (module, p, &p);
580 if (!klass)
581 return NULL;
583 image_index = decode_value (p, &p);
584 *token = decode_value (p, &p);
586 image = load_image (module, image_index);
587 if (!image)
588 return NULL;
590 *method = mono_get_method_full (image, *token, NULL, NULL);
591 if (!(*method))
592 return NULL;
594 memset (&ctx, 0, sizeof (ctx));
596 if (FALSE && klass->generic_class) {
597 ctx.class_inst = klass->generic_class->context.class_inst;
598 ctx.method_inst = NULL;
600 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
603 memset (&ctx, 0, sizeof (ctx));
605 if (!decode_generic_context (module, &ctx, p, &p))
606 return NULL;
608 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
609 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
610 MonoClass *klass;
611 int method_type;
613 klass = decode_klass_ref (module, p, &p);
614 if (!klass)
615 return NULL;
616 method_type = decode_value (p, &p);
617 *token = 0;
618 switch (method_type) {
619 case 0:
620 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
621 break;
622 case 1:
623 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
624 break;
625 case 2:
626 *method = mono_class_get_method_from_name (klass, "Get", -1);
627 break;
628 case 3:
629 *method = mono_class_get_method_from_name (klass, "Address", -1);
630 break;
631 case 4:
632 *method = mono_class_get_method_from_name (klass, "Set", -1);
633 break;
634 default:
635 g_assert_not_reached ();
637 } else {
638 g_assert (image_index < MONO_AOT_METHODREF_MIN);
639 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
641 image = load_image (module, image_index);
642 if (!image)
643 return NULL;
646 *endbuf = p;
648 return image;
652 * decode_method_ref_2:
654 * Similar to decode_method_ref, but resolve and return the method itself.
656 static MonoMethod*
657 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
659 MonoMethod *method;
660 guint32 token;
661 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
663 if (method)
664 return method;
665 if (!image)
666 return NULL;
667 method = mono_get_method (image, token, NULL);
668 return method;
671 G_GNUC_UNUSED
672 static void
673 make_writable (guint8* addr, guint32 len)
675 guint8 *page_start;
676 int pages, err;
678 if (mono_aot_only)
679 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
681 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1));
682 pages = (addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ();
684 err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_READ | MONO_MMAP_WRITE | MONO_MMAP_EXEC);
685 g_assert (err == 0);
688 static void
689 create_cache_structure (void)
691 const char *home;
692 char *tmp;
693 int err;
695 home = g_get_home_dir ();
696 if (!home)
697 return;
699 tmp = g_build_filename (home, ".mono", NULL);
700 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
701 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
702 #ifdef PLATFORM_WIN32
703 err = mkdir (tmp);
704 #else
705 err = mkdir (tmp, 0777);
706 #endif
707 if (err) {
708 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
709 g_free (tmp);
710 return;
713 g_free (tmp);
714 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
715 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
716 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
717 #ifdef PLATFORM_WIN32
718 err = mkdir (tmp);
719 #else
720 err = mkdir (tmp, 0777);
721 #endif
722 if (err) {
723 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
724 g_free (tmp);
725 return;
728 g_free (tmp);
732 * load_aot_module_from_cache:
734 * Experimental code to AOT compile loaded assemblies on demand.
736 * FIXME:
737 * - Add environment variable MONO_AOT_CACHE_OPTIONS
738 * - Add options for controlling the cache size
739 * - Handle full cache by deleting old assemblies lru style
740 * - Add options for excluding assemblies during development
741 * - Maybe add a threshold after an assembly is AOT compiled
742 * - invoking a new mono process is a security risk
743 * - recompile the AOT module if one of its dependencies changes
745 static MonoDl*
746 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
748 char *fname, *cmd, *tmp2, *aot_options;
749 const char *home;
750 MonoDl *module;
751 gboolean res;
752 gchar *out, *err;
753 gint exit_status;
755 *aot_name = NULL;
757 if (assembly->image->dynamic)
758 return NULL;
760 create_cache_structure ();
762 home = g_get_home_dir ();
764 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
765 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
766 *aot_name = fname;
767 g_free (tmp2);
769 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
770 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
772 if (!module) {
773 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
775 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
777 aot_options = g_strdup_printf ("outfile=%s", fname);
779 if (spawn_compiler) {
780 /* FIXME: security */
781 /* FIXME: Has to pass the assembly loading path to the child process */
782 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
784 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
786 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
787 if (res) {
788 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
789 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
790 else
791 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
792 g_free (out);
793 g_free (err);
795 #endif
796 g_free (cmd);
797 } else {
798 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
799 if (!res) {
800 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
801 } else {
802 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
806 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
808 g_free (aot_options);
811 return module;
814 static void
815 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
817 if (globals) {
818 int i = 0;
820 *value = NULL;
821 for (i = 0; globals [i]; i+= 2) {
822 if (strcmp (globals [i], name) == 0) {
823 *value = globals [i + 1];
824 break;
827 } else {
828 mono_dl_symbol (module, name, value);
832 static void
833 load_aot_module (MonoAssembly *assembly, gpointer user_data)
835 char *aot_name;
836 MonoAotModule *amodule;
837 MonoDl *sofile;
838 gboolean usable = TRUE;
839 char *saved_guid = NULL;
840 char *aot_version = NULL;
841 char *runtime_version, *build_info;
842 char *opt_flags = NULL;
843 gpointer *globals;
844 gboolean full_aot = FALSE;
845 MonoAotFileInfo *file_info = NULL;
846 int i;
847 gpointer *got_addr;
849 if (mono_compile_aot)
850 return;
852 if (assembly->image->aot_module)
854 * Already loaded. This can happen because the assembly loading code might invoke
855 * the assembly load hooks multiple times for the same assembly.
857 return;
859 if (assembly->image->dynamic)
860 return;
862 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
863 return;
865 mono_aot_lock ();
866 if (static_aot_modules)
867 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
868 else
869 globals = NULL;
870 mono_aot_unlock ();
872 if (globals) {
873 /* Statically linked AOT module */
874 sofile = NULL;
875 aot_name = g_strdup_printf ("%s", assembly->aname.name);
876 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
877 } else {
878 if (use_aot_cache)
879 sofile = load_aot_module_from_cache (assembly, &aot_name);
880 else {
881 char *err;
882 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
884 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
886 if (!sofile) {
887 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
888 g_free (err);
893 if (!sofile && !globals) {
894 if (mono_aot_only) {
895 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
896 exit (1);
898 g_free (aot_name);
899 return;
902 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
903 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
904 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
905 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
906 find_symbol (sofile, globals, "mono_aot_got_addr", (gpointer *)&got_addr);
908 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
909 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);
910 usable = FALSE;
912 else {
913 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
914 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
915 usable = FALSE;
919 build_info = mono_get_runtime_build_info ();
920 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
921 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);
922 usable = FALSE;
924 g_free (build_info);
927 char *full_aot_str;
929 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
931 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
932 full_aot = TRUE;
935 if (mono_aot_only && !full_aot) {
936 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
937 exit (1);
939 if (!mono_aot_only && full_aot) {
940 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
941 usable = FALSE;
944 if (!usable) {
945 if (mono_aot_only) {
946 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
947 exit (1);
949 g_free (aot_name);
950 if (sofile)
951 mono_dl_close (sofile);
952 assembly->image->aot_module = NULL;
953 return;
956 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
957 g_assert (file_info);
959 amodule = g_new0 (MonoAotModule, 1);
960 amodule->aot_name = aot_name;
961 amodule->assembly = assembly;
963 memcpy (&amodule->info, file_info, sizeof (*file_info));
965 amodule->got = *got_addr;
966 amodule->got [0] = assembly->image;
967 amodule->globals = globals;
968 amodule->sofile = sofile;
969 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
971 sscanf (opt_flags, "%d", &amodule->opts);
973 /* Read image table */
975 guint32 table_len, i;
976 char *table = NULL;
978 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
979 g_assert (table);
981 table_len = *(guint32*)table;
982 table += sizeof (guint32);
983 amodule->image_table = g_new0 (MonoImage*, table_len);
984 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
985 amodule->image_guids = g_new0 (char*, table_len);
986 amodule->image_table_len = table_len;
987 for (i = 0; i < table_len; ++i) {
988 MonoAssemblyName *aname = &(amodule->image_names [i]);
990 aname->name = g_strdup (table);
991 table += strlen (table) + 1;
992 amodule->image_guids [i] = g_strdup (table);
993 table += strlen (table) + 1;
994 if (table [0] != 0)
995 aname->culture = g_strdup (table);
996 table += strlen (table) + 1;
997 memcpy (aname->public_key_token, table, strlen (table) + 1);
998 table += strlen (table) + 1;
1000 table = ALIGN_PTR_TO (table, 8);
1001 aname->flags = *(guint32*)table;
1002 table += 4;
1003 aname->major = *(guint32*)table;
1004 table += 4;
1005 aname->minor = *(guint32*)table;
1006 table += 4;
1007 aname->build = *(guint32*)table;
1008 table += 4;
1009 aname->revision = *(guint32*)table;
1010 table += 4;
1014 /* Read method and method_info tables */
1015 find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
1016 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
1017 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
1018 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
1019 find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
1020 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
1021 find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
1022 find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
1023 find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
1024 find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
1025 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
1026 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
1027 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
1028 find_symbol (sofile, globals, "extra_method_info", (gpointer *)&amodule->extra_method_info);
1029 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1030 find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
1031 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1032 find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC]));
1033 find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX]));
1034 find_symbol (sofile, globals, "imt_thunks", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK]));
1035 find_symbol (sofile, globals, "unwind_info", (gpointer)&amodule->unwind_info);
1036 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1038 amodule->mem_begin = amodule->code;
1040 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1041 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1043 if (make_unreadable) {
1044 #ifndef PLATFORM_WIN32
1045 guint8 *addr;
1046 guint8 *page_start;
1047 int pages, err, len;
1049 addr = amodule->mem_begin;
1050 len = amodule->mem_end - amodule->mem_begin;
1052 /* Round down in both directions to avoid modifying data which is not ours */
1053 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1054 pages = ((addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ()) - 1;
1055 err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_NONE);
1056 g_assert (err == 0);
1057 #endif
1060 mono_aot_lock ();
1062 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1063 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1065 g_hash_table_insert (aot_modules, assembly, amodule);
1066 mono_aot_unlock ();
1068 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1070 assembly->image->aot_module = amodule;
1072 if (mono_aot_only) {
1073 if (mono_defaults.corlib) {
1074 /* The second got slot contains the mscorlib got addr */
1075 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1077 amodule->got [1] = mscorlib_amodule->got;
1078 } else {
1079 amodule->got [1] = amodule->got;
1084 * Since we store methoddef and classdef tokens when referring to methods/classes in
1085 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1086 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1087 * non-lazily, since we can't handle out-of-date errors later.
1089 for (i = 0; i < amodule->image_table_len; ++i)
1090 load_image (amodule, i);
1092 if (amodule->out_of_date) {
1093 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);
1094 if (mono_aot_only) {
1095 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);
1096 exit (1);
1099 else
1100 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1104 * mono_aot_register_globals:
1106 * This is called by the ctor function in AOT images compiled with the
1107 * 'no-dlsym' option.
1109 void
1110 mono_aot_register_globals (gpointer *globals)
1112 g_assert_not_reached ();
1116 * mono_aot_register_module:
1118 * This should be called by embedding code to register AOT modules statically linked
1119 * into the executable. AOT_INFO should be the value of the
1120 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1122 void
1123 mono_aot_register_module (gpointer *aot_info)
1125 gpointer *globals;
1126 char *aname;
1128 globals = aot_info;
1129 g_assert (globals);
1131 /* Determine the assembly name */
1132 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1133 g_assert (aname);
1135 /* This could be called before startup */
1136 if (aot_modules)
1137 mono_aot_lock ();
1139 if (!static_aot_modules)
1140 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1142 g_hash_table_insert (static_aot_modules, aname, globals);
1144 if (aot_modules)
1145 mono_aot_unlock ();
1148 void
1149 mono_aot_init (void)
1151 InitializeCriticalSection (&aot_mutex);
1152 aot_modules = g_hash_table_new (NULL, NULL);
1154 mono_install_assembly_load_hook (load_aot_module, NULL);
1156 if (g_getenv ("MONO_LASTAOT"))
1157 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1158 if (g_getenv ("MONO_AOT_CACHE"))
1159 use_aot_cache = TRUE;
1162 static gboolean
1163 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1165 guint32 flags;
1167 info->vtable_size = decode_value (buf, &buf);
1168 if (info->vtable_size == -1)
1169 /* Generic type */
1170 return FALSE;
1171 flags = decode_value (buf, &buf);
1172 info->ghcimpl = (flags >> 0) & 0x1;
1173 info->has_finalize = (flags >> 1) & 0x1;
1174 info->has_cctor = (flags >> 2) & 0x1;
1175 info->has_nested_classes = (flags >> 3) & 0x1;
1176 info->blittable = (flags >> 4) & 0x1;
1177 info->has_references = (flags >> 5) & 0x1;
1178 info->has_static_refs = (flags >> 6) & 0x1;
1179 info->no_special_static_fields = (flags >> 7) & 0x1;
1181 if (info->has_cctor) {
1182 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1183 if (!cctor_image)
1184 return FALSE;
1186 if (info->has_finalize) {
1187 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1188 if (!info->finalize_image)
1189 return FALSE;
1192 info->instance_size = decode_value (buf, &buf);
1193 info->class_size = decode_value (buf, &buf);
1194 info->packing_size = decode_value (buf, &buf);
1195 info->min_align = decode_value (buf, &buf);
1197 *endbuf = buf;
1199 return TRUE;
1202 gpointer
1203 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1205 int i;
1206 MonoClass *klass = vtable->klass;
1207 MonoAotModule *aot_module = klass->image->aot_module;
1208 guint8 *info, *p;
1209 MonoCachedClassInfo class_info;
1210 gboolean err;
1211 guint32 token;
1212 MonoImage *image;
1213 gboolean no_aot_trampoline;
1215 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1216 return NULL;
1218 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1219 p = info;
1221 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1222 if (!err)
1223 return NULL;
1225 for (i = 0; i < slot; ++i)
1226 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1228 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1229 if (!image)
1230 return NULL;
1231 if (no_aot_trampoline)
1232 return NULL;
1234 if (mono_metadata_token_index (token) == 0)
1235 return NULL;
1237 return mono_aot_get_method_from_token (domain, image, token);
1240 gboolean
1241 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1243 MonoAotModule *aot_module = klass->image->aot_module;
1244 guint8 *p;
1245 gboolean err;
1247 if (klass->rank || !aot_module)
1248 return FALSE;
1250 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1252 err = decode_cached_class_info (aot_module, res, p, &p);
1253 if (!err)
1254 return FALSE;
1256 return TRUE;
1260 * mono_aot_get_class_from_name:
1262 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1263 * using a cache stored in the AOT file.
1264 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1266 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1267 * found.
1269 gboolean
1270 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1272 MonoAotModule *aot_module = image->aot_module;
1273 guint16 *table, *entry;
1274 guint16 table_size;
1275 guint32 hash;
1276 char full_name_buf [1024];
1277 char *full_name;
1278 const char *name2, *name_space2;
1279 MonoTableInfo *t;
1280 guint32 cols [MONO_TYPEDEF_SIZE];
1281 GHashTable *nspace_table;
1283 if (!aot_module || !aot_module->class_name_table)
1284 return FALSE;
1286 mono_aot_lock ();
1288 *klass = NULL;
1290 /* First look in the cache */
1291 if (!aot_module->name_cache)
1292 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1293 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1294 if (nspace_table) {
1295 *klass = g_hash_table_lookup (nspace_table, name);
1296 if (*klass) {
1297 mono_aot_unlock ();
1298 return TRUE;
1302 table_size = aot_module->class_name_table [0];
1303 table = aot_module->class_name_table + 1;
1305 if (name_space [0] == '\0')
1306 full_name = g_strdup_printf ("%s", name);
1307 else {
1308 if (strlen (name_space) + strlen (name) < 1000) {
1309 sprintf (full_name_buf, "%s.%s", name_space, name);
1310 full_name = full_name_buf;
1311 } else {
1312 full_name = g_strdup_printf ("%s.%s", name_space, name);
1315 hash = mono_aot_str_hash (full_name) % table_size;
1316 if (full_name != full_name_buf)
1317 g_free (full_name);
1319 entry = &table [hash * 2];
1321 if (entry [0] != 0) {
1322 t = &image->tables [MONO_TABLE_TYPEDEF];
1324 while (TRUE) {
1325 guint32 index = entry [0];
1326 guint32 next = entry [1];
1327 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1329 name_table_accesses ++;
1331 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1333 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1334 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1336 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1337 mono_aot_unlock ();
1338 *klass = mono_class_get (image, token);
1340 /* Add to cache */
1341 if (*klass) {
1342 mono_aot_lock ();
1343 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1344 if (!nspace_table) {
1345 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1346 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1348 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1349 mono_aot_unlock ();
1351 return TRUE;
1354 if (next != 0) {
1355 entry = &table [next * 2];
1356 } else {
1357 break;
1362 mono_aot_unlock ();
1364 return TRUE;
1368 * LOCKING: Acquires the domain lock.
1370 static MonoJitInfo*
1371 decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
1372 MonoMethod *method, guint8* ex_info, guint8 *code)
1374 int i, buf_len;
1375 MonoJitInfo *jinfo;
1376 guint code_len, used_int_regs, flags;
1377 gboolean has_generic_jit_info, has_dwarf_unwind_info;
1378 guint8 *p;
1379 MonoMethodHeader *header;
1380 int generic_info_size;
1382 header = mono_method_get_header (method);
1384 /* Load the method info from the AOT file */
1386 p = ex_info;
1387 code_len = decode_value (p, &p);
1388 flags = decode_value (p, &p);
1389 has_generic_jit_info = (flags & 1) != 0;
1390 has_dwarf_unwind_info = (flags & 2) != 0;
1391 if (has_dwarf_unwind_info) {
1392 guint32 offset;
1394 offset = decode_value (p, &p);
1395 g_assert (offset < (1 << 30));
1396 used_int_regs = offset;
1397 } else {
1398 used_int_regs = decode_value (p, &p);
1400 if (has_generic_jit_info)
1401 generic_info_size = sizeof (MonoGenericJitInfo);
1402 else
1403 generic_info_size = 0;
1405 /* Exception table */
1406 if (header && header->num_clauses) {
1407 jinfo =
1408 mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + (sizeof (MonoJitExceptionInfo) * header->num_clauses) + generic_info_size);
1409 jinfo->num_clauses = header->num_clauses;
1411 for (i = 0; i < header->num_clauses; ++i) {
1412 MonoExceptionClause *ec = &header->clauses [i];
1413 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1415 ei->flags = ec->flags;
1416 ei->exvar_offset = decode_value (p, &p);
1418 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1419 ei->data.filter = code + decode_value (p, &p);
1420 else
1421 ei->data.catch_class = ec->data.catch_class;
1423 ei->try_start = code + decode_value (p, &p);
1424 ei->try_end = code + decode_value (p, &p);
1425 ei->handler_start = code + decode_value (p, &p);
1428 else {
1429 jinfo = mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + generic_info_size);
1432 jinfo->code_size = code_len;
1433 jinfo->used_regs = used_int_regs;
1434 jinfo->method = method;
1435 jinfo->code_start = code;
1436 jinfo->domain_neutral = 0;
1437 jinfo->from_aot = 1;
1439 if (has_generic_jit_info) {
1440 MonoGenericJitInfo *gi;
1442 jinfo->has_generic_jit_info = 1;
1444 gi = mono_jit_info_get_generic_jit_info (jinfo);
1445 g_assert (gi);
1447 gi->has_this = decode_value (p, &p);
1448 gi->this_reg = decode_value (p, &p);
1449 gi->this_offset = decode_value (p, &p);
1451 /* This currently contains no data */
1452 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1454 jinfo->method = decode_method_ref_2 (aot_module, p, &p);
1457 /* Load debug info */
1458 buf_len = decode_value (p, &p);
1459 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1461 return jinfo;
1465 * mono_aot_get_unwind_info:
1467 * Return a pointer to the DWARF unwind info belonging to JI.
1469 guint8*
1470 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
1472 MonoAotModule *amodule = ji->method->klass->image->aot_module;
1473 guint8 *p;
1475 g_assert (amodule);
1476 g_assert (ji->from_aot);
1478 p = amodule->unwind_info + ji->used_regs;
1479 *unwind_info_len = decode_value (p, &p);
1480 return p;
1483 MonoJitInfo *
1484 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1486 int pos, left, right, offset, offset1, offset2, last_offset, new_offset;
1487 int page_index, method_index, table_len, is_wrapper;
1488 guint32 token;
1489 MonoAotModule *amodule = image->aot_module;
1490 MonoMethod *method;
1491 MonoJitInfo *jinfo;
1492 guint8 *code, *ex_info, *p;
1493 guint32 *table, *ptr;
1494 gboolean found;
1496 if (!amodule)
1497 return NULL;
1499 if (domain != mono_get_root_domain ())
1500 /* FIXME: */
1501 return NULL;
1503 offset = (guint8*)addr - amodule->code;
1505 /* First search through the index */
1506 ptr = amodule->method_order;
1507 last_offset = 0;
1508 page_index = 0;
1509 found = FALSE;
1511 if (*ptr == 0xffffff)
1512 return NULL;
1513 ptr ++;
1515 while (*ptr != 0xffffff) {
1516 guint32 method_index = ptr [0];
1517 new_offset = amodule->code_offsets [method_index];
1519 if (offset >= last_offset && offset < new_offset) {
1520 found = TRUE;
1521 break;
1524 ptr ++;
1525 last_offset = new_offset;
1526 page_index ++;
1529 /* Skip rest of index */
1530 while (*ptr != 0xffffff)
1531 ptr ++;
1532 ptr ++;
1534 table = ptr;
1535 table_len = amodule->method_order_end - table;
1537 g_assert (table <= amodule->method_order_end);
1539 if (found) {
1540 left = (page_index * 1024);
1541 right = left + 1024;
1543 if (right > table_len)
1544 right = table_len;
1546 offset1 = amodule->code_offsets [table [left]];
1547 g_assert (offset1 <= offset);
1549 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1551 else {
1552 //printf ("Not found in index: 0x%x\n", offset);
1553 left = 0;
1554 right = table_len;
1557 /* Binary search inside the method_order table to find the method */
1558 while (TRUE) {
1559 pos = (left + right) / 2;
1561 g_assert (table + pos <= amodule->method_order_end);
1563 //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]]);
1565 offset1 = amodule->code_offsets [table [pos]];
1566 if (table + pos + 1 >= amodule->method_order_end)
1567 offset2 = amodule->code_end - amodule->code;
1568 else
1569 offset2 = amodule->code_offsets [table [pos + 1]];
1571 if (offset < offset1)
1572 right = pos;
1573 else if (offset >= offset2)
1574 left = pos + 1;
1575 else
1576 break;
1579 method_index = table [pos];
1581 /* Might be a wrapper/extra method */
1582 if (amodule->extra_methods) {
1583 mono_aot_lock ();
1584 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
1585 mono_aot_unlock ();
1586 } else {
1587 method = NULL;
1590 if (!method) {
1591 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
1593 * This is hit for extra methods which are called directly, so they are
1594 * not in amodule->extra_methods.
1596 table_len = amodule->extra_method_info_offsets [0];
1597 table = amodule->extra_method_info_offsets + 1;
1598 left = 0;
1599 right = table_len;
1600 pos = 0;
1602 /* Binary search */
1603 while (TRUE) {
1604 pos = ((left + right) / 2);
1606 g_assert (pos < table_len);
1608 if (table [pos * 2] < method_index)
1609 left = pos + 1;
1610 else if (table [pos * 2] > method_index)
1611 right = pos;
1612 else
1613 break;
1616 p = amodule->extra_method_info + table [(pos * 2) + 1];
1617 is_wrapper = decode_value (p, &p);
1618 g_assert (!is_wrapper);
1619 method = decode_method_ref_2 (amodule, p, &p);
1620 g_assert (method);
1621 } else {
1622 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1623 method = mono_get_method (image, token, NULL);
1627 /* FIXME: */
1628 g_assert (method);
1630 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1632 code = &amodule->code [amodule->code_offsets [method_index]];
1633 ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
1635 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code);
1637 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1638 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1640 /* Add it to the normal JitInfo tables */
1641 mono_jit_info_table_add (domain, jinfo);
1643 return jinfo;
1646 static gboolean
1647 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1649 guint8 *p = buf;
1650 gpointer *table;
1651 MonoImage *image;
1652 int i;
1654 switch (ji->type) {
1655 case MONO_PATCH_INFO_METHOD:
1656 case MONO_PATCH_INFO_METHOD_JUMP:
1657 case MONO_PATCH_INFO_ICALL_ADDR:
1658 case MONO_PATCH_INFO_METHOD_RGCTX: {
1659 guint32 token;
1660 MonoMethod *method;
1661 gboolean no_aot_trampoline;
1663 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
1664 if (!image)
1665 goto cleanup;
1667 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1668 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (image, token));
1669 ji->type = MONO_PATCH_INFO_ABS;
1671 else {
1672 if (method)
1673 ji->data.method = method;
1674 else
1675 ji->data.method = mono_get_method (image, token, NULL);
1676 g_assert (ji->data.method);
1677 mono_class_init (ji->data.method->klass);
1679 break;
1681 case MONO_PATCH_INFO_INTERNAL_METHOD:
1682 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1683 guint32 len = decode_value (p, &p);
1685 ji->data.name = (char*)p;
1686 p += len + 1;
1687 break;
1689 case MONO_PATCH_INFO_METHODCONST:
1690 /* Shared */
1691 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1692 if (!ji->data.method)
1693 goto cleanup;
1694 break;
1695 case MONO_PATCH_INFO_VTABLE:
1696 case MONO_PATCH_INFO_CLASS:
1697 case MONO_PATCH_INFO_IID:
1698 case MONO_PATCH_INFO_ADJUSTED_IID:
1699 /* Shared */
1700 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1701 if (!ji->data.klass)
1702 goto cleanup;
1703 break;
1704 case MONO_PATCH_INFO_CLASS_INIT:
1705 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1706 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1707 if (!ji->data.klass)
1708 goto cleanup;
1709 break;
1710 case MONO_PATCH_INFO_IMAGE:
1711 ji->data.image = load_image (aot_module, decode_value (p, &p));
1712 if (!ji->data.image)
1713 goto cleanup;
1714 break;
1715 case MONO_PATCH_INFO_FIELD:
1716 case MONO_PATCH_INFO_SFLDA:
1717 /* Shared */
1718 ji->data.field = decode_field_info (aot_module, p, &p);
1719 if (!ji->data.field)
1720 goto cleanup;
1721 break;
1722 case MONO_PATCH_INFO_SWITCH:
1723 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1724 ji->data.table->table_size = decode_value (p, &p);
1725 table = g_new (gpointer, ji->data.table->table_size);
1726 ji->data.table->table = (MonoBasicBlock**)table;
1727 for (i = 0; i < ji->data.table->table_size; i++)
1728 table [i] = (gpointer)(gssize)decode_value (p, &p);
1729 break;
1730 case MONO_PATCH_INFO_R4: {
1731 guint32 val;
1733 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
1734 val = decode_value (p, &p);
1735 *(float*)ji->data.target = *(float*)&val;
1736 break;
1738 case MONO_PATCH_INFO_R8: {
1739 guint32 val [2];
1741 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
1743 val [0] = decode_value (p, &p);
1744 val [1] = decode_value (p, &p);
1745 *(double*)ji->data.target = *(double*)val;
1746 break;
1748 case MONO_PATCH_INFO_LDSTR:
1749 image = load_image (aot_module, decode_value (p, &p));
1750 if (!image)
1751 goto cleanup;
1752 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1753 break;
1754 case MONO_PATCH_INFO_RVA:
1755 case MONO_PATCH_INFO_DECLSEC:
1756 case MONO_PATCH_INFO_LDTOKEN:
1757 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1758 /* Shared */
1759 image = load_image (aot_module, decode_value (p, &p));
1760 if (!image)
1761 goto cleanup;
1762 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1764 ji->data.token->has_context = decode_value (p, &p);
1765 if (ji->data.token->has_context) {
1766 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
1767 if (!res)
1768 goto cleanup;
1770 break;
1771 case MONO_PATCH_INFO_EXC_NAME:
1772 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1773 if (!ji->data.klass)
1774 goto cleanup;
1775 ji->data.name = ji->data.klass->name;
1776 break;
1777 case MONO_PATCH_INFO_METHOD_REL:
1778 ji->data.offset = decode_value (p, &p);
1779 break;
1780 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1781 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1782 case MONO_PATCH_INFO_MONITOR_ENTER:
1783 case MONO_PATCH_INFO_MONITOR_EXIT:
1784 break;
1785 case MONO_PATCH_INFO_RGCTX_FETCH: {
1786 gboolean res;
1787 MonoJumpInfoRgctxEntry *entry;
1789 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1790 entry->method = decode_method_ref_2 (aot_module, p, &p);
1791 entry->in_mrgctx = decode_value (p, &p);
1792 entry->info_type = decode_value (p, &p);
1793 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1794 entry->data->type = decode_value (p, &p);
1796 res = decode_patch (aot_module, mp, entry->data, p, &p);
1797 if (!res)
1798 goto cleanup;
1799 ji->data.rgctx_entry = entry;
1800 break;
1802 default:
1803 g_warning ("unhandled type %d", ji->type);
1804 g_assert_not_reached ();
1807 *endbuf = p;
1809 return TRUE;
1811 cleanup:
1812 return FALSE;
1815 static MonoJumpInfo*
1816 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1817 guint32 **got_slots,
1818 guint8 *buf, guint8 **endbuf)
1820 MonoJumpInfo *patches;
1821 int pindex;
1822 guint8 *p;
1824 p = buf;
1826 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1828 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1830 for (pindex = 0; pindex < n_patches; ++pindex) {
1831 MonoJumpInfo *ji = &patches [pindex];
1832 guint8 *shared_p;
1833 gboolean res;
1834 guint32 got_offset;
1836 got_offset = decode_value (p, &p);
1838 if (aot_module->got [got_offset]) {
1839 /* Already loaded */
1840 //printf ("HIT!\n");
1841 } else {
1842 shared_p = aot_module->got_info + aot_module->got_info_offsets [got_offset];
1844 ji->type = decode_value (shared_p, &shared_p);
1846 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1847 if (!res)
1848 goto cleanup;
1851 (*got_slots) [pindex] = got_offset;
1854 *endbuf = p;
1855 return patches;
1857 cleanup:
1858 g_free (*got_slots);
1859 *got_slots = NULL;
1861 return NULL;
1864 static void
1865 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1868 * Jump addresses cannot be patched by the trampoline code since it
1869 * does not have access to the caller's address. Instead, we collect
1870 * the addresses of the GOT slots pointing to a method, and patch
1871 * them after the method has been compiled.
1873 MonoJitDomainInfo *info = domain_jit_info (domain);
1874 GSList *list;
1876 mono_domain_lock (domain);
1877 if (!info->jump_target_got_slot_hash)
1878 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1879 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1880 list = g_slist_prepend (list, got_slot);
1881 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
1882 mono_domain_unlock (domain);
1886 * load_method:
1888 * Load the method identified by METHOD_INDEX from the AOT image. Return a
1889 * pointer to the native code of the method, or NULL if not found.
1890 * METHOD might not be set if the caller only has the image/token info.
1892 static gpointer
1893 load_method (MonoDomain *domain, MonoAotModule *aot_module, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
1895 MonoClass *klass;
1896 gboolean from_plt = method == NULL;
1897 MonoMemPool *mp;
1898 int i, pindex, n_patches, used_strings;
1899 gboolean keep_patches = TRUE;
1900 guint8 *p, *ex_info;
1901 MonoJitInfo *jinfo = NULL;
1902 guint8 *code, *info;
1904 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1905 return NULL;
1907 if ((domain != mono_get_root_domain ()) && (!(aot_module->opts & MONO_OPT_SHARED)))
1908 /* Non shared AOT code can't be used in other appdomains */
1909 return NULL;
1911 if (aot_module->out_of_date)
1912 return NULL;
1914 if (aot_module->code_offsets [method_index] == 0xffffffff) {
1915 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1916 char *full_name;
1918 if (!method)
1919 method = mono_get_method (image, token, NULL);
1920 full_name = mono_method_full_name (method, TRUE);
1921 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1922 g_free (full_name);
1924 return NULL;
1927 code = &aot_module->code [aot_module->code_offsets [method_index]];
1928 info = &aot_module->method_info [aot_module->method_info_offsets [method_index]];
1930 mono_aot_lock ();
1931 if (!aot_module->methods_loaded)
1932 aot_module->methods_loaded = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 1);
1933 mono_aot_unlock ();
1935 if ((aot_module->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1936 return code;
1938 if (mono_last_aot_method != -1) {
1939 if (mono_jit_stats.methods_aot > mono_last_aot_method)
1940 return NULL;
1941 else
1942 if (method && mono_jit_stats.methods_aot == mono_last_aot_method)
1943 printf ("LAST AOT METHOD: %s.%s.%s.\n", method->klass->name_space, method->klass->name, method->name);
1946 p = info;
1948 if (method) {
1949 klass = method->klass;
1950 decode_klass_ref (aot_module, p, &p);
1951 } else {
1952 klass = decode_klass_ref (aot_module, p, &p);
1955 if (aot_module->opts & MONO_OPT_SHARED)
1956 used_strings = decode_value (p, &p);
1957 else
1958 used_strings = 0;
1960 for (i = 0; i < used_strings; i++) {
1961 guint token = decode_value (p, &p);
1962 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
1965 if (aot_module->opts & MONO_OPT_SHARED)
1966 keep_patches = FALSE;
1968 n_patches = decode_value (p, &p);
1970 keep_patches = FALSE;
1972 if (n_patches) {
1973 MonoJumpInfo *patches;
1974 guint32 *got_slots;
1976 if (keep_patches)
1977 mp = domain->mp;
1978 else
1979 mp = mono_mempool_new ();
1981 patches = load_patch_info (aot_module, mp, n_patches, &got_slots, p, &p);
1982 if (patches == NULL)
1983 goto cleanup;
1985 for (pindex = 0; pindex < n_patches; ++pindex) {
1986 MonoJumpInfo *ji = &patches [pindex];
1988 if (!aot_module->got [got_slots [pindex]]) {
1989 aot_module->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
1990 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
1991 aot_module->got [got_slots [pindex]] = mono_create_ftnptr (domain, aot_module->got [got_slots [pindex]]);
1992 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
1993 register_jump_target_got_slot (domain, ji->data.method, &(aot_module->got [got_slots [pindex]]));
1995 ji->type = MONO_PATCH_INFO_NONE;
1998 g_free (got_slots);
2000 if (!keep_patches)
2001 mono_mempool_destroy (mp);
2004 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2005 char *full_name;
2007 if (!method)
2008 method = mono_get_method (image, token, NULL);
2010 full_name = mono_method_full_name (method, TRUE);
2012 if (!jinfo) {
2013 ex_info = &aot_module->ex_info [aot_module->ex_info_offsets [method_index]];
2014 jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
2017 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);
2018 g_free (full_name);
2021 mono_aot_lock ();
2023 mono_jit_stats.methods_aot++;
2025 aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2027 init_plt (aot_module);
2029 if (method && method->wrapper_type)
2030 g_hash_table_insert (aot_module->method_to_code, method, code);
2032 mono_aot_unlock ();
2034 if (from_plt && klass && !klass->generic_container)
2035 mono_runtime_class_init (mono_class_vtable (domain, klass));
2037 return code;
2039 cleanup:
2040 /* FIXME: The space in domain->mp is wasted */
2041 if (aot_module->opts & MONO_OPT_SHARED)
2042 /* No need to cache patches */
2043 mono_mempool_destroy (mp);
2045 if (jinfo)
2046 g_free (jinfo);
2048 return NULL;
2051 static guint32
2052 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
2054 guint32 table_size, entry_size, hash;
2055 guint32 *table, *entry;
2056 char *name = NULL;
2057 guint32 index;
2058 static guint32 n_extra_decodes;
2060 if (!amodule)
2061 return 0xffffff;
2063 table_size = amodule->extra_method_table [0];
2064 table = amodule->extra_method_table + 1;
2065 entry_size = 3;
2067 if (method->wrapper_type) {
2068 name = mono_aot_wrapper_name (method);
2071 hash = mono_aot_method_hash (method) % table_size;
2073 entry = &table [hash * entry_size];
2075 if (entry [0] == 0)
2076 return 0xffffff;
2078 index = 0xffffff;
2079 while (TRUE) {
2080 guint32 key = entry [0];
2081 guint32 value = entry [1];
2082 guint32 next = entry [entry_size - 1];
2083 MonoMethod *m;
2084 guint8 *p;
2085 int is_wrapper_name;
2087 p = amodule->extra_method_info + key;
2088 is_wrapper_name = decode_value (p, &p);
2089 if (is_wrapper_name) {
2090 int wrapper_type = decode_value (p, &p);
2091 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2092 index = value;
2093 break;
2095 } else if (can_method_ref_match_method (amodule, p, method)) {
2096 mono_aot_lock ();
2097 if (!amodule->method_ref_to_method)
2098 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2099 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2100 mono_aot_unlock ();
2101 if (!m) {
2102 guint8 *orig_p = p;
2103 m = decode_method_ref_2 (amodule, p, &p);
2104 if (m) {
2105 mono_aot_lock ();
2106 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2107 mono_aot_unlock ();
2110 if (m == method) {
2111 index = value;
2112 break;
2115 /* Special case: wrappers of shared generic methods */
2116 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2117 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2118 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2119 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2121 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2122 index = value;
2123 break;
2127 /* Methods decoded needlessly */
2129 if (m)
2130 printf ("%d %s %s\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
2132 n_extra_decodes ++;
2135 if (next != 0)
2136 entry = &table [next * entry_size];
2137 else
2138 break;
2141 g_free (name);
2142 return index;
2145 static void
2146 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2148 g_ptr_array_add ((GPtrArray*)user_data, value);
2152 * find_extra_method:
2154 * Try finding METHOD in the extra_method table in all AOT images.
2155 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2156 * module where the method was found.
2158 static guint32
2159 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2161 guint32 index;
2162 GPtrArray *modules;
2163 int i;
2165 /* Try the method's module first */
2166 *out_amodule = method->klass->image->aot_module;
2167 index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
2168 if (index != 0xffffff)
2169 return index;
2172 * Try all other modules.
2173 * This is needed because generic instances klass->image points to the image
2174 * containing the generic definition, but the native code is generated to the
2175 * AOT image which contains the reference.
2178 /* Make a copy to avoid doing the search inside the aot lock */
2179 modules = g_ptr_array_new ();
2180 mono_aot_lock ();
2181 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2182 mono_aot_unlock ();
2184 index = 0xffffff;
2185 for (i = 0; i < modules->len; ++i) {
2186 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2188 if (amodule != method->klass->image->aot_module)
2189 index = find_extra_method_in_amodule (amodule, method);
2190 if (index != 0xffffff) {
2191 *out_amodule = amodule;
2192 break;
2196 g_ptr_array_free (modules, TRUE);
2198 return index;
2202 * mono_aot_get_method:
2204 * Return a pointer to the AOTed native code for METHOD if it can be found,
2205 * NULL otherwise.
2206 * On platforms with function pointers, this doesn't return a function pointer.
2208 gpointer
2209 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2211 MonoClass *klass = method->klass;
2212 guint32 method_index;
2213 MonoAotModule *amodule = klass->image->aot_module;
2214 guint8 *code;
2216 if (!amodule)
2217 return NULL;
2219 if (amodule->out_of_date)
2220 return NULL;
2222 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2223 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2224 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2225 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2226 return NULL;
2229 * Use the original method instead of its invoke-with-check wrapper.
2230 * This is not a problem when using full-aot, since it doesn't support
2231 * remoting.
2233 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
2234 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
2236 g_assert (klass->inited);
2238 /* Find method index */
2239 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2240 method = mono_method_get_declaring_generic_method (method);
2241 method_index = mono_metadata_token_index (method->token) - 1;
2242 } else if (method->is_inflated || !method->token) {
2243 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2244 mono_aot_lock ();
2245 code = g_hash_table_lookup (amodule->method_to_code, method);
2246 mono_aot_unlock ();
2247 if (code)
2248 return code;
2250 method_index = find_extra_method (method, &amodule);
2251 if (method_index == 0xffffff) {
2252 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2253 char *full_name;
2255 full_name = mono_method_full_name (method, TRUE);
2256 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2257 g_free (full_name);
2259 return NULL;
2262 if (method_index == 0xffffff)
2263 return NULL;
2265 /* Needed by find_jit_info */
2266 mono_aot_lock ();
2267 if (!amodule->extra_methods)
2268 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2269 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2270 mono_aot_unlock ();
2271 } else {
2272 /* Common case */
2273 method_index = mono_metadata_token_index (method->token) - 1;
2276 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2280 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2281 * method.
2283 gpointer
2284 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2286 MonoAotModule *aot_module = image->aot_module;
2287 int method_index;
2289 if (!aot_module)
2290 return NULL;
2292 method_index = mono_metadata_token_index (token) - 1;
2294 return load_method (domain, aot_module, image, NULL, token, method_index);
2297 typedef struct {
2298 guint8 *addr;
2299 gboolean res;
2300 } IsGotEntryUserData;
2302 static void
2303 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2305 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2306 MonoAotModule *aot_module = (MonoAotModule*)value;
2308 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
2309 data->res = TRUE;
2312 gboolean
2313 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2315 IsGotEntryUserData user_data;
2317 if (!aot_modules)
2318 return FALSE;
2320 user_data.addr = addr;
2321 user_data.res = FALSE;
2322 mono_aot_lock ();
2323 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2324 mono_aot_unlock ();
2326 return user_data.res;
2329 typedef struct {
2330 guint8 *addr;
2331 MonoAotModule *module;
2332 } FindAotModuleUserData;
2334 static void
2335 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2337 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2338 MonoAotModule *aot_module = (MonoAotModule*)value;
2340 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2341 data->module = aot_module;
2344 static inline MonoAotModule*
2345 find_aot_module (guint8 *code)
2347 FindAotModuleUserData user_data;
2349 if (!aot_modules)
2350 return NULL;
2352 /* Reading these need no locking */
2353 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2354 return NULL;
2356 user_data.addr = code;
2357 user_data.module = NULL;
2359 mono_aot_lock ();
2360 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2361 mono_aot_unlock ();
2363 return user_data.module;
2367 * mono_aot_plt_resolve:
2369 * This function is called by the entries in the PLT to resolve the actual method that
2370 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2372 gpointer
2373 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2375 #ifdef MONO_ARCH_AOT_SUPPORTED
2376 guint8 *p, *target, *plt_entry;
2377 MonoJumpInfo ji;
2378 MonoAotModule *module = (MonoAotModule*)aot_module;
2379 gboolean res;
2380 MonoMemPool *mp;
2382 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2384 p = &module->got_info [plt_info_offset];
2386 ji.type = decode_value (p, &p);
2388 mp = mono_mempool_new_size (512);
2389 res = decode_patch (module, mp, &ji, p, &p);
2390 // FIXME: Error handling (how ?)
2391 g_assert (res);
2394 * Avoid calling resolve_patch_target in the full-aot case if possible, since
2395 * it would create a trampoline, and we don't need that.
2396 * We could do this only if the method does not need the special handling
2397 * in mono_magic_trampoline ().
2399 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) &&
2400 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
2401 target = mono_jit_compile_method (ji.data.method);
2402 } else {
2403 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2406 mono_mempool_destroy (mp);
2408 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2409 plt_entry = mono_aot_get_plt_entry (code);
2410 g_assert (plt_entry);
2411 mono_arch_patch_plt_entry (plt_entry, module->got, NULL, target);
2413 return target;
2414 #else
2415 g_assert_not_reached ();
2416 return NULL;
2417 #endif
2421 * init_plt:
2423 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2424 * method in the module is loaded to avoid committing memory by writing to it.
2425 * LOCKING: Assumes the AOT lock is held.
2427 static void
2428 init_plt (MonoAotModule *amodule)
2430 #ifndef MONO_CROSS_COMPILE
2432 #ifdef MONO_ARCH_AOT_SUPPORTED
2433 #ifdef __i386__
2434 guint8 *buf = amodule->plt;
2435 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
2436 int i;
2437 gpointer plt_0;
2438 #endif
2439 gpointer tramp;
2441 if (amodule->plt_inited)
2442 return;
2444 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2446 #ifdef __i386__
2447 /* Initialize the first PLT entry */
2448 make_writable (amodule->plt, amodule->plt_end - amodule->plt);
2449 x86_jump_code (buf, tramp);
2450 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
2452 * Initialize the PLT entries in the GOT to point to the default targets.
2455 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
2456 plt_0 = mono_create_ftnptr (mono_domain_get (), amodule->plt);
2457 /* The first entry points to the AOT trampoline */
2458 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base] = tramp;
2459 for (i = 1; i < amodule->info.plt_size; ++i)
2460 /* All the default entries point to the first entry */
2461 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = plt_0;
2462 #else
2463 g_assert_not_reached ();
2464 #endif
2466 amodule->plt_inited = TRUE;
2467 #endif
2469 #endif /* MONO_CROSS_COMPILE */
2473 * mono_aot_get_plt_entry:
2475 * Return the address of the PLT entry called by the code at CODE if exists.
2477 guint8*
2478 mono_aot_get_plt_entry (guint8 *code)
2480 MonoAotModule *aot_module = find_aot_module (code);
2481 #if defined(__arm__) || defined(__mono_ppc__)
2482 guint32 ins;
2483 #endif
2485 if (!aot_module)
2486 return NULL;
2488 #if defined(__i386__) || defined(__x86_64__)
2489 if (code [-5] == 0xe8) {
2490 guint32 disp = *(guint32*)(code - 4);
2491 guint8 *target = code + disp;
2493 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2494 return target;
2496 #elif defined(__arm__)
2497 ins = ((guint32*)(gpointer)code) [-1];
2499 /* Should be a 'bl' */
2500 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2501 gint32 disp = ((gint32)ins) & 0xffffff;
2502 guint8 *target = code - 4 + 8 + (disp * 4);
2504 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2505 return target;
2507 #elif defined(__mono_ppc__)
2508 /* Should be a bl */
2509 ins = ((guint32*)(gpointer)code) [-1];
2511 if ((ins >> 26 == 18) && ((ins & 1) == 1) && ((ins & 2) == 0)) {
2512 gint32 disp = (((gint32)ins) >> 2) & 0xffffff;
2513 guint8 *target = code - 4 + (disp * 4);
2515 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2516 return target;
2518 #else
2519 g_assert_not_reached ();
2520 #endif
2522 return NULL;
2526 * mono_aot_get_plt_info_offset:
2528 * Return the PLT info offset belonging to the plt entry called by CODE.
2530 guint32
2531 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2533 guint8 *plt_entry = mono_aot_get_plt_entry (code);
2535 g_assert (plt_entry);
2537 /* The offset is embedded inside the code after the plt entry */
2538 #if defined(__i386__)
2539 return *(guint32*)(plt_entry + 5);
2540 #elif defined(__x86_64__)
2541 return *(guint32*)(plt_entry + 6);
2542 #elif defined(__arm__)
2543 /* The offset is stored as the 4th word of the plt entry */
2544 return ((guint32*)plt_entry) [3];
2545 #elif defined(__mono_ppc__)
2546 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2547 return ((guint32*)plt_entry) [8];
2548 #else
2549 return ((guint32*)plt_entry) [6];
2550 #endif
2551 #else
2552 g_assert_not_reached ();
2553 return 0;
2554 #endif
2557 static gpointer
2558 mono_create_ftnptr_malloc (guint8 *code)
2560 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2561 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
2563 ftnptr->code = code;
2564 ftnptr->toc = NULL;
2565 ftnptr->env = NULL;
2567 return ftnptr;
2568 #else
2569 return code;
2570 #endif
2574 * load_function:
2576 * Load the function named NAME from the aot image.
2578 static gpointer
2579 load_function (MonoAotModule *amodule, const char *name)
2581 char *symbol;
2582 guint8 *p;
2583 int n_patches, pindex;
2584 MonoMemPool *mp;
2585 gpointer code;
2587 /* Load the code */
2589 symbol = g_strdup_printf ("%s", name);
2590 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2591 g_free (symbol);
2592 if (!code)
2593 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
2595 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2597 /* Load info */
2599 symbol = g_strdup_printf ("%s_p", name);
2600 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2601 g_free (symbol);
2602 if (!p)
2603 /* Nothing to patch */
2604 return code;
2606 /* Similar to mono_aot_load_method () */
2608 n_patches = decode_value (p, &p);
2610 if (n_patches) {
2611 MonoJumpInfo *patches;
2612 guint32 *got_slots;
2614 mp = mono_mempool_new ();
2616 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2617 g_assert (patches);
2619 for (pindex = 0; pindex < n_patches; ++pindex) {
2620 MonoJumpInfo *ji = &patches [pindex];
2621 gpointer target;
2623 if (amodule->got [got_slots [pindex]])
2624 continue;
2627 * When this code is executed, the runtime may not be initalized yet, so
2628 * resolve the patch info by hand.
2630 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2631 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2632 target = mono_get_lmf_addr;
2633 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2634 target = mono_thread_force_interruption_checkpoint;
2635 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2636 target = mono_exception_from_token;
2637 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2638 target = mono_get_throw_exception ();
2639 #ifdef __x86_64__
2640 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2641 target = mono_amd64_throw_exception;
2642 #endif
2643 #ifdef __x86_64__
2644 } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
2645 target = mono_amd64_get_original_ip;
2646 #endif
2647 #ifdef __arm__
2648 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2649 target = mono_arm_throw_exception;
2650 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
2651 target = mono_arm_throw_exception_by_token;
2652 #endif
2653 #ifdef __mono_ppc__
2654 } else if (!strcmp (ji->data.name, "mono_ppc_throw_exception")) {
2655 target = mono_ppc_throw_exception;
2656 #endif
2657 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2658 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2659 target = (gpointer)mono_get_trampoline_func (tramp_type2);
2660 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
2661 /* atoll is needed because the the offset is unsigned */
2662 guint32 slot;
2663 int res;
2665 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
2666 g_assert (res == 1);
2667 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
2668 target = mono_create_ftnptr_malloc (target);
2669 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
2670 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
2671 target = mono_create_ftnptr_malloc (target);
2672 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
2673 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
2674 target = mono_create_ftnptr_malloc (target);
2675 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
2676 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
2677 target = mono_create_ftnptr_malloc (target);
2678 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
2679 target = mono_thread_get_and_clear_pending_exception;
2680 } else {
2681 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
2682 g_assert_not_reached ();
2683 target = NULL;
2685 } else {
2686 /* Hopefully the code doesn't have patches which need method or
2687 * domain to be set.
2689 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2690 g_assert (target);
2693 amodule->got [got_slots [pindex]] = target;
2696 g_free (got_slots);
2698 mono_mempool_destroy (mp);
2701 return code;
2705 * Return the piece of code identified by NAME from the mscorlib AOT file.
2706 * On ppc64, this returns a function descriptor.
2708 gpointer
2709 mono_aot_get_named_code (const char *name)
2711 MonoImage *image;
2712 MonoAotModule *amodule;
2714 image = mono_defaults.corlib;
2715 g_assert (image);
2717 amodule = image->aot_module;
2718 g_assert (amodule);
2720 return mono_create_ftnptr_malloc (load_function (amodule, name));
2723 /* Return a given kind of trampoline */
2724 static gpointer
2725 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
2727 MonoAotModule *amodule;
2728 int index, tramp_size;
2729 MonoImage *image;
2731 /* Currently, we keep all trampolines in the mscorlib AOT image */
2732 image = mono_defaults.corlib;
2733 g_assert (image);
2735 mono_aot_lock ();
2737 amodule = image->aot_module;
2738 g_assert (amodule);
2740 *out_amodule = amodule;
2742 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
2743 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
2745 index = amodule->trampoline_index [tramp_type] ++;
2747 mono_aot_unlock ();
2749 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
2751 tramp_size = amodule->info.trampoline_size [tramp_type];
2753 if (out_tramp_size)
2754 *out_tramp_size = tramp_size;
2756 return amodule->trampolines [tramp_type] + (index * tramp_size);
2760 * Return a specific trampoline from the AOT file.
2762 gpointer
2763 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2765 MonoAotModule *amodule;
2766 guint32 got_offset, tramp_size;
2767 guint8 *code, *tramp;
2768 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2769 static gboolean inited;
2770 static guint32 num_trampolines;
2772 if (!inited) {
2773 mono_aot_lock ();
2775 if (!inited) {
2776 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
2777 inited = TRUE;
2780 mono_aot_unlock ();
2783 num_trampolines ++;
2785 if (!generic_trampolines [tramp_type]) {
2786 char *symbol;
2788 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2789 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2790 g_free (symbol);
2793 tramp = generic_trampolines [tramp_type];
2794 g_assert (tramp);
2796 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
2798 amodule->got [got_offset] = tramp;
2799 amodule->got [got_offset + 1] = arg1;
2801 if (code_len)
2802 *code_len = tramp_size;
2804 return code;
2807 gpointer
2808 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
2810 MonoAotModule *amodule;
2811 guint8 *code;
2812 guint32 got_offset;
2814 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
2816 amodule->got [got_offset] = ctx;
2817 amodule->got [got_offset + 1] = addr;
2819 /* The caller expects an ftnptr */
2820 return mono_create_ftnptr (mono_domain_get (), code);
2823 gpointer
2824 mono_aot_get_unbox_trampoline (MonoMethod *method)
2826 guint32 method_index = mono_metadata_token_index (method->token) - 1;
2827 MonoAotModule *amodule;
2828 char *symbol;
2829 gpointer code;
2831 if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
2832 guint32 index = find_extra_method (method, &amodule);
2833 g_assert (index != 0xffffff);
2835 symbol = g_strdup_printf ("ut_e_%d", index);
2836 } else {
2837 amodule = method->klass->image->aot_module;
2838 g_assert (amodule);
2840 symbol = g_strdup_printf ("ut_%d", method_index);
2842 code = load_function (amodule, symbol);
2843 g_free (symbol);
2845 /* The caller expects an ftnptr */
2846 return mono_create_ftnptr (mono_domain_get (), code);
2849 gpointer
2850 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2852 char *symbol;
2853 gpointer code;
2855 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
2856 code = load_function (mono_defaults.corlib->aot_module, symbol);
2857 g_free (symbol);
2858 return code;
2861 gpointer
2862 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
2864 guint32 got_offset;
2865 gpointer code;
2866 gpointer *buf;
2867 int i;
2868 MonoAotModule *amodule;
2870 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
2872 /* Save the entries into an array */
2873 buf = mono_domain_alloc (domain, (count + 1) * 2 * sizeof (gpointer));
2874 for (i = 0; i < count; ++i) {
2875 MonoIMTCheckItem *item = imt_entries [i];
2877 g_assert (item->key);
2878 /* FIXME: */
2879 g_assert (!item->has_target_code);
2881 buf [(i * 2)] = item->key;
2882 buf [(i * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
2884 buf [(count * 2)] = NULL;
2885 buf [(count * 2) + 1] = fail_tramp;
2887 amodule->got [got_offset] = buf;
2889 return code;
2892 #else
2893 /* AOT disabled */
2895 void
2896 mono_aot_init (void)
2900 gpointer
2901 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2903 return NULL;
2906 gboolean
2907 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2909 return FALSE;
2912 gboolean
2913 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
2915 return FALSE;
2918 gboolean
2919 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
2921 return FALSE;
2924 MonoJitInfo *
2925 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
2927 return NULL;
2930 gpointer
2931 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2933 return NULL;
2936 guint8*
2937 mono_aot_get_plt_entry (guint8 *code)
2939 return NULL;
2942 gpointer
2943 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2945 return NULL;
2948 gpointer
2949 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
2951 return NULL;
2954 guint32
2955 mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
2957 g_assert_not_reached ();
2959 return 0;
2962 gpointer
2963 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2965 g_assert_not_reached ();
2966 return NULL;
2969 gpointer
2970 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
2972 g_assert_not_reached ();
2973 return NULL;
2976 gpointer
2977 mono_aot_get_named_code (const char *name)
2979 g_assert_not_reached ();
2980 return NULL;
2983 gpointer
2984 mono_aot_get_unbox_trampoline (MonoMethod *method)
2986 g_assert_not_reached ();
2987 return NULL;
2990 gpointer
2991 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2993 g_assert_not_reached ();
2994 return NULL;
2997 gpointer
2998 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3000 g_assert_not_reached ();
3001 return NULL;
3004 guint8*
3005 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3007 g_assert_not_reached ();
3008 return NULL;
3011 #endif