Remove obsolete Microsoft.Vsa and Microsoft.JScript
[mono-project/dkf.git] / mono / mini / aot-runtime.c
blob017cc083dd0b7bccdb91c5c195bb571ff0912ee4
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 HOST_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 #ifdef HAVE_DL_ITERATE_PHDR
39 #include <link.h>
40 #endif
42 #include <mono/metadata/tabledefs.h>
43 #include <mono/metadata/class.h>
44 #include <mono/metadata/object.h>
45 #include <mono/metadata/tokentype.h>
46 #include <mono/metadata/appdomain.h>
47 #include <mono/metadata/debug-helpers.h>
48 #include <mono/metadata/assembly.h>
49 #include <mono/metadata/metadata-internals.h>
50 #include <mono/metadata/marshal.h>
51 #include <mono/metadata/gc-internal.h>
52 #include <mono/metadata/monitor.h>
53 #include <mono/metadata/threads-types.h>
54 #include <mono/utils/mono-logger-internal.h>
55 #include <mono/utils/mono-mmap.h>
56 #include "mono/utils/mono-compiler.h"
57 #include <mono/utils/mono-counters.h>
59 #include "mini.h"
60 #include "version.h"
62 #ifndef DISABLE_AOT
64 #ifdef TARGET_WIN32
65 #define SHARED_EXT ".dll"
66 #elif ((defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__)) || defined(__MACH__)) && !defined(__linux__)
67 #define SHARED_EXT ".dylib"
68 #else
69 #define SHARED_EXT ".so"
70 #endif
72 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
73 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
75 typedef struct MonoAotModule {
76 char *aot_name;
77 /* Pointer to the Global Offset Table */
78 gpointer *got;
79 GHashTable *name_cache;
80 GHashTable *extra_methods;
81 /* Maps methods to their code */
82 GHashTable *method_to_code;
83 /* Maps pointers into the method info to the methods themselves */
84 GHashTable *method_ref_to_method;
85 MonoAssemblyName *image_names;
86 char **image_guids;
87 MonoAssembly *assembly;
88 MonoImage **image_table;
89 guint32 image_table_len;
90 gboolean out_of_date;
91 gboolean plt_inited;
92 guint8 *mem_begin;
93 guint8 *mem_end;
94 guint8 *code;
95 guint8 *code_end;
96 guint8 *plt;
97 guint8 *plt_end;
98 guint8 *blob;
99 gint32 *code_offsets;
100 /* This contains <offset, index> pairs sorted by offset */
101 /* This is needed because LLVM emitted methods can be in any order */
102 gint32 *sorted_code_offsets;
103 gint32 sorted_code_offsets_len;
104 guint32 *method_info_offsets;
105 guint32 *got_info_offsets;
106 guint32 *ex_info_offsets;
107 guint32 *class_info_offsets;
108 guint32 *methods_loaded;
109 guint16 *class_name_table;
110 guint32 *extra_method_table;
111 guint32 *extra_method_info_offsets;
112 guint8 *unwind_info;
114 /* Points to the GNU .eh_frame_hdr section, if it exists */
115 guint8 *eh_frame_hdr;
117 /* Points to the trampolines */
118 guint8 *trampolines [MONO_AOT_TRAMP_NUM];
119 /* The first unused trampoline of each kind */
120 guint32 trampoline_index [MONO_AOT_TRAMP_NUM];
122 MonoAotFileInfo info;
124 gpointer *globals;
125 MonoDl *sofile;
126 } MonoAotModule;
128 static GHashTable *aot_modules;
129 #define mono_aot_lock() EnterCriticalSection (&aot_mutex)
130 #define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
131 static CRITICAL_SECTION aot_mutex;
134 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
135 * AOT modules registered by mono_aot_register_module ().
137 static GHashTable *static_aot_modules;
140 * Maps MonoJitInfo* to the aot module they belong to, this can be different
141 * from ji->method->klass->image's aot module for generic instances.
143 static GHashTable *ji_to_amodule;
146 * Whenever to AOT compile loaded assemblies on demand and store them in
147 * a cache under $HOME/.mono/aot-cache.
149 static gboolean use_aot_cache = FALSE;
152 * Whenever to spawn a new process to AOT a file or do it in-process. Only relevant if
153 * use_aot_cache is TRUE.
155 static gboolean spawn_compiler = TRUE;
157 /* For debugging */
158 static gint32 mono_last_aot_method = -1;
160 static gboolean make_unreadable = FALSE;
161 static guint32 name_table_accesses = 0;
162 static guint32 n_pagefaults = 0;
164 /* Used to speed-up find_aot_module () */
165 static gsize aot_code_low_addr = (gssize)-1;
166 static gsize aot_code_high_addr = 0;
168 static GHashTable *aot_jit_icall_hash;
170 static void
171 init_plt (MonoAotModule *info);
173 /*****************************************************/
174 /* AOT RUNTIME */
175 /*****************************************************/
178 * load_image:
180 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
181 * found, and sets the loader error if SET_ERROR is TRUE.
183 static MonoImage *
184 load_image (MonoAotModule *amodule, int index, gboolean set_error)
186 MonoAssembly *assembly;
187 MonoImageOpenStatus status;
189 g_assert (index < amodule->image_table_len);
191 if (amodule->image_table [index])
192 return amodule->image_table [index];
193 if (amodule->out_of_date)
194 return NULL;
196 assembly = mono_assembly_load (&amodule->image_names [index], NULL, &status);
197 if (!assembly) {
198 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable because dependency %s is not found.\n", amodule->aot_name, amodule->image_names [index].name);
199 amodule->out_of_date = TRUE;
201 if (set_error) {
202 char *full_name = mono_stringify_assembly_name (&amodule->image_names [index]);
203 mono_loader_set_error_assembly_load (full_name, FALSE);
204 g_free (full_name);
206 return NULL;
209 if (strcmp (assembly->image->guid, amodule->image_guids [index])) {
210 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date (Older than dependency %s).\n", amodule->aot_name, amodule->image_names [index].name);
211 amodule->out_of_date = TRUE;
212 return NULL;
215 amodule->image_table [index] = assembly->image;
216 return assembly->image;
219 static inline gint32
220 decode_value (guint8 *ptr, guint8 **rptr)
222 guint8 b = *ptr;
223 gint32 len;
225 if ((b & 0x80) == 0){
226 len = b;
227 ++ptr;
228 } else if ((b & 0x40) == 0){
229 len = ((b & 0x3f) << 8 | ptr [1]);
230 ptr += 2;
231 } else if (b != 0xff) {
232 len = ((b & 0x1f) << 24) |
233 (ptr [1] << 16) |
234 (ptr [2] << 8) |
235 ptr [3];
236 ptr += 4;
238 else {
239 len = (ptr [1] << 24) | (ptr [2] << 16) | (ptr [3] << 8) | ptr [4];
240 ptr += 5;
242 if (rptr)
243 *rptr = ptr;
245 //printf ("DECODE: %d.\n", len);
246 return len;
250 * mono_aot_get_method:
252 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
253 * entry.
255 static guint32
256 mono_aot_get_offset (guint32 *table, int index)
258 int i, group, ngroups, index_entry_size;
259 int start_offset, offset, noffsets, group_size;
260 guint8 *data_start, *p;
261 guint32 *index32 = NULL;
262 guint16 *index16 = NULL;
264 noffsets = table [0];
265 group_size = table [1];
266 ngroups = table [2];
267 index_entry_size = table [3];
268 group = index / group_size;
270 if (index_entry_size == 2) {
271 index16 = (guint16*)&table [4];
272 data_start = (guint8*)&index16 [ngroups];
273 p = data_start + index16 [group];
274 } else {
275 index32 = (guint32*)&table [4];
276 data_start = (guint8*)&index32 [ngroups];
277 p = data_start + index32 [group];
280 /* offset will contain the value of offsets [group * group_size] */
281 offset = start_offset = decode_value (p, &p);
282 for (i = group * group_size + 1; i <= index; ++i) {
283 offset += decode_value (p, &p);
286 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
288 return offset;
291 static MonoMethod*
292 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
294 static MonoClass*
295 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf);
297 static MonoGenericInst*
298 decode_generic_inst (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
300 int type_argc, i;
301 MonoType **type_argv;
302 MonoGenericInst *inst;
303 guint8 *p = buf;
305 type_argc = decode_value (p, &p);
306 type_argv = g_new0 (MonoType*, type_argc);
308 for (i = 0; i < type_argc; ++i) {
309 MonoClass *pclass = decode_klass_ref (module, p, &p);
310 if (!pclass) {
311 g_free (type_argv);
312 return NULL;
314 type_argv [i] = &pclass->byval_arg;
317 inst = mono_metadata_get_generic_inst (type_argc, type_argv);
318 g_free (type_argv);
320 *endbuf = p;
322 return inst;
325 static gboolean
326 decode_generic_context (MonoAotModule *module, MonoGenericContext *ctx, guint8 *buf, guint8 **endbuf)
328 gboolean has_class_inst, has_method_inst;
329 guint8 *p = buf;
331 has_class_inst = decode_value (p, &p);
332 if (has_class_inst) {
333 ctx->class_inst = decode_generic_inst (module, p, &p);
334 if (!ctx->class_inst)
335 return FALSE;
337 has_method_inst = decode_value (p, &p);
338 if (has_method_inst) {
339 ctx->method_inst = decode_generic_inst (module, p, &p);
340 if (!ctx->method_inst)
341 return FALSE;
344 *endbuf = p;
345 return TRUE;
348 static MonoClass*
349 decode_klass_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
351 MonoImage *image;
352 MonoClass *klass, *eklass;
353 guint32 token, rank;
354 guint8 *p = buf;
356 token = decode_value (p, &p);
357 if (token == 0) {
358 *endbuf = p;
359 return NULL;
361 if (mono_metadata_token_table (token) == 0) {
362 image = load_image (module, decode_value (p, &p), TRUE);
363 if (!image)
364 return NULL;
365 klass = mono_class_get (image, MONO_TOKEN_TYPE_DEF + token);
366 } else if (mono_metadata_token_table (token) == MONO_TABLE_TYPESPEC) {
367 if (token == MONO_TOKEN_TYPE_SPEC) {
368 MonoTypeEnum type = decode_value (p, &p);
370 if (type == MONO_TYPE_GENERICINST) {
371 MonoClass *gclass;
372 MonoGenericContext ctx;
373 MonoType *type;
375 gclass = decode_klass_ref (module, p, &p);
376 if (!gclass)
377 return NULL;
378 g_assert (gclass->generic_container);
380 memset (&ctx, 0, sizeof (ctx));
381 ctx.class_inst = decode_generic_inst (module, p, &p);
382 if (!ctx.class_inst)
383 return NULL;
384 type = mono_class_inflate_generic_type (&gclass->byval_arg, &ctx);
385 klass = mono_class_from_mono_type (type);
386 mono_metadata_free_type (type);
387 } else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
388 MonoType *t;
389 MonoGenericContainer *container;
391 int num = decode_value (p, &p);
392 gboolean is_method = decode_value (p, &p);
394 if (is_method) {
395 MonoMethod *method_def;
396 g_assert (type == MONO_TYPE_MVAR);
397 method_def = decode_resolve_method_ref (module, p, &p);
398 if (!method_def)
399 return NULL;
401 container = mono_method_get_generic_container (method_def);
402 } else {
403 MonoClass *class_def;
404 g_assert (type == MONO_TYPE_VAR);
405 class_def = decode_klass_ref (module, p, &p);
406 if (!class_def)
407 return NULL;
409 container = class_def->generic_container;
412 g_assert (container);
414 // FIXME: Memory management
415 t = g_new0 (MonoType, 1);
416 t->type = type;
417 t->data.generic_param = mono_generic_container_get_param (container, num);
419 // FIXME: Maybe use types directly to avoid
420 // the overhead of creating MonoClass-es
421 klass = mono_class_from_mono_type (t);
423 g_free (t);
424 } else {
425 g_assert_not_reached ();
427 } else {
428 image = load_image (module, decode_value (p, &p), TRUE);
429 if (!image)
430 return NULL;
431 klass = mono_class_get (image, token);
433 } else if (token == MONO_TOKEN_TYPE_DEF) {
434 /* Array */
435 image = load_image (module, decode_value (p, &p), TRUE);
436 if (!image)
437 return NULL;
438 rank = decode_value (p, &p);
439 eklass = decode_klass_ref (module, p, &p);
440 klass = mono_array_class_get (eklass, rank);
441 } else {
442 g_assert_not_reached ();
444 g_assert (klass);
446 *endbuf = p;
447 return klass;
450 static MonoClassField*
451 decode_field_info (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
453 MonoClass *klass = decode_klass_ref (module, buf, &buf);
454 guint32 token;
455 guint8 *p = buf;
457 if (!klass)
458 return NULL;
460 token = MONO_TOKEN_FIELD_DEF + decode_value (p, &p);
462 *endbuf = p;
464 return mono_class_get_field (klass, token);
468 * can_method_ref_match_method:
470 * Determine if calling decode_resolve_method_ref on P could return the same method as
471 * METHOD. This is an optimization to avoid calling decode_resolve_method_ref () which
472 * would create MonoMethods which are not needed etc.
474 static gboolean
475 can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
477 guint8 *p = buf;
478 guint32 image_index, value;
480 /* Keep this in sync with decode_method_ref () */
481 value = decode_value (p, &p);
482 image_index = value >> 24;
484 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
485 guint32 wrapper_type;
487 if (!method->wrapper_type)
488 return FALSE;
490 wrapper_type = decode_value (p, &p);
492 if (method->wrapper_type != wrapper_type)
493 return FALSE;
494 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
495 return FALSE;
496 } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
497 if (method->wrapper_type)
498 return FALSE;
501 return TRUE;
505 * decode_method_ref:
507 * Decode a method reference, and return its image and token. This avoids loading
508 * metadata for the method if the caller does not need it. If the method has no token,
509 * then it is loaded from metadata and METHOD is set to the method instance.
511 static MonoImage*
512 decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
514 guint32 image_index, value;
515 MonoImage *image = NULL;
516 guint8 *p = buf;
518 if (method)
519 *method = NULL;
520 if (no_aot_trampoline)
521 *no_aot_trampoline = FALSE;
523 value = decode_value (p, &p);
524 image_index = value >> 24;
526 if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
527 if (no_aot_trampoline)
528 *no_aot_trampoline = TRUE;
529 value = decode_value (p, &p);
530 image_index = value >> 24;
533 if (image_index == MONO_AOT_METHODREF_WRAPPER) {
534 guint32 wrapper_type;
536 wrapper_type = decode_value (p, &p);
538 /* Doesn't matter */
539 image = mono_defaults.corlib;
541 switch (wrapper_type) {
542 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
543 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
545 if (!m)
546 return NULL;
547 mono_class_init (m->klass);
548 *method = mono_marshal_get_remoting_invoke_with_check (m);
549 break;
551 case MONO_WRAPPER_PROXY_ISINST: {
552 MonoClass *klass = decode_klass_ref (module, p, &p);
553 if (!klass)
554 return NULL;
555 *method = mono_marshal_get_proxy_cancast (klass);
556 break;
558 case MONO_WRAPPER_LDFLD:
559 case MONO_WRAPPER_LDFLDA:
560 case MONO_WRAPPER_STFLD:
561 case MONO_WRAPPER_ISINST: {
562 MonoClass *klass = decode_klass_ref (module, p, &p);
563 if (!klass)
564 return NULL;
565 if (wrapper_type == MONO_WRAPPER_LDFLD)
566 *method = mono_marshal_get_ldfld_wrapper (&klass->byval_arg);
567 else if (wrapper_type == MONO_WRAPPER_LDFLDA)
568 *method = mono_marshal_get_ldflda_wrapper (&klass->byval_arg);
569 else if (wrapper_type == MONO_WRAPPER_STFLD)
570 *method = mono_marshal_get_stfld_wrapper (&klass->byval_arg);
571 else if (wrapper_type == MONO_WRAPPER_ISINST)
572 *method = mono_marshal_get_isinst (klass);
573 else
574 g_assert_not_reached ();
575 break;
577 case MONO_WRAPPER_LDFLD_REMOTE:
578 *method = mono_marshal_get_ldfld_remote_wrapper (NULL);
579 break;
580 case MONO_WRAPPER_STFLD_REMOTE:
581 *method = mono_marshal_get_stfld_remote_wrapper (NULL);
582 break;
583 case MONO_WRAPPER_ALLOC: {
584 int atype = decode_value (p, &p);
586 *method = mono_gc_get_managed_allocator_by_type (atype);
587 break;
589 case MONO_WRAPPER_WRITE_BARRIER:
590 *method = mono_gc_get_write_barrier ();
591 break;
592 case MONO_WRAPPER_STELEMREF:
593 *method = mono_marshal_get_stelemref ();
594 break;
595 case MONO_WRAPPER_SYNCHRONIZED: {
596 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
598 if (!m)
599 return NULL;
600 *method = mono_marshal_get_synchronized_wrapper (m);
601 break;
603 case MONO_WRAPPER_UNKNOWN: {
604 MonoMethodDesc *desc;
605 MonoMethod *orig_method;
606 int subtype = decode_value (p, &p);
608 if (subtype == MONO_AOT_WRAPPER_MONO_ENTER)
609 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
610 else if (subtype == MONO_AOT_WRAPPER_MONO_EXIT)
611 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
612 else
613 g_assert_not_reached ();
614 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
615 g_assert (orig_method);
616 mono_method_desc_free (desc);
617 *method = mono_monitor_get_fast_path (orig_method);
618 break;
620 case MONO_WRAPPER_RUNTIME_INVOKE: {
621 /* Direct wrapper */
622 MonoMethod *m = decode_resolve_method_ref (module, p, &p);
624 if (!m)
625 return NULL;
626 *method = mono_marshal_get_runtime_invoke (m, FALSE);
627 break;
629 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
630 int subtype = decode_value (p, &p);
632 if (subtype == MONO_AOT_WRAPPER_ELEMENT_ADDR) {
633 int rank = decode_value (p, &p);
634 int elem_size = decode_value (p, &p);
636 *method = mono_marshal_get_array_address (rank, elem_size);
637 } else {
638 g_assert_not_reached ();
640 break;
642 default:
643 g_assert_not_reached ();
645 } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
646 /* Can't decode these */
647 g_assert_not_reached ();
648 } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
649 image_index = decode_value (p, &p);
650 *token = decode_value (p, &p);
652 image = load_image (module, image_index, TRUE);
653 if (!image)
654 return NULL;
655 } else if (image_index == MONO_AOT_METHODREF_GINST) {
656 MonoClass *klass;
657 MonoGenericContext ctx;
660 * These methods do not have a token which resolves them, so we
661 * resolve them immediately.
663 klass = decode_klass_ref (module, p, &p);
664 if (!klass)
665 return NULL;
667 image_index = decode_value (p, &p);
668 *token = decode_value (p, &p);
670 image = load_image (module, image_index, TRUE);
671 if (!image)
672 return NULL;
674 *method = mono_get_method_full (image, *token, NULL, NULL);
675 if (!(*method))
676 return NULL;
678 memset (&ctx, 0, sizeof (ctx));
680 if (FALSE && klass->generic_class) {
681 ctx.class_inst = klass->generic_class->context.class_inst;
682 ctx.method_inst = NULL;
684 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
687 memset (&ctx, 0, sizeof (ctx));
689 if (!decode_generic_context (module, &ctx, p, &p))
690 return NULL;
692 *method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
693 } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
694 MonoClass *klass;
695 int method_type;
697 klass = decode_klass_ref (module, p, &p);
698 if (!klass)
699 return NULL;
700 method_type = decode_value (p, &p);
701 *token = 0;
702 switch (method_type) {
703 case 0:
704 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
705 break;
706 case 1:
707 *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
708 break;
709 case 2:
710 *method = mono_class_get_method_from_name (klass, "Get", -1);
711 break;
712 case 3:
713 *method = mono_class_get_method_from_name (klass, "Address", -1);
714 break;
715 case 4:
716 *method = mono_class_get_method_from_name (klass, "Set", -1);
717 break;
718 default:
719 g_assert_not_reached ();
721 } else {
722 g_assert (image_index < MONO_AOT_METHODREF_MIN);
723 *token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
725 image = load_image (module, image_index, TRUE);
726 if (!image)
727 return NULL;
730 *endbuf = p;
732 return image;
736 * decode_resolve_method_ref:
738 * Similar to decode_method_ref, but resolve and return the method itself.
740 static MonoMethod*
741 decode_resolve_method_ref (MonoAotModule *module, guint8 *buf, guint8 **endbuf)
743 MonoMethod *method;
744 guint32 token;
745 MonoImage *image = decode_method_ref (module, &token, &method, NULL, buf, endbuf);
747 if (method)
748 return method;
749 if (!image)
750 return NULL;
751 method = mono_get_method (image, token, NULL);
752 return method;
755 static void
756 create_cache_structure (void)
758 const char *home;
759 char *tmp;
760 int err;
762 home = g_get_home_dir ();
763 if (!home)
764 return;
766 tmp = g_build_filename (home, ".mono", NULL);
767 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
768 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
769 #ifdef HOST_WIN32
770 err = mkdir (tmp);
771 #else
772 err = mkdir (tmp, 0777);
773 #endif
774 if (err) {
775 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
776 g_free (tmp);
777 return;
780 g_free (tmp);
781 tmp = g_build_filename (home, ".mono", "aot-cache", NULL);
782 if (!g_file_test (tmp, G_FILE_TEST_IS_DIR)) {
783 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT creating directory %s", tmp);
784 #ifdef HOST_WIN32
785 err = mkdir (tmp);
786 #else
787 err = mkdir (tmp, 0777);
788 #endif
789 if (err) {
790 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed: %s", g_strerror (errno));
791 g_free (tmp);
792 return;
795 g_free (tmp);
799 * load_aot_module_from_cache:
801 * Experimental code to AOT compile loaded assemblies on demand.
803 * FIXME:
804 * - Add environment variable MONO_AOT_CACHE_OPTIONS
805 * - Add options for controlling the cache size
806 * - Handle full cache by deleting old assemblies lru style
807 * - Add options for excluding assemblies during development
808 * - Maybe add a threshold after an assembly is AOT compiled
809 * - invoking a new mono process is a security risk
810 * - recompile the AOT module if one of its dependencies changes
812 static MonoDl*
813 load_aot_module_from_cache (MonoAssembly *assembly, char **aot_name)
815 char *fname, *cmd, *tmp2, *aot_options;
816 const char *home;
817 MonoDl *module;
818 gboolean res;
819 gchar *out, *err;
820 gint exit_status;
822 *aot_name = NULL;
824 if (assembly->image->dynamic)
825 return NULL;
827 create_cache_structure ();
829 home = g_get_home_dir ();
831 tmp2 = g_strdup_printf ("%s-%s%s", assembly->image->assembly_name, assembly->image->guid, SHARED_EXT);
832 fname = g_build_filename (home, ".mono", "aot-cache", tmp2, NULL);
833 *aot_name = fname;
834 g_free (tmp2);
836 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT trying to load from cache: '%s'.", fname);
837 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
839 if (!module) {
840 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT not found.");
842 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT precompiling assembly '%s'... ", assembly->image->name);
844 aot_options = g_strdup_printf ("outfile=%s", fname);
846 if (spawn_compiler) {
847 /* FIXME: security */
848 /* FIXME: Has to pass the assembly loading path to the child process */
849 cmd = g_strdup_printf ("mono -O=all --aot=%s %s", aot_options, assembly->image->name);
851 res = g_spawn_command_line_sync (cmd, &out, &err, &exit_status, NULL);
853 #if !defined(HOST_WIN32) && !defined(__ppc__) && !defined(__ppc64__) && !defined(__powerpc__)
854 if (res) {
855 if (!WIFEXITED (exit_status) && (WEXITSTATUS (exit_status) == 0))
856 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed: %s.", err);
857 else
858 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
859 g_free (out);
860 g_free (err);
862 #endif
863 g_free (cmd);
864 } else {
865 res = mono_compile_assembly (assembly, mono_parse_default_optimizations (NULL), aot_options);
866 if (!res) {
867 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT failed.");
868 } else {
869 mono_trace (G_LOG_LEVEL_MESSAGE, MONO_TRACE_AOT, "AOT succeeded.");
873 module = mono_dl_open (fname, MONO_DL_LAZY, NULL);
875 g_free (aot_options);
878 return module;
881 static void
882 find_symbol (MonoDl *module, gpointer *globals, const char *name, gpointer *value)
884 if (globals) {
885 int global_index;
886 guint16 *table, *entry;
887 guint16 table_size;
888 guint32 hash;
890 /* The first entry points to the hash */
891 table = globals [0];
892 globals ++;
894 table_size = table [0];
895 table ++;
897 hash = mono_metadata_str_hash (name) % table_size;
899 entry = &table [hash * 2];
901 /* Search the hash for the index into the globals table */
902 global_index = -1;
903 while (entry [0] != 0) {
904 guint32 index = entry [0] - 1;
905 guint32 next = entry [1];
907 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
909 if (!strcmp (globals [index * 2], name)) {
910 global_index = index;
911 break;
914 if (next != 0) {
915 entry = &table [next * 2];
916 } else {
917 break;
921 if (global_index != -1)
922 *value = globals [global_index * 2 + 1];
923 else
924 *value = NULL;
925 } else {
926 char *err = mono_dl_symbol (module, name, value);
928 if (err)
929 g_free (err);
933 #if defined(HAVE_DL_ITERATE_PHDR) && defined(PT_GNU_EH_FRAME)
934 static int
935 dl_callback (struct dl_phdr_info *info, size_t size, void *data)
937 int j;
938 MonoAotModule *amodule = data;
940 if (!strcmp (amodule->aot_name, info->dlpi_name)) {
941 for (j = 0; j < info->dlpi_phnum; j++) {
942 if (info->dlpi_phdr [j].p_type == PT_GNU_EH_FRAME)
943 amodule->eh_frame_hdr = (guint8*)(info->dlpi_addr + info->dlpi_phdr [j].p_vaddr);
945 return 1;
946 } else {
947 return 0;
950 #endif
952 static void
953 load_aot_module (MonoAssembly *assembly, gpointer user_data)
955 char *aot_name;
956 MonoAotModule *amodule;
957 MonoDl *sofile;
958 gboolean usable = TRUE;
959 char *saved_guid = NULL;
960 char *aot_version = NULL;
961 char *runtime_version, *build_info;
962 char *opt_flags = NULL;
963 gpointer *globals;
964 gboolean full_aot = FALSE;
965 MonoAotFileInfo *file_info = NULL;
966 int i;
967 gpointer *got_addr;
968 guint8 *blob;
970 if (mono_compile_aot)
971 return;
973 if (assembly->image->aot_module)
975 * Already loaded. This can happen because the assembly loading code might invoke
976 * the assembly load hooks multiple times for the same assembly.
978 return;
980 if (assembly->image->dynamic)
981 return;
983 if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
984 return;
986 mono_aot_lock ();
987 if (static_aot_modules)
988 globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
989 else
990 globals = NULL;
991 mono_aot_unlock ();
993 if (globals) {
994 /* Statically linked AOT module */
995 sofile = NULL;
996 aot_name = g_strdup_printf ("%s", assembly->aname.name);
997 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "Found statically linked AOT module '%s'.\n", aot_name);
998 } else {
999 if (use_aot_cache)
1000 sofile = load_aot_module_from_cache (assembly, &aot_name);
1001 else {
1002 char *err;
1003 aot_name = g_strdup_printf ("%s%s", assembly->image->name, SHARED_EXT);
1005 sofile = mono_dl_open (aot_name, MONO_DL_LAZY, &err);
1007 if (!sofile) {
1008 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT failed to load AOT module %s: %s\n", aot_name, err);
1009 g_free (err);
1014 if (!sofile && !globals) {
1015 if (mono_aot_only) {
1016 fprintf (stderr, "Failed to load AOT module '%s' in aot-only mode.\n", aot_name);
1017 exit (1);
1019 g_free (aot_name);
1020 return;
1023 find_symbol (sofile, globals, "mono_assembly_guid", (gpointer *) &saved_guid);
1024 find_symbol (sofile, globals, "mono_aot_version", (gpointer *) &aot_version);
1025 find_symbol (sofile, globals, "mono_aot_opt_flags", (gpointer *)&opt_flags);
1026 find_symbol (sofile, globals, "mono_runtime_version", (gpointer *)&runtime_version);
1027 find_symbol (sofile, globals, "mono_aot_got_addr", (gpointer *)&got_addr);
1029 if (!aot_version || strcmp (aot_version, MONO_AOT_FILE_VERSION)) {
1030 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);
1031 usable = FALSE;
1033 else {
1034 if (!saved_guid || strcmp (assembly->image->guid, saved_guid)) {
1035 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is out of date.\n", aot_name);
1036 usable = FALSE;
1040 build_info = mono_get_runtime_build_info ();
1041 if (!runtime_version || ((strlen (runtime_version) > 0 && strcmp (runtime_version, build_info)))) {
1042 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);
1043 usable = FALSE;
1045 g_free (build_info);
1047 find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
1048 g_assert (file_info);
1050 full_aot = ((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_FULL_AOT;
1052 if (mono_aot_only && !full_aot) {
1053 fprintf (stderr, "Can't use AOT image '%s' in aot-only mode because it is not compiled with --aot=full.\n", aot_name);
1054 exit (1);
1056 if (!mono_aot_only && full_aot) {
1057 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with --aot=full.\n", aot_name);
1058 usable = FALSE;
1061 /* This is no longer needed, LLVM and non-LLVM runtimes should be compatible.
1062 if ((((MonoAotFileInfo*)file_info)->flags & MONO_AOT_FILE_FLAG_WITH_LLVM) && !mono_use_llvm) {
1063 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled with LLVM.\n", aot_name);
1064 usable = FALSE;
1068 find_symbol (sofile, globals, "blob", (gpointer*)&blob);
1070 if (((MonoAotFileInfo*)file_info)->gc_name_index != -1) {
1071 char *gc_name = (char*)&blob [((MonoAotFileInfo*)file_info)->gc_name_index];
1072 const char *current_gc_name = mono_gc_get_gc_name ();
1074 if (strcmp (current_gc_name, gc_name) != 0) {
1075 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is compiled against GC %s, while the current runtime uses GC %s.\n", aot_name, gc_name, current_gc_name);
1076 usable = FALSE;
1080 if (!usable) {
1081 if (mono_aot_only) {
1082 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode.\n", aot_name);
1083 exit (1);
1084 } else {
1085 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT module %s is unusable.\n", aot_name);
1087 g_free (aot_name);
1088 if (sofile)
1089 mono_dl_close (sofile);
1090 assembly->image->aot_module = NULL;
1091 return;
1094 amodule = g_new0 (MonoAotModule, 1);
1095 amodule->aot_name = aot_name;
1096 amodule->assembly = assembly;
1098 memcpy (&amodule->info, file_info, sizeof (*file_info));
1100 amodule->got = *got_addr;
1101 amodule->got [0] = assembly->image;
1102 amodule->globals = globals;
1103 amodule->sofile = sofile;
1104 amodule->method_to_code = g_hash_table_new (mono_aligned_addr_hash, NULL);
1105 amodule->blob = blob;
1107 /* Read image table */
1109 guint32 table_len, i;
1110 char *table = NULL;
1112 find_symbol (sofile, globals, "mono_image_table", (gpointer *)&table);
1113 g_assert (table);
1115 table_len = *(guint32*)table;
1116 table += sizeof (guint32);
1117 amodule->image_table = g_new0 (MonoImage*, table_len);
1118 amodule->image_names = g_new0 (MonoAssemblyName, table_len);
1119 amodule->image_guids = g_new0 (char*, table_len);
1120 amodule->image_table_len = table_len;
1121 for (i = 0; i < table_len; ++i) {
1122 MonoAssemblyName *aname = &(amodule->image_names [i]);
1124 aname->name = g_strdup (table);
1125 table += strlen (table) + 1;
1126 amodule->image_guids [i] = g_strdup (table);
1127 table += strlen (table) + 1;
1128 if (table [0] != 0)
1129 aname->culture = g_strdup (table);
1130 table += strlen (table) + 1;
1131 memcpy (aname->public_key_token, table, strlen (table) + 1);
1132 table += strlen (table) + 1;
1134 table = ALIGN_PTR_TO (table, 8);
1135 aname->flags = *(guint32*)table;
1136 table += 4;
1137 aname->major = *(guint32*)table;
1138 table += 4;
1139 aname->minor = *(guint32*)table;
1140 table += 4;
1141 aname->build = *(guint32*)table;
1142 table += 4;
1143 aname->revision = *(guint32*)table;
1144 table += 4;
1148 /* Read method and method_info tables */
1149 find_symbol (sofile, globals, "code_offsets", (gpointer*)&amodule->code_offsets);
1150 find_symbol (sofile, globals, "methods", (gpointer*)&amodule->code);
1151 find_symbol (sofile, globals, "methods_end", (gpointer*)&amodule->code_end);
1152 find_symbol (sofile, globals, "method_info_offsets", (gpointer*)&amodule->method_info_offsets);
1153 find_symbol (sofile, globals, "ex_info_offsets", (gpointer*)&amodule->ex_info_offsets);
1154 find_symbol (sofile, globals, "class_info_offsets", (gpointer*)&amodule->class_info_offsets);
1155 find_symbol (sofile, globals, "class_name_table", (gpointer *)&amodule->class_name_table);
1156 find_symbol (sofile, globals, "extra_method_table", (gpointer *)&amodule->extra_method_table);
1157 find_symbol (sofile, globals, "extra_method_info_offsets", (gpointer *)&amodule->extra_method_info_offsets);
1158 find_symbol (sofile, globals, "got_info_offsets", (gpointer*)&amodule->got_info_offsets);
1159 find_symbol (sofile, globals, "specific_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_SPECIFIC]));
1160 find_symbol (sofile, globals, "static_rgctx_trampolines", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_STATIC_RGCTX]));
1161 find_symbol (sofile, globals, "imt_thunks", (gpointer*)&(amodule->trampolines [MONO_AOT_TRAMP_IMT_THUNK]));
1162 find_symbol (sofile, globals, "unwind_info", (gpointer)&amodule->unwind_info);
1163 find_symbol (sofile, globals, "mem_end", (gpointer*)&amodule->mem_end);
1165 amodule->mem_begin = amodule->code;
1167 find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
1168 find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
1170 if (make_unreadable) {
1171 #ifndef TARGET_WIN32
1172 guint8 *addr;
1173 guint8 *page_start, *page_end;
1174 int err, len;
1176 addr = amodule->mem_begin;
1177 len = amodule->mem_end - amodule->mem_begin;
1179 /* Round down in both directions to avoid modifying data which is not ours */
1180 page_start = (guint8 *) (((gssize) (addr)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
1181 page_end = (guint8 *) (((gssize) (addr + len)) & ~ (mono_pagesize () - 1));
1182 if (page_end > page_start) {
1183 err = mono_mprotect (page_start, (page_end - page_start), MONO_MMAP_NONE);
1184 g_assert (err == 0);
1186 #endif
1189 mono_aot_lock ();
1191 aot_code_low_addr = MIN (aot_code_low_addr, (gsize)amodule->code);
1192 aot_code_high_addr = MAX (aot_code_high_addr, (gsize)amodule->code_end);
1194 g_hash_table_insert (aot_modules, assembly, amodule);
1195 mono_aot_unlock ();
1197 mono_jit_info_add_aot_module (assembly->image, amodule->code, amodule->code_end);
1199 assembly->image->aot_module = amodule;
1201 #if defined(HAVE_DL_ITERATE_PHDR) && defined(PT_GNU_EH_FRAME)
1202 /* Lookup the address of the .eh_frame_hdr () section if available */
1203 dl_iterate_phdr (dl_callback, amodule);
1204 #endif
1206 if (mono_aot_only) {
1207 if (mono_defaults.corlib) {
1208 /* The second got slot contains the mscorlib got addr */
1209 MonoAotModule *mscorlib_amodule = mono_defaults.corlib->aot_module;
1211 amodule->got [1] = mscorlib_amodule->got;
1212 } else {
1213 amodule->got [1] = amodule->got;
1218 * Since we store methoddef and classdef tokens when referring to methods/classes in
1219 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
1220 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
1221 * non-lazily, since we can't handle out-of-date errors later.
1222 * The cached class info also depends on the exact assemblies.
1224 for (i = 0; i < amodule->image_table_len; ++i)
1225 load_image (amodule, i, FALSE);
1227 if (amodule->out_of_date) {
1228 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT Module %s is unusable because a dependency is out-of-date.\n", assembly->image->name);
1229 if (mono_aot_only) {
1230 fprintf (stderr, "Failed to load AOT module '%s' while running in aot-only mode because a dependency cannot be found or it is out of date.\n", aot_name);
1231 exit (1);
1234 else
1235 mono_trace (G_LOG_LEVEL_INFO, MONO_TRACE_AOT, "AOT loaded AOT Module for %s.\n", assembly->image->name);
1239 * mono_aot_register_globals:
1241 * This is called by the ctor function in AOT images compiled with the
1242 * 'no-dlsym' option.
1244 void
1245 mono_aot_register_globals (gpointer *globals)
1247 g_assert_not_reached ();
1251 * mono_aot_register_module:
1253 * This should be called by embedding code to register AOT modules statically linked
1254 * into the executable. AOT_INFO should be the value of the
1255 * 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
1257 void
1258 mono_aot_register_module (gpointer *aot_info)
1260 gpointer *globals;
1261 char *aname;
1263 globals = aot_info;
1264 g_assert (globals);
1266 /* Determine the assembly name */
1267 find_symbol (NULL, globals, "mono_aot_assembly_name", (gpointer*)&aname);
1268 g_assert (aname);
1270 /* This could be called before startup */
1271 if (aot_modules)
1272 mono_aot_lock ();
1274 if (!static_aot_modules)
1275 static_aot_modules = g_hash_table_new (g_str_hash, g_str_equal);
1277 g_hash_table_insert (static_aot_modules, aname, globals);
1279 if (aot_modules)
1280 mono_aot_unlock ();
1283 void
1284 mono_aot_init (void)
1286 InitializeCriticalSection (&aot_mutex);
1287 aot_modules = g_hash_table_new (NULL, NULL);
1289 mono_install_assembly_load_hook (load_aot_module, NULL);
1291 if (g_getenv ("MONO_LASTAOT"))
1292 mono_last_aot_method = atoi (g_getenv ("MONO_LASTAOT"));
1293 if (g_getenv ("MONO_AOT_CACHE"))
1294 use_aot_cache = TRUE;
1297 void
1298 mono_aot_cleanup (void)
1300 if (aot_jit_icall_hash)
1301 g_hash_table_destroy (aot_jit_icall_hash);
1302 if (aot_modules)
1303 g_hash_table_destroy (aot_modules);
1306 static gboolean
1307 decode_cached_class_info (MonoAotModule *module, MonoCachedClassInfo *info, guint8 *buf, guint8 **endbuf)
1309 guint32 flags;
1311 info->vtable_size = decode_value (buf, &buf);
1312 if (info->vtable_size == -1)
1313 /* Generic type */
1314 return FALSE;
1315 flags = decode_value (buf, &buf);
1316 info->ghcimpl = (flags >> 0) & 0x1;
1317 info->has_finalize = (flags >> 1) & 0x1;
1318 info->has_cctor = (flags >> 2) & 0x1;
1319 info->has_nested_classes = (flags >> 3) & 0x1;
1320 info->blittable = (flags >> 4) & 0x1;
1321 info->has_references = (flags >> 5) & 0x1;
1322 info->has_static_refs = (flags >> 6) & 0x1;
1323 info->no_special_static_fields = (flags >> 7) & 0x1;
1324 info->is_generic_container = (flags >> 8) & 0x1;
1326 if (info->has_cctor) {
1327 MonoImage *cctor_image = decode_method_ref (module, &info->cctor_token, NULL, NULL, buf, &buf);
1328 if (!cctor_image)
1329 return FALSE;
1331 if (info->has_finalize) {
1332 info->finalize_image = decode_method_ref (module, &info->finalize_token, NULL, NULL, buf, &buf);
1333 if (!info->finalize_image)
1334 return FALSE;
1337 info->instance_size = decode_value (buf, &buf);
1338 info->class_size = decode_value (buf, &buf);
1339 info->packing_size = decode_value (buf, &buf);
1340 info->min_align = decode_value (buf, &buf);
1342 *endbuf = buf;
1344 return TRUE;
1347 gpointer
1348 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
1350 int i;
1351 MonoClass *klass = vtable->klass;
1352 MonoAotModule *amodule = klass->image->aot_module;
1353 guint8 *info, *p;
1354 MonoCachedClassInfo class_info;
1355 gboolean err;
1356 guint32 token;
1357 MonoImage *image;
1358 gboolean no_aot_trampoline;
1360 if (MONO_CLASS_IS_INTERFACE (klass) || klass->rank || !amodule)
1361 return NULL;
1363 info = &amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1364 p = info;
1366 err = decode_cached_class_info (amodule, &class_info, p, &p);
1367 if (!err)
1368 return NULL;
1370 for (i = 0; i < slot; ++i)
1371 decode_method_ref (amodule, &token, NULL, NULL, p, &p);
1373 image = decode_method_ref (amodule, &token, NULL, &no_aot_trampoline, p, &p);
1374 if (!image)
1375 return NULL;
1376 if (no_aot_trampoline)
1377 return NULL;
1379 if (mono_metadata_token_index (token) == 0)
1380 return NULL;
1382 return mono_aot_get_method_from_token (domain, image, token);
1385 gboolean
1386 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
1388 MonoAotModule *amodule = klass->image->aot_module;
1389 guint8 *p;
1390 gboolean err;
1392 if (klass->rank || !amodule)
1393 return FALSE;
1395 p = (guint8*)&amodule->blob [mono_aot_get_offset (amodule->class_info_offsets, mono_metadata_token_index (klass->type_token) - 1)];
1397 err = decode_cached_class_info (amodule, res, p, &p);
1398 if (!err)
1399 return FALSE;
1401 return TRUE;
1405 * mono_aot_get_class_from_name:
1407 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
1408 * using a cache stored in the AOT file.
1409 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
1411 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
1412 * found.
1414 gboolean
1415 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
1417 MonoAotModule *amodule = image->aot_module;
1418 guint16 *table, *entry;
1419 guint16 table_size;
1420 guint32 hash;
1421 char full_name_buf [1024];
1422 char *full_name;
1423 const char *name2, *name_space2;
1424 MonoTableInfo *t;
1425 guint32 cols [MONO_TYPEDEF_SIZE];
1426 GHashTable *nspace_table;
1428 if (!amodule || !amodule->class_name_table)
1429 return FALSE;
1431 mono_aot_lock ();
1433 *klass = NULL;
1435 /* First look in the cache */
1436 if (!amodule->name_cache)
1437 amodule->name_cache = g_hash_table_new (g_str_hash, g_str_equal);
1438 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
1439 if (nspace_table) {
1440 *klass = g_hash_table_lookup (nspace_table, name);
1441 if (*klass) {
1442 mono_aot_unlock ();
1443 return TRUE;
1447 table_size = amodule->class_name_table [0];
1448 table = amodule->class_name_table + 1;
1450 if (name_space [0] == '\0')
1451 full_name = g_strdup_printf ("%s", name);
1452 else {
1453 if (strlen (name_space) + strlen (name) < 1000) {
1454 sprintf (full_name_buf, "%s.%s", name_space, name);
1455 full_name = full_name_buf;
1456 } else {
1457 full_name = g_strdup_printf ("%s.%s", name_space, name);
1460 hash = mono_metadata_str_hash (full_name) % table_size;
1461 if (full_name != full_name_buf)
1462 g_free (full_name);
1464 entry = &table [hash * 2];
1466 if (entry [0] != 0) {
1467 t = &image->tables [MONO_TABLE_TYPEDEF];
1469 while (TRUE) {
1470 guint32 index = entry [0];
1471 guint32 next = entry [1];
1472 guint32 token = mono_metadata_make_token (MONO_TABLE_TYPEDEF, index);
1474 name_table_accesses ++;
1476 mono_metadata_decode_row (t, index - 1, cols, MONO_TYPEDEF_SIZE);
1478 name2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAME]);
1479 name_space2 = mono_metadata_string_heap (image, cols [MONO_TYPEDEF_NAMESPACE]);
1481 if (!strcmp (name, name2) && !strcmp (name_space, name_space2)) {
1482 mono_aot_unlock ();
1483 *klass = mono_class_get (image, token);
1485 /* Add to cache */
1486 if (*klass) {
1487 mono_aot_lock ();
1488 nspace_table = g_hash_table_lookup (amodule->name_cache, name_space);
1489 if (!nspace_table) {
1490 nspace_table = g_hash_table_new (g_str_hash, g_str_equal);
1491 g_hash_table_insert (amodule->name_cache, (char*)name_space2, nspace_table);
1493 g_hash_table_insert (nspace_table, (char*)name2, *klass);
1494 mono_aot_unlock ();
1496 return TRUE;
1499 if (next != 0) {
1500 entry = &table [next * 2];
1501 } else {
1502 break;
1507 mono_aot_unlock ();
1509 return TRUE;
1512 #define DW_EH_PE_omit 0xff
1513 #define DW_EH_PE_uleb128 0x01
1514 #define DW_EH_PE_udata2 0x02
1515 #define DW_EH_PE_udata4 0x03
1516 #define DW_EH_PE_udata8 0x04
1517 #define DW_EH_PE_sleb128 0x09
1518 #define DW_EH_PE_sdata2 0x0A
1519 #define DW_EH_PE_sdata4 0x0B
1520 #define DW_EH_PE_sdata8 0x0C
1522 #define DW_EH_PE_absptr 0x00
1523 #define DW_EH_PE_pcrel 0x10
1524 #define DW_EH_PE_datarel 0x30
1525 #define DW_EH_PE_omit 0xff
1527 typedef struct
1529 guint8 version;
1530 guint8 eh_frame_ptr_enc;
1531 guint8 fde_count_enc;
1532 guint8 table_enc;
1533 guint8 rest;
1534 } eh_frame_hdr;
1537 * decode_eh_frame:
1539 * Decode the exception handling information in the .eh_frame section of the AOT
1540 * file belong to CODE, and construct a MonoJitInfo structure from it.
1541 * LOCKING: Acquires the domain lock.
1543 static G_GNUC_UNUSED MonoJitInfo*
1544 decode_eh_frame (MonoAotModule *amodule, MonoDomain *domain,
1545 MonoMethod *method, guint8 *code,
1546 MonoJitExceptionInfo *clauses, int num_clauses,
1547 int extra_size, GSList **nesting,
1548 int *this_reg, int *this_offset)
1550 eh_frame_hdr *hdr;
1551 guint8 *p;
1552 guint8 *eh_frame, *unwind_info;
1553 guint32 eh_frame_ptr;
1554 int fde_count;
1555 gint32 *table;
1556 int i, j, pos, left, right, offset, offset1, offset2;
1557 guint32 unw_len, code_len;
1558 MonoJitExceptionInfo *ei;
1559 guint32 ei_len, nested_len, nindex;
1560 gpointer *type_info;
1561 MonoJitInfo *jinfo;
1563 g_assert (amodule->eh_frame_hdr);
1565 // http://refspecs.freestandards.org/LSB_1.3.0/gLSB/gLSB/ehframehdr.html
1566 hdr = (eh_frame_hdr*)amodule->eh_frame_hdr;
1567 g_assert (hdr->version == 1);
1568 g_assert (hdr->eh_frame_ptr_enc == (DW_EH_PE_pcrel | DW_EH_PE_sdata4));
1569 g_assert (hdr->fde_count_enc == DW_EH_PE_udata4);
1570 g_assert (hdr->table_enc == (DW_EH_PE_datarel | DW_EH_PE_sdata4));
1572 p = &(hdr->rest);
1573 eh_frame_ptr = *(guint32*)p;
1574 p += 4;
1575 fde_count = *(guint32*)p;
1576 p += 4;
1577 table = (gint32*)p;
1579 /* Binary search in the table to find the entry for code */
1580 offset = code - amodule->eh_frame_hdr;
1582 left = 0;
1583 right = fde_count;
1584 while (TRUE) {
1585 pos = (left + right) / 2;
1587 offset1 = table [(pos * 2)];
1588 if (pos + 1 == fde_count)
1589 /* FIXME: */
1590 offset2 = amodule->code_end - amodule->code;
1591 else
1592 offset2 = table [(pos + 1) * 2];
1594 if (offset < offset1)
1595 right = pos;
1596 else if (offset >= offset2)
1597 left = pos + 1;
1598 else
1599 break;
1602 g_assert (code >= amodule->eh_frame_hdr + table [(pos * 2)]);
1603 if (pos + 1 < fde_count)
1604 g_assert (code < amodule->eh_frame_hdr + table [(pos * 2) + 2]);
1606 eh_frame = amodule->eh_frame_hdr + table [(pos * 2) + 1];
1608 unwind_info = mono_unwind_decode_fde (eh_frame, &unw_len, &code_len, &ei, &ei_len, &type_info, this_reg, this_offset);
1610 /* Count number of nested clauses */
1611 nested_len = 0;
1612 for (i = 0; i < ei_len; ++i) {
1613 gint32 cindex1 = *(gint32*)type_info [i];
1614 GSList *l;
1616 for (l = nesting [cindex1]; l; l = l->next) {
1617 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
1619 for (j = 0; j < ei_len; ++j) {
1620 gint32 cindex2 = *(gint32*)type_info [j];
1622 if (cindex2 == nesting_cindex)
1623 nested_len ++;
1629 * LLVM might represent one IL region with multiple regions, so have to
1630 * allocate a new JI.
1632 jinfo =
1633 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * (ei_len + nested_len)) + extra_size);
1635 jinfo->code_size = code_len;
1636 jinfo->used_regs = mono_cache_unwind_info (unwind_info, unw_len);
1637 jinfo->method = method;
1638 jinfo->code_start = code;
1639 jinfo->domain_neutral = 0;
1640 /* This signals that used_regs points to a normal cached unwind info */
1641 jinfo->from_aot = 0;
1642 jinfo->num_clauses = ei_len + nested_len;
1644 for (i = 0; i < ei_len; ++i) {
1646 * orig_jinfo contains the original IL exception info saved by the AOT
1647 * compiler, we have to combine that with the information produced by LLVM
1649 /* The type_info entries contain IL clause indexes */
1650 int clause_index = *(gint32*)type_info [i];
1651 MonoJitExceptionInfo *jei = &jinfo->clauses [i];
1652 MonoJitExceptionInfo *orig_jei = &clauses [clause_index];
1654 g_assert (clause_index < num_clauses);
1655 jei->flags = orig_jei->flags;
1656 jei->data.catch_class = orig_jei->data.catch_class;
1658 jei->try_start = ei [i].try_start;
1659 jei->try_end = ei [i].try_end;
1660 jei->handler_start = ei [i].handler_start;
1663 /* See exception_cb () in mini-llvm.c as to why this is needed */
1664 nindex = ei_len;
1665 for (i = 0; i < ei_len; ++i) {
1666 gint32 cindex1 = *(gint32*)type_info [i];
1667 GSList *l;
1669 for (l = nesting [cindex1]; l; l = l->next) {
1670 gint32 nesting_cindex = GPOINTER_TO_INT (l->data);
1672 for (j = 0; j < ei_len; ++j) {
1673 gint32 cindex2 = *(gint32*)type_info [j];
1675 if (cindex2 == nesting_cindex) {
1677 * The try interval comes from the nested clause, everything else from the
1678 * nesting clause.
1680 memcpy (&jinfo->clauses [nindex], &jinfo->clauses [j], sizeof (MonoJitExceptionInfo));
1681 jinfo->clauses [nindex].try_start = jinfo->clauses [i].try_start;
1682 jinfo->clauses [nindex].try_end = jinfo->clauses [i].try_end;
1683 nindex ++;
1688 g_assert (nindex == ei_len + nested_len);
1690 return jinfo;
1694 * LOCKING: Acquires the domain lock.
1696 static MonoJitInfo*
1697 decode_exception_debug_info (MonoAotModule *amodule, MonoDomain *domain,
1698 MonoMethod *method, guint8* ex_info, guint8 *addr,
1699 guint8 *code, guint32 code_len)
1701 int i, buf_len, num_clauses;
1702 MonoJitInfo *jinfo;
1703 guint used_int_regs, flags;
1704 gboolean has_generic_jit_info, has_dwarf_unwind_info, has_clauses, has_seq_points, has_try_block_holes;
1705 gboolean from_llvm;
1706 guint8 *p;
1707 int generic_info_size, try_holes_info_size, num_holes, this_reg, this_offset;
1709 /* Load the method info from the AOT file */
1711 p = ex_info;
1712 flags = decode_value (p, &p);
1713 has_generic_jit_info = (flags & 1) != 0;
1714 has_dwarf_unwind_info = (flags & 2) != 0;
1715 has_clauses = (flags & 4) != 0;
1716 has_seq_points = (flags & 8) != 0;
1717 from_llvm = (flags & 16) != 0;
1718 has_try_block_holes = (flags & 32) != 0;
1720 if (has_dwarf_unwind_info) {
1721 guint32 offset;
1723 offset = decode_value (p, &p);
1724 g_assert (offset < (1 << 30));
1725 used_int_regs = offset;
1726 } else {
1727 used_int_regs = decode_value (p, &p);
1729 if (has_generic_jit_info)
1730 generic_info_size = sizeof (MonoGenericJitInfo);
1731 else
1732 generic_info_size = 0;
1734 if (has_try_block_holes) {
1735 num_holes = decode_value (p, &p);
1736 try_holes_info_size = sizeof (MonoTryBlockHoleTableJitInfo) + num_holes * sizeof (MonoTryBlockHoleJitInfo);
1737 } else {
1738 num_holes = try_holes_info_size = 0;
1740 /* Exception table */
1741 if (has_clauses)
1742 num_clauses = decode_value (p, &p);
1743 else
1744 num_clauses = 0;
1746 if (from_llvm) {
1747 MonoJitExceptionInfo *clauses;
1748 GSList **nesting;
1751 * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
1752 * section.
1754 clauses = g_new0 (MonoJitExceptionInfo, num_clauses);
1755 nesting = g_new0 (GSList*, num_clauses);
1757 for (i = 0; i < num_clauses; ++i) {
1758 MonoJitExceptionInfo *ei = &clauses [i];
1760 ei->flags = decode_value (p, &p);
1762 if (decode_value (p, &p))
1763 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1765 /* Read the list of nesting clauses */
1766 while (TRUE) {
1767 int nesting_index = decode_value (p, &p);
1768 if (nesting_index == -1)
1769 break;
1770 nesting [i] = g_slist_prepend (nesting [i], GINT_TO_POINTER (nesting_index));
1774 jinfo = decode_eh_frame (amodule, domain, method, code, clauses, num_clauses, generic_info_size + try_holes_info_size, nesting, &this_reg, &this_offset);
1775 jinfo->from_llvm = 1;
1777 g_free (clauses);
1778 for (i = 0; i < num_clauses; ++i)
1779 g_slist_free (nesting [i]);
1780 g_free (nesting);
1781 } else {
1782 jinfo =
1783 mono_domain_alloc0 (domain, MONO_SIZEOF_JIT_INFO + (sizeof (MonoJitExceptionInfo) * num_clauses) + generic_info_size + try_holes_info_size);
1784 jinfo->num_clauses = num_clauses;
1786 for (i = 0; i < jinfo->num_clauses; ++i) {
1787 MonoJitExceptionInfo *ei = &jinfo->clauses [i];
1789 ei->flags = decode_value (p, &p);
1791 ei->exvar_offset = decode_value (p, &p);
1793 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
1794 ei->data.filter = code + decode_value (p, &p);
1795 else {
1796 if (decode_value (p, &p))
1797 ei->data.catch_class = decode_klass_ref (amodule, p, &p);
1800 ei->try_start = code + decode_value (p, &p);
1801 ei->try_end = code + decode_value (p, &p);
1802 ei->handler_start = code + decode_value (p, &p);
1805 jinfo->code_size = code_len;
1806 jinfo->used_regs = used_int_regs;
1807 jinfo->method = method;
1808 jinfo->code_start = code;
1809 jinfo->domain_neutral = 0;
1810 jinfo->from_aot = 1;
1813 if (has_generic_jit_info) {
1814 MonoGenericJitInfo *gi;
1816 jinfo->has_generic_jit_info = 1;
1818 gi = mono_jit_info_get_generic_jit_info (jinfo);
1819 g_assert (gi);
1821 if (from_llvm) {
1822 gi->has_this = this_reg != -1;
1823 gi->this_reg = this_reg;
1824 gi->this_offset = this_offset;
1825 } else {
1826 gi->has_this = decode_value (p, &p);
1827 gi->this_reg = decode_value (p, &p);
1828 gi->this_offset = decode_value (p, &p);
1831 /* This currently contains no data */
1832 gi->generic_sharing_context = g_new0 (MonoGenericSharingContext, 1);
1834 jinfo->method = decode_resolve_method_ref (amodule, p, &p);
1837 if (has_try_block_holes) {
1838 MonoTryBlockHoleTableJitInfo *table;
1840 jinfo->has_try_block_holes = 1;
1842 table = mono_jit_info_get_try_block_hole_table_info (jinfo);
1843 g_assert (table);
1845 table->num_holes = (guint16)num_holes;
1846 for (i = 0; i < num_holes; ++i) {
1847 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
1848 hole->clause = decode_value (p, &p);
1849 hole->length = decode_value (p, &p);
1850 hole->offset = decode_value (p, &p);
1854 if (has_seq_points) {
1855 MonoSeqPointInfo *seq_points;
1856 int il_offset, native_offset, last_il_offset, last_native_offset, j;
1858 int len = decode_value (p, &p);
1860 seq_points = g_malloc0 (sizeof (MonoSeqPointInfo) + (len - MONO_ZERO_LEN_ARRAY) * sizeof (SeqPoint));
1861 seq_points->len = len;
1862 last_il_offset = last_native_offset = 0;
1863 for (i = 0; i < len; ++i) {
1864 SeqPoint *sp = &seq_points->seq_points [i];
1865 il_offset = last_il_offset + decode_value (p, &p);
1866 native_offset = last_native_offset + decode_value (p, &p);
1868 sp->il_offset = il_offset;
1869 sp->native_offset = native_offset;
1871 sp->next_len = decode_value (p, &p);
1872 sp->next = g_new (int, sp->next_len);
1873 for (j = 0; j < sp->next_len; ++j)
1874 sp->next [j] = decode_value (p, &p);
1876 last_il_offset = il_offset;
1877 last_native_offset = native_offset;
1880 mono_domain_lock (domain);
1881 g_hash_table_insert (domain_jit_info (domain)->seq_points, method, seq_points);
1882 mono_domain_unlock (domain);
1885 /* Load debug info */
1886 buf_len = decode_value (p, &p);
1887 mono_debug_add_aot_method (domain, method, code, p, buf_len);
1889 if (amodule != jinfo->method->klass->image->aot_module) {
1890 mono_aot_lock ();
1891 if (!ji_to_amodule)
1892 ji_to_amodule = g_hash_table_new (NULL, NULL);
1893 g_hash_table_insert (ji_to_amodule, jinfo, amodule);
1894 mono_aot_unlock ();
1897 return jinfo;
1901 * mono_aot_get_unwind_info:
1903 * Return a pointer to the DWARF unwind info belonging to JI.
1905 guint8*
1906 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
1908 MonoAotModule *amodule = ji->method->klass->image->aot_module;
1909 guint8 *p;
1910 guint8 *code = ji->code_start;
1912 g_assert (amodule);
1913 g_assert (ji->from_aot);
1915 if (!(code >= amodule->code && code <= amodule->code_end)) {
1916 /* ji belongs to a different aot module than amodule */
1917 mono_aot_lock ();
1918 g_assert (ji_to_amodule);
1919 amodule = g_hash_table_lookup (ji_to_amodule, ji);
1920 g_assert (amodule);
1921 g_assert (code >= amodule->code && code <= amodule->code_end);
1922 mono_aot_unlock ();
1925 p = amodule->unwind_info + ji->used_regs;
1926 *unwind_info_len = decode_value (p, &p);
1927 return p;
1930 static G_GNUC_UNUSED int
1931 compare_ints (const void *a, const void *b)
1933 return *(gint32*)a - *(gint32*)b;
1936 static void
1937 msort_code_offsets_internal (gint32 *array, int lo, int hi, gint32 *scratch)
1939 int mid = (lo + hi) / 2;
1940 int i, t_lo, t_hi;
1942 if (lo >= hi)
1943 return;
1945 if (hi - lo < 32) {
1946 for (i = lo; i < hi; ++i)
1947 if (array [(i * 2)] > array [(i * 2) + 2])
1948 break;
1949 if (i == hi)
1950 /* Already sorted */
1951 return;
1954 msort_code_offsets_internal (array, lo, mid, scratch);
1955 msort_code_offsets_internal (array, mid + 1, hi, scratch);
1957 if (array [mid * 2] < array [(mid + 1) * 2])
1958 return;
1960 /* Merge */
1961 t_lo = lo;
1962 t_hi = mid + 1;
1963 for (i = lo; i <= hi; i ++) {
1964 if (t_lo <= mid && ((t_hi > hi) || array [t_lo * 2] < array [t_hi * 2])) {
1965 scratch [(i * 2)] = array [t_lo * 2];
1966 scratch [(i * 2) + 1] = array [(t_lo *2) + 1];
1967 t_lo ++;
1968 } else {
1969 scratch [(i * 2)] = array [t_hi * 2];
1970 scratch [(i * 2) + 1] = array [(t_hi *2) + 1];
1971 t_hi ++;
1974 for (i = lo; i <= hi; ++i) {
1975 array [(i * 2)] = scratch [i * 2];
1976 array [(i * 2) + 1] = scratch [(i * 2) + 1];
1980 static void
1981 msort_code_offsets (gint32 *array, int len)
1983 gint32 *scratch;
1985 scratch = g_new (gint32, len * 2);
1986 msort_code_offsets_internal (array, 0, len - 1, scratch);
1987 g_free (scratch);
1990 MonoJitInfo *
1991 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
1993 int pos, left, right, offset, offset1, offset2, code_len;
1994 int method_index, table_len, is_wrapper;
1995 guint32 token;
1996 MonoAotModule *amodule = image->aot_module;
1997 MonoMethod *method;
1998 MonoJitInfo *jinfo;
1999 guint8 *code, *ex_info, *p;
2000 guint32 *table;
2001 int nmethods = amodule->info.nmethods;
2002 gint32 *code_offsets;
2003 int offsets_len, i;
2005 if (!amodule)
2006 return NULL;
2008 if (domain != mono_get_root_domain ())
2009 /* FIXME: */
2010 return NULL;
2012 offset = (guint8*)addr - amodule->code;
2014 /* Compute a sorted table mapping code offsets to method indexes. */
2015 if (!amodule->sorted_code_offsets) {
2017 code_offsets = g_new0 (gint32, nmethods * 2);
2018 offsets_len = 0;
2019 for (i = 0; i < nmethods; ++i) {
2020 /* Skip the -1 entries to speed up sorting */
2021 if (amodule->code_offsets [i] == 0xffffffff)
2022 continue;
2023 code_offsets [(offsets_len * 2)] = amodule->code_offsets [i];
2024 code_offsets [(offsets_len *2) + 1] = i;
2025 offsets_len ++;
2027 /* Use a merge sort as this is mostly sorted */
2028 msort_code_offsets (code_offsets, offsets_len);
2029 //qsort (code_offsets, offsets_len, sizeof (gint32) * 2, compare_ints);
2030 for (i = 0; i < offsets_len -1; ++i)
2031 g_assert (code_offsets [(i * 2)] <= code_offsets [(i + 1) * 2]);
2033 if (InterlockedCompareExchangePointer ((gpointer*)&amodule->sorted_code_offsets, code_offsets, NULL) != NULL)
2034 /* Somebody got in before us */
2035 g_free (code_offsets);
2036 amodule->sorted_code_offsets_len = offsets_len;
2039 code_offsets = amodule->sorted_code_offsets;
2040 offsets_len = amodule->sorted_code_offsets_len;
2042 /* Binary search in the sorted_code_offsets table */
2043 left = 0;
2044 right = offsets_len;
2045 while (TRUE) {
2046 pos = (left + right) / 2;
2048 offset1 = code_offsets [(pos * 2)];
2049 if (pos + 1 == offsets_len)
2050 offset2 = amodule->code_end - amodule->code;
2051 else
2052 offset2 = code_offsets [(pos + 1) * 2];
2054 if (offset < offset1)
2055 right = pos;
2056 else if (offset >= offset2)
2057 left = pos + 1;
2058 else
2059 break;
2062 g_assert (offset >= code_offsets [(pos * 2)]);
2063 if (pos + 1 < offsets_len)
2064 g_assert (offset < code_offsets [((pos + 1) * 2)]);
2065 method_index = code_offsets [(pos * 2) + 1];
2067 code = &amodule->code [amodule->code_offsets [method_index]];
2068 ex_info = &amodule->blob [mono_aot_get_offset (amodule->ex_info_offsets, method_index)];
2070 if (pos == offsets_len - 1)
2071 code_len = amodule->code_end - code;
2072 else
2073 code_len = code_offsets [(pos + 1) * 2] - code_offsets [pos * 2];
2075 g_assert ((guint8*)code <= (guint8*)addr && (guint8*)addr < (guint8*)code + code_len);
2077 /* Might be a wrapper/extra method */
2078 if (amodule->extra_methods) {
2079 mono_aot_lock ();
2080 method = g_hash_table_lookup (amodule->extra_methods, GUINT_TO_POINTER (method_index));
2081 mono_aot_unlock ();
2082 } else {
2083 method = NULL;
2086 if (!method) {
2087 if (method_index >= image->tables [MONO_TABLE_METHOD].rows) {
2089 * This is hit for extra methods which are called directly, so they are
2090 * not in amodule->extra_methods.
2092 table_len = amodule->extra_method_info_offsets [0];
2093 table = amodule->extra_method_info_offsets + 1;
2094 left = 0;
2095 right = table_len;
2096 pos = 0;
2098 /* Binary search */
2099 while (TRUE) {
2100 pos = ((left + right) / 2);
2102 g_assert (pos < table_len);
2104 if (table [pos * 2] < method_index)
2105 left = pos + 1;
2106 else if (table [pos * 2] > method_index)
2107 right = pos;
2108 else
2109 break;
2112 p = amodule->blob + table [(pos * 2) + 1];
2113 is_wrapper = decode_value (p, &p);
2114 g_assert (!is_wrapper);
2115 method = decode_resolve_method_ref (amodule, p, &p);
2116 g_assert (method);
2117 } else {
2118 token = mono_metadata_make_token (MONO_TABLE_METHOD, method_index + 1);
2119 method = mono_get_method (image, token, NULL);
2123 /* FIXME: */
2124 g_assert (method);
2126 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
2128 jinfo = decode_exception_debug_info (amodule, domain, method, ex_info, addr, code, code_len);
2130 g_assert ((guint8*)addr >= (guint8*)jinfo->code_start);
2131 g_assert ((guint8*)addr < (guint8*)jinfo->code_start + jinfo->code_size);
2133 /* Add it to the normal JitInfo tables */
2134 mono_jit_info_table_add (domain, jinfo);
2136 return jinfo;
2139 static gboolean
2140 decode_patch (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf)
2142 guint8 *p = buf;
2143 gpointer *table;
2144 MonoImage *image;
2145 int i;
2147 switch (ji->type) {
2148 case MONO_PATCH_INFO_METHOD:
2149 case MONO_PATCH_INFO_METHOD_JUMP:
2150 case MONO_PATCH_INFO_ICALL_ADDR:
2151 case MONO_PATCH_INFO_METHOD_RGCTX: {
2152 guint32 token;
2153 MonoMethod *method;
2154 gboolean no_aot_trampoline;
2156 image = decode_method_ref (aot_module, &token, &method, &no_aot_trampoline, p, &p);
2157 if (!image)
2158 goto cleanup;
2160 if (!method && !mono_aot_only && !no_aot_trampoline && (ji->type == MONO_PATCH_INFO_METHOD) && (mono_metadata_token_table (token) == MONO_TABLE_METHOD)) {
2161 ji->data.target = mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (image, token));
2162 ji->type = MONO_PATCH_INFO_ABS;
2164 else {
2165 if (method)
2166 ji->data.method = method;
2167 else
2168 ji->data.method = mono_get_method (image, token, NULL);
2169 g_assert (ji->data.method);
2170 mono_class_init (ji->data.method->klass);
2172 break;
2174 case MONO_PATCH_INFO_INTERNAL_METHOD:
2175 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2176 guint32 len = decode_value (p, &p);
2178 ji->data.name = (char*)p;
2179 p += len + 1;
2180 break;
2182 case MONO_PATCH_INFO_METHODCONST:
2183 /* Shared */
2184 ji->data.method = decode_resolve_method_ref (aot_module, p, &p);
2185 if (!ji->data.method)
2186 goto cleanup;
2187 break;
2188 case MONO_PATCH_INFO_VTABLE:
2189 case MONO_PATCH_INFO_CLASS:
2190 case MONO_PATCH_INFO_IID:
2191 case MONO_PATCH_INFO_ADJUSTED_IID:
2192 /* Shared */
2193 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2194 if (!ji->data.klass)
2195 goto cleanup;
2196 break;
2197 case MONO_PATCH_INFO_CLASS_INIT:
2198 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2199 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2200 if (!ji->data.klass)
2201 goto cleanup;
2202 break;
2203 case MONO_PATCH_INFO_IMAGE:
2204 ji->data.image = load_image (aot_module, decode_value (p, &p), TRUE);
2205 if (!ji->data.image)
2206 goto cleanup;
2207 break;
2208 case MONO_PATCH_INFO_FIELD:
2209 case MONO_PATCH_INFO_SFLDA:
2210 /* Shared */
2211 ji->data.field = decode_field_info (aot_module, p, &p);
2212 if (!ji->data.field)
2213 goto cleanup;
2214 break;
2215 case MONO_PATCH_INFO_SWITCH:
2216 ji->data.table = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoBBTable));
2217 ji->data.table->table_size = decode_value (p, &p);
2218 table = mono_domain_alloc (mono_domain_get (), sizeof (gpointer) * ji->data.table->table_size);
2219 ji->data.table->table = (MonoBasicBlock**)table;
2220 for (i = 0; i < ji->data.table->table_size; i++)
2221 table [i] = (gpointer)(gssize)decode_value (p, &p);
2222 break;
2223 case MONO_PATCH_INFO_R4: {
2224 guint32 val;
2226 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (float));
2227 val = decode_value (p, &p);
2228 *(float*)ji->data.target = *(float*)&val;
2229 break;
2231 case MONO_PATCH_INFO_R8: {
2232 guint32 val [2];
2233 guint64 v;
2235 ji->data.target = mono_domain_alloc0 (mono_domain_get (), sizeof (double));
2237 val [0] = decode_value (p, &p);
2238 val [1] = decode_value (p, &p);
2239 v = ((guint64)val [1] << 32) | ((guint64)val [0]);
2240 *(double*)ji->data.target = *(double*)&v;
2241 break;
2243 case MONO_PATCH_INFO_LDSTR:
2244 image = load_image (aot_module, decode_value (p, &p), TRUE);
2245 if (!image)
2246 goto cleanup;
2247 ji->data.token = mono_jump_info_token_new (mp, image, MONO_TOKEN_STRING + decode_value (p, &p));
2248 break;
2249 case MONO_PATCH_INFO_RVA:
2250 case MONO_PATCH_INFO_DECLSEC:
2251 case MONO_PATCH_INFO_LDTOKEN:
2252 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2253 /* Shared */
2254 image = load_image (aot_module, decode_value (p, &p), TRUE);
2255 if (!image)
2256 goto cleanup;
2257 ji->data.token = mono_jump_info_token_new (mp, image, decode_value (p, &p));
2259 ji->data.token->has_context = decode_value (p, &p);
2260 if (ji->data.token->has_context) {
2261 gboolean res = decode_generic_context (aot_module, &ji->data.token->context, p, &p);
2262 if (!res)
2263 goto cleanup;
2265 break;
2266 case MONO_PATCH_INFO_EXC_NAME:
2267 ji->data.klass = decode_klass_ref (aot_module, p, &p);
2268 if (!ji->data.klass)
2269 goto cleanup;
2270 ji->data.name = ji->data.klass->name;
2271 break;
2272 case MONO_PATCH_INFO_METHOD_REL:
2273 ji->data.offset = decode_value (p, &p);
2274 break;
2275 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2276 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2277 case MONO_PATCH_INFO_MONITOR_ENTER:
2278 case MONO_PATCH_INFO_MONITOR_EXIT:
2279 break;
2280 case MONO_PATCH_INFO_RGCTX_FETCH: {
2281 gboolean res;
2282 MonoJumpInfoRgctxEntry *entry;
2284 entry = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoRgctxEntry));
2285 entry->method = decode_resolve_method_ref (aot_module, p, &p);
2286 entry->in_mrgctx = decode_value (p, &p);
2287 entry->info_type = decode_value (p, &p);
2288 entry->data = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo));
2289 entry->data->type = decode_value (p, &p);
2291 res = decode_patch (aot_module, mp, entry->data, p, &p);
2292 if (!res)
2293 goto cleanup;
2294 ji->data.rgctx_entry = entry;
2295 break;
2297 case MONO_PATCH_INFO_SEQ_POINT_INFO:
2298 break;
2299 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE: {
2300 MonoJumpInfoImtTramp *imt_tramp = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfoImtTramp));
2302 imt_tramp->method = decode_resolve_method_ref (aot_module, p, &p);
2303 imt_tramp->vt_offset = decode_value (p, &p);
2305 ji->data.imt_tramp = imt_tramp;
2306 break;
2308 default:
2309 g_warning ("unhandled type %d", ji->type);
2310 g_assert_not_reached ();
2313 *endbuf = p;
2315 return TRUE;
2317 cleanup:
2318 return FALSE;
2321 static MonoJumpInfo*
2322 load_patch_info (MonoAotModule *aot_module, MonoMemPool *mp, int n_patches,
2323 guint32 **got_slots,
2324 guint8 *buf, guint8 **endbuf)
2326 MonoJumpInfo *patches;
2327 int pindex;
2328 guint8 *p;
2330 p = buf;
2332 patches = mono_mempool_alloc0 (mp, sizeof (MonoJumpInfo) * n_patches);
2334 *got_slots = g_malloc (sizeof (guint32) * n_patches);
2336 for (pindex = 0; pindex < n_patches; ++pindex) {
2337 MonoJumpInfo *ji = &patches [pindex];
2338 guint8 *shared_p;
2339 gboolean res;
2340 guint32 got_offset;
2342 got_offset = decode_value (p, &p);
2344 if (aot_module->got [got_offset]) {
2345 /* Already loaded */
2346 //printf ("HIT!\n");
2347 } else {
2348 shared_p = aot_module->blob + mono_aot_get_offset (aot_module->got_info_offsets, got_offset);
2350 ji->type = decode_value (shared_p, &shared_p);
2352 res = decode_patch (aot_module, mp, ji, shared_p, &shared_p);
2353 if (!res)
2354 goto cleanup;
2357 (*got_slots) [pindex] = got_offset;
2360 *endbuf = p;
2361 return patches;
2363 cleanup:
2364 g_free (*got_slots);
2365 *got_slots = NULL;
2367 return NULL;
2370 static void
2371 register_jump_target_got_slot (MonoDomain *domain, MonoMethod *method, gpointer *got_slot)
2374 * Jump addresses cannot be patched by the trampoline code since it
2375 * does not have access to the caller's address. Instead, we collect
2376 * the addresses of the GOT slots pointing to a method, and patch
2377 * them after the method has been compiled.
2379 MonoJitDomainInfo *info = domain_jit_info (domain);
2380 GSList *list;
2382 mono_domain_lock (domain);
2383 if (!info->jump_target_got_slot_hash)
2384 info->jump_target_got_slot_hash = g_hash_table_new (NULL, NULL);
2385 list = g_hash_table_lookup (info->jump_target_got_slot_hash, method);
2386 list = g_slist_prepend (list, got_slot);
2387 g_hash_table_insert (info->jump_target_got_slot_hash, method, list);
2388 mono_domain_unlock (domain);
2392 * load_method:
2394 * Load the method identified by METHOD_INDEX from the AOT image. Return a
2395 * pointer to the native code of the method, or NULL if not found.
2396 * METHOD might not be set if the caller only has the image/token info.
2398 static gpointer
2399 load_method (MonoDomain *domain, MonoAotModule *amodule, MonoImage *image, MonoMethod *method, guint32 token, int method_index)
2401 MonoClass *klass;
2402 gboolean from_plt = method == NULL;
2403 MonoMemPool *mp;
2404 int i, pindex, n_patches, used_strings;
2405 gboolean keep_patches = TRUE;
2406 guint8 *p;
2407 MonoJitInfo *jinfo = NULL;
2408 guint8 *code, *info;
2410 if (mono_profiler_get_events () & MONO_PROFILE_ENTER_LEAVE)
2411 return NULL;
2413 if ((domain != mono_get_root_domain ()) && (!(amodule->info.opts & MONO_OPT_SHARED)))
2414 /* Non shared AOT code can't be used in other appdomains */
2415 return NULL;
2417 if (amodule->out_of_date)
2418 return NULL;
2420 if (amodule->code_offsets [method_index] == 0xffffffff) {
2421 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2422 char *full_name;
2424 if (!method)
2425 method = mono_get_method (image, token, NULL);
2426 full_name = mono_method_full_name (method, TRUE);
2427 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2428 g_free (full_name);
2430 return NULL;
2433 code = &amodule->code [amodule->code_offsets [method_index]];
2435 info = &amodule->blob [mono_aot_get_offset (amodule->method_info_offsets, method_index)];
2437 mono_aot_lock ();
2438 if (!amodule->methods_loaded)
2439 amodule->methods_loaded = g_new0 (guint32, amodule->info.nmethods + 1);
2440 mono_aot_unlock ();
2442 if ((amodule->methods_loaded [method_index / 32] >> (method_index % 32)) & 0x1)
2443 return code;
2445 if (mono_last_aot_method != -1) {
2446 if (mono_jit_stats.methods_aot >= mono_last_aot_method)
2447 return NULL;
2448 else if (mono_jit_stats.methods_aot == mono_last_aot_method - 1) {
2449 if (!method)
2450 method = mono_get_method (image, token, NULL);
2451 if (method) {
2452 char *name = mono_method_full_name (method, TRUE);
2453 printf ("LAST AOT METHOD: %s.\n", name);
2454 g_free (name);
2455 } else {
2456 printf ("LAST AOT METHOD: %p %d\n", code, method_index);
2461 p = info;
2463 if (method) {
2464 klass = method->klass;
2465 decode_klass_ref (amodule, p, &p);
2466 } else {
2467 klass = decode_klass_ref (amodule, p, &p);
2470 if (amodule->info.opts & MONO_OPT_SHARED)
2471 used_strings = decode_value (p, &p);
2472 else
2473 used_strings = 0;
2475 for (i = 0; i < used_strings; i++) {
2476 guint token = decode_value (p, &p);
2477 mono_ldstr (mono_get_root_domain (), image, mono_metadata_token_index (token));
2480 if (amodule->info.opts & MONO_OPT_SHARED)
2481 keep_patches = FALSE;
2483 n_patches = decode_value (p, &p);
2485 keep_patches = FALSE;
2487 if (n_patches) {
2488 MonoJumpInfo *patches;
2489 guint32 *got_slots;
2491 if (keep_patches)
2492 mp = domain->mp;
2493 else
2494 mp = mono_mempool_new ();
2496 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
2497 if (patches == NULL)
2498 goto cleanup;
2500 for (pindex = 0; pindex < n_patches; ++pindex) {
2501 MonoJumpInfo *ji = &patches [pindex];
2503 if (!amodule->got [got_slots [pindex]]) {
2504 amodule->got [got_slots [pindex]] = mono_resolve_patch_target (method, domain, code, ji, TRUE);
2505 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2506 amodule->got [got_slots [pindex]] = mono_create_ftnptr (domain, amodule->got [got_slots [pindex]]);
2507 if (ji->type == MONO_PATCH_INFO_METHOD_JUMP)
2508 register_jump_target_got_slot (domain, ji->data.method, &(amodule->got [got_slots [pindex]]));
2510 ji->type = MONO_PATCH_INFO_NONE;
2513 g_free (got_slots);
2515 if (!keep_patches)
2516 mono_mempool_destroy (mp);
2519 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2520 char *full_name;
2522 if (!method)
2523 method = mono_get_method (image, token, NULL);
2525 full_name = mono_method_full_name (method, TRUE);
2527 if (!jinfo)
2528 jinfo = mono_aot_find_jit_info (domain, amodule->assembly->image, code);
2530 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);
2531 g_free (full_name);
2534 mono_aot_lock ();
2536 mono_jit_stats.methods_aot++;
2538 amodule->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
2540 init_plt (amodule);
2542 if (method && method->wrapper_type)
2543 g_hash_table_insert (amodule->method_to_code, method, code);
2545 mono_aot_unlock ();
2547 if (mono_profiler_get_events () & MONO_PROFILE_JIT_COMPILATION) {
2548 MonoJitInfo *jinfo;
2550 if (!method) {
2551 method = mono_get_method (image, token, NULL);
2552 g_assert (method);
2554 mono_profiler_method_jit (method);
2555 jinfo = mono_jit_info_table_find (domain, (char*)code);
2556 g_assert (jinfo);
2557 mono_profiler_method_end_jit (method, jinfo, MONO_PROFILE_OK);
2560 if (from_plt && klass && !klass->generic_container)
2561 mono_runtime_class_init (mono_class_vtable (domain, klass));
2563 return code;
2565 cleanup:
2566 /* FIXME: The space in domain->mp is wasted */
2567 if (amodule->info.opts & MONO_OPT_SHARED)
2568 /* No need to cache patches */
2569 mono_mempool_destroy (mp);
2571 if (jinfo)
2572 g_free (jinfo);
2574 return NULL;
2577 static guint32
2578 find_extra_method_in_amodule (MonoAotModule *amodule, MonoMethod *method, const char *name)
2580 guint32 table_size, entry_size, hash;
2581 guint32 *table, *entry;
2582 guint32 index;
2583 static guint32 n_extra_decodes;
2585 if (!amodule)
2586 return 0xffffff;
2588 table_size = amodule->extra_method_table [0];
2589 table = amodule->extra_method_table + 1;
2590 entry_size = 3;
2592 hash = mono_aot_method_hash (method) % table_size;
2594 entry = &table [hash * entry_size];
2596 if (entry [0] == 0)
2597 return 0xffffff;
2599 index = 0xffffff;
2600 while (TRUE) {
2601 guint32 key = entry [0];
2602 guint32 value = entry [1];
2603 guint32 next = entry [entry_size - 1];
2604 MonoMethod *m;
2605 guint8 *p;
2606 int is_wrapper_name;
2608 p = amodule->blob + key;
2609 is_wrapper_name = decode_value (p, &p);
2610 if (is_wrapper_name) {
2611 int wrapper_type = decode_value (p, &p);
2612 if (wrapper_type == method->wrapper_type && !strcmp (name, (char*)p)) {
2613 index = value;
2614 break;
2616 } else if (can_method_ref_match_method (amodule, p, method)) {
2617 mono_aot_lock ();
2618 if (!amodule->method_ref_to_method)
2619 amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
2620 m = g_hash_table_lookup (amodule->method_ref_to_method, p);
2621 mono_aot_unlock ();
2622 if (!m) {
2623 guint8 *orig_p = p;
2624 m = decode_resolve_method_ref (amodule, p, &p);
2625 if (m) {
2626 mono_aot_lock ();
2627 g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
2628 mono_aot_unlock ();
2631 if (m == method) {
2632 index = value;
2633 break;
2636 /* Special case: wrappers of shared generic methods */
2637 if (m && method->wrapper_type && m->wrapper_type == m->wrapper_type &&
2638 method->wrapper_type == MONO_WRAPPER_SYNCHRONIZED) {
2639 MonoMethod *w1 = mono_marshal_method_from_wrapper (method);
2640 MonoMethod *w2 = mono_marshal_method_from_wrapper (m);
2642 if (w1->is_inflated && ((MonoMethodInflated *)w1)->declaring == w2) {
2643 index = value;
2644 break;
2648 /* Methods decoded needlessly */
2650 if (m)
2651 printf ("%d %s %s\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
2653 n_extra_decodes ++;
2656 if (next != 0)
2657 entry = &table [next * entry_size];
2658 else
2659 break;
2662 return index;
2665 static void
2666 add_module_cb (gpointer key, gpointer value, gpointer user_data)
2668 g_ptr_array_add ((GPtrArray*)user_data, value);
2672 * find_extra_method:
2674 * Try finding METHOD in the extra_method table in all AOT images.
2675 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
2676 * module where the method was found.
2678 static guint32
2679 find_extra_method (MonoMethod *method, MonoAotModule **out_amodule)
2681 guint32 index;
2682 GPtrArray *modules;
2683 int i;
2684 char *name = NULL;
2686 if (method->wrapper_type)
2687 name = mono_aot_wrapper_name (method);
2689 /* Try the method's module first */
2690 *out_amodule = method->klass->image->aot_module;
2691 index = find_extra_method_in_amodule (method->klass->image->aot_module, method, name);
2692 if (index != 0xffffff) {
2693 g_free (name);
2694 return index;
2698 * Try all other modules.
2699 * This is needed because generic instances klass->image points to the image
2700 * containing the generic definition, but the native code is generated to the
2701 * AOT image which contains the reference.
2704 /* Make a copy to avoid doing the search inside the aot lock */
2705 modules = g_ptr_array_new ();
2706 mono_aot_lock ();
2707 g_hash_table_foreach (aot_modules, add_module_cb, modules);
2708 mono_aot_unlock ();
2710 index = 0xffffff;
2711 for (i = 0; i < modules->len; ++i) {
2712 MonoAotModule *amodule = g_ptr_array_index (modules, i);
2714 if (amodule != method->klass->image->aot_module)
2715 index = find_extra_method_in_amodule (amodule, method, name);
2716 if (index != 0xffffff) {
2717 *out_amodule = amodule;
2718 break;
2722 g_ptr_array_free (modules, TRUE);
2724 g_free (name);
2725 return index;
2729 * mono_aot_get_method:
2731 * Return a pointer to the AOTed native code for METHOD if it can be found,
2732 * NULL otherwise.
2733 * On platforms with function pointers, this doesn't return a function pointer.
2735 gpointer
2736 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
2738 MonoClass *klass = method->klass;
2739 guint32 method_index;
2740 MonoAotModule *amodule = klass->image->aot_module;
2741 guint8 *code;
2743 if (!amodule)
2744 return NULL;
2746 if (amodule->out_of_date)
2747 return NULL;
2749 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2750 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2751 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2752 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2753 return NULL;
2756 * Use the original method instead of its invoke-with-check wrapper.
2757 * This is not a problem when using full-aot, since it doesn't support
2758 * remoting.
2760 if (mono_aot_only && method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
2761 return mono_aot_get_method (domain, mono_marshal_method_from_wrapper (method));
2763 g_assert (klass->inited);
2765 /* Find method index */
2766 if (method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE)) {
2768 * For generic methods, we store the fully shared instance in place of the
2769 * original method.
2771 method = mono_method_get_declaring_generic_method (method);
2772 method_index = mono_metadata_token_index (method->token) - 1;
2773 } else if (method->is_inflated || !method->token) {
2774 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
2775 mono_aot_lock ();
2776 code = g_hash_table_lookup (amodule->method_to_code, method);
2777 mono_aot_unlock ();
2778 if (code)
2779 return code;
2781 method_index = find_extra_method (method, &amodule);
2783 * Special case the ICollection<T> wrappers for arrays, as they cannot
2784 * be statically enumerated, and each wrapper ends up calling the same
2785 * method in Array.
2787 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && method->klass->rank && strstr (method->name, "System.Collections.Generic")) {
2788 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
2790 code = mono_aot_get_method (domain, m);
2791 if (code) {
2792 if (mono_method_needs_static_rgctx_invoke (m, FALSE)) {
2793 code = mono_create_static_rgctx_trampoline (m, mono_create_ftnptr (domain, code));
2794 /* The call above returns an ftnptr */
2795 code = mono_get_addr_from_ftnptr (code);
2798 return code;
2803 * Special case Array.GetGenericValueImpl which is a generic icall.
2804 * Generic sharing currently can't handle it, but the icall returns data using
2805 * an out parameter, so the managed-to-native wrappers can share the same code.
2807 if (method_index == 0xffffff && method->wrapper_type == MONO_WRAPPER_MANAGED_TO_NATIVE && method->klass == mono_defaults.array_class && !strcmp (method->name, "GetGenericValueImpl")) {
2808 MonoMethod *m;
2809 MonoGenericContext ctx;
2810 MonoType *args [16];
2812 if (mono_method_signature (method)->params [1]->type == MONO_TYPE_OBJECT)
2813 /* Avoid recursion */
2814 return NULL;
2816 m = mono_class_get_method_from_name (mono_defaults.array_class, "GetGenericValueImpl", 2);
2817 g_assert (m);
2819 memset (&ctx, 0, sizeof (ctx));
2820 args [0] = &mono_defaults.object_class->byval_arg;
2821 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
2823 m = mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE);
2826 * Get the code for the <object> instantiation which should be emitted into
2827 * the mscorlib aot image by the AOT compiler.
2829 code = mono_aot_get_method (domain, m);
2830 if (code)
2831 return code;
2834 if (method_index == 0xffffff && method->is_inflated && mono_method_is_generic_sharable_impl_full (method, FALSE, TRUE)) {
2835 /* Partial sharing */
2836 method_index = find_extra_method (mini_get_shared_method (method), &amodule);
2839 if (method_index == 0xffffff) {
2840 if (mono_aot_only && mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
2841 char *full_name;
2843 full_name = mono_method_full_name (method, TRUE);
2844 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT NOT FOUND: %s.\n", full_name);
2845 g_free (full_name);
2847 return NULL;
2850 if (method_index == 0xffffff)
2851 return NULL;
2853 /* Needed by find_jit_info */
2854 mono_aot_lock ();
2855 if (!amodule->extra_methods)
2856 amodule->extra_methods = g_hash_table_new (NULL, NULL);
2857 g_hash_table_insert (amodule->extra_methods, GUINT_TO_POINTER (method_index), method);
2858 mono_aot_unlock ();
2859 } else {
2860 /* Common case */
2861 method_index = mono_metadata_token_index (method->token) - 1;
2864 return load_method (domain, amodule, klass->image, method, method->token, method_index);
2868 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
2869 * method.
2871 gpointer
2872 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
2874 MonoAotModule *aot_module = image->aot_module;
2875 int method_index;
2877 if (!aot_module)
2878 return NULL;
2880 method_index = mono_metadata_token_index (token) - 1;
2882 return load_method (domain, aot_module, image, NULL, token, method_index);
2885 typedef struct {
2886 guint8 *addr;
2887 gboolean res;
2888 } IsGotEntryUserData;
2890 static void
2891 check_is_got_entry (gpointer key, gpointer value, gpointer user_data)
2893 IsGotEntryUserData *data = (IsGotEntryUserData*)user_data;
2894 MonoAotModule *aot_module = (MonoAotModule*)value;
2896 if (aot_module->got && (data->addr >= (guint8*)(aot_module->got)) && (data->addr < (guint8*)(aot_module->got + aot_module->info.got_size)))
2897 data->res = TRUE;
2900 gboolean
2901 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
2903 IsGotEntryUserData user_data;
2905 if (!aot_modules)
2906 return FALSE;
2908 user_data.addr = addr;
2909 user_data.res = FALSE;
2910 mono_aot_lock ();
2911 g_hash_table_foreach (aot_modules, check_is_got_entry, &user_data);
2912 mono_aot_unlock ();
2914 return user_data.res;
2917 typedef struct {
2918 guint8 *addr;
2919 MonoAotModule *module;
2920 } FindAotModuleUserData;
2922 static void
2923 find_aot_module_cb (gpointer key, gpointer value, gpointer user_data)
2925 FindAotModuleUserData *data = (FindAotModuleUserData*)user_data;
2926 MonoAotModule *aot_module = (MonoAotModule*)value;
2928 if ((data->addr >= (guint8*)(aot_module->code)) && (data->addr < (guint8*)(aot_module->code_end)))
2929 data->module = aot_module;
2932 static inline MonoAotModule*
2933 find_aot_module (guint8 *code)
2935 FindAotModuleUserData user_data;
2937 if (!aot_modules)
2938 return NULL;
2940 /* Reading these need no locking */
2941 if (((gsize)code < aot_code_low_addr) || ((gsize)code > aot_code_high_addr))
2942 return NULL;
2944 user_data.addr = code;
2945 user_data.module = NULL;
2947 mono_aot_lock ();
2948 g_hash_table_foreach (aot_modules, find_aot_module_cb, &user_data);
2949 mono_aot_unlock ();
2951 return user_data.module;
2954 void
2955 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
2958 * Since AOT code is only used in the root domain,
2959 * mono_domain_get () != mono_get_root_domain () means the calling method
2960 * is AppDomain:InvokeInDomain, so this is the same check as in
2961 * mono_method_same_domain () but without loading the metadata for the method.
2963 if (mono_domain_get () == mono_get_root_domain ())
2964 mono_arch_patch_plt_entry (code, got, regs, addr);
2968 * mono_aot_plt_resolve:
2970 * This function is called by the entries in the PLT to resolve the actual method that
2971 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
2972 * Returns NULL if the something cannot be loaded.
2974 gpointer
2975 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
2977 #ifdef MONO_ARCH_AOT_SUPPORTED
2978 guint8 *p, *target, *plt_entry;
2979 MonoJumpInfo ji;
2980 MonoAotModule *module = (MonoAotModule*)aot_module;
2981 gboolean res, no_ftnptr = FALSE;
2982 MonoMemPool *mp;
2984 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
2986 p = &module->blob [plt_info_offset];
2988 ji.type = decode_value (p, &p);
2990 mp = mono_mempool_new_size (512);
2991 res = decode_patch (module, mp, &ji, p, &p);
2993 if (!res) {
2994 mono_mempool_destroy (mp);
2995 return NULL;
2999 * Avoid calling resolve_patch_target in the full-aot case if possible, since
3000 * it would create a trampoline, and we don't need that.
3001 * We could do this only if the method does not need the special handling
3002 * in mono_magic_trampoline ().
3004 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) &&
3005 !mono_method_needs_static_rgctx_invoke (ji.data.method, FALSE)) {
3006 target = mono_jit_compile_method (ji.data.method);
3007 no_ftnptr = TRUE;
3008 } else {
3009 target = mono_resolve_patch_target (NULL, mono_domain_get (), NULL, &ji, TRUE);
3013 * The trampoline expects us to return a function descriptor on platforms which use
3014 * it, but resolve_patch_target returns a direct function pointer for some type of
3015 * patches, so have to translate between the two.
3016 * FIXME: Clean this up, but how ?
3018 if (ji.type == MONO_PATCH_INFO_ABS || ji.type == MONO_PATCH_INFO_INTERNAL_METHOD || ji.type == MONO_PATCH_INFO_CLASS_INIT || ji.type == MONO_PATCH_INFO_ICALL_ADDR || ji.type == MONO_PATCH_INFO_JIT_ICALL_ADDR || ji.type == MONO_PATCH_INFO_RGCTX_FETCH) {
3019 /* These should already have a function descriptor */
3020 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3021 /* Our function descriptors have a 0 environment, gcc created ones don't */
3022 if (ji.type != MONO_PATCH_INFO_INTERNAL_METHOD && ji.type != MONO_PATCH_INFO_JIT_ICALL_ADDR && ji.type != MONO_PATCH_INFO_ICALL_ADDR)
3023 g_assert (((gpointer*)target) [2] == 0);
3024 #endif
3025 /* Empty */
3026 } else if (!no_ftnptr) {
3027 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3028 g_assert (((gpointer*)target) [2] != 0);
3029 #endif
3030 target = mono_create_ftnptr (mono_domain_get (), target);
3033 mono_mempool_destroy (mp);
3035 /* Patch the PLT entry with target which might be the actual method not a trampoline */
3036 plt_entry = mono_aot_get_plt_entry (code);
3037 g_assert (plt_entry);
3038 mono_aot_patch_plt_entry (plt_entry, module->got, NULL, target);
3040 return target;
3041 #else
3042 g_assert_not_reached ();
3043 return NULL;
3044 #endif
3048 * init_plt:
3050 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
3051 * method in the module is loaded to avoid committing memory by writing to it.
3052 * LOCKING: Assumes the AOT lock is held.
3054 static void
3055 init_plt (MonoAotModule *amodule)
3057 int i;
3058 gpointer tramp;
3060 if (amodule->plt_inited)
3061 return;
3063 tramp = mono_create_specific_trampoline (amodule, MONO_TRAMPOLINE_AOT_PLT, mono_get_root_domain (), NULL);
3066 * Initialize the PLT entries in the GOT to point to the default targets.
3069 tramp = mono_create_ftnptr (mono_domain_get (), tramp);
3070 for (i = 1; i < amodule->info.plt_size; ++i)
3071 /* All the default entries point to the AOT trampoline */
3072 ((gpointer*)amodule->got)[amodule->info.plt_got_offset_base + i] = tramp;
3074 amodule->plt_inited = TRUE;
3078 * mono_aot_get_plt_entry:
3080 * Return the address of the PLT entry called by the code at CODE if exists.
3082 guint8*
3083 mono_aot_get_plt_entry (guint8 *code)
3085 MonoAotModule *aot_module = find_aot_module (code);
3087 if (!aot_module)
3088 return NULL;
3090 #ifdef MONO_ARCH_AOT_SUPPORTED
3092 guint8 *target = mono_arch_get_call_target (code);
3094 if ((target >= (guint8*)(aot_module->plt)) && (target < (guint8*)(aot_module->plt_end)))
3095 return target;
3097 #else
3098 g_assert_not_reached ();
3099 #endif
3101 return NULL;
3105 * mono_aot_get_plt_info_offset:
3107 * Return the PLT info offset belonging to the plt entry called by CODE.
3109 guint32
3110 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3112 guint8 *plt_entry = mono_aot_get_plt_entry (code);
3114 g_assert (plt_entry);
3116 /* The offset is embedded inside the code after the plt entry */
3117 #ifdef MONO_ARCH_AOT_SUPPORTED
3118 return mono_arch_get_plt_info_offset (plt_entry, regs, code);
3119 #else
3120 g_assert_not_reached ();
3121 return 0;
3122 #endif
3125 static gpointer
3126 mono_create_ftnptr_malloc (guint8 *code)
3128 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
3129 MonoPPCFunctionDescriptor *ftnptr = g_malloc0 (sizeof (MonoPPCFunctionDescriptor));
3131 ftnptr->code = code;
3132 ftnptr->toc = NULL;
3133 ftnptr->env = NULL;
3135 return ftnptr;
3136 #else
3137 return code;
3138 #endif
3142 * mono_aot_register_jit_icall:
3144 * Register a JIT icall which is called by trampolines in full-aot mode. This should
3145 * be called from mono_arch_init () during startup.
3147 void
3148 mono_aot_register_jit_icall (const char *name, gpointer addr)
3150 /* No need for locking */
3151 if (!aot_jit_icall_hash)
3152 aot_jit_icall_hash = g_hash_table_new (g_str_hash, g_str_equal);
3153 g_hash_table_insert (aot_jit_icall_hash, (char*)name, addr);
3157 * load_function:
3159 * Load the function named NAME from the aot image.
3161 static gpointer
3162 load_function (MonoAotModule *amodule, const char *name)
3164 char *symbol;
3165 guint8 *p;
3166 int n_patches, pindex;
3167 MonoMemPool *mp;
3168 gpointer code;
3170 /* Load the code */
3172 symbol = g_strdup_printf ("%s", name);
3173 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&code);
3174 g_free (symbol);
3175 if (!code)
3176 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name, amodule->aot_name);
3178 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "AOT FOUND function '%s' in AOT file '%s'.\n", name, amodule->aot_name);
3180 /* Load info */
3182 symbol = g_strdup_printf ("%s_p", name);
3183 find_symbol (amodule->sofile, amodule->globals, symbol, (gpointer *)&p);
3184 g_free (symbol);
3185 if (!p)
3186 /* Nothing to patch */
3187 return code;
3189 p = amodule->blob + *(guint32*)p;
3191 /* Similar to mono_aot_load_method () */
3193 n_patches = decode_value (p, &p);
3195 if (n_patches) {
3196 MonoJumpInfo *patches;
3197 guint32 *got_slots;
3199 mp = mono_mempool_new ();
3201 patches = load_patch_info (amodule, mp, n_patches, &got_slots, p, &p);
3202 g_assert (patches);
3204 for (pindex = 0; pindex < n_patches; ++pindex) {
3205 MonoJumpInfo *ji = &patches [pindex];
3206 gpointer target;
3208 if (amodule->got [got_slots [pindex]])
3209 continue;
3212 * When this code is executed, the runtime may not be initalized yet, so
3213 * resolve the patch info by hand.
3215 if (ji->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
3216 if (!strcmp (ji->data.name, "mono_get_lmf_addr")) {
3217 target = mono_get_lmf_addr;
3218 } else if (!strcmp (ji->data.name, "mono_thread_force_interruption_checkpoint")) {
3219 target = mono_thread_force_interruption_checkpoint;
3220 } else if (!strcmp (ji->data.name, "mono_exception_from_token")) {
3221 target = mono_exception_from_token;
3222 } else if (!strcmp (ji->data.name, "mono_throw_exception")) {
3223 target = mono_get_throw_exception ();
3224 } else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
3225 int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
3226 target = (gpointer)mono_get_trampoline_func (tramp_type2);
3227 } else if (strstr (ji->data.name, "specific_trampoline_lazy_fetch_") == ji->data.name) {
3228 /* atoll is needed because the the offset is unsigned */
3229 guint32 slot;
3230 int res;
3232 res = sscanf (ji->data.name, "specific_trampoline_lazy_fetch_%u", &slot);
3233 g_assert (res == 1);
3234 target = mono_create_specific_trampoline (GUINT_TO_POINTER (slot), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH, mono_get_root_domain (), NULL);
3235 target = mono_create_ftnptr_malloc (target);
3236 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_enter")) {
3237 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
3238 target = mono_create_ftnptr_malloc (target);
3239 } else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
3240 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
3241 target = mono_create_ftnptr_malloc (target);
3242 } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
3243 target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
3244 target = mono_create_ftnptr_malloc (target);
3245 } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
3246 target = mono_thread_get_and_clear_pending_exception;
3247 } else if (strstr (ji->data.name, "generic_trampoline_")) {
3248 target = mono_aot_get_trampoline (ji->data.name);
3249 } else if (aot_jit_icall_hash && g_hash_table_lookup (aot_jit_icall_hash, ji->data.name)) {
3250 /* Registered by mono_arch_init () */
3251 target = g_hash_table_lookup (aot_jit_icall_hash, ji->data.name);
3252 } else {
3253 fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
3254 g_assert_not_reached ();
3255 target = NULL;
3257 } else {
3258 /* Hopefully the code doesn't have patches which need method or
3259 * domain to be set.
3261 target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
3262 g_assert (target);
3265 amodule->got [got_slots [pindex]] = target;
3268 g_free (got_slots);
3270 mono_mempool_destroy (mp);
3273 return code;
3277 * Return the trampoline identified by NAME from the mscorlib AOT file.
3278 * On ppc64, this returns a function descriptor.
3280 gpointer
3281 mono_aot_get_trampoline (const char *name)
3283 MonoImage *image;
3284 MonoAotModule *amodule;
3286 image = mono_defaults.corlib;
3287 g_assert (image);
3289 amodule = image->aot_module;
3290 g_assert (amodule);
3292 return mono_create_ftnptr_malloc (load_function (amodule, name));
3295 /* Return a given kind of trampoline */
3296 static gpointer
3297 get_numerous_trampoline (MonoAotTrampoline tramp_type, int n_got_slots, MonoAotModule **out_amodule, guint32 *got_offset, guint32 *out_tramp_size)
3299 MonoAotModule *amodule;
3300 int index, tramp_size;
3301 MonoImage *image;
3303 /* Currently, we keep all trampolines in the mscorlib AOT image */
3304 image = mono_defaults.corlib;
3305 g_assert (image);
3307 mono_aot_lock ();
3309 amodule = image->aot_module;
3310 g_assert (amodule);
3312 *out_amodule = amodule;
3314 if (amodule->trampoline_index [tramp_type] == amodule->info.num_trampolines [tramp_type])
3315 g_error ("Ran out of trampolines of type %d in '%s' (%d)\n", tramp_type, image->name, amodule->info.num_trampolines [tramp_type]);
3317 index = amodule->trampoline_index [tramp_type] ++;
3319 mono_aot_unlock ();
3321 *got_offset = amodule->info.trampoline_got_offset_base [tramp_type] + (index * n_got_slots);
3323 tramp_size = amodule->info.trampoline_size [tramp_type];
3325 if (out_tramp_size)
3326 *out_tramp_size = tramp_size;
3328 return amodule->trampolines [tramp_type] + (index * tramp_size);
3332 * Return a specific trampoline from the AOT file.
3334 gpointer
3335 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3337 MonoAotModule *amodule;
3338 guint32 got_offset, tramp_size;
3339 guint8 *code, *tramp;
3340 static gpointer generic_trampolines [MONO_TRAMPOLINE_NUM];
3341 static gboolean inited;
3342 static guint32 num_trampolines;
3344 if (!inited) {
3345 mono_aot_lock ();
3347 if (!inited) {
3348 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT | MONO_COUNTER_INT, &num_trampolines);
3349 inited = TRUE;
3352 mono_aot_unlock ();
3355 num_trampolines ++;
3357 if (!generic_trampolines [tramp_type]) {
3358 char *symbol;
3360 symbol = mono_get_generic_trampoline_name (tramp_type);
3361 generic_trampolines [tramp_type] = mono_aot_get_trampoline (symbol);
3362 g_free (symbol);
3365 tramp = generic_trampolines [tramp_type];
3366 g_assert (tramp);
3368 code = get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC, 2, &amodule, &got_offset, &tramp_size);
3370 amodule->got [got_offset] = tramp;
3371 amodule->got [got_offset + 1] = arg1;
3373 if (code_len)
3374 *code_len = tramp_size;
3376 return code;
3379 gpointer
3380 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3382 MonoAotModule *amodule;
3383 guint8 *code;
3384 guint32 got_offset;
3386 code = get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX, 2, &amodule, &got_offset, NULL);
3388 amodule->got [got_offset] = ctx;
3389 amodule->got [got_offset + 1] = addr;
3391 /* The caller expects an ftnptr */
3392 return mono_create_ftnptr (mono_domain_get (), code);
3395 gpointer
3396 mono_aot_get_unbox_trampoline (MonoMethod *method)
3398 guint32 method_index = mono_metadata_token_index (method->token) - 1;
3399 MonoAotModule *amodule;
3400 char *symbol;
3401 gpointer code;
3403 if (method->is_inflated && !mono_method_is_generic_sharable_impl (method, FALSE)) {
3404 guint32 index = find_extra_method (method, &amodule);
3405 g_assert (index != 0xffffff);
3407 symbol = g_strdup_printf ("ut_e_%d", index);
3408 } else {
3409 amodule = method->klass->image->aot_module;
3410 g_assert (amodule);
3412 symbol = g_strdup_printf ("ut_%d", method_index);
3414 code = load_function (amodule, symbol);
3415 g_free (symbol);
3417 /* The caller expects an ftnptr */
3418 return mono_create_ftnptr (mono_domain_get (), code);
3421 gpointer
3422 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3424 char *symbol;
3425 gpointer code;
3427 symbol = mono_get_rgctx_fetch_trampoline_name (slot);
3428 code = load_function (mono_defaults.corlib->aot_module, symbol);
3429 g_free (symbol);
3430 /* The caller expects an ftnptr */
3431 return mono_create_ftnptr (mono_domain_get (), code);
3434 gpointer
3435 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3437 guint32 got_offset;
3438 gpointer code;
3439 gpointer *buf;
3440 int i;
3441 MonoAotModule *amodule;
3443 code = get_numerous_trampoline (MONO_AOT_TRAMP_IMT_THUNK, 1, &amodule, &got_offset, NULL);
3445 /* Save the entries into an array */
3446 buf = mono_domain_alloc (domain, (count + 1) * 2 * sizeof (gpointer));
3447 for (i = 0; i < count; ++i) {
3448 MonoIMTCheckItem *item = imt_entries [i];
3450 g_assert (item->key);
3451 /* FIXME: */
3452 g_assert (!item->has_target_code);
3454 buf [(i * 2)] = item->key;
3455 buf [(i * 2) + 1] = &(vtable->vtable [item->value.vtable_slot]);
3457 buf [(count * 2)] = NULL;
3458 buf [(count * 2) + 1] = fail_tramp;
3460 amodule->got [got_offset] = buf;
3462 return code;
3466 * mono_aot_set_make_unreadable:
3468 * Set whenever to make all mmaped memory unreadable. In conjuction with a
3469 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
3471 void
3472 mono_aot_set_make_unreadable (gboolean unreadable)
3474 static int inited;
3476 make_unreadable = unreadable;
3478 if (make_unreadable && !inited) {
3479 mono_counters_register ("AOT pagefaults", MONO_COUNTER_JIT | MONO_COUNTER_INT, &n_pagefaults);
3483 typedef struct {
3484 MonoAotModule *module;
3485 guint8 *ptr;
3486 } FindMapUserData;
3488 static void
3489 find_map (gpointer key, gpointer value, gpointer user_data)
3491 MonoAotModule *module = (MonoAotModule*)value;
3492 FindMapUserData *data = (FindMapUserData*)user_data;
3494 if (!data->module)
3495 if ((data->ptr >= module->mem_begin) && (data->ptr < module->mem_end))
3496 data->module = module;
3499 static MonoAotModule*
3500 find_module_for_addr (void *ptr)
3502 FindMapUserData data;
3504 if (!make_unreadable)
3505 return NULL;
3507 data.module = NULL;
3508 data.ptr = (guint8*)ptr;
3510 mono_aot_lock ();
3511 g_hash_table_foreach (aot_modules, (GHFunc)find_map, &data);
3512 mono_aot_unlock ();
3514 return data.module;
3518 * mono_aot_is_pagefault:
3520 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
3521 * within memory allocated by this module.
3523 gboolean
3524 mono_aot_is_pagefault (void *ptr)
3526 if (!make_unreadable)
3527 return FALSE;
3530 * Not signal safe, but SIGSEGV's are synchronous, and
3531 * this is only turned on by a MONO_DEBUG option.
3533 return find_module_for_addr (ptr) != NULL;
3537 * mono_aot_handle_pagefault:
3539 * Handle a pagefault caused by an unreadable page by making it readable again.
3541 void
3542 mono_aot_handle_pagefault (void *ptr)
3544 #ifndef PLATFORM_WIN32
3545 guint8* start = (guint8*)ROUND_DOWN (((gssize)ptr), mono_pagesize ());
3546 int res;
3548 mono_aot_lock ();
3549 res = mono_mprotect (start, mono_pagesize (), MONO_MMAP_READ|MONO_MMAP_WRITE|MONO_MMAP_EXEC);
3550 g_assert (res == 0);
3552 n_pagefaults ++;
3553 mono_aot_unlock ();
3554 #endif
3557 #else
3558 /* AOT disabled */
3560 void
3561 mono_aot_init (void)
3565 gpointer
3566 mono_aot_get_method (MonoDomain *domain, MonoMethod *method)
3568 return NULL;
3571 gboolean
3572 mono_aot_is_got_entry (guint8 *code, guint8 *addr)
3574 return FALSE;
3577 gboolean
3578 mono_aot_get_cached_class_info (MonoClass *klass, MonoCachedClassInfo *res)
3580 return FALSE;
3583 gboolean
3584 mono_aot_get_class_from_name (MonoImage *image, const char *name_space, const char *name, MonoClass **klass)
3586 return FALSE;
3589 MonoJitInfo *
3590 mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
3592 return NULL;
3595 gpointer
3596 mono_aot_get_method_from_token (MonoDomain *domain, MonoImage *image, guint32 token)
3598 return NULL;
3601 guint8*
3602 mono_aot_get_plt_entry (guint8 *code)
3604 return NULL;
3607 gpointer
3608 mono_aot_plt_resolve (gpointer aot_module, guint32 plt_info_offset, guint8 *code)
3610 return NULL;
3613 void
3614 mono_aot_patch_plt_entry (guint8 *code, gpointer *got, mgreg_t *regs, guint8 *addr)
3618 gpointer
3619 mono_aot_get_method_from_vt_slot (MonoDomain *domain, MonoVTable *vtable, int slot)
3621 return NULL;
3624 guint32
3625 mono_aot_get_plt_info_offset (mgreg_t *regs, guint8 *code)
3627 g_assert_not_reached ();
3629 return 0;
3632 gpointer
3633 mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
3635 g_assert_not_reached ();
3636 return NULL;
3639 gpointer
3640 mono_aot_get_static_rgctx_trampoline (gpointer ctx, gpointer addr)
3642 g_assert_not_reached ();
3643 return NULL;
3646 gpointer
3647 mono_aot_get_trampoline (const char *name)
3649 g_assert_not_reached ();
3650 return NULL;
3653 gpointer
3654 mono_aot_get_unbox_trampoline (MonoMethod *method)
3656 g_assert_not_reached ();
3657 return NULL;
3660 gpointer
3661 mono_aot_get_lazy_fetch_trampoline (guint32 slot)
3663 g_assert_not_reached ();
3664 return NULL;
3667 gpointer
3668 mono_aot_get_imt_thunk (MonoVTable *vtable, MonoDomain *domain, MonoIMTCheckItem **imt_entries, int count, gpointer fail_tramp)
3670 g_assert_not_reached ();
3671 return NULL;
3674 guint8*
3675 mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
3677 g_assert_not_reached ();
3678 return NULL;
3681 void
3682 mono_aot_register_jit_icall (const char *name, gpointer addr)
3686 #endif