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
);
83 get_method_nofail (MonoClass
*klass
, const char *method_name
, int param_count
, int flags
)
85 MonoMethod
*result
= try_get_method_nofail (klass
, method_name
, param_count
, flags
);
86 g_assertf (result
, "Expected to find method %s in klass %s", method_name
, m_class_get_name (klass
));
90 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
92 // Use MSVC toolchain, Clang for MSVC using MSVC codegen and linker, when compiling for AMD64
93 // targeting WIN32 platforms running AOT compiler on WIN32 platform with VS installation.
94 #if defined(TARGET_AMD64) && defined(TARGET_WIN32) && defined(HOST_WIN32) && defined(_MSC_VER)
95 #define TARGET_X86_64_WIN32_MSVC
98 #if defined(TARGET_X86_64_WIN32_MSVC)
99 #define TARGET_WIN32_MSVC
102 // Emit native unwind info on Windows platforms (different from DWARF). Emitted unwind info
103 // works when using the MSVC toolchain using Clang for MSVC codegen and linker. Only supported when
104 // compiling for AMD64 (Windows x64 platforms).
105 #if defined(TARGET_WIN32_MSVC) && defined(MONO_ARCH_HAVE_UNWIND_TABLE)
106 #define EMIT_WIN32_UNWIND_INFO
109 #if defined(__linux__)
110 #define RODATA_SECT ".rodata"
111 #elif defined(TARGET_MACH)
112 #define RODATA_SECT ".section __TEXT, __const"
113 #elif defined(TARGET_WIN32_MSVC)
114 #define RODATA_SECT ".rdata"
116 #define RODATA_SECT ".text"
119 #define TV_DECLARE(name) gint64 name
120 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
121 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
123 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
130 typedef struct ClassProfileData ClassProfileData
;
134 ClassProfileData
**argv
;
135 MonoGenericInst
*inst
;
138 struct ClassProfileData
{
139 ImageProfileData
*image
;
141 GInstProfileData
*inst
;
146 ClassProfileData
*klass
;
151 GInstProfileData
*inst
;
156 GHashTable
*images
, *classes
, *ginsts
, *methods
;
159 /* predefined values for static readonly fields without needed to run the .cctor */
160 typedef struct _ReadOnlyValue ReadOnlyValue
;
161 struct _ReadOnlyValue
{
164 int type
; /* to be used later for typechecking to prevent user errors */
173 static ReadOnlyValue
*readonly_values
;
175 typedef struct MonoAotOptions
{
179 GList
*profile_files
;
181 gboolean write_symbols
;
182 gboolean metadata_only
;
183 gboolean bind_to_runtime_version
;
187 gboolean static_link
;
191 gboolean dwarf_debug
;
193 gboolean log_generics
;
194 gboolean log_instances
;
195 gboolean gen_msym_dir
;
196 char *gen_msym_dir_path
;
197 gboolean direct_pinvoke
;
198 gboolean direct_icalls
;
199 gboolean no_direct_calls
;
200 gboolean use_trampolines_page
;
201 gboolean no_instances
;
202 // We are collecting inflated methods and emitting non-inflated
204 // The name of the assembly for which the AOT module is going to have all deduped methods moved to.
205 // When set, we are emitting inflated methods only
211 gboolean llvm_disable_self_init
;
214 int nrgctx_trampolines
;
215 int nimt_trampolines
;
216 int ngsharedvt_arg_trampolines
;
217 int nftnptr_arg_trampolines
;
218 int nrgctx_fetch_trampolines
;
219 int nunbox_arbitrary_trampolines
;
220 gboolean print_skipped_methods
;
223 gboolean deterministic
;
229 char *instances_logfile_path
;
234 gboolean profile_only
;
242 METHOD_CAT_GSHAREDVT
,
248 typedef struct MonoAotStats
{
249 int ccount
, mcount
, lmfcount
, abscount
, gcount
, ocount
, genericcount
;
250 gint64 code_size
, method_info_size
, ex_info_size
, unwind_info_size
, got_size
, class_info_size
, got_info_size
, plt_size
, blob_size
;
251 int methods_without_got_slots
, direct_calls
, all_calls
, llvm_count
;
252 int got_slots
, offsets_size
;
253 int method_categories
[METHOD_CAT_NUM
];
254 int got_slot_types
[MONO_PATCH_INFO_NUM
];
255 int got_slot_info_sizes
[MONO_PATCH_INFO_NUM
];
256 int jit_time
, gen_time
, link_time
;
257 int method_ref_count
, method_ref_size
;
258 int class_ref_count
, class_ref_size
;
259 int ginst_count
, ginst_size
;
262 typedef struct GotInfo
{
263 GHashTable
*patch_to_got_offset
;
264 GHashTable
**patch_to_got_offset_by_type
;
265 GPtrArray
*got_patches
;
268 #ifdef EMIT_WIN32_UNWIND_INFO
269 typedef struct _UnwindInfoSectionCacheItem
{
270 char *xdata_section_label
;
271 PUNWIND_INFO unwind_info
;
272 gboolean xdata_section_emitted
;
273 } UnwindInfoSectionCacheItem
;
276 typedef struct MonoAotCompile
{
279 GHashTable
*method_indexes
;
280 GHashTable
*method_depth
;
283 GHashTable
**patch_to_plt_entry
;
284 GHashTable
*plt_offset_to_entry
;
285 //GHashTable *patch_to_got_offset;
286 //GHashTable **patch_to_got_offset_by_type;
287 //GPtrArray *got_patches;
288 GotInfo got_info
, llvm_got_info
;
289 GHashTable
*image_hash
;
290 GHashTable
*method_to_cfg
;
291 GHashTable
*token_info_hash
;
292 GHashTable
*method_to_pinvoke_import
;
293 GHashTable
*method_to_external_icall_symbol_name
;
294 GPtrArray
*extra_methods
;
295 GPtrArray
*image_table
;
297 GPtrArray
*method_order
;
298 GHashTable
*dedup_stats
;
299 GHashTable
*dedup_cache
;
300 gboolean dedup_cache_changed
;
301 GHashTable
*export_names
;
302 /* Maps MonoClass* -> blob offset */
303 GHashTable
*klass_blob_hash
;
304 /* Maps MonoMethod* -> blob offset */
305 GHashTable
*method_blob_hash
;
306 /* Maps MonoGenericInst* -> blob offset */
307 GHashTable
*ginst_blob_hash
;
308 GHashTable
*gsharedvt_in_signatures
;
309 GHashTable
*gsharedvt_out_signatures
;
310 guint32
*plt_got_info_offsets
;
311 guint32 got_offset
, llvm_got_offset
, plt_offset
, plt_got_offset_base
, nshared_got_entries
;
312 /* Number of GOT entries reserved for trampolines */
313 guint32 num_trampoline_got_entries
;
314 guint32 tramp_page_size
;
316 guint32 table_offsets
[MONO_AOT_TABLE_NUM
];
317 guint32 num_trampolines
[MONO_AOT_TRAMP_NUM
];
318 guint32 trampoline_got_offset_base
[MONO_AOT_TRAMP_NUM
];
319 guint32 trampoline_size
[MONO_AOT_TRAMP_NUM
];
320 guint32 tramp_page_code_offsets
[MONO_AOT_TRAMP_NUM
];
322 MonoAotOptions aot_opts
;
324 guint32 nextra_methods
;
327 MonoMemPool
*mempool
;
330 char *static_linking_symbol
;
332 gboolean gas_line_numbers
;
333 /* Whenever to emit an object file directly from llc */
334 gboolean llvm_owriter
;
335 gboolean llvm_owriter_supported
;
337 MonoDwarfWriter
*dwarf
;
341 char *temp_dir_to_delete
;
345 GHashTable
*unwind_info_offsets
;
346 GPtrArray
*unwind_ops
;
347 guint32 unwind_info_offset
;
350 char *llvm_got_symbol
;
352 char *llvm_eh_frame_symbol
;
353 GHashTable
*method_label_hash
;
354 const char *temp_prefix
;
355 const char *user_symbol_prefix
;
356 const char *llvm_label_prefix
;
357 const char *inst_directive
;
359 guint32 label_generator
;
361 gboolean has_jitted_code
;
362 gboolean is_full_aot
;
363 gboolean dedup_collect_only
;
364 MonoAotFileFlags flags
;
365 MonoDynamicStream blob
;
366 gboolean blob_closed
;
367 GHashTable
*typespec_classes
;
370 char *assembly_name_sym
;
371 GHashTable
*plt_entry_debug_sym_cache
;
372 gboolean thumb_mixed
, need_no_dead_strip
, need_pt_gnu_stack
;
373 GHashTable
*ginst_hash
;
374 GHashTable
*dwarf_ln_filenames
;
375 gboolean global_symbols
;
376 int objc_selector_index
, objc_selector_index_2
;
377 GPtrArray
*objc_selectors
;
378 GHashTable
*objc_selector_to_index
;
380 GHashTable
*profile_methods
;
381 #ifdef EMIT_WIN32_UNWIND_INFO
382 GList
*unwind_info_section_cache
;
385 FILE *instances_logfile
;
389 // In this mode, we are emitting dedupable methods that we encounter
390 gboolean dedup_emit_mode
;
395 char *symbol
, *llvm_symbol
, *debug_sym
;
397 gboolean jit_used
, llvm_used
;
400 #define mono_acfg_lock(acfg) mono_os_mutex_lock (&((acfg)->mutex))
401 #define mono_acfg_unlock(acfg) mono_os_mutex_unlock (&((acfg)->mutex))
403 /* This points to the current acfg in LLVM mode */
404 static MonoAotCompile
*llvm_acfg
;
406 /* Cache of decoded method external icall symbol names. */
407 /* Owned by acfg, but kept in this static as well since it is */
408 /* accessed by code paths not having access to acfg. */
409 static GHashTable
*method_to_external_icall_symbol_name
;
411 // This, instead of an array of pointers, to optimize away a pointer and a relocation per string.
412 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
413 #define MSGSTRFIELD1(line) str##line
414 static const struct msgstr_t
{
415 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
416 #include "patch-info.h"
419 #define PATCH_INFO(a,b) b,
420 #include "patch-info.h"
423 static const gint16 opidx
[] = {
424 #define PATCH_INFO(a,b) offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
425 #include "patch-info.h"
429 static G_GNUC_UNUSED
const char*
430 get_patch_name (int info
)
432 return (const char*)&opstr
+ opidx
[info
];
436 emit_aot_image (MonoAotCompile
*acfg
);
439 mono_flush_method_cache (MonoAotCompile
*acfg
);
442 mono_read_method_cache (MonoAotCompile
*acfg
);
445 get_unwind_info_offset (MonoAotCompile
*acfg
, guint8
*encoded
, guint32 encoded_len
);
448 get_plt_entry_debug_sym (MonoAotCompile
*acfg
, MonoJumpInfo
*ji
, GHashTable
*cache
);
451 add_gsharedvt_wrappers (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
, gboolean gsharedvt_in
, gboolean gsharedvt_out
, gboolean interp_in
);
454 add_profile_instances (MonoAotCompile
*acfg
, ProfileData
*data
);
456 static inline gboolean
457 ignore_cfg (MonoCompile
*cfg
)
459 return !cfg
|| cfg
->skip
;
463 aot_printf (MonoAotCompile
*acfg
, const gchar
*format
, ...)
469 output
= acfg
->logfile
;
473 va_start (args
, format
);
474 vfprintf (output
, format
, args
);
479 aot_printerrf (MonoAotCompile
*acfg
, const gchar
*format
, ...)
485 output
= acfg
->logfile
;
489 va_start (args
, format
);
490 vfprintf (output
, format
, args
);
495 report_loader_error (MonoAotCompile
*acfg
, MonoError
*error
, gboolean fatal
, const char *format
, ...)
504 output
= acfg
->logfile
;
508 va_start (args
, format
);
509 vfprintf (output
, format
, args
);
511 mono_error_cleanup (error
);
513 if (acfg
->is_full_aot
&& fatal
) {
514 fprintf (output
, "FullAOT cannot continue if there are loader errors.\n");
519 /* Wrappers around the image writer functions */
521 #define MAX_SYMBOL_SIZE 256
523 static inline const char *
524 mangle_symbol (const char * symbol
, char * mangled_symbol
, gsize length
)
526 gsize needed_size
= length
;
528 g_assert (NULL
!= symbol
);
529 g_assert (NULL
!= mangled_symbol
);
530 g_assert (0 != length
);
532 #if defined(TARGET_WIN32) && defined(TARGET_X86)
533 if (symbol
&& '_' != symbol
[0]) {
534 needed_size
= g_snprintf (mangled_symbol
, length
, "_%s", symbol
);
536 needed_size
= g_snprintf (mangled_symbol
, length
, "%s", symbol
);
539 needed_size
= g_snprintf (mangled_symbol
, length
, "%s", symbol
);
542 g_assert (0 <= needed_size
&& needed_size
< length
);
543 return mangled_symbol
;
547 mangle_symbol_alloc (const char * symbol
)
549 g_assert (NULL
!= symbol
);
551 #if defined(TARGET_WIN32) && defined(TARGET_X86)
552 if (symbol
&& '_' != symbol
[0]) {
553 return g_strdup_printf ("_%s", symbol
);
556 return g_strdup_printf ("%s", symbol
);
559 return g_strdup_printf ("%s", symbol
);
564 emit_section_change (MonoAotCompile
*acfg
, const char *section_name
, int subsection_index
)
566 mono_img_writer_emit_section_change (acfg
->w
, section_name
, subsection_index
);
569 #if defined(TARGET_WIN32) && defined(TARGET_X86)
572 emit_local_symbol (MonoAotCompile
*acfg
, const char *name
, const char *end_label
, gboolean func
)
574 const char * mangled_symbol_name
= name
;
575 char * mangled_symbol_name_alloc
= NULL
;
578 mangled_symbol_name_alloc
= mangle_symbol_alloc (name
);
579 mangled_symbol_name
= mangled_symbol_name_alloc
;
582 if (name
!= mangled_symbol_name
&& 0 != g_strcasecmp (name
, mangled_symbol_name
)) {
583 mono_img_writer_emit_label (acfg
->w
, mangled_symbol_name
);
585 mono_img_writer_emit_local_symbol (acfg
->w
, mangled_symbol_name
, end_label
, func
);
587 if (NULL
!= mangled_symbol_name_alloc
) {
588 g_free (mangled_symbol_name_alloc
);
595 emit_local_symbol (MonoAotCompile
*acfg
, const char *name
, const char *end_label
, gboolean func
)
597 mono_img_writer_emit_local_symbol (acfg
->w
, name
, end_label
, func
);
603 emit_label (MonoAotCompile
*acfg
, const char *name
)
605 mono_img_writer_emit_label (acfg
->w
, name
);
609 emit_bytes (MonoAotCompile
*acfg
, const guint8
* buf
, int size
)
611 mono_img_writer_emit_bytes (acfg
->w
, buf
, size
);
615 emit_string (MonoAotCompile
*acfg
, const char *value
)
617 mono_img_writer_emit_string (acfg
->w
, value
);
621 emit_line (MonoAotCompile
*acfg
)
623 mono_img_writer_emit_line (acfg
->w
);
627 emit_alignment (MonoAotCompile
*acfg
, int size
)
629 mono_img_writer_emit_alignment (acfg
->w
, size
);
633 emit_alignment_code (MonoAotCompile
*acfg
, int size
)
635 if (acfg
->align_pad_value
)
636 mono_img_writer_emit_alignment_fill (acfg
->w
, size
, acfg
->align_pad_value
);
638 mono_img_writer_emit_alignment (acfg
->w
, size
);
642 emit_padding (MonoAotCompile
*acfg
, int size
)
647 if (acfg
->align_pad_value
) {
648 for (i
= 0; i
< 16; ++i
)
649 buf
[i
] = acfg
->align_pad_value
;
651 memset (buf
, 0, sizeof (buf
));
654 for (i
= 0; i
< size
; i
+= 16) {
656 emit_bytes (acfg
, buf
, size
- i
);
658 emit_bytes (acfg
, buf
, 16);
663 emit_pointer (MonoAotCompile
*acfg
, const char *target
)
665 mono_img_writer_emit_pointer (acfg
->w
, target
);
669 emit_pointer_2 (MonoAotCompile
*acfg
, const char *prefix
, const char *target
)
671 if (prefix
[0] != '\0') {
672 char *s
= g_strdup_printf ("%s%s", prefix
, target
);
673 mono_img_writer_emit_pointer (acfg
->w
, s
);
676 mono_img_writer_emit_pointer (acfg
->w
, target
);
681 emit_int16 (MonoAotCompile
*acfg
, int value
)
683 mono_img_writer_emit_int16 (acfg
->w
, value
);
687 emit_int32 (MonoAotCompile
*acfg
, int value
)
689 mono_img_writer_emit_int32 (acfg
->w
, value
);
693 emit_symbol_diff (MonoAotCompile
*acfg
, const char *end
, const char* start
, int offset
)
695 mono_img_writer_emit_symbol_diff (acfg
->w
, end
, start
, offset
);
699 emit_zero_bytes (MonoAotCompile
*acfg
, int num
)
701 mono_img_writer_emit_zero_bytes (acfg
->w
, num
);
705 emit_byte (MonoAotCompile
*acfg
, guint8 val
)
707 mono_img_writer_emit_byte (acfg
->w
, val
);
710 #if defined(TARGET_WIN32) && defined(TARGET_X86)
712 static G_GNUC_UNUSED
void
713 emit_global_inner (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
715 const char * mangled_symbol_name
= name
;
716 char * mangled_symbol_name_alloc
= NULL
;
718 mangled_symbol_name_alloc
= mangle_symbol_alloc (name
);
719 mangled_symbol_name
= mangled_symbol_name_alloc
;
721 if (0 != g_strcasecmp (name
, mangled_symbol_name
)) {
722 mono_img_writer_emit_label (acfg
->w
, mangled_symbol_name
);
724 mono_img_writer_emit_global (acfg
->w
, mangled_symbol_name
, func
);
726 if (NULL
!= mangled_symbol_name_alloc
) {
727 g_free (mangled_symbol_name_alloc
);
733 static G_GNUC_UNUSED
void
734 emit_global_inner (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
736 mono_img_writer_emit_global (acfg
->w
, name
, func
);
741 static inline gboolean
742 link_shared_library (MonoAotCompile
*acfg
)
744 return !acfg
->aot_opts
.static_link
&& !acfg
->aot_opts
.asm_only
;
747 static inline gboolean
748 add_to_global_symbol_table (MonoAotCompile
*acfg
)
750 #ifdef TARGET_WIN32_MSVC
751 return acfg
->aot_opts
.no_dlsym
|| link_shared_library (acfg
);
753 return acfg
->aot_opts
.no_dlsym
;
758 emit_global (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
760 if (add_to_global_symbol_table (acfg
))
761 g_ptr_array_add (acfg
->globals
, g_strdup (name
));
763 if (acfg
->aot_opts
.no_dlsym
) {
764 mono_img_writer_emit_local_symbol (acfg
->w
, name
, NULL
, func
);
766 emit_global_inner (acfg
, name
, func
);
771 emit_symbol_size (MonoAotCompile
*acfg
, const char *name
, const char *end_label
)
773 mono_img_writer_emit_symbol_size (acfg
->w
, name
, end_label
);
776 /* Emit a symbol which is referenced by the MonoAotFileInfo structure */
778 emit_info_symbol (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
780 char symbol
[MAX_SYMBOL_SIZE
];
783 emit_label (acfg
, name
);
784 /* LLVM generated code references this */
785 sprintf (symbol
, "%s%s%s", acfg
->user_symbol_prefix
, acfg
->global_prefix
, name
);
786 emit_label (acfg
, symbol
);
788 #ifndef TARGET_WIN32_MSVC
789 emit_global_inner (acfg
, symbol
, FALSE
);
791 emit_global_inner (acfg
, symbol
, func
);
794 emit_label (acfg
, name
);
799 emit_string_symbol (MonoAotCompile
*acfg
, const char *name
, const char *value
)
802 mono_llvm_emit_aot_data (name
, (guint8
*)value
, strlen (value
) + 1);
806 mono_img_writer_emit_section_change (acfg
->w
, RODATA_SECT
, 1);
808 /* On apple, all symbols need to be aligned to avoid warnings from ld */
809 emit_alignment (acfg
, 4);
811 mono_img_writer_emit_label (acfg
->w
, name
);
812 mono_img_writer_emit_string (acfg
->w
, value
);
815 static G_GNUC_UNUSED
void
816 emit_uleb128 (MonoAotCompile
*acfg
, guint32 value
)
819 guint8 b
= value
& 0x7f;
821 if (value
!= 0) /* more bytes to come */
827 static G_GNUC_UNUSED
void
828 emit_sleb128 (MonoAotCompile
*acfg
, gint64 value
)
831 gboolean negative
= (value
< 0);
838 /* the following is unnecessary if the
839 * implementation of >>= uses an arithmetic rather
840 * than logical shift for a signed left operand
844 value
|= - ((gint64
)1 <<(size
- 7));
845 /* sign bit of byte is second high order bit (0x40) */
846 if ((value
== 0 && !(byte
& 0x40)) ||
847 (value
== -1 && (byte
& 0x40)))
851 emit_byte (acfg
, byte
);
855 static G_GNUC_UNUSED
void
856 encode_uleb128 (guint32 value
, guint8
*buf
, guint8
**endbuf
)
861 guint8 b
= value
& 0x7f;
863 if (value
!= 0) /* more bytes to come */
871 static G_GNUC_UNUSED
void
872 encode_sleb128 (gint32 value
, guint8
*buf
, guint8
**endbuf
)
875 gboolean negative
= (value
< 0);
883 /* the following is unnecessary if the
884 * implementation of >>= uses an arithmetic rather
885 * than logical shift for a signed left operand
889 value
|= - (1 <<(size
- 7));
890 /* sign bit of byte is second high order bit (0x40) */
891 if ((value
== 0 && !(byte
& 0x40)) ||
892 (value
== -1 && (byte
& 0x40)))
903 encode_int (gint32 val
, guint8
*buf
, guint8
**endbuf
)
906 buf
[0] = (val
>> 0) & 0xff;
907 buf
[1] = (val
>> 8) & 0xff;
908 buf
[2] = (val
>> 16) & 0xff;
909 buf
[3] = (val
>> 24) & 0xff;
915 encode_int16 (guint16 val
, guint8
*buf
, guint8
**endbuf
)
917 buf
[0] = (val
>> 0) & 0xff;
918 buf
[1] = (val
>> 8) & 0xff;
924 encode_string (const char *s
, guint8
*buf
, guint8
**endbuf
)
926 int len
= strlen (s
);
928 memcpy (buf
, s
, len
+ 1);
929 *endbuf
= buf
+ len
+ 1;
933 emit_unset_mode (MonoAotCompile
*acfg
)
935 mono_img_writer_emit_unset_mode (acfg
->w
);
938 static G_GNUC_UNUSED
void
939 emit_set_thumb_mode (MonoAotCompile
*acfg
)
941 emit_unset_mode (acfg
);
942 fprintf (acfg
->fp
, ".code 16\n");
945 static G_GNUC_UNUSED
void
946 emit_set_arm_mode (MonoAotCompile
*acfg
)
948 emit_unset_mode (acfg
);
949 fprintf (acfg
->fp
, ".code 32\n");
953 emit_code_bytes (MonoAotCompile
*acfg
, const guint8
* buf
, int size
)
958 g_assert (size
% 4 == 0);
959 emit_unset_mode (acfg
);
960 for (i
= 0; i
< size
; i
+= 4)
961 fprintf (acfg
->fp
, "%s 0x%x\n", acfg
->inst_directive
, *(guint32
*)(buf
+ i
));
963 emit_bytes (acfg
, buf
, size
);
967 /* ARCHITECTURE SPECIFIC CODE */
969 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC) || defined(TARGET_ARM64) || defined (TARGET_RISCV)
970 #define EMIT_DWARF_INFO 1
973 #ifdef TARGET_WIN32_MSVC
974 #undef EMIT_DWARF_INFO
975 #define EMIT_WIN32_CODEVIEW_INFO
978 #ifdef EMIT_WIN32_UNWIND_INFO
979 static UnwindInfoSectionCacheItem
*
980 get_cached_unwind_info_section_item_win32 (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
);
983 free_unwind_info_section_cache_win32 (MonoAotCompile
*acfg
);
986 emit_unwind_info_data_win32 (MonoAotCompile
*acfg
, PUNWIND_INFO unwind_info
);
989 emit_unwind_info_sections_win32 (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
);
993 arch_free_unwind_info_section_cache (MonoAotCompile
*acfg
)
995 #ifdef EMIT_WIN32_UNWIND_INFO
996 free_unwind_info_section_cache_win32 (acfg
);
1001 arch_emit_unwind_info_sections (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
)
1003 #ifdef EMIT_WIN32_UNWIND_INFO
1004 gboolean own_unwind_ops
= FALSE
;
1006 unwind_ops
= mono_unwind_get_cie_program ();
1007 own_unwind_ops
= TRUE
;
1010 emit_unwind_info_sections_win32 (acfg
, function_start
, function_end
, unwind_ops
);
1013 mono_free_unwind_info (unwind_ops
);
1017 #if defined(TARGET_ARM)
1018 #define AOT_FUNC_ALIGNMENT 4
1020 #define AOT_FUNC_ALIGNMENT 16
1023 #if defined(TARGET_POWERPC64) && !defined(MONO_ARCH_ILP32)
1024 #define PPC_LD_OP "ld"
1025 #define PPC_LDX_OP "ldx"
1027 #define PPC_LD_OP "lwz"
1028 #define PPC_LDX_OP "lwzx"
1031 #ifdef TARGET_X86_64_WIN32_MSVC
1032 #define AOT_TARGET_STR "AMD64 (WIN32) (MSVC codegen)"
1034 #define AOT_TARGET_STR "AMD64"
1039 #define AOT_TARGET_STR "ARM (MACH)"
1041 #define AOT_TARGET_STR "ARM (!MACH)"
1047 #define AOT_TARGET_STR "ARM64 (MACH)"
1049 #define AOT_TARGET_STR "ARM64 (!MACH)"
1053 #ifdef TARGET_POWERPC64
1054 #ifdef MONO_ARCH_ILP32
1055 #define AOT_TARGET_STR "POWERPC64 (mono ilp32)"
1057 #define AOT_TARGET_STR "POWERPC64 (!mono ilp32)"
1060 #ifdef TARGET_POWERPC
1061 #ifdef MONO_ARCH_ILP32
1062 #define AOT_TARGET_STR "POWERPC (mono ilp32)"
1064 #define AOT_TARGET_STR "POWERPC (!mono ilp32)"
1069 #ifdef TARGET_RISCV32
1070 #define AOT_TARGET_STR "RISCV32"
1073 #ifdef TARGET_RISCV64
1074 #define AOT_TARGET_STR "RISCV64"
1079 #define AOT_TARGET_STR "X86 (WIN32)"
1081 #define AOT_TARGET_STR "X86"
1085 #ifndef AOT_TARGET_STR
1086 #define AOT_TARGET_STR ""
1090 arch_init (MonoAotCompile
*acfg
)
1092 acfg
->llc_args
= g_string_new ("");
1093 acfg
->as_args
= g_string_new ("");
1094 acfg
->llvm_owriter_supported
= TRUE
;
1097 * The prefix LLVM likes to put in front of symbol names on darwin.
1098 * The mach-os specs require this for globals, but LLVM puts them in front of all
1099 * symbols. We need to handle this, since we need to refer to LLVM generated
1102 acfg
->llvm_label_prefix
= "";
1103 acfg
->user_symbol_prefix
= "";
1105 #if TARGET_X86 || TARGET_AMD64
1106 const gboolean has_custom_args
= !!acfg
->aot_opts
.llvm_llc
;
1109 #if defined(TARGET_X86)
1110 g_string_append_printf (acfg
->llc_args
, " -march=x86 %s", has_custom_args
? "" : "-mcpu=generic");
1113 #if defined(TARGET_AMD64)
1114 g_string_append_printf (acfg
->llc_args
, " -march=x86-64 %s", has_custom_args
? "" : "-mcpu=generic");
1116 acfg
->align_pad_value
= 0x90;
1119 if (mono_use_fast_math
) {
1120 // same parameters are passed to opt and LLVM JIT
1121 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");
1125 if (acfg
->aot_opts
.mtriple
&& strstr (acfg
->aot_opts
.mtriple
, "darwin")) {
1126 g_string_append (acfg
->llc_args
, "-mattr=+v6");
1128 if (!(acfg
->aot_opts
.mtriple
&& strstr (acfg
->aot_opts
.mtriple
, "thumb")))
1129 g_string_append (acfg
->llc_args
, " -march=arm");
1131 if (acfg
->aot_opts
.mtriple
&& strstr (acfg
->aot_opts
.mtriple
, "ios")) {
1132 g_string_append (acfg
->llc_args
, " -mattr=+v7");
1133 g_string_append (acfg
->llc_args
, " -exception-model=dwarf -disable-fp-elim");
1136 #if defined(ARM_FPU_VFP_HARD)
1137 g_string_append (acfg
->llc_args
, " -mattr=+vfp2,-neon,+d16 -float-abi=hard");
1138 g_string_append (acfg
->as_args
, " -mfpu=vfp3");
1139 #elif defined(ARM_FPU_VFP)
1140 g_string_append (acfg
->llc_args
, " -mattr=+vfp2,-neon,+d16");
1141 g_string_append (acfg
->as_args
, " -mfpu=vfp3");
1143 g_string_append (acfg
->llc_args
, " -mattr=+soft-float");
1146 if (acfg
->aot_opts
.mtriple
&& strstr (acfg
->aot_opts
.mtriple
, "thumb"))
1147 acfg
->thumb_mixed
= TRUE
;
1149 if (acfg
->aot_opts
.mtriple
)
1150 mono_arch_set_target (acfg
->aot_opts
.mtriple
);
1154 g_string_append (acfg
->llc_args
, " -march=aarch64");
1155 acfg
->inst_directive
= ".inst";
1156 if (acfg
->aot_opts
.mtriple
)
1157 mono_arch_set_target (acfg
->aot_opts
.mtriple
);
1161 acfg
->user_symbol_prefix
= "_";
1162 acfg
->llvm_label_prefix
= "_";
1163 acfg
->inst_directive
= ".word";
1164 acfg
->need_no_dead_strip
= TRUE
;
1165 acfg
->aot_opts
.gnu_asm
= TRUE
;
1168 #if defined(__linux__) && !defined(TARGET_ARM)
1169 acfg
->need_pt_gnu_stack
= TRUE
;
1173 if (acfg
->aot_opts
.mtriple
)
1174 mono_arch_set_target (acfg
->aot_opts
.mtriple
);
1176 #ifdef TARGET_RISCV64
1178 g_string_append (acfg
->as_args
, " -march=rv64i ");
1179 #ifdef RISCV_FPABI_DOUBLE
1180 g_string_append (acfg
->as_args
, " -mabi=lp64d");
1182 g_string_append (acfg
->as_args
, " -mabi=lp64");
1187 g_string_append (acfg
->as_args
, " -march=rv32i ");
1188 #ifdef RISCV_FPABI_DOUBLE
1189 g_string_append (acfg
->as_args
, " -mabi=ilp32d ");
1191 g_string_append (acfg
->as_args
, " -mabi=ilp32 ");
1199 acfg
->global_symbols
= TRUE
;
1202 #ifdef TARGET_ANDROID
1203 acfg
->llvm_owriter_supported
= FALSE
;
1210 /* Load the contents of GOT_SLOT into dreg, clobbering ip0 */
1212 arm64_emit_load_got_slot (MonoAotCompile
*acfg
, int dreg
, int got_slot
)
1216 g_assert (acfg
->fp
);
1217 emit_unset_mode (acfg
);
1219 offset
= (int)(got_slot
* sizeof (target_mgreg_t
));
1221 /* clang's integrated assembler */
1222 fprintf (acfg
->fp
, "adrp x16, %s@PAGE+%d\n", acfg
->got_symbol
, offset
& 0xfffff000);
1223 fprintf (acfg
->fp
, "add x16, x16, %s@PAGEOFF\n", acfg
->got_symbol
);
1224 fprintf (acfg
->fp
, "ldr x%d, [x16, #%d]\n", dreg
, offset
& 0xfff);
1227 fprintf (acfg
->fp
, "adrp x16, %s+%d\n", acfg
->got_symbol
, offset
& 0xfffff000);
1228 fprintf (acfg
->fp
, "add x16, x16, :lo12:%s\n", acfg
->got_symbol
);
1229 fprintf (acfg
->fp
, "ldr x%d, [x16, %d]\n", dreg
, offset
& 0xfff);
1234 arm64_emit_objc_selector_ref (MonoAotCompile
*acfg
, guint8
*code
, int index
, int *code_size
)
1238 g_assert (acfg
->fp
);
1239 emit_unset_mode (acfg
);
1241 /* ldr rt, target */
1242 reg
= arm_get_ldr_lit_reg (code
);
1244 fprintf (acfg
->fp
, "adrp x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGE\n", reg
, index
);
1245 fprintf (acfg
->fp
, "add x%d, x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGEOFF\n", reg
, reg
, index
);
1246 fprintf (acfg
->fp
, "ldr x%d, [x%d]\n", reg
, reg
);
1252 arm64_emit_direct_call (MonoAotCompile
*acfg
, const char *target
, gboolean external
, gboolean thumb
, MonoJumpInfo
*ji
, int *call_size
)
1254 g_assert (acfg
->fp
);
1255 emit_unset_mode (acfg
);
1256 if (ji
&& ji
->relocation
== MONO_R_ARM64_B
) {
1257 fprintf (acfg
->fp
, "b %s\n", target
);
1260 g_assert (ji
->relocation
== MONO_R_ARM64_BL
);
1261 fprintf (acfg
->fp
, "bl %s\n", target
);
1267 arm64_emit_got_access (MonoAotCompile
*acfg
, guint8
*code
, int got_slot
, int *code_size
)
1271 /* ldr rt, target */
1272 reg
= arm_get_ldr_lit_reg (code
);
1273 arm64_emit_load_got_slot (acfg
, reg
, got_slot
);
1278 arm64_emit_plt_entry (MonoAotCompile
*acfg
, const char *got_symbol
, int offset
, int info_offset
)
1280 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
/ sizeof (target_mgreg_t
));
1281 fprintf (acfg
->fp
, "br x16\n");
1282 /* Used by mono_aot_get_plt_info_offset () */
1283 fprintf (acfg
->fp
, "%s %d\n", acfg
->inst_directive
, info_offset
);
1287 arm64_emit_tramp_page_common_code (MonoAotCompile
*acfg
, int pagesize
, int arg_reg
, int *size
)
1293 /* The common code */
1296 /* The trampoline address is in IP0 */
1297 arm_movzx (code
, ARMREG_IP1
, imm
& 0xffff, 0);
1298 arm_movkx (code
, ARMREG_IP1
, (imm
>> 16) & 0xffff, 16);
1299 /* Compute the data slot address */
1300 arm_subx (code
, ARMREG_IP0
, ARMREG_IP0
, ARMREG_IP1
);
1301 /* Trampoline argument */
1302 arm_ldrx (code
, arg_reg
, ARMREG_IP0
, 0);
1304 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP0
, 8);
1305 arm_brx (code
, ARMREG_IP0
);
1308 emit_code_bytes (acfg
, buf
, code
- buf
);
1314 arm64_emit_tramp_page_specific_code (MonoAotCompile
*acfg
, int pagesize
, int common_tramp_size
, int specific_tramp_size
)
1320 count
= (pagesize
- common_tramp_size
) / specific_tramp_size
;
1321 for (i
= 0; i
< count
; ++i
) {
1323 arm_adrx (code
, ARMREG_IP0
, code
);
1324 /* Branch to the generic code */
1325 arm_b (code
, code
- 4 - (i
* specific_tramp_size
) - common_tramp_size
);
1326 /* This has to be 2 pointers long */
1329 g_assert (code
- buf
== specific_tramp_size
);
1330 emit_code_bytes (acfg
, buf
, code
- buf
);
1335 arm64_emit_specific_trampoline_pages (MonoAotCompile
*acfg
)
1339 guint8
*labels
[16];
1340 int common_tramp_size
;
1341 int specific_tramp_size
= 2 * 8;
1345 if (!acfg
->aot_opts
.use_trampolines_page
)
1349 /* Have to match the target pagesize */
1352 pagesize
= mono_pagesize ();
1354 acfg
->tramp_page_size
= pagesize
;
1356 /* The specific trampolines */
1357 sprintf (symbol
, "%sspecific_trampolines_page", acfg
->user_symbol_prefix
);
1358 emit_alignment (acfg
, pagesize
);
1359 emit_global (acfg
, symbol
, TRUE
);
1360 emit_label (acfg
, symbol
);
1362 /* The common code */
1363 arm64_emit_tramp_page_common_code (acfg
, pagesize
, ARMREG_IP1
, &common_tramp_size
);
1364 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_SPECIFIC
] = common_tramp_size
;
1366 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1368 /* The rgctx trampolines */
1369 /* These are the same as the specific trampolines, but they load the argument into MONO_ARCH_RGCTX_REG */
1370 sprintf (symbol
, "%srgctx_trampolines_page", acfg
->user_symbol_prefix
);
1371 emit_alignment (acfg
, pagesize
);
1372 emit_global (acfg
, symbol
, TRUE
);
1373 emit_label (acfg
, symbol
);
1375 /* The common code */
1376 arm64_emit_tramp_page_common_code (acfg
, pagesize
, MONO_ARCH_RGCTX_REG
, &common_tramp_size
);
1377 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_STATIC_RGCTX
] = common_tramp_size
;
1379 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1381 /* The gsharedvt arg trampolines */
1382 /* These are the same as the specific trampolines */
1383 sprintf (symbol
, "%sgsharedvt_arg_trampolines_page", acfg
->user_symbol_prefix
);
1384 emit_alignment (acfg
, pagesize
);
1385 emit_global (acfg
, symbol
, TRUE
);
1386 emit_label (acfg
, symbol
);
1388 arm64_emit_tramp_page_common_code (acfg
, pagesize
, ARMREG_IP1
, &common_tramp_size
);
1389 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = common_tramp_size
;
1391 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1393 /* Unbox arbitrary trampolines */
1394 sprintf (symbol
, "%sunbox_arbitrary_trampolines_page", acfg
->user_symbol_prefix
);
1395 emit_alignment (acfg
, pagesize
);
1396 emit_global (acfg
, symbol
, TRUE
);
1397 emit_label (acfg
, symbol
);
1402 /* Unbox this arg */
1403 arm_addx_imm (code
, ARMREG_R0
, ARMREG_R0
, MONO_ABI_SIZEOF (MonoObject
));
1405 /* The trampoline address is in IP0 */
1406 arm_movzx (code
, ARMREG_IP1
, imm
& 0xffff, 0);
1407 arm_movkx (code
, ARMREG_IP1
, (imm
>> 16) & 0xffff, 16);
1408 /* Compute the data slot address */
1409 arm_subx (code
, ARMREG_IP0
, ARMREG_IP0
, ARMREG_IP1
);
1411 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP0
, 0);
1412 arm_brx (code
, ARMREG_IP0
);
1415 emit_code_bytes (acfg
, buf
, code
- buf
);
1417 common_tramp_size
= code
- buf
;
1418 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_UNBOX_ARBITRARY
] = common_tramp_size
;
1420 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1422 /* The IMT trampolines */
1423 sprintf (symbol
, "%simt_trampolines_page", acfg
->user_symbol_prefix
);
1424 emit_alignment (acfg
, pagesize
);
1425 emit_global (acfg
, symbol
, TRUE
);
1426 emit_label (acfg
, symbol
);
1430 /* The trampoline address is in IP0 */
1431 arm_movzx (code
, ARMREG_IP1
, imm
& 0xffff, 0);
1432 arm_movkx (code
, ARMREG_IP1
, (imm
>> 16) & 0xffff, 16);
1433 /* Compute the data slot address */
1434 arm_subx (code
, ARMREG_IP0
, ARMREG_IP0
, ARMREG_IP1
);
1435 /* Trampoline argument */
1436 arm_ldrx (code
, ARMREG_IP1
, ARMREG_IP0
, 0);
1438 /* Same as arch_emit_imt_trampoline () */
1440 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 0);
1441 arm_cmpx (code
, ARMREG_IP0
, MONO_ARCH_RGCTX_REG
);
1443 arm_bcc (code
, ARMCOND_EQ
, 0);
1445 /* End-of-loop check */
1447 arm_cbzx (code
, ARMREG_IP0
, 0);
1450 arm_addx_imm (code
, ARMREG_IP1
, ARMREG_IP1
, 2 * 8);
1451 arm_b (code
, labels
[0]);
1454 mono_arm_patch (labels
[1], code
, MONO_R_ARM64_BCC
);
1455 /* Load vtable slot addr */
1456 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 8);
1457 /* Load vtable slot */
1458 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP0
, 0);
1459 arm_brx (code
, ARMREG_IP0
);
1462 mono_arm_patch (labels
[2], code
, MONO_R_ARM64_CBZ
);
1463 /* Load fail addr */
1464 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 8);
1465 arm_brx (code
, ARMREG_IP0
);
1467 emit_code_bytes (acfg
, buf
, code
- buf
);
1469 common_tramp_size
= code
- buf
;
1470 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_IMT
] = common_tramp_size
;
1472 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1476 arm64_emit_specific_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1478 /* Load argument from second GOT slot */
1479 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
+ 1);
1480 /* Load generic trampoline address from first GOT slot */
1481 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
);
1482 fprintf (acfg
->fp
, "br x16\n");
1483 *tramp_size
= 7 * 4;
1487 arm64_emit_unbox_trampoline (MonoAotCompile
*acfg
, MonoCompile
*cfg
, MonoMethod
*method
, const char *call_target
)
1489 emit_unset_mode (acfg
);
1490 fprintf (acfg
->fp
, "add x0, x0, %d\n", (int)(MONO_ABI_SIZEOF (MonoObject
)));
1491 fprintf (acfg
->fp
, "b %s\n", call_target
);
1495 arm64_emit_static_rgctx_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1497 /* Similar to the specific trampolines, but use the rgctx reg instead of ip1 */
1499 /* Load argument from first GOT slot */
1500 arm64_emit_load_got_slot (acfg
, MONO_ARCH_RGCTX_REG
, offset
);
1501 /* Load generic trampoline address from second GOT slot */
1502 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
+ 1);
1503 fprintf (acfg
->fp
, "br x16\n");
1504 *tramp_size
= 7 * 4;
1508 arm64_emit_imt_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1511 guint8
*code
, *labels
[16];
1513 /* Load parameter from GOT slot into ip1 */
1514 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
);
1518 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 0);
1519 arm_cmpx (code
, ARMREG_IP0
, MONO_ARCH_RGCTX_REG
);
1521 arm_bcc (code
, ARMCOND_EQ
, 0);
1523 /* End-of-loop check */
1525 arm_cbzx (code
, ARMREG_IP0
, 0);
1528 arm_addx_imm (code
, ARMREG_IP1
, ARMREG_IP1
, 2 * 8);
1529 arm_b (code
, labels
[0]);
1532 mono_arm_patch (labels
[1], code
, MONO_R_ARM64_BCC
);
1533 /* Load vtable slot addr */
1534 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 8);
1535 /* Load vtable slot */
1536 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP0
, 0);
1537 arm_brx (code
, ARMREG_IP0
);
1540 mono_arm_patch (labels
[2], code
, MONO_R_ARM64_CBZ
);
1541 /* Load fail addr */
1542 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 8);
1543 arm_brx (code
, ARMREG_IP0
);
1545 emit_code_bytes (acfg
, buf
, code
- buf
);
1547 *tramp_size
= code
- buf
+ (3 * 4);
1551 arm64_emit_gsharedvt_arg_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1553 /* Similar to the specific trampolines, but the address is in the second slot */
1554 /* Load argument from first GOT slot */
1555 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
);
1556 /* Load generic trampoline address from second GOT slot */
1557 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
+ 1);
1558 fprintf (acfg
->fp
, "br x16\n");
1559 *tramp_size
= 7 * 4;
1565 #ifdef MONO_ARCH_AOT_SUPPORTED
1567 * arch_emit_direct_call:
1569 * Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
1573 arch_emit_direct_call (MonoAotCompile
*acfg
, const char *target
, gboolean external
, gboolean thumb
, MonoJumpInfo
*ji
, int *call_size
)
1575 #if defined(TARGET_X86) || defined(TARGET_AMD64)
1576 /* Need to make sure this is exactly 5 bytes long */
1577 emit_unset_mode (acfg
);
1578 fprintf (acfg
->fp
, "call %s\n", target
);
1580 #elif defined(TARGET_ARM)
1581 emit_unset_mode (acfg
);
1583 fprintf (acfg
->fp
, "blx %s\n", target
);
1585 fprintf (acfg
->fp
, "bl %s\n", target
);
1587 #elif defined(TARGET_ARM64)
1588 arm64_emit_direct_call (acfg
, target
, external
, thumb
, ji
, call_size
);
1589 #elif defined(TARGET_POWERPC)
1590 emit_unset_mode (acfg
);
1591 fprintf (acfg
->fp
, "bl %s\n", target
);
1594 g_assert_not_reached ();
1601 * - we use an approach similar to the x86 abi: reserve a register (r30) to hold
1603 * - The full-aot trampolines need access to the GOT of mscorlib, so we store
1604 * in in the 2. slot of every GOT, and require every method to place the GOT
1605 * address in r30, even when it doesn't access the GOT otherwise. This way,
1606 * the trampolines can compute the mscorlib GOT address by loading 4(r30).
1611 * PPC64 uses function descriptors which greatly complicate all code, since
1612 * these are used very inconsistently in the runtime. Some functions like
1613 * mono_compile_method () return ftn descriptors, while others like the
1614 * trampoline creation functions do not.
1615 * We assume that all GOT slots contain function descriptors, and create
1616 * descriptors in aot-runtime.c when needed.
1617 * The ppc64 abi uses r2 to hold the address of the TOC/GOT, which is loaded
1618 * from function descriptors, we could do the same, but it would require
1619 * rewriting all the ppc/aot code to handle function descriptors properly.
1620 * So instead, we use the same approach as on PPC32.
1621 * This is a horrible mess, but fixing it would probably lead to an even bigger
1627 * - similar to the PPC32 design, we reserve EBX to hold the GOT pointer.
1630 #ifdef MONO_ARCH_AOT_SUPPORTED
1632 * arch_emit_got_offset:
1634 * The memory pointed to by CODE should hold native code for computing the GOT
1635 * address (OP_LOAD_GOTADDR). Emit this code while patching it with the offset
1636 * between code and the GOT. CODE_SIZE is set to the number of bytes emitted.
1639 arch_emit_got_offset (MonoAotCompile
*acfg
, guint8
*code
, int *code_size
)
1641 #if defined(TARGET_POWERPC64)
1642 emit_unset_mode (acfg
);
1644 * The ppc32 code doesn't seem to work on ppc64, the assembler complains about
1645 * unsupported relocations. So we store the got address into the .Lgot_addr
1646 * symbol which is in the text segment, compute its address, and load it.
1648 fprintf (acfg
->fp
, ".L%d:\n", acfg
->label_generator
);
1649 fprintf (acfg
->fp
, "lis 0, (.Lgot_addr + 4 - .L%d)@h\n", acfg
->label_generator
);
1650 fprintf (acfg
->fp
, "ori 0, 0, (.Lgot_addr + 4 - .L%d)@l\n", acfg
->label_generator
);
1651 fprintf (acfg
->fp
, "add 30, 30, 0\n");
1652 fprintf (acfg
->fp
, "%s 30, 0(30)\n", PPC_LD_OP
);
1653 acfg
->label_generator
++;
1655 #elif defined(TARGET_POWERPC)
1656 emit_unset_mode (acfg
);
1657 fprintf (acfg
->fp
, ".L%d:\n", acfg
->label_generator
);
1658 fprintf (acfg
->fp
, "lis 0, (%s + 4 - .L%d)@h\n", acfg
->got_symbol
, acfg
->label_generator
);
1659 fprintf (acfg
->fp
, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg
->got_symbol
, acfg
->label_generator
);
1660 acfg
->label_generator
++;
1663 guint32 offset
= mono_arch_get_patch_offset (code
);
1664 emit_bytes (acfg
, code
, offset
);
1665 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", offset
);
1667 *code_size
= offset
+ 4;
1672 * arch_emit_got_access:
1674 * The memory pointed to by CODE should hold native code for loading a GOT
1675 * slot (OP_AOTCONST/OP_GOT_ENTRY). Emit this code while patching it so it accesses the
1676 * GOT slot GOT_SLOT. CODE_SIZE is set to the number of bytes emitted.
1679 arch_emit_got_access (MonoAotCompile
*acfg
, const char *got_symbol
, guint8
*code
, int got_slot
, int *code_size
)
1682 /* mov reg, got+offset(%rip) */
1684 /* The GOT symbol is in the LLVM module, the clang assembler has problems emitting symbol diffs for it */
1688 /* Decode reg, see amd64_mov_reg_membase () */
1689 rex_r
= code
[0] & AMD64_REX_R
;
1690 g_assert (code
[0] == 0x49 + rex_r
);
1691 g_assert (code
[1] == 0x8b);
1692 dreg
= ((code
[2] >> 3) & 0x7) + (rex_r
? 8 : 0);
1694 emit_unset_mode (acfg
);
1695 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %s\n", got_symbol
, (unsigned int) ((got_slot
* sizeof (target_mgreg_t
))), mono_arch_regname (dreg
));
1698 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1699 emit_symbol_diff (acfg
, got_symbol
, ".", (unsigned int) ((got_slot
* sizeof (target_mgreg_t
)) - 4));
1700 *code_size
= mono_arch_get_patch_offset (code
) + 4;
1702 #elif defined(TARGET_X86)
1703 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1704 emit_int32 (acfg
, (unsigned int) ((got_slot
* sizeof (target_mgreg_t
))));
1705 *code_size
= mono_arch_get_patch_offset (code
) + 4;
1706 #elif defined(TARGET_ARM)
1707 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1708 emit_symbol_diff (acfg
, got_symbol
, ".", (unsigned int) ((got_slot
* sizeof (target_mgreg_t
))) - 12);
1709 *code_size
= mono_arch_get_patch_offset (code
) + 4;
1710 #elif defined(TARGET_ARM64)
1711 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1712 arm64_emit_got_access (acfg
, code
, got_slot
, code_size
);
1713 #elif defined(TARGET_POWERPC)
1717 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1719 ppc_load32 (code
, ppc_r0
, got_slot
* sizeof (target_mgreg_t
));
1720 g_assert (code
- buf
== 8);
1721 emit_bytes (acfg
, buf
, code
- buf
);
1722 *code_size
= code
- buf
;
1725 g_assert_not_reached ();
1731 #ifdef MONO_ARCH_AOT_SUPPORTED
1733 * arch_emit_objc_selector_ref:
1735 * Emit the implementation of OP_OBJC_GET_SELECTOR, which itself implements @selector(foo:) in objective-c.
1738 arch_emit_objc_selector_ref (MonoAotCompile
*acfg
, guint8
*code
, int index
, int *code_size
)
1740 #if defined(TARGET_ARM)
1741 char symbol1
[MAX_SYMBOL_SIZE
];
1742 char symbol2
[MAX_SYMBOL_SIZE
];
1743 int lindex
= acfg
->objc_selector_index_2
++;
1745 /* Emit ldr.imm/b */
1746 emit_bytes (acfg
, code
, 8);
1748 sprintf (symbol1
, "L_OBJC_SELECTOR_%d", lindex
);
1749 sprintf (symbol2
, "L_OBJC_SELECTOR_REFERENCES_%d", index
);
1751 emit_label (acfg
, symbol1
);
1752 mono_img_writer_emit_unset_mode (acfg
->w
);
1753 fprintf (acfg
->fp
, ".long %s-(%s+12)", symbol2
, symbol1
);
1756 #elif defined(TARGET_ARM64)
1757 arm64_emit_objc_selector_ref (acfg
, code
, index
, code_size
);
1759 g_assert_not_reached ();
1765 * arch_emit_plt_entry:
1767 * Emit code for the PLT entry.
1768 * The plt entry should look like this:
1769 * <indirect jump to GOT_SYMBOL + OFFSET>
1770 * <INFO_OFFSET embedded into the instruction stream>
1773 arch_emit_plt_entry (MonoAotCompile
*acfg
, const char *got_symbol
, int offset
, int info_offset
)
1775 #if defined(TARGET_X86)
1776 /* jmp *<offset>(%ebx) */
1777 emit_byte (acfg
, 0xff);
1778 emit_byte (acfg
, 0xa3);
1779 emit_int32 (acfg
, offset
);
1780 /* Used by mono_aot_get_plt_info_offset */
1781 emit_int32 (acfg
, info_offset
);
1782 #elif defined(TARGET_AMD64)
1783 emit_unset_mode (acfg
);
1784 fprintf (acfg
->fp
, "jmp *%s+%d(%%rip)\n", got_symbol
, offset
);
1785 /* Used by mono_aot_get_plt_info_offset */
1786 emit_int32 (acfg
, info_offset
);
1787 acfg
->stats
.plt_size
+= 10;
1788 #elif defined(TARGET_ARM)
1793 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 0);
1794 ARM_LDR_REG_REG (code
, ARMREG_PC
, ARMREG_PC
, ARMREG_IP
);
1795 emit_bytes (acfg
, buf
, code
- buf
);
1796 emit_symbol_diff (acfg
, got_symbol
, ".", offset
- 4);
1797 /* Used by mono_aot_get_plt_info_offset */
1798 emit_int32 (acfg
, info_offset
);
1799 #elif defined(TARGET_ARM64)
1800 arm64_emit_plt_entry (acfg
, got_symbol
, offset
, info_offset
);
1801 #elif defined(TARGET_POWERPC)
1802 /* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
1803 emit_unset_mode (acfg
);
1804 fprintf (acfg
->fp
, "lis 11, %d@h\n", offset
);
1805 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", offset
);
1806 fprintf (acfg
->fp
, "add 11, 11, 30\n");
1807 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
1808 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1809 fprintf (acfg
->fp
, "%s 2, %d(11)\n", PPC_LD_OP
, (int)sizeof (target_mgreg_t
));
1810 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
1812 fprintf (acfg
->fp
, "mtctr 11\n");
1813 fprintf (acfg
->fp
, "bctr\n");
1814 emit_int32 (acfg
, info_offset
);
1816 g_assert_not_reached ();
1821 * arch_emit_llvm_plt_entry:
1823 * Same as arch_emit_plt_entry, but handles calls from LLVM generated code.
1824 * This is only needed on arm to handle thumb interop.
1827 arch_emit_llvm_plt_entry (MonoAotCompile
*acfg
, const char *got_symbol
, int offset
, int info_offset
)
1829 #if defined(TARGET_ARM)
1830 /* LLVM calls the PLT entries using bl, so these have to be thumb2 */
1831 /* The caller already transitioned to thumb */
1832 /* The code below should be 12 bytes long */
1833 /* clang has trouble encoding these instructions, so emit the binary */
1835 fprintf (acfg
->fp
, "ldr ip, [pc, #8]\n");
1836 /* thumb can't encode ld pc, [pc, ip] */
1837 fprintf (acfg
->fp
, "add ip, pc, ip\n");
1838 fprintf (acfg
->fp
, "ldr ip, [ip, #0]\n");
1839 fprintf (acfg
->fp
, "bx ip\n");
1841 emit_set_thumb_mode (acfg
);
1842 fprintf (acfg
->fp
, ".4byte 0xc008f8df\n");
1843 fprintf (acfg
->fp
, ".2byte 0x44fc\n");
1844 fprintf (acfg
->fp
, ".4byte 0xc000f8dc\n");
1845 fprintf (acfg
->fp
, ".2byte 0x4760\n");
1846 emit_symbol_diff (acfg
, got_symbol
, ".", offset
+ 4);
1847 emit_int32 (acfg
, info_offset
);
1848 emit_unset_mode (acfg
);
1849 emit_set_arm_mode (acfg
);
1851 g_assert_not_reached ();
1855 /* Save unwind_info in the module and emit the offset to the information at symbol */
1856 static void save_unwind_info (MonoAotCompile
*acfg
, char *symbol
, GSList
*unwind_ops
)
1858 guint32 uw_offset
, encoded_len
;
1861 emit_section_change (acfg
, RODATA_SECT
, 0);
1862 emit_global (acfg
, symbol
, FALSE
);
1863 emit_label (acfg
, symbol
);
1865 encoded
= mono_unwind_ops_encode (unwind_ops
, &encoded_len
);
1866 uw_offset
= get_unwind_info_offset (acfg
, encoded
, encoded_len
);
1868 emit_int32 (acfg
, uw_offset
);
1872 * arch_emit_specific_trampoline_pages:
1874 * Emits a page full of trampolines: each trampoline uses its own address to
1875 * lookup both the generic trampoline code and the data argument.
1876 * This page can be remapped in process multiple times so we can get an
1877 * unlimited number of trampolines.
1878 * Specifically this implementation uses the following trick: two memory pages
1879 * are allocated, with the first containing the data and the second containing the trampolines.
1880 * To reduce trampoline size, each trampoline jumps at the start of the page where a common
1881 * implementation does all the lifting.
1882 * Note that the ARM single trampoline size is 8 bytes, exactly like the data that needs to be stored
1883 * on the arm 32 bit system.
1886 arch_emit_specific_trampoline_pages (MonoAotCompile
*acfg
)
1888 #if defined(TARGET_ARM)
1891 guint8
*loop_start
, *loop_branch_back
, *loop_end_check
, *imt_found_check
;
1893 int pagesize
= MONO_AOT_TRAMP_PAGE_SIZE
;
1894 GSList
*unwind_ops
= NULL
;
1895 #define COMMON_TRAMP_SIZE 16
1896 int count
= (pagesize
- COMMON_TRAMP_SIZE
) / 8;
1897 int imm8
, rot_amount
;
1900 if (!acfg
->aot_opts
.use_trampolines_page
)
1903 acfg
->tramp_page_size
= pagesize
;
1905 sprintf (symbol
, "%sspecific_trampolines_page", acfg
->user_symbol_prefix
);
1906 emit_alignment (acfg
, pagesize
);
1907 emit_global (acfg
, symbol
, TRUE
);
1908 emit_label (acfg
, symbol
);
1910 /* emit the generic code first, the trampoline address + 8 is in the lr register */
1912 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1913 ARM_SUB_REG_IMM (code
, ARMREG_LR
, ARMREG_LR
, imm8
, rot_amount
);
1914 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_LR
, -8);
1915 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_LR
, -4);
1917 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
1920 emit_bytes (acfg
, buf
, code
- buf
);
1922 for (i
= 0; i
< count
; ++i
) {
1924 ARM_PUSH (code
, 0x5fff);
1926 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
1927 g_assert (code
- buf
== 8);
1928 emit_bytes (acfg
, buf
, code
- buf
);
1931 /* now the rgctx trampolines: each specific trampolines puts in the ip register
1932 * the instruction pointer address, so the generic trampoline at the start of the page
1933 * subtracts 4096 to get to the data page and loads the values
1934 * We again fit the generic trampiline in 16 bytes.
1936 sprintf (symbol
, "%srgctx_trampolines_page", acfg
->user_symbol_prefix
);
1937 emit_global (acfg
, symbol
, TRUE
);
1938 emit_label (acfg
, symbol
);
1940 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1941 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
1942 ARM_LDR_IMM (code
, MONO_ARCH_RGCTX_REG
, ARMREG_IP
, -8);
1943 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_IP
, -4);
1945 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
1948 emit_bytes (acfg
, buf
, code
- buf
);
1950 for (i
= 0; i
< count
; ++i
) {
1952 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
1954 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
1955 g_assert (code
- buf
== 8);
1956 emit_bytes (acfg
, buf
, code
- buf
);
1960 * gsharedvt arg trampolines: see arch_emit_gsharedvt_arg_trampoline ()
1962 sprintf (symbol
, "%sgsharedvt_arg_trampolines_page", acfg
->user_symbol_prefix
);
1963 emit_global (acfg
, symbol
, TRUE
);
1964 emit_label (acfg
, symbol
);
1966 ARM_PUSH (code
, (1 << ARMREG_R0
) | (1 << ARMREG_R1
) | (1 << ARMREG_R2
) | (1 << ARMREG_R3
));
1967 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1968 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
1969 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_IP
, -8);
1970 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_IP
, -4);
1971 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
1973 emit_bytes (acfg
, buf
, code
- buf
);
1975 for (i
= 0; i
< count
; ++i
) {
1977 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
1979 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
1980 g_assert (code
- buf
== 8);
1981 emit_bytes (acfg
, buf
, code
- buf
);
1984 /* now the unbox arbitrary trampolines: each specific trampolines puts in the ip register
1985 * the instruction pointer address, so the generic trampoline at the start of the page
1986 * subtracts 4096 to get to the data page and loads the target addr.
1987 * We again fit the generic trampoline in 16 bytes.
1989 sprintf (symbol
, "%sunbox_arbitrary_trampolines_page", acfg
->user_symbol_prefix
);
1990 emit_global (acfg
, symbol
, TRUE
);
1991 emit_label (acfg
, symbol
);
1994 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, MONO_ABI_SIZEOF (MonoObject
));
1995 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1996 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
1997 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_IP
, -4);
1999 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
2002 emit_bytes (acfg
, buf
, code
- buf
);
2004 for (i
= 0; i
< count
; ++i
) {
2006 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
2008 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
2009 g_assert (code
- buf
== 8);
2010 emit_bytes (acfg
, buf
, code
- buf
);
2013 /* now the imt trampolines: each specific trampolines puts in the ip register
2014 * the instruction pointer address, so the generic trampoline at the start of the page
2015 * subtracts 4096 to get to the data page and loads the values
2017 #define IMT_TRAMP_SIZE 72
2018 sprintf (symbol
, "%simt_trampolines_page", acfg
->user_symbol_prefix
);
2019 emit_global (acfg
, symbol
, TRUE
);
2020 emit_label (acfg
, symbol
);
2022 /* Need at least two free registers, plus a slot for storing the pc */
2023 ARM_PUSH (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_R2
));
2025 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
2026 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
2027 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_IP
, -8);
2029 /* The IMT method is in v5, r0 has the imt array address */
2032 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_R0
, 0);
2033 ARM_CMP_REG_REG (code
, ARMREG_R1
, ARMREG_V5
);
2034 imt_found_check
= code
;
2035 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2037 /* End-of-loop check */
2038 ARM_CMP_REG_IMM (code
, ARMREG_R1
, 0, 0);
2039 loop_end_check
= code
;
2040 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2043 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, sizeof (target_mgreg_t
) * 2);
2044 loop_branch_back
= code
;
2046 arm_patch (loop_branch_back
, loop_start
);
2049 arm_patch (imt_found_check
, code
);
2050 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2051 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 0);
2052 /* Save it to the third stack slot */
2053 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2054 /* Restore the registers and branch */
2055 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2058 arm_patch (loop_end_check
, code
);
2059 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2060 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2061 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2065 g_assert (code
- buf
== IMT_TRAMP_SIZE
);
2066 emit_bytes (acfg
, buf
, code
- buf
);
2068 for (i
= 0; i
< count
; ++i
) {
2070 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
2072 arm_patch (code
- 4, code
- IMT_TRAMP_SIZE
- 8 * (i
+ 1));
2073 g_assert (code
- buf
== 8);
2074 emit_bytes (acfg
, buf
, code
- buf
);
2077 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_SPECIFIC
] = 16;
2078 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_STATIC_RGCTX
] = 16;
2079 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_IMT
] = 72;
2080 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = 16;
2081 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_UNBOX_ARBITRARY
] = 16;
2083 /* Unwind info for specifc trampolines */
2084 sprintf (symbol
, "%sspecific_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2085 /* We unwind to the original caller, from the stack, since lr is clobbered */
2086 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 14 * sizeof (target_mgreg_t
));
2087 mono_add_unwind_op_offset (unwind_ops
, 0, 0, ARMREG_LR
, -4);
2088 save_unwind_info (acfg
, symbol
, unwind_ops
);
2089 mono_free_unwind_info (unwind_ops
);
2091 sprintf (symbol
, "%sspecific_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2092 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2093 mono_add_unwind_op_def_cfa_offset (unwind_ops
, 4, 0, 14 * sizeof (target_mgreg_t
));
2094 save_unwind_info (acfg
, symbol
, unwind_ops
);
2095 mono_free_unwind_info (unwind_ops
);
2097 /* Unwind info for rgctx trampolines */
2098 sprintf (symbol
, "%srgctx_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2099 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2100 save_unwind_info (acfg
, symbol
, unwind_ops
);
2102 sprintf (symbol
, "%srgctx_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2103 save_unwind_info (acfg
, symbol
, unwind_ops
);
2104 mono_free_unwind_info (unwind_ops
);
2106 /* Unwind info for gsharedvt trampolines */
2107 sprintf (symbol
, "%sgsharedvt_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2108 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2109 mono_add_unwind_op_def_cfa_offset (unwind_ops
, 4, 0, 4 * sizeof (target_mgreg_t
));
2110 save_unwind_info (acfg
, symbol
, unwind_ops
);
2111 mono_free_unwind_info (unwind_ops
);
2113 sprintf (symbol
, "%sgsharedvt_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2114 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2115 save_unwind_info (acfg
, symbol
, unwind_ops
);
2116 mono_free_unwind_info (unwind_ops
);
2118 /* Unwind info for unbox arbitrary trampolines */
2119 sprintf (symbol
, "%sunbox_arbitrary_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2120 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2121 save_unwind_info (acfg
, symbol
, unwind_ops
);
2123 sprintf (symbol
, "%sunbox_arbitrary_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2124 save_unwind_info (acfg
, symbol
, unwind_ops
);
2125 mono_free_unwind_info (unwind_ops
);
2127 /* Unwind info for imt trampolines */
2128 sprintf (symbol
, "%simt_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
2129 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2130 mono_add_unwind_op_def_cfa_offset (unwind_ops
, 4, 0, 3 * sizeof (target_mgreg_t
));
2131 save_unwind_info (acfg
, symbol
, unwind_ops
);
2132 mono_free_unwind_info (unwind_ops
);
2134 sprintf (symbol
, "%simt_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
2135 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
2136 save_unwind_info (acfg
, symbol
, unwind_ops
);
2137 mono_free_unwind_info (unwind_ops
);
2138 #elif defined(TARGET_ARM64)
2139 arm64_emit_specific_trampoline_pages (acfg
);
2144 * arch_emit_specific_trampoline:
2146 * Emit code for a specific trampoline. OFFSET is the offset of the first of
2147 * two GOT slots which contain the generic trampoline address and the trampoline
2148 * argument. TRAMP_SIZE is set to the size of the emitted trampoline.
2151 arch_emit_specific_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2154 * The trampolines created here are variations of the specific
2155 * trampolines created in mono_arch_create_specific_trampoline (). The
2157 * - the generic trampoline address is taken from a got slot.
2158 * - the offset of the got slot where the trampoline argument is stored
2159 * is embedded in the instruction stream, and the generic trampoline
2160 * can load the argument by loading the offset, adding it to the
2161 * address of the trampoline to get the address of the got slot, and
2162 * loading the argument from there.
2163 * - all the trampolines should be of the same length.
2165 #if defined(TARGET_AMD64)
2166 /* This should be exactly 8 bytes long */
2168 /* call *<offset>(%rip) */
2170 emit_unset_mode (acfg
);
2171 fprintf (acfg
->fp
, "call *%s+%d(%%rip)\n", acfg
->got_symbol
, (int)(offset
* sizeof (target_mgreg_t
)));
2172 emit_zero_bytes (acfg
, 2);
2174 emit_byte (acfg
, '\x41');
2175 emit_byte (acfg
, '\xff');
2176 emit_byte (acfg
, '\x15');
2177 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4);
2178 emit_zero_bytes (acfg
, 1);
2180 #elif defined(TARGET_ARM)
2184 /* This should be exactly 20 bytes long */
2187 ARM_PUSH (code
, 0x5fff);
2188 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_PC
, 4);
2189 /* Load the value from the GOT */
2190 ARM_LDR_REG_REG (code
, ARMREG_R1
, ARMREG_PC
, ARMREG_R1
);
2192 ARM_BLX_REG (code
, ARMREG_R1
);
2194 g_assert (code
- buf
== 16);
2197 emit_bytes (acfg
, buf
, code
- buf
);
2199 * Only one offset is needed, since the second one would be equal to the
2202 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4 + 4);
2203 //emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (target_mgreg_t)) - 4 + 8);
2204 #elif defined(TARGET_ARM64)
2205 arm64_emit_specific_trampoline (acfg
, offset
, tramp_size
);
2206 #elif defined(TARGET_POWERPC)
2214 * PPC has no ip relative addressing, so we need to compute the address
2215 * of the mscorlib got. That is slow and complex, so instead, we store it
2216 * in the second got slot of every aot image. The caller already computed
2217 * the address of its got and placed it into r30.
2219 emit_unset_mode (acfg
);
2220 /* Load mscorlib got address */
2221 fprintf (acfg
->fp
, "%s 0, %d(30)\n", PPC_LD_OP
, (int)sizeof (target_mgreg_t
));
2222 /* Load generic trampoline address */
2223 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)(offset
* sizeof (target_mgreg_t
)));
2224 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)(offset
* sizeof (target_mgreg_t
)));
2225 fprintf (acfg
->fp
, "%s 11, 11, 0\n", PPC_LDX_OP
);
2226 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2227 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
2229 fprintf (acfg
->fp
, "mtctr 11\n");
2230 /* Load trampoline argument */
2231 /* On ppc, we pass it normally to the generic trampoline */
2232 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2233 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2234 fprintf (acfg
->fp
, "%s 0, 11, 0\n", PPC_LDX_OP
);
2235 /* Branch to generic trampoline */
2236 fprintf (acfg
->fp
, "bctr\n");
2238 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2239 *tramp_size
= 10 * 4;
2241 *tramp_size
= 9 * 4;
2243 #elif defined(TARGET_X86)
2247 /* Similar to the PPC code above */
2249 /* FIXME: Could this clobber the register needed by get_vcall_slot () ? */
2252 /* Load mscorlib got address */
2253 x86_mov_reg_membase (code
, X86_ECX
, MONO_ARCH_GOT_REG
, sizeof (target_mgreg_t
), 4);
2254 /* Push trampoline argument */
2255 x86_push_membase (code
, X86_ECX
, (offset
+ 1) * sizeof (target_mgreg_t
));
2256 /* Load generic trampoline address */
2257 x86_mov_reg_membase (code
, X86_ECX
, X86_ECX
, offset
* sizeof (target_mgreg_t
), 4);
2258 /* Branch to generic trampoline */
2259 x86_jump_reg (code
, X86_ECX
);
2261 emit_bytes (acfg
, buf
, code
- buf
);
2264 g_assert (code
- buf
== *tramp_size
);
2266 g_assert_not_reached ();
2271 * arch_emit_unbox_trampoline:
2273 * Emit code for the unbox trampoline for METHOD used in the full-aot case.
2274 * CALL_TARGET is the symbol pointing to the native code of METHOD.
2276 * See mono_aot_get_unbox_trampoline.
2279 arch_emit_unbox_trampoline (MonoAotCompile
*acfg
, MonoCompile
*cfg
, MonoMethod
*method
, const char *call_target
)
2281 #if defined(TARGET_AMD64)
2286 this_reg
= mono_arch_get_this_arg_reg (NULL
);
2288 amd64_alu_reg_imm (code
, X86_ADD
, this_reg
, MONO_ABI_SIZEOF (MonoObject
));
2290 emit_bytes (acfg
, buf
, code
- buf
);
2293 emit_unset_mode (acfg
);
2294 fprintf (acfg
->fp
, "jmp %s\n", call_target
);
2296 emit_byte (acfg
, '\xe9');
2297 emit_symbol_diff (acfg
, call_target
, ".", -4);
2299 #elif defined(TARGET_X86)
2306 x86_alu_membase_imm (code
, X86_ADD
, X86_ESP
, this_pos
, MONO_ABI_SIZEOF (MonoObject
));
2308 emit_bytes (acfg
, buf
, code
- buf
);
2311 emit_byte (acfg
, '\xe9');
2312 emit_symbol_diff (acfg
, call_target
, ".", -4);
2313 #elif defined(TARGET_ARM)
2317 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
) {
2318 fprintf (acfg
->fp
, "add r0, r0, #%d\n", (int)MONO_ABI_SIZEOF (MonoObject
));
2319 fprintf (acfg
->fp
, "b %s\n", call_target
);
2320 fprintf (acfg
->fp
, ".arm\n");
2321 fprintf (acfg
->fp
, ".align 2\n");
2327 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, MONO_ABI_SIZEOF (MonoObject
));
2329 emit_bytes (acfg
, buf
, code
- buf
);
2330 /* jump to method */
2331 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
)
2332 fprintf (acfg
->fp
, "\n\tbx %s\n", call_target
);
2334 fprintf (acfg
->fp
, "\n\tb %s\n", call_target
);
2335 #elif defined(TARGET_ARM64)
2336 arm64_emit_unbox_trampoline (acfg
, cfg
, method
, call_target
);
2337 #elif defined(TARGET_POWERPC)
2340 fprintf (acfg
->fp
, "\n\taddi %d, %d, %d\n", this_pos
, this_pos
, (int)MONO_ABI_SIZEOF (MonoObject
));
2341 fprintf (acfg
->fp
, "\n\tb %s\n", call_target
);
2343 g_assert_not_reached ();
2348 * arch_emit_static_rgctx_trampoline:
2350 * Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
2351 * two GOT slots which contain the rgctx argument, and the method to jump to.
2352 * TRAMP_SIZE is set to the size of the emitted trampoline.
2353 * These kinds of trampolines cannot be enumerated statically, since there could
2354 * be one trampoline per method instantiation, so we emit the same code for all
2355 * trampolines, and parameterize them using two GOT slots.
2358 arch_emit_static_rgctx_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2360 #if defined(TARGET_AMD64)
2361 /* This should be exactly 13 bytes long */
2365 emit_unset_mode (acfg
);
2366 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %%r10\n", acfg
->got_symbol
, (int)(offset
* sizeof (target_mgreg_t
)));
2367 fprintf (acfg
->fp
, "jmp *%s+%d(%%rip)\n", acfg
->got_symbol
, (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2369 /* mov <OFFSET>(%rip), %r10 */
2370 emit_byte (acfg
, '\x4d');
2371 emit_byte (acfg
, '\x8b');
2372 emit_byte (acfg
, '\x15');
2373 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4);
2375 /* jmp *<offset>(%rip) */
2376 emit_byte (acfg
, '\xff');
2377 emit_byte (acfg
, '\x25');
2378 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", ((offset
+ 1) * sizeof (target_mgreg_t
)) - 4);
2380 #elif defined(TARGET_ARM)
2384 /* This should be exactly 24 bytes long */
2387 /* Load rgctx value */
2388 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 8);
2389 ARM_LDR_REG_REG (code
, MONO_ARCH_RGCTX_REG
, ARMREG_PC
, ARMREG_IP
);
2390 /* Load branch addr + branch */
2391 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 4);
2392 ARM_LDR_REG_REG (code
, ARMREG_PC
, ARMREG_PC
, ARMREG_IP
);
2394 g_assert (code
- buf
== 16);
2397 emit_bytes (acfg
, buf
, code
- buf
);
2398 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4 + 8);
2399 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", ((offset
+ 1) * sizeof (target_mgreg_t
)) - 4 + 4);
2400 #elif defined(TARGET_ARM64)
2401 arm64_emit_static_rgctx_trampoline (acfg
, offset
, tramp_size
);
2402 #elif defined(TARGET_POWERPC)
2410 * PPC has no ip relative addressing, so we need to compute the address
2411 * of the mscorlib got. That is slow and complex, so instead, we store it
2412 * in the second got slot of every aot image. The caller already computed
2413 * the address of its got and placed it into r30.
2415 emit_unset_mode (acfg
);
2416 /* Load mscorlib got address */
2417 fprintf (acfg
->fp
, "%s 0, %d(30)\n", PPC_LD_OP
, (int)sizeof (target_mgreg_t
));
2419 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)(offset
* sizeof (target_mgreg_t
)));
2420 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)(offset
* sizeof (target_mgreg_t
)));
2421 fprintf (acfg
->fp
, "%s %d, 11, 0\n", PPC_LDX_OP
, MONO_ARCH_RGCTX_REG
);
2422 /* Load target address */
2423 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2424 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)((offset
+ 1) * sizeof (target_mgreg_t
)));
2425 fprintf (acfg
->fp
, "%s 11, 11, 0\n", PPC_LDX_OP
);
2426 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2427 fprintf (acfg
->fp
, "%s 2, %d(11)\n", PPC_LD_OP
, (int)sizeof (target_mgreg_t
));
2428 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
2430 fprintf (acfg
->fp
, "mtctr 11\n");
2431 /* Branch to the target address */
2432 fprintf (acfg
->fp
, "bctr\n");
2434 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2435 *tramp_size
= 11 * 4;
2437 *tramp_size
= 9 * 4;
2440 #elif defined(TARGET_X86)
2444 /* Similar to the PPC code above */
2446 g_assert (MONO_ARCH_RGCTX_REG
!= X86_ECX
);
2449 /* Load mscorlib got address */
2450 x86_mov_reg_membase (code
, X86_ECX
, MONO_ARCH_GOT_REG
, sizeof (target_mgreg_t
), 4);
2452 x86_mov_reg_membase (code
, MONO_ARCH_RGCTX_REG
, X86_ECX
, offset
* sizeof (target_mgreg_t
), 4);
2453 /* Branch to the target address */
2454 x86_jump_membase (code
, X86_ECX
, (offset
+ 1) * sizeof (target_mgreg_t
));
2456 emit_bytes (acfg
, buf
, code
- buf
);
2459 g_assert (code
- buf
== *tramp_size
);
2461 g_assert_not_reached ();
2466 * arch_emit_imt_trampoline:
2468 * Emit an IMT trampoline usable in full-aot mode. The trampoline uses 1 got slot which
2469 * points to an array of pointer pairs. The pairs of the form [key, ptr], where
2470 * key is the IMT key, and ptr holds the address of a memory location holding
2471 * the address to branch to if the IMT arg matches the key. The array is
2472 * terminated by a pair whose key is NULL, and whose ptr is the address of the
2474 * TRAMP_SIZE is set to the size of the emitted trampoline.
2477 arch_emit_imt_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2479 #if defined(TARGET_AMD64)
2481 guint8
*labels
[16];
2483 guint8
*mov_buf_ptr
= mov_buf
;
2485 const int kSizeOfMove
= 7;
2487 code
= buf
= (guint8
*)g_malloc (256);
2489 /* FIXME: Optimize this, i.e. use binary search etc. */
2490 /* Maybe move the body into a separate function (slower, but much smaller) */
2492 /* MONO_ARCH_IMT_SCRATCH_REG is a free register */
2495 emit_unset_mode (acfg
);
2496 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
));
2500 amd64_alu_membase_imm (code
, X86_CMP
, MONO_ARCH_IMT_SCRATCH_REG
, 0, 0);
2502 amd64_branch8 (code
, X86_CC_Z
, 0, FALSE
);
2505 amd64_alu_membase_reg_size (code
, X86_CMP
, MONO_ARCH_IMT_SCRATCH_REG
, 0, MONO_ARCH_IMT_REG
, sizeof (target_mgreg_t
));
2507 amd64_branch8 (code
, X86_CC_Z
, 0, FALSE
);
2510 amd64_alu_reg_imm (code
, X86_ADD
, MONO_ARCH_IMT_SCRATCH_REG
, 2 * sizeof (target_mgreg_t
));
2511 amd64_jump_code (code
, labels
[0]);
2514 mono_amd64_patch (labels
[2], code
);
2515 amd64_mov_reg_membase (code
, MONO_ARCH_IMT_SCRATCH_REG
, MONO_ARCH_IMT_SCRATCH_REG
, sizeof (target_mgreg_t
), sizeof (target_mgreg_t
));
2516 amd64_jump_membase (code
, MONO_ARCH_IMT_SCRATCH_REG
, 0);
2519 mono_amd64_patch (labels
[1], code
);
2520 /* Load fail tramp */
2521 amd64_alu_reg_imm (code
, X86_ADD
, MONO_ARCH_IMT_SCRATCH_REG
, sizeof (target_mgreg_t
));
2522 /* Check if there is a fail tramp */
2523 amd64_alu_membase_imm (code
, X86_CMP
, MONO_ARCH_IMT_SCRATCH_REG
, 0, 0);
2525 amd64_branch8 (code
, X86_CC_Z
, 0, FALSE
);
2526 /* Jump to fail tramp */
2527 amd64_jump_membase (code
, MONO_ARCH_IMT_SCRATCH_REG
, 0);
2530 mono_amd64_patch (labels
[3], code
);
2531 x86_breakpoint (code
);
2534 /* mov <OFFSET>(%rip), MONO_ARCH_IMT_SCRATCH_REG */
2535 amd64_emit_rex (mov_buf_ptr
, sizeof(gpointer
), MONO_ARCH_IMT_SCRATCH_REG
, 0, AMD64_RIP
);
2536 *(mov_buf_ptr
)++ = (unsigned char)0x8b; /* mov opcode */
2537 x86_address_byte (mov_buf_ptr
, 0, MONO_ARCH_IMT_SCRATCH_REG
& 0x7, 5);
2538 emit_bytes (acfg
, mov_buf
, mov_buf_ptr
- mov_buf
);
2539 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) - 4);
2541 emit_bytes (acfg
, buf
, code
- buf
);
2543 *tramp_size
= code
- buf
+ kSizeOfMove
;
2547 #elif defined(TARGET_X86)
2549 guint8
*labels
[16];
2551 code
= buf
= g_malloc (256);
2553 /* Allocate a temporary stack slot */
2554 x86_push_reg (code
, X86_EAX
);
2556 x86_push_reg (code
, X86_EAX
);
2558 /* Load mscorlib got address */
2559 x86_mov_reg_membase (code
, X86_EAX
, MONO_ARCH_GOT_REG
, sizeof (target_mgreg_t
), 4);
2561 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, offset
* sizeof (target_mgreg_t
), 4);
2564 x86_alu_membase_imm (code
, X86_CMP
, X86_EAX
, 0, 0);
2566 x86_branch8 (code
, X86_CC_Z
, FALSE
, 0);
2569 x86_alu_membase_reg (code
, X86_CMP
, X86_EAX
, 0, MONO_ARCH_IMT_REG
);
2571 x86_branch8 (code
, X86_CC_Z
, FALSE
, 0);
2574 x86_alu_reg_imm (code
, X86_ADD
, X86_EAX
, 2 * sizeof (target_mgreg_t
));
2575 x86_jump_code (code
, labels
[0]);
2578 mono_x86_patch (labels
[2], code
);
2579 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, sizeof (target_mgreg_t
), 4);
2580 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, 0, 4);
2581 /* Save the target address to the temporary stack location */
2582 x86_mov_membase_reg (code
, X86_ESP
, 4, X86_EAX
, 4);
2584 x86_pop_reg (code
, X86_EAX
);
2585 /* Jump to the target address */
2589 mono_x86_patch (labels
[1], code
);
2590 /* Load fail tramp */
2591 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, sizeof (target_mgreg_t
), 4);
2592 x86_alu_membase_imm (code
, X86_CMP
, X86_EAX
, 0, 0);
2594 x86_branch8 (code
, X86_CC_Z
, FALSE
, 0);
2595 /* Jump to fail tramp */
2596 x86_mov_membase_reg (code
, X86_ESP
, 4, X86_EAX
, 4);
2597 x86_pop_reg (code
, X86_EAX
);
2601 mono_x86_patch (labels
[3], code
);
2602 x86_breakpoint (code
);
2604 emit_bytes (acfg
, buf
, code
- buf
);
2606 *tramp_size
= code
- buf
;
2610 #elif defined(TARGET_ARM)
2612 guint8
*code
, *code2
, *labels
[16];
2616 /* The IMT method is in v5 */
2618 /* Need at least two free registers, plus a slot for storing the pc */
2619 ARM_PUSH (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_R2
));
2621 /* Load the parameter from the GOT */
2622 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_PC
, 0);
2623 ARM_LDR_REG_REG (code
, ARMREG_R0
, ARMREG_PC
, ARMREG_R0
);
2626 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_R0
, 0);
2627 ARM_CMP_REG_REG (code
, ARMREG_R1
, ARMREG_V5
);
2629 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2631 /* End-of-loop check */
2632 ARM_CMP_REG_IMM (code
, ARMREG_R1
, 0, 0);
2634 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2637 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, sizeof (target_mgreg_t
) * 2);
2640 arm_patch (labels
[4], labels
[1]);
2643 arm_patch (labels
[2], code
);
2644 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2645 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 0);
2646 /* Save it to the third stack slot */
2647 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2648 /* Restore the registers and branch */
2649 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2652 arm_patch (labels
[3], code
);
2653 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2654 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2655 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2659 ARM_LDR_IMM (code2
, ARMREG_R0
, ARMREG_PC
, (code
- (labels
[0] + 8)));
2661 emit_bytes (acfg
, buf
, code
- buf
);
2662 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) + (code
- (labels
[0] + 8)) - 4);
2664 *tramp_size
= code
- buf
+ 4;
2665 #elif defined(TARGET_ARM64)
2666 arm64_emit_imt_trampoline (acfg
, offset
, tramp_size
);
2667 #elif defined(TARGET_POWERPC)
2669 guint8
*code
, *labels
[16];
2673 /* Load the mscorlib got address */
2674 ppc_ldptr (code
, ppc_r12
, sizeof (target_mgreg_t
), ppc_r30
);
2675 /* Load the parameter from the GOT */
2676 ppc_load (code
, ppc_r0
, offset
* sizeof (target_mgreg_t
));
2677 ppc_ldptr_indexed (code
, ppc_r12
, ppc_r12
, ppc_r0
);
2679 /* Load and check key */
2681 ppc_ldptr (code
, ppc_r0
, 0, ppc_r12
);
2682 ppc_cmp (code
, 0, sizeof (target_mgreg_t
) == 8 ? 1 : 0, ppc_r0
, MONO_ARCH_IMT_REG
);
2684 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
2686 /* End-of-loop check */
2687 ppc_cmpi (code
, 0, sizeof (target_mgreg_t
) == 8 ? 1 : 0, ppc_r0
, 0);
2689 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
2692 ppc_addi (code
, ppc_r12
, ppc_r12
, 2 * sizeof (target_mgreg_t
));
2695 mono_ppc_patch (labels
[4], labels
[1]);
2698 mono_ppc_patch (labels
[2], code
);
2699 ppc_ldptr (code
, ppc_r12
, sizeof (target_mgreg_t
), ppc_r12
);
2700 /* r12 now contains the value of the vtable slot */
2701 /* this is not a function descriptor on ppc64 */
2702 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
2703 ppc_mtctr (code
, ppc_r12
);
2704 ppc_bcctr (code
, PPC_BR_ALWAYS
, 0);
2707 mono_ppc_patch (labels
[3], code
);
2711 *tramp_size
= code
- buf
;
2713 emit_bytes (acfg
, buf
, code
- buf
);
2715 g_assert_not_reached ();
2720 #if defined (TARGET_AMD64)
2723 amd64_emit_load_got_slot (MonoAotCompile
*acfg
, int dreg
, int got_slot
)
2726 g_assert (acfg
->fp
);
2727 emit_unset_mode (acfg
);
2729 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %s\n", acfg
->got_symbol
, (unsigned int) ((got_slot
* sizeof (target_mgreg_t
))), mono_arch_regname (dreg
));
2736 * arch_emit_gsharedvt_arg_trampoline:
2738 * Emit code for a gsharedvt arg trampoline. OFFSET is the offset of the first of
2739 * two GOT slots which contain the argument, and the code to jump to.
2740 * TRAMP_SIZE is set to the size of the emitted trampoline.
2741 * These kinds of trampolines cannot be enumerated statically, since there could
2742 * be one trampoline per method instantiation, so we emit the same code for all
2743 * trampolines, and parameterize them using two GOT slots.
2746 arch_emit_gsharedvt_arg_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2748 #if defined(TARGET_X86)
2752 /* Similar to the PPC code above */
2754 g_assert (MONO_ARCH_RGCTX_REG
!= X86_ECX
);
2757 /* Load mscorlib got address */
2758 x86_mov_reg_membase (code
, X86_ECX
, MONO_ARCH_GOT_REG
, sizeof (target_mgreg_t
), 4);
2760 x86_mov_reg_membase (code
, X86_EAX
, X86_ECX
, offset
* sizeof (target_mgreg_t
), 4);
2761 /* Branch to the target address */
2762 x86_jump_membase (code
, X86_ECX
, (offset
+ 1) * sizeof (target_mgreg_t
));
2764 emit_bytes (acfg
, buf
, code
- buf
);
2767 g_assert (code
- buf
== *tramp_size
);
2768 #elif defined(TARGET_ARM)
2772 /* The same as mono_arch_get_gsharedvt_arg_trampoline (), but for AOT */
2773 /* Similar to arch_emit_specific_trampoline () */
2776 ARM_PUSH (code
, (1 << ARMREG_R0
) | (1 << ARMREG_R1
) | (1 << ARMREG_R2
) | (1 << ARMREG_R3
));
2777 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_PC
, 8);
2778 /* Load the arg value from the GOT */
2779 ARM_LDR_REG_REG (code
, ARMREG_R0
, ARMREG_PC
, ARMREG_R1
);
2780 /* Load the addr from the GOT */
2781 ARM_LDR_REG_REG (code
, ARMREG_R1
, ARMREG_PC
, ARMREG_R1
);
2783 ARM_BX (code
, ARMREG_R1
);
2785 g_assert (code
- buf
== 20);
2788 emit_bytes (acfg
, buf
, code
- buf
);
2789 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) + 4);
2790 #elif defined(TARGET_ARM64)
2791 arm64_emit_gsharedvt_arg_trampoline (acfg
, offset
, tramp_size
);
2792 #elif defined (TARGET_AMD64)
2794 amd64_emit_load_got_slot (acfg
, AMD64_RAX
, offset
);
2795 amd64_emit_load_got_slot (acfg
, MONO_ARCH_IMT_SCRATCH_REG
, offset
+ 1);
2796 g_assert (AMD64_R11
== MONO_ARCH_IMT_SCRATCH_REG
);
2797 fprintf (acfg
->fp
, "jmp *%%r11\n");
2801 g_assert_not_reached ();
2806 arch_emit_ftnptr_arg_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2808 #if defined(TARGET_ARM)
2815 /* Load target address and push it on stack */
2816 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 16);
2817 ARM_LDR_REG_REG (code
, ARMREG_IP
, ARMREG_PC
, ARMREG_IP
);
2818 ARM_PUSH (code
, 1 << ARMREG_IP
);
2819 /* Load argument in ARMREG_IP */
2820 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 8);
2821 ARM_LDR_REG_REG (code
, ARMREG_IP
, ARMREG_PC
, ARMREG_IP
);
2823 ARM_POP (code
, 1 << ARMREG_PC
);
2825 g_assert (code
- buf
== 24);
2828 emit_bytes (acfg
, buf
, code
- buf
);
2829 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", ((offset
+ 1) * sizeof (target_mgreg_t
)) + 12); // offset from ldr pc to addr
2830 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) + 4); // offset from ldr pc to arg
2832 g_assert_not_reached ();
2837 arch_emit_unbox_arbitrary_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2839 #if defined(TARGET_ARM64)
2840 emit_unset_mode (acfg
);
2841 fprintf (acfg
->fp
, "add x0, x0, %d\n", (int)(MONO_ABI_SIZEOF (MonoObject
)));
2842 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
);
2843 fprintf (acfg
->fp
, "br x17\n");
2844 *tramp_size
= 5 * 4;
2845 #elif defined (TARGET_AMD64)
2850 this_reg
= mono_arch_get_this_arg_reg (NULL
);
2852 amd64_alu_reg_imm (code
, X86_ADD
, this_reg
, MONO_ABI_SIZEOF (MonoObject
));
2853 emit_bytes (acfg
, buf
, code
- buf
);
2855 amd64_emit_load_got_slot (acfg
, AMD64_RAX
, offset
);
2856 fprintf (acfg
->fp
, "jmp *%%rax\n");
2859 #elif defined (TARGET_ARM)
2861 guint8
*code
, *label
;
2865 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, MONO_ABI_SIZEOF (MonoObject
));
2868 /* Calculate GOT slot */
2869 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 0);
2870 /* Load target addr into PC*/
2871 ARM_LDR_REG_REG (code
, ARMREG_PC
, ARMREG_PC
, ARMREG_IP
);
2873 g_assert (code
- buf
== 12);
2876 emit_bytes (acfg
, buf
, code
- buf
);
2877 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (target_mgreg_t
)) + (code
- (label
+ 8)) - 4);
2878 *tramp_size
= 4 * 4;
2880 g_error ("NOT IMPLEMENTED: needed for AOT<>interp mixed mode transition");
2884 /* END OF ARCH SPECIFIC CODE */
2887 mono_get_field_token (MonoClassField
*field
)
2889 MonoClass
*klass
= field
->parent
;
2892 int fcount
= mono_class_get_field_count (klass
);
2893 MonoClassField
*klass_fields
= m_class_get_fields (klass
);
2894 for (i
= 0; i
< fcount
; ++i
) {
2895 if (field
== &klass_fields
[i
])
2896 return MONO_TOKEN_FIELD_DEF
| (mono_class_get_first_field_idx (klass
) + 1 + i
);
2899 g_assert_not_reached ();
2904 encode_value (gint32 value
, guint8
*buf
, guint8
**endbuf
)
2908 //printf ("ENCODE: %d 0x%x.\n", value, value);
2911 * Same encoding as the one used in the metadata, extended to handle values
2912 * greater than 0x1fffffff.
2914 if ((value
>= 0) && (value
<= 127))
2916 else if ((value
>= 0) && (value
<= 16383)) {
2917 p
[0] = 0x80 | (value
>> 8);
2918 p
[1] = value
& 0xff;
2920 } else if ((value
>= 0) && (value
<= 0x1fffffff)) {
2921 p
[0] = (value
>> 24) | 0xc0;
2922 p
[1] = (value
>> 16) & 0xff;
2923 p
[2] = (value
>> 8) & 0xff;
2924 p
[3] = value
& 0xff;
2929 p
[1] = (value
>> 24) & 0xff;
2930 p
[2] = (value
>> 16) & 0xff;
2931 p
[3] = (value
>> 8) & 0xff;
2932 p
[4] = value
& 0xff;
2940 stream_init (MonoDynamicStream
*sh
)
2943 sh
->alloc_size
= 4096;
2944 sh
->data
= (char *)g_malloc (4096);
2946 /* So offsets are > 0 */
2952 make_room_in_stream (MonoDynamicStream
*stream
, int size
)
2954 if (size
<= stream
->alloc_size
)
2957 while (stream
->alloc_size
<= size
) {
2958 if (stream
->alloc_size
< 4096)
2959 stream
->alloc_size
= 4096;
2961 stream
->alloc_size
*= 2;
2964 stream
->data
= (char *)g_realloc (stream
->data
, stream
->alloc_size
);
2968 add_stream_data (MonoDynamicStream
*stream
, const char *data
, guint32 len
)
2972 make_room_in_stream (stream
, stream
->index
+ len
);
2973 memcpy (stream
->data
+ stream
->index
, data
, len
);
2974 idx
= stream
->index
;
2975 stream
->index
+= len
;
2982 * Add data to the binary blob inside the aot image. Returns the offset inside the
2983 * blob where the data was stored.
2986 add_to_blob (MonoAotCompile
*acfg
, const guint8
*data
, guint32 data_len
)
2988 g_assert (!acfg
->blob_closed
);
2990 if (acfg
->blob
.alloc_size
== 0)
2991 stream_init (&acfg
->blob
);
2993 acfg
->stats
.blob_size
+= data_len
;
2995 return add_stream_data (&acfg
->blob
, (char*)data
, data_len
);
2999 add_to_blob_aligned (MonoAotCompile
*acfg
, const guint8
*data
, guint32 data_len
, guint32 align
)
3004 if (acfg
->blob
.alloc_size
== 0)
3005 stream_init (&acfg
->blob
);
3007 count
= acfg
->blob
.index
% align
;
3009 /* we assume the stream data will be aligned */
3011 add_stream_data (&acfg
->blob
, buf
, 4 - count
);
3013 return add_stream_data (&acfg
->blob
, (char*)data
, data_len
);
3016 /* Emit a table of data into the aot image */
3018 emit_aot_data (MonoAotCompile
*acfg
, MonoAotFileTable table
, const char *symbol
, guint8
*data
, int size
)
3020 if (acfg
->data_outfile
) {
3021 acfg
->table_offsets
[(int)table
] = acfg
->datafile_offset
;
3022 fwrite (data
,1, size
, acfg
->data_outfile
);
3023 acfg
->datafile_offset
+= size
;
3024 // align the data to 8 bytes. Put zeros in the file (so that every build results in consistent output).
3025 int align
= 8 - size
% 8;
3026 acfg
->datafile_offset
+= align
;
3027 guint8 align_buf
[16];
3028 memset (&align_buf
, 0, sizeof (align_buf
));
3029 fwrite (align_buf
, align
, 1, acfg
->data_outfile
);
3030 } else if (acfg
->llvm
) {
3031 mono_llvm_emit_aot_data (symbol
, data
, size
);
3033 emit_section_change (acfg
, RODATA_SECT
, 0);
3034 emit_alignment (acfg
, 8);
3035 emit_label (acfg
, symbol
);
3036 emit_bytes (acfg
, data
, size
);
3041 * emit_offset_table:
3043 * Emit a table of increasing offsets in a compact form using differential encoding.
3044 * There is an index entry for each GROUP_SIZE number of entries. The greater the
3045 * group size, the more compact the table becomes, but the slower it becomes to compute
3046 * a given entry. Returns the size of the table.
3049 emit_offset_table (MonoAotCompile
*acfg
, const char *symbol
, MonoAotFileTable table
, int noffsets
, int group_size
, gint32
*offsets
)
3051 gint32 current_offset
;
3052 int i
, buf_size
, ngroups
, index_entry_size
;
3054 guint8
*data_p
, *data_buf
;
3055 guint32
*index_offsets
;
3057 ngroups
= (noffsets
+ (group_size
- 1)) / group_size
;
3059 index_offsets
= g_new0 (guint32
, ngroups
);
3061 buf_size
= noffsets
* 4;
3062 p
= buf
= (guint8
*)g_malloc0 (buf_size
);
3065 for (i
= 0; i
< noffsets
; ++i
) {
3066 //printf ("D: %d -> %d\n", i, offsets [i]);
3067 if ((i
% group_size
) == 0) {
3068 index_offsets
[i
/ group_size
] = p
- buf
;
3069 /* Emit the full value for these entries */
3070 encode_value (offsets
[i
], p
, &p
);
3072 /* The offsets are allowed to be non-increasing */
3073 //g_assert (offsets [i] >= current_offset);
3074 encode_value (offsets
[i
] - current_offset
, p
, &p
);
3076 current_offset
= offsets
[i
];
3081 if (ngroups
&& index_offsets
[ngroups
- 1] < 65000)
3082 index_entry_size
= 2;
3084 index_entry_size
= 4;
3086 buf_size
= (data_p
- data_buf
) + (ngroups
* 4) + 16;
3087 p
= buf
= (guint8
*)g_malloc0 (buf_size
);
3089 /* Emit the header */
3090 encode_int (noffsets
, p
, &p
);
3091 encode_int (group_size
, p
, &p
);
3092 encode_int (ngroups
, p
, &p
);
3093 encode_int (index_entry_size
, p
, &p
);
3095 /* Emit the index */
3096 for (i
= 0; i
< ngroups
; ++i
) {
3097 if (index_entry_size
== 2)
3098 encode_int16 (index_offsets
[i
], p
, &p
);
3100 encode_int (index_offsets
[i
], p
, &p
);
3103 memcpy (p
, data_buf
, data_p
- data_buf
);
3104 p
+= data_p
- data_buf
;
3106 g_assert (p
- buf
<= buf_size
);
3108 emit_aot_data (acfg
, table
, symbol
, buf
, p
- buf
);
3113 return (int)(p
- buf
);
3117 get_image_index (MonoAotCompile
*cfg
, MonoImage
*image
)
3121 index
= GPOINTER_TO_UINT (g_hash_table_lookup (cfg
->image_hash
, image
));
3125 index
= g_hash_table_size (cfg
->image_hash
);
3126 g_hash_table_insert (cfg
->image_hash
, image
, GUINT_TO_POINTER (index
+ 1));
3127 g_ptr_array_add (cfg
->image_table
, image
);
3133 find_typespec_for_class (MonoAotCompile
*acfg
, MonoClass
*klass
)
3136 int len
= acfg
->image
->tables
[MONO_TABLE_TYPESPEC
].rows
;
3138 /* FIXME: Search referenced images as well */
3139 if (!acfg
->typespec_classes
) {
3140 acfg
->typespec_classes
= g_hash_table_new (NULL
, NULL
);
3141 for (i
= 0; i
< len
; i
++) {
3143 int typespec
= MONO_TOKEN_TYPE_SPEC
| (i
+ 1);
3144 MonoClass
*klass_key
= mono_class_get_and_inflate_typespec_checked (acfg
->image
, typespec
, NULL
, error
);
3145 if (!is_ok (error
)) {
3146 mono_error_cleanup (error
);
3149 g_hash_table_insert (acfg
->typespec_classes
, klass_key
, GINT_TO_POINTER (typespec
));
3152 return GPOINTER_TO_INT (g_hash_table_lookup (acfg
->typespec_classes
, klass
));
3156 encode_method_ref (MonoAotCompile
*acfg
, MonoMethod
*method
, guint8
*buf
, guint8
**endbuf
);
3159 encode_klass_ref (MonoAotCompile
*acfg
, MonoClass
*klass
, guint8
*buf
, guint8
**endbuf
);
3162 encode_ginst (MonoAotCompile
*acfg
, MonoGenericInst
*inst
, guint8
*buf
, guint8
**endbuf
);
3165 encode_type (MonoAotCompile
*acfg
, MonoType
*t
, guint8
*buf
, guint8
**endbuf
);
3168 get_shared_ginst_ref (MonoAotCompile
*acfg
, MonoGenericInst
*ginst
);
3171 encode_klass_ref_inner (MonoAotCompile
*acfg
, MonoClass
*klass
, guint8
*buf
, guint8
**endbuf
)
3176 * The encoding begins with one of the MONO_AOT_TYPEREF values, followed by additional
3180 if (mono_class_is_ginst (klass
)) {
3182 g_assert (m_class_get_type_token (klass
));
3184 /* Find a typespec for a class if possible */
3185 token
= find_typespec_for_class (acfg
, klass
);
3187 encode_value (MONO_AOT_TYPEREF_TYPESPEC_TOKEN
, p
, &p
);
3188 encode_value (token
, p
, &p
);
3190 MonoClass
*gclass
= mono_class_get_generic_class (klass
)->container_class
;
3191 MonoGenericInst
*inst
= mono_class_get_generic_class (klass
)->context
.class_inst
;
3192 static int count
= 0;
3195 encode_value (MONO_AOT_TYPEREF_GINST
, p
, &p
);
3196 encode_klass_ref (acfg
, gclass
, p
, &p
);
3197 guint32 offset
= get_shared_ginst_ref (acfg
, inst
);
3198 encode_value (offset
, p
, &p
);
3202 } else if (m_class_get_type_token (klass
)) {
3203 int iindex
= get_image_index (acfg
, m_class_get_image (klass
));
3205 g_assert (mono_metadata_token_code (m_class_get_type_token (klass
)) == MONO_TOKEN_TYPE_DEF
);
3207 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX
, p
, &p
);
3208 encode_value (m_class_get_type_token (klass
) - MONO_TOKEN_TYPE_DEF
, p
, &p
);
3210 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE
, p
, &p
);
3211 encode_value (m_class_get_type_token (klass
) - MONO_TOKEN_TYPE_DEF
, p
, &p
);
3212 encode_value (get_image_index (acfg
, m_class_get_image (klass
)), p
, &p
);
3214 } else if ((m_class_get_byval_arg (klass
)->type
== MONO_TYPE_VAR
) || (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_MVAR
)) {
3215 MonoGenericContainer
*container
= mono_type_get_generic_param_owner (m_class_get_byval_arg (klass
));
3216 MonoGenericParam
*par
= m_class_get_byval_arg (klass
)->data
.generic_param
;
3218 encode_value (MONO_AOT_TYPEREF_VAR
, p
, &p
);
3220 encode_value (par
->gshared_constraint
? 1 : 0, p
, &p
);
3221 if (par
->gshared_constraint
) {
3222 MonoGSharedGenericParam
*gpar
= (MonoGSharedGenericParam
*)par
;
3223 encode_type (acfg
, par
->gshared_constraint
, p
, &p
);
3224 encode_klass_ref (acfg
, mono_class_create_generic_parameter (gpar
->parent
), p
, &p
);
3226 encode_value (m_class_get_byval_arg (klass
)->type
, p
, &p
);
3227 encode_value (mono_type_get_generic_param_num (m_class_get_byval_arg (klass
)), p
, &p
);
3229 encode_value (container
->is_anonymous
? 0 : 1, p
, &p
);
3231 if (!container
->is_anonymous
) {
3232 encode_value (container
->is_method
, p
, &p
);
3233 if (container
->is_method
)
3234 encode_method_ref (acfg
, container
->owner
.method
, p
, &p
);
3236 encode_klass_ref (acfg
, container
->owner
.klass
, p
, &p
);
3239 } else if (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_PTR
) {
3240 encode_value (MONO_AOT_TYPEREF_PTR
, p
, &p
);
3241 encode_type (acfg
, m_class_get_byval_arg (klass
), p
, &p
);
3244 g_assert (m_class_get_rank (klass
) > 0);
3245 encode_value (MONO_AOT_TYPEREF_ARRAY
, p
, &p
);
3246 encode_value (m_class_get_rank (klass
), p
, &p
);
3247 encode_klass_ref (acfg
, m_class_get_element_class (klass
), p
, &p
);
3250 acfg
->stats
.class_ref_count
++;
3251 acfg
->stats
.class_ref_size
+= p
- buf
;
3257 get_shared_klass_ref (MonoAotCompile
*acfg
, MonoClass
*klass
)
3259 guint offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->klass_blob_hash
, klass
));
3263 buf2
= (guint8
*)g_malloc (1024);
3266 encode_klass_ref_inner (acfg
, klass
, p
, &p
);
3267 g_assert (p
- buf2
< 1024);
3269 offset
= add_to_blob (acfg
, buf2
, p
- buf2
);
3272 g_hash_table_insert (acfg
->klass_blob_hash
, klass
, GUINT_TO_POINTER (offset
+ 1));
3283 * Encode a reference to KLASS. We use our home-grown encoding instead of the
3284 * standard metadata encoding.
3287 encode_klass_ref (MonoAotCompile
*acfg
, MonoClass
*klass
, guint8
*buf
, guint8
**endbuf
)
3289 gboolean shared
= FALSE
;
3292 * The encoding of generic instances is large so emit them only once.
3294 if (mono_class_is_ginst (klass
)) {
3296 g_assert (m_class_get_type_token (klass
));
3298 /* Find a typespec for a class if possible */
3299 token
= find_typespec_for_class (acfg
, klass
);
3302 } else if ((m_class_get_byval_arg (klass
)->type
== MONO_TYPE_VAR
) || (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_MVAR
)) {
3308 guint32 offset
= get_shared_klass_ref (acfg
, klass
);
3311 encode_value (MONO_AOT_TYPEREF_BLOB_INDEX
, p
, &p
);
3312 encode_value (offset
, p
, &p
);
3317 encode_klass_ref_inner (acfg
, klass
, buf
, endbuf
);
3321 encode_field_info (MonoAotCompile
*cfg
, MonoClassField
*field
, guint8
*buf
, guint8
**endbuf
)
3323 guint32 token
= mono_get_field_token (field
);
3326 encode_klass_ref (cfg
, field
->parent
, p
, &p
);
3327 g_assert (mono_metadata_token_code (token
) == MONO_TOKEN_FIELD_DEF
);
3328 encode_value (token
- MONO_TOKEN_FIELD_DEF
, p
, &p
);
3333 encode_ginst (MonoAotCompile
*acfg
, MonoGenericInst
*inst
, guint8
*buf
, guint8
**endbuf
)
3338 encode_value (inst
->type_argc
, p
, &p
);
3339 for (i
= 0; i
< inst
->type_argc
; ++i
)
3340 encode_klass_ref (acfg
, mono_class_from_mono_type_internal (inst
->type_argv
[i
]), p
, &p
);
3342 acfg
->stats
.ginst_count
++;
3343 acfg
->stats
.ginst_size
+= p
- buf
;
3349 get_shared_ginst_ref (MonoAotCompile
*acfg
, MonoGenericInst
*ginst
)
3351 guint32 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->ginst_blob_hash
, ginst
));
3355 buf2
= (guint8
*)g_malloc (1024);
3358 encode_ginst (acfg
, ginst
, p2
, &p2
);
3359 g_assert (p2
- buf2
< 1024);
3361 offset
= add_to_blob (acfg
, buf2
, p2
- buf2
);
3364 g_hash_table_insert (acfg
->ginst_blob_hash
, ginst
, GUINT_TO_POINTER (offset
+ 1));
3372 encode_generic_context (MonoAotCompile
*acfg
, MonoGenericContext
*context
, guint8
*buf
, guint8
**endbuf
)
3375 MonoGenericInst
*inst
;
3376 guint32 flags
= (context
->class_inst
? 1 : 0) | (context
->method_inst
? 2 : 0);
3380 encode_value (flags
, p
, &p
);
3381 inst
= context
->class_inst
;
3383 guint32 offset
= get_shared_ginst_ref (acfg
, inst
);
3384 encode_value (offset
, p
, &p
);
3386 inst
= context
->method_inst
;
3388 guint32 offset
= get_shared_ginst_ref (acfg
, inst
);
3389 encode_value (offset
, p
, &p
);
3395 encode_type (MonoAotCompile
*acfg
, MonoType
*t
, guint8
*buf
, guint8
**endbuf
)
3400 int count
= mono_type_custom_modifier_count (t
);
3402 *p
= MONO_TYPE_CMOD_REQD
;
3405 encode_value (count
, p
, &p
);
3406 for (int i
= 0; i
< count
; ++i
) {
3409 MonoType
*cmod_type
= mono_type_get_custom_modifier (t
, i
, &required
, error
);
3410 mono_error_assert_ok (error
);
3411 encode_value (required
, p
, &p
);
3412 encode_type (acfg
, cmod_type
, p
, &p
);
3416 /* t->attrs can be ignored */
3417 //g_assert (t->attrs == 0);
3420 *p
= MONO_TYPE_PINNED
;
3424 *p
= MONO_TYPE_BYREF
;
3432 case MONO_TYPE_VOID
:
3433 case MONO_TYPE_BOOLEAN
:
3434 case MONO_TYPE_CHAR
:
3447 case MONO_TYPE_STRING
:
3448 case MONO_TYPE_OBJECT
:
3449 case MONO_TYPE_TYPEDBYREF
:
3451 case MONO_TYPE_VALUETYPE
:
3452 case MONO_TYPE_CLASS
:
3453 encode_klass_ref (acfg
, mono_class_from_mono_type_internal (t
), p
, &p
);
3455 case MONO_TYPE_SZARRAY
:
3456 encode_klass_ref (acfg
, t
->data
.klass
, p
, &p
);
3459 encode_type (acfg
, t
->data
.type
, p
, &p
);
3461 case MONO_TYPE_GENERICINST
: {
3462 MonoClass
*gclass
= t
->data
.generic_class
->container_class
;
3463 MonoGenericInst
*inst
= t
->data
.generic_class
->context
.class_inst
;
3465 encode_klass_ref (acfg
, gclass
, p
, &p
);
3466 encode_ginst (acfg
, inst
, p
, &p
);
3469 case MONO_TYPE_ARRAY
: {
3470 MonoArrayType
*array
= t
->data
.array
;
3473 encode_klass_ref (acfg
, array
->eklass
, p
, &p
);
3474 encode_value (array
->rank
, p
, &p
);
3475 encode_value (array
->numsizes
, p
, &p
);
3476 for (i
= 0; i
< array
->numsizes
; ++i
)
3477 encode_value (array
->sizes
[i
], p
, &p
);
3478 encode_value (array
->numlobounds
, p
, &p
);
3479 for (i
= 0; i
< array
->numlobounds
; ++i
)
3480 encode_value (array
->lobounds
[i
], p
, &p
);
3484 case MONO_TYPE_MVAR
:
3485 encode_klass_ref (acfg
, mono_class_from_mono_type_internal (t
), p
, &p
);
3488 g_assert_not_reached ();
3495 encode_signature (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
, guint8
*buf
, guint8
**endbuf
)
3501 /* Similar to the metadata encoding */
3502 if (sig
->generic_param_count
)
3506 if (sig
->explicit_this
)
3508 flags
|= (sig
->call_convention
& 0x0F);
3512 if (sig
->generic_param_count
)
3513 encode_value (sig
->generic_param_count
, p
, &p
);
3514 encode_value (sig
->param_count
, p
, &p
);
3516 encode_type (acfg
, sig
->ret
, p
, &p
);
3517 for (i
= 0; i
< sig
->param_count
; ++i
) {
3518 if (sig
->sentinelpos
== i
) {
3519 *p
= MONO_TYPE_SENTINEL
;
3522 encode_type (acfg
, sig
->params
[i
], p
, &p
);
3528 #define MAX_IMAGE_INDEX 250
3531 encode_method_ref (MonoAotCompile
*acfg
, MonoMethod
*method
, guint8
*buf
, guint8
**endbuf
)
3533 guint32 image_index
= get_image_index (acfg
, m_class_get_image (method
->klass
));
3534 guint32 token
= method
->token
;
3535 MonoJumpInfoToken
*ji
;
3539 * The encoding for most methods is as follows:
3540 * - image index encoded as a leb128
3541 * - token index encoded as a leb128
3542 * Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
3543 * types of method encodings.
3546 /* Mark methods which can't use aot trampolines because they need the further
3547 * processing in mono_magic_trampoline () which requires a MonoMethod*.
3549 if ((method
->is_generic
&& (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)) ||
3550 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
))
3551 encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE
<< 24), p
, &p
);
3553 if (method
->wrapper_type
) {
3554 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
3556 encode_value ((MONO_AOT_METHODREF_WRAPPER
<< 24), p
, &p
);
3558 encode_value (method
->wrapper_type
, p
, &p
);
3560 switch (method
->wrapper_type
) {
3561 case MONO_WRAPPER_REMOTING_INVOKE
:
3562 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
3563 case MONO_WRAPPER_XDOMAIN_INVOKE
: {
3566 m
= mono_marshal_method_from_wrapper (method
);
3568 encode_method_ref (acfg
, m
, p
, &p
);
3571 case MONO_WRAPPER_PROXY_ISINST
:
3572 case MONO_WRAPPER_LDFLD
:
3573 case MONO_WRAPPER_LDFLDA
:
3574 case MONO_WRAPPER_STFLD
: {
3576 encode_klass_ref (acfg
, info
->d
.proxy
.klass
, p
, &p
);
3579 case MONO_WRAPPER_ALLOC
: {
3580 /* The GC name is saved once in MonoAotFileInfo */
3581 g_assert (info
->d
.alloc
.alloc_type
!= -1);
3582 encode_value (info
->d
.alloc
.alloc_type
, p
, &p
);
3585 case MONO_WRAPPER_WRITE_BARRIER
: {
3589 case MONO_WRAPPER_STELEMREF
: {
3591 encode_value (info
->subtype
, p
, &p
);
3592 if (info
->subtype
== WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
)
3593 encode_value (info
->d
.virtual_stelemref
.kind
, p
, &p
);
3596 case MONO_WRAPPER_OTHER
: {
3598 encode_value (info
->subtype
, p
, &p
);
3599 if (info
->subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
||
3600 info
->subtype
== WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
)
3601 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3602 else if (info
->subtype
== WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
)
3603 encode_method_ref (acfg
, info
->d
.synchronized_inner
.method
, p
, &p
);
3604 else if (info
->subtype
== WRAPPER_SUBTYPE_ARRAY_ACCESSOR
)
3605 encode_method_ref (acfg
, info
->d
.array_accessor
.method
, p
, &p
);
3606 else if (info
->subtype
== WRAPPER_SUBTYPE_INTERP_IN
)
3607 encode_signature (acfg
, info
->d
.interp_in
.sig
, p
, &p
);
3608 else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
)
3609 encode_signature (acfg
, info
->d
.gsharedvt
.sig
, p
, &p
);
3610 else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
)
3611 encode_signature (acfg
, info
->d
.gsharedvt
.sig
, p
, &p
);
3612 else if (info
->subtype
== WRAPPER_SUBTYPE_INTERP_LMF
)
3613 encode_value (info
->d
.icall
.jit_icall_id
, p
, &p
);
3614 else if (info
->subtype
== WRAPPER_SUBTYPE_AOT_INIT
)
3615 encode_value (info
->d
.aot_init
.subtype
, p
, &p
);
3616 else if (info
->subtype
== WRAPPER_SUBTYPE_LLVM_FUNC
)
3617 encode_value (info
->d
.llvm_func
.subtype
, p
, &p
);
3620 case MONO_WRAPPER_MANAGED_TO_NATIVE
: {
3622 encode_value (info
->subtype
, p
, &p
);
3623 if (info
->subtype
== WRAPPER_SUBTYPE_ICALL_WRAPPER
) {
3624 encode_value (info
->d
.icall
.jit_icall_id
, p
, &p
);
3625 } else if (info
->subtype
== WRAPPER_SUBTYPE_NATIVE_FUNC_AOT
) {
3626 encode_method_ref (acfg
, info
->d
.managed_to_native
.method
, p
, &p
);
3628 g_assert (info
->subtype
== WRAPPER_SUBTYPE_NONE
|| info
->subtype
== WRAPPER_SUBTYPE_PINVOKE
);
3629 encode_method_ref (acfg
, info
->d
.managed_to_native
.method
, p
, &p
);
3633 case MONO_WRAPPER_SYNCHRONIZED
: {
3636 m
= mono_marshal_method_from_wrapper (method
);
3638 g_assert (m
!= method
);
3639 encode_method_ref (acfg
, m
, p
, &p
);
3642 case MONO_WRAPPER_MANAGED_TO_MANAGED
: {
3644 encode_value (info
->subtype
, p
, &p
);
3646 if (info
->subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
) {
3647 encode_value (info
->d
.element_addr
.rank
, p
, &p
);
3648 encode_value (info
->d
.element_addr
.elem_size
, p
, &p
);
3649 } else if (info
->subtype
== WRAPPER_SUBTYPE_STRING_CTOR
) {
3650 encode_method_ref (acfg
, info
->d
.string_ctor
.method
, p
, &p
);
3652 g_assert_not_reached ();
3656 case MONO_WRAPPER_CASTCLASS
: {
3658 encode_value (info
->subtype
, p
, &p
);
3661 case MONO_WRAPPER_RUNTIME_INVOKE
: {
3663 encode_value (info
->subtype
, p
, &p
);
3664 if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
|| info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
)
3665 encode_method_ref (acfg
, info
->d
.runtime_invoke
.method
, p
, &p
);
3666 else if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL
)
3667 encode_signature (acfg
, info
->d
.runtime_invoke
.sig
, p
, &p
);
3670 case MONO_WRAPPER_DELEGATE_INVOKE
:
3671 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
3672 case MONO_WRAPPER_DELEGATE_END_INVOKE
: {
3673 if (method
->is_inflated
) {
3674 /* These wrappers are identified by their class */
3675 encode_value (1, p
, &p
);
3676 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3678 MonoMethodSignature
*sig
= mono_method_signature_internal (method
);
3679 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
3681 encode_value (0, p
, &p
);
3682 if (method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
3683 encode_value (info
? info
->subtype
: 0, p
, &p
);
3684 encode_signature (acfg
, sig
, p
, &p
);
3688 case MONO_WRAPPER_NATIVE_TO_MANAGED
: {
3690 encode_method_ref (acfg
, info
->d
.native_to_managed
.method
, p
, &p
);
3691 encode_klass_ref (acfg
, info
->d
.native_to_managed
.klass
, p
, &p
);
3695 g_assert_not_reached ();
3697 } else if (mono_method_signature_internal (method
)->is_inflated
) {
3699 * This is a generic method, find the original token which referenced it and
3701 * Obtain the token from information recorded by the JIT.
3703 ji
= (MonoJumpInfoToken
*)g_hash_table_lookup (acfg
->token_info_hash
, method
);
3705 image_index
= get_image_index (acfg
, ji
->image
);
3706 g_assert (image_index
< MAX_IMAGE_INDEX
);
3709 encode_value ((MONO_AOT_METHODREF_METHODSPEC
<< 24), p
, &p
);
3710 encode_value (image_index
, p
, &p
);
3711 encode_value (token
, p
, &p
);
3712 } else if (g_hash_table_lookup (acfg
->method_blob_hash
, method
)) {
3713 /* Already emitted as part of an rgctx fetch */
3714 guint32 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_blob_hash
, method
));
3717 encode_value ((MONO_AOT_METHODREF_BLOB_INDEX
<< 24), p
, &p
);
3718 encode_value (offset
, p
, &p
);
3720 MonoMethod
*declaring
;
3721 MonoGenericContext
*context
= mono_method_get_context (method
);
3723 g_assert (method
->is_inflated
);
3724 declaring
= ((MonoMethodInflated
*)method
)->declaring
;
3727 * This might be a non-generic method of a generic instance, which
3728 * doesn't have a token since the reference is generated by the JIT
3729 * like Nullable:Box/Unbox, or by generic sharing.
3731 encode_value ((MONO_AOT_METHODREF_GINST
<< 24), p
, &p
);
3732 /* Encode the klass */
3733 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3734 /* Encode the method */
3735 image_index
= get_image_index (acfg
, m_class_get_image (method
->klass
));
3736 g_assert (image_index
< MAX_IMAGE_INDEX
);
3737 g_assert (declaring
->token
);
3738 token
= declaring
->token
;
3739 g_assert (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
);
3740 encode_value (image_index
, p
, &p
);
3741 encode_value (mono_metadata_token_index (token
), p
, &p
);
3742 encode_generic_context (acfg
, context
, p
, &p
);
3744 } else if (token
== 0) {
3745 /* This might be a method of a constructed type like int[,].Set */
3746 /* Obtain the token from information recorded by the JIT */
3747 ji
= (MonoJumpInfoToken
*)g_hash_table_lookup (acfg
->token_info_hash
, method
);
3749 image_index
= get_image_index (acfg
, ji
->image
);
3750 g_assert (image_index
< MAX_IMAGE_INDEX
);
3753 encode_value ((MONO_AOT_METHODREF_METHODSPEC
<< 24), p
, &p
);
3754 encode_value (image_index
, p
, &p
);
3755 encode_value (token
, p
, &p
);
3758 g_assert (m_class_get_rank (method
->klass
));
3760 /* Encode directly */
3761 encode_value ((MONO_AOT_METHODREF_ARRAY
<< 24), p
, &p
);
3762 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3763 if (!strcmp (method
->name
, ".ctor") && mono_method_signature_internal (method
)->param_count
== m_class_get_rank (method
->klass
))
3764 encode_value (0, p
, &p
);
3765 else if (!strcmp (method
->name
, ".ctor") && mono_method_signature_internal (method
)->param_count
== m_class_get_rank (method
->klass
) * 2)
3766 encode_value (1, p
, &p
);
3767 else if (!strcmp (method
->name
, "Get"))
3768 encode_value (2, p
, &p
);
3769 else if (!strcmp (method
->name
, "Address"))
3770 encode_value (3, p
, &p
);
3771 else if (!strcmp (method
->name
, "Set"))
3772 encode_value (4, p
, &p
);
3774 g_assert_not_reached ();
3777 g_assert (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
);
3779 if (image_index
>= MONO_AOT_METHODREF_MIN
) {
3780 encode_value ((MONO_AOT_METHODREF_LARGE_IMAGE_INDEX
<< 24), p
, &p
);
3781 encode_value (image_index
, p
, &p
);
3782 encode_value (mono_metadata_token_index (token
), p
, &p
);
3784 encode_value ((image_index
<< 24) | mono_metadata_token_index (token
), p
, &p
);
3788 acfg
->stats
.method_ref_count
++;
3789 acfg
->stats
.method_ref_size
+= p
- buf
;
3795 get_shared_method_ref (MonoAotCompile
*acfg
, MonoMethod
*method
)
3797 guint32 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_blob_hash
, method
));
3801 buf2
= (guint8
*)g_malloc (1024);
3804 encode_method_ref (acfg
, method
, p2
, &p2
);
3805 g_assert (p2
- buf2
< 1024);
3807 offset
= add_to_blob (acfg
, buf2
, p2
- buf2
);
3810 g_hash_table_insert (acfg
->method_blob_hash
, method
, GUINT_TO_POINTER (offset
+ 1));
3818 compare_patches (gconstpointer a
, gconstpointer b
)
3822 i
= (*(MonoJumpInfo
**)a
)->ip
.i
;
3823 j
= (*(MonoJumpInfo
**)b
)->ip
.i
;
3834 static G_GNUC_UNUSED
char*
3835 patch_to_string (MonoJumpInfo
*patch_info
)
3839 str
= g_string_new ("");
3841 g_string_append_printf (str
, "%s(", get_patch_name (patch_info
->type
));
3843 switch (patch_info
->type
) {
3844 case MONO_PATCH_INFO_VTABLE
:
3845 mono_type_get_desc (str
, m_class_get_byval_arg (patch_info
->data
.klass
), TRUE
);
3850 g_string_append_printf (str
, ")");
3851 return g_string_free (str
, FALSE
);
3857 * Return whenever PATCH_INFO refers to a direct call, and thus requires a
3860 static inline gboolean
3861 is_plt_patch (MonoJumpInfo
*patch_info
)
3863 switch (patch_info
->type
) {
3864 case MONO_PATCH_INFO_METHOD
:
3865 case MONO_PATCH_INFO_JIT_ICALL_ID
:
3866 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
3867 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
3868 case MONO_PATCH_INFO_RGCTX_FETCH
:
3869 case MONO_PATCH_INFO_SPECIFIC_TRAMPOLINE_LAZY_FETCH_ADDR
:
3871 case MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
:
3880 * Return the symbol identifying the plt entry PLT_OFFSET.
3883 get_plt_symbol (MonoAotCompile
*acfg
, int plt_offset
, MonoJumpInfo
*patch_info
)
3887 * The Apple linker reorganizes object files, so it doesn't like branches to local
3888 * labels, since those have no relocations.
3890 return g_strdup_printf ("%sp_%d", acfg
->llvm_label_prefix
, plt_offset
);
3892 return g_strdup_printf ("%sp_%d", acfg
->temp_prefix
, plt_offset
);
3899 * Return a PLT entry which belongs to the method identified by PATCH_INFO.
3901 static MonoPltEntry
*
3902 get_plt_entry (MonoAotCompile
*acfg
, MonoJumpInfo
*patch_info
)
3905 gboolean synchronized
= FALSE
;
3906 static int synchronized_symbol_idx
;
3908 if (!is_plt_patch (patch_info
))
3911 if (!acfg
->patch_to_plt_entry
[patch_info
->type
])
3912 acfg
->patch_to_plt_entry
[patch_info
->type
] = g_hash_table_new (mono_patch_info_hash
, mono_patch_info_equal
);
3913 res
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->patch_to_plt_entry
[patch_info
->type
], patch_info
);
3915 if (!acfg
->llvm
&& patch_info
->type
== MONO_PATCH_INFO_METHOD
&& (patch_info
->data
.method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
)) {
3917 * Allocate a separate PLT slot for each such patch, since some plt
3918 * entries will refer to the method itself, and some will refer to the
3922 synchronized
= TRUE
;
3926 MonoJumpInfo
*new_ji
;
3928 new_ji
= mono_patch_info_dup_mp (acfg
->mempool
, patch_info
);
3930 res
= (MonoPltEntry
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoPltEntry
));
3931 res
->plt_offset
= acfg
->plt_offset
;
3933 res
->symbol
= get_plt_symbol (acfg
, res
->plt_offset
, patch_info
);
3934 if (acfg
->aot_opts
.write_symbols
)
3935 res
->debug_sym
= get_plt_entry_debug_sym (acfg
, res
->ji
, acfg
->plt_entry_debug_sym_cache
);
3937 /* Avoid duplicate symbols because we don't cache */
3938 res
->symbol
= g_strdup_printf ("%s_%d", res
->symbol
, synchronized_symbol_idx
);
3940 res
->debug_sym
= g_strdup_printf ("%s_%d", res
->debug_sym
, synchronized_symbol_idx
);
3941 synchronized_symbol_idx
++;
3945 res
->llvm_symbol
= g_strdup_printf ("%s_%s_llvm", res
->symbol
, res
->debug_sym
);
3947 res
->llvm_symbol
= g_strdup_printf ("%s_llvm", res
->symbol
);
3948 if (strstr (res
->llvm_symbol
, acfg
->temp_prefix
) == res
->llvm_symbol
) {
3949 /* The llvm symbol shouldn't be temporary, since the llvm generated object file references it */
3950 char *tmp
= res
->llvm_symbol
;
3951 res
->llvm_symbol
= g_strdup (res
->llvm_symbol
+ strlen (acfg
->temp_prefix
));
3955 g_hash_table_insert (acfg
->patch_to_plt_entry
[new_ji
->type
], new_ji
, res
);
3957 g_hash_table_insert (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (res
->plt_offset
), res
);
3959 //g_assert (mono_patch_info_equal (patch_info, new_ji));
3960 //mono_print_ji (patch_info); printf ("\n");
3961 //g_hash_table_print_stats (acfg->patch_to_plt_entry);
3963 acfg
->plt_offset
++;
3970 lookup_got_offset (MonoAotCompile
*acfg
, gboolean llvm
, MonoJumpInfo
*ji
)
3973 GotInfo
*info
= llvm
? &acfg
->llvm_got_info
: &acfg
->got_info
;
3975 got_offset
= GPOINTER_TO_UINT (g_hash_table_lookup (info
->patch_to_got_offset_by_type
[ji
->type
], ji
));
3977 return got_offset
- 1;
3978 g_assert_not_reached ();
3984 * Returns the offset of the GOT slot where the runtime object resulting from resolving
3985 * JI could be found if it exists, otherwise allocates a new one.
3988 get_got_offset (MonoAotCompile
*acfg
, gboolean llvm
, MonoJumpInfo
*ji
)
3991 GotInfo
*info
= llvm
? &acfg
->llvm_got_info
: &acfg
->got_info
;
3993 got_offset
= GPOINTER_TO_UINT (g_hash_table_lookup (info
->patch_to_got_offset_by_type
[ji
->type
], ji
));
3995 return got_offset
- 1;
3998 got_offset
= acfg
->llvm_got_offset
;
3999 acfg
->llvm_got_offset
++;
4001 got_offset
= acfg
->got_offset
;
4002 acfg
->got_offset
++;
4005 acfg
->stats
.got_slots
++;
4006 acfg
->stats
.got_slot_types
[ji
->type
] ++;
4008 g_hash_table_insert (info
->patch_to_got_offset
, ji
, GUINT_TO_POINTER (got_offset
+ 1));
4009 g_hash_table_insert (info
->patch_to_got_offset_by_type
[ji
->type
], ji
, GUINT_TO_POINTER (got_offset
+ 1));
4010 g_ptr_array_add (info
->got_patches
, ji
);
4015 /* Add a method to the list of methods which need to be emitted */
4017 add_method_with_index (MonoAotCompile
*acfg
, MonoMethod
*method
, int index
, gboolean extra
)
4020 if (!g_hash_table_lookup (acfg
->method_indexes
, method
)) {
4021 g_ptr_array_add (acfg
->methods
, method
);
4022 g_hash_table_insert (acfg
->method_indexes
, method
, GUINT_TO_POINTER (index
+ 1));
4023 acfg
->nmethods
= acfg
->methods
->len
+ 1;
4026 if (method
->wrapper_type
|| extra
) {
4027 int token
= mono_metadata_token_index (method
->token
) - 1;
4029 acfg
->nextra_methods
++;
4030 g_ptr_array_add (acfg
->extra_methods
, method
);
4035 prefer_gsharedvt_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
4037 /* One instantiation with valuetypes is generated for each async method */
4038 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")))
4045 get_method_index (MonoAotCompile
*acfg
, MonoMethod
*method
)
4047 int index
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_indexes
, method
));
4055 add_method_full (MonoAotCompile
*acfg
, MonoMethod
*method
, gboolean extra
, int depth
)
4059 index
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_indexes
, method
));
4063 index
= acfg
->method_index
;
4064 add_method_with_index (acfg
, method
, index
, extra
);
4066 g_ptr_array_add (acfg
->method_order
, GUINT_TO_POINTER (index
));
4068 g_hash_table_insert (acfg
->method_depth
, method
, GUINT_TO_POINTER (depth
));
4070 acfg
->method_index
++;
4076 add_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
4078 return add_method_full (acfg
, method
, FALSE
, 0);
4082 mono_dedup_cache_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
4084 g_assert (acfg
->dedup_stats
);
4086 char *name
= mono_aot_get_mangled_method_name (method
);
4090 char *stats_name
= g_strdup (name
);
4092 g_assert (acfg
->dedup_cache
);
4094 if (!g_hash_table_lookup (acfg
->dedup_cache
, name
)) {
4095 // This AOTCompile owns this method
4096 // We do this to decide whether to write it to disk
4097 // during a dedup run (first phase, where we skip).
4099 // If never changed, then maybe can avoid a recompile
4102 // Files not read in during last phase.
4103 acfg
->dedup_cache_changed
= TRUE
;
4106 g_hash_table_insert (acfg
->dedup_cache
, name
, method
);
4112 guint count
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->dedup_stats
, stats_name
));
4114 g_hash_table_insert (acfg
->dedup_stats
, stats_name
, GUINT_TO_POINTER (count
));
4118 add_extra_method_with_depth (MonoAotCompile
*acfg
, MonoMethod
*method
, int depth
)
4122 if (mono_method_is_generic_sharable_full (method
, TRUE
, TRUE
, FALSE
)) {
4123 method
= mini_get_shared_method_full (method
, SHARE_MODE_NONE
, error
);
4124 if (!is_ok (error
)) {
4125 /* vtype constraint */
4126 mono_error_cleanup (error
);
4129 } else if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && prefer_gsharedvt_method (acfg
, method
) && mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, TRUE
)) {
4130 /* Use the gsharedvt version */
4131 method
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
4132 mono_error_assert_ok (error
);
4135 if ((acfg
->aot_opts
.dedup
|| acfg
->aot_opts
.dedup_include
) && mono_aot_can_dedup (method
)) {
4136 mono_dedup_cache_method (acfg
, method
);
4138 if (!acfg
->dedup_emit_mode
)
4142 if (acfg
->aot_opts
.log_generics
)
4143 aot_printf (acfg
, "%*sAdding method %s.\n", depth
, "", mono_method_get_full_name (method
));
4145 add_method_full (acfg
, method
, TRUE
, depth
);
4149 add_extra_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
4151 add_extra_method_with_depth (acfg
, method
, 0);
4155 add_jit_icall_wrapper (MonoAotCompile
*acfg
, MonoJitICallInfo
*callinfo
)
4160 g_assert (callinfo
->name
&& callinfo
->func
);
4162 add_method (acfg
, mono_marshal_get_icall_wrapper (callinfo
, TRUE
));
4168 add_lazy_init_wrappers (MonoAotCompile
*acfg
)
4170 add_method (acfg
, mono_marshal_get_aot_init_wrapper (AOT_INIT_METHOD
));
4171 add_method (acfg
, mono_marshal_get_aot_init_wrapper (AOT_INIT_METHOD_GSHARED_MRGCTX
));
4172 add_method (acfg
, mono_marshal_get_aot_init_wrapper (AOT_INIT_METHOD_GSHARED_VTABLE
));
4173 add_method (acfg
, mono_marshal_get_aot_init_wrapper (AOT_INIT_METHOD_GSHARED_THIS
));
4179 get_runtime_invoke_sig (MonoMethodSignature
*sig
)
4181 MonoMethodBuilder
*mb
;
4184 mb
= mono_mb_new (mono_defaults
.object_class
, "FOO", MONO_WRAPPER_NONE
);
4185 m
= mono_mb_create_method (mb
, sig
, 16);
4186 MonoMethod
*invoke
= mono_marshal_get_runtime_invoke (m
, FALSE
);
4192 get_runtime_invoke (MonoAotCompile
*acfg
, MonoMethod
*method
, gboolean virtual_
)
4194 return mono_marshal_get_runtime_invoke (method
, virtual_
);
4198 can_marshal_struct (MonoClass
*klass
)
4200 MonoClassField
*field
;
4201 gboolean can_marshal
= TRUE
;
4202 gpointer iter
= NULL
;
4203 MonoMarshalType
*info
;
4206 if (mono_class_is_auto_layout (klass
))
4209 info
= mono_marshal_load_type_info (klass
);
4211 /* Only allow a few field types to avoid asserts in the marshalling code */
4212 while ((field
= mono_class_get_fields_internal (klass
, &iter
))) {
4213 if ((field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
4216 switch (field
->type
->type
) {
4221 case MONO_TYPE_BOOLEAN
:
4224 case MONO_TYPE_CHAR
:
4232 case MONO_TYPE_STRING
:
4234 case MONO_TYPE_VALUETYPE
:
4235 if (!m_class_is_enumtype (mono_class_from_mono_type_internal (field
->type
)) && !can_marshal_struct (mono_class_from_mono_type_internal (field
->type
)))
4236 can_marshal
= FALSE
;
4238 case MONO_TYPE_SZARRAY
: {
4239 gboolean has_mspec
= FALSE
;
4242 for (i
= 0; i
< info
->num_fields
; ++i
) {
4243 if (info
->fields
[i
].field
== field
&& info
->fields
[i
].mspec
)
4248 can_marshal
= FALSE
;
4252 can_marshal
= FALSE
;
4258 /* Its hard to compute whenever these can be marshalled or not */
4259 if (!strcmp (m_class_get_name_space (klass
), "System.Net.NetworkInformation.MacOsStructs") && strcmp (m_class_get_name (klass
), "sockaddr_dl"))
4266 create_gsharedvt_inst (MonoAotCompile
*acfg
, MonoMethod
*method
, MonoGenericContext
*ctx
)
4268 /* Create a vtype instantiation */
4269 MonoGenericContext shared_context
;
4271 MonoGenericInst
*inst
;
4272 MonoGenericContainer
*container
;
4273 MonoClass
**constraints
;
4276 memset (ctx
, 0, sizeof (MonoGenericContext
));
4278 if (mono_class_is_gtd (method
->klass
)) {
4279 shared_context
= mono_class_get_generic_container (method
->klass
)->context
;
4280 inst
= shared_context
.class_inst
;
4282 args
= g_new0 (MonoType
*, inst
->type_argc
);
4283 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4284 args
[i
] = mono_get_int_type ();
4286 ctx
->class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, args
);
4288 if (method
->is_generic
) {
4289 container
= mono_method_get_generic_container (method
);
4290 g_assert (!container
->is_anonymous
&& container
->is_method
);
4291 shared_context
= container
->context
;
4292 inst
= shared_context
.method_inst
;
4294 args
= g_new0 (MonoType
*, inst
->type_argc
);
4295 for (i
= 0; i
< container
->type_argc
; ++i
) {
4296 MonoGenericParamInfo
*info
= mono_generic_param_info (&container
->type_params
[i
]);
4297 gboolean ref_only
= FALSE
;
4299 if (info
&& info
->constraints
) {
4300 constraints
= info
->constraints
;
4302 while (*constraints
) {
4303 MonoClass
*cklass
= *constraints
;
4304 if (!(cklass
== mono_defaults
.object_class
|| (m_class_get_image (cklass
) == mono_defaults
.corlib
&& !strcmp (m_class_get_name (cklass
), "ValueType"))))
4305 /* Inflaring the method with our vtype would not be valid */
4312 args
[i
] = mono_get_object_type ();
4314 args
[i
] = mono_get_int_type ();
4316 ctx
->method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, args
);
4321 add_gc_wrappers (MonoAotCompile
*acfg
)
4324 /* Managed Allocators */
4325 int nallocators
= mono_gc_get_managed_allocator_types ();
4326 for (int i
= 0; i
< nallocators
; ++i
) {
4327 if ((m
= mono_gc_get_managed_allocator_by_type (i
, MANAGED_ALLOCATOR_REGULAR
)))
4328 add_method (acfg
, m
);
4329 if ((m
= mono_gc_get_managed_allocator_by_type (i
, MANAGED_ALLOCATOR_SLOW_PATH
)))
4330 add_method (acfg
, m
);
4331 if ((m
= mono_gc_get_managed_allocator_by_type (i
, MANAGED_ALLOCATOR_PROFILER
)))
4332 add_method (acfg
, m
);
4335 /* write barriers */
4336 if (mono_gc_is_moving ()) {
4337 add_method (acfg
, mono_gc_get_specific_write_barrier (FALSE
));
4338 add_method (acfg
, mono_gc_get_specific_write_barrier (TRUE
));
4343 contains_disable_reflection_attribute (MonoCustomAttrInfo
*cattr
)
4345 for (int i
= 0; i
< cattr
->num_attrs
; ++i
) {
4346 MonoCustomAttrEntry
*attr
= &cattr
->attrs
[i
];
4351 if (strcmp (m_class_get_name_space (attr
->ctor
->klass
), "System.Runtime.CompilerServices"))
4354 if (strcmp (m_class_get_name (attr
->ctor
->klass
), "DisablePrivateReflectionAttribute"))
4362 mono_aot_can_specialize (MonoMethod
*method
)
4367 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
)
4370 // If it's not private, we can't specialize
4371 if ((method
->flags
& METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK
) != METHOD_ATTRIBUTE_PRIVATE
)
4374 // If it has the attribute disabling the specialization, we can't specialize
4376 // Set by linker, indicates that the method can be found through reflection
4377 // and that call-site specialization shouldn't be done.
4379 // Important that this attribute is used for *nothing else*
4381 // If future authors make use of it (to disable more optimizations),
4382 // change this place to use a new attribute.
4383 ERROR_DECL (cattr_error
);
4384 MonoCustomAttrInfo
*cattr
= mono_custom_attrs_from_class_checked (method
->klass
, cattr_error
);
4386 if (!is_ok (cattr_error
)) {
4387 mono_error_cleanup (cattr_error
);
4389 } else if (cattr
&& contains_disable_reflection_attribute (cattr
)) {
4393 cattr
= mono_custom_attrs_from_method_checked (method
, cattr_error
);
4395 if (!is_ok (cattr_error
)) {
4396 mono_error_cleanup (cattr_error
);
4398 } else if (cattr
&& contains_disable_reflection_attribute (cattr
)) {
4406 mono_custom_attrs_free (cattr
);
4411 mono_custom_attrs_free (cattr
);
4416 add_wrappers (MonoAotCompile
*acfg
)
4418 MonoMethod
*method
, *m
;
4420 MonoMethodSignature
*sig
, *csig
;
4424 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
4425 * so there is only one wrapper of a given type, or inlining their contents into their
4428 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4431 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4432 gboolean skip
= FALSE
;
4434 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4435 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
4437 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
4438 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
4439 (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
))
4442 /* Skip methods which can not be handled by get_runtime_invoke () */
4443 sig
= mono_method_signature_internal (method
);
4446 if ((sig
->ret
->type
== MONO_TYPE_PTR
) ||
4447 (sig
->ret
->type
== MONO_TYPE_TYPEDBYREF
))
4449 if (mono_class_is_open_constructed_type (sig
->ret
))
4452 for (j
= 0; j
< sig
->param_count
; j
++) {
4453 if (sig
->params
[j
]->type
== MONO_TYPE_TYPEDBYREF
)
4455 if (mono_class_is_open_constructed_type (sig
->params
[j
]))
4459 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
4460 if (!mono_class_is_contextbound (method
->klass
)) {
4461 MonoDynCallInfo
*info
= mono_arch_dyn_call_prepare (sig
);
4462 gboolean has_nullable
= FALSE
;
4464 for (j
= 0; j
< sig
->param_count
; j
++) {
4465 if (sig
->params
[j
]->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type_internal (sig
->params
[j
])))
4466 has_nullable
= TRUE
;
4469 if (info
&& !has_nullable
&& !acfg
->aot_opts
.llvm_only
) {
4470 /* Supported by the dynamic runtime-invoke wrapper */
4474 mono_arch_dyn_call_free (info
);
4478 if (acfg
->aot_opts
.llvm_only
)
4479 /* Supported by the gsharedvt based runtime-invoke wrapper */
4483 //printf ("%s\n", mono_method_full_name (method, TRUE));
4484 add_method (acfg
, get_runtime_invoke (acfg
, method
, FALSE
));
4488 if (mono_is_corlib_image (acfg
->image
->assembly
->image
)) {
4489 /* Runtime invoke wrappers */
4491 MonoType
*void_type
= mono_get_void_type ();
4492 MonoType
*string_type
= m_class_get_byval_arg (mono_defaults
.string_class
);
4494 /* void runtime-invoke () [.cctor] */
4495 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4496 csig
->ret
= void_type
;
4497 add_method (acfg
, get_runtime_invoke_sig (csig
));
4499 /* void runtime-invoke () [Finalize] */
4500 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4502 csig
->ret
= void_type
;
4503 add_method (acfg
, get_runtime_invoke_sig (csig
));
4505 /* void runtime-invoke (string) [exception ctor] */
4506 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
4508 csig
->ret
= void_type
;
4509 csig
->params
[0] = string_type
;
4510 add_method (acfg
, get_runtime_invoke_sig (csig
));
4512 /* void runtime-invoke (string, string) [exception ctor] */
4513 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 2);
4515 csig
->ret
= void_type
;
4516 csig
->params
[0] = string_type
;
4517 csig
->params
[1] = string_type
;
4518 add_method (acfg
, get_runtime_invoke_sig (csig
));
4520 /* string runtime-invoke () [Exception.ToString ()] */
4521 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
4523 csig
->ret
= string_type
;
4524 add_method (acfg
, get_runtime_invoke_sig (csig
));
4526 /* void runtime-invoke (string, Exception) [exception ctor] */
4527 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 2);
4529 csig
->ret
= void_type
;
4530 csig
->params
[0] = string_type
;
4531 csig
->params
[1] = m_class_get_byval_arg (mono_defaults
.exception_class
);
4532 add_method (acfg
, get_runtime_invoke_sig (csig
));
4534 /* Assembly runtime-invoke (string, Assembly, bool) [DoAssemblyResolve] */
4535 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
4537 csig
->ret
= m_class_get_byval_arg (mono_class_load_from_name (mono_defaults
.corlib
, "System.Reflection", "Assembly"));
4538 csig
->params
[0] = string_type
;
4539 csig
->params
[1] = m_class_get_byval_arg (mono_class_load_from_name (mono_defaults
.corlib
, "System.Reflection", "Assembly"));
4540 csig
->params
[2] = m_class_get_byval_arg (mono_defaults
.boolean_class
);
4541 add_method (acfg
, get_runtime_invoke_sig (csig
));
4543 /* runtime-invoke used by finalizers */
4544 add_method (acfg
, get_runtime_invoke (acfg
, get_method_nofail (mono_defaults
.object_class
, "Finalize", 0, 0), TRUE
));
4546 /* This is used by mono_runtime_capture_context () */
4547 method
= mono_get_context_capture_method ();
4549 add_method (acfg
, get_runtime_invoke (acfg
, method
, FALSE
));
4551 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
4552 if (!acfg
->aot_opts
.llvm_only
)
4553 add_method (acfg
, mono_marshal_get_runtime_invoke_dynamic ());
4556 /* These are used by mono_jit_runtime_invoke () to calls gsharedvt out wrappers */
4557 if (acfg
->aot_opts
.llvm_only
) {
4560 /* Create simplified signatures which match the signature used by the gsharedvt out wrappers */
4561 for (variants
= 0; variants
< 4; ++variants
) {
4562 for (i
= 0; i
< 40; ++i
) {
4563 sig
= mini_get_gsharedvt_out_sig_wrapper_signature ((variants
& 1) > 0, (variants
& 2) > 0, i
);
4564 add_extra_method (acfg
, mono_marshal_get_runtime_invoke_for_sig (sig
));
4572 add_method (acfg
, mono_marshal_get_stelemref ());
4574 add_gc_wrappers (acfg
);
4576 /* Stelemref wrappers */
4578 MonoMethod
**wrappers
;
4581 wrappers
= mono_marshal_get_virtual_stelemref_wrappers (&nwrappers
);
4582 for (i
= 0; i
< nwrappers
; ++i
)
4583 add_method (acfg
, wrappers
[i
]);
4587 /* castclass_with_check wrapper */
4588 add_method (acfg
, mono_marshal_get_castclass_with_cache ());
4589 /* isinst_with_check wrapper */
4590 add_method (acfg
, mono_marshal_get_isinst_with_cache ());
4592 /* JIT icall wrappers */
4593 /* FIXME: locking - this is "safe" as full-AOT threads don't mutate the icall data */
4594 for (int i
= 0; i
< MONO_JIT_ICALL_count
; ++i
)
4595 add_jit_icall_wrapper (acfg
, mono_find_jit_icall_info ((MonoJitICallId
)i
));
4599 * remoting-invoke-with-check wrappers are very frequent, so avoid emitting them,
4600 * we use the original method instead at runtime.
4601 * Since full-aot doesn't support remoting, this is not a problem.
4604 /* remoting-invoke wrappers */
4605 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4607 MonoMethodSignature
*sig
;
4609 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4610 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4611 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4613 sig
= mono_method_signature_internal (method
);
4615 if (sig
->hasthis
&& (method
->klass
->marshalbyref
|| method
->klass
== mono_defaults
.object_class
)) {
4616 m
= mono_marshal_get_remoting_invoke_with_check (method
);
4618 add_method (acfg
, m
);
4623 /* delegate-invoke wrappers */
4624 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
) {
4628 token
= MONO_TOKEN_TYPE_DEF
| (i
+ 1);
4629 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
4632 mono_error_cleanup (error
);
4636 if (!m_class_is_delegate (klass
) || klass
== mono_defaults
.delegate_class
|| klass
== mono_defaults
.multicastdelegate_class
)
4639 if (!mono_class_is_gtd (klass
)) {
4640 method
= mono_get_delegate_invoke_internal (klass
);
4642 m
= mono_marshal_get_delegate_invoke (method
, NULL
);
4644 add_method (acfg
, m
);
4646 method
= try_get_method_nofail (klass
, "BeginInvoke", -1, 0);
4648 add_method (acfg
, mono_marshal_get_delegate_begin_invoke (method
));
4650 method
= try_get_method_nofail (klass
, "EndInvoke", -1, 0);
4652 add_method (acfg
, mono_marshal_get_delegate_end_invoke (method
));
4654 MonoCustomAttrInfo
*cattr
;
4655 cattr
= mono_custom_attrs_from_class_checked (klass
, error
);
4656 if (!is_ok (error
)) {
4657 mono_error_cleanup (error
);
4665 for (j
= 0; j
< cattr
->num_attrs
; ++j
)
4666 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")))
4668 if (j
< cattr
->num_attrs
) {
4670 MonoMethod
*wrapper
;
4671 MonoMethod
*del_invoke
;
4673 /* Add wrappers needed by mono_ftnptr_to_delegate () */
4674 invoke
= mono_get_delegate_invoke_internal (klass
);
4675 wrapper
= mono_marshal_get_native_func_wrapper_aot (klass
);
4676 del_invoke
= mono_marshal_get_delegate_invoke_internal (invoke
, FALSE
, TRUE
, wrapper
);
4677 add_method (acfg
, wrapper
);
4678 add_method (acfg
, del_invoke
);
4680 mono_custom_attrs_free (cattr
);
4682 } else if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && mono_class_is_gtd (klass
)) {
4684 MonoGenericContext ctx
;
4685 MonoMethod
*inst
, *gshared
;
4688 * Emit gsharedvt versions of the generic delegate-invoke wrappers
4691 method
= mono_get_delegate_invoke_internal (klass
);
4692 create_gsharedvt_inst (acfg
, method
, &ctx
);
4694 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, error
);
4695 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4697 m
= mono_marshal_get_delegate_invoke (inst
, NULL
);
4698 g_assert (m
->is_inflated
);
4700 gshared
= mini_get_shared_method_full (m
, SHARE_MODE_GSHAREDVT
, error
);
4701 mono_error_assert_ok (error
);
4703 add_extra_method (acfg
, gshared
);
4706 method
= mono_get_delegate_begin_invoke_internal (klass
);
4708 create_gsharedvt_inst (acfg
, method
, &ctx
);
4710 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, error
);
4711 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4713 m
= mono_marshal_get_delegate_begin_invoke (inst
);
4714 g_assert (m
->is_inflated
);
4716 gshared
= mini_get_shared_method_full (m
, SHARE_MODE_GSHAREDVT
, error
);
4717 mono_error_assert_ok (error
);
4719 add_extra_method (acfg
, gshared
);
4723 method
= mono_get_delegate_end_invoke_internal (klass
);
4725 create_gsharedvt_inst (acfg
, method
, &ctx
);
4727 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, error
);
4728 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4730 m
= mono_marshal_get_delegate_end_invoke (inst
);
4731 g_assert (m
->is_inflated
);
4733 gshared
= mini_get_shared_method_full (m
, SHARE_MODE_GSHAREDVT
, error
);
4734 mono_error_assert_ok (error
);
4736 add_extra_method (acfg
, gshared
);
4741 /* array access wrappers */
4742 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPESPEC
].rows
; ++i
) {
4746 token
= MONO_TOKEN_TYPE_SPEC
| (i
+ 1);
4747 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
4750 mono_error_cleanup (error
);
4754 if (m_class_get_rank (klass
) && MONO_TYPE_IS_PRIMITIVE (m_class_get_byval_arg (m_class_get_element_class (klass
)))) {
4755 MonoMethod
*m
, *wrapper
;
4757 /* Add runtime-invoke wrappers too */
4759 m
= get_method_nofail (klass
, "Get", -1, 0);
4761 wrapper
= mono_marshal_get_array_accessor_wrapper (m
);
4762 add_extra_method (acfg
, wrapper
);
4763 if (!acfg
->aot_opts
.llvm_only
)
4764 add_extra_method (acfg
, get_runtime_invoke (acfg
, wrapper
, FALSE
));
4766 m
= get_method_nofail (klass
, "Set", -1, 0);
4768 wrapper
= mono_marshal_get_array_accessor_wrapper (m
);
4769 add_extra_method (acfg
, wrapper
);
4770 if (!acfg
->aot_opts
.llvm_only
)
4771 add_extra_method (acfg
, get_runtime_invoke (acfg
, wrapper
, FALSE
));
4775 /* Synchronized wrappers */
4776 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4778 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4779 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4780 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
4782 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
) {
4783 if (method
->is_generic
) {
4785 } else if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && mono_class_is_gtd (method
->klass
)) {
4787 MonoGenericContext ctx
;
4788 MonoMethod
*inst
, *gshared
, *m
;
4791 * Create a generic wrapper for a generic instance, and AOT that.
4793 create_gsharedvt_inst (acfg
, method
, &ctx
);
4794 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, error
);
4795 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
4796 m
= mono_marshal_get_synchronized_wrapper (inst
);
4797 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_method (acfg
, gshared
);
4803 add_method (acfg
, mono_marshal_get_synchronized_wrapper (method
));
4808 /* pinvoke wrappers */
4809 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4812 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4814 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4815 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
4817 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
4818 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)) {
4819 add_method (acfg
, mono_marshal_get_native_wrapper (method
, TRUE
, TRUE
));
4822 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
4823 if (acfg
->aot_opts
.llvm_only
) {
4824 /* The wrappers have a different signature (hasthis is not set) so need to add this too */
4825 add_gsharedvt_wrappers (acfg
, mono_method_signature_internal (method
), FALSE
, TRUE
, FALSE
);
4830 /* native-to-managed wrappers */
4831 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4834 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4835 MonoCustomAttrInfo
*cattr
;
4838 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
4839 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
4842 * Only generate native-to-managed wrappers for methods which have an
4843 * attribute named MonoPInvokeCallbackAttribute. We search for the attribute by
4844 * name to avoid defining a new assembly to contain it.
4846 cattr
= mono_custom_attrs_from_method_checked (method
, error
);
4847 if (!is_ok (error
)) {
4848 char *name
= mono_method_get_full_name (method
);
4849 report_loader_error (acfg
, error
, TRUE
, "Failed to load custom attributes from method %s due to %s\n", name
, mono_error_get_message (error
));
4854 for (j
= 0; j
< cattr
->num_attrs
; ++j
)
4855 if (cattr
->attrs
[j
].ctor
&& !strcmp (m_class_get_name (cattr
->attrs
[j
].ctor
->klass
), "MonoPInvokeCallbackAttribute"))
4857 if (j
< cattr
->num_attrs
) {
4858 MonoCustomAttrEntry
*e
= &cattr
->attrs
[j
];
4859 MonoMethodSignature
*sig
= mono_method_signature_internal (e
->ctor
);
4860 const char *p
= (const char*)e
->data
;
4862 int slen
, num_named
, named_type
;
4866 char *export_name
= NULL
;
4867 MonoMethod
*wrapper
;
4869 /* this cannot be enforced by the C# compiler so we must give the user some warning before aborting */
4870 if (!(method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
4871 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",
4872 mono_method_full_name (method
, TRUE
));
4876 g_assert (sig
->param_count
== 1);
4877 g_assert (sig
->params
[0]->type
== MONO_TYPE_CLASS
&& !strcmp (m_class_get_name (mono_class_from_mono_type_internal (sig
->params
[0])), "Type"));
4880 * Decode the cattr manually since we can't create objects
4881 * during aot compilation.
4887 /* From load_cattr_value () in reflection.c */
4888 slen
= mono_metadata_decode_value (p
, &p
);
4889 n
= (char *)g_memdup (p
, slen
+ 1);
4891 t
= mono_reflection_type_from_name_checked (n
, acfg
->image
, error
);
4893 mono_error_assert_ok (error
);
4896 klass
= mono_class_from_mono_type_internal (t
);
4897 g_assert (m_class_get_parent (klass
) == mono_defaults
.multicastdelegate_class
);
4901 num_named
= read16 (p
);
4904 g_assert (num_named
< 2);
4905 if (num_named
== 1) {
4909 /* parse ExportSymbol attribute */
4911 named_type
= *named
;
4913 /* data_type = *named; */
4916 name_len
= mono_metadata_decode_blob_size (named
, &named
);
4917 name
= (char *)g_malloc (name_len
+ 1);
4918 memcpy (name
, named
, name_len
);
4919 name
[name_len
] = 0;
4922 g_assert (named_type
== 0x54);
4923 g_assert (!strcmp (name
, "ExportSymbol"));
4925 /* load_cattr_value (), string case */
4926 MONO_DISABLE_WARNING (4310) // cast truncates constant value
4927 g_assert (*named
!= (char)0xFF);
4928 MONO_RESTORE_WARNING
4929 slen
= mono_metadata_decode_value (named
, &named
);
4930 export_name
= (char *)g_malloc (slen
+ 1);
4931 memcpy (export_name
, named
, slen
);
4932 export_name
[slen
] = 0;
4936 wrapper
= mono_marshal_get_managed_wrapper (method
, klass
, 0, error
);
4937 mono_error_assert_ok (error
);
4939 add_method (acfg
, wrapper
);
4941 g_hash_table_insert (acfg
->export_names
, wrapper
, export_name
);
4946 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
4947 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)) {
4948 add_method (acfg
, mono_marshal_get_native_wrapper (method
, TRUE
, TRUE
));
4952 /* StructureToPtr/PtrToStructure wrappers */
4953 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
) {
4957 token
= MONO_TOKEN_TYPE_DEF
| (i
+ 1);
4958 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
4961 mono_error_cleanup (error
);
4965 if (m_class_is_valuetype (klass
) && !mono_class_is_gtd (klass
) && can_marshal_struct (klass
) &&
4966 !(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
)))) {
4967 add_method (acfg
, mono_marshal_get_struct_to_ptr (klass
));
4968 add_method (acfg
, mono_marshal_get_ptr_to_struct (klass
));
4974 has_type_vars (MonoClass
*klass
)
4976 if ((m_class_get_byval_arg (klass
)->type
== MONO_TYPE_VAR
) || (m_class_get_byval_arg (klass
)->type
== MONO_TYPE_MVAR
))
4978 if (m_class_get_rank (klass
))
4979 return has_type_vars (m_class_get_element_class (klass
));
4980 if (mono_class_is_ginst (klass
)) {
4981 MonoGenericContext
*context
= &mono_class_get_generic_class (klass
)->context
;
4982 if (context
->class_inst
) {
4985 for (i
= 0; i
< context
->class_inst
->type_argc
; ++i
)
4986 if (has_type_vars (mono_class_from_mono_type_internal (context
->class_inst
->type_argv
[i
])))
4990 if (mono_class_is_gtd (klass
))
4996 is_vt_inst (MonoGenericInst
*inst
)
5000 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5001 MonoType
*t
= inst
->type_argv
[i
];
5002 if (MONO_TYPE_ISSTRUCT (t
) || t
->type
== MONO_TYPE_VALUETYPE
)
5009 method_has_type_vars (MonoMethod
*method
)
5011 if (has_type_vars (method
->klass
))
5014 if (method
->is_inflated
) {
5015 MonoGenericContext
*context
= mono_method_get_context (method
);
5016 if (context
->method_inst
) {
5019 for (i
= 0; i
< context
->method_inst
->type_argc
; ++i
)
5020 if (has_type_vars (mono_class_from_mono_type_internal (context
->method_inst
->type_argv
[i
])))
5028 gboolean
mono_aot_mode_is_full (MonoAotOptions
*opts
)
5030 return opts
->mode
== MONO_AOT_MODE_FULL
;
5034 gboolean
mono_aot_mode_is_interp (MonoAotOptions
*opts
)
5036 return opts
->interp
;
5040 gboolean
mono_aot_mode_is_hybrid (MonoAotOptions
*opts
)
5042 return opts
->mode
== MONO_AOT_MODE_HYBRID
;
5045 static void add_generic_class_with_depth (MonoAotCompile
*acfg
, MonoClass
*klass
, int depth
, const char *ref
);
5048 add_generic_class (MonoAotCompile
*acfg
, MonoClass
*klass
, gboolean force
, const char *ref
)
5050 /* This might lead to a huge code blowup so only do it if neccesary */
5051 if (!mono_aot_mode_is_full (&acfg
->aot_opts
) && !mono_aot_mode_is_hybrid (&acfg
->aot_opts
) && !force
)
5054 add_generic_class_with_depth (acfg
, klass
, 0, ref
);
5058 check_type_depth (MonoType
*t
, int depth
)
5066 case MONO_TYPE_GENERICINST
: {
5067 MonoGenericClass
*gklass
= t
->data
.generic_class
;
5068 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
5071 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
5072 if (check_type_depth (ginst
->type_argv
[i
], depth
+ 1))
5086 add_types_from_method_header (MonoAotCompile
*acfg
, MonoMethod
*method
);
5089 * add_generic_class:
5091 * Add all methods of a generic class.
5094 add_generic_class_with_depth (MonoAotCompile
*acfg
, MonoClass
*klass
, int depth
, const char *ref
)
5097 MonoClassField
*field
;
5099 gboolean use_gsharedvt
= FALSE
;
5101 if (!acfg
->ginst_hash
)
5102 acfg
->ginst_hash
= g_hash_table_new (NULL
, NULL
);
5104 mono_class_init_internal (klass
);
5106 if (mono_class_is_ginst (klass
) && mono_class_get_generic_class (klass
)->context
.class_inst
->is_open
)
5109 if (has_type_vars (klass
))
5112 if (!mono_class_is_ginst (klass
) && !m_class_get_rank (klass
))
5115 if (mono_class_has_failure (klass
))
5118 if (!acfg
->ginst_hash
)
5119 acfg
->ginst_hash
= g_hash_table_new (NULL
, NULL
);
5121 if (g_hash_table_lookup (acfg
->ginst_hash
, klass
))
5124 if (check_type_depth (m_class_get_byval_arg (klass
), 0))
5127 if (acfg
->aot_opts
.log_generics
) {
5128 char *s
= mono_type_full_name (m_class_get_byval_arg (klass
));
5129 aot_printf (acfg
, "%*sAdding generic instance %s [%s].\n", depth
, "", s
, ref
);
5133 g_hash_table_insert (acfg
->ginst_hash
, klass
, klass
);
5136 * Use gsharedvt for generic collections with vtype arguments to avoid code blowup.
5137 * Enable this only for some classes since gsharedvt might not support all methods.
5139 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
) &&
5140 (!strcmp (m_class_get_name (klass
), "Dictionary`2") || !strcmp (m_class_get_name (klass
), "List`1") || !strcmp (m_class_get_name (klass
), "ReadOnlyCollection`1")))
5141 use_gsharedvt
= TRUE
;
5144 while ((method
= mono_class_get_methods (klass
, &iter
))) {
5145 if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && method
->is_inflated
&& mono_method_get_context (method
)->method_inst
) {
5147 * This is partial sharing, and we can't handle it yet
5152 if (mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, use_gsharedvt
)) {
5154 add_types_from_method_header (acfg
, method
);
5158 if (method
->is_generic
)
5163 * FIXME: Instances which are referenced by these methods are not added,
5164 * for example Array.Resize<int> for List<int>.Add ().
5166 add_extra_method_with_depth (acfg
, method
, depth
+ 1);
5170 while ((field
= mono_class_get_fields_internal (klass
, &iter
))) {
5171 if (field
->type
->type
== MONO_TYPE_GENERICINST
)
5172 add_generic_class_with_depth (acfg
, mono_class_from_mono_type_internal (field
->type
), depth
+ 1, "field");
5175 if (m_class_is_delegate (klass
)) {
5176 method
= mono_get_delegate_invoke_internal (klass
);
5178 method
= mono_marshal_get_delegate_invoke (method
, NULL
);
5180 if (acfg
->aot_opts
.log_generics
)
5181 aot_printf (acfg
, "%*sAdding method %s.\n", depth
, "", mono_method_get_full_name (method
));
5183 add_method (acfg
, method
);
5186 /* Add superclasses */
5187 if (m_class_get_parent (klass
))
5188 add_generic_class_with_depth (acfg
, m_class_get_parent (klass
), depth
, "parent");
5190 const char *klass_name
= m_class_get_name (klass
);
5191 const char *klass_name_space
= m_class_get_name_space (klass
);
5192 const gboolean in_corlib
= m_class_get_image (klass
) == mono_defaults
.corlib
;
5194 * For ICollection<T>, add instances of the helper methods
5195 * in Array, since a T[] could be cast to ICollection<T>.
5197 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") &&
5198 (!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"))) {
5199 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5200 MonoClass
*array_class
= mono_class_create_bounded_array (tclass
, 1, FALSE
);
5204 if (!strcmp (klass_name
, "IEnumerator`1"))
5205 name_prefix
= g_strdup_printf ("%s.%s", klass_name_space
, "IEnumerable`1");
5207 name_prefix
= g_strdup_printf ("%s.%s", klass_name_space
, klass_name
);
5209 /* Add the T[]/InternalEnumerator class */
5210 if (!strcmp (klass_name
, "IEnumerable`1") || !strcmp (klass_name
, "IEnumerator`1")) {
5215 while ((nclass
= mono_class_get_nested_types (m_class_get_parent (array_class
), &iter
))) {
5216 if (!strcmp (m_class_get_name (nclass
), "InternalEnumerator`1"))
5220 nclass
= mono_class_inflate_generic_class_checked (nclass
, mono_generic_class_get_context (mono_class_get_generic_class (klass
)), error
);
5221 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5222 add_generic_class (acfg
, nclass
, FALSE
, "ICollection<T>");
5226 while ((method
= mono_class_get_methods (array_class
, &iter
))) {
5227 if (!strncmp (method
->name
, name_prefix
, strlen (name_prefix
))) {
5228 MonoMethod
*m
= mono_aot_get_array_helper_from_wrapper (method
);
5230 if (m
->is_inflated
&& !mono_method_is_generic_sharable_full (m
, FALSE
, FALSE
, FALSE
))
5231 add_extra_method_with_depth (acfg
, m
, depth
);
5235 g_free (name_prefix
);
5238 /* Add an instance of GenericComparer<T> which is created dynamically by Comparer<T> */
5239 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") && !strcmp (klass_name
, "Comparer`1")) {
5241 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5242 MonoClass
*icomparable
, *gcomparer
, *icomparable_inst
;
5243 MonoGenericContext ctx
;
5244 MonoType
*args
[16];
5246 memset (&ctx
, 0, sizeof (ctx
));
5248 icomparable
= mono_class_load_from_name (mono_defaults
.corlib
, "System", "IComparable`1");
5250 args
[0] = m_class_get_byval_arg (tclass
);
5251 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5253 icomparable_inst
= mono_class_inflate_generic_class_checked (icomparable
, &ctx
, error
);
5254 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5256 if (mono_class_is_assignable_from_internal (icomparable_inst
, tclass
)) {
5257 MonoClass
*gcomparer_inst
;
5258 gcomparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "GenericComparer`1");
5259 gcomparer_inst
= mono_class_inflate_generic_class_checked (gcomparer
, &ctx
, error
);
5260 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5262 add_generic_class (acfg
, gcomparer_inst
, FALSE
, "Comparer<T>");
5266 /* Add an instance of GenericEqualityComparer<T> which is created dynamically by EqualityComparer<T> */
5267 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") && !strcmp (klass_name
, "EqualityComparer`1")) {
5269 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5270 MonoClass
*iface
, *gcomparer
, *iface_inst
;
5271 MonoGenericContext ctx
;
5272 MonoType
*args
[16];
5274 memset (&ctx
, 0, sizeof (ctx
));
5276 iface
= mono_class_load_from_name (mono_defaults
.corlib
, "System", "IEquatable`1");
5278 args
[0] = m_class_get_byval_arg (tclass
);
5279 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5281 iface_inst
= mono_class_inflate_generic_class_checked (iface
, &ctx
, error
);
5282 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5284 if (mono_class_is_assignable_from_internal (iface_inst
, tclass
)) {
5285 MonoClass
*gcomparer_inst
;
5288 gcomparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "GenericEqualityComparer`1");
5289 gcomparer_inst
= mono_class_inflate_generic_class_checked (gcomparer
, &ctx
, error
);
5290 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5291 add_generic_class (acfg
, gcomparer_inst
, FALSE
, "EqualityComparer<T>");
5295 /* Add an instance of EnumComparer<T> which is created dynamically by EqualityComparer<T> for enums */
5296 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") && !strcmp (klass_name
, "EqualityComparer`1")) {
5297 MonoClass
*enum_comparer
;
5298 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5299 MonoGenericContext ctx
;
5300 MonoType
*args
[16];
5302 if (m_class_is_enumtype (tclass
)) {
5303 MonoClass
*enum_comparer_inst
;
5306 memset (&ctx
, 0, sizeof (ctx
));
5307 args
[0] = m_class_get_byval_arg (tclass
);
5308 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5310 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "EnumEqualityComparer`1");
5311 enum_comparer_inst
= mono_class_inflate_generic_class_checked (enum_comparer
, &ctx
, error
);
5312 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5313 add_generic_class (acfg
, enum_comparer_inst
, FALSE
, "EqualityComparer<T>");
5317 /* Add an instance of ObjectComparer<T> which is created dynamically by Comparer<T> for enums */
5318 if (in_corlib
&& !strcmp (klass_name_space
, "System.Collections.Generic") && !strcmp (klass_name
, "Comparer`1")) {
5319 MonoClass
*comparer
;
5320 MonoClass
*tclass
= mono_class_from_mono_type_internal (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
5321 MonoGenericContext ctx
;
5322 MonoType
*args
[16];
5324 if (m_class_is_enumtype (tclass
)) {
5325 MonoClass
*comparer_inst
;
5328 memset (&ctx
, 0, sizeof (ctx
));
5329 args
[0] = m_class_get_byval_arg (tclass
);
5330 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5332 comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "ObjectComparer`1");
5333 comparer_inst
= mono_class_inflate_generic_class_checked (comparer
, &ctx
, error
);
5334 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5335 add_generic_class (acfg
, comparer_inst
, FALSE
, "Comparer<T>");
5341 add_instances_of (MonoAotCompile
*acfg
, MonoClass
*klass
, MonoType
**insts
, int ninsts
, gboolean force
)
5344 MonoGenericContext ctx
;
5345 MonoType
*args
[16];
5347 if (acfg
->aot_opts
.no_instances
)
5350 memset (&ctx
, 0, sizeof (ctx
));
5352 for (i
= 0; i
< ninsts
; ++i
) {
5354 MonoClass
*generic_inst
;
5355 args
[0] = insts
[i
];
5356 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
5357 generic_inst
= mono_class_inflate_generic_class_checked (klass
, &ctx
, error
);
5358 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5359 add_generic_class (acfg
, generic_inst
, force
, "");
5364 add_types_from_method_header (MonoAotCompile
*acfg
, MonoMethod
*method
)
5367 MonoMethodHeader
*header
;
5368 MonoMethodSignature
*sig
;
5371 depth
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_depth
, method
));
5373 sig
= mono_method_signature_internal (method
);
5376 for (j
= 0; j
< sig
->param_count
; ++j
)
5377 if (sig
->params
[j
]->type
== MONO_TYPE_GENERICINST
)
5378 add_generic_class_with_depth (acfg
, mono_class_from_mono_type_internal (sig
->params
[j
]), depth
+ 1, "arg");
5381 header
= mono_method_get_header_checked (method
, error
);
5384 for (j
= 0; j
< header
->num_locals
; ++j
)
5385 if (header
->locals
[j
]->type
== MONO_TYPE_GENERICINST
)
5386 add_generic_class_with_depth (acfg
, mono_class_from_mono_type_internal (header
->locals
[j
]), depth
+ 1, "local");
5387 mono_metadata_free_mh (header
);
5389 mono_error_cleanup (error
); /* FIXME report the error */
5395 * add_generic_instances:
5397 * Add instances referenced by the METHODSPEC/TYPESPEC table.
5400 add_generic_instances (MonoAotCompile
*acfg
)
5405 MonoGenericContext
*context
;
5407 if (acfg
->aot_opts
.no_instances
)
5410 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHODSPEC
].rows
; ++i
) {
5412 token
= MONO_TOKEN_METHOD_SPEC
| (i
+ 1);
5413 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
5416 aot_printerrf (acfg
, "Failed to load methodspec 0x%x due to %s.\n", token
, mono_error_get_message (error
));
5417 aot_printerrf (acfg
, "Run with MONO_LOG_LEVEL=debug for more information.\n");
5418 mono_error_cleanup (error
);
5422 if (m_class_get_image (method
->klass
) != acfg
->image
)
5425 context
= mono_method_get_context (method
);
5427 if (context
&& ((context
->class_inst
&& context
->class_inst
->is_open
)))
5431 * For open methods, create an instantiation which can be passed to the JIT.
5432 * FIXME: Handle class_inst as well.
5434 if (context
&& context
->method_inst
&& context
->method_inst
->is_open
) {
5436 MonoGenericContext shared_context
;
5437 MonoGenericInst
*inst
;
5438 MonoType
**type_argv
;
5440 MonoMethod
*declaring_method
;
5441 gboolean supported
= TRUE
;
5443 /* Check that the context doesn't contain open constructed types */
5444 if (context
->class_inst
) {
5445 inst
= context
->class_inst
;
5446 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5447 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
)
5449 if (mono_class_is_open_constructed_type (inst
->type_argv
[i
]))
5453 if (context
->method_inst
) {
5454 inst
= context
->method_inst
;
5455 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5456 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
)
5458 if (mono_class_is_open_constructed_type (inst
->type_argv
[i
]))
5466 memset (&shared_context
, 0, sizeof (MonoGenericContext
));
5468 inst
= context
->class_inst
;
5470 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
5471 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5472 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
)
5473 type_argv
[i
] = mono_get_object_type ();
5475 type_argv
[i
] = inst
->type_argv
[i
];
5478 shared_context
.class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
5482 inst
= context
->method_inst
;
5484 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
5485 for (i
= 0; i
< inst
->type_argc
; ++i
) {
5486 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
)
5487 type_argv
[i
] = mono_get_object_type ();
5489 type_argv
[i
] = inst
->type_argv
[i
];
5492 shared_context
.method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
5496 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
))
5497 declaring_method
= method
;
5499 declaring_method
= mono_method_get_declaring_generic_method (method
);
5501 method
= mono_class_inflate_generic_method_checked (declaring_method
, &shared_context
, error
);
5502 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
5506 * If the method is fully sharable, it was already added in place of its
5507 * generic definition.
5509 if (mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, FALSE
))
5513 * FIXME: Partially shared methods are not shared here, so we end up with
5514 * many identical methods.
5516 add_extra_method (acfg
, method
);
5519 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPESPEC
].rows
; ++i
) {
5523 token
= MONO_TOKEN_TYPE_SPEC
| (i
+ 1);
5525 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
5526 if (!klass
|| m_class_get_rank (klass
)) {
5527 mono_error_cleanup (error
);
5531 add_generic_class (acfg
, klass
, FALSE
, "typespec");
5534 /* Add types of args/locals */
5535 for (i
= 0; i
< acfg
->methods
->len
; ++i
) {
5536 method
= (MonoMethod
*)g_ptr_array_index (acfg
->methods
, i
);
5537 add_types_from_method_header (acfg
, method
);
5540 if (acfg
->image
== mono_defaults
.corlib
) {
5542 MonoType
*insts
[256];
5545 MonoType
*byte_type
= m_class_get_byval_arg (mono_defaults
.byte_class
);
5546 MonoType
*sbyte_type
= m_class_get_byval_arg (mono_defaults
.sbyte_class
);
5547 MonoType
*int16_type
= m_class_get_byval_arg (mono_defaults
.int16_class
);
5548 MonoType
*uint16_type
= m_class_get_byval_arg (mono_defaults
.uint16_class
);
5549 MonoType
*int32_type
= mono_get_int32_type ();
5550 MonoType
*uint32_type
= m_class_get_byval_arg (mono_defaults
.uint32_class
);
5551 MonoType
*int64_type
= m_class_get_byval_arg (mono_defaults
.int64_class
);
5552 MonoType
*uint64_type
= m_class_get_byval_arg (mono_defaults
.uint64_class
);
5553 MonoType
*object_type
= mono_get_object_type ();
5555 insts
[ninsts
++] = byte_type
;
5556 insts
[ninsts
++] = sbyte_type
;
5557 insts
[ninsts
++] = int16_type
;
5558 insts
[ninsts
++] = uint16_type
;
5559 insts
[ninsts
++] = int32_type
;
5560 insts
[ninsts
++] = uint32_type
;
5561 insts
[ninsts
++] = int64_type
;
5562 insts
[ninsts
++] = uint64_type
;
5563 insts
[ninsts
++] = m_class_get_byval_arg (mono_defaults
.single_class
);
5564 insts
[ninsts
++] = m_class_get_byval_arg (mono_defaults
.double_class
);
5565 insts
[ninsts
++] = m_class_get_byval_arg (mono_defaults
.char_class
);
5566 insts
[ninsts
++] = m_class_get_byval_arg (mono_defaults
.boolean_class
);
5568 /* Add GenericComparer<T> instances for primitive types for Enum.ToString () */
5569 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "GenericComparer`1");
5571 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5572 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "GenericEqualityComparer`1");
5574 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5576 /* Add instances of EnumEqualityComparer which are created by EqualityComparer<T> for enums */
5578 MonoClass
*enum_comparer
;
5579 MonoType
*insts
[16];
5583 insts
[ninsts
++] = int32_type
;
5584 insts
[ninsts
++] = uint32_type
;
5585 insts
[ninsts
++] = uint16_type
;
5586 insts
[ninsts
++] = byte_type
;
5587 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "EnumEqualityComparer`1");
5588 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
5591 insts
[ninsts
++] = int16_type
;
5592 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "ShortEnumEqualityComparer`1");
5593 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
5596 insts
[ninsts
++] = sbyte_type
;
5597 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "SByteEnumEqualityComparer`1");
5598 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
5600 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "LongEnumEqualityComparer`1");
5602 insts
[ninsts
++] = int64_type
;
5603 insts
[ninsts
++] = uint64_type
;
5604 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
5607 /* Add instances of the array generic interfaces for primitive types */
5608 /* This will add instances of the InternalArray_ helper methods in Array too */
5609 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "ICollection`1");
5611 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5613 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "IList`1");
5615 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5617 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "IEnumerable`1");
5619 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
5622 * Add a managed-to-native wrapper of Array.GetGenericValueImpl<object>, which is
5623 * used for all instances of GetGenericValueImpl by the AOT runtime.
5627 MonoGenericContext ctx
;
5628 MonoType
*args
[16];
5629 MonoMethod
*get_method
;
5630 MonoClass
*array_klass
= m_class_get_parent (mono_class_create_array (mono_defaults
.object_class
, 1));
5632 get_method
= mono_class_get_method_from_name_checked (array_klass
, "GetGenericValueImpl", 2, 0, error
);
5633 mono_error_assert_ok (error
);
5636 memset (&ctx
, 0, sizeof (ctx
));
5637 args
[0] = object_type
;
5638 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
5639 add_extra_method (acfg
, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (get_method
, &ctx
, error
), TRUE
, TRUE
));
5640 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
5644 /* Same for CompareExchange<T>/Exchange<T> */
5646 MonoGenericContext ctx
;
5647 MonoType
*args
[16];
5649 MonoClass
*interlocked_klass
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Threading", "Interlocked");
5650 gpointer iter
= NULL
;
5652 while ((m
= mono_class_get_methods (interlocked_klass
, &iter
))) {
5653 if ((!strcmp (m
->name
, "CompareExchange") || !strcmp (m
->name
, "Exchange")) && m
->is_generic
) {
5655 memset (&ctx
, 0, sizeof (ctx
));
5656 args
[0] = object_type
;
5657 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
5658 add_extra_method (acfg
, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m
, &ctx
, error
), TRUE
, TRUE
));
5659 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
5664 /* object[] accessor wrappers. */
5665 for (i
= 1; i
< 4; ++i
) {
5666 MonoClass
*obj_array_class
= mono_class_create_array (mono_defaults
.object_class
, i
);
5669 m
= get_method_nofail (obj_array_class
, "Get", i
, 0);
5672 m
= mono_marshal_get_array_accessor_wrapper (m
);
5673 add_extra_method (acfg
, m
);
5675 m
= get_method_nofail (obj_array_class
, "Address", i
, 0);
5678 m
= mono_marshal_get_array_accessor_wrapper (m
);
5679 add_extra_method (acfg
, m
);
5681 m
= get_method_nofail (obj_array_class
, "Set", i
+ 1, 0);
5684 m
= mono_marshal_get_array_accessor_wrapper (m
);
5685 add_extra_method (acfg
, m
);
5691 decode_direct_icall_symbol_name_attribute (MonoMethod
*method
)
5696 char *symbol_name
= NULL
;
5698 MonoCustomAttrInfo
*cattr
= mono_custom_attrs_from_method_checked (method
, error
);
5699 if (is_ok(error
) && cattr
) {
5700 for (j
= 0; j
< cattr
->num_attrs
; j
++)
5701 if (cattr
->attrs
[j
].ctor
&& !strcmp (m_class_get_name (cattr
->attrs
[j
].ctor
->klass
), "MonoDirectICallSymbolNameAttribute"))
5704 if (j
< cattr
->num_attrs
) {
5705 MonoCustomAttrEntry
*e
= &cattr
->attrs
[j
];
5706 MonoMethodSignature
*sig
= mono_method_signature_internal (e
->ctor
);
5707 if (e
->data
&& sig
&& sig
->param_count
== 1 && sig
->params
[0]->type
== MONO_TYPE_STRING
) {
5709 * Decode the cattr manually since we can't create objects
5710 * during aot compilation.
5714 const char *p
= ((const char*)e
->data
) + 2;
5715 int slen
= mono_metadata_decode_value (p
, &p
);
5717 symbol_name
= (char *)g_memdup (p
, slen
+ 1);
5719 symbol_name
[slen
] = 0;
5727 lookup_external_icall_symbol_name_aot (MonoMethod
*method
)
5729 g_assert (method_to_external_icall_symbol_name
);
5731 gpointer key
, value
;
5732 if (g_hash_table_lookup_extended (method_to_external_icall_symbol_name
, method
, &key
, &value
))
5733 return (const char*)value
;
5735 char *symbol_name
= decode_direct_icall_symbol_name_attribute (method
);
5736 g_hash_table_insert (method_to_external_icall_symbol_name
, method
, symbol_name
);
5742 lookup_icall_symbol_name_aot (MonoMethod
*method
)
5744 const char * symbol_name
= mono_lookup_icall_symbol (method
);
5746 symbol_name
= lookup_external_icall_symbol_name_aot (method
);
5752 mono_aot_direct_icalls_enabled_for_method (MonoCompile
*cfg
, MonoMethod
*method
)
5754 gboolean enable_icall
= FALSE
;
5755 if (cfg
->compile_aot
)
5756 enable_icall
= lookup_external_icall_symbol_name_aot (method
) ? TRUE
: FALSE
;
5758 enable_icall
= FALSE
;
5760 return enable_icall
;
5764 * is_direct_callable:
5766 * Return whenever the method identified by JI is directly callable without
5767 * going through the PLT.
5770 is_direct_callable (MonoAotCompile
*acfg
, MonoMethod
*method
, MonoJumpInfo
*patch_info
)
5772 if ((patch_info
->type
== MONO_PATCH_INFO_METHOD
) && (m_class_get_image (patch_info
->data
.method
->klass
) == acfg
->image
)) {
5773 MonoCompile
*callee_cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, patch_info
->data
.method
);
5775 gboolean direct_callable
= TRUE
;
5777 if (direct_callable
&& (acfg
->aot_opts
.dedup
|| acfg
->aot_opts
.dedup_include
) && mono_aot_can_dedup (patch_info
->data
.method
))
5778 direct_callable
= FALSE
;
5780 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
)))
5781 direct_callable
= FALSE
;
5783 if (direct_callable
&& !strcmp (callee_cfg
->method
->name
, ".cctor"))
5784 direct_callable
= FALSE
;
5787 // FIXME: Support inflated methods, it asserts in mini_llvm_init_gshared_method_this () because the method is not in
5788 // amodule->extra_methods.
5790 if (direct_callable
&& callee_cfg
->method
->is_inflated
)
5791 direct_callable
= FALSE
;
5793 if (direct_callable
&& (callee_cfg
->method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
) && (!method
|| method
->wrapper_type
!= MONO_WRAPPER_SYNCHRONIZED
))
5794 // FIXME: Maybe call the wrapper directly ?
5795 direct_callable
= FALSE
;
5797 if (direct_callable
&& (acfg
->aot_opts
.soft_debug
|| acfg
->aot_opts
.no_direct_calls
)) {
5798 /* Disable this so all calls go through load_method (), see the
5799 * mini_get_debug_options ()->load_aot_jit_info_eagerly = TRUE; line in
5800 * mono_debugger_agent_init ().
5802 direct_callable
= FALSE
;
5805 if (direct_callable
&& (callee_cfg
->method
->wrapper_type
== MONO_WRAPPER_ALLOC
))
5806 /* sgen does some initialization when the allocator method is created */
5807 direct_callable
= FALSE
;
5808 if (direct_callable
&& (callee_cfg
->method
->wrapper_type
== MONO_WRAPPER_WRITE_BARRIER
))
5809 /* we don't know at compile time whether sgen is concurrent or not */
5810 direct_callable
= FALSE
;
5812 if (direct_callable
)
5815 } else if ((patch_info
->type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
&& patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)) {
5816 if (acfg
->aot_opts
.direct_pinvoke
)
5818 } else if (patch_info
->type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
5819 if (acfg
->aot_opts
.direct_icalls
)
5827 #ifdef MONO_ARCH_AOT_SUPPORTED
5829 get_pinvoke_import (MonoAotCompile
*acfg
, MonoMethod
*method
)
5831 MonoImage
*image
= m_class_get_image (method
->klass
);
5832 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*) method
;
5833 MonoTableInfo
*tables
= image
->tables
;
5834 MonoTableInfo
*im
= &tables
[MONO_TABLE_IMPLMAP
];
5835 MonoTableInfo
*mr
= &tables
[MONO_TABLE_MODULEREF
];
5836 guint32 im_cols
[MONO_IMPLMAP_SIZE
];
5839 import
= (char *)g_hash_table_lookup (acfg
->method_to_pinvoke_import
, method
);
5843 if (!piinfo
->implmap_idx
|| piinfo
->implmap_idx
> im
->rows
)
5846 mono_metadata_decode_row (im
, piinfo
->implmap_idx
- 1, im_cols
, MONO_IMPLMAP_SIZE
);
5848 if (!im_cols
[MONO_IMPLMAP_SCOPE
] || im_cols
[MONO_IMPLMAP_SCOPE
] > mr
->rows
)
5851 import
= g_strdup_printf ("%s", mono_metadata_string_heap (image
, im_cols
[MONO_IMPLMAP_NAME
]));
5853 g_hash_table_insert (acfg
->method_to_pinvoke_import
, method
, import
);
5859 get_pinvoke_import (MonoAotCompile
*acfg
, MonoMethod
*method
)
5866 compare_lne (MonoDebugLineNumberEntry
*a
, MonoDebugLineNumberEntry
*b
)
5868 if (a
->native_offset
== b
->native_offset
)
5869 return a
->il_offset
- b
->il_offset
;
5871 return a
->native_offset
- b
->native_offset
;
5875 * compute_line_numbers:
5877 * Returns a sparse array of size CODE_SIZE containing MonoDebugSourceLocation* entries for the native offsets which have a corresponding line number
5880 static MonoDebugSourceLocation
**
5881 compute_line_numbers (MonoMethod
*method
, int code_size
, MonoDebugMethodJitInfo
*debug_info
)
5883 MonoDebugMethodInfo
*minfo
;
5884 MonoDebugLineNumberEntry
*ln_array
;
5885 MonoDebugSourceLocation
*loc
;
5886 int i
, prev_line
, prev_il_offset
;
5887 int *native_to_il_offset
= NULL
;
5888 MonoDebugSourceLocation
**res
;
5891 minfo
= mono_debug_lookup_method (method
);
5894 // FIXME: This seems to happen when two methods have the same cfg->method_to_register
5895 if (debug_info
->code_size
!= code_size
)
5898 g_assert (code_size
);
5900 /* Compute the native->IL offset mapping */
5902 ln_array
= g_new0 (MonoDebugLineNumberEntry
, debug_info
->num_line_numbers
);
5903 memcpy (ln_array
, debug_info
->line_numbers
, debug_info
->num_line_numbers
* sizeof (MonoDebugLineNumberEntry
));
5905 mono_qsort (ln_array
, debug_info
->num_line_numbers
, sizeof (MonoDebugLineNumberEntry
), (int (*)(const void *, const void *))compare_lne
);
5907 native_to_il_offset
= g_new0 (int, code_size
+ 1);
5909 for (i
= 0; i
< debug_info
->num_line_numbers
; ++i
) {
5911 MonoDebugLineNumberEntry
*lne
= &ln_array
[i
];
5914 for (j
= 0; j
< lne
->native_offset
; ++j
)
5915 native_to_il_offset
[j
] = -1;
5918 if (i
< debug_info
->num_line_numbers
- 1) {
5919 MonoDebugLineNumberEntry
*lne_next
= &ln_array
[i
+ 1];
5921 for (j
= lne
->native_offset
; j
< lne_next
->native_offset
; ++j
)
5922 native_to_il_offset
[j
] = lne
->il_offset
;
5924 for (j
= lne
->native_offset
; j
< code_size
; ++j
)
5925 native_to_il_offset
[j
] = lne
->il_offset
;
5930 /* Compute the native->line number mapping */
5931 res
= g_new0 (MonoDebugSourceLocation
*, code_size
);
5932 prev_il_offset
= -1;
5935 for (i
= 0; i
< code_size
; ++i
) {
5936 int il_offset
= native_to_il_offset
[i
];
5938 if (il_offset
== -1 || il_offset
== prev_il_offset
)
5940 prev_il_offset
= il_offset
;
5941 loc
= mono_debug_method_lookup_location (minfo
, il_offset
);
5942 if (!(loc
&& loc
->source_file
))
5944 if (loc
->row
== prev_line
) {
5945 mono_debug_free_source_location (loc
);
5948 prev_line
= loc
->row
;
5949 //printf ("D: %s:%d il=%x native=%x\n", loc->source_file, loc->row, il_offset, i);
5951 /* This will cover the prolog too */
5961 get_file_index (MonoAotCompile
*acfg
, const char *source_file
)
5965 // FIXME: Free these
5966 if (!acfg
->dwarf_ln_filenames
)
5967 acfg
->dwarf_ln_filenames
= g_hash_table_new (g_str_hash
, g_str_equal
);
5968 findex
= GPOINTER_TO_INT (g_hash_table_lookup (acfg
->dwarf_ln_filenames
, source_file
));
5970 findex
= g_hash_table_size (acfg
->dwarf_ln_filenames
) + 1;
5971 g_hash_table_insert (acfg
->dwarf_ln_filenames
, g_strdup (source_file
), GINT_TO_POINTER (findex
));
5972 emit_unset_mode (acfg
);
5973 fprintf (acfg
->fp
, ".file %d \"%s\"\n", findex
, mono_dwarf_escape_path (source_file
));
5985 * emit_and_reloc_code:
5987 * Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
5988 * is true, calls are made through the GOT too. This is used for emitting trampolines
5989 * in full-aot mode, since calls made from trampolines couldn't go through the PLT,
5990 * since trampolines are needed to make PLT work.
5993 emit_and_reloc_code (MonoAotCompile
*acfg
, MonoMethod
*method
, guint8
*code
, guint32 code_len
, MonoJumpInfo
*relocs
, gboolean got_only
, MonoDebugMethodJitInfo
*debug_info
)
5995 int i
, pindex
, start_index
;
5997 MonoJumpInfo
*patch_info
;
5998 MonoDebugSourceLocation
**locs
= NULL
;
5999 gboolean skip
, prologue_end
= FALSE
;
6000 #ifdef MONO_ARCH_AOT_SUPPORTED
6001 gboolean direct_call
, external_call
;
6003 const char *direct_call_target
= 0;
6004 const char *direct_pinvoke
;
6007 if (acfg
->gas_line_numbers
&& method
&& debug_info
) {
6008 locs
= compute_line_numbers (method
, code_len
, debug_info
);
6010 int findex
= get_file_index (acfg
, "<unknown>");
6011 emit_unset_mode (acfg
);
6012 fprintf (acfg
->fp
, ".loc %d %d 0\n", findex
, 1);
6016 /* Collect and sort relocations */
6017 patches
= g_ptr_array_new ();
6018 for (patch_info
= relocs
; patch_info
; patch_info
= patch_info
->next
)
6019 g_ptr_array_add (patches
, patch_info
);
6020 g_ptr_array_sort (patches
, compare_patches
);
6023 for (i
= 0; i
< code_len
; i
+= INST_LEN
) {
6025 for (pindex
= start_index
; pindex
< patches
->len
; ++pindex
) {
6026 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
6027 if (patch_info
->ip
.i
>= i
)
6031 if (locs
&& locs
[i
]) {
6032 MonoDebugSourceLocation
*loc
= locs
[i
];
6034 const char *options
;
6036 findex
= get_file_index (acfg
, loc
->source_file
);
6037 emit_unset_mode (acfg
);
6039 options
= " prologue_end";
6042 prologue_end
= TRUE
;
6043 fprintf (acfg
->fp
, ".loc %d %d 0%s\n", findex
, loc
->row
, options
);
6044 mono_debug_free_source_location (loc
);
6048 #ifdef MONO_ARCH_AOT_SUPPORTED
6049 if (patch_info
&& (patch_info
->ip
.i
== i
) && (pindex
< patches
->len
)) {
6050 start_index
= pindex
;
6052 switch (patch_info
->type
) {
6053 case MONO_PATCH_INFO_NONE
:
6055 case MONO_PATCH_INFO_GOT_OFFSET
: {
6058 arch_emit_got_offset (acfg
, code
+ i
, &code_size
);
6059 i
+= code_size
- INST_LEN
;
6061 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6064 case MONO_PATCH_INFO_OBJC_SELECTOR_REF
: {
6065 int code_size
, index
;
6066 char *selector
= (char *)patch_info
->data
.target
;
6068 if (!acfg
->objc_selector_to_index
)
6069 acfg
->objc_selector_to_index
= g_hash_table_new (g_str_hash
, g_str_equal
);
6070 if (!acfg
->objc_selectors
)
6071 acfg
->objc_selectors
= g_ptr_array_new ();
6072 index
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->objc_selector_to_index
, selector
));
6076 index
= acfg
->objc_selector_index
;
6077 g_ptr_array_add (acfg
->objc_selectors
, (void*)patch_info
->data
.target
);
6078 g_hash_table_insert (acfg
->objc_selector_to_index
, selector
, GUINT_TO_POINTER (index
+ 1));
6079 acfg
->objc_selector_index
++;
6082 arch_emit_objc_selector_ref (acfg
, code
+ i
, index
, &code_size
);
6083 i
+= code_size
- INST_LEN
;
6085 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6090 * If this patch is a call, try emitting a direct call instead of
6091 * through a PLT entry. This is possible if the called method is in
6092 * the same assembly and requires no initialization.
6094 direct_call
= FALSE
;
6095 external_call
= FALSE
;
6096 if ((patch_info
->type
== MONO_PATCH_INFO_METHOD
) && (m_class_get_image (patch_info
->data
.method
->klass
) == acfg
->image
)) {
6097 if (!got_only
&& is_direct_callable (acfg
, method
, patch_info
)) {
6098 MonoCompile
*callee_cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, patch_info
->data
.method
);
6100 // Don't compile inflated methods if we're doing dedup
6101 if (acfg
->aot_opts
.dedup
&& !mono_aot_can_dedup (patch_info
->data
.method
)) {
6102 char *name
= mono_aot_get_mangled_method_name (patch_info
->data
.method
);
6103 mono_trace (G_LOG_LEVEL_DEBUG
, MONO_TRACE_AOT
, "DIRECT CALL: %s by %s", name
, method
? mono_method_full_name (method
, TRUE
) : "");
6107 direct_call_target
= callee_cfg
->asm_symbol
;
6108 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6109 acfg
->stats
.direct_calls
++;
6113 acfg
->stats
.all_calls
++;
6114 } else if (patch_info
->type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
6115 if (!got_only
&& is_direct_callable (acfg
, method
, patch_info
)) {
6116 if (!(patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
6117 direct_pinvoke
= lookup_icall_symbol_name_aot (patch_info
->data
.method
);
6119 direct_pinvoke
= get_pinvoke_import (acfg
, patch_info
->data
.method
);
6120 if (direct_pinvoke
) {
6122 g_assert (strlen (direct_pinvoke
) < 1000);
6123 direct_call_target
= g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, direct_pinvoke
);
6126 } else if (patch_info
->type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
6127 const char *sym
= mono_find_jit_icall_info (patch_info
->data
.jit_icall_id
)->c_symbol
;
6128 if (!got_only
&& sym
&& acfg
->aot_opts
.direct_icalls
) {
6129 /* Call to a C function implementing a jit icall */
6131 external_call
= TRUE
;
6132 g_assert (strlen (sym
) < 1000);
6133 direct_call_target
= g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, sym
);
6135 } else if (patch_info
->type
== MONO_PATCH_INFO_JIT_ICALL_ID
) {
6136 MonoJitICallInfo
* const info
= mono_find_jit_icall_info (patch_info
->data
.jit_icall_id
);
6137 const char * const sym
= info
->c_symbol
;
6138 if (!got_only
&& sym
&& acfg
->aot_opts
.direct_icalls
&& info
->func
== info
->wrapper
) {
6139 /* Call to a jit icall without a wrapper */
6141 external_call
= TRUE
;
6142 g_assert (strlen (sym
) < 1000);
6143 direct_call_target
= g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, sym
);
6147 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6148 acfg
->stats
.direct_calls
++;
6151 if (!got_only
&& !direct_call
) {
6152 MonoPltEntry
*plt_entry
= get_plt_entry (acfg
, patch_info
);
6154 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
6156 direct_call_target
= plt_entry
->symbol
;
6158 /* Nullify the patch */
6159 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6160 plt_entry
->jit_used
= TRUE
;
6167 arch_emit_direct_call (acfg
, direct_call_target
, external_call
, FALSE
, patch_info
, &call_size
);
6168 i
+= call_size
- INST_LEN
;
6172 got_slot
= get_got_offset (acfg
, FALSE
, patch_info
);
6174 arch_emit_got_access (acfg
, acfg
->got_symbol
, code
+ i
, got_slot
, &code_size
);
6175 i
+= code_size
- INST_LEN
;
6181 #endif /* MONO_ARCH_AOT_SUPPORTED */
6184 /* Find next patch */
6186 for (pindex
= start_index
; pindex
< patches
->len
; ++pindex
) {
6187 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
6188 if (patch_info
->ip
.i
>= i
)
6192 /* Try to emit multiple bytes at once */
6193 if (pindex
< patches
->len
&& patch_info
->ip
.i
> i
) {
6196 for (limit
= i
+ INST_LEN
; limit
< patch_info
->ip
.i
; limit
+= INST_LEN
) {
6197 if (locs
&& locs
[limit
])
6201 emit_code_bytes (acfg
, code
+ i
, limit
- i
);
6202 i
= limit
- INST_LEN
;
6204 emit_code_bytes (acfg
, code
+ i
, INST_LEN
);
6209 g_ptr_array_free (patches
, TRUE
);
6216 * Return a modified version of S which only includes characters permissible in symbols.
6219 sanitize_symbol (MonoAotCompile
*acfg
, char *s
)
6221 gboolean process
= FALSE
;
6230 for (i
= 0; i
< len
; ++i
)
6231 if (!(s
[i
] <= 0x7f && (isalnum (s
[i
]) || s
[i
] == '_')))
6236 gs
= g_string_sized_new (len
);
6237 for (i
= 0; i
< len
; ++i
) {
6239 if (c
<= 0x7f && (isalnum (c
) || c
== '_')) {
6240 g_string_append_c (gs
, c
);
6241 } else if (c
> 0x7f) {
6242 /* multi-byte utf8 */
6243 g_string_append_printf (gs
, "_0x%x", c
);
6246 while (c
>> 6 == 0x2) {
6247 g_string_append_printf (gs
, "%x", c
);
6251 g_string_append_printf (gs
, "_");
6254 g_string_append_c (gs
, '_');
6258 res
= mono_mempool_strdup (acfg
->mempool
, gs
->str
);
6259 g_string_free (gs
, TRUE
);
6264 get_debug_sym (MonoMethod
*method
, const char *prefix
, GHashTable
*cache
)
6266 char *name1
, *name2
, *cached
;
6267 int i
, j
, len
, count
;
6268 MonoMethod
*cached_method
;
6270 name1
= mono_method_full_name (method
, TRUE
);
6273 // This is so that we don't accidentally create a local symbol (which starts with 'L')
6274 if ((!prefix
|| !*prefix
) && name1
[0] == 'L')
6278 #if defined(TARGET_WIN32) && defined(TARGET_X86)
6279 char adjustedPrefix
[MAX_SYMBOL_SIZE
];
6280 prefix
= mangle_symbol (prefix
, adjustedPrefix
, G_N_ELEMENTS (adjustedPrefix
));
6283 len
= strlen (name1
);
6284 name2
= (char *) g_malloc (strlen (prefix
) + len
+ 16);
6285 memcpy (name2
, prefix
, strlen (prefix
));
6286 j
= strlen (prefix
);
6287 for (i
= 0; i
< len
; ++i
) {
6288 if (i
== 0 && name1
[0] >= '0' && name1
[0] <= '9') {
6290 } else if (isalnum (name1
[i
])) {
6291 name2
[j
++] = name1
[i
];
6292 } else if (name1
[i
] == ' ' && name1
[i
+ 1] == '(' && name1
[i
+ 2] == ')') {
6294 } else if (name1
[i
] == ',' && name1
[i
+ 1] == ' ') {
6297 } else if (name1
[i
] == '(' || name1
[i
] == ')' || name1
[i
] == '>') {
6307 cached_method
= (MonoMethod
*)g_hash_table_lookup (cache
, name2
);
6308 if (!(cached_method
&& cached_method
!= method
))
6310 sprintf (name2
+ j
, "_%d", count
);
6314 cached
= g_strdup (name2
);
6315 g_hash_table_insert (cache
, cached
, method
);
6321 emit_method_code (MonoAotCompile
*acfg
, MonoCompile
*cfg
)
6326 char *debug_sym
= NULL
;
6327 char *symbol
= NULL
;
6328 int func_alignment
= AOT_FUNC_ALIGNMENT
;
6331 g_assert (!ignore_cfg (cfg
));
6333 method
= cfg
->orig_method
;
6334 code
= cfg
->native_code
;
6336 method_index
= get_method_index (acfg
, method
);
6337 symbol
= g_strdup_printf ("%sme_%x", acfg
->temp_prefix
, method_index
);
6339 /* Make the labels local */
6340 emit_section_change (acfg
, ".text", 0);
6341 emit_alignment_code (acfg
, func_alignment
);
6343 if (acfg
->global_symbols
&& acfg
->need_no_dead_strip
)
6344 fprintf (acfg
->fp
, " .no_dead_strip %s\n", cfg
->asm_symbol
);
6346 emit_label (acfg
, cfg
->asm_symbol
);
6348 if (acfg
->aot_opts
.write_symbols
&& !acfg
->global_symbols
&& !acfg
->llvm
) {
6350 * Write a C style symbol for every method, this has two uses:
6351 * - it works on platforms where the dwarf debugging info is not
6353 * - it allows the setting of breakpoints of aot-ed methods.
6356 // Comment out to force dedup to link these symbols and forbid compiling
6357 // in duplicated code. This is an "assert when linking if broken" trick.
6358 /*if (mono_aot_can_dedup (method) && (acfg->aot_opts.dedup || acfg->aot_opts.dedup_include))*/
6359 /*debug_sym = mono_aot_get_mangled_method_name (method);*/
6361 debug_sym
= get_debug_sym (method
, "", acfg
->method_label_hash
);
6363 cfg
->asm_debug_symbol
= g_strdup (debug_sym
);
6365 if (acfg
->need_no_dead_strip
)
6366 fprintf (acfg
->fp
, " .no_dead_strip %s\n", debug_sym
);
6368 // Comment out to force dedup to link these symbols and forbid compiling
6369 // in duplicated code. This is an "assert when linking if broken" trick.
6370 /*if (mono_aot_can_dedup (method) && (acfg->aot_opts.dedup || acfg->aot_opts.dedup_include))*/
6371 /*emit_global_inner (acfg, debug_sym, TRUE);*/
6373 emit_local_symbol (acfg
, debug_sym
, symbol
, TRUE
);
6375 emit_label (acfg
, debug_sym
);
6378 export_name
= (char *)g_hash_table_lookup (acfg
->export_names
, method
);
6380 /* Emit a global symbol for the method */
6381 emit_global_inner (acfg
, export_name
, TRUE
);
6382 emit_label (acfg
, export_name
);
6385 if (cfg
->verbose_level
> 0 && !ignore_cfg (cfg
))
6386 g_print ("Method %s emitted as %s\n", mono_method_get_full_name (method
), cfg
->asm_symbol
);
6388 acfg
->stats
.code_size
+= cfg
->code_len
;
6390 acfg
->cfgs
[method_index
]->got_offset
= acfg
->got_offset
;
6392 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 ()));
6396 if (acfg
->aot_opts
.write_symbols
) {
6398 emit_symbol_size (acfg
, debug_sym
, ".");
6400 emit_symbol_size (acfg
, cfg
->asm_symbol
, ".");
6404 emit_label (acfg
, symbol
);
6406 arch_emit_unwind_info_sections (acfg
, cfg
->asm_symbol
, symbol
, cfg
->unwind_ops
);
6414 * Encode PATCH_INFO into its disk representation.
6417 encode_patch (MonoAotCompile
*acfg
, MonoJumpInfo
*patch_info
, guint8
*buf
, guint8
**endbuf
)
6421 switch (patch_info
->type
) {
6422 case MONO_PATCH_INFO_NONE
:
6424 case MONO_PATCH_INFO_IMAGE
:
6425 encode_value (get_image_index (acfg
, patch_info
->data
.image
), p
, &p
);
6427 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
:
6428 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
:
6429 case MONO_PATCH_INFO_GC_NURSERY_START
:
6430 case MONO_PATCH_INFO_GC_NURSERY_BITS
:
6432 case MONO_PATCH_INFO_SWITCH
: {
6433 gpointer
*table
= (gpointer
*)patch_info
->data
.table
->table
;
6436 encode_value (patch_info
->data
.table
->table_size
, p
, &p
);
6437 for (k
= 0; k
< patch_info
->data
.table
->table_size
; k
++)
6438 encode_value ((int)(gssize
)table
[k
], p
, &p
);
6441 case MONO_PATCH_INFO_METHODCONST
:
6442 case MONO_PATCH_INFO_METHOD
:
6443 case MONO_PATCH_INFO_METHOD_JUMP
:
6444 case MONO_PATCH_INFO_METHOD_FTNDESC
:
6445 case MONO_PATCH_INFO_ICALL_ADDR
:
6446 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
6447 case MONO_PATCH_INFO_METHOD_RGCTX
:
6448 case MONO_PATCH_INFO_METHOD_CODE_SLOT
:
6449 encode_method_ref (acfg
, patch_info
->data
.method
, p
, &p
);
6451 case MONO_PATCH_INFO_AOT_JIT_INFO
:
6452 case MONO_PATCH_INFO_CASTCLASS_CACHE
:
6453 encode_value (patch_info
->data
.index
, p
, &p
);
6455 case MONO_PATCH_INFO_JIT_ICALL_ID
:
6456 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
6457 case MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
:
6458 encode_value (patch_info
->data
.jit_icall_id
, p
, &p
);
6460 case MONO_PATCH_INFO_SPECIFIC_TRAMPOLINE_LAZY_FETCH_ADDR
:
6461 encode_value (patch_info
->data
.uindex
, p
, &p
);
6463 case MONO_PATCH_INFO_LDSTR_LIT
: {
6464 guint32 len
= strlen (patch_info
->data
.name
);
6465 encode_value (len
, p
, &p
);
6466 memcpy (p
, patch_info
->data
.name
, len
+ 1);
6470 case MONO_PATCH_INFO_LDSTR
: {
6471 guint32 image_index
= get_image_index (acfg
, patch_info
->data
.token
->image
);
6472 guint32 token
= patch_info
->data
.token
->token
;
6473 g_assert (mono_metadata_token_code (token
) == MONO_TOKEN_STRING
);
6474 encode_value (image_index
, p
, &p
);
6475 encode_value (patch_info
->data
.token
->token
- MONO_TOKEN_STRING
, p
, &p
);
6478 case MONO_PATCH_INFO_RVA
:
6479 case MONO_PATCH_INFO_DECLSEC
:
6480 case MONO_PATCH_INFO_LDTOKEN
:
6481 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
6482 encode_value (get_image_index (acfg
, patch_info
->data
.token
->image
), p
, &p
);
6483 encode_value (patch_info
->data
.token
->token
, p
, &p
);
6484 encode_value (patch_info
->data
.token
->has_context
, p
, &p
);
6485 if (patch_info
->data
.token
->has_context
)
6486 encode_generic_context (acfg
, &patch_info
->data
.token
->context
, p
, &p
);
6488 case MONO_PATCH_INFO_EXC_NAME
: {
6489 MonoClass
*ex_class
;
6492 mono_class_load_from_name (m_class_get_image (mono_defaults
.exception_class
),
6493 "System", (const char *)patch_info
->data
.target
);
6494 encode_klass_ref (acfg
, ex_class
, p
, &p
);
6497 case MONO_PATCH_INFO_R4
:
6498 encode_value (*((guint32
*)patch_info
->data
.target
), p
, &p
);
6500 case MONO_PATCH_INFO_R8
:
6501 encode_value (((guint32
*)patch_info
->data
.target
) [MINI_LS_WORD_IDX
], p
, &p
);
6502 encode_value (((guint32
*)patch_info
->data
.target
) [MINI_MS_WORD_IDX
], p
, &p
);
6504 case MONO_PATCH_INFO_VTABLE
:
6505 case MONO_PATCH_INFO_CLASS
:
6506 case MONO_PATCH_INFO_IID
:
6507 case MONO_PATCH_INFO_ADJUSTED_IID
:
6508 encode_klass_ref (acfg
, patch_info
->data
.klass
, p
, &p
);
6510 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
6511 encode_klass_ref (acfg
, patch_info
->data
.del_tramp
->klass
, p
, &p
);
6512 if (patch_info
->data
.del_tramp
->method
) {
6513 encode_value (1, p
, &p
);
6514 encode_method_ref (acfg
, patch_info
->data
.del_tramp
->method
, p
, &p
);
6516 encode_value (0, p
, &p
);
6518 encode_value (patch_info
->data
.del_tramp
->is_virtual
, p
, &p
);
6520 case MONO_PATCH_INFO_FIELD
:
6521 case MONO_PATCH_INFO_SFLDA
:
6522 encode_field_info (acfg
, patch_info
->data
.field
, p
, &p
);
6524 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
6526 case MONO_PATCH_INFO_PROFILER_ALLOCATION_COUNT
:
6527 case MONO_PATCH_INFO_PROFILER_CLAUSE_COUNT
:
6529 case MONO_PATCH_INFO_RGCTX_FETCH
:
6530 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
6531 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
6535 * entry->d.klass/method has a lenghtly encoding and multiple rgctx_fetch entries
6536 * reference the same klass/method, so encode it only once.
6537 * For patches which refer to got entries, this sharing is done by get_got_offset, but
6538 * these are not got entries.
6540 if (entry
->in_mrgctx
) {
6541 offset
= get_shared_method_ref (acfg
, entry
->d
.method
);
6543 offset
= get_shared_klass_ref (acfg
, entry
->d
.klass
);
6546 encode_value (offset
, p
, &p
);
6547 g_assert ((int)entry
->info_type
< 256);
6548 g_assert (entry
->data
->type
< 256);
6549 encode_value ((entry
->in_mrgctx
? 1 : 0) | (entry
->info_type
<< 1) | (entry
->data
->type
<< 9), p
, &p
);
6550 encode_patch (acfg
, entry
->data
, p
, &p
);
6553 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
6554 case MONO_PATCH_INFO_AOT_MODULE
:
6556 case MONO_PATCH_INFO_SIGNATURE
:
6557 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER
:
6558 encode_signature (acfg
, (MonoMethodSignature
*)patch_info
->data
.target
, p
, &p
);
6560 case MONO_PATCH_INFO_GSHAREDVT_CALL
:
6561 encode_signature (acfg
, (MonoMethodSignature
*)patch_info
->data
.gsharedvt
->sig
, p
, &p
);
6562 encode_method_ref (acfg
, patch_info
->data
.gsharedvt
->method
, p
, &p
);
6564 case MONO_PATCH_INFO_GSHAREDVT_METHOD
: {
6565 MonoGSharedVtMethodInfo
*info
= patch_info
->data
.gsharedvt_method
;
6568 encode_method_ref (acfg
, info
->method
, p
, &p
);
6569 encode_value (info
->num_entries
, p
, &p
);
6570 for (i
= 0; i
< info
->num_entries
; ++i
) {
6571 MonoRuntimeGenericContextInfoTemplate
*template_
= &info
->entries
[i
];
6573 encode_value (template_
->info_type
, p
, &p
);
6574 switch (mini_rgctx_info_type_to_patch_info_type (template_
->info_type
)) {
6575 case MONO_PATCH_INFO_CLASS
:
6576 encode_klass_ref (acfg
, mono_class_from_mono_type_internal ((MonoType
*)template_
->data
), p
, &p
);
6578 case MONO_PATCH_INFO_FIELD
:
6579 encode_field_info (acfg
, (MonoClassField
*)template_
->data
, p
, &p
);
6582 g_assert_not_reached ();
6588 case MONO_PATCH_INFO_VIRT_METHOD
:
6589 encode_klass_ref (acfg
, patch_info
->data
.virt_method
->klass
, p
, &p
);
6590 encode_method_ref (acfg
, patch_info
->data
.virt_method
->method
, p
, &p
);
6592 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG
:
6595 g_error ("unable to handle jump info %d", patch_info
->type
);
6602 encode_patch_list (MonoAotCompile
*acfg
, GPtrArray
*patches
, int n_patches
, gboolean llvm
, guint8
*buf
, guint8
**endbuf
)
6605 guint32 pindex
, offset
;
6606 MonoJumpInfo
*patch_info
;
6608 encode_value (n_patches
, p
, &p
);
6610 for (pindex
= 0; pindex
< patches
->len
; ++pindex
) {
6611 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
6613 if (patch_info
->type
== MONO_PATCH_INFO_NONE
|| patch_info
->type
== MONO_PATCH_INFO_BB
)
6616 /* This shouldn't allocate a new offset */
6617 offset
= lookup_got_offset (acfg
, llvm
, patch_info
);
6618 encode_value (offset
, p
, &p
);
6625 emit_method_info (MonoAotCompile
*acfg
, MonoCompile
*cfg
)
6628 int pindex
, buf_size
, n_patches
;
6630 MonoJumpInfo
*patch_info
;
6633 gboolean needs_ctx
= FALSE
;
6635 method
= cfg
->orig_method
;
6637 (void)get_method_index (acfg
, method
);
6639 /* Sort relocations */
6640 patches
= g_ptr_array_new ();
6641 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
)
6642 g_ptr_array_add (patches
, patch_info
);
6643 if (!acfg
->aot_opts
.llvm_only
)
6644 g_ptr_array_sort (patches
, compare_patches
);
6646 /**********************/
6647 /* Encode method info */
6648 /**********************/
6650 g_assert (!(cfg
->opt
& MONO_OPT_SHARED
));
6652 guint32
*got_offsets
= g_new0 (guint32
, patches
->len
);
6655 for (pindex
= 0; pindex
< patches
->len
; ++pindex
) {
6656 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
6658 if ((patch_info
->type
== MONO_PATCH_INFO_GOT_OFFSET
) ||
6659 (patch_info
->type
== MONO_PATCH_INFO_NONE
)) {
6660 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6665 if ((patch_info
->type
== MONO_PATCH_INFO_IMAGE
) && (patch_info
->data
.image
== acfg
->image
)) {
6666 /* Stored in a GOT slot initialized at module load time */
6667 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6671 if (patch_info
->type
== MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
||
6672 patch_info
->type
== MONO_PATCH_INFO_GC_NURSERY_START
||
6673 patch_info
->type
== MONO_PATCH_INFO_GC_NURSERY_BITS
||
6674 patch_info
->type
== MONO_PATCH_INFO_AOT_MODULE
) {
6675 /* Stored in a GOT slot initialized at module load time */
6676 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6680 if (is_plt_patch (patch_info
) && !(cfg
->compile_llvm
&& acfg
->aot_opts
.llvm_only
)) {
6681 /* Calls are made through the PLT */
6682 patch_info
->type
= MONO_PATCH_INFO_NONE
;
6686 if (acfg
->aot_opts
.llvm_only
&& patch_info
->type
== MONO_PATCH_INFO_METHOD
)
6689 /* This shouldn't allocate a new offset */
6690 offset
= lookup_got_offset (acfg
, cfg
->compile_llvm
, patch_info
);
6691 if (offset
>= acfg
->nshared_got_entries
)
6692 got_offsets
[n_patches
++] = offset
;
6696 g_assert (cfg
->has_got_slots
);
6698 buf_size
= (patches
->len
< 1000) ? 40960 : 40960 + (patches
->len
* 64);
6699 p
= buf
= (guint8
*)g_malloc (buf_size
);
6701 MonoGenericContext
*ctx
= mono_method_get_context (cfg
->method
);
6704 if (mono_class_get_cctor (method
->klass
))
6705 flags
|= MONO_AOT_METHOD_FLAG_HAS_CCTOR
;
6706 if (mini_jit_info_is_gsharedvt (cfg
->jit_info
) && mini_is_gsharedvt_variable_signature (mono_method_signature_internal (jinfo_get_method (cfg
->jit_info
))))
6707 flags
|= MONO_AOT_METHOD_FLAG_GSHAREDVT_VARIABLE
;
6709 flags
|= MONO_AOT_METHOD_FLAG_HAS_PATCHES
;
6710 if (needs_ctx
&& ctx
)
6711 flags
|= MONO_AOT_METHOD_FLAG_HAS_CTX
;
6712 encode_value (flags
, p
, &p
);
6713 if (flags
& MONO_AOT_METHOD_FLAG_HAS_CCTOR
)
6714 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
6715 if (needs_ctx
&& ctx
)
6716 encode_generic_context (acfg
, ctx
, p
, &p
);
6719 encode_value (n_patches
, p
, &p
);
6720 for (int i
= 0; i
< n_patches
; ++i
)
6721 encode_value (got_offsets
[i
], p
, &p
);
6724 g_ptr_array_free (patches
, TRUE
);
6725 g_free (got_offsets
);
6727 acfg
->stats
.method_info_size
+= p
- buf
;
6729 g_assert (p
- buf
< buf_size
);
6731 cfg
->method_info_offset
= add_to_blob (acfg
, buf
, p
- buf
);
6736 get_unwind_info_offset (MonoAotCompile
*acfg
, guint8
*encoded
, guint32 encoded_len
)
6738 guint32 cache_index
;
6741 /* Reuse the unwind module to canonize and store unwind info entries */
6742 cache_index
= mono_cache_unwind_info (encoded
, encoded_len
);
6744 /* Use +/- 1 to distinguish 0s from missing entries */
6745 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->unwind_info_offsets
, GUINT_TO_POINTER (cache_index
+ 1)));
6753 * It would be easier to use assembler symbols, but the caller needs an
6756 offset
= acfg
->unwind_info_offset
;
6757 g_hash_table_insert (acfg
->unwind_info_offsets
, GUINT_TO_POINTER (cache_index
+ 1), GUINT_TO_POINTER (offset
+ 1));
6758 g_ptr_array_add (acfg
->unwind_ops
, GUINT_TO_POINTER (cache_index
));
6761 encode_value (encoded_len
, p
, &p
);
6763 acfg
->unwind_info_offset
+= encoded_len
+ (p
- buf
);
6769 emit_exception_debug_info (MonoAotCompile
*acfg
, MonoCompile
*cfg
, gboolean store_seq_points
)
6772 guint32 debug_info_size
, seq_points_size
;
6774 MonoMethodHeader
*header
;
6775 guint8
*p
, *buf
, *debug_info
;
6776 MonoJitInfo
*jinfo
= cfg
->jit_info
;
6778 gboolean use_unwind_ops
= FALSE
;
6779 MonoSeqPointInfo
*seq_points
;
6781 code
= cfg
->native_code
;
6782 header
= cfg
->header
;
6784 if (!acfg
->aot_opts
.nodebug
) {
6785 mono_debug_serialize_debug_info (cfg
, &debug_info
, &debug_info_size
);
6788 debug_info_size
= 0;
6791 seq_points
= cfg
->seq_point_info
;
6792 seq_points_size
= (store_seq_points
)? mono_seq_point_info_get_write_size (seq_points
) : 0;
6794 buf_size
= header
->num_clauses
* 256 + debug_info_size
+ 2048 + seq_points_size
+ cfg
->gc_map_size
;
6795 if (jinfo
->has_try_block_holes
) {
6796 MonoTryBlockHoleTableJitInfo
*table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
6797 buf_size
+= table
->num_holes
* 16;
6800 p
= buf
= (guint8
*)g_malloc (buf_size
);
6802 use_unwind_ops
= cfg
->unwind_ops
!= NULL
;
6804 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);
6806 encode_value (flags
, p
, &p
);
6808 if (use_unwind_ops
) {
6809 guint32 encoded_len
;
6811 guint32 unwind_desc
;
6813 encoded
= mono_unwind_ops_encode (cfg
->unwind_ops
, &encoded_len
);
6815 unwind_desc
= get_unwind_info_offset (acfg
, encoded
, encoded_len
);
6816 encode_value (unwind_desc
, p
, &p
);
6820 encode_value (jinfo
->unwind_info
, p
, &p
);
6823 /*Encode the number of holes before the number of clauses to make decoding easier*/
6824 if (jinfo
->has_try_block_holes
) {
6825 MonoTryBlockHoleTableJitInfo
*table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
6826 encode_value (table
->num_holes
, p
, &p
);
6829 if (jinfo
->has_arch_eh_info
) {
6831 * In AOT mode, the code length is calculated from the address of the previous method,
6832 * which could include alignment padding, so calculating the start of the epilog as
6833 * code_len - epilog_size is correct any more. Save the real code len as a workaround.
6835 encode_value (jinfo
->code_size
, p
, &p
);
6838 /* Exception table */
6839 if (cfg
->compile_llvm
) {
6841 * When using LLVM, we can't emit some data, like pc offsets, this reg/offset etc.,
6842 * since the information is only available to llc. Instead, we let llc save the data
6843 * into the LSDA, and read it from there at runtime.
6845 /* The assembly might be CIL stripped so emit the data ourselves */
6846 if (header
->num_clauses
)
6847 encode_value (header
->num_clauses
, p
, &p
);
6849 for (k
= 0; k
< header
->num_clauses
; ++k
) {
6850 MonoExceptionClause
*clause
;
6852 clause
= &header
->clauses
[k
];
6854 encode_value (clause
->flags
, p
, &p
);
6855 if (!(clause
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
|| clause
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)) {
6856 if (clause
->data
.catch_class
) {
6860 buf2
= (guint8
*)g_malloc (4096);
6862 encode_klass_ref (acfg
, clause
->data
.catch_class
, p2
, &p2
);
6864 g_assert (len
< 4096);
6865 encode_value (len
, p
, &p
);
6866 memcpy (p
, buf2
, len
);
6870 encode_value (0, p
, &p
);
6874 /* Emit the IL ranges too, since they might not be available at runtime */
6875 encode_value (clause
->try_offset
, p
, &p
);
6876 encode_value (clause
->try_len
, p
, &p
);
6877 encode_value (clause
->handler_offset
, p
, &p
);
6878 encode_value (clause
->handler_len
, p
, &p
);
6880 /* Emit a list of nesting clauses */
6881 for (i
= 0; i
< header
->num_clauses
; ++i
) {
6883 MonoExceptionClause
*clause1
= &header
->clauses
[cindex1
];
6885 MonoExceptionClause
*clause2
= &header
->clauses
[cindex2
];
6887 if (cindex1
!= cindex2
&& clause1
->try_offset
>= clause2
->try_offset
&& clause1
->handler_offset
<= clause2
->handler_offset
)
6888 encode_value (i
, p
, &p
);
6890 encode_value (-1, p
, &p
);
6893 if (jinfo
->num_clauses
)
6894 encode_value (jinfo
->num_clauses
, p
, &p
);
6896 for (k
= 0; k
< jinfo
->num_clauses
; ++k
) {
6897 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[k
];
6899 encode_value (ei
->flags
, p
, &p
);
6900 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
6901 /* Not used for catch clauses */
6902 if (ei
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
)
6903 encode_value (ei
->exvar_offset
, p
, &p
);
6905 encode_value (ei
->exvar_offset
, p
, &p
);
6908 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
|| ei
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
6909 encode_value ((gint
)((guint8
*)ei
->data
.filter
- code
), p
, &p
);
6911 if (ei
->data
.catch_class
) {
6915 buf2
= (guint8
*)g_malloc (4096);
6917 encode_klass_ref (acfg
, ei
->data
.catch_class
, p2
, &p2
);
6919 g_assert (len
< 4096);
6920 encode_value (len
, p
, &p
);
6921 memcpy (p
, buf2
, len
);
6925 encode_value (0, p
, &p
);
6929 encode_value ((gint
)((guint8
*)ei
->try_start
- code
), p
, &p
);
6930 encode_value ((gint
)((guint8
*)ei
->try_end
- code
), p
, &p
);
6931 encode_value ((gint
)((guint8
*)ei
->handler_start
- code
), p
, &p
);
6935 if (jinfo
->has_try_block_holes
) {
6936 MonoTryBlockHoleTableJitInfo
*table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
6937 for (i
= 0; i
< table
->num_holes
; ++i
) {
6938 MonoTryBlockHoleJitInfo
*hole
= &table
->holes
[i
];
6939 encode_value (hole
->clause
, p
, &p
);
6940 encode_value (hole
->length
, p
, &p
);
6941 encode_value (hole
->offset
, p
, &p
);
6945 if (jinfo
->has_arch_eh_info
) {
6946 MonoArchEHJitInfo
*eh_info
;
6948 eh_info
= mono_jit_info_get_arch_eh_info (jinfo
);
6949 encode_value (eh_info
->stack_size
, p
, &p
);
6950 encode_value (eh_info
->epilog_size
, p
, &p
);
6953 if (jinfo
->has_generic_jit_info
) {
6954 MonoGenericJitInfo
*gi
= mono_jit_info_get_generic_jit_info (jinfo
);
6955 MonoGenericSharingContext
* gsctx
= gi
->generic_sharing_context
;
6959 encode_value (gi
->nlocs
, p
, &p
);
6961 for (i
= 0; i
< gi
->nlocs
; ++i
) {
6962 MonoDwarfLocListEntry
*entry
= &gi
->locations
[i
];
6964 encode_value (entry
->is_reg
? 1 : 0, p
, &p
);
6965 encode_value (entry
->reg
, p
, &p
);
6967 encode_value (entry
->offset
, p
, &p
);
6969 g_assert (entry
->from
== 0);
6971 encode_value (entry
->from
, p
, &p
);
6972 encode_value (entry
->to
, p
, &p
);
6975 if (!cfg
->compile_llvm
) {
6976 encode_value (gi
->has_this
? 1 : 0, p
, &p
);
6977 encode_value (gi
->this_reg
, p
, &p
);
6978 encode_value (gi
->this_offset
, p
, &p
);
6983 * Need to encode jinfo->method too, since it is not equal to 'method'
6984 * when using generic sharing.
6986 buf2
= (guint8
*)g_malloc (4096);
6988 encode_method_ref (acfg
, jinfo
->d
.method
, p2
, &p2
);
6990 g_assert (len
< 4096);
6991 encode_value (len
, p
, &p
);
6992 memcpy (p
, buf2
, len
);
6996 if (gsctx
&& gsctx
->is_gsharedvt
) {
6997 encode_value (1, p
, &p
);
6999 encode_value (0, p
, &p
);
7003 if (seq_points_size
)
7004 p
+= mono_seq_point_info_write (seq_points
, p
);
7006 g_assert (debug_info_size
< buf_size
);
7008 encode_value (debug_info_size
, p
, &p
);
7009 if (debug_info_size
) {
7010 memcpy (p
, debug_info
, debug_info_size
);
7011 p
+= debug_info_size
;
7012 g_free (debug_info
);
7017 encode_value (cfg
->gc_map_size
, p
, &p
);
7018 /* The GC map requires 4 bytes of alignment */
7019 while ((gsize
)p
% 4)
7021 memcpy (p
, cfg
->gc_map
, cfg
->gc_map_size
);
7022 p
+= cfg
->gc_map_size
;
7025 acfg
->stats
.ex_info_size
+= p
- buf
;
7027 g_assert (p
- buf
< buf_size
);
7030 /* The GC Map requires 4 byte alignment */
7031 cfg
->ex_info_offset
= add_to_blob_aligned (acfg
, buf
, p
- buf
, cfg
->gc_map
? 4 : 1);
7036 emit_klass_info (MonoAotCompile
*acfg
, guint32 token
)
7039 MonoClass
*klass
= mono_class_get_checked (acfg
->image
, token
, error
);
7041 int i
, buf_size
, res
;
7042 gboolean no_special_static
, cant_encode
;
7043 gpointer iter
= NULL
;
7046 mono_error_cleanup (error
);
7050 p
= buf
= (guint8
*)g_malloc (buf_size
);
7052 /* Mark as unusable */
7053 encode_value (-1, p
, &p
);
7055 res
= add_to_blob (acfg
, buf
, p
- buf
);
7061 buf_size
= 10240 + (m_class_get_vtable_size (klass
) * 16);
7062 p
= buf
= (guint8
*)g_malloc (buf_size
);
7066 mono_class_init_internal (klass
);
7068 mono_class_get_nested_types (klass
, &iter
);
7069 g_assert (m_class_is_nested_classes_inited (klass
));
7071 mono_class_setup_vtable (klass
);
7074 * Emit all the information which is required for creating vtables so
7075 * the runtime does not need to create the MonoMethod structures which
7076 * take up a lot of space.
7079 no_special_static
= !mono_class_has_special_static_fields (klass
);
7081 /* Check whenever we have enough info to encode the vtable */
7082 cant_encode
= FALSE
;
7083 MonoMethod
**klass_vtable
= m_class_get_vtable (klass
);
7084 for (i
= 0; i
< m_class_get_vtable_size (klass
); ++i
) {
7085 MonoMethod
*cm
= klass_vtable
[i
];
7087 if (cm
&& mono_method_signature_internal (cm
)->is_inflated
&& !g_hash_table_lookup (acfg
->token_info_hash
, cm
))
7091 mono_class_has_finalizer (klass
);
7092 if (mono_class_has_failure (klass
))
7095 if (mono_class_is_gtd (klass
) || cant_encode
) {
7096 encode_value (-1, p
, &p
);
7098 gboolean has_nested
= mono_class_get_nested_classes_property (klass
) != NULL
;
7099 encode_value (m_class_get_vtable_size (klass
), p
, &p
);
7100 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
);
7101 if (m_class_has_cctor (klass
))
7102 encode_method_ref (acfg
, mono_class_get_cctor (klass
), p
, &p
);
7103 if (m_class_has_finalize (klass
))
7104 encode_method_ref (acfg
, mono_class_get_finalizer (klass
), p
, &p
);
7106 encode_value (m_class_get_instance_size (klass
), p
, &p
);
7107 encode_value (mono_class_data_size (klass
), p
, &p
);
7108 encode_value (m_class_get_packing_size (klass
), p
, &p
);
7109 encode_value (m_class_get_min_align (klass
), p
, &p
);
7111 for (i
= 0; i
< m_class_get_vtable_size (klass
); ++i
) {
7112 MonoMethod
*cm
= klass_vtable
[i
];
7115 encode_method_ref (acfg
, cm
, p
, &p
);
7117 encode_value (0, p
, &p
);
7121 acfg
->stats
.class_info_size
+= p
- buf
;
7123 g_assert (p
- buf
< buf_size
);
7124 res
= add_to_blob (acfg
, buf
, p
- buf
);
7131 get_plt_entry_debug_sym (MonoAotCompile
*acfg
, MonoJumpInfo
*ji
, GHashTable
*cache
)
7133 char *debug_sym
= NULL
;
7136 if (acfg
->llvm
&& llvm_acfg
->aot_opts
.static_link
) {
7137 /* Need to add a prefix to create unique symbols */
7138 prefix
= g_strdup_printf ("plt_%s_", acfg
->assembly_name_sym
);
7140 #if defined(TARGET_WIN32) && defined(TARGET_X86)
7141 prefix
= mangle_symbol_alloc ("plt_");
7143 prefix
= g_strdup ("plt_");
7148 case MONO_PATCH_INFO_METHOD
:
7149 debug_sym
= get_debug_sym (ji
->data
.method
, prefix
, cache
);
7151 case MONO_PATCH_INFO_JIT_ICALL_ID
:
7152 debug_sym
= g_strdup_printf ("%s_jit_icall_%s", prefix
, mono_find_jit_icall_info (ji
->data
.jit_icall_id
)->name
);
7154 case MONO_PATCH_INFO_RGCTX_FETCH
:
7155 debug_sym
= g_strdup_printf ("%s_rgctx_fetch_%d", prefix
, acfg
->label_generator
++);
7157 case MONO_PATCH_INFO_ICALL_ADDR
:
7158 case MONO_PATCH_INFO_ICALL_ADDR_CALL
: {
7159 char *s
= get_debug_sym (ji
->data
.method
, "", cache
);
7161 debug_sym
= g_strdup_printf ("%s_icall_native_%s", prefix
, s
);
7165 case MONO_PATCH_INFO_SPECIFIC_TRAMPOLINE_LAZY_FETCH_ADDR
:
7166 debug_sym
= g_strdup_printf ("%s_jit_icall_native_specific_trampoline_lazy_fetch_%lu", prefix
, (gulong
)ji
->data
.uindex
);
7168 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
7169 debug_sym
= g_strdup_printf ("%s_jit_icall_native_%s", prefix
, mono_find_jit_icall_info (ji
->data
.jit_icall_id
)->name
);
7177 return sanitize_symbol (acfg
, debug_sym
);
7181 * Calls made from AOTed code are routed through a table of jumps similar to the
7182 * ELF PLT (Program Linkage Table). Initially the PLT entries jump to code which transfers
7183 * control to the AOT runtime through a trampoline.
7186 emit_plt (MonoAotCompile
*acfg
)
7190 if (acfg
->aot_opts
.llvm_only
) {
7191 g_assert (acfg
->plt_offset
== 1);
7197 emit_section_change (acfg
, ".text", 0);
7198 emit_alignment_code (acfg
, 16);
7199 emit_info_symbol (acfg
, "plt", TRUE
);
7200 emit_label (acfg
, acfg
->plt_symbol
);
7202 for (i
= 0; i
< acfg
->plt_offset
; ++i
) {
7203 char *debug_sym
= NULL
;
7204 MonoPltEntry
*plt_entry
= NULL
;
7208 * The first plt entry is unused.
7212 plt_entry
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
7214 debug_sym
= plt_entry
->debug_sym
;
7216 if (acfg
->thumb_mixed
&& !plt_entry
->jit_used
)
7217 /* Emit only a thumb version */
7220 /* Skip plt entries not actually called */
7221 if (!plt_entry
->jit_used
&& !plt_entry
->llvm_used
)
7224 if (acfg
->llvm
&& !acfg
->thumb_mixed
) {
7225 emit_label (acfg
, plt_entry
->llvm_symbol
);
7227 emit_global_inner (acfg
, plt_entry
->llvm_symbol
, TRUE
);
7228 #if defined(TARGET_MACH)
7229 fprintf (acfg
->fp
, ".private_extern %s\n", plt_entry
->llvm_symbol
);
7235 if (acfg
->need_no_dead_strip
) {
7236 emit_unset_mode (acfg
);
7237 fprintf (acfg
->fp
, " .no_dead_strip %s\n", debug_sym
);
7239 emit_local_symbol (acfg
, debug_sym
, NULL
, TRUE
);
7240 emit_label (acfg
, debug_sym
);
7243 emit_label (acfg
, plt_entry
->symbol
);
7245 arch_emit_plt_entry (acfg
, acfg
->got_symbol
, (acfg
->plt_got_offset_base
+ i
) * sizeof (target_mgreg_t
), acfg
->plt_got_info_offsets
[i
]);
7248 emit_symbol_size (acfg
, debug_sym
, ".");
7251 if (acfg
->thumb_mixed
) {
7252 /* Make sure the ARM symbols don't alias the thumb ones */
7253 emit_zero_bytes (acfg
, 16);
7256 * Emit a separate set of PLT entries using thumb2 which is called by LLVM generated
7259 for (i
= 0; i
< acfg
->plt_offset
; ++i
) {
7260 char *debug_sym
= NULL
;
7261 MonoPltEntry
*plt_entry
= NULL
;
7266 plt_entry
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
7268 /* Skip plt entries not actually called by LLVM code */
7269 if (!plt_entry
->llvm_used
)
7272 if (acfg
->aot_opts
.write_symbols
) {
7273 if (plt_entry
->debug_sym
)
7274 debug_sym
= g_strdup_printf ("%s_thumb", plt_entry
->debug_sym
);
7278 #if defined(TARGET_MACH)
7279 fprintf (acfg
->fp
, " .thumb_func %s\n", debug_sym
);
7280 fprintf (acfg
->fp
, " .no_dead_strip %s\n", debug_sym
);
7282 emit_local_symbol (acfg
, debug_sym
, NULL
, TRUE
);
7283 emit_label (acfg
, debug_sym
);
7285 fprintf (acfg
->fp
, "\n.thumb_func\n");
7287 emit_label (acfg
, plt_entry
->llvm_symbol
);
7290 emit_global_inner (acfg
, plt_entry
->llvm_symbol
, TRUE
);
7292 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
]);
7295 emit_symbol_size (acfg
, debug_sym
, ".");
7301 emit_symbol_size (acfg
, acfg
->plt_symbol
, ".");
7303 emit_info_symbol (acfg
, "plt_end", TRUE
);
7305 arch_emit_unwind_info_sections (acfg
, "plt", "plt_end", NULL
);
7309 * emit_trampoline_full:
7311 * If EMIT_TINFO is TRUE, emit additional information which can be used to create a MonoJitInfo for this trampoline by
7312 * create_jit_info_for_trampoline ().
7314 static G_GNUC_UNUSED
void
7315 emit_trampoline_full (MonoAotCompile
*acfg
, MonoTrampInfo
*info
, gboolean emit_tinfo
)
7317 char start_symbol
[MAX_SYMBOL_SIZE
];
7318 char end_symbol
[MAX_SYMBOL_SIZE
];
7319 char symbol
[MAX_SYMBOL_SIZE
];
7320 guint32 buf_size
, info_offset
;
7321 MonoJumpInfo
*patch_info
;
7334 code_size
= info
->code_size
;
7336 unwind_ops
= info
->unwind_ops
;
7340 sprintf (start_symbol
, "%s%s", acfg
->user_symbol_prefix
, name
);
7342 emit_section_change (acfg
, ".text", 0);
7343 emit_global (acfg
, start_symbol
, TRUE
);
7344 emit_alignment_code (acfg
, AOT_FUNC_ALIGNMENT
);
7345 emit_label (acfg
, start_symbol
);
7347 sprintf (symbol
, "%snamed_%s", acfg
->temp_prefix
, name
);
7348 emit_label (acfg
, symbol
);
7351 * The code should access everything through the GOT, so we pass
7354 emit_and_reloc_code (acfg
, NULL
, code
, code_size
, ji
, TRUE
, NULL
);
7356 emit_symbol_size (acfg
, start_symbol
, ".");
7359 sprintf (end_symbol
, "%snamede_%s", acfg
->temp_prefix
, name
);
7360 emit_label (acfg
, end_symbol
);
7365 /* Sort relocations */
7366 patches
= g_ptr_array_new ();
7367 for (patch_info
= ji
; patch_info
; patch_info
= patch_info
->next
)
7368 if (patch_info
->type
!= MONO_PATCH_INFO_NONE
)
7369 g_ptr_array_add (patches
, patch_info
);
7370 g_ptr_array_sort (patches
, compare_patches
);
7372 buf_size
= patches
->len
* 128 + 128;
7373 buf
= (guint8
*)g_malloc (buf_size
);
7376 encode_patch_list (acfg
, patches
, patches
->len
, FALSE
, p
, &p
);
7377 g_assert (p
- buf
< buf_size
);
7378 g_ptr_array_free (patches
, TRUE
);
7380 sprintf (symbol
, "%s%s_p", acfg
->user_symbol_prefix
, name
);
7382 info_offset
= add_to_blob (acfg
, buf
, p
- buf
);
7384 emit_section_change (acfg
, RODATA_SECT
, 0);
7385 emit_global (acfg
, symbol
, FALSE
);
7386 emit_label (acfg
, symbol
);
7388 emit_int32 (acfg
, info_offset
);
7392 guint32 encoded_len
;
7396 * Emit additional information which can be used to reconstruct a partial MonoTrampInfo.
7398 encoded
= mono_unwind_ops_encode (info
->unwind_ops
, &encoded_len
);
7399 uw_offset
= get_unwind_info_offset (acfg
, encoded
, encoded_len
);
7402 emit_symbol_diff (acfg
, end_symbol
, start_symbol
, 0);
7403 emit_int32 (acfg
, uw_offset
);
7406 /* Emit debug info */
7408 char symbol2
[MAX_SYMBOL_SIZE
];
7410 sprintf (symbol
, "%s", name
);
7411 sprintf (symbol2
, "%snamed_%s", acfg
->temp_prefix
, name
);
7413 arch_emit_unwind_info_sections (acfg
, start_symbol
, end_symbol
, unwind_ops
);
7416 mono_dwarf_writer_emit_trampoline (acfg
->dwarf
, symbol
, symbol2
, NULL
, NULL
, code_size
, unwind_ops
);
7422 static G_GNUC_UNUSED
void
7423 emit_trampoline (MonoAotCompile
*acfg
, MonoTrampInfo
*info
)
7425 emit_trampoline_full (acfg
, info
, TRUE
);
7429 emit_trampolines (MonoAotCompile
*acfg
)
7431 char symbol
[MAX_SYMBOL_SIZE
];
7432 char end_symbol
[MAX_SYMBOL_SIZE
];
7433 int i
, tramp_got_offset
;
7435 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
7439 if (!mono_aot_mode_is_full (&acfg
->aot_opts
) && !mono_aot_mode_is_interp (&acfg
->aot_opts
))
7441 if (acfg
->aot_opts
.llvm_only
)
7444 g_assert (acfg
->image
->assembly
);
7446 /* Currently, we emit most trampolines into the mscorlib AOT image. */
7447 if (mono_is_corlib_image(acfg
->image
->assembly
->image
)) {
7448 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
7449 MonoTrampInfo
*info
;
7452 * Emit the generic trampolines.
7454 * We could save some code by treating the generic trampolines as a wrapper
7455 * method, but that approach has its own complexities, so we choose the simpler
7458 for (tramp_type
= 0; tramp_type
< MONO_TRAMPOLINE_NUM
; ++tramp_type
) {
7459 /* we overload the boolean here to indicate the slightly different trampoline needed, see mono_arch_create_generic_trampoline() */
7460 #ifdef DISABLE_REMOTING
7461 if (tramp_type
== MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
)
7464 mono_arch_create_generic_trampoline ((MonoTrampolineType
)tramp_type
, &info
, acfg
->aot_opts
.use_trampolines_page
? 2: TRUE
);
7465 emit_trampoline (acfg
, info
);
7466 mono_tramp_info_free (info
);
7469 /* Emit the exception related code pieces */
7470 mono_arch_get_restore_context (&info
, TRUE
);
7471 emit_trampoline (acfg
, info
);
7472 mono_tramp_info_free (info
);
7474 mono_arch_get_call_filter (&info
, TRUE
);
7475 emit_trampoline (acfg
, info
);
7476 mono_tramp_info_free (info
);
7478 mono_arch_get_throw_exception (&info
, TRUE
);
7479 emit_trampoline (acfg
, info
);
7480 mono_tramp_info_free (info
);
7482 mono_arch_get_rethrow_exception (&info
, TRUE
);
7483 emit_trampoline (acfg
, info
);
7484 mono_tramp_info_free (info
);
7486 mono_arch_get_rethrow_preserve_exception (&info
, TRUE
);
7487 emit_trampoline (acfg
, info
);
7488 mono_tramp_info_free (info
);
7490 mono_arch_get_throw_corlib_exception (&info
, TRUE
);
7491 emit_trampoline (acfg
, info
);
7492 mono_tramp_info_free (info
);
7494 #ifdef MONO_ARCH_HAVE_SDB_TRAMPOLINES
7495 mono_arch_create_sdb_trampoline (TRUE
, &info
, TRUE
);
7496 emit_trampoline (acfg
, info
);
7497 mono_tramp_info_free (info
);
7499 mono_arch_create_sdb_trampoline (FALSE
, &info
, TRUE
);
7500 emit_trampoline (acfg
, info
);
7501 mono_tramp_info_free (info
);
7504 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
7505 mono_arch_get_gsharedvt_trampoline (&info
, TRUE
);
7507 emit_trampoline_full (acfg
, info
, TRUE
);
7509 /* Create a separate out trampoline for more information in stack traces */
7510 info
->name
= g_strdup ("gsharedvt_out_trampoline");
7511 emit_trampoline_full (acfg
, info
, TRUE
);
7512 mono_tramp_info_free (info
);
7516 #if defined(MONO_ARCH_HAVE_GET_TRAMPOLINES)
7518 GSList
*l
= mono_arch_get_trampolines (TRUE
);
7521 MonoTrampInfo
*info
= (MonoTrampInfo
*)l
->data
;
7523 emit_trampoline (acfg
, info
);
7529 for (i
= 0; i
< acfg
->aot_opts
.nrgctx_fetch_trampolines
; ++i
) {
7532 offset
= MONO_RGCTX_SLOT_MAKE_RGCTX (i
);
7533 mono_arch_create_rgctx_lazy_fetch_trampoline (offset
, &info
, TRUE
);
7534 emit_trampoline (acfg
, info
);
7535 mono_tramp_info_free (info
);
7537 offset
= MONO_RGCTX_SLOT_MAKE_MRGCTX (i
);
7538 mono_arch_create_rgctx_lazy_fetch_trampoline (offset
, &info
, TRUE
);
7539 emit_trampoline (acfg
, info
);
7540 mono_tramp_info_free (info
);
7543 #ifdef MONO_ARCH_HAVE_GENERAL_RGCTX_LAZY_FETCH_TRAMPOLINE
7544 mono_arch_create_general_rgctx_lazy_fetch_trampoline (&info
, TRUE
);
7545 emit_trampoline (acfg
, info
);
7546 mono_tramp_info_free (info
);
7552 /* delegate_invoke_impl trampolines */
7553 l
= mono_arch_get_delegate_invoke_impls ();
7555 MonoTrampInfo
*info
= (MonoTrampInfo
*)l
->data
;
7557 emit_trampoline (acfg
, info
);
7562 if (mono_aot_mode_is_interp (&acfg
->aot_opts
) && mono_is_corlib_image (acfg
->image
->assembly
->image
)) {
7563 mono_arch_get_interp_to_native_trampoline (&info
);
7564 emit_trampoline (acfg
, info
);
7566 #ifdef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
7567 mono_arch_get_native_to_interp_trampoline (&info
);
7568 emit_trampoline (acfg
, info
);
7572 #endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
7574 /* Emit trampolines which are numerous */
7577 * These include the following:
7578 * - specific trampolines
7579 * - static rgctx invoke trampolines
7581 * These trampolines have the same code, they are parameterized by GOT
7583 * They are defined in this file, in the arch_... routines instead of
7584 * in tramp-<ARCH>.c, since it is easier to do it this way.
7588 * When running in aot-only mode, we can't create specific trampolines at
7589 * runtime, so we create a few, and save them in the AOT file.
7590 * Normal trampolines embed their argument as a literal inside the
7591 * trampoline code, we can't do that here, so instead we embed an offset
7592 * which needs to be added to the trampoline address to get the address of
7593 * the GOT slot which contains the argument value.
7594 * The generated trampolines jump to the generic trampolines using another
7595 * GOT slot, which will be setup by the AOT loader to point to the
7596 * generic trampoline code of the given type.
7600 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for
7604 emit_section_change (acfg
, ".text", 0);
7606 tramp_got_offset
= acfg
->got_offset
;
7608 for (ntype
= 0; ntype
< MONO_AOT_TRAMP_NUM
; ++ntype
) {
7610 case MONO_AOT_TRAMP_SPECIFIC
:
7611 sprintf (symbol
, "specific_trampolines");
7613 case MONO_AOT_TRAMP_STATIC_RGCTX
:
7614 sprintf (symbol
, "static_rgctx_trampolines");
7616 case MONO_AOT_TRAMP_IMT
:
7617 sprintf (symbol
, "imt_trampolines");
7619 case MONO_AOT_TRAMP_GSHAREDVT_ARG
:
7620 sprintf (symbol
, "gsharedvt_arg_trampolines");
7622 case MONO_AOT_TRAMP_FTNPTR_ARG
:
7623 sprintf (symbol
, "ftnptr_arg_trampolines");
7625 case MONO_AOT_TRAMP_UNBOX_ARBITRARY
:
7626 sprintf (symbol
, "unbox_arbitrary_trampolines");
7629 g_assert_not_reached ();
7632 sprintf (end_symbol
, "%s_e", symbol
);
7634 if (acfg
->aot_opts
.write_symbols
)
7635 emit_local_symbol (acfg
, symbol
, end_symbol
, TRUE
);
7637 emit_alignment_code (acfg
, AOT_FUNC_ALIGNMENT
);
7638 emit_info_symbol (acfg
, symbol
, TRUE
);
7640 acfg
->trampoline_got_offset_base
[ntype
] = tramp_got_offset
;
7642 for (i
= 0; i
< acfg
->num_trampolines
[ntype
]; ++i
) {
7646 case MONO_AOT_TRAMP_SPECIFIC
:
7647 arch_emit_specific_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7648 tramp_got_offset
+= 2;
7650 case MONO_AOT_TRAMP_STATIC_RGCTX
:
7651 arch_emit_static_rgctx_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7652 tramp_got_offset
+= 2;
7654 case MONO_AOT_TRAMP_IMT
:
7655 arch_emit_imt_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7656 tramp_got_offset
+= 1;
7658 case MONO_AOT_TRAMP_GSHAREDVT_ARG
:
7659 arch_emit_gsharedvt_arg_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7660 tramp_got_offset
+= 2;
7662 case MONO_AOT_TRAMP_FTNPTR_ARG
:
7663 arch_emit_ftnptr_arg_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7664 tramp_got_offset
+= 2;
7666 case MONO_AOT_TRAMP_UNBOX_ARBITRARY
:
7667 arch_emit_unbox_arbitrary_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
7668 tramp_got_offset
+= 1;
7671 g_assert_not_reached ();
7673 if (!acfg
->trampoline_size
[ntype
]) {
7674 g_assert (tramp_size
);
7675 acfg
->trampoline_size
[ntype
] = tramp_size
;
7679 emit_label (acfg
, end_symbol
);
7680 emit_int32 (acfg
, 0);
7683 arch_emit_specific_trampoline_pages (acfg
);
7685 /* Reserve some entries at the end of the GOT for our use */
7686 acfg
->num_trampoline_got_entries
= tramp_got_offset
- acfg
->got_offset
;
7689 acfg
->got_offset
+= acfg
->num_trampoline_got_entries
;
7693 str_begins_with (const char *str1
, const char *str2
)
7695 int len
= strlen (str2
);
7696 return strncmp (str1
, str2
, len
) == 0;
7700 mono_aot_readonly_field_override (MonoClassField
*field
)
7703 for (rdv
= readonly_values
; rdv
; rdv
= rdv
->next
) {
7704 char *p
= rdv
->name
;
7706 len
= strlen (m_class_get_name_space (field
->parent
));
7707 if (strncmp (p
, m_class_get_name_space (field
->parent
), len
))
7712 len
= strlen (m_class_get_name (field
->parent
));
7713 if (strncmp (p
, m_class_get_name (field
->parent
), len
))
7718 if (strcmp (p
, field
->name
))
7720 switch (rdv
->type
) {
7722 return &rdv
->value
.i1
;
7724 return &rdv
->value
.i2
;
7726 return &rdv
->value
.i4
;
7735 add_readonly_value (MonoAotOptions
*opts
, const char *val
)
7740 /* the format of val is:
7741 * namespace.typename.fieldname=type/value
7742 * type can be i1 for uint8/int8/boolean, i2 for uint16/int16/char, i4 for uint32/int32
7744 fval
= strrchr (val
, '/');
7746 fprintf (stderr
, "AOT : invalid format for readonly field '%s', missing /.\n", val
);
7749 tval
= strrchr (val
, '=');
7751 fprintf (stderr
, "AOT : invalid format for readonly field '%s', missing =.\n", val
);
7754 rdv
= g_new0 (ReadOnlyValue
, 1);
7755 rdv
->name
= (char *)g_malloc0 (tval
- val
+ 1);
7756 memcpy (rdv
->name
, val
, tval
- val
);
7759 if (strncmp (tval
, "i1", 2) == 0) {
7760 rdv
->value
.i1
= atoi (fval
);
7761 rdv
->type
= MONO_TYPE_I1
;
7762 } else if (strncmp (tval
, "i2", 2) == 0) {
7763 rdv
->value
.i2
= atoi (fval
);
7764 rdv
->type
= MONO_TYPE_I2
;
7765 } else if (strncmp (tval
, "i4", 2) == 0) {
7766 rdv
->value
.i4
= atoi (fval
);
7767 rdv
->type
= MONO_TYPE_I4
;
7769 fprintf (stderr
, "AOT : unsupported type for readonly field '%s'.\n", tval
);
7772 rdv
->next
= readonly_values
;
7773 readonly_values
= rdv
;
7777 clean_path (gchar
* path
)
7782 if (g_str_has_suffix (path
, G_DIR_SEPARATOR_S
))
7785 gchar
*clean
= g_strconcat (path
, G_DIR_SEPARATOR_S
, (const char*)NULL
);
7791 static const gchar
*
7792 wrap_path (const gchar
* path
)
7798 // If the string contains no spaces, just return the original string.
7799 if (strstr (path
, " ") == NULL
)
7802 // If the string is already wrapped in quotes, return it.
7803 len
= strlen (path
);
7804 if (len
>= 2 && path
[0] == '\"' && path
[len
-1] == '\"')
7807 // If the string contains spaces, then wrap it in quotes.
7808 gchar
*clean
= g_strdup_printf ("\"%s\"", path
);
7813 // Duplicate a char range and add it to a ptrarray, but only if it is nonempty
7815 ptr_array_add_range_if_nonempty(GPtrArray
*args
, gchar
const *start
, gchar
const *end
)
7817 ptrdiff_t len
= end
-start
;
7819 g_ptr_array_add (args
, g_strndup (start
, len
));
7823 mono_aot_split_options (const char *aot_options
)
7825 enum MonoAotOptionState
{
7826 MONO_AOT_OPTION_STATE_DEFAULT
,
7827 MONO_AOT_OPTION_STATE_STRING
,
7828 MONO_AOT_OPTION_STATE_ESCAPE
,
7831 GPtrArray
*args
= g_ptr_array_new ();
7832 enum MonoAotOptionState state
= MONO_AOT_OPTION_STATE_DEFAULT
;
7833 gchar
const *opt_start
= aot_options
;
7834 gboolean end_of_string
= FALSE
;
7837 g_return_val_if_fail (aot_options
!= NULL
, NULL
);
7839 while ((cur
= *aot_options
) != '\0') {
7840 if (state
== MONO_AOT_OPTION_STATE_ESCAPE
)
7845 // If we find a quote, then if we're in the default case then
7846 // it means we've found the start of a string, if not then it
7847 // means we've found the end of the string and should switch
7848 // back to the default case.
7850 case MONO_AOT_OPTION_STATE_DEFAULT
:
7851 state
= MONO_AOT_OPTION_STATE_STRING
;
7853 case MONO_AOT_OPTION_STATE_STRING
:
7854 state
= MONO_AOT_OPTION_STATE_DEFAULT
;
7856 case MONO_AOT_OPTION_STATE_ESCAPE
:
7857 g_assert_not_reached ();
7862 // If we've found an escaping operator, then this means we
7863 // should not process the next character if inside a string.
7864 if (state
== MONO_AOT_OPTION_STATE_STRING
)
7865 state
= MONO_AOT_OPTION_STATE_ESCAPE
;
7868 // If we're in the default state then this means we've found
7869 // an option, store it for later processing.
7870 if (state
== MONO_AOT_OPTION_STATE_DEFAULT
)
7878 // If the next character is end of string, then process the last option.
7879 if (*(aot_options
) == '\0') {
7880 end_of_string
= TRUE
;
7886 ptr_array_add_range_if_nonempty (args
, opt_start
, aot_options
);
7887 opt_start
= ++aot_options
;
7890 goto restart
; // Check for null and continue loop
7897 mono_aot_parse_options (const char *aot_options
, MonoAotOptions
*opts
)
7901 args
= mono_aot_split_options (aot_options
? aot_options
: "");
7902 for (int i
= 0; i
< args
->len
; ++i
) {
7903 const char *arg
= (const char *)g_ptr_array_index (args
, i
);
7905 if (str_begins_with (arg
, "outfile=")) {
7906 opts
->outfile
= g_strdup (arg
+ strlen ("outfile="));
7907 } else if (str_begins_with (arg
, "llvm-outfile=")) {
7908 opts
->llvm_outfile
= g_strdup (arg
+ strlen ("llvm-outfile="));
7909 } else if (str_begins_with (arg
, "temp-path=")) {
7910 opts
->temp_path
= clean_path (g_strdup (arg
+ strlen ("temp-path=")));
7911 } else if (str_begins_with (arg
, "save-temps")) {
7912 opts
->save_temps
= TRUE
;
7913 } else if (str_begins_with (arg
, "keep-temps")) {
7914 opts
->save_temps
= TRUE
;
7915 } else if (str_begins_with (arg
, "write-symbols")) {
7916 opts
->write_symbols
= TRUE
;
7917 } else if (str_begins_with (arg
, "no-write-symbols")) {
7918 opts
->write_symbols
= FALSE
;
7919 // Intentionally undocumented -- one-off experiment
7920 } else if (str_begins_with (arg
, "metadata-only")) {
7921 opts
->metadata_only
= TRUE
;
7922 } else if (str_begins_with (arg
, "bind-to-runtime-version")) {
7923 opts
->bind_to_runtime_version
= TRUE
;
7924 } else if (str_begins_with (arg
, "full")) {
7925 opts
->mode
= MONO_AOT_MODE_FULL
;
7926 } else if (str_begins_with (arg
, "hybrid")) {
7927 opts
->mode
= MONO_AOT_MODE_HYBRID
;
7928 } else if (str_begins_with (arg
, "interp")) {
7929 opts
->interp
= TRUE
;
7930 } else if (str_begins_with (arg
, "threads=")) {
7931 opts
->nthreads
= atoi (arg
+ strlen ("threads="));
7932 } else if (str_begins_with (arg
, "static")) {
7933 opts
->static_link
= TRUE
;
7934 opts
->no_dlsym
= TRUE
;
7935 } else if (str_begins_with (arg
, "asmonly")) {
7936 opts
->asm_only
= TRUE
;
7937 } else if (str_begins_with (arg
, "asmwriter")) {
7938 opts
->asm_writer
= TRUE
;
7939 } else if (str_begins_with (arg
, "nodebug")) {
7940 opts
->nodebug
= TRUE
;
7941 } else if (str_begins_with (arg
, "dwarfdebug")) {
7942 opts
->dwarf_debug
= TRUE
;
7943 // Intentionally undocumented -- No one remembers what this does. It appears to be ARM-only
7944 } else if (str_begins_with (arg
, "nopagetrampolines")) {
7945 opts
->use_trampolines_page
= FALSE
;
7946 } else if (str_begins_with (arg
, "ntrampolines=")) {
7947 opts
->ntrampolines
= atoi (arg
+ strlen ("ntrampolines="));
7948 } else if (str_begins_with (arg
, "nrgctx-trampolines=")) {
7949 opts
->nrgctx_trampolines
= atoi (arg
+ strlen ("nrgctx-trampolines="));
7950 } else if (str_begins_with (arg
, "nrgctx-fetch-trampolines=")) {
7951 opts
->nrgctx_fetch_trampolines
= atoi (arg
+ strlen ("nrgctx-fetch-trampolines="));
7952 } else if (str_begins_with (arg
, "nimt-trampolines=")) {
7953 opts
->nimt_trampolines
= atoi (arg
+ strlen ("nimt-trampolines="));
7954 } else if (str_begins_with (arg
, "ngsharedvt-trampolines=")) {
7955 opts
->ngsharedvt_arg_trampolines
= atoi (arg
+ strlen ("ngsharedvt-trampolines="));
7956 } else if (str_begins_with (arg
, "nftnptr-arg-trampolines=")) {
7957 opts
->nftnptr_arg_trampolines
= atoi (arg
+ strlen ("nftnptr-arg-trampolines="));
7958 } else if (str_begins_with (arg
, "nunbox-arbitrary-trampolines=")) {
7959 opts
->nunbox_arbitrary_trampolines
= atoi (arg
+ strlen ("unbox-arbitrary-trampolines="));
7960 } else if (str_begins_with (arg
, "tool-prefix=")) {
7961 opts
->tool_prefix
= g_strdup (arg
+ strlen ("tool-prefix="));
7962 } else if (str_begins_with (arg
, "ld-flags=")) {
7963 opts
->ld_flags
= g_strdup (arg
+ strlen ("ld-flags="));
7964 } else if (str_begins_with (arg
, "soft-debug")) {
7965 opts
->soft_debug
= TRUE
;
7966 // Intentionally undocumented x2-- deprecated
7967 } else if (str_begins_with (arg
, "gen-seq-points-file=")) {
7968 fprintf (stderr
, "Mono Warning: aot option gen-seq-points-file= is deprecated.\n");
7969 } else if (str_begins_with (arg
, "gen-seq-points-file")) {
7970 fprintf (stderr
, "Mono Warning: aot option gen-seq-points-file is deprecated.\n");
7971 } else if (str_begins_with (arg
, "msym-dir=")) {
7972 mini_debug_options
.no_seq_points_compact_data
= FALSE
;
7973 opts
->gen_msym_dir
= TRUE
;
7974 opts
->gen_msym_dir_path
= g_strdup (arg
+ strlen ("msym_dir="));
7975 } else if (str_begins_with (arg
, "direct-pinvoke")) {
7976 opts
->direct_pinvoke
= TRUE
;
7977 } else if (str_begins_with (arg
, "direct-icalls")) {
7978 opts
->direct_icalls
= TRUE
;
7979 } else if (str_begins_with (arg
, "no-direct-calls")) {
7980 opts
->no_direct_calls
= TRUE
;
7981 } else if (str_begins_with (arg
, "print-skipped")) {
7982 opts
->print_skipped_methods
= TRUE
;
7983 } else if (str_begins_with (arg
, "stats")) {
7985 // Intentionally undocumented-- has no known function other than to debug the compiler
7986 } else if (str_begins_with (arg
, "no-instances")) {
7987 opts
->no_instances
= TRUE
;
7988 // Intentionally undocumented x4-- Used for internal debugging of compiler
7989 } else if (str_begins_with (arg
, "log-generics")) {
7990 opts
->log_generics
= TRUE
;
7991 } else if (str_begins_with (arg
, "log-instances=")) {
7992 opts
->log_instances
= TRUE
;
7993 opts
->instances_logfile_path
= g_strdup (arg
+ strlen ("log-instances="));
7994 } else if (str_begins_with (arg
, "log-instances")) {
7995 opts
->log_instances
= TRUE
;
7996 } else if (str_begins_with (arg
, "internal-logfile=")) {
7997 opts
->logfile
= g_strdup (arg
+ strlen ("internal-logfile="));
7998 } else if (str_begins_with (arg
, "dedup-skip")) {
8000 } else if (str_begins_with (arg
, "dedup-include=")) {
8001 opts
->dedup_include
= g_strdup (arg
+ strlen ("dedup-include="));
8002 } else if (str_begins_with (arg
, "mtriple=")) {
8003 opts
->mtriple
= g_strdup (arg
+ strlen ("mtriple="));
8004 } else if (str_begins_with (arg
, "llvm-path=")) {
8005 opts
->llvm_path
= clean_path (g_strdup (arg
+ strlen ("llvm-path=")));
8006 } else if (!strcmp (arg
, "try-llvm")) {
8007 // If we can load LLVM, use it
8008 // Note: if you call this function from anywhere but mono_compile_assembly,
8009 // this will only set the try_llvm attribute and not do the probing / set the
8011 opts
->try_llvm
= TRUE
;
8012 } else if (!strcmp (arg
, "llvm")) {
8014 } else if (str_begins_with (arg
, "readonly-value=")) {
8015 add_readonly_value (opts
, arg
+ strlen ("readonly-value="));
8016 } else if (str_begins_with (arg
, "info")) {
8017 printf ("AOT target setup: %s.\n", AOT_TARGET_STR
);
8019 // Intentionally undocumented: Used for precise stack maps, which are not available yet
8020 } else if (str_begins_with (arg
, "gc-maps")) {
8021 mini_gc_enable_gc_maps_for_aot ();
8022 // Intentionally undocumented: Used for internal debugging
8023 } else if (str_begins_with (arg
, "dump")) {
8024 opts
->dump_json
= TRUE
;
8025 } else if (str_begins_with (arg
, "llvmonly")) {
8026 opts
->mode
= MONO_AOT_MODE_FULL
;
8028 opts
->llvm_only
= TRUE
;
8029 } else if (str_begins_with (arg
, "data-outfile=")) {
8030 opts
->data_outfile
= g_strdup (arg
+ strlen ("data-outfile="));
8031 } else if (str_begins_with (arg
, "profile=")) {
8032 opts
->profile_files
= g_list_append (opts
->profile_files
, g_strdup (arg
+ strlen ("profile=")));
8033 } else if (!strcmp (arg
, "profile-only")) {
8034 opts
->profile_only
= TRUE
;
8035 } else if (!strcmp (arg
, "verbose")) {
8036 opts
->verbose
= TRUE
;
8037 } else if (str_begins_with (arg
, "llvmopts=")){
8038 if (opts
->llvm_opts
) {
8039 char *s
= g_strdup_printf ("%s %s", opts
->llvm_opts
, arg
+ strlen ("llvmopts="));
8040 g_free (opts
->llvm_opts
);
8041 opts
->llvm_opts
= s
;
8043 opts
->llvm_opts
= g_strdup (arg
+ strlen ("llvmopts="));
8045 } else if (str_begins_with (arg
, "llvmllc=")){
8046 opts
->llvm_llc
= g_strdup (arg
+ strlen ("llvmllc="));
8047 } else if (!strcmp (arg
, "deterministic")) {
8048 opts
->deterministic
= TRUE
;
8049 } else if (!strcmp (arg
, "no-opt")) {
8050 opts
->no_opt
= TRUE
;
8051 } else if (str_begins_with (arg
, "clangxx=")) {
8052 opts
->clangxx
= g_strdup (arg
+ strlen ("clangxx="));
8053 } else if (str_begins_with (arg
, "depfile=")) {
8054 opts
->depfile
= g_strdup (arg
+ strlen ("depfile="));
8055 } else if (str_begins_with (arg
, "help") || str_begins_with (arg
, "?")) {
8056 printf ("Supported options for --aot:\n");
8057 printf (" asmonly\n");
8058 printf (" bind-to-runtime-version\n");
8059 printf (" bitcode\n");
8060 printf (" data-outfile=\n");
8061 printf (" direct-icalls\n");
8062 printf (" direct-pinvoke\n");
8063 printf (" dwarfdebug\n");
8065 printf (" hybrid\n");
8067 printf (" keep-temps\n");
8069 printf (" llvmonly\n");
8070 printf (" llvm-outfile=\n");
8071 printf (" llvm-path=\n");
8072 printf (" msym-dir=\n");
8073 printf (" mtriple\n");
8074 printf (" nimt-trampolines=\n");
8075 printf (" nodebug\n");
8076 printf (" no-direct-calls\n");
8077 printf (" no-write-symbols\n");
8078 printf (" nrgctx-trampolines=\n");
8079 printf (" nrgctx-fetch-trampolines=\n");
8080 printf (" ngsharedvt-trampolines=\n");
8081 printf (" nftnptr-arg-trampolines=\n");
8082 printf (" nunbox-arbitrary-trampolines=\n");
8083 printf (" ntrampolines=\n");
8084 printf (" outfile=\n");
8085 printf (" profile=\n");
8086 printf (" profile-only\n");
8087 printf (" print-skipped-methods\n");
8088 printf (" readonly-value=\n");
8089 printf (" save-temps\n");
8090 printf (" soft-debug\n");
8091 printf (" static\n");
8092 printf (" stats\n");
8093 printf (" temp-path=\n");
8094 printf (" tool-prefix=\n");
8095 printf (" threads=\n");
8096 printf (" write-symbols\n");
8097 printf (" verbose\n");
8098 printf (" no-opt\n");
8099 printf (" llvmopts=\n");
8100 printf (" llvmllc=\n");
8101 printf (" clangxx=\n");
8102 printf (" depfile=\n");
8103 printf (" help/?\n");
8106 fprintf (stderr
, "AOT : Unknown argument '%s'.\n", arg
);
8110 g_free ((gpointer
) arg
);
8113 if (opts
->use_trampolines_page
) {
8114 opts
->ntrampolines
= 0;
8115 opts
->nrgctx_trampolines
= 0;
8116 opts
->nimt_trampolines
= 0;
8117 opts
->ngsharedvt_arg_trampolines
= 0;
8118 opts
->nftnptr_arg_trampolines
= 0;
8119 opts
->nunbox_arbitrary_trampolines
= 0;
8122 g_ptr_array_free (args
, /*free_seg=*/TRUE
);
8126 add_token_info_hash (gpointer key
, gpointer value
, gpointer user_data
)
8128 MonoMethod
*method
= (MonoMethod
*)key
;
8129 MonoJumpInfoToken
*ji
= (MonoJumpInfoToken
*)value
;
8130 MonoAotCompile
*acfg
= (MonoAotCompile
*)user_data
;
8131 MonoJumpInfoToken
*new_ji
;
8133 new_ji
= (MonoJumpInfoToken
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfoToken
));
8134 new_ji
->image
= ji
->image
;
8135 new_ji
->token
= ji
->token
;
8136 g_hash_table_insert (acfg
->token_info_hash
, method
, new_ji
);
8140 can_encode_class (MonoAotCompile
*acfg
, MonoClass
*klass
)
8142 if (m_class_get_type_token (klass
))
8144 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
))
8146 if (m_class_get_rank (klass
))
8147 return can_encode_class (acfg
, m_class_get_element_class (klass
));
8152 can_encode_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
8154 if (method
->wrapper_type
) {
8155 switch (method
->wrapper_type
) {
8156 case MONO_WRAPPER_NONE
:
8157 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
8158 case MONO_WRAPPER_XDOMAIN_INVOKE
:
8159 case MONO_WRAPPER_STFLD
:
8160 case MONO_WRAPPER_LDFLD
:
8161 case MONO_WRAPPER_LDFLDA
:
8162 case MONO_WRAPPER_STELEMREF
:
8163 case MONO_WRAPPER_PROXY_ISINST
:
8164 case MONO_WRAPPER_ALLOC
:
8165 case MONO_WRAPPER_REMOTING_INVOKE
:
8166 case MONO_WRAPPER_OTHER
:
8167 case MONO_WRAPPER_WRITE_BARRIER
:
8168 case MONO_WRAPPER_DELEGATE_INVOKE
:
8169 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
8170 case MONO_WRAPPER_DELEGATE_END_INVOKE
:
8171 case MONO_WRAPPER_SYNCHRONIZED
:
8172 case MONO_WRAPPER_MANAGED_TO_NATIVE
:
8174 case MONO_WRAPPER_MANAGED_TO_MANAGED
:
8175 case MONO_WRAPPER_CASTCLASS
: {
8176 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
8185 //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
8189 if (!method
->token
) {
8190 /* The method is part of a constructed type like Int[,].Set (). */
8191 if (!g_hash_table_lookup (acfg
->token_info_hash
, method
)) {
8192 if (m_class_get_rank (method
->klass
))
8202 can_encode_patch (MonoAotCompile
*acfg
, MonoJumpInfo
*patch_info
)
8204 switch (patch_info
->type
) {
8205 case MONO_PATCH_INFO_METHOD
:
8206 case MONO_PATCH_INFO_METHOD_FTNDESC
:
8207 case MONO_PATCH_INFO_METHODCONST
:
8208 case MONO_PATCH_INFO_METHOD_CODE_SLOT
: {
8209 MonoMethod
*method
= patch_info
->data
.method
;
8211 return can_encode_method (acfg
, method
);
8213 case MONO_PATCH_INFO_VTABLE
:
8214 case MONO_PATCH_INFO_CLASS
:
8215 case MONO_PATCH_INFO_IID
:
8216 case MONO_PATCH_INFO_ADJUSTED_IID
:
8217 if (!can_encode_class (acfg
, patch_info
->data
.klass
)) {
8218 //printf ("Skip: %s\n", mono_type_full_name (m_class_get_byval_arg (patch_info->data.klass)));
8222 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
: {
8223 if (!can_encode_class (acfg
, patch_info
->data
.del_tramp
->klass
)) {
8224 //printf ("Skip: %s\n", mono_type_full_name (m_class_get_byval_arg (patch_info->data.klass)));
8229 case MONO_PATCH_INFO_RGCTX_FETCH
:
8230 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
8231 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
8233 if (entry
->in_mrgctx
) {
8234 if (!can_encode_method (acfg
, entry
->d
.method
))
8237 if (!can_encode_class (acfg
, entry
->d
.klass
))
8240 if (!can_encode_patch (acfg
, entry
->data
))
8252 is_concrete_type (MonoType
*t
)
8257 if (t
->type
== MONO_TYPE_VAR
|| t
->type
== MONO_TYPE_MVAR
)
8259 if (t
->type
== MONO_TYPE_GENERICINST
) {
8260 MonoGenericContext
*orig_ctx
;
8261 MonoGenericInst
*inst
;
8264 if (!MONO_TYPE_ISSTRUCT (t
))
8266 klass
= mono_class_from_mono_type_internal (t
);
8267 orig_ctx
= &mono_class_get_generic_class (klass
)->context
;
8269 inst
= orig_ctx
->class_inst
;
8271 for (i
= 0; i
< inst
->type_argc
; ++i
) {
8272 arg
= mini_get_underlying_type (inst
->type_argv
[i
]);
8273 if (!is_concrete_type (arg
))
8277 inst
= orig_ctx
->method_inst
;
8279 for (i
= 0; i
< inst
->type_argc
; ++i
) {
8280 arg
= mini_get_underlying_type (inst
->type_argv
[i
]);
8281 if (!is_concrete_type (arg
))
8289 static MonoMethodSignature
*
8290 get_concrete_sig (MonoMethodSignature
*sig
)
8292 gboolean concrete
= TRUE
;
8294 if (!sig
->has_type_parameters
)
8297 /* For signatures created during generic sharing, convert them to a concrete signature if possible */
8298 MonoMethodSignature
*copy
= mono_metadata_signature_dup (sig
);
8301 //printf ("%s\n", mono_signature_full_name (sig));
8303 if (sig
->ret
->byref
)
8304 copy
->ret
= m_class_get_this_arg (mono_defaults
.int_class
);
8306 copy
->ret
= mini_get_underlying_type (sig
->ret
);
8307 if (!is_concrete_type (copy
->ret
))
8309 for (i
= 0; i
< sig
->param_count
; ++i
) {
8310 if (sig
->params
[i
]->byref
) {
8311 MonoType
*t
= m_class_get_byval_arg (mono_class_from_mono_type_internal (sig
->params
[i
]));
8312 t
= mini_get_underlying_type (t
);
8313 copy
->params
[i
] = m_class_get_this_arg (mono_class_from_mono_type_internal (t
));
8315 copy
->params
[i
] = mini_get_underlying_type (sig
->params
[i
]);
8317 if (!is_concrete_type (copy
->params
[i
]))
8320 copy
->has_type_parameters
= 0;
8326 /* LOCKING: Assumes the loader lock is held */
8328 add_gsharedvt_wrappers (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
, gboolean gsharedvt_in
, gboolean gsharedvt_out
, gboolean interp_in
)
8330 MonoMethod
*wrapper
;
8331 gboolean add_in
= gsharedvt_in
;
8332 gboolean add_out
= gsharedvt_out
;
8334 if (gsharedvt_in
&& g_hash_table_lookup (acfg
->gsharedvt_in_signatures
, sig
))
8336 if (gsharedvt_out
&& g_hash_table_lookup (acfg
->gsharedvt_out_signatures
, sig
))
8339 if (!add_in
&& !add_out
)
8342 if (mini_is_gsharedvt_variable_signature (sig
))
8346 g_hash_table_insert (acfg
->gsharedvt_in_signatures
, sig
, sig
);
8348 g_hash_table_insert (acfg
->gsharedvt_out_signatures
, sig
, sig
);
8350 sig
= get_concrete_sig (sig
);
8353 //printf ("%s\n", mono_signature_full_name (sig));
8356 wrapper
= mini_get_gsharedvt_in_sig_wrapper (sig
);
8357 add_extra_method (acfg
, wrapper
);
8359 if (gsharedvt_out
) {
8360 wrapper
= mini_get_gsharedvt_out_sig_wrapper (sig
);
8361 add_extra_method (acfg
, wrapper
);
8364 #ifndef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
8366 wrapper
= mini_get_interp_in_wrapper (sig
);
8367 add_extra_method (acfg
, wrapper
);
8368 //printf ("X: %s\n", mono_method_full_name (wrapper, 1));
8376 * AOT compile a given method.
8377 * This function might be called by multiple threads, so it must be thread-safe.
8380 compile_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
8383 MonoJumpInfo
*patch_info
;
8386 MonoMethod
*wrapped
;
8387 gint64 jit_time_start
;
8390 if (acfg
->aot_opts
.metadata_only
)
8393 mono_acfg_lock (acfg
);
8394 index
= get_method_index (acfg
, method
);
8395 mono_acfg_unlock (acfg
);
8397 /* fixme: maybe we can also precompile wrapper methods */
8398 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
8399 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
8400 (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)) {
8401 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
8405 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
8408 wrapped
= mono_marshal_method_from_wrapper (method
);
8409 if (wrapped
&& (wrapped
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) && wrapped
->is_generic
)
8410 // FIXME: The wrapper should be generic too, but it is not
8413 if (method
->wrapper_type
== MONO_WRAPPER_COMINTEROP
)
8416 if (acfg
->aot_opts
.profile_only
&& !method
->is_inflated
&& !g_hash_table_lookup (acfg
->profile_methods
, method
))
8419 mono_atomic_inc_i32 (&acfg
->stats
.mcount
);
8422 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
)) {
8423 mono_atomic_inc_i32 (&acfg
->stats
.genericcount
);
8428 //acfg->aot_opts.print_skipped_methods = TRUE;
8431 * Since these methods are the only ones which are compiled with
8432 * AOT support, and they are not used by runtime startup/shutdown code,
8433 * the runtime will not see AOT methods during AOT compilation,so it
8434 * does not need to support them by creating a fake GOT etc.
8436 flags
= JIT_FLAG_AOT
;
8437 if (mono_aot_mode_is_full (&acfg
->aot_opts
))
8438 flags
= (JitFlags
)(flags
| JIT_FLAG_FULL_AOT
);
8440 flags
= (JitFlags
)(flags
| JIT_FLAG_LLVM
);
8441 if (acfg
->aot_opts
.llvm_only
)
8442 flags
= (JitFlags
)(flags
| JIT_FLAG_LLVM_ONLY
| JIT_FLAG_EXPLICIT_NULL_CHECKS
);
8443 if (acfg
->aot_opts
.no_direct_calls
)
8444 flags
= (JitFlags
)(flags
| JIT_FLAG_NO_DIRECT_ICALLS
);
8445 if (acfg
->aot_opts
.direct_pinvoke
)
8446 flags
= (JitFlags
)(flags
| JIT_FLAG_DIRECT_PINVOKE
);
8447 if (acfg
->aot_opts
.interp
)
8448 flags
= (JitFlags
)(flags
| JIT_FLAG_INTERP
);
8450 jit_time_start
= mono_time_track_start ();
8451 cfg
= mini_method_compile (method
, acfg
->opts
, mono_get_root_domain (), flags
, 0, index
);
8452 mono_time_track_end (&mono_jit_stats
.jit_time
, jit_time_start
);
8454 if (cfg
->exception_type
== MONO_EXCEPTION_GENERIC_SHARING_FAILED
) {
8455 if (acfg
->aot_opts
.print_skipped_methods
)
8456 printf ("Skip (gshared failure): %s (%s)\n", mono_method_get_full_name (method
), cfg
->exception_message
);
8457 mono_atomic_inc_i32 (&acfg
->stats
.genericcount
);
8460 if (cfg
->exception_type
!= MONO_EXCEPTION_NONE
) {
8461 /* Some instances cannot be JITted due to constraints etc. */
8462 if (!method
->is_inflated
)
8463 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
));
8464 /* Let the exception happen at runtime */
8468 if (cfg
->disable_aot
) {
8469 if (acfg
->aot_opts
.print_skipped_methods
)
8470 printf ("Skip (disabled): %s\n", mono_method_get_full_name (method
));
8471 mono_atomic_inc_i32 (&acfg
->stats
.ocount
);
8474 cfg
->method_index
= index
;
8476 /* Nullify patches which need no aot processing */
8477 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8478 switch (patch_info
->type
) {
8479 case MONO_PATCH_INFO_LABEL
:
8480 case MONO_PATCH_INFO_BB
:
8481 patch_info
->type
= MONO_PATCH_INFO_NONE
;
8488 /* Collect method->token associations from the cfg */
8489 mono_acfg_lock (acfg
);
8490 g_hash_table_foreach (cfg
->token_info_hash
, add_token_info_hash
, acfg
);
8491 mono_acfg_unlock (acfg
);
8492 g_hash_table_destroy (cfg
->token_info_hash
);
8493 cfg
->token_info_hash
= NULL
;
8496 * Check for absolute addresses.
8499 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8500 switch (patch_info
->type
) {
8501 case MONO_PATCH_INFO_ABS
:
8502 /* unable to handle this */
8511 if (acfg
->aot_opts
.print_skipped_methods
)
8512 printf ("Skip (abs call): %s\n", mono_method_get_full_name (method
));
8513 mono_atomic_inc_i32 (&acfg
->stats
.abscount
);
8517 /* Lock for the rest of the code */
8518 mono_acfg_lock (acfg
);
8521 acfg
->stats
.method_categories
[METHOD_CAT_GSHAREDVT
] ++;
8522 else if (cfg
->gshared
)
8523 acfg
->stats
.method_categories
[METHOD_CAT_INST
] ++;
8524 else if (cfg
->method
->wrapper_type
)
8525 acfg
->stats
.method_categories
[METHOD_CAT_WRAPPER
] ++;
8527 acfg
->stats
.method_categories
[METHOD_CAT_NORMAL
] ++;
8530 * Check for methods/klasses we can't encode.
8533 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8534 if (!can_encode_patch (acfg
, patch_info
))
8539 if (acfg
->aot_opts
.print_skipped_methods
)
8540 printf ("Skip (patches): %s\n", mono_method_get_full_name (method
));
8541 acfg
->stats
.ocount
++;
8542 mono_acfg_unlock (acfg
);
8546 if (!cfg
->compile_llvm
)
8547 acfg
->has_jitted_code
= TRUE
;
8549 if (method
->is_inflated
&& acfg
->aot_opts
.log_instances
) {
8550 if (acfg
->instances_logfile
)
8551 fprintf (acfg
->instances_logfile
, "%s ### %d\n", mono_method_get_full_name (method
), cfg
->code_size
);
8553 printf ("%s ### %d\n", mono_method_get_full_name (method
), cfg
->code_size
);
8556 /* Adds generic instances referenced by this method */
8558 * The depth is used to avoid infinite loops when generic virtual recursion is
8561 depth
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_depth
, method
));
8562 if (!acfg
->aot_opts
.no_instances
&& depth
< 32 && (mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))) {
8563 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8564 switch (patch_info
->type
) {
8565 case MONO_PATCH_INFO_RGCTX_FETCH
:
8566 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
:
8567 case MONO_PATCH_INFO_METHOD
:
8568 case MONO_PATCH_INFO_METHOD_FTNDESC
:
8569 case MONO_PATCH_INFO_METHOD_RGCTX
: {
8570 MonoMethod
*m
= NULL
;
8572 if (patch_info
->type
== MONO_PATCH_INFO_RGCTX_FETCH
|| patch_info
->type
== MONO_PATCH_INFO_RGCTX_SLOT_INDEX
) {
8573 MonoJumpInfoRgctxEntry
*e
= patch_info
->data
.rgctx_entry
;
8575 if (e
->info_type
== MONO_RGCTX_INFO_GENERIC_METHOD_CODE
|| e
->info_type
== MONO_RGCTX_INFO_METHOD_FTNDESC
)
8576 m
= e
->data
->data
.method
;
8578 m
= patch_info
->data
.method
;
8583 if (m
->is_inflated
&& (mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))) {
8584 if (!(mono_class_generic_sharing_enabled (m
->klass
) &&
8585 mono_method_is_generic_sharable_full (m
, FALSE
, FALSE
, FALSE
)) &&
8586 (!method_has_type_vars (m
) || mono_method_is_generic_sharable_full (m
, TRUE
, TRUE
, FALSE
))) {
8587 if (m
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
8588 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && !method_has_type_vars (m
))
8589 add_extra_method_with_depth (acfg
, mono_marshal_get_native_wrapper (m
, TRUE
, TRUE
), depth
+ 1);
8591 add_extra_method_with_depth (acfg
, m
, depth
+ 1);
8592 add_types_from_method_header (acfg
, m
);
8595 add_generic_class_with_depth (acfg
, m
->klass
, depth
+ 5, "method");
8597 if (m
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_MANAGED
) {
8598 WrapperInfo
*info
= mono_marshal_get_wrapper_info (m
);
8600 if (info
&& info
->subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
)
8601 add_extra_method_with_depth (acfg
, m
, depth
+ 1);
8605 case MONO_PATCH_INFO_VTABLE
: {
8606 MonoClass
*klass
= patch_info
->data
.klass
;
8608 if (mono_class_is_ginst (klass
) && !mini_class_is_generic_sharable (klass
))
8609 add_generic_class_with_depth (acfg
, klass
, depth
+ 5, "vtable");
8612 case MONO_PATCH_INFO_SFLDA
: {
8613 MonoClass
*klass
= patch_info
->data
.field
->parent
;
8615 /* The .cctor needs to run at runtime. */
8616 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
))
8617 add_extra_method_with_depth (acfg
, mono_class_get_cctor (klass
), depth
+ 1);
8626 /* Determine whenever the method has GOT slots */
8627 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8628 switch (patch_info
->type
) {
8629 case MONO_PATCH_INFO_GOT_OFFSET
:
8630 case MONO_PATCH_INFO_NONE
:
8631 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
:
8632 case MONO_PATCH_INFO_GC_NURSERY_START
:
8633 case MONO_PATCH_INFO_GC_NURSERY_BITS
:
8635 case MONO_PATCH_INFO_IMAGE
:
8636 /* The assembly is stored in GOT slot 0 */
8637 if (patch_info
->data
.image
!= acfg
->image
)
8638 cfg
->has_got_slots
= TRUE
;
8641 if (!is_plt_patch (patch_info
) || (cfg
->compile_llvm
&& acfg
->aot_opts
.llvm_only
))
8642 cfg
->has_got_slots
= TRUE
;
8647 if (!cfg
->has_got_slots
)
8648 mono_atomic_inc_i32 (&acfg
->stats
.methods_without_got_slots
);
8650 /* Add gsharedvt wrappers for signatures used by the method */
8651 if (acfg
->aot_opts
.llvm_only
) {
8654 if (!cfg
->method
->wrapper_type
|| cfg
->method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
8655 /* These only need out wrappers */
8656 add_gsharedvt_wrappers (acfg
, mono_method_signature_internal (cfg
->method
), FALSE
, TRUE
, FALSE
);
8658 for (l
= cfg
->signatures
; l
; l
= l
->next
) {
8659 MonoMethodSignature
*sig
= mono_metadata_signature_dup ((MonoMethodSignature
*)l
->data
);
8661 /* These only need in wrappers */
8662 add_gsharedvt_wrappers (acfg
, sig
, TRUE
, FALSE
, FALSE
);
8665 for (l
= cfg
->interp_in_signatures
; l
; l
= l
->next
) {
8666 MonoMethodSignature
*sig
= mono_metadata_signature_dup ((MonoMethodSignature
*)l
->data
);
8668 add_gsharedvt_wrappers (acfg
, sig
, TRUE
, FALSE
, FALSE
);
8670 } else if (mono_aot_mode_is_full (&acfg
->aot_opts
) && mono_aot_mode_is_interp (&acfg
->aot_opts
)) {
8671 /* The interpreter uses these wrappers to call aot-ed code */
8672 if (!cfg
->method
->wrapper_type
|| cfg
->method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
8673 add_gsharedvt_wrappers (acfg
, mono_method_signature_internal (cfg
->method
), FALSE
, TRUE
, TRUE
);
8677 acfg
->stats
.llvm_count
++;
8680 * FIXME: Instead of this mess, allocate the patches from the aot mempool.
8682 /* Make a copy of the patch info which is in the mempool */
8684 MonoJumpInfo
*patches
= NULL
, *patches_end
= NULL
;
8686 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
8687 MonoJumpInfo
*new_patch_info
= mono_patch_info_dup_mp (acfg
->mempool
, patch_info
);
8690 patches
= new_patch_info
;
8692 patches_end
->next
= new_patch_info
;
8693 patches_end
= new_patch_info
;
8695 cfg
->patch_info
= patches
;
8697 /* Make a copy of the unwind info */
8699 GSList
*l
, *unwind_ops
;
8703 for (l
= cfg
->unwind_ops
; l
; l
= l
->next
) {
8704 op
= (MonoUnwindOp
*)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoUnwindOp
));
8705 memcpy (op
, l
->data
, sizeof (MonoUnwindOp
));
8706 unwind_ops
= g_slist_prepend_mempool (acfg
->mempool
, unwind_ops
, op
);
8708 cfg
->unwind_ops
= g_slist_reverse (unwind_ops
);
8710 /* Make a copy of the argument/local info */
8713 MonoInst
**args
, **locals
;
8714 MonoMethodSignature
*sig
;
8715 MonoMethodHeader
*header
;
8718 sig
= mono_method_signature_internal (method
);
8719 args
= (MonoInst
**)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
*) * (sig
->param_count
+ sig
->hasthis
));
8720 for (i
= 0; i
< sig
->param_count
+ sig
->hasthis
; ++i
) {
8721 args
[i
] = (MonoInst
*)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
));
8722 memcpy (args
[i
], cfg
->args
[i
], sizeof (MonoInst
));
8726 header
= mono_method_get_header_checked (method
, error
);
8727 mono_error_assert_ok (error
); /* FIXME don't swallow the error */
8728 locals
= (MonoInst
**)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
*) * header
->num_locals
);
8729 for (i
= 0; i
< header
->num_locals
; ++i
) {
8730 locals
[i
] = (MonoInst
*)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
));
8731 memcpy (locals
[i
], cfg
->locals
[i
], sizeof (MonoInst
));
8733 mono_metadata_free_mh (header
);
8734 cfg
->locals
= locals
;
8737 /* Free some fields used by cfg to conserve memory */
8738 mono_empty_compile (cfg
);
8740 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
8742 while (index
>= acfg
->cfgs_size
) {
8743 MonoCompile
**new_cfgs
;
8746 new_size
= acfg
->cfgs_size
* 2;
8747 new_cfgs
= g_new0 (MonoCompile
*, new_size
);
8748 memcpy (new_cfgs
, acfg
->cfgs
, sizeof (MonoCompile
*) * acfg
->cfgs_size
);
8749 g_free (acfg
->cfgs
);
8750 acfg
->cfgs
= new_cfgs
;
8751 acfg
->cfgs_size
= new_size
;
8753 acfg
->cfgs
[index
] = cfg
;
8755 g_hash_table_insert (acfg
->method_to_cfg
, cfg
->orig_method
, cfg
);
8757 /* Update global stats while holding a lock. */
8758 mono_update_jit_stats (cfg
);
8761 if (cfg->orig_method->wrapper_type)
8762 g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
8765 mono_acfg_unlock (acfg
);
8767 mono_atomic_inc_i32 (&acfg
->stats
.ccount
);
8770 static mono_thread_start_return_t WINAPI
8771 compile_thread_main (gpointer user_data
)
8773 MonoAotCompile
*acfg
= ((MonoAotCompile
**)user_data
) [0];
8774 GPtrArray
*methods
= ((GPtrArray
**)user_data
) [1];
8777 mono_thread_set_name_constant_ignore_error (mono_thread_internal_current (), "AOT compiler", MonoSetThreadNameFlag_Permanent
);
8779 for (i
= 0; i
< methods
->len
; ++i
)
8780 compile_method (acfg
, (MonoMethod
*)g_ptr_array_index (methods
, i
));
8785 /* Used by the LLVM backend */
8787 mono_aot_get_got_offset (MonoJumpInfo
*ji
)
8789 return get_got_offset (llvm_acfg
, TRUE
, ji
);
8793 * mono_aot_is_shared_got_offset:
8795 * Return whenever OFFSET refers to a GOT slot which is preinitialized
8796 * when the AOT image is loaded.
8799 mono_aot_is_shared_got_offset (int offset
)
8801 return offset
< llvm_acfg
->nshared_got_entries
;
8805 mono_aot_get_method_name (MonoCompile
*cfg
)
8807 MonoMethod
*method
= cfg
->orig_method
;
8809 /* Use the mangled name if possible */
8810 if (method
->wrapper_type
== MONO_WRAPPER_OTHER
) {
8811 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
8812 if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
|| info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
)
8813 return mono_aot_get_mangled_method_name (method
);
8816 if (llvm_acfg
->aot_opts
.static_link
)
8817 /* Include the assembly name too to avoid duplicate symbol errors */
8818 return g_strdup_printf ("%s_%s", llvm_acfg
->assembly_name_sym
, get_debug_sym (cfg
->orig_method
, "", llvm_acfg
->method_label_hash
));
8820 return get_debug_sym (cfg
->orig_method
, "", llvm_acfg
->method_label_hash
);
8824 append_mangled_type (GString
*s
, MonoType
*t
)
8827 g_string_append_printf (s
, "b");
8829 case MONO_TYPE_VOID
:
8830 g_string_append_printf (s
, "void");
8833 g_string_append_printf (s
, "i1");
8836 g_string_append_printf (s
, "u1");
8839 g_string_append_printf (s
, "i2");
8842 g_string_append_printf (s
, "u2");
8845 g_string_append_printf (s
, "i4");
8848 g_string_append_printf (s
, "u4");
8851 g_string_append_printf (s
, "i8");
8854 g_string_append_printf (s
, "u8");
8857 g_string_append_printf (s
, "ii");
8860 g_string_append_printf (s
, "ui");
8863 g_string_append_printf (s
, "fl");
8866 g_string_append_printf (s
, "do");
8868 case MONO_TYPE_OBJECT
:
8869 g_string_append_printf (s
, "obj");
8872 char *fullname
= mono_type_full_name (t
);
8878 * Have to create a mangled name which is:
8882 temp
= g_string_new ("");
8883 len
= strlen (fullname
);
8884 for (i
= 0; i
< len
; ++i
) {
8885 char c
= fullname
[i
];
8887 g_string_append_c (temp
, c
);
8888 } else if (c
== '_') {
8889 g_string_append_c (temp
, '_');
8890 g_string_append_c (temp
, '_');
8892 g_string_append_c (temp
, '_');
8893 g_string_append_printf (temp
, "%x", (int)c
);
8896 temps
= g_string_free (temp
, FALSE
);
8897 /* Include the length to avoid different length type names aliasing each other */
8898 g_string_append_printf (s
, "cl%x_%s_", (int)strlen (temps
), temps
);
8903 g_string_append_printf (s
, "_attrs_%d", t
->attrs
);
8908 append_mangled_signature (GString
*s
, MonoMethodSignature
*sig
)
8914 g_string_append_printf (s
, "pinvoke_");
8915 supported
= append_mangled_type (s
, sig
->ret
);
8918 g_string_append_printf (s
, "_");
8920 g_string_append_printf (s
, "this_");
8921 for (i
= 0; i
< sig
->param_count
; ++i
) {
8922 supported
= append_mangled_type (s
, sig
->params
[i
]);
8931 append_mangled_wrapper_type (GString
*s
, guint32 wrapper_type
)
8935 switch (wrapper_type
) {
8936 case MONO_WRAPPER_REMOTING_INVOKE
:
8937 label
= "remoting_invoke";
8939 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
8940 label
= "remoting_invoke_check";
8942 case MONO_WRAPPER_XDOMAIN_INVOKE
:
8943 label
= "remoting_invoke_xdomain";
8945 case MONO_WRAPPER_PROXY_ISINST
:
8946 label
= "proxy_isinst";
8948 case MONO_WRAPPER_LDFLD
:
8951 case MONO_WRAPPER_LDFLDA
:
8954 case MONO_WRAPPER_STFLD
:
8957 case MONO_WRAPPER_ALLOC
:
8960 case MONO_WRAPPER_WRITE_BARRIER
:
8961 label
= "write_barrier";
8963 case MONO_WRAPPER_STELEMREF
:
8964 label
= "stelemref";
8966 case MONO_WRAPPER_OTHER
:
8969 case MONO_WRAPPER_MANAGED_TO_NATIVE
:
8970 label
= "man2native";
8972 case MONO_WRAPPER_SYNCHRONIZED
:
8975 case MONO_WRAPPER_MANAGED_TO_MANAGED
:
8978 case MONO_WRAPPER_CASTCLASS
:
8979 label
= "castclass";
8981 case MONO_WRAPPER_RUNTIME_INVOKE
:
8982 label
= "run_invoke";
8984 case MONO_WRAPPER_DELEGATE_INVOKE
:
8987 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
8988 label
= "del_beg_inv";
8990 case MONO_WRAPPER_DELEGATE_END_INVOKE
:
8991 label
= "del_end_inv";
8993 case MONO_WRAPPER_NATIVE_TO_MANAGED
:
8994 label
= "native2man";
8997 g_assert_not_reached ();
9000 g_string_append_printf (s
, "%s_", label
);
9004 append_mangled_wrapper_subtype (GString
*s
, WrapperSubtype subtype
)
9010 case WRAPPER_SUBTYPE_NONE
:
9012 case WRAPPER_SUBTYPE_ELEMENT_ADDR
:
9013 label
= "elem_addr";
9015 case WRAPPER_SUBTYPE_STRING_CTOR
:
9018 case WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
:
9019 label
= "virt_stelem";
9021 case WRAPPER_SUBTYPE_FAST_MONITOR_ENTER
:
9022 label
= "fast_mon_enter";
9024 case WRAPPER_SUBTYPE_FAST_MONITOR_ENTER_V4
:
9025 label
= "fast_mon_enter_4";
9027 case WRAPPER_SUBTYPE_FAST_MONITOR_EXIT
:
9028 label
= "fast_monitor_exit";
9030 case WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
:
9031 label
= "ptr2struct";
9033 case WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
:
9034 label
= "struct2ptr";
9036 case WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE
:
9037 label
= "castclass_w_cache";
9039 case WRAPPER_SUBTYPE_ISINST_WITH_CACHE
:
9040 label
= "isinst_w_cache";
9042 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL
:
9043 label
= "run_inv_norm";
9045 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC
:
9046 label
= "run_inv_dyn";
9048 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
:
9049 label
= "run_inv_dir";
9051 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
:
9052 label
= "run_inv_vir";
9054 case WRAPPER_SUBTYPE_ICALL_WRAPPER
:
9057 case WRAPPER_SUBTYPE_NATIVE_FUNC_AOT
:
9058 label
= "native_func_aot";
9060 case WRAPPER_SUBTYPE_PINVOKE
:
9063 case WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
:
9064 label
= "synch_inner";
9066 case WRAPPER_SUBTYPE_GSHAREDVT_IN
:
9067 label
= "gshared_in";
9069 case WRAPPER_SUBTYPE_GSHAREDVT_OUT
:
9070 label
= "gshared_out";
9072 case WRAPPER_SUBTYPE_ARRAY_ACCESSOR
:
9073 label
= "array_acc";
9075 case WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER
:
9076 label
= "generic_arry_help";
9078 case WRAPPER_SUBTYPE_DELEGATE_INVOKE_VIRTUAL
:
9079 label
= "del_inv_virt";
9081 case WRAPPER_SUBTYPE_DELEGATE_INVOKE_BOUND
:
9082 label
= "del_inv_bound";
9084 case WRAPPER_SUBTYPE_INTERP_IN
:
9085 label
= "interp_in";
9087 case WRAPPER_SUBTYPE_INTERP_LMF
:
9088 label
= "interp_lmf";
9090 case WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
:
9091 label
= "gsharedvt_in_sig";
9093 case WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
:
9094 label
= "gsharedvt_out_sig";
9096 case WRAPPER_SUBTYPE_AOT_INIT
:
9100 g_assert_not_reached ();
9103 g_string_append_printf (s
, "%s_", label
);
9107 sanitize_mangled_string (const char *input
)
9109 GString
*s
= g_string_new ("");
9111 for (int i
=0; input
[i
] != '\0'; i
++) {
9115 g_string_append (s
, "_dot_");
9118 g_string_append (s
, "_");
9121 g_string_append (s
, "_bt_");
9124 g_string_append (s
, "_le_");
9127 g_string_append (s
, "_gt_");
9130 g_string_append (s
, "_sl_");
9133 g_string_append (s
, "_lbrack_");
9136 g_string_append (s
, "_rbrack_");
9139 g_string_append (s
, "_lparen_");
9142 g_string_append (s
, "_dash_");
9145 g_string_append (s
, "_rparen_");
9148 g_string_append (s
, "_comma_");
9151 g_string_append (s
, "_colon_");
9154 g_string_append_c (s
, c
);
9158 return g_string_free (s
, FALSE
);
9162 append_mangled_klass (GString
*s
, MonoClass
*klass
)
9164 char *klass_desc
= mono_class_full_name (klass
);
9165 g_string_append_printf (s
, "_%s_%s_", m_class_get_name_space (klass
), klass_desc
);
9166 g_free (klass_desc
);
9173 append_mangled_method (GString
*s
, MonoMethod
*method
);
9176 append_mangled_wrapper (GString
*s
, MonoMethod
*method
)
9178 gboolean success
= TRUE
;
9179 gboolean append_sig
= TRUE
;
9180 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
9181 g_string_append_printf (s
, "wrapper_");
9182 /* Most wrappers are in mscorlib */
9183 if (m_class_get_image (method
->klass
) != mono_get_corlib ())
9184 g_string_append_printf (s
, "%s_", m_class_get_image (method
->klass
)->assembly
->aname
.name
);
9186 if (method
->wrapper_type
!= MONO_WRAPPER_OTHER
&& method
->wrapper_type
!= MONO_WRAPPER_MANAGED_TO_NATIVE
)
9187 append_mangled_wrapper_type (s
, method
->wrapper_type
);
9189 switch (method
->wrapper_type
) {
9190 case MONO_WRAPPER_REMOTING_INVOKE
:
9191 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
9192 case MONO_WRAPPER_XDOMAIN_INVOKE
: {
9193 MonoMethod
*m
= mono_marshal_method_from_wrapper (method
);
9195 success
= success
&& append_mangled_method (s
, m
);
9198 case MONO_WRAPPER_PROXY_ISINST
:
9199 case MONO_WRAPPER_LDFLD
:
9200 case MONO_WRAPPER_LDFLDA
:
9201 case MONO_WRAPPER_STFLD
: {
9203 success
= success
&& append_mangled_klass (s
, info
->d
.proxy
.klass
);
9206 case MONO_WRAPPER_ALLOC
: {
9207 /* The GC name is saved once in MonoAotFileInfo */
9208 g_assert (info
->d
.alloc
.alloc_type
!= -1);
9209 g_string_append_printf (s
, "%d_", info
->d
.alloc
.alloc_type
);
9211 g_string_append_printf (s
, "%s_", method
->name
);
9214 case MONO_WRAPPER_WRITE_BARRIER
: {
9215 g_string_append_printf (s
, "%s_", method
->name
);
9218 case MONO_WRAPPER_STELEMREF
: {
9219 append_mangled_wrapper_subtype (s
, info
->subtype
);
9220 if (info
->subtype
== WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
)
9221 g_string_append_printf (s
, "%d", info
->d
.virtual_stelemref
.kind
);
9224 case MONO_WRAPPER_OTHER
: {
9225 append_mangled_wrapper_subtype (s
, info
->subtype
);
9226 if (info
->subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
||
9227 info
->subtype
== WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
)
9228 success
= success
&& append_mangled_klass (s
, method
->klass
);
9229 else if (info
->subtype
== WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
)
9230 success
= success
&& append_mangled_method (s
, info
->d
.synchronized_inner
.method
);
9231 else if (info
->subtype
== WRAPPER_SUBTYPE_ARRAY_ACCESSOR
)
9232 success
= success
&& append_mangled_method (s
, info
->d
.array_accessor
.method
);
9233 else if (info
->subtype
== WRAPPER_SUBTYPE_INTERP_IN
)
9234 append_mangled_signature (s
, info
->d
.interp_in
.sig
);
9235 else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
) {
9236 append_mangled_signature (s
, info
->d
.gsharedvt
.sig
);
9238 } else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
) {
9239 append_mangled_signature (s
, info
->d
.gsharedvt
.sig
);
9241 } else if (info
->subtype
== WRAPPER_SUBTYPE_INTERP_LMF
)
9242 g_string_append_printf (s
, "%s", method
->name
);
9243 else if (info
->subtype
== WRAPPER_SUBTYPE_AOT_INIT
) {
9244 g_string_append_printf (s
, "%s_%d_", m_class_get_image (method
->klass
)->assembly
->aname
.name
, info
->d
.aot_init
.subtype
);
9249 case MONO_WRAPPER_MANAGED_TO_NATIVE
: {
9250 append_mangled_wrapper_subtype (s
, info
->subtype
);
9251 if (info
->subtype
== WRAPPER_SUBTYPE_ICALL_WRAPPER
) {
9252 const char *name
= method
->name
;
9253 const char *prefix
= "__icall_wrapper_";
9254 if (strstr (name
, prefix
) == name
)
9255 name
+= strlen (prefix
);
9256 g_string_append_printf (s
, "%s", name
);
9258 } else if (info
->subtype
== WRAPPER_SUBTYPE_NATIVE_FUNC_AOT
) {
9259 success
= success
&& append_mangled_method (s
, info
->d
.managed_to_native
.method
);
9261 g_assert (info
->subtype
== WRAPPER_SUBTYPE_NONE
|| info
->subtype
== WRAPPER_SUBTYPE_PINVOKE
);
9262 success
= success
&& append_mangled_method (s
, info
->d
.managed_to_native
.method
);
9266 case MONO_WRAPPER_SYNCHRONIZED
: {
9269 m
= mono_marshal_method_from_wrapper (method
);
9271 g_assert (m
!= method
);
9272 success
= success
&& append_mangled_method (s
, m
);
9275 case MONO_WRAPPER_MANAGED_TO_MANAGED
: {
9276 append_mangled_wrapper_subtype (s
, info
->subtype
);
9278 if (info
->subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
) {
9279 g_string_append_printf (s
, "%d_", info
->d
.element_addr
.rank
);
9280 g_string_append_printf (s
, "%d_", info
->d
.element_addr
.elem_size
);
9281 } else if (info
->subtype
== WRAPPER_SUBTYPE_STRING_CTOR
) {
9282 success
= success
&& append_mangled_method (s
, info
->d
.string_ctor
.method
);
9283 } else if (info
->subtype
== WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER
) {
9284 success
= success
&& append_mangled_method (s
, info
->d
.generic_array_helper
.method
);
9290 case MONO_WRAPPER_CASTCLASS
: {
9291 append_mangled_wrapper_subtype (s
, info
->subtype
);
9294 case MONO_WRAPPER_RUNTIME_INVOKE
: {
9295 append_mangled_wrapper_subtype (s
, info
->subtype
);
9296 if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
|| info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
)
9297 success
= success
&& append_mangled_method (s
, info
->d
.runtime_invoke
.method
);
9298 else if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL
)
9299 success
= success
&& append_mangled_signature (s
, info
->d
.runtime_invoke
.sig
);
9302 case MONO_WRAPPER_DELEGATE_INVOKE
:
9303 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
9304 case MONO_WRAPPER_DELEGATE_END_INVOKE
: {
9305 if (method
->is_inflated
) {
9306 /* These wrappers are identified by their class */
9307 g_string_append_printf (s
, "i_");
9308 success
= success
&& append_mangled_klass (s
, method
->klass
);
9310 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
9312 g_string_append_printf (s
, "u_");
9313 if (method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
9314 append_mangled_wrapper_subtype (s
, info
->subtype
);
9315 g_string_append_printf (s
, "u_sigstart");
9319 case MONO_WRAPPER_NATIVE_TO_MANAGED
: {
9321 success
= success
&& append_mangled_method (s
, info
->d
.native_to_managed
.method
);
9322 success
= success
&& append_mangled_klass (s
, method
->klass
);
9326 g_assert_not_reached ();
9328 if (success
&& append_sig
)
9329 success
= append_mangled_signature (s
, mono_method_signature_internal (method
));
9334 append_mangled_ginst (GString
*str
, MonoGenericInst
*ginst
)
9338 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
9340 g_string_append (str
, ", ");
9341 MonoType
*type
= ginst
->type_argv
[i
];
9342 switch (type
->type
) {
9344 case MONO_TYPE_MVAR
: {
9345 MonoType
*constraint
= NULL
;
9346 if (type
->data
.generic_param
)
9347 constraint
= type
->data
.generic_param
->gshared_constraint
;
9349 g_assert (constraint
->type
!= MONO_TYPE_VAR
&& constraint
->type
!= MONO_TYPE_MVAR
);
9350 g_string_append (str
, "gshared:");
9351 mono_type_get_desc (str
, constraint
, TRUE
);
9354 // Else falls through to common case
9357 mono_type_get_desc (str
, type
, TRUE
);
9363 append_mangled_context (GString
*str
, MonoGenericContext
*context
)
9365 GString
*res
= g_string_new ("");
9367 g_string_append_printf (res
, "gens_");
9368 g_string_append (res
, "00");
9370 gboolean good
= context
->class_inst
&& context
->class_inst
->type_argc
> 0;
9371 good
= good
|| (context
->method_inst
&& context
->method_inst
->type_argc
> 0);
9374 if (context
->class_inst
)
9375 append_mangled_ginst (res
, context
->class_inst
);
9376 if (context
->method_inst
) {
9377 if (context
->class_inst
)
9378 g_string_append (res
, "11");
9379 append_mangled_ginst (res
, context
->method_inst
);
9381 g_string_append_printf (str
, "gens_%s", res
->str
);
9386 append_mangled_method (GString
*s
, MonoMethod
*method
)
9388 if (method
->wrapper_type
)
9389 return append_mangled_wrapper (s
, method
);
9391 if (method
->is_inflated
) {
9392 g_string_append_printf (s
, "inflated_");
9393 MonoMethodInflated
*imethod
= (MonoMethodInflated
*) method
;
9394 g_assert (imethod
->context
.class_inst
!= NULL
|| imethod
->context
.method_inst
!= NULL
);
9396 append_mangled_context (s
, &imethod
->context
);
9397 g_string_append_printf (s
, "_declared_by_%s_", m_class_get_image (imethod
->declaring
->klass
)->assembly
->aname
.name
);
9398 append_mangled_method (s
, imethod
->declaring
);
9399 } else if (method
->is_generic
) {
9400 g_string_append_printf (s
, "%s_", m_class_get_image (method
->klass
)->assembly
->aname
.name
);
9402 g_string_append_printf (s
, "generic_");
9403 append_mangled_klass (s
, method
->klass
);
9404 g_string_append_printf (s
, "_%s_", method
->name
);
9406 MonoGenericContainer
*container
= mono_method_get_generic_container (method
);
9407 g_string_append_printf (s
, "_");
9408 append_mangled_context (s
, &container
->context
);
9410 return append_mangled_signature (s
, mono_method_signature_internal (method
));
9412 g_string_append_printf (s
, "%s", m_class_get_image (method
->klass
)->assembly
->aname
.name
);
9413 append_mangled_klass (s
, method
->klass
);
9414 g_string_append_printf (s
, "_%s_", method
->name
);
9415 if (!append_mangled_signature (s
, mono_method_signature_internal (method
))) {
9416 g_string_free (s
, TRUE
);
9425 * mono_aot_get_mangled_method_name:
9427 * Return a unique mangled name for METHOD, or NULL.
9430 mono_aot_get_mangled_method_name (MonoMethod
*method
)
9432 // FIXME: use static cache (mempool?)
9433 // We call this a *lot*
9435 GString
*s
= g_string_new ("aot_");
9436 if (!append_mangled_method (s
, method
)) {
9437 g_string_free (s
, TRUE
);
9440 char *out
= g_string_free (s
, FALSE
);
9441 // Scrub method and class names
9442 char *cleaned
= sanitize_mangled_string (out
);
9449 mono_aot_is_direct_callable (MonoJumpInfo
*patch_info
)
9451 return is_direct_callable (llvm_acfg
, NULL
, patch_info
);
9455 mono_aot_mark_unused_llvm_plt_entry (MonoJumpInfo
*patch_info
)
9457 MonoPltEntry
*plt_entry
;
9459 plt_entry
= get_plt_entry (llvm_acfg
, patch_info
);
9460 plt_entry
->llvm_used
= FALSE
;
9464 mono_aot_get_direct_call_symbol (MonoJumpInfoType type
, gconstpointer data
)
9466 const char *sym
= NULL
;
9468 if (llvm_acfg
->aot_opts
.direct_icalls
) {
9469 if (type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
9470 /* Call to a C function implementing a jit icall */
9471 sym
= mono_find_jit_icall_info ((MonoJitICallId
)(gsize
)data
)->c_symbol
;
9472 } else if (type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
9473 MonoMethod
*method
= (MonoMethod
*)data
;
9474 if (!(method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
9475 sym
= lookup_icall_symbol_name_aot (method
);
9476 else if (llvm_acfg
->aot_opts
.direct_pinvoke
)
9477 sym
= get_pinvoke_import (llvm_acfg
, method
);
9478 } else if (type
== MONO_PATCH_INFO_JIT_ICALL_ID
) {
9479 MonoJitICallInfo
const * const info
= mono_find_jit_icall_info ((MonoJitICallId
)(gsize
)data
);
9480 char const * const name
= info
->c_symbol
;
9481 if (name
&& info
->func
== info
->wrapper
)
9485 return g_strdup (sym
);
9491 mono_aot_get_plt_symbol (MonoJumpInfoType type
, gconstpointer data
)
9493 MonoJumpInfo
*ji
= (MonoJumpInfo
*)mono_mempool_alloc (llvm_acfg
->mempool
, sizeof (MonoJumpInfo
));
9494 MonoPltEntry
*plt_entry
;
9495 const char *sym
= NULL
;
9498 ji
->data
.target
= data
;
9500 if (!can_encode_patch (llvm_acfg
, ji
))
9503 if (llvm_acfg
->aot_opts
.direct_icalls
) {
9504 if (type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
9505 /* Call to a C function implementing a jit icall */
9506 sym
= mono_find_jit_icall_info ((MonoJitICallId
)(gsize
)data
)->c_symbol
;
9507 } else if (type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
9508 MonoMethod
*method
= (MonoMethod
*)data
;
9509 if (!(method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
9510 sym
= lookup_icall_symbol_name_aot (method
);
9513 return g_strdup (sym
);
9516 plt_entry
= get_plt_entry (llvm_acfg
, ji
);
9517 plt_entry
->llvm_used
= TRUE
;
9519 #if defined(TARGET_MACH)
9520 return g_strdup (plt_entry
->llvm_symbol
+ strlen (llvm_acfg
->llvm_label_prefix
));
9522 return g_strdup (plt_entry
->llvm_symbol
);
9527 mono_aot_get_method_index (MonoMethod
*method
)
9529 g_assert (llvm_acfg
);
9530 return get_method_index (llvm_acfg
, method
);
9534 mono_aot_patch_info_dup (MonoJumpInfo
* ji
)
9538 mono_acfg_lock (llvm_acfg
);
9539 res
= mono_patch_info_dup_mp (llvm_acfg
->mempool
, ji
);
9540 mono_acfg_unlock (llvm_acfg
);
9546 execute_system (const char * command
)
9550 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) && defined(HOST_WIN32)
9551 // We need an extra set of quotes around the whole command to properly handle commands
9552 // with spaces since internally the command is called through "cmd /c.
9553 char * quoted_command
= g_strdup_printf ("\"%s\"", command
);
9555 int size
= MultiByteToWideChar (CP_UTF8
, 0 , quoted_command
, -1, NULL
, 0);
9556 wchar_t* wstr
= g_malloc (sizeof (wchar_t) * size
);
9557 MultiByteToWideChar (CP_UTF8
, 0, quoted_command
, -1, wstr
, size
);
9558 status
= _wsystem (wstr
);
9561 g_free (quoted_command
);
9562 #elif defined (HAVE_SYSTEM)
9563 status
= system (command
);
9565 g_assert_not_reached ();
9576 * Emit the LLVM code into an LLVM bytecode file, and compile it using the LLVM
9580 emit_llvm_file (MonoAotCompile
*acfg
)
9582 char *command
, *opts
, *tempbc
, *optbc
, *output_fname
;
9584 if (acfg
->aot_opts
.llvm_only
&& acfg
->aot_opts
.asm_only
) {
9585 if (acfg
->aot_opts
.no_opt
)
9586 tempbc
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
9588 tempbc
= g_strdup_printf ("%s.bc", acfg
->tmpbasename
);
9589 optbc
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
9591 tempbc
= g_strdup_printf ("%s.bc", acfg
->tmpbasename
);
9592 optbc
= g_strdup_printf ("%s.opt.bc", acfg
->tmpbasename
);
9595 mono_llvm_emit_aot_module (tempbc
, g_path_get_basename (acfg
->image
->name
));
9597 if (acfg
->aot_opts
.no_opt
)
9600 * FIXME: Experiment with adding optimizations, the -std-compile-opts set takes
9601 * a lot of time, and doesn't seem to save much space.
9602 * The following optimizations cannot be enabled:
9604 * - 'jump-threading' changes our blockaddress references to int constants.
9605 * - 'basiccg' fails because it contains:
9606 * if (CS && !isa<IntrinsicInst>(II)) {
9607 * and isa<IntrinsicInst> is false for invokes to intrinsics (iltests.exe).
9608 * - 'prune-eh' and 'functionattrs' depend on 'basiccg'.
9609 * The opt list below was produced by taking the output of:
9610 * llvm-as < /dev/null | opt -O2 -disable-output -debug-pass=Arguments
9611 * then removing tailcallelim + the global opts.
9612 * strip-dead-prototypes deletes unused intrinsics definitions.
9614 /* The dse pass is disabled because of #13734 and #17616 */
9616 * The dse bug is in DeadStoreElimination.cpp:isOverwrite ():
9617 * // If we have no DataLayout information around, then the size of the store
9618 * // is inferrable from the pointee type. If they are the same type, then
9619 * // we know that the store is safe.
9620 * if (AA.getDataLayout() == 0 &&
9621 * Later.Ptr->getType() == Earlier.Ptr->getType()) {
9622 * return OverwriteComplete;
9623 * Here, if 'Earlier' refers to a memset, and Later has no size info, it mistakenly thinks the memset is redundant.
9625 if (acfg
->aot_opts
.llvm_only
) {
9626 // FIXME: This doesn't work yet
9627 opts
= g_strdup ("");
9629 opts
= g_strdup ("-O2 -disable-tail-calls -place-safepoints -spp-all-backedges");
9632 if (acfg
->aot_opts
.llvm_opts
) {
9633 opts
= g_strdup_printf ("%s %s", opts
, acfg
->aot_opts
.llvm_opts
);
9636 if (mono_use_fast_math
) {
9637 // same parameters are passed to llc and LLVM JIT
9638 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
);
9641 command
= g_strdup_printf ("\"%sopt\" -f %s -o \"%s\" \"%s\"", acfg
->aot_opts
.llvm_path
, opts
, optbc
, tempbc
);
9642 aot_printf (acfg
, "Executing opt: %s\n", command
);
9643 if (execute_system (command
) != 0)
9647 if (acfg
->aot_opts
.llvm_only
&& acfg
->aot_opts
.asm_only
)
9648 /* Nothing else to do */
9651 if (acfg
->aot_opts
.llvm_only
) {
9652 /* Use the stock clang from xcode */
9654 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
);
9656 aot_printf (acfg
, "Executing clang: %s\n", command
);
9657 if (execute_system (command
) != 0)
9662 if (!acfg
->llc_args
)
9663 acfg
->llc_args
= g_string_new ("");
9665 /* Verbose asm slows down llc greatly */
9666 g_string_append (acfg
->llc_args
, " -asm-verbose=false");
9668 if (acfg
->aot_opts
.mtriple
)
9669 g_string_append_printf (acfg
->llc_args
, " -mtriple=%s", acfg
->aot_opts
.mtriple
);
9671 #if defined(TARGET_X86_64_WIN32_MSVC)
9672 if (!acfg
->aot_opts
.mtriple
)
9673 g_string_append_printf (acfg
->llc_args
, " -mtriple=%s", "x86_64-pc-windows-msvc");
9676 g_string_append (acfg
->llc_args
, " -disable-gnu-eh-frame -enable-mono-eh-frame");
9678 g_string_append_printf (acfg
->llc_args
, " -mono-eh-frame-symbol=%s%s", acfg
->user_symbol_prefix
, acfg
->llvm_eh_frame_symbol
);
9680 g_string_append_printf (acfg
->llc_args
, " -disable-tail-calls");
9682 #if defined(TARGET_AMD64) || defined(TARGET_X86)
9683 /* This generates stack adjustments in the middle of functions breaking unwind info */
9684 g_string_append_printf (acfg
->llc_args
, " -no-x86-call-frame-opt");
9687 #if ( defined(TARGET_MACH) && defined(TARGET_ARM) ) || defined(TARGET_ORBIS) || defined(TARGET_X86_64_WIN32_MSVC)
9688 g_string_append_printf (acfg
->llc_args
, " -relocation-model=pic");
9690 if (llvm_acfg
->aot_opts
.static_link
)
9691 g_string_append_printf (acfg
->llc_args
, " -relocation-model=static");
9693 g_string_append_printf (acfg
->llc_args
, " -relocation-model=pic");
9696 if (acfg
->llvm_owriter
) {
9697 /* Emit an object file directly */
9698 output_fname
= g_strdup_printf ("%s", acfg
->llvm_ofile
);
9699 g_string_append_printf (acfg
->llc_args
, " -filetype=obj");
9701 output_fname
= g_strdup_printf ("%s", acfg
->llvm_sfile
);
9704 if (acfg
->aot_opts
.llvm_llc
) {
9705 g_string_append_printf (acfg
->llc_args
, " %s", acfg
->aot_opts
.llvm_llc
);
9708 command
= g_strdup_printf ("\"%sllc\" %s -o \"%s\" \"%s.opt.bc\"", acfg
->aot_opts
.llvm_path
, acfg
->llc_args
->str
, output_fname
, acfg
->tmpbasename
);
9709 g_free (output_fname
);
9711 aot_printf (acfg
, "Executing llc: %s\n", command
);
9713 if (execute_system (command
) != 0)
9719 /* Set the skip flag for methods which do not need to be emitted because of dedup */
9721 dedup_skip_methods (MonoAotCompile
*acfg
)
9725 if (acfg
->aot_opts
.llvm_only
)
9728 for (oindex
= 0; oindex
< acfg
->method_order
->len
; ++oindex
) {
9732 i
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->method_order
, oindex
));
9734 cfg
= acfg
->cfgs
[i
];
9739 method
= cfg
->orig_method
;
9741 gboolean dedup_collect
= acfg
->aot_opts
.dedup
|| (acfg
->aot_opts
.dedup_include
&& !acfg
->dedup_emit_mode
);
9742 gboolean dedupable
= mono_aot_can_dedup (method
);
9744 // cfg->skip is vital for LLVM to work, can't just continue in this loop
9745 if (dedupable
&& strcmp (method
->name
, "wbarrier_conc") && dedup_collect
) {
9746 mono_dedup_cache_method (acfg
, method
);
9748 // Don't compile inflated methods if we're in first phase of
9751 // In second phase, we emit methods that
9752 // are dedupable. We also emit later methods
9753 // which are referenced by them and added later.
9754 // For this reason, when in the dedup_include mode,
9755 // we never set skip.
9756 if (acfg
->aot_opts
.dedup
)
9760 // Don't compile anything in this mode
9761 if (acfg
->aot_opts
.dedup_include
&& !acfg
->dedup_emit_mode
)
9764 // Compile everything in this mode
9765 if (acfg
->aot_opts
.dedup_include
&& acfg
->dedup_emit_mode
)
9768 /*if (dedup_collect) {*/
9769 /*char *name = mono_aot_get_mangled_method_name (method);*/
9771 /*if (ignore_cfg (cfg))*/
9772 /*aot_printf (acfg, "Dedup Skipping %s\n", acfg->image->name, name);*/
9774 /*aot_printf (acfg, "Dedup Keeping %s\n", acfg->image->name, name);*/
9782 emit_code (MonoAotCompile
*acfg
)
9784 int oindex
, i
, prev_index
;
9785 gboolean saved_unbox_info
= FALSE
; // See mono_aot_get_unbox_trampoline.
9786 char symbol
[MAX_SYMBOL_SIZE
];
9788 if (acfg
->aot_opts
.llvm_only
)
9791 #if defined(TARGET_POWERPC64)
9792 sprintf (symbol
, ".Lgot_addr");
9793 emit_section_change (acfg
, ".text", 0);
9794 emit_alignment (acfg
, 8);
9795 emit_label (acfg
, symbol
);
9796 emit_pointer (acfg
, acfg
->got_symbol
);
9800 * This global symbol is used to compute the address of each method using the
9801 * code_offsets array. It is also used to compute the memory ranges occupied by
9802 * AOT code, so it must be equal to the address of the first emitted method.
9804 emit_section_change (acfg
, ".text", 0);
9805 emit_alignment_code (acfg
, 8);
9806 emit_info_symbol (acfg
, "jit_code_start", TRUE
);
9809 * Emit some padding so the local symbol for the first method doesn't have the
9810 * same address as 'methods'.
9812 emit_padding (acfg
, 16);
9814 for (oindex
= 0; oindex
< acfg
->method_order
->len
; ++oindex
) {
9818 i
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->method_order
, oindex
));
9820 cfg
= acfg
->cfgs
[i
];
9825 method
= cfg
->orig_method
;
9827 if (ignore_cfg (cfg
))
9830 /* Emit unbox trampoline */
9831 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && m_class_is_valuetype (cfg
->orig_method
->klass
)) {
9832 sprintf (symbol
, "ut_%d", get_method_index (acfg
, method
));
9834 emit_section_change (acfg
, ".text", 0);
9836 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
) {
9837 emit_set_thumb_mode (acfg
);
9838 fprintf (acfg
->fp
, "\n.thumb_func\n");
9841 emit_label (acfg
, symbol
);
9843 arch_emit_unbox_trampoline (acfg
, cfg
, cfg
->orig_method
, cfg
->asm_symbol
);
9845 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
)
9846 emit_set_arm_mode (acfg
);
9848 if (!saved_unbox_info
) {
9849 char user_symbol
[128];
9851 sprintf (user_symbol
, "%sunbox_trampoline_p", acfg
->user_symbol_prefix
);
9853 emit_label (acfg
, "ut_end");
9855 unwind_ops
= mono_unwind_get_cie_program ();
9856 save_unwind_info (acfg
, user_symbol
, unwind_ops
);
9857 mono_free_unwind_info (unwind_ops
);
9859 /* Save the unbox trampoline size */
9861 // LLVM unbox trampolines vary in size, 6 or 9 bytes,
9862 // due to the last instruction being 2 or 5 bytes.
9863 // There is no need to describe interior bytes of instructions
9864 // however, so state the size as if the last instruction is size 1.
9865 emit_int32 (acfg
, 5);
9867 emit_symbol_diff (acfg
, "ut_end", symbol
, 0);
9869 saved_unbox_info
= TRUE
;
9873 if (cfg
->compile_llvm
) {
9874 acfg
->stats
.llvm_count
++;
9876 emit_method_code (acfg
, cfg
);
9880 emit_section_change (acfg
, ".text", 0);
9881 emit_alignment_code (acfg
, 8);
9882 emit_info_symbol (acfg
, "jit_code_end", TRUE
);
9884 /* To distinguish it from the next symbol */
9885 emit_padding (acfg
, 4);
9888 * Add .no_dead_strip directives for all LLVM methods to prevent the OSX linker
9889 * from optimizing them away, since it doesn't see that code_offsets references them.
9890 * JITted methods don't need this since they are referenced using assembler local
9892 * FIXME: This is why write-symbols doesn't work on OSX ?
9894 if (acfg
->llvm
&& acfg
->need_no_dead_strip
) {
9895 fprintf (acfg
->fp
, "\n");
9896 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
9897 if (acfg
->cfgs
[i
] && acfg
->cfgs
[i
]->compile_llvm
)
9898 fprintf (acfg
->fp
, ".no_dead_strip %s\n", acfg
->cfgs
[i
]->asm_symbol
);
9903 * To work around linker issues, we emit a table of branches, and disassemble them at runtime.
9904 * This is PIE code, and the linker can update it if needed.
9907 sprintf (symbol
, "method_addresses");
9908 emit_section_change (acfg
, ".text", 1);
9909 emit_alignment_code (acfg
, 8);
9910 emit_info_symbol (acfg
, symbol
, TRUE
);
9911 if (acfg
->aot_opts
.write_symbols
)
9912 emit_local_symbol (acfg
, symbol
, "method_addresses_end", TRUE
);
9913 emit_unset_mode (acfg
);
9914 if (acfg
->need_no_dead_strip
)
9915 fprintf (acfg
->fp
, " .no_dead_strip %s\n", symbol
);
9917 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
9918 #ifdef MONO_ARCH_AOT_SUPPORTED
9921 if (!ignore_cfg (acfg
->cfgs
[i
])) {
9922 arch_emit_direct_call (acfg
, acfg
->cfgs
[i
]->asm_symbol
, FALSE
, acfg
->thumb_mixed
&& acfg
->cfgs
[i
]->compile_llvm
, NULL
, &call_size
);
9924 arch_emit_direct_call (acfg
, symbol
, FALSE
, FALSE
, NULL
, &call_size
);
9929 sprintf (symbol
, "method_addresses_end");
9930 emit_label (acfg
, symbol
);
9933 /* Emit a sorted table mapping methods to the index of their unbox trampolines */
9934 sprintf (symbol
, "unbox_trampolines");
9935 emit_section_change (acfg
, RODATA_SECT
, 0);
9936 emit_alignment (acfg
, 8);
9937 emit_info_symbol (acfg
, symbol
, FALSE
);
9940 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
9945 cfg
= acfg
->cfgs
[i
];
9946 if (ignore_cfg (cfg
))
9949 method
= cfg
->orig_method
;
9951 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && m_class_is_valuetype (cfg
->orig_method
->klass
)) {
9952 index
= get_method_index (acfg
, method
);
9954 emit_int32 (acfg
, index
);
9955 /* Make sure the table is sorted by index */
9956 g_assert (index
> prev_index
);
9960 sprintf (symbol
, "unbox_trampolines_end");
9961 emit_info_symbol (acfg
, symbol
, FALSE
);
9962 emit_int32 (acfg
, 0);
9964 /* Emit a separate table with the trampoline addresses/offsets */
9965 sprintf (symbol
, "unbox_trampoline_addresses");
9966 emit_section_change (acfg
, ".text", 0);
9967 emit_alignment_code (acfg
, 8);
9968 emit_info_symbol (acfg
, symbol
, TRUE
);
9970 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
9974 cfg
= acfg
->cfgs
[i
];
9975 if (ignore_cfg (cfg
))
9978 method
= cfg
->orig_method
;
9981 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && m_class_is_valuetype (cfg
->orig_method
->klass
)) {
9982 #ifdef MONO_ARCH_AOT_SUPPORTED
9985 const int index
= get_method_index (acfg
, method
);
9986 sprintf (symbol
, "ut_%d", index
);
9988 arch_emit_direct_call (acfg
, symbol
, FALSE
, acfg
->thumb_mixed
&& cfg
->compile_llvm
, NULL
, &call_size
);
9992 emit_int32 (acfg
, 0);
9996 emit_info (MonoAotCompile
*acfg
)
10001 offsets
= g_new0 (gint32
, acfg
->nmethods
);
10003 for (oindex
= 0; oindex
< acfg
->method_order
->len
; ++oindex
) {
10004 i
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->method_order
, oindex
));
10006 if (acfg
->cfgs
[i
]) {
10007 emit_method_info (acfg
, acfg
->cfgs
[i
]);
10008 offsets
[i
] = acfg
->cfgs
[i
]->method_info_offset
;
10014 acfg
->stats
.offsets_size
+= emit_offset_table (acfg
, "method_info_offsets", MONO_AOT_TABLE_METHOD_INFO_OFFSETS
, acfg
->nmethods
, 10, offsets
);
10019 #endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
10021 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
10022 #define mix(a,b,c) { \
10023 a -= c; a ^= rot(c, 4); c += b; \
10024 b -= a; b ^= rot(a, 6); a += c; \
10025 c -= b; c ^= rot(b, 8); b += a; \
10026 a -= c; a ^= rot(c,16); c += b; \
10027 b -= a; b ^= rot(a,19); a += c; \
10028 c -= b; c ^= rot(b, 4); b += a; \
10030 #define mono_final(a,b,c) { \
10031 c ^= b; c -= rot(b,14); \
10032 a ^= c; a -= rot(c,11); \
10033 b ^= a; b -= rot(a,25); \
10034 c ^= b; c -= rot(b,16); \
10035 a ^= c; a -= rot(c,4); \
10036 b ^= a; b -= rot(a,14); \
10037 c ^= b; c -= rot(b,24); \
10041 mono_aot_type_hash (MonoType
*t1
)
10043 guint hash
= t1
->type
;
10045 hash
|= t1
->byref
<< 6; /* do not collide with t1->type values */
10046 switch (t1
->type
) {
10047 case MONO_TYPE_VALUETYPE
:
10048 case MONO_TYPE_CLASS
:
10049 case MONO_TYPE_SZARRAY
:
10050 /* check if the distribution is good enough */
10051 return ((hash
<< 5) - hash
) ^ mono_metadata_str_hash (m_class_get_name (t1
->data
.klass
));
10052 case MONO_TYPE_PTR
:
10053 return ((hash
<< 5) - hash
) ^ mono_metadata_type_hash (t1
->data
.type
);
10054 case MONO_TYPE_ARRAY
:
10055 return ((hash
<< 5) - hash
) ^ mono_metadata_type_hash (m_class_get_byval_arg (t1
->data
.array
->eklass
));
10056 case MONO_TYPE_GENERICINST
:
10057 return ((hash
<< 5) - hash
) ^ 0;
10064 * mono_aot_method_hash:
10066 * Return a hash code for methods which only depends on metadata.
10069 mono_aot_method_hash (MonoMethod
*method
)
10071 MonoMethodSignature
*sig
;
10075 guint32
*hashes_start
, *hashes
;
10077 MonoGenericInst
*class_ginst
= NULL
;
10078 MonoGenericInst
*ginst
= NULL
;
10080 /* Similar to the hash in mono_method_get_imt_slot () */
10082 sig
= mono_method_signature_internal (method
);
10084 if (mono_class_is_ginst (method
->klass
))
10085 class_ginst
= mono_class_get_generic_class (method
->klass
)->context
.class_inst
;
10086 if (method
->is_inflated
)
10087 ginst
= ((MonoMethodInflated
*)method
)->context
.method_inst
;
10089 hashes_count
= sig
->param_count
+ 5 + (class_ginst
? class_ginst
->type_argc
: 0) + (ginst
? ginst
->type_argc
: 0);
10090 hashes_start
= (guint32
*)g_malloc0 (hashes_count
* sizeof (guint32
));
10091 hashes
= hashes_start
;
10093 /* Some wrappers are assigned to random classes */
10094 if (!method
->wrapper_type
|| method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
10095 klass
= method
->klass
;
10097 klass
= mono_defaults
.object_class
;
10099 if (!method
->wrapper_type
) {
10102 if (mono_class_is_ginst (klass
))
10103 full_name
= mono_type_full_name (m_class_get_byval_arg (mono_class_get_generic_class (klass
)->container_class
));
10105 full_name
= mono_type_full_name (m_class_get_byval_arg (klass
));
10107 hashes
[0] = mono_metadata_str_hash (full_name
);
10109 g_free (full_name
);
10111 hashes
[0] = mono_metadata_str_hash (m_class_get_name (klass
));
10112 hashes
[1] = mono_metadata_str_hash (m_class_get_name_space (klass
));
10114 if (method
->wrapper_type
== MONO_WRAPPER_STFLD
|| method
->wrapper_type
== MONO_WRAPPER_LDFLD
|| method
->wrapper_type
== MONO_WRAPPER_LDFLDA
)
10115 /* The method name includes a stringified pointer */
10118 hashes
[2] = mono_metadata_str_hash (method
->name
);
10119 hashes
[3] = method
->wrapper_type
;
10120 hashes
[4] = mono_aot_type_hash (sig
->ret
);
10122 for (i
= 0; i
< sig
->param_count
; i
++) {
10123 hashes
[hindex
++] = mono_aot_type_hash (sig
->params
[i
]);
10126 for (i
= 0; i
< class_ginst
->type_argc
; ++i
)
10127 hashes
[hindex
++] = mono_aot_type_hash (class_ginst
->type_argv
[i
]);
10130 for (i
= 0; i
< ginst
->type_argc
; ++i
)
10131 hashes
[hindex
++] = mono_aot_type_hash (ginst
->type_argv
[i
]);
10133 g_assert (hindex
== hashes_count
);
10135 /* Setup internal state */
10136 a
= b
= c
= 0xdeadbeef + (((guint32
)hashes_count
)<<2);
10138 /* Handle most of the hashes */
10139 while (hashes_count
> 3) {
10148 /* Handle the last 3 hashes (all the case statements fall through) */
10149 switch (hashes_count
) {
10150 case 3 : c
+= hashes
[2];
10151 case 2 : b
+= hashes
[1];
10152 case 1 : a
+= hashes
[0];
10153 mono_final (a
,b
,c
);
10154 case 0: /* nothing left to add */
10158 g_free (hashes_start
);
10167 * mono_aot_get_array_helper_from_wrapper;
10169 * Get the helper method in Array called by an array wrapper method.
10172 mono_aot_get_array_helper_from_wrapper (MonoMethod
*method
)
10175 const char *prefix
;
10176 MonoGenericContext ctx
;
10177 MonoType
*args
[16];
10178 char *mname
, *iname
, *s
, *s2
, *helper_name
= NULL
;
10180 prefix
= "System.Collections.Generic";
10181 s
= g_strdup_printf ("%s", method
->name
+ strlen (prefix
) + 1);
10182 s2
= strstr (s
, "`1.");
10188 //printf ("X: %s %s\n", iname, mname);
10190 if (!strcmp (iname
, "IList"))
10191 helper_name
= g_strdup_printf ("InternalArray__%s", mname
);
10193 helper_name
= g_strdup_printf ("InternalArray__%s_%s", iname
, mname
);
10194 m
= get_method_nofail (mono_defaults
.array_class
, helper_name
, mono_method_signature_internal (method
)->param_count
, 0);
10196 g_free (helper_name
);
10199 if (m
->is_generic
) {
10200 ERROR_DECL (error
);
10201 memset (&ctx
, 0, sizeof (ctx
));
10202 args
[0] = m_class_get_byval_arg (m_class_get_element_class (method
->klass
));
10203 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
10204 m
= mono_class_inflate_generic_method_checked (m
, &ctx
, error
);
10205 g_assert (is_ok (error
)); /* FIXME don't swallow the error */
10211 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
10213 typedef struct HashEntry
{
10214 guint32 key
, value
, index
;
10215 struct HashEntry
*next
;
10219 * emit_extra_methods:
10221 * Emit methods which are not in the METHOD table, like wrappers.
10224 emit_extra_methods (MonoAotCompile
*acfg
)
10226 int i
, table_size
, buf_size
;
10228 guint32
*info_offsets
;
10231 HashEntry
*entry
, *new_entry
;
10232 int nmethods
, max_chain_length
;
10233 int *chain_lengths
;
10235 info_offsets
= g_new0 (guint32
, acfg
->extra_methods
->len
);
10237 /* Emit method info */
10239 for (i
= 0; i
< acfg
->extra_methods
->len
; ++i
) {
10240 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (acfg
->extra_methods
, i
);
10241 MonoCompile
*cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, method
);
10243 if (ignore_cfg (cfg
))
10247 p
= buf
= (guint8
*)g_malloc (buf_size
);
10251 method
= cfg
->method_to_register
;
10253 encode_method_ref (acfg
, method
, p
, &p
);
10255 g_assert ((p
- buf
) < buf_size
);
10257 info_offsets
[i
] = add_to_blob (acfg
, buf
, p
- buf
);
10262 * Construct a chained hash table for mapping indexes in extra_method_info to
10265 table_size
= g_spaced_primes_closest ((int)(nmethods
* 1.5));
10266 table
= g_ptr_array_sized_new (table_size
);
10267 for (i
= 0; i
< table_size
; ++i
)
10268 g_ptr_array_add (table
, NULL
);
10269 chain_lengths
= g_new0 (int, table_size
);
10270 max_chain_length
= 0;
10271 for (i
= 0; i
< acfg
->extra_methods
->len
; ++i
) {
10272 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (acfg
->extra_methods
, i
);
10273 MonoCompile
*cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, method
);
10274 guint32 key
, value
;
10276 if (ignore_cfg (cfg
))
10279 key
= info_offsets
[i
];
10280 value
= get_method_index (acfg
, method
);
10282 hash
= mono_aot_method_hash (method
) % table_size
;
10283 //printf ("X: %s %x\n", mono_method_get_full_name (method), mono_aot_method_hash (method));
10285 chain_lengths
[hash
] ++;
10286 max_chain_length
= MAX (max_chain_length
, chain_lengths
[hash
]);
10288 new_entry
= (HashEntry
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (HashEntry
));
10289 new_entry
->key
= key
;
10290 new_entry
->value
= value
;
10292 entry
= (HashEntry
*)g_ptr_array_index (table
, hash
);
10293 if (entry
== NULL
) {
10294 new_entry
->index
= hash
;
10295 g_ptr_array_index (table
, hash
) = new_entry
;
10297 while (entry
->next
)
10298 entry
= entry
->next
;
10300 entry
->next
= new_entry
;
10301 new_entry
->index
= table
->len
;
10302 g_ptr_array_add (table
, new_entry
);
10305 g_free (chain_lengths
);
10307 //printf ("MAX: %d\n", max_chain_length);
10309 buf_size
= table
->len
* 12 + 4;
10310 p
= buf
= (guint8
*)g_malloc (buf_size
);
10311 encode_int (table_size
, p
, &p
);
10313 for (i
= 0; i
< table
->len
; ++i
) {
10314 HashEntry
*entry
= (HashEntry
*)g_ptr_array_index (table
, i
);
10316 if (entry
== NULL
) {
10317 encode_int (0, p
, &p
);
10318 encode_int (0, p
, &p
);
10319 encode_int (0, p
, &p
);
10321 //g_assert (entry->key > 0);
10322 encode_int (entry
->key
, p
, &p
);
10323 encode_int (entry
->value
, p
, &p
);
10325 encode_int (entry
->next
->index
, p
, &p
);
10327 encode_int (0, p
, &p
);
10330 g_assert (p
- buf
<= buf_size
);
10332 /* Emit the table */
10333 emit_aot_data (acfg
, MONO_AOT_TABLE_EXTRA_METHOD_TABLE
, "extra_method_table", buf
, p
- buf
);
10338 * Emit a table reverse mapping method indexes to their index in extra_method_info.
10339 * This is used by mono_aot_find_jit_info ().
10341 buf_size
= acfg
->extra_methods
->len
* 8 + 4;
10342 p
= buf
= (guint8
*)g_malloc (buf_size
);
10343 encode_int (acfg
->extra_methods
->len
, p
, &p
);
10344 for (i
= 0; i
< acfg
->extra_methods
->len
; ++i
) {
10345 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (acfg
->extra_methods
, i
);
10347 encode_int (get_method_index (acfg
, method
), p
, &p
);
10348 encode_int (info_offsets
[i
], p
, &p
);
10350 emit_aot_data (acfg
, MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS
, "extra_method_info_offsets", buf
, p
- buf
);
10353 g_free (info_offsets
);
10354 g_ptr_array_free (table
, TRUE
);
10358 generate_aotid (guint8
* aotid
)
10360 gpointer rand_handle
;
10361 ERROR_DECL (error
);
10364 rand_handle
= mono_rand_init (NULL
, 0);
10366 mono_rand_try_get_bytes (&rand_handle
, aotid
, 16, error
);
10367 mono_error_assert_ok (error
);
10369 mono_rand_close (rand_handle
);
10373 emit_exception_info (MonoAotCompile
*acfg
)
10377 SeqPointData sp_data
;
10378 gboolean seq_points_to_file
= FALSE
;
10380 offsets
= g_new0 (gint32
, acfg
->nmethods
);
10381 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
10382 if (acfg
->cfgs
[i
]) {
10383 MonoCompile
*cfg
= acfg
->cfgs
[i
];
10385 // By design aot-runtime decode_exception_debug_info is not able to load sequence point debug data from a file.
10386 // As it is not possible to load debug data from a file its is also not possible to store it in a file.
10387 gboolean method_seq_points_to_file
= acfg
->aot_opts
.gen_msym_dir
&&
10388 cfg
->gen_seq_points
&& !cfg
->gen_sdb_seq_points
;
10389 gboolean method_seq_points_to_binary
= cfg
->gen_seq_points
&& !method_seq_points_to_file
;
10391 emit_exception_debug_info (acfg
, cfg
, method_seq_points_to_binary
);
10392 offsets
[i
] = cfg
->ex_info_offset
;
10394 if (method_seq_points_to_file
) {
10395 if (!seq_points_to_file
) {
10396 mono_seq_point_data_init (&sp_data
, acfg
->nmethods
);
10397 seq_points_to_file
= TRUE
;
10399 mono_seq_point_data_add (&sp_data
, cfg
->method
->token
, cfg
->method_index
, cfg
->seq_point_info
);
10406 if (seq_points_to_file
) {
10407 char *aotid
= mono_guid_to_string_minimal (acfg
->image
->aotid
);
10408 char *dir
= g_build_filename (acfg
->aot_opts
.gen_msym_dir_path
, aotid
, (const char*)NULL
);
10409 char *image_basename
= g_path_get_basename (acfg
->image
->name
);
10410 char *aot_file
= g_strdup_printf("%s%s", image_basename
, SEQ_POINT_AOT_EXT
);
10411 char *aot_file_path
= g_build_filename (dir
, aot_file
, (const char*)NULL
);
10413 if (g_ensure_directory_exists (aot_file_path
) == FALSE
) {
10414 fprintf (stderr
, "AOT : failed to create msym directory: %s\n", aot_file_path
);
10418 mono_seq_point_data_write (&sp_data
, aot_file_path
);
10419 mono_seq_point_data_free (&sp_data
);
10423 g_free (image_basename
);
10425 g_free (aot_file_path
);
10428 acfg
->stats
.offsets_size
+= emit_offset_table (acfg
, "ex_info_offsets", MONO_AOT_TABLE_EX_INFO_OFFSETS
, acfg
->nmethods
, 10, offsets
);
10433 emit_unwind_info (MonoAotCompile
*acfg
)
10438 if (acfg
->aot_opts
.llvm_only
) {
10439 g_assert (acfg
->unwind_ops
->len
== 0);
10444 * The unwind info contains a lot of duplicates so we emit each unique
10445 * entry once, and only store the offset from the start of the table in the
10449 sprintf (symbol
, "unwind_info");
10450 emit_section_change (acfg
, RODATA_SECT
, 1);
10451 emit_alignment (acfg
, 8);
10452 emit_info_symbol (acfg
, symbol
, TRUE
);
10454 for (i
= 0; i
< acfg
->unwind_ops
->len
; ++i
) {
10455 guint32 index
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->unwind_ops
, i
));
10456 guint8
*unwind_info
;
10457 guint32 unwind_info_len
;
10461 unwind_info
= mono_get_cached_unwind_info (index
, &unwind_info_len
);
10464 encode_value (unwind_info_len
, p
, &p
);
10465 emit_bytes (acfg
, buf
, p
- buf
);
10466 emit_bytes (acfg
, unwind_info
, unwind_info_len
);
10468 acfg
->stats
.unwind_info_size
+= (p
- buf
) + unwind_info_len
;
10473 emit_class_info (MonoAotCompile
*acfg
)
10478 offsets
= g_new0 (gint32
, acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
);
10479 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
)
10480 offsets
[i
] = emit_klass_info (acfg
, MONO_TOKEN_TYPE_DEF
| (i
+ 1));
10482 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
);
10486 typedef struct ClassNameTableEntry
{
10487 guint32 token
, index
;
10488 struct ClassNameTableEntry
*next
;
10489 } ClassNameTableEntry
;
10492 emit_class_name_table (MonoAotCompile
*acfg
)
10494 int i
, table_size
, buf_size
;
10495 guint32 token
, hash
;
10500 ClassNameTableEntry
*entry
, *new_entry
;
10503 * Construct a chained hash table for mapping class names to typedef tokens.
10505 table_size
= g_spaced_primes_closest ((int)(acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
* 1.5));
10506 table
= g_ptr_array_sized_new (table_size
);
10507 for (i
= 0; i
< table_size
; ++i
)
10508 g_ptr_array_add (table
, NULL
);
10509 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
) {
10510 ERROR_DECL (error
);
10511 token
= MONO_TOKEN_TYPE_DEF
| (i
+ 1);
10512 klass
= mono_class_get_checked (acfg
->image
, token
, error
);
10514 mono_error_cleanup (error
);
10517 full_name
= mono_type_get_name_full (m_class_get_byval_arg (klass
), MONO_TYPE_NAME_FORMAT_FULL_NAME
);
10518 hash
= mono_metadata_str_hash (full_name
) % table_size
;
10519 g_free (full_name
);
10521 /* FIXME: Allocate from the mempool */
10522 new_entry
= g_new0 (ClassNameTableEntry
, 1);
10523 new_entry
->token
= token
;
10525 entry
= (ClassNameTableEntry
*)g_ptr_array_index (table
, hash
);
10526 if (entry
== NULL
) {
10527 new_entry
->index
= hash
;
10528 g_ptr_array_index (table
, hash
) = new_entry
;
10530 while (entry
->next
)
10531 entry
= entry
->next
;
10533 entry
->next
= new_entry
;
10534 new_entry
->index
= table
->len
;
10535 g_ptr_array_add (table
, new_entry
);
10539 /* Emit the table */
10540 buf_size
= table
->len
* 4 + 4;
10541 p
= buf
= (guint8
*)g_malloc0 (buf_size
);
10543 /* FIXME: Optimize memory usage */
10544 g_assert (table_size
< 65000);
10545 encode_int16 (table_size
, p
, &p
);
10546 g_assert (table
->len
< 65000);
10547 for (i
= 0; i
< table
->len
; ++i
) {
10548 ClassNameTableEntry
*entry
= (ClassNameTableEntry
*)g_ptr_array_index (table
, i
);
10550 if (entry
== NULL
) {
10551 encode_int16 (0, p
, &p
);
10552 encode_int16 (0, p
, &p
);
10554 encode_int16 (mono_metadata_token_index (entry
->token
), p
, &p
);
10556 encode_int16 (entry
->next
->index
, p
, &p
);
10558 encode_int16 (0, p
, &p
);
10562 g_assert (p
- buf
<= buf_size
);
10563 g_ptr_array_free (table
, TRUE
);
10565 emit_aot_data (acfg
, MONO_AOT_TABLE_CLASS_NAME
, "class_name_table", buf
, p
- buf
);
10571 emit_image_table (MonoAotCompile
*acfg
)
10577 * The image table is small but referenced in a lot of places.
10578 * So we emit it at once, and reference its elements by an index.
10580 buf_size
= acfg
->image_table
->len
* 28 + 4;
10581 for (i
= 0; i
< acfg
->image_table
->len
; i
++) {
10582 MonoImage
*image
= (MonoImage
*)g_ptr_array_index (acfg
->image_table
, i
);
10583 MonoAssemblyName
*aname
= &image
->assembly
->aname
;
10585 buf_size
+= strlen (image
->assembly_name
) + strlen (image
->guid
) + (aname
->culture
? strlen (aname
->culture
) : 1) + strlen ((char*)aname
->public_key_token
) + 4;
10588 buf
= p
= (guint8
*)g_malloc0 (buf_size
);
10589 encode_int (acfg
->image_table
->len
, p
, &p
);
10590 for (i
= 0; i
< acfg
->image_table
->len
; i
++) {
10591 MonoImage
*image
= (MonoImage
*)g_ptr_array_index (acfg
->image_table
, i
);
10592 MonoAssemblyName
*aname
= &image
->assembly
->aname
;
10594 /* FIXME: Support multi-module assemblies */
10595 g_assert (image
->assembly
->image
== image
);
10597 encode_string (image
->assembly_name
, p
, &p
);
10598 encode_string (image
->guid
, p
, &p
);
10599 encode_string (aname
->culture
? aname
->culture
: "", p
, &p
);
10600 encode_string ((const char*)aname
->public_key_token
, p
, &p
);
10602 while (GPOINTER_TO_UINT (p
) % 8 != 0)
10605 encode_int (aname
->flags
, p
, &p
);
10606 encode_int (aname
->major
, p
, &p
);
10607 encode_int (aname
->minor
, p
, &p
);
10608 encode_int (aname
->build
, p
, &p
);
10609 encode_int (aname
->revision
, p
, &p
);
10611 g_assert (p
- buf
<= buf_size
);
10613 emit_aot_data (acfg
, MONO_AOT_TABLE_IMAGE_TABLE
, "image_table", buf
, p
- buf
);
10619 emit_weak_field_indexes (MonoAotCompile
*acfg
)
10621 GHashTable
*indexes
;
10622 GHashTableIter iter
;
10623 gpointer key
, value
;
10627 /* Emit a table of weak field indexes, since computing these at runtime is expensive */
10628 mono_assembly_init_weak_fields (acfg
->image
);
10629 indexes
= acfg
->image
->weak_field_indexes
;
10630 g_assert (indexes
);
10632 buf_size
= (g_hash_table_size (indexes
) + 1) * 4;
10633 buf
= p
= (guint8
*)g_malloc0 (buf_size
);
10635 encode_int (g_hash_table_size (indexes
), p
, &p
);
10636 g_hash_table_iter_init (&iter
, indexes
);
10637 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
10638 guint32 index
= GPOINTER_TO_UINT (key
);
10639 encode_int (index
, p
, &p
);
10641 g_assert (p
- buf
<= buf_size
);
10643 emit_aot_data (acfg
, MONO_AOT_TABLE_WEAK_FIELD_INDEXES
, "weak_field_indexes", buf
, p
- buf
);
10649 emit_got_info (MonoAotCompile
*acfg
, gboolean llvm
)
10651 int i
, first_plt_got_patch
= 0, buf_size
;
10653 guint32
*got_info_offsets
;
10654 GotInfo
*info
= llvm
? &acfg
->llvm_got_info
: &acfg
->got_info
;
10656 /* Add the patches needed by the PLT to the GOT */
10658 acfg
->plt_got_offset_base
= acfg
->got_offset
;
10659 first_plt_got_patch
= info
->got_patches
->len
;
10660 for (i
= 1; i
< acfg
->plt_offset
; ++i
) {
10661 MonoPltEntry
*plt_entry
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
10663 g_ptr_array_add (info
->got_patches
, plt_entry
->ji
);
10665 acfg
->stats
.got_slot_types
[plt_entry
->ji
->type
] ++;
10668 acfg
->got_offset
+= acfg
->plt_offset
;
10673 * - optimize offsets table.
10674 * - reduce number of exported symbols.
10675 * - emit info for a klass only once.
10676 * - determine when a method uses a GOT slot which is guaranteed to be already
10678 * - clean up and document the code.
10679 * - use String.Empty in class libs.
10682 /* Encode info required to decode shared GOT entries */
10683 buf_size
= info
->got_patches
->len
* 128;
10684 p
= buf
= (guint8
*)mono_mempool_alloc (acfg
->mempool
, buf_size
);
10685 got_info_offsets
= (guint32
*)mono_mempool_alloc (acfg
->mempool
, info
->got_patches
->len
* sizeof (guint32
));
10687 acfg
->plt_got_info_offsets
= (guint32
*)mono_mempool_alloc (acfg
->mempool
, acfg
->plt_offset
* sizeof (guint32
));
10689 if (acfg
->plt_offset
)
10690 acfg
->plt_got_info_offsets
[0] = 0;
10692 for (i
= 0; i
< info
->got_patches
->len
; ++i
) {
10693 MonoJumpInfo
*ji
= (MonoJumpInfo
*)g_ptr_array_index (info
->got_patches
, i
);
10698 encode_value (ji
->type
, p
, &p
);
10700 encode_patch (acfg
, ji
, p
, &p
);
10701 acfg
->stats
.got_slot_info_sizes
[ji
->type
] += p
- p2
;
10702 g_assert (p
- buf
<= buf_size
);
10703 got_info_offsets
[i
] = add_to_blob (acfg
, buf
, p
- buf
);
10705 if (!llvm
&& i
>= first_plt_got_patch
)
10706 acfg
->plt_got_info_offsets
[i
- first_plt_got_patch
+ 1] = got_info_offsets
[i
];
10707 acfg
->stats
.got_info_size
+= p
- buf
;
10710 /* Emit got_info_offsets table */
10712 /* No need to emit offsets for the got plt entries, the plt embeds them directly */
10713 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
);
10717 emit_got (MonoAotCompile
*acfg
)
10719 char symbol
[MAX_SYMBOL_SIZE
];
10721 if (acfg
->aot_opts
.llvm_only
)
10724 /* Don't make GOT global so accesses to it don't need relocations */
10725 sprintf (symbol
, "%s", acfg
->got_symbol
);
10728 emit_unset_mode (acfg
);
10729 fprintf (acfg
->fp
, ".section __DATA, __bss\n");
10730 emit_alignment (acfg
, 8);
10732 emit_info_symbol (acfg
, "jit_got", FALSE
);
10733 fprintf (acfg
->fp
, ".lcomm %s, %d\n", acfg
->got_symbol
, (int)(acfg
->got_offset
* sizeof (target_mgreg_t
)));
10735 emit_section_change (acfg
, ".bss", 0);
10736 emit_alignment (acfg
, 8);
10737 if (acfg
->aot_opts
.write_symbols
)
10738 emit_local_symbol (acfg
, symbol
, "got_end", FALSE
);
10739 emit_label (acfg
, symbol
);
10741 emit_info_symbol (acfg
, "jit_got", FALSE
);
10742 if (acfg
->got_offset
> 0)
10743 emit_zero_bytes (acfg
, (int)(acfg
->got_offset
* sizeof (target_mgreg_t
)));
10746 sprintf (symbol
, "got_end");
10747 emit_label (acfg
, symbol
);
10750 typedef struct GlobalsTableEntry
{
10751 guint32 value
, index
;
10752 struct GlobalsTableEntry
*next
;
10753 } GlobalsTableEntry
;
10755 #ifdef TARGET_WIN32_MSVC
10756 #define DLL_ENTRY_POINT "DllMain"
10759 emit_library_info (MonoAotCompile
*acfg
)
10761 // Only include for shared libraries linked directly from generated object.
10762 if (link_shared_library (acfg
)) {
10764 char symbol
[MAX_SYMBOL_SIZE
];
10766 // Ask linker to export all global symbols.
10767 emit_section_change (acfg
, ".drectve", 0);
10768 for (guint i
= 0; i
< acfg
->globals
->len
; ++i
) {
10769 name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
10770 g_assert (name
!= NULL
);
10771 sprintf_s (symbol
, MAX_SYMBOL_SIZE
, " /EXPORT:%s", name
);
10772 emit_string (acfg
, symbol
);
10775 // Emit DLLMain function, needed by MSVC linker for DLL's.
10776 // NOTE, DllMain should not go into exports above.
10777 emit_section_change (acfg
, ".text", 0);
10778 emit_global (acfg
, DLL_ENTRY_POINT
, TRUE
);
10779 emit_label (acfg
, DLL_ENTRY_POINT
);
10781 // Simple implementation of DLLMain, just returning TRUE.
10782 // For more information about DLLMain: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx
10783 fprintf (acfg
->fp
, "movl $1, %%eax\n");
10784 fprintf (acfg
->fp
, "ret\n");
10786 // Inform linker about our dll entry function.
10787 emit_section_change (acfg
, ".drectve", 0);
10788 emit_string (acfg
, "/ENTRY:" DLL_ENTRY_POINT
);
10796 emit_library_info (MonoAotCompile
*acfg
)
10803 emit_globals (MonoAotCompile
*acfg
)
10808 char symbol
[1024];
10809 GlobalsTableEntry
*entry
, *new_entry
;
10811 if (!acfg
->aot_opts
.static_link
)
10814 if (acfg
->aot_opts
.llvm_only
) {
10815 g_assert (acfg
->globals
->len
== 0);
10820 * When static linking, we emit a table containing our globals.
10824 * Construct a chained hash table for mapping global names to their index in
10825 * the globals table.
10827 table_size
= g_spaced_primes_closest ((int)(acfg
->globals
->len
* 1.5));
10828 table
= g_ptr_array_sized_new (table_size
);
10829 for (i
= 0; i
< table_size
; ++i
)
10830 g_ptr_array_add (table
, NULL
);
10831 for (i
= 0; i
< acfg
->globals
->len
; ++i
) {
10832 char *name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
10834 hash
= mono_metadata_str_hash (name
) % table_size
;
10836 /* FIXME: Allocate from the mempool */
10837 new_entry
= g_new0 (GlobalsTableEntry
, 1);
10838 new_entry
->value
= i
;
10840 entry
= (GlobalsTableEntry
*)g_ptr_array_index (table
, hash
);
10841 if (entry
== NULL
) {
10842 new_entry
->index
= hash
;
10843 g_ptr_array_index (table
, hash
) = new_entry
;
10845 while (entry
->next
)
10846 entry
= entry
->next
;
10848 entry
->next
= new_entry
;
10849 new_entry
->index
= table
->len
;
10850 g_ptr_array_add (table
, new_entry
);
10854 /* Emit the table */
10855 sprintf (symbol
, ".Lglobals_hash");
10856 emit_section_change (acfg
, RODATA_SECT
, 0);
10857 emit_alignment (acfg
, 8);
10858 emit_label (acfg
, symbol
);
10860 /* FIXME: Optimize memory usage */
10861 g_assert (table_size
< 65000);
10862 emit_int16 (acfg
, table_size
);
10863 for (i
= 0; i
< table
->len
; ++i
) {
10864 GlobalsTableEntry
*entry
= (GlobalsTableEntry
*)g_ptr_array_index (table
, i
);
10866 if (entry
== NULL
) {
10867 emit_int16 (acfg
, 0);
10868 emit_int16 (acfg
, 0);
10870 emit_int16 (acfg
, entry
->value
+ 1);
10872 emit_int16 (acfg
, entry
->next
->index
);
10874 emit_int16 (acfg
, 0);
10878 /* Emit the names */
10879 for (i
= 0; i
< acfg
->globals
->len
; ++i
) {
10880 char *name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
10882 sprintf (symbol
, "name_%d", i
);
10883 emit_section_change (acfg
, RODATA_SECT
, 1);
10885 emit_alignment (acfg
, 4);
10887 emit_label (acfg
, symbol
);
10888 emit_string (acfg
, name
);
10891 /* Emit the globals table */
10892 sprintf (symbol
, "globals");
10893 emit_section_change (acfg
, ".data", 0);
10894 /* This is not a global, since it is accessed by the init function */
10895 emit_alignment (acfg
, 8);
10896 emit_info_symbol (acfg
, symbol
, FALSE
);
10898 sprintf (symbol
, "%sglobals_hash", acfg
->temp_prefix
);
10899 emit_pointer (acfg
, symbol
);
10901 for (i
= 0; i
< acfg
->globals
->len
; ++i
) {
10902 char *name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
10904 sprintf (symbol
, "name_%d", i
);
10905 emit_pointer (acfg
, symbol
);
10907 g_assert (strlen (name
) < sizeof (symbol
));
10908 sprintf (symbol
, "%s", name
);
10909 emit_pointer (acfg
, symbol
);
10911 /* Null terminate the table */
10912 emit_int32 (acfg
, 0);
10913 emit_int32 (acfg
, 0);
10917 emit_mem_end (MonoAotCompile
*acfg
)
10921 if (acfg
->aot_opts
.llvm_only
)
10924 sprintf (symbol
, "mem_end");
10925 emit_section_change (acfg
, ".text", 1);
10926 emit_alignment_code (acfg
, 8);
10927 emit_label (acfg
, symbol
);
10931 init_aot_file_info (MonoAotCompile
*acfg
, MonoAotFileInfo
*info
)
10935 info
->version
= MONO_AOT_FILE_VERSION
;
10936 info
->plt_got_offset_base
= acfg
->plt_got_offset_base
;
10937 info
->got_size
= acfg
->got_offset
* sizeof (target_mgreg_t
);
10938 info
->plt_size
= acfg
->plt_offset
;
10939 info
->nmethods
= acfg
->nmethods
;
10940 info
->nextra_methods
= acfg
->nextra_methods
;
10941 info
->flags
= acfg
->flags
;
10942 info
->opts
= acfg
->opts
;
10943 info
->simd_opts
= acfg
->simd_opts
;
10944 info
->gc_name_index
= acfg
->gc_name_offset
;
10945 info
->datafile_size
= acfg
->datafile_offset
;
10946 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
10947 info
->table_offsets
[i
] = acfg
->table_offsets
[i
];
10948 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
10949 info
->num_trampolines
[i
] = acfg
->num_trampolines
[i
];
10950 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
10951 info
->trampoline_got_offset_base
[i
] = acfg
->trampoline_got_offset_base
[i
];
10952 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
10953 info
->trampoline_size
[i
] = acfg
->trampoline_size
[i
];
10954 info
->num_rgctx_fetch_trampolines
= acfg
->aot_opts
.nrgctx_fetch_trampolines
;
10956 int card_table_shift_bits
= 0;
10957 target_mgreg_t card_table_mask
= 0;
10959 mono_gc_get_target_card_table (&card_table_shift_bits
, &card_table_mask
);
10962 * Sanity checking variables used to make sure the host and target
10963 * environment matches when cross compiling.
10965 info
->double_align
= MONO_ABI_ALIGNOF (double);
10966 info
->long_align
= MONO_ABI_ALIGNOF (gint64
);
10967 info
->generic_tramp_num
= MONO_TRAMPOLINE_NUM
;
10968 info
->card_table_shift_bits
= card_table_shift_bits
;
10969 info
->card_table_mask
= card_table_mask
;
10971 info
->tramp_page_size
= acfg
->tramp_page_size
;
10972 info
->nshared_got_entries
= acfg
->nshared_got_entries
;
10973 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
10974 info
->tramp_page_code_offsets
[i
] = acfg
->tramp_page_code_offsets
[i
];
10976 memcpy(&info
->aotid
, acfg
->image
->aotid
, 16);
10980 emit_aot_file_info (MonoAotCompile
*acfg
, MonoAotFileInfo
*info
)
10982 char symbol
[MAX_SYMBOL_SIZE
];
10984 const char **symbols
;
10986 symbols
= g_new0 (const char *, MONO_AOT_FILE_INFO_NUM_SYMBOLS
);
10988 symbols
[sindex
++] = acfg
->got_symbol
;
10990 symbols
[sindex
++] = g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, acfg
->llvm_got_symbol
);
10991 symbols
[sindex
++] = acfg
->llvm_eh_frame_symbol
;
10993 symbols
[sindex
++] = NULL
;
10994 symbols
[sindex
++] = NULL
;
10996 /* llvm_get_method */
10997 symbols
[sindex
++] = NULL
;
10998 /* llvm_get_unbox_tramp */
10999 symbols
[sindex
++] = NULL
;
11000 if (!acfg
->aot_opts
.llvm_only
) {
11001 symbols
[sindex
++] = "jit_code_start";
11002 symbols
[sindex
++] = "jit_code_end";
11003 symbols
[sindex
++] = "method_addresses";
11005 symbols
[sindex
++] = NULL
;
11006 symbols
[sindex
++] = NULL
;
11007 symbols
[sindex
++] = NULL
;
11009 symbols
[sindex
++] = NULL
;
11010 symbols
[sindex
++] = NULL
;
11012 if (acfg
->data_outfile
) {
11013 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
11014 symbols
[sindex
++] = NULL
;
11016 symbols
[sindex
++] = "blob";
11017 symbols
[sindex
++] = "class_name_table";
11018 symbols
[sindex
++] = "class_info_offsets";
11019 symbols
[sindex
++] = "method_info_offsets";
11020 symbols
[sindex
++] = "ex_info_offsets";
11021 symbols
[sindex
++] = "extra_method_info_offsets";
11022 symbols
[sindex
++] = "extra_method_table";
11023 symbols
[sindex
++] = "got_info_offsets";
11025 symbols
[sindex
++] = "llvm_got_info_offsets";
11027 symbols
[sindex
++] = NULL
;
11028 symbols
[sindex
++] = "image_table";
11029 symbols
[sindex
++] = "weak_field_indexes";
11032 symbols
[sindex
++] = "mem_end";
11033 symbols
[sindex
++] = "assembly_guid";
11034 symbols
[sindex
++] = "runtime_version";
11035 if (acfg
->num_trampoline_got_entries
) {
11036 symbols
[sindex
++] = "specific_trampolines";
11037 symbols
[sindex
++] = "static_rgctx_trampolines";
11038 symbols
[sindex
++] = "imt_trampolines";
11039 symbols
[sindex
++] = "gsharedvt_arg_trampolines";
11040 symbols
[sindex
++] = "ftnptr_arg_trampolines";
11041 symbols
[sindex
++] = "unbox_arbitrary_trampolines";
11043 symbols
[sindex
++] = NULL
;
11044 symbols
[sindex
++] = NULL
;
11045 symbols
[sindex
++] = NULL
;
11046 symbols
[sindex
++] = NULL
;
11047 symbols
[sindex
++] = NULL
;
11048 symbols
[sindex
++] = NULL
;
11050 if (acfg
->aot_opts
.static_link
) {
11051 symbols
[sindex
++] = "globals";
11053 symbols
[sindex
++] = NULL
;
11055 symbols
[sindex
++] = "assembly_name";
11056 symbols
[sindex
++] = "plt";
11057 symbols
[sindex
++] = "plt_end";
11058 symbols
[sindex
++] = "unwind_info";
11059 if (!acfg
->aot_opts
.llvm_only
) {
11060 symbols
[sindex
++] = "unbox_trampolines";
11061 symbols
[sindex
++] = "unbox_trampolines_end";
11062 symbols
[sindex
++] = "unbox_trampoline_addresses";
11064 symbols
[sindex
++] = NULL
;
11065 symbols
[sindex
++] = NULL
;
11066 symbols
[sindex
++] = NULL
;
11069 g_assert (sindex
== MONO_AOT_FILE_INFO_NUM_SYMBOLS
);
11071 sprintf (symbol
, "%smono_aot_file_info", acfg
->user_symbol_prefix
);
11072 emit_section_change (acfg
, ".data", 0);
11073 emit_alignment (acfg
, 8);
11074 emit_label (acfg
, symbol
);
11075 if (!acfg
->aot_opts
.static_link
)
11076 emit_global (acfg
, symbol
, FALSE
);
11078 /* The data emitted here must match MonoAotFileInfo. */
11080 emit_int32 (acfg
, info
->version
);
11081 emit_int32 (acfg
, info
->dummy
);
11084 * We emit pointers to our data structures instead of emitting global symbols which
11085 * point to them, to reduce the number of globals, and because using globals leads to
11086 * various problems (i.e. arm/thumb).
11088 for (i
= 0; i
< MONO_AOT_FILE_INFO_NUM_SYMBOLS
; ++i
)
11089 emit_pointer (acfg
, symbols
[i
]);
11091 emit_int32 (acfg
, info
->plt_got_offset_base
);
11092 emit_int32 (acfg
, info
->got_size
);
11093 emit_int32 (acfg
, info
->plt_size
);
11094 emit_int32 (acfg
, info
->nmethods
);
11095 emit_int32 (acfg
, info
->nextra_methods
);
11096 emit_int32 (acfg
, info
->flags
);
11097 emit_int32 (acfg
, info
->opts
);
11098 emit_int32 (acfg
, info
->simd_opts
);
11099 emit_int32 (acfg
, info
->gc_name_index
);
11100 emit_int32 (acfg
, info
->num_rgctx_fetch_trampolines
);
11101 emit_int32 (acfg
, info
->double_align
);
11102 emit_int32 (acfg
, info
->long_align
);
11103 emit_int32 (acfg
, info
->generic_tramp_num
);
11104 emit_int32 (acfg
, info
->card_table_shift_bits
);
11105 emit_int32 (acfg
, info
->card_table_mask
);
11106 emit_int32 (acfg
, info
->tramp_page_size
);
11107 emit_int32 (acfg
, info
->nshared_got_entries
);
11108 emit_int32 (acfg
, info
->datafile_size
);
11109 emit_int32 (acfg
, 0);
11110 emit_int32 (acfg
, 0);
11112 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
11113 emit_int32 (acfg
, info
->table_offsets
[i
]);
11114 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11115 emit_int32 (acfg
, info
->num_trampolines
[i
]);
11116 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11117 emit_int32 (acfg
, info
->trampoline_got_offset_base
[i
]);
11118 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11119 emit_int32 (acfg
, info
->trampoline_size
[i
]);
11120 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
11121 emit_int32 (acfg
, info
->tramp_page_code_offsets
[i
]);
11123 emit_bytes (acfg
, info
->aotid
, 16);
11125 if (acfg
->aot_opts
.static_link
) {
11126 emit_global_inner (acfg
, acfg
->static_linking_symbol
, FALSE
);
11127 emit_alignment (acfg
, sizeof (target_mgreg_t
));
11128 emit_label (acfg
, acfg
->static_linking_symbol
);
11129 emit_pointer_2 (acfg
, acfg
->user_symbol_prefix
, "mono_aot_file_info");
11134 * Emit a structure containing all the information not stored elsewhere.
11137 emit_file_info (MonoAotCompile
*acfg
)
11140 MonoAotFileInfo
*info
;
11142 if (acfg
->aot_opts
.bind_to_runtime_version
) {
11143 build_info
= mono_get_runtime_build_info ();
11144 emit_string_symbol (acfg
, "runtime_version", build_info
);
11145 g_free (build_info
);
11147 emit_string_symbol (acfg
, "runtime_version", "");
11150 emit_string_symbol (acfg
, "assembly_guid" , acfg
->image
->guid
);
11152 /* Emit a string holding the assembly name */
11153 emit_string_symbol (acfg
, "assembly_name", acfg
->image
->assembly
->aname
.name
);
11155 info
= g_new0 (MonoAotFileInfo
, 1);
11156 init_aot_file_info (acfg
, info
);
11158 if (acfg
->aot_opts
.static_link
) {
11159 char symbol
[MAX_SYMBOL_SIZE
];
11163 * Emit a global symbol which can be passed by an embedding app to
11164 * mono_aot_register_module (). The symbol points to a pointer to the the file info
11167 sprintf (symbol
, "%smono_aot_module_%s_info", acfg
->user_symbol_prefix
, acfg
->image
->assembly
->aname
.name
);
11169 /* Get rid of characters which cannot occur in symbols */
11171 for (p
= symbol
; *p
; ++p
) {
11172 if (!(isalnum (*p
) || *p
== '_'))
11175 acfg
->static_linking_symbol
= g_strdup (symbol
);
11179 mono_llvm_emit_aot_file_info (info
, acfg
->has_jitted_code
);
11181 emit_aot_file_info (acfg
, info
);
11185 emit_blob (MonoAotCompile
*acfg
)
11187 acfg
->blob_closed
= TRUE
;
11189 emit_aot_data (acfg
, MONO_AOT_TABLE_BLOB
, "blob", (guint8
*)acfg
->blob
.data
, acfg
->blob
.index
);
11193 emit_objc_selectors (MonoAotCompile
*acfg
)
11198 if (!acfg
->objc_selectors
|| acfg
->objc_selectors
->len
== 0)
11203 * cat > foo.m << EOF
11206 * return @selector(print:);
11211 mono_img_writer_emit_unset_mode (acfg
->w
);
11212 g_assert (acfg
->fp
);
11213 fprintf (acfg
->fp
, ".section __DATA,__objc_selrefs,literal_pointers,no_dead_strip\n");
11214 fprintf (acfg
->fp
, ".align 3\n");
11215 for (i
= 0; i
< acfg
->objc_selectors
->len
; ++i
) {
11216 sprintf (symbol
, "L_OBJC_SELECTOR_REFERENCES_%d", i
);
11217 emit_label (acfg
, symbol
);
11218 sprintf (symbol
, "L_OBJC_METH_VAR_NAME_%d", i
);
11219 emit_pointer (acfg
, symbol
);
11222 fprintf (acfg
->fp
, ".section __TEXT,__cstring,cstring_literals\n");
11223 for (i
= 0; i
< acfg
->objc_selectors
->len
; ++i
) {
11224 fprintf (acfg
->fp
, "L_OBJC_METH_VAR_NAME_%d:\n", i
);
11225 fprintf (acfg
->fp
, ".asciz \"%s\"\n", (char*)g_ptr_array_index (acfg
->objc_selectors
, i
));
11228 fprintf (acfg
->fp
, ".section __DATA,__objc_imageinfo,regular,no_dead_strip\n");
11229 fprintf (acfg
->fp
, ".align 3\n");
11230 fprintf (acfg
->fp
, "L_OBJC_IMAGE_INFO:\n");
11231 fprintf (acfg
->fp
, ".long 0\n");
11232 fprintf (acfg
->fp
, ".long 16\n");
11236 emit_dwarf_info (MonoAotCompile
*acfg
)
11238 #ifdef EMIT_DWARF_INFO
11240 char symbol2
[128];
11242 /* DIEs for methods */
11243 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
11244 MonoCompile
*cfg
= acfg
->cfgs
[i
];
11246 if (ignore_cfg (cfg
))
11249 // FIXME: LLVM doesn't define .Lme_...
11250 if (cfg
->compile_llvm
)
11253 sprintf (symbol2
, "%sme_%x", acfg
->temp_prefix
, i
);
11255 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 ()));
11260 #ifdef EMIT_WIN32_CODEVIEW_INFO
11261 typedef struct _CodeViewSubSectionData
11263 gchar
*start_section
;
11264 gchar
*end_section
;
11265 gchar
*start_section_record
;
11266 gchar
*end_section_record
;
11268 int section_record_type
;
11270 } CodeViewSubsectionData
;
11272 typedef struct _CodeViewCompilerVersion
11278 } CodeViewCompilerVersion
;
11280 #define CODEVIEW_SUBSECTION_SYMBOL_TYPE 0xF1
11281 #define CODEVIEW_SUBSECTION_RECORD_COMPILER_TYPE 0x113c
11282 #define CODEVIEW_SUBSECTION_RECORD_FUNCTION_START_TYPE 0x1147
11283 #define CODEVIEW_SUBSECTION_RECORD_FUNCTION_END_TYPE 0x114F
11284 #define CODEVIEW_CSHARP_LANGUAGE_TYPE 0x0A
11285 #define CODEVIEW_CPU_TYPE 0x0
11286 #define CODEVIEW_MAGIC_HEADER 0x4
11289 codeview_clear_subsection_data (CodeViewSubsectionData
*section_data
)
11291 g_free (section_data
->start_section
);
11292 g_free (section_data
->end_section
);
11293 g_free (section_data
->start_section_record
);
11294 g_free (section_data
->end_section_record
);
11296 memset (section_data
, 0, sizeof (CodeViewSubsectionData
));
11300 codeview_parse_compiler_version (gchar
*version
, CodeViewCompilerVersion
*data
)
11302 gint values
[4] = { 0 };
11303 gint
*value
= values
;
11305 while (*version
&& (value
< values
+ G_N_ELEMENTS (values
))) {
11306 if (isdigit (*version
)) {
11308 *value
+= *version
- '0';
11310 else if (*version
== '.') {
11317 data
->major
= values
[0];
11318 data
->minor
= values
[1];
11319 data
->revision
= values
[2];
11320 data
->patch
= values
[3];
11324 emit_codeview_start_subsection (MonoAotCompile
*acfg
, int section_id
, int section_type
, int section_record_type
, CodeViewSubsectionData
*section_data
)
11326 // Starting a new subsection, clear old data.
11327 codeview_clear_subsection_data (section_data
);
11329 // Keep subsection data.
11330 section_data
->section_id
= section_id
;
11331 section_data
->section_type
= section_type
;
11332 section_data
->section_record_type
= section_record_type
;
11334 // Allocate all labels used in subsection.
11335 section_data
->start_section
= g_strdup_printf ("%scvs_%d", acfg
->temp_prefix
, section_data
->section_id
);
11336 section_data
->end_section
= g_strdup_printf ("%scvse_%d", acfg
->temp_prefix
, section_data
->section_id
);
11337 section_data
->start_section_record
= g_strdup_printf ("%scvsr_%d", acfg
->temp_prefix
, section_data
->section_id
);
11338 section_data
->end_section_record
= g_strdup_printf ("%scvsre_%d", acfg
->temp_prefix
, section_data
->section_id
);
11340 // Subsection type, function symbol.
11341 emit_int32 (acfg
, section_data
->section_type
);
11343 // Subsection size.
11344 emit_symbol_diff (acfg
, section_data
->end_section
, section_data
->start_section
, 0);
11345 emit_label (acfg
, section_data
->start_section
);
11347 // Subsection record size.
11348 fprintf (acfg
->fp
, "\t.word %s - %s\n", section_data
->end_section_record
, section_data
->start_section_record
);
11349 emit_label (acfg
, section_data
->start_section_record
);
11351 // Subsection record type.
11352 emit_int16 (acfg
, section_record_type
);
11356 emit_codeview_end_subsection (MonoAotCompile
*acfg
, CodeViewSubsectionData
*section_data
, int *section_id
)
11358 g_assert (section_data
->start_section
);
11359 g_assert (section_data
->end_section
);
11360 g_assert (section_data
->start_section_record
);
11361 g_assert (section_data
->end_section_record
);
11363 emit_label (acfg
, section_data
->end_section_record
);
11365 if (section_data
->section_record_type
== CODEVIEW_SUBSECTION_RECORD_FUNCTION_START_TYPE
) {
11366 // Emit record length.
11367 emit_int16 (acfg
, 2);
11369 // Emit specific record type end.
11370 emit_int16 (acfg
, CODEVIEW_SUBSECTION_RECORD_FUNCTION_END_TYPE
);
11373 emit_label (acfg
, section_data
->end_section
);
11375 // Next subsection needs to be 4 byte aligned.
11376 emit_alignment (acfg
, 4);
11378 *section_id
= section_data
->section_id
+ 1;
11379 codeview_clear_subsection_data (section_data
);
11383 emit_codeview_start_symbol_subsection (MonoAotCompile
*acfg
, int section_id
, int section_record_type
, CodeViewSubsectionData
*section_data
)
11385 emit_codeview_start_subsection (acfg
, section_id
, CODEVIEW_SUBSECTION_SYMBOL_TYPE
, section_record_type
, section_data
);
11389 emit_codeview_end_symbol_subsection (MonoAotCompile
*acfg
, CodeViewSubsectionData
*section_data
, int *section_id
)
11391 emit_codeview_end_subsection (acfg
, section_data
, section_id
);
11395 emit_codeview_compiler_info (MonoAotCompile
*acfg
, int *section_id
)
11397 CodeViewSubsectionData section_data
= { 0 };
11398 CodeViewCompilerVersion compiler_version
= { 0 };
11400 // Start new compiler record subsection.
11401 emit_codeview_start_symbol_subsection (acfg
, *section_id
, CODEVIEW_SUBSECTION_RECORD_COMPILER_TYPE
, §ion_data
);
11403 emit_int32 (acfg
, CODEVIEW_CSHARP_LANGUAGE_TYPE
);
11404 emit_int16 (acfg
, CODEVIEW_CPU_TYPE
);
11406 // Get compiler version information.
11407 codeview_parse_compiler_version (VERSION
, &compiler_version
);
11409 // Compiler frontend version, 4 digits.
11410 emit_int16 (acfg
, compiler_version
.major
);
11411 emit_int16 (acfg
, compiler_version
.minor
);
11412 emit_int16 (acfg
, compiler_version
.revision
);
11413 emit_int16 (acfg
, compiler_version
.patch
);
11415 // Compiler backend version, 4 digits (currently same as frontend).
11416 emit_int16 (acfg
, compiler_version
.major
);
11417 emit_int16 (acfg
, compiler_version
.minor
);
11418 emit_int16 (acfg
, compiler_version
.revision
);
11419 emit_int16 (acfg
, compiler_version
.patch
);
11421 // Compiler string.
11422 emit_string (acfg
, "Mono AOT compiler");
11424 // Done with section.
11425 emit_codeview_end_symbol_subsection (acfg
, §ion_data
, section_id
);
11429 emit_codeview_function_info (MonoAotCompile
*acfg
, MonoMethod
*method
, int *section_id
, gchar
*symbol
, gchar
*symbol_start
, gchar
*symbol_end
)
11431 CodeViewSubsectionData section_data
= { 0 };
11432 gchar
*full_method_name
= NULL
;
11434 // Start new function record subsection.
11435 emit_codeview_start_symbol_subsection (acfg
, *section_id
, CODEVIEW_SUBSECTION_RECORD_FUNCTION_START_TYPE
, §ion_data
);
11437 // Emit 3 int 0 byte padding, currently not used.
11438 emit_zero_bytes (acfg
, sizeof (int) * 3);
11440 // Emit size of function.
11441 emit_symbol_diff (acfg
, symbol_end
, symbol_start
, 0);
11443 // Emit 3 int 0 byte padding, currently not used.
11444 emit_zero_bytes (acfg
, sizeof (int) * 3);
11446 // Emit reallocation info.
11447 fprintf (acfg
->fp
, "\t.secrel32 %s\n", symbol
);
11448 fprintf (acfg
->fp
, "\t.secidx %s\n", symbol
);
11450 // Emit flag, currently not used.
11451 emit_zero_bytes (acfg
, 1);
11453 // Emit function name, exclude signature since it should be described by own metadata.
11454 full_method_name
= mono_method_full_name (method
, FALSE
);
11455 emit_string (acfg
, full_method_name
? full_method_name
: "");
11456 g_free (full_method_name
);
11458 // Done with section.
11459 emit_codeview_end_symbol_subsection (acfg
, §ion_data
, section_id
);
11463 emit_codeview_info (MonoAotCompile
*acfg
)
11466 int section_id
= 0;
11467 gchar symbol_buffer
[MAX_SYMBOL_SIZE
];
11469 // Emit codeview debug info section
11470 emit_section_change (acfg
, ".debug$S", 0);
11472 // Emit magic header.
11473 emit_int32 (acfg
, CODEVIEW_MAGIC_HEADER
);
11475 emit_codeview_compiler_info (acfg
, §ion_id
);
11477 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
11478 MonoCompile
*cfg
= acfg
->cfgs
[i
];
11480 if (!cfg
|| COMPILE_LLVM (cfg
))
11483 int ret
= g_snprintf (symbol_buffer
, G_N_ELEMENTS (symbol_buffer
), "%sme_%x", acfg
->temp_prefix
, i
);
11484 if (ret
> 0 && ret
< G_N_ELEMENTS (symbol_buffer
))
11485 emit_codeview_function_info (acfg
, cfg
->method
, §ion_id
, cfg
->asm_debug_symbol
, cfg
->asm_symbol
, symbol_buffer
);
11490 emit_codeview_info (MonoAotCompile
*acfg
)
11493 #endif /* EMIT_WIN32_CODEVIEW_INFO */
11495 #ifdef EMIT_WIN32_UNWIND_INFO
11496 static UnwindInfoSectionCacheItem
*
11497 get_cached_unwind_info_section_item_win32 (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
)
11499 UnwindInfoSectionCacheItem
*item
= NULL
;
11501 if (!acfg
->unwind_info_section_cache
)
11502 acfg
->unwind_info_section_cache
= g_list_alloc ();
11504 PUNWIND_INFO unwind_info
= mono_arch_unwindinfo_alloc_unwind_info (unwind_ops
);
11506 // Search for unwind info in cache.
11507 GList
*list
= acfg
->unwind_info_section_cache
;
11509 while (list
&& list
->data
) {
11510 item
= (UnwindInfoSectionCacheItem
*)list
->data
;
11511 if (!memcmp (unwind_info
, item
->unwind_info
, sizeof (UNWIND_INFO
))) {
11512 // Cache hit, return cached item.
11520 if (acfg
->unwind_info_section_cache
) {
11521 item
= g_new0 (UnwindInfoSectionCacheItem
, 1);
11523 // Format .xdata section label for function, used to get unwind info address RVA.
11524 // Since the unwind info is similar for most functions, the symbol will be reused.
11525 item
->xdata_section_label
= g_strdup_printf ("%sunwind_%d", acfg
->temp_prefix
, list_size
);
11527 // Cache unwind info data, used when checking cache for matching unwind info. NOTE, cache takes
11528 //over ownership of unwind info.
11529 item
->unwind_info
= unwind_info
;
11531 // Needs to be emitted once.
11532 item
->xdata_section_emitted
= FALSE
;
11534 // Prepend to beginning of list to speed up inserts.
11535 acfg
->unwind_info_section_cache
= g_list_prepend (acfg
->unwind_info_section_cache
, (gpointer
)item
);
11543 free_unwind_info_section_cache_win32 (MonoAotCompile
*acfg
)
11545 GList
*list
= acfg
->unwind_info_section_cache
;
11548 UnwindInfoSectionCacheItem
*item
= (UnwindInfoSectionCacheItem
*)list
->data
;
11550 g_free (item
->xdata_section_label
);
11551 mono_arch_unwindinfo_free_unwind_info (item
->unwind_info
);
11560 g_list_free (acfg
->unwind_info_section_cache
);
11561 acfg
->unwind_info_section_cache
= NULL
;
11565 emit_unwind_info_data_win32 (MonoAotCompile
*acfg
, PUNWIND_INFO unwind_info
)
11567 // Emit the unwind info struct.
11568 emit_bytes (acfg
, (guint8
*)unwind_info
, sizeof (UNWIND_INFO
) - (sizeof (UNWIND_CODE
) * MONO_MAX_UNWIND_CODES
));
11570 // Emit all unwind codes encoded in unwind info struct.
11571 PUNWIND_CODE current_unwind_node
= &unwind_info
->UnwindCode
[MONO_MAX_UNWIND_CODES
- unwind_info
->CountOfCodes
];
11572 PUNWIND_CODE last_unwind_node
= &unwind_info
->UnwindCode
[MONO_MAX_UNWIND_CODES
];
11574 while (current_unwind_node
< last_unwind_node
) {
11575 guint8 node_count
= 0;
11576 switch (current_unwind_node
->UnwindOp
) {
11577 case UWOP_PUSH_NONVOL
:
11578 case UWOP_ALLOC_SMALL
:
11579 case UWOP_SET_FPREG
:
11580 case UWOP_PUSH_MACHFRAME
:
11583 case UWOP_SAVE_NONVOL
:
11584 case UWOP_SAVE_XMM128
:
11587 case UWOP_SAVE_NONVOL_FAR
:
11588 case UWOP_SAVE_XMM128_FAR
:
11591 case UWOP_ALLOC_LARGE
:
11592 if (current_unwind_node
->OpInfo
== 0)
11598 g_assert (!"Unknown unwind opcode.");
11601 while (node_count
> 0) {
11602 g_assert (current_unwind_node
< last_unwind_node
);
11604 //Emit current node.
11605 emit_bytes (acfg
, (guint8
*)current_unwind_node
, sizeof (UNWIND_CODE
));
11608 current_unwind_node
++;
11613 // Emit unwind info sections for each function. Unwind info on Windows x64 is emitted into two different sections.
11614 // .pdata includes the serialized DWORD aligned RVA's of function start, end and address of serialized
11615 // UNWIND_INFO struct emitted into .xdata, see https://msdn.microsoft.com/en-us/library/ft9x1kdx.aspx.
11616 // .xdata section includes DWORD aligned serialized version of UNWIND_INFO struct, https://msdn.microsoft.com/en-us/library/ddssxxy8.aspx.
11618 emit_unwind_info_sections_win32 (MonoAotCompile
*acfg
, const char *function_start
, const char *function_end
, GSList
*unwind_ops
)
11620 char *pdata_section_label
= NULL
;
11622 int temp_prefix_len
= (acfg
->temp_prefix
!= NULL
) ? strlen (acfg
->temp_prefix
) : 0;
11623 if (strncmp (function_start
, acfg
->temp_prefix
, temp_prefix_len
)) {
11624 temp_prefix_len
= 0;
11627 // Format .pdata section label for function.
11628 pdata_section_label
= g_strdup_printf ("%spdata_%s", acfg
->temp_prefix
, function_start
+ temp_prefix_len
);
11630 UnwindInfoSectionCacheItem
*cache_item
= get_cached_unwind_info_section_item_win32 (acfg
, function_start
, function_end
, unwind_ops
);
11631 g_assert (cache_item
&& cache_item
->xdata_section_label
&& cache_item
->unwind_info
);
11633 // Emit .pdata section.
11634 emit_section_change (acfg
, ".pdata", 0);
11635 emit_alignment (acfg
, sizeof (DWORD
));
11636 emit_label (acfg
, pdata_section_label
);
11638 // Emit function start address RVA.
11639 fprintf (acfg
->fp
, "\t.long %s@IMGREL\n", function_start
);
11641 // Emit function end address RVA.
11642 fprintf (acfg
->fp
, "\t.long %s@IMGREL\n", function_end
);
11644 // Emit unwind info address RVA.
11645 fprintf (acfg
->fp
, "\t.long %s@IMGREL\n", cache_item
->xdata_section_label
);
11647 if (!cache_item
->xdata_section_emitted
) {
11648 // Emit .xdata section.
11649 emit_section_change (acfg
, ".xdata", 0);
11650 emit_alignment (acfg
, sizeof (DWORD
));
11651 emit_label (acfg
, cache_item
->xdata_section_label
);
11653 // Emit unwind info into .xdata section.
11654 emit_unwind_info_data_win32 (acfg
, cache_item
->unwind_info
);
11655 cache_item
->xdata_section_emitted
= TRUE
;
11658 g_free (pdata_section_label
);
11663 should_emit_gsharedvt_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
11666 if (acfg
->image
== mono_get_corlib () && !strcmp (m_class_get_name (method
->klass
), "Vector`1"))
11667 /* The SIMD fallback code in Vector<T> is very large, and not likely to be used */
11670 if (acfg
->image
== mono_get_corlib () && !strcmp (m_class_get_name (method
->klass
), "Volatile"))
11671 /* Read<T>/Write<T> are not needed and cause JIT failures */
11677 collect_methods (MonoAotCompile
*acfg
)
11680 MonoImage
*image
= acfg
->image
;
11682 /* Collect methods */
11683 for (i
= 0; i
< image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
11684 ERROR_DECL (error
);
11685 MonoMethod
*method
;
11686 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
11688 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
11691 aot_printerrf (acfg
, "Failed to load method 0x%x from '%s' due to %s.\n", token
, image
->name
, mono_error_get_message (error
));
11692 aot_printerrf (acfg
, "Run with MONO_LOG_LEVEL=debug for more information.\n");
11693 mono_error_cleanup (error
);
11697 /* Load all methods eagerly to skip the slower lazy loading code */
11698 mono_class_setup_methods (method
->klass
);
11700 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
11701 /* Compile the wrapper instead */
11702 /* We do this here instead of add_wrappers () because it is easy to do it here */
11703 MonoMethod
*wrapper
= mono_marshal_get_native_wrapper (method
, TRUE
, TRUE
);
11707 /* FIXME: Some mscorlib methods don't have debug info */
11709 if (acfg->aot_opts.soft_debug && !method->wrapper_type) {
11710 if (!((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
11711 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
11712 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
11713 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))) {
11714 if (!mono_debug_lookup_method (method)) {
11715 fprintf (stderr, "Method %s has no debug info, probably the .mdb file for the assembly is missing.\n", mono_method_get_full_name (method));
11722 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
)) {
11723 /* Compile the ref shared version instead */
11724 method
= mini_get_shared_method_full (method
, SHARE_MODE_NONE
, error
);
11726 aot_printerrf (acfg
, "Failed to load method 0x%x from '%s' due to %s.\n", token
, image
->name
, mono_error_get_message (error
));
11727 aot_printerrf (acfg
, "Run with MONO_LOG_LEVEL=debug for more information.\n");
11728 mono_error_cleanup (error
);
11733 /* Since we add the normal methods first, their index will be equal to their zero based token index */
11734 add_method_with_index (acfg
, method
, i
, FALSE
);
11735 acfg
->method_index
++;
11738 /* gsharedvt methods */
11739 for (mindex
= 0; mindex
< image
->tables
[MONO_TABLE_METHOD
].rows
; ++mindex
) {
11740 ERROR_DECL (error
);
11741 MonoMethod
*method
;
11742 guint32 token
= MONO_TOKEN_METHOD_DEF
| (mindex
+ 1);
11744 if (!(acfg
->opts
& MONO_OPT_GSHAREDVT
))
11747 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, error
);
11748 report_loader_error (acfg
, error
, TRUE
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (error
));
11750 if ((method
->is_generic
|| mono_class_is_gtd (method
->klass
)) && should_emit_gsharedvt_method (acfg
, method
)) {
11751 MonoMethod
*gshared
;
11753 gshared
= mini_get_shared_method_full (method
, SHARE_MODE_GSHAREDVT
, error
);
11754 mono_error_assert_ok (error
);
11756 add_extra_method (acfg
, gshared
);
11760 if (mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))
11761 add_generic_instances (acfg
);
11763 if (mono_aot_mode_is_full (&acfg
->aot_opts
))
11764 add_wrappers (acfg
);
11769 compile_methods (MonoAotCompile
*acfg
)
11771 int i
, methods_len
;
11773 if (acfg
->aot_opts
.nthreads
> 0) {
11776 GPtrArray
*threads
;
11777 MonoThreadHandle
*thread_handle
;
11778 gpointer
*user_data
;
11779 MonoMethod
**methods
;
11781 methods_len
= acfg
->methods
->len
;
11783 len
= acfg
->methods
->len
/ acfg
->aot_opts
.nthreads
;
11784 g_assert (len
> 0);
11786 * Partition the list of methods into fragments, and hand it to threads to
11789 threads
= g_ptr_array_new ();
11790 /* Make a copy since acfg->methods is modified by compile_method () */
11791 methods
= g_new0 (MonoMethod
*, methods_len
);
11792 //memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
11793 for (i
= 0; i
< methods_len
; ++i
)
11794 methods
[i
] = (MonoMethod
*)g_ptr_array_index (acfg
->methods
, i
);
11796 while (i
< methods_len
) {
11797 ERROR_DECL (error
);
11798 MonoInternalThread
*thread
;
11800 frag
= g_ptr_array_new ();
11801 for (j
= 0; j
< len
; ++j
) {
11802 if (i
< methods_len
) {
11803 g_ptr_array_add (frag
, methods
[i
]);
11808 user_data
= g_new0 (gpointer
, 3);
11809 user_data
[0] = acfg
;
11810 user_data
[1] = frag
;
11812 thread
= mono_thread_create_internal (mono_domain_get (), (gpointer
)compile_thread_main
, user_data
, MONO_THREAD_CREATE_FLAGS_NONE
, error
);
11813 mono_error_assert_ok (error
);
11815 thread_handle
= mono_threads_open_thread_handle (thread
->handle
);
11816 g_ptr_array_add (threads
, thread_handle
);
11820 for (i
= 0; i
< threads
->len
; ++i
) {
11821 mono_thread_info_wait_one_handle ((MonoThreadHandle
*)g_ptr_array_index (threads
, i
), MONO_INFINITE_WAIT
, FALSE
);
11822 mono_threads_close_thread_handle ((MonoThreadHandle
*)g_ptr_array_index (threads
, i
));
11828 /* Compile methods added by compile_method () or all methods if nthreads == 0 */
11829 for (i
= methods_len
; i
< acfg
->methods
->len
; ++i
) {
11830 /* This can add new methods to acfg->methods */
11831 compile_method (acfg
, (MonoMethod
*)g_ptr_array_index (acfg
->methods
, i
));
11836 mono_llvm_fixup_aot_module ();
11841 compile_asm (MonoAotCompile
*acfg
)
11843 char *command
, *objfile
;
11844 char *outfile_name
, *tmp_outfile_name
, *llvm_ofile
;
11845 const char *tool_prefix
= acfg
->aot_opts
.tool_prefix
? acfg
->aot_opts
.tool_prefix
: "";
11846 char *ld_flags
= acfg
->aot_opts
.ld_flags
? acfg
->aot_opts
.ld_flags
: g_strdup("");
11848 #ifdef TARGET_WIN32_MSVC
11849 #define AS_OPTIONS "-c -x assembler"
11850 #elif defined(TARGET_AMD64) && !defined(TARGET_MACH)
11851 #define AS_OPTIONS "--64"
11852 #elif defined(TARGET_POWERPC64)
11853 #define AS_OPTIONS "-a64 -mppc64"
11854 #elif defined(sparc) && TARGET_SIZEOF_VOID_P == 8
11855 #define AS_OPTIONS "-xarch=v9"
11856 #elif defined(TARGET_X86) && defined(TARGET_MACH)
11857 #define AS_OPTIONS "-arch i386"
11858 #elif defined(TARGET_X86) && !defined(TARGET_MACH)
11859 #define AS_OPTIONS "--32"
11861 #define AS_OPTIONS ""
11864 #if defined(TARGET_OSX)
11865 #define AS_NAME "clang"
11866 #elif defined(TARGET_WIN32_MSVC)
11867 #define AS_NAME "clang.exe"
11869 #define AS_NAME "as"
11872 #ifdef TARGET_WIN32_MSVC
11873 #define AS_OBJECT_FILE_SUFFIX "obj"
11875 #define AS_OBJECT_FILE_SUFFIX "o"
11879 #define LD_NAME "ld"
11880 #define LD_OPTIONS "-shared -G"
11881 #elif defined(__ppc__) && defined(TARGET_MACH)
11882 #define LD_NAME "gcc"
11883 #define LD_OPTIONS "-dynamiclib"
11884 #elif defined(TARGET_AMD64) && defined(TARGET_MACH)
11885 #define LD_NAME "clang"
11886 #define LD_OPTIONS "--shared"
11887 #elif defined(TARGET_WIN32_MSVC)
11888 #define LD_NAME "link.exe"
11889 #define LD_OPTIONS "/DLL /MACHINE:X64 /NOLOGO /INCREMENTAL:NO"
11890 #define LD_DEBUG_OPTIONS LD_OPTIONS " /DEBUG"
11891 #elif defined(TARGET_WIN32) && !defined(TARGET_ANDROID)
11892 #define LD_NAME "gcc"
11893 #define LD_OPTIONS "-shared"
11894 #elif defined(TARGET_X86) && defined(TARGET_MACH)
11895 #define LD_NAME "clang"
11896 #define LD_OPTIONS "-m32 -dynamiclib"
11897 #elif defined(TARGET_X86) && !defined(TARGET_MACH)
11898 #define LD_OPTIONS "-m elf_i386"
11899 #elif defined(TARGET_ARM) && !defined(TARGET_ANDROID)
11900 #define LD_NAME "gcc"
11901 #define LD_OPTIONS "--shared"
11902 #elif defined(TARGET_POWERPC64)
11903 #define LD_OPTIONS "-m elf64ppc"
11907 #define LD_OPTIONS ""
11910 if (acfg
->aot_opts
.asm_only
) {
11911 aot_printf (acfg
, "Output file: '%s'.\n", acfg
->tmpfname
);
11912 if (acfg
->aot_opts
.static_link
)
11913 aot_printf (acfg
, "Linking symbol: '%s'.\n", acfg
->static_linking_symbol
);
11915 aot_printf (acfg
, "LLVM output file: '%s'.\n", acfg
->llvm_sfile
);
11919 if (acfg
->aot_opts
.static_link
) {
11920 if (acfg
->aot_opts
.outfile
)
11921 objfile
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
11923 objfile
= g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->image
->name
);
11925 objfile
= g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
);
11929 g_string_append (acfg
->as_args
, "-c -x assembler");
11932 command
= g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix
, AS_NAME
, AS_OPTIONS
,
11933 acfg
->as_args
? acfg
->as_args
->str
: "",
11934 wrap_path (objfile
), wrap_path (acfg
->tmpfname
));
11935 aot_printf (acfg
, "Executing the native assembler: %s\n", command
);
11936 if (execute_system (command
) != 0) {
11942 if (acfg
->llvm
&& !acfg
->llvm_owriter
) {
11943 command
= g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix
, AS_NAME
, AS_OPTIONS
,
11944 acfg
->as_args
? acfg
->as_args
->str
: "",
11945 wrap_path (acfg
->llvm_ofile
), wrap_path (acfg
->llvm_sfile
));
11946 aot_printf (acfg
, "Executing the native assembler: %s\n", command
);
11947 if (execute_system (command
) != 0) {
11956 if (acfg
->aot_opts
.static_link
) {
11957 aot_printf (acfg
, "Output file: '%s'.\n", objfile
);
11958 aot_printf (acfg
, "Linking symbol: '%s'.\n", acfg
->static_linking_symbol
);
11963 if (acfg
->aot_opts
.outfile
)
11964 outfile_name
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
11966 outfile_name
= g_strdup_printf ("%s%s", acfg
->image
->name
, MONO_SOLIB_EXT
);
11968 tmp_outfile_name
= g_strdup_printf ("%s.tmp", outfile_name
);
11971 llvm_ofile
= g_strdup_printf ("\"%s\"", acfg
->llvm_ofile
);
11973 llvm_ofile
= g_strdup ("");
11976 /* replace the ; flags separators with spaces */
11977 g_strdelimit (ld_flags
, ';', ' ');
11979 if (acfg
->aot_opts
.llvm_only
)
11980 ld_flags
= g_strdup_printf ("%s %s", ld_flags
, "-lstdc++");
11982 #ifdef TARGET_WIN32_MSVC
11983 g_assert (tmp_outfile_name
!= NULL
);
11984 g_assert (objfile
!= NULL
);
11985 command
= g_strdup_printf ("\"%s%s\" %s %s /OUT:%s %s %s", tool_prefix
, LD_NAME
,
11986 acfg
->aot_opts
.nodebug
? LD_OPTIONS
: LD_DEBUG_OPTIONS
, ld_flags
, wrap_path (tmp_outfile_name
), wrap_path (objfile
), wrap_path (llvm_ofile
));
11987 #elif defined(LD_NAME)
11988 command
= g_strdup_printf ("%s%s %s -o %s %s %s %s", tool_prefix
, LD_NAME
, LD_OPTIONS
,
11989 wrap_path (tmp_outfile_name
), wrap_path (llvm_ofile
),
11990 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
)), ld_flags
);
11993 if (acfg
->aot_opts
.tool_prefix
) {
11994 /* Cross compiling */
11995 command
= g_strdup_printf ("\"%sld\" %s -shared -o %s %s %s %s", tool_prefix
, LD_OPTIONS
,
11996 wrap_path (tmp_outfile_name
), wrap_path (llvm_ofile
),
11997 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
)), ld_flags
);
11999 char *args
= g_strdup_printf ("%s -shared -o %s %s %s %s", LD_OPTIONS
,
12000 wrap_path (tmp_outfile_name
), wrap_path (llvm_ofile
),
12001 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
)), ld_flags
);
12003 if (acfg
->aot_opts
.llvm_only
) {
12004 command
= g_strdup_printf ("%s %s", acfg
->aot_opts
.clangxx
, args
);
12006 command
= g_strdup_printf ("\"%sld\" %s", tool_prefix
, args
);
12011 aot_printf (acfg
, "Executing the native linker: %s\n", command
);
12012 if (execute_system (command
) != 0) {
12013 g_free (tmp_outfile_name
);
12014 g_free (outfile_name
);
12023 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, MONO_SOLIB_EXT);
12024 printf ("Stripping the binary: %s\n", com);
12025 execute_system (com);
12028 #if defined(TARGET_ARM) && !defined(TARGET_MACH)
12030 * gas generates 'mapping symbols' each time code and data is mixed, which
12031 * happens a lot in emit_and_reloc_code (), so we need to get rid of them.
12033 command
= g_strdup_printf ("\"%sstrip\" --strip-symbol=\\$a --strip-symbol=\\$d %s", wrap_path(tool_prefix
), wrap_path(tmp_outfile_name
));
12034 aot_printf (acfg
, "Stripping the binary: %s\n", command
);
12035 if (execute_system (command
) != 0) {
12036 g_free (tmp_outfile_name
);
12037 g_free (outfile_name
);
12044 if (0 != rename (tmp_outfile_name
, outfile_name
)) {
12045 if (G_FILE_ERROR_EXIST
== g_file_error_from_errno (errno
)) {
12046 /* Since we are rebuilding the module we need to be able to replace any old copies. Remove old file and retry rename operation. */
12047 unlink (outfile_name
);
12048 rename (tmp_outfile_name
, outfile_name
);
12052 #if defined(TARGET_MACH)
12053 command
= g_strdup_printf ("dsymutil \"%s\"", outfile_name
);
12054 aot_printf (acfg
, "Executing dsymutil: %s\n", command
);
12055 if (execute_system (command
) != 0) {
12060 if (!acfg
->aot_opts
.save_temps
)
12063 g_free (tmp_outfile_name
);
12064 g_free (outfile_name
);
12067 if (acfg
->aot_opts
.save_temps
)
12068 aot_printf (acfg
, "Retained input file.\n");
12070 unlink (acfg
->tmpfname
);
12076 profread_byte (FILE *infile
)
12081 res
= fread (&i
, 1, 1, infile
);
12082 g_assert (res
== 1);
12087 profread_int (FILE *infile
)
12091 res
= fread (&i
, 4, 1, infile
);
12092 g_assert (res
== 1);
12097 profread_string (FILE *infile
)
12102 len
= profread_int (infile
);
12103 pbuf
= (char*)g_malloc (len
+ 1);
12104 res
= fread (pbuf
, 1, len
, infile
);
12105 g_assert (res
== len
);
12111 load_profile_file (MonoAotCompile
*acfg
, char *filename
)
12115 int res
, len
, version
;
12118 infile
= fopen (filename
, "rb");
12120 fprintf (stderr
, "Unable to open file '%s': %s.\n", filename
, strerror (errno
));
12124 printf ("Using profile data file '%s'\n", filename
);
12126 sprintf (magic
, AOT_PROFILER_MAGIC
);
12127 len
= strlen (magic
);
12128 res
= fread (buf
, 1, len
, infile
);
12129 magic
[len
] = '\0';
12131 if ((res
!= len
) || strcmp (buf
, magic
) != 0) {
12132 printf ("Profile file has wrong header: '%s'.\n", buf
);
12136 guint32 expected_version
= (AOT_PROFILER_MAJOR_VERSION
<< 16) | AOT_PROFILER_MINOR_VERSION
;
12137 version
= profread_int (infile
);
12138 if (version
!= expected_version
) {
12139 printf ("Profile file has wrong version 0x%4x, expected 0x%4x.\n", version
, expected_version
);
12144 ProfileData
*data
= g_new0 (ProfileData
, 1);
12145 data
->images
= g_hash_table_new (NULL
, NULL
);
12146 data
->classes
= g_hash_table_new (NULL
, NULL
);
12147 data
->ginsts
= g_hash_table_new (NULL
, NULL
);
12148 data
->methods
= g_hash_table_new (NULL
, NULL
);
12151 int type
= profread_byte (infile
);
12152 int id
= profread_int (infile
);
12154 if (type
== AOTPROF_RECORD_NONE
)
12158 case AOTPROF_RECORD_IMAGE
: {
12159 ImageProfileData
*idata
= g_new0 (ImageProfileData
, 1);
12160 idata
->name
= profread_string (infile
);
12161 char *mvid
= profread_string (infile
);
12163 g_hash_table_insert (data
->images
, GINT_TO_POINTER (id
), idata
);
12166 case AOTPROF_RECORD_GINST
: {
12168 int len
= profread_int (infile
);
12170 GInstProfileData
*gdata
= g_new0 (GInstProfileData
, 1);
12172 gdata
->argv
= g_new0 (ClassProfileData
*, len
);
12174 for (i
= 0; i
< len
; ++i
) {
12175 int class_id
= profread_int (infile
);
12177 gdata
->argv
[i
] = (ClassProfileData
*)g_hash_table_lookup (data
->classes
, GINT_TO_POINTER (class_id
));
12178 g_assert (gdata
->argv
[i
]);
12180 g_hash_table_insert (data
->ginsts
, GINT_TO_POINTER (id
), gdata
);
12183 case AOTPROF_RECORD_TYPE
: {
12184 int type
= profread_byte (infile
);
12187 case MONO_TYPE_CLASS
: {
12188 int image_id
= profread_int (infile
);
12189 int ginst_id
= profread_int (infile
);
12190 char *class_name
= profread_string (infile
);
12192 ImageProfileData
*image
= (ImageProfileData
*)g_hash_table_lookup (data
->images
, GINT_TO_POINTER (image_id
));
12195 char *p
= strrchr (class_name
, '.');
12199 ClassProfileData
*cdata
= g_new0 (ClassProfileData
, 1);
12200 cdata
->image
= image
;
12201 cdata
->ns
= g_strdup (class_name
);
12202 cdata
->name
= g_strdup (p
+ 1);
12204 if (ginst_id
!= -1) {
12205 cdata
->inst
= (GInstProfileData
*)g_hash_table_lookup (data
->ginsts
, GINT_TO_POINTER (ginst_id
));
12206 g_assert (cdata
->inst
);
12208 g_free (class_name
);
12210 g_hash_table_insert (data
->classes
, GINT_TO_POINTER (id
), cdata
);
12214 case MONO_TYPE_SZARRAY
: {
12215 int elem_id
= profread_int (infile
);
12221 g_assert_not_reached ();
12226 case AOTPROF_RECORD_METHOD
: {
12227 int class_id
= profread_int (infile
);
12228 int ginst_id
= profread_int (infile
);
12229 int param_count
= profread_int (infile
);
12230 char *method_name
= profread_string (infile
);
12231 char *sig
= profread_string (infile
);
12233 ClassProfileData
*klass
= (ClassProfileData
*)g_hash_table_lookup (data
->classes
, GINT_TO_POINTER (class_id
));
12236 MethodProfileData
*mdata
= g_new0 (MethodProfileData
, 1);
12238 mdata
->klass
= klass
;
12239 mdata
->name
= method_name
;
12240 mdata
->signature
= sig
;
12241 mdata
->param_count
= param_count
;
12243 if (ginst_id
!= -1) {
12244 mdata
->inst
= (GInstProfileData
*)g_hash_table_lookup (data
->ginsts
, GINT_TO_POINTER (ginst_id
));
12245 g_assert (mdata
->inst
);
12247 g_hash_table_insert (data
->methods
, GINT_TO_POINTER (id
), mdata
);
12251 printf ("%d\n", type
);
12252 g_assert_not_reached ();
12258 acfg
->profile_data
= g_list_append (acfg
->profile_data
, data
);
12262 resolve_class (ClassProfileData
*cdata
);
12265 resolve_ginst (GInstProfileData
*inst_data
)
12269 if (inst_data
->inst
)
12272 for (i
= 0; i
< inst_data
->argc
; ++i
) {
12273 resolve_class (inst_data
->argv
[i
]);
12274 if (!inst_data
->argv
[i
]->klass
)
12277 MonoType
**args
= g_new0 (MonoType
*, inst_data
->argc
);
12278 for (i
= 0; i
< inst_data
->argc
; ++i
)
12279 args
[i
] = m_class_get_byval_arg (inst_data
->argv
[i
]->klass
);
12281 inst_data
->inst
= mono_metadata_get_generic_inst (inst_data
->argc
, args
);
12285 resolve_class (ClassProfileData
*cdata
)
12287 ERROR_DECL (error
);
12290 if (!cdata
->image
->image
)
12293 klass
= mono_class_from_name_checked (cdata
->image
->image
, cdata
->ns
, cdata
->name
, error
);
12295 //printf ("[%s] %s.%s\n", cdata->image->name, cdata->ns, cdata->name);
12299 resolve_ginst (cdata
->inst
);
12300 if (!cdata
->inst
->inst
)
12302 MonoGenericContext ctx
;
12304 memset (&ctx
, 0, sizeof (ctx
));
12305 ctx
.class_inst
= cdata
->inst
->inst
;
12306 cdata
->klass
= mono_class_inflate_generic_class_checked (klass
, &ctx
, error
);
12308 cdata
->klass
= klass
;
12313 * Resolve the profile data to the corresponding loaded classes/methods etc. if possible.
12316 resolve_profile_data (MonoAotCompile
*acfg
, ProfileData
*data
, MonoAssembly
* current
)
12318 GHashTableIter iter
;
12319 gpointer key
, value
;
12326 GPtrArray
*assemblies
= mono_domain_get_assemblies (mono_get_root_domain (), FALSE
);
12327 g_hash_table_iter_init (&iter
, data
->images
);
12328 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12329 ImageProfileData
*idata
= (ImageProfileData
*)value
;
12331 if (!strcmp (current
->aname
.name
, idata
->name
)) {
12332 idata
->image
= current
->image
;
12336 for (i
= 0; i
< assemblies
->len
; ++i
) {
12337 MonoAssembly
*ass
= (MonoAssembly
*)g_ptr_array_index (assemblies
, i
);
12339 if (!strcmp (ass
->aname
.name
, idata
->name
)) {
12340 idata
->image
= ass
->image
;
12345 g_ptr_array_free (assemblies
, TRUE
);
12348 g_hash_table_iter_init (&iter
, data
->classes
);
12349 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12350 ClassProfileData
*cdata
= (ClassProfileData
*)value
;
12352 if (!cdata
->image
->image
) {
12353 if (acfg
->aot_opts
.verbose
)
12354 printf ("Unable to load class '%s.%s' because its image '%s' is not loaded.\n", cdata
->ns
, cdata
->name
, cdata
->image
->name
);
12358 resolve_class (cdata
);
12361 printf ("%s %s %s\n", cdata->ns, cdata->name, mono_class_full_name (cdata->klass));
12366 g_hash_table_iter_init (&iter
, data
->methods
);
12367 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12368 MethodProfileData
*mdata
= (MethodProfileData
*)value
;
12373 resolve_class (mdata
->klass
);
12374 klass
= mdata
->klass
->klass
;
12376 if (acfg
->aot_opts
.verbose
)
12377 printf ("Unable to load method '%s' because its class '%s.%s' is not loaded.\n", mdata
->name
, mdata
->klass
->ns
, mdata
->klass
->name
);
12381 while ((m
= mono_class_get_methods (klass
, &miter
))) {
12382 ERROR_DECL (error
);
12384 if (strcmp (m
->name
, mdata
->name
))
12386 MonoMethodSignature
*sig
= mono_method_signature_internal (m
);
12389 if (sig
->param_count
!= mdata
->param_count
)
12392 resolve_ginst (mdata
->inst
);
12393 if (!mdata
->inst
->inst
)
12395 MonoGenericContext ctx
;
12397 memset (&ctx
, 0, sizeof (ctx
));
12398 ctx
.method_inst
= mdata
->inst
->inst
;
12400 m
= mono_class_inflate_generic_method_checked (m
, &ctx
, error
);
12403 sig
= mono_method_signature_checked (m
, error
);
12404 if (!is_ok (error
)) {
12405 mono_error_cleanup (error
);
12409 char *sig_str
= mono_signature_full_name (sig
);
12410 gboolean match
= !strcmp (sig_str
, mdata
->signature
);
12415 //printf ("%s\n", mono_method_full_name (m, 1));
12419 if (!mdata
->method
) {
12420 if (acfg
->aot_opts
.verbose
)
12421 printf ("Unable to load method '%s' from class '%s', not found.\n", mdata
->name
, mono_class_full_name (klass
));
12427 inst_references_image (MonoGenericInst
*inst
, MonoImage
*image
)
12431 for (i
= 0; i
< inst
->type_argc
; ++i
) {
12432 MonoClass
*k
= mono_class_from_mono_type_internal (inst
->type_argv
[i
]);
12433 if (m_class_get_image (k
) == image
)
12435 if (mono_class_is_ginst (k
)) {
12436 MonoGenericInst
*kinst
= mono_class_get_context (k
)->class_inst
;
12437 if (inst_references_image (kinst
, image
))
12445 is_local_inst (MonoGenericInst
*inst
, MonoImage
*image
)
12449 for (i
= 0; i
< inst
->type_argc
; ++i
) {
12450 MonoClass
*k
= mono_class_from_mono_type_internal (inst
->type_argv
[i
]);
12451 if (!MONO_TYPE_IS_PRIMITIVE (inst
->type_argv
[i
]) && m_class_get_image (k
) != image
)
12458 add_profile_instances (MonoAotCompile
*acfg
, ProfileData
*data
)
12460 GHashTableIter iter
;
12461 gpointer key
, value
;
12467 if (acfg
->aot_opts
.profile_only
) {
12468 /* Add methods referenced by the profile */
12469 g_hash_table_iter_init (&iter
, data
->methods
);
12470 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12471 MethodProfileData
*mdata
= (MethodProfileData
*)value
;
12472 MonoMethod
*m
= mdata
->method
;
12476 if (m
->is_inflated
)
12478 add_extra_method (acfg
, m
);
12479 g_hash_table_insert (acfg
->profile_methods
, m
, m
);
12485 * Add method instances 'related' to this assembly to the AOT image.
12487 g_hash_table_iter_init (&iter
, data
->methods
);
12488 while (g_hash_table_iter_next (&iter
, &key
, &value
)) {
12489 MethodProfileData
*mdata
= (MethodProfileData
*)value
;
12490 MonoMethod
*m
= mdata
->method
;
12491 MonoGenericContext
*ctx
;
12495 if (!m
->is_inflated
)
12498 ctx
= mono_method_get_context (m
);
12499 /* For simplicity, add instances which reference the assembly we are compiling */
12500 if (((ctx
->class_inst
&& inst_references_image (ctx
->class_inst
, acfg
->image
)) ||
12501 (ctx
->method_inst
&& inst_references_image (ctx
->method_inst
, acfg
->image
))) &&
12502 !mono_method_is_generic_sharable_full (m
, FALSE
, FALSE
, FALSE
)) {
12503 //printf ("%s\n", mono_method_full_name (m, TRUE));
12504 add_extra_method (acfg
, m
);
12506 } else if (m_class_get_image (m
->klass
) == acfg
->image
&&
12507 ((ctx
->class_inst
&& is_local_inst (ctx
->class_inst
, acfg
->image
)) ||
12508 (ctx
->method_inst
&& is_local_inst (ctx
->method_inst
, acfg
->image
))) &&
12509 !mono_method_is_generic_sharable_full (m
, FALSE
, FALSE
, FALSE
)) {
12510 /* Add instances where the gtd is in the assembly and its inflated with types from this assembly or corlib */
12511 //printf ("%s\n", mono_method_full_name (m, TRUE));
12512 add_extra_method (acfg
, m
);
12516 * FIXME: We might skip some instances, for example:
12517 * Foo<Bar> won't be compiled when compiling Foo's assembly since it doesn't match the first case,
12518 * and it won't be compiled when compiling Bar's assembly if Foo's assembly is not loaded.
12522 printf ("Added %d methods from profile.\n", count
);
12526 init_got_info (GotInfo
*info
)
12530 info
->patch_to_got_offset
= g_hash_table_new (mono_patch_info_hash
, mono_patch_info_equal
);
12531 info
->patch_to_got_offset_by_type
= g_new0 (GHashTable
*, MONO_PATCH_INFO_NUM
);
12532 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
)
12533 info
->patch_to_got_offset_by_type
[i
] = g_hash_table_new (mono_patch_info_hash
, mono_patch_info_equal
);
12534 info
->got_patches
= g_ptr_array_new ();
12537 static MonoAotCompile
*
12538 acfg_create (MonoAssembly
*ass
, guint32 opts
)
12540 MonoImage
*image
= ass
->image
;
12541 MonoAotCompile
*acfg
;
12543 acfg
= g_new0 (MonoAotCompile
, 1);
12544 acfg
->methods
= g_ptr_array_new ();
12545 acfg
->method_indexes
= g_hash_table_new (NULL
, NULL
);
12546 acfg
->method_depth
= g_hash_table_new (NULL
, NULL
);
12547 acfg
->plt_offset_to_entry
= g_hash_table_new (NULL
, NULL
);
12548 acfg
->patch_to_plt_entry
= g_new0 (GHashTable
*, MONO_PATCH_INFO_NUM
);
12549 acfg
->method_to_cfg
= g_hash_table_new (NULL
, NULL
);
12550 acfg
->token_info_hash
= g_hash_table_new_full (NULL
, NULL
, NULL
, NULL
);
12551 acfg
->method_to_pinvoke_import
= g_hash_table_new_full (NULL
, NULL
, NULL
, g_free
);
12552 acfg
->method_to_external_icall_symbol_name
= g_hash_table_new_full (NULL
, NULL
, NULL
, g_free
);
12553 acfg
->image_hash
= g_hash_table_new (NULL
, NULL
);
12554 acfg
->image_table
= g_ptr_array_new ();
12555 acfg
->globals
= g_ptr_array_new ();
12556 acfg
->image
= image
;
12558 /* TODO: Write out set of SIMD instructions used, rather than just those available */
12559 #ifndef MONO_CROSS_COMPILE
12560 acfg
->simd_opts
= mono_arch_cpu_enumerate_simd_versions ();
12562 acfg
->mempool
= mono_mempool_new ();
12563 acfg
->extra_methods
= g_ptr_array_new ();
12564 acfg
->unwind_info_offsets
= g_hash_table_new (NULL
, NULL
);
12565 acfg
->unwind_ops
= g_ptr_array_new ();
12566 acfg
->method_label_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
12567 acfg
->method_order
= g_ptr_array_new ();
12568 acfg
->export_names
= g_hash_table_new (NULL
, NULL
);
12569 acfg
->klass_blob_hash
= g_hash_table_new (NULL
, NULL
);
12570 acfg
->method_blob_hash
= g_hash_table_new (NULL
, NULL
);
12571 acfg
->ginst_blob_hash
= g_hash_table_new (mono_metadata_generic_inst_hash
, mono_metadata_generic_inst_equal
);
12572 acfg
->plt_entry_debug_sym_cache
= g_hash_table_new (g_str_hash
, g_str_equal
);
12573 acfg
->gsharedvt_in_signatures
= g_hash_table_new ((GHashFunc
)mono_signature_hash
, (GEqualFunc
)mono_metadata_signature_equal
);
12574 acfg
->gsharedvt_out_signatures
= g_hash_table_new ((GHashFunc
)mono_signature_hash
, (GEqualFunc
)mono_metadata_signature_equal
);
12575 acfg
->profile_methods
= g_hash_table_new (NULL
, NULL
);
12576 mono_os_mutex_init_recursive (&acfg
->mutex
);
12578 init_got_info (&acfg
->got_info
);
12579 init_got_info (&acfg
->llvm_got_info
);
12581 method_to_external_icall_symbol_name
= acfg
->method_to_external_icall_symbol_name
;
12586 got_info_free (GotInfo
*info
)
12590 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
)
12591 g_hash_table_destroy (info
->patch_to_got_offset_by_type
[i
]);
12592 g_free (info
->patch_to_got_offset_by_type
);
12593 g_hash_table_destroy (info
->patch_to_got_offset
);
12594 g_ptr_array_free (info
->got_patches
, TRUE
);
12598 acfg_free (MonoAotCompile
*acfg
)
12602 mono_img_writer_destroy (acfg
->w
);
12603 for (i
= 0; i
< acfg
->nmethods
; ++i
)
12604 if (acfg
->cfgs
[i
])
12605 mono_destroy_compile (acfg
->cfgs
[i
]);
12607 g_free (acfg
->cfgs
);
12609 g_free (acfg
->static_linking_symbol
);
12610 g_free (acfg
->got_symbol
);
12611 g_free (acfg
->plt_symbol
);
12612 g_ptr_array_free (acfg
->methods
, TRUE
);
12613 g_ptr_array_free (acfg
->image_table
, TRUE
);
12614 g_ptr_array_free (acfg
->globals
, TRUE
);
12615 g_ptr_array_free (acfg
->unwind_ops
, TRUE
);
12616 g_hash_table_destroy (acfg
->method_indexes
);
12617 g_hash_table_destroy (acfg
->method_depth
);
12618 g_hash_table_destroy (acfg
->plt_offset_to_entry
);
12619 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
)
12620 g_hash_table_destroy (acfg
->patch_to_plt_entry
[i
]);
12621 g_free (acfg
->patch_to_plt_entry
);
12622 g_hash_table_destroy (acfg
->method_to_cfg
);
12623 g_hash_table_destroy (acfg
->token_info_hash
);
12624 g_hash_table_destroy (acfg
->method_to_pinvoke_import
);
12625 g_hash_table_destroy (acfg
->method_to_external_icall_symbol_name
);
12626 g_hash_table_destroy (acfg
->image_hash
);
12627 g_hash_table_destroy (acfg
->unwind_info_offsets
);
12628 g_hash_table_destroy (acfg
->method_label_hash
);
12629 g_hash_table_destroy (acfg
->typespec_classes
);
12630 g_hash_table_destroy (acfg
->export_names
);
12631 g_hash_table_destroy (acfg
->plt_entry_debug_sym_cache
);
12632 g_hash_table_destroy (acfg
->klass_blob_hash
);
12633 g_hash_table_destroy (acfg
->method_blob_hash
);
12634 got_info_free (&acfg
->got_info
);
12635 got_info_free (&acfg
->llvm_got_info
);
12636 arch_free_unwind_info_section_cache (acfg
);
12637 mono_mempool_destroy (acfg
->mempool
);
12639 method_to_external_icall_symbol_name
= NULL
;
12643 #define WRAPPER(e,n) n,
12644 static const char* const
12645 wrapper_type_names
[MONO_WRAPPER_NUM
+ 1] = {
12646 #include "mono/metadata/wrapper-types.h"
12650 static G_GNUC_UNUSED
const char*
12651 get_wrapper_type_name (int type
)
12653 return wrapper_type_names
[type
];
12659 static void aot_dump (MonoAotCompile
*acfg
)
12665 mono_json_writer_init (&writer
);
12667 mono_json_writer_object_begin(&writer
);
12670 mono_json_writer_indent (&writer
);
12671 mono_json_writer_object_key(&writer
, "methods");
12672 mono_json_writer_array_begin (&writer
);
12675 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
12677 MonoMethod
*method
;
12680 cfg
= acfg
->cfgs
[i
];
12681 if (ignore_cfg (cfg
))
12684 method
= cfg
->orig_method
;
12686 mono_json_writer_indent (&writer
);
12687 mono_json_writer_object_begin(&writer
);
12689 mono_json_writer_indent (&writer
);
12690 mono_json_writer_object_key(&writer
, "name");
12691 mono_json_writer_printf (&writer
, "\"%s\",\n", method
->name
);
12693 mono_json_writer_indent (&writer
);
12694 mono_json_writer_object_key(&writer
, "signature");
12695 mono_json_writer_printf (&writer
, "\"%s\",\n", mono_method_get_full_name (method
));
12697 mono_json_writer_indent (&writer
);
12698 mono_json_writer_object_key(&writer
, "code_size");
12699 mono_json_writer_printf (&writer
, "\"%d\",\n", cfg
->code_size
);
12701 klass
= method
->klass
;
12703 mono_json_writer_indent (&writer
);
12704 mono_json_writer_object_key(&writer
, "class");
12705 mono_json_writer_printf (&writer
, "\"%s\",\n", m_class_get_name (klass
));
12707 mono_json_writer_indent (&writer
);
12708 mono_json_writer_object_key(&writer
, "namespace");
12709 mono_json_writer_printf (&writer
, "\"%s\",\n", m_class_get_name_space (klass
));
12711 mono_json_writer_indent (&writer
);
12712 mono_json_writer_object_key(&writer
, "wrapper_type");
12713 mono_json_writer_printf (&writer
, "\"%s\",\n", get_wrapper_type_name(method
->wrapper_type
));
12715 mono_json_writer_indent_pop (&writer
);
12716 mono_json_writer_indent (&writer
);
12717 mono_json_writer_object_end (&writer
);
12718 mono_json_writer_printf (&writer
, ",\n");
12721 mono_json_writer_indent_pop (&writer
);
12722 mono_json_writer_indent (&writer
);
12723 mono_json_writer_array_end (&writer
);
12724 mono_json_writer_printf (&writer
, ",\n");
12728 mono_json_writer_indent_push (&writer
);
12729 mono_json_writer_indent (&writer
);
12730 mono_json_writer_object_key(&writer
, "plt");
12731 mono_json_writer_array_begin (&writer
);
12733 for (i
= 0; i
< acfg
->plt_offset
; ++i
) {
12734 MonoPltEntry
*plt_entry
= NULL
;
12739 * The first plt entry is unused.
12743 plt_entry
= g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
12744 ji
= plt_entry
->ji
;
12746 mono_json_writer_indent (&writer
);
12747 mono_json_writer_printf (&writer
, "{ ");
12748 mono_json_writer_object_key(&writer
, "symbol");
12749 mono_json_writer_printf (&writer
, "\"%s\" },\n", plt_entry
->symbol
);
12752 mono_json_writer_indent_pop (&writer
);
12753 mono_json_writer_indent (&writer
);
12754 mono_json_writer_array_end (&writer
);
12755 mono_json_writer_printf (&writer
, ",\n");
12760 mono_json_writer_indent_push (&writer
);
12761 mono_json_writer_indent (&writer
);
12762 mono_json_writer_object_key(&writer
, "got");
12763 mono_json_writer_array_begin (&writer
);
12765 mono_json_writer_indent_push (&writer
);
12766 for (i
= 0; i
< acfg
->got_info
.got_patches
->len
; ++i
) {
12767 MonoJumpInfo
*ji
= g_ptr_array_index (acfg
->got_info
.got_patches
, i
);
12769 mono_json_writer_indent (&writer
);
12770 mono_json_writer_printf (&writer
, "{ ");
12771 mono_json_writer_object_key(&writer
, "patch_name");
12772 mono_json_writer_printf (&writer
, "\"%s\" },\n", get_patch_name (ji
->type
));
12775 mono_json_writer_indent_pop (&writer
);
12776 mono_json_writer_indent (&writer
);
12777 mono_json_writer_array_end (&writer
);
12778 mono_json_writer_printf (&writer
, ",\n");
12781 mono_json_writer_indent_pop (&writer
);
12782 mono_json_writer_indent (&writer
);
12783 mono_json_writer_object_end (&writer
);
12785 dumpname
= g_strdup_printf ("%s.json", g_path_get_basename (acfg
->image
->name
));
12786 dumpfile
= fopen (dumpname
, "w+");
12789 fprintf (dumpfile
, "%s", writer
.text
->str
);
12792 mono_json_writer_destroy (&writer
);
12795 static const MonoJitICallId preinited_jit_icalls
[] = {
12796 MONO_JIT_ICALL_mini_llvm_init_method
,
12797 MONO_JIT_ICALL_mini_llvm_init_gshared_method_this
,
12798 MONO_JIT_ICALL_mini_llvm_init_gshared_method_mrgctx
,
12799 MONO_JIT_ICALL_mini_llvm_init_gshared_method_vtable
,
12800 MONO_JIT_ICALL_mini_llvmonly_throw_nullref_exception
,
12801 MONO_JIT_ICALL_mono_llvm_throw_corlib_exception
,
12802 MONO_JIT_ICALL_mono_threads_state_poll
,
12803 MONO_JIT_ICALL_mini_llvmonly_init_vtable_slot
,
12804 MONO_JIT_ICALL_mono_helper_ldstr_mscorlib
,
12805 MONO_JIT_ICALL_mono_fill_method_rgctx
,
12806 MONO_JIT_ICALL_mono_fill_class_rgctx
12810 add_preinit_slot (MonoAotCompile
*acfg
, MonoJumpInfo
*ji
)
12812 if (!acfg
->aot_opts
.llvm_only
)
12813 get_got_offset (acfg
, FALSE
, ji
);
12814 get_got_offset (acfg
, TRUE
, ji
);
12818 add_preinit_got_slots (MonoAotCompile
*acfg
)
12824 * Allocate the first few GOT entries to information which is needed frequently, or it is needed
12825 * during method initialization etc.
12828 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12829 ji
->type
= MONO_PATCH_INFO_IMAGE
;
12830 ji
->data
.image
= acfg
->image
;
12831 add_preinit_slot (acfg
, ji
);
12833 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12834 ji
->type
= MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
;
12835 add_preinit_slot (acfg
, ji
);
12837 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12838 ji
->type
= MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
;
12839 add_preinit_slot (acfg
, ji
);
12841 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12842 ji
->type
= MONO_PATCH_INFO_GC_NURSERY_START
;
12843 add_preinit_slot (acfg
, ji
);
12845 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12846 ji
->type
= MONO_PATCH_INFO_AOT_MODULE
;
12847 add_preinit_slot (acfg
, ji
);
12849 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12850 ji
->type
= MONO_PATCH_INFO_GC_NURSERY_BITS
;
12851 add_preinit_slot (acfg
, ji
);
12853 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12854 ji
->type
= MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
;
12855 add_preinit_slot (acfg
, ji
);
12857 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12858 ji
->type
= MONO_PATCH_INFO_GC_SAFE_POINT_FLAG
;
12859 add_preinit_slot (acfg
, ji
);
12861 if (!acfg
->aot_opts
.llvm_only
) {
12862 for (i
= 0; i
< TLS_KEY_NUM
; i
++) {
12863 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12864 ji
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
;
12865 ji
->data
.jit_icall_id
= mono_get_tls_key_to_jit_icall_id (i
);
12866 add_preinit_slot (acfg
, ji
);
12870 /* Called by native-to-managed wrappers on possibly unattached threads */
12871 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12872 ji
->type
= MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL
;
12873 ji
->data
.jit_icall_id
= MONO_JIT_ICALL_mono_threads_attach_coop
;
12874 add_preinit_slot (acfg
, ji
);
12876 for (i
= 0; i
< G_N_ELEMENTS (preinited_jit_icalls
); ++i
) {
12877 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
12878 ji
->type
= MONO_PATCH_INFO_JIT_ICALL_ID
;
12879 ji
->data
.jit_icall_id
= preinited_jit_icalls
[i
];
12880 add_preinit_slot (acfg
, ji
);
12883 if (acfg
->aot_opts
.llvm_only
)
12884 acfg
->nshared_got_entries
= acfg
->llvm_got_offset
;
12886 acfg
->nshared_got_entries
= acfg
->got_offset
;
12887 g_assert (acfg
->nshared_got_entries
);
12891 mono_dedup_log_stats (MonoAotCompile
*acfg
)
12893 GHashTableIter iter
;
12894 g_assert (acfg
->dedup_stats
);
12896 if (!acfg
->dedup_emit_mode
)
12899 // If dedup_emit_mode, acfg is the dummy dedup module that consolidates
12901 g_hash_table_iter_init (&iter
, acfg
->method_to_cfg
);
12902 MonoCompile
*dcfg
= NULL
;
12903 MonoMethod
*method
= NULL
;
12905 size_t wrappers_size_saved
= 0;
12906 size_t inflated_size_saved
= 0;
12907 size_t copied_singles
= 0;
12908 int wrappers_saved
= 0;
12909 int instances_saved
= 0;
12912 while (g_hash_table_iter_next (&iter
, (gpointer
*) &method
, (gpointer
*)&dcfg
)) {
12913 gchar
*dedup_name
= mono_aot_get_mangled_method_name (method
);
12914 guint count
= GPOINTER_TO_UINT(g_hash_table_lookup (acfg
->dedup_stats
, dedup_name
));
12919 if (acfg
->dedup_emit_mode
) {
12920 // Size *saved* is the size due to things not emitted.
12922 // Just moved, didn't save space / dedup
12924 copied_singles
+= dcfg
->code_len
;
12925 } else if (method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
12927 wrappers_size_saved
+= dcfg
->code_len
* (count
- 1);
12929 instances_saved
++;
12930 inflated_size_saved
+= dcfg
->code_len
* (count
- 1);
12933 if (acfg
->aot_opts
.dedup
) {
12934 if (method
->wrapper_type
!= MONO_WRAPPER_NONE
) {
12936 wrappers_size_saved
+= dcfg
->code_len
* count
;
12938 instances_saved
++;
12939 inflated_size_saved
+= dcfg
->code_len
* count
;
12944 aot_printf (acfg
, "Dedup Pass: Size Saved From Deduped Wrappers:\t%d methods, %zu bytes\n", wrappers_saved
, wrappers_size_saved
);
12945 aot_printf (acfg
, "Dedup Pass: Size Saved From Inflated Methods:\t%d methods, %zu bytes\n", instances_saved
, inflated_size_saved
);
12946 if (acfg
->dedup_emit_mode
)
12947 aot_printf (acfg
, "Dedup Pass: Size of Moved But Not Deduped (only 1 copy) Methods:\t%d methods, %zu bytes\n", copied
, copied_singles
);
12949 g_hash_table_destroy (acfg
->dedup_stats
);
12950 acfg
->dedup_stats
= NULL
;
12953 // Flush the cache to tell future calls what to skip
12955 mono_flush_method_cache (MonoAotCompile
*acfg
)
12957 GHashTable
*method_cache
= acfg
->dedup_cache
;
12958 char *filename
= g_strdup_printf ("%s.dedup", acfg
->image
->name
);
12959 if (!acfg
->dedup_cache_changed
|| !acfg
->aot_opts
.dedup
) {
12964 acfg
->dedup_cache
= NULL
;
12966 FILE *cache
= fopen (filename
, "w");
12969 g_error ("Could not create cache at %s because of error: %s\n", filename
, strerror (errno
));
12971 GHashTableIter iter
;
12972 gchar
*name
= NULL
;
12973 g_hash_table_iter_init (&iter
, method_cache
);
12974 gboolean cont
= TRUE
;
12975 while (cont
&& g_hash_table_iter_next (&iter
, (gpointer
*) &name
, NULL
)) {
12976 int res
= fprintf (cache
, "%s\n", name
);
12979 // FIXME: don't assert if error when flushing
12985 // The keys are all in the imageset, nothing to free
12986 // Values are just pointers to memory owned elsewhere, or sentinels
12987 g_hash_table_destroy (method_cache
);
12990 // Read in what has been emitted by previous invocations,
12991 // what can be skipped
12993 mono_read_method_cache (MonoAotCompile
*acfg
)
12995 char *filename
= g_strdup_printf ("%s.dedup", acfg
->image
->name
);
12996 // Only do once, when dedup_cache is null
12997 if (acfg
->dedup_cache
)
13000 if (acfg
->aot_opts
.dedup_include
|| acfg
->aot_opts
.dedup
)
13001 g_assert (acfg
->dedup_stats
);
13003 // only in skip mode
13004 if (!acfg
->aot_opts
.dedup
)
13007 g_assert (acfg
->dedup_cache
);
13010 cache
= fopen (filename
, "r");
13014 // Since we do pointer comparisons, and it can't be allocated at
13015 // the address 0x1 due to alignment, we use this as a sentinel
13016 gpointer other_acfg_sentinel
;
13017 other_acfg_sentinel
= GINT_TO_POINTER (0x1);
13019 if (fseek (cache
, 0L, SEEK_END
))
13023 fileLength
= ftell (cache
);
13024 g_assert (fileLength
> 0);
13026 if (fseek (cache
, 0L, SEEK_SET
))
13029 // Avoid thousands of new malloc entries
13030 // FIXME: allocate into imageset, so we don't need to free.
13031 // put the other mangled names there too.
13033 bulk
= g_malloc0 (fileLength
* sizeof (char));
13037 while (fgets (&bulk
[offset
], fileLength
- offset
, cache
)) {
13039 char *line
= &bulk
[offset
];
13040 size_t len
= strlen (line
);
13044 if (len
>= 0 && line
[len
] == '\n')
13046 offset
+= strlen (line
) + 1;
13047 g_assert (fileLength
>= offset
);
13049 g_hash_table_insert (acfg
->dedup_cache
, line
, other_acfg_sentinel
);
13063 gboolean emit_inflated_methods
;
13064 MonoAssembly
*inflated_assembly
;
13067 static MonoAotState
*
13068 alloc_aot_state (void)
13070 MonoAotState
*state
= g_malloc (sizeof (MonoAotState
));
13071 // FIXME: Should this own the memory?
13072 state
->cache
= g_hash_table_new (g_str_hash
, g_str_equal
);
13073 state
->stats
= g_hash_table_new (g_str_hash
, g_str_equal
);
13074 // Start in "collect mode"
13075 state
->emit_inflated_methods
= FALSE
;
13076 state
->inflated_assembly
= NULL
;
13081 free_aot_state (MonoAotState
*astate
)
13083 g_hash_table_destroy (astate
->cache
);
13088 mono_add_deferred_extra_methods (MonoAotCompile
*acfg
, MonoAotState
*astate
)
13090 GHashTableIter iter
;
13091 gchar
*name
= NULL
;
13092 MonoMethod
*method
= NULL
;
13094 acfg
->dedup_emit_mode
= TRUE
;
13096 g_hash_table_iter_init (&iter
, astate
->cache
);
13097 while (g_hash_table_iter_next (&iter
, (gpointer
*) &name
, (gpointer
*) &method
)) {
13098 add_method_full (acfg
, method
, TRUE
, 0);
13104 mono_setup_dedup_state (MonoAotCompile
*acfg
, MonoAotState
**global_aot_state
, MonoAssembly
*ass
, MonoAotState
**astate
, gboolean
*is_dedup_dummy
)
13106 if (!acfg
->aot_opts
.dedup_include
&& !acfg
->aot_opts
.dedup
)
13109 if (global_aot_state
&& *global_aot_state
&& acfg
->aot_opts
.dedup_include
) {
13110 // Thread the state through when making the inflate pass
13111 *astate
= *global_aot_state
;
13115 *astate
= alloc_aot_state ();
13116 *global_aot_state
= *astate
;
13119 acfg
->dedup_cache
= (*astate
)->cache
;
13120 acfg
->dedup_stats
= (*astate
)->stats
;
13122 // fills out acfg->dedup_cache
13123 if (acfg
->aot_opts
.dedup
)
13124 mono_read_method_cache (acfg
);
13126 if (!(*astate
)->inflated_assembly
&& acfg
->aot_opts
.dedup_include
) {
13127 gchar
**asm_path
= g_strsplit (ass
->image
->name
, G_DIR_SEPARATOR_S
, 0);
13128 gchar
*asm_file
= NULL
;
13130 // Get the last part of the path, the filename
13131 for (int i
=0; asm_path
[i
] != NULL
; i
++)
13132 asm_file
= asm_path
[i
];
13134 if (!strcmp (acfg
->aot_opts
.dedup_include
, asm_file
)) {
13136 *is_dedup_dummy
= TRUE
;
13137 (*astate
)->inflated_assembly
= ass
;
13139 g_strfreev (asm_path
);
13140 } else if ((*astate
)->inflated_assembly
) {
13141 *is_dedup_dummy
= (ass
== (*astate
)->inflated_assembly
);
13146 mono_compile_deferred_assemblies (guint32 opts
, const char *aot_options
, gpointer
**aot_state
)
13148 // create assembly, loop and add extra_methods
13149 // in add_generic_instances , rip out what's in that for loop
13150 // and apply that to this aot_state inside of mono_compile_assembly
13151 MonoAotState
*astate
;
13152 astate
= *(MonoAotState
**)aot_state
;
13155 // FIXME: allow suffixes?
13156 if (!astate
->inflated_assembly
) {
13157 const char* inflate
= strstr (aot_options
, "dedup-inflate");
13161 g_error ("Error: mono was not given an assembly with the provided inflate name\n");
13165 astate
->emit_inflated_methods
= TRUE
;
13167 int res
= mono_compile_assembly (astate
->inflated_assembly
, opts
, aot_options
, aot_state
);
13170 free_aot_state (astate
);
13175 #ifndef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
13176 static MonoMethodSignature
* const * const interp_in_static_sigs
[] = {
13177 &mono_icall_sig_bool_ptr_int32_ptrref
,
13178 &mono_icall_sig_bool_ptr_ptrref
,
13179 &mono_icall_sig_int32_int32_ptrref
,
13180 &mono_icall_sig_int32_int32_ptr_ptrref
,
13181 &mono_icall_sig_int32_ptr_int32_ptr
,
13182 &mono_icall_sig_int32_ptr_int32_ptrref
,
13183 &mono_icall_sig_int32_ptr_ptrref
,
13184 &mono_icall_sig_object_object_ptr_ptr_ptr
,
13185 &mono_icall_sig_object
,
13186 &mono_icall_sig_ptr_int32_ptrref
,
13187 &mono_icall_sig_ptr_ptr_int32_ptr_ptr_ptrref
,
13188 &mono_icall_sig_ptr_ptr_int32_ptr_ptrref
,
13189 &mono_icall_sig_ptr_ptr_int32_ptrref
,
13190 &mono_icall_sig_ptr_ptr_ptr_int32_ptrref
,
13191 &mono_icall_sig_ptr_ptr_ptr_ptrref_ptrref
,
13192 &mono_icall_sig_ptr_ptr_ptr_ptr_ptrref
,
13193 &mono_icall_sig_ptr_ptr_ptr_ptrref
,
13194 &mono_icall_sig_ptr_ptr_ptrref
,
13195 &mono_icall_sig_ptr_ptr_uint32_ptrref
,
13196 &mono_icall_sig_ptr_uint32_ptrref
,
13197 &mono_icall_sig_void_object_ptr_ptr_ptr
,
13198 &mono_icall_sig_void_ptr_ptr_int32_ptr_ptrref_ptr_ptrref
,
13199 &mono_icall_sig_void_ptr_ptr_int32_ptr_ptrref
,
13200 &mono_icall_sig_void_ptr_ptr_ptrref
,
13201 &mono_icall_sig_void_ptr_ptrref
,
13202 &mono_icall_sig_void_ptr
,
13203 &mono_icall_sig_void_int32_ptrref
,
13204 &mono_icall_sig_void_uint32_ptrref
,
13205 &mono_icall_sig_void
,
13208 // Common signatures for which we use interp in wrappers even in fullaot + trampolines mode
13209 // for increased performance
13210 static MonoMethodSignature
*const * const interp_in_static_common_sigs
[] = {
13211 &mono_icall_sig_void
,
13212 &mono_icall_sig_ptr
,
13213 &mono_icall_sig_void_ptr
,
13214 &mono_icall_sig_ptr_ptr
,
13215 &mono_icall_sig_void_ptr_ptr
,
13216 &mono_icall_sig_ptr_ptr_ptr
,
13217 &mono_icall_sig_void_ptr_ptr_ptr
,
13218 &mono_icall_sig_ptr_ptr_ptr_ptr
,
13219 &mono_icall_sig_void_ptr_ptr_ptr_ptr
,
13220 &mono_icall_sig_ptr_ptr_ptr_ptr_ptr
,
13221 &mono_icall_sig_void_ptr_ptr_ptr_ptr_ptr
,
13222 &mono_icall_sig_ptr_ptr_ptr_ptr_ptr_ptr
,
13223 &mono_icall_sig_void_ptr_ptr_ptr_ptr_ptr_ptr
,
13224 &mono_icall_sig_ptr_ptr_ptr_ptr_ptr_ptr_ptr
,
13229 add_interp_in_wrapper_for_sig (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
)
13231 MonoMethod
*wrapper
;
13233 sig
= mono_metadata_signature_dup_full (mono_get_corlib (), sig
);
13234 sig
->pinvoke
= FALSE
;
13235 wrapper
= mini_get_interp_in_wrapper (sig
);
13236 add_method (acfg
, wrapper
);
13240 mono_compile_assembly (MonoAssembly
*ass
, guint32 opts
, const char *aot_options
, gpointer
**global_aot_state
)
13242 MonoImage
*image
= ass
->image
;
13244 MonoAotCompile
*acfg
;
13249 acfg
= acfg_create (ass
, opts
);
13251 memset (&acfg
->aot_opts
, 0, sizeof (acfg
->aot_opts
));
13252 acfg
->aot_opts
.write_symbols
= TRUE
;
13253 acfg
->aot_opts
.ntrampolines
= 4096;
13254 acfg
->aot_opts
.nrgctx_trampolines
= 4096;
13255 acfg
->aot_opts
.nimt_trampolines
= 512;
13256 acfg
->aot_opts
.nrgctx_fetch_trampolines
= 128;
13257 acfg
->aot_opts
.ngsharedvt_arg_trampolines
= 512;
13258 #ifdef MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE
13259 acfg
->aot_opts
.nftnptr_arg_trampolines
= 128;
13261 acfg
->aot_opts
.nunbox_arbitrary_trampolines
= 256;
13262 if (!acfg
->aot_opts
.llvm_path
)
13263 acfg
->aot_opts
.llvm_path
= g_strdup ("");
13264 acfg
->aot_opts
.temp_path
= g_strdup ("");
13266 acfg
->aot_opts
.use_trampolines_page
= TRUE
;
13268 acfg
->aot_opts
.clangxx
= g_strdup ("clang++");
13270 mono_aot_parse_options (aot_options
, &acfg
->aot_opts
);
13273 MonoAotState
*astate
= NULL
;
13274 gboolean is_dedup_dummy
= FALSE
;
13275 mono_setup_dedup_state (acfg
, (MonoAotState
**) global_aot_state
, ass
, &astate
, &is_dedup_dummy
);
13278 if (is_dedup_dummy
&& astate
&& !astate
->emit_inflated_methods
)
13281 if (acfg
->aot_opts
.dedup_include
&& !is_dedup_dummy
)
13282 acfg
->dedup_collect_only
= TRUE
;
13285 if (acfg
->aot_opts
.logfile
) {
13286 acfg
->logfile
= fopen (acfg
->aot_opts
.logfile
, "a+");
13289 if (acfg
->aot_opts
.data_outfile
) {
13290 acfg
->data_outfile
= fopen (acfg
->aot_opts
.data_outfile
, "w+");
13291 if (!acfg
->data_outfile
) {
13292 aot_printerrf (acfg
, "Unable to create file '%s': %s\n", acfg
->aot_opts
.data_outfile
, strerror (errno
));
13295 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_SEPARATE_DATA
);
13298 //acfg->aot_opts.print_skipped_methods = TRUE;
13300 #if !defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
13301 if (acfg
->opts
& MONO_OPT_GSHAREDVT
) {
13302 aot_printerrf (acfg
, "-O=gsharedvt not supported on this platform.\n");
13305 if (acfg
->aot_opts
.llvm_only
) {
13306 aot_printerrf (acfg
, "--aot=llvmonly requires a runtime that supports gsharedvt.\n");
13310 if (acfg
->aot_opts
.llvm_only
|| mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))
13311 acfg
->opts
|= MONO_OPT_GSHAREDVT
;
13314 #if !defined(ENABLE_LLVM)
13315 if (acfg
->aot_opts
.llvm_only
) {
13316 aot_printerrf (acfg
, "--aot=llvmonly requires a runtime compiled with llvm support.\n");
13321 if (acfg
->opts
& MONO_OPT_GSHAREDVT
)
13322 mono_set_generic_sharing_vt_supported (TRUE
);
13324 if (!acfg
->dedup_collect_only
)
13325 aot_printf (acfg
, "Mono Ahead of Time compiler - compiling assembly %s\n", image
->name
);
13327 if (!acfg
->aot_opts
.deterministic
)
13328 generate_aotid ((guint8
*) &acfg
->image
->aotid
);
13330 char *aotid
= mono_guid_to_string (acfg
->image
->aotid
);
13331 if (!acfg
->dedup_collect_only
)
13332 aot_printf (acfg
, "AOTID %s\n", aotid
);
13335 #ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
13336 if (mono_aot_mode_is_full (&acfg
->aot_opts
)) {
13337 aot_printerrf (acfg
, "--aot=full is not supported on this platform.\n");
13342 if (acfg
->aot_opts
.direct_pinvoke
&& !acfg
->aot_opts
.static_link
) {
13343 aot_printerrf (acfg
, "The 'direct-pinvoke' AOT option also requires the 'static' AOT option.\n");
13347 if (acfg
->aot_opts
.static_link
)
13348 acfg
->aot_opts
.asm_writer
= TRUE
;
13350 if (acfg
->aot_opts
.soft_debug
) {
13351 mini_debug_options
.mdb_optimizations
= TRUE
;
13352 mini_debug_options
.gen_sdb_seq_points
= TRUE
;
13354 if (!mono_debug_enabled ()) {
13355 aot_printerrf (acfg
, "The soft-debug AOT option requires the --debug option.\n");
13358 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_DEBUG
);
13361 if (acfg
->aot_opts
.try_llvm
)
13362 acfg
->aot_opts
.llvm
= mini_llvm_init ();
13364 if (mono_use_llvm
|| acfg
->aot_opts
.llvm
) {
13366 acfg
->aot_opts
.asm_writer
= TRUE
;
13367 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_WITH_LLVM
);
13369 if (acfg
->aot_opts
.soft_debug
) {
13370 aot_printerrf (acfg
, "The 'soft-debug' option is not supported when compiling with LLVM.\n");
13376 if (acfg
->aot_opts
.asm_only
&& !acfg
->aot_opts
.llvm_outfile
) {
13377 aot_printerrf (acfg
, "Compiling with LLVM and the asm-only option requires the llvm-outfile= option.\n");
13382 if (mono_aot_mode_is_full (&acfg
->aot_opts
)) {
13383 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_FULL_AOT
);
13384 acfg
->is_full_aot
= TRUE
;
13387 if (mono_aot_mode_is_interp (&acfg
->aot_opts
)) {
13388 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_INTERP
);
13389 acfg
->is_full_aot
= TRUE
;
13392 if (mini_safepoints_enabled ())
13393 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_SAFEPOINTS
);
13395 // The methods in dedup-emit amodules must be available on runtime startup
13396 // Note: Only one such amodule can have this attribute
13397 if (astate
&& astate
->emit_inflated_methods
)
13398 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_EAGER_LOAD
);
13401 if (acfg
->aot_opts
.instances_logfile_path
) {
13402 acfg
->instances_logfile
= fopen (acfg
->aot_opts
.instances_logfile_path
, "w");
13403 if (!acfg
->instances_logfile
) {
13404 aot_printerrf (acfg
, "Unable to create logfile: '%s'.\n", acfg
->aot_opts
.instances_logfile_path
);
13409 if (acfg
->aot_opts
.profile_files
) {
13412 for (l
= acfg
->aot_opts
.profile_files
; l
; l
= l
->next
) {
13413 load_profile_file (acfg
, (char*)l
->data
);
13417 if (!(mono_aot_mode_is_interp (&acfg
->aot_opts
) && !mono_aot_mode_is_full (&acfg
->aot_opts
))) {
13418 for (int method_index
= 0; method_index
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++method_index
)
13419 g_ptr_array_add (acfg
->method_order
,GUINT_TO_POINTER (method_index
));
13422 acfg
->num_trampolines
[MONO_AOT_TRAMP_SPECIFIC
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.ntrampolines
: 0;
13423 #ifdef MONO_ARCH_GSHARED_SUPPORTED
13424 acfg
->num_trampolines
[MONO_AOT_TRAMP_STATIC_RGCTX
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.nrgctx_trampolines
: 0;
13426 acfg
->num_trampolines
[MONO_AOT_TRAMP_IMT
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.nimt_trampolines
: 0;
13427 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
13428 if (acfg
->opts
& MONO_OPT_GSHAREDVT
)
13429 acfg
->num_trampolines
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.ngsharedvt_arg_trampolines
: 0;
13431 #ifdef MONO_ARCH_HAVE_FTNPTR_ARG_TRAMPOLINE
13432 acfg
->num_trampolines
[MONO_AOT_TRAMP_FTNPTR_ARG
] = mono_aot_mode_is_interp (&acfg
->aot_opts
) ? acfg
->aot_opts
.nftnptr_arg_trampolines
: 0;
13434 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;
13436 acfg
->temp_prefix
= mono_img_writer_get_temp_label_prefix (NULL
);
13440 if (mono_use_llvm
|| acfg
->aot_opts
.llvm
) {
13442 * Emit all LLVM code into a separate assembly/object file and link with it
13445 if (!acfg
->aot_opts
.asm_only
&& acfg
->llvm_owriter_supported
) {
13446 acfg
->llvm_owriter
= TRUE
;
13447 } else if (acfg
->aot_opts
.llvm_outfile
) {
13448 int len
= strlen (acfg
->aot_opts
.llvm_outfile
);
13450 if (len
>= 2 && acfg
->aot_opts
.llvm_outfile
[len
- 2] == '.' && acfg
->aot_opts
.llvm_outfile
[len
- 1] == 'o')
13451 acfg
->llvm_owriter
= TRUE
;
13455 if (acfg
->llvm
&& acfg
->thumb_mixed
)
13456 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_LLVM_THUMB
);
13457 if (acfg
->aot_opts
.llvm_only
)
13458 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_LLVM_ONLY
);
13460 acfg
->assembly_name_sym
= g_strdup (acfg
->image
->assembly
->aname
.name
);
13461 /* Get rid of characters which cannot occur in symbols */
13462 for (p
= acfg
->assembly_name_sym
; *p
; ++p
) {
13463 if (!(isalnum (*p
) || *p
== '_'))
13467 acfg
->global_prefix
= g_strdup_printf ("mono_aot_%s", acfg
->assembly_name_sym
);
13468 acfg
->plt_symbol
= g_strdup_printf ("%s_plt", acfg
->global_prefix
);
13469 acfg
->got_symbol
= g_strdup_printf ("%s_got", acfg
->global_prefix
);
13471 acfg
->llvm_got_symbol
= g_strdup_printf ("%s_llvm_got", acfg
->global_prefix
);
13472 acfg
->llvm_eh_frame_symbol
= g_strdup_printf ("%s_eh_frame", acfg
->global_prefix
);
13475 acfg
->method_index
= 1;
13477 if (mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))
13478 mono_set_partial_sharing_supported (TRUE
);
13480 if (!(mono_aot_mode_is_interp (&acfg
->aot_opts
) && !mono_aot_mode_is_full (&acfg
->aot_opts
))) {
13481 res
= collect_methods (acfg
);
13486 // If we're emitting all of the inflated methods into a dummy
13487 // Assembly, then after extra_methods is set up, we're done
13488 // in this function.
13489 if (astate
&& astate
->emit_inflated_methods
)
13490 mono_add_deferred_extra_methods (acfg
, astate
);
13495 for (l
= acfg
->profile_data
; l
; l
= l
->next
)
13496 resolve_profile_data (acfg
, (ProfileData
*)l
->data
, ass
);
13497 for (l
= acfg
->profile_data
; l
; l
= l
->next
)
13498 add_profile_instances (acfg
, (ProfileData
*)l
->data
);
13501 acfg
->cfgs_size
= acfg
->methods
->len
+ 32;
13502 acfg
->cfgs
= g_new0 (MonoCompile
*, acfg
->cfgs_size
);
13504 /* PLT offset 0 is reserved for the PLT trampoline */
13505 acfg
->plt_offset
= 1;
13506 add_preinit_got_slots (acfg
);
13511 LLVMModuleFlags flags
= (LLVMModuleFlags
)0;
13512 #ifdef EMIT_DWARF_INFO
13513 flags
= LLVM_MODULE_FLAG_DWARF
;
13515 #ifdef EMIT_WIN32_CODEVIEW_INFO
13516 flags
= LLVM_MODULE_FLAG_CODEVIEW
;
13518 if (acfg
->aot_opts
.static_link
)
13519 flags
= (LLVMModuleFlags
)(flags
| LLVM_MODULE_FLAG_STATIC
);
13520 if (acfg
->aot_opts
.llvm_only
)
13521 flags
= (LLVMModuleFlags
)(flags
| LLVM_MODULE_FLAG_LLVM_ONLY
);
13522 if (acfg
->aot_opts
.interp
)
13523 flags
= (LLVMModuleFlags
)(flags
| LLVM_MODULE_FLAG_INTERP
);
13524 mono_llvm_create_aot_module (acfg
->image
->assembly
, acfg
->global_prefix
, acfg
->nshared_got_entries
, flags
);
13526 add_lazy_init_wrappers (acfg
);
13527 add_method (acfg
, mono_marshal_get_llvm_func_wrapper (LLVM_FUNC_WRAPPER_GC_POLL
));
13531 if (mono_aot_mode_is_interp (&acfg
->aot_opts
) && mono_is_corlib_image (acfg
->image
->assembly
->image
)) {
13532 MonoMethod
*wrapper
= mini_get_interp_lmf_wrapper ("mono_interp_to_native_trampoline", (gpointer
) mono_interp_to_native_trampoline
);
13533 add_method (acfg
, wrapper
);
13535 wrapper
= mini_get_interp_lmf_wrapper ("mono_interp_entry_from_trampoline", (gpointer
) mono_interp_entry_from_trampoline
);
13536 add_method (acfg
, wrapper
);
13538 #ifndef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
13539 for (int i
= 0; i
< G_N_ELEMENTS (interp_in_static_sigs
); i
++)
13540 add_interp_in_wrapper_for_sig (acfg
, *interp_in_static_sigs
[i
]);
13542 for (int i
= 0; i
< G_N_ELEMENTS (interp_in_static_common_sigs
); i
++)
13543 add_interp_in_wrapper_for_sig (acfg
, *interp_in_static_common_sigs
[i
]);
13546 /* required for mixed mode */
13547 if (strcmp (acfg
->image
->assembly
->aname
.name
, "mscorlib") == 0) {
13548 add_gc_wrappers (acfg
);
13550 for (int i
= 0; i
< MONO_JIT_ICALL_count
; ++i
)
13551 add_jit_icall_wrapper (acfg
, mono_find_jit_icall_info ((MonoJitICallId
)i
));
13557 compile_methods (acfg
);
13561 acfg
->stats
.jit_time
= TV_ELAPSED (atv
, btv
);
13563 dedup_skip_methods (acfg
);
13565 if (acfg
->aot_opts
.dedup_include
&& !is_dedup_dummy
)
13566 /* We only collected methods from this assembly */
13569 return emit_aot_image (acfg
);
13573 print_stats (MonoAotCompile
*acfg
)
13577 char llvm_stats_msg
[256];
13579 if (acfg
->llvm
&& !acfg
->aot_opts
.llvm_only
)
13580 sprintf (llvm_stats_msg
, ", LLVM: %d (%d%%)", acfg
->stats
.llvm_count
, acfg
->stats
.mcount
? (acfg
->stats
.llvm_count
* 100) / acfg
->stats
.mcount
: 100);
13582 strcpy (llvm_stats_msg
, "");
13584 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
;
13586 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",
13587 (int)acfg
->stats
.code_size
, (int)(acfg
->stats
.code_size
* 100 / all_sizes
),
13588 (int)acfg
->stats
.method_info_size
, (int)(acfg
->stats
.method_info_size
* 100 / all_sizes
),
13589 (int)acfg
->stats
.ex_info_size
, (int)(acfg
->stats
.ex_info_size
* 100 / all_sizes
),
13590 (int)acfg
->stats
.unwind_info_size
, (int)(acfg
->stats
.unwind_info_size
* 100 / all_sizes
),
13591 (int)acfg
->stats
.class_info_size
, (int)(acfg
->stats
.class_info_size
* 100 / all_sizes
),
13592 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,
13593 (int)acfg
->stats
.got_info_size
, (int)(acfg
->stats
.got_info_size
* 100 / all_sizes
),
13594 (int)acfg
->stats
.offsets_size
, (int)(acfg
->stats
.offsets_size
* 100 / all_sizes
),
13595 (int)(acfg
->got_offset
* sizeof (target_mgreg_t
)),
13596 (int)acfg
->stats
.blob_size
);
13597 aot_printf (acfg
, "Compiled: %d/%d (%d%%)%s, No GOT slots: %d (%d%%), Direct calls: %d (%d%%)\n",
13598 acfg
->stats
.ccount
, acfg
->stats
.mcount
, acfg
->stats
.mcount
? (acfg
->stats
.ccount
* 100) / acfg
->stats
.mcount
: 100,
13600 acfg
->stats
.methods_without_got_slots
, acfg
->stats
.mcount
? (acfg
->stats
.methods_without_got_slots
* 100) / acfg
->stats
.mcount
: 100,
13601 acfg
->stats
.direct_calls
, acfg
->stats
.all_calls
? (acfg
->stats
.direct_calls
* 100) / acfg
->stats
.all_calls
: 100);
13602 if (acfg
->stats
.genericcount
)
13603 aot_printf (acfg
, "%d methods failed gsharing (%d%%)\n", acfg
->stats
.genericcount
, acfg
->stats
.mcount
? (acfg
->stats
.genericcount
* 100) / acfg
->stats
.mcount
: 100);
13604 if (acfg
->stats
.abscount
)
13605 aot_printf (acfg
, "%d methods contain absolute addresses (%d%%)\n", acfg
->stats
.abscount
, acfg
->stats
.mcount
? (acfg
->stats
.abscount
* 100) / acfg
->stats
.mcount
: 100);
13606 if (acfg
->stats
.lmfcount
)
13607 aot_printf (acfg
, "%d methods contain lmf pointers (%d%%)\n", acfg
->stats
.lmfcount
, acfg
->stats
.mcount
? (acfg
->stats
.lmfcount
* 100) / acfg
->stats
.mcount
: 100);
13608 if (acfg
->stats
.ocount
)
13609 aot_printf (acfg
, "%d methods have other problems (%d%%)\n", acfg
->stats
.ocount
, acfg
->stats
.mcount
? (acfg
->stats
.ocount
* 100) / acfg
->stats
.mcount
: 100);
13611 aot_printf (acfg
, "GOT slot distribution:\n");
13614 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
) {
13615 nslots
+= acfg
->stats
.got_slot_types
[i
];
13616 size
+= acfg
->stats
.got_slot_info_sizes
[i
];
13617 if (acfg
->stats
.got_slot_types
[i
])
13618 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
]);
13620 aot_printf (acfg
, "GOT SLOTS: %d, INFO SIZE: %d\n", nslots
, size
);
13622 aot_printf (acfg
, "\nEncoding stats:\n");
13623 aot_printf (acfg
, "\tMethod ref: %d (%dk)\n", acfg
->stats
.method_ref_count
, acfg
->stats
.method_ref_size
/ 1024);
13624 aot_printf (acfg
, "\tClass ref: %d (%dk)\n", acfg
->stats
.class_ref_count
, acfg
->stats
.class_ref_size
/ 1024);
13625 aot_printf (acfg
, "\tGinst: %d (%dk)\n", acfg
->stats
.ginst_count
, acfg
->stats
.ginst_size
/ 1024);
13627 aot_printf (acfg
, "\nMethod stats:\n");
13628 aot_printf (acfg
, "\tNormal: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_NORMAL
]);
13629 aot_printf (acfg
, "\tInstance: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_INST
]);
13630 aot_printf (acfg
, "\tGSharedvt: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_GSHAREDVT
]);
13631 aot_printf (acfg
, "\tWrapper: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_WRAPPER
]);
13633 if (acfg
->aot_opts
.dedup
|| acfg
->dedup_emit_mode
)
13634 mono_dedup_log_stats (acfg
);
13638 create_depfile (MonoAotCompile
*acfg
)
13642 // FIXME: Support other configurations
13643 g_assert (acfg
->aot_opts
.llvm_only
&& acfg
->aot_opts
.asm_only
&& acfg
->aot_opts
.llvm_outfile
);
13645 depfile
= fopen (acfg
->aot_opts
.depfile
, "w");
13646 g_assert (depfile
);
13649 char **targets
= g_new0 (char*, ntargets
);
13650 targets
[0] = acfg
->aot_opts
.llvm_outfile
;
13651 for (int tindex
= 0; tindex
< ntargets
; ++tindex
) {
13652 fprintf (depfile
, "%s: ", targets
[tindex
]);
13653 for (int i
= 0; i
< acfg
->image_table
->len
; i
++) {
13654 MonoImage
*image
= (MonoImage
*)g_ptr_array_index (acfg
->image_table
, i
);
13655 fprintf (depfile
, " %s", image
->filename
);
13657 fprintf (depfile
, "\n");
13664 emit_aot_image (MonoAotCompile
*acfg
)
13674 if (acfg
->aot_opts
.asm_only
) {
13675 if (acfg
->aot_opts
.outfile
) {
13676 acfg
->tmpfname
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
13677 acfg
->tmpbasename
= g_strdup (acfg
->tmpfname
);
13679 acfg
->tmpbasename
= g_strdup_printf ("%s", acfg
->image
->name
);
13680 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->tmpbasename
);
13682 g_assert (acfg
->aot_opts
.llvm_outfile
);
13683 acfg
->llvm_sfile
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
13684 if (acfg
->llvm_owriter
)
13685 acfg
->llvm_ofile
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
13687 acfg
->llvm_sfile
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
13690 if (strcmp (acfg
->aot_opts
.temp_path
, "") != 0) {
13691 temp_path
= g_strdup (acfg
->aot_opts
.temp_path
);
13693 temp_path
= g_mkdtemp (g_strdup ("mono_aot_XXXXXX"));
13694 g_assertf (temp_path
, "mkdtemp failed, error = (%d) %s", errno
, g_strerror (errno
));
13695 acfg
->temp_dir_to_delete
= g_strdup (temp_path
);
13698 acfg
->tmpbasename
= g_build_filename (temp_path
, "temp", (const char*)NULL
);
13699 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->tmpbasename
);
13700 acfg
->llvm_sfile
= g_strdup_printf ("%s-llvm.s", acfg
->tmpbasename
);
13701 acfg
->llvm_ofile
= g_strdup_printf ("%s-llvm." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpbasename
);
13703 g_free (temp_path
);
13708 if (acfg
->aot_opts
.asm_only
&& !acfg
->aot_opts
.llvm_only
) {
13709 if (acfg
->aot_opts
.outfile
)
13710 acfg
->tmpfname
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
13712 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->image
->name
);
13713 acfg
->fp
= fopen (acfg
->tmpfname
, "w+");
13715 if (strcmp (acfg
->aot_opts
.temp_path
, "") == 0) {
13716 int i
= g_file_open_tmp ("mono_aot_XXXXXX", &acfg
->tmpfname
, NULL
);
13717 acfg
->fp
= fdopen (i
, "w+");
13719 acfg
->tmpbasename
= g_build_filename (acfg
->aot_opts
.temp_path
, "temp", (const char*)NULL
);
13720 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->tmpbasename
);
13721 acfg
->fp
= fopen (acfg
->tmpfname
, "w+");
13724 if (acfg
->fp
== 0 && !acfg
->aot_opts
.llvm_only
) {
13725 aot_printerrf (acfg
, "Unable to open file '%s': %s\n", acfg
->tmpfname
, strerror (errno
));
13729 acfg
->w
= mono_img_writer_create (acfg
->fp
, FALSE
);
13731 /* Compute symbols for methods */
13732 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
13733 if (acfg
->cfgs
[i
]) {
13734 MonoCompile
*cfg
= acfg
->cfgs
[i
];
13735 int method_index
= get_method_index (acfg
, cfg
->orig_method
);
13737 if (cfg
->asm_symbol
) {
13738 // Set by method emitter in backend
13739 if (acfg
->llvm_label_prefix
) {
13740 char *old_symbol
= cfg
->asm_symbol
;
13741 cfg
->asm_symbol
= g_strdup_printf ("%s%s", acfg
->llvm_label_prefix
, cfg
->asm_symbol
);
13742 g_free (old_symbol
);
13744 } else if (COMPILE_LLVM (cfg
)) {
13745 cfg
->asm_symbol
= g_strdup_printf ("%s%s", acfg
->llvm_label_prefix
, cfg
->llvm_method_name
);
13746 } else if (acfg
->global_symbols
|| acfg
->llvm
) {
13747 cfg
->asm_symbol
= get_debug_sym (cfg
->orig_method
, "", acfg
->method_label_hash
);
13749 cfg
->asm_symbol
= g_strdup_printf ("%s%sm_%x", acfg
->temp_prefix
, acfg
->llvm_label_prefix
, method_index
);
13751 cfg
->asm_debug_symbol
= cfg
->asm_symbol
;
13755 if (acfg
->aot_opts
.dwarf_debug
&& acfg
->aot_opts
.gnu_asm
) {
13757 * CLANG supports GAS .file/.loc directives, so emit line number information this way
13759 acfg
->gas_line_numbers
= TRUE
;
13762 #ifdef EMIT_DWARF_INFO
13763 if ((!acfg
->aot_opts
.nodebug
|| acfg
->aot_opts
.dwarf_debug
) && acfg
->has_jitted_code
) {
13764 if (acfg
->aot_opts
.dwarf_debug
&& !mono_debug_enabled ()) {
13765 aot_printerrf (acfg
, "The dwarf AOT option requires the --debug option.\n");
13768 acfg
->dwarf
= mono_dwarf_writer_create (acfg
->w
, NULL
, 0, !acfg
->gas_line_numbers
);
13770 #endif /* EMIT_DWARF_INFO */
13773 mono_img_writer_emit_start (acfg
->w
);
13776 mono_dwarf_writer_emit_base_info (acfg
->dwarf
, g_path_get_basename (acfg
->image
->name
), mono_unwind_get_cie_program ());
13778 if (acfg
->aot_opts
.dedup
)
13779 mono_flush_method_cache (acfg
);
13785 emit_extra_methods (acfg
);
13787 emit_trampolines (acfg
);
13789 emit_class_name_table (acfg
);
13791 emit_got_info (acfg
, FALSE
);
13793 emit_got_info (acfg
, TRUE
);
13795 emit_exception_info (acfg
);
13797 emit_unwind_info (acfg
);
13799 emit_class_info (acfg
);
13803 emit_image_table (acfg
);
13805 emit_weak_field_indexes (acfg
);
13811 * The managed allocators are GC specific, so can't use an AOT image created by one GC
13814 const char *gc_name
= mono_gc_get_gc_name ();
13815 acfg
->gc_name_offset
= add_to_blob (acfg
, (guint8
*)gc_name
, strlen (gc_name
) + 1);
13820 emit_objc_selectors (acfg
);
13822 emit_globals (acfg
);
13824 emit_file_info (acfg
);
13827 emit_dwarf_info (acfg
);
13828 mono_dwarf_writer_close (acfg
->dwarf
);
13830 if (!acfg
->aot_opts
.nodebug
)
13831 emit_codeview_info (acfg
);
13834 emit_mem_end (acfg
);
13836 if (acfg
->need_pt_gnu_stack
) {
13837 /* This is required so the .so doesn't have an executable stack */
13838 /* The bin writer already emits this */
13839 fprintf (acfg
->fp
, "\n.section .note.GNU-stack,\"\",@progbits\n");
13842 if (acfg
->aot_opts
.data_outfile
)
13843 fclose (acfg
->data_outfile
);
13849 res
= emit_llvm_file (acfg
);
13855 emit_library_info (acfg
);
13859 acfg
->stats
.gen_time
= TV_ELAPSED (atv
, btv
);
13861 if (!acfg
->aot_opts
.stats
)
13862 aot_printf (acfg
, "Compiled: %d/%d\n", acfg
->stats
.ccount
, acfg
->stats
.mcount
);
13866 res
= mono_img_writer_emit_writeout (acfg
->w
);
13871 res
= compile_asm (acfg
);
13878 acfg
->stats
.link_time
= TV_ELAPSED (atv
, btv
);
13880 if (acfg
->aot_opts
.stats
)
13881 print_stats (acfg
);
13883 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);
13885 if (acfg
->aot_opts
.depfile
)
13886 create_depfile (acfg
);
13888 if (acfg
->aot_opts
.dump_json
)
13891 if (!acfg
->aot_opts
.save_temps
&& acfg
->temp_dir_to_delete
) {
13892 char *command
= g_strdup_printf ("rm -r %s", acfg
->temp_dir_to_delete
);
13893 execute_system (command
);
13907 mono_aot_readonly_field_override (MonoClassField
*field
)
13913 mono_compile_assembly (MonoAssembly
*ass
, guint32 opts
, const char *aot_options
, gpointer
**aot_state
)
13919 mono_aot_is_shared_got_offset (int offset
)
13925 mono_compile_deferred_assemblies (guint32 opts
, const char *aot_options
, gpointer
**aot_state
)
13927 g_assert_not_reached ();
13932 mono_aot_direct_icalls_enabled_for_method (MonoCompile
*cfg
, MonoMethod
*method
)
13934 g_assert_not_reached ();