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.
11 /* Remaining AOT-only work:
12 * - optimize the trampolines, generate more code in the arch files.
13 * - make things more consistent with how elf works, for example, use ELF
15 * Remaining generics sharing work:
16 * - optimize the size of the data which is encoded.
17 * - optimize the runtime loading of data:
18 * - the trampoline code calls mono_jit_info_table_find () to find the rgctx,
19 * which loads the debugging+exception handling info for the method. This is a
20 * huge waste of time and code, since the rgctx structure is currently empty.
21 * - every shared method has a MonoGenericJitInfo structure which is only really
22 * used for handling catch clauses with open types, not a very common use case.
25 #include <sys/types.h>
46 #include <mono/metadata/tabledefs.h>
47 #include <mono/metadata/class.h>
48 #include <mono/metadata/object.h>
49 #include <mono/metadata/tokentype.h>
50 #include <mono/metadata/appdomain.h>
51 #include <mono/metadata/debug-helpers.h>
52 #include <mono/metadata/assembly.h>
53 #include <mono/metadata/metadata-internals.h>
54 #include <mono/metadata/marshal.h>
55 #include <mono/metadata/gc-internal.h>
56 #include <mono/metadata/monitor.h>
57 #include <mono/metadata/mempool-internals.h>
58 #include <mono/metadata/mono-endian.h>
59 #include <mono/metadata/threads-types.h>
60 #include <mono/utils/mono-logger-internal.h>
61 #include <mono/utils/mono-compiler.h>
62 #include <mono/utils/mono-time.h>
63 #include <mono/utils/mono-mmap.h>
66 #include "image-writer.h"
67 #include "dwarfwriter.h"
69 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
71 #if defined(__linux__) || defined(__native_client_codegen__)
72 #define RODATA_SECT ".rodata"
74 #define RODATA_SECT ".text"
77 #define TV_DECLARE(name) gint64 name
78 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
79 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
82 #define SHARED_EXT ".dll"
83 #elif defined(__ppc__) && defined(__APPLE__)
84 #define SHARED_EXT ".dylib"
85 #elif defined(__APPLE__) && defined(TARGET_X86) && !defined(__native_client_codegen__)
86 #define SHARED_EXT ".dylib"
88 #define SHARED_EXT ".so"
91 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
92 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
93 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
95 /* predefined values for static readonly fields without needed to run the .cctor */
96 typedef struct _ReadOnlyValue ReadOnlyValue;
97 struct _ReadOnlyValue {
100 int type; /* to be used later for typechecking to prevent user errors */
109 static ReadOnlyValue *readonly_values = NULL;
111 typedef struct MonoAotOptions {
114 gboolean write_symbols;
115 gboolean metadata_only;
116 gboolean bind_to_runtime_version;
119 gboolean static_link;
126 int nrgctx_trampolines;
127 int nimt_trampolines;
128 gboolean print_skipped_methods;
136 typedef struct MonoAotStats {
137 int ccount, mcount, lmfcount, abscount, gcount, ocount, genericcount;
138 int code_size, info_size, ex_info_size, unwind_info_size, got_size, class_info_size, got_info_size;
139 int methods_without_got_slots, direct_calls, all_calls, llvm_count;
140 int got_slots, offsets_size;
141 int got_slot_types [MONO_PATCH_INFO_NONE];
142 int jit_time, gen_time, link_time;
145 typedef struct MonoAotCompile {
148 GHashTable *method_indexes;
149 GHashTable *method_depth;
152 GHashTable *patch_to_plt_entry;
153 GHashTable *plt_offset_to_entry;
154 GHashTable *patch_to_got_offset;
155 GHashTable **patch_to_got_offset_by_type;
156 GPtrArray *got_patches;
157 GHashTable *image_hash;
158 GHashTable *method_to_cfg;
159 GHashTable *token_info_hash;
160 GPtrArray *extra_methods;
161 GPtrArray *image_table;
163 GPtrArray *method_order;
164 GHashTable *export_names;
165 guint32 *plt_got_info_offsets;
166 guint32 got_offset, plt_offset, plt_got_offset_base;
167 guint32 final_got_size;
168 /* Number of GOT entries reserved for trampolines */
169 guint32 num_trampoline_got_entries;
171 guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
172 guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
173 guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
175 MonoAotOptions aot_opts;
178 MonoMemPool *mempool;
181 char *static_linking_symbol;
182 CRITICAL_SECTION mutex;
183 gboolean use_bin_writer;
185 MonoDwarfWriter *dwarf;
189 GHashTable *unwind_info_offsets;
190 GPtrArray *unwind_ops;
191 guint32 unwind_info_offset;
192 char *got_symbol_base;
195 GHashTable *method_label_hash;
196 const char *temp_prefix;
197 const char *llvm_label_prefix;
198 guint32 label_generator;
200 MonoAotFileFlags flags;
201 MonoDynamicStream blob;
202 MonoClass **typespec_classes;
205 char *assembly_name_sym;
206 gboolean thumb_mixed, need_no_dead_strip, need_pt_gnu_stack;
211 char *symbol, *llvm_symbol, *debug_sym;
213 gboolean jit_used, llvm_used;
216 #define mono_acfg_lock(acfg) EnterCriticalSection (&((acfg)->mutex))
217 #define mono_acfg_unlock(acfg) LeaveCriticalSection (&((acfg)->mutex))
219 /* This points to the current acfg in LLVM mode */
220 static MonoAotCompile *llvm_acfg;
222 #ifdef HAVE_ARRAY_ELEM_INIT
223 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
224 #define MSGSTRFIELD1(line) str##line
225 static const struct msgstr_t {
226 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
227 #include "patch-info.h"
230 #define PATCH_INFO(a,b) b,
231 #include "patch-info.h"
234 static const gint16 opidx [] = {
235 #define PATCH_INFO(a,b) [MONO_PATCH_INFO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
236 #include "patch-info.h"
240 static G_GNUC_UNUSED const char*
241 get_patch_name (int info)
243 return (const char*)&opstr + opidx [info];
247 #define PATCH_INFO(a,b) b,
248 static const char* const
249 patch_types [MONO_PATCH_INFO_NUM + 1] = {
250 #include "patch-info.h"
254 static G_GNUC_UNUSED const char*
255 get_patch_name (int info)
257 return patch_types [info];
262 /* Wrappers around the image writer functions */
265 emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
267 img_writer_emit_section_change (acfg->w, section_name, subsection_index);
271 emit_push_section (MonoAotCompile *acfg, const char *section_name, int subsection)
273 img_writer_emit_push_section (acfg->w, section_name, subsection);
277 emit_pop_section (MonoAotCompile *acfg)
279 img_writer_emit_pop_section (acfg->w);
283 emit_local_symbol (MonoAotCompile *acfg, const char *name, const char *end_label, gboolean func)
285 img_writer_emit_local_symbol (acfg->w, name, end_label, func);
289 emit_label (MonoAotCompile *acfg, const char *name)
291 img_writer_emit_label (acfg->w, name);
295 emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
297 img_writer_emit_bytes (acfg->w, buf, size);
301 emit_string (MonoAotCompile *acfg, const char *value)
303 img_writer_emit_string (acfg->w, value);
307 emit_line (MonoAotCompile *acfg)
309 img_writer_emit_line (acfg->w);
313 emit_alignment (MonoAotCompile *acfg, int size)
315 img_writer_emit_alignment (acfg->w, size);
319 emit_pointer_unaligned (MonoAotCompile *acfg, const char *target)
321 img_writer_emit_pointer_unaligned (acfg->w, target);
325 emit_pointer (MonoAotCompile *acfg, const char *target)
327 img_writer_emit_pointer (acfg->w, target);
331 emit_int16 (MonoAotCompile *acfg, int value)
333 img_writer_emit_int16 (acfg->w, value);
337 emit_int32 (MonoAotCompile *acfg, int value)
339 img_writer_emit_int32 (acfg->w, value);
343 emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
345 img_writer_emit_symbol_diff (acfg->w, end, start, offset);
349 emit_zero_bytes (MonoAotCompile *acfg, int num)
351 img_writer_emit_zero_bytes (acfg->w, num);
355 emit_byte (MonoAotCompile *acfg, guint8 val)
357 img_writer_emit_byte (acfg->w, val);
360 #ifdef __native_client_codegen__
362 emit_nacl_call_alignment (MonoAotCompile *acfg)
364 img_writer_emit_nacl_call_alignment (acfg->w);
368 static G_GNUC_UNUSED void
369 emit_global_inner (MonoAotCompile *acfg, const char *name, gboolean func)
371 img_writer_emit_global (acfg->w, name, func);
375 emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
377 if (acfg->aot_opts.no_dlsym) {
378 g_ptr_array_add (acfg->globals, g_strdup (name));
379 img_writer_emit_local_symbol (acfg->w, name, NULL, func);
381 img_writer_emit_global (acfg->w, name, func);
386 emit_symbol_size (MonoAotCompile *acfg, const char *name, const char *end_label)
388 img_writer_emit_symbol_size (acfg->w, name, end_label);
392 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
394 img_writer_emit_section_change (acfg->w, RODATA_SECT, 1);
396 /* On apple, all symbols need to be aligned to avoid warnings from ld */
397 emit_alignment (acfg, 4);
399 img_writer_emit_label (acfg->w, name);
400 img_writer_emit_string (acfg->w, value);
403 static G_GNUC_UNUSED void
404 emit_uleb128 (MonoAotCompile *acfg, guint32 value)
407 guint8 b = value & 0x7f;
409 if (value != 0) /* more bytes to come */
415 static G_GNUC_UNUSED void
416 emit_sleb128 (MonoAotCompile *acfg, gint64 value)
419 gboolean negative = (value < 0);
426 /* the following is unnecessary if the
427 * implementation of >>= uses an arithmetic rather
428 * than logical shift for a signed left operand
432 value |= - ((gint64)1 <<(size - 7));
433 /* sign bit of byte is second high order bit (0x40) */
434 if ((value == 0 && !(byte & 0x40)) ||
435 (value == -1 && (byte & 0x40)))
439 emit_byte (acfg, byte);
443 static G_GNUC_UNUSED void
444 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
449 guint8 b = value & 0x7f;
451 if (value != 0) /* more bytes to come */
459 static G_GNUC_UNUSED void
460 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
463 gboolean negative = (value < 0);
471 /* the following is unnecessary if the
472 * implementation of >>= uses an arithmetic rather
473 * than logical shift for a signed left operand
477 value |= - (1 <<(size - 7));
478 /* sign bit of byte is second high order bit (0x40) */
479 if ((value == 0 && !(byte & 0x40)) ||
480 (value == -1 && (byte & 0x40)))
490 /* ARCHITECTURE SPECIFIC CODE */
492 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC)
493 #define EMIT_DWARF_INFO 1
496 #if defined(TARGET_ARM)
497 #define AOT_FUNC_ALIGNMENT 4
499 #define AOT_FUNC_ALIGNMENT 16
501 #if (defined(TARGET_X86) || defined(TARGET_AMD64)) && defined(__native_client_codegen__)
502 #undef AOT_FUNC_ALIGNMENT
503 #define AOT_FUNC_ALIGNMENT 32
506 #if defined(TARGET_POWERPC64) && !defined(__mono_ilp32__)
507 #define PPC_LD_OP "ld"
508 #define PPC_LDX_OP "ldx"
510 #define PPC_LD_OP "lwz"
511 #define PPC_LDX_OP "lwzx"
515 #define AOT_TARGET_STR "AMD64"
520 #define AOT_TARGET_STR "ARM (MACH)"
522 #define AOT_TARGET_STR "ARM (!MACH)"
526 #ifdef TARGET_POWERPC64
527 #ifdef __mono_ilp32__
528 #define AOT_TARGET_STR "POWERPC64 (mono ilp32)"
530 #define AOT_TARGET_STR "POWERPC64 (!mono ilp32)"
533 #ifdef TARGET_POWERPC
534 #ifdef __mono_ilp32__
535 #define AOT_TARGET_STR "POWERPC (mono ilp32)"
537 #define AOT_TARGET_STR "POWERPC (!mono ilp32)"
543 #define AOT_TARGET_STR "WIN32"
547 #ifdef __native_client_codegen__
548 #define AOT_TARGET_STR "X86 (native client codegen)"
550 #define AOT_TARGET_STR "X86 (!native client codegen)"
554 #ifndef AOT_TARGET_STR
555 #define AOT_TARGET_STR ""
559 arch_init (MonoAotCompile *acfg)
561 acfg->llc_args = g_string_new ("");
562 acfg->as_args = g_string_new ("");
565 * The prefix LLVM likes to put in front of symbol names on darwin.
566 * The mach-os specs require this for globals, but LLVM puts them in front of all
567 * symbols. We need to handle this, since we need to refer to LLVM generated
570 acfg->llvm_label_prefix = "";
573 if (acfg->aot_opts.mtriple && strstr (acfg->aot_opts.mtriple, "darwin")) {
574 g_string_append (acfg->llc_args, "-mattr=+v6");
577 g_string_append (acfg->llc_args, " -mattr=+vfp2,+d16");
578 g_string_append (acfg->as_args, " -mfpu=vfp3");
580 g_string_append (acfg->llc_args, " -soft-float");
583 if (acfg->aot_opts.mtriple && strstr (acfg->aot_opts.mtriple, "thumb"))
584 acfg->thumb_mixed = TRUE;
586 if (acfg->aot_opts.mtriple)
587 mono_arch_set_target (acfg->aot_opts.mtriple);
591 acfg->llvm_label_prefix = "_";
592 acfg->need_no_dead_strip = TRUE;
595 #if defined(__linux__) && !defined(TARGET_ARM)
596 acfg->need_pt_gnu_stack = TRUE;
601 * arch_emit_direct_call:
603 * Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
607 arch_emit_direct_call (MonoAotCompile *acfg, const char *target, int *call_size)
609 #if defined(TARGET_X86) || defined(TARGET_AMD64)
610 /* Need to make sure this is exactly 5 bytes long */
611 emit_byte (acfg, '\xe8');
612 emit_symbol_diff (acfg, target, ".", -4);
614 #elif defined(TARGET_ARM)
615 if (acfg->use_bin_writer) {
622 img_writer_emit_reloc (acfg->w, R_ARM_CALL, target, -8);
623 emit_bytes (acfg, buf, 4);
625 img_writer_emit_unset_mode (acfg->w);
626 fprintf (acfg->fp, "bl %s\n", target);
629 #elif defined(TARGET_POWERPC)
630 if (acfg->use_bin_writer) {
631 g_assert_not_reached ();
633 img_writer_emit_unset_mode (acfg->w);
634 fprintf (acfg->fp, "bl %s\n", target);
638 g_assert_not_reached ();
644 * - we use an approach similar to the x86 abi: reserve a register (r30) to hold
646 * - The full-aot trampolines need access to the GOT of mscorlib, so we store
647 * in in the 2. slot of every GOT, and require every method to place the GOT
648 * address in r30, even when it doesn't access the GOT otherwise. This way,
649 * the trampolines can compute the mscorlib GOT address by loading 4(r30).
654 * PPC64 uses function descriptors which greatly complicate all code, since
655 * these are used very inconsistently in the runtime. Some functions like
656 * mono_compile_method () return ftn descriptors, while others like the
657 * trampoline creation functions do not.
658 * We assume that all GOT slots contain function descriptors, and create
659 * descriptors in aot-runtime.c when needed.
660 * The ppc64 abi uses r2 to hold the address of the TOC/GOT, which is loaded
661 * from function descriptors, we could do the same, but it would require
662 * rewriting all the ppc/aot code to handle function descriptors properly.
663 * So instead, we use the same approach as on PPC32.
664 * This is a horrible mess, but fixing it would probably lead to an even bigger
670 * - similar to the PPC32 design, we reserve EBX to hold the GOT pointer.
673 #ifdef MONO_ARCH_AOT_SUPPORTED
675 * arch_emit_got_offset:
677 * The memory pointed to by CODE should hold native code for computing the GOT
678 * address. Emit this code while patching it with the offset between code and
679 * the GOT. CODE_SIZE is set to the number of bytes emitted.
682 arch_emit_got_offset (MonoAotCompile *acfg, guint8 *code, int *code_size)
684 #if defined(TARGET_POWERPC64)
685 g_assert (!acfg->use_bin_writer);
686 img_writer_emit_unset_mode (acfg->w);
688 * The ppc32 code doesn't seem to work on ppc64, the assembler complains about
689 * unsupported relocations. So we store the got address into the .Lgot_addr
690 * symbol which is in the text segment, compute its address, and load it.
692 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
693 fprintf (acfg->fp, "lis 0, (.Lgot_addr + 4 - .L%d)@h\n", acfg->label_generator);
694 fprintf (acfg->fp, "ori 0, 0, (.Lgot_addr + 4 - .L%d)@l\n", acfg->label_generator);
695 fprintf (acfg->fp, "add 30, 30, 0\n");
696 fprintf (acfg->fp, "%s 30, 0(30)\n", PPC_LD_OP);
697 acfg->label_generator ++;
699 #elif defined(TARGET_POWERPC)
700 g_assert (!acfg->use_bin_writer);
701 img_writer_emit_unset_mode (acfg->w);
702 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
703 fprintf (acfg->fp, "lis 0, (%s + 4 - .L%d)@h\n", acfg->got_symbol, acfg->label_generator);
704 fprintf (acfg->fp, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg->got_symbol, acfg->label_generator);
705 acfg->label_generator ++;
708 guint32 offset = mono_arch_get_patch_offset (code);
709 emit_bytes (acfg, code, offset);
710 emit_symbol_diff (acfg, acfg->got_symbol, ".", offset);
712 *code_size = offset + 4;
717 * arch_emit_got_access:
719 * The memory pointed to by CODE should hold native code for loading a GOT
720 * slot. Emit this code while patching it so it accesses the GOT slot GOT_SLOT.
721 * CODE_SIZE is set to the number of bytes emitted.
724 arch_emit_got_access (MonoAotCompile *acfg, guint8 *code, int got_slot, int *code_size)
726 /* Emit beginning of instruction */
727 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
729 /* Emit the offset */
731 emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
732 *code_size = mono_arch_get_patch_offset (code) + 4;
733 #elif defined(TARGET_X86)
734 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
735 *code_size = mono_arch_get_patch_offset (code) + 4;
736 #elif defined(TARGET_ARM)
737 emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
738 *code_size = mono_arch_get_patch_offset (code) + 4;
739 #elif defined(TARGET_POWERPC)
745 ppc_load32 (code, ppc_r0, got_slot * sizeof (gpointer));
746 g_assert (code - buf == 8);
747 emit_bytes (acfg, buf, code - buf);
748 *code_size = code - buf;
751 g_assert_not_reached ();
758 * arch_emit_plt_entry:
760 * Emit code for the PLT entry with index INDEX.
763 arch_emit_plt_entry (MonoAotCompile *acfg, int index)
765 #if defined(TARGET_X86)
766 guint32 offset = (acfg->plt_got_offset_base + index) * sizeof (gpointer);
767 #if defined(__default_codegen__)
768 /* jmp *<offset>(%ebx) */
769 emit_byte (acfg, 0xff);
770 emit_byte (acfg, 0xa3);
771 emit_int32 (acfg, offset);
772 /* Used by mono_aot_get_plt_info_offset */
773 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
774 #elif defined(__native_client_codegen__)
775 const guint8 kSizeOfNaClJmp = 11;
776 guint8 bytes[kSizeOfNaClJmp];
777 guint8 *pbytes = &bytes[0];
779 x86_jump_membase32 (pbytes, X86_EBX, offset);
780 emit_bytes (acfg, bytes, kSizeOfNaClJmp);
781 /* four bytes of data, used by mono_arch_patch_plt_entry */
782 /* For Native Client, make this work with data embedded in push. */
783 emit_byte (acfg, 0x68); /* hide data in a push */
784 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
785 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
786 #endif /*__native_client_codegen__*/
787 #elif defined(TARGET_AMD64)
788 #if defined(__default_codegen__)
790 * We can't emit jumps because they are 32 bits only so they can't be patched.
791 * So we make indirect calls through GOT entries which are patched by the AOT
792 * loader to point to .Lpd entries.
794 /* jmpq *<offset>(%rip) */
795 emit_byte (acfg, '\xff');
796 emit_byte (acfg, '\x25');
797 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) -4);
798 /* Used by mono_aot_get_plt_info_offset */
799 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
800 #elif defined(__native_client_codegen__)
802 guint8 *buf_aligned = ALIGN_TO(buf, kNaClAlignment);
803 guint8 *code = buf_aligned;
805 /* mov <OFFSET>(%rip), %r11d */
806 emit_byte (acfg, '\x45');
807 emit_byte (acfg, '\x8b');
808 emit_byte (acfg, '\x1d');
809 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) -4);
811 amd64_jump_reg (code, AMD64_R11);
812 /* This should be constant for the plt patch */
813 g_assert ((size_t)(code-buf_aligned) == 10);
814 emit_bytes (acfg, buf_aligned, code - buf_aligned);
816 /* Hide data in a push imm32 so it passes validation */
817 emit_byte (acfg, 0x68); /* push */
818 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
819 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
820 #endif /*__native_client_codegen__*/
821 #elif defined(TARGET_ARM)
826 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
827 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
828 emit_bytes (acfg, buf, code - buf);
829 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) - 4);
830 /* Used by mono_aot_get_plt_info_offset */
831 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
832 #elif defined(TARGET_POWERPC)
833 guint32 offset = (acfg->plt_got_offset_base + index) * sizeof (gpointer);
835 /* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
836 g_assert (!acfg->use_bin_writer);
837 img_writer_emit_unset_mode (acfg->w);
838 fprintf (acfg->fp, "lis 11, %d@h\n", offset);
839 fprintf (acfg->fp, "ori 11, 11, %d@l\n", offset);
840 fprintf (acfg->fp, "add 11, 11, 30\n");
841 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
842 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
843 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
844 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
846 fprintf (acfg->fp, "mtctr 11\n");
847 fprintf (acfg->fp, "bctr\n");
848 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
850 g_assert_not_reached ();
855 arch_emit_llvm_plt_entry (MonoAotCompile *acfg, int index)
857 #if defined(TARGET_ARM)
859 /* LLVM calls the PLT entries using bl, so emit a stub */
860 /* FIXME: Too much overhead on every call */
861 fprintf (acfg->fp, ".thumb_func\n");
862 fprintf (acfg->fp, "bx pc\n");
863 fprintf (acfg->fp, "nop\n");
864 fprintf (acfg->fp, ".arm\n");
866 /* LLVM calls the PLT entries using bl, so these have to be thumb2 */
867 /* The caller already transitioned to thumb */
868 /* The code below should be 12 bytes long */
869 fprintf (acfg->fp, "ldr ip, [pc, #8]\n");
870 /* thumb can't encode ld pc, [pc, ip] */
871 fprintf (acfg->fp, "add ip, pc, ip\n");
872 fprintf (acfg->fp, "ldr ip, [ip, #0]\n");
873 fprintf (acfg->fp, "bx ip\n");
874 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) + 4);
875 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
877 g_assert_not_reached ();
882 * arch_emit_specific_trampoline:
884 * Emit code for a specific trampoline. OFFSET is the offset of the first of
885 * two GOT slots which contain the generic trampoline address and the trampoline
886 * argument. TRAMP_SIZE is set to the size of the emitted trampoline.
889 arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
892 * The trampolines created here are variations of the specific
893 * trampolines created in mono_arch_create_specific_trampoline (). The
895 * - the generic trampoline address is taken from a got slot.
896 * - the offset of the got slot where the trampoline argument is stored
897 * is embedded in the instruction stream, and the generic trampoline
898 * can load the argument by loading the offset, adding it to the
899 * address of the trampoline to get the address of the got slot, and
900 * loading the argument from there.
901 * - all the trampolines should be of the same length.
903 #if defined(TARGET_AMD64)
904 #if defined(__default_codegen__)
905 /* This should be exactly 16 bytes long */
907 /* call *<offset>(%rip) */
908 emit_byte (acfg, '\x41');
909 emit_byte (acfg, '\xff');
910 emit_byte (acfg, '\x15');
911 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
912 /* This should be relative to the start of the trampoline */
913 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset+1) * sizeof (gpointer)) + 7);
914 emit_zero_bytes (acfg, 5);
915 #elif defined(__native_client_codegen__)
917 guint8 *buf_aligned = ALIGN_TO(buf, kNaClAlignment);
918 guint8 *code = buf_aligned;
923 /* Emit this call in 'code' so we can find out how long it is. */
924 amd64_call_reg (code, AMD64_R11);
925 call_start = mono_arch_nacl_skip_nops (buf_aligned);
926 call_len = code - call_start;
928 /* The tramp_size is twice the NaCl alignment because it starts with */
929 /* a call which needs to be aligned to the end of the boundary. */
930 *tramp_size = kNaClAlignment*2;
932 /* Emit nops to align call site below which is 7 bytes plus */
933 /* the length of the call sequence emitted above. */
934 /* Note: this requires the specific trampoline starts on a */
935 /* kNaclAlignedment aligned address, which it does because */
936 /* it's its own function that is aligned. */
938 guint8 *nopbuf_aligned = ALIGN_TO (nop_buf, kNaClAlignment);
939 guint8 *nopbuf_end = mono_arch_nacl_pad (nopbuf_aligned, kNaClAlignment - 7 - (call_len));
940 emit_bytes (acfg, nopbuf_aligned, nopbuf_end - nopbuf_aligned);
942 /* The trampoline is stored at the offset'th pointer, the -4 is */
943 /* present because RIP relative addressing starts at the end of */
944 /* the current instruction, while the label "." is relative to */
945 /* the beginning of the current asm location, which in this case */
946 /* is not the mov instruction, but the offset itself, due to the */
947 /* way the bytes and ints are emitted here. */
948 got_offset = (offset * sizeof(gpointer)) - 4;
950 /* mov <OFFSET>(%rip), %r11d */
951 emit_byte (acfg, '\x45');
952 emit_byte (acfg, '\x8b');
953 emit_byte (acfg, '\x1d');
954 emit_symbol_diff (acfg, acfg->got_symbol, ".", got_offset);
957 emit_bytes (acfg, call_start, call_len);
959 /* The arg is stored at the offset+1 pointer, relative to beginning */
960 /* of trampoline: 7 for mov, plus the call length, and 1 for push. */
961 got_offset = ((offset + 1) * sizeof(gpointer)) + 7 + call_len + 1;
963 /* We can't emit this data directly, hide in a "push imm32" */
964 emit_byte (acfg, '\x68'); /* push */
965 emit_symbol_diff (acfg, acfg->got_symbol, ".", got_offset);
966 emit_alignment (acfg, kNaClAlignment);
967 #endif /*__native_client_codegen__*/
968 #elif defined(TARGET_ARM)
972 /* This should be exactly 20 bytes long */
975 ARM_PUSH (code, 0x5fff);
976 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 4);
977 /* Load the value from the GOT */
978 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
980 ARM_BLX_REG (code, ARMREG_R1);
982 g_assert (code - buf == 16);
985 emit_bytes (acfg, buf, code - buf);
987 * Only one offset is needed, since the second one would be equal to the
990 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 4);
991 //emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 8);
992 #elif defined(TARGET_POWERPC)
999 g_assert (!acfg->use_bin_writer);
1002 * PPC has no ip relative addressing, so we need to compute the address
1003 * of the mscorlib got. That is slow and complex, so instead, we store it
1004 * in the second got slot of every aot image. The caller already computed
1005 * the address of its got and placed it into r30.
1007 img_writer_emit_unset_mode (acfg->w);
1008 /* Load mscorlib got address */
1009 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
1010 /* Load generic trampoline address */
1011 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
1012 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
1013 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
1014 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1015 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1017 fprintf (acfg->fp, "mtctr 11\n");
1018 /* Load trampoline argument */
1019 /* On ppc, we pass it normally to the generic trampoline */
1020 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
1021 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
1022 fprintf (acfg->fp, "%s 0, 11, 0\n", PPC_LDX_OP);
1023 /* Branch to generic trampoline */
1024 fprintf (acfg->fp, "bctr\n");
1026 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1027 *tramp_size = 10 * 4;
1029 *tramp_size = 9 * 4;
1031 #elif defined(TARGET_X86)
1035 /* Similar to the PPC code above */
1037 /* FIXME: Could this clobber the register needed by get_vcall_slot () ? */
1039 /* We clobber ECX, since EAX is used as MONO_ARCH_MONITOR_OBJECT_REG */
1040 #ifdef MONO_ARCH_MONITOR_OBJECT_REG
1041 g_assert (MONO_ARCH_MONITOR_OBJECT_REG != X86_ECX);
1045 /* Load mscorlib got address */
1046 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
1047 /* Push trampoline argument */
1048 x86_push_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
1049 /* Load generic trampoline address */
1050 x86_mov_reg_membase (code, X86_ECX, X86_ECX, offset * sizeof (gpointer), 4);
1051 /* Branch to generic trampoline */
1052 x86_jump_reg (code, X86_ECX);
1054 #ifdef __native_client_codegen__
1056 /* emit nops to next 32 byte alignment */
1057 int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
1058 while (code < (buf + a)) x86_nop(code);
1061 emit_bytes (acfg, buf, code - buf);
1063 *tramp_size = NACL_SIZE(17, kNaClAlignment);
1064 g_assert (code - buf == *tramp_size);
1066 g_assert_not_reached ();
1071 * arch_emit_unbox_trampoline:
1073 * Emit code for the unbox trampoline for METHOD used in the full-aot case.
1074 * CALL_TARGET is the symbol pointing to the native code of METHOD.
1077 arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoCompile *cfg, MonoMethod *method, const char *call_target)
1079 #if defined(TARGET_AMD64)
1084 this_reg = mono_arch_get_this_arg_reg (NULL);
1086 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
1088 emit_bytes (acfg, buf, code - buf);
1090 emit_byte (acfg, '\xe9');
1091 emit_symbol_diff (acfg, call_target, ".", -4);
1092 #elif defined(TARGET_X86)
1099 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
1101 emit_bytes (acfg, buf, code - buf);
1104 emit_byte (acfg, '\xe9');
1105 emit_symbol_diff (acfg, call_target, ".", -4);
1106 #elif defined(TARGET_ARM)
1110 if (acfg->thumb_mixed && cfg->compile_llvm) {
1111 fprintf (acfg->fp, "add r0, r0, #%d\n", sizeof (MonoObject));
1112 fprintf (acfg->fp, "b %s\n", call_target);
1113 fprintf (acfg->fp, ".arm\n");
1119 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
1121 emit_bytes (acfg, buf, code - buf);
1122 /* jump to method */
1123 if (acfg->use_bin_writer) {
1130 img_writer_emit_reloc (acfg->w, R_ARM_JUMP24, call_target, -8);
1131 emit_bytes (acfg, buf, 4);
1133 if (acfg->thumb_mixed && cfg->compile_llvm)
1134 fprintf (acfg->fp, "\n\tbx %s\n", call_target);
1136 fprintf (acfg->fp, "\n\tb %s\n", call_target);
1138 #elif defined(TARGET_POWERPC)
1141 g_assert (!acfg->use_bin_writer);
1143 fprintf (acfg->fp, "\n\taddi %d, %d, %d\n", this_pos, this_pos, (int)sizeof (MonoObject));
1144 fprintf (acfg->fp, "\n\tb %s\n", call_target);
1146 g_assert_not_reached ();
1151 * arch_emit_static_rgctx_trampoline:
1153 * Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
1154 * two GOT slots which contain the rgctx argument, and the method to jump to.
1155 * TRAMP_SIZE is set to the size of the emitted trampoline.
1156 * These kinds of trampolines cannot be enumerated statically, since there could
1157 * be one trampoline per method instantiation, so we emit the same code for all
1158 * trampolines, and parameterize them using two GOT slots.
1161 arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1163 #if defined(TARGET_AMD64)
1164 #if defined(__default_codegen__)
1165 /* This should be exactly 13 bytes long */
1168 /* mov <OFFSET>(%rip), %r10 */
1169 emit_byte (acfg, '\x4d');
1170 emit_byte (acfg, '\x8b');
1171 emit_byte (acfg, '\x15');
1172 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
1174 /* jmp *<offset>(%rip) */
1175 emit_byte (acfg, '\xff');
1176 emit_byte (acfg, '\x25');
1177 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
1178 #elif defined(__native_client_codegen__)
1180 guint8 *buf_aligned = ALIGN_TO(buf, kNaClAlignment);
1181 guint8 *code = buf_aligned;
1183 /* mov <OFFSET>(%rip), %r10d */
1184 emit_byte (acfg, '\x45');
1185 emit_byte (acfg, '\x8b');
1186 emit_byte (acfg, '\x15');
1187 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
1189 /* mov <OFFSET>(%rip), %r11d */
1190 emit_byte (acfg, '\x45');
1191 emit_byte (acfg, '\x8b');
1192 emit_byte (acfg, '\x1d');
1193 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
1196 amd64_jump_reg (code, AMD64_R11);
1197 emit_bytes (acfg, buf_aligned, code - buf_aligned);
1199 emit_alignment (acfg, kNaClAlignment);
1200 *tramp_size = kNaClAlignment;
1201 #endif /*__native_client_codegen__*/
1203 #elif defined(TARGET_ARM)
1207 /* This should be exactly 24 bytes long */
1210 /* Load rgctx value */
1211 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
1212 ARM_LDR_REG_REG (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, ARMREG_IP);
1213 /* Load branch addr + branch */
1214 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
1215 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
1217 g_assert (code - buf == 16);
1220 emit_bytes (acfg, buf, code - buf);
1221 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 8);
1222 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 4);
1223 #elif defined(TARGET_POWERPC)
1230 g_assert (!acfg->use_bin_writer);
1233 * PPC has no ip relative addressing, so we need to compute the address
1234 * of the mscorlib got. That is slow and complex, so instead, we store it
1235 * in the second got slot of every aot image. The caller already computed
1236 * the address of its got and placed it into r30.
1238 img_writer_emit_unset_mode (acfg->w);
1239 /* Load mscorlib got address */
1240 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
1242 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
1243 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
1244 fprintf (acfg->fp, "%s %d, 11, 0\n", PPC_LDX_OP, MONO_ARCH_RGCTX_REG);
1245 /* Load target address */
1246 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
1247 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
1248 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
1249 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1250 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
1251 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1253 fprintf (acfg->fp, "mtctr 11\n");
1254 /* Branch to the target address */
1255 fprintf (acfg->fp, "bctr\n");
1257 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1258 *tramp_size = 11 * 4;
1260 *tramp_size = 9 * 4;
1263 #elif defined(TARGET_X86)
1267 /* Similar to the PPC code above */
1269 g_assert (MONO_ARCH_RGCTX_REG != X86_ECX);
1272 /* Load mscorlib got address */
1273 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
1275 x86_mov_reg_membase (code, MONO_ARCH_RGCTX_REG, X86_ECX, offset * sizeof (gpointer), 4);
1276 /* Branch to the target address */
1277 x86_jump_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
1279 #ifdef __native_client_codegen__
1281 /* emit nops to next 32 byte alignment */
1282 int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
1283 while (code < (buf + a)) x86_nop(code);
1287 emit_bytes (acfg, buf, code - buf);
1289 *tramp_size = NACL_SIZE (15, kNaClAlignment);
1290 g_assert (code - buf == *tramp_size);
1292 g_assert_not_reached ();
1297 * arch_emit_imt_thunk:
1299 * Emit an IMT thunk usable in full-aot mode. The thunk uses 1 got slot which
1300 * points to an array of pointer pairs. The pairs of the form [key, ptr], where
1301 * key is the IMT key, and ptr holds the address of a memory location holding
1302 * the address to branch to if the IMT arg matches the key. The array is
1303 * terminated by a pair whose key is NULL, and whose ptr is the address of the
1305 * TRAMP_SIZE is set to the size of the emitted trampoline.
1308 arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
1310 #if defined(TARGET_AMD64)
1312 #if defined(__native_client_codegen__)
1317 guint8 *mov_buf_ptr = mov_buf;
1319 const int kSizeOfMove = 7;
1320 #if defined(__default_codegen__)
1321 code = buf = g_malloc (256);
1322 #elif defined(__native_client_codegen__)
1323 buf_alloc = g_malloc (256 + kNaClAlignment + kSizeOfMove);
1324 buf = ((guint)buf_alloc + kNaClAlignment) & ~kNaClAlignmentMask;
1325 /* The RIP relative move below is emitted first */
1330 /* FIXME: Optimize this, i.e. use binary search etc. */
1331 /* Maybe move the body into a separate function (slower, but much smaller) */
1333 /* MONO_ARCH_IMT_SCRATCH_REG is a free register */
1336 amd64_alu_membase_imm (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, 0);
1338 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
1341 amd64_alu_membase_reg_size (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, MONO_ARCH_IMT_REG, sizeof (gpointer));
1343 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
1346 amd64_alu_reg_imm (code, X86_ADD, MONO_ARCH_IMT_SCRATCH_REG, 2 * sizeof (gpointer));
1347 amd64_jump_code (code, labels [0]);
1350 mono_amd64_patch (labels [2], code);
1351 amd64_mov_reg_membase (code, MONO_ARCH_IMT_SCRATCH_REG, MONO_ARCH_IMT_SCRATCH_REG, sizeof (gpointer), sizeof (gpointer));
1352 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
1356 mono_amd64_patch (labels [1], code);
1357 x86_breakpoint (code);
1359 /* mov <OFFSET>(%rip), MONO_ARCH_IMT_SCRATCH_REG */
1360 amd64_emit_rex (mov_buf_ptr, sizeof(gpointer), MONO_ARCH_IMT_SCRATCH_REG, 0, AMD64_RIP);
1361 *(mov_buf_ptr)++ = (unsigned char)0x8b; /* mov opcode */
1362 x86_address_byte (mov_buf_ptr, 0, MONO_ARCH_IMT_SCRATCH_REG & 0x7, 5);
1363 emit_bytes (acfg, mov_buf, mov_buf_ptr - mov_buf);
1364 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
1366 emit_bytes (acfg, buf, code - buf);
1368 *tramp_size = code - buf + kSizeOfMove;
1369 #if defined(__native_client_codegen__)
1370 /* The tramp will be padded to the next kNaClAlignment bundle. */
1371 *tramp_size = ALIGN_TO ((*tramp_size), kNaClAlignment);
1374 #if defined(__default_codegen__)
1376 #elif defined(__native_client_codegen__)
1380 #elif defined(TARGET_X86)
1382 #ifdef __native_client_codegen__
1387 #if defined(__default_codegen__)
1388 code = buf = g_malloc (256);
1389 #elif defined(__native_client_codegen__)
1390 buf_alloc = g_malloc (256 + kNaClAlignment);
1391 code = buf = ((guint)buf_alloc + kNaClAlignment) & ~kNaClAlignmentMask;
1394 /* Allocate a temporary stack slot */
1395 x86_push_reg (code, X86_EAX);
1397 x86_push_reg (code, X86_EAX);
1399 /* Load mscorlib got address */
1400 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
1402 x86_mov_reg_membase (code, X86_EAX, X86_EAX, offset * sizeof (gpointer), 4);
1405 x86_alu_membase_imm (code, X86_CMP, X86_EAX, 0, 0);
1407 x86_branch8 (code, X86_CC_Z, FALSE, 0);
1410 x86_alu_membase_reg (code, X86_CMP, X86_EAX, 0, MONO_ARCH_IMT_REG);
1412 x86_branch8 (code, X86_CC_Z, FALSE, 0);
1415 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 2 * sizeof (gpointer));
1416 x86_jump_code (code, labels [0]);
1419 mono_x86_patch (labels [2], code);
1420 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer), 4);
1421 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
1422 /* Save the target address to the temporary stack location */
1423 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
1425 x86_pop_reg (code, X86_EAX);
1426 /* Jump to the target address */
1431 mono_x86_patch (labels [1], code);
1432 x86_breakpoint (code);
1434 #ifdef __native_client_codegen__
1436 /* emit nops to next 32 byte alignment */
1437 int a = (~kNaClAlignmentMask) & ((code - buf) + kNaClAlignment - 1);
1438 while (code < (buf + a)) x86_nop(code);
1441 emit_bytes (acfg, buf, code - buf);
1443 *tramp_size = code - buf;
1445 #if defined(__default_codegen__)
1447 #elif defined(__native_client_codegen__)
1451 #elif defined(TARGET_ARM)
1453 guint8 *code, *code2, *labels [16];
1457 /* The IMT method is in v5 */
1459 /* Need at least two free registers, plus a slot for storing the pc */
1460 ARM_PUSH (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_R2));
1462 /* Load the parameter from the GOT */
1463 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
1464 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
1467 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, 0);
1468 ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
1470 ARM_B_COND (code, ARMCOND_EQ, 0);
1472 /* End-of-loop check */
1473 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
1475 ARM_B_COND (code, ARMCOND_EQ, 0);
1478 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (gpointer) * 2);
1481 arm_patch (labels [4], labels [1]);
1484 arm_patch (labels [2], code);
1485 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1486 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 0);
1487 /* Save it to the third stack slot */
1488 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1489 /* Restore the registers and branch */
1490 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1493 arm_patch (labels [3], code);
1494 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1495 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1496 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1500 ARM_LDR_IMM (code2, ARMREG_R0, ARMREG_PC, (code - (labels [0] + 8)));
1502 emit_bytes (acfg, buf, code - buf);
1503 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + (code - (labels [0] + 8)) - 4);
1505 *tramp_size = code - buf + 4;
1506 #elif defined(TARGET_POWERPC)
1508 guint8 *code, *labels [16];
1512 /* Load the mscorlib got address */
1513 ppc_ldptr (code, ppc_r11, sizeof (gpointer), ppc_r30);
1514 /* Load the parameter from the GOT */
1515 ppc_load (code, ppc_r0, offset * sizeof (gpointer));
1516 ppc_ldptr_indexed (code, ppc_r11, ppc_r11, ppc_r0);
1518 /* Load and check key */
1520 ppc_ldptr (code, ppc_r0, 0, ppc_r11);
1521 ppc_cmp (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, MONO_ARCH_IMT_REG);
1523 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
1525 /* End-of-loop check */
1526 ppc_cmpi (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, 0);
1528 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
1531 ppc_addi (code, ppc_r11, ppc_r11, 2 * sizeof (gpointer));
1534 mono_ppc_patch (labels [4], labels [1]);
1537 mono_ppc_patch (labels [2], code);
1538 ppc_ldptr (code, ppc_r11, sizeof (gpointer), ppc_r11);
1539 /* r11 now contains the value of the vtable slot */
1540 /* this is not a function descriptor on ppc64 */
1541 ppc_ldptr (code, ppc_r11, 0, ppc_r11);
1542 ppc_mtctr (code, ppc_r11);
1543 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
1546 mono_ppc_patch (labels [3], code);
1550 *tramp_size = code - buf;
1552 emit_bytes (acfg, buf, code - buf);
1554 g_assert_not_reached ();
1559 arch_emit_autoreg (MonoAotCompile *acfg, char *symbol)
1561 #if defined(TARGET_POWERPC) && defined(__mono_ilp32__)
1562 /* Based on code generated by gcc */
1563 img_writer_emit_unset_mode (acfg->w);
1566 #if defined(_MSC_VER) || defined(MONO_CROSS_COMPILE)
1567 ".section .ctors,\"aw\",@progbits\n"
1571 ".section .opd,\"aw\"\n"
1574 ".long .%s,.TOC.@tocbase32\n"
1577 ".type .%s,@function\n"
1579 ".%s:\n", symbol, symbol, symbol, symbol, symbol, symbol, symbol, symbol);
1581 ".section .ctors,\"aw\",@progbits\n"
1585 ".section .opd,\"aw\"\n"
1588 ".long .%1$s,.TOC.@tocbase32\n"
1589 ".size %1$s,.-%1$s\n"
1591 ".type .%1$s,@function\n"
1593 ".%1$s:\n", symbol);
1604 "lis 3, .Lglobals@h\n"
1605 "ori 3, 3, .Lglobals@l\n"
1606 "bl .mono_aot_register_module\n"
1614 #if defined(_MSC_VER) || defined(MONO_CROSS_COMPILE)
1616 ".size .%s,.-.%s\n", symbol, symbol);
1619 ".size .%1$s,.-.%1$s\n", symbol);
1625 /* END OF ARCH SPECIFIC CODE */
1628 mono_get_field_token (MonoClassField *field)
1630 MonoClass *klass = field->parent;
1633 for (i = 0; i < klass->field.count; ++i) {
1634 if (field == &klass->fields [i])
1635 return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
1638 g_assert_not_reached ();
1643 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
1647 //printf ("ENCODE: %d 0x%x.\n", value, value);
1650 * Same encoding as the one used in the metadata, extended to handle values
1651 * greater than 0x1fffffff.
1653 if ((value >= 0) && (value <= 127))
1655 else if ((value >= 0) && (value <= 16383)) {
1656 p [0] = 0x80 | (value >> 8);
1657 p [1] = value & 0xff;
1659 } else if ((value >= 0) && (value <= 0x1fffffff)) {
1660 p [0] = (value >> 24) | 0xc0;
1661 p [1] = (value >> 16) & 0xff;
1662 p [2] = (value >> 8) & 0xff;
1663 p [3] = value & 0xff;
1668 p [1] = (value >> 24) & 0xff;
1669 p [2] = (value >> 16) & 0xff;
1670 p [3] = (value >> 8) & 0xff;
1671 p [4] = value & 0xff;
1679 stream_init (MonoDynamicStream *sh)
1682 sh->alloc_size = 4096;
1683 sh->data = g_malloc (4096);
1685 /* So offsets are > 0 */
1691 make_room_in_stream (MonoDynamicStream *stream, int size)
1693 if (size <= stream->alloc_size)
1696 while (stream->alloc_size <= size) {
1697 if (stream->alloc_size < 4096)
1698 stream->alloc_size = 4096;
1700 stream->alloc_size *= 2;
1703 stream->data = g_realloc (stream->data, stream->alloc_size);
1707 add_stream_data (MonoDynamicStream *stream, const char *data, guint32 len)
1711 make_room_in_stream (stream, stream->index + len);
1712 memcpy (stream->data + stream->index, data, len);
1713 idx = stream->index;
1714 stream->index += len;
1721 * Add data to the binary blob inside the aot image. Returns the offset inside the
1722 * blob where the data was stored.
1725 add_to_blob (MonoAotCompile *acfg, const guint8 *data, guint32 data_len)
1727 if (acfg->blob.alloc_size == 0)
1728 stream_init (&acfg->blob);
1730 return add_stream_data (&acfg->blob, (char*)data, data_len);
1734 add_to_blob_aligned (MonoAotCompile *acfg, const guint8 *data, guint32 data_len, guint32 align)
1739 if (acfg->blob.alloc_size == 0)
1740 stream_init (&acfg->blob);
1742 count = acfg->blob.index % align;
1744 /* we assume the stream data will be aligned */
1746 add_stream_data (&acfg->blob, buf, 4 - count);
1748 return add_stream_data (&acfg->blob, (char*)data, data_len);
1752 * emit_offset_table:
1754 * Emit a table of increasing offsets in a compact form using differential encoding.
1755 * There is an index entry for each GROUP_SIZE number of entries. The greater the
1756 * group size, the more compact the table becomes, but the slower it becomes to compute
1757 * a given entry. Returns the size of the table.
1760 emit_offset_table (MonoAotCompile *acfg, int noffsets, int group_size, gint32 *offsets)
1762 gint32 current_offset;
1763 int i, buf_size, ngroups, index_entry_size;
1765 guint32 *index_offsets;
1767 ngroups = (noffsets + (group_size - 1)) / group_size;
1769 index_offsets = g_new0 (guint32, ngroups);
1771 buf_size = noffsets * 4;
1772 p = buf = g_malloc0 (buf_size);
1775 for (i = 0; i < noffsets; ++i) {
1776 //printf ("D: %d -> %d\n", i, offsets [i]);
1777 if ((i % group_size) == 0) {
1778 index_offsets [i / group_size] = p - buf;
1779 /* Emit the full value for these entries */
1780 encode_value (offsets [i], p, &p);
1782 /* The offsets are allowed to be non-increasing */
1783 //g_assert (offsets [i] >= current_offset);
1784 encode_value (offsets [i] - current_offset, p, &p);
1786 current_offset = offsets [i];
1789 if (ngroups && index_offsets [ngroups - 1] < 65000)
1790 index_entry_size = 2;
1792 index_entry_size = 4;
1794 /* Emit the header */
1795 emit_int32 (acfg, noffsets);
1796 emit_int32 (acfg, group_size);
1797 emit_int32 (acfg, ngroups);
1798 emit_int32 (acfg, index_entry_size);
1800 /* Emit the index */
1801 for (i = 0; i < ngroups; ++i) {
1802 if (index_entry_size == 2)
1803 emit_int16 (acfg, index_offsets [i]);
1805 emit_int32 (acfg, index_offsets [i]);
1809 emit_bytes (acfg, buf, p - buf);
1811 return (int)(p - buf) + (ngroups * 4);
1815 get_image_index (MonoAotCompile *cfg, MonoImage *image)
1819 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
1823 index = g_hash_table_size (cfg->image_hash);
1824 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
1825 g_ptr_array_add (cfg->image_table, image);
1831 find_typespec_for_class (MonoAotCompile *acfg, MonoClass *klass)
1834 int len = acfg->image->tables [MONO_TABLE_TYPESPEC].rows;
1836 /* FIXME: Search referenced images as well */
1837 if (!acfg->typespec_classes) {
1838 acfg->typespec_classes = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoClass*) * len);
1839 for (i = 0; i < len; ++i) {
1840 acfg->typespec_classes [i] = mono_class_get_full (acfg->image, MONO_TOKEN_TYPE_SPEC | (i + 1), NULL);
1843 for (i = 0; i < len; ++i) {
1844 if (acfg->typespec_classes [i] == klass)
1849 return MONO_TOKEN_TYPE_SPEC | (i + 1);
1855 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf);
1860 * Encode a reference to KLASS. We use our home-grown encoding instead of the
1861 * standard metadata encoding.
1864 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
1868 if (klass->generic_class) {
1870 g_assert (klass->type_token);
1872 /* Find a typespec for a class if possible */
1873 token = find_typespec_for_class (acfg, klass);
1875 encode_value (token, p, &p);
1876 encode_value (get_image_index (acfg, acfg->image), p, &p);
1878 MonoClass *gclass = klass->generic_class->container_class;
1879 MonoGenericInst *inst = klass->generic_class->context.class_inst;
1882 /* Encode it ourselves */
1884 encode_value (MONO_TOKEN_TYPE_SPEC, p, &p);
1885 encode_value (MONO_TYPE_GENERICINST, p, &p);
1886 encode_klass_ref (acfg, gclass, p, &p);
1887 encode_value (inst->type_argc, p, &p);
1888 for (i = 0; i < inst->type_argc; ++i)
1889 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
1891 } else if (klass->type_token) {
1892 g_assert (mono_metadata_token_code (klass->type_token) == MONO_TOKEN_TYPE_DEF);
1893 encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, p, &p);
1894 encode_value (get_image_index (acfg, klass->image), p, &p);
1895 } else if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR)) {
1896 MonoGenericContainer *container = mono_type_get_generic_param_owner (&klass->byval_arg);
1897 g_assert (container);
1900 encode_value (MONO_TOKEN_TYPE_SPEC, p, &p);
1901 encode_value (klass->byval_arg.type, p, &p);
1903 encode_value (mono_type_get_generic_param_num (&klass->byval_arg), p, &p);
1905 encode_value (container->is_method, p, &p);
1906 if (container->is_method)
1907 encode_method_ref (acfg, container->owner.method, p, &p);
1909 encode_klass_ref (acfg, container->owner.klass, p, &p);
1912 g_assert (klass->rank > 0);
1913 encode_value (MONO_TOKEN_TYPE_DEF, p, &p);
1914 encode_value (get_image_index (acfg, klass->image), p, &p);
1915 encode_value (klass->rank, p, &p);
1916 encode_klass_ref (acfg, klass->element_class, p, &p);
1922 encode_field_info (MonoAotCompile *cfg, MonoClassField *field, guint8 *buf, guint8 **endbuf)
1924 guint32 token = mono_get_field_token (field);
1927 encode_klass_ref (cfg, field->parent, p, &p);
1928 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
1929 encode_value (token - MONO_TOKEN_FIELD_DEF, p, &p);
1934 encode_generic_context (MonoAotCompile *acfg, MonoGenericContext *context, guint8 *buf, guint8 **endbuf)
1938 MonoGenericInst *inst;
1940 /* Encode the context */
1941 inst = context->class_inst;
1942 encode_value (inst ? 1 : 0, p, &p);
1944 encode_value (inst->type_argc, p, &p);
1945 for (i = 0; i < inst->type_argc; ++i)
1946 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
1948 inst = context->method_inst;
1949 encode_value (inst ? 1 : 0, p, &p);
1951 encode_value (inst->type_argc, p, &p);
1952 for (i = 0; i < inst->type_argc; ++i)
1953 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
1959 #define MAX_IMAGE_INDEX 250
1962 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf)
1964 guint32 image_index = get_image_index (acfg, method->klass->image);
1965 guint32 token = method->token;
1966 MonoJumpInfoToken *ji;
1971 * The encoding for most methods is as follows:
1972 * - image index encoded as a leb128
1973 * - token index encoded as a leb128
1974 * Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
1975 * types of method encodings.
1978 g_assert (image_index < MONO_AOT_METHODREF_MIN);
1980 /* Mark methods which can't use aot trampolines because they need the further
1981 * processing in mono_magic_trampoline () which requires a MonoMethod*.
1983 if ((method->is_generic && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) ||
1984 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
1985 encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE << 24), p, &p);
1988 * Some wrapper methods are shared using their signature, encode their
1989 * stringified signature instead.
1990 * FIXME: Optimize disk usage
1993 if (method->wrapper_type) {
1994 if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1995 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1996 if (strcmp (method->name, "runtime_invoke_dynamic")) {
1997 name = mono_aot_wrapper_name (method);
1998 } else if (mono_marshal_method_from_wrapper (method) != method) {
1999 /* Direct wrapper, encode it normally */
2001 name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
2004 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
2005 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
2006 name = g_strdup_printf ("(wrapper delegate-invoke):%s (%s)", method->name, tmpsig);
2008 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_BEGIN_INVOKE) {
2009 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
2010 name = g_strdup_printf ("(wrapper delegate-begin-invoke):%s (%s)", method->name, tmpsig);
2012 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_END_INVOKE) {
2013 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
2014 name = g_strdup_printf ("(wrapper delegate-end-invoke):%s (%s)", method->name, tmpsig);
2020 encode_value ((MONO_AOT_METHODREF_WRAPPER_NAME << 24), p, &p);
2021 strcpy ((char*)p, name);
2022 p += strlen (name) + 1;
2024 } else if (method->wrapper_type) {
2025 encode_value ((MONO_AOT_METHODREF_WRAPPER << 24), p, &p);
2027 encode_value (method->wrapper_type, p, &p);
2029 switch (method->wrapper_type) {
2030 case MONO_WRAPPER_REMOTING_INVOKE:
2031 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
2032 case MONO_WRAPPER_XDOMAIN_INVOKE: {
2035 m = mono_marshal_method_from_wrapper (method);
2037 encode_method_ref (acfg, m, p, &p);
2040 case MONO_WRAPPER_PROXY_ISINST:
2041 case MONO_WRAPPER_LDFLD:
2042 case MONO_WRAPPER_LDFLDA:
2043 case MONO_WRAPPER_STFLD:
2044 case MONO_WRAPPER_ISINST: {
2045 MonoClass *proxy_class = mono_marshal_get_wrapper_info (method);
2046 encode_klass_ref (acfg, proxy_class, p, &p);
2049 case MONO_WRAPPER_LDFLD_REMOTE:
2050 case MONO_WRAPPER_STFLD_REMOTE:
2052 case MONO_WRAPPER_ALLOC: {
2053 AllocatorWrapperInfo *info = mono_marshal_get_wrapper_info (method);
2055 /* The GC name is saved once in MonoAotFileInfo */
2056 g_assert (info->alloc_type != -1);
2057 encode_value (info->alloc_type, p, &p);
2060 case MONO_WRAPPER_WRITE_BARRIER:
2062 case MONO_WRAPPER_STELEMREF: {
2063 MonoClass *klass = mono_marshal_get_wrapper_info (method);
2065 /* Make sure this is the 'normal' stelemref wrapper, not the virtual one */
2069 case MONO_WRAPPER_UNKNOWN:
2070 if (strcmp (method->name, "FastMonitorEnter") == 0) {
2071 encode_value (MONO_AOT_WRAPPER_MONITOR_ENTER, p, &p);
2072 } else if (strcmp (method->name, "FastMonitorExit") == 0) {
2073 encode_value (MONO_AOT_WRAPPER_MONITOR_EXIT, p, &p);
2074 } else if (strcmp (method->name, "FastMonitorEnterV4") == 0) {
2075 encode_value (MONO_AOT_WRAPPER_MONITOR_ENTER_V4, p, &p);
2076 } else if (strcmp (method->name, "PtrToStructure") == 0) {
2077 encode_value (MONO_AOT_WRAPPER_PTR_TO_STRUCTURE, p, &p);
2078 encode_klass_ref (acfg, method->klass, p, &p);
2079 } else if (strcmp (method->name, "StructureToPtr") == 0) {
2080 encode_value (MONO_AOT_WRAPPER_STRUCTURE_TO_PTR, p, &p);
2081 encode_klass_ref (acfg, method->klass, p, &p);
2083 g_assert_not_reached ();
2086 case MONO_WRAPPER_SYNCHRONIZED:
2087 case MONO_WRAPPER_MANAGED_TO_NATIVE:
2088 case MONO_WRAPPER_RUNTIME_INVOKE: {
2091 m = mono_marshal_method_from_wrapper (method);
2093 g_assert (m != method);
2094 encode_method_ref (acfg, m, p, &p);
2097 case MONO_WRAPPER_MANAGED_TO_MANAGED:
2098 if (!strcmp (method->name, "ElementAddr")) {
2099 ElementAddrWrapperInfo *info = mono_marshal_get_wrapper_info (method);
2102 encode_value (MONO_AOT_WRAPPER_ELEMENT_ADDR, p, &p);
2103 encode_value (info->rank, p, &p);
2104 encode_value (info->elem_size, p, &p);
2106 g_assert_not_reached ();
2109 case MONO_WRAPPER_CASTCLASS:
2110 if (!strcmp (method->name, "__castclass_with_cache")) {
2111 encode_value (MONO_AOT_WRAPPER_CASTCLASS_WITH_CACHE, p, &p);
2112 } else if (!strcmp (method->name, "__isinst_with_cache")) {
2113 encode_value (MONO_AOT_WRAPPER_ISINST_WITH_CACHE, p, &p);
2115 g_assert_not_reached ();
2119 g_assert_not_reached ();
2121 } else if (mono_method_signature (method)->is_inflated) {
2123 * This is a generic method, find the original token which referenced it and
2125 * Obtain the token from information recorded by the JIT.
2127 ji = g_hash_table_lookup (acfg->token_info_hash, method);
2129 image_index = get_image_index (acfg, ji->image);
2130 g_assert (image_index < MAX_IMAGE_INDEX);
2133 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
2134 encode_value (image_index, p, &p);
2135 encode_value (token, p, &p);
2137 MonoMethod *declaring;
2138 MonoGenericContext *context = mono_method_get_context (method);
2140 g_assert (method->is_inflated);
2141 declaring = ((MonoMethodInflated*)method)->declaring;
2144 * This might be a non-generic method of a generic instance, which
2145 * doesn't have a token since the reference is generated by the JIT
2146 * like Nullable:Box/Unbox, or by generic sharing.
2149 encode_value ((MONO_AOT_METHODREF_GINST << 24), p, &p);
2150 /* Encode the klass */
2151 encode_klass_ref (acfg, method->klass, p, &p);
2152 /* Encode the method */
2153 image_index = get_image_index (acfg, method->klass->image);
2154 g_assert (image_index < MAX_IMAGE_INDEX);
2155 g_assert (declaring->token);
2156 token = declaring->token;
2157 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
2158 encode_value (image_index, p, &p);
2159 encode_value (token, p, &p);
2160 encode_generic_context (acfg, context, p, &p);
2162 } else if (token == 0) {
2163 /* This might be a method of a constructed type like int[,].Set */
2164 /* Obtain the token from information recorded by the JIT */
2165 ji = g_hash_table_lookup (acfg->token_info_hash, method);
2167 image_index = get_image_index (acfg, ji->image);
2168 g_assert (image_index < MAX_IMAGE_INDEX);
2171 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
2172 encode_value (image_index, p, &p);
2173 encode_value (token, p, &p);
2176 g_assert (method->klass->rank);
2178 /* Encode directly */
2179 encode_value ((MONO_AOT_METHODREF_ARRAY << 24), p, &p);
2180 encode_klass_ref (acfg, method->klass, p, &p);
2181 if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank)
2182 encode_value (0, p, &p);
2183 else if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank * 2)
2184 encode_value (1, p, &p);
2185 else if (!strcmp (method->name, "Get"))
2186 encode_value (2, p, &p);
2187 else if (!strcmp (method->name, "Address"))
2188 encode_value (3, p, &p);
2189 else if (!strcmp (method->name, "Set"))
2190 encode_value (4, p, &p);
2192 g_assert_not_reached ();
2195 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
2196 encode_value ((image_index << 24) | mono_metadata_token_index (token), p, &p);
2202 compare_patches (gconstpointer a, gconstpointer b)
2206 i = (*(MonoJumpInfo**)a)->ip.i;
2207 j = (*(MonoJumpInfo**)b)->ip.i;
2218 static G_GNUC_UNUSED char*
2219 patch_to_string (MonoJumpInfo *patch_info)
2223 str = g_string_new ("");
2225 g_string_append_printf (str, "%s(", get_patch_name (patch_info->type));
2227 switch (patch_info->type) {
2228 case MONO_PATCH_INFO_VTABLE:
2229 mono_type_get_desc (str, &patch_info->data.klass->byval_arg, TRUE);
2234 g_string_append_printf (str, ")");
2235 return g_string_free (str, FALSE);
2241 * Return whenever PATCH_INFO refers to a direct call, and thus requires a
2244 static inline gboolean
2245 is_plt_patch (MonoJumpInfo *patch_info)
2247 switch (patch_info->type) {
2248 case MONO_PATCH_INFO_METHOD:
2249 case MONO_PATCH_INFO_INTERNAL_METHOD:
2250 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
2251 case MONO_PATCH_INFO_ICALL_ADDR:
2252 case MONO_PATCH_INFO_CLASS_INIT:
2253 case MONO_PATCH_INFO_RGCTX_FETCH:
2254 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2255 case MONO_PATCH_INFO_MONITOR_ENTER:
2256 case MONO_PATCH_INFO_MONITOR_EXIT:
2257 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE:
2267 * Return the symbol identifying the plt entry PLT_OFFSET.
2270 get_plt_symbol (MonoAotCompile *acfg, int plt_offset, MonoJumpInfo *patch_info)
2274 * The Apple linker reorganizes object files, so it doesn't like branches to local
2275 * labels, since those have no relocations.
2277 return g_strdup_printf ("%sp_%d", acfg->llvm_label_prefix, plt_offset);
2279 return g_strdup_printf ("%sp_%d", acfg->temp_prefix, plt_offset);
2286 * Return a PLT entry which belongs to the method identified by PATCH_INFO.
2288 static MonoPltEntry*
2289 get_plt_entry (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
2293 if (!is_plt_patch (patch_info))
2296 res = g_hash_table_lookup (acfg->patch_to_plt_entry, patch_info);
2298 // FIXME: This breaks the calculation of final_got_size
2299 if (!acfg->llvm && patch_info->type == MONO_PATCH_INFO_METHOD && (patch_info->data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
2301 * Allocate a separate PLT slot for each such patch, since some plt
2302 * entries will refer to the method itself, and some will refer to the
2309 MonoJumpInfo *new_ji;
2311 g_assert (!acfg->final_got_size);
2313 new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
2315 res = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoPltEntry));
2316 res->plt_offset = acfg->plt_offset;
2318 res->symbol = get_plt_symbol (acfg, res->plt_offset, patch_info);
2319 res->llvm_symbol = g_strdup_printf ("%s_llvm", res->symbol);
2321 g_hash_table_insert (acfg->patch_to_plt_entry, new_ji, res);
2323 g_hash_table_insert (acfg->plt_offset_to_entry, GUINT_TO_POINTER (res->plt_offset), res);
2325 acfg->plt_offset ++;
2334 * Returns the offset of the GOT slot where the runtime object resulting from resolving
2335 * JI could be found if it exists, otherwise allocates a new one.
2338 get_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
2342 got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_got_offset_by_type [ji->type], ji));
2344 return got_offset - 1;
2346 got_offset = acfg->got_offset;
2347 acfg->got_offset ++;
2349 if (acfg->final_got_size)
2350 g_assert (got_offset < acfg->final_got_size);
2352 acfg->stats.got_slots ++;
2353 acfg->stats.got_slot_types [ji->type] ++;
2355 g_hash_table_insert (acfg->patch_to_got_offset, ji, GUINT_TO_POINTER (got_offset + 1));
2356 g_hash_table_insert (acfg->patch_to_got_offset_by_type [ji->type], ji, GUINT_TO_POINTER (got_offset + 1));
2357 g_ptr_array_add (acfg->got_patches, ji);
2362 /* Add a method to the list of methods which need to be emitted */
2364 add_method_with_index (MonoAotCompile *acfg, MonoMethod *method, int index, gboolean extra)
2367 if (!g_hash_table_lookup (acfg->method_indexes, method)) {
2368 g_ptr_array_add (acfg->methods, method);
2369 g_hash_table_insert (acfg->method_indexes, method, GUINT_TO_POINTER (index + 1));
2370 acfg->nmethods = acfg->methods->len + 1;
2373 if (method->wrapper_type || extra)
2374 g_ptr_array_add (acfg->extra_methods, method);
2378 get_method_index (MonoAotCompile *acfg, MonoMethod *method)
2380 int index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
2388 add_method_full (MonoAotCompile *acfg, MonoMethod *method, gboolean extra, int depth)
2392 index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
2396 index = acfg->method_index;
2397 add_method_with_index (acfg, method, index, extra);
2399 g_ptr_array_add (acfg->method_order, GUINT_TO_POINTER (index));
2401 g_hash_table_insert (acfg->method_depth, method, GUINT_TO_POINTER (depth));
2403 acfg->method_index ++;
2409 add_method (MonoAotCompile *acfg, MonoMethod *method)
2411 return add_method_full (acfg, method, FALSE, 0);
2415 add_extra_method (MonoAotCompile *acfg, MonoMethod *method)
2417 add_method_full (acfg, method, TRUE, 0);
2421 add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth)
2423 add_method_full (acfg, method, TRUE, depth);
2427 add_jit_icall_wrapper (gpointer key, gpointer value, gpointer user_data)
2429 MonoAotCompile *acfg = user_data;
2430 MonoJitICallInfo *callinfo = value;
2431 MonoMethod *wrapper;
2437 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
2438 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
2441 add_method (acfg, wrapper);
2445 get_runtime_invoke_sig (MonoMethodSignature *sig)
2447 MonoMethodBuilder *mb;
2450 mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
2451 m = mono_mb_create_method (mb, sig, 16);
2452 return mono_marshal_get_runtime_invoke (m, FALSE);
2456 can_marshal_struct (MonoClass *klass)
2458 MonoClassField *field;
2459 gboolean can_marshal = TRUE;
2460 gpointer iter = NULL;
2462 if ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) == TYPE_ATTRIBUTE_AUTO_LAYOUT)
2465 /* Only allow a few field types to avoid asserts in the marshalling code */
2466 while ((field = mono_class_get_fields (klass, &iter))) {
2467 if ((field->type->attrs & FIELD_ATTRIBUTE_STATIC))
2470 switch (field->type->type) {
2475 case MONO_TYPE_BOOLEAN:
2478 case MONO_TYPE_CHAR:
2486 case MONO_TYPE_STRING:
2488 case MONO_TYPE_VALUETYPE:
2489 if (!mono_class_from_mono_type (field->type)->enumtype && !can_marshal_struct (mono_class_from_mono_type (field->type)))
2490 can_marshal = FALSE;
2493 can_marshal = FALSE;
2499 /* Its hard to compute whenever these can be marshalled or not */
2500 if (!strcmp (klass->name_space, "System.Net.NetworkInformation.MacOsStructs"))
2507 add_wrappers (MonoAotCompile *acfg)
2509 MonoMethod *method, *m;
2511 MonoMethodSignature *sig, *csig;
2515 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
2516 * so there is only one wrapper of a given type, or inlining their contents into their
2521 * FIXME: This depends on the fact that different wrappers have different
2525 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2527 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
2528 gboolean skip = FALSE;
2530 method = mono_get_method (acfg->image, token, NULL);
2532 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2533 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2534 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
2537 if (method->is_generic || method->klass->generic_container)
2540 /* Skip methods which can not be handled by get_runtime_invoke () */
2541 sig = mono_method_signature (method);
2544 if ((sig->ret->type == MONO_TYPE_PTR) ||
2545 (sig->ret->type == MONO_TYPE_TYPEDBYREF))
2548 for (j = 0; j < sig->param_count; j++) {
2549 if (sig->params [j]->type == MONO_TYPE_TYPEDBYREF)
2553 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2554 if (!method->klass->contextbound) {
2555 MonoDynCallInfo *info = mono_arch_dyn_call_prepare (sig);
2556 gboolean has_nullable = FALSE;
2558 for (j = 0; j < sig->param_count; j++) {
2559 if (sig->params [j]->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (sig->params [j])))
2560 has_nullable = TRUE;
2563 if (info && !has_nullable) {
2564 /* Supported by the dynamic runtime-invoke wrapper */
2572 //printf ("%s\n", mono_method_full_name (method, TRUE));
2573 add_method (acfg, mono_marshal_get_runtime_invoke (method, FALSE));
2577 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
2578 #ifdef MONO_ARCH_HAVE_TLS_GET
2579 MonoMethodDesc *desc;
2580 MonoMethod *orig_method;
2584 /* Runtime invoke wrappers */
2586 /* void runtime-invoke () [.cctor] */
2587 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
2588 csig->ret = &mono_defaults.void_class->byval_arg;
2589 add_method (acfg, get_runtime_invoke_sig (csig));
2591 /* void runtime-invoke () [Finalize] */
2592 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
2594 csig->ret = &mono_defaults.void_class->byval_arg;
2595 add_method (acfg, get_runtime_invoke_sig (csig));
2597 /* void runtime-invoke (string) [exception ctor] */
2598 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
2600 csig->ret = &mono_defaults.void_class->byval_arg;
2601 csig->params [0] = &mono_defaults.string_class->byval_arg;
2602 add_method (acfg, get_runtime_invoke_sig (csig));
2604 /* void runtime-invoke (string, string) [exception ctor] */
2605 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
2607 csig->ret = &mono_defaults.void_class->byval_arg;
2608 csig->params [0] = &mono_defaults.string_class->byval_arg;
2609 csig->params [1] = &mono_defaults.string_class->byval_arg;
2610 add_method (acfg, get_runtime_invoke_sig (csig));
2612 /* string runtime-invoke () [Exception.ToString ()] */
2613 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
2615 csig->ret = &mono_defaults.string_class->byval_arg;
2616 add_method (acfg, get_runtime_invoke_sig (csig));
2618 /* void runtime-invoke (string, Exception) [exception ctor] */
2619 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
2621 csig->ret = &mono_defaults.void_class->byval_arg;
2622 csig->params [0] = &mono_defaults.string_class->byval_arg;
2623 csig->params [1] = &mono_defaults.exception_class->byval_arg;
2624 add_method (acfg, get_runtime_invoke_sig (csig));
2626 /* Assembly runtime-invoke (string, bool) [DoAssemblyResolve] */
2627 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
2629 csig->ret = &(mono_class_from_name (
2630 mono_defaults.corlib, "System.Reflection", "Assembly"))->byval_arg;
2631 csig->params [0] = &mono_defaults.string_class->byval_arg;
2632 csig->params [1] = &mono_defaults.boolean_class->byval_arg;
2633 add_method (acfg, get_runtime_invoke_sig (csig));
2635 /* runtime-invoke used by finalizers */
2636 add_method (acfg, mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE));
2638 /* This is used by mono_runtime_capture_context () */
2639 method = mono_get_context_capture_method ();
2641 add_method (acfg, mono_marshal_get_runtime_invoke (method, FALSE));
2643 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
2644 add_method (acfg, mono_marshal_get_runtime_invoke_dynamic ());
2647 /* JIT icall wrappers */
2648 /* FIXME: locking */
2649 g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
2652 add_method (acfg, mono_marshal_get_stelemref ());
2654 #ifdef MONO_ARCH_HAVE_TLS_GET
2655 /* Managed Allocators */
2656 nallocators = mono_gc_get_managed_allocator_types ();
2657 for (i = 0; i < nallocators; ++i) {
2658 m = mono_gc_get_managed_allocator_by_type (i);
2660 add_method (acfg, m);
2663 /* Monitor Enter/Exit */
2664 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
2665 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
2666 g_assert (orig_method);
2667 mono_method_desc_free (desc);
2668 method = mono_monitor_get_fast_path (orig_method);
2670 add_method (acfg, method);
2672 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
2673 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
2674 g_assert (orig_method);
2675 mono_method_desc_free (desc);
2676 method = mono_monitor_get_fast_path (orig_method);
2678 add_method (acfg, method);
2681 /* Stelemref wrappers */
2682 /* There is only a constant number of these, iterating over all types should handle them all */
2683 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
2686 token = MONO_TOKEN_TYPE_DEF | (i + 1);
2687 klass = mono_class_get (acfg->image, token);
2689 add_method (acfg, mono_marshal_get_virtual_stelemref (mono_array_class_get (klass, 1)));
2691 mono_loader_clear_error ();
2694 /* castclass_with_check wrapper */
2695 add_method (acfg, mono_marshal_get_castclass_with_cache ());
2696 /* isinst_with_check wrapper */
2697 add_method (acfg, mono_marshal_get_isinst_with_cache ());
2699 #if defined(MONO_ARCH_ENABLE_MONITOR_IL_FASTPATH)
2701 MonoMethodDesc *desc;
2704 desc = mono_method_desc_new ("Monitor:Enter(object,bool&)", FALSE);
2705 m = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
2706 mono_method_desc_free (desc);
2708 m = mono_monitor_get_fast_path (m);
2709 add_method (acfg, m);
2716 * remoting-invoke-with-check wrappers are very frequent, so avoid emitting them,
2717 * we use the original method instead at runtime.
2718 * Since full-aot doesn't support remoting, this is not a problem.
2721 /* remoting-invoke wrappers */
2722 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2723 MonoMethodSignature *sig;
2725 token = MONO_TOKEN_METHOD_DEF | (i + 1);
2726 method = mono_get_method (acfg->image, token, NULL);
2728 sig = mono_method_signature (method);
2730 if (sig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
2731 m = mono_marshal_get_remoting_invoke_with_check (method);
2733 add_method (acfg, m);
2738 /* delegate-invoke wrappers */
2739 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
2742 token = MONO_TOKEN_TYPE_DEF | (i + 1);
2743 klass = mono_class_get (acfg->image, token);
2746 mono_loader_clear_error ();
2750 if (klass->delegate && klass != mono_defaults.delegate_class && klass != mono_defaults.multicastdelegate_class && !klass->generic_container) {
2751 method = mono_get_delegate_invoke (klass);
2753 m = mono_marshal_get_delegate_invoke (method, NULL);
2755 add_method (acfg, m);
2757 method = mono_class_get_method_from_name_flags (klass, "BeginInvoke", -1, 0);
2758 add_method (acfg, mono_marshal_get_delegate_begin_invoke (method));
2760 method = mono_class_get_method_from_name_flags (klass, "EndInvoke", -1, 0);
2761 add_method (acfg, mono_marshal_get_delegate_end_invoke (method));
2765 /* Synchronized wrappers */
2766 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2767 token = MONO_TOKEN_METHOD_DEF | (i + 1);
2768 method = mono_get_method (acfg->image, token, NULL);
2770 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED && !method->is_generic)
2771 add_method (acfg, mono_marshal_get_synchronized_wrapper (method));
2774 /* pinvoke wrappers */
2775 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2777 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
2779 method = mono_get_method (acfg->image, token, NULL);
2781 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2782 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
2783 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
2787 /* native-to-managed wrappers */
2788 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2790 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
2791 MonoCustomAttrInfo *cattr;
2794 method = mono_get_method (acfg->image, token, NULL);
2797 * Only generate native-to-managed wrappers for methods which have an
2798 * attribute named MonoPInvokeCallbackAttribute. We search for the attribute by
2799 * name to avoid defining a new assembly to contain it.
2801 cattr = mono_custom_attrs_from_method (method);
2804 for (j = 0; j < cattr->num_attrs; ++j)
2805 if (cattr->attrs [j].ctor && !strcmp (cattr->attrs [j].ctor->klass->name, "MonoPInvokeCallbackAttribute"))
2807 if (j < cattr->num_attrs) {
2808 MonoCustomAttrEntry *e = &cattr->attrs [j];
2809 MonoMethodSignature *sig = mono_method_signature (e->ctor);
2810 const char *p = (const char*)e->data;
2812 int slen, num_named, named_type, data_type;
2816 char *export_name = NULL;
2817 MonoMethod *wrapper;
2819 g_assert (method->flags & METHOD_ATTRIBUTE_STATIC);
2821 g_assert (sig->param_count == 1);
2822 g_assert (sig->params [0]->type == MONO_TYPE_CLASS && !strcmp (mono_class_from_mono_type (sig->params [0])->name, "Type"));
2825 * Decode the cattr manually since we can't create objects
2826 * during aot compilation.
2832 /* From load_cattr_value () in reflection.c */
2833 slen = mono_metadata_decode_value (p, &p);
2834 n = g_memdup (p, slen + 1);
2836 t = mono_reflection_type_from_name (n, acfg->image);
2840 klass = mono_class_from_mono_type (t);
2841 g_assert (klass->parent == mono_defaults.multicastdelegate_class);
2845 num_named = read16 (p);
2848 g_assert (num_named < 2);
2849 if (num_named == 1) {
2852 MonoType *prop_type;
2854 /* parse ExportSymbol attribute */
2856 named_type = *named;
2861 name_len = mono_metadata_decode_blob_size (named, &named);
2862 name = g_malloc (name_len + 1);
2863 memcpy (name, named, name_len);
2864 name [name_len] = 0;
2867 g_assert (named_type == 0x54);
2868 g_assert (!strcmp (name, "ExportSymbol"));
2870 prop_type = &mono_defaults.string_class->byval_arg;
2872 /* load_cattr_value (), string case */
2873 g_assert (*named != (char)0xff);
2874 slen = mono_metadata_decode_value (named, &named);
2875 export_name = g_malloc (slen + 1);
2876 memcpy (export_name, named, slen);
2877 export_name [slen] = 0;
2881 wrapper = mono_marshal_get_managed_wrapper (method, klass, 0);
2882 add_method (acfg, wrapper);
2884 g_hash_table_insert (acfg->export_names, wrapper, export_name);
2888 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2889 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
2890 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
2894 /* StructureToPtr/PtrToStructure wrappers */
2895 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
2898 token = MONO_TOKEN_TYPE_DEF | (i + 1);
2899 klass = mono_class_get (acfg->image, token);
2902 mono_loader_clear_error ();
2906 if (klass->valuetype && !klass->generic_container && can_marshal_struct (klass)) {
2907 add_method (acfg, mono_marshal_get_struct_to_ptr (klass));
2908 add_method (acfg, mono_marshal_get_ptr_to_struct (klass));
2914 has_type_vars (MonoClass *klass)
2916 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
2919 return has_type_vars (klass->element_class);
2920 if (klass->generic_class) {
2921 MonoGenericContext *context = &klass->generic_class->context;
2922 if (context->class_inst) {
2925 for (i = 0; i < context->class_inst->type_argc; ++i)
2926 if (has_type_vars (mono_class_from_mono_type (context->class_inst->type_argv [i])))
2934 method_has_type_vars (MonoMethod *method)
2936 if (has_type_vars (method->klass))
2939 if (method->is_inflated) {
2940 MonoGenericContext *context = mono_method_get_context (method);
2941 if (context->method_inst) {
2944 for (i = 0; i < context->method_inst->type_argc; ++i)
2945 if (has_type_vars (mono_class_from_mono_type (context->method_inst->type_argv [i])))
2952 static void add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth);
2955 add_generic_class (MonoAotCompile *acfg, MonoClass *klass, gboolean force)
2957 /* This might lead to a huge code blowup so only do it if neccesary */
2958 if (!acfg->aot_opts.full_aot && !force)
2961 add_generic_class_with_depth (acfg, klass, 0);
2965 check_type_depth (MonoType *t, int depth)
2973 case MONO_TYPE_GENERICINST: {
2974 MonoGenericClass *gklass = t->data.generic_class;
2975 MonoGenericInst *ginst = gklass->context.class_inst;
2978 for (i = 0; i < ginst->type_argc; ++i) {
2979 if (check_type_depth (ginst->type_argv [i], depth + 1))
2993 * add_generic_class:
2995 * Add all methods of a generic class.
2998 add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth)
3003 mono_class_init (klass);
3005 if (klass->generic_class && klass->generic_class->context.class_inst->is_open)
3008 if (has_type_vars (klass))
3011 if (!klass->generic_class && !klass->rank)
3014 if (check_type_depth (&klass->byval_arg, 0))
3018 while ((method = mono_class_get_methods (klass, &iter))) {
3019 if (mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE))
3023 if (method->is_generic)
3028 * FIXME: Instances which are referenced by these methods are not added,
3029 * for example Array.Resize<int> for List<int>.Add ().
3031 add_extra_method_with_depth (acfg, method, depth + 1);
3034 if (klass->delegate) {
3035 method = mono_get_delegate_invoke (klass);
3037 method = mono_marshal_get_delegate_invoke (method, NULL);
3039 add_method (acfg, method);
3043 * For ICollection<T>, add instances of the helper methods
3044 * in Array, since a T[] could be cast to ICollection<T>.
3046 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") &&
3047 (!strcmp(klass->name, "ICollection`1") || !strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IList`1") || !strcmp (klass->name, "IEnumerator`1"))) {
3048 MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
3049 MonoClass *array_class = mono_bounded_array_class_get (tclass, 1, FALSE);
3053 if (!strcmp (klass->name, "IEnumerator`1"))
3054 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, "IEnumerable`1");
3056 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
3058 /* Add the T[]/InternalEnumerator class */
3059 if (!strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IEnumerator`1")) {
3063 while ((nclass = mono_class_get_nested_types (array_class->parent, &iter))) {
3064 if (!strcmp (nclass->name, "InternalEnumerator`1"))
3068 nclass = mono_class_inflate_generic_class (nclass, mono_generic_class_get_context (klass->generic_class));
3069 add_generic_class (acfg, nclass, FALSE);
3073 while ((method = mono_class_get_methods (array_class, &iter))) {
3074 if (strstr (method->name, name_prefix)) {
3075 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
3076 add_extra_method_with_depth (acfg, m, depth);
3080 g_free (name_prefix);
3083 /* Add an instance of GenericComparer<T> which is created dynamically by Comparer<T> */
3084 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "Comparer`1")) {
3085 MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
3086 MonoClass *icomparable, *gcomparer;
3087 MonoGenericContext ctx;
3088 MonoType *args [16];
3090 memset (&ctx, 0, sizeof (ctx));
3092 icomparable = mono_class_from_name (mono_defaults.corlib, "System", "IComparable`1");
3093 g_assert (icomparable);
3094 args [0] = &tclass->byval_arg;
3095 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
3097 if (mono_class_is_assignable_from (mono_class_inflate_generic_class (icomparable, &ctx), tclass)) {
3098 gcomparer = mono_class_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericComparer`1");
3099 g_assert (gcomparer);
3100 add_generic_class (acfg, mono_class_inflate_generic_class (gcomparer, &ctx), FALSE);
3104 /* Add an instance of GenericEqualityComparer<T> which is created dynamically by EqualityComparer<T> */
3105 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") && !strcmp (klass->name, "EqualityComparer`1")) {
3106 MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
3107 MonoClass *iface, *gcomparer;
3108 MonoGenericContext ctx;
3109 MonoType *args [16];
3111 memset (&ctx, 0, sizeof (ctx));
3113 iface = mono_class_from_name (mono_defaults.corlib, "System", "IEquatable`1");
3115 args [0] = &tclass->byval_arg;
3116 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
3118 if (mono_class_is_assignable_from (mono_class_inflate_generic_class (iface, &ctx), tclass)) {
3119 gcomparer = mono_class_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericEqualityComparer`1");
3120 g_assert (gcomparer);
3121 add_generic_class (acfg, mono_class_inflate_generic_class (gcomparer, &ctx), FALSE);
3127 add_instances_of (MonoAotCompile *acfg, MonoClass *klass, MonoType **insts, int ninsts, gboolean force)
3130 MonoGenericContext ctx;
3131 MonoType *args [16];
3133 memset (&ctx, 0, sizeof (ctx));
3135 for (i = 0; i < ninsts; ++i) {
3136 args [0] = insts [i];
3137 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
3138 add_generic_class (acfg, mono_class_inflate_generic_class (klass, &ctx), force);
3143 * add_generic_instances:
3145 * Add instances referenced by the METHODSPEC/TYPESPEC table.
3148 add_generic_instances (MonoAotCompile *acfg)
3153 MonoMethodHeader *header;
3154 MonoMethodSignature *sig;
3155 MonoGenericContext *context;
3157 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHODSPEC].rows; ++i) {
3158 token = MONO_TOKEN_METHOD_SPEC | (i + 1);
3159 method = mono_get_method (acfg->image, token, NULL);
3164 if (method->klass->image != acfg->image)
3167 context = mono_method_get_context (method);
3169 if (context && ((context->class_inst && context->class_inst->is_open)))
3173 * For open methods, create an instantiation which can be passed to the JIT.
3174 * FIXME: Handle class_inst as well.
3176 if (context && context->method_inst && context->method_inst->is_open) {
3177 MonoGenericContext shared_context;
3178 MonoGenericInst *inst;
3179 MonoType **type_argv;
3181 MonoMethod *declaring_method;
3182 gboolean supported = TRUE;
3184 /* Check that the context doesn't contain open constructed types */
3185 if (context->class_inst) {
3186 inst = context->class_inst;
3187 for (i = 0; i < inst->type_argc; ++i) {
3188 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)
3190 if (mono_class_is_open_constructed_type (inst->type_argv [i]))
3194 if (context->method_inst) {
3195 inst = context->method_inst;
3196 for (i = 0; i < inst->type_argc; ++i) {
3197 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)
3199 if (mono_class_is_open_constructed_type (inst->type_argv [i]))
3207 memset (&shared_context, 0, sizeof (MonoGenericContext));
3209 inst = context->class_inst;
3211 type_argv = g_new0 (MonoType*, inst->type_argc);
3212 for (i = 0; i < inst->type_argc; ++i) {
3213 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)
3214 type_argv [i] = &mono_defaults.object_class->byval_arg;
3216 type_argv [i] = inst->type_argv [i];
3219 shared_context.class_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
3223 inst = context->method_inst;
3225 type_argv = g_new0 (MonoType*, inst->type_argc);
3226 for (i = 0; i < inst->type_argc; ++i) {
3227 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)
3228 type_argv [i] = &mono_defaults.object_class->byval_arg;
3230 type_argv [i] = inst->type_argv [i];
3233 shared_context.method_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
3237 if (method->is_generic || method->klass->generic_container)
3238 declaring_method = method;
3240 declaring_method = mono_method_get_declaring_generic_method (method);
3242 method = mono_class_inflate_generic_method (declaring_method, &shared_context);
3246 * If the method is fully sharable, it was already added in place of its
3247 * generic definition.
3249 if (mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE))
3253 * FIXME: Partially shared methods are not shared here, so we end up with
3254 * many identical methods.
3256 add_extra_method (acfg, method);
3259 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
3262 token = MONO_TOKEN_TYPE_SPEC | (i + 1);
3264 klass = mono_class_get (acfg->image, token);
3265 if (!klass || klass->rank) {
3266 mono_loader_clear_error ();
3270 add_generic_class (acfg, klass, FALSE);
3273 /* Add types of args/locals */
3274 for (i = 0; i < acfg->methods->len; ++i) {
3277 method = g_ptr_array_index (acfg->methods, i);
3279 depth = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_depth, method));
3281 sig = mono_method_signature (method);
3284 for (j = 0; j < sig->param_count; ++j)
3285 if (sig->params [j]->type == MONO_TYPE_GENERICINST)
3286 add_generic_class_with_depth (acfg, mono_class_from_mono_type (sig->params [j]), depth + 1);
3289 header = mono_method_get_header (method);
3292 for (j = 0; j < header->num_locals; ++j)
3293 if (header->locals [j]->type == MONO_TYPE_GENERICINST)
3294 add_generic_class_with_depth (acfg, mono_class_from_mono_type (header->locals [j]), depth + 1);
3298 if (acfg->image == mono_defaults.corlib) {
3300 MonoType *insts [256];
3303 insts [ninsts ++] = &mono_defaults.byte_class->byval_arg;
3304 insts [ninsts ++] = &mono_defaults.sbyte_class->byval_arg;
3305 insts [ninsts ++] = &mono_defaults.int16_class->byval_arg;
3306 insts [ninsts ++] = &mono_defaults.uint16_class->byval_arg;
3307 insts [ninsts ++] = &mono_defaults.int32_class->byval_arg;
3308 insts [ninsts ++] = &mono_defaults.uint32_class->byval_arg;
3309 insts [ninsts ++] = &mono_defaults.int64_class->byval_arg;
3310 insts [ninsts ++] = &mono_defaults.uint64_class->byval_arg;
3311 insts [ninsts ++] = &mono_defaults.single_class->byval_arg;
3312 insts [ninsts ++] = &mono_defaults.double_class->byval_arg;
3313 insts [ninsts ++] = &mono_defaults.char_class->byval_arg;
3314 insts [ninsts ++] = &mono_defaults.boolean_class->byval_arg;
3316 /* Add GenericComparer<T> instances for primitive types for Enum.ToString () */
3317 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "GenericComparer`1");
3319 add_instances_of (acfg, klass, insts, ninsts, TRUE);
3320 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "GenericEqualityComparer`1");
3322 add_instances_of (acfg, klass, insts, ninsts, TRUE);
3324 /* Add instances of the array generic interfaces for primitive types */
3325 /* This will add instances of the InternalArray_ helper methods in Array too */
3326 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "ICollection`1");
3328 add_instances_of (acfg, klass, insts, ninsts, TRUE);
3329 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "IList`1");
3331 add_instances_of (acfg, klass, insts, ninsts, TRUE);
3332 klass = mono_class_from_name (acfg->image, "System.Collections.Generic", "IEnumerable`1");
3334 add_instances_of (acfg, klass, insts, ninsts, TRUE);
3337 * Add a managed-to-native wrapper of Array.GetGenericValueImpl<object>, which is
3338 * used for all instances of GetGenericValueImpl by the AOT runtime.
3341 MonoGenericContext ctx;
3342 MonoType *args [16];
3343 MonoMethod *get_method;
3344 MonoClass *array_klass = mono_array_class_get (mono_defaults.object_class, 1)->parent;
3346 get_method = mono_class_get_method_from_name (array_klass, "GetGenericValueImpl", 2);
3349 memset (&ctx, 0, sizeof (ctx));
3350 args [0] = &mono_defaults.object_class->byval_arg;
3351 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
3352 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (get_method, &ctx), TRUE, TRUE));
3356 /* Same for CompareExchange<T>/Exchange<T> */
3358 MonoGenericContext ctx;
3359 MonoType *args [16];
3361 MonoClass *interlocked_klass = mono_class_from_name (mono_defaults.corlib, "System.Threading", "Interlocked");
3362 gpointer iter = NULL;
3364 while ((m = mono_class_get_methods (interlocked_klass, &iter))) {
3365 if ((!strcmp (m->name, "CompareExchange") || !strcmp (m->name, "Exchange")) && m->is_generic) {
3366 memset (&ctx, 0, sizeof (ctx));
3367 args [0] = &mono_defaults.object_class->byval_arg;
3368 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
3369 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method (m, &ctx), TRUE, TRUE));
3377 * is_direct_callable:
3379 * Return whenever the method identified by JI is directly callable without
3380 * going through the PLT.
3383 is_direct_callable (MonoAotCompile *acfg, MonoMethod *method, MonoJumpInfo *patch_info)
3385 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
3386 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
3388 gboolean direct_callable = TRUE;
3390 if (direct_callable && !(!callee_cfg->has_got_slots && (callee_cfg->method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
3391 direct_callable = FALSE;
3392 if ((callee_cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) && (!method || method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED))
3393 // FIXME: Maybe call the wrapper directly ?
3394 direct_callable = FALSE;
3396 if (direct_callable)
3405 * emit_and_reloc_code:
3407 * Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
3408 * is true, calls are made through the GOT too. This is used for emitting trampolines
3409 * in full-aot mode, since calls made from trampolines couldn't go through the PLT,
3410 * since trampolines are needed to make PTL work.
3413 emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, guint32 code_len, MonoJumpInfo *relocs, gboolean got_only)
3415 int i, pindex, start_index, method_index;
3417 MonoJumpInfo *patch_info;
3418 MonoMethodHeader *header;
3419 gboolean skip, direct_call;
3421 char direct_call_target [1024];
3424 header = mono_method_get_header (method);
3426 method_index = get_method_index (acfg, method);
3429 /* Collect and sort relocations */
3430 patches = g_ptr_array_new ();
3431 for (patch_info = relocs; patch_info; patch_info = patch_info->next)
3432 g_ptr_array_add (patches, patch_info);
3433 g_ptr_array_sort (patches, compare_patches);
3436 for (i = 0; i < code_len; i++) {
3438 for (pindex = start_index; pindex < patches->len; ++pindex) {
3439 patch_info = g_ptr_array_index (patches, pindex);
3440 if (patch_info->ip.i >= i)
3444 #ifdef MONO_ARCH_AOT_SUPPORTED
3446 if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
3447 start_index = pindex;
3449 switch (patch_info->type) {
3450 case MONO_PATCH_INFO_NONE:
3452 case MONO_PATCH_INFO_GOT_OFFSET: {
3455 arch_emit_got_offset (acfg, code + i, &code_size);
3458 patch_info->type = MONO_PATCH_INFO_NONE;
3463 * If this patch is a call, try emitting a direct call instead of
3464 * through a PLT entry. This is possible if the called method is in
3465 * the same assembly and requires no initialization.
3467 direct_call = FALSE;
3468 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
3469 if (!got_only && is_direct_callable (acfg, method, patch_info)) {
3470 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
3471 //printf ("DIRECT: %s %s\n", method ? mono_method_full_name (method, TRUE) : "", mono_method_full_name (callee_cfg->method, TRUE));
3473 g_assert (strlen (callee_cfg->asm_symbol) < 1000);
3474 sprintf (direct_call_target, "%s", callee_cfg->asm_symbol);
3475 patch_info->type = MONO_PATCH_INFO_NONE;
3476 acfg->stats.direct_calls ++;
3479 acfg->stats.all_calls ++;
3482 if (!got_only && !direct_call) {
3483 MonoPltEntry *plt_entry = get_plt_entry (acfg, patch_info);
3485 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
3487 sprintf (direct_call_target, "%s", plt_entry->symbol);
3489 /* Nullify the patch */
3490 patch_info->type = MONO_PATCH_INFO_NONE;
3491 plt_entry->jit_used = TRUE;
3498 arch_emit_direct_call (acfg, direct_call_target, &call_size);
3503 got_slot = get_got_offset (acfg, patch_info);
3505 arch_emit_got_access (acfg, code + i, got_slot, &code_size);
3512 #endif /* MONO_ARCH_AOT_SUPPORTED */
3515 /* Find next patch */
3517 for (pindex = start_index; pindex < patches->len; ++pindex) {
3518 patch_info = g_ptr_array_index (patches, pindex);
3519 if (patch_info->ip.i >= i)
3523 /* Try to emit multiple bytes at once */
3524 if (pindex < patches->len && patch_info->ip.i > i) {
3525 emit_bytes (acfg, code + i, patch_info->ip.i - i);
3526 i = patch_info->ip.i - 1;
3528 emit_bytes (acfg, code + i, 1);
3537 * Modify SYMBOL so it only includes characters permissible in symbols.
3540 sanitize_symbol (char *symbol)
3542 int i, len = strlen (symbol);
3544 for (i = 0; i < len; ++i)
3545 if (!isalnum (symbol [i]) && (symbol [i] != '_'))
3550 get_debug_sym (MonoMethod *method, const char *prefix, GHashTable *cache)
3552 char *name1, *name2, *cached;
3553 int i, j, len, count;
3555 name1 = mono_method_full_name (method, TRUE);
3556 len = strlen (name1);
3557 name2 = malloc (strlen (prefix) + len + 16);
3558 memcpy (name2, prefix, strlen (prefix));
3559 j = strlen (prefix);
3560 for (i = 0; i < len; ++i) {
3561 if (isalnum (name1 [i])) {
3562 name2 [j ++] = name1 [i];
3563 } else if (name1 [i] == ' ' && name1 [i + 1] == '(' && name1 [i + 2] == ')') {
3565 } else if (name1 [i] == ',' && name1 [i + 1] == ' ') {
3568 } else if (name1 [i] == '(' || name1 [i] == ')' || name1 [i] == '>') {
3577 while (g_hash_table_lookup (cache, name2)) {
3578 sprintf (name2 + j, "_%d", count);
3582 cached = g_strdup (name2);
3583 g_hash_table_insert (cache, cached, cached);
3589 emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
3594 char *debug_sym = NULL;
3596 int func_alignment = AOT_FUNC_ALIGNMENT;
3597 MonoMethodHeader *header;
3600 method = cfg->orig_method;
3601 code = cfg->native_code;
3602 header = cfg->header;
3604 method_index = get_method_index (acfg, method);
3606 /* Make the labels local */
3607 sprintf (symbol, "%s", cfg->asm_symbol);
3609 emit_section_change (acfg, ".text", 0);
3610 emit_alignment (acfg, func_alignment);
3611 emit_label (acfg, symbol);
3613 if (acfg->aot_opts.write_symbols) {
3615 * Write a C style symbol for every method, this has two uses:
3616 * - it works on platforms where the dwarf debugging info is not
3618 * - it allows the setting of breakpoints of aot-ed methods.
3620 debug_sym = get_debug_sym (method, "", acfg->method_label_hash);
3622 sprintf (symbol, "%sme_%x", acfg->temp_prefix, method_index);
3623 if (acfg->need_no_dead_strip)
3624 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
3625 emit_local_symbol (acfg, debug_sym, symbol, TRUE);
3626 emit_label (acfg, debug_sym);
3629 export_name = g_hash_table_lookup (acfg->export_names, method);
3631 /* Emit a global symbol for the method */
3632 emit_global_inner (acfg, export_name, TRUE);
3633 emit_label (acfg, export_name);
3636 if (cfg->verbose_level > 0)
3637 g_print ("Method %s emitted as %s\n", mono_method_full_name (method, TRUE), symbol);
3639 acfg->stats.code_size += cfg->code_len;
3641 acfg->cfgs [method_index]->got_offset = acfg->got_offset;
3643 emit_and_reloc_code (acfg, method, code, cfg->code_len, cfg->patch_info, FALSE);
3647 if (acfg->aot_opts.write_symbols) {
3648 emit_symbol_size (acfg, debug_sym, ".");
3652 sprintf (symbol, "%sme_%x", acfg->temp_prefix, method_index);
3653 emit_label (acfg, symbol);
3659 * Encode PATCH_INFO into its disk representation.
3662 encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf)
3666 switch (patch_info->type) {
3667 case MONO_PATCH_INFO_NONE:
3669 case MONO_PATCH_INFO_IMAGE:
3670 encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
3672 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
3673 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
3674 case MONO_PATCH_INFO_CASTCLASS_CACHE:
3676 case MONO_PATCH_INFO_METHOD_REL:
3677 encode_value ((gint)patch_info->data.offset, p, &p);
3679 case MONO_PATCH_INFO_SWITCH: {
3680 gpointer *table = (gpointer *)patch_info->data.table->table;
3683 encode_value (patch_info->data.table->table_size, p, &p);
3684 for (k = 0; k < patch_info->data.table->table_size; k++)
3685 encode_value ((int)(gssize)table [k], p, &p);
3688 case MONO_PATCH_INFO_METHODCONST:
3689 case MONO_PATCH_INFO_METHOD:
3690 case MONO_PATCH_INFO_METHOD_JUMP:
3691 case MONO_PATCH_INFO_ICALL_ADDR:
3692 case MONO_PATCH_INFO_METHOD_RGCTX:
3693 encode_method_ref (acfg, patch_info->data.method, p, &p);
3695 case MONO_PATCH_INFO_INTERNAL_METHOD:
3696 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
3697 guint32 len = strlen (patch_info->data.name);
3699 encode_value (len, p, &p);
3701 memcpy (p, patch_info->data.name, len);
3706 case MONO_PATCH_INFO_LDSTR: {
3707 guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
3708 guint32 token = patch_info->data.token->token;
3709 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
3710 encode_value (image_index, p, &p);
3711 encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
3714 case MONO_PATCH_INFO_RVA:
3715 case MONO_PATCH_INFO_DECLSEC:
3716 case MONO_PATCH_INFO_LDTOKEN:
3717 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3718 encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
3719 encode_value (patch_info->data.token->token, p, &p);
3720 encode_value (patch_info->data.token->has_context, p, &p);
3721 if (patch_info->data.token->has_context)
3722 encode_generic_context (acfg, &patch_info->data.token->context, p, &p);
3724 case MONO_PATCH_INFO_EXC_NAME: {
3725 MonoClass *ex_class;
3728 mono_class_from_name (mono_defaults.exception_class->image,
3729 "System", patch_info->data.target);
3730 g_assert (ex_class);
3731 encode_klass_ref (acfg, ex_class, p, &p);
3734 case MONO_PATCH_INFO_R4:
3735 encode_value (*((guint32 *)patch_info->data.target), p, &p);
3737 case MONO_PATCH_INFO_R8:
3738 encode_value (((guint32 *)patch_info->data.target) [MINI_LS_WORD_IDX], p, &p);
3739 encode_value (((guint32 *)patch_info->data.target) [MINI_MS_WORD_IDX], p, &p);
3741 case MONO_PATCH_INFO_VTABLE:
3742 case MONO_PATCH_INFO_CLASS:
3743 case MONO_PATCH_INFO_IID:
3744 case MONO_PATCH_INFO_ADJUSTED_IID:
3745 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
3747 case MONO_PATCH_INFO_CLASS_INIT:
3748 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
3749 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
3751 case MONO_PATCH_INFO_FIELD:
3752 case MONO_PATCH_INFO_SFLDA:
3753 encode_field_info (acfg, patch_info->data.field, p, &p);
3755 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
3757 case MONO_PATCH_INFO_RGCTX_FETCH: {
3758 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
3760 encode_method_ref (acfg, entry->method, p, &p);
3761 encode_value (entry->in_mrgctx, p, &p);
3762 encode_value (entry->info_type, p, &p);
3763 encode_value (entry->data->type, p, &p);
3764 encode_patch (acfg, entry->data, p, &p);
3767 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
3768 case MONO_PATCH_INFO_MONITOR_ENTER:
3769 case MONO_PATCH_INFO_MONITOR_EXIT:
3770 case MONO_PATCH_INFO_SEQ_POINT_INFO:
3772 case MONO_PATCH_INFO_LLVM_IMT_TRAMPOLINE:
3773 encode_method_ref (acfg, patch_info->data.imt_tramp->method, p, &p);
3774 encode_value (patch_info->data.imt_tramp->vt_offset, p, &p);
3777 g_warning ("unable to handle jump info %d", patch_info->type);
3778 g_assert_not_reached ();
3785 encode_patch_list (MonoAotCompile *acfg, GPtrArray *patches, int n_patches, int first_got_offset, guint8 *buf, guint8 **endbuf)
3788 guint32 pindex, offset;
3789 MonoJumpInfo *patch_info;
3791 encode_value (n_patches, p, &p);
3793 for (pindex = 0; pindex < patches->len; ++pindex) {
3794 patch_info = g_ptr_array_index (patches, pindex);
3796 if (patch_info->type == MONO_PATCH_INFO_NONE || patch_info->type == MONO_PATCH_INFO_BB)
3800 offset = get_got_offset (acfg, patch_info);
3801 encode_value (offset, p, &p);
3808 emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
3812 int pindex, buf_size, n_patches;
3814 MonoJumpInfo *patch_info;
3815 MonoMethodHeader *header;
3816 guint32 method_index;
3818 guint32 first_got_offset;
3820 method = cfg->orig_method;
3821 header = mono_method_get_header (method);
3823 method_index = get_method_index (acfg, method);
3825 /* Sort relocations */
3826 patches = g_ptr_array_new ();
3827 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
3828 g_ptr_array_add (patches, patch_info);
3829 g_ptr_array_sort (patches, compare_patches);
3831 first_got_offset = acfg->cfgs [method_index]->got_offset;
3833 /**********************/
3834 /* Encode method info */
3835 /**********************/
3837 buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
3838 p = buf = g_malloc (buf_size);
3840 if (mono_class_get_cctor (method->klass))
3841 encode_klass_ref (acfg, method->klass, p, &p);
3843 /* Not needed when loading the method */
3844 encode_value (0, p, &p);
3847 if (cfg->opt & MONO_OPT_SHARED) {
3848 encode_value (g_list_length (cfg->ldstr_list), p, &p);
3849 for (l = cfg->ldstr_list; l; l = l->next) {
3850 encode_value ((long)l->data, p, &p);
3854 /* Used only in shared mode */
3855 g_assert (!cfg->ldstr_list);
3858 for (pindex = 0; pindex < patches->len; ++pindex) {
3859 patch_info = g_ptr_array_index (patches, pindex);
3861 if ((patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
3862 (patch_info->type == MONO_PATCH_INFO_NONE)) {
3863 patch_info->type = MONO_PATCH_INFO_NONE;
3868 if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
3869 /* Stored in a GOT slot initialized at module load time */
3870 patch_info->type = MONO_PATCH_INFO_NONE;
3874 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR) {
3875 /* Stored in a GOT slot initialized at module load time */
3876 patch_info->type = MONO_PATCH_INFO_NONE;
3880 if (is_plt_patch (patch_info)) {
3881 /* Calls are made through the PLT */
3882 patch_info->type = MONO_PATCH_INFO_NONE;
3890 g_assert (cfg->has_got_slots);
3892 encode_patch_list (acfg, patches, n_patches, first_got_offset, p, &p);
3894 acfg->stats.info_size += p - buf;
3896 g_assert (p - buf < buf_size);
3898 cfg->method_info_offset = add_to_blob (acfg, buf, p - buf);
3903 get_unwind_info_offset (MonoAotCompile *acfg, guint8 *encoded, guint32 encoded_len)
3905 guint32 cache_index;
3908 /* Reuse the unwind module to canonize and store unwind info entries */
3909 cache_index = mono_cache_unwind_info (encoded, encoded_len);
3911 /* Use +/- 1 to distinguish 0s from missing entries */
3912 offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1)));
3920 * It would be easier to use assembler symbols, but the caller needs an
3923 offset = acfg->unwind_info_offset;
3924 g_hash_table_insert (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1), GUINT_TO_POINTER (offset + 1));
3925 g_ptr_array_add (acfg->unwind_ops, GUINT_TO_POINTER (cache_index));
3928 encode_value (encoded_len, p, &p);
3930 acfg->unwind_info_offset += encoded_len + (p - buf);
3936 emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
3939 int i, k, buf_size, method_index;
3940 guint32 debug_info_size;
3942 MonoMethodHeader *header;
3943 guint8 *p, *buf, *debug_info;
3944 MonoJitInfo *jinfo = cfg->jit_info;
3946 gboolean use_unwind_ops = FALSE;
3947 MonoSeqPointInfo *seq_points;
3949 method = cfg->orig_method;
3950 code = cfg->native_code;
3951 header = cfg->header;
3953 method_index = get_method_index (acfg, method);
3955 if (!acfg->aot_opts.nodebug) {
3956 mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
3959 debug_info_size = 0;
3962 seq_points = cfg->seq_point_info;
3964 buf_size = header->num_clauses * 256 + debug_info_size + 2048 + (seq_points ? (seq_points->len * 64) : 0) + cfg->gc_map_size;
3965 p = buf = g_malloc (buf_size);
3967 #ifdef MONO_ARCH_HAVE_XP_UNWIND
3968 use_unwind_ops = cfg->unwind_ops != NULL;
3971 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);
3973 encode_value (flags, p, &p);
3975 if (use_unwind_ops) {
3976 guint32 encoded_len;
3980 * This is a duplicate of the data in the .debug_frame section, but that
3981 * section cannot be accessed using the dl interface.
3983 encoded = mono_unwind_ops_encode (cfg->unwind_ops, &encoded_len);
3984 encode_value (get_unwind_info_offset (acfg, encoded, encoded_len), p, &p);
3987 encode_value (jinfo->used_regs, p, &p);
3990 /*Encode the number of holes before the number of clauses to make decoding easier*/
3991 if (jinfo->has_try_block_holes) {
3992 MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
3993 encode_value (table->num_holes, p, &p);
3996 /* Exception table */
3997 if (cfg->compile_llvm) {
3999 * When using LLVM, we can't emit some data, like pc offsets, this reg/offset etc.,
4000 * since the information is only available to llc. Instead, we let llc save the data
4001 * into the LSDA, and read it from there at runtime.
4003 /* The assembly might be CIL stripped so emit the data ourselves */
4004 if (header->num_clauses)
4005 encode_value (header->num_clauses, p, &p);
4007 for (k = 0; k < header->num_clauses; ++k) {
4008 MonoExceptionClause *clause;
4010 clause = &header->clauses [k];
4012 encode_value (clause->flags, p, &p);
4013 if (clause->data.catch_class) {
4014 encode_value (1, p, &p);
4015 encode_klass_ref (acfg, clause->data.catch_class, p, &p);
4017 encode_value (0, p, &p);
4020 /* Emit a list of nesting clauses */
4021 for (i = 0; i < header->num_clauses; ++i) {
4023 MonoExceptionClause *clause1 = &header->clauses [cindex1];
4025 MonoExceptionClause *clause2 = &header->clauses [cindex2];
4027 if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset)
4028 encode_value (i, p, &p);
4030 encode_value (-1, p, &p);
4033 if (jinfo->num_clauses)
4034 encode_value (jinfo->num_clauses, p, &p);
4036 for (k = 0; k < jinfo->num_clauses; ++k) {
4037 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
4039 encode_value (ei->flags, p, &p);
4040 encode_value (ei->exvar_offset, p, &p);
4042 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
4043 encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
4045 if (ei->data.catch_class) {
4046 encode_value (1, p, &p);
4047 encode_klass_ref (acfg, ei->data.catch_class, p, &p);
4049 encode_value (0, p, &p);
4053 encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
4054 encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
4055 encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
4059 if (jinfo->has_generic_jit_info) {
4060 MonoGenericJitInfo *gi = mono_jit_info_get_generic_jit_info (jinfo);
4062 if (!cfg->compile_llvm) {
4063 encode_value (gi->has_this ? 1 : 0, p, &p);
4064 encode_value (gi->this_reg, p, &p);
4065 encode_value (gi->this_offset, p, &p);
4069 * Need to encode jinfo->method too, since it is not equal to 'method'
4070 * when using generic sharing.
4072 encode_method_ref (acfg, jinfo->method, p, &p);
4075 if (jinfo->has_try_block_holes) {
4076 MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
4077 for (i = 0; i < table->num_holes; ++i) {
4078 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
4079 encode_value (hole->clause, p, &p);
4080 encode_value (hole->length, p, &p);
4081 encode_value (hole->offset, p, &p);
4086 int il_offset, native_offset, last_il_offset, last_native_offset, j;
4088 encode_value (seq_points->len, p, &p);
4089 last_il_offset = last_native_offset = 0;
4090 for (i = 0; i < seq_points->len; ++i) {
4091 SeqPoint *sp = &seq_points->seq_points [i];
4092 il_offset = sp->il_offset;
4093 native_offset = sp->native_offset;
4094 encode_value (il_offset - last_il_offset, p, &p);
4095 encode_value (native_offset - last_native_offset, p, &p);
4096 last_il_offset = il_offset;
4097 last_native_offset = native_offset;
4099 encode_value (sp->next_len, p, &p);
4100 for (j = 0; j < sp->next_len; ++j)
4101 encode_value (sp->next [j], p, &p);
4105 g_assert (debug_info_size < buf_size);
4107 encode_value (debug_info_size, p, &p);
4108 if (debug_info_size) {
4109 memcpy (p, debug_info, debug_info_size);
4110 p += debug_info_size;
4111 g_free (debug_info);
4116 encode_value (cfg->gc_map_size, p, &p);
4117 /* The GC map requires 4 bytes of alignment */
4118 while ((gsize)p % 4)
4120 memcpy (p, cfg->gc_map, cfg->gc_map_size);
4121 p += cfg->gc_map_size;
4124 acfg->stats.ex_info_size += p - buf;
4126 g_assert (p - buf < buf_size);
4129 /* The GC Map requires 4 byte alignment */
4130 cfg->ex_info_offset = add_to_blob_aligned (acfg, buf, p - buf, cfg->gc_map ? 4 : 1);
4135 emit_klass_info (MonoAotCompile *acfg, guint32 token)
4137 MonoClass *klass = mono_class_get (acfg->image, token);
4139 int i, buf_size, res;
4140 gboolean no_special_static, cant_encode;
4141 gpointer iter = NULL;
4144 mono_loader_clear_error ();
4148 p = buf = g_malloc (buf_size);
4150 /* Mark as unusable */
4151 encode_value (-1, p, &p);
4153 res = add_to_blob (acfg, buf, p - buf);
4159 buf_size = 10240 + (klass->vtable_size * 16);
4160 p = buf = g_malloc (buf_size);
4164 mono_class_init (klass);
4166 mono_class_get_nested_types (klass, &iter);
4167 g_assert (klass->nested_classes_inited);
4169 mono_class_setup_vtable (klass);
4172 * Emit all the information which is required for creating vtables so
4173 * the runtime does not need to create the MonoMethod structures which
4174 * take up a lot of space.
4177 no_special_static = !mono_class_has_special_static_fields (klass);
4179 /* Check whenever we have enough info to encode the vtable */
4180 cant_encode = FALSE;
4181 for (i = 0; i < klass->vtable_size; ++i) {
4182 MonoMethod *cm = klass->vtable [i];
4184 if (cm && mono_method_signature (cm)->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, cm))
4188 if (klass->generic_container || cant_encode) {
4189 encode_value (-1, p, &p);
4191 encode_value (klass->vtable_size, p, &p);
4192 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);
4193 if (klass->has_cctor)
4194 encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
4195 if (klass->has_finalize)
4196 encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
4198 encode_value (klass->instance_size, p, &p);
4199 encode_value (mono_class_data_size (klass), p, &p);
4200 encode_value (klass->packing_size, p, &p);
4201 encode_value (klass->min_align, p, &p);
4203 for (i = 0; i < klass->vtable_size; ++i) {
4204 MonoMethod *cm = klass->vtable [i];
4207 encode_method_ref (acfg, cm, p, &p);
4209 encode_value (0, p, &p);
4213 acfg->stats.class_info_size += p - buf;
4215 g_assert (p - buf < buf_size);
4216 res = add_to_blob (acfg, buf, p - buf);
4223 get_plt_entry_debug_sym (MonoAotCompile *acfg, MonoJumpInfo *ji, GHashTable *cache)
4225 char *debug_sym = NULL;
4228 case MONO_PATCH_INFO_METHOD:
4229 debug_sym = get_debug_sym (ji->data.method, "plt_", cache);
4231 case MONO_PATCH_INFO_INTERNAL_METHOD:
4232 debug_sym = g_strdup_printf ("plt__jit_icall_%s", ji->data.name);
4234 case MONO_PATCH_INFO_CLASS_INIT:
4235 debug_sym = g_strdup_printf ("plt__class_init_%s", mono_type_get_name (&ji->data.klass->byval_arg));
4236 sanitize_symbol (debug_sym);
4238 case MONO_PATCH_INFO_RGCTX_FETCH:
4239 debug_sym = g_strdup_printf ("plt__rgctx_fetch_%d", acfg->label_generator ++);
4241 case MONO_PATCH_INFO_ICALL_ADDR: {
4242 char *s = get_debug_sym (ji->data.method, "", cache);
4244 debug_sym = g_strdup_printf ("plt__icall_native_%s", s);
4248 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
4249 debug_sym = g_strdup_printf ("plt__jit_icall_native_%s", ji->data.name);
4251 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
4252 debug_sym = g_strdup_printf ("plt__generic_class_init");
4262 * Calls made from AOTed code are routed through a table of jumps similar to the
4263 * ELF PLT (Program Linkage Table). Initially the PLT entries jump to code which transfers
4264 * control to the AOT runtime through a trampoline.
4267 emit_plt (MonoAotCompile *acfg)
4273 cache = g_hash_table_new (g_str_hash, g_str_equal);
4276 sprintf (symbol, "plt");
4278 emit_section_change (acfg, ".text", 0);
4279 emit_alignment (acfg, NACL_SIZE(16, kNaClAlignment));
4280 emit_label (acfg, symbol);
4281 emit_label (acfg, acfg->plt_symbol);
4283 for (i = 0; i < acfg->plt_offset; ++i) {
4284 char *debug_sym = NULL;
4285 MonoPltEntry *plt_entry = NULL;
4290 * The first plt entry is unused.
4294 plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
4299 * If the target is directly callable, alias the plt symbol to point to
4301 * FIXME: Use this to simplify emit_and_reloc_code ().
4302 * FIXME: Avoid the got slot.
4303 * FIXME: Add support to the binary writer.
4305 if (ji && is_direct_callable (acfg, NULL, ji) && !acfg->use_bin_writer) {
4306 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, ji->data.method);
4308 if (acfg->thumb_mixed && !callee_cfg->compile_llvm) {
4309 /* LLVM calls the PLT entries using bl, so emit a stub */
4310 fprintf (acfg->fp, "\n.thumb_func\n");
4311 emit_label (acfg, plt_entry->llvm_symbol);
4312 fprintf (acfg->fp, "bx pc\n");
4313 fprintf (acfg->fp, "nop\n");
4314 fprintf (acfg->fp, ".arm\n");
4315 fprintf (acfg->fp, "b %s\n", callee_cfg->asm_symbol);
4317 fprintf (acfg->fp, "\n.set %s, %s\n", plt_entry->llvm_symbol, callee_cfg->asm_symbol);
4323 if (acfg->aot_opts.write_symbols)
4324 plt_entry->debug_sym = get_plt_entry_debug_sym (acfg, ji, cache);
4325 debug_sym = plt_entry->debug_sym;
4327 if (acfg->thumb_mixed && !plt_entry->jit_used)
4328 /* Emit only a thumb version */
4331 if (!acfg->thumb_mixed)
4332 emit_label (acfg, plt_entry->llvm_symbol);
4335 if (acfg->need_no_dead_strip)
4336 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
4337 emit_local_symbol (acfg, debug_sym, NULL, TRUE);
4338 emit_label (acfg, debug_sym);
4341 emit_label (acfg, plt_entry->symbol);
4343 arch_emit_plt_entry (acfg, i);
4346 emit_symbol_size (acfg, debug_sym, ".");
4349 if (acfg->thumb_mixed) {
4350 /* Make sure the ARM symbols don't alias the thumb ones */
4351 emit_zero_bytes (acfg, 16);
4354 * Emit a separate set of PLT entries using thumb2 which is called by LLVM generated
4357 for (i = 0; i < acfg->plt_offset; ++i) {
4358 char *debug_sym = NULL;
4359 MonoPltEntry *plt_entry = NULL;
4365 plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
4368 if (ji && is_direct_callable (acfg, NULL, ji) && !acfg->use_bin_writer)
4371 /* Skip plt entries not actually called by LLVM code */
4372 if (!plt_entry->llvm_used)
4375 if (acfg->aot_opts.write_symbols) {
4376 if (plt_entry->debug_sym)
4377 debug_sym = g_strdup_printf ("%s_thumb", plt_entry->debug_sym);
4381 #if defined(__APPLE__)
4382 fprintf (acfg->fp, " .thumb_func %s\n", debug_sym);
4383 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
4385 emit_local_symbol (acfg, debug_sym, NULL, TRUE);
4386 emit_label (acfg, debug_sym);
4388 fprintf (acfg->fp, "\n.thumb_func\n");
4390 emit_label (acfg, plt_entry->llvm_symbol);
4392 arch_emit_llvm_plt_entry (acfg, i);
4395 emit_symbol_size (acfg, debug_sym, ".");
4401 emit_symbol_size (acfg, acfg->plt_symbol, ".");
4403 sprintf (symbol, "plt_end");
4404 emit_label (acfg, symbol);
4406 g_hash_table_destroy (cache);
4409 static G_GNUC_UNUSED void
4410 emit_trampoline (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info)
4412 char start_symbol [256];
4414 guint32 buf_size, info_offset;
4415 MonoJumpInfo *patch_info;
4426 code_size = info->code_size;
4428 unwind_ops = info->unwind_ops;
4430 #ifdef __native_client_codegen__
4431 mono_nacl_fix_patches (code, ji);
4436 sprintf (start_symbol, "%s", name);
4438 emit_section_change (acfg, ".text", 0);
4439 emit_global (acfg, start_symbol, TRUE);
4440 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
4441 emit_label (acfg, start_symbol);
4443 sprintf (symbol, "%snamed_%s", acfg->temp_prefix, name);
4444 emit_label (acfg, symbol);
4447 * The code should access everything through the GOT, so we pass
4450 emit_and_reloc_code (acfg, NULL, code, code_size, ji, TRUE);
4452 emit_symbol_size (acfg, start_symbol, ".");
4456 /* Sort relocations */
4457 patches = g_ptr_array_new ();
4458 for (patch_info = ji; patch_info; patch_info = patch_info->next)
4459 if (patch_info->type != MONO_PATCH_INFO_NONE)
4460 g_ptr_array_add (patches, patch_info);
4461 g_ptr_array_sort (patches, compare_patches);
4463 buf_size = patches->len * 128 + 128;
4464 buf = g_malloc (buf_size);
4467 encode_patch_list (acfg, patches, patches->len, got_offset, p, &p);
4468 g_assert (p - buf < buf_size);
4470 sprintf (symbol, "%s_p", name);
4472 info_offset = add_to_blob (acfg, buf, p - buf);
4474 emit_section_change (acfg, RODATA_SECT, 0);
4475 emit_global (acfg, symbol, FALSE);
4476 emit_label (acfg, symbol);
4478 emit_int32 (acfg, info_offset);
4480 /* Emit debug info */
4484 sprintf (symbol, "%s", name);
4485 sprintf (symbol2, "%snamed_%s", acfg->temp_prefix, name);
4488 mono_dwarf_writer_emit_trampoline (acfg->dwarf, symbol, symbol2, NULL, NULL, code_size, unwind_ops);
4493 emit_trampolines (MonoAotCompile *acfg)
4496 char end_symbol [256];
4497 int i, tramp_got_offset;
4498 MonoAotTrampoline ntype;
4499 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
4503 if (!acfg->aot_opts.full_aot)
4506 g_assert (acfg->image->assembly);
4508 /* Currently, we emit most trampolines into the mscorlib AOT image. */
4509 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
4510 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
4511 MonoTrampInfo *info;
4514 * Emit the generic trampolines.
4516 * We could save some code by treating the generic trampolines as a wrapper
4517 * method, but that approach has its own complexities, so we choose the simpler
4520 for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
4521 mono_arch_create_generic_trampoline (tramp_type, &info, TRUE);
4522 emit_trampoline (acfg, acfg->got_offset, info);
4525 mono_arch_get_nullified_class_init_trampoline (&info);
4526 emit_trampoline (acfg, acfg->got_offset, info);
4527 #if defined(MONO_ARCH_MONITOR_OBJECT_REG)
4528 mono_arch_create_monitor_enter_trampoline (&info, TRUE);
4529 emit_trampoline (acfg, acfg->got_offset, info);
4530 mono_arch_create_monitor_exit_trampoline (&info, TRUE);
4531 emit_trampoline (acfg, acfg->got_offset, info);
4534 mono_arch_create_generic_class_init_trampoline (&info, TRUE);
4535 emit_trampoline (acfg, acfg->got_offset, info);
4537 /* Emit the exception related code pieces */
4538 mono_arch_get_restore_context (&info, TRUE);
4539 emit_trampoline (acfg, acfg->got_offset, info);
4540 mono_arch_get_call_filter (&info, TRUE);
4541 emit_trampoline (acfg, acfg->got_offset, info);
4542 mono_arch_get_throw_exception (&info, TRUE);
4543 emit_trampoline (acfg, acfg->got_offset, info);
4544 mono_arch_get_rethrow_exception (&info, TRUE);
4545 emit_trampoline (acfg, acfg->got_offset, info);
4546 mono_arch_get_throw_corlib_exception (&info, TRUE);
4547 emit_trampoline (acfg, acfg->got_offset, info);
4549 #if defined(MONO_ARCH_HAVE_GET_TRAMPOLINES)
4551 GSList *l = mono_arch_get_trampolines (TRUE);
4554 MonoTrampInfo *info = l->data;
4556 emit_trampoline (acfg, acfg->got_offset, info);
4562 for (i = 0; i < 128; ++i) {
4565 offset = MONO_RGCTX_SLOT_MAKE_RGCTX (i);
4566 mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
4567 emit_trampoline (acfg, acfg->got_offset, info);
4569 offset = MONO_RGCTX_SLOT_MAKE_MRGCTX (i);
4570 mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
4571 emit_trampoline (acfg, acfg->got_offset, info);
4577 /* delegate_invoke_impl trampolines */
4578 l = mono_arch_get_delegate_invoke_impls ();
4580 MonoTrampInfo *info = l->data;
4582 emit_trampoline (acfg, acfg->got_offset, info);
4587 #endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
4589 /* Emit trampolines which are numerous */
4592 * These include the following:
4593 * - specific trampolines
4594 * - static rgctx invoke trampolines
4596 * These trampolines have the same code, they are parameterized by GOT
4598 * They are defined in this file, in the arch_... routines instead of
4599 * in tramp-<ARCH>.c, since it is easier to do it this way.
4603 * When running in aot-only mode, we can't create specific trampolines at
4604 * runtime, so we create a few, and save them in the AOT file.
4605 * Normal trampolines embed their argument as a literal inside the
4606 * trampoline code, we can't do that here, so instead we embed an offset
4607 * which needs to be added to the trampoline address to get the address of
4608 * the GOT slot which contains the argument value.
4609 * The generated trampolines jump to the generic trampolines using another
4610 * GOT slot, which will be setup by the AOT loader to point to the
4611 * generic trampoline code of the given type.
4615 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for
4619 emit_section_change (acfg, ".text", 0);
4621 tramp_got_offset = acfg->got_offset;
4623 for (ntype = 0; ntype < MONO_AOT_TRAMP_NUM; ++ntype) {
4625 case MONO_AOT_TRAMP_SPECIFIC:
4626 sprintf (symbol, "specific_trampolines");
4628 case MONO_AOT_TRAMP_STATIC_RGCTX:
4629 sprintf (symbol, "static_rgctx_trampolines");
4631 case MONO_AOT_TRAMP_IMT_THUNK:
4632 sprintf (symbol, "imt_thunks");
4635 g_assert_not_reached ();
4638 sprintf (end_symbol, "%s_e", symbol);
4640 if (acfg->aot_opts.write_symbols)
4641 emit_local_symbol (acfg, symbol, end_symbol, TRUE);
4643 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
4644 emit_label (acfg, symbol);
4646 acfg->trampoline_got_offset_base [ntype] = tramp_got_offset;
4648 for (i = 0; i < acfg->num_trampolines [ntype]; ++i) {
4652 case MONO_AOT_TRAMP_SPECIFIC:
4653 arch_emit_specific_trampoline (acfg, tramp_got_offset, &tramp_size);
4654 tramp_got_offset += 2;
4656 case MONO_AOT_TRAMP_STATIC_RGCTX:
4657 arch_emit_static_rgctx_trampoline (acfg, tramp_got_offset, &tramp_size);
4658 tramp_got_offset += 2;
4660 case MONO_AOT_TRAMP_IMT_THUNK:
4661 arch_emit_imt_thunk (acfg, tramp_got_offset, &tramp_size);
4662 tramp_got_offset += 1;
4665 g_assert_not_reached ();
4667 #ifdef __native_client_codegen__
4668 /* align to avoid 32-byte boundary crossings */
4669 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
4672 if (!acfg->trampoline_size [ntype]) {
4673 g_assert (tramp_size);
4674 acfg->trampoline_size [ntype] = tramp_size;
4678 emit_label (acfg, end_symbol);
4681 /* Reserve some entries at the end of the GOT for our use */
4682 acfg->num_trampoline_got_entries = tramp_got_offset - acfg->got_offset;
4685 acfg->got_offset += acfg->num_trampoline_got_entries;
4689 str_begins_with (const char *str1, const char *str2)
4691 int len = strlen (str2);
4692 return strncmp (str1, str2, len) == 0;
4696 mono_aot_readonly_field_override (MonoClassField *field)
4699 for (rdv = readonly_values; rdv; rdv = rdv->next) {
4700 char *p = rdv->name;
4702 len = strlen (field->parent->name_space);
4703 if (strncmp (p, field->parent->name_space, len))
4708 len = strlen (field->parent->name);
4709 if (strncmp (p, field->parent->name, len))
4714 if (strcmp (p, field->name))
4716 switch (rdv->type) {
4718 return &rdv->value.i1;
4720 return &rdv->value.i2;
4722 return &rdv->value.i4;
4731 add_readonly_value (MonoAotOptions *opts, const char *val)
4736 /* the format of val is:
4737 * namespace.typename.fieldname=type/value
4738 * type can be i1 for uint8/int8/boolean, i2 for uint16/int16/char, i4 for uint32/int32
4740 fval = strrchr (val, '/');
4742 fprintf (stderr, "AOT : invalid format for readonly field '%s', missing /.\n", val);
4745 tval = strrchr (val, '=');
4747 fprintf (stderr, "AOT : invalid format for readonly field '%s', missing =.\n", val);
4750 rdv = g_new0 (ReadOnlyValue, 1);
4751 rdv->name = g_malloc0 (tval - val + 1);
4752 memcpy (rdv->name, val, tval - val);
4755 if (strncmp (tval, "i1", 2) == 0) {
4756 rdv->value.i1 = atoi (fval);
4757 rdv->type = MONO_TYPE_I1;
4758 } else if (strncmp (tval, "i2", 2) == 0) {
4759 rdv->value.i2 = atoi (fval);
4760 rdv->type = MONO_TYPE_I2;
4761 } else if (strncmp (tval, "i4", 2) == 0) {
4762 rdv->value.i4 = atoi (fval);
4763 rdv->type = MONO_TYPE_I4;
4765 fprintf (stderr, "AOT : unsupported type for readonly field '%s'.\n", tval);
4768 rdv->next = readonly_values;
4769 readonly_values = rdv;
4773 mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
4775 gchar **args, **ptr;
4777 args = g_strsplit (aot_options ? aot_options : "", ",", -1);
4778 for (ptr = args; ptr && *ptr; ptr ++) {
4779 const char *arg = *ptr;
4781 if (str_begins_with (arg, "outfile=")) {
4782 opts->outfile = g_strdup (arg + strlen ("outfile="));
4783 } else if (str_begins_with (arg, "save-temps")) {
4784 opts->save_temps = TRUE;
4785 } else if (str_begins_with (arg, "keep-temps")) {
4786 opts->save_temps = TRUE;
4787 } else if (str_begins_with (arg, "write-symbols")) {
4788 opts->write_symbols = TRUE;
4789 } else if (str_begins_with (arg, "no-write-symbols")) {
4790 opts->write_symbols = FALSE;
4791 } else if (str_begins_with (arg, "metadata-only")) {
4792 opts->metadata_only = TRUE;
4793 } else if (str_begins_with (arg, "bind-to-runtime-version")) {
4794 opts->bind_to_runtime_version = TRUE;
4795 } else if (str_begins_with (arg, "full")) {
4796 opts->full_aot = TRUE;
4797 } else if (str_begins_with (arg, "threads=")) {
4798 opts->nthreads = atoi (arg + strlen ("threads="));
4799 } else if (str_begins_with (arg, "static")) {
4800 opts->static_link = TRUE;
4801 opts->no_dlsym = TRUE;
4802 } else if (str_begins_with (arg, "asmonly")) {
4803 opts->asm_only = TRUE;
4804 } else if (str_begins_with (arg, "asmwriter")) {
4805 opts->asm_writer = TRUE;
4806 } else if (str_begins_with (arg, "nodebug")) {
4807 opts->nodebug = TRUE;
4808 } else if (str_begins_with (arg, "ntrampolines=")) {
4809 opts->ntrampolines = atoi (arg + strlen ("ntrampolines="));
4810 } else if (str_begins_with (arg, "nrgctx-trampolines=")) {
4811 opts->nrgctx_trampolines = atoi (arg + strlen ("nrgctx-trampolines="));
4812 } else if (str_begins_with (arg, "nimt-trampolines=")) {
4813 opts->nimt_trampolines = atoi (arg + strlen ("nimt-trampolines="));
4814 } else if (str_begins_with (arg, "autoreg")) {
4815 opts->autoreg = TRUE;
4816 } else if (str_begins_with (arg, "tool-prefix=")) {
4817 opts->tool_prefix = g_strdup (arg + strlen ("tool-prefix="));
4818 } else if (str_begins_with (arg, "soft-debug")) {
4819 opts->soft_debug = TRUE;
4820 } else if (str_begins_with (arg, "print-skipped")) {
4821 opts->print_skipped_methods = TRUE;
4822 } else if (str_begins_with (arg, "stats")) {
4824 } else if (str_begins_with (arg, "mtriple=")) {
4825 opts->mtriple = g_strdup (arg + strlen ("mtriple="));
4826 } else if (str_begins_with (arg, "llvm-path=")) {
4827 opts->llvm_path = g_strdup (arg + strlen ("llvm-path="));
4828 } else if (str_begins_with (arg, "readonly-value=")) {
4829 add_readonly_value (opts, arg + strlen ("readonly-value="));
4830 } else if (str_begins_with (arg, "info")) {
4831 printf ("AOT target setup: %s.\n", AOT_TARGET_STR);
4833 } else if (str_begins_with (arg, "help") || str_begins_with (arg, "?")) {
4834 printf ("Supported options for --aot:\n");
4835 printf (" outfile=\n");
4836 printf (" save-temps\n");
4837 printf (" keep-temps\n");
4838 printf (" write-symbols\n");
4839 printf (" metadata-only\n");
4840 printf (" bind-to-runtime-version\n");
4842 printf (" threads=\n");
4843 printf (" static\n");
4844 printf (" asmonly\n");
4845 printf (" asmwriter\n");
4846 printf (" nodebug\n");
4847 printf (" ntrampolines=\n");
4848 printf (" nrgctx-trampolines=\n");
4849 printf (" nimt-trampolines=\n");
4850 printf (" autoreg\n");
4851 printf (" tool-prefix=\n");
4852 printf (" readonly-value=\n");
4853 printf (" soft-debug\n");
4854 printf (" print-skipped\n");
4855 printf (" stats\n");
4857 printf (" help/?\n");
4860 fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
4869 add_token_info_hash (gpointer key, gpointer value, gpointer user_data)
4871 MonoMethod *method = (MonoMethod*)key;
4872 MonoJumpInfoToken *ji = (MonoJumpInfoToken*)value;
4873 MonoJumpInfoToken *new_ji = g_new0 (MonoJumpInfoToken, 1);
4874 MonoAotCompile *acfg = user_data;
4876 new_ji->image = ji->image;
4877 new_ji->token = ji->token;
4878 g_hash_table_insert (acfg->token_info_hash, method, new_ji);
4882 can_encode_class (MonoAotCompile *acfg, MonoClass *klass)
4884 if (klass->type_token)
4886 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
4889 return can_encode_class (acfg, klass->element_class);
4894 can_encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
4896 switch (patch_info->type) {
4897 case MONO_PATCH_INFO_METHOD:
4898 case MONO_PATCH_INFO_METHODCONST: {
4899 MonoMethod *method = patch_info->data.method;
4901 if (method->wrapper_type) {
4902 switch (method->wrapper_type) {
4903 case MONO_WRAPPER_NONE:
4904 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
4905 case MONO_WRAPPER_XDOMAIN_INVOKE:
4906 case MONO_WRAPPER_STFLD:
4907 case MONO_WRAPPER_LDFLD:
4908 case MONO_WRAPPER_LDFLDA:
4909 case MONO_WRAPPER_LDFLD_REMOTE:
4910 case MONO_WRAPPER_STFLD_REMOTE:
4911 case MONO_WRAPPER_STELEMREF:
4912 case MONO_WRAPPER_ISINST:
4913 case MONO_WRAPPER_PROXY_ISINST:
4914 case MONO_WRAPPER_ALLOC:
4915 case MONO_WRAPPER_REMOTING_INVOKE:
4916 case MONO_WRAPPER_UNKNOWN:
4917 case MONO_WRAPPER_WRITE_BARRIER:
4919 case MONO_WRAPPER_MANAGED_TO_MANAGED:
4920 if (!strcmp (method->name, "ElementAddr"))
4924 case MONO_WRAPPER_CASTCLASS:
4925 if (!strcmp (method->name, "__castclass_with_cache"))
4927 else if (!strcmp (method->name, "__isinst_with_cache"))
4932 //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
4936 if (!method->token) {
4937 /* The method is part of a constructed type like Int[,].Set (). */
4938 if (!g_hash_table_lookup (acfg->token_info_hash, method)) {
4939 if (method->klass->rank)
4947 case MONO_PATCH_INFO_VTABLE:
4948 case MONO_PATCH_INFO_CLASS_INIT:
4949 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
4950 case MONO_PATCH_INFO_CLASS:
4951 case MONO_PATCH_INFO_IID:
4952 case MONO_PATCH_INFO_ADJUSTED_IID:
4953 if (!can_encode_class (acfg, patch_info->data.klass)) {
4954 //printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
4958 case MONO_PATCH_INFO_RGCTX_FETCH: {
4959 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
4961 if (!can_encode_patch (acfg, entry->data))
4975 * AOT compile a given method.
4976 * This function might be called by multiple threads, so it must be thread-safe.
4979 compile_method (MonoAotCompile *acfg, MonoMethod *method)
4982 MonoJumpInfo *patch_info;
4985 MonoMethod *wrapped;
4987 if (acfg->aot_opts.metadata_only)
4990 mono_acfg_lock (acfg);
4991 index = get_method_index (acfg, method);
4992 mono_acfg_unlock (acfg);
4994 /* fixme: maybe we can also precompile wrapper methods */
4995 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
4996 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
4997 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
4998 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
5002 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
5005 wrapped = mono_marshal_method_from_wrapper (method);
5006 if (wrapped && (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && wrapped->is_generic)
5007 // FIXME: The wrapper should be generic too, but it is not
5010 InterlockedIncrement (&acfg->stats.mcount);
5013 if (method->is_generic || method->klass->generic_container) {
5014 InterlockedIncrement (&acfg->stats.genericcount);
5019 //acfg->aot_opts.print_skipped_methods = TRUE;
5022 * Since these methods are the only ones which are compiled with
5023 * AOT support, and they are not used by runtime startup/shutdown code,
5024 * the runtime will not see AOT methods during AOT compilation,so it
5025 * does not need to support them by creating a fake GOT etc.
5027 cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), FALSE, TRUE, 0);
5028 if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
5029 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
5030 InterlockedIncrement (&acfg->stats.genericcount);
5033 if (cfg->exception_type != MONO_EXCEPTION_NONE) {
5034 //printf ("E: %s\n", mono_method_full_name (method, TRUE));
5035 /* Let the exception happen at runtime */
5039 if (cfg->disable_aot) {
5040 if (acfg->aot_opts.print_skipped_methods)
5041 printf ("Skip (disabled): %s\n", mono_method_full_name (method, TRUE));
5042 InterlockedIncrement (&acfg->stats.ocount);
5043 mono_destroy_compile (cfg);
5047 /* Nullify patches which need no aot processing */
5048 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5049 switch (patch_info->type) {
5050 case MONO_PATCH_INFO_LABEL:
5051 case MONO_PATCH_INFO_BB:
5052 patch_info->type = MONO_PATCH_INFO_NONE;
5059 /* Collect method->token associations from the cfg */
5060 mono_acfg_lock (acfg);
5061 g_hash_table_foreach (cfg->token_info_hash, add_token_info_hash, acfg);
5062 mono_acfg_unlock (acfg);
5065 * Check for absolute addresses.
5068 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5069 switch (patch_info->type) {
5070 case MONO_PATCH_INFO_ABS:
5071 /* unable to handle this */
5080 if (acfg->aot_opts.print_skipped_methods)
5081 printf ("Skip (abs call): %s\n", mono_method_full_name (method, TRUE));
5082 InterlockedIncrement (&acfg->stats.abscount);
5083 mono_destroy_compile (cfg);
5087 /* Lock for the rest of the code */
5088 mono_acfg_lock (acfg);
5091 * Check for methods/klasses we can't encode.
5094 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5095 if (!can_encode_patch (acfg, patch_info))
5100 if (acfg->aot_opts.print_skipped_methods)
5101 printf ("Skip (patches): %s\n", mono_method_full_name (method, TRUE));
5102 acfg->stats.ocount++;
5103 mono_destroy_compile (cfg);
5104 mono_acfg_unlock (acfg);
5108 /* Adds generic instances referenced by this method */
5110 * The depth is used to avoid infinite loops when generic virtual recursion is
5113 depth = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_depth, method));
5115 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5116 switch (patch_info->type) {
5117 case MONO_PATCH_INFO_METHOD: {
5118 MonoMethod *m = patch_info->data.method;
5119 if (m->is_inflated) {
5120 if (!(mono_class_generic_sharing_enabled (m->klass) &&
5121 mono_method_is_generic_sharable_impl (m, FALSE)) &&
5122 !method_has_type_vars (m)) {
5123 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
5124 if (acfg->aot_opts.full_aot)
5125 add_extra_method_with_depth (acfg, mono_marshal_get_native_wrapper (m, TRUE, TRUE), depth + 1);
5127 add_extra_method_with_depth (acfg, m, depth + 1);
5130 add_generic_class_with_depth (acfg, m->klass, depth + 5);
5132 if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED && !strcmp (m->name, "ElementAddr"))
5133 add_extra_method_with_depth (acfg, m, depth + 1);
5136 case MONO_PATCH_INFO_VTABLE: {
5137 MonoClass *klass = patch_info->data.klass;
5139 if (klass->generic_class && !mono_generic_context_is_sharable (&klass->generic_class->context, FALSE))
5140 add_generic_class_with_depth (acfg, klass, depth + 5);
5149 /* Determine whenever the method has GOT slots */
5150 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5151 switch (patch_info->type) {
5152 case MONO_PATCH_INFO_GOT_OFFSET:
5153 case MONO_PATCH_INFO_NONE:
5154 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
5156 case MONO_PATCH_INFO_IMAGE:
5157 /* The assembly is stored in GOT slot 0 */
5158 if (patch_info->data.image != acfg->image)
5159 cfg->has_got_slots = TRUE;
5162 if (!is_plt_patch (patch_info))
5163 cfg->has_got_slots = TRUE;
5168 if (!cfg->has_got_slots)
5169 InterlockedIncrement (&acfg->stats.methods_without_got_slots);
5172 * FIXME: Instead of this mess, allocate the patches from the aot mempool.
5174 /* Make a copy of the patch info which is in the mempool */
5176 MonoJumpInfo *patches = NULL, *patches_end = NULL;
5178 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
5179 MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
5182 patches = new_patch_info;
5184 patches_end->next = new_patch_info;
5185 patches_end = new_patch_info;
5187 cfg->patch_info = patches;
5189 /* Make a copy of the unwind info */
5191 GSList *l, *unwind_ops;
5195 for (l = cfg->unwind_ops; l; l = l->next) {
5196 op = mono_mempool_alloc (acfg->mempool, sizeof (MonoUnwindOp));
5197 memcpy (op, l->data, sizeof (MonoUnwindOp));
5198 unwind_ops = g_slist_prepend_mempool (acfg->mempool, unwind_ops, op);
5200 cfg->unwind_ops = g_slist_reverse (unwind_ops);
5202 /* Make a copy of the argument/local info */
5204 MonoInst **args, **locals;
5205 MonoMethodSignature *sig;
5206 MonoMethodHeader *header;
5209 sig = mono_method_signature (method);
5210 args = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * (sig->param_count + sig->hasthis));
5211 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
5212 args [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
5213 memcpy (args [i], cfg->args [i], sizeof (MonoInst));
5217 header = mono_method_get_header (method);
5218 locals = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * header->num_locals);
5219 for (i = 0; i < header->num_locals; ++i) {
5220 locals [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
5221 memcpy (locals [i], cfg->locals [i], sizeof (MonoInst));
5223 cfg->locals = locals;
5226 /* Free some fields used by cfg to conserve memory */
5227 mono_mempool_destroy (cfg->mempool);
5228 cfg->mempool = NULL;
5229 g_free (cfg->varinfo);
5230 cfg->varinfo = NULL;
5234 mono_regstate_free (cfg->rs);
5238 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
5240 while (index >= acfg->cfgs_size) {
5241 MonoCompile **new_cfgs;
5244 new_size = acfg->cfgs_size * 2;
5245 new_cfgs = g_new0 (MonoCompile*, new_size);
5246 memcpy (new_cfgs, acfg->cfgs, sizeof (MonoCompile*) * acfg->cfgs_size);
5247 g_free (acfg->cfgs);
5248 acfg->cfgs = new_cfgs;
5249 acfg->cfgs_size = new_size;
5251 acfg->cfgs [index] = cfg;
5253 g_hash_table_insert (acfg->method_to_cfg, cfg->orig_method, cfg);
5256 if (cfg->orig_method->wrapper_type)
5257 g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
5260 mono_acfg_unlock (acfg);
5262 InterlockedIncrement (&acfg->stats.ccount);
5266 compile_thread_main (gpointer *user_data)
5268 MonoDomain *domain = user_data [0];
5269 MonoAotCompile *acfg = user_data [1];
5270 GPtrArray *methods = user_data [2];
5273 mono_thread_attach (domain);
5275 for (i = 0; i < methods->len; ++i)
5276 compile_method (acfg, g_ptr_array_index (methods, i));
5280 load_profile_files (MonoAotCompile *acfg)
5284 int file_index, res, method_index, i;
5287 GList *unordered, *l;
5292 tmp = g_strdup_printf ("%s/.mono/aot-profile-data/%s-%d", g_get_home_dir (), acfg->image->assembly_name, file_index);
5294 if (!g_file_test (tmp, G_FILE_TEST_IS_REGULAR)) {
5299 infile = fopen (tmp, "r");
5302 printf ("Using profile data file '%s'\n", tmp);
5307 res = fscanf (infile, "%32s\n", ver);
5308 if ((res != 1) || strcmp (ver, "#VER:2") != 0) {
5309 printf ("Profile file has wrong version or invalid.\n");
5316 MonoMethodDesc *desc;
5319 if (fgets (name, 1023, infile) == NULL)
5322 /* Kill the newline */
5323 if (strlen (name) > 0)
5324 name [strlen (name) - 1] = '\0';
5326 desc = mono_method_desc_new (name, TRUE);
5328 method = mono_method_desc_search_in_image (desc, acfg->image);
5330 if (method && mono_method_get_token (method)) {
5331 token = mono_method_get_token (method);
5332 method_index = mono_metadata_token_index (token) - 1;
5335 for (i = 0; i < acfg->method_order->len; ++i) {
5336 if (g_ptr_array_index (acfg->method_order, i) == GUINT_TO_POINTER (method_index)) {
5342 g_ptr_array_add (acfg->method_order, GUINT_TO_POINTER (method_index));
5344 //printf ("No method found matching '%s'.\n", name);
5350 /* Add missing methods */
5352 for (method_index = 0; method_index < acfg->image->tables [MONO_TABLE_METHOD].rows; ++method_index) {
5354 for (i = 0; i < acfg->method_order->len; ++i) {
5355 if (g_ptr_array_index (acfg->method_order, i) == GUINT_TO_POINTER (method_index)) {
5361 unordered = g_list_prepend (unordered, GUINT_TO_POINTER (method_index));
5363 unordered = g_list_reverse (unordered);
5364 for (l = unordered; l; l = l->next)
5365 g_ptr_array_add (acfg->method_order, l->data);
5368 /* Used by the LLVM backend */
5370 mono_aot_get_got_offset (MonoJumpInfo *ji)
5372 return get_got_offset (llvm_acfg, ji);
5376 mono_aot_get_method_name (MonoCompile *cfg)
5378 if (llvm_acfg->aot_opts.static_link)
5379 /* Include the assembly name too to avoid duplicate symbol errors */
5380 return g_strdup_printf ("%s_%s", llvm_acfg->assembly_name_sym, get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash));
5382 return get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash);
5386 mono_aot_get_plt_symbol (MonoJumpInfoType type, gconstpointer data)
5388 MonoJumpInfo *ji = mono_mempool_alloc (llvm_acfg->mempool, sizeof (MonoJumpInfo));
5389 MonoPltEntry *plt_entry;
5392 ji->data.target = data;
5394 if (!can_encode_patch (llvm_acfg, ji))
5397 plt_entry = get_plt_entry (llvm_acfg, ji);
5398 plt_entry->llvm_used = TRUE;
5400 #if defined(__APPLE__)
5401 return g_strdup_printf (plt_entry->llvm_symbol + strlen (llvm_acfg->llvm_label_prefix));
5403 return g_strdup_printf (plt_entry->llvm_symbol);
5408 mono_aot_patch_info_dup (MonoJumpInfo* ji)
5412 mono_acfg_lock (llvm_acfg);
5413 res = mono_patch_info_dup_mp (llvm_acfg->mempool, ji);
5414 mono_acfg_unlock (llvm_acfg);
5424 * Emit the LLVM code into an LLVM bytecode file, and compile it using the LLVM
5428 emit_llvm_file (MonoAotCompile *acfg)
5430 char *command, *opts;
5432 MonoJumpInfo *patch_info;
5435 * When using LLVM, we let llvm emit the got since the LLVM IL needs to refer
5439 /* Compute the final size of the got */
5440 for (i = 0; i < acfg->nmethods; ++i) {
5441 if (acfg->cfgs [i]) {
5442 for (patch_info = acfg->cfgs [i]->patch_info; patch_info; patch_info = patch_info->next) {
5443 if (patch_info->type != MONO_PATCH_INFO_NONE) {
5444 if (!is_plt_patch (patch_info))
5445 get_got_offset (acfg, patch_info);
5447 get_plt_entry (acfg, patch_info);
5453 acfg->final_got_size = acfg->got_offset + acfg->plt_offset;
5455 if (acfg->aot_opts.full_aot) {
5459 * Need to add the got entries used by the trampolines.
5460 * This is only a conservative approximation.
5462 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
5463 /* For the generic + rgctx trampolines */
5464 acfg->final_got_size += 200;
5465 /* For the specific trampolines */
5466 for (ntype = 0; ntype < MONO_AOT_TRAMP_NUM; ++ntype)
5467 acfg->final_got_size += acfg->num_trampolines [ntype] * 2;
5472 mono_llvm_emit_aot_module ("temp.bc", acfg->final_got_size);
5475 * FIXME: Experiment with adding optimizations, the -std-compile-opts set takes
5476 * a lot of time, and doesn't seem to save much space.
5477 * The following optimizations cannot be enabled:
5479 * - 'jump-threading' changes our blockaddress references to int constants.
5480 * The opt list below was produced by taking the output of:
5481 * llvm-as < /dev/null | opt -O2 -disable-output -debug-pass=Arguments
5482 * then removing tailcallelim + the global opts, and adding a second gvn.
5484 opts = g_strdup ("-instcombine -simplifycfg");
5485 opts = g_strdup ("-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -basiccg -prune-eh -inline -functionattrs -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 -preverify -domtree -verify");
5487 command = g_strdup_printf ("%sopt -f %s -o temp.opt.bc temp.bc", acfg->aot_opts.llvm_path, opts);
5488 printf ("Executing opt: %s\n", command);
5489 if (system (command) != 0) {
5495 if (!acfg->llc_args)
5496 acfg->llc_args = g_string_new ("");
5498 /* Verbose asm slows down llc greatly */
5499 g_string_append (acfg->llc_args, " -asm-verbose=false");
5501 if (acfg->aot_opts.mtriple)
5502 g_string_append_printf (acfg->llc_args, " -mtriple=%s", acfg->aot_opts.mtriple);
5504 if (llvm_acfg->aot_opts.static_link)
5505 g_string_append_printf (acfg->llc_args, " -relocation-model=static");
5507 g_string_append_printf (acfg->llc_args, " -relocation-model=pic");
5508 unlink (acfg->tmpfname);
5510 command = g_strdup_printf ("%sllc %s -disable-gnu-eh-frame -enable-mono-eh-frame -o %s temp.opt.bc", acfg->aot_opts.llvm_path, acfg->llc_args->str, acfg->tmpfname);
5512 printf ("Executing llc: %s\n", command);
5514 if (system (command) != 0) {
5521 emit_code (MonoAotCompile *acfg)
5525 char end_symbol [256];
5527 #if defined(TARGET_POWERPC64)
5528 sprintf (symbol, ".Lgot_addr");
5529 emit_section_change (acfg, ".text", 0);
5530 emit_alignment (acfg, 8);
5531 emit_label (acfg, symbol);
5532 emit_pointer (acfg, acfg->got_symbol);
5536 * This global symbol is used to compute the address of each method using the
5537 * code_offsets array. It is also used to compute the memory ranges occupied by
5538 * AOT code, so it must be equal to the address of the first emitted method.
5540 sprintf (symbol, "methods");
5541 emit_section_change (acfg, ".text", 0);
5542 emit_alignment (acfg, 8);
5544 for (i = 0; i < acfg->nmethods; ++i) {
5545 if (acfg->cfgs [i] && acfg->cfgs [i]->compile_llvm) {
5546 fprintf (acfg->fp, "\n.set methods, %s\n", acfg->cfgs [i]->asm_symbol);
5550 if (i == acfg->nmethods)
5551 /* No LLVM compiled methods */
5552 emit_label (acfg, symbol);
5554 emit_label (acfg, symbol);
5558 * Emit some padding so the local symbol for the first method doesn't have the
5559 * same address as 'methods'.
5561 #if defined(__default_codegen__)
5562 emit_zero_bytes (acfg, 16);
5563 #elif defined(__native_client_codegen__)
5565 const int kPaddingSize = 16;
5566 guint8 pad_buffer[kPaddingSize];
5567 mono_arch_nacl_pad (pad_buffer, kPaddingSize);
5568 emit_bytes (acfg, pad_buffer, kPaddingSize);
5572 for (oindex = 0; oindex < acfg->method_order->len; ++oindex) {
5576 i = GPOINTER_TO_UINT (g_ptr_array_index (acfg->method_order, oindex));
5578 cfg = acfg->cfgs [i];
5583 method = cfg->orig_method;
5585 /* Emit unbox trampoline */
5586 if (acfg->aot_opts.full_aot && cfg->orig_method->klass->valuetype && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
5587 sprintf (symbol, "ut_%d", get_method_index (acfg, method));
5589 emit_section_change (acfg, ".text", 0);
5590 #ifdef __native_client_codegen__
5591 emit_alignment (acfg, AOT_FUNC_ALIGNMENT);
5593 emit_global (acfg, symbol, TRUE);
5595 if (acfg->thumb_mixed && cfg->compile_llvm)
5596 fprintf (acfg->fp, "\n.thumb_func\n");
5598 emit_label (acfg, symbol);
5600 arch_emit_unbox_trampoline (acfg, cfg, cfg->orig_method, cfg->asm_symbol);
5603 if (cfg->compile_llvm)
5604 acfg->stats.llvm_count ++;
5606 emit_method_code (acfg, cfg);
5609 sprintf (symbol, "methods_end");
5610 emit_section_change (acfg, ".text", 0);
5611 emit_alignment (acfg, 8);
5612 emit_label (acfg, symbol);
5615 * Add .no_dead_strip directives for all LLVM methods to prevent the OSX linker
5616 * from optimizing them away, since it doesn't see that code_offsets references them.
5617 * JITted methods don't need this since they are referenced using assembler local
5619 * FIXME: This is why write-symbols doesn't work on OSX ?
5621 if (acfg->llvm && acfg->need_no_dead_strip) {
5622 fprintf (acfg->fp, "\n");
5623 for (i = 0; i < acfg->nmethods; ++i) {
5624 if (acfg->cfgs [i] && acfg->cfgs [i]->compile_llvm)
5625 fprintf (acfg->fp, ".no_dead_strip %s\n", acfg->cfgs [i]->asm_symbol);
5629 sprintf (symbol, "code_offsets");
5630 emit_section_change (acfg, RODATA_SECT, 1);
5631 emit_alignment (acfg, 8);
5632 emit_label (acfg, symbol);
5634 acfg->stats.offsets_size += acfg->nmethods * 4;
5636 sprintf (end_symbol, "methods");
5637 for (i = 0; i < acfg->nmethods; ++i) {
5638 if (acfg->cfgs [i]) {
5639 emit_symbol_diff (acfg, acfg->cfgs [i]->asm_symbol, end_symbol, 0);
5641 emit_int32 (acfg, 0xffffffff);
5648 emit_info (MonoAotCompile *acfg)
5654 offsets = g_new0 (gint32, acfg->nmethods);
5656 for (oindex = 0; oindex < acfg->method_order->len; ++oindex) {
5657 i = GPOINTER_TO_UINT (g_ptr_array_index (acfg->method_order, oindex));
5659 if (acfg->cfgs [i]) {
5660 emit_method_info (acfg, acfg->cfgs [i]);
5661 offsets [i] = acfg->cfgs [i]->method_info_offset;
5667 sprintf (symbol, "method_info_offsets");
5668 emit_section_change (acfg, RODATA_SECT, 1);
5669 emit_alignment (acfg, 8);
5670 emit_label (acfg, symbol);
5672 acfg->stats.offsets_size += emit_offset_table (acfg, acfg->nmethods, 10, offsets);
5677 #endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
5679 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
5680 #define mix(a,b,c) { \
5681 a -= c; a ^= rot(c, 4); c += b; \
5682 b -= a; b ^= rot(a, 6); a += c; \
5683 c -= b; c ^= rot(b, 8); b += a; \
5684 a -= c; a ^= rot(c,16); c += b; \
5685 b -= a; b ^= rot(a,19); a += c; \
5686 c -= b; c ^= rot(b, 4); b += a; \
5688 #define final(a,b,c) { \
5689 c ^= b; c -= rot(b,14); \
5690 a ^= c; a -= rot(c,11); \
5691 b ^= a; b -= rot(a,25); \
5692 c ^= b; c -= rot(b,16); \
5693 a ^= c; a -= rot(c,4); \
5694 b ^= a; b -= rot(a,14); \
5695 c ^= b; c -= rot(b,24); \
5699 mono_aot_type_hash (MonoType *t1)
5701 guint hash = t1->type;
5703 hash |= t1->byref << 6; /* do not collide with t1->type values */
5705 case MONO_TYPE_VALUETYPE:
5706 case MONO_TYPE_CLASS:
5707 case MONO_TYPE_SZARRAY:
5708 /* check if the distribution is good enough */
5709 return ((hash << 5) - hash) ^ mono_metadata_str_hash (t1->data.klass->name);
5711 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
5712 case MONO_TYPE_ARRAY:
5713 return ((hash << 5) - hash) ^ mono_metadata_type_hash (&t1->data.array->eklass->byval_arg);
5714 case MONO_TYPE_GENERICINST:
5715 return ((hash << 5) - hash) ^ 0;
5722 * mono_aot_method_hash:
5724 * Return a hash code for methods which only depends on metadata.
5727 mono_aot_method_hash (MonoMethod *method)
5729 MonoMethodSignature *sig;
5733 guint32 *hashes_start, *hashes;
5735 MonoGenericInst *ginst = NULL;
5737 /* Similar to the hash in mono_method_get_imt_slot () */
5739 sig = mono_method_signature (method);
5741 if (method->is_inflated)
5742 ginst = ((MonoMethodInflated*)method)->context.method_inst;
5744 hashes_count = sig->param_count + 5 + (ginst ? ginst->type_argc : 0);
5745 hashes_start = g_malloc0 (hashes_count * sizeof (guint32));
5746 hashes = hashes_start;
5748 /* Some wrappers are assigned to random classes */
5749 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
5750 klass = method->klass;
5752 klass = mono_defaults.object_class;
5754 if (!method->wrapper_type) {
5755 char *full_name = mono_type_full_name (&klass->byval_arg);
5757 hashes [0] = mono_metadata_str_hash (full_name);
5761 hashes [0] = mono_metadata_str_hash (klass->name);
5762 hashes [1] = mono_metadata_str_hash (klass->name_space);
5764 if (method->wrapper_type == MONO_WRAPPER_STFLD || method->wrapper_type == MONO_WRAPPER_LDFLD || method->wrapper_type == MONO_WRAPPER_LDFLDA)
5765 /* The method name includes a stringified pointer */
5768 hashes [2] = mono_metadata_str_hash (method->name);
5769 hashes [3] = method->wrapper_type;
5770 hashes [4] = mono_aot_type_hash (sig->ret);
5772 for (i = 0; i < sig->param_count; i++) {
5773 hashes [hindex ++] = mono_aot_type_hash (sig->params [i]);
5776 for (i = 0; i < ginst->type_argc; ++i)
5777 hashes [hindex ++] = mono_aot_type_hash (ginst->type_argv [i]);
5779 g_assert (hindex == hashes_count);
5781 /* Setup internal state */
5782 a = b = c = 0xdeadbeef + (((guint32)hashes_count)<<2);
5784 /* Handle most of the hashes */
5785 while (hashes_count > 3) {
5794 /* Handle the last 3 hashes (all the case statements fall through) */
5795 switch (hashes_count) {
5796 case 3 : c += hashes [2];
5797 case 2 : b += hashes [1];
5798 case 1 : a += hashes [0];
5800 case 0: /* nothing left to add */
5804 free (hashes_start);
5813 * mono_aot_wrapper_name:
5815 * Return a string which uniqely identifies the given wrapper method.
5818 mono_aot_wrapper_name (MonoMethod *method)
5820 char *name, *tmpsig, *klass_desc;
5822 tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
5824 switch (method->wrapper_type) {
5825 case MONO_WRAPPER_RUNTIME_INVOKE:
5826 if (!strcmp (method->name, "runtime_invoke_dynamic"))
5827 name = g_strdup_printf ("(wrapper runtime-invoke-dynamic)");
5829 name = g_strdup_printf ("%s (%s)", method->name, tmpsig);
5832 klass_desc = mono_type_full_name (&method->klass->byval_arg);
5833 name = g_strdup_printf ("%s:%s (%s)", klass_desc, method->name, tmpsig);
5834 g_free (klass_desc);
5844 * mono_aot_get_array_helper_from_wrapper;
5846 * Get the helper method in Array called by an array wrapper method.
5849 mono_aot_get_array_helper_from_wrapper (MonoMethod *method)
5853 MonoGenericContext ctx;
5854 MonoType *args [16];
5855 char *mname, *iname, *s, *s2, *helper_name = NULL;
5857 prefix = "System.Collections.Generic";
5858 s = g_strdup_printf ("%s", method->name + strlen (prefix) + 1);
5859 s2 = strstr (s, "`1.");
5865 //printf ("X: %s %s\n", iname, mname);
5867 if (!strcmp (iname, "IList"))
5868 helper_name = g_strdup_printf ("InternalArray__%s", mname);
5870 helper_name = g_strdup_printf ("InternalArray__%s_%s", iname, mname);
5871 m = mono_class_get_method_from_name (mono_defaults.array_class, helper_name, mono_method_signature (method)->param_count);
5873 g_free (helper_name);
5876 if (m->is_generic) {
5877 memset (&ctx, 0, sizeof (ctx));
5878 args [0] = &method->klass->element_class->byval_arg;
5879 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
5880 m = mono_class_inflate_generic_method (m, &ctx);
5886 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
5888 typedef struct HashEntry {
5889 guint32 key, value, index;
5890 struct HashEntry *next;
5894 * emit_extra_methods:
5896 * Emit methods which are not in the METHOD table, like wrappers.
5899 emit_extra_methods (MonoAotCompile *acfg)
5901 int i, table_size, buf_size;
5904 guint32 *info_offsets;
5907 HashEntry *entry, *new_entry;
5908 int nmethods, max_chain_length;
5911 info_offsets = g_new0 (guint32, acfg->extra_methods->len);
5913 /* Emit method info */
5915 for (i = 0; i < acfg->extra_methods->len; ++i) {
5916 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
5917 MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
5924 p = buf = g_malloc (buf_size);
5928 method = cfg->method_to_register;
5931 if (method->wrapper_type) {
5932 gboolean encode_ref = FALSE;
5935 * We encode some wrappers using their name, since encoding them
5936 * directly would be difficult. This works because at runtime, we only need to
5937 * check whenever a method ref matches an existing MonoMethod. The downside is
5938 * that the method names are large, so we use the binary encoding if possible.
5940 switch (method->wrapper_type) {
5941 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
5942 case MONO_WRAPPER_SYNCHRONIZED:
5945 case MONO_WRAPPER_MANAGED_TO_NATIVE:
5946 /* Skip JIT icall wrappers */
5947 if (!strstr (method->name, "__icall_wrapper"))
5950 case MONO_WRAPPER_UNKNOWN:
5951 if (!strcmp (method->name, "PtrToStructure") || !strcmp (method->name, "StructureToPtr"))
5954 case MONO_WRAPPER_RUNTIME_INVOKE:
5955 if (mono_marshal_method_from_wrapper (method) != method && !strstr (method->name, "virtual"))
5956 /* Direct wrapper, encode normally */
5964 name = mono_aot_wrapper_name (method);
5968 encode_value (1, p, &p);
5969 encode_value (method->wrapper_type, p, &p);
5970 strcpy ((char*)p, name);
5971 p += strlen (name ) + 1;
5974 encode_value (0, p, &p);
5975 encode_method_ref (acfg, method, p, &p);
5978 g_assert ((p - buf) < buf_size);
5980 info_offsets [i] = add_to_blob (acfg, buf, p - buf);
5985 * Construct a chained hash table for mapping indexes in extra_method_info to
5988 table_size = g_spaced_primes_closest ((int)(nmethods * 1.5));
5989 table = g_ptr_array_sized_new (table_size);
5990 for (i = 0; i < table_size; ++i)
5991 g_ptr_array_add (table, NULL);
5992 chain_lengths = g_new0 (int, table_size);
5993 max_chain_length = 0;
5994 for (i = 0; i < acfg->extra_methods->len; ++i) {
5995 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
5996 MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
6002 key = info_offsets [i];
6003 value = get_method_index (acfg, method);
6005 hash = mono_aot_method_hash (method) % table_size;
6007 chain_lengths [hash] ++;
6008 max_chain_length = MAX (max_chain_length, chain_lengths [hash]);
6010 new_entry = mono_mempool_alloc0 (acfg->mempool, sizeof (HashEntry));
6011 new_entry->key = key;
6012 new_entry->value = value;
6014 entry = g_ptr_array_index (table, hash);
6015 if (entry == NULL) {
6016 new_entry->index = hash;
6017 g_ptr_array_index (table, hash) = new_entry;
6020 entry = entry->next;
6022 entry->next = new_entry;
6023 new_entry->index = table->len;
6024 g_ptr_array_add (table, new_entry);
6028 //printf ("MAX: %d\n", max_chain_length);
6030 /* Emit the table */
6031 sprintf (symbol, "extra_method_table");
6032 emit_section_change (acfg, RODATA_SECT, 0);
6033 emit_alignment (acfg, 8);
6034 emit_label (acfg, symbol);
6036 emit_int32 (acfg, table_size);
6037 for (i = 0; i < table->len; ++i) {
6038 HashEntry *entry = g_ptr_array_index (table, i);
6040 if (entry == NULL) {
6041 emit_int32 (acfg, 0);
6042 emit_int32 (acfg, 0);
6043 emit_int32 (acfg, 0);
6045 //g_assert (entry->key > 0);
6046 emit_int32 (acfg, entry->key);
6047 emit_int32 (acfg, entry->value);
6049 emit_int32 (acfg, entry->next->index);
6051 emit_int32 (acfg, 0);
6056 * Emit a table reverse mapping method indexes to their index in extra_method_info.
6057 * This is used by mono_aot_find_jit_info ().
6059 sprintf (symbol, "extra_method_info_offsets");
6060 emit_section_change (acfg, RODATA_SECT, 0);
6061 emit_alignment (acfg, 8);
6062 emit_label (acfg, symbol);
6064 emit_int32 (acfg, acfg->extra_methods->len);
6065 for (i = 0; i < acfg->extra_methods->len; ++i) {
6066 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
6068 emit_int32 (acfg, get_method_index (acfg, method));
6069 emit_int32 (acfg, info_offsets [i]);
6074 emit_exception_info (MonoAotCompile *acfg)
6080 offsets = g_new0 (gint32, acfg->nmethods);
6081 for (i = 0; i < acfg->nmethods; ++i) {
6082 if (acfg->cfgs [i]) {
6083 emit_exception_debug_info (acfg, acfg->cfgs [i]);
6084 offsets [i] = acfg->cfgs [i]->ex_info_offset;
6090 sprintf (symbol, "ex_info_offsets");
6091 emit_section_change (acfg, RODATA_SECT, 1);
6092 emit_alignment (acfg, 8);
6093 emit_label (acfg, symbol);
6095 acfg->stats.offsets_size += emit_offset_table (acfg, acfg->nmethods, 10, offsets);
6100 emit_unwind_info (MonoAotCompile *acfg)
6106 * The unwind info contains a lot of duplicates so we emit each unique
6107 * entry once, and only store the offset from the start of the table in the
6111 sprintf (symbol, "unwind_info");
6112 emit_section_change (acfg, RODATA_SECT, 1);
6113 emit_alignment (acfg, 8);
6114 emit_label (acfg, symbol);
6116 for (i = 0; i < acfg->unwind_ops->len; ++i) {
6117 guint32 index = GPOINTER_TO_UINT (g_ptr_array_index (acfg->unwind_ops, i));
6118 guint8 *unwind_info;
6119 guint32 unwind_info_len;
6123 unwind_info = mono_get_cached_unwind_info (index, &unwind_info_len);
6126 encode_value (unwind_info_len, p, &p);
6127 emit_bytes (acfg, buf, p - buf);
6128 emit_bytes (acfg, unwind_info, unwind_info_len);
6130 acfg->stats.unwind_info_size += (p - buf) + unwind_info_len;
6135 emit_class_info (MonoAotCompile *acfg)
6141 offsets = g_new0 (gint32, acfg->image->tables [MONO_TABLE_TYPEDEF].rows);
6142 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
6143 offsets [i] = emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
6145 sprintf (symbol, "class_info_offsets");
6146 emit_section_change (acfg, RODATA_SECT, 1);
6147 emit_alignment (acfg, 8);
6148 emit_label (acfg, symbol);
6150 acfg->stats.offsets_size += emit_offset_table (acfg, acfg->image->tables [MONO_TABLE_TYPEDEF].rows, 10, offsets);
6154 typedef struct ClassNameTableEntry {
6155 guint32 token, index;
6156 struct ClassNameTableEntry *next;
6157 } ClassNameTableEntry;
6160 emit_class_name_table (MonoAotCompile *acfg)
6163 guint32 token, hash;
6168 ClassNameTableEntry *entry, *new_entry;
6171 * Construct a chained hash table for mapping class names to typedef tokens.
6173 table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
6174 table = g_ptr_array_sized_new (table_size);
6175 for (i = 0; i < table_size; ++i)
6176 g_ptr_array_add (table, NULL);
6177 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
6178 token = MONO_TOKEN_TYPE_DEF | (i + 1);
6179 klass = mono_class_get (acfg->image, token);
6181 mono_loader_clear_error ();
6184 full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
6185 hash = mono_metadata_str_hash (full_name) % table_size;
6188 /* FIXME: Allocate from the mempool */
6189 new_entry = g_new0 (ClassNameTableEntry, 1);
6190 new_entry->token = token;
6192 entry = g_ptr_array_index (table, hash);
6193 if (entry == NULL) {
6194 new_entry->index = hash;
6195 g_ptr_array_index (table, hash) = new_entry;
6198 entry = entry->next;
6200 entry->next = new_entry;
6201 new_entry->index = table->len;
6202 g_ptr_array_add (table, new_entry);
6206 /* Emit the table */
6207 sprintf (symbol, "class_name_table");
6208 emit_section_change (acfg, RODATA_SECT, 0);
6209 emit_alignment (acfg, 8);
6210 emit_label (acfg, symbol);
6212 /* FIXME: Optimize memory usage */
6213 g_assert (table_size < 65000);
6214 emit_int16 (acfg, table_size);
6215 g_assert (table->len < 65000);
6216 for (i = 0; i < table->len; ++i) {
6217 ClassNameTableEntry *entry = g_ptr_array_index (table, i);
6219 if (entry == NULL) {
6220 emit_int16 (acfg, 0);
6221 emit_int16 (acfg, 0);
6223 emit_int16 (acfg, mono_metadata_token_index (entry->token));
6225 emit_int16 (acfg, entry->next->index);
6227 emit_int16 (acfg, 0);
6233 emit_image_table (MonoAotCompile *acfg)
6239 * The image table is small but referenced in a lot of places.
6240 * So we emit it at once, and reference its elements by an index.
6243 sprintf (symbol, "image_table");
6244 emit_section_change (acfg, RODATA_SECT, 1);
6245 emit_alignment (acfg, 8);
6246 emit_label (acfg, symbol);
6248 emit_int32 (acfg, acfg->image_table->len);
6249 for (i = 0; i < acfg->image_table->len; i++) {
6250 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
6251 MonoAssemblyName *aname = &image->assembly->aname;
6253 /* FIXME: Support multi-module assemblies */
6254 g_assert (image->assembly->image == image);
6256 emit_string (acfg, image->assembly_name);
6257 emit_string (acfg, image->guid);
6258 emit_string (acfg, aname->culture ? aname->culture : "");
6259 emit_string (acfg, (const char*)aname->public_key_token);
6261 emit_alignment (acfg, 8);
6262 emit_int32 (acfg, aname->flags);
6263 emit_int32 (acfg, aname->major);
6264 emit_int32 (acfg, aname->minor);
6265 emit_int32 (acfg, aname->build);
6266 emit_int32 (acfg, aname->revision);
6271 emit_got_info (MonoAotCompile *acfg)
6274 int i, first_plt_got_patch, buf_size;
6276 guint32 *got_info_offsets;
6278 /* Add the patches needed by the PLT to the GOT */
6279 acfg->plt_got_offset_base = acfg->got_offset;
6280 first_plt_got_patch = acfg->got_patches->len;
6281 for (i = 1; i < acfg->plt_offset; ++i) {
6282 MonoPltEntry *plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
6284 g_ptr_array_add (acfg->got_patches, plt_entry->ji);
6287 acfg->got_offset += acfg->plt_offset;
6291 * - optimize offsets table.
6292 * - reduce number of exported symbols.
6293 * - emit info for a klass only once.
6294 * - determine when a method uses a GOT slot which is guaranteed to be already
6296 * - clean up and document the code.
6297 * - use String.Empty in class libs.
6300 /* Encode info required to decode shared GOT entries */
6301 buf_size = acfg->got_patches->len * 128;
6302 p = buf = mono_mempool_alloc (acfg->mempool, buf_size);
6303 got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->got_patches->len * sizeof (guint32));
6304 acfg->plt_got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->plt_offset * sizeof (guint32));
6306 if (acfg->plt_offset)
6307 acfg->plt_got_info_offsets [0] = 0;
6308 for (i = 0; i < acfg->got_patches->len; ++i) {
6309 MonoJumpInfo *ji = g_ptr_array_index (acfg->got_patches, i);
6313 encode_value (ji->type, p, &p);
6314 encode_patch (acfg, ji, p, &p);
6316 g_assert (p - buf <= buf_size);
6317 got_info_offsets [i] = add_to_blob (acfg, buf, p - buf);
6319 if (i >= first_plt_got_patch)
6320 acfg->plt_got_info_offsets [i - first_plt_got_patch + 1] = got_info_offsets [i];
6321 acfg->stats.got_info_size += p - buf;
6324 /* Emit got_info_offsets table */
6325 sprintf (symbol, "got_info_offsets");
6326 emit_section_change (acfg, RODATA_SECT, 1);
6327 emit_alignment (acfg, 8);
6328 emit_label (acfg, symbol);
6330 /* No need to emit offsets for the got plt entries, the plt embeds them directly */
6331 acfg->stats.offsets_size += emit_offset_table (acfg, first_plt_got_patch, 10, (gint32*)got_info_offsets);
6335 emit_got (MonoAotCompile *acfg)
6340 /* Don't make GOT global so accesses to it don't need relocations */
6341 sprintf (symbol, "%s", acfg->got_symbol);
6342 emit_section_change (acfg, ".bss", 0);
6343 emit_alignment (acfg, 8);
6344 emit_local_symbol (acfg, symbol, "got_end", FALSE);
6345 emit_label (acfg, symbol);
6346 if (acfg->got_offset > 0)
6347 emit_zero_bytes (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
6349 sprintf (symbol, "got_end");
6350 emit_label (acfg, symbol);
6354 typedef struct GlobalsTableEntry {
6355 guint32 value, index;
6356 struct GlobalsTableEntry *next;
6357 } GlobalsTableEntry;
6360 emit_globals (MonoAotCompile *acfg)
6366 GlobalsTableEntry *entry, *new_entry;
6368 if (!acfg->aot_opts.static_link)
6372 * When static linking, we emit a table containing our globals.
6376 * Construct a chained hash table for mapping global names to their index in
6377 * the globals table.
6379 table_size = g_spaced_primes_closest ((int)(acfg->globals->len * 1.5));
6380 table = g_ptr_array_sized_new (table_size);
6381 for (i = 0; i < table_size; ++i)
6382 g_ptr_array_add (table, NULL);
6383 for (i = 0; i < acfg->globals->len; ++i) {
6384 char *name = g_ptr_array_index (acfg->globals, i);
6386 hash = mono_metadata_str_hash (name) % table_size;
6388 /* FIXME: Allocate from the mempool */
6389 new_entry = g_new0 (GlobalsTableEntry, 1);
6390 new_entry->value = i;
6392 entry = g_ptr_array_index (table, hash);
6393 if (entry == NULL) {
6394 new_entry->index = hash;
6395 g_ptr_array_index (table, hash) = new_entry;
6398 entry = entry->next;
6400 entry->next = new_entry;
6401 new_entry->index = table->len;
6402 g_ptr_array_add (table, new_entry);
6406 /* Emit the table */
6407 sprintf (symbol, ".Lglobals_hash");
6408 emit_section_change (acfg, RODATA_SECT, 0);
6409 emit_alignment (acfg, 8);
6410 emit_label (acfg, symbol);
6412 /* FIXME: Optimize memory usage */
6413 g_assert (table_size < 65000);
6414 emit_int16 (acfg, table_size);
6415 for (i = 0; i < table->len; ++i) {
6416 GlobalsTableEntry *entry = g_ptr_array_index (table, i);
6418 if (entry == NULL) {
6419 emit_int16 (acfg, 0);
6420 emit_int16 (acfg, 0);
6422 emit_int16 (acfg, entry->value + 1);
6424 emit_int16 (acfg, entry->next->index);
6426 emit_int16 (acfg, 0);
6430 /* Emit the names */
6431 for (i = 0; i < acfg->globals->len; ++i) {
6432 char *name = g_ptr_array_index (acfg->globals, i);
6434 sprintf (symbol, "name_%d", i);
6435 emit_section_change (acfg, RODATA_SECT, 1);
6437 emit_alignment (acfg, 4);
6439 emit_label (acfg, symbol);
6440 emit_string (acfg, name);
6443 /* Emit the globals table */
6444 sprintf (symbol, "globals");
6445 emit_section_change (acfg, ".data", 0);
6446 /* This is not a global, since it is accessed by the init function */
6447 emit_alignment (acfg, 8);
6448 emit_label (acfg, symbol);
6450 sprintf (symbol, "%sglobals_hash", acfg->temp_prefix);
6451 emit_pointer (acfg, symbol);
6453 for (i = 0; i < acfg->globals->len; ++i) {
6454 char *name = g_ptr_array_index (acfg->globals, i);
6456 sprintf (symbol, "name_%d", i);
6457 emit_pointer (acfg, symbol);
6459 sprintf (symbol, "%s", name);
6460 emit_pointer (acfg, symbol);
6462 /* Null terminate the table */
6463 emit_int32 (acfg, 0);
6464 emit_int32 (acfg, 0);
6468 emit_autoreg (MonoAotCompile *acfg)
6473 * Emit a function into the .ctor section which will be called by the ELF
6474 * loader to register this module with the runtime.
6476 if (! (!acfg->use_bin_writer && acfg->aot_opts.static_link && acfg->aot_opts.autoreg))
6479 symbol = g_strdup_printf ("_%s_autoreg", acfg->static_linking_symbol);
6481 arch_emit_autoreg (acfg, symbol);
6487 emit_mem_end (MonoAotCompile *acfg)
6491 sprintf (symbol, "mem_end");
6492 emit_section_change (acfg, ".text", 1);
6493 emit_alignment (acfg, 8);
6494 emit_label (acfg, symbol);
6498 * Emit a structure containing all the information not stored elsewhere.
6501 emit_file_info (MonoAotCompile *acfg)
6506 const char *gc_name;
6509 emit_string_symbol (acfg, "assembly_guid" , acfg->image->guid);
6511 if (acfg->aot_opts.bind_to_runtime_version) {
6512 build_info = mono_get_runtime_build_info ();
6513 emit_string_symbol (acfg, "runtime_version", build_info);
6514 g_free (build_info);
6516 emit_string_symbol (acfg, "runtime_version", "");
6519 /* Emit a string holding the assembly name */
6520 emit_string_symbol (acfg, "assembly_name", acfg->image->assembly->aname.name);
6523 * The managed allocators are GC specific, so can't use an AOT image created by one GC
6526 gc_name = mono_gc_get_gc_name ();
6527 gc_name_offset = add_to_blob (acfg, (guint8*)gc_name, strlen (gc_name) + 1);
6529 sprintf (symbol, "mono_aot_file_info");
6530 emit_section_change (acfg, ".data", 0);
6531 emit_alignment (acfg, 8);
6532 emit_label (acfg, symbol);
6533 if (!acfg->aot_opts.static_link)
6534 emit_global (acfg, symbol, FALSE);
6536 /* The data emitted here must match MonoAotFileInfo. */
6538 emit_int32 (acfg, MONO_AOT_FILE_VERSION);
6539 emit_int32 (acfg, 0);
6542 * We emit pointers to our data structures instead of emitting global symbols which
6543 * point to them, to reduce the number of globals, and because using globals leads to
6544 * various problems (i.e. arm/thumb).
6546 emit_pointer (acfg, acfg->got_symbol);
6547 emit_pointer (acfg, "methods");
6550 * Emit a reference to the mono_eh_frame table created by our modified LLVM compiler.
6552 emit_pointer (acfg, "mono_eh_frame");
6554 emit_pointer (acfg, NULL);
6556 emit_pointer (acfg, "blob");
6557 emit_pointer (acfg, "class_name_table");
6558 emit_pointer (acfg, "class_info_offsets");
6559 emit_pointer (acfg, "method_info_offsets");
6560 emit_pointer (acfg, "ex_info_offsets");
6561 emit_pointer (acfg, "code_offsets");
6562 emit_pointer (acfg, "extra_method_info_offsets");
6563 emit_pointer (acfg, "extra_method_table");
6564 emit_pointer (acfg, "got_info_offsets");
6565 emit_pointer (acfg, "methods_end");
6566 emit_pointer (acfg, "unwind_info");
6567 emit_pointer (acfg, "mem_end");
6568 emit_pointer (acfg, "image_table");
6569 emit_pointer (acfg, "plt");
6570 emit_pointer (acfg, "plt_end");
6571 emit_pointer (acfg, "assembly_guid");
6572 emit_pointer (acfg, "runtime_version");
6573 if (acfg->num_trampoline_got_entries) {
6574 emit_pointer (acfg, "specific_trampolines");
6575 emit_pointer (acfg, "static_rgctx_trampolines");
6576 emit_pointer (acfg, "imt_thunks");
6578 emit_pointer (acfg, NULL);
6579 emit_pointer (acfg, NULL);
6580 emit_pointer (acfg, NULL);
6582 if (acfg->thumb_mixed) {
6583 emit_pointer (acfg, "thumb_end");
6585 emit_pointer (acfg, NULL);
6587 if (acfg->aot_opts.static_link) {
6588 emit_pointer (acfg, "globals");
6590 emit_pointer (acfg, NULL);
6592 emit_pointer (acfg, "assembly_name");
6594 emit_int32 (acfg, acfg->plt_got_offset_base);
6595 emit_int32 (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
6596 emit_int32 (acfg, acfg->plt_offset);
6597 emit_int32 (acfg, acfg->nmethods);
6598 emit_int32 (acfg, acfg->flags);
6599 emit_int32 (acfg, acfg->opts);
6600 emit_int32 (acfg, gc_name_offset);
6602 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
6603 emit_int32 (acfg, acfg->num_trampolines [i]);
6604 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
6605 emit_int32 (acfg, acfg->trampoline_got_offset_base [i]);
6606 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
6607 emit_int32 (acfg, acfg->trampoline_size [i]);
6609 #if defined (TARGET_ARM) && defined (__APPLE__)
6614 t.type = MONO_TYPE_R8;
6615 mono_type_size (&t, &align);
6617 emit_int32 (acfg, align);
6619 t.type = MONO_TYPE_I8;
6620 mono_type_size (&t, &align);
6622 emit_int32 (acfg, align);
6625 emit_int32 (acfg, __alignof__ (double));
6626 emit_int32 (acfg, __alignof__ (gint64));
6629 if (acfg->aot_opts.static_link) {
6633 * Emit a global symbol which can be passed by an embedding app to
6634 * mono_aot_register_module (). The symbol points to a pointer to the the file info
6637 #if defined(__APPLE__) && !defined(__native_client_codegen__)
6638 sprintf (symbol, "_mono_aot_module_%s_info", acfg->image->assembly->aname.name);
6640 sprintf (symbol, "mono_aot_module_%s_info", acfg->image->assembly->aname.name);
6643 /* Get rid of characters which cannot occur in symbols */
6645 for (p = symbol; *p; ++p) {
6646 if (!(isalnum (*p) || *p == '_'))
6649 acfg->static_linking_symbol = g_strdup (symbol);
6650 emit_global_inner (acfg, symbol, FALSE);
6651 emit_label (acfg, symbol);
6652 emit_pointer (acfg, "mono_aot_file_info");
6657 emit_blob (MonoAotCompile *acfg)
6661 sprintf (symbol, "blob");
6662 emit_section_change (acfg, RODATA_SECT, 1);
6663 emit_alignment (acfg, 8);
6664 emit_label (acfg, symbol);
6666 emit_bytes (acfg, (guint8*)acfg->blob.data, acfg->blob.index);
6670 emit_dwarf_info (MonoAotCompile *acfg)
6672 #ifdef EMIT_DWARF_INFO
6674 char symbol [128], symbol2 [128];
6676 /* DIEs for methods */
6677 for (i = 0; i < acfg->nmethods; ++i) {
6678 MonoCompile *cfg = acfg->cfgs [i];
6683 // FIXME: LLVM doesn't define .Lme_...
6684 if (cfg->compile_llvm)
6687 sprintf (symbol, "%s", cfg->asm_symbol);
6688 sprintf (symbol2, "%sme_%x", acfg->temp_prefix, i);
6690 mono_dwarf_writer_emit_method (acfg->dwarf, cfg, cfg->method, 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->method, mono_domain_get ()));
6696 collect_methods (MonoAotCompile *acfg)
6699 MonoImage *image = acfg->image;
6701 /* Collect methods */
6702 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
6704 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
6706 method = mono_get_method (acfg->image, token, NULL);
6709 printf ("Failed to load method 0x%x from '%s'.\n", token, image->name);
6713 /* Load all methods eagerly to skip the slower lazy loading code */
6714 mono_class_setup_methods (method->klass);
6716 if (acfg->aot_opts.full_aot && method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
6717 /* Compile the wrapper instead */
6718 /* We do this here instead of add_wrappers () because it is easy to do it here */
6719 MonoMethod *wrapper = mono_marshal_get_native_wrapper (method, check_for_pending_exc, TRUE);
6723 /* FIXME: Some mscorlib methods don't have debug info */
6725 if (acfg->aot_opts.soft_debug && !method->wrapper_type) {
6726 if (!((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
6727 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
6728 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
6729 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))) {
6730 if (!mono_debug_lookup_method (method)) {
6731 fprintf (stderr, "Method %s has no debug info, probably the .mdb file for the assembly is missing.\n", mono_method_full_name (method, TRUE));
6738 /* Since we add the normal methods first, their index will be equal to their zero based token index */
6739 add_method_with_index (acfg, method, i, FALSE);
6740 acfg->method_index ++;
6743 add_generic_instances (acfg);
6745 if (acfg->aot_opts.full_aot)
6746 add_wrappers (acfg);
6750 compile_methods (MonoAotCompile *acfg)
6754 if (acfg->aot_opts.nthreads > 0) {
6759 gpointer *user_data;
6760 MonoMethod **methods;
6762 methods_len = acfg->methods->len;
6764 len = acfg->methods->len / acfg->aot_opts.nthreads;
6767 * Partition the list of methods into fragments, and hand it to threads to
6770 threads = g_ptr_array_new ();
6771 /* Make a copy since acfg->methods is modified by compile_method () */
6772 methods = g_new0 (MonoMethod*, methods_len);
6773 //memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
6774 for (i = 0; i < methods_len; ++i)
6775 methods [i] = g_ptr_array_index (acfg->methods, i);
6777 while (i < methods_len) {
6778 frag = g_ptr_array_new ();
6779 for (j = 0; j < len; ++j) {
6780 if (i < methods_len) {
6781 g_ptr_array_add (frag, methods [i]);
6786 user_data = g_new0 (gpointer, 3);
6787 user_data [0] = mono_domain_get ();
6788 user_data [1] = acfg;
6789 user_data [2] = frag;
6791 handle = mono_create_thread (NULL, 0, (gpointer)compile_thread_main, user_data, 0, NULL);
6792 g_ptr_array_add (threads, handle);
6796 for (i = 0; i < threads->len; ++i) {
6797 WaitForSingleObjectEx (g_ptr_array_index (threads, i), INFINITE, FALSE);
6803 /* Compile methods added by compile_method () or all methods if nthreads == 0 */
6804 for (i = methods_len; i < acfg->methods->len; ++i) {
6805 /* This can new methods to acfg->methods */
6806 compile_method (acfg, g_ptr_array_index (acfg->methods, i));
6811 compile_asm (MonoAotCompile *acfg)
6813 char *command, *objfile;
6814 char *outfile_name, *tmp_outfile_name;
6815 const char *tool_prefix = acfg->aot_opts.tool_prefix ? acfg->aot_opts.tool_prefix : "";
6817 #if defined(TARGET_AMD64)
6818 #define AS_OPTIONS "--64"
6819 #elif defined(TARGET_POWERPC64)
6820 #define AS_OPTIONS "-a64 -mppc64"
6821 #define LD_OPTIONS "-m elf64ppc"
6822 #elif defined(sparc) && SIZEOF_VOID_P == 8
6823 #define AS_OPTIONS "-xarch=v9"
6824 #elif defined(TARGET_X86) && defined(__APPLE__) && !defined(__native_client_codegen__)
6825 #define AS_OPTIONS "-arch i386 -W"
6827 #define AS_OPTIONS ""
6830 #ifdef __native_client_codegen__
6831 #if defined(TARGET_AMD64)
6832 #define AS_NAME "nacl64-as"
6834 #define AS_NAME "nacl-as"
6837 #define AS_NAME "as"
6841 #define LD_OPTIONS ""
6844 #define EH_LD_OPTIONS ""
6846 if (acfg->aot_opts.asm_only) {
6847 printf ("Output file: '%s'.\n", acfg->tmpfname);
6848 if (acfg->aot_opts.static_link)
6849 printf ("Linking symbol: '%s'.\n", acfg->static_linking_symbol);
6853 if (acfg->aot_opts.static_link) {
6854 if (acfg->aot_opts.outfile)
6855 objfile = g_strdup_printf ("%s", acfg->aot_opts.outfile);
6857 objfile = g_strdup_printf ("%s.o", acfg->image->name);
6859 objfile = g_strdup_printf ("%s.o", acfg->tmpfname);
6861 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);
6862 printf ("Executing the native assembler: %s\n", command);
6863 if (system (command) != 0) {
6871 if (acfg->aot_opts.static_link) {
6872 printf ("Output file: '%s'.\n", objfile);
6873 printf ("Linking symbol: '%s'.\n", acfg->static_linking_symbol);
6878 if (acfg->aot_opts.outfile)
6879 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
6881 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
6883 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
6886 command = g_strdup_printf ("ld -shared -G -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
6887 #elif defined(__ppc__) && defined(__APPLE__)
6888 command = g_strdup_printf ("gcc -dynamiclib -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
6889 #elif defined(HOST_WIN32)
6890 command = g_strdup_printf ("gcc -shared --dll -mno-cygwin -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
6891 #elif defined(TARGET_X86) && defined(__APPLE__) && !defined(__native_client_codegen__)
6892 command = g_strdup_printf ("gcc -m32 -dynamiclib -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
6894 command = g_strdup_printf ("%sld %s %s -shared -o %s %s.o", tool_prefix, EH_LD_OPTIONS, LD_OPTIONS, tmp_outfile_name, acfg->tmpfname);
6896 printf ("Executing the native linker: %s\n", command);
6897 if (system (command) != 0) {
6898 g_free (tmp_outfile_name);
6899 g_free (outfile_name);
6907 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, SHARED_EXT);
6908 printf ("Stripping the binary: %s\n", com);
6912 #if defined(TARGET_ARM) && !defined(__APPLE__)
6914 * gas generates 'mapping symbols' each time code and data is mixed, which
6915 * happens a lot in emit_and_reloc_code (), so we need to get rid of them.
6917 command = g_strdup_printf ("%sstrip --strip-symbol=\\$a --strip-symbol=\\$d %s", tool_prefix, tmp_outfile_name);
6918 printf ("Stripping the binary: %s\n", command);
6919 if (system (command) != 0) {
6920 g_free (tmp_outfile_name);
6921 g_free (outfile_name);
6928 rename (tmp_outfile_name, outfile_name);
6930 g_free (tmp_outfile_name);
6931 g_free (outfile_name);
6934 if (acfg->aot_opts.save_temps)
6935 printf ("Retained input file.\n");
6937 unlink (acfg->tmpfname);
6942 static MonoAotCompile*
6943 acfg_create (MonoAssembly *ass, guint32 opts)
6945 MonoImage *image = ass->image;
6946 MonoAotCompile *acfg;
6949 acfg = g_new0 (MonoAotCompile, 1);
6950 acfg->methods = g_ptr_array_new ();
6951 acfg->method_indexes = g_hash_table_new (NULL, NULL);
6952 acfg->method_depth = g_hash_table_new (NULL, NULL);
6953 acfg->plt_offset_to_entry = g_hash_table_new (NULL, NULL);
6954 acfg->patch_to_plt_entry = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
6955 acfg->patch_to_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
6956 acfg->patch_to_got_offset_by_type = g_new0 (GHashTable*, MONO_PATCH_INFO_NUM);
6957 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
6958 acfg->patch_to_got_offset_by_type [i] = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
6959 acfg->got_patches = g_ptr_array_new ();
6960 acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
6961 acfg->token_info_hash = g_hash_table_new_full (NULL, NULL, NULL, g_free);
6962 acfg->image_hash = g_hash_table_new (NULL, NULL);
6963 acfg->image_table = g_ptr_array_new ();
6964 acfg->globals = g_ptr_array_new ();
6965 acfg->image = image;
6967 acfg->mempool = mono_mempool_new ();
6968 acfg->extra_methods = g_ptr_array_new ();
6969 acfg->unwind_info_offsets = g_hash_table_new (NULL, NULL);
6970 acfg->unwind_ops = g_ptr_array_new ();
6971 acfg->method_label_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
6972 acfg->method_order = g_ptr_array_new ();
6973 acfg->export_names = g_hash_table_new (NULL, NULL);
6974 InitializeCriticalSection (&acfg->mutex);
6980 acfg_free (MonoAotCompile *acfg)
6984 img_writer_destroy (acfg->w);
6985 for (i = 0; i < acfg->nmethods; ++i)
6987 g_free (acfg->cfgs [i]);
6988 g_free (acfg->cfgs);
6989 g_free (acfg->static_linking_symbol);
6990 g_free (acfg->got_symbol);
6991 g_free (acfg->plt_symbol);
6992 g_ptr_array_free (acfg->methods, TRUE);
6993 g_ptr_array_free (acfg->got_patches, TRUE);
6994 g_ptr_array_free (acfg->image_table, TRUE);
6995 g_ptr_array_free (acfg->globals, TRUE);
6996 g_ptr_array_free (acfg->unwind_ops, TRUE);
6997 g_hash_table_destroy (acfg->method_indexes);
6998 g_hash_table_destroy (acfg->method_depth);
6999 g_hash_table_destroy (acfg->plt_offset_to_entry);
7000 g_hash_table_destroy (acfg->patch_to_plt_entry);
7001 g_hash_table_destroy (acfg->patch_to_got_offset);
7002 g_hash_table_destroy (acfg->method_to_cfg);
7003 g_hash_table_destroy (acfg->token_info_hash);
7004 g_hash_table_destroy (acfg->image_hash);
7005 g_hash_table_destroy (acfg->unwind_info_offsets);
7006 g_hash_table_destroy (acfg->method_label_hash);
7007 g_hash_table_destroy (acfg->export_names);
7008 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
7009 g_hash_table_destroy (acfg->patch_to_got_offset_by_type [i]);
7010 g_free (acfg->patch_to_got_offset_by_type);
7011 mono_mempool_destroy (acfg->mempool);
7016 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
7018 MonoImage *image = ass->image;
7020 MonoAotCompile *acfg;
7021 char *outfile_name, *tmp_outfile_name, *p;
7025 printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
7027 acfg = acfg_create (ass, opts);
7029 memset (&acfg->aot_opts, 0, sizeof (acfg->aot_opts));
7030 acfg->aot_opts.write_symbols = TRUE;
7031 acfg->aot_opts.ntrampolines = 1024;
7032 acfg->aot_opts.nrgctx_trampolines = 1024;
7033 acfg->aot_opts.nimt_trampolines = 128;
7034 acfg->aot_opts.llvm_path = g_strdup ("");
7036 mono_aot_parse_options (aot_options, &acfg->aot_opts);
7038 if (acfg->aot_opts.static_link)
7039 acfg->aot_opts.autoreg = TRUE;
7041 //acfg->aot_opts.print_skipped_methods = TRUE;
7043 #ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
7044 if (acfg->aot_opts.full_aot) {
7045 printf ("--aot=full is not supported on this platform.\n");
7050 if (acfg->aot_opts.static_link)
7051 acfg->aot_opts.asm_writer = TRUE;
7053 if (acfg->aot_opts.soft_debug) {
7054 MonoDebugOptions *opt = mini_get_debug_options ();
7056 opt->mdb_optimizations = TRUE;
7057 opt->gen_seq_points = TRUE;
7059 if (mono_debug_format == MONO_DEBUG_FORMAT_NONE) {
7060 fprintf (stderr, "The soft-debug AOT option requires the --debug option.\n");
7063 acfg->flags |= MONO_AOT_FILE_FLAG_DEBUG;
7066 if (mono_use_llvm) {
7068 acfg->aot_opts.asm_writer = TRUE;
7069 acfg->flags |= MONO_AOT_FILE_FLAG_WITH_LLVM;
7071 if (acfg->aot_opts.soft_debug) {
7072 fprintf (stderr, "The 'soft-debug' option is not supported when compiling with LLVM.\n");
7077 if (acfg->aot_opts.full_aot)
7078 acfg->flags |= MONO_AOT_FILE_FLAG_FULL_AOT;
7080 load_profile_files (acfg);
7082 acfg->num_trampolines [MONO_AOT_TRAMP_SPECIFIC] = acfg->aot_opts.full_aot ? acfg->aot_opts.ntrampolines : 0;
7083 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
7084 acfg->num_trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = acfg->aot_opts.full_aot ? acfg->aot_opts.nrgctx_trampolines : 0;
7086 acfg->num_trampolines [MONO_AOT_TRAMP_IMT_THUNK] = acfg->aot_opts.full_aot ? acfg->aot_opts.nimt_trampolines : 0;
7088 acfg->temp_prefix = img_writer_get_temp_label_prefix (NULL);
7092 acfg->got_symbol_base = g_strdup_printf ("mono_aot_%s_got", acfg->image->assembly->aname.name);
7093 acfg->plt_symbol = g_strdup_printf ("%smono_aot_%s_plt", acfg->llvm_label_prefix, acfg->image->assembly->aname.name);
7094 acfg->assembly_name_sym = g_strdup (acfg->image->assembly->aname.name);
7096 /* Get rid of characters which cannot occur in symbols */
7097 for (p = acfg->got_symbol_base; *p; ++p) {
7098 if (!(isalnum (*p) || *p == '_'))
7101 for (p = acfg->plt_symbol; *p; ++p) {
7102 if (!(isalnum (*p) || *p == '_'))
7105 for (p = acfg->assembly_name_sym; *p; ++p) {
7106 if (!(isalnum (*p) || *p == '_'))
7110 acfg->method_index = 1;
7112 collect_methods (acfg);
7114 acfg->cfgs_size = acfg->methods->len + 32;
7115 acfg->cfgs = g_new0 (MonoCompile*, acfg->cfgs_size);
7117 /* PLT offset 0 is reserved for the PLT trampoline */
7118 acfg->plt_offset = 1;
7123 mono_llvm_create_aot_module (acfg->got_symbol_base);
7127 /* GOT offset 0 is reserved for the address of the current assembly */
7131 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
7132 ji->type = MONO_PATCH_INFO_IMAGE;
7133 ji->data.image = acfg->image;
7135 get_got_offset (acfg, ji);
7137 /* Slot 1 is reserved for the mscorlib got addr */
7138 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
7139 ji->type = MONO_PATCH_INFO_MSCORLIB_GOT_ADDR;
7140 get_got_offset (acfg, ji);
7142 /* This is very common */
7143 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
7144 ji->type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
7145 get_got_offset (acfg, ji);
7150 compile_methods (acfg);
7154 acfg->stats.jit_time = TV_ELAPSED (atv, btv);
7160 if (acfg->aot_opts.asm_only) {
7161 if (acfg->aot_opts.outfile)
7162 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
7164 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->image->name);
7166 acfg->tmpfname = g_strdup ("temp.s");
7169 emit_llvm_file (acfg);
7173 if (!acfg->aot_opts.asm_only && !acfg->aot_opts.asm_writer && bin_writer_supported ()) {
7174 if (acfg->aot_opts.outfile)
7175 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
7177 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
7180 * Can't use g_file_open_tmp () as it will be deleted at exit, and
7181 * it might be in another file system so the rename () won't work.
7183 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
7185 acfg->fp = fopen (tmp_outfile_name, "w");
7187 printf ("Unable to create temporary file '%s': %s\n", tmp_outfile_name, strerror (errno));
7191 acfg->w = img_writer_create (acfg->fp, TRUE);
7192 acfg->use_bin_writer = TRUE;
7195 /* Append to the .s file created by llvm */
7196 /* FIXME: Use multiple files instead */
7197 acfg->fp = fopen (acfg->tmpfname, "a+");
7199 if (acfg->aot_opts.asm_only) {
7200 if (acfg->aot_opts.outfile)
7201 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
7203 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->image->name);
7204 acfg->fp = fopen (acfg->tmpfname, "w+");
7206 int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
7207 acfg->fp = fdopen (i, "w+");
7209 if (acfg->fp == 0) {
7210 fprintf (stderr, "Unable to open file '%s': %s\n", acfg->tmpfname, strerror (errno));
7214 acfg->w = img_writer_create (acfg->fp, FALSE);
7216 tmp_outfile_name = NULL;
7217 outfile_name = NULL;
7220 acfg->got_symbol = g_strdup_printf ("%s%s", acfg->llvm_label_prefix, acfg->got_symbol_base);
7222 /* Compute symbols for methods */
7223 for (i = 0; i < acfg->nmethods; ++i) {
7224 if (acfg->cfgs [i]) {
7225 MonoCompile *cfg = acfg->cfgs [i];
7226 int method_index = get_method_index (acfg, cfg->orig_method);
7228 if (COMPILE_LLVM (cfg))
7229 cfg->asm_symbol = g_strdup_printf ("%s%s", acfg->llvm_label_prefix, cfg->llvm_method_name);
7231 cfg->asm_symbol = g_strdup_printf ("%s%sm_%x", acfg->temp_prefix, acfg->llvm_label_prefix, method_index);
7235 if (!acfg->aot_opts.nodebug)
7236 acfg->dwarf = mono_dwarf_writer_create (acfg->w, NULL, 0, FALSE);
7238 img_writer_emit_start (acfg->w);
7241 mono_dwarf_writer_emit_base_info (acfg->dwarf, mono_unwind_get_cie_program ());
7243 if (acfg->thumb_mixed) {
7246 * This global symbol marks the end of THUMB code, and the beginning of ARM
7247 * code generated by our JIT.
7249 sprintf (symbol, "thumb_end");
7250 emit_section_change (acfg, ".text", 0);
7251 emit_label (acfg, symbol);
7252 emit_zero_bytes (acfg, 16);
7254 fprintf (acfg->fp, ".arm\n");
7261 emit_extra_methods (acfg);
7263 emit_trampolines (acfg);
7265 emit_class_name_table (acfg);
7267 emit_got_info (acfg);
7269 emit_exception_info (acfg);
7271 emit_unwind_info (acfg);
7273 emit_class_info (acfg);
7277 emit_image_table (acfg);
7281 emit_file_info (acfg);
7285 emit_globals (acfg);
7287 emit_autoreg (acfg);
7290 emit_dwarf_info (acfg);
7291 mono_dwarf_writer_close (acfg->dwarf);
7294 emit_mem_end (acfg);
7296 if (acfg->need_pt_gnu_stack) {
7297 /* This is required so the .so doesn't have an executable stack */
7298 /* The bin writer already emits this */
7299 if (!acfg->use_bin_writer)
7300 fprintf (acfg->fp, "\n.section .note.GNU-stack,\"\",@progbits\n");
7305 acfg->stats.gen_time = TV_ELAPSED (atv, btv);
7308 g_assert (acfg->got_offset <= acfg->final_got_size);
7310 printf ("Code: %d Info: %d Ex Info: %d Unwind Info: %d Class Info: %d PLT: %d GOT Info: %d GOT: %d Offsets: %d\n", acfg->stats.code_size, acfg->stats.info_size, acfg->stats.ex_info_size, acfg->stats.unwind_info_size, acfg->stats.class_info_size, acfg->plt_offset, acfg->stats.got_info_size, (int)(acfg->got_offset * sizeof (gpointer)), acfg->stats.offsets_size);
7313 res = img_writer_emit_writeout (acfg->w);
7318 if (acfg->use_bin_writer) {
7319 int err = rename (tmp_outfile_name, outfile_name);
7322 printf ("Unable to rename '%s' to '%s': %s\n", tmp_outfile_name, outfile_name, strerror (errno));
7326 res = compile_asm (acfg);
7333 acfg->stats.link_time = TV_ELAPSED (atv, btv);
7335 printf ("Compiled %d out of %d methods (%d%%)\n", acfg->stats.ccount, acfg->stats.mcount, acfg->stats.mcount ? (acfg->stats.ccount * 100) / acfg->stats.mcount : 100);
7336 if (acfg->stats.genericcount)
7337 printf ("%d methods are generic (%d%%)\n", acfg->stats.genericcount, acfg->stats.mcount ? (acfg->stats.genericcount * 100) / acfg->stats.mcount : 100);
7338 if (acfg->stats.abscount)
7339 printf ("%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
7340 if (acfg->stats.lmfcount)
7341 printf ("%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
7342 if (acfg->stats.ocount)
7343 printf ("%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
7345 printf ("Methods compiled with LLVM: %d (%d%%)\n", acfg->stats.llvm_count, acfg->stats.mcount ? (acfg->stats.llvm_count * 100) / acfg->stats.mcount : 100);
7346 printf ("Methods without GOT slots: %d (%d%%)\n", acfg->stats.methods_without_got_slots, acfg->stats.mcount ? (acfg->stats.methods_without_got_slots * 100) / acfg->stats.mcount : 100);
7347 printf ("Direct calls: %d (%d%%)\n", acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
7349 if (acfg->aot_opts.stats) {
7352 printf ("GOT slot distribution:\n");
7353 for (i = 0; i < MONO_PATCH_INFO_NONE; ++i)
7354 if (acfg->stats.got_slot_types [i])
7355 printf ("\t%s: %d\n", get_patch_name (i), acfg->stats.got_slot_types [i]);
7358 printf ("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);
7370 mono_aot_readonly_field_override (MonoClassField *field)
7376 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)