3 * mono Ahead of Time compiler
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Zoltan Varga (vargaz@gmail.com)
8 * Johan Lorensson (lateralusx.github@gmail.com)
10 * (C) 2002 Ximian, Inc.
11 * Copyright 2003-2011 Novell, Inc
12 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
17 #include <sys/types.h>
37 #include <mono/metadata/abi-details.h>
38 #include <mono/metadata/tabledefs.h>
39 #include <mono/metadata/class.h>
40 #include <mono/metadata/object.h>
41 #include <mono/metadata/tokentype.h>
42 #include <mono/metadata/appdomain.h>
43 #include <mono/metadata/debug-helpers.h>
44 #include <mono/metadata/assembly.h>
45 #include <mono/metadata/metadata-internals.h>
46 #include <mono/metadata/reflection-internals.h>
47 #include <mono/metadata/marshal.h>
48 #include <mono/metadata/gc-internals.h>
49 #include <mono/metadata/mempool-internals.h>
50 #include <mono/metadata/mono-endian.h>
51 #include <mono/metadata/threads-types.h>
52 #include <mono/metadata/custom-attrs-internals.h>
53 #include <mono/utils/mono-logger-internals.h>
54 #include <mono/utils/mono-compiler.h>
55 #include <mono/utils/mono-time.h>
56 #include <mono/utils/mono-mmap.h>
57 #include <mono/utils/mono-rand.h>
58 #include <mono/utils/json.h>
59 #include <mono/utils/mono-threads-coop.h>
60 #include <mono/profiler/aot.h>
61 #include <mono/utils/w32api.h>
63 #include "aot-compiler.h"
64 #include "aot-runtime.h"
65 #include "seq-points.h"
66 #include "image-writer.h"
67 #include "dwarfwriter.h"
69 #include "mini-llvm.h"
70 #include "mini-runtime.h"
73 try_get_method_nofail (MonoClass
*klass
, const char *method_name
, int param_count
, int flags
)
77 result
= mono_class_get_method_from_name_checked (klass
, method_name
, param_count
, flags
, error
);
78 mono_error_assert_ok (error
);
82 // FIXME Consolidate the multiple functions named get_method_nofail.
84 get_method_nofail (MonoClass
*klass
, const char *method_name
, int param_count
, int flags
)
86 MonoMethod
*result
= try_get_method_nofail (klass
, method_name
, param_count
, flags
);
87 g_assertf (result
, "Expected to find method %s in klass %s", method_name
, m_class_get_name (klass
));
91 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
93 // Use MSVC toolchain, Clang for MSVC using MSVC codegen and linker, when compiling for AMD64
94 // targeting WIN32 platforms running AOT compiler on WIN32 platform with VS installation.
95 #if defined(TARGET_AMD64) && defined(TARGET_WIN32) && defined(HOST_WIN32) && defined(_MSC_VER)
96 #define TARGET_X86_64_WIN32_MSVC
99 #if defined(TARGET_X86_64_WIN32_MSVC)
100 #define TARGET_WIN32_MSVC
103 // Emit native unwind info on Windows platforms (different from DWARF). Emitted unwind info
104 // works when using the MSVC toolchain using Clang for MSVC codegen and linker. Only supported when
105 // compiling for AMD64 (Windows x64 platforms).
106 #if defined(TARGET_WIN32_MSVC) && defined(MONO_ARCH_HAVE_UNWIND_TABLE)
107 #define EMIT_WIN32_UNWIND_INFO
110 #if defined(TARGET_ANDROID) || defined(__linux__)
111 #define RODATA_REL_SECT ".data.rel.ro"
113 #define RODATA_REL_SECT ".text"
116 #if defined(__linux__)
117 #define RODATA_SECT ".rodata"
118 #elif defined(TARGET_MACH)
119 #define RODATA_SECT ".section __TEXT, __const"
120 #elif defined(TARGET_WIN32_MSVC)
121 #define RODATA_SECT ".rdata"
123 #define RODATA_SECT ".text"
126 #define TV_DECLARE(name) gint64 name
127 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
128 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
130 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
137 typedef struct ClassProfileData ClassProfileData
;
141 ClassProfileData
**argv
;
142 MonoGenericInst
*inst
;
145 struct ClassProfileData
{
146 ImageProfileData
*image
;
148 GInstProfileData
*inst
;
153 ClassProfileData
*klass
;
158 GInstProfileData
*inst
;
163 GHashTable
*images
, *classes
, *ginsts
, *methods
;
166 /* predefined values for static readonly fields without needed to run the .cctor */
167 typedef struct _ReadOnlyValue ReadOnlyValue
;
168 struct _ReadOnlyValue
{
171 int type
; /* to be used later for typechecking to prevent user errors */
180 static ReadOnlyValue
*readonly_values
;
182 typedef struct MonoAotOptions
{
186 GList
*profile_files
;
188 gboolean write_symbols
;
189 gboolean metadata_only
;
190 gboolean bind_to_runtime_version
;
194 gboolean static_link
;
198 gboolean dwarf_debug
;
200 gboolean log_generics
;
201 gboolean log_instances
;
202 gboolean gen_msym_dir
;
203 char *gen_msym_dir_path
;
204 gboolean direct_pinvoke
;
205 gboolean direct_icalls
;
206 gboolean no_direct_calls
;
207 gboolean use_trampolines_page
;
208 gboolean no_instances
;
209 // We are collecting inflated methods and emitting non-inflated
211 // The name of the assembly for which the AOT module is going to have all deduped methods moved to.
212 // When set, we are emitting inflated methods only
218 gboolean llvm_disable_self_init
;
221 int nrgctx_trampolines
;
222 int nimt_trampolines
;
223 int ngsharedvt_arg_trampolines
;
224 int nftnptr_arg_trampolines
;
225 int nrgctx_fetch_trampolines
;
226 int nunbox_arbitrary_trampolines
;
227 gboolean print_skipped_methods
;
230 gboolean deterministic
;
236 char *instances_logfile_path
;
241 gboolean use_current_cpu
;
243 gboolean profile_only
;
251 METHOD_CAT_GSHAREDVT
,
257 typedef struct MonoAotStats
{
258 int ccount
, mcount
, lmfcount
, abscount
, gcount
, ocount
, genericcount
;
259 gint64 code_size
, method_info_size
, ex_info_size
, unwind_info_size
, got_size
, class_info_size
, got_info_size
, plt_size
, blob_size
;
260 int methods_without_got_slots
, direct_calls
, all_calls
, llvm_count
;
261 int got_slots
, offsets_size
;
262 int method_categories
[METHOD_CAT_NUM
];
263 int got_slot_types
[MONO_PATCH_INFO_NUM
];
264 int got_slot_info_sizes
[MONO_PATCH_INFO_NUM
];
265 int jit_time
, gen_time
, link_time
;
266 int method_ref_count
, method_ref_size
;
267 int class_ref_count
, class_ref_size
;
268 int ginst_count
, ginst_size
;
271 typedef struct GotInfo
{
272 GHashTable
*patch_to_got_offset
;
273 GHashTable
**patch_to_got_offset_by_type
;
274 GPtrArray
*got_patches
;
277 #ifdef EMIT_WIN32_UNWIND_INFO
278 typedef struct _UnwindInfoSectionCacheItem
{
279 char *xdata_section_label
;
280 PUNWIND_INFO unwind_info
;
281 gboolean xdata_section_emitted
;
282 } UnwindInfoSectionCacheItem
;
285 typedef struct MonoAotCompile
{
288 GHashTable
*method_indexes
;
289 GHashTable
*method_depth
;
292 GHashTable
**patch_to_plt_entry
;
293 GHashTable
*plt_offset_to_entry
;
294 //GHashTable *patch_to_got_offset;
295 //GHashTable **patch_to_got_offset_by_type;
296 //GPtrArray *got_patches;
297 GotInfo got_info
, llvm_got_info
;
298 GHashTable
*image_hash
;
299 GHashTable
*method_to_cfg
;
300 GHashTable
*token_info_hash
;
301 GHashTable
*method_to_pinvoke_import
;
302 GHashTable
*method_to_external_icall_symbol_name
;
303 GPtrArray
*extra_methods
;
304 GPtrArray
*image_table
;
306 GPtrArray
*method_order
;
307 GHashTable
*dedup_stats
;
308 GHashTable
*dedup_cache
;
309 gboolean dedup_cache_changed
;
310 GHashTable
*export_names
;
311 /* Maps MonoClass* -> blob offset */
312 GHashTable
*klass_blob_hash
;
313 /* Maps MonoMethod* -> blob offset */
314 GHashTable
*method_blob_hash
;
315 /* Maps MonoGenericInst* -> blob offset */
316 GHashTable
*ginst_blob_hash
;
317 GHashTable
*gsharedvt_in_signatures
;
318 GHashTable
*gsharedvt_out_signatures
;
319 guint32
*plt_got_info_offsets
;
320 guint32 got_offset
, llvm_got_offset
, plt_offset
, plt_got_offset_base
, nshared_got_entries
;
321 /* Number of GOT entries reserved for trampolines */
322 guint32 num_trampoline_got_entries
;
323 guint32 tramp_page_size
;
325 guint32 table_offsets
[MONO_AOT_TABLE_NUM
];
326 guint32 num_trampolines
[MONO_AOT_TRAMP_NUM
];
327 guint32 trampoline_got_offset_base
[MONO_AOT_TRAMP_NUM
];
328 guint32 trampoline_size
[MONO_AOT_TRAMP_NUM
];
329 guint32 tramp_page_code_offsets
[MONO_AOT_TRAMP_NUM
];
331 MonoAotOptions aot_opts
;
333 int call_table_entry_size
;
334 guint32 nextra_methods
;
337 MonoMemPool
*mempool
;
340 char *static_linking_symbol
;
342 gboolean gas_line_numbers
;
343 /* Whenever to emit an object file directly from llc */
344 gboolean llvm_owriter
;
345 gboolean llvm_owriter_supported
;
347 MonoDwarfWriter
*dwarf
;
351 char *temp_dir_to_delete
;
355 GHashTable
*unwind_info_offsets
;
356 GPtrArray
*unwind_ops
;
357 guint32 unwind_info_offset
;
360 char *llvm_got_symbol
;
362 char *llvm_eh_frame_symbol
;
363 GHashTable
*method_label_hash
;
364 const char *temp_prefix
;
365 const char *user_symbol_prefix
;
366 const char *llvm_label_prefix
;
367 const char *inst_directive
;
369 guint32 label_generator
;
371 gboolean has_jitted_code
;
372 gboolean is_full_aot
;
373 gboolean dedup_collect_only
;
374 MonoAotFileFlags flags
;
375 MonoDynamicStream blob
;
376 gboolean blob_closed
;
377 GHashTable
*typespec_classes
;
380 char *assembly_name_sym
;
381 GHashTable
*plt_entry_debug_sym_cache
;
382 gboolean thumb_mixed
, need_no_dead_strip
, need_pt_gnu_stack
;
383 GHashTable
*ginst_hash
;
384 GHashTable
*dwarf_ln_filenames
;
385 gboolean global_symbols
;
386 int objc_selector_index
, objc_selector_index_2
;
387 GPtrArray
*objc_selectors
;
388 GHashTable
*objc_selector_to_index
;
390 GHashTable
*profile_methods
;
391 #ifdef EMIT_WIN32_UNWIND_INFO
392 GList
*unwind_info_section_cache
;
395 FILE *instances_logfile
;
399 // In this mode, we are emitting dedupable methods that we encounter
400 gboolean dedup_emit_mode
;
405 char *symbol
, *llvm_symbol
, *debug_sym
;
407 gboolean jit_used
, llvm_used
;
410 #define mono_acfg_lock(acfg) mono_os_mutex_lock (&((acfg)->mutex))
411 #define mono_acfg_unlock(acfg) mono_os_mutex_unlock (&((acfg)->mutex))
413 /* This points to the current acfg in LLVM mode */
414 static MonoAotCompile
*llvm_acfg
;
415 static MonoAotCompile
*current_acfg
;
417 /* Cache of decoded method external icall symbol names. */
418 /* Owned by acfg, but kept in this static as well since it is */
419 /* accessed by code paths not having access to acfg. */
420 static GHashTable
*method_to_external_icall_symbol_name
;
422 // This, instead of an array of pointers, to optimize away a pointer and a relocation per string.
423 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
424 #define MSGSTRFIELD1(line) str##line
425 static const struct msgstr_t
{
426 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
427 #include "patch-info.h"
430 #define PATCH_INFO(a,b) b,
431 #include "patch-info.h"
434 static const gint16 opidx
[] = {
435 #define PATCH_INFO(a,b) offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
436 #include "patch-info.h"
440 static G_GNUC_UNUSED
const char*
441 get_patch_name (int info
)
443 return (const char*)&opstr
+ opidx
[info
];
447 emit_aot_image (MonoAotCompile
*acfg
);
450 mono_flush_method_cache (MonoAotCompile
*acfg
);
453 mono_read_method_cache (MonoAotCompile
*acfg
);
456 get_unwind_info_offset (MonoAotCompile
*acfg
, guint8
*encoded
, guint32 encoded_len
);
459 get_plt_entry_debug_sym (MonoAotCompile
*acfg
, MonoJumpInfo
*ji
, GHashTable
*cache
);
462 add_gsharedvt_wrappers (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
, gboolean gsharedvt_in
, gboolean gsharedvt_out
, gboolean interp_in
);
465 add_profile_instances (MonoAotCompile
*acfg
, ProfileData
*data
);
468 ignore_cfg (MonoCompile
*cfg
)
470 return !cfg
|| cfg
->skip
;
474 aot_printf (MonoAotCompile
*acfg
, const gchar
*format
, ...)
480 output
= acfg
->logfile
;
484 va_start (args
, format
);
485 vfprintf (output
, format
, args
);
490 aot_printerrf (MonoAotCompile
*acfg
, const gchar
*format
, ...)
496 output
= acfg
->logfile
;
500 va_start (args
, format
);
501 vfprintf (output
, format
, args
);
506 report_loader_error (MonoAotCompile
*acfg
, MonoError
*error
, gboolean fatal
, const char *format
, ...)
515 output
= acfg
->logfile
;
519 va_start (args
, format
);
520 vfprintf (output
, format
, args
);
522 mono_error_cleanup (error
);
524 if (acfg
->is_full_aot
&& fatal
) {
525 fprintf (output
, "FullAOT cannot continue if there are loader errors.\n");
530 /* Wrappers around the image writer functions */
532 #define MAX_SYMBOL_SIZE 256
535 mangle_symbol (const char * symbol
, char * mangled_symbol
, gsize length
)
537 gsize needed_size
= length
;
539 g_assert (NULL
!= symbol
);
540 g_assert (NULL
!= mangled_symbol
);
541 g_assert (0 != length
);
543 #if defined(TARGET_WIN32) && defined(TARGET_X86)
544 if (symbol
&& '_' != symbol
[0]) {
545 needed_size
= g_snprintf (mangled_symbol
, length
, "_%s", symbol
);
547 needed_size
= g_snprintf (mangled_symbol
, length
, "%s", symbol
);
550 needed_size
= g_snprintf (mangled_symbol
, length
, "%s", symbol
);
553 g_assert (0 <= needed_size
&& needed_size
< length
);
554 return mangled_symbol
;
558 mangle_symbol_alloc (const char * symbol
)
560 g_assert (NULL
!= symbol
);
562 #if defined(TARGET_WIN32) && defined(TARGET_X86)
563 if (symbol
&& '_' != symbol
[0]) {
564 return g_strdup_printf ("_%s", symbol
);
567 return g_strdup_printf ("%s", symbol
);
570 return g_strdup_printf ("%s", symbol
);
575 emit_section_change (MonoAotCompile
*acfg
, const char *section_name
, int subsection_index
)
577 mono_img_writer_emit_section_change (acfg
->w
, section_name
, subsection_index
);
580 #if defined(TARGET_WIN32) && defined(TARGET_X86)
583 emit_local_symbol (MonoAotCompile
*acfg
, const char *name
, const char *end_label
, gboolean func
)
585 const char * mangled_symbol_name
= name
;
586 char * mangled_symbol_name_alloc
= NULL
;
589 mangled_symbol_name_alloc
= mangle_symbol_alloc (name
);
590 mangled_symbol_name
= mangled_symbol_name_alloc
;
593 if (name
!= mangled_symbol_name
&& 0 != g_strcasecmp (name
, mangled_symbol_name
)) {
594 mono_img_writer_emit_label (acfg
->w
, mangled_symbol_name
);
596 mono_img_writer_emit_local_symbol (acfg
->w
, mangled_symbol_name
, end_label
, func
);
598 if (NULL
!= mangled_symbol_name_alloc
) {
599 g_free (mangled_symbol_name_alloc
);
606 emit_local_symbol (MonoAotCompile
*acfg
, const char *name
, const char *end_label
, gboolean func
)
608 mono_img_writer_emit_local_symbol (acfg
->w
, name
, end_label
, func
);
614 emit_label (MonoAotCompile
*acfg
, const char *name
)
616 mono_img_writer_emit_label (acfg
->w
, name
);
620 emit_bytes (MonoAotCompile
*acfg
, const guint8
* buf
, int size
)
622 mono_img_writer_emit_bytes (acfg
->w
, buf
, size
);
626 emit_string (MonoAotCompile
*acfg
, const char *value
)
628 mono_img_writer_emit_string (acfg
->w
, value
);
632 emit_line (MonoAotCompile
*acfg
)
634 mono_img_writer_emit_line (acfg
->w
);
638 emit_alignment (MonoAotCompile
*acfg
, int size
)
640 mono_img_writer_emit_alignment (acfg
->w
, size
);
644 emit_alignment_code (MonoAotCompile
*acfg
, int size
)
646 if (acfg
->align_pad_value
)
647 mono_img_writer_emit_alignment_fill (acfg
->w
, size
, acfg
->align_pad_value
);
649 mono_img_writer_emit_alignment (acfg
->w
, size
);
653 emit_padding (MonoAotCompile
*acfg
, int size
)
658 if (acfg
->align_pad_value
) {
659 for (i
= 0; i
< 16; ++i
)
660 buf
[i
] = acfg
->align_pad_value
;
662 memset (buf
, 0, sizeof (buf
));
665 for (i
= 0; i
< size
; i
+= 16) {
667 emit_bytes (acfg
, buf
, size
- i
);
669 emit_bytes (acfg
, buf
, 16);
674 emit_pointer (MonoAotCompile
*acfg
, const char *target
)
676 mono_img_writer_emit_pointer (acfg
->w
, target
);
680 emit_pointer_2 (MonoAotCompile
*acfg
, const char *prefix
, const char *target
)
682 if (prefix
[0] != '\0') {
683 char *s
= g_strdup_printf ("%s%s", prefix
, target
);
684 mono_img_writer_emit_pointer (acfg
->w
, s
);
687 mono_img_writer_emit_pointer (acfg
->w
, target
);
692 emit_int16 (MonoAotCompile
*acfg
, int value
)
694 mono_img_writer_emit_int16 (acfg
->w
, value
);
698 emit_int32 (MonoAotCompile
*acfg
, int value
)
700 mono_img_writer_emit_int32 (acfg
->w
, value
);
704 emit_symbol_diff (MonoAotCompile
*acfg
, const char *end
, const char* start
, int offset
)
706 mono_img_writer_emit_symbol_diff (acfg
->w
, end
, start
, offset
);
710 emit_zero_bytes (MonoAotCompile
*acfg
, int num
)
712 mono_img_writer_emit_zero_bytes (acfg
->w
, num
);
716 emit_byte (MonoAotCompile
*acfg
, guint8 val
)
718 mono_img_writer_emit_byte (acfg
->w
, val
);
721 #if defined(TARGET_WIN32) && defined(TARGET_X86)
723 static G_GNUC_UNUSED
void
724 emit_global_inner (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
726 const char * mangled_symbol_name
= name
;
727 char * mangled_symbol_name_alloc
= NULL
;
729 mangled_symbol_name_alloc
= mangle_symbol_alloc (name
);
730 mangled_symbol_name
= mangled_symbol_name_alloc
;
732 if (0 != g_strcasecmp (name
, mangled_symbol_name
)) {
733 mono_img_writer_emit_label (acfg
->w
, mangled_symbol_name
);
735 mono_img_writer_emit_global (acfg
->w
, mangled_symbol_name
, func
);
737 if (NULL
!= mangled_symbol_name_alloc
) {
738 g_free (mangled_symbol_name_alloc
);
744 static G_GNUC_UNUSED
void
745 emit_global_inner (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
747 mono_img_writer_emit_global (acfg
->w
, name
, func
);
753 link_shared_library (MonoAotCompile
*acfg
)
755 return !acfg
->aot_opts
.static_link
&& !acfg
->aot_opts
.asm_only
;
759 add_to_global_symbol_table (MonoAotCompile
*acfg
)
761 #ifdef TARGET_WIN32_MSVC
762 return acfg
->aot_opts
.no_dlsym
|| link_shared_library (acfg
);
764 return acfg
->aot_opts
.no_dlsym
;
769 emit_global (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
771 if (add_to_global_symbol_table (acfg
))
772 g_ptr_array_add (acfg
->globals
, g_strdup (name
));
774 if (acfg
->aot_opts
.no_dlsym
) {
775 mono_img_writer_emit_local_symbol (acfg
->w
, name
, NULL
, func
);
777 emit_global_inner (acfg
, name
, func
);
782 emit_symbol_size (MonoAotCompile
*acfg
, const char *name
, const char *end_label
)
784 mono_img_writer_emit_symbol_size (acfg
->w
, name
, end_label
);
787 /* Emit a symbol which is referenced by the MonoAotFileInfo structure */
789 emit_info_symbol (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
791 char symbol
[MAX_SYMBOL_SIZE
];
794 emit_label (acfg
, name
);
795 /* LLVM generated code references this */
796 sprintf (symbol
, "%s%s%s", acfg
->user_symbol_prefix
, acfg
->global_prefix
, name
);
797 emit_label (acfg
, symbol
);
799 #ifndef TARGET_WIN32_MSVC
800 emit_global_inner (acfg
, symbol
, FALSE
);
802 emit_global_inner (acfg
, symbol
, func
);
805 emit_label (acfg
, name
);
810 emit_string_symbol (MonoAotCompile
*acfg
, const char *name
, const char *value
)
813 mono_llvm_emit_aot_data (name
, (guint8
*)value
, strlen (value
) + 1);
817 mono_img_writer_emit_section_change (acfg
->w
, RODATA_SECT
, 1);
819 /* On apple, all symbols need to be aligned to avoid warnings from ld */
820 emit_alignment (acfg
, 4);
822 mono_img_writer_emit_label (acfg
->w
, name
);
823 mono_img_writer_emit_string (acfg
->w
, value
);
826 static G_GNUC_UNUSED
void
827 emit_uleb128 (MonoAotCompile
*acfg
, guint32 value
)
830 guint8 b
= value
& 0x7f;
832 if (value
!= 0) /* more bytes to come */
838 static G_GNUC_UNUSED
void
839 emit_sleb128 (MonoAotCompile
*acfg
, gint64 value
)
842 gboolean negative
= (value
< 0);
849 /* the following is unnecessary if the
850 * implementation of >>= uses an arithmetic rather
851 * than logical shift for a signed left operand
855 value
|= - ((gint64
)1 <<(size
- 7));
856 /* sign bit of byte is second high order bit (0x40) */
857 if ((value
== 0 && !(byte
& 0x40)) ||
858 (value
== -1 && (byte
& 0x40)))
862 emit_byte (acfg
, byte
);
866 static G_GNUC_UNUSED
void
867 encode_uleb128 (guint32 value
, guint8
*buf
, guint8
**endbuf
)
872 guint8 b
= value
& 0x7f;
874 if (value
!= 0) /* more bytes to come */
882 static G_GNUC_UNUSED
void
883 encode_sleb128 (gint32 value
, guint8
*buf
, guint8
**endbuf
)
886 gboolean negative
= (value
< 0);
894 /* the following is unnecessary if the
895 * implementation of >>= uses an arithmetic rather
896 * than logical shift for a signed left operand
900 value
|= - (1 <<(size
- 7));
901 /* sign bit of byte is second high order bit (0x40) */
902 if ((value
== 0 && !(byte
& 0x40)) ||
903 (value
== -1 && (byte
& 0x40)))
914 encode_int (gint32 val
, guint8
*buf
, guint8
**endbuf
)
917 buf
[0] = (val
>> 0) & 0xff;
918 buf
[1] = (val
>> 8) & 0xff;
919 buf
[2] = (val
>> 16) & 0xff;
920 buf
[3] = (val
>> 24) & 0xff;
926 encode_int16 (guint16 val
, guint8
*buf
, guint8
**endbuf
)
928 buf
[0] = (val
>> 0) & 0xff;
929 buf
[1] = (val
>> 8) & 0xff;
935 encode_string (const char *s
, guint8
*buf
, guint8
**endbuf
)
937 int len
= strlen (s
);
939 memcpy (buf
, s
, len
+ 1);
940 *endbuf
= buf
+ len
+ 1;
944 emit_unset_mode (MonoAotCompile
*acfg
)
946 mono_img_writer_emit_unset_mode (acfg
->w
);
949 static G_GNUC_UNUSED
void
950 emit_set_thumb_mode (MonoAotCompile
*acfg
)
952 emit_unset_mode (acfg
);
953 fprintf (acfg
->fp
, ".code 16\n");
956 static G_GNUC_UNUSED
void
957 emit_set_arm_mode (MonoAotCompile
*acfg
)
959 emit_unset_mode (acfg
);
960 fprintf (acfg
->fp
, ".code 32\n");
964 emit_code_bytes (MonoAotCompile
*acfg
, const guint8
* buf
, int size
)
969 g_assert (size
% 4 == 0);
970 emit_unset_mode (acfg
);
971 for (i
= 0; i
< size
; i
+= 4)
972 fprintf (acfg
->fp
, "%s 0x%x\n", acfg
->inst_directive
, *(guint32
*)(buf
+ i
));
974 emit_bytes (acfg
, buf
, size
);
978 /* ARCHITECTURE SPECIFIC CODE */
980 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC) || defined(TARGET_ARM64) || defined (TARGET_RISCV)
981 #define EMIT_DWARF_INFO 1
984 #ifdef TARGET_WIN32_MSVC
985 #undef EMIT_DWARF_INFO
986 #define EMIT_WIN32_CODEVIEW_INFO
989 #ifdef EMIT_WIN32_UNWIND_INFO
990 static UnwindInfoSectionCacheItem
*
991 get_cached_unwind_info_section_item_win32 (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
);
994 free_unwind_info_section_cache_win32 (MonoAotCompile
*acfg
);
997 emit_unwind_info_data_win32 (MonoAotCompile
*acfg
, PUNWIND_INFO unwind_info
);
1000 emit_unwind_info_sections_win32 (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
);
1004 arch_free_unwind_info_section_cache (MonoAotCompile
*acfg
)
1006 #ifdef EMIT_WIN32_UNWIND_INFO
1007 free_unwind_info_section_cache_win32 (acfg
);
1012 arch_emit_unwind_info_sections (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
)
1014 #ifdef EMIT_WIN32_UNWIND_INFO
1015 gboolean own_unwind_ops
= FALSE
;
1017 unwind_ops
= mono_unwind_get_cie_program ();
1018 own_unwind_ops
= TRUE
;
1021 emit_unwind_info_sections_win32 (acfg
, function_start
, function_end
, unwind_ops
);
1024 mono_free_unwind_info (unwind_ops
);
1028 #if defined(TARGET_ARM)
1029 #define AOT_FUNC_ALIGNMENT 4
1031 #define AOT_FUNC_ALIGNMENT 16
1034 #if defined(TARGET_POWERPC64) && !defined(MONO_ARCH_ILP32)
1035 #define PPC_LD_OP "ld"
1036 #define PPC_LDX_OP "ldx"
1038 #define PPC_LD_OP "lwz"
1039 #define PPC_LDX_OP "lwzx"
1042 #ifdef TARGET_X86_64_WIN32_MSVC
1043 #define AOT_TARGET_STR "AMD64 (WIN32) (MSVC codegen)"
1045 #define AOT_TARGET_STR "AMD64"
1050 #define AOT_TARGET_STR "ARM (MACH)"
1052 #define AOT_TARGET_STR "ARM (!MACH)"
1058 #define AOT_TARGET_STR "ARM64 (MACH)"
1060 #define AOT_TARGET_STR "ARM64 (!MACH)"
1064 #ifdef TARGET_POWERPC64
1065 #ifdef MONO_ARCH_ILP32
1066 #define AOT_TARGET_STR "POWERPC64 (mono ilp32)"
1068 #define AOT_TARGET_STR "POWERPC64 (!mono ilp32)"
1071 #ifdef TARGET_POWERPC
1072 #ifdef MONO_ARCH_ILP32
1073 #define AOT_TARGET_STR "POWERPC (mono ilp32)"
1075 #define AOT_TARGET_STR "POWERPC (!mono ilp32)"
1080 #ifdef TARGET_RISCV32
1081 #define AOT_TARGET_STR "RISCV32"
1084 #ifdef TARGET_RISCV64
1085 #define AOT_TARGET_STR "RISCV64"
1090 #define AOT_TARGET_STR "X86 (WIN32)"
1092 #define AOT_TARGET_STR "X86"
1096 #ifndef AOT_TARGET_STR
1097 #define AOT_TARGET_STR ""
1101 arch_init (MonoAotCompile
*acfg
)
1103 acfg
->llc_args
= g_string_new ("");
1104 acfg
->as_args
= g_string_new ("");
1105 acfg
->llvm_owriter_supported
= TRUE
;
1108 * The prefix LLVM likes to put in front of symbol names on darwin.
1109 * The mach-os specs require this for globals, but LLVM puts them in front of all
1110 * symbols. We need to handle this, since we need to refer to LLVM generated
1113 acfg
->llvm_label_prefix
= "";
1114 acfg
->user_symbol_prefix
= "";
1116 #if TARGET_X86 || TARGET_AMD64
1117 const gboolean has_custom_args
= !!acfg
->aot_opts
.llvm_llc
|| acfg
->aot_opts
.use_current_cpu
;
1120 #if defined(TARGET_X86)
1121 g_string_append_printf (acfg
->llc_args
, " -march=x86 %s", has_custom_args
? "" : "-mcpu=generic");
1124 #if defined(TARGET_AMD64)
1125 g_string_append_printf (acfg
->llc_args
, " -march=x86-64 %s", has_custom_args
? "" : "-mcpu=generic");
1127 acfg
->align_pad_value
= 0x90;
1129 g_string_append (acfg
->llc_args
, " -enable-implicit-null-checks -disable-fault-maps");
1131 if (mono_use_fast_math
) {
1132 // same parameters are passed to opt and LLVM JIT
1133 g_string_append (acfg
->llc_args
, " -fp-contract=fast -enable-no-infs-fp-math -enable-no-nans-fp-math -enable-no-signed-zeros-fp-math -enable-no-trapping-fp-math -enable-unsafe-fp-math");
1137 if (acfg
->aot_opts
.mtriple
&& strstr (acfg
->aot_opts
.mtriple
, "darwin")) {
1138 g_string_append (acfg
->llc_args
, "-mattr=+v6");
1140 if (!acfg
->aot_opts
.mtriple
) {
1141 g_string_append (acfg
->llc_args
, " -march=arm");
1143 /* arch will be defined via mtriple, e.g. armv7s-ios or thumb. */
1145 if (strstr (acfg
->aot_opts
.mtriple
, "ios")) {
1146 g_string_append (acfg
->llc_args
, " -mattr=+v7");
1147 g_string_append (acfg
->llc_args
, " -exception-model=dwarf -disable-fp-elim");
1151 #if defined(ARM_FPU_VFP_HARD)
1152 g_string_append (acfg
->llc_args
, " -mattr=+vfp2,-neon,+d16 -float-abi=hard");
1153 g_string_append (acfg
->as_args
, " -mfpu=vfp3");
1154 #elif defined(ARM_FPU_VFP)
1155 g_string_append (acfg
->llc_args
, " -mattr=+vfp2,-neon,+d16");
1156 g_string_append (acfg
->as_args
, " -mfpu=vfp3");
1158 g_string_append (acfg
->llc_args
, " -mattr=+soft-float");
1161 if (acfg
->aot_opts
.mtriple
&& strstr (acfg
->aot_opts
.mtriple
, "thumb"))
1162 acfg
->thumb_mixed
= TRUE
;
1164 if (acfg
->aot_opts
.mtriple
)
1165 mono_arch_set_target (acfg
->aot_opts
.mtriple
);
1169 g_string_append (acfg
->llc_args
, " -march=aarch64");
1170 acfg
->inst_directive
= ".inst";
1171 if (acfg
->aot_opts
.mtriple
)
1172 mono_arch_set_target (acfg
->aot_opts
.mtriple
);
1176 acfg
->user_symbol_prefix
= "_";
1177 acfg
->llvm_label_prefix
= "_";
1178 acfg
->inst_directive
= ".word";
1179 acfg
->need_no_dead_strip
= TRUE
;
1180 acfg
->aot_opts
.gnu_asm
= TRUE
;
1183 #if defined(__linux__) && !defined(TARGET_ARM)
1184 acfg
->need_pt_gnu_stack
= TRUE
;
1188 if (acfg
->aot_opts
.mtriple
)
1189 mono_arch_set_target (acfg
->aot_opts
.mtriple
);
1191 #ifdef TARGET_RISCV64
1193 g_string_append (acfg
->as_args
, " -march=rv64i ");
1194 #ifdef RISCV_FPABI_DOUBLE
1195 g_string_append (acfg
->as_args
, " -mabi=lp64d");
1197 g_string_append (acfg
->as_args
, " -mabi=lp64");
1202 g_string_append (acfg
->as_args
, " -march=rv32i ");
1203 #ifdef RISCV_FPABI_DOUBLE
1204 g_string_append (acfg
->as_args
, " -mabi=ilp32d ");
1206 g_string_append (acfg
->as_args
, " -mabi=ilp32 ");
1214 acfg
->global_symbols
= TRUE
;
1217 #ifdef TARGET_ANDROID
1218 acfg
->llvm_owriter_supported
= FALSE
;
1225 /* Load the contents of GOT_SLOT into dreg, clobbering ip0 */
1226 /* Must emit 12 bytes of instructions */
1228 arm64_emit_load_got_slot (MonoAotCompile
*acfg
, int dreg
, int got_slot
)
1232 g_assert (acfg
->fp
);
1233 emit_unset_mode (acfg
);
1235 offset
= (int)(got_slot
* TARGET_SIZEOF_VOID_P
);
1237 /* clang's integrated assembler */
1238 fprintf (acfg
->fp
, "adrp x16, %s@PAGE+%d\n", acfg
->got_symbol
, offset
& 0xfffff000);
1239 #ifdef MONO_ARCH_ILP32
1240 fprintf (acfg
->fp
, "add x16, x16, %s@PAGEOFF+%d\n", acfg
->got_symbol
, offset
& 0xfff);
1241 fprintf (acfg
->fp
, "ldr w%d, [x16, #%d]\n", dreg
, 0);
1243 fprintf (acfg
->fp
, "add x16, x16, %s@PAGEOFF\n", acfg
->got_symbol
);
1244 fprintf (acfg
->fp
, "ldr x%d, [x16, #%d]\n", dreg
, offset
& 0xfff);
1248 fprintf (acfg
->fp
, "adrp x16, %s+%d\n", acfg
->got_symbol
, offset
& 0xfffff000);
1249 fprintf (acfg
->fp
, "add x16, x16, :lo12:%s\n", acfg
->got_symbol
);
1250 fprintf (acfg
->fp
, "ldr x%d, [x16, %d]\n", dreg
, offset
& 0xfff);
1255 arm64_emit_objc_selector_ref (MonoAotCompile
*acfg
, guint8
*code
, int index
, int *code_size
)
1259 g_assert (acfg
->fp
);
1260 emit_unset_mode (acfg
);
1262 /* ldr rt, target */
1263 reg
= arm_get_ldr_lit_reg (code
);
1265 fprintf (acfg
->fp
, "adrp x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGE\n", reg
, index
);
1266 fprintf (acfg
->fp
, "add x%d, x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGEOFF\n", reg
, reg
, index
);
1267 fprintf (acfg
->fp
, "ldr x%d, [x%d]\n", reg
, reg
);
1273 arm64_emit_direct_call (MonoAotCompile
*acfg
, const char *target
, gboolean external
, gboolean thumb
, MonoJumpInfo
*ji
, int *call_size
)
1275 g_assert (acfg
->fp
);
1276 emit_unset_mode (acfg
);
1277 if (ji
&& ji
->relocation
== MONO_R_ARM64_B
) {
1278 fprintf (acfg
->fp
, "b %s\n", target
);
1281 g_assert (ji
->relocation
== MONO_R_ARM64_BL
);
1282 fprintf (acfg
->fp
, "bl %s\n", target
);
1288 arm64_emit_got_access (MonoAotCompile
*acfg
, guint8
*code
, int got_slot
, int *code_size
)
1292 /* ldr rt, target */
1293 reg
= arm_get_ldr_lit_reg (code
);
1294 arm64_emit_load_got_slot (acfg
, reg
, got_slot
);
1299 arm64_emit_plt_entry (MonoAotCompile
*acfg
, const char *got_symbol
, int offset
, int info_offset
)
1301 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
/ sizeof (target_mgreg_t
));
1302 fprintf (acfg
->fp
, "br x16\n");
1303 /* Used by mono_aot_get_plt_info_offset () */
1304 fprintf (acfg
->fp
, "%s %d\n", acfg
->inst_directive
, info_offset
);
1308 arm64_emit_tramp_page_common_code (MonoAotCompile
*acfg
, int pagesize
, int arg_reg
, int *size
)
1314 /* The common code */
1317 /* The trampoline address is in IP0 */
1318 arm_movzx (code
, ARMREG_IP1
, imm
& 0xffff, 0);
1319 arm_movkx (code
, ARMREG_IP1
, (imm
>> 16) & 0xffff, 16);
1320 /* Compute the data slot address */
1321 arm_subx (code
, ARMREG_IP0
, ARMREG_IP0
, ARMREG_IP1
);
1322 /* Trampoline argument */
1323 arm_ldrp (code
, arg_reg
, ARMREG_IP0
, 0);
1325 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP0
, TARGET_SIZEOF_VOID_P
);
1326 arm_brx (code
, ARMREG_IP0
);
1329 emit_code_bytes (acfg
, buf
, code
- buf
);
1335 arm64_emit_tramp_page_specific_code (MonoAotCompile
*acfg
, int pagesize
, int common_tramp_size
, int specific_tramp_size
)
1341 count
= (pagesize
- common_tramp_size
) / specific_tramp_size
;
1342 for (i
= 0; i
< count
; ++i
) {
1344 arm_adrx (code
, ARMREG_IP0
, code
);
1345 /* Branch to the generic code */
1346 arm_b (code
, code
- 4 - (i
* specific_tramp_size
) - common_tramp_size
);
1347 #ifndef MONO_ARCH_ILP32
1348 /* This has to be 2 pointers long */
1352 g_assert (code
- buf
== specific_tramp_size
);
1353 emit_code_bytes (acfg
, buf
, code
- buf
);
1358 arm64_emit_specific_trampoline_pages (MonoAotCompile
*acfg
)
1362 guint8
*labels
[16];
1363 int common_tramp_size
;
1364 int specific_tramp_size
= 2 * TARGET_SIZEOF_VOID_P
;
1368 if (!acfg
->aot_opts
.use_trampolines_page
)
1372 /* Have to match the target pagesize */
1375 pagesize
= mono_pagesize ();
1377 acfg
->tramp_page_size
= pagesize
;
1379 /* The specific trampolines */
1380 sprintf (symbol
, "%sspecific_trampolines_page", acfg
->user_symbol_prefix
);
1381 emit_alignment (acfg
, pagesize
);
1382 emit_global (acfg
, symbol
, TRUE
);
1383 emit_label (acfg
, symbol
);
1385 /* The common code */
1386 arm64_emit_tramp_page_common_code (acfg
, pagesize
, ARMREG_IP1
, &common_tramp_size
);
1387 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_SPECIFIC
] = common_tramp_size
;
1389 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1391 /* The rgctx trampolines */
1392 /* These are the same as the specific trampolines, but they load the argument into MONO_ARCH_RGCTX_REG */
1393 sprintf (symbol
, "%srgctx_trampolines_page", acfg
->user_symbol_prefix
);
1394 emit_alignment (acfg
, pagesize
);
1395 emit_global (acfg
, symbol
, TRUE
);
1396 emit_label (acfg
, symbol
);
1398 /* The common code */
1399 arm64_emit_tramp_page_common_code (acfg
, pagesize
, MONO_ARCH_RGCTX_REG
, &common_tramp_size
);
1400 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_STATIC_RGCTX
] = common_tramp_size
;
1402 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1404 /* The gsharedvt arg trampolines */
1405 /* These are the same as the specific trampolines */
1406 sprintf (symbol
, "%sgsharedvt_arg_trampolines_page", acfg
->user_symbol_prefix
);
1407 emit_alignment (acfg
, pagesize
);
1408 emit_global (acfg
, symbol
, TRUE
);
1409 emit_label (acfg
, symbol
);
1411 arm64_emit_tramp_page_common_code (acfg
, pagesize
, ARMREG_IP1
, &common_tramp_size
);
1412 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = common_tramp_size
;
1414 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1416 /* Unbox arbitrary trampolines */
1417 sprintf (symbol
, "%sunbox_arbitrary_trampolines_page", acfg
->user_symbol_prefix
);
1418 emit_alignment (acfg
, pagesize
);
1419 emit_global (acfg
, symbol
, TRUE
);
1420 emit_label (acfg
, symbol
);
1425 /* Unbox this arg */
1426 arm_addx_imm (code
, ARMREG_R0
, ARMREG_R0
, MONO_ABI_SIZEOF (MonoObject
));
1428 /* The trampoline address is in IP0 */
1429 arm_movzx (code
, ARMREG_IP1
, imm
& 0xffff, 0);
1430 arm_movkx (code
, ARMREG_IP1
, (imm
>> 16) & 0xffff, 16);
1431 /* Compute the data slot address */
1432 arm_subx (code
, ARMREG_IP0
, ARMREG_IP0
, ARMREG_IP1
);
1434 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP0
, 0);
1435 arm_brx (code
, ARMREG_IP0
);
1438 emit_code_bytes (acfg
, buf
, code
- buf
);
1440 common_tramp_size
= code
- buf
;
1441 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_UNBOX_ARBITRARY
] = common_tramp_size
;
1443 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1445 /* The IMT trampolines */
1446 sprintf (symbol
, "%simt_trampolines_page", acfg
->user_symbol_prefix
);
1447 emit_alignment (acfg
, pagesize
);
1448 emit_global (acfg
, symbol
, TRUE
);
1449 emit_label (acfg
, symbol
);
1453 /* The trampoline address is in IP0 */
1454 arm_movzx (code
, ARMREG_IP1
, imm
& 0xffff, 0);
1455 arm_movkx (code
, ARMREG_IP1
, (imm
>> 16) & 0xffff, 16);
1456 /* Compute the data slot address */
1457 arm_subx (code
, ARMREG_IP0
, ARMREG_IP0
, ARMREG_IP1
);
1458 /* Trampoline argument */
1459 arm_ldrp (code
, ARMREG_IP1
, ARMREG_IP0
, 0);
1461 /* Same as arch_emit_imt_trampoline () */
1463 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP1
, 0);
1464 arm_cmpp (code
, ARMREG_IP0
, MONO_ARCH_RGCTX_REG
);
1466 arm_bcc (code
, ARMCOND_EQ
, 0);
1468 /* End-of-loop check */
1470 arm_cbzx (code
, ARMREG_IP0
, 0);
1473 arm_addx_imm (code
, ARMREG_IP1
, ARMREG_IP1
, 2 * TARGET_SIZEOF_VOID_P
);
1474 arm_b (code
, labels
[0]);
1477 mono_arm_patch (labels
[1], code
, MONO_R_ARM64_BCC
);
1478 /* Load vtable slot addr */
1479 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP1
, TARGET_SIZEOF_VOID_P
);
1480 /* Load vtable slot */
1481 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP0
, 0);
1482 arm_brx (code
, ARMREG_IP0
);
1485 mono_arm_patch (labels
[2], code
, MONO_R_ARM64_CBZ
);
1486 /* Load fail addr */
1487 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP1
, TARGET_SIZEOF_VOID_P
);
1488 arm_brx (code
, ARMREG_IP0
);
1490 emit_code_bytes (acfg
, buf
, code
- buf
);
1492 common_tramp_size
= code
- buf
;
1493 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_IMT
] = common_tramp_size
;
1495 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1499 arm64_emit_specific_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1501 /* Load argument from second GOT slot */
1502 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
+ 1);
1503 /* Load generic trampoline address from first GOT slot */
1504 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
);
1505 fprintf (acfg
->fp
, "br x16\n");
1506 *tramp_size
= 7 * 4;
1510 arm64_emit_unbox_trampoline (MonoAotCompile
*acfg
, MonoCompile
*cfg
, MonoMethod
*method
, const char *call_target
)
1512 emit_unset_mode (acfg
);
1513 fprintf (acfg
->fp
, "add x0, x0, %d\n", (int)(MONO_ABI_SIZEOF (MonoObject
)));
1514 fprintf (acfg
->fp
, "b %s\n", call_target
);
1518 arm64_emit_static_rgctx_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1520 /* Similar to the specific trampolines, but use the rgctx reg instead of ip1 */
1522 /* Load argument from first GOT slot */
1523 arm64_emit_load_got_slot (acfg
, MONO_ARCH_RGCTX_REG
, offset
);
1524 /* Load generic trampoline address from second GOT slot */
1525 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
+ 1);
1526 fprintf (acfg
->fp
, "br x16\n");
1527 *tramp_size
= 7 * 4;
1531 arm64_emit_imt_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1534 guint8
*code
, *labels
[16];
1536 /* Load parameter from GOT slot into ip1 */
1537 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
);
1541 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP1
, 0);
1542 arm_cmpp (code
, ARMREG_IP0
, MONO_ARCH_RGCTX_REG
);
1544 arm_bcc (code
, ARMCOND_EQ
, 0);
1546 /* End-of-loop check */
1548 arm_cbzx (code
, ARMREG_IP0
, 0);
1551 arm_addx_imm (code
, ARMREG_IP1
, ARMREG_IP1
, 2 * 8);
1552 arm_b (code
, labels
[0]);
1555 mono_arm_patch (labels
[1], code
, MONO_R_ARM64_BCC
);
1556 /* Load vtable slot addr */
1557 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP1
, TARGET_SIZEOF_VOID_P
);
1558 /* Load vtable slot */
1559 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP0
, 0);
1560 arm_brx (code
, ARMREG_IP0
);
1563 mono_arm_patch (labels
[2], code
, MONO_R_ARM64_CBZ
);
1564 /* Load fail addr */
1565 arm_ldrp (code
, ARMREG_IP0
, ARMREG_IP1
, TARGET_SIZEOF_VOID_P
);
1566 arm_brx (code
, ARMREG_IP0
);
1568 emit_code_bytes (acfg
, buf
, code
- buf
);
1570 *tramp_size
= code
- buf
+ (3 * 4);
1574 arm64_emit_gsharedvt_arg_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1576 /* Similar to the specific trampolines, but the address is in the second slot */
1577 /* Load argument from first GOT slot */
1578 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
);
1579 /* Load generic trampoline address from second GOT slot */
1580 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
+ 1);
1581 fprintf (acfg
->fp
, "br x16\n");
1582 *tramp_size
= 7 * 4;
1588 #ifdef MONO_ARCH_AOT_SUPPORTED
1590 * arch_emit_direct_call:
1592 * Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
1596 arch_emit_direct_call (MonoAotCompile
*acfg
, const char *target
, gboolean external
, gboolean thumb
, MonoJumpInfo
*ji
, int *call_size
)
1598 #if defined(TARGET_X86) || defined(TARGET_AMD64)
1599 /* Need to make sure this is exactly 5 bytes long */
1600 emit_unset_mode (acfg
);
1601 fprintf (acfg
->fp
, "call %s\n", target
);
1603 #elif defined(TARGET_ARM)
1604 emit_unset_mode (acfg
);
1606 fprintf (acfg
->fp
, "blx %s\n", target
);
1608 fprintf (acfg
->fp
, "bl %s\n", target
);
1610 #elif defined(TARGET_ARM64)
1611 arm64_emit_direct_call (acfg
, target
, external
, thumb
, ji
, call_size
);
1612 #elif defined(TARGET_POWERPC)
1613 emit_unset_mode (acfg
);
1614 fprintf (acfg
->fp
, "bl %s\n", target
);
1617 g_assert_not_reached ();
1622 arch_emit_label_address (MonoAotCompile
*acfg
, const char *target
, gboolean external_call
, gboolean thumb
, MonoJumpInfo
*ji
, int *call_size
)
1624 #if defined(TARGET_ARM) && defined(TARGET_ANDROID)
1625 /* binutils ld does not support branch islands on arm32 */
1627 emit_unset_mode (acfg
);
1628 fprintf (acfg
->fp
, "ldr pc,=%s\n", target
);
1629 fprintf (acfg
->fp
, ".ltorg\n");
1633 arch_emit_direct_call (acfg
, target
, external_call
, thumb
, ji
, call_size
);
1639 * - we use an approach similar to the x86 abi: reserve a register (r30) to hold
1641 * - The full-aot trampolines need access to the GOT of mscorlib, so we store
1642 * in in the 2. slot of every GOT, and require every method to place the GOT
1643 * address in r30, even when it doesn't access the GOT otherwise. This way,
1644 * the trampolines can compute the mscorlib GOT address by loading 4(r30).
1649 * PPC64 uses function descriptors which greatly complicate all code, since
1650 * these are used very inconsistently in the runtime. Some functions like
1651 * mono_compile_method () return ftn descriptors, while others like the
1652 * trampoline creation functions do not.
1653 * We assume that all GOT slots contain function descriptors, and create
1654 * descriptors in aot-runtime.c when needed.
1655 * The ppc64 abi uses r2 to hold the address of the TOC/GOT, which is loaded
1656 * from function descriptors, we could do the same, but it would require
1657 * rewriting all the ppc/aot code to handle function descriptors properly.
1658 * So instead, we use the same approach as on PPC32.
1659 * This is a horrible mess, but fixing it would probably lead to an even bigger
1665 * - similar to the PPC32 design, we reserve EBX to hold the GOT pointer.
1668 #ifdef MONO_ARCH_AOT_SUPPORTED
1670 * arch_emit_got_offset:
1672 * The memory pointed to by CODE should hold native code for computing the GOT
1673 * address (OP_LOAD_GOTADDR). Emit this code while patching it with the offset
1674 * between code and the GOT. CODE_SIZE is set to the number of bytes emitted.
1677 arch_emit_got_offset (MonoAotCompile
*acfg
, guint8
*code
, int *code_size
)
1679 #if defined(TARGET_POWERPC64)
1680 emit_unset_mode (acfg
);
1682 * The ppc32 code doesn't seem to work on ppc64, the assembler complains about
1683 * unsupported relocations. So we store the got address into the .Lgot_addr
1684 * symbol which is in the text segment, compute its address, and load it.
1686 fprintf (acfg
->fp
, ".L%d:\n", acfg
->label_generator
);
1687 fprintf (acfg
->fp
, "lis 0, (.Lgot_addr + 4 - .L%d)@h\n", acfg
->label_generator
);
1688 fprintf (acfg
->fp
, "ori 0, 0, (.Lgot_addr + 4 - .L%d)@l\n", acfg
->label_generator
);
1689 fprintf (acfg
->fp
, "add 30, 30, 0\n");
1690 fprintf (acfg
->fp
, "%s 30, 0(30)\n", PPC_LD_OP
);
1691 acfg
->label_generator
++;
1693 #elif defined(TARGET_POWERPC)
1694 emit_unset_mode (acfg
);
1695 fprintf (acfg
->fp
, ".L%d:\n", acfg
->label_generator
);
1696 fprintf (acfg
->fp
, "lis 0, (%s + 4 - .L%d)@h\n", acfg
->got_symbol
, acfg
->label_generator
);
1697 fprintf (acfg
->fp
, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg
->got_symbol
, acfg
->label_generator
);
1698 acfg
->label_generator
++;
1701 guint32 offset
= mono_arch_get_patch_offset (code
);
1702 emit_bytes (acfg
, code
, offset
);
1703 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", offset
);
1705 *code_size
= offset
+ 4;
1710 * arch_emit_got_access:
1712 * The memory pointed to by CODE should hold native code for loading a GOT
1713 * slot (OP_AOTCONST/OP_GOT_ENTRY). Emit this code while patching it so it accesses the
1714 * GOT slot GOT_SLOT. CODE_SIZE is set to the number of bytes emitted.
1717 arch_emit_got_access (MonoAotCompile
*acfg
, const char *got_symbol
, guint8
*code
, int got_slot
, int *code_size
)
1720 /* mov reg, got+offset(%rip) */
1722 /* The GOT symbol is in the LLVM module, the clang assembler has problems emitting symbol diffs for it */
1726 /* Decode reg, see amd64_mov_reg_membase () */
1727 rex_r
= code
[0] & AMD64_REX_R
;
1728 g_assert (code
[0] == 0x49 + rex_r
);
1729 g_assert (code
[1] == 0x8b);
1730 dreg
= ((code
[2] >> 3) & 0x7) + (rex_r
? 8 : 0);
1732 emit_unset_mode (acfg
);
1733 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %s\n", got_symbol
, (unsigned int) ((got_slot
* sizeof (target_mgreg_t
))), mono_arch_regname (dreg
));
1736 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1737 emit_symbol_diff (acfg
, got_symbol
, ".", (unsigned int) ((got_slot
* sizeof (target_mgreg_t
)) - 4));
1738 *code_size
= mono_arch_get_patch_offset (code
) + 4;
1740 #elif defined(TARGET_X86)
1741 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1742 emit_int32 (acfg
, (unsigned int) ((got_slot
* sizeof (target_mgreg_t
))));
1743 *code_size
= mono_arch_get_patch_offset (code
) + 4;
1744 #elif defined(TARGET_ARM)
1745 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1746 emit_symbol_diff (acfg
, got_symbol
, ".", (unsigned int) ((got_slot
* sizeof (target_mgreg_t
))) - 12);
1747 *code_size
= mono_arch_get_patch_offset (code
) + 4;
1748 #elif defined(TARGET_ARM64)
1749 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1750 arm64_emit_got_access (acfg
, code
, got_slot
, code_size
);
1751 #elif defined(TARGET_POWERPC)
1755 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1757 ppc_load32 (code
, ppc_r0
, got_slot
* sizeof (target_mgreg_t
));
1758 g_assert (code
- buf
== 8);
1759 emit_bytes (acfg
, buf
, code
- buf
);
1760 *code_size
= code
- buf
;
1763 g_assert_not_reached ();
1769 #ifdef MONO_ARCH_AOT_SUPPORTED
1771 * arch_emit_objc_selector_ref:
1773 * Emit the implementation of OP_OBJC_GET_SELECTOR, which itself implements @selector(foo:) in objective-c.
1776 arch_emit_objc_selector_ref (MonoAotCompile
*acfg
, guint8
*code
, int index
, int *code_size
)
1778 #if defined(TARGET_ARM)
1779 char symbol1
[MAX_SYMBOL_SIZE
];
1780 char symbol2
[MAX_SYMBOL_SIZE
];
1781 int lindex
= acfg
->objc_selector_index_2
++;
1783 /* Emit ldr.imm/b */
1784 emit_bytes (acfg
, code
, 8);
1786 sprintf (symbol1
, "L_OBJC_SELECTOR_%d", lindex
);
1787 sprintf (symbol2
, "L_OBJC_SELECTOR_REFERENCES_%d", index
);
1789 emit_label (acfg
, symbol1
);
1790 mono_img_writer_emit_unset_mode (acfg
->w
);
1791 fprintf (acfg
->fp
, ".long %s-(%s+12)", symbol2
, symbol1
);
1794 #elif defined(TARGET_ARM64)
1795 arm64_emit_objc_selector_ref (acfg
, code
, index
, code_size
);
1797 g_assert_not_reached ();
1803 * arch_emit_plt_entry:
1805 * Emit code for the PLT entry.
1806 * The plt entry should look like this:
1807 * <indirect jump to GOT_SYMBOL + OFFSET>
1808 * <INFO_OFFSET embedded into the instruction stream>
1811 arch_emit_plt_entry (MonoAotCompile
*acfg
, const char *got_symbol
, int offset
, int info_offset
)
1813 #if defined(TARGET_X86)
1814 /* jmp *<offset>(%ebx) */
1815 emit_byte (acfg
, 0xff);
1816 emit_byte (acfg
, 0xa3);
1817 emit_int32 (acfg
, offset
);
1818 /* Used by mono_aot_get_plt_info_offset */
1819 emit_int32 (acfg
, info_offset
);
1820 #elif defined(TARGET_AMD64)
1821 emit_unset_mode (acfg
);
1822 fprintf (acfg
->fp
, "jmp *%s+%d(%%rip)\n", got_symbol
, offset
);
1823 /* Used by mono_aot_get_plt_info_offset */
1824 emit_int32 (acfg
, info_offset
);
1825 acfg
->stats
.plt_size
+= 10;
1826 #elif defined(TARGET_ARM)
1831 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 0);
1832 ARM_LDR_REG_REG (code
, ARMREG_PC
, ARMREG_PC
, ARMREG_IP
);
1833 emit_bytes (acfg
, buf
, code
- buf
);
1834 emit_symbol_diff (acfg
, got_symbol
, ".", offset
- 4);
1835 /* Used by mono_aot_get_plt_info_offset */
1836 emit_int32 (acfg
, info_offset
);
1837 #elif defined(TARGET_ARM64)
1838 arm64_emit_plt_entry (acfg
, got_symbol
, offset
, info_offset
);
1839 #elif defined(TARGET_POWERPC)
1840 /* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
1841 emit_unset_mode (acfg
);
1842 fprintf (acfg
->fp
, "lis 11, %d@h\n", offset
);
1843 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", offset
);
1844 fprintf (acfg
->fp
, "add 11, 11, 30\n");
1845 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
1846 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1847 fprintf (acfg
->fp
, "%s 2, %d(11)\n", PPC_LD_OP
, (int)sizeof (target_mgreg_t
));
1848 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
1850 fprintf (acfg
->fp
, "mtctr 11\n");
1851 fprintf (acfg
->fp
, "bctr\n");
1852 emit_int32 (acfg
, info_offset
);
1854 g_assert_not_reached ();
1859 * arch_emit_llvm_plt_entry:
1861 * Same as arch_emit_plt_entry, but handles calls from LLVM generated code.
1862 * This is only needed on arm to handle thumb interop.
1865 arch_emit_llvm_plt_entry (MonoAotCompile
*acfg
, const char *got_symbol
, int offset
, int info_offset
)
1867 #if defined(TARGET_ARM)
1868 /* LLVM calls the PLT entries using bl, so these have to be thumb2 */
1869 /* The caller already transitioned to thumb */
1870 /* The code below should be 12 bytes long */
1871 /* clang has trouble encoding these instructions, so emit the binary */
1873 fprintf (acfg
->fp
, "ldr ip, [pc, #8]\n");
1874 /* thumb can't encode ld pc, [pc, ip] */
1875 fprintf (acfg
->fp
, "add ip, pc, ip\n");
1876 fprintf (acfg
->fp
, "ldr ip, [ip, #0]\n");
1877 fprintf (acfg
->fp
, "bx ip\n");
1879 emit_set_thumb_mode (acfg
);
1880 fprintf (acfg
->fp
, ".4byte 0xc008f8df\n");
1881 fprintf (acfg
->fp
, ".2byte 0x44fc\n");
1882 fprintf (acfg
->fp
, ".4byte 0xc000f8dc\n");
1883 fprintf (acfg
->fp
, ".2byte 0x4760\n");
1884 emit_symbol_diff (acfg
, got_symbol
, ".", offset
+ 4);
1885 emit_int32 (acfg
, info_offset
);
1886 emit_unset_mode (acfg
);
1887 emit_set_arm_mode (acfg
);
1889 g_assert_not_reached ();
1893 /* Save unwind_info in the module and emit the offset to the information at symbol */
1894 static void save_unwind_info (MonoAotCompile
*acfg
, char *symbol
, GSList
*unwind_ops
)
1896 guint32 uw_offset
, encoded_len
;
1899 emit_section_change (acfg
, RODATA_SECT
, 0);
1900 emit_global (acfg
, symbol
, FALSE
);
1901 emit_label (acfg
, symbol
);
1903 encoded
= mono_unwind_ops_encode (unwind_ops
, &encoded_len
);
1904 uw_offset
= get_unwind_info_offset (acfg
, encoded
, encoded_len
);
1906 emit_int32 (acfg
, uw_offset
);
1910 * arch_emit_specific_trampoline_pages:
1912 * Emits a page full of trampolines: each trampoline uses its own address to
1913 * lookup both the generic trampoline code and the data argument.
1914 * This page can be remapped in process multiple times so we can get an
1915 * unlimited number of trampolines.
1916 * Specifically this implementation uses the following trick: two memory pages
1917 * are allocated, with the first containing the data and the second containing the trampolines.
1918 * To reduce trampoline size, each trampoline jumps at the start of the page where a common
1919 * implementation does all the lifting.
1920 * Note that the ARM single trampoline size is 8 bytes, exactly like the data that needs to be stored
1921 * on the arm 32 bit system.
1924 arch_emit_specific_trampoline_pages (MonoAotCompile
*acfg
)
1926 #if defined(TARGET_ARM)
1929 guint8
*loop_start
, *loop_branch_back
, *loop_end_check
, *imt_found_check
;
1931 int pagesize
= MONO_AOT_TRAMP_PAGE_SIZE
;
1932 GSList
*unwind_ops
= NULL
;
1933 #define COMMON_TRAMP_SIZE 16
1934 int count
= (pagesize
- COMMON_TRAMP_SIZE
) / 8;
1935 int imm8
, rot_amount
;
1938 if (!acfg
->aot_opts
.use_trampolines_page
)
1941 acfg
->tramp_page_size
= pagesize
;
1943 sprintf (symbol
, "%sspecific_trampolines_page", acfg
->user_symbol_prefix
);
1944 emit_alignment (acfg
, pagesize
);
1945 emit_global (acfg
, symbol
, TRUE
);
1946 emit_label (acfg
, symbol
);
1948 /* emit the generic code first, the trampoline address + 8 is in the lr register */
1950 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1951 ARM_SUB_REG_IMM (code
, ARMREG_LR
, ARMREG_LR
, imm8
, rot_amount
);
1952 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_LR
, -8);
1953 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_LR
, -4);
1955 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
1958 emit_bytes (acfg
, buf
, code
- buf
);
1960 for (i
= 0; i
< count
; ++i
) {
1962 ARM_PUSH (code
, 0x5fff);
1964 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
1965 g_assert (code
- buf
== 8);
1966 emit_bytes (acfg
, buf
, code
- buf
);
1969 /* now the rgctx trampolines: each specific trampolines puts in the ip register
1970 * the instruction pointer address, so the generic trampoline at the start of the page
1971 * subtracts 4096 to get to the data page and loads the values
1972 * We again fit the generic trampiline in 16 bytes.
1974 sprintf (symbol
, "%srgctx_trampolines_page", acfg
->user_symbol_prefix
);
1975 emit_global (acfg
, symbol
, TRUE
);
1976 emit_label (acfg
, symbol
);
1978 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1979 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
1980 ARM_LDR_IMM (code
, MONO_ARCH_RGCTX_REG
, ARMREG_IP
, -8);
1981 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_IP
, -4);
1983 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
1986 emit_bytes (acfg
, buf
, code
- buf
);
1988 for (i
= 0; i
< count
; ++i
) {
1990 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
1992 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
1993 g_assert (code
- buf
== 8);
1994 emit_bytes (acfg
, buf
, code
- buf
);
1998 * gsharedvt arg trampolines: see arch_emit_gsharedvt_arg_trampoline ()
2000 sprintf (symbol
, "%sgsharedvt_arg_trampolines_page", acfg
->user_symbol_prefix
);
2001 emit_global (acfg
, symbol
, TRUE
);
2002 emit_label (acfg
, symbol
);
2004 ARM_PUSH (code
, (1 << ARMREG_R0
) | (1 << ARMREG_R1
) | (1 << ARMREG_R2
) | (1 << ARMREG_R3
));
2005 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
2006 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
2007 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_IP
, -8);
2008 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_IP
, -4);
2009 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
2011 emit_bytes (acfg
, buf
, code
- buf
);
2013 for (i
= 0; i
< count
; ++i
) {
2015 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
2017 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
2018 g_assert (code
- buf
== 8);
2019 emit_bytes (acfg
, buf
, code
- buf
);
2022 /* now the unbox arbitrary trampolines: each specific trampolines puts in the ip register
2023 * the instruction pointer address, so the generic trampoline at the start of the page
2024 * subtracts 4096 to get to the data page and loads the target addr.
2025 * We again fit the generic trampoline in 16 bytes.
2027 sprintf (symbol
, "%sunbox_arbitrary_trampolines_page", acfg
->user_symbol_prefix
);
2028 emit_global (acfg
, symbol
, TRUE
);
2029 emit_label (acfg
, symbol
);
2032 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, MONO_ABI_SIZEOF (MonoObject
));
2033 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
2034 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
2035 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_IP
, -4);
2037 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
2040 emit_bytes (acfg
, buf
, code
- buf
);
2042 for (i
= 0; i
< count
; ++i
) {
2044 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
2046 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
2047 g_assert (code
- buf
== 8);
2048 emit_bytes (acfg
, buf
, code
- buf
);
2051 /* now the imt trampolines: each specific trampolines puts in the ip register
2052 * the instruction pointer address, so the generic trampoline at the start of the page
2053 * subtracts 4096 to get to the data page and loads the values
2055 #define IMT_TRAMP_SIZE 72
2056 sprintf (symbol
, "%simt_trampolines_page", acfg
->user_symbol_prefix
);
2057 emit_global (acfg
, symbol
, TRUE
);
2058 emit_label (acfg
, symbol
);
2060 /* Need at least two free registers, plus a slot for storing the pc */
2061 ARM_PUSH (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_R2
));
2063 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
2064 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
2065 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_IP
, -8);
2067 /* The IMT method is in v5, r0 has the imt array address */
2070 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_R0
, 0);
2071 ARM_CMP_REG_REG (code
, ARMREG_R1
, ARMREG_V5
);
2072 imt_found_check
= code
;
2073 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2075 /* End-of-loop check */
2076 ARM_CMP_REG_IMM (code
, ARMREG_R1
, 0, 0);
2077 loop_end_check
= code
;
2078 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2081 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, sizeof (target_mgreg_t
) * 2);
2082 loop_branch_back
= code
;
2084 arm_patch (loop_branch_back
, loop_start
);
2087 arm_patch (imt_found_check
, code
);
2088 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2089 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 0);
2090 /* Save it to the third stack slot */
2091 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2092 /* Restore the registers and branch */
2093 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2096 arm_patch (loop_end_check
, code
);
2097 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2098 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2099 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2103 g_assert (code
- buf
== IMT_TRAMP_SIZE
);
2104 emit_bytes (acfg
, buf
, code
- buf
);
2106 for (i
= 0; i
< count
; ++i
) {
2108 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
2110 arm_patch (code
- 4, code
- IMT_TRAMP_SIZE
- 8 * (i
+ 1));
2111 g_assert (code
- buf
== 8);
2112 emit_bytes (acfg
, buf
, code
- buf
);
2115 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_SPECIFIC
] = 16;
2116 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_STATIC_RGCTX
] = 16;
2117 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_IMT
] = 72;
2118 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = 16;
2119 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_UNBOX_ARBITRARY
] = 16;
2121 /* Unwind info for specifc trampolines */
2122 sprintf (symbol
, "%sspecific_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2123 /* We unwind to the original caller, from the stack, since lr is clobbered */
2124 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 14 * sizeof (target_mgreg_t
));
2125 mono_add_unwind_op_offset (unwind_ops
, 0, 0, ARMREG_LR
, -4);
2126 save_unwind_info (acfg
, symbol
, unwind_ops
);
2127 mono_free_unwind_info (unwind_ops
);
2129 sprintf (symbol
, "%sspecific_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2130 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2131 mono_add_unwind_op_def_cfa_offset (unwind_ops
, 4, 0, 14 * sizeof (target_mgreg_t
));
2132 save_unwind_info (acfg
, symbol
, unwind_ops
);
2133 mono_free_unwind_info (unwind_ops
);
2135 /* Unwind info for rgctx trampolines */
2136 sprintf (symbol
, "%srgctx_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2137 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2138 save_unwind_info (acfg
, symbol
, unwind_ops
);
2140 sprintf (symbol
, "%srgctx_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2141 save_unwind_info (acfg
, symbol
, unwind_ops
);
2142 mono_free_unwind_info (unwind_ops
);
2144 /* Unwind info for gsharedvt trampolines */
2145 sprintf (symbol
, "%sgsharedvt_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2146 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2147 mono_add_unwind_op_def_cfa_offset (unwind_ops
, 4, 0, 4 * sizeof (target_mgreg_t
));
2148 save_unwind_info (acfg
, symbol
, unwind_ops
);
2149 mono_free_unwind_info (unwind_ops
);
2151 sprintf (symbol
, "%sgsharedvt_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2152 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2153 save_unwind_info (acfg
, symbol
, unwind_ops
);
2154 mono_free_unwind_info (unwind_ops
);
2156 /* Unwind info for unbox arbitrary trampolines */
2157 sprintf (symbol
, "%sunbox_arbitrary_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2158 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2159 save_unwind_info (acfg
, symbol
, unwind_ops
);
2161 sprintf (symbol
, "%sunbox_arbitrary_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2162 save_unwind_info (acfg
, symbol
, unwind_ops
);
2163 mono_free_unwind_info (unwind_ops
);
2165 /* Unwind info for imt trampolines */
2166 sprintf (symbol
, "%simt_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2167 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2168 mono_add_unwind_op_def_cfa_offset (unwind_ops
, 4, 0, 3 * sizeof (target_mgreg_t
));
2169 save_unwind_info (acfg
, symbol
, unwind_ops
);
2170 mono_free_unwind_info (unwind_ops
);
2172 sprintf (symbol
, "%simt_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2173 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2174 save_unwind_info (acfg
, symbol
, unwind_ops
);
2175 mono_free_unwind_info (unwind_ops
);
2176 #elif defined(TARGET_ARM64)
2177 arm64_emit_specific_trampoline_pages (acfg
);
2182 * arch_emit_specific_trampoline:
2184 * Emit code for a specific trampoline. OFFSET is the offset of the first of
2185 * two GOT slots which contain the generic trampoline address and the trampoline
2186 * argument. TRAMP_SIZE is set to the size of the emitted trampoline.
2189 arch_emit_specific_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2192 * The trampolines created here are variations of the specific
2193 * trampolines created in mono_arch_create_specific_trampoline (). The
2195 * - the generic trampoline address is taken from a got slot.
2196 * - the offset of the got slot where the trampoline argument is stored
2197 * is embedded in the instruction stream, and the generic trampoline
2198 * can load the argument by loading the offset, adding it to the
2199 * address of the trampoline to get the address of the got slot, and
2200 * loading the argument from there.
2201 * - all the trampolines should be of the same length.
2203 #if defined(TARGET_AMD64)
2204 /* This should be exactly 8 bytes long */
2206 /* call *<offset>(%rip) */
2208 emit_unset_mode (acfg
);
2209 fprintf (acfg
->fp
, "call *%s+%d(%%rip)\n", acfg
->got_symbol
, (int)(offset
* sizeof (target_mgreg_t
)));
2210 emit_zero_bytes (acfg
, 2);
2212 emit_byte (acfg
, '\x41');
2213 emit_byte (acfg
, '\xff');
2214 emit_byte (acfg
, '\x15');
2215 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4);
2216 emit_zero_bytes (acfg
, 1);
2218 #elif defined(TARGET_ARM)
2222 /* This should be exactly 20 bytes long */
2225 ARM_PUSH (code
, 0x5fff);
2226 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_PC
, 4);
2227 /* Load the value from the GOT */
2228 ARM_LDR_REG_REG (code
, ARMREG_R1
, ARMREG_PC
, ARMREG_R1
);
2230 ARM_BLX_REG (code
, ARMREG_R1
);
2232 g_assert (code
- buf
== 16);
2235 emit_bytes (acfg
, buf
, code
- buf
);
2237 * Only one offset is needed, since the second one would be equal to the
2240 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4 + 4);
2241 //emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (target_mgreg_t)) - 4 + 8);
2242 #elif defined(TARGET_ARM64)
2243 arm64_emit_specific_trampoline (acfg
, offset
, tramp_size
);
2244 #elif defined(TARGET_POWERPC)
2252 * PPC has no ip relative addressing, so we need to compute the address
2253 * of the mscorlib got. That is slow and complex, so instead, we store it
2254 * in the second got slot of every aot image. The caller already computed
2255 * the address of its got and placed it into r30.
2257 emit_unset_mode (acfg
);
2258 /* Load mscorlib got address */
2259 fprintf (acfg
->fp
, "%s 0, %d(30)\n", PPC_LD_OP
, (int)sizeof (target_mgreg_t
));
2260 /* Load generic trampoline address */
2261 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)(offset
* sizeof (target_mgreg_t
)));
2262 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)(offset
* sizeof (target_mgreg_t
)));
2263 fprintf (acfg
->fp
, "%s 11, 11, 0\n", PPC_LDX_OP
);
2264 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2265 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
2267 fprintf (acfg
->fp
, "mtctr 11\n");
2268 /* Load trampoline argument */
2269 /* On ppc, we pass it normally to the generic trampoline */
2270 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2271 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2272 fprintf (acfg
->fp
, "%s 0, 11, 0\n", PPC_LDX_OP
);
2273 /* Branch to generic trampoline */
2274 fprintf (acfg
->fp
, "bctr\n");
2276 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2277 *tramp_size
= 10 * 4;
2279 *tramp_size
= 9 * 4;
2281 #elif defined(TARGET_X86)
2285 /* Similar to the PPC code above */
2287 /* FIXME: Could this clobber the register needed by get_vcall_slot () ? */
2290 /* Load mscorlib got address */
2291 x86_mov_reg_membase (code
, X86_ECX
, MONO_ARCH_GOT_REG
, sizeof (target_mgreg_t
), 4);
2292 /* Push trampoline argument */
2293 x86_push_membase (code
, X86_ECX
, (offset
+ 1) * sizeof (target_mgreg_t
));
2294 /* Load generic trampoline address */
2295 x86_mov_reg_membase (code
, X86_ECX
, X86_ECX
, offset
* sizeof (target_mgreg_t
), 4);
2296 /* Branch to generic trampoline */
2297 x86_jump_reg (code
, X86_ECX
);
2299 emit_bytes (acfg
, buf
, code
- buf
);
2302 g_assert (code
- buf
== *tramp_size
);
2304 g_assert_not_reached ();
2309 * arch_emit_unbox_trampoline:
2311 * Emit code for the unbox trampoline for METHOD used in the full-aot case.
2312 * CALL_TARGET is the symbol pointing to the native code of METHOD.
2314 * See mono_aot_get_unbox_trampoline.
2317 arch_emit_unbox_trampoline (MonoAotCompile
*acfg
, MonoCompile
*cfg
, MonoMethod
*method
, const char *call_target
)
2319 #if defined(TARGET_AMD64)
2324 this_reg
= mono_arch_get_this_arg_reg (NULL
);
2326 amd64_alu_reg_imm (code
, X86_ADD
, this_reg
, MONO_ABI_SIZEOF (MonoObject
));
2328 emit_bytes (acfg
, buf
, code
- buf
);
2331 emit_unset_mode (acfg
);
2332 fprintf (acfg
->fp
, "jmp %s\n", call_target
);
2334 emit_byte (acfg
, '\xe9');
2335 emit_symbol_diff (acfg
, call_target
, ".", -4);
2337 #elif defined(TARGET_X86)
2344 x86_alu_membase_imm (code
, X86_ADD
, X86_ESP
, this_pos
, MONO_ABI_SIZEOF (MonoObject
));
2346 emit_bytes (acfg
, buf
, code
- buf
);
2349 emit_byte (acfg
, '\xe9');
2350 emit_symbol_diff (acfg
, call_target
, ".", -4);
2351 #elif defined(TARGET_ARM)
2355 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
) {
2356 fprintf (acfg
->fp
, "add r0, r0, #%d\n", (int)MONO_ABI_SIZEOF (MonoObject
));
2357 fprintf (acfg
->fp
, "b %s\n", call_target
);
2358 fprintf (acfg
->fp
, ".arm\n");
2359 fprintf (acfg
->fp
, ".align 2\n");
2365 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, MONO_ABI_SIZEOF (MonoObject
));
2367 emit_bytes (acfg
, buf
, code
- buf
);
2368 /* jump to method */
2369 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
)
2370 fprintf (acfg
->fp
, "\n\tbx %s\n", call_target
);
2372 fprintf (acfg
->fp
, "\n\tb %s\n", call_target
);
2373 #elif defined(TARGET_ARM64)
2374 arm64_emit_unbox_trampoline (acfg
, cfg
, method
, call_target
);
2375 #elif defined(TARGET_POWERPC)
2378 fprintf (acfg
->fp
, "\n\taddi %d, %d, %d\n", this_pos
, this_pos
, (int)MONO_ABI_SIZEOF (MonoObject
));
2379 fprintf (acfg
->fp
, "\n\tb %s\n", call_target
);
2381 g_assert_not_reached ();
2386 * arch_emit_static_rgctx_trampoline:
2388 * Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
2389 * two GOT slots which contain the rgctx argument, and the method to jump to.
2390 * TRAMP_SIZE is set to the size of the emitted trampoline.
2391 * These kinds of trampolines cannot be enumerated statically, since there could
2392 * be one trampoline per method instantiation, so we emit the same code for all
2393 * trampolines, and parameterize them using two GOT slots.
2396 arch_emit_static_rgctx_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2398 #if defined(TARGET_AMD64)
2399 /* This should be exactly 13 bytes long */
2403 emit_unset_mode (acfg
);
2404 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %%r10\n", acfg
->got_symbol
, (int)(offset
* sizeof (target_mgreg_t
)));
2405 fprintf (acfg
->fp
, "jmp *%s+%d(%%rip)\n", acfg
->got_symbol
, (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2407 /* mov <OFFSET>(%rip), %r10 */
2408 emit_byte (acfg
, '\x4d');
2409 emit_byte (acfg
, '\x8b');
2410 emit_byte (acfg
, '\x15');
2411 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4);
2413 /* jmp *<offset>(%rip) */
2414 emit_byte (acfg
, '\xff');
2415 emit_byte (acfg
, '\x25');
2416 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", ((offset
+ 1) * sizeof (target_mgreg_t
)) - 4);
2418 #elif defined(TARGET_ARM)
2422 /* This should be exactly 24 bytes long */
2425 /* Load rgctx value */
2426 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 8);
2427 ARM_LDR_REG_REG (code
, MONO_ARCH_RGCTX_REG
, ARMREG_PC
, ARMREG_IP
);
2428 /* Load branch addr + branch */
2429 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 4);
2430 ARM_LDR_REG_REG (code
, ARMREG_PC
, ARMREG_PC
, ARMREG_IP
);
2432 g_assert (code
- buf
== 16);
2435 emit_bytes (acfg
, buf
, code
- buf
);
2436 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4 + 8);
2437 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", ((offset
+ 1) * sizeof (target_mgreg_t
)) - 4 + 4);
2438 #elif defined(TARGET_ARM64)
2439 arm64_emit_static_rgctx_trampoline (acfg
, offset
, tramp_size
);
2440 #elif defined(TARGET_POWERPC)
2448 * PPC has no ip relative addressing, so we need to compute the address
2449 * of the mscorlib got. That is slow and complex, so instead, we store it
2450 * in the second got slot of every aot image. The caller already computed
2451 * the address of its got and placed it into r30.
2453 emit_unset_mode (acfg
);
2454 /* Load mscorlib got address */
2455 fprintf (acfg
->fp
, "%s 0, %d(30)\n", PPC_LD_OP
, (int)sizeof (target_mgreg_t
));
2457 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)(offset
* sizeof (target_mgreg_t
)));
2458 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)(offset
* sizeof (target_mgreg_t
)));
2459 fprintf (acfg
->fp
, "%s %d, 11, 0\n", PPC_LDX_OP
, MONO_ARCH_RGCTX_REG
);
2460 /* Load target address */
2461 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2462 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2463 fprintf (acfg
->fp
, "%s 11, 11, 0\n", PPC_LDX_OP
);
2464 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2465 fprintf (acfg
->fp
, "%s 2, %d(11)\n", PPC_LD_OP
, (int)sizeof (target_mgreg_t
));
2466 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
2468 fprintf (acfg
->fp
, "mtctr 11\n");
2469 /* Branch to the target address */
2470 fprintf (acfg
->fp
, "bctr\n");
2472 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2473 *tramp_size
= 11 * 4;
2475 *tramp_size
= 9 * 4;
2478 #elif defined(TARGET_X86)
2482 /* Similar to the PPC code above */
2484 g_assert (MONO_ARCH_RGCTX_REG
!= X86_ECX
);
2487 /* Load mscorlib got address */
2488 x86_mov_reg_membase (code
, X86_ECX
, MONO_ARCH_GOT_REG
, sizeof (target_mgreg_t
), 4);
2490 x86_mov_reg_membase (code
, MONO_ARCH_RGCTX_REG
, X86_ECX
, offset
* sizeof (target_mgreg_t
), 4);
2491 /* Branch to the target address */
2492 x86_jump_membase (code
, X86_ECX
, (offset
+ 1) * sizeof (target_mgreg_t
));
2494 emit_bytes (acfg
, buf
, code
- buf
);
2497 g_assert (code
- buf
== *tramp_size
);
2499 g_assert_not_reached ();
2504 * arch_emit_imt_trampoline:
2506 * Emit an IMT trampoline usable in full-aot mode. The trampoline uses 1 got slot which
2507 * points to an array of pointer pairs. The pairs of the form [key, ptr], where
2508 * key is the IMT key, and ptr holds the address of a memory location holding
2509 * the address to branch to if the IMT arg matches the key. The array is
2510 * terminated by a pair whose key is NULL, and whose ptr is the address of the
2512 * TRAMP_SIZE is set to the size of the emitted trampoline.
2515 arch_emit_imt_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2517 #if defined(TARGET_AMD64)
2519 guint8
*labels
[16];
2521 guint8
*mov_buf_ptr
= mov_buf
;
2523 const int kSizeOfMove
= 7;
2525 code
= buf
= (guint8
*)g_malloc (256);
2527 /* FIXME: Optimize this, i.e. use binary search etc. */
2528 /* Maybe move the body into a separate function (slower, but much smaller) */
2530 /* MONO_ARCH_IMT_SCRATCH_REG is a free register */
2533 emit_unset_mode (acfg
);
2534 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %s\n", acfg
->got_symbol
, (int)(offset
* sizeof (target_mgreg_t
)), mono_arch_regname (MONO_ARCH_IMT_SCRATCH_REG
));
2538 amd64_alu_membase_imm (code
, X86_CMP
, MONO_ARCH_IMT_SCRATCH_REG
, 0, 0);
2540 amd64_branch8 (code
, X86_CC_Z
, 0, FALSE
);
2543 amd64_alu_membase_reg_size (code
, X86_CMP
, MONO_ARCH_IMT_SCRATCH_REG
, 0, MONO_ARCH_IMT_REG
, sizeof (target_mgreg_t
));
2545 amd64_branch8 (code
, X86_CC_Z
, 0, FALSE
);
2548 amd64_alu_reg_imm (code
, X86_ADD
, MONO_ARCH_IMT_SCRATCH_REG
, 2 * sizeof (target_mgreg_t
));
2549 amd64_jump_code (code
, labels
[0]);
2552 mono_amd64_patch (labels
[2], code
);
2553 amd64_mov_reg_membase (code
, MONO_ARCH_IMT_SCRATCH_REG
, MONO_ARCH_IMT_SCRATCH_REG
, sizeof (target_mgreg_t
), sizeof (target_mgreg_t
));
2554 amd64_jump_membase (code
, MONO_ARCH_IMT_SCRATCH_REG
, 0);
2557 mono_amd64_patch (labels
[1], code
);
2558 /* Load fail tramp */
2559 amd64_alu_reg_imm (code
, X86_ADD
, MONO_ARCH_IMT_SCRATCH_REG
, sizeof (target_mgreg_t
));
2560 /* Check if there is a fail tramp */
2561 amd64_alu_membase_imm (code
, X86_CMP
, MONO_ARCH_IMT_SCRATCH_REG
, 0, 0);
2563 amd64_branch8 (code
, X86_CC_Z
, 0, FALSE
);
2564 /* Jump to fail tramp */
2565 amd64_jump_membase (code
, MONO_ARCH_IMT_SCRATCH_REG
, 0);
2568 mono_amd64_patch (labels
[3], code
);
2569 x86_breakpoint (code
);
2572 /* mov <OFFSET>(%rip), MONO_ARCH_IMT_SCRATCH_REG */
2573 amd64_emit_rex (mov_buf_ptr
, sizeof(gpointer
), MONO_ARCH_IMT_SCRATCH_REG
, 0, AMD64_RIP
);
2574 *(mov_buf_ptr
)++ = (unsigned char)0x8b; /* mov opcode */
2575 x86_address_byte (mov_buf_ptr
, 0, MONO_ARCH_IMT_SCRATCH_REG
& 0x7, 5);
2576 emit_bytes (acfg
, mov_buf
, mov_buf_ptr
- mov_buf
);
2577 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4);
2579 emit_bytes (acfg
, buf
, code
- buf
);
2581 *tramp_size
= code
- buf
+ kSizeOfMove
;
2585 #elif defined(TARGET_X86)
2587 guint8
*labels
[16];
2589 code
= buf
= g_malloc (256);
2591 /* Allocate a temporary stack slot */
2592 x86_push_reg (code
, X86_EAX
);
2594 x86_push_reg (code
, X86_EAX
);
2596 /* Load mscorlib got address */
2597 x86_mov_reg_membase (code
, X86_EAX
, MONO_ARCH_GOT_REG
, sizeof (target_mgreg_t
), 4);
2599 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, offset
* sizeof (target_mgreg_t
), 4);
2602 x86_alu_membase_imm (code
, X86_CMP
, X86_EAX
, 0, 0);
2604 x86_branch8 (code
, X86_CC_Z
, FALSE
, 0);
2607 x86_alu_membase_reg (code
, X86_CMP
, X86_EAX
, 0, MONO_ARCH_IMT_REG
);
2609 x86_branch8 (code
, X86_CC_Z
, FALSE
, 0);
2612 x86_alu_reg_imm (code
, X86_ADD
, X86_EAX
, 2 * sizeof (target_mgreg_t
));
2613 x86_jump_code (code
, labels
[0]);
2616 mono_x86_patch (labels
[2], code
);
2617 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, sizeof (target_mgreg_t
), 4);
2618 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, 0, 4);
2619 /* Save the target address to the temporary stack location */
2620 x86_mov_membase_reg (code
, X86_ESP
, 4, X86_EAX
, 4);
2622 x86_pop_reg (code
, X86_EAX
);
2623 /* Jump to the target address */
2627 mono_x86_patch (labels
[1], code
);
2628 /* Load fail tramp */
2629 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, sizeof (target_mgreg_t
), 4);
2630 x86_alu_membase_imm (code
, X86_CMP
, X86_EAX
, 0, 0);
2632 x86_branch8 (code
, X86_CC_Z
, FALSE
, 0);
2633 /* Jump to fail tramp */
2634 x86_mov_membase_reg (code
, X86_ESP
, 4, X86_EAX
, 4);
2635 x86_pop_reg (code
, X86_EAX
);
2639 mono_x86_patch (labels
[3], code
);
2640 x86_breakpoint (code
);
2642 emit_bytes (acfg
, buf
, code
- buf
);
2644 *tramp_size
= code
- buf
;
2648 #elif defined(TARGET_ARM)
2650 guint8
*code
, *code2
, *labels
[16];
2654 /* The IMT method is in v5 */
2656 /* Need at least two free registers, plus a slot for storing the pc */
2657 ARM_PUSH (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_R2
));
2659 /* Load the parameter from the GOT */
2660 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_PC
, 0);
2661 ARM_LDR_REG_REG (code
, ARMREG_R0
, ARMREG_PC
, ARMREG_R0
);
2664 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_R0
, 0);
2665 ARM_CMP_REG_REG (code
, ARMREG_R1
, ARMREG_V5
);
2667 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2669 /* End-of-loop check */
2670 ARM_CMP_REG_IMM (code
, ARMREG_R1
, 0, 0);
2672 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2675 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, sizeof (target_mgreg_t
) * 2);
2678 arm_patch (labels
[4], labels
[1]);
2681 arm_patch (labels
[2], code
);
2682 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2683 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 0);
2684 /* Save it to the third stack slot */
2685 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2686 /* Restore the registers and branch */
2687 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2690 arm_patch (labels
[3], code
);
2691 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2692 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2693 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2697 ARM_LDR_IMM (code2
, ARMREG_R0
, ARMREG_PC
, (code
- (labels
[0] + 8)));
2699 emit_bytes (acfg
, buf
, code
- buf
);
2700 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) + (code
- (labels
[0] + 8)) - 4);
2702 *tramp_size
= code
- buf
+ 4;
2703 #elif defined(TARGET_ARM64)
2704 arm64_emit_imt_trampoline (acfg
, offset
, tramp_size
);
2705 #elif defined(TARGET_POWERPC)
2707 guint8
*code
, *labels
[16];
2711 /* Load the mscorlib got address */
2712 ppc_ldptr (code
, ppc_r12
, sizeof (target_mgreg_t
), ppc_r30
);
2713 /* Load the parameter from the GOT */
2714 ppc_load (code
, ppc_r0
, offset
* sizeof (target_mgreg_t
));
2715 ppc_ldptr_indexed (code
, ppc_r12
, ppc_r12
, ppc_r0
);
2717 /* Load and check key */
2719 ppc_ldptr (code
, ppc_r0
, 0, ppc_r12
);
2720 ppc_cmp (code
, 0, sizeof (target_mgreg_t
) == 8 ? 1 : 0, ppc_r0
, MONO_ARCH_IMT_REG
);
2722 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
2724 /* End-of-loop check */
2725 ppc_cmpi (code
, 0, sizeof (target_mgreg_t
) == 8 ? 1 : 0, ppc_r0
, 0);
2727 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
2730 ppc_addi (code
, ppc_r12
, ppc_r12
, 2 * sizeof (target_mgreg_t
));
2733 mono_ppc_patch (labels
[4], labels
[1]);
2736 mono_ppc_patch (labels
[2], code
);
2737 ppc_ldptr (code
, ppc_r12
, sizeof (target_mgreg_t
), ppc_r12
);
2738 /* r12 now contains the value of the vtable slot */
2739 /* this is not a function descriptor on ppc64 */
2740 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
2741 ppc_mtctr (code
, ppc_r12
);
2742 ppc_bcctr (code
, PPC_BR_ALWAYS
, 0);
2745 mono_ppc_patch (labels
[3], code
);
2749 *tramp_size
= code
- buf
;
2751 emit_bytes (acfg
, buf
, code
- buf
);
2753 g_assert_not_reached ();
2758 #if defined (TARGET_AMD64)
2761 amd64_emit_load_got_slot (MonoAotCompile
*acfg
, int dreg
, int got_slot
)
2764 g_assert (acfg
->fp
);
2765 emit_unset_mode (acfg
);
2767 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %s\n", acfg
->got_symbol
, (unsigned int) ((got_slot
* sizeof (target_mgreg_t
))), mono_arch_regname (dreg
));
2774 * arch_emit_gsharedvt_arg_trampoline:
2776 * Emit code for a gsharedvt arg trampoline. OFFSET is the offset of the first of
2777 * two GOT slots which contain the argument, and the code to jump to.
2778 * TRAMP_SIZE is set to the size of the emitted trampoline.
2779 * These kinds of trampolines cannot be enumerated statically, since there could
2780 * be one trampoline per method instantiation, so we emit the same code for all
2781 * trampolines, and parameterize them using two GOT slots.
2784 arch_emit_gsharedvt_arg_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2786 #if defined(TARGET_X86)
2790 /* Similar to the PPC code above */
2792 g_assert (MONO_ARCH_RGCTX_REG
!= X86_ECX
);
2795 /* Load mscorlib got address */
2796 x86_mov_reg_membase (code
, X86_ECX
, MONO_ARCH_GOT_REG
, sizeof (target_mgreg_t
), 4);
2798 x86_mov_reg_membase (code
, X86_EAX
, X86_ECX
, offset
* sizeof (target_mgreg_t
), 4);
2799 /* Branch to the target address */
2800 x86_jump_membase (code
, X86_ECX
, (offset
+ 1) * sizeof (target_mgreg_t
));
2802 emit_bytes (acfg
, buf
, code
- buf
);
2805 g_assert (code
- buf
== *tramp_size
);
2806 #elif defined(TARGET_ARM)
2810 /* The same as mono_arch_get_gsharedvt_arg_trampoline (), but for AOT */
2811 /* Similar to arch_emit_specific_trampoline () */
2814 ARM_PUSH (code
, (1 << ARMREG_R0
) | (1 << ARMREG_R1
) | (1 << ARMREG_R2
) | (1 << ARMREG_R3
));
2815 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_PC
, 8);
2816 /* Load the arg value from the GOT */
2817 ARM_LDR_REG_REG (code
, ARMREG_R0
, ARMREG_PC
, ARMREG_R1
);
2818 /* Load the addr from the GOT */
2819 ARM_LDR_REG_REG (code
, ARMREG_R1
, ARMREG_PC
, ARMREG_R1
);
2821 ARM_BX (code
, ARMREG_R1
);
2823 g_assert (code
- buf
== 20);
2826 emit_bytes (acfg
, buf
, code
- buf
);
2827 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) + 4);
2828 #elif defined(TARGET_ARM64)
2829 arm64_emit_gsharedvt_arg_trampoline (acfg
, offset
, tramp_size
);
2830 #elif defined (TARGET_AMD64)
2832 amd64_emit_load_got_slot (acfg
, AMD64_RAX
, offset
);
2833 amd64_emit_load_got_slot (acfg
, MONO_ARCH_IMT_SCRATCH_REG
, offset
+ 1);
2834 g_assert (AMD64_R11
== MONO_ARCH_IMT_SCRATCH_REG
);
2835 fprintf (acfg
->fp
, "jmp *%%r11\n");
2839 g_assert_not_reached ();
2844 arch_emit_ftnptr_arg_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2846 #if defined(TARGET_ARM)
2853 /* Load target address and push it on stack */
2854 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 16);
2855 ARM_LDR_REG_REG (code
, ARMREG_IP
, ARMREG_PC
, ARMREG_IP
);
2856 ARM_PUSH (code
, 1 << ARMREG_IP
);
2857 /* Load argument in ARMREG_IP */
2858 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 8);
2859 ARM_LDR_REG_REG (code
, ARMREG_IP
, ARMREG_PC
, ARMREG_IP
);
2861 ARM_POP (code
, 1 << ARMREG_PC
);
2863 g_assert (code
- buf
== 24);
2866 emit_bytes (acfg
, buf
, code
- buf
);
2867 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", ((offset
+ 1) * sizeof (target_mgreg_t
)) + 12); // offset from ldr pc to addr
2868 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) + 4); // offset from ldr pc to arg
2870 g_assert_not_reached ();
2875 arch_emit_unbox_arbitrary_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2877 #if defined(TARGET_ARM64)
2878 emit_unset_mode (acfg
);
2879 fprintf (acfg
->fp
, "add x0, x0, %d\n", (int)(MONO_ABI_SIZEOF (MonoObject
)));
2880 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
);
2881 fprintf (acfg
->fp
, "br x17\n");
2882 *tramp_size
= 5 * 4;
2883 #elif defined (TARGET_AMD64)
2888 this_reg
= mono_arch_get_this_arg_reg (NULL
);
2890 amd64_alu_reg_imm (code
, X86_ADD
, this_reg
, MONO_ABI_SIZEOF (MonoObject
));
2891 emit_bytes (acfg
, buf
, code
- buf
);
2893 amd64_emit_load_got_slot (acfg
, AMD64_RAX
, offset
);
2894 fprintf (acfg
->fp
, "jmp *%%rax\n");
2897 #elif defined (TARGET_ARM)
2899 guint8
*code
, *label
;
2903 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, MONO_ABI_SIZEOF (MonoObject
));
2906 /* Calculate GOT slot */
2907 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 0);
2908 /* Load target addr into PC*/
2909 ARM_LDR_REG_REG (code
, ARMREG_PC
, ARMREG_PC
, ARMREG_IP
);
2911 g_assert (code
- buf
== 12);
2914 emit_bytes (acfg
, buf
, code
- buf
);
2915 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) + (code
- (label
+ 8)) - 4);
2916 *tramp_size
= 4 * 4;
2918 g_error ("NOT IMPLEMENTED: needed for AOT<>interp mixed mode transition");
2922 /* END OF ARCH SPECIFIC CODE */
2925 mono_get_field_token (MonoClassField
*field
)
2927 MonoClass
*klass
= field
->parent
;
2930 int fcount
= mono_class_get_field_count (klass
);
2931 MonoClassField
*klass_fields
= m_class_get_fields (klass
);
2932 for (i
= 0; i
< fcount
; ++i
) {
2933 if (field
== &klass_fields
[i
])
2934 return MONO_TOKEN_FIELD_DEF
| (mono_class_get_first_field_idx (klass
) + 1 + i
);
2937 g_assert_not_reached ();
2942 encode_value (gint32 value
, guint8
*buf
, guint8
**endbuf
)
2946 //printf ("ENCODE: %d 0x%x.\n", value, value);
2949 * Same encoding as the one used in the metadata, extended to handle values
2950 * greater than 0x1fffffff.
2952 if ((value
>= 0) && (value
<= 127))
2954 else if ((value
>= 0) && (value
<= 16383)) {
2955 p
[0] = 0x80 | (value
>> 8);
2956 p
[1] = value
& 0xff;
2958 } else if ((value
>= 0) && (value
<= 0x1fffffff)) {
2959 p
[0] = (value
>> 24) | 0xc0;
2960 p
[1] = (value
>> 16) & 0xff;
2961 p
[2] = (value
>> 8) & 0xff;
2962 p
[3] = value
& 0xff;
2967 p
[1] = (value
>> 24) & 0xff;
2968 p
[2] = (value
>> 16) & 0xff;
2969 p
[3] = (value
>> 8) & 0xff;
2970 p
[4] = value
& 0xff;
2978 stream_init (MonoDynamicStream
*sh
)
2981 sh
->alloc_size
= 4096;
2982 sh
->data
= (char *)g_malloc (4096);
2984 /* So offsets are > 0 */
2990 make_room_in_stream (MonoDynamicStream
*stream
, int size
)
2992 if (size
<= stream
->alloc_size
)
2995 while (stream
->alloc_size
<= size
) {
2996 if (stream
->alloc_size
< 4096)
2997 stream
->alloc_size
= 4096;
2999 stream
->alloc_size
*= 2;
3002 stream
->data
= (char *)g_realloc (stream
->data
, stream
->alloc_size
);
3006 add_stream_data (MonoDynamicStream
*stream
, const char *data
, guint32 len
)
3010 make_room_in_stream (stream
, stream
->index
+ len
);
3011 memcpy (stream
->data
+ stream
->index
, data
, len
);
3012 idx
= stream
->index
;
3013 stream
->index
+= len
;
3020 * Add data to the binary blob inside the aot image. Returns the offset inside the
3021 * blob where the data was stored.
3024 add_to_blob (MonoAotCompile
*acfg
, const guint8
*data
, guint32 data_len
)
3026 g_assert (!acfg
->blob_closed
);
3028 if (acfg
->blob
.alloc_size
== 0)
3029 stream_init (&acfg
->blob
);
3031 acfg
->stats
.blob_size
+= data_len
;
3033 return add_stream_data (&acfg
->blob
, (char*)data
, data_len
);
3037 add_to_blob_aligned (MonoAotCompile
*acfg
, const guint8
*data
, guint32 data_len
, guint32 align
)
3042 if (acfg
->blob
.alloc_size
== 0)
3043 stream_init (&acfg
->blob
);
3045 count
= acfg
->blob
.index
% align
;
3047 /* we assume the stream data will be aligned */
3049 add_stream_data (&acfg
->blob
, buf
, 4 - count
);
3051 return add_stream_data (&acfg
->blob
, (char*)data
, data_len
);
3054 /* Emit a table of data into the aot image */
3056 emit_aot_data (MonoAotCompile
*acfg
, MonoAotFileTable table
, const char *symbol
, guint8
*data
, int size
)
3058 if (acfg
->data_outfile
) {
3059 acfg
->table_offsets
[(int)table
] = acfg
->datafile_offset
;
3060 fwrite (data
,1, size
, acfg
->data_outfile
);
3061 acfg
->datafile_offset
+= size
;
3062 // align the data to 8 bytes. Put zeros in the file (so that every build results in consistent output).
3063 int align
= 8 - size
% 8;
3064 acfg
->datafile_offset
+= align
;
3065 guint8 align_buf
[16];
3066 memset (&align_buf
, 0, sizeof (align_buf
));
3067 fwrite (align_buf
, align
, 1, acfg
->data_outfile
);
3068 } else if (acfg
->llvm
) {
3069 mono_llvm_emit_aot_data (symbol
, data
, size
);
3071 emit_section_change (acfg
, RODATA_SECT
, 0);
3072 emit_alignment (acfg
, 8);
3073 emit_label (acfg
, symbol
);
3074 emit_bytes (acfg
, data
, size
);
3079 * emit_offset_table:
3081 * Emit a table of increasing offsets in a compact form using differential encoding.
3082 * There is an index entry for each GROUP_SIZE number of entries. The greater the
3083 * group size, the more compact the table becomes, but the slower it becomes to compute
3084 * a given entry. Returns the size of the table.
3087 emit_offset_table (MonoAotCompile
*acfg
, const char *symbol
, MonoAotFileTable table
, int noffsets
, int group_size
, gint32
*offsets
)
3089 gint32 current_offset
;
3090 int i
, buf_size
, ngroups
, index_entry_size
;
3092 guint8
*data_p
, *data_buf
;
3093 guint32
*index_offsets
;
3095 ngroups
= (noffsets
+ (group_size
- 1)) / group_size
;
3097 index_offsets
= g_new0 (guint32
, ngroups
);
3099 buf_size
= noffsets
* 4;
3100 p
= buf
= (guint8
*)g_malloc0 (buf_size
);
3103 for (i
= 0; i
< noffsets
; ++i
) {
3104 //printf ("D: %d -> %d\n", i, offsets [i]);
3105 if ((i
% group_size
) == 0) {
3106 index_offsets
[i
/ group_size
] = p
- buf
;
3107 /* Emit the full value for these entries */
3108 encode_value (offsets
[i
], p
, &p
);
3110 /* The offsets are allowed to be non-increasing */
3111 //g_assert (offsets [i] >= current_offset);
3112 encode_value (offsets
[i
] - current_offset
, p
, &p
);
3114 current_offset
= offsets
[i
];
3119 if (ngroups
&& index_offsets
[ngroups
- 1] < 65000)
3120 index_entry_size
= 2;
3122 index_entry_size
= 4;
3124 buf_size
= (data_p
- data_buf
) + (ngroups
* 4) + 16;
3125 p
= buf
= (guint8
*)g_malloc0 (buf_size
);
3127 /* Emit the header */
3128 encode_int (noffsets
, p
, &p
);
3129 encode_int (group_size
, p
, &p
);
3130 encode_int (ngroups
, p
, &p
);
3131 encode_int (index_entry_size
, p
, &p
);
3133 /* Emit the index */
3134 for (i
= 0; i
< ngroups
; ++i
) {
3135 if (index_entry_size
== 2)
3136 encode_int16 (index_offsets
[i
], p
, &p
);
3138 encode_int (index_offsets
[i
], p
, &p
);
3141 memcpy (p
, data_buf
, data_p
- data_buf
);
3142 p
+= data_p
- data_buf
;
3144 g_assert (p
- buf
<= buf_size
);
3146 emit_aot_data (acfg
, table
, symbol
, buf
, p
- buf
);
3151 return (int)(p
- buf
);
3155 get_image_index (MonoAotCompile
*cfg
, MonoImage
*image
)
3159 index
= GPOINTER_TO_UINT (g_hash_table_lookup (cfg
->image_hash
, image
));
3163 index
= g_hash_table_size (cfg
->image_hash
);
3164 g_hash_table_insert (cfg
->image_hash
, image
, GUINT_TO_POINTER (index
+ 1));
3165 g_ptr_array_add (cfg
->image_table
, image
);
3171 find_typespec_for_class (MonoAotCompile
*acfg
, MonoClass
*klass
)
3174 int len
= acfg
->image
->tables
[MONO_TABLE_TYPESPEC
].rows
;
3176 /* FIXME: Search referenced images as well */
3177 if (!acfg
->typespec_classes
) {
3178 acfg
->typespec_classes
= g_hash_table_new (NULL
, NULL
);
3179 for (i
= 0; i
< len
; i
++) {
3181 int typespec
= MONO_TOKEN_TYPE_SPEC
| (i
+ 1);
3182 MonoClass
*klass_key
= mono_class_get_and_inflate_typespec_checked (acfg
->image
, typespec
, NULL
, error
);
3183 if (!is_ok (error
)) {
3184 mono_error_cleanup (error
);
3187 g_hash_table_insert (acfg
->typespec_classes
, klass_key
, GINT_TO_POINTER (typespec
));
3190 return GPOINTER_TO_INT (g_hash_table_lookup (acfg
->typespec_classes
, klass
));
3194 encode_method_ref (MonoAotCompile
*acfg
, MonoMethod
*method
, guint8
*buf
, guint8
**endbuf
);
3197 encode_klass_ref (MonoAotCompile
*acfg
, MonoClass
*klass
, guint8
*buf
, guint8
**endbuf
);
3200 encode_ginst (MonoAotCompile
*acfg
, MonoGenericInst
*inst
, guint8
*buf
, guint8
**endbuf
);
3203 encode_type (MonoAotCompile
*acfg
, MonoType
*t
, guint8
*buf
, guint8
**endbuf
);
3206 get_shared_ginst_ref (MonoAotCompile
*acfg
, MonoGenericInst
*ginst
);
3209 encode_klass_ref_inner (MonoAotCompile
*acfg
, MonoClass
*klass
, guint8
*buf
, guint8
**endbuf
)
3214 * The encoding begins with one of the MONO_AOT_TYPEREF values, followed by additional
3218 if (mono_class_is_ginst (klass
)) {
3220 g_assert (m_class_get_type_token (klass
));
3222 /* Find a typespec for a class if possible */
3223 token
= find_typespec_for_class (acfg
, klass
);
3225 encode_value (MONO_AOT_TYPEREF_TYPESPEC_TOKEN
, p
, &p
);
3226 encode_value (token
, p
, &p
);
3228 MonoClass
*gclass
= mono_class_get_generic_class (klass
)->container_class
;
3229 MonoGenericInst
*inst
= mono_class_get_generic_class (klass
)->context
.class_inst
;
3230 static int count
= 0;
3233 encode_value (MONO_AOT_TYPEREF_GINST
, p
, &p
);
3234 encode_klass_ref (acfg
, gclass
, p
, &p
);
3235 guint32 offset
= get_shared_ginst_ref (acfg
, inst
);
3236 encode_value (offset
, p
, &p
);
3240 } else if (m_class_get_type_token (klass
)) {
3241 int iindex
= get_image_index (acfg
, m_class_get_image (klass
));
3243 g_assert (mono_metadata_token_code (m_class_get_type_token (klass
)) == MONO_TOKEN_TYPE_DEF
);
3245 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX
, p
, &p
);
3246 encode_value (m_class_get_type_token (klass
) - MONO_TOKEN_TYPE_DEF
, p
, &p
);
3248 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE
, p
, &p
);
3249 encode_value (m_class_get_type_token (klass
) - MONO_TOKEN_TYPE_DEF
, p
, &p
);
3250 encode_value (get_image_index (acfg
, m_class_get_image (klass
)), p
, &p
);
3252 } else if ((m_class_get_byval_arg (klass
)->type
== MONO_TYPE_VAR
) || (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_MVAR
)) {
3253 MonoGenericContainer
*container
= mono_type_get_generic_param_owner (m_class_get_byval_arg (klass
));
3254 MonoGenericParam
*par
= m_class_get_byval_arg (klass
)->data
.generic_param
;
3256 encode_value (MONO_AOT_TYPEREF_VAR
, p
, &p
);
3258 encode_value (par
->gshared_constraint
? 1 : 0, p
, &p
);
3259 if (par
->gshared_constraint
) {
3260 MonoGSharedGenericParam
*gpar
= (MonoGSharedGenericParam
*)par
;
3261 encode_type (acfg
, par
->gshared_constraint
, p
, &p
);
3262 encode_klass_ref (acfg
, mono_class_create_generic_parameter (gpar
->parent
), p
, &p
);
3264 encode_value (m_class_get_byval_arg (klass
)->type
, p
, &p
);
3265 encode_value (mono_type_get_generic_param_num (m_class_get_byval_arg (klass
)), p
, &p
);
3267 encode_value (container
->is_anonymous
? 0 : 1, p
, &p
);
3269 if (!container
->is_anonymous
) {
3270 encode_value (container
->is_method
, p
, &p
);
3271 if (container
->is_method
)
3272 encode_method_ref (acfg
, container
->owner
.method
, p
, &p
);
3274 encode_klass_ref (acfg
, container
->owner
.klass
, p
, &p
);
3277 } else if (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_PTR
) {
3278 encode_value (MONO_AOT_TYPEREF_PTR
, p
, &p
);
3279 encode_type (acfg
, m_class_get_byval_arg (klass
), p
, &p
);
3282 g_assert (m_class_get_rank (klass
) > 0);
3283 encode_value (MONO_AOT_TYPEREF_ARRAY
, p
, &p
);
3284 encode_value (m_class_get_rank (klass
), p
, &p
);
3285 encode_klass_ref (acfg
, m_class_get_element_class (klass
), p
, &p
);
3288 acfg
->stats
.class_ref_count
++;
3289 acfg
->stats
.class_ref_size
+= p
- buf
;
3295 get_shared_klass_ref (MonoAotCompile
*acfg
, MonoClass
*klass
)
3297 guint offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->klass_blob_hash
, klass
));
3301 buf2
= (guint8
*)g_malloc (1024);
3304 encode_klass_ref_inner (acfg
, klass
, p
, &p
);
3305 g_assert (p
- buf2
< 1024);
3307 offset
= add_to_blob (acfg
, buf2
, p
- buf2
);
3310 g_hash_table_insert (acfg
->klass_blob_hash
, klass
, GUINT_TO_POINTER (offset
+ 1));
3321 * Encode a reference to KLASS. We use our home-grown encoding instead of the
3322 * standard metadata encoding.
3325 encode_klass_ref (MonoAotCompile
*acfg
, MonoClass
*klass
, guint8
*buf
, guint8
**endbuf
)
3327 gboolean shared
= FALSE
;
3330 * The encoding of generic instances is large so emit them only once.
3332 if (mono_class_is_ginst (klass
)) {
3334 g_assert (m_class_get_type_token (klass
));
3336 /* Find a typespec for a class if possible */
3337 token
= find_typespec_for_class (acfg
, klass
);
3340 } else if ((m_class_get_byval_arg (klass
)->type
== MONO_TYPE_VAR
) || (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_MVAR
)) {
3346 guint32 offset
= get_shared_klass_ref (acfg
, klass
);
3349 encode_value (MONO_AOT_TYPEREF_BLOB_INDEX
, p
, &p
);
3350 encode_value (offset
, p
, &p
);
3355 encode_klass_ref_inner (acfg
, klass
, buf
, endbuf
);
3359 encode_field_info (MonoAotCompile
*cfg
, MonoClassField
*field
, guint8
*buf
, guint8
**endbuf
)
3361 guint32 token
= mono_get_field_token (field
);
3364 encode_klass_ref (cfg
, field
->parent
, p
, &p
);
3365 g_assert (mono_metadata_token_code (token
) == MONO_TOKEN_FIELD_DEF
);
3366 encode_value (token
- MONO_TOKEN_FIELD_DEF
, p
, &p
);
3371 encode_ginst (MonoAotCompile
*acfg
, MonoGenericInst
*inst
, guint8
*buf
, guint8
**endbuf
)
3376 encode_value (inst
->type_argc
, p
, &p
);
3377 for (i
= 0; i
< inst
->type_argc
; ++i
)
3378 encode_klass_ref (acfg
, mono_class_from_mono_type_internal (inst
->type_argv
[i
]), p
, &p
);
3380 acfg
->stats
.ginst_count
++;
3381 acfg
->stats
.ginst_size
+= p
- buf
;
3387 get_shared_ginst_ref (MonoAotCompile
*acfg
, MonoGenericInst
*ginst
)
3389 guint32 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->ginst_blob_hash
, ginst
));
3393 buf2
= (guint8
*)g_malloc (1024);
3396 encode_ginst (acfg
, ginst
, p2
, &p2
);
3397 g_assert (p2
- buf2
< 1024);
3399 offset
= add_to_blob (acfg
, buf2
, p2
- buf2
);
3402 g_hash_table_insert (acfg
->ginst_blob_hash
, ginst
, GUINT_TO_POINTER (offset
+ 1));
3410 encode_generic_context (MonoAotCompile
*acfg
, MonoGenericContext
*context
, guint8
*buf
, guint8
**endbuf
)
3413 MonoGenericInst
*inst
;
3414 guint32 flags
= (context
->class_inst
? 1 : 0) | (context
->method_inst
? 2 : 0);
3418 encode_value (flags
, p
, &p
);
3419 inst
= context
->class_inst
;
3421 guint32 offset
= get_shared_ginst_ref (acfg
, inst
);
3422 encode_value (offset
, p
, &p
);
3424 inst
= context
->method_inst
;
3426 guint32 offset
= get_shared_ginst_ref (acfg
, inst
);
3427 encode_value (offset
, p
, &p
);
3433 encode_type (MonoAotCompile
*acfg
, MonoType
*t
, guint8
*buf
, guint8
**endbuf
)
3438 int count
= mono_type_custom_modifier_count (t
);
3440 *p
= MONO_TYPE_CMOD_REQD
;
3443 encode_value (count
, p
, &p
);
3444 for (int i
= 0; i
< count
; ++i
) {
3447 MonoType
*cmod_type
= mono_type_get_custom_modifier (t
, i
, &required
, error
);
3448 mono_error_assert_ok (error
);
3449 encode_value (required
, p
, &p
);
3450 encode_type (acfg
, cmod_type
, p
, &p
);
3454 /* t->attrs can be ignored */
3455 //g_assert (t->attrs == 0);
3458 *p
= MONO_TYPE_PINNED
;
3462 *p
= MONO_TYPE_BYREF
;
3470 case MONO_TYPE_VOID
:
3471 case MONO_TYPE_BOOLEAN
:
3472 case MONO_TYPE_CHAR
:
3485 case MONO_TYPE_STRING
:
3486 case MONO_TYPE_OBJECT
:
3487 case MONO_TYPE_TYPEDBYREF
:
3489 case MONO_TYPE_VALUETYPE
:
3490 case MONO_TYPE_CLASS
:
3491 encode_klass_ref (acfg
, mono_class_from_mono_type_internal (t
), p
, &p
);
3493 case MONO_TYPE_SZARRAY
:
3494 encode_klass_ref (acfg
, t
->data
.klass
, p
, &p
);
3497 encode_type (acfg
, t
->data
.type
, p
, &p
);
3499 case MONO_TYPE_GENERICINST
: {
3500 MonoClass
*gclass
= t
->data
.generic_class
->container_class
;
3501 MonoGenericInst
*inst
= t
->data
.generic_class
->context
.class_inst
;
3503 encode_klass_ref (acfg
, gclass
, p
, &p
);
3504 encode_ginst (acfg
, inst
, p
, &p
);
3507 case MONO_TYPE_ARRAY
: {
3508 MonoArrayType
*array
= t
->data
.array
;
3511 encode_klass_ref (acfg
, array
->eklass
, p
, &p
);
3512 encode_value (array
->rank
, p
, &p
);
3513 encode_value (array
->numsizes
, p
, &p
);
3514 for (i
= 0; i
< array
->numsizes
; ++i
)
3515 encode_value (array
->sizes
[i
], p
, &p
);
3516 encode_value (array
->numlobounds
, p
, &p
);
3517 for (i
= 0; i
< array
->numlobounds
; ++i
)
3518 encode_value (array
->lobounds
[i
], p
, &p
);
3522 case MONO_TYPE_MVAR
:
3523 encode_klass_ref (acfg
, mono_class_from_mono_type_internal (t
), p
, &p
);
3526 g_assert_not_reached ();
3533 encode_signature (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
, guint8
*buf
, guint8
**endbuf
)
3539 /* Similar to the metadata encoding */
3540 if (sig
->generic_param_count
)
3544 if (sig
->explicit_this
)
3546 flags
|= (sig
->call_convention
& 0x0F);
3550 if (sig
->generic_param_count
)
3551 encode_value (sig
->generic_param_count
, p
, &p
);
3552 encode_value (sig
->param_count
, p
, &p
);
3554 encode_type (acfg
, sig
->ret
, p
, &p
);
3555 for (i
= 0; i
< sig
->param_count
; ++i
) {
3556 if (sig
->sentinelpos
== i
) {
3557 *p
= MONO_TYPE_SENTINEL
;
3560 encode_type (acfg
, sig
->params
[i
], p
, &p
);
3566 #define MAX_IMAGE_INDEX 250
3569 encode_method_ref (MonoAotCompile
*acfg
, MonoMethod
*method
, guint8
*buf
, guint8
**endbuf
)
3571 guint32 image_index
= get_image_index (acfg
, m_class_get_image (method
->klass
));
3572 guint32 token
= method
->token
;
3573 MonoJumpInfoToken
*ji
;
3577 * The encoding for most methods is as follows:
3578 * - image index encoded as a leb128
3579 * - token index encoded as a leb128
3580 * Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
3581 * types of method encodings.
3584 /* Mark methods which can't use aot trampolines because they need the further
3585 * processing in mono_magic_trampoline () which requires a MonoMethod*.
3587 if ((method
->is_generic
&& (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)) ||
3588 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
))
3589 encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE
<< 24), p
, &p
);
3591 if (method
->wrapper_type
) {
3592 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
3594 encode_value ((MONO_AOT_METHODREF_WRAPPER
<< 24), p
, &p
);
3596 encode_value (method
->wrapper_type
, p
, &p
);
3598 switch (method
->wrapper_type
) {
3599 case MONO_WRAPPER_REMOTING_INVOKE
:
3600 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
3601 case MONO_WRAPPER_XDOMAIN_INVOKE
: {
3604 m
= mono_marshal_method_from_wrapper (method
);
3606 encode_method_ref (acfg
, m
, p
, &p
);
3609 case MONO_WRAPPER_PROXY_ISINST
:
3610 case MONO_WRAPPER_LDFLD
:
3611 case MONO_WRAPPER_LDFLDA
:
3612 case MONO_WRAPPER_STFLD
: {
3614 encode_klass_ref (acfg
, info
->d
.proxy
.klass
, p
, &p
);
3617 case MONO_WRAPPER_ALLOC
: {
3618 /* The GC name is saved once in MonoAotFileInfo */
3619 g_assert (info
->d
.alloc
.alloc_type
!= -1);
3620 encode_value (info
->d
.alloc
.alloc_type
, p
, &p
);
3623 case MONO_WRAPPER_WRITE_BARRIER
: {
3627 case MONO_WRAPPER_STELEMREF
: {
3629 encode_value (info
->subtype
, p
, &p
);
3630 if (info
->subtype
== WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
)
3631 encode_value (info
->d
.virtual_stelemref
.kind
, p
, &p
);
3634 case MONO_WRAPPER_OTHER
: {
3636 encode_value (info
->subtype
, p
, &p
);
3637 if (info
->subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
||
3638 info
->subtype
== WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
)
3639 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3640 else if (info
->subtype
== WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
)
3641 encode_method_ref (acfg
, info
->d
.synchronized_inner
.method
, p
, &p
);
3642 else if (info
->subtype
== WRAPPER_SUBTYPE_ARRAY_ACCESSOR
)
3643 encode_method_ref (acfg
, info
->d
.array_accessor
.method
, p
, &p
);
3644 else if (info
->subtype
== WRAPPER_SUBTYPE_INTERP_IN
)
3645 encode_signature (acfg
, info
->d
.interp_in
.sig
, p
, &p
);
3646 else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
)
3647 encode_signature (acfg
, info
->d
.gsharedvt
.sig
, p
, &p
);
3648 else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
)
3649 encode_signature (acfg
, info
->d
.gsharedvt
.sig
, p
, &p
);
3650 else if (info
->subtype
== WRAPPER_SUBTYPE_INTERP_LMF
)
3651 encode_value (info
->d
.icall
.jit_icall_id
, p
, &p
);
3652 else if (info
->subtype
== WRAPPER_SUBTYPE_AOT_INIT
)
3653 encode_value (info
->d
.aot_init
.subtype
, p
, &p
);
3654 else if (info
->subtype
== WRAPPER_SUBTYPE_LLVM_FUNC
)
3655 encode_value (info
->d
.llvm_func
.subtype
, p
, &p
);
3658 case MONO_WRAPPER_MANAGED_TO_NATIVE
: {
3660 encode_value (info
->subtype
, p
, &p
);
3661 if (info
->subtype
== WRAPPER_SUBTYPE_ICALL_WRAPPER
) {
3662 encode_value (info
->d
.icall
.jit_icall_id
, p
, &p
);
3663 } else if (info
->subtype
== WRAPPER_SUBTYPE_NATIVE_FUNC_AOT
) {
3664 encode_method_ref (acfg
, info
->d
.managed_to_native
.method
, p
, &p
);
3666 g_assert (info
->subtype
== WRAPPER_SUBTYPE_NONE
|| info
->subtype
== WRAPPER_SUBTYPE_PINVOKE
);
3667 encode_method_ref (acfg
, info
->d
.managed_to_native
.method
, p
, &p
);
3671 case MONO_WRAPPER_SYNCHRONIZED
: {
3674 m
= mono_marshal_method_from_wrapper (method
);
3676 g_assert (m
!= method
);
3677 encode_method_ref (acfg
, m
, p
, &p
);
3680 case MONO_WRAPPER_MANAGED_TO_MANAGED
: {
3682 encode_value (info
->subtype
, p
, &p
);
3684 if (info
->subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
) {
3685 encode_value (info
->d
.element_addr
.rank
, p
, &p
);
3686 encode_value (info
->d
.element_addr
.elem_size
, p
, &p
);
3687 } else if (info
->subtype
== WRAPPER_SUBTYPE_STRING_CTOR
) {
3688 encode_method_ref (acfg
, info
->d
.string_ctor
.method
, p
, &p
);
3690 g_assert_not_reached ();
3694 case MONO_WRAPPER_CASTCLASS
: {
3696 encode_value (info
->subtype
, p
, &p
);
3699 case MONO_WRAPPER_RUNTIME_INVOKE
: {
3701 encode_value (info
->subtype
, p
, &p
);
3702 if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
|| info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
)
3703 encode_method_ref (acfg
, info
->d
.runtime_invoke
.method
, p
, &p
);
3704 else if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL
)
3705 encode_signature (acfg
, info
->d
.runtime_invoke
.sig
, p
, &p
);
3708 case MONO_WRAPPER_DELEGATE_INVOKE
:
3709 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
3710 case MONO_WRAPPER_DELEGATE_END_INVOKE
: {
3711 if (method
->is_inflated
) {
3712 /* These wrappers are identified by their class */
3713 encode_value (1, p
, &p
);
3714 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3716 MonoMethodSignature
*sig
= mono_method_signature_internal (method
);
3717 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
3719 encode_value (0, p
, &p
);
3720 if (method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
3721 encode_value (info
? info
->subtype
: 0, p
, &p
);
3722 encode_signature (acfg
, sig
, p
, &p
);
3726 case MONO_WRAPPER_NATIVE_TO_MANAGED
: {
3728 encode_method_ref (acfg
, info
->d
.native_to_managed
.method
, p
, &p
);
3729 encode_klass_ref (acfg
, info
->d
.native_to_managed
.klass
, p
, &p
);
3733 g_assert_not_reached ();
3735 } else if (mono_method_signature_internal (method
)->is_inflated
) {
3737 * This is a generic method, find the original token which referenced it and
3739 * Obtain the token from information recorded by the JIT.
3741 ji
= (MonoJumpInfoToken
*)g_hash_table_lookup (acfg
->token_info_hash
, method
);
3743 image_index
= get_image_index (acfg
, ji
->image
);
3744 g_assert (image_index
< MAX_IMAGE_INDEX
);
3747 encode_value ((MONO_AOT_METHODREF_METHODSPEC
<< 24), p
, &p
);
3748 encode_value (image_index
, p
, &p
);
3749 encode_value (token
, p
, &p
);
3750 } else if (g_hash_table_lookup (acfg
->method_blob_hash
, method
)) {
3751 /* Already emitted as part of an rgctx fetch */
3752 guint32 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_blob_hash
, method
));
3755 encode_value ((MONO_AOT_METHODREF_BLOB_INDEX
<< 24), p
, &p
);
3756 encode_value (offset
, p
, &p
);
3758 MonoMethod
*declaring
;
3759 MonoGenericContext
*context
= mono_method_get_context (method
);
3761 g_assert (method
->is_inflated
);
3762 declaring
= ((MonoMethodInflated
*)method
)->declaring
;
3765 * This might be a non-generic method of a generic instance, which
3766 * doesn't have a token since the reference is generated by the JIT
3767 * like Nullable:Box/Unbox, or by generic sharing.
3769 encode_value ((MONO_AOT_METHODREF_GINST
<< 24), p
, &p
);
3770 /* Encode the klass */
3771 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3772 /* Encode the method */
3773 image_index
= get_image_index (acfg
, m_class_get_image (method
->klass
));
3774 g_assert (image_index
< MAX_IMAGE_INDEX
);
3775 g_assert (declaring
->token
);
3776 token
= declaring
->token
;
3777 g_assert (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
);
3778 encode_value (image_index
, p
, &p
);
3779 encode_value (mono_metadata_token_index (token
), p
, &p
);
3780 encode_generic_context (acfg
, context
, p
, &p
);
3782 } else if (token
== 0) {
3783 /* This might be a method of a constructed type like int[,].Set */
3784 /* Obtain the token from information recorded by the JIT */
3785 ji
= (MonoJumpInfoToken
*)g_hash_table_lookup (acfg
->token_info_hash
, method
);
3787 image_index
= get_image_index (acfg
, ji
->image
);
3788 g_assert (image_index
< MAX_IMAGE_INDEX
);
3791 encode_value ((MONO_AOT_METHODREF_METHODSPEC
<< 24), p
, &p
);
3792 encode_value (image_index
, p
, &p
);
3793 encode_value (token
, p
, &p
);
3796 g_assert (m_class_get_rank (method
->klass
));
3798 /* Encode directly */
3799 encode_value ((MONO_AOT_METHODREF_ARRAY
<< 24), p
, &p
);
3800 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3801 if (!strcmp (method
->name
, ".ctor") && mono_method_signature_internal (method
)->param_count
== m_class_get_rank (method
->klass
))
3802 encode_value (0, p
, &p
);
3803 else if (!strcmp (method
->name
, ".ctor") && mono_method_signature_internal (method
)->param_count
== m_class_get_rank (method
->klass
) * 2)
3804 encode_value (1, p
, &p
);
3805 else if (!strcmp (method
->name
, "Get"))
3806 encode_value (2, p
, &p
);
3807 else if (!strcmp (method
->name
, "Address"))
3808 encode_value (3, p
, &p
);
3809 else if (!strcmp (method
->name
, "Set"))
3810 encode_value (4, p
, &p
);
3812 g_assert_not_reached ();
3815 g_assert (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
);
3817 if (image_index
>= MONO_AOT_METHODREF_MIN
) {
3818 encode_value ((MONO_AOT_METHODREF_LARGE_IMAGE_INDEX
<< 24), p
, &p
);
3819 encode_value (image_index
, p
, &p
);
3820 encode_value (mono_metadata_token_index (token
), p
, &p
);
3822 encode_value ((image_index
<< 24) | mono_metadata_token_index (token
), p
, &p
);
3826 acfg
->stats
.method_ref_count
++;
3827 acfg
->stats
.method_ref_size
+= p
- buf
;
3833 get_shared_method_ref (MonoAotCompile
*acfg
, MonoMethod
*method
)
3835 guint32 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_blob_hash
, method
));
3839 buf2
= (guint8
*)g_malloc (1024);
3842 encode_method_ref (acfg
, method
, p2
, &p2
);
3843 g_assert (p2
- buf2
< 1024);
3845 offset
= add_to_blob (acfg
, buf2
, p2
- buf2
);
3848 g_hash_table_insert (acfg
->method_blob_hash
, method
, GUINT_TO_POINTER (offset
+ 1));
3856 compare_patches (gconstpointer a
, gconstpointer b
)
3860 i
= (*(MonoJumpInfo
**)a
)->ip
.i
;
3861 j
= (*(MonoJumpInfo
**)b
)->ip
.i
;
3872 static G_GNUC_UNUSED
char*
3873 patch_to_string (MonoJumpInfo
*patch_info
)
3877 str
= g_string_new ("");
3879 g_string_append_printf (str
, "%s(", get_patch_name (patch_info
->type
));
3881 switch (patch_info
->type
) {
3882 case MONO_PATCH_INFO_VTABLE
:
3883 mono_type_get_desc (str
, m_class_get_byval_arg (patch_info
->data
.klass
), TRUE
);
3888 g_string_append_printf (str
, ")");
3889 return g_string_free (str
, FALSE
);
3895 * Return whenever PATCH_INFO refers to a direct call, and thus requires a
3899 is_plt_patch (MonoJumpInfo
*patch_info
)
3901 switch (patch_info
->type
) {
3902 case MONO_PATCH_INFO_METHOD
:
3903 case MONO_PATCH_INFO_JIT_ICALL_ID
:
3904 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
3905 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
3906 case MONO_PATCH_INFO_RGCTX_FETCH
:
3907 case MONO_PATCH_INFO_SPECIFIC_TRAMPOLINE_LAZY_FETCH_ADDR
:
3909 case MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
:
3918 * Return the symbol identifying the plt entry PLT_OFFSET.
3921 get_plt_symbol (MonoAotCompile
*acfg
, int plt_offset
, MonoJumpInfo
*patch_info
)
3925 * The Apple linker reorganizes object files, so it doesn't like branches to local
3926 * labels, since those have no relocations.
3928 return g_strdup_printf ("%sp_%d", acfg
->llvm_label_prefix
, plt_offset
);
3930 return g_strdup_printf ("%sp_%d", acfg
->temp_prefix
, plt_offset
);
3937 * Return a PLT entry which belongs to the method identified by PATCH_INFO.
3939 static MonoPltEntry
*
3940 get_plt_entry (MonoAotCompile
*acfg
, MonoJumpInfo
*patch_info
)
3943 gboolean synchronized
= FALSE
;
3944 static int synchronized_symbol_idx
;
3946 if (!is_plt_patch (patch_info
))
3949 if (!acfg
->patch_to_plt_entry
[patch_info
->type
])
3950 acfg
->patch_to_plt_entry
[patch_info
->type
] = g_hash_table_new (mono_patch_info_hash
, mono_patch_info_equal
);
3951 res
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->patch_to_plt_entry
[patch_info
->type
], patch_info
);
3953 if (!acfg
->llvm
&& patch_info
->type
== MONO_PATCH_INFO_METHOD
&& (patch_info
->data
.method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
)) {
3955 * Allocate a separate PLT slot for each such patch, since some plt
3956 * entries will refer to the method itself, and some will refer to the
3960 synchronized
= TRUE
;
3964 MonoJumpInfo
*new_ji
;
3966 new_ji
= mono_patch_info_dup_mp (acfg
->mempool
, patch_info
);
3968 res
= (MonoPltEntry
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoPltEntry
));
3969 res
->plt_offset
= acfg
->plt_offset
;
3971 res
->symbol
= get_plt_symbol (acfg
, res
->plt_offset
, patch_info
);
3972 if (acfg
->aot_opts
.write_symbols
)
3973 res
->debug_sym
= get_plt_entry_debug_sym (acfg
, res
->ji
, acfg
->plt_entry_debug_sym_cache
);
3975 /* Avoid duplicate symbols because we don't cache */
3976 res
->symbol
= g_strdup_printf ("%s_%d", res
->symbol
, synchronized_symbol_idx
);
3978 res
->debug_sym
= g_strdup_printf ("%s_%d", res
->debug_sym
, synchronized_symbol_idx
);
3979 synchronized_symbol_idx
++;
3983 res
->llvm_symbol
= g_strdup_printf ("%s_%s_llvm", res
->symbol
, res
->debug_sym
);
3985 res
->llvm_symbol
= g_strdup_printf ("%s_llvm", res
->symbol
);
3986 if (strstr (res
->llvm_symbol
, acfg
->temp_prefix
) == res
->llvm_symbol
) {
3987 /* The llvm symbol shouldn't be temporary, since the llvm generated object file references it */
3988 char *tmp
= res
->llvm_symbol
;
3989 res
->llvm_symbol
= g_strdup (res
->llvm_symbol
+ strlen (acfg
->temp_prefix
));
3993 g_hash_table_insert (acfg
->patch_to_plt_entry
[new_ji
->type
], new_ji
, res
);
3995 g_hash_table_insert (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (res
->plt_offset
), res
);
3997 //g_assert (mono_patch_info_equal (patch_info, new_ji));
3998 //mono_print_ji (patch_info); printf ("\n");
3999 //g_hash_table_print_stats (acfg->patch_to_plt_entry);
4001 acfg
->plt_offset
++;
4008 lookup_got_offset (MonoAotCompile
*acfg
, gboolean llvm
, MonoJumpInfo
*ji
)
4011 GotInfo
*info
= llvm
? &acfg
->llvm_got_info
: &acfg
->got_info
;
4013 got_offset
= GPOINTER_TO_UINT (g_hash_table_lookup (info
->patch_to_got_offset_by_type
[ji
->type
], ji
));
4015 return got_offset
- 1;
4016 g_assert_not_reached ();
4022 * Returns the offset of the GOT slot where the runtime object resulting from resolving
4023 * JI could be found if it exists, otherwise allocates a new one.
4026 get_got_offset (MonoAotCompile
*acfg
, gboolean llvm
, MonoJumpInfo
*ji
)
4029 GotInfo
*info
= llvm
? &acfg
->llvm_got_info
: &acfg
->got_info
;
4031 got_offset
= GPOINTER_TO_UINT (g_hash_table_lookup (info
->patch_to_got_offset_by_type
[ji
->type
], ji
));
4033 return got_offset
- 1;
4036 got_offset
= acfg
->llvm_got_offset
;
4037 acfg
->llvm_got_offset
++;
4039 got_offset
= acfg
->got_offset
;
4040 acfg
->got_offset
++;
4043 acfg
->stats
.got_slots
++;
4044 acfg
->stats
.got_slot_types
[ji
->type
] ++;
4046 g_hash_table_insert (info
->patch_to_got_offset
, ji
, GUINT_TO_POINTER (got_offset
+ 1));
4047 g_hash_table_insert (info
->patch_to_got_offset_by_type
[ji
->type
], ji
, GUINT_TO_POINTER (got_offset
+ 1));
4048 g_ptr_array_add (info
->got_patches
, ji
);
4053 /* Add a method to the list of methods which need to be emitted */
4055 add_method_with_index (MonoAotCompile
*acfg
, MonoMethod
*method
, int index
, gboolean extra
)
4058 if (!g_hash_table_lookup (acfg
->method_indexes
, method
)) {
4059 g_ptr_array_add (acfg
->methods
, method
);
4060 g_hash_table_insert (acfg
->method_indexes
, method
, GUINT_TO_POINTER (index
+ 1));
4061 acfg
->nmethods
= acfg
->methods
->len
+ 1;
4064 if (method
->wrapper_type
|| extra
) {
4065 int token
= mono_metadata_token_index (method
->token
) - 1;
4067 acfg
->nextra_methods
++;
4068 g_ptr_array_add (acfg
->extra_methods
, method
);
4073 prefer_gsharedvt_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
4075 /* One instantiation with valuetypes is generated for each async method */
4076 if (m_class_get_image (method
->klass
) == mono_defaults
.corlib
&& (!strcmp (m_class_get_name (method
->klass
), "AsyncMethodBuilderCore") || !strcmp (m_class_get_name (method
->klass
), "AsyncVoidMethodBuilder")))
4083 get_method_index (MonoAotCompile
*acfg
, MonoMethod
*method
)
4085 int index
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_indexes
, method
));
4093 add_method_full (MonoAotCompile
*acfg
, MonoMethod
*method
, gboolean extra
, int depth
)
4097 index
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_indexes
, method
));
4101 index
= acfg
->method_index
;
4102 add_method_with_index (acfg
, method
, index
, extra
);
4104 g_ptr_array_add (acfg
->method_order
, GUINT_TO_POINTER (index
));
4106 g_hash_table_insert (acfg
->method_depth
, method
, GUINT_TO_POINTER (depth
));
4108 acfg
->method_index
++;
4114 add_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
4116 return add_method_full (acfg
, method
, FALSE
, 0);
4120 mono_dedup_cache_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
4122 g_assert (acfg
->dedup_stats
);
4124 char *name
= mono_aot_get_mangled_method_name (method
);
4128 char *stats_name
= g_strdup (name
);
4130 g_assert (acfg
->dedup_cache
);
4132 if (!g_hash_table_lookup (acfg
->dedup_cache
, name
)) {
4133 // This AOTCompile owns this method
4134 // We do this to decide whether to write it to disk
4135 // during a dedup run (first phase, where we skip).
4137 // If never changed, then maybe can avoid a recompile
4140 // Files not read in during last phase.
4141 acfg
->dedup_cache_changed
= TRUE
;
4144 g_hash_table_insert (acfg
->dedup_cache
, name
, method
);
4150 guint count
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->dedup_stats
, stats_name
));
4152 g_hash_table_insert (acfg
->dedup_stats
, stats_name
, GUINT_TO_POINTER (count
));
4156 add_extra_method_with_depth (MonoAotCompile
*acfg
, MonoMethod
*method
, int depth
)
4160 if (mono_method_is_generic_sharable_full (method
, TRUE
, TRUE
, FALSE
)) {
4161 method
= mini_get_shared_method_full (method
, SHARE_MODE_NONE
, error
);
4162 if (!is_ok (error
)) {
4163 /* vtype constraint */
4164 mono_error_cleanup (error
);
4167 } else if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && prefer_gsharedvt_method (acfg
, method
) && mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, TRUE
)) {
4168 /* Use the gsharedvt version */
4169 method
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
4170 mono_error_assert_ok (error
);
4173 if ((acfg
->aot_opts
.dedup
|| acfg
->aot_opts
.dedup_include
) && mono_aot_can_dedup (method
)) {
4174 mono_dedup_cache_method (acfg
, method
);
4176 if (!acfg
->dedup_emit_mode
)
4180 if (acfg
->aot_opts
.log_generics
)
4181 aot_printf (acfg
, "%*sAdding method %s.\n", depth
, "", mono_method_get_full_name (method
));
4183 add_method_full (acfg
, method
, TRUE
, depth
);
4187 add_extra_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
4189 add_extra_method_with_depth (acfg
, method
, 0);
4193 add_jit_icall_wrapper (MonoAotCompile
*acfg
, MonoJitICallInfo
*callinfo
)
4198 g_assert (callinfo
->name
&& callinfo
->func
);
4200 add_method (acfg
, mono_marshal_get_icall_wrapper (callinfo
, TRUE
));
4206 add_lazy_init_wrappers (MonoAotCompile
*acfg
)
4208 add_method (acfg
, mono_marshal_get_aot_init_wrapper (AOT_INIT_METHOD
));
4209 add_method (acfg
, mono_marshal_get_aot_init_wrapper (AOT_INIT_METHOD_GSHARED_MRGCTX
));
4210 add_method (acfg
, mono_marshal_get_aot_init_wrapper (AOT_INIT_METHOD_GSHARED_VTABLE
));
4211 add_method (acfg
, mono_marshal_get_aot_init_wrapper (AOT_INIT_METHOD_GSHARED_THIS
));
4217 get_runtime_invoke_sig (MonoMethodSignature
*sig
)
4219 MonoMethodBuilder
*mb
;
4222 mb
= mono_mb_new (mono_defaults
.object_class
, "FOO", MONO_WRAPPER_NONE
);
4223 m
= mono_mb_create_method (mb
, sig
, 16);
4224 MonoMethod
*invoke
= mono_marshal_get_runtime_invoke (m
, FALSE
);
4230 get_runtime_invoke (MonoAotCompile
*acfg
, MonoMethod
*method
, gboolean virtual_
)
4232 return mono_marshal_get_runtime_invoke (method
, virtual_
);
4236 can_marshal_struct (MonoClass
*klass
)
4238 MonoClassField
*field
;
4239 gboolean can_marshal
= TRUE
;
4240 gpointer iter
= NULL
;
4241 MonoMarshalType
*info
;
4244 if (mono_class_is_auto_layout (klass
))
4247 info
= mono_marshal_load_type_info (klass
);
4249 /* Only allow a few field types to avoid asserts in the marshalling code */
4250 while ((field
= mono_class_get_fields_internal (klass
, &iter
))) {
4251 if ((field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
4254 switch (field
->type
->type
) {
4259 case MONO_TYPE_BOOLEAN
:
4262 case MONO_TYPE_CHAR
:
4270 case MONO_TYPE_STRING
:
4272 case MONO_TYPE_VALUETYPE
:
4273 if (!m_class_is_enumtype (mono_class_from_mono_type_internal (field
->type
)) && !can_marshal_struct (mono_class_from_mono_type_internal (field
->type
)))
4274 can_marshal
= FALSE
;
4276 case MONO_TYPE_SZARRAY
: {
4277 gboolean has_mspec
= FALSE
;
4280 for (i
= 0; i
< info
->num_fields
; ++i
) {
4281 if (info
->fields
[i
].field
== field
&& info
->fields
[i
].mspec
)
4286 can_marshal
= FALSE
;
4290 can_marshal
= FALSE
;
4296 /* Its hard to compute whenever these can be marshalled or not */
4297 if (!strcmp (m_class_get_name_space (klass
), "System.Net.NetworkInformation.MacOsStructs") && strcmp (m_class_get_name (klass
), "sockaddr_dl"))
4304 create_gsharedvt_inst (MonoAotCompile
*acfg
, MonoMethod
*method
, MonoGenericContext
*ctx
)
4306 /* Create a vtype instantiation */
4307 MonoGenericContext shared_context
;
4309 MonoGenericInst
*inst
;
4310 MonoGenericContainer
*container
;
4311 MonoClass
**constraints
;
4314 memset (ctx
, 0, sizeof (MonoGenericContext
));
4316 if (mono_class_is_gtd (method
->klass
)) {
4317 shared_context
= mono_class_get_generic_container (method
->klass
)->context
;
4318 inst
= shared_context
.class_inst
;
4320 args
= g_new0 (MonoType
*, inst
->type_argc
);
4321 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4322 args
[i
] = mono_get_int_type ();
4324 ctx
->class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, args
);
4326 if (method
->is_generic
) {
4327 container
= mono_method_get_generic_container (method
);
4328 g_assert (!container
->is_anonymous
&& container
->is_method
);
4329 shared_context
= container
->context
;
4330 inst
= shared_context
.method_inst
;
4332 args
= g_new0 (MonoType
*, inst
->type_argc
);
4333 for (i
= 0; i
< container
->type_argc
; ++i
) {
4334 MonoGenericParamInfo
*info
= mono_generic_param_info (&container
->type_params
[i
]);
4335 gboolean ref_only
= FALSE
;
4337 if (info
&& info
->constraints
) {
4338 constraints
= info
->constraints
;
4340 while (*constraints
) {
4341 MonoClass
*cklass
= *constraints
;
4342 if (!(cklass
== mono_defaults
.object_class
|| (m_class_get_image (cklass
) == mono_defaults
.corlib
&& !strcmp (m_class_get_name (cklass
), "ValueType"))))
4343 /* Inflaring the method with our vtype would not be valid */
4350 args
[i
] = mono_get_object_type ();
4352 args
[i
] = mono_get_int_type ();
4354 ctx
->method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, args
);
4359 add_gc_wrappers (MonoAotCompile
*acfg
)
4362 /* Managed Allocators */
4363 int nallocators
= mono_gc_get_managed_allocator_types ();
4364 for (int i
= 0; i
< nallocators
; ++i
) {
4365 if ((m
= mono_gc_get_managed_allocator_by_type (i
, MANAGED_ALLOCATOR_REGULAR
)))
4366 add_method (acfg
, m
);
4367 if ((m
= mono_gc_get_managed_allocator_by_type (i
, MANAGED_ALLOCATOR_SLOW_PATH
)))
4368 add_method (acfg
, m
);
4369 if ((m
= mono_gc_get_managed_allocator_by_type (i
, MANAGED_ALLOCATOR_PROFILER
)))
4370 add_method (acfg
, m
);
4373 /* write barriers */
4374 if (mono_gc_is_moving ()) {
4375 add_method (acfg
, mono_gc_get_specific_write_barrier (FALSE
));
4376 add_method (acfg
, mono_gc_get_specific_write_barrier (TRUE
));
4381 contains_disable_reflection_attribute (MonoCustomAttrInfo
*cattr
)
4383 for (int i
= 0; i
< cattr
->num_attrs
; ++i
) {
4384 MonoCustomAttrEntry
*attr
= &cattr
->attrs
[i
];
4389 if (strcmp (m_class_get_name_space (attr
->ctor
->klass
), "System.Runtime.CompilerServices"))
4392 if (strcmp (m_class_get_name (attr
->ctor
->klass
), "DisablePrivateReflectionAttribute"))
4400 mono_aot_can_specialize (MonoMethod
*method
)
4405 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4408 // If it's not private, we can't specialize
4409 if ((method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) != METHOD_ATTRIBUTE_PRIVATE
)
4412 // If it has the attribute disabling the specialization, we can't specialize
4414 // Set by linker, indicates that the method can be found through reflection
4415 // and that call-site specialization shouldn't be done.
4417 // Important that this attribute is used for *nothing else*
4419 // If future authors make use of it (to disable more optimizations),
4420 // change this place to use a new attribute.
4421 ERROR_DECL (cattr_error
);
4422 MonoCustomAttrInfo
*cattr
= mono_custom_attrs_from_class_checked (method
->klass
, cattr_error
);
4424 if (!is_ok (cattr_error
)) {
4425 mono_error_cleanup (cattr_error
);
4427 } else if (cattr
&& contains_disable_reflection_attribute (cattr
)) {
4431 cattr
= mono_custom_attrs_from_method_checked (method
, cattr_error
);
4433 if (!is_ok (cattr_error
)) {
4434 mono_error_cleanup (cattr_error
);
4436 } else if (cattr
&& contains_disable_reflection_attribute (cattr
)) {
4444 mono_custom_attrs_free (cattr
);
4449 mono_custom_attrs_free (cattr
);
4454 always_aot (MonoMethod
*method
)
4457 * Calls to these methods do not go through the normal call processing code so
4458 * calling code cannot enter the interpreter. So always aot them even in profile guided aot mode.
4460 if (method
->klass
== mono_get_string_class () && (strstr (method
->name
, "memcpy") || strstr (method
->name
, "bzero")))
4462 if (method
->string_ctor
)
4468 mono_aot_can_enter_interp (MonoMethod
*method
)
4470 MonoAotCompile
*acfg
= current_acfg
;
4473 if (always_aot (method
))
4475 if (acfg
->aot_opts
.profile_only
&& !g_hash_table_lookup (acfg
->profile_methods
, method
))
4481 add_wrappers (MonoAotCompile
*acfg
)
4483 MonoMethod
*method
, *m
;
4485 MonoMethodSignature
*sig
, *csig
;
4489 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
4490 * so there is only one wrapper of a given type, or inlining their contents into their
4493 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4496 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4497 gboolean skip
= FALSE
;
4499 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4500 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
4502 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
4503 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4504 (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
))
4507 /* Skip methods which can not be handled by get_runtime_invoke () */
4508 sig
= mono_method_signature_internal (method
);
4511 if ((sig
->ret
->type
== MONO_TYPE_PTR
) ||
4512 (sig
->ret
->type
== MONO_TYPE_TYPEDBYREF
))
4514 if (mono_class_is_open_constructed_type (sig
->ret
))
4517 for (j
= 0; j
< sig
->param_count
; j
++) {
4518 if (sig
->params
[j
]->type
== MONO_TYPE_TYPEDBYREF
)
4520 if (mono_class_is_open_constructed_type (sig
->params
[j
]))
4524 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
4525 if (!mono_class_is_contextbound (method
->klass
)) {
4526 MonoDynCallInfo
*info
= mono_arch_dyn_call_prepare (sig
);
4527 gboolean has_nullable
= FALSE
;
4529 for (j
= 0; j
< sig
->param_count
; j
++) {
4530 if (sig
->params
[j
]->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type_internal (sig
->params
[j
])))
4531 has_nullable
= TRUE
;
4534 if (info
&& !has_nullable
&& !acfg
->aot_opts
.llvm_only
) {
4535 /* Supported by the dynamic runtime-invoke wrapper */
4539 mono_arch_dyn_call_free (info
);
4543 if (acfg
->aot_opts
.llvm_only
)
4544 /* Supported by the gsharedvt based runtime-invoke wrapper */
4548 //printf ("%s\n", mono_method_full_name (method, TRUE));
4549 add_method (acfg
, get_runtime_invoke (acfg
, method
, FALSE
));
4553 if (mono_is_corlib_image (acfg
->image
->assembly
->image
)) {
4554 /* Runtime invoke wrappers */
4556 MonoType
*void_type
= mono_get_void_type ();
4557 MonoType
*string_type
= m_class_get_byval_arg (mono_defaults
.string_class
);
4559 /* void runtime-invoke () [.cctor] */
4560 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4561 csig
->ret
= void_type
;
4562 add_method (acfg
, get_runtime_invoke_sig (csig
));
4564 /* void runtime-invoke () [Finalize] */
4565 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4567 csig
->ret
= void_type
;
4568 add_method (acfg
, get_runtime_invoke_sig (csig
));
4570 /* void runtime-invoke (string) [exception ctor] */
4571 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
4573 csig
->ret
= void_type
;
4574 csig
->params
[0] = string_type
;
4575 add_method (acfg
, get_runtime_invoke_sig (csig
));
4577 /* void runtime-invoke (string, string) [exception ctor] */
4578 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 2);
4580 csig
->ret
= void_type
;
4581 csig
->params
[0] = string_type
;
4582 csig
->params
[1] = string_type
;
4583 add_method (acfg
, get_runtime_invoke_sig (csig
));
4585 /* string runtime-invoke () [Exception.ToString ()] */
4586 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4588 csig
->ret
= string_type
;
4589 add_method (acfg
, get_runtime_invoke_sig (csig
));
4591 /* void runtime-invoke (string, Exception) [exception ctor] */
4592 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 2);
4594 csig
->ret
= void_type
;
4595 csig
->params
[0] = string_type
;
4596 csig
->params
[1] = m_class_get_byval_arg (mono_defaults
.exception_class
);
4597 add_method (acfg
, get_runtime_invoke_sig (csig
));
4599 /* Assembly runtime-invoke (string, Assembly, bool) [DoAssemblyResolve] */
4600 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
4602 csig
->ret
= m_class_get_byval_arg (mono_class_load_from_name (mono_defaults
.corlib
, "System.Reflection", "Assembly"));
4603 csig
->params
[0] = string_type
;
4604 csig
->params
[1] = m_class_get_byval_arg (mono_class_load_from_name (mono_defaults
.corlib
, "System.Reflection", "Assembly"));
4605 csig
->params
[2] = m_class_get_byval_arg (mono_defaults
.boolean_class
);
4606 add_method (acfg
, get_runtime_invoke_sig (csig
));
4608 /* runtime-invoke used by finalizers */
4609 add_method (acfg
, get_runtime_invoke (acfg
, get_method_nofail (mono_defaults
.object_class
, "Finalize", 0, 0), TRUE
));
4611 /* This is used by mono_runtime_capture_context () */
4612 method
= mono_get_context_capture_method ();
4614 add_method (acfg
, get_runtime_invoke (acfg
, method
, FALSE
));
4616 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
4617 if (!acfg
->aot_opts
.llvm_only
)
4618 add_method (acfg
, mono_marshal_get_runtime_invoke_dynamic ());
4621 /* These are used by mono_jit_runtime_invoke () to calls gsharedvt out wrappers */
4622 if (acfg
->aot_opts
.llvm_only
) {
4625 /* Create simplified signatures which match the signature used by the gsharedvt out wrappers */
4626 for (variants
= 0; variants
< 4; ++variants
) {
4627 for (i
= 0; i
< 40; ++i
) {
4628 sig
= mini_get_gsharedvt_out_sig_wrapper_signature ((variants
& 1) > 0, (variants
& 2) > 0, i
);
4629 add_extra_method (acfg
, mono_marshal_get_runtime_invoke_for_sig (sig
));
4637 add_method (acfg
, mono_marshal_get_stelemref ());
4639 add_gc_wrappers (acfg
);
4641 /* Stelemref wrappers */
4643 MonoMethod
**wrappers
;
4646 wrappers
= mono_marshal_get_virtual_stelemref_wrappers (&nwrappers
);
4647 for (i
= 0; i
< nwrappers
; ++i
)
4648 add_method (acfg
, wrappers
[i
]);
4652 /* castclass_with_check wrapper */
4653 add_method (acfg
, mono_marshal_get_castclass_with_cache ());
4654 /* isinst_with_check wrapper */
4655 add_method (acfg
, mono_marshal_get_isinst_with_cache ());
4657 /* JIT icall wrappers */
4658 /* FIXME: locking - this is "safe" as full-AOT threads don't mutate the icall data */
4659 for (int i
= 0; i
< MONO_JIT_ICALL_count
; ++i
)
4660 add_jit_icall_wrapper (acfg
, mono_find_jit_icall_info ((MonoJitICallId
)i
));
4664 * remoting-invoke-with-check wrappers are very frequent, so avoid emitting them,
4665 * we use the original method instead at runtime.
4666 * Since full-aot doesn't support remoting, this is not a problem.
4669 /* remoting-invoke wrappers */
4670 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4672 MonoMethodSignature
*sig
;
4674 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4675 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4676 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4678 sig
= mono_method_signature_internal (method
);
4680 if (sig
->hasthis
&& (method
->klass
->marshalbyref
|| method
->klass
== mono_defaults
.object_class
)) {
4681 m
= mono_marshal_get_remoting_invoke_with_check (method
);
4683 add_method (acfg
, m
);
4688 /* delegate-invoke wrappers */
4689 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
) {
4693 token
= MONO_TOKEN_TYPE_DEF
| (i
+ 1);
4694 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
4697 mono_error_cleanup (error
);
4701 if (!m_class_is_delegate (klass
) || klass
== mono_defaults
.delegate_class
|| klass
== mono_defaults
.multicastdelegate_class
)
4704 if (!mono_class_is_gtd (klass
)) {
4705 method
= mono_get_delegate_invoke_internal (klass
);
4707 m
= mono_marshal_get_delegate_invoke (method
, NULL
);
4709 add_method (acfg
, m
);
4711 method
= try_get_method_nofail (klass
, "BeginInvoke", -1, 0);
4713 add_method (acfg
, mono_marshal_get_delegate_begin_invoke (method
));
4715 method
= try_get_method_nofail (klass
, "EndInvoke", -1, 0);
4717 add_method (acfg
, mono_marshal_get_delegate_end_invoke (method
));
4719 MonoCustomAttrInfo
*cattr
;
4720 cattr
= mono_custom_attrs_from_class_checked (klass
, error
);
4721 if (!is_ok (error
)) {
4722 mono_error_cleanup (error
);
4730 for (j
= 0; j
< cattr
->num_attrs
; ++j
)
4731 if (cattr
->attrs
[j
].ctor
&& (!strcmp (m_class_get_name (cattr
->attrs
[j
].ctor
->klass
), "MonoNativeFunctionWrapperAttribute") || !strcmp (m_class_get_name (cattr
->attrs
[j
].ctor
->klass
), "UnmanagedFunctionPointerAttribute")))
4733 if (j
< cattr
->num_attrs
) {
4735 MonoMethod
*wrapper
;
4736 MonoMethod
*del_invoke
;
4738 /* Add wrappers needed by mono_ftnptr_to_delegate () */
4739 invoke
= mono_get_delegate_invoke_internal (klass
);
4740 wrapper
= mono_marshal_get_native_func_wrapper_aot (klass
);
4741 del_invoke
= mono_marshal_get_delegate_invoke_internal (invoke
, FALSE
, TRUE
, wrapper
);
4742 add_method (acfg
, wrapper
);
4743 add_method (acfg
, del_invoke
);
4745 mono_custom_attrs_free (cattr
);
4747 } else if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && mono_class_is_gtd (klass
)) {
4749 MonoGenericContext ctx
;
4750 MonoMethod
*inst
, *gshared
;
4753 * Emit gsharedvt versions of the generic delegate-invoke wrappers
4756 method
= mono_get_delegate_invoke_internal (klass
);
4757 create_gsharedvt_inst (acfg
, method
, &ctx
);
4759 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, error
);
4760 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4762 m
= mono_marshal_get_delegate_invoke (inst
, NULL
);
4763 g_assert (m
->is_inflated
);
4765 gshared
= mini_get_shared_method_full (m
, SHARE_MODE_GSHAREDVT
, error
);
4766 mono_error_assert_ok (error
);
4768 add_extra_method (acfg
, gshared
);
4771 method
= mono_get_delegate_begin_invoke_internal (klass
);
4773 create_gsharedvt_inst (acfg
, method
, &ctx
);
4775 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, error
);
4776 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4778 m
= mono_marshal_get_delegate_begin_invoke (inst
);
4779 g_assert (m
->is_inflated
);
4781 gshared
= mini_get_shared_method_full (m
, SHARE_MODE_GSHAREDVT
, error
);
4782 mono_error_assert_ok (error
);
4784 add_extra_method (acfg
, gshared
);
4788 method
= mono_get_delegate_end_invoke_internal (klass
);
4790 create_gsharedvt_inst (acfg
, method
, &ctx
);
4792 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, error
);
4793 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4795 m
= mono_marshal_get_delegate_end_invoke (inst
);
4796 g_assert (m
->is_inflated
);
4798 gshared
= mini_get_shared_method_full (m
, SHARE_MODE_GSHAREDVT
, error
);
4799 mono_error_assert_ok (error
);
4801 add_extra_method (acfg
, gshared
);
4806 /* array access wrappers */
4807 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPESPEC
].rows
; ++i
) {
4811 token
= MONO_TOKEN_TYPE_SPEC
| (i
+ 1);
4812 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
4815 mono_error_cleanup (error
);
4819 if (m_class_get_rank (klass
) && MONO_TYPE_IS_PRIMITIVE (m_class_get_byval_arg (m_class_get_element_class (klass
)))) {
4820 MonoMethod
*m
, *wrapper
;
4822 /* Add runtime-invoke wrappers too */
4824 m
= get_method_nofail (klass
, "Get", -1, 0);
4826 wrapper
= mono_marshal_get_array_accessor_wrapper (m
);
4827 add_extra_method (acfg
, wrapper
);
4828 if (!acfg
->aot_opts
.llvm_only
)
4829 add_extra_method (acfg
, get_runtime_invoke (acfg
, wrapper
, FALSE
));
4831 m
= get_method_nofail (klass
, "Set", -1, 0);
4833 wrapper
= mono_marshal_get_array_accessor_wrapper (m
);
4834 add_extra_method (acfg
, wrapper
);
4835 if (!acfg
->aot_opts
.llvm_only
)
4836 add_extra_method (acfg
, get_runtime_invoke (acfg
, wrapper
, FALSE
));
4840 /* Synchronized wrappers */
4841 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4843 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4844 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4845 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
4847 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
) {
4848 if (method
->is_generic
) {
4850 } else if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && mono_class_is_gtd (method
->klass
)) {
4852 MonoGenericContext ctx
;
4853 MonoMethod
*inst
, *gshared
, *m
;
4856 * Create a generic wrapper for a generic instance, and AOT that.
4858 create_gsharedvt_inst (acfg
, method
, &ctx
);
4859 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, error
);
4860 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4861 m
= mono_marshal_get_synchronized_wrapper (inst
);
4862 g_assert (m
->is_inflated
);
4863 gshared
= mini_get_shared_method_full (m
, SHARE_MODE_GSHAREDVT
, error
);
4864 mono_error_assert_ok (error
);
4866 add_method (acfg
, gshared
);
4868 add_method (acfg
, mono_marshal_get_synchronized_wrapper (method
));
4873 /* pinvoke wrappers */
4874 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4877 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4879 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4880 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
4882 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
4883 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)) {
4884 add_method (acfg
, mono_marshal_get_native_wrapper (method
, TRUE
, TRUE
));
4887 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
4888 if (acfg
->aot_opts
.llvm_only
) {
4889 /* The wrappers have a different signature (hasthis is not set) so need to add this too */
4890 add_gsharedvt_wrappers (acfg
, mono_method_signature_internal (method
), FALSE
, TRUE
, FALSE
);
4895 /* native-to-managed wrappers */
4896 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4899 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4900 MonoCustomAttrInfo
*cattr
;
4903 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4904 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
4907 * Only generate native-to-managed wrappers for methods which have an
4908 * attribute named MonoPInvokeCallbackAttribute. We search for the attribute by
4909 * name to avoid defining a new assembly to contain it.
4911 cattr
= mono_custom_attrs_from_method_checked (method
, error
);
4912 if (!is_ok (error
)) {
4913 char *name
= mono_method_get_full_name (method
);
4914 report_loader_error (acfg
, error
, TRUE
, "Failed to load custom attributes from method %s due to %s\n", name
, mono_error_get_message (error
));
4919 for (j
= 0; j
< cattr
->num_attrs
; ++j
)
4920 if (cattr
->attrs
[j
].ctor
&& !strcmp (m_class_get_name (cattr
->attrs
[j
].ctor
->klass
), "MonoPInvokeCallbackAttribute"))
4922 if (j
< cattr
->num_attrs
) {
4923 MonoCustomAttrEntry
*e
= &cattr
->attrs
[j
];
4924 MonoMethodSignature
*sig
= mono_method_signature_internal (e
->ctor
);
4925 const char *p
= (const char*)e
->data
;
4927 int slen
, num_named
, named_type
;
4931 char *export_name
= NULL
;
4932 MonoMethod
*wrapper
;
4934 /* this cannot be enforced by the C# compiler so we must give the user some warning before aborting */
4935 if (!(method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
4936 g_warning ("AOT restriction: Method '%s' must be static since it is decorated with [MonoPInvokeCallback]. See https://docs.microsoft.com/xamarin/ios/internals/limitations#reverse-callbacks",
4937 mono_method_full_name (method
, TRUE
));
4941 g_assert (sig
->param_count
== 1);
4942 g_assert (sig
->params
[0]->type
== MONO_TYPE_CLASS
&& !strcmp (m_class_get_name (mono_class_from_mono_type_internal (sig
->params
[0])), "Type"));
4945 * Decode the cattr manually since we can't create objects
4946 * during aot compilation.
4952 /* From load_cattr_value () in reflection.c */
4953 slen
= mono_metadata_decode_value (p
, &p
);
4954 n
= (char *)g_memdup (p
, slen
+ 1);
4956 t
= mono_reflection_type_from_name_checked (n
, mono_domain_ambient_alc (mono_domain_get ()), acfg
->image
, error
);
4958 mono_error_assert_ok (error
);
4961 klass
= mono_class_from_mono_type_internal (t
);
4962 g_assert (m_class_get_parent (klass
) == mono_defaults
.multicastdelegate_class
);
4966 num_named
= read16 (p
);
4969 g_assert (num_named
< 2);
4970 if (num_named
== 1) {
4974 /* parse ExportSymbol attribute */
4976 named_type
= *named
;
4978 /* data_type = *named; */
4981 name_len
= mono_metadata_decode_blob_size (named
, &named
);
4982 name
= (char *)g_malloc (name_len
+ 1);
4983 memcpy (name
, named
, name_len
);
4984 name
[name_len
] = 0;
4987 g_assert (named_type
== 0x54);
4988 g_assert (!strcmp (name
, "ExportSymbol"));
4990 /* load_cattr_value (), string case */
4991 MONO_DISABLE_WARNING (4310) // cast truncates constant value
4992 g_assert (*named
!= (char)0xFF);
4993 MONO_RESTORE_WARNING
4994 slen
= mono_metadata_decode_value (named
, &named
);
4995 export_name
= (char *)g_malloc (slen
+ 1);
4996 memcpy (export_name
, named
, slen
);
4997 export_name
[slen
] = 0;
5001 wrapper
= mono_marshal_get_managed_wrapper (method
, klass
, 0, error
);
5002 mono_error_assert_ok (error
);
5004 add_method (acfg
, wrapper
);
5006 g_hash_table_insert (acfg
->export_names
, wrapper
, export_name
);
5011 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
5012 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)) {
5013 add_method (acfg
, mono_marshal_get_native_wrapper (method
, TRUE
, TRUE
));
5017 /* StructureToPtr/PtrToStructure wrappers */
5018 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
) {
5022 token
= MONO_TOKEN_TYPE_DEF
| (i
+ 1);
5023 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
5026 mono_error_cleanup (error
);
5030 if (m_class_is_valuetype (klass
) && !mono_class_is_gtd (klass
) && can_marshal_struct (klass
) &&
5031 !(m_class_get_nested_in (klass
) && strstr (m_class_get_name (m_class_get_nested_in (klass
)), "<PrivateImplementationDetails>") == m_class_get_name (m_class_get_nested_in (klass
)))) {
5032 add_method (acfg
, mono_marshal_get_struct_to_ptr (klass
));
5033 add_method (acfg
, mono_marshal_get_ptr_to_struct (klass
));
5039 has_type_vars (MonoClass
*klass
)
5041 if ((m_class_get_byval_arg (klass
)->type
== MONO_TYPE_VAR
) || (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_MVAR
))
5043 if (m_class_get_rank (klass
))
5044 return has_type_vars (m_class_get_element_class (klass
));
5045 if (mono_class_is_ginst (klass
)) {
5046 MonoGenericContext
*context
= &mono_class_get_generic_class (klass
)->context
;
5047 if (context
->class_inst
) {
5050 for (i
= 0; i
< context
->class_inst
->type_argc
; ++i
)
5051 if (has_type_vars (mono_class_from_mono_type_internal (context
->class_inst
->type_argv
[i
])))
5055 if (mono_class_is_gtd (klass
))
5061 is_vt_inst (MonoGenericInst
*inst
)
5065 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5066 MonoType
*t
= inst
->type_argv
[i
];
5067 if (MONO_TYPE_ISSTRUCT (t
) || t
->type
== MONO_TYPE_VALUETYPE
)
5074 method_has_type_vars (MonoMethod
*method
)
5076 if (has_type_vars (method
->klass
))
5079 if (method
->is_inflated
) {
5080 MonoGenericContext
*context
= mono_method_get_context (method
);
5081 if (context
->method_inst
) {
5084 for (i
= 0; i
< context
->method_inst
->type_argc
; ++i
)
5085 if (has_type_vars (mono_class_from_mono_type_internal (context
->method_inst
->type_argv
[i
])))
5093 gboolean
mono_aot_mode_is_full (MonoAotOptions
*opts
)
5095 return opts
->mode
== MONO_AOT_MODE_FULL
;
5099 gboolean
mono_aot_mode_is_interp (MonoAotOptions
*opts
)
5101 return opts
->interp
;
5105 gboolean
mono_aot_mode_is_hybrid (MonoAotOptions
*opts
)
5107 return opts
->mode
== MONO_AOT_MODE_HYBRID
;
5110 static void add_generic_class_with_depth (MonoAotCompile
*acfg
, MonoClass
*klass
, int depth
, const char *ref
);
5113 add_generic_class (MonoAotCompile
*acfg
, MonoClass
*klass
, gboolean force
, const char *ref
)
5115 /* This might lead to a huge code blowup so only do it if neccesary */
5116 if (!mono_aot_mode_is_full (&acfg
->aot_opts
) && !mono_aot_mode_is_hybrid (&acfg
->aot_opts
) && !force
)
5119 add_generic_class_with_depth (acfg
, klass
, 0, ref
);
5123 check_type_depth (MonoType
*t
, int depth
)
5131 case MONO_TYPE_GENERICINST
: {
5132 MonoGenericClass
*gklass
= t
->data
.generic_class
;
5133 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
5136 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
5137 if (check_type_depth (ginst
->type_argv
[i
], depth
+ 1))
5151 add_types_from_method_header (MonoAotCompile
*acfg
, MonoMethod
*method
);
5154 * add_generic_class:
5156 * Add all methods of a generic class.
5159 add_generic_class_with_depth (MonoAotCompile
*acfg
, MonoClass
*klass
, int depth
, const char *ref
)
5162 MonoClassField
*field
;
5164 gboolean use_gsharedvt
= FALSE
;
5166 if (!acfg
->ginst_hash
)
5167 acfg
->ginst_hash
= g_hash_table_new (NULL
, NULL
);
5169 mono_class_init_internal (klass
);
5171 if (mono_class_is_ginst (klass
) && mono_class_get_generic_class (klass
)->context
.class_inst
->is_open
)
5174 if (has_type_vars (klass
))
5177 if (!mono_class_is_ginst (klass
) && !m_class_get_rank (klass
))
5180 if (mono_class_has_failure (klass
))
5183 if (!acfg
->ginst_hash
)
5184 acfg
->ginst_hash
= g_hash_table_new (NULL
, NULL
);
5186 if (g_hash_table_lookup (acfg
->ginst_hash
, klass
))
5189 if (check_type_depth (m_class_get_byval_arg (klass
), 0))
5192 if (acfg
->aot_opts
.log_generics
) {
5193 char *s
= mono_type_full_name (m_class_get_byval_arg (klass
));
5194 aot_printf (acfg
, "%*sAdding generic instance %s [%s].\n", depth
, "", s
, ref
);
5198 g_hash_table_insert (acfg
->ginst_hash
, klass
, klass
);
5201 * Use gsharedvt for generic collections with vtype arguments to avoid code blowup.
5202 * Enable this only for some classes since gsharedvt might not support all methods.
5204 if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && m_class_get_image (klass
) == mono_defaults
.corlib
&& mono_class_is_ginst (klass
) && mono_class_get_generic_class (klass
)->context
.class_inst
&& is_vt_inst (mono_class_get_generic_class (klass
)->context
.class_inst
) &&
5205 (!strcmp (m_class_get_name (klass
), "Dictionary`2") || !strcmp (m_class_get_name (klass
), "List`1") || !strcmp (m_class_get_name (klass
), "ReadOnlyCollection`1")))
5206 use_gsharedvt
= TRUE
;
5209 while ((method
= mono_class_get_methods (klass
, &iter
))) {
5210 if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && method
->is_inflated
&& mono_method_get_context (method
)->method_inst
) {
5212 * This is partial sharing, and we can't handle it yet
5217 if (mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, use_gsharedvt
)) {
5219 add_types_from_method_header (acfg
, method
);
5223 if (method
->is_generic
)
5228 * FIXME: Instances which are referenced by these methods are not added,
5229 * for example Array.Resize<int> for List<int>.Add ().
5231 add_extra_method_with_depth (acfg
, method
, depth
+ 1);
5235 while ((field
= mono_class_get_fields_internal (klass
, &iter
))) {
5236 if (field
->type
->type
== MONO_TYPE_GENERICINST
)
5237 add_generic_class_with_depth (acfg
, mono_class_from_mono_type_internal (field
->type
), depth
+ 1, "field");
5240 if (m_class_is_delegate (klass
)) {
5241 method
= mono_get_delegate_invoke_internal (klass
);
5243 method
= mono_marshal_get_delegate_invoke (method
, NULL
);
5245 if (acfg
->aot_opts
.log_generics
)
5246 aot_printf (acfg
, "%*sAdding method %s.\n", depth
, "", mono_method_get_full_name (method
));
5248 add_method (acfg
, method
);
5251 /* Add superclasses */
5252 if (m_class_get_parent (klass
))
5253 add_generic_class_with_depth (acfg
, m_class_get_parent (klass
), depth
, "parent");
5255 const char *klass_name
= m_class_get_name (klass
);
5256 const char *klass_name_space
= m_class_get_name_space (klass
);
5257 const gboolean in_corlib
= m_class_get_image (klass
) == mono_defaults
.corlib
;
5259 * For ICollection<T>, add instances of the helper methods
5260 * in Array, since a T[] could be cast to ICollection<T>.
5262 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") &&
5263 (!strcmp(klass_name
, "ICollection`1") || !strcmp (klass_name
, "IEnumerable`1") || !strcmp (klass_name
, "IList`1") || !strcmp (klass_name
, "IEnumerator`1") || !strcmp (klass_name
, "IReadOnlyList`1"))) {
5264 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5265 MonoClass
*array_class
= mono_class_create_bounded_array (tclass
, 1, FALSE
);
5269 if (!strcmp (klass_name
, "IEnumerator`1"))
5270 name_prefix
= g_strdup_printf ("%s.%s", klass_name_space
, "IEnumerable`1");
5272 name_prefix
= g_strdup_printf ("%s.%s", klass_name_space
, klass_name
);
5274 #ifndef ENABLE_NETCORE
5275 /* Add the T[]/InternalEnumerator class */
5276 if (!strcmp (klass_name
, "IEnumerable`1") || !strcmp (klass_name
, "IEnumerator`1")) {
5281 while ((nclass
= mono_class_get_nested_types (m_class_get_parent (array_class
), &iter
))) {
5282 if (!strcmp (m_class_get_name (nclass
), "InternalEnumerator`1"))
5286 nclass
= mono_class_inflate_generic_class_checked (nclass
, mono_generic_class_get_context (mono_class_get_generic_class (klass
)), error
);
5287 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5288 add_generic_class (acfg
, nclass
, FALSE
, "ICollection<T>");
5293 while ((method
= mono_class_get_methods (array_class
, &iter
))) {
5294 if (!strncmp (method
->name
, name_prefix
, strlen (name_prefix
))) {
5295 MonoMethod
*m
= mono_aot_get_array_helper_from_wrapper (method
);
5297 if (m
->is_inflated
&& !mono_method_is_generic_sharable_full (m
, FALSE
, FALSE
, FALSE
))
5298 add_extra_method_with_depth (acfg
, m
, depth
);
5302 g_free (name_prefix
);
5305 /* Add an instance of GenericComparer<T> which is created dynamically by Comparer<T> */
5306 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") && !strcmp (klass_name
, "Comparer`1")) {
5308 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5309 MonoClass
*icomparable
, *gcomparer
, *icomparable_inst
;
5310 MonoGenericContext ctx
;
5311 MonoType
*args
[16];
5313 memset (&ctx
, 0, sizeof (ctx
));
5315 icomparable
= mono_class_load_from_name (mono_defaults
.corlib
, "System", "IComparable`1");
5317 args
[0] = m_class_get_byval_arg (tclass
);
5318 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5320 icomparable_inst
= mono_class_inflate_generic_class_checked (icomparable
, &ctx
, error
);
5321 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5323 if (mono_class_is_assignable_from_internal (icomparable_inst
, tclass
)) {
5324 MonoClass
*gcomparer_inst
;
5325 gcomparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "GenericComparer`1");
5326 gcomparer_inst
= mono_class_inflate_generic_class_checked (gcomparer
, &ctx
, error
);
5327 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5329 add_generic_class (acfg
, gcomparer_inst
, FALSE
, "Comparer<T>");
5333 /* Add an instance of GenericEqualityComparer<T> which is created dynamically by EqualityComparer<T> */
5334 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") && !strcmp (klass_name
, "EqualityComparer`1")) {
5336 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5337 MonoClass
*iface
, *gcomparer
, *iface_inst
;
5338 MonoGenericContext ctx
;
5339 MonoType
*args
[16];
5341 memset (&ctx
, 0, sizeof (ctx
));
5343 iface
= mono_class_load_from_name (mono_defaults
.corlib
, "System", "IEquatable`1");
5345 args
[0] = m_class_get_byval_arg (tclass
);
5346 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5348 iface_inst
= mono_class_inflate_generic_class_checked (iface
, &ctx
, error
);
5349 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5351 if (mono_class_is_assignable_from_internal (iface_inst
, tclass
)) {
5352 MonoClass
*gcomparer_inst
;
5355 gcomparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "GenericEqualityComparer`1");
5356 gcomparer_inst
= mono_class_inflate_generic_class_checked (gcomparer
, &ctx
, error
);
5357 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5358 add_generic_class (acfg
, gcomparer_inst
, FALSE
, "EqualityComparer<T>");
5362 /* Add an instance of EnumComparer<T> which is created dynamically by EqualityComparer<T> for enums */
5363 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") && !strcmp (klass_name
, "EqualityComparer`1")) {
5364 MonoClass
*enum_comparer
;
5365 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5366 MonoGenericContext ctx
;
5367 MonoType
*args
[16];
5369 if (m_class_is_enumtype (tclass
)) {
5370 MonoClass
*enum_comparer_inst
;
5373 memset (&ctx
, 0, sizeof (ctx
));
5374 args
[0] = m_class_get_byval_arg (tclass
);
5375 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5377 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "EnumEqualityComparer`1");
5378 enum_comparer_inst
= mono_class_inflate_generic_class_checked (enum_comparer
, &ctx
, error
);
5379 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5380 add_generic_class (acfg
, enum_comparer_inst
, FALSE
, "EqualityComparer<T>");
5384 /* Add an instance of ObjectComparer<T> which is created dynamically by Comparer<T> for enums */
5385 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") && !strcmp (klass_name
, "Comparer`1")) {
5386 MonoClass
*comparer
;
5387 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5388 MonoGenericContext ctx
;
5389 MonoType
*args
[16];
5391 if (m_class_is_enumtype (tclass
)) {
5392 MonoClass
*comparer_inst
;
5395 memset (&ctx
, 0, sizeof (ctx
));
5396 args
[0] = m_class_get_byval_arg (tclass
);
5397 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5399 comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "ObjectComparer`1");
5400 comparer_inst
= mono_class_inflate_generic_class_checked (comparer
, &ctx
, error
);
5401 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5402 add_generic_class (acfg
, comparer_inst
, FALSE
, "Comparer<T>");
5408 add_instances_of (MonoAotCompile
*acfg
, MonoClass
*klass
, MonoType
**insts
, int ninsts
, gboolean force
)
5411 MonoGenericContext ctx
;
5412 MonoType
*args
[16];
5414 if (acfg
->aot_opts
.no_instances
)
5417 memset (&ctx
, 0, sizeof (ctx
));
5419 for (i
= 0; i
< ninsts
; ++i
) {
5421 MonoClass
*generic_inst
;
5422 args
[0] = insts
[i
];
5423 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5424 generic_inst
= mono_class_inflate_generic_class_checked (klass
, &ctx
, error
);
5425 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5426 add_generic_class (acfg
, generic_inst
, force
, "");
5431 add_types_from_method_header (MonoAotCompile
*acfg
, MonoMethod
*method
)
5434 MonoMethodHeader
*header
;
5435 MonoMethodSignature
*sig
;
5438 depth
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_depth
, method
));
5440 sig
= mono_method_signature_internal (method
);
5443 for (j
= 0; j
< sig
->param_count
; ++j
)
5444 if (sig
->params
[j
]->type
== MONO_TYPE_GENERICINST
)
5445 add_generic_class_with_depth (acfg
, mono_class_from_mono_type_internal (sig
->params
[j
]), depth
+ 1, "arg");
5448 header
= mono_method_get_header_checked (method
, error
);
5451 for (j
= 0; j
< header
->num_locals
; ++j
)
5452 if (header
->locals
[j
]->type
== MONO_TYPE_GENERICINST
)
5453 add_generic_class_with_depth (acfg
, mono_class_from_mono_type_internal (header
->locals
[j
]), depth
+ 1, "local");
5454 mono_metadata_free_mh (header
);
5456 mono_error_cleanup (error
); /* FIXME report the error */
5462 * add_generic_instances:
5464 * Add instances referenced by the METHODSPEC/TYPESPEC table.
5467 add_generic_instances (MonoAotCompile
*acfg
)
5472 MonoGenericContext
*context
;
5474 if (acfg
->aot_opts
.no_instances
)
5477 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHODSPEC
].rows
; ++i
) {
5479 token
= MONO_TOKEN_METHOD_SPEC
| (i
+ 1);
5480 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
5483 aot_printerrf (acfg
, "Failed to load methodspec 0x%x due to %s.\n", token
, mono_error_get_message (error
));
5484 aot_printerrf (acfg
, "Run with MONO_LOG_LEVEL=debug for more information.\n");
5485 mono_error_cleanup (error
);
5489 if (m_class_get_image (method
->klass
) != acfg
->image
)
5492 context
= mono_method_get_context (method
);
5494 if (context
&& ((context
->class_inst
&& context
->class_inst
->is_open
)))
5498 * For open methods, create an instantiation which can be passed to the JIT.
5499 * FIXME: Handle class_inst as well.
5501 if (context
&& context
->method_inst
&& context
->method_inst
->is_open
) {
5503 MonoGenericContext shared_context
;
5504 MonoGenericInst
*inst
;
5505 MonoType
**type_argv
;
5507 MonoMethod
*declaring_method
;
5508 gboolean supported
= TRUE
;
5510 /* Check that the context doesn't contain open constructed types */
5511 if (context
->class_inst
) {
5512 inst
= context
->class_inst
;
5513 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5514 if (MONO_TYPE_IS_REFERENCE (inst
->type_argv
[i
]) || inst
->type_argv
[i
]->type
== MONO_TYPE_VAR
|| inst
->type_argv
[i
]->type
== MONO_TYPE_MVAR
)
5516 if (mono_class_is_open_constructed_type (inst
->type_argv
[i
]))
5520 if (context
->method_inst
) {
5521 inst
= context
->method_inst
;
5522 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5523 if (MONO_TYPE_IS_REFERENCE (inst
->type_argv
[i
]) || inst
->type_argv
[i
]->type
== MONO_TYPE_VAR
|| inst
->type_argv
[i
]->type
== MONO_TYPE_MVAR
)
5525 if (mono_class_is_open_constructed_type (inst
->type_argv
[i
]))
5533 memset (&shared_context
, 0, sizeof (MonoGenericContext
));
5535 inst
= context
->class_inst
;
5537 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
5538 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5539 if (MONO_TYPE_IS_REFERENCE (inst
->type_argv
[i
]) || inst
->type_argv
[i
]->type
== MONO_TYPE_VAR
|| inst
->type_argv
[i
]->type
== MONO_TYPE_MVAR
)
5540 type_argv
[i
] = mono_get_object_type ();
5542 type_argv
[i
] = inst
->type_argv
[i
];
5545 shared_context
.class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
5549 inst
= context
->method_inst
;
5551 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
5552 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5553 if (MONO_TYPE_IS_REFERENCE (inst
->type_argv
[i
]) || inst
->type_argv
[i
]->type
== MONO_TYPE_VAR
|| inst
->type_argv
[i
]->type
== MONO_TYPE_MVAR
)
5554 type_argv
[i
] = mono_get_object_type ();
5556 type_argv
[i
] = inst
->type_argv
[i
];
5559 shared_context
.method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
5563 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
))
5564 declaring_method
= method
;
5566 declaring_method
= mono_method_get_declaring_generic_method (method
);
5568 method
= mono_class_inflate_generic_method_checked (declaring_method
, &shared_context
, error
);
5569 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
5573 * If the method is fully sharable, it was already added in place of its
5574 * generic definition.
5576 if (mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, FALSE
))
5580 * FIXME: Partially shared methods are not shared here, so we end up with
5581 * many identical methods.
5583 add_extra_method (acfg
, method
);
5586 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPESPEC
].rows
; ++i
) {
5590 token
= MONO_TOKEN_TYPE_SPEC
| (i
+ 1);
5592 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
5593 if (!klass
|| m_class_get_rank (klass
)) {
5594 mono_error_cleanup (error
);
5598 add_generic_class (acfg
, klass
, FALSE
, "typespec");
5601 /* Add types of args/locals */
5602 for (i
= 0; i
< acfg
->methods
->len
; ++i
) {
5603 method
= (MonoMethod
*)g_ptr_array_index (acfg
->methods
, i
);
5604 add_types_from_method_header (acfg
, method
);
5607 if (acfg
->image
== mono_defaults
.corlib
) {
5609 MonoType
*insts
[256];
5612 MonoType
*byte_type
= m_class_get_byval_arg (mono_defaults
.byte_class
);
5613 MonoType
*sbyte_type
= m_class_get_byval_arg (mono_defaults
.sbyte_class
);
5614 MonoType
*int16_type
= m_class_get_byval_arg (mono_defaults
.int16_class
);
5615 MonoType
*uint16_type
= m_class_get_byval_arg (mono_defaults
.uint16_class
);
5616 MonoType
*int32_type
= mono_get_int32_type ();
5617 MonoType
*uint32_type
= m_class_get_byval_arg (mono_defaults
.uint32_class
);
5618 MonoType
*int64_type
= m_class_get_byval_arg (mono_defaults
.int64_class
);
5619 MonoType
*uint64_type
= m_class_get_byval_arg (mono_defaults
.uint64_class
);
5620 MonoType
*object_type
= mono_get_object_type ();
5622 insts
[ninsts
++] = byte_type
;
5623 insts
[ninsts
++] = sbyte_type
;
5624 insts
[ninsts
++] = int16_type
;
5625 insts
[ninsts
++] = uint16_type
;
5626 insts
[ninsts
++] = int32_type
;
5627 insts
[ninsts
++] = uint32_type
;
5628 insts
[ninsts
++] = int64_type
;
5629 insts
[ninsts
++] = uint64_type
;
5630 insts
[ninsts
++] = m_class_get_byval_arg (mono_defaults
.single_class
);
5631 insts
[ninsts
++] = m_class_get_byval_arg (mono_defaults
.double_class
);
5632 insts
[ninsts
++] = m_class_get_byval_arg (mono_defaults
.char_class
);
5633 insts
[ninsts
++] = m_class_get_byval_arg (mono_defaults
.boolean_class
);
5635 /* Add GenericComparer<T> instances for primitive types for Enum.ToString () */
5636 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "GenericComparer`1");
5638 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5639 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "GenericEqualityComparer`1");
5641 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5643 /* Add instances of EnumEqualityComparer which are created by EqualityComparer<T> for enums */
5645 MonoClass
*enum_comparer
;
5646 MonoType
*insts
[16];
5650 insts
[ninsts
++] = int32_type
;
5651 insts
[ninsts
++] = uint32_type
;
5652 insts
[ninsts
++] = uint16_type
;
5653 insts
[ninsts
++] = byte_type
;
5654 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "EnumEqualityComparer`1");
5655 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
5657 #ifndef ENABLE_NETCORE
5659 insts
[ninsts
++] = int16_type
;
5660 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "ShortEnumEqualityComparer`1");
5661 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
5664 insts
[ninsts
++] = sbyte_type
;
5665 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "SByteEnumEqualityComparer`1");
5666 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
5668 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "LongEnumEqualityComparer`1");
5670 insts
[ninsts
++] = int64_type
;
5671 insts
[ninsts
++] = uint64_type
;
5672 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
5676 /* Add instances of the array generic interfaces for primitive types */
5677 /* This will add instances of the InternalArray_ helper methods in Array too */
5678 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "ICollection`1");
5680 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5682 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "IList`1");
5684 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5686 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "IEnumerable`1");
5688 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5691 * Add a managed-to-native wrapper of Array.GetGenericValue_icall<object>, which is
5692 * used for all instances of GetGenericValue_icall by the AOT runtime.
5696 MonoGenericContext ctx
;
5697 MonoMethod
*get_method
;
5698 MonoClass
*array_klass
= m_class_get_parent (mono_class_create_array (mono_defaults
.object_class
, 1));
5700 get_method
= mono_class_get_method_from_name_checked (array_klass
, "GetGenericValue_icall", 3, 0, error
);
5701 mono_error_assert_ok (error
);
5704 memset (&ctx
, 0, sizeof (ctx
));
5705 MonoType
*args
[ ] = { object_type
};
5706 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
5707 add_extra_method (acfg
, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (get_method
, &ctx
, error
), TRUE
, TRUE
));
5708 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5712 /* object[] accessor wrappers. */
5713 for (i
= 1; i
< 4; ++i
) {
5714 MonoClass
*obj_array_class
= mono_class_create_array (mono_defaults
.object_class
, i
);
5717 m
= get_method_nofail (obj_array_class
, "Get", i
, 0);
5720 m
= mono_marshal_get_array_accessor_wrapper (m
);
5721 add_extra_method (acfg
, m
);
5723 m
= get_method_nofail (obj_array_class
, "Address", i
, 0);
5726 m
= mono_marshal_get_array_accessor_wrapper (m
);
5727 add_extra_method (acfg
, m
);
5729 m
= get_method_nofail (obj_array_class
, "Set", i
+ 1, 0);
5732 m
= mono_marshal_get_array_accessor_wrapper (m
);
5733 add_extra_method (acfg
, m
);
5739 decode_direct_icall_symbol_name_attribute (MonoMethod
*method
)
5744 char *symbol_name
= NULL
;
5746 MonoCustomAttrInfo
*cattr
= mono_custom_attrs_from_method_checked (method
, error
);
5747 if (is_ok(error
) && cattr
) {
5748 for (j
= 0; j
< cattr
->num_attrs
; j
++)
5749 if (cattr
->attrs
[j
].ctor
&& !strcmp (m_class_get_name (cattr
->attrs
[j
].ctor
->klass
), "MonoDirectICallSymbolNameAttribute"))
5752 if (j
< cattr
->num_attrs
) {
5753 MonoCustomAttrEntry
*e
= &cattr
->attrs
[j
];
5754 MonoMethodSignature
*sig
= mono_method_signature_internal (e
->ctor
);
5755 if (e
->data
&& sig
&& sig
->param_count
== 1 && sig
->params
[0]->type
== MONO_TYPE_STRING
) {
5757 * Decode the cattr manually since we can't create objects
5758 * during aot compilation.
5762 const char *p
= ((const char*)e
->data
) + 2;
5763 int slen
= mono_metadata_decode_value (p
, &p
);
5765 symbol_name
= (char *)g_memdup (p
, slen
+ 1);
5767 symbol_name
[slen
] = 0;
5775 lookup_external_icall_symbol_name_aot (MonoMethod
*method
)
5777 g_assert (method_to_external_icall_symbol_name
);
5779 gpointer key
, value
;
5780 if (g_hash_table_lookup_extended (method_to_external_icall_symbol_name
, method
, &key
, &value
))
5781 return (const char*)value
;
5783 char *symbol_name
= decode_direct_icall_symbol_name_attribute (method
);
5784 g_hash_table_insert (method_to_external_icall_symbol_name
, method
, symbol_name
);
5790 lookup_icall_symbol_name_aot (MonoMethod
*method
)
5792 const char * symbol_name
= mono_lookup_icall_symbol (method
);
5794 symbol_name
= lookup_external_icall_symbol_name_aot (method
);
5800 mono_aot_direct_icalls_enabled_for_method (MonoCompile
*cfg
, MonoMethod
*method
)
5802 gboolean enable_icall
= FALSE
;
5803 if (cfg
->compile_aot
)
5804 enable_icall
= lookup_external_icall_symbol_name_aot (method
) ? TRUE
: FALSE
;
5806 enable_icall
= FALSE
;
5808 return enable_icall
;
5812 * is_direct_callable:
5814 * Return whenever the method identified by JI is directly callable without
5815 * going through the PLT.
5818 is_direct_callable (MonoAotCompile
*acfg
, MonoMethod
*method
, MonoJumpInfo
*patch_info
)
5820 if ((patch_info
->type
== MONO_PATCH_INFO_METHOD
) && (m_class_get_image (patch_info
->data
.method
->klass
) == acfg
->image
)) {
5821 MonoCompile
*callee_cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, patch_info
->data
.method
);
5823 gboolean direct_callable
= TRUE
;
5825 if (direct_callable
&& (acfg
->aot_opts
.dedup
|| acfg
->aot_opts
.dedup_include
) && mono_aot_can_dedup (patch_info
->data
.method
))
5826 direct_callable
= FALSE
;
5828 if (direct_callable
&& (!acfg
->llvm
|| acfg
->aot_opts
.llvm_disable_self_init
) && !(!callee_cfg
->has_got_slots
&& mono_class_is_before_field_init (callee_cfg
->method
->klass
)))
5829 direct_callable
= FALSE
;
5831 if (direct_callable
&& !strcmp (callee_cfg
->method
->name
, ".cctor"))
5832 direct_callable
= FALSE
;
5835 // FIXME: Support inflated methods, it asserts in mini_llvm_init_gshared_method_this () because the method is not in
5836 // amodule->extra_methods.
5838 if (direct_callable
&& callee_cfg
->method
->is_inflated
)
5839 direct_callable
= FALSE
;
5841 if (direct_callable
&& (callee_cfg
->method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
) && (!method
|| method
->wrapper_type
!= MONO_WRAPPER_SYNCHRONIZED
))
5842 // FIXME: Maybe call the wrapper directly ?
5843 direct_callable
= FALSE
;
5845 if (direct_callable
&& (acfg
->aot_opts
.soft_debug
|| acfg
->aot_opts
.no_direct_calls
)) {
5846 /* Disable this so all calls go through load_method (), see the
5847 * mini_get_debug_options ()->load_aot_jit_info_eagerly = TRUE; line in
5848 * mono_debugger_agent_init ().
5850 direct_callable
= FALSE
;
5853 if (direct_callable
&& (callee_cfg
->method
->wrapper_type
== MONO_WRAPPER_ALLOC
))
5854 /* sgen does some initialization when the allocator method is created */
5855 direct_callable
= FALSE
;
5856 if (direct_callable
&& (callee_cfg
->method
->wrapper_type
== MONO_WRAPPER_WRITE_BARRIER
))
5857 /* we don't know at compile time whether sgen is concurrent or not */
5858 direct_callable
= FALSE
;
5860 if (direct_callable
)
5863 } else if ((patch_info
->type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
&& patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)) {
5864 if (acfg
->aot_opts
.direct_pinvoke
)
5866 } else if (patch_info
->type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
5867 if (acfg
->aot_opts
.direct_icalls
)
5875 #ifdef MONO_ARCH_AOT_SUPPORTED
5877 get_pinvoke_import (MonoAotCompile
*acfg
, MonoMethod
*method
)
5879 MonoImage
*image
= m_class_get_image (method
->klass
);
5880 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*) method
;
5881 MonoTableInfo
*tables
= image
->tables
;
5882 MonoTableInfo
*im
= &tables
[MONO_TABLE_IMPLMAP
];
5883 MonoTableInfo
*mr
= &tables
[MONO_TABLE_MODULEREF
];
5884 guint32 im_cols
[MONO_IMPLMAP_SIZE
];
5887 import
= (char *)g_hash_table_lookup (acfg
->method_to_pinvoke_import
, method
);
5891 if (!piinfo
->implmap_idx
|| piinfo
->implmap_idx
> im
->rows
)
5894 mono_metadata_decode_row (im
, piinfo
->implmap_idx
- 1, im_cols
, MONO_IMPLMAP_SIZE
);
5896 if (!im_cols
[MONO_IMPLMAP_SCOPE
] || im_cols
[MONO_IMPLMAP_SCOPE
] > mr
->rows
)
5899 import
= g_strdup_printf ("%s", mono_metadata_string_heap (image
, im_cols
[MONO_IMPLMAP_NAME
]));
5901 g_hash_table_insert (acfg
->method_to_pinvoke_import
, method
, import
);
5907 get_pinvoke_import (MonoAotCompile
*acfg
, MonoMethod
*method
)
5914 compare_lne (MonoDebugLineNumberEntry
*a
, MonoDebugLineNumberEntry
*b
)
5916 if (a
->native_offset
== b
->native_offset
)
5917 return a
->il_offset
- b
->il_offset
;
5919 return a
->native_offset
- b
->native_offset
;
5923 * compute_line_numbers:
5925 * Returns a sparse array of size CODE_SIZE containing MonoDebugSourceLocation* entries for the native offsets which have a corresponding line number
5928 static MonoDebugSourceLocation
**
5929 compute_line_numbers (MonoMethod
*method
, int code_size
, MonoDebugMethodJitInfo
*debug_info
)
5931 MonoDebugMethodInfo
*minfo
;
5932 MonoDebugLineNumberEntry
*ln_array
;
5933 MonoDebugSourceLocation
*loc
;
5934 int i
, prev_line
, prev_il_offset
;
5935 int *native_to_il_offset
= NULL
;
5936 MonoDebugSourceLocation
**res
;
5939 minfo
= mono_debug_lookup_method (method
);
5942 // FIXME: This seems to happen when two methods have the same cfg->method_to_register
5943 if (debug_info
->code_size
!= code_size
)
5946 g_assert (code_size
);
5948 /* Compute the native->IL offset mapping */
5950 ln_array
= g_new0 (MonoDebugLineNumberEntry
, debug_info
->num_line_numbers
);
5951 memcpy (ln_array
, debug_info
->line_numbers
, debug_info
->num_line_numbers
* sizeof (MonoDebugLineNumberEntry
));
5953 mono_qsort (ln_array
, debug_info
->num_line_numbers
, sizeof (MonoDebugLineNumberEntry
), (int (*)(const void *, const void *))compare_lne
);
5955 native_to_il_offset
= g_new0 (int, code_size
+ 1);
5957 for (i
= 0; i
< debug_info
->num_line_numbers
; ++i
) {
5959 MonoDebugLineNumberEntry
*lne
= &ln_array
[i
];
5962 for (j
= 0; j
< lne
->native_offset
; ++j
)
5963 native_to_il_offset
[j
] = -1;
5966 if (i
< debug_info
->num_line_numbers
- 1) {
5967 MonoDebugLineNumberEntry
*lne_next
= &ln_array
[i
+ 1];
5969 for (j
= lne
->native_offset
; j
< lne_next
->native_offset
; ++j
)
5970 native_to_il_offset
[j
] = lne
->il_offset
;
5972 for (j
= lne
->native_offset
; j
< code_size
; ++j
)
5973 native_to_il_offset
[j
] = lne
->il_offset
;
5978 /* Compute the native->line number mapping */
5979 res
= g_new0 (MonoDebugSourceLocation
*, code_size
);
5980 prev_il_offset
= -1;
5983 for (i
= 0; i
< code_size
; ++i
) {
5984 int il_offset
= native_to_il_offset
[i
];
5986 if (il_offset
== -1 || il_offset
== prev_il_offset
)
5988 prev_il_offset
= il_offset
;
5989 loc
= mono_debug_method_lookup_location (minfo
, il_offset
);
5990 if (!(loc
&& loc
->source_file
))
5992 if (loc
->row
== prev_line
) {
5993 mono_debug_free_source_location (loc
);
5996 prev_line
= loc
->row
;
5997 //printf ("D: %s:%d il=%x native=%x\n", loc->source_file, loc->row, il_offset, i);
5999 /* This will cover the prolog too */
6009 get_file_index (MonoAotCompile
*acfg
, const char *source_file
)
6013 // FIXME: Free these
6014 if (!acfg
->dwarf_ln_filenames
)
6015 acfg
->dwarf_ln_filenames
= g_hash_table_new (g_str_hash
, g_str_equal
);
6016 findex
= GPOINTER_TO_INT (g_hash_table_lookup (acfg
->dwarf_ln_filenames
, source_file
));
6018 findex
= g_hash_table_size (acfg
->dwarf_ln_filenames
) + 1;
6019 g_hash_table_insert (acfg
->dwarf_ln_filenames
, g_strdup (source_file
), GINT_TO_POINTER (findex
));
6020 emit_unset_mode (acfg
);
6021 fprintf (acfg
->fp
, ".file %d \"%s\"\n", findex
, mono_dwarf_escape_path (source_file
));
6033 * emit_and_reloc_code:
6035 * Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
6036 * is true, calls are made through the GOT too. This is used for emitting trampolines
6037 * in full-aot mode, since calls made from trampolines couldn't go through the PLT,
6038 * since trampolines are needed to make PLT work.
6041 emit_and_reloc_code (MonoAotCompile
*acfg
, MonoMethod
*method
, guint8
*code
, guint32 code_len
, MonoJumpInfo
*relocs
, gboolean got_only
, MonoDebugMethodJitInfo
*debug_info
)
6043 int i
, pindex
, start_index
;
6045 MonoJumpInfo
*patch_info
;
6046 MonoDebugSourceLocation
**locs
= NULL
;
6047 gboolean skip
, prologue_end
= FALSE
;
6048 #ifdef MONO_ARCH_AOT_SUPPORTED
6049 gboolean direct_call
, external_call
;
6051 const char *direct_call_target
= 0;
6052 const char *direct_pinvoke
;
6055 if (acfg
->gas_line_numbers
&& method
&& debug_info
) {
6056 locs
= compute_line_numbers (method
, code_len
, debug_info
);
6058 int findex
= get_file_index (acfg
, "<unknown>");
6059 emit_unset_mode (acfg
);
6060 fprintf (acfg
->fp
, ".loc %d %d 0\n", findex
, 1);
6064 /* Collect and sort relocations */
6065 patches
= g_ptr_array_new ();
6066 for (patch_info
= relocs
; patch_info
; patch_info
= patch_info
->next
)
6067 g_ptr_array_add (patches
, patch_info
);
6068 g_ptr_array_sort (patches
, compare_patches
);
6071 for (i
= 0; i
< code_len
; i
+= INST_LEN
) {
6073 for (pindex
= start_index
; pindex
< patches
->len
; ++pindex
) {
6074 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
6075 if (patch_info
->ip
.i
>= i
)
6079 if (locs
&& locs
[i
]) {
6080 MonoDebugSourceLocation
*loc
= locs
[i
];
6082 const char *options
;
6084 findex
= get_file_index (acfg
, loc
->source_file
);
6085 emit_unset_mode (acfg
);
6087 options
= " prologue_end";
6090 prologue_end
= TRUE
;
6091 fprintf (acfg
->fp
, ".loc %d %d 0%s\n", findex
, loc
->row
, options
);
6092 mono_debug_free_source_location (loc
);
6096 #ifdef MONO_ARCH_AOT_SUPPORTED
6097 if (patch_info
&& (patch_info
->ip
.i
== i
) && (pindex
< patches
->len
)) {
6098 start_index
= pindex
;
6100 switch (patch_info
->type
) {
6101 case MONO_PATCH_INFO_NONE
:
6103 case MONO_PATCH_INFO_GOT_OFFSET
: {
6106 arch_emit_got_offset (acfg
, code
+ i
, &code_size
);
6107 i
+= code_size
- INST_LEN
;
6109 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6112 case MONO_PATCH_INFO_OBJC_SELECTOR_REF
: {
6113 int code_size
, index
;
6114 char *selector
= (char *)patch_info
->data
.target
;
6116 if (!acfg
->objc_selector_to_index
)
6117 acfg
->objc_selector_to_index
= g_hash_table_new (g_str_hash
, g_str_equal
);
6118 if (!acfg
->objc_selectors
)
6119 acfg
->objc_selectors
= g_ptr_array_new ();
6120 index
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->objc_selector_to_index
, selector
));
6124 index
= acfg
->objc_selector_index
;
6125 g_ptr_array_add (acfg
->objc_selectors
, (void*)patch_info
->data
.target
);
6126 g_hash_table_insert (acfg
->objc_selector_to_index
, selector
, GUINT_TO_POINTER (index
+ 1));
6127 acfg
->objc_selector_index
++;
6130 arch_emit_objc_selector_ref (acfg
, code
+ i
, index
, &code_size
);
6131 i
+= code_size
- INST_LEN
;
6133 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6138 * If this patch is a call, try emitting a direct call instead of
6139 * through a PLT entry. This is possible if the called method is in
6140 * the same assembly and requires no initialization.
6142 direct_call
= FALSE
;
6143 external_call
= FALSE
;
6144 if ((patch_info
->type
== MONO_PATCH_INFO_METHOD
) && (m_class_get_image (patch_info
->data
.method
->klass
) == acfg
->image
)) {
6145 if (!got_only
&& is_direct_callable (acfg
, method
, patch_info
)) {
6146 MonoCompile
*callee_cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, patch_info
->data
.method
);
6148 // Don't compile inflated methods if we're doing dedup
6149 if (acfg
->aot_opts
.dedup
&& !mono_aot_can_dedup (patch_info
->data
.method
)) {
6150 char *name
= mono_aot_get_mangled_method_name (patch_info
->data
.method
);
6151 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "DIRECT CALL: %s by %s", name
, method
? mono_method_full_name (method
, TRUE
) : "");
6155 direct_call_target
= callee_cfg
->asm_symbol
;
6156 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6157 acfg
->stats
.direct_calls
++;
6161 acfg
->stats
.all_calls
++;
6162 } else if (patch_info
->type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
6163 if (!got_only
&& is_direct_callable (acfg
, method
, patch_info
)) {
6164 if (!(patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
6165 direct_pinvoke
= lookup_icall_symbol_name_aot (patch_info
->data
.method
);
6167 direct_pinvoke
= get_pinvoke_import (acfg
, patch_info
->data
.method
);
6168 if (direct_pinvoke
) {
6170 g_assert (strlen (direct_pinvoke
) < 1000);
6171 direct_call_target
= g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, direct_pinvoke
);
6174 } else if (patch_info
->type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
6175 const char *sym
= mono_find_jit_icall_info (patch_info
->data
.jit_icall_id
)->c_symbol
;
6176 if (!got_only
&& sym
&& acfg
->aot_opts
.direct_icalls
) {
6177 /* Call to a C function implementing a jit icall */
6179 external_call
= TRUE
;
6180 g_assert (strlen (sym
) < 1000);
6181 direct_call_target
= g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, sym
);
6183 } else if (patch_info
->type
== MONO_PATCH_INFO_JIT_ICALL_ID
) {
6184 MonoJitICallInfo
* const info
= mono_find_jit_icall_info (patch_info
->data
.jit_icall_id
);
6185 const char * const sym
= info
->c_symbol
;
6186 if (!got_only
&& sym
&& acfg
->aot_opts
.direct_icalls
&& info
->func
== info
->wrapper
) {
6187 /* Call to a jit icall without a wrapper */
6189 external_call
= TRUE
;
6190 g_assert (strlen (sym
) < 1000);
6191 direct_call_target
= g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, sym
);
6195 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6196 acfg
->stats
.direct_calls
++;
6199 if (!got_only
&& !direct_call
) {
6200 MonoPltEntry
*plt_entry
= get_plt_entry (acfg
, patch_info
);
6202 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
6204 direct_call_target
= plt_entry
->symbol
;
6206 /* Nullify the patch */
6207 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6208 plt_entry
->jit_used
= TRUE
;
6215 arch_emit_direct_call (acfg
, direct_call_target
, external_call
, FALSE
, patch_info
, &call_size
);
6216 i
+= call_size
- INST_LEN
;
6220 got_slot
= get_got_offset (acfg
, FALSE
, patch_info
);
6222 arch_emit_got_access (acfg
, acfg
->got_symbol
, code
+ i
, got_slot
, &code_size
);
6223 i
+= code_size
- INST_LEN
;
6229 #endif /* MONO_ARCH_AOT_SUPPORTED */
6232 /* Find next patch */
6234 for (pindex
= start_index
; pindex
< patches
->len
; ++pindex
) {
6235 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
6236 if (patch_info
->ip
.i
>= i
)
6240 /* Try to emit multiple bytes at once */
6241 if (pindex
< patches
->len
&& patch_info
->ip
.i
> i
) {
6244 for (limit
= i
+ INST_LEN
; limit
< patch_info
->ip
.i
; limit
+= INST_LEN
) {
6245 if (locs
&& locs
[limit
])
6249 emit_code_bytes (acfg
, code
+ i
, limit
- i
);
6250 i
= limit
- INST_LEN
;
6252 emit_code_bytes (acfg
, code
+ i
, INST_LEN
);
6257 g_ptr_array_free (patches
, TRUE
);
6264 * Return a modified version of S which only includes characters permissible in symbols.
6267 sanitize_symbol (MonoAotCompile
*acfg
, char *s
)
6269 gboolean process
= FALSE
;
6278 for (i
= 0; i
< len
; ++i
)
6279 if (!(s
[i
] <= 0x7f && (isalnum (s
[i
]) || s
[i
] == '_')))
6284 gs
= g_string_sized_new (len
);
6285 for (i
= 0; i
< len
; ++i
) {
6287 if (c
<= 0x7f && (isalnum (c
) || c
== '_')) {
6288 g_string_append_c (gs
, c
);
6289 } else if (c
> 0x7f) {
6290 /* multi-byte utf8 */
6291 g_string_append_printf (gs
, "_0x%x", c
);
6294 while (c
>> 6 == 0x2) {
6295 g_string_append_printf (gs
, "%x", c
);
6299 g_string_append_printf (gs
, "_");
6302 g_string_append_c (gs
, '_');
6306 res
= mono_mempool_strdup (acfg
->mempool
, gs
->str
);
6307 g_string_free (gs
, TRUE
);
6312 get_debug_sym (MonoMethod
*method
, const char *prefix
, GHashTable
*cache
)
6314 char *name1
, *name2
, *cached
;
6315 int i
, j
, len
, count
;
6316 MonoMethod
*cached_method
;
6318 name1
= mono_method_full_name (method
, TRUE
);
6321 // This is so that we don't accidentally create a local symbol (which starts with 'L')
6322 if ((!prefix
|| !*prefix
) && name1
[0] == 'L')
6326 #if defined(TARGET_WIN32) && defined(TARGET_X86)
6327 char adjustedPrefix
[MAX_SYMBOL_SIZE
];
6328 prefix
= mangle_symbol (prefix
, adjustedPrefix
, G_N_ELEMENTS (adjustedPrefix
));
6331 len
= strlen (name1
);
6332 name2
= (char *) g_malloc (strlen (prefix
) + len
+ 16);
6333 memcpy (name2
, prefix
, strlen (prefix
));
6334 j
= strlen (prefix
);
6335 for (i
= 0; i
< len
; ++i
) {
6336 if (i
== 0 && name1
[0] >= '0' && name1
[0] <= '9') {
6338 } else if (isalnum (name1
[i
])) {
6339 name2
[j
++] = name1
[i
];
6340 } else if (name1
[i
] == ' ' && name1
[i
+ 1] == '(' && name1
[i
+ 2] == ')') {
6342 } else if (name1
[i
] == ',' && name1
[i
+ 1] == ' ') {
6345 } else if (name1
[i
] == '(' || name1
[i
] == ')' || name1
[i
] == '>') {
6355 cached_method
= (MonoMethod
*)g_hash_table_lookup (cache
, name2
);
6356 if (!(cached_method
&& cached_method
!= method
))
6358 sprintf (name2
+ j
, "_%d", count
);
6362 cached
= g_strdup (name2
);
6363 g_hash_table_insert (cache
, cached
, method
);
6369 emit_method_code (MonoAotCompile
*acfg
, MonoCompile
*cfg
)
6374 char *debug_sym
= NULL
;
6375 char *symbol
= NULL
;
6376 int func_alignment
= AOT_FUNC_ALIGNMENT
;
6379 g_assert (!ignore_cfg (cfg
));
6381 method
= cfg
->orig_method
;
6382 code
= cfg
->native_code
;
6384 method_index
= get_method_index (acfg
, method
);
6385 symbol
= g_strdup_printf ("%sme_%x", acfg
->temp_prefix
, method_index
);
6387 /* Make the labels local */
6388 emit_section_change (acfg
, ".text", 0);
6389 emit_alignment_code (acfg
, func_alignment
);
6391 if (acfg
->global_symbols
&& acfg
->need_no_dead_strip
)
6392 fprintf (acfg
->fp
, " .no_dead_strip %s\n", cfg
->asm_symbol
);
6394 emit_label (acfg
, cfg
->asm_symbol
);
6396 if (acfg
->aot_opts
.write_symbols
&& !acfg
->global_symbols
&& !acfg
->llvm
) {
6398 * Write a C style symbol for every method, this has two uses:
6399 * - it works on platforms where the dwarf debugging info is not
6401 * - it allows the setting of breakpoints of aot-ed methods.
6404 // Comment out to force dedup to link these symbols and forbid compiling
6405 // in duplicated code. This is an "assert when linking if broken" trick.
6406 /*if (mono_aot_can_dedup (method) && (acfg->aot_opts.dedup || acfg->aot_opts.dedup_include))*/
6407 /*debug_sym = mono_aot_get_mangled_method_name (method);*/
6409 debug_sym
= get_debug_sym (method
, "", acfg
->method_label_hash
);
6411 cfg
->asm_debug_symbol
= g_strdup (debug_sym
);
6413 if (acfg
->need_no_dead_strip
)
6414 fprintf (acfg
->fp
, " .no_dead_strip %s\n", debug_sym
);
6416 // Comment out to force dedup to link these symbols and forbid compiling
6417 // in duplicated code. This is an "assert when linking if broken" trick.
6418 /*if (mono_aot_can_dedup (method) && (acfg->aot_opts.dedup || acfg->aot_opts.dedup_include))*/
6419 /*emit_global_inner (acfg, debug_sym, TRUE);*/
6421 emit_local_symbol (acfg
, debug_sym
, symbol
, TRUE
);
6423 emit_label (acfg
, debug_sym
);
6426 export_name
= (char *)g_hash_table_lookup (acfg
->export_names
, method
);
6428 /* Emit a global symbol for the method */
6429 emit_global_inner (acfg
, export_name
, TRUE
);
6430 emit_label (acfg
, export_name
);
6433 if (cfg
->verbose_level
> 0 && !ignore_cfg (cfg
))
6434 g_print ("Method %s emitted as %s\n", mono_method_get_full_name (method
), cfg
->asm_symbol
);
6436 acfg
->stats
.code_size
+= cfg
->code_len
;
6438 acfg
->cfgs
[method_index
]->got_offset
= acfg
->got_offset
;
6440 emit_and_reloc_code (acfg
, method
, code
, cfg
->code_len
, cfg
->patch_info
, FALSE
, mono_debug_find_method (cfg
->jit_info
->d
.method
, mono_domain_get ()));
6444 if (acfg
->aot_opts
.write_symbols
) {
6446 emit_symbol_size (acfg
, debug_sym
, ".");
6448 emit_symbol_size (acfg
, cfg
->asm_symbol
, ".");
6452 emit_label (acfg
, symbol
);
6454 arch_emit_unwind_info_sections (acfg
, cfg
->asm_symbol
, symbol
, cfg
->unwind_ops
);
6462 * Encode PATCH_INFO into its disk representation.
6465 encode_patch (MonoAotCompile
*acfg
, MonoJumpInfo
*patch_info
, guint8
*buf
, guint8
**endbuf
)
6469 switch (patch_info
->type
) {
6470 case MONO_PATCH_INFO_NONE
:
6472 case MONO_PATCH_INFO_IMAGE
:
6473 encode_value (get_image_index (acfg
, patch_info
->data
.image
), p
, &p
);
6475 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
:
6476 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
:
6477 case MONO_PATCH_INFO_GC_NURSERY_START
:
6478 case MONO_PATCH_INFO_GC_NURSERY_BITS
:
6480 case MONO_PATCH_INFO_SWITCH
: {
6481 gpointer
*table
= (gpointer
*)patch_info
->data
.table
->table
;
6484 encode_value (patch_info
->data
.table
->table_size
, p
, &p
);
6485 for (k
= 0; k
< patch_info
->data
.table
->table_size
; k
++)
6486 encode_value ((int)(gssize
)table
[k
], p
, &p
);
6489 case MONO_PATCH_INFO_METHODCONST
:
6490 case MONO_PATCH_INFO_METHOD
:
6491 case MONO_PATCH_INFO_METHOD_JUMP
:
6492 case MONO_PATCH_INFO_METHOD_FTNDESC
:
6493 case MONO_PATCH_INFO_ICALL_ADDR
:
6494 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
6495 case MONO_PATCH_INFO_METHOD_RGCTX
:
6496 case MONO_PATCH_INFO_METHOD_CODE_SLOT
:
6497 encode_method_ref (acfg
, patch_info
->data
.method
, p
, &p
);
6499 case MONO_PATCH_INFO_AOT_JIT_INFO
:
6500 case MONO_PATCH_INFO_CASTCLASS_CACHE
:
6501 encode_value (patch_info
->data
.index
, p
, &p
);
6503 case MONO_PATCH_INFO_JIT_ICALL_ID
:
6504 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
6505 case MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
:
6506 encode_value (patch_info
->data
.jit_icall_id
, p
, &p
);
6508 case MONO_PATCH_INFO_SPECIFIC_TRAMPOLINE_LAZY_FETCH_ADDR
:
6509 encode_value (patch_info
->data
.uindex
, p
, &p
);
6511 case MONO_PATCH_INFO_LDSTR_LIT
: {
6512 guint32 len
= strlen (patch_info
->data
.name
);
6513 encode_value (len
, p
, &p
);
6514 memcpy (p
, patch_info
->data
.name
, len
+ 1);
6518 case MONO_PATCH_INFO_LDSTR
: {
6519 guint32 image_index
= get_image_index (acfg
, patch_info
->data
.token
->image
);
6520 guint32 token
= patch_info
->data
.token
->token
;
6521 g_assert (mono_metadata_token_code (token
) == MONO_TOKEN_STRING
);
6522 encode_value (image_index
, p
, &p
);
6523 encode_value (patch_info
->data
.token
->token
- MONO_TOKEN_STRING
, p
, &p
);
6526 case MONO_PATCH_INFO_RVA
:
6527 case MONO_PATCH_INFO_DECLSEC
:
6528 case MONO_PATCH_INFO_LDTOKEN
:
6529 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
6530 encode_value (get_image_index (acfg
, patch_info
->data
.token
->image
), p
, &p
);
6531 encode_value (patch_info
->data
.token
->token
, p
, &p
);
6532 encode_value (patch_info
->data
.token
->has_context
, p
, &p
);
6533 if (patch_info
->data
.token
->has_context
)
6534 encode_generic_context (acfg
, &patch_info
->data
.token
->context
, p
, &p
);
6536 case MONO_PATCH_INFO_EXC_NAME
: {
6537 MonoClass
*ex_class
;
6540 mono_class_load_from_name (m_class_get_image (mono_defaults
.exception_class
),
6541 "System", (const char *)patch_info
->data
.target
);
6542 encode_klass_ref (acfg
, ex_class
, p
, &p
);
6545 case MONO_PATCH_INFO_R4
:
6546 encode_value (*((guint32
*)patch_info
->data
.target
), p
, &p
);
6548 case MONO_PATCH_INFO_R8
:
6549 encode_value (((guint32
*)patch_info
->data
.target
) [MINI_LS_WORD_IDX
], p
, &p
);
6550 encode_value (((guint32
*)patch_info
->data
.target
) [MINI_MS_WORD_IDX
], p
, &p
);
6552 case MONO_PATCH_INFO_VTABLE
:
6553 case MONO_PATCH_INFO_CLASS
:
6554 case MONO_PATCH_INFO_IID
:
6555 case MONO_PATCH_INFO_ADJUSTED_IID
:
6556 encode_klass_ref (acfg
, patch_info
->data
.klass
, p
, &p
);
6558 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
6559 encode_klass_ref (acfg
, patch_info
->data
.del_tramp
->klass
, p
, &p
);
6560 if (patch_info
->data
.del_tramp
->method
) {
6561 encode_value (1, p
, &p
);
6562 encode_method_ref (acfg
, patch_info
->data
.del_tramp
->method
, p
, &p
);
6564 encode_value (0, p
, &p
);
6566 encode_value (patch_info
->data
.del_tramp
->is_virtual
, p
, &p
);
6568 case MONO_PATCH_INFO_FIELD
:
6569 case MONO_PATCH_INFO_SFLDA
:
6570 encode_field_info (acfg
, patch_info
->data
.field
, p
, &p
);
6572 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
6574 case MONO_PATCH_INFO_PROFILER_ALLOCATION_COUNT
:
6575 case MONO_PATCH_INFO_PROFILER_CLAUSE_COUNT
:
6577 case MONO_PATCH_INFO_RGCTX_FETCH
:
6578 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
6579 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
6583 * entry->d.klass/method has a lenghtly encoding and multiple rgctx_fetch entries
6584 * reference the same klass/method, so encode it only once.
6585 * For patches which refer to got entries, this sharing is done by get_got_offset, but
6586 * these are not got entries.
6588 if (entry
->in_mrgctx
) {
6589 offset
= get_shared_method_ref (acfg
, entry
->d
.method
);
6591 offset
= get_shared_klass_ref (acfg
, entry
->d
.klass
);
6594 encode_value (offset
, p
, &p
);
6595 g_assert ((int)entry
->info_type
< 256);
6596 g_assert (entry
->data
->type
< 256);
6597 encode_value ((entry
->in_mrgctx
? 1 : 0) | (entry
->info_type
<< 1) | (entry
->data
->type
<< 9), p
, &p
);
6598 encode_patch (acfg
, entry
->data
, p
, &p
);
6601 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
6602 case MONO_PATCH_INFO_AOT_MODULE
:
6604 case MONO_PATCH_INFO_SIGNATURE
:
6605 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER
:
6606 encode_signature (acfg
, (MonoMethodSignature
*)patch_info
->data
.target
, p
, &p
);
6608 case MONO_PATCH_INFO_GSHAREDVT_CALL
:
6609 encode_signature (acfg
, (MonoMethodSignature
*)patch_info
->data
.gsharedvt
->sig
, p
, &p
);
6610 encode_method_ref (acfg
, patch_info
->data
.gsharedvt
->method
, p
, &p
);
6612 case MONO_PATCH_INFO_GSHAREDVT_METHOD
: {
6613 MonoGSharedVtMethodInfo
*info
= patch_info
->data
.gsharedvt_method
;
6616 encode_method_ref (acfg
, info
->method
, p
, &p
);
6617 encode_value (info
->num_entries
, p
, &p
);
6618 for (i
= 0; i
< info
->num_entries
; ++i
) {
6619 MonoRuntimeGenericContextInfoTemplate
*template_
= &info
->entries
[i
];
6621 encode_value (template_
->info_type
, p
, &p
);
6622 switch (mini_rgctx_info_type_to_patch_info_type (template_
->info_type
)) {
6623 case MONO_PATCH_INFO_CLASS
:
6624 encode_klass_ref (acfg
, mono_class_from_mono_type_internal ((MonoType
*)template_
->data
), p
, &p
);
6626 case MONO_PATCH_INFO_FIELD
:
6627 encode_field_info (acfg
, (MonoClassField
*)template_
->data
, p
, &p
);
6630 g_assert_not_reached ();
6636 case MONO_PATCH_INFO_VIRT_METHOD
:
6637 encode_klass_ref (acfg
, patch_info
->data
.virt_method
->klass
, p
, &p
);
6638 encode_method_ref (acfg
, patch_info
->data
.virt_method
->method
, p
, &p
);
6640 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG
:
6643 g_error ("unable to handle jump info %d", patch_info
->type
);
6650 encode_patch_list (MonoAotCompile
*acfg
, GPtrArray
*patches
, int n_patches
, gboolean llvm
, guint8
*buf
, guint8
**endbuf
)
6653 guint32 pindex
, offset
;
6654 MonoJumpInfo
*patch_info
;
6656 encode_value (n_patches
, p
, &p
);
6658 for (pindex
= 0; pindex
< patches
->len
; ++pindex
) {
6659 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
6661 if (patch_info
->type
== MONO_PATCH_INFO_NONE
|| patch_info
->type
== MONO_PATCH_INFO_BB
)
6664 /* This shouldn't allocate a new offset */
6665 offset
= lookup_got_offset (acfg
, llvm
, patch_info
);
6666 encode_value (offset
, p
, &p
);
6673 emit_method_info (MonoAotCompile
*acfg
, MonoCompile
*cfg
)
6676 int pindex
, buf_size
, n_patches
;
6678 MonoJumpInfo
*patch_info
;
6681 gboolean needs_ctx
= FALSE
;
6683 method
= cfg
->orig_method
;
6685 (void)get_method_index (acfg
, method
);
6687 /* Sort relocations */
6688 patches
= g_ptr_array_new ();
6689 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
)
6690 g_ptr_array_add (patches
, patch_info
);
6691 if (!acfg
->aot_opts
.llvm_only
)
6692 g_ptr_array_sort (patches
, compare_patches
);
6694 /**********************/
6695 /* Encode method info */
6696 /**********************/
6698 g_assert (!(cfg
->opt
& MONO_OPT_SHARED
));
6700 guint32
*got_offsets
= g_new0 (guint32
, patches
->len
);
6703 for (pindex
= 0; pindex
< patches
->len
; ++pindex
) {
6704 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
6706 if ((patch_info
->type
== MONO_PATCH_INFO_GOT_OFFSET
) ||
6707 (patch_info
->type
== MONO_PATCH_INFO_NONE
)) {
6708 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6713 if ((patch_info
->type
== MONO_PATCH_INFO_IMAGE
) && (patch_info
->data
.image
== acfg
->image
)) {
6714 /* Stored in a GOT slot initialized at module load time */
6715 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6719 if (patch_info
->type
== MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
||
6720 patch_info
->type
== MONO_PATCH_INFO_GC_NURSERY_START
||
6721 patch_info
->type
== MONO_PATCH_INFO_GC_NURSERY_BITS
||
6722 patch_info
->type
== MONO_PATCH_INFO_AOT_MODULE
) {
6723 /* Stored in a GOT slot initialized at module load time */
6724 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6728 if (is_plt_patch (patch_info
) && !(cfg
->compile_llvm
&& acfg
->aot_opts
.llvm_only
)) {
6729 /* Calls are made through the PLT */
6730 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6734 if (acfg
->aot_opts
.llvm_only
&& patch_info
->type
== MONO_PATCH_INFO_METHOD
)
6737 /* This shouldn't allocate a new offset */
6738 offset
= lookup_got_offset (acfg
, cfg
->compile_llvm
, patch_info
);
6739 if (offset
>= acfg
->nshared_got_entries
)
6740 got_offsets
[n_patches
++] = offset
;
6744 g_assert (cfg
->has_got_slots
);
6746 buf_size
= (patches
->len
< 1000) ? 40960 : 40960 + (patches
->len
* 64);
6747 p
= buf
= (guint8
*)g_malloc (buf_size
);
6749 MonoGenericContext
*ctx
= mono_method_get_context (cfg
->method
);
6752 if (mono_class_get_cctor (method
->klass
))
6753 flags
|= MONO_AOT_METHOD_FLAG_HAS_CCTOR
;
6754 if (mini_jit_info_is_gsharedvt (cfg
->jit_info
) && mini_is_gsharedvt_variable_signature (mono_method_signature_internal (jinfo_get_method (cfg
->jit_info
))))
6755 flags
|= MONO_AOT_METHOD_FLAG_GSHAREDVT_VARIABLE
;
6757 flags
|= MONO_AOT_METHOD_FLAG_HAS_PATCHES
;
6758 if (needs_ctx
&& ctx
)
6759 flags
|= MONO_AOT_METHOD_FLAG_HAS_CTX
;
6760 encode_value (flags
, p
, &p
);
6761 if (flags
& MONO_AOT_METHOD_FLAG_HAS_CCTOR
)
6762 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
6763 if (needs_ctx
&& ctx
)
6764 encode_generic_context (acfg
, ctx
, p
, &p
);
6767 encode_value (n_patches
, p
, &p
);
6768 for (int i
= 0; i
< n_patches
; ++i
)
6769 encode_value (got_offsets
[i
], p
, &p
);
6772 g_ptr_array_free (patches
, TRUE
);
6773 g_free (got_offsets
);
6775 acfg
->stats
.method_info_size
+= p
- buf
;
6777 g_assert (p
- buf
< buf_size
);
6779 cfg
->method_info_offset
= add_to_blob (acfg
, buf
, p
- buf
);
6784 get_unwind_info_offset (MonoAotCompile
*acfg
, guint8
*encoded
, guint32 encoded_len
)
6786 guint32 cache_index
;
6789 /* Reuse the unwind module to canonize and store unwind info entries */
6790 cache_index
= mono_cache_unwind_info (encoded
, encoded_len
);
6792 /* Use +/- 1 to distinguish 0s from missing entries */
6793 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->unwind_info_offsets
, GUINT_TO_POINTER (cache_index
+ 1)));
6801 * It would be easier to use assembler symbols, but the caller needs an
6804 offset
= acfg
->unwind_info_offset
;
6805 g_hash_table_insert (acfg
->unwind_info_offsets
, GUINT_TO_POINTER (cache_index
+ 1), GUINT_TO_POINTER (offset
+ 1));
6806 g_ptr_array_add (acfg
->unwind_ops
, GUINT_TO_POINTER (cache_index
));
6809 encode_value (encoded_len
, p
, &p
);
6811 acfg
->unwind_info_offset
+= encoded_len
+ (p
- buf
);
6817 emit_exception_debug_info (MonoAotCompile
*acfg
, MonoCompile
*cfg
, gboolean store_seq_points
)
6820 guint32 debug_info_size
, seq_points_size
;
6822 MonoMethodHeader
*header
;
6823 guint8
*p
, *buf
, *debug_info
;
6824 MonoJitInfo
*jinfo
= cfg
->jit_info
;
6826 gboolean use_unwind_ops
= FALSE
;
6827 MonoSeqPointInfo
*seq_points
;
6829 code
= cfg
->native_code
;
6830 header
= cfg
->header
;
6832 if (!acfg
->aot_opts
.nodebug
) {
6833 mono_debug_serialize_debug_info (cfg
, &debug_info
, &debug_info_size
);
6836 debug_info_size
= 0;
6839 seq_points
= cfg
->seq_point_info
;
6840 seq_points_size
= (store_seq_points
)? mono_seq_point_info_get_write_size (seq_points
) : 0;
6842 buf_size
= header
->num_clauses
* 256 + debug_info_size
+ 2048 + seq_points_size
+ cfg
->gc_map_size
;
6843 if (jinfo
->has_try_block_holes
) {
6844 MonoTryBlockHoleTableJitInfo
*table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
6845 buf_size
+= table
->num_holes
* 16;
6848 p
= buf
= (guint8
*)g_malloc (buf_size
);
6850 use_unwind_ops
= cfg
->unwind_ops
!= NULL
;
6852 flags
= (jinfo
->has_generic_jit_info
? 1 : 0) | (use_unwind_ops
? 2 : 0) | (header
->num_clauses
? 4 : 0) | (seq_points_size
? 8 : 0) | (cfg
->compile_llvm
? 16 : 0) | (jinfo
->has_try_block_holes
? 32 : 0) | (cfg
->gc_map
? 64 : 0) | (jinfo
->has_arch_eh_info
? 128 : 0);
6854 encode_value (flags
, p
, &p
);
6856 if (use_unwind_ops
) {
6857 guint32 encoded_len
;
6859 guint32 unwind_desc
;
6861 encoded
= mono_unwind_ops_encode (cfg
->unwind_ops
, &encoded_len
);
6863 unwind_desc
= get_unwind_info_offset (acfg
, encoded
, encoded_len
);
6864 encode_value (unwind_desc
, p
, &p
);
6868 encode_value (jinfo
->unwind_info
, p
, &p
);
6871 /*Encode the number of holes before the number of clauses to make decoding easier*/
6872 if (jinfo
->has_try_block_holes
) {
6873 MonoTryBlockHoleTableJitInfo
*table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
6874 encode_value (table
->num_holes
, p
, &p
);
6877 if (jinfo
->has_arch_eh_info
) {
6879 * In AOT mode, the code length is calculated from the address of the previous method,
6880 * which could include alignment padding, so calculating the start of the epilog as
6881 * code_len - epilog_size is correct any more. Save the real code len as a workaround.
6883 encode_value (jinfo
->code_size
, p
, &p
);
6886 /* Exception table */
6887 if (cfg
->compile_llvm
) {
6889 * When using LLVM, we can't emit some data, like pc offsets, this reg/offset etc.,
6890 * since the information is only available to llc. Instead, we let llc save the data
6891 * into the LSDA, and read it from there at runtime.
6893 /* The assembly might be CIL stripped so emit the data ourselves */
6894 if (header
->num_clauses
)
6895 encode_value (header
->num_clauses
, p
, &p
);
6897 for (k
= 0; k
< header
->num_clauses
; ++k
) {
6898 MonoExceptionClause
*clause
;
6900 clause
= &header
->clauses
[k
];
6902 encode_value (clause
->flags
, p
, &p
);
6903 if (!(clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)) {
6904 if (clause
->data
.catch_class
) {
6908 buf2
= (guint8
*)g_malloc (4096);
6910 encode_klass_ref (acfg
, clause
->data
.catch_class
, p2
, &p2
);
6912 g_assert (len
< 4096);
6913 encode_value (len
, p
, &p
);
6914 memcpy (p
, buf2
, len
);
6918 encode_value (0, p
, &p
);
6922 /* Emit the IL ranges too, since they might not be available at runtime */
6923 encode_value (clause
->try_offset
, p
, &p
);
6924 encode_value (clause
->try_len
, p
, &p
);
6925 encode_value (clause
->handler_offset
, p
, &p
);
6926 encode_value (clause
->handler_len
, p
, &p
);
6928 /* Emit a list of nesting clauses */
6929 for (i
= 0; i
< header
->num_clauses
; ++i
) {
6931 MonoExceptionClause
*clause1
= &header
->clauses
[cindex1
];
6933 MonoExceptionClause
*clause2
= &header
->clauses
[cindex2
];
6935 if (cindex1
!= cindex2
&& clause1
->try_offset
>= clause2
->try_offset
&& clause1
->handler_offset
<= clause2
->handler_offset
)
6936 encode_value (i
, p
, &p
);
6938 encode_value (-1, p
, &p
);
6941 if (jinfo
->num_clauses
)
6942 encode_value (jinfo
->num_clauses
, p
, &p
);
6944 for (k
= 0; k
< jinfo
->num_clauses
; ++k
) {
6945 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[k
];
6947 encode_value (ei
->flags
, p
, &p
);
6948 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
6949 /* Not used for catch clauses */
6950 if (ei
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
)
6951 encode_value (ei
->exvar_offset
, p
, &p
);
6953 encode_value (ei
->exvar_offset
, p
, &p
);
6956 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
|| ei
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
6957 encode_value ((gint
)((guint8
*)ei
->data
.filter
- code
), p
, &p
);
6959 if (ei
->data
.catch_class
) {
6963 buf2
= (guint8
*)g_malloc (4096);
6965 encode_klass_ref (acfg
, ei
->data
.catch_class
, p2
, &p2
);
6967 g_assert (len
< 4096);
6968 encode_value (len
, p
, &p
);
6969 memcpy (p
, buf2
, len
);
6973 encode_value (0, p
, &p
);
6977 encode_value ((gint
)((guint8
*)ei
->try_start
- code
), p
, &p
);
6978 encode_value ((gint
)((guint8
*)ei
->try_end
- code
), p
, &p
);
6979 encode_value ((gint
)((guint8
*)ei
->handler_start
- code
), p
, &p
);
6983 if (jinfo
->has_try_block_holes
) {
6984 MonoTryBlockHoleTableJitInfo
*table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
6985 for (i
= 0; i
< table
->num_holes
; ++i
) {
6986 MonoTryBlockHoleJitInfo
*hole
= &table
->holes
[i
];
6987 encode_value (hole
->clause
, p
, &p
);
6988 encode_value (hole
->length
, p
, &p
);
6989 encode_value (hole
->offset
, p
, &p
);
6993 if (jinfo
->has_arch_eh_info
) {
6994 MonoArchEHJitInfo
*eh_info
;
6996 eh_info
= mono_jit_info_get_arch_eh_info (jinfo
);
6997 encode_value (eh_info
->stack_size
, p
, &p
);
6998 encode_value (eh_info
->epilog_size
, p
, &p
);
7001 if (jinfo
->has_generic_jit_info
) {
7002 MonoGenericJitInfo
*gi
= mono_jit_info_get_generic_jit_info (jinfo
);
7003 MonoGenericSharingContext
* gsctx
= gi
->generic_sharing_context
;
7007 encode_value (gi
->nlocs
, p
, &p
);
7009 for (i
= 0; i
< gi
->nlocs
; ++i
) {
7010 MonoDwarfLocListEntry
*entry
= &gi
->locations
[i
];
7012 encode_value (entry
->is_reg
? 1 : 0, p
, &p
);
7013 encode_value (entry
->reg
, p
, &p
);
7015 encode_value (entry
->offset
, p
, &p
);
7017 g_assert (entry
->from
== 0);
7019 encode_value (entry
->from
, p
, &p
);
7020 encode_value (entry
->to
, p
, &p
);
7023 if (!cfg
->compile_llvm
) {
7024 encode_value (gi
->has_this
? 1 : 0, p
, &p
);
7025 encode_value (gi
->this_reg
, p
, &p
);
7026 encode_value (gi
->this_offset
, p
, &p
);
7031 * Need to encode jinfo->method too, since it is not equal to 'method'
7032 * when using generic sharing.
7034 buf2
= (guint8
*)g_malloc (4096);
7036 encode_method_ref (acfg
, jinfo
->d
.method
, p2
, &p2
);
7038 g_assert (len
< 4096);
7039 encode_value (len
, p
, &p
);
7040 memcpy (p
, buf2
, len
);
7044 if (gsctx
&& gsctx
->is_gsharedvt
) {
7045 encode_value (1, p
, &p
);
7047 encode_value (0, p
, &p
);
7051 if (seq_points_size
)
7052 p
+= mono_seq_point_info_write (seq_points
, p
);
7054 g_assert (debug_info_size
< buf_size
);
7056 encode_value (debug_info_size
, p
, &p
);
7057 if (debug_info_size
) {
7058 memcpy (p
, debug_info
, debug_info_size
);
7059 p
+= debug_info_size
;
7060 g_free (debug_info
);
7065 encode_value (cfg
->gc_map_size
, p
, &p
);
7066 /* The GC map requires 4 bytes of alignment */
7067 while ((gsize
)p
% 4)
7069 memcpy (p
, cfg
->gc_map
, cfg
->gc_map_size
);
7070 p
+= cfg
->gc_map_size
;
7073 acfg
->stats
.ex_info_size
+= p
- buf
;
7075 g_assert (p
- buf
< buf_size
);
7078 /* The GC Map requires 4 byte alignment */
7079 cfg
->ex_info_offset
= add_to_blob_aligned (acfg
, buf
, p
- buf
, cfg
->gc_map
? 4 : 1);
7084 emit_klass_info (MonoAotCompile
*acfg
, guint32 token
)
7087 MonoClass
*klass
= mono_class_get_checked (acfg
->image
, token
, error
);
7089 int i
, buf_size
, res
;
7090 gboolean no_special_static
, cant_encode
;
7091 gpointer iter
= NULL
;
7094 mono_error_cleanup (error
);
7098 p
= buf
= (guint8
*)g_malloc (buf_size
);
7100 /* Mark as unusable */
7101 encode_value (-1, p
, &p
);
7103 res
= add_to_blob (acfg
, buf
, p
- buf
);
7109 buf_size
= 10240 + (m_class_get_vtable_size (klass
) * 16);
7110 p
= buf
= (guint8
*)g_malloc (buf_size
);
7114 mono_class_init_internal (klass
);
7116 mono_class_get_nested_types (klass
, &iter
);
7117 g_assert (m_class_is_nested_classes_inited (klass
));
7119 mono_class_setup_vtable (klass
);
7122 * Emit all the information which is required for creating vtables so
7123 * the runtime does not need to create the MonoMethod structures which
7124 * take up a lot of space.
7127 no_special_static
= !mono_class_has_special_static_fields (klass
);
7129 /* Check whenever we have enough info to encode the vtable */
7130 cant_encode
= FALSE
;
7131 MonoMethod
**klass_vtable
= m_class_get_vtable (klass
);
7132 for (i
= 0; i
< m_class_get_vtable_size (klass
); ++i
) {
7133 MonoMethod
*cm
= klass_vtable
[i
];
7135 if (cm
&& mono_method_signature_internal (cm
)->is_inflated
&& !g_hash_table_lookup (acfg
->token_info_hash
, cm
))
7139 mono_class_has_finalizer (klass
);
7140 if (mono_class_has_failure (klass
))
7143 if (mono_class_is_gtd (klass
) || cant_encode
) {
7144 encode_value (-1, p
, &p
);
7146 gboolean has_nested
= mono_class_get_nested_classes_property (klass
) != NULL
;
7147 encode_value (m_class_get_vtable_size (klass
), p
, &p
);
7148 encode_value ((m_class_has_weak_fields (klass
) << 9) | (mono_class_is_gtd (klass
) ? (1 << 8) : 0) | (no_special_static
<< 7) | (m_class_has_static_refs (klass
) << 6) | (m_class_has_references (klass
) << 5) | ((m_class_is_blittable (klass
) << 4) | (has_nested
? 1 : 0) << 3) | (m_class_has_cctor (klass
) << 2) | (m_class_has_finalize (klass
) << 1) | m_class_is_ghcimpl (klass
), p
, &p
);
7149 if (m_class_has_cctor (klass
))
7150 encode_method_ref (acfg
, mono_class_get_cctor (klass
), p
, &p
);
7151 if (m_class_has_finalize (klass
))
7152 encode_method_ref (acfg
, mono_class_get_finalizer (klass
), p
, &p
);
7154 encode_value (m_class_get_instance_size (klass
), p
, &p
);
7155 encode_value (mono_class_data_size (klass
), p
, &p
);
7156 encode_value (m_class_get_packing_size (klass
), p
, &p
);
7157 encode_value (m_class_get_min_align (klass
), p
, &p
);
7159 for (i
= 0; i
< m_class_get_vtable_size (klass
); ++i
) {
7160 MonoMethod
*cm
= klass_vtable
[i
];
7163 encode_method_ref (acfg
, cm
, p
, &p
);
7165 encode_value (0, p
, &p
);
7169 acfg
->stats
.class_info_size
+= p
- buf
;
7171 g_assert (p
- buf
< buf_size
);
7172 res
= add_to_blob (acfg
, buf
, p
- buf
);
7179 get_plt_entry_debug_sym (MonoAotCompile
*acfg
, MonoJumpInfo
*ji
, GHashTable
*cache
)
7181 char *debug_sym
= NULL
;
7184 if (acfg
->llvm
&& llvm_acfg
->aot_opts
.static_link
) {
7185 /* Need to add a prefix to create unique symbols */
7186 prefix
= g_strdup_printf ("plt_%s_", acfg
->assembly_name_sym
);
7188 #if defined(TARGET_WIN32) && defined(TARGET_X86)
7189 prefix
= mangle_symbol_alloc ("plt_");
7191 prefix
= g_strdup ("plt_");
7196 case MONO_PATCH_INFO_METHOD
:
7197 debug_sym
= get_debug_sym (ji
->data
.method
, prefix
, cache
);
7199 case MONO_PATCH_INFO_JIT_ICALL_ID
:
7200 debug_sym
= g_strdup_printf ("%s_jit_icall_%s", prefix
, mono_find_jit_icall_info (ji
->data
.jit_icall_id
)->name
);
7202 case MONO_PATCH_INFO_RGCTX_FETCH
:
7203 debug_sym
= g_strdup_printf ("%s_rgctx_fetch_%d", prefix
, acfg
->label_generator
++);
7205 case MONO_PATCH_INFO_ICALL_ADDR
:
7206 case MONO_PATCH_INFO_ICALL_ADDR_CALL
: {
7207 char *s
= get_debug_sym (ji
->data
.method
, "", cache
);
7209 debug_sym
= g_strdup_printf ("%s_icall_native_%s", prefix
, s
);
7213 case MONO_PATCH_INFO_SPECIFIC_TRAMPOLINE_LAZY_FETCH_ADDR
:
7214 debug_sym
= g_strdup_printf ("%s_jit_icall_native_specific_trampoline_lazy_fetch_%lu", prefix
, (gulong
)ji
->data
.uindex
);
7216 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
7217 debug_sym
= g_strdup_printf ("%s_jit_icall_native_%s", prefix
, mono_find_jit_icall_info (ji
->data
.jit_icall_id
)->name
);
7225 return sanitize_symbol (acfg
, debug_sym
);
7229 * Calls made from AOTed code are routed through a table of jumps similar to the
7230 * ELF PLT (Program Linkage Table). Initially the PLT entries jump to code which transfers
7231 * control to the AOT runtime through a trampoline.
7234 emit_plt (MonoAotCompile
*acfg
)
7238 if (acfg
->aot_opts
.llvm_only
) {
7239 g_assert (acfg
->plt_offset
== 1);
7245 emit_section_change (acfg
, ".text", 0);
7246 emit_alignment_code (acfg
, 16);
7247 emit_info_symbol (acfg
, "plt", TRUE
);
7248 emit_label (acfg
, acfg
->plt_symbol
);
7250 for (i
= 0; i
< acfg
->plt_offset
; ++i
) {
7251 char *debug_sym
= NULL
;
7252 MonoPltEntry
*plt_entry
= NULL
;
7256 * The first plt entry is unused.
7260 plt_entry
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
7262 debug_sym
= plt_entry
->debug_sym
;
7264 if (acfg
->thumb_mixed
&& !plt_entry
->jit_used
)
7265 /* Emit only a thumb version */
7268 /* Skip plt entries not actually called */
7269 if (!plt_entry
->jit_used
&& !plt_entry
->llvm_used
)
7272 if (acfg
->llvm
&& !acfg
->thumb_mixed
) {
7273 emit_label (acfg
, plt_entry
->llvm_symbol
);
7275 emit_global_inner (acfg
, plt_entry
->llvm_symbol
, TRUE
);
7276 #if defined(TARGET_MACH)
7277 fprintf (acfg
->fp
, ".private_extern %s\n", plt_entry
->llvm_symbol
);
7283 if (acfg
->need_no_dead_strip
) {
7284 emit_unset_mode (acfg
);
7285 fprintf (acfg
->fp
, " .no_dead_strip %s\n", debug_sym
);
7287 emit_local_symbol (acfg
, debug_sym
, NULL
, TRUE
);
7288 emit_label (acfg
, debug_sym
);
7291 emit_label (acfg
, plt_entry
->symbol
);
7293 arch_emit_plt_entry (acfg
, acfg
->got_symbol
, (acfg
->plt_got_offset_base
+ i
) * sizeof (target_mgreg_t
), acfg
->plt_got_info_offsets
[i
]);
7296 emit_symbol_size (acfg
, debug_sym
, ".");
7299 if (acfg
->thumb_mixed
) {
7300 /* Make sure the ARM symbols don't alias the thumb ones */
7301 emit_zero_bytes (acfg
, 16);
7304 * Emit a separate set of PLT entries using thumb2 which is called by LLVM generated
7307 for (i
= 0; i
< acfg
->plt_offset
; ++i
) {
7308 char *debug_sym
= NULL
;
7309 MonoPltEntry
*plt_entry
= NULL
;
7314 plt_entry
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
7316 /* Skip plt entries not actually called by LLVM code */
7317 if (!plt_entry
->llvm_used
)
7320 if (acfg
->aot_opts
.write_symbols
) {
7321 if (plt_entry
->debug_sym
)
7322 debug_sym
= g_strdup_printf ("%s_thumb", plt_entry
->debug_sym
);
7326 #if defined(TARGET_MACH)
7327 fprintf (acfg
->fp
, " .thumb_func %s\n", debug_sym
);
7328 fprintf (acfg
->fp
, " .no_dead_strip %s\n", debug_sym
);
7330 emit_local_symbol (acfg
, debug_sym
, NULL
, TRUE
);
7331 emit_label (acfg
, debug_sym
);
7333 fprintf (acfg
->fp
, "\n.thumb_func\n");
7335 emit_label (acfg
, plt_entry
->llvm_symbol
);
7338 emit_global_inner (acfg
, plt_entry
->llvm_symbol
, TRUE
);
7340 arch_emit_llvm_plt_entry (acfg
, acfg
->got_symbol
, (acfg
->plt_got_offset_base
+ i
) * sizeof (target_mgreg_t
), acfg
->plt_got_info_offsets
[i
]);
7343 emit_symbol_size (acfg
, debug_sym
, ".");
7349 emit_symbol_size (acfg
, acfg
->plt_symbol
, ".");
7351 emit_info_symbol (acfg
, "plt_end", TRUE
);
7353 arch_emit_unwind_info_sections (acfg
, "plt", "plt_end", NULL
);
7357 * emit_trampoline_full:
7359 * If EMIT_TINFO is TRUE, emit additional information which can be used to create a MonoJitInfo for this trampoline by
7360 * create_jit_info_for_trampoline ().
7362 static G_GNUC_UNUSED
void
7363 emit_trampoline_full (MonoAotCompile
*acfg
, MonoTrampInfo
*info
, gboolean emit_tinfo
)
7365 char start_symbol
[MAX_SYMBOL_SIZE
];
7366 char end_symbol
[MAX_SYMBOL_SIZE
];
7367 char symbol
[MAX_SYMBOL_SIZE
];
7368 guint32 buf_size
, info_offset
;
7369 MonoJumpInfo
*patch_info
;
7382 code_size
= info
->code_size
;
7384 unwind_ops
= info
->unwind_ops
;
7388 sprintf (start_symbol
, "%s%s", acfg
->user_symbol_prefix
, name
);
7390 emit_section_change (acfg
, ".text", 0);
7391 emit_global (acfg
, start_symbol
, TRUE
);
7392 emit_alignment_code (acfg
, AOT_FUNC_ALIGNMENT
);
7393 emit_label (acfg
, start_symbol
);
7395 sprintf (symbol
, "%snamed_%s", acfg
->temp_prefix
, name
);
7396 emit_label (acfg
, symbol
);
7399 * The code should access everything through the GOT, so we pass
7402 emit_and_reloc_code (acfg
, NULL
, code
, code_size
, ji
, TRUE
, NULL
);
7404 emit_symbol_size (acfg
, start_symbol
, ".");
7407 sprintf (end_symbol
, "%snamede_%s", acfg
->temp_prefix
, name
);
7408 emit_label (acfg
, end_symbol
);
7413 /* Sort relocations */
7414 patches
= g_ptr_array_new ();
7415 for (patch_info
= ji
; patch_info
; patch_info
= patch_info
->next
)
7416 if (patch_info
->type
!= MONO_PATCH_INFO_NONE
)
7417 g_ptr_array_add (patches
, patch_info
);
7418 g_ptr_array_sort (patches
, compare_patches
);
7420 buf_size
= patches
->len
* 128 + 128;
7421 buf
= (guint8
*)g_malloc (buf_size
);
7424 encode_patch_list (acfg
, patches
, patches
->len
, FALSE
, p
, &p
);
7425 g_assert (p
- buf
< buf_size
);
7426 g_ptr_array_free (patches
, TRUE
);
7428 sprintf (symbol
, "%s%s_p", acfg
->user_symbol_prefix
, name
);
7430 info_offset
= add_to_blob (acfg
, buf
, p
- buf
);
7432 emit_section_change (acfg
, RODATA_SECT
, 0);
7433 emit_global (acfg
, symbol
, FALSE
);
7434 emit_label (acfg
, symbol
);
7436 emit_int32 (acfg
, info_offset
);
7440 guint32 encoded_len
;
7444 * Emit additional information which can be used to reconstruct a partial MonoTrampInfo.
7446 encoded
= mono_unwind_ops_encode (info
->unwind_ops
, &encoded_len
);
7447 uw_offset
= get_unwind_info_offset (acfg
, encoded
, encoded_len
);
7450 emit_symbol_diff (acfg
, end_symbol
, start_symbol
, 0);
7451 emit_int32 (acfg
, uw_offset
);
7454 /* Emit debug info */
7456 char symbol2
[MAX_SYMBOL_SIZE
];
7458 sprintf (symbol
, "%s", name
);
7459 sprintf (symbol2
, "%snamed_%s", acfg
->temp_prefix
, name
);
7461 arch_emit_unwind_info_sections (acfg
, start_symbol
, end_symbol
, unwind_ops
);
7464 mono_dwarf_writer_emit_trampoline (acfg
->dwarf
, symbol
, symbol2
, NULL
, NULL
, code_size
, unwind_ops
);
7470 static G_GNUC_UNUSED
void
7471 emit_trampoline (MonoAotCompile
*acfg
, MonoTrampInfo
*info
)
7473 emit_trampoline_full (acfg
, info
, TRUE
);
7477 emit_trampolines (MonoAotCompile
*acfg
)
7479 char symbol
[MAX_SYMBOL_SIZE
];
7480 char end_symbol
[MAX_SYMBOL_SIZE
];
7481 int i
, tramp_got_offset
;
7483 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
7487 if (!mono_aot_mode_is_full (&acfg
->aot_opts
) && !mono_aot_mode_is_interp (&acfg
->aot_opts
))
7489 if (acfg
->aot_opts
.llvm_only
)
7492 g_assert (acfg
->image
->assembly
);
7494 /* Currently, we emit most trampolines into the mscorlib AOT image. */
7495 if (mono_is_corlib_image(acfg
->image
->assembly
->image
)) {
7496 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
7497 MonoTrampInfo
*info
;
7500 * Emit the generic trampolines.
7502 * We could save some code by treating the generic trampolines as a wrapper
7503 * method, but that approach has its own complexities, so we choose the simpler
7506 for (tramp_type
= 0; tramp_type
< MONO_TRAMPOLINE_NUM
; ++tramp_type
) {
7507 /* we overload the boolean here to indicate the slightly different trampoline needed, see mono_arch_create_generic_trampoline() */
7508 #ifdef DISABLE_REMOTING
7509 if (tramp_type
== MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
)
7512 mono_arch_create_generic_trampoline ((MonoTrampolineType
)tramp_type
, &info
, acfg
->aot_opts
.use_trampolines_page
? 2: TRUE
);
7513 emit_trampoline (acfg
, info
);
7514 mono_tramp_info_free (info
);
7517 /* Emit the exception related code pieces */
7518 mono_arch_get_restore_context (&info
, TRUE
);
7519 emit_trampoline (acfg
, info
);
7520 mono_tramp_info_free (info
);
7522 mono_arch_get_call_filter (&info
, TRUE
);
7523 emit_trampoline (acfg
, info
);
7524 mono_tramp_info_free (info
);
7526 mono_arch_get_throw_exception (&info
, TRUE
);
7527 emit_trampoline (acfg
, info
);
7528 mono_tramp_info_free (info
);
7530 mono_arch_get_rethrow_exception (&info
, TRUE
);
7531 emit_trampoline (acfg
, info
);
7532 mono_tramp_info_free (info
);
7534 mono_arch_get_rethrow_preserve_exception (&info
, TRUE
);
7535 emit_trampoline (acfg
, info
);
7536 mono_tramp_info_free (info
);
7538 mono_arch_get_throw_corlib_exception (&info
, TRUE
);
7539 emit_trampoline (acfg
, info
);
7540 mono_tramp_info_free (info
);
7542 #ifdef MONO_ARCH_HAVE_SDB_TRAMPOLINES
7543 mono_arch_create_sdb_trampoline (TRUE
, &info
, TRUE
);
7544 emit_trampoline (acfg
, info
);
7545 mono_tramp_info_free (info
);
7547 mono_arch_create_sdb_trampoline (FALSE
, &info
, TRUE
);
7548 emit_trampoline (acfg
, info
);
7549 mono_tramp_info_free (info
);
7552 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
7553 mono_arch_get_gsharedvt_trampoline (&info
, TRUE
);
7555 emit_trampoline_full (acfg
, info
, TRUE
);
7557 /* Create a separate out trampoline for more information in stack traces */
7558 info
->name
= g_strdup ("gsharedvt_out_trampoline");
7559 emit_trampoline_full (acfg
, info
, TRUE
);
7560 mono_tramp_info_free (info
);
7564 #if defined(MONO_ARCH_HAVE_GET_TRAMPOLINES)
7566 GSList
*l
= mono_arch_get_trampolines (TRUE
);
7569 MonoTrampInfo
*info
= (MonoTrampInfo
*)l
->data
;
7571 emit_trampoline (acfg
, info
);
7577 for (i
= 0; i
< acfg
->aot_opts
.nrgctx_fetch_trampolines
; ++i
) {
7580 offset
= MONO_RGCTX_SLOT_MAKE_RGCTX (i
);
7581 mono_arch_create_rgctx_lazy_fetch_trampoline (offset
, &info
, TRUE
);
7582 emit_trampoline (acfg
, info
);
7583 mono_tramp_info_free (info
);
7585 offset
= MONO_RGCTX_SLOT_MAKE_MRGCTX (i
);
7586 mono_arch_create_rgctx_lazy_fetch_trampoline (offset
, &info
, TRUE
);
7587 emit_trampoline (acfg
, info
);
7588 mono_tramp_info_free (info
);
7591 #ifdef MONO_ARCH_HAVE_GENERAL_RGCTX_LAZY_FETCH_TRAMPOLINE
7592 mono_arch_create_general_rgctx_lazy_fetch_trampoline (&info
, TRUE
);
7593 emit_trampoline (acfg
, info
);
7594 mono_tramp_info_free (info
);
7600 /* delegate_invoke_impl trampolines */
7601 l
= mono_arch_get_delegate_invoke_impls ();
7603 MonoTrampInfo
*info
= (MonoTrampInfo
*)l
->data
;
7605 emit_trampoline (acfg
, info
);
7610 if (mono_aot_mode_is_interp (&acfg
->aot_opts
) && mono_is_corlib_image (acfg
->image
->assembly
->image
)) {
7611 mono_arch_get_interp_to_native_trampoline (&info
);
7612 emit_trampoline (acfg
, info
);
7614 #ifdef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
7615 mono_arch_get_native_to_interp_trampoline (&info
);
7616 emit_trampoline (acfg
, info
);
7620 #endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
7622 /* Emit trampolines which are numerous */
7625 * These include the following:
7626 * - specific trampolines
7627 * - static rgctx invoke trampolines
7629 * These trampolines have the same code, they are parameterized by GOT
7631 * They are defined in this file, in the arch_... routines instead of
7632 * in tramp-<ARCH>.c, since it is easier to do it this way.
7636 * When running in aot-only mode, we can't create specific trampolines at
7637 * runtime, so we create a few, and save them in the AOT file.
7638 * Normal trampolines embed their argument as a literal inside the
7639 * trampoline code, we can't do that here, so instead we embed an offset
7640 * which needs to be added to the trampoline address to get the address of
7641 * the GOT slot which contains the argument value.
7642 * The generated trampolines jump to the generic trampolines using another
7643 * GOT slot, which will be setup by the AOT loader to point to the
7644 * generic trampoline code of the given type.
7648 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for
7652 emit_section_change (acfg
, ".text", 0);
7654 tramp_got_offset
= acfg
->got_offset
;
7656 for (ntype
= 0; ntype
< MONO_AOT_TRAMP_NUM
; ++ntype
) {
7658 case MONO_AOT_TRAMP_SPECIFIC
:
7659 sprintf (symbol
, "specific_trampolines");
7661 case MONO_AOT_TRAMP_STATIC_RGCTX
:
7662 sprintf (symbol
, "static_rgctx_trampolines");
7664 case MONO_AOT_TRAMP_IMT
:
7665 sprintf (symbol
, "imt_trampolines");
7667 case MONO_AOT_TRAMP_GSHAREDVT_ARG
:
7668 sprintf (symbol
, "gsharedvt_arg_trampolines");
7670 case MONO_AOT_TRAMP_FTNPTR_ARG
:
7671 sprintf (symbol
, "ftnptr_arg_trampolines");
7673 case MONO_AOT_TRAMP_UNBOX_ARBITRARY
:
7674 sprintf (symbol
, "unbox_arbitrary_trampolines");
7677 g_assert_not_reached ();
7680 sprintf (end_symbol
, "%s_e", symbol
);
7682 if (acfg
->aot_opts
.write_symbols
)
7683 emit_local_symbol (acfg
, symbol
, end_symbol
, TRUE
);
7685 emit_alignment_code (acfg
, AOT_FUNC_ALIGNMENT
);
7686 emit_info_symbol (acfg
, symbol
, TRUE
);
7688 acfg
->trampoline_got_offset_base
[ntype
] = tramp_got_offset
;
7690 for (i
= 0; i
< acfg
->num_trampolines
[ntype
]; ++i
) {
7694 case MONO_AOT_TRAMP_SPECIFIC
:
7695 arch_emit_specific_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7696 tramp_got_offset
+= 2;
7698 case MONO_AOT_TRAMP_STATIC_RGCTX
:
7699 arch_emit_static_rgctx_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7700 tramp_got_offset
+= 2;
7702 case MONO_AOT_TRAMP_IMT
:
7703 arch_emit_imt_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7704 tramp_got_offset
+= 1;
7706 case MONO_AOT_TRAMP_GSHAREDVT_ARG
:
7707 arch_emit_gsharedvt_arg_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7708 tramp_got_offset
+= 2;
7710 case MONO_AOT_TRAMP_FTNPTR_ARG
:
7711 arch_emit_ftnptr_arg_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7712 tramp_got_offset
+= 2;
7714 case MONO_AOT_TRAMP_UNBOX_ARBITRARY
:
7715 arch_emit_unbox_arbitrary_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7716 tramp_got_offset
+= 1;
7719 g_assert_not_reached ();
7721 if (!acfg
->trampoline_size
[ntype
]) {
7722 g_assert (tramp_size
);
7723 acfg
->trampoline_size
[ntype
] = tramp_size
;
7727 emit_label (acfg
, end_symbol
);
7728 emit_int32 (acfg
, 0);
7731 arch_emit_specific_trampoline_pages (acfg
);
7733 /* Reserve some entries at the end of the GOT for our use */
7734 acfg
->num_trampoline_got_entries
= tramp_got_offset
- acfg
->got_offset
;
7737 acfg
->got_offset
+= acfg
->num_trampoline_got_entries
;
7741 str_begins_with (const char *str1
, const char *str2
)
7743 int len
= strlen (str2
);
7744 return strncmp (str1
, str2
, len
) == 0;
7748 mono_aot_readonly_field_override (MonoClassField
*field
)
7751 for (rdv
= readonly_values
; rdv
; rdv
= rdv
->next
) {
7752 char *p
= rdv
->name
;
7754 len
= strlen (m_class_get_name_space (field
->parent
));
7755 if (strncmp (p
, m_class_get_name_space (field
->parent
), len
))
7760 len
= strlen (m_class_get_name (field
->parent
));
7761 if (strncmp (p
, m_class_get_name (field
->parent
), len
))
7766 if (strcmp (p
, field
->name
))
7768 switch (rdv
->type
) {
7770 return &rdv
->value
.i1
;
7772 return &rdv
->value
.i2
;
7774 return &rdv
->value
.i4
;
7783 add_readonly_value (MonoAotOptions
*opts
, const char *val
)
7788 /* the format of val is:
7789 * namespace.typename.fieldname=type/value
7790 * type can be i1 for uint8/int8/boolean, i2 for uint16/int16/char, i4 for uint32/int32
7792 fval
= strrchr (val
, '/');
7794 fprintf (stderr
, "AOT : invalid format for readonly field '%s', missing /.\n", val
);
7797 tval
= strrchr (val
, '=');
7799 fprintf (stderr
, "AOT : invalid format for readonly field '%s', missing =.\n", val
);
7802 rdv
= g_new0 (ReadOnlyValue
, 1);
7803 rdv
->name
= (char *)g_malloc0 (tval
- val
+ 1);
7804 memcpy (rdv
->name
, val
, tval
- val
);
7807 if (strncmp (tval
, "i1", 2) == 0) {
7808 rdv
->value
.i1
= atoi (fval
);
7809 rdv
->type
= MONO_TYPE_I1
;
7810 } else if (strncmp (tval
, "i2", 2) == 0) {
7811 rdv
->value
.i2
= atoi (fval
);
7812 rdv
->type
= MONO_TYPE_I2
;
7813 } else if (strncmp (tval
, "i4", 2) == 0) {
7814 rdv
->value
.i4
= atoi (fval
);
7815 rdv
->type
= MONO_TYPE_I4
;
7817 fprintf (stderr
, "AOT : unsupported type for readonly field '%s'.\n", tval
);
7820 rdv
->next
= readonly_values
;
7821 readonly_values
= rdv
;
7825 clean_path (gchar
* path
)
7830 if (g_str_has_suffix (path
, G_DIR_SEPARATOR_S
))
7833 gchar
*clean
= g_strconcat (path
, G_DIR_SEPARATOR_S
, (const char*)NULL
);
7839 static const gchar
*
7840 wrap_path (const gchar
* path
)
7846 // If the string contains no spaces, just return the original string.
7847 if (strstr (path
, " ") == NULL
)
7850 // If the string is already wrapped in quotes, return it.
7851 len
= strlen (path
);
7852 if (len
>= 2 && path
[0] == '\"' && path
[len
-1] == '\"')
7855 // If the string contains spaces, then wrap it in quotes.
7856 gchar
*clean
= g_strdup_printf ("\"%s\"", path
);
7861 // Duplicate a char range and add it to a ptrarray, but only if it is nonempty
7863 ptr_array_add_range_if_nonempty(GPtrArray
*args
, gchar
const *start
, gchar
const *end
)
7865 ptrdiff_t len
= end
-start
;
7867 g_ptr_array_add (args
, g_strndup (start
, len
));
7871 mono_aot_split_options (const char *aot_options
)
7873 enum MonoAotOptionState
{
7874 MONO_AOT_OPTION_STATE_DEFAULT
,
7875 MONO_AOT_OPTION_STATE_STRING
,
7876 MONO_AOT_OPTION_STATE_ESCAPE
,
7879 GPtrArray
*args
= g_ptr_array_new ();
7880 enum MonoAotOptionState state
= MONO_AOT_OPTION_STATE_DEFAULT
;
7881 gchar
const *opt_start
= aot_options
;
7882 gboolean end_of_string
= FALSE
;
7885 g_return_val_if_fail (aot_options
!= NULL
, NULL
);
7887 while ((cur
= *aot_options
) != '\0') {
7888 if (state
== MONO_AOT_OPTION_STATE_ESCAPE
)
7893 // If we find a quote, then if we're in the default case then
7894 // it means we've found the start of a string, if not then it
7895 // means we've found the end of the string and should switch
7896 // back to the default case.
7898 case MONO_AOT_OPTION_STATE_DEFAULT
:
7899 state
= MONO_AOT_OPTION_STATE_STRING
;
7901 case MONO_AOT_OPTION_STATE_STRING
:
7902 state
= MONO_AOT_OPTION_STATE_DEFAULT
;
7904 case MONO_AOT_OPTION_STATE_ESCAPE
:
7905 g_assert_not_reached ();
7910 // If we've found an escaping operator, then this means we
7911 // should not process the next character if inside a string.
7912 if (state
== MONO_AOT_OPTION_STATE_STRING
)
7913 state
= MONO_AOT_OPTION_STATE_ESCAPE
;
7916 // If we're in the default state then this means we've found
7917 // an option, store it for later processing.
7918 if (state
== MONO_AOT_OPTION_STATE_DEFAULT
)
7926 // If the next character is end of string, then process the last option.
7927 if (*(aot_options
) == '\0') {
7928 end_of_string
= TRUE
;
7934 ptr_array_add_range_if_nonempty (args
, opt_start
, aot_options
);
7935 opt_start
= ++aot_options
;
7938 goto restart
; // Check for null and continue loop
7945 parse_cpu_features (const gchar
*attr
)
7947 if (!attr
|| strlen (attr
) < 2) {
7948 fprintf (stderr
, "Invalid attribute");
7954 //-foo - disable foo
7955 gboolean enabled
= TRUE
;
7956 if (attr
[0] == '-')
7958 int prefix
= (attr
[0] == '-' || attr
[0] == '+') ? 1 : 0;
7959 MonoCPUFeatures feature
= (MonoCPUFeatures
) 0;
7961 #if defined(TARGET_X86) || defined(TARGET_AMD64)
7963 // `mattr=+sse3` = +sse,+sse2,+pclmul,+aes,+sse3
7964 // `mattr=-sse3` = -sse3,-ssse3,-sse4.1,-sse4.2,-popcnt,-avx,-avx2,-fma
7965 if (!strcmp (attr
+ prefix
, "sse"))
7966 feature
= MONO_CPU_X86_SSE_COMBINED
;
7967 else if (!strcmp (attr
+ prefix
, "sse2"))
7968 feature
= MONO_CPU_X86_SSE2_COMBINED
;
7969 else if (!strcmp (attr
+ prefix
, "sse3"))
7970 feature
= MONO_CPU_X86_SSE3_COMBINED
;
7971 else if (!strcmp (attr
+ prefix
, "ssse3"))
7972 feature
= MONO_CPU_X86_SSSE3_COMBINED
;
7973 else if (!strcmp (attr
+ prefix
, "sse4.1"))
7974 feature
= MONO_CPU_X86_SSE41_COMBINED
;
7975 else if (!strcmp (attr
+ prefix
, "sse4.2"))
7976 feature
= MONO_CPU_X86_SSE42_COMBINED
;
7977 else if (!strcmp (attr
+ prefix
, "avx"))
7978 feature
= MONO_CPU_X86_AVX_COMBINED
;
7979 else if (!strcmp (attr
+ prefix
, "avx2"))
7980 feature
= MONO_CPU_X86_AVX2_COMBINED
;
7981 else if (!strcmp (attr
+ prefix
, "pclmul"))
7982 feature
= MONO_CPU_X86_PCLMUL_COMBINED
;
7983 else if (!strcmp (attr
+ prefix
, "aes"))
7984 feature
= MONO_CPU_X86_AES_COMBINED
;
7985 else if (!strcmp (attr
+ prefix
, "popcnt"))
7986 feature
= MONO_CPU_X86_POPCNT_COMBINED
;
7987 else if (!strcmp (attr
+ prefix
, "fma"))
7988 feature
= MONO_CPU_X86_FMA_COMBINED
;
7989 // these are independent
7990 else if (!strcmp (attr
+ prefix
, "lzcnt")) // technically, it'a a part of BMI but only on Intel
7991 feature
= MONO_CPU_X86_LZCNT
;
7992 else if (!strcmp (attr
+ prefix
, "bmi")) // NOTE: it's not "bmi1"
7993 feature
= MONO_CPU_X86_BMI1
;
7994 else if (!strcmp (attr
+ prefix
, "bmi2"))
7995 feature
= MONO_CPU_X86_BMI2
; // BMI2 doesn't imply BMI1
7997 // we don't have a flag for it but it's probably recognized by opt/llc so let's don't fire an error here
7998 // printf ("Unknown cpu feature: %s\n", attr);
8001 // if we disable a feature from the SSE-AVX tree we also need to disable all dependencies
8002 if (!enabled
&& (feature
& MONO_CPU_X86_FULL_SSEAVX_COMBINED
))
8003 feature
= (MonoCPUFeatures
) (MONO_CPU_X86_FULL_SSEAVX_COMBINED
& ~feature
);
8005 #elif defined(TARGET_ARM64)
8006 // TODO: neon, sha1, sha2, asimd, etc...
8007 #elif defined(TARGET_WASM)
8008 if (!strcmp (attr
+ prefix
, "simd"))
8009 feature
= MONO_CPU_WASM_SIMD
;
8013 mono_cpu_features_enabled
= (MonoCPUFeatures
) (mono_cpu_features_enabled
| feature
);
8015 mono_cpu_features_disabled
= (MonoCPUFeatures
) (mono_cpu_features_disabled
| feature
);
8021 mono_aot_parse_options (const char *aot_options
, MonoAotOptions
*opts
)
8025 args
= mono_aot_split_options (aot_options
? aot_options
: "");
8026 for (int i
= 0; i
< args
->len
; ++i
) {
8027 const char *arg
= (const char *)g_ptr_array_index (args
, i
);
8029 if (str_begins_with (arg
, "outfile=")) {
8030 opts
->outfile
= g_strdup (arg
+ strlen ("outfile="));
8031 } else if (str_begins_with (arg
, "llvm-outfile=")) {
8032 opts
->llvm_outfile
= g_strdup (arg
+ strlen ("llvm-outfile="));
8033 } else if (str_begins_with (arg
, "temp-path=")) {
8034 opts
->temp_path
= clean_path (g_strdup (arg
+ strlen ("temp-path=")));
8035 } else if (str_begins_with (arg
, "save-temps")) {
8036 opts
->save_temps
= TRUE
;
8037 } else if (str_begins_with (arg
, "keep-temps")) {
8038 opts
->save_temps
= TRUE
;
8039 } else if (str_begins_with (arg
, "write-symbols")) {
8040 opts
->write_symbols
= TRUE
;
8041 } else if (str_begins_with (arg
, "no-write-symbols")) {
8042 opts
->write_symbols
= FALSE
;
8043 // Intentionally undocumented -- one-off experiment
8044 } else if (str_begins_with (arg
, "metadata-only")) {
8045 opts
->metadata_only
= TRUE
;
8046 } else if (str_begins_with (arg
, "bind-to-runtime-version")) {
8047 opts
->bind_to_runtime_version
= TRUE
;
8048 } else if (str_begins_with (arg
, "full")) {
8049 opts
->mode
= MONO_AOT_MODE_FULL
;
8050 } else if (str_begins_with (arg
, "hybrid")) {
8051 opts
->mode
= MONO_AOT_MODE_HYBRID
;
8052 } else if (str_begins_with (arg
, "interp")) {
8053 opts
->interp
= TRUE
;
8054 } else if (str_begins_with (arg
, "threads=")) {
8055 opts
->nthreads
= atoi (arg
+ strlen ("threads="));
8056 } else if (str_begins_with (arg
, "static")) {
8057 opts
->static_link
= TRUE
;
8058 opts
->no_dlsym
= TRUE
;
8059 } else if (str_begins_with (arg
, "asmonly")) {
8060 opts
->asm_only
= TRUE
;
8061 } else if (str_begins_with (arg
, "asmwriter")) {
8062 opts
->asm_writer
= TRUE
;
8063 } else if (str_begins_with (arg
, "nodebug")) {
8064 opts
->nodebug
= TRUE
;
8065 } else if (str_begins_with (arg
, "dwarfdebug")) {
8066 opts
->dwarf_debug
= TRUE
;
8067 // Intentionally undocumented -- No one remembers what this does. It appears to be ARM-only
8068 } else if (str_begins_with (arg
, "nopagetrampolines")) {
8069 opts
->use_trampolines_page
= FALSE
;
8070 } else if (str_begins_with (arg
, "ntrampolines=")) {
8071 opts
->ntrampolines
= atoi (arg
+ strlen ("ntrampolines="));
8072 } else if (str_begins_with (arg
, "nrgctx-trampolines=")) {
8073 opts
->nrgctx_trampolines
= atoi (arg
+ strlen ("nrgctx-trampolines="));
8074 } else if (str_begins_with (arg
, "nrgctx-fetch-trampolines=")) {
8075 opts
->nrgctx_fetch_trampolines
= atoi (arg
+ strlen ("nrgctx-fetch-trampolines="));
8076 } else if (str_begins_with (arg
, "nimt-trampolines=")) {
8077 opts
->nimt_trampolines
= atoi (arg
+ strlen ("nimt-trampolines="));
8078 } else if (str_begins_with (arg
, "ngsharedvt-trampolines=")) {
8079 opts
->ngsharedvt_arg_trampolines
= atoi (arg
+ strlen ("ngsharedvt-trampolines="));
8080 } else if (str_begins_with (arg
, "nftnptr-arg-trampolines=")) {
8081 opts
->nftnptr_arg_trampolines
= atoi (arg
+ strlen ("nftnptr-arg-trampolines="));
8082 } else if (str_begins_with (arg
, "nunbox-arbitrary-trampolines=")) {
8083 opts
->nunbox_arbitrary_trampolines
= atoi (arg
+ strlen ("unbox-arbitrary-trampolines="));
8084 } else if (str_begins_with (arg
, "tool-prefix=")) {
8085 opts
->tool_prefix
= g_strdup (arg
+ strlen ("tool-prefix="));
8086 } else if (str_begins_with (arg
, "ld-flags=")) {
8087 opts
->ld_flags
= g_strdup (arg
+ strlen ("ld-flags="));
8088 } else if (str_begins_with (arg
, "soft-debug")) {
8089 opts
->soft_debug
= TRUE
;
8090 // Intentionally undocumented x2-- deprecated
8091 } else if (str_begins_with (arg
, "gen-seq-points-file=")) {
8092 fprintf (stderr
, "Mono Warning: aot option gen-seq-points-file= is deprecated.\n");
8093 } else if (str_begins_with (arg
, "gen-seq-points-file")) {
8094 fprintf (stderr
, "Mono Warning: aot option gen-seq-points-file is deprecated.\n");
8095 } else if (str_begins_with (arg
, "msym-dir=")) {
8096 mini_debug_options
.no_seq_points_compact_data
= FALSE
;
8097 opts
->gen_msym_dir
= TRUE
;
8098 opts
->gen_msym_dir_path
= g_strdup (arg
+ strlen ("msym_dir="));
8099 } else if (str_begins_with (arg
, "direct-pinvoke")) {
8100 opts
->direct_pinvoke
= TRUE
;
8101 } else if (str_begins_with (arg
, "direct-icalls")) {
8102 opts
->direct_icalls
= TRUE
;
8103 } else if (str_begins_with (arg
, "no-direct-calls")) {
8104 opts
->no_direct_calls
= TRUE
;
8105 } else if (str_begins_with (arg
, "print-skipped")) {
8106 opts
->print_skipped_methods
= TRUE
;
8107 } else if (str_begins_with (arg
, "stats")) {
8109 // Intentionally undocumented-- has no known function other than to debug the compiler
8110 } else if (str_begins_with (arg
, "no-instances")) {
8111 opts
->no_instances
= TRUE
;
8112 // Intentionally undocumented x4-- Used for internal debugging of compiler
8113 } else if (str_begins_with (arg
, "log-generics")) {
8114 opts
->log_generics
= TRUE
;
8115 } else if (str_begins_with (arg
, "log-instances=")) {
8116 opts
->log_instances
= TRUE
;
8117 opts
->instances_logfile_path
= g_strdup (arg
+ strlen ("log-instances="));
8118 } else if (str_begins_with (arg
, "log-instances")) {
8119 opts
->log_instances
= TRUE
;
8120 } else if (str_begins_with (arg
, "internal-logfile=")) {
8121 opts
->logfile
= g_strdup (arg
+ strlen ("internal-logfile="));
8122 } else if (str_begins_with (arg
, "dedup-skip")) {
8124 } else if (str_begins_with (arg
, "dedup-include=")) {
8125 opts
->dedup_include
= g_strdup (arg
+ strlen ("dedup-include="));
8126 } else if (str_begins_with (arg
, "mtriple=")) {
8127 opts
->mtriple
= g_strdup (arg
+ strlen ("mtriple="));
8128 } else if (str_begins_with (arg
, "llvm-path=")) {
8129 opts
->llvm_path
= clean_path (g_strdup (arg
+ strlen ("llvm-path=")));
8130 } else if (!strcmp (arg
, "try-llvm")) {
8131 // If we can load LLVM, use it
8132 // Note: if you call this function from anywhere but mono_compile_assembly,
8133 // this will only set the try_llvm attribute and not do the probing / set the
8135 opts
->try_llvm
= TRUE
;
8136 } else if (!strcmp (arg
, "llvm")) {
8138 } else if (str_begins_with (arg
, "readonly-value=")) {
8139 add_readonly_value (opts
, arg
+ strlen ("readonly-value="));
8140 } else if (str_begins_with (arg
, "info")) {
8141 printf ("AOT target setup: %s.\n", AOT_TARGET_STR
);
8143 // Intentionally undocumented: Used for precise stack maps, which are not available yet
8144 } else if (str_begins_with (arg
, "gc-maps")) {
8145 mini_gc_enable_gc_maps_for_aot ();
8146 // Intentionally undocumented: Used for internal debugging
8147 } else if (str_begins_with (arg
, "dump")) {
8148 opts
->dump_json
= TRUE
;
8149 } else if (str_begins_with (arg
, "llvmonly")) {
8150 opts
->mode
= MONO_AOT_MODE_FULL
;
8152 opts
->llvm_only
= TRUE
;
8153 } else if (str_begins_with (arg
, "data-outfile=")) {
8154 opts
->data_outfile
= g_strdup (arg
+ strlen ("data-outfile="));
8155 } else if (str_begins_with (arg
, "profile=")) {
8156 opts
->profile_files
= g_list_append (opts
->profile_files
, g_strdup (arg
+ strlen ("profile=")));
8157 } else if (!strcmp (arg
, "profile-only")) {
8158 opts
->profile_only
= TRUE
;
8159 } else if (!strcmp (arg
, "verbose")) {
8160 opts
->verbose
= TRUE
;
8161 } else if (str_begins_with (arg
, "llvmopts=")){
8162 if (opts
->llvm_opts
) {
8163 char *s
= g_strdup_printf ("%s %s", opts
->llvm_opts
, arg
+ strlen ("llvmopts="));
8164 g_free (opts
->llvm_opts
);
8165 opts
->llvm_opts
= s
;
8167 opts
->llvm_opts
= g_strdup (arg
+ strlen ("llvmopts="));
8169 } else if (str_begins_with (arg
, "llvmllc=")){
8170 opts
->llvm_llc
= g_strdup (arg
+ strlen ("llvmllc="));
8171 } else if (!strcmp (arg
, "deterministic")) {
8172 opts
->deterministic
= TRUE
;
8173 } else if (!strcmp (arg
, "no-opt")) {
8174 opts
->no_opt
= TRUE
;
8175 } else if (str_begins_with (arg
, "clangxx=")) {
8176 opts
->clangxx
= g_strdup (arg
+ strlen ("clangxx="));
8177 } else if (str_begins_with (arg
, "mcpu=")) {
8178 if (!strcmp(arg
, "mcpu=native")) {
8179 opts
->use_current_cpu
= TRUE
;
8180 } else if (!strcmp(arg
, "mcpu=generic")) {
8181 opts
->use_current_cpu
= FALSE
;
8183 printf ("mcpu can only be 'native' or 'generic' (default).\n");
8186 } else if (str_begins_with (arg
, "mattr=")) {
8187 gchar
* attr
= g_strdup (arg
+ strlen ("mattr="));
8188 if (!parse_cpu_features (attr
))
8190 // mattr can be declared more than once, e.g.
8191 // `mattr=avx2,mattr=lzcnt,mattr=bmi2`
8192 if (!opts
->llvm_cpu_attr
)
8193 opts
->llvm_cpu_attr
= attr
;
8195 char* old_attrs
= opts
->llvm_cpu_attr
;
8196 opts
->llvm_cpu_attr
= g_strdup_printf ("%s,%s", opts
->llvm_cpu_attr
, attr
);
8199 } else if (str_begins_with (arg
, "depfile=")) {
8200 opts
->depfile
= g_strdup (arg
+ strlen ("depfile="));
8201 } else if (str_begins_with (arg
, "help") || str_begins_with (arg
, "?")) {
8202 printf ("Supported options for --aot:\n");
8203 printf (" asmonly\n");
8204 printf (" bind-to-runtime-version\n");
8205 printf (" bitcode\n");
8206 printf (" data-outfile=\n");
8207 printf (" direct-icalls\n");
8208 printf (" direct-pinvoke\n");
8209 printf (" dwarfdebug\n");
8211 printf (" hybrid\n");
8213 printf (" keep-temps\n");
8215 printf (" llvmonly\n");
8216 printf (" llvm-outfile=\n");
8217 printf (" llvm-path=\n");
8218 printf (" msym-dir=\n");
8219 printf (" mtriple\n");
8220 printf (" nimt-trampolines=\n");
8221 printf (" nodebug\n");
8222 printf (" no-direct-calls\n");
8223 printf (" no-write-symbols\n");
8224 printf (" nrgctx-trampolines=\n");
8225 printf (" nrgctx-fetch-trampolines=\n");
8226 printf (" ngsharedvt-trampolines=\n");
8227 printf (" nftnptr-arg-trampolines=\n");
8228 printf (" nunbox-arbitrary-trampolines=\n");
8229 printf (" ntrampolines=\n");
8230 printf (" outfile=\n");
8231 printf (" profile=\n");
8232 printf (" profile-only\n");
8233 printf (" print-skipped-methods\n");
8234 printf (" readonly-value=\n");
8235 printf (" save-temps\n");
8236 printf (" soft-debug\n");
8237 printf (" static\n");
8238 printf (" stats\n");
8239 printf (" temp-path=\n");
8240 printf (" tool-prefix=\n");
8241 printf (" threads=\n");
8242 printf (" write-symbols\n");
8243 printf (" verbose\n");
8244 printf (" no-opt\n");
8245 printf (" llvmopts=\n");
8246 printf (" llvmllc=\n");
8247 printf (" clangxx=\n");
8248 printf (" depfile=\n");
8249 printf (" mcpu=\n");
8250 printf (" mattr=\n");
8251 printf (" help/?\n");
8254 fprintf (stderr
, "AOT : Unknown argument '%s'.\n", arg
);
8258 g_free ((gpointer
) arg
);
8261 if (opts
->use_trampolines_page
) {
8262 opts
->ntrampolines
= 0;
8263 opts
->nrgctx_trampolines
= 0;
8264 opts
->nimt_trampolines
= 0;
8265 opts
->ngsharedvt_arg_trampolines
= 0;
8266 opts
->nftnptr_arg_trampolines
= 0;
8267 opts
->nunbox_arbitrary_trampolines
= 0;
8270 g_ptr_array_free (args
, /*free_seg=*/TRUE
);
8274 add_token_info_hash (gpointer key
, gpointer value
, gpointer user_data
)
8276 MonoMethod
*method
= (MonoMethod
*)key
;
8277 MonoJumpInfoToken
*ji
= (MonoJumpInfoToken
*)value
;
8278 MonoAotCompile
*acfg
= (MonoAotCompile
*)user_data
;
8279 MonoJumpInfoToken
*new_ji
;
8281 new_ji
= (MonoJumpInfoToken
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfoToken
));
8282 new_ji
->image
= ji
->image
;
8283 new_ji
->token
= ji
->token
;
8284 g_hash_table_insert (acfg
->token_info_hash
, method
, new_ji
);
8288 can_encode_class (MonoAotCompile
*acfg
, MonoClass
*klass
)
8290 if (m_class_get_type_token (klass
))
8292 if ((m_class_get_byval_arg (klass
)->type
== MONO_TYPE_VAR
) || (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_MVAR
) || (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_PTR
))
8294 if (m_class_get_rank (klass
))
8295 return can_encode_class (acfg
, m_class_get_element_class (klass
));
8300 can_encode_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
8302 if (method
->wrapper_type
) {
8303 switch (method
->wrapper_type
) {
8304 case MONO_WRAPPER_NONE
:
8305 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
8306 case MONO_WRAPPER_XDOMAIN_INVOKE
:
8307 case MONO_WRAPPER_STFLD
:
8308 case MONO_WRAPPER_LDFLD
:
8309 case MONO_WRAPPER_LDFLDA
:
8310 case MONO_WRAPPER_STELEMREF
:
8311 case MONO_WRAPPER_PROXY_ISINST
:
8312 case MONO_WRAPPER_ALLOC
:
8313 case MONO_WRAPPER_REMOTING_INVOKE
:
8314 case MONO_WRAPPER_OTHER
:
8315 case MONO_WRAPPER_WRITE_BARRIER
:
8316 case MONO_WRAPPER_DELEGATE_INVOKE
:
8317 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
8318 case MONO_WRAPPER_DELEGATE_END_INVOKE
:
8319 case MONO_WRAPPER_SYNCHRONIZED
:
8320 case MONO_WRAPPER_MANAGED_TO_NATIVE
:
8322 case MONO_WRAPPER_MANAGED_TO_MANAGED
:
8323 case MONO_WRAPPER_CASTCLASS
: {
8324 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
8333 //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
8337 if (!method
->token
) {
8338 /* The method is part of a constructed type like Int[,].Set (). */
8339 if (!g_hash_table_lookup (acfg
->token_info_hash
, method
)) {
8340 if (m_class_get_rank (method
->klass
))
8350 can_encode_patch (MonoAotCompile
*acfg
, MonoJumpInfo
*patch_info
)
8352 switch (patch_info
->type
) {
8353 case MONO_PATCH_INFO_METHOD
:
8354 case MONO_PATCH_INFO_METHOD_FTNDESC
:
8355 case MONO_PATCH_INFO_METHODCONST
:
8356 case MONO_PATCH_INFO_METHOD_CODE_SLOT
: {
8357 MonoMethod
*method
= patch_info
->data
.method
;
8359 return can_encode_method (acfg
, method
);
8361 case MONO_PATCH_INFO_VTABLE
:
8362 case MONO_PATCH_INFO_CLASS
:
8363 case MONO_PATCH_INFO_IID
:
8364 case MONO_PATCH_INFO_ADJUSTED_IID
:
8365 if (!can_encode_class (acfg
, patch_info
->data
.klass
)) {
8366 //printf ("Skip: %s\n", mono_type_full_name (m_class_get_byval_arg (patch_info->data.klass)));
8370 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
: {
8371 if (!can_encode_class (acfg
, patch_info
->data
.del_tramp
->klass
)) {
8372 //printf ("Skip: %s\n", mono_type_full_name (m_class_get_byval_arg (patch_info->data.klass)));
8377 case MONO_PATCH_INFO_RGCTX_FETCH
:
8378 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
8379 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
8381 if (entry
->in_mrgctx
) {
8382 if (!can_encode_method (acfg
, entry
->d
.method
))
8385 if (!can_encode_class (acfg
, entry
->d
.klass
))
8388 if (!can_encode_patch (acfg
, entry
->data
))
8400 is_concrete_type (MonoType
*t
)
8405 if (t
->type
== MONO_TYPE_VAR
|| t
->type
== MONO_TYPE_MVAR
)
8407 if (t
->type
== MONO_TYPE_GENERICINST
) {
8408 MonoGenericContext
*orig_ctx
;
8409 MonoGenericInst
*inst
;
8412 if (!MONO_TYPE_ISSTRUCT (t
))
8414 klass
= mono_class_from_mono_type_internal (t
);
8415 orig_ctx
= &mono_class_get_generic_class (klass
)->context
;
8417 inst
= orig_ctx
->class_inst
;
8419 for (i
= 0; i
< inst
->type_argc
; ++i
) {
8420 arg
= mini_get_underlying_type (inst
->type_argv
[i
]);
8421 if (!is_concrete_type (arg
))
8425 inst
= orig_ctx
->method_inst
;
8427 for (i
= 0; i
< inst
->type_argc
; ++i
) {
8428 arg
= mini_get_underlying_type (inst
->type_argv
[i
]);
8429 if (!is_concrete_type (arg
))
8437 static MonoMethodSignature
*
8438 get_concrete_sig (MonoMethodSignature
*sig
)
8440 gboolean concrete
= TRUE
;
8442 if (!sig
->has_type_parameters
)
8445 /* For signatures created during generic sharing, convert them to a concrete signature if possible */
8446 MonoMethodSignature
*copy
= mono_metadata_signature_dup (sig
);
8449 //printf ("%s\n", mono_signature_full_name (sig));
8451 if (sig
->ret
->byref
)
8452 copy
->ret
= m_class_get_this_arg (mono_defaults
.int_class
);
8454 copy
->ret
= mini_get_underlying_type (sig
->ret
);
8455 if (!is_concrete_type (copy
->ret
))
8457 for (i
= 0; i
< sig
->param_count
; ++i
) {
8458 if (sig
->params
[i
]->byref
) {
8459 MonoType
*t
= m_class_get_byval_arg (mono_class_from_mono_type_internal (sig
->params
[i
]));
8460 t
= mini_get_underlying_type (t
);
8461 copy
->params
[i
] = m_class_get_this_arg (mono_class_from_mono_type_internal (t
));
8463 copy
->params
[i
] = mini_get_underlying_type (sig
->params
[i
]);
8465 if (!is_concrete_type (copy
->params
[i
]))
8468 copy
->has_type_parameters
= 0;
8474 /* LOCKING: Assumes the loader lock is held */
8476 add_gsharedvt_wrappers (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
, gboolean gsharedvt_in
, gboolean gsharedvt_out
, gboolean interp_in
)
8478 MonoMethod
*wrapper
;
8479 gboolean add_in
= gsharedvt_in
;
8480 gboolean add_out
= gsharedvt_out
;
8482 if (gsharedvt_in
&& g_hash_table_lookup (acfg
->gsharedvt_in_signatures
, sig
))
8484 if (gsharedvt_out
&& g_hash_table_lookup (acfg
->gsharedvt_out_signatures
, sig
))
8487 if (!add_in
&& !add_out
)
8490 if (mini_is_gsharedvt_variable_signature (sig
))
8494 g_hash_table_insert (acfg
->gsharedvt_in_signatures
, sig
, sig
);
8496 g_hash_table_insert (acfg
->gsharedvt_out_signatures
, sig
, sig
);
8498 sig
= get_concrete_sig (sig
);
8501 //printf ("%s\n", mono_signature_full_name (sig));
8504 wrapper
= mini_get_gsharedvt_in_sig_wrapper (sig
);
8505 add_extra_method (acfg
, wrapper
);
8507 if (gsharedvt_out
) {
8508 wrapper
= mini_get_gsharedvt_out_sig_wrapper (sig
);
8509 add_extra_method (acfg
, wrapper
);
8512 #ifndef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
8514 wrapper
= mini_get_interp_in_wrapper (sig
);
8515 add_extra_method (acfg
, wrapper
);
8516 //printf ("X: %s\n", mono_method_full_name (wrapper, 1));
8524 * AOT compile a given method.
8525 * This function might be called by multiple threads, so it must be thread-safe.
8528 compile_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
8531 MonoJumpInfo
*patch_info
;
8534 MonoMethod
*wrapped
;
8535 gint64 jit_time_start
;
8538 if (acfg
->aot_opts
.metadata_only
)
8541 mono_acfg_lock (acfg
);
8542 index
= get_method_index (acfg
, method
);
8543 mono_acfg_unlock (acfg
);
8545 /* fixme: maybe we can also precompile wrapper methods */
8546 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
8547 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
8548 (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)) {
8549 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
8553 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
8556 wrapped
= mono_marshal_method_from_wrapper (method
);
8557 if (wrapped
&& (wrapped
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) && wrapped
->is_generic
)
8558 // FIXME: The wrapper should be generic too, but it is not
8561 if (method
->wrapper_type
== MONO_WRAPPER_COMINTEROP
)
8564 if (acfg
->aot_opts
.profile_only
&& !g_hash_table_lookup (acfg
->profile_methods
, method
)) {
8565 if (acfg
->aot_opts
.llvm_only
) {
8566 gboolean keep
= FALSE
;
8567 if (method
->wrapper_type
) {
8568 /* Keep most wrappers */
8569 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
8570 switch (info
->subtype
) {
8571 case WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
:
8572 case WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
:
8579 if (always_aot (method
))
8584 if (!method
->is_inflated
)
8589 mono_atomic_inc_i32 (&acfg
->stats
.mcount
);
8592 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
)) {
8593 mono_atomic_inc_i32 (&acfg
->stats
.genericcount
);
8598 //acfg->aot_opts.print_skipped_methods = TRUE;
8601 * Since these methods are the only ones which are compiled with
8602 * AOT support, and they are not used by runtime startup/shutdown code,
8603 * the runtime will not see AOT methods during AOT compilation,so it
8604 * does not need to support them by creating a fake GOT etc.
8606 flags
= JIT_FLAG_AOT
;
8607 if (mono_aot_mode_is_full (&acfg
->aot_opts
))
8608 flags
= (JitFlags
)(flags
| JIT_FLAG_FULL_AOT
);
8610 flags
= (JitFlags
)(flags
| JIT_FLAG_LLVM
);
8611 if (acfg
->aot_opts
.llvm_only
)
8612 flags
= (JitFlags
)(flags
| JIT_FLAG_LLVM_ONLY
| JIT_FLAG_EXPLICIT_NULL_CHECKS
);
8613 if (acfg
->aot_opts
.no_direct_calls
)
8614 flags
= (JitFlags
)(flags
| JIT_FLAG_NO_DIRECT_ICALLS
);
8615 if (acfg
->aot_opts
.direct_pinvoke
)
8616 flags
= (JitFlags
)(flags
| JIT_FLAG_DIRECT_PINVOKE
);
8617 if (acfg
->aot_opts
.interp
)
8618 flags
= (JitFlags
)(flags
| JIT_FLAG_INTERP
);
8619 if (acfg
->aot_opts
.use_current_cpu
)
8620 flags
= (JitFlags
)(flags
| JIT_FLAG_USE_CURRENT_CPU
);
8622 jit_time_start
= mono_time_track_start ();
8623 cfg
= mini_method_compile (method
, acfg
->opts
, mono_get_root_domain (), flags
, 0, index
);
8624 mono_time_track_end (&mono_jit_stats
.jit_time
, jit_time_start
);
8626 if (cfg
->exception_type
== MONO_EXCEPTION_GENERIC_SHARING_FAILED
) {
8627 if (acfg
->aot_opts
.print_skipped_methods
)
8628 printf ("Skip (gshared failure): %s (%s)\n", mono_method_get_full_name (method
), cfg
->exception_message
);
8629 mono_atomic_inc_i32 (&acfg
->stats
.genericcount
);
8632 if (cfg
->exception_type
!= MONO_EXCEPTION_NONE
) {
8633 /* Some instances cannot be JITted due to constraints etc. */
8634 if (!method
->is_inflated
)
8635 report_loader_error (acfg
, cfg
->error
, FALSE
, "Unable to compile method '%s' due to: '%s'.\n", mono_method_get_full_name (method
), mono_error_get_message (cfg
->error
));
8636 /* Let the exception happen at runtime */
8640 if (cfg
->disable_aot
) {
8641 if (acfg
->aot_opts
.print_skipped_methods
)
8642 printf ("Skip (disabled): %s\n", mono_method_get_full_name (method
));
8643 mono_atomic_inc_i32 (&acfg
->stats
.ocount
);
8646 cfg
->method_index
= index
;
8648 /* Nullify patches which need no aot processing */
8649 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8650 switch (patch_info
->type
) {
8651 case MONO_PATCH_INFO_LABEL
:
8652 case MONO_PATCH_INFO_BB
:
8653 patch_info
->type
= MONO_PATCH_INFO_NONE
;
8660 /* Collect method->token associations from the cfg */
8661 mono_acfg_lock (acfg
);
8662 g_hash_table_foreach (cfg
->token_info_hash
, add_token_info_hash
, acfg
);
8663 mono_acfg_unlock (acfg
);
8664 g_hash_table_destroy (cfg
->token_info_hash
);
8665 cfg
->token_info_hash
= NULL
;
8668 * Check for absolute addresses.
8671 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8672 switch (patch_info
->type
) {
8673 case MONO_PATCH_INFO_ABS
:
8674 /* unable to handle this */
8683 if (acfg
->aot_opts
.print_skipped_methods
)
8684 printf ("Skip (abs call): %s\n", mono_method_get_full_name (method
));
8685 mono_atomic_inc_i32 (&acfg
->stats
.abscount
);
8689 /* Lock for the rest of the code */
8690 mono_acfg_lock (acfg
);
8693 acfg
->stats
.method_categories
[METHOD_CAT_GSHAREDVT
] ++;
8694 else if (cfg
->gshared
)
8695 acfg
->stats
.method_categories
[METHOD_CAT_INST
] ++;
8696 else if (cfg
->method
->wrapper_type
)
8697 acfg
->stats
.method_categories
[METHOD_CAT_WRAPPER
] ++;
8699 acfg
->stats
.method_categories
[METHOD_CAT_NORMAL
] ++;
8702 * Check for methods/klasses we can't encode.
8705 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8706 if (!can_encode_patch (acfg
, patch_info
))
8711 if (acfg
->aot_opts
.print_skipped_methods
)
8712 printf ("Skip (patches): %s\n", mono_method_get_full_name (method
));
8713 acfg
->stats
.ocount
++;
8714 mono_acfg_unlock (acfg
);
8718 if (!cfg
->compile_llvm
)
8719 acfg
->has_jitted_code
= TRUE
;
8721 if (method
->is_inflated
&& acfg
->aot_opts
.log_instances
) {
8722 if (acfg
->instances_logfile
)
8723 fprintf (acfg
->instances_logfile
, "%s ### %d\n", mono_method_get_full_name (method
), cfg
->code_size
);
8725 printf ("%s ### %d\n", mono_method_get_full_name (method
), cfg
->code_size
);
8728 /* Adds generic instances referenced by this method */
8730 * The depth is used to avoid infinite loops when generic virtual recursion is
8733 depth
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_depth
, method
));
8734 if (!acfg
->aot_opts
.no_instances
&& depth
< 32 && (mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))) {
8735 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8736 switch (patch_info
->type
) {
8737 case MONO_PATCH_INFO_RGCTX_FETCH
:
8738 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
:
8739 case MONO_PATCH_INFO_METHOD
:
8740 case MONO_PATCH_INFO_METHOD_FTNDESC
:
8741 case MONO_PATCH_INFO_METHOD_RGCTX
: {
8742 MonoMethod
*m
= NULL
;
8744 if (patch_info
->type
== MONO_PATCH_INFO_RGCTX_FETCH
|| patch_info
->type
== MONO_PATCH_INFO_RGCTX_SLOT_INDEX
) {
8745 MonoJumpInfoRgctxEntry
*e
= patch_info
->data
.rgctx_entry
;
8747 if (e
->info_type
== MONO_RGCTX_INFO_GENERIC_METHOD_CODE
|| e
->info_type
== MONO_RGCTX_INFO_METHOD_FTNDESC
)
8748 m
= e
->data
->data
.method
;
8750 m
= patch_info
->data
.method
;
8755 if (m
->is_inflated
&& (mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))) {
8756 if (!(mono_class_generic_sharing_enabled (m
->klass
) &&
8757 mono_method_is_generic_sharable_full (m
, FALSE
, FALSE
, FALSE
)) &&
8758 (!method_has_type_vars (m
) || mono_method_is_generic_sharable_full (m
, TRUE
, TRUE
, FALSE
))) {
8759 if (m
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
8760 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && !method_has_type_vars (m
))
8761 add_extra_method_with_depth (acfg
, mono_marshal_get_native_wrapper (m
, TRUE
, TRUE
), depth
+ 1);
8763 add_extra_method_with_depth (acfg
, m
, depth
+ 1);
8764 add_types_from_method_header (acfg
, m
);
8767 add_generic_class_with_depth (acfg
, m
->klass
, depth
+ 5, "method");
8769 if (m
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_MANAGED
) {
8770 WrapperInfo
*info
= mono_marshal_get_wrapper_info (m
);
8772 if (info
&& info
->subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
)
8773 add_extra_method_with_depth (acfg
, m
, depth
+ 1);
8777 case MONO_PATCH_INFO_VTABLE
: {
8778 MonoClass
*klass
= patch_info
->data
.klass
;
8780 if (mono_class_is_ginst (klass
) && !mini_class_is_generic_sharable (klass
))
8781 add_generic_class_with_depth (acfg
, klass
, depth
+ 5, "vtable");
8784 case MONO_PATCH_INFO_SFLDA
: {
8785 MonoClass
*klass
= patch_info
->data
.field
->parent
;
8787 /* The .cctor needs to run at runtime. */
8788 if (mono_class_is_ginst (klass
) && !mono_generic_context_is_sharable_full (&mono_class_get_generic_class (klass
)->context
, FALSE
, FALSE
) && mono_class_get_cctor (klass
))
8789 add_extra_method_with_depth (acfg
, mono_class_get_cctor (klass
), depth
+ 1);
8798 /* Determine whenever the method has GOT slots */
8799 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8800 switch (patch_info
->type
) {
8801 case MONO_PATCH_INFO_GOT_OFFSET
:
8802 case MONO_PATCH_INFO_NONE
:
8803 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
:
8804 case MONO_PATCH_INFO_GC_NURSERY_START
:
8805 case MONO_PATCH_INFO_GC_NURSERY_BITS
:
8807 case MONO_PATCH_INFO_IMAGE
:
8808 /* The assembly is stored in GOT slot 0 */
8809 if (patch_info
->data
.image
!= acfg
->image
)
8810 cfg
->has_got_slots
= TRUE
;
8813 if (!is_plt_patch (patch_info
) || (cfg
->compile_llvm
&& acfg
->aot_opts
.llvm_only
))
8814 cfg
->has_got_slots
= TRUE
;
8819 if (!cfg
->has_got_slots
)
8820 mono_atomic_inc_i32 (&acfg
->stats
.methods_without_got_slots
);
8822 /* Add gsharedvt wrappers for signatures used by the method */
8823 if (acfg
->aot_opts
.llvm_only
) {
8826 if (!cfg
->method
->wrapper_type
|| cfg
->method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
8827 /* These only need out wrappers */
8828 add_gsharedvt_wrappers (acfg
, mono_method_signature_internal (cfg
->method
), FALSE
, TRUE
, FALSE
);
8830 for (l
= cfg
->signatures
; l
; l
= l
->next
) {
8831 MonoMethodSignature
*sig
= mono_metadata_signature_dup ((MonoMethodSignature
*)l
->data
);
8833 /* These only need in wrappers */
8834 add_gsharedvt_wrappers (acfg
, sig
, TRUE
, FALSE
, FALSE
);
8837 for (l
= cfg
->interp_in_signatures
; l
; l
= l
->next
) {
8838 MonoMethodSignature
*sig
= mono_metadata_signature_dup ((MonoMethodSignature
*)l
->data
);
8840 add_gsharedvt_wrappers (acfg
, sig
, TRUE
, FALSE
, FALSE
);
8842 } else if (mono_aot_mode_is_full (&acfg
->aot_opts
) && mono_aot_mode_is_interp (&acfg
->aot_opts
)) {
8843 /* The interpreter uses these wrappers to call aot-ed code */
8844 if (!cfg
->method
->wrapper_type
|| cfg
->method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
8845 add_gsharedvt_wrappers (acfg
, mono_method_signature_internal (cfg
->method
), FALSE
, TRUE
, TRUE
);
8849 acfg
->stats
.llvm_count
++;
8852 * FIXME: Instead of this mess, allocate the patches from the aot mempool.
8854 /* Make a copy of the patch info which is in the mempool */
8856 MonoJumpInfo
*patches
= NULL
, *patches_end
= NULL
;
8858 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8859 MonoJumpInfo
*new_patch_info
= mono_patch_info_dup_mp (acfg
->mempool
, patch_info
);
8862 patches
= new_patch_info
;
8864 patches_end
->next
= new_patch_info
;
8865 patches_end
= new_patch_info
;
8867 cfg
->patch_info
= patches
;
8869 /* Make a copy of the unwind info */
8871 GSList
*l
, *unwind_ops
;
8875 for (l
= cfg
->unwind_ops
; l
; l
= l
->next
) {
8876 op
= (MonoUnwindOp
*)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoUnwindOp
));
8877 memcpy (op
, l
->data
, sizeof (MonoUnwindOp
));
8878 unwind_ops
= g_slist_prepend_mempool (acfg
->mempool
, unwind_ops
, op
);
8880 cfg
->unwind_ops
= g_slist_reverse (unwind_ops
);
8882 /* Make a copy of the argument/local info */
8885 MonoInst
**args
, **locals
;
8886 MonoMethodSignature
*sig
;
8887 MonoMethodHeader
*header
;
8890 sig
= mono_method_signature_internal (method
);
8891 args
= (MonoInst
**)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
*) * (sig
->param_count
+ sig
->hasthis
));
8892 for (i
= 0; i
< sig
->param_count
+ sig
->hasthis
; ++i
) {
8893 args
[i
] = (MonoInst
*)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
));
8894 memcpy (args
[i
], cfg
->args
[i
], sizeof (MonoInst
));
8898 header
= mono_method_get_header_checked (method
, error
);
8899 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
8900 locals
= (MonoInst
**)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
*) * header
->num_locals
);
8901 for (i
= 0; i
< header
->num_locals
; ++i
) {
8902 locals
[i
] = (MonoInst
*)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
));
8903 memcpy (locals
[i
], cfg
->locals
[i
], sizeof (MonoInst
));
8905 mono_metadata_free_mh (header
);
8906 cfg
->locals
= locals
;
8909 /* Free some fields used by cfg to conserve memory */
8910 mono_empty_compile (cfg
);
8912 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
8914 while (index
>= acfg
->cfgs_size
) {
8915 MonoCompile
**new_cfgs
;
8918 new_size
= acfg
->cfgs_size
* 2;
8919 new_cfgs
= g_new0 (MonoCompile
*, new_size
);
8920 memcpy (new_cfgs
, acfg
->cfgs
, sizeof (MonoCompile
*) * acfg
->cfgs_size
);
8921 g_free (acfg
->cfgs
);
8922 acfg
->cfgs
= new_cfgs
;
8923 acfg
->cfgs_size
= new_size
;
8925 acfg
->cfgs
[index
] = cfg
;
8927 g_hash_table_insert (acfg
->method_to_cfg
, cfg
->orig_method
, cfg
);
8929 /* Update global stats while holding a lock. */
8930 mono_update_jit_stats (cfg
);
8933 if (cfg->orig_method->wrapper_type)
8934 g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
8937 mono_acfg_unlock (acfg
);
8939 mono_atomic_inc_i32 (&acfg
->stats
.ccount
);
8942 static mono_thread_start_return_t WINAPI
8943 compile_thread_main (gpointer user_data
)
8945 MonoAotCompile
*acfg
= ((MonoAotCompile
**)user_data
) [0];
8946 GPtrArray
*methods
= ((GPtrArray
**)user_data
) [1];
8949 mono_thread_set_name_constant_ignore_error (mono_thread_internal_current (), "AOT compiler", MonoSetThreadNameFlag_Permanent
);
8951 for (i
= 0; i
< methods
->len
; ++i
)
8952 compile_method (acfg
, (MonoMethod
*)g_ptr_array_index (methods
, i
));
8957 /* Used by the LLVM backend */
8959 mono_aot_get_got_offset (MonoJumpInfo
*ji
)
8961 return get_got_offset (llvm_acfg
, TRUE
, ji
);
8965 * mono_aot_is_shared_got_offset:
8967 * Return whenever OFFSET refers to a GOT slot which is preinitialized
8968 * when the AOT image is loaded.
8971 mono_aot_is_shared_got_offset (int offset
)
8973 return offset
< llvm_acfg
->nshared_got_entries
;
8977 mono_aot_get_method_name (MonoCompile
*cfg
)
8979 MonoMethod
*method
= cfg
->orig_method
;
8981 /* Use the mangled name if possible */
8982 if (method
->wrapper_type
== MONO_WRAPPER_OTHER
) {
8983 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
8984 if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
|| info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
)
8985 return mono_aot_get_mangled_method_name (method
);
8988 if (llvm_acfg
->aot_opts
.static_link
)
8989 /* Include the assembly name too to avoid duplicate symbol errors */
8990 return g_strdup_printf ("%s_%s", llvm_acfg
->assembly_name_sym
, get_debug_sym (cfg
->orig_method
, "", llvm_acfg
->method_label_hash
));
8992 return get_debug_sym (cfg
->orig_method
, "", llvm_acfg
->method_label_hash
);
8996 append_mangled_type (GString
*s
, MonoType
*t
)
8999 g_string_append_printf (s
, "b");
9001 case MONO_TYPE_VOID
:
9002 g_string_append_printf (s
, "void");
9004 case MONO_TYPE_BOOLEAN
:
9005 g_string_append_printf (s
, "bool");
9007 case MONO_TYPE_CHAR
:
9008 g_string_append_printf (s
, "char");
9011 g_string_append_printf (s
, "i1");
9014 g_string_append_printf (s
, "u1");
9017 g_string_append_printf (s
, "i2");
9020 g_string_append_printf (s
, "u2");
9023 g_string_append_printf (s
, "i4");
9026 g_string_append_printf (s
, "u4");
9029 g_string_append_printf (s
, "i8");
9032 g_string_append_printf (s
, "u8");
9035 g_string_append_printf (s
, "ii");
9038 g_string_append_printf (s
, "ui");
9041 g_string_append_printf (s
, "fl");
9044 g_string_append_printf (s
, "do");
9046 case MONO_TYPE_OBJECT
:
9047 g_string_append_printf (s
, "obj");
9050 char *fullname
= mono_type_full_name (t
);
9051 char *name
= fullname
;
9054 gboolean is_system
= FALSE
;
9057 len
= strlen ("System.");
9058 if (strncmp (fullname
, "System.", len
) == 0) {
9059 name
= fullname
+ len
;
9063 * Have to create a mangled name which is:
9067 temp
= g_string_new ("");
9068 len
= strlen (name
);
9069 for (i
= 0; i
< len
; ++i
) {
9072 g_string_append_c (temp
, c
);
9073 } else if (c
== '_') {
9074 g_string_append_c (temp
, '_');
9075 g_string_append_c (temp
, '_');
9077 g_string_append_c (temp
, '_');
9079 g_string_append_c (temp
, 'd');
9081 g_string_append_printf (temp
, "%x", (int)c
);
9084 temps
= g_string_free (temp
, FALSE
);
9085 /* Include the length to avoid different length type names aliasing each other */
9086 g_string_append_printf (s
, "cl%s%x_%s_", is_system
? "s" : "", (int)strlen (temps
), temps
);
9092 g_string_append_printf (s
, "_attrs_%d", t
->attrs
);
9097 append_mangled_signature (GString
*s
, MonoMethodSignature
*sig
)
9103 g_string_append_printf (s
, "pinvoke_");
9104 supported
= append_mangled_type (s
, sig
->ret
);
9107 g_string_append_printf (s
, "_");
9109 g_string_append_printf (s
, "this_");
9110 for (i
= 0; i
< sig
->param_count
; ++i
) {
9111 supported
= append_mangled_type (s
, sig
->params
[i
]);
9120 append_mangled_wrapper_type (GString
*s
, guint32 wrapper_type
)
9124 switch (wrapper_type
) {
9125 case MONO_WRAPPER_REMOTING_INVOKE
:
9126 label
= "remoting_invoke";
9128 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
9129 label
= "remoting_invoke_check";
9131 case MONO_WRAPPER_XDOMAIN_INVOKE
:
9132 label
= "remoting_invoke_xdomain";
9134 case MONO_WRAPPER_PROXY_ISINST
:
9135 label
= "proxy_isinst";
9137 case MONO_WRAPPER_LDFLD
:
9140 case MONO_WRAPPER_LDFLDA
:
9143 case MONO_WRAPPER_STFLD
:
9146 case MONO_WRAPPER_ALLOC
:
9149 case MONO_WRAPPER_WRITE_BARRIER
:
9150 label
= "write_barrier";
9152 case MONO_WRAPPER_STELEMREF
:
9153 label
= "stelemref";
9155 case MONO_WRAPPER_OTHER
:
9158 case MONO_WRAPPER_MANAGED_TO_NATIVE
:
9159 label
= "man2native";
9161 case MONO_WRAPPER_SYNCHRONIZED
:
9164 case MONO_WRAPPER_MANAGED_TO_MANAGED
:
9167 case MONO_WRAPPER_CASTCLASS
:
9168 label
= "castclass";
9170 case MONO_WRAPPER_RUNTIME_INVOKE
:
9171 label
= "run_invoke";
9173 case MONO_WRAPPER_DELEGATE_INVOKE
:
9176 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
9177 label
= "del_beg_inv";
9179 case MONO_WRAPPER_DELEGATE_END_INVOKE
:
9180 label
= "del_end_inv";
9182 case MONO_WRAPPER_NATIVE_TO_MANAGED
:
9183 label
= "native2man";
9186 g_assert_not_reached ();
9189 g_string_append_printf (s
, "%s_", label
);
9193 append_mangled_wrapper_subtype (GString
*s
, WrapperSubtype subtype
)
9199 case WRAPPER_SUBTYPE_NONE
:
9201 case WRAPPER_SUBTYPE_ELEMENT_ADDR
:
9202 label
= "elem_addr";
9204 case WRAPPER_SUBTYPE_STRING_CTOR
:
9207 case WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
:
9208 label
= "virt_stelem";
9210 case WRAPPER_SUBTYPE_FAST_MONITOR_ENTER
:
9211 label
= "fast_mon_enter";
9213 case WRAPPER_SUBTYPE_FAST_MONITOR_ENTER_V4
:
9214 label
= "fast_mon_enter_4";
9216 case WRAPPER_SUBTYPE_FAST_MONITOR_EXIT
:
9217 label
= "fast_monitor_exit";
9219 case WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
:
9220 label
= "ptr2struct";
9222 case WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
:
9223 label
= "struct2ptr";
9225 case WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE
:
9226 label
= "castclass_w_cache";
9228 case WRAPPER_SUBTYPE_ISINST_WITH_CACHE
:
9229 label
= "isinst_w_cache";
9231 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL
:
9232 label
= "run_inv_norm";
9234 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC
:
9235 label
= "run_inv_dyn";
9237 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
:
9238 label
= "run_inv_dir";
9240 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
:
9241 label
= "run_inv_vir";
9243 case WRAPPER_SUBTYPE_ICALL_WRAPPER
:
9246 case WRAPPER_SUBTYPE_NATIVE_FUNC_AOT
:
9247 label
= "native_func_aot";
9249 case WRAPPER_SUBTYPE_PINVOKE
:
9252 case WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
:
9253 label
= "synch_inner";
9255 case WRAPPER_SUBTYPE_GSHAREDVT_IN
:
9256 label
= "gshared_in";
9258 case WRAPPER_SUBTYPE_GSHAREDVT_OUT
:
9259 label
= "gshared_out";
9261 case WRAPPER_SUBTYPE_ARRAY_ACCESSOR
:
9262 label
= "array_acc";
9264 case WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER
:
9265 label
= "generic_arry_help";
9267 case WRAPPER_SUBTYPE_DELEGATE_INVOKE_VIRTUAL
:
9268 label
= "del_inv_virt";
9270 case WRAPPER_SUBTYPE_DELEGATE_INVOKE_BOUND
:
9271 label
= "del_inv_bound";
9273 case WRAPPER_SUBTYPE_INTERP_IN
:
9274 label
= "interp_in";
9276 case WRAPPER_SUBTYPE_INTERP_LMF
:
9277 label
= "interp_lmf";
9279 case WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
:
9280 label
= "gsharedvt_in_sig";
9282 case WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
:
9283 label
= "gsharedvt_out_sig";
9285 case WRAPPER_SUBTYPE_AOT_INIT
:
9289 g_assert_not_reached ();
9292 g_string_append_printf (s
, "%s_", label
);
9296 sanitize_mangled_string (const char *input
)
9298 GString
*s
= g_string_new ("");
9300 for (int i
=0; input
[i
] != '\0'; i
++) {
9304 g_string_append (s
, "_dot_");
9307 g_string_append (s
, "_");
9310 g_string_append (s
, "_bt_");
9313 g_string_append (s
, "_le_");
9316 g_string_append (s
, "_gt_");
9319 g_string_append (s
, "_sl_");
9322 g_string_append (s
, "_lbrack_");
9325 g_string_append (s
, "_rbrack_");
9328 g_string_append (s
, "_lparen_");
9331 g_string_append (s
, "_dash_");
9334 g_string_append (s
, "_rparen_");
9337 g_string_append (s
, "_comma_");
9340 g_string_append (s
, "_colon_");
9343 g_string_append_c (s
, c
);
9347 return g_string_free (s
, FALSE
);
9351 append_mangled_klass (GString
*s
, MonoClass
*klass
)
9353 char *klass_desc
= mono_class_full_name (klass
);
9354 g_string_append_printf (s
, "_%s_%s_", m_class_get_name_space (klass
), klass_desc
);
9355 g_free (klass_desc
);
9362 append_mangled_method (GString
*s
, MonoMethod
*method
);
9365 append_mangled_wrapper (GString
*s
, MonoMethod
*method
)
9367 gboolean success
= TRUE
;
9368 gboolean append_sig
= TRUE
;
9369 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
9370 g_string_append_printf (s
, "wrapper_");
9371 /* Most wrappers are in mscorlib */
9372 if (m_class_get_image (method
->klass
) != mono_get_corlib ())
9373 g_string_append_printf (s
, "%s_", m_class_get_image (method
->klass
)->assembly
->aname
.name
);
9375 if (method
->wrapper_type
!= MONO_WRAPPER_OTHER
&& method
->wrapper_type
!= MONO_WRAPPER_MANAGED_TO_NATIVE
)
9376 append_mangled_wrapper_type (s
, method
->wrapper_type
);
9378 switch (method
->wrapper_type
) {
9379 case MONO_WRAPPER_REMOTING_INVOKE
:
9380 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
9381 case MONO_WRAPPER_XDOMAIN_INVOKE
: {
9382 MonoMethod
*m
= mono_marshal_method_from_wrapper (method
);
9384 success
= success
&& append_mangled_method (s
, m
);
9387 case MONO_WRAPPER_PROXY_ISINST
:
9388 case MONO_WRAPPER_LDFLD
:
9389 case MONO_WRAPPER_LDFLDA
:
9390 case MONO_WRAPPER_STFLD
: {
9392 success
= success
&& append_mangled_klass (s
, info
->d
.proxy
.klass
);
9395 case MONO_WRAPPER_ALLOC
: {
9396 /* The GC name is saved once in MonoAotFileInfo */
9397 g_assert (info
->d
.alloc
.alloc_type
!= -1);
9398 g_string_append_printf (s
, "%d_", info
->d
.alloc
.alloc_type
);
9400 g_string_append_printf (s
, "%s_", method
->name
);
9403 case MONO_WRAPPER_WRITE_BARRIER
: {
9404 g_string_append_printf (s
, "%s_", method
->name
);
9407 case MONO_WRAPPER_STELEMREF
: {
9408 append_mangled_wrapper_subtype (s
, info
->subtype
);
9409 if (info
->subtype
== WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
)
9410 g_string_append_printf (s
, "%d", info
->d
.virtual_stelemref
.kind
);
9413 case MONO_WRAPPER_OTHER
: {
9414 append_mangled_wrapper_subtype (s
, info
->subtype
);
9415 if (info
->subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
||
9416 info
->subtype
== WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
)
9417 success
= success
&& append_mangled_klass (s
, method
->klass
);
9418 else if (info
->subtype
== WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
)
9419 success
= success
&& append_mangled_method (s
, info
->d
.synchronized_inner
.method
);
9420 else if (info
->subtype
== WRAPPER_SUBTYPE_ARRAY_ACCESSOR
)
9421 success
= success
&& append_mangled_method (s
, info
->d
.array_accessor
.method
);
9422 else if (info
->subtype
== WRAPPER_SUBTYPE_INTERP_IN
)
9423 append_mangled_signature (s
, info
->d
.interp_in
.sig
);
9424 else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
) {
9425 append_mangled_signature (s
, info
->d
.gsharedvt
.sig
);
9427 } else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
) {
9428 append_mangled_signature (s
, info
->d
.gsharedvt
.sig
);
9430 } else if (info
->subtype
== WRAPPER_SUBTYPE_INTERP_LMF
)
9431 g_string_append_printf (s
, "%s", method
->name
);
9432 else if (info
->subtype
== WRAPPER_SUBTYPE_AOT_INIT
) {
9433 g_string_append_printf (s
, "%s_%d_", m_class_get_image (method
->klass
)->assembly
->aname
.name
, info
->d
.aot_init
.subtype
);
9438 case MONO_WRAPPER_MANAGED_TO_NATIVE
: {
9439 append_mangled_wrapper_subtype (s
, info
->subtype
);
9440 if (info
->subtype
== WRAPPER_SUBTYPE_ICALL_WRAPPER
) {
9441 const char *name
= method
->name
;
9442 const char *prefix
= "__icall_wrapper_";
9443 if (strstr (name
, prefix
) == name
)
9444 name
+= strlen (prefix
);
9445 g_string_append_printf (s
, "%s", name
);
9447 } else if (info
->subtype
== WRAPPER_SUBTYPE_NATIVE_FUNC_AOT
) {
9448 success
= success
&& append_mangled_method (s
, info
->d
.managed_to_native
.method
);
9450 g_assert (info
->subtype
== WRAPPER_SUBTYPE_NONE
|| info
->subtype
== WRAPPER_SUBTYPE_PINVOKE
);
9451 success
= success
&& append_mangled_method (s
, info
->d
.managed_to_native
.method
);
9455 case MONO_WRAPPER_SYNCHRONIZED
: {
9458 m
= mono_marshal_method_from_wrapper (method
);
9460 g_assert (m
!= method
);
9461 success
= success
&& append_mangled_method (s
, m
);
9464 case MONO_WRAPPER_MANAGED_TO_MANAGED
: {
9465 append_mangled_wrapper_subtype (s
, info
->subtype
);
9467 if (info
->subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
) {
9468 g_string_append_printf (s
, "%d_", info
->d
.element_addr
.rank
);
9469 g_string_append_printf (s
, "%d_", info
->d
.element_addr
.elem_size
);
9470 } else if (info
->subtype
== WRAPPER_SUBTYPE_STRING_CTOR
) {
9471 success
= success
&& append_mangled_method (s
, info
->d
.string_ctor
.method
);
9472 } else if (info
->subtype
== WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER
) {
9473 success
= success
&& append_mangled_method (s
, info
->d
.generic_array_helper
.method
);
9479 case MONO_WRAPPER_CASTCLASS
: {
9480 append_mangled_wrapper_subtype (s
, info
->subtype
);
9483 case MONO_WRAPPER_RUNTIME_INVOKE
: {
9484 append_mangled_wrapper_subtype (s
, info
->subtype
);
9485 if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
|| info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
)
9486 success
= success
&& append_mangled_method (s
, info
->d
.runtime_invoke
.method
);
9487 else if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL
)
9488 success
= success
&& append_mangled_signature (s
, info
->d
.runtime_invoke
.sig
);
9491 case MONO_WRAPPER_DELEGATE_INVOKE
:
9492 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
9493 case MONO_WRAPPER_DELEGATE_END_INVOKE
: {
9494 if (method
->is_inflated
) {
9495 /* These wrappers are identified by their class */
9496 g_string_append_printf (s
, "i_");
9497 success
= success
&& append_mangled_klass (s
, method
->klass
);
9499 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
9501 g_string_append_printf (s
, "u_");
9502 if (method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
9503 append_mangled_wrapper_subtype (s
, info
->subtype
);
9504 g_string_append_printf (s
, "u_sigstart");
9508 case MONO_WRAPPER_NATIVE_TO_MANAGED
: {
9510 success
= success
&& append_mangled_method (s
, info
->d
.native_to_managed
.method
);
9511 success
= success
&& append_mangled_klass (s
, method
->klass
);
9515 g_assert_not_reached ();
9517 if (success
&& append_sig
)
9518 success
= append_mangled_signature (s
, mono_method_signature_internal (method
));
9523 append_mangled_ginst (GString
*str
, MonoGenericInst
*ginst
)
9527 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
9529 g_string_append (str
, ", ");
9530 MonoType
*type
= ginst
->type_argv
[i
];
9531 switch (type
->type
) {
9533 case MONO_TYPE_MVAR
: {
9534 MonoType
*constraint
= NULL
;
9535 if (type
->data
.generic_param
)
9536 constraint
= type
->data
.generic_param
->gshared_constraint
;
9538 g_assert (constraint
->type
!= MONO_TYPE_VAR
&& constraint
->type
!= MONO_TYPE_MVAR
);
9539 g_string_append (str
, "gshared:");
9540 mono_type_get_desc (str
, constraint
, TRUE
);
9543 // Else falls through to common case
9546 mono_type_get_desc (str
, type
, TRUE
);
9552 append_mangled_context (GString
*str
, MonoGenericContext
*context
)
9554 GString
*res
= g_string_new ("");
9556 g_string_append_printf (res
, "gens_");
9557 g_string_append (res
, "00");
9559 gboolean good
= context
->class_inst
&& context
->class_inst
->type_argc
> 0;
9560 good
= good
|| (context
->method_inst
&& context
->method_inst
->type_argc
> 0);
9563 if (context
->class_inst
)
9564 append_mangled_ginst (res
, context
->class_inst
);
9565 if (context
->method_inst
) {
9566 if (context
->class_inst
)
9567 g_string_append (res
, "11");
9568 append_mangled_ginst (res
, context
->method_inst
);
9570 g_string_append_printf (str
, "gens_%s", res
->str
);
9575 append_mangled_method (GString
*s
, MonoMethod
*method
)
9577 if (method
->wrapper_type
)
9578 return append_mangled_wrapper (s
, method
);
9580 if (method
->is_inflated
) {
9581 g_string_append_printf (s
, "inflated_");
9582 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
9583 g_assert (imethod
->context
.class_inst
!= NULL
|| imethod
->context
.method_inst
!= NULL
);
9585 append_mangled_context (s
, &imethod
->context
);
9586 g_string_append_printf (s
, "_declared_by_%s_", m_class_get_image (imethod
->declaring
->klass
)->assembly
->aname
.name
);
9587 append_mangled_method (s
, imethod
->declaring
);
9588 } else if (method
->is_generic
) {
9589 g_string_append_printf (s
, "%s_", m_class_get_image (method
->klass
)->assembly
->aname
.name
);
9591 g_string_append_printf (s
, "generic_");
9592 append_mangled_klass (s
, method
->klass
);
9593 g_string_append_printf (s
, "_%s_", method
->name
);
9595 MonoGenericContainer
*container
= mono_method_get_generic_container (method
);
9596 g_string_append_printf (s
, "_");
9597 append_mangled_context (s
, &container
->context
);
9599 return append_mangled_signature (s
, mono_method_signature_internal (method
));
9601 g_string_append_printf (s
, "%s", m_class_get_image (method
->klass
)->assembly
->aname
.name
);
9602 append_mangled_klass (s
, method
->klass
);
9603 g_string_append_printf (s
, "_%s_", method
->name
);
9604 if (!append_mangled_signature (s
, mono_method_signature_internal (method
))) {
9605 g_string_free (s
, TRUE
);
9614 * mono_aot_get_mangled_method_name:
9616 * Return a unique mangled name for METHOD, or NULL.
9619 mono_aot_get_mangled_method_name (MonoMethod
*method
)
9621 // FIXME: use static cache (mempool?)
9622 // We call this a *lot*
9624 GString
*s
= g_string_new ("aot_");
9625 if (!append_mangled_method (s
, method
)) {
9626 g_string_free (s
, TRUE
);
9629 char *out
= g_string_free (s
, FALSE
);
9630 // Scrub method and class names
9631 char *cleaned
= sanitize_mangled_string (out
);
9638 mono_aot_is_direct_callable (MonoJumpInfo
*patch_info
)
9640 return is_direct_callable (llvm_acfg
, NULL
, patch_info
);
9644 mono_aot_mark_unused_llvm_plt_entry (MonoJumpInfo
*patch_info
)
9646 MonoPltEntry
*plt_entry
;
9648 plt_entry
= get_plt_entry (llvm_acfg
, patch_info
);
9649 plt_entry
->llvm_used
= FALSE
;
9653 mono_aot_get_direct_call_symbol (MonoJumpInfoType type
, gconstpointer data
)
9655 const char *sym
= NULL
;
9657 if (llvm_acfg
->aot_opts
.direct_icalls
) {
9658 if (type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
9659 /* Call to a C function implementing a jit icall */
9660 sym
= mono_find_jit_icall_info ((MonoJitICallId
)(gsize
)data
)->c_symbol
;
9661 } else if (type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
9662 MonoMethod
*method
= (MonoMethod
*)data
;
9663 if (!(method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
9664 sym
= lookup_icall_symbol_name_aot (method
);
9665 else if (llvm_acfg
->aot_opts
.direct_pinvoke
)
9666 sym
= get_pinvoke_import (llvm_acfg
, method
);
9667 } else if (type
== MONO_PATCH_INFO_JIT_ICALL_ID
) {
9668 MonoJitICallInfo
const * const info
= mono_find_jit_icall_info ((MonoJitICallId
)(gsize
)data
);
9669 char const * const name
= info
->c_symbol
;
9670 if (name
&& info
->func
== info
->wrapper
)
9674 return g_strdup (sym
);
9680 mono_aot_get_plt_symbol (MonoJumpInfoType type
, gconstpointer data
)
9682 MonoJumpInfo
*ji
= (MonoJumpInfo
*)mono_mempool_alloc (llvm_acfg
->mempool
, sizeof (MonoJumpInfo
));
9683 MonoPltEntry
*plt_entry
;
9684 const char *sym
= NULL
;
9687 ji
->data
.target
= data
;
9689 if (!can_encode_patch (llvm_acfg
, ji
))
9692 if (llvm_acfg
->aot_opts
.direct_icalls
) {
9693 if (type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
9694 /* Call to a C function implementing a jit icall */
9695 sym
= mono_find_jit_icall_info ((MonoJitICallId
)(gsize
)data
)->c_symbol
;
9696 } else if (type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
9697 MonoMethod
*method
= (MonoMethod
*)data
;
9698 if (!(method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
9699 sym
= lookup_icall_symbol_name_aot (method
);
9702 return g_strdup (sym
);
9705 plt_entry
= get_plt_entry (llvm_acfg
, ji
);
9706 plt_entry
->llvm_used
= TRUE
;
9708 #if defined(TARGET_MACH)
9709 return g_strdup (plt_entry
->llvm_symbol
+ strlen (llvm_acfg
->llvm_label_prefix
));
9711 return g_strdup (plt_entry
->llvm_symbol
);
9716 mono_aot_get_method_index (MonoMethod
*method
)
9718 g_assert (llvm_acfg
);
9719 return get_method_index (llvm_acfg
, method
);
9723 mono_aot_patch_info_dup (MonoJumpInfo
* ji
)
9727 mono_acfg_lock (llvm_acfg
);
9728 res
= mono_patch_info_dup_mp (llvm_acfg
->mempool
, ji
);
9729 mono_acfg_unlock (llvm_acfg
);
9735 execute_system (const char * command
)
9739 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) && defined(HOST_WIN32)
9740 // We need an extra set of quotes around the whole command to properly handle commands
9741 // with spaces since internally the command is called through "cmd /c.
9742 char * quoted_command
= g_strdup_printf ("\"%s\"", command
);
9744 int size
= MultiByteToWideChar (CP_UTF8
, 0 , quoted_command
, -1, NULL
, 0);
9745 wchar_t* wstr
= g_malloc (sizeof (wchar_t) * size
);
9746 MultiByteToWideChar (CP_UTF8
, 0, quoted_command
, -1, wstr
, size
);
9747 status
= _wsystem (wstr
);
9750 g_free (quoted_command
);
9751 #elif defined (HAVE_SYSTEM)
9752 status
= system (command
);
9754 g_assert_not_reached ();
9765 * Emit the LLVM code into an LLVM bytecode file, and compile it using the LLVM
9769 emit_llvm_file (MonoAotCompile
*acfg
)
9771 char *command
, *opts
, *tempbc
, *optbc
, *output_fname
;
9773 if (acfg
->aot_opts
.llvm_only
&& acfg
->aot_opts
.asm_only
) {
9774 if (acfg
->aot_opts
.no_opt
)
9775 tempbc
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
9777 tempbc
= g_strdup_printf ("%s.bc", acfg
->tmpbasename
);
9778 optbc
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
9780 tempbc
= g_strdup_printf ("%s.bc", acfg
->tmpbasename
);
9781 optbc
= g_strdup_printf ("%s.opt.bc", acfg
->tmpbasename
);
9784 mono_llvm_emit_aot_module (tempbc
, g_path_get_basename (acfg
->image
->name
));
9786 if (acfg
->aot_opts
.no_opt
)
9789 * FIXME: Experiment with adding optimizations, the -std-compile-opts set takes
9790 * a lot of time, and doesn't seem to save much space.
9791 * The following optimizations cannot be enabled:
9793 * - 'jump-threading' changes our blockaddress references to int constants.
9794 * - 'basiccg' fails because it contains:
9795 * if (CS && !isa<IntrinsicInst>(II)) {
9796 * and isa<IntrinsicInst> is false for invokes to intrinsics (iltests.exe).
9797 * - 'prune-eh' and 'functionattrs' depend on 'basiccg'.
9798 * The opt list below was produced by taking the output of:
9799 * llvm-as < /dev/null | opt -O2 -disable-output -debug-pass=Arguments
9800 * then removing tailcallelim + the global opts.
9801 * strip-dead-prototypes deletes unused intrinsics definitions.
9803 /* The dse pass is disabled because of #13734 and #17616 */
9805 * The dse bug is in DeadStoreElimination.cpp:isOverwrite ():
9806 * // If we have no DataLayout information around, then the size of the store
9807 * // is inferrable from the pointee type. If they are the same type, then
9808 * // we know that the store is safe.
9809 * if (AA.getDataLayout() == 0 &&
9810 * Later.Ptr->getType() == Earlier.Ptr->getType()) {
9811 * return OverwriteComplete;
9812 * Here, if 'Earlier' refers to a memset, and Later has no size info, it mistakenly thinks the memset is redundant.
9814 if (acfg
->aot_opts
.llvm_only
) {
9815 // FIXME: This doesn't work yet
9816 opts
= g_strdup ("");
9818 opts
= g_strdup ("-disable-tail-calls -place-safepoints -spp-all-backedges");
9821 if (acfg
->aot_opts
.llvm_opts
) {
9822 opts
= g_strdup_printf ("%s %s", acfg
->aot_opts
.llvm_opts
, opts
);
9823 } else if (!acfg
->aot_opts
.llvm_only
) {
9824 opts
= g_strdup_printf ("-O2 %s", opts
);
9827 if (acfg
->aot_opts
.use_current_cpu
) {
9828 opts
= g_strdup_printf ("%s -mcpu=native", opts
);
9831 if (acfg
->aot_opts
.llvm_cpu_attr
) {
9832 opts
= g_strdup_printf ("%s -mattr=%s", opts
, acfg
->aot_opts
.llvm_cpu_attr
);
9835 if (mono_use_fast_math
) {
9836 // same parameters are passed to llc and LLVM JIT
9837 opts
= g_strdup_printf ("%s -fp-contract=fast -enable-no-infs-fp-math -enable-no-nans-fp-math -enable-no-signed-zeros-fp-math -enable-no-trapping-fp-math -enable-unsafe-fp-math", opts
);
9840 command
= g_strdup_printf ("\"%sopt\" -f %s -o \"%s\" \"%s\"", acfg
->aot_opts
.llvm_path
, opts
, optbc
, tempbc
);
9841 aot_printf (acfg
, "Executing opt: %s\n", command
);
9842 if (execute_system (command
) != 0)
9846 if (acfg
->aot_opts
.llvm_only
&& acfg
->aot_opts
.asm_only
)
9847 /* Nothing else to do */
9850 if (acfg
->aot_opts
.llvm_only
) {
9851 /* Use the stock clang from xcode */
9853 command
= g_strdup_printf ("%s -fexceptions -fpic -O2 -fno-optimize-sibling-calls -Wno-override-module -c -o \"%s\" \"%s.opt.bc\"", acfg
->aot_opts
.clangxx
, acfg
->llvm_ofile
, acfg
->tmpbasename
);
9855 aot_printf (acfg
, "Executing clang: %s\n", command
);
9856 if (execute_system (command
) != 0)
9861 if (!acfg
->llc_args
)
9862 acfg
->llc_args
= g_string_new ("");
9864 /* Verbose asm slows down llc greatly */
9865 g_string_append (acfg
->llc_args
, " -asm-verbose=false");
9867 if (acfg
->aot_opts
.mtriple
)
9868 g_string_append_printf (acfg
->llc_args
, " -mtriple=%s", acfg
->aot_opts
.mtriple
);
9870 #if defined(TARGET_X86_64_WIN32_MSVC)
9871 if (!acfg
->aot_opts
.mtriple
)
9872 g_string_append_printf (acfg
->llc_args
, " -mtriple=%s", "x86_64-pc-windows-msvc");
9875 g_string_append (acfg
->llc_args
, " -disable-gnu-eh-frame -enable-mono-eh-frame");
9877 g_string_append_printf (acfg
->llc_args
, " -mono-eh-frame-symbol=%s%s", acfg
->user_symbol_prefix
, acfg
->llvm_eh_frame_symbol
);
9879 g_string_append_printf (acfg
->llc_args
, " -disable-tail-calls");
9881 #if defined(TARGET_AMD64) || defined(TARGET_X86)
9882 /* This generates stack adjustments in the middle of functions breaking unwind info */
9883 g_string_append_printf (acfg
->llc_args
, " -no-x86-call-frame-opt");
9886 #if ( defined(TARGET_MACH) && defined(TARGET_ARM) ) || defined(TARGET_ORBIS) || defined(TARGET_X86_64_WIN32_MSVC)
9887 g_string_append_printf (acfg
->llc_args
, " -relocation-model=pic");
9889 if (llvm_acfg
->aot_opts
.static_link
)
9890 g_string_append_printf (acfg
->llc_args
, " -relocation-model=static");
9892 g_string_append_printf (acfg
->llc_args
, " -relocation-model=pic");
9895 if (acfg
->llvm_owriter
) {
9896 /* Emit an object file directly */
9897 output_fname
= g_strdup_printf ("%s", acfg
->llvm_ofile
);
9898 g_string_append_printf (acfg
->llc_args
, " -filetype=obj");
9900 output_fname
= g_strdup_printf ("%s", acfg
->llvm_sfile
);
9903 if (acfg
->aot_opts
.llvm_llc
) {
9904 g_string_append_printf (acfg
->llc_args
, " %s", acfg
->aot_opts
.llvm_llc
);
9907 if (acfg
->aot_opts
.use_current_cpu
) {
9908 g_string_append (acfg
->llc_args
, " -mcpu=native");
9911 if (acfg
->aot_opts
.llvm_cpu_attr
) {
9912 g_string_append_printf (acfg
->llc_args
, " -mattr=%s", acfg
->aot_opts
.llvm_cpu_attr
);
9915 command
= g_strdup_printf ("\"%sllc\" %s -o \"%s\" \"%s.opt.bc\"", acfg
->aot_opts
.llvm_path
, acfg
->llc_args
->str
, output_fname
, acfg
->tmpbasename
);
9916 g_free (output_fname
);
9918 aot_printf (acfg
, "Executing llc: %s\n", command
);
9920 if (execute_system (command
) != 0)
9926 /* Set the skip flag for methods which do not need to be emitted because of dedup */
9928 dedup_skip_methods (MonoAotCompile
*acfg
)
9932 if (acfg
->aot_opts
.llvm_only
)
9935 for (oindex
= 0; oindex
< acfg
->method_order
->len
; ++oindex
) {
9939 i
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->method_order
, oindex
));
9941 cfg
= acfg
->cfgs
[i
];
9946 method
= cfg
->orig_method
;
9948 gboolean dedup_collect
= acfg
->aot_opts
.dedup
|| (acfg
->aot_opts
.dedup_include
&& !acfg
->dedup_emit_mode
);
9949 gboolean dedupable
= mono_aot_can_dedup (method
);
9951 // cfg->skip is vital for LLVM to work, can't just continue in this loop
9952 if (dedupable
&& strcmp (method
->name
, "wbarrier_conc") && dedup_collect
) {
9953 mono_dedup_cache_method (acfg
, method
);
9955 // Don't compile inflated methods if we're in first phase of
9958 // In second phase, we emit methods that
9959 // are dedupable. We also emit later methods
9960 // which are referenced by them and added later.
9961 // For this reason, when in the dedup_include mode,
9962 // we never set skip.
9963 if (acfg
->aot_opts
.dedup
)
9967 // Don't compile anything in this mode
9968 if (acfg
->aot_opts
.dedup_include
&& !acfg
->dedup_emit_mode
)
9971 // Compile everything in this mode
9972 if (acfg
->aot_opts
.dedup_include
&& acfg
->dedup_emit_mode
)
9975 /*if (dedup_collect) {*/
9976 /*char *name = mono_aot_get_mangled_method_name (method);*/
9978 /*if (ignore_cfg (cfg))*/
9979 /*aot_printf (acfg, "Dedup Skipping %s\n", acfg->image->name, name);*/
9981 /*aot_printf (acfg, "Dedup Keeping %s\n", acfg->image->name, name);*/
9989 emit_code (MonoAotCompile
*acfg
)
9991 int oindex
, i
, prev_index
;
9992 gboolean saved_unbox_info
= FALSE
; // See mono_aot_get_unbox_trampoline.
9993 char symbol
[MAX_SYMBOL_SIZE
];
9995 if (acfg
->aot_opts
.llvm_only
)
9998 #if defined(TARGET_POWERPC64)
9999 sprintf (symbol
, ".Lgot_addr");
10000 emit_section_change (acfg
, ".text", 0);
10001 emit_alignment (acfg
, 8);
10002 emit_label (acfg
, symbol
);
10003 emit_pointer (acfg
, acfg
->got_symbol
);
10007 * This global symbol is used to compute the address of each method using the
10008 * code_offsets array. It is also used to compute the memory ranges occupied by
10009 * AOT code, so it must be equal to the address of the first emitted method.
10011 emit_section_change (acfg
, ".text", 0);
10012 emit_alignment_code (acfg
, 8);
10013 emit_info_symbol (acfg
, "jit_code_start", TRUE
);
10016 * Emit some padding so the local symbol for the first method doesn't have the
10017 * same address as 'methods'.
10019 emit_padding (acfg
, 16);
10021 for (oindex
= 0; oindex
< acfg
->method_order
->len
; ++oindex
) {
10023 MonoMethod
*method
;
10025 i
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->method_order
, oindex
));
10027 cfg
= acfg
->cfgs
[i
];
10032 method
= cfg
->orig_method
;
10034 if (ignore_cfg (cfg
))
10037 /* Emit unbox trampoline */
10038 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && m_class_is_valuetype (cfg
->orig_method
->klass
)) {
10039 sprintf (symbol
, "ut_%d", get_method_index (acfg
, method
));
10041 emit_section_change (acfg
, ".text", 0);
10043 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
) {
10044 emit_set_thumb_mode (acfg
);
10045 fprintf (acfg
->fp
, "\n.thumb_func\n");
10048 emit_label (acfg
, symbol
);
10050 arch_emit_unbox_trampoline (acfg
, cfg
, cfg
->orig_method
, cfg
->asm_symbol
);
10052 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
)
10053 emit_set_arm_mode (acfg
);
10055 if (!saved_unbox_info
) {
10056 char user_symbol
[128];
10057 GSList
*unwind_ops
;
10058 sprintf (user_symbol
, "%sunbox_trampoline_p", acfg
->user_symbol_prefix
);
10060 emit_label (acfg
, "ut_end");
10062 unwind_ops
= mono_unwind_get_cie_program ();
10063 save_unwind_info (acfg
, user_symbol
, unwind_ops
);
10064 mono_free_unwind_info (unwind_ops
);
10066 /* Save the unbox trampoline size */
10067 #ifdef TARGET_AMD64
10068 // LLVM unbox trampolines vary in size, 6 or 9 bytes,
10069 // due to the last instruction being 2 or 5 bytes.
10070 // There is no need to describe interior bytes of instructions
10071 // however, so state the size as if the last instruction is size 1.
10072 emit_int32 (acfg
, 5);
10074 emit_symbol_diff (acfg
, "ut_end", symbol
, 0);
10076 saved_unbox_info
= TRUE
;
10080 if (cfg
->compile_llvm
) {
10081 acfg
->stats
.llvm_count
++;
10083 emit_method_code (acfg
, cfg
);
10087 emit_section_change (acfg
, ".text", 0);
10088 emit_alignment_code (acfg
, 8);
10089 emit_info_symbol (acfg
, "jit_code_end", TRUE
);
10091 /* To distinguish it from the next symbol */
10092 emit_padding (acfg
, 4);
10095 * Add .no_dead_strip directives for all LLVM methods to prevent the OSX linker
10096 * from optimizing them away, since it doesn't see that code_offsets references them.
10097 * JITted methods don't need this since they are referenced using assembler local
10099 * FIXME: This is why write-symbols doesn't work on OSX ?
10101 if (acfg
->llvm
&& acfg
->need_no_dead_strip
) {
10102 fprintf (acfg
->fp
, "\n");
10103 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
10104 if (acfg
->cfgs
[i
] && acfg
->cfgs
[i
]->compile_llvm
)
10105 fprintf (acfg
->fp
, ".no_dead_strip %s\n", acfg
->cfgs
[i
]->asm_symbol
);
10110 * To work around linker issues, we emit a table of branches, and disassemble them at runtime.
10111 * This is PIE code, and the linker can update it if needed.
10113 #if defined(TARGET_ANDROID) || defined(__linux__)
10114 gboolean is_func
= FALSE
;
10116 gboolean is_func
= TRUE
;
10119 sprintf (symbol
, "method_addresses");
10120 emit_section_change (acfg
, RODATA_REL_SECT
, !!is_func
);
10121 emit_alignment_code (acfg
, 8);
10122 emit_info_symbol (acfg
, symbol
, is_func
);
10123 if (acfg
->aot_opts
.write_symbols
)
10124 emit_local_symbol (acfg
, symbol
, "method_addresses_end", is_func
);
10125 emit_unset_mode (acfg
);
10126 if (acfg
->need_no_dead_strip
)
10127 fprintf (acfg
->fp
, " .no_dead_strip %s\n", symbol
);
10129 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
10130 #ifdef MONO_ARCH_AOT_SUPPORTED
10131 if (!ignore_cfg (acfg
->cfgs
[i
])) {
10132 arch_emit_label_address (acfg
, acfg
->cfgs
[i
]->asm_symbol
, FALSE
, acfg
->thumb_mixed
&& acfg
->cfgs
[i
]->compile_llvm
, NULL
, &acfg
->call_table_entry_size
);
10134 arch_emit_label_address (acfg
, symbol
, FALSE
, FALSE
, NULL
, &acfg
->call_table_entry_size
);
10139 sprintf (symbol
, "method_addresses_end");
10140 emit_label (acfg
, symbol
);
10143 /* Emit a sorted table mapping methods to the index of their unbox trampolines */
10144 sprintf (symbol
, "unbox_trampolines");
10145 emit_section_change (acfg
, RODATA_SECT
, 0);
10146 emit_alignment (acfg
, 8);
10147 emit_info_symbol (acfg
, symbol
, FALSE
);
10150 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
10152 MonoMethod
*method
;
10155 cfg
= acfg
->cfgs
[i
];
10156 if (ignore_cfg (cfg
))
10159 method
= cfg
->orig_method
;
10161 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && m_class_is_valuetype (cfg
->orig_method
->klass
)) {
10162 index
= get_method_index (acfg
, method
);
10164 emit_int32 (acfg
, index
);
10165 /* Make sure the table is sorted by index */
10166 g_assert (index
> prev_index
);
10167 prev_index
= index
;
10170 sprintf (symbol
, "unbox_trampolines_end");
10171 emit_info_symbol (acfg
, symbol
, FALSE
);
10172 emit_int32 (acfg
, 0);
10174 /* Emit a separate table with the trampoline addresses/offsets */
10175 sprintf (symbol
, "unbox_trampoline_addresses");
10176 emit_section_change (acfg
, ".text", 0);
10177 emit_alignment_code (acfg
, 8);
10178 emit_info_symbol (acfg
, symbol
, TRUE
);
10180 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
10182 MonoMethod
*method
;
10184 cfg
= acfg
->cfgs
[i
];
10185 if (ignore_cfg (cfg
))
10188 method
= cfg
->orig_method
;
10191 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && m_class_is_valuetype (cfg
->orig_method
->klass
)) {
10192 #ifdef MONO_ARCH_AOT_SUPPORTED
10195 const int index
= get_method_index (acfg
, method
);
10196 sprintf (symbol
, "ut_%d", index
);
10198 arch_emit_direct_call (acfg
, symbol
, FALSE
, acfg
->thumb_mixed
&& cfg
->compile_llvm
, NULL
, &call_size
);
10202 emit_int32 (acfg
, 0);
10206 emit_info (MonoAotCompile
*acfg
)
10211 offsets
= g_new0 (gint32
, acfg
->nmethods
);
10213 for (oindex
= 0; oindex
< acfg
->method_order
->len
; ++oindex
) {
10214 i
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->method_order
, oindex
));
10216 if (acfg
->cfgs
[i
]) {
10217 emit_method_info (acfg
, acfg
->cfgs
[i
]);
10218 offsets
[i
] = acfg
->cfgs
[i
]->method_info_offset
;
10224 acfg
->stats
.offsets_size
+= emit_offset_table (acfg
, "method_info_offsets", MONO_AOT_TABLE_METHOD_INFO_OFFSETS
, acfg
->nmethods
, 10, offsets
);
10229 #endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
10231 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
10232 #define mix(a,b,c) { \
10233 a -= c; a ^= rot(c, 4); c += b; \
10234 b -= a; b ^= rot(a, 6); a += c; \
10235 c -= b; c ^= rot(b, 8); b += a; \
10236 a -= c; a ^= rot(c,16); c += b; \
10237 b -= a; b ^= rot(a,19); a += c; \
10238 c -= b; c ^= rot(b, 4); b += a; \
10240 #define mono_final(a,b,c) { \
10241 c ^= b; c -= rot(b,14); \
10242 a ^= c; a -= rot(c,11); \
10243 b ^= a; b -= rot(a,25); \
10244 c ^= b; c -= rot(b,16); \
10245 a ^= c; a -= rot(c,4); \
10246 b ^= a; b -= rot(a,14); \
10247 c ^= b; c -= rot(b,24); \
10251 mono_aot_type_hash (MonoType
*t1
)
10253 guint hash
= t1
->type
;
10255 hash
|= t1
->byref
<< 6; /* do not collide with t1->type values */
10256 switch (t1
->type
) {
10257 case MONO_TYPE_VALUETYPE
:
10258 case MONO_TYPE_CLASS
:
10259 case MONO_TYPE_SZARRAY
:
10260 /* check if the distribution is good enough */
10261 return ((hash
<< 5) - hash
) ^ mono_metadata_str_hash (m_class_get_name (t1
->data
.klass
));
10262 case MONO_TYPE_PTR
:
10263 return ((hash
<< 5) - hash
) ^ mono_metadata_type_hash (t1
->data
.type
);
10264 case MONO_TYPE_ARRAY
:
10265 return ((hash
<< 5) - hash
) ^ mono_metadata_type_hash (m_class_get_byval_arg (t1
->data
.array
->eklass
));
10266 case MONO_TYPE_GENERICINST
:
10267 return ((hash
<< 5) - hash
) ^ 0;
10274 * mono_aot_method_hash:
10276 * Return a hash code for methods which only depends on metadata.
10279 mono_aot_method_hash (MonoMethod
*method
)
10281 MonoMethodSignature
*sig
;
10285 guint32
*hashes_start
, *hashes
;
10287 MonoGenericInst
*class_ginst
= NULL
;
10288 MonoGenericInst
*ginst
= NULL
;
10290 /* Similar to the hash in mono_method_get_imt_slot () */
10292 sig
= mono_method_signature_internal (method
);
10294 if (mono_class_is_ginst (method
->klass
))
10295 class_ginst
= mono_class_get_generic_class (method
->klass
)->context
.class_inst
;
10296 if (method
->is_inflated
)
10297 ginst
= ((MonoMethodInflated
*)method
)->context
.method_inst
;
10299 hashes_count
= sig
->param_count
+ 5 + (class_ginst
? class_ginst
->type_argc
: 0) + (ginst
? ginst
->type_argc
: 0);
10300 hashes_start
= (guint32
*)g_malloc0 (hashes_count
* sizeof (guint32
));
10301 hashes
= hashes_start
;
10303 /* Some wrappers are assigned to random classes */
10304 if (!method
->wrapper_type
|| method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
10305 klass
= method
->klass
;
10307 klass
= mono_defaults
.object_class
;
10309 if (!method
->wrapper_type
) {
10312 if (mono_class_is_ginst (klass
))
10313 full_name
= mono_type_full_name (m_class_get_byval_arg (mono_class_get_generic_class (klass
)->container_class
));
10315 full_name
= mono_type_full_name (m_class_get_byval_arg (klass
));
10317 hashes
[0] = mono_metadata_str_hash (full_name
);
10319 g_free (full_name
);
10321 hashes
[0] = mono_metadata_str_hash (m_class_get_name (klass
));
10322 hashes
[1] = mono_metadata_str_hash (m_class_get_name_space (klass
));
10324 if (method
->wrapper_type
== MONO_WRAPPER_STFLD
|| method
->wrapper_type
== MONO_WRAPPER_LDFLD
|| method
->wrapper_type
== MONO_WRAPPER_LDFLDA
)
10325 /* The method name includes a stringified pointer */
10328 hashes
[2] = mono_metadata_str_hash (method
->name
);
10329 hashes
[3] = method
->wrapper_type
;
10330 hashes
[4] = mono_aot_type_hash (sig
->ret
);
10332 for (i
= 0; i
< sig
->param_count
; i
++) {
10333 hashes
[hindex
++] = mono_aot_type_hash (sig
->params
[i
]);
10336 for (i
= 0; i
< class_ginst
->type_argc
; ++i
)
10337 hashes
[hindex
++] = mono_aot_type_hash (class_ginst
->type_argv
[i
]);
10340 for (i
= 0; i
< ginst
->type_argc
; ++i
)
10341 hashes
[hindex
++] = mono_aot_type_hash (ginst
->type_argv
[i
]);
10343 g_assert (hindex
== hashes_count
);
10345 /* Setup internal state */
10346 a
= b
= c
= 0xdeadbeef + (((guint32
)hashes_count
)<<2);
10348 /* Handle most of the hashes */
10349 while (hashes_count
> 3) {
10358 /* Handle the last 3 hashes (all the case statements fall through) */
10359 switch (hashes_count
) {
10360 case 3 : c
+= hashes
[2];
10361 case 2 : b
+= hashes
[1];
10362 case 1 : a
+= hashes
[0];
10363 mono_final (a
,b
,c
);
10364 case 0: /* nothing left to add */
10368 g_free (hashes_start
);
10377 * mono_aot_get_array_helper_from_wrapper;
10379 * Get the helper method in Array called by an array wrapper method.
10382 mono_aot_get_array_helper_from_wrapper (MonoMethod
*method
)
10385 const char *prefix
;
10386 MonoGenericContext ctx
;
10387 MonoType
*args
[16];
10388 char *mname
, *iname
, *s
, *s2
, *helper_name
= NULL
;
10390 prefix
= "System.Collections.Generic";
10391 s
= g_strdup_printf ("%s", method
->name
+ strlen (prefix
) + 1);
10392 s2
= strstr (s
, "`1.");
10398 //printf ("X: %s %s\n", iname, mname);
10400 if (!strcmp (iname
, "IList"))
10401 helper_name
= g_strdup_printf ("InternalArray__%s", mname
);
10403 helper_name
= g_strdup_printf ("InternalArray__%s_%s", iname
, mname
);
10404 m
= get_method_nofail (mono_defaults
.array_class
, helper_name
, mono_method_signature_internal (method
)->param_count
, 0);
10406 g_free (helper_name
);
10409 if (m
->is_generic
) {
10410 ERROR_DECL (error
);
10411 memset (&ctx
, 0, sizeof (ctx
));
10412 args
[0] = m_class_get_byval_arg (m_class_get_element_class (method
->klass
));
10413 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
10414 m
= mono_class_inflate_generic_method_checked (m
, &ctx
, error
);
10415 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
10421 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
10423 typedef struct HashEntry
{
10424 guint32 key
, value
, index
;
10425 struct HashEntry
*next
;
10429 * emit_extra_methods:
10431 * Emit methods which are not in the METHOD table, like wrappers.
10434 emit_extra_methods (MonoAotCompile
*acfg
)
10436 int i
, table_size
, buf_size
;
10438 guint32
*info_offsets
;
10441 HashEntry
*entry
, *new_entry
;
10442 int nmethods
, max_chain_length
;
10443 int *chain_lengths
;
10445 info_offsets
= g_new0 (guint32
, acfg
->extra_methods
->len
);
10447 /* Emit method info */
10449 for (i
= 0; i
< acfg
->extra_methods
->len
; ++i
) {
10450 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (acfg
->extra_methods
, i
);
10451 MonoCompile
*cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, method
);
10453 if (ignore_cfg (cfg
))
10457 p
= buf
= (guint8
*)g_malloc (buf_size
);
10461 method
= cfg
->method_to_register
;
10463 encode_method_ref (acfg
, method
, p
, &p
);
10465 g_assert ((p
- buf
) < buf_size
);
10467 info_offsets
[i
] = add_to_blob (acfg
, buf
, p
- buf
);
10472 * Construct a chained hash table for mapping indexes in extra_method_info to
10475 table_size
= g_spaced_primes_closest ((int)(nmethods
* 1.5));
10476 table
= g_ptr_array_sized_new (table_size
);
10477 for (i
= 0; i
< table_size
; ++i
)
10478 g_ptr_array_add (table
, NULL
);
10479 chain_lengths
= g_new0 (int, table_size
);
10480 max_chain_length
= 0;
10481 for (i
= 0; i
< acfg
->extra_methods
->len
; ++i
) {
10482 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (acfg
->extra_methods
, i
);
10483 MonoCompile
*cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, method
);
10484 guint32 key
, value
;
10486 if (ignore_cfg (cfg
))
10489 key
= info_offsets
[i
];
10490 value
= get_method_index (acfg
, method
);
10492 hash
= mono_aot_method_hash (method
) % table_size
;
10493 //printf ("X: %s %x\n", mono_method_get_full_name (method), mono_aot_method_hash (method));
10495 chain_lengths
[hash
] ++;
10496 max_chain_length
= MAX (max_chain_length
, chain_lengths
[hash
]);
10498 new_entry
= (HashEntry
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (HashEntry
));
10499 new_entry
->key
= key
;
10500 new_entry
->value
= value
;
10502 entry
= (HashEntry
*)g_ptr_array_index (table
, hash
);
10503 if (entry
== NULL
) {
10504 new_entry
->index
= hash
;
10505 g_ptr_array_index (table
, hash
) = new_entry
;
10507 while (entry
->next
)
10508 entry
= entry
->next
;
10510 entry
->next
= new_entry
;
10511 new_entry
->index
= table
->len
;
10512 g_ptr_array_add (table
, new_entry
);
10515 g_free (chain_lengths
);
10517 //printf ("MAX: %d\n", max_chain_length);
10519 buf_size
= table
->len
* 12 + 4;
10520 p
= buf
= (guint8
*)g_malloc (buf_size
);
10521 encode_int (table_size
, p
, &p
);
10523 for (i
= 0; i
< table
->len
; ++i
) {
10524 HashEntry
*entry
= (HashEntry
*)g_ptr_array_index (table
, i
);
10526 if (entry
== NULL
) {
10527 encode_int (0, p
, &p
);
10528 encode_int (0, p
, &p
);
10529 encode_int (0, p
, &p
);
10531 //g_assert (entry->key > 0);
10532 encode_int (entry
->key
, p
, &p
);
10533 encode_int (entry
->value
, p
, &p
);
10535 encode_int (entry
->next
->index
, p
, &p
);
10537 encode_int (0, p
, &p
);
10540 g_assert (p
- buf
<= buf_size
);
10542 /* Emit the table */
10543 emit_aot_data (acfg
, MONO_AOT_TABLE_EXTRA_METHOD_TABLE
, "extra_method_table", buf
, p
- buf
);
10548 * Emit a table reverse mapping method indexes to their index in extra_method_info.
10549 * This is used by mono_aot_find_jit_info ().
10551 buf_size
= acfg
->extra_methods
->len
* 8 + 4;
10552 p
= buf
= (guint8
*)g_malloc (buf_size
);
10553 encode_int (acfg
->extra_methods
->len
, p
, &p
);
10554 for (i
= 0; i
< acfg
->extra_methods
->len
; ++i
) {
10555 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (acfg
->extra_methods
, i
);
10557 encode_int (get_method_index (acfg
, method
), p
, &p
);
10558 encode_int (info_offsets
[i
], p
, &p
);
10560 emit_aot_data (acfg
, MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS
, "extra_method_info_offsets", buf
, p
- buf
);
10563 g_free (info_offsets
);
10564 g_ptr_array_free (table
, TRUE
);
10568 generate_aotid (guint8
* aotid
)
10570 gpointer rand_handle
;
10571 ERROR_DECL (error
);
10574 rand_handle
= mono_rand_init (NULL
, 0);
10576 mono_rand_try_get_bytes (&rand_handle
, aotid
, 16, error
);
10577 mono_error_assert_ok (error
);
10579 mono_rand_close (rand_handle
);
10583 emit_exception_info (MonoAotCompile
*acfg
)
10587 SeqPointData sp_data
;
10588 gboolean seq_points_to_file
= FALSE
;
10590 offsets
= g_new0 (gint32
, acfg
->nmethods
);
10591 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
10592 if (acfg
->cfgs
[i
]) {
10593 MonoCompile
*cfg
= acfg
->cfgs
[i
];
10595 // By design aot-runtime decode_exception_debug_info is not able to load sequence point debug data from a file.
10596 // As it is not possible to load debug data from a file its is also not possible to store it in a file.
10597 gboolean method_seq_points_to_file
= acfg
->aot_opts
.gen_msym_dir
&&
10598 cfg
->gen_seq_points
&& !cfg
->gen_sdb_seq_points
;
10599 gboolean method_seq_points_to_binary
= cfg
->gen_seq_points
&& !method_seq_points_to_file
;
10601 emit_exception_debug_info (acfg
, cfg
, method_seq_points_to_binary
);
10602 offsets
[i
] = cfg
->ex_info_offset
;
10604 if (method_seq_points_to_file
) {
10605 if (!seq_points_to_file
) {
10606 mono_seq_point_data_init (&sp_data
, acfg
->nmethods
);
10607 seq_points_to_file
= TRUE
;
10609 mono_seq_point_data_add (&sp_data
, cfg
->method
->token
, cfg
->method_index
, cfg
->seq_point_info
);
10616 if (seq_points_to_file
) {
10617 char *aotid
= mono_guid_to_string_minimal (acfg
->image
->aotid
);
10618 char *dir
= g_build_filename (acfg
->aot_opts
.gen_msym_dir_path
, aotid
, (const char*)NULL
);
10619 char *image_basename
= g_path_get_basename (acfg
->image
->name
);
10620 char *aot_file
= g_strdup_printf("%s%s", image_basename
, SEQ_POINT_AOT_EXT
);
10621 char *aot_file_path
= g_build_filename (dir
, aot_file
, (const char*)NULL
);
10623 if (g_ensure_directory_exists (aot_file_path
) == FALSE
) {
10624 fprintf (stderr
, "AOT : failed to create msym directory: %s\n", aot_file_path
);
10628 mono_seq_point_data_write (&sp_data
, aot_file_path
);
10629 mono_seq_point_data_free (&sp_data
);
10633 g_free (image_basename
);
10635 g_free (aot_file_path
);
10638 acfg
->stats
.offsets_size
+= emit_offset_table (acfg
, "ex_info_offsets", MONO_AOT_TABLE_EX_INFO_OFFSETS
, acfg
->nmethods
, 10, offsets
);
10643 emit_unwind_info (MonoAotCompile
*acfg
)
10648 if (acfg
->aot_opts
.llvm_only
) {
10649 g_assert (acfg
->unwind_ops
->len
== 0);
10654 * The unwind info contains a lot of duplicates so we emit each unique
10655 * entry once, and only store the offset from the start of the table in the
10659 sprintf (symbol
, "unwind_info");
10660 emit_section_change (acfg
, RODATA_SECT
, 1);
10661 emit_alignment (acfg
, 8);
10662 emit_info_symbol (acfg
, symbol
, TRUE
);
10664 for (i
= 0; i
< acfg
->unwind_ops
->len
; ++i
) {
10665 guint32 index
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->unwind_ops
, i
));
10666 guint8
*unwind_info
;
10667 guint32 unwind_info_len
;
10671 unwind_info
= mono_get_cached_unwind_info (index
, &unwind_info_len
);
10674 encode_value (unwind_info_len
, p
, &p
);
10675 emit_bytes (acfg
, buf
, p
- buf
);
10676 emit_bytes (acfg
, unwind_info
, unwind_info_len
);
10678 acfg
->stats
.unwind_info_size
+= (p
- buf
) + unwind_info_len
;
10683 emit_class_info (MonoAotCompile
*acfg
)
10688 offsets
= g_new0 (gint32
, acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
);
10689 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
)
10690 offsets
[i
] = emit_klass_info (acfg
, MONO_TOKEN_TYPE_DEF
| (i
+ 1));
10692 acfg
->stats
.offsets_size
+= emit_offset_table (acfg
, "class_info_offsets", MONO_AOT_TABLE_CLASS_INFO_OFFSETS
, acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
, 10, offsets
);
10696 typedef struct ClassNameTableEntry
{
10697 guint32 token
, index
;
10698 struct ClassNameTableEntry
*next
;
10699 } ClassNameTableEntry
;
10702 emit_class_name_table (MonoAotCompile
*acfg
)
10704 int i
, table_size
, buf_size
;
10705 guint32 token
, hash
;
10710 ClassNameTableEntry
*entry
, *new_entry
;
10713 * Construct a chained hash table for mapping class names to typedef tokens.
10715 table_size
= g_spaced_primes_closest ((int)(acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
* 1.5));
10716 table
= g_ptr_array_sized_new (table_size
);
10717 for (i
= 0; i
< table_size
; ++i
)
10718 g_ptr_array_add (table
, NULL
);
10719 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
) {
10720 ERROR_DECL (error
);
10721 token
= MONO_TOKEN_TYPE_DEF
| (i
+ 1);
10722 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
10724 mono_error_cleanup (error
);
10727 full_name
= mono_type_get_name_full (m_class_get_byval_arg (klass
), MONO_TYPE_NAME_FORMAT_FULL_NAME
);
10728 hash
= mono_metadata_str_hash (full_name
) % table_size
;
10729 g_free (full_name
);
10731 /* FIXME: Allocate from the mempool */
10732 new_entry
= g_new0 (ClassNameTableEntry
, 1);
10733 new_entry
->token
= token
;
10735 entry
= (ClassNameTableEntry
*)g_ptr_array_index (table
, hash
);
10736 if (entry
== NULL
) {
10737 new_entry
->index
= hash
;
10738 g_ptr_array_index (table
, hash
) = new_entry
;
10740 while (entry
->next
)
10741 entry
= entry
->next
;
10743 entry
->next
= new_entry
;
10744 new_entry
->index
= table
->len
;
10745 g_ptr_array_add (table
, new_entry
);
10749 /* Emit the table */
10750 buf_size
= table
->len
* 4 + 4;
10751 p
= buf
= (guint8
*)g_malloc0 (buf_size
);
10753 /* FIXME: Optimize memory usage */
10754 g_assert (table_size
< 65000);
10755 encode_int16 (table_size
, p
, &p
);
10756 g_assert (table
->len
< 65000);
10757 for (i
= 0; i
< table
->len
; ++i
) {
10758 ClassNameTableEntry
*entry
= (ClassNameTableEntry
*)g_ptr_array_index (table
, i
);
10760 if (entry
== NULL
) {
10761 encode_int16 (0, p
, &p
);
10762 encode_int16 (0, p
, &p
);
10764 encode_int16 (mono_metadata_token_index (entry
->token
), p
, &p
);
10766 encode_int16 (entry
->next
->index
, p
, &p
);
10768 encode_int16 (0, p
, &p
);
10772 g_assert (p
- buf
<= buf_size
);
10773 g_ptr_array_free (table
, TRUE
);
10775 emit_aot_data (acfg
, MONO_AOT_TABLE_CLASS_NAME
, "class_name_table", buf
, p
- buf
);
10781 emit_image_table (MonoAotCompile
*acfg
)
10787 * The image table is small but referenced in a lot of places.
10788 * So we emit it at once, and reference its elements by an index.
10790 buf_size
= acfg
->image_table
->len
* 28 + 4;
10791 for (i
= 0; i
< acfg
->image_table
->len
; i
++) {
10792 MonoImage
*image
= (MonoImage
*)g_ptr_array_index (acfg
->image_table
, i
);
10793 MonoAssemblyName
*aname
= &image
->assembly
->aname
;
10795 buf_size
+= strlen (image
->assembly_name
) + strlen (image
->guid
) + (aname
->culture
? strlen (aname
->culture
) : 1) + strlen ((char*)aname
->public_key_token
) + 4;
10798 buf
= p
= (guint8
*)g_malloc0 (buf_size
);
10799 encode_int (acfg
->image_table
->len
, p
, &p
);
10800 for (i
= 0; i
< acfg
->image_table
->len
; i
++) {
10801 MonoImage
*image
= (MonoImage
*)g_ptr_array_index (acfg
->image_table
, i
);
10802 MonoAssemblyName
*aname
= &image
->assembly
->aname
;
10804 /* FIXME: Support multi-module assemblies */
10805 g_assert (image
->assembly
->image
== image
);
10807 encode_string (image
->assembly_name
, p
, &p
);
10808 encode_string (image
->guid
, p
, &p
);
10809 encode_string (aname
->culture
? aname
->culture
: "", p
, &p
);
10810 encode_string ((const char*)aname
->public_key_token
, p
, &p
);
10812 while (GPOINTER_TO_UINT (p
) % 8 != 0)
10815 encode_int (aname
->flags
, p
, &p
);
10816 encode_int (aname
->major
, p
, &p
);
10817 encode_int (aname
->minor
, p
, &p
);
10818 encode_int (aname
->build
, p
, &p
);
10819 encode_int (aname
->revision
, p
, &p
);
10821 g_assert (p
- buf
<= buf_size
);
10823 emit_aot_data (acfg
, MONO_AOT_TABLE_IMAGE_TABLE
, "image_table", buf
, p
- buf
);
10829 emit_weak_field_indexes (MonoAotCompile
*acfg
)
10831 GHashTable
*indexes
;
10832 GHashTableIter iter
;
10833 gpointer key
, value
;
10837 /* Emit a table of weak field indexes, since computing these at runtime is expensive */
10838 mono_assembly_init_weak_fields (acfg
->image
);
10839 indexes
= acfg
->image
->weak_field_indexes
;
10840 g_assert (indexes
);
10842 buf_size
= (g_hash_table_size (indexes
) + 1) * 4;
10843 buf
= p
= (guint8
*)g_malloc0 (buf_size
);
10845 encode_int (g_hash_table_size (indexes
), p
, &p
);
10846 g_hash_table_iter_init (&iter
, indexes
);
10847 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
10848 guint32 index
= GPOINTER_TO_UINT (key
);
10849 encode_int (index
, p
, &p
);
10851 g_assert (p
- buf
<= buf_size
);
10853 emit_aot_data (acfg
, MONO_AOT_TABLE_WEAK_FIELD_INDEXES
, "weak_field_indexes", buf
, p
- buf
);
10859 emit_got_info (MonoAotCompile
*acfg
, gboolean llvm
)
10861 int i
, first_plt_got_patch
= 0, buf_size
;
10863 guint32
*got_info_offsets
;
10864 GotInfo
*info
= llvm
? &acfg
->llvm_got_info
: &acfg
->got_info
;
10866 /* Add the patches needed by the PLT to the GOT */
10868 acfg
->plt_got_offset_base
= acfg
->got_offset
;
10869 first_plt_got_patch
= info
->got_patches
->len
;
10870 for (i
= 1; i
< acfg
->plt_offset
; ++i
) {
10871 MonoPltEntry
*plt_entry
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
10873 g_ptr_array_add (info
->got_patches
, plt_entry
->ji
);
10875 acfg
->stats
.got_slot_types
[plt_entry
->ji
->type
] ++;
10878 acfg
->got_offset
+= acfg
->plt_offset
;
10883 * - optimize offsets table.
10884 * - reduce number of exported symbols.
10885 * - emit info for a klass only once.
10886 * - determine when a method uses a GOT slot which is guaranteed to be already
10888 * - clean up and document the code.
10889 * - use String.Empty in class libs.
10892 /* Encode info required to decode shared GOT entries */
10893 buf_size
= info
->got_patches
->len
* 128;
10894 p
= buf
= (guint8
*)mono_mempool_alloc (acfg
->mempool
, buf_size
);
10895 got_info_offsets
= (guint32
*)mono_mempool_alloc (acfg
->mempool
, info
->got_patches
->len
* sizeof (guint32
));
10897 acfg
->plt_got_info_offsets
= (guint32
*)mono_mempool_alloc (acfg
->mempool
, acfg
->plt_offset
* sizeof (guint32
));
10899 if (acfg
->plt_offset
)
10900 acfg
->plt_got_info_offsets
[0] = 0;
10902 for (i
= 0; i
< info
->got_patches
->len
; ++i
) {
10903 MonoJumpInfo
*ji
= (MonoJumpInfo
*)g_ptr_array_index (info
->got_patches
, i
);
10908 encode_value (ji
->type
, p
, &p
);
10910 encode_patch (acfg
, ji
, p
, &p
);
10911 acfg
->stats
.got_slot_info_sizes
[ji
->type
] += p
- p2
;
10912 g_assert (p
- buf
<= buf_size
);
10913 got_info_offsets
[i
] = add_to_blob (acfg
, buf
, p
- buf
);
10915 if (!llvm
&& i
>= first_plt_got_patch
)
10916 acfg
->plt_got_info_offsets
[i
- first_plt_got_patch
+ 1] = got_info_offsets
[i
];
10917 acfg
->stats
.got_info_size
+= p
- buf
;
10920 /* Emit got_info_offsets table */
10922 /* No need to emit offsets for the got plt entries, the plt embeds them directly */
10923 acfg
->stats
.offsets_size
+= emit_offset_table (acfg
, llvm
? "llvm_got_info_offsets" : "got_info_offsets", llvm
? MONO_AOT_TABLE_LLVM_GOT_INFO_OFFSETS
: MONO_AOT_TABLE_GOT_INFO_OFFSETS
, llvm
? acfg
->llvm_got_offset
: first_plt_got_patch
, 10, (gint32
*)got_info_offsets
);
10927 emit_got (MonoAotCompile
*acfg
)
10929 char symbol
[MAX_SYMBOL_SIZE
];
10931 if (acfg
->aot_opts
.llvm_only
)
10934 /* Don't make GOT global so accesses to it don't need relocations */
10935 sprintf (symbol
, "%s", acfg
->got_symbol
);
10938 emit_unset_mode (acfg
);
10939 fprintf (acfg
->fp
, ".section __DATA, __bss\n");
10940 emit_alignment (acfg
, 8);
10942 emit_info_symbol (acfg
, "jit_got", FALSE
);
10943 fprintf (acfg
->fp
, ".lcomm %s, %d\n", acfg
->got_symbol
, (int)(acfg
->got_offset
* sizeof (target_mgreg_t
)));
10945 emit_section_change (acfg
, ".bss", 0);
10946 emit_alignment (acfg
, 8);
10947 if (acfg
->aot_opts
.write_symbols
)
10948 emit_local_symbol (acfg
, symbol
, "got_end", FALSE
);
10949 emit_label (acfg
, symbol
);
10951 emit_info_symbol (acfg
, "jit_got", FALSE
);
10952 if (acfg
->got_offset
> 0)
10953 emit_zero_bytes (acfg
, (int)(acfg
->got_offset
* sizeof (target_mgreg_t
)));
10956 sprintf (symbol
, "got_end");
10957 emit_label (acfg
, symbol
);
10960 typedef struct GlobalsTableEntry
{
10961 guint32 value
, index
;
10962 struct GlobalsTableEntry
*next
;
10963 } GlobalsTableEntry
;
10965 #ifdef TARGET_WIN32_MSVC
10966 #define DLL_ENTRY_POINT "DllMain"
10969 emit_library_info (MonoAotCompile
*acfg
)
10971 // Only include for shared libraries linked directly from generated object.
10972 if (link_shared_library (acfg
)) {
10974 char symbol
[MAX_SYMBOL_SIZE
];
10976 // Ask linker to export all global symbols.
10977 emit_section_change (acfg
, ".drectve", 0);
10978 for (guint i
= 0; i
< acfg
->globals
->len
; ++i
) {
10979 name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
10980 g_assert (name
!= NULL
);
10981 sprintf_s (symbol
, MAX_SYMBOL_SIZE
, " /EXPORT:%s", name
);
10982 emit_string (acfg
, symbol
);
10985 // Emit DLLMain function, needed by MSVC linker for DLL's.
10986 // NOTE, DllMain should not go into exports above.
10987 emit_section_change (acfg
, ".text", 0);
10988 emit_global (acfg
, DLL_ENTRY_POINT
, TRUE
);
10989 emit_label (acfg
, DLL_ENTRY_POINT
);
10991 // Simple implementation of DLLMain, just returning TRUE.
10992 // For more information about DLLMain: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx
10993 fprintf (acfg
->fp
, "movl $1, %%eax\n");
10994 fprintf (acfg
->fp
, "ret\n");
10996 // Inform linker about our dll entry function.
10997 emit_section_change (acfg
, ".drectve", 0);
10998 emit_string (acfg
, "/ENTRY:" DLL_ENTRY_POINT
);
11006 emit_library_info (MonoAotCompile
*acfg
)
11013 emit_globals (MonoAotCompile
*acfg
)
11018 char symbol
[1024];
11019 GlobalsTableEntry
*entry
, *new_entry
;
11021 if (!acfg
->aot_opts
.static_link
)
11024 if (acfg
->aot_opts
.llvm_only
) {
11025 g_assert (acfg
->globals
->len
== 0);
11030 * When static linking, we emit a table containing our globals.
11034 * Construct a chained hash table for mapping global names to their index in
11035 * the globals table.
11037 table_size
= g_spaced_primes_closest ((int)(acfg
->globals
->len
* 1.5));
11038 table
= g_ptr_array_sized_new (table_size
);
11039 for (i
= 0; i
< table_size
; ++i
)
11040 g_ptr_array_add (table
, NULL
);
11041 for (i
= 0; i
< acfg
->globals
->len
; ++i
) {
11042 char *name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
11044 hash
= mono_metadata_str_hash (name
) % table_size
;
11046 /* FIXME: Allocate from the mempool */
11047 new_entry
= g_new0 (GlobalsTableEntry
, 1);
11048 new_entry
->value
= i
;
11050 entry
= (GlobalsTableEntry
*)g_ptr_array_index (table
, hash
);
11051 if (entry
== NULL
) {
11052 new_entry
->index
= hash
;
11053 g_ptr_array_index (table
, hash
) = new_entry
;
11055 while (entry
->next
)
11056 entry
= entry
->next
;
11058 entry
->next
= new_entry
;
11059 new_entry
->index
= table
->len
;
11060 g_ptr_array_add (table
, new_entry
);
11064 /* Emit the table */
11065 sprintf (symbol
, ".Lglobals_hash");
11066 emit_section_change (acfg
, RODATA_SECT
, 0);
11067 emit_alignment (acfg
, 8);
11068 emit_label (acfg
, symbol
);
11070 /* FIXME: Optimize memory usage */
11071 g_assert (table_size
< 65000);
11072 emit_int16 (acfg
, table_size
);
11073 for (i
= 0; i
< table
->len
; ++i
) {
11074 GlobalsTableEntry
*entry
= (GlobalsTableEntry
*)g_ptr_array_index (table
, i
);
11076 if (entry
== NULL
) {
11077 emit_int16 (acfg
, 0);
11078 emit_int16 (acfg
, 0);
11080 emit_int16 (acfg
, entry
->value
+ 1);
11082 emit_int16 (acfg
, entry
->next
->index
);
11084 emit_int16 (acfg
, 0);
11088 /* Emit the names */
11089 for (i
= 0; i
< acfg
->globals
->len
; ++i
) {
11090 char *name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
11092 sprintf (symbol
, "name_%d", i
);
11093 emit_section_change (acfg
, RODATA_SECT
, 1);
11095 emit_alignment (acfg
, 4);
11097 emit_label (acfg
, symbol
);
11098 emit_string (acfg
, name
);
11101 /* Emit the globals table */
11102 sprintf (symbol
, "globals");
11103 emit_section_change (acfg
, ".data", 0);
11104 /* This is not a global, since it is accessed by the init function */
11105 emit_alignment (acfg
, 8);
11106 emit_info_symbol (acfg
, symbol
, FALSE
);
11108 sprintf (symbol
, "%sglobals_hash", acfg
->temp_prefix
);
11109 emit_pointer (acfg
, symbol
);
11111 for (i
= 0; i
< acfg
->globals
->len
; ++i
) {
11112 char *name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
11114 sprintf (symbol
, "name_%d", i
);
11115 emit_pointer (acfg
, symbol
);
11117 g_assert (strlen (name
) < sizeof (symbol
));
11118 sprintf (symbol
, "%s", name
);
11119 emit_pointer (acfg
, symbol
);
11121 /* Null terminate the table */
11122 emit_int32 (acfg
, 0);
11123 emit_int32 (acfg
, 0);
11127 emit_mem_end (MonoAotCompile
*acfg
)
11131 if (acfg
->aot_opts
.llvm_only
)
11134 sprintf (symbol
, "mem_end");
11135 emit_section_change (acfg
, ".text", 1);
11136 emit_alignment_code (acfg
, 8);
11137 emit_label (acfg
, symbol
);
11141 init_aot_file_info (MonoAotCompile
*acfg
, MonoAotFileInfo
*info
)
11145 info
->version
= MONO_AOT_FILE_VERSION
;
11146 info
->plt_got_offset_base
= acfg
->plt_got_offset_base
;
11147 info
->got_size
= acfg
->got_offset
* sizeof (target_mgreg_t
);
11148 info
->plt_size
= acfg
->plt_offset
;
11149 info
->nmethods
= acfg
->nmethods
;
11150 info
->call_table_entry_size
= acfg
->call_table_entry_size
;
11151 info
->nextra_methods
= acfg
->nextra_methods
;
11152 info
->flags
= acfg
->flags
;
11153 info
->opts
= acfg
->opts
;
11154 info
->simd_opts
= acfg
->simd_opts
;
11155 info
->gc_name_index
= acfg
->gc_name_offset
;
11156 info
->datafile_size
= acfg
->datafile_offset
;
11157 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
11158 info
->table_offsets
[i
] = acfg
->table_offsets
[i
];
11159 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11160 info
->num_trampolines
[i
] = acfg
->num_trampolines
[i
];
11161 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11162 info
->trampoline_got_offset_base
[i
] = acfg
->trampoline_got_offset_base
[i
];
11163 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11164 info
->trampoline_size
[i
] = acfg
->trampoline_size
[i
];
11165 info
->num_rgctx_fetch_trampolines
= acfg
->aot_opts
.nrgctx_fetch_trampolines
;
11167 int card_table_shift_bits
= 0;
11168 target_mgreg_t card_table_mask
= 0;
11170 mono_gc_get_target_card_table (&card_table_shift_bits
, &card_table_mask
);
11173 * Sanity checking variables used to make sure the host and target
11174 * environment matches when cross compiling.
11176 info
->double_align
= MONO_ABI_ALIGNOF (double);
11177 info
->long_align
= MONO_ABI_ALIGNOF (gint64
);
11178 info
->generic_tramp_num
= MONO_TRAMPOLINE_NUM
;
11179 info
->card_table_shift_bits
= card_table_shift_bits
;
11180 info
->card_table_mask
= card_table_mask
;
11182 info
->tramp_page_size
= acfg
->tramp_page_size
;
11183 info
->nshared_got_entries
= acfg
->nshared_got_entries
;
11184 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11185 info
->tramp_page_code_offsets
[i
] = acfg
->tramp_page_code_offsets
[i
];
11187 memcpy(&info
->aotid
, acfg
->image
->aotid
, 16);
11191 emit_aot_file_info (MonoAotCompile
*acfg
, MonoAotFileInfo
*info
)
11193 char symbol
[MAX_SYMBOL_SIZE
];
11195 const char **symbols
;
11197 symbols
= g_new0 (const char *, MONO_AOT_FILE_INFO_NUM_SYMBOLS
);
11199 symbols
[sindex
++] = acfg
->got_symbol
;
11201 symbols
[sindex
++] = g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, acfg
->llvm_got_symbol
);
11202 symbols
[sindex
++] = acfg
->llvm_eh_frame_symbol
;
11204 symbols
[sindex
++] = NULL
;
11205 symbols
[sindex
++] = NULL
;
11207 /* llvm_get_method */
11208 symbols
[sindex
++] = NULL
;
11209 /* llvm_get_unbox_tramp */
11210 symbols
[sindex
++] = NULL
;
11211 if (!acfg
->aot_opts
.llvm_only
) {
11212 symbols
[sindex
++] = "jit_code_start";
11213 symbols
[sindex
++] = "jit_code_end";
11214 symbols
[sindex
++] = "method_addresses";
11216 symbols
[sindex
++] = NULL
;
11217 symbols
[sindex
++] = NULL
;
11218 symbols
[sindex
++] = NULL
;
11220 symbols
[sindex
++] = NULL
;
11221 symbols
[sindex
++] = NULL
;
11223 if (acfg
->data_outfile
) {
11224 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
11225 symbols
[sindex
++] = NULL
;
11227 symbols
[sindex
++] = "blob";
11228 symbols
[sindex
++] = "class_name_table";
11229 symbols
[sindex
++] = "class_info_offsets";
11230 symbols
[sindex
++] = "method_info_offsets";
11231 symbols
[sindex
++] = "ex_info_offsets";
11232 symbols
[sindex
++] = "extra_method_info_offsets";
11233 symbols
[sindex
++] = "extra_method_table";
11234 symbols
[sindex
++] = "got_info_offsets";
11236 symbols
[sindex
++] = "llvm_got_info_offsets";
11238 symbols
[sindex
++] = NULL
;
11239 symbols
[sindex
++] = "image_table";
11240 symbols
[sindex
++] = "weak_field_indexes";
11243 symbols
[sindex
++] = "mem_end";
11244 symbols
[sindex
++] = "assembly_guid";
11245 symbols
[sindex
++] = "runtime_version";
11246 if (acfg
->num_trampoline_got_entries
) {
11247 symbols
[sindex
++] = "specific_trampolines";
11248 symbols
[sindex
++] = "static_rgctx_trampolines";
11249 symbols
[sindex
++] = "imt_trampolines";
11250 symbols
[sindex
++] = "gsharedvt_arg_trampolines";
11251 symbols
[sindex
++] = "ftnptr_arg_trampolines";
11252 symbols
[sindex
++] = "unbox_arbitrary_trampolines";
11254 symbols
[sindex
++] = NULL
;
11255 symbols
[sindex
++] = NULL
;
11256 symbols
[sindex
++] = NULL
;
11257 symbols
[sindex
++] = NULL
;
11258 symbols
[sindex
++] = NULL
;
11259 symbols
[sindex
++] = NULL
;
11261 if (acfg
->aot_opts
.static_link
) {
11262 symbols
[sindex
++] = "globals";
11264 symbols
[sindex
++] = NULL
;
11266 symbols
[sindex
++] = "assembly_name";
11267 symbols
[sindex
++] = "plt";
11268 symbols
[sindex
++] = "plt_end";
11269 symbols
[sindex
++] = "unwind_info";
11270 if (!acfg
->aot_opts
.llvm_only
) {
11271 symbols
[sindex
++] = "unbox_trampolines";
11272 symbols
[sindex
++] = "unbox_trampolines_end";
11273 symbols
[sindex
++] = "unbox_trampoline_addresses";
11275 symbols
[sindex
++] = NULL
;
11276 symbols
[sindex
++] = NULL
;
11277 symbols
[sindex
++] = NULL
;
11280 g_assert (sindex
== MONO_AOT_FILE_INFO_NUM_SYMBOLS
);
11282 sprintf (symbol
, "%smono_aot_file_info", acfg
->user_symbol_prefix
);
11283 emit_section_change (acfg
, ".data", 0);
11284 emit_alignment (acfg
, 8);
11285 emit_label (acfg
, symbol
);
11286 if (!acfg
->aot_opts
.static_link
)
11287 emit_global (acfg
, symbol
, FALSE
);
11289 /* The data emitted here must match MonoAotFileInfo. */
11291 emit_int32 (acfg
, info
->version
);
11292 emit_int32 (acfg
, info
->dummy
);
11295 * We emit pointers to our data structures instead of emitting global symbols which
11296 * point to them, to reduce the number of globals, and because using globals leads to
11297 * various problems (i.e. arm/thumb).
11299 for (i
= 0; i
< MONO_AOT_FILE_INFO_NUM_SYMBOLS
; ++i
)
11300 emit_pointer (acfg
, symbols
[i
]);
11302 emit_int32 (acfg
, info
->plt_got_offset_base
);
11303 emit_int32 (acfg
, info
->got_size
);
11304 emit_int32 (acfg
, info
->plt_size
);
11305 emit_int32 (acfg
, info
->nmethods
);
11306 emit_int32 (acfg
, info
->nextra_methods
);
11307 emit_int32 (acfg
, info
->flags
);
11308 emit_int32 (acfg
, info
->opts
);
11309 emit_int32 (acfg
, info
->simd_opts
);
11310 emit_int32 (acfg
, info
->gc_name_index
);
11311 emit_int32 (acfg
, info
->num_rgctx_fetch_trampolines
);
11312 emit_int32 (acfg
, info
->double_align
);
11313 emit_int32 (acfg
, info
->long_align
);
11314 emit_int32 (acfg
, info
->generic_tramp_num
);
11315 emit_int32 (acfg
, info
->card_table_shift_bits
);
11316 emit_int32 (acfg
, info
->card_table_mask
);
11317 emit_int32 (acfg
, info
->tramp_page_size
);
11318 emit_int32 (acfg
, info
->call_table_entry_size
);
11319 emit_int32 (acfg
, info
->nshared_got_entries
);
11320 emit_int32 (acfg
, info
->datafile_size
);
11321 emit_int32 (acfg
, 0);
11322 emit_int32 (acfg
, 0);
11324 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
11325 emit_int32 (acfg
, info
->table_offsets
[i
]);
11326 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11327 emit_int32 (acfg
, info
->num_trampolines
[i
]);
11328 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11329 emit_int32 (acfg
, info
->trampoline_got_offset_base
[i
]);
11330 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11331 emit_int32 (acfg
, info
->trampoline_size
[i
]);
11332 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11333 emit_int32 (acfg
, info
->tramp_page_code_offsets
[i
]);
11335 emit_bytes (acfg
, info
->aotid
, 16);
11337 if (acfg
->aot_opts
.static_link
) {
11338 emit_global_inner (acfg
, acfg
->static_linking_symbol
, FALSE
);
11339 emit_alignment (acfg
, sizeof (target_mgreg_t
));
11340 emit_label (acfg
, acfg
->static_linking_symbol
);
11341 emit_pointer_2 (acfg
, acfg
->user_symbol_prefix
, "mono_aot_file_info");
11346 * Emit a structure containing all the information not stored elsewhere.
11349 emit_file_info (MonoAotCompile
*acfg
)
11352 MonoAotFileInfo
*info
;
11354 if (acfg
->aot_opts
.bind_to_runtime_version
) {
11355 build_info
= mono_get_runtime_build_info ();
11356 emit_string_symbol (acfg
, "runtime_version", build_info
);
11357 g_free (build_info
);
11359 emit_string_symbol (acfg
, "runtime_version", "");
11362 emit_string_symbol (acfg
, "assembly_guid" , acfg
->image
->guid
);
11364 /* Emit a string holding the assembly name */
11365 emit_string_symbol (acfg
, "assembly_name", acfg
->image
->assembly
->aname
.name
);
11367 info
= g_new0 (MonoAotFileInfo
, 1);
11368 init_aot_file_info (acfg
, info
);
11370 if (acfg
->aot_opts
.static_link
) {
11371 char symbol
[MAX_SYMBOL_SIZE
];
11375 * Emit a global symbol which can be passed by an embedding app to
11376 * mono_aot_register_module (). The symbol points to a pointer to the the file info
11379 sprintf (symbol
, "%smono_aot_module_%s_info", acfg
->user_symbol_prefix
, acfg
->image
->assembly
->aname
.name
);
11381 /* Get rid of characters which cannot occur in symbols */
11383 for (p
= symbol
; *p
; ++p
) {
11384 if (!(isalnum (*p
) || *p
== '_'))
11387 acfg
->static_linking_symbol
= g_strdup (symbol
);
11391 mono_llvm_emit_aot_file_info (info
, acfg
->has_jitted_code
);
11393 emit_aot_file_info (acfg
, info
);
11397 emit_blob (MonoAotCompile
*acfg
)
11399 acfg
->blob_closed
= TRUE
;
11401 emit_aot_data (acfg
, MONO_AOT_TABLE_BLOB
, "blob", (guint8
*)acfg
->blob
.data
, acfg
->blob
.index
);
11405 emit_objc_selectors (MonoAotCompile
*acfg
)
11410 if (!acfg
->objc_selectors
|| acfg
->objc_selectors
->len
== 0)
11415 * cat > foo.m << EOF
11418 * return @selector(print:);
11423 mono_img_writer_emit_unset_mode (acfg
->w
);
11424 g_assert (acfg
->fp
);
11425 fprintf (acfg
->fp
, ".section __DATA,__objc_selrefs,literal_pointers,no_dead_strip\n");
11426 fprintf (acfg
->fp
, ".align 3\n");
11427 for (i
= 0; i
< acfg
->objc_selectors
->len
; ++i
) {
11428 sprintf (symbol
, "L_OBJC_SELECTOR_REFERENCES_%d", i
);
11429 emit_label (acfg
, symbol
);
11430 sprintf (symbol
, "L_OBJC_METH_VAR_NAME_%d", i
);
11431 emit_pointer (acfg
, symbol
);
11434 fprintf (acfg
->fp
, ".section __TEXT,__cstring,cstring_literals\n");
11435 for (i
= 0; i
< acfg
->objc_selectors
->len
; ++i
) {
11436 fprintf (acfg
->fp
, "L_OBJC_METH_VAR_NAME_%d:\n", i
);
11437 fprintf (acfg
->fp
, ".asciz \"%s\"\n", (char*)g_ptr_array_index (acfg
->objc_selectors
, i
));
11440 fprintf (acfg
->fp
, ".section __DATA,__objc_imageinfo,regular,no_dead_strip\n");
11441 fprintf (acfg
->fp
, ".align 3\n");
11442 fprintf (acfg
->fp
, "L_OBJC_IMAGE_INFO:\n");
11443 fprintf (acfg
->fp
, ".long 0\n");
11444 fprintf (acfg
->fp
, ".long 16\n");
11448 emit_dwarf_info (MonoAotCompile
*acfg
)
11450 #ifdef EMIT_DWARF_INFO
11452 char symbol2
[128];
11454 /* DIEs for methods */
11455 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
11456 MonoCompile
*cfg
= acfg
->cfgs
[i
];
11458 if (ignore_cfg (cfg
))
11461 // FIXME: LLVM doesn't define .Lme_...
11462 if (cfg
->compile_llvm
)
11465 sprintf (symbol2
, "%sme_%x", acfg
->temp_prefix
, i
);
11467 mono_dwarf_writer_emit_method (acfg
->dwarf
, cfg
, cfg
->method
, cfg
->asm_symbol
, symbol2
, cfg
->asm_debug_symbol
, (guint8
*)cfg
->jit_info
->code_start
, cfg
->jit_info
->code_size
, cfg
->args
, cfg
->locals
, cfg
->unwind_ops
, mono_debug_find_method (cfg
->jit_info
->d
.method
, mono_domain_get ()));
11472 #ifdef EMIT_WIN32_CODEVIEW_INFO
11473 typedef struct _CodeViewSubSectionData
11475 gchar
*start_section
;
11476 gchar
*end_section
;
11477 gchar
*start_section_record
;
11478 gchar
*end_section_record
;
11480 int section_record_type
;
11482 } CodeViewSubsectionData
;
11484 typedef struct _CodeViewCompilerVersion
11490 } CodeViewCompilerVersion
;
11492 #define CODEVIEW_SUBSECTION_SYMBOL_TYPE 0xF1
11493 #define CODEVIEW_SUBSECTION_RECORD_COMPILER_TYPE 0x113c
11494 #define CODEVIEW_SUBSECTION_RECORD_FUNCTION_START_TYPE 0x1147
11495 #define CODEVIEW_SUBSECTION_RECORD_FUNCTION_END_TYPE 0x114F
11496 #define CODEVIEW_CSHARP_LANGUAGE_TYPE 0x0A
11497 #define CODEVIEW_CPU_TYPE 0x0
11498 #define CODEVIEW_MAGIC_HEADER 0x4
11501 codeview_clear_subsection_data (CodeViewSubsectionData
*section_data
)
11503 g_free (section_data
->start_section
);
11504 g_free (section_data
->end_section
);
11505 g_free (section_data
->start_section_record
);
11506 g_free (section_data
->end_section_record
);
11508 memset (section_data
, 0, sizeof (CodeViewSubsectionData
));
11512 codeview_parse_compiler_version (gchar
*version
, CodeViewCompilerVersion
*data
)
11514 gint values
[4] = { 0 };
11515 gint
*value
= values
;
11517 while (*version
&& (value
< values
+ G_N_ELEMENTS (values
))) {
11518 if (isdigit (*version
)) {
11520 *value
+= *version
- '0';
11522 else if (*version
== '.') {
11529 data
->major
= values
[0];
11530 data
->minor
= values
[1];
11531 data
->revision
= values
[2];
11532 data
->patch
= values
[3];
11536 emit_codeview_start_subsection (MonoAotCompile
*acfg
, int section_id
, int section_type
, int section_record_type
, CodeViewSubsectionData
*section_data
)
11538 // Starting a new subsection, clear old data.
11539 codeview_clear_subsection_data (section_data
);
11541 // Keep subsection data.
11542 section_data
->section_id
= section_id
;
11543 section_data
->section_type
= section_type
;
11544 section_data
->section_record_type
= section_record_type
;
11546 // Allocate all labels used in subsection.
11547 section_data
->start_section
= g_strdup_printf ("%scvs_%d", acfg
->temp_prefix
, section_data
->section_id
);
11548 section_data
->end_section
= g_strdup_printf ("%scvse_%d", acfg
->temp_prefix
, section_data
->section_id
);
11549 section_data
->start_section_record
= g_strdup_printf ("%scvsr_%d", acfg
->temp_prefix
, section_data
->section_id
);
11550 section_data
->end_section_record
= g_strdup_printf ("%scvsre_%d", acfg
->temp_prefix
, section_data
->section_id
);
11552 // Subsection type, function symbol.
11553 emit_int32 (acfg
, section_data
->section_type
);
11555 // Subsection size.
11556 emit_symbol_diff (acfg
, section_data
->end_section
, section_data
->start_section
, 0);
11557 emit_label (acfg
, section_data
->start_section
);
11559 // Subsection record size.
11560 fprintf (acfg
->fp
, "\t.word %s - %s\n", section_data
->end_section_record
, section_data
->start_section_record
);
11561 emit_label (acfg
, section_data
->start_section_record
);
11563 // Subsection record type.
11564 emit_int16 (acfg
, section_record_type
);
11568 emit_codeview_end_subsection (MonoAotCompile
*acfg
, CodeViewSubsectionData
*section_data
, int *section_id
)
11570 g_assert (section_data
->start_section
);
11571 g_assert (section_data
->end_section
);
11572 g_assert (section_data
->start_section_record
);
11573 g_assert (section_data
->end_section_record
);
11575 emit_label (acfg
, section_data
->end_section_record
);
11577 if (section_data
->section_record_type
== CODEVIEW_SUBSECTION_RECORD_FUNCTION_START_TYPE
) {
11578 // Emit record length.
11579 emit_int16 (acfg
, 2);
11581 // Emit specific record type end.
11582 emit_int16 (acfg
, CODEVIEW_SUBSECTION_RECORD_FUNCTION_END_TYPE
);
11585 emit_label (acfg
, section_data
->end_section
);
11587 // Next subsection needs to be 4 byte aligned.
11588 emit_alignment (acfg
, 4);
11590 *section_id
= section_data
->section_id
+ 1;
11591 codeview_clear_subsection_data (section_data
);
11595 emit_codeview_start_symbol_subsection (MonoAotCompile
*acfg
, int section_id
, int section_record_type
, CodeViewSubsectionData
*section_data
)
11597 emit_codeview_start_subsection (acfg
, section_id
, CODEVIEW_SUBSECTION_SYMBOL_TYPE
, section_record_type
, section_data
);
11601 emit_codeview_end_symbol_subsection (MonoAotCompile
*acfg
, CodeViewSubsectionData
*section_data
, int *section_id
)
11603 emit_codeview_end_subsection (acfg
, section_data
, section_id
);
11607 emit_codeview_compiler_info (MonoAotCompile
*acfg
, int *section_id
)
11609 CodeViewSubsectionData section_data
= { 0 };
11610 CodeViewCompilerVersion compiler_version
= { 0 };
11612 // Start new compiler record subsection.
11613 emit_codeview_start_symbol_subsection (acfg
, *section_id
, CODEVIEW_SUBSECTION_RECORD_COMPILER_TYPE
, §ion_data
);
11615 emit_int32 (acfg
, CODEVIEW_CSHARP_LANGUAGE_TYPE
);
11616 emit_int16 (acfg
, CODEVIEW_CPU_TYPE
);
11618 // Get compiler version information.
11619 codeview_parse_compiler_version (VERSION
, &compiler_version
);
11621 // Compiler frontend version, 4 digits.
11622 emit_int16 (acfg
, compiler_version
.major
);
11623 emit_int16 (acfg
, compiler_version
.minor
);
11624 emit_int16 (acfg
, compiler_version
.revision
);
11625 emit_int16 (acfg
, compiler_version
.patch
);
11627 // Compiler backend version, 4 digits (currently same as frontend).
11628 emit_int16 (acfg
, compiler_version
.major
);
11629 emit_int16 (acfg
, compiler_version
.minor
);
11630 emit_int16 (acfg
, compiler_version
.revision
);
11631 emit_int16 (acfg
, compiler_version
.patch
);
11633 // Compiler string.
11634 emit_string (acfg
, "Mono AOT compiler");
11636 // Done with section.
11637 emit_codeview_end_symbol_subsection (acfg
, §ion_data
, section_id
);
11641 emit_codeview_function_info (MonoAotCompile
*acfg
, MonoMethod
*method
, int *section_id
, gchar
*symbol
, gchar
*symbol_start
, gchar
*symbol_end
)
11643 CodeViewSubsectionData section_data
= { 0 };
11644 gchar
*full_method_name
= NULL
;
11646 // Start new function record subsection.
11647 emit_codeview_start_symbol_subsection (acfg
, *section_id
, CODEVIEW_SUBSECTION_RECORD_FUNCTION_START_TYPE
, §ion_data
);
11649 // Emit 3 int 0 byte padding, currently not used.
11650 emit_zero_bytes (acfg
, sizeof (int) * 3);
11652 // Emit size of function.
11653 emit_symbol_diff (acfg
, symbol_end
, symbol_start
, 0);
11655 // Emit 3 int 0 byte padding, currently not used.
11656 emit_zero_bytes (acfg
, sizeof (int) * 3);
11658 // Emit reallocation info.
11659 fprintf (acfg
->fp
, "\t.secrel32 %s\n", symbol
);
11660 fprintf (acfg
->fp
, "\t.secidx %s\n", symbol
);
11662 // Emit flag, currently not used.
11663 emit_zero_bytes (acfg
, 1);
11665 // Emit function name, exclude signature since it should be described by own metadata.
11666 full_method_name
= mono_method_full_name (method
, FALSE
);
11667 emit_string (acfg
, full_method_name
? full_method_name
: "");
11668 g_free (full_method_name
);
11670 // Done with section.
11671 emit_codeview_end_symbol_subsection (acfg
, §ion_data
, section_id
);
11675 emit_codeview_info (MonoAotCompile
*acfg
)
11678 int section_id
= 0;
11679 gchar symbol_buffer
[MAX_SYMBOL_SIZE
];
11681 // Emit codeview debug info section
11682 emit_section_change (acfg
, ".debug$S", 0);
11684 // Emit magic header.
11685 emit_int32 (acfg
, CODEVIEW_MAGIC_HEADER
);
11687 emit_codeview_compiler_info (acfg
, §ion_id
);
11689 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
11690 MonoCompile
*cfg
= acfg
->cfgs
[i
];
11692 if (!cfg
|| COMPILE_LLVM (cfg
))
11695 int ret
= g_snprintf (symbol_buffer
, G_N_ELEMENTS (symbol_buffer
), "%sme_%x", acfg
->temp_prefix
, i
);
11696 if (ret
> 0 && ret
< G_N_ELEMENTS (symbol_buffer
))
11697 emit_codeview_function_info (acfg
, cfg
->method
, §ion_id
, cfg
->asm_debug_symbol
, cfg
->asm_symbol
, symbol_buffer
);
11702 emit_codeview_info (MonoAotCompile
*acfg
)
11705 #endif /* EMIT_WIN32_CODEVIEW_INFO */
11707 #ifdef EMIT_WIN32_UNWIND_INFO
11708 static UnwindInfoSectionCacheItem
*
11709 get_cached_unwind_info_section_item_win32 (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
)
11711 UnwindInfoSectionCacheItem
*item
= NULL
;
11713 if (!acfg
->unwind_info_section_cache
)
11714 acfg
->unwind_info_section_cache
= g_list_alloc ();
11716 PUNWIND_INFO unwind_info
= mono_arch_unwindinfo_alloc_unwind_info (unwind_ops
);
11718 // Search for unwind info in cache.
11719 GList
*list
= acfg
->unwind_info_section_cache
;
11721 while (list
&& list
->data
) {
11722 item
= (UnwindInfoSectionCacheItem
*)list
->data
;
11723 if (!memcmp (unwind_info
, item
->unwind_info
, sizeof (UNWIND_INFO
))) {
11724 // Cache hit, return cached item.
11732 if (acfg
->unwind_info_section_cache
) {
11733 item
= g_new0 (UnwindInfoSectionCacheItem
, 1);
11735 // Format .xdata section label for function, used to get unwind info address RVA.
11736 // Since the unwind info is similar for most functions, the symbol will be reused.
11737 item
->xdata_section_label
= g_strdup_printf ("%sunwind_%d", acfg
->temp_prefix
, list_size
);
11739 // Cache unwind info data, used when checking cache for matching unwind info. NOTE, cache takes
11740 //over ownership of unwind info.
11741 item
->unwind_info
= unwind_info
;
11743 // Needs to be emitted once.
11744 item
->xdata_section_emitted
= FALSE
;
11746 // Prepend to beginning of list to speed up inserts.
11747 acfg
->unwind_info_section_cache
= g_list_prepend (acfg
->unwind_info_section_cache
, (gpointer
)item
);
11755 free_unwind_info_section_cache_win32 (MonoAotCompile
*acfg
)
11757 GList
*list
= acfg
->unwind_info_section_cache
;
11760 UnwindInfoSectionCacheItem
*item
= (UnwindInfoSectionCacheItem
*)list
->data
;
11762 g_free (item
->xdata_section_label
);
11763 mono_arch_unwindinfo_free_unwind_info (item
->unwind_info
);
11772 g_list_free (acfg
->unwind_info_section_cache
);
11773 acfg
->unwind_info_section_cache
= NULL
;
11777 emit_unwind_info_data_win32 (MonoAotCompile
*acfg
, PUNWIND_INFO unwind_info
)
11779 // Emit the unwind info struct.
11780 emit_bytes (acfg
, (guint8
*)unwind_info
, sizeof (UNWIND_INFO
) - (sizeof (UNWIND_CODE
) * MONO_MAX_UNWIND_CODES
));
11782 // Emit all unwind codes encoded in unwind info struct.
11783 PUNWIND_CODE current_unwind_node
= &unwind_info
->UnwindCode
[MONO_MAX_UNWIND_CODES
- unwind_info
->CountOfCodes
];
11784 PUNWIND_CODE last_unwind_node
= &unwind_info
->UnwindCode
[MONO_MAX_UNWIND_CODES
];
11786 while (current_unwind_node
< last_unwind_node
) {
11787 guint8 node_count
= 0;
11788 switch (current_unwind_node
->UnwindOp
) {
11789 case UWOP_PUSH_NONVOL
:
11790 case UWOP_ALLOC_SMALL
:
11791 case UWOP_SET_FPREG
:
11792 case UWOP_PUSH_MACHFRAME
:
11795 case UWOP_SAVE_NONVOL
:
11796 case UWOP_SAVE_XMM128
:
11799 case UWOP_SAVE_NONVOL_FAR
:
11800 case UWOP_SAVE_XMM128_FAR
:
11803 case UWOP_ALLOC_LARGE
:
11804 if (current_unwind_node
->OpInfo
== 0)
11810 g_assert (!"Unknown unwind opcode.");
11813 while (node_count
> 0) {
11814 g_assert (current_unwind_node
< last_unwind_node
);
11816 //Emit current node.
11817 emit_bytes (acfg
, (guint8
*)current_unwind_node
, sizeof (UNWIND_CODE
));
11820 current_unwind_node
++;
11825 // Emit unwind info sections for each function. Unwind info on Windows x64 is emitted into two different sections.
11826 // .pdata includes the serialized DWORD aligned RVA's of function start, end and address of serialized
11827 // UNWIND_INFO struct emitted into .xdata, see https://msdn.microsoft.com/en-us/library/ft9x1kdx.aspx.
11828 // .xdata section includes DWORD aligned serialized version of UNWIND_INFO struct, https://msdn.microsoft.com/en-us/library/ddssxxy8.aspx.
11830 emit_unwind_info_sections_win32 (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
)
11832 char *pdata_section_label
= NULL
;
11834 int temp_prefix_len
= (acfg
->temp_prefix
!= NULL
) ? strlen (acfg
->temp_prefix
) : 0;
11835 if (strncmp (function_start
, acfg
->temp_prefix
, temp_prefix_len
)) {
11836 temp_prefix_len
= 0;
11839 // Format .pdata section label for function.
11840 pdata_section_label
= g_strdup_printf ("%spdata_%s", acfg
->temp_prefix
, function_start
+ temp_prefix_len
);
11842 UnwindInfoSectionCacheItem
*cache_item
= get_cached_unwind_info_section_item_win32 (acfg
, function_start
, function_end
, unwind_ops
);
11843 g_assert (cache_item
&& cache_item
->xdata_section_label
&& cache_item
->unwind_info
);
11845 // Emit .pdata section.
11846 emit_section_change (acfg
, ".pdata", 0);
11847 emit_alignment (acfg
, sizeof (DWORD
));
11848 emit_label (acfg
, pdata_section_label
);
11850 // Emit function start address RVA.
11851 fprintf (acfg
->fp
, "\t.long %s@IMGREL\n", function_start
);
11853 // Emit function end address RVA.
11854 fprintf (acfg
->fp
, "\t.long %s@IMGREL\n", function_end
);
11856 // Emit unwind info address RVA.
11857 fprintf (acfg
->fp
, "\t.long %s@IMGREL\n", cache_item
->xdata_section_label
);
11859 if (!cache_item
->xdata_section_emitted
) {
11860 // Emit .xdata section.
11861 emit_section_change (acfg
, ".xdata", 0);
11862 emit_alignment (acfg
, sizeof (DWORD
));
11863 emit_label (acfg
, cache_item
->xdata_section_label
);
11865 // Emit unwind info into .xdata section.
11866 emit_unwind_info_data_win32 (acfg
, cache_item
->unwind_info
);
11867 cache_item
->xdata_section_emitted
= TRUE
;
11870 g_free (pdata_section_label
);
11875 should_emit_gsharedvt_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
11878 if (acfg
->image
== mono_get_corlib () && !strcmp (m_class_get_name (method
->klass
), "Vector`1"))
11879 /* The SIMD fallback code in Vector<T> is very large, and not likely to be used */
11882 if (acfg
->image
== mono_get_corlib () && !strcmp (m_class_get_name (method
->klass
), "Volatile"))
11883 /* Read<T>/Write<T> are not needed and cause JIT failures */
11889 collect_methods (MonoAotCompile
*acfg
)
11892 MonoImage
*image
= acfg
->image
;
11894 /* Collect methods */
11895 for (i
= 0; i
< image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
11896 ERROR_DECL (error
);
11897 MonoMethod
*method
;
11898 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
11900 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
11903 aot_printerrf (acfg
, "Failed to load method 0x%x from '%s' due to %s.\n", token
, image
->name
, mono_error_get_message (error
));
11904 aot_printerrf (acfg
, "Run with MONO_LOG_LEVEL=debug for more information.\n");
11905 mono_error_cleanup (error
);
11909 /* Load all methods eagerly to skip the slower lazy loading code */
11910 mono_class_setup_methods (method
->klass
);
11912 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
11913 /* Compile the wrapper instead */
11914 /* We do this here instead of add_wrappers () because it is easy to do it here */
11915 MonoMethod
*wrapper
= mono_marshal_get_native_wrapper (method
, TRUE
, TRUE
);
11919 /* FIXME: Some mscorlib methods don't have debug info */
11921 if (acfg->aot_opts.soft_debug && !method->wrapper_type) {
11922 if (!((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
11923 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
11924 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
11925 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))) {
11926 if (!mono_debug_lookup_method (method)) {
11927 fprintf (stderr, "Method %s has no debug info, probably the .mdb file for the assembly is missing.\n", mono_method_get_full_name (method));
11934 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
)) {
11935 /* Compile the ref shared version instead */
11936 method
= mini_get_shared_method_full (method
, SHARE_MODE_NONE
, error
);
11938 aot_printerrf (acfg
, "Failed to load method 0x%x from '%s' due to %s.\n", token
, image
->name
, mono_error_get_message (error
));
11939 aot_printerrf (acfg
, "Run with MONO_LOG_LEVEL=debug for more information.\n");
11940 mono_error_cleanup (error
);
11945 /* Since we add the normal methods first, their index will be equal to their zero based token index */
11946 add_method_with_index (acfg
, method
, i
, FALSE
);
11947 acfg
->method_index
++;
11950 /* gsharedvt methods */
11951 for (mindex
= 0; mindex
< image
->tables
[MONO_TABLE_METHOD
].rows
; ++mindex
) {
11952 ERROR_DECL (error
);
11953 MonoMethod
*method
;
11954 guint32 token
= MONO_TOKEN_METHOD_DEF
| (mindex
+ 1);
11956 if (!(acfg
->opts
& MONO_OPT_GSHAREDVT
))
11959 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
11960 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
11962 if ((method
->is_generic
|| mono_class_is_gtd (method
->klass
)) && should_emit_gsharedvt_method (acfg
, method
)) {
11963 MonoMethod
*gshared
;
11965 gshared
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
11966 mono_error_assert_ok (error
);
11968 add_extra_method (acfg
, gshared
);
11972 if (mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))
11973 add_generic_instances (acfg
);
11975 if (mono_aot_mode_is_full (&acfg
->aot_opts
))
11976 add_wrappers (acfg
);
11981 compile_methods (MonoAotCompile
*acfg
)
11983 int i
, methods_len
;
11985 if (acfg
->aot_opts
.nthreads
> 0) {
11988 GPtrArray
*threads
;
11989 MonoThreadHandle
*thread_handle
;
11990 gpointer
*user_data
;
11991 MonoMethod
**methods
;
11993 methods_len
= acfg
->methods
->len
;
11995 len
= acfg
->methods
->len
/ acfg
->aot_opts
.nthreads
;
11996 g_assert (len
> 0);
11998 * Partition the list of methods into fragments, and hand it to threads to
12001 threads
= g_ptr_array_new ();
12002 /* Make a copy since acfg->methods is modified by compile_method () */
12003 methods
= g_new0 (MonoMethod
*, methods_len
);
12004 //memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
12005 for (i
= 0; i
< methods_len
; ++i
)
12006 methods
[i
] = (MonoMethod
*)g_ptr_array_index (acfg
->methods
, i
);
12008 while (i
< methods_len
) {
12009 ERROR_DECL (error
);
12010 MonoInternalThread
*thread
;
12012 frag
= g_ptr_array_new ();
12013 for (j
= 0; j
< len
; ++j
) {
12014 if (i
< methods_len
) {
12015 g_ptr_array_add (frag
, methods
[i
]);
12020 user_data
= g_new0 (gpointer
, 3);
12021 user_data
[0] = acfg
;
12022 user_data
[1] = frag
;
12024 thread
= mono_thread_create_internal (mono_domain_get (), (gpointer
)compile_thread_main
, user_data
, MONO_THREAD_CREATE_FLAGS_NONE
, error
);
12025 mono_error_assert_ok (error
);
12027 thread_handle
= mono_threads_open_thread_handle (thread
->handle
);
12028 g_ptr_array_add (threads
, thread_handle
);
12032 for (i
= 0; i
< threads
->len
; ++i
) {
12033 mono_thread_info_wait_one_handle ((MonoThreadHandle
*)g_ptr_array_index (threads
, i
), MONO_INFINITE_WAIT
, FALSE
);
12034 mono_threads_close_thread_handle ((MonoThreadHandle
*)g_ptr_array_index (threads
, i
));
12040 /* Compile methods added by compile_method () or all methods if nthreads == 0 */
12041 for (i
= methods_len
; i
< acfg
->methods
->len
; ++i
) {
12042 /* This can add new methods to acfg->methods */
12043 compile_method (acfg
, (MonoMethod
*)g_ptr_array_index (acfg
->methods
, i
));
12048 mono_llvm_fixup_aot_module ();
12053 compile_asm (MonoAotCompile
*acfg
)
12055 char *command
, *objfile
;
12056 char *outfile_name
, *tmp_outfile_name
, *llvm_ofile
;
12057 const char *tool_prefix
= acfg
->aot_opts
.tool_prefix
? acfg
->aot_opts
.tool_prefix
: "";
12058 char *ld_flags
= acfg
->aot_opts
.ld_flags
? acfg
->aot_opts
.ld_flags
: g_strdup("");
12060 #ifdef TARGET_WIN32_MSVC
12061 #define AS_OPTIONS "--target=x86_64-pc-windows-msvc -c -x assembler"
12062 #elif defined(TARGET_AMD64) && !defined(TARGET_MACH)
12063 #define AS_OPTIONS "--64"
12064 #elif defined(TARGET_POWERPC64)
12065 #define AS_OPTIONS "-a64 -mppc64"
12066 #elif defined(sparc) && TARGET_SIZEOF_VOID_P == 8
12067 #define AS_OPTIONS "-xarch=v9"
12068 #elif defined(TARGET_X86) && defined(TARGET_MACH)
12069 #define AS_OPTIONS "-arch i386"
12070 #elif defined(TARGET_X86) && !defined(TARGET_MACH)
12071 #define AS_OPTIONS "--32"
12073 #define AS_OPTIONS ""
12076 #if defined(TARGET_OSX)
12077 #define AS_NAME "clang"
12078 #elif defined(TARGET_WIN32_MSVC)
12079 #define AS_NAME "clang.exe"
12081 #define AS_NAME "as"
12084 #ifdef TARGET_WIN32_MSVC
12085 #define AS_OBJECT_FILE_SUFFIX "obj"
12087 #define AS_OBJECT_FILE_SUFFIX "o"
12091 #define LD_NAME "ld"
12092 #define LD_OPTIONS "-shared -G -Bsymbolic"
12093 #elif defined(__ppc__) && defined(TARGET_MACH)
12094 #define LD_NAME "gcc"
12095 #define LD_OPTIONS "-dynamiclib -Wl,-Bsymbolic"
12096 #elif defined(TARGET_AMD64) && defined(TARGET_MACH)
12097 #define LD_NAME "clang"
12098 #define LD_OPTIONS "--shared"
12099 #elif defined(TARGET_WIN32_MSVC)
12100 #define LD_NAME "link.exe"
12101 #define LD_OPTIONS "/DLL /MACHINE:X64 /NOLOGO /INCREMENTAL:NO"
12102 #define LD_DEBUG_OPTIONS LD_OPTIONS " /DEBUG"
12103 #elif defined(TARGET_WIN32) && !defined(TARGET_ANDROID)
12104 #define LD_NAME "gcc"
12105 #define LD_OPTIONS "-shared -Wl,-Bsymbolic"
12106 #elif defined(TARGET_X86) && defined(TARGET_MACH)
12107 #define LD_NAME "clang"
12108 #define LD_OPTIONS "-m32 -dynamiclib"
12109 #elif defined(TARGET_X86) && !defined(TARGET_MACH)
12110 #define LD_OPTIONS "-m elf_i386 -Bsymbolic"
12111 #elif defined(TARGET_ARM) && !defined(TARGET_ANDROID)
12112 #define LD_NAME "gcc"
12113 #define LD_OPTIONS "--shared -Wl,-Bsymbolic"
12114 #elif defined(TARGET_POWERPC64)
12115 #define LD_OPTIONS "-m elf64ppc -Bsymbolic"
12119 #define LD_OPTIONS "-Bsymbolic"
12122 if (acfg
->aot_opts
.asm_only
) {
12123 aot_printf (acfg
, "Output file: '%s'.\n", acfg
->tmpfname
);
12124 if (acfg
->aot_opts
.static_link
)
12125 aot_printf (acfg
, "Linking symbol: '%s'.\n", acfg
->static_linking_symbol
);
12127 aot_printf (acfg
, "LLVM output file: '%s'.\n", acfg
->llvm_sfile
);
12131 if (acfg
->aot_opts
.static_link
) {
12132 if (acfg
->aot_opts
.outfile
)
12133 objfile
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
12135 objfile
= g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->image
->name
);
12137 objfile
= g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
);
12141 g_string_append (acfg
->as_args
, "-c -x assembler");
12144 command
= g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix
, AS_NAME
, AS_OPTIONS
,
12145 acfg
->as_args
? acfg
->as_args
->str
: "",
12146 wrap_path (objfile
), wrap_path (acfg
->tmpfname
));
12147 aot_printf (acfg
, "Executing the native assembler: %s\n", command
);
12148 if (execute_system (command
) != 0) {
12154 if (acfg
->llvm
&& !acfg
->llvm_owriter
) {
12155 command
= g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix
, AS_NAME
, AS_OPTIONS
,
12156 acfg
->as_args
? acfg
->as_args
->str
: "",
12157 wrap_path (acfg
->llvm_ofile
), wrap_path (acfg
->llvm_sfile
));
12158 aot_printf (acfg
, "Executing the native assembler: %s\n", command
);
12159 if (execute_system (command
) != 0) {
12168 if (acfg
->aot_opts
.static_link
) {
12169 aot_printf (acfg
, "Output file: '%s'.\n", objfile
);
12170 aot_printf (acfg
, "Linking symbol: '%s'.\n", acfg
->static_linking_symbol
);
12175 if (acfg
->aot_opts
.outfile
)
12176 outfile_name
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
12178 outfile_name
= g_strdup_printf ("%s%s", acfg
->image
->name
, MONO_SOLIB_EXT
);
12180 tmp_outfile_name
= g_strdup_printf ("%s.tmp", outfile_name
);
12183 llvm_ofile
= g_strdup_printf ("\"%s\"", acfg
->llvm_ofile
);
12185 llvm_ofile
= g_strdup ("");
12188 /* replace the ; flags separators with spaces */
12189 g_strdelimit (ld_flags
, ';', ' ');
12191 if (acfg
->aot_opts
.llvm_only
)
12192 ld_flags
= g_strdup_printf ("%s %s", ld_flags
, "-lstdc++");
12194 #ifdef TARGET_WIN32_MSVC
12195 g_assert (tmp_outfile_name
!= NULL
);
12196 g_assert (objfile
!= NULL
);
12197 command
= g_strdup_printf ("\"%s%s\" %s %s /OUT:%s %s %s", tool_prefix
, LD_NAME
,
12198 acfg
->aot_opts
.nodebug
? LD_OPTIONS
: LD_DEBUG_OPTIONS
, ld_flags
, wrap_path (tmp_outfile_name
), wrap_path (objfile
), wrap_path (llvm_ofile
));
12199 #elif defined(LD_NAME)
12200 command
= g_strdup_printf ("%s%s %s -o %s %s %s %s", tool_prefix
, LD_NAME
, LD_OPTIONS
,
12201 wrap_path (tmp_outfile_name
), wrap_path (llvm_ofile
),
12202 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
)), ld_flags
);
12205 if (acfg
->aot_opts
.tool_prefix
) {
12206 /* Cross compiling */
12207 command
= g_strdup_printf ("\"%sld\" %s -shared -o %s %s %s %s", tool_prefix
, LD_OPTIONS
,
12208 wrap_path (tmp_outfile_name
), wrap_path (llvm_ofile
),
12209 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
)), ld_flags
);
12211 char *args
= g_strdup_printf ("%s -shared -o %s %s %s %s", LD_OPTIONS
,
12212 wrap_path (tmp_outfile_name
), wrap_path (llvm_ofile
),
12213 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
)), ld_flags
);
12215 if (acfg
->aot_opts
.llvm_only
) {
12216 command
= g_strdup_printf ("%s %s", acfg
->aot_opts
.clangxx
, args
);
12218 command
= g_strdup_printf ("\"%sld\" %s", tool_prefix
, args
);
12223 aot_printf (acfg
, "Executing the native linker: %s\n", command
);
12224 if (execute_system (command
) != 0) {
12225 g_free (tmp_outfile_name
);
12226 g_free (outfile_name
);
12235 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, MONO_SOLIB_EXT);
12236 printf ("Stripping the binary: %s\n", com);
12237 execute_system (com);
12240 #if defined(TARGET_ARM) && !defined(TARGET_MACH)
12242 * gas generates 'mapping symbols' each time code and data is mixed, which
12243 * happens a lot in emit_and_reloc_code (), so we need to get rid of them.
12245 command
= g_strdup_printf ("\"%sstrip\" --strip-symbol=\\$a --strip-symbol=\\$d %s", tool_prefix
, wrap_path(tmp_outfile_name
));
12246 aot_printf (acfg
, "Stripping the binary: %s\n", command
);
12247 if (execute_system (command
) != 0) {
12248 g_free (tmp_outfile_name
);
12249 g_free (outfile_name
);
12256 if (0 != rename (tmp_outfile_name
, outfile_name
)) {
12257 if (G_FILE_ERROR_EXIST
== g_file_error_from_errno (errno
)) {
12258 /* Since we are rebuilding the module we need to be able to replace any old copies. Remove old file and retry rename operation. */
12259 unlink (outfile_name
);
12260 rename (tmp_outfile_name
, outfile_name
);
12264 #if defined(TARGET_MACH)
12265 command
= g_strdup_printf ("dsymutil \"%s\"", outfile_name
);
12266 aot_printf (acfg
, "Executing dsymutil: %s\n", command
);
12267 if (execute_system (command
) != 0) {
12272 if (!acfg
->aot_opts
.save_temps
)
12275 g_free (tmp_outfile_name
);
12276 g_free (outfile_name
);
12279 if (acfg
->aot_opts
.save_temps
)
12280 aot_printf (acfg
, "Retained input file.\n");
12282 unlink (acfg
->tmpfname
);
12288 profread_byte (FILE *infile
)
12293 res
= fread (&i
, 1, 1, infile
);
12294 g_assert (res
== 1);
12299 profread_int (FILE *infile
)
12303 res
= fread (&i
, 4, 1, infile
);
12304 g_assert (res
== 1);
12309 profread_string (FILE *infile
)
12314 len
= profread_int (infile
);
12315 pbuf
= (char*)g_malloc (len
+ 1);
12316 res
= fread (pbuf
, 1, len
, infile
);
12317 g_assert (res
== len
);
12323 load_profile_file (MonoAotCompile
*acfg
, char *filename
)
12327 int res
, len
, version
;
12330 infile
= fopen (filename
, "rb");
12332 fprintf (stderr
, "Unable to open file '%s': %s.\n", filename
, strerror (errno
));
12336 printf ("Using profile data file '%s'\n", filename
);
12338 sprintf (magic
, AOT_PROFILER_MAGIC
);
12339 len
= strlen (magic
);
12340 res
= fread (buf
, 1, len
, infile
);
12341 magic
[len
] = '\0';
12343 if ((res
!= len
) || strcmp (buf
, magic
) != 0) {
12344 printf ("Profile file has wrong header: '%s'.\n", buf
);
12348 guint32 expected_version
= (AOT_PROFILER_MAJOR_VERSION
<< 16) | AOT_PROFILER_MINOR_VERSION
;
12349 version
= profread_int (infile
);
12350 if (version
!= expected_version
) {
12351 printf ("Profile file has wrong version 0x%4x, expected 0x%4x.\n", version
, expected_version
);
12356 ProfileData
*data
= g_new0 (ProfileData
, 1);
12357 data
->images
= g_hash_table_new (NULL
, NULL
);
12358 data
->classes
= g_hash_table_new (NULL
, NULL
);
12359 data
->ginsts
= g_hash_table_new (NULL
, NULL
);
12360 data
->methods
= g_hash_table_new (NULL
, NULL
);
12363 int type
= profread_byte (infile
);
12364 int id
= profread_int (infile
);
12366 if (type
== AOTPROF_RECORD_NONE
)
12370 case AOTPROF_RECORD_IMAGE
: {
12371 ImageProfileData
*idata
= g_new0 (ImageProfileData
, 1);
12372 idata
->name
= profread_string (infile
);
12373 char *mvid
= profread_string (infile
);
12375 g_hash_table_insert (data
->images
, GINT_TO_POINTER (id
), idata
);
12378 case AOTPROF_RECORD_GINST
: {
12380 int len
= profread_int (infile
);
12382 GInstProfileData
*gdata
= g_new0 (GInstProfileData
, 1);
12384 gdata
->argv
= g_new0 (ClassProfileData
*, len
);
12386 for (i
= 0; i
< len
; ++i
) {
12387 int class_id
= profread_int (infile
);
12389 gdata
->argv
[i
] = (ClassProfileData
*)g_hash_table_lookup (data
->classes
, GINT_TO_POINTER (class_id
));
12390 g_assert (gdata
->argv
[i
]);
12392 g_hash_table_insert (data
->ginsts
, GINT_TO_POINTER (id
), gdata
);
12395 case AOTPROF_RECORD_TYPE
: {
12396 int type
= profread_byte (infile
);
12399 case MONO_TYPE_CLASS
: {
12400 int image_id
= profread_int (infile
);
12401 int ginst_id
= profread_int (infile
);
12402 char *class_name
= profread_string (infile
);
12404 ImageProfileData
*image
= (ImageProfileData
*)g_hash_table_lookup (data
->images
, GINT_TO_POINTER (image_id
));
12407 char *p
= strrchr (class_name
, '.');
12411 ClassProfileData
*cdata
= g_new0 (ClassProfileData
, 1);
12412 cdata
->image
= image
;
12413 cdata
->ns
= g_strdup (class_name
);
12414 cdata
->name
= g_strdup (p
+ 1);
12416 if (ginst_id
!= -1) {
12417 cdata
->inst
= (GInstProfileData
*)g_hash_table_lookup (data
->ginsts
, GINT_TO_POINTER (ginst_id
));
12418 g_assert (cdata
->inst
);
12420 g_free (class_name
);
12422 g_hash_table_insert (data
->classes
, GINT_TO_POINTER (id
), cdata
);
12426 case MONO_TYPE_SZARRAY
: {
12427 int elem_id
= profread_int (infile
);
12433 g_assert_not_reached ();
12438 case AOTPROF_RECORD_METHOD
: {
12439 int class_id
= profread_int (infile
);
12440 int ginst_id
= profread_int (infile
);
12441 int param_count
= profread_int (infile
);
12442 char *method_name
= profread_string (infile
);
12443 char *sig
= profread_string (infile
);
12445 ClassProfileData
*klass
= (ClassProfileData
*)g_hash_table_lookup (data
->classes
, GINT_TO_POINTER (class_id
));
12448 MethodProfileData
*mdata
= g_new0 (MethodProfileData
, 1);
12450 mdata
->klass
= klass
;
12451 mdata
->name
= method_name
;
12452 mdata
->signature
= sig
;
12453 mdata
->param_count
= param_count
;
12455 if (ginst_id
!= -1) {
12456 mdata
->inst
= (GInstProfileData
*)g_hash_table_lookup (data
->ginsts
, GINT_TO_POINTER (ginst_id
));
12457 g_assert (mdata
->inst
);
12459 g_hash_table_insert (data
->methods
, GINT_TO_POINTER (id
), mdata
);
12463 printf ("%d\n", type
);
12464 g_assert_not_reached ();
12470 acfg
->profile_data
= g_list_append (acfg
->profile_data
, data
);
12474 resolve_class (ClassProfileData
*cdata
);
12477 resolve_ginst (GInstProfileData
*inst_data
)
12481 if (inst_data
->inst
)
12484 for (i
= 0; i
< inst_data
->argc
; ++i
) {
12485 resolve_class (inst_data
->argv
[i
]);
12486 if (!inst_data
->argv
[i
]->klass
)
12489 MonoType
**args
= g_new0 (MonoType
*, inst_data
->argc
);
12490 for (i
= 0; i
< inst_data
->argc
; ++i
)
12491 args
[i
] = m_class_get_byval_arg (inst_data
->argv
[i
]->klass
);
12493 inst_data
->inst
= mono_metadata_get_generic_inst (inst_data
->argc
, args
);
12497 resolve_class (ClassProfileData
*cdata
)
12499 ERROR_DECL (error
);
12502 if (!cdata
->image
->image
)
12505 klass
= mono_class_from_name_checked (cdata
->image
->image
, cdata
->ns
, cdata
->name
, error
);
12507 //printf ("[%s] %s.%s\n", cdata->image->name, cdata->ns, cdata->name);
12511 resolve_ginst (cdata
->inst
);
12512 if (!cdata
->inst
->inst
)
12514 MonoGenericContext ctx
;
12516 memset (&ctx
, 0, sizeof (ctx
));
12517 ctx
.class_inst
= cdata
->inst
->inst
;
12518 cdata
->klass
= mono_class_inflate_generic_class_checked (klass
, &ctx
, error
);
12520 cdata
->klass
= klass
;
12525 * Resolve the profile data to the corresponding loaded classes/methods etc. if possible.
12528 resolve_profile_data (MonoAotCompile
*acfg
, ProfileData
*data
, MonoAssembly
* current
)
12530 GHashTableIter iter
;
12531 gpointer key
, value
;
12538 GPtrArray
*assemblies
= mono_domain_get_assemblies (mono_get_root_domain (), FALSE
);
12539 g_hash_table_iter_init (&iter
, data
->images
);
12540 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12541 ImageProfileData
*idata
= (ImageProfileData
*)value
;
12543 if (!strcmp (current
->aname
.name
, idata
->name
)) {
12544 idata
->image
= current
->image
;
12548 for (i
= 0; i
< assemblies
->len
; ++i
) {
12549 MonoAssembly
*ass
= (MonoAssembly
*)g_ptr_array_index (assemblies
, i
);
12551 if (!strcmp (ass
->aname
.name
, idata
->name
)) {
12552 idata
->image
= ass
->image
;
12557 g_ptr_array_free (assemblies
, TRUE
);
12560 g_hash_table_iter_init (&iter
, data
->classes
);
12561 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12562 ClassProfileData
*cdata
= (ClassProfileData
*)value
;
12564 if (!cdata
->image
->image
) {
12565 if (acfg
->aot_opts
.verbose
)
12566 printf ("Unable to load class '%s.%s' because its image '%s' is not loaded.\n", cdata
->ns
, cdata
->name
, cdata
->image
->name
);
12570 resolve_class (cdata
);
12573 printf ("%s %s %s\n", cdata->ns, cdata->name, mono_class_full_name (cdata->klass));
12578 g_hash_table_iter_init (&iter
, data
->methods
);
12579 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12580 MethodProfileData
*mdata
= (MethodProfileData
*)value
;
12585 resolve_class (mdata
->klass
);
12586 klass
= mdata
->klass
->klass
;
12588 if (acfg
->aot_opts
.verbose
)
12589 printf ("Unable to load method '%s' because its class '%s.%s' is not loaded.\n", mdata
->name
, mdata
->klass
->ns
, mdata
->klass
->name
);
12593 while ((m
= mono_class_get_methods (klass
, &miter
))) {
12594 ERROR_DECL (error
);
12596 if (strcmp (m
->name
, mdata
->name
))
12598 MonoMethodSignature
*sig
= mono_method_signature_internal (m
);
12601 if (sig
->param_count
!= mdata
->param_count
)
12604 resolve_ginst (mdata
->inst
);
12605 if (!mdata
->inst
->inst
)
12607 MonoGenericContext ctx
;
12609 memset (&ctx
, 0, sizeof (ctx
));
12610 ctx
.method_inst
= mdata
->inst
->inst
;
12612 m
= mono_class_inflate_generic_method_checked (m
, &ctx
, error
);
12615 sig
= mono_method_signature_checked (m
, error
);
12616 if (!is_ok (error
)) {
12617 mono_error_cleanup (error
);
12621 char *sig_str
= mono_signature_full_name (sig
);
12622 gboolean match
= !strcmp (sig_str
, mdata
->signature
);
12627 //printf ("%s\n", mono_method_full_name (m, 1));
12631 if (!mdata
->method
) {
12632 if (acfg
->aot_opts
.verbose
)
12633 printf ("Unable to load method '%s' from class '%s', not found.\n", mdata
->name
, mono_class_full_name (klass
));
12639 inst_references_image (MonoGenericInst
*inst
, MonoImage
*image
)
12643 for (i
= 0; i
< inst
->type_argc
; ++i
) {
12644 MonoClass
*k
= mono_class_from_mono_type_internal (inst
->type_argv
[i
]);
12645 if (m_class_get_image (k
) == image
)
12647 if (mono_class_is_ginst (k
)) {
12648 MonoGenericInst
*kinst
= mono_class_get_context (k
)->class_inst
;
12649 if (inst_references_image (kinst
, image
))
12657 is_local_inst (MonoGenericInst
*inst
, MonoImage
*image
)
12661 for (i
= 0; i
< inst
->type_argc
; ++i
) {
12662 MonoClass
*k
= mono_class_from_mono_type_internal (inst
->type_argv
[i
]);
12663 if (!MONO_TYPE_IS_PRIMITIVE (inst
->type_argv
[i
]) && m_class_get_image (k
) != image
)
12670 add_profile_instances (MonoAotCompile
*acfg
, ProfileData
*data
)
12672 GHashTableIter iter
;
12673 gpointer key
, value
;
12679 if (acfg
->aot_opts
.profile_only
) {
12680 /* Add methods referenced by the profile */
12681 g_hash_table_iter_init (&iter
, data
->methods
);
12682 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12683 MethodProfileData
*mdata
= (MethodProfileData
*)value
;
12684 MonoMethod
*m
= mdata
->method
;
12688 if (m
->is_inflated
)
12690 add_extra_method (acfg
, m
);
12691 g_hash_table_insert (acfg
->profile_methods
, m
, m
);
12697 * Add method instances 'related' to this assembly to the AOT image.
12699 g_hash_table_iter_init (&iter
, data
->methods
);
12700 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12701 MethodProfileData
*mdata
= (MethodProfileData
*)value
;
12702 MonoMethod
*m
= mdata
->method
;
12703 MonoGenericContext
*ctx
;
12707 if (!m
->is_inflated
)
12710 ctx
= mono_method_get_context (m
);
12711 /* For simplicity, add instances which reference the assembly we are compiling */
12712 if (((ctx
->class_inst
&& inst_references_image (ctx
->class_inst
, acfg
->image
)) ||
12713 (ctx
->method_inst
&& inst_references_image (ctx
->method_inst
, acfg
->image
))) &&
12714 !mono_method_is_generic_sharable_full (m
, FALSE
, FALSE
, FALSE
)) {
12715 //printf ("%s\n", mono_method_full_name (m, TRUE));
12716 add_extra_method (acfg
, m
);
12718 } else if (m_class_get_image (m
->klass
) == acfg
->image
&&
12719 ((ctx
->class_inst
&& is_local_inst (ctx
->class_inst
, acfg
->image
)) ||
12720 (ctx
->method_inst
&& is_local_inst (ctx
->method_inst
, acfg
->image
))) &&
12721 !mono_method_is_generic_sharable_full (m
, FALSE
, FALSE
, FALSE
)) {
12722 /* Add instances where the gtd is in the assembly and its inflated with types from this assembly or corlib */
12723 //printf ("%s\n", mono_method_full_name (m, TRUE));
12724 add_extra_method (acfg
, m
);
12728 * FIXME: We might skip some instances, for example:
12729 * Foo<Bar> won't be compiled when compiling Foo's assembly since it doesn't match the first case,
12730 * and it won't be compiled when compiling Bar's assembly if Foo's assembly is not loaded.
12734 printf ("Added %d methods from profile.\n", count
);
12738 init_got_info (GotInfo
*info
)
12742 info
->patch_to_got_offset
= g_hash_table_new (mono_patch_info_hash
, mono_patch_info_equal
);
12743 info
->patch_to_got_offset_by_type
= g_new0 (GHashTable
*, MONO_PATCH_INFO_NUM
);
12744 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
)
12745 info
->patch_to_got_offset_by_type
[i
] = g_hash_table_new (mono_patch_info_hash
, mono_patch_info_equal
);
12746 info
->got_patches
= g_ptr_array_new ();
12749 static MonoAotCompile
*
12750 acfg_create (MonoAssembly
*ass
, guint32 opts
)
12752 MonoImage
*image
= ass
->image
;
12753 MonoAotCompile
*acfg
;
12755 acfg
= g_new0 (MonoAotCompile
, 1);
12756 acfg
->methods
= g_ptr_array_new ();
12757 acfg
->method_indexes
= g_hash_table_new (NULL
, NULL
);
12758 acfg
->method_depth
= g_hash_table_new (NULL
, NULL
);
12759 acfg
->plt_offset_to_entry
= g_hash_table_new (NULL
, NULL
);
12760 acfg
->patch_to_plt_entry
= g_new0 (GHashTable
*, MONO_PATCH_INFO_NUM
);
12761 acfg
->method_to_cfg
= g_hash_table_new (NULL
, NULL
);
12762 acfg
->token_info_hash
= g_hash_table_new_full (NULL
, NULL
, NULL
, NULL
);
12763 acfg
->method_to_pinvoke_import
= g_hash_table_new_full (NULL
, NULL
, NULL
, g_free
);
12764 acfg
->method_to_external_icall_symbol_name
= g_hash_table_new_full (NULL
, NULL
, NULL
, g_free
);
12765 acfg
->image_hash
= g_hash_table_new (NULL
, NULL
);
12766 acfg
->image_table
= g_ptr_array_new ();
12767 acfg
->globals
= g_ptr_array_new ();
12768 acfg
->image
= image
;
12770 /* TODO: Write out set of SIMD instructions used, rather than just those available */
12771 #ifndef MONO_CROSS_COMPILE
12772 acfg
->simd_opts
= mono_arch_cpu_enumerate_simd_versions ();
12774 acfg
->mempool
= mono_mempool_new ();
12775 acfg
->extra_methods
= g_ptr_array_new ();
12776 acfg
->unwind_info_offsets
= g_hash_table_new (NULL
, NULL
);
12777 acfg
->unwind_ops
= g_ptr_array_new ();
12778 acfg
->method_label_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
12779 acfg
->method_order
= g_ptr_array_new ();
12780 acfg
->export_names
= g_hash_table_new (NULL
, NULL
);
12781 acfg
->klass_blob_hash
= g_hash_table_new (NULL
, NULL
);
12782 acfg
->method_blob_hash
= g_hash_table_new (NULL
, NULL
);
12783 acfg
->ginst_blob_hash
= g_hash_table_new (mono_metadata_generic_inst_hash
, mono_metadata_generic_inst_equal
);
12784 acfg
->plt_entry_debug_sym_cache
= g_hash_table_new (g_str_hash
, g_str_equal
);
12785 acfg
->gsharedvt_in_signatures
= g_hash_table_new ((GHashFunc
)mono_signature_hash
, (GEqualFunc
)mono_metadata_signature_equal
);
12786 acfg
->gsharedvt_out_signatures
= g_hash_table_new ((GHashFunc
)mono_signature_hash
, (GEqualFunc
)mono_metadata_signature_equal
);
12787 acfg
->profile_methods
= g_hash_table_new (NULL
, NULL
);
12788 mono_os_mutex_init_recursive (&acfg
->mutex
);
12790 init_got_info (&acfg
->got_info
);
12791 init_got_info (&acfg
->llvm_got_info
);
12793 method_to_external_icall_symbol_name
= acfg
->method_to_external_icall_symbol_name
;
12798 got_info_free (GotInfo
*info
)
12802 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
)
12803 g_hash_table_destroy (info
->patch_to_got_offset_by_type
[i
]);
12804 g_free (info
->patch_to_got_offset_by_type
);
12805 g_hash_table_destroy (info
->patch_to_got_offset
);
12806 g_ptr_array_free (info
->got_patches
, TRUE
);
12810 acfg_free (MonoAotCompile
*acfg
)
12814 mono_img_writer_destroy (acfg
->w
);
12815 for (i
= 0; i
< acfg
->nmethods
; ++i
)
12816 if (acfg
->cfgs
[i
])
12817 mono_destroy_compile (acfg
->cfgs
[i
]);
12819 g_free (acfg
->cfgs
);
12821 g_free (acfg
->static_linking_symbol
);
12822 g_free (acfg
->got_symbol
);
12823 g_free (acfg
->plt_symbol
);
12824 g_ptr_array_free (acfg
->methods
, TRUE
);
12825 g_ptr_array_free (acfg
->image_table
, TRUE
);
12826 g_ptr_array_free (acfg
->globals
, TRUE
);
12827 g_ptr_array_free (acfg
->unwind_ops
, TRUE
);
12828 g_hash_table_destroy (acfg
->method_indexes
);
12829 g_hash_table_destroy (acfg
->method_depth
);
12830 g_hash_table_destroy (acfg
->plt_offset_to_entry
);
12831 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
)
12832 g_hash_table_destroy (acfg
->patch_to_plt_entry
[i
]);
12833 g_free (acfg
->patch_to_plt_entry
);
12834 g_hash_table_destroy (acfg
->method_to_cfg
);
12835 g_hash_table_destroy (acfg
->token_info_hash
);
12836 g_hash_table_destroy (acfg
->method_to_pinvoke_import
);
12837 g_hash_table_destroy (acfg
->method_to_external_icall_symbol_name
);
12838 g_hash_table_destroy (acfg
->image_hash
);
12839 g_hash_table_destroy (acfg
->unwind_info_offsets
);
12840 g_hash_table_destroy (acfg
->method_label_hash
);
12841 g_hash_table_destroy (acfg
->typespec_classes
);
12842 g_hash_table_destroy (acfg
->export_names
);
12843 g_hash_table_destroy (acfg
->plt_entry_debug_sym_cache
);
12844 g_hash_table_destroy (acfg
->klass_blob_hash
);
12845 g_hash_table_destroy (acfg
->method_blob_hash
);
12846 got_info_free (&acfg
->got_info
);
12847 got_info_free (&acfg
->llvm_got_info
);
12848 arch_free_unwind_info_section_cache (acfg
);
12849 mono_mempool_destroy (acfg
->mempool
);
12851 method_to_external_icall_symbol_name
= NULL
;
12855 #define WRAPPER(e,n) n,
12856 static const char* const
12857 wrapper_type_names
[MONO_WRAPPER_NUM
+ 1] = {
12858 #include "mono/metadata/wrapper-types.h"
12862 static G_GNUC_UNUSED
const char*
12863 get_wrapper_type_name (int type
)
12865 return wrapper_type_names
[type
];
12871 static void aot_dump (MonoAotCompile
*acfg
)
12877 mono_json_writer_init (&writer
);
12879 mono_json_writer_object_begin(&writer
);
12882 mono_json_writer_indent (&writer
);
12883 mono_json_writer_object_key(&writer
, "methods");
12884 mono_json_writer_array_begin (&writer
);
12887 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
12889 MonoMethod
*method
;
12892 cfg
= acfg
->cfgs
[i
];
12893 if (ignore_cfg (cfg
))
12896 method
= cfg
->orig_method
;
12898 mono_json_writer_indent (&writer
);
12899 mono_json_writer_object_begin(&writer
);
12901 mono_json_writer_indent (&writer
);
12902 mono_json_writer_object_key(&writer
, "name");
12903 mono_json_writer_printf (&writer
, "\"%s\",\n", method
->name
);
12905 mono_json_writer_indent (&writer
);
12906 mono_json_writer_object_key(&writer
, "signature");
12907 mono_json_writer_printf (&writer
, "\"%s\",\n", mono_method_get_full_name (method
));
12909 mono_json_writer_indent (&writer
);
12910 mono_json_writer_object_key(&writer
, "code_size");
12911 mono_json_writer_printf (&writer
, "\"%d\",\n", cfg
->code_size
);
12913 klass
= method
->klass
;
12915 mono_json_writer_indent (&writer
);
12916 mono_json_writer_object_key(&writer
, "class");
12917 mono_json_writer_printf (&writer
, "\"%s\",\n", m_class_get_name (klass
));
12919 mono_json_writer_indent (&writer
);
12920 mono_json_writer_object_key(&writer
, "namespace");
12921 mono_json_writer_printf (&writer
, "\"%s\",\n", m_class_get_name_space (klass
));
12923 mono_json_writer_indent (&writer
);
12924 mono_json_writer_object_key(&writer
, "wrapper_type");
12925 mono_json_writer_printf (&writer
, "\"%s\",\n", get_wrapper_type_name(method
->wrapper_type
));
12927 mono_json_writer_indent_pop (&writer
);
12928 mono_json_writer_indent (&writer
);
12929 mono_json_writer_object_end (&writer
);
12930 mono_json_writer_printf (&writer
, ",\n");
12933 mono_json_writer_indent_pop (&writer
);
12934 mono_json_writer_indent (&writer
);
12935 mono_json_writer_array_end (&writer
);
12936 mono_json_writer_printf (&writer
, ",\n");
12940 mono_json_writer_indent_push (&writer
);
12941 mono_json_writer_indent (&writer
);
12942 mono_json_writer_object_key(&writer
, "plt");
12943 mono_json_writer_array_begin (&writer
);
12945 for (i
= 0; i
< acfg
->plt_offset
; ++i
) {
12946 MonoPltEntry
*plt_entry
= NULL
;
12951 * The first plt entry is unused.
12955 plt_entry
= g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
12956 ji
= plt_entry
->ji
;
12958 mono_json_writer_indent (&writer
);
12959 mono_json_writer_printf (&writer
, "{ ");
12960 mono_json_writer_object_key(&writer
, "symbol");
12961 mono_json_writer_printf (&writer
, "\"%s\" },\n", plt_entry
->symbol
);
12964 mono_json_writer_indent_pop (&writer
);
12965 mono_json_writer_indent (&writer
);
12966 mono_json_writer_array_end (&writer
);
12967 mono_json_writer_printf (&writer
, ",\n");
12972 mono_json_writer_indent_push (&writer
);
12973 mono_json_writer_indent (&writer
);
12974 mono_json_writer_object_key(&writer
, "got");
12975 mono_json_writer_array_begin (&writer
);
12977 mono_json_writer_indent_push (&writer
);
12978 for (i
= 0; i
< acfg
->got_info
.got_patches
->len
; ++i
) {
12979 MonoJumpInfo
*ji
= g_ptr_array_index (acfg
->got_info
.got_patches
, i
);
12981 mono_json_writer_indent (&writer
);
12982 mono_json_writer_printf (&writer
, "{ ");
12983 mono_json_writer_object_key(&writer
, "patch_name");
12984 mono_json_writer_printf (&writer
, "\"%s\" },\n", get_patch_name (ji
->type
));
12987 mono_json_writer_indent_pop (&writer
);
12988 mono_json_writer_indent (&writer
);
12989 mono_json_writer_array_end (&writer
);
12990 mono_json_writer_printf (&writer
, ",\n");
12993 mono_json_writer_indent_pop (&writer
);
12994 mono_json_writer_indent (&writer
);
12995 mono_json_writer_object_end (&writer
);
12997 dumpname
= g_strdup_printf ("%s.json", g_path_get_basename (acfg
->image
->name
));
12998 dumpfile
= fopen (dumpname
, "w+");
13001 fprintf (dumpfile
, "%s", writer
.text
->str
);
13004 mono_json_writer_destroy (&writer
);
13007 static const MonoJitICallId preinited_jit_icalls
[] = {
13008 MONO_JIT_ICALL_mini_llvm_init_method
,
13009 MONO_JIT_ICALL_mini_llvm_init_gshared_method_this
,
13010 MONO_JIT_ICALL_mini_llvm_init_gshared_method_mrgctx
,
13011 MONO_JIT_ICALL_mini_llvm_init_gshared_method_vtable
,
13012 MONO_JIT_ICALL_mini_llvmonly_throw_nullref_exception
,
13013 MONO_JIT_ICALL_mono_llvm_throw_corlib_exception
,
13014 MONO_JIT_ICALL_mono_threads_state_poll
,
13015 MONO_JIT_ICALL_mini_llvmonly_init_vtable_slot
,
13016 MONO_JIT_ICALL_mono_helper_ldstr_mscorlib
,
13017 MONO_JIT_ICALL_mono_fill_method_rgctx
,
13018 MONO_JIT_ICALL_mono_fill_class_rgctx
13022 add_preinit_slot (MonoAotCompile
*acfg
, MonoJumpInfo
*ji
)
13024 if (!acfg
->aot_opts
.llvm_only
)
13025 get_got_offset (acfg
, FALSE
, ji
);
13026 get_got_offset (acfg
, TRUE
, ji
);
13030 add_preinit_got_slots (MonoAotCompile
*acfg
)
13036 * Allocate the first few GOT entries to information which is needed frequently, or it is needed
13037 * during method initialization etc.
13040 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13041 ji
->type
= MONO_PATCH_INFO_IMAGE
;
13042 ji
->data
.image
= acfg
->image
;
13043 add_preinit_slot (acfg
, ji
);
13045 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13046 ji
->type
= MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
;
13047 add_preinit_slot (acfg
, ji
);
13049 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13050 ji
->type
= MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
;
13051 add_preinit_slot (acfg
, ji
);
13053 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13054 ji
->type
= MONO_PATCH_INFO_GC_NURSERY_START
;
13055 add_preinit_slot (acfg
, ji
);
13057 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13058 ji
->type
= MONO_PATCH_INFO_AOT_MODULE
;
13059 add_preinit_slot (acfg
, ji
);
13061 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13062 ji
->type
= MONO_PATCH_INFO_GC_NURSERY_BITS
;
13063 add_preinit_slot (acfg
, ji
);
13065 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13066 ji
->type
= MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
;
13067 add_preinit_slot (acfg
, ji
);
13069 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13070 ji
->type
= MONO_PATCH_INFO_GC_SAFE_POINT_FLAG
;
13071 add_preinit_slot (acfg
, ji
);
13073 if (!acfg
->aot_opts
.llvm_only
) {
13074 for (i
= 0; i
< TLS_KEY_NUM
; i
++) {
13075 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13076 ji
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
;
13077 ji
->data
.jit_icall_id
= mono_get_tls_key_to_jit_icall_id (i
);
13078 add_preinit_slot (acfg
, ji
);
13082 /* Called by native-to-managed wrappers on possibly unattached threads */
13083 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13084 ji
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
;
13085 ji
->data
.jit_icall_id
= MONO_JIT_ICALL_mono_threads_attach_coop
;
13086 add_preinit_slot (acfg
, ji
);
13088 for (i
= 0; i
< G_N_ELEMENTS (preinited_jit_icalls
); ++i
) {
13089 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
13090 ji
->type
= MONO_PATCH_INFO_JIT_ICALL_ID
;
13091 ji
->data
.jit_icall_id
= preinited_jit_icalls
[i
];
13092 add_preinit_slot (acfg
, ji
);
13095 if (acfg
->aot_opts
.llvm_only
)
13096 acfg
->nshared_got_entries
= acfg
->llvm_got_offset
;
13098 acfg
->nshared_got_entries
= acfg
->got_offset
;
13099 g_assert (acfg
->nshared_got_entries
);
13103 mono_dedup_log_stats (MonoAotCompile
*acfg
)
13105 GHashTableIter iter
;
13106 g_assert (acfg
->dedup_stats
);
13108 if (!acfg
->dedup_emit_mode
)
13111 // If dedup_emit_mode, acfg is the dummy dedup module that consolidates
13113 g_hash_table_iter_init (&iter
, acfg
->method_to_cfg
);
13114 MonoCompile
*dcfg
= NULL
;
13115 MonoMethod
*method
= NULL
;
13117 size_t wrappers_size_saved
= 0;
13118 size_t inflated_size_saved
= 0;
13119 size_t copied_singles
= 0;
13120 int wrappers_saved
= 0;
13121 int instances_saved
= 0;
13124 while (g_hash_table_iter_next (&iter
, (gpointer
*) &method
, (gpointer
*)&dcfg
)) {
13125 gchar
*dedup_name
= mono_aot_get_mangled_method_name (method
);
13126 guint count
= GPOINTER_TO_UINT(g_hash_table_lookup (acfg
->dedup_stats
, dedup_name
));
13131 if (acfg
->dedup_emit_mode
) {
13132 // Size *saved* is the size due to things not emitted.
13134 // Just moved, didn't save space / dedup
13136 copied_singles
+= dcfg
->code_len
;
13137 } else if (method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
13139 wrappers_size_saved
+= dcfg
->code_len
* (count
- 1);
13141 instances_saved
++;
13142 inflated_size_saved
+= dcfg
->code_len
* (count
- 1);
13145 if (acfg
->aot_opts
.dedup
) {
13146 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
13148 wrappers_size_saved
+= dcfg
->code_len
* count
;
13150 instances_saved
++;
13151 inflated_size_saved
+= dcfg
->code_len
* count
;
13156 aot_printf (acfg
, "Dedup Pass: Size Saved From Deduped Wrappers:\t%d methods, %zu bytes\n", wrappers_saved
, wrappers_size_saved
);
13157 aot_printf (acfg
, "Dedup Pass: Size Saved From Inflated Methods:\t%d methods, %zu bytes\n", instances_saved
, inflated_size_saved
);
13158 if (acfg
->dedup_emit_mode
)
13159 aot_printf (acfg
, "Dedup Pass: Size of Moved But Not Deduped (only 1 copy) Methods:\t%d methods, %zu bytes\n", copied
, copied_singles
);
13161 g_hash_table_destroy (acfg
->dedup_stats
);
13162 acfg
->dedup_stats
= NULL
;
13165 // Flush the cache to tell future calls what to skip
13167 mono_flush_method_cache (MonoAotCompile
*acfg
)
13169 GHashTable
*method_cache
= acfg
->dedup_cache
;
13170 char *filename
= g_strdup_printf ("%s.dedup", acfg
->image
->name
);
13171 if (!acfg
->dedup_cache_changed
|| !acfg
->aot_opts
.dedup
) {
13176 acfg
->dedup_cache
= NULL
;
13178 FILE *cache
= fopen (filename
, "w");
13181 g_error ("Could not create cache at %s because of error: %s\n", filename
, strerror (errno
));
13183 GHashTableIter iter
;
13184 gchar
*name
= NULL
;
13185 g_hash_table_iter_init (&iter
, method_cache
);
13186 gboolean cont
= TRUE
;
13187 while (cont
&& g_hash_table_iter_next (&iter
, (gpointer
*) &name
, NULL
)) {
13188 int res
= fprintf (cache
, "%s\n", name
);
13191 // FIXME: don't assert if error when flushing
13197 // The keys are all in the imageset, nothing to free
13198 // Values are just pointers to memory owned elsewhere, or sentinels
13199 g_hash_table_destroy (method_cache
);
13202 // Read in what has been emitted by previous invocations,
13203 // what can be skipped
13205 mono_read_method_cache (MonoAotCompile
*acfg
)
13207 char *filename
= g_strdup_printf ("%s.dedup", acfg
->image
->name
);
13208 // Only do once, when dedup_cache is null
13209 if (acfg
->dedup_cache
)
13212 if (acfg
->aot_opts
.dedup_include
|| acfg
->aot_opts
.dedup
)
13213 g_assert (acfg
->dedup_stats
);
13215 // only in skip mode
13216 if (!acfg
->aot_opts
.dedup
)
13219 g_assert (acfg
->dedup_cache
);
13222 cache
= fopen (filename
, "r");
13226 // Since we do pointer comparisons, and it can't be allocated at
13227 // the address 0x1 due to alignment, we use this as a sentinel
13228 gpointer other_acfg_sentinel
;
13229 other_acfg_sentinel
= GINT_TO_POINTER (0x1);
13231 if (fseek (cache
, 0L, SEEK_END
))
13235 fileLength
= ftell (cache
);
13236 g_assert (fileLength
> 0);
13238 if (fseek (cache
, 0L, SEEK_SET
))
13241 // Avoid thousands of new malloc entries
13242 // FIXME: allocate into imageset, so we don't need to free.
13243 // put the other mangled names there too.
13245 bulk
= g_malloc0 (fileLength
* sizeof (char));
13249 while (fgets (&bulk
[offset
], fileLength
- offset
, cache
)) {
13251 char *line
= &bulk
[offset
];
13252 size_t len
= strlen (line
);
13256 if (len
>= 0 && line
[len
] == '\n')
13258 offset
+= strlen (line
) + 1;
13259 g_assert (fileLength
>= offset
);
13261 g_hash_table_insert (acfg
->dedup_cache
, line
, other_acfg_sentinel
);
13275 gboolean emit_inflated_methods
;
13276 MonoAssembly
*inflated_assembly
;
13279 static MonoAotState
*
13280 alloc_aot_state (void)
13282 MonoAotState
*state
= g_malloc (sizeof (MonoAotState
));
13283 // FIXME: Should this own the memory?
13284 state
->cache
= g_hash_table_new (g_str_hash
, g_str_equal
);
13285 state
->stats
= g_hash_table_new (g_str_hash
, g_str_equal
);
13286 // Start in "collect mode"
13287 state
->emit_inflated_methods
= FALSE
;
13288 state
->inflated_assembly
= NULL
;
13293 free_aot_state (MonoAotState
*astate
)
13295 g_hash_table_destroy (astate
->cache
);
13300 mono_add_deferred_extra_methods (MonoAotCompile
*acfg
, MonoAotState
*astate
)
13302 GHashTableIter iter
;
13303 gchar
*name
= NULL
;
13304 MonoMethod
*method
= NULL
;
13306 acfg
->dedup_emit_mode
= TRUE
;
13308 g_hash_table_iter_init (&iter
, astate
->cache
);
13309 while (g_hash_table_iter_next (&iter
, (gpointer
*) &name
, (gpointer
*) &method
)) {
13310 add_method_full (acfg
, method
, TRUE
, 0);
13316 mono_setup_dedup_state (MonoAotCompile
*acfg
, MonoAotState
**global_aot_state
, MonoAssembly
*ass
, MonoAotState
**astate
, gboolean
*is_dedup_dummy
)
13318 if (!acfg
->aot_opts
.dedup_include
&& !acfg
->aot_opts
.dedup
)
13321 if (global_aot_state
&& *global_aot_state
&& acfg
->aot_opts
.dedup_include
) {
13322 // Thread the state through when making the inflate pass
13323 *astate
= *global_aot_state
;
13327 *astate
= alloc_aot_state ();
13328 *global_aot_state
= *astate
;
13331 acfg
->dedup_cache
= (*astate
)->cache
;
13332 acfg
->dedup_stats
= (*astate
)->stats
;
13334 // fills out acfg->dedup_cache
13335 if (acfg
->aot_opts
.dedup
)
13336 mono_read_method_cache (acfg
);
13338 if (!(*astate
)->inflated_assembly
&& acfg
->aot_opts
.dedup_include
) {
13339 gchar
**asm_path
= g_strsplit (ass
->image
->name
, G_DIR_SEPARATOR_S
, 0);
13340 gchar
*asm_file
= NULL
;
13342 // Get the last part of the path, the filename
13343 for (int i
=0; asm_path
[i
] != NULL
; i
++)
13344 asm_file
= asm_path
[i
];
13346 if (!strcmp (acfg
->aot_opts
.dedup_include
, asm_file
)) {
13348 *is_dedup_dummy
= TRUE
;
13349 (*astate
)->inflated_assembly
= ass
;
13351 g_strfreev (asm_path
);
13352 } else if ((*astate
)->inflated_assembly
) {
13353 *is_dedup_dummy
= (ass
== (*astate
)->inflated_assembly
);
13358 mono_compile_deferred_assemblies (guint32 opts
, const char *aot_options
, gpointer
**aot_state
)
13360 // create assembly, loop and add extra_methods
13361 // in add_generic_instances , rip out what's in that for loop
13362 // and apply that to this aot_state inside of mono_compile_assembly
13363 MonoAotState
*astate
;
13364 astate
= *(MonoAotState
**)aot_state
;
13367 // FIXME: allow suffixes?
13368 if (!astate
->inflated_assembly
) {
13369 const char* inflate
= strstr (aot_options
, "dedup-inflate");
13373 g_error ("Error: mono was not given an assembly with the provided inflate name\n");
13377 astate
->emit_inflated_methods
= TRUE
;
13379 int res
= mono_compile_assembly (astate
->inflated_assembly
, opts
, aot_options
, aot_state
);
13382 free_aot_state (astate
);
13387 #ifndef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
13388 static MonoMethodSignature
* const * const interp_in_static_sigs
[] = {
13389 &mono_icall_sig_bool_ptr_int32_ptrref
,
13390 &mono_icall_sig_bool_ptr_ptrref
,
13391 &mono_icall_sig_int32_int32_ptrref
,
13392 &mono_icall_sig_int32_int32_ptr_ptrref
,
13393 &mono_icall_sig_int32_ptr_int32_ptr
,
13394 &mono_icall_sig_int32_ptr_int32_ptrref
,
13395 &mono_icall_sig_int32_ptr_ptrref
,
13396 &mono_icall_sig_object_object_ptr_ptr_ptr
,
13397 &mono_icall_sig_object
,
13398 &mono_icall_sig_ptr_int32_ptrref
,
13399 &mono_icall_sig_ptr_ptr_int32_ptr_ptr_ptrref
,
13400 &mono_icall_sig_ptr_ptr_int32_ptr_ptrref
,
13401 &mono_icall_sig_ptr_ptr_int32_ptrref
,
13402 &mono_icall_sig_ptr_ptr_ptr_int32_ptrref
,
13403 &mono_icall_sig_ptr_ptr_ptr_ptrref_ptrref
,
13404 &mono_icall_sig_ptr_ptr_ptr_ptr_ptrref
,
13405 &mono_icall_sig_ptr_ptr_ptr_ptrref
,
13406 &mono_icall_sig_ptr_ptr_ptrref
,
13407 &mono_icall_sig_ptr_ptr_uint32_ptrref
,
13408 &mono_icall_sig_ptr_uint32_ptrref
,
13409 &mono_icall_sig_void_object_ptr_ptr_ptr
,
13410 &mono_icall_sig_void_ptr_ptr_int32_ptr_ptrref_ptr_ptrref
,
13411 &mono_icall_sig_void_ptr_ptr_int32_ptr_ptrref
,
13412 &mono_icall_sig_void_ptr_ptr_ptrref
,
13413 &mono_icall_sig_void_ptr_ptrref
,
13414 &mono_icall_sig_void_ptr
,
13415 &mono_icall_sig_void_int32_ptrref
,
13416 &mono_icall_sig_void_uint32_ptrref
,
13417 &mono_icall_sig_void
,
13420 // Common signatures for which we use interp in wrappers even in fullaot + trampolines mode
13421 // for increased performance
13422 static MonoMethodSignature
*const * const interp_in_static_common_sigs
[] = {
13423 &mono_icall_sig_void
,
13424 &mono_icall_sig_ptr
,
13425 &mono_icall_sig_void_ptr
,
13426 &mono_icall_sig_ptr_ptr
,
13427 &mono_icall_sig_void_ptr_ptr
,
13428 &mono_icall_sig_ptr_ptr_ptr
,
13429 &mono_icall_sig_void_ptr_ptr_ptr
,
13430 &mono_icall_sig_ptr_ptr_ptr_ptr
,
13431 &mono_icall_sig_void_ptr_ptr_ptr_ptr
,
13432 &mono_icall_sig_ptr_ptr_ptr_ptr_ptr
,
13433 &mono_icall_sig_void_ptr_ptr_ptr_ptr_ptr
,
13434 &mono_icall_sig_ptr_ptr_ptr_ptr_ptr_ptr
,
13435 &mono_icall_sig_void_ptr_ptr_ptr_ptr_ptr_ptr
,
13436 &mono_icall_sig_ptr_ptr_ptr_ptr_ptr_ptr_ptr
,
13441 add_interp_in_wrapper_for_sig (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
)
13443 MonoMethod
*wrapper
;
13445 sig
= mono_metadata_signature_dup_full (mono_get_corlib (), sig
);
13446 sig
->pinvoke
= FALSE
;
13447 wrapper
= mini_get_interp_in_wrapper (sig
);
13448 add_method (acfg
, wrapper
);
13452 mono_compile_assembly (MonoAssembly
*ass
, guint32 opts
, const char *aot_options
, gpointer
**global_aot_state
)
13454 MonoImage
*image
= ass
->image
;
13456 MonoAotCompile
*acfg
;
13461 acfg
= acfg_create (ass
, opts
);
13463 memset (&acfg
->aot_opts
, 0, sizeof (acfg
->aot_opts
));
13464 acfg
->aot_opts
.write_symbols
= TRUE
;
13465 acfg
->aot_opts
.ntrampolines
= 4096;
13466 acfg
->aot_opts
.nrgctx_trampolines
= 4096;
13467 acfg
->aot_opts
.nimt_trampolines
= 512;
13468 acfg
->aot_opts
.nrgctx_fetch_trampolines
= 128;
13469 acfg
->aot_opts
.ngsharedvt_arg_trampolines
= 512;
13470 #ifdef MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE
13471 acfg
->aot_opts
.nftnptr_arg_trampolines
= 128;
13473 acfg
->aot_opts
.nunbox_arbitrary_trampolines
= 256;
13474 if (!acfg
->aot_opts
.llvm_path
)
13475 acfg
->aot_opts
.llvm_path
= g_strdup ("");
13476 acfg
->aot_opts
.temp_path
= g_strdup ("");
13478 acfg
->aot_opts
.use_trampolines_page
= TRUE
;
13480 acfg
->aot_opts
.clangxx
= g_strdup ("clang++");
13482 mono_aot_parse_options (aot_options
, &acfg
->aot_opts
);
13485 MonoAotState
*astate
= NULL
;
13486 gboolean is_dedup_dummy
= FALSE
;
13487 mono_setup_dedup_state (acfg
, (MonoAotState
**) global_aot_state
, ass
, &astate
, &is_dedup_dummy
);
13490 if (is_dedup_dummy
&& astate
&& !astate
->emit_inflated_methods
)
13493 if (acfg
->aot_opts
.dedup_include
&& !is_dedup_dummy
)
13494 acfg
->dedup_collect_only
= TRUE
;
13497 if (acfg
->aot_opts
.logfile
) {
13498 acfg
->logfile
= fopen (acfg
->aot_opts
.logfile
, "a+");
13501 if (acfg
->aot_opts
.data_outfile
) {
13502 acfg
->data_outfile
= fopen (acfg
->aot_opts
.data_outfile
, "w+");
13503 if (!acfg
->data_outfile
) {
13504 aot_printerrf (acfg
, "Unable to create file '%s': %s\n", acfg
->aot_opts
.data_outfile
, strerror (errno
));
13507 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_SEPARATE_DATA
);
13510 //acfg->aot_opts.print_skipped_methods = TRUE;
13512 #if !defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
13513 if (acfg
->opts
& MONO_OPT_GSHAREDVT
) {
13514 aot_printerrf (acfg
, "-O=gsharedvt not supported on this platform.\n");
13517 if (acfg
->aot_opts
.llvm_only
) {
13518 aot_printerrf (acfg
, "--aot=llvmonly requires a runtime that supports gsharedvt.\n");
13522 if (acfg
->aot_opts
.llvm_only
|| mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))
13523 acfg
->opts
|= MONO_OPT_GSHAREDVT
;
13526 #if !defined(ENABLE_LLVM)
13527 if (acfg
->aot_opts
.llvm_only
) {
13528 aot_printerrf (acfg
, "--aot=llvmonly requires a runtime compiled with llvm support.\n");
13531 if (mono_use_llvm
|| acfg
->aot_opts
.llvm
) {
13532 aot_printerrf (acfg
, "--aot=llvm requires a runtime compiled with llvm support.\n");
13537 if (acfg
->opts
& MONO_OPT_GSHAREDVT
)
13538 mono_set_generic_sharing_vt_supported (TRUE
);
13540 if (!acfg
->dedup_collect_only
)
13541 aot_printf (acfg
, "Mono Ahead of Time compiler - compiling assembly %s\n", image
->name
);
13543 if (!acfg
->aot_opts
.deterministic
)
13544 generate_aotid ((guint8
*) &acfg
->image
->aotid
);
13546 char *aotid
= mono_guid_to_string (acfg
->image
->aotid
);
13547 if (!acfg
->dedup_collect_only
&& !acfg
->aot_opts
.deterministic
)
13548 aot_printf (acfg
, "AOTID %s\n", aotid
);
13551 #ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
13552 if (mono_aot_mode_is_full (&acfg
->aot_opts
)) {
13553 aot_printerrf (acfg
, "--aot=full is not supported on this platform.\n");
13558 if (acfg
->aot_opts
.direct_pinvoke
&& !acfg
->aot_opts
.static_link
) {
13559 aot_printerrf (acfg
, "The 'direct-pinvoke' AOT option also requires the 'static' AOT option.\n");
13563 if (acfg
->aot_opts
.static_link
)
13564 acfg
->aot_opts
.asm_writer
= TRUE
;
13566 if (acfg
->aot_opts
.soft_debug
) {
13567 mini_debug_options
.mdb_optimizations
= TRUE
;
13568 mini_debug_options
.gen_sdb_seq_points
= TRUE
;
13570 if (!mono_debug_enabled ()) {
13571 aot_printerrf (acfg
, "The soft-debug AOT option requires the --debug option.\n");
13574 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_DEBUG
);
13577 if (acfg
->aot_opts
.try_llvm
)
13578 acfg
->aot_opts
.llvm
= mini_llvm_init ();
13580 if (mono_use_llvm
|| acfg
->aot_opts
.llvm
) {
13582 acfg
->aot_opts
.asm_writer
= TRUE
;
13583 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_WITH_LLVM
);
13585 if (acfg
->aot_opts
.soft_debug
) {
13586 aot_printerrf (acfg
, "The 'soft-debug' option is not supported when compiling with LLVM.\n");
13592 if (acfg
->aot_opts
.asm_only
&& !acfg
->aot_opts
.llvm_outfile
) {
13593 aot_printerrf (acfg
, "Compiling with LLVM and the asm-only option requires the llvm-outfile= option.\n");
13598 if (mono_aot_mode_is_full (&acfg
->aot_opts
)) {
13599 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_FULL_AOT
);
13600 acfg
->is_full_aot
= TRUE
;
13603 if (mono_aot_mode_is_interp (&acfg
->aot_opts
)) {
13604 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_INTERP
);
13605 acfg
->is_full_aot
= TRUE
;
13608 if (mini_safepoints_enabled ())
13609 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_SAFEPOINTS
);
13611 // The methods in dedup-emit amodules must be available on runtime startup
13612 // Note: Only one such amodule can have this attribute
13613 if (astate
&& astate
->emit_inflated_methods
)
13614 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_EAGER_LOAD
);
13617 if (acfg
->aot_opts
.instances_logfile_path
) {
13618 acfg
->instances_logfile
= fopen (acfg
->aot_opts
.instances_logfile_path
, "w");
13619 if (!acfg
->instances_logfile
) {
13620 aot_printerrf (acfg
, "Unable to create logfile: '%s'.\n", acfg
->aot_opts
.instances_logfile_path
);
13625 if (acfg
->aot_opts
.profile_files
) {
13628 for (l
= acfg
->aot_opts
.profile_files
; l
; l
= l
->next
) {
13629 load_profile_file (acfg
, (char*)l
->data
);
13633 if (!(mono_aot_mode_is_interp (&acfg
->aot_opts
) && !mono_aot_mode_is_full (&acfg
->aot_opts
))) {
13634 for (int method_index
= 0; method_index
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++method_index
)
13635 g_ptr_array_add (acfg
->method_order
,GUINT_TO_POINTER (method_index
));
13638 acfg
->num_trampolines
[MONO_AOT_TRAMP_SPECIFIC
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.ntrampolines
: 0;
13639 #ifdef MONO_ARCH_GSHARED_SUPPORTED
13640 acfg
->num_trampolines
[MONO_AOT_TRAMP_STATIC_RGCTX
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.nrgctx_trampolines
: 0;
13642 acfg
->num_trampolines
[MONO_AOT_TRAMP_IMT
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.nimt_trampolines
: 0;
13643 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
13644 if (acfg
->opts
& MONO_OPT_GSHAREDVT
)
13645 acfg
->num_trampolines
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.ngsharedvt_arg_trampolines
: 0;
13647 #ifdef MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE
13648 acfg
->num_trampolines
[MONO_AOT_TRAMP_FTNPTR_ARG
] = mono_aot_mode_is_interp (&acfg
->aot_opts
) ? acfg
->aot_opts
.nftnptr_arg_trampolines
: 0;
13650 acfg
->num_trampolines
[MONO_AOT_TRAMP_UNBOX_ARBITRARY
] = mono_aot_mode_is_interp (&acfg
->aot_opts
) && mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.nunbox_arbitrary_trampolines
: 0;
13652 acfg
->temp_prefix
= mono_img_writer_get_temp_label_prefix (NULL
);
13656 if (mono_use_llvm
|| acfg
->aot_opts
.llvm
) {
13658 * Emit all LLVM code into a separate assembly/object file and link with it
13661 if (!acfg
->aot_opts
.asm_only
&& acfg
->llvm_owriter_supported
) {
13662 acfg
->llvm_owriter
= TRUE
;
13663 } else if (acfg
->aot_opts
.llvm_outfile
) {
13664 int len
= strlen (acfg
->aot_opts
.llvm_outfile
);
13666 if (len
>= 2 && acfg
->aot_opts
.llvm_outfile
[len
- 2] == '.' && acfg
->aot_opts
.llvm_outfile
[len
- 1] == 'o')
13667 acfg
->llvm_owriter
= TRUE
;
13671 if (acfg
->llvm
&& acfg
->thumb_mixed
)
13672 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_LLVM_THUMB
);
13673 if (acfg
->aot_opts
.llvm_only
)
13674 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_LLVM_ONLY
);
13676 acfg
->assembly_name_sym
= g_strdup (acfg
->image
->assembly
->aname
.name
);
13677 /* Get rid of characters which cannot occur in symbols */
13678 for (p
= acfg
->assembly_name_sym
; *p
; ++p
) {
13679 if (!(isalnum (*p
) || *p
== '_'))
13683 acfg
->global_prefix
= g_strdup_printf ("mono_aot_%s", acfg
->assembly_name_sym
);
13684 acfg
->plt_symbol
= g_strdup_printf ("%s_plt", acfg
->global_prefix
);
13685 acfg
->got_symbol
= g_strdup_printf ("%s_got", acfg
->global_prefix
);
13687 acfg
->llvm_got_symbol
= g_strdup_printf ("%s_llvm_got", acfg
->global_prefix
);
13688 acfg
->llvm_eh_frame_symbol
= g_strdup_printf ("%s_eh_frame", acfg
->global_prefix
);
13691 acfg
->method_index
= 1;
13693 if (mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))
13694 mono_set_partial_sharing_supported (TRUE
);
13696 if (!(mono_aot_mode_is_interp (&acfg
->aot_opts
) && !mono_aot_mode_is_full (&acfg
->aot_opts
))) {
13697 res
= collect_methods (acfg
);
13702 // If we're emitting all of the inflated methods into a dummy
13703 // Assembly, then after extra_methods is set up, we're done
13704 // in this function.
13705 if (astate
&& astate
->emit_inflated_methods
)
13706 mono_add_deferred_extra_methods (acfg
, astate
);
13711 for (l
= acfg
->profile_data
; l
; l
= l
->next
)
13712 resolve_profile_data (acfg
, (ProfileData
*)l
->data
, ass
);
13713 for (l
= acfg
->profile_data
; l
; l
= l
->next
)
13714 add_profile_instances (acfg
, (ProfileData
*)l
->data
);
13717 acfg
->cfgs_size
= acfg
->methods
->len
+ 32;
13718 acfg
->cfgs
= g_new0 (MonoCompile
*, acfg
->cfgs_size
);
13720 /* PLT offset 0 is reserved for the PLT trampoline */
13721 acfg
->plt_offset
= 1;
13722 add_preinit_got_slots (acfg
);
13724 current_acfg
= acfg
;
13729 LLVMModuleFlags flags
= (LLVMModuleFlags
)0;
13730 #ifdef EMIT_DWARF_INFO
13731 flags
= LLVM_MODULE_FLAG_DWARF
;
13733 #ifdef EMIT_WIN32_CODEVIEW_INFO
13734 flags
= LLVM_MODULE_FLAG_CODEVIEW
;
13736 if (acfg
->aot_opts
.static_link
)
13737 flags
= (LLVMModuleFlags
)(flags
| LLVM_MODULE_FLAG_STATIC
);
13738 if (acfg
->aot_opts
.llvm_only
)
13739 flags
= (LLVMModuleFlags
)(flags
| LLVM_MODULE_FLAG_LLVM_ONLY
);
13740 if (acfg
->aot_opts
.interp
)
13741 flags
= (LLVMModuleFlags
)(flags
| LLVM_MODULE_FLAG_INTERP
);
13742 mono_llvm_create_aot_module (acfg
->image
->assembly
, acfg
->global_prefix
, acfg
->nshared_got_entries
, flags
);
13744 add_lazy_init_wrappers (acfg
);
13745 add_method (acfg
, mono_marshal_get_llvm_func_wrapper (LLVM_FUNC_WRAPPER_GC_POLL
));
13749 if (mono_aot_mode_is_interp (&acfg
->aot_opts
) && mono_is_corlib_image (acfg
->image
->assembly
->image
)) {
13750 MonoMethod
*wrapper
= mini_get_interp_lmf_wrapper ("mono_interp_to_native_trampoline", (gpointer
) mono_interp_to_native_trampoline
);
13751 add_method (acfg
, wrapper
);
13753 wrapper
= mini_get_interp_lmf_wrapper ("mono_interp_entry_from_trampoline", (gpointer
) mono_interp_entry_from_trampoline
);
13754 add_method (acfg
, wrapper
);
13756 #ifndef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
13757 for (int i
= 0; i
< G_N_ELEMENTS (interp_in_static_sigs
); i
++)
13758 add_interp_in_wrapper_for_sig (acfg
, *interp_in_static_sigs
[i
]);
13760 for (int i
= 0; i
< G_N_ELEMENTS (interp_in_static_common_sigs
); i
++)
13761 add_interp_in_wrapper_for_sig (acfg
, *interp_in_static_common_sigs
[i
]);
13764 /* required for mixed mode */
13765 if (strcmp (acfg
->image
->assembly
->aname
.name
, "mscorlib") == 0) {
13766 add_gc_wrappers (acfg
);
13768 for (int i
= 0; i
< MONO_JIT_ICALL_count
; ++i
)
13769 add_jit_icall_wrapper (acfg
, mono_find_jit_icall_info ((MonoJitICallId
)i
));
13775 compile_methods (acfg
);
13779 acfg
->stats
.jit_time
= TV_ELAPSED (atv
, btv
);
13781 dedup_skip_methods (acfg
);
13783 if (acfg
->aot_opts
.dedup_include
&& !is_dedup_dummy
)
13784 /* We only collected methods from this assembly */
13787 current_acfg
= NULL
;
13789 return emit_aot_image (acfg
);
13793 print_stats (MonoAotCompile
*acfg
)
13797 char llvm_stats_msg
[256];
13799 if (acfg
->llvm
&& !acfg
->aot_opts
.llvm_only
)
13800 sprintf (llvm_stats_msg
, ", LLVM: %d (%d%%)", acfg
->stats
.llvm_count
, acfg
->stats
.mcount
? (acfg
->stats
.llvm_count
* 100) / acfg
->stats
.mcount
: 100);
13802 strcpy (llvm_stats_msg
, "");
13804 all_sizes
= acfg
->stats
.code_size
+ acfg
->stats
.method_info_size
+ acfg
->stats
.ex_info_size
+ acfg
->stats
.unwind_info_size
+ acfg
->stats
.class_info_size
+ acfg
->stats
.got_info_size
+ acfg
->stats
.offsets_size
+ acfg
->stats
.plt_size
;
13806 aot_printf (acfg
, "Code: %d(%d%%) Info: %d(%d%%) Ex Info: %d(%d%%) Unwind Info: %d(%d%%) Class Info: %d(%d%%) PLT: %d(%d%%) GOT Info: %d(%d%%) Offsets: %d(%d%%) GOT: %d, BLOB: %d\n",
13807 (int)acfg
->stats
.code_size
, (int)(acfg
->stats
.code_size
* 100 / all_sizes
),
13808 (int)acfg
->stats
.method_info_size
, (int)(acfg
->stats
.method_info_size
* 100 / all_sizes
),
13809 (int)acfg
->stats
.ex_info_size
, (int)(acfg
->stats
.ex_info_size
* 100 / all_sizes
),
13810 (int)acfg
->stats
.unwind_info_size
, (int)(acfg
->stats
.unwind_info_size
* 100 / all_sizes
),
13811 (int)acfg
->stats
.class_info_size
, (int)(acfg
->stats
.class_info_size
* 100 / all_sizes
),
13812 acfg
->stats
.plt_size
? (int)acfg
->stats
.plt_size
: (int)acfg
->plt_offset
, acfg
->stats
.plt_size
? (int)(acfg
->stats
.plt_size
* 100 / all_sizes
) : 0,
13813 (int)acfg
->stats
.got_info_size
, (int)(acfg
->stats
.got_info_size
* 100 / all_sizes
),
13814 (int)acfg
->stats
.offsets_size
, (int)(acfg
->stats
.offsets_size
* 100 / all_sizes
),
13815 (int)(acfg
->got_offset
* sizeof (target_mgreg_t
)),
13816 (int)acfg
->stats
.blob_size
);
13817 aot_printf (acfg
, "Compiled: %d/%d (%d%%)%s, No GOT slots: %d (%d%%), Direct calls: %d (%d%%)\n",
13818 acfg
->stats
.ccount
, acfg
->stats
.mcount
, acfg
->stats
.mcount
? (acfg
->stats
.ccount
* 100) / acfg
->stats
.mcount
: 100,
13820 acfg
->stats
.methods_without_got_slots
, acfg
->stats
.mcount
? (acfg
->stats
.methods_without_got_slots
* 100) / acfg
->stats
.mcount
: 100,
13821 acfg
->stats
.direct_calls
, acfg
->stats
.all_calls
? (acfg
->stats
.direct_calls
* 100) / acfg
->stats
.all_calls
: 100);
13822 if (acfg
->stats
.genericcount
)
13823 aot_printf (acfg
, "%d methods failed gsharing (%d%%)\n", acfg
->stats
.genericcount
, acfg
->stats
.mcount
? (acfg
->stats
.genericcount
* 100) / acfg
->stats
.mcount
: 100);
13824 if (acfg
->stats
.abscount
)
13825 aot_printf (acfg
, "%d methods contain absolute addresses (%d%%)\n", acfg
->stats
.abscount
, acfg
->stats
.mcount
? (acfg
->stats
.abscount
* 100) / acfg
->stats
.mcount
: 100);
13826 if (acfg
->stats
.lmfcount
)
13827 aot_printf (acfg
, "%d methods contain lmf pointers (%d%%)\n", acfg
->stats
.lmfcount
, acfg
->stats
.mcount
? (acfg
->stats
.lmfcount
* 100) / acfg
->stats
.mcount
: 100);
13828 if (acfg
->stats
.ocount
)
13829 aot_printf (acfg
, "%d methods have other problems (%d%%)\n", acfg
->stats
.ocount
, acfg
->stats
.mcount
? (acfg
->stats
.ocount
* 100) / acfg
->stats
.mcount
: 100);
13831 aot_printf (acfg
, "GOT slot distribution:\n");
13834 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
) {
13835 nslots
+= acfg
->stats
.got_slot_types
[i
];
13836 size
+= acfg
->stats
.got_slot_info_sizes
[i
];
13837 if (acfg
->stats
.got_slot_types
[i
])
13838 aot_printf (acfg
, "\t%s: %d (%d)\n", get_patch_name (i
), acfg
->stats
.got_slot_types
[i
], acfg
->stats
.got_slot_info_sizes
[i
]);
13840 aot_printf (acfg
, "GOT SLOTS: %d, INFO SIZE: %d\n", nslots
, size
);
13842 aot_printf (acfg
, "\nEncoding stats:\n");
13843 aot_printf (acfg
, "\tMethod ref: %d (%dk)\n", acfg
->stats
.method_ref_count
, acfg
->stats
.method_ref_size
/ 1024);
13844 aot_printf (acfg
, "\tClass ref: %d (%dk)\n", acfg
->stats
.class_ref_count
, acfg
->stats
.class_ref_size
/ 1024);
13845 aot_printf (acfg
, "\tGinst: %d (%dk)\n", acfg
->stats
.ginst_count
, acfg
->stats
.ginst_size
/ 1024);
13847 aot_printf (acfg
, "\nMethod stats:\n");
13848 aot_printf (acfg
, "\tNormal: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_NORMAL
]);
13849 aot_printf (acfg
, "\tInstance: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_INST
]);
13850 aot_printf (acfg
, "\tGSharedvt: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_GSHAREDVT
]);
13851 aot_printf (acfg
, "\tWrapper: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_WRAPPER
]);
13853 if (acfg
->aot_opts
.dedup
|| acfg
->dedup_emit_mode
)
13854 mono_dedup_log_stats (acfg
);
13858 create_depfile (MonoAotCompile
*acfg
)
13862 // FIXME: Support other configurations
13863 g_assert (acfg
->aot_opts
.llvm_only
&& acfg
->aot_opts
.asm_only
&& acfg
->aot_opts
.llvm_outfile
);
13865 depfile
= fopen (acfg
->aot_opts
.depfile
, "w");
13866 g_assert (depfile
);
13869 char **targets
= g_new0 (char*, ntargets
);
13870 targets
[0] = acfg
->aot_opts
.llvm_outfile
;
13871 for (int tindex
= 0; tindex
< ntargets
; ++tindex
) {
13872 fprintf (depfile
, "%s: ", targets
[tindex
]);
13873 for (int i
= 0; i
< acfg
->image_table
->len
; i
++) {
13874 MonoImage
*image
= (MonoImage
*)g_ptr_array_index (acfg
->image_table
, i
);
13875 fprintf (depfile
, " %s", image
->filename
);
13877 fprintf (depfile
, "\n");
13884 emit_aot_image (MonoAotCompile
*acfg
)
13894 if (acfg
->aot_opts
.asm_only
) {
13895 if (acfg
->aot_opts
.outfile
) {
13896 acfg
->tmpfname
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
13897 acfg
->tmpbasename
= g_strdup (acfg
->tmpfname
);
13899 acfg
->tmpbasename
= g_strdup_printf ("%s", acfg
->image
->name
);
13900 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->tmpbasename
);
13902 g_assert (acfg
->aot_opts
.llvm_outfile
);
13903 acfg
->llvm_sfile
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
13904 if (acfg
->llvm_owriter
)
13905 acfg
->llvm_ofile
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
13907 acfg
->llvm_sfile
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
13910 if (strcmp (acfg
->aot_opts
.temp_path
, "") != 0) {
13911 temp_path
= g_strdup (acfg
->aot_opts
.temp_path
);
13913 temp_path
= g_mkdtemp (g_strdup ("mono_aot_XXXXXX"));
13914 g_assertf (temp_path
, "mkdtemp failed, error = (%d) %s", errno
, g_strerror (errno
));
13915 acfg
->temp_dir_to_delete
= g_strdup (temp_path
);
13918 acfg
->tmpbasename
= g_build_filename (temp_path
, "temp", (const char*)NULL
);
13919 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->tmpbasename
);
13920 acfg
->llvm_sfile
= g_strdup_printf ("%s-llvm.s", acfg
->tmpbasename
);
13921 acfg
->llvm_ofile
= g_strdup_printf ("%s-llvm." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpbasename
);
13923 g_free (temp_path
);
13928 if (acfg
->aot_opts
.asm_only
&& !acfg
->aot_opts
.llvm_only
) {
13929 if (acfg
->aot_opts
.outfile
)
13930 acfg
->tmpfname
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
13932 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->image
->name
);
13933 acfg
->fp
= fopen (acfg
->tmpfname
, "w+");
13935 if (strcmp (acfg
->aot_opts
.temp_path
, "") == 0) {
13936 int i
= g_file_open_tmp ("mono_aot_XXXXXX", &acfg
->tmpfname
, NULL
);
13937 acfg
->fp
= fdopen (i
, "w+");
13939 acfg
->tmpbasename
= g_build_filename (acfg
->aot_opts
.temp_path
, "temp", (const char*)NULL
);
13940 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->tmpbasename
);
13941 acfg
->fp
= fopen (acfg
->tmpfname
, "w+");
13944 if (acfg
->fp
== 0 && !acfg
->aot_opts
.llvm_only
) {
13945 aot_printerrf (acfg
, "Unable to open file '%s': %s\n", acfg
->tmpfname
, strerror (errno
));
13949 acfg
->w
= mono_img_writer_create (acfg
->fp
, FALSE
);
13951 /* Compute symbols for methods */
13952 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
13953 if (acfg
->cfgs
[i
]) {
13954 MonoCompile
*cfg
= acfg
->cfgs
[i
];
13955 int method_index
= get_method_index (acfg
, cfg
->orig_method
);
13957 if (cfg
->asm_symbol
) {
13958 // Set by method emitter in backend
13959 if (acfg
->llvm_label_prefix
) {
13960 char *old_symbol
= cfg
->asm_symbol
;
13961 cfg
->asm_symbol
= g_strdup_printf ("%s%s", acfg
->llvm_label_prefix
, cfg
->asm_symbol
);
13962 g_free (old_symbol
);
13964 } else if (COMPILE_LLVM (cfg
)) {
13965 cfg
->asm_symbol
= g_strdup_printf ("%s%s", acfg
->llvm_label_prefix
, cfg
->llvm_method_name
);
13966 } else if (acfg
->global_symbols
|| acfg
->llvm
) {
13967 cfg
->asm_symbol
= get_debug_sym (cfg
->orig_method
, "", acfg
->method_label_hash
);
13969 cfg
->asm_symbol
= g_strdup_printf ("%s%sm_%x", acfg
->temp_prefix
, acfg
->llvm_label_prefix
, method_index
);
13971 cfg
->asm_debug_symbol
= cfg
->asm_symbol
;
13975 if (acfg
->aot_opts
.dwarf_debug
&& acfg
->aot_opts
.gnu_asm
) {
13977 * CLANG supports GAS .file/.loc directives, so emit line number information this way
13979 acfg
->gas_line_numbers
= TRUE
;
13982 #ifdef EMIT_DWARF_INFO
13983 if ((!acfg
->aot_opts
.nodebug
|| acfg
->aot_opts
.dwarf_debug
) && acfg
->has_jitted_code
) {
13984 if (acfg
->aot_opts
.dwarf_debug
&& !mono_debug_enabled ()) {
13985 aot_printerrf (acfg
, "The dwarf AOT option requires the --debug option.\n");
13988 acfg
->dwarf
= mono_dwarf_writer_create (acfg
->w
, NULL
, 0, !acfg
->gas_line_numbers
);
13990 #endif /* EMIT_DWARF_INFO */
13993 mono_img_writer_emit_start (acfg
->w
);
13996 mono_dwarf_writer_emit_base_info (acfg
->dwarf
, g_path_get_basename (acfg
->image
->name
), mono_unwind_get_cie_program ());
13998 if (acfg
->aot_opts
.dedup
)
13999 mono_flush_method_cache (acfg
);
14005 emit_extra_methods (acfg
);
14007 emit_trampolines (acfg
);
14009 emit_class_name_table (acfg
);
14011 emit_got_info (acfg
, FALSE
);
14013 emit_got_info (acfg
, TRUE
);
14015 emit_exception_info (acfg
);
14017 emit_unwind_info (acfg
);
14019 emit_class_info (acfg
);
14023 emit_image_table (acfg
);
14025 emit_weak_field_indexes (acfg
);
14031 * The managed allocators are GC specific, so can't use an AOT image created by one GC
14034 const char *gc_name
= mono_gc_get_gc_name ();
14035 acfg
->gc_name_offset
= add_to_blob (acfg
, (guint8
*)gc_name
, strlen (gc_name
) + 1);
14040 emit_objc_selectors (acfg
);
14042 emit_globals (acfg
);
14044 emit_file_info (acfg
);
14047 emit_dwarf_info (acfg
);
14048 mono_dwarf_writer_close (acfg
->dwarf
);
14050 if (!acfg
->aot_opts
.nodebug
)
14051 emit_codeview_info (acfg
);
14054 emit_mem_end (acfg
);
14056 if (acfg
->need_pt_gnu_stack
) {
14057 /* This is required so the .so doesn't have an executable stack */
14058 /* The bin writer already emits this */
14059 fprintf (acfg
->fp
, "\n.section .note.GNU-stack,\"\",@progbits\n");
14062 if (acfg
->aot_opts
.data_outfile
)
14063 fclose (acfg
->data_outfile
);
14069 res
= emit_llvm_file (acfg
);
14075 emit_library_info (acfg
);
14079 acfg
->stats
.gen_time
= TV_ELAPSED (atv
, btv
);
14081 if (!acfg
->aot_opts
.stats
)
14082 aot_printf (acfg
, "Compiled: %d/%d\n", acfg
->stats
.ccount
, acfg
->stats
.mcount
);
14086 res
= mono_img_writer_emit_writeout (acfg
->w
);
14091 res
= compile_asm (acfg
);
14098 acfg
->stats
.link_time
= TV_ELAPSED (atv
, btv
);
14100 if (acfg
->aot_opts
.stats
)
14101 print_stats (acfg
);
14103 aot_printf (acfg
, "JIT time: %d ms, Generation time: %d ms, Assembly+Link time: %d ms.\n", acfg
->stats
.jit_time
/ 1000, acfg
->stats
.gen_time
/ 1000, acfg
->stats
.link_time
/ 1000);
14105 if (acfg
->aot_opts
.depfile
)
14106 create_depfile (acfg
);
14108 if (acfg
->aot_opts
.dump_json
)
14111 if (!acfg
->aot_opts
.save_temps
&& acfg
->temp_dir_to_delete
) {
14112 char *command
= g_strdup_printf ("rm -r %s", acfg
->temp_dir_to_delete
);
14113 execute_system (command
);
14127 mono_aot_readonly_field_override (MonoClassField
*field
)
14133 mono_compile_assembly (MonoAssembly
*ass
, guint32 opts
, const char *aot_options
, gpointer
**aot_state
)
14139 mono_aot_is_shared_got_offset (int offset
)
14145 mono_compile_deferred_assemblies (guint32 opts
, const char *aot_options
, gpointer
**aot_state
)
14147 g_assert_not_reached ();
14152 mono_aot_direct_icalls_enabled_for_method (MonoCompile
*cfg
, MonoMethod
*method
)
14154 g_assert_not_reached ();
14159 mono_aot_can_enter_interp (MonoMethod
*method
)