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>
63 #include <mono/utils/bsearch.h>
66 #include "seq-points.h"
68 #include "debugger-agent.h"
69 #include "aot-compiler.h"
70 #include "aot-runtime.h"
71 #include "jit-icalls.h"
72 #include "mini-runtime.h"
77 #define ENABLE_AOT_CACHE
80 /* Number of got entries shared between the JIT and LLVM GOT */
81 #define N_COMMON_GOT_ENTRIES 10
83 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
90 #define GOT_INITIALIZING 1
91 #define GOT_INITIALIZED 2
93 struct MonoAotModule
{
95 /* Pointer to the Global Offset Table */
99 GHashTable
*name_cache
;
100 GHashTable
*extra_methods
;
101 /* Maps methods to their code */
102 GHashTable
*method_to_code
;
103 /* Maps pointers into the method info to the methods themselves */
104 GHashTable
*method_ref_to_method
;
105 MonoAssemblyName
*image_names
;
107 MonoAssembly
*assembly
;
108 MonoImage
**image_table
;
109 guint32 image_table_len
;
110 gboolean out_of_date
;
115 guint8
*jit_code_start
;
116 guint8
*jit_code_end
;
117 guint8
*llvm_code_start
;
118 guint8
*llvm_code_end
;
122 gpointer weak_field_indexes
;
123 /* Maps method indexes to their code */
125 /* Sorted array of method addresses */
126 gpointer
*sorted_methods
;
127 /* Method indexes for each method in sorted_methods */
128 int *sorted_method_indexes
;
129 /* The length of the two tables above */
130 int sorted_methods_len
;
131 guint32
*method_info_offsets
;
132 guint32
*ex_info_offsets
;
133 guint32
*class_info_offsets
;
134 guint32
*got_info_offsets
;
135 guint32
*llvm_got_info_offsets
;
136 guint32
*methods_loaded
;
137 guint16
*class_name_table
;
138 guint32
*extra_method_table
;
139 guint32
*extra_method_info_offsets
;
140 guint32
*unbox_trampolines
;
141 guint32
*unbox_trampolines_end
;
142 guint32
*unbox_trampoline_addresses
;
145 /* Points to the mono EH data created by LLVM */
146 guint8
*mono_eh_frame
;
148 /* Points to the data tables if MONO_AOT_FILE_FLAG_SEPARATE_DATA is set */
149 gpointer tables
[MONO_AOT_TABLE_NUM
];
150 /* Points to the trampolines */
151 guint8
*trampolines
[MONO_AOT_TRAMP_NUM
];
152 /* The first unused trampoline of each kind */
153 guint32 trampoline_index
[MONO_AOT_TRAMP_NUM
];
155 gboolean use_page_trampolines
;
157 MonoAotFileInfo info
;
162 JitInfoMap
*async_jit_info_table
;
168 unsigned char *trampolines
;
169 unsigned char *trampolines_end
;
172 static GHashTable
*aot_modules
;
173 #define mono_aot_lock() mono_os_mutex_lock (&aot_mutex)
174 #define mono_aot_unlock() mono_os_mutex_unlock (&aot_mutex)
175 static mono_mutex_t aot_mutex
;
178 * Maps assembly names to the mono_aot_module_<NAME>_info symbols in the
179 * AOT modules registered by mono_aot_register_module ().
181 static GHashTable
*static_aot_modules
;
183 * Same as above, but tracks module that must be loaded before others are
184 * This allows us to have a "container" module which contains resources for
185 * other modules. Since it doesn't provide methods for a managed assembly,
186 * and it needs to be fully loaded by the time the other code needs it, it
187 * must be eagerly loaded before other modules.
189 static char *container_assm_name
= NULL
;
190 static MonoAotModule
*container_amodule
= NULL
;
193 * Maps MonoJitInfo* to the aot module they belong to, this can be different
194 * from ji->method->klass->image's aot module for generic instances.
196 static GHashTable
*ji_to_amodule
;
198 /* Maps method addresses to MonoAotMethodFlags */
199 static GHashTable
*code_to_method_flags
;
202 * Whenever to AOT compile loaded assemblies on demand and store them in
205 static gboolean enable_aot_cache
= FALSE
;
207 static gboolean mscorlib_aot_loaded
;
210 static gint32 mono_last_aot_method
= -1;
212 static gboolean make_unreadable
= FALSE
;
213 static guint32 name_table_accesses
= 0;
214 static guint32 n_pagefaults
= 0;
216 /* Used to speed-up find_aot_module () */
217 static gsize aot_code_low_addr
= (gssize
)-1;
218 static gsize aot_code_high_addr
= 0;
221 static gint32 async_jit_info_size
;
223 static GHashTable
*aot_jit_icall_hash
;
226 #define USE_PAGE_TRAMPOLINES (mono_defaults.corlib->aot_module->use_page_trampolines)
228 #define USE_PAGE_TRAMPOLINES 0
231 #define mono_aot_page_lock() mono_os_mutex_lock (&aot_page_mutex)
232 #define mono_aot_page_unlock() mono_os_mutex_unlock (&aot_page_mutex)
233 static mono_mutex_t aot_page_mutex
;
235 static MonoAotModule
*mscorlib_aot_module
;
237 /* Embedding API hooks to load the AOT data for AOT images compiled with MONO_AOT_FILE_FLAG_SEPARATE_DATA */
238 static MonoLoadAotDataFunc aot_data_load_func
;
239 static MonoFreeAotDataFunc aot_data_free_func
;
240 static gpointer aot_data_func_user_data
;
243 init_plt (MonoAotModule
*info
);
246 compute_llvm_code_range (MonoAotModule
*amodule
, guint8
**code_start
, guint8
**code_end
);
249 init_method (MonoAotModule
*amodule
, guint32 method_index
, MonoMethod
*method
, MonoClass
*init_class
, MonoGenericContext
*context
, MonoError
*error
);
252 decode_patches (MonoAotModule
*amodule
, MonoMemPool
*mp
, int n_patches
, gboolean llvm
, guint32
*got_offsets
);
255 amodule_lock (MonoAotModule
*amodule
)
257 mono_os_mutex_lock (&amodule
->mutex
);
261 amodule_unlock (MonoAotModule
*amodule
)
263 mono_os_mutex_unlock (&amodule
->mutex
);
269 * Load one of the images referenced by AMODULE. Returns NULL if the image is not
270 * found, and sets @error for what happened
273 load_image (MonoAotModule
*amodule
, int index
, MonoError
*error
)
275 MonoAssembly
*assembly
;
276 MonoImageOpenStatus status
;
278 g_assert (index
< amodule
->image_table_len
);
282 if (amodule
->image_table
[index
])
283 return amodule
->image_table
[index
];
284 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
);
285 if (amodule
->out_of_date
) {
286 mono_error_set_bad_image_by_name (error
, amodule
->aot_name
, "Image out of date");
291 * LoadFile allows loading more than one assembly with the same name.
292 * That means that just calling mono_assembly_load is unlikely to find
293 * the correct assembly (it'll just return the first one loaded). But
294 * we shouldn't hardcode the full assembly filepath into the AOT image,
295 * so it's not obvious that we can call mono_assembly_open_predicate.
297 * In the JIT, an assembly opened with LoadFile is supposed to only
298 * refer to already-loaded assemblies (or to GAC & MONO_PATH)
299 * assemblies - so nothing new should be loading. And for the
300 * LoadFile'd assembly itself, we can check if the name and guid of the
301 * current AOT module matches the wanted name and guid and just return
302 * the AOT module's assembly.
304 if (mono_asmctx_get_kind (&amodule
->assembly
->context
) == MONO_ASMCTX_INDIVIDUAL
&&
305 !strcmp (amodule
->assembly
->image
->guid
, amodule
->image_guids
[index
]) &&
306 mono_assembly_names_equal (&amodule
->image_names
[index
], &amodule
->assembly
->aname
))
307 assembly
= amodule
->assembly
;
309 assembly
= mono_assembly_load (&amodule
->image_names
[index
], amodule
->assembly
->basedir
, &status
);
311 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
);
312 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
);
313 amodule
->out_of_date
= TRUE
;
317 if (strcmp (assembly
->image
->guid
, amodule
->image_guids
[index
])) {
318 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
);
319 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
);
320 amodule
->out_of_date
= TRUE
;
324 amodule
->image_table
[index
] = assembly
->image
;
325 return assembly
->image
;
329 decode_value (guint8
*ptr
, guint8
**rptr
)
334 if ((b
& 0x80) == 0){
337 } else if ((b
& 0x40) == 0){
338 len
= ((b
& 0x3f) << 8 | ptr
[1]);
340 } else if (b
!= 0xff) {
341 len
= ((b
& 0x1f) << 24) |
348 len
= (ptr
[1] << 24) | (ptr
[2] << 16) | (ptr
[3] << 8) | ptr
[4];
354 //printf ("DECODE: %d.\n", len);
359 * mono_aot_get_offset:
361 * Decode an offset table emitted by emit_offset_table (), returning the INDEXth
365 mono_aot_get_offset (guint32
*table
, int index
)
367 int i
, group
, ngroups
, index_entry_size
;
368 int start_offset
, offset
, group_size
;
369 guint8
*data_start
, *p
;
370 guint32
*index32
= NULL
;
371 guint16
*index16
= NULL
;
373 /* noffsets = table [0]; */
374 group_size
= table
[1];
376 index_entry_size
= table
[3];
377 group
= index
/ group_size
;
379 if (index_entry_size
== 2) {
380 index16
= (guint16
*)&table
[4];
381 data_start
= (guint8
*)&index16
[ngroups
];
382 p
= data_start
+ index16
[group
];
384 index32
= (guint32
*)&table
[4];
385 data_start
= (guint8
*)&index32
[ngroups
];
386 p
= data_start
+ index32
[group
];
389 /* offset will contain the value of offsets [group * group_size] */
390 offset
= start_offset
= decode_value (p
, &p
);
391 for (i
= group
* group_size
+ 1; i
<= index
; ++i
) {
392 offset
+= decode_value (p
, &p
);
395 //printf ("Offset lookup: %d -> %d, start=%d, p=%d\n", index, offset, start_offset, table [3 + group]);
401 decode_resolve_method_ref (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
);
404 decode_klass_ref (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
);
407 decode_type (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
);
409 static MonoGenericInst
*
410 decode_generic_inst (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
413 MonoType
**type_argv
;
414 MonoGenericInst
*inst
;
418 type_argc
= decode_value (p
, &p
);
419 type_argv
= g_new0 (MonoType
*, type_argc
);
421 for (i
= 0; i
< type_argc
; ++i
) {
422 MonoClass
*pclass
= decode_klass_ref (module
, p
, &p
, error
);
427 type_argv
[i
] = m_class_get_byval_arg (pclass
);
430 inst
= mono_metadata_get_generic_inst (type_argc
, type_argv
);
439 decode_generic_context (MonoAotModule
*amodule
, MonoGenericContext
*ctx
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
443 guint32 offset
, flags
;
445 /* Either the class_inst or method_inst offset */
446 flags
= decode_value (p
, &p
);
449 offset
= decode_value (p
, &p
);
450 p2
= amodule
->blob
+ offset
;
451 ctx
->class_inst
= decode_generic_inst (amodule
, p2
, &p2
, error
);
452 if (!ctx
->class_inst
)
456 offset
= decode_value (p
, &p
);
457 p2
= amodule
->blob
+ offset
;
458 ctx
->method_inst
= decode_generic_inst (amodule
, p2
, &p2
, error
);
459 if (!ctx
->method_inst
)
468 decode_klass_ref (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
471 MonoClass
*klass
= NULL
, *eklass
;
472 guint32 token
, rank
, idx
;
477 reftype
= decode_value (p
, &p
);
480 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Decoding a null class ref");
485 case MONO_AOT_TYPEREF_TYPEDEF_INDEX
:
486 idx
= decode_value (p
, &p
);
487 image
= load_image (module
, 0, error
);
490 klass
= mono_class_get_checked (image
, MONO_TOKEN_TYPE_DEF
+ idx
, error
);
492 case MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE
:
493 idx
= decode_value (p
, &p
);
494 image
= load_image (module
, decode_value (p
, &p
), error
);
497 klass
= mono_class_get_checked (image
, MONO_TOKEN_TYPE_DEF
+ idx
, error
);
499 case MONO_AOT_TYPEREF_TYPESPEC_TOKEN
:
500 token
= decode_value (p
, &p
);
501 image
= module
->assembly
->image
;
503 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "No image associated with the aot module");
506 klass
= mono_class_get_checked (image
, token
, error
);
508 case MONO_AOT_TYPEREF_GINST
: {
510 MonoGenericContext ctx
;
513 gclass
= decode_klass_ref (module
, p
, &p
, error
);
516 g_assert (mono_class_is_gtd (gclass
));
518 memset (&ctx
, 0, sizeof (ctx
));
519 guint32 offset
= decode_value (p
, &p
);
520 guint8
*p2
= module
->blob
+ offset
;
521 ctx
.class_inst
= decode_generic_inst (module
, p2
, &p2
, error
);
524 type
= mono_class_inflate_generic_type_checked (m_class_get_byval_arg (gclass
), &ctx
, error
);
527 klass
= mono_class_from_mono_type_internal (type
);
528 mono_metadata_free_type (type
);
531 case MONO_AOT_TYPEREF_VAR
: {
533 MonoGenericContainer
*container
= NULL
;
534 gboolean has_constraint
= decode_value (p
, &p
);
536 if (has_constraint
) {
537 MonoClass
*par_klass
;
538 MonoType
*gshared_constraint
;
540 gshared_constraint
= decode_type (module
, p
, &p
, error
);
541 if (!gshared_constraint
)
544 par_klass
= decode_klass_ref (module
, p
, &p
, error
);
548 t
= mini_get_shared_gparam (m_class_get_byval_arg (par_klass
), gshared_constraint
);
549 mono_metadata_free_type (gshared_constraint
);
550 klass
= mono_class_from_mono_type_internal (t
);
552 int type
= decode_value (p
, &p
);
553 int num
= decode_value (p
, &p
);
554 gboolean is_not_anonymous
= decode_value (p
, &p
);
556 if (is_not_anonymous
) {
557 gboolean is_method
= decode_value (p
, &p
);
560 MonoMethod
*method_def
;
561 g_assert (type
== MONO_TYPE_MVAR
);
562 method_def
= decode_resolve_method_ref (module
, p
, &p
, error
);
566 container
= mono_method_get_generic_container (method_def
);
568 MonoClass
*class_def
;
569 g_assert (type
== MONO_TYPE_VAR
);
570 class_def
= decode_klass_ref (module
, p
, &p
, error
);
574 container
= mono_class_try_get_generic_container (class_def
); //FIXME is this a case for a try_get?
577 // We didn't decode is_method, so we have to infer it from type enum.
578 container
= mono_get_anonymous_container_for_image (module
->assembly
->image
, type
== MONO_TYPE_MVAR
);
581 t
= g_new0 (MonoType
, 1);
582 t
->type
= (MonoTypeEnum
)type
;
583 if (is_not_anonymous
) {
584 t
->data
.generic_param
= mono_generic_container_get_param (container
, num
);
587 MonoGenericParam
*par
= mono_metadata_create_anon_gparam (module
->assembly
->image
, num
, type
== MONO_TYPE_MVAR
);
588 t
->data
.generic_param
= par
;
589 // FIXME: maybe do this for all anon gparams?
590 ((MonoGenericParamFull
*)par
)->info
.name
= mono_make_generic_name_string (module
->assembly
->image
, num
);
592 // FIXME: Maybe use types directly to avoid
593 // the overhead of creating MonoClass-es
594 klass
= mono_class_from_mono_type_internal (t
);
600 case MONO_AOT_TYPEREF_ARRAY
:
602 rank
= decode_value (p
, &p
);
603 eklass
= decode_klass_ref (module
, p
, &p
, error
);
606 klass
= mono_class_create_array (eklass
, rank
);
608 case MONO_AOT_TYPEREF_PTR
: {
611 t
= decode_type (module
, p
, &p
, error
);
614 klass
= mono_class_from_mono_type_internal (t
);
618 case MONO_AOT_TYPEREF_BLOB_INDEX
: {
619 guint32 offset
= decode_value (p
, &p
);
622 p2
= module
->blob
+ offset
;
623 klass
= decode_klass_ref (module
, p2
, &p2
, error
);
627 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid klass reftype %d", reftype
);
630 //printf ("BLA: %s\n", mono_type_full_name (m_class_get_byval_arg (klass)));
635 static MonoClassField
*
636 decode_field_info (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
)
639 MonoClass
*klass
= decode_klass_ref (module
, buf
, &buf
, error
);
644 mono_error_cleanup (error
); /* FIXME don't swallow the error */
648 token
= MONO_TOKEN_FIELD_DEF
+ decode_value (p
, &p
);
652 return mono_class_get_field (klass
, token
);
656 * Parse a MonoType encoded by encode_type () in aot-compiler.c. Return malloc-ed
660 decode_type (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
665 if (*p
== MONO_TYPE_CMOD_REQD
) {
668 int count
= decode_value (p
, &p
);
670 t
= (MonoType
*)g_malloc0 (mono_sizeof_type_with_mods (count
));
673 MonoCustomModContainer
*cm
= mono_type_get_cmods (t
);
674 int iindex
= decode_value (p
, &p
);
675 cm
->image
= load_image (module
, iindex
, error
);
681 for (int i
= 0; i
< cm
->count
; ++i
) {
682 cm
->modifiers
[i
].required
= decode_value (p
, &p
);
683 cm
->modifiers
[i
].token
= decode_value (p
, &p
);
686 t
= (MonoType
*) g_malloc0 (sizeof (MonoType
));
690 if (*p
== MONO_TYPE_PINNED
) {
693 } else if (*p
== MONO_TYPE_BYREF
) {
701 t
->type
= (MonoTypeEnum
)*p
;
706 case MONO_TYPE_BOOLEAN
:
720 case MONO_TYPE_STRING
:
721 case MONO_TYPE_OBJECT
:
722 case MONO_TYPE_TYPEDBYREF
:
724 case MONO_TYPE_VALUETYPE
:
725 case MONO_TYPE_CLASS
:
726 t
->data
.klass
= decode_klass_ref (module
, p
, &p
, error
);
730 case MONO_TYPE_SZARRAY
:
731 t
->data
.klass
= decode_klass_ref (module
, p
, &p
, error
);
737 t
->data
.type
= decode_type (module
, p
, &p
, error
);
741 case MONO_TYPE_GENERICINST
: {
743 MonoGenericContext ctx
;
747 gclass
= decode_klass_ref (module
, p
, &p
, error
);
750 g_assert (mono_class_is_gtd (gclass
));
752 memset (&ctx
, 0, sizeof (ctx
));
753 ctx
.class_inst
= decode_generic_inst (module
, p
, &p
, error
);
756 type
= mono_class_inflate_generic_type_checked (m_class_get_byval_arg (gclass
), &ctx
, error
);
759 klass
= mono_class_from_mono_type_internal (type
);
760 t
->data
.generic_class
= mono_class_get_generic_class (klass
);
763 case MONO_TYPE_ARRAY
: {
764 MonoArrayType
*array
;
767 // FIXME: memory management
768 array
= g_new0 (MonoArrayType
, 1);
769 array
->eklass
= decode_klass_ref (module
, p
, &p
, error
);
772 array
->rank
= decode_value (p
, &p
);
773 array
->numsizes
= decode_value (p
, &p
);
776 array
->sizes
= (int *)g_malloc0 (sizeof (int) * array
->numsizes
);
777 for (i
= 0; i
< array
->numsizes
; ++i
)
778 array
->sizes
[i
] = decode_value (p
, &p
);
780 array
->numlobounds
= decode_value (p
, &p
);
781 if (array
->numlobounds
)
782 array
->lobounds
= (int *)g_malloc0 (sizeof (int) * array
->numlobounds
);
783 for (i
= 0; i
< array
->numlobounds
; ++i
)
784 array
->lobounds
[i
] = decode_value (p
, &p
);
785 t
->data
.array
= array
;
789 case MONO_TYPE_MVAR
: {
790 MonoClass
*klass
= decode_klass_ref (module
, p
, &p
, error
);
793 t
->data
.generic_param
= m_class_get_byval_arg (klass
)->data
.generic_param
;
797 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid encoded type %d", t
->type
);
809 // FIXME: Error handling, memory management
811 static MonoMethodSignature
*
812 decode_signature_with_target (MonoAotModule
*module
, MonoMethodSignature
*target
, guint8
*buf
, guint8
**endbuf
)
815 MonoMethodSignature
*sig
;
817 int i
, gen_param_count
= 0, param_count
, call_conv
;
819 gboolean hasthis
, explicit_this
, has_gen_params
;
823 has_gen_params
= (flags
& 0x10) != 0;
824 hasthis
= (flags
& 0x20) != 0;
825 explicit_this
= (flags
& 0x40) != 0;
826 call_conv
= flags
& 0x0F;
829 gen_param_count
= decode_value (p
, &p
);
830 param_count
= decode_value (p
, &p
);
831 if (target
&& param_count
!= target
->param_count
)
833 sig
= (MonoMethodSignature
*)g_malloc0 (MONO_SIZEOF_METHOD_SIGNATURE
+ param_count
* sizeof (MonoType
*));
834 sig
->param_count
= param_count
;
835 sig
->sentinelpos
= -1;
836 sig
->hasthis
= hasthis
;
837 sig
->explicit_this
= explicit_this
;
838 sig
->call_convention
= call_conv
;
839 sig
->generic_param_count
= gen_param_count
;
840 sig
->ret
= decode_type (module
, p
, &p
, error
);
843 for (i
= 0; i
< param_count
; ++i
) {
844 if (*p
== MONO_TYPE_SENTINEL
) {
845 g_assert (sig
->call_convention
== MONO_CALL_VARARG
);
846 sig
->sentinelpos
= i
;
849 sig
->params
[i
] = decode_type (module
, p
, &p
, error
);
850 if (!sig
->params
[i
])
854 if (sig
->call_convention
== MONO_CALL_VARARG
&& sig
->sentinelpos
== -1)
855 sig
->sentinelpos
= sig
->param_count
;
861 mono_error_cleanup (error
); /* FIXME don't swallow the error */
866 static MonoMethodSignature
*
867 decode_signature (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
)
869 return decode_signature_with_target (module
, NULL
, buf
, endbuf
);
873 sig_matches_target (MonoAotModule
*module
, MonoMethod
*target
, guint8
*buf
, guint8
**endbuf
)
875 MonoMethodSignature
*sig
;
879 sig
= decode_signature_with_target (module
, mono_method_signature_internal (target
), p
, &p
);
880 res
= sig
&& mono_metadata_signature_equal (mono_method_signature_internal (target
), sig
);
886 /* Stores information returned by decode_method_ref () */
891 gboolean no_aot_trampoline
;
895 * decode_method_ref_with_target:
897 * Decode a method reference, storing the image/token into a MethodRef structure.
898 * This avoids loading metadata for the method if the caller does not need it. If the method has
899 * no token, then it is loaded from metadata and ref->method is set to the method instance.
900 * If TARGET is non-NULL, abort decoding if it can be determined that the decoded method
901 * couldn't resolve to TARGET, and return FALSE.
902 * There are some kinds of method references which only support a non-null TARGET.
903 * This means that its not possible to decode this into a method, only to check
904 * that the method reference matches a given method. This is normally not a problem
905 * as these wrappers only occur in the extra_methods table, where we already have
906 * a method we want to lookup.
908 * If there was a decoding error, we return FALSE and set @error
911 decode_method_ref_with_target (MonoAotModule
*module
, MethodRef
*ref
, MonoMethod
*target
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
913 guint32 image_index
, value
;
914 MonoImage
*image
= NULL
;
917 memset (ref
, 0, sizeof (MethodRef
));
920 value
= decode_value (p
, &p
);
921 image_index
= value
>> 24;
923 if (image_index
== MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE
) {
924 ref
->no_aot_trampoline
= TRUE
;
925 value
= decode_value (p
, &p
);
926 image_index
= value
>> 24;
929 if (image_index
< MONO_AOT_METHODREF_MIN
|| image_index
== MONO_AOT_METHODREF_METHODSPEC
||
930 image_index
== MONO_AOT_METHODREF_GINST
|| image_index
== MONO_AOT_METHODREF_BLOB_INDEX
) {
931 if (target
&& target
->wrapper_type
) {
936 if (image_index
== MONO_AOT_METHODREF_WRAPPER
) {
938 guint32 wrapper_type
;
940 wrapper_type
= decode_value (p
, &p
);
942 if (target
&& target
->wrapper_type
!= wrapper_type
)
946 image
= mono_defaults
.corlib
;
948 switch (wrapper_type
) {
949 #ifndef DISABLE_REMOTING
950 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
: {
951 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
954 mono_class_init_internal (m
->klass
);
958 ref
->method
= mono_marshal_get_remoting_invoke_with_check (m
, error
);
959 return_val_if_nok (error
, FALSE
);
963 case MONO_WRAPPER_PROXY_ISINST
: {
964 MonoClass
*klass
= decode_klass_ref (module
, p
, &p
, error
);
967 ref
->method
= mono_marshal_get_proxy_cancast (klass
);
970 case MONO_WRAPPER_LDFLD
:
971 case MONO_WRAPPER_LDFLDA
:
972 case MONO_WRAPPER_STFLD
: {
973 MonoClass
*klass
= decode_klass_ref (module
, p
, &p
, error
);
976 MonoType
*type
= m_class_get_byval_arg (klass
);
977 if (wrapper_type
== MONO_WRAPPER_LDFLD
)
978 ref
->method
= mono_marshal_get_ldfld_wrapper (type
);
979 else if (wrapper_type
== MONO_WRAPPER_LDFLDA
)
980 ref
->method
= mono_marshal_get_ldflda_wrapper (type
);
981 else if (wrapper_type
== MONO_WRAPPER_STFLD
)
982 ref
->method
= mono_marshal_get_stfld_wrapper (type
);
984 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Unknown AOT wrapper type %d", wrapper_type
);
990 case MONO_WRAPPER_ALLOC
: {
991 int atype
= decode_value (p
, &p
);
992 ManagedAllocatorVariant variant
=
993 mono_profiler_allocations_enabled () ?
994 MANAGED_ALLOCATOR_PROFILER
: MANAGED_ALLOCATOR_REGULAR
;
996 ref
->method
= mono_gc_get_managed_allocator_by_type (atype
, variant
);
997 /* Try to fallback to the slow path version */
999 ref
->method
= mono_gc_get_managed_allocator_by_type (atype
, MANAGED_ALLOCATOR_SLOW_PATH
);
1001 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");
1006 case MONO_WRAPPER_WRITE_BARRIER
: {
1007 ref
->method
= mono_gc_get_write_barrier ();
1010 case MONO_WRAPPER_STELEMREF
: {
1011 int subtype
= decode_value (p
, &p
);
1013 if (subtype
== WRAPPER_SUBTYPE_NONE
) {
1014 ref
->method
= mono_marshal_get_stelemref ();
1015 } else if (subtype
== WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
) {
1018 kind
= decode_value (p
, &p
);
1020 /* Can't decode this */
1023 if (target
->wrapper_type
== MONO_WRAPPER_STELEMREF
) {
1024 info
= mono_marshal_get_wrapper_info (target
);
1027 if (info
->subtype
== subtype
&& info
->d
.virtual_stelemref
.kind
== kind
)
1028 ref
->method
= target
;
1035 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid STELEMREF subtype %d", subtype
);
1040 case MONO_WRAPPER_SYNCHRONIZED
: {
1041 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1044 ref
->method
= mono_marshal_get_synchronized_wrapper (m
);
1047 case MONO_WRAPPER_OTHER
: {
1048 int subtype
= decode_value (p
, &p
);
1050 if (subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
|| subtype
== WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
) {
1051 MonoClass
*klass
= decode_klass_ref (module
, p
, &p
, error
);
1057 if (klass
!= target
->klass
)
1060 if (subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
) {
1061 if (strcmp (target
->name
, "PtrToStructure"))
1063 ref
->method
= mono_marshal_get_ptr_to_struct (klass
);
1065 if (strcmp (target
->name
, "StructureToPtr"))
1067 ref
->method
= mono_marshal_get_struct_to_ptr (klass
);
1069 } else if (subtype
== WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
) {
1070 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1073 ref
->method
= mono_marshal_get_synchronized_inner_wrapper (m
);
1074 } else if (subtype
== WRAPPER_SUBTYPE_ARRAY_ACCESSOR
) {
1075 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1078 ref
->method
= mono_marshal_get_array_accessor_wrapper (m
);
1079 } else if (subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN
) {
1080 ref
->method
= mono_marshal_get_gsharedvt_in_wrapper ();
1081 } else if (subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT
) {
1082 ref
->method
= mono_marshal_get_gsharedvt_out_wrapper ();
1083 } else if (subtype
== WRAPPER_SUBTYPE_INTERP_IN
) {
1084 MonoMethodSignature
*sig
= decode_signature (module
, p
, &p
);
1087 ref
->method
= mini_get_interp_in_wrapper (sig
);
1088 } else if (subtype
== WRAPPER_SUBTYPE_INTERP_LMF
) {
1089 MonoJitICallInfo
*info
= mono_find_jit_icall_by_name ((char *) p
);
1091 ref
->method
= mini_get_interp_lmf_wrapper (info
->name
, (gpointer
) info
->func
);
1092 } else if (subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
) {
1093 MonoMethodSignature
*sig
= decode_signature (module
, p
, &p
);
1096 ref
->method
= mini_get_gsharedvt_in_sig_wrapper (sig
);
1097 } else if (subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
) {
1098 MonoMethodSignature
*sig
= decode_signature (module
, p
, &p
);
1101 ref
->method
= mini_get_gsharedvt_out_sig_wrapper (sig
);
1103 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid UNKNOWN wrapper subtype %d", subtype
);
1108 case MONO_WRAPPER_MANAGED_TO_MANAGED
: {
1109 int subtype
= decode_value (p
, &p
);
1111 if (subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
) {
1112 int rank
= decode_value (p
, &p
);
1113 int elem_size
= decode_value (p
, &p
);
1115 ref
->method
= mono_marshal_get_array_address (rank
, elem_size
);
1116 } else if (subtype
== WRAPPER_SUBTYPE_STRING_CTOR
) {
1119 m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1125 g_assert (target
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_MANAGED
);
1127 info
= mono_marshal_get_wrapper_info (target
);
1128 if (info
&& info
->subtype
== subtype
&& info
->d
.string_ctor
.method
== m
)
1129 ref
->method
= target
;
1135 case MONO_WRAPPER_MANAGED_TO_NATIVE
: {
1137 int subtype
= decode_value (p
, &p
);
1140 if (subtype
== WRAPPER_SUBTYPE_ICALL_WRAPPER
) {
1143 MonoJitICallInfo
*info
= mono_find_jit_icall_by_name (name
);
1145 ref
->method
= mono_icall_get_wrapper_method (info
);
1147 m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1151 /* This should only happen when looking for an extra method */
1154 if (mono_marshal_method_from_wrapper (target
) == m
)
1155 ref
->method
= target
;
1161 case MONO_WRAPPER_CASTCLASS
: {
1162 int subtype
= decode_value (p
, &p
);
1164 if (subtype
== WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE
)
1165 ref
->method
= mono_marshal_get_castclass_with_cache ();
1166 else if (subtype
== WRAPPER_SUBTYPE_ISINST_WITH_CACHE
)
1167 ref
->method
= mono_marshal_get_isinst_with_cache ();
1169 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid CASTCLASS wrapper subtype %d", subtype
);
1174 case MONO_WRAPPER_RUNTIME_INVOKE
: {
1175 int subtype
= decode_value (p
, &p
);
1180 if (subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC
) {
1181 if (strcmp (target
->name
, "runtime_invoke_dynamic") != 0)
1183 ref
->method
= target
;
1184 } else if (subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
) {
1185 /* Direct wrapper */
1186 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1189 ref
->method
= mono_marshal_get_runtime_invoke (m
, FALSE
);
1190 } else if (subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
) {
1191 /* Virtual direct wrapper */
1192 MonoMethod
*m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1195 ref
->method
= mono_marshal_get_runtime_invoke (m
, TRUE
);
1197 MonoMethodSignature
*sig
;
1199 sig
= decode_signature_with_target (module
, NULL
, p
, &p
);
1200 info
= mono_marshal_get_wrapper_info (target
);
1203 if (info
->subtype
!= subtype
)
1205 g_assert (info
->d
.runtime_invoke
.sig
);
1206 if (mono_metadata_signature_equal (sig
, info
->d
.runtime_invoke
.sig
))
1207 ref
->method
= target
;
1213 case MONO_WRAPPER_DELEGATE_INVOKE
:
1214 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
1215 case MONO_WRAPPER_DELEGATE_END_INVOKE
: {
1216 gboolean is_inflated
= decode_value (p
, &p
);
1217 WrapperSubtype subtype
;
1221 MonoMethod
*invoke
, *wrapper
;
1223 klass
= decode_klass_ref (module
, p
, &p
, error
);
1227 switch (wrapper_type
) {
1228 case MONO_WRAPPER_DELEGATE_INVOKE
:
1229 invoke
= mono_get_delegate_invoke_internal (klass
);
1230 wrapper
= mono_marshal_get_delegate_invoke (invoke
, NULL
);
1232 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
1233 invoke
= mono_get_delegate_begin_invoke_internal (klass
);
1234 wrapper
= mono_marshal_get_delegate_begin_invoke (invoke
);
1236 case MONO_WRAPPER_DELEGATE_END_INVOKE
:
1237 invoke
= mono_get_delegate_end_invoke_internal (klass
);
1238 wrapper
= mono_marshal_get_delegate_end_invoke (invoke
);
1241 g_assert_not_reached ();
1246 * Due to the way mini_get_shared_method_full () works, we could end up with
1247 * multiple copies of the same wrapper.
1249 if (wrapper
->klass
!= target
->klass
)
1251 ref
->method
= target
;
1253 ref
->method
= wrapper
;
1257 * These wrappers are associated with a signature, not with a method.
1258 * Since we can't decode them into methods, they need a target method.
1263 if (wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
) {
1264 subtype
= (WrapperSubtype
)decode_value (p
, &p
);
1265 info
= mono_marshal_get_wrapper_info (target
);
1267 if (info
->subtype
!= subtype
)
1270 if (subtype
!= WRAPPER_SUBTYPE_NONE
)
1274 if (sig_matches_target (module
, target
, p
, &p
))
1275 ref
->method
= target
;
1281 case MONO_WRAPPER_NATIVE_TO_MANAGED
: {
1285 m
= decode_resolve_method_ref (module
, p
, &p
, error
);
1288 klass
= decode_klass_ref (module
, p
, &p
, error
);
1291 ref
->method
= mono_marshal_get_managed_wrapper (m
, klass
, 0, error
);
1292 if (!mono_error_ok (error
))
1297 g_assert_not_reached ();
1299 } else if (image_index
== MONO_AOT_METHODREF_METHODSPEC
) {
1300 image_index
= decode_value (p
, &p
);
1301 ref
->token
= decode_value (p
, &p
);
1303 image
= load_image (module
, image_index
, error
);
1306 } else if (image_index
== MONO_AOT_METHODREF_BLOB_INDEX
) {
1307 guint32 offset
= decode_value (p
, &p
);
1311 p2
= module
->blob
+ offset
;
1312 if (!decode_method_ref_with_target (module
, ref
, target
, p2
, &p2
, error
))
1317 } else if (image_index
== MONO_AOT_METHODREF_GINST
) {
1319 MonoGenericContext ctx
;
1320 guint32 token_index
;
1323 * These methods do not have a token which resolves them, so we
1324 * resolve them immediately.
1326 klass
= decode_klass_ref (module
, p
, &p
, error
);
1330 if (target
&& target
->klass
!= klass
)
1333 image_index
= decode_value (p
, &p
);
1334 token_index
= decode_value (p
, &p
);
1335 ref
->token
= mono_metadata_make_token (MONO_TABLE_METHOD
, token_index
);
1337 image
= load_image (module
, image_index
, error
);
1341 ref
->method
= mono_get_method_checked (image
, ref
->token
, NULL
, NULL
, error
);
1345 memset (&ctx
, 0, sizeof (ctx
));
1347 if (FALSE
&& mono_class_is_ginst (klass
)) {
1348 ctx
.class_inst
= mono_class_get_generic_class (klass
)->context
.class_inst
;
1349 ctx
.method_inst
= NULL
;
1351 ref
->method
= mono_class_inflate_generic_method_full_checked (ref
->method
, klass
, &ctx
, error
);
1356 memset (&ctx
, 0, sizeof (ctx
));
1358 if (!decode_generic_context (module
, &ctx
, p
, &p
, error
))
1361 ref
->method
= mono_class_inflate_generic_method_full_checked (ref
->method
, klass
, &ctx
, error
);
1365 } else if (image_index
== MONO_AOT_METHODREF_ARRAY
) {
1369 klass
= decode_klass_ref (module
, p
, &p
, error
);
1372 method_type
= decode_value (p
, &p
);
1373 switch (method_type
) {
1375 ref
->method
= mono_class_get_method_from_name_checked (klass
, ".ctor", m_class_get_rank (klass
), 0, error
);
1376 return_val_if_nok (error
, FALSE
);
1379 ref
->method
= mono_class_get_method_from_name_checked (klass
, ".ctor", m_class_get_rank (klass
) * 2, 0, error
);
1380 return_val_if_nok (error
, FALSE
);
1383 ref
->method
= mono_class_get_method_from_name_checked (klass
, "Get", -1, 0, error
);
1384 return_val_if_nok (error
, FALSE
);
1387 ref
->method
= mono_class_get_method_from_name_checked (klass
, "Address", -1, 0, error
);
1388 return_val_if_nok (error
, FALSE
);
1391 ref
->method
= mono_class_get_method_from_name_checked (klass
, "Set", -1, 0, error
);
1392 return_val_if_nok (error
, FALSE
);
1395 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "Invalid METHODREF_ARRAY method type %d", method_type
);
1399 if (image_index
== MONO_AOT_METHODREF_LARGE_IMAGE_INDEX
) {
1400 image_index
= decode_value (p
, &p
);
1401 value
= decode_value (p
, &p
);
1404 ref
->token
= MONO_TOKEN_METHOD_DEF
| (value
& 0xffffff);
1406 image
= load_image (module
, image_index
, error
);
1419 decode_method_ref (MonoAotModule
*module
, MethodRef
*ref
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
1421 return decode_method_ref_with_target (module
, ref
, NULL
, buf
, endbuf
, error
);
1425 * decode_resolve_method_ref_with_target:
1427 * Similar to decode_method_ref, but resolve and return the method itself.
1430 decode_resolve_method_ref_with_target (MonoAotModule
*module
, MonoMethod
*target
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
1436 if (!decode_method_ref_with_target (module
, &ref
, target
, buf
, endbuf
, error
))
1441 mono_error_set_bad_image_by_name (error
, module
->aot_name
, "No image found for methodref with target");
1444 return mono_get_method_checked (ref
.image
, ref
.token
, NULL
, NULL
, error
);
1448 decode_resolve_method_ref (MonoAotModule
*module
, guint8
*buf
, guint8
**endbuf
, MonoError
*error
)
1450 return decode_resolve_method_ref_with_target (module
, NULL
, buf
, endbuf
, error
);
1453 #ifdef ENABLE_AOT_CACHE
1459 * - Add options for controlling the cache size
1460 * - Handle full cache by deleting old assemblies lru style
1461 * - Maybe add a threshold after an assembly is AOT compiled
1462 * - Add options for enabling this for specific main assemblies
1465 /* The cache directory */
1466 static char *cache_dir
;
1468 /* The number of assemblies AOTed in this run */
1469 static int cache_count
;
1471 /* Whenever to AOT in-process */
1472 static gboolean in_process
;
1475 collect_assemblies (gpointer data
, gpointer user_data
)
1477 MonoAssembly
*ass
= (MonoAssembly
*)data
;
1478 GSList
**l
= (GSList
**)user_data
;
1480 *l
= g_slist_prepend (*l
, ass
);
1483 #define SHA1_DIGEST_LENGTH 20
1486 * get_aot_config_hash:
1488 * Return a hash for all the version information an AOT module depends on.
1490 static G_GNUC_UNUSED
char*
1491 get_aot_config_hash (MonoAssembly
*assembly
)
1494 GSList
*l
, *assembly_list
= NULL
;
1497 guint8 digest
[SHA1_DIGEST_LENGTH
];
1500 build_info
= mono_get_runtime_build_info ();
1502 s
= g_string_new (build_info
);
1504 mono_assembly_foreach (collect_assemblies
, &assembly_list
);
1507 * The assembly list includes the current assembly as well, no need
1510 for (l
= assembly_list
; l
; l
= l
->next
) {
1511 MonoAssembly
*ass
= (MonoAssembly
*)l
->data
;
1513 g_string_append (s
, "_");
1514 g_string_append (s
, ass
->aname
.name
);
1515 g_string_append (s
, "_");
1516 g_string_append (s
, ass
->image
->guid
);
1519 for (i
= 0; i
< s
->len
; ++i
) {
1520 if (!isalnum (s
->str
[i
]) && s
->str
[i
] != '-')
1524 mono_sha1_get_digest ((guint8
*)s
->str
, s
->len
, digest
);
1526 digest_str
= g_malloc0 ((SHA1_DIGEST_LENGTH
* 2) + 1);
1527 for (i
= 0; i
< SHA1_DIGEST_LENGTH
; ++i
)
1528 sprintf (digest_str
+ (i
* 2), "%02x", digest
[i
]);
1530 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: file dependencies: %s, hash %s", s
->str
, digest_str
);
1532 g_string_free (s
, TRUE
);
1538 aot_cache_init (void)
1542 enable_aot_cache
= TRUE
;
1547 * aot_cache_load_module:
1549 * Load the AOT image corresponding to ASSEMBLY from the aot cache, AOTing it if neccessary.
1552 aot_cache_load_module (MonoAssembly
*assembly
, char **aot_name
)
1554 MonoAotCacheConfig
*config
;
1556 char *fname
, *tmp2
, *aot_options
, *failure_fname
;
1568 if (image_is_dynamic (assembly
->image
))
1571 /* Check in the list of assemblies enabled for aot caching */
1572 config
= mono_get_aot_cache_config ();
1576 MonoDomain
*domain
= mono_domain_get ();
1577 MonoAssembly
*entry_assembly
= domain
->entry_assembly
;
1579 // FIXME: This cannot be used for mscorlib during startup, since entry_assembly is not set yet
1580 for (l
= config
->apps
; l
; l
= l
->next
) {
1581 char *n
= (char*)l
->data
;
1583 if ((entry_assembly
&& !strcmp (entry_assembly
->aname
.name
, n
)) || (!entry_assembly
&& !strcmp (assembly
->aname
.name
, n
)))
1591 for (l
= config
->assemblies
; l
; l
= l
->next
) {
1592 char *n
= (char*)l
->data
;
1594 if (!strcmp (assembly
->aname
.name
, n
))
1604 home
= g_get_home_dir ();
1607 cache_dir
= g_strdup_printf ("%s/Library/Caches/mono/aot-cache", home
);
1608 if (!g_file_test (cache_dir
, (GFileTest
)(G_FILE_TEST_EXISTS
|G_FILE_TEST_IS_DIR
)))
1609 g_mkdir_with_parents (cache_dir
, 0777);
1613 * The same assembly can be used in multiple configurations, i.e. multiple
1614 * versions of the runtime, with multiple versions of dependent assemblies etc.
1615 * To handle this, we compute a version string containing all this information, hash it,
1616 * and use the hash as a filename suffix.
1618 hash
= get_aot_config_hash (assembly
);
1620 tmp2
= g_strdup_printf ("%s-%s%s", assembly
->image
->assembly_name
, hash
, MONO_SOLIB_EXT
);
1621 fname
= g_build_filename (cache_dir
, tmp2
, NULL
);
1625 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: loading from cache: '%s'.", fname
);
1626 module
= mono_dl_open (fname
, MONO_DL_LAZY
, NULL
);
1629 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: found in cache: '%s'.", fname
);
1633 if (mono_is_corlib_image (assembly
->image
) && !mscorlib_aot_loaded
)
1635 * Can't AOT this during startup, so we AOT it when called later from
1636 * mono_aot_get_method ().
1640 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: not found.");
1642 /* Only AOT one assembly per run to avoid slowing down execution too much */
1643 if (cache_count
> 0)
1647 /* Check for previous failure */
1648 failure_fname
= g_strdup_printf ("%s.failure", fname
);
1649 failure_file
= fopen (failure_fname
, "r");
1651 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: assembly '%s' previously failed to compile '%s' ('%s')... ", assembly
->image
->name
, fname
, failure_fname
);
1652 g_free (failure_fname
);
1655 g_free (failure_fname
);
1656 fclose (failure_file
);
1659 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: compiling assembly '%s', logfile: '%s.log'... ", assembly
->image
->name
, fname
);
1662 * We need to invoke the AOT compiler here. There are multiple approaches:
1663 * - spawn a new runtime process. This can be hard when running with mkbundle, and
1664 * its hard to make the new process load the same set of assemblies.
1665 * - doing it in-process. This exposes the current process to bugs/leaks/side effects of
1667 * - fork a new process and do the work there.
1670 aot_options
= g_strdup_printf ("outfile=%s,internal-logfile=%s.log%s%s", fname
, fname
, config
->aot_options
? "," : "", config
->aot_options
? config
->aot_options
: "");
1671 /* Maybe due this in another thread ? */
1672 res
= mono_compile_assembly (assembly
, mono_parse_default_optimizations (NULL
), aot_options
, NULL
);
1674 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: compilation failed.");
1675 failure_fname
= g_strdup_printf ("%s.failure", fname
);
1676 failure_file
= fopen (failure_fname
, "a+");
1677 fclose (failure_file
);
1678 g_free (failure_fname
);
1680 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: compilation succeeded.");
1684 * - Avoid waiting for the aot process to finish ?
1685 * (less overhead, but multiple processes could aot the same assembly at the same time)
1694 logfile_name
= g_strdup_printf ("%s/aot.log", cache_dir
);
1695 logfile
= fopen (logfile_name
, "a+");
1696 g_free (logfile_name
);
1698 dup2 (fileno (logfile
), 1);
1699 dup2 (fileno (logfile
), 2);
1701 aot_options
= g_strdup_printf ("outfile=%s", fname
);
1702 res
= mono_compile_assembly (assembly
, mono_parse_default_optimizations (NULL
), aot_options
, NULL
);
1710 waitpid (pid
, &exit_status
, 0);
1711 if (!WIFEXITED (exit_status
) && (WEXITSTATUS (exit_status
) == 0))
1712 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: failed.");
1714 mono_trace (G_LOG_LEVEL_MESSAGE
, MONO_TRACE_AOT
, "AOT: succeeded.");
1718 module
= mono_dl_open (fname
, MONO_DL_LAZY
, NULL
);
1726 aot_cache_init (void)
1731 aot_cache_load_module (MonoAssembly
*assembly
, char **aot_name
)
1739 find_symbol (MonoDl
*module
, gpointer
*globals
, const char *name
, gpointer
*value
)
1743 guint16
*table
, *entry
;
1746 char *symbol
= (char*)name
;
1749 symbol
= g_strdup_printf ("_%s", name
);
1752 /* The first entry points to the hash */
1753 table
= (guint16
*)globals
[0];
1756 table_size
= table
[0];
1759 hash
= mono_metadata_str_hash (symbol
) % table_size
;
1761 entry
= &table
[hash
* 2];
1763 /* Search the hash for the index into the globals table */
1765 while (entry
[0] != 0) {
1766 guint32 index
= entry
[0] - 1;
1767 guint32 next
= entry
[1];
1769 //printf ("X: %s %s\n", (char*)globals [index * 2], name);
1771 if (!strcmp ((const char*)globals
[index
* 2], symbol
)) {
1772 global_index
= index
;
1777 entry
= &table
[next
* 2];
1783 if (global_index
!= -1)
1784 *value
= globals
[global_index
* 2 + 1];
1791 char *err
= mono_dl_symbol (module
, name
, value
);
1799 find_amodule_symbol (MonoAotModule
*amodule
, const char *name
, gpointer
*value
)
1801 g_assert (!(amodule
->info
.flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
));
1803 find_symbol (amodule
->sofile
, amodule
->globals
, name
, value
);
1807 mono_install_load_aot_data_hook (MonoLoadAotDataFunc load_func
, MonoFreeAotDataFunc free_func
, gpointer user_data
)
1809 aot_data_load_func
= load_func
;
1810 aot_data_free_func
= free_func
;
1811 aot_data_func_user_data
= user_data
;
1814 /* Load the separate aot data file for ASSEMBLY */
1816 open_aot_data (MonoAssembly
*assembly
, MonoAotFileInfo
*info
, void **ret_handle
)
1822 if (aot_data_load_func
) {
1823 data
= aot_data_load_func (assembly
, info
->datafile_size
, aot_data_func_user_data
, ret_handle
);
1829 * Use <assembly name>.aotdata as the default implementation if no callback is given
1831 filename
= g_strdup_printf ("%s.aotdata", assembly
->image
->name
);
1832 map
= mono_file_map_open (filename
);
1834 data
= (guint8
*)mono_file_map (info
->datafile_size
, MONO_MMAP_READ
, mono_file_map_fd (map
), 0, ret_handle
);
1841 check_usable (MonoAssembly
*assembly
, MonoAotFileInfo
*info
, guint8
*blob
, char **out_msg
)
1845 gboolean usable
= TRUE
;
1846 gboolean full_aot
, interp
, safepoints
;
1847 guint32 excluded_cpu_optimizations
;
1849 if (strcmp (assembly
->image
->guid
, (const char*)info
->assembly_guid
)) {
1850 msg
= g_strdup_printf ("doesn't match assembly");
1854 build_info
= mono_get_runtime_build_info ();
1855 if (strlen ((const char *)info
->runtime_version
) > 0 && strcmp (info
->runtime_version
, build_info
)) {
1856 msg
= g_strdup_printf ("compiled against runtime version '%s' while this runtime has version '%s'", info
->runtime_version
, build_info
);
1859 g_free (build_info
);
1861 full_aot
= info
->flags
& MONO_AOT_FILE_FLAG_FULL_AOT
;
1862 interp
= info
->flags
& MONO_AOT_FILE_FLAG_INTERP
;
1864 if (mono_aot_only
&& !full_aot
) {
1866 msg
= g_strdup_printf ("not compiled with --aot=full");
1870 if (!mono_aot_only
&& full_aot
) {
1871 msg
= g_strdup_printf ("compiled with --aot=full");
1874 if (mono_use_interpreter
&& !interp
&& !strcmp (assembly
->aname
.name
, "mscorlib")) {
1875 /* mscorlib contains necessary interpreter trampolines */
1876 msg
= g_strdup_printf ("not compiled with --aot=interp");
1879 if (mono_llvm_only
&& !(info
->flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
)) {
1880 msg
= g_strdup_printf ("not compiled with --aot=llvmonly");
1883 if (mono_use_llvm
&& !(info
->flags
& MONO_AOT_FILE_FLAG_WITH_LLVM
)) {
1884 /* Prefer LLVM JITted code when using --llvm */
1885 msg
= g_strdup_printf ("not compiled with --aot=llvm");
1888 if (mini_get_debug_options ()->mdb_optimizations
&& !(info
->flags
& MONO_AOT_FILE_FLAG_DEBUG
) && !full_aot
&& !interp
) {
1889 msg
= g_strdup_printf ("not compiled for debugging");
1893 mono_arch_cpu_optimizations (&excluded_cpu_optimizations
);
1894 if (info
->opts
& excluded_cpu_optimizations
) {
1895 msg
= g_strdup_printf ("compiled with unsupported CPU optimizations");
1899 if (!mono_aot_only
&& (info
->simd_opts
& ~mono_arch_cpu_enumerate_simd_versions ())) {
1900 msg
= g_strdup_printf ("compiled with unsupported SIMD extensions");
1904 if (info
->gc_name_index
!= -1) {
1905 char *gc_name
= (char*)&blob
[info
->gc_name_index
];
1906 const char *current_gc_name
= mono_gc_get_gc_name ();
1908 if (strcmp (current_gc_name
, gc_name
) != 0) {
1909 msg
= g_strdup_printf ("compiled against GC %s, while the current runtime uses GC %s.\n", gc_name
, current_gc_name
);
1914 safepoints
= info
->flags
& MONO_AOT_FILE_FLAG_SAFEPOINTS
;
1916 if (!safepoints
&& mono_threads_are_safepoints_enabled ()) {
1917 msg
= g_strdup_printf ("not compiled with safepoints");
1926 * TABLE should point to a table of call instructions. Return the address called by the INDEXth entry.
1929 get_call_table_entry (void *table
, int index
)
1931 #if defined(TARGET_ARM)
1936 ins_addr
= (guint32
*)table
+ index
;
1938 if ((ins
>> ARMCOND_SHIFT
) == ARMCOND_NV
) {
1940 offset
= (((int)(((ins
& 0xffffff) << 1) | ((ins
>> 24) & 0x1))) << 7) >> 7;
1941 return (char*)ins_addr
+ (offset
* 2) + 8 + 1;
1943 offset
= (((int)ins
& 0xffffff) << 8) >> 8;
1944 return (char*)ins_addr
+ (offset
* 4) + 8;
1946 #elif defined(TARGET_ARM64)
1947 return mono_arch_get_call_target ((guint8
*)table
+ (index
* 4) + 4);
1948 #elif defined(TARGET_X86) || defined(TARGET_AMD64)
1949 /* The callee expects an ip which points after the call */
1950 return mono_arch_get_call_target ((guint8
*)table
+ (index
* 5) + 5);
1952 g_assert_not_reached ();
1960 * Initialize the shared got entries for AMODULE.
1963 init_amodule_got (MonoAotModule
*amodule
)
1967 MonoJumpInfo
*patches
;
1968 guint32 got_offsets
[128];
1972 /* These can't be initialized in load_aot_module () */
1973 if (amodule
->got_initialized
== GOT_INITIALIZED
)
1976 mono_loader_lock ();
1979 * If it is initialized some other thread did it in the meantime. If it is
1980 * initializing it means the current thread is initializing it since we are
1981 * holding the loader lock, skip it.
1983 if (amodule
->got_initialized
) {
1984 mono_loader_unlock ();
1988 amodule
->got_initialized
= GOT_INITIALIZING
;
1990 mp
= mono_mempool_new ();
1991 npatches
= amodule
->info
.nshared_got_entries
;
1992 for (i
= 0; i
< npatches
; ++i
)
1993 got_offsets
[i
] = i
;
1995 patches
= decode_patches (amodule
, mp
, npatches
, FALSE
, got_offsets
);
1997 patches
= decode_patches (amodule
, mp
, npatches
, TRUE
, got_offsets
);
1999 for (i
= 0; i
< npatches
; ++i
) {
2002 if (ji
->type
== MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
&& !mono_gc_is_moving ()) {
2003 amodule
->shared_got
[i
] = NULL
;
2004 } else if (ji
->type
== MONO_PATCH_INFO_GC_NURSERY_START
&& !mono_gc_is_moving ()) {
2005 amodule
->shared_got
[i
] = NULL
;
2006 } else if (ji
->type
== MONO_PATCH_INFO_GC_NURSERY_BITS
&& !mono_gc_is_moving ()) {
2007 amodule
->shared_got
[i
] = NULL
;
2008 } else if (ji
->type
== MONO_PATCH_INFO_IMAGE
) {
2009 amodule
->shared_got
[i
] = amodule
->assembly
->image
;
2010 } else if (ji
->type
== MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
) {
2011 if (mono_defaults
.corlib
) {
2012 MonoAotModule
*mscorlib_amodule
= mono_defaults
.corlib
->aot_module
;
2014 if (mscorlib_amodule
)
2015 amodule
->shared_got
[i
] = mscorlib_amodule
->got
;
2017 amodule
->shared_got
[i
] = amodule
->got
;
2019 } else if (ji
->type
== MONO_PATCH_INFO_AOT_MODULE
) {
2020 amodule
->shared_got
[i
] = amodule
;
2022 amodule
->shared_got
[i
] = mono_resolve_patch_target (NULL
, mono_get_root_domain (), NULL
, ji
, FALSE
, error
);
2023 mono_error_assert_ok (error
);
2028 for (i
= 0; i
< npatches
; ++i
)
2029 amodule
->got
[i
] = amodule
->shared_got
[i
];
2031 if (amodule
->llvm_got
) {
2032 for (i
= 0; i
< npatches
; ++i
)
2033 amodule
->llvm_got
[i
] = amodule
->shared_got
[i
];
2036 mono_mempool_destroy (mp
);
2038 mono_memory_barrier ();
2039 amodule
->got_initialized
= GOT_INITIALIZED
;
2040 mono_loader_unlock ();
2044 load_aot_module (MonoAssembly
*assembly
, gpointer user_data
)
2046 char *aot_name
, *found_aot_name
;
2047 MonoAotModule
*amodule
;
2049 gboolean usable
= TRUE
;
2050 char *version_symbol
= NULL
;
2052 gpointer
*globals
= NULL
;
2053 MonoAotFileInfo
*info
= NULL
;
2055 gboolean do_load_image
= TRUE
;
2056 int align_double
, align_int64
;
2057 guint8
*aot_data
= NULL
;
2059 if (mono_compile_aot
)
2062 if (mono_aot_mode
== MONO_AOT_MODE_NONE
)
2065 if (assembly
->image
->aot_module
)
2067 * Already loaded. This can happen because the assembly loading code might invoke
2068 * the assembly load hooks multiple times for the same assembly.
2072 if (image_is_dynamic (assembly
->image
) || mono_asmctx_get_kind (&assembly
->context
) == MONO_ASMCTX_REFONLY
|| mono_domain_get () != mono_get_root_domain ())
2077 if (container_assm_name
&& !container_amodule
) {
2078 char *local_ref
= container_assm_name
;
2079 container_assm_name
= NULL
;
2080 MonoImageOpenStatus status
= MONO_IMAGE_OK
;
2081 MonoAssemblyOpenRequest req
;
2082 gchar
*dll
= g_strdup_printf ( "%s.dll", local_ref
);
2083 mono_assembly_request_prepare (&req
.request
, sizeof (req
), MONO_ASMCTX_DEFAULT
);
2084 MonoAssembly
*assm
= mono_assembly_request_open (dll
, &req
, &status
);
2086 gchar
*exe
= g_strdup_printf ("%s.exe", local_ref
);
2087 assm
= mono_assembly_request_open (exe
, &req
, &status
);
2090 load_aot_module (assm
, NULL
);
2091 container_amodule
= assm
->image
->aot_module
;
2094 if (static_aot_modules
)
2095 info
= (MonoAotFileInfo
*)g_hash_table_lookup (static_aot_modules
, assembly
->aname
.name
);
2101 found_aot_name
= NULL
;
2104 /* Statically linked AOT module */
2105 aot_name
= g_strdup_printf ("%s", assembly
->aname
.name
);
2106 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "Found statically linked AOT module '%s'.", aot_name
);
2107 if (!(info
->flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
)) {
2108 globals
= (void **)info
->globals
;
2111 found_aot_name
= g_strdup (aot_name
);
2115 if (enable_aot_cache
)
2116 sofile
= aot_cache_load_module (assembly
, &aot_name
);
2118 aot_name
= g_strdup_printf ("%s%s", assembly
->image
->name
, MONO_SOLIB_EXT
);
2120 sofile
= mono_dl_open (aot_name
, MONO_DL_LAZY
, &err
);
2122 found_aot_name
= g_strdup (aot_name
);
2124 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: image '%s' not found: %s", aot_name
, err
);
2129 #ifndef PLATFORM_ANDROID
2131 char *basename
= g_path_get_basename (assembly
->image
->name
);
2132 aot_name
= g_strdup_printf ("%s/mono/aot-cache/%s/%s%s", mono_assembly_getrootdir(), MONO_ARCHITECTURE
, basename
, MONO_SOLIB_EXT
);
2134 sofile
= mono_dl_open (aot_name
, MONO_DL_LAZY
, &err
);
2136 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: image '%s' not found: %s", aot_name
, err
);
2145 for (l
= mono_aot_paths
; l
; l
= l
->next
) {
2146 char *path
= (char*)l
->data
;
2148 char *basename
= g_path_get_basename (assembly
->image
->name
);
2149 aot_name
= g_strdup_printf ("%s/%s%s", path
, basename
, MONO_SOLIB_EXT
);
2150 sofile
= mono_dl_open (aot_name
, MONO_DL_LAZY
, &err
);
2152 found_aot_name
= g_strdup (aot_name
);
2154 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: image '%s' not found: %s", aot_name
, err
);
2164 if (mono_aot_only
&& !mono_use_interpreter
&& assembly
->image
->tables
[MONO_TABLE_METHOD
].rows
) {
2165 aot_name
= g_strdup_printf ("%s%s", assembly
->image
->name
, MONO_SOLIB_EXT
);
2166 g_error ("Failed to load AOT module '%s' in aot-only mode.\n", aot_name
);
2174 find_symbol (sofile
, globals
, "mono_aot_version", (gpointer
*) &version_symbol
);
2175 find_symbol (sofile
, globals
, "mono_aot_file_info", (gpointer
*)&info
);
2178 // Copy aotid to MonoImage
2179 memcpy(&assembly
->image
->aotid
, info
->aotid
, 16);
2181 if (version_symbol
) {
2182 /* Old file format */
2183 version
= atoi (version_symbol
);
2186 version
= info
->version
;
2189 if (version
!= MONO_AOT_FILE_VERSION
) {
2190 msg
= g_strdup_printf ("wrong file format version (expected %d got %d)", MONO_AOT_FILE_VERSION
, version
);
2196 if (info
->flags
& MONO_AOT_FILE_FLAG_SEPARATE_DATA
) {
2197 aot_data
= open_aot_data (assembly
, info
, &handle
);
2199 blob
= aot_data
+ info
->table_offsets
[MONO_AOT_TABLE_BLOB
];
2201 blob
= (guint8
*)info
->blob
;
2204 usable
= check_usable (assembly
, info
, blob
, &msg
);
2208 if (mono_aot_only
) {
2209 g_error ("Failed to load AOT module '%s' while running in aot-only mode: %s.\n", found_aot_name
, msg
);
2211 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: module %s is unusable: %s.", found_aot_name
, msg
);
2214 g_free (found_aot_name
);
2216 mono_dl_close (sofile
);
2217 assembly
->image
->aot_module
= NULL
;
2222 align_double
= MONO_ABI_ALIGNOF (double);
2223 align_int64
= MONO_ABI_ALIGNOF (gint64
);
2224 int card_table_shift_bits
= 0;
2225 gpointer card_table_mask
= NULL
;
2226 mono_gc_get_card_table (&card_table_shift_bits
, &card_table_mask
);
2228 g_assert (info
->double_align
== align_double
);
2229 g_assert (info
->long_align
== align_int64
);
2230 g_assert (info
->generic_tramp_num
== MONO_TRAMPOLINE_NUM
);
2231 g_assert (info
->card_table_shift_bits
== card_table_shift_bits
);
2232 g_assert (info
->card_table_mask
== GPOINTER_TO_UINT (card_table_mask
));
2234 amodule
= g_new0 (MonoAotModule
, 1);
2235 amodule
->aot_name
= found_aot_name
;
2236 amodule
->assembly
= assembly
;
2238 memcpy (&amodule
->info
, info
, sizeof (*info
));
2240 amodule
->got
= (void **)amodule
->info
.jit_got
;
2241 amodule
->llvm_got
= (void **)amodule
->info
.llvm_got
;
2242 amodule
->globals
= globals
;
2243 amodule
->sofile
= sofile
;
2244 amodule
->method_to_code
= g_hash_table_new (mono_aligned_addr_hash
, NULL
);
2245 amodule
->extra_methods
= g_hash_table_new (NULL
, NULL
);
2246 amodule
->shared_got
= g_new0 (gpointer
, info
->nshared_got_entries
);
2248 if (info
->flags
& MONO_AOT_FILE_FLAG_SEPARATE_DATA
) {
2249 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
2250 amodule
->tables
[i
] = aot_data
+ info
->table_offsets
[i
];
2253 mono_os_mutex_init_recursive (&amodule
->mutex
);
2255 /* Read image table */
2257 guint32 table_len
, i
;
2260 if (info
->flags
& MONO_AOT_FILE_FLAG_SEPARATE_DATA
)
2261 table
= (char *)amodule
->tables
[MONO_AOT_TABLE_IMAGE_TABLE
];
2263 table
= (char *)info
->image_table
;
2266 table_len
= *(guint32
*)table
;
2267 table
+= sizeof (guint32
);
2268 amodule
->image_table
= g_new0 (MonoImage
*, table_len
);
2269 amodule
->image_names
= g_new0 (MonoAssemblyName
, table_len
);
2270 amodule
->image_guids
= g_new0 (char*, table_len
);
2271 amodule
->image_table_len
= table_len
;
2272 for (i
= 0; i
< table_len
; ++i
) {
2273 MonoAssemblyName
*aname
= &(amodule
->image_names
[i
]);
2275 aname
->name
= g_strdup (table
);
2276 table
+= strlen (table
) + 1;
2277 amodule
->image_guids
[i
] = g_strdup (table
);
2278 table
+= strlen (table
) + 1;
2280 aname
->culture
= g_strdup (table
);
2281 table
+= strlen (table
) + 1;
2282 memcpy (aname
->public_key_token
, table
, strlen (table
) + 1);
2283 table
+= strlen (table
) + 1;
2285 table
= (char *)ALIGN_PTR_TO (table
, 8);
2286 aname
->flags
= *(guint32
*)table
;
2288 aname
->major
= *(guint32
*)table
;
2290 aname
->minor
= *(guint32
*)table
;
2292 aname
->build
= *(guint32
*)table
;
2294 aname
->revision
= *(guint32
*)table
;
2299 amodule
->jit_code_start
= (guint8
*)info
->jit_code_start
;
2300 amodule
->jit_code_end
= (guint8
*)info
->jit_code_end
;
2301 if (info
->flags
& MONO_AOT_FILE_FLAG_SEPARATE_DATA
) {
2302 amodule
->blob
= (guint8
*)amodule
->tables
[MONO_AOT_TABLE_BLOB
];
2303 amodule
->method_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_METHOD_INFO_OFFSETS
];
2304 amodule
->ex_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_EX_INFO_OFFSETS
];
2305 amodule
->class_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_CLASS_INFO_OFFSETS
];
2306 amodule
->class_name_table
= (guint16
*)amodule
->tables
[MONO_AOT_TABLE_CLASS_NAME
];
2307 amodule
->extra_method_table
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_EXTRA_METHOD_TABLE
];
2308 amodule
->extra_method_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS
];
2309 amodule
->got_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_GOT_INFO_OFFSETS
];
2310 amodule
->llvm_got_info_offsets
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_LLVM_GOT_INFO_OFFSETS
];
2311 amodule
->weak_field_indexes
= (guint32
*)amodule
->tables
[MONO_AOT_TABLE_WEAK_FIELD_INDEXES
];
2313 amodule
->blob
= (guint8
*)info
->blob
;
2314 amodule
->method_info_offsets
= (guint32
*)info
->method_info_offsets
;
2315 amodule
->ex_info_offsets
= (guint32
*)info
->ex_info_offsets
;
2316 amodule
->class_info_offsets
= (guint32
*)info
->class_info_offsets
;
2317 amodule
->class_name_table
= (guint16
*)info
->class_name_table
;
2318 amodule
->extra_method_table
= (guint32
*)info
->extra_method_table
;
2319 amodule
->extra_method_info_offsets
= (guint32
*)info
->extra_method_info_offsets
;
2320 amodule
->got_info_offsets
= (guint32
*)info
->got_info_offsets
;
2321 amodule
->llvm_got_info_offsets
= (guint32
*)info
->llvm_got_info_offsets
;
2322 amodule
->weak_field_indexes
= (guint32
*)info
->weak_field_indexes
;
2324 amodule
->unbox_trampolines
= (guint32
*)info
->unbox_trampolines
;
2325 amodule
->unbox_trampolines_end
= (guint32
*)info
->unbox_trampolines_end
;
2326 amodule
->unbox_trampoline_addresses
= (guint32
*)info
->unbox_trampoline_addresses
;
2327 amodule
->unwind_info
= (guint8
*)info
->unwind_info
;
2328 amodule
->mem_begin
= (guint8
*)amodule
->jit_code_start
;
2329 amodule
->mem_end
= (guint8
*)info
->mem_end
;
2330 amodule
->plt
= (guint8
*)info
->plt
;
2331 amodule
->plt_end
= (guint8
*)info
->plt_end
;
2332 amodule
->mono_eh_frame
= (guint8
*)info
->mono_eh_frame
;
2333 amodule
->trampolines
[MONO_AOT_TRAMP_SPECIFIC
] = (guint8
*)info
->specific_trampolines
;
2334 amodule
->trampolines
[MONO_AOT_TRAMP_STATIC_RGCTX
] = (guint8
*)info
->static_rgctx_trampolines
;
2335 amodule
->trampolines
[MONO_AOT_TRAMP_IMT
] = (guint8
*)info
->imt_trampolines
;
2336 amodule
->trampolines
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = (guint8
*)info
->gsharedvt_arg_trampolines
;
2337 amodule
->trampolines
[MONO_AOT_TRAMP_FTNPTR_ARG
] = (guint8
*)info
->ftnptr_arg_trampolines
;
2338 amodule
->trampolines
[MONO_AOT_TRAMP_UNBOX_ARBITRARY
] = (guint8
*)info
->unbox_arbitrary_trampolines
;
2340 if (mono_is_corlib_image (assembly
->image
))
2341 mscorlib_aot_module
= amodule
;
2343 /* Compute method addresses */
2344 amodule
->methods
= (void **)g_malloc0 (amodule
->info
.nmethods
* sizeof (gpointer
));
2345 for (i
= 0; i
< amodule
->info
.nmethods
; ++i
) {
2348 if (amodule
->info
.llvm_get_method
) {
2349 gpointer (*get_method
) (int) = (gpointer (*)(int))amodule
->info
.llvm_get_method
;
2351 addr
= get_method (i
);
2354 /* method_addresses () contains a table of branches, since the ios linker can update those correctly */
2355 if (!addr
&& amodule
->info
.method_addresses
) {
2356 addr
= get_call_table_entry (amodule
->info
.method_addresses
, i
);
2358 if (addr
== amodule
->info
.method_addresses
)
2362 amodule
->methods
[i
] = GINT_TO_POINTER (-1);
2364 amodule
->methods
[i
] = addr
;
2367 if (make_unreadable
) {
2368 #ifndef TARGET_WIN32
2370 guint8
*page_start
, *page_end
;
2373 addr
= amodule
->mem_begin
;
2375 len
= amodule
->mem_end
- amodule
->mem_begin
;
2377 /* Round down in both directions to avoid modifying data which is not ours */
2378 page_start
= (guint8
*) (((gssize
) (addr
)) & ~ (mono_pagesize () - 1)) + mono_pagesize ();
2379 page_end
= (guint8
*) (((gssize
) (addr
+ len
)) & ~ (mono_pagesize () - 1));
2380 if (page_end
> page_start
) {
2381 err
= mono_mprotect (page_start
, (page_end
- page_start
), MONO_MMAP_NONE
);
2382 g_assert (err
== 0);
2387 /* Compute the boundaries of LLVM code */
2388 if (info
->flags
& MONO_AOT_FILE_FLAG_WITH_LLVM
)
2389 compute_llvm_code_range (amodule
, &amodule
->llvm_code_start
, &amodule
->llvm_code_end
);
2393 if (amodule
->jit_code_start
) {
2394 aot_code_low_addr
= MIN (aot_code_low_addr
, (gsize
)amodule
->jit_code_start
);
2395 aot_code_high_addr
= MAX (aot_code_high_addr
, (gsize
)amodule
->jit_code_end
);
2397 if (amodule
->llvm_code_start
) {
2398 aot_code_low_addr
= MIN (aot_code_low_addr
, (gsize
)amodule
->llvm_code_start
);
2399 aot_code_high_addr
= MAX (aot_code_high_addr
, (gsize
)amodule
->llvm_code_end
);
2402 g_hash_table_insert (aot_modules
, assembly
, amodule
);
2405 if (amodule
->jit_code_start
)
2406 mono_jit_info_add_aot_module (assembly
->image
, amodule
->jit_code_start
, amodule
->jit_code_end
);
2407 if (amodule
->llvm_code_start
)
2408 mono_jit_info_add_aot_module (assembly
->image
, amodule
->llvm_code_start
, amodule
->llvm_code_end
);
2410 assembly
->image
->aot_module
= amodule
;
2412 if (mono_aot_only
&& !mono_llvm_only
) {
2414 find_amodule_symbol (amodule
, "specific_trampolines_page", (gpointer
*)&code
);
2415 amodule
->use_page_trampolines
= code
!= NULL
;
2416 /*g_warning ("using page trampolines: %d", amodule->use_page_trampolines);*/
2420 * Register the plt region as a single trampoline so we can unwind from this code
2422 mono_aot_tramp_info_register (
2423 mono_tramp_info_create (
2426 amodule
->plt_end
- amodule
->plt
,
2428 mono_unwind_get_cie_program ()
2434 * Since we store methoddef and classdef tokens when referring to methods/classes in
2435 * referenced assemblies, we depend on the exact versions of the referenced assemblies.
2436 * MS calls this 'hard binding'. This means we have to load all referenced assemblies
2437 * non-lazily, since we can't handle out-of-date errors later.
2438 * The cached class info also depends on the exact assemblies.
2440 if (do_load_image
) {
2441 for (i
= 0; i
< amodule
->image_table_len
; ++i
) {
2443 load_image (amodule
, i
, error
);
2444 mono_error_cleanup (error
); /* FIXME don't swallow the error */
2448 if (amodule
->out_of_date
) {
2449 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: Module %s is unusable because a dependency is out-of-date.", assembly
->image
->name
);
2451 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
);
2453 mono_trace (G_LOG_LEVEL_INFO
, MONO_TRACE_AOT
, "AOT: image '%s' found.", found_aot_name
);
2458 * mono_aot_register_module:
2460 * This should be called by embedding code to register normal AOT modules statically linked
2461 * into the executable.
2463 * \param aot_info the value of the 'mono_aot_module_<ASSEMBLY_NAME>_info' global symbol from the AOT module.
2466 mono_aot_register_module (gpointer
*aot_info
)
2470 MonoAotFileInfo
*info
= (MonoAotFileInfo
*)aot_info
;
2472 g_assert (info
->version
== MONO_AOT_FILE_VERSION
);
2474 if (!(info
->flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
)) {
2475 globals
= (void **)info
->globals
;
2479 aname
= (char *)info
->assembly_name
;
2481 /* This could be called before startup */
2485 if (!static_aot_modules
)
2486 static_aot_modules
= g_hash_table_new (g_str_hash
, g_str_equal
);
2488 g_hash_table_insert (static_aot_modules
, aname
, info
);
2490 if (info
->flags
& MONO_AOT_FILE_FLAG_EAGER_LOAD
) {
2491 g_assert (!container_assm_name
);
2492 container_assm_name
= aname
;
2500 mono_aot_init (void)
2502 mono_os_mutex_init_recursive (&aot_mutex
);
2503 mono_os_mutex_init_recursive (&aot_page_mutex
);
2504 aot_modules
= g_hash_table_new (NULL
, NULL
);
2506 mono_install_assembly_load_hook (load_aot_module
, NULL
);
2507 mono_counters_register ("Async JIT info size", MONO_COUNTER_INT
|MONO_COUNTER_JIT
, &async_jit_info_size
);
2509 char *lastaot
= g_getenv ("MONO_LASTAOT");
2511 mono_last_aot_method
= atoi (lastaot
);
2518 mono_aot_cleanup (void)
2520 g_hash_table_destroy (aot_jit_icall_hash
);
2521 g_hash_table_destroy (aot_modules
);
2525 decode_cached_class_info (MonoAotModule
*module
, MonoCachedClassInfo
*info
, guint8
*buf
, guint8
**endbuf
)
2532 info
->vtable_size
= decode_value (buf
, &buf
);
2533 if (info
->vtable_size
== -1)
2536 flags
= decode_value (buf
, &buf
);
2537 info
->ghcimpl
= (flags
>> 0) & 0x1;
2538 info
->has_finalize
= (flags
>> 1) & 0x1;
2539 info
->has_cctor
= (flags
>> 2) & 0x1;
2540 info
->has_nested_classes
= (flags
>> 3) & 0x1;
2541 info
->blittable
= (flags
>> 4) & 0x1;
2542 info
->has_references
= (flags
>> 5) & 0x1;
2543 info
->has_static_refs
= (flags
>> 6) & 0x1;
2544 info
->no_special_static_fields
= (flags
>> 7) & 0x1;
2545 info
->is_generic_container
= (flags
>> 8) & 0x1;
2546 info
->has_weak_fields
= (flags
>> 9) & 0x1;
2548 if (info
->has_cctor
) {
2549 res
= decode_method_ref (module
, &ref
, buf
, &buf
, error
);
2550 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
2553 info
->cctor_token
= ref
.token
;
2555 if (info
->has_finalize
) {
2556 res
= decode_method_ref (module
, &ref
, buf
, &buf
, error
);
2557 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
2560 info
->finalize_image
= ref
.image
;
2561 info
->finalize_token
= ref
.token
;
2564 info
->instance_size
= decode_value (buf
, &buf
);
2565 info
->class_size
= decode_value (buf
, &buf
);
2566 info
->packing_size
= decode_value (buf
, &buf
);
2567 info
->min_align
= decode_value (buf
, &buf
);
2575 mono_aot_get_method_from_vt_slot (MonoDomain
*domain
, MonoVTable
*vtable
, int slot
, MonoError
*error
)
2578 MonoClass
*klass
= vtable
->klass
;
2579 MonoAotModule
*amodule
= m_class_get_image (klass
)->aot_module
;
2581 MonoCachedClassInfo class_info
;
2586 ERROR_DECL (inner_error
);
2590 if (MONO_CLASS_IS_INTERFACE_INTERNAL (klass
) || m_class_get_rank (klass
) || !amodule
)
2593 info
= &amodule
->blob
[mono_aot_get_offset (amodule
->class_info_offsets
, mono_metadata_token_index (m_class_get_type_token (klass
)) - 1)];
2596 err
= decode_cached_class_info (amodule
, &class_info
, p
, &p
);
2600 for (i
= 0; i
< slot
; ++i
) {
2601 decode_method_ref (amodule
, &ref
, p
, &p
, inner_error
);
2602 mono_error_cleanup (inner_error
); /* FIXME don't swallow the error */
2605 res
= decode_method_ref (amodule
, &ref
, p
, &p
, inner_error
);
2606 mono_error_cleanup (inner_error
); /* FIXME don't swallow the error */
2609 if (ref
.no_aot_trampoline
)
2612 if (mono_metadata_token_index (ref
.token
) == 0 || mono_metadata_token_table (ref
.token
) != MONO_TABLE_METHOD
)
2615 addr
= mono_aot_get_method_from_token (domain
, ref
.image
, ref
.token
, error
);
2620 mono_aot_get_cached_class_info (MonoClass
*klass
, MonoCachedClassInfo
*res
)
2622 MonoAotModule
*amodule
= m_class_get_image (klass
)->aot_module
;
2626 if (m_class_get_rank (klass
) || !m_class_get_type_token (klass
) || !amodule
)
2629 p
= (guint8
*)&amodule
->blob
[mono_aot_get_offset (amodule
->class_info_offsets
, mono_metadata_token_index (m_class_get_type_token (klass
)) - 1)];
2631 err
= decode_cached_class_info (amodule
, res
, p
, &p
);
2639 * mono_aot_get_class_from_name:
2641 * Obtains a MonoClass with a given namespace and a given name which is located in IMAGE,
2642 * using a cache stored in the AOT file.
2643 * Stores the resulting class in *KLASS if found, stores NULL otherwise.
2645 * Returns: TRUE if the klass was found/not found in the cache, FALSE if no aot file was
2649 mono_aot_get_class_from_name (MonoImage
*image
, const char *name_space
, const char *name
, MonoClass
**klass
)
2651 MonoAotModule
*amodule
= image
->aot_module
;
2652 guint16
*table
, *entry
;
2655 char full_name_buf
[1024];
2657 const char *name2
, *name_space2
;
2659 guint32 cols
[MONO_TYPEDEF_SIZE
];
2660 GHashTable
*nspace_table
;
2662 if (!amodule
|| !amodule
->class_name_table
)
2665 amodule_lock (amodule
);
2669 /* First look in the cache */
2670 if (!amodule
->name_cache
)
2671 amodule
->name_cache
= g_hash_table_new (g_str_hash
, g_str_equal
);
2672 nspace_table
= (GHashTable
*)g_hash_table_lookup (amodule
->name_cache
, name_space
);
2674 *klass
= (MonoClass
*)g_hash_table_lookup (nspace_table
, name
);
2676 amodule_unlock (amodule
);
2681 table_size
= amodule
->class_name_table
[0];
2682 table
= amodule
->class_name_table
+ 1;
2684 if (name_space
[0] == '\0')
2685 full_name
= g_strdup_printf ("%s", name
);
2687 if (strlen (name_space
) + strlen (name
) < 1000) {
2688 sprintf (full_name_buf
, "%s.%s", name_space
, name
);
2689 full_name
= full_name_buf
;
2691 full_name
= g_strdup_printf ("%s.%s", name_space
, name
);
2694 hash
= mono_metadata_str_hash (full_name
) % table_size
;
2695 if (full_name
!= full_name_buf
)
2698 entry
= &table
[hash
* 2];
2700 if (entry
[0] != 0) {
2701 t
= &image
->tables
[MONO_TABLE_TYPEDEF
];
2704 guint32 index
= entry
[0];
2705 guint32 next
= entry
[1];
2706 guint32 token
= mono_metadata_make_token (MONO_TABLE_TYPEDEF
, index
);
2708 name_table_accesses
++;
2710 mono_metadata_decode_row (t
, index
- 1, cols
, MONO_TYPEDEF_SIZE
);
2712 name2
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAME
]);
2713 name_space2
= mono_metadata_string_heap (image
, cols
[MONO_TYPEDEF_NAMESPACE
]);
2715 if (!strcmp (name
, name2
) && !strcmp (name_space
, name_space2
)) {
2717 amodule_unlock (amodule
);
2718 *klass
= mono_class_get_checked (image
, token
, error
);
2719 if (!mono_error_ok (error
))
2720 mono_error_cleanup (error
); /* FIXME don't swallow the error */
2724 amodule_lock (amodule
);
2725 nspace_table
= (GHashTable
*)g_hash_table_lookup (amodule
->name_cache
, name_space
);
2726 if (!nspace_table
) {
2727 nspace_table
= g_hash_table_new (g_str_hash
, g_str_equal
);
2728 g_hash_table_insert (amodule
->name_cache
, (char*)name_space2
, nspace_table
);
2730 g_hash_table_insert (nspace_table
, (char*)name2
, *klass
);
2731 amodule_unlock (amodule
);
2737 entry
= &table
[next
* 2];
2744 amodule_unlock (amodule
);
2750 mono_aot_get_weak_field_indexes (MonoImage
*image
)
2752 MonoAotModule
*amodule
= image
->aot_module
;
2757 /* Initialize weak field indexes from the cached copy */
2758 guint32
*indexes
= (guint32
*)amodule
->weak_field_indexes
;
2759 int len
= indexes
[0];
2760 GHashTable
*indexes_hash
= g_hash_table_new (NULL
, NULL
);
2761 for (int i
= 0; i
< len
; ++i
)
2762 g_hash_table_insert (indexes_hash
, GUINT_TO_POINTER (indexes
[i
+ 1]), GUINT_TO_POINTER (1));
2763 return indexes_hash
;
2766 /* Compute the boundaries of the LLVM code for AMODULE. */
2768 compute_llvm_code_range (MonoAotModule
*amodule
, guint8
**code_start
, guint8
**code_end
)
2771 int version
, fde_count
;
2774 if (amodule
->info
.llvm_get_method
) {
2775 gpointer (*get_method
) (int) = (gpointer (*)(int))amodule
->info
.llvm_get_method
;
2778 gsize min
= 1 << 30, max
= 0;
2781 // FIXME: This depends on emscripten allocating ftnptr ids sequentially
2782 for (int i
= 0; i
< amodule
->info
.nmethods
; ++i
) {
2785 addr
= get_method (i
);
2786 gsize val
= (gsize
)addr
;
2788 g_assert (val
> prev
);
2797 *code_start
= (guint8
*)min
;
2798 *code_end
= (guint8
*)(max
+ 1);
2804 *code_start
= (guint8
*)get_method (-1);
2805 *code_end
= (guint8
*)get_method (-2);
2807 g_assert (*code_end
> *code_start
);
2812 g_assert (amodule
->mono_eh_frame
);
2814 p
= amodule
->mono_eh_frame
;
2816 /* p points to data emitted by LLVM in DwarfException::EmitMonoEHFrame () */
2820 g_assert (version
== 3);
2823 p
= (guint8
*)ALIGN_PTR_TO (p
, 4);
2825 fde_count
= *(guint32
*)p
;
2829 if (fde_count
> 0) {
2830 *code_start
= (guint8
*)amodule
->methods
[table
[0]];
2831 *code_end
= (guint8
*)amodule
->methods
[table
[(fde_count
- 1) * 2]] + table
[fde_count
* 2];
2839 is_llvm_code (MonoAotModule
*amodule
, guint8
*code
)
2844 if ((guint8
*)code
>= amodule
->llvm_code_start
&& (guint8
*)code
< amodule
->llvm_code_end
)
2852 is_thumb_code (MonoAotModule
*amodule
, guint8
*code
)
2854 if (is_llvm_code (amodule
, code
) && (amodule
->info
.flags
& MONO_AOT_FILE_FLAG_LLVM_THUMB
))
2861 * decode_llvm_mono_eh_frame:
2863 * Decode the EH information emitted by our modified LLVM compiler and construct a
2864 * MonoJitInfo structure from it.
2865 * If JINFO is NULL, set OUT_LLVM_CLAUSES to the number of llvm level clauses.
2866 * This function is async safe when called in async context.
2869 decode_llvm_mono_eh_frame (MonoAotModule
*amodule
, MonoDomain
*domain
, MonoJitInfo
*jinfo
,
2870 guint8
*code
, guint32 code_len
,
2871 MonoJitExceptionInfo
*clauses
, int num_clauses
,
2873 int *this_reg
, int *this_offset
, int *out_llvm_clauses
)
2875 guint8
*p
, *code1
, *code2
;
2876 guint8
*fde
, *cie
, *code_start
, *code_end
;
2877 int version
, fde_count
;
2879 int i
, pos
, left
, right
;
2880 MonoJitExceptionInfo
*ei
;
2881 guint32 fde_len
, ei_len
, nested_len
, nindex
;
2882 gpointer
*type_info
;
2883 MonoLLVMFDEInfo info
;
2887 async
= mono_thread_info_is_async_context ();
2889 if (!amodule
->mono_eh_frame
) {
2891 *out_llvm_clauses
= num_clauses
;
2894 memcpy (jinfo
->clauses
, clauses
, num_clauses
* sizeof (MonoJitExceptionInfo
));
2898 g_assert (amodule
->mono_eh_frame
&& code
);
2900 p
= amodule
->mono_eh_frame
;
2902 /* p points to data emitted by LLVM in DwarfMonoException::EmitMonoEHFrame () */
2906 g_assert (version
== 3);
2908 /* func_encoding = *p; */
2910 p
= (guint8
*)ALIGN_PTR_TO (p
, 4);
2912 fde_count
= *(guint32
*)p
;
2916 /* There is +1 entry in the table */
2917 cie
= p
+ ((fde_count
+ 1) * 8);
2919 /* Binary search in the table to find the entry for code */
2923 pos
= (left
+ right
) / 2;
2925 /* The table contains method index/fde offset pairs */
2926 g_assert (table
[(pos
* 2)] != -1);
2927 code1
= (guint8
*)amodule
->methods
[table
[(pos
* 2)]];
2928 if (pos
+ 1 == fde_count
) {
2929 code2
= amodule
->llvm_code_end
;
2931 g_assert (table
[(pos
+ 1) * 2] != -1);
2932 code2
= (guint8
*)amodule
->methods
[table
[(pos
+ 1) * 2]];
2937 else if (code
>= code2
)
2943 code_start
= (guint8
*)amodule
->methods
[table
[(pos
* 2)]];
2944 if (pos
+ 1 == fde_count
) {
2945 /* The +1 entry in the table contains the length of the last method */
2946 int len
= table
[(pos
+ 1) * 2];
2947 code_end
= code_start
+ len
;
2949 code_end
= (guint8
*)amodule
->methods
[table
[(pos
+ 1) * 2]];
2952 code_len
= code_end
- code_start
;
2954 g_assert (code
>= code_start
&& code
< code_end
);
2956 if (is_thumb_code (amodule
, code_start
))
2957 /* Clear thumb flag */
2958 code_start
= (guint8
*)(((gsize
)code_start
) & ~1);
2960 fde
= amodule
->mono_eh_frame
+ table
[(pos
* 2) + 1];
2961 /* This won't overflow because there is +1 entry in the table */
2962 fde_len
= table
[(pos
* 2) + 2 + 1] - table
[(pos
* 2) + 1];
2964 /* Compute lengths */
2965 mono_unwind_decode_llvm_mono_fde (fde
, fde_len
, cie
, code_start
, &info
, NULL
, NULL
, NULL
);
2968 /* These are leaked, but the leak is bounded */
2969 ei
= mono_domain_alloc0_lock_free (domain
, info
.ex_info_len
* sizeof (MonoJitExceptionInfo
));
2970 type_info
= mono_domain_alloc0_lock_free (domain
, info
.ex_info_len
* sizeof (gpointer
));
2971 unw_info
= mono_domain_alloc0_lock_free (domain
, info
.unw_info_len
);
2973 ei
= (MonoJitExceptionInfo
*)g_malloc0 (info
.ex_info_len
* sizeof (MonoJitExceptionInfo
));
2974 type_info
= (gpointer
*)g_malloc0 (info
.ex_info_len
* sizeof (gpointer
));
2975 unw_info
= (guint8
*)g_malloc0 (info
.unw_info_len
);
2977 mono_unwind_decode_llvm_mono_fde (fde
, fde_len
, cie
, code_start
, &info
, ei
, type_info
, unw_info
);
2979 ei_len
= info
.ex_info_len
;
2980 *this_reg
= info
.this_reg
;
2981 *this_offset
= info
.this_offset
;
2984 * LLVM might represent one IL region with multiple regions.
2987 /* Count number of nested clauses */
2989 for (i
= 0; i
< ei_len
; ++i
) {
2990 /* This might be unaligned */
2991 gint32 cindex1
= read32 (type_info
[i
]);
2994 for (l
= nesting
[cindex1
]; l
; l
= l
->next
)
2999 *out_llvm_clauses
= ei_len
+ nested_len
;
3003 /* Store the unwind info addr/length in the MonoJitInfo structure itself so its async safe */
3004 MonoUnwindJitInfo
*jinfo_unwind
= mono_jit_info_get_unwind_info (jinfo
);
3005 g_assert (jinfo_unwind
);
3006 jinfo_unwind
->unw_info
= unw_info
;
3007 jinfo_unwind
->unw_info_len
= info
.unw_info_len
;
3009 for (i
= 0; i
< ei_len
; ++i
) {
3011 * clauses contains the original IL exception info saved by the AOT
3012 * compiler, we have to combine that with the information produced by LLVM
3014 /* The type_info entries contain IL clause indexes */
3015 int clause_index
= read32 (type_info
[i
]);
3016 MonoJitExceptionInfo
*jei
= &jinfo
->clauses
[i
];
3017 MonoJitExceptionInfo
*orig_jei
= &clauses
[clause_index
];
3019 g_assert (clause_index
< num_clauses
);
3020 jei
->flags
= orig_jei
->flags
;
3021 jei
->data
.catch_class
= orig_jei
->data
.catch_class
;
3023 jei
->try_start
= ei
[i
].try_start
;
3024 jei
->try_end
= ei
[i
].try_end
;
3025 jei
->handler_start
= ei
[i
].handler_start
;
3026 jei
->clause_index
= clause_index
;
3028 if (is_thumb_code (amodule
, (guint8
*)jei
->try_start
)) {
3029 jei
->try_start
= (void*)((gsize
)jei
->try_start
& ~1);
3030 jei
->try_end
= (void*)((gsize
)jei
->try_end
& ~1);
3031 /* Make sure we transition to thumb when a handler starts */
3032 jei
->handler_start
= (void*)((gsize
)jei
->handler_start
+ 1);
3036 /* See exception_cb () in mini-llvm.c as to why this is needed */
3038 for (i
= 0; i
< ei_len
; ++i
) {
3039 gint32 cindex1
= read32 (type_info
[i
]);
3042 for (l
= nesting
[cindex1
]; l
; l
= l
->next
) {
3043 gint32 nesting_cindex
= GPOINTER_TO_INT (l
->data
);
3044 MonoJitExceptionInfo
*nesting_ei
;
3045 MonoJitExceptionInfo
*nesting_clause
= &clauses
[nesting_cindex
];
3047 nesting_ei
= &jinfo
->clauses
[nindex
];
3050 memcpy (nesting_ei
, &jinfo
->clauses
[i
], sizeof (MonoJitExceptionInfo
));
3051 nesting_ei
->flags
= nesting_clause
->flags
;
3052 nesting_ei
->data
.catch_class
= nesting_clause
->data
.catch_class
;
3053 nesting_ei
->clause_index
= nesting_cindex
;
3056 g_assert (nindex
== ei_len
+ nested_len
);
3060 alloc0_jit_info_data (MonoDomain
*domain
, int size
, gboolean async_context
)
3062 #define alloc0_jit_info_data(domain, size, async_context) (g_cast (alloc0_jit_info_data ((domain), (size), (async_context))))
3067 if (async_context
) {
3068 res
= mono_domain_alloc0_lock_free (domain
, size
);
3069 mono_atomic_fetch_add_i32 (&async_jit_info_size
, size
);
3071 res
= mono_domain_alloc0 (domain
, size
);
3077 * LOCKING: Acquires the domain lock.
3078 * In async context, this is async safe.
3081 decode_exception_debug_info (MonoAotModule
*amodule
, MonoDomain
*domain
,
3082 MonoMethod
*method
, guint8
* ex_info
,
3083 guint8
*code
, guint32 code_len
)
3086 int i
, buf_len
, num_clauses
, len
;
3088 MonoJitInfoFlags flags
= JIT_INFO_NONE
;
3089 guint unwind_info
, eflags
;
3090 gboolean has_generic_jit_info
, has_dwarf_unwind_info
, has_clauses
, has_seq_points
, has_try_block_holes
, has_arch_eh_jit_info
;
3091 gboolean from_llvm
, has_gc_map
;
3093 int try_holes_info_size
, num_holes
;
3094 int this_reg
= 0, this_offset
= 0;
3097 /* Load the method info from the AOT file */
3098 async
= mono_thread_info_is_async_context ();
3101 eflags
= decode_value (p
, &p
);
3102 has_generic_jit_info
= (eflags
& 1) != 0;
3103 has_dwarf_unwind_info
= (eflags
& 2) != 0;
3104 has_clauses
= (eflags
& 4) != 0;
3105 has_seq_points
= (eflags
& 8) != 0;
3106 from_llvm
= (eflags
& 16) != 0;
3107 has_try_block_holes
= (eflags
& 32) != 0;
3108 has_gc_map
= (eflags
& 64) != 0;
3109 has_arch_eh_jit_info
= (eflags
& 128) != 0;
3111 if (has_dwarf_unwind_info
) {
3112 unwind_info
= decode_value (p
, &p
);
3113 g_assert (unwind_info
< (1 << 30));
3115 unwind_info
= decode_value (p
, &p
);
3117 if (has_generic_jit_info
)
3118 flags
|= JIT_INFO_HAS_GENERIC_JIT_INFO
;
3120 if (has_try_block_holes
) {
3121 num_holes
= decode_value (p
, &p
);
3122 flags
|= JIT_INFO_HAS_TRY_BLOCK_HOLES
;
3123 try_holes_info_size
= sizeof (MonoTryBlockHoleTableJitInfo
) + num_holes
* sizeof (MonoTryBlockHoleJitInfo
);
3125 num_holes
= try_holes_info_size
= 0;
3128 if (has_arch_eh_jit_info
) {
3129 flags
|= JIT_INFO_HAS_ARCH_EH_INFO
;
3130 /* Overwrite the original code_len which includes alignment padding */
3131 code_len
= decode_value (p
, &p
);
3134 /* Exception table */
3136 num_clauses
= decode_value (p
, &p
);
3141 MonoJitExceptionInfo
*clauses
;
3145 * Part of the info is encoded by the AOT compiler, the rest is in the .eh_frame
3149 if (num_clauses
< 16) {
3150 clauses
= g_newa (MonoJitExceptionInfo
, num_clauses
);
3151 nesting
= g_newa (GSList
*, num_clauses
);
3153 clauses
= alloc0_jit_info_data (domain
, sizeof (MonoJitExceptionInfo
) * num_clauses
, TRUE
);
3154 nesting
= alloc0_jit_info_data (domain
, sizeof (GSList
*) * num_clauses
, TRUE
);
3156 memset (clauses
, 0, sizeof (MonoJitExceptionInfo
) * num_clauses
);
3157 memset (nesting
, 0, sizeof (GSList
*) * num_clauses
);
3159 clauses
= g_new0 (MonoJitExceptionInfo
, num_clauses
);
3160 nesting
= g_new0 (GSList
*, num_clauses
);
3163 for (i
= 0; i
< num_clauses
; ++i
) {
3164 MonoJitExceptionInfo
*ei
= &clauses
[i
];
3166 ei
->flags
= decode_value (p
, &p
);
3168 if (!(ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
|| ei
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)) {
3169 int len
= decode_value (p
, &p
);
3175 ei
->data
.catch_class
= decode_klass_ref (amodule
, p
, &p
, error
);
3176 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3181 ei
->clause_index
= i
;
3183 ei
->try_offset
= decode_value (p
, &p
);
3184 ei
->try_len
= decode_value (p
, &p
);
3185 ei
->handler_offset
= decode_value (p
, &p
);
3186 ei
->handler_len
= decode_value (p
, &p
);
3188 /* Read the list of nesting clauses */
3190 int nesting_index
= decode_value (p
, &p
);
3191 if (nesting_index
== -1)
3195 nesting
[i
] = g_slist_prepend (nesting
[i
], GINT_TO_POINTER (nesting_index
));
3199 flags
|= JIT_INFO_HAS_UNWIND_INFO
;
3201 int num_llvm_clauses
;
3202 /* Get the length first */
3203 decode_llvm_mono_eh_frame (amodule
, domain
, NULL
, code
, code_len
, clauses
, num_clauses
, nesting
, &this_reg
, &this_offset
, &num_llvm_clauses
);
3204 len
= mono_jit_info_size (flags
, num_llvm_clauses
, num_holes
);
3205 jinfo
= (MonoJitInfo
*)alloc0_jit_info_data (domain
, len
, async
);
3206 mono_jit_info_init (jinfo
, method
, code
, code_len
, flags
, num_llvm_clauses
, num_holes
);
3208 decode_llvm_mono_eh_frame (amodule
, domain
, jinfo
, code
, code_len
, clauses
, num_clauses
, nesting
, &this_reg
, &this_offset
, NULL
);
3212 for (i
= 0; i
< num_clauses
; ++i
)
3213 g_slist_free (nesting
[i
]);
3216 jinfo
->from_llvm
= 1;
3218 len
= mono_jit_info_size (flags
, num_clauses
, num_holes
);
3219 jinfo
= (MonoJitInfo
*)alloc0_jit_info_data (domain
, len
, async
);
3220 mono_jit_info_init (jinfo
, method
, code
, code_len
, flags
, num_clauses
, num_holes
);
3222 for (i
= 0; i
< jinfo
->num_clauses
; ++i
) {
3223 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[i
];
3225 ei
->flags
= decode_value (p
, &p
);
3227 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
3228 /* Not used for catch clauses */
3229 if (ei
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
)
3230 ei
->exvar_offset
= decode_value (p
, &p
);
3232 ei
->exvar_offset
= decode_value (p
, &p
);
3235 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
|| ei
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
3236 ei
->data
.filter
= code
+ decode_value (p
, &p
);
3238 int len
= decode_value (p
, &p
);
3244 ei
->data
.catch_class
= decode_klass_ref (amodule
, p
, &p
, error
);
3245 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3250 ei
->try_start
= code
+ decode_value (p
, &p
);
3251 ei
->try_end
= code
+ decode_value (p
, &p
);
3252 ei
->handler_start
= code
+ decode_value (p
, &p
);
3255 jinfo
->unwind_info
= unwind_info
;
3256 jinfo
->domain_neutral
= 0;
3257 jinfo
->from_aot
= 1;
3260 if (has_try_block_holes
) {
3261 MonoTryBlockHoleTableJitInfo
*table
;
3263 g_assert (jinfo
->has_try_block_holes
);
3265 table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
3268 table
->num_holes
= (guint16
)num_holes
;
3269 for (i
= 0; i
< num_holes
; ++i
) {
3270 MonoTryBlockHoleJitInfo
*hole
= &table
->holes
[i
];
3271 hole
->clause
= decode_value (p
, &p
);
3272 hole
->length
= decode_value (p
, &p
);
3273 hole
->offset
= decode_value (p
, &p
);
3277 if (has_arch_eh_jit_info
) {
3278 MonoArchEHJitInfo
*eh_info
;
3280 g_assert (jinfo
->has_arch_eh_info
);
3282 eh_info
= mono_jit_info_get_arch_eh_info (jinfo
);
3283 eh_info
->stack_size
= decode_value (p
, &p
);
3284 eh_info
->epilog_size
= decode_value (p
, &p
);
3288 /* The rest is not needed in async mode */
3289 jinfo
->async
= TRUE
;
3290 jinfo
->d
.aot_info
= amodule
;
3295 if (has_generic_jit_info
) {
3296 MonoGenericJitInfo
*gi
;
3299 g_assert (jinfo
->has_generic_jit_info
);
3301 gi
= mono_jit_info_get_generic_jit_info (jinfo
);
3304 gi
->nlocs
= decode_value (p
, &p
);
3306 gi
->locations
= (MonoDwarfLocListEntry
*)alloc0_jit_info_data (domain
, gi
->nlocs
* sizeof (MonoDwarfLocListEntry
), async
);
3307 for (i
= 0; i
< gi
->nlocs
; ++i
) {
3308 MonoDwarfLocListEntry
*entry
= &gi
->locations
[i
];
3310 entry
->is_reg
= decode_value (p
, &p
);
3311 entry
->reg
= decode_value (p
, &p
);
3313 entry
->offset
= decode_value (p
, &p
);
3315 entry
->from
= decode_value (p
, &p
);
3316 entry
->to
= decode_value (p
, &p
);
3321 gi
->has_this
= this_reg
!= -1;
3322 gi
->this_reg
= this_reg
;
3323 gi
->this_offset
= this_offset
;
3325 gi
->has_this
= decode_value (p
, &p
);
3326 gi
->this_reg
= decode_value (p
, &p
);
3327 gi
->this_offset
= decode_value (p
, &p
);
3331 len
= decode_value (p
, &p
);
3335 jinfo
->d
.method
= decode_resolve_method_ref (amodule
, p
, &p
, error
);
3336 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3339 gi
->generic_sharing_context
= alloc0_jit_info_data (domain
, sizeof (MonoGenericSharingContext
), async
);
3340 if (decode_value (p
, &p
)) {
3342 MonoGenericSharingContext
*gsctx
= gi
->generic_sharing_context
;
3344 gsctx
->is_gsharedvt
= TRUE
;
3348 if (method
&& has_seq_points
) {
3349 MonoSeqPointInfo
*seq_points
;
3351 p
+= mono_seq_point_info_read (&seq_points
, p
, FALSE
);
3353 mono_domain_lock (domain
);
3354 /* This could be set already since this function can be called more than once for the same method */
3355 if (!g_hash_table_lookup (domain_jit_info (domain
)->seq_points
, method
))
3356 g_hash_table_insert (domain_jit_info (domain
)->seq_points
, method
, seq_points
);
3358 mono_seq_point_info_free (seq_points
);
3359 mono_domain_unlock (domain
);
3361 jinfo
->seq_points
= seq_points
;
3364 /* Load debug info */
3365 buf_len
= decode_value (p
, &p
);
3367 mono_debug_add_aot_method (domain
, method
, code
, p
, buf_len
);
3371 int map_size
= decode_value (p
, &p
);
3372 /* The GC map requires 4 bytes of alignment */
3373 while ((guint64
)(gsize
)p
% 4)
3379 if (amodule
!= m_class_get_image (jinfo
->d
.method
->klass
)->aot_module
) {
3382 ji_to_amodule
= g_hash_table_new (NULL
, NULL
);
3383 g_hash_table_insert (ji_to_amodule
, jinfo
, amodule
);
3391 amodule_contains_code_addr (MonoAotModule
*amodule
, guint8
*code
)
3393 return (code
>= amodule
->jit_code_start
&& code
<= amodule
->jit_code_end
) ||
3394 (code
>= amodule
->llvm_code_start
&& code
<= amodule
->llvm_code_end
);
3398 * mono_aot_get_unwind_info:
3400 * Return a pointer to the DWARF unwind info belonging to JI.
3403 mono_aot_get_unwind_info (MonoJitInfo
*ji
, guint32
*unwind_info_len
)
3405 MonoAotModule
*amodule
;
3407 guint8
*code
= (guint8
*)ji
->code_start
;
3410 amodule
= ji
->d
.aot_info
;
3412 amodule
= m_class_get_image (jinfo_get_method (ji
)->klass
)->aot_module
;
3414 g_assert (ji
->from_aot
);
3416 if (!amodule_contains_code_addr (amodule
, code
)) {
3417 /* ji belongs to a different aot module than amodule */
3419 g_assert (ji_to_amodule
);
3420 amodule
= (MonoAotModule
*)g_hash_table_lookup (ji_to_amodule
, ji
);
3422 g_assert (amodule_contains_code_addr (amodule
, code
));
3426 p
= amodule
->unwind_info
+ ji
->unwind_info
;
3427 *unwind_info_len
= decode_value (p
, &p
);
3432 msort_method_addresses_internal (gpointer
*array
, int *indexes
, int lo
, int hi
, gpointer
*scratch
, int *scratch_indexes
)
3434 int mid
= (lo
+ hi
) / 2;
3441 for (i
= lo
; i
< hi
; ++i
)
3442 if (array
[i
] > array
[i
+ 1])
3445 /* Already sorted */
3449 msort_method_addresses_internal (array
, indexes
, lo
, mid
, scratch
, scratch_indexes
);
3450 msort_method_addresses_internal (array
, indexes
, mid
+ 1, hi
, scratch
, scratch_indexes
);
3452 if (array
[mid
] < array
[mid
+ 1])
3458 for (i
= lo
; i
<= hi
; i
++) {
3459 if (t_lo
<= mid
&& ((t_hi
> hi
) || array
[t_lo
] < array
[t_hi
])) {
3460 scratch
[i
] = array
[t_lo
];
3461 scratch_indexes
[i
] = indexes
[t_lo
];
3464 scratch
[i
] = array
[t_hi
];
3465 scratch_indexes
[i
] = indexes
[t_hi
];
3469 for (i
= lo
; i
<= hi
; ++i
) {
3470 array
[i
] = scratch
[i
];
3471 indexes
[i
] = scratch_indexes
[i
];
3476 msort_method_addresses (gpointer
*array
, int *indexes
, int len
)
3479 int *scratch_indexes
;
3481 scratch
= g_new (gpointer
, len
);
3482 scratch_indexes
= g_new (int, len
);
3483 msort_method_addresses_internal (array
, indexes
, 0, len
- 1, scratch
, scratch_indexes
);
3485 g_free (scratch_indexes
);
3489 * mono_aot_find_jit_info:
3491 * In async context, the resulting MonoJitInfo will not have its method field set, and it will not be added
3492 * to the jit info tables.
3493 * FIXME: Large sizes in the lock free allocator
3496 mono_aot_find_jit_info (MonoDomain
*domain
, MonoImage
*image
, gpointer addr
)
3499 int pos
, left
, right
, code_len
;
3500 int method_index
, table_len
;
3502 MonoAotModule
*amodule
= image
->aot_module
;
3503 MonoMethod
*method
= NULL
;
3505 guint8
*code
, *ex_info
, *p
;
3509 guint8
*code1
, *code2
;
3516 nmethods
= amodule
->info
.nmethods
;
3518 if (domain
!= mono_get_root_domain ())
3522 if (!amodule_contains_code_addr (amodule
, (guint8
*)addr
))
3525 async
= mono_thread_info_is_async_context ();
3527 /* Compute a sorted table mapping code to method indexes. */
3528 if (!amodule
->sorted_methods
) {
3530 gpointer
*methods
= g_new0 (gpointer
, nmethods
);
3531 int *method_indexes
= g_new0 (int, nmethods
);
3532 int methods_len
= 0;
3534 for (i
= 0; i
< nmethods
; ++i
) {
3535 /* Skip the -1 entries to speed up sorting */
3536 if (amodule
->methods
[i
] == GINT_TO_POINTER (-1))
3538 methods
[methods_len
] = amodule
->methods
[i
];
3539 method_indexes
[methods_len
] = i
;
3542 /* Use a merge sort as this is mostly sorted */
3543 msort_method_addresses (methods
, method_indexes
, methods_len
);
3544 for (i
= 0; i
< methods_len
-1; ++i
)
3545 g_assert (methods
[i
] <= methods
[i
+ 1]);
3546 amodule
->sorted_methods_len
= methods_len
;
3547 if (mono_atomic_cas_ptr ((gpointer
*)&amodule
->sorted_methods
, methods
, NULL
) != NULL
)
3548 /* Somebody got in before us */
3550 if (mono_atomic_cas_ptr ((gpointer
*)&amodule
->sorted_method_indexes
, method_indexes
, NULL
) != NULL
)
3551 /* Somebody got in before us */
3552 g_free (method_indexes
);
3555 /* Binary search in the sorted_methods table */
3556 methods
= amodule
->sorted_methods
;
3557 methods_len
= amodule
->sorted_methods_len
;
3558 code
= (guint8
*)addr
;
3560 right
= methods_len
;
3562 pos
= (left
+ right
) / 2;
3564 code1
= (guint8
*)methods
[pos
];
3565 if (pos
+ 1 == methods_len
) {
3566 if (code1
>= amodule
->jit_code_start
&& code1
< amodule
->jit_code_end
)
3567 code2
= amodule
->jit_code_end
;
3569 code2
= amodule
->llvm_code_end
;
3571 code2
= (guint8
*)methods
[pos
+ 1];
3576 else if (code
>= code2
)
3582 g_assert (addr
>= methods
[pos
]);
3583 if (pos
+ 1 < methods_len
)
3584 g_assert (addr
< methods
[pos
+ 1]);
3585 method_index
= amodule
->sorted_method_indexes
[pos
];
3587 /* In async mode, jinfo is not added to the normal jit info table, so have to cache it ourselves */
3589 JitInfoMap
*table
= amodule
->async_jit_info_table
;
3593 len
= table
[0].method_index
;
3594 for (i
= 1; i
< len
; ++i
) {
3595 if (table
[i
].method_index
== method_index
)
3596 return table
[i
].jinfo
;
3601 code
= (guint8
*)amodule
->methods
[method_index
];
3602 ex_info
= &amodule
->blob
[mono_aot_get_offset (amodule
->ex_info_offsets
, method_index
)];
3604 if (pos
== methods_len
- 1) {
3605 if (code
>= amodule
->jit_code_start
&& code
< amodule
->jit_code_end
)
3606 code_len
= amodule
->jit_code_end
- code
;
3608 code_len
= amodule
->llvm_code_end
- code
;
3610 code_len
= (guint8
*)methods
[pos
+ 1] - (guint8
*)methods
[pos
];
3613 g_assert ((guint8
*)code
<= (guint8
*)addr
&& (guint8
*)addr
< (guint8
*)code
+ code_len
);
3615 /* Might be a wrapper/extra method */
3617 if (amodule
->extra_methods
) {
3618 amodule_lock (amodule
);
3619 method
= (MonoMethod
*)g_hash_table_lookup (amodule
->extra_methods
, GUINT_TO_POINTER (method_index
));
3620 amodule_unlock (amodule
);
3626 if (method_index
>= image
->tables
[MONO_TABLE_METHOD
].rows
) {
3628 * This is hit for extra methods which are called directly, so they are
3629 * not in amodule->extra_methods.
3631 table_len
= amodule
->extra_method_info_offsets
[0];
3632 table
= amodule
->extra_method_info_offsets
+ 1;
3639 pos
= ((left
+ right
) / 2);
3641 g_assert (pos
< table_len
);
3643 if (table
[pos
* 2] < method_index
)
3645 else if (table
[pos
* 2] > method_index
)
3651 p
= amodule
->blob
+ table
[(pos
* 2) + 1];
3652 method
= decode_resolve_method_ref (amodule
, p
, &p
, error
);
3653 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3655 /* Happens when a random address is passed in which matches a not-yey called wrapper encoded using its name */
3659 token
= mono_metadata_make_token (MONO_TABLE_METHOD
, method_index
+ 1);
3660 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
3662 g_error ("AOT runtime could not load method due to %s", mono_error_get_message (error
)); /* FIXME don't swallow the error */
3669 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
3671 jinfo
= decode_exception_debug_info (amodule
, domain
, method
, ex_info
, code
, code_len
);
3673 g_assert ((guint8
*)addr
>= (guint8
*)jinfo
->code_start
);
3675 /* Add it to the normal JitInfo tables */
3677 JitInfoMap
*old_table
, *new_table
;
3681 * Use a simple inmutable table with linear search to cache async jit info entries.
3682 * This assumes that the number of entries is small.
3685 /* Copy the table, adding a new entry at the end */
3686 old_table
= amodule
->async_jit_info_table
;
3688 len
= old_table
[0].method_index
;
3691 new_table
= (JitInfoMap
*)alloc0_jit_info_data (domain
, (len
+ 1) * sizeof (JitInfoMap
), async
);
3693 memcpy (new_table
, old_table
, len
* sizeof (JitInfoMap
));
3694 new_table
[0].method_index
= len
+ 1;
3695 new_table
[len
].method_index
= method_index
;
3696 new_table
[len
].jinfo
= jinfo
;
3698 mono_memory_barrier ();
3699 if (mono_atomic_cas_ptr ((volatile gpointer
*)&amodule
->async_jit_info_table
, new_table
, old_table
) == old_table
)
3703 mono_jit_info_table_add (domain
, jinfo
);
3706 if ((guint8
*)addr
>= (guint8
*)jinfo
->code_start
+ jinfo
->code_size
)
3707 /* addr is in the padding between methods, see the adjustment of code_size in decode_exception_debug_info () */
3714 decode_patch (MonoAotModule
*aot_module
, MonoMemPool
*mp
, MonoJumpInfo
*ji
, guint8
*buf
, guint8
**endbuf
)
3723 case MONO_PATCH_INFO_METHOD
:
3724 case MONO_PATCH_INFO_METHOD_JUMP
:
3725 case MONO_PATCH_INFO_METHOD_FTNDESC
:
3726 case MONO_PATCH_INFO_ICALL_ADDR
:
3727 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
3728 case MONO_PATCH_INFO_METHOD_RGCTX
:
3729 case MONO_PATCH_INFO_METHOD_CODE_SLOT
: {
3733 res
= decode_method_ref (aot_module
, &ref
, p
, &p
, error
);
3734 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3738 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
)) {
3739 ji
->data
.target
= mono_create_ftnptr (mono_domain_get (), mono_create_jit_trampoline_from_token (ref
.image
, ref
.token
));
3740 ji
->type
= MONO_PATCH_INFO_ABS
;
3744 ji
->data
.method
= ref
.method
;
3747 ji
->data
.method
= mono_get_method_checked (ref
.image
, ref
.token
, NULL
, NULL
, error
);
3748 if (!ji
->data
.method
)
3749 g_error ("AOT Runtime could not load method due to %s", mono_error_get_message (error
)); /* FIXME don't swallow the error */
3751 g_assert (ji
->data
.method
);
3752 mono_class_init_internal (ji
->data
.method
->klass
);
3756 case MONO_PATCH_INFO_JIT_ICALL
:
3757 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
3758 case MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
: {
3759 guint32 len
= decode_value (p
, &p
);
3761 ji
->data
.name
= (char*)p
;
3765 case MONO_PATCH_INFO_METHODCONST
:
3767 ji
->data
.method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3768 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3769 if (!ji
->data
.method
)
3772 case MONO_PATCH_INFO_VTABLE
:
3773 case MONO_PATCH_INFO_CLASS
:
3774 case MONO_PATCH_INFO_IID
:
3775 case MONO_PATCH_INFO_ADJUSTED_IID
:
3777 ji
->data
.klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3778 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3779 if (!ji
->data
.klass
)
3782 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
3783 ji
->data
.del_tramp
= (MonoDelegateClassMethodPair
*)mono_mempool_alloc0 (mp
, sizeof (MonoDelegateClassMethodPair
));
3784 ji
->data
.del_tramp
->klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3785 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3786 if (!ji
->data
.del_tramp
->klass
)
3788 if (decode_value (p
, &p
)) {
3789 ji
->data
.del_tramp
->method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3790 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3791 if (!ji
->data
.del_tramp
->method
)
3794 ji
->data
.del_tramp
->is_virtual
= decode_value (p
, &p
) ? TRUE
: FALSE
;
3796 case MONO_PATCH_INFO_IMAGE
:
3797 ji
->data
.image
= load_image (aot_module
, decode_value (p
, &p
), error
);
3798 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3799 if (!ji
->data
.image
)
3802 case MONO_PATCH_INFO_FIELD
:
3803 case MONO_PATCH_INFO_SFLDA
:
3805 ji
->data
.field
= decode_field_info (aot_module
, p
, &p
);
3806 if (!ji
->data
.field
)
3809 case MONO_PATCH_INFO_SWITCH
:
3810 ji
->data
.table
= (MonoJumpInfoBBTable
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoBBTable
));
3811 ji
->data
.table
->table_size
= decode_value (p
, &p
);
3812 table
= (void **)mono_domain_alloc (mono_domain_get (), sizeof (gpointer
) * ji
->data
.table
->table_size
);
3813 ji
->data
.table
->table
= (MonoBasicBlock
**)table
;
3814 for (i
= 0; i
< ji
->data
.table
->table_size
; i
++)
3815 table
[i
] = (gpointer
)(gssize
)decode_value (p
, &p
);
3817 case MONO_PATCH_INFO_R4
: {
3820 ji
->data
.target
= mono_domain_alloc0 (mono_domain_get (), sizeof (float));
3821 val
= decode_value (p
, &p
);
3822 *(float*)ji
->data
.target
= *(float*)&val
;
3825 case MONO_PATCH_INFO_R8
: {
3829 ji
->data
.target
= mono_domain_alloc0 (mono_domain_get (), sizeof (double));
3831 val
[0] = decode_value (p
, &p
);
3832 val
[1] = decode_value (p
, &p
);
3833 v
= ((guint64
)val
[1] << 32) | ((guint64
)val
[0]);
3834 *(double*)ji
->data
.target
= *(double*)&v
;
3837 case MONO_PATCH_INFO_LDSTR
:
3838 image
= load_image (aot_module
, decode_value (p
, &p
), error
);
3839 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3842 ji
->data
.token
= mono_jump_info_token_new (mp
, image
, MONO_TOKEN_STRING
+ decode_value (p
, &p
));
3844 case MONO_PATCH_INFO_RVA
:
3845 case MONO_PATCH_INFO_DECLSEC
:
3846 case MONO_PATCH_INFO_LDTOKEN
:
3847 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
3849 image
= load_image (aot_module
, decode_value (p
, &p
), error
);
3850 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3853 ji
->data
.token
= mono_jump_info_token_new (mp
, image
, decode_value (p
, &p
));
3855 ji
->data
.token
->has_context
= decode_value (p
, &p
);
3856 if (ji
->data
.token
->has_context
) {
3857 gboolean res
= decode_generic_context (aot_module
, &ji
->data
.token
->context
, p
, &p
, error
);
3858 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3863 case MONO_PATCH_INFO_EXC_NAME
:
3864 ji
->data
.klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3865 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3866 if (!ji
->data
.klass
)
3868 ji
->data
.name
= m_class_get_name (ji
->data
.klass
);
3870 case MONO_PATCH_INFO_METHOD_REL
:
3871 ji
->data
.offset
= decode_value (p
, &p
);
3873 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
3874 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
:
3875 case MONO_PATCH_INFO_GC_NURSERY_START
:
3876 case MONO_PATCH_INFO_GC_NURSERY_BITS
:
3877 case MONO_PATCH_INFO_PROFILER_ALLOCATION_COUNT
:
3878 case MONO_PATCH_INFO_PROFILER_CLAUSE_COUNT
:
3880 case MONO_PATCH_INFO_CASTCLASS_CACHE
:
3881 ji
->data
.index
= decode_value (p
, &p
);
3883 case MONO_PATCH_INFO_RGCTX_FETCH
:
3884 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
3886 MonoJumpInfoRgctxEntry
*entry
;
3887 guint32 offset
, val
;
3890 offset
= decode_value (p
, &p
);
3891 val
= decode_value (p
, &p
);
3893 entry
= (MonoJumpInfoRgctxEntry
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoRgctxEntry
));
3894 p2
= aot_module
->blob
+ offset
;
3895 entry
->in_mrgctx
= ((val
& 1) > 0) ? TRUE
: FALSE
;
3896 if (entry
->in_mrgctx
)
3897 entry
->d
.method
= decode_resolve_method_ref (aot_module
, p2
, &p2
, error
);
3899 entry
->d
.klass
= decode_klass_ref (aot_module
, p2
, &p2
, error
);
3900 entry
->info_type
= (MonoRgctxInfoType
)((val
>> 1) & 0xff);
3901 entry
->data
= (MonoJumpInfo
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfo
));
3902 entry
->data
->type
= (MonoJumpInfoType
)((val
>> 9) & 0xff);
3903 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3905 res
= decode_patch (aot_module
, mp
, entry
->data
, p
, &p
);
3908 ji
->data
.rgctx_entry
= entry
;
3911 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
3912 case MONO_PATCH_INFO_AOT_MODULE
:
3913 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
:
3915 case MONO_PATCH_INFO_SIGNATURE
:
3916 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER
:
3917 ji
->data
.target
= decode_signature (aot_module
, p
, &p
);
3919 case MONO_PATCH_INFO_GSHAREDVT_CALL
: {
3920 MonoJumpInfoGSharedVtCall
*info
= (MonoJumpInfoGSharedVtCall
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoGSharedVtCall
));
3921 info
->sig
= decode_signature (aot_module
, p
, &p
);
3922 g_assert (info
->sig
);
3923 info
->method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3924 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3926 ji
->data
.target
= info
;
3929 case MONO_PATCH_INFO_GSHAREDVT_METHOD
: {
3930 MonoGSharedVtMethodInfo
*info
= (MonoGSharedVtMethodInfo
*)mono_mempool_alloc0 (mp
, sizeof (MonoGSharedVtMethodInfo
));
3933 info
->method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3934 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3936 info
->num_entries
= decode_value (p
, &p
);
3937 info
->count_entries
= info
->num_entries
;
3938 info
->entries
= (MonoRuntimeGenericContextInfoTemplate
*)mono_mempool_alloc0 (mp
, sizeof (MonoRuntimeGenericContextInfoTemplate
) * info
->num_entries
);
3939 for (i
= 0; i
< info
->num_entries
; ++i
) {
3940 MonoRuntimeGenericContextInfoTemplate
*template_
= &info
->entries
[i
];
3942 template_
->info_type
= (MonoRgctxInfoType
)decode_value (p
, &p
);
3943 switch (mini_rgctx_info_type_to_patch_info_type (template_
->info_type
)) {
3944 case MONO_PATCH_INFO_CLASS
: {
3945 MonoClass
*klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3946 mono_error_cleanup (error
); /* FIXME don't swallow the error */
3949 template_
->data
= m_class_get_byval_arg (klass
);
3952 case MONO_PATCH_INFO_FIELD
:
3953 template_
->data
= decode_field_info (aot_module
, p
, &p
);
3954 if (!template_
->data
)
3958 g_assert_not_reached ();
3962 ji
->data
.target
= info
;
3965 case MONO_PATCH_INFO_LDSTR_LIT
: {
3966 int len
= decode_value (p
, &p
);
3969 s
= (char *)mono_mempool_alloc0 (mp
, len
+ 1);
3970 memcpy (s
, p
, len
+ 1);
3973 ji
->data
.target
= s
;
3976 case MONO_PATCH_INFO_VIRT_METHOD
: {
3977 MonoJumpInfoVirtMethod
*info
= (MonoJumpInfoVirtMethod
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfoVirtMethod
));
3979 info
->klass
= decode_klass_ref (aot_module
, p
, &p
, error
);
3980 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3982 info
->method
= decode_resolve_method_ref (aot_module
, p
, &p
, error
);
3983 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
3985 ji
->data
.target
= info
;
3988 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG
:
3990 case MONO_PATCH_INFO_GET_TLS_TRAMP
:
3991 case MONO_PATCH_INFO_SET_TLS_TRAMP
:
3992 case MONO_PATCH_INFO_AOT_JIT_INFO
:
3993 ji
->data
.index
= decode_value (p
, &p
);
3996 g_warning ("unhandled type %d", ji
->type
);
3997 g_assert_not_reached ();
4011 * Decode a list of patches identified by the got offsets in GOT_OFFSETS. Return an array of
4012 * MonoJumpInfo structures allocated from MP.
4014 static MonoJumpInfo
*
4015 decode_patches (MonoAotModule
*amodule
, MonoMemPool
*mp
, int n_patches
, gboolean llvm
, guint32
*got_offsets
)
4017 MonoJumpInfo
*patches
;
4020 guint32
*got_info_offsets
;
4025 got
= amodule
->llvm_got
;
4026 got_info_offsets
= (guint32
*)amodule
->llvm_got_info_offsets
;
4029 got_info_offsets
= (guint32
*)amodule
->got_info_offsets
;
4032 patches
= (MonoJumpInfo
*)mono_mempool_alloc0 (mp
, sizeof (MonoJumpInfo
) * n_patches
);
4033 for (i
= 0; i
< n_patches
; ++i
) {
4034 guint8
*p
= amodule
->blob
+ mono_aot_get_offset (got_info_offsets
, got_offsets
[i
]);
4037 ji
->type
= (MonoJumpInfoType
)decode_value (p
, &p
);
4039 /* See load_method () for SFLDA */
4040 if (got
&& got
[got_offsets
[i
]] && ji
->type
!= MONO_PATCH_INFO_SFLDA
) {
4041 /* Already loaded */
4043 res
= decode_patch (amodule
, mp
, ji
, p
, &p
);
4052 static MonoJumpInfo
*
4053 load_patch_info (MonoAotModule
*amodule
, MonoMemPool
*mp
, int n_patches
,
4054 gboolean llvm
, guint32
**got_slots
,
4055 guint8
*buf
, guint8
**endbuf
)
4057 MonoJumpInfo
*patches
;
4063 *got_slots
= (guint32
*)g_malloc (sizeof (guint32
) * n_patches
);
4064 for (pindex
= 0; pindex
< n_patches
; ++pindex
) {
4065 (*got_slots
)[pindex
] = decode_value (p
, &p
);
4068 patches
= decode_patches (amodule
, mp
, n_patches
, llvm
, *got_slots
);
4070 g_free (*got_slots
);
4080 register_jump_target_got_slot (MonoDomain
*domain
, MonoMethod
*method
, gpointer
*got_slot
)
4083 * Jump addresses cannot be patched by the trampoline code since it
4084 * does not have access to the caller's address. Instead, we collect
4085 * the addresses of the GOT slots pointing to a method, and patch
4086 * them after the method has been compiled.
4088 MonoJitDomainInfo
*info
= domain_jit_info (domain
);
4090 MonoMethod
*shared_method
= mini_method_to_shared (method
);
4091 method
= shared_method
? shared_method
: method
;
4093 mono_domain_lock (domain
);
4094 if (!info
->jump_target_got_slot_hash
)
4095 info
->jump_target_got_slot_hash
= g_hash_table_new (NULL
, NULL
);
4096 list
= (GSList
*)g_hash_table_lookup (info
->jump_target_got_slot_hash
, method
);
4097 list
= g_slist_prepend (list
, got_slot
);
4098 g_hash_table_insert (info
->jump_target_got_slot_hash
, method
, list
);
4099 mono_domain_unlock (domain
);
4105 * Load the method identified by METHOD_INDEX from the AOT image. Return a
4106 * pointer to the native code of the method, or NULL if not found.
4107 * METHOD might not be set if the caller only has the image/token info.
4110 load_method (MonoDomain
*domain
, MonoAotModule
*amodule
, MonoImage
*image
, MonoMethod
*method
, guint32 token
, int method_index
,
4113 MonoJitInfo
*jinfo
= NULL
;
4114 guint8
*code
= NULL
, *info
;
4119 init_amodule_got (amodule
);
4121 if (domain
!= mono_get_root_domain ())
4122 /* Non shared AOT code can't be used in other appdomains */
4125 if (amodule
->out_of_date
)
4128 if (amodule
->info
.llvm_get_method
) {
4130 * Obtain the method address by calling a generated function in the LLVM module.
4132 gpointer (*get_method
) (int) = (gpointer (*)(int))amodule
->info
.llvm_get_method
;
4133 code
= (guint8
*)get_method (method_index
);
4137 if (method_index
< amodule
->info
.nmethods
)
4138 code
= (guint8
*)amodule
->methods
[method_index
];
4143 if (amodule
->methods
[method_index
] == GINT_TO_POINTER (-1)) {
4144 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
)) {
4148 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
4152 if (!(method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)) {
4153 full_name
= mono_method_full_name (method
, TRUE
);
4154 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "AOT: NOT FOUND: %s.", full_name
);
4162 info
= &amodule
->blob
[mono_aot_get_offset (amodule
->method_info_offsets
, method_index
)];
4164 if (!amodule
->methods_loaded
) {
4165 amodule_lock (amodule
);
4166 if (!amodule
->methods_loaded
) {
4169 loaded
= g_new0 (guint32
, amodule
->info
.nmethods
/ 32 + 1);
4170 mono_memory_barrier ();
4171 amodule
->methods_loaded
= loaded
;
4173 amodule_unlock (amodule
);
4176 if ((amodule
->methods_loaded
[method_index
/ 32] >> (method_index
% 32)) & 0x1)
4179 if (mini_debug_options
.aot_skip_set
&& !(method
&& method
->wrapper_type
)) {
4180 gint32 methods_aot
= mono_atomic_load_i32 (&mono_jit_stats
.methods_aot
);
4181 if (methods_aot
== mini_debug_options
.aot_skip
) {
4183 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
4188 char *name
= mono_method_full_name (method
, TRUE
);
4189 g_print ("NON AOT METHOD: %s.\n", name
);
4192 g_print ("NON AOT METHOD: %p %d\n", code
, method_index
);
4194 mini_debug_options
.aot_skip_set
= FALSE
;
4199 if (mono_last_aot_method
!= -1) {
4200 gint32 methods_aot
= mono_atomic_load_i32 (&mono_jit_stats
.methods_aot
);
4201 if (methods_aot
>= mono_last_aot_method
)
4203 else if (methods_aot
== mono_last_aot_method
- 1) {
4205 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
4210 char *name
= mono_method_full_name (method
, TRUE
);
4211 g_print ("LAST AOT METHOD: %s.\n", name
);
4214 g_print ("LAST AOT METHOD: %p %d\n", code
, method_index
);
4219 if (!(is_llvm_code (amodule
, code
) && (amodule
->info
.flags
& MONO_AOT_FILE_FLAG_LLVM_ONLY
)) ||
4220 (mono_llvm_only
&& method
&& method
->wrapper_type
== MONO_WRAPPER_NATIVE_TO_MANAGED
)) {
4221 res
= init_method (amodule
, method_index
, method
, NULL
, NULL
, error
);
4226 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
)) {
4230 method
= mono_get_method_checked (image
, token
, NULL
, NULL
, error
);
4235 full_name
= mono_method_full_name (method
, TRUE
);
4238 jinfo
= mono_aot_find_jit_info (domain
, amodule
->assembly
->image
, code
);
4240 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "AOT: FOUND method %s [%p - %p %p]", full_name
, code
, code
+ jinfo
->code_size
, info
);
4244 if (mono_llvm_only
) {
4247 info
= &amodule
->blob
[mono_aot_get_offset (amodule
->method_info_offsets
, method_index
)];
4249 guint8 flags
= decode_value (p
, &p
);
4250 /* The caller needs to looks this up, but its hard to do without constructing the full MonoJitInfo, so save it here */
4251 if (flags
& MONO_AOT_METHOD_FLAG_GSHAREDVT_VARIABLE
) {
4253 if (!code_to_method_flags
)
4254 code_to_method_flags
= g_hash_table_new (NULL
, NULL
);
4255 g_hash_table_insert (code_to_method_flags
, code
, GUINT_TO_POINTER (flags
));
4260 amodule_lock (amodule
);
4264 mono_atomic_inc_i32 (&mono_jit_stats
.methods_aot
);
4266 if (method
&& method
->wrapper_type
)
4267 g_hash_table_insert (amodule
->method_to_code
, method
, code
);
4269 /* Commit changes since methods_loaded is accessed outside the lock */
4270 mono_memory_barrier ();
4272 amodule
->methods_loaded
[method_index
/ 32] |= 1 << (method_index
% 32);
4274 amodule_unlock (amodule
);
4276 if (MONO_PROFILER_ENABLED (jit_begin
) || MONO_PROFILER_ENABLED (jit_done
)) {
4280 method
= mono_get_method_checked (amodule
->assembly
->image
, token
, NULL
, NULL
, error
);
4284 MONO_PROFILER_RAISE (jit_begin
, (method
));
4285 jinfo
= mono_jit_info_table_find (domain
, code
);
4287 MONO_PROFILER_RAISE (jit_done
, (method
, jinfo
));
4299 /** find_aot_method_in_amodule
4301 * \param code_amodule The AOT module containing the code pointer
4302 * \param method The method to find the code index for
4303 * \param hash_full The hash for the method
4306 find_aot_method_in_amodule (MonoAotModule
*code_amodule
, MonoMethod
*method
, guint32 hash_full
)
4309 guint32 table_size
, entry_size
, hash
;
4310 guint32
*table
, *entry
;
4312 static guint32 n_extra_decodes
;
4314 // The AOT module containing the MonoMethod
4315 // The reference to the metadata amodule will differ among multiple dedup methods
4316 // which mangle to the same name but live in different assemblies. This leads to
4317 // the caching breaking. The solution seems to be to cache using the "metadata" amodule.
4318 MonoAotModule
*metadata_amodule
= m_class_get_image (method
->klass
)->aot_module
;
4320 if (!metadata_amodule
|| metadata_amodule
->out_of_date
|| !code_amodule
|| code_amodule
->out_of_date
)
4323 table_size
= code_amodule
->extra_method_table
[0];
4324 hash
= hash_full
% table_size
;
4325 table
= code_amodule
->extra_method_table
+ 1;
4328 entry
= &table
[hash
* entry_size
];
4335 guint32 key
= entry
[0];
4336 guint32 value
= entry
[1];
4337 guint32 next
= entry
[entry_size
- 1];
4341 p
= code_amodule
->blob
+ key
;
4344 amodule_lock (metadata_amodule
);
4345 if (!metadata_amodule
->method_ref_to_method
)
4346 metadata_amodule
->method_ref_to_method
= g_hash_table_new (NULL
, NULL
);
4347 m
= (MonoMethod
*)g_hash_table_lookup (metadata_amodule
->method_ref_to_method
, p
);
4348 amodule_unlock (metadata_amodule
);
4350 m
= decode_resolve_method_ref_with_target (code_amodule
, method
, p
, &p
, error
);
4351 mono_error_cleanup (error
); /* FIXME don't swallow the error */
4353 * Can't catche runtime invoke wrappers since it would break
4354 * the check in decode_method_ref_with_target ().
4356 if (m
&& m
->wrapper_type
!= MONO_WRAPPER_RUNTIME_INVOKE
) {
4357 amodule_lock (metadata_amodule
);
4358 g_hash_table_insert (metadata_amodule
->method_ref_to_method
, orig_p
, m
);
4359 amodule_unlock (metadata_amodule
);
4367 /* Methods decoded needlessly */
4369 //printf ("%d %s %s %p\n", n_extra_decodes, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE), orig_p);
4374 entry
= &table
[next
* entry_size
];
4379 if (index
!= 0xffffff)
4380 g_assert (index
< code_amodule
->info
.nmethods
);
4386 add_module_cb (gpointer key
, gpointer value
, gpointer user_data
)
4388 g_ptr_array_add ((GPtrArray
*)user_data
, value
);
4392 mono_aot_can_dedup (MonoMethod
*method
)
4395 /* Use a set of wrappers/instances which work and useful */
4396 switch (method
->wrapper_type
) {
4397 case MONO_WRAPPER_RUNTIME_INVOKE
:
4400 case MONO_WRAPPER_OTHER
: {
4401 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
4403 if (info
->subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
||
4404 info
->subtype
== WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
||
4405 info
->subtype
== WRAPPER_SUBTYPE_INTERP_LMF
)
4413 if (method
->is_inflated
&& !mono_method_is_generic_sharable_full (method
, TRUE
, FALSE
, FALSE
) &&
4414 !mini_is_gsharedvt_signature (mono_method_signature_internal (method
)) &&
4415 !mini_is_gsharedvt_klass (method
->klass
))
4420 gboolean not_normal_gshared
= method
->is_inflated
&& !mono_method_is_generic_sharable_full (method
, TRUE
, FALSE
, FALSE
);
4421 gboolean extra_method
= (method
->wrapper_type
!= MONO_WRAPPER_NONE
) || not_normal_gshared
;
4423 return extra_method
;
4431 * Try finding METHOD in the extra_method table in all AOT images.
4432 * Return its method index, or 0xffffff if not found. Set OUT_AMODULE to the AOT
4433 * module where the method was found.
4436 find_aot_method (MonoMethod
*method
, MonoAotModule
**out_amodule
)
4441 guint32 hash
= mono_aot_method_hash (method
);
4443 /* Try the place we expect to have moved the method only
4444 * We don't probe, as that causes hard-to-debug issues when we fail
4445 * to find the method */
4446 if (container_amodule
&& mono_aot_can_dedup (method
)) {
4447 *out_amodule
= container_amodule
;
4448 index
= find_aot_method_in_amodule (container_amodule
, method
, hash
);
4452 /* Try the method's module first */
4453 *out_amodule
= m_class_get_image (method
->klass
)->aot_module
;
4454 index
= find_aot_method_in_amodule (m_class_get_image (method
->klass
)->aot_module
, method
, hash
);
4455 if (index
!= 0xffffff)
4459 * Try all other modules.
4460 * This is needed because generic instances klass->image points to the image
4461 * containing the generic definition, but the native code is generated to the
4462 * AOT image which contains the reference.
4465 /* Make a copy to avoid doing the search inside the aot lock */
4466 modules
= g_ptr_array_new ();
4468 g_hash_table_foreach (aot_modules
, add_module_cb
, modules
);
4472 for (i
= 0; i
< modules
->len
; ++i
) {
4473 MonoAotModule
*amodule
= (MonoAotModule
*)g_ptr_array_index (modules
, i
);
4475 if (amodule
!= m_class_get_image (method
->klass
)->aot_module
)
4476 index
= find_aot_method_in_amodule (amodule
, method
, hash
);
4477 if (index
!= 0xffffff) {
4478 *out_amodule
= amodule
;
4483 g_ptr_array_free (modules
, TRUE
);
4489 mono_aot_find_method_index (MonoMethod
*method
)
4491 MonoAotModule
*out_amodule
;
4492 return find_aot_method (method
, &out_amodule
);
4496 init_method (MonoAotModule
*amodule
, guint32 method_index
, MonoMethod
*method
, MonoClass
*init_class
, MonoGenericContext
*context
, MonoError
*error
)
4498 MonoDomain
*domain
= mono_domain_get ();
4500 MonoClass
*klass_to_run_ctor
= NULL
;
4501 gboolean from_plt
= method
== NULL
;
4502 int pindex
, n_patches
;
4504 MonoJitInfo
*jinfo
= NULL
;
4505 guint8
*code
, *info
;
4509 code
= (guint8
*)amodule
->methods
[method_index
];
4510 info
= &amodule
->blob
[mono_aot_get_offset (amodule
->method_info_offsets
, method_index
)];
4514 guint8 flags
= decode_value (p
, &p
);
4515 if (flags
& MONO_AOT_METHOD_FLAG_HAS_CCTOR
)
4516 klass_to_run_ctor
= decode_klass_ref (amodule
, p
, &p
, error
);
4520 //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
4522 klass_to_run_ctor
= method
->klass
;
4524 if (flags
& MONO_AOT_METHOD_FLAG_HAS_PATCHES
)
4525 n_patches
= decode_value (p
, &p
);
4530 MonoJumpInfo
*patches
;
4535 mp
= mono_mempool_new ();
4537 if ((gpointer
)code
>= amodule
->info
.jit_code_start
&& (gpointer
)code
<= amodule
->info
.jit_code_end
) {
4542 got
= amodule
->llvm_got
;
4546 patches
= load_patch_info (amodule
, mp
, n_patches
, llvm
, &got_slots
, p
, &p
);
4547 if (patches
== NULL
) {
4548 mono_mempool_destroy (mp
);
4552 for (pindex
= 0; pindex
< n_patches
; ++pindex
) {
4553 MonoJumpInfo
*ji
= &patches
[pindex
];
4557 * For SFLDA, we need to call resolve_patch_target () since the GOT slot could have
4558 * been initialized by load_method () for a static cctor before the cctor has
4559 * finished executing (#23242).
4561 if (!got
[got_slots
[pindex
]] || ji
->type
== MONO_PATCH_INFO_SFLDA
) {
4562 /* In llvm-only made, we might encounter shared methods */
4563 if (mono_llvm_only
&& ji
->type
== MONO_PATCH_INFO_METHOD
&& mono_method_check_context_used (ji
->data
.method
)) {
4565 ji
->data
.method
= mono_class_inflate_generic_method_checked (ji
->data
.method
, context
, error
);
4566 if (!mono_error_ok (error
)) {
4568 mono_mempool_destroy (mp
);
4572 /* This cannot be resolved in mono_resolve_patch_target () */
4573 if (ji
->type
== MONO_PATCH_INFO_AOT_JIT_INFO
) {
4574 // FIXME: Lookup using the index
4575 jinfo
= mono_aot_find_jit_info (domain
, amodule
->assembly
->image
, code
);
4576 ji
->type
= MONO_PATCH_INFO_ABS
;
4577 ji
->data
.target
= jinfo
;
4579 addr
= mono_resolve_patch_target (method
, domain
, code
, ji
, TRUE
, error
);
4580 if (!mono_error_ok (error
)) {
4582 mono_mempool_destroy (mp
);
4585 if (ji
->type
== MONO_PATCH_INFO_METHOD_JUMP
)
4586 addr
= mono_create_ftnptr (domain
, addr
);
4587 mono_memory_barrier ();
4588 got
[got_slots
[pindex
]] = addr
;
4589 if (ji
->type
== MONO_PATCH_INFO_METHOD_JUMP
)
4590 register_jump_target_got_slot (domain
, ji
->data
.method
, &(got
[got_slots
[pindex
]]));
4592 ji
->type
= MONO_PATCH_INFO_NONE
;
4597 mono_mempool_destroy (mp
);
4600 if (mini_get_debug_options ()->load_aot_jit_info_eagerly
)
4601 jinfo
= mono_aot_find_jit_info (domain
, amodule
->assembly
->image
, code
);
4606 MonoVTable
*vt
= mono_class_vtable_checked (domain
, init_class
, error
);
4610 inited_ok
= mono_runtime_class_init_full (vt
, error
);
4611 } else if (from_plt
&& klass_to_run_ctor
&& !mono_class_is_gtd (klass_to_run_ctor
)) {
4612 MonoVTable
*vt
= mono_class_vtable_checked (domain
, klass_to_run_ctor
, error
);
4616 inited_ok
= mono_runtime_class_init_full (vt
, error
);
4631 * mono_aot_init_llvmonly_method:
4633 * Initialize the method identified by METHOD_INDEX in llvmonly mode.
4634 * If LOOKUP_CONTEXT is TRUE, assume the method is in extra_methods, and load the context from there.
4637 mono_aot_init_llvmonly_method (gpointer aot_module
, guint32 method_index
, MonoClass
*init_class
, MonoGenericContext
*context
,
4638 gboolean lookup_context
, MonoError
*error
)
4640 MonoAotModule
*amodule
= (MonoAotModule
*)aot_module
;
4641 MonoMethod
*method
= NULL
;
4643 if (lookup_context
) {
4644 amodule_lock (amodule
);
4645 method
= (MonoMethod
*)g_hash_table_lookup (amodule
->extra_methods
, GUINT_TO_POINTER (method_index
));
4646 amodule_unlock (amodule
);
4649 context
= mono_method_get_context (method
);
4652 return init_method (amodule
, method_index
, method
, init_class
, context
, error
);
4656 * mono_aot_get_method:
4658 * Return a pointer to the AOTed native code for METHOD if it can be found,
4660 * On platforms with function pointers, this doesn't return a function pointer.
4663 mono_aot_get_method (MonoDomain
*domain
, MonoMethod
*method
, MonoError
*error
)
4665 MonoClass
*klass
= method
->klass
;
4666 MonoMethod
*orig_method
= method
;
4667 guint32 method_index
;
4668 MonoAotModule
*amodule
= m_class_get_image (klass
)->aot_module
;
4670 gboolean cache_result
= FALSE
;
4671 ERROR_DECL (inner_error
);
4675 if (domain
!= mono_get_root_domain ())
4676 /* Non shared AOT code can't be used in other appdomains */
4679 if (enable_aot_cache
&& !amodule
&& domain
->entry_assembly
&& mono_is_corlib_image (m_class_get_image (klass
))) {
4680 /* This cannot be AOTed during startup, so do it now */
4681 if (!mscorlib_aot_loaded
) {
4682 mscorlib_aot_loaded
= TRUE
;
4683 load_aot_module (m_class_get_image (klass
)->assembly
, NULL
);
4684 amodule
= m_class_get_image (klass
)->aot_module
;
4691 if (amodule
->out_of_date
)
4694 if ((method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) ||
4695 (method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
4696 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4697 (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
))
4701 * Use the original method instead of its invoke-with-check wrapper.
4702 * This is not a problem when using full-aot, since it doesn't support
4705 if (mono_aot_only
&& method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
4706 return mono_aot_get_method (domain
, mono_marshal_method_from_wrapper (method
), error
);
4708 g_assert (m_class_is_inited (klass
));
4710 /* Find method index */
4711 method_index
= 0xffffff;
4713 gboolean dedupable
= mono_aot_can_dedup (method
);
4715 if (method
->is_inflated
&& !method
->wrapper_type
&& mono_method_is_generic_sharable_full (method
, TRUE
, FALSE
, FALSE
) && !dedupable
) {
4716 MonoMethod
*orig_method
= method
;
4718 * For generic methods, we store the fully shared instance in place of the
4721 method
= mono_method_get_declaring_generic_method (method
);
4722 method_index
= mono_metadata_token_index (method
->token
) - 1;
4724 if (mono_llvm_only
) {
4725 /* Needed by mono_aot_init_gshared_method_this () */
4726 /* orig_method is a random instance but it is enough to make init_method () work */
4727 amodule_lock (amodule
);
4728 g_hash_table_insert (amodule
->extra_methods
, GUINT_TO_POINTER (method_index
), orig_method
);
4729 amodule_unlock (amodule
);
4733 if (method_index
== 0xffffff && (method
->is_inflated
|| !method
->token
)) {
4734 /* This hash table is used to avoid the slower search in the extra_method_table in the AOT image */
4735 amodule_lock (amodule
);
4736 code
= (guint8
*)g_hash_table_lookup (amodule
->method_to_code
, method
);
4737 amodule_unlock (amodule
);
4741 cache_result
= TRUE
;
4742 if (method_index
== 0xffffff)
4743 method_index
= find_aot_method (method
, &amodule
);
4746 * Special case the ICollection<T> wrappers for arrays, as they cannot
4747 * be statically enumerated, and each wrapper ends up calling the same
4750 if (method_index
== 0xffffff && method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_MANAGED
&& m_class_get_rank (method
->klass
) && strstr (method
->name
, "System.Collections.Generic")) {
4751 MonoMethod
*m
= mono_aot_get_array_helper_from_wrapper (method
);
4753 code
= (guint8
*)mono_aot_get_method (domain
, m
, inner_error
);
4754 mono_error_cleanup (inner_error
);
4760 * Special case Array.GetGenericValueImpl which is a generic icall.
4761 * Generic sharing currently can't handle it, but the icall returns data using
4762 * an out parameter, so the managed-to-native wrappers can share the same code.
4764 if (method_index
== 0xffffff && method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
&& method
->klass
== mono_defaults
.array_class
&& !strcmp (method
->name
, "GetGenericValueImpl")) {
4766 MonoGenericContext ctx
;
4767 MonoType
*args
[16];
4769 if (mono_method_signature_internal (method
)->params
[1]->type
== MONO_TYPE_OBJECT
)
4770 /* Avoid recursion */
4773 m
= mono_class_get_method_from_name_checked (mono_defaults
.array_class
, "GetGenericValueImpl", 2, 0, error
);
4774 mono_error_assert_ok (error
);
4777 memset (&ctx
, 0, sizeof (ctx
));
4778 args
[0] = m_class_get_byval_arg (mono_defaults
.object_class
);
4779 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
4781 m
= mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m
, &ctx
, error
), TRUE
, TRUE
);
4783 g_error ("AOT runtime could not load method due to %s", mono_error_get_message (error
)); /* FIXME don't swallow the error */
4786 * Get the code for the <object> instantiation which should be emitted into
4787 * the mscorlib aot image by the AOT compiler.
4789 code
= (guint8
*)mono_aot_get_method (domain
, m
, inner_error
);
4790 mono_error_cleanup (inner_error
);
4795 const char *klass_name_space
= m_class_get_name_space (method
->klass
);
4796 const char *klass_name
= m_class_get_name (method
->klass
);
4797 /* Same for CompareExchange<T> and Exchange<T> */
4798 /* Same for Volatile.Read<T>/Write<T> */
4799 if (method_index
== 0xffffff && method
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_NATIVE
&& m_class_get_image (method
->klass
) == mono_defaults
.corlib
&&
4800 ((!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_internal (method
)->params
[1]))) ||
4801 (!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_internal (method
)->ret
)))) ||
4802 (!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_internal (method
)->params
[1])))))) {
4804 MonoGenericContext ctx
;
4805 MonoType
*args
[16];
4806 gpointer iter
= NULL
;
4808 while ((m
= mono_class_get_methods (method
->klass
, &iter
))) {
4809 if (mono_method_signature_internal (m
)->generic_param_count
&& !strcmp (m
->name
, method
->name
))
4814 memset (&ctx
, 0, sizeof (ctx
));
4815 args
[0] = mono_get_object_type ();
4816 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
4818 m
= mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m
, &ctx
, error
), TRUE
, TRUE
);
4820 g_error ("AOT runtime could not load method due to %s", mono_error_get_message (error
)); /* FIXME don't swallow the error */
4822 /* Avoid recursion */
4827 * Get the code for the <object> instantiation which should be emitted into
4828 * the mscorlib aot image by the AOT compiler.
4830 code
= (guint8
*)mono_aot_get_method (domain
, m
, inner_error
);
4831 mono_error_cleanup (inner_error
);
4836 /* For ARRAY_ACCESSOR wrappers with reference types, use the <object> instantiation saved in corlib */
4837 if (method_index
== 0xffffff && method
->wrapper_type
== MONO_WRAPPER_OTHER
) {
4838 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
4840 if (info
->subtype
== WRAPPER_SUBTYPE_ARRAY_ACCESSOR
) {
4841 MonoMethod
*array_method
= info
->d
.array_accessor
.method
;
4842 if (MONO_TYPE_IS_REFERENCE (m_class_get_byval_arg (m_class_get_element_class (array_method
->klass
)))) {
4845 if (!strcmp (array_method
->name
, "Set"))
4846 rank
= mono_method_signature_internal (array_method
)->param_count
- 1;
4847 else if (!strcmp (array_method
->name
, "Get") || !strcmp (array_method
->name
, "Address"))
4848 rank
= mono_method_signature_internal (array_method
)->param_count
;
4850 g_assert_not_reached ();
4851 MonoClass
*obj_array_class
= mono_class_create_array (mono_defaults
.object_class
, rank
);
4852 MonoMethod
*m
= mono_class_get_method_from_name_checked (obj_array_class
, array_method
->name
, mono_method_signature_internal (array_method
)->param_count
, 0, error
);
4853 mono_error_assert_ok (error
);
4856 m
= mono_marshal_get_array_accessor_wrapper (m
);
4858 code
= (guint8
*)mono_aot_get_method (domain
, m
, inner_error
);
4859 mono_error_cleanup (inner_error
);
4867 if (method_index
== 0xffffff && method
->is_inflated
&& mono_method_is_generic_sharable_full (method
, FALSE
, TRUE
, FALSE
)) {
4868 /* Partial sharing */
4871 shared
= mini_get_shared_method_full (method
, SHARE_MODE_NONE
, error
);
4872 return_val_if_nok (error
, NULL
);
4874 method_index
= find_aot_method (shared
, &amodule
);
4875 if (method_index
!= 0xffffff)
4879 if (method_index
== 0xffffff && method
->is_inflated
&& mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, TRUE
)) {
4882 /* Use the all-vt shared method since this is what was AOTed */
4883 shared
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
4887 method_index
= find_aot_method (shared
, &amodule
);
4888 if (method_index
!= 0xffffff) {
4889 method
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
4895 if (method_index
== 0xffffff) {
4896 if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
)) {
4899 full_name
= mono_method_full_name (method
, TRUE
);
4900 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "AOT NOT FOUND: %s.", full_name
);
4906 if (method_index
== 0xffffff)
4909 /* Needed by find_jit_info */
4910 amodule_lock (amodule
);
4911 g_hash_table_insert (amodule
->extra_methods
, GUINT_TO_POINTER (method_index
), method
);
4912 amodule_unlock (amodule
);
4915 method_index
= mono_metadata_token_index (method
->token
) - 1;
4917 guint32 num_methods
= amodule
->info
.nmethods
- amodule
->info
.nextra_methods
;
4918 if (method_index
>= num_methods
)
4919 /* method not available in AOT image */
4923 code
= (guint8
*)load_method (domain
, amodule
, m_class_get_image (klass
), method
, method
->token
, method_index
, error
);
4926 if (code
&& cache_result
) {
4927 amodule_lock (amodule
);
4928 g_hash_table_insert (amodule
->method_to_code
, orig_method
, code
);
4929 amodule_unlock (amodule
);
4935 * Same as mono_aot_get_method, but we try to avoid loading any metadata from the
4939 mono_aot_get_method_from_token (MonoDomain
*domain
, MonoImage
*image
, guint32 token
, MonoError
*error
)
4941 MonoAotModule
*aot_module
= image
->aot_module
;
4950 method_index
= mono_metadata_token_index (token
) - 1;
4952 res
= load_method (domain
, aot_module
, image
, NULL
, token
, method_index
, error
);
4959 } IsGotEntryUserData
;
4962 check_is_got_entry (gpointer key
, gpointer value
, gpointer user_data
)
4964 IsGotEntryUserData
*data
= (IsGotEntryUserData
*)user_data
;
4965 MonoAotModule
*aot_module
= (MonoAotModule
*)value
;
4967 if (aot_module
->got
&& (data
->addr
>= (guint8
*)(aot_module
->got
)) && (data
->addr
< (guint8
*)(aot_module
->got
+ aot_module
->info
.got_size
)))
4972 mono_aot_is_got_entry (guint8
*code
, guint8
*addr
)
4974 IsGotEntryUserData user_data
;
4979 user_data
.addr
= addr
;
4980 user_data
.res
= FALSE
;
4982 g_hash_table_foreach (aot_modules
, check_is_got_entry
, &user_data
);
4985 return user_data
.res
;
4990 MonoAotModule
*module
;
4991 } FindAotModuleUserData
;
4994 find_aot_module_cb (gpointer key
, gpointer value
, gpointer user_data
)
4996 FindAotModuleUserData
*data
= (FindAotModuleUserData
*)user_data
;
4997 MonoAotModule
*aot_module
= (MonoAotModule
*)value
;
4999 if (amodule_contains_code_addr (aot_module
, data
->addr
))
5000 data
->module
= aot_module
;
5003 static inline MonoAotModule
*
5004 find_aot_module (guint8
*code
)
5006 FindAotModuleUserData user_data
;
5011 /* Reading these need no locking */
5012 if (((gsize
)code
< aot_code_low_addr
) || ((gsize
)code
> aot_code_high_addr
))
5015 user_data
.addr
= code
;
5016 user_data
.module
= NULL
;
5019 g_hash_table_foreach (aot_modules
, find_aot_module_cb
, &user_data
);
5022 return user_data
.module
;
5026 mono_aot_patch_plt_entry (guint8
*code
, guint8
*plt_entry
, gpointer
*got
, host_mgreg_t
*regs
, guint8
*addr
)
5028 MonoAotModule
*amodule
;
5031 * Since AOT code is only used in the root domain,
5032 * mono_domain_get () != mono_get_root_domain () means the calling method
5033 * is AppDomain:InvokeInDomain, so this is the same check as in
5034 * mono_method_same_domain () but without loading the metadata for the method.
5036 if (mono_domain_get () == mono_get_root_domain ()) {
5038 amodule
= find_aot_module (code
);
5042 mono_arch_patch_plt_entry (plt_entry
, got
, regs
, addr
);
5047 * mono_aot_plt_resolve:
5049 * This function is called by the entries in the PLT to resolve the actual method that
5050 * needs to be called. It returns a trampoline to the method and patches the PLT entry.
5051 * Returns NULL if the something cannot be loaded.
5054 mono_aot_plt_resolve (gpointer aot_module
, guint32 plt_info_offset
, guint8
*code
, MonoError
*error
)
5056 #ifdef MONO_ARCH_AOT_SUPPORTED
5057 guint8
*p
, *target
, *plt_entry
;
5059 MonoAotModule
*module
= (MonoAotModule
*)aot_module
;
5060 gboolean res
, no_ftnptr
= FALSE
;
5062 gboolean using_gsharedvt
= FALSE
;
5066 //printf ("DYN: %p %d\n", aot_module, plt_info_offset);
5068 p
= &module
->blob
[plt_info_offset
];
5070 ji
.type
= (MonoJumpInfoType
)decode_value (p
, &p
);
5072 mp
= mono_mempool_new ();
5073 res
= decode_patch (module
, mp
, &ji
, p
, &p
);
5076 mono_mempool_destroy (mp
);
5080 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
5081 using_gsharedvt
= TRUE
;
5085 * Avoid calling resolve_patch_target in the full-aot case if possible, since
5086 * it would create a trampoline, and we don't need that.
5087 * We could do this only if the method does not need the special handling
5088 * in mono_magic_trampoline ().
5090 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
) &&
5091 !mono_method_needs_static_rgctx_invoke (ji
.data
.method
, FALSE
) && !using_gsharedvt
) {
5092 target
= (guint8
*)mono_jit_compile_method (ji
.data
.method
, error
);
5093 if (!mono_error_ok (error
)) {
5094 mono_mempool_destroy (mp
);
5099 target
= (guint8
*)mono_resolve_patch_target (NULL
, mono_domain_get (), NULL
, &ji
, TRUE
, error
);
5100 if (!mono_error_ok (error
)) {
5101 mono_mempool_destroy (mp
);
5107 * The trampoline expects us to return a function descriptor on platforms which use
5108 * it, but resolve_patch_target returns a direct function pointer for some type of
5109 * patches, so have to translate between the two.
5110 * FIXME: Clean this up, but how ?
5112 if (ji
.type
== MONO_PATCH_INFO_ABS
|| ji
.type
== MONO_PATCH_INFO_JIT_ICALL
|| ji
.type
== MONO_PATCH_INFO_ICALL_ADDR
|| ji
.type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
|| ji
.type
== MONO_PATCH_INFO_RGCTX_FETCH
) {
5113 /* These should already have a function descriptor */
5114 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
5115 /* Our function descriptors have a 0 environment, gcc created ones don't */
5116 if (ji
.type
!= MONO_PATCH_INFO_JIT_ICALL
&& ji
.type
!= MONO_PATCH_INFO_JIT_ICALL_ADDR
&& ji
.type
!= MONO_PATCH_INFO_ICALL_ADDR
)
5117 g_assert (((gpointer
*)target
) [2] == 0);
5120 } else if (!no_ftnptr
) {
5121 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
5122 g_assert (((gpointer
*)target
) [2] != 0);
5124 target
= (guint8
*)mono_create_ftnptr (mono_domain_get (), target
);
5127 mono_mempool_destroy (mp
);
5129 /* Patch the PLT entry with target which might be the actual method not a trampoline */
5130 plt_entry
= mono_aot_get_plt_entry (code
);
5131 g_assert (plt_entry
);
5132 mono_aot_patch_plt_entry (code
, plt_entry
, module
->got
, NULL
, target
);
5136 g_assert_not_reached ();
5144 * Initialize the PLT table of the AOT module. Called lazily when the first AOT
5145 * method in the module is loaded to avoid committing memory by writing to it.
5146 * LOCKING: Assumes the AMODULE lock is held.
5149 init_plt (MonoAotModule
*amodule
)
5154 if (amodule
->plt_inited
)
5157 if (amodule
->info
.plt_size
<= 1) {
5158 amodule
->plt_inited
= TRUE
;
5162 tramp
= mono_create_specific_trampoline (amodule
, MONO_TRAMPOLINE_AOT_PLT
, mono_get_root_domain (), NULL
);
5165 * Initialize the PLT entries in the GOT to point to the default targets.
5168 tramp
= mono_create_ftnptr (mono_domain_get (), tramp
);
5169 for (i
= 1; i
< amodule
->info
.plt_size
; ++i
)
5170 /* All the default entries point to the AOT trampoline */
5171 ((gpointer
*)amodule
->got
)[amodule
->info
.plt_got_offset_base
+ i
] = tramp
;
5173 amodule
->plt_inited
= TRUE
;
5177 * mono_aot_get_plt_entry:
5179 * Return the address of the PLT entry called by the code at CODE if exists.
5182 mono_aot_get_plt_entry (guint8
*code
)
5184 MonoAotModule
*amodule
= find_aot_module (code
);
5185 guint8
*target
= NULL
;
5191 if (is_thumb_code (amodule
, code
- 4))
5192 return mono_arm_get_thumb_plt_entry (code
);
5195 #ifdef MONO_ARCH_AOT_SUPPORTED
5196 target
= mono_arch_get_call_target (code
);
5198 g_assert_not_reached ();
5202 while (target
!= NULL
) {
5203 if ((target
>= (guint8
*)(amodule
->plt
)) && (target
< (guint8
*)(amodule
->plt_end
)))
5206 // Add 4 since mono_arch_get_call_target assumes we're passing
5207 // the instruction after the actual branch instruction.
5208 target
= mono_arch_get_call_target (target
+ 4);
5213 if ((target
>= (guint8
*)(amodule
->plt
)) && (target
< (guint8
*)(amodule
->plt_end
)))
5221 * mono_aot_get_plt_info_offset:
5223 * Return the PLT info offset belonging to the plt entry called by CODE.
5226 mono_aot_get_plt_info_offset (host_mgreg_t
*regs
, guint8
*code
)
5228 guint8
*plt_entry
= mono_aot_get_plt_entry (code
);
5230 g_assert (plt_entry
);
5232 /* The offset is embedded inside the code after the plt entry */
5233 #ifdef MONO_ARCH_AOT_SUPPORTED
5234 return mono_arch_get_plt_info_offset (plt_entry
, regs
, code
);
5236 g_assert_not_reached ();
5242 mono_create_ftnptr_malloc (guint8
*code
)
5244 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
5245 MonoPPCFunctionDescriptor
*ftnptr
= g_malloc0 (sizeof (MonoPPCFunctionDescriptor
));
5247 ftnptr
->code
= code
;
5258 * mono_aot_register_jit_icall:
5260 * Register a JIT icall which is called by trampolines in full-aot mode. This should
5261 * be called from mono_arch_init () during startup.
5264 mono_aot_register_jit_icall (const char *name
, gpointer addr
)
5266 /* No need for locking */
5267 if (!aot_jit_icall_hash
)
5268 aot_jit_icall_hash
= g_hash_table_new (g_str_hash
, g_str_equal
);
5269 g_hash_table_insert (aot_jit_icall_hash
, (char*)name
, addr
);
5273 * load_function_full:
5275 * Load the function named NAME from the aot image.
5278 load_function_full (MonoAotModule
*amodule
, const char *name
, MonoTrampInfo
**out_tinfo
)
5282 int n_patches
, pindex
;
5285 guint32 info_offset
;
5289 symbol
= g_strdup_printf ("%s", name
);
5290 find_amodule_symbol (amodule
, symbol
, (gpointer
*)&code
);
5293 g_error ("Symbol '%s' not found in AOT file '%s'.\n", name
, amodule
->aot_name
);
5295 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "AOT: FOUND function '%s' in AOT file '%s'.", name
, amodule
->aot_name
);
5299 symbol
= g_strdup_printf ("%s_p", name
);
5300 find_amodule_symbol (amodule
, symbol
, (gpointer
*)&p
);
5303 /* Nothing to patch */
5306 info_offset
= *(guint32
*)p
;
5308 MonoTrampInfo
*tinfo
;
5309 guint32 code_size
, uw_info_len
, uw_offset
;
5311 /* Construct a MonoTrampInfo from the data in the AOT image */
5313 p
+= sizeof (guint32
);
5314 code_size
= *(guint32
*)p
;
5315 p
+= sizeof (guint32
);
5316 uw_offset
= *(guint32
*)p
;
5317 uw_info
= amodule
->unwind_info
+ uw_offset
;
5318 uw_info_len
= decode_value (uw_info
, &uw_info
);
5320 tinfo
= g_new0 (MonoTrampInfo
, 1);
5321 tinfo
->code
= (guint8
*)code
;
5322 tinfo
->code_size
= code_size
;
5323 tinfo
->uw_info_len
= uw_info_len
;
5325 tinfo
->uw_info
= uw_info
;
5330 p
= amodule
->blob
+ info_offset
;
5332 /* Similar to mono_aot_load_method () */
5334 n_patches
= decode_value (p
, &p
);
5337 MonoJumpInfo
*patches
;
5340 mp
= mono_mempool_new ();
5342 patches
= load_patch_info (amodule
, mp
, n_patches
, FALSE
, &got_slots
, p
, &p
);
5345 for (pindex
= 0; pindex
< n_patches
; ++pindex
) {
5346 MonoJumpInfo
*ji
= &patches
[pindex
];
5350 if (amodule
->got
[got_slots
[pindex
]])
5354 * When this code is executed, the runtime may not be initalized yet, so
5355 * resolve the patch info by hand.
5357 if (ji
->type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
5358 if (!strcmp (ji
->data
.name
, "mono_get_lmf_addr")) {
5359 target
= (gpointer
)mono_get_lmf_addr
;
5360 } else if (!strcmp (ji
->data
.name
, "mono_thread_force_interruption_checkpoint_noraise")) {
5361 target
= (gpointer
)mono_thread_force_interruption_checkpoint_noraise
;
5362 } else if (!strcmp (ji
->data
.name
, "mono_exception_from_token")) {
5363 target
= (gpointer
)mono_exception_from_token
;
5364 } else if (!strcmp (ji
->data
.name
, "mono_throw_exception")) {
5365 target
= (gpointer
)mono_get_throw_exception ();
5366 } else if (!strcmp (ji
->data
.name
, "mono_rethrow_preserve_exception")) {
5367 target
= (gpointer
)mono_get_rethrow_preserve_exception ();
5368 } else if (strstr (ji
->data
.name
, "trampoline_func_") == ji
->data
.name
) {
5369 MonoTrampolineType tramp_type2
= (MonoTrampolineType
)atoi (ji
->data
.name
+ strlen ("trampoline_func_"));
5370 target
= (gpointer
)mono_get_trampoline_func (tramp_type2
);
5371 } else if (strstr (ji
->data
.name
, "specific_trampoline_lazy_fetch_") == ji
->data
.name
) {
5372 /* atoll is needed because the the offset is unsigned */
5376 res
= sscanf (ji
->data
.name
, "specific_trampoline_lazy_fetch_%u", &slot
);
5377 g_assert (res
== 1);
5378 target
= mono_create_specific_trampoline (GUINT_TO_POINTER (slot
), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH
, mono_get_root_domain (), NULL
);
5379 target
= mono_create_ftnptr_malloc ((guint8
*)target
);
5380 } else if (!strcmp (ji
->data
.name
, "debugger_agent_single_step_from_context")) {
5381 target
= (gpointer
)mini_get_dbg_callbacks ()->single_step_from_context
;
5382 } else if (!strcmp (ji
->data
.name
, "debugger_agent_breakpoint_from_context")) {
5383 target
= (gpointer
)mini_get_dbg_callbacks ()->breakpoint_from_context
;
5384 } else if (!strcmp (ji
->data
.name
, "throw_exception_addr")) {
5385 target
= mono_get_throw_exception_addr ();
5386 } else if (!strcmp (ji
->data
.name
, "rethrow_preserve_exception_addr")) {
5387 target
= mono_get_rethrow_preserve_exception_addr ();
5388 } else if (strstr (ji
->data
.name
, "generic_trampoline_")) {
5389 target
= mono_aot_get_trampoline (ji
->data
.name
);
5390 } else if (aot_jit_icall_hash
&& g_hash_table_lookup (aot_jit_icall_hash
, ji
->data
.name
)) {
5391 /* Registered by mono_arch_init () */
5392 target
= g_hash_table_lookup (aot_jit_icall_hash
, ji
->data
.name
);
5394 fprintf (stderr
, "Unknown relocation '%s'\n", ji
->data
.name
);
5395 g_assert_not_reached ();
5399 /* Hopefully the code doesn't have patches which need method or
5402 target
= mono_resolve_patch_target (NULL
, NULL
, (guint8
*)code
, ji
, FALSE
, error
);
5403 mono_error_assert_ok (error
);
5407 amodule
->got
[got_slots
[pindex
]] = target
;
5412 mono_mempool_destroy (mp
);
5419 load_function (MonoAotModule
*amodule
, const char *name
)
5421 return load_function_full (amodule
, name
, NULL
);
5424 static MonoAotModule
*
5425 get_mscorlib_aot_module (void)
5428 MonoAotModule
*amodule
;
5430 image
= mono_defaults
.corlib
;
5432 amodule
= image
->aot_module
;
5434 amodule
= mscorlib_aot_module
;
5440 no_trampolines (void)
5442 g_assert_not_reached ();
5446 * Return the trampoline identified by NAME from the mscorlib AOT file.
5447 * On ppc64, this returns a function descriptor.
5450 mono_aot_get_trampoline_full (const char *name
, MonoTrampInfo
**out_tinfo
)
5452 MonoAotModule
*amodule
= get_mscorlib_aot_module ();
5454 if (mono_llvm_only
) {
5456 return (gpointer
)no_trampolines
;
5459 return mono_create_ftnptr_malloc ((guint8
*)load_function_full (amodule
, name
, out_tinfo
));
5463 (mono_aot_get_trampoline
) (const char *name
)
5465 MonoTrampInfo
*out_tinfo
;
5468 code
= mono_aot_get_trampoline_full (name
, &out_tinfo
);
5469 mono_aot_tramp_info_register (out_tinfo
, NULL
);
5475 read_unwind_info (MonoAotModule
*amodule
, MonoTrampInfo
*info
, const char *symbol_name
)
5477 gpointer symbol_addr
;
5478 guint32 uw_offset
, uw_info_len
;
5481 find_amodule_symbol (amodule
, symbol_name
, &symbol_addr
);
5486 uw_offset
= *(guint32
*)symbol_addr
;
5487 uw_info
= amodule
->unwind_info
+ uw_offset
;
5488 uw_info_len
= decode_value (uw_info
, &uw_info
);
5490 info
->uw_info_len
= uw_info_len
;
5492 info
->uw_info
= uw_info
;
5494 info
->uw_info
= NULL
;
5496 /* If successful return the address of the following data */
5497 return (guint32
*)symbol_addr
+ 1;
5501 #include <mach/mach.h>
5503 static TrampolinePage
* trampoline_pages
[MONO_AOT_TRAMP_NUM
];
5506 read_page_trampoline_uwinfo (MonoTrampInfo
*info
, int tramp_type
, gboolean is_generic
)
5508 char symbol_name
[128];
5510 if (tramp_type
== MONO_AOT_TRAMP_SPECIFIC
)
5511 sprintf (symbol_name
, "specific_trampolines_page_%s_p", is_generic
? "gen" : "sp");
5512 else if (tramp_type
== MONO_AOT_TRAMP_STATIC_RGCTX
)
5513 sprintf (symbol_name
, "rgctx_trampolines_page_%s_p", is_generic
? "gen" : "sp");
5514 else if (tramp_type
== MONO_AOT_TRAMP_IMT
)
5515 sprintf (symbol_name
, "imt_trampolines_page_%s_p", is_generic
? "gen" : "sp");
5516 else if (tramp_type
== MONO_AOT_TRAMP_GSHAREDVT_ARG
)
5517 sprintf (symbol_name
, "gsharedvt_trampolines_page_%s_p", is_generic
? "gen" : "sp");
5518 else if (tramp_type
== MONO_AOT_TRAMP_UNBOX_ARBITRARY
)
5519 sprintf (symbol_name
, "unbox_arbitrary_trampolines_page_%s_p", is_generic
? "gen" : "sp");
5521 g_assert_not_reached ();
5523 read_unwind_info (mono_defaults
.corlib
->aot_module
, info
, symbol_name
);
5526 static unsigned char*
5527 get_new_trampoline_from_page (int tramp_type
)
5529 MonoAotModule
*amodule
;
5531 TrampolinePage
*page
;
5534 vm_address_t addr
, taddr
;
5536 vm_prot_t prot
, max_prot
;
5537 int psize
, specific_trampoline_size
;
5538 unsigned char *code
;
5540 specific_trampoline_size
= 2 * sizeof (gpointer
);
5542 mono_aot_page_lock ();
5543 page
= trampoline_pages
[tramp_type
];
5544 if (page
&& page
->trampolines
< page
->trampolines_end
) {
5545 code
= page
->trampolines
;
5546 page
->trampolines
+= specific_trampoline_size
;
5547 mono_aot_page_unlock ();
5550 mono_aot_page_unlock ();
5551 /* the trampoline template page is in the mscorlib module */
5552 image
= mono_defaults
.corlib
;
5555 psize
= MONO_AOT_TRAMP_PAGE_SIZE
;
5557 amodule
= image
->aot_module
;
5560 if (tramp_type
== MONO_AOT_TRAMP_SPECIFIC
)
5561 tpage
= load_function (amodule
, "specific_trampolines_page");
5562 else if (tramp_type
== MONO_AOT_TRAMP_STATIC_RGCTX
)
5563 tpage
= load_function (amodule
, "rgctx_trampolines_page");
5564 else if (tramp_type
== MONO_AOT_TRAMP_IMT
)
5565 tpage
= load_function (amodule
, "imt_trampolines_page");
5566 else if (tramp_type
== MONO_AOT_TRAMP_GSHAREDVT_ARG
)
5567 tpage
= load_function (amodule
, "gsharedvt_arg_trampolines_page");
5568 else if (tramp_type
== MONO_AOT_TRAMP_UNBOX_ARBITRARY
)
5569 tpage
= load_function (amodule
, "unbox_arbitrary_trampolines_page");
5571 g_error ("Incorrect tramp type for trampolines page");
5573 /*g_warning ("loaded trampolines page at %x", tpage);*/
5575 /* avoid the unlikely case of looping forever */
5578 while (page
== NULL
&& count
-- > 0) {
5579 MonoTrampInfo
*gen_info
, *sp_info
;
5582 /* allocate two contiguous pages of memory: the first page will contain the data (like a local constant pool)
5583 * while the second will contain the trampolines.
5586 ret
= vm_allocate (mach_task_self (), &addr
, psize
* 2, VM_FLAGS_ANYWHERE
);
5587 } while (ret
== KERN_ABORTED
);
5588 if (ret
!= KERN_SUCCESS
) {
5589 g_error ("Cannot allocate memory for trampolines: %d", ret
);
5592 /*g_warning ("allocated trampoline double page at %x", addr);*/
5593 /* replace the second page with a remapped trampoline page */
5594 taddr
= addr
+ psize
;
5595 vm_deallocate (mach_task_self (), taddr
, psize
);
5596 ret
= vm_remap (mach_task_self (), &taddr
, psize
, 0, FALSE
, mach_task_self(), (vm_address_t
)tpage
, FALSE
, &prot
, &max_prot
, VM_INHERIT_SHARE
);
5597 if (ret
!= KERN_SUCCESS
) {
5598 /* someone else got the page, try again */
5599 vm_deallocate (mach_task_self (), addr
, psize
);
5602 /*g_warning ("remapped trampoline page at %x", taddr);*/
5604 mono_aot_page_lock ();
5605 page
= trampoline_pages
[tramp_type
];
5606 /* some other thread already allocated, so use that to avoid wasting memory */
5607 if (page
&& page
->trampolines
< page
->trampolines_end
) {
5608 code
= page
->trampolines
;
5609 page
->trampolines
+= specific_trampoline_size
;
5610 mono_aot_page_unlock ();
5611 vm_deallocate (mach_task_self (), addr
, psize
);
5612 vm_deallocate (mach_task_self (), taddr
, psize
);
5615 page
= (TrampolinePage
*)addr
;
5616 page
->next
= trampoline_pages
[tramp_type
];
5617 trampoline_pages
[tramp_type
] = page
;
5618 page
->trampolines
= (guint8
*)(taddr
+ amodule
->info
.tramp_page_code_offsets
[tramp_type
]);
5619 page
->trampolines_end
= (guint8
*)(taddr
+ psize
- 64);
5620 code
= page
->trampolines
;
5621 page
->trampolines
+= specific_trampoline_size
;
5622 mono_aot_page_unlock ();
5624 /* Register the generic part at the beggining of the trampoline page */
5625 gen_info
= mono_tramp_info_create (NULL
, (guint8
*)taddr
, amodule
->info
.tramp_page_code_offsets
[tramp_type
], NULL
, NULL
);
5626 read_page_trampoline_uwinfo (gen_info
, tramp_type
, TRUE
);
5627 mono_aot_tramp_info_register (gen_info
, NULL
);
5630 * Registering each specific trampoline produces a lot of
5631 * MonoJitInfo structures. Jump trampolines are also registered
5634 if (tramp_type
!= MONO_AOT_TRAMP_SPECIFIC
) {
5635 /* Register the rest of the page as a single trampoline */
5636 sp_info
= mono_tramp_info_create (NULL
, code
, page
->trampolines_end
- code
, NULL
, NULL
);
5637 read_page_trampoline_uwinfo (sp_info
, tramp_type
, FALSE
);
5638 mono_aot_tramp_info_register (sp_info
, NULL
);
5642 g_error ("Cannot allocate more trampoline pages: %d", ret
);
5647 static unsigned char*
5648 get_new_trampoline_from_page (int tramp_type
)
5650 g_error ("Page trampolines not supported.");
5657 get_new_specific_trampoline_from_page (gpointer tramp
, gpointer arg
)
5662 code
= get_new_trampoline_from_page (MONO_AOT_TRAMP_SPECIFIC
);
5664 data
= (gpointer
*)((char*)code
- MONO_AOT_TRAMP_PAGE_SIZE
);
5667 /*g_warning ("new trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
5673 get_new_rgctx_trampoline_from_page (gpointer tramp
, gpointer arg
)
5678 code
= get_new_trampoline_from_page (MONO_AOT_TRAMP_STATIC_RGCTX
);
5680 data
= (gpointer
*)((char*)code
- MONO_AOT_TRAMP_PAGE_SIZE
);
5683 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
5689 get_new_imt_trampoline_from_page (gpointer arg
)
5694 code
= get_new_trampoline_from_page (MONO_AOT_TRAMP_IMT
);
5696 data
= (gpointer
*)((char*)code
- MONO_AOT_TRAMP_PAGE_SIZE
);
5698 /*g_warning ("new imt trampoline at %p for data %p, (stored at %p)", code, arg, data);*/
5704 get_new_gsharedvt_arg_trampoline_from_page (gpointer tramp
, gpointer arg
)
5709 code
= get_new_trampoline_from_page (MONO_AOT_TRAMP_GSHAREDVT_ARG
);
5711 data
= (gpointer
*)((char*)code
- MONO_AOT_TRAMP_PAGE_SIZE
);
5714 /*g_warning ("new rgctx trampoline at %p for data %p, tramp %p (stored at %p)", code, arg, tramp, data);*/
5719 get_new_unbox_arbitrary_trampoline_frome_page (gpointer addr
)
5724 code
= get_new_trampoline_from_page (MONO_AOT_TRAMP_UNBOX_ARBITRARY
);
5726 data
= (gpointer
*)((char*)code
- MONO_AOT_TRAMP_PAGE_SIZE
);
5732 /* Return a given kind of trampoline */
5733 /* FIXME set unwind info for these trampolines */
5735 get_numerous_trampoline (MonoAotTrampoline tramp_type
, int n_got_slots
, MonoAotModule
**out_amodule
, guint32
*got_offset
, guint32
*out_tramp_size
)
5738 MonoAotModule
*amodule
= get_mscorlib_aot_module ();
5739 int index
, tramp_size
;
5741 /* Currently, we keep all trampolines in the mscorlib AOT image */
5742 image
= mono_defaults
.corlib
;
5744 *out_amodule
= amodule
;
5749 #define MONOTOUCH_TRAMPOLINES_ERROR ". See http://docs.xamarin.com/ios/troubleshooting for instructions on how to fix this condition."
5751 #define MONOTOUCH_TRAMPOLINES_ERROR ""
5753 if (amodule
->trampoline_index
[tramp_type
] == amodule
->info
.num_trampolines
[tramp_type
]) {
5754 g_error ("Ran out of trampolines of type %d in '%s' (limit %d)%s\n",
5755 tramp_type
, image
? image
->name
: MONO_ASSEMBLY_CORLIB_NAME
, amodule
->info
.num_trampolines
[tramp_type
], MONOTOUCH_TRAMPOLINES_ERROR
);
5757 index
= amodule
->trampoline_index
[tramp_type
] ++;
5761 *got_offset
= amodule
->info
.trampoline_got_offset_base
[tramp_type
] + (index
* n_got_slots
);
5763 tramp_size
= amodule
->info
.trampoline_size
[tramp_type
];
5766 *out_tramp_size
= tramp_size
;
5768 return amodule
->trampolines
[tramp_type
] + (index
* tramp_size
);
5772 no_specific_trampoline (void)
5774 g_assert_not_reached ();
5778 * Return a specific trampoline from the AOT file.
5781 mono_aot_create_specific_trampoline (MonoImage
*image
, gpointer arg1
, MonoTrampolineType tramp_type
, MonoDomain
*domain
, guint32
*code_len
)
5783 MonoAotModule
*amodule
;
5784 guint32 got_offset
, tramp_size
;
5785 guint8
*code
, *tramp
;
5786 static gpointer generic_trampolines
[MONO_TRAMPOLINE_NUM
];
5787 static gboolean inited
;
5788 static guint32 num_trampolines
;
5790 if (mono_llvm_only
) {
5792 return (gpointer
)no_specific_trampoline
;
5799 mono_counters_register ("Specific trampolines", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &num_trampolines
);
5808 if (!generic_trampolines
[tramp_type
]) {
5811 symbol
= mono_get_generic_trampoline_name (tramp_type
);
5812 generic_trampolines
[tramp_type
] = mono_aot_get_trampoline (symbol
);
5816 tramp
= (guint8
*)generic_trampolines
[tramp_type
];
5819 if (USE_PAGE_TRAMPOLINES
) {
5820 code
= (guint8
*)get_new_specific_trampoline_from_page (tramp
, arg1
);
5823 code
= (guint8
*)get_numerous_trampoline (MONO_AOT_TRAMP_SPECIFIC
, 2, &amodule
, &got_offset
, &tramp_size
);
5825 amodule
->got
[got_offset
] = tramp
;
5826 amodule
->got
[got_offset
+ 1] = arg1
;
5830 *code_len
= tramp_size
;
5836 mono_aot_get_static_rgctx_trampoline (gpointer ctx
, gpointer addr
)
5838 MonoAotModule
*amodule
;
5842 if (USE_PAGE_TRAMPOLINES
) {
5843 code
= (guint8
*)get_new_rgctx_trampoline_from_page (addr
, ctx
);
5845 code
= (guint8
*)get_numerous_trampoline (MONO_AOT_TRAMP_STATIC_RGCTX
, 2, &amodule
, &got_offset
, NULL
);
5847 amodule
->got
[got_offset
] = ctx
;
5848 amodule
->got
[got_offset
+ 1] = addr
;
5851 /* The caller expects an ftnptr */
5852 return mono_create_ftnptr (mono_domain_get (), code
);
5856 mono_aot_get_unbox_arbitrary_trampoline (gpointer addr
)
5858 MonoAotModule
*amodule
;
5862 if (USE_PAGE_TRAMPOLINES
) {
5863 code
= (guint8
*)get_new_unbox_arbitrary_trampoline_frome_page (addr
);
5865 code
= (guint8
*)get_numerous_trampoline (MONO_AOT_TRAMP_UNBOX_ARBITRARY
, 1, &amodule
, &got_offset
, NULL
);
5866 amodule
->got
[got_offset
] = addr
;
5869 /* The caller expects an ftnptr */
5870 return mono_create_ftnptr (mono_domain_get (), code
);
5874 i32_idx_comparer (const void *key
, const void *member
)
5876 gint32 idx1
= GPOINTER_TO_INT (key
);
5877 gint32 idx2
= *(gint32
*)member
;
5882 i16_idx_comparer (const void *key
, const void *member
)
5884 int idx1
= GPOINTER_TO_INT (key
);
5885 int idx2
= *(gint16
*)member
;
5890 aot_is_slim_amodule (MonoAotModule
*amodule
)
5895 /* "slim" only applies to mscorlib.dll */
5896 if (strcmp (amodule
->aot_name
, "mscorlib"))
5899 guint32 f
= amodule
->info
.flags
;
5900 return (f
& MONO_AOT_FILE_FLAG_INTERP
) && !(f
& MONO_AOT_FILE_FLAG_FULL_AOT
);
5904 mono_aot_get_unbox_trampoline (MonoMethod
*method
, gpointer addr
)
5907 guint32 method_index
= mono_metadata_token_index (method
->token
) - 1;
5908 MonoAotModule
*amodule
;
5910 guint32
*ut
, *ut_end
, *entry
;
5911 int low
, high
, entry_index
= 0;
5912 MonoTrampInfo
*tinfo
;
5914 if (method
->is_inflated
&& !mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, FALSE
)) {
5915 method_index
= find_aot_method (method
, &amodule
);
5916 if (method_index
== 0xffffff && mono_method_is_generic_sharable_full (method
, FALSE
, TRUE
, FALSE
)) {
5917 MonoMethod
*shared
= mini_get_shared_method_full (method
, SHARE_MODE_NONE
, error
);
5918 mono_error_assert_ok (error
);
5919 method_index
= find_aot_method (shared
, &amodule
);
5921 if (method_index
== 0xffffff && mono_method_is_generic_sharable_full (method
, FALSE
, TRUE
, TRUE
)) {
5922 MonoMethod
*shared
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
5923 mono_error_assert_ok (error
);
5925 method_index
= find_aot_method (shared
, &amodule
);
5928 amodule
= m_class_get_image (method
->klass
)->aot_module
;
5930 if (amodule
== NULL
|| method_index
== 0xffffff || aot_is_slim_amodule (amodule
)) {
5931 /* couldn't find unbox trampoline specifically generated for that
5932 * method. this should only happen when an unbox trampoline is needed
5933 * for `fullAOT code -> native-to-interp -> interp` transition if
5934 * (1) it's a virtual call
5935 * (2) the receiver is a value type, thus needs unboxing */
5936 g_assert (mono_use_interpreter
);
5937 return mono_aot_get_unbox_arbitrary_trampoline (addr
);
5940 if (amodule
->info
.llvm_unbox_tramp_indexes
) {
5941 int unbox_tramp_idx
;
5943 /* Search the llvm_unbox_tramp_indexes table using a binary search */
5944 if (amodule
->info
.llvm_unbox_tramp_elemsize
== sizeof (guint32
)) {
5945 void *ptr
= mono_binary_search (GINT_TO_POINTER (method_index
), amodule
->info
.llvm_unbox_tramp_indexes
, amodule
->info
.llvm_unbox_tramp_num
, amodule
->info
.llvm_unbox_tramp_elemsize
, i32_idx_comparer
);
5947 g_assert (*(int*)ptr
== method_index
);
5948 unbox_tramp_idx
= (guint32
*)ptr
- (guint32
*)amodule
->info
.llvm_unbox_tramp_indexes
;
5950 void *ptr
= mono_binary_search (GINT_TO_POINTER (method_index
), amodule
->info
.llvm_unbox_tramp_indexes
, amodule
->info
.llvm_unbox_tramp_num
, amodule
->info
.llvm_unbox_tramp_elemsize
, i16_idx_comparer
);
5952 g_assert (*(gint16
*)ptr
== method_index
);
5953 unbox_tramp_idx
= (guint16
*)ptr
- (guint16
*)amodule
->info
.llvm_unbox_tramp_indexes
;
5955 g_assert (unbox_tramp_idx
< amodule
->info
.llvm_unbox_tramp_num
);
5956 code
= ((gpointer
*)(amodule
->info
.llvm_unbox_trampolines
))[unbox_tramp_idx
];
5961 if (amodule
->info
.llvm_get_unbox_tramp
) {
5962 gpointer (*get_tramp
) (int) = (gpointer (*)(int))amodule
->info
.llvm_get_unbox_tramp
;
5963 code
= get_tramp (method_index
);
5969 ut
= amodule
->unbox_trampolines
;
5970 ut_end
= amodule
->unbox_trampolines_end
;
5972 /* Do a binary search in the sorted table */
5975 high
= (ut_end
- ut
);
5976 while (low
< high
) {
5977 entry_index
= (low
+ high
) / 2;
5978 entry
= &ut
[entry_index
];
5979 if (entry
[0] < method_index
) {
5980 low
= entry_index
+ 1;
5981 } else if (entry
[0] > method_index
) {
5988 code
= get_call_table_entry (amodule
->unbox_trampoline_addresses
, entry_index
);
5991 tinfo
= mono_tramp_info_create (NULL
, (guint8
*)code
, 0, NULL
, NULL
);
5993 gpointer
const symbol_addr
= read_unwind_info (amodule
, tinfo
, "unbox_trampoline_p");
5995 mono_tramp_info_free (tinfo
);
5999 tinfo
->code_size
= *(guint32
*)symbol_addr
;
6000 mono_aot_tramp_info_register (tinfo
, NULL
);
6002 /* The caller expects an ftnptr */
6003 return mono_create_ftnptr (mono_domain_get (), code
);
6007 mono_aot_get_lazy_fetch_trampoline (guint32 slot
)
6011 MonoAotModule
*amodule
= mono_defaults
.corlib
->aot_module
;
6012 guint32 index
= MONO_RGCTX_SLOT_INDEX (slot
);
6013 static int count
= 0;
6016 if (index
>= amodule
->info
.num_rgctx_fetch_trampolines
) {
6017 static gpointer addr
;
6021 * Use the general version of the rgctx fetch trampoline. It receives a pair of <slot, trampoline> in the rgctx arg reg.
6024 addr
= load_function (amodule
, "rgctx_fetch_trampoline_general");
6025 info
= (void **)mono_domain_alloc0 (mono_get_root_domain (), sizeof (gpointer
) * 2);
6026 info
[0] = GUINT_TO_POINTER (slot
);
6027 info
[1] = mono_create_specific_trampoline (GUINT_TO_POINTER (slot
), MONO_TRAMPOLINE_RGCTX_LAZY_FETCH
, mono_get_root_domain (), NULL
);
6028 code
= mono_aot_get_static_rgctx_trampoline (info
, addr
);
6029 return mono_create_ftnptr (mono_domain_get (), code
);
6032 symbol
= mono_get_rgctx_fetch_trampoline_name (slot
);
6033 code
= load_function (mono_defaults
.corlib
->aot_module
, symbol
);
6035 /* The caller expects an ftnptr */
6036 return mono_create_ftnptr (mono_domain_get (), code
);
6040 no_imt_trampoline (void)
6042 g_assert_not_reached ();
6046 mono_aot_get_imt_trampoline (MonoVTable
*vtable
, MonoDomain
*domain
, MonoIMTCheckItem
**imt_entries
, int count
, gpointer fail_tramp
)
6051 int i
, index
, real_count
;
6052 MonoAotModule
*amodule
;
6055 return (gpointer
)no_imt_trampoline
;
6058 for (i
= 0; i
< count
; ++i
) {
6059 MonoIMTCheckItem
*item
= imt_entries
[i
];
6061 if (item
->is_equals
)
6065 /* Save the entries into an array */
6066 buf
= (void **)mono_domain_alloc (domain
, (real_count
+ 1) * 2 * sizeof (gpointer
));
6068 for (i
= 0; i
< count
; ++i
) {
6069 MonoIMTCheckItem
*item
= imt_entries
[i
];
6071 if (!item
->is_equals
)
6074 g_assert (item
->key
);
6076 buf
[(index
* 2)] = item
->key
;
6077 if (item
->has_target_code
) {
6078 gpointer
*p
= (gpointer
*)mono_domain_alloc (domain
, sizeof (gpointer
));
6079 *p
= item
->value
.target_code
;
6080 buf
[(index
* 2) + 1] = p
;
6082 buf
[(index
* 2) + 1] = &(vtable
->vtable
[item
->value
.vtable_slot
]);
6086 buf
[(index
* 2)] = NULL
;
6087 buf
[(index
* 2) + 1] = fail_tramp
;
6089 if (USE_PAGE_TRAMPOLINES
) {
6090 code
= get_new_imt_trampoline_from_page (buf
);
6092 code
= get_numerous_trampoline (MONO_AOT_TRAMP_IMT
, 1, &amodule
, &got_offset
, NULL
);
6094 amodule
->got
[got_offset
] = buf
;
6101 mono_aot_get_gsharedvt_arg_trampoline (gpointer arg
, gpointer addr
)
6103 MonoAotModule
*amodule
;
6107 if (USE_PAGE_TRAMPOLINES
) {
6108 code
= (guint8
*)get_new_gsharedvt_arg_trampoline_from_page (addr
, arg
);
6110 code
= (guint8
*)get_numerous_trampoline (MONO_AOT_TRAMP_GSHAREDVT_ARG
, 2, &amodule
, &got_offset
, NULL
);
6112 amodule
->got
[got_offset
] = arg
;
6113 amodule
->got
[got_offset
+ 1] = addr
;
6116 /* The caller expects an ftnptr */
6117 return mono_create_ftnptr (mono_domain_get (), code
);
6120 #ifdef MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE
6122 mono_aot_get_ftnptr_arg_trampoline (gpointer arg
, gpointer addr
)
6124 MonoAotModule
*amodule
;
6128 if (USE_PAGE_TRAMPOLINES
) {
6129 g_error ("FIXME: ftnptr_arg page trampolines");
6131 code
= (guint8
*)get_numerous_trampoline (MONO_AOT_TRAMP_FTNPTR_ARG
, 2, &amodule
, &got_offset
, NULL
);
6133 amodule
->got
[got_offset
] = arg
;
6134 amodule
->got
[got_offset
+ 1] = addr
;
6137 /* The caller expects an ftnptr */
6138 return mono_create_ftnptr (mono_domain_get (), code
);
6144 * mono_aot_set_make_unreadable:
6146 * Set whenever to make all mmaped memory unreadable. In conjuction with a
6147 * SIGSEGV handler, this is useful to find out which pages the runtime tries to read.
6150 mono_aot_set_make_unreadable (gboolean unreadable
)
6154 make_unreadable
= unreadable
;
6156 if (make_unreadable
&& !inited
) {
6157 mono_counters_register ("AOT: pagefaults", MONO_COUNTER_JIT
| MONO_COUNTER_INT
, &n_pagefaults
);
6162 MonoAotModule
*module
;
6167 find_map (gpointer key
, gpointer value
, gpointer user_data
)
6169 MonoAotModule
*module
= (MonoAotModule
*)value
;
6170 FindMapUserData
*data
= (FindMapUserData
*)user_data
;
6173 if ((data
->ptr
>= module
->mem_begin
) && (data
->ptr
< module
->mem_end
))
6174 data
->module
= module
;
6177 static MonoAotModule
*
6178 find_module_for_addr (void *ptr
)
6180 FindMapUserData data
;
6182 if (!make_unreadable
)
6186 data
.ptr
= (guint8
*)ptr
;
6189 g_hash_table_foreach (aot_modules
, (GHFunc
)find_map
, &data
);
6196 * mono_aot_is_pagefault:
6198 * Should be called from a SIGSEGV signal handler to find out whenever @ptr is
6199 * within memory allocated by this module.
6202 mono_aot_is_pagefault (void *ptr
)
6204 if (!make_unreadable
)
6208 * Not signal safe, but SIGSEGV's are synchronous, and
6209 * this is only turned on by a MONO_DEBUG option.
6211 return find_module_for_addr (ptr
) != NULL
;
6215 * mono_aot_handle_pagefault:
6217 * Handle a pagefault caused by an unreadable page by making it readable again.
6220 mono_aot_handle_pagefault (void *ptr
)
6223 guint8
* start
= (guint8
*)ROUND_DOWN (((gssize
)ptr
), mono_pagesize ());
6227 res
= mono_mprotect (start
, mono_pagesize (), MONO_MMAP_READ
|MONO_MMAP_WRITE
|MONO_MMAP_EXEC
);
6228 g_assert (res
== 0);
6236 mono_aot_get_method_flags (guint8
*code
)
6240 if (!code_to_method_flags
)
6241 return MONO_AOT_METHOD_FLAG_NONE
;
6243 /* Not found and no FLAG_NONE are the same, but its not a problem */
6244 flags
= GPOINTER_TO_UINT (g_hash_table_lookup (code_to_method_flags
, code
));
6246 return (MonoAotMethodFlags
)flags
;
6253 mono_aot_init (void)
6258 mono_aot_cleanup (void)
6263 mono_aot_find_method_index (MonoMethod
*method
)
6265 g_assert_not_reached ();
6270 mono_aot_init_llvm_method (gpointer aot_module
, guint32 method_index
)
6275 mono_aot_init_gshared_method_this (gpointer aot_module
, guint32 method_index
, MonoObject
*this)
6280 mono_aot_init_gshared_method_mrgctx (gpointer aot_module
, guint32 method_index
, MonoMethodRuntimeGenericContext
*rgctx
)
6285 mono_aot_init_gshared_method_vtable (gpointer aot_module
, guint32 method_index
, MonoVTable
*vtable
)
6290 mono_aot_get_method (MonoDomain
*domain
,
6291 MonoMethod
*method
, MonoError
*error
)
6298 mono_aot_is_got_entry (guint8
*code
, guint8
*addr
)
6304 mono_aot_get_cached_class_info (MonoClass
*klass
, MonoCachedClassInfo
*res
)
6310 mono_aot_get_class_from_name (MonoImage
*image
, const char *name_space
, const char *name
, MonoClass
**klass
)
6316 mono_aot_find_jit_info (MonoDomain
*domain
, MonoImage
*image
, gpointer addr
)
6322 mono_aot_get_method_from_token (MonoDomain
*domain
, MonoImage
*image
, guint32 token
, MonoError
*error
)
6329 mono_aot_get_plt_entry (guint8
*code
)
6335 mono_aot_plt_resolve (gpointer aot_module
, guint32 plt_info_offset
, guint8
*code
, MonoError
*error
)
6341 mono_aot_patch_plt_entry (guint8
*code
, guint8
*plt_entry
, gpointer
*got
, host_mgreg_t
*regs
, guint8
*addr
)
6346 mono_aot_get_method_from_vt_slot (MonoDomain
*domain
, MonoVTable
*vtable
, int slot
, MonoError
*error
)
6354 mono_aot_get_plt_info_offset (host_mgreg_t
*regs
, guint8
*code
)
6356 g_assert_not_reached ();
6362 mono_aot_create_specific_trampoline (MonoImage
*image
, gpointer arg1
, MonoTrampolineType tramp_type
, MonoDomain
*domain
, guint32
*code_len
)
6364 g_assert_not_reached ();
6369 mono_aot_get_static_rgctx_trampoline (gpointer ctx
, gpointer addr
)
6371 g_assert_not_reached ();
6376 mono_aot_get_trampoline_full (const char *name
, MonoTrampInfo
**out_tinfo
)
6378 g_assert_not_reached ();
6383 mono_aot_get_trampoline (const char *name
)
6385 g_assert_not_reached ();
6390 mono_aot_get_unbox_arbitrary_trampoline (gpointer addr
)
6392 g_assert_not_reached ();
6397 mono_aot_get_unbox_trampoline (MonoMethod
*method
, gpointer addr
)
6399 g_assert_not_reached ();
6404 mono_aot_get_lazy_fetch_trampoline (guint32 slot
)
6406 g_assert_not_reached ();
6411 mono_aot_get_imt_trampoline (MonoVTable
*vtable
, MonoDomain
*domain
, MonoIMTCheckItem
**imt_entries
, int count
, gpointer fail_tramp
)
6413 g_assert_not_reached ();
6418 mono_aot_get_gsharedvt_arg_trampoline (gpointer arg
, gpointer addr
)
6420 g_assert_not_reached ();
6424 #ifdef MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE
6426 mono_aot_get_ftnptr_arg_trampoline (gpointer arg
, gpointer addr
)
6428 g_assert_not_reached ();
6434 mono_aot_set_make_unreadable (gboolean unreadable
)
6439 mono_aot_is_pagefault (void *ptr
)
6445 mono_aot_handle_pagefault (void *ptr
)
6450 mono_aot_get_unwind_info (MonoJitInfo
*ji
, guint32
*unwind_info_len
)
6452 g_assert_not_reached ();
6457 mono_aot_register_jit_icall (const char *name
, gpointer addr
)
6462 mono_aot_get_weak_field_indexes (MonoImage
*image
)
6468 mono_aot_get_method_flags (guint8
*code
)
6470 return MONO_AOT_METHOD_FLAG_NONE
;