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 * - reduce the length of the wrapper names.
13 * - aot IMT tables, so we don't have two kinds of aot code.
14 * - optimize the trampolines, generate more code in the arch files.
15 * - make things more consistent with how elf works, for example, use ELF
17 * Remaining generics sharing work:
18 * - optimize the size of the data which is encoded.
19 * - optimize the runtime loading of data:
20 * - the trampoline code calls mono_jit_info_table_find () to find the rgctx,
21 * which loads the debugging+exception handling info for the method. This is a
22 * huge waste of time and code, since the rgctx structure is currently empty.
23 * - every shared method has a MonoGenericJitInfo structure which is only really
24 * used for handling catch clauses with open types, not a very common use case.
28 #include <sys/types.h>
38 #ifndef PLATFORM_WIN32
48 #include <mono/metadata/tabledefs.h>
49 #include <mono/metadata/class.h>
50 #include <mono/metadata/object.h>
51 #include <mono/metadata/tokentype.h>
52 #include <mono/metadata/appdomain.h>
53 #include <mono/metadata/debug-helpers.h>
54 #include <mono/metadata/assembly.h>
55 #include <mono/metadata/metadata-internals.h>
56 #include <mono/metadata/marshal.h>
57 #include <mono/metadata/gc-internal.h>
58 #include <mono/metadata/monitor.h>
59 #include <mono/metadata/mempool-internals.h>
60 #include <mono/metadata/mono-endian.h>
61 #include <mono/metadata/threads-types.h>
62 #include <mono/utils/mono-logger.h>
63 #include <mono/utils/mono-compiler.h>
64 #include <mono/utils/mono-time.h>
65 #include <mono/utils/mono-mmap.h>
68 #include "image-writer.h"
69 #include "dwarfwriter.h"
71 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
73 #define TV_DECLARE(name) gint64 name
74 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
75 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
78 #define SHARED_EXT ".dll"
79 #elif defined(__ppc__) && defined(__MACH__)
80 #define SHARED_EXT ".dylib"
82 #define SHARED_EXT ".so"
85 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
86 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
87 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
89 typedef struct MonoAotOptions {
92 gboolean write_symbols;
93 gboolean metadata_only;
94 gboolean bind_to_runtime_version;
103 gboolean print_skipped_methods;
106 typedef struct MonoAotStats {
107 int ccount, mcount, lmfcount, abscount, gcount, ocount, genericcount;
108 int code_size, info_size, ex_info_size, unwind_info_size, got_size, class_info_size, got_info_size, got_info_offsets_size;
109 int methods_without_got_slots, direct_calls, all_calls;
111 int got_slot_types [MONO_PATCH_INFO_NONE];
112 int jit_time, gen_time, link_time;
115 typedef struct MonoAotCompile {
118 GHashTable *method_indexes;
121 GHashTable *patch_to_plt_offset;
122 GHashTable *plt_offset_to_patch;
123 GHashTable *patch_to_got_offset;
124 GPtrArray *got_patches;
125 GHashTable *image_hash;
126 GHashTable *method_to_cfg;
127 GHashTable *token_info_hash;
128 GPtrArray *extra_methods;
129 GPtrArray *image_table;
132 guint32 *plt_got_info_offsets;
133 guint32 got_offset, plt_offset, plt_got_offset_base;
134 /* Number of GOT entries reserved for trampolines */
135 guint32 num_trampoline_got_entries;
137 guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
138 guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
139 guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
141 MonoAotOptions aot_opts;
144 MonoMemPool *mempool;
147 char *static_linking_symbol;
148 CRITICAL_SECTION mutex;
149 gboolean use_bin_writer;
151 MonoDwarfWriter *dwarf;
155 GHashTable *unwind_info_offsets;
156 GPtrArray *unwind_ops;
157 guint32 unwind_info_offset;
159 GHashTable *method_label_hash;
160 const char *temp_prefix;
163 #define mono_acfg_lock(acfg) EnterCriticalSection (&((acfg)->mutex))
164 #define mono_acfg_unlock(acfg) LeaveCriticalSection (&((acfg)->mutex))
166 #ifdef HAVE_ARRAY_ELEM_INIT
167 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
168 #define MSGSTRFIELD1(line) str##line
169 static const struct msgstr_t {
170 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
171 #include "patch-info.h"
174 #define PATCH_INFO(a,b) b,
175 #include "patch-info.h"
178 static const gint16 opidx [] = {
179 #define PATCH_INFO(a,b) [MONO_PATCH_INFO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
180 #include "patch-info.h"
184 static G_GNUC_UNUSED const char*
185 get_patch_name (int info)
187 return (const char*)&opstr + opidx [info];
191 #define PATCH_INFO(a,b) b,
192 static const char* const
193 patch_types [MONO_PATCH_INFO_NUM + 1] = {
194 #include "patch-info.h"
199 get_patch_name (int info)
201 return patch_types [info];
206 /* Wrappers around the image writer functions */
209 emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
211 img_writer_emit_section_change (acfg->w, section_name, subsection_index);
215 emit_push_section (MonoAotCompile *acfg, const char *section_name, int subsection)
217 img_writer_emit_push_section (acfg->w, section_name, subsection);
221 emit_pop_section (MonoAotCompile *acfg)
223 img_writer_emit_pop_section (acfg->w);
227 emit_local_symbol (MonoAotCompile *acfg, const char *name, const char *end_label, gboolean func)
229 img_writer_emit_local_symbol (acfg->w, name, end_label, func);
233 emit_label (MonoAotCompile *acfg, const char *name)
235 img_writer_emit_label (acfg->w, name);
239 emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
241 img_writer_emit_bytes (acfg->w, buf, size);
245 emit_string (MonoAotCompile *acfg, const char *value)
247 img_writer_emit_string (acfg->w, value);
251 emit_line (MonoAotCompile *acfg)
253 img_writer_emit_line (acfg->w);
257 emit_alignment (MonoAotCompile *acfg, int size)
259 img_writer_emit_alignment (acfg->w, size);
263 emit_pointer_unaligned (MonoAotCompile *acfg, const char *target)
265 img_writer_emit_pointer_unaligned (acfg->w, target);
269 emit_pointer (MonoAotCompile *acfg, const char *target)
271 img_writer_emit_pointer (acfg->w, target);
275 emit_int16 (MonoAotCompile *acfg, int value)
277 img_writer_emit_int16 (acfg->w, value);
281 emit_int32 (MonoAotCompile *acfg, int value)
283 img_writer_emit_int32 (acfg->w, value);
287 emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
289 img_writer_emit_symbol_diff (acfg->w, end, start, offset);
293 emit_zero_bytes (MonoAotCompile *acfg, int num)
295 img_writer_emit_zero_bytes (acfg->w, num);
299 emit_byte (MonoAotCompile *acfg, guint8 val)
301 img_writer_emit_byte (acfg->w, val);
304 static G_GNUC_UNUSED void
305 emit_global_inner (MonoAotCompile *acfg, const char *name, gboolean func)
307 img_writer_emit_global (acfg->w, name, func);
311 emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
313 if (acfg->aot_opts.no_dlsym) {
314 g_ptr_array_add (acfg->globals, g_strdup (name));
316 img_writer_emit_global (acfg->w, name, func);
321 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
323 img_writer_emit_section_change (acfg->w, ".text", 1);
324 emit_global (acfg, name, FALSE);
325 img_writer_emit_label (acfg->w, name);
326 img_writer_emit_string (acfg->w, value);
329 static G_GNUC_UNUSED void
330 emit_uleb128 (MonoAotCompile *acfg, guint32 value)
333 guint8 b = value & 0x7f;
335 if (value != 0) /* more bytes to come */
341 static G_GNUC_UNUSED void
342 emit_sleb128 (MonoAotCompile *acfg, gint64 value)
345 gboolean negative = (value < 0);
352 /* the following is unnecessary if the
353 * implementation of >>= uses an arithmetic rather
354 * than logical shift for a signed left operand
358 value |= - ((gint64)1 <<(size - 7));
359 /* sign bit of byte is second high order bit (0x40) */
360 if ((value == 0 && !(byte & 0x40)) ||
361 (value == -1 && (byte & 0x40)))
365 emit_byte (acfg, byte);
369 static G_GNUC_UNUSED void
370 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
375 guint8 b = value & 0x7f;
377 if (value != 0) /* more bytes to come */
385 static G_GNUC_UNUSED void
386 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
389 gboolean negative = (value < 0);
397 /* the following is unnecessary if the
398 * implementation of >>= uses an arithmetic rather
399 * than logical shift for a signed left operand
403 value |= - (1 <<(size - 7));
404 /* sign bit of byte is second high order bit (0x40) */
405 if ((value == 0 && !(byte & 0x40)) ||
406 (value == -1 && (byte & 0x40)))
416 /* ARCHITECTURE SPECIFIC CODE */
418 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM)
419 #define EMIT_DWARF_INFO 1
422 #if defined(TARGET_ARM)
423 #define AOT_FUNC_ALIGNMENT 4
425 #define AOT_FUNC_ALIGNMENT 16
429 * arch_emit_direct_call:
431 * Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
435 arch_emit_direct_call (MonoAotCompile *acfg, const char *target, int *call_size)
437 #if defined(TARGET_X86) || defined(TARGET_AMD64)
438 /* Need to make sure this is exactly 5 bytes long */
439 emit_byte (acfg, '\xe8');
440 emit_symbol_diff (acfg, target, ".", -4);
442 #elif defined(TARGET_ARM)
443 if (acfg->use_bin_writer) {
450 img_writer_emit_reloc (acfg->w, R_ARM_CALL, target, -8);
451 emit_bytes (acfg, buf, 4);
453 img_writer_emit_unset_mode (acfg->w);
454 fprintf (acfg->fp, "bl %s\n", target);
458 g_assert_not_reached ();
462 #ifdef MONO_ARCH_AOT_SUPPORTED
464 * arch_emit_got_offset:
466 * The memory pointed to by CODE should hold native code for computing the GOT
467 * address. Emit this code while patching it with the offset between code and
468 * the GOT. CODE_SIZE is set to the number of bytes emitted.
471 arch_emit_got_offset (MonoAotCompile *acfg, guint8 *code, int *code_size)
473 guint32 offset = mono_arch_get_patch_offset (code);
474 emit_bytes (acfg, code, offset);
475 emit_symbol_diff (acfg, acfg->got_symbol, ".", offset);
477 *code_size = offset + 4;
481 * arch_emit_got_access:
483 * The memory pointed to by CODE should hold native code for loading a GOT
484 * slot. Emit this code while patching it so it accesses the GOT slot GOT_SLOT.
485 * CODE_SIZE is set to the number of bytes emitted.
488 arch_emit_got_access (MonoAotCompile *acfg, guint8 *code, int got_slot, int *code_size)
490 /* Emit beginning of instruction */
491 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
493 /* Emit the offset */
495 emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
496 #elif defined(TARGET_X86)
497 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
498 #elif defined(TARGET_ARM)
499 emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
501 g_assert_not_reached ();
504 *code_size = mono_arch_get_patch_offset (code) + 4;
510 * arch_emit_plt_entry:
512 * Emit code for the PLT entry with index INDEX.
515 arch_emit_plt_entry (MonoAotCompile *acfg, int index)
517 #if defined(TARGET_X86)
519 /* It is filled up during loading by the AOT loader. */
520 emit_zero_bytes (acfg, 16);
522 /* Need to make sure this is 9 bytes long */
523 emit_byte (acfg, '\xe9');
524 emit_symbol_diff (acfg, "plt", ".", -4);
525 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
527 #elif defined(TARGET_AMD64)
529 * We can't emit jumps because they are 32 bits only so they can't be patched.
530 * So we make indirect calls through GOT entries which are patched by the AOT
531 * loader to point to .Lpd entries.
532 * An x86_64 plt entry is 10 bytes long, init_plt () depends on this.
534 /* jmpq *<offset>(%rip) */
535 emit_byte (acfg, '\xff');
536 emit_byte (acfg, '\x25');
537 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) -4);
538 /* Used by mono_aot_get_plt_info_offset */
539 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
540 #elif defined(TARGET_ARM)
545 * - optimize OP_AOTCONST implementation
546 * - optimize the PLT entries
547 * - optimize SWITCH AOT implementation
548 * - implement IMT support
551 if (acfg->use_bin_writer && FALSE) {
552 /* FIXME: mono_arch_patch_plt_entry () needs to decode this */
553 /* We only emit 1 relocation since we implement it ourselves anyway */
554 img_writer_emit_reloc (acfg->w, R_ARM_ALU_PC_G0_NC, acfg->got_symbol, ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) - 8);
555 /* FIXME: A 2 instruction encoding is sufficient in most cases */
556 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
557 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, 0, 0);
558 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, 0);
559 emit_bytes (acfg, buf, code - buf);
560 /* Used by mono_aot_get_plt_info_offset */
561 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
563 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
564 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
565 emit_bytes (acfg, buf, code - buf);
566 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) - 4);
567 /* Used by mono_aot_get_plt_info_offset */
568 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
571 g_assert_not_reached ();
576 * arch_emit_specific_trampoline:
578 * Emit code for a specific trampoline. OFFSET is the offset of the first of
579 * two GOT slots which contain the generic trampoline address and the trampoline
580 * argument. TRAMP_SIZE is set to the size of the emitted trampoline.
583 arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
586 * The trampolines created here are variations of the specific
587 * trampolines created in mono_arch_create_specific_trampoline (). The
589 * - the generic trampoline address is taken from a got slot.
590 * - the offset of the got slot where the trampoline argument is stored
591 * is embedded in the instruction stream, and the generic trampoline
592 * can load the argument by loading the offset, adding it to the
593 * address of the trampoline to get the address of the got slot, and
594 * loading the argument from there.
595 * - all the trampolines should be of the same length.
597 #if defined(TARGET_AMD64)
598 /* This should be exactly 16 bytes long */
600 /* call *<offset>(%rip) */
601 emit_byte (acfg, '\x41');
602 emit_byte (acfg, '\xff');
603 emit_byte (acfg, '\x15');
604 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
605 /* This should be relative to the start of the trampoline */
606 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 19);
607 emit_zero_bytes (acfg, 5);
608 #elif defined(TARGET_ARM)
612 /* This should be exactly 20 bytes long */
615 ARM_PUSH (code, 0x5fff);
616 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 4);
617 /* Load the value from the GOT */
618 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
620 ARM_BLX_REG (code, ARMREG_R1);
622 g_assert (code - buf == 16);
625 emit_bytes (acfg, buf, code - buf);
627 * Only one offset is needed, since the second one would be equal to the
630 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 4);
631 //emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 8);
633 g_assert_not_reached ();
638 * arch_emit_unbox_trampoline:
640 * Emit code for the unbox trampoline for METHOD used in the full-aot case.
641 * CALL_TARGET is the symbol pointing to the native code of METHOD.
644 arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoMethod *method, MonoGenericSharingContext *gsctx, const char *call_target)
646 #if defined(TARGET_AMD64)
651 this_reg = mono_arch_get_this_arg_reg (mono_method_signature (method), gsctx, NULL);
653 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
655 emit_bytes (acfg, buf, code - buf);
657 emit_byte (acfg, '\xe9');
658 emit_symbol_diff (acfg, call_target, ".", -4);
659 #elif defined(TARGET_ARM)
666 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
669 ARM_ADD_REG_IMM8 (code, this_pos, this_pos, sizeof (MonoObject));
671 emit_bytes (acfg, buf, code - buf);
673 if (acfg->use_bin_writer) {
680 img_writer_emit_reloc (acfg->w, R_ARM_JUMP24, call_target, -8);
681 emit_bytes (acfg, buf, 4);
683 fprintf (acfg->fp, "\n\tb %s\n", call_target);
686 g_assert_not_reached ();
691 * arch_emit_static_rgctx_trampoline:
693 * Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
694 * two GOT slots which contain the rgctx argument, and the method to jump to.
695 * TRAMP_SIZE is set to the size of the emitted trampoline.
696 * These kinds of trampolines cannot be enumerated statically, since there could
697 * be one trampoline per method instantiation, so we emit the same code for all
698 * trampolines, and parameterize them using two GOT slots.
701 arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
703 #if defined(TARGET_AMD64)
704 /* This should be exactly 13 bytes long */
707 /* mov <OFFSET>(%rip), %r10 */
708 emit_byte (acfg, '\x4d');
709 emit_byte (acfg, '\x8b');
710 emit_byte (acfg, '\x15');
711 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
713 /* jmp *<offset>(%rip) */
714 emit_byte (acfg, '\xff');
715 emit_byte (acfg, '\x25');
716 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
717 #elif defined(TARGET_ARM)
721 /* This should be exactly 24 bytes long */
724 /* Load rgctx value */
725 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8);
726 ARM_LDR_REG_REG (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, ARMREG_R1);
727 /* Load branch addr + branch */
728 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 4);
729 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
731 g_assert (code - buf == 16);
734 emit_bytes (acfg, buf, code - buf);
735 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 8);
736 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 4);
738 g_assert_not_reached ();
743 * arch_emit_imt_thunk:
745 * Emit an IMT thunk usable in full-aot mode. The thunk uses 1 got slot which
746 * points to an array of pointer pairs. The pairs of the form [key, ptr], where
747 * key is the IMT key, and ptr holds the address of a memory location holding
748 * the address to branch to if the IMT arg matches the key. The array is
749 * terminated by a pair whose key is NULL, and whose ptr is the address of the
751 * TRAMP_SIZE is set to the size of the emitted trampoline.
754 arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
756 #if defined(TARGET_AMD64)
760 code = buf = g_malloc (256);
762 /* FIXME: Optimize this, i.e. use binary search etc. */
763 /* Maybe move the body into a separate function (slower, but much smaller) */
765 /* R10 is a free register */
768 amd64_alu_membase_imm (code, X86_CMP, AMD64_R10, 0, 0);
770 amd64_branch8 (code, X86_CC_Z, FALSE, 0);
773 amd64_alu_membase_reg (code, X86_CMP, AMD64_R10, 0, MONO_ARCH_IMT_REG);
775 amd64_branch8 (code, X86_CC_Z, FALSE, 0);
778 amd64_alu_reg_imm (code, X86_ADD, AMD64_R10, 2 * sizeof (gpointer));
779 amd64_jump_code (code, labels [0]);
782 mono_amd64_patch (labels [2], code);
783 amd64_mov_reg_membase (code, AMD64_R10, AMD64_R10, sizeof (gpointer), 8);
784 amd64_jump_membase (code, AMD64_R10, 0);
788 mono_amd64_patch (labels [1], code);
789 x86_breakpoint (code);
791 /* mov <OFFSET>(%rip), %r10 */
792 emit_byte (acfg, '\x4d');
793 emit_byte (acfg, '\x8b');
794 emit_byte (acfg, '\x15');
795 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
797 emit_bytes (acfg, buf, code - buf);
799 *tramp_size = code - buf + 7;
800 #elif defined(TARGET_ARM)
802 guint8 *code, *code2, *labels [16];
806 /* The IMT method is in v5 */
808 /* Only IP is available, but we need at least two free registers */
809 ARM_PUSH1 (code, ARMREG_R1);
811 /* Load the parameter from the GOT */
812 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
813 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
816 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_IP, 0);
817 ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
819 ARM_B_COND (code, ARMCOND_EQ, 0);
821 /* End-of-loop check */
822 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
824 ARM_B_COND (code, ARMCOND_EQ, 0);
827 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_IP, sizeof (gpointer) * 2);
830 arm_patch (labels [4], labels [1]);
833 arm_patch (labels [2], code);
834 ARM_POP1 (code, ARMREG_R1);
835 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, 4);
836 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, 0);
839 arm_patch (labels [3], code);
844 ARM_LDR_IMM (code2, ARMREG_IP, ARMREG_PC, (code - (labels [0] + 8)));
846 emit_bytes (acfg, buf, code - buf);
847 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + (code - (labels [0] + 8)) - 4);
849 *tramp_size = code - buf + 4;
851 g_assert_not_reached ();
856 * arch_get_cie_program:
858 * Get the unwind bytecode for the DWARF CIE.
861 arch_get_cie_program (void)
866 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
867 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
875 /* END OF ARCH SPECIFIC CODE */
878 mono_get_field_token (MonoClassField *field)
880 MonoClass *klass = field->parent;
883 for (i = 0; i < klass->field.count; ++i) {
884 if (field == &klass->fields [i])
885 return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
888 g_assert_not_reached ();
893 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
897 //printf ("ENCODE: %d 0x%x.\n", value, value);
900 * Same encoding as the one used in the metadata, extended to handle values
901 * greater than 0x1fffffff.
903 if ((value >= 0) && (value <= 127))
905 else if ((value >= 0) && (value <= 16383)) {
906 p [0] = 0x80 | (value >> 8);
907 p [1] = value & 0xff;
909 } else if ((value >= 0) && (value <= 0x1fffffff)) {
910 p [0] = (value >> 24) | 0xc0;
911 p [1] = (value >> 16) & 0xff;
912 p [2] = (value >> 8) & 0xff;
913 p [3] = value & 0xff;
918 p [1] = (value >> 24) & 0xff;
919 p [2] = (value >> 16) & 0xff;
920 p [3] = (value >> 8) & 0xff;
921 p [4] = value & 0xff;
929 get_image_index (MonoAotCompile *cfg, MonoImage *image)
933 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
937 index = g_hash_table_size (cfg->image_hash);
938 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
939 g_ptr_array_add (cfg->image_table, image);
945 find_typespec_for_class (MonoAotCompile *acfg, MonoClass *klass)
950 /* FIXME: Search referenced images as well */
951 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
952 k = mono_class_get_full (acfg->image, MONO_TOKEN_TYPE_SPEC | (i + 1), NULL);
957 if (i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows)
958 return MONO_TOKEN_TYPE_SPEC | (i + 1);
964 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf);
969 * Encode a reference to KLASS. We use our home-grown encoding instead of the
970 * standard metadata encoding.
973 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
977 if (klass->generic_class) {
979 g_assert (klass->type_token);
981 /* Find a typespec for a class if possible */
982 token = find_typespec_for_class (acfg, klass);
984 encode_value (token, p, &p);
985 encode_value (get_image_index (acfg, acfg->image), p, &p);
987 MonoClass *gclass = klass->generic_class->container_class;
988 MonoGenericInst *inst = klass->generic_class->context.class_inst;
991 /* Encode it ourselves */
993 encode_value (MONO_TOKEN_TYPE_SPEC, p, &p);
994 encode_value (MONO_TYPE_GENERICINST, p, &p);
995 encode_klass_ref (acfg, gclass, p, &p);
996 encode_value (inst->type_argc, p, &p);
997 for (i = 0; i < inst->type_argc; ++i)
998 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
1000 } else if (klass->type_token) {
1001 g_assert (mono_metadata_token_code (klass->type_token) == MONO_TOKEN_TYPE_DEF);
1002 encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, p, &p);
1003 encode_value (get_image_index (acfg, klass->image), p, &p);
1004 } else if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR)) {
1005 MonoGenericContainer *container = mono_type_get_generic_param_owner (&klass->byval_arg);
1006 g_assert (container);
1009 encode_value (MONO_TOKEN_TYPE_SPEC, p, &p);
1010 encode_value (klass->byval_arg.type, p, &p);
1012 encode_value (mono_type_get_generic_param_num (&klass->byval_arg), p, &p);
1014 encode_value (container->is_method, p, &p);
1015 if (container->is_method)
1016 encode_method_ref (acfg, container->owner.method, p, &p);
1018 encode_klass_ref (acfg, container->owner.klass, p, &p);
1021 g_assert (klass->rank > 0);
1022 encode_value (MONO_TOKEN_TYPE_DEF, p, &p);
1023 encode_value (get_image_index (acfg, klass->image), p, &p);
1024 encode_value (klass->rank, p, &p);
1025 encode_klass_ref (acfg, klass->element_class, p, &p);
1031 encode_field_info (MonoAotCompile *cfg, MonoClassField *field, guint8 *buf, guint8 **endbuf)
1033 guint32 token = mono_get_field_token (field);
1036 encode_klass_ref (cfg, field->parent, p, &p);
1037 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
1038 encode_value (token - MONO_TOKEN_FIELD_DEF, p, &p);
1043 encode_generic_context (MonoAotCompile *acfg, MonoGenericContext *context, guint8 *buf, guint8 **endbuf)
1047 MonoGenericInst *inst;
1049 /* Encode the context */
1050 inst = context->class_inst;
1051 encode_value (inst ? 1 : 0, p, &p);
1053 encode_value (inst->type_argc, p, &p);
1054 for (i = 0; i < inst->type_argc; ++i)
1055 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
1057 inst = context->method_inst;
1058 encode_value (inst ? 1 : 0, p, &p);
1060 encode_value (inst->type_argc, p, &p);
1061 for (i = 0; i < inst->type_argc; ++i)
1062 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
1068 #define MAX_IMAGE_INDEX 250
1071 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf)
1073 guint32 image_index = get_image_index (acfg, method->klass->image);
1074 guint32 token = method->token;
1075 MonoJumpInfoToken *ji;
1080 * The encoding for most methods is as follows:
1081 * - image index encoded as a leb128
1082 * - token index encoded as a leb128
1083 * Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
1084 * types of method encodings.
1087 g_assert (image_index < MONO_AOT_METHODREF_MIN);
1089 /* Mark methods which can't use aot trampolines because they need the further
1090 * processing in mono_magic_trampoline () which requires a MonoMethod*.
1092 if ((method->is_generic && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) ||
1093 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
1094 encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE << 24), p, &p);
1097 * Some wrapper methods are shared using their signature, encode their
1098 * stringified signature instead.
1099 * FIXME: Optimize disk usage
1102 if (method->wrapper_type) {
1103 if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1104 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1105 if (mono_marshal_method_from_wrapper (method) != method) {
1106 /* Direct wrapper, encode it normally */
1108 name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
1111 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
1112 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1113 name = g_strdup_printf ("(wrapper delegate-invoke):%s (%s)", method->name, tmpsig);
1115 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_BEGIN_INVOKE) {
1116 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1117 name = g_strdup_printf ("(wrapper delegate-begin-invoke):%s (%s)", method->name, tmpsig);
1119 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_END_INVOKE) {
1120 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1121 name = g_strdup_printf ("(wrapper delegate-end-invoke):%s (%s)", method->name, tmpsig);
1127 encode_value ((MONO_AOT_METHODREF_WRAPPER_NAME << 24), p, &p);
1128 strcpy ((char*)p, name);
1129 p += strlen (name) + 1;
1131 } else if (method->wrapper_type) {
1132 encode_value ((MONO_AOT_METHODREF_WRAPPER << 24), p, &p);
1134 encode_value (method->wrapper_type, p, &p);
1136 switch (method->wrapper_type) {
1137 case MONO_WRAPPER_REMOTING_INVOKE:
1138 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
1139 case MONO_WRAPPER_XDOMAIN_INVOKE: {
1142 m = mono_marshal_method_from_wrapper (method);
1144 encode_method_ref (acfg, m, p, &p);
1147 case MONO_WRAPPER_PROXY_ISINST:
1148 case MONO_WRAPPER_LDFLD:
1149 case MONO_WRAPPER_LDFLDA:
1150 case MONO_WRAPPER_STFLD:
1151 case MONO_WRAPPER_ISINST: {
1152 MonoClass *proxy_class = (MonoClass*)mono_marshal_method_from_wrapper (method);
1153 encode_klass_ref (acfg, proxy_class, p, &p);
1156 case MONO_WRAPPER_LDFLD_REMOTE:
1157 case MONO_WRAPPER_STFLD_REMOTE:
1159 case MONO_WRAPPER_ALLOC: {
1160 int alloc_type = mono_gc_get_managed_allocator_type (method);
1161 g_assert (alloc_type != -1);
1162 encode_value (alloc_type, p, &p);
1165 case MONO_WRAPPER_STELEMREF:
1167 case MONO_WRAPPER_UNKNOWN:
1168 if (strcmp (method->name, "FastMonitorEnter") == 0)
1169 encode_value (MONO_AOT_WRAPPER_MONO_ENTER, p, &p);
1170 else if (strcmp (method->name, "FastMonitorExit") == 0)
1171 encode_value (MONO_AOT_WRAPPER_MONO_EXIT, p, &p);
1173 g_assert_not_reached ();
1175 case MONO_WRAPPER_SYNCHRONIZED:
1176 case MONO_WRAPPER_MANAGED_TO_NATIVE:
1177 case MONO_WRAPPER_RUNTIME_INVOKE: {
1180 m = mono_marshal_method_from_wrapper (method);
1182 g_assert (m != method);
1183 encode_method_ref (acfg, m, p, &p);
1187 g_assert_not_reached ();
1189 } else if (mono_method_signature (method)->is_inflated) {
1191 * This is a generic method, find the original token which referenced it and
1193 * Obtain the token from information recorded by the JIT.
1195 ji = g_hash_table_lookup (acfg->token_info_hash, method);
1197 image_index = get_image_index (acfg, ji->image);
1198 g_assert (image_index < MAX_IMAGE_INDEX);
1201 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
1202 encode_value (image_index, p, &p);
1203 encode_value (token, p, &p);
1205 MonoMethod *declaring;
1206 MonoGenericContext *context = mono_method_get_context (method);
1208 g_assert (method->is_inflated);
1209 declaring = ((MonoMethodInflated*)method)->declaring;
1212 * This might be a non-generic method of a generic instance, which
1213 * doesn't have a token since the reference is generated by the JIT
1214 * like Nullable:Box/Unbox, or by generic sharing.
1217 encode_value ((MONO_AOT_METHODREF_GINST << 24), p, &p);
1218 /* Encode the klass */
1219 encode_klass_ref (acfg, method->klass, p, &p);
1220 /* Encode the method */
1221 image_index = get_image_index (acfg, method->klass->image);
1222 g_assert (image_index < MAX_IMAGE_INDEX);
1223 g_assert (declaring->token);
1224 token = declaring->token;
1225 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
1226 encode_value (image_index, p, &p);
1227 encode_value (token, p, &p);
1228 encode_generic_context (acfg, context, p, &p);
1230 } else if (token == 0) {
1231 /* This might be a method of a constructed type like int[,].Set */
1232 /* Obtain the token from information recorded by the JIT */
1233 ji = g_hash_table_lookup (acfg->token_info_hash, method);
1235 image_index = get_image_index (acfg, ji->image);
1236 g_assert (image_index < MAX_IMAGE_INDEX);
1239 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
1240 encode_value (image_index, p, &p);
1241 encode_value (token, p, &p);
1244 g_assert (method->klass->rank);
1246 /* Encode directly */
1247 encode_value ((MONO_AOT_METHODREF_ARRAY << 24), p, &p);
1248 encode_klass_ref (acfg, method->klass, p, &p);
1249 if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank)
1250 encode_value (0, p, &p);
1251 else if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank * 2)
1252 encode_value (1, p, &p);
1253 else if (!strcmp (method->name, "Get"))
1254 encode_value (2, p, &p);
1255 else if (!strcmp (method->name, "Address"))
1256 encode_value (3, p, &p);
1257 else if (!strcmp (method->name, "Set"))
1258 encode_value (4, p, &p);
1260 g_assert_not_reached ();
1263 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
1264 encode_value ((image_index << 24) | mono_metadata_token_index (token), p, &p);
1270 compare_patches (gconstpointer a, gconstpointer b)
1274 i = (*(MonoJumpInfo**)a)->ip.i;
1275 j = (*(MonoJumpInfo**)b)->ip.i;
1289 * Return whenever PATCH_INFO refers to a direct call, and thus requires a
1292 static inline gboolean
1293 is_plt_patch (MonoJumpInfo *patch_info)
1295 switch (patch_info->type) {
1296 case MONO_PATCH_INFO_METHOD:
1297 case MONO_PATCH_INFO_INTERNAL_METHOD:
1298 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
1299 case MONO_PATCH_INFO_ICALL_ADDR:
1300 case MONO_PATCH_INFO_CLASS_INIT:
1301 case MONO_PATCH_INFO_RGCTX_FETCH:
1302 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1303 case MONO_PATCH_INFO_MONITOR_ENTER:
1304 case MONO_PATCH_INFO_MONITOR_EXIT:
1312 get_plt_offset (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
1316 if (is_plt_patch (patch_info)) {
1317 int idx = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_plt_offset, patch_info));
1319 if (patch_info->type == MONO_PATCH_INFO_METHOD && (patch_info->data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
1321 * Allocate a separate PLT slot for each such patch, since some plt
1322 * entries will refer to the method itself, and some will refer to
1331 MonoJumpInfo *new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
1333 res = acfg->plt_offset;
1334 g_hash_table_insert (acfg->plt_offset_to_patch, GUINT_TO_POINTER (res), new_ji);
1335 g_hash_table_insert (acfg->patch_to_plt_offset, new_ji, GUINT_TO_POINTER (res));
1336 acfg->plt_offset ++;
1346 * Returns the offset of the GOT slot where the runtime object resulting from resolving
1347 * JI could be found if it exists, otherwise allocates a new one.
1350 get_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
1354 got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_got_offset, ji));
1356 return got_offset - 1;
1358 got_offset = acfg->got_offset;
1359 acfg->got_offset ++;
1361 acfg->stats.got_slots ++;
1362 acfg->stats.got_slot_types [ji->type] ++;
1364 g_hash_table_insert (acfg->patch_to_got_offset, ji, GUINT_TO_POINTER (got_offset + 1));
1365 g_ptr_array_add (acfg->got_patches, ji);
1370 /* Add a method to the list of methods which need to be emitted */
1372 add_method_with_index (MonoAotCompile *acfg, MonoMethod *method, int index, gboolean extra)
1375 if (!g_hash_table_lookup (acfg->method_indexes, method)) {
1376 g_ptr_array_add (acfg->methods, method);
1377 g_hash_table_insert (acfg->method_indexes, method, GUINT_TO_POINTER (index + 1));
1378 acfg->nmethods = acfg->methods->len + 1;
1381 if (method->wrapper_type || extra)
1382 g_ptr_array_add (acfg->extra_methods, method);
1386 get_method_index (MonoAotCompile *acfg, MonoMethod *method)
1388 int index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
1396 add_method_full (MonoAotCompile *acfg, MonoMethod *method, gboolean extra)
1400 index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
1404 index = acfg->method_index;
1405 add_method_with_index (acfg, method, index, extra);
1407 /* FIXME: Fix quadratic behavior */
1408 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (index));
1410 acfg->method_index ++;
1416 add_method (MonoAotCompile *acfg, MonoMethod *method)
1418 return add_method_full (acfg, method, FALSE);
1422 add_extra_method (MonoAotCompile *acfg, MonoMethod *method)
1424 add_method_full (acfg, method, TRUE);
1428 add_jit_icall_wrapper (gpointer key, gpointer value, gpointer user_data)
1430 MonoAotCompile *acfg = user_data;
1431 MonoJitICallInfo *callinfo = value;
1432 MonoMethod *wrapper;
1438 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
1439 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
1442 add_method (acfg, wrapper);
1446 get_runtime_invoke_sig (MonoMethodSignature *sig)
1448 MonoMethodBuilder *mb;
1451 mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
1452 m = mono_mb_create_method (mb, sig, 16);
1453 return mono_marshal_get_runtime_invoke (m, FALSE);
1457 add_wrappers (MonoAotCompile *acfg)
1459 MonoMethod *method, *m;
1460 int i, j, nallocators;
1461 MonoMethodSignature *sig, *csig;
1465 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
1466 * so there is only one wrapper of a given type, or inlining their contents into their
1471 * FIXME: This depends on the fact that different wrappers have different
1475 /* FIXME: Collect these automatically */
1477 /* Runtime invoke wrappers */
1479 /* void runtime-invoke () [.cctor] */
1480 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
1481 csig->ret = &mono_defaults.void_class->byval_arg;
1482 add_method (acfg, get_runtime_invoke_sig (csig));
1484 /* void runtime-invoke () [Finalize] */
1485 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
1487 csig->ret = &mono_defaults.void_class->byval_arg;
1488 add_method (acfg, get_runtime_invoke_sig (csig));
1490 /* void runtime-invoke (string) [exception ctor] */
1491 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
1493 csig->ret = &mono_defaults.void_class->byval_arg;
1494 csig->params [0] = &mono_defaults.string_class->byval_arg;
1495 add_method (acfg, get_runtime_invoke_sig (csig));
1497 /* void runtime-invoke (string, string) [exception ctor] */
1498 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
1500 csig->ret = &mono_defaults.void_class->byval_arg;
1501 csig->params [0] = &mono_defaults.string_class->byval_arg;
1502 csig->params [1] = &mono_defaults.string_class->byval_arg;
1503 add_method (acfg, get_runtime_invoke_sig (csig));
1505 /* string runtime-invoke () [Exception.ToString ()] */
1506 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
1508 csig->ret = &mono_defaults.string_class->byval_arg;
1509 add_method (acfg, get_runtime_invoke_sig (csig));
1511 /* void runtime-invoke (string, Exception) [exception ctor] */
1512 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
1514 csig->ret = &mono_defaults.void_class->byval_arg;
1515 csig->params [0] = &mono_defaults.string_class->byval_arg;
1516 csig->params [1] = &mono_defaults.exception_class->byval_arg;
1517 add_method (acfg, get_runtime_invoke_sig (csig));
1519 /* Assembly runtime-invoke (string, bool) [DoAssemblyResolve] */
1520 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
1522 csig->ret = &(mono_class_from_name (
1523 mono_defaults.corlib, "System.Reflection", "Assembly"))->byval_arg;
1524 csig->params [0] = &mono_defaults.string_class->byval_arg;
1525 csig->params [1] = &mono_defaults.boolean_class->byval_arg;
1526 add_method (acfg, get_runtime_invoke_sig (csig));
1528 /* runtime-invoke used by finalizers */
1529 add_method (acfg, mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE));
1531 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1533 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1534 gboolean skip = FALSE;
1536 method = mono_get_method (acfg->image, token, NULL);
1538 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1539 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1540 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1543 if (method->is_generic || method->klass->generic_container)
1546 /* Skip methods which can not be handled by get_runtime_invoke () */
1547 sig = mono_method_signature (method);
1548 if ((sig->ret->type == MONO_TYPE_PTR) ||
1549 (sig->ret->type == MONO_TYPE_TYPEDBYREF))
1552 for (j = 0; j < sig->param_count; j++) {
1553 if (sig->params [j]->type == MONO_TYPE_TYPEDBYREF)
1558 add_method (acfg, mono_marshal_get_runtime_invoke (method, FALSE));
1561 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
1562 MonoMethodDesc *desc;
1563 MonoMethod *orig_method;
1565 /* JIT icall wrappers */
1566 /* FIXME: locking */
1567 g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
1569 /* Managed Allocators */
1570 nallocators = mono_gc_get_managed_allocator_types ();
1571 for (i = 0; i < nallocators; ++i) {
1572 m = mono_gc_get_managed_allocator_by_type (i);
1574 add_method (acfg, m);
1578 add_method (acfg, mono_marshal_get_stelemref ());
1580 /* Monitor Enter/Exit */
1581 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
1582 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
1583 g_assert (orig_method);
1584 mono_method_desc_free (desc);
1585 method = mono_monitor_get_fast_path (orig_method);
1587 add_method (acfg, method);
1589 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
1590 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
1591 g_assert (orig_method);
1592 mono_method_desc_free (desc);
1593 method = mono_monitor_get_fast_path (orig_method);
1595 add_method (acfg, method);
1598 /* remoting-invoke wrappers */
1599 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1600 MonoMethodSignature *sig;
1602 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1603 method = mono_get_method (acfg->image, token, NULL);
1605 sig = mono_method_signature (method);
1607 if (sig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
1608 m = mono_marshal_get_remoting_invoke_with_check (method);
1610 add_method (acfg, m);
1614 /* delegate-invoke wrappers */
1615 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
1618 token = MONO_TOKEN_TYPE_DEF | (i + 1);
1619 klass = mono_class_get (acfg->image, token);
1621 if (klass->delegate && klass != mono_defaults.delegate_class && klass != mono_defaults.multicastdelegate_class && !klass->generic_container) {
1622 method = mono_get_delegate_invoke (klass);
1624 m = mono_marshal_get_delegate_invoke (method, NULL);
1626 add_method (acfg, m);
1628 method = mono_class_get_method_from_name_flags (klass, "BeginInvoke", -1, 0);
1629 add_method (acfg, mono_marshal_get_delegate_begin_invoke (method));
1631 method = mono_class_get_method_from_name_flags (klass, "EndInvoke", -1, 0);
1632 add_method (acfg, mono_marshal_get_delegate_end_invoke (method));
1636 /* Synchronized wrappers */
1637 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1638 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1639 method = mono_get_method (acfg->image, token, NULL);
1641 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
1642 add_method (acfg, mono_marshal_get_synchronized_wrapper (method));
1645 /* pinvoke wrappers */
1646 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1648 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1650 method = mono_get_method (acfg->image, token, NULL);
1652 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1653 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
1654 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
1658 /* StructureToPtr/PtrToStructure wrappers */
1659 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
1662 token = MONO_TOKEN_TYPE_DEF | (i + 1);
1663 klass = mono_class_get (acfg->image, token);
1665 if (klass->valuetype && !klass->generic_container && ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) != TYPE_ATTRIBUTE_AUTO_LAYOUT)) {
1666 MonoMarshalType *info;
1667 gboolean can_marshal = TRUE;
1670 info = mono_marshal_load_type_info (klass);
1672 /* Only allow a few field types to avoid asserts in the marshalling code */
1673 for (j = 0; j < info->num_fields; j++) {
1674 switch (info->fields [j].field->type->type) {
1679 case MONO_TYPE_BOOLEAN:
1682 case MONO_TYPE_CHAR:
1690 can_marshal = FALSE;
1696 add_method (acfg, mono_marshal_get_struct_to_ptr (klass));
1697 add_method (acfg, mono_marshal_get_ptr_to_struct (klass));
1704 has_type_vars (MonoClass *klass)
1706 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
1709 return has_type_vars (klass->element_class);
1710 if (klass->generic_class) {
1711 MonoGenericContext *context = &klass->generic_class->context;
1712 if (context->class_inst) {
1715 for (i = 0; i < context->class_inst->type_argc; ++i)
1716 if (has_type_vars (mono_class_from_mono_type (context->class_inst->type_argv [i])))
1724 method_has_type_vars (MonoMethod *method)
1726 if (has_type_vars (method->klass))
1729 if (method->is_inflated) {
1730 MonoGenericContext *context = mono_method_get_context (method);
1731 if (context->method_inst) {
1734 for (i = 0; i < context->method_inst->type_argc; ++i)
1735 if (has_type_vars (mono_class_from_mono_type (context->method_inst->type_argv [i])))
1743 * add_generic_class:
1745 * Add all methods of a generic class.
1748 add_generic_class (MonoAotCompile *acfg, MonoClass *klass)
1753 mono_class_init (klass);
1755 if (klass->generic_class && klass->generic_class->context.class_inst->is_open)
1758 if (has_type_vars (klass))
1761 if (!klass->generic_class && !klass->rank)
1765 while ((method = mono_class_get_methods (klass, &iter))) {
1766 if (mono_method_is_generic_sharable_impl (method, FALSE))
1770 if (method->is_generic)
1775 * FIXME: Instances which are referenced by these methods are not added,
1776 * for example Array.Resize<int> for List<int>.Add ().
1778 add_extra_method (acfg, method);
1782 * For ICollection<T>, where T is a vtype, add instances of the helper methods
1783 * in Array, since a T[] could be cast to ICollection<T>.
1785 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") &&
1786 (!strcmp(klass->name, "ICollection`1") || !strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IList`1") || !strcmp (klass->name, "IEnumerator`1")) &&
1787 MONO_TYPE_ISSTRUCT (klass->generic_class->context.class_inst->type_argv [0])) {
1788 MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
1789 MonoClass *array_class = mono_bounded_array_class_get (tclass, 1, FALSE);
1793 if (!strcmp (klass->name, "IEnumerator`1"))
1794 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, "IEnumerable`1");
1796 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
1798 /* Add the T[]/InternalEnumerator class */
1799 if (!strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IEnumerator`1")) {
1803 while ((nclass = mono_class_get_nested_types (array_class->parent, &iter))) {
1804 if (!strcmp (nclass->name, "InternalEnumerator`1"))
1808 nclass = mono_class_inflate_generic_class (nclass, mono_generic_class_get_context (klass->generic_class));
1809 add_generic_class (acfg, nclass);
1813 while ((method = mono_class_get_methods (array_class, &iter))) {
1814 if (strstr (method->name, name_prefix))
1815 add_extra_method (acfg, method);
1818 g_free (name_prefix);
1823 * add_generic_instances:
1825 * Add instances referenced by the METHODSPEC/TYPESPEC table.
1828 add_generic_instances (MonoAotCompile *acfg)
1833 MonoMethodHeader *header;
1834 MonoMethodSignature *sig;
1835 MonoGenericContext *context;
1837 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHODSPEC].rows; ++i) {
1838 token = MONO_TOKEN_METHOD_SPEC | (i + 1);
1839 method = mono_get_method (acfg->image, token, NULL);
1841 context = mono_method_get_context (method);
1842 if (context && ((context->class_inst && context->class_inst->is_open) ||
1843 (context->method_inst && context->method_inst->is_open)))
1846 if (method->klass->image != acfg->image)
1849 if (mono_method_is_generic_sharable_impl (method, FALSE))
1853 add_extra_method (acfg, method);
1856 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
1859 token = MONO_TOKEN_TYPE_SPEC | (i + 1);
1861 klass = mono_class_get (acfg->image, token);
1862 if (!klass || klass->rank)
1865 add_generic_class (acfg, klass);
1868 /* Add types of args/locals */
1869 for (i = 0; i < acfg->methods->len; ++i) {
1872 method = g_ptr_array_index (acfg->methods, i);
1874 sig = mono_method_signature (method);
1877 for (j = 0; j < sig->param_count; ++j)
1878 if (sig->params [j]->type == MONO_TYPE_GENERICINST)
1879 add_generic_class (acfg, mono_class_from_mono_type (sig->params [j]));
1882 header = mono_method_get_header (method);
1885 for (j = 0; j < header->num_locals; ++j)
1886 if (header->locals [j]->type == MONO_TYPE_GENERICINST)
1887 add_generic_class (acfg, mono_class_from_mono_type (header->locals [j]));
1893 * emit_and_reloc_code:
1895 * Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
1896 * is true, calls are made through the GOT too. This is used for emitting trampolines
1897 * in full-aot mode, since calls made from trampolines couldn't go through the PLT,
1898 * since trampolines are needed to make PTL work.
1901 emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, guint32 code_len, MonoJumpInfo *relocs, gboolean got_only)
1903 int i, pindex, start_index, method_index;
1905 MonoJumpInfo *patch_info;
1906 MonoMethodHeader *header;
1907 gboolean skip, direct_call;
1909 char direct_call_target [128];
1912 header = mono_method_get_header (method);
1914 method_index = get_method_index (acfg, method);
1917 /* Collect and sort relocations */
1918 patches = g_ptr_array_new ();
1919 for (patch_info = relocs; patch_info; patch_info = patch_info->next)
1920 g_ptr_array_add (patches, patch_info);
1921 g_ptr_array_sort (patches, compare_patches);
1924 for (i = 0; i < code_len; i++) {
1926 for (pindex = start_index; pindex < patches->len; ++pindex) {
1927 patch_info = g_ptr_array_index (patches, pindex);
1928 if (patch_info->ip.i >= i)
1932 #ifdef MONO_ARCH_AOT_SUPPORTED
1934 if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
1935 start_index = pindex;
1937 switch (patch_info->type) {
1938 case MONO_PATCH_INFO_NONE:
1940 case MONO_PATCH_INFO_GOT_OFFSET: {
1943 arch_emit_got_offset (acfg, code + i, &code_size);
1950 * If this patch is a call, try emitting a direct call instead of
1951 * through a PLT entry. This is possible if the called method is in
1952 * the same assembly and requires no initialization.
1954 direct_call = FALSE;
1955 if (!got_only && (patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == method->klass->image)) {
1956 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
1958 gboolean direct_callable = TRUE;
1960 if (direct_callable && !(!callee_cfg->has_got_slots && (callee_cfg->method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
1961 direct_callable = FALSE;
1962 if ((callee_cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) && method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED)
1963 // FIXME: Maybe call the wrapper directly ?
1964 direct_callable = FALSE;
1965 if (direct_callable) {
1966 //printf ("DIRECT: %s %s\n", method ? mono_method_full_name (method, TRUE) : "", mono_method_full_name (callee_cfg->method, TRUE));
1968 sprintf (direct_call_target, "%sm_%x", acfg->temp_prefix, get_method_index (acfg, callee_cfg->orig_method));
1969 patch_info->type = MONO_PATCH_INFO_NONE;
1970 acfg->stats.direct_calls ++;
1974 acfg->stats.all_calls ++;
1977 if (!got_only && !direct_call) {
1978 int plt_offset = get_plt_offset (acfg, patch_info);
1979 if (plt_offset != -1) {
1980 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
1982 sprintf (direct_call_target, "%sp_%d", acfg->temp_prefix, plt_offset);
1984 /* Nullify the patch */
1985 patch_info->type = MONO_PATCH_INFO_NONE;
1992 arch_emit_direct_call (acfg, direct_call_target, &call_size);
1997 got_slot = get_got_offset (acfg, patch_info);
1999 arch_emit_got_access (acfg, code + i, got_slot, &code_size);
2006 #endif /* MONO_ARCH_AOT_SUPPORTED */
2009 /* Find next patch */
2011 for (pindex = start_index; pindex < patches->len; ++pindex) {
2012 patch_info = g_ptr_array_index (patches, pindex);
2013 if (patch_info->ip.i >= i)
2017 /* Try to emit multiple bytes at once */
2018 if (pindex < patches->len && patch_info->ip.i > i) {
2019 emit_bytes (acfg, code + i, patch_info->ip.i - i);
2020 i = patch_info->ip.i - 1;
2022 emit_bytes (acfg, code + i, 1);
2029 emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
2035 int func_alignment = AOT_FUNC_ALIGNMENT;
2036 MonoMethodHeader *header;
2038 method = cfg->orig_method;
2039 code = cfg->native_code;
2040 header = mono_method_get_header (method);
2042 method_index = get_method_index (acfg, method);
2044 /* Emit unbox trampoline */
2045 if (acfg->aot_opts.full_aot && cfg->orig_method->klass->valuetype && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
2046 char call_target [256];
2048 if (!method->wrapper_type && !method->is_inflated) {
2049 g_assert (method->token);
2050 sprintf (symbol, "ut_%d", mono_metadata_token_index (method->token) - 1);
2052 sprintf (symbol, "ut_e_%d", get_method_index (acfg, method));
2055 emit_section_change (acfg, ".text", 0);
2056 emit_global (acfg, symbol, TRUE);
2057 emit_label (acfg, symbol);
2059 sprintf (call_target, "%sm_%x", acfg->temp_prefix, method_index);
2061 arch_emit_unbox_trampoline (acfg, cfg->orig_method, cfg->generic_sharing_context, call_target);
2064 /* Make the labels local */
2065 sprintf (symbol, "%sm_%x", acfg->temp_prefix, method_index);
2067 emit_alignment (acfg, func_alignment);
2068 emit_label (acfg, symbol);
2070 if (acfg->aot_opts.write_symbols) {
2071 char *name1, *name2, *cached;
2072 int i, j, len, count;
2075 * Write a C style symbol for every method, this has two uses:
2076 * - it works on platforms where the dwarf debugging info is not
2078 * - it allows the setting of breakpoints of aot-ed methods.
2080 name1 = mono_method_full_name (method, TRUE);
2081 len = strlen (name1);
2082 name2 = malloc (len + 1);
2084 for (i = 0; i < len; ++i) {
2085 if (isalnum (name1 [i])) {
2086 name2 [j ++] = name1 [i];
2087 } else if (name1 [i] == ' ' && name1 [i + 1] == '(' && name1 [i + 2] == ')') {
2089 } else if (name1 [i] == ',' && name1 [i + 1] == ' ') {
2092 } else if (name1 [i] == '(' || name1 [i] == ')' || name1 [i] == '>') {
2099 while (g_hash_table_lookup (acfg->method_label_hash, name2)) {
2100 sprintf (name2 + j, "_%d", count);
2104 cached = g_strdup (name2);
2105 g_hash_table_insert (acfg->method_label_hash, cached, cached);
2107 sprintf (symbol, "%sme_%x", acfg->temp_prefix, method_index);
2108 emit_local_symbol (acfg, name2, symbol, TRUE);
2109 emit_label (acfg, name2);
2114 if (cfg->verbose_level > 0)
2115 g_print ("Method %s emitted as %s\n", mono_method_full_name (method, TRUE), symbol);
2117 acfg->stats.code_size += cfg->code_len;
2119 acfg->cfgs [method_index]->got_offset = acfg->got_offset;
2121 emit_and_reloc_code (acfg, method, code, cfg->code_len, cfg->patch_info, FALSE);
2125 sprintf (symbol, "%sme_%x", acfg->temp_prefix, method_index);
2126 emit_label (acfg, symbol);
2132 * Encode PATCH_INFO into its disk representation.
2135 encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf)
2139 switch (patch_info->type) {
2140 case MONO_PATCH_INFO_NONE:
2142 case MONO_PATCH_INFO_IMAGE:
2143 encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
2145 case MONO_PATCH_INFO_METHOD_REL:
2146 encode_value ((gint)patch_info->data.offset, p, &p);
2148 case MONO_PATCH_INFO_SWITCH: {
2149 gpointer *table = (gpointer *)patch_info->data.table->table;
2152 encode_value (patch_info->data.table->table_size, p, &p);
2153 for (k = 0; k < patch_info->data.table->table_size; k++)
2154 encode_value ((int)(gssize)table [k], p, &p);
2157 case MONO_PATCH_INFO_METHODCONST:
2158 case MONO_PATCH_INFO_METHOD:
2159 case MONO_PATCH_INFO_METHOD_JUMP:
2160 case MONO_PATCH_INFO_ICALL_ADDR:
2161 case MONO_PATCH_INFO_METHOD_RGCTX:
2162 encode_method_ref (acfg, patch_info->data.method, p, &p);
2164 case MONO_PATCH_INFO_INTERNAL_METHOD:
2165 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2166 guint32 len = strlen (patch_info->data.name);
2168 encode_value (len, p, &p);
2170 memcpy (p, patch_info->data.name, len);
2175 case MONO_PATCH_INFO_LDSTR: {
2176 guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
2177 guint32 token = patch_info->data.token->token;
2178 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
2179 encode_value (image_index, p, &p);
2180 encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
2183 case MONO_PATCH_INFO_RVA:
2184 case MONO_PATCH_INFO_DECLSEC:
2185 case MONO_PATCH_INFO_LDTOKEN:
2186 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2187 encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
2188 encode_value (patch_info->data.token->token, p, &p);
2189 encode_value (patch_info->data.token->has_context, p, &p);
2190 if (patch_info->data.token->has_context)
2191 encode_generic_context (acfg, &patch_info->data.token->context, p, &p);
2193 case MONO_PATCH_INFO_EXC_NAME: {
2194 MonoClass *ex_class;
2197 mono_class_from_name (mono_defaults.exception_class->image,
2198 "System", patch_info->data.target);
2199 g_assert (ex_class);
2200 encode_klass_ref (acfg, ex_class, p, &p);
2203 case MONO_PATCH_INFO_R4:
2204 encode_value (*((guint32 *)patch_info->data.target), p, &p);
2206 case MONO_PATCH_INFO_R8:
2207 encode_value (*((guint32 *)patch_info->data.target), p, &p);
2208 encode_value (*(((guint32 *)patch_info->data.target) + 1), p, &p);
2210 case MONO_PATCH_INFO_VTABLE:
2211 case MONO_PATCH_INFO_CLASS:
2212 case MONO_PATCH_INFO_IID:
2213 case MONO_PATCH_INFO_ADJUSTED_IID:
2214 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
2216 case MONO_PATCH_INFO_CLASS_INIT:
2217 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2218 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
2220 case MONO_PATCH_INFO_FIELD:
2221 case MONO_PATCH_INFO_SFLDA:
2222 encode_field_info (acfg, patch_info->data.field, p, &p);
2224 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2226 case MONO_PATCH_INFO_RGCTX_FETCH: {
2227 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
2229 encode_method_ref (acfg, entry->method, p, &p);
2230 encode_value (entry->in_mrgctx, p, &p);
2231 encode_value (entry->info_type, p, &p);
2232 encode_value (entry->data->type, p, &p);
2233 encode_patch (acfg, entry->data, p, &p);
2236 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2237 case MONO_PATCH_INFO_MONITOR_ENTER:
2238 case MONO_PATCH_INFO_MONITOR_EXIT:
2241 g_warning ("unable to handle jump info %d", patch_info->type);
2242 g_assert_not_reached ();
2249 encode_patch_list (MonoAotCompile *acfg, GPtrArray *patches, int n_patches, int first_got_offset, guint8 *buf, guint8 **endbuf)
2252 guint32 pindex, offset;
2253 MonoJumpInfo *patch_info;
2255 encode_value (n_patches, p, &p);
2258 encode_value (first_got_offset, p, &p);
2260 for (pindex = 0; pindex < patches->len; ++pindex) {
2261 patch_info = g_ptr_array_index (patches, pindex);
2263 if (patch_info->type == MONO_PATCH_INFO_NONE)
2267 offset = get_got_offset (acfg, patch_info);
2268 encode_value (offset, p, &p);
2275 emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
2279 int pindex, buf_size, n_patches;
2283 MonoJumpInfo *patch_info;
2284 MonoMethodHeader *header;
2285 guint32 method_index;
2287 guint32 first_got_offset;
2289 method = cfg->orig_method;
2290 code = cfg->native_code;
2291 header = mono_method_get_header (method);
2293 method_index = get_method_index (acfg, method);
2295 /* Make the labels local */
2296 sprintf (symbol, "%sm_%x_p", acfg->temp_prefix, method_index);
2298 /* Sort relocations */
2299 patches = g_ptr_array_new ();
2300 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
2301 g_ptr_array_add (patches, patch_info);
2302 g_ptr_array_sort (patches, compare_patches);
2304 first_got_offset = acfg->cfgs [method_index]->got_offset;
2306 /**********************/
2307 /* Encode method info */
2308 /**********************/
2310 buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
2311 p = buf = g_malloc (buf_size);
2313 if (mono_class_get_cctor (method->klass))
2314 encode_klass_ref (acfg, method->klass, p, &p);
2316 /* Not needed when loading the method */
2317 encode_value (0, p, &p);
2320 if (cfg->opt & MONO_OPT_SHARED) {
2321 encode_value (g_list_length (cfg->ldstr_list), p, &p);
2322 for (l = cfg->ldstr_list; l; l = l->next) {
2323 encode_value ((long)l->data, p, &p);
2327 /* Used only in shared mode */
2328 g_assert (!cfg->ldstr_list);
2331 for (pindex = 0; pindex < patches->len; ++pindex) {
2332 patch_info = g_ptr_array_index (patches, pindex);
2334 if ((patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
2335 (patch_info->type == MONO_PATCH_INFO_NONE)) {
2336 patch_info->type = MONO_PATCH_INFO_NONE;
2341 if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
2342 /* Stored in a GOT slot initialized at module load time */
2343 patch_info->type = MONO_PATCH_INFO_NONE;
2347 if (is_plt_patch (patch_info)) {
2348 /* Calls are made through the PLT */
2349 patch_info->type = MONO_PATCH_INFO_NONE;
2357 g_assert (cfg->has_got_slots);
2359 encode_patch_list (acfg, patches, n_patches, first_got_offset, p, &p);
2361 acfg->stats.info_size += p - buf;
2363 /* Emit method info */
2365 emit_label (acfg, symbol);
2367 g_assert (p - buf < buf_size);
2368 emit_bytes (acfg, buf, p - buf);
2373 get_unwind_info_offset (MonoAotCompile *acfg, guint8 *encoded, guint32 encoded_len)
2375 guint32 cache_index;
2378 /* Reuse the unwind module to canonize and store unwind info entries */
2379 cache_index = mono_cache_unwind_info (encoded, encoded_len);
2381 /* Use +/- 1 to distinguish 0s from missing entries */
2382 offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1)));
2390 * It would be easier to use assembler symbols, but the caller needs an
2393 offset = acfg->unwind_info_offset;
2394 g_hash_table_insert (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1), GUINT_TO_POINTER (offset + 1));
2395 g_ptr_array_add (acfg->unwind_ops, GUINT_TO_POINTER (cache_index));
2398 encode_value (encoded_len, p, &p);
2400 acfg->unwind_info_offset += encoded_len + (p - buf);
2406 emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
2409 int k, buf_size, method_index;
2410 guint32 debug_info_size;
2413 MonoMethodHeader *header;
2414 guint8 *p, *buf, *debug_info;
2415 MonoJitInfo *jinfo = cfg->jit_info;
2417 gboolean use_unwind_ops = FALSE;
2419 method = cfg->orig_method;
2420 code = cfg->native_code;
2421 header = mono_method_get_header (method);
2423 method_index = get_method_index (acfg, method);
2425 /* Make the labels local */
2426 sprintf (symbol, "%se_%x_p", acfg->temp_prefix, method_index);
2428 if (!acfg->aot_opts.nodebug) {
2429 mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
2432 debug_info_size = 0;
2435 buf_size = header->num_clauses * 256 + debug_info_size + 1024;
2436 p = buf = g_malloc (buf_size);
2438 #ifdef MONO_ARCH_HAVE_XP_UNWIND
2439 use_unwind_ops = cfg->unwind_ops != NULL;
2442 flags = (jinfo->has_generic_jit_info ? 1 : 0) | (use_unwind_ops ? 2 : 0);
2444 encode_value (jinfo->code_size, p, &p);
2445 encode_value (flags, p, &p);
2447 if (use_unwind_ops) {
2448 guint32 encoded_len;
2452 * This is a duplicate of the data in the .debug_frame section, but that
2453 * section cannot be accessed using the dl interface.
2455 encoded = mono_unwind_ops_encode (cfg->unwind_ops, &encoded_len);
2456 encode_value (get_unwind_info_offset (acfg, encoded, encoded_len), p, &p);
2459 encode_value (jinfo->used_regs, p, &p);
2462 /* Exception table */
2463 if (header->num_clauses) {
2464 for (k = 0; k < header->num_clauses; ++k) {
2465 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
2467 encode_value (ei->exvar_offset, p, &p);
2469 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
2470 encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
2472 encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
2473 encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
2474 encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
2478 if (jinfo->has_generic_jit_info) {
2479 MonoGenericJitInfo *gi = mono_jit_info_get_generic_jit_info (jinfo);
2481 encode_value (gi->has_this ? 1 : 0, p, &p);
2482 encode_value (gi->this_reg, p, &p);
2483 encode_value (gi->this_offset, p, &p);
2486 * Need to encode jinfo->method too, since it is not equal to 'method'
2487 * when using generic sharing.
2489 encode_method_ref (acfg, jinfo->method, p, &p);
2492 g_assert (debug_info_size < buf_size);
2494 encode_value (debug_info_size, p, &p);
2495 if (debug_info_size) {
2496 memcpy (p, debug_info, debug_info_size);
2497 p += debug_info_size;
2498 g_free (debug_info);
2501 acfg->stats.ex_info_size += p - buf;
2505 emit_label (acfg, symbol);
2507 g_assert (p - buf < buf_size);
2508 emit_bytes (acfg, buf, p - buf);
2513 emit_klass_info (MonoAotCompile *acfg, guint32 token)
2515 MonoClass *klass = mono_class_get (acfg->image, token);
2519 gboolean no_special_static, cant_encode;
2520 gpointer iter = NULL;
2522 buf_size = 10240 + (klass->vtable_size * 16);
2523 p = buf = g_malloc (buf_size);
2527 mono_class_init (klass);
2529 mono_class_get_nested_types (klass, &iter);
2530 g_assert (klass->nested_classes_inited);
2532 mono_class_setup_vtable (klass);
2535 * Emit all the information which is required for creating vtables so
2536 * the runtime does not need to create the MonoMethod structures which
2537 * take up a lot of space.
2540 no_special_static = !mono_class_has_special_static_fields (klass);
2542 /* Check whenever we have enough info to encode the vtable */
2543 cant_encode = FALSE;
2544 for (i = 0; i < klass->vtable_size; ++i) {
2545 MonoMethod *cm = klass->vtable [i];
2547 if (cm && mono_method_signature (cm)->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, cm))
2551 if (klass->generic_container || cant_encode) {
2552 encode_value (-1, p, &p);
2554 encode_value (klass->vtable_size, p, &p);
2555 encode_value ((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);
2556 if (klass->has_cctor)
2557 encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
2558 if (klass->has_finalize)
2559 encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
2561 encode_value (klass->instance_size, p, &p);
2562 encode_value (mono_class_data_size (klass), p, &p);
2563 encode_value (klass->packing_size, p, &p);
2564 encode_value (klass->min_align, p, &p);
2566 for (i = 0; i < klass->vtable_size; ++i) {
2567 MonoMethod *cm = klass->vtable [i];
2570 encode_method_ref (acfg, cm, p, &p);
2572 encode_value (0, p, &p);
2576 acfg->stats.class_info_size += p - buf;
2579 sprintf (symbol, "%sK_I_%x", acfg->temp_prefix, token - MONO_TOKEN_TYPE_DEF - 1);
2580 emit_label (acfg, symbol);
2582 g_assert (p - buf < buf_size);
2583 emit_bytes (acfg, buf, p - buf);
2588 * Calls made from AOTed code are routed through a table of jumps similar to the
2589 * ELF PLT (Program Linkage Table). The differences are the following:
2590 * - the ELF PLT entries make an indirect jump though the GOT so they expect the
2591 * GOT pointer to be in EBX. We want to avoid this, so our table contains direct
2592 * jumps. This means the jumps need to be patched when the address of the callee is
2593 * known. Initially the PLT entries jump to code which transfers control to the
2594 * AOT runtime through the first PLT entry.
2597 emit_plt (MonoAotCompile *acfg)
2603 sprintf (symbol, "plt");
2605 emit_section_change (acfg, ".text", 0);
2606 emit_global (acfg, symbol, TRUE);
2608 /* This section will be made read-write by the AOT loader */
2609 emit_alignment (acfg, mono_pagesize ());
2611 emit_alignment (acfg, 16);
2613 emit_label (acfg, symbol);
2615 for (i = 0; i < acfg->plt_offset; ++i) {
2618 sprintf (label, "%sp_%d", acfg->temp_prefix, i);
2619 emit_label (acfg, label);
2622 * The first plt entry is used to transfer code to the AOT loader.
2624 arch_emit_plt_entry (acfg, i);
2627 sprintf (symbol, "plt_end");
2628 emit_global (acfg, symbol, TRUE);
2629 emit_label (acfg, symbol);
2632 static G_GNUC_UNUSED void
2633 emit_trampoline (MonoAotCompile *acfg, const char *name, guint8 *code,
2634 guint32 code_size, int got_offset, MonoJumpInfo *ji, GSList *unwind_ops)
2638 MonoJumpInfo *patch_info;
2644 sprintf (symbol, "%s", name);
2646 emit_section_change (acfg, ".text", 0);
2647 emit_global (acfg, symbol, TRUE);
2648 emit_alignment (acfg, 16);
2649 emit_label (acfg, symbol);
2651 sprintf (symbol, "%snamed_%s", acfg->temp_prefix, name);
2652 emit_label (acfg, symbol);
2655 * The code should access everything through the GOT, so we pass
2658 emit_and_reloc_code (acfg, NULL, code, code_size, ji, TRUE);
2662 /* Sort relocations */
2663 patches = g_ptr_array_new ();
2664 for (patch_info = ji; patch_info; patch_info = patch_info->next)
2665 g_ptr_array_add (patches, patch_info);
2666 g_ptr_array_sort (patches, compare_patches);
2668 buf_size = patches->len * 128 + 128;
2669 buf = g_malloc (buf_size);
2672 encode_patch_list (acfg, patches, patches->len, got_offset, p, &p);
2673 g_assert (p - buf < buf_size);
2675 sprintf (symbol, "%s_p", name);
2677 emit_section_change (acfg, ".text", 0);
2678 emit_global (acfg, symbol, FALSE);
2679 emit_label (acfg, symbol);
2681 emit_bytes (acfg, buf, p - buf);
2683 /* Emit debug info */
2687 sprintf (symbol, "%s", name);
2688 sprintf (symbol2, "%snamed_%s", acfg->temp_prefix, name);
2691 mono_dwarf_writer_emit_trampoline (acfg->dwarf, symbol, symbol2, NULL, NULL, code_size, unwind_ops);
2696 emit_trampolines (MonoAotCompile *acfg)
2699 int i, tramp_got_offset;
2700 MonoAotTrampoline ntype;
2701 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
2709 if (!acfg->aot_opts.full_aot)
2712 g_assert (acfg->image->assembly);
2714 /* Currently, we only emit most trampolines into the mscorlib AOT image. */
2715 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
2716 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
2718 * Emit the generic trampolines.
2720 * We could save some code by treating the generic trampolines as a wrapper
2721 * method, but that approach has its own complexities, so we choose the simpler
2724 for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
2725 code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, TRUE);
2727 /* Emit trampoline code */
2729 sprintf (symbol, "generic_trampoline_%d", tramp_type);
2731 emit_trampoline (acfg, symbol, code, code_size, acfg->got_offset, ji, unwind_ops);
2734 code = mono_arch_get_nullified_class_init_trampoline (&code_size);
2735 emit_trampoline (acfg, "nullified_class_init_trampoline", code, code_size, acfg->got_offset, NULL, NULL);
2736 #if defined(TARGET_AMD64) && defined(MONO_ARCH_MONITOR_OBJECT_REG)
2737 code = mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, TRUE);
2738 emit_trampoline (acfg, "monitor_enter_trampoline", code, code_size, acfg->got_offset, ji, NULL);
2739 code = mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, TRUE);
2740 emit_trampoline (acfg, "monitor_exit_trampoline", code, code_size, acfg->got_offset, ji, NULL);
2743 code = mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, TRUE);
2744 emit_trampoline (acfg, "generic_class_init_trampoline", code, code_size, acfg->got_offset, ji, NULL);
2746 /* Emit the exception related code pieces */
2747 code = mono_arch_get_restore_context_full (&code_size, &ji, TRUE);
2748 emit_trampoline (acfg, "restore_context", code, code_size, acfg->got_offset, ji, NULL);
2749 code = mono_arch_get_call_filter_full (&code_size, &ji, TRUE);
2750 emit_trampoline (acfg, "call_filter", code, code_size, acfg->got_offset, ji, NULL);
2751 code = mono_arch_get_throw_exception_full (&code_size, &ji, TRUE);
2752 emit_trampoline (acfg, "throw_exception", code, code_size, acfg->got_offset, ji, NULL);
2753 code = mono_arch_get_rethrow_exception_full (&code_size, &ji, TRUE);
2754 emit_trampoline (acfg, "rethrow_exception", code, code_size, acfg->got_offset, ji, NULL);
2755 code = mono_arch_get_throw_exception_by_name_full (&code_size, &ji, TRUE);
2756 emit_trampoline (acfg, "throw_exception_by_name", code, code_size, acfg->got_offset, ji, NULL);
2757 code = mono_arch_get_throw_corlib_exception_full (&code_size, &ji, TRUE);
2758 emit_trampoline (acfg, "throw_corlib_exception", code, code_size, acfg->got_offset, ji, NULL);
2760 #if defined(TARGET_AMD64)
2761 code = mono_arch_get_throw_pending_exception_full (&code_size, &ji, TRUE);
2762 emit_trampoline (acfg, "throw_pending_exception", code, code_size, acfg->got_offset, ji, NULL);
2765 #if defined(TARGET_AMD64) || defined(TARGET_ARM)
2766 for (i = 0; i < 128; ++i) {
2769 offset = MONO_RGCTX_SLOT_MAKE_RGCTX (i);
2770 code = mono_arch_create_rgctx_lazy_fetch_trampoline_full (offset, &code_size, &ji, TRUE);
2771 sprintf (symbol, "rgctx_fetch_trampoline_%u", offset);
2772 emit_trampoline (acfg, symbol, code, code_size, acfg->got_offset, ji, NULL);
2774 offset = MONO_RGCTX_SLOT_MAKE_MRGCTX (i);
2775 code = mono_arch_create_rgctx_lazy_fetch_trampoline_full (offset, &code_size, &ji, TRUE);
2776 sprintf (symbol, "rgctx_fetch_trampoline_%u", offset);
2777 emit_trampoline (acfg, symbol, code, code_size, acfg->got_offset, ji, NULL);
2781 #if defined(TARGET_AMD64) || defined(TARGET_ARM)
2785 /* delegate_invoke_impl trampolines */
2786 l = mono_arch_get_delegate_invoke_impls ();
2788 MonoAotTrampInfo *info = l->data;
2790 emit_trampoline (acfg, info->name, info->code, info->code_size, acfg->got_offset, NULL, NULL);
2796 #endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
2798 /* Emit trampolines which are numerous */
2801 * These include the following:
2802 * - specific trampolines
2803 * - static rgctx invoke trampolines
2805 * These trampolines have the same code, they are parameterized by GOT
2807 * They are defined in this file, in the arch_... routines instead of
2808 * in tramp-<ARCH>.c, since it is easier to do it this way.
2812 * When running in aot-only mode, we can't create specific trampolines at
2813 * runtime, so we create a few, and save them in the AOT file.
2814 * Normal trampolines embed their argument as a literal inside the
2815 * trampoline code, we can't do that here, so instead we embed an offset
2816 * which needs to be added to the trampoline address to get the address of
2817 * the GOT slot which contains the argument value.
2818 * The generated trampolines jump to the generic trampolines using another
2819 * GOT slot, which will be setup by the AOT loader to point to the
2820 * generic trampoline code of the given type.
2824 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for
2828 emit_section_change (acfg, ".text", 0);
2830 tramp_got_offset = acfg->got_offset;
2832 for (ntype = 0; ntype < MONO_AOT_TRAMP_NUM; ++ntype) {
2834 case MONO_AOT_TRAMP_SPECIFIC:
2835 sprintf (symbol, "specific_trampolines");
2837 case MONO_AOT_TRAMP_STATIC_RGCTX:
2838 sprintf (symbol, "static_rgctx_trampolines");
2840 case MONO_AOT_TRAMP_IMT_THUNK:
2841 sprintf (symbol, "imt_thunks");
2844 g_assert_not_reached ();
2847 emit_global (acfg, symbol, TRUE);
2848 emit_alignment (acfg, 16);
2849 emit_label (acfg, symbol);
2851 acfg->trampoline_got_offset_base [ntype] = tramp_got_offset;
2853 for (i = 0; i < acfg->num_trampolines [ntype]; ++i) {
2857 case MONO_AOT_TRAMP_SPECIFIC:
2858 arch_emit_specific_trampoline (acfg, tramp_got_offset, &tramp_size);
2859 tramp_got_offset += 2;
2861 case MONO_AOT_TRAMP_STATIC_RGCTX:
2862 arch_emit_static_rgctx_trampoline (acfg, tramp_got_offset, &tramp_size);
2863 tramp_got_offset += 2;
2865 case MONO_AOT_TRAMP_IMT_THUNK:
2866 arch_emit_imt_thunk (acfg, tramp_got_offset, &tramp_size);
2867 tramp_got_offset += 1;
2870 g_assert_not_reached ();
2873 if (!acfg->trampoline_size [ntype]) {
2874 g_assert (tramp_size);
2875 acfg->trampoline_size [ntype] = tramp_size;
2880 /* Reserve some entries at the end of the GOT for our use */
2881 acfg->num_trampoline_got_entries = tramp_got_offset - acfg->got_offset;
2884 acfg->got_offset += acfg->num_trampoline_got_entries;
2888 str_begins_with (const char *str1, const char *str2)
2890 int len = strlen (str2);
2891 return strncmp (str1, str2, len) == 0;
2895 mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
2897 gchar **args, **ptr;
2899 args = g_strsplit (aot_options ? aot_options : "", ",", -1);
2900 for (ptr = args; ptr && *ptr; ptr ++) {
2901 const char *arg = *ptr;
2903 if (str_begins_with (arg, "outfile=")) {
2904 opts->outfile = g_strdup (arg + strlen ("outfile="));
2905 } else if (str_begins_with (arg, "save-temps")) {
2906 opts->save_temps = TRUE;
2907 } else if (str_begins_with (arg, "keep-temps")) {
2908 opts->save_temps = TRUE;
2909 } else if (str_begins_with (arg, "write-symbols")) {
2910 opts->write_symbols = TRUE;
2911 } else if (str_begins_with (arg, "metadata-only")) {
2912 opts->metadata_only = TRUE;
2913 } else if (str_begins_with (arg, "bind-to-runtime-version")) {
2914 opts->bind_to_runtime_version = TRUE;
2915 } else if (str_begins_with (arg, "full")) {
2916 opts->full_aot = TRUE;
2917 } else if (str_begins_with (arg, "threads=")) {
2918 opts->nthreads = atoi (arg + strlen ("threads="));
2919 } else if (str_begins_with (arg, "static")) {
2920 opts->static_link = TRUE;
2921 opts->no_dlsym = TRUE;
2922 } else if (str_begins_with (arg, "asmonly")) {
2923 opts->asm_only = TRUE;
2924 } else if (str_begins_with (arg, "asmwriter")) {
2925 opts->asm_writer = TRUE;
2926 } else if (str_begins_with (arg, "nodebug")) {
2927 opts->nodebug = TRUE;
2928 } else if (str_begins_with (arg, "ntrampolines=")) {
2929 opts->ntrampolines = atoi (arg + strlen ("ntrampolines="));
2931 fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
2940 add_token_info_hash (gpointer key, gpointer value, gpointer user_data)
2942 MonoMethod *method = (MonoMethod*)key;
2943 MonoJumpInfoToken *ji = (MonoJumpInfoToken*)value;
2944 MonoJumpInfoToken *new_ji = g_new0 (MonoJumpInfoToken, 1);
2945 MonoAotCompile *acfg = user_data;
2947 new_ji->image = ji->image;
2948 new_ji->token = ji->token;
2949 g_hash_table_insert (acfg->token_info_hash, method, new_ji);
2953 can_encode_class (MonoAotCompile *acfg, MonoClass *klass)
2955 if (klass->type_token)
2957 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
2960 return can_encode_class (acfg, klass->element_class);
2965 can_encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
2967 switch (patch_info->type) {
2968 case MONO_PATCH_INFO_METHOD:
2969 case MONO_PATCH_INFO_METHODCONST: {
2970 MonoMethod *method = patch_info->data.method;
2972 if (method->wrapper_type) {
2973 switch (method->wrapper_type) {
2974 case MONO_WRAPPER_NONE:
2975 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
2976 case MONO_WRAPPER_XDOMAIN_INVOKE:
2977 case MONO_WRAPPER_STFLD:
2978 case MONO_WRAPPER_LDFLD:
2979 case MONO_WRAPPER_LDFLDA:
2980 case MONO_WRAPPER_LDFLD_REMOTE:
2981 case MONO_WRAPPER_STFLD_REMOTE:
2982 case MONO_WRAPPER_STELEMREF:
2983 case MONO_WRAPPER_ISINST:
2984 case MONO_WRAPPER_PROXY_ISINST:
2985 case MONO_WRAPPER_ALLOC:
2986 case MONO_WRAPPER_REMOTING_INVOKE:
2987 case MONO_WRAPPER_UNKNOWN:
2990 //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
2994 if (!method->token) {
2995 /* The method is part of a constructed type like Int[,].Set (). */
2996 if (!g_hash_table_lookup (acfg->token_info_hash, method)) {
2997 if (method->klass->rank)
3005 case MONO_PATCH_INFO_VTABLE:
3006 case MONO_PATCH_INFO_CLASS_INIT:
3007 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
3008 case MONO_PATCH_INFO_CLASS:
3009 case MONO_PATCH_INFO_IID:
3010 case MONO_PATCH_INFO_ADJUSTED_IID:
3011 if (!can_encode_class (acfg, patch_info->data.klass)) {
3012 //printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
3016 case MONO_PATCH_INFO_RGCTX_FETCH: {
3017 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
3019 if (!can_encode_patch (acfg, entry->data))
3031 add_generic_class (MonoAotCompile *acfg, MonoClass *klass);
3036 * AOT compile a given method.
3037 * This function might be called by multiple threads, so it must be thread-safe.
3040 compile_method (MonoAotCompile *acfg, MonoMethod *method)
3043 MonoJumpInfo *patch_info;
3046 MonoMethod *wrapped;
3048 if (acfg->aot_opts.metadata_only)
3051 mono_acfg_lock (acfg);
3052 index = get_method_index (acfg, method);
3053 mono_acfg_unlock (acfg);
3055 /* fixme: maybe we can also precompile wrapper methods */
3056 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3057 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3058 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
3059 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
3063 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
3066 wrapped = mono_marshal_method_from_wrapper (method);
3067 if (wrapped && (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && wrapped->is_generic)
3068 // FIXME: The wrapper should be generic too, but it is not
3071 InterlockedIncrement (&acfg->stats.mcount);
3074 if (method->is_generic || method->klass->generic_container) {
3075 InterlockedIncrement (&acfg->stats.genericcount);
3080 //acfg->aot_opts.print_skipped_methods = TRUE;
3083 * Since these methods are the only ones which are compiled with
3084 * AOT support, and they are not used by runtime startup/shutdown code,
3085 * the runtime will not see AOT methods during AOT compilation,so it
3086 * does not need to support them by creating a fake GOT etc.
3088 cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), FALSE, TRUE, 0);
3089 if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
3090 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
3091 InterlockedIncrement (&acfg->stats.genericcount);
3094 if (cfg->exception_type != MONO_EXCEPTION_NONE) {
3095 /* Let the exception happen at runtime */
3099 if (cfg->disable_aot) {
3100 if (acfg->aot_opts.print_skipped_methods)
3101 printf ("Skip (disabled): %s\n", mono_method_full_name (method, TRUE));
3102 InterlockedIncrement (&acfg->stats.ocount);
3103 mono_destroy_compile (cfg);
3107 /* Nullify patches which need no aot processing */
3108 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3109 switch (patch_info->type) {
3110 case MONO_PATCH_INFO_LABEL:
3111 case MONO_PATCH_INFO_BB:
3112 patch_info->type = MONO_PATCH_INFO_NONE;
3119 /* Collect method->token associations from the cfg */
3120 mono_acfg_lock (acfg);
3121 g_hash_table_foreach (cfg->token_info_hash, add_token_info_hash, acfg);
3122 mono_acfg_unlock (acfg);
3125 * Check for absolute addresses.
3128 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3129 switch (patch_info->type) {
3130 case MONO_PATCH_INFO_ABS:
3131 /* unable to handle this */
3140 if (acfg->aot_opts.print_skipped_methods)
3141 printf ("Skip (abs call): %s\n", mono_method_full_name (method, TRUE));
3142 InterlockedIncrement (&acfg->stats.abscount);
3143 mono_destroy_compile (cfg);
3147 /* Lock for the rest of the code */
3148 mono_acfg_lock (acfg);
3151 * Check for methods/klasses we can't encode.
3154 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3155 if (!can_encode_patch (acfg, patch_info))
3160 if (acfg->aot_opts.print_skipped_methods)
3161 printf ("Skip (patches): %s\n", mono_method_full_name (method, TRUE));
3162 acfg->stats.ocount++;
3163 mono_destroy_compile (cfg);
3164 mono_acfg_unlock (acfg);
3168 /* Adds generic instances referenced by this method */
3169 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3170 switch (patch_info->type) {
3171 case MONO_PATCH_INFO_METHOD: {
3172 MonoMethod *m = patch_info->data.method;
3173 if (m->is_inflated) {
3174 if (!(mono_class_generic_sharing_enabled (m->klass) &&
3175 mono_method_is_generic_sharable_impl (m, FALSE)) &&
3176 !method_has_type_vars (m)) {
3177 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
3178 if (acfg->aot_opts.full_aot)
3179 add_extra_method (acfg, mono_marshal_get_native_wrapper (m, TRUE, TRUE));
3181 add_extra_method (acfg, m);
3184 add_generic_class (acfg, m->klass);
3193 /* Determine whenever the method has GOT slots */
3194 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3195 switch (patch_info->type) {
3196 case MONO_PATCH_INFO_GOT_OFFSET:
3197 case MONO_PATCH_INFO_NONE:
3199 case MONO_PATCH_INFO_IMAGE:
3200 /* The assembly is stored in GOT slot 0 */
3201 if (patch_info->data.image != acfg->image)
3202 cfg->has_got_slots = TRUE;
3205 if (!is_plt_patch (patch_info))
3206 cfg->has_got_slots = TRUE;
3211 if (!cfg->has_got_slots)
3212 InterlockedIncrement (&acfg->stats.methods_without_got_slots);
3214 /* Make a copy of the patch info which is in the mempool */
3216 MonoJumpInfo *patches = NULL, *patches_end = NULL;
3218 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3219 MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
3222 patches = new_patch_info;
3224 patches_end->next = new_patch_info;
3225 patches_end = new_patch_info;
3227 cfg->patch_info = patches;
3229 /* Make a copy of the unwind info */
3231 GSList *l, *unwind_ops;
3235 for (l = cfg->unwind_ops; l; l = l->next) {
3236 op = mono_mempool_alloc (acfg->mempool, sizeof (MonoUnwindOp));
3237 memcpy (op, l->data, sizeof (MonoUnwindOp));
3238 unwind_ops = g_slist_prepend_mempool (acfg->mempool, unwind_ops, op);
3240 cfg->unwind_ops = g_slist_reverse (unwind_ops);
3242 /* Make a copy of the argument/local info */
3244 MonoInst **args, **locals;
3245 MonoMethodSignature *sig;
3246 MonoMethodHeader *header;
3249 sig = mono_method_signature (method);
3250 args = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * (sig->param_count + sig->hasthis));
3251 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3252 args [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
3253 memcpy (args [i], cfg->args [i], sizeof (MonoInst));
3257 header = mono_method_get_header (method);
3258 locals = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * header->num_locals);
3259 for (i = 0; i < header->num_locals; ++i) {
3260 locals [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
3261 memcpy (locals [i], cfg->locals [i], sizeof (MonoInst));
3263 cfg->locals = locals;
3266 /* Free some fields used by cfg to conserve memory */
3267 mono_mempool_destroy (cfg->mempool);
3268 cfg->mempool = NULL;
3269 g_free (cfg->varinfo);
3270 cfg->varinfo = NULL;
3274 mono_regstate_free (cfg->rs);
3278 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
3280 while (index >= acfg->cfgs_size) {
3281 MonoCompile **new_cfgs;
3284 new_size = acfg->cfgs_size * 2;
3285 new_cfgs = g_new0 (MonoCompile*, new_size);
3286 memcpy (new_cfgs, acfg->cfgs, sizeof (MonoCompile*) * acfg->cfgs_size);
3287 g_free (acfg->cfgs);
3288 acfg->cfgs = new_cfgs;
3289 acfg->cfgs_size = new_size;
3291 acfg->cfgs [index] = cfg;
3293 g_hash_table_insert (acfg->method_to_cfg, cfg->orig_method, cfg);
3296 if (cfg->orig_method->wrapper_type)
3297 g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
3300 mono_acfg_unlock (acfg);
3302 InterlockedIncrement (&acfg->stats.ccount);
3306 compile_thread_main (gpointer *user_data)
3308 MonoDomain *domain = user_data [0];
3309 MonoAotCompile *acfg = user_data [1];
3310 GPtrArray *methods = user_data [2];
3313 mono_thread_attach (domain);
3315 for (i = 0; i < methods->len; ++i)
3316 compile_method (acfg, g_ptr_array_index (methods, i));
3320 load_profile_files (MonoAotCompile *acfg)
3324 int file_index, res, method_index, i;
3331 tmp = g_strdup_printf ("%s/.mono/aot-profile-data/%s-%s-%d", g_get_home_dir (), acfg->image->assembly_name, acfg->image->guid, file_index);
3333 if (!g_file_test (tmp, G_FILE_TEST_IS_REGULAR)) {
3338 infile = fopen (tmp, "r");
3341 printf ("Using profile data file '%s'\n", tmp);
3346 res = fscanf (infile, "%32s\n", ver);
3347 if ((res != 1) || strcmp (ver, "#VER:1") != 0) {
3348 printf ("Profile file has wrong version or invalid.\n");
3354 res = fscanf (infile, "%d\n", &token);
3358 method_index = mono_metadata_token_index (token) - 1;
3360 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (method_index)))
3361 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (method_index));
3366 /* Add missing methods */
3368 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3369 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (i)))
3370 unordered = g_list_prepend (unordered, GUINT_TO_POINTER (i));
3372 unordered = g_list_reverse (unordered);
3373 if (acfg->method_order)
3374 g_list_last (acfg->method_order)->next = unordered;
3376 acfg->method_order = unordered;
3380 emit_code (MonoAotCompile *acfg)
3386 sprintf (symbol, "methods");
3387 emit_section_change (acfg, ".text", 0);
3388 emit_global (acfg, symbol, TRUE);
3389 emit_alignment (acfg, 8);
3390 emit_label (acfg, symbol);
3393 * Emit some padding so the local symbol for the first method doesn't have the
3394 * same address as 'methods'.
3396 emit_zero_bytes (acfg, 16);
3398 for (l = acfg->method_order; l != NULL; l = l->next) {
3399 i = GPOINTER_TO_UINT (l->data);
3402 emit_method_code (acfg, acfg->cfgs [i]);
3405 sprintf (symbol, "methods_end");
3406 emit_section_change (acfg, ".text", 0);
3407 emit_global (acfg, symbol, FALSE);
3408 emit_alignment (acfg, 8);
3409 emit_label (acfg, symbol);
3411 sprintf (symbol, "method_offsets");
3412 emit_section_change (acfg, ".text", 1);
3413 emit_global (acfg, symbol, FALSE);
3414 emit_alignment (acfg, 8);
3415 emit_label (acfg, symbol);
3417 for (i = 0; i < acfg->nmethods; ++i) {
3418 if (acfg->cfgs [i]) {
3419 sprintf (symbol, "%sm_%x", acfg->temp_prefix, i);
3420 emit_symbol_diff (acfg, symbol, "methods", 0);
3422 emit_int32 (acfg, 0xffffffff);
3429 emit_info (MonoAotCompile *acfg)
3435 /* Emit method info */
3436 sprintf (symbol, "method_info");
3437 emit_section_change (acfg, ".text", 1);
3438 emit_global (acfg, symbol, FALSE);
3439 emit_alignment (acfg, 8);
3440 emit_label (acfg, symbol);
3442 /* To reduce size of generated assembly code */
3443 sprintf (symbol, "mi");
3444 emit_label (acfg, symbol);
3446 for (l = acfg->method_order; l != NULL; l = l->next) {
3447 i = GPOINTER_TO_UINT (l->data);
3450 emit_method_info (acfg, acfg->cfgs [i]);
3453 sprintf (symbol, "method_info_offsets");
3454 emit_section_change (acfg, ".text", 1);
3455 emit_global (acfg, symbol, FALSE);
3456 emit_alignment (acfg, 8);
3457 emit_label (acfg, symbol);
3459 for (i = 0; i < acfg->nmethods; ++i) {
3460 if (acfg->cfgs [i]) {
3461 sprintf (symbol, "%sm_%x_p", acfg->temp_prefix, i);
3462 emit_symbol_diff (acfg, symbol, "mi", 0);
3464 emit_int32 (acfg, 0);
3470 #endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
3473 * mono_aot_str_hash:
3475 * Hash function for strings which we use to hash strings for things which are
3476 * saved in the AOT image, since g_str_hash () can change.
3479 mono_aot_str_hash (gconstpointer v1)
3481 /* Same as g_str_hash () in glib */
3482 char *p = (char *) v1;
3487 hash = (hash << 5) - hash + *p;
3493 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
3494 #define mix(a,b,c) { \
3495 a -= c; a ^= rot(c, 4); c += b; \
3496 b -= a; b ^= rot(a, 6); a += c; \
3497 c -= b; c ^= rot(b, 8); b += a; \
3498 a -= c; a ^= rot(c,16); c += b; \
3499 b -= a; b ^= rot(a,19); a += c; \
3500 c -= b; c ^= rot(b, 4); b += a; \
3502 #define final(a,b,c) { \
3503 c ^= b; c -= rot(b,14); \
3504 a ^= c; a -= rot(c,11); \
3505 b ^= a; b -= rot(a,25); \
3506 c ^= b; c -= rot(b,16); \
3507 a ^= c; a -= rot(c,4); \
3508 b ^= a; b -= rot(a,14); \
3509 c ^= b; c -= rot(b,24); \
3513 mono_aot_type_hash (MonoType *t1)
3515 guint hash = t1->type;
3517 hash |= t1->byref << 6; /* do not collide with t1->type values */
3519 case MONO_TYPE_VALUETYPE:
3520 case MONO_TYPE_CLASS:
3521 case MONO_TYPE_SZARRAY:
3522 /* check if the distribution is good enough */
3523 return ((hash << 5) - hash) ^ mono_aot_str_hash (t1->data.klass->name);
3525 return ((hash << 5) - hash) ^ mono_aot_type_hash (t1->data.type);
3526 case MONO_TYPE_ARRAY:
3527 return ((hash << 5) - hash) ^ mono_aot_type_hash (&t1->data.array->eklass->byval_arg);
3528 case MONO_TYPE_GENERICINST:
3529 return ((hash << 5) - hash) ^ 0;
3535 * mono_aot_method_hash:
3537 * Return a hash code for methods which only depends on metadata.
3540 mono_aot_method_hash (MonoMethod *method)
3542 MonoMethodSignature *sig;
3546 guint32 *hashes_start, *hashes;
3549 /* Similar to the hash in mono_method_get_imt_slot () */
3551 sig = mono_method_signature (method);
3553 hashes_count = sig->param_count + 5;
3554 hashes_start = malloc (hashes_count * sizeof (guint32));
3555 hashes = hashes_start;
3557 /* Some wrappers are assigned to random classes */
3558 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
3559 klass = method->klass;
3561 klass = mono_defaults.object_class;
3563 if (!method->wrapper_type) {
3564 char *full_name = mono_type_full_name (&klass->byval_arg);
3566 hashes [0] = mono_aot_str_hash (full_name);
3570 hashes [0] = mono_aot_str_hash (klass->name);
3571 hashes [1] = mono_aot_str_hash (klass->name_space);
3573 hashes [2] = mono_aot_str_hash (method->name);
3574 hashes [3] = method->wrapper_type;
3575 hashes [4] = mono_aot_type_hash (sig->ret);
3576 for (i = 0; i < sig->param_count; i++) {
3577 hashes [5 + i] = mono_aot_type_hash (sig->params [i]);
3580 /* Setup internal state */
3581 a = b = c = 0xdeadbeef + (((guint32)hashes_count)<<2);
3583 /* Handle most of the hashes */
3584 while (hashes_count > 3) {
3593 /* Handle the last 3 hashes (all the case statements fall through) */
3594 switch (hashes_count) {
3595 case 3 : c += hashes [2];
3596 case 2 : b += hashes [1];
3597 case 1 : a += hashes [0];
3599 case 0: /* nothing left to add */
3603 free (hashes_start);
3612 * mono_aot_wrapper_name:
3614 * Return a string which uniqely identifies the given wrapper method.
3617 mono_aot_wrapper_name (MonoMethod *method)
3619 char *name, *tmpsig, *klass_desc;
3621 tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
3623 switch (method->wrapper_type) {
3624 case MONO_WRAPPER_RUNTIME_INVOKE:
3625 case MONO_WRAPPER_DELEGATE_INVOKE:
3626 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
3627 case MONO_WRAPPER_DELEGATE_END_INVOKE:
3628 /* This is a hack to work around the fact that runtime invoke wrappers get assigned to some random class */
3629 name = g_strdup_printf ("%s (%s)", method->name, tmpsig);
3632 klass_desc = mono_type_full_name (&method->klass->byval_arg);
3634 name = g_strdup_printf ("%s:%s (%s)", klass_desc, method->name, tmpsig);
3644 * mono_aot_tramp_info_create:
3646 * Create a MonoAotTrampInfo structure from the arguments.
3649 mono_aot_tramp_info_create (const char *name, guint8 *code, guint32 code_size)
3651 MonoAotTrampInfo *info = g_new0 (MonoAotTrampInfo, 1);
3653 info->name = (char*)name;
3655 info->code_size = code_size;
3660 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
3662 typedef struct HashEntry {
3663 guint32 key, value, index;
3664 struct HashEntry *next;
3668 * emit_extra_methods:
3670 * Emit methods which are not in the METHOD table, like wrappers.
3673 emit_extra_methods (MonoAotCompile *acfg)
3675 int i, table_size, buf_size;
3678 guint32 *info_offsets;
3681 HashEntry *entry, *new_entry;
3682 int nmethods, max_chain_length;
3685 info_offsets = g_new0 (guint32, acfg->extra_methods->len);
3687 buf_size = acfg->extra_methods->len * 256 + 256;
3688 p = buf = g_malloc (buf_size);
3690 /* Encode method info */
3692 /* So offsets are > 0 */
3695 for (i = 0; i < acfg->extra_methods->len; ++i) {
3696 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
3697 MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
3704 info_offsets [i] = p - buf;
3707 if (method->wrapper_type) {
3709 * We encode some wrappers using their name, since encoding them
3710 * directly would be difficult. This also avoids creating the wrapper
3711 * methods at runtime, since they are not needed anyway.
3713 switch (method->wrapper_type) {
3714 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
3715 case MONO_WRAPPER_SYNCHRONIZED:
3716 /* encode_method_ref () can handle these */
3718 case MONO_WRAPPER_RUNTIME_INVOKE:
3719 if (mono_marshal_method_from_wrapper (method) != method && !strstr (method->name, "virtual"))
3720 /* Direct wrapper, encode normally */
3724 name = mono_aot_wrapper_name (method);
3730 encode_value (1, p, &p);
3731 encode_value (method->wrapper_type, p, &p);
3732 strcpy ((char*)p, name);
3733 p += strlen (name ) + 1;
3736 encode_value (0, p, &p);
3737 encode_method_ref (acfg, method, p, &p);
3740 g_assert ((p - buf) < buf_size);
3743 g_assert ((p - buf) < buf_size);
3745 /* Emit method info */
3746 sprintf (symbol, "extra_method_info");
3747 emit_section_change (acfg, ".text", 1);
3748 emit_global (acfg, symbol, FALSE);
3749 emit_alignment (acfg, 8);
3750 emit_label (acfg, symbol);
3752 emit_bytes (acfg, buf, p - buf);
3757 * Construct a chained hash table for mapping indexes in extra_method_info to
3760 table_size = g_spaced_primes_closest ((int)(nmethods * 1.5));
3761 table = g_ptr_array_sized_new (table_size);
3762 for (i = 0; i < table_size; ++i)
3763 g_ptr_array_add (table, NULL);
3764 chain_lengths = g_new0 (int, table_size);
3765 max_chain_length = 0;
3766 for (i = 0; i < acfg->extra_methods->len; ++i) {
3767 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
3768 MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
3774 key = info_offsets [i];
3775 value = get_method_index (acfg, method);
3777 hash = mono_aot_method_hash (method) % table_size;
3779 chain_lengths [hash] ++;
3780 max_chain_length = MAX (max_chain_length, chain_lengths [hash]);
3782 /* FIXME: Allocate from the mempool */
3783 new_entry = g_new0 (HashEntry, 1);
3784 new_entry->key = key;
3785 new_entry->value = value;
3787 entry = g_ptr_array_index (table, hash);
3788 if (entry == NULL) {
3789 new_entry->index = hash;
3790 g_ptr_array_index (table, hash) = new_entry;
3793 entry = entry->next;
3795 entry->next = new_entry;
3796 new_entry->index = table->len;
3797 g_ptr_array_add (table, new_entry);
3801 //printf ("MAX: %d\n", max_chain_length);
3803 /* Emit the table */
3804 sprintf (symbol, "extra_method_table");
3805 emit_section_change (acfg, ".text", 0);
3806 emit_global (acfg, symbol, FALSE);
3807 emit_alignment (acfg, 8);
3808 emit_label (acfg, symbol);
3810 g_assert (table_size < 65000);
3811 emit_int32 (acfg, table_size);
3812 g_assert (table->len < 65000);
3813 for (i = 0; i < table->len; ++i) {
3814 HashEntry *entry = g_ptr_array_index (table, i);
3816 if (entry == NULL) {
3817 emit_int32 (acfg, 0);
3818 emit_int32 (acfg, 0);
3819 emit_int32 (acfg, 0);
3821 g_assert (entry->key > 0);
3822 emit_int32 (acfg, entry->key);
3823 emit_int32 (acfg, entry->value);
3825 emit_int32 (acfg, entry->next->index);
3827 emit_int32 (acfg, 0);
3832 * Emit a table reverse mapping method indexes to their index in extra_method_info.
3833 * This is used by mono_aot_find_jit_info ().
3835 sprintf (symbol, "extra_method_info_offsets");
3836 emit_section_change (acfg, ".text", 0);
3837 emit_global (acfg, symbol, FALSE);
3838 emit_alignment (acfg, 8);
3839 emit_label (acfg, symbol);
3841 emit_int32 (acfg, acfg->extra_methods->len);
3842 for (i = 0; i < acfg->extra_methods->len; ++i) {
3843 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
3845 emit_int32 (acfg, get_method_index (acfg, method));
3846 emit_int32 (acfg, info_offsets [i]);
3851 emit_method_order (MonoAotCompile *acfg)
3857 sprintf (symbol, "method_order");
3858 emit_section_change (acfg, ".text", 1);
3859 emit_global (acfg, symbol, FALSE);
3860 emit_alignment (acfg, 8);
3861 emit_label (acfg, symbol);
3863 /* First emit an index table */
3866 for (l = acfg->method_order; l != NULL; l = l->next) {
3867 i = GPOINTER_TO_UINT (l->data);
3869 if (acfg->cfgs [i]) {
3870 if ((index % 1024) == 0) {
3871 emit_int32 (acfg, i);
3879 emit_int32 (acfg, 0xffffff);
3881 /* Then emit the whole method order */
3882 for (l = acfg->method_order; l != NULL; l = l->next) {
3883 i = GPOINTER_TO_UINT (l->data);
3885 if (acfg->cfgs [i]) {
3886 emit_int32 (acfg, i);
3891 sprintf (symbol, "method_order_end");
3892 emit_section_change (acfg, ".text", 1);
3893 emit_global (acfg, symbol, FALSE);
3894 emit_label (acfg, symbol);
3898 emit_exception_info (MonoAotCompile *acfg)
3903 sprintf (symbol, "ex_info");
3904 emit_section_change (acfg, ".text", 1);
3905 emit_global (acfg, symbol, FALSE);
3906 emit_alignment (acfg, 8);
3907 emit_label (acfg, symbol);
3909 /* To reduce size of generated assembly */
3910 sprintf (symbol, "ex");
3911 emit_label (acfg, symbol);
3913 for (i = 0; i < acfg->nmethods; ++i) {
3915 emit_exception_debug_info (acfg, acfg->cfgs [i]);
3918 sprintf (symbol, "ex_info_offsets");
3919 emit_section_change (acfg, ".text", 1);
3920 emit_global (acfg, symbol, FALSE);
3921 emit_alignment (acfg, 8);
3922 emit_label (acfg, symbol);
3924 for (i = 0; i < acfg->nmethods; ++i) {
3925 if (acfg->cfgs [i]) {
3926 sprintf (symbol, "%se_%x_p", acfg->temp_prefix, i);
3927 emit_symbol_diff (acfg, symbol, "ex", 0);
3929 emit_int32 (acfg, 0);
3936 emit_unwind_info (MonoAotCompile *acfg)
3942 * The unwind info contains a lot of duplicates so we emit each unique
3943 * entry once, and only store the offset from the start of the table in the
3947 sprintf (symbol, "unwind_info");
3948 emit_section_change (acfg, ".text", 1);
3949 emit_alignment (acfg, 8);
3950 emit_label (acfg, symbol);
3951 emit_global (acfg, symbol, FALSE);
3953 for (i = 0; i < acfg->unwind_ops->len; ++i) {
3954 guint32 index = GPOINTER_TO_UINT (g_ptr_array_index (acfg->unwind_ops, i));
3955 guint8 *unwind_info;
3956 guint32 unwind_info_len;
3960 unwind_info = mono_get_cached_unwind_info (index, &unwind_info_len);
3963 encode_value (unwind_info_len, p, &p);
3964 emit_bytes (acfg, buf, p - buf);
3965 emit_bytes (acfg, unwind_info, unwind_info_len);
3967 acfg->stats.unwind_info_size += (p - buf) + unwind_info_len;
3972 emit_class_info (MonoAotCompile *acfg)
3977 sprintf (symbol, "class_info");
3978 emit_section_change (acfg, ".text", 1);
3979 emit_global (acfg, symbol, FALSE);
3980 emit_alignment (acfg, 8);
3981 emit_label (acfg, symbol);
3983 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
3984 emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
3986 sprintf (symbol, "class_info_offsets");
3987 emit_section_change (acfg, ".text", 1);
3988 emit_global (acfg, symbol, FALSE);
3989 emit_alignment (acfg, 8);
3990 emit_label (acfg, symbol);
3992 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3993 sprintf (symbol, "%sK_I_%x", acfg->temp_prefix, i);
3994 emit_symbol_diff (acfg, symbol, "class_info", 0);
3999 typedef struct ClassNameTableEntry {
4000 guint32 token, index;
4001 struct ClassNameTableEntry *next;
4002 } ClassNameTableEntry;
4005 emit_class_name_table (MonoAotCompile *acfg)
4008 guint32 token, hash;
4013 ClassNameTableEntry *entry, *new_entry;
4016 * Construct a chained hash table for mapping class names to typedef tokens.
4018 table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
4019 table = g_ptr_array_sized_new (table_size);
4020 for (i = 0; i < table_size; ++i)
4021 g_ptr_array_add (table, NULL);
4022 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
4023 token = MONO_TOKEN_TYPE_DEF | (i + 1);
4024 klass = mono_class_get (acfg->image, token);
4025 full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
4026 hash = mono_aot_str_hash (full_name) % table_size;
4029 /* FIXME: Allocate from the mempool */
4030 new_entry = g_new0 (ClassNameTableEntry, 1);
4031 new_entry->token = token;
4033 entry = g_ptr_array_index (table, hash);
4034 if (entry == NULL) {
4035 new_entry->index = hash;
4036 g_ptr_array_index (table, hash) = new_entry;
4039 entry = entry->next;
4041 entry->next = new_entry;
4042 new_entry->index = table->len;
4043 g_ptr_array_add (table, new_entry);
4047 /* Emit the table */
4048 sprintf (symbol, "class_name_table");
4049 emit_section_change (acfg, ".text", 0);
4050 emit_global (acfg, symbol, FALSE);
4051 emit_alignment (acfg, 8);
4052 emit_label (acfg, symbol);
4054 /* FIXME: Optimize memory usage */
4055 g_assert (table_size < 65000);
4056 emit_int16 (acfg, table_size);
4057 g_assert (table->len < 65000);
4058 for (i = 0; i < table->len; ++i) {
4059 ClassNameTableEntry *entry = g_ptr_array_index (table, i);
4061 if (entry == NULL) {
4062 emit_int16 (acfg, 0);
4063 emit_int16 (acfg, 0);
4065 emit_int16 (acfg, mono_metadata_token_index (entry->token));
4067 emit_int16 (acfg, entry->next->index);
4069 emit_int16 (acfg, 0);
4075 emit_image_table (MonoAotCompile *acfg)
4081 * The image table is small but referenced in a lot of places.
4082 * So we emit it at once, and reference its elements by an index.
4085 sprintf (symbol, "mono_image_table");
4086 emit_section_change (acfg, ".text", 1);
4087 emit_global (acfg, symbol, FALSE);
4088 emit_alignment (acfg, 8);
4089 emit_label (acfg, symbol);
4091 emit_int32 (acfg, acfg->image_table->len);
4092 for (i = 0; i < acfg->image_table->len; i++) {
4093 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
4094 MonoAssemblyName *aname = &image->assembly->aname;
4096 /* FIXME: Support multi-module assemblies */
4097 g_assert (image->assembly->image == image);
4099 emit_string (acfg, image->assembly_name);
4100 emit_string (acfg, image->guid);
4101 emit_string (acfg, aname->culture ? aname->culture : "");
4102 emit_string (acfg, (const char*)aname->public_key_token);
4104 emit_alignment (acfg, 8);
4105 emit_int32 (acfg, aname->flags);
4106 emit_int32 (acfg, aname->major);
4107 emit_int32 (acfg, aname->minor);
4108 emit_int32 (acfg, aname->build);
4109 emit_int32 (acfg, aname->revision);
4114 emit_got_info (MonoAotCompile *acfg)
4117 int i, first_plt_got_patch, buf_size;
4119 guint32 *got_info_offsets;
4121 /* Add the patches needed by the PLT to the GOT */
4122 acfg->plt_got_offset_base = acfg->got_offset;
4123 first_plt_got_patch = acfg->got_patches->len;
4124 for (i = 1; i < acfg->plt_offset; ++i) {
4125 MonoJumpInfo *patch_info = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
4127 g_ptr_array_add (acfg->got_patches, patch_info);
4130 acfg->got_offset += acfg->plt_offset;
4134 * - optimize offsets table.
4135 * - reduce number of exported symbols.
4136 * - emit info for a klass only once.
4137 * - determine when a method uses a GOT slot which is guaranteed to be already
4139 * - clean up and document the code.
4140 * - use String.Empty in class libs.
4143 /* Encode info required to decode shared GOT entries */
4144 buf_size = acfg->got_patches->len * 64;
4145 p = buf = mono_mempool_alloc (acfg->mempool, buf_size);
4146 got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->got_patches->len * sizeof (guint32));
4147 acfg->plt_got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->plt_offset * sizeof (guint32));
4148 for (i = 0; i < acfg->got_patches->len; ++i) {
4149 MonoJumpInfo *ji = g_ptr_array_index (acfg->got_patches, i);
4151 got_info_offsets [i] = p - buf;
4152 if (i >= first_plt_got_patch)
4153 acfg->plt_got_info_offsets [i - first_plt_got_patch + 1] = got_info_offsets [i];
4154 encode_value (ji->type, p, &p);
4155 encode_patch (acfg, ji, p, &p);
4158 g_assert (p - buf <= buf_size);
4160 acfg->stats.got_info_size = p - buf;
4162 /* Emit got_info table */
4163 sprintf (symbol, "got_info");
4164 emit_section_change (acfg, ".text", 1);
4165 emit_global (acfg, symbol, FALSE);
4166 emit_alignment (acfg, 8);
4167 emit_label (acfg, symbol);
4169 emit_bytes (acfg, buf, p - buf);
4171 /* Emit got_info_offsets table */
4172 sprintf (symbol, "got_info_offsets");
4173 emit_section_change (acfg, ".text", 1);
4174 emit_global (acfg, symbol, FALSE);
4175 emit_alignment (acfg, 8);
4176 emit_label (acfg, symbol);
4178 for (i = 0; i < acfg->got_patches->len; ++i)
4179 emit_int32 (acfg, got_info_offsets [i]);
4181 acfg->stats.got_info_offsets_size = acfg->got_patches->len * 4;
4185 emit_got (MonoAotCompile *acfg)
4189 /* Don't make GOT global so accesses to it don't need relocations */
4190 sprintf (symbol, acfg->got_symbol);
4191 emit_section_change (acfg, ".bss", 0);
4192 emit_alignment (acfg, 8);
4193 emit_local_symbol (acfg, symbol, "got_end", FALSE);
4194 emit_label (acfg, symbol);
4195 if (acfg->got_offset > 0)
4196 emit_zero_bytes (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
4198 sprintf (symbol, "got_end");
4199 emit_label (acfg, symbol);
4201 sprintf (symbol, "mono_aot_got_addr");
4202 emit_section_change (acfg, ".data", 0);
4203 emit_global (acfg, symbol, FALSE);
4204 emit_alignment (acfg, 8);
4205 emit_label (acfg, symbol);
4206 emit_pointer (acfg, acfg->got_symbol);
4210 emit_globals (MonoAotCompile *acfg)
4215 emit_string_symbol (acfg, "mono_assembly_guid" , acfg->image->guid);
4217 emit_string_symbol (acfg, "mono_aot_version", MONO_AOT_FILE_VERSION);
4219 opts_str = g_strdup_printf ("%d", acfg->opts);
4220 emit_string_symbol (acfg, "mono_aot_opt_flags", opts_str);
4223 emit_string_symbol (acfg, "mono_aot_full_aot", acfg->aot_opts.full_aot ? "TRUE" : "FALSE");
4225 if (acfg->aot_opts.bind_to_runtime_version) {
4226 build_info = mono_get_runtime_build_info ();
4227 emit_string_symbol (acfg, "mono_runtime_version", build_info);
4228 g_free (build_info);
4230 emit_string_symbol (acfg, "mono_runtime_version", "");
4234 * When static linking, we emit a global which will point to the symbol table.
4236 if (acfg->aot_opts.static_link) {
4241 /* Emit a string holding the assembly name */
4242 emit_string_symbol (acfg, "mono_aot_assembly_name", acfg->image->assembly->aname.name);
4244 /* Emit the names */
4245 for (i = 0; i < acfg->globals->len; ++i) {
4246 char *name = g_ptr_array_index (acfg->globals, i);
4248 sprintf (symbol, "name_%d", i);
4249 emit_section_change (acfg, ".text", 1);
4250 emit_label (acfg, symbol);
4251 emit_string (acfg, name);
4254 /* Emit the globals table */
4255 sprintf (symbol, "globals");
4256 emit_section_change (acfg, ".data", 0);
4257 /* This is not a global, since it is accessed by the init function */
4258 emit_alignment (acfg, 8);
4259 emit_label (acfg, symbol);
4261 for (i = 0; i < acfg->globals->len; ++i) {
4262 char *name = g_ptr_array_index (acfg->globals, i);
4264 sprintf (symbol, "name_%d", i);
4265 emit_pointer (acfg, symbol);
4267 sprintf (symbol, "%s", name);
4268 emit_pointer (acfg, symbol);
4270 /* Null terminate the table */
4271 emit_int32 (acfg, 0);
4272 emit_int32 (acfg, 0);
4275 * Emit a global symbol which can be passed by an embedding app to
4276 * mono_aot_register_module ().
4278 #if defined(__MACH__)
4279 sprintf (symbol, "_mono_aot_module_%s_info", acfg->image->assembly->aname.name);
4281 sprintf (symbol, "mono_aot_module_%s_info", acfg->image->assembly->aname.name);
4284 /* Get rid of characters which cannot occur in symbols */
4286 for (p = symbol; *p; ++p) {
4287 if (!(isalnum (*p) || *p == '_'))
4290 acfg->static_linking_symbol = g_strdup (symbol);
4291 emit_global_inner (acfg, symbol, FALSE);
4292 emit_alignment (acfg, 8);
4293 emit_label (acfg, symbol);
4294 emit_pointer (acfg, "globals");
4299 emit_mem_end (MonoAotCompile *acfg)
4303 sprintf (symbol, "mem_end");
4304 emit_section_change (acfg, ".text", 1);
4305 emit_global (acfg, symbol, FALSE);
4306 emit_alignment (acfg, 8);
4307 emit_label (acfg, symbol);
4311 * Emit a structure containing all the information not stored elsewhere.
4314 emit_file_info (MonoAotCompile *acfg)
4319 sprintf (symbol, "mono_aot_file_info");
4320 emit_section_change (acfg, ".data", 0);
4321 emit_alignment (acfg, 8);
4322 emit_label (acfg, symbol);
4323 emit_global (acfg, symbol, FALSE);
4325 /* The data emitted here must match MonoAotFileInfo in aot-runtime.c. */
4326 emit_int32 (acfg, acfg->plt_got_offset_base);
4327 emit_int32 (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
4328 emit_int32 (acfg, acfg->plt_offset);
4330 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
4331 emit_int32 (acfg, acfg->num_trampolines [i]);
4332 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
4333 emit_int32 (acfg, acfg->trampoline_got_offset_base [i]);
4334 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
4335 emit_int32 (acfg, acfg->trampoline_size [i]);
4339 emit_dwarf_info (MonoAotCompile *acfg)
4341 #ifdef EMIT_DWARF_INFO
4343 char symbol [128], symbol2 [128];
4345 /* DIEs for methods */
4346 for (i = 0; i < acfg->nmethods; ++i) {
4347 MonoCompile *cfg = acfg->cfgs [i];
4352 sprintf (symbol, "%sm_%x", acfg->temp_prefix, i);
4353 sprintf (symbol2, "%sme_%x", acfg->temp_prefix, i);
4355 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 ()));
4361 collect_methods (MonoAotCompile *acfg)
4364 MonoImage *image = acfg->image;
4366 /* Collect methods */
4367 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
4369 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
4371 method = mono_get_method (acfg->image, token, NULL);
4374 printf ("Failed to load method 0x%x from '%s'.\n", token, image->name);
4378 /* Load all methods eagerly to skip the slower lazy loading code */
4379 mono_class_setup_methods (method->klass);
4381 if (acfg->aot_opts.full_aot && method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
4382 /* Compile the wrapper instead */
4383 /* We do this here instead of add_wrappers () because it is easy to do it here */
4384 MonoMethod *wrapper = mono_marshal_get_native_wrapper (method, check_for_pending_exc, TRUE);
4388 /* Since we add the normal methods first, their index will be equal to their zero based token index */
4389 add_method_with_index (acfg, method, i, FALSE);
4390 acfg->method_index ++;
4393 add_generic_instances (acfg);
4395 if (acfg->aot_opts.full_aot)
4396 add_wrappers (acfg);
4400 compile_methods (MonoAotCompile *acfg)
4404 if (acfg->aot_opts.nthreads > 0) {
4409 gpointer *user_data;
4410 MonoMethod **methods;
4412 methods_len = acfg->methods->len;
4414 len = acfg->methods->len / acfg->aot_opts.nthreads;
4417 * Partition the list of methods into fragments, and hand it to threads to
4420 threads = g_ptr_array_new ();
4421 /* Make a copy since acfg->methods is modified by compile_method () */
4422 methods = g_new0 (MonoMethod*, methods_len);
4423 //memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
4424 for (i = 0; i < methods_len; ++i)
4425 methods [i] = g_ptr_array_index (acfg->methods, i);
4427 while (i < methods_len) {
4428 frag = g_ptr_array_new ();
4429 for (j = 0; j < len; ++j) {
4430 if (i < methods_len) {
4431 g_ptr_array_add (frag, methods [i]);
4436 user_data = g_new0 (gpointer, 3);
4437 user_data [0] = mono_domain_get ();
4438 user_data [1] = acfg;
4439 user_data [2] = frag;
4441 handle = mono_create_thread (NULL, 0, (gpointer)compile_thread_main, user_data, 0, NULL);
4442 g_ptr_array_add (threads, handle);
4446 for (i = 0; i < threads->len; ++i) {
4447 WaitForSingleObjectEx (g_ptr_array_index (threads, i), INFINITE, FALSE);
4453 /* Compile methods added by compile_method () or all methods if nthreads == 0 */
4454 for (i = methods_len; i < acfg->methods->len; ++i) {
4455 /* This can new methods to acfg->methods */
4456 compile_method (acfg, g_ptr_array_index (acfg->methods, i));
4461 compile_asm (MonoAotCompile *acfg)
4463 char *command, *objfile;
4464 char *outfile_name, *tmp_outfile_name;
4466 #if defined(TARGET_AMD64)
4467 #define AS_OPTIONS "--64"
4468 #elif defined(sparc) && SIZEOF_VOID_P == 8
4469 #define AS_OPTIONS "-xarch=v9"
4471 #define AS_OPTIONS ""
4474 if (acfg->aot_opts.asm_only) {
4475 printf ("Output file: '%s'.\n", acfg->tmpfname);
4476 if (acfg->aot_opts.static_link)
4477 printf ("Linking symbol: '%s'.\n", acfg->static_linking_symbol);
4481 if (acfg->aot_opts.static_link) {
4482 if (acfg->aot_opts.outfile)
4483 objfile = g_strdup_printf ("%s", acfg->aot_opts.outfile);
4485 objfile = g_strdup_printf ("%s.o", acfg->image->name);
4487 objfile = g_strdup_printf ("%s.o", acfg->tmpfname);
4489 command = g_strdup_printf ("as %s %s -o %s", AS_OPTIONS, acfg->tmpfname, objfile);
4490 printf ("Executing the native assembler: %s\n", command);
4491 if (system (command) != 0) {
4499 if (acfg->aot_opts.static_link) {
4500 printf ("Output file: '%s'.\n", objfile);
4501 printf ("Linking symbol: '%s'.\n", acfg->static_linking_symbol);
4506 if (acfg->aot_opts.outfile)
4507 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
4509 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
4511 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
4514 command = g_strdup_printf ("ld -shared -G -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
4515 #elif defined(__ppc__) && defined(__MACH__)
4516 command = g_strdup_printf ("gcc -dynamiclib -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
4517 #elif defined(PLATFORM_WIN32)
4518 command = g_strdup_printf ("gcc -shared --dll -mno-cygwin -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
4520 command = g_strdup_printf ("ld -shared -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
4522 printf ("Executing the native linker: %s\n", command);
4523 if (system (command) != 0) {
4524 g_free (tmp_outfile_name);
4525 g_free (outfile_name);
4533 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, SHARED_EXT);
4534 printf ("Stripping the binary: %s\n", com);
4538 #if defined(TARGET_ARM) && !defined(__MACH__)
4540 * gas generates 'mapping symbols' each time code and data is mixed, which
4541 * happens a lot in emit_and_reloc_code (), so we need to get rid of them.
4543 command = g_strdup_printf ("strip --strip-symbol=\\$a --strip-symbol=\\$d %s", tmp_outfile_name);
4544 printf ("Stripping the binary: %s\n", command);
4545 if (system (command) != 0) {
4546 g_free (tmp_outfile_name);
4547 g_free (outfile_name);
4554 rename (tmp_outfile_name, outfile_name);
4556 g_free (tmp_outfile_name);
4557 g_free (outfile_name);
4560 if (acfg->aot_opts.save_temps)
4561 printf ("Retained input file.\n");
4563 unlink (acfg->tmpfname);
4568 static MonoAotCompile*
4569 acfg_create (MonoAssembly *ass, guint32 opts)
4571 MonoImage *image = ass->image;
4572 MonoAotCompile *acfg;
4574 acfg = g_new0 (MonoAotCompile, 1);
4575 acfg->methods = g_ptr_array_new ();
4576 acfg->method_indexes = g_hash_table_new (NULL, NULL);
4577 acfg->plt_offset_to_patch = g_hash_table_new (NULL, NULL);
4578 acfg->patch_to_plt_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
4579 acfg->patch_to_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
4580 acfg->got_patches = g_ptr_array_new ();
4581 acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
4582 acfg->token_info_hash = g_hash_table_new_full (NULL, NULL, NULL, g_free);
4583 acfg->image_hash = g_hash_table_new (NULL, NULL);
4584 acfg->image_table = g_ptr_array_new ();
4585 acfg->globals = g_ptr_array_new ();
4586 acfg->image = image;
4588 acfg->mempool = mono_mempool_new ();
4589 acfg->extra_methods = g_ptr_array_new ();
4590 acfg->unwind_info_offsets = g_hash_table_new (NULL, NULL);
4591 acfg->unwind_ops = g_ptr_array_new ();
4592 acfg->method_label_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
4593 InitializeCriticalSection (&acfg->mutex);
4599 acfg_free (MonoAotCompile *acfg)
4603 img_writer_destroy (acfg->w);
4604 for (i = 0; i < acfg->nmethods; ++i)
4606 g_free (acfg->cfgs [i]);
4607 g_free (acfg->cfgs);
4608 g_free (acfg->static_linking_symbol);
4609 g_ptr_array_free (acfg->methods, TRUE);
4610 g_ptr_array_free (acfg->got_patches, TRUE);
4611 g_ptr_array_free (acfg->image_table, TRUE);
4612 g_ptr_array_free (acfg->globals, TRUE);
4613 g_ptr_array_free (acfg->unwind_ops, TRUE);
4614 g_hash_table_destroy (acfg->method_indexes);
4615 g_hash_table_destroy (acfg->plt_offset_to_patch);
4616 g_hash_table_destroy (acfg->patch_to_plt_offset);
4617 g_hash_table_destroy (acfg->patch_to_got_offset);
4618 g_hash_table_destroy (acfg->method_to_cfg);
4619 g_hash_table_destroy (acfg->token_info_hash);
4620 g_hash_table_destroy (acfg->image_hash);
4621 g_hash_table_destroy (acfg->unwind_info_offsets);
4622 g_hash_table_destroy (acfg->method_label_hash);
4623 mono_mempool_destroy (acfg->mempool);
4628 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
4630 MonoImage *image = ass->image;
4632 MonoAotCompile *acfg;
4633 char *outfile_name, *tmp_outfile_name, *p;
4637 printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
4639 acfg = acfg_create (ass, opts);
4641 memset (&acfg->aot_opts, 0, sizeof (acfg->aot_opts));
4642 acfg->aot_opts.write_symbols = TRUE;
4643 acfg->aot_opts.ntrampolines = 1024;
4645 mono_aot_parse_options (aot_options, &acfg->aot_opts);
4647 //acfg->aot_opts.print_skipped_methods = TRUE;
4649 #ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
4650 if (acfg->aot_opts.full_aot) {
4651 printf ("--aot=full is not supported on this platform.\n");
4656 if (acfg->aot_opts.static_link)
4657 acfg->aot_opts.asm_writer = TRUE;
4659 load_profile_files (acfg);
4661 acfg->num_trampolines [MONO_AOT_TRAMP_SPECIFIC] = acfg->aot_opts.full_aot ? acfg->aot_opts.ntrampolines : 0;
4662 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
4663 acfg->num_trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = acfg->aot_opts.full_aot ? 1024 : 0;
4665 acfg->num_trampolines [MONO_AOT_TRAMP_IMT_THUNK] = acfg->aot_opts.full_aot ? 128 : 0;
4667 acfg->got_symbol = g_strdup_printf ("mono_aot_%s_got", acfg->image->assembly->aname.name);
4669 /* Get rid of characters which cannot occur in symbols */
4670 p = acfg->got_symbol;
4671 for (p = acfg->got_symbol; *p; ++p) {
4672 if (!(isalnum (*p) || *p == '_'))
4676 acfg->method_index = 1;
4678 collect_methods (acfg);
4680 acfg->cfgs_size = acfg->methods->len + 32;
4681 acfg->cfgs = g_new0 (MonoCompile*, acfg->cfgs_size);
4683 /* PLT offset 0 is reserved for the PLT trampoline */
4684 acfg->plt_offset = 1;
4686 /* GOT offset 0 is reserved for the address of the current assembly */
4690 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
4691 ji->type = MONO_PATCH_INFO_IMAGE;
4692 ji->data.image = acfg->image;
4694 get_got_offset (acfg, ji);
4699 compile_methods (acfg);
4703 acfg->stats.jit_time = TV_ELAPSED (atv, btv);
4707 if (!acfg->aot_opts.asm_only && !acfg->aot_opts.asm_writer && bin_writer_supported ()) {
4708 if (acfg->aot_opts.outfile)
4709 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
4711 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
4714 * Can't use g_file_open_tmp () as it will be deleted at exit, and
4715 * it might be in another file system so the rename () won't work.
4717 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
4719 acfg->fp = fopen (tmp_outfile_name, "w");
4721 printf ("Unable to create temporary file '%s': %s\n", tmp_outfile_name, strerror (errno));
4725 acfg->w = img_writer_create (acfg->fp, TRUE);
4726 acfg->use_bin_writer = TRUE;
4728 if (acfg->aot_opts.asm_only) {
4729 if (acfg->aot_opts.outfile)
4730 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
4732 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->image->name);
4733 acfg->fp = fopen (acfg->tmpfname, "w+");
4735 int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
4736 acfg->fp = fdopen (i, "w+");
4738 g_assert (acfg->fp);
4740 acfg->w = img_writer_create (acfg->fp, FALSE);
4742 tmp_outfile_name = NULL;
4743 outfile_name = NULL;
4746 acfg->temp_prefix = img_writer_get_temp_label_prefix (acfg->w);
4748 if (!acfg->aot_opts.nodebug)
4749 acfg->dwarf = mono_dwarf_writer_create (acfg->w, NULL);
4751 img_writer_emit_start (acfg->w);
4754 mono_dwarf_writer_emit_base_info (acfg->dwarf, arch_get_cie_program ());
4760 emit_extra_methods (acfg);
4762 emit_method_order (acfg);
4764 emit_trampolines (acfg);
4766 emit_class_name_table (acfg);
4768 emit_got_info (acfg);
4770 emit_exception_info (acfg);
4772 emit_unwind_info (acfg);
4774 emit_class_info (acfg);
4778 emit_image_table (acfg);
4782 emit_file_info (acfg);
4784 emit_globals (acfg);
4787 emit_dwarf_info (acfg);
4789 emit_mem_end (acfg);
4793 acfg->stats.gen_time = TV_ELAPSED (atv, btv);
4795 printf ("Code: %d Info: %d Ex Info: %d Unwind Info: %d Class Info: %d PLT: %d GOT Info: %d GOT Info Offsets: %d GOT: %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, acfg->stats.got_info_offsets_size, (int)(acfg->got_offset * sizeof (gpointer)));
4798 res = img_writer_emit_writeout (acfg->w);
4803 if (acfg->use_bin_writer) {
4804 int err = rename (tmp_outfile_name, outfile_name);
4807 printf ("Unable to rename '%s' to '%s': %s\n", tmp_outfile_name, outfile_name, strerror (errno));
4811 res = compile_asm (acfg);
4818 acfg->stats.link_time = TV_ELAPSED (atv, btv);
4820 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);
4821 if (acfg->stats.genericcount)
4822 printf ("%d methods are generic (%d%%)\n", acfg->stats.genericcount, acfg->stats.mcount ? (acfg->stats.genericcount * 100) / acfg->stats.mcount : 100);
4823 if (acfg->stats.abscount)
4824 printf ("%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
4825 if (acfg->stats.lmfcount)
4826 printf ("%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
4827 if (acfg->stats.ocount)
4828 printf ("%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
4829 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);
4830 printf ("Direct calls: %d (%d%%)\n", acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
4833 printf ("GOT slot distribution:\n");
4834 for (i = 0; i < MONO_PATCH_INFO_NONE; ++i)
4835 if (acfg->stats.got_slot_types [i])
4836 printf ("\t%s: %d\n", get_patch_name (i), acfg->stats.got_slot_types [i]);
4839 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);
4847 * Support for emitting debug info for JITted code.
4849 * This works as follows:
4850 * - the runtime writes out an xdb.s file containing DWARF debug info.
4851 * - the user calls a gdb macro
4852 * - the macro compiles and loads this shared library using add-symbol-file.
4854 * This is based on the xdebug functionality in the Kaffe Java VM.
4856 * We emit assembly code instead of using the ELF writer, so we can emit debug info
4857 * incrementally as each method is JITted, and the debugger doesn't have to call
4858 * into the runtime to emit the shared library, which would cause all kinds of
4859 * complications, like threading issues, and the fact that the ELF writer's
4860 * emit_writeout () function cannot be called more than once.
4863 /* The recommended gdb macro is: */
4866 shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
4867 add-symbol-file xdb.so 0
4871 static MonoDwarfWriter *xdebug_writer;
4872 static FILE *xdebug_fp;
4875 mono_xdebug_init (void)
4881 xdebug_fp = fopen ("xdb.s", "w");
4883 w = img_writer_create (xdebug_fp, FALSE);
4885 img_writer_emit_start (w);
4887 /* This file will contain the IL code for methods which don't have debug info */
4888 il_file = fopen ("xdb.il", "w");
4890 xdebug_writer = mono_dwarf_writer_create (w, il_file);
4892 /* Emit something so the file has a text segment */
4893 img_writer_emit_section_change (w, ".text", 0);
4894 img_writer_emit_string (w, "");
4896 mono_dwarf_writer_emit_base_info (xdebug_writer, arch_get_cie_program ());
4900 * mono_save_xdebug_info:
4902 * Emit debugging info for METHOD into an assembly file which can be assembled
4903 * and loaded into gdb to provide debugging info for JITted code.
4904 * LOCKING: Acquires the loader lock.
4907 mono_save_xdebug_info (MonoCompile *cfg)
4912 mono_loader_lock ();
4913 mono_dwarf_writer_emit_method (xdebug_writer, cfg, cfg->jit_info->method, NULL, NULL, 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 ()));
4915 mono_loader_unlock ();
4919 * mono_save_trampoline_xdebug_info:
4921 * Same as mono_save_xdebug_info, but for trampolines.
4922 * LOCKING: Acquires the loader lock.
4925 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
4930 mono_loader_lock ();
4931 mono_dwarf_writer_emit_trampoline (xdebug_writer, tramp_name, NULL, NULL, code, code_size, unwind_info);
4933 mono_loader_unlock ();
4941 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
4947 mono_xdebug_init (void)
4952 mono_save_xdebug_info (MonoCompile *cfg)
4957 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)