In mono/metadata:
[mono.git] / mono / mini / aot-runtime.c
blob9d9e3e8697b6a8b39ea8b135374622a1da3f1f08
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 * Maps MonoJitInfo* to the aot module they belong to, this can be different
138 * from ji->method->klass->image's aot module for generic instances.
140 static GHashTable *ji_to_amodule;
143 * Whenever to AOT compile loaded assemblies on demand and store them in
144 * a cache under $HOME/.mono/aot-cache.
146 static gboolean use_aot_cache = FALSE;
149 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
150 * use_aot_cache is TRUE.
152 static gboolean spawn_compiler = TRUE;
154 /* For debugging */
155 static gint32 mono_last_aot_method = -1;
157 static gboolean make_unreadable = FALSE;
158 static guint32 name_table_accesses = 0;
160 /* Used to speed-up find_aot_module () */
161 static gsize aot_code_low_addr = (gssize)-1;
162 static gsize aot_code_high_addr = 0;
164 static void
165 init_plt (MonoAotModule *info);
167 /*****************************************************/
168 /* AOT RUNTIME */
169 /*****************************************************/
171 static MonoImage *
172 load_image (MonoAotModule *module, int index)
174 MonoAssembly *assembly;
175 MonoImageOpenStatus status;
177 g_assert (index < module->image_table_len);
179 if (module->image_table [index])
180 return module->image_table [index];
181 if (module->out_of_date)
182 return NULL;
184 assembly = mono_assembly_load (&module->image_names [index], NULL, &status);
185 if (!assembly) {
186 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);
187 module->out_of_date = TRUE;
188 return NULL;
191 if (strcmp (assembly->image->guid, module->image_guids [index])) {
192 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);
193 module->out_of_date = TRUE;
194 return NULL;
197 module->image_table [index] = assembly->image;
198 return assembly->image;
202 static inline gint32
203 decode_value (guint8 *ptr, guint8 **rptr)
205 guint8 b = *ptr;
206 gint32 len;
208 if ((b & 0x80) == 0){
209 len = b;
210 ++ptr;
211 } else if ((b & 0x40) == 0){
212 len = ((b & 0x3f) << 8 | ptr [1]);
213 ptr += 2;
214 } else if (b != 0xff) {
215 len = ((b & 0x1f) << 24) |
216 (ptr [1] << 16) |
217 (ptr [2] << 8) |
218 ptr [3];
219 ptr += 4;
221 else {
222 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
223 ptr += 5;
225 if (rptr)
226 *rptr = ptr;
228 //printf ("DECODE: %d.\n", len);
229 return len;
232 static MonoMethod*
233 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
235 static MonoClass*
236 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
238 static MonoGenericInst*
239 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
241 int type_argc, i;
242 MonoType **type_argv;
243 MonoGenericInst *inst;
244 guint8 *p = buf;
246 type_argc = decode_value (p, &p);
247 type_argv = g_new0 (MonoType*, type_argc);
249 for (i = 0; i < type_argc; ++i) {
250 MonoClass *pclass = decode_klass_ref (module, p, &p);
251 if (!pclass) {
252 g_free (type_argv);
253 return NULL;
255 type_argv [i] = &pclass->byval_arg;
258 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
259 g_free (type_argv);
261 *endbuf = p;
263 return inst;
266 static gboolean
267 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
269 gboolean has_class_inst, has_method_inst;
270 guint8 *p = buf;
272 has_class_inst = decode_value (p, &p);
273 if (has_class_inst) {
274 ctx->class_inst = decode_generic_inst (module, p, &p);
275 if (!ctx->class_inst)
276 return FALSE;
278 has_method_inst = decode_value (p, &p);
279 if (has_method_inst) {
280 ctx->method_inst = decode_generic_inst (module, p, &p);
281 if (!ctx->method_inst)
282 return FALSE;
285 *endbuf = p;
286 return TRUE;
289 static MonoClass*
290 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
292 MonoImage *image;
293 MonoClass *klass, *eklass;
294 guint32 token, rank;
295 guint8 *p = buf;
297 token = decode_value (p, &p);
298 if (token == 0) {
299 *endbuf = p;
300 return NULL;
302 if (mono_metadata_token_table (token) == 0) {
303 image = load_image (module, decode_value (p, &p));
304 if (!image)
305 return NULL;
306 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
307 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
308 if (token == MONO_TOKEN_TYPE_SPEC) {
309 MonoTypeEnum type = decode_value (p, &p);
311 if (type == MONO_TYPE_GENERICINST) {
312 MonoClass *gclass;
313 MonoGenericContext ctx;
314 MonoType *type;
316 gclass = decode_klass_ref (module, p, &p);
317 g_assert (gclass->generic_container);
319 memset (&ctx, 0, sizeof (ctx));
320 ctx.class_inst = decode_generic_inst (module, p, &p);
321 if (!ctx.class_inst)
322 return NULL;
323 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
324 klass = mono_class_from_mono_type (type);
325 mono_metadata_free_type (type);
326 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
327 MonoType *t;
328 MonoGenericContainer *container;
330 int num = decode_value (p, &p);
331 gboolean is_method = decode_value (p, &p);
333 if (is_method) {
334 MonoMethod *method_def;
335 g_assert (type == MONO_TYPE_MVAR);
336 method_def = decode_method_ref_2 (module, p, &p);
337 if (!method_def)
338 return NULL;
340 container = mono_method_get_generic_container (method_def);
341 } else {
342 MonoClass *class_def;
343 g_assert (type == MONO_TYPE_VAR);
344 class_def = decode_klass_ref (module, p, &p);
345 if (!class_def)
346 return NULL;
348 container = class_def->generic_container;
351 g_assert (container);
353 // FIXME: Memory management
354 t = g_new0 (MonoType, 1);
355 t->type = type;
356 t->data.generic_param = mono_generic_container_get_param (container, num);
358 // FIXME: Maybe use types directly to avoid
359 // the overhead of creating MonoClass-es
360 klass = mono_class_from_mono_type (t);
362 g_free (t);
363 } else {
364 g_assert_not_reached ();
366 } else {
367 image = load_image (module, decode_value (p, &p));
368 if (!image)
369 return NULL;
370 klass = mono_class_get (image, token);
372 } else if (token == MONO_TOKEN_TYPE_DEF) {
373 /* Array */
374 image = load_image (module, decode_value (p, &p));
375 if (!image)
376 return NULL;
377 rank = decode_value (p, &p);
378 eklass = decode_klass_ref (module, p, &p);
379 klass = mono_array_class_get (eklass, rank);
380 } else {
381 g_assert_not_reached ();
383 g_assert (klass);
384 mono_class_init (klass);
386 *endbuf = p;
387 return klass;
390 static MonoClassField*
391 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
393 MonoClass *klass = decode_klass_ref (module, buf, &buf);
394 guint32 token;
395 guint8 *p = buf;
397 if (!klass)
398 return NULL;
400 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
402 *endbuf = p;
404 return mono_class_get_field (klass, token);
408 * can_method_ref_match_method:
410 * Determine if calling decode_method_ref_2 on P could return the same method as
411 * METHOD. This is an optimization to avoid calling decode_method_ref_2 () which
412 * would create MonoMethods which are not needed etc.
414 static gboolean
415 can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
417 guint8 *p = buf;
418 guint32 image_index, value;
420 /* Keep this in sync with decode_method_ref () */
421 value = decode_value (p, &p);
422 image_index = value >> 24;
424 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
425 guint32 wrapper_type;
427 if (!method->wrapper_type)
428 return FALSE;
430 wrapper_type = decode_value (p, &p);
432 if (method->wrapper_type != wrapper_type)
433 return FALSE;
434 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
435 return FALSE;
436 } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
437 if (method->wrapper_type)
438 return FALSE;
441 return TRUE;
445 * decode_method_ref:
447 * Decode a method reference, and return its image and token. This avoids loading
448 * metadata for the method if the caller does not need it. If the method has no token,
449 * then it is loaded from metadata and METHOD is set to the method instance.
451 static MonoImage*
452 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
454 guint32 image_index, value;
455 MonoImage *image = NULL;
456 guint8 *p = buf;
458 if (method)
459 *method = NULL;
460 if (no_aot_trampoline)
461 *no_aot_trampoline = FALSE;
463 value = decode_value (p, &p);
464 image_index = value >> 24;
466 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
467 if (no_aot_trampoline)
468 *no_aot_trampoline = TRUE;
469 value = decode_value (p, &p);
470 image_index = value >> 24;
473 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
474 guint32 wrapper_type;
476 wrapper_type = decode_value (p, &p);
478 /* Doesn't matter */
479 image = mono_defaults.corlib;
481 switch (wrapper_type) {
482 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
483 MonoMethod *m = decode_method_ref_2 (module, p, &p);
485 if (!m)
486 return NULL;
487 mono_class_init (m->klass);
488 *method = mono_marshal_get_remoting_invoke_with_check (m);
489 break;
491 case MONO_WRAPPER_PROXY_ISINST: {
492 MonoClass *klass = decode_klass_ref (module, p, &p);
493 if (!klass)
494 return NULL;
495 *method = mono_marshal_get_proxy_cancast (klass);
496 break;
498 case MONO_WRAPPER_LDFLD:
499 case MONO_WRAPPER_LDFLDA:
500 case MONO_WRAPPER_STFLD:
501 case MONO_WRAPPER_ISINST: {
502 MonoClass *klass = decode_klass_ref (module, p, &p);
503 if (!klass)
504 return NULL;
505 if (wrapper_type == MONO_WRAPPER_LDFLD)
506 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
507 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
508 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
509 else if (wrapper_type == MONO_WRAPPER_STFLD)
510 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
511 else if (wrapper_type == MONO_WRAPPER_ISINST)
512 *method = mono_marshal_get_isinst (klass);
513 else
514 g_assert_not_reached ();
515 break;
517 case MONO_WRAPPER_LDFLD_REMOTE:
518 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
519 break;
520 case MONO_WRAPPER_STFLD_REMOTE:
521 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
522 break;
523 case MONO_WRAPPER_ALLOC: {
524 int atype = decode_value (p, &p);
526 *method = mono_gc_get_managed_allocator_by_type (atype);
527 break;
529 case MONO_WRAPPER_STELEMREF:
530 *method = mono_marshal_get_stelemref ();
531 break;
532 case MONO_WRAPPER_SYNCHRONIZED: {
533 MonoMethod *m = decode_method_ref_2 (module, p, &p);
535 if (!m)
536 return NULL;
537 *method = mono_marshal_get_synchronized_wrapper (m);
538 break;
540 case MONO_WRAPPER_UNKNOWN: {
541 MonoMethodDesc *desc;
542 MonoMethod *orig_method;
543 int subtype = decode_value (p, &p);
545 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
546 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
547 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
548 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
549 else
550 g_assert_not_reached ();
551 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
552 g_assert (orig_method);
553 mono_method_desc_free (desc);
554 *method = mono_monitor_get_fast_path (orig_method);
555 break;
557 case MONO_WRAPPER_RUNTIME_INVOKE: {
558 /* Direct wrapper */
559 MonoMethod *m = decode_method_ref_2 (module, p, &p);
561 if (!m)
562 return NULL;
563 *method = mono_marshal_get_runtime_invoke (m, FALSE);
564 break;
566 default:
567 g_assert_not_reached ();
569 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
570 /* Can't decode these */
571 g_assert_not_reached ();
572 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
573 image_index = decode_value (p, &p);
574 *token = decode_value (p, &p);
576 image = load_image (module, image_index);
577 if (!image)
578 return NULL;
579 } else if (image_index == MONO_AOT_METHODREF_GINST) {
580 MonoClass *klass;
581 MonoGenericContext ctx;
584 * These methods do not have a token which resolves them, so we
585 * resolve them immediately.
587 klass = decode_klass_ref (module, p, &p);
588 if (!klass)
589 return NULL;
591 image_index = decode_value (p, &p);
592 *token = decode_value (p, &p);
594 image = load_image (module, image_index);
595 if (!image)
596 return NULL;
598 *method = mono_get_method_full (image, *token, NULL, NULL);
599 if (!(*method))
600 return NULL;
602 memset (&ctx, 0, sizeof (ctx));
604 if (FALSE && klass->generic_class) {
605 ctx.class_inst = klass->generic_class->context.class_inst;
606 ctx.method_inst = NULL;
608 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
611 memset (&ctx, 0, sizeof (ctx));
613 if (!decode_generic_context (module, &ctx, p, &p))
614 return NULL;
616 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
617 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
618 MonoClass *klass;
619 int method_type;
621 klass = decode_klass_ref (module, p, &p);
622 if (!klass)
623 return NULL;
624 method_type = decode_value (p, &p);
625 *token = 0;
626 switch (method_type) {
627 case 0:
628 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
629 break;
630 case 1:
631 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
632 break;
633 case 2:
634 *method = mono_class_get_method_from_name (klass, "Get", -1);
635 break;
636 case 3:
637 *method = mono_class_get_method_from_name (klass, "Address", -1);
638 break;
639 case 4:
640 *method = mono_class_get_method_from_name (klass, "Set", -1);
641 break;
642 default:
643 g_assert_not_reached ();
645 } else {
646 g_assert (image_index < MONO_AOT_METHODREF_MIN);
647 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
649 image = load_image (module, image_index);
650 if (!image)
651 return NULL;
654 *endbuf = p;
656 return image;
660 * decode_method_ref_2:
662 * Similar to decode_method_ref, but resolve and return the method itself.
664 static MonoMethod*
665 decode_method_ref_2 (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
667 MonoMethod *method;
668 guint32 token;
669 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
671 if (method)
672 return method;
673 if (!image)
674 return NULL;
675 method = mono_get_method (image, token, NULL);
676 return method;
679 G_GNUC_UNUSED
680 static void
681 make_writable (guint8* addr, guint32 len)
683 guint8 *page_start;
684 int pages, err;
686 if (mono_aot_only)
687 g_error ("Attempt to make AOT memory writable while running in aot-only mode.\n");
689 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1));
690 pages = (addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ();
692 err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_READ | MONO_MMAP_WRITE | MONO_MMAP_EXEC);
693 g_assert (err == 0);
696 static void
697 create_cache_structure (void)
699 const char *home;
700 char *tmp;
701 int err;
703 home = g_get_home_dir ();
704 if (!home)
705 return;
707 tmp = g_build_filename (home, ".mono", NULL);
708 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
709 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
710 #ifdef PLATFORM_WIN32
711 err = mkdir (tmp);
712 #else
713 err = mkdir (tmp, 0777);
714 #endif
715 if (err) {
716 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
717 g_free (tmp);
718 return;
721 g_free (tmp);
722 tmp = g_build_filename (home, ".mono", "aot-cache", 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
726 err = mkdir (tmp);
727 #else
728 err = mkdir (tmp, 0777);
729 #endif
730 if (err) {
731 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
732 g_free (tmp);
733 return;
736 g_free (tmp);
740 * load_aot_module_from_cache:
742 * Experimental code to AOT compile loaded assemblies on demand.
744 * FIXME:
745 * - Add environment variable MONO_AOT_CACHE_OPTIONS
746 * - Add options for controlling the cache size
747 * - Handle full cache by deleting old assemblies lru style
748 * - Add options for excluding assemblies during development
749 * - Maybe add a threshold after an assembly is AOT compiled
750 * - invoking a new mono process is a security risk
751 * - recompile the AOT module if one of its dependencies changes
753 static MonoDl*
754 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
756 char *fname, *cmd, *tmp2, *aot_options;
757 const char *home;
758 MonoDl *module;
759 gboolean res;
760 gchar *out, *err;
761 gint exit_status;
763 *aot_name = NULL;
765 if (assembly->image->dynamic)
766 return NULL;
768 create_cache_structure ();
770 home = g_get_home_dir ();
772 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
773 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
774 *aot_name = fname;
775 g_free (tmp2);
777 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
778 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
780 if (!module) {
781 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
783 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
785 aot_options = g_strdup_printf ("outfile=%s", fname);
787 if (spawn_compiler) {
788 /* FIXME: security */
789 /* FIXME: Has to pass the assembly loading path to the child process */
790 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
792 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
794 #if !defined(PLATFORM_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
795 if (res) {
796 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
797 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
798 else
799 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
800 g_free (out);
801 g_free (err);
803 #endif
804 g_free (cmd);
805 } else {
806 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
807 if (!res) {
808 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
809 } else {
810 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
814 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
816 g_free (aot_options);
819 return module;
822 static void
823 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
825 if (globals) {
826 int i = 0;
828 *value = NULL;
829 for (i = 0; globals [i]; i+= 2) {
830 if (strcmp (globals [i], name) == 0) {
831 *value = globals [i + 1];
832 break;
835 } else {
836 mono_dl_symbol (module, name, value);
840 static void
841 load_aot_module (MonoAssembly *assembly, gpointer user_data)
843 char *aot_name;
844 MonoAotModule *amodule;
845 MonoDl *sofile;
846 gboolean usable = TRUE;
847 char *saved_guid = NULL;
848 char *aot_version = NULL;
849 char *runtime_version, *build_info;
850 char *opt_flags = NULL;
851 gpointer *globals;
852 gboolean full_aot = FALSE;
853 MonoAotFileInfo *file_info = NULL;
854 int i;
855 gpointer *got_addr;
857 if (mono_compile_aot)
858 return;
860 if (assembly->image->aot_module)
862 * Already loaded. This can happen because the assembly loading code might invoke
863 * the assembly load hooks multiple times for the same assembly.
865 return;
867 if (assembly->image->dynamic)
868 return;
870 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
871 return;
873 mono_aot_lock ();
874 if (static_aot_modules)
875 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
876 else
877 globals = NULL;
878 mono_aot_unlock ();
880 if (globals) {
881 /* Statically linked AOT module */
882 sofile = NULL;
883 aot_name = g_strdup_printf ("%s", assembly->aname.name);
884 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
885 } else {
886 if (use_aot_cache)
887 sofile = load_aot_module_from_cache (assembly, &aot_name);
888 else {
889 char *err;
890 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
892 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
894 if (!sofile) {
895 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
896 g_free (err);
901 if (!sofile && !globals) {
902 if (mono_aot_only) {
903 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
904 exit (1);
906 g_free (aot_name);
907 return;
910 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
911 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
912 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
913 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
914 find_symbol (sofile, globals, "mono_aot_got_addr", (gpointer *)&got_addr);
916 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
917 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);
918 usable = FALSE;
920 else {
921 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
922 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
923 usable = FALSE;
927 build_info = mono_get_runtime_build_info ();
928 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
929 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);
930 usable = FALSE;
932 g_free (build_info);
935 char *full_aot_str;
937 find_symbol (sofile, globals, "mono_aot_full_aot", (gpointer *)&full_aot_str);
939 if (full_aot_str && !strcmp (full_aot_str, "TRUE"))
940 full_aot = TRUE;
943 if (mono_aot_only && !full_aot) {
944 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
945 exit (1);
947 if (!mono_aot_only && full_aot) {
948 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
949 usable = FALSE;
952 if (!usable) {
953 if (mono_aot_only) {
954 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
955 exit (1);
957 g_free (aot_name);
958 if (sofile)
959 mono_dl_close (sofile);
960 assembly->image->aot_module = NULL;
961 return;
964 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
965 g_assert (file_info);
967 amodule = g_new0 (MonoAotModule, 1);
968 amodule->aot_name = aot_name;
969 amodule->assembly = assembly;
971 memcpy (&amodule->info, file_info, sizeof (*file_info));
973 amodule->got = *got_addr;
974 amodule->got [0] = assembly->image;
975 amodule->globals = globals;
976 amodule->sofile = sofile;
977 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
979 sscanf (opt_flags, "%d", &amodule->opts);
981 /* Read image table */
983 guint32 table_len, i;
984 char *table = NULL;
986 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
987 g_assert (table);
989 table_len = *(guint32*)table;
990 table += sizeof (guint32);
991 amodule->image_table = g_new0 (MonoImage*, table_len);
992 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
993 amodule->image_guids = g_new0 (char*, table_len);
994 amodule->image_table_len = table_len;
995 for (i = 0; i < table_len; ++i) {
996 MonoAssemblyName *aname = &(amodule->image_names [i]);
998 aname->name = g_strdup (table);
999 table += strlen (table) + 1;
1000 amodule->image_guids [i] = g_strdup (table);
1001 table += strlen (table) + 1;
1002 if (table [0] != 0)
1003 aname->culture = g_strdup (table);
1004 table += strlen (table) + 1;
1005 memcpy (aname->public_key_token, table, strlen (table) + 1);
1006 table += strlen (table) + 1;
1008 table = ALIGN_PTR_TO (table, 8);
1009 aname->flags = *(guint32*)table;
1010 table += 4;
1011 aname->major = *(guint32*)table;
1012 table += 4;
1013 aname->minor = *(guint32*)table;
1014 table += 4;
1015 aname->build = *(guint32*)table;
1016 table += 4;
1017 aname->revision = *(guint32*)table;
1018 table += 4;
1022 /* Read method and method_info tables */
1023 find_symbol (sofile, globals, "method_offsets", (gpointer*)&amodule->code_offsets);
1024 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
1025 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
1026 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
1027 find_symbol (sofile, globals, "method_info", (gpointer*)&amodule->method_info);
1028 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
1029 find_symbol (sofile, globals, "ex_info", (gpointer*)&amodule->ex_info);
1030 find_symbol (sofile, globals, "method_order", (gpointer*)&amodule->method_order);
1031 find_symbol (sofile, globals, "method_order_end", (gpointer*)&amodule->method_order_end);
1032 find_symbol (sofile, globals, "class_info", (gpointer*)&amodule->class_info);
1033 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
1034 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
1035 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
1036 find_symbol (sofile, globals, "extra_method_info", (gpointer *)&amodule->extra_method_info);
1037 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1038 find_symbol (sofile, globals, "got_info", (gpointer*)&amodule->got_info);
1039 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1040 find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC]));
1041 find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX]));
1042 find_symbol (sofile, globals, "imt_thunks", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK]));
1043 find_symbol (sofile, globals, "unwind_info", (gpointer)&amodule->unwind_info);
1044 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1046 amodule->mem_begin = amodule->code;
1048 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1049 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1051 if (make_unreadable) {
1052 #ifndef PLATFORM_WIN32
1053 guint8 *addr;
1054 guint8 *page_start;
1055 int pages, err, len;
1057 addr = amodule->mem_begin;
1058 len = amodule->mem_end - amodule->mem_begin;
1060 /* Round down in both directions to avoid modifying data which is not ours */
1061 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1062 pages = ((addr + len - page_start + mono_pagesize () - 1) / mono_pagesize ()) - 1;
1063 err = mono_mprotect (page_start, pages * mono_pagesize (), MONO_MMAP_NONE);
1064 g_assert (err == 0);
1065 #endif
1068 mono_aot_lock ();
1070 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1071 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1073 g_hash_table_insert (aot_modules, assembly, amodule);
1074 mono_aot_unlock ();
1076 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1078 assembly->image->aot_module = amodule;
1080 if (mono_aot_only) {
1081 if (mono_defaults.corlib) {
1082 /* The second got slot contains the mscorlib got addr */
1083 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1085 amodule->got [1] = mscorlib_amodule->got;
1086 } else {
1087 amodule->got [1] = amodule->got;
1092 * Since we store methoddef and classdef tokens when referring to methods/classes in
1093 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1094 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1095 * non-lazily, since we can't handle out-of-date errors later.
1097 for (i = 0; i < amodule->image_table_len; ++i)
1098 load_image (amodule, i);
1100 if (amodule->out_of_date) {
1101 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);
1102 if (mono_aot_only) {
1103 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);
1104 exit (1);
1107 else
1108 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1112 * mono_aot_register_globals:
1114 * This is called by the ctor function in AOT images compiled with the
1115 * 'no-dlsym' option.
1117 void
1118 mono_aot_register_globals (gpointer *globals)
1120 g_assert_not_reached ();
1124 * mono_aot_register_module:
1126 * This should be called by embedding code to register AOT modules statically linked
1127 * into the executable. AOT_INFO should be the value of the
1128 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1130 void
1131 mono_aot_register_module (gpointer *aot_info)
1133 gpointer *globals;
1134 char *aname;
1136 globals = aot_info;
1137 g_assert (globals);
1139 /* Determine the assembly name */
1140 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1141 g_assert (aname);
1143 /* This could be called before startup */
1144 if (aot_modules)
1145 mono_aot_lock ();
1147 if (!static_aot_modules)
1148 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1150 g_hash_table_insert (static_aot_modules, aname, globals);
1152 if (aot_modules)
1153 mono_aot_unlock ();
1156 void
1157 mono_aot_init (void)
1159 InitializeCriticalSection (&aot_mutex);
1160 aot_modules = g_hash_table_new (NULL, NULL);
1162 mono_install_assembly_load_hook (load_aot_module, NULL);
1164 if (g_getenv ("MONO_LASTAOT"))
1165 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1166 if (g_getenv ("MONO_AOT_CACHE"))
1167 use_aot_cache = TRUE;
1170 static gboolean
1171 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1173 guint32 flags;
1175 info->vtable_size = decode_value (buf, &buf);
1176 if (info->vtable_size == -1)
1177 /* Generic type */
1178 return FALSE;
1179 flags = decode_value (buf, &buf);
1180 info->ghcimpl = (flags >> 0) & 0x1;
1181 info->has_finalize = (flags >> 1) & 0x1;
1182 info->has_cctor = (flags >> 2) & 0x1;
1183 info->has_nested_classes = (flags >> 3) & 0x1;
1184 info->blittable = (flags >> 4) & 0x1;
1185 info->has_references = (flags >> 5) & 0x1;
1186 info->has_static_refs = (flags >> 6) & 0x1;
1187 info->no_special_static_fields = (flags >> 7) & 0x1;
1189 if (info->has_cctor) {
1190 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1191 if (!cctor_image)
1192 return FALSE;
1194 if (info->has_finalize) {
1195 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1196 if (!info->finalize_image)
1197 return FALSE;
1200 info->instance_size = decode_value (buf, &buf);
1201 info->class_size = decode_value (buf, &buf);
1202 info->packing_size = decode_value (buf, &buf);
1203 info->min_align = decode_value (buf, &buf);
1205 *endbuf = buf;
1207 return TRUE;
1210 gpointer
1211 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1213 int i;
1214 MonoClass *klass = vtable->klass;
1215 MonoAotModule *aot_module = klass->image->aot_module;
1216 guint8 *info, *p;
1217 MonoCachedClassInfo class_info;
1218 gboolean err;
1219 guint32 token;
1220 MonoImage *image;
1221 gboolean no_aot_trampoline;
1223 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !aot_module)
1224 return NULL;
1226 info = &aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1227 p = info;
1229 err = decode_cached_class_info (aot_module, &class_info, p, &p);
1230 if (!err)
1231 return NULL;
1233 for (i = 0; i < slot; ++i)
1234 decode_method_ref (aot_module, &token, NULL, NULL, p, &p);
1236 image = decode_method_ref (aot_module, &token, NULL, &no_aot_trampoline, p, &p);
1237 if (!image)
1238 return NULL;
1239 if (no_aot_trampoline)
1240 return NULL;
1242 if (mono_metadata_token_index (token) == 0)
1243 return NULL;
1245 return mono_aot_get_method_from_token (domain, image, token);
1248 gboolean
1249 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1251 MonoAotModule *aot_module = klass->image->aot_module;
1252 guint8 *p;
1253 gboolean err;
1255 if (klass->rank || !aot_module)
1256 return FALSE;
1258 p = (guint8*)&aot_module->class_info [aot_module->class_info_offsets [mono_metadata_token_index (klass->type_token) - 1]];
1260 err = decode_cached_class_info (aot_module, res, p, &p);
1261 if (!err)
1262 return FALSE;
1264 return TRUE;
1268 * mono_aot_get_class_from_name:
1270 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1271 * using a cache stored in the AOT file.
1272 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1274 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1275 * found.
1277 gboolean
1278 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1280 MonoAotModule *aot_module = image->aot_module;
1281 guint16 *table, *entry;
1282 guint16 table_size;
1283 guint32 hash;
1284 char full_name_buf [1024];
1285 char *full_name;
1286 const char *name2, *name_space2;
1287 MonoTableInfo *t;
1288 guint32 cols [MONO_TYPEDEF_SIZE];
1289 GHashTable *nspace_table;
1291 if (!aot_module || !aot_module->class_name_table)
1292 return FALSE;
1294 mono_aot_lock ();
1296 *klass = NULL;
1298 /* First look in the cache */
1299 if (!aot_module->name_cache)
1300 aot_module->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1301 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1302 if (nspace_table) {
1303 *klass = g_hash_table_lookup (nspace_table, name);
1304 if (*klass) {
1305 mono_aot_unlock ();
1306 return TRUE;
1310 table_size = aot_module->class_name_table [0];
1311 table = aot_module->class_name_table + 1;
1313 if (name_space [0] == '\0')
1314 full_name = g_strdup_printf ("%s", name);
1315 else {
1316 if (strlen (name_space) + strlen (name) < 1000) {
1317 sprintf (full_name_buf, "%s.%s", name_space, name);
1318 full_name = full_name_buf;
1319 } else {
1320 full_name = g_strdup_printf ("%s.%s", name_space, name);
1323 hash = mono_aot_str_hash (full_name) % table_size;
1324 if (full_name != full_name_buf)
1325 g_free (full_name);
1327 entry = &table [hash * 2];
1329 if (entry [0] != 0) {
1330 t = &image->tables [MONO_TABLE_TYPEDEF];
1332 while (TRUE) {
1333 guint32 index = entry [0];
1334 guint32 next = entry [1];
1335 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1337 name_table_accesses ++;
1339 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1341 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1342 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1344 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1345 mono_aot_unlock ();
1346 *klass = mono_class_get (image, token);
1348 /* Add to cache */
1349 if (*klass) {
1350 mono_aot_lock ();
1351 nspace_table = g_hash_table_lookup (aot_module->name_cache, name_space);
1352 if (!nspace_table) {
1353 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1354 g_hash_table_insert (aot_module->name_cache, (char*)name_space2, nspace_table);
1356 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1357 mono_aot_unlock ();
1359 return TRUE;
1362 if (next != 0) {
1363 entry = &table [next * 2];
1364 } else {
1365 break;
1370 mono_aot_unlock ();
1372 return TRUE;
1376 * LOCKING: Acquires the domain lock.
1378 static MonoJitInfo*
1379 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
1380 MonoMethod *method, guint8* ex_info, guint8 *code)
1382 int i, buf_len;
1383 MonoJitInfo *jinfo;
1384 guint code_len, used_int_regs, flags;
1385 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points;
1386 guint8 *p;
1387 int generic_info_size;
1389 /* Load the method info from the AOT file */
1391 p = ex_info;
1392 code_len = decode_value (p, &p);
1393 flags = decode_value (p, &p);
1394 has_generic_jit_info = (flags & 1) != 0;
1395 has_dwarf_unwind_info = (flags & 2) != 0;
1396 has_clauses = (flags & 4) != 0;
1397 has_seq_points = (flags & 8) != 0;
1398 if (has_dwarf_unwind_info) {
1399 guint32 offset;
1401 offset = decode_value (p, &p);
1402 g_assert (offset < (1 << 30));
1403 used_int_regs = offset;
1404 } else {
1405 used_int_regs = decode_value (p, &p);
1407 if (has_generic_jit_info)
1408 generic_info_size = sizeof (MonoGenericJitInfo);
1409 else
1410 generic_info_size = 0;
1412 /* Exception table */
1413 if (has_clauses) {
1414 int num_clauses = decode_value (p, &p);
1416 jinfo =
1417 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size);
1418 jinfo->num_clauses = num_clauses;
1420 for (i = 0; i < num_clauses; ++i) {
1421 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1423 ei->flags = decode_value (p, &p);
1424 ei->exvar_offset = decode_value (p, &p);
1426 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
1427 ei->data.filter = code + decode_value (p, &p);
1428 else {
1429 if (decode_value (p, &p))
1430 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1433 ei->try_start = code + decode_value (p, &p);
1434 ei->try_end = code + decode_value (p, &p);
1435 ei->handler_start = code + decode_value (p, &p);
1438 else {
1439 jinfo = mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + generic_info_size);
1442 jinfo->code_size = code_len;
1443 jinfo->used_regs = used_int_regs;
1444 jinfo->method = method;
1445 jinfo->code_start = code;
1446 jinfo->domain_neutral = 0;
1447 jinfo->from_aot = 1;
1449 if (has_generic_jit_info) {
1450 MonoGenericJitInfo *gi;
1452 jinfo->has_generic_jit_info = 1;
1454 gi = mono_jit_info_get_generic_jit_info (jinfo);
1455 g_assert (gi);
1457 gi->has_this = decode_value (p, &p);
1458 gi->this_reg = decode_value (p, &p);
1459 gi->this_offset = decode_value (p, &p);
1461 /* This currently contains no data */
1462 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1464 jinfo->method = decode_method_ref_2 (amodule, p, &p);
1467 if (has_seq_points) {
1468 GPtrArray *seq_points;
1469 int il_offset, native_offset, last_il_offset, last_native_offset;
1471 int len = decode_value (p, &p);
1473 seq_points = g_ptr_array_new ();
1474 last_il_offset = last_native_offset = 0;
1475 for (i = 0; i < len; i += 2) {
1476 il_offset = last_il_offset + decode_value (p, &p);
1477 native_offset = last_native_offset + decode_value (p, &p);
1479 g_ptr_array_add (seq_points, GINT_TO_POINTER (il_offset));
1480 g_ptr_array_add (seq_points, GINT_TO_POINTER (native_offset));
1482 last_il_offset = il_offset;
1483 last_native_offset = native_offset;
1486 mono_domain_lock (domain);
1487 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
1488 mono_domain_unlock (domain);
1491 /* Load debug info */
1492 buf_len = decode_value (p, &p);
1493 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1495 if (amodule != jinfo->method->klass->image->aot_module) {
1496 mono_aot_lock ();
1497 if (!ji_to_amodule)
1498 ji_to_amodule = g_hash_table_new (NULL, NULL);
1499 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
1500 mono_aot_unlock ();
1503 return jinfo;
1507 * mono_aot_get_unwind_info:
1509 * Return a pointer to the DWARF unwind info belonging to JI.
1511 guint8*
1512 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
1514 MonoAotModule *amodule = ji->method->klass->image->aot_module;
1515 guint8 *p;
1516 guint8 *code = ji->code_start;
1518 g_assert (amodule);
1519 g_assert (ji->from_aot);
1521 if (!(code >= amodule->code && code <= amodule->code_end)) {
1522 /* ji belongs to a different aot module than amodule */
1523 mono_aot_lock ();
1524 g_assert (ji_to_amodule);
1525 amodule = g_hash_table_lookup (ji_to_amodule, ji);
1526 g_assert (amodule);
1527 g_assert (code >= amodule->code && code <= amodule->code_end);
1530 p = amodule->unwind_info + ji->used_regs;
1531 *unwind_info_len = decode_value (p, &p);
1532 return p;
1535 MonoJitInfo *
1536 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1538 int pos, left, right, offset, offset1, offset2, last_offset, new_offset;
1539 int page_index, method_index, table_len, is_wrapper;
1540 guint32 token;
1541 MonoAotModule *amodule = image->aot_module;
1542 MonoMethod *method;
1543 MonoJitInfo *jinfo;
1544 guint8 *code, *ex_info, *p;
1545 guint32 *table, *ptr;
1546 gboolean found;
1548 if (!amodule)
1549 return NULL;
1551 if (domain != mono_get_root_domain ())
1552 /* FIXME: */
1553 return NULL;
1555 offset = (guint8*)addr - amodule->code;
1557 /* First search through the index */
1558 ptr = amodule->method_order;
1559 last_offset = 0;
1560 page_index = 0;
1561 found = FALSE;
1563 if (*ptr == 0xffffff)
1564 return NULL;
1565 ptr ++;
1567 while (*ptr != 0xffffff) {
1568 guint32 method_index = ptr [0];
1569 new_offset = amodule->code_offsets [method_index];
1571 if (offset >= last_offset && offset < new_offset) {
1572 found = TRUE;
1573 break;
1576 ptr ++;
1577 last_offset = new_offset;
1578 page_index ++;
1581 /* Skip rest of index */
1582 while (*ptr != 0xffffff)
1583 ptr ++;
1584 ptr ++;
1586 table = ptr;
1587 table_len = amodule->method_order_end - table;
1589 g_assert (table <= amodule->method_order_end);
1591 if (found) {
1592 left = (page_index * 1024);
1593 right = left + 1024;
1595 if (right > table_len)
1596 right = table_len;
1598 offset1 = amodule->code_offsets [table [left]];
1599 g_assert (offset1 <= offset);
1601 //printf ("Found in index: 0x%x 0x%x 0x%x\n", offset, last_offset, new_offset);
1603 else {
1604 //printf ("Not found in index: 0x%x\n", offset);
1605 left = 0;
1606 right = table_len;
1609 /* Binary search inside the method_order table to find the method */
1610 while (TRUE) {
1611 pos = (left + right) / 2;
1613 g_assert (table + pos <= amodule->method_order_end);
1615 //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]]);
1617 offset1 = amodule->code_offsets [table [pos]];
1618 if (table + pos + 1 >= amodule->method_order_end)
1619 offset2 = amodule->code_end - amodule->code;
1620 else
1621 offset2 = amodule->code_offsets [table [pos + 1]];
1623 if (offset < offset1)
1624 right = pos;
1625 else if (offset >= offset2)
1626 left = pos + 1;
1627 else
1628 break;
1631 method_index = table [pos];
1633 /* Might be a wrapper/extra method */
1634 if (amodule->extra_methods) {
1635 mono_aot_lock ();
1636 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
1637 mono_aot_unlock ();
1638 } else {
1639 method = NULL;
1642 if (!method) {
1643 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
1645 * This is hit for extra methods which are called directly, so they are
1646 * not in amodule->extra_methods.
1648 table_len = amodule->extra_method_info_offsets [0];
1649 table = amodule->extra_method_info_offsets + 1;
1650 left = 0;
1651 right = table_len;
1652 pos = 0;
1654 /* Binary search */
1655 while (TRUE) {
1656 pos = ((left + right) / 2);
1658 g_assert (pos < table_len);
1660 if (table [pos * 2] < method_index)
1661 left = pos + 1;
1662 else if (table [pos * 2] > method_index)
1663 right = pos;
1664 else
1665 break;
1668 p = amodule->extra_method_info + table [(pos * 2) + 1];
1669 is_wrapper = decode_value (p, &p);
1670 g_assert (!is_wrapper);
1671 method = decode_method_ref_2 (amodule, p, &p);
1672 g_assert (method);
1673 } else {
1674 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
1675 method = mono_get_method (image, token, NULL);
1679 /* FIXME: */
1680 g_assert (method);
1682 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
1684 code = &amodule->code [amodule->code_offsets [method_index]];
1685 ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
1687 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code);
1689 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
1690 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
1692 /* Add it to the normal JitInfo tables */
1693 mono_jit_info_table_add (domain, jinfo);
1695 return jinfo;
1698 static gboolean
1699 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
1701 guint8 *p = buf;
1702 gpointer *table;
1703 MonoImage *image;
1704 int i;
1706 switch (ji->type) {
1707 case MONO_PATCH_INFO_METHOD:
1708 case MONO_PATCH_INFO_METHOD_JUMP:
1709 case MONO_PATCH_INFO_ICALL_ADDR:
1710 case MONO_PATCH_INFO_METHOD_RGCTX: {
1711 guint32 token;
1712 MonoMethod *method;
1713 gboolean no_aot_trampoline;
1715 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
1716 if (!image)
1717 goto cleanup;
1719 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
1720 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (image, token));
1721 ji->type = MONO_PATCH_INFO_ABS;
1723 else {
1724 if (method)
1725 ji->data.method = method;
1726 else
1727 ji->data.method = mono_get_method (image, token, NULL);
1728 g_assert (ji->data.method);
1729 mono_class_init (ji->data.method->klass);
1731 break;
1733 case MONO_PATCH_INFO_INTERNAL_METHOD:
1734 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
1735 guint32 len = decode_value (p, &p);
1737 ji->data.name = (char*)p;
1738 p += len + 1;
1739 break;
1741 case MONO_PATCH_INFO_METHODCONST:
1742 /* Shared */
1743 ji->data.method = decode_method_ref_2 (aot_module, p, &p);
1744 if (!ji->data.method)
1745 goto cleanup;
1746 break;
1747 case MONO_PATCH_INFO_VTABLE:
1748 case MONO_PATCH_INFO_CLASS:
1749 case MONO_PATCH_INFO_IID:
1750 case MONO_PATCH_INFO_ADJUSTED_IID:
1751 /* Shared */
1752 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1753 if (!ji->data.klass)
1754 goto cleanup;
1755 break;
1756 case MONO_PATCH_INFO_CLASS_INIT:
1757 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
1758 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1759 if (!ji->data.klass)
1760 goto cleanup;
1761 break;
1762 case MONO_PATCH_INFO_IMAGE:
1763 ji->data.image = load_image (aot_module, decode_value (p, &p));
1764 if (!ji->data.image)
1765 goto cleanup;
1766 break;
1767 case MONO_PATCH_INFO_FIELD:
1768 case MONO_PATCH_INFO_SFLDA:
1769 /* Shared */
1770 ji->data.field = decode_field_info (aot_module, p, &p);
1771 if (!ji->data.field)
1772 goto cleanup;
1773 break;
1774 case MONO_PATCH_INFO_SWITCH:
1775 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
1776 ji->data.table->table_size = decode_value (p, &p);
1777 table = g_new (gpointer, ji->data.table->table_size);
1778 ji->data.table->table = (MonoBasicBlock**)table;
1779 for (i = 0; i < ji->data.table->table_size; i++)
1780 table [i] = (gpointer)(gssize)decode_value (p, &p);
1781 break;
1782 case MONO_PATCH_INFO_R4: {
1783 guint32 val;
1785 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
1786 val = decode_value (p, &p);
1787 *(float*)ji->data.target = *(float*)&val;
1788 break;
1790 case MONO_PATCH_INFO_R8: {
1791 guint32 val [2];
1792 guint64 v;
1794 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
1796 val [0] = decode_value (p, &p);
1797 val [1] = decode_value (p, &p);
1798 // FIXME: Is this correct ?
1799 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
1800 *(double*)ji->data.target = *(double*)&v;
1801 break;
1803 case MONO_PATCH_INFO_LDSTR:
1804 image = load_image (aot_module, decode_value (p, &p));
1805 if (!image)
1806 goto cleanup;
1807 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
1808 break;
1809 case MONO_PATCH_INFO_RVA:
1810 case MONO_PATCH_INFO_DECLSEC:
1811 case MONO_PATCH_INFO_LDTOKEN:
1812 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1813 /* Shared */
1814 image = load_image (aot_module, decode_value (p, &p));
1815 if (!image)
1816 goto cleanup;
1817 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
1819 ji->data.token->has_context = decode_value (p, &p);
1820 if (ji->data.token->has_context) {
1821 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
1822 if (!res)
1823 goto cleanup;
1825 break;
1826 case MONO_PATCH_INFO_EXC_NAME:
1827 ji->data.klass = decode_klass_ref (aot_module, p, &p);
1828 if (!ji->data.klass)
1829 goto cleanup;
1830 ji->data.name = ji->data.klass->name;
1831 break;
1832 case MONO_PATCH_INFO_METHOD_REL:
1833 ji->data.offset = decode_value (p, &p);
1834 break;
1835 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
1836 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1837 case MONO_PATCH_INFO_MONITOR_ENTER:
1838 case MONO_PATCH_INFO_MONITOR_EXIT:
1839 break;
1840 case MONO_PATCH_INFO_RGCTX_FETCH: {
1841 gboolean res;
1842 MonoJumpInfoRgctxEntry *entry;
1844 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
1845 entry->method = decode_method_ref_2 (aot_module, p, &p);
1846 entry->in_mrgctx = decode_value (p, &p);
1847 entry->info_type = decode_value (p, &p);
1848 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
1849 entry->data->type = decode_value (p, &p);
1851 res = decode_patch (aot_module, mp, entry->data, p, &p);
1852 if (!res)
1853 goto cleanup;
1854 ji->data.rgctx_entry = entry;
1855 break;
1857 case MONO_PATCH_INFO_SEQ_POINT_INFO:
1858 break;
1859 default:
1860 g_warning ("unhandled type %d", ji->type);
1861 g_assert_not_reached ();
1864 *endbuf = p;
1866 return TRUE;
1868 cleanup:
1869 return FALSE;
1872 static MonoJumpInfo*
1873 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
1874 guint32 **got_slots,
1875 guint8 *buf, guint8 **endbuf)
1877 MonoJumpInfo *patches;
1878 int pindex;
1879 guint8 *p;
1881 p = buf;
1883 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
1885 *got_slots = g_malloc (sizeof (guint32) * n_patches);
1887 for (pindex = 0; pindex < n_patches; ++pindex) {
1888 MonoJumpInfo *ji = &patches [pindex];
1889 guint8 *shared_p;
1890 gboolean res;
1891 guint32 got_offset;
1893 got_offset = decode_value (p, &p);
1895 if (aot_module->got [got_offset]) {
1896 /* Already loaded */
1897 //printf ("HIT!\n");
1898 } else {
1899 shared_p = aot_module->got_info + aot_module->got_info_offsets [got_offset];
1901 ji->type = decode_value (shared_p, &shared_p);
1903 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
1904 if (!res)
1905 goto cleanup;
1908 (*got_slots) [pindex] = got_offset;
1911 *endbuf = p;
1912 return patches;
1914 cleanup:
1915 g_free (*got_slots);
1916 *got_slots = NULL;
1918 return NULL;
1921 static void
1922 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
1925 * Jump addresses cannot be patched by the trampoline code since it
1926 * does not have access to the caller's address. Instead, we collect
1927 * the addresses of the GOT slots pointing to a method, and patch
1928 * them after the method has been compiled.
1930 MonoJitDomainInfo *info = domain_jit_info (domain);
1931 GSList *list;
1933 mono_domain_lock (domain);
1934 if (!info->jump_target_got_slot_hash)
1935 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
1936 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
1937 list = g_slist_prepend (list, got_slot);
1938 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
1939 mono_domain_unlock (domain);
1943 * load_method:
1945 * Load the method identified by METHOD_INDEX from the AOT image. Return a
1946 * pointer to the native code of the method, or NULL if not found.
1947 * METHOD might not be set if the caller only has the image/token info.
1949 static gpointer
1950 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
1952 MonoClass *klass;
1953 gboolean from_plt = method == NULL;
1954 MonoMemPool *mp;
1955 int i, pindex, n_patches, used_strings;
1956 gboolean keep_patches = TRUE;
1957 guint8 *p, *ex_info;
1958 MonoJitInfo *jinfo = NULL;
1959 guint8 *code, *info;
1961 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
1962 return NULL;
1964 if ((domain != mono_get_root_domain ()) && (!(amodule->opts & MONO_OPT_SHARED)))
1965 /* Non shared AOT code can't be used in other appdomains */
1966 return NULL;
1968 if (amodule->out_of_date)
1969 return NULL;
1971 if (amodule->code_offsets [method_index] == 0xffffffff) {
1972 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
1973 char *full_name;
1975 if (!method)
1976 method = mono_get_method (image, token, NULL);
1977 full_name = mono_method_full_name (method, TRUE);
1978 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
1979 g_free (full_name);
1981 return NULL;
1984 code = &amodule->code [amodule->code_offsets [method_index]];
1985 info = &amodule->method_info [amodule->method_info_offsets [method_index]];
1987 mono_aot_lock ();
1988 if (!amodule->methods_loaded)
1989 amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods + 1);
1990 mono_aot_unlock ();
1992 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
1993 return code;
1995 if (mono_last_aot_method != -1) {
1996 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
1997 return NULL;
1998 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
1999 if (method)
2000 printf ("LAST AOT METHOD: %s%s%s.%s.\n", method->klass->name_space, method->klass->name_space [0] ? "." : "", method->klass->name, method->name);
2001 else
2002 printf ("LAST AOT METHOD: %p %d\n", code, method_index);
2006 p = info;
2008 if (method) {
2009 klass = method->klass;
2010 decode_klass_ref (amodule, p, &p);
2011 } else {
2012 klass = decode_klass_ref (amodule, p, &p);
2015 if (amodule->opts & MONO_OPT_SHARED)
2016 used_strings = decode_value (p, &p);
2017 else
2018 used_strings = 0;
2020 for (i = 0; i < used_strings; i++) {
2021 guint token = decode_value (p, &p);
2022 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2025 if (amodule->opts & MONO_OPT_SHARED)
2026 keep_patches = FALSE;
2028 n_patches = decode_value (p, &p);
2030 keep_patches = FALSE;
2032 if (n_patches) {
2033 MonoJumpInfo *patches;
2034 guint32 *got_slots;
2036 if (keep_patches)
2037 mp = domain->mp;
2038 else
2039 mp = mono_mempool_new ();
2041 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2042 if (patches == NULL)
2043 goto cleanup;
2045 for (pindex = 0; pindex < n_patches; ++pindex) {
2046 MonoJumpInfo *ji = &patches [pindex];
2048 if (!amodule->got [got_slots [pindex]]) {
2049 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2050 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2051 amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
2052 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2053 register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
2055 ji->type = MONO_PATCH_INFO_NONE;
2058 g_free (got_slots);
2060 if (!keep_patches)
2061 mono_mempool_destroy (mp);
2064 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2065 char *full_name;
2067 if (!method)
2068 method = mono_get_method (image, token, NULL);
2070 full_name = mono_method_full_name (method, TRUE);
2072 if (!jinfo) {
2073 ex_info = &amodule->ex_info [amodule->ex_info_offsets [method_index]];
2074 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, code);
2077 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);
2078 g_free (full_name);
2081 mono_aot_lock ();
2083 mono_jit_stats.methods_aot++;
2085 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2087 init_plt (amodule);
2089 if (method && method->wrapper_type)
2090 g_hash_table_insert (amodule->method_to_code, method, code);
2092 mono_aot_unlock ();
2094 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
2095 MonoJitInfo *jinfo;
2097 if (!method) {
2098 method = mono_get_method (image, token, NULL);
2099 g_assert (method);
2101 mono_profiler_method_jit (method);
2102 jinfo = mono_jit_info_table_find (domain, (char*)code);
2103 g_assert (jinfo);
2104 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
2107 if (from_plt && klass && !klass->generic_container)
2108 mono_runtime_class_init (mono_class_vtable (domain, klass));
2110 return code;
2112 cleanup:
2113 /* FIXME: The space in domain->mp is wasted */
2114 if (amodule->opts & MONO_OPT_SHARED)
2115 /* No need to cache patches */
2116 mono_mempool_destroy (mp);
2118 if (jinfo)
2119 g_free (jinfo);
2121 return NULL;
2124 static guint32
2125 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method)
2127 guint32 table_size, entry_size, hash;
2128 guint32 *table, *entry;
2129 char *name = NULL;
2130 guint32 index;
2131 static guint32 n_extra_decodes;
2133 if (!amodule)
2134 return 0xffffff;
2136 table_size = amodule->extra_method_table [0];
2137 table = amodule->extra_method_table + 1;
2138 entry_size = 3;
2140 if (method->wrapper_type) {
2141 name = mono_aot_wrapper_name (method);
2144 hash = mono_aot_method_hash (method) % table_size;
2146 entry = &table [hash * entry_size];
2148 if (entry [0] == 0)
2149 return 0xffffff;
2151 index = 0xffffff;
2152 while (TRUE) {
2153 guint32 key = entry [0];
2154 guint32 value = entry [1];
2155 guint32 next = entry [entry_size - 1];
2156 MonoMethod *m;
2157 guint8 *p;
2158 int is_wrapper_name;
2160 p = amodule->extra_method_info + key;
2161 is_wrapper_name = decode_value (p, &p);
2162 if (is_wrapper_name) {
2163 int wrapper_type = decode_value (p, &p);
2164 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2165 index = value;
2166 break;
2168 } else if (can_method_ref_match_method (amodule, p, method)) {
2169 mono_aot_lock ();
2170 if (!amodule->method_ref_to_method)
2171 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2172 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2173 mono_aot_unlock ();
2174 if (!m) {
2175 guint8 *orig_p = p;
2176 m = decode_method_ref_2 (amodule, p, &p);
2177 if (m) {
2178 mono_aot_lock ();
2179 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2180 mono_aot_unlock ();
2183 if (m == method) {
2184 index = value;
2185 break;
2188 /* Special case: wrappers of shared generic methods */
2189 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2190 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2191 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2192 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2194 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2195 index = value;
2196 break;
2200 /* Methods decoded needlessly */
2202 if (m)
2203 printf ("%d %s %s\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
2205 n_extra_decodes ++;
2208 if (next != 0)
2209 entry = &table [next * entry_size];
2210 else
2211 break;
2214 g_free (name);
2215 return index;
2218 static void
2219 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2221 g_ptr_array_add ((GPtrArray*)user_data, value);
2225 * find_extra_method:
2227 * Try finding METHOD in the extra_method table in all AOT images.
2228 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2229 * module where the method was found.
2231 static guint32
2232 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2234 guint32 index;
2235 GPtrArray *modules;
2236 int i;
2238 /* Try the method's module first */
2239 *out_amodule = method->klass->image->aot_module;
2240 index = find_extra_method_in_amodule (method->klass->image->aot_module, method);
2241 if (index != 0xffffff)
2242 return index;
2245 * Try all other modules.
2246 * This is needed because generic instances klass->image points to the image
2247 * containing the generic definition, but the native code is generated to the
2248 * AOT image which contains the reference.
2251 /* Make a copy to avoid doing the search inside the aot lock */
2252 modules = g_ptr_array_new ();
2253 mono_aot_lock ();
2254 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2255 mono_aot_unlock ();
2257 index = 0xffffff;
2258 for (i = 0; i < modules->len; ++i) {
2259 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2261 if (amodule != method->klass->image->aot_module)
2262 index = find_extra_method_in_amodule (amodule, method);
2263 if (index != 0xffffff) {
2264 *out_amodule = amodule;
2265 break;
2269 g_ptr_array_free (modules, TRUE);
2271 return index;
2275 * mono_aot_get_method:
2277 * Return a pointer to the AOTed native code for METHOD if it can be found,
2278 * NULL otherwise.
2279 * On platforms with function pointers, this doesn't return a function pointer.
2281 gpointer
2282 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2284 MonoClass *klass = method->klass;
2285 guint32 method_index;
2286 MonoAotModule *amodule = klass->image->aot_module;
2287 guint8 *code;
2289 if (!amodule)
2290 return NULL;
2292 if (amodule->out_of_date)
2293 return NULL;
2295 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2296 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2297 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2298 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2299 return NULL;
2302 * Use the original method instead of its invoke-with-check wrapper.
2303 * This is not a problem when using full-aot, since it doesn't support
2304 * remoting.
2306 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
2307 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
2309 g_assert (klass->inited);
2311 /* Find method index */
2312 if (method->is_inflated && mono_method_is_generic_sharable_impl (method, FALSE)) {
2313 method = mono_method_get_declaring_generic_method (method);
2314 method_index = mono_metadata_token_index (method->token) - 1;
2315 } else if (method->is_inflated || !method->token) {
2316 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2317 mono_aot_lock ();
2318 code = g_hash_table_lookup (amodule->method_to_code, method);
2319 mono_aot_unlock ();
2320 if (code)
2321 return code;
2323 method_index = find_extra_method (method, &amodule);
2325 * Special case the ICollection<T> wrappers for arrays, as they cannot
2326 * be statically enumerated, and each wrapper ends up calling the same
2327 * method in Array.
2329 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
2330 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
2332 code = mono_aot_get_method (domain, m);
2333 if (code) {
2334 if (mono_method_needs_static_rgctx_invoke (m, FALSE))
2335 code = mono_create_static_rgctx_trampoline (m, code);
2337 return code;
2342 * Special case Array.GetGenericValueImpl which is a generic icall.
2343 * Generic sharing currently can't handle it, but the icall returns data using
2344 * an out parameter, so the managed-to-native wrappers can share the same code.
2346 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
2347 MonoMethod *m;
2348 MonoGenericContext ctx;
2349 MonoType *args [16];
2351 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
2352 /* Avoid recursion */
2353 return NULL;
2355 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
2356 g_assert (m);
2358 memset (&ctx, 0, sizeof (ctx));
2359 args [0] = &mono_defaults.object_class->byval_arg;
2360 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2362 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2365 * Get the code for the <object> instantiation which should be emitted into
2366 * the mscorlib aot image by the AOT compiler.
2368 code = mono_aot_get_method (domain, m);
2369 if (code)
2370 return code;
2373 if (method_index == 0xffffff) {
2374 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2375 char *full_name;
2377 full_name = mono_method_full_name (method, TRUE);
2378 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2379 g_free (full_name);
2381 return NULL;
2384 if (method_index == 0xffffff)
2385 return NULL;
2387 /* Needed by find_jit_info */
2388 mono_aot_lock ();
2389 if (!amodule->extra_methods)
2390 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2391 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2392 mono_aot_unlock ();
2393 } else {
2394 /* Common case */
2395 method_index = mono_metadata_token_index (method->token) - 1;
2398 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2402 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2403 * method.
2405 gpointer
2406 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2408 MonoAotModule *aot_module = image->aot_module;
2409 int method_index;
2411 if (!aot_module)
2412 return NULL;
2414 method_index = mono_metadata_token_index (token) - 1;
2416 return load_method (domain, aot_module, image, NULL, token, method_index);
2419 typedef struct {
2420 guint8 *addr;
2421 gboolean res;
2422 } IsGotEntryUserData;
2424 static void
2425 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2427 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2428 MonoAotModule *aot_module = (MonoAotModule*)value;
2430 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
2431 data->res = TRUE;
2434 gboolean
2435 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2437 IsGotEntryUserData user_data;
2439 if (!aot_modules)
2440 return FALSE;
2442 user_data.addr = addr;
2443 user_data.res = FALSE;
2444 mono_aot_lock ();
2445 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2446 mono_aot_unlock ();
2448 return user_data.res;
2451 typedef struct {
2452 guint8 *addr;
2453 MonoAotModule *module;
2454 } FindAotModuleUserData;
2456 static void
2457 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2459 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2460 MonoAotModule *aot_module = (MonoAotModule*)value;
2462 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2463 data->module = aot_module;
2466 static inline MonoAotModule*
2467 find_aot_module (guint8 *code)
2469 FindAotModuleUserData user_data;
2471 if (!aot_modules)
2472 return NULL;
2474 /* Reading these need no locking */
2475 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2476 return NULL;
2478 user_data.addr = code;
2479 user_data.module = NULL;
2481 mono_aot_lock ();
2482 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2483 mono_aot_unlock ();
2485 return user_data.module;
2489 * mono_aot_plt_resolve:
2491 * This function is called by the entries in the PLT to resolve the actual method that
2492 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2494 gpointer
2495 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2497 #ifdef MONO_ARCH_AOT_SUPPORTED
2498 guint8 *p, *target, *plt_entry;
2499 MonoJumpInfo ji;
2500 MonoAotModule *module = (MonoAotModule*)aot_module;
2501 gboolean res, no_ftnptr = FALSE;
2502 MonoMemPool *mp;
2504 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2506 p = &module->got_info [plt_info_offset];
2508 ji.type = decode_value (p, &p);
2510 mp = mono_mempool_new_size (512);
2511 res = decode_patch (module, mp, &ji, p, &p);
2512 // FIXME: Error handling (how ?)
2513 g_assert (res);
2516 * Avoid calling resolve_patch_target in the full-aot case if possible, since
2517 * it would create a trampoline, and we don't need that.
2518 * We could do this only if the method does not need the special handling
2519 * in mono_magic_trampoline ().
2521 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) &&
2522 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
2523 target = mono_jit_compile_method (ji.data.method);
2524 no_ftnptr = TRUE;
2525 } else {
2526 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
2529 // FIXME: Clean this up, but how ?
2530 if (ji.type != MONO_PATCH_INFO_ABS && ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_CLASS_INIT && ji.type != MONO_PATCH_INFO_GENERIC_CLASS_INIT && ji.type != MONO_PATCH_INFO_ICALL_ADDR && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && !no_ftnptr) {
2531 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2532 g_assert (((gpointer*)target) [2] != 0);
2533 #endif
2534 target = mono_create_ftnptr (mono_domain_get (), target);
2537 mono_mempool_destroy (mp);
2539 /* Patch the PLT entry with target which might be the actual method not a trampoline */
2540 plt_entry = mono_aot_get_plt_entry (code);
2541 g_assert (plt_entry);
2542 mono_arch_patch_plt_entry (plt_entry, module->got, NULL, target);
2544 return target;
2545 #else
2546 g_assert_not_reached ();
2547 return NULL;
2548 #endif
2552 * init_plt:
2554 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
2555 * method in the module is loaded to avoid committing memory by writing to it.
2556 * LOCKING: Assumes the AOT lock is held.
2558 static void
2559 init_plt (MonoAotModule *amodule)
2561 #ifndef MONO_CROSS_COMPILE
2563 #ifdef MONO_ARCH_AOT_SUPPORTED
2564 #ifdef __i386__
2565 guint8 *buf = amodule->plt;
2566 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
2567 int i;
2568 gpointer plt_0;
2569 #endif
2570 gpointer tramp;
2572 if (amodule->plt_inited)
2573 return;
2575 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
2577 #ifdef __i386__
2578 /* Initialize the first PLT entry */
2579 make_writable (amodule->plt, amodule->plt_end - amodule->plt);
2580 x86_jump_code (buf, tramp);
2581 #elif defined(__x86_64__) || defined(__arm__) || defined(__mono_ppc__)
2583 * Initialize the PLT entries in the GOT to point to the default targets.
2586 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
2587 plt_0 = mono_create_ftnptr (mono_domain_get (), amodule->plt);
2588 /* The first entry points to the AOT trampoline */
2589 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base] = tramp;
2590 for (i = 1; i < amodule->info.plt_size; ++i)
2591 /* All the default entries point to the first entry */
2592 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = plt_0;
2593 #else
2594 g_assert_not_reached ();
2595 #endif
2597 amodule->plt_inited = TRUE;
2598 #endif
2600 #endif /* MONO_CROSS_COMPILE */
2604 * mono_aot_get_plt_entry:
2606 * Return the address of the PLT entry called by the code at CODE if exists.
2608 guint8*
2609 mono_aot_get_plt_entry (guint8 *code)
2611 MonoAotModule *aot_module = find_aot_module (code);
2612 #if defined(__arm__) || defined(__mono_ppc__)
2613 guint32 ins;
2614 #endif
2616 if (!aot_module)
2617 return NULL;
2619 #if defined(__i386__) || defined(__x86_64__)
2620 if (code [-5] == 0xe8) {
2621 guint32 disp = *(guint32*)(code - 4);
2622 guint8 *target = code + disp;
2624 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2625 return target;
2627 #elif defined(__arm__)
2628 ins = ((guint32*)(gpointer)code) [-1];
2630 /* Should be a 'bl' */
2631 if ((((ins >> 25) & 0x7) == 0x5) && (((ins >> 24) & 0x1) == 0x1)) {
2632 gint32 disp = ((gint32)ins) & 0xffffff;
2633 guint8 *target = code - 4 + 8 + (disp * 4);
2635 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2636 return target;
2638 #elif defined(__mono_ppc__)
2639 /* Should be a bl */
2640 ins = ((guint32*)(gpointer)code) [-1];
2642 if ((ins >> 26 == 18) && ((ins & 1) == 1) && ((ins & 2) == 0)) {
2643 gint32 disp = (((gint32)ins) >> 2) & 0xffffff;
2644 guint8 *target = code - 4 + (disp * 4);
2646 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
2647 return target;
2649 #else
2650 g_assert_not_reached ();
2651 #endif
2653 return NULL;
2657 * mono_aot_get_plt_info_offset:
2659 * Return the PLT info offset belonging to the plt entry called by CODE.
2661 guint32
2662 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
2664 guint8 *plt_entry = mono_aot_get_plt_entry (code);
2666 g_assert (plt_entry);
2668 /* The offset is embedded inside the code after the plt entry */
2669 #if defined(__i386__)
2670 return *(guint32*)(plt_entry + 5);
2671 #elif defined(__x86_64__)
2672 return *(guint32*)(plt_entry + 6);
2673 #elif defined(__arm__)
2674 /* The offset is stored as the 4th word of the plt entry */
2675 return ((guint32*)plt_entry) [3];
2676 #elif defined(__mono_ppc__)
2677 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2678 return ((guint32*)plt_entry) [8];
2679 #else
2680 return ((guint32*)plt_entry) [6];
2681 #endif
2682 #else
2683 g_assert_not_reached ();
2684 return 0;
2685 #endif
2688 static gpointer
2689 mono_create_ftnptr_malloc (guint8 *code)
2691 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2692 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
2694 ftnptr->code = code;
2695 ftnptr->toc = NULL;
2696 ftnptr->env = NULL;
2698 return ftnptr;
2699 #else
2700 return code;
2701 #endif
2705 * load_function:
2707 * Load the function named NAME from the aot image.
2709 static gpointer
2710 load_function (MonoAotModule *amodule, const char *name)
2712 char *symbol;
2713 guint8 *p;
2714 int n_patches, pindex;
2715 MonoMemPool *mp;
2716 gpointer code;
2718 /* Load the code */
2720 symbol = g_strdup_printf ("%s", name);
2721 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
2722 g_free (symbol);
2723 if (!code)
2724 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
2726 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
2728 /* Load info */
2730 symbol = g_strdup_printf ("%s_p", name);
2731 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
2732 g_free (symbol);
2733 if (!p)
2734 /* Nothing to patch */
2735 return code;
2737 /* Similar to mono_aot_load_method () */
2739 n_patches = decode_value (p, &p);
2741 if (n_patches) {
2742 MonoJumpInfo *patches;
2743 guint32 *got_slots;
2745 mp = mono_mempool_new ();
2747 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2748 g_assert (patches);
2750 for (pindex = 0; pindex < n_patches; ++pindex) {
2751 MonoJumpInfo *ji = &patches [pindex];
2752 gpointer target;
2754 if (amodule->got [got_slots [pindex]])
2755 continue;
2758 * When this code is executed, the runtime may not be initalized yet, so
2759 * resolve the patch info by hand.
2761 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
2762 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
2763 target = mono_get_lmf_addr;
2764 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
2765 target = mono_thread_force_interruption_checkpoint;
2766 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
2767 target = mono_exception_from_token;
2768 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
2769 target = mono_get_throw_exception ();
2770 #ifdef __x86_64__
2771 } else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
2772 target = mono_amd64_throw_exception;
2773 #endif
2774 #ifdef __x86_64__
2775 } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
2776 target = mono_amd64_get_original_ip;
2777 #endif
2778 #ifdef __arm__
2779 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
2780 target = mono_arm_throw_exception;
2781 } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
2782 target = mono_arm_throw_exception_by_token;
2783 #endif
2784 #ifdef __mono_ppc__
2785 } else if (!strcmp (ji->data.name, "mono_ppc_throw_exception")) {
2786 target = mono_ppc_throw_exception;
2787 #endif
2788 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
2789 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
2790 target = (gpointer)mono_get_trampoline_func (tramp_type2);
2791 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
2792 /* atoll is needed because the the offset is unsigned */
2793 guint32 slot;
2794 int res;
2796 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
2797 g_assert (res == 1);
2798 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
2799 target = mono_create_ftnptr_malloc (target);
2800 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
2801 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
2802 target = mono_create_ftnptr_malloc (target);
2803 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
2804 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
2805 target = mono_create_ftnptr_malloc (target);
2806 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
2807 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
2808 target = mono_create_ftnptr_malloc (target);
2809 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
2810 target = mono_thread_get_and_clear_pending_exception;
2811 } else {
2812 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
2813 g_assert_not_reached ();
2814 target = NULL;
2816 } else {
2817 /* Hopefully the code doesn't have patches which need method or
2818 * domain to be set.
2820 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
2821 g_assert (target);
2824 amodule->got [got_slots [pindex]] = target;
2827 g_free (got_slots);
2829 mono_mempool_destroy (mp);
2832 return code;
2836 * Return the piece of code identified by NAME from the mscorlib AOT file.
2837 * On ppc64, this returns a function descriptor.
2839 gpointer
2840 mono_aot_get_named_code (const char *name)
2842 MonoImage *image;
2843 MonoAotModule *amodule;
2845 image = mono_defaults.corlib;
2846 g_assert (image);
2848 amodule = image->aot_module;
2849 g_assert (amodule);
2851 return mono_create_ftnptr_malloc (load_function (amodule, name));
2854 /* Return a given kind of trampoline */
2855 static gpointer
2856 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
2858 MonoAotModule *amodule;
2859 int index, tramp_size;
2860 MonoImage *image;
2862 /* Currently, we keep all trampolines in the mscorlib AOT image */
2863 image = mono_defaults.corlib;
2864 g_assert (image);
2866 mono_aot_lock ();
2868 amodule = image->aot_module;
2869 g_assert (amodule);
2871 *out_amodule = amodule;
2873 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
2874 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
2876 index = amodule->trampoline_index [tramp_type] ++;
2878 mono_aot_unlock ();
2880 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
2882 tramp_size = amodule->info.trampoline_size [tramp_type];
2884 if (out_tramp_size)
2885 *out_tramp_size = tramp_size;
2887 return amodule->trampolines [tramp_type] + (index * tramp_size);
2891 * Return a specific trampoline from the AOT file.
2893 gpointer
2894 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
2896 MonoAotModule *amodule;
2897 guint32 got_offset, tramp_size;
2898 guint8 *code, *tramp;
2899 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
2900 static gboolean inited;
2901 static guint32 num_trampolines;
2903 if (!inited) {
2904 mono_aot_lock ();
2906 if (!inited) {
2907 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
2908 inited = TRUE;
2911 mono_aot_unlock ();
2914 num_trampolines ++;
2916 if (!generic_trampolines [tramp_type]) {
2917 char *symbol;
2919 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2920 generic_trampolines [tramp_type] = mono_aot_get_named_code (symbol);
2921 g_free (symbol);
2924 tramp = generic_trampolines [tramp_type];
2925 g_assert (tramp);
2927 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
2929 amodule->got [got_offset] = tramp;
2930 amodule->got [got_offset + 1] = arg1;
2932 if (code_len)
2933 *code_len = tramp_size;
2935 return code;
2938 gpointer
2939 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
2941 MonoAotModule *amodule;
2942 guint8 *code;
2943 guint32 got_offset;
2945 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
2947 amodule->got [got_offset] = ctx;
2948 amodule->got [got_offset + 1] = addr;
2950 /* The caller expects an ftnptr */
2951 return mono_create_ftnptr (mono_domain_get (), code);
2954 gpointer
2955 mono_aot_get_unbox_trampoline (MonoMethod *method)
2957 guint32 method_index = mono_metadata_token_index (method->token) - 1;
2958 MonoAotModule *amodule;
2959 char *symbol;
2960 gpointer code;
2962 if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
2963 guint32 index = find_extra_method (method, &amodule);
2964 g_assert (index != 0xffffff);
2966 symbol = g_strdup_printf ("ut_e_%d", index);
2967 } else {
2968 amodule = method->klass->image->aot_module;
2969 g_assert (amodule);
2971 symbol = g_strdup_printf ("ut_%d", method_index);
2973 code = load_function (amodule, symbol);
2974 g_free (symbol);
2976 /* The caller expects an ftnptr */
2977 return mono_create_ftnptr (mono_domain_get (), code);
2980 gpointer
2981 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
2983 char *symbol;
2984 gpointer code;
2986 symbol = g_strdup_printf ("rgctx_fetch_trampoline_%u", slot);
2987 code = load_function (mono_defaults.corlib->aot_module, symbol);
2988 g_free (symbol);
2989 return code;
2992 gpointer
2993 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
2995 guint32 got_offset;
2996 gpointer code;
2997 gpointer *buf;
2998 int i;
2999 MonoAotModule *amodule;
3001 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
3003 /* Save the entries into an array */
3004 buf = mono_domain_alloc (domain, (count + 1) * 2 * sizeof (gpointer));
3005 for (i = 0; i < count; ++i) {
3006 MonoIMTCheckItem *item = imt_entries [i];
3008 g_assert (item->key);
3009 /* FIXME: */
3010 g_assert (!item->has_target_code);
3012 buf [(i * 2)] = item->key;
3013 buf [(i * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
3015 buf [(count * 2)] = NULL;
3016 buf [(count * 2) + 1] = fail_tramp;
3018 amodule->got [got_offset] = buf;
3020 return code;
3023 #else
3024 /* AOT disabled */
3026 void
3027 mono_aot_init (void)
3031 gpointer
3032 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3034 return NULL;
3037 gboolean
3038 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3040 return FALSE;
3043 gboolean
3044 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3046 return FALSE;
3049 gboolean
3050 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
3052 return FALSE;
3055 MonoJitInfo *
3056 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
3058 return NULL;
3061 gpointer
3062 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3064 return NULL;
3067 guint8*
3068 mono_aot_get_plt_entry (guint8 *code)
3070 return NULL;
3073 gpointer
3074 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3076 return NULL;
3079 gpointer
3080 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
3082 return NULL;
3085 guint32
3086 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3088 g_assert_not_reached ();
3090 return 0;
3093 gpointer
3094 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3096 g_assert_not_reached ();
3097 return NULL;
3100 gpointer
3101 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3103 g_assert_not_reached ();
3104 return NULL;
3107 gpointer
3108 mono_aot_get_named_code (const char *name)
3110 g_assert_not_reached ();
3111 return NULL;
3114 gpointer
3115 mono_aot_get_unbox_trampoline (MonoMethod *method)
3117 g_assert_not_reached ();
3118 return NULL;
3121 gpointer
3122 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3124 g_assert_not_reached ();
3125 return NULL;
3128 gpointer
3129 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3131 g_assert_not_reached ();
3132 return NULL;
3135 guint8*
3136 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3138 g_assert_not_reached ();
3139 return NULL;
3142 #endif