2 * aot-compiler.c: mono Ahead of Time compiler
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2002 Ximian, Inc.
9 * Copyright 2003-2011 Novell, Inc
10 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
13 /* Remaining AOT-only work:
14 * - optimize the trampolines, generate more code in the arch files.
15 * - make things more consistent with how elf works, for example, use ELF
17 * Remaining generics sharing work:
18 * - optimize the size of the data which is encoded.
19 * - optimize the runtime loading of data:
20 * - the trampoline code calls mono_jit_info_table_find () to find the rgctx,
21 * which loads the debugging+exception handling info for the method. This is a
22 * huge waste of time and code, since the rgctx structure is currently empty.
25 #include <sys/types.h>
46 #include <mono/metadata/abi-details.h>
47 #include <mono/metadata/tabledefs.h>
48 #include <mono/metadata/class.h>
49 #include <mono/metadata/object.h>
50 #include <mono/metadata/tokentype.h>
51 #include <mono/metadata/appdomain.h>
52 #include <mono/metadata/debug-helpers.h>
53 #include <mono/metadata/assembly.h>
54 #include <mono/metadata/metadata-internals.h>
55 #include <mono/metadata/marshal.h>
56 #include <mono/metadata/gc-internal.h>
57 #include <mono/metadata/monitor.h>
58 #include <mono/metadata/mempool-internals.h>
59 #include <mono/metadata/mono-endian.h>
60 #include <mono/metadata/threads-types.h>
61 #include <mono/utils/mono-logger-internal.h>
62 #include <mono/utils/mono-compiler.h>
63 #include <mono/utils/mono-time.h>
64 #include <mono/utils/mono-mmap.h>
67 #include "image-writer.h"
68 #include "dwarfwriter.h"
71 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
73 #if defined(__linux__) || defined(__native_client_codegen__)
74 #define RODATA_SECT ".rodata"
75 #elif defined(TARGET_MACH)
76 #define RODATA_SECT ".section __TEXT, __const"
78 #define RODATA_SECT ".text"
81 #define TV_DECLARE(name) gint64 name
82 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
83 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
86 #define SHARED_EXT ".dll"
87 #elif defined(__ppc__) && defined(TARGET_MACH)
88 #define SHARED_EXT ".dylib"
89 #elif defined(TARGET_MACH) && defined(TARGET_X86) && !defined(__native_client_codegen__)
90 #define SHARED_EXT ".dylib"
91 #elif defined(TARGET_MACH) && defined(TARGET_AMD64) && !defined(__native_client_codegen__)
92 #define SHARED_EXT ".dylib"
94 #define SHARED_EXT ".so"
97 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
98 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
99 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
101 /* predefined values for static readonly fields without needed to run the .cctor */
102 typedef struct _ReadOnlyValue ReadOnlyValue;
103 struct _ReadOnlyValue {
106 int type; /* to be used later for typechecking to prevent user errors */
115 static ReadOnlyValue *readonly_values;
117 typedef struct MonoAotOptions {
120 gboolean write_symbols;
121 gboolean metadata_only;
122 gboolean bind_to_runtime_version;
125 gboolean static_link;
129 gboolean dwarf_debug;
131 gboolean log_generics;
132 gboolean log_instances;
133 gboolean direct_pinvoke;
134 gboolean direct_icalls;
135 gboolean no_direct_calls;
136 gboolean use_trampolines_page;
137 gboolean no_instances;
141 int nrgctx_trampolines;
142 int nimt_trampolines;
143 int ngsharedvt_arg_trampolines;
144 int nrgctx_fetch_trampolines;
145 gboolean print_skipped_methods;
151 char *instances_logfile_path;
155 typedef struct MonoAotStats {
156 int ccount, mcount, lmfcount, abscount, gcount, ocount, genericcount;
157 int code_size, info_size, ex_info_size, unwind_info_size, got_size, class_info_size, got_info_size, plt_size;
158 int methods_without_got_slots, direct_calls, all_calls, llvm_count;
159 int got_slots, offsets_size;
160 int got_slot_types [MONO_PATCH_INFO_NONE];
161 int got_slot_info_sizes [MONO_PATCH_INFO_NONE];
162 int jit_time, gen_time, link_time;
165 typedef struct MonoAotCompile {
168 GHashTable *method_indexes;
169 GHashTable *method_depth;
172 GHashTable **patch_to_plt_entry;
173 GHashTable *plt_offset_to_entry;
174 GHashTable *patch_to_got_offset;
175 GHashTable **patch_to_got_offset_by_type;
176 GPtrArray *got_patches;
177 GHashTable *image_hash;
178 GHashTable *method_to_cfg;
179 GHashTable *token_info_hash;
180 GHashTable *method_to_pinvoke_import;
181 GPtrArray *extra_methods;
182 GPtrArray *image_table;
184 GPtrArray *method_order;
185 GHashTable *export_names;
186 /* Maps MonoClass* -> blob offset */
187 GHashTable *klass_blob_hash;
188 /* Maps MonoMethod* -> blob offset */
189 GHashTable *method_blob_hash;
190 guint32 *plt_got_info_offsets;
191 guint32 got_offset, plt_offset, plt_got_offset_base;
192 guint32 final_got_size;
193 /* Number of GOT entries reserved for trampolines */
194 guint32 num_trampoline_got_entries;
195 guint32 tramp_page_size;
197 guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
198 guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
199 guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
200 guint32 tramp_page_code_offsets [MONO_AOT_TRAMP_NUM];
202 MonoAotOptions aot_opts;
206 MonoMemPool *mempool;
209 char *static_linking_symbol;
211 gboolean use_bin_writer;
212 gboolean gas_line_numbers;
214 MonoDwarfWriter *dwarf;
219 GHashTable *unwind_info_offsets;
220 GPtrArray *unwind_ops;
221 guint32 unwind_info_offset;
222 char *got_symbol_base;
225 char *methods_symbol;
226 GHashTable *method_label_hash;
227 const char *temp_prefix;
228 const char *user_symbol_prefix;
229 const char *llvm_label_prefix;
230 const char *inst_directive;
231 guint32 label_generator;
233 MonoAotFileFlags flags;
234 MonoDynamicStream blob;
235 MonoClass **typespec_classes;
238 char *assembly_name_sym;
239 GHashTable *plt_entry_debug_sym_cache;
240 gboolean thumb_mixed, need_no_dead_strip, need_pt_gnu_stack;
241 GHashTable *ginst_hash;
242 GHashTable *dwarf_ln_filenames;
243 gboolean global_symbols;
244 gboolean direct_method_addresses;
245 int objc_selector_index, objc_selector_index_2;
246 GPtrArray *objc_selectors;
247 GHashTable *objc_selector_to_index;
249 FILE *instances_logfile;
254 char *symbol, *llvm_symbol, *debug_sym;
256 gboolean jit_used, llvm_used;
259 #define mono_acfg_lock(acfg) mono_mutex_lock (&((acfg)->mutex))
260 #define mono_acfg_unlock(acfg) mono_mutex_unlock (&((acfg)->mutex))
262 /* This points to the current acfg in LLVM mode */
263 static MonoAotCompile *llvm_acfg;
265 #ifdef HAVE_ARRAY_ELEM_INIT
266 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
267 #define MSGSTRFIELD1(line) str##line
268 static const struct msgstr_t {
269 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
270 #include "patch-info.h"
273 #define PATCH_INFO(a,b) b,
274 #include "patch-info.h"
277 static const gint16 opidx [] = {
278 #define PATCH_INFO(a,b) [MONO_PATCH_INFO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
279 #include "patch-info.h"
283 static G_GNUC_UNUSED const char*
284 get_patch_name (int info)
286 return (const char*)&opstr + opidx [info];
290 #define PATCH_INFO(a,b) b,
291 static const char* const
292 patch_types [MONO_PATCH_INFO_NUM + 1] = {
293 #include "patch-info.h"
297 static G_GNUC_UNUSED const char*
298 get_patch_name (int info)
300 return patch_types [info];
306 get_plt_entry_debug_sym (MonoAotCompile *acfg, MonoJumpInfo *ji, GHashTable *cache);
309 aot_printf (MonoAotCompile *acfg, const gchar *format, ...)
315 output = acfg->logfile;
319 va_start (args, format);
320 vfprintf (output, format, args);
325 aot_printerrf (MonoAotCompile *acfg, const gchar *format, ...)
331 output = acfg->logfile;
335 va_start (args, format);
336 vfprintf (output, format, args);
340 /* Wrappers around the image writer functions */
343 emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
345 img_writer_emit_section_change (acfg->w, section_name, subsection_index);
349 emit_push_section (MonoAotCompile *acfg, const char *section_name, int subsection)
351 img_writer_emit_push_section (acfg->w, section_name, subsection);
355 emit_pop_section (MonoAotCompile *acfg)
357 img_writer_emit_pop_section (acfg->w);
361 emit_local_symbol (MonoAotCompile *acfg, const char *name, const char *end_label, gboolean func)
363 img_writer_emit_local_symbol (acfg->w, name, end_label, func);
367 emit_label (MonoAotCompile *acfg, const char *name)
369 img_writer_emit_label (acfg->w, name);
373 emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
375 img_writer_emit_bytes (acfg->w, buf, size);
379 emit_string (MonoAotCompile *acfg, const char *value)
381 img_writer_emit_string (acfg->w, value);
385 emit_line (MonoAotCompile *acfg)
387 img_writer_emit_line (acfg->w);
391 emit_alignment (MonoAotCompile *acfg, int size)
393 img_writer_emit_alignment (acfg->w, size);
397 emit_pointer_unaligned (MonoAotCompile *acfg, const char *target)
399 img_writer_emit_pointer_unaligned (acfg->w, target);
403 emit_pointer (MonoAotCompile *acfg, const char *target)
405 img_writer_emit_pointer (acfg->w, target);
409 emit_pointer_2 (MonoAotCompile *acfg, const char *prefix, const char *target)
411 if (prefix [0] != '\0') {
412 char *s = g_strdup_printf ("%s%s", prefix, target);
413 img_writer_emit_pointer (acfg->w, s);
416 img_writer_emit_pointer (acfg->w, target);
421 emit_int16 (MonoAotCompile *acfg, int value)
423 img_writer_emit_int16 (acfg->w, value);
427 emit_int32 (MonoAotCompile *acfg, int value)
429 img_writer_emit_int32 (acfg->w, value);
433 emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
435 img_writer_emit_symbol_diff (acfg->w, end, start, offset);
439 emit_zero_bytes (MonoAotCompile *acfg, int num)
441 img_writer_emit_zero_bytes (acfg->w, num);
445 emit_byte (MonoAotCompile *acfg, guint8 val)
447 img_writer_emit_byte (acfg->w, val);
450 #ifdef __native_client_codegen__
452 emit_nacl_call_alignment (MonoAotCompile *acfg)
454 img_writer_emit_nacl_call_alignment (acfg->w);
458 static G_GNUC_UNUSED void
459 emit_global_inner (MonoAotCompile *acfg, const char *name, gboolean func)
461 img_writer_emit_global (acfg->w, name, func);
465 emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
467 if (acfg->aot_opts.no_dlsym) {
468 g_ptr_array_add (acfg->globals, g_strdup (name));
469 img_writer_emit_local_symbol (acfg->w, name, NULL, func);
471 img_writer_emit_global (acfg->w, name, func);
476 emit_symbol_size (MonoAotCompile *acfg, const char *name, const char *end_label)
478 img_writer_emit_symbol_size (acfg->w, name, end_label);
482 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
484 img_writer_emit_section_change (acfg->w, RODATA_SECT, 1);
486 /* On apple, all symbols need to be aligned to avoid warnings from ld */
487 emit_alignment (acfg, 4);
489 img_writer_emit_label (acfg->w, name);
490 img_writer_emit_string (acfg->w, value);
493 static G_GNUC_UNUSED void
494 emit_uleb128 (MonoAotCompile *acfg, guint32 value)
497 guint8 b = value & 0x7f;
499 if (value != 0) /* more bytes to come */
505 static G_GNUC_UNUSED void
506 emit_sleb128 (MonoAotCompile *acfg, gint64 value)
509 gboolean negative = (value < 0);
516 /* the following is unnecessary if the
517 * implementation of >>= uses an arithmetic rather
518 * than logical shift for a signed left operand
522 value |= - ((gint64)1 <<(size - 7));
523 /* sign bit of byte is second high order bit (0x40) */
524 if ((value == 0 && !(byte & 0x40)) ||
525 (value == -1 && (byte & 0x40)))
529 emit_byte (acfg, byte);
533 static G_GNUC_UNUSED void
534 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
539 guint8 b = value & 0x7f;
541 if (value != 0) /* more bytes to come */
549 static G_GNUC_UNUSED void
550 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
553 gboolean negative = (value < 0);
561 /* the following is unnecessary if the
562 * implementation of >>= uses an arithmetic rather
563 * than logical shift for a signed left operand
567 value |= - (1 <<(size - 7));
568 /* sign bit of byte is second high order bit (0x40) */
569 if ((value == 0 && !(byte & 0x40)) ||
570 (value == -1 && (byte & 0x40)))
581 emit_unset_mode (MonoAotCompile *acfg)
583 img_writer_emit_unset_mode (acfg->w);
586 static G_GNUC_UNUSED void
587 emit_set_thumb_mode (MonoAotCompile *acfg)
589 emit_unset_mode (acfg);
590 fprintf (acfg->fp, ".code 16\n");
593 static G_GNUC_UNUSED void
594 emit_set_arm_mode (MonoAotCompile *acfg)
596 emit_unset_mode (acfg);
597 fprintf (acfg->fp, ".code 32\n");
601 emit_code_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
606 g_assert (size % 4 == 0);
607 emit_unset_mode (acfg);
608 for (i = 0; i < size; i += 4)
609 fprintf (acfg->fp, "%s 0x%x\n", acfg->inst_directive, *(guint32*)(buf + i));
611 emit_bytes (acfg, buf, size);
615 /* ARCHITECTURE SPECIFIC CODE */
617 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC)
618 #define EMIT_DWARF_INFO 1
621 #if defined(TARGET_ARM)
622 #define AOT_FUNC_ALIGNMENT 4
624 #define AOT_FUNC_ALIGNMENT 16
626 #if (defined(TARGET_X86) || defined(TARGET_AMD64)) && defined(__native_client_codegen__)
627 #undef AOT_FUNC_ALIGNMENT
628 #define AOT_FUNC_ALIGNMENT 32
631 #if defined(TARGET_POWERPC64) && !defined(__mono_ilp32__)
632 #define PPC_LD_OP "ld"
633 #define PPC_LDX_OP "ldx"
635 #define PPC_LD_OP "lwz"
636 #define PPC_LDX_OP "lwzx"
640 #define AOT_TARGET_STR "AMD64"
645 #define AOT_TARGET_STR "ARM (MACH)"
647 #define AOT_TARGET_STR "ARM (!MACH)"
653 #define AOT_TARGET_STR "ARM64 (MACH)"
655 #define AOT_TARGET_STR "ARM64 (!MACH)"
659 #ifdef TARGET_POWERPC64
660 #ifdef __mono_ilp32__
661 #define AOT_TARGET_STR "POWERPC64 (mono ilp32)"
663 #define AOT_TARGET_STR "POWERPC64 (!mono ilp32)"
666 #ifdef TARGET_POWERPC
667 #ifdef __mono_ilp32__
668 #define AOT_TARGET_STR "POWERPC (mono ilp32)"
670 #define AOT_TARGET_STR "POWERPC (!mono ilp32)"
677 #define AOT_TARGET_STR "X86 (WIN32)"
678 #elif defined(__native_client_codegen__)
679 #define AOT_TARGET_STR "X86 (native client codegen)"
681 #define AOT_TARGET_STR "X86 (!native client codegen)"
685 #ifndef AOT_TARGET_STR
686 #define AOT_TARGET_STR ""
690 arch_init (MonoAotCompile *acfg)
692 acfg->llc_args = g_string_new ("");
693 acfg->as_args = g_string_new ("");
696 * The prefix LLVM likes to put in front of symbol names on darwin.
697 * The mach-os specs require this for globals, but LLVM puts them in front of all
698 * symbols. We need to handle this, since we need to refer to LLVM generated
701 acfg->llvm_label_prefix = "";
702 acfg->user_symbol_prefix = "";
704 #if defined(TARGET_AMD64)
705 g_string_append (acfg->llc_args, " -march=x86-64 -mattr=sse4.1");
709 if (acfg->aot_opts.mtriple && strstr (acfg->aot_opts.mtriple, "darwin")) {
710 g_string_append (acfg->llc_args, "-mattr=+v6");
713 g_string_append (acfg->llc_args, " -mattr=+vfp2,-neon,+d16");
714 g_string_append (acfg->as_args, " -mfpu=vfp3");
716 g_string_append (acfg->llc_args, " -soft-float");
719 if (acfg->aot_opts.mtriple && strstr (acfg->aot_opts.mtriple, "thumb"))
720 acfg->thumb_mixed = TRUE;
722 if (acfg->aot_opts.mtriple)
723 mono_arch_set_target (acfg->aot_opts.mtriple);
727 acfg->inst_directive = ".inst";
728 if (acfg->aot_opts.mtriple)
729 mono_arch_set_target (acfg->aot_opts.mtriple);
733 acfg->user_symbol_prefix = "_";
734 acfg->llvm_label_prefix = "_";
735 acfg->inst_directive = ".word";
736 acfg->need_no_dead_strip = TRUE;
737 acfg->aot_opts.gnu_asm = TRUE;
740 #if defined(__linux__) && !defined(TARGET_ARM)
741 acfg->need_pt_gnu_stack = TRUE;
745 acfg->direct_method_addresses = TRUE;
746 acfg->global_symbols = TRUE;
752 #include "../../../mono-extensions/mono/mini/aot-compiler-arm64.c"
756 #ifdef MONO_ARCH_AOT_SUPPORTED
758 * arch_emit_direct_call:
760 * Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
764 arch_emit_direct_call (MonoAotCompile *acfg, const char *target, gboolean external, gboolean thumb, MonoJumpInfo *ji, int *call_size)
766 #if defined(TARGET_X86) || defined(TARGET_AMD64)
767 /* Need to make sure this is exactly 5 bytes long */
768 if (external && !acfg->use_bin_writer) {
769 emit_unset_mode (acfg);
770 fprintf (acfg->fp, "call %s\n", target);
772 emit_byte (acfg, '\xe8');
773 emit_symbol_diff (acfg, target, ".", -4);
776 #elif defined(TARGET_ARM)
777 if (acfg->use_bin_writer) {
784 img_writer_emit_reloc (acfg->w, R_ARM_CALL, target, -8);
785 emit_bytes (acfg, buf, 4);
787 emit_unset_mode (acfg);
789 fprintf (acfg->fp, "blx %s\n", target);
791 fprintf (acfg->fp, "bl %s\n", target);
794 #elif defined(TARGET_ARM64)
795 arm64_emit_direct_call (acfg, target, external, thumb, ji, call_size);
796 #elif defined(TARGET_POWERPC)
797 if (acfg->use_bin_writer) {
798 g_assert_not_reached ();
800 emit_unset_mode (acfg);
801 fprintf (acfg->fp, "bl %s\n", target);
805 g_assert_not_reached ();
812 * - we use an approach similar to the x86 abi: reserve a register (r30) to hold
814 * - The full-aot trampolines need access to the GOT of mscorlib, so we store
815 * in in the 2. slot of every GOT, and require every method to place the GOT
816 * address in r30, even when it doesn't access the GOT otherwise. This way,
817 * the trampolines can compute the mscorlib GOT address by loading 4(r30).
822 * PPC64 uses function descriptors which greatly complicate all code, since
823 * these are used very inconsistently in the runtime. Some functions like
824 * mono_compile_method () return ftn descriptors, while others like the
825 * trampoline creation functions do not.
826 * We assume that all GOT slots contain function descriptors, and create
827 * descriptors in aot-runtime.c when needed.
828 * The ppc64 abi uses r2 to hold the address of the TOC/GOT, which is loaded
829 * from function descriptors, we could do the same, but it would require
830 * rewriting all the ppc/aot code to handle function descriptors properly.
831 * So instead, we use the same approach as on PPC32.
832 * This is a horrible mess, but fixing it would probably lead to an even bigger
838 * - similar to the PPC32 design, we reserve EBX to hold the GOT pointer.
841 #ifdef MONO_ARCH_AOT_SUPPORTED
843 * arch_emit_got_offset:
845 * The memory pointed to by CODE should hold native code for computing the GOT
846 * address. Emit this code while patching it with the offset between code and
847 * the GOT. CODE_SIZE is set to the number of bytes emitted.
850 arch_emit_got_offset (MonoAotCompile *acfg, guint8 *code, int *code_size)
852 #if defined(TARGET_POWERPC64)
853 g_assert (!acfg->use_bin_writer);
854 emit_unset_mode (acfg);
856 * The ppc32 code doesn't seem to work on ppc64, the assembler complains about
857 * unsupported relocations. So we store the got address into the .Lgot_addr
858 * symbol which is in the text segment, compute its address, and load it.
860 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
861 fprintf (acfg->fp, "lis 0, (.Lgot_addr + 4 - .L%d)@h\n", acfg->label_generator);
862 fprintf (acfg->fp, "ori 0, 0, (.Lgot_addr + 4 - .L%d)@l\n", acfg->label_generator);
863 fprintf (acfg->fp, "add 30, 30, 0\n");
864 fprintf (acfg->fp, "%s 30, 0(30)\n", PPC_LD_OP);
865 acfg->label_generator ++;
867 #elif defined(TARGET_POWERPC)
868 g_assert (!acfg->use_bin_writer);
869 emit_unset_mode (acfg);
870 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
871 fprintf (acfg->fp, "lis 0, (%s + 4 - .L%d)@h\n", acfg->got_symbol, acfg->label_generator);
872 fprintf (acfg->fp, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg->got_symbol, acfg->label_generator);
873 acfg->label_generator ++;
876 guint32 offset = mono_arch_get_patch_offset (code);
877 emit_bytes (acfg, code, offset);
878 emit_symbol_diff (acfg, acfg->got_symbol, ".", offset);
880 *code_size = offset + 4;
885 * arch_emit_got_access:
887 * The memory pointed to by CODE should hold native code for loading a GOT
888 * slot. Emit this code while patching it so it accesses the GOT slot GOT_SLOT.
889 * CODE_SIZE is set to the number of bytes emitted.
892 arch_emit_got_access (MonoAotCompile *acfg, guint8 *code, int got_slot, int *code_size)
894 /* Emit beginning of instruction */
895 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
897 /* Emit the offset */
899 emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
900 *code_size = mono_arch_get_patch_offset (code) + 4;
901 #elif defined(TARGET_X86)
902 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
903 *code_size = mono_arch_get_patch_offset (code) + 4;
904 #elif defined(TARGET_ARM)
905 emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
906 *code_size = mono_arch_get_patch_offset (code) + 4;
907 #elif defined(TARGET_ARM64)
908 arm64_emit_got_access (acfg, code, got_slot, code_size);
909 #elif defined(TARGET_POWERPC)
915 ppc_load32 (code, ppc_r0, got_slot * sizeof (gpointer));
916 g_assert (code - buf == 8);
917 emit_bytes (acfg, buf, code - buf);
918 *code_size = code - buf;
921 g_assert_not_reached ();
927 #ifdef MONO_ARCH_AOT_SUPPORTED
929 * arch_emit_objc_selector_ref:
931 * Emit the implementation of OP_OBJC_GET_SELECTOR, which itself implements @selector(foo:) in objective-c.
934 arch_emit_objc_selector_ref (MonoAotCompile *acfg, guint8 *code, int index, int *code_size)
936 #if defined(TARGET_ARM)
939 int lindex = acfg->objc_selector_index_2 ++;
942 emit_bytes (acfg, code, 8);
944 sprintf (symbol1, "L_OBJC_SELECTOR_%d", lindex);
945 sprintf (symbol2, "L_OBJC_SELECTOR_REFERENCES_%d", index);
947 emit_label (acfg, symbol1);
948 img_writer_emit_unset_mode (acfg->w);
949 fprintf (acfg->fp, ".long %s-(%s+12)", symbol2, symbol1);
952 #elif defined(TARGET_ARM64)
953 arm64_emit_objc_selector_ref (acfg, code, index, code_size);
955 g_assert_not_reached ();
961 * arch_emit_plt_entry:
963 * Emit code for the PLT entry with index INDEX.
966 arch_emit_plt_entry (MonoAotCompile *acfg, int index)
968 #if defined(TARGET_X86)
969 guint32 offset = (acfg->plt_got_offset_base + index) * sizeof (gpointer);
970 #if defined(__default_codegen__)
971 /* jmp *<offset>(%ebx) */
972 emit_byte (acfg, 0xff);
973 emit_byte (acfg, 0xa3);
974 emit_int32 (acfg, offset);
975 /* Used by mono_aot_get_plt_info_offset */
976 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
977 #elif defined(__native_client_codegen__)
978 const guint8 kSizeOfNaClJmp = 11;
979 guint8 bytes[kSizeOfNaClJmp];
980 guint8 *pbytes = &bytes[0];
982 x86_jump_membase32 (pbytes, X86_EBX, offset);
983 emit_bytes (acfg, bytes, kSizeOfNaClJmp);
984 /* four bytes of data, used by mono_arch_patch_plt_entry */
985 /* For Native Client, make this work with data embedded in push. */
986 emit_byte (acfg, 0x68); /* hide data in a push */
987 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
988 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
989 #endif /*__native_client_codegen__*/
990 #elif defined(TARGET_AMD64)
991 #if defined(__default_codegen__)
993 * We can't emit jumps because they are 32 bits only so they can't be patched.
994 * So we make indirect calls through GOT entries which are patched by the AOT
995 * loader to point to .Lpd entries.
997 /* jmpq *<offset>(%rip) */
998 emit_byte (acfg, '\xff');
999 emit_byte (acfg, '\x25');
1000 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) -4);
1001 /* Used by mono_aot_get_plt_info_offset */
1002 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
1003 acfg->stats.plt_size += 10;
1004 #elif defined(__native_client_codegen__)
1006 guint8 *buf_aligned = ALIGN_TO(buf, kNaClAlignment);
1007 guint8 *code = buf_aligned;
1009 /* mov <OFFSET>(%rip), %r11d */
1010 emit_byte (acfg, '\x45');
1011 emit_byte (acfg, '\x8b');
1012 emit_byte (acfg, '\x1d');
1013 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) -4);
1015 amd64_jump_reg (code, AMD64_R11);
1016 /* This should be constant for the plt patch */
1017 g_assert ((size_t)(code-buf_aligned) == 10);
1018 emit_bytes (acfg, buf_aligned, code - buf_aligned);
1020 /* Hide data in a push imm32 so it passes validation */
1021 emit_byte (acfg, 0x68); /* push */
1022 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
1023 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
1024 #endif /*__native_client_codegen__*/
1025 #elif defined(TARGET_ARM)
1030 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1031 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
1032 emit_bytes (acfg, buf, code - buf);
1033 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) - 4);
1034 /* Used by mono_aot_get_plt_info_offset */
1035 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
1036 #elif defined(TARGET_ARM64)
1037 arm64_emit_plt_entry (acfg, index);
1038 #elif defined(TARGET_POWERPC)
1039 guint32 offset = (acfg->plt_got_offset_base + index) * sizeof (gpointer);
1041 /* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
1042 g_assert (!acfg->use_bin_writer);
1043 emit_unset_mode (acfg);
1044 fprintf (acfg->fp, "lis 11, %d@h\n", offset);
1045 fprintf (acfg->fp, "ori 11, 11, %d@l\n", offset);
1046 fprintf (acfg->fp, "add 11, 11, 30\n");
1047 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1048 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1049 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
1050 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1052 fprintf (acfg->fp, "mtctr 11\n");
1053 fprintf (acfg->fp, "bctr\n");
1054 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
1056 g_assert_not_reached ();
1061 arch_emit_llvm_plt_entry (MonoAotCompile *acfg, int index)
1063 #if defined(TARGET_ARM)
1065 /* LLVM calls the PLT entries using bl, so emit a stub */
1066 /* FIXME: Too much overhead on every call */
1067 fprintf (acfg->fp, ".thumb_func\n");
1068 fprintf (acfg->fp, "bx pc\n");
1069 fprintf (acfg->fp, "nop\n");
1070 fprintf (acfg->fp, ".arm\n");
1072 /* LLVM calls the PLT entries using bl, so these have to be thumb2 */
1073 /* The caller already transitioned to thumb */
1074 /* The code below should be 12 bytes long */
1075 /* clang has trouble encoding these instructions, so emit the binary */
1077 fprintf (acfg->fp, "ldr ip, [pc, #8]\n");
1078 /* thumb can't encode ld pc, [pc, ip] */
1079 fprintf (acfg->fp, "add ip, pc, ip\n");
1080 fprintf (acfg->fp, "ldr ip, [ip, #0]\n");
1081 fprintf (acfg->fp, "bx ip\n");
1083 emit_set_thumb_mode (acfg);
1084 fprintf (acfg->fp, ".4byte 0xc008f8df\n");
1085 fprintf (acfg->fp, ".2byte 0x44fc\n");
1086 fprintf (acfg->fp, ".4byte 0xc000f8dc\n");
1087 fprintf (acfg->fp, ".2byte 0x4760\n");
1088 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) + 4);
1089 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
1090 emit_unset_mode (acfg);
1091 emit_set_arm_mode (acfg);
1093 g_assert_not_reached ();
1098 * arch_emit_specific_trampoline_pages:
1100 * Emits a page full of trampolines: each trampoline uses its own address to
1101 * lookup both the generic trampoline code and the data argument.
1102 * This page can be remapped in process multiple times so we can get an
1103 * unlimited number of trampolines.
1104 * Specifically this implementation uses the following trick: two memory pages
1105 * are allocated, with the first containing the data and the second containing the trampolines.
1106 * To reduce trampoline size, each trampoline jumps at the start of the page where a common
1107 * implementation does all the lifting.
1108 * Note that the ARM single trampoline size is 8 bytes, exactly like the data that needs to be stored
1109 * on the arm 32 bit system.
1112 arch_emit_specific_trampoline_pages (MonoAotCompile *acfg)
1114 #if defined(TARGET_ARM)
1117 guint8 *loop_start, *loop_branch_back, *loop_end_check, *imt_found_check;
1119 #define COMMON_TRAMP_SIZE 16
1120 int count = (mono_pagesize () - COMMON_TRAMP_SIZE) / 8;
1121 int imm8, rot_amount;
1124 if (!acfg->aot_opts.use_trampolines_page)
1127 acfg->tramp_page_size = mono_pagesize ();
1129 sprintf (symbol, "%sspecific_trampolines_page", acfg->user_symbol_prefix);
1130 emit_alignment (acfg, mono_pagesize ());
1131 emit_global (acfg, symbol, TRUE);
1132 emit_label (acfg, symbol);
1134 /* emit the generic code first, the trampoline address + 8 is in the lr register */
1136 imm8 = mono_arm_is_rotated_imm8 (mono_pagesize (), &rot_amount);
1137 ARM_SUB_REG_IMM (code, ARMREG_LR, ARMREG_LR, imm8, rot_amount);
1138 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, -8);
1139 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_LR, -4);
1141 g_assert (code - buf == COMMON_TRAMP_SIZE);
1144 emit_bytes (acfg, buf, code - buf);
1146 for (i = 0; i < count; ++i) {
1148 ARM_PUSH (code, 0x5fff);
1150 arm_patch (code - 4, code - COMMON_TRAMP_SIZE - 8 * (i + 1));
1151 g_assert (code - buf == 8);
1152 emit_bytes (acfg, buf, code - buf);
1155 /* now the rgctx trampolines: each specific trampolines puts in the ip register
1156 * the instruction pointer address, so the generic trampoline at the start of the page
1157 * subtracts 4096 to get to the data page and loads the values
1158 * We again fit the generic trampiline in 16 bytes.
1160 sprintf (symbol, "%srgctx_trampolines_page", acfg->user_symbol_prefix);
1161 emit_global (acfg, symbol, TRUE);
1162 emit_label (acfg, symbol);
1164 imm8 = mono_arm_is_rotated_imm8 (mono_pagesize (), &rot_amount);
1165 ARM_SUB_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
1166 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, -8);
1167 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, -4);
1169 g_assert (code - buf == COMMON_TRAMP_SIZE);
1172 emit_bytes (acfg, buf, code - buf);
1174 for (i = 0; i < count; ++i) {
1176 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
1178 arm_patch (code - 4, code - COMMON_TRAMP_SIZE - 8 * (i + 1));
1179 g_assert (code - buf == 8);
1180 emit_bytes (acfg, buf, code - buf);
1184 * gsharedvt arg trampolines: see arch_emit_gsharedvt_arg_trampoline ()
1186 sprintf (symbol, "%sgsharedvt_arg_trampolines_page", acfg->user_symbol_prefix);
1187 emit_global (acfg, symbol, TRUE);
1188 emit_label (acfg, symbol);
1190 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3));
1191 imm8 = mono_arm_is_rotated_imm8 (mono_pagesize (), &rot_amount);
1192 ARM_SUB_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
1193 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_IP, -8);
1194 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, -4);
1195 g_assert (code - buf == COMMON_TRAMP_SIZE);
1197 emit_bytes (acfg, buf, code - buf);
1199 for (i = 0; i < count; ++i) {
1201 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
1203 arm_patch (code - 4, code - COMMON_TRAMP_SIZE - 8 * (i + 1));
1204 g_assert (code - buf == 8);
1205 emit_bytes (acfg, buf, code - buf);
1208 /* now the imt trampolines: each specific trampolines puts in the ip register
1209 * the instruction pointer address, so the generic trampoline at the start of the page
1210 * subtracts 4096 to get to the data page and loads the values
1211 * We again fit the generic trampiline in 16 bytes.
1213 #define IMT_TRAMP_SIZE 72
1214 sprintf (symbol, "%simt_trampolines_page", acfg->user_symbol_prefix);
1215 emit_global (acfg, symbol, TRUE);
1216 emit_label (acfg, symbol);
1218 /* Need at least two free registers, plus a slot for storing the pc */
1219 ARM_PUSH (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_R2));
1221 imm8 = mono_arm_is_rotated_imm8 (mono_pagesize (), &rot_amount);
1222 ARM_SUB_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
1223 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_IP, -8);
1225 /* The IMT method is in v5, r0 has the imt array address */
1228 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, 0);
1229 ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
1230 imt_found_check = code;
1231 ARM_B_COND (code, ARMCOND_EQ, 0);
1233 /* End-of-loop check */
1234 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
1235 loop_end_check = code;
1236 ARM_B_COND (code, ARMCOND_EQ, 0);
1239 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (gpointer) * 2);
1240 loop_branch_back = code;
1242 arm_patch (loop_branch_back, loop_start);
1245 arm_patch (imt_found_check, code);
1246 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1247 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 0);
1248 /* Save it to the third stack slot */
1249 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1250 /* Restore the registers and branch */
1251 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1254 arm_patch (loop_end_check, code);
1255 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1256 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1257 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1261 g_assert (code - buf == IMT_TRAMP_SIZE);
1262 emit_bytes (acfg, buf, code - buf);
1264 for (i = 0; i < count; ++i) {
1266 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
1268 arm_patch (code - 4, code - IMT_TRAMP_SIZE - 8 * (i + 1));
1269 g_assert (code - buf == 8);
1270 emit_bytes (acfg, buf, code - buf);
1272 #elif defined(TARGET_ARM64)
1273 arm64_emit_specific_trampoline_pages (acfg);
1278 * arch_emit_specific_trampoline:
1280 * Emit code for a specific trampoline. OFFSET is the offset of the first of
1281 * two GOT slots which contain the generic trampoline address and the trampoline
1282 * argument. TRAMP_SIZE is set to the size of the emitted trampoline.
1285 arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1288 * The trampolines created here are variations of the specific
1289 * trampolines created in mono_arch_create_specific_trampoline (). The
1291 * - the generic trampoline address is taken from a got slot.
1292 * - the offset of the got slot where the trampoline argument is stored
1293 * is embedded in the instruction stream, and the generic trampoline
1294 * can load the argument by loading the offset, adding it to the
1295 * address of the trampoline to get the address of the got slot, and
1296 * loading the argument from there.
1297 * - all the trampolines should be of the same length.
1299 #if defined(TARGET_AMD64)
1300 #if defined(__default_codegen__)
1301 /* This should be exactly 16 bytes long */
1303 /* call *<offset>(%rip) */
1304 emit_byte (acfg, '\x41');
1305 emit_byte (acfg, '\xff');
1306 emit_byte (acfg, '\x15');
1307 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
1308 /* This should be relative to the start of the trampoline */
1309 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset+1) * sizeof (gpointer)) + 7);
1310 emit_zero_bytes (acfg, 5);
1311 #elif defined(__native_client_codegen__)
1313 guint8 *buf_aligned = ALIGN_TO(buf, kNaClAlignment);
1314 guint8 *code = buf_aligned;
1319 /* Emit this call in 'code' so we can find out how long it is. */
1320 amd64_call_reg (code, AMD64_R11);
1321 call_start = mono_arch_nacl_skip_nops (buf_aligned);
1322 call_len = code - call_start;
1324 /* The tramp_size is twice the NaCl alignment because it starts with */
1325 /* a call which needs to be aligned to the end of the boundary. */
1326 *tramp_size = kNaClAlignment*2;
1328 /* Emit nops to align call site below which is 7 bytes plus */
1329 /* the length of the call sequence emitted above. */
1330 /* Note: this requires the specific trampoline starts on a */
1331 /* kNaclAlignedment aligned address, which it does because */
1332 /* it's its own function that is aligned. */
1333 guint8 nop_buf[256];
1334 guint8 *nopbuf_aligned = ALIGN_TO (nop_buf, kNaClAlignment);
1335 guint8 *nopbuf_end = mono_arch_nacl_pad (nopbuf_aligned, kNaClAlignment - 7 - (call_len));
1336 emit_bytes (acfg, nopbuf_aligned, nopbuf_end - nopbuf_aligned);
1338 /* The trampoline is stored at the offset'th pointer, the -4 is */
1339 /* present because RIP relative addressing starts at the end of */
1340 /* the current instruction, while the label "." is relative to */
1341 /* the beginning of the current asm location, which in this case */
1342 /* is not the mov instruction, but the offset itself, due to the */
1343 /* way the bytes and ints are emitted here. */
1344 got_offset = (offset * sizeof(gpointer)) - 4;
1346 /* mov <OFFSET>(%rip), %r11d */
1347 emit_byte (acfg, '\x45');
1348 emit_byte (acfg, '\x8b');
1349 emit_byte (acfg, '\x1d');
1350 emit_symbol_diff (acfg, acfg->got_symbol, ".", got_offset);
1353 emit_bytes (acfg, call_start, call_len);
1355 /* The arg is stored at the offset+1 pointer, relative to beginning */
1356 /* of trampoline: 7 for mov, plus the call length, and 1 for push. */
1357 got_offset = ((offset + 1) * sizeof(gpointer)) + 7 + call_len + 1;
1359 /* We can't emit this data directly, hide in a "push imm32" */
1360 emit_byte (acfg, '\x68'); /* push */
1361 emit_symbol_diff (acfg, acfg->got_symbol, ".", got_offset);
1362 emit_alignment (acfg, kNaClAlignment);
1363 #endif /*__native_client_codegen__*/
1364 #elif defined(TARGET_ARM)
1368 /* This should be exactly 20 bytes long */
1371 ARM_PUSH (code, 0x5fff);
1372 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 4);
1373 /* Load the value from the GOT */
1374 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
1376 ARM_BLX_REG (code, ARMREG_R1);
1378 g_assert (code - buf == 16);
1381 emit_bytes (acfg, buf, code - buf);
1383 * Only one offset is needed, since the second one would be equal to the
1386 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 4);
1387 //emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 8);
1388 #elif defined(TARGET_ARM64)
1389 arm64_emit_specific_trampoline (acfg, offset, tramp_size);
1390 #elif defined(TARGET_POWERPC)
1397 g_assert (!acfg->use_bin_writer);
1400 * PPC has no ip relative addressing, so we need to compute the address
1401 * of the mscorlib got. That is slow and complex, so instead, we store it
1402 * in the second got slot of every aot image. The caller already computed
1403 * the address of its got and placed it into r30.
1405 emit_unset_mode (acfg);
1406 /* Load mscorlib got address */
1407 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
1408 /* Load generic trampoline address */
1409 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
1410 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
1411 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
1412 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1413 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1415 fprintf (acfg->fp, "mtctr 11\n");
1416 /* Load trampoline argument */
1417 /* On ppc, we pass it normally to the generic trampoline */
1418 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
1419 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
1420 fprintf (acfg->fp, "%s 0, 11, 0\n", PPC_LDX_OP);
1421 /* Branch to generic trampoline */
1422 fprintf (acfg->fp, "bctr\n");
1424 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1425 *tramp_size = 10 * 4;
1427 *tramp_size = 9 * 4;
1429 #elif defined(TARGET_X86)
1433 /* Similar to the PPC code above */
1435 /* FIXME: Could this clobber the register needed by get_vcall_slot () ? */
1437 /* We clobber ECX, since EAX is used as MONO_ARCH_MONITOR_OBJECT_REG */
1438 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1439 g_assert (MONO_ARCH_MONITOR_OBJECT_REG != X86_ECX);
1443 /* Load mscorlib got address */
1444 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
1445 /* Push trampoline argument */
1446 x86_push_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
1447 /* Load generic trampoline address */
1448 x86_mov_reg_membase (code, X86_ECX, X86_ECX, offset * sizeof (gpointer), 4);
1449 /* Branch to generic trampoline */
1450 x86_jump_reg (code, X86_ECX);
1452 #ifdef __native_client_codegen__
1454 /* emit nops to next 32 byte alignment */
1455 int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
1456 while (code < (buf + a)) x86_nop(code);
1459 emit_bytes (acfg, buf, code - buf);
1461 *tramp_size = NACL_SIZE(17, kNaClAlignment);
1462 g_assert (code - buf == *tramp_size);
1464 g_assert_not_reached ();
1469 * arch_emit_unbox_trampoline:
1471 * Emit code for the unbox trampoline for METHOD used in the full-aot case.
1472 * CALL_TARGET is the symbol pointing to the native code of METHOD.
1475 arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoCompile *cfg, MonoMethod *method, const char *call_target)
1477 #if defined(TARGET_AMD64)
1482 this_reg = mono_arch_get_this_arg_reg (NULL);
1484 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
1486 emit_bytes (acfg, buf, code - buf);
1488 emit_byte (acfg, '\xe9');
1489 emit_symbol_diff (acfg, call_target, ".", -4);
1490 #elif defined(TARGET_X86)
1497 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
1499 emit_bytes (acfg, buf, code - buf);
1502 emit_byte (acfg, '\xe9');
1503 emit_symbol_diff (acfg, call_target, ".", -4);
1504 #elif defined(TARGET_ARM)
1508 if (acfg->thumb_mixed && cfg->compile_llvm) {
1509 fprintf (acfg->fp, "add r0, r0, #%d\n", (int)sizeof (MonoObject));
1510 fprintf (acfg->fp, "b %s\n", call_target);
1511 fprintf (acfg->fp, ".arm\n");
1512 fprintf (acfg->fp, ".align 2\n");
1518 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
1520 emit_bytes (acfg, buf, code - buf);
1521 /* jump to method */
1522 if (acfg->use_bin_writer) {
1529 img_writer_emit_reloc (acfg->w, R_ARM_JUMP24, call_target, -8);
1530 emit_bytes (acfg, buf, 4);
1532 if (acfg->thumb_mixed && cfg->compile_llvm)
1533 fprintf (acfg->fp, "\n\tbx %s\n", call_target);
1535 fprintf (acfg->fp, "\n\tb %s\n", call_target);
1537 #elif defined(TARGET_ARM64)
1538 arm64_emit_unbox_trampoline (acfg, cfg, method, call_target);
1539 #elif defined(TARGET_POWERPC)
1542 g_assert (!acfg->use_bin_writer);
1544 fprintf (acfg->fp, "\n\taddi %d, %d, %d\n", this_pos, this_pos, (int)sizeof (MonoObject));
1545 fprintf (acfg->fp, "\n\tb %s\n", call_target);
1547 g_assert_not_reached ();
1552 * arch_emit_static_rgctx_trampoline:
1554 * Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
1555 * two GOT slots which contain the rgctx argument, and the method to jump to.
1556 * TRAMP_SIZE is set to the size of the emitted trampoline.
1557 * These kinds of trampolines cannot be enumerated statically, since there could
1558 * be one trampoline per method instantiation, so we emit the same code for all
1559 * trampolines, and parameterize them using two GOT slots.
1562 arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1564 #if defined(TARGET_AMD64)
1565 #if defined(__default_codegen__)
1566 /* This should be exactly 13 bytes long */
1569 /* mov <OFFSET>(%rip), %r10 */
1570 emit_byte (acfg, '\x4d');
1571 emit_byte (acfg, '\x8b');
1572 emit_byte (acfg, '\x15');
1573 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
1575 /* jmp *<offset>(%rip) */
1576 emit_byte (acfg, '\xff');
1577 emit_byte (acfg, '\x25');
1578 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
1579 #elif defined(__native_client_codegen__)
1581 guint8 *buf_aligned = ALIGN_TO(buf, kNaClAlignment);
1582 guint8 *code = buf_aligned;
1584 /* mov <OFFSET>(%rip), %r10d */
1585 emit_byte (acfg, '\x45');
1586 emit_byte (acfg, '\x8b');
1587 emit_byte (acfg, '\x15');
1588 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
1590 /* mov <OFFSET>(%rip), %r11d */
1591 emit_byte (acfg, '\x45');
1592 emit_byte (acfg, '\x8b');
1593 emit_byte (acfg, '\x1d');
1594 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
1597 amd64_jump_reg (code, AMD64_R11);
1598 emit_bytes (acfg, buf_aligned, code - buf_aligned);
1600 emit_alignment (acfg, kNaClAlignment);
1601 *tramp_size = kNaClAlignment;
1602 #endif /*__native_client_codegen__*/
1604 #elif defined(TARGET_ARM)
1608 /* This should be exactly 24 bytes long */
1611 /* Load rgctx value */
1612 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
1613 ARM_LDR_REG_REG (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, ARMREG_IP);
1614 /* Load branch addr + branch */
1615 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
1616 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
1618 g_assert (code - buf == 16);
1621 emit_bytes (acfg, buf, code - buf);
1622 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 8);
1623 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 4);
1624 #elif defined(TARGET_ARM64)
1625 arm64_emit_static_rgctx_trampoline (acfg, offset, tramp_size);
1626 #elif defined(TARGET_POWERPC)
1633 g_assert (!acfg->use_bin_writer);
1636 * PPC has no ip relative addressing, so we need to compute the address
1637 * of the mscorlib got. That is slow and complex, so instead, we store it
1638 * in the second got slot of every aot image. The caller already computed
1639 * the address of its got and placed it into r30.
1641 emit_unset_mode (acfg);
1642 /* Load mscorlib got address */
1643 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
1645 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
1646 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
1647 fprintf (acfg->fp, "%s %d, 11, 0\n", PPC_LDX_OP, MONO_ARCH_RGCTX_REG);
1648 /* Load target address */
1649 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
1650 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
1651 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
1652 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1653 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
1654 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1656 fprintf (acfg->fp, "mtctr 11\n");
1657 /* Branch to the target address */
1658 fprintf (acfg->fp, "bctr\n");
1660 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1661 *tramp_size = 11 * 4;
1663 *tramp_size = 9 * 4;
1666 #elif defined(TARGET_X86)
1670 /* Similar to the PPC code above */
1672 g_assert (MONO_ARCH_RGCTX_REG != X86_ECX);
1675 /* Load mscorlib got address */
1676 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
1678 x86_mov_reg_membase (code, MONO_ARCH_RGCTX_REG, X86_ECX, offset * sizeof (gpointer), 4);
1679 /* Branch to the target address */
1680 x86_jump_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
1682 #ifdef __native_client_codegen__
1684 /* emit nops to next 32 byte alignment */
1685 int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
1686 while (code < (buf + a)) x86_nop(code);
1690 emit_bytes (acfg, buf, code - buf);
1692 *tramp_size = NACL_SIZE (15, kNaClAlignment);
1693 g_assert (code - buf == *tramp_size);
1695 g_assert_not_reached ();
1700 * arch_emit_imt_thunk:
1702 * Emit an IMT thunk usable in full-aot mode. The thunk uses 1 got slot which
1703 * points to an array of pointer pairs. The pairs of the form [key, ptr], where
1704 * key is the IMT key, and ptr holds the address of a memory location holding
1705 * the address to branch to if the IMT arg matches the key. The array is
1706 * terminated by a pair whose key is NULL, and whose ptr is the address of the
1708 * TRAMP_SIZE is set to the size of the emitted trampoline.
1711 arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
1713 #if defined(TARGET_AMD64)
1715 #if defined(__native_client_codegen__)
1718 guint8 *labels [16];
1720 guint8 *mov_buf_ptr = mov_buf;
1722 const int kSizeOfMove = 7;
1723 #if defined(__default_codegen__)
1724 code = buf = g_malloc (256);
1725 #elif defined(__native_client_codegen__)
1726 buf_alloc = g_malloc (256 + kNaClAlignment + kSizeOfMove);
1727 buf = ((guint)buf_alloc + kNaClAlignment) & ~kNaClAlignmentMask;
1728 /* The RIP relative move below is emitted first */
1733 /* FIXME: Optimize this, i.e. use binary search etc. */
1734 /* Maybe move the body into a separate function (slower, but much smaller) */
1736 /* MONO_ARCH_IMT_SCRATCH_REG is a free register */
1739 amd64_alu_membase_imm (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, 0);
1741 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
1744 amd64_alu_membase_reg_size (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, MONO_ARCH_IMT_REG, sizeof (gpointer));
1746 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
1749 amd64_alu_reg_imm (code, X86_ADD, MONO_ARCH_IMT_SCRATCH_REG, 2 * sizeof (gpointer));
1750 amd64_jump_code (code, labels [0]);
1753 mono_amd64_patch (labels [2], code);
1754 amd64_mov_reg_membase (code, MONO_ARCH_IMT_SCRATCH_REG, MONO_ARCH_IMT_SCRATCH_REG, sizeof (gpointer), sizeof (gpointer));
1755 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
1758 mono_amd64_patch (labels [1], code);
1759 /* Load fail tramp */
1760 amd64_alu_reg_imm (code, X86_ADD, MONO_ARCH_IMT_SCRATCH_REG, sizeof (gpointer));
1761 /* Check if there is a fail tramp */
1762 amd64_alu_membase_imm (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, 0);
1764 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
1765 /* Jump to fail tramp */
1766 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
1769 mono_amd64_patch (labels [3], code);
1770 x86_breakpoint (code);
1772 /* mov <OFFSET>(%rip), MONO_ARCH_IMT_SCRATCH_REG */
1773 amd64_emit_rex (mov_buf_ptr, sizeof(gpointer), MONO_ARCH_IMT_SCRATCH_REG, 0, AMD64_RIP);
1774 *(mov_buf_ptr)++ = (unsigned char)0x8b; /* mov opcode */
1775 x86_address_byte (mov_buf_ptr, 0, MONO_ARCH_IMT_SCRATCH_REG & 0x7, 5);
1776 emit_bytes (acfg, mov_buf, mov_buf_ptr - mov_buf);
1777 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
1779 emit_bytes (acfg, buf, code - buf);
1781 *tramp_size = code - buf + kSizeOfMove;
1782 #if defined(__native_client_codegen__)
1783 /* The tramp will be padded to the next kNaClAlignment bundle. */
1784 *tramp_size = ALIGN_TO ((*tramp_size), kNaClAlignment);
1787 #if defined(__default_codegen__)
1789 #elif defined(__native_client_codegen__)
1793 #elif defined(TARGET_X86)
1795 #ifdef __native_client_codegen__
1798 guint8 *labels [16];
1800 #if defined(__default_codegen__)
1801 code = buf = g_malloc (256);
1802 #elif defined(__native_client_codegen__)
1803 buf_alloc = g_malloc (256 + kNaClAlignment);
1804 code = buf = ((guint)buf_alloc + kNaClAlignment) & ~kNaClAlignmentMask;
1807 /* Allocate a temporary stack slot */
1808 x86_push_reg (code, X86_EAX);
1810 x86_push_reg (code, X86_EAX);
1812 /* Load mscorlib got address */
1813 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
1815 x86_mov_reg_membase (code, X86_EAX, X86_EAX, offset * sizeof (gpointer), 4);
1818 x86_alu_membase_imm (code, X86_CMP, X86_EAX, 0, 0);
1820 x86_branch8 (code, X86_CC_Z, FALSE, 0);
1823 x86_alu_membase_reg (code, X86_CMP, X86_EAX, 0, MONO_ARCH_IMT_REG);
1825 x86_branch8 (code, X86_CC_Z, FALSE, 0);
1828 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 2 * sizeof (gpointer));
1829 x86_jump_code (code, labels [0]);
1832 mono_x86_patch (labels [2], code);
1833 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer), 4);
1834 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
1835 /* Save the target address to the temporary stack location */
1836 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
1838 x86_pop_reg (code, X86_EAX);
1839 /* Jump to the target address */
1843 mono_x86_patch (labels [1], code);
1844 /* Load fail tramp */
1845 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer), 4);
1846 x86_alu_membase_imm (code, X86_CMP, X86_EAX, 0, 0);
1848 x86_branch8 (code, X86_CC_Z, FALSE, 0);
1849 /* Jump to fail tramp */
1850 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
1851 x86_pop_reg (code, X86_EAX);
1855 mono_x86_patch (labels [3], code);
1856 x86_breakpoint (code);
1858 #ifdef __native_client_codegen__
1860 /* emit nops to next 32 byte alignment */
1861 int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
1862 while (code < (buf + a)) x86_nop(code);
1865 emit_bytes (acfg, buf, code - buf);
1867 *tramp_size = code - buf;
1869 #if defined(__default_codegen__)
1871 #elif defined(__native_client_codegen__)
1875 #elif defined(TARGET_ARM)
1877 guint8 *code, *code2, *labels [16];
1881 /* The IMT method is in v5 */
1883 /* Need at least two free registers, plus a slot for storing the pc */
1884 ARM_PUSH (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_R2));
1886 /* Load the parameter from the GOT */
1887 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
1888 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
1891 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, 0);
1892 ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
1894 ARM_B_COND (code, ARMCOND_EQ, 0);
1896 /* End-of-loop check */
1897 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
1899 ARM_B_COND (code, ARMCOND_EQ, 0);
1902 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (gpointer) * 2);
1905 arm_patch (labels [4], labels [1]);
1908 arm_patch (labels [2], code);
1909 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1910 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 0);
1911 /* Save it to the third stack slot */
1912 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1913 /* Restore the registers and branch */
1914 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1917 arm_patch (labels [3], code);
1918 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1919 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1920 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1924 ARM_LDR_IMM (code2, ARMREG_R0, ARMREG_PC, (code - (labels [0] + 8)));
1926 emit_bytes (acfg, buf, code - buf);
1927 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + (code - (labels [0] + 8)) - 4);
1929 *tramp_size = code - buf + 4;
1930 #elif defined(TARGET_ARM64)
1931 arm64_emit_imt_thunk (acfg, offset, tramp_size);
1932 #elif defined(TARGET_POWERPC)
1934 guint8 *code, *labels [16];
1938 /* Load the mscorlib got address */
1939 ppc_ldptr (code, ppc_r11, sizeof (gpointer), ppc_r30);
1940 /* Load the parameter from the GOT */
1941 ppc_load (code, ppc_r0, offset * sizeof (gpointer));
1942 ppc_ldptr_indexed (code, ppc_r11, ppc_r11, ppc_r0);
1944 /* Load and check key */
1946 ppc_ldptr (code, ppc_r0, 0, ppc_r11);
1947 ppc_cmp (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, MONO_ARCH_IMT_REG);
1949 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
1951 /* End-of-loop check */
1952 ppc_cmpi (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, 0);
1954 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
1957 ppc_addi (code, ppc_r11, ppc_r11, 2 * sizeof (gpointer));
1960 mono_ppc_patch (labels [4], labels [1]);
1963 mono_ppc_patch (labels [2], code);
1964 ppc_ldptr (code, ppc_r11, sizeof (gpointer), ppc_r11);
1965 /* r11 now contains the value of the vtable slot */
1966 /* this is not a function descriptor on ppc64 */
1967 ppc_ldptr (code, ppc_r11, 0, ppc_r11);
1968 ppc_mtctr (code, ppc_r11);
1969 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
1972 mono_ppc_patch (labels [3], code);
1976 *tramp_size = code - buf;
1978 emit_bytes (acfg, buf, code - buf);
1980 g_assert_not_reached ();
1985 * arch_emit_gsharedvt_arg_trampoline:
1987 * Emit code for a gsharedvt arg trampoline. OFFSET is the offset of the first of
1988 * two GOT slots which contain the argument, and the code to jump to.
1989 * TRAMP_SIZE is set to the size of the emitted trampoline.
1990 * These kinds of trampolines cannot be enumerated statically, since there could
1991 * be one trampoline per method instantiation, so we emit the same code for all
1992 * trampolines, and parameterize them using two GOT slots.
1995 arch_emit_gsharedvt_arg_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1997 #if defined(TARGET_X86)
2001 /* Similar to the PPC code above */
2003 g_assert (MONO_ARCH_RGCTX_REG != X86_ECX);
2006 /* Load mscorlib got address */
2007 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
2009 x86_mov_reg_membase (code, X86_EAX, X86_ECX, offset * sizeof (gpointer), 4);
2010 /* Branch to the target address */
2011 x86_jump_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
2013 #ifdef __native_client_codegen__
2015 /* emit nops to next 32 byte alignment */
2016 int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
2017 while (code < (buf + a)) x86_nop(code);
2021 emit_bytes (acfg, buf, code - buf);
2023 *tramp_size = NACL_SIZE (15, kNaClAlignment);
2024 g_assert (code - buf == *tramp_size);
2025 #elif defined(TARGET_ARM)
2029 /* The same as mono_arch_get_gsharedvt_arg_trampoline (), but for AOT */
2030 /* Similar to arch_emit_specific_trampoline () */
2033 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3));
2034 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8);
2035 /* Load the arg value from the GOT */
2036 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R1);
2037 /* Load the addr from the GOT */
2038 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
2040 ARM_BX (code, ARMREG_R1);
2042 g_assert (code - buf == 20);
2045 emit_bytes (acfg, buf, code - buf);
2046 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + 4);
2047 #elif defined(TARGET_ARM64)
2048 arm64_emit_gsharedvt_arg_trampoline (acfg, offset, tramp_size);
2050 g_assert_not_reached ();
2055 arch_emit_autoreg (MonoAotCompile *acfg, char *symbol)
2057 #if defined(TARGET_POWERPC) && defined(__mono_ilp32__)
2058 /* Based on code generated by gcc */
2059 emit_unset_mode (acfg);
2062 #if defined(_MSC_VER) || defined(MONO_CROSS_COMPILE)
2063 ".section .ctors,\"aw\",@progbits\n"
2067 ".section .opd,\"aw\"\n"
2070 ".long .%s,.TOC.@tocbase32\n"
2073 ".type .%s,@function\n"
2075 ".%s:\n", symbol, symbol, symbol, symbol, symbol, symbol, symbol, symbol);
2077 ".section .ctors,\"aw\",@progbits\n"
2081 ".section .opd,\"aw\"\n"
2084 ".long .%1$s,.TOC.@tocbase32\n"
2085 ".size %1$s,.-%1$s\n"
2087 ".type .%1$s,@function\n"
2089 ".%1$s:\n", symbol);
2100 "lis 3, .Lglobals@h\n"
2101 "ori 3, 3, .Lglobals@l\n"
2102 "bl .mono_aot_register_module\n"
2110 #if defined(_MSC_VER) || defined(MONO_CROSS_COMPILE)
2112 ".size .%s,.-.%s\n", symbol, symbol);
2115 ".size .%1$s,.-.%1$s\n", symbol);
2121 /* END OF ARCH SPECIFIC CODE */
2124 mono_get_field_token (MonoClassField *field)
2126 MonoClass *klass = field->parent;
2129 for (i = 0; i < klass->field.count; ++i) {
2130 if (field == &klass->fields [i])
2131 return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
2134 g_assert_not_reached ();
2139 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
2143 //printf ("ENCODE: %d 0x%x.\n", value, value);
2146 * Same encoding as the one used in the metadata, extended to handle values
2147 * greater than 0x1fffffff.
2149 if ((value >= 0) && (value <= 127))
2151 else if ((value >= 0) && (value <= 16383)) {
2152 p [0] = 0x80 | (value >> 8);
2153 p [1] = value & 0xff;
2155 } else if ((value >= 0) && (value <= 0x1fffffff)) {
2156 p [0] = (value >> 24) | 0xc0;
2157 p [1] = (value >> 16) & 0xff;
2158 p [2] = (value >> 8) & 0xff;
2159 p [3] = value & 0xff;
2164 p [1] = (value >> 24) & 0xff;
2165 p [2] = (value >> 16) & 0xff;
2166 p [3] = (value >> 8) & 0xff;
2167 p [4] = value & 0xff;
2175 stream_init (MonoDynamicStream *sh)
2178 sh->alloc_size = 4096;
2179 sh->data = g_malloc (4096);
2181 /* So offsets are > 0 */
2187 make_room_in_stream (MonoDynamicStream *stream, int size)
2189 if (size <= stream->alloc_size)
2192 while (stream->alloc_size <= size) {
2193 if (stream->alloc_size < 4096)
2194 stream->alloc_size = 4096;
2196 stream->alloc_size *= 2;
2199 stream->data = g_realloc (stream->data, stream->alloc_size);
2203 add_stream_data (MonoDynamicStream *stream, const char *data, guint32 len)
2207 make_room_in_stream (stream, stream->index + len);
2208 memcpy (stream->data + stream->index, data, len);
2209 idx = stream->index;
2210 stream->index += len;
2217 * Add data to the binary blob inside the aot image. Returns the offset inside the
2218 * blob where the data was stored.
2221 add_to_blob (MonoAotCompile *acfg, const guint8 *data, guint32 data_len)
2223 if (acfg->blob.alloc_size == 0)
2224 stream_init (&acfg->blob);
2226 return add_stream_data (&acfg->blob, (char*)data, data_len);
2230 add_to_blob_aligned (MonoAotCompile *acfg, const guint8 *data, guint32 data_len, guint32 align)
2235 if (acfg->blob.alloc_size == 0)
2236 stream_init (&acfg->blob);
2238 count = acfg->blob.index % align;
2240 /* we assume the stream data will be aligned */
2242 add_stream_data (&acfg->blob, buf, 4 - count);
2244 return add_stream_data (&acfg->blob, (char*)data, data_len);
2248 * emit_offset_table:
2250 * Emit a table of increasing offsets in a compact form using differential encoding.
2251 * There is an index entry for each GROUP_SIZE number of entries. The greater the
2252 * group size, the more compact the table becomes, but the slower it becomes to compute
2253 * a given entry. Returns the size of the table.
2256 emit_offset_table (MonoAotCompile *acfg, int noffsets, int group_size, gint32 *offsets)
2258 gint32 current_offset;
2259 int i, buf_size, ngroups, index_entry_size;
2261 guint32 *index_offsets;
2263 ngroups = (noffsets + (group_size - 1)) / group_size;
2265 index_offsets = g_new0 (guint32, ngroups);
2267 buf_size = noffsets * 4;
2268 p = buf = g_malloc0 (buf_size);
2271 for (i = 0; i < noffsets; ++i) {
2272 //printf ("D: %d -> %d\n", i, offsets [i]);
2273 if ((i % group_size) == 0) {
2274 index_offsets [i / group_size] = p - buf;
2275 /* Emit the full value for these entries */
2276 encode_value (offsets [i], p, &p);
2278 /* The offsets are allowed to be non-increasing */
2279 //g_assert (offsets [i] >= current_offset);
2280 encode_value (offsets [i] - current_offset, p, &p);
2282 current_offset = offsets [i];
2285 if (ngroups && index_offsets [ngroups - 1] < 65000)
2286 index_entry_size = 2;
2288 index_entry_size = 4;
2290 /* Emit the header */
2291 emit_int32 (acfg, noffsets);
2292 emit_int32 (acfg, group_size);
2293 emit_int32 (acfg, ngroups);
2294 emit_int32 (acfg, index_entry_size);
2296 /* Emit the index */
2297 for (i = 0; i < ngroups; ++i) {
2298 if (index_entry_size == 2)
2299 emit_int16 (acfg, index_offsets [i]);
2301 emit_int32 (acfg, index_offsets [i]);
2305 emit_bytes (acfg, buf, p - buf);
2307 return (int)(p - buf) + (ngroups * 4);
2311 get_image_index (MonoAotCompile *cfg, MonoImage *image)
2315 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
2319 index = g_hash_table_size (cfg->image_hash);
2320 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
2321 g_ptr_array_add (cfg->image_table, image);
2327 find_typespec_for_class (MonoAotCompile *acfg, MonoClass *klass)
2330 int len = acfg->image->tables [MONO_TABLE_TYPESPEC].rows;
2332 /* FIXME: Search referenced images as well */
2333 if (!acfg->typespec_classes) {
2334 acfg->typespec_classes = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoClass*) * len);
2335 for (i = 0; i < len; ++i) {
2336 acfg->typespec_classes [i] = mono_class_get_full (acfg->image, MONO_TOKEN_TYPE_SPEC | (i + 1), NULL);
2339 for (i = 0; i < len; ++i) {
2340 if (acfg->typespec_classes [i] == klass)
2345 return MONO_TOKEN_TYPE_SPEC | (i + 1);
2351 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf);
2354 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf);
2357 encode_ginst (MonoAotCompile *acfg, MonoGenericInst *inst, guint8 *buf, guint8 **endbuf);
2360 encode_type (MonoAotCompile *acfg, MonoType *t, guint8 *buf, guint8 **endbuf);
2363 encode_klass_ref_inner (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
2368 * The encoding begins with one of the MONO_AOT_TYPEREF values, followed by additional
2372 if (klass->generic_class) {
2374 g_assert (klass->type_token);
2376 /* Find a typespec for a class if possible */
2377 token = find_typespec_for_class (acfg, klass);
2379 encode_value (MONO_AOT_TYPEREF_TYPESPEC_TOKEN, p, &p);
2380 encode_value (token, p, &p);
2382 MonoClass *gclass = klass->generic_class->container_class;
2383 MonoGenericInst *inst = klass->generic_class->context.class_inst;
2384 static int count = 0;
2387 encode_value (MONO_AOT_TYPEREF_GINST, p, &p);
2388 encode_klass_ref (acfg, gclass, p, &p);
2389 encode_ginst (acfg, inst, p, &p);
2393 } else if (klass->type_token) {
2394 int iindex = get_image_index (acfg, klass->image);
2396 g_assert (mono_metadata_token_code (klass->type_token) == MONO_TOKEN_TYPE_DEF);
2398 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX, p, &p);
2399 encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, p, &p);
2401 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE, p, &p);
2402 encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, p, &p);
2403 encode_value (get_image_index (acfg, klass->image), p, &p);
2405 } else if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR)) {
2406 MonoGenericContainer *container = mono_type_get_generic_param_owner (&klass->byval_arg);
2407 MonoGenericParam *par = klass->byval_arg.data.generic_param;
2409 encode_value (MONO_AOT_TYPEREF_VAR, p, &p);
2410 encode_value (klass->byval_arg.type, p, &p);
2411 encode_value (mono_type_get_generic_param_num (&klass->byval_arg), p, &p);
2413 encode_value (container ? 1 : 0, p, &p);
2415 encode_value (container->is_method, p, &p);
2416 g_assert (par->serial == 0);
2417 if (container->is_method)
2418 encode_method_ref (acfg, container->owner.method, p, &p);
2420 encode_klass_ref (acfg, container->owner.klass, p, &p);
2422 encode_value (par->serial, p, &p);
2424 } else if (klass->byval_arg.type == MONO_TYPE_PTR) {
2425 encode_value (MONO_AOT_TYPEREF_PTR, p, &p);
2426 encode_type (acfg, &klass->byval_arg, p, &p);
2429 g_assert (klass->rank > 0);
2430 encode_value (MONO_AOT_TYPEREF_ARRAY, p, &p);
2431 encode_value (klass->rank, p, &p);
2432 encode_klass_ref (acfg, klass->element_class, p, &p);
2440 * Encode a reference to KLASS. We use our home-grown encoding instead of the
2441 * standard metadata encoding.
2444 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
2446 gboolean shared = FALSE;
2449 * The encoding of generic instances is large so emit them only once.
2451 if (klass->generic_class) {
2453 g_assert (klass->type_token);
2455 /* Find a typespec for a class if possible */
2456 token = find_typespec_for_class (acfg, klass);
2459 } else if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR)) {
2464 guint offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->klass_blob_hash, klass));
2468 buf2 = g_malloc (1024);
2471 encode_klass_ref_inner (acfg, klass, p, &p);
2472 g_assert (p - buf2 < 1024);
2474 offset = add_to_blob (acfg, buf2, p - buf2);
2477 g_hash_table_insert (acfg->klass_blob_hash, klass, GUINT_TO_POINTER (offset + 1));
2483 encode_value (MONO_AOT_TYPEREF_BLOB_INDEX, p, &p);
2484 encode_value (offset, p, &p);
2489 encode_klass_ref_inner (acfg, klass, buf, endbuf);
2493 encode_field_info (MonoAotCompile *cfg, MonoClassField *field, guint8 *buf, guint8 **endbuf)
2495 guint32 token = mono_get_field_token (field);
2498 encode_klass_ref (cfg, field->parent, p, &p);
2499 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
2500 encode_value (token - MONO_TOKEN_FIELD_DEF, p, &p);
2505 encode_ginst (MonoAotCompile *acfg, MonoGenericInst *inst, guint8 *buf, guint8 **endbuf)
2510 encode_value (inst->type_argc, p, &p);
2511 for (i = 0; i < inst->type_argc; ++i)
2512 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
2517 encode_generic_context (MonoAotCompile *acfg, MonoGenericContext *context, guint8 *buf, guint8 **endbuf)
2520 MonoGenericInst *inst;
2522 inst = context->class_inst;
2524 g_assert (inst->type_argc);
2525 encode_ginst (acfg, inst, p, &p);
2527 encode_value (0, p, &p);
2529 inst = context->method_inst;
2531 g_assert (inst->type_argc);
2532 encode_ginst (acfg, inst, p, &p);
2534 encode_value (0, p, &p);
2540 encode_type (MonoAotCompile *acfg, MonoType *t, guint8 *buf, guint8 **endbuf)
2544 g_assert (t->num_mods == 0);
2545 /* t->attrs can be ignored */
2546 //g_assert (t->attrs == 0);
2549 *p = MONO_TYPE_PINNED;
2553 *p = MONO_TYPE_BYREF;
2561 case MONO_TYPE_VOID:
2562 case MONO_TYPE_BOOLEAN:
2563 case MONO_TYPE_CHAR:
2576 case MONO_TYPE_STRING:
2577 case MONO_TYPE_OBJECT:
2578 case MONO_TYPE_TYPEDBYREF:
2580 case MONO_TYPE_VALUETYPE:
2581 case MONO_TYPE_CLASS:
2582 encode_klass_ref (acfg, mono_class_from_mono_type (t), p, &p);
2584 case MONO_TYPE_SZARRAY:
2585 encode_klass_ref (acfg, t->data.klass, p, &p);
2588 encode_type (acfg, t->data.type, p, &p);
2590 case MONO_TYPE_GENERICINST: {
2591 MonoClass *gclass = t->data.generic_class->container_class;
2592 MonoGenericInst *inst = t->data.generic_class->context.class_inst;
2594 encode_klass_ref (acfg, gclass, p, &p);
2595 encode_ginst (acfg, inst, p, &p);
2598 case MONO_TYPE_ARRAY: {
2599 MonoArrayType *array = t->data.array;
2602 encode_klass_ref (acfg, array->eklass, p, &p);
2603 encode_value (array->rank, p, &p);
2604 encode_value (array->numsizes, p, &p);
2605 for (i = 0; i < array->numsizes; ++i)
2606 encode_value (array->sizes [i], p, &p);
2607 encode_value (array->numlobounds, p, &p);
2608 for (i = 0; i < array->numlobounds; ++i)
2609 encode_value (array->lobounds [i], p, &p);
2613 case MONO_TYPE_MVAR:
2614 encode_klass_ref (acfg, mono_class_from_mono_type (t), p, &p);
2617 g_assert_not_reached ();
2624 encode_signature (MonoAotCompile *acfg, MonoMethodSignature *sig, guint8 *buf, guint8 **endbuf)
2630 /* Similar to the metadata encoding */
2631 if (sig->generic_param_count)
2635 if (sig->explicit_this)
2637 flags |= (sig->call_convention & 0x0F);
2641 if (sig->generic_param_count)
2642 encode_value (sig->generic_param_count, p, &p);
2643 encode_value (sig->param_count, p, &p);
2645 encode_type (acfg, sig->ret, p, &p);
2646 for (i = 0; i < sig->param_count; ++i) {
2647 if (sig->sentinelpos == i) {
2648 *p = MONO_TYPE_SENTINEL;
2651 encode_type (acfg, sig->params [i], p, &p);
2657 #define MAX_IMAGE_INDEX 250
2660 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf)
2662 guint32 image_index = get_image_index (acfg, method->klass->image);
2663 guint32 token = method->token;
2664 MonoJumpInfoToken *ji;
2668 * The encoding for most methods is as follows:
2669 * - image index encoded as a leb128
2670 * - token index encoded as a leb128
2671 * Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
2672 * types of method encodings.
2675 /* Mark methods which can't use aot trampolines because they need the further
2676 * processing in mono_magic_trampoline () which requires a MonoMethod*.
2678 if ((method->is_generic && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) ||
2679 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
2680 encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE << 24), p, &p);
2682 if (method->wrapper_type) {
2683 encode_value ((MONO_AOT_METHODREF_WRAPPER << 24), p, &p);
2685 encode_value (method->wrapper_type, p, &p);
2687 switch (method->wrapper_type) {
2688 case MONO_WRAPPER_REMOTING_INVOKE:
2689 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
2690 case MONO_WRAPPER_XDOMAIN_INVOKE: {
2693 m = mono_marshal_method_from_wrapper (method);
2695 encode_method_ref (acfg, m, p, &p);
2698 case MONO_WRAPPER_PROXY_ISINST:
2699 case MONO_WRAPPER_LDFLD:
2700 case MONO_WRAPPER_LDFLDA:
2701 case MONO_WRAPPER_STFLD:
2702 case MONO_WRAPPER_ISINST: {
2703 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2706 encode_klass_ref (acfg, info->d.proxy.klass, p, &p);
2709 case MONO_WRAPPER_LDFLD_REMOTE:
2710 case MONO_WRAPPER_STFLD_REMOTE:
2712 case MONO_WRAPPER_ALLOC: {
2713 AllocatorWrapperInfo *info = mono_marshal_get_wrapper_info (method);
2715 /* The GC name is saved once in MonoAotFileInfo */
2716 g_assert (info->alloc_type != -1);
2717 encode_value (info->alloc_type, p, &p);
2720 case MONO_WRAPPER_WRITE_BARRIER:
2722 case MONO_WRAPPER_STELEMREF: {
2723 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2726 encode_value (info->subtype, p, &p);
2727 if (info->subtype == WRAPPER_SUBTYPE_VIRTUAL_STELEMREF)
2728 encode_value (info->d.virtual_stelemref.kind, p, &p);
2731 case MONO_WRAPPER_UNKNOWN: {
2732 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2735 encode_value (info->subtype, p, &p);
2736 if (info->subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE ||
2737 info->subtype == WRAPPER_SUBTYPE_STRUCTURE_TO_PTR)
2738 encode_klass_ref (acfg, method->klass, p, &p);
2739 else if (info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER)
2740 encode_method_ref (acfg, info->d.synchronized_inner.method, p, &p);
2741 else if (info->subtype == WRAPPER_SUBTYPE_ARRAY_ACCESSOR)
2742 encode_method_ref (acfg, info->d.array_accessor.method, p, &p);
2745 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
2746 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2749 encode_value (info->subtype, p, &p);
2750 if (info->subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER) {
2751 strcpy ((char*)p, method->name);
2752 p += strlen (method->name) + 1;
2753 } else if (info->subtype == WRAPPER_SUBTYPE_NATIVE_FUNC_AOT) {
2754 encode_method_ref (acfg, info->d.managed_to_native.method, p, &p);
2756 g_assert (info->subtype == WRAPPER_SUBTYPE_NONE || info->subtype == WRAPPER_SUBTYPE_PINVOKE);
2757 encode_method_ref (acfg, info->d.managed_to_native.method, p, &p);
2761 case MONO_WRAPPER_SYNCHRONIZED: {
2764 m = mono_marshal_method_from_wrapper (method);
2766 g_assert (m != method);
2767 encode_method_ref (acfg, m, p, &p);
2770 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
2771 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2774 encode_value (info->subtype, p, &p);
2776 if (info->subtype == WRAPPER_SUBTYPE_ELEMENT_ADDR) {
2777 encode_value (info->d.element_addr.rank, p, &p);
2778 encode_value (info->d.element_addr.elem_size, p, &p);
2779 } else if (info->subtype == WRAPPER_SUBTYPE_STRING_CTOR) {
2780 encode_method_ref (acfg, info->d.string_ctor.method, p, &p);
2782 g_assert_not_reached ();
2786 case MONO_WRAPPER_CASTCLASS: {
2787 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2790 encode_value (info->subtype, p, &p);
2793 case MONO_WRAPPER_RUNTIME_INVOKE: {
2794 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2797 encode_value (info->subtype, p, &p);
2798 if (info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT || info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL)
2799 encode_method_ref (acfg, info->d.runtime_invoke.method, p, &p);
2800 else if (info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL)
2801 encode_signature (acfg, info->d.runtime_invoke.sig, p, &p);
2804 case MONO_WRAPPER_DELEGATE_INVOKE:
2805 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
2806 case MONO_WRAPPER_DELEGATE_END_INVOKE: {
2807 if (method->is_inflated) {
2808 /* These wrappers are identified by their class */
2809 encode_value (1, p, &p);
2810 encode_klass_ref (acfg, method->klass, p, &p);
2812 MonoMethodSignature *sig = mono_method_signature (method);
2813 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2815 encode_value (0, p, &p);
2816 if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE)
2817 encode_value (info ? info->subtype : 0, p, &p);
2818 encode_signature (acfg, sig, p, &p);
2822 case MONO_WRAPPER_NATIVE_TO_MANAGED: {
2823 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
2826 encode_method_ref (acfg, info->d.native_to_managed.method, p, &p);
2827 encode_klass_ref (acfg, info->d.native_to_managed.klass, p, &p);
2831 g_assert_not_reached ();
2833 } else if (mono_method_signature (method)->is_inflated) {
2835 * This is a generic method, find the original token which referenced it and
2837 * Obtain the token from information recorded by the JIT.
2839 ji = g_hash_table_lookup (acfg->token_info_hash, method);
2841 image_index = get_image_index (acfg, ji->image);
2842 g_assert (image_index < MAX_IMAGE_INDEX);
2845 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
2846 encode_value (image_index, p, &p);
2847 encode_value (token, p, &p);
2849 MonoMethod *declaring;
2850 MonoGenericContext *context = mono_method_get_context (method);
2852 g_assert (method->is_inflated);
2853 declaring = ((MonoMethodInflated*)method)->declaring;
2856 * This might be a non-generic method of a generic instance, which
2857 * doesn't have a token since the reference is generated by the JIT
2858 * like Nullable:Box/Unbox, or by generic sharing.
2860 encode_value ((MONO_AOT_METHODREF_GINST << 24), p, &p);
2861 /* Encode the klass */
2862 encode_klass_ref (acfg, method->klass, p, &p);
2863 /* Encode the method */
2864 image_index = get_image_index (acfg, method->klass->image);
2865 g_assert (image_index < MAX_IMAGE_INDEX);
2866 g_assert (declaring->token);
2867 token = declaring->token;
2868 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
2869 encode_value (image_index, p, &p);
2870 encode_value (token, p, &p);
2871 encode_generic_context (acfg, context, p, &p);
2873 } else if (token == 0) {
2874 /* This might be a method of a constructed type like int[,].Set */
2875 /* Obtain the token from information recorded by the JIT */
2876 ji = g_hash_table_lookup (acfg->token_info_hash, method);
2878 image_index = get_image_index (acfg, ji->image);
2879 g_assert (image_index < MAX_IMAGE_INDEX);
2882 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
2883 encode_value (image_index, p, &p);
2884 encode_value (token, p, &p);
2887 g_assert (method->klass->rank);
2889 /* Encode directly */
2890 encode_value ((MONO_AOT_METHODREF_ARRAY << 24), p, &p);
2891 encode_klass_ref (acfg, method->klass, p, &p);
2892 if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank)
2893 encode_value (0, p, &p);
2894 else if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank * 2)
2895 encode_value (1, p, &p);
2896 else if (!strcmp (method->name, "Get"))
2897 encode_value (2, p, &p);
2898 else if (!strcmp (method->name, "Address"))
2899 encode_value (3, p, &p);
2900 else if (!strcmp (method->name, "Set"))
2901 encode_value (4, p, &p);
2903 g_assert_not_reached ();
2906 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
2908 if (image_index >= MONO_AOT_METHODREF_MIN) {
2909 encode_value ((MONO_AOT_METHODREF_LARGE_IMAGE_INDEX << 24), p, &p);
2910 encode_value (image_index, p, &p);
2911 encode_value (mono_metadata_token_index (token), p, &p);
2913 encode_value ((image_index << 24) | mono_metadata_token_index (token), p, &p);
2920 compare_patches (gconstpointer a, gconstpointer b)
2924 i = (*(MonoJumpInfo**)a)->ip.i;
2925 j = (*(MonoJumpInfo**)b)->ip.i;
2936 static G_GNUC_UNUSED char*
2937 patch_to_string (MonoJumpInfo *patch_info)
2941 str = g_string_new ("");
2943 g_string_append_printf (str, "%s(", get_patch_name (patch_info->type));
2945 switch (patch_info->type) {
2946 case MONO_PATCH_INFO_VTABLE:
2947 mono_type_get_desc (str, &patch_info->data.klass->byval_arg, TRUE);
2952 g_string_append_printf (str, ")");
2953 return g_string_free (str, FALSE);
2959 * Return whenever PATCH_INFO refers to a direct call, and thus requires a
2962 static inline gboolean
2963 is_plt_patch (MonoJumpInfo *patch_info)
2965 switch (patch_info->type) {
2966 case MONO_PATCH_INFO_METHOD:
2967 case MONO_PATCH_INFO_INTERNAL_METHOD:
2968 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
2969 case MONO_PATCH_INFO_ICALL_ADDR:
2970 case MONO_PATCH_INFO_CLASS_INIT:
2971 case MONO_PATCH_INFO_RGCTX_FETCH:
2972 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2973 case MONO_PATCH_INFO_MONITOR_ENTER:
2974 case MONO_PATCH_INFO_MONITOR_EXIT:
2975 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE:
2985 * Return the symbol identifying the plt entry PLT_OFFSET.
2988 get_plt_symbol (MonoAotCompile *acfg, int plt_offset, MonoJumpInfo *patch_info)
2992 * The Apple linker reorganizes object files, so it doesn't like branches to local
2993 * labels, since those have no relocations.
2995 return g_strdup_printf ("%sp_%d", acfg->llvm_label_prefix, plt_offset);
2997 return g_strdup_printf ("%sp_%d", acfg->temp_prefix, plt_offset);
3004 * Return a PLT entry which belongs to the method identified by PATCH_INFO.
3006 static MonoPltEntry*
3007 get_plt_entry (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
3011 if (!is_plt_patch (patch_info))
3014 if (!acfg->patch_to_plt_entry [patch_info->type])
3015 acfg->patch_to_plt_entry [patch_info->type] = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
3016 res = g_hash_table_lookup (acfg->patch_to_plt_entry [patch_info->type], patch_info);
3018 // FIXME: This breaks the calculation of final_got_size
3019 if (!acfg->llvm && patch_info->type == MONO_PATCH_INFO_METHOD && (patch_info->data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
3021 * Allocate a separate PLT slot for each such patch, since some plt
3022 * entries will refer to the method itself, and some will refer to the
3029 MonoJumpInfo *new_ji;
3031 g_assert (!acfg->final_got_size);
3033 new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
3035 res = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoPltEntry));
3036 res->plt_offset = acfg->plt_offset;
3038 res->symbol = get_plt_symbol (acfg, res->plt_offset, patch_info);
3039 if (acfg->aot_opts.write_symbols)
3040 res->debug_sym = get_plt_entry_debug_sym (acfg, res->ji, acfg->plt_entry_debug_sym_cache);
3042 res->llvm_symbol = g_strdup_printf ("%s_%s_llvm", res->symbol, res->debug_sym);
3044 res->llvm_symbol = g_strdup_printf ("%s_llvm", res->symbol);
3046 g_hash_table_insert (acfg->patch_to_plt_entry [new_ji->type], new_ji, res);
3048 g_hash_table_insert (acfg->plt_offset_to_entry, GUINT_TO_POINTER (res->plt_offset), res);
3050 //g_assert (mono_patch_info_equal (patch_info, new_ji));
3051 //mono_print_ji (patch_info); printf ("\n");
3052 //g_hash_table_print_stats (acfg->patch_to_plt_entry);
3054 acfg->plt_offset ++;
3063 * Returns the offset of the GOT slot where the runtime object resulting from resolving
3064 * JI could be found if it exists, otherwise allocates a new one.
3067 get_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
3071 got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_got_offset_by_type [ji->type], ji));
3073 return got_offset - 1;
3075 got_offset = acfg->got_offset;
3076 acfg->got_offset ++;
3078 if (acfg->final_got_size)
3079 g_assert (got_offset < acfg->final_got_size);
3081 acfg->stats.got_slots ++;
3082 acfg->stats.got_slot_types [ji->type] ++;
3084 g_hash_table_insert (acfg->patch_to_got_offset, ji, GUINT_TO_POINTER (got_offset + 1));
3085 g_hash_table_insert (acfg->patch_to_got_offset_by_type [ji->type], ji, GUINT_TO_POINTER (got_offset + 1));
3086 g_ptr_array_add (acfg->got_patches, ji);
3091 /* Add a method to the list of methods which need to be emitted */
3093 add_method_with_index (MonoAotCompile *acfg, MonoMethod *method, int index, gboolean extra)
3096 if (!g_hash_table_lookup (acfg->method_indexes, method)) {
3097 g_ptr_array_add (acfg->methods, method);
3098 g_hash_table_insert (acfg->method_indexes, method, GUINT_TO_POINTER (index + 1));
3099 acfg->nmethods = acfg->methods->len + 1;
3102 if (method->wrapper_type || extra)
3103 g_ptr_array_add (acfg->extra_methods, method);
3107 get_method_index (MonoAotCompile *acfg, MonoMethod *method)
3109 int index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
3117 add_method_full (MonoAotCompile *acfg, MonoMethod *method, gboolean extra, int depth)
3121 index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
3125 index = acfg->method_index;
3126 add_method_with_index (acfg, method, index, extra);
3128 g_ptr_array_add (acfg->method_order, GUINT_TO_POINTER (index));
3130 g_hash_table_insert (acfg->method_depth, method, GUINT_TO_POINTER (depth));
3132 acfg->method_index ++;
3138 add_method (MonoAotCompile *acfg, MonoMethod *method)
3140 return add_method_full (acfg, method, FALSE, 0);
3144 add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth)
3146 if (mono_method_is_generic_sharable_full (method, FALSE, TRUE, FALSE))
3147 method = mini_get_shared_method (method);
3149 if (acfg->aot_opts.log_generics)
3150 aot_printf (acfg, "%*sAdding method %s.\n", depth, "", mono_method_full_name (method, TRUE));
3152 add_method_full (acfg, method, TRUE, depth);
3156 add_extra_method (MonoAotCompile *acfg, MonoMethod *method)
3158 add_extra_method_with_depth (acfg, method, 0);
3162 add_jit_icall_wrapper (gpointer key, gpointer value, gpointer user_data)
3164 MonoAotCompile *acfg = user_data;
3165 MonoJitICallInfo *callinfo = value;
3166 MonoMethod *wrapper;
3172 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
3173 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
3176 add_method (acfg, wrapper);
3180 get_runtime_invoke_sig (MonoMethodSignature *sig)
3182 MonoMethodBuilder *mb;
3185 mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
3186 m = mono_mb_create_method (mb, sig, 16);
3187 return mono_marshal_get_runtime_invoke (m, FALSE);
3191 can_marshal_struct (MonoClass *klass)
3193 MonoClassField *field;
3194 gboolean can_marshal = TRUE;
3195 gpointer iter = NULL;
3196 MonoMarshalType *info;
3199 if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
3202 info = mono_marshal_load_type_info (klass);
3204 /* Only allow a few field types to avoid asserts in the marshalling code */
3205 while ((field = mono_class_get_fields (klass, &iter))) {
3206 if ((field->type->attrs & FIELD_ATTRIBUTE_STATIC))
3209 switch (field->type->type) {
3214 case MONO_TYPE_BOOLEAN:
3217 case MONO_TYPE_CHAR:
3225 case MONO_TYPE_STRING:
3227 case MONO_TYPE_VALUETYPE:
3228 if (!mono_class_from_mono_type (field->type)->enumtype && !can_marshal_struct (mono_class_from_mono_type (field->type)))
3229 can_marshal = FALSE;
3231 case MONO_TYPE_SZARRAY: {
3232 gboolean has_mspec = FALSE;
3235 for (i = 0; i < info->num_fields; ++i) {
3236 if (info->fields [i].field == field && info->fields [i].mspec)
3241 can_marshal = FALSE;
3245 can_marshal = FALSE;
3251 /* Its hard to compute whenever these can be marshalled or not */
3252 if (!strcmp (klass->name_space, "System.Net.NetworkInformation.MacOsStructs") && strcmp (klass->name, "sockaddr_dl"))
3259 create_gsharedvt_inst (MonoAotCompile *acfg, MonoMethod *method, MonoGenericContext *ctx)
3261 /* Create a vtype instantiation */
3262 MonoGenericContext shared_context;
3264 MonoGenericInst *inst;
3265 MonoGenericContainer *container;
3266 MonoClass **constraints;
3269 memset (ctx, 0, sizeof (MonoGenericContext));
3271 if (method->klass->generic_container) {
3272 shared_context = method->klass->generic_container->context;
3273 inst = shared_context.class_inst;
3275 args = g_new0 (MonoType*, inst->type_argc);
3276 for (i = 0; i < inst->type_argc; ++i) {
3277 args [i] = &mono_defaults.int_class->byval_arg;
3279 ctx->class_inst = mono_metadata_get_generic_inst (inst->type_argc, args);
3281 if (method->is_generic) {
3282 container = mono_method_get_generic_container (method);
3283 shared_context = container->context;
3284 inst = shared_context.method_inst;
3286 args = g_new0 (MonoType*, inst->type_argc);
3287 for (i = 0; i < container->type_argc; ++i) {
3288 MonoGenericParamInfo *info = &container->type_params [i].info;
3289 gboolean ref_only = FALSE;
3291 if (info && info->constraints) {
3292 constraints = info->constraints;
3294 while (*constraints) {
3295 MonoClass *cklass = *constraints;
3296 if (!(cklass == mono_defaults.object_class || (cklass->image == mono_defaults.corlib && !strcmp (cklass->name, "ValueType"))))
3297 /* Inflaring the method with our vtype would not be valid */
3304 args [i] = &mono_defaults.object_class->byval_arg;
3306 args [i] = &mono_defaults.int_class->byval_arg;
3308 ctx->method_inst = mono_metadata_get_generic_inst (inst->type_argc, args);
3313 add_wrappers (MonoAotCompile *acfg)
3315 MonoMethod *method, *m;
3317 MonoMethodSignature *sig, *csig;
3321 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
3322 * so there is only one wrapper of a given type, or inlining their contents into their
3325 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3327 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3328 gboolean skip = FALSE;
3330 method = mono_get_method (acfg->image, token, NULL);
3332 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3333 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3334 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
3337 /* Skip methods which can not be handled by get_runtime_invoke () */
3338 sig = mono_method_signature (method);
3341 if ((sig->ret->type == MONO_TYPE_PTR) ||
3342 (sig->ret->type == MONO_TYPE_TYPEDBYREF))
3344 if (mono_class_is_open_constructed_type (sig->ret))
3347 for (j = 0; j < sig->param_count; j++) {
3348 if (sig->params [j]->type == MONO_TYPE_TYPEDBYREF)
3350 if (mono_class_is_open_constructed_type (sig->params [j]))
3354 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
3355 if (!mono_class_is_contextbound (method->klass)) {
3356 MonoDynCallInfo *info = mono_arch_dyn_call_prepare (sig);
3357 gboolean has_nullable = FALSE;
3359 for (j = 0; j < sig->param_count; j++) {
3360 if (sig->params [j]->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (sig->params [j])))
3361 has_nullable = TRUE;
3364 if (info && !has_nullable) {
3365 /* Supported by the dynamic runtime-invoke wrapper */
3373 //printf ("%s\n", mono_method_full_name (method, TRUE));
3374 add_method (acfg, mono_marshal_get_runtime_invoke (method, FALSE));
3378 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
3379 MonoMethodDesc *desc;
3380 MonoMethod *orig_method;
3383 /* Runtime invoke wrappers */
3385 /* void runtime-invoke () [.cctor] */
3386 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
3387 csig->ret = &mono_defaults.void_class->byval_arg;
3388 add_method (acfg, get_runtime_invoke_sig (csig));
3390 /* void runtime-invoke () [Finalize] */
3391 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
3393 csig->ret = &mono_defaults.void_class->byval_arg;
3394 add_method (acfg, get_runtime_invoke_sig (csig));
3396 /* void runtime-invoke (string) [exception ctor] */
3397 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
3399 csig->ret = &mono_defaults.void_class->byval_arg;
3400 csig->params [0] = &mono_defaults.string_class->byval_arg;
3401 add_method (acfg, get_runtime_invoke_sig (csig));
3403 /* void runtime-invoke (string, string) [exception ctor] */
3404 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
3406 csig->ret = &mono_defaults.void_class->byval_arg;
3407 csig->params [0] = &mono_defaults.string_class->byval_arg;
3408 csig->params [1] = &mono_defaults.string_class->byval_arg;
3409 add_method (acfg, get_runtime_invoke_sig (csig));
3411 /* string runtime-invoke () [Exception.ToString ()] */
3412 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
3414 csig->ret = &mono_defaults.string_class->byval_arg;
3415 add_method (acfg, get_runtime_invoke_sig (csig));
3417 /* void runtime-invoke (string, Exception) [exception ctor] */
3418 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
3420 csig->ret = &mono_defaults.void_class->byval_arg;
3421 csig->params [0] = &mono_defaults.string_class->byval_arg;
3422 csig->params [1] = &mono_defaults.exception_class->byval_arg;
3423 add_method (acfg, get_runtime_invoke_sig (csig));
3425 /* Assembly runtime-invoke (string, bool) [DoAssemblyResolve] */
3426 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
3428 csig->ret = &(mono_class_from_name (
3429 mono_defaults.corlib, "System.Reflection", "Assembly"))->byval_arg;
3430 csig->params [0] = &mono_defaults.string_class->byval_arg;
3431 csig->params [1] = &mono_defaults.boolean_class->byval_arg;
3432 add_method (acfg, get_runtime_invoke_sig (csig));
3434 /* runtime-invoke used by finalizers */
3435 add_method (acfg, mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE));
3437 /* This is used by mono_runtime_capture_context () */
3438 method = mono_get_context_capture_method ();
3440 add_method (acfg, mono_marshal_get_runtime_invoke (method, FALSE));
3442 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
3443 add_method (acfg, mono_marshal_get_runtime_invoke_dynamic ());
3447 add_method (acfg, mono_marshal_get_stelemref ());
3449 if (MONO_ARCH_HAVE_TLS_GET) {
3450 /* Managed Allocators */
3451 nallocators = mono_gc_get_managed_allocator_types ();
3452 for (i = 0; i < nallocators; ++i) {
3453 m = mono_gc_get_managed_allocator_by_type (i);
3455 add_method (acfg, m);
3458 /* Monitor Enter/Exit */
3459 desc = mono_method_desc_new ("Monitor:Enter(object,bool&)", FALSE);
3460 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
3461 /* This is a v4 method */
3463 method = mono_monitor_get_fast_path (orig_method);
3465 add_method (acfg, method);
3467 mono_method_desc_free (desc);
3469 desc = mono_method_desc_new ("Monitor:Exit(object)", FALSE);
3470 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
3471 g_assert (orig_method);
3472 mono_method_desc_free (desc);
3473 method = mono_monitor_get_fast_path (orig_method);
3475 add_method (acfg, method);
3478 /* Stelemref wrappers */
3480 MonoMethod **wrappers;
3483 wrappers = mono_marshal_get_virtual_stelemref_wrappers (&nwrappers);
3484 for (i = 0; i < nwrappers; ++i)
3485 add_method (acfg, wrappers [i]);
3489 /* castclass_with_check wrapper */
3490 add_method (acfg, mono_marshal_get_castclass_with_cache ());
3491 /* isinst_with_check wrapper */
3492 add_method (acfg, mono_marshal_get_isinst_with_cache ());
3494 #if defined(MONO_ARCH_ENABLE_MONITOR_IL_FASTPATH)
3496 MonoMethodDesc *desc;
3499 desc = mono_method_desc_new ("Monitor:Enter(object,bool&)", FALSE);
3500 m = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
3501 mono_method_desc_free (desc);
3503 m = mono_monitor_get_fast_path (m);
3505 add_method (acfg, m);
3510 /* JIT icall wrappers */
3511 /* FIXME: locking - this is "safe" as full-AOT threads don't mutate the icall hash*/
3512 g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
3516 * remoting-invoke-with-check wrappers are very frequent, so avoid emitting them,
3517 * we use the original method instead at runtime.
3518 * Since full-aot doesn't support remoting, this is not a problem.
3521 /* remoting-invoke wrappers */
3522 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3523 MonoMethodSignature *sig;
3525 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3526 method = mono_get_method (acfg->image, token, NULL);
3528 sig = mono_method_signature (method);
3530 if (sig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
3531 m = mono_marshal_get_remoting_invoke_with_check (method);
3533 add_method (acfg, m);
3538 /* delegate-invoke wrappers */
3539 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3541 MonoCustomAttrInfo *cattr;
3543 token = MONO_TOKEN_TYPE_DEF | (i + 1);
3544 klass = mono_class_get (acfg->image, token);
3547 mono_loader_clear_error ();
3551 if (!klass->delegate || klass == mono_defaults.delegate_class || klass == mono_defaults.multicastdelegate_class)
3554 if (!klass->generic_container) {
3555 method = mono_get_delegate_invoke (klass);
3557 m = mono_marshal_get_delegate_invoke (method, NULL);
3559 add_method (acfg, m);
3561 method = mono_class_get_method_from_name_flags (klass, "BeginInvoke", -1, 0);
3563 add_method (acfg, mono_marshal_get_delegate_begin_invoke (method));
3565 method = mono_class_get_method_from_name_flags (klass, "EndInvoke", -1, 0);
3567 add_method (acfg, mono_marshal_get_delegate_end_invoke (method));
3569 cattr = mono_custom_attrs_from_class (klass);
3574 for (j = 0; j < cattr->num_attrs; ++j)
3575 if (cattr->attrs [j].ctor && (!strcmp (cattr->attrs [j].ctor->klass->name, "MonoNativeFunctionWrapperAttribute") || !strcmp (cattr->attrs [j].ctor->klass->name, "UnmanagedFunctionPointerAttribute")))
3577 if (j < cattr->num_attrs)
3578 add_method (acfg, mono_marshal_get_native_func_wrapper_aot (klass));
3580 } else if ((acfg->opts & MONO_OPT_GSHAREDVT) && klass->generic_container) {
3581 MonoGenericContext ctx;
3582 MonoMethod *inst, *gshared;
3585 * Emit gsharedvt versions of the generic delegate-invoke wrappers
3588 method = mono_get_delegate_invoke (klass);
3589 create_gsharedvt_inst (acfg, method, &ctx);
3591 inst = mono_class_inflate_generic_method (method, &ctx);
3593 m = mono_marshal_get_delegate_invoke (inst, NULL);
3594 g_assert (m->is_inflated);
3596 gshared = mini_get_shared_method_full (m, FALSE, TRUE);
3597 add_extra_method (acfg, gshared);
3600 method = mono_get_delegate_begin_invoke (klass);
3601 create_gsharedvt_inst (acfg, method, &ctx);
3603 inst = mono_class_inflate_generic_method (method, &ctx);
3605 m = mono_marshal_get_delegate_begin_invoke (inst);
3606 g_assert (m->is_inflated);
3608 gshared = mini_get_shared_method_full (m, FALSE, TRUE);
3609 add_extra_method (acfg, gshared);
3612 method = mono_get_delegate_end_invoke (klass);
3613 create_gsharedvt_inst (acfg, method, &ctx);
3615 inst = mono_class_inflate_generic_method (method, &ctx);
3617 m = mono_marshal_get_delegate_end_invoke (inst);
3618 g_assert (m->is_inflated);
3620 gshared = mini_get_shared_method_full (m, FALSE, TRUE);
3621 add_extra_method (acfg, gshared);
3626 /* array access wrappers */
3627 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
3630 token = MONO_TOKEN_TYPE_SPEC | (i + 1);
3631 klass = mono_class_get (acfg->image, token);
3634 mono_loader_clear_error ();
3638 if (klass->rank && MONO_TYPE_IS_PRIMITIVE (&klass->element_class->byval_arg)) {
3639 MonoMethod *m, *wrapper;
3641 /* Add runtime-invoke wrappers too */
3643 m = mono_class_get_method_from_name (klass, "Get", -1);
3645 wrapper = mono_marshal_get_array_accessor_wrapper (m);
3646 add_extra_method (acfg, wrapper);
3647 add_extra_method (acfg, mono_marshal_get_runtime_invoke (wrapper, FALSE));
3649 m = mono_class_get_method_from_name (klass, "Set", -1);
3651 wrapper = mono_marshal_get_array_accessor_wrapper (m);
3652 add_extra_method (acfg, wrapper);
3653 add_extra_method (acfg, mono_marshal_get_runtime_invoke (wrapper, FALSE));
3657 /* Synchronized wrappers */
3658 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3659 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3660 method = mono_get_method (acfg->image, token, NULL);
3662 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
3663 if (method->is_generic) {
3665 } else if (method->klass->generic_container) {
3666 MonoGenericContext ctx;
3667 MonoMethod *inst, *gshared, *m;
3670 * Create a generic wrapper for a generic instance, and AOT that.
3672 create_gsharedvt_inst (acfg, method, &ctx);
3673 inst = mono_class_inflate_generic_method (method, &ctx);
3674 m = mono_marshal_get_synchronized_wrapper (inst);
3675 g_assert (m->is_inflated);
3676 gshared = mini_get_shared_method_full (m, FALSE, TRUE);
3677 add_method (acfg, gshared);
3679 add_method (acfg, mono_marshal_get_synchronized_wrapper (method));
3684 /* pinvoke wrappers */
3685 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3687 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3689 method = mono_get_method (acfg->image, token, NULL);
3691 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3692 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
3693 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
3697 /* native-to-managed wrappers */
3698 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3700 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3701 MonoCustomAttrInfo *cattr;
3704 method = mono_get_method (acfg->image, token, NULL);
3707 * Only generate native-to-managed wrappers for methods which have an
3708 * attribute named MonoPInvokeCallbackAttribute. We search for the attribute by
3709 * name to avoid defining a new assembly to contain it.
3711 cattr = mono_custom_attrs_from_method (method);
3714 for (j = 0; j < cattr->num_attrs; ++j)
3715 if (cattr->attrs [j].ctor && !strcmp (cattr->attrs [j].ctor->klass->name, "MonoPInvokeCallbackAttribute"))
3717 if (j < cattr->num_attrs) {
3718 MonoCustomAttrEntry *e = &cattr->attrs [j];
3719 MonoMethodSignature *sig = mono_method_signature (e->ctor);
3720 const char *p = (const char*)e->data;
3722 int slen, num_named, named_type, data_type;
3726 char *export_name = NULL;
3727 MonoMethod *wrapper;
3729 /* this cannot be enforced by the C# compiler so we must give the user some warning before aborting */
3730 if (!(method->flags & METHOD_ATTRIBUTE_STATIC)) {
3731 g_warning ("AOT restriction: Method '%s' must be static since it is decorated with [MonoPInvokeCallback]. See http://ios.xamarin.com/Documentation/Limitations#Reverse_Callbacks",
3732 mono_method_full_name (method, TRUE));
3736 g_assert (sig->param_count == 1);
3737 g_assert (sig->params [0]->type == MONO_TYPE_CLASS && !strcmp (mono_class_from_mono_type (sig->params [0])->name, "Type"));
3740 * Decode the cattr manually since we can't create objects
3741 * during aot compilation.
3747 /* From load_cattr_value () in reflection.c */
3748 slen = mono_metadata_decode_value (p, &p);
3749 n = g_memdup (p, slen + 1);
3751 t = mono_reflection_type_from_name (n, acfg->image);
3755 klass = mono_class_from_mono_type (t);
3756 g_assert (klass->parent == mono_defaults.multicastdelegate_class);
3760 num_named = read16 (p);
3763 g_assert (num_named < 2);
3764 if (num_named == 1) {
3767 MonoType *prop_type;
3769 /* parse ExportSymbol attribute */
3771 named_type = *named;
3776 name_len = mono_metadata_decode_blob_size (named, &named);
3777 name = g_malloc (name_len + 1);
3778 memcpy (name, named, name_len);
3779 name [name_len] = 0;
3782 g_assert (named_type == 0x54);
3783 g_assert (!strcmp (name, "ExportSymbol"));
3785 prop_type = &mono_defaults.string_class->byval_arg;
3787 /* load_cattr_value (), string case */
3788 g_assert (*named != (char)0xff);
3789 slen = mono_metadata_decode_value (named, &named);
3790 export_name = g_malloc (slen + 1);
3791 memcpy (export_name, named, slen);
3792 export_name [slen] = 0;
3796 wrapper = mono_marshal_get_managed_wrapper (method, klass, 0);
3797 add_method (acfg, wrapper);
3799 g_hash_table_insert (acfg->export_names, wrapper, export_name);
3803 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3804 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
3805 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
3809 /* StructureToPtr/PtrToStructure wrappers */
3810 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3813 token = MONO_TOKEN_TYPE_DEF | (i + 1);
3814 klass = mono_class_get (acfg->image, token);
3817 mono_loader_clear_error ();
3821 if (klass->valuetype && !klass->generic_container && can_marshal_struct (klass) &&
3822 !(klass->nested_in && strstr (klass->nested_in->name, "<PrivateImplementationDetails>") == klass->nested_in->name)) {
3823 add_method (acfg, mono_marshal_get_struct_to_ptr (klass));
3824 add_method (acfg, mono_marshal_get_ptr_to_struct (klass));
3830 has_type_vars (MonoClass *klass)
3832 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
3835 return has_type_vars (klass->element_class);
3836 if (klass->generic_class) {
3837 MonoGenericContext *context = &klass->generic_class->context;
3838 if (context->class_inst) {
3841 for (i = 0; i < context->class_inst->type_argc; ++i)
3842 if (has_type_vars (mono_class_from_mono_type (context->class_inst->type_argv [i])))
3846 if (klass->generic_container)
3852 is_vt_inst (MonoGenericInst *inst)
3856 for (i = 0; i < inst->type_argc; ++i) {
3857 MonoType *t = inst->type_argv [i];
3858 if (t->type == MONO_TYPE_VALUETYPE)
3865 method_has_type_vars (MonoMethod *method)
3867 if (has_type_vars (method->klass))
3870 if (method->is_inflated) {
3871 MonoGenericContext *context = mono_method_get_context (method);
3872 if (context->method_inst) {
3875 for (i = 0; i < context->method_inst->type_argc; ++i)
3876 if (has_type_vars (mono_class_from_mono_type (context->method_inst->type_argv [i])))
3883 static void add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth, const char *ref);
3886 add_generic_class (MonoAotCompile *acfg, MonoClass *klass, gboolean force, const char *ref)
3888 /* This might lead to a huge code blowup so only do it if neccesary */
3889 if (!acfg->aot_opts.full_aot && !force)
3892 add_generic_class_with_depth (acfg, klass, 0, ref);
3896 check_type_depth (MonoType *t, int depth)
3904 case MONO_TYPE_GENERICINST: {
3905 MonoGenericClass *gklass = t->data.generic_class;
3906 MonoGenericInst *ginst = gklass->context.class_inst;
3909 for (i = 0; i < ginst->type_argc; ++i) {
3910 if (check_type_depth (ginst->type_argv [i], depth + 1))
3924 add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method);
3927 * add_generic_class:
3929 * Add all methods of a generic class.
3932 add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth, const char *ref)
3935 MonoClassField *field;
3937 gboolean use_gsharedvt = FALSE;
3939 if (!acfg->ginst_hash)
3940 acfg->ginst_hash = g_hash_table_new (NULL, NULL);
3942 mono_class_init (klass);
3944 if (klass->generic_class && klass->generic_class->context.class_inst->is_open)
3947 if (has_type_vars (klass))
3950 if (!klass->generic_class && !klass->rank)
3953 if (klass->exception_type)
3956 if (!acfg->ginst_hash)
3957 acfg->ginst_hash = g_hash_table_new (NULL, NULL);
3959 if (g_hash_table_lookup (acfg->ginst_hash, klass))
3962 if (check_type_depth (&klass->byval_arg, 0))
3965 if (acfg->aot_opts.log_generics)
3966 aot_printf (acfg, "%*sAdding generic instance %s [%s].\n", depth, "", mono_type_full_name (&klass->byval_arg), ref);
3968 g_hash_table_insert (acfg->ginst_hash, klass, klass);
3971 * Use gsharedvt for generic collections with vtype arguments to avoid code blowup.
3972 * Enable this only for some classes since gsharedvt might not support all methods.
3974 if ((acfg->opts & MONO_OPT_GSHAREDVT) && klass->image == mono_defaults.corlib && klass->generic_class && klass->generic_class->context.class_inst && is_vt_inst (klass->generic_class->context.class_inst) &&
3975 (!strcmp (klass->name, "Dictionary`2") || !strcmp (klass->name, "List`1") || !strcmp (klass->name, "ReadOnlyCollection`1")))
3976 use_gsharedvt = TRUE;
3979 while ((method = mono_class_get_methods (klass, &iter))) {
3980 if ((acfg->opts & MONO_OPT_GSHAREDVT) && method->is_inflated && mono_method_get_context (method)->method_inst) {
3982 * This is partial sharing, and we can't handle it yet
3987 if (mono_method_is_generic_sharable_full (method, FALSE, FALSE, use_gsharedvt)) {
3989 add_types_from_method_header (acfg, method);
3993 if (method->is_generic)
3998 * FIXME: Instances which are referenced by these methods are not added,
3999 * for example Array.Resize<int> for List<int>.Add ().
4001 add_extra_method_with_depth (acfg, method, depth + 1);
4005 while ((field = mono_class_get_fields (klass, &iter))) {
4006 if (field->type->type == MONO_TYPE_GENERICINST)
4007 add_generic_class_with_depth (acfg, mono_class_from_mono_type (field->type), depth + 1, "field");
4010 if (klass->delegate) {
4011 method = mono_get_delegate_invoke (klass);
4013 method = mono_marshal_get_delegate_invoke (method, NULL);
4015 if (acfg->aot_opts.log_generics)
4016 aot_printf (acfg, "%*sAdding method %s.\n", depth, "", mono_method_full_name (method, TRUE));
4018 add_method (acfg, method);
4021 /* Add superclasses */
4023 add_generic_class_with_depth (acfg, klass->parent, depth, "parent");
4026 * For ICollection<T>, add instances of the helper methods
4027 * in Array, since a T[] could be cast to ICollection<T>.
4029 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") &&
4030 (!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"))) {
4031 MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
4032 MonoClass *array_class = mono_bounded_array_class_get (tclass, 1, FALSE);
4036 if (!strcmp (klass->name, "IEnumerator`1"))
4037 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, "IEnumerable`1");
4039 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
4041 /* Add the T[]/InternalEnumerator class */
4042 if (!strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IEnumerator`1")) {
4046 while ((nclass = mono_class_get_nested_types (array_class->parent, &iter))) {
4047 if (!strcmp (nclass->name, "InternalEnumerator`1"))
4051 nclass = mono_class_inflate_generic_class (nclass, mono_generic_class_get_context (klass->generic_class));
4052 add_generic_class (acfg, nclass, FALSE, "ICollection<T>");
4056 while ((method = mono_class_get_methods (array_class, &iter))) {
4057 if (strstr (method->name, name_prefix)) {
4058 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
4060 add_extra_method_with_depth (acfg, m, depth);
4064 g_free (name_prefix);
4067 /* Add an instance of GenericComparer<T> which is created dynamically by Comparer<T> */
4068 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "Comparer`1")) {
4069 MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
4070 MonoClass *icomparable, *gcomparer;
4071 MonoGenericContext ctx;
4072 MonoType *args [16];
4074 memset (&ctx, 0, sizeof (ctx));
4076 icomparable = mono_class_from_name (mono_defaults.corlib, "System", "IComparable`1");
4077 g_assert (icomparable);
4078 args [0] = &tclass->byval_arg;
4079 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4081 if (mono_class_is_assignable_from (mono_class_inflate_generic_class (icomparable, &ctx), tclass)) {
4082 gcomparer = mono_class_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericComparer`1");
4083 g_assert (gcomparer);
4084 add_generic_class (acfg, mono_class_inflate_generic_class (gcomparer, &ctx), FALSE, "Comparer<T>");
4088 /* Add an instance of GenericEqualityComparer<T> which is created dynamically by EqualityComparer<T> */
4089 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "EqualityComparer`1")) {
4090 MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
4091 MonoClass *iface, *gcomparer;
4092 MonoGenericContext ctx;
4093 MonoType *args [16];
4095 memset (&ctx, 0, sizeof (ctx));
4097 iface = mono_class_from_name (mono_defaults.corlib, "System", "IEquatable`1");
4099 args [0] = &tclass->byval_arg;
4100 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4102 if (mono_class_is_assignable_from (mono_class_inflate_generic_class (iface, &ctx), tclass)) {
4103 gcomparer = mono_class_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericEqualityComparer`1");
4104 g_assert (gcomparer);
4105 add_generic_class (acfg, mono_class_inflate_generic_class (gcomparer, &ctx), FALSE, "EqualityComparer<T>");
4111 add_instances_of (MonoAotCompile *acfg, MonoClass *klass, MonoType **insts, int ninsts, gboolean force)
4114 MonoGenericContext ctx;
4115 MonoType *args [16];
4117 if (acfg->aot_opts.no_instances)
4120 memset (&ctx, 0, sizeof (ctx));
4122 for (i = 0; i < ninsts; ++i) {
4123 args [0] = insts [i];
4124 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4125 add_generic_class (acfg, mono_class_inflate_generic_class (klass, &ctx), force, "");
4130 add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method)
4132 MonoMethodHeader *header;
4133 MonoMethodSignature *sig;
4136 depth = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_depth, method));
4138 sig = mono_method_signature (method);
4141 for (j = 0; j < sig->param_count; ++j)
4142 if (sig->params [j]->type == MONO_TYPE_GENERICINST)
4143 add_generic_class_with_depth (acfg, mono_class_from_mono_type (sig->params [j]), depth + 1, "arg");
4146 header = mono_method_get_header (method);
4149 for (j = 0; j < header->num_locals; ++j)
4150 if (header->locals [j]->type == MONO_TYPE_GENERICINST)
4151 add_generic_class_with_depth (acfg, mono_class_from_mono_type (header->locals [j]), depth + 1, "local");
4153 mono_loader_clear_error ();
4158 * add_generic_instances:
4160 * Add instances referenced by the METHODSPEC/TYPESPEC table.
4163 add_generic_instances (MonoAotCompile *acfg)
4168 MonoGenericContext *context;
4170 if (acfg->aot_opts.no_instances)
4173 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHODSPEC].rows; ++i) {
4174 token = MONO_TOKEN_METHOD_SPEC | (i + 1);
4175 method = mono_get_method (acfg->image, token, NULL);
4180 if (method->klass->image != acfg->image)
4183 context = mono_method_get_context (method);
4185 if (context && ((context->class_inst && context->class_inst->is_open)))
4189 * For open methods, create an instantiation which can be passed to the JIT.
4190 * FIXME: Handle class_inst as well.
4192 if (context && context->method_inst && context->method_inst->is_open) {
4193 MonoGenericContext shared_context;
4194 MonoGenericInst *inst;
4195 MonoType **type_argv;
4197 MonoMethod *declaring_method;
4198 gboolean supported = TRUE;
4200 /* Check that the context doesn't contain open constructed types */
4201 if (context->class_inst) {
4202 inst = context->class_inst;
4203 for (i = 0; i < inst->type_argc; ++i) {
4204 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)
4206 if (mono_class_is_open_constructed_type (inst->type_argv [i]))
4210 if (context->method_inst) {
4211 inst = context->method_inst;
4212 for (i = 0; i < inst->type_argc; ++i) {
4213 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)
4215 if (mono_class_is_open_constructed_type (inst->type_argv [i]))
4223 memset (&shared_context, 0, sizeof (MonoGenericContext));
4225 inst = context->class_inst;
4227 type_argv = g_new0 (MonoType*, inst->type_argc);
4228 for (i = 0; i < inst->type_argc; ++i) {
4229 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)
4230 type_argv [i] = &mono_defaults.object_class->byval_arg;
4232 type_argv [i] = inst->type_argv [i];
4235 shared_context.class_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
4239 inst = context->method_inst;
4241 type_argv = g_new0 (MonoType*, inst->type_argc);
4242 for (i = 0; i < inst->type_argc; ++i) {
4243 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)
4244 type_argv [i] = &mono_defaults.object_class->byval_arg;
4246 type_argv [i] = inst->type_argv [i];
4249 shared_context.method_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
4253 if (method->is_generic || method->klass->generic_container)
4254 declaring_method = method;
4256 declaring_method = mono_method_get_declaring_generic_method (method);
4258 method = mono_class_inflate_generic_method (declaring_method, &shared_context);
4262 * If the method is fully sharable, it was already added in place of its
4263 * generic definition.
4265 if (mono_method_is_generic_sharable_full (method, FALSE, FALSE, FALSE))
4269 * FIXME: Partially shared methods are not shared here, so we end up with
4270 * many identical methods.
4272 add_extra_method (acfg, method);
4275 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
4278 token = MONO_TOKEN_TYPE_SPEC | (i + 1);
4280 klass = mono_class_get (acfg->image, token);
4281 if (!klass || klass->rank) {
4282 mono_loader_clear_error ();
4286 add_generic_class (acfg, klass, FALSE, "typespec");
4289 /* Add types of args/locals */
4290 for (i = 0; i < acfg->methods->len; ++i) {
4291 method = g_ptr_array_index (acfg->methods, i);
4292 add_types_from_method_header (acfg, method);
4295 if (acfg->image == mono_defaults.corlib) {
4297 MonoType *insts [256];
4300 insts [ninsts ++] = &mono_defaults.byte_class->byval_arg;
4301 insts [ninsts ++] = &mono_defaults.sbyte_class->byval_arg;
4302 insts [ninsts ++] = &mono_defaults.int16_class->byval_arg;
4303 insts [ninsts ++] = &mono_defaults.uint16_class->byval_arg;
4304 insts [ninsts ++] = &mono_defaults.int32_class->byval_arg;
4305 insts [ninsts ++] = &mono_defaults.uint32_class->byval_arg;
4306 insts [ninsts ++] = &mono_defaults.int64_class->byval_arg;
4307 insts [ninsts ++] = &mono_defaults.uint64_class->byval_arg;
4308 insts [ninsts ++] = &mono_defaults.single_class->byval_arg;
4309 insts [ninsts ++] = &mono_defaults.double_class->byval_arg;
4310 insts [ninsts ++] = &mono_defaults.char_class->byval_arg;
4311 insts [ninsts ++] = &mono_defaults.boolean_class->byval_arg;
4313 /* Add GenericComparer<T> instances for primitive types for Enum.ToString () */
4314 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "GenericComparer`1");
4316 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4317 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "GenericEqualityComparer`1");
4319 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4321 /* Add instances of the array generic interfaces for primitive types */
4322 /* This will add instances of the InternalArray_ helper methods in Array too */
4323 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "ICollection`1");
4325 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4326 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "IList`1");
4328 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4329 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "IEnumerable`1");
4331 add_instances_of (acfg, klass, insts, ninsts, TRUE);
4334 * Add a managed-to-native wrapper of Array.GetGenericValueImpl<object>, which is
4335 * used for all instances of GetGenericValueImpl by the AOT runtime.
4338 MonoGenericContext ctx;
4339 MonoType *args [16];
4340 MonoMethod *get_method;
4341 MonoClass *array_klass = mono_array_class_get (mono_defaults.object_class, 1)->parent;
4343 get_method = mono_class_get_method_from_name (array_klass, "GetGenericValueImpl", 2);
4346 memset (&ctx, 0, sizeof (ctx));
4347 args [0] = &mono_defaults.object_class->byval_arg;
4348 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
4349 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (get_method, &ctx), TRUE, TRUE));
4353 /* Same for CompareExchange<T>/Exchange<T> */
4355 MonoGenericContext ctx;
4356 MonoType *args [16];
4358 MonoClass *interlocked_klass = mono_class_from_name (mono_defaults.corlib, "System.Threading", "Interlocked");
4359 gpointer iter = NULL;
4361 while ((m = mono_class_get_methods (interlocked_klass, &iter))) {
4362 if ((!strcmp (m->name, "CompareExchange") || !strcmp (m->name, "Exchange")) && m->is_generic) {
4363 memset (&ctx, 0, sizeof (ctx));
4364 args [0] = &mono_defaults.object_class->byval_arg;
4365 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
4366 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE));
4371 /* Same for Volatile.Read/Write<T> */
4373 MonoGenericContext ctx;
4374 MonoType *args [16];
4376 MonoClass *volatile_klass = mono_class_from_name (mono_defaults.corlib, "System.Threading", "Volatile");
4377 gpointer iter = NULL;
4379 if (volatile_klass) {
4380 while ((m = mono_class_get_methods (volatile_klass, &iter))) {
4381 if ((!strcmp (m->name, "Read") || !strcmp (m->name, "Write")) && m->is_generic) {
4382 memset (&ctx, 0, sizeof (ctx));
4383 args [0] = &mono_defaults.object_class->byval_arg;
4384 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
4385 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE));
4394 * is_direct_callable:
4396 * Return whenever the method identified by JI is directly callable without
4397 * going through the PLT.
4400 is_direct_callable (MonoAotCompile *acfg, MonoMethod *method, MonoJumpInfo *patch_info)
4402 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
4403 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
4405 gboolean direct_callable = TRUE;
4407 if (direct_callable && !(!callee_cfg->has_got_slots && (callee_cfg->method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
4408 direct_callable = FALSE;
4409 if ((callee_cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) && (!method || method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED))
4410 // FIXME: Maybe call the wrapper directly ?
4411 direct_callable = FALSE;
4413 if (acfg->aot_opts.soft_debug || acfg->aot_opts.no_direct_calls) {
4414 /* Disable this so all calls go through load_method (), see the
4415 * mini_get_debug_options ()->load_aot_jit_info_eagerly = TRUE; line in
4416 * mono_debugger_agent_init ().
4418 direct_callable = FALSE;
4421 if (callee_cfg->method->wrapper_type == MONO_WRAPPER_ALLOC)
4422 /* sgen does some initialization when the allocator method is created */
4423 direct_callable = FALSE;
4425 if (direct_callable)
4428 } else if ((patch_info->type == MONO_PATCH_INFO_ICALL_ADDR && patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
4429 if (acfg->aot_opts.direct_pinvoke)
4431 } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR) {
4432 if (acfg->aot_opts.direct_icalls)
4440 #ifdef MONO_ARCH_AOT_SUPPORTED
4442 get_pinvoke_import (MonoAotCompile *acfg, MonoMethod *method)
4444 MonoImage *image = method->klass->image;
4445 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *) method;
4446 MonoTableInfo *tables = image->tables;
4447 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
4448 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
4449 guint32 im_cols [MONO_IMPLMAP_SIZE];
4452 import = g_hash_table_lookup (acfg->method_to_pinvoke_import, method);
4456 if (!piinfo->implmap_idx || piinfo->implmap_idx > im->rows)
4459 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
4461 if (!im_cols [MONO_IMPLMAP_SCOPE] || im_cols [MONO_IMPLMAP_SCOPE] > mr->rows)
4464 import = g_strdup_printf ("%s", mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]));
4466 g_hash_table_insert (acfg->method_to_pinvoke_import, method, import);
4473 compare_lne (MonoDebugLineNumberEntry *a, MonoDebugLineNumberEntry *b)
4475 if (a->native_offset == b->native_offset)
4476 return a->il_offset - b->il_offset;
4478 return a->native_offset - b->native_offset;
4482 * compute_line_numbers:
4484 * Returns a sparse array of size CODE_SIZE containing MonoDebugSourceLocation* entries for the native offsets which have a corresponding line number
4487 static MonoDebugSourceLocation**
4488 compute_line_numbers (MonoMethod *method, int code_size, MonoDebugMethodJitInfo *debug_info)
4490 MonoDebugMethodInfo *minfo;
4491 MonoDebugLineNumberEntry *ln_array;
4492 MonoDebugSourceLocation *loc;
4493 int i, prev_line, prev_il_offset;
4494 int *native_to_il_offset = NULL;
4495 MonoDebugSourceLocation **res;
4498 minfo = mono_debug_lookup_method (method);
4501 // FIXME: This seems to happen when two methods have the same cfg->method_to_register
4502 if (debug_info->code_size != code_size)
4505 g_assert (code_size);
4507 /* Compute the native->IL offset mapping */
4509 ln_array = g_new0 (MonoDebugLineNumberEntry, debug_info->num_line_numbers);
4510 memcpy (ln_array, debug_info->line_numbers, debug_info->num_line_numbers * sizeof (MonoDebugLineNumberEntry));
4512 qsort (ln_array, debug_info->num_line_numbers, sizeof (MonoDebugLineNumberEntry), (gpointer)compare_lne);
4514 native_to_il_offset = g_new0 (int, code_size + 1);
4516 for (i = 0; i < debug_info->num_line_numbers; ++i) {
4518 MonoDebugLineNumberEntry *lne = &ln_array [i];
4521 for (j = 0; j < lne->native_offset; ++j)
4522 native_to_il_offset [j] = -1;
4525 if (i < debug_info->num_line_numbers - 1) {
4526 MonoDebugLineNumberEntry *lne_next = &ln_array [i + 1];
4528 for (j = lne->native_offset; j < lne_next->native_offset; ++j)
4529 native_to_il_offset [j] = lne->il_offset;
4531 for (j = lne->native_offset; j < code_size; ++j)
4532 native_to_il_offset [j] = lne->il_offset;
4537 /* Compute the native->line number mapping */
4538 res = g_new0 (MonoDebugSourceLocation*, code_size);
4539 prev_il_offset = -1;
4542 for (i = 0; i < code_size; ++i) {
4543 int il_offset = native_to_il_offset [i];
4545 if (il_offset == -1 || il_offset == prev_il_offset)
4547 prev_il_offset = il_offset;
4548 loc = mono_debug_symfile_lookup_location (minfo, il_offset);
4549 if (!(loc && loc->source_file))
4551 if (loc->row == prev_line) {
4552 mono_debug_symfile_free_location (loc);
4555 prev_line = loc->row;
4556 //printf ("D: %s:%d il=%x native=%x\n", loc->source_file, loc->row, il_offset, i);
4558 /* This will cover the prolog too */
4568 get_file_index (MonoAotCompile *acfg, const char *source_file)
4572 // FIXME: Free these
4573 if (!acfg->dwarf_ln_filenames)
4574 acfg->dwarf_ln_filenames = g_hash_table_new (g_str_hash, g_str_equal);
4575 findex = GPOINTER_TO_INT (g_hash_table_lookup (acfg->dwarf_ln_filenames, source_file));
4577 findex = g_hash_table_size (acfg->dwarf_ln_filenames) + 1;
4578 g_hash_table_insert (acfg->dwarf_ln_filenames, g_strdup (source_file), GINT_TO_POINTER (findex));
4579 emit_unset_mode (acfg);
4580 fprintf (acfg->fp, ".file %d \"%s\"\n", findex, mono_dwarf_escape_path (source_file));
4592 * emit_and_reloc_code:
4594 * Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
4595 * is true, calls are made through the GOT too. This is used for emitting trampolines
4596 * in full-aot mode, since calls made from trampolines couldn't go through the PLT,
4597 * since trampolines are needed to make PTL work.
4600 emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, guint32 code_len, MonoJumpInfo *relocs, gboolean got_only, MonoDebugMethodJitInfo *debug_info)
4602 int i, pindex, start_index, method_index;
4604 MonoJumpInfo *patch_info;
4605 MonoMethodHeader *header;
4606 MonoDebugSourceLocation **locs = NULL;
4608 #ifdef MONO_ARCH_AOT_SUPPORTED
4609 gboolean direct_call, external_call;
4611 const char *direct_call_target = 0;
4612 const char *direct_pinvoke;
4616 header = mono_method_get_header (method);
4618 method_index = get_method_index (acfg, method);
4621 if (acfg->gas_line_numbers && method && debug_info) {
4622 locs = compute_line_numbers (method, code_len, debug_info);
4624 int findex = get_file_index (acfg, "<unknown>");
4625 emit_unset_mode (acfg);
4626 fprintf (acfg->fp, ".loc %d %d 0\n", findex, 1);
4630 /* Collect and sort relocations */
4631 patches = g_ptr_array_new ();
4632 for (patch_info = relocs; patch_info; patch_info = patch_info->next)
4633 g_ptr_array_add (patches, patch_info);
4634 g_ptr_array_sort (patches, compare_patches);
4637 for (i = 0; i < code_len; i += INST_LEN) {
4639 for (pindex = start_index; pindex < patches->len; ++pindex) {
4640 patch_info = g_ptr_array_index (patches, pindex);
4641 if (patch_info->ip.i >= i)
4645 if (locs && locs [i]) {
4646 MonoDebugSourceLocation *loc = locs [i];
4649 findex = get_file_index (acfg, loc->source_file);
4650 emit_unset_mode (acfg);
4651 fprintf (acfg->fp, ".loc %d %d 0\n", findex, loc->row);
4652 mono_debug_symfile_free_location (loc);
4656 #ifdef MONO_ARCH_AOT_SUPPORTED
4657 if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
4658 start_index = pindex;
4660 switch (patch_info->type) {
4661 case MONO_PATCH_INFO_NONE:
4663 case MONO_PATCH_INFO_GOT_OFFSET: {
4666 arch_emit_got_offset (acfg, code + i, &code_size);
4667 i += code_size - INST_LEN;
4669 patch_info->type = MONO_PATCH_INFO_NONE;
4672 case MONO_PATCH_INFO_OBJC_SELECTOR_REF: {
4673 int code_size, index;
4674 char *selector = (void*)patch_info->data.target;
4676 if (!acfg->objc_selector_to_index)
4677 acfg->objc_selector_to_index = g_hash_table_new (g_str_hash, g_str_equal);
4678 if (!acfg->objc_selectors)
4679 acfg->objc_selectors = g_ptr_array_new ();
4680 index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->objc_selector_to_index, selector));
4684 index = acfg->objc_selector_index;
4685 g_ptr_array_add (acfg->objc_selectors, (void*)patch_info->data.target);
4686 g_hash_table_insert (acfg->objc_selector_to_index, selector, GUINT_TO_POINTER (index + 1));
4687 acfg->objc_selector_index ++;
4690 arch_emit_objc_selector_ref (acfg, code + i, index, &code_size);
4691 i += code_size - INST_LEN;
4693 patch_info->type = MONO_PATCH_INFO_NONE;
4698 * If this patch is a call, try emitting a direct call instead of
4699 * through a PLT entry. This is possible if the called method is in
4700 * the same assembly and requires no initialization.
4702 direct_call = FALSE;
4703 external_call = FALSE;
4704 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
4705 if (!got_only && is_direct_callable (acfg, method, patch_info)) {
4706 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
4707 //printf ("DIRECT: %s %s\n", method ? mono_method_full_name (method, TRUE) : "", mono_method_full_name (callee_cfg->method, TRUE));
4709 direct_call_target = callee_cfg->asm_symbol;
4710 patch_info->type = MONO_PATCH_INFO_NONE;
4711 acfg->stats.direct_calls ++;
4714 acfg->stats.all_calls ++;
4715 } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR) {
4716 if (!got_only && is_direct_callable (acfg, method, patch_info)) {
4717 if (!(patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
4718 direct_pinvoke = mono_lookup_icall_symbol (patch_info->data.method);
4720 direct_pinvoke = get_pinvoke_import (acfg, patch_info->data.method);
4721 if (direct_pinvoke) {
4723 g_assert (strlen (direct_pinvoke) < 1000);
4724 direct_call_target = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, direct_pinvoke);
4727 } else if (patch_info->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
4728 const char *sym = mono_lookup_jit_icall_symbol (patch_info->data.name);
4729 if (!got_only && sym && acfg->aot_opts.direct_icalls) {
4730 /* Call to a C function implementing a jit icall */
4732 external_call = TRUE;
4733 g_assert (strlen (sym) < 1000);
4734 direct_call_target = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, sym);
4736 } else if (patch_info->type == MONO_PATCH_INFO_INTERNAL_METHOD) {
4737 MonoJitICallInfo *info = mono_find_jit_icall_by_name (patch_info->data.name);
4738 const char *sym = mono_lookup_jit_icall_symbol (patch_info->data.name);
4739 if (!got_only && sym && acfg->aot_opts.direct_icalls && info->func == info->wrapper) {
4740 /* Call to a jit icall without a wrapper */
4742 external_call = TRUE;
4743 g_assert (strlen (sym) < 1000);
4744 direct_call_target = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, sym);
4749 patch_info->type = MONO_PATCH_INFO_NONE;
4750 acfg->stats.direct_calls ++;
4753 if (!got_only && !direct_call) {
4754 MonoPltEntry *plt_entry = get_plt_entry (acfg, patch_info);
4756 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
4758 direct_call_target = plt_entry->symbol;
4760 /* Nullify the patch */
4761 patch_info->type = MONO_PATCH_INFO_NONE;
4762 plt_entry->jit_used = TRUE;
4769 arch_emit_direct_call (acfg, direct_call_target, external_call, FALSE, patch_info, &call_size);
4770 i += call_size - INST_LEN;
4774 got_slot = get_got_offset (acfg, patch_info);
4776 arch_emit_got_access (acfg, code + i, got_slot, &code_size);
4777 i += code_size - INST_LEN;
4783 #endif /* MONO_ARCH_AOT_SUPPORTED */
4786 /* Find next patch */
4788 for (pindex = start_index; pindex < patches->len; ++pindex) {
4789 patch_info = g_ptr_array_index (patches, pindex);
4790 if (patch_info->ip.i >= i)
4794 /* Try to emit multiple bytes at once */
4795 if (pindex < patches->len && patch_info->ip.i > i) {
4798 for (limit = i + INST_LEN; limit < patch_info->ip.i; limit += INST_LEN) {
4799 if (locs && locs [limit])
4803 emit_code_bytes (acfg, code + i, limit - i);
4804 i = limit - INST_LEN;
4806 emit_code_bytes (acfg, code + i, INST_LEN);
4817 * Return a modified version of S which only includes characters permissible in symbols.
4820 sanitize_symbol (MonoAotCompile *acfg, char *s)
4822 gboolean process = FALSE;
4831 for (i = 0; i < len; ++i)
4832 if (!(s [i] <= 0x7f && (isalnum (s [i]) || s [i] == '_')))
4837 gs = g_string_sized_new (len);
4838 for (i = 0; i < len; ++i) {
4840 if (c <= 0x7f && (isalnum (c) || c == '_')) {
4841 g_string_append_c (gs, c);
4842 } else if (c > 0x7f) {
4843 /* multi-byte utf8 */
4844 g_string_append_printf (gs, "_0x%x", c);
4847 while (c >> 6 == 0x2) {
4848 g_string_append_printf (gs, "%x", c);
4852 g_string_append_printf (gs, "_");
4855 g_string_append_c (gs, '_');
4859 res = mono_mempool_strdup (acfg->mempool, gs->str);
4860 g_string_free (gs, TRUE);
4865 get_debug_sym (MonoMethod *method, const char *prefix, GHashTable *cache)
4867 char *name1, *name2, *cached;
4868 int i, j, len, count;
4871 // This is so that we don't accidentally create a local symbol (which starts with 'L')
4872 if (!prefix || !*prefix)
4876 name1 = mono_method_full_name (method, TRUE);
4877 len = strlen (name1);
4878 name2 = malloc (strlen (prefix) + len + 16);
4879 memcpy (name2, prefix, strlen (prefix));
4880 j = strlen (prefix);
4881 for (i = 0; i < len; ++i) {
4882 if (isalnum (name1 [i])) {
4883 name2 [j ++] = name1 [i];
4884 } else if (name1 [i] == ' ' && name1 [i + 1] == '(' && name1 [i + 2] == ')') {
4886 } else if (name1 [i] == ',' && name1 [i + 1] == ' ') {
4889 } else if (name1 [i] == '(' || name1 [i] == ')' || name1 [i] == '>') {
4898 while (g_hash_table_lookup (cache, name2)) {
4899 sprintf (name2 + j, "_%d", count);
4903 cached = g_strdup (name2);
4904 g_hash_table_insert (cache, cached, cached);
4910 emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
4915 char *debug_sym = NULL;
4916 char *symbol = NULL;
4917 int func_alignment = AOT_FUNC_ALIGNMENT;
4918 MonoMethodHeader *header;
4921 method = cfg->orig_method;
4922 code = cfg->native_code;
4923 header = cfg->header;
4925 method_index = get_method_index (acfg, method);
4926 symbol = g_strdup_printf ("%sme_%x", acfg->temp_prefix, method_index);
4929 /* Make the labels local */
4930 emit_section_change (acfg, ".text", 0);
4931 emit_alignment (acfg, func_alignment);
4933 if (acfg->global_symbols && acfg->need_no_dead_strip)
4934 fprintf (acfg->fp, " .no_dead_strip %s\n", cfg->asm_symbol);
4936 emit_label (acfg, cfg->asm_symbol);
4938 if (acfg->aot_opts.write_symbols && !acfg->global_symbols) {
4940 * Write a C style symbol for every method, this has two uses:
4941 * - it works on platforms where the dwarf debugging info is not
4943 * - it allows the setting of breakpoints of aot-ed methods.
4945 debug_sym = get_debug_sym (method, "", acfg->method_label_hash);
4947 if (acfg->need_no_dead_strip)
4948 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
4949 emit_local_symbol (acfg, debug_sym, symbol, TRUE);
4950 emit_label (acfg, debug_sym);
4953 export_name = g_hash_table_lookup (acfg->export_names, method);
4955 /* Emit a global symbol for the method */
4956 emit_global_inner (acfg, export_name, TRUE);
4957 emit_label (acfg, export_name);
4960 if (cfg->verbose_level > 0)
4961 g_print ("Method %s emitted as %s\n", mono_method_full_name (method, TRUE), cfg->asm_symbol);
4963 acfg->stats.code_size += cfg->code_len;
4965 acfg->cfgs [method_index]->got_offset = acfg->got_offset;
4967 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 ()));
4971 if (acfg->aot_opts.write_symbols) {
4972 emit_symbol_size (acfg, debug_sym, ".");
4976 emit_label (acfg, symbol);
4983 * Encode PATCH_INFO into its disk representation.
4986 encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf)
4990 switch (patch_info->type) {
4991 case MONO_PATCH_INFO_NONE:
4993 case MONO_PATCH_INFO_IMAGE:
4994 encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
4996 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
4997 case MONO_PATCH_INFO_JIT_TLS_ID:
4998 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
4999 case MONO_PATCH_INFO_CASTCLASS_CACHE:
5001 case MONO_PATCH_INFO_METHOD_REL:
5002 encode_value ((gint)patch_info->data.offset, p, &p);
5004 case MONO_PATCH_INFO_SWITCH: {
5005 gpointer *table = (gpointer *)patch_info->data.table->table;
5008 encode_value (patch_info->data.table->table_size, p, &p);
5009 for (k = 0; k < patch_info->data.table->table_size; k++)
5010 encode_value ((int)(gssize)table [k], p, &p);
5013 case MONO_PATCH_INFO_METHODCONST:
5014 case MONO_PATCH_INFO_METHOD:
5015 case MONO_PATCH_INFO_METHOD_JUMP:
5016 case MONO_PATCH_INFO_ICALL_ADDR:
5017 case MONO_PATCH_INFO_METHOD_RGCTX:
5018 case MONO_PATCH_INFO_METHOD_CODE_SLOT:
5019 encode_method_ref (acfg, patch_info->data.method, p, &p);
5021 case MONO_PATCH_INFO_INTERNAL_METHOD:
5022 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
5023 guint32 len = strlen (patch_info->data.name);
5025 encode_value (len, p, &p);
5027 memcpy (p, patch_info->data.name, len);
5032 case MONO_PATCH_INFO_LDSTR: {
5033 guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
5034 guint32 token = patch_info->data.token->token;
5035 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
5036 encode_value (image_index, p, &p);
5037 encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
5040 case MONO_PATCH_INFO_RVA:
5041 case MONO_PATCH_INFO_DECLSEC:
5042 case MONO_PATCH_INFO_LDTOKEN:
5043 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
5044 encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
5045 encode_value (patch_info->data.token->token, p, &p);
5046 encode_value (patch_info->data.token->has_context, p, &p);
5047 if (patch_info->data.token->has_context)
5048 encode_generic_context (acfg, &patch_info->data.token->context, p, &p);
5050 case MONO_PATCH_INFO_EXC_NAME: {
5051 MonoClass *ex_class;
5054 mono_class_from_name (mono_defaults.exception_class->image,
5055 "System", patch_info->data.target);
5056 g_assert (ex_class);
5057 encode_klass_ref (acfg, ex_class, p, &p);
5060 case MONO_PATCH_INFO_R4:
5061 encode_value (*((guint32 *)patch_info->data.target), p, &p);
5063 case MONO_PATCH_INFO_R8:
5064 encode_value (((guint32 *)patch_info->data.target) [MINI_LS_WORD_IDX], p, &p);
5065 encode_value (((guint32 *)patch_info->data.target) [MINI_MS_WORD_IDX], p, &p);
5067 case MONO_PATCH_INFO_VTABLE:
5068 case MONO_PATCH_INFO_CLASS:
5069 case MONO_PATCH_INFO_IID:
5070 case MONO_PATCH_INFO_ADJUSTED_IID:
5071 case MONO_PATCH_INFO_CLASS_INIT:
5072 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
5074 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
5075 encode_klass_ref (acfg, patch_info->data.del_tramp->klass, p, &p);
5076 if (patch_info->data.del_tramp->method) {
5077 encode_value (1, p, &p);
5078 encode_method_ref (acfg, patch_info->data.del_tramp->method, p, &p);
5080 encode_value (0, p, &p);
5082 encode_value (patch_info->data.del_tramp->virtual, p, &p);
5084 case MONO_PATCH_INFO_FIELD:
5085 case MONO_PATCH_INFO_SFLDA:
5086 encode_field_info (acfg, patch_info->data.field, p, &p);
5088 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
5090 case MONO_PATCH_INFO_RGCTX_FETCH: {
5091 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
5096 * entry->method has a lenghtly encoding and multiple rgctx_fetch entries
5097 * reference the same method, so encode the method only once.
5099 offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_blob_hash, entry->method));
5101 buf2 = g_malloc (1024);
5104 encode_method_ref (acfg, entry->method, p2, &p2);
5105 g_assert (p2 - buf2 < 1024);
5107 offset = add_to_blob (acfg, buf2, p2 - buf2);
5110 g_hash_table_insert (acfg->method_blob_hash, entry->method, GUINT_TO_POINTER (offset + 1));
5115 encode_value (offset, p, &p);
5116 g_assert ((int)entry->info_type < 256);
5117 g_assert (entry->data->type < 256);
5118 encode_value ((entry->in_mrgctx ? 1 : 0) | (entry->info_type << 1) | (entry->data->type << 9), p, &p);
5119 encode_patch (acfg, entry->data, p, &p);
5122 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
5123 case MONO_PATCH_INFO_MONITOR_ENTER:
5124 case MONO_PATCH_INFO_MONITOR_EXIT:
5125 case MONO_PATCH_INFO_SEQ_POINT_INFO:
5127 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE:
5128 encode_method_ref (acfg, patch_info->data.imt_tramp->method, p, &p);
5129 encode_value (patch_info->data.imt_tramp->vt_offset, p, &p);
5131 case MONO_PATCH_INFO_SIGNATURE:
5132 encode_signature (acfg, (MonoMethodSignature*)patch_info->data.target, p, &p);
5134 case MONO_PATCH_INFO_TLS_OFFSET:
5135 encode_value (GPOINTER_TO_INT (patch_info->data.target), p, &p);
5137 case MONO_PATCH_INFO_GSHAREDVT_CALL:
5138 encode_signature (acfg, (MonoMethodSignature*)patch_info->data.gsharedvt->sig, p, &p);
5139 encode_method_ref (acfg, patch_info->data.gsharedvt->method, p, &p);
5141 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
5142 MonoGSharedVtMethodInfo *info = patch_info->data.gsharedvt_method;
5145 encode_method_ref (acfg, info->method, p, &p);
5146 encode_value (info->num_entries, p, &p);
5147 for (i = 0; i < info->num_entries; ++i) {
5148 MonoRuntimeGenericContextInfoTemplate *template = &info->entries [i];
5150 encode_value (template->info_type, p, &p);
5151 switch (mini_rgctx_info_type_to_patch_info_type (template->info_type)) {
5152 case MONO_PATCH_INFO_CLASS:
5153 encode_klass_ref (acfg, mono_class_from_mono_type (template->data), p, &p);
5155 case MONO_PATCH_INFO_FIELD:
5156 encode_field_info (acfg, template->data, p, &p);
5159 g_assert_not_reached ();
5166 g_warning ("unable to handle jump info %d", patch_info->type);
5167 g_assert_not_reached ();
5174 encode_patch_list (MonoAotCompile *acfg, GPtrArray *patches, int n_patches, int first_got_offset, guint8 *buf, guint8 **endbuf)
5177 guint32 pindex, offset;
5178 MonoJumpInfo *patch_info;
5180 encode_value (n_patches, p, &p);
5182 for (pindex = 0; pindex < patches->len; ++pindex) {
5183 patch_info = g_ptr_array_index (patches, pindex);
5185 if (patch_info->type == MONO_PATCH_INFO_NONE || patch_info->type == MONO_PATCH_INFO_BB)
5189 offset = get_got_offset (acfg, patch_info);
5190 encode_value (offset, p, &p);
5197 emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
5201 int pindex, buf_size, n_patches;
5203 MonoJumpInfo *patch_info;
5204 MonoMethodHeader *header;
5205 guint32 method_index;
5207 guint32 first_got_offset;
5209 method = cfg->orig_method;
5210 header = mono_method_get_header (method);
5212 method_index = get_method_index (acfg, method);
5214 /* Sort relocations */
5215 patches = g_ptr_array_new ();
5216 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
5217 g_ptr_array_add (patches, patch_info);
5218 g_ptr_array_sort (patches, compare_patches);
5220 first_got_offset = acfg->cfgs [method_index]->got_offset;
5222 /**********************/
5223 /* Encode method info */
5224 /**********************/
5226 buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
5227 p = buf = g_malloc (buf_size);
5229 if (mono_class_get_cctor (method->klass))
5230 encode_klass_ref (acfg, method->klass, p, &p);
5232 /* Not needed when loading the method */
5233 encode_value (0, p, &p);
5236 if (cfg->opt & MONO_OPT_SHARED) {
5237 encode_value (g_list_length (cfg->ldstr_list), p, &p);
5238 for (l = cfg->ldstr_list; l; l = l->next) {
5239 encode_value ((long)l->data, p, &p);
5243 /* Used only in shared mode */
5244 g_assert (!cfg->ldstr_list);
5247 for (pindex = 0; pindex < patches->len; ++pindex) {
5248 patch_info = g_ptr_array_index (patches, pindex);
5250 if ((patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
5251 (patch_info->type == MONO_PATCH_INFO_NONE)) {
5252 patch_info->type = MONO_PATCH_INFO_NONE;
5257 if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
5258 /* Stored in a GOT slot initialized at module load time */
5259 patch_info->type = MONO_PATCH_INFO_NONE;
5263 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR) {
5264 /* Stored in a GOT slot initialized at module load time */
5265 patch_info->type = MONO_PATCH_INFO_NONE;
5269 if (is_plt_patch (patch_info)) {
5270 /* Calls are made through the PLT */
5271 patch_info->type = MONO_PATCH_INFO_NONE;
5279 g_assert (cfg->has_got_slots);
5281 encode_patch_list (acfg, patches, n_patches, first_got_offset, p, &p);
5283 acfg->stats.info_size += p - buf;
5285 g_assert (p - buf < buf_size);
5287 cfg->method_info_offset = add_to_blob (acfg, buf, p - buf);
5292 get_unwind_info_offset (MonoAotCompile *acfg, guint8 *encoded, guint32 encoded_len)
5294 guint32 cache_index;
5297 /* Reuse the unwind module to canonize and store unwind info entries */
5298 cache_index = mono_cache_unwind_info (encoded, encoded_len);
5300 /* Use +/- 1 to distinguish 0s from missing entries */
5301 offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1)));
5309 * It would be easier to use assembler symbols, but the caller needs an
5312 offset = acfg->unwind_info_offset;
5313 g_hash_table_insert (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1), GUINT_TO_POINTER (offset + 1));
5314 g_ptr_array_add (acfg->unwind_ops, GUINT_TO_POINTER (cache_index));
5317 encode_value (encoded_len, p, &p);
5319 acfg->unwind_info_offset += encoded_len + (p - buf);
5325 emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
5328 int i, k, buf_size, method_index;
5329 guint32 debug_info_size;
5331 MonoMethodHeader *header;
5332 guint8 *p, *buf, *debug_info;
5333 MonoJitInfo *jinfo = cfg->jit_info;
5335 gboolean use_unwind_ops = FALSE;
5336 MonoSeqPointInfo *seq_points;
5338 method = cfg->orig_method;
5339 code = cfg->native_code;
5340 header = cfg->header;
5342 method_index = get_method_index (acfg, method);
5344 if (!acfg->aot_opts.nodebug) {
5345 mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
5348 debug_info_size = 0;
5351 seq_points = cfg->seq_point_info;
5353 buf_size = header->num_clauses * 256 + debug_info_size + 2048 + (seq_points ? (seq_points->len * 128) : 0) + cfg->gc_map_size;
5354 p = buf = g_malloc (buf_size);
5356 use_unwind_ops = cfg->unwind_ops != NULL;
5358 flags = (jinfo->has_generic_jit_info ? 1 : 0) | (use_unwind_ops ? 2 : 0) | (header->num_clauses ? 4 : 0) | (seq_points ? 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);
5360 encode_value (flags, p, &p);
5362 if (use_unwind_ops) {
5363 guint32 encoded_len;
5365 guint32 unwind_desc;
5367 encoded = mono_unwind_ops_encode (cfg->unwind_ops, &encoded_len);
5369 unwind_desc = get_unwind_info_offset (acfg, encoded, encoded_len);
5370 encode_value (unwind_desc, p, &p);
5372 encode_value (jinfo->unwind_info, p, &p);
5375 /*Encode the number of holes before the number of clauses to make decoding easier*/
5376 if (jinfo->has_try_block_holes) {
5377 MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
5378 encode_value (table->num_holes, p, &p);
5381 /* Exception table */
5382 if (cfg->compile_llvm) {
5384 * When using LLVM, we can't emit some data, like pc offsets, this reg/offset etc.,
5385 * since the information is only available to llc. Instead, we let llc save the data
5386 * into the LSDA, and read it from there at runtime.
5388 /* The assembly might be CIL stripped so emit the data ourselves */
5389 if (header->num_clauses)
5390 encode_value (header->num_clauses, p, &p);
5392 for (k = 0; k < header->num_clauses; ++k) {
5393 MonoExceptionClause *clause;
5395 clause = &header->clauses [k];
5397 encode_value (clause->flags, p, &p);
5398 if (clause->data.catch_class) {
5399 encode_value (1, p, &p);
5400 encode_klass_ref (acfg, clause->data.catch_class, p, &p);
5402 encode_value (0, p, &p);
5405 /* Emit a list of nesting clauses */
5406 for (i = 0; i < header->num_clauses; ++i) {
5408 MonoExceptionClause *clause1 = &header->clauses [cindex1];
5410 MonoExceptionClause *clause2 = &header->clauses [cindex2];
5412 if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset)
5413 encode_value (i, p, &p);
5415 encode_value (-1, p, &p);
5418 if (jinfo->num_clauses)
5419 encode_value (jinfo->num_clauses, p, &p);
5421 for (k = 0; k < jinfo->num_clauses; ++k) {
5422 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
5424 encode_value (ei->flags, p, &p);
5425 encode_value (ei->exvar_offset, p, &p);
5427 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
5428 encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
5430 if (ei->data.catch_class) {
5434 buf2 = g_malloc (4096);
5436 encode_klass_ref (acfg, ei->data.catch_class, p2, &p2);
5438 g_assert (len < 4096);
5439 encode_value (len, p, &p);
5440 memcpy (p, buf2, len);
5444 encode_value (0, p, &p);
5448 encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
5449 encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
5450 encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
5454 if (jinfo->has_try_block_holes) {
5455 MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
5456 for (i = 0; i < table->num_holes; ++i) {
5457 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
5458 encode_value (hole->clause, p, &p);
5459 encode_value (hole->length, p, &p);
5460 encode_value (hole->offset, p, &p);
5464 if (jinfo->has_arch_eh_info) {
5465 MonoArchEHJitInfo *eh_info;
5467 eh_info = mono_jit_info_get_arch_eh_info (jinfo);
5468 encode_value (eh_info->stack_size, p, &p);
5469 encode_value (eh_info->epilog_size, p, &p);
5472 if (jinfo->has_generic_jit_info) {
5473 MonoGenericJitInfo *gi = mono_jit_info_get_generic_jit_info (jinfo);
5474 MonoGenericSharingContext* gsctx = gi->generic_sharing_context;
5480 encode_value (gi->nlocs, p, &p);
5482 for (i = 0; i < gi->nlocs; ++i) {
5483 MonoDwarfLocListEntry *entry = &gi->locations [i];
5485 encode_value (entry->is_reg ? 1 : 0, p, &p);
5486 encode_value (entry->reg, p, &p);
5488 encode_value (entry->offset, p, &p);
5490 g_assert (entry->from == 0);
5492 encode_value (entry->from, p, &p);
5493 encode_value (entry->to, p, &p);
5496 if (!cfg->compile_llvm) {
5497 encode_value (gi->has_this ? 1 : 0, p, &p);
5498 encode_value (gi->this_reg, p, &p);
5499 encode_value (gi->this_offset, p, &p);
5504 * Need to encode jinfo->method too, since it is not equal to 'method'
5505 * when using generic sharing.
5507 buf2 = g_malloc (4096);
5509 encode_method_ref (acfg, jinfo->d.method, p2, &p2);
5511 g_assert (len < 4096);
5512 encode_value (len, p, &p);
5513 memcpy (p, buf2, len);
5517 if (gsctx && (gsctx->var_is_vt || gsctx->mvar_is_vt)) {
5518 MonoMethodInflated *inflated;
5519 MonoGenericContext *context;
5520 MonoGenericInst *inst;
5522 g_assert (jinfo->d.method->is_inflated);
5523 inflated = (MonoMethodInflated*)jinfo->d.method;
5524 context = &inflated->context;
5526 encode_value (1, p, &p);
5527 if (context->class_inst) {
5528 inst = context->class_inst;
5530 encode_value (inst->type_argc, p, &p);
5531 for (i = 0; i < inst->type_argc; ++i)
5532 encode_value (gsctx->var_is_vt [i], p, &p);
5534 encode_value (0, p, &p);
5536 if (context->method_inst) {
5537 inst = context->method_inst;
5539 encode_value (inst->type_argc, p, &p);
5540 for (i = 0; i < inst->type_argc; ++i)
5541 encode_value (gsctx->mvar_is_vt [i], p, &p);
5543 encode_value (0, p, &p);
5546 encode_value (0, p, &p);
5551 int il_offset, native_offset, last_il_offset, last_native_offset, j;
5553 encode_value (seq_points->len, p, &p);
5554 last_il_offset = last_native_offset = 0;
5555 for (i = 0; i < seq_points->len; ++i) {
5556 SeqPoint *sp = &seq_points->seq_points [i];
5557 il_offset = sp->il_offset;
5558 native_offset = sp->native_offset;
5559 encode_value (il_offset - last_il_offset, p, &p);
5560 encode_value (native_offset - last_native_offset, p, &p);
5561 last_il_offset = il_offset;
5562 last_native_offset = native_offset;
5564 encode_value (sp->flags, p, &p);
5565 encode_value (sp->next_len, p, &p);
5566 for (j = 0; j < sp->next_len; ++j)
5567 encode_value (sp->next [j], p, &p);
5571 g_assert (debug_info_size < buf_size);
5573 encode_value (debug_info_size, p, &p);
5574 if (debug_info_size) {
5575 memcpy (p, debug_info, debug_info_size);
5576 p += debug_info_size;
5577 g_free (debug_info);
5582 encode_value (cfg->gc_map_size, p, &p);
5583 /* The GC map requires 4 bytes of alignment */
5584 while ((gsize)p % 4)
5586 memcpy (p, cfg->gc_map, cfg->gc_map_size);
5587 p += cfg->gc_map_size;
5590 acfg->stats.ex_info_size += p - buf;
5592 g_assert (p - buf < buf_size);
5595 /* The GC Map requires 4 byte alignment */
5596 cfg->ex_info_offset = add_to_blob_aligned (acfg, buf, p - buf, cfg->gc_map ? 4 : 1);
5601 emit_klass_info (MonoAotCompile *acfg, guint32 token)
5603 MonoClass *klass = mono_class_get (acfg->image, token);
5605 int i, buf_size, res;
5606 gboolean no_special_static, cant_encode;
5607 gpointer iter = NULL;
5610 mono_loader_clear_error ();
5614 p = buf = g_malloc (buf_size);
5616 /* Mark as unusable */
5617 encode_value (-1, p, &p);
5619 res = add_to_blob (acfg, buf, p - buf);
5625 buf_size = 10240 + (klass->vtable_size * 16);
5626 p = buf = g_malloc (buf_size);
5630 mono_class_init (klass);
5632 mono_class_get_nested_types (klass, &iter);
5633 g_assert (klass->nested_classes_inited);
5635 mono_class_setup_vtable (klass);
5638 * Emit all the information which is required for creating vtables so
5639 * the runtime does not need to create the MonoMethod structures which
5640 * take up a lot of space.
5643 no_special_static = !mono_class_has_special_static_fields (klass);
5645 /* Check whenever we have enough info to encode the vtable */
5646 cant_encode = FALSE;
5647 for (i = 0; i < klass->vtable_size; ++i) {
5648 MonoMethod *cm = klass->vtable [i];
5650 if (cm && mono_method_signature (cm)->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, cm))
5654 mono_class_has_finalizer (klass);
5656 if (klass->generic_container || cant_encode) {
5657 encode_value (-1, p, &p);
5659 encode_value (klass->vtable_size, p, &p);
5660 encode_value ((klass->generic_container ? (1 << 8) : 0) | (no_special_static << 7) | (klass->has_static_refs << 6) | (klass->has_references << 5) | ((klass->blittable << 4) | ((klass->ext && klass->ext->nested_classes) ? 1 : 0) << 3) | (klass->has_cctor << 2) | (klass->has_finalize << 1) | klass->ghcimpl, p, &p);
5661 if (klass->has_cctor)
5662 encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
5663 if (klass->has_finalize)
5664 encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
5666 encode_value (klass->instance_size, p, &p);
5667 encode_value (mono_class_data_size (klass), p, &p);
5668 encode_value (klass->packing_size, p, &p);
5669 encode_value (klass->min_align, p, &p);
5671 for (i = 0; i < klass->vtable_size; ++i) {
5672 MonoMethod *cm = klass->vtable [i];
5675 encode_method_ref (acfg, cm, p, &p);
5677 encode_value (0, p, &p);
5681 acfg->stats.class_info_size += p - buf;
5683 g_assert (p - buf < buf_size);
5684 res = add_to_blob (acfg, buf, p - buf);
5691 get_plt_entry_debug_sym (MonoAotCompile *acfg, MonoJumpInfo *ji, GHashTable *cache)
5693 char *debug_sym = NULL;
5697 case MONO_PATCH_INFO_METHOD:
5698 debug_sym = get_debug_sym (ji->data.method, "plt_", cache);
5700 case MONO_PATCH_INFO_INTERNAL_METHOD:
5701 debug_sym = g_strdup_printf ("plt__jit_icall_%s", ji->data.name);
5703 case MONO_PATCH_INFO_CLASS_INIT:
5704 s = mono_type_get_name (&ji->data.klass->byval_arg);
5705 debug_sym = g_strdup_printf ("plt__class_init_%s", s);
5708 case MONO_PATCH_INFO_RGCTX_FETCH:
5709 debug_sym = g_strdup_printf ("plt__rgctx_fetch_%d", acfg->label_generator ++);
5711 case MONO_PATCH_INFO_ICALL_ADDR: {
5712 char *s = get_debug_sym (ji->data.method, "", cache);
5714 debug_sym = g_strdup_printf ("plt__icall_native_%s", s);
5718 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
5719 debug_sym = g_strdup_printf ("plt__jit_icall_native_%s", ji->data.name);
5721 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
5722 debug_sym = g_strdup_printf ("plt__generic_class_init");
5728 return sanitize_symbol (acfg, debug_sym);
5732 * Calls made from AOTed code are routed through a table of jumps similar to the
5733 * ELF PLT (Program Linkage Table). Initially the PLT entries jump to code which transfers
5734 * control to the AOT runtime through a trampoline.
5737 emit_plt (MonoAotCompile *acfg)
5743 sprintf (symbol, "plt");
5745 emit_section_change (acfg, ".text", 0);
5746 emit_alignment (acfg, NACL_SIZE(16, kNaClAlignment));
5747 emit_label (acfg, symbol);
5748 emit_label (acfg, acfg->plt_symbol);
5750 for (i = 0; i < acfg->plt_offset; ++i) {
5751 char *debug_sym = NULL;
5752 MonoPltEntry *plt_entry = NULL;
5757 * The first plt entry is unused.
5761 plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
5766 * If the target is directly callable, alias the plt symbol to point to
5768 * FIXME: Use this to simplify emit_and_reloc_code ().
5769 * FIXME: Avoid the got slot.
5770 * FIXME: Add support to the binary writer.
5772 if (ji && is_direct_callable (acfg, NULL, ji) && !acfg->use_bin_writer) {
5773 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, ji->data.method);
5776 if (acfg->thumb_mixed && !callee_cfg->compile_llvm) {
5777 /* LLVM calls the PLT entries using bl, so emit a stub */
5778 emit_set_thumb_mode (acfg);
5779 fprintf (acfg->fp, "\n.thumb_func\n");
5780 emit_label (acfg, plt_entry->llvm_symbol);
5781 fprintf (acfg->fp, "bx pc\n");
5782 fprintf (acfg->fp, "nop\n");
5783 emit_set_arm_mode (acfg);
5784 fprintf (acfg->fp, "b %s\n", callee_cfg->asm_symbol);
5786 fprintf (acfg->fp, "\n.set %s, %s\n", plt_entry->llvm_symbol, callee_cfg->asm_symbol);
5793 debug_sym = plt_entry->debug_sym;
5795 if (acfg->thumb_mixed && !plt_entry->jit_used)
5796 /* Emit only a thumb version */
5799 if (acfg->llvm && !acfg->thumb_mixed)
5800 emit_label (acfg, plt_entry->llvm_symbol);
5803 if (acfg->need_no_dead_strip) {
5804 emit_unset_mode (acfg);
5805 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
5807 emit_local_symbol (acfg, debug_sym, NULL, TRUE);
5808 emit_label (acfg, debug_sym);
5811 emit_label (acfg, plt_entry->symbol);
5813 arch_emit_plt_entry (acfg, i);
5816 emit_symbol_size (acfg, debug_sym, ".");
5819 if (acfg->thumb_mixed) {
5820 /* Make sure the ARM symbols don't alias the thumb ones */
5821 emit_zero_bytes (acfg, 16);
5824 * Emit a separate set of PLT entries using thumb2 which is called by LLVM generated
5827 for (i = 0; i < acfg->plt_offset; ++i) {
5828 char *debug_sym = NULL;
5829 MonoPltEntry *plt_entry = NULL;
5835 plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
5838 if (ji && is_direct_callable (acfg, NULL, ji) && !acfg->use_bin_writer)
5841 /* Skip plt entries not actually called by LLVM code */
5842 if (!plt_entry->llvm_used)
5845 if (acfg->aot_opts.write_symbols) {
5846 if (plt_entry->debug_sym)
5847 debug_sym = g_strdup_printf ("%s_thumb", plt_entry->debug_sym);
5851 #if defined(TARGET_MACH)
5852 fprintf (acfg->fp, " .thumb_func %s\n", debug_sym);
5853 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
5855 emit_local_symbol (acfg, debug_sym, NULL, TRUE);
5856 emit_label (acfg, debug_sym);
5858 fprintf (acfg->fp, "\n.thumb_func\n");
5860 emit_label (acfg, plt_entry->llvm_symbol);
5862 arch_emit_llvm_plt_entry (acfg, i);
5865 emit_symbol_size (acfg, debug_sym, ".");
5871 emit_symbol_size (acfg, acfg->plt_symbol, ".");
5873 sprintf (symbol, "plt_end");
5874 emit_label (acfg, symbol);
5878 * emit_trampoline_full:
5880 * If EMIT_TINFO is TRUE, emit additional information which can be used to create a MonoJitInfo for this trampoline by
5881 * create_jit_info_for_trampoline ().
5883 static G_GNUC_UNUSED void
5884 emit_trampoline_full (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info, gboolean emit_tinfo)
5886 char start_symbol [256];
5887 char end_symbol [256];
5889 guint32 buf_size, info_offset;
5890 MonoJumpInfo *patch_info;
5903 code_size = info->code_size;
5905 unwind_ops = info->unwind_ops;
5907 #ifdef __native_client_codegen__
5908 mono_nacl_fix_patches (code, ji);
5913 sprintf (start_symbol, "%s%s", acfg->user_symbol_prefix, name);
5915 emit_section_change (acfg, ".text", 0);
5916 emit_global (acfg, start_symbol, TRUE);
5917 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
5918 emit_label (acfg, start_symbol);
5920 sprintf (symbol, "%snamed_%s", acfg->temp_prefix, name);
5921 emit_label (acfg, symbol);
5924 * The code should access everything through the GOT, so we pass
5927 emit_and_reloc_code (acfg, NULL, code, code_size, ji, TRUE, NULL);
5929 emit_symbol_size (acfg, start_symbol, ".");
5932 sprintf (end_symbol, "%snamede_%s", acfg->temp_prefix, name);
5933 emit_label (acfg, end_symbol);
5938 /* Sort relocations */
5939 patches = g_ptr_array_new ();
5940 for (patch_info = ji; patch_info; patch_info = patch_info->next)
5941 if (patch_info->type != MONO_PATCH_INFO_NONE)
5942 g_ptr_array_add (patches, patch_info);
5943 g_ptr_array_sort (patches, compare_patches);
5945 buf_size = patches->len * 128 + 128;
5946 buf = g_malloc (buf_size);
5949 encode_patch_list (acfg, patches, patches->len, got_offset, p, &p);
5950 g_assert (p - buf < buf_size);
5952 sprintf (symbol, "%s%s_p", acfg->user_symbol_prefix, name);
5954 info_offset = add_to_blob (acfg, buf, p - buf);
5956 emit_section_change (acfg, RODATA_SECT, 0);
5957 emit_global (acfg, symbol, FALSE);
5958 emit_label (acfg, symbol);
5960 emit_int32 (acfg, info_offset);
5964 guint32 encoded_len;
5968 * Emit additional information which can be used to reconstruct a partial MonoTrampInfo.
5970 encoded = mono_unwind_ops_encode (info->unwind_ops, &encoded_len);
5971 uw_offset = get_unwind_info_offset (acfg, encoded, encoded_len);
5974 emit_symbol_diff (acfg, end_symbol, start_symbol, 0);
5975 emit_int32 (acfg, uw_offset);
5978 /* Emit debug info */
5982 sprintf (symbol, "%s", name);
5983 sprintf (symbol2, "%snamed_%s", acfg->temp_prefix, name);
5986 mono_dwarf_writer_emit_trampoline (acfg->dwarf, symbol, symbol2, NULL, NULL, code_size, unwind_ops);
5990 static G_GNUC_UNUSED void
5991 emit_trampoline (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info)
5993 emit_trampoline_full (acfg, got_offset, info, FALSE);
5997 emit_trampolines (MonoAotCompile *acfg)
6000 char end_symbol [256];
6001 int i, tramp_got_offset;
6002 MonoAotTrampoline ntype;
6003 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
6007 if (!acfg->aot_opts.full_aot)
6010 g_assert (acfg->image->assembly);
6012 /* Currently, we emit most trampolines into the mscorlib AOT image. */
6013 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
6014 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
6015 MonoTrampInfo *info;
6018 * Emit the generic trampolines.
6020 * We could save some code by treating the generic trampolines as a wrapper
6021 * method, but that approach has its own complexities, so we choose the simpler
6024 for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
6025 /* we overload the boolean here to indicate the slightly different trampoline needed, see mono_arch_create_generic_trampoline() */
6026 #ifdef DISABLE_REMOTING
6027 if (tramp_type == MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING)
6030 #ifndef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
6031 if (tramp_type == MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD)
6034 mono_arch_create_generic_trampoline (tramp_type, &info, acfg->aot_opts.use_trampolines_page? 2: TRUE);
6035 emit_trampoline (acfg, acfg->got_offset, info);
6038 mono_arch_get_nullified_class_init_trampoline (&info);
6039 emit_trampoline (acfg, acfg->got_offset, info);
6040 #if defined(MONO_ARCH_MONITOR_OBJECT_REG)
6041 mono_arch_create_monitor_enter_trampoline (&info, TRUE);
6042 emit_trampoline (acfg, acfg->got_offset, info);
6043 mono_arch_create_monitor_exit_trampoline (&info, TRUE);
6044 emit_trampoline (acfg, acfg->got_offset, info);
6047 mono_arch_create_generic_class_init_trampoline (&info, TRUE);
6048 emit_trampoline (acfg, acfg->got_offset, info);
6050 /* Emit the exception related code pieces */
6051 mono_arch_get_restore_context (&info, TRUE);
6052 emit_trampoline (acfg, acfg->got_offset, info);
6053 mono_arch_get_call_filter (&info, TRUE);
6054 emit_trampoline (acfg, acfg->got_offset, info);
6055 mono_arch_get_throw_exception (&info, TRUE);
6056 emit_trampoline (acfg, acfg->got_offset, info);
6057 mono_arch_get_rethrow_exception (&info, TRUE);
6058 emit_trampoline (acfg, acfg->got_offset, info);
6059 mono_arch_get_throw_corlib_exception (&info, TRUE);
6060 emit_trampoline (acfg, acfg->got_offset, info);
6062 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
6063 mono_arch_get_gsharedvt_trampoline (&info, TRUE);
6065 emit_trampoline_full (acfg, acfg->got_offset, info, TRUE);
6067 /* Create a separate out trampoline for more information in stack traces */
6068 info->name = g_strdup ("gsharedvt_out_trampoline");
6069 emit_trampoline_full (acfg, acfg->got_offset, info, TRUE);
6073 #if defined(MONO_ARCH_HAVE_GET_TRAMPOLINES)
6075 GSList *l = mono_arch_get_trampolines (TRUE);
6078 MonoTrampInfo *info = l->data;
6080 emit_trampoline (acfg, acfg->got_offset, info);
6086 for (i = 0; i < acfg->aot_opts.nrgctx_fetch_trampolines; ++i) {
6089 offset = MONO_RGCTX_SLOT_MAKE_RGCTX (i);
6090 mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
6091 emit_trampoline (acfg, acfg->got_offset, info);
6093 offset = MONO_RGCTX_SLOT_MAKE_MRGCTX (i);
6094 mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
6095 emit_trampoline (acfg, acfg->got_offset, info);
6098 #ifdef MONO_ARCH_HAVE_GENERAL_RGCTX_LAZY_FETCH_TRAMPOLINE
6099 mono_arch_create_general_rgctx_lazy_fetch_trampoline (&info, TRUE);
6100 emit_trampoline (acfg, acfg->got_offset, info);
6106 /* delegate_invoke_impl trampolines */
6107 l = mono_arch_get_delegate_invoke_impls ();
6109 MonoTrampInfo *info = l->data;
6111 emit_trampoline (acfg, acfg->got_offset, info);
6116 #endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
6118 /* Emit trampolines which are numerous */
6121 * These include the following:
6122 * - specific trampolines
6123 * - static rgctx invoke trampolines
6125 * These trampolines have the same code, they are parameterized by GOT
6127 * They are defined in this file, in the arch_... routines instead of
6128 * in tramp-<ARCH>.c, since it is easier to do it this way.
6132 * When running in aot-only mode, we can't create specific trampolines at
6133 * runtime, so we create a few, and save them in the AOT file.
6134 * Normal trampolines embed their argument as a literal inside the
6135 * trampoline code, we can't do that here, so instead we embed an offset
6136 * which needs to be added to the trampoline address to get the address of
6137 * the GOT slot which contains the argument value.
6138 * The generated trampolines jump to the generic trampolines using another
6139 * GOT slot, which will be setup by the AOT loader to point to the
6140 * generic trampoline code of the given type.
6144 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for
6148 emit_section_change (acfg, ".text", 0);
6150 tramp_got_offset = acfg->got_offset;
6152 for (ntype = 0; ntype < MONO_AOT_TRAMP_NUM; ++ntype) {
6154 case MONO_AOT_TRAMP_SPECIFIC:
6155 sprintf (symbol, "specific_trampolines");
6157 case MONO_AOT_TRAMP_STATIC_RGCTX:
6158 sprintf (symbol, "static_rgctx_trampolines");
6160 case MONO_AOT_TRAMP_IMT_THUNK:
6161 sprintf (symbol, "imt_thunks");
6163 case MONO_AOT_TRAMP_GSHAREDVT_ARG:
6164 sprintf (symbol, "gsharedvt_arg_trampolines");
6167 g_assert_not_reached ();
6170 sprintf (end_symbol, "%s_e", symbol);
6172 if (acfg->aot_opts.write_symbols)
6173 emit_local_symbol (acfg, symbol, end_symbol, TRUE);
6175 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
6176 emit_label (acfg, symbol);
6178 acfg->trampoline_got_offset_base [ntype] = tramp_got_offset;
6180 for (i = 0; i < acfg->num_trampolines [ntype]; ++i) {
6184 case MONO_AOT_TRAMP_SPECIFIC:
6185 arch_emit_specific_trampoline (acfg, tramp_got_offset, &tramp_size);
6186 tramp_got_offset += 2;
6188 case MONO_AOT_TRAMP_STATIC_RGCTX:
6189 arch_emit_static_rgctx_trampoline (acfg, tramp_got_offset, &tramp_size);
6190 tramp_got_offset += 2;
6192 case MONO_AOT_TRAMP_IMT_THUNK:
6193 arch_emit_imt_thunk (acfg, tramp_got_offset, &tramp_size);
6194 tramp_got_offset += 1;
6196 case MONO_AOT_TRAMP_GSHAREDVT_ARG:
6197 arch_emit_gsharedvt_arg_trampoline (acfg, tramp_got_offset, &tramp_size);
6198 tramp_got_offset += 2;
6201 g_assert_not_reached ();
6203 #ifdef __native_client_codegen__
6204 /* align to avoid 32-byte boundary crossings */
6205 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
6208 if (!acfg->trampoline_size [ntype]) {
6209 g_assert (tramp_size);
6210 acfg->trampoline_size [ntype] = tramp_size;
6214 emit_label (acfg, end_symbol);
6215 emit_int32 (acfg, 0);
6218 arch_emit_specific_trampoline_pages (acfg);
6220 /* Reserve some entries at the end of the GOT for our use */
6221 acfg->num_trampoline_got_entries = tramp_got_offset - acfg->got_offset;
6224 acfg->got_offset += acfg->num_trampoline_got_entries;
6228 str_begins_with (const char *str1, const char *str2)
6230 int len = strlen (str2);
6231 return strncmp (str1, str2, len) == 0;
6235 mono_aot_readonly_field_override (MonoClassField *field)
6238 for (rdv = readonly_values; rdv; rdv = rdv->next) {
6239 char *p = rdv->name;
6241 len = strlen (field->parent->name_space);
6242 if (strncmp (p, field->parent->name_space, len))
6247 len = strlen (field->parent->name);
6248 if (strncmp (p, field->parent->name, len))
6253 if (strcmp (p, field->name))
6255 switch (rdv->type) {
6257 return &rdv->value.i1;
6259 return &rdv->value.i2;
6261 return &rdv->value.i4;
6270 add_readonly_value (MonoAotOptions *opts, const char *val)
6275 /* the format of val is:
6276 * namespace.typename.fieldname=type/value
6277 * type can be i1 for uint8/int8/boolean, i2 for uint16/int16/char, i4 for uint32/int32
6279 fval = strrchr (val, '/');
6281 fprintf (stderr, "AOT : invalid format for readonly field '%s', missing /.\n", val);
6284 tval = strrchr (val, '=');
6286 fprintf (stderr, "AOT : invalid format for readonly field '%s', missing =.\n", val);
6289 rdv = g_new0 (ReadOnlyValue, 1);
6290 rdv->name = g_malloc0 (tval - val + 1);
6291 memcpy (rdv->name, val, tval - val);
6294 if (strncmp (tval, "i1", 2) == 0) {
6295 rdv->value.i1 = atoi (fval);
6296 rdv->type = MONO_TYPE_I1;
6297 } else if (strncmp (tval, "i2", 2) == 0) {
6298 rdv->value.i2 = atoi (fval);
6299 rdv->type = MONO_TYPE_I2;
6300 } else if (strncmp (tval, "i4", 2) == 0) {
6301 rdv->value.i4 = atoi (fval);
6302 rdv->type = MONO_TYPE_I4;
6304 fprintf (stderr, "AOT : unsupported type for readonly field '%s'.\n", tval);
6307 rdv->next = readonly_values;
6308 readonly_values = rdv;
6312 mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
6314 gchar **args, **ptr;
6316 args = g_strsplit (aot_options ? aot_options : "", ",", -1);
6317 for (ptr = args; ptr && *ptr; ptr ++) {
6318 const char *arg = *ptr;
6320 if (str_begins_with (arg, "outfile=")) {
6321 opts->outfile = g_strdup (arg + strlen ("outfile="));
6322 } else if (str_begins_with (arg, "save-temps")) {
6323 opts->save_temps = TRUE;
6324 } else if (str_begins_with (arg, "keep-temps")) {
6325 opts->save_temps = TRUE;
6326 } else if (str_begins_with (arg, "write-symbols")) {
6327 opts->write_symbols = TRUE;
6328 } else if (str_begins_with (arg, "no-write-symbols")) {
6329 opts->write_symbols = FALSE;
6330 } else if (str_begins_with (arg, "metadata-only")) {
6331 opts->metadata_only = TRUE;
6332 } else if (str_begins_with (arg, "bind-to-runtime-version")) {
6333 opts->bind_to_runtime_version = TRUE;
6334 } else if (str_begins_with (arg, "full")) {
6335 opts->full_aot = TRUE;
6336 } else if (str_begins_with (arg, "threads=")) {
6337 opts->nthreads = atoi (arg + strlen ("threads="));
6338 } else if (str_begins_with (arg, "static")) {
6339 opts->static_link = TRUE;
6340 opts->no_dlsym = TRUE;
6341 } else if (str_begins_with (arg, "asmonly")) {
6342 opts->asm_only = TRUE;
6343 } else if (str_begins_with (arg, "asmwriter")) {
6344 opts->asm_writer = TRUE;
6345 } else if (str_begins_with (arg, "nodebug")) {
6346 opts->nodebug = TRUE;
6347 } else if (str_begins_with (arg, "dwarfdebug")) {
6348 opts->dwarf_debug = TRUE;
6349 } else if (str_begins_with (arg, "nopagetrampolines")) {
6350 opts->use_trampolines_page = FALSE;
6351 } else if (str_begins_with (arg, "ntrampolines=")) {
6352 opts->ntrampolines = atoi (arg + strlen ("ntrampolines="));
6353 } else if (str_begins_with (arg, "nrgctx-trampolines=")) {
6354 opts->nrgctx_trampolines = atoi (arg + strlen ("nrgctx-trampolines="));
6355 } else if (str_begins_with (arg, "nimt-trampolines=")) {
6356 opts->nimt_trampolines = atoi (arg + strlen ("nimt-trampolines="));
6357 } else if (str_begins_with (arg, "ngsharedvt-trampolines=")) {
6358 opts->ngsharedvt_arg_trampolines = atoi (arg + strlen ("ngsharedvt-trampolines="));
6359 } else if (str_begins_with (arg, "autoreg")) {
6360 opts->autoreg = TRUE;
6361 } else if (str_begins_with (arg, "tool-prefix=")) {
6362 opts->tool_prefix = g_strdup (arg + strlen ("tool-prefix="));
6363 } else if (str_begins_with (arg, "soft-debug")) {
6364 opts->soft_debug = TRUE;
6365 } else if (str_begins_with (arg, "direct-pinvoke")) {
6366 opts->direct_pinvoke = TRUE;
6367 } else if (str_begins_with (arg, "direct-icalls")) {
6368 opts->direct_icalls = TRUE;
6369 #if defined(TARGET_ARM) || defined(TARGET_ARM64)
6370 } else if (str_begins_with (arg, "iphone-abi")) {
6371 // older full-aot users did depend on this.
6373 } else if (str_begins_with (arg, "no-direct-calls")) {
6374 opts->no_direct_calls = TRUE;
6375 } else if (str_begins_with (arg, "print-skipped")) {
6376 opts->print_skipped_methods = TRUE;
6377 } else if (str_begins_with (arg, "stats")) {
6379 } else if (str_begins_with (arg, "no-instances")) {
6380 opts->no_instances = TRUE;
6381 } else if (str_begins_with (arg, "log-generics")) {
6382 opts->log_generics = TRUE;
6383 } else if (str_begins_with (arg, "log-instances=")) {
6384 opts->log_instances = TRUE;
6385 opts->instances_logfile_path = g_strdup (arg + strlen ("log-instances="));
6386 } else if (str_begins_with (arg, "log-instances")) {
6387 opts->log_instances = TRUE;
6388 } else if (str_begins_with (arg, "internal-logfile=")) {
6389 opts->logfile = g_strdup (arg + strlen ("internal-logfile="));
6390 } else if (str_begins_with (arg, "mtriple=")) {
6391 opts->mtriple = g_strdup (arg + strlen ("mtriple="));
6392 } else if (str_begins_with (arg, "llvm-path=")) {
6393 opts->llvm_path = g_strdup (arg + strlen ("llvm-path="));
6394 } else if (str_begins_with (arg, "readonly-value=")) {
6395 add_readonly_value (opts, arg + strlen ("readonly-value="));
6396 } else if (str_begins_with (arg, "info")) {
6397 printf ("AOT target setup: %s.\n", AOT_TARGET_STR);
6399 } else if (str_begins_with (arg, "gc-maps")) {
6400 mini_gc_enable_gc_maps_for_aot ();
6401 } else if (str_begins_with (arg, "help") || str_begins_with (arg, "?")) {
6402 printf ("Supported options for --aot:\n");
6403 printf (" outfile=\n");
6404 printf (" save-temps\n");
6405 printf (" keep-temps\n");
6406 printf (" write-symbols\n");
6407 printf (" metadata-only\n");
6408 printf (" bind-to-runtime-version\n");
6410 printf (" threads=\n");
6411 printf (" static\n");
6412 printf (" asmonly\n");
6413 printf (" asmwriter\n");
6414 printf (" nodebug\n");
6415 printf (" dwarfdebug\n");
6416 printf (" ntrampolines=\n");
6417 printf (" nrgctx-trampolines=\n");
6418 printf (" nimt-trampolines=\n");
6419 printf (" ngsharedvt-trampolines=\n");
6420 printf (" autoreg\n");
6421 printf (" tool-prefix=\n");
6422 printf (" readonly-value=\n");
6423 printf (" soft-debug\n");
6424 printf (" gc-maps\n");
6425 printf (" print-skipped\n");
6426 printf (" no-instances\n");
6427 printf (" stats\n");
6429 printf (" help/?\n");
6432 fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
6437 if (opts->use_trampolines_page) {
6438 opts->ntrampolines = 0;
6439 opts->nrgctx_trampolines = 0;
6440 opts->nimt_trampolines = 0;
6441 opts->ngsharedvt_arg_trampolines = 0;
6447 add_token_info_hash (gpointer key, gpointer value, gpointer user_data)
6449 MonoMethod *method = (MonoMethod*)key;
6450 MonoJumpInfoToken *ji = (MonoJumpInfoToken*)value;
6451 MonoJumpInfoToken *new_ji = g_new0 (MonoJumpInfoToken, 1);
6452 MonoAotCompile *acfg = user_data;
6454 new_ji->image = ji->image;
6455 new_ji->token = ji->token;
6456 g_hash_table_insert (acfg->token_info_hash, method, new_ji);
6460 can_encode_class (MonoAotCompile *acfg, MonoClass *klass)
6462 if (klass->type_token)
6464 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR) || (klass->byval_arg.type == MONO_TYPE_PTR))
6467 return can_encode_class (acfg, klass->element_class);
6472 can_encode_method (MonoAotCompile *acfg, MonoMethod *method)
6474 if (method->wrapper_type) {
6475 switch (method->wrapper_type) {
6476 case MONO_WRAPPER_NONE:
6477 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
6478 case MONO_WRAPPER_XDOMAIN_INVOKE:
6479 case MONO_WRAPPER_STFLD:
6480 case MONO_WRAPPER_LDFLD:
6481 case MONO_WRAPPER_LDFLDA:
6482 case MONO_WRAPPER_LDFLD_REMOTE:
6483 case MONO_WRAPPER_STFLD_REMOTE:
6484 case MONO_WRAPPER_STELEMREF:
6485 case MONO_WRAPPER_ISINST:
6486 case MONO_WRAPPER_PROXY_ISINST:
6487 case MONO_WRAPPER_ALLOC:
6488 case MONO_WRAPPER_REMOTING_INVOKE:
6489 case MONO_WRAPPER_UNKNOWN:
6490 case MONO_WRAPPER_WRITE_BARRIER:
6491 case MONO_WRAPPER_DELEGATE_INVOKE:
6492 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
6493 case MONO_WRAPPER_DELEGATE_END_INVOKE:
6494 case MONO_WRAPPER_SYNCHRONIZED:
6496 case MONO_WRAPPER_MANAGED_TO_MANAGED:
6497 case MONO_WRAPPER_CASTCLASS: {
6498 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
6507 //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
6511 if (!method->token) {
6512 /* The method is part of a constructed type like Int[,].Set (). */
6513 if (!g_hash_table_lookup (acfg->token_info_hash, method)) {
6514 if (method->klass->rank)
6524 can_encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
6526 switch (patch_info->type) {
6527 case MONO_PATCH_INFO_METHOD:
6528 case MONO_PATCH_INFO_METHODCONST:
6529 case MONO_PATCH_INFO_METHOD_CODE_SLOT: {
6530 MonoMethod *method = patch_info->data.method;
6532 return can_encode_method (acfg, method);
6534 case MONO_PATCH_INFO_VTABLE:
6535 case MONO_PATCH_INFO_CLASS_INIT:
6536 case MONO_PATCH_INFO_CLASS:
6537 case MONO_PATCH_INFO_IID:
6538 case MONO_PATCH_INFO_ADJUSTED_IID:
6539 if (!can_encode_class (acfg, patch_info->data.klass)) {
6540 //printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
6544 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE: {
6545 if (!can_encode_class (acfg, patch_info->data.del_tramp->klass)) {
6546 //printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
6551 case MONO_PATCH_INFO_RGCTX_FETCH: {
6552 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
6554 if (!can_encode_method (acfg, entry->method))
6556 if (!can_encode_patch (acfg, entry->data))
6570 * AOT compile a given method.
6571 * This function might be called by multiple threads, so it must be thread-safe.
6574 compile_method (MonoAotCompile *acfg, MonoMethod *method)
6577 MonoJumpInfo *patch_info;
6580 MonoMethod *wrapped;
6582 if (acfg->aot_opts.metadata_only)
6585 mono_acfg_lock (acfg);
6586 index = get_method_index (acfg, method);
6587 mono_acfg_unlock (acfg);
6589 /* fixme: maybe we can also precompile wrapper methods */
6590 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
6591 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
6592 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
6593 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
6597 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
6600 wrapped = mono_marshal_method_from_wrapper (method);
6601 if (wrapped && (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && wrapped->is_generic)
6602 // FIXME: The wrapper should be generic too, but it is not
6605 if (method->wrapper_type == MONO_WRAPPER_COMINTEROP)
6608 InterlockedIncrement (&acfg->stats.mcount);
6611 if (method->is_generic || method->klass->generic_container) {
6612 InterlockedIncrement (&acfg->stats.genericcount);
6617 //acfg->aot_opts.print_skipped_methods = TRUE;
6620 * Since these methods are the only ones which are compiled with
6621 * AOT support, and they are not used by runtime startup/shutdown code,
6622 * the runtime will not see AOT methods during AOT compilation,so it
6623 * does not need to support them by creating a fake GOT etc.
6625 cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), acfg->aot_opts.full_aot ? (JIT_FLAG_AOT|JIT_FLAG_FULL_AOT) : (JIT_FLAG_AOT), 0);
6626 mono_loader_clear_error ();
6628 if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
6629 if (acfg->aot_opts.print_skipped_methods)
6630 printf ("Skip (gshared failure): %s (%s)\n", mono_method_full_name (method, TRUE), cfg->exception_message);
6631 InterlockedIncrement (&acfg->stats.genericcount);
6634 if (cfg->exception_type != MONO_EXCEPTION_NONE) {
6635 if (acfg->aot_opts.print_skipped_methods)
6636 printf ("Skip (JIT failure): %s\n", mono_method_full_name (method, TRUE));
6637 /* Let the exception happen at runtime */
6641 if (cfg->disable_aot) {
6642 if (acfg->aot_opts.print_skipped_methods)
6643 printf ("Skip (disabled): %s\n", mono_method_full_name (method, TRUE));
6644 InterlockedIncrement (&acfg->stats.ocount);
6645 mono_destroy_compile (cfg);
6648 cfg->method_index = index;
6650 /* Nullify patches which need no aot processing */
6651 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6652 switch (patch_info->type) {
6653 case MONO_PATCH_INFO_LABEL:
6654 case MONO_PATCH_INFO_BB:
6655 patch_info->type = MONO_PATCH_INFO_NONE;
6662 /* Collect method->token associations from the cfg */
6663 mono_acfg_lock (acfg);
6664 g_hash_table_foreach (cfg->token_info_hash, add_token_info_hash, acfg);
6665 mono_acfg_unlock (acfg);
6668 * Check for absolute addresses.
6671 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6672 switch (patch_info->type) {
6673 case MONO_PATCH_INFO_ABS:
6674 /* unable to handle this */
6683 if (acfg->aot_opts.print_skipped_methods)
6684 printf ("Skip (abs call): %s\n", mono_method_full_name (method, TRUE));
6685 InterlockedIncrement (&acfg->stats.abscount);
6686 mono_destroy_compile (cfg);
6690 /* Lock for the rest of the code */
6691 mono_acfg_lock (acfg);
6694 * Check for methods/klasses we can't encode.
6697 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6698 if (!can_encode_patch (acfg, patch_info))
6703 if (acfg->aot_opts.print_skipped_methods)
6704 printf ("Skip (patches): %s\n", mono_method_full_name (method, TRUE));
6705 acfg->stats.ocount++;
6706 mono_destroy_compile (cfg);
6707 mono_acfg_unlock (acfg);
6711 if (method->is_inflated && acfg->aot_opts.log_instances) {
6712 if (acfg->instances_logfile)
6713 fprintf (acfg->instances_logfile, "%s ### %d\n", mono_method_full_name (method, TRUE), cfg->code_size);
6715 printf ("%s ### %d\n", mono_method_full_name (method, TRUE), cfg->code_size);
6718 /* Adds generic instances referenced by this method */
6720 * The depth is used to avoid infinite loops when generic virtual recursion is
6723 depth = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_depth, method));
6724 if (!acfg->aot_opts.no_instances && depth < 32) {
6725 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6726 switch (patch_info->type) {
6727 case MONO_PATCH_INFO_METHOD: {
6728 MonoMethod *m = patch_info->data.method;
6729 if (m->is_inflated) {
6730 if (!(mono_class_generic_sharing_enabled (m->klass) &&
6731 mono_method_is_generic_sharable_full (m, FALSE, FALSE, FALSE)) &&
6732 !method_has_type_vars (m)) {
6733 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
6734 if (acfg->aot_opts.full_aot)
6735 add_extra_method_with_depth (acfg, mono_marshal_get_native_wrapper (m, TRUE, TRUE), depth + 1);
6737 add_extra_method_with_depth (acfg, m, depth + 1);
6738 add_types_from_method_header (acfg, m);
6741 add_generic_class_with_depth (acfg, m->klass, depth + 5, "method");
6743 if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && !strcmp (m->name, "ElementAddr"))
6744 add_extra_method_with_depth (acfg, m, depth + 1);
6747 case MONO_PATCH_INFO_VTABLE: {
6748 MonoClass *klass = patch_info->data.klass;
6750 if (klass->generic_class && !mini_class_is_generic_sharable (klass))
6751 add_generic_class_with_depth (acfg, klass, depth + 5, "vtable");
6754 case MONO_PATCH_INFO_SFLDA: {
6755 MonoClass *klass = patch_info->data.field->parent;
6757 /* The .cctor needs to run at runtime. */
6758 if (klass->generic_class && !mono_generic_context_is_sharable (&klass->generic_class->context, FALSE) && mono_class_get_cctor (klass))
6759 add_extra_method_with_depth (acfg, mono_class_get_cctor (klass), depth + 1);
6768 /* Determine whenever the method has GOT slots */
6769 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6770 switch (patch_info->type) {
6771 case MONO_PATCH_INFO_GOT_OFFSET:
6772 case MONO_PATCH_INFO_NONE:
6773 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
6775 case MONO_PATCH_INFO_IMAGE:
6776 /* The assembly is stored in GOT slot 0 */
6777 if (patch_info->data.image != acfg->image)
6778 cfg->has_got_slots = TRUE;
6781 if (!is_plt_patch (patch_info))
6782 cfg->has_got_slots = TRUE;
6787 if (!cfg->has_got_slots)
6788 InterlockedIncrement (&acfg->stats.methods_without_got_slots);
6791 * FIXME: Instead of this mess, allocate the patches from the aot mempool.
6793 /* Make a copy of the patch info which is in the mempool */
6795 MonoJumpInfo *patches = NULL, *patches_end = NULL;
6797 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
6798 MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
6801 patches = new_patch_info;
6803 patches_end->next = new_patch_info;
6804 patches_end = new_patch_info;
6806 cfg->patch_info = patches;
6808 /* Make a copy of the unwind info */
6810 GSList *l, *unwind_ops;
6814 for (l = cfg->unwind_ops; l; l = l->next) {
6815 op = mono_mempool_alloc (acfg->mempool, sizeof (MonoUnwindOp));
6816 memcpy (op, l->data, sizeof (MonoUnwindOp));
6817 unwind_ops = g_slist_prepend_mempool (acfg->mempool, unwind_ops, op);
6819 cfg->unwind_ops = g_slist_reverse (unwind_ops);
6821 /* Make a copy of the argument/local info */
6823 MonoInst **args, **locals;
6824 MonoMethodSignature *sig;
6825 MonoMethodHeader *header;
6828 sig = mono_method_signature (method);
6829 args = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * (sig->param_count + sig->hasthis));
6830 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
6831 args [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
6832 memcpy (args [i], cfg->args [i], sizeof (MonoInst));
6836 header = mono_method_get_header (method);
6837 locals = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * header->num_locals);
6838 for (i = 0; i < header->num_locals; ++i) {
6839 locals [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
6840 memcpy (locals [i], cfg->locals [i], sizeof (MonoInst));
6842 cfg->locals = locals;
6845 /* Free some fields used by cfg to conserve memory */
6846 mono_mempool_destroy (cfg->mempool);
6847 cfg->mempool = NULL;
6848 g_free (cfg->varinfo);
6849 cfg->varinfo = NULL;
6853 mono_regstate_free (cfg->rs);
6857 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
6859 while (index >= acfg->cfgs_size) {
6860 MonoCompile **new_cfgs;
6863 new_size = acfg->cfgs_size * 2;
6864 new_cfgs = g_new0 (MonoCompile*, new_size);
6865 memcpy (new_cfgs, acfg->cfgs, sizeof (MonoCompile*) * acfg->cfgs_size);
6866 g_free (acfg->cfgs);
6867 acfg->cfgs = new_cfgs;
6868 acfg->cfgs_size = new_size;
6870 acfg->cfgs [index] = cfg;
6872 g_hash_table_insert (acfg->method_to_cfg, cfg->orig_method, cfg);
6875 if (cfg->orig_method->wrapper_type)
6876 g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
6879 mono_acfg_unlock (acfg);
6881 InterlockedIncrement (&acfg->stats.ccount);
6885 compile_thread_main (gpointer *user_data)
6887 MonoDomain *domain = user_data [0];
6888 MonoAotCompile *acfg = user_data [1];
6889 GPtrArray *methods = user_data [2];
6892 mono_thread_attach (domain);
6894 for (i = 0; i < methods->len; ++i)
6895 compile_method (acfg, g_ptr_array_index (methods, i));
6899 load_profile_files (MonoAotCompile *acfg)
6903 int file_index, res, method_index, i;
6906 GList *unordered, *l;
6911 tmp = g_strdup_printf ("%s/.mono/aot-profile-data/%s-%d", g_get_home_dir (), acfg->image->assembly_name, file_index);
6913 if (!g_file_test (tmp, G_FILE_TEST_IS_REGULAR)) {
6918 infile = fopen (tmp, "r");
6921 printf ("Using profile data file '%s'\n", tmp);
6926 res = fscanf (infile, "%32s\n", ver);
6927 if ((res != 1) || strcmp (ver, "#VER:2") != 0) {
6928 printf ("Profile file has wrong version or invalid.\n");
6935 MonoMethodDesc *desc;
6938 if (fgets (name, 1023, infile) == NULL)
6941 /* Kill the newline */
6942 if (strlen (name) > 0)
6943 name [strlen (name) - 1] = '\0';
6945 desc = mono_method_desc_new (name, TRUE);
6947 method = mono_method_desc_search_in_image (desc, acfg->image);
6949 if (method && mono_method_get_token (method)) {
6950 token = mono_method_get_token (method);
6951 method_index = mono_metadata_token_index (token) - 1;
6954 for (i = 0; i < acfg->method_order->len; ++i) {
6955 if (g_ptr_array_index (acfg->method_order, i) == GUINT_TO_POINTER (method_index)) {
6961 g_ptr_array_add (acfg->method_order, GUINT_TO_POINTER (method_index));
6963 //printf ("No method found matching '%s'.\n", name);
6969 /* Add missing methods */
6971 for (method_index = 0; method_index < acfg->image->tables [MONO_TABLE_METHOD].rows; ++method_index) {
6973 for (i = 0; i < acfg->method_order->len; ++i) {
6974 if (g_ptr_array_index (acfg->method_order, i) == GUINT_TO_POINTER (method_index)) {
6980 unordered = g_list_prepend (unordered, GUINT_TO_POINTER (method_index));
6982 unordered = g_list_reverse (unordered);
6983 for (l = unordered; l; l = l->next)
6984 g_ptr_array_add (acfg->method_order, l->data);
6987 /* Used by the LLVM backend */
6989 mono_aot_get_got_offset (MonoJumpInfo *ji)
6991 return get_got_offset (llvm_acfg, ji);
6995 mono_aot_get_method_name (MonoCompile *cfg)
6997 if (llvm_acfg->aot_opts.static_link)
6998 /* Include the assembly name too to avoid duplicate symbol errors */
6999 return g_strdup_printf ("%s_%s", llvm_acfg->assembly_name_sym, get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash));
7001 return get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash);
7005 mono_aot_get_plt_symbol (MonoJumpInfoType type, gconstpointer data)
7007 MonoJumpInfo *ji = mono_mempool_alloc (llvm_acfg->mempool, sizeof (MonoJumpInfo));
7008 MonoPltEntry *plt_entry;
7011 ji->data.target = data;
7013 if (!can_encode_patch (llvm_acfg, ji))
7016 plt_entry = get_plt_entry (llvm_acfg, ji);
7017 plt_entry->llvm_used = TRUE;
7019 #if defined(TARGET_MACH)
7020 return g_strdup_printf (plt_entry->llvm_symbol + strlen (llvm_acfg->llvm_label_prefix));
7022 return g_strdup_printf (plt_entry->llvm_symbol);
7027 mono_aot_get_method_index (MonoMethod *method)
7029 g_assert (llvm_acfg);
7030 return get_method_index (llvm_acfg, method);
7034 mono_aot_patch_info_dup (MonoJumpInfo* ji)
7038 mono_acfg_lock (llvm_acfg);
7039 res = mono_patch_info_dup_mp (llvm_acfg->mempool, ji);
7040 mono_acfg_unlock (llvm_acfg);
7050 * Emit the LLVM code into an LLVM bytecode file, and compile it using the LLVM
7054 emit_llvm_file (MonoAotCompile *acfg)
7056 char *command, *opts, *tempbc;
7058 MonoJumpInfo *patch_info;
7061 * When using LLVM, we let llvm emit the got since the LLVM IL needs to refer
7065 /* Compute the final size of the got */
7066 for (i = 0; i < acfg->nmethods; ++i) {
7067 if (acfg->cfgs [i]) {
7068 for (patch_info = acfg->cfgs [i]->patch_info; patch_info; patch_info = patch_info->next) {
7069 if (patch_info->type != MONO_PATCH_INFO_NONE) {
7070 if (!is_plt_patch (patch_info))
7071 get_got_offset (acfg, patch_info);
7073 get_plt_entry (acfg, patch_info);
7079 acfg->final_got_size = acfg->got_offset + acfg->plt_offset;
7081 if (acfg->aot_opts.full_aot) {
7085 * Need to add the got entries used by the trampolines.
7086 * This is only a conservative approximation.
7088 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
7089 /* For the generic + rgctx trampolines */
7090 acfg->final_got_size += 400;
7091 /* For the specific trampolines */
7092 for (ntype = 0; ntype < MONO_AOT_TRAMP_NUM; ++ntype)
7093 acfg->final_got_size += acfg->num_trampolines [ntype] * 2;
7098 tempbc = g_strdup_printf ("%s.bc", acfg->tmpbasename);
7099 mono_llvm_emit_aot_module (tempbc, acfg->final_got_size);
7103 * FIXME: Experiment with adding optimizations, the -std-compile-opts set takes
7104 * a lot of time, and doesn't seem to save much space.
7105 * The following optimizations cannot be enabled:
7107 * - 'jump-threading' changes our blockaddress references to int constants.
7108 * - 'basiccg' fails because it contains:
7109 * if (CS && !isa<IntrinsicInst>(II)) {
7110 * and isa<IntrinsicInst> is false for invokes to intrinsics (iltests.exe).
7111 * - 'prune-eh' and 'functionattrs' depend on 'basiccg'.
7112 * The opt list below was produced by taking the output of:
7113 * llvm-as < /dev/null | opt -O2 -disable-output -debug-pass=Arguments
7114 * then removing tailcallelim + the global opts.
7115 * strip-dead-prototypes deletes unused intrinsics definitions.
7117 opts = g_strdup ("-instcombine -simplifycfg");
7118 //opts = g_strdup ("-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loop-simplify -domfrontier -loop-simplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loop-simplify -lcssa -iv-users -indvars -loop-deletion -loop-simplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -simplifycfg -domtree -verify");
7119 /* The dse pass is disabled because of #13734 and #17616 */
7121 * The dse bug is in DeadStoreElimination.cpp:isOverwrite ():
7122 * // If we have no DataLayout information around, then the size of the store
7123 * // is inferrable from the pointee type. If they are the same type, then
7124 * // we know that the store is safe.
7125 * if (AA.getDataLayout() == 0 &&
7126 * Later.Ptr->getType() == Earlier.Ptr->getType()) {
7127 * return OverwriteComplete;
7128 * Here, if 'Earlier' refers to a memset, and Later has no size info, it mistakenly thinks the memset is redundant.
7130 opts = g_strdup ("-targetlibinfo -no-aa -basicaa -notti -instcombine -simplifycfg -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");
7132 command = g_strdup_printf ("%sopt -f %s -o \"%s.opt.bc\" \"%s.bc\"", acfg->aot_opts.llvm_path, opts, acfg->tmpbasename, acfg->tmpbasename);
7133 aot_printf (acfg, "Executing opt: %s\n", command);
7134 if (system (command) != 0)
7139 if (!acfg->llc_args)
7140 acfg->llc_args = g_string_new ("");
7142 /* Verbose asm slows down llc greatly */
7143 g_string_append (acfg->llc_args, " -asm-verbose=false");
7145 if (acfg->aot_opts.mtriple)
7146 g_string_append_printf (acfg->llc_args, " -mtriple=%s", acfg->aot_opts.mtriple);
7148 #if defined(TARGET_MACH) && defined(TARGET_ARM)
7149 /* ios requires PIC code now */
7150 g_string_append_printf (acfg->llc_args, " -relocation-model=pic");
7152 if (llvm_acfg->aot_opts.static_link)
7153 g_string_append_printf (acfg->llc_args, " -relocation-model=static");
7155 g_string_append_printf (acfg->llc_args, " -relocation-model=pic");
7157 unlink (acfg->tmpfname);
7159 command = g_strdup_printf ("%sllc %s -disable-gnu-eh-frame -enable-mono-eh-frame -o \"%s\" \"%s.opt.bc\"", acfg->aot_opts.llvm_path, acfg->llc_args->str, acfg->tmpfname, acfg->tmpbasename);
7161 aot_printf (acfg, "Executing llc: %s\n", command);
7163 if (system (command) != 0)
7170 emit_code (MonoAotCompile *acfg)
7172 int oindex, i, prev_index;
7175 #if defined(TARGET_POWERPC64)
7176 sprintf (symbol, ".Lgot_addr");
7177 emit_section_change (acfg, ".text", 0);
7178 emit_alignment (acfg, 8);
7179 emit_label (acfg, symbol);
7180 emit_pointer (acfg, acfg->got_symbol);
7184 * This global symbol is used to compute the address of each method using the
7185 * code_offsets array. It is also used to compute the memory ranges occupied by
7186 * AOT code, so it must be equal to the address of the first emitted method.
7188 emit_section_change (acfg, ".text", 0);
7189 emit_alignment (acfg, 8);
7191 for (i = 0; i < acfg->nmethods; ++i) {
7192 if (acfg->cfgs [i] && acfg->cfgs [i]->compile_llvm) {
7193 acfg->methods_symbol = g_strdup (acfg->cfgs [i]->asm_symbol);
7198 if (!acfg->methods_symbol) {
7199 sprintf (symbol, "methods");
7200 emit_label (acfg, symbol);
7201 acfg->methods_symbol = g_strdup (symbol);
7205 * Emit some padding so the local symbol for the first method doesn't have the
7206 * same address as 'methods'.
7208 #if defined(__default_codegen__)
7209 emit_zero_bytes (acfg, 16);
7210 #elif defined(__native_client_codegen__)
7212 const int kPaddingSize = 16;
7213 guint8 pad_buffer[kPaddingSize];
7214 mono_arch_nacl_pad (pad_buffer, kPaddingSize);
7215 emit_bytes (acfg, pad_buffer, kPaddingSize);
7219 for (oindex = 0; oindex < acfg->method_order->len; ++oindex) {
7223 i = GPOINTER_TO_UINT (g_ptr_array_index (acfg->method_order, oindex));
7225 cfg = acfg->cfgs [i];
7230 method = cfg->orig_method;
7232 /* Emit unbox trampoline */
7233 if (acfg->aot_opts.full_aot && cfg->orig_method->klass->valuetype) {
7234 sprintf (symbol, "ut_%d", get_method_index (acfg, method));
7236 emit_section_change (acfg, ".text", 0);
7237 #ifdef __native_client_codegen__
7238 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
7241 if (acfg->thumb_mixed && cfg->compile_llvm) {
7242 emit_set_thumb_mode (acfg);
7243 fprintf (acfg->fp, "\n.thumb_func\n");
7246 emit_label (acfg, symbol);
7248 arch_emit_unbox_trampoline (acfg, cfg, cfg->orig_method, cfg->asm_symbol);
7250 if (acfg->thumb_mixed && cfg->compile_llvm) {
7251 emit_set_arm_mode (acfg);
7255 if (cfg->compile_llvm)
7256 acfg->stats.llvm_count ++;
7258 emit_method_code (acfg, cfg);
7261 sprintf (symbol, "methods_end");
7262 emit_section_change (acfg, ".text", 0);
7263 emit_alignment (acfg, 8);
7264 emit_label (acfg, symbol);
7265 /* To distinguish it from the next symbol */
7266 emit_int32 (acfg, 0);
7269 * Add .no_dead_strip directives for all LLVM methods to prevent the OSX linker
7270 * from optimizing them away, since it doesn't see that code_offsets references them.
7271 * JITted methods don't need this since they are referenced using assembler local
7273 * FIXME: This is why write-symbols doesn't work on OSX ?
7275 if (acfg->llvm && acfg->need_no_dead_strip) {
7276 fprintf (acfg->fp, "\n");
7277 for (i = 0; i < acfg->nmethods; ++i) {
7278 if (acfg->cfgs [i] && acfg->cfgs [i]->compile_llvm)
7279 fprintf (acfg->fp, ".no_dead_strip %s\n", acfg->cfgs [i]->asm_symbol);
7283 if (acfg->direct_method_addresses) {
7284 acfg->flags |= MONO_AOT_FILE_FLAG_DIRECT_METHOD_ADDRESSES;
7287 * To work around linker issues, we emit a table of branches, and disassemble them at runtime.
7288 * This is PIE code, and the linker can update it if needed.
7290 sprintf (symbol, "method_addresses");
7291 emit_section_change (acfg, ".text", 1);
7292 emit_alignment (acfg, 8);
7293 emit_label (acfg, symbol);
7294 emit_local_symbol (acfg, symbol, "method_addresses_end", TRUE);
7295 emit_unset_mode (acfg);
7296 if (acfg->need_no_dead_strip)
7297 fprintf (acfg->fp, " .no_dead_strip %s\n", symbol);
7299 for (i = 0; i < acfg->nmethods; ++i) {
7300 #ifdef MONO_ARCH_AOT_SUPPORTED
7304 arch_emit_direct_call (acfg, acfg->cfgs [i]->asm_symbol, FALSE, acfg->thumb_mixed && acfg->cfgs [i]->compile_llvm, NULL, &call_size);
7306 arch_emit_direct_call (acfg, "method_addresses", FALSE, FALSE, NULL, &call_size);
7310 sprintf (symbol, "method_addresses_end");
7311 emit_label (acfg, symbol);
7314 sprintf (symbol, "code_offsets");
7315 emit_section_change (acfg, RODATA_SECT, 1);
7316 emit_alignment (acfg, 8);
7317 emit_label (acfg, symbol);
7318 emit_int32 (acfg, 0);
7320 sprintf (symbol, "code_offsets");
7321 emit_section_change (acfg, RODATA_SECT, 1);
7322 emit_alignment (acfg, 8);
7323 emit_label (acfg, symbol);
7325 acfg->stats.offsets_size += acfg->nmethods * 4;
7327 for (i = 0; i < acfg->nmethods; ++i) {
7328 if (acfg->cfgs [i]) {
7329 emit_symbol_diff (acfg, acfg->cfgs [i]->asm_symbol, acfg->methods_symbol, 0);
7331 emit_int32 (acfg, 0xffffffff);
7337 /* Emit a sorted table mapping methods to their unbox trampolines */
7338 sprintf (symbol, "unbox_trampolines");
7339 if (acfg->direct_method_addresses)
7340 emit_section_change (acfg, ".text", 0);
7342 emit_section_change (acfg, RODATA_SECT, 0);
7343 emit_alignment (acfg, 8);
7344 emit_label (acfg, symbol);
7347 for (i = 0; i < acfg->nmethods; ++i) {
7352 cfg = acfg->cfgs [i];
7356 method = cfg->orig_method;
7358 if (acfg->aot_opts.full_aot && cfg->orig_method->klass->valuetype) {
7359 #ifdef MONO_ARCH_AOT_SUPPORTED
7363 index = get_method_index (acfg, method);
7364 sprintf (symbol, "ut_%d", index);
7366 emit_int32 (acfg, index);
7367 if (acfg->direct_method_addresses) {
7368 #ifdef MONO_ARCH_AOT_SUPPORTED
7369 arch_emit_direct_call (acfg, symbol, FALSE, acfg->thumb_mixed && cfg->compile_llvm, NULL, &call_size);
7372 emit_symbol_diff (acfg, symbol, acfg->methods_symbol, 0);
7374 /* Make sure the table is sorted by index */
7375 g_assert (index > prev_index);
7379 sprintf (symbol, "unbox_trampolines_end");
7380 emit_label (acfg, symbol);
7381 emit_int32 (acfg, 0);
7385 emit_info (MonoAotCompile *acfg)
7391 offsets = g_new0 (gint32, acfg->nmethods);
7393 for (oindex = 0; oindex < acfg->method_order->len; ++oindex) {
7394 i = GPOINTER_TO_UINT (g_ptr_array_index (acfg->method_order, oindex));
7396 if (acfg->cfgs [i]) {
7397 emit_method_info (acfg, acfg->cfgs [i]);
7398 offsets [i] = acfg->cfgs [i]->method_info_offset;
7404 sprintf (symbol, "method_info_offsets");
7405 emit_section_change (acfg, RODATA_SECT, 1);
7406 emit_alignment (acfg, 8);
7407 emit_label (acfg, symbol);
7409 acfg->stats.offsets_size += emit_offset_table (acfg, acfg->nmethods, 10, offsets);
7414 #endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
7416 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
7417 #define mix(a,b,c) { \
7418 a -= c; a ^= rot(c, 4); c += b; \
7419 b -= a; b ^= rot(a, 6); a += c; \
7420 c -= b; c ^= rot(b, 8); b += a; \
7421 a -= c; a ^= rot(c,16); c += b; \
7422 b -= a; b ^= rot(a,19); a += c; \
7423 c -= b; c ^= rot(b, 4); b += a; \
7425 #define final(a,b,c) { \
7426 c ^= b; c -= rot(b,14); \
7427 a ^= c; a -= rot(c,11); \
7428 b ^= a; b -= rot(a,25); \
7429 c ^= b; c -= rot(b,16); \
7430 a ^= c; a -= rot(c,4); \
7431 b ^= a; b -= rot(a,14); \
7432 c ^= b; c -= rot(b,24); \
7436 mono_aot_type_hash (MonoType *t1)
7438 guint hash = t1->type;
7440 hash |= t1->byref << 6; /* do not collide with t1->type values */
7442 case MONO_TYPE_VALUETYPE:
7443 case MONO_TYPE_CLASS:
7444 case MONO_TYPE_SZARRAY:
7445 /* check if the distribution is good enough */
7446 return ((hash << 5) - hash) ^ mono_metadata_str_hash (t1->data.klass->name);
7448 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
7449 case MONO_TYPE_ARRAY:
7450 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
7451 case MONO_TYPE_GENERICINST:
7452 return ((hash << 5) - hash) ^ 0;
7459 * mono_aot_method_hash:
7461 * Return a hash code for methods which only depends on metadata.
7464 mono_aot_method_hash (MonoMethod *method)
7466 MonoMethodSignature *sig;
7470 guint32 *hashes_start, *hashes;
7472 MonoGenericInst *ginst = NULL;
7474 /* Similar to the hash in mono_method_get_imt_slot () */
7476 sig = mono_method_signature (method);
7478 if (method->is_inflated)
7479 ginst = ((MonoMethodInflated*)method)->context.method_inst;
7481 hashes_count = sig->param_count + 5 + (ginst ? ginst->type_argc : 0);
7482 hashes_start = g_malloc0 (hashes_count * sizeof (guint32));
7483 hashes = hashes_start;
7485 /* Some wrappers are assigned to random classes */
7486 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
7487 klass = method->klass;
7489 klass = mono_defaults.object_class;
7491 if (!method->wrapper_type) {
7492 char *full_name = mono_type_full_name (&klass->byval_arg);
7494 hashes [0] = mono_metadata_str_hash (full_name);
7498 hashes [0] = mono_metadata_str_hash (klass->name);
7499 hashes [1] = mono_metadata_str_hash (klass->name_space);
7501 if (method->wrapper_type == MONO_WRAPPER_STFLD || method->wrapper_type == MONO_WRAPPER_LDFLD || method->wrapper_type == MONO_WRAPPER_LDFLDA)
7502 /* The method name includes a stringified pointer */
7505 hashes [2] = mono_metadata_str_hash (method->name);
7506 hashes [3] = method->wrapper_type;
7507 hashes [4] = mono_aot_type_hash (sig->ret);
7509 for (i = 0; i < sig->param_count; i++) {
7510 hashes [hindex ++] = mono_aot_type_hash (sig->params [i]);
7513 for (i = 0; i < ginst->type_argc; ++i)
7514 hashes [hindex ++] = mono_aot_type_hash (ginst->type_argv [i]);
7516 g_assert (hindex == hashes_count);
7518 /* Setup internal state */
7519 a = b = c = 0xdeadbeef + (((guint32)hashes_count)<<2);
7521 /* Handle most of the hashes */
7522 while (hashes_count > 3) {
7531 /* Handle the last 3 hashes (all the case statements fall through) */
7532 switch (hashes_count) {
7533 case 3 : c += hashes [2];
7534 case 2 : b += hashes [1];
7535 case 1 : a += hashes [0];
7537 case 0: /* nothing left to add */
7541 free (hashes_start);
7550 * mono_aot_get_array_helper_from_wrapper;
7552 * Get the helper method in Array called by an array wrapper method.
7555 mono_aot_get_array_helper_from_wrapper (MonoMethod *method)
7559 MonoGenericContext ctx;
7560 MonoType *args [16];
7561 char *mname, *iname, *s, *s2, *helper_name = NULL;
7563 prefix = "System.Collections.Generic";
7564 s = g_strdup_printf ("%s", method->name + strlen (prefix) + 1);
7565 s2 = strstr (s, "`1.");
7571 //printf ("X: %s %s\n", iname, mname);
7573 if (!strcmp (iname, "IList"))
7574 helper_name = g_strdup_printf ("InternalArray__%s", mname);
7576 helper_name = g_strdup_printf ("InternalArray__%s_%s", iname, mname);
7577 m = mono_class_get_method_from_name (mono_defaults.array_class, helper_name, mono_method_signature (method)->param_count);
7579 g_free (helper_name);
7582 if (m->is_generic) {
7583 memset (&ctx, 0, sizeof (ctx));
7584 args [0] = &method->klass->element_class->byval_arg;
7585 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
7586 m = mono_class_inflate_generic_method (m, &ctx);
7592 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
7594 typedef struct HashEntry {
7595 guint32 key, value, index;
7596 struct HashEntry *next;
7600 * emit_extra_methods:
7602 * Emit methods which are not in the METHOD table, like wrappers.
7605 emit_extra_methods (MonoAotCompile *acfg)
7607 int i, table_size, buf_size;
7610 guint32 *info_offsets;
7613 HashEntry *entry, *new_entry;
7614 int nmethods, max_chain_length;
7617 info_offsets = g_new0 (guint32, acfg->extra_methods->len);
7619 /* Emit method info */
7621 for (i = 0; i < acfg->extra_methods->len; ++i) {
7622 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
7623 MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
7629 p = buf = g_malloc (buf_size);
7633 method = cfg->method_to_register;
7635 encode_method_ref (acfg, method, p, &p);
7637 g_assert ((p - buf) < buf_size);
7639 info_offsets [i] = add_to_blob (acfg, buf, p - buf);
7644 * Construct a chained hash table for mapping indexes in extra_method_info to
7647 table_size = g_spaced_primes_closest ((int)(nmethods * 1.5));
7648 table = g_ptr_array_sized_new (table_size);
7649 for (i = 0; i < table_size; ++i)
7650 g_ptr_array_add (table, NULL);
7651 chain_lengths = g_new0 (int, table_size);
7652 max_chain_length = 0;
7653 for (i = 0; i < acfg->extra_methods->len; ++i) {
7654 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
7655 MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
7661 key = info_offsets [i];
7662 value = get_method_index (acfg, method);
7664 hash = mono_aot_method_hash (method) % table_size;
7665 //printf ("X: %s %d\n", mono_method_full_name (method, 1), hash);
7667 chain_lengths [hash] ++;
7668 max_chain_length = MAX (max_chain_length, chain_lengths [hash]);
7670 new_entry = mono_mempool_alloc0 (acfg->mempool, sizeof (HashEntry));
7671 new_entry->key = key;
7672 new_entry->value = value;
7674 entry = g_ptr_array_index (table, hash);
7675 if (entry == NULL) {
7676 new_entry->index = hash;
7677 g_ptr_array_index (table, hash) = new_entry;
7680 entry = entry->next;
7682 entry->next = new_entry;
7683 new_entry->index = table->len;
7684 g_ptr_array_add (table, new_entry);
7688 //printf ("MAX: %d\n", max_chain_length);
7690 /* Emit the table */
7691 sprintf (symbol, "extra_method_table");
7692 emit_section_change (acfg, RODATA_SECT, 0);
7693 emit_alignment (acfg, 8);
7694 emit_label (acfg, symbol);
7696 emit_int32 (acfg, table_size);
7697 for (i = 0; i < table->len; ++i) {
7698 HashEntry *entry = g_ptr_array_index (table, i);
7700 if (entry == NULL) {
7701 emit_int32 (acfg, 0);
7702 emit_int32 (acfg, 0);
7703 emit_int32 (acfg, 0);
7705 //g_assert (entry->key > 0);
7706 emit_int32 (acfg, entry->key);
7707 emit_int32 (acfg, entry->value);
7709 emit_int32 (acfg, entry->next->index);
7711 emit_int32 (acfg, 0);
7716 * Emit a table reverse mapping method indexes to their index in extra_method_info.
7717 * This is used by mono_aot_find_jit_info ().
7719 sprintf (symbol, "extra_method_info_offsets");
7720 emit_section_change (acfg, RODATA_SECT, 0);
7721 emit_alignment (acfg, 8);
7722 emit_label (acfg, symbol);
7724 emit_int32 (acfg, acfg->extra_methods->len);
7725 for (i = 0; i < acfg->extra_methods->len; ++i) {
7726 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
7728 emit_int32 (acfg, get_method_index (acfg, method));
7729 emit_int32 (acfg, info_offsets [i]);
7734 emit_exception_info (MonoAotCompile *acfg)
7740 offsets = g_new0 (gint32, acfg->nmethods);
7741 for (i = 0; i < acfg->nmethods; ++i) {
7742 if (acfg->cfgs [i]) {
7743 emit_exception_debug_info (acfg, acfg->cfgs [i]);
7744 offsets [i] = acfg->cfgs [i]->ex_info_offset;
7750 sprintf (symbol, "ex_info_offsets");
7751 emit_section_change (acfg, RODATA_SECT, 1);
7752 emit_alignment (acfg, 8);
7753 emit_label (acfg, symbol);
7755 acfg->stats.offsets_size += emit_offset_table (acfg, acfg->nmethods, 10, offsets);
7760 emit_unwind_info (MonoAotCompile *acfg)
7766 * The unwind info contains a lot of duplicates so we emit each unique
7767 * entry once, and only store the offset from the start of the table in the
7771 sprintf (symbol, "unwind_info");
7772 emit_section_change (acfg, RODATA_SECT, 1);
7773 emit_alignment (acfg, 8);
7774 emit_label (acfg, symbol);
7776 for (i = 0; i < acfg->unwind_ops->len; ++i) {
7777 guint32 index = GPOINTER_TO_UINT (g_ptr_array_index (acfg->unwind_ops, i));
7778 guint8 *unwind_info;
7779 guint32 unwind_info_len;
7783 unwind_info = mono_get_cached_unwind_info (index, &unwind_info_len);
7786 encode_value (unwind_info_len, p, &p);
7787 emit_bytes (acfg, buf, p - buf);
7788 emit_bytes (acfg, unwind_info, unwind_info_len);
7790 acfg->stats.unwind_info_size += (p - buf) + unwind_info_len;
7795 emit_class_info (MonoAotCompile *acfg)
7801 offsets = g_new0 (gint32, acfg->image->tables [MONO_TABLE_TYPEDEF].rows);
7802 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
7803 offsets [i] = emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
7805 sprintf (symbol, "class_info_offsets");
7806 emit_section_change (acfg, RODATA_SECT, 1);
7807 emit_alignment (acfg, 8);
7808 emit_label (acfg, symbol);
7810 acfg->stats.offsets_size += emit_offset_table (acfg, acfg->image->tables [MONO_TABLE_TYPEDEF].rows, 10, offsets);
7814 typedef struct ClassNameTableEntry {
7815 guint32 token, index;
7816 struct ClassNameTableEntry *next;
7817 } ClassNameTableEntry;
7820 emit_class_name_table (MonoAotCompile *acfg)
7823 guint32 token, hash;
7828 ClassNameTableEntry *entry, *new_entry;
7831 * Construct a chained hash table for mapping class names to typedef tokens.
7833 table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
7834 table = g_ptr_array_sized_new (table_size);
7835 for (i = 0; i < table_size; ++i)
7836 g_ptr_array_add (table, NULL);
7837 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
7838 token = MONO_TOKEN_TYPE_DEF | (i + 1);
7839 klass = mono_class_get (acfg->image, token);
7841 mono_loader_clear_error ();
7844 full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
7845 hash = mono_metadata_str_hash (full_name) % table_size;
7848 /* FIXME: Allocate from the mempool */
7849 new_entry = g_new0 (ClassNameTableEntry, 1);
7850 new_entry->token = token;
7852 entry = g_ptr_array_index (table, hash);
7853 if (entry == NULL) {
7854 new_entry->index = hash;
7855 g_ptr_array_index (table, hash) = new_entry;
7858 entry = entry->next;
7860 entry->next = new_entry;
7861 new_entry->index = table->len;
7862 g_ptr_array_add (table, new_entry);
7866 /* Emit the table */
7867 sprintf (symbol, "class_name_table");
7868 emit_section_change (acfg, RODATA_SECT, 0);
7869 emit_alignment (acfg, 8);
7870 emit_label (acfg, symbol);
7872 /* FIXME: Optimize memory usage */
7873 g_assert (table_size < 65000);
7874 emit_int16 (acfg, table_size);
7875 g_assert (table->len < 65000);
7876 for (i = 0; i < table->len; ++i) {
7877 ClassNameTableEntry *entry = g_ptr_array_index (table, i);
7879 if (entry == NULL) {
7880 emit_int16 (acfg, 0);
7881 emit_int16 (acfg, 0);
7883 emit_int16 (acfg, mono_metadata_token_index (entry->token));
7885 emit_int16 (acfg, entry->next->index);
7887 emit_int16 (acfg, 0);
7893 emit_image_table (MonoAotCompile *acfg)
7899 * The image table is small but referenced in a lot of places.
7900 * So we emit it at once, and reference its elements by an index.
7903 sprintf (symbol, "image_table");
7904 emit_section_change (acfg, RODATA_SECT, 1);
7905 emit_alignment (acfg, 8);
7906 emit_label (acfg, symbol);
7908 emit_int32 (acfg, acfg->image_table->len);
7909 for (i = 0; i < acfg->image_table->len; i++) {
7910 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
7911 MonoAssemblyName *aname = &image->assembly->aname;
7913 /* FIXME: Support multi-module assemblies */
7914 g_assert (image->assembly->image == image);
7916 emit_string (acfg, image->assembly_name);
7917 emit_string (acfg, image->guid);
7918 emit_string (acfg, aname->culture ? aname->culture : "");
7919 emit_string (acfg, (const char*)aname->public_key_token);
7921 emit_alignment (acfg, 8);
7922 emit_int32 (acfg, aname->flags);
7923 emit_int32 (acfg, aname->major);
7924 emit_int32 (acfg, aname->minor);
7925 emit_int32 (acfg, aname->build);
7926 emit_int32 (acfg, aname->revision);
7931 emit_got_info (MonoAotCompile *acfg)
7934 int i, first_plt_got_patch, buf_size;
7936 guint32 *got_info_offsets;
7938 /* Add the patches needed by the PLT to the GOT */
7939 acfg->plt_got_offset_base = acfg->got_offset;
7940 first_plt_got_patch = acfg->got_patches->len;
7941 for (i = 1; i < acfg->plt_offset; ++i) {
7942 MonoPltEntry *plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
7944 g_ptr_array_add (acfg->got_patches, plt_entry->ji);
7946 acfg->stats.got_slot_types [plt_entry->ji->type] ++;
7949 acfg->got_offset += acfg->plt_offset;
7953 * - optimize offsets table.
7954 * - reduce number of exported symbols.
7955 * - emit info for a klass only once.
7956 * - determine when a method uses a GOT slot which is guaranteed to be already
7958 * - clean up and document the code.
7959 * - use String.Empty in class libs.
7962 /* Encode info required to decode shared GOT entries */
7963 buf_size = acfg->got_patches->len * 128;
7964 p = buf = mono_mempool_alloc (acfg->mempool, buf_size);
7965 got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->got_patches->len * sizeof (guint32));
7966 acfg->plt_got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->plt_offset * sizeof (guint32));
7968 if (acfg->plt_offset)
7969 acfg->plt_got_info_offsets [0] = 0;
7970 for (i = 0; i < acfg->got_patches->len; ++i) {
7971 MonoJumpInfo *ji = g_ptr_array_index (acfg->got_patches, i);
7976 encode_value (ji->type, p, &p);
7978 encode_patch (acfg, ji, p, &p);
7979 acfg->stats.got_slot_info_sizes [ji->type] += p - p2;
7980 g_assert (p - buf <= buf_size);
7981 got_info_offsets [i] = add_to_blob (acfg, buf, p - buf);
7983 if (i >= first_plt_got_patch)
7984 acfg->plt_got_info_offsets [i - first_plt_got_patch + 1] = got_info_offsets [i];
7985 acfg->stats.got_info_size += p - buf;
7988 /* Emit got_info_offsets table */
7989 sprintf (symbol, "got_info_offsets");
7990 emit_section_change (acfg, RODATA_SECT, 1);
7991 emit_alignment (acfg, 8);
7992 emit_label (acfg, symbol);
7994 /* No need to emit offsets for the got plt entries, the plt embeds them directly */
7995 acfg->stats.offsets_size += emit_offset_table (acfg, first_plt_got_patch, 10, (gint32*)got_info_offsets);
7999 emit_got (MonoAotCompile *acfg)
8004 /* Don't make GOT global so accesses to it don't need relocations */
8005 sprintf (symbol, "%s", acfg->got_symbol);
8006 emit_section_change (acfg, ".bss", 0);
8007 emit_alignment (acfg, 8);
8008 emit_local_symbol (acfg, symbol, "got_end", FALSE);
8009 emit_label (acfg, symbol);
8010 if (acfg->got_offset > 0)
8011 emit_zero_bytes (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
8013 sprintf (symbol, "got_end");
8014 emit_label (acfg, symbol);
8018 typedef struct GlobalsTableEntry {
8019 guint32 value, index;
8020 struct GlobalsTableEntry *next;
8021 } GlobalsTableEntry;
8024 emit_globals (MonoAotCompile *acfg)
8030 GlobalsTableEntry *entry, *new_entry;
8032 if (!acfg->aot_opts.static_link)
8036 * When static linking, we emit a table containing our globals.
8040 * Construct a chained hash table for mapping global names to their index in
8041 * the globals table.
8043 table_size = g_spaced_primes_closest ((int)(acfg->globals->len * 1.5));
8044 table = g_ptr_array_sized_new (table_size);
8045 for (i = 0; i < table_size; ++i)
8046 g_ptr_array_add (table, NULL);
8047 for (i = 0; i < acfg->globals->len; ++i) {
8048 char *name = g_ptr_array_index (acfg->globals, i);
8050 hash = mono_metadata_str_hash (name) % table_size;
8052 /* FIXME: Allocate from the mempool */
8053 new_entry = g_new0 (GlobalsTableEntry, 1);
8054 new_entry->value = i;
8056 entry = g_ptr_array_index (table, hash);
8057 if (entry == NULL) {
8058 new_entry->index = hash;
8059 g_ptr_array_index (table, hash) = new_entry;
8062 entry = entry->next;
8064 entry->next = new_entry;
8065 new_entry->index = table->len;
8066 g_ptr_array_add (table, new_entry);
8070 /* Emit the table */
8071 sprintf (symbol, ".Lglobals_hash");
8072 emit_section_change (acfg, RODATA_SECT, 0);
8073 emit_alignment (acfg, 8);
8074 emit_label (acfg, symbol);
8076 /* FIXME: Optimize memory usage */
8077 g_assert (table_size < 65000);
8078 emit_int16 (acfg, table_size);
8079 for (i = 0; i < table->len; ++i) {
8080 GlobalsTableEntry *entry = g_ptr_array_index (table, i);
8082 if (entry == NULL) {
8083 emit_int16 (acfg, 0);
8084 emit_int16 (acfg, 0);
8086 emit_int16 (acfg, entry->value + 1);
8088 emit_int16 (acfg, entry->next->index);
8090 emit_int16 (acfg, 0);
8094 /* Emit the names */
8095 for (i = 0; i < acfg->globals->len; ++i) {
8096 char *name = g_ptr_array_index (acfg->globals, i);
8098 sprintf (symbol, "name_%d", i);
8099 emit_section_change (acfg, RODATA_SECT, 1);
8101 emit_alignment (acfg, 4);
8103 emit_label (acfg, symbol);
8104 emit_string (acfg, name);
8107 /* Emit the globals table */
8108 sprintf (symbol, "globals");
8109 emit_section_change (acfg, ".data", 0);
8110 /* This is not a global, since it is accessed by the init function */
8111 emit_alignment (acfg, 8);
8112 emit_label (acfg, symbol);
8114 sprintf (symbol, "%sglobals_hash", acfg->temp_prefix);
8115 emit_pointer (acfg, symbol);
8117 for (i = 0; i < acfg->globals->len; ++i) {
8118 char *name = g_ptr_array_index (acfg->globals, i);
8120 sprintf (symbol, "name_%d", i);
8121 emit_pointer (acfg, symbol);
8123 sprintf (symbol, "%s", name);
8124 emit_pointer (acfg, symbol);
8126 /* Null terminate the table */
8127 emit_int32 (acfg, 0);
8128 emit_int32 (acfg, 0);
8132 emit_autoreg (MonoAotCompile *acfg)
8137 * Emit a function into the .ctor section which will be called by the ELF
8138 * loader to register this module with the runtime.
8140 if (! (!acfg->use_bin_writer && acfg->aot_opts.static_link && acfg->aot_opts.autoreg))
8143 symbol = g_strdup_printf ("_%s_autoreg", acfg->static_linking_symbol);
8145 arch_emit_autoreg (acfg, symbol);
8151 emit_mem_end (MonoAotCompile *acfg)
8155 sprintf (symbol, "mem_end");
8156 emit_section_change (acfg, ".text", 1);
8157 emit_alignment (acfg, 8);
8158 emit_label (acfg, symbol);
8162 * Emit a structure containing all the information not stored elsewhere.
8165 emit_file_info (MonoAotCompile *acfg)
8170 const char *gc_name;
8173 emit_string_symbol (acfg, "assembly_guid" , acfg->image->guid);
8175 if (acfg->aot_opts.bind_to_runtime_version) {
8176 build_info = mono_get_runtime_build_info ();
8177 emit_string_symbol (acfg, "runtime_version", build_info);
8178 g_free (build_info);
8180 emit_string_symbol (acfg, "runtime_version", "");
8183 /* Emit a string holding the assembly name */
8184 emit_string_symbol (acfg, "assembly_name", acfg->image->assembly->aname.name);
8187 * The managed allocators are GC specific, so can't use an AOT image created by one GC
8190 gc_name = mono_gc_get_gc_name ();
8191 gc_name_offset = add_to_blob (acfg, (guint8*)gc_name, strlen (gc_name) + 1);
8193 sprintf (symbol, "%smono_aot_file_info", acfg->user_symbol_prefix);
8194 emit_section_change (acfg, ".data", 0);
8195 emit_alignment (acfg, 8);
8196 emit_label (acfg, symbol);
8197 if (!acfg->aot_opts.static_link)
8198 emit_global (acfg, symbol, FALSE);
8200 /* The data emitted here must match MonoAotFileInfo. */
8202 emit_int32 (acfg, MONO_AOT_FILE_VERSION);
8203 emit_int32 (acfg, 0);
8206 * We emit pointers to our data structures instead of emitting global symbols which
8207 * point to them, to reduce the number of globals, and because using globals leads to
8208 * various problems (i.e. arm/thumb).
8210 emit_pointer (acfg, acfg->got_symbol);
8211 emit_pointer (acfg, acfg->methods_symbol);
8214 * Emit a reference to the mono_eh_frame table created by our modified LLVM compiler.
8216 emit_pointer (acfg, "mono_eh_frame");
8218 emit_pointer (acfg, NULL);
8220 emit_pointer (acfg, "blob");
8221 emit_pointer (acfg, "class_name_table");
8222 emit_pointer (acfg, "class_info_offsets");
8223 emit_pointer (acfg, "method_info_offsets");
8224 emit_pointer (acfg, "ex_info_offsets");
8225 emit_pointer (acfg, "code_offsets");
8226 if (acfg->direct_method_addresses)
8227 emit_pointer (acfg, "method_addresses");
8229 emit_pointer (acfg, NULL);
8230 emit_pointer (acfg, "extra_method_info_offsets");
8231 emit_pointer (acfg, "extra_method_table");
8232 emit_pointer (acfg, "got_info_offsets");
8233 emit_pointer (acfg, "methods_end");
8234 emit_pointer (acfg, "unwind_info");
8235 emit_pointer (acfg, "mem_end");
8236 emit_pointer (acfg, "image_table");
8237 emit_pointer (acfg, "plt");
8238 emit_pointer (acfg, "plt_end");
8239 emit_pointer (acfg, "assembly_guid");
8240 emit_pointer (acfg, "runtime_version");
8241 if (acfg->num_trampoline_got_entries) {
8242 emit_pointer (acfg, "specific_trampolines");
8243 emit_pointer (acfg, "static_rgctx_trampolines");
8244 emit_pointer (acfg, "imt_thunks");
8245 emit_pointer (acfg, "gsharedvt_arg_trampolines");
8247 emit_pointer (acfg, NULL);
8248 emit_pointer (acfg, NULL);
8249 emit_pointer (acfg, NULL);
8250 emit_pointer (acfg, NULL);
8252 if (acfg->thumb_mixed) {
8253 emit_pointer (acfg, "thumb_end");
8255 emit_pointer (acfg, NULL);
8257 if (acfg->aot_opts.static_link) {
8258 emit_pointer (acfg, "globals");
8260 emit_pointer (acfg, NULL);
8262 emit_pointer (acfg, "assembly_name");
8263 emit_pointer (acfg, "unbox_trampolines");
8264 emit_pointer (acfg, "unbox_trampolines_end");
8266 emit_int32 (acfg, acfg->plt_got_offset_base);
8267 emit_int32 (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
8268 emit_int32 (acfg, acfg->plt_offset);
8269 emit_int32 (acfg, acfg->nmethods);
8270 emit_int32 (acfg, acfg->flags);
8271 emit_int32 (acfg, acfg->opts);
8272 emit_int32 (acfg, acfg->simd_opts);
8273 emit_int32 (acfg, gc_name_offset);
8275 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
8276 emit_int32 (acfg, acfg->num_trampolines [i]);
8277 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
8278 emit_int32 (acfg, acfg->trampoline_got_offset_base [i]);
8279 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
8280 emit_int32 (acfg, acfg->trampoline_size [i]);
8281 emit_int32 (acfg, acfg->aot_opts.nrgctx_fetch_trampolines);
8283 #if defined (TARGET_ARM) && defined (TARGET_MACH)
8288 memset (&t, 0, sizeof (MonoType));
8289 t.type = MONO_TYPE_R8;
8290 mono_type_size (&t, &align);
8291 emit_int32 (acfg, align);
8293 memset (&t, 0, sizeof (MonoType));
8294 t.type = MONO_TYPE_I8;
8295 mono_type_size (&t, &align);
8297 emit_int32 (acfg, align);
8300 emit_int32 (acfg, MONO_ABI_ALIGNOF (double));
8301 emit_int32 (acfg, MONO_ABI_ALIGNOF (gint64));
8303 emit_int32 (acfg, MONO_TRAMPOLINE_NUM);
8304 emit_int32 (acfg, acfg->tramp_page_size);
8305 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
8306 emit_int32 (acfg, acfg->tramp_page_code_offsets [i]);
8308 if (acfg->aot_opts.static_link) {
8312 * Emit a global symbol which can be passed by an embedding app to
8313 * mono_aot_register_module (). The symbol points to a pointer to the the file info
8316 sprintf (symbol, "%smono_aot_module_%s_info", acfg->user_symbol_prefix, acfg->image->assembly->aname.name);
8318 /* Get rid of characters which cannot occur in symbols */
8320 for (p = symbol; *p; ++p) {
8321 if (!(isalnum (*p) || *p == '_'))
8324 acfg->static_linking_symbol = g_strdup (symbol);
8325 emit_global_inner (acfg, symbol, FALSE);
8326 emit_alignment (acfg, sizeof (gpointer));
8327 emit_label (acfg, symbol);
8328 emit_pointer_2 (acfg, acfg->user_symbol_prefix, "mono_aot_file_info");
8333 emit_blob (MonoAotCompile *acfg)
8337 sprintf (symbol, "blob");
8338 emit_section_change (acfg, RODATA_SECT, 1);
8339 emit_alignment (acfg, 8);
8340 emit_label (acfg, symbol);
8342 emit_bytes (acfg, (guint8*)acfg->blob.data, acfg->blob.index);
8346 emit_objc_selectors (MonoAotCompile *acfg)
8350 if (!acfg->objc_selectors || acfg->objc_selectors->len == 0)
8355 * cat > foo.m << EOF
8358 * return @selector(print:);
8363 img_writer_emit_unset_mode (acfg->w);
8364 g_assert (acfg->fp);
8365 fprintf (acfg->fp, ".section __DATA,__objc_selrefs,literal_pointers,no_dead_strip\n");
8366 fprintf (acfg->fp, ".align 3\n");
8367 for (i = 0; i < acfg->objc_selectors->len; ++i) {
8368 fprintf (acfg->fp, "L_OBJC_SELECTOR_REFERENCES_%d:\n", i);
8369 fprintf (acfg->fp, ".long L_OBJC_METH_VAR_NAME_%d\n", i);
8371 fprintf (acfg->fp, ".section __TEXT,__cstring,cstring_literals\n");
8372 for (i = 0; i < acfg->objc_selectors->len; ++i) {
8373 fprintf (acfg->fp, "L_OBJC_METH_VAR_NAME_%d:\n", i);
8374 fprintf (acfg->fp, ".asciz \"%s\"\n", (char*)g_ptr_array_index (acfg->objc_selectors, i));
8377 fprintf (acfg->fp, ".section __DATA,__objc_imageinfo,regular,no_dead_strip\n");
8378 fprintf (acfg->fp, ".align 3\n");
8379 fprintf (acfg->fp, "L_OBJC_IMAGE_INFO:\n");
8380 fprintf (acfg->fp, ".long 0\n");
8381 fprintf (acfg->fp, ".long 16\n");
8385 emit_dwarf_info (MonoAotCompile *acfg)
8387 #ifdef EMIT_DWARF_INFO
8391 /* DIEs for methods */
8392 for (i = 0; i < acfg->nmethods; ++i) {
8393 MonoCompile *cfg = acfg->cfgs [i];
8398 // FIXME: LLVM doesn't define .Lme_...
8399 if (cfg->compile_llvm)
8402 sprintf (symbol2, "%sme_%x", acfg->temp_prefix, i);
8404 mono_dwarf_writer_emit_method (acfg->dwarf, cfg, cfg->method, cfg->asm_symbol, symbol2, 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 ()));
8410 collect_methods (MonoAotCompile *acfg)
8413 MonoImage *image = acfg->image;
8415 /* Collect methods */
8416 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
8418 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
8420 method = mono_get_method (acfg->image, token, NULL);
8423 aot_printerrf (acfg, "Failed to load method 0x%x from '%s'.\n", token, image->name);
8424 aot_printerrf (acfg, "Run with MONO_LOG_LEVEL=debug for more information.\n");
8428 /* Load all methods eagerly to skip the slower lazy loading code */
8429 mono_class_setup_methods (method->klass);
8431 if (acfg->aot_opts.full_aot && method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
8432 /* Compile the wrapper instead */
8433 /* We do this here instead of add_wrappers () because it is easy to do it here */
8434 MonoMethod *wrapper = mono_marshal_get_native_wrapper (method, check_for_pending_exc, TRUE);
8438 /* FIXME: Some mscorlib methods don't have debug info */
8440 if (acfg->aot_opts.soft_debug && !method->wrapper_type) {
8441 if (!((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
8442 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
8443 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
8444 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))) {
8445 if (!mono_debug_lookup_method (method)) {
8446 fprintf (stderr, "Method %s has no debug info, probably the .mdb file for the assembly is missing.\n", mono_method_full_name (method, TRUE));
8453 /* Since we add the normal methods first, their index will be equal to their zero based token index */
8454 add_method_with_index (acfg, method, i, FALSE);
8455 acfg->method_index ++;
8458 /* gsharedvt methods */
8459 for (mindex = 0; mindex < image->tables [MONO_TABLE_METHOD].rows; ++mindex) {
8461 guint32 token = MONO_TOKEN_METHOD_DEF | (mindex + 1);
8463 if (!(acfg->opts & MONO_OPT_GSHAREDVT))
8466 method = mono_get_method (acfg->image, token, NULL);
8470 if (strcmp (method->name, "gshared2"))
8474 if (!strstr (method->klass->image->name, "mini"))
8477 if (method->is_generic || method->klass->generic_container) {
8478 MonoMethod *gshared;
8480 gshared = mini_get_shared_method_full (method, TRUE, TRUE);
8481 add_extra_method (acfg, gshared);
8485 add_generic_instances (acfg);
8487 if (acfg->aot_opts.full_aot)
8488 add_wrappers (acfg);
8493 compile_methods (MonoAotCompile *acfg)
8497 if (acfg->aot_opts.nthreads > 0) {
8502 gpointer *user_data;
8503 MonoMethod **methods;
8505 methods_len = acfg->methods->len;
8507 len = acfg->methods->len / acfg->aot_opts.nthreads;
8510 * Partition the list of methods into fragments, and hand it to threads to
8513 threads = g_ptr_array_new ();
8514 /* Make a copy since acfg->methods is modified by compile_method () */
8515 methods = g_new0 (MonoMethod*, methods_len);
8516 //memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
8517 for (i = 0; i < methods_len; ++i)
8518 methods [i] = g_ptr_array_index (acfg->methods, i);
8520 while (i < methods_len) {
8521 frag = g_ptr_array_new ();
8522 for (j = 0; j < len; ++j) {
8523 if (i < methods_len) {
8524 g_ptr_array_add (frag, methods [i]);
8529 user_data = g_new0 (gpointer, 3);
8530 user_data [0] = mono_domain_get ();
8531 user_data [1] = acfg;
8532 user_data [2] = frag;
8534 handle = mono_threads_create_thread ((gpointer)compile_thread_main, user_data, 0, 0, NULL);
8535 g_ptr_array_add (threads, handle);
8539 for (i = 0; i < threads->len; ++i) {
8540 WaitForSingleObjectEx (g_ptr_array_index (threads, i), INFINITE, FALSE);
8546 /* Compile methods added by compile_method () or all methods if nthreads == 0 */
8547 for (i = methods_len; i < acfg->methods->len; ++i) {
8548 /* This can new methods to acfg->methods */
8549 compile_method (acfg, g_ptr_array_index (acfg->methods, i));
8554 compile_asm (MonoAotCompile *acfg)
8556 char *command, *objfile;
8557 char *outfile_name, *tmp_outfile_name;
8558 const char *tool_prefix = acfg->aot_opts.tool_prefix ? acfg->aot_opts.tool_prefix : "";
8560 #if defined(TARGET_AMD64) && !defined(TARGET_MACH)
8561 #define AS_OPTIONS "--64"
8562 #elif defined(TARGET_POWERPC64)
8563 #define AS_OPTIONS "-a64 -mppc64"
8564 #define LD_OPTIONS "-m elf64ppc"
8565 #elif defined(sparc) && SIZEOF_VOID_P == 8
8566 #define AS_OPTIONS "-xarch=v9"
8567 #elif defined(TARGET_X86) && defined(TARGET_MACH) && !defined(__native_client_codegen__)
8568 #define AS_OPTIONS "-arch i386"
8570 #define AS_OPTIONS ""
8573 #ifdef __native_client_codegen__
8574 #if defined(TARGET_AMD64)
8575 #define AS_NAME "nacl64-as"
8577 #define AS_NAME "nacl-as"
8579 #elif defined(TARGET_OSX)
8580 #define AS_NAME "clang -c -x assembler"
8582 #define AS_NAME "as"
8586 #define LD_OPTIONS ""
8590 #define LD_NAME "ld -shared -G"
8591 #elif defined(__ppc__) && defined(TARGET_MACH)
8592 #define LD_NAME "gcc -dynamiclib"
8593 #elif defined(TARGET_AMD64) && defined(TARGET_MACH)
8594 #define LD_NAME "clang --shared"
8595 #elif defined(HOST_WIN32)
8596 #define LD_NAME "gcc -shared --dll"
8597 #elif defined(TARGET_X86) && defined(TARGET_MACH) && !defined(__native_client_codegen__)
8598 #define LD_NAME "clang -m32 -dynamiclib"
8601 if (acfg->aot_opts.asm_only) {
8602 aot_printf (acfg, "Output file: '%s'.\n", acfg->tmpfname);
8603 if (acfg->aot_opts.static_link)
8604 aot_printf (acfg, "Linking symbol: '%s'.\n", acfg->static_linking_symbol);
8608 if (acfg->aot_opts.static_link) {
8609 if (acfg->aot_opts.outfile)
8610 objfile = g_strdup_printf ("%s", acfg->aot_opts.outfile);
8612 objfile = g_strdup_printf ("%s.o", acfg->image->name);
8614 objfile = g_strdup_printf ("%s.o", acfg->tmpfname);
8616 command = g_strdup_printf ("%s%s %s %s -o %s %s", tool_prefix, AS_NAME, AS_OPTIONS, acfg->as_args ? acfg->as_args->str : "", objfile, acfg->tmpfname);
8617 aot_printf (acfg, "Executing the native assembler: %s\n", command);
8618 if (system (command) != 0) {
8626 if (acfg->aot_opts.static_link) {
8627 aot_printf (acfg, "Output file: '%s'.\n", objfile);
8628 aot_printf (acfg, "Linking symbol: '%s'.\n", acfg->static_linking_symbol);
8633 if (acfg->aot_opts.outfile)
8634 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
8636 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
8638 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
8641 command = g_strdup_printf ("%s -o %s %s.o", LD_NAME, tmp_outfile_name, acfg->tmpfname);
8643 command = g_strdup_printf ("%sld %s -shared -o %s %s.o", tool_prefix, LD_OPTIONS, tmp_outfile_name, acfg->tmpfname);
8645 aot_printf (acfg, "Executing the native linker: %s\n", command);
8646 if (system (command) != 0) {
8647 g_free (tmp_outfile_name);
8648 g_free (outfile_name);
8656 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, SHARED_EXT);
8657 printf ("Stripping the binary: %s\n", com);
8661 #if defined(TARGET_ARM) && !defined(TARGET_MACH)
8663 * gas generates 'mapping symbols' each time code and data is mixed, which
8664 * happens a lot in emit_and_reloc_code (), so we need to get rid of them.
8666 command = g_strdup_printf ("%sstrip --strip-symbol=\\$a --strip-symbol=\\$d %s", tool_prefix, tmp_outfile_name);
8667 aot_printf (acfg, "Stripping the binary: %s\n", command);
8668 if (system (command) != 0) {
8669 g_free (tmp_outfile_name);
8670 g_free (outfile_name);
8677 rename (tmp_outfile_name, outfile_name);
8679 #if defined(TARGET_MACH)
8680 command = g_strdup_printf ("dsymutil %s", outfile_name);
8681 aot_printf (acfg, "Executing dsymutil: %s\n", command);
8682 if (system (command) != 0) {
8687 if (!acfg->aot_opts.save_temps)
8690 g_free (tmp_outfile_name);
8691 g_free (outfile_name);
8694 if (acfg->aot_opts.save_temps)
8695 aot_printf (acfg, "Retained input file.\n");
8697 unlink (acfg->tmpfname);
8702 static MonoAotCompile*
8703 acfg_create (MonoAssembly *ass, guint32 opts)
8705 MonoImage *image = ass->image;
8706 MonoAotCompile *acfg;
8709 acfg = g_new0 (MonoAotCompile, 1);
8710 acfg->methods = g_ptr_array_new ();
8711 acfg->method_indexes = g_hash_table_new (NULL, NULL);
8712 acfg->method_depth = g_hash_table_new (NULL, NULL);
8713 acfg->plt_offset_to_entry = g_hash_table_new (NULL, NULL);
8714 acfg->patch_to_plt_entry = g_new0 (GHashTable*, MONO_PATCH_INFO_NUM);
8715 acfg->patch_to_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
8716 acfg->patch_to_got_offset_by_type = g_new0 (GHashTable*, MONO_PATCH_INFO_NUM);
8717 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
8718 acfg->patch_to_got_offset_by_type [i] = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
8719 acfg->got_patches = g_ptr_array_new ();
8720 acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
8721 acfg->token_info_hash = g_hash_table_new_full (NULL, NULL, NULL, g_free);
8722 acfg->method_to_pinvoke_import = g_hash_table_new_full (NULL, NULL, NULL, g_free);
8723 acfg->image_hash = g_hash_table_new (NULL, NULL);
8724 acfg->image_table = g_ptr_array_new ();
8725 acfg->globals = g_ptr_array_new ();
8726 acfg->image = image;
8728 /* TODO: Write out set of SIMD instructions used, rather than just those available */
8729 acfg->simd_opts = mono_arch_cpu_enumerate_simd_versions ();
8730 acfg->mempool = mono_mempool_new ();
8731 acfg->extra_methods = g_ptr_array_new ();
8732 acfg->unwind_info_offsets = g_hash_table_new (NULL, NULL);
8733 acfg->unwind_ops = g_ptr_array_new ();
8734 acfg->method_label_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
8735 acfg->method_order = g_ptr_array_new ();
8736 acfg->export_names = g_hash_table_new (NULL, NULL);
8737 acfg->klass_blob_hash = g_hash_table_new (NULL, NULL);
8738 acfg->method_blob_hash = g_hash_table_new (NULL, NULL);
8739 acfg->plt_entry_debug_sym_cache = g_hash_table_new (g_str_hash, g_str_equal);
8740 mono_mutex_init_recursive (&acfg->mutex);
8746 acfg_free (MonoAotCompile *acfg)
8750 img_writer_destroy (acfg->w);
8751 for (i = 0; i < acfg->nmethods; ++i)
8753 g_free (acfg->cfgs [i]);
8754 g_free (acfg->cfgs);
8755 g_free (acfg->static_linking_symbol);
8756 g_free (acfg->got_symbol);
8757 g_free (acfg->plt_symbol);
8758 g_ptr_array_free (acfg->methods, TRUE);
8759 g_ptr_array_free (acfg->got_patches, TRUE);
8760 g_ptr_array_free (acfg->image_table, TRUE);
8761 g_ptr_array_free (acfg->globals, TRUE);
8762 g_ptr_array_free (acfg->unwind_ops, TRUE);
8763 g_hash_table_destroy (acfg->method_indexes);
8764 g_hash_table_destroy (acfg->method_depth);
8765 g_hash_table_destroy (acfg->plt_offset_to_entry);
8766 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i) {
8767 if (acfg->patch_to_plt_entry [i])
8768 g_hash_table_destroy (acfg->patch_to_plt_entry [i]);
8770 g_free (acfg->patch_to_plt_entry);
8771 g_hash_table_destroy (acfg->patch_to_got_offset);
8772 g_hash_table_destroy (acfg->method_to_cfg);
8773 g_hash_table_destroy (acfg->token_info_hash);
8774 g_hash_table_destroy (acfg->method_to_pinvoke_import);
8775 g_hash_table_destroy (acfg->image_hash);
8776 g_hash_table_destroy (acfg->unwind_info_offsets);
8777 g_hash_table_destroy (acfg->method_label_hash);
8778 g_hash_table_destroy (acfg->export_names);
8779 g_hash_table_destroy (acfg->plt_entry_debug_sym_cache);
8780 g_hash_table_destroy (acfg->klass_blob_hash);
8781 g_hash_table_destroy (acfg->method_blob_hash);
8782 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
8783 g_hash_table_destroy (acfg->patch_to_got_offset_by_type [i]);
8784 g_free (acfg->patch_to_got_offset_by_type);
8785 mono_mempool_destroy (acfg->mempool);
8790 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
8792 MonoImage *image = ass->image;
8793 int i, res, all_sizes;
8794 MonoAotCompile *acfg;
8795 char *outfile_name, *tmp_outfile_name, *p;
8796 char llvm_stats_msg [256];
8800 acfg = acfg_create (ass, opts);
8802 memset (&acfg->aot_opts, 0, sizeof (acfg->aot_opts));
8803 acfg->aot_opts.write_symbols = TRUE;
8804 acfg->aot_opts.ntrampolines = 1024;
8805 acfg->aot_opts.nrgctx_trampolines = 1024;
8806 acfg->aot_opts.nimt_trampolines = 128;
8807 acfg->aot_opts.nrgctx_fetch_trampolines = 128;
8808 acfg->aot_opts.ngsharedvt_arg_trampolines = 128;
8809 acfg->aot_opts.llvm_path = g_strdup ("");
8811 acfg->aot_opts.use_trampolines_page = TRUE;
8814 mono_aot_parse_options (aot_options, &acfg->aot_opts);
8816 if (acfg->aot_opts.logfile) {
8817 acfg->logfile = fopen (acfg->aot_opts.logfile, "a+");
8820 if (acfg->aot_opts.static_link)
8821 acfg->aot_opts.autoreg = TRUE;
8823 //acfg->aot_opts.print_skipped_methods = TRUE;
8825 #if !defined(MONO_ARCH_GSHAREDVT_SUPPORTED) || !defined(ENABLE_GSHAREDVT)
8826 if (opts & MONO_OPT_GSHAREDVT) {
8827 aot_printerrf (acfg, "-O=gsharedvt not supported on this platform.\n");
8832 aot_printf (acfg, "Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
8834 #ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
8835 if (acfg->aot_opts.full_aot) {
8836 aot_printerrf (acfg, "--aot=full is not supported on this platform.\n");
8841 if (acfg->aot_opts.direct_pinvoke && !acfg->aot_opts.static_link) {
8842 aot_printerrf (acfg, "The 'direct-pinvoke' AOT option also requires the 'static' AOT option.\n");
8846 if (acfg->aot_opts.static_link)
8847 acfg->aot_opts.asm_writer = TRUE;
8849 if (acfg->aot_opts.soft_debug) {
8850 MonoDebugOptions *opt = mini_get_debug_options ();
8852 opt->mdb_optimizations = TRUE;
8853 opt->gen_seq_points = TRUE;
8855 if (!mono_debug_enabled ()) {
8856 aot_printerrf (acfg, "The soft-debug AOT option requires the --debug option.\n");
8859 acfg->flags |= MONO_AOT_FILE_FLAG_DEBUG;
8862 if (mono_use_llvm) {
8864 acfg->aot_opts.asm_writer = TRUE;
8865 acfg->flags |= MONO_AOT_FILE_FLAG_WITH_LLVM;
8867 if (acfg->aot_opts.soft_debug) {
8868 aot_printerrf (acfg, "The 'soft-debug' option is not supported when compiling with LLVM.\n");
8873 if (acfg->aot_opts.full_aot)
8874 acfg->flags |= MONO_AOT_FILE_FLAG_FULL_AOT;
8876 if (acfg->aot_opts.instances_logfile_path) {
8877 acfg->instances_logfile = fopen (acfg->aot_opts.instances_logfile_path, "w");
8878 if (!acfg->instances_logfile) {
8879 aot_printerrf (acfg, "Unable to create logfile: '%s'.\n", acfg->aot_opts.instances_logfile_path);
8884 load_profile_files (acfg);
8886 acfg->num_trampolines [MONO_AOT_TRAMP_SPECIFIC] = acfg->aot_opts.full_aot ? acfg->aot_opts.ntrampolines : 0;
8887 #ifdef MONO_ARCH_GSHARED_SUPPORTED
8888 acfg->num_trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = acfg->aot_opts.full_aot ? acfg->aot_opts.nrgctx_trampolines : 0;
8890 acfg->num_trampolines [MONO_AOT_TRAMP_IMT_THUNK] = acfg->aot_opts.full_aot ? acfg->aot_opts.nimt_trampolines : 0;
8891 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
8892 if (acfg->opts & MONO_OPT_GSHAREDVT)
8893 acfg->num_trampolines [MONO_AOT_TRAMP_GSHAREDVT_ARG] = acfg->aot_opts.full_aot ? acfg->aot_opts.ngsharedvt_arg_trampolines : 0;
8896 acfg->temp_prefix = img_writer_get_temp_label_prefix (NULL);
8900 acfg->got_symbol_base = g_strdup_printf ("mono_aot_%s_got", acfg->image->assembly->aname.name);
8901 acfg->plt_symbol = g_strdup_printf ("%smono_aot_%s_plt", acfg->llvm_label_prefix, acfg->image->assembly->aname.name);
8902 acfg->assembly_name_sym = g_strdup (acfg->image->assembly->aname.name);
8904 /* Get rid of characters which cannot occur in symbols */
8905 for (p = acfg->got_symbol_base; *p; ++p) {
8906 if (!(isalnum (*p) || *p == '_'))
8909 for (p = acfg->plt_symbol; *p; ++p) {
8910 if (!(isalnum (*p) || *p == '_'))
8913 for (p = acfg->assembly_name_sym; *p; ++p) {
8914 if (!(isalnum (*p) || *p == '_'))
8918 acfg->method_index = 1;
8922 if (acfg->aot_opts.full_aot)
8923 mono_set_partial_sharing_supported (TRUE);
8926 res = collect_methods (acfg);
8930 acfg->cfgs_size = acfg->methods->len + 32;
8931 acfg->cfgs = g_new0 (MonoCompile*, acfg->cfgs_size);
8933 /* PLT offset 0 is reserved for the PLT trampoline */
8934 acfg->plt_offset = 1;
8939 mono_llvm_create_aot_module (acfg->got_symbol_base);
8943 /* GOT offset 0 is reserved for the address of the current assembly */
8947 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
8948 ji->type = MONO_PATCH_INFO_IMAGE;
8949 ji->data.image = acfg->image;
8951 get_got_offset (acfg, ji);
8953 /* Slot 1 is reserved for the mscorlib got addr */
8954 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
8955 ji->type = MONO_PATCH_INFO_MSCORLIB_GOT_ADDR;
8956 get_got_offset (acfg, ji);
8958 /* This is very common */
8959 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
8960 ji->type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
8961 get_got_offset (acfg, ji);
8963 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
8964 ji->type = MONO_PATCH_INFO_JIT_TLS_ID;
8965 get_got_offset (acfg, ji);
8970 compile_methods (acfg);
8974 acfg->stats.jit_time = TV_ELAPSED (atv, btv);
8982 if (acfg->aot_opts.asm_only) {
8983 if (acfg->aot_opts.outfile) {
8984 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
8985 acfg->tmpbasename = g_strdup (acfg->tmpfname);
8987 acfg->tmpbasename = g_strdup_printf ("%s", acfg->image->name);
8988 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->tmpbasename);
8991 acfg->tmpbasename = g_strdup_printf ("%s", "temp");
8992 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->tmpbasename);
8995 res = emit_llvm_file (acfg);
9001 if (!acfg->aot_opts.asm_only && !acfg->aot_opts.asm_writer && bin_writer_supported ()) {
9002 if (acfg->aot_opts.outfile)
9003 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
9005 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
9008 * Can't use g_file_open_tmp () as it will be deleted at exit, and
9009 * it might be in another file system so the rename () won't work.
9011 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
9013 acfg->fp = fopen (tmp_outfile_name, "w");
9015 aot_printf (acfg, "Unable to create temporary file '%s': %s\n", tmp_outfile_name, strerror (errno));
9019 acfg->w = img_writer_create (acfg->fp, TRUE);
9020 acfg->use_bin_writer = TRUE;
9023 /* Append to the .s file created by llvm */
9024 /* FIXME: Use multiple files instead */
9025 acfg->fp = fopen (acfg->tmpfname, "a+");
9027 if (acfg->aot_opts.asm_only) {
9028 if (acfg->aot_opts.outfile)
9029 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
9031 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->image->name);
9032 acfg->fp = fopen (acfg->tmpfname, "w+");
9034 int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
9035 acfg->fp = fdopen (i, "w+");
9038 if (acfg->fp == 0) {
9039 aot_printerrf (acfg, "Unable to open file '%s': %s\n", acfg->tmpfname, strerror (errno));
9042 acfg->w = img_writer_create (acfg->fp, FALSE);
9044 tmp_outfile_name = NULL;
9045 outfile_name = NULL;
9048 acfg->got_symbol = g_strdup_printf ("%s%s", acfg->llvm_label_prefix, acfg->got_symbol_base);
9050 /* Compute symbols for methods */
9051 for (i = 0; i < acfg->nmethods; ++i) {
9052 if (acfg->cfgs [i]) {
9053 MonoCompile *cfg = acfg->cfgs [i];
9054 int method_index = get_method_index (acfg, cfg->orig_method);
9056 if (COMPILE_LLVM (cfg))
9057 cfg->asm_symbol = g_strdup_printf ("%s%s", acfg->llvm_label_prefix, cfg->llvm_method_name);
9058 else if (acfg->global_symbols)
9059 cfg->asm_symbol = get_debug_sym (cfg->method, "", acfg->method_label_hash);
9061 cfg->asm_symbol = g_strdup_printf ("%s%sm_%x", acfg->temp_prefix, acfg->llvm_label_prefix, method_index);
9065 if (acfg->aot_opts.dwarf_debug && acfg->aot_opts.asm_only && acfg->aot_opts.gnu_asm) {
9067 * CLANG supports GAS .file/.loc directives, so emit line number information this way
9068 * FIXME: CLANG only emits line number info for .loc directives followed by assembly, not
9071 //acfg->gas_line_numbers = TRUE;
9074 if (!acfg->aot_opts.nodebug || acfg->aot_opts.dwarf_debug) {
9075 if (acfg->aot_opts.dwarf_debug && !mono_debug_enabled ()) {
9076 aot_printerrf (acfg, "The dwarf AOT option requires the --debug option.\n");
9079 acfg->dwarf = mono_dwarf_writer_create (acfg->w, NULL, 0, FALSE, !acfg->gas_line_numbers);
9082 img_writer_emit_start (acfg->w);
9085 mono_dwarf_writer_emit_base_info (acfg->dwarf, g_path_get_basename (acfg->image->name), mono_unwind_get_cie_program ());
9087 if (acfg->thumb_mixed) {
9090 * This global symbol marks the end of THUMB code, and the beginning of ARM
9091 * code generated by our JIT.
9093 sprintf (symbol, "thumb_end");
9094 emit_section_change (acfg, ".text", 0);
9095 emit_alignment (acfg, 8);
9096 emit_label (acfg, symbol);
9097 emit_zero_bytes (acfg, 16);
9099 fprintf (acfg->fp, ".arm\n");
9106 emit_extra_methods (acfg);
9108 emit_trampolines (acfg);
9110 emit_class_name_table (acfg);
9112 emit_got_info (acfg);
9114 emit_exception_info (acfg);
9116 emit_unwind_info (acfg);
9118 emit_class_info (acfg);
9122 emit_image_table (acfg);
9126 emit_file_info (acfg);
9130 emit_objc_selectors (acfg);
9132 emit_globals (acfg);
9134 emit_autoreg (acfg);
9137 emit_dwarf_info (acfg);
9138 mono_dwarf_writer_close (acfg->dwarf);
9141 emit_mem_end (acfg);
9143 if (acfg->need_pt_gnu_stack) {
9144 /* This is required so the .so doesn't have an executable stack */
9145 /* The bin writer already emits this */
9146 if (!acfg->use_bin_writer)
9147 fprintf (acfg->fp, "\n.section .note.GNU-stack,\"\",@progbits\n");
9152 acfg->stats.gen_time = TV_ELAPSED (atv, btv);
9155 g_assert (acfg->got_offset <= acfg->final_got_size);
9158 sprintf (llvm_stats_msg, ", LLVM: %d (%d%%)", acfg->stats.llvm_count, acfg->stats.mcount ? (acfg->stats.llvm_count * 100) / acfg->stats.mcount : 100);
9160 strcpy (llvm_stats_msg, "");
9162 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;
9164 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",
9165 acfg->stats.code_size, acfg->stats.code_size * 100 / all_sizes,
9166 acfg->stats.info_size, acfg->stats.info_size * 100 / all_sizes,
9167 acfg->stats.ex_info_size, acfg->stats.ex_info_size * 100 / all_sizes,
9168 acfg->stats.unwind_info_size, acfg->stats.unwind_info_size * 100 / all_sizes,
9169 acfg->stats.class_info_size, acfg->stats.class_info_size * 100 / all_sizes,
9170 acfg->stats.plt_size ? acfg->stats.plt_size : acfg->plt_offset, acfg->stats.plt_size ? acfg->stats.plt_size * 100 / all_sizes : 0,
9171 acfg->stats.got_info_size, acfg->stats.got_info_size * 100 / all_sizes,
9172 acfg->stats.offsets_size, acfg->stats.offsets_size * 100 / all_sizes,
9173 (int)(acfg->got_offset * sizeof (gpointer)));
9174 aot_printf (acfg, "Compiled: %d/%d (%d%%)%s, No GOT slots: %d (%d%%), Direct calls: %d (%d%%)\n",
9175 acfg->stats.ccount, acfg->stats.mcount, acfg->stats.mcount ? (acfg->stats.ccount * 100) / acfg->stats.mcount : 100,
9177 acfg->stats.methods_without_got_slots, acfg->stats.mcount ? (acfg->stats.methods_without_got_slots * 100) / acfg->stats.mcount : 100,
9178 acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
9179 if (acfg->stats.genericcount)
9180 aot_printf (acfg, "%d methods are generic (%d%%)\n", acfg->stats.genericcount, acfg->stats.mcount ? (acfg->stats.genericcount * 100) / acfg->stats.mcount : 100);
9181 if (acfg->stats.abscount)
9182 aot_printf (acfg, "%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
9183 if (acfg->stats.lmfcount)
9184 aot_printf (acfg, "%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
9185 if (acfg->stats.ocount)
9186 aot_printf (acfg, "%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
9189 res = img_writer_emit_writeout (acfg->w);
9194 if (acfg->use_bin_writer) {
9195 int err = rename (tmp_outfile_name, outfile_name);
9198 aot_printf (acfg, "Unable to rename '%s' to '%s': %s\n", tmp_outfile_name, outfile_name, strerror (errno));
9202 res = compile_asm (acfg);
9209 acfg->stats.link_time = TV_ELAPSED (atv, btv);
9211 if (acfg->aot_opts.stats) {
9214 aot_printf (acfg, "GOT slot distribution:\n");
9215 for (i = 0; i < MONO_PATCH_INFO_NONE; ++i)
9216 if (acfg->stats.got_slot_types [i])
9217 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]);
9220 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);
9232 mono_aot_readonly_field_override (MonoClassField *field)
9238 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)