3 * mono Ahead of Time compiler
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Zoltan Varga (vargaz@gmail.com)
9 * (C) 2002 Ximian, Inc.
10 * Copyright 2003-2011 Novell, Inc.
11 * Copyright 2011 Xamarin, Inc.
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include <sys/types.h>
22 #ifdef HAVE_SYS_MMAN_H
31 #ifdef HAVE_EXECINFO_H
38 #ifdef HAVE_SYS_WAIT_H
39 #include <sys/wait.h> /* for WIFEXITED, WEXITSTATUS */
42 #include <mono/metadata/abi-details.h>
43 #include <mono/metadata/tabledefs.h>
44 #include <mono/metadata/class.h>
45 #include <mono/metadata/object.h>
46 #include <mono/metadata/tokentype.h>
47 #include <mono/metadata/appdomain.h>
48 #include <mono/metadata/debug-helpers.h>
49 #include <mono/metadata/assembly.h>
50 #include <mono/metadata/assembly-internals.h>
51 #include <mono/metadata/metadata-internals.h>
52 #include <mono/metadata/exception-internals.h>
53 #include <mono/metadata/marshal.h>
54 #include <mono/metadata/gc-internals.h>
55 #include <mono/metadata/threads-types.h>
56 #include <mono/metadata/mono-endian.h>
57 #include <mono/utils/mono-logger-internals.h>
58 #include <mono/utils/mono-mmap.h>
59 #include <mono/utils/mono-compiler.h>
60 #include <mono/utils/mono-counters.h>
61 #include <mono/utils/mono-digest.h>
62 #include <mono/utils/mono-threads-coop.h>
65 #include "seq-points.h"
67 #include "debugger-agent.h"
68 #include "aot-compiler.h"
69 #include "aot-runtime.h"
70 #include "jit-icalls.h"
71 #include "mini-runtime.h"
76 #define ENABLE_AOT_CACHE
79 /* Number of got entries shared between the JIT and LLVM GOT */
80 #define N_COMMON_GOT_ENTRIES 10
82 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
89 typedef struct MonoAotModule
{
91 /* Pointer to the Global Offset Table */
95 GHashTable
*name_cache
;
96 GHashTable
*extra_methods
;
97 /* Maps methods to their code */
98 GHashTable
*method_to_code
;
99 /* Maps pointers into the method info to the methods themselves */
100 GHashTable
*method_ref_to_method
;
101 MonoAssemblyName
*image_names
;
103 MonoAssembly
*assembly
;
104 MonoImage
**image_table
;
105 guint32 image_table_len
;
106 gboolean out_of_date
;
108 gboolean got_initializing
;
111 guint8
*jit_code_start
;
112 guint8
*jit_code_end
;
113 guint8
*llvm_code_start
;
114 guint8
*llvm_code_end
;
118 gpointer weak_field_indexes
;
119 /* Maps method indexes to their code */
121 /* Sorted array of method addresses */
122 gpointer
*sorted_methods
;
123 /* Method indexes for each method in sorted_methods */
124 int *sorted_method_indexes
;
125 /* The length of the two tables above */
126 int sorted_methods_len
;
127 guint32
*method_info_offsets
;
128 guint32
*ex_info_offsets
;
129 guint32
*class_info_offsets
;
130 guint32
*got_info_offsets
;
131 guint32
*llvm_got_info_offsets
;
132 guint32
*methods_loaded
;
133 guint16
*class_name_table
;
134 guint32
*extra_method_table
;
135 guint32
*extra_method_info_offsets
;
136 guint32
*unbox_trampolines
;
137 guint32
*unbox_trampolines_end
;
138 guint32
*unbox_trampoline_addresses
;
141 /* Points to the mono EH data created by LLVM */
142 guint8
*mono_eh_frame
;
144 /* Points to the data tables if MONO_AOT_FILE_FLAG_SEPARATE_DATA is set */
145 gpointer tables
[MONO_AOT_TABLE_NUM
];
146 /* Points to the trampolines */
147 guint8
*trampolines
[MONO_AOT_TRAMP_NUM
];
148 /* The first unused trampoline of each kind */
149 guint32 trampoline_index
[MONO_AOT_TRAMP_NUM
];
151 gboolean use_page_trampolines
;
153 MonoAotFileInfo info
;
158 JitInfoMap
*async_jit_info_table
;
164 unsigned char *trampolines
;
165 unsigned char *trampolines_end
;
168 static GHashTable
*aot_modules
;
169 #define mono_aot_lock() mono_os_mutex_lock (&aot_mutex)
170 #define mono_aot_unlock() mono_os_mutex_unlock (&aot_mutex)
171 static mono_mutex_t aot_mutex
;
174 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
175 * AOT modules registered by mono_aot_register_module ().
177 static GHashTable
*static_aot_modules
;
179 * Same as above, but tracks module that must be loaded before others are
180 * This allows us to have a "container" module which contains resources for
181 * other modules. Since it doesn't provide methods for a managed assembly,
182 * and it needs to be fully loaded by the time the other code needs it, it
183 * must be eagerly loaded before other modules.
185 static char *container_assm_name
= NULL
;
186 static MonoAotModule
*container_amodule
= NULL
;
189 * Maps MonoJitInfo* to the aot module they belong to, this can be different
190 * from ji->method->klass->image's aot module for generic instances.
192 static GHashTable
*ji_to_amodule
;
195 * Whenever to AOT compile loaded assemblies on demand and store them in
198 static gboolean enable_aot_cache
= FALSE
;
200 static gboolean mscorlib_aot_loaded
;
203 static gint32 mono_last_aot_method
= -1;
205 static gboolean make_unreadable
= FALSE
;
206 static guint32 name_table_accesses
= 0;
207 static guint32 n_pagefaults
= 0;
209 /* Used to speed-up find_aot_module () */
210 static gsize aot_code_low_addr
= (gssize
)-1;
211 static gsize aot_code_high_addr
= 0;
214 static gint32 async_jit_info_size
;
216 static GHashTable
*aot_jit_icall_hash
;
219 #define USE_PAGE_TRAMPOLINES (mono_defaults.corlib->aot_module->use_page_trampolines)
221 #define USE_PAGE_TRAMPOLINES 0
224 #define mono_aot_page_lock() mono_os_mutex_lock (&aot_page_mutex)
225 #define mono_aot_page_unlock() mono_os_mutex_unlock (&aot_page_mutex)
226 static mono_mutex_t aot_page_mutex
;
228 static MonoAotModule
*mscorlib_aot_module
;
230 /* Embedding API hooks to load the AOT data for AOT images compiled with MONO_AOT_FILE_FLAG_SEPARATE_DATA */
231 static MonoLoadAotDataFunc aot_data_load_func
;
232 static MonoFreeAotDataFunc aot_data_free_func
;
233 static gpointer aot_data_func_user_data
;
236 init_plt (MonoAotModule
*info
);
239 compute_llvm_code_range (MonoAotModule
*amodule
, guint8
**code_start
, guint8
**code_end
);
242 init_method (MonoAotModule
*amodule
, guint32 method_index
, MonoMethod
*method
, MonoClass
*init_class
, MonoGenericContext
*context
, MonoError
*error
);
245 decode_patches (MonoAotModule
*amodule
, MonoMemPool
*mp
, int n_patches
, gboolean llvm
, guint32
*got_offsets
);
248 amodule_lock (MonoAotModule
*amodule
)
250 mono_os_mutex_lock (&amodule
->mutex
);
254 amodule_unlock (MonoAotModule
*amodule
)
256 mono_os_mutex_unlock (&amodule
->mutex
);
262 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
263 * found, and sets @error for what happened
266 load_image (MonoAotModule
*amodule
, int index
, MonoError
*error
)
268 MonoAssembly
*assembly
;
269 MonoImageOpenStatus status
;
271 g_assert (index
< amodule
->image_table_len
);
275 if (amodule
->image_table
[index
])
276 return amodule
->image_table
[index
];
277 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "AOT: module %s wants to load image %d: %s", amodule
->aot_name
, index
, amodule
->image_names
[index
].name
);
278 if (amodule
->out_of_date
) {
279 mono_error_set_bad_image_by_name (error
, amodule
->aot_name
, "Image out of date");
284 * LoadFile allows loading more than one assembly with the same name.
285 * That means that just calling mono_assembly_load is unlikely to find
286 * the correct assembly (it'll just return the first one loaded). But
287 * we shouldn't hardcode the full assembly filepath into the AOT image,
288 * so it's not obvious that we can call mono_assembly_open_predicate.
290 * In the JIT, an assembly opened with LoadFile is supposed to only
291 * refer to already-loaded assemblies (or to GAC & MONO_PATH)
292 * assemblies - so nothing new should be loading. And for the
293 * LoadFile'd assembly itself, we can check if the name and guid of the
294 * current AOT module matches the wanted name and guid and just return
295 * the AOT module's assembly.
297 if (mono_asmctx_get_kind (&amodule
->assembly
->context
) == MONO_ASMCTX_INDIVIDUAL
&&
298 !strcmp (amodule
->assembly
->image
->guid
, amodule
->image_guids
[index
]) &&
299 mono_assembly_names_equal (&amodule
->image_names
[index
], &amodule
->assembly
->aname
))
300 assembly
= amodule
->assembly
;
302 assembly
= mono_assembly_load (&amodule
->image_names
[index
], amodule
->assembly
->basedir
, &status
);
304 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: module %s is unusable because dependency %s is not found.", amodule
->aot_name
, amodule
->image_names
[index
].name
);
305 mono_error_set_bad_image_by_name (error
, amodule
->aot_name
, "module is unusable because dependency %s is not found (error %d).\n", amodule
->image_names
[index
].name
, status
);
306 amodule
->out_of_date
= TRUE
;
310 if (strcmp (assembly
->image
->guid
, amodule
->image_guids
[index
])) {
311 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: module %s is unusable (GUID of dependent assembly %s doesn't match (expected '%s', got '%s').", amodule
->aot_name
, amodule
->image_names
[index
].name
, amodule
->image_guids
[index
], assembly
->image
->guid
);
312 mono_error_set_bad_image_by_name (error
, amodule
->aot_name
, "module is unusable (GUID of dependent assembly %s doesn't match (expected '%s', got '%s').", amodule
->image_names
[index
].name
, amodule
->image_guids
[index
], assembly
->image
->guid
);
313 amodule
->out_of_date
= TRUE
;
317 amodule
->image_table
[index
] = assembly
->image
;
318 return assembly
->image
;
322 decode_value (guint8
*ptr
, guint8
**rptr
)
327 if ((b
& 0x80) == 0){
330 } else if ((b
& 0x40) == 0){
331 len
= ((b
& 0x3f) << 8 | ptr
[1]);
333 } else if (b
!= 0xff) {
334 len
= ((b
& 0x1f) << 24) |
341 len
= (ptr
[1] << 24) | (ptr
[2] << 16) | (ptr
[3] << 8) | ptr
[4];
347 //printf ("DECODE: %d.\n", len);
352 * mono_aot_get_offset:
354 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
358 mono_aot_get_offset (guint32
*table
, int index
)
360 int i
, group
, ngroups
, index_entry_size
;
361 int start_offset
, offset
, group_size
;
362 guint8
*data_start
, *p
;
363 guint32
*index32
= NULL
;
364 guint16
*index16
= NULL
;
366 /* noffsets = table [0]; */
367 group_size
= table
[1];
369 index_entry_size
= table
[3];
370 group
= index
/ group_size
;
372 if (index_entry_size
== 2) {
373 index16
= (guint16
*)&table
[4];
374 data_start
= (guint8
*)&index16
[ngroups
];
375 p
= data_start
+ index16
[group
];
377 index32
= (guint32
*)&table
[4];
378 data_start
= (guint8
*)&index32
[ngroups
];
379 p
= data_start
+ index32
[group
];
382 /* offset will contain the value of offsets [group * group_size] */
383 offset
= start_offset
= decode_value (p
, &p
);
384 for (i
= group
* group_size
+ 1; i
<= index
; ++i
) {
385 offset
+= decode_value (p
, &p
);
388 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
394 decode_resolve_method_ref (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
);
397 decode_klass_ref (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
);
400 decode_type (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
);
402 static MonoGenericInst
*
403 decode_generic_inst (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
406 MonoType
**type_argv
;
407 MonoGenericInst
*inst
;
411 type_argc
= decode_value (p
, &p
);
412 type_argv
= g_new0 (MonoType
*, type_argc
);
414 for (i
= 0; i
< type_argc
; ++i
) {
415 MonoClass
*pclass
= decode_klass_ref (module
, p
, &p
, error
);
420 type_argv
[i
] = m_class_get_byval_arg (pclass
);
423 inst
= mono_metadata_get_generic_inst (type_argc
, type_argv
);
432 decode_generic_context (MonoAotModule
*module
, MonoGenericContext
*ctx
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
440 argc
= decode_value (p
, &p
);
443 ctx
->class_inst
= decode_generic_inst (module
, p
, &p
, error
);
444 if (!ctx
->class_inst
)
448 argc
= decode_value (p
, &p
);
451 ctx
->method_inst
= decode_generic_inst (module
, p
, &p
, error
);
452 if (!ctx
->method_inst
)
461 decode_klass_ref (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
464 MonoClass
*klass
= NULL
, *eklass
;
465 guint32 token
, rank
, idx
;
470 reftype
= decode_value (p
, &p
);
473 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Decoding a null class ref");
478 case MONO_AOT_TYPEREF_TYPEDEF_INDEX
:
479 idx
= decode_value (p
, &p
);
480 image
= load_image (module
, 0, error
);
483 klass
= mono_class_get_checked (image
, MONO_TOKEN_TYPE_DEF
+ idx
, error
);
485 case MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE
:
486 idx
= decode_value (p
, &p
);
487 image
= load_image (module
, decode_value (p
, &p
), error
);
490 klass
= mono_class_get_checked (image
, MONO_TOKEN_TYPE_DEF
+ idx
, error
);
492 case MONO_AOT_TYPEREF_TYPESPEC_TOKEN
:
493 token
= decode_value (p
, &p
);
494 image
= module
->assembly
->image
;
496 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "No image associated with the aot module");
499 klass
= mono_class_get_checked (image
, token
, error
);
501 case MONO_AOT_TYPEREF_GINST
: {
503 MonoGenericContext ctx
;
506 gclass
= decode_klass_ref (module
, p
, &p
, error
);
509 g_assert (mono_class_is_gtd (gclass
));
511 memset (&ctx
, 0, sizeof (ctx
));
512 ctx
.class_inst
= decode_generic_inst (module
, p
, &p
, error
);
515 type
= mono_class_inflate_generic_type_checked (m_class_get_byval_arg (gclass
), &ctx
, error
);
518 klass
= mono_class_from_mono_type (type
);
519 mono_metadata_free_type (type
);
522 case MONO_AOT_TYPEREF_VAR
: {
524 MonoGenericContainer
*container
= NULL
;
525 gboolean has_constraint
= decode_value (p
, &p
);
527 if (has_constraint
) {
528 MonoClass
*par_klass
;
529 MonoType
*gshared_constraint
;
531 gshared_constraint
= decode_type (module
, p
, &p
, error
);
532 if (!gshared_constraint
)
535 par_klass
= decode_klass_ref (module
, p
, &p
, error
);
539 t
= mini_get_shared_gparam (m_class_get_byval_arg (par_klass
), gshared_constraint
);
540 mono_metadata_free_type (gshared_constraint
);
541 klass
= mono_class_from_mono_type (t
);
543 int type
= decode_value (p
, &p
);
544 int num
= decode_value (p
, &p
);
545 gboolean is_not_anonymous
= decode_value (p
, &p
);
547 if (is_not_anonymous
) {
548 gboolean is_method
= decode_value (p
, &p
);
551 MonoMethod
*method_def
;
552 g_assert (type
== MONO_TYPE_MVAR
);
553 method_def
= decode_resolve_method_ref (module
, p
, &p
, error
);
557 container
= mono_method_get_generic_container (method_def
);
559 MonoClass
*class_def
;
560 g_assert (type
== MONO_TYPE_VAR
);
561 class_def
= decode_klass_ref (module
, p
, &p
, error
);
565 container
= mono_class_try_get_generic_container (class_def
); //FIXME is this a case for a try_get?
568 // We didn't decode is_method, so we have to infer it from type enum.
569 container
= mono_get_anonymous_container_for_image (module
->assembly
->image
, type
== MONO_TYPE_MVAR
);
572 t
= g_new0 (MonoType
, 1);
573 t
->type
= (MonoTypeEnum
)type
;
574 if (is_not_anonymous
) {
575 t
->data
.generic_param
= mono_generic_container_get_param (container
, num
);
578 MonoGenericParam
*par
= mono_metadata_create_anon_gparam (module
->assembly
->image
, num
, type
== MONO_TYPE_MVAR
);
579 t
->data
.generic_param
= par
;
580 // FIXME: maybe do this for all anon gparams?
581 ((MonoGenericParamFull
*)par
)->info
.name
= mono_make_generic_name_string (module
->assembly
->image
, num
);
583 // FIXME: Maybe use types directly to avoid
584 // the overhead of creating MonoClass-es
585 klass
= mono_class_from_mono_type (t
);
591 case MONO_AOT_TYPEREF_ARRAY
:
593 rank
= decode_value (p
, &p
);
594 eklass
= decode_klass_ref (module
, p
, &p
, error
);
597 klass
= mono_class_create_array (eklass
, rank
);
599 case MONO_AOT_TYPEREF_PTR
: {
602 t
= decode_type (module
, p
, &p
, error
);
605 klass
= mono_class_from_mono_type (t
);
609 case MONO_AOT_TYPEREF_BLOB_INDEX
: {
610 guint32 offset
= decode_value (p
, &p
);
613 p2
= module
->blob
+ offset
;
614 klass
= decode_klass_ref (module
, p2
, &p2
, error
);
618 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid klass reftype %d", reftype
);
621 //printf ("BLA: %s\n", mono_type_full_name (m_class_get_byval_arg (klass)));
626 static MonoClassField
*
627 decode_field_info (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
)
630 MonoClass
*klass
= decode_klass_ref (module
, buf
, &buf
, error
);
635 mono_error_cleanup (error
); /* FIXME don't swallow the error */
639 token
= MONO_TOKEN_FIELD_DEF
+ decode_value (p
, &p
);
643 return mono_class_get_field (klass
, token
);
647 * Parse a MonoType encoded by encode_type () in aot-compiler.c. Return malloc-ed
651 decode_type (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
656 if (*p
== MONO_TYPE_CMOD_REQD
) {
659 int count
= decode_value (p
, &p
);
661 t
= (MonoType
*)g_malloc0 (mono_sizeof_type_with_mods (count
));
664 MonoCustomModContainer
*cm
= mono_type_get_cmods (t
);
665 int iindex
= decode_value (p
, &p
);
666 cm
->image
= load_image (module
, iindex
, error
);
672 for (int i
= 0; i
< cm
->count
; ++i
) {
673 cm
->modifiers
[i
].required
= decode_value (p
, &p
);
674 cm
->modifiers
[i
].token
= decode_value (p
, &p
);
677 t
= (MonoType
*) g_malloc0 (sizeof (MonoType
));
681 if (*p
== MONO_TYPE_PINNED
) {
684 } else if (*p
== MONO_TYPE_BYREF
) {
692 t
->type
= (MonoTypeEnum
)*p
;
697 case MONO_TYPE_BOOLEAN
:
711 case MONO_TYPE_STRING
:
712 case MONO_TYPE_OBJECT
:
713 case MONO_TYPE_TYPEDBYREF
:
715 case MONO_TYPE_VALUETYPE
:
716 case MONO_TYPE_CLASS
:
717 t
->data
.klass
= decode_klass_ref (module
, p
, &p
, error
);
721 case MONO_TYPE_SZARRAY
:
722 t
->data
.klass
= decode_klass_ref (module
, p
, &p
, error
);
728 t
->data
.type
= decode_type (module
, p
, &p
, error
);
732 case MONO_TYPE_GENERICINST
: {
734 MonoGenericContext ctx
;
738 gclass
= decode_klass_ref (module
, p
, &p
, error
);
741 g_assert (mono_class_is_gtd (gclass
));
743 memset (&ctx
, 0, sizeof (ctx
));
744 ctx
.class_inst
= decode_generic_inst (module
, p
, &p
, error
);
747 type
= mono_class_inflate_generic_type_checked (m_class_get_byval_arg (gclass
), &ctx
, error
);
750 klass
= mono_class_from_mono_type (type
);
751 t
->data
.generic_class
= mono_class_get_generic_class (klass
);
754 case MONO_TYPE_ARRAY
: {
755 MonoArrayType
*array
;
758 // FIXME: memory management
759 array
= g_new0 (MonoArrayType
, 1);
760 array
->eklass
= decode_klass_ref (module
, p
, &p
, error
);
763 array
->rank
= decode_value (p
, &p
);
764 array
->numsizes
= decode_value (p
, &p
);
767 array
->sizes
= (int *)g_malloc0 (sizeof (int) * array
->numsizes
);
768 for (i
= 0; i
< array
->numsizes
; ++i
)
769 array
->sizes
[i
] = decode_value (p
, &p
);
771 array
->numlobounds
= decode_value (p
, &p
);
772 if (array
->numlobounds
)
773 array
->lobounds
= (int *)g_malloc0 (sizeof (int) * array
->numlobounds
);
774 for (i
= 0; i
< array
->numlobounds
; ++i
)
775 array
->lobounds
[i
] = decode_value (p
, &p
);
776 t
->data
.array
= array
;
780 case MONO_TYPE_MVAR
: {
781 MonoClass
*klass
= decode_klass_ref (module
, p
, &p
, error
);
784 t
->data
.generic_param
= m_class_get_byval_arg (klass
)->data
.generic_param
;
788 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid encoded type %d", t
->type
);
800 // FIXME: Error handling, memory management
802 static MonoMethodSignature
*
803 decode_signature_with_target (MonoAotModule
*module
, MonoMethodSignature
*target
, guint8
*buf
, guint8
**endbuf
)
806 MonoMethodSignature
*sig
;
808 int i
, gen_param_count
= 0, param_count
, call_conv
;
810 gboolean hasthis
, explicit_this
, has_gen_params
;
814 has_gen_params
= (flags
& 0x10) != 0;
815 hasthis
= (flags
& 0x20) != 0;
816 explicit_this
= (flags
& 0x40) != 0;
817 call_conv
= flags
& 0x0F;
820 gen_param_count
= decode_value (p
, &p
);
821 param_count
= decode_value (p
, &p
);
822 if (target
&& param_count
!= target
->param_count
)
824 sig
= (MonoMethodSignature
*)g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE
+ param_count
* sizeof (MonoType
*));
825 sig
->param_count
= param_count
;
826 sig
->sentinelpos
= -1;
827 sig
->hasthis
= hasthis
;
828 sig
->explicit_this
= explicit_this
;
829 sig
->call_convention
= call_conv
;
830 sig
->generic_param_count
= gen_param_count
;
831 sig
->ret
= decode_type (module
, p
, &p
, error
);
834 for (i
= 0; i
< param_count
; ++i
) {
835 if (*p
== MONO_TYPE_SENTINEL
) {
836 g_assert (sig
->call_convention
== MONO_CALL_VARARG
);
837 sig
->sentinelpos
= i
;
840 sig
->params
[i
] = decode_type (module
, p
, &p
, error
);
841 if (!sig
->params
[i
])
845 if (sig
->call_convention
== MONO_CALL_VARARG
&& sig
->sentinelpos
== -1)
846 sig
->sentinelpos
= sig
->param_count
;
852 mono_error_cleanup (error
); /* FIXME don't swallow the error */
857 static MonoMethodSignature
*
858 decode_signature (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
)
860 return decode_signature_with_target (module
, NULL
, buf
, endbuf
);
864 sig_matches_target (MonoAotModule
*module
, MonoMethod
*target
, guint8
*buf
, guint8
**endbuf
)
866 MonoMethodSignature
*sig
;
870 sig
= decode_signature_with_target (module
, mono_method_signature (target
), p
, &p
);
871 res
= sig
&& mono_metadata_signature_equal (mono_method_signature (target
), sig
);
877 /* Stores information returned by decode_method_ref () */
882 gboolean no_aot_trampoline
;
886 * decode_method_ref_with_target:
888 * Decode a method reference, storing the image/token into a MethodRef structure.
889 * This avoids loading metadata for the method if the caller does not need it. If the method has
890 * no token, then it is loaded from metadata and ref->method is set to the method instance.
891 * If TARGET is non-NULL, abort decoding if it can be determined that the decoded method
892 * couldn't resolve to TARGET, and return FALSE.
893 * There are some kinds of method references which only support a non-null TARGET.
894 * This means that its not possible to decode this into a method, only to check
895 * that the method reference matches a given method. This is normally not a problem
896 * as these wrappers only occur in the extra_methods table, where we already have
897 * a method we want to lookup.
899 * If there was a decoding error, we return FALSE and set @error
902 decode_method_ref_with_target (MonoAotModule
*module
, MethodRef
*ref
, MonoMethod
*target
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
904 guint32 image_index
, value
;
905 MonoImage
*image
= NULL
;
908 memset (ref
, 0, sizeof (MethodRef
));
911 value
= decode_value (p
, &p
);
912 image_index
= value
>> 24;
914 if (image_index
== MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE
) {
915 ref
->no_aot_trampoline
= TRUE
;
916 value
= decode_value (p
, &p
);
917 image_index
= value
>> 24;
920 if (image_index
< MONO_AOT_METHODREF_MIN
|| image_index
== MONO_AOT_METHODREF_METHODSPEC
|| image_index
== MONO_AOT_METHODREF_GINST
) {
921 if (target
&& target
->wrapper_type
) {
926 if (image_index
== MONO_AOT_METHODREF_WRAPPER
) {
928 guint32 wrapper_type
;
930 wrapper_type
= decode_value (p
, &p
);
932 if (target
&& target
->wrapper_type
!= wrapper_type
)
936 image
= mono_defaults
.corlib
;
938 switch (wrapper_type
) {
939 #ifndef DISABLE_REMOTING
940 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: {
941 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
944 mono_class_init (m
->klass
);
948 ref
->method
= mono_marshal_get_remoting_invoke_with_check (m
, error
);
949 return_val_if_nok (error
, FALSE
);
953 case MONO_WRAPPER_PROXY_ISINST
: {
954 MonoClass
*klass
= decode_klass_ref (module
, p
, &p
, error
);
957 ref
->method
= mono_marshal_get_proxy_cancast (klass
);
960 case MONO_WRAPPER_LDFLD
:
961 case MONO_WRAPPER_LDFLDA
:
962 case MONO_WRAPPER_STFLD
: {
963 MonoClass
*klass
= decode_klass_ref (module
, p
, &p
, error
);
966 MonoType
*type
= m_class_get_byval_arg (klass
);
967 if (wrapper_type
== MONO_WRAPPER_LDFLD
)
968 ref
->method
= mono_marshal_get_ldfld_wrapper (type
);
969 else if (wrapper_type
== MONO_WRAPPER_LDFLDA
)
970 ref
->method
= mono_marshal_get_ldflda_wrapper (type
);
971 else if (wrapper_type
== MONO_WRAPPER_STFLD
)
972 ref
->method
= mono_marshal_get_stfld_wrapper (type
);
974 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Unknown AOT wrapper type %d", wrapper_type
);
980 case MONO_WRAPPER_ALLOC
: {
981 int atype
= decode_value (p
, &p
);
982 ManagedAllocatorVariant variant
=
983 mono_profiler_allocations_enabled () ?
984 MANAGED_ALLOCATOR_PROFILER
: MANAGED_ALLOCATOR_REGULAR
;
986 ref
->method
= mono_gc_get_managed_allocator_by_type (atype
, variant
);
987 /* Try to fallback to the slow path version */
989 ref
->method
= mono_gc_get_managed_allocator_by_type (atype
, MANAGED_ALLOCATOR_SLOW_PATH
);
991 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Error: No managed allocator, but we need one for AOT.\nAre you using non-standard GC options?\n");
996 case MONO_WRAPPER_WRITE_BARRIER
: {
997 ref
->method
= mono_gc_get_write_barrier ();
1000 case MONO_WRAPPER_STELEMREF
: {
1001 int subtype
= decode_value (p
, &p
);
1003 if (subtype
== WRAPPER_SUBTYPE_NONE
) {
1004 ref
->method
= mono_marshal_get_stelemref ();
1005 } else if (subtype
== WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
) {
1008 kind
= decode_value (p
, &p
);
1010 /* Can't decode this */
1013 if (target
->wrapper_type
== MONO_WRAPPER_STELEMREF
) {
1014 info
= mono_marshal_get_wrapper_info (target
);
1017 if (info
->subtype
== subtype
&& info
->d
.virtual_stelemref
.kind
== kind
)
1018 ref
->method
= target
;
1025 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid STELEMREF subtype %d", subtype
);
1030 case MONO_WRAPPER_SYNCHRONIZED
: {
1031 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1034 ref
->method
= mono_marshal_get_synchronized_wrapper (m
);
1037 case MONO_WRAPPER_UNKNOWN
: {
1038 int subtype
= decode_value (p
, &p
);
1040 if (subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
|| subtype
== WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
) {
1041 MonoClass
*klass
= decode_klass_ref (module
, p
, &p
, error
);
1047 if (klass
!= target
->klass
)
1050 if (subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
) {
1051 if (strcmp (target
->name
, "PtrToStructure"))
1053 ref
->method
= mono_marshal_get_ptr_to_struct (klass
);
1055 if (strcmp (target
->name
, "StructureToPtr"))
1057 ref
->method
= mono_marshal_get_struct_to_ptr (klass
);
1059 } else if (subtype
== WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
) {
1060 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1063 ref
->method
= mono_marshal_get_synchronized_inner_wrapper (m
);
1064 } else if (subtype
== WRAPPER_SUBTYPE_ARRAY_ACCESSOR
) {
1065 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1068 ref
->method
= mono_marshal_get_array_accessor_wrapper (m
);
1069 } else if (subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN
) {
1070 ref
->method
= mono_marshal_get_gsharedvt_in_wrapper ();
1071 } else if (subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT
) {
1072 ref
->method
= mono_marshal_get_gsharedvt_out_wrapper ();
1073 } else if (subtype
== WRAPPER_SUBTYPE_INTERP_IN
) {
1074 MonoMethodSignature
*sig
= decode_signature (module
, p
, &p
);
1077 ref
->method
= mini_get_interp_in_wrapper (sig
);
1078 } else if (subtype
== WRAPPER_SUBTYPE_INTERP_LMF
) {
1079 ref
->method
= mini_get_interp_lmf_wrapper ();
1080 } else if (subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
) {
1081 MonoMethodSignature
*sig
= decode_signature (module
, p
, &p
);
1084 ref
->method
= mini_get_gsharedvt_in_sig_wrapper (sig
);
1085 } else if (subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
) {
1086 MonoMethodSignature
*sig
= decode_signature (module
, p
, &p
);
1089 ref
->method
= mini_get_gsharedvt_out_sig_wrapper (sig
);
1091 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid UNKNOWN wrapper subtype %d", subtype
);
1096 case MONO_WRAPPER_MANAGED_TO_MANAGED
: {
1097 int subtype
= decode_value (p
, &p
);
1099 if (subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
) {
1100 int rank
= decode_value (p
, &p
);
1101 int elem_size
= decode_value (p
, &p
);
1103 ref
->method
= mono_marshal_get_array_address (rank
, elem_size
);
1104 } else if (subtype
== WRAPPER_SUBTYPE_STRING_CTOR
) {
1107 m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1113 g_assert (target
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_MANAGED
);
1115 info
= mono_marshal_get_wrapper_info (target
);
1116 if (info
&& info
->subtype
== subtype
&& info
->d
.string_ctor
.method
== m
)
1117 ref
->method
= target
;
1123 case MONO_WRAPPER_MANAGED_TO_NATIVE
: {
1125 int subtype
= decode_value (p
, &p
);
1128 if (subtype
== WRAPPER_SUBTYPE_ICALL_WRAPPER
) {
1133 if (strcmp (target
->name
, name
) != 0)
1135 ref
->method
= target
;
1137 m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1141 /* This should only happen when looking for an extra method */
1144 if (mono_marshal_method_from_wrapper (target
) == m
)
1145 ref
->method
= target
;
1151 case MONO_WRAPPER_CASTCLASS
: {
1152 int subtype
= decode_value (p
, &p
);
1154 if (subtype
== WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE
)
1155 ref
->method
= mono_marshal_get_castclass_with_cache ();
1156 else if (subtype
== WRAPPER_SUBTYPE_ISINST_WITH_CACHE
)
1157 ref
->method
= mono_marshal_get_isinst_with_cache ();
1159 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid CASTCLASS wrapper subtype %d", subtype
);
1164 case MONO_WRAPPER_RUNTIME_INVOKE
: {
1165 int subtype
= decode_value (p
, &p
);
1170 if (subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC
) {
1171 if (strcmp (target
->name
, "runtime_invoke_dynamic") != 0)
1173 ref
->method
= target
;
1174 } else if (subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
) {
1175 /* Direct wrapper */
1176 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1179 ref
->method
= mono_marshal_get_runtime_invoke (m
, FALSE
);
1180 } else if (subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
) {
1181 /* Virtual direct wrapper */
1182 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1185 ref
->method
= mono_marshal_get_runtime_invoke (m
, TRUE
);
1187 MonoMethodSignature
*sig
;
1189 sig
= decode_signature_with_target (module
, NULL
, p
, &p
);
1190 info
= mono_marshal_get_wrapper_info (target
);
1193 if (info
->subtype
!= subtype
)
1195 g_assert (info
->d
.runtime_invoke
.sig
);
1196 if (mono_metadata_signature_equal (sig
, info
->d
.runtime_invoke
.sig
))
1197 ref
->method
= target
;
1203 case MONO_WRAPPER_DELEGATE_INVOKE
:
1204 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
1205 case MONO_WRAPPER_DELEGATE_END_INVOKE
: {
1206 gboolean is_inflated
= decode_value (p
, &p
);
1207 WrapperSubtype subtype
;
1211 MonoMethod
*invoke
, *wrapper
;
1213 klass
= decode_klass_ref (module
, p
, &p
, error
);
1217 switch (wrapper_type
) {
1218 case MONO_WRAPPER_DELEGATE_INVOKE
:
1219 invoke
= mono_get_delegate_invoke (klass
);
1220 wrapper
= mono_marshal_get_delegate_invoke (invoke
, NULL
);
1222 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
1223 invoke
= mono_get_delegate_begin_invoke (klass
);
1224 wrapper
= mono_marshal_get_delegate_begin_invoke (invoke
);
1226 case MONO_WRAPPER_DELEGATE_END_INVOKE
:
1227 invoke
= mono_get_delegate_end_invoke (klass
);
1228 wrapper
= mono_marshal_get_delegate_end_invoke (invoke
);
1231 g_assert_not_reached ();
1236 * Due to the way mini_get_shared_method_full () works, we could end up with
1237 * multiple copies of the same wrapper.
1239 if (wrapper
->klass
!= target
->klass
)
1241 ref
->method
= target
;
1243 ref
->method
= wrapper
;
1247 * These wrappers are associated with a signature, not with a method.
1248 * Since we can't decode them into methods, they need a target method.
1253 if (wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
) {
1254 subtype
= (WrapperSubtype
)decode_value (p
, &p
);
1255 info
= mono_marshal_get_wrapper_info (target
);
1257 if (info
->subtype
!= subtype
)
1260 if (subtype
!= WRAPPER_SUBTYPE_NONE
)
1264 if (sig_matches_target (module
, target
, p
, &p
))
1265 ref
->method
= target
;
1271 case MONO_WRAPPER_NATIVE_TO_MANAGED
: {
1275 m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1278 klass
= decode_klass_ref (module
, p
, &p
, error
);
1281 ref
->method
= mono_marshal_get_managed_wrapper (m
, klass
, 0, error
);
1282 if (!mono_error_ok (error
))
1287 g_assert_not_reached ();
1289 } else if (image_index
== MONO_AOT_METHODREF_METHODSPEC
) {
1290 image_index
= decode_value (p
, &p
);
1291 ref
->token
= decode_value (p
, &p
);
1293 image
= load_image (module
, image_index
, error
);
1296 } else if (image_index
== MONO_AOT_METHODREF_GINST
) {
1298 MonoGenericContext ctx
;
1301 * These methods do not have a token which resolves them, so we
1302 * resolve them immediately.
1304 klass
= decode_klass_ref (module
, p
, &p
, error
);
1308 if (target
&& target
->klass
!= klass
)
1311 image_index
= decode_value (p
, &p
);
1312 ref
->token
= decode_value (p
, &p
);
1314 image
= load_image (module
, image_index
, error
);
1318 ref
->method
= mono_get_method_checked (image
, ref
->token
, NULL
, NULL
, error
);
1323 memset (&ctx
, 0, sizeof (ctx
));
1325 if (FALSE
&& mono_class_is_ginst (klass
)) {
1326 ctx
.class_inst
= mono_class_get_generic_class (klass
)->context
.class_inst
;
1327 ctx
.method_inst
= NULL
;
1329 ref
->method
= mono_class_inflate_generic_method_full_checked (ref
->method
, klass
, &ctx
, error
);
1334 memset (&ctx
, 0, sizeof (ctx
));
1336 if (!decode_generic_context (module
, &ctx
, p
, &p
, error
))
1339 ref
->method
= mono_class_inflate_generic_method_full_checked (ref
->method
, klass
, &ctx
, error
);
1343 } else if (image_index
== MONO_AOT_METHODREF_ARRAY
) {
1347 klass
= decode_klass_ref (module
, p
, &p
, error
);
1350 method_type
= decode_value (p
, &p
);
1351 switch (method_type
) {
1353 ref
->method
= mono_class_get_method_from_name_checked (klass
, ".ctor", m_class_get_rank (klass
), 0, error
);
1354 return_val_if_nok (error
, FALSE
);
1357 ref
->method
= mono_class_get_method_from_name_checked (klass
, ".ctor", m_class_get_rank (klass
) * 2, 0, error
);
1358 return_val_if_nok (error
, FALSE
);
1361 ref
->method
= mono_class_get_method_from_name_checked (klass
, "Get", -1, 0, error
);
1362 return_val_if_nok (error
, FALSE
);
1365 ref
->method
= mono_class_get_method_from_name_checked (klass
, "Address", -1, 0, error
);
1366 return_val_if_nok (error
, FALSE
);
1369 ref
->method
= mono_class_get_method_from_name_checked (klass
, "Set", -1, 0, error
);
1370 return_val_if_nok (error
, FALSE
);
1373 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid METHODREF_ARRAY method type %d", method_type
);
1377 if (image_index
== MONO_AOT_METHODREF_LARGE_IMAGE_INDEX
) {
1378 image_index
= decode_value (p
, &p
);
1379 value
= decode_value (p
, &p
);
1382 ref
->token
= MONO_TOKEN_METHOD_DEF
| (value
& 0xffffff);
1384 image
= load_image (module
, image_index
, error
);
1397 decode_method_ref (MonoAotModule
*module
, MethodRef
*ref
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
1399 return decode_method_ref_with_target (module
, ref
, NULL
, buf
, endbuf
, error
);
1403 * decode_resolve_method_ref_with_target:
1405 * Similar to decode_method_ref, but resolve and return the method itself.
1408 decode_resolve_method_ref_with_target (MonoAotModule
*module
, MonoMethod
*target
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
1414 if (!decode_method_ref_with_target (module
, &ref
, target
, buf
, endbuf
, error
))
1419 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "No image found for methodref with target");
1422 return mono_get_method_checked (ref
.image
, ref
.token
, NULL
, NULL
, error
);
1426 decode_resolve_method_ref (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
1428 return decode_resolve_method_ref_with_target (module
, NULL
, buf
, endbuf
, error
);
1431 #ifdef ENABLE_AOT_CACHE
1437 * - Add options for controlling the cache size
1438 * - Handle full cache by deleting old assemblies lru style
1439 * - Maybe add a threshold after an assembly is AOT compiled
1440 * - Add options for enabling this for specific main assemblies
1443 /* The cache directory */
1444 static char *cache_dir
;
1446 /* The number of assemblies AOTed in this run */
1447 static int cache_count
;
1449 /* Whenever to AOT in-process */
1450 static gboolean in_process
;
1453 collect_assemblies (gpointer data
, gpointer user_data
)
1455 MonoAssembly
*ass
= (MonoAssembly
*)data
;
1456 GSList
**l
= (GSList
**)user_data
;
1458 *l
= g_slist_prepend (*l
, ass
);
1461 #define SHA1_DIGEST_LENGTH 20
1464 * get_aot_config_hash:
1466 * Return a hash for all the version information an AOT module depends on.
1468 static G_GNUC_UNUSED
char*
1469 get_aot_config_hash (MonoAssembly
*assembly
)
1472 GSList
*l
, *assembly_list
= NULL
;
1475 guint8 digest
[SHA1_DIGEST_LENGTH
];
1478 build_info
= mono_get_runtime_build_info ();
1480 s
= g_string_new (build_info
);
1482 mono_assembly_foreach (collect_assemblies
, &assembly_list
);
1485 * The assembly list includes the current assembly as well, no need
1488 for (l
= assembly_list
; l
; l
= l
->next
) {
1489 MonoAssembly
*ass
= (MonoAssembly
*)l
->data
;
1491 g_string_append (s
, "_");
1492 g_string_append (s
, ass
->aname
.name
);
1493 g_string_append (s
, "_");
1494 g_string_append (s
, ass
->image
->guid
);
1497 for (i
= 0; i
< s
->len
; ++i
) {
1498 if (!isalnum (s
->str
[i
]) && s
->str
[i
] != '-')
1502 mono_sha1_get_digest ((guint8
*)s
->str
, s
->len
, digest
);
1504 digest_str
= g_malloc0 ((SHA1_DIGEST_LENGTH
* 2) + 1);
1505 for (i
= 0; i
< SHA1_DIGEST_LENGTH
; ++i
)
1506 sprintf (digest_str
+ (i
* 2), "%02x", digest
[i
]);
1508 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: file dependencies: %s, hash %s", s
->str
, digest_str
);
1510 g_string_free (s
, TRUE
);
1516 aot_cache_init (void)
1520 enable_aot_cache
= TRUE
;
1525 * aot_cache_load_module:
1527 * Load the AOT image corresponding to ASSEMBLY from the aot cache, AOTing it if neccessary.
1530 aot_cache_load_module (MonoAssembly
*assembly
, char **aot_name
)
1532 MonoAotCacheConfig
*config
;
1534 char *fname
, *tmp2
, *aot_options
, *failure_fname
;
1546 if (image_is_dynamic (assembly
->image
))
1549 /* Check in the list of assemblies enabled for aot caching */
1550 config
= mono_get_aot_cache_config ();
1554 MonoDomain
*domain
= mono_domain_get ();
1555 MonoAssembly
*entry_assembly
= domain
->entry_assembly
;
1557 // FIXME: This cannot be used for mscorlib during startup, since entry_assembly is not set yet
1558 for (l
= config
->apps
; l
; l
= l
->next
) {
1559 char *n
= (char*)l
->data
;
1561 if ((entry_assembly
&& !strcmp (entry_assembly
->aname
.name
, n
)) || (!entry_assembly
&& !strcmp (assembly
->aname
.name
, n
)))
1569 for (l
= config
->assemblies
; l
; l
= l
->next
) {
1570 char *n
= (char*)l
->data
;
1572 if (!strcmp (assembly
->aname
.name
, n
))
1582 home
= g_get_home_dir ();
1585 cache_dir
= g_strdup_printf ("%s/Library/Caches/mono/aot-cache", home
);
1586 if (!g_file_test (cache_dir
, (GFileTest
)(G_FILE_TEST_EXISTS
|G_FILE_TEST_IS_DIR
)))
1587 g_mkdir_with_parents (cache_dir
, 0777);
1591 * The same assembly can be used in multiple configurations, i.e. multiple
1592 * versions of the runtime, with multiple versions of dependent assemblies etc.
1593 * To handle this, we compute a version string containing all this information, hash it,
1594 * and use the hash as a filename suffix.
1596 hash
= get_aot_config_hash (assembly
);
1598 tmp2
= g_strdup_printf ("%s-%s%s", assembly
->image
->assembly_name
, hash
, MONO_SOLIB_EXT
);
1599 fname
= g_build_filename (cache_dir
, tmp2
, NULL
);
1603 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: loading from cache: '%s'.", fname
);
1604 module
= mono_dl_open (fname
, MONO_DL_LAZY
, NULL
);
1607 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: found in cache: '%s'.", fname
);
1611 if (!strcmp (assembly
->aname
.name
, "mscorlib") && !mscorlib_aot_loaded
)
1613 * Can't AOT this during startup, so we AOT it when called later from
1614 * mono_aot_get_method ().
1618 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: not found.");
1620 /* Only AOT one assembly per run to avoid slowing down execution too much */
1621 if (cache_count
> 0)
1625 /* Check for previous failure */
1626 failure_fname
= g_strdup_printf ("%s.failure", fname
);
1627 failure_file
= fopen (failure_fname
, "r");
1629 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: assembly '%s' previously failed to compile '%s' ('%s')... ", assembly
->image
->name
, fname
, failure_fname
);
1630 g_free (failure_fname
);
1633 g_free (failure_fname
);
1634 fclose (failure_file
);
1637 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: compiling assembly '%s', logfile: '%s.log'... ", assembly
->image
->name
, fname
);
1640 * We need to invoke the AOT compiler here. There are multiple approaches:
1641 * - spawn a new runtime process. This can be hard when running with mkbundle, and
1642 * its hard to make the new process load the same set of assemblies.
1643 * - doing it in-process. This exposes the current process to bugs/leaks/side effects of
1645 * - fork a new process and do the work there.
1648 aot_options
= g_strdup_printf ("outfile=%s,internal-logfile=%s.log%s%s", fname
, fname
, config
->aot_options
? "," : "", config
->aot_options
? config
->aot_options
: "");
1649 /* Maybe due this in another thread ? */
1650 res
= mono_compile_assembly (assembly
, mono_parse_default_optimizations (NULL
), aot_options
, NULL
);
1652 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: compilation failed.");
1653 failure_fname
= g_strdup_printf ("%s.failure", fname
);
1654 failure_file
= fopen (failure_fname
, "a+");
1655 fclose (failure_file
);
1656 g_free (failure_fname
);
1658 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: compilation succeeded.");
1662 * - Avoid waiting for the aot process to finish ?
1663 * (less overhead, but multiple processes could aot the same assembly at the same time)
1672 logfile_name
= g_strdup_printf ("%s/aot.log", cache_dir
);
1673 logfile
= fopen (logfile_name
, "a+");
1674 g_free (logfile_name
);
1676 dup2 (fileno (logfile
), 1);
1677 dup2 (fileno (logfile
), 2);
1679 aot_options
= g_strdup_printf ("outfile=%s", fname
);
1680 res
= mono_compile_assembly (assembly
, mono_parse_default_optimizations (NULL
), aot_options
, NULL
);
1688 waitpid (pid
, &exit_status
, 0);
1689 if (!WIFEXITED (exit_status
) && (WEXITSTATUS (exit_status
) == 0))
1690 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: failed.");
1692 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: succeeded.");
1696 module
= mono_dl_open (fname
, MONO_DL_LAZY
, NULL
);
1704 aot_cache_init (void)
1709 aot_cache_load_module (MonoAssembly
*assembly
, char **aot_name
)
1717 find_symbol (MonoDl
*module
, gpointer
*globals
, const char *name
, gpointer
*value
)
1721 guint16
*table
, *entry
;
1724 char *symbol
= (char*)name
;
1727 symbol
= g_strdup_printf ("_%s", name
);
1730 /* The first entry points to the hash */
1731 table
= (guint16
*)globals
[0];
1734 table_size
= table
[0];
1737 hash
= mono_metadata_str_hash (symbol
) % table_size
;
1739 entry
= &table
[hash
* 2];
1741 /* Search the hash for the index into the globals table */
1743 while (entry
[0] != 0) {
1744 guint32 index
= entry
[0] - 1;
1745 guint32 next
= entry
[1];
1747 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
1749 if (!strcmp ((const char*)globals
[index
* 2], symbol
)) {
1750 global_index
= index
;
1755 entry
= &table
[next
* 2];
1761 if (global_index
!= -1)
1762 *value
= globals
[global_index
* 2 + 1];
1769 char *err
= mono_dl_symbol (module
, name
, value
);
1777 find_amodule_symbol (MonoAotModule
*amodule
, const char *name
, gpointer
*value
)
1779 g_assert (!(amodule
->info
.flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
));
1781 find_symbol (amodule
->sofile
, amodule
->globals
, name
, value
);
1785 mono_install_load_aot_data_hook (MonoLoadAotDataFunc load_func
, MonoFreeAotDataFunc free_func
, gpointer user_data
)
1787 aot_data_load_func
= load_func
;
1788 aot_data_free_func
= free_func
;
1789 aot_data_func_user_data
= user_data
;
1792 /* Load the separate aot data file for ASSEMBLY */
1794 open_aot_data (MonoAssembly
*assembly
, MonoAotFileInfo
*info
, void **ret_handle
)
1800 if (aot_data_load_func
) {
1801 data
= aot_data_load_func (assembly
, info
->datafile_size
, aot_data_func_user_data
, ret_handle
);
1807 * Use <assembly name>.aotdata as the default implementation if no callback is given
1809 filename
= g_strdup_printf ("%s.aotdata", assembly
->image
->name
);
1810 map
= mono_file_map_open (filename
);
1812 data
= (guint8
*)mono_file_map (info
->datafile_size
, MONO_MMAP_READ
, mono_file_map_fd (map
), 0, ret_handle
);
1819 check_usable (MonoAssembly
*assembly
, MonoAotFileInfo
*info
, guint8
*blob
, char **out_msg
)
1823 gboolean usable
= TRUE
;
1824 gboolean full_aot
, interp
, safepoints
;
1825 guint32 excluded_cpu_optimizations
;
1827 if (strcmp (assembly
->image
->guid
, (const char*)info
->assembly_guid
)) {
1828 msg
= g_strdup_printf ("doesn't match assembly");
1832 build_info
= mono_get_runtime_build_info ();
1833 if (strlen ((const char *)info
->runtime_version
) > 0 && strcmp (info
->runtime_version
, build_info
)) {
1834 msg
= g_strdup_printf ("compiled against runtime version '%s' while this runtime has version '%s'", info
->runtime_version
, build_info
);
1837 g_free (build_info
);
1839 full_aot
= info
->flags
& MONO_AOT_FILE_FLAG_FULL_AOT
;
1840 interp
= info
->flags
& MONO_AOT_FILE_FLAG_INTERP
;
1842 if (mono_aot_only
&& !full_aot
) {
1844 msg
= g_strdup_printf ("not compiled with --aot=full");
1848 if (!mono_aot_only
&& full_aot
) {
1849 msg
= g_strdup_printf ("compiled with --aot=full");
1852 if (mono_use_interpreter
&& !interp
) {
1853 msg
= g_strdup_printf ("not compiled with --aot=interp");
1856 if (mono_llvm_only
&& !(info
->flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
)) {
1857 msg
= g_strdup_printf ("not compiled with --aot=llvmonly");
1860 if (mono_use_llvm
&& !(info
->flags
& MONO_AOT_FILE_FLAG_WITH_LLVM
)) {
1861 /* Prefer LLVM JITted code when using --llvm */
1862 msg
= g_strdup_printf ("not compiled with --aot=llvm");
1865 if (mini_get_debug_options ()->mdb_optimizations
&& !(info
->flags
& MONO_AOT_FILE_FLAG_DEBUG
) && !full_aot
) {
1866 msg
= g_strdup_printf ("not compiled for debugging");
1870 mono_arch_cpu_optimizations (&excluded_cpu_optimizations
);
1871 if (info
->opts
& excluded_cpu_optimizations
) {
1872 msg
= g_strdup_printf ("compiled with unsupported CPU optimizations");
1876 if (!mono_aot_only
&& (info
->simd_opts
& ~mono_arch_cpu_enumerate_simd_versions ())) {
1877 msg
= g_strdup_printf ("compiled with unsupported SIMD extensions");
1881 if (info
->gc_name_index
!= -1) {
1882 char *gc_name
= (char*)&blob
[info
->gc_name_index
];
1883 const char *current_gc_name
= mono_gc_get_gc_name ();
1885 if (strcmp (current_gc_name
, gc_name
) != 0) {
1886 msg
= g_strdup_printf ("compiled against GC %s, while the current runtime uses GC %s.\n", gc_name
, current_gc_name
);
1891 safepoints
= info
->flags
& MONO_AOT_FILE_FLAG_SAFEPOINTS
;
1893 if (!safepoints
&& mono_threads_are_safepoints_enabled ()) {
1894 msg
= g_strdup_printf ("not compiled with safepoints");
1903 * TABLE should point to a table of call instructions. Return the address called by the INDEXth entry.
1906 get_call_table_entry (void *table
, int index
)
1908 #if defined(TARGET_ARM)
1913 ins_addr
= (guint32
*)table
+ index
;
1915 if ((ins
>> ARMCOND_SHIFT
) == ARMCOND_NV
) {
1917 offset
= (((int)(((ins
& 0xffffff) << 1) | ((ins
>> 24) & 0x1))) << 7) >> 7;
1918 return (char*)ins_addr
+ (offset
* 2) + 8 + 1;
1920 offset
= (((int)ins
& 0xffffff) << 8) >> 8;
1921 return (char*)ins_addr
+ (offset
* 4) + 8;
1923 #elif defined(TARGET_ARM64)
1924 return mono_arch_get_call_target ((guint8
*)table
+ (index
* 4) + 4);
1925 #elif defined(TARGET_X86) || defined(TARGET_AMD64)
1926 /* The callee expects an ip which points after the call */
1927 return mono_arch_get_call_target ((guint8
*)table
+ (index
* 5) + 5);
1929 g_assert_not_reached ();
1937 * Initialize the shared got entries for AMODULE.
1940 init_amodule_got (MonoAotModule
*amodule
)
1944 MonoJumpInfo
*patches
;
1945 guint32 got_offsets
[128];
1949 /* These can't be initialized in load_aot_module () */
1950 if (amodule
->shared_got
[0] || amodule
->got_initializing
)
1953 amodule
->got_initializing
= TRUE
;
1955 mp
= mono_mempool_new ();
1956 npatches
= amodule
->info
.nshared_got_entries
;
1957 for (i
= 0; i
< npatches
; ++i
)
1958 got_offsets
[i
] = i
;
1959 patches
= decode_patches (amodule
, mp
, npatches
, FALSE
, got_offsets
);
1961 for (i
= 0; i
< npatches
; ++i
) {
1964 if (ji
->type
== MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
&& !mono_gc_is_moving ()) {
1965 amodule
->shared_got
[i
] = NULL
;
1966 } else if (ji
->type
== MONO_PATCH_INFO_GC_NURSERY_START
&& !mono_gc_is_moving ()) {
1967 amodule
->shared_got
[i
] = NULL
;
1968 } else if (ji
->type
== MONO_PATCH_INFO_GC_NURSERY_BITS
&& !mono_gc_is_moving ()) {
1969 amodule
->shared_got
[i
] = NULL
;
1970 } else if (ji
->type
== MONO_PATCH_INFO_IMAGE
) {
1971 amodule
->shared_got
[i
] = amodule
->assembly
->image
;
1972 } else if (ji
->type
== MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
) {
1973 if (mono_defaults
.corlib
) {
1974 MonoAotModule
*mscorlib_amodule
= mono_defaults
.corlib
->aot_module
;
1976 if (mscorlib_amodule
)
1977 amodule
->shared_got
[i
] = mscorlib_amodule
->got
;
1979 amodule
->shared_got
[i
] = amodule
->got
;
1981 } else if (ji
->type
== MONO_PATCH_INFO_AOT_MODULE
) {
1982 amodule
->shared_got
[i
] = amodule
;
1984 amodule
->shared_got
[i
] = mono_resolve_patch_target (NULL
, mono_get_root_domain (), NULL
, ji
, FALSE
, error
);
1985 mono_error_assert_ok (error
);
1990 for (i
= 0; i
< npatches
; ++i
)
1991 amodule
->got
[i
] = amodule
->shared_got
[i
];
1993 if (amodule
->llvm_got
) {
1994 for (i
= 0; i
< npatches
; ++i
)
1995 amodule
->llvm_got
[i
] = amodule
->shared_got
[i
];
1998 mono_mempool_destroy (mp
);
2002 load_aot_module (MonoAssembly
*assembly
, gpointer user_data
)
2004 char *aot_name
, *found_aot_name
;
2005 MonoAotModule
*amodule
;
2007 gboolean usable
= TRUE
;
2008 char *version_symbol
= NULL
;
2010 gpointer
*globals
= NULL
;
2011 MonoAotFileInfo
*info
= NULL
;
2013 gboolean do_load_image
= TRUE
;
2014 int align_double
, align_int64
;
2015 guint8
*aot_data
= NULL
;
2017 if (mono_compile_aot
)
2020 if (assembly
->image
->aot_module
)
2022 * Already loaded. This can happen because the assembly loading code might invoke
2023 * the assembly load hooks multiple times for the same assembly.
2027 if (image_is_dynamic (assembly
->image
) || mono_asmctx_get_kind (&assembly
->context
) == MONO_ASMCTX_REFONLY
|| mono_domain_get () != mono_get_root_domain ())
2032 if (container_assm_name
&& !container_amodule
) {
2033 char *local_ref
= container_assm_name
;
2034 container_assm_name
= NULL
;
2035 MonoImageOpenStatus status
= MONO_IMAGE_OK
;
2036 gchar
*dll
= g_strdup_printf ( "%s.dll", local_ref
);
2037 MonoAssembly
*assm
= mono_assembly_open_a_lot (dll
, &status
, MONO_ASMCTX_DEFAULT
);
2039 gchar
*exe
= g_strdup_printf ("%s.exe", local_ref
);
2040 assm
= mono_assembly_open_a_lot (exe
, &status
, MONO_ASMCTX_DEFAULT
);
2043 load_aot_module (assm
, NULL
);
2044 container_amodule
= assm
->image
->aot_module
;
2047 if (static_aot_modules
)
2048 info
= (MonoAotFileInfo
*)g_hash_table_lookup (static_aot_modules
, assembly
->aname
.name
);
2054 found_aot_name
= NULL
;
2057 /* Statically linked AOT module */
2058 aot_name
= g_strdup_printf ("%s", assembly
->aname
.name
);
2059 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "Found statically linked AOT module '%s'.", aot_name
);
2060 if (!(info
->flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
)) {
2061 globals
= (void **)info
->globals
;
2064 found_aot_name
= g_strdup (aot_name
);
2068 if (enable_aot_cache
)
2069 sofile
= aot_cache_load_module (assembly
, &aot_name
);
2071 aot_name
= g_strdup_printf ("%s%s", assembly
->image
->name
, MONO_SOLIB_EXT
);
2073 sofile
= mono_dl_open (aot_name
, MONO_DL_LAZY
, &err
);
2075 found_aot_name
= g_strdup (aot_name
);
2077 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: image '%s' not found: %s", aot_name
, err
);
2083 char *basename
= g_path_get_basename (assembly
->image
->name
);
2084 aot_name
= g_strdup_printf ("%s/mono/aot-cache/%s/%s%s", mono_assembly_getrootdir(), MONO_ARCHITECTURE
, basename
, MONO_SOLIB_EXT
);
2086 sofile
= mono_dl_open (aot_name
, MONO_DL_LAZY
, &err
);
2088 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: image '%s' not found: %s", aot_name
, err
);
2096 for (l
= mono_aot_paths
; l
; l
= l
->next
) {
2097 char *path
= (char*)l
->data
;
2099 char *basename
= g_path_get_basename (assembly
->image
->name
);
2100 aot_name
= g_strdup_printf ("%s/%s%s", path
, basename
, MONO_SOLIB_EXT
);
2101 sofile
= mono_dl_open (aot_name
, MONO_DL_LAZY
, &err
);
2103 found_aot_name
= g_strdup (aot_name
);
2105 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: image '%s' not found: %s", aot_name
, err
);
2115 if (mono_aot_only
&& !mono_use_interpreter
&& assembly
->image
->tables
[MONO_TABLE_METHOD
].rows
) {
2116 aot_name
= g_strdup_printf ("%s%s", assembly
->image
->name
, MONO_SOLIB_EXT
);
2117 g_error ("Failed to load AOT module '%s' in aot-only mode.\n", aot_name
);
2125 find_symbol (sofile
, globals
, "mono_aot_version", (gpointer
*) &version_symbol
);
2126 find_symbol (sofile
, globals
, "mono_aot_file_info", (gpointer
*)&info
);
2129 // Copy aotid to MonoImage
2130 memcpy(&assembly
->image
->aotid
, info
->aotid
, 16);
2132 if (version_symbol
) {
2133 /* Old file format */
2134 version
= atoi (version_symbol
);
2137 version
= info
->version
;
2140 if (version
!= MONO_AOT_FILE_VERSION
) {
2141 msg
= g_strdup_printf ("wrong file format version (expected %d got %d)", MONO_AOT_FILE_VERSION
, version
);
2147 if (info
->flags
& MONO_AOT_FILE_FLAG_SEPARATE_DATA
) {
2148 aot_data
= open_aot_data (assembly
, info
, &handle
);
2150 blob
= aot_data
+ info
->table_offsets
[MONO_AOT_TABLE_BLOB
];
2152 blob
= (guint8
*)info
->blob
;
2155 usable
= check_usable (assembly
, info
, blob
, &msg
);
2159 if (mono_aot_only
) {
2160 g_error ("Failed to load AOT module '%s' while running in aot-only mode: %s.\n", found_aot_name
, msg
);
2162 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: module %s is unusable: %s.", found_aot_name
, msg
);
2165 g_free (found_aot_name
);
2167 mono_dl_close (sofile
);
2168 assembly
->image
->aot_module
= NULL
;
2173 align_double
= MONO_ABI_ALIGNOF (double);
2174 align_int64
= MONO_ABI_ALIGNOF (gint64
);
2175 int card_table_shift_bits
= 0;
2176 gpointer card_table_mask
= NULL
;
2177 mono_gc_get_card_table (&card_table_shift_bits
, &card_table_mask
);
2179 g_assert (info
->double_align
== align_double
);
2180 g_assert (info
->long_align
== align_int64
);
2181 g_assert (info
->generic_tramp_num
== MONO_TRAMPOLINE_NUM
);
2182 g_assert (info
->card_table_shift_bits
== card_table_shift_bits
);
2183 g_assert (info
->card_table_mask
== GPOINTER_TO_UINT (card_table_mask
));
2185 amodule
= g_new0 (MonoAotModule
, 1);
2186 amodule
->aot_name
= found_aot_name
;
2187 amodule
->assembly
= assembly
;
2189 memcpy (&amodule
->info
, info
, sizeof (*info
));
2191 amodule
->got
= (void **)amodule
->info
.jit_got
;
2192 amodule
->llvm_got
= (void **)amodule
->info
.llvm_got
;
2193 amodule
->globals
= globals
;
2194 amodule
->sofile
= sofile
;
2195 amodule
->method_to_code
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
2196 amodule
->extra_methods
= g_hash_table_new (NULL
, NULL
);
2197 amodule
->shared_got
= g_new0 (gpointer
, info
->nshared_got_entries
);
2199 if (info
->flags
& MONO_AOT_FILE_FLAG_SEPARATE_DATA
) {
2200 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
2201 amodule
->tables
[i
] = aot_data
+ info
->table_offsets
[i
];
2204 mono_os_mutex_init_recursive (&amodule
->mutex
);
2206 /* Read image table */
2208 guint32 table_len
, i
;
2211 if (info
->flags
& MONO_AOT_FILE_FLAG_SEPARATE_DATA
)
2212 table
= (char *)amodule
->tables
[MONO_AOT_TABLE_IMAGE_TABLE
];
2214 table
= (char *)info
->image_table
;
2217 table_len
= *(guint32
*)table
;
2218 table
+= sizeof (guint32
);
2219 amodule
->image_table
= g_new0 (MonoImage
*, table_len
);
2220 amodule
->image_names
= g_new0 (MonoAssemblyName
, table_len
);
2221 amodule
->image_guids
= g_new0 (char*, table_len
);
2222 amodule
->image_table_len
= table_len
;
2223 for (i
= 0; i
< table_len
; ++i
) {
2224 MonoAssemblyName
*aname
= &(amodule
->image_names
[i
]);
2226 aname
->name
= g_strdup (table
);
2227 table
+= strlen (table
) + 1;
2228 amodule
->image_guids
[i
] = g_strdup (table
);
2229 table
+= strlen (table
) + 1;
2231 aname
->culture
= g_strdup (table
);
2232 table
+= strlen (table
) + 1;
2233 memcpy (aname
->public_key_token
, table
, strlen (table
) + 1);
2234 table
+= strlen (table
) + 1;
2236 table
= (char *)ALIGN_PTR_TO (table
, 8);
2237 aname
->flags
= *(guint32
*)table
;
2239 aname
->major
= *(guint32
*)table
;
2241 aname
->minor
= *(guint32
*)table
;
2243 aname
->build
= *(guint32
*)table
;
2245 aname
->revision
= *(guint32
*)table
;
2250 amodule
->jit_code_start
= (guint8
*)info
->jit_code_start
;
2251 amodule
->jit_code_end
= (guint8
*)info
->jit_code_end
;
2252 if (info
->flags
& MONO_AOT_FILE_FLAG_SEPARATE_DATA
) {
2253 amodule
->blob
= (guint8
*)amodule
->tables
[MONO_AOT_TABLE_BLOB
];
2254 amodule
->method_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_METHOD_INFO_OFFSETS
];
2255 amodule
->ex_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_EX_INFO_OFFSETS
];
2256 amodule
->class_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_CLASS_INFO_OFFSETS
];
2257 amodule
->class_name_table
= (guint16
*)amodule
->tables
[MONO_AOT_TABLE_CLASS_NAME
];
2258 amodule
->extra_method_table
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_EXTRA_METHOD_TABLE
];
2259 amodule
->extra_method_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS
];
2260 amodule
->got_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_GOT_INFO_OFFSETS
];
2261 amodule
->llvm_got_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_LLVM_GOT_INFO_OFFSETS
];
2262 amodule
->weak_field_indexes
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_WEAK_FIELD_INDEXES
];
2264 amodule
->blob
= (guint8
*)info
->blob
;
2265 amodule
->method_info_offsets
= (guint32
*)info
->method_info_offsets
;
2266 amodule
->ex_info_offsets
= (guint32
*)info
->ex_info_offsets
;
2267 amodule
->class_info_offsets
= (guint32
*)info
->class_info_offsets
;
2268 amodule
->class_name_table
= (guint16
*)info
->class_name_table
;
2269 amodule
->extra_method_table
= (guint32
*)info
->extra_method_table
;
2270 amodule
->extra_method_info_offsets
= (guint32
*)info
->extra_method_info_offsets
;
2271 amodule
->got_info_offsets
= (guint32
*)info
->got_info_offsets
;
2272 amodule
->llvm_got_info_offsets
= (guint32
*)info
->llvm_got_info_offsets
;
2273 amodule
->weak_field_indexes
= (guint32
*)info
->weak_field_indexes
;
2275 amodule
->unbox_trampolines
= (guint32
*)info
->unbox_trampolines
;
2276 amodule
->unbox_trampolines_end
= (guint32
*)info
->unbox_trampolines_end
;
2277 amodule
->unbox_trampoline_addresses
= (guint32
*)info
->unbox_trampoline_addresses
;
2278 amodule
->unwind_info
= (guint8
*)info
->unwind_info
;
2279 amodule
->mem_begin
= (guint8
*)amodule
->jit_code_start
;
2280 amodule
->mem_end
= (guint8
*)info
->mem_end
;
2281 amodule
->plt
= (guint8
*)info
->plt
;
2282 amodule
->plt_end
= (guint8
*)info
->plt_end
;
2283 amodule
->mono_eh_frame
= (guint8
*)info
->mono_eh_frame
;
2284 amodule
->trampolines
[MONO_AOT_TRAMP_SPECIFIC
] = (guint8
*)info
->specific_trampolines
;
2285 amodule
->trampolines
[MONO_AOT_TRAMP_STATIC_RGCTX
] = (guint8
*)info
->static_rgctx_trampolines
;
2286 amodule
->trampolines
[MONO_AOT_TRAMP_IMT
] = (guint8
*)info
->imt_trampolines
;
2287 amodule
->trampolines
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = (guint8
*)info
->gsharedvt_arg_trampolines
;
2288 amodule
->trampolines
[MONO_AOT_TRAMP_FTNPTR_ARG
] = (guint8
*)info
->ftnptr_arg_trampolines
;
2290 if (!strcmp (assembly
->aname
.name
, "mscorlib"))
2291 mscorlib_aot_module
= amodule
;
2293 /* Compute method addresses */
2294 amodule
->methods
= (void **)g_malloc0 (amodule
->info
.nmethods
* sizeof (gpointer
));
2295 for (i
= 0; i
< amodule
->info
.nmethods
; ++i
) {
2298 if (amodule
->info
.llvm_get_method
) {
2299 gpointer (*get_method
) (int) = (gpointer (*)(int))amodule
->info
.llvm_get_method
;
2301 addr
= get_method (i
);
2304 /* method_addresses () contains a table of branches, since the ios linker can update those correctly */
2305 if (!addr
&& amodule
->info
.method_addresses
) {
2306 addr
= get_call_table_entry (amodule
->info
.method_addresses
, i
);
2308 if (addr
== amodule
->info
.method_addresses
)
2312 amodule
->methods
[i
] = GINT_TO_POINTER (-1);
2314 amodule
->methods
[i
] = addr
;
2317 if (make_unreadable
) {
2318 #ifndef TARGET_WIN32
2320 guint8
*page_start
, *page_end
;
2323 addr
= amodule
->mem_begin
;
2325 len
= amodule
->mem_end
- amodule
->mem_begin
;
2327 /* Round down in both directions to avoid modifying data which is not ours */
2328 page_start
= (guint8
*) (((gssize
) (addr
)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
2329 page_end
= (guint8
*) (((gssize
) (addr
+ len
)) & ~ (mono_pagesize () - 1));
2330 if (page_end
> page_start
) {
2331 err
= mono_mprotect (page_start
, (page_end
- page_start
), MONO_MMAP_NONE
);
2332 g_assert (err
== 0);
2337 /* Compute the boundaries of LLVM code */
2338 if (info
->flags
& MONO_AOT_FILE_FLAG_WITH_LLVM
)
2339 compute_llvm_code_range (amodule
, &amodule
->llvm_code_start
, &amodule
->llvm_code_end
);
2343 if (amodule
->jit_code_start
) {
2344 aot_code_low_addr
= MIN (aot_code_low_addr
, (gsize
)amodule
->jit_code_start
);
2345 aot_code_high_addr
= MAX (aot_code_high_addr
, (gsize
)amodule
->jit_code_end
);
2347 if (amodule
->llvm_code_start
) {
2348 aot_code_low_addr
= MIN (aot_code_low_addr
, (gsize
)amodule
->llvm_code_start
);
2349 aot_code_high_addr
= MAX (aot_code_high_addr
, (gsize
)amodule
->llvm_code_end
);
2352 g_hash_table_insert (aot_modules
, assembly
, amodule
);
2355 if (amodule
->jit_code_start
)
2356 mono_jit_info_add_aot_module (assembly
->image
, amodule
->jit_code_start
, amodule
->jit_code_end
);
2357 if (amodule
->llvm_code_start
)
2358 mono_jit_info_add_aot_module (assembly
->image
, amodule
->llvm_code_start
, amodule
->llvm_code_end
);
2360 assembly
->image
->aot_module
= amodule
;
2362 if (mono_aot_only
&& !mono_llvm_only
) {
2364 find_amodule_symbol (amodule
, "specific_trampolines_page", (gpointer
*)&code
);
2365 amodule
->use_page_trampolines
= code
!= NULL
;
2366 /*g_warning ("using page trampolines: %d", amodule->use_page_trampolines);*/
2370 * Register the plt region as a single trampoline so we can unwind from this code
2372 mono_aot_tramp_info_register (
2373 mono_tramp_info_create (
2376 amodule
->plt_end
- amodule
->plt
,
2378 mono_unwind_get_cie_program ()
2384 * Since we store methoddef and classdef tokens when referring to methods/classes in
2385 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
2386 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
2387 * non-lazily, since we can't handle out-of-date errors later.
2388 * The cached class info also depends on the exact assemblies.
2390 if (do_load_image
) {
2391 for (i
= 0; i
< amodule
->image_table_len
; ++i
) {
2393 load_image (amodule
, i
, error
);
2394 mono_error_cleanup (error
); /* FIXME don't swallow the error */
2398 if (amodule
->out_of_date
) {
2399 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: Module %s is unusable because a dependency is out-of-date.", assembly
->image
->name
);
2401 g_error ("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", found_aot_name
);
2403 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: image '%s' found.", found_aot_name
);
2408 * mono_aot_register_module:
2410 * This should be called by embedding code to register normal AOT modules statically linked
2411 * into the executable.
2413 * \param aot_info the value of the 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
2416 mono_aot_register_module (gpointer
*aot_info
)
2420 MonoAotFileInfo
*info
= (MonoAotFileInfo
*)aot_info
;
2422 g_assert (info
->version
== MONO_AOT_FILE_VERSION
);
2424 if (!(info
->flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
)) {
2425 globals
= (void **)info
->globals
;
2429 aname
= (char *)info
->assembly_name
;
2431 /* This could be called before startup */
2435 if (!static_aot_modules
)
2436 static_aot_modules
= g_hash_table_new (g_str_hash
, g_str_equal
);
2438 g_hash_table_insert (static_aot_modules
, aname
, info
);
2440 if (info
->flags
& MONO_AOT_FILE_FLAG_EAGER_LOAD
) {
2441 g_assert (!container_assm_name
);
2442 container_assm_name
= aname
;
2450 mono_aot_init (void)
2452 mono_os_mutex_init_recursive (&aot_mutex
);
2453 mono_os_mutex_init_recursive (&aot_page_mutex
);
2454 aot_modules
= g_hash_table_new (NULL
, NULL
);
2456 mono_install_assembly_load_hook (load_aot_module
, NULL
);
2457 mono_counters_register ("Async JIT info size", MONO_COUNTER_INT
|MONO_COUNTER_JIT
, &async_jit_info_size
);
2459 char *lastaot
= g_getenv ("MONO_LASTAOT");
2461 mono_last_aot_method
= atoi (lastaot
);
2468 mono_aot_cleanup (void)
2470 g_hash_table_destroy (aot_jit_icall_hash
);
2471 g_hash_table_destroy (aot_modules
);
2475 decode_cached_class_info (MonoAotModule
*module
, MonoCachedClassInfo
*info
, guint8
*buf
, guint8
**endbuf
)
2482 info
->vtable_size
= decode_value (buf
, &buf
);
2483 if (info
->vtable_size
== -1)
2486 flags
= decode_value (buf
, &buf
);
2487 info
->ghcimpl
= (flags
>> 0) & 0x1;
2488 info
->has_finalize
= (flags
>> 1) & 0x1;
2489 info
->has_cctor
= (flags
>> 2) & 0x1;
2490 info
->has_nested_classes
= (flags
>> 3) & 0x1;
2491 info
->blittable
= (flags
>> 4) & 0x1;
2492 info
->has_references
= (flags
>> 5) & 0x1;
2493 info
->has_static_refs
= (flags
>> 6) & 0x1;
2494 info
->no_special_static_fields
= (flags
>> 7) & 0x1;
2495 info
->is_generic_container
= (flags
>> 8) & 0x1;
2496 info
->has_weak_fields
= (flags
>> 9) & 0x1;
2498 if (info
->has_cctor
) {
2499 res
= decode_method_ref (module
, &ref
, buf
, &buf
, error
);
2500 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
2503 info
->cctor_token
= ref
.token
;
2505 if (info
->has_finalize
) {
2506 res
= decode_method_ref (module
, &ref
, buf
, &buf
, error
);
2507 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
2510 info
->finalize_image
= ref
.image
;
2511 info
->finalize_token
= ref
.token
;
2514 info
->instance_size
= decode_value (buf
, &buf
);
2515 info
->class_size
= decode_value (buf
, &buf
);
2516 info
->packing_size
= decode_value (buf
, &buf
);
2517 info
->min_align
= decode_value (buf
, &buf
);
2525 mono_aot_get_method_from_vt_slot (MonoDomain
*domain
, MonoVTable
*vtable
, int slot
, MonoError
*error
)
2528 MonoClass
*klass
= vtable
->klass
;
2529 MonoAotModule
*amodule
= m_class_get_image (klass
)->aot_module
;
2531 MonoCachedClassInfo class_info
;
2536 ERROR_DECL_VALUE (inner_error
);
2540 if (MONO_CLASS_IS_INTERFACE (klass
) || m_class_get_rank (klass
) || !amodule
)
2543 info
= &amodule
->blob
[mono_aot_get_offset (amodule
->class_info_offsets
, mono_metadata_token_index (m_class_get_type_token (klass
)) - 1)];
2546 err
= decode_cached_class_info (amodule
, &class_info
, p
, &p
);
2550 for (i
= 0; i
< slot
; ++i
) {
2551 decode_method_ref (amodule
, &ref
, p
, &p
, &inner_error
);
2552 mono_error_cleanup (&inner_error
); /* FIXME don't swallow the error */
2555 res
= decode_method_ref (amodule
, &ref
, p
, &p
, &inner_error
);
2556 mono_error_cleanup (&inner_error
); /* FIXME don't swallow the error */
2559 if (ref
.no_aot_trampoline
)
2562 if (mono_metadata_token_index (ref
.token
) == 0 || mono_metadata_token_table (ref
.token
) != MONO_TABLE_METHOD
)
2565 addr
= mono_aot_get_method_from_token (domain
, ref
.image
, ref
.token
, error
);
2570 mono_aot_get_cached_class_info (MonoClass
*klass
, MonoCachedClassInfo
*res
)
2572 MonoAotModule
*amodule
= m_class_get_image (klass
)->aot_module
;
2576 if (m_class_get_rank (klass
) || !m_class_get_type_token (klass
) || !amodule
)
2579 p
= (guint8
*)&amodule
->blob
[mono_aot_get_offset (amodule
->class_info_offsets
, mono_metadata_token_index (m_class_get_type_token (klass
)) - 1)];
2581 err
= decode_cached_class_info (amodule
, res
, p
, &p
);
2589 * mono_aot_get_class_from_name:
2591 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
2592 * using a cache stored in the AOT file.
2593 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
2595 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
2599 mono_aot_get_class_from_name (MonoImage
*image
, const char *name_space
, const char *name
, MonoClass
**klass
)
2601 MonoAotModule
*amodule
= image
->aot_module
;
2602 guint16
*table
, *entry
;
2605 char full_name_buf
[1024];
2607 const char *name2
, *name_space2
;
2609 guint32 cols
[MONO_TYPEDEF_SIZE
];
2610 GHashTable
*nspace_table
;
2612 if (!amodule
|| !amodule
->class_name_table
)
2615 amodule_lock (amodule
);
2619 /* First look in the cache */
2620 if (!amodule
->name_cache
)
2621 amodule
->name_cache
= g_hash_table_new (g_str_hash
, g_str_equal
);
2622 nspace_table
= (GHashTable
*)g_hash_table_lookup (amodule
->name_cache
, name_space
);
2624 *klass
= (MonoClass
*)g_hash_table_lookup (nspace_table
, name
);
2626 amodule_unlock (amodule
);
2631 table_size
= amodule
->class_name_table
[0];
2632 table
= amodule
->class_name_table
+ 1;
2634 if (name_space
[0] == '\0')
2635 full_name
= g_strdup_printf ("%s", name
);
2637 if (strlen (name_space
) + strlen (name
) < 1000) {
2638 sprintf (full_name_buf
, "%s.%s", name_space
, name
);
2639 full_name
= full_name_buf
;
2641 full_name
= g_strdup_printf ("%s.%s", name_space
, name
);
2644 hash
= mono_metadata_str_hash (full_name
) % table_size
;
2645 if (full_name
!= full_name_buf
)
2648 entry
= &table
[hash
* 2];
2650 if (entry
[0] != 0) {
2651 t
= &image
->tables
[MONO_TABLE_TYPEDEF
];
2654 guint32 index
= entry
[0];
2655 guint32 next
= entry
[1];
2656 guint32 token
= mono_metadata_make_token (MONO_TABLE_TYPEDEF
, index
);
2658 name_table_accesses
++;
2660 mono_metadata_decode_row (t
, index
- 1, cols
, MONO_TYPEDEF_SIZE
);
2662 name2
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
2663 name_space2
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
2665 if (!strcmp (name
, name2
) && !strcmp (name_space
, name_space2
)) {
2667 amodule_unlock (amodule
);
2668 *klass
= mono_class_get_checked (image
, token
, error
);
2669 if (!mono_error_ok (error
))
2670 mono_error_cleanup (error
); /* FIXME don't swallow the error */
2674 amodule_lock (amodule
);
2675 nspace_table
= (GHashTable
*)g_hash_table_lookup (amodule
->name_cache
, name_space
);
2676 if (!nspace_table
) {
2677 nspace_table
= g_hash_table_new (g_str_hash
, g_str_equal
);
2678 g_hash_table_insert (amodule
->name_cache
, (char*)name_space2
, nspace_table
);
2680 g_hash_table_insert (nspace_table
, (char*)name2
, *klass
);
2681 amodule_unlock (amodule
);
2687 entry
= &table
[next
* 2];
2694 amodule_unlock (amodule
);
2700 mono_aot_get_weak_field_indexes (MonoImage
*image
)
2702 MonoAotModule
*amodule
= image
->aot_module
;
2707 /* Initialize weak field indexes from the cached copy */
2708 guint32
*indexes
= (guint32
*)amodule
->weak_field_indexes
;
2709 int len
= indexes
[0];
2710 GHashTable
*indexes_hash
= g_hash_table_new (NULL
, NULL
);
2711 for (int i
= 0; i
< len
; ++i
)
2712 g_hash_table_insert (indexes_hash
, GUINT_TO_POINTER (indexes
[i
+ 1]), GUINT_TO_POINTER (1));
2713 return indexes_hash
;
2716 /* Compute the boundaries of the LLVM code for AMODULE. */
2718 compute_llvm_code_range (MonoAotModule
*amodule
, guint8
**code_start
, guint8
**code_end
)
2721 int version
, fde_count
;
2724 if (amodule
->info
.llvm_get_method
) {
2725 gpointer (*get_method
) (int) = (gpointer (*)(int))amodule
->info
.llvm_get_method
;
2728 gsize min
= 1 << 30, max
= 0;
2731 // FIXME: This depends on emscripten allocating ftnptr ids sequentially
2732 for (int i
= 0; i
< amodule
->info
.nmethods
; ++i
) {
2735 addr
= get_method (i
);
2736 gsize val
= (gsize
)addr
;
2738 g_assert (val
> prev
);
2747 *code_start
= (guint8
*)min
;
2748 *code_end
= (guint8
*)(max
+ 1);
2754 *code_start
= (guint8
*)get_method (-1);
2755 *code_end
= (guint8
*)get_method (-2);
2757 g_assert (*code_end
> *code_start
);
2762 g_assert (amodule
->mono_eh_frame
);
2764 p
= amodule
->mono_eh_frame
;
2766 /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
2770 g_assert (version
== 3);
2773 p
= (guint8
*)ALIGN_PTR_TO (p
, 4);
2775 fde_count
= *(guint32
*)p
;
2779 if (fde_count
> 0) {
2780 *code_start
= (guint8
*)amodule
->methods
[table
[0]];
2781 *code_end
= (guint8
*)amodule
->methods
[table
[(fde_count
- 1) * 2]] + table
[fde_count
* 2];
2789 is_llvm_code (MonoAotModule
*amodule
, guint8
*code
)
2794 if ((guint8
*)code
>= amodule
->llvm_code_start
&& (guint8
*)code
< amodule
->llvm_code_end
)
2802 is_thumb_code (MonoAotModule
*amodule
, guint8
*code
)
2804 if (is_llvm_code (amodule
, code
) && (amodule
->info
.flags
& MONO_AOT_FILE_FLAG_LLVM_THUMB
))
2811 * decode_llvm_mono_eh_frame:
2813 * Decode the EH information emitted by our modified LLVM compiler and construct a
2814 * MonoJitInfo structure from it.
2815 * If JINFO is NULL, set OUT_LLVM_CLAUSES to the number of llvm level clauses.
2816 * This function is async safe when called in async context.
2819 decode_llvm_mono_eh_frame (MonoAotModule
*amodule
, MonoDomain
*domain
, MonoJitInfo
*jinfo
,
2820 guint8
*code
, guint32 code_len
,
2821 MonoJitExceptionInfo
*clauses
, int num_clauses
,
2823 int *this_reg
, int *this_offset
, int *out_llvm_clauses
)
2825 guint8
*p
, *code1
, *code2
;
2826 guint8
*fde
, *cie
, *code_start
, *code_end
;
2827 int version
, fde_count
;
2829 int i
, pos
, left
, right
;
2830 MonoJitExceptionInfo
*ei
;
2831 guint32 fde_len
, ei_len
, nested_len
, nindex
;
2832 gpointer
*type_info
;
2833 MonoLLVMFDEInfo info
;
2837 async
= mono_thread_info_is_async_context ();
2839 if (!amodule
->mono_eh_frame
) {
2841 *out_llvm_clauses
= num_clauses
;
2844 memcpy (jinfo
->clauses
, clauses
, num_clauses
* sizeof (MonoJitExceptionInfo
));
2848 g_assert (amodule
->mono_eh_frame
&& code
);
2850 p
= amodule
->mono_eh_frame
;
2852 /* p points to data emitted by LLVM in DwarfMonoException::EmitMonoEHFrame () */
2856 g_assert (version
== 3);
2858 /* func_encoding = *p; */
2860 p
= (guint8
*)ALIGN_PTR_TO (p
, 4);
2862 fde_count
= *(guint32
*)p
;
2866 /* There is +1 entry in the table */
2867 cie
= p
+ ((fde_count
+ 1) * 8);
2869 /* Binary search in the table to find the entry for code */
2873 pos
= (left
+ right
) / 2;
2875 /* The table contains method index/fde offset pairs */
2876 g_assert (table
[(pos
* 2)] != -1);
2877 code1
= (guint8
*)amodule
->methods
[table
[(pos
* 2)]];
2878 if (pos
+ 1 == fde_count
) {
2879 code2
= amodule
->llvm_code_end
;
2881 g_assert (table
[(pos
+ 1) * 2] != -1);
2882 code2
= (guint8
*)amodule
->methods
[table
[(pos
+ 1) * 2]];
2887 else if (code
>= code2
)
2893 code_start
= (guint8
*)amodule
->methods
[table
[(pos
* 2)]];
2894 if (pos
+ 1 == fde_count
) {
2895 /* The +1 entry in the table contains the length of the last method */
2896 int len
= table
[(pos
+ 1) * 2];
2897 code_end
= code_start
+ len
;
2899 code_end
= (guint8
*)amodule
->methods
[table
[(pos
+ 1) * 2]];
2902 code_len
= code_end
- code_start
;
2904 g_assert (code
>= code_start
&& code
< code_end
);
2906 if (is_thumb_code (amodule
, code_start
))
2907 /* Clear thumb flag */
2908 code_start
= (guint8
*)(((gsize
)code_start
) & ~1);
2910 fde
= amodule
->mono_eh_frame
+ table
[(pos
* 2) + 1];
2911 /* This won't overflow because there is +1 entry in the table */
2912 fde_len
= table
[(pos
* 2) + 2 + 1] - table
[(pos
* 2) + 1];
2914 /* Compute lengths */
2915 mono_unwind_decode_llvm_mono_fde (fde
, fde_len
, cie
, code_start
, &info
, NULL
, NULL
, NULL
);
2918 /* These are leaked, but the leak is bounded */
2919 ei
= mono_domain_alloc0_lock_free (domain
, info
.ex_info_len
* sizeof (MonoJitExceptionInfo
));
2920 type_info
= mono_domain_alloc0_lock_free (domain
, info
.ex_info_len
* sizeof (gpointer
));
2921 unw_info
= mono_domain_alloc0_lock_free (domain
, info
.unw_info_len
);
2923 ei
= (MonoJitExceptionInfo
*)g_malloc0 (info
.ex_info_len
* sizeof (MonoJitExceptionInfo
));
2924 type_info
= (gpointer
*)g_malloc0 (info
.ex_info_len
* sizeof (gpointer
));
2925 unw_info
= (guint8
*)g_malloc0 (info
.unw_info_len
);
2927 mono_unwind_decode_llvm_mono_fde (fde
, fde_len
, cie
, code_start
, &info
, ei
, type_info
, unw_info
);
2929 ei_len
= info
.ex_info_len
;
2930 *this_reg
= info
.this_reg
;
2931 *this_offset
= info
.this_offset
;
2934 * LLVM might represent one IL region with multiple regions.
2937 /* Count number of nested clauses */
2939 for (i
= 0; i
< ei_len
; ++i
) {
2940 /* This might be unaligned */
2941 gint32 cindex1
= read32 (type_info
[i
]);
2944 for (l
= nesting
[cindex1
]; l
; l
= l
->next
)
2949 *out_llvm_clauses
= ei_len
+ nested_len
;
2953 /* Store the unwind info addr/length in the MonoJitInfo structure itself so its async safe */
2954 MonoUnwindJitInfo
*jinfo_unwind
= mono_jit_info_get_unwind_info (jinfo
);
2955 g_assert (jinfo_unwind
);
2956 jinfo_unwind
->unw_info
= unw_info
;
2957 jinfo_unwind
->unw_info_len
= info
.unw_info_len
;
2959 for (i
= 0; i
< ei_len
; ++i
) {
2961 * clauses contains the original IL exception info saved by the AOT
2962 * compiler, we have to combine that with the information produced by LLVM
2964 /* The type_info entries contain IL clause indexes */
2965 int clause_index
= read32 (type_info
[i
]);
2966 MonoJitExceptionInfo
*jei
= &jinfo
->clauses
[i
];
2967 MonoJitExceptionInfo
*orig_jei
= &clauses
[clause_index
];
2969 g_assert (clause_index
< num_clauses
);
2970 jei
->flags
= orig_jei
->flags
;
2971 jei
->data
.catch_class
= orig_jei
->data
.catch_class
;
2973 jei
->try_start
= ei
[i
].try_start
;
2974 jei
->try_end
= ei
[i
].try_end
;
2975 jei
->handler_start
= ei
[i
].handler_start
;
2976 jei
->clause_index
= clause_index
;
2978 if (is_thumb_code (amodule
, (guint8
*)jei
->try_start
)) {
2979 jei
->try_start
= (void*)((gsize
)jei
->try_start
& ~1);
2980 jei
->try_end
= (void*)((gsize
)jei
->try_end
& ~1);
2981 /* Make sure we transition to thumb when a handler starts */
2982 jei
->handler_start
= (void*)((gsize
)jei
->handler_start
+ 1);
2986 /* See exception_cb () in mini-llvm.c as to why this is needed */
2988 for (i
= 0; i
< ei_len
; ++i
) {
2989 gint32 cindex1
= read32 (type_info
[i
]);
2992 for (l
= nesting
[cindex1
]; l
; l
= l
->next
) {
2993 gint32 nesting_cindex
= GPOINTER_TO_INT (l
->data
);
2994 MonoJitExceptionInfo
*nesting_ei
;
2995 MonoJitExceptionInfo
*nesting_clause
= &clauses
[nesting_cindex
];
2997 nesting_ei
= &jinfo
->clauses
[nindex
];
3000 memcpy (nesting_ei
, &jinfo
->clauses
[i
], sizeof (MonoJitExceptionInfo
));
3001 nesting_ei
->flags
= nesting_clause
->flags
;
3002 nesting_ei
->data
.catch_class
= nesting_clause
->data
.catch_class
;
3003 nesting_ei
->clause_index
= nesting_cindex
;
3006 g_assert (nindex
== ei_len
+ nested_len
);
3010 alloc0_jit_info_data (MonoDomain
*domain
, int size
, gboolean async_context
)
3012 #define alloc0_jit_info_data(domain, size, async_context) (g_cast (alloc0_jit_info_data ((domain), (size), (async_context))))
3017 if (async_context
) {
3018 res
= mono_domain_alloc0_lock_free (domain
, size
);
3019 mono_atomic_fetch_add_i32 (&async_jit_info_size
, size
);
3021 res
= mono_domain_alloc0 (domain
, size
);
3027 * LOCKING: Acquires the domain lock.
3028 * In async context, this is async safe.
3031 decode_exception_debug_info (MonoAotModule
*amodule
, MonoDomain
*domain
,
3032 MonoMethod
*method
, guint8
* ex_info
,
3033 guint8
*code
, guint32 code_len
)
3036 int i
, buf_len
, num_clauses
, len
;
3038 MonoJitInfoFlags flags
= JIT_INFO_NONE
;
3039 guint unwind_info
, eflags
;
3040 gboolean has_generic_jit_info
, has_dwarf_unwind_info
, has_clauses
, has_seq_points
, has_try_block_holes
, has_arch_eh_jit_info
;
3041 gboolean from_llvm
, has_gc_map
;
3043 int try_holes_info_size
, num_holes
;
3044 int this_reg
= 0, this_offset
= 0;
3047 /* Load the method info from the AOT file */
3048 async
= mono_thread_info_is_async_context ();
3051 eflags
= decode_value (p
, &p
);
3052 has_generic_jit_info
= (eflags
& 1) != 0;
3053 has_dwarf_unwind_info
= (eflags
& 2) != 0;
3054 has_clauses
= (eflags
& 4) != 0;
3055 has_seq_points
= (eflags
& 8) != 0;
3056 from_llvm
= (eflags
& 16) != 0;
3057 has_try_block_holes
= (eflags
& 32) != 0;
3058 has_gc_map
= (eflags
& 64) != 0;
3059 has_arch_eh_jit_info
= (eflags
& 128) != 0;
3061 if (has_dwarf_unwind_info
) {
3062 unwind_info
= decode_value (p
, &p
);
3063 g_assert (unwind_info
< (1 << 30));
3065 unwind_info
= decode_value (p
, &p
);
3067 if (has_generic_jit_info
)
3068 flags
|= JIT_INFO_HAS_GENERIC_JIT_INFO
;
3070 if (has_try_block_holes
) {
3071 num_holes
= decode_value (p
, &p
);
3072 flags
|= JIT_INFO_HAS_TRY_BLOCK_HOLES
;
3073 try_holes_info_size
= sizeof (MonoTryBlockHoleTableJitInfo
) + num_holes
* sizeof (MonoTryBlockHoleJitInfo
);
3075 num_holes
= try_holes_info_size
= 0;
3078 if (has_arch_eh_jit_info
) {
3079 flags
|= JIT_INFO_HAS_ARCH_EH_INFO
;
3080 /* Overwrite the original code_len which includes alignment padding */
3081 code_len
= decode_value (p
, &p
);
3084 /* Exception table */
3086 num_clauses
= decode_value (p
, &p
);
3091 MonoJitExceptionInfo
*clauses
;
3095 * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
3099 if (num_clauses
< 16) {
3100 clauses
= g_newa (MonoJitExceptionInfo
, num_clauses
);
3101 nesting
= g_newa (GSList
*, num_clauses
);
3103 clauses
= alloc0_jit_info_data (domain
, sizeof (MonoJitExceptionInfo
) * num_clauses
, TRUE
);
3104 nesting
= alloc0_jit_info_data (domain
, sizeof (GSList
*) * num_clauses
, TRUE
);
3106 memset (clauses
, 0, sizeof (MonoJitExceptionInfo
) * num_clauses
);
3107 memset (nesting
, 0, sizeof (GSList
*) * num_clauses
);
3109 clauses
= g_new0 (MonoJitExceptionInfo
, num_clauses
);
3110 nesting
= g_new0 (GSList
*, num_clauses
);
3113 for (i
= 0; i
< num_clauses
; ++i
) {
3114 MonoJitExceptionInfo
*ei
= &clauses
[i
];
3116 ei
->flags
= decode_value (p
, &p
);
3118 if (!(ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
|| ei
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)) {
3119 int len
= decode_value (p
, &p
);
3125 ei
->data
.catch_class
= decode_klass_ref (amodule
, p
, &p
, error
);
3126 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3131 ei
->clause_index
= i
;
3133 ei
->try_offset
= decode_value (p
, &p
);
3134 ei
->try_len
= decode_value (p
, &p
);
3135 ei
->handler_offset
= decode_value (p
, &p
);
3136 ei
->handler_len
= decode_value (p
, &p
);
3138 /* Read the list of nesting clauses */
3140 int nesting_index
= decode_value (p
, &p
);
3141 if (nesting_index
== -1)
3145 nesting
[i
] = g_slist_prepend (nesting
[i
], GINT_TO_POINTER (nesting_index
));
3149 flags
|= JIT_INFO_HAS_UNWIND_INFO
;
3151 int num_llvm_clauses
;
3152 /* Get the length first */
3153 decode_llvm_mono_eh_frame (amodule
, domain
, NULL
, code
, code_len
, clauses
, num_clauses
, nesting
, &this_reg
, &this_offset
, &num_llvm_clauses
);
3154 len
= mono_jit_info_size (flags
, num_llvm_clauses
, num_holes
);
3155 jinfo
= (MonoJitInfo
*)alloc0_jit_info_data (domain
, len
, async
);
3156 mono_jit_info_init (jinfo
, method
, code
, code_len
, flags
, num_llvm_clauses
, num_holes
);
3158 decode_llvm_mono_eh_frame (amodule
, domain
, jinfo
, code
, code_len
, clauses
, num_clauses
, nesting
, &this_reg
, &this_offset
, NULL
);
3162 for (i
= 0; i
< num_clauses
; ++i
)
3163 g_slist_free (nesting
[i
]);
3166 jinfo
->from_llvm
= 1;
3168 len
= mono_jit_info_size (flags
, num_clauses
, num_holes
);
3169 jinfo
= (MonoJitInfo
*)alloc0_jit_info_data (domain
, len
, async
);
3170 mono_jit_info_init (jinfo
, method
, code
, code_len
, flags
, num_clauses
, num_holes
);
3172 for (i
= 0; i
< jinfo
->num_clauses
; ++i
) {
3173 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3175 ei
->flags
= decode_value (p
, &p
);
3177 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
3178 /* Not used for catch clauses */
3179 if (ei
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
)
3180 ei
->exvar_offset
= decode_value (p
, &p
);
3182 ei
->exvar_offset
= decode_value (p
, &p
);
3185 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
|| ei
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
3186 ei
->data
.filter
= code
+ decode_value (p
, &p
);
3188 int len
= decode_value (p
, &p
);
3194 ei
->data
.catch_class
= decode_klass_ref (amodule
, p
, &p
, error
);
3195 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3200 ei
->try_start
= code
+ decode_value (p
, &p
);
3201 ei
->try_end
= code
+ decode_value (p
, &p
);
3202 ei
->handler_start
= code
+ decode_value (p
, &p
);
3205 jinfo
->unwind_info
= unwind_info
;
3206 jinfo
->domain_neutral
= 0;
3207 jinfo
->from_aot
= 1;
3210 if (has_try_block_holes
) {
3211 MonoTryBlockHoleTableJitInfo
*table
;
3213 g_assert (jinfo
->has_try_block_holes
);
3215 table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
3218 table
->num_holes
= (guint16
)num_holes
;
3219 for (i
= 0; i
< num_holes
; ++i
) {
3220 MonoTryBlockHoleJitInfo
*hole
= &table
->holes
[i
];
3221 hole
->clause
= decode_value (p
, &p
);
3222 hole
->length
= decode_value (p
, &p
);
3223 hole
->offset
= decode_value (p
, &p
);
3227 if (has_arch_eh_jit_info
) {
3228 MonoArchEHJitInfo
*eh_info
;
3230 g_assert (jinfo
->has_arch_eh_info
);
3232 eh_info
= mono_jit_info_get_arch_eh_info (jinfo
);
3233 eh_info
->stack_size
= decode_value (p
, &p
);
3234 eh_info
->epilog_size
= decode_value (p
, &p
);
3238 /* The rest is not needed in async mode */
3239 jinfo
->async
= TRUE
;
3240 jinfo
->d
.aot_info
= amodule
;
3245 if (has_generic_jit_info
) {
3246 MonoGenericJitInfo
*gi
;
3249 g_assert (jinfo
->has_generic_jit_info
);
3251 gi
= mono_jit_info_get_generic_jit_info (jinfo
);
3254 gi
->nlocs
= decode_value (p
, &p
);
3256 gi
->locations
= (MonoDwarfLocListEntry
*)alloc0_jit_info_data (domain
, gi
->nlocs
* sizeof (MonoDwarfLocListEntry
), async
);
3257 for (i
= 0; i
< gi
->nlocs
; ++i
) {
3258 MonoDwarfLocListEntry
*entry
= &gi
->locations
[i
];
3260 entry
->is_reg
= decode_value (p
, &p
);
3261 entry
->reg
= decode_value (p
, &p
);
3263 entry
->offset
= decode_value (p
, &p
);
3265 entry
->from
= decode_value (p
, &p
);
3266 entry
->to
= decode_value (p
, &p
);
3271 gi
->has_this
= this_reg
!= -1;
3272 gi
->this_reg
= this_reg
;
3273 gi
->this_offset
= this_offset
;
3275 gi
->has_this
= decode_value (p
, &p
);
3276 gi
->this_reg
= decode_value (p
, &p
);
3277 gi
->this_offset
= decode_value (p
, &p
);
3281 len
= decode_value (p
, &p
);
3285 jinfo
->d
.method
= decode_resolve_method_ref (amodule
, p
, &p
, error
);
3286 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3289 gi
->generic_sharing_context
= alloc0_jit_info_data (domain
, sizeof (MonoGenericSharingContext
), async
);
3290 if (decode_value (p
, &p
)) {
3292 MonoGenericSharingContext
*gsctx
= gi
->generic_sharing_context
;
3294 gsctx
->is_gsharedvt
= TRUE
;
3298 if (method
&& has_seq_points
) {
3299 MonoSeqPointInfo
*seq_points
;
3301 p
+= mono_seq_point_info_read (&seq_points
, p
, FALSE
);
3303 mono_domain_lock (domain
);
3304 /* This could be set already since this function can be called more than once for the same method */
3305 if (!g_hash_table_lookup (domain_jit_info (domain
)->seq_points
, method
))
3306 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, method
, seq_points
);
3308 mono_seq_point_info_free (seq_points
);
3309 mono_domain_unlock (domain
);
3312 /* Load debug info */
3313 buf_len
= decode_value (p
, &p
);
3315 mono_debug_add_aot_method (domain
, method
, code
, p
, buf_len
);
3319 int map_size
= decode_value (p
, &p
);
3320 /* The GC map requires 4 bytes of alignment */
3321 while ((guint64
)(gsize
)p
% 4)
3327 if (amodule
!= m_class_get_image (jinfo
->d
.method
->klass
)->aot_module
) {
3330 ji_to_amodule
= g_hash_table_new (NULL
, NULL
);
3331 g_hash_table_insert (ji_to_amodule
, jinfo
, amodule
);
3339 amodule_contains_code_addr (MonoAotModule
*amodule
, guint8
*code
)
3341 return (code
>= amodule
->jit_code_start
&& code
<= amodule
->jit_code_end
) ||
3342 (code
>= amodule
->llvm_code_start
&& code
<= amodule
->llvm_code_end
);
3346 * mono_aot_get_unwind_info:
3348 * Return a pointer to the DWARF unwind info belonging to JI.
3351 mono_aot_get_unwind_info (MonoJitInfo
*ji
, guint32
*unwind_info_len
)
3353 MonoAotModule
*amodule
;
3355 guint8
*code
= (guint8
*)ji
->code_start
;
3358 amodule
= ji
->d
.aot_info
;
3360 amodule
= m_class_get_image (jinfo_get_method (ji
)->klass
)->aot_module
;
3362 g_assert (ji
->from_aot
);
3364 if (!amodule_contains_code_addr (amodule
, code
)) {
3365 /* ji belongs to a different aot module than amodule */
3367 g_assert (ji_to_amodule
);
3368 amodule
= (MonoAotModule
*)g_hash_table_lookup (ji_to_amodule
, ji
);
3370 g_assert (amodule_contains_code_addr (amodule
, code
));
3374 p
= amodule
->unwind_info
+ ji
->unwind_info
;
3375 *unwind_info_len
= decode_value (p
, &p
);
3380 msort_method_addresses_internal (gpointer
*array
, int *indexes
, int lo
, int hi
, gpointer
*scratch
, int *scratch_indexes
)
3382 int mid
= (lo
+ hi
) / 2;
3389 for (i
= lo
; i
< hi
; ++i
)
3390 if (array
[i
] > array
[i
+ 1])
3393 /* Already sorted */
3397 msort_method_addresses_internal (array
, indexes
, lo
, mid
, scratch
, scratch_indexes
);
3398 msort_method_addresses_internal (array
, indexes
, mid
+ 1, hi
, scratch
, scratch_indexes
);
3400 if (array
[mid
] < array
[mid
+ 1])
3406 for (i
= lo
; i
<= hi
; i
++) {
3407 if (t_lo
<= mid
&& ((t_hi
> hi
) || array
[t_lo
] < array
[t_hi
])) {
3408 scratch
[i
] = array
[t_lo
];
3409 scratch_indexes
[i
] = indexes
[t_lo
];
3412 scratch
[i
] = array
[t_hi
];
3413 scratch_indexes
[i
] = indexes
[t_hi
];
3417 for (i
= lo
; i
<= hi
; ++i
) {
3418 array
[i
] = scratch
[i
];
3419 indexes
[i
] = scratch_indexes
[i
];
3424 msort_method_addresses (gpointer
*array
, int *indexes
, int len
)
3427 int *scratch_indexes
;
3429 scratch
= g_new (gpointer
, len
);
3430 scratch_indexes
= g_new (int, len
);
3431 msort_method_addresses_internal (array
, indexes
, 0, len
- 1, scratch
, scratch_indexes
);
3433 g_free (scratch_indexes
);
3437 * mono_aot_find_jit_info:
3439 * In async context, the resulting MonoJitInfo will not have its method field set, and it will not be added
3440 * to the jit info tables.
3441 * FIXME: Large sizes in the lock free allocator
3444 mono_aot_find_jit_info (MonoDomain
*domain
, MonoImage
*image
, gpointer addr
)
3447 int pos
, left
, right
, code_len
;
3448 int method_index
, table_len
;
3450 MonoAotModule
*amodule
= image
->aot_module
;
3451 MonoMethod
*method
= NULL
;
3453 guint8
*code
, *ex_info
, *p
;
3457 guint8
*code1
, *code2
;
3464 nmethods
= amodule
->info
.nmethods
;
3466 if (domain
!= mono_get_root_domain ())
3470 if (!amodule_contains_code_addr (amodule
, (guint8
*)addr
))
3473 async
= mono_thread_info_is_async_context ();
3475 /* Compute a sorted table mapping code to method indexes. */
3476 if (!amodule
->sorted_methods
) {
3478 gpointer
*methods
= g_new0 (gpointer
, nmethods
);
3479 int *method_indexes
= g_new0 (int, nmethods
);
3480 int methods_len
= 0;
3482 for (i
= 0; i
< nmethods
; ++i
) {
3483 /* Skip the -1 entries to speed up sorting */
3484 if (amodule
->methods
[i
] == GINT_TO_POINTER (-1))
3486 methods
[methods_len
] = amodule
->methods
[i
];
3487 method_indexes
[methods_len
] = i
;
3490 /* Use a merge sort as this is mostly sorted */
3491 msort_method_addresses (methods
, method_indexes
, methods_len
);
3492 for (i
= 0; i
< methods_len
-1; ++i
)
3493 g_assert (methods
[i
] <= methods
[i
+ 1]);
3494 amodule
->sorted_methods_len
= methods_len
;
3495 if (mono_atomic_cas_ptr ((gpointer
*)&amodule
->sorted_methods
, methods
, NULL
) != NULL
)
3496 /* Somebody got in before us */
3498 if (mono_atomic_cas_ptr ((gpointer
*)&amodule
->sorted_method_indexes
, method_indexes
, NULL
) != NULL
)
3499 /* Somebody got in before us */
3500 g_free (method_indexes
);
3503 /* Binary search in the sorted_methods table */
3504 methods
= amodule
->sorted_methods
;
3505 methods_len
= amodule
->sorted_methods_len
;
3506 code
= (guint8
*)addr
;
3508 right
= methods_len
;
3510 pos
= (left
+ right
) / 2;
3512 code1
= (guint8
*)methods
[pos
];
3513 if (pos
+ 1 == methods_len
) {
3514 if (code1
>= amodule
->jit_code_start
&& code1
< amodule
->jit_code_end
)
3515 code2
= amodule
->jit_code_end
;
3517 code2
= amodule
->llvm_code_end
;
3519 code2
= (guint8
*)methods
[pos
+ 1];
3524 else if (code
>= code2
)
3530 g_assert (addr
>= methods
[pos
]);
3531 if (pos
+ 1 < methods_len
)
3532 g_assert (addr
< methods
[pos
+ 1]);
3533 method_index
= amodule
->sorted_method_indexes
[pos
];
3535 /* In async mode, jinfo is not added to the normal jit info table, so have to cache it ourselves */
3537 JitInfoMap
*table
= amodule
->async_jit_info_table
;
3541 len
= table
[0].method_index
;
3542 for (i
= 1; i
< len
; ++i
) {
3543 if (table
[i
].method_index
== method_index
)
3544 return table
[i
].jinfo
;
3549 code
= (guint8
*)amodule
->methods
[method_index
];
3550 ex_info
= &amodule
->blob
[mono_aot_get_offset (amodule
->ex_info_offsets
, method_index
)];
3552 if (pos
== methods_len
- 1) {
3553 if (code
>= amodule
->jit_code_start
&& code
< amodule
->jit_code_end
)
3554 code_len
= amodule
->jit_code_end
- code
;
3556 code_len
= amodule
->llvm_code_end
- code
;
3558 code_len
= (guint8
*)methods
[pos
+ 1] - (guint8
*)methods
[pos
];
3561 g_assert ((guint8
*)code
<= (guint8
*)addr
&& (guint8
*)addr
< (guint8
*)code
+ code_len
);
3563 /* Might be a wrapper/extra method */
3565 if (amodule
->extra_methods
) {
3566 amodule_lock (amodule
);
3567 method
= (MonoMethod
*)g_hash_table_lookup (amodule
->extra_methods
, GUINT_TO_POINTER (method_index
));
3568 amodule_unlock (amodule
);
3574 if (method_index
>= image
->tables
[MONO_TABLE_METHOD
].rows
) {
3576 * This is hit for extra methods which are called directly, so they are
3577 * not in amodule->extra_methods.
3579 table_len
= amodule
->extra_method_info_offsets
[0];
3580 table
= amodule
->extra_method_info_offsets
+ 1;
3587 pos
= ((left
+ right
) / 2);
3589 g_assert (pos
< table_len
);
3591 if (table
[pos
* 2] < method_index
)
3593 else if (table
[pos
* 2] > method_index
)
3599 p
= amodule
->blob
+ table
[(pos
* 2) + 1];
3600 method
= decode_resolve_method_ref (amodule
, p
, &p
, error
);
3601 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3603 /* Happens when a random address is passed in which matches a not-yey called wrapper encoded using its name */
3607 token
= mono_metadata_make_token (MONO_TABLE_METHOD
, method_index
+ 1);
3608 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
3610 g_error ("AOT runtime could not load method due to %s", mono_error_get_message (error
)); /* FIXME don't swallow the error */
3617 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
3619 jinfo
= decode_exception_debug_info (amodule
, domain
, method
, ex_info
, code
, code_len
);
3621 g_assert ((guint8
*)addr
>= (guint8
*)jinfo
->code_start
);
3623 /* Add it to the normal JitInfo tables */
3625 JitInfoMap
*old_table
, *new_table
;
3629 * Use a simple inmutable table with linear search to cache async jit info entries.
3630 * This assumes that the number of entries is small.
3633 /* Copy the table, adding a new entry at the end */
3634 old_table
= amodule
->async_jit_info_table
;
3636 len
= old_table
[0].method_index
;
3639 new_table
= (JitInfoMap
*)alloc0_jit_info_data (domain
, (len
+ 1) * sizeof (JitInfoMap
), async
);
3641 memcpy (new_table
, old_table
, len
* sizeof (JitInfoMap
));
3642 new_table
[0].method_index
= len
+ 1;
3643 new_table
[len
].method_index
= method_index
;
3644 new_table
[len
].jinfo
= jinfo
;
3646 mono_memory_barrier ();
3647 if (mono_atomic_cas_ptr ((volatile gpointer
*)&amodule
->async_jit_info_table
, new_table
, old_table
) == old_table
)
3651 mono_jit_info_table_add (domain
, jinfo
);
3654 if ((guint8
*)addr
>= (guint8
*)jinfo
->code_start
+ jinfo
->code_size
)
3655 /* addr is in the padding between methods, see the adjustment of code_size in decode_exception_debug_info () */
3662 decode_patch (MonoAotModule
*aot_module
, MonoMemPool
*mp
, MonoJumpInfo
*ji
, guint8
*buf
, guint8
**endbuf
)
3671 case MONO_PATCH_INFO_METHOD
:
3672 case MONO_PATCH_INFO_METHOD_JUMP
:
3673 case MONO_PATCH_INFO_ICALL_ADDR
:
3674 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
3675 case MONO_PATCH_INFO_METHOD_RGCTX
:
3676 case MONO_PATCH_INFO_METHOD_CODE_SLOT
: {
3680 res
= decode_method_ref (aot_module
, &ref
, p
, &p
, error
);
3681 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3685 if (!ref
.method
&& !mono_aot_only
&& !ref
.no_aot_trampoline
&& (ji
->type
== MONO_PATCH_INFO_METHOD
) && (mono_metadata_token_table (ref
.token
) == MONO_TABLE_METHOD
)) {
3686 ji
->data
.target
= mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (ref
.image
, ref
.token
));
3687 ji
->type
= MONO_PATCH_INFO_ABS
;
3691 ji
->data
.method
= ref
.method
;
3694 ji
->data
.method
= mono_get_method_checked (ref
.image
, ref
.token
, NULL
, NULL
, error
);
3695 if (!ji
->data
.method
)
3696 g_error ("AOT Runtime could not load method due to %s", mono_error_get_message (error
)); /* FIXME don't swallow the error */
3698 g_assert (ji
->data
.method
);
3699 mono_class_init (ji
->data
.method
->klass
);
3703 case MONO_PATCH_INFO_INTERNAL_METHOD
:
3704 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
3705 case MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
: {
3706 guint32 len
= decode_value (p
, &p
);
3708 ji
->data
.name
= (char*)p
;
3712 case MONO_PATCH_INFO_METHODCONST
:
3714 ji
->data
.method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3715 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3716 if (!ji
->data
.method
)
3719 case MONO_PATCH_INFO_VTABLE
:
3720 case MONO_PATCH_INFO_CLASS
:
3721 case MONO_PATCH_INFO_IID
:
3722 case MONO_PATCH_INFO_ADJUSTED_IID
:
3724 ji
->data
.klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3725 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3726 if (!ji
->data
.klass
)
3729 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
3730 ji
->data
.del_tramp
= (MonoDelegateClassMethodPair
*)mono_mempool_alloc0 (mp
, sizeof (MonoDelegateClassMethodPair
));
3731 ji
->data
.del_tramp
->klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3732 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3733 if (!ji
->data
.del_tramp
->klass
)
3735 if (decode_value (p
, &p
)) {
3736 ji
->data
.del_tramp
->method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3737 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3738 if (!ji
->data
.del_tramp
->method
)
3741 ji
->data
.del_tramp
->is_virtual
= decode_value (p
, &p
) ? TRUE
: FALSE
;
3743 case MONO_PATCH_INFO_IMAGE
:
3744 ji
->data
.image
= load_image (aot_module
, decode_value (p
, &p
), error
);
3745 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3746 if (!ji
->data
.image
)
3749 case MONO_PATCH_INFO_FIELD
:
3750 case MONO_PATCH_INFO_SFLDA
:
3752 ji
->data
.field
= decode_field_info (aot_module
, p
, &p
);
3753 if (!ji
->data
.field
)
3756 case MONO_PATCH_INFO_SWITCH
:
3757 ji
->data
.table
= (MonoJumpInfoBBTable
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoBBTable
));
3758 ji
->data
.table
->table_size
= decode_value (p
, &p
);
3759 table
= (void **)mono_domain_alloc (mono_domain_get (), sizeof (gpointer
) * ji
->data
.table
->table_size
);
3760 ji
->data
.table
->table
= (MonoBasicBlock
**)table
;
3761 for (i
= 0; i
< ji
->data
.table
->table_size
; i
++)
3762 table
[i
] = (gpointer
)(gssize
)decode_value (p
, &p
);
3764 case MONO_PATCH_INFO_R4
: {
3767 ji
->data
.target
= mono_domain_alloc0 (mono_domain_get (), sizeof (float));
3768 val
= decode_value (p
, &p
);
3769 *(float*)ji
->data
.target
= *(float*)&val
;
3772 case MONO_PATCH_INFO_R8
: {
3776 ji
->data
.target
= mono_domain_alloc0 (mono_domain_get (), sizeof (double));
3778 val
[0] = decode_value (p
, &p
);
3779 val
[1] = decode_value (p
, &p
);
3780 v
= ((guint64
)val
[1] << 32) | ((guint64
)val
[0]);
3781 *(double*)ji
->data
.target
= *(double*)&v
;
3784 case MONO_PATCH_INFO_LDSTR
:
3785 image
= load_image (aot_module
, decode_value (p
, &p
), error
);
3786 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3789 ji
->data
.token
= mono_jump_info_token_new (mp
, image
, MONO_TOKEN_STRING
+ decode_value (p
, &p
));
3791 case MONO_PATCH_INFO_RVA
:
3792 case MONO_PATCH_INFO_DECLSEC
:
3793 case MONO_PATCH_INFO_LDTOKEN
:
3794 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
3796 image
= load_image (aot_module
, decode_value (p
, &p
), error
);
3797 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3800 ji
->data
.token
= mono_jump_info_token_new (mp
, image
, decode_value (p
, &p
));
3802 ji
->data
.token
->has_context
= decode_value (p
, &p
);
3803 if (ji
->data
.token
->has_context
) {
3804 gboolean res
= decode_generic_context (aot_module
, &ji
->data
.token
->context
, p
, &p
, error
);
3805 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3810 case MONO_PATCH_INFO_EXC_NAME
:
3811 ji
->data
.klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3812 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3813 if (!ji
->data
.klass
)
3815 ji
->data
.name
= m_class_get_name (ji
->data
.klass
);
3817 case MONO_PATCH_INFO_METHOD_REL
:
3818 ji
->data
.offset
= decode_value (p
, &p
);
3820 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
3821 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
:
3822 case MONO_PATCH_INFO_GC_NURSERY_START
:
3823 case MONO_PATCH_INFO_GC_NURSERY_BITS
:
3824 case MONO_PATCH_INFO_PROFILER_ALLOCATION_COUNT
:
3825 case MONO_PATCH_INFO_PROFILER_CLAUSE_COUNT
:
3827 case MONO_PATCH_INFO_CASTCLASS_CACHE
:
3828 ji
->data
.index
= decode_value (p
, &p
);
3830 case MONO_PATCH_INFO_RGCTX_FETCH
:
3831 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
3833 MonoJumpInfoRgctxEntry
*entry
;
3834 guint32 offset
, val
;
3837 offset
= decode_value (p
, &p
);
3838 val
= decode_value (p
, &p
);
3840 entry
= (MonoJumpInfoRgctxEntry
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoRgctxEntry
));
3841 p2
= aot_module
->blob
+ offset
;
3842 entry
->method
= decode_resolve_method_ref (aot_module
, p2
, &p2
, error
);
3843 entry
->in_mrgctx
= ((val
& 1) > 0) ? TRUE
: FALSE
;
3844 entry
->info_type
= (MonoRgctxInfoType
)((val
>> 1) & 0xff);
3845 entry
->data
= (MonoJumpInfo
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfo
));
3846 entry
->data
->type
= (MonoJumpInfoType
)((val
>> 9) & 0xff);
3847 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3849 res
= decode_patch (aot_module
, mp
, entry
->data
, p
, &p
);
3852 ji
->data
.rgctx_entry
= entry
;
3855 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
3856 case MONO_PATCH_INFO_AOT_MODULE
:
3857 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
:
3859 case MONO_PATCH_INFO_SIGNATURE
:
3860 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER
:
3861 ji
->data
.target
= decode_signature (aot_module
, p
, &p
);
3863 case MONO_PATCH_INFO_GSHAREDVT_CALL
: {
3864 MonoJumpInfoGSharedVtCall
*info
= (MonoJumpInfoGSharedVtCall
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoGSharedVtCall
));
3865 info
->sig
= decode_signature (aot_module
, p
, &p
);
3866 g_assert (info
->sig
);
3867 info
->method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3868 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3870 ji
->data
.target
= info
;
3873 case MONO_PATCH_INFO_GSHAREDVT_METHOD
: {
3874 MonoGSharedVtMethodInfo
*info
= (MonoGSharedVtMethodInfo
*)mono_mempool_alloc0 (mp
, sizeof (MonoGSharedVtMethodInfo
));
3877 info
->method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3878 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3880 info
->num_entries
= decode_value (p
, &p
);
3881 info
->count_entries
= info
->num_entries
;
3882 info
->entries
= (MonoRuntimeGenericContextInfoTemplate
*)mono_mempool_alloc0 (mp
, sizeof (MonoRuntimeGenericContextInfoTemplate
) * info
->num_entries
);
3883 for (i
= 0; i
< info
->num_entries
; ++i
) {
3884 MonoRuntimeGenericContextInfoTemplate
*template_
= &info
->entries
[i
];
3886 template_
->info_type
= (MonoRgctxInfoType
)decode_value (p
, &p
);
3887 switch (mini_rgctx_info_type_to_patch_info_type (template_
->info_type
)) {
3888 case MONO_PATCH_INFO_CLASS
: {
3889 MonoClass
*klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3890 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3893 template_
->data
= m_class_get_byval_arg (klass
);
3896 case MONO_PATCH_INFO_FIELD
:
3897 template_
->data
= decode_field_info (aot_module
, p
, &p
);
3898 if (!template_
->data
)
3902 g_assert_not_reached ();
3906 ji
->data
.target
= info
;
3909 case MONO_PATCH_INFO_LDSTR_LIT
: {
3910 int len
= decode_value (p
, &p
);
3913 s
= (char *)mono_mempool_alloc0 (mp
, len
+ 1);
3914 memcpy (s
, p
, len
+ 1);
3917 ji
->data
.target
= s
;
3920 case MONO_PATCH_INFO_VIRT_METHOD
: {
3921 MonoJumpInfoVirtMethod
*info
= (MonoJumpInfoVirtMethod
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoVirtMethod
));
3923 info
->klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3924 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3926 info
->method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3927 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3929 ji
->data
.target
= info
;
3932 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG
:
3934 case MONO_PATCH_INFO_GET_TLS_TRAMP
:
3935 case MONO_PATCH_INFO_SET_TLS_TRAMP
:
3936 case MONO_PATCH_INFO_AOT_JIT_INFO
:
3937 ji
->data
.index
= decode_value (p
, &p
);
3940 g_warning ("unhandled type %d", ji
->type
);
3941 g_assert_not_reached ();
3955 * Decode a list of patches identified by the got offsets in GOT_OFFSETS. Return an array of
3956 * MonoJumpInfo structures allocated from MP.
3958 static MonoJumpInfo
*
3959 decode_patches (MonoAotModule
*amodule
, MonoMemPool
*mp
, int n_patches
, gboolean llvm
, guint32
*got_offsets
)
3961 MonoJumpInfo
*patches
;
3964 guint32
*got_info_offsets
;
3969 got
= amodule
->llvm_got
;
3970 got_info_offsets
= (guint32
*)amodule
->llvm_got_info_offsets
;
3973 got_info_offsets
= (guint32
*)amodule
->got_info_offsets
;
3976 patches
= (MonoJumpInfo
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfo
) * n_patches
);
3977 for (i
= 0; i
< n_patches
; ++i
) {
3978 guint8
*p
= amodule
->blob
+ mono_aot_get_offset (got_info_offsets
, got_offsets
[i
]);
3981 ji
->type
= (MonoJumpInfoType
)decode_value (p
, &p
);
3983 /* See load_method () for SFLDA */
3984 if (got
&& got
[got_offsets
[i
]] && ji
->type
!= MONO_PATCH_INFO_SFLDA
) {
3985 /* Already loaded */
3987 res
= decode_patch (amodule
, mp
, ji
, p
, &p
);
3996 static MonoJumpInfo
*
3997 load_patch_info (MonoAotModule
*amodule
, MonoMemPool
*mp
, int n_patches
,
3998 gboolean llvm
, guint32
**got_slots
,
3999 guint8
*buf
, guint8
**endbuf
)
4001 MonoJumpInfo
*patches
;
4007 *got_slots
= (guint32
*)g_malloc (sizeof (guint32
) * n_patches
);
4008 for (pindex
= 0; pindex
< n_patches
; ++pindex
) {
4009 (*got_slots
)[pindex
] = decode_value (p
, &p
);
4012 patches
= decode_patches (amodule
, mp
, n_patches
, llvm
, *got_slots
);
4014 g_free (*got_slots
);
4024 register_jump_target_got_slot (MonoDomain
*domain
, MonoMethod
*method
, gpointer
*got_slot
)
4027 * Jump addresses cannot be patched by the trampoline code since it
4028 * does not have access to the caller's address. Instead, we collect
4029 * the addresses of the GOT slots pointing to a method, and patch
4030 * them after the method has been compiled.
4032 MonoJitDomainInfo
*info
= domain_jit_info (domain
);
4034 MonoMethod
*shared_method
= mini_method_to_shared (method
);
4035 method
= shared_method
? shared_method
: method
;
4037 mono_domain_lock (domain
);
4038 if (!info
->jump_target_got_slot_hash
)
4039 info
->jump_target_got_slot_hash
= g_hash_table_new (NULL
, NULL
);
4040 list
= (GSList
*)g_hash_table_lookup (info
->jump_target_got_slot_hash
, method
);
4041 list
= g_slist_prepend (list
, got_slot
);
4042 g_hash_table_insert (info
->jump_target_got_slot_hash
, method
, list
);
4043 mono_domain_unlock (domain
);
4049 * Load the method identified by METHOD_INDEX from the AOT image. Return a
4050 * pointer to the native code of the method, or NULL if not found.
4051 * METHOD might not be set if the caller only has the image/token info.
4054 load_method (MonoDomain
*domain
, MonoAotModule
*amodule
, MonoImage
*image
, MonoMethod
*method
, guint32 token
, int method_index
,
4057 MonoJitInfo
*jinfo
= NULL
;
4058 guint8
*code
= NULL
, *info
;
4063 init_amodule_got (amodule
);
4065 if (domain
!= mono_get_root_domain ())
4066 /* Non shared AOT code can't be used in other appdomains */
4069 if (amodule
->out_of_date
)
4072 if (amodule
->info
.llvm_get_method
) {
4074 * Obtain the method address by calling a generated function in the LLVM module.
4076 gpointer (*get_method
) (int) = (gpointer (*)(int))amodule
->info
.llvm_get_method
;
4077 code
= (guint8
*)get_method (method_index
);
4082 if (amodule
->methods
[method_index
] == GINT_TO_POINTER (-1)) {
4083 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
)) {
4087 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
4091 if (!(method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)) {
4092 full_name
= mono_method_full_name (method
, TRUE
);
4093 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "AOT: NOT FOUND: %s.", full_name
);
4099 if (method_index
< amodule
->info
.nmethods
)
4100 code
= (guint8
*)amodule
->methods
[method_index
];
4103 info
= &amodule
->blob
[mono_aot_get_offset (amodule
->method_info_offsets
, method_index
)];
4105 if (!amodule
->methods_loaded
) {
4106 amodule_lock (amodule
);
4107 if (!amodule
->methods_loaded
) {
4110 loaded
= g_new0 (guint32
, amodule
->info
.nmethods
/ 32 + 1);
4111 mono_memory_barrier ();
4112 amodule
->methods_loaded
= loaded
;
4114 amodule_unlock (amodule
);
4117 if ((amodule
->methods_loaded
[method_index
/ 32] >> (method_index
% 32)) & 0x1)
4120 if (mini_debug_options
.aot_skip_set
&& !(method
&& method
->wrapper_type
)) {
4121 gint32 methods_aot
= mono_atomic_load_i32 (&mono_jit_stats
.methods_aot
);
4122 if (methods_aot
== mini_debug_options
.aot_skip
) {
4124 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
4129 char *name
= mono_method_full_name (method
, TRUE
);
4130 g_print ("NON AOT METHOD: %s.\n", name
);
4133 g_print ("NON AOT METHOD: %p %d\n", code
, method_index
);
4135 mini_debug_options
.aot_skip_set
= FALSE
;
4140 if (mono_last_aot_method
!= -1) {
4141 gint32 methods_aot
= mono_atomic_load_i32 (&mono_jit_stats
.methods_aot
);
4142 if (methods_aot
>= mono_last_aot_method
)
4144 else if (methods_aot
== mono_last_aot_method
- 1) {
4146 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
4151 char *name
= mono_method_full_name (method
, TRUE
);
4152 g_print ("LAST AOT METHOD: %s.\n", name
);
4155 g_print ("LAST AOT METHOD: %p %d\n", code
, method_index
);
4160 if (!(is_llvm_code (amodule
, code
) && (amodule
->info
.flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
)) ||
4161 (mono_llvm_only
&& method
&& method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
)) {
4162 res
= init_method (amodule
, method_index
, method
, NULL
, NULL
, error
);
4167 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
)) {
4171 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
4176 full_name
= mono_method_full_name (method
, TRUE
);
4179 jinfo
= mono_aot_find_jit_info (domain
, amodule
->assembly
->image
, code
);
4181 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "AOT: FOUND method %s [%p - %p %p]", full_name
, code
, code
+ jinfo
->code_size
, info
);
4185 amodule_lock (amodule
);
4189 mono_atomic_inc_i32 (&mono_jit_stats
.methods_aot
);
4191 if (method
&& method
->wrapper_type
)
4192 g_hash_table_insert (amodule
->method_to_code
, method
, code
);
4194 /* Commit changes since methods_loaded is accessed outside the lock */
4195 mono_memory_barrier ();
4197 amodule
->methods_loaded
[method_index
/ 32] |= 1 << (method_index
% 32);
4199 amodule_unlock (amodule
);
4201 if (MONO_PROFILER_ENABLED (jit_begin
) || MONO_PROFILER_ENABLED (jit_done
)) {
4205 method
= mono_get_method_checked (amodule
->assembly
->image
, token
, NULL
, NULL
, error
);
4209 MONO_PROFILER_RAISE (jit_begin
, (method
));
4210 jinfo
= mono_jit_info_table_find (domain
, code
);
4212 MONO_PROFILER_RAISE (jit_done
, (method
, jinfo
));
4224 /** find_aot_method_in_amodule
4226 * \param code_amodule The AOT module containing the code pointer
4227 * \param method The method to find the code index for
4228 * \param hash_full The hash for the method
4231 find_aot_method_in_amodule (MonoAotModule
*code_amodule
, MonoMethod
*method
, guint32 hash_full
)
4234 guint32 table_size
, entry_size
, hash
;
4235 guint32
*table
, *entry
;
4237 static guint32 n_extra_decodes
;
4239 // The AOT module containing the MonoMethod
4240 // The reference to the metadata amodule will differ among multiple dedup methods
4241 // which mangle to the same name but live in different assemblies. This leads to
4242 // the caching breaking. The solution seems to be to cache using the "metadata" amodule.
4243 MonoAotModule
*metadata_amodule
= m_class_get_image (method
->klass
)->aot_module
;
4245 if (!metadata_amodule
|| metadata_amodule
->out_of_date
|| !code_amodule
|| code_amodule
->out_of_date
)
4248 table_size
= code_amodule
->extra_method_table
[0];
4249 hash
= hash_full
% table_size
;
4250 table
= code_amodule
->extra_method_table
+ 1;
4253 entry
= &table
[hash
* entry_size
];
4260 guint32 key
= entry
[0];
4261 guint32 value
= entry
[1];
4262 guint32 next
= entry
[entry_size
- 1];
4266 p
= code_amodule
->blob
+ key
;
4269 amodule_lock (metadata_amodule
);
4270 if (!metadata_amodule
->method_ref_to_method
)
4271 metadata_amodule
->method_ref_to_method
= g_hash_table_new (NULL
, NULL
);
4272 m
= (MonoMethod
*)g_hash_table_lookup (metadata_amodule
->method_ref_to_method
, p
);
4273 amodule_unlock (metadata_amodule
);
4275 m
= decode_resolve_method_ref_with_target (code_amodule
, method
, p
, &p
, error
);
4276 mono_error_cleanup (error
); /* FIXME don't swallow the error */
4278 * Can't catche runtime invoke wrappers since it would break
4279 * the check in decode_method_ref_with_target ().
4281 if (m
&& m
->wrapper_type
!= MONO_WRAPPER_RUNTIME_INVOKE
) {
4282 amodule_lock (metadata_amodule
);
4283 g_hash_table_insert (metadata_amodule
->method_ref_to_method
, orig_p
, m
);
4284 amodule_unlock (metadata_amodule
);
4292 /* Methods decoded needlessly */
4294 //printf ("%d %s %s %p\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE), orig_p);
4299 entry
= &table
[next
* entry_size
];
4304 if (index
!= 0xffffff)
4305 g_assert (index
< code_amodule
->info
.nmethods
);
4311 add_module_cb (gpointer key
, gpointer value
, gpointer user_data
)
4313 g_ptr_array_add ((GPtrArray
*)user_data
, value
);
4317 mono_aot_can_dedup (MonoMethod
*method
)
4319 gboolean not_normal_gshared
= method
->is_inflated
&& !mono_method_is_generic_sharable_full (method
, TRUE
, FALSE
, FALSE
);
4320 gboolean extra_method
= (method
->wrapper_type
!= MONO_WRAPPER_NONE
) || not_normal_gshared
;
4322 return extra_method
;
4329 * Try finding METHOD in the extra_method table in all AOT images.
4330 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
4331 * module where the method was found.
4334 find_aot_method (MonoMethod
*method
, MonoAotModule
**out_amodule
)
4339 guint32 hash
= mono_aot_method_hash (method
);
4341 /* Try the place we expect to have moved the method only
4342 * We don't probe, as that causes hard-to-debug issues when we fail
4343 * to find the method */
4344 if (container_amodule
&& mono_aot_can_dedup (method
)) {
4345 *out_amodule
= container_amodule
;
4346 index
= find_aot_method_in_amodule (container_amodule
, method
, hash
);
4350 /* Try the method's module first */
4351 *out_amodule
= m_class_get_image (method
->klass
)->aot_module
;
4352 index
= find_aot_method_in_amodule (m_class_get_image (method
->klass
)->aot_module
, method
, hash
);
4353 if (index
!= 0xffffff)
4357 * Try all other modules.
4358 * This is needed because generic instances klass->image points to the image
4359 * containing the generic definition, but the native code is generated to the
4360 * AOT image which contains the reference.
4363 /* Make a copy to avoid doing the search inside the aot lock */
4364 modules
= g_ptr_array_new ();
4366 g_hash_table_foreach (aot_modules
, add_module_cb
, modules
);
4370 for (i
= 0; i
< modules
->len
; ++i
) {
4371 MonoAotModule
*amodule
= (MonoAotModule
*)g_ptr_array_index (modules
, i
);
4373 if (amodule
!= m_class_get_image (method
->klass
)->aot_module
)
4374 index
= find_aot_method_in_amodule (amodule
, method
, hash
);
4375 if (index
!= 0xffffff) {
4376 *out_amodule
= amodule
;
4381 g_ptr_array_free (modules
, TRUE
);
4387 mono_aot_find_method_index (MonoMethod
*method
)
4389 MonoAotModule
*out_amodule
;
4390 return find_aot_method (method
, &out_amodule
);
4394 init_method (MonoAotModule
*amodule
, guint32 method_index
, MonoMethod
*method
, MonoClass
*init_class
, MonoGenericContext
*context
, MonoError
*error
)
4396 MonoDomain
*domain
= mono_domain_get ();
4398 MonoClass
*klass_to_run_ctor
= NULL
;
4399 gboolean from_plt
= method
== NULL
;
4400 int pindex
, n_patches
;
4402 MonoJitInfo
*jinfo
= NULL
;
4403 guint8
*code
, *info
;
4407 code
= (guint8
*)amodule
->methods
[method_index
];
4408 info
= &amodule
->blob
[mono_aot_get_offset (amodule
->method_info_offsets
, method_index
)];
4412 //does the method's class has a cctor?
4413 if (decode_value (p
, &p
) == 1)
4414 klass_to_run_ctor
= decode_klass_ref (amodule
, p
, &p
, error
);
4418 //FIXME old code would use the class from @method if not null and ignore the one encoded. I don't know if we need to honor that -- @kumpera
4420 klass_to_run_ctor
= method
->klass
;
4422 n_patches
= decode_value (p
, &p
);
4425 MonoJumpInfo
*patches
;
4430 mp
= mono_mempool_new ();
4432 if ((gpointer
)code
>= amodule
->info
.jit_code_start
&& (gpointer
)code
<= amodule
->info
.jit_code_end
) {
4437 got
= amodule
->llvm_got
;
4441 patches
= load_patch_info (amodule
, mp
, n_patches
, llvm
, &got_slots
, p
, &p
);
4442 if (patches
== NULL
) {
4443 mono_mempool_destroy (mp
);
4447 for (pindex
= 0; pindex
< n_patches
; ++pindex
) {
4448 MonoJumpInfo
*ji
= &patches
[pindex
];
4452 * For SFLDA, we need to call resolve_patch_target () since the GOT slot could have
4453 * been initialized by load_method () for a static cctor before the cctor has
4454 * finished executing (#23242).
4456 if (!got
[got_slots
[pindex
]] || ji
->type
== MONO_PATCH_INFO_SFLDA
) {
4457 /* In llvm-only made, we might encounter shared methods */
4458 if (mono_llvm_only
&& ji
->type
== MONO_PATCH_INFO_METHOD
&& mono_method_check_context_used (ji
->data
.method
)) {
4460 ji
->data
.method
= mono_class_inflate_generic_method_checked (ji
->data
.method
, context
, error
);
4461 if (!mono_error_ok (error
)) {
4463 mono_mempool_destroy (mp
);
4467 /* This cannot be resolved in mono_resolve_patch_target () */
4468 if (ji
->type
== MONO_PATCH_INFO_AOT_JIT_INFO
) {
4469 // FIXME: Lookup using the index
4470 jinfo
= mono_aot_find_jit_info (domain
, amodule
->assembly
->image
, code
);
4471 ji
->type
= MONO_PATCH_INFO_ABS
;
4472 ji
->data
.target
= jinfo
;
4474 addr
= mono_resolve_patch_target (method
, domain
, code
, ji
, TRUE
, error
);
4475 if (!mono_error_ok (error
)) {
4477 mono_mempool_destroy (mp
);
4480 if (ji
->type
== MONO_PATCH_INFO_METHOD_JUMP
)
4481 addr
= mono_create_ftnptr (domain
, addr
);
4482 mono_memory_barrier ();
4483 got
[got_slots
[pindex
]] = addr
;
4484 if (ji
->type
== MONO_PATCH_INFO_METHOD_JUMP
)
4485 register_jump_target_got_slot (domain
, ji
->data
.method
, &(got
[got_slots
[pindex
]]));
4487 ji
->type
= MONO_PATCH_INFO_NONE
;
4492 mono_mempool_destroy (mp
);
4495 if (mini_get_debug_options ()->load_aot_jit_info_eagerly
)
4496 jinfo
= mono_aot_find_jit_info (domain
, amodule
->assembly
->image
, code
);
4501 MonoVTable
*vt
= mono_class_vtable_checked (domain
, init_class
, error
);
4505 inited_ok
= mono_runtime_class_init_full (vt
, error
);
4506 } else if (from_plt
&& klass_to_run_ctor
&& !mono_class_is_gtd (klass_to_run_ctor
)) {
4507 MonoVTable
*vt
= mono_class_vtable_checked (domain
, klass_to_run_ctor
, error
);
4511 inited_ok
= mono_runtime_class_init_full (vt
, error
);
4526 init_llvmonly_method (MonoAotModule
*amodule
, guint32 method_index
, MonoMethod
*method
, MonoClass
*init_class
, MonoGenericContext
*context
)
4530 init_method (amodule
, method_index
, method
, init_class
, context
, error
);
4531 if (!is_ok (error
)) {
4532 MonoException
*ex
= mono_error_convert_to_exception (error
);
4533 /* Its okay to raise in llvmonly mode */
4535 mono_llvm_throw_exception ((MonoObject
*)ex
);
4540 mono_aot_init_llvm_method (gpointer aot_module
, guint32 method_index
)
4542 MonoAotModule
*amodule
= (MonoAotModule
*)aot_module
;
4544 init_llvmonly_method (amodule
, method_index
, NULL
, NULL
, NULL
);
4548 mono_aot_init_gshared_method_this (gpointer aot_module
, guint32 method_index
, MonoObject
*this_obj
)
4550 MonoAotModule
*amodule
= (MonoAotModule
*)aot_module
;
4552 MonoGenericContext
*context
;
4556 g_assert (this_obj
);
4557 klass
= this_obj
->vtable
->klass
;
4559 amodule_lock (amodule
);
4560 method
= (MonoMethod
*)g_hash_table_lookup (amodule
->extra_methods
, GUINT_TO_POINTER (method_index
));
4561 amodule_unlock (amodule
);
4564 context
= mono_method_get_context (method
);
4567 init_llvmonly_method (amodule
, method_index
, NULL
, klass
, context
);
4571 mono_aot_init_gshared_method_mrgctx (gpointer aot_module
, guint32 method_index
, MonoMethodRuntimeGenericContext
*rgctx
)
4573 MonoAotModule
*amodule
= (MonoAotModule
*)aot_module
;
4574 MonoGenericContext context
= { NULL
, NULL
};
4575 MonoClass
*klass
= rgctx
->class_vtable
->klass
;
4577 if (mono_class_is_ginst (klass
))
4578 context
.class_inst
= mono_class_get_generic_class (klass
)->context
.class_inst
;
4579 else if (mono_class_is_gtd (klass
))
4580 context
.class_inst
= mono_class_get_generic_container (klass
)->context
.class_inst
;
4581 context
.method_inst
= rgctx
->method_inst
;
4583 init_llvmonly_method (amodule
, method_index
, NULL
, rgctx
->class_vtable
->klass
, &context
);
4587 mono_aot_init_gshared_method_vtable (gpointer aot_module
, guint32 method_index
, MonoVTable
*vtable
)
4589 MonoAotModule
*amodule
= (MonoAotModule
*)aot_module
;
4591 MonoGenericContext
*context
;
4594 klass
= vtable
->klass
;
4596 amodule_lock (amodule
);
4597 method
= (MonoMethod
*)g_hash_table_lookup (amodule
->extra_methods
, GUINT_TO_POINTER (method_index
));
4598 amodule_unlock (amodule
);
4601 context
= mono_method_get_context (method
);
4604 init_llvmonly_method (amodule
, method_index
, NULL
, klass
, context
);
4608 * mono_aot_get_method:
4610 * Return a pointer to the AOTed native code for METHOD if it can be found,
4612 * On platforms with function pointers, this doesn't return a function pointer.
4615 mono_aot_get_method (MonoDomain
*domain
, MonoMethod
*method
, MonoError
*error
)
4617 MonoClass
*klass
= method
->klass
;
4618 MonoMethod
*orig_method
= method
;
4619 guint32 method_index
;
4620 MonoAotModule
*amodule
= m_class_get_image (klass
)->aot_module
;
4622 gboolean cache_result
= FALSE
;
4623 ERROR_DECL_VALUE (inner_error
);
4627 if (domain
!= mono_get_root_domain ())
4628 /* Non shared AOT code can't be used in other appdomains */
4631 if (enable_aot_cache
&& !amodule
&& domain
->entry_assembly
&& m_class_get_image (klass
) == mono_defaults
.corlib
) {
4632 /* This cannot be AOTed during startup, so do it now */
4633 if (!mscorlib_aot_loaded
) {
4634 mscorlib_aot_loaded
= TRUE
;
4635 load_aot_module (m_class_get_image (klass
)->assembly
, NULL
);
4636 amodule
= m_class_get_image (klass
)->aot_module
;
4643 if (amodule
->out_of_date
)
4646 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
4647 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
4648 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4649 (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
))
4653 * Use the original method instead of its invoke-with-check wrapper.
4654 * This is not a problem when using full-aot, since it doesn't support
4657 if (mono_aot_only
&& method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
4658 return mono_aot_get_method (domain
, mono_marshal_method_from_wrapper (method
), error
);
4660 g_assert (m_class_is_inited (klass
));
4662 /* Find method index */
4663 method_index
= 0xffffff;
4665 gboolean dedupable
= mono_aot_can_dedup (method
);
4667 if (method
->is_inflated
&& !method
->wrapper_type
&& mono_method_is_generic_sharable_full (method
, TRUE
, FALSE
, FALSE
) && !dedupable
) {
4668 MonoMethod
*orig_method
= method
;
4670 * For generic methods, we store the fully shared instance in place of the
4673 method
= mono_method_get_declaring_generic_method (method
);
4674 method_index
= mono_metadata_token_index (method
->token
) - 1;
4676 if (mono_llvm_only
) {
4677 /* Needed by mono_aot_init_gshared_method_this () */
4678 /* orig_method is a random instance but it is enough to make init_method () work */
4679 amodule_lock (amodule
);
4680 g_hash_table_insert (amodule
->extra_methods
, GUINT_TO_POINTER (method_index
), orig_method
);
4681 amodule_unlock (amodule
);
4685 if (method_index
== 0xffffff && (method
->is_inflated
|| !method
->token
)) {
4686 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
4687 amodule_lock (amodule
);
4688 code
= (guint8
*)g_hash_table_lookup (amodule
->method_to_code
, method
);
4689 amodule_unlock (amodule
);
4693 cache_result
= TRUE
;
4694 if (method_index
== 0xffffff)
4695 method_index
= find_aot_method (method
, &amodule
);
4698 * Special case the ICollection<T> wrappers for arrays, as they cannot
4699 * be statically enumerated, and each wrapper ends up calling the same
4702 if (method_index
== 0xffffff && method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_MANAGED
&& m_class_get_rank (method
->klass
) && strstr (method
->name
, "System.Collections.Generic")) {
4703 MonoMethod
*m
= mono_aot_get_array_helper_from_wrapper (method
);
4705 code
= (guint8
*)mono_aot_get_method (domain
, m
, &inner_error
);
4706 mono_error_cleanup (&inner_error
);
4712 * Special case Array.GetGenericValueImpl which is a generic icall.
4713 * Generic sharing currently can't handle it, but the icall returns data using
4714 * an out parameter, so the managed-to-native wrappers can share the same code.
4716 if (method_index
== 0xffffff && method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
&& method
->klass
== mono_defaults
.array_class
&& !strcmp (method
->name
, "GetGenericValueImpl")) {
4718 MonoGenericContext ctx
;
4719 MonoType
*args
[16];
4721 if (mono_method_signature (method
)->params
[1]->type
== MONO_TYPE_OBJECT
)
4722 /* Avoid recursion */
4725 m
= mono_class_get_method_from_name_checked (mono_defaults
.array_class
, "GetGenericValueImpl", 2, 0, error
);
4726 mono_error_assert_ok (error
);
4729 memset (&ctx
, 0, sizeof (ctx
));
4730 args
[0] = m_class_get_byval_arg (mono_defaults
.object_class
);
4731 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
4733 m
= mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m
, &ctx
, error
), TRUE
, TRUE
);
4735 g_error ("AOT runtime could not load method due to %s", mono_error_get_message (error
)); /* FIXME don't swallow the error */
4738 * Get the code for the <object> instantiation which should be emitted into
4739 * the mscorlib aot image by the AOT compiler.
4741 code
= (guint8
*)mono_aot_get_method (domain
, m
, &inner_error
);
4742 mono_error_cleanup (&inner_error
);
4747 const char *klass_name_space
= m_class_get_name_space (method
->klass
);
4748 const char *klass_name
= m_class_get_name (method
->klass
);
4749 /* Same for CompareExchange<T> and Exchange<T> */
4750 /* Same for Volatile.Read<T>/Write<T> */
4751 if (method_index
== 0xffffff && method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
&& m_class_get_image (method
->klass
) == mono_defaults
.corlib
&&
4752 ((!strcmp (klass_name_space
, "System.Threading") && !strcmp (klass_name
, "Interlocked") && (!strcmp (method
->name
, "CompareExchange") || !strcmp (method
->name
, "Exchange")) && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (mono_method_signature (method
)->params
[1]))) ||
4753 (!strcmp (klass_name_space
, "System.Threading") && !strcmp (klass_name
, "Volatile") && (!strcmp (method
->name
, "Read") && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (mono_method_signature (method
)->ret
)))) ||
4754 (!strcmp (klass_name_space
, "System.Threading") && !strcmp (klass_name
, "Volatile") && (!strcmp (method
->name
, "Write") && MONO_TYPE_IS_REFERENCE (mini_type_get_underlying_type (mono_method_signature (method
)->params
[1])))))) {
4756 MonoGenericContext ctx
;
4757 MonoType
*args
[16];
4758 gpointer iter
= NULL
;
4760 while ((m
= mono_class_get_methods (method
->klass
, &iter
))) {
4761 if (mono_method_signature (m
)->generic_param_count
&& !strcmp (m
->name
, method
->name
))
4766 memset (&ctx
, 0, sizeof (ctx
));
4767 args
[0] = mono_get_object_type ();
4768 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
4770 m
= mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m
, &ctx
, error
), TRUE
, TRUE
);
4772 g_error ("AOT runtime could not load method due to %s", mono_error_get_message (error
)); /* FIXME don't swallow the error */
4774 /* Avoid recursion */
4779 * Get the code for the <object> instantiation which should be emitted into
4780 * the mscorlib aot image by the AOT compiler.
4782 code
= (guint8
*)mono_aot_get_method (domain
, m
, &inner_error
);
4783 mono_error_cleanup (&inner_error
);
4788 /* For ARRAY_ACCESSOR wrappers with reference types, use the <object> instantiation saved in corlib */
4789 if (method_index
== 0xffffff && method
->wrapper_type
== MONO_WRAPPER_UNKNOWN
) {
4790 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
4792 if (info
->subtype
== WRAPPER_SUBTYPE_ARRAY_ACCESSOR
) {
4793 MonoMethod
*array_method
= info
->d
.array_accessor
.method
;
4794 if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (m_class_get_element_class (array_method
->klass
)))) {
4797 if (!strcmp (array_method
->name
, "Set"))
4798 rank
= mono_method_signature (array_method
)->param_count
- 1;
4799 else if (!strcmp (array_method
->name
, "Get") || !strcmp (array_method
->name
, "Address"))
4800 rank
= mono_method_signature (array_method
)->param_count
;
4802 g_assert_not_reached ();
4803 MonoClass
*obj_array_class
= mono_class_create_array (mono_defaults
.object_class
, rank
);
4804 MonoMethod
*m
= mono_class_get_method_from_name_checked (obj_array_class
, array_method
->name
, mono_method_signature (array_method
)->param_count
, 0, error
);
4805 mono_error_assert_ok (error
);
4808 m
= mono_marshal_get_array_accessor_wrapper (m
);
4810 code
= (guint8
*)mono_aot_get_method (domain
, m
, &inner_error
);
4811 mono_error_cleanup (&inner_error
);
4819 if (method_index
== 0xffffff && method
->is_inflated
&& mono_method_is_generic_sharable_full (method
, FALSE
, TRUE
, FALSE
)) {
4820 /* Partial sharing */
4823 shared
= mini_get_shared_method_full (method
, SHARE_MODE_NONE
, error
);
4824 return_val_if_nok (error
, NULL
);
4826 method_index
= find_aot_method (shared
, &amodule
);
4827 if (method_index
!= 0xffffff)
4831 if (method_index
== 0xffffff && method
->is_inflated
&& mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, TRUE
)) {
4834 /* Use the all-vt shared method since this is what was AOTed */
4835 shared
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
4839 method_index
= find_aot_method (shared
, &amodule
);
4840 if (method_index
!= 0xffffff) {
4841 method
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
4847 if (method_index
== 0xffffff) {
4848 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
)) {
4851 full_name
= mono_method_full_name (method
, TRUE
);
4852 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "AOT NOT FOUND: %s.", full_name
);
4858 if (method_index
== 0xffffff)
4861 /* Needed by find_jit_info */
4862 amodule_lock (amodule
);
4863 g_hash_table_insert (amodule
->extra_methods
, GUINT_TO_POINTER (method_index
), method
);
4864 amodule_unlock (amodule
);
4867 method_index
= mono_metadata_token_index (method
->token
) - 1;
4869 guint32 num_methods
= amodule
->info
.nmethods
- amodule
->info
.nextra_methods
;
4870 if (method_index
>= num_methods
)
4871 /* method not available in AOT image */
4875 code
= (guint8
*)load_method (domain
, amodule
, m_class_get_image (klass
), method
, method
->token
, method_index
, error
);
4878 if (code
&& cache_result
) {
4879 amodule_lock (amodule
);
4880 g_hash_table_insert (amodule
->method_to_code
, orig_method
, code
);
4881 amodule_unlock (amodule
);
4887 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
4891 mono_aot_get_method_from_token (MonoDomain
*domain
, MonoImage
*image
, guint32 token
, MonoError
*error
)
4893 MonoAotModule
*aot_module
= image
->aot_module
;
4902 method_index
= mono_metadata_token_index (token
) - 1;
4904 res
= load_method (domain
, aot_module
, image
, NULL
, token
, method_index
, error
);
4911 } IsGotEntryUserData
;
4914 check_is_got_entry (gpointer key
, gpointer value
, gpointer user_data
)
4916 IsGotEntryUserData
*data
= (IsGotEntryUserData
*)user_data
;
4917 MonoAotModule
*aot_module
= (MonoAotModule
*)value
;
4919 if (aot_module
->got
&& (data
->addr
>= (guint8
*)(aot_module
->got
)) && (data
->addr
< (guint8
*)(aot_module
->got
+ aot_module
->info
.got_size
)))
4924 mono_aot_is_got_entry (guint8
*code
, guint8
*addr
)
4926 IsGotEntryUserData user_data
;
4931 user_data
.addr
= addr
;
4932 user_data
.res
= FALSE
;
4934 g_hash_table_foreach (aot_modules
, check_is_got_entry
, &user_data
);
4937 return user_data
.res
;
4942 MonoAotModule
*module
;
4943 } FindAotModuleUserData
;
4946 find_aot_module_cb (gpointer key
, gpointer value
, gpointer user_data
)
4948 FindAotModuleUserData
*data
= (FindAotModuleUserData
*)user_data
;
4949 MonoAotModule
*aot_module
= (MonoAotModule
*)value
;
4951 if (amodule_contains_code_addr (aot_module
, data
->addr
))
4952 data
->module
= aot_module
;
4955 static inline MonoAotModule
*
4956 find_aot_module (guint8
*code
)
4958 FindAotModuleUserData user_data
;
4963 /* Reading these need no locking */
4964 if (((gsize
)code
< aot_code_low_addr
) || ((gsize
)code
> aot_code_high_addr
))
4967 user_data
.addr
= code
;
4968 user_data
.module
= NULL
;
4971 g_hash_table_foreach (aot_modules
, find_aot_module_cb
, &user_data
);
4974 return user_data
.module
;
4978 mono_aot_patch_plt_entry (guint8
*code
, guint8
*plt_entry
, gpointer
*got
, host_mgreg_t
*regs
, guint8
*addr
)
4980 MonoAotModule
*amodule
;
4983 * Since AOT code is only used in the root domain,
4984 * mono_domain_get () != mono_get_root_domain () means the calling method
4985 * is AppDomain:InvokeInDomain, so this is the same check as in
4986 * mono_method_same_domain () but without loading the metadata for the method.
4988 if (mono_domain_get () == mono_get_root_domain ()) {
4990 amodule
= find_aot_module (code
);
4994 mono_arch_patch_plt_entry (plt_entry
, got
, regs
, addr
);
4999 * mono_aot_plt_resolve:
5001 * This function is called by the entries in the PLT to resolve the actual method that
5002 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
5003 * Returns NULL if the something cannot be loaded.
5006 mono_aot_plt_resolve (gpointer aot_module
, guint32 plt_info_offset
, guint8
*code
, MonoError
*error
)
5008 #ifdef MONO_ARCH_AOT_SUPPORTED
5009 guint8
*p
, *target
, *plt_entry
;
5011 MonoAotModule
*module
= (MonoAotModule
*)aot_module
;
5012 gboolean res
, no_ftnptr
= FALSE
;
5014 gboolean using_gsharedvt
= FALSE
;
5018 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
5020 p
= &module
->blob
[plt_info_offset
];
5022 ji
.type
= (MonoJumpInfoType
)decode_value (p
, &p
);
5024 mp
= mono_mempool_new ();
5025 res
= decode_patch (module
, mp
, &ji
, p
, &p
);
5028 mono_mempool_destroy (mp
);
5032 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
5033 using_gsharedvt
= TRUE
;
5037 * Avoid calling resolve_patch_target in the full-aot case if possible, since
5038 * it would create a trampoline, and we don't need that.
5039 * We could do this only if the method does not need the special handling
5040 * in mono_magic_trampoline ().
5042 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
) &&
5043 !mono_method_needs_static_rgctx_invoke (ji
.data
.method
, FALSE
) && !using_gsharedvt
) {
5044 target
= (guint8
*)mono_jit_compile_method (ji
.data
.method
, error
);
5045 if (!mono_error_ok (error
)) {
5046 mono_mempool_destroy (mp
);
5051 target
= (guint8
*)mono_resolve_patch_target (NULL
, mono_domain_get (), NULL
, &ji
, TRUE
, error
);
5052 if (!mono_error_ok (error
)) {
5053 mono_mempool_destroy (mp
);
5059 * The trampoline expects us to return a function descriptor on platforms which use
5060 * it, but resolve_patch_target returns a direct function pointer for some type of
5061 * patches, so have to translate between the two.
5062 * FIXME: Clean this up, but how ?
5064 if (ji
.type
== MONO_PATCH_INFO_ABS
|| ji
.type
== MONO_PATCH_INFO_INTERNAL_METHOD
|| ji
.type
== MONO_PATCH_INFO_ICALL_ADDR
|| ji
.type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
|| ji
.type
== MONO_PATCH_INFO_RGCTX_FETCH
) {
5065 /* These should already have a function descriptor */
5066 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
5067 /* Our function descriptors have a 0 environment, gcc created ones don't */
5068 if (ji
.type
!= MONO_PATCH_INFO_INTERNAL_METHOD
&& ji
.type
!= MONO_PATCH_INFO_JIT_ICALL_ADDR
&& ji
.type
!= MONO_PATCH_INFO_ICALL_ADDR
)
5069 g_assert (((gpointer
*)target
) [2] == 0);
5072 } else if (!no_ftnptr
) {
5073 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
5074 g_assert (((gpointer
*)target
) [2] != 0);
5076 target
= (guint8
*)mono_create_ftnptr (mono_domain_get (), target
);
5079 mono_mempool_destroy (mp
);
5081 /* Patch the PLT entry with target which might be the actual method not a trampoline */
5082 plt_entry
= mono_aot_get_plt_entry (code
);
5083 g_assert (plt_entry
);
5084 mono_aot_patch_plt_entry (code
, plt_entry
, module
->got
, NULL
, target
);
5088 g_assert_not_reached ();
5096 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
5097 * method in the module is loaded to avoid committing memory by writing to it.
5098 * LOCKING: Assumes the AMODULE lock is held.
5101 init_plt (MonoAotModule
*amodule
)
5106 if (amodule
->plt_inited
)
5109 if (amodule
->info
.plt_size
<= 1) {
5110 amodule
->plt_inited
= TRUE
;
5114 tramp
= mono_create_specific_trampoline (amodule
, MONO_TRAMPOLINE_AOT_PLT
, mono_get_root_domain (), NULL
);
5117 * Initialize the PLT entries in the GOT to point to the default targets.
5120 tramp
= mono_create_ftnptr (mono_domain_get (), tramp
);
5121 for (i
= 1; i
< amodule
->info
.plt_size
; ++i
)
5122 /* All the default entries point to the AOT trampoline */
5123 ((gpointer
*)amodule
->got
)[amodule
->info
.plt_got_offset_base
+ i
] = tramp
;
5125 amodule
->plt_inited
= TRUE
;
5129 * mono_aot_get_plt_entry:
5131 * Return the address of the PLT entry called by the code at CODE if exists.
5134 mono_aot_get_plt_entry (guint8
*code
)
5136 MonoAotModule
*amodule
= find_aot_module (code
);
5137 guint8
*target
= NULL
;
5143 if (is_thumb_code (amodule
, code
- 4))
5144 return mono_arm_get_thumb_plt_entry (code
);
5147 #ifdef MONO_ARCH_AOT_SUPPORTED
5148 target
= mono_arch_get_call_target (code
);
5150 g_assert_not_reached ();
5154 while (target
!= NULL
) {
5155 if ((target
>= (guint8
*)(amodule
->plt
)) && (target
< (guint8
*)(amodule
->plt_end
)))
5158 // Add 4 since mono_arch_get_call_target assumes we're passing
5159 // the instruction after the actual branch instruction.
5160 target
= mono_arch_get_call_target (target
+ 4);
5165 if ((target
>= (guint8
*)(amodule
->plt
)) && (target
< (guint8
*)(amodule
->plt_end
)))
5173 * mono_aot_get_plt_info_offset:
5175 * Return the PLT info offset belonging to the plt entry called by CODE.
5178 mono_aot_get_plt_info_offset (host_mgreg_t
*regs
, guint8
*code
)
5180 guint8
*plt_entry
= mono_aot_get_plt_entry (code
);
5182 g_assert (plt_entry
);
5184 /* The offset is embedded inside the code after the plt entry */
5185 #ifdef MONO_ARCH_AOT_SUPPORTED
5186 return mono_arch_get_plt_info_offset (plt_entry
, regs
, code
);
5188 g_assert_not_reached ();
5194 mono_create_ftnptr_malloc (guint8
*code
)
5196 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
5197 MonoPPCFunctionDescriptor
*ftnptr
= g_malloc0 (sizeof (MonoPPCFunctionDescriptor
));
5199 ftnptr
->code
= code
;
5210 * mono_aot_register_jit_icall:
5212 * Register a JIT icall which is called by trampolines in full-aot mode. This should
5213 * be called from mono_arch_init () during startup.
5216 mono_aot_register_jit_icall (const char *name
, gpointer addr
)
5218 /* No need for locking */
5219 if (!aot_jit_icall_hash
)
5220 aot_jit_icall_hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
5221 g_hash_table_insert (aot_jit_icall_hash
, (char*)name
, addr
);
5225 * load_function_full:
5227 * Load the function named NAME from the aot image.
5230 load_function_full (MonoAotModule
*amodule
, const char *name
, MonoTrampInfo
**out_tinfo
)
5234 int n_patches
, pindex
;
5237 guint32 info_offset
;
5241 symbol
= g_strdup_printf ("%s", name
);
5242 find_amodule_symbol (amodule
, symbol
, (gpointer
*)&code
);
5245 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name
, amodule
->aot_name
);
5247 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "AOT: FOUND function '%s' in AOT file '%s'.", name
, amodule
->aot_name
);
5251 symbol
= g_strdup_printf ("%s_p", name
);
5252 find_amodule_symbol (amodule
, symbol
, (gpointer
*)&p
);
5255 /* Nothing to patch */
5258 info_offset
= *(guint32
*)p
;
5260 MonoTrampInfo
*tinfo
;
5261 guint32 code_size
, uw_info_len
, uw_offset
;
5263 /* Construct a MonoTrampInfo from the data in the AOT image */
5265 p
+= sizeof (guint32
);
5266 code_size
= *(guint32
*)p
;
5267 p
+= sizeof (guint32
);
5268 uw_offset
= *(guint32
*)p
;
5269 uw_info
= amodule
->unwind_info
+ uw_offset
;
5270 uw_info_len
= decode_value (uw_info
, &uw_info
);
5272 tinfo
= g_new0 (MonoTrampInfo
, 1);
5273 tinfo
->code
= (guint8
*)code
;
5274 tinfo
->code_size
= code_size
;
5275 tinfo
->uw_info_len
= uw_info_len
;
5277 tinfo
->uw_info
= uw_info
;
5282 p
= amodule
->blob
+ info_offset
;
5284 /* Similar to mono_aot_load_method () */
5286 n_patches
= decode_value (p
, &p
);
5289 MonoJumpInfo
*patches
;
5292 mp
= mono_mempool_new ();
5294 patches
= load_patch_info (amodule
, mp
, n_patches
, FALSE
, &got_slots
, p
, &p
);
5297 for (pindex
= 0; pindex
< n_patches
; ++pindex
) {
5298 MonoJumpInfo
*ji
= &patches
[pindex
];
5302 if (amodule
->got
[got_slots
[pindex
]])
5306 * When this code is executed, the runtime may not be initalized yet, so
5307 * resolve the patch info by hand.
5309 if (ji
->type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
5310 if (!strcmp (ji
->data
.name
, "mono_get_lmf_addr")) {
5311 target
= (gpointer
)mono_get_lmf_addr
;
5312 } else if (!strcmp (ji
->data
.name
, "mono_thread_force_interruption_checkpoint_noraise")) {
5313 target
= (gpointer
)mono_thread_force_interruption_checkpoint_noraise
;
5314 } else if (!strcmp (ji
->data
.name
, "mono_exception_from_token")) {
5315 target
= (gpointer
)mono_exception_from_token
;
5316 } else if (!strcmp (ji
->data
.name
, "mono_throw_exception")) {
5317 target
= (gpointer
)mono_get_throw_exception ();
5318 } else if (!strcmp (ji
->data
.name
, "mono_rethrow_preserve_exception")) {
5319 target
= (gpointer
)mono_get_rethrow_preserve_exception ();
5320 } else if (strstr (ji
->data
.name
, "trampoline_func_") == ji
->data
.name
) {
5321 MonoTrampolineType tramp_type2
= (MonoTrampolineType
)atoi (ji
->data
.name
+ strlen ("trampoline_func_"));
5322 target
= (gpointer
)mono_get_trampoline_func (tramp_type2
);
5323 } else if (strstr (ji
->data
.name
, "specific_trampoline_lazy_fetch_") == ji
->data
.name
) {
5324 /* atoll is needed because the the offset is unsigned */
5328 res
= sscanf (ji
->data
.name
, "specific_trampoline_lazy_fetch_%u", &slot
);
5329 g_assert (res
== 1);
5330 target
= mono_create_specific_trampoline (GUINT_TO_POINTER (slot
), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH
, mono_get_root_domain (), NULL
);
5331 target
= mono_create_ftnptr_malloc ((guint8
*)target
);
5332 } else if (!strcmp (ji
->data
.name
, "debugger_agent_single_step_from_context")) {
5333 target
= (gpointer
)mini_get_dbg_callbacks ()->single_step_from_context
;
5334 } else if (!strcmp (ji
->data
.name
, "debugger_agent_breakpoint_from_context")) {
5335 target
= (gpointer
)mini_get_dbg_callbacks ()->breakpoint_from_context
;
5336 } else if (!strcmp (ji
->data
.name
, "throw_exception_addr")) {
5337 target
= mono_get_throw_exception_addr ();
5338 } else if (!strcmp (ji
->data
.name
, "rethrow_preserve_exception_addr")) {
5339 target
= mono_get_rethrow_preserve_exception_addr ();
5340 } else if (strstr (ji
->data
.name
, "generic_trampoline_")) {
5341 target
= mono_aot_get_trampoline (ji
->data
.name
);
5342 } else if (aot_jit_icall_hash
&& g_hash_table_lookup (aot_jit_icall_hash
, ji
->data
.name
)) {
5343 /* Registered by mono_arch_init () */
5344 target
= g_hash_table_lookup (aot_jit_icall_hash
, ji
->data
.name
);
5346 fprintf (stderr
, "Unknown relocation '%s'\n", ji
->data
.name
);
5347 g_assert_not_reached ();
5351 /* Hopefully the code doesn't have patches which need method or
5354 target
= mono_resolve_patch_target (NULL
, NULL
, (guint8
*)code
, ji
, FALSE
, error
);
5355 mono_error_assert_ok (error
);
5359 amodule
->got
[got_slots
[pindex
]] = target
;
5364 mono_mempool_destroy (mp
);
5371 load_function (MonoAotModule
*amodule
, const char *name
)
5373 return load_function_full (amodule
, name
, NULL
);
5376 static MonoAotModule
*
5377 get_mscorlib_aot_module (void)
5380 MonoAotModule
*amodule
;
5382 image
= mono_defaults
.corlib
;
5384 amodule
= image
->aot_module
;
5386 amodule
= mscorlib_aot_module
;
5392 no_trampolines (void)
5394 g_assert_not_reached ();
5398 * Return the trampoline identified by NAME from the mscorlib AOT file.
5399 * On ppc64, this returns a function descriptor.
5402 mono_aot_get_trampoline_full (const char *name
, MonoTrampInfo
**out_tinfo
)
5404 MonoAotModule
*amodule
= get_mscorlib_aot_module ();
5406 if (mono_llvm_only
) {
5408 return (gpointer
)no_trampolines
;
5411 return mono_create_ftnptr_malloc ((guint8
*)load_function_full (amodule
, name
, out_tinfo
));
5415 (mono_aot_get_trampoline
) (const char *name
)
5417 MonoTrampInfo
*out_tinfo
;
5420 code
= mono_aot_get_trampoline_full (name
, &out_tinfo
);
5421 mono_aot_tramp_info_register (out_tinfo
, NULL
);
5427 read_unwind_info (MonoAotModule
*amodule
, MonoTrampInfo
*info
, const char *symbol_name
)
5429 gpointer symbol_addr
;
5430 guint32 uw_offset
, uw_info_len
;
5433 find_amodule_symbol (amodule
, symbol_name
, &symbol_addr
);
5438 uw_offset
= *(guint32
*)symbol_addr
;
5439 uw_info
= amodule
->unwind_info
+ uw_offset
;
5440 uw_info_len
= decode_value (uw_info
, &uw_info
);
5442 info
->uw_info_len
= uw_info_len
;
5444 info
->uw_info
= uw_info
;
5446 info
->uw_info
= NULL
;
5448 /* If successful return the address of the following data */
5449 return (guint32
*)symbol_addr
+ 1;
5453 #include <mach/mach.h>
5455 static TrampolinePage
* trampoline_pages
[MONO_AOT_TRAMP_NUM
];
5458 read_page_trampoline_uwinfo (MonoTrampInfo
*info
, int tramp_type
, gboolean is_generic
)
5460 char symbol_name
[128];
5462 if (tramp_type
== MONO_AOT_TRAMP_SPECIFIC
)
5463 sprintf (symbol_name
, "specific_trampolines_page_%s_p", is_generic
? "gen" : "sp");
5464 else if (tramp_type
== MONO_AOT_TRAMP_STATIC_RGCTX
)
5465 sprintf (symbol_name
, "rgctx_trampolines_page_%s_p", is_generic
? "gen" : "sp");
5466 else if (tramp_type
== MONO_AOT_TRAMP_IMT
)
5467 sprintf (symbol_name
, "imt_trampolines_page_%s_p", is_generic
? "gen" : "sp");
5468 else if (tramp_type
== MONO_AOT_TRAMP_GSHAREDVT_ARG
)
5469 sprintf (symbol_name
, "gsharedvt_trampolines_page_%s_p", is_generic
? "gen" : "sp");
5471 g_assert_not_reached ();
5473 read_unwind_info (mono_defaults
.corlib
->aot_module
, info
, symbol_name
);
5476 static unsigned char*
5477 get_new_trampoline_from_page (int tramp_type
)
5479 MonoAotModule
*amodule
;
5481 TrampolinePage
*page
;
5484 vm_address_t addr
, taddr
;
5486 vm_prot_t prot
, max_prot
;
5487 int psize
, specific_trampoline_size
;
5488 unsigned char *code
;
5490 specific_trampoline_size
= 2 * sizeof (gpointer
);
5492 mono_aot_page_lock ();
5493 page
= trampoline_pages
[tramp_type
];
5494 if (page
&& page
->trampolines
< page
->trampolines_end
) {
5495 code
= page
->trampolines
;
5496 page
->trampolines
+= specific_trampoline_size
;
5497 mono_aot_page_unlock ();
5500 mono_aot_page_unlock ();
5501 /* the trampoline template page is in the mscorlib module */
5502 image
= mono_defaults
.corlib
;
5505 psize
= MONO_AOT_TRAMP_PAGE_SIZE
;
5507 amodule
= image
->aot_module
;
5510 if (tramp_type
== MONO_AOT_TRAMP_SPECIFIC
)
5511 tpage
= load_function (amodule
, "specific_trampolines_page");
5512 else if (tramp_type
== MONO_AOT_TRAMP_STATIC_RGCTX
)
5513 tpage
= load_function (amodule
, "rgctx_trampolines_page");
5514 else if (tramp_type
== MONO_AOT_TRAMP_IMT
)
5515 tpage
= load_function (amodule
, "imt_trampolines_page");
5516 else if (tramp_type
== MONO_AOT_TRAMP_GSHAREDVT_ARG
)
5517 tpage
= load_function (amodule
, "gsharedvt_arg_trampolines_page");
5519 g_error ("Incorrect tramp type for trampolines page");
5521 /*g_warning ("loaded trampolines page at %x", tpage);*/
5523 /* avoid the unlikely case of looping forever */
5526 while (page
== NULL
&& count
-- > 0) {
5527 MonoTrampInfo
*gen_info
, *sp_info
;
5530 /* allocate two contiguous pages of memory: the first page will contain the data (like a local constant pool)
5531 * while the second will contain the trampolines.
5534 ret
= vm_allocate (mach_task_self (), &addr
, psize
* 2, VM_FLAGS_ANYWHERE
);
5535 } while (ret
== KERN_ABORTED
);
5536 if (ret
!= KERN_SUCCESS
) {
5537 g_error ("Cannot allocate memory for trampolines: %d", ret
);
5540 /*g_warning ("allocated trampoline double page at %x", addr);*/
5541 /* replace the second page with a remapped trampoline page */
5542 taddr
= addr
+ psize
;
5543 vm_deallocate (mach_task_self (), taddr
, psize
);
5544 ret
= vm_remap (mach_task_self (), &taddr
, psize
, 0, FALSE
, mach_task_self(), (vm_address_t
)tpage
, FALSE
, &prot
, &max_prot
, VM_INHERIT_SHARE
);
5545 if (ret
!= KERN_SUCCESS
) {
5546 /* someone else got the page, try again */
5547 vm_deallocate (mach_task_self (), addr
, psize
);
5550 /*g_warning ("remapped trampoline page at %x", taddr);*/
5552 mono_aot_page_lock ();
5553 page
= trampoline_pages
[tramp_type
];
5554 /* some other thread already allocated, so use that to avoid wasting memory */
5555 if (page
&& page
->trampolines
< page
->trampolines_end
) {
5556 code
= page
->trampolines
;
5557 page
->trampolines
+= specific_trampoline_size
;
5558 mono_aot_page_unlock ();
5559 vm_deallocate (mach_task_self (), addr
, psize
);
5560 vm_deallocate (mach_task_self (), taddr
, psize
);
5563 page
= (TrampolinePage
*)addr
;
5564 page
->next
= trampoline_pages
[tramp_type
];
5565 trampoline_pages
[tramp_type
] = page
;
5566 page
->trampolines
= (guint8
*)(taddr
+ amodule
->info
.tramp_page_code_offsets
[tramp_type
]);
5567 page
->trampolines_end
= (guint8
*)(taddr
+ psize
- 64);
5568 code
= page
->trampolines
;
5569 page
->trampolines
+= specific_trampoline_size
;
5570 mono_aot_page_unlock ();
5572 /* Register the generic part at the beggining of the trampoline page */
5573 gen_info
= mono_tramp_info_create (NULL
, (guint8
*)taddr
, amodule
->info
.tramp_page_code_offsets
[tramp_type
], NULL
, NULL
);
5574 read_page_trampoline_uwinfo (gen_info
, tramp_type
, TRUE
);
5575 mono_aot_tramp_info_register (gen_info
, NULL
);
5578 * Registering each specific trampoline produces a lot of
5579 * MonoJitInfo structures. Jump trampolines are also registered
5582 if (tramp_type
!= MONO_AOT_TRAMP_SPECIFIC
) {
5583 /* Register the rest of the page as a single trampoline */
5584 sp_info
= mono_tramp_info_create (NULL
, code
, page
->trampolines_end
- code
, NULL
, NULL
);
5585 read_page_trampoline_uwinfo (sp_info
, tramp_type
, FALSE
);
5586 mono_aot_tramp_info_register (sp_info
, NULL
);
5590 g_error ("Cannot allocate more trampoline pages: %d", ret
);
5595 static unsigned char*
5596 get_new_trampoline_from_page (int tramp_type
)
5598 g_error ("Page trampolines not supported.");
5605 get_new_specific_trampoline_from_page (gpointer tramp
, gpointer arg
)
5610 code
= get_new_trampoline_from_page (MONO_AOT_TRAMP_SPECIFIC
);
5612 data
= (gpointer
*)((char*)code
- MONO_AOT_TRAMP_PAGE_SIZE
);
5615 /*g_warning ("new trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
5621 get_new_rgctx_trampoline_from_page (gpointer tramp
, gpointer arg
)
5626 code
= get_new_trampoline_from_page (MONO_AOT_TRAMP_STATIC_RGCTX
);
5628 data
= (gpointer
*)((char*)code
- MONO_AOT_TRAMP_PAGE_SIZE
);
5631 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
5637 get_new_imt_trampoline_from_page (gpointer arg
)
5642 code
= get_new_trampoline_from_page (MONO_AOT_TRAMP_IMT
);
5644 data
= (gpointer
*)((char*)code
- MONO_AOT_TRAMP_PAGE_SIZE
);
5646 /*g_warning ("new imt trampoline at %p for data %p, (stored at %p)", code, arg, data);*/
5652 get_new_gsharedvt_arg_trampoline_from_page (gpointer tramp
, gpointer arg
)
5657 code
= get_new_trampoline_from_page (MONO_AOT_TRAMP_GSHAREDVT_ARG
);
5659 data
= (gpointer
*)((char*)code
- MONO_AOT_TRAMP_PAGE_SIZE
);
5662 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
5666 /* Return a given kind of trampoline */
5667 /* FIXME set unwind info for these trampolines */
5669 get_numerous_trampoline (MonoAotTrampoline tramp_type
, int n_got_slots
, MonoAotModule
**out_amodule
, guint32
*got_offset
, guint32
*out_tramp_size
)
5672 MonoAotModule
*amodule
= get_mscorlib_aot_module ();
5673 int index
, tramp_size
;
5675 /* Currently, we keep all trampolines in the mscorlib AOT image */
5676 image
= mono_defaults
.corlib
;
5678 *out_amodule
= amodule
;
5683 #define MONOTOUCH_TRAMPOLINES_ERROR ". See http://docs.xamarin.com/ios/troubleshooting for instructions on how to fix this condition."
5685 #define MONOTOUCH_TRAMPOLINES_ERROR ""
5687 if (amodule
->trampoline_index
[tramp_type
] == amodule
->info
.num_trampolines
[tramp_type
]) {
5688 g_error ("Ran out of trampolines of type %d in '%s' (limit %d)%s\n",
5689 tramp_type
, image
? image
->name
: "mscorlib", amodule
->info
.num_trampolines
[tramp_type
], MONOTOUCH_TRAMPOLINES_ERROR
);
5691 index
= amodule
->trampoline_index
[tramp_type
] ++;
5695 *got_offset
= amodule
->info
.trampoline_got_offset_base
[tramp_type
] + (index
* n_got_slots
);
5697 tramp_size
= amodule
->info
.trampoline_size
[tramp_type
];
5700 *out_tramp_size
= tramp_size
;
5702 return amodule
->trampolines
[tramp_type
] + (index
* tramp_size
);
5706 no_specific_trampoline (void)
5708 g_assert_not_reached ();
5712 * Return a specific trampoline from the AOT file.
5715 mono_aot_create_specific_trampoline (MonoImage
*image
, gpointer arg1
, MonoTrampolineType tramp_type
, MonoDomain
*domain
, guint32
*code_len
)
5717 MonoAotModule
*amodule
;
5718 guint32 got_offset
, tramp_size
;
5719 guint8
*code
, *tramp
;
5720 static gpointer generic_trampolines
[MONO_TRAMPOLINE_NUM
];
5721 static gboolean inited
;
5722 static guint32 num_trampolines
;
5724 if (mono_llvm_only
) {
5726 return (gpointer
)no_specific_trampoline
;
5733 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &num_trampolines
);
5742 if (!generic_trampolines
[tramp_type
]) {
5745 symbol
= mono_get_generic_trampoline_name (tramp_type
);
5746 generic_trampolines
[tramp_type
] = mono_aot_get_trampoline (symbol
);
5750 tramp
= (guint8
*)generic_trampolines
[tramp_type
];
5753 if (USE_PAGE_TRAMPOLINES
) {
5754 code
= (guint8
*)get_new_specific_trampoline_from_page (tramp
, arg1
);
5757 code
= (guint8
*)get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC
, 2, &amodule
, &got_offset
, &tramp_size
);
5759 amodule
->got
[got_offset
] = tramp
;
5760 amodule
->got
[got_offset
+ 1] = arg1
;
5764 *code_len
= tramp_size
;
5770 mono_aot_get_static_rgctx_trampoline (gpointer ctx
, gpointer addr
)
5772 MonoAotModule
*amodule
;
5776 if (USE_PAGE_TRAMPOLINES
) {
5777 code
= (guint8
*)get_new_rgctx_trampoline_from_page (addr
, ctx
);
5779 code
= (guint8
*)get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX
, 2, &amodule
, &got_offset
, NULL
);
5781 amodule
->got
[got_offset
] = ctx
;
5782 amodule
->got
[got_offset
+ 1] = addr
;
5785 /* The caller expects an ftnptr */
5786 return mono_create_ftnptr (mono_domain_get (), code
);
5790 mono_aot_get_unbox_trampoline (MonoMethod
*method
)
5793 guint32 method_index
= mono_metadata_token_index (method
->token
) - 1;
5794 MonoAotModule
*amodule
;
5796 guint32
*ut
, *ut_end
, *entry
;
5797 int low
, high
, entry_index
= 0;
5798 MonoTrampInfo
*tinfo
;
5800 if (method
->is_inflated
&& !mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, FALSE
)) {
5801 method_index
= find_aot_method (method
, &amodule
);
5802 if (method_index
== 0xffffff && mono_method_is_generic_sharable_full (method
, FALSE
, TRUE
, FALSE
)) {
5803 MonoMethod
*shared
= mini_get_shared_method_full (method
, SHARE_MODE_NONE
, error
);
5804 mono_error_assert_ok (error
);
5805 method_index
= find_aot_method (shared
, &amodule
);
5807 if (method_index
== 0xffffff && mono_method_is_generic_sharable_full (method
, FALSE
, TRUE
, TRUE
)) {
5808 MonoMethod
*shared
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
5809 mono_error_assert_ok (error
);
5811 method_index
= find_aot_method (shared
, &amodule
);
5813 g_assert (method_index
!= 0xffffff);
5815 amodule
= m_class_get_image (method
->klass
)->aot_module
;
5819 if (amodule
->info
.llvm_get_unbox_tramp
) {
5820 gpointer (*get_tramp
) (int) = (gpointer (*)(int))amodule
->info
.llvm_get_unbox_tramp
;
5821 code
= get_tramp (method_index
);
5827 ut
= amodule
->unbox_trampolines
;
5828 ut_end
= amodule
->unbox_trampolines_end
;
5830 /* Do a binary search in the sorted table */
5833 high
= (ut_end
- ut
);
5834 while (low
< high
) {
5835 entry_index
= (low
+ high
) / 2;
5836 entry
= &ut
[entry_index
];
5837 if (entry
[0] < method_index
) {
5838 low
= entry_index
+ 1;
5839 } else if (entry
[0] > method_index
) {
5846 code
= get_call_table_entry (amodule
->unbox_trampoline_addresses
, entry_index
);
5849 tinfo
= mono_tramp_info_create (NULL
, (guint8
*)code
, 0, NULL
, NULL
);
5851 gpointer
const symbol_addr
= read_unwind_info (amodule
, tinfo
, "unbox_trampoline_p");
5853 mono_tramp_info_free (tinfo
);
5857 tinfo
->code_size
= *(guint32
*)symbol_addr
;
5858 mono_aot_tramp_info_register (tinfo
, NULL
);
5860 /* The caller expects an ftnptr */
5861 return mono_create_ftnptr (mono_domain_get (), code
);
5865 mono_aot_get_lazy_fetch_trampoline (guint32 slot
)
5869 MonoAotModule
*amodule
= mono_defaults
.corlib
->aot_module
;
5870 guint32 index
= MONO_RGCTX_SLOT_INDEX (slot
);
5871 static int count
= 0;
5874 if (index
>= amodule
->info
.num_rgctx_fetch_trampolines
) {
5875 static gpointer addr
;
5879 * Use the general version of the rgctx fetch trampoline. It receives a pair of <slot, trampoline> in the rgctx arg reg.
5882 addr
= load_function (amodule
, "rgctx_fetch_trampoline_general");
5883 info
= (void **)mono_domain_alloc0 (mono_get_root_domain (), sizeof (gpointer
) * 2);
5884 info
[0] = GUINT_TO_POINTER (slot
);
5885 info
[1] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot
), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH
, mono_get_root_domain (), NULL
);
5886 code
= mono_aot_get_static_rgctx_trampoline (info
, addr
);
5887 return mono_create_ftnptr (mono_domain_get (), code
);
5890 symbol
= mono_get_rgctx_fetch_trampoline_name (slot
);
5891 code
= load_function (mono_defaults
.corlib
->aot_module
, symbol
);
5893 /* The caller expects an ftnptr */
5894 return mono_create_ftnptr (mono_domain_get (), code
);
5898 no_imt_trampoline (void)
5900 g_assert_not_reached ();
5904 mono_aot_get_imt_trampoline (MonoVTable
*vtable
, MonoDomain
*domain
, MonoIMTCheckItem
**imt_entries
, int count
, gpointer fail_tramp
)
5909 int i
, index
, real_count
;
5910 MonoAotModule
*amodule
;
5913 return (gpointer
)no_imt_trampoline
;
5916 for (i
= 0; i
< count
; ++i
) {
5917 MonoIMTCheckItem
*item
= imt_entries
[i
];
5919 if (item
->is_equals
)
5923 /* Save the entries into an array */
5924 buf
= (void **)mono_domain_alloc (domain
, (real_count
+ 1) * 2 * sizeof (gpointer
));
5926 for (i
= 0; i
< count
; ++i
) {
5927 MonoIMTCheckItem
*item
= imt_entries
[i
];
5929 if (!item
->is_equals
)
5932 g_assert (item
->key
);
5934 buf
[(index
* 2)] = item
->key
;
5935 if (item
->has_target_code
) {
5936 gpointer
*p
= (gpointer
*)mono_domain_alloc (domain
, sizeof (gpointer
));
5937 *p
= item
->value
.target_code
;
5938 buf
[(index
* 2) + 1] = p
;
5940 buf
[(index
* 2) + 1] = &(vtable
->vtable
[item
->value
.vtable_slot
]);
5944 buf
[(index
* 2)] = NULL
;
5945 buf
[(index
* 2) + 1] = fail_tramp
;
5947 if (USE_PAGE_TRAMPOLINES
) {
5948 code
= get_new_imt_trampoline_from_page (buf
);
5950 code
= get_numerous_trampoline (MONO_AOT_TRAMP_IMT
, 1, &amodule
, &got_offset
, NULL
);
5952 amodule
->got
[got_offset
] = buf
;
5959 mono_aot_get_gsharedvt_arg_trampoline (gpointer arg
, gpointer addr
)
5961 MonoAotModule
*amodule
;
5965 if (USE_PAGE_TRAMPOLINES
) {
5966 code
= (guint8
*)get_new_gsharedvt_arg_trampoline_from_page (addr
, arg
);
5968 code
= (guint8
*)get_numerous_trampoline (MONO_AOT_TRAMP_GSHAREDVT_ARG
, 2, &amodule
, &got_offset
, NULL
);
5970 amodule
->got
[got_offset
] = arg
;
5971 amodule
->got
[got_offset
+ 1] = addr
;
5974 /* The caller expects an ftnptr */
5975 return mono_create_ftnptr (mono_domain_get (), code
);
5978 #ifdef MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE
5980 mono_aot_get_ftnptr_arg_trampoline (gpointer arg
, gpointer addr
)
5982 MonoAotModule
*amodule
;
5986 if (USE_PAGE_TRAMPOLINES
) {
5987 g_error ("FIXME: ftnptr_arg page trampolines");
5989 code
= (guint8
*)get_numerous_trampoline (MONO_AOT_TRAMP_FTNPTR_ARG
, 2, &amodule
, &got_offset
, NULL
);
5991 amodule
->got
[got_offset
] = arg
;
5992 amodule
->got
[got_offset
+ 1] = addr
;
5995 /* The caller expects an ftnptr */
5996 return mono_create_ftnptr (mono_domain_get (), code
);
6002 * mono_aot_set_make_unreadable:
6004 * Set whenever to make all mmaped memory unreadable. In conjuction with a
6005 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
6008 mono_aot_set_make_unreadable (gboolean unreadable
)
6012 make_unreadable
= unreadable
;
6014 if (make_unreadable
&& !inited
) {
6015 mono_counters_register ("AOT: pagefaults", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &n_pagefaults
);
6020 MonoAotModule
*module
;
6025 find_map (gpointer key
, gpointer value
, gpointer user_data
)
6027 MonoAotModule
*module
= (MonoAotModule
*)value
;
6028 FindMapUserData
*data
= (FindMapUserData
*)user_data
;
6031 if ((data
->ptr
>= module
->mem_begin
) && (data
->ptr
< module
->mem_end
))
6032 data
->module
= module
;
6035 static MonoAotModule
*
6036 find_module_for_addr (void *ptr
)
6038 FindMapUserData data
;
6040 if (!make_unreadable
)
6044 data
.ptr
= (guint8
*)ptr
;
6047 g_hash_table_foreach (aot_modules
, (GHFunc
)find_map
, &data
);
6054 * mono_aot_is_pagefault:
6056 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
6057 * within memory allocated by this module.
6060 mono_aot_is_pagefault (void *ptr
)
6062 if (!make_unreadable
)
6066 * Not signal safe, but SIGSEGV's are synchronous, and
6067 * this is only turned on by a MONO_DEBUG option.
6069 return find_module_for_addr (ptr
) != NULL
;
6073 * mono_aot_handle_pagefault:
6075 * Handle a pagefault caused by an unreadable page by making it readable again.
6078 mono_aot_handle_pagefault (void *ptr
)
6081 guint8
* start
= (guint8
*)ROUND_DOWN (((gssize
)ptr
), mono_pagesize ());
6085 res
= mono_mprotect (start
, mono_pagesize (), MONO_MMAP_READ
|MONO_MMAP_WRITE
|MONO_MMAP_EXEC
);
6086 g_assert (res
== 0);
6097 mono_aot_init (void)
6102 mono_aot_cleanup (void)
6107 mono_aot_find_method_index (MonoMethod
*method
)
6109 g_assert_not_reached ();
6114 mono_aot_init_llvm_method (gpointer aot_module
, guint32 method_index
)
6119 mono_aot_init_gshared_method_this (gpointer aot_module
, guint32 method_index
, MonoObject
*this)
6124 mono_aot_init_gshared_method_mrgctx (gpointer aot_module
, guint32 method_index
, MonoMethodRuntimeGenericContext
*rgctx
)
6129 mono_aot_init_gshared_method_vtable (gpointer aot_module
, guint32 method_index
, MonoVTable
*vtable
)
6134 mono_aot_get_method (MonoDomain
*domain
,
6135 MonoMethod
*method
, MonoError
*error
)
6142 mono_aot_is_got_entry (guint8
*code
, guint8
*addr
)
6148 mono_aot_get_cached_class_info (MonoClass
*klass
, MonoCachedClassInfo
*res
)
6154 mono_aot_get_class_from_name (MonoImage
*image
, const char *name_space
, const char *name
, MonoClass
**klass
)
6160 mono_aot_find_jit_info (MonoDomain
*domain
, MonoImage
*image
, gpointer addr
)
6166 mono_aot_get_method_from_token (MonoDomain
*domain
, MonoImage
*image
, guint32 token
, MonoError
*error
)
6173 mono_aot_get_plt_entry (guint8
*code
)
6179 mono_aot_plt_resolve (gpointer aot_module
, guint32 plt_info_offset
, guint8
*code
, MonoError
*error
)
6185 mono_aot_patch_plt_entry (guint8
*code
, guint8
*plt_entry
, gpointer
*got
, host_mgreg_t
*regs
, guint8
*addr
)
6190 mono_aot_get_method_from_vt_slot (MonoDomain
*domain
, MonoVTable
*vtable
, int slot
, MonoError
*error
)
6198 mono_aot_get_plt_info_offset (host_mgreg_t
*regs
, guint8
*code
)
6200 g_assert_not_reached ();
6206 mono_aot_create_specific_trampoline (MonoImage
*image
, gpointer arg1
, MonoTrampolineType tramp_type
, MonoDomain
*domain
, guint32
*code_len
)
6208 g_assert_not_reached ();
6213 mono_aot_get_static_rgctx_trampoline (gpointer ctx
, gpointer addr
)
6215 g_assert_not_reached ();
6220 mono_aot_get_trampoline_full (const char *name
, MonoTrampInfo
**out_tinfo
)
6222 g_assert_not_reached ();
6227 mono_aot_get_trampoline (const char *name
)
6229 g_assert_not_reached ();
6234 mono_aot_get_unbox_trampoline (MonoMethod
*method
)
6236 g_assert_not_reached ();
6241 mono_aot_get_lazy_fetch_trampoline (guint32 slot
)
6243 g_assert_not_reached ();
6248 mono_aot_get_imt_trampoline (MonoVTable
*vtable
, MonoDomain
*domain
, MonoIMTCheckItem
**imt_entries
, int count
, gpointer fail_tramp
)
6250 g_assert_not_reached ();
6255 mono_aot_get_gsharedvt_arg_trampoline (gpointer arg
, gpointer addr
)
6257 g_assert_not_reached ();
6262 mono_aot_set_make_unreadable (gboolean unreadable
)
6267 mono_aot_is_pagefault (void *ptr
)
6273 mono_aot_handle_pagefault (void *ptr
)
6278 mono_aot_get_unwind_info (MonoJitInfo
*ji
, guint32
*unwind_info_len
)
6280 g_assert_not_reached ();
6285 mono_aot_register_jit_icall (const char *name
, gpointer addr
)
6290 mono_aot_get_weak_field_indexes (MonoImage
*image
)