2 * aot-compiler.c: mono Ahead of Time compiler
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
7 * Johan Lorensson (lateralusx.github@gmail.com)
9 * (C) 2002 Ximian, Inc.
10 * Copyright 2003-2011 Novell, Inc
11 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include <sys/types.h>
36 #include <mono/metadata/abi-details.h>
37 #include <mono/metadata/tabledefs.h>
38 #include <mono/metadata/class.h>
39 #include <mono/metadata/object.h>
40 #include <mono/metadata/tokentype.h>
41 #include <mono/metadata/appdomain.h>
42 #include <mono/metadata/debug-helpers.h>
43 #include <mono/metadata/assembly.h>
44 #include <mono/metadata/metadata-internals.h>
45 #include <mono/metadata/reflection-internals.h>
46 #include <mono/metadata/marshal.h>
47 #include <mono/metadata/gc-internals.h>
48 #include <mono/metadata/mempool-internals.h>
49 #include <mono/metadata/mono-endian.h>
50 #include <mono/metadata/threads-types.h>
51 #include <mono/utils/mono-logger-internals.h>
52 #include <mono/utils/mono-compiler.h>
53 #include <mono/utils/mono-time.h>
54 #include <mono/utils/mono-mmap.h>
55 #include <mono/utils/mono-rand.h>
56 #include <mono/utils/json.h>
57 #include <mono/utils/mono-threads-coop.h>
58 #include <mono/io-layer/io-layer.h>
60 #include "aot-compiler.h"
61 #include "seq-points.h"
62 #include "image-writer.h"
63 #include "dwarfwriter.h"
65 #include "mini-llvm.h"
67 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
69 // Use MSVC toolchain, Clang for MSVC using MSVC codegen and linker, when compiling for AMD64
70 // targeting WIN32 platforms running AOT compiler on WIN32 platform with VS installation.
71 #if defined(TARGET_AMD64) && defined(TARGET_WIN32) && defined(HOST_WIN32) && defined(_MSC_VER)
72 #define TARGET_X86_64_WIN32_MSVC
75 #if defined(TARGET_X86_64_WIN32_MSVC)
76 #define TARGET_WIN32_MSVC
79 #if defined(__linux__) || defined(__native_client_codegen__)
80 #define RODATA_SECT ".rodata"
81 #elif defined(TARGET_MACH)
82 #define RODATA_SECT ".section __TEXT, __const"
83 #elif defined(TARGET_WIN32_MSVC)
84 #define RODATA_SECT ".rdata"
86 #define RODATA_SECT ".text"
89 #define TV_DECLARE(name) gint64 name
90 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
91 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
93 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
94 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
95 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
97 /* predefined values for static readonly fields without needed to run the .cctor */
98 typedef struct _ReadOnlyValue ReadOnlyValue;
99 struct _ReadOnlyValue {
102 int type; /* to be used later for typechecking to prevent user errors */
111 static ReadOnlyValue *readonly_values;
113 typedef struct MonoAotOptions {
118 gboolean write_symbols;
119 gboolean metadata_only;
120 gboolean bind_to_runtime_version;
123 gboolean static_link;
127 gboolean dwarf_debug;
129 gboolean log_generics;
130 gboolean log_instances;
131 gboolean gen_msym_dir;
132 char *gen_msym_dir_path;
133 gboolean direct_pinvoke;
134 gboolean direct_icalls;
135 gboolean no_direct_calls;
136 gboolean use_trampolines_page;
137 gboolean no_instances;
143 int nrgctx_trampolines;
144 int nimt_trampolines;
145 int ngsharedvt_arg_trampolines;
146 int nrgctx_fetch_trampolines;
147 gboolean print_skipped_methods;
154 char *instances_logfile_path;
161 METHOD_CAT_GSHAREDVT,
167 typedef struct MonoAotStats {
168 int ccount, mcount, lmfcount, abscount, gcount, ocount, genericcount;
169 gint64 code_size, info_size, ex_info_size, unwind_info_size, got_size, class_info_size, got_info_size, plt_size;
170 int methods_without_got_slots, direct_calls, all_calls, llvm_count;
171 int got_slots, offsets_size;
172 int method_categories [METHOD_CAT_NUM];
173 int got_slot_types [MONO_PATCH_INFO_NUM];
174 int got_slot_info_sizes [MONO_PATCH_INFO_NUM];
175 int jit_time, gen_time, link_time;
178 typedef struct GotInfo {
179 GHashTable *patch_to_got_offset;
180 GHashTable **patch_to_got_offset_by_type;
181 GPtrArray *got_patches;
184 typedef struct MonoAotCompile {
187 GHashTable *method_indexes;
188 GHashTable *method_depth;
191 GHashTable **patch_to_plt_entry;
192 GHashTable *plt_offset_to_entry;
193 //GHashTable *patch_to_got_offset;
194 //GHashTable **patch_to_got_offset_by_type;
195 //GPtrArray *got_patches;
196 GotInfo got_info, llvm_got_info;
197 GHashTable *image_hash;
198 GHashTable *method_to_cfg;
199 GHashTable *token_info_hash;
200 GHashTable *method_to_pinvoke_import;
201 GPtrArray *extra_methods;
202 GPtrArray *image_table;
204 GPtrArray *method_order;
205 GHashTable *export_names;
206 /* Maps MonoClass* -> blob offset */
207 GHashTable *klass_blob_hash;
208 /* Maps MonoMethod* -> blob offset */
209 GHashTable *method_blob_hash;
210 GHashTable *gsharedvt_in_signatures;
211 GHashTable *gsharedvt_out_signatures;
212 guint32 *plt_got_info_offsets;
213 guint32 got_offset, llvm_got_offset, plt_offset, plt_got_offset_base, nshared_got_entries;
214 /* Number of GOT entries reserved for trampolines */
215 guint32 num_trampoline_got_entries;
216 guint32 tramp_page_size;
218 guint32 table_offsets [MONO_AOT_TABLE_NUM];
219 guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
220 guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
221 guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
222 guint32 tramp_page_code_offsets [MONO_AOT_TRAMP_NUM];
224 MonoAotOptions aot_opts;
228 MonoMemPool *mempool;
231 char *static_linking_symbol;
233 gboolean gas_line_numbers;
234 /* Whenever to emit an object file directly from llc */
235 gboolean llvm_owriter;
236 gboolean llvm_owriter_supported;
238 MonoDwarfWriter *dwarf;
245 GHashTable *unwind_info_offsets;
246 GPtrArray *unwind_ops;
247 guint32 unwind_info_offset;
250 char *llvm_got_symbol;
252 char *llvm_eh_frame_symbol;
253 GHashTable *method_label_hash;
254 const char *temp_prefix;
255 const char *user_symbol_prefix;
256 const char *llvm_label_prefix;
257 const char *inst_directive;
259 guint32 label_generator;
261 gboolean has_jitted_code;
262 MonoAotFileFlags flags;
263 MonoDynamicStream blob;
264 gboolean blob_closed;
265 GHashTable *typespec_classes;
268 char *assembly_name_sym;
269 GHashTable *plt_entry_debug_sym_cache;
270 gboolean thumb_mixed, need_no_dead_strip, need_pt_gnu_stack;
271 GHashTable *ginst_hash;
272 GHashTable *dwarf_ln_filenames;
273 gboolean global_symbols;
274 int objc_selector_index, objc_selector_index_2;
275 GPtrArray *objc_selectors;
276 GHashTable *objc_selector_to_index;
278 FILE *instances_logfile;
286 char *symbol, *llvm_symbol, *debug_sym;
288 gboolean jit_used, llvm_used;
291 #define mono_acfg_lock(acfg) mono_os_mutex_lock (&((acfg)->mutex))
292 #define mono_acfg_unlock(acfg) mono_os_mutex_unlock (&((acfg)->mutex))
294 /* This points to the current acfg in LLVM mode */
295 static MonoAotCompile *llvm_acfg;
297 #ifdef HAVE_ARRAY_ELEM_INIT
298 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
299 #define MSGSTRFIELD1(line) str##line
300 static const struct msgstr_t {
301 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
302 #include "patch-info.h"
305 #define PATCH_INFO(a,b) b,
306 #include "patch-info.h"
309 static const gint16 opidx [] = {
310 #define PATCH_INFO(a,b) [MONO_PATCH_INFO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
311 #include "patch-info.h"
315 static G_GNUC_UNUSED const char*
316 get_patch_name (int info)
318 return (const char*)&opstr + opidx [info];
322 #define PATCH_INFO(a,b) b,
323 static const char* const
324 patch_types [MONO_PATCH_INFO_NUM + 1] = {
325 #include "patch-info.h"
329 static G_GNUC_UNUSED const char*
330 get_patch_name (int info)
332 return patch_types [info];
338 get_unwind_info_offset (MonoAotCompile *acfg, guint8 *encoded, guint32 encoded_len);
341 get_plt_entry_debug_sym (MonoAotCompile *acfg, MonoJumpInfo *ji, GHashTable *cache);
344 add_gsharedvt_wrappers (MonoAotCompile *acfg, MonoMethodSignature *sig, gboolean gsharedvt_in, gboolean gsharedvt_out);
347 aot_printf (MonoAotCompile *acfg, const gchar *format, ...)
353 output = acfg->logfile;
357 va_start (args, format);
358 vfprintf (output, format, args);
363 aot_printerrf (MonoAotCompile *acfg, const gchar *format, ...)
369 output = acfg->logfile;
373 va_start (args, format);
374 vfprintf (output, format, args);
379 report_loader_error (MonoAotCompile *acfg, MonoError *error, const char *format, ...)
384 if (mono_error_ok (error))
388 output = acfg->logfile;
392 va_start (args, format);
393 vfprintf (output, format, args);
395 mono_error_cleanup (error);
397 g_error ("FullAOT cannot continue if there are loader errors");
400 /* Wrappers around the image writer functions */
402 #define MAX_SYMBOL_SIZE 256
404 static inline const char *
405 mangle_symbol (const char * symbol, char * mangled_symbol, gsize length)
407 gsize needed_size = length;
409 g_assert (NULL != symbol);
410 g_assert (NULL != mangled_symbol);
411 g_assert (0 != length);
413 #if defined(TARGET_WIN32) && defined(TARGET_X86)
414 if (symbol && '_' != symbol [0]) {
415 needed_size = g_snprintf (mangled_symbol, length, "_%s", symbol);
417 needed_size = g_snprintf (mangled_symbol, length, "%s", symbol);
420 needed_size = g_snprintf (mangled_symbol, length, "%s", symbol);
423 g_assert (0 <= needed_size && needed_size < length);
424 return mangled_symbol;
428 mangle_symbol_alloc (const char * symbol)
430 g_assert (NULL != symbol);
432 #if defined(TARGET_WIN32) && defined(TARGET_X86)
433 if (symbol && '_' != symbol [0]) {
434 return g_strdup_printf ("_%s", symbol);
437 return g_strdup_printf ("%s", symbol);
440 return g_strdup_printf ("%s", symbol);
445 emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
447 mono_img_writer_emit_section_change (acfg->w, section_name, subsection_index);
450 #if defined(TARGET_WIN32) && defined(TARGET_X86)
453 emit_local_symbol (MonoAotCompile *acfg, const char *name, const char *end_label, gboolean func)
455 const char * mangled_symbol_name = name;
456 char * mangled_symbol_name_alloc = NULL;
459 mangled_symbol_name_alloc = mangle_symbol_alloc (name);
460 mangled_symbol_name = mangled_symbol_name_alloc;
463 if (name != mangled_symbol_name && 0 != g_strcasecmp (name, mangled_symbol_name)) {
464 mono_img_writer_emit_label (acfg->w, mangled_symbol_name);
466 mono_img_writer_emit_local_symbol (acfg->w, mangled_symbol_name, end_label, func);
468 if (NULL != mangled_symbol_name_alloc) {
469 g_free (mangled_symbol_name_alloc);
476 emit_local_symbol (MonoAotCompile *acfg, const char *name, const char *end_label, gboolean func)
478 mono_img_writer_emit_local_symbol (acfg->w, name, end_label, func);
484 emit_label (MonoAotCompile *acfg, const char *name)
486 mono_img_writer_emit_label (acfg->w, name);
490 emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
492 mono_img_writer_emit_bytes (acfg->w, buf, size);
496 emit_string (MonoAotCompile *acfg, const char *value)
498 mono_img_writer_emit_string (acfg->w, value);
502 emit_line (MonoAotCompile *acfg)
504 mono_img_writer_emit_line (acfg->w);
508 emit_alignment (MonoAotCompile *acfg, int size)
510 mono_img_writer_emit_alignment (acfg->w, size);
514 emit_alignment_code (MonoAotCompile *acfg, int size)
516 if (acfg->align_pad_value)
517 mono_img_writer_emit_alignment_fill (acfg->w, size, acfg->align_pad_value);
519 mono_img_writer_emit_alignment (acfg->w, size);
523 emit_padding (MonoAotCompile *acfg, int size)
528 if (acfg->align_pad_value) {
529 for (i = 0; i < 16; ++i)
530 buf [i] = acfg->align_pad_value;
532 memset (buf, 0, sizeof (buf));
535 for (i = 0; i < size; i += 16) {
537 emit_bytes (acfg, buf, size - i);
539 emit_bytes (acfg, buf, 16);
544 emit_pointer (MonoAotCompile *acfg, const char *target)
546 mono_img_writer_emit_pointer (acfg->w, target);
550 emit_pointer_2 (MonoAotCompile *acfg, const char *prefix, const char *target)
552 if (prefix [0] != '\0') {
553 char *s = g_strdup_printf ("%s%s", prefix, target);
554 mono_img_writer_emit_pointer (acfg->w, s);
557 mono_img_writer_emit_pointer (acfg->w, target);
562 emit_int16 (MonoAotCompile *acfg, int value)
564 mono_img_writer_emit_int16 (acfg->w, value);
568 emit_int32 (MonoAotCompile *acfg, int value)
570 mono_img_writer_emit_int32 (acfg->w, value);
574 emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
576 mono_img_writer_emit_symbol_diff (acfg->w, end, start, offset);
580 emit_zero_bytes (MonoAotCompile *acfg, int num)
582 mono_img_writer_emit_zero_bytes (acfg->w, num);
586 emit_byte (MonoAotCompile *acfg, guint8 val)
588 mono_img_writer_emit_byte (acfg->w, val);
591 #if defined(TARGET_WIN32) && defined(TARGET_X86)
593 static G_GNUC_UNUSED void
594 emit_global_inner (MonoAotCompile *acfg, const char *name, gboolean func)
596 const char * mangled_symbol_name = name;
597 char * mangled_symbol_name_alloc = NULL;
599 mangled_symbol_name_alloc = mangle_symbol_alloc (name);
600 mangled_symbol_name = mangled_symbol_name_alloc;
602 if (0 != g_strcasecmp (name, mangled_symbol_name)) {
603 mono_img_writer_emit_label (acfg->w, mangled_symbol_name);
605 mono_img_writer_emit_global (acfg->w, mangled_symbol_name, func);
607 if (NULL != mangled_symbol_name_alloc) {
608 g_free (mangled_symbol_name_alloc);
614 static G_GNUC_UNUSED void
615 emit_global_inner (MonoAotCompile *acfg, const char *name, gboolean func)
617 mono_img_writer_emit_global (acfg->w, name, func);
622 static inline gboolean
623 link_shared_library (MonoAotCompile *acfg)
625 return !acfg->aot_opts.static_link && !acfg->aot_opts.asm_only;
628 static inline gboolean
629 add_to_global_symbol_table (MonoAotCompile *acfg)
631 #ifdef TARGET_WIN32_MSVC
632 return acfg->aot_opts.no_dlsym || link_shared_library (acfg);
634 return acfg->aot_opts.no_dlsym;
639 emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
641 if (add_to_global_symbol_table (acfg))
642 g_ptr_array_add (acfg->globals, g_strdup (name));
644 if (acfg->aot_opts.no_dlsym) {
645 mono_img_writer_emit_local_symbol (acfg->w, name, NULL, func);
647 emit_global_inner (acfg, name, func);
652 emit_symbol_size (MonoAotCompile *acfg, const char *name, const char *end_label)
654 mono_img_writer_emit_symbol_size (acfg->w, name, end_label);
657 /* Emit a symbol which is referenced by the MonoAotFileInfo structure */
659 emit_info_symbol (MonoAotCompile *acfg, const char *name)
661 char symbol [MAX_SYMBOL_SIZE];
664 emit_label (acfg, name);
665 /* LLVM generated code references this */
666 sprintf (symbol, "%s%s%s", acfg->user_symbol_prefix, acfg->global_prefix, name);
667 emit_label (acfg, symbol);
668 emit_global_inner (acfg, symbol, FALSE);
670 emit_label (acfg, name);
675 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
678 mono_llvm_emit_aot_data (name, (guint8*)value, strlen (value) + 1);
682 mono_img_writer_emit_section_change (acfg->w, RODATA_SECT, 1);
684 /* On apple, all symbols need to be aligned to avoid warnings from ld */
685 emit_alignment (acfg, 4);
687 mono_img_writer_emit_label (acfg->w, name);
688 mono_img_writer_emit_string (acfg->w, value);
691 static G_GNUC_UNUSED void
692 emit_uleb128 (MonoAotCompile *acfg, guint32 value)
695 guint8 b = value & 0x7f;
697 if (value != 0) /* more bytes to come */
703 static G_GNUC_UNUSED void
704 emit_sleb128 (MonoAotCompile *acfg, gint64 value)
707 gboolean negative = (value < 0);
714 /* the following is unnecessary if the
715 * implementation of >>= uses an arithmetic rather
716 * than logical shift for a signed left operand
720 value |= - ((gint64)1 <<(size - 7));
721 /* sign bit of byte is second high order bit (0x40) */
722 if ((value == 0 && !(byte & 0x40)) ||
723 (value == -1 && (byte & 0x40)))
727 emit_byte (acfg, byte);
731 static G_GNUC_UNUSED void
732 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
737 guint8 b = value & 0x7f;
739 if (value != 0) /* more bytes to come */
747 static G_GNUC_UNUSED void
748 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
751 gboolean negative = (value < 0);
759 /* the following is unnecessary if the
760 * implementation of >>= uses an arithmetic rather
761 * than logical shift for a signed left operand
765 value |= - (1 <<(size - 7));
766 /* sign bit of byte is second high order bit (0x40) */
767 if ((value == 0 && !(byte & 0x40)) ||
768 (value == -1 && (byte & 0x40)))
779 encode_int (gint32 val, guint8 *buf, guint8 **endbuf)
782 buf [0] = (val >> 0) & 0xff;
783 buf [1] = (val >> 8) & 0xff;
784 buf [2] = (val >> 16) & 0xff;
785 buf [3] = (val >> 24) & 0xff;
791 encode_int16 (guint16 val, guint8 *buf, guint8 **endbuf)
793 buf [0] = (val >> 0) & 0xff;
794 buf [1] = (val >> 8) & 0xff;
800 encode_string (const char *s, guint8 *buf, guint8 **endbuf)
802 int len = strlen (s);
804 memcpy (buf, s, len + 1);
805 *endbuf = buf + len + 1;
809 emit_unset_mode (MonoAotCompile *acfg)
811 mono_img_writer_emit_unset_mode (acfg->w);
814 static G_GNUC_UNUSED void
815 emit_set_thumb_mode (MonoAotCompile *acfg)
817 emit_unset_mode (acfg);
818 fprintf (acfg->fp, ".code 16\n");
821 static G_GNUC_UNUSED void
822 emit_set_arm_mode (MonoAotCompile *acfg)
824 emit_unset_mode (acfg);
825 fprintf (acfg->fp, ".code 32\n");
829 emit_code_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
834 g_assert (size % 4 == 0);
835 emit_unset_mode (acfg);
836 for (i = 0; i < size; i += 4)
837 fprintf (acfg->fp, "%s 0x%x\n", acfg->inst_directive, *(guint32*)(buf + i));
839 emit_bytes (acfg, buf, size);
843 /* ARCHITECTURE SPECIFIC CODE */
845 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC) || defined(TARGET_ARM64)
846 #define EMIT_DWARF_INFO 1
849 #ifdef TARGET_WIN32_MSVC
850 #undef EMIT_DWARF_INFO
853 #if defined(TARGET_ARM)
854 #define AOT_FUNC_ALIGNMENT 4
856 #define AOT_FUNC_ALIGNMENT 16
859 #if defined(TARGET_POWERPC64) && !defined(__mono_ilp32__)
860 #define PPC_LD_OP "ld"
861 #define PPC_LDX_OP "ldx"
863 #define PPC_LD_OP "lwz"
864 #define PPC_LDX_OP "lwzx"
867 #ifdef TARGET_X86_64_WIN32_MSVC
868 #define AOT_TARGET_STR "AMD64 (WIN32) (MSVC codegen)"
870 #define AOT_TARGET_STR "AMD64"
875 #define AOT_TARGET_STR "ARM (MACH)"
877 #define AOT_TARGET_STR "ARM (!MACH)"
883 #define AOT_TARGET_STR "ARM64 (MACH)"
885 #define AOT_TARGET_STR "ARM64 (!MACH)"
889 #ifdef TARGET_POWERPC64
890 #ifdef __mono_ilp32__
891 #define AOT_TARGET_STR "POWERPC64 (mono ilp32)"
893 #define AOT_TARGET_STR "POWERPC64 (!mono ilp32)"
896 #ifdef TARGET_POWERPC
897 #ifdef __mono_ilp32__
898 #define AOT_TARGET_STR "POWERPC (mono ilp32)"
900 #define AOT_TARGET_STR "POWERPC (!mono ilp32)"
907 #define AOT_TARGET_STR "X86 (WIN32)"
908 #elif defined(__native_client_codegen__)
909 #define AOT_TARGET_STR "X86 (native client codegen)"
911 #define AOT_TARGET_STR "X86 (!native client codegen)"
915 #ifndef AOT_TARGET_STR
916 #define AOT_TARGET_STR ""
920 arch_init (MonoAotCompile *acfg)
922 acfg->llc_args = g_string_new ("");
923 acfg->as_args = g_string_new ("");
924 acfg->llvm_owriter_supported = TRUE;
927 * The prefix LLVM likes to put in front of symbol names on darwin.
928 * The mach-os specs require this for globals, but LLVM puts them in front of all
929 * symbols. We need to handle this, since we need to refer to LLVM generated
932 acfg->llvm_label_prefix = "";
933 acfg->user_symbol_prefix = "";
935 #if defined(TARGET_X86)
936 g_string_append (acfg->llc_args, " -march=x86 -mattr=sse4.1");
939 #if defined(TARGET_AMD64)
940 g_string_append (acfg->llc_args, " -march=x86-64 -mattr=sse4.1");
942 acfg->align_pad_value = 0x90;
946 if (acfg->aot_opts.mtriple && strstr (acfg->aot_opts.mtriple, "darwin")) {
947 g_string_append (acfg->llc_args, "-mattr=+v6");
949 #if defined(ARM_FPU_VFP_HARD)
950 g_string_append (acfg->llc_args, " -mattr=+vfp2,-neon,+d16 -float-abi=hard");
951 g_string_append (acfg->as_args, " -mfpu=vfp3");
952 #elif defined(ARM_FPU_VFP)
953 g_string_append (acfg->llc_args, " -mattr=+vfp2,-neon,+d16");
954 g_string_append (acfg->as_args, " -mfpu=vfp3");
956 g_string_append (acfg->llc_args, " -soft-float");
959 if (acfg->aot_opts.mtriple && strstr (acfg->aot_opts.mtriple, "thumb"))
960 acfg->thumb_mixed = TRUE;
962 if (acfg->aot_opts.mtriple)
963 mono_arch_set_target (acfg->aot_opts.mtriple);
967 acfg->inst_directive = ".inst";
968 if (acfg->aot_opts.mtriple)
969 mono_arch_set_target (acfg->aot_opts.mtriple);
973 acfg->user_symbol_prefix = "_";
974 acfg->llvm_label_prefix = "_";
975 acfg->inst_directive = ".word";
976 acfg->need_no_dead_strip = TRUE;
977 acfg->aot_opts.gnu_asm = TRUE;
980 #if defined(__linux__) && !defined(TARGET_ARM)
981 acfg->need_pt_gnu_stack = TRUE;
985 acfg->global_symbols = TRUE;
988 #ifdef TARGET_ANDROID
989 acfg->llvm_owriter_supported = FALSE;
996 /* Load the contents of GOT_SLOT into dreg, clobbering ip0 */
998 arm64_emit_load_got_slot (MonoAotCompile *acfg, int dreg, int got_slot)
1002 g_assert (acfg->fp);
1003 emit_unset_mode (acfg);
1005 offset = (int)(got_slot * sizeof (gpointer));
1007 /* clang's integrated assembler */
1008 fprintf (acfg->fp, "adrp x16, %s@PAGE+%d\n", acfg->got_symbol, offset & 0xfffff000);
1009 fprintf (acfg->fp, "add x16, x16, %s@PAGEOFF\n", acfg->got_symbol);
1010 fprintf (acfg->fp, "ldr x%d, [x16, #%d]\n", dreg, offset & 0xfff);
1013 fprintf (acfg->fp, "adrp x16, %s+%d\n", acfg->got_symbol, offset & 0xfffff000);
1014 fprintf (acfg->fp, "add x16, x16, :lo12:%s\n", acfg->got_symbol);
1015 fprintf (acfg->fp, "ldr x%d, [x16, %d]\n", dreg, offset & 0xfff);
1020 arm64_emit_objc_selector_ref (MonoAotCompile *acfg, guint8 *code, int index, int *code_size)
1024 g_assert (acfg->fp);
1025 emit_unset_mode (acfg);
1027 /* ldr rt, target */
1028 reg = arm_get_ldr_lit_reg (code);
1030 fprintf (acfg->fp, "adrp x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGE\n", reg, index);
1031 fprintf (acfg->fp, "add x%d, x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGEOFF\n", reg, reg, index);
1032 fprintf (acfg->fp, "ldr x%d, [x%d]\n", reg, reg);
1038 arm64_emit_direct_call (MonoAotCompile *acfg, const char *target, gboolean external, gboolean thumb, MonoJumpInfo *ji, int *call_size)
1040 g_assert (acfg->fp);
1041 emit_unset_mode (acfg);
1042 if (ji && ji->relocation == MONO_R_ARM64_B) {
1043 fprintf (acfg->fp, "b %s\n", target);
1046 g_assert (ji->relocation == MONO_R_ARM64_BL);
1047 fprintf (acfg->fp, "bl %s\n", target);
1053 arm64_emit_got_access (MonoAotCompile *acfg, guint8 *code, int got_slot, int *code_size)
1057 /* ldr rt, target */
1058 reg = arm_get_ldr_lit_reg (code);
1059 arm64_emit_load_got_slot (acfg, reg, got_slot);
1064 arm64_emit_plt_entry (MonoAotCompile *acfg, const char *got_symbol, int offset, int info_offset)
1066 arm64_emit_load_got_slot (acfg, ARMREG_R16, offset / sizeof (gpointer));
1067 fprintf (acfg->fp, "br x16\n");
1068 /* Used by mono_aot_get_plt_info_offset () */
1069 fprintf (acfg->fp, "%s %d\n", acfg->inst_directive, info_offset);
1073 arm64_emit_tramp_page_common_code (MonoAotCompile *acfg, int pagesize, int arg_reg, int *size)
1079 /* The common code */
1082 /* The trampoline address is in IP0 */
1083 arm_movzx (code, ARMREG_IP1, imm & 0xffff, 0);
1084 arm_movkx (code, ARMREG_IP1, (imm >> 16) & 0xffff, 16);
1085 /* Compute the data slot address */
1086 arm_subx (code, ARMREG_IP0, ARMREG_IP0, ARMREG_IP1);
1087 /* Trampoline argument */
1088 arm_ldrx (code, arg_reg, ARMREG_IP0, 0);
1090 arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, 8);
1091 arm_brx (code, ARMREG_IP0);
1094 emit_code_bytes (acfg, buf, code - buf);
1100 arm64_emit_tramp_page_specific_code (MonoAotCompile *acfg, int pagesize, int common_tramp_size, int specific_tramp_size)
1106 count = (pagesize - common_tramp_size) / specific_tramp_size;
1107 for (i = 0; i < count; ++i) {
1109 arm_adrx (code, ARMREG_IP0, code);
1110 /* Branch to the generic code */
1111 arm_b (code, code - 4 - (i * specific_tramp_size) - common_tramp_size);
1112 /* This has to be 2 pointers long */
1115 g_assert (code - buf == specific_tramp_size);
1116 emit_code_bytes (acfg, buf, code - buf);
1121 arm64_emit_specific_trampoline_pages (MonoAotCompile *acfg)
1125 guint8 *labels [16];
1126 int common_tramp_size;
1127 int specific_tramp_size = 2 * 8;
1131 if (!acfg->aot_opts.use_trampolines_page)
1135 /* Have to match the target pagesize */
1138 pagesize = mono_pagesize ();
1140 acfg->tramp_page_size = pagesize;
1142 /* The specific trampolines */
1143 sprintf (symbol, "%sspecific_trampolines_page", acfg->user_symbol_prefix);
1144 emit_alignment (acfg, pagesize);
1145 emit_global (acfg, symbol, TRUE);
1146 emit_label (acfg, symbol);
1148 /* The common code */
1149 arm64_emit_tramp_page_common_code (acfg, pagesize, ARMREG_IP1, &common_tramp_size);
1150 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_SPECIFIC] = common_tramp_size;
1152 arm64_emit_tramp_page_specific_code (acfg, pagesize, common_tramp_size, specific_tramp_size);
1154 /* The rgctx trampolines */
1155 /* These are the same as the specific trampolines, but they load the argument into MONO_ARCH_RGCTX_REG */
1156 sprintf (symbol, "%srgctx_trampolines_page", acfg->user_symbol_prefix);
1157 emit_alignment (acfg, pagesize);
1158 emit_global (acfg, symbol, TRUE);
1159 emit_label (acfg, symbol);
1161 /* The common code */
1162 arm64_emit_tramp_page_common_code (acfg, pagesize, MONO_ARCH_RGCTX_REG, &common_tramp_size);
1163 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_STATIC_RGCTX] = common_tramp_size;
1165 arm64_emit_tramp_page_specific_code (acfg, pagesize, common_tramp_size, specific_tramp_size);
1167 /* The gsharedvt arg trampolines */
1168 /* These are the same as the specific trampolines */
1169 sprintf (symbol, "%sgsharedvt_arg_trampolines_page", acfg->user_symbol_prefix);
1170 emit_alignment (acfg, pagesize);
1171 emit_global (acfg, symbol, TRUE);
1172 emit_label (acfg, symbol);
1174 arm64_emit_tramp_page_common_code (acfg, pagesize, ARMREG_IP1, &common_tramp_size);
1175 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_GSHAREDVT_ARG] = common_tramp_size;
1177 arm64_emit_tramp_page_specific_code (acfg, pagesize, common_tramp_size, specific_tramp_size);
1179 /* The IMT trampolines */
1180 sprintf (symbol, "%simt_trampolines_page", acfg->user_symbol_prefix);
1181 emit_alignment (acfg, pagesize);
1182 emit_global (acfg, symbol, TRUE);
1183 emit_label (acfg, symbol);
1187 /* The trampoline address is in IP0 */
1188 arm_movzx (code, ARMREG_IP1, imm & 0xffff, 0);
1189 arm_movkx (code, ARMREG_IP1, (imm >> 16) & 0xffff, 16);
1190 /* Compute the data slot address */
1191 arm_subx (code, ARMREG_IP0, ARMREG_IP0, ARMREG_IP1);
1192 /* Trampoline argument */
1193 arm_ldrx (code, ARMREG_IP1, ARMREG_IP0, 0);
1195 /* Same as arch_emit_imt_trampoline () */
1197 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 0);
1198 arm_cmpx (code, ARMREG_IP0, MONO_ARCH_RGCTX_REG);
1200 arm_bcc (code, ARMCOND_EQ, 0);
1202 /* End-of-loop check */
1204 arm_cbzx (code, ARMREG_IP0, 0);
1207 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 2 * 8);
1208 arm_b (code, labels [0]);
1211 mono_arm_patch (labels [1], code, MONO_R_ARM64_BCC);
1212 /* Load vtable slot addr */
1213 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 8);
1214 /* Load vtable slot */
1215 arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, 0);
1216 arm_brx (code, ARMREG_IP0);
1219 mono_arm_patch (labels [2], code, MONO_R_ARM64_CBZ);
1220 /* Load fail addr */
1221 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 8);
1222 arm_brx (code, ARMREG_IP0);
1224 emit_code_bytes (acfg, buf, code - buf);
1226 common_tramp_size = code - buf;
1227 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_IMT] = common_tramp_size;
1229 arm64_emit_tramp_page_specific_code (acfg, pagesize, common_tramp_size, specific_tramp_size);
1233 arm64_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1235 /* Load argument from second GOT slot */
1236 arm64_emit_load_got_slot (acfg, ARMREG_R17, offset + 1);
1237 /* Load generic trampoline address from first GOT slot */
1238 arm64_emit_load_got_slot (acfg, ARMREG_R16, offset);
1239 fprintf (acfg->fp, "br x16\n");
1240 *tramp_size = 7 * 4;
1244 arm64_emit_unbox_trampoline (MonoAotCompile *acfg, MonoCompile *cfg, MonoMethod *method, const char *call_target)
1246 emit_unset_mode (acfg);
1247 fprintf (acfg->fp, "add x0, x0, %d\n", (int)(sizeof (MonoObject)));
1248 fprintf (acfg->fp, "b %s\n", call_target);
1252 arm64_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1254 /* Similar to the specific trampolines, but use the rgctx reg instead of ip1 */
1256 /* Load argument from first GOT slot */
1257 arm64_emit_load_got_slot (acfg, MONO_ARCH_RGCTX_REG, offset);
1258 /* Load generic trampoline address from second GOT slot */
1259 arm64_emit_load_got_slot (acfg, ARMREG_R16, offset + 1);
1260 fprintf (acfg->fp, "br x16\n");
1261 *tramp_size = 7 * 4;
1265 arm64_emit_imt_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1268 guint8 *code, *labels [16];
1270 /* Load parameter from GOT slot into ip1 */
1271 arm64_emit_load_got_slot (acfg, ARMREG_R17, offset);
1275 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 0);
1276 arm_cmpx (code, ARMREG_IP0, MONO_ARCH_RGCTX_REG);
1278 arm_bcc (code, ARMCOND_EQ, 0);
1280 /* End-of-loop check */
1282 arm_cbzx (code, ARMREG_IP0, 0);
1285 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 2 * 8);
1286 arm_b (code, labels [0]);
1289 mono_arm_patch (labels [1], code, MONO_R_ARM64_BCC);
1290 /* Load vtable slot addr */
1291 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 8);
1292 /* Load vtable slot */
1293 arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, 0);
1294 arm_brx (code, ARMREG_IP0);
1297 mono_arm_patch (labels [2], code, MONO_R_ARM64_CBZ);
1298 /* Load fail addr */
1299 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 8);
1300 arm_brx (code, ARMREG_IP0);
1302 emit_code_bytes (acfg, buf, code - buf);
1304 *tramp_size = code - buf + (3 * 4);
1308 arm64_emit_gsharedvt_arg_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1310 /* Similar to the specific trampolines, but the address is in the second slot */
1311 /* Load argument from first GOT slot */
1312 arm64_emit_load_got_slot (acfg, ARMREG_R17, offset);
1313 /* Load generic trampoline address from second GOT slot */
1314 arm64_emit_load_got_slot (acfg, ARMREG_R16, offset + 1);
1315 fprintf (acfg->fp, "br x16\n");
1316 *tramp_size = 7 * 4;
1322 #ifdef MONO_ARCH_AOT_SUPPORTED
1324 * arch_emit_direct_call:
1326 * Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
1330 arch_emit_direct_call (MonoAotCompile *acfg, const char *target, gboolean external, gboolean thumb, MonoJumpInfo *ji, int *call_size)
1332 #if defined(TARGET_X86) || defined(TARGET_AMD64)
1333 /* Need to make sure this is exactly 5 bytes long */
1334 emit_unset_mode (acfg);
1335 fprintf (acfg->fp, "call %s\n", target);
1337 #elif defined(TARGET_ARM)
1338 emit_unset_mode (acfg);
1340 fprintf (acfg->fp, "blx %s\n", target);
1342 fprintf (acfg->fp, "bl %s\n", target);
1344 #elif defined(TARGET_ARM64)
1345 arm64_emit_direct_call (acfg, target, external, thumb, ji, call_size);
1346 #elif defined(TARGET_POWERPC)
1347 emit_unset_mode (acfg);
1348 fprintf (acfg->fp, "bl %s\n", target);
1351 g_assert_not_reached ();
1358 * - we use an approach similar to the x86 abi: reserve a register (r30) to hold
1360 * - The full-aot trampolines need access to the GOT of mscorlib, so we store
1361 * in in the 2. slot of every GOT, and require every method to place the GOT
1362 * address in r30, even when it doesn't access the GOT otherwise. This way,
1363 * the trampolines can compute the mscorlib GOT address by loading 4(r30).
1368 * PPC64 uses function descriptors which greatly complicate all code, since
1369 * these are used very inconsistently in the runtime. Some functions like
1370 * mono_compile_method () return ftn descriptors, while others like the
1371 * trampoline creation functions do not.
1372 * We assume that all GOT slots contain function descriptors, and create
1373 * descriptors in aot-runtime.c when needed.
1374 * The ppc64 abi uses r2 to hold the address of the TOC/GOT, which is loaded
1375 * from function descriptors, we could do the same, but it would require
1376 * rewriting all the ppc/aot code to handle function descriptors properly.
1377 * So instead, we use the same approach as on PPC32.
1378 * This is a horrible mess, but fixing it would probably lead to an even bigger
1384 * - similar to the PPC32 design, we reserve EBX to hold the GOT pointer.
1387 #ifdef MONO_ARCH_AOT_SUPPORTED
1389 * arch_emit_got_offset:
1391 * The memory pointed to by CODE should hold native code for computing the GOT
1392 * address (OP_LOAD_GOTADDR). Emit this code while patching it with the offset
1393 * between code and the GOT. CODE_SIZE is set to the number of bytes emitted.
1396 arch_emit_got_offset (MonoAotCompile *acfg, guint8 *code, int *code_size)
1398 #if defined(TARGET_POWERPC64)
1399 emit_unset_mode (acfg);
1401 * The ppc32 code doesn't seem to work on ppc64, the assembler complains about
1402 * unsupported relocations. So we store the got address into the .Lgot_addr
1403 * symbol which is in the text segment, compute its address, and load it.
1405 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
1406 fprintf (acfg->fp, "lis 0, (.Lgot_addr + 4 - .L%d)@h\n", acfg->label_generator);
1407 fprintf (acfg->fp, "ori 0, 0, (.Lgot_addr + 4 - .L%d)@l\n", acfg->label_generator);
1408 fprintf (acfg->fp, "add 30, 30, 0\n");
1409 fprintf (acfg->fp, "%s 30, 0(30)\n", PPC_LD_OP);
1410 acfg->label_generator ++;
1412 #elif defined(TARGET_POWERPC)
1413 emit_unset_mode (acfg);
1414 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
1415 fprintf (acfg->fp, "lis 0, (%s + 4 - .L%d)@h\n", acfg->got_symbol, acfg->label_generator);
1416 fprintf (acfg->fp, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg->got_symbol, acfg->label_generator);
1417 acfg->label_generator ++;
1420 guint32 offset = mono_arch_get_patch_offset (code);
1421 emit_bytes (acfg, code, offset);
1422 emit_symbol_diff (acfg, acfg->got_symbol, ".", offset);
1424 *code_size = offset + 4;
1429 * arch_emit_got_access:
1431 * The memory pointed to by CODE should hold native code for loading a GOT
1432 * slot (OP_AOTCONST/OP_GOT_ENTRY). Emit this code while patching it so it accesses the
1433 * GOT slot GOT_SLOT. CODE_SIZE is set to the number of bytes emitted.
1436 arch_emit_got_access (MonoAotCompile *acfg, const char *got_symbol, guint8 *code, int got_slot, int *code_size)
1439 /* mov reg, got+offset(%rip) */
1441 /* The GOT symbol is in the LLVM module, the clang assembler has problems emitting symbol diffs for it */
1445 /* Decode reg, see amd64_mov_reg_membase () */
1446 rex_r = code [0] & AMD64_REX_R;
1447 g_assert (code [0] == 0x49 + rex_r);
1448 g_assert (code [1] == 0x8b);
1449 dreg = ((code [2] >> 3) & 0x7) + (rex_r ? 8 : 0);
1451 emit_unset_mode (acfg);
1452 fprintf (acfg->fp, "mov %s+%d(%%rip), %s\n", got_symbol, (unsigned int) ((got_slot * sizeof (gpointer))), mono_arch_regname (dreg));
1455 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1456 emit_symbol_diff (acfg, got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
1457 *code_size = mono_arch_get_patch_offset (code) + 4;
1459 #elif defined(TARGET_X86)
1460 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1461 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
1462 *code_size = mono_arch_get_patch_offset (code) + 4;
1463 #elif defined(TARGET_ARM)
1464 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1465 emit_symbol_diff (acfg, got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
1466 *code_size = mono_arch_get_patch_offset (code) + 4;
1467 #elif defined(TARGET_ARM64)
1468 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1469 arm64_emit_got_access (acfg, code, got_slot, code_size);
1470 #elif defined(TARGET_POWERPC)
1474 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1476 ppc_load32 (code, ppc_r0, got_slot * sizeof (gpointer));
1477 g_assert (code - buf == 8);
1478 emit_bytes (acfg, buf, code - buf);
1479 *code_size = code - buf;
1482 g_assert_not_reached ();
1488 #ifdef MONO_ARCH_AOT_SUPPORTED
1490 * arch_emit_objc_selector_ref:
1492 * Emit the implementation of OP_OBJC_GET_SELECTOR, which itself implements @selector(foo:) in objective-c.
1495 arch_emit_objc_selector_ref (MonoAotCompile *acfg, guint8 *code, int index, int *code_size)
1497 #if defined(TARGET_ARM)
1498 char symbol1 [MAX_SYMBOL_SIZE];
1499 char symbol2 [MAX_SYMBOL_SIZE];
1500 int lindex = acfg->objc_selector_index_2 ++;
1502 /* Emit ldr.imm/b */
1503 emit_bytes (acfg, code, 8);
1505 sprintf (symbol1, "L_OBJC_SELECTOR_%d", lindex);
1506 sprintf (symbol2, "L_OBJC_SELECTOR_REFERENCES_%d", index);
1508 emit_label (acfg, symbol1);
1509 mono_img_writer_emit_unset_mode (acfg->w);
1510 fprintf (acfg->fp, ".long %s-(%s+12)", symbol2, symbol1);
1513 #elif defined(TARGET_ARM64)
1514 arm64_emit_objc_selector_ref (acfg, code, index, code_size);
1516 g_assert_not_reached ();
1522 * arch_emit_plt_entry:
1524 * Emit code for the PLT entry.
1525 * The plt entry should look like this:
1526 * <indirect jump to GOT_SYMBOL + OFFSET>
1527 * <INFO_OFFSET embedded into the instruction stream>
1530 arch_emit_plt_entry (MonoAotCompile *acfg, const char *got_symbol, int offset, int info_offset)
1532 #if defined(TARGET_X86)
1533 /* jmp *<offset>(%ebx) */
1534 emit_byte (acfg, 0xff);
1535 emit_byte (acfg, 0xa3);
1536 emit_int32 (acfg, offset);
1537 /* Used by mono_aot_get_plt_info_offset */
1538 emit_int32 (acfg, info_offset);
1539 #elif defined(TARGET_AMD64)
1540 emit_unset_mode (acfg);
1541 fprintf (acfg->fp, "jmp *%s+%d(%%rip)\n", got_symbol, offset);
1542 /* Used by mono_aot_get_plt_info_offset */
1543 emit_int32 (acfg, info_offset);
1544 acfg->stats.plt_size += 10;
1545 #elif defined(TARGET_ARM)
1550 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1551 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
1552 emit_bytes (acfg, buf, code - buf);
1553 emit_symbol_diff (acfg, got_symbol, ".", offset - 4);
1554 /* Used by mono_aot_get_plt_info_offset */
1555 emit_int32 (acfg, info_offset);
1556 #elif defined(TARGET_ARM64)
1557 arm64_emit_plt_entry (acfg, got_symbol, offset, info_offset);
1558 #elif defined(TARGET_POWERPC)
1559 /* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
1560 emit_unset_mode (acfg);
1561 fprintf (acfg->fp, "lis 11, %d@h\n", offset);
1562 fprintf (acfg->fp, "ori 11, 11, %d@l\n", offset);
1563 fprintf (acfg->fp, "add 11, 11, 30\n");
1564 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1565 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1566 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
1567 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1569 fprintf (acfg->fp, "mtctr 11\n");
1570 fprintf (acfg->fp, "bctr\n");
1571 emit_int32 (acfg, info_offset);
1573 g_assert_not_reached ();
1578 * arch_emit_llvm_plt_entry:
1580 * Same as arch_emit_plt_entry, but handles calls from LLVM generated code.
1581 * This is only needed on arm to handle thumb interop.
1584 arch_emit_llvm_plt_entry (MonoAotCompile *acfg, const char *got_symbol, int offset, int info_offset)
1586 #if defined(TARGET_ARM)
1587 /* LLVM calls the PLT entries using bl, so these have to be thumb2 */
1588 /* The caller already transitioned to thumb */
1589 /* The code below should be 12 bytes long */
1590 /* clang has trouble encoding these instructions, so emit the binary */
1592 fprintf (acfg->fp, "ldr ip, [pc, #8]\n");
1593 /* thumb can't encode ld pc, [pc, ip] */
1594 fprintf (acfg->fp, "add ip, pc, ip\n");
1595 fprintf (acfg->fp, "ldr ip, [ip, #0]\n");
1596 fprintf (acfg->fp, "bx ip\n");
1598 emit_set_thumb_mode (acfg);
1599 fprintf (acfg->fp, ".4byte 0xc008f8df\n");
1600 fprintf (acfg->fp, ".2byte 0x44fc\n");
1601 fprintf (acfg->fp, ".4byte 0xc000f8dc\n");
1602 fprintf (acfg->fp, ".2byte 0x4760\n");
1603 emit_symbol_diff (acfg, got_symbol, ".", offset + 4);
1604 emit_int32 (acfg, info_offset);
1605 emit_unset_mode (acfg);
1606 emit_set_arm_mode (acfg);
1608 g_assert_not_reached ();
1612 /* Save unwind_info in the module and emit the offset to the information at symbol */
1613 static void save_unwind_info (MonoAotCompile *acfg, char *symbol, GSList *unwind_ops)
1615 guint32 uw_offset, encoded_len;
1618 emit_section_change (acfg, RODATA_SECT, 0);
1619 emit_global (acfg, symbol, FALSE);
1620 emit_label (acfg, symbol);
1622 encoded = mono_unwind_ops_encode (unwind_ops, &encoded_len);
1623 uw_offset = get_unwind_info_offset (acfg, encoded, encoded_len);
1625 emit_int32 (acfg, uw_offset);
1629 * arch_emit_specific_trampoline_pages:
1631 * Emits a page full of trampolines: each trampoline uses its own address to
1632 * lookup both the generic trampoline code and the data argument.
1633 * This page can be remapped in process multiple times so we can get an
1634 * unlimited number of trampolines.
1635 * Specifically this implementation uses the following trick: two memory pages
1636 * are allocated, with the first containing the data and the second containing the trampolines.
1637 * To reduce trampoline size, each trampoline jumps at the start of the page where a common
1638 * implementation does all the lifting.
1639 * Note that the ARM single trampoline size is 8 bytes, exactly like the data that needs to be stored
1640 * on the arm 32 bit system.
1643 arch_emit_specific_trampoline_pages (MonoAotCompile *acfg)
1645 #if defined(TARGET_ARM)
1648 guint8 *loop_start, *loop_branch_back, *loop_end_check, *imt_found_check;
1650 int pagesize = MONO_AOT_TRAMP_PAGE_SIZE;
1651 GSList *unwind_ops = NULL;
1652 #define COMMON_TRAMP_SIZE 16
1653 int count = (pagesize - COMMON_TRAMP_SIZE) / 8;
1654 int imm8, rot_amount;
1657 if (!acfg->aot_opts.use_trampolines_page)
1660 acfg->tramp_page_size = pagesize;
1662 sprintf (symbol, "%sspecific_trampolines_page", acfg->user_symbol_prefix);
1663 emit_alignment (acfg, pagesize);
1664 emit_global (acfg, symbol, TRUE);
1665 emit_label (acfg, symbol);
1667 /* emit the generic code first, the trampoline address + 8 is in the lr register */
1669 imm8 = mono_arm_is_rotated_imm8 (pagesize, &rot_amount);
1670 ARM_SUB_REG_IMM (code, ARMREG_LR, ARMREG_LR, imm8, rot_amount);
1671 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, -8);
1672 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_LR, -4);
1674 g_assert (code - buf == COMMON_TRAMP_SIZE);
1677 emit_bytes (acfg, buf, code - buf);
1679 for (i = 0; i < count; ++i) {
1681 ARM_PUSH (code, 0x5fff);
1683 arm_patch (code - 4, code - COMMON_TRAMP_SIZE - 8 * (i + 1));
1684 g_assert (code - buf == 8);
1685 emit_bytes (acfg, buf, code - buf);
1688 /* now the rgctx trampolines: each specific trampolines puts in the ip register
1689 * the instruction pointer address, so the generic trampoline at the start of the page
1690 * subtracts 4096 to get to the data page and loads the values
1691 * We again fit the generic trampiline in 16 bytes.
1693 sprintf (symbol, "%srgctx_trampolines_page", acfg->user_symbol_prefix);
1694 emit_global (acfg, symbol, TRUE);
1695 emit_label (acfg, symbol);
1697 imm8 = mono_arm_is_rotated_imm8 (pagesize, &rot_amount);
1698 ARM_SUB_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
1699 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, -8);
1700 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, -4);
1702 g_assert (code - buf == COMMON_TRAMP_SIZE);
1705 emit_bytes (acfg, buf, code - buf);
1707 for (i = 0; i < count; ++i) {
1709 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
1711 arm_patch (code - 4, code - COMMON_TRAMP_SIZE - 8 * (i + 1));
1712 g_assert (code - buf == 8);
1713 emit_bytes (acfg, buf, code - buf);
1717 * gsharedvt arg trampolines: see arch_emit_gsharedvt_arg_trampoline ()
1719 sprintf (symbol, "%sgsharedvt_arg_trampolines_page", acfg->user_symbol_prefix);
1720 emit_global (acfg, symbol, TRUE);
1721 emit_label (acfg, symbol);
1723 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3));
1724 imm8 = mono_arm_is_rotated_imm8 (pagesize, &rot_amount);
1725 ARM_SUB_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
1726 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_IP, -8);
1727 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, -4);
1728 g_assert (code - buf == COMMON_TRAMP_SIZE);
1730 emit_bytes (acfg, buf, code - buf);
1732 for (i = 0; i < count; ++i) {
1734 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
1736 arm_patch (code - 4, code - COMMON_TRAMP_SIZE - 8 * (i + 1));
1737 g_assert (code - buf == 8);
1738 emit_bytes (acfg, buf, code - buf);
1741 /* now the imt trampolines: each specific trampolines puts in the ip register
1742 * the instruction pointer address, so the generic trampoline at the start of the page
1743 * subtracts 4096 to get to the data page and loads the values
1745 #define IMT_TRAMP_SIZE 72
1746 sprintf (symbol, "%simt_trampolines_page", acfg->user_symbol_prefix);
1747 emit_global (acfg, symbol, TRUE);
1748 emit_label (acfg, symbol);
1750 /* Need at least two free registers, plus a slot for storing the pc */
1751 ARM_PUSH (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_R2));
1753 imm8 = mono_arm_is_rotated_imm8 (pagesize, &rot_amount);
1754 ARM_SUB_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
1755 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_IP, -8);
1757 /* The IMT method is in v5, r0 has the imt array address */
1760 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, 0);
1761 ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
1762 imt_found_check = code;
1763 ARM_B_COND (code, ARMCOND_EQ, 0);
1765 /* End-of-loop check */
1766 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
1767 loop_end_check = code;
1768 ARM_B_COND (code, ARMCOND_EQ, 0);
1771 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (gpointer) * 2);
1772 loop_branch_back = code;
1774 arm_patch (loop_branch_back, loop_start);
1777 arm_patch (imt_found_check, code);
1778 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1779 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 0);
1780 /* Save it to the third stack slot */
1781 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1782 /* Restore the registers and branch */
1783 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1786 arm_patch (loop_end_check, code);
1787 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1788 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1789 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1793 g_assert (code - buf == IMT_TRAMP_SIZE);
1794 emit_bytes (acfg, buf, code - buf);
1796 for (i = 0; i < count; ++i) {
1798 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
1800 arm_patch (code - 4, code - IMT_TRAMP_SIZE - 8 * (i + 1));
1801 g_assert (code - buf == 8);
1802 emit_bytes (acfg, buf, code - buf);
1805 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_SPECIFIC] = 16;
1806 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_STATIC_RGCTX] = 16;
1807 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_IMT] = 72;
1808 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_GSHAREDVT_ARG] = 16;
1810 /* Unwind info for specifc trampolines */
1811 sprintf (symbol, "%sspecific_trampolines_page_gen_p", acfg->user_symbol_prefix);
1812 /* We unwind to the original caller, from the stack, since lr is clobbered */
1813 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 14 * sizeof (mgreg_t));
1814 mono_add_unwind_op_offset (unwind_ops, 0, 0, ARMREG_LR, -4);
1815 save_unwind_info (acfg, symbol, unwind_ops);
1816 mono_free_unwind_info (unwind_ops);
1818 sprintf (symbol, "%sspecific_trampolines_page_sp_p", acfg->user_symbol_prefix);
1819 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1820 mono_add_unwind_op_def_cfa_offset (unwind_ops, 4, 0, 14 * sizeof (mgreg_t));
1821 save_unwind_info (acfg, symbol, unwind_ops);
1822 mono_free_unwind_info (unwind_ops);
1824 /* Unwind info for rgctx trampolines */
1825 sprintf (symbol, "%srgctx_trampolines_page_gen_p", acfg->user_symbol_prefix);
1826 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1827 save_unwind_info (acfg, symbol, unwind_ops);
1829 sprintf (symbol, "%srgctx_trampolines_page_sp_p", acfg->user_symbol_prefix);
1830 save_unwind_info (acfg, symbol, unwind_ops);
1831 mono_free_unwind_info (unwind_ops);
1833 /* Unwind info for gsharedvt trampolines */
1834 sprintf (symbol, "%sgsharedvt_trampolines_page_gen_p", acfg->user_symbol_prefix);
1835 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1836 mono_add_unwind_op_def_cfa_offset (unwind_ops, 4, 0, 4 * sizeof (mgreg_t));
1837 save_unwind_info (acfg, symbol, unwind_ops);
1838 mono_free_unwind_info (unwind_ops);
1840 sprintf (symbol, "%sgsharedvt_trampolines_page_sp_p", acfg->user_symbol_prefix);
1841 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1842 save_unwind_info (acfg, symbol, unwind_ops);
1843 mono_free_unwind_info (unwind_ops);
1845 /* Unwind info for imt trampolines */
1846 sprintf (symbol, "%simt_trampolines_page_gen_p", acfg->user_symbol_prefix);
1847 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1848 mono_add_unwind_op_def_cfa_offset (unwind_ops, 4, 0, 3 * sizeof (mgreg_t));
1849 save_unwind_info (acfg, symbol, unwind_ops);
1850 mono_free_unwind_info (unwind_ops);
1852 sprintf (symbol, "%simt_trampolines_page_sp_p", acfg->user_symbol_prefix);
1853 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1854 save_unwind_info (acfg, symbol, unwind_ops);
1855 mono_free_unwind_info (unwind_ops);
1856 #elif defined(TARGET_ARM64)
1857 arm64_emit_specific_trampoline_pages (acfg);
1862 * arch_emit_specific_trampoline:
1864 * Emit code for a specific trampoline. OFFSET is the offset of the first of
1865 * two GOT slots which contain the generic trampoline address and the trampoline
1866 * argument. TRAMP_SIZE is set to the size of the emitted trampoline.
1869 arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1872 * The trampolines created here are variations of the specific
1873 * trampolines created in mono_arch_create_specific_trampoline (). The
1875 * - the generic trampoline address is taken from a got slot.
1876 * - the offset of the got slot where the trampoline argument is stored
1877 * is embedded in the instruction stream, and the generic trampoline
1878 * can load the argument by loading the offset, adding it to the
1879 * address of the trampoline to get the address of the got slot, and
1880 * loading the argument from there.
1881 * - all the trampolines should be of the same length.
1883 #if defined(TARGET_AMD64)
1884 /* This should be exactly 8 bytes long */
1886 /* call *<offset>(%rip) */
1888 emit_unset_mode (acfg);
1889 fprintf (acfg->fp, "call *%s+%d(%%rip)\n", acfg->got_symbol, (int)(offset * sizeof (gpointer)));
1890 emit_zero_bytes (acfg, 2);
1892 emit_byte (acfg, '\x41');
1893 emit_byte (acfg, '\xff');
1894 emit_byte (acfg, '\x15');
1895 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
1896 emit_zero_bytes (acfg, 1);
1898 #elif defined(TARGET_ARM)
1902 /* This should be exactly 20 bytes long */
1905 ARM_PUSH (code, 0x5fff);
1906 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 4);
1907 /* Load the value from the GOT */
1908 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
1910 ARM_BLX_REG (code, ARMREG_R1);
1912 g_assert (code - buf == 16);
1915 emit_bytes (acfg, buf, code - buf);
1917 * Only one offset is needed, since the second one would be equal to the
1920 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 4);
1921 //emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 8);
1922 #elif defined(TARGET_ARM64)
1923 arm64_emit_specific_trampoline (acfg, offset, tramp_size);
1924 #elif defined(TARGET_POWERPC)
1932 * PPC has no ip relative addressing, so we need to compute the address
1933 * of the mscorlib got. That is slow and complex, so instead, we store it
1934 * in the second got slot of every aot image. The caller already computed
1935 * the address of its got and placed it into r30.
1937 emit_unset_mode (acfg);
1938 /* Load mscorlib got address */
1939 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
1940 /* Load generic trampoline address */
1941 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
1942 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
1943 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
1944 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1945 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1947 fprintf (acfg->fp, "mtctr 11\n");
1948 /* Load trampoline argument */
1949 /* On ppc, we pass it normally to the generic trampoline */
1950 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
1951 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
1952 fprintf (acfg->fp, "%s 0, 11, 0\n", PPC_LDX_OP);
1953 /* Branch to generic trampoline */
1954 fprintf (acfg->fp, "bctr\n");
1956 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1957 *tramp_size = 10 * 4;
1959 *tramp_size = 9 * 4;
1961 #elif defined(TARGET_X86)
1965 /* Similar to the PPC code above */
1967 /* FIXME: Could this clobber the register needed by get_vcall_slot () ? */
1970 /* Load mscorlib got address */
1971 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
1972 /* Push trampoline argument */
1973 x86_push_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
1974 /* Load generic trampoline address */
1975 x86_mov_reg_membase (code, X86_ECX, X86_ECX, offset * sizeof (gpointer), 4);
1976 /* Branch to generic trampoline */
1977 x86_jump_reg (code, X86_ECX);
1979 emit_bytes (acfg, buf, code - buf);
1982 g_assert (code - buf == *tramp_size);
1984 g_assert_not_reached ();
1989 * arch_emit_unbox_trampoline:
1991 * Emit code for the unbox trampoline for METHOD used in the full-aot case.
1992 * CALL_TARGET is the symbol pointing to the native code of METHOD.
1995 arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoCompile *cfg, MonoMethod *method, const char *call_target)
1997 #if defined(TARGET_AMD64)
2002 this_reg = mono_arch_get_this_arg_reg (NULL);
2004 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
2006 emit_bytes (acfg, buf, code - buf);
2009 emit_unset_mode (acfg);
2010 fprintf (acfg->fp, "jmp %s\n", call_target);
2012 emit_byte (acfg, '\xe9');
2013 emit_symbol_diff (acfg, call_target, ".", -4);
2015 #elif defined(TARGET_X86)
2022 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
2024 emit_bytes (acfg, buf, code - buf);
2027 emit_byte (acfg, '\xe9');
2028 emit_symbol_diff (acfg, call_target, ".", -4);
2029 #elif defined(TARGET_ARM)
2033 if (acfg->thumb_mixed && cfg->compile_llvm) {
2034 fprintf (acfg->fp, "add r0, r0, #%d\n", (int)sizeof (MonoObject));
2035 fprintf (acfg->fp, "b %s\n", call_target);
2036 fprintf (acfg->fp, ".arm\n");
2037 fprintf (acfg->fp, ".align 2\n");
2043 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
2045 emit_bytes (acfg, buf, code - buf);
2046 /* jump to method */
2047 if (acfg->thumb_mixed && cfg->compile_llvm)
2048 fprintf (acfg->fp, "\n\tbx %s\n", call_target);
2050 fprintf (acfg->fp, "\n\tb %s\n", call_target);
2051 #elif defined(TARGET_ARM64)
2052 arm64_emit_unbox_trampoline (acfg, cfg, method, call_target);
2053 #elif defined(TARGET_POWERPC)
2056 fprintf (acfg->fp, "\n\taddi %d, %d, %d\n", this_pos, this_pos, (int)sizeof (MonoObject));
2057 fprintf (acfg->fp, "\n\tb %s\n", call_target);
2059 g_assert_not_reached ();
2064 * arch_emit_static_rgctx_trampoline:
2066 * Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
2067 * two GOT slots which contain the rgctx argument, and the method to jump to.
2068 * TRAMP_SIZE is set to the size of the emitted trampoline.
2069 * These kinds of trampolines cannot be enumerated statically, since there could
2070 * be one trampoline per method instantiation, so we emit the same code for all
2071 * trampolines, and parameterize them using two GOT slots.
2074 arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
2076 #if defined(TARGET_AMD64)
2077 /* This should be exactly 13 bytes long */
2081 emit_unset_mode (acfg);
2082 fprintf (acfg->fp, "mov %s+%d(%%rip), %%r10\n", acfg->got_symbol, (int)(offset * sizeof (gpointer)));
2083 fprintf (acfg->fp, "jmp *%s+%d(%%rip)\n", acfg->got_symbol, (int)((offset + 1) * sizeof (gpointer)));
2085 /* mov <OFFSET>(%rip), %r10 */
2086 emit_byte (acfg, '\x4d');
2087 emit_byte (acfg, '\x8b');
2088 emit_byte (acfg, '\x15');
2089 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
2091 /* jmp *<offset>(%rip) */
2092 emit_byte (acfg, '\xff');
2093 emit_byte (acfg, '\x25');
2094 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
2096 #elif defined(TARGET_ARM)
2100 /* This should be exactly 24 bytes long */
2103 /* Load rgctx value */
2104 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
2105 ARM_LDR_REG_REG (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, ARMREG_IP);
2106 /* Load branch addr + branch */
2107 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
2108 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
2110 g_assert (code - buf == 16);
2113 emit_bytes (acfg, buf, code - buf);
2114 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 8);
2115 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 4);
2116 #elif defined(TARGET_ARM64)
2117 arm64_emit_static_rgctx_trampoline (acfg, offset, tramp_size);
2118 #elif defined(TARGET_POWERPC)
2126 * PPC has no ip relative addressing, so we need to compute the address
2127 * of the mscorlib got. That is slow and complex, so instead, we store it
2128 * in the second got slot of every aot image. The caller already computed
2129 * the address of its got and placed it into r30.
2131 emit_unset_mode (acfg);
2132 /* Load mscorlib got address */
2133 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
2135 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
2136 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
2137 fprintf (acfg->fp, "%s %d, 11, 0\n", PPC_LDX_OP, MONO_ARCH_RGCTX_REG);
2138 /* Load target address */
2139 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
2140 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
2141 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
2142 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2143 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
2144 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
2146 fprintf (acfg->fp, "mtctr 11\n");
2147 /* Branch to the target address */
2148 fprintf (acfg->fp, "bctr\n");
2150 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2151 *tramp_size = 11 * 4;
2153 *tramp_size = 9 * 4;
2156 #elif defined(TARGET_X86)
2160 /* Similar to the PPC code above */
2162 g_assert (MONO_ARCH_RGCTX_REG != X86_ECX);
2165 /* Load mscorlib got address */
2166 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
2168 x86_mov_reg_membase (code, MONO_ARCH_RGCTX_REG, X86_ECX, offset * sizeof (gpointer), 4);
2169 /* Branch to the target address */
2170 x86_jump_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
2172 emit_bytes (acfg, buf, code - buf);
2175 g_assert (code - buf == *tramp_size);
2177 g_assert_not_reached ();
2182 * arch_emit_imt_trampoline:
2184 * Emit an IMT trampoline usable in full-aot mode. The trampoline uses 1 got slot which
2185 * points to an array of pointer pairs. The pairs of the form [key, ptr], where
2186 * key is the IMT key, and ptr holds the address of a memory location holding
2187 * the address to branch to if the IMT arg matches the key. The array is
2188 * terminated by a pair whose key is NULL, and whose ptr is the address of the
2190 * TRAMP_SIZE is set to the size of the emitted trampoline.
2193 arch_emit_imt_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
2195 #if defined(TARGET_AMD64)
2197 guint8 *labels [16];
2199 guint8 *mov_buf_ptr = mov_buf;
2201 const int kSizeOfMove = 7;
2203 code = buf = (guint8 *)g_malloc (256);
2205 /* FIXME: Optimize this, i.e. use binary search etc. */
2206 /* Maybe move the body into a separate function (slower, but much smaller) */
2208 /* MONO_ARCH_IMT_SCRATCH_REG is a free register */
2211 emit_unset_mode (acfg);
2212 fprintf (acfg->fp, "mov %s+%d(%%rip), %s\n", acfg->got_symbol, (int)(offset * sizeof (gpointer)), mono_arch_regname (MONO_ARCH_IMT_SCRATCH_REG));
2216 amd64_alu_membase_imm (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, 0);
2218 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
2221 amd64_alu_membase_reg_size (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, MONO_ARCH_IMT_REG, sizeof (gpointer));
2223 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
2226 amd64_alu_reg_imm (code, X86_ADD, MONO_ARCH_IMT_SCRATCH_REG, 2 * sizeof (gpointer));
2227 amd64_jump_code (code, labels [0]);
2230 mono_amd64_patch (labels [2], code);
2231 amd64_mov_reg_membase (code, MONO_ARCH_IMT_SCRATCH_REG, MONO_ARCH_IMT_SCRATCH_REG, sizeof (gpointer), sizeof (gpointer));
2232 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
2235 mono_amd64_patch (labels [1], code);
2236 /* Load fail tramp */
2237 amd64_alu_reg_imm (code, X86_ADD, MONO_ARCH_IMT_SCRATCH_REG, sizeof (gpointer));
2238 /* Check if there is a fail tramp */
2239 amd64_alu_membase_imm (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, 0);
2241 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
2242 /* Jump to fail tramp */
2243 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
2246 mono_amd64_patch (labels [3], code);
2247 x86_breakpoint (code);
2250 /* mov <OFFSET>(%rip), MONO_ARCH_IMT_SCRATCH_REG */
2251 amd64_emit_rex (mov_buf_ptr, sizeof(gpointer), MONO_ARCH_IMT_SCRATCH_REG, 0, AMD64_RIP);
2252 *(mov_buf_ptr)++ = (unsigned char)0x8b; /* mov opcode */
2253 x86_address_byte (mov_buf_ptr, 0, MONO_ARCH_IMT_SCRATCH_REG & 0x7, 5);
2254 emit_bytes (acfg, mov_buf, mov_buf_ptr - mov_buf);
2255 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
2257 emit_bytes (acfg, buf, code - buf);
2259 *tramp_size = code - buf + kSizeOfMove;
2263 #elif defined(TARGET_X86)
2265 guint8 *labels [16];
2267 code = buf = g_malloc (256);
2269 /* Allocate a temporary stack slot */
2270 x86_push_reg (code, X86_EAX);
2272 x86_push_reg (code, X86_EAX);
2274 /* Load mscorlib got address */
2275 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
2277 x86_mov_reg_membase (code, X86_EAX, X86_EAX, offset * sizeof (gpointer), 4);
2280 x86_alu_membase_imm (code, X86_CMP, X86_EAX, 0, 0);
2282 x86_branch8 (code, X86_CC_Z, FALSE, 0);
2285 x86_alu_membase_reg (code, X86_CMP, X86_EAX, 0, MONO_ARCH_IMT_REG);
2287 x86_branch8 (code, X86_CC_Z, FALSE, 0);
2290 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 2 * sizeof (gpointer));
2291 x86_jump_code (code, labels [0]);
2294 mono_x86_patch (labels [2], code);
2295 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer), 4);
2296 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
2297 /* Save the target address to the temporary stack location */
2298 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
2300 x86_pop_reg (code, X86_EAX);
2301 /* Jump to the target address */
2305 mono_x86_patch (labels [1], code);
2306 /* Load fail tramp */
2307 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer), 4);
2308 x86_alu_membase_imm (code, X86_CMP, X86_EAX, 0, 0);
2310 x86_branch8 (code, X86_CC_Z, FALSE, 0);
2311 /* Jump to fail tramp */
2312 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
2313 x86_pop_reg (code, X86_EAX);
2317 mono_x86_patch (labels [3], code);
2318 x86_breakpoint (code);
2320 emit_bytes (acfg, buf, code - buf);
2322 *tramp_size = code - buf;
2326 #elif defined(TARGET_ARM)
2328 guint8 *code, *code2, *labels [16];
2332 /* The IMT method is in v5 */
2334 /* Need at least two free registers, plus a slot for storing the pc */
2335 ARM_PUSH (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_R2));
2337 /* Load the parameter from the GOT */
2338 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
2339 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
2342 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, 0);
2343 ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
2345 ARM_B_COND (code, ARMCOND_EQ, 0);
2347 /* End-of-loop check */
2348 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
2350 ARM_B_COND (code, ARMCOND_EQ, 0);
2353 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (gpointer) * 2);
2356 arm_patch (labels [4], labels [1]);
2359 arm_patch (labels [2], code);
2360 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
2361 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 0);
2362 /* Save it to the third stack slot */
2363 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
2364 /* Restore the registers and branch */
2365 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
2368 arm_patch (labels [3], code);
2369 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
2370 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
2371 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
2375 ARM_LDR_IMM (code2, ARMREG_R0, ARMREG_PC, (code - (labels [0] + 8)));
2377 emit_bytes (acfg, buf, code - buf);
2378 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + (code - (labels [0] + 8)) - 4);
2380 *tramp_size = code - buf + 4;
2381 #elif defined(TARGET_ARM64)
2382 arm64_emit_imt_trampoline (acfg, offset, tramp_size);
2383 #elif defined(TARGET_POWERPC)
2385 guint8 *code, *labels [16];
2389 /* Load the mscorlib got address */
2390 ppc_ldptr (code, ppc_r12, sizeof (gpointer), ppc_r30);
2391 /* Load the parameter from the GOT */
2392 ppc_load (code, ppc_r0, offset * sizeof (gpointer));
2393 ppc_ldptr_indexed (code, ppc_r12, ppc_r12, ppc_r0);
2395 /* Load and check key */
2397 ppc_ldptr (code, ppc_r0, 0, ppc_r12);
2398 ppc_cmp (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, MONO_ARCH_IMT_REG);
2400 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
2402 /* End-of-loop check */
2403 ppc_cmpi (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, 0);
2405 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
2408 ppc_addi (code, ppc_r12, ppc_r12, 2 * sizeof (gpointer));
2411 mono_ppc_patch (labels [4], labels [1]);
2414 mono_ppc_patch (labels [2], code);
2415 ppc_ldptr (code, ppc_r12, sizeof (gpointer), ppc_r12);
2416 /* r12 now contains the value of the vtable slot */
2417 /* this is not a function descriptor on ppc64 */
2418 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
2419 ppc_mtctr (code, ppc_r12);
2420 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2423 mono_ppc_patch (labels [3], code);
2427 *tramp_size = code - buf;
2429 emit_bytes (acfg, buf, code - buf);
2431 g_assert_not_reached ();
2436 #if defined (TARGET_AMD64)
2439 amd64_emit_load_got_slot (MonoAotCompile *acfg, int dreg, int got_slot)
2442 g_assert (acfg->fp);
2443 emit_unset_mode (acfg);
2445 fprintf (acfg->fp, "mov %s+%d(%%rip), %s\n", acfg->got_symbol, (unsigned int) ((got_slot * sizeof (gpointer))), mono_arch_regname (dreg));
2452 * arch_emit_gsharedvt_arg_trampoline:
2454 * Emit code for a gsharedvt arg trampoline. OFFSET is the offset of the first of
2455 * two GOT slots which contain the argument, and the code to jump to.
2456 * TRAMP_SIZE is set to the size of the emitted trampoline.
2457 * These kinds of trampolines cannot be enumerated statically, since there could
2458 * be one trampoline per method instantiation, so we emit the same code for all
2459 * trampolines, and parameterize them using two GOT slots.
2462 arch_emit_gsharedvt_arg_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
2464 #if defined(TARGET_X86)
2468 /* Similar to the PPC code above */
2470 g_assert (MONO_ARCH_RGCTX_REG != X86_ECX);
2473 /* Load mscorlib got address */
2474 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
2476 x86_mov_reg_membase (code, X86_EAX, X86_ECX, offset * sizeof (gpointer), 4);
2477 /* Branch to the target address */
2478 x86_jump_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
2480 emit_bytes (acfg, buf, code - buf);
2483 g_assert (code - buf == *tramp_size);
2484 #elif defined(TARGET_ARM)
2488 /* The same as mono_arch_get_gsharedvt_arg_trampoline (), but for AOT */
2489 /* Similar to arch_emit_specific_trampoline () */
2492 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3));
2493 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8);
2494 /* Load the arg value from the GOT */
2495 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R1);
2496 /* Load the addr from the GOT */
2497 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
2499 ARM_BX (code, ARMREG_R1);
2501 g_assert (code - buf == 20);
2504 emit_bytes (acfg, buf, code - buf);
2505 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + 4);
2506 #elif defined(TARGET_ARM64)
2507 arm64_emit_gsharedvt_arg_trampoline (acfg, offset, tramp_size);
2508 #elif defined (TARGET_AMD64)
2510 amd64_emit_load_got_slot (acfg, AMD64_RAX, offset);
2511 amd64_emit_load_got_slot (acfg, MONO_ARCH_IMT_SCRATCH_REG, offset + 1);
2512 g_assert (AMD64_R11 == MONO_ARCH_IMT_SCRATCH_REG);
2513 fprintf (acfg->fp, "jmp *%%r11\n");
2517 g_assert_not_reached ();
2521 /* END OF ARCH SPECIFIC CODE */
2524 mono_get_field_token (MonoClassField *field)
2526 MonoClass *klass = field->parent;
2529 int fcount = mono_class_get_field_count (klass);
2530 for (i = 0; i < fcount; ++i) {
2531 if (field == &klass->fields [i])
2532 return MONO_TOKEN_FIELD_DEF | (mono_class_get_first_field_idx (klass) + 1 + i);
2535 g_assert_not_reached ();
2540 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
2544 //printf ("ENCODE: %d 0x%x.\n", value, value);
2547 * Same encoding as the one used in the metadata, extended to handle values
2548 * greater than 0x1fffffff.
2550 if ((value >= 0) && (value <= 127))
2552 else if ((value >= 0) && (value <= 16383)) {
2553 p [0] = 0x80 | (value >> 8);
2554 p [1] = value & 0xff;
2556 } else if ((value >= 0) && (value <= 0x1fffffff)) {
2557 p [0] = (value >> 24) | 0xc0;
2558 p [1] = (value >> 16) & 0xff;
2559 p [2] = (value >> 8) & 0xff;
2560 p [3] = value & 0xff;
2565 p [1] = (value >> 24) & 0xff;
2566 p [2] = (value >> 16) & 0xff;
2567 p [3] = (value >> 8) & 0xff;
2568 p [4] = value & 0xff;
2576 stream_init (MonoDynamicStream *sh)
2579 sh->alloc_size = 4096;
2580 sh->data = (char *)g_malloc (4096);
2582 /* So offsets are > 0 */
2588 make_room_in_stream (MonoDynamicStream *stream, int size)
2590 if (size <= stream->alloc_size)
2593 while (stream->alloc_size <= size) {
2594 if (stream->alloc_size < 4096)
2595 stream->alloc_size = 4096;
2597 stream->alloc_size *= 2;
2600 stream->data = (char *)g_realloc (stream->data, stream->alloc_size);
2604 add_stream_data (MonoDynamicStream *stream, const char *data, guint32 len)
2608 make_room_in_stream (stream, stream->index + len);
2609 memcpy (stream->data + stream->index, data, len);
2610 idx = stream->index;
2611 stream->index += len;
2618 * Add data to the binary blob inside the aot image. Returns the offset inside the
2619 * blob where the data was stored.
2622 add_to_blob (MonoAotCompile *acfg, const guint8 *data, guint32 data_len)
2624 g_assert (!acfg->blob_closed);
2626 if (acfg->blob.alloc_size == 0)
2627 stream_init (&acfg->blob);
2629 return add_stream_data (&acfg->blob, (char*)data, data_len);
2633 add_to_blob_aligned (MonoAotCompile *acfg, const guint8 *data, guint32 data_len, guint32 align)
2638 if (acfg->blob.alloc_size == 0)
2639 stream_init (&acfg->blob);
2641 count = acfg->blob.index % align;
2643 /* we assume the stream data will be aligned */
2645 add_stream_data (&acfg->blob, buf, 4 - count);
2647 return add_stream_data (&acfg->blob, (char*)data, data_len);
2650 /* Emit a table of data into the aot image */
2652 emit_aot_data (MonoAotCompile *acfg, MonoAotFileTable table, const char *symbol, guint8 *data, int size)
2654 if (acfg->data_outfile) {
2655 acfg->table_offsets [(int)table] = acfg->datafile_offset;
2656 fwrite (data,1, size, acfg->data_outfile);
2657 acfg->datafile_offset += size;
2658 // align the data to 8 bytes. Put zeros in the file (so that every build results in consistent output).
2659 int align = 8 - size % 8;
2660 acfg->datafile_offset += align;
2661 guint8 align_buf [16];
2662 memset (&align_buf, 0, sizeof (align_buf));
2663 fwrite (align_buf, align, 1, acfg->data_outfile);
2664 } else if (acfg->llvm) {
2665 mono_llvm_emit_aot_data (symbol, data, size);
2667 emit_section_change (acfg, RODATA_SECT, 0);
2668 emit_alignment (acfg, 8);
2669 emit_label (acfg, symbol);
2670 emit_bytes (acfg, data, size);
2675 * emit_offset_table:
2677 * Emit a table of increasing offsets in a compact form using differential encoding.
2678 * There is an index entry for each GROUP_SIZE number of entries. The greater the
2679 * group size, the more compact the table becomes, but the slower it becomes to compute
2680 * a given entry. Returns the size of the table.
2683 emit_offset_table (MonoAotCompile *acfg, const char *symbol, MonoAotFileTable table, int noffsets, int group_size, gint32 *offsets)
2685 gint32 current_offset;
2686 int i, buf_size, ngroups, index_entry_size;
2688 guint8 *data_p, *data_buf;
2689 guint32 *index_offsets;
2691 ngroups = (noffsets + (group_size - 1)) / group_size;
2693 index_offsets = g_new0 (guint32, ngroups);
2695 buf_size = noffsets * 4;
2696 p = buf = (guint8 *)g_malloc0 (buf_size);
2699 for (i = 0; i < noffsets; ++i) {
2700 //printf ("D: %d -> %d\n", i, offsets [i]);
2701 if ((i % group_size) == 0) {
2702 index_offsets [i / group_size] = p - buf;
2703 /* Emit the full value for these entries */
2704 encode_value (offsets [i], p, &p);
2706 /* The offsets are allowed to be non-increasing */
2707 //g_assert (offsets [i] >= current_offset);
2708 encode_value (offsets [i] - current_offset, p, &p);
2710 current_offset = offsets [i];
2715 if (ngroups && index_offsets [ngroups - 1] < 65000)
2716 index_entry_size = 2;
2718 index_entry_size = 4;
2720 buf_size = (data_p - data_buf) + (ngroups * 4) + 16;
2721 p = buf = (guint8 *)g_malloc0 (buf_size);
2723 /* Emit the header */
2724 encode_int (noffsets, p, &p);
2725 encode_int (group_size, p, &p);
2726 encode_int (ngroups, p, &p);
2727 encode_int (index_entry_size, p, &p);
2729 /* Emit the index */
2730 for (i = 0; i < ngroups; ++i) {
2731 if (index_entry_size == 2)
2732 encode_int16 (index_offsets [i], p, &p);
2734 encode_int (index_offsets [i], p, &p);
2737 memcpy (p, data_buf, data_p - data_buf);
2738 p += data_p - data_buf;
2740 g_assert (p - buf <= buf_size);
2742 emit_aot_data (acfg, table, symbol, buf, p - buf);
2747 return (int)(p - buf);
2751 get_image_index (MonoAotCompile *cfg, MonoImage *image)
2755 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
2759 index = g_hash_table_size (cfg->image_hash);
2760 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
2761 g_ptr_array_add (cfg->image_table, image);
2767 find_typespec_for_class (MonoAotCompile *acfg, MonoClass *klass)
2770 int len = acfg->image->tables [MONO_TABLE_TYPESPEC].rows;
2772 /* FIXME: Search referenced images as well */
2773 if (!acfg->typespec_classes) {
2774 acfg->typespec_classes = g_hash_table_new (NULL, NULL);
2775 for (i = 0; i < len; i++) {
2777 int typespec = MONO_TOKEN_TYPE_SPEC | (i + 1);
2778 MonoClass *klass_key = mono_class_get_and_inflate_typespec_checked (acfg->image, typespec, NULL, &error);
2779 if (!is_ok (&error)) {
2780 mono_error_cleanup (&error);
2783 g_hash_table_insert (acfg->typespec_classes, klass_key, GINT_TO_POINTER (typespec));
2786 return GPOINTER_TO_INT (g_hash_table_lookup (acfg->typespec_classes, klass));
2790 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf);
2793 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf);
2796 encode_ginst (MonoAotCompile *acfg, MonoGenericInst *inst, guint8 *buf, guint8 **endbuf);
2799 encode_type (MonoAotCompile *acfg, MonoType *t, guint8 *buf, guint8 **endbuf);
2802 encode_klass_ref_inner (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
2807 * The encoding begins with one of the MONO_AOT_TYPEREF values, followed by additional
2811 if (mono_class_is_ginst (klass)) {
2813 g_assert (klass->type_token);
2815 /* Find a typespec for a class if possible */
2816 token = find_typespec_for_class (acfg, klass);
2818 encode_value (MONO_AOT_TYPEREF_TYPESPEC_TOKEN, p, &p);
2819 encode_value (token, p, &p);
2821 MonoClass *gclass = mono_class_get_generic_class (klass)->container_class;
2822 MonoGenericInst *inst = mono_class_get_generic_class (klass)->context.class_inst;
2823 static int count = 0;
2826 encode_value (MONO_AOT_TYPEREF_GINST, p, &p);
2827 encode_klass_ref (acfg, gclass, p, &p);
2828 encode_ginst (acfg, inst, p, &p);
2832 } else if (klass->type_token) {
2833 int iindex = get_image_index (acfg, klass->image);
2835 g_assert (mono_metadata_token_code (klass->type_token) == MONO_TOKEN_TYPE_DEF);
2837 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX, p, &p);
2838 encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, p, &p);
2840 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE, p, &p);
2841 encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, p, &p);
2842 encode_value (get_image_index (acfg, klass->image), p, &p);
2844 } else if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR)) {
2845 MonoGenericContainer *container = mono_type_get_generic_param_owner (&klass->byval_arg);
2846 MonoGenericParam *par = klass->byval_arg.data.generic_param;
2848 encode_value (MONO_AOT_TYPEREF_VAR, p, &p);
2850 encode_value (par->gshared_constraint ? 1 : 0, p, &p);
2851 if (par->gshared_constraint) {
2852 MonoGSharedGenericParam *gpar = (MonoGSharedGenericParam*)par;
2853 encode_type (acfg, par->gshared_constraint, p, &p);
2854 encode_klass_ref (acfg, mono_class_from_generic_parameter (gpar->parent, NULL, klass->byval_arg.type == MONO_TYPE_MVAR), p, &p);
2856 encode_value (klass->byval_arg.type, p, &p);
2857 encode_value (mono_type_get_generic_param_num (&klass->byval_arg), p, &p);
2859 encode_value (container->is_anonymous ? 0 : 1, p, &p);
2861 if (!container->is_anonymous) {
2862 encode_value (container->is_method, p, &p);
2863 if (container->is_method)
2864 encode_method_ref (acfg, container->owner.method, p, &p);
2866 encode_klass_ref (acfg, container->owner.klass, p, &p);
2869 } else if (klass->byval_arg.type == MONO_TYPE_PTR) {
2870 encode_value (MONO_AOT_TYPEREF_PTR, p, &p);
2871 encode_type (acfg, &klass->byval_arg, p, &p);
2874 g_assert (klass->rank > 0);
2875 encode_value (MONO_AOT_TYPEREF_ARRAY, p, &p);
2876 encode_value (klass->rank, p, &p);
2877 encode_klass_ref (acfg, klass->element_class, p, &p);
2885 * Encode a reference to KLASS. We use our home-grown encoding instead of the
2886 * standard metadata encoding.
2889 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
2891 gboolean shared = FALSE;
2894 * The encoding of generic instances is large so emit them only once.
2896 if (mono_class_is_ginst (klass)) {
2898 g_assert (klass->type_token);
2900 /* Find a typespec for a class if possible */
2901 token = find_typespec_for_class (acfg, klass);
2904 } else if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR)) {
2909 guint offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->klass_blob_hash, klass));
2913 buf2 = (guint8 *)g_malloc (1024);
2916 encode_klass_ref_inner (acfg, klass, p, &p);
2917 g_assert (p - buf2 < 1024);
2919 offset = add_to_blob (acfg, buf2, p - buf2);
2922 g_hash_table_insert (acfg->klass_blob_hash, klass, GUINT_TO_POINTER (offset + 1));
2928 encode_value (MONO_AOT_TYPEREF_BLOB_INDEX, p, &p);
2929 encode_value (offset, p, &p);
2934 encode_klass_ref_inner (acfg, klass, buf, endbuf);
2938 encode_field_info (MonoAotCompile *cfg, MonoClassField *field, guint8 *buf, guint8 **endbuf)
2940 guint32 token = mono_get_field_token (field);
2943 encode_klass_ref (cfg, field->parent, p, &p);
2944 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
2945 encode_value (token - MONO_TOKEN_FIELD_DEF, p, &p);
2950 encode_ginst (MonoAotCompile *acfg, MonoGenericInst *inst, guint8 *buf, guint8 **endbuf)
2955 encode_value (inst->type_argc, p, &p);
2956 for (i = 0; i < inst->type_argc; ++i)
2957 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
2962 encode_generic_context (MonoAotCompile *acfg, MonoGenericContext *context, guint8 *buf, guint8 **endbuf)
2965 MonoGenericInst *inst;
2967 inst = context->class_inst;
2969 g_assert (inst->type_argc);
2970 encode_ginst (acfg, inst, p, &p);
2972 encode_value (0, p, &p);
2974 inst = context->method_inst;
2976 g_assert (inst->type_argc);
2977 encode_ginst (acfg, inst, p, &p);
2979 encode_value (0, p, &p);
2985 encode_type (MonoAotCompile *acfg, MonoType *t, guint8 *buf, guint8 **endbuf)
2989 g_assert (t->num_mods == 0);
2990 /* t->attrs can be ignored */
2991 //g_assert (t->attrs == 0);
2994 *p = MONO_TYPE_PINNED;
2998 *p = MONO_TYPE_BYREF;
3006 case MONO_TYPE_VOID:
3007 case MONO_TYPE_BOOLEAN:
3008 case MONO_TYPE_CHAR:
3021 case MONO_TYPE_STRING:
3022 case MONO_TYPE_OBJECT:
3023 case MONO_TYPE_TYPEDBYREF:
3025 case MONO_TYPE_VALUETYPE:
3026 case MONO_TYPE_CLASS:
3027 encode_klass_ref (acfg, mono_class_from_mono_type (t), p, &p);
3029 case MONO_TYPE_SZARRAY:
3030 encode_klass_ref (acfg, t->data.klass, p, &p);
3033 encode_type (acfg, t->data.type, p, &p);
3035 case MONO_TYPE_GENERICINST: {
3036 MonoClass *gclass = t->data.generic_class->container_class;
3037 MonoGenericInst *inst = t->data.generic_class->context.class_inst;
3039 encode_klass_ref (acfg, gclass, p, &p);
3040 encode_ginst (acfg, inst, p, &p);
3043 case MONO_TYPE_ARRAY: {
3044 MonoArrayType *array = t->data.array;
3047 encode_klass_ref (acfg, array->eklass, p, &p);
3048 encode_value (array->rank, p, &p);
3049 encode_value (array->numsizes, p, &p);
3050 for (i = 0; i < array->numsizes; ++i)
3051 encode_value (array->sizes [i], p, &p);
3052 encode_value (array->numlobounds, p, &p);
3053 for (i = 0; i < array->numlobounds; ++i)
3054 encode_value (array->lobounds [i], p, &p);
3058 case MONO_TYPE_MVAR:
3059 encode_klass_ref (acfg, mono_class_from_mono_type (t), p, &p);
3062 g_assert_not_reached ();
3069 encode_signature (MonoAotCompile *acfg, MonoMethodSignature *sig, guint8 *buf, guint8 **endbuf)
3075 /* Similar to the metadata encoding */
3076 if (sig->generic_param_count)
3080 if (sig->explicit_this)
3082 flags |= (sig->call_convention & 0x0F);
3086 if (sig->generic_param_count)
3087 encode_value (sig->generic_param_count, p, &p);
3088 encode_value (sig->param_count, p, &p);
3090 encode_type (acfg, sig->ret, p, &p);
3091 for (i = 0; i < sig->param_count; ++i) {
3092 if (sig->sentinelpos == i) {
3093 *p = MONO_TYPE_SENTINEL;
3096 encode_type (acfg, sig->params [i], p, &p);
3102 #define MAX_IMAGE_INDEX 250
3105 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf)
3107 guint32 image_index = get_image_index (acfg, method->klass->image);
3108 guint32 token = method->token;
3109 MonoJumpInfoToken *ji;
3113 * The encoding for most methods is as follows:
3114 * - image index encoded as a leb128
3115 * - token index encoded as a leb128
3116 * Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
3117 * types of method encodings.
3120 /* Mark methods which can't use aot trampolines because they need the further
3121 * processing in mono_magic_trampoline () which requires a MonoMethod*.
3123 if ((method->is_generic && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) ||
3124 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
3125 encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE << 24), p, &p);
3127 if (method->wrapper_type) {
3128 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
3130 encode_value ((MONO_AOT_METHODREF_WRAPPER << 24), p, &p);
3132 encode_value (method->wrapper_type, p, &p);
3134 switch (method->wrapper_type) {
3135 case MONO_WRAPPER_REMOTING_INVOKE:
3136 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
3137 case MONO_WRAPPER_XDOMAIN_INVOKE: {
3140 m = mono_marshal_method_from_wrapper (method);
3142 encode_method_ref (acfg, m, p, &p);
3145 case MONO_WRAPPER_PROXY_ISINST:
3146 case MONO_WRAPPER_LDFLD:
3147 case MONO_WRAPPER_LDFLDA:
3148 case MONO_WRAPPER_STFLD: {
3150 encode_klass_ref (acfg, info->d.proxy.klass, p, &p);
3153 case MONO_WRAPPER_ALLOC: {
3154 /* The GC name is saved once in MonoAotFileInfo */
3155 g_assert (info->d.alloc.alloc_type != -1);
3156 encode_value (info->d.alloc.alloc_type, p, &p);
3159 case MONO_WRAPPER_WRITE_BARRIER: {
3163 case MONO_WRAPPER_STELEMREF: {
3165 encode_value (info->subtype, p, &p);
3166 if (info->subtype == WRAPPER_SUBTYPE_VIRTUAL_STELEMREF)
3167 encode_value (info->d.virtual_stelemref.kind, p, &p);
3170 case MONO_WRAPPER_UNKNOWN: {
3172 encode_value (info->subtype, p, &p);
3173 if (info->subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE ||
3174 info->subtype == WRAPPER_SUBTYPE_STRUCTURE_TO_PTR)
3175 encode_klass_ref (acfg, method->klass, p, &p);
3176 else if (info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER)
3177 encode_method_ref (acfg, info->d.synchronized_inner.method, p, &p);
3178 else if (info->subtype == WRAPPER_SUBTYPE_ARRAY_ACCESSOR)
3179 encode_method_ref (acfg, info->d.array_accessor.method, p, &p);
3180 else if (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG)
3181 encode_signature (acfg, info->d.gsharedvt.sig, p, &p);
3182 else if (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG)
3183 encode_signature (acfg, info->d.gsharedvt.sig, p, &p);
3186 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
3188 encode_value (info->subtype, p, &p);
3189 if (info->subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER) {
3190 strcpy ((char*)p, method->name);
3191 p += strlen (method->name) + 1;
3192 } else if (info->subtype == WRAPPER_SUBTYPE_NATIVE_FUNC_AOT) {
3193 encode_method_ref (acfg, info->d.managed_to_native.method, p, &p);
3195 g_assert (info->subtype == WRAPPER_SUBTYPE_NONE || info->subtype == WRAPPER_SUBTYPE_PINVOKE);
3196 encode_method_ref (acfg, info->d.managed_to_native.method, p, &p);
3200 case MONO_WRAPPER_SYNCHRONIZED: {
3203 m = mono_marshal_method_from_wrapper (method);
3205 g_assert (m != method);
3206 encode_method_ref (acfg, m, p, &p);
3209 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
3211 encode_value (info->subtype, p, &p);
3213 if (info->subtype == WRAPPER_SUBTYPE_ELEMENT_ADDR) {
3214 encode_value (info->d.element_addr.rank, p, &p);
3215 encode_value (info->d.element_addr.elem_size, p, &p);
3216 } else if (info->subtype == WRAPPER_SUBTYPE_STRING_CTOR) {
3217 encode_method_ref (acfg, info->d.string_ctor.method, p, &p);
3219 g_assert_not_reached ();
3223 case MONO_WRAPPER_CASTCLASS: {
3225 encode_value (info->subtype, p, &p);
3228 case MONO_WRAPPER_RUNTIME_INVOKE: {
3230 encode_value (info->subtype, p, &p);
3231 if (info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT || info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL)
3232 encode_method_ref (acfg, info->d.runtime_invoke.method, p, &p);
3233 else if (info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL)
3234 encode_signature (acfg, info->d.runtime_invoke.sig, p, &p);
3237 case MONO_WRAPPER_DELEGATE_INVOKE:
3238 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
3239 case MONO_WRAPPER_DELEGATE_END_INVOKE: {
3240 if (method->is_inflated) {
3241 /* These wrappers are identified by their class */
3242 encode_value (1, p, &p);
3243 encode_klass_ref (acfg, method->klass, p, &p);
3245 MonoMethodSignature *sig = mono_method_signature (method);
3246 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
3248 encode_value (0, p, &p);
3249 if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE)
3250 encode_value (info ? info->subtype : 0, p, &p);
3251 encode_signature (acfg, sig, p, &p);
3255 case MONO_WRAPPER_NATIVE_TO_MANAGED: {
3257 encode_method_ref (acfg, info->d.native_to_managed.method, p, &p);
3258 encode_klass_ref (acfg, info->d.native_to_managed.klass, p, &p);
3262 g_assert_not_reached ();
3264 } else if (mono_method_signature (method)->is_inflated) {
3266 * This is a generic method, find the original token which referenced it and
3268 * Obtain the token from information recorded by the JIT.
3270 ji = (MonoJumpInfoToken *)g_hash_table_lookup (acfg->token_info_hash, method);
3272 image_index = get_image_index (acfg, ji->image);
3273 g_assert (image_index < MAX_IMAGE_INDEX);
3276 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
3277 encode_value (image_index, p, &p);
3278 encode_value (token, p, &p);
3280 MonoMethod *declaring;
3281 MonoGenericContext *context = mono_method_get_context (method);
3283 g_assert (method->is_inflated);
3284 declaring = ((MonoMethodInflated*)method)->declaring;
3287 * This might be a non-generic method of a generic instance, which
3288 * doesn't have a token since the reference is generated by the JIT
3289 * like Nullable:Box/Unbox, or by generic sharing.
3291 encode_value ((MONO_AOT_METHODREF_GINST << 24), p, &p);
3292 /* Encode the klass */
3293 encode_klass_ref (acfg, method->klass, p, &p);
3294 /* Encode the method */
3295 image_index = get_image_index (acfg, method->klass->image);
3296 g_assert (image_index < MAX_IMAGE_INDEX);
3297 g_assert (declaring->token);
3298 token = declaring->token;
3299 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
3300 encode_value (image_index, p, &p);
3301 encode_value (token, p, &p);
3302 encode_generic_context (acfg, context, p, &p);
3304 } else if (token == 0) {
3305 /* This might be a method of a constructed type like int[,].Set */
3306 /* Obtain the token from information recorded by the JIT */
3307 ji = (MonoJumpInfoToken *)g_hash_table_lookup (acfg->token_info_hash, method);
3309 image_index = get_image_index (acfg, ji->image);
3310 g_assert (image_index < MAX_IMAGE_INDEX);
3313 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
3314 encode_value (image_index, p, &p);
3315 encode_value (token, p, &p);
3318 g_assert (method->klass->rank);
3320 /* Encode directly */
3321 encode_value ((MONO_AOT_METHODREF_ARRAY << 24), p, &p);
3322 encode_klass_ref (acfg, method->klass, p, &p);
3323 if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank)
3324 encode_value (0, p, &p);
3325 else if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank * 2)
3326 encode_value (1, p, &p);
3327 else if (!strcmp (method->name, "Get"))
3328 encode_value (2, p, &p);
3329 else if (!strcmp (method->name, "Address"))
3330 encode_value (3, p, &p);
3331 else if (!strcmp (method->name, "Set"))
3332 encode_value (4, p, &p);
3334 g_assert_not_reached ();
3337 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
3339 if (image_index >= MONO_AOT_METHODREF_MIN) {
3340 encode_value ((MONO_AOT_METHODREF_LARGE_IMAGE_INDEX << 24), p, &p);
3341 encode_value (image_index, p, &p);
3342 encode_value (mono_metadata_token_index (token), p, &p);
3344 encode_value ((image_index << 24) | mono_metadata_token_index (token), p, &p);
3351 compare_patches (gconstpointer a, gconstpointer b)
3355 i = (*(MonoJumpInfo**)a)->ip.i;
3356 j = (*(MonoJumpInfo**)b)->ip.i;
3367 static G_GNUC_UNUSED char*
3368 patch_to_string (MonoJumpInfo *patch_info)
3372 str = g_string_new ("");
3374 g_string_append_printf (str, "%s(", get_patch_name (patch_info->type));
3376 switch (patch_info->type) {
3377 case MONO_PATCH_INFO_VTABLE:
3378 mono_type_get_desc (str, &patch_info->data.klass->byval_arg, TRUE);
3383 g_string_append_printf (str, ")");
3384 return g_string_free (str, FALSE);
3390 * Return whenever PATCH_INFO refers to a direct call, and thus requires a
3393 static inline gboolean
3394 is_plt_patch (MonoJumpInfo *patch_info)
3396 switch (patch_info->type) {
3397 case MONO_PATCH_INFO_METHOD:
3398 case MONO_PATCH_INFO_INTERNAL_METHOD:
3399 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
3400 case MONO_PATCH_INFO_ICALL_ADDR_CALL:
3401 case MONO_PATCH_INFO_RGCTX_FETCH:
3411 * Return the symbol identifying the plt entry PLT_OFFSET.
3414 get_plt_symbol (MonoAotCompile *acfg, int plt_offset, MonoJumpInfo *patch_info)
3418 * The Apple linker reorganizes object files, so it doesn't like branches to local
3419 * labels, since those have no relocations.
3421 return g_strdup_printf ("%sp_%d", acfg->llvm_label_prefix, plt_offset);
3423 return g_strdup_printf ("%sp_%d", acfg->temp_prefix, plt_offset);
3430 * Return a PLT entry which belongs to the method identified by PATCH_INFO.
3432 static MonoPltEntry*
3433 get_plt_entry (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
3436 gboolean synchronized = FALSE;
3437 static int synchronized_symbol_idx;
3439 if (!is_plt_patch (patch_info))
3442 if (!acfg->patch_to_plt_entry [patch_info->type])
3443 acfg->patch_to_plt_entry [patch_info->type] = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
3444 res = (MonoPltEntry *)g_hash_table_lookup (acfg->patch_to_plt_entry [patch_info->type], patch_info);
3446 if (!acfg->llvm && patch_info->type == MONO_PATCH_INFO_METHOD && (patch_info->data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
3448 * Allocate a separate PLT slot for each such patch, since some plt
3449 * entries will refer to the method itself, and some will refer to the
3453 synchronized = TRUE;
3457 MonoJumpInfo *new_ji;
3459 new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
3461 res = (MonoPltEntry *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoPltEntry));
3462 res->plt_offset = acfg->plt_offset;
3464 res->symbol = get_plt_symbol (acfg, res->plt_offset, patch_info);
3465 if (acfg->aot_opts.write_symbols)
3466 res->debug_sym = get_plt_entry_debug_sym (acfg, res->ji, acfg->plt_entry_debug_sym_cache);
3468 /* Avoid duplicate symbols because we don't cache */
3469 res->symbol = g_strdup_printf ("%s_%d", res->symbol, synchronized_symbol_idx);
3471 res->debug_sym = g_strdup_printf ("%s_%d", res->debug_sym, synchronized_symbol_idx);
3472 synchronized_symbol_idx ++;
3475 res->llvm_symbol = g_strdup_printf ("%s_%s_llvm", res->symbol, res->debug_sym);
3477 res->llvm_symbol = g_strdup_printf ("%s_llvm", res->symbol);
3479 g_hash_table_insert (acfg->patch_to_plt_entry [new_ji->type], new_ji, res);
3481 g_hash_table_insert (acfg->plt_offset_to_entry, GUINT_TO_POINTER (res->plt_offset), res);
3483 //g_assert (mono_patch_info_equal (patch_info, new_ji));
3484 //mono_print_ji (patch_info); printf ("\n");
3485 //g_hash_table_print_stats (acfg->patch_to_plt_entry);
3487 acfg->plt_offset ++;
3496 * Returns the offset of the GOT slot where the runtime object resulting from resolving
3497 * JI could be found if it exists, otherwise allocates a new one.
3500 get_got_offset (MonoAotCompile *acfg, gboolean llvm, MonoJumpInfo *ji)
3503 GotInfo *info = llvm ? &acfg->llvm_got_info : &acfg->got_info;
3505 got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (info->patch_to_got_offset_by_type [ji->type], ji));
3507 return got_offset - 1;
3510 got_offset = acfg->llvm_got_offset;
3511 acfg->llvm_got_offset ++;
3513 got_offset = acfg->got_offset;
3514 acfg->got_offset ++;
3517 acfg->stats.got_slots ++;
3518 acfg->stats.got_slot_types [ji->type] ++;
3520 g_hash_table_insert (info->patch_to_got_offset, ji, GUINT_TO_POINTER (got_offset + 1));
3521 g_hash_table_insert (info->patch_to_got_offset_by_type [ji->type], ji, GUINT_TO_POINTER (got_offset + 1));
3522 g_ptr_array_add (info->got_patches, ji);
3527 /* Add a method to the list of methods which need to be emitted */
3529 add_method_with_index (MonoAotCompile *acfg, MonoMethod *method, int index, gboolean extra)
3532 if (!g_hash_table_lookup (acfg->method_indexes, method)) {
3533 g_ptr_array_add (acfg->methods, method);
3534 g_hash_table_insert (acfg->method_indexes, method, GUINT_TO_POINTER (index + 1));
3535 acfg->nmethods = acfg->methods->len + 1;
3538 if (method->wrapper_type || extra)
3539 g_ptr_array_add (acfg->extra_methods, method);
3543 prefer_gsharedvt_method (MonoAotCompile *acfg, MonoMethod *method)
3545 /* One instantiation with valuetypes is generated for each async method */
3546 if (method->klass->image == mono_defaults.corlib && (!strcmp (method->klass->name, "AsyncMethodBuilderCore") || !strcmp (method->klass->name, "AsyncVoidMethodBuilder")))
3553 get_method_index (MonoAotCompile *acfg, MonoMethod *method)
3555 int index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
3563 add_method_full (MonoAotCompile *acfg, MonoMethod *method, gboolean extra, int depth)
3567 index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
3571 index = acfg->method_index;
3572 add_method_with_index (acfg, method, index, extra);
3574 g_ptr_array_add (acfg->method_order, GUINT_TO_POINTER (index));
3576 g_hash_table_insert (acfg->method_depth, method, GUINT_TO_POINTER (depth));
3578 acfg->method_index ++;
3584 add_method (MonoAotCompile *acfg, MonoMethod *method)
3586 return add_method_full (acfg, method, FALSE, 0);
3590 add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth)
3592 if (mono_method_is_generic_sharable_full (method, TRUE, TRUE, FALSE))
3593 method = mini_get_shared_method (method);
3594 else if ((acfg->opts & MONO_OPT_GSHAREDVT) && prefer_gsharedvt_method (acfg, method) && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE))
3595 /* Use the gsharedvt version */
3596 method = mini_get_shared_method_full (method, TRUE, TRUE);
3598 if (acfg->aot_opts.log_generics)
3599 aot_printf (acfg, "%*sAdding method %s.\n", depth, "", mono_method_get_full_name (method));
3601 add_method_full (acfg, method, TRUE, depth);
3605 add_extra_method (MonoAotCompile *acfg, MonoMethod *method)
3607 add_extra_method_with_depth (acfg, method, 0);
3611 add_jit_icall_wrapper (gpointer key, gpointer value, gpointer user_data)
3613 MonoAotCompile *acfg = (MonoAotCompile *)user_data;
3614 MonoJitICallInfo *callinfo = (MonoJitICallInfo *)value;
3615 MonoMethod *wrapper;
3621 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
3622 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, TRUE);
3625 add_method (acfg, wrapper);
3629 get_runtime_invoke_sig (MonoMethodSignature *sig)
3631 MonoMethodBuilder *mb;
3634 mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
3635 m = mono_mb_create_method (mb, sig, 16);
3636 MonoMethod *invoke = mono_marshal_get_runtime_invoke (m, FALSE);
3642 get_runtime_invoke (MonoAotCompile *acfg, MonoMethod *method, gboolean virtual_)
3644 return mono_marshal_get_runtime_invoke (method, virtual_);
3648 can_marshal_struct (MonoClass *klass)
3650 MonoClassField *field;
3651 gboolean can_marshal = TRUE;
3652 gpointer iter = NULL;
3653 MonoMarshalType *info;
3656 if (mono_class_is_auto_layout (klass))
3659 info = mono_marshal_load_type_info (klass);
3661 /* Only allow a few field types to avoid asserts in the marshalling code */
3662 while ((field = mono_class_get_fields (klass, &iter))) {
3663 if ((field->type->attrs & FIELD_ATTRIBUTE_STATIC))
3666 switch (field->type->type) {
3671 case MONO_TYPE_BOOLEAN:
3674 case MONO_TYPE_CHAR:
3682 case MONO_TYPE_STRING:
3684 case MONO_TYPE_VALUETYPE:
3685 if (!mono_class_from_mono_type (field->type)->enumtype && !can_marshal_struct (mono_class_from_mono_type (field->type)))
3686 can_marshal = FALSE;
3688 case MONO_TYPE_SZARRAY: {
3689 gboolean has_mspec = FALSE;
3692 for (i = 0; i < info->num_fields; ++i) {
3693 if (info->fields [i].field == field && info->fields [i].mspec)
3698 can_marshal = FALSE;
3702 can_marshal = FALSE;
3708 /* Its hard to compute whenever these can be marshalled or not */
3709 if (!strcmp (klass->name_space, "System.Net.NetworkInformation.MacOsStructs") && strcmp (klass->name, "sockaddr_dl"))
3716 create_gsharedvt_inst (MonoAotCompile *acfg, MonoMethod *method, MonoGenericContext *ctx)
3718 /* Create a vtype instantiation */
3719 MonoGenericContext shared_context;
3721 MonoGenericInst *inst;
3722 MonoGenericContainer *container;
3723 MonoClass **constraints;
3726 memset (ctx, 0, sizeof (MonoGenericContext));
3728 if (mono_class_is_gtd (method->klass)) {
3729 shared_context = mono_class_get_generic_container (method->klass)->context;
3730 inst = shared_context.class_inst;
3732 args = g_new0 (MonoType*, inst->type_argc);
3733 for (i = 0; i < inst->type_argc; ++i) {
3734 args [i] = &mono_defaults.int_class->byval_arg;
3736 ctx->class_inst = mono_metadata_get_generic_inst (inst->type_argc, args);
3738 if (method->is_generic) {
3739 container = mono_method_get_generic_container (method);
3740 shared_context = container->context;
3741 inst = shared_context.method_inst;
3743 args = g_new0 (MonoType*, inst->type_argc);
3744 for (i = 0; i < container->type_argc; ++i) {
3745 MonoGenericParamInfo *info = &container->type_params [i].info;
3746 gboolean ref_only = FALSE;
3748 if (info && info->constraints) {
3749 constraints = info->constraints;
3751 while (*constraints) {
3752 MonoClass *cklass = *constraints;
3753 if (!(cklass == mono_defaults.object_class || (cklass->image == mono_defaults.corlib && !strcmp (cklass->name, "ValueType"))))
3754 /* Inflaring the method with our vtype would not be valid */
3761 args [i] = &mono_defaults.object_class->byval_arg;
3763 args [i] = &mono_defaults.int_class->byval_arg;
3765 ctx->method_inst = mono_metadata_get_generic_inst (inst->type_argc, args);
3770 add_wrappers (MonoAotCompile *acfg)
3772 MonoMethod *method, *m;
3774 MonoMethodSignature *sig, *csig;
3778 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
3779 * so there is only one wrapper of a given type, or inlining their contents into their
3782 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3785 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3786 gboolean skip = FALSE;
3788 method = mono_get_method_checked (acfg->image, token, NULL, NULL, &error);
3789 report_loader_error (acfg, &error, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (&error));
3791 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3792 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3793 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
3796 /* Skip methods which can not be handled by get_runtime_invoke () */
3797 sig = mono_method_signature (method);
3800 if ((sig->ret->type == MONO_TYPE_PTR) ||
3801 (sig->ret->type == MONO_TYPE_TYPEDBYREF))
3803 if (mono_class_is_open_constructed_type (sig->ret))
3806 for (j = 0; j < sig->param_count; j++) {
3807 if (sig->params [j]->type == MONO_TYPE_TYPEDBYREF)
3809 if (mono_class_is_open_constructed_type (sig->params [j]))
3813 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
3814 if (!mono_class_is_contextbound (method->klass)) {
3815 MonoDynCallInfo *info = mono_arch_dyn_call_prepare (sig);
3816 gboolean has_nullable = FALSE;
3818 for (j = 0; j < sig->param_count; j++) {
3819 if (sig->params [j]->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (sig->params [j])))
3820 has_nullable = TRUE;
3823 if (info && !has_nullable && !acfg->aot_opts.llvm_only) {
3824 /* Supported by the dynamic runtime-invoke wrapper */
3828 mono_arch_dyn_call_free (info);
3832 if (acfg->aot_opts.llvm_only)
3833 /* Supported by the gsharedvt based runtime-invoke wrapper */
3837 //printf ("%s\n", mono_method_full_name (method, TRUE));
3838 add_method (acfg, get_runtime_invoke (acfg, method, FALSE));
3842 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
3845 /* Runtime invoke wrappers */
3847 /* void runtime-invoke () [.cctor] */
3848 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
3849 csig->ret = &mono_defaults.void_class->byval_arg;
3850 add_method (acfg, get_runtime_invoke_sig (csig));
3852 /* void runtime-invoke () [Finalize] */
3853 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
3855 csig->ret = &mono_defaults.void_class->byval_arg;
3856 add_method (acfg, get_runtime_invoke_sig (csig));
3858 /* void runtime-invoke (string) [exception ctor] */
3859 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
3861 csig->ret = &mono_defaults.void_class->byval_arg;
3862 csig->params [0] = &mono_defaults.string_class->byval_arg;
3863 add_method (acfg, get_runtime_invoke_sig (csig));
3865 /* void runtime-invoke (string, string) [exception ctor] */
3866 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
3868 csig->ret = &mono_defaults.void_class->byval_arg;
3869 csig->params [0] = &mono_defaults.string_class->byval_arg;
3870 csig->params [1] = &mono_defaults.string_class->byval_arg;
3871 add_method (acfg, get_runtime_invoke_sig (csig));
3873 /* string runtime-invoke () [Exception.ToString ()] */
3874 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
3876 csig->ret = &mono_defaults.string_class->byval_arg;
3877 add_method (acfg, get_runtime_invoke_sig (csig));
3879 /* void runtime-invoke (string, Exception) [exception ctor] */
3880 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
3882 csig->ret = &mono_defaults.void_class->byval_arg;
3883 csig->params [0] = &mono_defaults.string_class->byval_arg;
3884 csig->params [1] = &mono_defaults.exception_class->byval_arg;
3885 add_method (acfg, get_runtime_invoke_sig (csig));
3887 /* Assembly runtime-invoke (string, Assembly, bool) [DoAssemblyResolve] */
3888 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
3890 csig->ret = &(mono_class_load_from_name (
3891 mono_defaults.corlib, "System.Reflection", "Assembly"))->byval_arg;
3892 csig->params [0] = &mono_defaults.string_class->byval_arg;
3893 csig->params [1] = &(mono_class_load_from_name (mono_defaults.corlib, "System.Reflection", "Assembly"))->byval_arg;
3894 csig->params [2] = &mono_defaults.boolean_class->byval_arg;
3895 add_method (acfg, get_runtime_invoke_sig (csig));
3897 /* runtime-invoke used by finalizers */
3898 add_method (acfg, get_runtime_invoke (acfg, mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE));
3900 /* This is used by mono_runtime_capture_context () */
3901 method = mono_get_context_capture_method ();
3903 add_method (acfg, get_runtime_invoke (acfg, method, FALSE));
3905 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
3906 if (!acfg->aot_opts.llvm_only)
3907 add_method (acfg, mono_marshal_get_runtime_invoke_dynamic ());
3910 /* These are used by mono_jit_runtime_invoke () to calls gsharedvt out wrappers */
3911 if (acfg->aot_opts.llvm_only) {
3914 /* Create simplified signatures which match the signature used by the gsharedvt out wrappers */
3915 for (variants = 0; variants < 4; ++variants) {
3916 for (i = 0; i < 16; ++i) {
3917 sig = mini_get_gsharedvt_out_sig_wrapper_signature ((variants & 1) > 0, (variants & 2) > 0, i);
3918 add_extra_method (acfg, mono_marshal_get_runtime_invoke_for_sig (sig));
3926 add_method (acfg, mono_marshal_get_stelemref ());
3928 /* Managed Allocators */
3929 nallocators = mono_gc_get_managed_allocator_types ();
3930 for (i = 0; i < nallocators; ++i) {
3931 if ((m = mono_gc_get_managed_allocator_by_type (i, MANAGED_ALLOCATOR_REGULAR)))
3932 add_method (acfg, m);
3933 if ((m = mono_gc_get_managed_allocator_by_type (i, MANAGED_ALLOCATOR_SLOW_PATH)))
3934 add_method (acfg, m);
3937 /* write barriers */
3938 if (mono_gc_is_moving ()) {
3939 add_method (acfg, mono_gc_get_specific_write_barrier (FALSE));
3940 add_method (acfg, mono_gc_get_specific_write_barrier (TRUE));
3943 /* Stelemref wrappers */
3945 MonoMethod **wrappers;
3948 wrappers = mono_marshal_get_virtual_stelemref_wrappers (&nwrappers);
3949 for (i = 0; i < nwrappers; ++i)
3950 add_method (acfg, wrappers [i]);
3954 /* castclass_with_check wrapper */
3955 add_method (acfg, mono_marshal_get_castclass_with_cache ());
3956 /* isinst_with_check wrapper */
3957 add_method (acfg, mono_marshal_get_isinst_with_cache ());
3959 /* JIT icall wrappers */
3960 /* FIXME: locking - this is "safe" as full-AOT threads don't mutate the icall hash*/
3961 g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
3965 * remoting-invoke-with-check wrappers are very frequent, so avoid emitting them,
3966 * we use the original method instead at runtime.
3967 * Since full-aot doesn't support remoting, this is not a problem.
3970 /* remoting-invoke wrappers */
3971 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3973 MonoMethodSignature *sig;
3975 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3976 method = mono_get_method_checked (acfg->image, token, NULL, NULL, &error);
3977 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
3979 sig = mono_method_signature (method);
3981 if (sig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
3982 m = mono_marshal_get_remoting_invoke_with_check (method);
3984 add_method (acfg, m);
3989 /* delegate-invoke wrappers */
3990 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3993 MonoCustomAttrInfo *cattr;
3995 token = MONO_TOKEN_TYPE_DEF | (i + 1);
3996 klass = mono_class_get_checked (acfg->image, token, &error);
3999 mono_error_cleanup (&error);
4003 if (!klass->delegate || klass == mono_defaults.delegate_class || klass == mono_defaults.multicastdelegate_class)
4006 if (!mono_class_is_gtd (klass)) {
4007 method = mono_get_delegate_invoke (klass);
4009 m = mono_marshal_get_delegate_invoke (method, NULL);
4011 add_method (acfg, m);
4013 method = mono_class_get_method_from_name_flags (klass, "BeginInvoke", -1, 0);
4015 add_method (acfg, mono_marshal_get_delegate_begin_invoke (method));
4017 method = mono_class_get_method_from_name_flags (klass, "EndInvoke", -1, 0);
4019 add_method (acfg, mono_marshal_get_delegate_end_invoke (method));
4021 cattr = mono_custom_attrs_from_class_checked (klass, &error);
4022 if (!is_ok (&error)) {
4023 mono_error_cleanup (&error);
4030 for (j = 0; j < cattr->num_attrs; ++j)
4031 if (cattr->attrs [j].ctor && (!strcmp (cattr->attrs [j].ctor->klass->name, "MonoNativeFunctionWrapperAttribute") || !strcmp (cattr->attrs [j].ctor->klass->name, "UnmanagedFunctionPointerAttribute")))
4033 if (j < cattr->num_attrs) {
4035 MonoMethod *wrapper;
4036 MonoMethod *del_invoke;
4038 /* Add wrappers needed by mono_ftnptr_to_delegate () */
4039 invoke = mono_get_delegate_invoke (klass);
4040 wrapper = mono_marshal_get_native_func_wrapper_aot (klass);
4041 del_invoke = mono_marshal_get_delegate_invoke_internal (invoke, FALSE, TRUE, wrapper);
4042 add_method (acfg, wrapper);
4043 add_method (acfg, del_invoke);
4046 } else if ((acfg->opts & MONO_OPT_GSHAREDVT) && mono_class_is_gtd (klass)) {
4048 MonoGenericContext ctx;
4049 MonoMethod *inst, *gshared;
4052 * Emit gsharedvt versions of the generic delegate-invoke wrappers
4055 method = mono_get_delegate_invoke (klass);
4056 create_gsharedvt_inst (acfg, method, &ctx);
4058 inst = mono_class_inflate_generic_method_checked (method, &ctx, &error);
4059 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4061 m = mono_marshal_get_delegate_invoke (inst, NULL);
4062 g_assert (m->is_inflated);
4064 gshared = mini_get_shared_method_full (m, FALSE, TRUE);
4065 add_extra_method (acfg, gshared);
4068 method = mono_get_delegate_begin_invoke (klass);
4069 create_gsharedvt_inst (acfg, method, &ctx);
4071 inst = mono_class_inflate_generic_method_checked (method, &ctx, &error);
4072 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4074 m = mono_marshal_get_delegate_begin_invoke (inst);
4075 g_assert (m->is_inflated);
4077 gshared = mini_get_shared_method_full (m, FALSE, TRUE);
4078 add_extra_method (acfg, gshared);
4081 method = mono_get_delegate_end_invoke (klass);
4082 create_gsharedvt_inst (acfg, method, &ctx);
4084 inst = mono_class_inflate_generic_method_checked (method, &ctx, &error);
4085 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4087 m = mono_marshal_get_delegate_end_invoke (inst);
4088 g_assert (m->is_inflated);
4090 gshared = mini_get_shared_method_full (m, FALSE, TRUE);
4091 add_extra_method (acfg, gshared);
4096 /* array access wrappers */
4097 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
4101 token = MONO_TOKEN_TYPE_SPEC | (i + 1);
4102 klass = mono_class_get_checked (acfg->image, token, &error);
4105 mono_error_cleanup (&error);
4109 if (klass->rank && MONO_TYPE_IS_PRIMITIVE (&klass->element_class->byval_arg)) {
4110 MonoMethod *m, *wrapper;
4112 /* Add runtime-invoke wrappers too */
4114 m = mono_class_get_method_from_name (klass, "Get", -1);
4116 wrapper = mono_marshal_get_array_accessor_wrapper (m);
4117 add_extra_method (acfg, wrapper);
4118 if (!acfg->aot_opts.llvm_only)
4119 add_extra_method (acfg, get_runtime_invoke (acfg, wrapper, FALSE));
4121 m = mono_class_get_method_from_name (klass, "Set", -1);
4123 wrapper = mono_marshal_get_array_accessor_wrapper (m);
4124 add_extra_method (acfg, wrapper);
4125 if (!acfg->aot_opts.llvm_only)
4126 add_extra_method (acfg, get_runtime_invoke (acfg, wrapper, FALSE));
4130 /* Synchronized wrappers */
4131 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
4133 token = MONO_TOKEN_METHOD_DEF | (i + 1);
4134 method = mono_get_method_checked (acfg->image, token, NULL, NULL, &error);
4135 report_loader_error (acfg, &error, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (&error));
4137 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
4138 if (method->is_generic) {
4140 } else if ((acfg->opts & MONO_OPT_GSHAREDVT) && mono_class_is_gtd (method->klass)) {
4142 MonoGenericContext ctx;
4143 MonoMethod *inst, *gshared, *m;
4146 * Create a generic wrapper for a generic instance, and AOT that.
4148 create_gsharedvt_inst (acfg, method, &ctx);
4149 inst = mono_class_inflate_generic_method_checked (method, &ctx, &error);
4150 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4151 m = mono_marshal_get_synchronized_wrapper (inst);
4152 g_assert (m->is_inflated);
4153 gshared = mini_get_shared_method_full (m, FALSE, TRUE);
4154 add_method (acfg, gshared);
4156 add_method (acfg, mono_marshal_get_synchronized_wrapper (method));
4161 /* pinvoke wrappers */
4162 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
4165 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
4167 method = mono_get_method_checked (acfg->image, token, NULL, NULL, &error);
4168 report_loader_error (acfg, &error, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (&error));
4170 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
4171 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
4172 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
4175 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
4176 if (acfg->aot_opts.llvm_only) {
4177 /* The wrappers have a different signature (hasthis is not set) so need to add this too */
4178 add_gsharedvt_wrappers (acfg, mono_method_signature (method), FALSE, TRUE);
4183 /* native-to-managed wrappers */
4184 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
4187 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
4188 MonoCustomAttrInfo *cattr;
4191 method = mono_get_method_checked (acfg->image, token, NULL, NULL, &error);
4192 report_loader_error (acfg, &error, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (&error));
4195 * Only generate native-to-managed wrappers for methods which have an
4196 * attribute named MonoPInvokeCallbackAttribute. We search for the attribute by
4197 * name to avoid defining a new assembly to contain it.
4199 cattr = mono_custom_attrs_from_method_checked (method, &error);
4200 if (!is_ok (&error)) {
4201 char *name = mono_method_get_full_name (method);
4202 report_loader_error (acfg, &error, "Failed to load custom attributes from method %s due to %s\n", name, mono_error_get_message (&error));
4207 for (j = 0; j < cattr->num_attrs; ++j)
4208 if (cattr->attrs [j].ctor && !strcmp (cattr->attrs [j].ctor->klass->name, "MonoPInvokeCallbackAttribute"))
4210 if (j < cattr->num_attrs) {
4211 MonoCustomAttrEntry *e = &cattr->attrs [j];
4212 MonoMethodSignature *sig = mono_method_signature (e->ctor);
4213 const char *p = (const char*)e->data;
4215 int slen, num_named, named_type;
4219 char *export_name = NULL;
4220 MonoMethod *wrapper;
4222 /* this cannot be enforced by the C# compiler so we must give the user some warning before aborting */
4223 if (!(method->flags & METHOD_ATTRIBUTE_STATIC)) {
4224 g_warning ("AOT restriction: Method '%s' must be static since it is decorated with [MonoPInvokeCallback]. See http://ios.xamarin.com/Documentation/Limitations#Reverse_Callbacks",
4225 mono_method_full_name (method, TRUE));
4229 g_assert (sig->param_count == 1);
4230 g_assert (sig->params [0]->type == MONO_TYPE_CLASS && !strcmp (mono_class_from_mono_type (sig->params [0])->name, "Type"));
4233 * Decode the cattr manually since we can't create objects
4234 * during aot compilation.
4240 /* From load_cattr_value () in reflection.c */
4241 slen = mono_metadata_decode_value (p, &p);
4242 n = (char *)g_memdup (p, slen + 1);
4244 t = mono_reflection_type_from_name_checked (n, acfg->image, &error);
4246 mono_error_assert_ok (&error);
4249 klass = mono_class_from_mono_type (t);
4250 g_assert (klass->parent == mono_defaults.multicastdelegate_class);
4254 num_named = read16 (p);
4257 g_assert (num_named < 2);
4258 if (num_named == 1) {
4262 /* parse ExportSymbol attribute */
4264 named_type = *named;
4266 /* data_type = *named; */
4269 name_len = mono_metadata_decode_blob_size (named, &named);
4270 name = (char *)g_malloc (name_len + 1);
4271 memcpy (name, named, name_len);
4272 name [name_len] = 0;
4275 g_assert (named_type == 0x54);
4276 g_assert (!strcmp (name, "ExportSymbol"));
4278 /* load_cattr_value (), string case */
4279 g_assert (*named != (char)0xff);
4280 slen = mono_metadata_decode_value (named, &named);
4281 export_name = (char *)g_malloc (slen + 1);
4282 memcpy (export_name, named, slen);
4283 export_name [slen] = 0;
4287 wrapper = mono_marshal_get_managed_wrapper (method, klass, 0);
4288 add_method (acfg, wrapper);
4290 g_hash_table_insert (acfg->export_names, wrapper, export_name);
4295 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
4296 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
4297 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
4301 /* StructureToPtr/PtrToStructure wrappers */
4302 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
4306 token = MONO_TOKEN_TYPE_DEF | (i + 1);
4307 klass = mono_class_get_checked (acfg->image, token, &error);
4310 mono_error_cleanup (&error);
4314 if (klass->valuetype && !mono_class_is_gtd (klass) && can_marshal_struct (klass) &&
4315 !(klass->nested_in && strstr (klass->nested_in->name, "<PrivateImplementationDetails>") == klass->nested_in->name)) {
4316 add_method (acfg, mono_marshal_get_struct_to_ptr (klass));
4317 add_method (acfg, mono_marshal_get_ptr_to_struct (klass));
4323 has_type_vars (MonoClass *klass)
4325 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
4328 return has_type_vars (klass->element_class);
4329 if (mono_class_is_ginst (klass)) {
4330 MonoGenericContext *context = &mono_class_get_generic_class (klass)->context;
4331 if (context->class_inst) {
4334 for (i = 0; i < context->class_inst->type_argc; ++i)
4335 if (has_type_vars (mono_class_from_mono_type (context->class_inst->type_argv [i])))
4339 if (mono_class_is_gtd (klass))
4345 is_vt_inst (MonoGenericInst *inst)
4349 for (i = 0; i < inst->type_argc; ++i) {
4350 MonoType *t = inst->type_argv [i];
4351 if (MONO_TYPE_ISSTRUCT (t) || t->type == MONO_TYPE_VALUETYPE)
4358 method_has_type_vars (MonoMethod *method)
4360 if (has_type_vars (method->klass))
4363 if (method->is_inflated) {
4364 MonoGenericContext *context = mono_method_get_context (method);
4365 if (context->method_inst) {
4368 for (i = 0; i < context->method_inst->type_argc; ++i)
4369 if (has_type_vars (mono_class_from_mono_type (context->method_inst->type_argv [i])))
4377 gboolean mono_aot_mode_is_full (MonoAotOptions *opts)
4379 return opts->mode == MONO_AOT_MODE_FULL;
4383 gboolean mono_aot_mode_is_hybrid (MonoAotOptions *opts)
4385 return opts->mode == MONO_AOT_MODE_HYBRID;
4388 static void add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth, const char *ref);
4391 add_generic_class (MonoAotCompile *acfg, MonoClass *klass, gboolean force, const char *ref)
4393 /* This might lead to a huge code blowup so only do it if neccesary */
4394 if (!mono_aot_mode_is_full (&acfg->aot_opts) && !mono_aot_mode_is_hybrid (&acfg->aot_opts) && !force)
4397 add_generic_class_with_depth (acfg, klass, 0, ref);
4401 check_type_depth (MonoType *t, int depth)
4409 case MONO_TYPE_GENERICINST: {
4410 MonoGenericClass *gklass = t->data.generic_class;
4411 MonoGenericInst *ginst = gklass->context.class_inst;
4414 for (i = 0; i < ginst->type_argc; ++i) {
4415 if (check_type_depth (ginst->type_argv [i], depth + 1))
4429 add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method);
4432 * add_generic_class:
4434 * Add all methods of a generic class.
4437 add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth, const char *ref)
4440 MonoClassField *field;
4442 gboolean use_gsharedvt = FALSE;
4444 if (!acfg->ginst_hash)
4445 acfg->ginst_hash = g_hash_table_new (NULL, NULL);
4447 mono_class_init (klass);
4449 if (mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->context.class_inst->is_open)
4452 if (has_type_vars (klass))
4455 if (!mono_class_is_ginst (klass) && !klass->rank)
4458 if (mono_class_has_failure (klass))
4461 if (!acfg->ginst_hash)
4462 acfg->ginst_hash = g_hash_table_new (NULL, NULL);
4464 if (g_hash_table_lookup (acfg->ginst_hash, klass))
4467 if (check_type_depth (&klass->byval_arg, 0))
4470 if (acfg->aot_opts.log_generics)
4471 aot_printf (acfg, "%*sAdding generic instance %s [%s].\n", depth, "", mono_type_full_name (&klass->byval_arg), ref);
4473 g_hash_table_insert (acfg->ginst_hash, klass, klass);
4476 * Use gsharedvt for generic collections with vtype arguments to avoid code blowup.
4477 * Enable this only for some classes since gsharedvt might not support all methods.
4479 if ((acfg->opts & MONO_OPT_GSHAREDVT) && klass->image == 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) &&
4480 (!strcmp (klass->name, "Dictionary`2") || !strcmp (klass->name, "List`1") || !strcmp (klass->name, "ReadOnlyCollection`1")))
4481 use_gsharedvt = TRUE;
4484 while ((method = mono_class_get_methods (klass, &iter))) {
4485 if ((acfg->opts & MONO_OPT_GSHAREDVT) && method->is_inflated && mono_method_get_context (method)->method_inst) {
4487 * This is partial sharing, and we can't handle it yet
4492 if (mono_method_is_generic_sharable_full (method, FALSE, FALSE, use_gsharedvt)) {
4494 add_types_from_method_header (acfg, method);
4498 if (method->is_generic)
4503 * FIXME: Instances which are referenced by these methods are not added,
4504 * for example Array.Resize<int> for List<int>.Add ().
4506 add_extra_method_with_depth (acfg, method, depth + 1);
4510 while ((field = mono_class_get_fields (klass, &iter))) {
4511 if (field->type->type == MONO_TYPE_GENERICINST)
4512 add_generic_class_with_depth (acfg, mono_class_from_mono_type (field->type), depth + 1, "field");
4515 if (klass->delegate) {
4516 method = mono_get_delegate_invoke (klass);
4518 method = mono_marshal_get_delegate_invoke (method, NULL);
4520 if (acfg->aot_opts.log_generics)
4521 aot_printf (acfg, "%*sAdding method %s.\n", depth, "", mono_method_get_full_name (method));
4523 add_method (acfg, method);
4526 /* Add superclasses */
4528 add_generic_class_with_depth (acfg, klass->parent, depth, "parent");
4531 * For ICollection<T>, add instances of the helper methods
4532 * in Array, since a T[] could be cast to ICollection<T>.
4534 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") &&
4535 (!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"))) {
4536 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4537 MonoClass *array_class = mono_bounded_array_class_get (tclass, 1, FALSE);
4541 if (!strcmp (klass->name, "IEnumerator`1"))
4542 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, "IEnumerable`1");
4544 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
4546 /* Add the T[]/InternalEnumerator class */
4547 if (!strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IEnumerator`1")) {
4552 while ((nclass = mono_class_get_nested_types (array_class->parent, &iter))) {
4553 if (!strcmp (nclass->name, "InternalEnumerator`1"))
4557 nclass = mono_class_inflate_generic_class_checked (nclass, mono_generic_class_get_context (mono_class_get_generic_class (klass)), &error);
4558 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
4559 add_generic_class (acfg, nclass, FALSE, "ICollection<T>");
4563 while ((method = mono_class_get_methods (array_class, &iter))) {
4564 if (strstr (method->name, name_prefix)) {
4565 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
4567 add_extra_method_with_depth (acfg, m, depth);
4571 g_free (name_prefix);
4574 /* Add an instance of GenericComparer<T> which is created dynamically by Comparer<T> */
4575 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "Comparer`1")) {
4577 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4578 MonoClass *icomparable, *gcomparer, *icomparable_inst;
4579 MonoGenericContext ctx;
4580 MonoType *args [16];
4582 memset (&ctx, 0, sizeof (ctx));
4584 icomparable = mono_class_load_from_name (mono_defaults.corlib, "System", "IComparable`1");
4586 args [0] = &tclass->byval_arg;
4587 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4589 icomparable_inst = mono_class_inflate_generic_class_checked (icomparable, &ctx, &error);
4590 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
4592 if (mono_class_is_assignable_from (icomparable_inst, tclass)) {
4593 MonoClass *gcomparer_inst;
4594 gcomparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericComparer`1");
4595 gcomparer_inst = mono_class_inflate_generic_class_checked (gcomparer, &ctx, &error);
4596 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
4598 add_generic_class (acfg, gcomparer_inst, FALSE, "Comparer<T>");
4602 /* Add an instance of GenericEqualityComparer<T> which is created dynamically by EqualityComparer<T> */
4603 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "EqualityComparer`1")) {
4605 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4606 MonoClass *iface, *gcomparer, *iface_inst;
4607 MonoGenericContext ctx;
4608 MonoType *args [16];
4610 memset (&ctx, 0, sizeof (ctx));
4612 iface = mono_class_load_from_name (mono_defaults.corlib, "System", "IEquatable`1");
4614 args [0] = &tclass->byval_arg;
4615 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4617 iface_inst = mono_class_inflate_generic_class_checked (iface, &ctx, &error);
4618 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
4620 if (mono_class_is_assignable_from (iface_inst, tclass)) {
4621 MonoClass *gcomparer_inst;
4624 gcomparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericEqualityComparer`1");
4625 gcomparer_inst = mono_class_inflate_generic_class_checked (gcomparer, &ctx, &error);
4626 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
4627 add_generic_class (acfg, gcomparer_inst, FALSE, "EqualityComparer<T>");
4631 /* Add an instance of EnumComparer<T> which is created dynamically by EqualityComparer<T> for enums */
4632 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "EqualityComparer`1")) {
4633 MonoClass *enum_comparer;
4634 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4635 MonoGenericContext ctx;
4636 MonoType *args [16];
4638 if (mono_class_is_enum (tclass)) {
4639 MonoClass *enum_comparer_inst;
4642 memset (&ctx, 0, sizeof (ctx));
4643 args [0] = &tclass->byval_arg;
4644 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4646 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "EnumEqualityComparer`1");
4647 enum_comparer_inst = mono_class_inflate_generic_class_checked (enum_comparer, &ctx, &error);
4648 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
4649 add_generic_class (acfg, enum_comparer_inst, FALSE, "EqualityComparer<T>");
4653 /* Add an instance of ObjectComparer<T> which is created dynamically by Comparer<T> for enums */
4654 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "Comparer`1")) {
4655 MonoClass *comparer;
4656 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4657 MonoGenericContext ctx;
4658 MonoType *args [16];
4660 if (mono_class_is_enum (tclass)) {
4661 MonoClass *comparer_inst;
4664 memset (&ctx, 0, sizeof (ctx));
4665 args [0] = &tclass->byval_arg;
4666 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4668 comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "ObjectComparer`1");
4669 comparer_inst = mono_class_inflate_generic_class_checked (comparer, &ctx, &error);
4670 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
4671 add_generic_class (acfg, comparer_inst, FALSE, "Comparer<T>");
4677 add_instances_of (MonoAotCompile *acfg, MonoClass *klass, MonoType **insts, int ninsts, gboolean force)
4680 MonoGenericContext ctx;
4681 MonoType *args [16];
4683 if (acfg->aot_opts.no_instances)
4686 memset (&ctx, 0, sizeof (ctx));
4688 for (i = 0; i < ninsts; ++i) {
4690 MonoClass *generic_inst;
4691 args [0] = insts [i];
4692 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4693 generic_inst = mono_class_inflate_generic_class_checked (klass, &ctx, &error);
4694 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
4695 add_generic_class (acfg, generic_inst, force, "");
4700 add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method)
4703 MonoMethodHeader *header;
4704 MonoMethodSignature *sig;
4707 depth = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_depth, method));
4709 sig = mono_method_signature (method);
4712 for (j = 0; j < sig->param_count; ++j)
4713 if (sig->params [j]->type == MONO_TYPE_GENERICINST)
4714 add_generic_class_with_depth (acfg, mono_class_from_mono_type (sig->params [j]), depth + 1, "arg");
4717 header = mono_method_get_header_checked (method, &error);
4720 for (j = 0; j < header->num_locals; ++j)
4721 if (header->locals [j]->type == MONO_TYPE_GENERICINST)
4722 add_generic_class_with_depth (acfg, mono_class_from_mono_type (header->locals [j]), depth + 1, "local");
4723 mono_metadata_free_mh (header);
4725 mono_error_cleanup (&error); /* FIXME report the error */
4731 * add_generic_instances:
4733 * Add instances referenced by the METHODSPEC/TYPESPEC table.
4736 add_generic_instances (MonoAotCompile *acfg)
4741 MonoGenericContext *context;
4743 if (acfg->aot_opts.no_instances)
4746 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHODSPEC].rows; ++i) {
4748 token = MONO_TOKEN_METHOD_SPEC | (i + 1);
4749 method = mono_get_method_checked (acfg->image, token, NULL, NULL, &error);
4752 aot_printerrf (acfg, "Failed to load methodspec 0x%x due to %s.\n", token, mono_error_get_message (&error));
4753 aot_printerrf (acfg, "Run with MONO_LOG_LEVEL=debug for more information.\n");
4754 mono_error_cleanup (&error);
4758 if (method->klass->image != acfg->image)
4761 context = mono_method_get_context (method);
4763 if (context && ((context->class_inst && context->class_inst->is_open)))
4767 * For open methods, create an instantiation which can be passed to the JIT.
4768 * FIXME: Handle class_inst as well.
4770 if (context && context->method_inst && context->method_inst->is_open) {
4772 MonoGenericContext shared_context;
4773 MonoGenericInst *inst;
4774 MonoType **type_argv;
4776 MonoMethod *declaring_method;
4777 gboolean supported = TRUE;
4779 /* Check that the context doesn't contain open constructed types */
4780 if (context->class_inst) {
4781 inst = context->class_inst;
4782 for (i = 0; i < inst->type_argc; ++i) {
4783 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)
4785 if (mono_class_is_open_constructed_type (inst->type_argv [i]))
4789 if (context->method_inst) {
4790 inst = context->method_inst;
4791 for (i = 0; i < inst->type_argc; ++i) {
4792 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)
4794 if (mono_class_is_open_constructed_type (inst->type_argv [i]))
4802 memset (&shared_context, 0, sizeof (MonoGenericContext));
4804 inst = context->class_inst;
4806 type_argv = g_new0 (MonoType*, inst->type_argc);
4807 for (i = 0; i < inst->type_argc; ++i) {
4808 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)
4809 type_argv [i] = &mono_defaults.object_class->byval_arg;
4811 type_argv [i] = inst->type_argv [i];
4814 shared_context.class_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
4818 inst = context->method_inst;
4820 type_argv = g_new0 (MonoType*, inst->type_argc);
4821 for (i = 0; i < inst->type_argc; ++i) {
4822 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)
4823 type_argv [i] = &mono_defaults.object_class->byval_arg;
4825 type_argv [i] = inst->type_argv [i];
4828 shared_context.method_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
4832 if (method->is_generic || mono_class_is_gtd (method->klass))
4833 declaring_method = method;
4835 declaring_method = mono_method_get_declaring_generic_method (method);
4837 method = mono_class_inflate_generic_method_checked (declaring_method, &shared_context, &error);
4838 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4842 * If the method is fully sharable, it was already added in place of its
4843 * generic definition.
4845 if (mono_method_is_generic_sharable_full (method, FALSE, FALSE, FALSE))
4849 * FIXME: Partially shared methods are not shared here, so we end up with
4850 * many identical methods.
4852 add_extra_method (acfg, method);
4855 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
4859 token = MONO_TOKEN_TYPE_SPEC | (i + 1);
4861 klass = mono_class_get_checked (acfg->image, token, &error);
4862 if (!klass || klass->rank) {
4863 mono_error_cleanup (&error);
4867 add_generic_class (acfg, klass, FALSE, "typespec");
4870 /* Add types of args/locals */
4871 for (i = 0; i < acfg->methods->len; ++i) {
4872 method = (MonoMethod *)g_ptr_array_index (acfg->methods, i);
4873 add_types_from_method_header (acfg, method);
4876 if (acfg->image == mono_defaults.corlib) {
4878 MonoType *insts [256];
4881 insts [ninsts ++] = &mono_defaults.byte_class->byval_arg;
4882 insts [ninsts ++] = &mono_defaults.sbyte_class->byval_arg;
4883 insts [ninsts ++] = &mono_defaults.int16_class->byval_arg;
4884 insts [ninsts ++] = &mono_defaults.uint16_class->byval_arg;
4885 insts [ninsts ++] = &mono_defaults.int32_class->byval_arg;
4886 insts [ninsts ++] = &mono_defaults.uint32_class->byval_arg;
4887 insts [ninsts ++] = &mono_defaults.int64_class->byval_arg;
4888 insts [ninsts ++] = &mono_defaults.uint64_class->byval_arg;
4889 insts [ninsts ++] = &mono_defaults.single_class->byval_arg;
4890 insts [ninsts ++] = &mono_defaults.double_class->byval_arg;
4891 insts [ninsts ++] = &mono_defaults.char_class->byval_arg;
4892 insts [ninsts ++] = &mono_defaults.boolean_class->byval_arg;
4894 /* Add GenericComparer<T> instances for primitive types for Enum.ToString () */
4895 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "GenericComparer`1");
4897 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4898 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "GenericEqualityComparer`1");
4900 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4902 /* Add instances of EnumEqualityComparer which are created by EqualityComparer<T> for enums */
4904 MonoClass *enum_comparer;
4905 MonoType *insts [16];
4909 insts [ninsts ++] = &mono_defaults.int32_class->byval_arg;
4910 insts [ninsts ++] = &mono_defaults.uint32_class->byval_arg;
4911 insts [ninsts ++] = &mono_defaults.uint16_class->byval_arg;
4912 insts [ninsts ++] = &mono_defaults.byte_class->byval_arg;
4913 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "EnumEqualityComparer`1");
4914 add_instances_of (acfg, enum_comparer, insts, ninsts, FALSE);
4917 insts [ninsts ++] = &mono_defaults.int16_class->byval_arg;
4918 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "ShortEnumEqualityComparer`1");
4919 add_instances_of (acfg, enum_comparer, insts, ninsts, FALSE);
4922 insts [ninsts ++] = &mono_defaults.sbyte_class->byval_arg;
4923 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "SByteEnumEqualityComparer`1");
4924 add_instances_of (acfg, enum_comparer, insts, ninsts, FALSE);
4926 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "LongEnumEqualityComparer`1");
4928 insts [ninsts ++] = &mono_defaults.int64_class->byval_arg;
4929 insts [ninsts ++] = &mono_defaults.uint64_class->byval_arg;
4930 add_instances_of (acfg, enum_comparer, insts, ninsts, FALSE);
4933 /* Add instances of the array generic interfaces for primitive types */
4934 /* This will add instances of the InternalArray_ helper methods in Array too */
4935 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "ICollection`1");
4937 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4939 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "IList`1");
4941 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4943 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "IEnumerable`1");
4945 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4948 * Add a managed-to-native wrapper of Array.GetGenericValueImpl<object>, which is
4949 * used for all instances of GetGenericValueImpl by the AOT runtime.
4952 MonoGenericContext ctx;
4953 MonoType *args [16];
4954 MonoMethod *get_method;
4955 MonoClass *array_klass = mono_array_class_get (mono_defaults.object_class, 1)->parent;
4957 get_method = mono_class_get_method_from_name (array_klass, "GetGenericValueImpl", 2);
4961 memset (&ctx, 0, sizeof (ctx));
4962 args [0] = &mono_defaults.object_class->byval_arg;
4963 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
4964 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (get_method, &ctx, &error), TRUE, TRUE));
4965 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4969 /* Same for CompareExchange<T>/Exchange<T> */
4971 MonoGenericContext ctx;
4972 MonoType *args [16];
4974 MonoClass *interlocked_klass = mono_class_load_from_name (mono_defaults.corlib, "System.Threading", "Interlocked");
4975 gpointer iter = NULL;
4977 while ((m = mono_class_get_methods (interlocked_klass, &iter))) {
4978 if ((!strcmp (m->name, "CompareExchange") || !strcmp (m->name, "Exchange")) && m->is_generic) {
4980 memset (&ctx, 0, sizeof (ctx));
4981 args [0] = &mono_defaults.object_class->byval_arg;
4982 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
4983 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m, &ctx, &error), TRUE, TRUE));
4984 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
4989 /* Same for Volatile.Read/Write<T> */
4991 MonoGenericContext ctx;
4992 MonoType *args [16];
4994 MonoClass *volatile_klass = mono_class_try_load_from_name (mono_defaults.corlib, "System.Threading", "Volatile");
4995 gpointer iter = NULL;
4997 if (volatile_klass) {
4998 while ((m = mono_class_get_methods (volatile_klass, &iter))) {
4999 if ((!strcmp (m->name, "Read") || !strcmp (m->name, "Write")) && m->is_generic) {
5001 memset (&ctx, 0, sizeof (ctx));
5002 args [0] = &mono_defaults.object_class->byval_arg;
5003 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
5004 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m, &ctx, &error), TRUE, TRUE));
5005 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
5011 /* object[] accessor wrappers. */
5012 for (i = 1; i < 4; ++i) {
5013 MonoClass *obj_array_class = mono_array_class_get (mono_defaults.object_class, i);
5016 m = mono_class_get_method_from_name (obj_array_class, "Get", i);
5019 m = mono_marshal_get_array_accessor_wrapper (m);
5020 add_extra_method (acfg, m);
5022 m = mono_class_get_method_from_name (obj_array_class, "Address", i);
5025 m = mono_marshal_get_array_accessor_wrapper (m);
5026 add_extra_method (acfg, m);
5028 m = mono_class_get_method_from_name (obj_array_class, "Set", i + 1);
5031 m = mono_marshal_get_array_accessor_wrapper (m);
5032 add_extra_method (acfg, m);
5038 * is_direct_callable:
5040 * Return whenever the method identified by JI is directly callable without
5041 * going through the PLT.
5044 is_direct_callable (MonoAotCompile *acfg, MonoMethod *method, MonoJumpInfo *patch_info)
5046 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
5047 MonoCompile *callee_cfg = (MonoCompile *)g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
5049 gboolean direct_callable = TRUE;
5051 if (direct_callable && !(!callee_cfg->has_got_slots && mono_class_is_before_field_init (callee_cfg->method->klass)))
5052 direct_callable = FALSE;
5053 if ((callee_cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) && (!method || method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED))
5054 // FIXME: Maybe call the wrapper directly ?
5055 direct_callable = FALSE;
5057 if (acfg->aot_opts.soft_debug || acfg->aot_opts.no_direct_calls) {
5058 /* Disable this so all calls go through load_method (), see the
5059 * mini_get_debug_options ()->load_aot_jit_info_eagerly = TRUE; line in
5060 * mono_debugger_agent_init ().
5062 direct_callable = FALSE;
5065 if (callee_cfg->method->wrapper_type == MONO_WRAPPER_ALLOC)
5066 /* sgen does some initialization when the allocator method is created */
5067 direct_callable = FALSE;
5068 if (callee_cfg->method->wrapper_type == MONO_WRAPPER_WRITE_BARRIER)
5069 /* we don't know at compile time whether sgen is concurrent or not */
5070 direct_callable = FALSE;
5072 if (direct_callable)
5075 } else if ((patch_info->type == MONO_PATCH_INFO_ICALL_ADDR_CALL && patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5076 if (acfg->aot_opts.direct_pinvoke)
5078 } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
5079 if (acfg->aot_opts.direct_icalls)
5087 #ifdef MONO_ARCH_AOT_SUPPORTED
5089 get_pinvoke_import (MonoAotCompile *acfg, MonoMethod *method)
5091 MonoImage *image = method->klass->image;
5092 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *) method;
5093 MonoTableInfo *tables = image->tables;
5094 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
5095 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
5096 guint32 im_cols [MONO_IMPLMAP_SIZE];
5099 import = (char *)g_hash_table_lookup (acfg->method_to_pinvoke_import, method);
5103 if (!piinfo->implmap_idx || piinfo->implmap_idx > im->rows)
5106 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
5108 if (!im_cols [MONO_IMPLMAP_SCOPE] || im_cols [MONO_IMPLMAP_SCOPE] > mr->rows)
5111 import = g_strdup_printf ("%s", mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]));
5113 g_hash_table_insert (acfg->method_to_pinvoke_import, method, import);
5119 get_pinvoke_import (MonoAotCompile *acfg, MonoMethod *method)
5126 compare_lne (MonoDebugLineNumberEntry *a, MonoDebugLineNumberEntry *b)
5128 if (a->native_offset == b->native_offset)
5129 return a->il_offset - b->il_offset;
5131 return a->native_offset - b->native_offset;
5135 * compute_line_numbers:
5137 * Returns a sparse array of size CODE_SIZE containing MonoDebugSourceLocation* entries for the native offsets which have a corresponding line number
5140 static MonoDebugSourceLocation**
5141 compute_line_numbers (MonoMethod *method, int code_size, MonoDebugMethodJitInfo *debug_info)
5143 MonoDebugMethodInfo *minfo;
5144 MonoDebugLineNumberEntry *ln_array;
5145 MonoDebugSourceLocation *loc;
5146 int i, prev_line, prev_il_offset;
5147 int *native_to_il_offset = NULL;
5148 MonoDebugSourceLocation **res;
5151 minfo = mono_debug_lookup_method (method);
5154 // FIXME: This seems to happen when two methods have the same cfg->method_to_register
5155 if (debug_info->code_size != code_size)
5158 g_assert (code_size);
5160 /* Compute the native->IL offset mapping */
5162 ln_array = g_new0 (MonoDebugLineNumberEntry, debug_info->num_line_numbers);
5163 memcpy (ln_array, debug_info->line_numbers, debug_info->num_line_numbers * sizeof (MonoDebugLineNumberEntry));
5165 qsort (ln_array, debug_info->num_line_numbers, sizeof (MonoDebugLineNumberEntry), (int (*)(const void *, const void *))compare_lne);
5167 native_to_il_offset = g_new0 (int, code_size + 1);
5169 for (i = 0; i < debug_info->num_line_numbers; ++i) {
5171 MonoDebugLineNumberEntry *lne = &ln_array [i];
5174 for (j = 0; j < lne->native_offset; ++j)
5175 native_to_il_offset [j] = -1;
5178 if (i < debug_info->num_line_numbers - 1) {
5179 MonoDebugLineNumberEntry *lne_next = &ln_array [i + 1];
5181 for (j = lne->native_offset; j < lne_next->native_offset; ++j)
5182 native_to_il_offset [j] = lne->il_offset;
5184 for (j = lne->native_offset; j < code_size; ++j)
5185 native_to_il_offset [j] = lne->il_offset;
5190 /* Compute the native->line number mapping */
5191 res = g_new0 (MonoDebugSourceLocation*, code_size);
5192 prev_il_offset = -1;
5195 for (i = 0; i < code_size; ++i) {
5196 int il_offset = native_to_il_offset [i];
5198 if (il_offset == -1 || il_offset == prev_il_offset)
5200 prev_il_offset = il_offset;
5201 loc = mono_debug_symfile_lookup_location (minfo, il_offset);
5202 if (!(loc && loc->source_file))
5204 if (loc->row == prev_line) {
5205 mono_debug_symfile_free_location (loc);
5208 prev_line = loc->row;
5209 //printf ("D: %s:%d il=%x native=%x\n", loc->source_file, loc->row, il_offset, i);
5211 /* This will cover the prolog too */
5221 get_file_index (MonoAotCompile *acfg, const char *source_file)
5225 // FIXME: Free these
5226 if (!acfg->dwarf_ln_filenames)
5227 acfg->dwarf_ln_filenames = g_hash_table_new (g_str_hash, g_str_equal);
5228 findex = GPOINTER_TO_INT (g_hash_table_lookup (acfg->dwarf_ln_filenames, source_file));
5230 findex = g_hash_table_size (acfg->dwarf_ln_filenames) + 1;
5231 g_hash_table_insert (acfg->dwarf_ln_filenames, g_strdup (source_file), GINT_TO_POINTER (findex));
5232 emit_unset_mode (acfg);
5233 fprintf (acfg->fp, ".file %d \"%s\"\n", findex, mono_dwarf_escape_path (source_file));
5245 * emit_and_reloc_code:
5247 * Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
5248 * is true, calls are made through the GOT too. This is used for emitting trampolines
5249 * in full-aot mode, since calls made from trampolines couldn't go through the PLT,
5250 * since trampolines are needed to make PTL work.
5253 emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, guint32 code_len, MonoJumpInfo *relocs, gboolean got_only, MonoDebugMethodJitInfo *debug_info)
5255 int i, pindex, start_index;
5257 MonoJumpInfo *patch_info;
5258 MonoDebugSourceLocation **locs = NULL;
5259 gboolean skip, prologue_end = FALSE;
5260 #ifdef MONO_ARCH_AOT_SUPPORTED
5261 gboolean direct_call, external_call;
5263 const char *direct_call_target = 0;
5264 const char *direct_pinvoke;
5267 if (acfg->gas_line_numbers && method && debug_info) {
5268 locs = compute_line_numbers (method, code_len, debug_info);
5270 int findex = get_file_index (acfg, "<unknown>");
5271 emit_unset_mode (acfg);
5272 fprintf (acfg->fp, ".loc %d %d 0\n", findex, 1);
5276 /* Collect and sort relocations */
5277 patches = g_ptr_array_new ();
5278 for (patch_info = relocs; patch_info; patch_info = patch_info->next)
5279 g_ptr_array_add (patches, patch_info);
5280 g_ptr_array_sort (patches, compare_patches);
5283 for (i = 0; i < code_len; i += INST_LEN) {
5285 for (pindex = start_index; pindex < patches->len; ++pindex) {
5286 patch_info = (MonoJumpInfo *)g_ptr_array_index (patches, pindex);
5287 if (patch_info->ip.i >= i)
5291 if (locs && locs [i]) {
5292 MonoDebugSourceLocation *loc = locs [i];
5294 const char *options;
5296 findex = get_file_index (acfg, loc->source_file);
5297 emit_unset_mode (acfg);
5299 options = " prologue_end";
5302 prologue_end = TRUE;
5303 fprintf (acfg->fp, ".loc %d %d 0%s\n", findex, loc->row, options);
5304 mono_debug_symfile_free_location (loc);
5308 #ifdef MONO_ARCH_AOT_SUPPORTED
5309 if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
5310 start_index = pindex;
5312 switch (patch_info->type) {
5313 case MONO_PATCH_INFO_NONE:
5315 case MONO_PATCH_INFO_GOT_OFFSET: {
5318 arch_emit_got_offset (acfg, code + i, &code_size);
5319 i += code_size - INST_LEN;
5321 patch_info->type = MONO_PATCH_INFO_NONE;
5324 case MONO_PATCH_INFO_OBJC_SELECTOR_REF: {
5325 int code_size, index;
5326 char *selector = (char *)patch_info->data.target;
5328 if (!acfg->objc_selector_to_index)
5329 acfg->objc_selector_to_index = g_hash_table_new (g_str_hash, g_str_equal);
5330 if (!acfg->objc_selectors)
5331 acfg->objc_selectors = g_ptr_array_new ();
5332 index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->objc_selector_to_index, selector));
5336 index = acfg->objc_selector_index;
5337 g_ptr_array_add (acfg->objc_selectors, (void*)patch_info->data.target);
5338 g_hash_table_insert (acfg->objc_selector_to_index, selector, GUINT_TO_POINTER (index + 1));
5339 acfg->objc_selector_index ++;
5342 arch_emit_objc_selector_ref (acfg, code + i, index, &code_size);
5343 i += code_size - INST_LEN;
5345 patch_info->type = MONO_PATCH_INFO_NONE;
5350 * If this patch is a call, try emitting a direct call instead of
5351 * through a PLT entry. This is possible if the called method is in
5352 * the same assembly and requires no initialization.
5354 direct_call = FALSE;
5355 external_call = FALSE;
5356 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
5357 if (!got_only && is_direct_callable (acfg, method, patch_info)) {
5358 MonoCompile *callee_cfg = (MonoCompile *)g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
5359 //printf ("DIRECT: %s %s\n", method ? mono_method_full_name (method, TRUE) : "", mono_method_full_name (callee_cfg->method, TRUE));
5361 direct_call_target = callee_cfg->asm_symbol;
5362 patch_info->type = MONO_PATCH_INFO_NONE;
5363 acfg->stats.direct_calls ++;
5366 acfg->stats.all_calls ++;
5367 } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
5368 if (!got_only && is_direct_callable (acfg, method, patch_info)) {
5369 if (!(patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
5370 direct_pinvoke = mono_lookup_icall_symbol (patch_info->data.method);
5372 direct_pinvoke = get_pinvoke_import (acfg, patch_info->data.method);
5373 if (direct_pinvoke) {
5375 g_assert (strlen (direct_pinvoke) < 1000);
5376 direct_call_target = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, direct_pinvoke);
5379 } else if (patch_info->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
5380 const char *sym = mono_lookup_jit_icall_symbol (patch_info->data.name);
5381 if (!got_only && sym && acfg->aot_opts.direct_icalls) {
5382 /* Call to a C function implementing a jit icall */
5384 external_call = TRUE;
5385 g_assert (strlen (sym) < 1000);
5386 direct_call_target = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, sym);
5388 } else if (patch_info->type == MONO_PATCH_INFO_INTERNAL_METHOD) {
5389 MonoJitICallInfo *info = mono_find_jit_icall_by_name (patch_info->data.name);
5390 const char *sym = mono_lookup_jit_icall_symbol (patch_info->data.name);
5391 if (!got_only && sym && acfg->aot_opts.direct_icalls && info->func == info->wrapper) {
5392 /* Call to a jit icall without a wrapper */
5394 external_call = TRUE;
5395 g_assert (strlen (sym) < 1000);
5396 direct_call_target = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, sym);
5401 patch_info->type = MONO_PATCH_INFO_NONE;
5402 acfg->stats.direct_calls ++;
5405 if (!got_only && !direct_call) {
5406 MonoPltEntry *plt_entry = get_plt_entry (acfg, patch_info);
5408 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
5410 direct_call_target = plt_entry->symbol;
5412 /* Nullify the patch */
5413 patch_info->type = MONO_PATCH_INFO_NONE;
5414 plt_entry->jit_used = TRUE;
5421 arch_emit_direct_call (acfg, direct_call_target, external_call, FALSE, patch_info, &call_size);
5422 i += call_size - INST_LEN;
5426 got_slot = get_got_offset (acfg, FALSE, patch_info);
5428 arch_emit_got_access (acfg, acfg->got_symbol, code + i, got_slot, &code_size);
5429 i += code_size - INST_LEN;
5435 #endif /* MONO_ARCH_AOT_SUPPORTED */
5438 /* Find next patch */
5440 for (pindex = start_index; pindex < patches->len; ++pindex) {
5441 patch_info = (MonoJumpInfo *)g_ptr_array_index (patches, pindex);
5442 if (patch_info->ip.i >= i)
5446 /* Try to emit multiple bytes at once */
5447 if (pindex < patches->len && patch_info->ip.i > i) {
5450 for (limit = i + INST_LEN; limit < patch_info->ip.i; limit += INST_LEN) {
5451 if (locs && locs [limit])
5455 emit_code_bytes (acfg, code + i, limit - i);
5456 i = limit - INST_LEN;
5458 emit_code_bytes (acfg, code + i, INST_LEN);
5463 g_ptr_array_free (patches, TRUE);
5470 * Return a modified version of S which only includes characters permissible in symbols.
5473 sanitize_symbol (MonoAotCompile *acfg, char *s)
5475 gboolean process = FALSE;
5484 for (i = 0; i < len; ++i)
5485 if (!(s [i] <= 0x7f && (isalnum (s [i]) || s [i] == '_')))
5490 gs = g_string_sized_new (len);
5491 for (i = 0; i < len; ++i) {
5493 if (c <= 0x7f && (isalnum (c) || c == '_')) {
5494 g_string_append_c (gs, c);
5495 } else if (c > 0x7f) {
5496 /* multi-byte utf8 */
5497 g_string_append_printf (gs, "_0x%x", c);
5500 while (c >> 6 == 0x2) {
5501 g_string_append_printf (gs, "%x", c);
5505 g_string_append_printf (gs, "_");
5508 g_string_append_c (gs, '_');
5512 res = mono_mempool_strdup (acfg->mempool, gs->str);
5513 g_string_free (gs, TRUE);
5518 get_debug_sym (MonoMethod *method, const char *prefix, GHashTable *cache)
5520 char *name1, *name2, *cached;
5521 int i, j, len, count;
5522 MonoMethod *cached_method;
5524 name1 = mono_method_full_name (method, TRUE);
5527 // This is so that we don't accidentally create a local symbol (which starts with 'L')
5528 if ((!prefix || !*prefix) && name1 [0] == 'L')
5532 #if defined(TARGET_WIN32) && defined(TARGET_X86)
5533 char adjustedPrefix [MAX_SYMBOL_SIZE];
5534 prefix = mangle_symbol (prefix, adjustedPrefix, G_N_ELEMENTS (adjustedPrefix));
5537 len = strlen (name1);
5538 name2 = (char *)malloc (strlen (prefix) + len + 16);
5539 memcpy (name2, prefix, strlen (prefix));
5540 j = strlen (prefix);
5541 for (i = 0; i < len; ++i) {
5542 if (i == 0 && name1 [0] >= '0' && name1 [0] <= '9') {
5544 } else if (isalnum (name1 [i])) {
5545 name2 [j ++] = name1 [i];
5546 } else if (name1 [i] == ' ' && name1 [i + 1] == '(' && name1 [i + 2] == ')') {
5548 } else if (name1 [i] == ',' && name1 [i + 1] == ' ') {
5551 } else if (name1 [i] == '(' || name1 [i] == ')' || name1 [i] == '>') {
5561 cached_method = (MonoMethod *)g_hash_table_lookup (cache, name2);
5562 if (!(cached_method && cached_method != method))
5564 sprintf (name2 + j, "_%d", count);
5568 cached = g_strdup (name2);
5569 g_hash_table_insert (cache, cached, method);
5575 emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
5580 char *debug_sym = NULL;
5581 char *symbol = NULL;
5582 int func_alignment = AOT_FUNC_ALIGNMENT;
5585 method = cfg->orig_method;
5586 code = cfg->native_code;
5588 method_index = get_method_index (acfg, method);
5589 symbol = g_strdup_printf ("%sme_%x", acfg->temp_prefix, method_index);
5591 /* Make the labels local */
5592 emit_section_change (acfg, ".text", 0);
5593 emit_alignment_code (acfg, func_alignment);
5595 if (acfg->global_symbols && acfg->need_no_dead_strip)
5596 fprintf (acfg->fp, " .no_dead_strip %s\n", cfg->asm_symbol);
5598 emit_label (acfg, cfg->asm_symbol);
5600 if (acfg->aot_opts.write_symbols && !acfg->global_symbols && !acfg->llvm) {
5602 * Write a C style symbol for every method, this has two uses:
5603 * - it works on platforms where the dwarf debugging info is not
5605 * - it allows the setting of breakpoints of aot-ed methods.
5607 debug_sym = get_debug_sym (method, "", acfg->method_label_hash);
5608 cfg->asm_debug_symbol = g_strdup (debug_sym);
5610 if (acfg->need_no_dead_strip)
5611 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
5612 emit_local_symbol (acfg, debug_sym, symbol, TRUE);
5613 emit_label (acfg, debug_sym);
5616 export_name = (char *)g_hash_table_lookup (acfg->export_names, method);
5618 /* Emit a global symbol for the method */
5619 emit_global_inner (acfg, export_name, TRUE);
5620 emit_label (acfg, export_name);
5623 if (cfg->verbose_level > 0)
5624 g_print ("Method %s emitted as %s\n", mono_method_get_full_name (method), cfg->asm_symbol);
5626 acfg->stats.code_size += cfg->code_len;
5628 acfg->cfgs [method_index]->got_offset = acfg->got_offset;
5630 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 ()));
5634 if (acfg->aot_opts.write_symbols) {
5636 emit_symbol_size (acfg, debug_sym, ".");
5638 emit_symbol_size (acfg, cfg->asm_symbol, ".");
5642 emit_label (acfg, symbol);
5649 * Encode PATCH_INFO into its disk representation.
5652 encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf)
5656 switch (patch_info->type) {
5657 case MONO_PATCH_INFO_NONE:
5659 case MONO_PATCH_INFO_IMAGE:
5660 encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
5662 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
5663 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
5664 case MONO_PATCH_INFO_GC_NURSERY_START:
5665 case MONO_PATCH_INFO_GC_NURSERY_BITS:
5667 case MONO_PATCH_INFO_CASTCLASS_CACHE:
5668 encode_value (patch_info->data.index, p, &p);
5670 case MONO_PATCH_INFO_METHOD_REL:
5671 encode_value ((gint)patch_info->data.offset, p, &p);
5673 case MONO_PATCH_INFO_SWITCH: {
5674 gpointer *table = (gpointer *)patch_info->data.table->table;
5677 encode_value (patch_info->data.table->table_size, p, &p);
5678 for (k = 0; k < patch_info->data.table->table_size; k++)
5679 encode_value ((int)(gssize)table [k], p, &p);
5682 case MONO_PATCH_INFO_METHODCONST:
5683 case MONO_PATCH_INFO_METHOD:
5684 case MONO_PATCH_INFO_METHOD_JUMP:
5685 case MONO_PATCH_INFO_ICALL_ADDR:
5686 case MONO_PATCH_INFO_ICALL_ADDR_CALL:
5687 case MONO_PATCH_INFO_METHOD_RGCTX:
5688 case MONO_PATCH_INFO_METHOD_CODE_SLOT:
5689 encode_method_ref (acfg, patch_info->data.method, p, &p);
5691 case MONO_PATCH_INFO_AOT_JIT_INFO:
5692 case MONO_PATCH_INFO_GET_TLS_TRAMP:
5693 case MONO_PATCH_INFO_SET_TLS_TRAMP:
5694 encode_value (patch_info->data.index, p, &p);
5696 case MONO_PATCH_INFO_INTERNAL_METHOD:
5697 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
5698 guint32 len = strlen (patch_info->data.name);
5700 encode_value (len, p, &p);
5702 memcpy (p, patch_info->data.name, len);
5707 case MONO_PATCH_INFO_LDSTR: {
5708 guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
5709 guint32 token = patch_info->data.token->token;
5710 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
5711 encode_value (image_index, p, &p);
5712 encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
5715 case MONO_PATCH_INFO_RVA:
5716 case MONO_PATCH_INFO_DECLSEC:
5717 case MONO_PATCH_INFO_LDTOKEN:
5718 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
5719 encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
5720 encode_value (patch_info->data.token->token, p, &p);
5721 encode_value (patch_info->data.token->has_context, p, &p);
5722 if (patch_info->data.token->has_context)
5723 encode_generic_context (acfg, &patch_info->data.token->context, p, &p);
5725 case MONO_PATCH_INFO_EXC_NAME: {
5726 MonoClass *ex_class;
5729 mono_class_load_from_name (mono_defaults.exception_class->image,
5730 "System", (const char *)patch_info->data.target);
5731 encode_klass_ref (acfg, ex_class, p, &p);
5734 case MONO_PATCH_INFO_R4:
5735 encode_value (*((guint32 *)patch_info->data.target), p, &p);
5737 case MONO_PATCH_INFO_R8:
5738 encode_value (((guint32 *)patch_info->data.target) [MINI_LS_WORD_IDX], p, &p);
5739 encode_value (((guint32 *)patch_info->data.target) [MINI_MS_WORD_IDX], p, &p);
5741 case MONO_PATCH_INFO_VTABLE:
5742 case MONO_PATCH_INFO_CLASS:
5743 case MONO_PATCH_INFO_IID:
5744 case MONO_PATCH_INFO_ADJUSTED_IID:
5745 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
5747 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
5748 encode_klass_ref (acfg, patch_info->data.del_tramp->klass, p, &p);
5749 if (patch_info->data.del_tramp->method) {
5750 encode_value (1, p, &p);
5751 encode_method_ref (acfg, patch_info->data.del_tramp->method, p, &p);
5753 encode_value (0, p, &p);
5755 encode_value (patch_info->data.del_tramp->is_virtual, p, &p);
5757 case MONO_PATCH_INFO_FIELD:
5758 case MONO_PATCH_INFO_SFLDA:
5759 encode_field_info (acfg, patch_info->data.field, p, &p);
5761 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
5763 case MONO_PATCH_INFO_RGCTX_FETCH:
5764 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX: {
5765 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
5770 * entry->method has a lenghtly encoding and multiple rgctx_fetch entries
5771 * reference the same method, so encode the method only once.
5773 offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_blob_hash, entry->method));
5775 buf2 = (guint8 *)g_malloc (1024);
5778 encode_method_ref (acfg, entry->method, p2, &p2);
5779 g_assert (p2 - buf2 < 1024);
5781 offset = add_to_blob (acfg, buf2, p2 - buf2);
5784 g_hash_table_insert (acfg->method_blob_hash, entry->method, GUINT_TO_POINTER (offset + 1));
5789 encode_value (offset, p, &p);
5790 g_assert ((int)entry->info_type < 256);
5791 g_assert (entry->data->type < 256);
5792 encode_value ((entry->in_mrgctx ? 1 : 0) | (entry->info_type << 1) | (entry->data->type << 9), p, &p);
5793 encode_patch (acfg, entry->data, p, &p);
5796 case MONO_PATCH_INFO_SEQ_POINT_INFO:
5797 case MONO_PATCH_INFO_AOT_MODULE:
5799 case MONO_PATCH_INFO_SIGNATURE:
5800 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER:
5801 encode_signature (acfg, (MonoMethodSignature*)patch_info->data.target, p, &p);
5803 case MONO_PATCH_INFO_GSHAREDVT_CALL:
5804 encode_signature (acfg, (MonoMethodSignature*)patch_info->data.gsharedvt->sig, p, &p);
5805 encode_method_ref (acfg, patch_info->data.gsharedvt->method, p, &p);
5807 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
5808 MonoGSharedVtMethodInfo *info = patch_info->data.gsharedvt_method;
5811 encode_method_ref (acfg, info->method, p, &p);
5812 encode_value (info->num_entries, p, &p);
5813 for (i = 0; i < info->num_entries; ++i) {
5814 MonoRuntimeGenericContextInfoTemplate *template_ = &info->entries [i];
5816 encode_value (template_->info_type, p, &p);
5817 switch (mini_rgctx_info_type_to_patch_info_type (template_->info_type)) {
5818 case MONO_PATCH_INFO_CLASS:
5819 encode_klass_ref (acfg, mono_class_from_mono_type ((MonoType *)template_->data), p, &p);
5821 case MONO_PATCH_INFO_FIELD:
5822 encode_field_info (acfg, (MonoClassField *)template_->data, p, &p);
5825 g_assert_not_reached ();
5831 case MONO_PATCH_INFO_LDSTR_LIT: {
5832 const char *s = (const char *)patch_info->data.target;
5833 int len = strlen (s);
5835 encode_value (len, p, &p);
5836 memcpy (p, s, len + 1);
5840 case MONO_PATCH_INFO_VIRT_METHOD:
5841 encode_klass_ref (acfg, patch_info->data.virt_method->klass, p, &p);
5842 encode_method_ref (acfg, patch_info->data.virt_method->method, p, &p);
5844 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG:
5845 case MONO_PATCH_INFO_JIT_THREAD_ATTACH:
5848 g_warning ("unable to handle jump info %d", patch_info->type);
5849 g_assert_not_reached ();
5856 encode_patch_list (MonoAotCompile *acfg, GPtrArray *patches, int n_patches, gboolean llvm, int first_got_offset, guint8 *buf, guint8 **endbuf)
5859 guint32 pindex, offset;
5860 MonoJumpInfo *patch_info;
5862 encode_value (n_patches, p, &p);
5864 for (pindex = 0; pindex < patches->len; ++pindex) {
5865 patch_info = (MonoJumpInfo *)g_ptr_array_index (patches, pindex);
5867 if (patch_info->type == MONO_PATCH_INFO_NONE || patch_info->type == MONO_PATCH_INFO_BB)
5871 offset = get_got_offset (acfg, llvm, patch_info);
5872 encode_value (offset, p, &p);
5879 emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
5882 int pindex, buf_size, n_patches;
5884 MonoJumpInfo *patch_info;
5885 guint32 method_index;
5887 guint32 first_got_offset;
5889 method = cfg->orig_method;
5891 method_index = get_method_index (acfg, method);
5893 /* Sort relocations */
5894 patches = g_ptr_array_new ();
5895 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
5896 g_ptr_array_add (patches, patch_info);
5897 g_ptr_array_sort (patches, compare_patches);
5899 first_got_offset = acfg->cfgs [method_index]->got_offset;
5901 /**********************/
5902 /* Encode method info */
5903 /**********************/
5905 buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
5906 p = buf = (guint8 *)g_malloc (buf_size);
5908 if (mono_class_get_cctor (method->klass)) {
5909 encode_value (1, p, &p);
5910 encode_klass_ref (acfg, method->klass, p, &p);
5912 /* Not needed when loading the method */
5913 encode_value (0, p, &p);
5916 g_assert (!(cfg->opt & MONO_OPT_SHARED));
5919 for (pindex = 0; pindex < patches->len; ++pindex) {
5920 patch_info = (MonoJumpInfo *)g_ptr_array_index (patches, pindex);
5922 if ((patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
5923 (patch_info->type == MONO_PATCH_INFO_NONE)) {
5924 patch_info->type = MONO_PATCH_INFO_NONE;
5929 if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
5930 /* Stored in a GOT slot initialized at module load time */
5931 patch_info->type = MONO_PATCH_INFO_NONE;
5935 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR ||
5936 patch_info->type == MONO_PATCH_INFO_GC_NURSERY_START ||
5937 patch_info->type == MONO_PATCH_INFO_GC_NURSERY_BITS ||
5938 patch_info->type == MONO_PATCH_INFO_AOT_MODULE) {
5939 /* Stored in a GOT slot initialized at module load time */
5940 patch_info->type = MONO_PATCH_INFO_NONE;
5944 if (is_plt_patch (patch_info) && !(cfg->compile_llvm && acfg->aot_opts.llvm_only)) {
5945 /* Calls are made through the PLT */
5946 patch_info->type = MONO_PATCH_INFO_NONE;
5954 g_assert (cfg->has_got_slots);
5956 encode_patch_list (acfg, patches, n_patches, cfg->compile_llvm, first_got_offset, p, &p);
5958 g_ptr_array_free (patches, TRUE);
5960 acfg->stats.info_size += p - buf;
5962 g_assert (p - buf < buf_size);
5964 cfg->method_info_offset = add_to_blob (acfg, buf, p - buf);
5969 get_unwind_info_offset (MonoAotCompile *acfg, guint8 *encoded, guint32 encoded_len)
5971 guint32 cache_index;
5974 /* Reuse the unwind module to canonize and store unwind info entries */
5975 cache_index = mono_cache_unwind_info (encoded, encoded_len);
5977 /* Use +/- 1 to distinguish 0s from missing entries */
5978 offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1)));
5986 * It would be easier to use assembler symbols, but the caller needs an
5989 offset = acfg->unwind_info_offset;
5990 g_hash_table_insert (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1), GUINT_TO_POINTER (offset + 1));
5991 g_ptr_array_add (acfg->unwind_ops, GUINT_TO_POINTER (cache_index));
5994 encode_value (encoded_len, p, &p);
5996 acfg->unwind_info_offset += encoded_len + (p - buf);
6002 emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg, gboolean store_seq_points)
6005 guint32 debug_info_size, seq_points_size;
6007 MonoMethodHeader *header;
6008 guint8 *p, *buf, *debug_info;
6009 MonoJitInfo *jinfo = cfg->jit_info;
6011 gboolean use_unwind_ops = FALSE;
6012 MonoSeqPointInfo *seq_points;
6014 code = cfg->native_code;
6015 header = cfg->header;
6017 if (!acfg->aot_opts.nodebug) {
6018 mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
6021 debug_info_size = 0;
6024 seq_points = cfg->seq_point_info;
6025 seq_points_size = (store_seq_points)? mono_seq_point_info_get_write_size (seq_points) : 0;
6027 buf_size = header->num_clauses * 256 + debug_info_size + 2048 + seq_points_size + cfg->gc_map_size;
6029 p = buf = (guint8 *)g_malloc (buf_size);
6031 use_unwind_ops = cfg->unwind_ops != NULL;
6033 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);
6035 encode_value (flags, p, &p);
6037 if (use_unwind_ops) {
6038 guint32 encoded_len;
6040 guint32 unwind_desc;
6042 encoded = mono_unwind_ops_encode (cfg->unwind_ops, &encoded_len);
6044 unwind_desc = get_unwind_info_offset (acfg, encoded, encoded_len);
6045 encode_value (unwind_desc, p, &p);
6049 encode_value (jinfo->unwind_info, p, &p);
6052 /*Encode the number of holes before the number of clauses to make decoding easier*/
6053 if (jinfo->has_try_block_holes) {
6054 MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
6055 encode_value (table->num_holes, p, &p);
6058 if (jinfo->has_arch_eh_info) {
6060 * In AOT mode, the code length is calculated from the address of the previous method,
6061 * which could include alignment padding, so calculating the start of the epilog as
6062 * code_len - epilog_size is correct any more. Save the real code len as a workaround.
6064 encode_value (jinfo->code_size, p, &p);
6067 /* Exception table */
6068 if (cfg->compile_llvm) {
6070 * When using LLVM, we can't emit some data, like pc offsets, this reg/offset etc.,
6071 * since the information is only available to llc. Instead, we let llc save the data
6072 * into the LSDA, and read it from there at runtime.
6074 /* The assembly might be CIL stripped so emit the data ourselves */
6075 if (header->num_clauses)
6076 encode_value (header->num_clauses, p, &p);
6078 for (k = 0; k < header->num_clauses; ++k) {
6079 MonoExceptionClause *clause;
6081 clause = &header->clauses [k];
6083 encode_value (clause->flags, p, &p);
6084 if (clause->data.catch_class) {
6085 encode_value (1, p, &p);
6086 encode_klass_ref (acfg, clause->data.catch_class, p, &p);
6088 encode_value (0, p, &p);
6091 /* Emit the IL ranges too, since they might not be available at runtime */
6092 encode_value (clause->try_offset, p, &p);
6093 encode_value (clause->try_len, p, &p);
6094 encode_value (clause->handler_offset, p, &p);
6095 encode_value (clause->handler_len, p, &p);
6097 /* Emit a list of nesting clauses */
6098 for (i = 0; i < header->num_clauses; ++i) {
6100 MonoExceptionClause *clause1 = &header->clauses [cindex1];
6102 MonoExceptionClause *clause2 = &header->clauses [cindex2];
6104 if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset)
6105 encode_value (i, p, &p);
6107 encode_value (-1, p, &p);
6110 if (jinfo->num_clauses)
6111 encode_value (jinfo->num_clauses, p, &p);
6113 for (k = 0; k < jinfo->num_clauses; ++k) {
6114 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
6116 encode_value (ei->flags, p, &p);
6117 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
6118 /* Not used for catch clauses */
6119 if (ei->flags != MONO_EXCEPTION_CLAUSE_NONE)
6120 encode_value (ei->exvar_offset, p, &p);
6122 encode_value (ei->exvar_offset, p, &p);
6125 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
6126 encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
6128 if (ei->data.catch_class) {
6132 buf2 = (guint8 *)g_malloc (4096);
6134 encode_klass_ref (acfg, ei->data.catch_class, p2, &p2);
6136 g_assert (len < 4096);
6137 encode_value (len, p, &p);
6138 memcpy (p, buf2, len);
6142 encode_value (0, p, &p);
6146 encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
6147 encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
6148 encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
6152 if (jinfo->has_try_block_holes) {
6153 MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
6154 for (i = 0; i < table->num_holes; ++i) {
6155 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
6156 encode_value (hole->clause, p, &p);
6157 encode_value (hole->length, p, &p);
6158 encode_value (hole->offset, p, &p);
6162 if (jinfo->has_arch_eh_info) {
6163 MonoArchEHJitInfo *eh_info;
6165 eh_info = mono_jit_info_get_arch_eh_info (jinfo);
6166 encode_value (eh_info->stack_size, p, &p);
6167 encode_value (eh_info->epilog_size, p, &p);
6170 if (jinfo->has_generic_jit_info) {
6171 MonoGenericJitInfo *gi = mono_jit_info_get_generic_jit_info (jinfo);
6172 MonoGenericSharingContext* gsctx = gi->generic_sharing_context;
6176 encode_value (gi->nlocs, p, &p);
6178 for (i = 0; i < gi->nlocs; ++i) {
6179 MonoDwarfLocListEntry *entry = &gi->locations [i];
6181 encode_value (entry->is_reg ? 1 : 0, p, &p);
6182 encode_value (entry->reg, p, &p);
6184 encode_value (entry->offset, p, &p);
6186 g_assert (entry->from == 0);
6188 encode_value (entry->from, p, &p);
6189 encode_value (entry->to, p, &p);
6192 if (!cfg->compile_llvm) {
6193 encode_value (gi->has_this ? 1 : 0, p, &p);
6194 encode_value (gi->this_reg, p, &p);
6195 encode_value (gi->this_offset, p, &p);
6200 * Need to encode jinfo->method too, since it is not equal to 'method'
6201 * when using generic sharing.
6203 buf2 = (guint8 *)g_malloc (4096);
6205 encode_method_ref (acfg, jinfo->d.method, p2, &p2);
6207 g_assert (len < 4096);
6208 encode_value (len, p, &p);
6209 memcpy (p, buf2, len);
6213 if (gsctx && gsctx->is_gsharedvt) {
6214 encode_value (1, p, &p);
6216 encode_value (0, p, &p);
6220 if (seq_points_size)
6221 p += mono_seq_point_info_write (seq_points, p);
6223 g_assert (debug_info_size < buf_size);
6225 encode_value (debug_info_size, p, &p);
6226 if (debug_info_size) {
6227 memcpy (p, debug_info, debug_info_size);
6228 p += debug_info_size;
6229 g_free (debug_info);
6234 encode_value (cfg->gc_map_size, p, &p);
6235 /* The GC map requires 4 bytes of alignment */
6236 while ((gsize)p % 4)
6238 memcpy (p, cfg->gc_map, cfg->gc_map_size);
6239 p += cfg->gc_map_size;
6242 acfg->stats.ex_info_size += p - buf;
6244 g_assert (p - buf < buf_size);
6247 /* The GC Map requires 4 byte alignment */
6248 cfg->ex_info_offset = add_to_blob_aligned (acfg, buf, p - buf, cfg->gc_map ? 4 : 1);
6253 emit_klass_info (MonoAotCompile *acfg, guint32 token)
6256 MonoClass *klass = mono_class_get_checked (acfg->image, token, &error);
6258 int i, buf_size, res;
6259 gboolean no_special_static, cant_encode;
6260 gpointer iter = NULL;
6263 mono_error_cleanup (&error);
6267 p = buf = (guint8 *)g_malloc (buf_size);
6269 /* Mark as unusable */
6270 encode_value (-1, p, &p);
6272 res = add_to_blob (acfg, buf, p - buf);
6278 buf_size = 10240 + (klass->vtable_size * 16);
6279 p = buf = (guint8 *)g_malloc (buf_size);
6283 mono_class_init (klass);
6285 mono_class_get_nested_types (klass, &iter);
6286 g_assert (klass->nested_classes_inited);
6288 mono_class_setup_vtable (klass);
6291 * Emit all the information which is required for creating vtables so
6292 * the runtime does not need to create the MonoMethod structures which
6293 * take up a lot of space.
6296 no_special_static = !mono_class_has_special_static_fields (klass);
6298 /* Check whenever we have enough info to encode the vtable */
6299 cant_encode = FALSE;
6300 for (i = 0; i < klass->vtable_size; ++i) {
6301 MonoMethod *cm = klass->vtable [i];
6303 if (cm && mono_method_signature (cm)->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, cm))
6307 mono_class_has_finalizer (klass);
6308 if (mono_class_has_failure (klass))
6311 if (mono_class_is_gtd (klass) || cant_encode) {
6312 encode_value (-1, p, &p);
6314 gboolean has_nested = mono_class_get_nested_classes_property (klass) != NULL;
6315 encode_value (klass->vtable_size, p, &p);
6316 encode_value ((mono_class_is_gtd (klass) ? (1 << 8) : 0) | (no_special_static << 7) | (klass->has_static_refs << 6) | (klass->has_references << 5) | ((klass->blittable << 4) | (has_nested ? 1 : 0) << 3) | (klass->has_cctor << 2) | (klass->has_finalize << 1) | klass->ghcimpl, p, &p);
6317 if (klass->has_cctor)
6318 encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
6319 if (klass->has_finalize)
6320 encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
6322 encode_value (klass->instance_size, p, &p);
6323 encode_value (mono_class_data_size (klass), p, &p);
6324 encode_value (klass->packing_size, p, &p);
6325 encode_value (klass->min_align, p, &p);
6327 for (i = 0; i < klass->vtable_size; ++i) {
6328 MonoMethod *cm = klass->vtable [i];
6331 encode_method_ref (acfg, cm, p, &p);
6333 encode_value (0, p, &p);
6337 acfg->stats.class_info_size += p - buf;
6339 g_assert (p - buf < buf_size);
6340 res = add_to_blob (acfg, buf, p - buf);
6347 get_plt_entry_debug_sym (MonoAotCompile *acfg, MonoJumpInfo *ji, GHashTable *cache)
6349 char *debug_sym = NULL;
6352 if (acfg->llvm && llvm_acfg->aot_opts.static_link) {
6353 /* Need to add a prefix to create unique symbols */
6354 prefix = g_strdup_printf ("plt_%s_", acfg->assembly_name_sym);
6356 #if defined(TARGET_WIN32) && defined(TARGET_X86)
6357 prefix = mangle_symbol_alloc ("plt_");
6359 prefix = g_strdup ("plt_");
6364 case MONO_PATCH_INFO_METHOD:
6365 debug_sym = get_debug_sym (ji->data.method, prefix, cache);
6367 case MONO_PATCH_INFO_INTERNAL_METHOD:
6368 debug_sym = g_strdup_printf ("%s_jit_icall_%s", prefix, ji->data.name);
6370 case MONO_PATCH_INFO_RGCTX_FETCH:
6371 debug_sym = g_strdup_printf ("%s_rgctx_fetch_%d", prefix, acfg->label_generator ++);
6373 case MONO_PATCH_INFO_ICALL_ADDR:
6374 case MONO_PATCH_INFO_ICALL_ADDR_CALL: {
6375 char *s = get_debug_sym (ji->data.method, "", cache);
6377 debug_sym = g_strdup_printf ("%s_icall_native_%s", prefix, s);
6381 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
6382 debug_sym = g_strdup_printf ("%s_jit_icall_native_%s", prefix, ji->data.name);
6390 return sanitize_symbol (acfg, debug_sym);
6394 * Calls made from AOTed code are routed through a table of jumps similar to the
6395 * ELF PLT (Program Linkage Table). Initially the PLT entries jump to code which transfers
6396 * control to the AOT runtime through a trampoline.
6399 emit_plt (MonoAotCompile *acfg)
6403 if (acfg->aot_opts.llvm_only) {
6404 g_assert (acfg->plt_offset == 1);
6410 emit_section_change (acfg, ".text", 0);
6411 emit_alignment_code (acfg, 16);
6412 emit_info_symbol (acfg, "plt");
6413 emit_label (acfg, acfg->plt_symbol);
6415 for (i = 0; i < acfg->plt_offset; ++i) {
6416 char *debug_sym = NULL;
6417 MonoPltEntry *plt_entry = NULL;
6421 * The first plt entry is unused.
6425 plt_entry = (MonoPltEntry *)g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
6427 debug_sym = plt_entry->debug_sym;
6429 if (acfg->thumb_mixed && !plt_entry->jit_used)
6430 /* Emit only a thumb version */
6433 /* Skip plt entries not actually called */
6434 if (!plt_entry->jit_used && !plt_entry->llvm_used)
6437 if (acfg->llvm && !acfg->thumb_mixed) {
6438 emit_label (acfg, plt_entry->llvm_symbol);
6440 emit_global_inner (acfg, plt_entry->llvm_symbol, TRUE);
6441 #if defined(TARGET_MACH)
6442 fprintf (acfg->fp, ".private_extern %s\n", plt_entry->llvm_symbol);
6448 if (acfg->need_no_dead_strip) {
6449 emit_unset_mode (acfg);
6450 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
6452 emit_local_symbol (acfg, debug_sym, NULL, TRUE);
6453 emit_label (acfg, debug_sym);
6456 emit_label (acfg, plt_entry->symbol);
6458 arch_emit_plt_entry (acfg, acfg->got_symbol, (acfg->plt_got_offset_base + i) * sizeof (gpointer), acfg->plt_got_info_offsets [i]);
6461 emit_symbol_size (acfg, debug_sym, ".");
6464 if (acfg->thumb_mixed) {
6465 /* Make sure the ARM symbols don't alias the thumb ones */
6466 emit_zero_bytes (acfg, 16);
6469 * Emit a separate set of PLT entries using thumb2 which is called by LLVM generated
6472 for (i = 0; i < acfg->plt_offset; ++i) {
6473 char *debug_sym = NULL;
6474 MonoPltEntry *plt_entry = NULL;
6479 plt_entry = (MonoPltEntry *)g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
6481 /* Skip plt entries not actually called by LLVM code */
6482 if (!plt_entry->llvm_used)
6485 if (acfg->aot_opts.write_symbols) {
6486 if (plt_entry->debug_sym)
6487 debug_sym = g_strdup_printf ("%s_thumb", plt_entry->debug_sym);
6491 #if defined(TARGET_MACH)
6492 fprintf (acfg->fp, " .thumb_func %s\n", debug_sym);
6493 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
6495 emit_local_symbol (acfg, debug_sym, NULL, TRUE);
6496 emit_label (acfg, debug_sym);
6498 fprintf (acfg->fp, "\n.thumb_func\n");
6500 emit_label (acfg, plt_entry->llvm_symbol);
6503 emit_global_inner (acfg, plt_entry->llvm_symbol, TRUE);
6505 arch_emit_llvm_plt_entry (acfg, acfg->got_symbol, (acfg->plt_got_offset_base + i) * sizeof (gpointer), acfg->plt_got_info_offsets [i]);
6508 emit_symbol_size (acfg, debug_sym, ".");
6514 emit_symbol_size (acfg, acfg->plt_symbol, ".");
6516 emit_info_symbol (acfg, "plt_end");
6520 * emit_trampoline_full:
6522 * If EMIT_TINFO is TRUE, emit additional information which can be used to create a MonoJitInfo for this trampoline by
6523 * create_jit_info_for_trampoline ().
6525 static G_GNUC_UNUSED void
6526 emit_trampoline_full (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info, gboolean emit_tinfo)
6528 char start_symbol [MAX_SYMBOL_SIZE];
6529 char end_symbol [MAX_SYMBOL_SIZE];
6530 char symbol [MAX_SYMBOL_SIZE];
6531 guint32 buf_size, info_offset;
6532 MonoJumpInfo *patch_info;
6545 code_size = info->code_size;
6547 unwind_ops = info->unwind_ops;
6551 sprintf (start_symbol, "%s%s", acfg->user_symbol_prefix, name);
6553 emit_section_change (acfg, ".text", 0);
6554 emit_global (acfg, start_symbol, TRUE);
6555 emit_alignment_code (acfg, AOT_FUNC_ALIGNMENT);
6556 emit_label (acfg, start_symbol);
6558 sprintf (symbol, "%snamed_%s", acfg->temp_prefix, name);
6559 emit_label (acfg, symbol);
6562 * The code should access everything through the GOT, so we pass
6565 emit_and_reloc_code (acfg, NULL, code, code_size, ji, TRUE, NULL);
6567 emit_symbol_size (acfg, start_symbol, ".");
6570 sprintf (end_symbol, "%snamede_%s", acfg->temp_prefix, name);
6571 emit_label (acfg, end_symbol);
6576 /* Sort relocations */
6577 patches = g_ptr_array_new ();
6578 for (patch_info = ji; patch_info; patch_info = patch_info->next)
6579 if (patch_info->type != MONO_PATCH_INFO_NONE)
6580 g_ptr_array_add (patches, patch_info);
6581 g_ptr_array_sort (patches, compare_patches);
6583 buf_size = patches->len * 128 + 128;
6584 buf = (guint8 *)g_malloc (buf_size);
6587 encode_patch_list (acfg, patches, patches->len, FALSE, got_offset, p, &p);
6588 g_assert (p - buf < buf_size);
6589 g_ptr_array_free (patches, TRUE);
6591 sprintf (symbol, "%s%s_p", acfg->user_symbol_prefix, name);
6593 info_offset = add_to_blob (acfg, buf, p - buf);
6595 emit_section_change (acfg, RODATA_SECT, 0);
6596 emit_global (acfg, symbol, FALSE);
6597 emit_label (acfg, symbol);
6599 emit_int32 (acfg, info_offset);
6603 guint32 encoded_len;
6607 * Emit additional information which can be used to reconstruct a partial MonoTrampInfo.
6609 encoded = mono_unwind_ops_encode (info->unwind_ops, &encoded_len);
6610 uw_offset = get_unwind_info_offset (acfg, encoded, encoded_len);
6613 emit_symbol_diff (acfg, end_symbol, start_symbol, 0);
6614 emit_int32 (acfg, uw_offset);
6617 /* Emit debug info */
6619 char symbol2 [MAX_SYMBOL_SIZE];
6621 sprintf (symbol, "%s", name);
6622 sprintf (symbol2, "%snamed_%s", acfg->temp_prefix, name);
6625 mono_dwarf_writer_emit_trampoline (acfg->dwarf, symbol, symbol2, NULL, NULL, code_size, unwind_ops);
6631 static G_GNUC_UNUSED void
6632 emit_trampoline (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info)
6634 emit_trampoline_full (acfg, got_offset, info, TRUE);
6638 emit_trampolines (MonoAotCompile *acfg)
6640 char symbol [MAX_SYMBOL_SIZE];
6641 char end_symbol [MAX_SYMBOL_SIZE];
6642 int i, tramp_got_offset;
6644 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
6648 if (!mono_aot_mode_is_full (&acfg->aot_opts) || acfg->aot_opts.llvm_only)
6651 g_assert (acfg->image->assembly);
6653 /* Currently, we emit most trampolines into the mscorlib AOT image. */
6654 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
6655 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
6656 MonoTrampInfo *info;
6659 * Emit the generic trampolines.
6661 * We could save some code by treating the generic trampolines as a wrapper
6662 * method, but that approach has its own complexities, so we choose the simpler
6665 for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
6666 /* we overload the boolean here to indicate the slightly different trampoline needed, see mono_arch_create_generic_trampoline() */
6667 #ifdef DISABLE_REMOTING
6668 if (tramp_type == MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING)
6671 #ifndef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
6672 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)
6675 mono_arch_create_generic_trampoline ((MonoTrampolineType)tramp_type, &info, acfg->aot_opts.use_trampolines_page? 2: TRUE);
6676 emit_trampoline (acfg, acfg->got_offset, info);
6679 /* Emit the exception related code pieces */
6680 mono_arch_get_restore_context (&info, TRUE);
6681 emit_trampoline (acfg, acfg->got_offset, info);
6682 mono_arch_get_call_filter (&info, TRUE);
6683 emit_trampoline (acfg, acfg->got_offset, info);
6684 mono_arch_get_throw_exception (&info, TRUE);
6685 emit_trampoline (acfg, acfg->got_offset, info);
6686 mono_arch_get_rethrow_exception (&info, TRUE);
6687 emit_trampoline (acfg, acfg->got_offset, info);
6688 mono_arch_get_throw_corlib_exception (&info, TRUE);
6689 emit_trampoline (acfg, acfg->got_offset, info);
6691 #ifdef MONO_ARCH_HAVE_SDB_TRAMPOLINES
6692 mono_arch_create_sdb_trampoline (TRUE, &info, TRUE);
6693 emit_trampoline (acfg, acfg->got_offset, info);
6694 mono_arch_create_sdb_trampoline (FALSE, &info, TRUE);
6695 emit_trampoline (acfg, acfg->got_offset, info);
6698 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
6699 mono_arch_get_gsharedvt_trampoline (&info, TRUE);
6701 emit_trampoline_full (acfg, acfg->got_offset, info, TRUE);
6703 /* Create a separate out trampoline for more information in stack traces */
6704 info->name = g_strdup ("gsharedvt_out_trampoline");
6705 emit_trampoline_full (acfg, acfg->got_offset, info, TRUE);
6709 #if defined(MONO_ARCH_HAVE_GET_TRAMPOLINES)
6711 GSList *l = mono_arch_get_trampolines (TRUE);
6714 MonoTrampInfo *info = (MonoTrampInfo *)l->data;
6716 emit_trampoline (acfg, acfg->got_offset, info);
6722 for (i = 0; i < acfg->aot_opts.nrgctx_fetch_trampolines; ++i) {
6725 offset = MONO_RGCTX_SLOT_MAKE_RGCTX (i);
6726 mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
6727 emit_trampoline (acfg, acfg->got_offset, info);
6730 offset = MONO_RGCTX_SLOT_MAKE_MRGCTX (i);
6731 mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
6732 emit_trampoline (acfg, acfg->got_offset, info);
6736 #ifdef MONO_ARCH_HAVE_GENERAL_RGCTX_LAZY_FETCH_TRAMPOLINE
6737 mono_arch_create_general_rgctx_lazy_fetch_trampoline (&info, TRUE);
6738 emit_trampoline (acfg, acfg->got_offset, info);
6744 /* delegate_invoke_impl trampolines */
6745 l = mono_arch_get_delegate_invoke_impls ();
6747 MonoTrampInfo *info = (MonoTrampInfo *)l->data;
6749 emit_trampoline (acfg, acfg->got_offset, info);
6754 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD_AOT
6755 mono_arch_create_handler_block_trampoline (&info, TRUE);
6756 emit_trampoline (acfg, acfg->got_offset, info);
6759 #endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
6761 /* Emit trampolines which are numerous */
6764 * These include the following:
6765 * - specific trampolines
6766 * - static rgctx invoke trampolines
6768 * These trampolines have the same code, they are parameterized by GOT
6770 * They are defined in this file, in the arch_... routines instead of
6771 * in tramp-<ARCH>.c, since it is easier to do it this way.
6775 * When running in aot-only mode, we can't create specific trampolines at
6776 * runtime, so we create a few, and save them in the AOT file.
6777 * Normal trampolines embed their argument as a literal inside the
6778 * trampoline code, we can't do that here, so instead we embed an offset
6779 * which needs to be added to the trampoline address to get the address of
6780 * the GOT slot which contains the argument value.
6781 * The generated trampolines jump to the generic trampolines using another
6782 * GOT slot, which will be setup by the AOT loader to point to the
6783 * generic trampoline code of the given type.
6787 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for
6791 emit_section_change (acfg, ".text", 0);
6793 tramp_got_offset = acfg->got_offset;
6795 for (ntype = 0; ntype < MONO_AOT_TRAMP_NUM; ++ntype) {
6797 case MONO_AOT_TRAMP_SPECIFIC:
6798 sprintf (symbol, "specific_trampolines");
6800 case MONO_AOT_TRAMP_STATIC_RGCTX:
6801 sprintf (symbol, "static_rgctx_trampolines");
6803 case MONO_AOT_TRAMP_IMT:
6804 sprintf (symbol, "imt_trampolines");
6806 case MONO_AOT_TRAMP_GSHAREDVT_ARG:
6807 sprintf (symbol, "gsharedvt_arg_trampolines");
6810 g_assert_not_reached ();
6813 sprintf (end_symbol, "%s_e", symbol);
6815 if (acfg->aot_opts.write_symbols)
6816 emit_local_symbol (acfg, symbol, end_symbol, TRUE);
6818 emit_alignment_code (acfg, AOT_FUNC_ALIGNMENT);
6819 emit_info_symbol (acfg, symbol);
6821 acfg->trampoline_got_offset_base [ntype] = tramp_got_offset;
6823 for (i = 0; i < acfg->num_trampolines [ntype]; ++i) {
6827 case MONO_AOT_TRAMP_SPECIFIC:
6828 arch_emit_specific_trampoline (acfg, tramp_got_offset, &tramp_size);
6829 tramp_got_offset += 2;
6831 case MONO_AOT_TRAMP_STATIC_RGCTX:
6832 arch_emit_static_rgctx_trampoline (acfg, tramp_got_offset, &tramp_size);
6833 tramp_got_offset += 2;
6835 case MONO_AOT_TRAMP_IMT:
6836 arch_emit_imt_trampoline (acfg, tramp_got_offset, &tramp_size);
6837 tramp_got_offset += 1;
6839 case MONO_AOT_TRAMP_GSHAREDVT_ARG:
6840 arch_emit_gsharedvt_arg_trampoline (acfg, tramp_got_offset, &tramp_size);
6841 tramp_got_offset += 2;
6844 g_assert_not_reached ();
6846 if (!acfg->trampoline_size [ntype]) {
6847 g_assert (tramp_size);
6848 acfg->trampoline_size [ntype] = tramp_size;
6852 emit_label (acfg, end_symbol);
6853 emit_int32 (acfg, 0);
6856 arch_emit_specific_trampoline_pages (acfg);
6858 /* Reserve some entries at the end of the GOT for our use */
6859 acfg->num_trampoline_got_entries = tramp_got_offset - acfg->got_offset;
6862 acfg->got_offset += acfg->num_trampoline_got_entries;
6866 str_begins_with (const char *str1, const char *str2)
6868 int len = strlen (str2);
6869 return strncmp (str1, str2, len) == 0;
6873 mono_aot_readonly_field_override (MonoClassField *field)
6876 for (rdv = readonly_values; rdv; rdv = rdv->next) {
6877 char *p = rdv->name;
6879 len = strlen (field->parent->name_space);
6880 if (strncmp (p, field->parent->name_space, len))
6885 len = strlen (field->parent->name);
6886 if (strncmp (p, field->parent->name, len))
6891 if (strcmp (p, field->name))
6893 switch (rdv->type) {
6895 return &rdv->value.i1;
6897 return &rdv->value.i2;
6899 return &rdv->value.i4;
6908 add_readonly_value (MonoAotOptions *opts, const char *val)
6913 /* the format of val is:
6914 * namespace.typename.fieldname=type/value
6915 * type can be i1 for uint8/int8/boolean, i2 for uint16/int16/char, i4 for uint32/int32
6917 fval = strrchr (val, '/');
6919 fprintf (stderr, "AOT : invalid format for readonly field '%s', missing /.\n", val);
6922 tval = strrchr (val, '=');
6924 fprintf (stderr, "AOT : invalid format for readonly field '%s', missing =.\n", val);
6927 rdv = g_new0 (ReadOnlyValue, 1);
6928 rdv->name = (char *)g_malloc0 (tval - val + 1);
6929 memcpy (rdv->name, val, tval - val);
6932 if (strncmp (tval, "i1", 2) == 0) {
6933 rdv->value.i1 = atoi (fval);
6934 rdv->type = MONO_TYPE_I1;
6935 } else if (strncmp (tval, "i2", 2) == 0) {
6936 rdv->value.i2 = atoi (fval);
6937 rdv->type = MONO_TYPE_I2;
6938 } else if (strncmp (tval, "i4", 2) == 0) {
6939 rdv->value.i4 = atoi (fval);
6940 rdv->type = MONO_TYPE_I4;
6942 fprintf (stderr, "AOT : unsupported type for readonly field '%s'.\n", tval);
6945 rdv->next = readonly_values;
6946 readonly_values = rdv;
6950 clean_path (gchar * path)
6955 if (g_str_has_suffix (path, G_DIR_SEPARATOR_S))
6958 gchar *clean = g_strconcat (path, G_DIR_SEPARATOR_S, NULL);
6965 wrap_path (gchar * path)
6971 // If the string contains no spaces, just return the original string.
6972 if (strstr (path, " ") == NULL)
6975 // If the string is already wrapped in quotes, return it.
6976 len = strlen (path);
6977 if (len >= 2 && path[0] == '\"' && path[len-1] == '\"')
6980 // If the string contains spaces, then wrap it in quotes.
6981 gchar *clean = g_strdup_printf ("\"%s\"", path);
6986 // Duplicate a char range and add it to a ptrarray, but only if it is nonempty
6988 ptr_array_add_range_if_nonempty(GPtrArray *args, gchar const *start, gchar const *end)
6990 ptrdiff_t len = end-start;
6992 g_ptr_array_add (args, g_strndup (start, len));
6996 mono_aot_split_options (const char *aot_options)
6998 enum MonoAotOptionState {
6999 MONO_AOT_OPTION_STATE_DEFAULT,
7000 MONO_AOT_OPTION_STATE_STRING,
7001 MONO_AOT_OPTION_STATE_ESCAPE,
7004 GPtrArray *args = g_ptr_array_new ();
7005 enum MonoAotOptionState state = MONO_AOT_OPTION_STATE_DEFAULT;
7006 gchar const *opt_start = aot_options;
7007 gboolean end_of_string = FALSE;
7010 g_return_val_if_fail (aot_options != NULL, NULL);
7012 while ((cur = *aot_options) != '\0') {
7013 if (state == MONO_AOT_OPTION_STATE_ESCAPE)
7018 // If we find a quote, then if we're in the default case then
7019 // it means we've found the start of a string, if not then it
7020 // means we've found the end of the string and should switch
7021 // back to the default case.
7023 case MONO_AOT_OPTION_STATE_DEFAULT:
7024 state = MONO_AOT_OPTION_STATE_STRING;
7026 case MONO_AOT_OPTION_STATE_STRING:
7027 state = MONO_AOT_OPTION_STATE_DEFAULT;
7029 case MONO_AOT_OPTION_STATE_ESCAPE:
7030 g_assert_not_reached ();
7035 // If we've found an escaping operator, then this means we
7036 // should not process the next character if inside a string.
7037 if (state == MONO_AOT_OPTION_STATE_STRING)
7038 state = MONO_AOT_OPTION_STATE_ESCAPE;
7041 // If we're in the default state then this means we've found
7042 // an option, store it for later processing.
7043 if (state == MONO_AOT_OPTION_STATE_DEFAULT)
7051 // If the next character is end of string, then process the last option.
7052 if (*(aot_options) == '\0') {
7053 end_of_string = TRUE;
7059 ptr_array_add_range_if_nonempty (args, opt_start, aot_options);
7060 opt_start = ++aot_options;
7063 goto restart; // Check for null and continue loop
7070 mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
7074 args = mono_aot_split_options (aot_options ? aot_options : "");
7075 for (int i = 0; i < args->len; ++i) {
7076 const char *arg = (const char *)g_ptr_array_index (args, i);
7078 if (str_begins_with (arg, "outfile=")) {
7079 opts->outfile = g_strdup (arg + strlen ("outfile="));
7080 } else if (str_begins_with (arg, "llvm-outfile=")) {
7081 opts->llvm_outfile = g_strdup (arg + strlen ("llvm-outfile="));
7082 } else if (str_begins_with (arg, "temp-path=")) {
7083 opts->temp_path = clean_path (g_strdup (arg + strlen ("temp-path=")));
7084 } else if (str_begins_with (arg, "save-temps")) {
7085 opts->save_temps = TRUE;
7086 } else if (str_begins_with (arg, "keep-temps")) {
7087 opts->save_temps = TRUE;
7088 } else if (str_begins_with (arg, "write-symbols")) {
7089 opts->write_symbols = TRUE;
7090 } else if (str_begins_with (arg, "no-write-symbols")) {
7091 opts->write_symbols = FALSE;
7092 } else if (str_begins_with (arg, "metadata-only")) {
7093 opts->metadata_only = TRUE;
7094 } else if (str_begins_with (arg, "bind-to-runtime-version")) {
7095 opts->bind_to_runtime_version = TRUE;
7096 } else if (str_begins_with (arg, "full")) {
7097 opts->mode = MONO_AOT_MODE_FULL;
7098 } else if (str_begins_with (arg, "hybrid")) {
7099 opts->mode = MONO_AOT_MODE_HYBRID;
7100 } else if (str_begins_with (arg, "threads=")) {
7101 opts->nthreads = atoi (arg + strlen ("threads="));
7102 } else if (str_begins_with (arg, "static")) {
7103 opts->static_link = TRUE;
7104 opts->no_dlsym = TRUE;
7105 } else if (str_begins_with (arg, "asmonly")) {
7106 opts->asm_only = TRUE;
7107 } else if (str_begins_with (arg, "asmwriter")) {
7108 opts->asm_writer = TRUE;
7109 } else if (str_begins_with (arg, "nodebug")) {
7110 opts->nodebug = TRUE;
7111 } else if (str_begins_with (arg, "dwarfdebug")) {
7112 opts->dwarf_debug = TRUE;
7113 } else if (str_begins_with (arg, "nopagetrampolines")) {
7114 opts->use_trampolines_page = FALSE;
7115 } else if (str_begins_with (arg, "ntrampolines=")) {
7116 opts->ntrampolines = atoi (arg + strlen ("ntrampolines="));
7117 } else if (str_begins_with (arg, "nrgctx-trampolines=")) {
7118 opts->nrgctx_trampolines = atoi (arg + strlen ("nrgctx-trampolines="));
7119 } else if (str_begins_with (arg, "nrgctx-fetch-trampolines=")) {
7120 opts->nrgctx_fetch_trampolines = atoi (arg + strlen ("nrgctx-fetch-trampolines="));
7121 } else if (str_begins_with (arg, "nimt-trampolines=")) {
7122 opts->nimt_trampolines = atoi (arg + strlen ("nimt-trampolines="));
7123 } else if (str_begins_with (arg, "ngsharedvt-trampolines=")) {
7124 opts->ngsharedvt_arg_trampolines = atoi (arg + strlen ("ngsharedvt-trampolines="));
7125 } else if (str_begins_with (arg, "tool-prefix=")) {
7126 opts->tool_prefix = g_strdup (arg + strlen ("tool-prefix="));
7127 } else if (str_begins_with (arg, "ld-flags=")) {
7128 opts->ld_flags = g_strdup (arg + strlen ("ld-flags="));
7129 } else if (str_begins_with (arg, "soft-debug")) {
7130 opts->soft_debug = TRUE;
7131 } else if (str_begins_with (arg, "gen-seq-points-file=")) {
7132 fprintf (stderr, "Mono Warning: aot option gen-seq-points-file= is deprecated.\n");
7133 } else if (str_begins_with (arg, "gen-seq-points-file")) {
7134 fprintf (stderr, "Mono Warning: aot option gen-seq-points-file is deprecated.\n");
7135 } else if (str_begins_with (arg, "msym-dir=")) {
7136 debug_options.no_seq_points_compact_data = FALSE;
7137 opts->gen_msym_dir = TRUE;
7138 opts->gen_msym_dir_path = g_strdup (arg + strlen ("msym_dir="));;
7139 } else if (str_begins_with (arg, "direct-pinvoke")) {
7140 opts->direct_pinvoke = TRUE;
7141 } else if (str_begins_with (arg, "direct-icalls")) {
7142 opts->direct_icalls = TRUE;
7143 } else if (str_begins_with (arg, "no-direct-calls")) {
7144 opts->no_direct_calls = TRUE;
7145 } else if (str_begins_with (arg, "print-skipped")) {
7146 opts->print_skipped_methods = TRUE;
7147 } else if (str_begins_with (arg, "stats")) {
7149 } else if (str_begins_with (arg, "no-instances")) {
7150 opts->no_instances = TRUE;
7151 } else if (str_begins_with (arg, "log-generics")) {
7152 opts->log_generics = TRUE;
7153 } else if (str_begins_with (arg, "log-instances=")) {
7154 opts->log_instances = TRUE;
7155 opts->instances_logfile_path = g_strdup (arg + strlen ("log-instances="));
7156 } else if (str_begins_with (arg, "log-instances")) {
7157 opts->log_instances = TRUE;
7158 } else if (str_begins_with (arg, "internal-logfile=")) {
7159 opts->logfile = g_strdup (arg + strlen ("internal-logfile="));
7160 } else if (str_begins_with (arg, "mtriple=")) {
7161 opts->mtriple = g_strdup (arg + strlen ("mtriple="));
7162 } else if (str_begins_with (arg, "llvm-path=")) {
7163 opts->llvm_path = clean_path (g_strdup (arg + strlen ("llvm-path=")));
7164 } else if (!strcmp (arg, "llvm")) {
7166 } else if (str_begins_with (arg, "readonly-value=")) {
7167 add_readonly_value (opts, arg + strlen ("readonly-value="));
7168 } else if (str_begins_with (arg, "info")) {
7169 printf ("AOT target setup: %s.\n", AOT_TARGET_STR);
7171 } else if (str_begins_with (arg, "gc-maps")) {
7172 mini_gc_enable_gc_maps_for_aot ();
7173 } else if (str_begins_with (arg, "dump")) {
7174 opts->dump_json = TRUE;
7175 } else if (str_begins_with (arg, "llvmonly")) {
7176 opts->mode = MONO_AOT_MODE_FULL;
7178 opts->llvm_only = TRUE;
7179 } else if (str_begins_with (arg, "data-outfile=")) {
7180 opts->data_outfile = g_strdup (arg + strlen ("data-outfile="));
7181 } else if (str_begins_with (arg, "help") || str_begins_with (arg, "?")) {
7182 printf ("Supported options for --aot:\n");
7183 printf (" outfile=\n");
7184 printf (" llvm-outfile=\n");
7185 printf (" llvm-path=\n");
7186 printf (" temp-path=\n");
7187 printf (" save-temps\n");
7188 printf (" keep-temps\n");
7189 printf (" write-symbols\n");
7190 printf (" metadata-only\n");
7191 printf (" bind-to-runtime-version\n");
7193 printf (" threads=\n");
7194 printf (" static\n");
7195 printf (" asmonly\n");
7196 printf (" asmwriter\n");
7197 printf (" nodebug\n");
7198 printf (" dwarfdebug\n");
7199 printf (" ntrampolines=\n");
7200 printf (" nrgctx-trampolines=\n");
7201 printf (" nimt-trampolines=\n");
7202 printf (" ngsharedvt-trampolines=\n");
7203 printf (" tool-prefix=\n");
7204 printf (" readonly-value=\n");
7205 printf (" soft-debug\n");
7206 printf (" msym-dir=\n");
7207 printf (" gc-maps\n");
7208 printf (" print-skipped\n");
7209 printf (" no-instances\n");
7210 printf (" stats\n");
7213 printf (" help/?\n");
7216 fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
7220 g_free ((gpointer) arg);
7223 if (opts->use_trampolines_page) {
7224 opts->ntrampolines = 0;
7225 opts->nrgctx_trampolines = 0;
7226 opts->nimt_trampolines = 0;
7227 opts->ngsharedvt_arg_trampolines = 0;
7230 g_ptr_array_free (args, /*free_seg=*/TRUE);
7234 add_token_info_hash (gpointer key, gpointer value, gpointer user_data)
7236 MonoMethod *method = (MonoMethod*)key;
7237 MonoJumpInfoToken *ji = (MonoJumpInfoToken*)value;
7238 MonoAotCompile *acfg = (MonoAotCompile *)user_data;
7239 MonoJumpInfoToken *new_ji;
7241 new_ji = (MonoJumpInfoToken *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfoToken));
7242 new_ji->image = ji->image;
7243 new_ji->token = ji->token;
7244 g_hash_table_insert (acfg->token_info_hash, method, new_ji);
7248 can_encode_class (MonoAotCompile *acfg, MonoClass *klass)
7250 if (klass->type_token)
7252 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR) || (klass->byval_arg.type == MONO_TYPE_PTR))
7255 return can_encode_class (acfg, klass->element_class);
7260 can_encode_method (MonoAotCompile *acfg, MonoMethod *method)
7262 if (method->wrapper_type) {
7263 switch (method->wrapper_type) {
7264 case MONO_WRAPPER_NONE:
7265 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
7266 case MONO_WRAPPER_XDOMAIN_INVOKE:
7267 case MONO_WRAPPER_STFLD:
7268 case MONO_WRAPPER_LDFLD:
7269 case MONO_WRAPPER_LDFLDA:
7270 case MONO_WRAPPER_STELEMREF:
7271 case MONO_WRAPPER_PROXY_ISINST:
7272 case MONO_WRAPPER_ALLOC:
7273 case MONO_WRAPPER_REMOTING_INVOKE:
7274 case MONO_WRAPPER_UNKNOWN:
7275 case MONO_WRAPPER_WRITE_BARRIER:
7276 case MONO_WRAPPER_DELEGATE_INVOKE:
7277 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
7278 case MONO_WRAPPER_DELEGATE_END_INVOKE:
7279 case MONO_WRAPPER_SYNCHRONIZED:
7281 case MONO_WRAPPER_MANAGED_TO_MANAGED:
7282 case MONO_WRAPPER_CASTCLASS: {
7283 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
7292 //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
7296 if (!method->token) {
7297 /* The method is part of a constructed type like Int[,].Set (). */
7298 if (!g_hash_table_lookup (acfg->token_info_hash, method)) {
7299 if (method->klass->rank)
7309 can_encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
7311 switch (patch_info->type) {
7312 case MONO_PATCH_INFO_METHOD:
7313 case MONO_PATCH_INFO_METHODCONST:
7314 case MONO_PATCH_INFO_METHOD_CODE_SLOT: {
7315 MonoMethod *method = patch_info->data.method;
7317 return can_encode_method (acfg, method);
7319 case MONO_PATCH_INFO_VTABLE:
7320 case MONO_PATCH_INFO_CLASS:
7321 case MONO_PATCH_INFO_IID:
7322 case MONO_PATCH_INFO_ADJUSTED_IID:
7323 if (!can_encode_class (acfg, patch_info->data.klass)) {
7324 //printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
7328 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE: {
7329 if (!can_encode_class (acfg, patch_info->data.del_tramp->klass)) {
7330 //printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
7335 case MONO_PATCH_INFO_RGCTX_FETCH:
7336 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX: {
7337 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
7339 if (!can_encode_method (acfg, entry->method))
7341 if (!can_encode_patch (acfg, entry->data))
7353 is_concrete_type (MonoType *t)
7358 if (t->type == MONO_TYPE_VAR || t->type == MONO_TYPE_MVAR)
7360 if (t->type == MONO_TYPE_GENERICINST) {
7361 MonoGenericContext *orig_ctx;
7362 MonoGenericInst *inst;
7365 if (!MONO_TYPE_ISSTRUCT (t))
7367 klass = mono_class_from_mono_type (t);
7368 orig_ctx = &mono_class_get_generic_class (klass)->context;
7370 inst = orig_ctx->class_inst;
7372 for (i = 0; i < inst->type_argc; ++i) {
7373 arg = mini_get_underlying_type (inst->type_argv [i]);
7374 if (!is_concrete_type (arg))
7378 inst = orig_ctx->method_inst;
7380 for (i = 0; i < inst->type_argc; ++i) {
7381 arg = mini_get_underlying_type (inst->type_argv [i]);
7382 if (!is_concrete_type (arg))
7390 /* LOCKING: Assumes the loader lock is held */
7392 add_gsharedvt_wrappers (MonoAotCompile *acfg, MonoMethodSignature *sig, gboolean gsharedvt_in, gboolean gsharedvt_out)
7394 MonoMethod *wrapper;
7395 gboolean concrete = TRUE;
7396 gboolean add_in = gsharedvt_in;
7397 gboolean add_out = gsharedvt_out;
7399 if (gsharedvt_in && g_hash_table_lookup (acfg->gsharedvt_in_signatures, sig))
7401 if (gsharedvt_out && g_hash_table_lookup (acfg->gsharedvt_out_signatures, sig))
7404 if (!add_in && !add_out)
7407 if (mini_is_gsharedvt_variable_signature (sig))
7411 g_hash_table_insert (acfg->gsharedvt_in_signatures, sig, sig);
7413 g_hash_table_insert (acfg->gsharedvt_out_signatures, sig, sig);
7415 if (!sig->has_type_parameters) {
7416 //printf ("%s\n", mono_signature_full_name (sig));
7419 wrapper = mini_get_gsharedvt_in_sig_wrapper (sig);
7420 add_extra_method (acfg, wrapper);
7422 if (gsharedvt_out) {
7423 wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
7424 add_extra_method (acfg, wrapper);
7427 /* For signatures creared during generic sharing, convert them to a concrete signature if possible */
7428 MonoMethodSignature *copy = mono_metadata_signature_dup (sig);
7431 //printf ("%s\n", mono_signature_full_name (sig));
7433 copy->ret = mini_get_underlying_type (sig->ret);
7434 if (!is_concrete_type (copy->ret))
7436 for (i = 0; i < sig->param_count; ++i) {
7437 copy->params [i] = mini_get_underlying_type (sig->params [i]);
7438 if (!is_concrete_type (copy->params [i]))
7442 copy->has_type_parameters = 0;
7445 wrapper = mini_get_gsharedvt_in_sig_wrapper (copy);
7446 add_extra_method (acfg, wrapper);
7449 if (gsharedvt_out) {
7450 wrapper = mini_get_gsharedvt_out_sig_wrapper (copy);
7451 add_extra_method (acfg, wrapper);
7454 //printf ("%s\n", mono_method_full_name (wrapper, 1));
7462 * AOT compile a given method.
7463 * This function might be called by multiple threads, so it must be thread-safe.
7466 compile_method (MonoAotCompile *acfg, MonoMethod *method)
7469 MonoJumpInfo *patch_info;
7472 MonoMethod *wrapped;
7476 if (acfg->aot_opts.metadata_only)
7479 mono_acfg_lock (acfg);
7480 index = get_method_index (acfg, method);
7481 mono_acfg_unlock (acfg);
7483 /* fixme: maybe we can also precompile wrapper methods */
7484 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
7485 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
7486 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
7487 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
7491 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7494 wrapped = mono_marshal_method_from_wrapper (method);
7495 if (wrapped && (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && wrapped->is_generic)
7496 // FIXME: The wrapper should be generic too, but it is not
7499 if (method->wrapper_type == MONO_WRAPPER_COMINTEROP)
7502 InterlockedIncrement (&acfg->stats.mcount);
7505 if (method->is_generic || mono_class_is_gtd (method->klass)) {
7506 InterlockedIncrement (&acfg->stats.genericcount);
7511 //acfg->aot_opts.print_skipped_methods = TRUE;
7514 * Since these methods are the only ones which are compiled with
7515 * AOT support, and they are not used by runtime startup/shutdown code,
7516 * the runtime will not see AOT methods during AOT compilation,so it
7517 * does not need to support them by creating a fake GOT etc.
7519 flags = JIT_FLAG_AOT;
7520 if (mono_aot_mode_is_full (&acfg->aot_opts))
7521 flags = (JitFlags)(flags | JIT_FLAG_FULL_AOT);
7523 flags = (JitFlags)(flags | JIT_FLAG_LLVM);
7524 if (acfg->aot_opts.llvm_only)
7525 flags = (JitFlags)(flags | JIT_FLAG_LLVM_ONLY | JIT_FLAG_EXPLICIT_NULL_CHECKS);
7526 if (acfg->aot_opts.no_direct_calls)
7527 flags = (JitFlags)(flags | JIT_FLAG_NO_DIRECT_ICALLS);
7528 if (acfg->aot_opts.direct_pinvoke)
7529 flags = (JitFlags)(flags | JIT_FLAG_DIRECT_PINVOKE);
7531 jit_timer = mono_time_track_start ();
7532 cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), flags, 0, index);
7533 mono_time_track_end (&mono_jit_stats.jit_time, jit_timer);
7535 if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
7536 if (acfg->aot_opts.print_skipped_methods)
7537 printf ("Skip (gshared failure): %s (%s)\n", mono_method_get_full_name (method), cfg->exception_message);
7538 InterlockedIncrement (&acfg->stats.genericcount);
7541 if (cfg->exception_type != MONO_EXCEPTION_NONE) {
7542 if (acfg->aot_opts.print_skipped_methods)
7543 printf ("Skip (JIT failure): %s\n", mono_method_get_full_name (method));
7544 /* Let the exception happen at runtime */
7548 if (cfg->disable_aot) {
7549 if (acfg->aot_opts.print_skipped_methods)
7550 printf ("Skip (disabled): %s\n", mono_method_get_full_name (method));
7551 InterlockedIncrement (&acfg->stats.ocount);
7554 cfg->method_index = index;
7556 /* Nullify patches which need no aot processing */
7557 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7558 switch (patch_info->type) {
7559 case MONO_PATCH_INFO_LABEL:
7560 case MONO_PATCH_INFO_BB:
7561 patch_info->type = MONO_PATCH_INFO_NONE;
7568 /* Collect method->token associations from the cfg */
7569 mono_acfg_lock (acfg);
7570 g_hash_table_foreach (cfg->token_info_hash, add_token_info_hash, acfg);
7571 mono_acfg_unlock (acfg);
7572 g_hash_table_destroy (cfg->token_info_hash);
7573 cfg->token_info_hash = NULL;
7576 * Check for absolute addresses.
7579 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7580 switch (patch_info->type) {
7581 case MONO_PATCH_INFO_ABS:
7582 /* unable to handle this */
7591 if (acfg->aot_opts.print_skipped_methods)
7592 printf ("Skip (abs call): %s\n", mono_method_get_full_name (method));
7593 InterlockedIncrement (&acfg->stats.abscount);
7597 /* Lock for the rest of the code */
7598 mono_acfg_lock (acfg);
7601 acfg->stats.method_categories [METHOD_CAT_GSHAREDVT] ++;
7602 else if (cfg->gshared)
7603 acfg->stats.method_categories [METHOD_CAT_INST] ++;
7604 else if (cfg->method->wrapper_type)
7605 acfg->stats.method_categories [METHOD_CAT_WRAPPER] ++;
7607 acfg->stats.method_categories [METHOD_CAT_NORMAL] ++;
7610 * Check for methods/klasses we can't encode.
7613 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7614 if (!can_encode_patch (acfg, patch_info))
7619 if (acfg->aot_opts.print_skipped_methods)
7620 printf ("Skip (patches): %s\n", mono_method_get_full_name (method));
7621 acfg->stats.ocount++;
7622 mono_acfg_unlock (acfg);
7626 if (!cfg->compile_llvm)
7627 acfg->has_jitted_code = TRUE;
7629 if (method->is_inflated && acfg->aot_opts.log_instances) {
7630 if (acfg->instances_logfile)
7631 fprintf (acfg->instances_logfile, "%s ### %d\n", mono_method_get_full_name (method), cfg->code_size);
7633 printf ("%s ### %d\n", mono_method_get_full_name (method), cfg->code_size);
7636 /* Adds generic instances referenced by this method */
7638 * The depth is used to avoid infinite loops when generic virtual recursion is
7641 depth = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_depth, method));
7642 if (!acfg->aot_opts.no_instances && depth < 32 && mono_aot_mode_is_full (&acfg->aot_opts)) {
7643 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7644 switch (patch_info->type) {
7645 case MONO_PATCH_INFO_RGCTX_FETCH:
7646 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX:
7647 case MONO_PATCH_INFO_METHOD: {
7648 MonoMethod *m = NULL;
7650 if (patch_info->type == MONO_PATCH_INFO_RGCTX_FETCH || patch_info->type == MONO_PATCH_INFO_RGCTX_SLOT_INDEX) {
7651 MonoJumpInfoRgctxEntry *e = patch_info->data.rgctx_entry;
7653 if (e->info_type == MONO_RGCTX_INFO_GENERIC_METHOD_CODE)
7654 m = e->data->data.method;
7656 m = patch_info->data.method;
7661 if (m->is_inflated && mono_aot_mode_is_full (&acfg->aot_opts)) {
7662 if (!(mono_class_generic_sharing_enabled (m->klass) &&
7663 mono_method_is_generic_sharable_full (m, FALSE, FALSE, FALSE)) &&
7664 (!method_has_type_vars (m) || mono_method_is_generic_sharable_full (m, TRUE, TRUE, FALSE))) {
7665 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
7666 if (mono_aot_mode_is_full (&acfg->aot_opts) && !method_has_type_vars (m))
7667 add_extra_method_with_depth (acfg, mono_marshal_get_native_wrapper (m, TRUE, TRUE), depth + 1);
7669 add_extra_method_with_depth (acfg, m, depth + 1);
7670 add_types_from_method_header (acfg, m);
7673 add_generic_class_with_depth (acfg, m->klass, depth + 5, "method");
7675 if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
7676 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
7678 if (info && info->subtype == WRAPPER_SUBTYPE_ELEMENT_ADDR)
7679 add_extra_method_with_depth (acfg, m, depth + 1);
7683 case MONO_PATCH_INFO_VTABLE: {
7684 MonoClass *klass = patch_info->data.klass;
7686 if (mono_class_is_ginst (klass) && !mini_class_is_generic_sharable (klass))
7687 add_generic_class_with_depth (acfg, klass, depth + 5, "vtable");
7690 case MONO_PATCH_INFO_SFLDA: {
7691 MonoClass *klass = patch_info->data.field->parent;
7693 /* The .cctor needs to run at runtime. */
7694 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))
7695 add_extra_method_with_depth (acfg, mono_class_get_cctor (klass), depth + 1);
7704 /* Determine whenever the method has GOT slots */
7705 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7706 switch (patch_info->type) {
7707 case MONO_PATCH_INFO_GOT_OFFSET:
7708 case MONO_PATCH_INFO_NONE:
7709 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
7710 case MONO_PATCH_INFO_GC_NURSERY_START:
7711 case MONO_PATCH_INFO_GC_NURSERY_BITS:
7713 case MONO_PATCH_INFO_IMAGE:
7714 /* The assembly is stored in GOT slot 0 */
7715 if (patch_info->data.image != acfg->image)
7716 cfg->has_got_slots = TRUE;
7719 if (!is_plt_patch (patch_info) || (cfg->compile_llvm && acfg->aot_opts.llvm_only))
7720 cfg->has_got_slots = TRUE;
7725 if (!cfg->has_got_slots)
7726 InterlockedIncrement (&acfg->stats.methods_without_got_slots);
7728 /* Add gsharedvt wrappers for signatures used by the method */
7729 if (acfg->aot_opts.llvm_only) {
7732 if (!cfg->method->wrapper_type || cfg->method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE)
7733 /* These only need out wrappers */
7734 add_gsharedvt_wrappers (acfg, mono_method_signature (cfg->method), FALSE, TRUE);
7736 for (l = cfg->signatures; l; l = l->next) {
7737 MonoMethodSignature *sig = mono_metadata_signature_dup ((MonoMethodSignature*)l->data);
7739 /* These only need in wrappers */
7740 add_gsharedvt_wrappers (acfg, sig, TRUE, FALSE);
7745 * FIXME: Instead of this mess, allocate the patches from the aot mempool.
7747 /* Make a copy of the patch info which is in the mempool */
7749 MonoJumpInfo *patches = NULL, *patches_end = NULL;
7751 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7752 MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
7755 patches = new_patch_info;
7757 patches_end->next = new_patch_info;
7758 patches_end = new_patch_info;
7760 cfg->patch_info = patches;
7762 /* Make a copy of the unwind info */
7764 GSList *l, *unwind_ops;
7768 for (l = cfg->unwind_ops; l; l = l->next) {
7769 op = (MonoUnwindOp *)mono_mempool_alloc (acfg->mempool, sizeof (MonoUnwindOp));
7770 memcpy (op, l->data, sizeof (MonoUnwindOp));
7771 unwind_ops = g_slist_prepend_mempool (acfg->mempool, unwind_ops, op);
7773 cfg->unwind_ops = g_slist_reverse (unwind_ops);
7775 /* Make a copy of the argument/local info */
7778 MonoInst **args, **locals;
7779 MonoMethodSignature *sig;
7780 MonoMethodHeader *header;
7783 sig = mono_method_signature (method);
7784 args = (MonoInst **)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * (sig->param_count + sig->hasthis));
7785 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
7786 args [i] = (MonoInst *)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
7787 memcpy (args [i], cfg->args [i], sizeof (MonoInst));
7791 header = mono_method_get_header_checked (method, &error);
7792 mono_error_assert_ok (&error); /* FIXME don't swallow the error */
7793 locals = (MonoInst **)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * header->num_locals);
7794 for (i = 0; i < header->num_locals; ++i) {
7795 locals [i] = (MonoInst *)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
7796 memcpy (locals [i], cfg->locals [i], sizeof (MonoInst));
7798 mono_metadata_free_mh (header);
7799 cfg->locals = locals;
7802 /* Free some fields used by cfg to conserve memory */
7803 mono_empty_compile (cfg);
7805 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
7807 while (index >= acfg->cfgs_size) {
7808 MonoCompile **new_cfgs;
7811 new_size = acfg->cfgs_size * 2;
7812 new_cfgs = g_new0 (MonoCompile*, new_size);
7813 memcpy (new_cfgs, acfg->cfgs, sizeof (MonoCompile*) * acfg->cfgs_size);
7814 g_free (acfg->cfgs);
7815 acfg->cfgs = new_cfgs;
7816 acfg->cfgs_size = new_size;
7818 acfg->cfgs [index] = cfg;
7820 g_hash_table_insert (acfg->method_to_cfg, cfg->orig_method, cfg);
7822 mono_update_jit_stats (cfg);
7825 if (cfg->orig_method->wrapper_type)
7826 g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
7829 mono_acfg_unlock (acfg);
7831 InterlockedIncrement (&acfg->stats.ccount);
7834 static mono_thread_start_return_t WINAPI
7835 compile_thread_main (gpointer user_data)
7837 MonoDomain *domain = ((MonoDomain **)user_data) [0];
7838 MonoAotCompile *acfg = ((MonoAotCompile **)user_data) [1];
7839 GPtrArray *methods = ((GPtrArray **)user_data) [2];
7843 MonoThread *thread = mono_thread_attach (domain);
7844 mono_thread_set_name_internal (thread->internal_thread, mono_string_new (mono_get_root_domain (), "AOT compiler"), TRUE, &error);
7845 mono_error_assert_ok (&error);
7847 for (i = 0; i < methods->len; ++i)
7848 compile_method (acfg, (MonoMethod *)g_ptr_array_index (methods, i));
7854 load_profile_files (MonoAotCompile *acfg)
7858 int file_index, res, method_index, i;
7861 GList *unordered, *l;
7866 tmp = g_strdup_printf ("%s/.mono/aot-profile-data/%s-%d", g_get_home_dir (), acfg->image->assembly_name, file_index);
7868 if (!g_file_test (tmp, G_FILE_TEST_IS_REGULAR)) {
7873 infile = fopen (tmp, "r");
7876 printf ("Using profile data file '%s'\n", tmp);
7881 res = fscanf (infile, "%32s\n", ver);
7882 if ((res != 1) || strcmp (ver, "#VER:2") != 0) {
7883 printf ("Profile file has wrong version or invalid.\n");
7890 MonoMethodDesc *desc;
7893 if (fgets (name, 1023, infile) == NULL)
7896 /* Kill the newline */
7897 if (strlen (name) > 0)
7898 name [strlen (name) - 1] = '\0';
7900 desc = mono_method_desc_new (name, TRUE);
7902 method = mono_method_desc_search_in_image (desc, acfg->image);
7904 if (method && mono_method_get_token (method)) {
7905 token = mono_method_get_token (method);
7906 method_index = mono_metadata_token_index (token) - 1;
7909 for (i = 0; i < acfg->method_order->len; ++i) {
7910 if (g_ptr_array_index (acfg->method_order, i) == GUINT_TO_POINTER (method_index)) {
7916 g_ptr_array_add (acfg->method_order, GUINT_TO_POINTER (method_index));
7918 //printf ("No method found matching '%s'.\n", name);
7924 /* Add missing methods */
7926 for (method_index = 0; method_index < acfg->image->tables [MONO_TABLE_METHOD].rows; ++method_index) {
7928 for (i = 0; i < acfg->method_order->len; ++i) {
7929 if (g_ptr_array_index (acfg->method_order, i) == GUINT_TO_POINTER (method_index)) {
7935 unordered = g_list_prepend (unordered, GUINT_TO_POINTER (method_index));
7937 unordered = g_list_reverse (unordered);
7938 for (l = unordered; l; l = l->next)
7939 g_ptr_array_add (acfg->method_order, l->data);
7942 /* Used by the LLVM backend */
7944 mono_aot_get_got_offset (MonoJumpInfo *ji)
7946 return get_got_offset (llvm_acfg, TRUE, ji);
7950 * mono_aot_is_shared_got_offset:
7952 * Return whenever OFFSET refers to a GOT slot which is preinitialized
7953 * when the AOT image is loaded.
7956 mono_aot_is_shared_got_offset (int offset)
7958 return offset < llvm_acfg->nshared_got_entries;
7962 mono_aot_get_method_name (MonoCompile *cfg)
7964 if (llvm_acfg->aot_opts.static_link)
7965 /* Include the assembly name too to avoid duplicate symbol errors */
7966 return g_strdup_printf ("%s_%s", llvm_acfg->assembly_name_sym, get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash));
7968 return get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash);
7972 * mono_aot_is_linkonce_method:
7974 * Return whenever METHOD should be emitted with linkonce linkage,
7975 * eliminating duplicate copies when compiling in static mode.
7978 mono_aot_is_linkonce_method (MonoMethod *method)
7984 // FIXME: Add more cases
7985 if (method->wrapper_type != MONO_WRAPPER_UNKNOWN)
7987 info = mono_marshal_get_wrapper_info (method);
7988 if ((info && (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG || info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG)))
7995 append_mangled_type (GString *s, MonoType *t)
7998 g_string_append_printf (s, "b");
8000 case MONO_TYPE_VOID:
8001 g_string_append_printf (s, "void_");
8004 g_string_append_printf (s, "i1");
8007 g_string_append_printf (s, "u1");
8010 g_string_append_printf (s, "i2");
8013 g_string_append_printf (s, "u2");
8016 g_string_append_printf (s, "i4");
8019 g_string_append_printf (s, "u4");
8022 g_string_append_printf (s, "i8");
8025 g_string_append_printf (s, "u8");
8028 g_string_append_printf (s, "ii");
8031 g_string_append_printf (s, "ui");
8034 g_string_append_printf (s, "fl");
8037 g_string_append_printf (s, "do");
8040 char *fullname = mono_type_full_name (t);
8046 * Have to create a mangled name which is:
8050 temp = g_string_new ("");
8051 len = strlen (fullname);
8052 for (i = 0; i < len; ++i) {
8053 char c = fullname [i];
8055 g_string_append_c (temp, c);
8056 } else if (c == '_') {
8057 g_string_append_c (temp, '_');
8058 g_string_append_c (temp, '_');
8060 g_string_append_c (temp, '_');
8061 g_string_append_printf (temp, "%x", (int)c);
8064 temps = g_string_free (temp, FALSE);
8065 /* Include the length to avoid different length type names aliasing each other */
8066 g_string_append_printf (s, "cl%x_%s_", strlen (temps), temps);
8075 append_mangled_signature (GString *s, MonoMethodSignature *sig)
8080 supported = append_mangled_type (s, sig->ret);
8084 g_string_append_printf (s, "this_");
8085 for (i = 0; i < sig->param_count; ++i) {
8086 supported = append_mangled_type (s, sig->params [i]);
8095 * mono_aot_get_mangled_method_name:
8097 * Return a unique mangled name for METHOD, or NULL.
8100 mono_aot_get_mangled_method_name (MonoMethod *method)
8106 // FIXME: Add more cases
8107 if (method->wrapper_type != MONO_WRAPPER_UNKNOWN)
8109 info = mono_marshal_get_wrapper_info (method);
8110 if (!(info && (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG || info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG)))
8113 s = g_string_new ("");
8115 g_string_append_printf (s, "aot_method_w_");
8117 switch (info->subtype) {
8118 case WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG:
8119 g_string_append_printf (s, "gsharedvt_in_");
8121 case WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG:
8122 g_string_append_printf (s, "gsharedvt_out_");
8125 g_assert_not_reached ();
8129 supported = append_mangled_signature (s, info->d.gsharedvt.sig);
8131 g_string_free (s, TRUE);
8135 return g_string_free (s, FALSE);
8139 mono_aot_is_direct_callable (MonoJumpInfo *patch_info)
8141 return is_direct_callable (llvm_acfg, NULL, patch_info);
8145 mono_aot_mark_unused_llvm_plt_entry (MonoJumpInfo *patch_info)
8147 MonoPltEntry *plt_entry;
8149 plt_entry = get_plt_entry (llvm_acfg, patch_info);
8150 plt_entry->llvm_used = FALSE;
8154 mono_aot_get_direct_call_symbol (MonoJumpInfoType type, gconstpointer data)
8156 const char *sym = NULL;
8158 if (llvm_acfg->aot_opts.direct_icalls) {
8159 if (type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8160 /* Call to a C function implementing a jit icall */
8161 sym = mono_lookup_jit_icall_symbol ((const char *)data);
8162 } else if (type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
8163 MonoMethod *method = (MonoMethod *)data;
8164 if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
8165 sym = mono_lookup_icall_symbol (method);
8166 else if (llvm_acfg->aot_opts.direct_pinvoke)
8167 sym = get_pinvoke_import (llvm_acfg, method);
8170 return g_strdup (sym);
8176 mono_aot_get_plt_symbol (MonoJumpInfoType type, gconstpointer data)
8178 MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (llvm_acfg->mempool, sizeof (MonoJumpInfo));
8179 MonoPltEntry *plt_entry;
8180 const char *sym = NULL;
8183 ji->data.target = data;
8185 if (!can_encode_patch (llvm_acfg, ji))
8188 if (llvm_acfg->aot_opts.direct_icalls) {
8189 if (type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8190 /* Call to a C function implementing a jit icall */
8191 sym = mono_lookup_jit_icall_symbol ((const char *)data);
8192 } else if (type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
8193 MonoMethod *method = (MonoMethod *)data;
8194 if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
8195 sym = mono_lookup_icall_symbol (method);
8198 return g_strdup (sym);
8201 plt_entry = get_plt_entry (llvm_acfg, ji);
8202 plt_entry->llvm_used = TRUE;
8204 #if defined(TARGET_MACH)
8205 return g_strdup_printf (plt_entry->llvm_symbol + strlen (llvm_acfg->llvm_label_prefix));
8207 return g_strdup_printf (plt_entry->llvm_symbol);
8212 mono_aot_get_method_index (MonoMethod *method)
8214 g_assert (llvm_acfg);
8215 return get_method_index (llvm_acfg, method);
8219 mono_aot_patch_info_dup (MonoJumpInfo* ji)
8223 mono_acfg_lock (llvm_acfg);
8224 res = mono_patch_info_dup_mp (llvm_acfg->mempool, ji);
8225 mono_acfg_unlock (llvm_acfg);
8231 execute_system (const char * command)
8236 // We need an extra set of quotes around the whole command to properly handle commands
8237 // with spaces since internally the command is called through "cmd /c.
8238 char * quoted_command = g_strdup_printf ("\"%s\"", command);
8240 int size = MultiByteToWideChar (CP_UTF8, 0 , quoted_command , -1, NULL , 0);
8241 wchar_t* wstr = g_malloc (sizeof (wchar_t) * size);
8242 MultiByteToWideChar (CP_UTF8, 0, quoted_command, -1, wstr , size);
8243 status = _wsystem (wstr);
8246 g_free (quoted_command);
8247 #elif defined (HAVE_SYSTEM)
8248 status = system (command);
8250 g_assert_not_reached ();
8261 * Emit the LLVM code into an LLVM bytecode file, and compile it using the LLVM
8265 emit_llvm_file (MonoAotCompile *acfg)
8267 char *command, *opts, *tempbc, *optbc, *output_fname;
8269 if (acfg->aot_opts.llvm_only && acfg->aot_opts.asm_only) {
8270 tempbc = g_strdup_printf ("%s.bc", acfg->tmpbasename);
8271 optbc = g_strdup (acfg->aot_opts.llvm_outfile);
8273 tempbc = g_strdup_printf ("%s.bc", acfg->tmpbasename);
8274 optbc = g_strdup_printf ("%s.opt.bc", acfg->tmpbasename);
8277 mono_llvm_emit_aot_module (tempbc, g_path_get_basename (acfg->image->name));
8280 * FIXME: Experiment with adding optimizations, the -std-compile-opts set takes
8281 * a lot of time, and doesn't seem to save much space.
8282 * The following optimizations cannot be enabled:
8284 * - 'jump-threading' changes our blockaddress references to int constants.
8285 * - 'basiccg' fails because it contains:
8286 * if (CS && !isa<IntrinsicInst>(II)) {
8287 * and isa<IntrinsicInst> is false for invokes to intrinsics (iltests.exe).
8288 * - 'prune-eh' and 'functionattrs' depend on 'basiccg'.
8289 * The opt list below was produced by taking the output of:
8290 * llvm-as < /dev/null | opt -O2 -disable-output -debug-pass=Arguments
8291 * then removing tailcallelim + the global opts.
8292 * strip-dead-prototypes deletes unused intrinsics definitions.
8294 /* The dse pass is disabled because of #13734 and #17616 */
8296 * The dse bug is in DeadStoreElimination.cpp:isOverwrite ():
8297 * // If we have no DataLayout information around, then the size of the store
8298 * // is inferrable from the pointee type. If they are the same type, then
8299 * // we know that the store is safe.
8300 * if (AA.getDataLayout() == 0 &&
8301 * Later.Ptr->getType() == Earlier.Ptr->getType()) {
8302 * return OverwriteComplete;
8303 * Here, if 'Earlier' refers to a memset, and Later has no size info, it mistakenly thinks the memset is redundant.
8305 if (acfg->aot_opts.llvm_only)
8306 // FIXME: This doesn't work yet
8307 opts = g_strdup ("");
8309 #if LLVM_API_VERSION > 100
8310 opts = g_strdup ("-O2 -disable-tail-calls");
8312 opts = g_strdup ("-targetlibinfo -no-aa -basicaa -notti -instcombine -simplifycfg -inline-cost -inline -sroa -domtree -early-cse -lazy-value-info -correlated-propagation -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loop-simplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loop-simplify -lcssa -indvars -loop-idiom -loop-deletion -loop-unroll -memdep -gvn -memdep -memcpyopt -sccp -instcombine -lazy-value-info -correlated-propagation -domtree -memdep -adce -simplifycfg -instcombine -strip-dead-prototypes -domtree -verify");
8314 command = g_strdup_printf ("\"%sopt\" -f %s -o \"%s\" \"%s\"", acfg->aot_opts.llvm_path, opts, optbc, tempbc);
8315 aot_printf (acfg, "Executing opt: %s\n", command);
8316 if (execute_system (command) != 0)
8320 if (acfg->aot_opts.llvm_only && acfg->aot_opts.asm_only)
8321 /* Nothing else to do */
8324 if (acfg->aot_opts.llvm_only) {
8325 /* Use the stock clang from xcode */
8327 command = g_strdup_printf ("clang++ -fexceptions -march=x86-64 -fpic -msse -msse2 -msse3 -msse4 -O2 -fno-optimize-sibling-calls -Wno-override-module -c -o \"%s\" \"%s.opt.bc\"", acfg->llvm_ofile, acfg->tmpbasename);
8329 aot_printf (acfg, "Executing clang: %s\n", command);
8330 if (execute_system (command) != 0)
8335 if (!acfg->llc_args)
8336 acfg->llc_args = g_string_new ("");
8338 /* Verbose asm slows down llc greatly */
8339 g_string_append (acfg->llc_args, " -asm-verbose=false");
8341 if (acfg->aot_opts.mtriple)
8342 g_string_append_printf (acfg->llc_args, " -mtriple=%s", acfg->aot_opts.mtriple);
8344 g_string_append (acfg->llc_args, " -disable-gnu-eh-frame -enable-mono-eh-frame");
8346 g_string_append_printf (acfg->llc_args, " -mono-eh-frame-symbol=%s%s", acfg->user_symbol_prefix, acfg->llvm_eh_frame_symbol);
8348 #if LLVM_API_VERSION > 100
8349 g_string_append_printf (acfg->llc_args, " -disable-tail-calls");
8352 #if defined(TARGET_MACH) && defined(TARGET_ARM)
8353 /* ios requires PIC code now */
8354 g_string_append_printf (acfg->llc_args, " -relocation-model=pic");
8356 if (llvm_acfg->aot_opts.static_link)
8357 g_string_append_printf (acfg->llc_args, " -relocation-model=static");
8359 g_string_append_printf (acfg->llc_args, " -relocation-model=pic");
8362 if (acfg->llvm_owriter) {
8363 /* Emit an object file directly */
8364 output_fname = g_strdup_printf ("%s", acfg->llvm_ofile);
8365 g_string_append_printf (acfg->llc_args, " -filetype=obj");
8367 output_fname = g_strdup_printf ("%s", acfg->llvm_sfile);
8369 command = g_strdup_printf ("\"%sllc\" %s -o \"%s\" \"%s.opt.bc\"", acfg->aot_opts.llvm_path, acfg->llc_args->str, output_fname, acfg->tmpbasename);
8370 g_free (output_fname);
8372 aot_printf (acfg, "Executing llc: %s\n", command);
8374 if (execute_system (command) != 0)
8381 emit_code (MonoAotCompile *acfg)
8383 int oindex, i, prev_index;
8384 gboolean saved_unbox_info = FALSE;
8385 char symbol [MAX_SYMBOL_SIZE];
8387 if (acfg->aot_opts.llvm_only)
8390 #if defined(TARGET_POWERPC64)
8391 sprintf (symbol, ".Lgot_addr");
8392 emit_section_change (acfg, ".text", 0);
8393 emit_alignment (acfg, 8);
8394 emit_label (acfg, symbol);
8395 emit_pointer (acfg, acfg->got_symbol);
8399 * This global symbol is used to compute the address of each method using the
8400 * code_offsets array. It is also used to compute the memory ranges occupied by
8401 * AOT code, so it must be equal to the address of the first emitted method.
8403 emit_section_change (acfg, ".text", 0);
8404 emit_alignment_code (acfg, 8);
8405 emit_info_symbol (acfg, "jit_code_start");
8408 * Emit some padding so the local symbol for the first method doesn't have the
8409 * same address as 'methods'.
8411 emit_padding (acfg, 16);
8413 for (oindex = 0; oindex < acfg->method_order->len; ++oindex) {
8417 i = GPOINTER_TO_UINT (g_ptr_array_index (acfg->method_order, oindex));
8419 cfg = acfg->cfgs [i];
8424 method = cfg->orig_method;
8426 /* Emit unbox trampoline */
8427 if (mono_aot_mode_is_full (&acfg->aot_opts) && cfg->orig_method->klass->valuetype) {
8428 sprintf (symbol, "ut_%d", get_method_index (acfg, method));
8430 emit_section_change (acfg, ".text", 0);
8432 if (acfg->thumb_mixed && cfg->compile_llvm) {
8433 emit_set_thumb_mode (acfg);
8434 fprintf (acfg->fp, "\n.thumb_func\n");
8437 emit_label (acfg, symbol);
8439 arch_emit_unbox_trampoline (acfg, cfg, cfg->orig_method, cfg->asm_symbol);
8441 if (acfg->thumb_mixed && cfg->compile_llvm)
8442 emit_set_arm_mode (acfg);
8444 if (!saved_unbox_info) {
8445 char user_symbol [128];
8447 sprintf (user_symbol, "%sunbox_trampoline_p", acfg->user_symbol_prefix);
8449 emit_label (acfg, "ut_end");
8451 unwind_ops = mono_unwind_get_cie_program ();
8452 save_unwind_info (acfg, user_symbol, unwind_ops);
8453 mono_free_unwind_info (unwind_ops);
8455 /* Save the unbox trampoline size */
8456 emit_symbol_diff (acfg, "ut_end", symbol, 0);
8458 saved_unbox_info = TRUE;
8462 if (cfg->compile_llvm)
8463 acfg->stats.llvm_count ++;
8465 emit_method_code (acfg, cfg);
8468 emit_section_change (acfg, ".text", 0);
8469 emit_alignment_code (acfg, 8);
8470 emit_info_symbol (acfg, "jit_code_end");
8472 /* To distinguish it from the next symbol */
8473 emit_padding (acfg, 4);
8476 * Add .no_dead_strip directives for all LLVM methods to prevent the OSX linker
8477 * from optimizing them away, since it doesn't see that code_offsets references them.
8478 * JITted methods don't need this since they are referenced using assembler local
8480 * FIXME: This is why write-symbols doesn't work on OSX ?
8482 if (acfg->llvm && acfg->need_no_dead_strip) {
8483 fprintf (acfg->fp, "\n");
8484 for (i = 0; i < acfg->nmethods; ++i) {
8485 if (acfg->cfgs [i] && acfg->cfgs [i]->compile_llvm)
8486 fprintf (acfg->fp, ".no_dead_strip %s\n", acfg->cfgs [i]->asm_symbol);
8491 * To work around linker issues, we emit a table of branches, and disassemble them at runtime.
8492 * This is PIE code, and the linker can update it if needed.
8495 sprintf (symbol, "method_addresses");
8496 emit_section_change (acfg, ".text", 1);
8497 emit_alignment_code (acfg, 8);
8498 emit_info_symbol (acfg, symbol);
8499 if (acfg->aot_opts.write_symbols)
8500 emit_local_symbol (acfg, symbol, "method_addresses_end", TRUE);
8501 emit_unset_mode (acfg);
8502 if (acfg->need_no_dead_strip)
8503 fprintf (acfg->fp, " .no_dead_strip %s\n", symbol);
8505 for (i = 0; i < acfg->nmethods; ++i) {
8506 #ifdef MONO_ARCH_AOT_SUPPORTED
8509 if (acfg->cfgs [i]) {
8510 arch_emit_direct_call (acfg, acfg->cfgs [i]->asm_symbol, FALSE, acfg->thumb_mixed && acfg->cfgs [i]->compile_llvm, NULL, &call_size);
8512 arch_emit_direct_call (acfg, symbol, FALSE, FALSE, NULL, &call_size);
8517 sprintf (symbol, "method_addresses_end");
8518 emit_label (acfg, symbol);
8521 /* Emit a sorted table mapping methods to the index of their unbox trampolines */
8522 sprintf (symbol, "unbox_trampolines");
8523 emit_section_change (acfg, RODATA_SECT, 0);
8524 emit_alignment (acfg, 8);
8525 emit_info_symbol (acfg, symbol);
8528 for (i = 0; i < acfg->nmethods; ++i) {
8533 cfg = acfg->cfgs [i];
8537 method = cfg->orig_method;
8539 if (mono_aot_mode_is_full (&acfg->aot_opts) && cfg->orig_method->klass->valuetype) {
8540 index = get_method_index (acfg, method);
8542 emit_int32 (acfg, index);
8543 /* Make sure the table is sorted by index */
8544 g_assert (index > prev_index);
8548 sprintf (symbol, "unbox_trampolines_end");
8549 emit_info_symbol (acfg, symbol);
8550 emit_int32 (acfg, 0);
8552 /* Emit a separate table with the trampoline addresses/offsets */
8553 sprintf (symbol, "unbox_trampoline_addresses");
8554 emit_section_change (acfg, ".text", 0);
8555 emit_alignment_code (acfg, 8);
8556 emit_info_symbol (acfg, symbol);
8558 for (i = 0; i < acfg->nmethods; ++i) {
8563 cfg = acfg->cfgs [i];
8567 method = cfg->orig_method;
8569 if (mono_aot_mode_is_full (&acfg->aot_opts) && cfg->orig_method->klass->valuetype) {
8570 #ifdef MONO_ARCH_AOT_SUPPORTED
8573 index = get_method_index (acfg, method);
8574 sprintf (symbol, "ut_%d", index);
8576 arch_emit_direct_call (acfg, symbol, FALSE, acfg->thumb_mixed && cfg->compile_llvm, NULL, &call_size);
8580 emit_int32 (acfg, 0);
8584 emit_info (MonoAotCompile *acfg)
8589 offsets = g_new0 (gint32, acfg->nmethods);
8591 for (oindex = 0; oindex < acfg->method_order->len; ++oindex) {
8592 i = GPOINTER_TO_UINT (g_ptr_array_index (acfg->method_order, oindex));
8594 if (acfg->cfgs [i]) {
8595 emit_method_info (acfg, acfg->cfgs [i]);
8596 offsets [i] = acfg->cfgs [i]->method_info_offset;
8602 acfg->stats.offsets_size += emit_offset_table (acfg, "method_info_offsets", MONO_AOT_TABLE_METHOD_INFO_OFFSETS, acfg->nmethods, 10, offsets);
8607 #endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
8609 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
8610 #define mix(a,b,c) { \
8611 a -= c; a ^= rot(c, 4); c += b; \
8612 b -= a; b ^= rot(a, 6); a += c; \
8613 c -= b; c ^= rot(b, 8); b += a; \
8614 a -= c; a ^= rot(c,16); c += b; \
8615 b -= a; b ^= rot(a,19); a += c; \
8616 c -= b; c ^= rot(b, 4); b += a; \
8618 #define final(a,b,c) { \
8619 c ^= b; c -= rot(b,14); \
8620 a ^= c; a -= rot(c,11); \
8621 b ^= a; b -= rot(a,25); \
8622 c ^= b; c -= rot(b,16); \
8623 a ^= c; a -= rot(c,4); \
8624 b ^= a; b -= rot(a,14); \
8625 c ^= b; c -= rot(b,24); \
8629 mono_aot_type_hash (MonoType *t1)
8631 guint hash = t1->type;
8633 hash |= t1->byref << 6; /* do not collide with t1->type values */
8635 case MONO_TYPE_VALUETYPE:
8636 case MONO_TYPE_CLASS:
8637 case MONO_TYPE_SZARRAY:
8638 /* check if the distribution is good enough */
8639 return ((hash << 5) - hash) ^ mono_metadata_str_hash (t1->data.klass->name);
8641 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
8642 case MONO_TYPE_ARRAY:
8643 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
8644 case MONO_TYPE_GENERICINST:
8645 return ((hash << 5) - hash) ^ 0;
8652 * mono_aot_method_hash:
8654 * Return a hash code for methods which only depends on metadata.
8657 mono_aot_method_hash (MonoMethod *method)
8659 MonoMethodSignature *sig;
8663 guint32 *hashes_start, *hashes;
8665 MonoGenericInst *class_ginst = NULL;
8666 MonoGenericInst *ginst = NULL;
8668 /* Similar to the hash in mono_method_get_imt_slot () */
8670 sig = mono_method_signature (method);
8672 if (mono_class_is_ginst (method->klass))
8673 class_ginst = mono_class_get_generic_class (method->klass)->context.class_inst;
8674 if (method->is_inflated)
8675 ginst = ((MonoMethodInflated*)method)->context.method_inst;
8677 hashes_count = sig->param_count + 5 + (class_ginst ? class_ginst->type_argc : 0) + (ginst ? ginst->type_argc : 0);
8678 hashes_start = (guint32 *)g_malloc0 (hashes_count * sizeof (guint32));
8679 hashes = hashes_start;
8681 /* Some wrappers are assigned to random classes */
8682 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
8683 klass = method->klass;
8685 klass = mono_defaults.object_class;
8687 if (!method->wrapper_type) {
8690 if (mono_class_is_ginst (klass))
8691 full_name = mono_type_full_name (&mono_class_get_generic_class (klass)->container_class->byval_arg);
8693 full_name = mono_type_full_name (&klass->byval_arg);
8695 hashes [0] = mono_metadata_str_hash (full_name);
8699 hashes [0] = mono_metadata_str_hash (klass->name);
8700 hashes [1] = mono_metadata_str_hash (klass->name_space);
8702 if (method->wrapper_type == MONO_WRAPPER_STFLD || method->wrapper_type == MONO_WRAPPER_LDFLD || method->wrapper_type == MONO_WRAPPER_LDFLDA)
8703 /* The method name includes a stringified pointer */
8706 hashes [2] = mono_metadata_str_hash (method->name);
8707 hashes [3] = method->wrapper_type;
8708 hashes [4] = mono_aot_type_hash (sig->ret);
8710 for (i = 0; i < sig->param_count; i++) {
8711 hashes [hindex ++] = mono_aot_type_hash (sig->params [i]);
8714 for (i = 0; i < class_ginst->type_argc; ++i)
8715 hashes [hindex ++] = mono_aot_type_hash (class_ginst->type_argv [i]);
8718 for (i = 0; i < ginst->type_argc; ++i)
8719 hashes [hindex ++] = mono_aot_type_hash (ginst->type_argv [i]);
8721 g_assert (hindex == hashes_count);
8723 /* Setup internal state */
8724 a = b = c = 0xdeadbeef + (((guint32)hashes_count)<<2);
8726 /* Handle most of the hashes */
8727 while (hashes_count > 3) {
8736 /* Handle the last 3 hashes (all the case statements fall through) */
8737 switch (hashes_count) {
8738 case 3 : c += hashes [2];
8739 case 2 : b += hashes [1];
8740 case 1 : a += hashes [0];
8742 case 0: /* nothing left to add */
8746 g_free (hashes_start);
8755 * mono_aot_get_array_helper_from_wrapper;
8757 * Get the helper method in Array called by an array wrapper method.
8760 mono_aot_get_array_helper_from_wrapper (MonoMethod *method)
8764 MonoGenericContext ctx;
8765 MonoType *args [16];
8766 char *mname, *iname, *s, *s2, *helper_name = NULL;
8768 prefix = "System.Collections.Generic";
8769 s = g_strdup_printf ("%s", method->name + strlen (prefix) + 1);
8770 s2 = strstr (s, "`1.");
8776 //printf ("X: %s %s\n", iname, mname);
8778 if (!strcmp (iname, "IList"))
8779 helper_name = g_strdup_printf ("InternalArray__%s", mname);
8781 helper_name = g_strdup_printf ("InternalArray__%s_%s", iname, mname);
8782 m = mono_class_get_method_from_name (mono_defaults.array_class, helper_name, mono_method_signature (method)->param_count);
8784 g_free (helper_name);
8787 if (m->is_generic) {
8789 memset (&ctx, 0, sizeof (ctx));
8790 args [0] = &method->klass->element_class->byval_arg;
8791 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
8792 m = mono_class_inflate_generic_method_checked (m, &ctx, &error);
8793 g_assert (mono_error_ok (&error)); /* FIXME don't swallow the error */
8799 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
8801 typedef struct HashEntry {
8802 guint32 key, value, index;
8803 struct HashEntry *next;
8807 * emit_extra_methods:
8809 * Emit methods which are not in the METHOD table, like wrappers.
8812 emit_extra_methods (MonoAotCompile *acfg)
8814 int i, table_size, buf_size;
8816 guint32 *info_offsets;
8819 HashEntry *entry, *new_entry;
8820 int nmethods, max_chain_length;
8823 info_offsets = g_new0 (guint32, acfg->extra_methods->len);
8825 /* Emit method info */
8827 for (i = 0; i < acfg->extra_methods->len; ++i) {
8828 MonoMethod *method = (MonoMethod *)g_ptr_array_index (acfg->extra_methods, i);
8829 MonoCompile *cfg = (MonoCompile *)g_hash_table_lookup (acfg->method_to_cfg, method);
8835 p = buf = (guint8 *)g_malloc (buf_size);
8839 method = cfg->method_to_register;
8841 encode_method_ref (acfg, method, p, &p);
8843 g_assert ((p - buf) < buf_size);
8845 info_offsets [i] = add_to_blob (acfg, buf, p - buf);
8850 * Construct a chained hash table for mapping indexes in extra_method_info to
8853 table_size = g_spaced_primes_closest ((int)(nmethods * 1.5));
8854 table = g_ptr_array_sized_new (table_size);
8855 for (i = 0; i < table_size; ++i)
8856 g_ptr_array_add (table, NULL);
8857 chain_lengths = g_new0 (int, table_size);
8858 max_chain_length = 0;
8859 for (i = 0; i < acfg->extra_methods->len; ++i) {
8860 MonoMethod *method = (MonoMethod *)g_ptr_array_index (acfg->extra_methods, i);
8861 MonoCompile *cfg = (MonoCompile *)g_hash_table_lookup (acfg->method_to_cfg, method);
8867 key = info_offsets [i];
8868 value = get_method_index (acfg, method);
8870 hash = mono_aot_method_hash (method) % table_size;
8871 //printf ("X: %s %x\n", mono_method_get_full_name (method), mono_aot_method_hash (method));
8873 chain_lengths [hash] ++;
8874 max_chain_length = MAX (max_chain_length, chain_lengths [hash]);
8876 new_entry = (HashEntry *)mono_mempool_alloc0 (acfg->mempool, sizeof (HashEntry));
8877 new_entry->key = key;
8878 new_entry->value = value;
8880 entry = (HashEntry *)g_ptr_array_index (table, hash);
8881 if (entry == NULL) {
8882 new_entry->index = hash;
8883 g_ptr_array_index (table, hash) = new_entry;
8886 entry = entry->next;
8888 entry->next = new_entry;
8889 new_entry->index = table->len;
8890 g_ptr_array_add (table, new_entry);
8893 g_free (chain_lengths);
8895 //printf ("MAX: %d\n", max_chain_length);
8897 buf_size = table->len * 12 + 4;
8898 p = buf = (guint8 *)g_malloc (buf_size);
8899 encode_int (table_size, p, &p);
8901 for (i = 0; i < table->len; ++i) {
8902 HashEntry *entry = (HashEntry *)g_ptr_array_index (table, i);
8904 if (entry == NULL) {
8905 encode_int (0, p, &p);
8906 encode_int (0, p, &p);
8907 encode_int (0, p, &p);
8909 //g_assert (entry->key > 0);
8910 encode_int (entry->key, p, &p);
8911 encode_int (entry->value, p, &p);
8913 encode_int (entry->next->index, p, &p);
8915 encode_int (0, p, &p);
8918 g_assert (p - buf <= buf_size);
8920 /* Emit the table */
8921 emit_aot_data (acfg, MONO_AOT_TABLE_EXTRA_METHOD_TABLE, "extra_method_table", buf, p - buf);
8926 * Emit a table reverse mapping method indexes to their index in extra_method_info.
8927 * This is used by mono_aot_find_jit_info ().
8929 buf_size = acfg->extra_methods->len * 8 + 4;
8930 p = buf = (guint8 *)g_malloc (buf_size);
8931 encode_int (acfg->extra_methods->len, p, &p);
8932 for (i = 0; i < acfg->extra_methods->len; ++i) {
8933 MonoMethod *method = (MonoMethod *)g_ptr_array_index (acfg->extra_methods, i);
8935 encode_int (get_method_index (acfg, method), p, &p);
8936 encode_int (info_offsets [i], p, &p);
8938 emit_aot_data (acfg, MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS, "extra_method_info_offsets", buf, p - buf);
8941 g_free (info_offsets);
8942 g_ptr_array_free (table, TRUE);
8946 generate_aotid (guint8* aotid)
8948 gpointer rand_handle;
8952 rand_handle = mono_rand_init (NULL, 0);
8954 mono_rand_try_get_bytes (&rand_handle, aotid, 16, &error);
8955 mono_error_assert_ok (&error);
8957 mono_rand_close (rand_handle);
8961 emit_exception_info (MonoAotCompile *acfg)
8965 SeqPointData sp_data;
8966 gboolean seq_points_to_file = FALSE;
8968 offsets = g_new0 (gint32, acfg->nmethods);
8969 for (i = 0; i < acfg->nmethods; ++i) {
8970 if (acfg->cfgs [i]) {
8971 MonoCompile *cfg = acfg->cfgs [i];
8973 // By design aot-runtime decode_exception_debug_info is not able to load sequence point debug data from a file.
8974 // As it is not possible to load debug data from a file its is also not possible to store it in a file.
8975 gboolean method_seq_points_to_file = acfg->aot_opts.gen_msym_dir &&
8976 cfg->gen_seq_points && !cfg->gen_sdb_seq_points;
8977 gboolean method_seq_points_to_binary = cfg->gen_seq_points && !method_seq_points_to_file;
8979 emit_exception_debug_info (acfg, cfg, method_seq_points_to_binary);
8980 offsets [i] = cfg->ex_info_offset;
8982 if (method_seq_points_to_file) {
8983 if (!seq_points_to_file) {
8984 mono_seq_point_data_init (&sp_data, acfg->nmethods);
8985 seq_points_to_file = TRUE;
8987 mono_seq_point_data_add (&sp_data, cfg->method->token, cfg->method_index, cfg->seq_point_info);
8994 if (seq_points_to_file) {
8995 char *aotid = mono_guid_to_string_minimal (acfg->image->aotid);
8996 char *dir = g_build_filename (acfg->aot_opts.gen_msym_dir_path, aotid, NULL);
8997 char *image_basename = g_path_get_basename (acfg->image->name);
8998 char *aot_file = g_strdup_printf("%s%s", image_basename, SEQ_POINT_AOT_EXT);
8999 char *aot_file_path = g_build_filename (dir, aot_file, NULL);
9001 if (g_ensure_directory_exists (aot_file_path) == FALSE) {
9002 fprintf (stderr, "AOT : failed to create msym directory: %s\n", aot_file_path);
9006 mono_seq_point_data_write (&sp_data, aot_file_path);
9007 mono_seq_point_data_free (&sp_data);
9011 g_free (image_basename);
9013 g_free (aot_file_path);
9016 acfg->stats.offsets_size += emit_offset_table (acfg, "ex_info_offsets", MONO_AOT_TABLE_EX_INFO_OFFSETS, acfg->nmethods, 10, offsets);
9021 emit_unwind_info (MonoAotCompile *acfg)
9026 if (acfg->aot_opts.llvm_only) {
9027 g_assert (acfg->unwind_ops->len == 0);
9032 * The unwind info contains a lot of duplicates so we emit each unique
9033 * entry once, and only store the offset from the start of the table in the
9037 sprintf (symbol, "unwind_info");
9038 emit_section_change (acfg, RODATA_SECT, 1);
9039 emit_alignment (acfg, 8);
9040 emit_info_symbol (acfg, symbol);
9042 for (i = 0; i < acfg->unwind_ops->len; ++i) {
9043 guint32 index = GPOINTER_TO_UINT (g_ptr_array_index (acfg->unwind_ops, i));
9044 guint8 *unwind_info;
9045 guint32 unwind_info_len;
9049 unwind_info = mono_get_cached_unwind_info (index, &unwind_info_len);
9052 encode_value (unwind_info_len, p, &p);
9053 emit_bytes (acfg, buf, p - buf);
9054 emit_bytes (acfg, unwind_info, unwind_info_len);
9056 acfg->stats.unwind_info_size += (p - buf) + unwind_info_len;
9061 emit_class_info (MonoAotCompile *acfg)
9066 offsets = g_new0 (gint32, acfg->image->tables [MONO_TABLE_TYPEDEF].rows);
9067 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
9068 offsets [i] = emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
9070 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);
9074 typedef struct ClassNameTableEntry {
9075 guint32 token, index;
9076 struct ClassNameTableEntry *next;
9077 } ClassNameTableEntry;
9080 emit_class_name_table (MonoAotCompile *acfg)
9082 int i, table_size, buf_size;
9083 guint32 token, hash;
9088 ClassNameTableEntry *entry, *new_entry;
9091 * Construct a chained hash table for mapping class names to typedef tokens.
9093 table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
9094 table = g_ptr_array_sized_new (table_size);
9095 for (i = 0; i < table_size; ++i)
9096 g_ptr_array_add (table, NULL);
9097 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
9099 token = MONO_TOKEN_TYPE_DEF | (i + 1);
9100 klass = mono_class_get_checked (acfg->image, token, &error);
9102 mono_error_cleanup (&error);
9105 full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
9106 hash = mono_metadata_str_hash (full_name) % table_size;
9109 /* FIXME: Allocate from the mempool */
9110 new_entry = g_new0 (ClassNameTableEntry, 1);
9111 new_entry->token = token;
9113 entry = (ClassNameTableEntry *)g_ptr_array_index (table, hash);
9114 if (entry == NULL) {
9115 new_entry->index = hash;
9116 g_ptr_array_index (table, hash) = new_entry;
9119 entry = entry->next;
9121 entry->next = new_entry;
9122 new_entry->index = table->len;
9123 g_ptr_array_add (table, new_entry);
9127 /* Emit the table */
9128 buf_size = table->len * 4 + 4;
9129 p = buf = (guint8 *)g_malloc0 (buf_size);
9131 /* FIXME: Optimize memory usage */
9132 g_assert (table_size < 65000);
9133 encode_int16 (table_size, p, &p);
9134 g_assert (table->len < 65000);
9135 for (i = 0; i < table->len; ++i) {
9136 ClassNameTableEntry *entry = (ClassNameTableEntry *)g_ptr_array_index (table, i);
9138 if (entry == NULL) {
9139 encode_int16 (0, p, &p);
9140 encode_int16 (0, p, &p);
9142 encode_int16 (mono_metadata_token_index (entry->token), p, &p);
9144 encode_int16 (entry->next->index, p, &p);
9146 encode_int16 (0, p, &p);
9150 g_assert (p - buf <= buf_size);
9151 g_ptr_array_free (table, TRUE);
9153 emit_aot_data (acfg, MONO_AOT_TABLE_CLASS_NAME, "class_name_table", buf, p - buf);
9159 emit_image_table (MonoAotCompile *acfg)
9165 * The image table is small but referenced in a lot of places.
9166 * So we emit it at once, and reference its elements by an index.
9168 buf_size = acfg->image_table->len * 28 + 4;
9169 for (i = 0; i < acfg->image_table->len; i++) {
9170 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
9171 MonoAssemblyName *aname = &image->assembly->aname;
9173 buf_size += strlen (image->assembly_name) + strlen (image->guid) + (aname->culture ? strlen (aname->culture) : 1) + strlen ((char*)aname->public_key_token) + 4;
9176 buf = p = (guint8 *)g_malloc0 (buf_size);
9177 encode_int (acfg->image_table->len, p, &p);
9178 for (i = 0; i < acfg->image_table->len; i++) {
9179 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
9180 MonoAssemblyName *aname = &image->assembly->aname;
9182 /* FIXME: Support multi-module assemblies */
9183 g_assert (image->assembly->image == image);
9185 encode_string (image->assembly_name, p, &p);
9186 encode_string (image->guid, p, &p);
9187 encode_string (aname->culture ? aname->culture : "", p, &p);
9188 encode_string ((const char*)aname->public_key_token, p, &p);
9190 while (GPOINTER_TO_UINT (p) % 8 != 0)
9193 encode_int (aname->flags, p, &p);
9194 encode_int (aname->major, p, &p);
9195 encode_int (aname->minor, p, &p);
9196 encode_int (aname->build, p, &p);
9197 encode_int (aname->revision, p, &p);
9199 g_assert (p - buf <= buf_size);
9201 emit_aot_data (acfg, MONO_AOT_TABLE_IMAGE_TABLE, "image_table", buf, p - buf);
9207 emit_got_info (MonoAotCompile *acfg, gboolean llvm)
9209 int i, first_plt_got_patch = 0, buf_size;
9211 guint32 *got_info_offsets;
9212 GotInfo *info = llvm ? &acfg->llvm_got_info : &acfg->got_info;
9214 /* Add the patches needed by the PLT to the GOT */
9216 acfg->plt_got_offset_base = acfg->got_offset;
9217 first_plt_got_patch = info->got_patches->len;
9218 for (i = 1; i < acfg->plt_offset; ++i) {
9219 MonoPltEntry *plt_entry = (MonoPltEntry *)g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
9221 g_ptr_array_add (info->got_patches, plt_entry->ji);
9223 acfg->stats.got_slot_types [plt_entry->ji->type] ++;
9226 acfg->got_offset += acfg->plt_offset;
9231 * - optimize offsets table.
9232 * - reduce number of exported symbols.
9233 * - emit info for a klass only once.
9234 * - determine when a method uses a GOT slot which is guaranteed to be already
9236 * - clean up and document the code.
9237 * - use String.Empty in class libs.
9240 /* Encode info required to decode shared GOT entries */
9241 buf_size = info->got_patches->len * 128;
9242 p = buf = (guint8 *)mono_mempool_alloc (acfg->mempool, buf_size);
9243 got_info_offsets = (guint32 *)mono_mempool_alloc (acfg->mempool, info->got_patches->len * sizeof (guint32));
9245 acfg->plt_got_info_offsets = (guint32 *)mono_mempool_alloc (acfg->mempool, acfg->plt_offset * sizeof (guint32));
9247 if (acfg->plt_offset)
9248 acfg->plt_got_info_offsets [0] = 0;
9250 for (i = 0; i < info->got_patches->len; ++i) {
9251 MonoJumpInfo *ji = (MonoJumpInfo *)g_ptr_array_index (info->got_patches, i);
9256 encode_value (ji->type, p, &p);
9258 encode_patch (acfg, ji, p, &p);
9259 acfg->stats.got_slot_info_sizes [ji->type] += p - p2;
9260 g_assert (p - buf <= buf_size);
9261 got_info_offsets [i] = add_to_blob (acfg, buf, p - buf);
9263 if (!llvm && i >= first_plt_got_patch)
9264 acfg->plt_got_info_offsets [i - first_plt_got_patch + 1] = got_info_offsets [i];
9265 acfg->stats.got_info_size += p - buf;
9268 /* Emit got_info_offsets table */
9270 /* No need to emit offsets for the got plt entries, the plt embeds them directly */
9271 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);
9275 emit_got (MonoAotCompile *acfg)
9277 char symbol [MAX_SYMBOL_SIZE];
9279 if (acfg->aot_opts.llvm_only)
9282 /* Don't make GOT global so accesses to it don't need relocations */
9283 sprintf (symbol, "%s", acfg->got_symbol);
9286 emit_unset_mode (acfg);
9287 fprintf (acfg->fp, ".section __DATA, __bss\n");
9288 emit_alignment (acfg, 8);
9290 emit_info_symbol (acfg, "jit_got");
9291 fprintf (acfg->fp, ".lcomm %s, %d\n", acfg->got_symbol, (int)(acfg->got_offset * sizeof (gpointer)));
9293 emit_section_change (acfg, ".bss", 0);
9294 emit_alignment (acfg, 8);
9295 if (acfg->aot_opts.write_symbols)
9296 emit_local_symbol (acfg, symbol, "got_end", FALSE);
9297 emit_label (acfg, symbol);
9299 emit_info_symbol (acfg, "jit_got");
9300 if (acfg->got_offset > 0)
9301 emit_zero_bytes (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
9304 sprintf (symbol, "got_end");
9305 emit_label (acfg, symbol);
9308 typedef struct GlobalsTableEntry {
9309 guint32 value, index;
9310 struct GlobalsTableEntry *next;
9311 } GlobalsTableEntry;
9313 #ifdef TARGET_WIN32_MSVC
9314 #define DLL_ENTRY_POINT "DllMain"
9317 emit_library_info (MonoAotCompile *acfg)
9319 // Only include for shared libraries linked directly from generated object.
9320 if (link_shared_library (acfg)) {
9322 char symbol [MAX_SYMBOL_SIZE];
9324 // Ask linker to export all global symbols.
9325 emit_section_change (acfg, ".drectve", 0);
9326 for (guint i = 0; i < acfg->globals->len; ++i) {
9327 name = (char *)g_ptr_array_index (acfg->globals, i);
9328 g_assert (name != NULL);
9329 sprintf_s (symbol, MAX_SYMBOL_SIZE, " /EXPORT:%s", name);
9330 emit_string (acfg, symbol);
9333 // Emit DLLMain function, needed by MSVC linker for DLL's.
9334 // NOTE, DllMain should not go into exports above.
9335 emit_section_change (acfg, ".text", 0);
9336 emit_global (acfg, DLL_ENTRY_POINT, TRUE);
9337 emit_label (acfg, DLL_ENTRY_POINT);
9339 // Simple implementation of DLLMain, just returning TRUE.
9340 // For more information about DLLMain: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx
9341 fprintf (acfg->fp, "movl $1, %%eax\n");
9342 fprintf (acfg->fp, "ret\n");
9344 // Inform linker about our dll entry function.
9345 emit_section_change (acfg, ".drectve", 0);
9346 emit_string (acfg, "/ENTRY:" DLL_ENTRY_POINT);
9354 emit_library_info (MonoAotCompile *acfg)
9361 emit_globals (MonoAotCompile *acfg)
9367 GlobalsTableEntry *entry, *new_entry;
9369 if (!acfg->aot_opts.static_link)
9372 if (acfg->aot_opts.llvm_only) {
9373 g_assert (acfg->globals->len == 0);
9378 * When static linking, we emit a table containing our globals.
9382 * Construct a chained hash table for mapping global names to their index in
9383 * the globals table.
9385 table_size = g_spaced_primes_closest ((int)(acfg->globals->len * 1.5));
9386 table = g_ptr_array_sized_new (table_size);
9387 for (i = 0; i < table_size; ++i)
9388 g_ptr_array_add (table, NULL);
9389 for (i = 0; i < acfg->globals->len; ++i) {
9390 char *name = (char *)g_ptr_array_index (acfg->globals, i);
9392 hash = mono_metadata_str_hash (name) % table_size;
9394 /* FIXME: Allocate from the mempool */
9395 new_entry = g_new0 (GlobalsTableEntry, 1);
9396 new_entry->value = i;
9398 entry = (GlobalsTableEntry *)g_ptr_array_index (table, hash);
9399 if (entry == NULL) {
9400 new_entry->index = hash;
9401 g_ptr_array_index (table, hash) = new_entry;
9404 entry = entry->next;
9406 entry->next = new_entry;
9407 new_entry->index = table->len;
9408 g_ptr_array_add (table, new_entry);
9412 /* Emit the table */
9413 sprintf (symbol, ".Lglobals_hash");
9414 emit_section_change (acfg, RODATA_SECT, 0);
9415 emit_alignment (acfg, 8);
9416 emit_label (acfg, symbol);
9418 /* FIXME: Optimize memory usage */
9419 g_assert (table_size < 65000);
9420 emit_int16 (acfg, table_size);
9421 for (i = 0; i < table->len; ++i) {
9422 GlobalsTableEntry *entry = (GlobalsTableEntry *)g_ptr_array_index (table, i);
9424 if (entry == NULL) {
9425 emit_int16 (acfg, 0);
9426 emit_int16 (acfg, 0);
9428 emit_int16 (acfg, entry->value + 1);
9430 emit_int16 (acfg, entry->next->index);
9432 emit_int16 (acfg, 0);
9436 /* Emit the names */
9437 for (i = 0; i < acfg->globals->len; ++i) {
9438 char *name = (char *)g_ptr_array_index (acfg->globals, i);
9440 sprintf (symbol, "name_%d", i);
9441 emit_section_change (acfg, RODATA_SECT, 1);
9443 emit_alignment (acfg, 4);
9445 emit_label (acfg, symbol);
9446 emit_string (acfg, name);
9449 /* Emit the globals table */
9450 sprintf (symbol, "globals");
9451 emit_section_change (acfg, ".data", 0);
9452 /* This is not a global, since it is accessed by the init function */
9453 emit_alignment (acfg, 8);
9454 emit_info_symbol (acfg, symbol);
9456 sprintf (symbol, "%sglobals_hash", acfg->temp_prefix);
9457 emit_pointer (acfg, symbol);
9459 for (i = 0; i < acfg->globals->len; ++i) {
9460 char *name = (char *)g_ptr_array_index (acfg->globals, i);
9462 sprintf (symbol, "name_%d", i);
9463 emit_pointer (acfg, symbol);
9465 g_assert (strlen (name) < sizeof (symbol));
9466 sprintf (symbol, "%s", name);
9467 emit_pointer (acfg, symbol);
9469 /* Null terminate the table */
9470 emit_int32 (acfg, 0);
9471 emit_int32 (acfg, 0);
9475 emit_mem_end (MonoAotCompile *acfg)
9479 if (acfg->aot_opts.llvm_only)
9482 sprintf (symbol, "mem_end");
9483 emit_section_change (acfg, ".text", 1);
9484 emit_alignment_code (acfg, 8);
9485 emit_label (acfg, symbol);
9489 init_aot_file_info (MonoAotCompile *acfg, MonoAotFileInfo *info)
9493 info->version = MONO_AOT_FILE_VERSION;
9494 info->plt_got_offset_base = acfg->plt_got_offset_base;
9495 info->got_size = acfg->got_offset * sizeof (gpointer);
9496 info->plt_size = acfg->plt_offset;
9497 info->nmethods = acfg->nmethods;
9498 info->flags = acfg->flags;
9499 info->opts = acfg->opts;
9500 info->simd_opts = acfg->simd_opts;
9501 info->gc_name_index = acfg->gc_name_offset;
9502 info->datafile_size = acfg->datafile_offset;
9503 for (i = 0; i < MONO_AOT_TABLE_NUM; ++i)
9504 info->table_offsets [i] = acfg->table_offsets [i];
9505 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
9506 info->num_trampolines [i] = acfg->num_trampolines [i];
9507 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
9508 info->trampoline_got_offset_base [i] = acfg->trampoline_got_offset_base [i];
9509 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
9510 info->trampoline_size [i] = acfg->trampoline_size [i];
9511 info->num_rgctx_fetch_trampolines = acfg->aot_opts.nrgctx_fetch_trampolines;
9513 info->double_align = MONO_ABI_ALIGNOF (double);
9514 info->long_align = MONO_ABI_ALIGNOF (gint64);
9515 info->generic_tramp_num = MONO_TRAMPOLINE_NUM;
9516 info->tramp_page_size = acfg->tramp_page_size;
9517 info->nshared_got_entries = acfg->nshared_got_entries;
9518 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
9519 info->tramp_page_code_offsets [i] = acfg->tramp_page_code_offsets [i];
9521 memcpy(&info->aotid, acfg->image->aotid, 16);
9525 emit_aot_file_info (MonoAotCompile *acfg, MonoAotFileInfo *info)
9527 char symbol [MAX_SYMBOL_SIZE];
9529 const char **symbols;
9531 symbols = g_new0 (const char *, MONO_AOT_FILE_INFO_NUM_SYMBOLS);
9533 symbols [sindex ++] = acfg->got_symbol;
9535 symbols [sindex ++] = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, acfg->llvm_got_symbol);
9536 symbols [sindex ++] = acfg->llvm_eh_frame_symbol;
9538 symbols [sindex ++] = NULL;
9539 symbols [sindex ++] = NULL;
9541 /* llvm_get_method */
9542 symbols [sindex ++] = NULL;
9543 /* llvm_get_unbox_tramp */
9544 symbols [sindex ++] = NULL;
9545 if (!acfg->aot_opts.llvm_only) {
9546 symbols [sindex ++] = "jit_code_start";
9547 symbols [sindex ++] = "jit_code_end";
9548 symbols [sindex ++] = "method_addresses";
9550 symbols [sindex ++] = NULL;
9551 symbols [sindex ++] = NULL;
9552 symbols [sindex ++] = NULL;
9554 if (acfg->data_outfile) {
9555 for (i = 0; i < MONO_AOT_TABLE_NUM; ++i)
9556 symbols [sindex ++] = NULL;
9558 symbols [sindex ++] = "blob";
9559 symbols [sindex ++] = "class_name_table";
9560 symbols [sindex ++] = "class_info_offsets";
9561 symbols [sindex ++] = "method_info_offsets";
9562 symbols [sindex ++] = "ex_info_offsets";
9563 symbols [sindex ++] = "extra_method_info_offsets";
9564 symbols [sindex ++] = "extra_method_table";
9565 symbols [sindex ++] = "got_info_offsets";
9567 symbols [sindex ++] = "llvm_got_info_offsets";
9569 symbols [sindex ++] = NULL;
9570 symbols [sindex ++] = "image_table";
9572 symbols [sindex ++] = "mem_end";
9573 symbols [sindex ++] = "assembly_guid";
9574 symbols [sindex ++] = "runtime_version";
9575 if (acfg->num_trampoline_got_entries) {
9576 symbols [sindex ++] = "specific_trampolines";
9577 symbols [sindex ++] = "static_rgctx_trampolines";
9578 symbols [sindex ++] = "imt_trampolines";
9579 symbols [sindex ++] = "gsharedvt_arg_trampolines";
9581 symbols [sindex ++] = NULL;
9582 symbols [sindex ++] = NULL;
9583 symbols [sindex ++] = NULL;
9584 symbols [sindex ++] = NULL;
9586 if (acfg->aot_opts.static_link) {
9587 symbols [sindex ++] = "globals";
9589 symbols [sindex ++] = NULL;
9591 symbols [sindex ++] = "assembly_name";
9592 symbols [sindex ++] = "plt";
9593 symbols [sindex ++] = "plt_end";
9594 symbols [sindex ++] = "unwind_info";
9595 if (!acfg->aot_opts.llvm_only) {
9596 symbols [sindex ++] = "unbox_trampolines";
9597 symbols [sindex ++] = "unbox_trampolines_end";
9598 symbols [sindex ++] = "unbox_trampoline_addresses";
9600 symbols [sindex ++] = NULL;
9601 symbols [sindex ++] = NULL;
9602 symbols [sindex ++] = NULL;
9605 g_assert (sindex == MONO_AOT_FILE_INFO_NUM_SYMBOLS);
9607 sprintf (symbol, "%smono_aot_file_info", acfg->user_symbol_prefix);
9608 emit_section_change (acfg, ".data", 0);
9609 emit_alignment (acfg, 8);
9610 emit_label (acfg, symbol);
9611 if (!acfg->aot_opts.static_link)
9612 emit_global (acfg, symbol, FALSE);
9614 /* The data emitted here must match MonoAotFileInfo. */
9616 emit_int32 (acfg, info->version);
9617 emit_int32 (acfg, info->dummy);
9620 * We emit pointers to our data structures instead of emitting global symbols which
9621 * point to them, to reduce the number of globals, and because using globals leads to
9622 * various problems (i.e. arm/thumb).
9624 for (i = 0; i < MONO_AOT_FILE_INFO_NUM_SYMBOLS; ++i)
9625 emit_pointer (acfg, symbols [i]);
9627 emit_int32 (acfg, info->plt_got_offset_base);
9628 emit_int32 (acfg, info->got_size);
9629 emit_int32 (acfg, info->plt_size);
9630 emit_int32 (acfg, info->nmethods);
9631 emit_int32 (acfg, info->flags);
9632 emit_int32 (acfg, info->opts);
9633 emit_int32 (acfg, info->simd_opts);
9634 emit_int32 (acfg, info->gc_name_index);
9635 emit_int32 (acfg, info->num_rgctx_fetch_trampolines);
9636 emit_int32 (acfg, info->double_align);
9637 emit_int32 (acfg, info->long_align);
9638 emit_int32 (acfg, info->generic_tramp_num);
9639 emit_int32 (acfg, info->tramp_page_size);
9640 emit_int32 (acfg, info->nshared_got_entries);
9641 emit_int32 (acfg, info->datafile_size);
9643 for (i = 0; i < MONO_AOT_TABLE_NUM; ++i)
9644 emit_int32 (acfg, info->table_offsets [i]);
9645 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
9646 emit_int32 (acfg, info->num_trampolines [i]);
9647 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
9648 emit_int32 (acfg, info->trampoline_got_offset_base [i]);
9649 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
9650 emit_int32 (acfg, info->trampoline_size [i]);
9651 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
9652 emit_int32 (acfg, info->tramp_page_code_offsets [i]);
9654 emit_bytes (acfg, info->aotid, 16);
9656 if (acfg->aot_opts.static_link) {
9657 emit_global_inner (acfg, acfg->static_linking_symbol, FALSE);
9658 emit_alignment (acfg, sizeof (gpointer));
9659 emit_label (acfg, acfg->static_linking_symbol);
9660 emit_pointer_2 (acfg, acfg->user_symbol_prefix, "mono_aot_file_info");
9665 * Emit a structure containing all the information not stored elsewhere.
9668 emit_file_info (MonoAotCompile *acfg)
9671 MonoAotFileInfo *info;
9673 if (acfg->aot_opts.bind_to_runtime_version) {
9674 build_info = mono_get_runtime_build_info ();
9675 emit_string_symbol (acfg, "runtime_version", build_info);
9676 g_free (build_info);
9678 emit_string_symbol (acfg, "runtime_version", "");
9681 emit_string_symbol (acfg, "assembly_guid" , acfg->image->guid);
9683 /* Emit a string holding the assembly name */
9684 emit_string_symbol (acfg, "assembly_name", acfg->image->assembly->aname.name);
9686 info = g_new0 (MonoAotFileInfo, 1);
9687 init_aot_file_info (acfg, info);
9689 if (acfg->aot_opts.static_link) {
9690 char symbol [MAX_SYMBOL_SIZE];
9694 * Emit a global symbol which can be passed by an embedding app to
9695 * mono_aot_register_module (). The symbol points to a pointer to the the file info
9698 sprintf (symbol, "%smono_aot_module_%s_info", acfg->user_symbol_prefix, acfg->image->assembly->aname.name);
9700 /* Get rid of characters which cannot occur in symbols */
9702 for (p = symbol; *p; ++p) {
9703 if (!(isalnum (*p) || *p == '_'))
9706 acfg->static_linking_symbol = g_strdup (symbol);
9710 mono_llvm_emit_aot_file_info (info, acfg->has_jitted_code);
9712 emit_aot_file_info (acfg, info);
9716 emit_blob (MonoAotCompile *acfg)
9718 acfg->blob_closed = TRUE;
9720 emit_aot_data (acfg, MONO_AOT_TABLE_BLOB, "blob", (guint8*)acfg->blob.data, acfg->blob.index);
9724 emit_objc_selectors (MonoAotCompile *acfg)
9729 if (!acfg->objc_selectors || acfg->objc_selectors->len == 0)
9734 * cat > foo.m << EOF
9737 * return @selector(print:);
9742 mono_img_writer_emit_unset_mode (acfg->w);
9743 g_assert (acfg->fp);
9744 fprintf (acfg->fp, ".section __DATA,__objc_selrefs,literal_pointers,no_dead_strip\n");
9745 fprintf (acfg->fp, ".align 3\n");
9746 for (i = 0; i < acfg->objc_selectors->len; ++i) {
9747 sprintf (symbol, "L_OBJC_SELECTOR_REFERENCES_%d", i);
9748 emit_label (acfg, symbol);
9749 sprintf (symbol, "L_OBJC_METH_VAR_NAME_%d", i);
9750 emit_pointer (acfg, symbol);
9753 fprintf (acfg->fp, ".section __TEXT,__cstring,cstring_literals\n");
9754 for (i = 0; i < acfg->objc_selectors->len; ++i) {
9755 fprintf (acfg->fp, "L_OBJC_METH_VAR_NAME_%d:\n", i);
9756 fprintf (acfg->fp, ".asciz \"%s\"\n", (char*)g_ptr_array_index (acfg->objc_selectors, i));
9759 fprintf (acfg->fp, ".section __DATA,__objc_imageinfo,regular,no_dead_strip\n");
9760 fprintf (acfg->fp, ".align 3\n");
9761 fprintf (acfg->fp, "L_OBJC_IMAGE_INFO:\n");
9762 fprintf (acfg->fp, ".long 0\n");
9763 fprintf (acfg->fp, ".long 16\n");
9767 emit_dwarf_info (MonoAotCompile *acfg)
9769 #ifdef EMIT_DWARF_INFO
9773 /* DIEs for methods */
9774 for (i = 0; i < acfg->nmethods; ++i) {
9775 MonoCompile *cfg = acfg->cfgs [i];
9780 // FIXME: LLVM doesn't define .Lme_...
9781 if (cfg->compile_llvm)
9784 sprintf (symbol2, "%sme_%x", acfg->temp_prefix, i);
9786 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 ()));
9792 collect_methods (MonoAotCompile *acfg)
9795 MonoImage *image = acfg->image;
9797 /* Collect methods */
9798 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
9801 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
9803 method = mono_get_method_checked (acfg->image, token, NULL, NULL, &error);
9806 aot_printerrf (acfg, "Failed to load method 0x%x from '%s' due to %s.\n", token, image->name, mono_error_get_message (&error));
9807 aot_printerrf (acfg, "Run with MONO_LOG_LEVEL=debug for more information.\n");
9808 mono_error_cleanup (&error);
9812 /* Load all methods eagerly to skip the slower lazy loading code */
9813 mono_class_setup_methods (method->klass);
9815 if (mono_aot_mode_is_full (&acfg->aot_opts) && method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
9816 /* Compile the wrapper instead */
9817 /* We do this here instead of add_wrappers () because it is easy to do it here */
9818 MonoMethod *wrapper = mono_marshal_get_native_wrapper (method, TRUE, TRUE);
9822 /* FIXME: Some mscorlib methods don't have debug info */
9824 if (acfg->aot_opts.soft_debug && !method->wrapper_type) {
9825 if (!((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
9826 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
9827 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
9828 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))) {
9829 if (!mono_debug_lookup_method (method)) {
9830 fprintf (stderr, "Method %s has no debug info, probably the .mdb file for the assembly is missing.\n", mono_method_get_full_name (method));
9837 if (method->is_generic || mono_class_is_gtd (method->klass))
9838 /* Compile the ref shared version instead */
9839 method = mini_get_shared_method (method);
9841 /* Since we add the normal methods first, their index will be equal to their zero based token index */
9842 add_method_with_index (acfg, method, i, FALSE);
9843 acfg->method_index ++;
9846 /* gsharedvt methods */
9847 for (mindex = 0; mindex < image->tables [MONO_TABLE_METHOD].rows; ++mindex) {
9850 guint32 token = MONO_TOKEN_METHOD_DEF | (mindex + 1);
9852 if (!(acfg->opts & MONO_OPT_GSHAREDVT))
9855 method = mono_get_method_checked (acfg->image, token, NULL, NULL, &error);
9856 report_loader_error (acfg, &error, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (&error));
9858 if (method->is_generic || mono_class_is_gtd (method->klass)) {
9859 MonoMethod *gshared;
9861 gshared = mini_get_shared_method_full (method, TRUE, TRUE);
9862 add_extra_method (acfg, gshared);
9866 if (mono_aot_mode_is_full (&acfg->aot_opts))
9867 add_generic_instances (acfg);
9869 if (mono_aot_mode_is_full (&acfg->aot_opts))
9870 add_wrappers (acfg);
9875 compile_methods (MonoAotCompile *acfg)
9879 if (acfg->aot_opts.nthreads > 0) {
9883 MonoThreadHandle *thread_handle;
9884 gpointer *user_data;
9885 MonoMethod **methods;
9887 methods_len = acfg->methods->len;
9889 len = acfg->methods->len / acfg->aot_opts.nthreads;
9892 * Partition the list of methods into fragments, and hand it to threads to
9895 threads = g_ptr_array_new ();
9896 /* Make a copy since acfg->methods is modified by compile_method () */
9897 methods = g_new0 (MonoMethod*, methods_len);
9898 //memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
9899 for (i = 0; i < methods_len; ++i)
9900 methods [i] = (MonoMethod *)g_ptr_array_index (acfg->methods, i);
9902 while (i < methods_len) {
9903 frag = g_ptr_array_new ();
9904 for (j = 0; j < len; ++j) {
9905 if (i < methods_len) {
9906 g_ptr_array_add (frag, methods [i]);
9911 user_data = g_new0 (gpointer, 3);
9912 user_data [0] = mono_domain_get ();
9913 user_data [1] = acfg;
9914 user_data [2] = frag;
9916 thread_handle = mono_threads_create_thread (compile_thread_main, (gpointer) user_data, NULL, NULL);
9917 g_ptr_array_add (threads, thread_handle);
9921 for (i = 0; i < threads->len; ++i) {
9922 mono_thread_info_wait_one_handle (g_ptr_array_index (threads, i), MONO_INFINITE_WAIT, FALSE);
9923 mono_threads_close_thread_handle (g_ptr_array_index (threads, i));
9929 /* Compile methods added by compile_method () or all methods if nthreads == 0 */
9930 for (i = methods_len; i < acfg->methods->len; ++i) {
9931 /* This can new methods to acfg->methods */
9932 compile_method (acfg, (MonoMethod *)g_ptr_array_index (acfg->methods, i));
9937 compile_asm (MonoAotCompile *acfg)
9939 char *command, *objfile;
9940 char *outfile_name, *tmp_outfile_name, *llvm_ofile;
9941 const char *tool_prefix = acfg->aot_opts.tool_prefix ? acfg->aot_opts.tool_prefix : "";
9942 char *ld_flags = acfg->aot_opts.ld_flags ? acfg->aot_opts.ld_flags : g_strdup("");
9944 #ifdef TARGET_WIN32_MSVC
9945 #define AS_OPTIONS "-c -x assembler"
9946 #elif defined(TARGET_AMD64) && !defined(TARGET_MACH)
9947 #define AS_OPTIONS "--64"
9948 #elif defined(TARGET_POWERPC64)
9949 #define AS_OPTIONS "-a64 -mppc64"
9950 #elif defined(sparc) && SIZEOF_VOID_P == 8
9951 #define AS_OPTIONS "-xarch=v9"
9952 #elif defined(TARGET_X86) && defined(TARGET_MACH) && !defined(__native_client_codegen__)
9953 #define AS_OPTIONS "-arch i386"
9955 #define AS_OPTIONS ""
9958 #ifdef __native_client_codegen__
9959 #if defined(TARGET_AMD64)
9960 #define AS_NAME "nacl64-as"
9962 #define AS_NAME "nacl-as"
9964 #elif defined(TARGET_OSX)
9965 #define AS_NAME "clang"
9966 #elif defined(TARGET_WIN32_MSVC)
9967 #define AS_NAME "clang.exe"
9969 #define AS_NAME "as"
9972 #ifdef TARGET_WIN32_MSVC
9973 #define AS_OBJECT_FILE_SUFFIX "obj"
9975 #define AS_OBJECT_FILE_SUFFIX "o"
9979 #define LD_NAME "ld"
9980 #define LD_OPTIONS "-shared -G"
9981 #elif defined(__ppc__) && defined(TARGET_MACH)
9982 #define LD_NAME "gcc"
9983 #define LD_OPTIONS "-dynamiclib"
9984 #elif defined(TARGET_AMD64) && defined(TARGET_MACH)
9985 #define LD_NAME "clang"
9986 #define LD_OPTIONS "--shared"
9987 #elif defined(TARGET_WIN32_MSVC)
9988 #define LD_NAME "link.exe"
9989 #define LD_OPTIONS "/DLL /MACHINE:X64 /NOLOGO"
9990 #elif defined(TARGET_WIN32) && !defined(TARGET_ANDROID)
9991 #define LD_NAME "gcc"
9992 #define LD_OPTIONS "-shared"
9993 #elif defined(TARGET_X86) && defined(TARGET_MACH) && !defined(__native_client_codegen__)
9994 #define LD_NAME "clang"
9995 #define LD_OPTIONS "-m32 -dynamiclib"
9996 #elif defined(TARGET_ARM) && !defined(TARGET_ANDROID)
9997 #define LD_NAME "gcc"
9998 #define LD_OPTIONS "--shared"
9999 #elif defined(TARGET_POWERPC64)
10000 #define LD_OPTIONS "-m elf64ppc"
10004 #define LD_OPTIONS ""
10007 if (acfg->aot_opts.asm_only) {
10008 aot_printf (acfg, "Output file: '%s'.\n", acfg->tmpfname);
10009 if (acfg->aot_opts.static_link)
10010 aot_printf (acfg, "Linking symbol: '%s'.\n", acfg->static_linking_symbol);
10012 aot_printf (acfg, "LLVM output file: '%s'.\n", acfg->llvm_sfile);
10016 if (acfg->aot_opts.static_link) {
10017 if (acfg->aot_opts.outfile)
10018 objfile = g_strdup_printf ("%s", acfg->aot_opts.outfile);
10020 objfile = g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->image->name);
10022 objfile = g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->tmpfname);
10026 g_string_append (acfg->as_args, "-c -x assembler");
10029 command = g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix, AS_NAME, AS_OPTIONS,
10030 acfg->as_args ? acfg->as_args->str : "",
10031 wrap_path (objfile), wrap_path (acfg->tmpfname));
10032 aot_printf (acfg, "Executing the native assembler: %s\n", command);
10033 if (execute_system (command) != 0) {
10039 if (acfg->llvm && !acfg->llvm_owriter) {
10040 command = g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix, AS_NAME, AS_OPTIONS,
10041 acfg->as_args ? acfg->as_args->str : "",
10042 wrap_path (acfg->llvm_ofile), wrap_path (acfg->llvm_sfile));
10043 aot_printf (acfg, "Executing the native assembler: %s\n", command);
10044 if (execute_system (command) != 0) {
10053 if (acfg->aot_opts.static_link) {
10054 aot_printf (acfg, "Output file: '%s'.\n", objfile);
10055 aot_printf (acfg, "Linking symbol: '%s'.\n", acfg->static_linking_symbol);
10060 if (acfg->aot_opts.outfile)
10061 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
10063 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, MONO_SOLIB_EXT);
10065 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
10068 llvm_ofile = g_strdup_printf ("\"%s\"", acfg->llvm_ofile);
10070 llvm_ofile = g_strdup ("");
10073 /* replace the ; flags separators with spaces */
10074 g_strdelimit (ld_flags, ";", ' ');
10076 if (acfg->aot_opts.llvm_only)
10077 ld_flags = g_strdup_printf ("%s %s", ld_flags, "-lstdc++");
10079 #ifdef TARGET_WIN32_MSVC
10080 g_assert (tmp_outfile_name != NULL);
10081 g_assert (objfile != NULL);
10082 command = g_strdup_printf ("\"%s%s\" %s %s /OUT:\"%s\" \"%s\"", tool_prefix, LD_NAME, LD_OPTIONS,
10083 ld_flags, tmp_outfile_name, objfile);
10084 #elif defined(LD_NAME)
10085 command = g_strdup_printf ("%s%s %s -o %s %s %s %s", tool_prefix, LD_NAME, LD_OPTIONS,
10086 wrap_path (tmp_outfile_name), wrap_path (llvm_ofile),
10087 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->tmpfname)), ld_flags);
10090 if (acfg->aot_opts.tool_prefix) {
10091 /* Cross compiling */
10092 command = g_strdup_printf ("\"%sld\" %s -shared -o %s %s %s %s", tool_prefix, LD_OPTIONS,
10093 wrap_path (tmp_outfile_name), wrap_path (llvm_ofile),
10094 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->tmpfname)), ld_flags);
10096 char *args = g_strdup_printf ("%s -shared -o %s %s %s %s", LD_OPTIONS,
10097 wrap_path (tmp_outfile_name), wrap_path (llvm_ofile),
10098 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->tmpfname)), ld_flags);
10100 if (acfg->aot_opts.llvm_only) {
10101 command = g_strdup_printf ("clang++ %s", args);
10103 command = g_strdup_printf ("\"%sld\" %s", tool_prefix, args);
10108 aot_printf (acfg, "Executing the native linker: %s\n", command);
10109 if (execute_system (command) != 0) {
10110 g_free (tmp_outfile_name);
10111 g_free (outfile_name);
10120 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, MONO_SOLIB_EXT);
10121 printf ("Stripping the binary: %s\n", com);
10122 execute_system (com);
10125 #if defined(TARGET_ARM) && !defined(TARGET_MACH)
10127 * gas generates 'mapping symbols' each time code and data is mixed, which
10128 * happens a lot in emit_and_reloc_code (), so we need to get rid of them.
10130 command = g_strdup_printf ("\"%sstrip\" --strip-symbol=\\$a --strip-symbol=\\$d %s", wrap_path(tool_prefix), wrap_path(tmp_outfile_name));
10131 aot_printf (acfg, "Stripping the binary: %s\n", command);
10132 if (execute_system (command) != 0) {
10133 g_free (tmp_outfile_name);
10134 g_free (outfile_name);
10141 if (0 != rename (tmp_outfile_name, outfile_name)) {
10142 if (G_FILE_ERROR_EXIST == g_file_error_from_errno (errno)) {
10143 /* Since we are rebuilding the module we need to be able to replace any old copies. Remove old file and retry rename operation. */
10144 unlink (outfile_name);
10145 rename (tmp_outfile_name, outfile_name);
10149 #if defined(TARGET_MACH)
10150 command = g_strdup_printf ("dsymutil \"%s\"", outfile_name);
10151 aot_printf (acfg, "Executing dsymutil: %s\n", command);
10152 if (execute_system (command) != 0) {
10157 if (!acfg->aot_opts.save_temps)
10160 g_free (tmp_outfile_name);
10161 g_free (outfile_name);
10164 if (acfg->aot_opts.save_temps)
10165 aot_printf (acfg, "Retained input file.\n");
10167 unlink (acfg->tmpfname);
10172 static void init_got_info (GotInfo *info)
10176 info->patch_to_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
10177 info->patch_to_got_offset_by_type = g_new0 (GHashTable*, MONO_PATCH_INFO_NUM);
10178 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
10179 info->patch_to_got_offset_by_type [i] = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
10180 info->got_patches = g_ptr_array_new ();
10183 static MonoAotCompile*
10184 acfg_create (MonoAssembly *ass, guint32 opts)
10186 MonoImage *image = ass->image;
10187 MonoAotCompile *acfg;
10189 acfg = g_new0 (MonoAotCompile, 1);
10190 acfg->methods = g_ptr_array_new ();
10191 acfg->method_indexes = g_hash_table_new (NULL, NULL);
10192 acfg->method_depth = g_hash_table_new (NULL, NULL);
10193 acfg->plt_offset_to_entry = g_hash_table_new (NULL, NULL);
10194 acfg->patch_to_plt_entry = g_new0 (GHashTable*, MONO_PATCH_INFO_NUM);
10195 acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
10196 acfg->token_info_hash = g_hash_table_new_full (NULL, NULL, NULL, NULL);
10197 acfg->method_to_pinvoke_import = g_hash_table_new_full (NULL, NULL, NULL, g_free);
10198 acfg->image_hash = g_hash_table_new (NULL, NULL);
10199 acfg->image_table = g_ptr_array_new ();
10200 acfg->globals = g_ptr_array_new ();
10201 acfg->image = image;
10203 /* TODO: Write out set of SIMD instructions used, rather than just those available */
10204 acfg->simd_opts = mono_arch_cpu_enumerate_simd_versions ();
10205 acfg->mempool = mono_mempool_new ();
10206 acfg->extra_methods = g_ptr_array_new ();
10207 acfg->unwind_info_offsets = g_hash_table_new (NULL, NULL);
10208 acfg->unwind_ops = g_ptr_array_new ();
10209 acfg->method_label_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
10210 acfg->method_order = g_ptr_array_new ();
10211 acfg->export_names = g_hash_table_new (NULL, NULL);
10212 acfg->klass_blob_hash = g_hash_table_new (NULL, NULL);
10213 acfg->method_blob_hash = g_hash_table_new (NULL, NULL);
10214 acfg->plt_entry_debug_sym_cache = g_hash_table_new (g_str_hash, g_str_equal);
10215 acfg->gsharedvt_in_signatures = g_hash_table_new ((GHashFunc)mono_signature_hash, (GEqualFunc)mono_metadata_signature_equal);
10216 acfg->gsharedvt_out_signatures = g_hash_table_new ((GHashFunc)mono_signature_hash, (GEqualFunc)mono_metadata_signature_equal);
10217 mono_os_mutex_init_recursive (&acfg->mutex);
10219 init_got_info (&acfg->got_info);
10220 init_got_info (&acfg->llvm_got_info);
10226 got_info_free (GotInfo *info)
10230 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
10231 g_hash_table_destroy (info->patch_to_got_offset_by_type [i]);
10232 g_free (info->patch_to_got_offset_by_type);
10233 g_hash_table_destroy (info->patch_to_got_offset);
10234 g_ptr_array_free (info->got_patches, TRUE);
10238 acfg_free (MonoAotCompile *acfg)
10242 mono_img_writer_destroy (acfg->w);
10243 for (i = 0; i < acfg->nmethods; ++i)
10244 if (acfg->cfgs [i])
10245 mono_destroy_compile (acfg->cfgs [i]);
10247 g_free (acfg->cfgs);
10249 g_free (acfg->static_linking_symbol);
10250 g_free (acfg->got_symbol);
10251 g_free (acfg->plt_symbol);
10252 g_ptr_array_free (acfg->methods, TRUE);
10253 g_ptr_array_free (acfg->image_table, TRUE);
10254 g_ptr_array_free (acfg->globals, TRUE);
10255 g_ptr_array_free (acfg->unwind_ops, TRUE);
10256 g_hash_table_destroy (acfg->method_indexes);
10257 g_hash_table_destroy (acfg->method_depth);
10258 g_hash_table_destroy (acfg->plt_offset_to_entry);
10259 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i) {
10260 if (acfg->patch_to_plt_entry [i])
10261 g_hash_table_destroy (acfg->patch_to_plt_entry [i]);
10263 g_free (acfg->patch_to_plt_entry);
10264 g_hash_table_destroy (acfg->method_to_cfg);
10265 g_hash_table_destroy (acfg->token_info_hash);
10266 g_hash_table_destroy (acfg->method_to_pinvoke_import);
10267 g_hash_table_destroy (acfg->image_hash);
10268 g_hash_table_destroy (acfg->unwind_info_offsets);
10269 g_hash_table_destroy (acfg->method_label_hash);
10270 if (acfg->typespec_classes)
10271 g_hash_table_destroy (acfg->typespec_classes);
10272 g_hash_table_destroy (acfg->export_names);
10273 g_hash_table_destroy (acfg->plt_entry_debug_sym_cache);
10274 g_hash_table_destroy (acfg->klass_blob_hash);
10275 g_hash_table_destroy (acfg->method_blob_hash);
10276 got_info_free (&acfg->got_info);
10277 got_info_free (&acfg->llvm_got_info);
10278 mono_mempool_destroy (acfg->mempool);
10282 #define WRAPPER(e,n) n,
10283 static const char* const
10284 wrapper_type_names [MONO_WRAPPER_NUM + 1] = {
10285 #include "mono/metadata/wrapper-types.h"
10289 static G_GNUC_UNUSED const char*
10290 get_wrapper_type_name (int type)
10292 return wrapper_type_names [type];
10298 static void aot_dump (MonoAotCompile *acfg)
10304 mono_json_writer_init (&writer);
10306 mono_json_writer_object_begin(&writer);
10309 mono_json_writer_indent (&writer);
10310 mono_json_writer_object_key(&writer, "methods");
10311 mono_json_writer_array_begin (&writer);
10314 for (i = 0; i < acfg->nmethods; ++i) {
10316 MonoMethod *method;
10319 cfg = acfg->cfgs [i];
10323 method = cfg->orig_method;
10325 mono_json_writer_indent (&writer);
10326 mono_json_writer_object_begin(&writer);
10328 mono_json_writer_indent (&writer);
10329 mono_json_writer_object_key(&writer, "name");
10330 mono_json_writer_printf (&writer, "\"%s\",\n", method->name);
10332 mono_json_writer_indent (&writer);
10333 mono_json_writer_object_key(&writer, "signature");
10334 mono_json_writer_printf (&writer, "\"%s\",\n", mono_method_get_full_name (method));
10336 mono_json_writer_indent (&writer);
10337 mono_json_writer_object_key(&writer, "code_size");
10338 mono_json_writer_printf (&writer, "\"%d\",\n", cfg->code_size);
10340 klass = method->klass;
10342 mono_json_writer_indent (&writer);
10343 mono_json_writer_object_key(&writer, "class");
10344 mono_json_writer_printf (&writer, "\"%s\",\n", klass->name);
10346 mono_json_writer_indent (&writer);
10347 mono_json_writer_object_key(&writer, "namespace");
10348 mono_json_writer_printf (&writer, "\"%s\",\n", klass->name_space);
10350 mono_json_writer_indent (&writer);
10351 mono_json_writer_object_key(&writer, "wrapper_type");
10352 mono_json_writer_printf (&writer, "\"%s\",\n", get_wrapper_type_name(method->wrapper_type));
10354 mono_json_writer_indent_pop (&writer);
10355 mono_json_writer_indent (&writer);
10356 mono_json_writer_object_end (&writer);
10357 mono_json_writer_printf (&writer, ",\n");
10360 mono_json_writer_indent_pop (&writer);
10361 mono_json_writer_indent (&writer);
10362 mono_json_writer_array_end (&writer);
10363 mono_json_writer_printf (&writer, ",\n");
10367 mono_json_writer_indent_push (&writer);
10368 mono_json_writer_indent (&writer);
10369 mono_json_writer_object_key(&writer, "plt");
10370 mono_json_writer_array_begin (&writer);
10372 for (i = 0; i < acfg->plt_offset; ++i) {
10373 MonoPltEntry *plt_entry = NULL;
10378 * The first plt entry is unused.
10382 plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
10383 ji = plt_entry->ji;
10385 mono_json_writer_indent (&writer);
10386 mono_json_writer_printf (&writer, "{ ");
10387 mono_json_writer_object_key(&writer, "symbol");
10388 mono_json_writer_printf (&writer, "\"%s\" },\n", plt_entry->symbol);
10391 mono_json_writer_indent_pop (&writer);
10392 mono_json_writer_indent (&writer);
10393 mono_json_writer_array_end (&writer);
10394 mono_json_writer_printf (&writer, ",\n");
10399 mono_json_writer_indent_push (&writer);
10400 mono_json_writer_indent (&writer);
10401 mono_json_writer_object_key(&writer, "got");
10402 mono_json_writer_array_begin (&writer);
10404 mono_json_writer_indent_push (&writer);
10405 for (i = 0; i < acfg->got_info.got_patches->len; ++i) {
10406 MonoJumpInfo *ji = g_ptr_array_index (acfg->got_info.got_patches, i);
10408 mono_json_writer_indent (&writer);
10409 mono_json_writer_printf (&writer, "{ ");
10410 mono_json_writer_object_key(&writer, "patch_name");
10411 mono_json_writer_printf (&writer, "\"%s\" },\n", get_patch_name (ji->type));
10414 mono_json_writer_indent_pop (&writer);
10415 mono_json_writer_indent (&writer);
10416 mono_json_writer_array_end (&writer);
10417 mono_json_writer_printf (&writer, ",\n");
10420 mono_json_writer_indent_pop (&writer);
10421 mono_json_writer_indent (&writer);
10422 mono_json_writer_object_end (&writer);
10424 dumpname = g_strdup_printf ("%s.json", g_path_get_basename (acfg->image->name));
10425 dumpfile = fopen (dumpname, "w+");
10428 fprintf (dumpfile, "%s", writer.text->str);
10431 mono_json_writer_destroy (&writer);
10434 static const char *preinited_jit_icalls[] = {
10435 "mono_aot_init_llvm_method",
10436 "mono_aot_init_gshared_method_this",
10437 "mono_aot_init_gshared_method_mrgctx",
10438 "mono_aot_init_gshared_method_vtable",
10439 "mono_llvm_throw_corlib_exception",
10440 "mono_init_vtable_slot",
10441 "mono_helper_ldstr_mscorlib"
10445 add_preinit_got_slots (MonoAotCompile *acfg)
10451 * Allocate the first few GOT entries to information which is needed frequently, or it is needed
10452 * during method initialization etc.
10455 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
10456 ji->type = MONO_PATCH_INFO_IMAGE;
10457 ji->data.image = acfg->image;
10458 get_got_offset (acfg, FALSE, ji);
10459 get_got_offset (acfg, TRUE, ji);
10461 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
10462 ji->type = MONO_PATCH_INFO_MSCORLIB_GOT_ADDR;
10463 get_got_offset (acfg, FALSE, ji);
10464 get_got_offset (acfg, TRUE, ji);
10466 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
10467 ji->type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
10468 get_got_offset (acfg, FALSE, ji);
10469 get_got_offset (acfg, TRUE, ji);
10471 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
10472 ji->type = MONO_PATCH_INFO_GC_NURSERY_START;
10473 get_got_offset (acfg, FALSE, ji);
10474 get_got_offset (acfg, TRUE, ji);
10476 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
10477 ji->type = MONO_PATCH_INFO_AOT_MODULE;
10478 get_got_offset (acfg, FALSE, ji);
10479 get_got_offset (acfg, TRUE, ji);
10481 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
10482 ji->type = MONO_PATCH_INFO_GC_NURSERY_BITS;
10483 get_got_offset (acfg, FALSE, ji);
10484 get_got_offset (acfg, TRUE, ji);
10486 for (i = 0; i < TLS_KEY_NUM; i++) {
10487 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
10488 ji->type = MONO_PATCH_INFO_GET_TLS_TRAMP;
10489 ji->data.index = i;
10490 get_got_offset (acfg, FALSE, ji);
10491 get_got_offset (acfg, TRUE, ji);
10493 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
10494 ji->type = MONO_PATCH_INFO_SET_TLS_TRAMP;
10495 ji->data.index = i;
10496 get_got_offset (acfg, FALSE, ji);
10497 get_got_offset (acfg, TRUE, ji);
10500 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
10501 ji->type = MONO_PATCH_INFO_JIT_THREAD_ATTACH;
10502 get_got_offset (acfg, FALSE, ji);
10503 get_got_offset (acfg, TRUE, ji);
10505 for (i = 0; i < sizeof (preinited_jit_icalls) / sizeof (char*); ++i) {
10506 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
10507 ji->type = MONO_PATCH_INFO_INTERNAL_METHOD;
10508 ji->data.name = preinited_jit_icalls [i];
10509 get_got_offset (acfg, FALSE, ji);
10510 get_got_offset (acfg, TRUE, ji);
10513 acfg->nshared_got_entries = acfg->got_offset;
10517 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
10519 MonoImage *image = ass->image;
10522 MonoAotCompile *acfg;
10523 char *outfile_name, *tmp_outfile_name, *p;
10524 char llvm_stats_msg [256];
10528 acfg = acfg_create (ass, opts);
10530 memset (&acfg->aot_opts, 0, sizeof (acfg->aot_opts));
10531 acfg->aot_opts.write_symbols = TRUE;
10532 acfg->aot_opts.ntrampolines = 4096;
10533 acfg->aot_opts.nrgctx_trampolines = 4096;
10534 acfg->aot_opts.nimt_trampolines = 512;
10535 acfg->aot_opts.nrgctx_fetch_trampolines = 128;
10536 acfg->aot_opts.ngsharedvt_arg_trampolines = 512;
10537 acfg->aot_opts.llvm_path = g_strdup ("");
10538 acfg->aot_opts.temp_path = g_strdup ("");
10540 acfg->aot_opts.use_trampolines_page = TRUE;
10543 mono_aot_parse_options (aot_options, &acfg->aot_opts);
10545 if (acfg->aot_opts.logfile) {
10546 acfg->logfile = fopen (acfg->aot_opts.logfile, "a+");
10549 if (acfg->aot_opts.data_outfile) {
10550 acfg->data_outfile = fopen (acfg->aot_opts.data_outfile, "w+");
10551 if (!acfg->data_outfile) {
10552 aot_printerrf (acfg, "Unable to create file '%s': %s\n", acfg->aot_opts.data_outfile, strerror (errno));
10555 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_SEPARATE_DATA);
10558 //acfg->aot_opts.print_skipped_methods = TRUE;
10560 #if !defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
10561 if (acfg->opts & MONO_OPT_GSHAREDVT) {
10562 aot_printerrf (acfg, "-O=gsharedvt not supported on this platform.\n");
10565 if (acfg->aot_opts.llvm_only) {
10566 aot_printerrf (acfg, "--aot=llvmonly requires a runtime that supports gsharedvt.\n");
10570 if (acfg->aot_opts.llvm_only || mono_aot_mode_is_full (&acfg->aot_opts) || mono_aot_mode_is_hybrid (&acfg->aot_opts))
10571 acfg->opts |= MONO_OPT_GSHAREDVT;
10574 #if !defined(ENABLE_LLVM)
10575 if (acfg->aot_opts.llvm_only) {
10576 aot_printerrf (acfg, "--aot=llvmonly requires a runtime compiled with llvm support.\n");
10581 if (acfg->opts & MONO_OPT_GSHAREDVT)
10582 mono_set_generic_sharing_vt_supported (TRUE);
10584 aot_printf (acfg, "Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
10586 generate_aotid ((guint8*) &acfg->image->aotid);
10588 char *aotid = mono_guid_to_string (acfg->image->aotid);
10589 aot_printf (acfg, "AOTID %s\n", aotid);
10592 #ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
10593 if (mono_aot_mode_is_full (&acfg->aot_opts)) {
10594 aot_printerrf (acfg, "--aot=full is not supported on this platform.\n");
10599 if (acfg->aot_opts.direct_pinvoke && !acfg->aot_opts.static_link) {
10600 aot_printerrf (acfg, "The 'direct-pinvoke' AOT option also requires the 'static' AOT option.\n");
10604 if (acfg->aot_opts.static_link)
10605 acfg->aot_opts.asm_writer = TRUE;
10607 if (acfg->aot_opts.soft_debug) {
10608 MonoDebugOptions *opt = mini_get_debug_options ();
10610 opt->mdb_optimizations = TRUE;
10611 opt->gen_sdb_seq_points = TRUE;
10613 if (!mono_debug_enabled ()) {
10614 aot_printerrf (acfg, "The soft-debug AOT option requires the --debug option.\n");
10617 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_DEBUG);
10620 if (mono_use_llvm || acfg->aot_opts.llvm) {
10622 acfg->aot_opts.asm_writer = TRUE;
10623 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_WITH_LLVM);
10625 if (acfg->aot_opts.soft_debug) {
10626 aot_printerrf (acfg, "The 'soft-debug' option is not supported when compiling with LLVM.\n");
10632 if (acfg->aot_opts.asm_only && !acfg->aot_opts.llvm_outfile) {
10633 aot_printerrf (acfg, "Compiling with LLVM and the asm-only option requires the llvm-outfile= option.\n");
10638 if (mono_aot_mode_is_full (&acfg->aot_opts))
10639 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_FULL_AOT);
10641 if (mono_threads_is_coop_enabled ())
10642 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_SAFEPOINTS);
10644 if (acfg->aot_opts.instances_logfile_path) {
10645 acfg->instances_logfile = fopen (acfg->aot_opts.instances_logfile_path, "w");
10646 if (!acfg->instances_logfile) {
10647 aot_printerrf (acfg, "Unable to create logfile: '%s'.\n", acfg->aot_opts.instances_logfile_path);
10652 load_profile_files (acfg);
10654 acfg->num_trampolines [MONO_AOT_TRAMP_SPECIFIC] = mono_aot_mode_is_full (&acfg->aot_opts) ? acfg->aot_opts.ntrampolines : 0;
10655 #ifdef MONO_ARCH_GSHARED_SUPPORTED
10656 acfg->num_trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = mono_aot_mode_is_full (&acfg->aot_opts) ? acfg->aot_opts.nrgctx_trampolines : 0;
10658 acfg->num_trampolines [MONO_AOT_TRAMP_IMT] = mono_aot_mode_is_full (&acfg->aot_opts) ? acfg->aot_opts.nimt_trampolines : 0;
10659 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
10660 if (acfg->opts & MONO_OPT_GSHAREDVT)
10661 acfg->num_trampolines [MONO_AOT_TRAMP_GSHAREDVT_ARG] = mono_aot_mode_is_full (&acfg->aot_opts) ? acfg->aot_opts.ngsharedvt_arg_trampolines : 0;
10664 acfg->temp_prefix = mono_img_writer_get_temp_label_prefix (NULL);
10668 if (mono_use_llvm || acfg->aot_opts.llvm) {
10671 * Emit all LLVM code into a separate assembly/object file and link with it
10674 if (!acfg->aot_opts.asm_only && acfg->llvm_owriter_supported) {
10675 acfg->llvm_owriter = TRUE;
10676 } else if (acfg->aot_opts.llvm_outfile) {
10677 int len = strlen (acfg->aot_opts.llvm_outfile);
10679 if (len >= 2 && acfg->aot_opts.llvm_outfile [len - 2] == '.' && acfg->aot_opts.llvm_outfile [len - 1] == 'o')
10680 acfg->llvm_owriter = TRUE;
10684 if (acfg->llvm && acfg->thumb_mixed)
10685 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_LLVM_THUMB);
10686 if (acfg->aot_opts.llvm_only)
10687 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_LLVM_ONLY);
10689 acfg->assembly_name_sym = g_strdup (acfg->image->assembly->aname.name);
10690 /* Get rid of characters which cannot occur in symbols */
10691 for (p = acfg->assembly_name_sym; *p; ++p) {
10692 if (!(isalnum (*p) || *p == '_'))
10696 acfg->global_prefix = g_strdup_printf ("mono_aot_%s", acfg->assembly_name_sym);
10697 acfg->plt_symbol = g_strdup_printf ("%s_plt", acfg->global_prefix);
10698 acfg->got_symbol = g_strdup_printf ("%s_got", acfg->global_prefix);
10700 acfg->llvm_got_symbol = g_strdup_printf ("%s_llvm_got", acfg->global_prefix);
10701 acfg->llvm_eh_frame_symbol = g_strdup_printf ("%s_eh_frame", acfg->global_prefix);
10704 acfg->method_index = 1;
10706 if (mono_aot_mode_is_full (&acfg->aot_opts) || mono_aot_mode_is_hybrid (&acfg->aot_opts))
10707 mono_set_partial_sharing_supported (TRUE);
10709 res = collect_methods (acfg);
10713 acfg->cfgs_size = acfg->methods->len + 32;
10714 acfg->cfgs = g_new0 (MonoCompile*, acfg->cfgs_size);
10716 /* PLT offset 0 is reserved for the PLT trampoline */
10717 acfg->plt_offset = 1;
10718 add_preinit_got_slots (acfg);
10723 mono_llvm_create_aot_module (acfg->image->assembly, acfg->global_prefix, TRUE, acfg->aot_opts.static_link, acfg->aot_opts.llvm_only);
10729 compile_methods (acfg);
10733 acfg->stats.jit_time = TV_ELAPSED (atv, btv);
10739 if (acfg->aot_opts.asm_only) {
10740 if (acfg->aot_opts.outfile) {
10741 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
10742 acfg->tmpbasename = g_strdup (acfg->tmpfname);
10744 acfg->tmpbasename = g_strdup_printf ("%s", acfg->image->name);
10745 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->tmpbasename);
10747 g_assert (acfg->aot_opts.llvm_outfile);
10748 acfg->llvm_sfile = g_strdup (acfg->aot_opts.llvm_outfile);
10749 if (acfg->llvm_owriter)
10750 acfg->llvm_ofile = g_strdup (acfg->aot_opts.llvm_outfile);
10752 acfg->llvm_sfile = g_strdup (acfg->aot_opts.llvm_outfile);
10754 acfg->tmpbasename = (strcmp (acfg->aot_opts.temp_path, "") == 0) ?
10755 g_strdup_printf ("%s", "temp") :
10756 g_build_filename (acfg->aot_opts.temp_path, "temp", NULL);
10758 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->tmpbasename);
10759 acfg->llvm_sfile = g_strdup_printf ("%s-llvm.s", acfg->tmpbasename);
10760 acfg->llvm_ofile = g_strdup_printf ("%s-llvm.o", acfg->tmpbasename);
10765 if (acfg->aot_opts.asm_only && !acfg->aot_opts.llvm_only) {
10766 if (acfg->aot_opts.outfile)
10767 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
10769 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->image->name);
10770 acfg->fp = fopen (acfg->tmpfname, "w+");
10772 int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
10773 acfg->fp = fdopen (i, "w+");
10775 if (acfg->fp == 0 && !acfg->aot_opts.llvm_only) {
10776 aot_printerrf (acfg, "Unable to open file '%s': %s\n", acfg->tmpfname, strerror (errno));
10780 acfg->w = mono_img_writer_create (acfg->fp, FALSE);
10782 tmp_outfile_name = NULL;
10783 outfile_name = NULL;
10785 /* Compute symbols for methods */
10786 for (i = 0; i < acfg->nmethods; ++i) {
10787 if (acfg->cfgs [i]) {
10788 MonoCompile *cfg = acfg->cfgs [i];
10789 int method_index = get_method_index (acfg, cfg->orig_method);
10791 if (COMPILE_LLVM (cfg))
10792 cfg->asm_symbol = g_strdup_printf ("%s%s", acfg->llvm_label_prefix, cfg->llvm_method_name);
10793 else if (acfg->global_symbols || acfg->llvm)
10794 cfg->asm_symbol = get_debug_sym (cfg->orig_method, "", acfg->method_label_hash);
10796 cfg->asm_symbol = g_strdup_printf ("%s%sm_%x", acfg->temp_prefix, acfg->llvm_label_prefix, method_index);
10797 cfg->asm_debug_symbol = cfg->asm_symbol;
10801 if (acfg->aot_opts.dwarf_debug && acfg->aot_opts.gnu_asm) {
10803 * CLANG supports GAS .file/.loc directives, so emit line number information this way
10805 acfg->gas_line_numbers = TRUE;
10808 #ifdef EMIT_DWARF_INFO
10809 if ((!acfg->aot_opts.nodebug || acfg->aot_opts.dwarf_debug) && acfg->has_jitted_code) {
10810 if (acfg->aot_opts.dwarf_debug && !mono_debug_enabled ()) {
10811 aot_printerrf (acfg, "The dwarf AOT option requires the --debug option.\n");
10814 acfg->dwarf = mono_dwarf_writer_create (acfg->w, NULL, 0, !acfg->gas_line_numbers);
10816 #endif /* EMIT_DWARF_INFO */
10819 mono_img_writer_emit_start (acfg->w);
10822 mono_dwarf_writer_emit_base_info (acfg->dwarf, g_path_get_basename (acfg->image->name), mono_unwind_get_cie_program ());
10828 emit_extra_methods (acfg);
10830 emit_trampolines (acfg);
10832 emit_class_name_table (acfg);
10834 emit_got_info (acfg, FALSE);
10836 emit_got_info (acfg, TRUE);
10838 emit_exception_info (acfg);
10840 emit_unwind_info (acfg);
10842 emit_class_info (acfg);
10846 emit_image_table (acfg);
10852 * The managed allocators are GC specific, so can't use an AOT image created by one GC
10855 const char *gc_name = mono_gc_get_gc_name ();
10856 acfg->gc_name_offset = add_to_blob (acfg, (guint8*)gc_name, strlen (gc_name) + 1);
10861 emit_objc_selectors (acfg);
10863 emit_globals (acfg);
10865 emit_file_info (acfg);
10867 emit_library_info (acfg);
10870 emit_dwarf_info (acfg);
10871 mono_dwarf_writer_close (acfg->dwarf);
10874 emit_mem_end (acfg);
10876 if (acfg->need_pt_gnu_stack) {
10877 /* This is required so the .so doesn't have an executable stack */
10878 /* The bin writer already emits this */
10879 fprintf (acfg->fp, "\n.section .note.GNU-stack,\"\",@progbits\n");
10882 if (acfg->aot_opts.data_outfile)
10883 fclose (acfg->data_outfile);
10889 res = emit_llvm_file (acfg);
10897 acfg->stats.gen_time = TV_ELAPSED (atv, btv);
10900 sprintf (llvm_stats_msg, ", LLVM: %d (%d%%)", acfg->stats.llvm_count, acfg->stats.mcount ? (acfg->stats.llvm_count * 100) / acfg->stats.mcount : 100);
10902 strcpy (llvm_stats_msg, "");
10904 all_sizes = acfg->stats.code_size + acfg->stats.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;
10906 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\n",
10907 (int)acfg->stats.code_size, (int)(acfg->stats.code_size * 100 / all_sizes),
10908 (int)acfg->stats.info_size, (int)(acfg->stats.info_size * 100 / all_sizes),
10909 (int)acfg->stats.ex_info_size, (int)(acfg->stats.ex_info_size * 100 / all_sizes),
10910 (int)acfg->stats.unwind_info_size, (int)(acfg->stats.unwind_info_size * 100 / all_sizes),
10911 (int)acfg->stats.class_info_size, (int)(acfg->stats.class_info_size * 100 / all_sizes),
10912 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,
10913 (int)acfg->stats.got_info_size, (int)(acfg->stats.got_info_size * 100 / all_sizes),
10914 (int)acfg->stats.offsets_size, (int)(acfg->stats.offsets_size * 100 / all_sizes),
10915 (int)(acfg->got_offset * sizeof (gpointer)));
10916 aot_printf (acfg, "Compiled: %d/%d (%d%%)%s, No GOT slots: %d (%d%%), Direct calls: %d (%d%%)\n",
10917 acfg->stats.ccount, acfg->stats.mcount, acfg->stats.mcount ? (acfg->stats.ccount * 100) / acfg->stats.mcount : 100,
10919 acfg->stats.methods_without_got_slots, acfg->stats.mcount ? (acfg->stats.methods_without_got_slots * 100) / acfg->stats.mcount : 100,
10920 acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
10921 if (acfg->stats.genericcount)
10922 aot_printf (acfg, "%d methods are generic (%d%%)\n", acfg->stats.genericcount, acfg->stats.mcount ? (acfg->stats.genericcount * 100) / acfg->stats.mcount : 100);
10923 if (acfg->stats.abscount)
10924 aot_printf (acfg, "%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
10925 if (acfg->stats.lmfcount)
10926 aot_printf (acfg, "%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
10927 if (acfg->stats.ocount)
10928 aot_printf (acfg, "%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
10932 res = mono_img_writer_emit_writeout (acfg->w);
10937 res = compile_asm (acfg);
10944 acfg->stats.link_time = TV_ELAPSED (atv, btv);
10946 if (acfg->aot_opts.stats) {
10949 aot_printf (acfg, "GOT slot distribution:\n");
10950 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
10951 if (acfg->stats.got_slot_types [i])
10952 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]);
10953 aot_printf (acfg, "\nMethod stats:\n");
10954 aot_printf (acfg, "\tNormal: %d\n", acfg->stats.method_categories [METHOD_CAT_NORMAL]);
10955 aot_printf (acfg, "\tInstance: %d\n", acfg->stats.method_categories [METHOD_CAT_INST]);
10956 aot_printf (acfg, "\tGSharedvt: %d\n", acfg->stats.method_categories [METHOD_CAT_GSHAREDVT]);
10957 aot_printf (acfg, "\tWrapper: %d\n", acfg->stats.method_categories [METHOD_CAT_WRAPPER]);
10960 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);
10962 if (acfg->aot_opts.dump_json)
10975 mono_aot_readonly_field_override (MonoClassField *field)
10981 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
10987 mono_aot_is_shared_got_offset (int offset)