2 * aot-compiler.c: mono Ahead of Time compiler
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2002 Ximian, Inc.
11 /* Remaining AOT-only work:
12 * - optimize the trampolines, generate more code in the arch files.
13 * - make things more consistent with how elf works, for example, use ELF
15 * Remaining generics sharing work:
16 * - optimize the size of the data which is encoded.
17 * - optimize the runtime loading of data:
18 * - the trampoline code calls mono_jit_info_table_find () to find the rgctx,
19 * which loads the debugging+exception handling info for the method. This is a
20 * huge waste of time and code, since the rgctx structure is currently empty.
21 * - every shared method has a MonoGenericJitInfo structure which is only really
22 * used for handling catch clauses with open types, not a very common use case.
26 #include <sys/types.h>
36 #ifndef PLATFORM_WIN32
46 #include <mono/metadata/tabledefs.h>
47 #include <mono/metadata/class.h>
48 #include <mono/metadata/object.h>
49 #include <mono/metadata/tokentype.h>
50 #include <mono/metadata/appdomain.h>
51 #include <mono/metadata/debug-helpers.h>
52 #include <mono/metadata/assembly.h>
53 #include <mono/metadata/metadata-internals.h>
54 #include <mono/metadata/marshal.h>
55 #include <mono/metadata/gc-internal.h>
56 #include <mono/metadata/monitor.h>
57 #include <mono/metadata/mempool-internals.h>
58 #include <mono/metadata/mono-endian.h>
59 #include <mono/metadata/threads-types.h>
60 #include <mono/utils/mono-logger.h>
61 #include <mono/utils/mono-compiler.h>
62 #include <mono/utils/mono-time.h>
63 #include <mono/utils/mono-mmap.h>
66 #include "image-writer.h"
67 #include "dwarfwriter.h"
69 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
71 #define TV_DECLARE(name) gint64 name
72 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
73 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
76 #define SHARED_EXT ".dll"
77 #elif defined(__ppc__) && defined(__MACH__)
78 #define SHARED_EXT ".dylib"
80 #define SHARED_EXT ".so"
83 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
84 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
85 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
87 typedef struct MonoAotOptions {
90 gboolean write_symbols;
91 gboolean metadata_only;
92 gboolean bind_to_runtime_version;
101 gboolean print_skipped_methods;
104 typedef struct MonoAotStats {
105 int ccount, mcount, lmfcount, abscount, gcount, ocount, genericcount;
106 int code_size, info_size, ex_info_size, unwind_info_size, got_size, class_info_size, got_info_size, got_info_offsets_size;
107 int methods_without_got_slots, direct_calls, all_calls;
109 int got_slot_types [MONO_PATCH_INFO_NONE];
110 int jit_time, gen_time, link_time;
113 typedef struct MonoAotCompile {
116 GHashTable *method_indexes;
119 GHashTable *patch_to_plt_offset;
120 GHashTable *plt_offset_to_patch;
121 GHashTable *patch_to_got_offset;
122 GPtrArray *got_patches;
123 GHashTable *image_hash;
124 GHashTable *method_to_cfg;
125 GHashTable *token_info_hash;
126 GPtrArray *extra_methods;
127 GPtrArray *image_table;
130 guint32 *plt_got_info_offsets;
131 guint32 got_offset, plt_offset, plt_got_offset_base;
132 /* Number of GOT entries reserved for trampolines */
133 guint32 num_trampoline_got_entries;
135 guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
136 guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
137 guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
139 MonoAotOptions aot_opts;
142 MonoMemPool *mempool;
145 char *static_linking_symbol;
146 CRITICAL_SECTION mutex;
147 gboolean use_bin_writer;
149 MonoDwarfWriter *dwarf;
153 GHashTable *unwind_info_offsets;
154 GPtrArray *unwind_ops;
155 guint32 unwind_info_offset;
158 GHashTable *method_label_hash;
159 const char *temp_prefix;
160 guint32 label_generator;
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"
198 static G_GNUC_UNUSED const char*
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));
315 img_writer_emit_local_symbol (acfg->w, name, NULL, func);
317 img_writer_emit_global (acfg->w, name, func);
322 emit_symbol_size (MonoAotCompile *acfg, const char *name, const char *end_label)
324 img_writer_emit_symbol_size (acfg->w, name, end_label);
328 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
330 img_writer_emit_section_change (acfg->w, ".text", 1);
331 emit_global (acfg, name, FALSE);
332 img_writer_emit_label (acfg->w, name);
333 img_writer_emit_string (acfg->w, value);
336 static G_GNUC_UNUSED void
337 emit_uleb128 (MonoAotCompile *acfg, guint32 value)
340 guint8 b = value & 0x7f;
342 if (value != 0) /* more bytes to come */
348 static G_GNUC_UNUSED void
349 emit_sleb128 (MonoAotCompile *acfg, gint64 value)
352 gboolean negative = (value < 0);
359 /* the following is unnecessary if the
360 * implementation of >>= uses an arithmetic rather
361 * than logical shift for a signed left operand
365 value |= - ((gint64)1 <<(size - 7));
366 /* sign bit of byte is second high order bit (0x40) */
367 if ((value == 0 && !(byte & 0x40)) ||
368 (value == -1 && (byte & 0x40)))
372 emit_byte (acfg, byte);
376 static G_GNUC_UNUSED void
377 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
382 guint8 b = value & 0x7f;
384 if (value != 0) /* more bytes to come */
392 static G_GNUC_UNUSED void
393 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
396 gboolean negative = (value < 0);
404 /* the following is unnecessary if the
405 * implementation of >>= uses an arithmetic rather
406 * than logical shift for a signed left operand
410 value |= - (1 <<(size - 7));
411 /* sign bit of byte is second high order bit (0x40) */
412 if ((value == 0 && !(byte & 0x40)) ||
413 (value == -1 && (byte & 0x40)))
423 /* ARCHITECTURE SPECIFIC CODE */
425 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC)
426 #define EMIT_DWARF_INFO 1
429 #if defined(TARGET_ARM)
430 #define AOT_FUNC_ALIGNMENT 4
432 #define AOT_FUNC_ALIGNMENT 16
435 #if defined(TARGET_POWERPC64) && !defined(__mono_ilp32__)
436 #define PPC_LD_OP "ld"
437 #define PPC_LDX_OP "ldx"
439 #define PPC_LD_OP "lwz"
440 #define PPC_LDX_OP "lwzx"
444 * arch_emit_direct_call:
446 * Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
450 arch_emit_direct_call (MonoAotCompile *acfg, const char *target, int *call_size)
452 #if defined(TARGET_X86) || defined(TARGET_AMD64)
453 /* Need to make sure this is exactly 5 bytes long */
454 emit_byte (acfg, '\xe8');
455 emit_symbol_diff (acfg, target, ".", -4);
457 #elif defined(TARGET_ARM)
458 if (acfg->use_bin_writer) {
465 img_writer_emit_reloc (acfg->w, R_ARM_CALL, target, -8);
466 emit_bytes (acfg, buf, 4);
468 img_writer_emit_unset_mode (acfg->w);
469 fprintf (acfg->fp, "bl %s\n", target);
472 #elif defined(TARGET_POWERPC)
473 if (acfg->use_bin_writer) {
474 g_assert_not_reached ();
476 img_writer_emit_unset_mode (acfg->w);
477 fprintf (acfg->fp, "bl %s\n", target);
481 g_assert_not_reached ();
487 * - we use an approach similar to the x86 abi: reserve a register (r30) to hold
489 * - The full-aot trampolines need access to the GOT of mscorlib, so we store
490 * in in the 2. slot of every GOT, and require every method to place the GOT
491 * address in r30, even when it doesn't access the GOT otherwise. This way,
492 * the trampolines can compute the mscorlib GOT address by loading 4(r30).
497 * PPC64 uses function descriptors which greatly complicate all code, since
498 * these are used very inconsistently in the runtime. Some functions like
499 * mono_compile_method () return ftn descriptors, while others like the
500 * trampoline creation functions do not.
501 * We assume that all GOT slots contain function descriptors, and create
502 * descriptors in aot-runtime.c when needed.
503 * The ppc64 abi uses r2 to hold the address of the TOC/GOT, which is loaded
504 * from function descriptors, we could do the same, but it would require
505 * rewriting all the ppc/aot code to handle function descriptors properly.
506 * So instead, we use the same approach as on PPC32.
507 * This is a horrible mess, but fixing it would probably lead to an even bigger
511 #ifdef MONO_ARCH_AOT_SUPPORTED
513 * arch_emit_got_offset:
515 * The memory pointed to by CODE should hold native code for computing the GOT
516 * address. Emit this code while patching it with the offset between code and
517 * the GOT. CODE_SIZE is set to the number of bytes emitted.
520 arch_emit_got_offset (MonoAotCompile *acfg, guint8 *code, int *code_size)
522 #if defined(TARGET_POWERPC64)
523 g_assert (!acfg->use_bin_writer);
524 img_writer_emit_unset_mode (acfg->w);
526 * The ppc32 code doesn't seem to work on ppc64, the assembler complains about
527 * unsupported relocations. So we store the got address into the .Lgot_addr
528 * symbol which is in the text segment, compute its address, and load it.
530 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
531 fprintf (acfg->fp, "lis 0, (.Lgot_addr + 4 - .L%d)@h\n", acfg->label_generator);
532 fprintf (acfg->fp, "ori 0, 0, (.Lgot_addr + 4 - .L%d)@l\n", acfg->label_generator);
533 fprintf (acfg->fp, "add 30, 30, 0\n");
534 fprintf (acfg->fp, "%s 30, 0(30)\n", PPC_LD_OP);
535 acfg->label_generator ++;
537 #elif defined(TARGET_POWERPC)
538 g_assert (!acfg->use_bin_writer);
539 img_writer_emit_unset_mode (acfg->w);
540 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
541 fprintf (acfg->fp, "lis 0, (%s + 4 - .L%d)@h\n", acfg->got_symbol, acfg->label_generator);
542 fprintf (acfg->fp, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg->got_symbol, acfg->label_generator);
543 acfg->label_generator ++;
546 guint32 offset = mono_arch_get_patch_offset (code);
547 emit_bytes (acfg, code, offset);
548 emit_symbol_diff (acfg, acfg->got_symbol, ".", offset);
550 *code_size = offset + 4;
555 * arch_emit_got_access:
557 * The memory pointed to by CODE should hold native code for loading a GOT
558 * slot. Emit this code while patching it so it accesses the GOT slot GOT_SLOT.
559 * CODE_SIZE is set to the number of bytes emitted.
562 arch_emit_got_access (MonoAotCompile *acfg, guint8 *code, int got_slot, int *code_size)
564 /* Emit beginning of instruction */
565 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
567 /* Emit the offset */
569 emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
570 *code_size = mono_arch_get_patch_offset (code) + 4;
571 #elif defined(TARGET_X86)
572 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
573 *code_size = mono_arch_get_patch_offset (code) + 4;
574 #elif defined(TARGET_ARM)
575 emit_symbol_diff (acfg, acfg->got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
576 *code_size = mono_arch_get_patch_offset (code) + 4;
577 #elif defined(TARGET_POWERPC)
583 ppc_load32 (code, ppc_r0, got_slot * sizeof (gpointer));
584 g_assert (code - buf == 8);
585 emit_bytes (acfg, buf, code - buf);
586 *code_size = code - buf;
589 g_assert_not_reached ();
596 * arch_emit_plt_entry:
598 * Emit code for the PLT entry with index INDEX.
601 arch_emit_plt_entry (MonoAotCompile *acfg, int index)
603 #if defined(TARGET_X86)
605 /* It is filled up during loading by the AOT loader. */
606 emit_zero_bytes (acfg, 16);
608 /* Need to make sure this is 9 bytes long */
609 emit_byte (acfg, '\xe9');
610 emit_symbol_diff (acfg, acfg->plt_symbol, ".", -4);
611 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
613 #elif defined(TARGET_AMD64)
615 * We can't emit jumps because they are 32 bits only so they can't be patched.
616 * So we make indirect calls through GOT entries which are patched by the AOT
617 * loader to point to .Lpd entries.
618 * An x86_64 plt entry is 10 bytes long, init_plt () depends on this.
620 /* jmpq *<offset>(%rip) */
621 emit_byte (acfg, '\xff');
622 emit_byte (acfg, '\x25');
623 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) -4);
624 /* Used by mono_aot_get_plt_info_offset */
625 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
626 #elif defined(TARGET_ARM)
631 * - optimize OP_AOTCONST implementation
632 * - optimize the PLT entries
633 * - optimize SWITCH AOT implementation
634 * - implement IMT support
637 if (acfg->use_bin_writer && FALSE) {
638 /* FIXME: mono_arch_patch_plt_entry () needs to decode this */
639 /* We only emit 1 relocation since we implement it ourselves anyway */
640 img_writer_emit_reloc (acfg->w, R_ARM_ALU_PC_G0_NC, acfg->got_symbol, ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) - 8);
641 /* FIXME: A 2 instruction encoding is sufficient in most cases */
642 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_PC, 0, 0);
643 ARM_ADD_REG_IMM (code, ARMREG_IP, ARMREG_IP, 0, 0);
644 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, 0);
645 emit_bytes (acfg, buf, code - buf);
646 /* Used by mono_aot_get_plt_info_offset */
647 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
649 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
650 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
651 emit_bytes (acfg, buf, code - buf);
652 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((acfg->plt_got_offset_base + index) * sizeof (gpointer)) - 4);
653 /* Used by mono_aot_get_plt_info_offset */
654 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
657 * The plt_got_info_offset is computed automatically by
658 * mono_aot_get_plt_info_offset (), so no need to save it here.
660 #elif defined(TARGET_POWERPC)
661 guint32 offset = (acfg->plt_got_offset_base + index) * sizeof (gpointer);
663 /* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
664 g_assert (!acfg->use_bin_writer);
665 img_writer_emit_unset_mode (acfg->w);
666 fprintf (acfg->fp, "lis 11, %d@h\n", offset);
667 fprintf (acfg->fp, "ori 11, 11, %d@l\n", offset);
668 fprintf (acfg->fp, "add 11, 11, 30\n");
669 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
670 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
671 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
672 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
674 fprintf (acfg->fp, "mtctr 11\n");
675 fprintf (acfg->fp, "bctr\n");
676 emit_int32 (acfg, acfg->plt_got_info_offsets [index]);
678 g_assert_not_reached ();
683 * arch_emit_specific_trampoline:
685 * Emit code for a specific trampoline. OFFSET is the offset of the first of
686 * two GOT slots which contain the generic trampoline address and the trampoline
687 * argument. TRAMP_SIZE is set to the size of the emitted trampoline.
690 arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
693 * The trampolines created here are variations of the specific
694 * trampolines created in mono_arch_create_specific_trampoline (). The
696 * - the generic trampoline address is taken from a got slot.
697 * - the offset of the got slot where the trampoline argument is stored
698 * is embedded in the instruction stream, and the generic trampoline
699 * can load the argument by loading the offset, adding it to the
700 * address of the trampoline to get the address of the got slot, and
701 * loading the argument from there.
702 * - all the trampolines should be of the same length.
704 #if defined(TARGET_AMD64)
705 /* This should be exactly 16 bytes long */
707 /* call *<offset>(%rip) */
708 emit_byte (acfg, '\x41');
709 emit_byte (acfg, '\xff');
710 emit_byte (acfg, '\x15');
711 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
712 /* This should be relative to the start of the trampoline */
713 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 19);
714 emit_zero_bytes (acfg, 5);
715 #elif defined(TARGET_ARM)
719 /* This should be exactly 20 bytes long */
722 ARM_PUSH (code, 0x5fff);
723 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 4);
724 /* Load the value from the GOT */
725 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
727 ARM_BLX_REG (code, ARMREG_R1);
729 g_assert (code - buf == 16);
732 emit_bytes (acfg, buf, code - buf);
734 * Only one offset is needed, since the second one would be equal to the
737 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 4);
738 //emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 8);
739 #elif defined(TARGET_POWERPC)
746 g_assert (!acfg->use_bin_writer);
749 * PPC has no ip relative addressing, so we need to compute the address
750 * of the mscorlib got. That is slow and complex, so instead, we store it
751 * in the second got slot of every aot image. The caller already computed
752 * the address of its got and placed it into r30.
754 img_writer_emit_unset_mode (acfg->w);
755 /* Load mscorlib got address */
756 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
757 /* Load generic trampoline address */
758 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
759 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
760 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
761 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
762 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
764 fprintf (acfg->fp, "mtctr 11\n");
765 /* Load trampoline argument */
766 /* On ppc, we pass it normally to the generic trampoline */
767 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
768 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
769 fprintf (acfg->fp, "%s 0, 11, 0\n", PPC_LDX_OP);
770 /* Branch to generic trampoline */
771 fprintf (acfg->fp, "bctr\n");
773 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
774 *tramp_size = 10 * 4;
779 g_assert_not_reached ();
784 * arch_emit_unbox_trampoline:
786 * Emit code for the unbox trampoline for METHOD used in the full-aot case.
787 * CALL_TARGET is the symbol pointing to the native code of METHOD.
790 arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoMethod *method, MonoGenericSharingContext *gsctx, const char *call_target)
792 #if defined(TARGET_AMD64)
797 this_reg = mono_arch_get_this_arg_reg (mono_method_signature (method), gsctx, NULL);
799 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
801 emit_bytes (acfg, buf, code - buf);
803 emit_byte (acfg, '\xe9');
804 emit_symbol_diff (acfg, call_target, ".", -4);
805 #elif defined(TARGET_ARM)
812 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
815 ARM_ADD_REG_IMM8 (code, this_pos, this_pos, sizeof (MonoObject));
817 emit_bytes (acfg, buf, code - buf);
819 if (acfg->use_bin_writer) {
826 img_writer_emit_reloc (acfg->w, R_ARM_JUMP24, call_target, -8);
827 emit_bytes (acfg, buf, 4);
829 fprintf (acfg->fp, "\n\tb %s\n", call_target);
831 #elif defined(TARGET_POWERPC)
834 if (MONO_TYPE_ISSTRUCT (mono_method_signature (method)->ret))
837 g_assert (!acfg->use_bin_writer);
839 fprintf (acfg->fp, "\n\taddi %d, %d, %d\n", this_pos, this_pos, (int)sizeof (MonoObject));
840 fprintf (acfg->fp, "\n\tb %s\n", call_target);
842 g_assert_not_reached ();
847 * arch_emit_static_rgctx_trampoline:
849 * Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
850 * two GOT slots which contain the rgctx argument, and the method to jump to.
851 * TRAMP_SIZE is set to the size of the emitted trampoline.
852 * These kinds of trampolines cannot be enumerated statically, since there could
853 * be one trampoline per method instantiation, so we emit the same code for all
854 * trampolines, and parameterize them using two GOT slots.
857 arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
859 #if defined(TARGET_AMD64)
860 /* This should be exactly 13 bytes long */
863 /* mov <OFFSET>(%rip), %r10 */
864 emit_byte (acfg, '\x4d');
865 emit_byte (acfg, '\x8b');
866 emit_byte (acfg, '\x15');
867 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
869 /* jmp *<offset>(%rip) */
870 emit_byte (acfg, '\xff');
871 emit_byte (acfg, '\x25');
872 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
873 #elif defined(TARGET_ARM)
877 /* This should be exactly 24 bytes long */
880 /* Load rgctx value */
881 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8);
882 ARM_LDR_REG_REG (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, ARMREG_R1);
883 /* Load branch addr + branch */
884 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 4);
885 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_R1);
887 g_assert (code - buf == 16);
890 emit_bytes (acfg, buf, code - buf);
891 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 8);
892 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 4);
893 #elif defined(TARGET_POWERPC)
900 g_assert (!acfg->use_bin_writer);
903 * PPC has no ip relative addressing, so we need to compute the address
904 * of the mscorlib got. That is slow and complex, so instead, we store it
905 * in the second got slot of every aot image. The caller already computed
906 * the address of its got and placed it into r30.
908 img_writer_emit_unset_mode (acfg->w);
909 /* Load mscorlib got address */
910 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
912 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
913 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
914 fprintf (acfg->fp, "%s %d, 11, 0\n", PPC_LDX_OP, MONO_ARCH_RGCTX_REG);
915 /* Load target address */
916 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
917 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
918 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
919 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
920 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
921 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
923 fprintf (acfg->fp, "mtctr 11\n");
924 /* Branch to the target address */
925 fprintf (acfg->fp, "bctr\n");
927 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
928 *tramp_size = 11 * 4;
934 g_assert_not_reached ();
939 * arch_emit_imt_thunk:
941 * Emit an IMT thunk usable in full-aot mode. The thunk uses 1 got slot which
942 * points to an array of pointer pairs. The pairs of the form [key, ptr], where
943 * key is the IMT key, and ptr holds the address of a memory location holding
944 * the address to branch to if the IMT arg matches the key. The array is
945 * terminated by a pair whose key is NULL, and whose ptr is the address of the
947 * TRAMP_SIZE is set to the size of the emitted trampoline.
950 arch_emit_imt_thunk (MonoAotCompile *acfg, int offset, int *tramp_size)
952 #if defined(TARGET_AMD64)
956 code = buf = g_malloc (256);
958 /* FIXME: Optimize this, i.e. use binary search etc. */
959 /* Maybe move the body into a separate function (slower, but much smaller) */
961 /* R10 is a free register */
964 amd64_alu_membase_imm (code, X86_CMP, AMD64_R10, 0, 0);
966 amd64_branch8 (code, X86_CC_Z, FALSE, 0);
969 amd64_alu_membase_reg (code, X86_CMP, AMD64_R10, 0, MONO_ARCH_IMT_REG);
971 amd64_branch8 (code, X86_CC_Z, FALSE, 0);
974 amd64_alu_reg_imm (code, X86_ADD, AMD64_R10, 2 * sizeof (gpointer));
975 amd64_jump_code (code, labels [0]);
978 mono_amd64_patch (labels [2], code);
979 amd64_mov_reg_membase (code, AMD64_R10, AMD64_R10, sizeof (gpointer), 8);
980 amd64_jump_membase (code, AMD64_R10, 0);
984 mono_amd64_patch (labels [1], code);
985 x86_breakpoint (code);
987 /* mov <OFFSET>(%rip), %r10 */
988 emit_byte (acfg, '\x4d');
989 emit_byte (acfg, '\x8b');
990 emit_byte (acfg, '\x15');
991 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
993 emit_bytes (acfg, buf, code - buf);
995 *tramp_size = code - buf + 7;
996 #elif defined(TARGET_ARM)
998 guint8 *code, *code2, *labels [16];
1002 /* The IMT method is in v5 */
1004 /* Only IP is available, but we need at least two free registers */
1005 ARM_PUSH1 (code, ARMREG_R1);
1007 /* Load the parameter from the GOT */
1008 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1009 ARM_LDR_REG_REG (code, ARMREG_IP, ARMREG_PC, ARMREG_IP);
1012 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_IP, 0);
1013 ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
1015 ARM_B_COND (code, ARMCOND_EQ, 0);
1017 /* End-of-loop check */
1018 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
1020 ARM_B_COND (code, ARMCOND_EQ, 0);
1023 ARM_ADD_REG_IMM8 (code, ARMREG_IP, ARMREG_IP, sizeof (gpointer) * 2);
1026 arm_patch (labels [4], labels [1]);
1029 arm_patch (labels [2], code);
1030 ARM_POP1 (code, ARMREG_R1);
1031 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_IP, 4);
1032 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, 0);
1035 arm_patch (labels [3], code);
1040 ARM_LDR_IMM (code2, ARMREG_IP, ARMREG_PC, (code - (labels [0] + 8)));
1042 emit_bytes (acfg, buf, code - buf);
1043 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + (code - (labels [0] + 8)) - 4);
1045 *tramp_size = code - buf + 4;
1046 #elif defined(TARGET_POWERPC)
1048 guint8 *code, *labels [16];
1052 /* Load the mscorlib got address */
1053 ppc_ldptr (code, ppc_r11, sizeof (gpointer), ppc_r30);
1054 /* Load the parameter from the GOT */
1055 ppc_load (code, ppc_r0, offset * sizeof (gpointer));
1056 ppc_ldptr_indexed (code, ppc_r11, ppc_r11, ppc_r0);
1058 /* Load and check key */
1060 ppc_ldptr (code, ppc_r0, 0, ppc_r11);
1061 ppc_cmp (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, MONO_ARCH_IMT_REG);
1063 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
1065 /* End-of-loop check */
1066 ppc_cmpi (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, 0);
1068 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
1071 ppc_addi (code, ppc_r11, ppc_r11, 2 * sizeof (gpointer));
1074 mono_ppc_patch (labels [4], labels [1]);
1077 mono_ppc_patch (labels [2], code);
1078 ppc_ldptr (code, ppc_r11, sizeof (gpointer), ppc_r11);
1079 /* r11 now contains the value of the vtable slot */
1080 /* this is not a function descriptor on ppc64 */
1081 ppc_ldptr (code, ppc_r11, 0, ppc_r11);
1082 ppc_mtctr (code, ppc_r11);
1083 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
1086 mono_ppc_patch (labels [3], code);
1090 *tramp_size = code - buf;
1092 emit_bytes (acfg, buf, code - buf);
1094 g_assert_not_reached ();
1099 * arch_get_cie_program:
1101 * Get the unwind bytecode for the DWARF CIE.
1104 arch_get_cie_program (void)
1109 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, AMD64_RSP, 8);
1110 mono_add_unwind_op_offset (l, (guint8*)NULL, (guint8*)NULL, AMD64_RIP, -8);
1113 #elif defined(TARGET_POWERPC)
1116 mono_add_unwind_op_def_cfa (l, (guint8*)NULL, (guint8*)NULL, ppc_r1, 0);
1124 /* END OF ARCH SPECIFIC CODE */
1127 mono_get_field_token (MonoClassField *field)
1129 MonoClass *klass = field->parent;
1132 for (i = 0; i < klass->field.count; ++i) {
1133 if (field == &klass->fields [i])
1134 return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
1137 g_assert_not_reached ();
1142 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
1146 //printf ("ENCODE: %d 0x%x.\n", value, value);
1149 * Same encoding as the one used in the metadata, extended to handle values
1150 * greater than 0x1fffffff.
1152 if ((value >= 0) && (value <= 127))
1154 else if ((value >= 0) && (value <= 16383)) {
1155 p [0] = 0x80 | (value >> 8);
1156 p [1] = value & 0xff;
1158 } else if ((value >= 0) && (value <= 0x1fffffff)) {
1159 p [0] = (value >> 24) | 0xc0;
1160 p [1] = (value >> 16) & 0xff;
1161 p [2] = (value >> 8) & 0xff;
1162 p [3] = value & 0xff;
1167 p [1] = (value >> 24) & 0xff;
1168 p [2] = (value >> 16) & 0xff;
1169 p [3] = (value >> 8) & 0xff;
1170 p [4] = value & 0xff;
1178 get_image_index (MonoAotCompile *cfg, MonoImage *image)
1182 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
1186 index = g_hash_table_size (cfg->image_hash);
1187 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
1188 g_ptr_array_add (cfg->image_table, image);
1194 find_typespec_for_class (MonoAotCompile *acfg, MonoClass *klass)
1197 MonoClass *k = NULL;
1199 /* FIXME: Search referenced images as well */
1200 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
1201 k = mono_class_get_full (acfg->image, MONO_TOKEN_TYPE_SPEC | (i + 1), NULL);
1206 if (i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows)
1207 return MONO_TOKEN_TYPE_SPEC | (i + 1);
1213 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf);
1218 * Encode a reference to KLASS. We use our home-grown encoding instead of the
1219 * standard metadata encoding.
1222 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
1226 if (klass->generic_class) {
1228 g_assert (klass->type_token);
1230 /* Find a typespec for a class if possible */
1231 token = find_typespec_for_class (acfg, klass);
1233 encode_value (token, p, &p);
1234 encode_value (get_image_index (acfg, acfg->image), p, &p);
1236 MonoClass *gclass = klass->generic_class->container_class;
1237 MonoGenericInst *inst = klass->generic_class->context.class_inst;
1240 /* Encode it ourselves */
1242 encode_value (MONO_TOKEN_TYPE_SPEC, p, &p);
1243 encode_value (MONO_TYPE_GENERICINST, p, &p);
1244 encode_klass_ref (acfg, gclass, p, &p);
1245 encode_value (inst->type_argc, p, &p);
1246 for (i = 0; i < inst->type_argc; ++i)
1247 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
1249 } else if (klass->type_token) {
1250 g_assert (mono_metadata_token_code (klass->type_token) == MONO_TOKEN_TYPE_DEF);
1251 encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, p, &p);
1252 encode_value (get_image_index (acfg, klass->image), p, &p);
1253 } else if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR)) {
1254 MonoGenericContainer *container = mono_type_get_generic_param_owner (&klass->byval_arg);
1255 g_assert (container);
1258 encode_value (MONO_TOKEN_TYPE_SPEC, p, &p);
1259 encode_value (klass->byval_arg.type, p, &p);
1261 encode_value (mono_type_get_generic_param_num (&klass->byval_arg), p, &p);
1263 encode_value (container->is_method, p, &p);
1264 if (container->is_method)
1265 encode_method_ref (acfg, container->owner.method, p, &p);
1267 encode_klass_ref (acfg, container->owner.klass, p, &p);
1270 g_assert (klass->rank > 0);
1271 encode_value (MONO_TOKEN_TYPE_DEF, p, &p);
1272 encode_value (get_image_index (acfg, klass->image), p, &p);
1273 encode_value (klass->rank, p, &p);
1274 encode_klass_ref (acfg, klass->element_class, p, &p);
1280 encode_field_info (MonoAotCompile *cfg, MonoClassField *field, guint8 *buf, guint8 **endbuf)
1282 guint32 token = mono_get_field_token (field);
1285 encode_klass_ref (cfg, field->parent, p, &p);
1286 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
1287 encode_value (token - MONO_TOKEN_FIELD_DEF, p, &p);
1292 encode_generic_context (MonoAotCompile *acfg, MonoGenericContext *context, guint8 *buf, guint8 **endbuf)
1296 MonoGenericInst *inst;
1298 /* Encode the context */
1299 inst = context->class_inst;
1300 encode_value (inst ? 1 : 0, p, &p);
1302 encode_value (inst->type_argc, p, &p);
1303 for (i = 0; i < inst->type_argc; ++i)
1304 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
1306 inst = context->method_inst;
1307 encode_value (inst ? 1 : 0, p, &p);
1309 encode_value (inst->type_argc, p, &p);
1310 for (i = 0; i < inst->type_argc; ++i)
1311 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
1317 #define MAX_IMAGE_INDEX 250
1320 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf)
1322 guint32 image_index = get_image_index (acfg, method->klass->image);
1323 guint32 token = method->token;
1324 MonoJumpInfoToken *ji;
1329 * The encoding for most methods is as follows:
1330 * - image index encoded as a leb128
1331 * - token index encoded as a leb128
1332 * Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
1333 * types of method encodings.
1336 g_assert (image_index < MONO_AOT_METHODREF_MIN);
1338 /* Mark methods which can't use aot trampolines because they need the further
1339 * processing in mono_magic_trampoline () which requires a MonoMethod*.
1341 if ((method->is_generic && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) ||
1342 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
1343 encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE << 24), p, &p);
1346 * Some wrapper methods are shared using their signature, encode their
1347 * stringified signature instead.
1348 * FIXME: Optimize disk usage
1351 if (method->wrapper_type) {
1352 if (method->wrapper_type == MONO_WRAPPER_RUNTIME_INVOKE) {
1353 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1354 if (mono_marshal_method_from_wrapper (method) != method) {
1355 /* Direct wrapper, encode it normally */
1357 name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
1360 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
1361 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1362 name = g_strdup_printf ("(wrapper delegate-invoke):%s (%s)", method->name, tmpsig);
1364 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_BEGIN_INVOKE) {
1365 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1366 name = g_strdup_printf ("(wrapper delegate-begin-invoke):%s (%s)", method->name, tmpsig);
1368 } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_END_INVOKE) {
1369 char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
1370 name = g_strdup_printf ("(wrapper delegate-end-invoke):%s (%s)", method->name, tmpsig);
1376 encode_value ((MONO_AOT_METHODREF_WRAPPER_NAME << 24), p, &p);
1377 strcpy ((char*)p, name);
1378 p += strlen (name) + 1;
1380 } else if (method->wrapper_type) {
1381 encode_value ((MONO_AOT_METHODREF_WRAPPER << 24), p, &p);
1383 encode_value (method->wrapper_type, p, &p);
1385 switch (method->wrapper_type) {
1386 case MONO_WRAPPER_REMOTING_INVOKE:
1387 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
1388 case MONO_WRAPPER_XDOMAIN_INVOKE: {
1391 m = mono_marshal_method_from_wrapper (method);
1393 encode_method_ref (acfg, m, p, &p);
1396 case MONO_WRAPPER_PROXY_ISINST:
1397 case MONO_WRAPPER_LDFLD:
1398 case MONO_WRAPPER_LDFLDA:
1399 case MONO_WRAPPER_STFLD:
1400 case MONO_WRAPPER_ISINST: {
1401 MonoClass *proxy_class = (MonoClass*)mono_marshal_method_from_wrapper (method);
1402 encode_klass_ref (acfg, proxy_class, p, &p);
1405 case MONO_WRAPPER_LDFLD_REMOTE:
1406 case MONO_WRAPPER_STFLD_REMOTE:
1408 case MONO_WRAPPER_ALLOC: {
1409 int alloc_type = mono_gc_get_managed_allocator_type (method);
1410 g_assert (alloc_type != -1);
1411 encode_value (alloc_type, p, &p);
1414 case MONO_WRAPPER_STELEMREF:
1416 case MONO_WRAPPER_UNKNOWN:
1417 if (strcmp (method->name, "FastMonitorEnter") == 0)
1418 encode_value (MONO_AOT_WRAPPER_MONO_ENTER, p, &p);
1419 else if (strcmp (method->name, "FastMonitorExit") == 0)
1420 encode_value (MONO_AOT_WRAPPER_MONO_EXIT, p, &p);
1422 g_assert_not_reached ();
1424 case MONO_WRAPPER_SYNCHRONIZED:
1425 case MONO_WRAPPER_MANAGED_TO_NATIVE:
1426 case MONO_WRAPPER_RUNTIME_INVOKE: {
1429 m = mono_marshal_method_from_wrapper (method);
1431 g_assert (m != method);
1432 encode_method_ref (acfg, m, p, &p);
1436 g_assert_not_reached ();
1438 } else if (mono_method_signature (method)->is_inflated) {
1440 * This is a generic method, find the original token which referenced it and
1442 * Obtain the token from information recorded by the JIT.
1444 ji = g_hash_table_lookup (acfg->token_info_hash, method);
1446 image_index = get_image_index (acfg, ji->image);
1447 g_assert (image_index < MAX_IMAGE_INDEX);
1450 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
1451 encode_value (image_index, p, &p);
1452 encode_value (token, p, &p);
1454 MonoMethod *declaring;
1455 MonoGenericContext *context = mono_method_get_context (method);
1457 g_assert (method->is_inflated);
1458 declaring = ((MonoMethodInflated*)method)->declaring;
1461 * This might be a non-generic method of a generic instance, which
1462 * doesn't have a token since the reference is generated by the JIT
1463 * like Nullable:Box/Unbox, or by generic sharing.
1466 encode_value ((MONO_AOT_METHODREF_GINST << 24), p, &p);
1467 /* Encode the klass */
1468 encode_klass_ref (acfg, method->klass, p, &p);
1469 /* Encode the method */
1470 image_index = get_image_index (acfg, method->klass->image);
1471 g_assert (image_index < MAX_IMAGE_INDEX);
1472 g_assert (declaring->token);
1473 token = declaring->token;
1474 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
1475 encode_value (image_index, p, &p);
1476 encode_value (token, p, &p);
1477 encode_generic_context (acfg, context, p, &p);
1479 } else if (token == 0) {
1480 /* This might be a method of a constructed type like int[,].Set */
1481 /* Obtain the token from information recorded by the JIT */
1482 ji = g_hash_table_lookup (acfg->token_info_hash, method);
1484 image_index = get_image_index (acfg, ji->image);
1485 g_assert (image_index < MAX_IMAGE_INDEX);
1488 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
1489 encode_value (image_index, p, &p);
1490 encode_value (token, p, &p);
1493 g_assert (method->klass->rank);
1495 /* Encode directly */
1496 encode_value ((MONO_AOT_METHODREF_ARRAY << 24), p, &p);
1497 encode_klass_ref (acfg, method->klass, p, &p);
1498 if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank)
1499 encode_value (0, p, &p);
1500 else if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == method->klass->rank * 2)
1501 encode_value (1, p, &p);
1502 else if (!strcmp (method->name, "Get"))
1503 encode_value (2, p, &p);
1504 else if (!strcmp (method->name, "Address"))
1505 encode_value (3, p, &p);
1506 else if (!strcmp (method->name, "Set"))
1507 encode_value (4, p, &p);
1509 g_assert_not_reached ();
1512 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
1513 encode_value ((image_index << 24) | mono_metadata_token_index (token), p, &p);
1519 compare_patches (gconstpointer a, gconstpointer b)
1523 i = (*(MonoJumpInfo**)a)->ip.i;
1524 j = (*(MonoJumpInfo**)b)->ip.i;
1538 * Return whenever PATCH_INFO refers to a direct call, and thus requires a
1541 static inline gboolean
1542 is_plt_patch (MonoJumpInfo *patch_info)
1544 switch (patch_info->type) {
1545 case MONO_PATCH_INFO_METHOD:
1546 case MONO_PATCH_INFO_INTERNAL_METHOD:
1547 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
1548 case MONO_PATCH_INFO_ICALL_ADDR:
1549 case MONO_PATCH_INFO_CLASS_INIT:
1550 case MONO_PATCH_INFO_RGCTX_FETCH:
1551 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
1552 case MONO_PATCH_INFO_MONITOR_ENTER:
1553 case MONO_PATCH_INFO_MONITOR_EXIT:
1561 get_plt_offset (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
1565 if (is_plt_patch (patch_info)) {
1566 int idx = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_plt_offset, patch_info));
1568 if (patch_info->type == MONO_PATCH_INFO_METHOD && (patch_info->data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
1570 * Allocate a separate PLT slot for each such patch, since some plt
1571 * entries will refer to the method itself, and some will refer to
1580 MonoJumpInfo *new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
1582 res = acfg->plt_offset;
1583 g_hash_table_insert (acfg->plt_offset_to_patch, GUINT_TO_POINTER (res), new_ji);
1584 g_hash_table_insert (acfg->patch_to_plt_offset, new_ji, GUINT_TO_POINTER (res));
1585 acfg->plt_offset ++;
1595 * Returns the offset of the GOT slot where the runtime object resulting from resolving
1596 * JI could be found if it exists, otherwise allocates a new one.
1599 get_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
1603 got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_got_offset, ji));
1605 return got_offset - 1;
1607 got_offset = acfg->got_offset;
1608 acfg->got_offset ++;
1610 acfg->stats.got_slots ++;
1611 acfg->stats.got_slot_types [ji->type] ++;
1613 g_hash_table_insert (acfg->patch_to_got_offset, ji, GUINT_TO_POINTER (got_offset + 1));
1614 g_ptr_array_add (acfg->got_patches, ji);
1619 /* Add a method to the list of methods which need to be emitted */
1621 add_method_with_index (MonoAotCompile *acfg, MonoMethod *method, int index, gboolean extra)
1624 if (!g_hash_table_lookup (acfg->method_indexes, method)) {
1625 g_ptr_array_add (acfg->methods, method);
1626 g_hash_table_insert (acfg->method_indexes, method, GUINT_TO_POINTER (index + 1));
1627 acfg->nmethods = acfg->methods->len + 1;
1630 if (method->wrapper_type || extra)
1631 g_ptr_array_add (acfg->extra_methods, method);
1635 get_method_index (MonoAotCompile *acfg, MonoMethod *method)
1637 int index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
1645 add_method_full (MonoAotCompile *acfg, MonoMethod *method, gboolean extra)
1649 index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
1653 index = acfg->method_index;
1654 add_method_with_index (acfg, method, index, extra);
1656 /* FIXME: Fix quadratic behavior */
1657 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (index));
1659 acfg->method_index ++;
1665 add_method (MonoAotCompile *acfg, MonoMethod *method)
1667 return add_method_full (acfg, method, FALSE);
1671 add_extra_method (MonoAotCompile *acfg, MonoMethod *method)
1673 add_method_full (acfg, method, TRUE);
1677 add_jit_icall_wrapper (gpointer key, gpointer value, gpointer user_data)
1679 MonoAotCompile *acfg = user_data;
1680 MonoJitICallInfo *callinfo = value;
1681 MonoMethod *wrapper;
1687 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
1688 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
1691 add_method (acfg, wrapper);
1695 get_runtime_invoke_sig (MonoMethodSignature *sig)
1697 MonoMethodBuilder *mb;
1700 mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
1701 m = mono_mb_create_method (mb, sig, 16);
1702 return mono_marshal_get_runtime_invoke (m, FALSE);
1706 add_wrappers (MonoAotCompile *acfg)
1708 MonoMethod *method, *m;
1710 MonoMethodSignature *sig, *csig;
1714 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
1715 * so there is only one wrapper of a given type, or inlining their contents into their
1720 * FIXME: This depends on the fact that different wrappers have different
1724 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1726 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1727 gboolean skip = FALSE;
1729 method = mono_get_method (acfg->image, token, NULL);
1731 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1732 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
1733 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
1736 if (method->is_generic || method->klass->generic_container)
1739 /* Skip methods which can not be handled by get_runtime_invoke () */
1740 sig = mono_method_signature (method);
1741 if ((sig->ret->type == MONO_TYPE_PTR) ||
1742 (sig->ret->type == MONO_TYPE_TYPEDBYREF))
1745 for (j = 0; j < sig->param_count; j++) {
1746 if (sig->params [j]->type == MONO_TYPE_TYPEDBYREF)
1751 add_method (acfg, mono_marshal_get_runtime_invoke (method, FALSE));
1754 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
1755 #ifdef MONO_ARCH_HAVE_TLS_GET
1756 MonoMethodDesc *desc;
1757 MonoMethod *orig_method;
1761 /* Runtime invoke wrappers */
1763 /* void runtime-invoke () [.cctor] */
1764 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
1765 csig->ret = &mono_defaults.void_class->byval_arg;
1766 add_method (acfg, get_runtime_invoke_sig (csig));
1768 /* void runtime-invoke () [Finalize] */
1769 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
1771 csig->ret = &mono_defaults.void_class->byval_arg;
1772 add_method (acfg, get_runtime_invoke_sig (csig));
1774 /* void runtime-invoke (string) [exception ctor] */
1775 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
1777 csig->ret = &mono_defaults.void_class->byval_arg;
1778 csig->params [0] = &mono_defaults.string_class->byval_arg;
1779 add_method (acfg, get_runtime_invoke_sig (csig));
1781 /* void runtime-invoke (string, string) [exception ctor] */
1782 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
1784 csig->ret = &mono_defaults.void_class->byval_arg;
1785 csig->params [0] = &mono_defaults.string_class->byval_arg;
1786 csig->params [1] = &mono_defaults.string_class->byval_arg;
1787 add_method (acfg, get_runtime_invoke_sig (csig));
1789 /* string runtime-invoke () [Exception.ToString ()] */
1790 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
1792 csig->ret = &mono_defaults.string_class->byval_arg;
1793 add_method (acfg, get_runtime_invoke_sig (csig));
1795 /* void runtime-invoke (string, Exception) [exception ctor] */
1796 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
1798 csig->ret = &mono_defaults.void_class->byval_arg;
1799 csig->params [0] = &mono_defaults.string_class->byval_arg;
1800 csig->params [1] = &mono_defaults.exception_class->byval_arg;
1801 add_method (acfg, get_runtime_invoke_sig (csig));
1803 /* Assembly runtime-invoke (string, bool) [DoAssemblyResolve] */
1804 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
1806 csig->ret = &(mono_class_from_name (
1807 mono_defaults.corlib, "System.Reflection", "Assembly"))->byval_arg;
1808 csig->params [0] = &mono_defaults.string_class->byval_arg;
1809 csig->params [1] = &mono_defaults.boolean_class->byval_arg;
1810 add_method (acfg, get_runtime_invoke_sig (csig));
1812 /* runtime-invoke used by finalizers */
1813 add_method (acfg, mono_marshal_get_runtime_invoke (mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE));
1815 /* JIT icall wrappers */
1816 /* FIXME: locking */
1817 g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
1820 add_method (acfg, mono_marshal_get_stelemref ());
1822 #ifdef MONO_ARCH_HAVE_TLS_GET
1823 /* Managed Allocators */
1824 nallocators = mono_gc_get_managed_allocator_types ();
1825 for (i = 0; i < nallocators; ++i) {
1826 m = mono_gc_get_managed_allocator_by_type (i);
1828 add_method (acfg, m);
1831 /* Monitor Enter/Exit */
1832 desc = mono_method_desc_new ("Monitor:Enter", FALSE);
1833 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
1834 g_assert (orig_method);
1835 mono_method_desc_free (desc);
1836 method = mono_monitor_get_fast_path (orig_method);
1838 add_method (acfg, method);
1840 desc = mono_method_desc_new ("Monitor:Exit", FALSE);
1841 orig_method = mono_method_desc_search_in_class (desc, mono_defaults.monitor_class);
1842 g_assert (orig_method);
1843 mono_method_desc_free (desc);
1844 method = mono_monitor_get_fast_path (orig_method);
1846 add_method (acfg, method);
1851 * remoting-invoke-with-check wrappers are very frequent, so avoid emitting them,
1852 * we use the original method instead at runtime.
1853 * Since full-aot doesn't support remoting, this is not a problem.
1856 /* remoting-invoke wrappers */
1857 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1858 MonoMethodSignature *sig;
1860 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1861 method = mono_get_method (acfg->image, token, NULL);
1863 sig = mono_method_signature (method);
1865 if (sig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
1866 m = mono_marshal_get_remoting_invoke_with_check (method);
1868 add_method (acfg, m);
1873 /* delegate-invoke wrappers */
1874 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
1877 token = MONO_TOKEN_TYPE_DEF | (i + 1);
1878 klass = mono_class_get (acfg->image, token);
1880 if (klass->delegate && klass != mono_defaults.delegate_class && klass != mono_defaults.multicastdelegate_class && !klass->generic_container) {
1881 method = mono_get_delegate_invoke (klass);
1883 m = mono_marshal_get_delegate_invoke (method, NULL);
1885 add_method (acfg, m);
1887 method = mono_class_get_method_from_name_flags (klass, "BeginInvoke", -1, 0);
1888 add_method (acfg, mono_marshal_get_delegate_begin_invoke (method));
1890 method = mono_class_get_method_from_name_flags (klass, "EndInvoke", -1, 0);
1891 add_method (acfg, mono_marshal_get_delegate_end_invoke (method));
1895 /* Synchronized wrappers */
1896 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1897 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1898 method = mono_get_method (acfg->image, token, NULL);
1900 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)
1901 add_method (acfg, mono_marshal_get_synchronized_wrapper (method));
1904 /* pinvoke wrappers */
1905 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1907 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1909 method = mono_get_method (acfg->image, token, NULL);
1911 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
1912 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
1913 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
1917 /* StructureToPtr/PtrToStructure wrappers */
1918 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
1921 token = MONO_TOKEN_TYPE_DEF | (i + 1);
1922 klass = mono_class_get (acfg->image, token);
1924 if (klass->valuetype && !klass->generic_container && ((klass->flags & TYPE_ATTRIBUTE_LAYOUT_MASK) != TYPE_ATTRIBUTE_AUTO_LAYOUT)) {
1925 gboolean can_marshal = TRUE;
1926 gpointer iter = NULL;
1927 MonoClassField *field;
1929 /* Only allow a few field types to avoid asserts in the marshalling code */
1930 while ((field = mono_class_get_fields (klass, &iter))) {
1931 switch (field->type->type) {
1936 case MONO_TYPE_BOOLEAN:
1939 case MONO_TYPE_CHAR:
1947 can_marshal = FALSE;
1953 add_method (acfg, mono_marshal_get_struct_to_ptr (klass));
1954 add_method (acfg, mono_marshal_get_ptr_to_struct (klass));
1961 has_type_vars (MonoClass *klass)
1963 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
1966 return has_type_vars (klass->element_class);
1967 if (klass->generic_class) {
1968 MonoGenericContext *context = &klass->generic_class->context;
1969 if (context->class_inst) {
1972 for (i = 0; i < context->class_inst->type_argc; ++i)
1973 if (has_type_vars (mono_class_from_mono_type (context->class_inst->type_argv [i])))
1981 method_has_type_vars (MonoMethod *method)
1983 if (has_type_vars (method->klass))
1986 if (method->is_inflated) {
1987 MonoGenericContext *context = mono_method_get_context (method);
1988 if (context->method_inst) {
1991 for (i = 0; i < context->method_inst->type_argc; ++i)
1992 if (has_type_vars (mono_class_from_mono_type (context->method_inst->type_argv [i])))
2000 * add_generic_class:
2002 * Add all methods of a generic class.
2005 add_generic_class (MonoAotCompile *acfg, MonoClass *klass)
2010 mono_class_init (klass);
2012 if (klass->generic_class && klass->generic_class->context.class_inst->is_open)
2015 if (has_type_vars (klass))
2018 if (!klass->generic_class && !klass->rank)
2022 while ((method = mono_class_get_methods (klass, &iter))) {
2023 if (mono_method_is_generic_sharable_impl (method, FALSE))
2027 if (method->is_generic)
2032 * FIXME: Instances which are referenced by these methods are not added,
2033 * for example Array.Resize<int> for List<int>.Add ().
2035 add_extra_method (acfg, method);
2039 * For ICollection<T>, where T is a vtype, add instances of the helper methods
2040 * in Array, since a T[] could be cast to ICollection<T>.
2042 if (klass->image == mono_defaults.corlib && !strcmp (klass->name_space, "System.Collections.Generic") &&
2043 (!strcmp(klass->name, "ICollection`1") || !strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IList`1") || !strcmp (klass->name, "IEnumerator`1")) &&
2044 MONO_TYPE_ISSTRUCT (klass->generic_class->context.class_inst->type_argv [0])) {
2045 MonoClass *tclass = mono_class_from_mono_type (klass->generic_class->context.class_inst->type_argv [0]);
2046 MonoClass *array_class = mono_bounded_array_class_get (tclass, 1, FALSE);
2050 if (!strcmp (klass->name, "IEnumerator`1"))
2051 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, "IEnumerable`1");
2053 name_prefix = g_strdup_printf ("%s.%s", klass->name_space, klass->name);
2055 /* Add the T[]/InternalEnumerator class */
2056 if (!strcmp (klass->name, "IEnumerable`1") || !strcmp (klass->name, "IEnumerator`1")) {
2060 while ((nclass = mono_class_get_nested_types (array_class->parent, &iter))) {
2061 if (!strcmp (nclass->name, "InternalEnumerator`1"))
2065 nclass = mono_class_inflate_generic_class (nclass, mono_generic_class_get_context (klass->generic_class));
2066 add_generic_class (acfg, nclass);
2070 while ((method = mono_class_get_methods (array_class, &iter))) {
2071 if (strstr (method->name, name_prefix))
2072 add_extra_method (acfg, method);
2075 g_free (name_prefix);
2080 * add_generic_instances:
2082 * Add instances referenced by the METHODSPEC/TYPESPEC table.
2085 add_generic_instances (MonoAotCompile *acfg)
2090 MonoMethodHeader *header;
2091 MonoMethodSignature *sig;
2092 MonoGenericContext *context;
2094 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHODSPEC].rows; ++i) {
2095 token = MONO_TOKEN_METHOD_SPEC | (i + 1);
2096 method = mono_get_method (acfg->image, token, NULL);
2098 context = mono_method_get_context (method);
2099 if (context && ((context->class_inst && context->class_inst->is_open) ||
2100 (context->method_inst && context->method_inst->is_open)))
2103 if (method->klass->image != acfg->image)
2106 if (mono_method_is_generic_sharable_impl (method, FALSE))
2110 add_extra_method (acfg, method);
2113 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
2116 token = MONO_TOKEN_TYPE_SPEC | (i + 1);
2118 klass = mono_class_get (acfg->image, token);
2119 if (!klass || klass->rank)
2122 add_generic_class (acfg, klass);
2125 /* Add types of args/locals */
2126 for (i = 0; i < acfg->methods->len; ++i) {
2129 method = g_ptr_array_index (acfg->methods, i);
2131 sig = mono_method_signature (method);
2134 for (j = 0; j < sig->param_count; ++j)
2135 if (sig->params [j]->type == MONO_TYPE_GENERICINST)
2136 add_generic_class (acfg, mono_class_from_mono_type (sig->params [j]));
2139 header = mono_method_get_header (method);
2142 for (j = 0; j < header->num_locals; ++j)
2143 if (header->locals [j]->type == MONO_TYPE_GENERICINST)
2144 add_generic_class (acfg, mono_class_from_mono_type (header->locals [j]));
2150 * is_direct_callable:
2152 * Return whenever the method identified by JI is directly callable without
2153 * going through the PLT.
2156 is_direct_callable (MonoAotCompile *acfg, MonoMethod *method, MonoJumpInfo *patch_info)
2158 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
2159 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
2161 gboolean direct_callable = TRUE;
2163 if (direct_callable && !(!callee_cfg->has_got_slots && (callee_cfg->method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)))
2164 direct_callable = FALSE;
2165 if ((callee_cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) && (!method || method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED))
2166 // FIXME: Maybe call the wrapper directly ?
2167 direct_callable = FALSE;
2169 if (direct_callable)
2178 * emit_and_reloc_code:
2180 * Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
2181 * is true, calls are made through the GOT too. This is used for emitting trampolines
2182 * in full-aot mode, since calls made from trampolines couldn't go through the PLT,
2183 * since trampolines are needed to make PTL work.
2186 emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, guint32 code_len, MonoJumpInfo *relocs, gboolean got_only)
2188 int i, pindex, start_index, method_index;
2190 MonoJumpInfo *patch_info;
2191 MonoMethodHeader *header;
2192 gboolean skip, direct_call;
2194 char direct_call_target [128];
2197 header = mono_method_get_header (method);
2199 method_index = get_method_index (acfg, method);
2202 /* Collect and sort relocations */
2203 patches = g_ptr_array_new ();
2204 for (patch_info = relocs; patch_info; patch_info = patch_info->next)
2205 g_ptr_array_add (patches, patch_info);
2206 g_ptr_array_sort (patches, compare_patches);
2209 for (i = 0; i < code_len; i++) {
2211 for (pindex = start_index; pindex < patches->len; ++pindex) {
2212 patch_info = g_ptr_array_index (patches, pindex);
2213 if (patch_info->ip.i >= i)
2217 #ifdef MONO_ARCH_AOT_SUPPORTED
2219 if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
2220 start_index = pindex;
2222 switch (patch_info->type) {
2223 case MONO_PATCH_INFO_NONE:
2225 case MONO_PATCH_INFO_GOT_OFFSET: {
2228 arch_emit_got_offset (acfg, code + i, &code_size);
2231 patch_info->type = MONO_PATCH_INFO_NONE;
2236 * If this patch is a call, try emitting a direct call instead of
2237 * through a PLT entry. This is possible if the called method is in
2238 * the same assembly and requires no initialization.
2240 direct_call = FALSE;
2241 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == acfg->image)) {
2242 if (!got_only && is_direct_callable (acfg, method, patch_info)) {
2243 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
2244 //printf ("DIRECT: %s %s\n", method ? mono_method_full_name (method, TRUE) : "", mono_method_full_name (callee_cfg->method, TRUE));
2246 sprintf (direct_call_target, "%sm_%x", acfg->temp_prefix, get_method_index (acfg, callee_cfg->orig_method));
2247 patch_info->type = MONO_PATCH_INFO_NONE;
2248 acfg->stats.direct_calls ++;
2251 acfg->stats.all_calls ++;
2254 if (!got_only && !direct_call) {
2255 int plt_offset = get_plt_offset (acfg, patch_info);
2256 if (plt_offset != -1) {
2257 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
2259 sprintf (direct_call_target, "%sp_%d", acfg->temp_prefix, plt_offset);
2261 /* Nullify the patch */
2262 patch_info->type = MONO_PATCH_INFO_NONE;
2269 arch_emit_direct_call (acfg, direct_call_target, &call_size);
2274 got_slot = get_got_offset (acfg, patch_info);
2276 arch_emit_got_access (acfg, code + i, got_slot, &code_size);
2283 #endif /* MONO_ARCH_AOT_SUPPORTED */
2286 /* Find next patch */
2288 for (pindex = start_index; pindex < patches->len; ++pindex) {
2289 patch_info = g_ptr_array_index (patches, pindex);
2290 if (patch_info->ip.i >= i)
2294 /* Try to emit multiple bytes at once */
2295 if (pindex < patches->len && patch_info->ip.i > i) {
2296 emit_bytes (acfg, code + i, patch_info->ip.i - i);
2297 i = patch_info->ip.i - 1;
2299 emit_bytes (acfg, code + i, 1);
2308 * Modify SYMBOL so it only includes characters permissible in symbols.
2311 sanitize_symbol (char *symbol)
2313 int i, len = strlen (symbol);
2315 for (i = 0; i < len; ++i)
2316 if (!isalnum (symbol [i]) && (symbol [i] != '_'))
2321 get_debug_sym (MonoMethod *method, const char *prefix, GHashTable *cache)
2323 char *name1, *name2, *cached;
2324 int i, j, len, count;
2326 name1 = mono_method_full_name (method, TRUE);
2327 len = strlen (name1);
2328 name2 = malloc (strlen (prefix) + len + 16);
2329 memcpy (name2, prefix, strlen (prefix));
2330 j = strlen (prefix);
2331 for (i = 0; i < len; ++i) {
2332 if (isalnum (name1 [i])) {
2333 name2 [j ++] = name1 [i];
2334 } else if (name1 [i] == ' ' && name1 [i + 1] == '(' && name1 [i + 2] == ')') {
2336 } else if (name1 [i] == ',' && name1 [i + 1] == ' ') {
2339 } else if (name1 [i] == '(' || name1 [i] == ')' || name1 [i] == '>') {
2348 while (g_hash_table_lookup (cache, name2)) {
2349 sprintf (name2 + j, "_%d", count);
2353 cached = g_strdup (name2);
2354 g_hash_table_insert (cache, cached, cached);
2360 emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
2365 char *debug_sym = NULL;
2367 int func_alignment = AOT_FUNC_ALIGNMENT;
2368 MonoMethodHeader *header;
2370 method = cfg->orig_method;
2371 code = cfg->native_code;
2372 header = mono_method_get_header (method);
2374 method_index = get_method_index (acfg, method);
2376 /* Emit unbox trampoline */
2377 if (acfg->aot_opts.full_aot && cfg->orig_method->klass->valuetype && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
2378 char call_target [256];
2380 if (!method->wrapper_type && !method->is_inflated) {
2381 g_assert (method->token);
2382 sprintf (symbol, "ut_%d", mono_metadata_token_index (method->token) - 1);
2384 sprintf (symbol, "ut_e_%d", get_method_index (acfg, method));
2387 emit_section_change (acfg, ".text", 0);
2388 emit_global (acfg, symbol, TRUE);
2389 emit_label (acfg, symbol);
2391 sprintf (call_target, "%sm_%x", acfg->temp_prefix, method_index);
2393 arch_emit_unbox_trampoline (acfg, cfg->orig_method, cfg->generic_sharing_context, call_target);
2396 /* Make the labels local */
2397 sprintf (symbol, "%sm_%x", acfg->temp_prefix, method_index);
2399 emit_alignment (acfg, func_alignment);
2400 emit_label (acfg, symbol);
2402 if (acfg->aot_opts.write_symbols) {
2404 * Write a C style symbol for every method, this has two uses:
2405 * - it works on platforms where the dwarf debugging info is not
2407 * - it allows the setting of breakpoints of aot-ed methods.
2409 debug_sym = get_debug_sym (method, "", acfg->method_label_hash);
2411 sprintf (symbol, "%sme_%x", acfg->temp_prefix, method_index);
2412 emit_local_symbol (acfg, debug_sym, symbol, TRUE);
2413 emit_label (acfg, debug_sym);
2416 if (cfg->verbose_level > 0)
2417 g_print ("Method %s emitted as %s\n", mono_method_full_name (method, TRUE), symbol);
2419 acfg->stats.code_size += cfg->code_len;
2421 acfg->cfgs [method_index]->got_offset = acfg->got_offset;
2423 emit_and_reloc_code (acfg, method, code, cfg->code_len, cfg->patch_info, FALSE);
2427 if (acfg->aot_opts.write_symbols) {
2428 emit_symbol_size (acfg, debug_sym, ".");
2432 sprintf (symbol, "%sme_%x", acfg->temp_prefix, method_index);
2433 emit_label (acfg, symbol);
2439 * Encode PATCH_INFO into its disk representation.
2442 encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf)
2446 switch (patch_info->type) {
2447 case MONO_PATCH_INFO_NONE:
2449 case MONO_PATCH_INFO_IMAGE:
2450 encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
2452 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
2454 case MONO_PATCH_INFO_METHOD_REL:
2455 encode_value ((gint)patch_info->data.offset, p, &p);
2457 case MONO_PATCH_INFO_SWITCH: {
2458 gpointer *table = (gpointer *)patch_info->data.table->table;
2461 encode_value (patch_info->data.table->table_size, p, &p);
2462 for (k = 0; k < patch_info->data.table->table_size; k++)
2463 encode_value ((int)(gssize)table [k], p, &p);
2466 case MONO_PATCH_INFO_METHODCONST:
2467 case MONO_PATCH_INFO_METHOD:
2468 case MONO_PATCH_INFO_METHOD_JUMP:
2469 case MONO_PATCH_INFO_ICALL_ADDR:
2470 case MONO_PATCH_INFO_METHOD_RGCTX:
2471 encode_method_ref (acfg, patch_info->data.method, p, &p);
2473 case MONO_PATCH_INFO_INTERNAL_METHOD:
2474 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2475 guint32 len = strlen (patch_info->data.name);
2477 encode_value (len, p, &p);
2479 memcpy (p, patch_info->data.name, len);
2484 case MONO_PATCH_INFO_LDSTR: {
2485 guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
2486 guint32 token = patch_info->data.token->token;
2487 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
2488 encode_value (image_index, p, &p);
2489 encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
2492 case MONO_PATCH_INFO_RVA:
2493 case MONO_PATCH_INFO_DECLSEC:
2494 case MONO_PATCH_INFO_LDTOKEN:
2495 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2496 encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
2497 encode_value (patch_info->data.token->token, p, &p);
2498 encode_value (patch_info->data.token->has_context, p, &p);
2499 if (patch_info->data.token->has_context)
2500 encode_generic_context (acfg, &patch_info->data.token->context, p, &p);
2502 case MONO_PATCH_INFO_EXC_NAME: {
2503 MonoClass *ex_class;
2506 mono_class_from_name (mono_defaults.exception_class->image,
2507 "System", patch_info->data.target);
2508 g_assert (ex_class);
2509 encode_klass_ref (acfg, ex_class, p, &p);
2512 case MONO_PATCH_INFO_R4:
2513 encode_value (*((guint32 *)patch_info->data.target), p, &p);
2515 case MONO_PATCH_INFO_R8:
2516 encode_value (*((guint32 *)patch_info->data.target), p, &p);
2517 encode_value (*(((guint32 *)patch_info->data.target) + 1), p, &p);
2519 case MONO_PATCH_INFO_VTABLE:
2520 case MONO_PATCH_INFO_CLASS:
2521 case MONO_PATCH_INFO_IID:
2522 case MONO_PATCH_INFO_ADJUSTED_IID:
2523 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
2525 case MONO_PATCH_INFO_CLASS_INIT:
2526 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2527 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
2529 case MONO_PATCH_INFO_FIELD:
2530 case MONO_PATCH_INFO_SFLDA:
2531 encode_field_info (acfg, patch_info->data.field, p, &p);
2533 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2535 case MONO_PATCH_INFO_RGCTX_FETCH: {
2536 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
2538 encode_method_ref (acfg, entry->method, p, &p);
2539 encode_value (entry->in_mrgctx, p, &p);
2540 encode_value (entry->info_type, p, &p);
2541 encode_value (entry->data->type, p, &p);
2542 encode_patch (acfg, entry->data, p, &p);
2545 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2546 case MONO_PATCH_INFO_MONITOR_ENTER:
2547 case MONO_PATCH_INFO_MONITOR_EXIT:
2550 g_warning ("unable to handle jump info %d", patch_info->type);
2551 g_assert_not_reached ();
2558 encode_patch_list (MonoAotCompile *acfg, GPtrArray *patches, int n_patches, int first_got_offset, guint8 *buf, guint8 **endbuf)
2561 guint32 pindex, offset;
2562 MonoJumpInfo *patch_info;
2564 encode_value (n_patches, p, &p);
2566 for (pindex = 0; pindex < patches->len; ++pindex) {
2567 patch_info = g_ptr_array_index (patches, pindex);
2569 if (patch_info->type == MONO_PATCH_INFO_NONE)
2573 offset = get_got_offset (acfg, patch_info);
2574 encode_value (offset, p, &p);
2581 emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
2585 int pindex, buf_size, n_patches;
2589 MonoJumpInfo *patch_info;
2590 MonoMethodHeader *header;
2591 guint32 method_index;
2593 guint32 first_got_offset;
2595 method = cfg->orig_method;
2596 code = cfg->native_code;
2597 header = mono_method_get_header (method);
2599 method_index = get_method_index (acfg, method);
2601 /* Make the labels local */
2602 sprintf (symbol, "%sm_%x_p", acfg->temp_prefix, method_index);
2604 /* Sort relocations */
2605 patches = g_ptr_array_new ();
2606 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
2607 g_ptr_array_add (patches, patch_info);
2608 g_ptr_array_sort (patches, compare_patches);
2610 first_got_offset = acfg->cfgs [method_index]->got_offset;
2612 /**********************/
2613 /* Encode method info */
2614 /**********************/
2616 buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
2617 p = buf = g_malloc (buf_size);
2619 if (mono_class_get_cctor (method->klass))
2620 encode_klass_ref (acfg, method->klass, p, &p);
2622 /* Not needed when loading the method */
2623 encode_value (0, p, &p);
2626 if (cfg->opt & MONO_OPT_SHARED) {
2627 encode_value (g_list_length (cfg->ldstr_list), p, &p);
2628 for (l = cfg->ldstr_list; l; l = l->next) {
2629 encode_value ((long)l->data, p, &p);
2633 /* Used only in shared mode */
2634 g_assert (!cfg->ldstr_list);
2637 for (pindex = 0; pindex < patches->len; ++pindex) {
2638 patch_info = g_ptr_array_index (patches, pindex);
2640 if ((patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
2641 (patch_info->type == MONO_PATCH_INFO_NONE)) {
2642 patch_info->type = MONO_PATCH_INFO_NONE;
2647 if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
2648 /* Stored in a GOT slot initialized at module load time */
2649 patch_info->type = MONO_PATCH_INFO_NONE;
2653 if (is_plt_patch (patch_info)) {
2654 /* Calls are made through the PLT */
2655 patch_info->type = MONO_PATCH_INFO_NONE;
2663 g_assert (cfg->has_got_slots);
2665 encode_patch_list (acfg, patches, n_patches, first_got_offset, p, &p);
2667 acfg->stats.info_size += p - buf;
2669 /* Emit method info */
2671 emit_label (acfg, symbol);
2673 g_assert (p - buf < buf_size);
2674 emit_bytes (acfg, buf, p - buf);
2679 get_unwind_info_offset (MonoAotCompile *acfg, guint8 *encoded, guint32 encoded_len)
2681 guint32 cache_index;
2684 /* Reuse the unwind module to canonize and store unwind info entries */
2685 cache_index = mono_cache_unwind_info (encoded, encoded_len);
2687 /* Use +/- 1 to distinguish 0s from missing entries */
2688 offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1)));
2696 * It would be easier to use assembler symbols, but the caller needs an
2699 offset = acfg->unwind_info_offset;
2700 g_hash_table_insert (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1), GUINT_TO_POINTER (offset + 1));
2701 g_ptr_array_add (acfg->unwind_ops, GUINT_TO_POINTER (cache_index));
2704 encode_value (encoded_len, p, &p);
2706 acfg->unwind_info_offset += encoded_len + (p - buf);
2712 emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
2715 int k, buf_size, method_index;
2716 guint32 debug_info_size;
2719 MonoMethodHeader *header;
2720 guint8 *p, *buf, *debug_info;
2721 MonoJitInfo *jinfo = cfg->jit_info;
2723 gboolean use_unwind_ops = FALSE;
2725 method = cfg->orig_method;
2726 code = cfg->native_code;
2727 header = mono_method_get_header (method);
2729 method_index = get_method_index (acfg, method);
2731 /* Make the labels local */
2732 sprintf (symbol, "%se_%x_p", acfg->temp_prefix, method_index);
2734 if (!acfg->aot_opts.nodebug) {
2735 mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
2738 debug_info_size = 0;
2741 buf_size = header->num_clauses * 256 + debug_info_size + 1024;
2742 p = buf = g_malloc (buf_size);
2744 #ifdef MONO_ARCH_HAVE_XP_UNWIND
2745 use_unwind_ops = cfg->unwind_ops != NULL;
2748 flags = (jinfo->has_generic_jit_info ? 1 : 0) | (use_unwind_ops ? 2 : 0);
2750 encode_value (jinfo->code_size, p, &p);
2751 encode_value (flags, p, &p);
2753 if (use_unwind_ops) {
2754 guint32 encoded_len;
2758 * This is a duplicate of the data in the .debug_frame section, but that
2759 * section cannot be accessed using the dl interface.
2761 encoded = mono_unwind_ops_encode (cfg->unwind_ops, &encoded_len);
2762 encode_value (get_unwind_info_offset (acfg, encoded, encoded_len), p, &p);
2765 encode_value (jinfo->used_regs, p, &p);
2768 /* Exception table */
2769 if (header->num_clauses) {
2770 for (k = 0; k < header->num_clauses; ++k) {
2771 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
2773 encode_value (ei->exvar_offset, p, &p);
2775 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
2776 encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
2778 encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
2779 encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
2780 encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
2784 if (jinfo->has_generic_jit_info) {
2785 MonoGenericJitInfo *gi = mono_jit_info_get_generic_jit_info (jinfo);
2787 encode_value (gi->has_this ? 1 : 0, p, &p);
2788 encode_value (gi->this_reg, p, &p);
2789 encode_value (gi->this_offset, p, &p);
2792 * Need to encode jinfo->method too, since it is not equal to 'method'
2793 * when using generic sharing.
2795 encode_method_ref (acfg, jinfo->method, p, &p);
2798 g_assert (debug_info_size < buf_size);
2800 encode_value (debug_info_size, p, &p);
2801 if (debug_info_size) {
2802 memcpy (p, debug_info, debug_info_size);
2803 p += debug_info_size;
2804 g_free (debug_info);
2807 acfg->stats.ex_info_size += p - buf;
2811 emit_label (acfg, symbol);
2813 g_assert (p - buf < buf_size);
2814 emit_bytes (acfg, buf, p - buf);
2819 emit_klass_info (MonoAotCompile *acfg, guint32 token)
2821 MonoClass *klass = mono_class_get (acfg->image, token);
2825 gboolean no_special_static, cant_encode;
2826 gpointer iter = NULL;
2828 buf_size = 10240 + (klass->vtable_size * 16);
2829 p = buf = g_malloc (buf_size);
2833 mono_class_init (klass);
2835 mono_class_get_nested_types (klass, &iter);
2836 g_assert (klass->nested_classes_inited);
2838 mono_class_setup_vtable (klass);
2841 * Emit all the information which is required for creating vtables so
2842 * the runtime does not need to create the MonoMethod structures which
2843 * take up a lot of space.
2846 no_special_static = !mono_class_has_special_static_fields (klass);
2848 /* Check whenever we have enough info to encode the vtable */
2849 cant_encode = FALSE;
2850 for (i = 0; i < klass->vtable_size; ++i) {
2851 MonoMethod *cm = klass->vtable [i];
2853 if (cm && mono_method_signature (cm)->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, cm))
2857 if (klass->generic_container || cant_encode) {
2858 encode_value (-1, p, &p);
2860 encode_value (klass->vtable_size, p, &p);
2861 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);
2862 if (klass->has_cctor)
2863 encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
2864 if (klass->has_finalize)
2865 encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
2867 encode_value (klass->instance_size, p, &p);
2868 encode_value (mono_class_data_size (klass), p, &p);
2869 encode_value (klass->packing_size, p, &p);
2870 encode_value (klass->min_align, p, &p);
2872 for (i = 0; i < klass->vtable_size; ++i) {
2873 MonoMethod *cm = klass->vtable [i];
2876 encode_method_ref (acfg, cm, p, &p);
2878 encode_value (0, p, &p);
2882 acfg->stats.class_info_size += p - buf;
2885 sprintf (symbol, "%sK_I_%x", acfg->temp_prefix, token - MONO_TOKEN_TYPE_DEF - 1);
2886 emit_label (acfg, symbol);
2888 g_assert (p - buf < buf_size);
2889 emit_bytes (acfg, buf, p - buf);
2894 * Calls made from AOTed code are routed through a table of jumps similar to the
2895 * ELF PLT (Program Linkage Table). The differences are the following:
2896 * - the ELF PLT entries make an indirect jump though the GOT so they expect the
2897 * GOT pointer to be in EBX. We want to avoid this, so our table contains direct
2898 * jumps. This means the jumps need to be patched when the address of the callee is
2899 * known. Initially the PLT entries jump to code which transfers control to the
2900 * AOT runtime through the first PLT entry.
2903 emit_plt (MonoAotCompile *acfg)
2909 cache = g_hash_table_new (g_str_hash, g_str_equal);
2912 sprintf (symbol, "plt");
2914 emit_section_change (acfg, ".text", 0);
2915 emit_global (acfg, symbol, TRUE);
2917 /* This section will be made read-write by the AOT loader */
2918 emit_alignment (acfg, mono_pagesize ());
2920 emit_alignment (acfg, 16);
2922 emit_label (acfg, symbol);
2923 emit_label (acfg, acfg->plt_symbol);
2925 for (i = 0; i < acfg->plt_offset; ++i) {
2927 char *debug_sym = NULL;
2929 sprintf (label, "%sp_%d", acfg->temp_prefix, i);
2930 emit_label (acfg, label);
2932 if (acfg->aot_opts.write_symbols) {
2933 MonoJumpInfo *ji = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
2934 char *debug_sym = NULL;
2938 case MONO_PATCH_INFO_METHOD:
2939 debug_sym = get_debug_sym (ji->data.method, "plt_", cache);
2941 case MONO_PATCH_INFO_INTERNAL_METHOD:
2942 debug_sym = g_strdup_printf ("plt__jit_icall_%s", ji->data.name);
2944 case MONO_PATCH_INFO_CLASS_INIT:
2945 debug_sym = g_strdup_printf ("plt__class_init_%s", mono_type_get_name (&ji->data.klass->byval_arg));
2946 sanitize_symbol (debug_sym);
2948 case MONO_PATCH_INFO_RGCTX_FETCH:
2949 debug_sym = g_strdup_printf ("plt__rgctx_fetch_%d", acfg->label_generator ++);
2951 case MONO_PATCH_INFO_ICALL_ADDR: {
2952 char *s = get_debug_sym (ji->data.method, "", cache);
2954 debug_sym = g_strdup_printf ("plt__icall_native_%s", s);
2958 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
2959 debug_sym = g_strdup_printf ("plt__jit_icall_native_%s", ji->data.name);
2961 case MONO_PATCH_INFO_GENERIC_CLASS_INIT:
2962 debug_sym = g_strdup_printf ("plt__generic_class_init");
2969 emit_local_symbol (acfg, debug_sym, NULL, TRUE);
2970 emit_label (acfg, debug_sym);
2976 * The first plt entry is used to transfer code to the AOT loader.
2978 arch_emit_plt_entry (acfg, i);
2981 emit_symbol_size (acfg, debug_sym, ".");
2986 emit_symbol_size (acfg, acfg->plt_symbol, ".");
2988 sprintf (symbol, "plt_end");
2989 emit_global (acfg, symbol, TRUE);
2990 emit_label (acfg, symbol);
2992 g_hash_table_destroy (cache);
2995 static G_GNUC_UNUSED void
2996 emit_trampoline (MonoAotCompile *acfg, const char *name, guint8 *code,
2997 guint32 code_size, int got_offset, MonoJumpInfo *ji, GSList *unwind_ops)
2999 char start_symbol [256];
3002 MonoJumpInfo *patch_info;
3008 sprintf (start_symbol, "%s", name);
3010 emit_section_change (acfg, ".text", 0);
3011 emit_global (acfg, start_symbol, TRUE);
3012 emit_alignment (acfg, 16);
3013 emit_label (acfg, start_symbol);
3015 sprintf (symbol, "%snamed_%s", acfg->temp_prefix, name);
3016 emit_label (acfg, symbol);
3019 * The code should access everything through the GOT, so we pass
3022 emit_and_reloc_code (acfg, NULL, code, code_size, ji, TRUE);
3024 emit_symbol_size (acfg, start_symbol, ".");
3028 /* Sort relocations */
3029 patches = g_ptr_array_new ();
3030 for (patch_info = ji; patch_info; patch_info = patch_info->next)
3031 if (patch_info->type != MONO_PATCH_INFO_NONE)
3032 g_ptr_array_add (patches, patch_info);
3033 g_ptr_array_sort (patches, compare_patches);
3035 buf_size = patches->len * 128 + 128;
3036 buf = g_malloc (buf_size);
3039 encode_patch_list (acfg, patches, patches->len, got_offset, p, &p);
3040 g_assert (p - buf < buf_size);
3042 sprintf (symbol, "%s_p", name);
3044 emit_section_change (acfg, ".text", 0);
3045 emit_global (acfg, symbol, FALSE);
3046 emit_label (acfg, symbol);
3048 emit_bytes (acfg, buf, p - buf);
3050 /* Emit debug info */
3054 sprintf (symbol, "%s", name);
3055 sprintf (symbol2, "%snamed_%s", acfg->temp_prefix, name);
3058 mono_dwarf_writer_emit_trampoline (acfg->dwarf, symbol, symbol2, NULL, NULL, code_size, unwind_ops);
3063 emit_trampolines (MonoAotCompile *acfg)
3066 int i, tramp_got_offset;
3067 MonoAotTrampoline ntype;
3068 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
3076 if (!acfg->aot_opts.full_aot)
3079 g_assert (acfg->image->assembly);
3081 /* Currently, we only emit most trampolines into the mscorlib AOT image. */
3082 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
3083 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
3085 * Emit the generic trampolines.
3087 * We could save some code by treating the generic trampolines as a wrapper
3088 * method, but that approach has its own complexities, so we choose the simpler
3091 for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
3092 code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, &unwind_ops, TRUE);
3094 /* Emit trampoline code */
3096 sprintf (symbol, "generic_trampoline_%d", tramp_type);
3098 emit_trampoline (acfg, symbol, code, code_size, acfg->got_offset, ji, unwind_ops);
3101 code = mono_arch_get_nullified_class_init_trampoline (&code_size);
3102 emit_trampoline (acfg, "nullified_class_init_trampoline", code, code_size, acfg->got_offset, NULL, NULL);
3103 #if defined(TARGET_AMD64) && defined(MONO_ARCH_MONITOR_OBJECT_REG)
3104 code = mono_arch_create_monitor_enter_trampoline_full (&code_size, &ji, TRUE);
3105 emit_trampoline (acfg, "monitor_enter_trampoline", code, code_size, acfg->got_offset, ji, NULL);
3106 code = mono_arch_create_monitor_exit_trampoline_full (&code_size, &ji, TRUE);
3107 emit_trampoline (acfg, "monitor_exit_trampoline", code, code_size, acfg->got_offset, ji, NULL);
3110 code = mono_arch_create_generic_class_init_trampoline_full (&code_size, &ji, TRUE);
3111 emit_trampoline (acfg, "generic_class_init_trampoline", code, code_size, acfg->got_offset, ji, NULL);
3113 /* Emit the exception related code pieces */
3114 code = mono_arch_get_restore_context_full (&code_size, &ji, TRUE);
3115 emit_trampoline (acfg, "restore_context", code, code_size, acfg->got_offset, ji, NULL);
3116 code = mono_arch_get_call_filter_full (&code_size, &ji, TRUE);
3117 emit_trampoline (acfg, "call_filter", code, code_size, acfg->got_offset, ji, NULL);
3118 code = mono_arch_get_throw_exception_full (&code_size, &ji, TRUE);
3119 emit_trampoline (acfg, "throw_exception", code, code_size, acfg->got_offset, ji, NULL);
3120 code = mono_arch_get_rethrow_exception_full (&code_size, &ji, TRUE);
3121 emit_trampoline (acfg, "rethrow_exception", code, code_size, acfg->got_offset, ji, NULL);
3122 code = mono_arch_get_throw_exception_by_name_full (&code_size, &ji, TRUE);
3123 emit_trampoline (acfg, "throw_exception_by_name", code, code_size, acfg->got_offset, ji, NULL);
3124 code = mono_arch_get_throw_corlib_exception_full (&code_size, &ji, TRUE);
3125 emit_trampoline (acfg, "throw_corlib_exception", code, code_size, acfg->got_offset, ji, NULL);
3127 #if defined(TARGET_AMD64)
3128 code = mono_arch_get_throw_pending_exception_full (&code_size, &ji, TRUE);
3129 emit_trampoline (acfg, "throw_pending_exception", code, code_size, acfg->got_offset, ji, NULL);
3132 for (i = 0; i < 128; ++i) {
3135 offset = MONO_RGCTX_SLOT_MAKE_RGCTX (i);
3136 code = mono_arch_create_rgctx_lazy_fetch_trampoline_full (offset, &code_size, &ji, TRUE);
3137 sprintf (symbol, "rgctx_fetch_trampoline_%u", offset);
3138 emit_trampoline (acfg, symbol, code, code_size, acfg->got_offset, ji, NULL);
3140 offset = MONO_RGCTX_SLOT_MAKE_MRGCTX (i);
3141 code = mono_arch_create_rgctx_lazy_fetch_trampoline_full (offset, &code_size, &ji, TRUE);
3142 sprintf (symbol, "rgctx_fetch_trampoline_%u", offset);
3143 emit_trampoline (acfg, symbol, code, code_size, acfg->got_offset, ji, NULL);
3149 /* delegate_invoke_impl trampolines */
3150 l = mono_arch_get_delegate_invoke_impls ();
3152 MonoAotTrampInfo *info = l->data;
3154 emit_trampoline (acfg, info->name, info->code, info->code_size, acfg->got_offset, NULL, NULL);
3159 #endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
3161 /* Emit trampolines which are numerous */
3164 * These include the following:
3165 * - specific trampolines
3166 * - static rgctx invoke trampolines
3168 * These trampolines have the same code, they are parameterized by GOT
3170 * They are defined in this file, in the arch_... routines instead of
3171 * in tramp-<ARCH>.c, since it is easier to do it this way.
3175 * When running in aot-only mode, we can't create specific trampolines at
3176 * runtime, so we create a few, and save them in the AOT file.
3177 * Normal trampolines embed their argument as a literal inside the
3178 * trampoline code, we can't do that here, so instead we embed an offset
3179 * which needs to be added to the trampoline address to get the address of
3180 * the GOT slot which contains the argument value.
3181 * The generated trampolines jump to the generic trampolines using another
3182 * GOT slot, which will be setup by the AOT loader to point to the
3183 * generic trampoline code of the given type.
3187 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for
3191 emit_section_change (acfg, ".text", 0);
3193 tramp_got_offset = acfg->got_offset;
3195 for (ntype = 0; ntype < MONO_AOT_TRAMP_NUM; ++ntype) {
3197 case MONO_AOT_TRAMP_SPECIFIC:
3198 sprintf (symbol, "specific_trampolines");
3200 case MONO_AOT_TRAMP_STATIC_RGCTX:
3201 sprintf (symbol, "static_rgctx_trampolines");
3203 case MONO_AOT_TRAMP_IMT_THUNK:
3204 sprintf (symbol, "imt_thunks");
3207 g_assert_not_reached ();
3210 emit_global (acfg, symbol, TRUE);
3211 emit_alignment (acfg, 16);
3212 emit_label (acfg, symbol);
3214 acfg->trampoline_got_offset_base [ntype] = tramp_got_offset;
3216 for (i = 0; i < acfg->num_trampolines [ntype]; ++i) {
3220 case MONO_AOT_TRAMP_SPECIFIC:
3221 arch_emit_specific_trampoline (acfg, tramp_got_offset, &tramp_size);
3222 tramp_got_offset += 2;
3224 case MONO_AOT_TRAMP_STATIC_RGCTX:
3225 arch_emit_static_rgctx_trampoline (acfg, tramp_got_offset, &tramp_size);
3226 tramp_got_offset += 2;
3228 case MONO_AOT_TRAMP_IMT_THUNK:
3229 arch_emit_imt_thunk (acfg, tramp_got_offset, &tramp_size);
3230 tramp_got_offset += 1;
3233 g_assert_not_reached ();
3236 if (!acfg->trampoline_size [ntype]) {
3237 g_assert (tramp_size);
3238 acfg->trampoline_size [ntype] = tramp_size;
3243 /* Reserve some entries at the end of the GOT for our use */
3244 acfg->num_trampoline_got_entries = tramp_got_offset - acfg->got_offset;
3247 acfg->got_offset += acfg->num_trampoline_got_entries;
3251 str_begins_with (const char *str1, const char *str2)
3253 int len = strlen (str2);
3254 return strncmp (str1, str2, len) == 0;
3258 mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
3260 gchar **args, **ptr;
3262 args = g_strsplit (aot_options ? aot_options : "", ",", -1);
3263 for (ptr = args; ptr && *ptr; ptr ++) {
3264 const char *arg = *ptr;
3266 if (str_begins_with (arg, "outfile=")) {
3267 opts->outfile = g_strdup (arg + strlen ("outfile="));
3268 } else if (str_begins_with (arg, "save-temps")) {
3269 opts->save_temps = TRUE;
3270 } else if (str_begins_with (arg, "keep-temps")) {
3271 opts->save_temps = TRUE;
3272 } else if (str_begins_with (arg, "write-symbols")) {
3273 opts->write_symbols = TRUE;
3274 } else if (str_begins_with (arg, "metadata-only")) {
3275 opts->metadata_only = TRUE;
3276 } else if (str_begins_with (arg, "bind-to-runtime-version")) {
3277 opts->bind_to_runtime_version = TRUE;
3278 } else if (str_begins_with (arg, "full")) {
3279 opts->full_aot = TRUE;
3280 } else if (str_begins_with (arg, "threads=")) {
3281 opts->nthreads = atoi (arg + strlen ("threads="));
3282 } else if (str_begins_with (arg, "static")) {
3283 opts->static_link = TRUE;
3284 opts->no_dlsym = TRUE;
3285 } else if (str_begins_with (arg, "asmonly")) {
3286 opts->asm_only = TRUE;
3287 } else if (str_begins_with (arg, "asmwriter")) {
3288 opts->asm_writer = TRUE;
3289 } else if (str_begins_with (arg, "nodebug")) {
3290 opts->nodebug = TRUE;
3291 } else if (str_begins_with (arg, "ntrampolines=")) {
3292 opts->ntrampolines = atoi (arg + strlen ("ntrampolines="));
3294 fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
3303 add_token_info_hash (gpointer key, gpointer value, gpointer user_data)
3305 MonoMethod *method = (MonoMethod*)key;
3306 MonoJumpInfoToken *ji = (MonoJumpInfoToken*)value;
3307 MonoJumpInfoToken *new_ji = g_new0 (MonoJumpInfoToken, 1);
3308 MonoAotCompile *acfg = user_data;
3310 new_ji->image = ji->image;
3311 new_ji->token = ji->token;
3312 g_hash_table_insert (acfg->token_info_hash, method, new_ji);
3316 can_encode_class (MonoAotCompile *acfg, MonoClass *klass)
3318 if (klass->type_token)
3320 if ((klass->byval_arg.type == MONO_TYPE_VAR) || (klass->byval_arg.type == MONO_TYPE_MVAR))
3323 return can_encode_class (acfg, klass->element_class);
3328 can_encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
3330 switch (patch_info->type) {
3331 case MONO_PATCH_INFO_METHOD:
3332 case MONO_PATCH_INFO_METHODCONST: {
3333 MonoMethod *method = patch_info->data.method;
3335 if (method->wrapper_type) {
3336 switch (method->wrapper_type) {
3337 case MONO_WRAPPER_NONE:
3338 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
3339 case MONO_WRAPPER_XDOMAIN_INVOKE:
3340 case MONO_WRAPPER_STFLD:
3341 case MONO_WRAPPER_LDFLD:
3342 case MONO_WRAPPER_LDFLDA:
3343 case MONO_WRAPPER_LDFLD_REMOTE:
3344 case MONO_WRAPPER_STFLD_REMOTE:
3345 case MONO_WRAPPER_STELEMREF:
3346 case MONO_WRAPPER_ISINST:
3347 case MONO_WRAPPER_PROXY_ISINST:
3348 case MONO_WRAPPER_ALLOC:
3349 case MONO_WRAPPER_REMOTING_INVOKE:
3350 case MONO_WRAPPER_UNKNOWN:
3353 //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
3357 if (!method->token) {
3358 /* The method is part of a constructed type like Int[,].Set (). */
3359 if (!g_hash_table_lookup (acfg->token_info_hash, method)) {
3360 if (method->klass->rank)
3368 case MONO_PATCH_INFO_VTABLE:
3369 case MONO_PATCH_INFO_CLASS_INIT:
3370 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
3371 case MONO_PATCH_INFO_CLASS:
3372 case MONO_PATCH_INFO_IID:
3373 case MONO_PATCH_INFO_ADJUSTED_IID:
3374 if (!can_encode_class (acfg, patch_info->data.klass)) {
3375 //printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
3379 case MONO_PATCH_INFO_RGCTX_FETCH: {
3380 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
3382 if (!can_encode_patch (acfg, entry->data))
3394 add_generic_class (MonoAotCompile *acfg, MonoClass *klass);
3399 * AOT compile a given method.
3400 * This function might be called by multiple threads, so it must be thread-safe.
3403 compile_method (MonoAotCompile *acfg, MonoMethod *method)
3406 MonoJumpInfo *patch_info;
3409 MonoMethod *wrapped;
3411 if (acfg->aot_opts.metadata_only)
3414 mono_acfg_lock (acfg);
3415 index = get_method_index (acfg, method);
3416 mono_acfg_unlock (acfg);
3418 /* fixme: maybe we can also precompile wrapper methods */
3419 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3420 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3421 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
3422 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
3426 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
3429 wrapped = mono_marshal_method_from_wrapper (method);
3430 if (wrapped && (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && wrapped->is_generic)
3431 // FIXME: The wrapper should be generic too, but it is not
3434 InterlockedIncrement (&acfg->stats.mcount);
3437 if (method->is_generic || method->klass->generic_container) {
3438 InterlockedIncrement (&acfg->stats.genericcount);
3443 //acfg->aot_opts.print_skipped_methods = TRUE;
3446 * Since these methods are the only ones which are compiled with
3447 * AOT support, and they are not used by runtime startup/shutdown code,
3448 * the runtime will not see AOT methods during AOT compilation,so it
3449 * does not need to support them by creating a fake GOT etc.
3451 cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), FALSE, TRUE, 0);
3452 if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
3453 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
3454 InterlockedIncrement (&acfg->stats.genericcount);
3457 if (cfg->exception_type != MONO_EXCEPTION_NONE) {
3458 /* Let the exception happen at runtime */
3462 if (cfg->disable_aot) {
3463 if (acfg->aot_opts.print_skipped_methods)
3464 printf ("Skip (disabled): %s\n", mono_method_full_name (method, TRUE));
3465 InterlockedIncrement (&acfg->stats.ocount);
3466 mono_destroy_compile (cfg);
3470 /* Nullify patches which need no aot processing */
3471 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3472 switch (patch_info->type) {
3473 case MONO_PATCH_INFO_LABEL:
3474 case MONO_PATCH_INFO_BB:
3475 patch_info->type = MONO_PATCH_INFO_NONE;
3482 /* Collect method->token associations from the cfg */
3483 mono_acfg_lock (acfg);
3484 g_hash_table_foreach (cfg->token_info_hash, add_token_info_hash, acfg);
3485 mono_acfg_unlock (acfg);
3488 * Check for absolute addresses.
3491 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3492 switch (patch_info->type) {
3493 case MONO_PATCH_INFO_ABS:
3494 /* unable to handle this */
3503 if (acfg->aot_opts.print_skipped_methods)
3504 printf ("Skip (abs call): %s\n", mono_method_full_name (method, TRUE));
3505 InterlockedIncrement (&acfg->stats.abscount);
3506 mono_destroy_compile (cfg);
3510 /* Lock for the rest of the code */
3511 mono_acfg_lock (acfg);
3514 * Check for methods/klasses we can't encode.
3517 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3518 if (!can_encode_patch (acfg, patch_info))
3523 if (acfg->aot_opts.print_skipped_methods)
3524 printf ("Skip (patches): %s\n", mono_method_full_name (method, TRUE));
3525 acfg->stats.ocount++;
3526 mono_destroy_compile (cfg);
3527 mono_acfg_unlock (acfg);
3531 /* Adds generic instances referenced by this method */
3532 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3533 switch (patch_info->type) {
3534 case MONO_PATCH_INFO_METHOD: {
3535 MonoMethod *m = patch_info->data.method;
3536 if (m->is_inflated) {
3537 if (!(mono_class_generic_sharing_enabled (m->klass) &&
3538 mono_method_is_generic_sharable_impl (m, FALSE)) &&
3539 !method_has_type_vars (m)) {
3540 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
3541 if (acfg->aot_opts.full_aot)
3542 add_extra_method (acfg, mono_marshal_get_native_wrapper (m, TRUE, TRUE));
3544 add_extra_method (acfg, m);
3547 add_generic_class (acfg, m->klass);
3556 /* Determine whenever the method has GOT slots */
3557 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3558 switch (patch_info->type) {
3559 case MONO_PATCH_INFO_GOT_OFFSET:
3560 case MONO_PATCH_INFO_NONE:
3562 case MONO_PATCH_INFO_IMAGE:
3563 /* The assembly is stored in GOT slot 0 */
3564 if (patch_info->data.image != acfg->image)
3565 cfg->has_got_slots = TRUE;
3568 if (!is_plt_patch (patch_info))
3569 cfg->has_got_slots = TRUE;
3574 if (!cfg->has_got_slots)
3575 InterlockedIncrement (&acfg->stats.methods_without_got_slots);
3577 /* Make a copy of the patch info which is in the mempool */
3579 MonoJumpInfo *patches = NULL, *patches_end = NULL;
3581 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3582 MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
3585 patches = new_patch_info;
3587 patches_end->next = new_patch_info;
3588 patches_end = new_patch_info;
3590 cfg->patch_info = patches;
3592 /* Make a copy of the unwind info */
3594 GSList *l, *unwind_ops;
3598 for (l = cfg->unwind_ops; l; l = l->next) {
3599 op = mono_mempool_alloc (acfg->mempool, sizeof (MonoUnwindOp));
3600 memcpy (op, l->data, sizeof (MonoUnwindOp));
3601 unwind_ops = g_slist_prepend_mempool (acfg->mempool, unwind_ops, op);
3603 cfg->unwind_ops = g_slist_reverse (unwind_ops);
3605 /* Make a copy of the argument/local info */
3607 MonoInst **args, **locals;
3608 MonoMethodSignature *sig;
3609 MonoMethodHeader *header;
3612 sig = mono_method_signature (method);
3613 args = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * (sig->param_count + sig->hasthis));
3614 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
3615 args [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
3616 memcpy (args [i], cfg->args [i], sizeof (MonoInst));
3620 header = mono_method_get_header (method);
3621 locals = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * header->num_locals);
3622 for (i = 0; i < header->num_locals; ++i) {
3623 locals [i] = mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
3624 memcpy (locals [i], cfg->locals [i], sizeof (MonoInst));
3626 cfg->locals = locals;
3629 /* Free some fields used by cfg to conserve memory */
3630 mono_mempool_destroy (cfg->mempool);
3631 cfg->mempool = NULL;
3632 g_free (cfg->varinfo);
3633 cfg->varinfo = NULL;
3637 mono_regstate_free (cfg->rs);
3641 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
3643 while (index >= acfg->cfgs_size) {
3644 MonoCompile **new_cfgs;
3647 new_size = acfg->cfgs_size * 2;
3648 new_cfgs = g_new0 (MonoCompile*, new_size);
3649 memcpy (new_cfgs, acfg->cfgs, sizeof (MonoCompile*) * acfg->cfgs_size);
3650 g_free (acfg->cfgs);
3651 acfg->cfgs = new_cfgs;
3652 acfg->cfgs_size = new_size;
3654 acfg->cfgs [index] = cfg;
3656 g_hash_table_insert (acfg->method_to_cfg, cfg->orig_method, cfg);
3659 if (cfg->orig_method->wrapper_type)
3660 g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
3663 mono_acfg_unlock (acfg);
3665 InterlockedIncrement (&acfg->stats.ccount);
3669 compile_thread_main (gpointer *user_data)
3671 MonoDomain *domain = user_data [0];
3672 MonoAotCompile *acfg = user_data [1];
3673 GPtrArray *methods = user_data [2];
3676 mono_thread_attach (domain);
3678 for (i = 0; i < methods->len; ++i)
3679 compile_method (acfg, g_ptr_array_index (methods, i));
3683 load_profile_files (MonoAotCompile *acfg)
3687 int file_index, res, method_index, i;
3694 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);
3696 if (!g_file_test (tmp, G_FILE_TEST_IS_REGULAR)) {
3701 infile = fopen (tmp, "r");
3704 printf ("Using profile data file '%s'\n", tmp);
3709 res = fscanf (infile, "%32s\n", ver);
3710 if ((res != 1) || strcmp (ver, "#VER:1") != 0) {
3711 printf ("Profile file has wrong version or invalid.\n");
3717 res = fscanf (infile, "%d\n", &token);
3721 method_index = mono_metadata_token_index (token) - 1;
3723 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (method_index)))
3724 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (method_index));
3729 /* Add missing methods */
3731 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3732 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (i)))
3733 unordered = g_list_prepend (unordered, GUINT_TO_POINTER (i));
3735 unordered = g_list_reverse (unordered);
3736 if (acfg->method_order)
3737 g_list_last (acfg->method_order)->next = unordered;
3739 acfg->method_order = unordered;
3743 emit_code (MonoAotCompile *acfg)
3749 #if defined(TARGET_POWERPC64)
3750 sprintf (symbol, ".Lgot_addr");
3751 emit_section_change (acfg, ".text", 0);
3752 emit_alignment (acfg, 8);
3753 emit_label (acfg, symbol);
3754 emit_pointer (acfg, acfg->got_symbol);
3757 sprintf (symbol, "methods");
3758 emit_section_change (acfg, ".text", 0);
3759 emit_global (acfg, symbol, TRUE);
3760 emit_alignment (acfg, 8);
3761 emit_label (acfg, symbol);
3764 * Emit some padding so the local symbol for the first method doesn't have the
3765 * same address as 'methods'.
3767 emit_zero_bytes (acfg, 16);
3769 for (l = acfg->method_order; l != NULL; l = l->next) {
3770 i = GPOINTER_TO_UINT (l->data);
3773 emit_method_code (acfg, acfg->cfgs [i]);
3776 sprintf (symbol, "methods_end");
3777 emit_section_change (acfg, ".text", 0);
3778 emit_global (acfg, symbol, FALSE);
3779 emit_alignment (acfg, 8);
3780 emit_label (acfg, symbol);
3782 sprintf (symbol, "method_offsets");
3783 emit_section_change (acfg, ".text", 1);
3784 emit_global (acfg, symbol, FALSE);
3785 emit_alignment (acfg, 8);
3786 emit_label (acfg, symbol);
3788 for (i = 0; i < acfg->nmethods; ++i) {
3789 if (acfg->cfgs [i]) {
3790 sprintf (symbol, "%sm_%x", acfg->temp_prefix, i);
3791 emit_symbol_diff (acfg, symbol, "methods", 0);
3793 emit_int32 (acfg, 0xffffffff);
3800 emit_info (MonoAotCompile *acfg)
3806 /* Emit method info */
3807 sprintf (symbol, "method_info");
3808 emit_section_change (acfg, ".text", 1);
3809 emit_global (acfg, symbol, FALSE);
3810 emit_alignment (acfg, 8);
3811 emit_label (acfg, symbol);
3813 /* To reduce size of generated assembly code */
3814 sprintf (symbol, "mi");
3815 emit_label (acfg, symbol);
3817 for (l = acfg->method_order; l != NULL; l = l->next) {
3818 i = GPOINTER_TO_UINT (l->data);
3821 emit_method_info (acfg, acfg->cfgs [i]);
3824 sprintf (symbol, "method_info_offsets");
3825 emit_section_change (acfg, ".text", 1);
3826 emit_global (acfg, symbol, FALSE);
3827 emit_alignment (acfg, 8);
3828 emit_label (acfg, symbol);
3830 for (i = 0; i < acfg->nmethods; ++i) {
3831 if (acfg->cfgs [i]) {
3832 sprintf (symbol, "%sm_%x_p", acfg->temp_prefix, i);
3833 emit_symbol_diff (acfg, symbol, "mi", 0);
3835 emit_int32 (acfg, 0);
3841 #endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
3844 * mono_aot_str_hash:
3846 * Hash function for strings which we use to hash strings for things which are
3847 * saved in the AOT image, since g_str_hash () can change.
3850 mono_aot_str_hash (gconstpointer v1)
3852 /* Same as g_str_hash () in glib */
3853 char *p = (char *) v1;
3858 hash = (hash << 5) - hash + *p;
3864 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
3865 #define mix(a,b,c) { \
3866 a -= c; a ^= rot(c, 4); c += b; \
3867 b -= a; b ^= rot(a, 6); a += c; \
3868 c -= b; c ^= rot(b, 8); b += a; \
3869 a -= c; a ^= rot(c,16); c += b; \
3870 b -= a; b ^= rot(a,19); a += c; \
3871 c -= b; c ^= rot(b, 4); b += a; \
3873 #define final(a,b,c) { \
3874 c ^= b; c -= rot(b,14); \
3875 a ^= c; a -= rot(c,11); \
3876 b ^= a; b -= rot(a,25); \
3877 c ^= b; c -= rot(b,16); \
3878 a ^= c; a -= rot(c,4); \
3879 b ^= a; b -= rot(a,14); \
3880 c ^= b; c -= rot(b,24); \
3884 mono_aot_type_hash (MonoType *t1)
3886 guint hash = t1->type;
3888 hash |= t1->byref << 6; /* do not collide with t1->type values */
3890 case MONO_TYPE_VALUETYPE:
3891 case MONO_TYPE_CLASS:
3892 case MONO_TYPE_SZARRAY:
3893 /* check if the distribution is good enough */
3894 return ((hash << 5) - hash) ^ mono_aot_str_hash (t1->data.klass->name);
3896 return ((hash << 5) - hash) ^ mono_aot_type_hash (t1->data.type);
3897 case MONO_TYPE_ARRAY:
3898 return ((hash << 5) - hash) ^ mono_aot_type_hash (&t1->data.array->eklass->byval_arg);
3899 case MONO_TYPE_GENERICINST:
3900 return ((hash << 5) - hash) ^ 0;
3906 * mono_aot_method_hash:
3908 * Return a hash code for methods which only depends on metadata.
3911 mono_aot_method_hash (MonoMethod *method)
3913 MonoMethodSignature *sig;
3917 guint32 *hashes_start, *hashes;
3920 /* Similar to the hash in mono_method_get_imt_slot () */
3922 sig = mono_method_signature (method);
3924 hashes_count = sig->param_count + 5;
3925 hashes_start = malloc (hashes_count * sizeof (guint32));
3926 hashes = hashes_start;
3928 /* Some wrappers are assigned to random classes */
3929 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
3930 klass = method->klass;
3932 klass = mono_defaults.object_class;
3934 if (!method->wrapper_type) {
3935 char *full_name = mono_type_full_name (&klass->byval_arg);
3937 hashes [0] = mono_aot_str_hash (full_name);
3941 hashes [0] = mono_aot_str_hash (klass->name);
3942 hashes [1] = mono_aot_str_hash (klass->name_space);
3944 hashes [2] = mono_aot_str_hash (method->name);
3945 hashes [3] = method->wrapper_type;
3946 hashes [4] = mono_aot_type_hash (sig->ret);
3947 for (i = 0; i < sig->param_count; i++) {
3948 hashes [5 + i] = mono_aot_type_hash (sig->params [i]);
3951 /* Setup internal state */
3952 a = b = c = 0xdeadbeef + (((guint32)hashes_count)<<2);
3954 /* Handle most of the hashes */
3955 while (hashes_count > 3) {
3964 /* Handle the last 3 hashes (all the case statements fall through) */
3965 switch (hashes_count) {
3966 case 3 : c += hashes [2];
3967 case 2 : b += hashes [1];
3968 case 1 : a += hashes [0];
3970 case 0: /* nothing left to add */
3974 free (hashes_start);
3983 * mono_aot_wrapper_name:
3985 * Return a string which uniqely identifies the given wrapper method.
3988 mono_aot_wrapper_name (MonoMethod *method)
3990 char *name, *tmpsig, *klass_desc;
3992 tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
3994 switch (method->wrapper_type) {
3995 case MONO_WRAPPER_RUNTIME_INVOKE:
3996 case MONO_WRAPPER_DELEGATE_INVOKE:
3997 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
3998 case MONO_WRAPPER_DELEGATE_END_INVOKE:
3999 /* This is a hack to work around the fact that runtime invoke wrappers get assigned to some random class */
4000 name = g_strdup_printf ("%s (%s)", method->name, tmpsig);
4003 klass_desc = mono_type_full_name (&method->klass->byval_arg);
4005 name = g_strdup_printf ("%s:%s (%s)", klass_desc, method->name, tmpsig);
4015 * mono_aot_tramp_info_create:
4017 * Create a MonoAotTrampInfo structure from the arguments.
4020 mono_aot_tramp_info_create (const char *name, guint8 *code, guint32 code_size)
4022 MonoAotTrampInfo *info = g_new0 (MonoAotTrampInfo, 1);
4024 info->name = (char*)name;
4026 info->code_size = code_size;
4031 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
4033 typedef struct HashEntry {
4034 guint32 key, value, index;
4035 struct HashEntry *next;
4039 * emit_extra_methods:
4041 * Emit methods which are not in the METHOD table, like wrappers.
4044 emit_extra_methods (MonoAotCompile *acfg)
4046 int i, table_size, buf_size;
4049 guint32 *info_offsets;
4052 HashEntry *entry, *new_entry;
4053 int nmethods, max_chain_length;
4056 info_offsets = g_new0 (guint32, acfg->extra_methods->len);
4058 buf_size = acfg->extra_methods->len * 256 + 256;
4059 p = buf = g_malloc (buf_size);
4061 /* Encode method info */
4063 /* So offsets are > 0 */
4066 for (i = 0; i < acfg->extra_methods->len; ++i) {
4067 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
4068 MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
4075 info_offsets [i] = p - buf;
4078 if (method->wrapper_type) {
4080 * We encode some wrappers using their name, since encoding them
4081 * directly would be difficult. This also avoids creating the wrapper
4082 * methods at runtime, since they are not needed anyway.
4084 switch (method->wrapper_type) {
4085 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
4086 case MONO_WRAPPER_SYNCHRONIZED:
4087 /* encode_method_ref () can handle these */
4089 case MONO_WRAPPER_RUNTIME_INVOKE:
4090 if (mono_marshal_method_from_wrapper (method) != method && !strstr (method->name, "virtual"))
4091 /* Direct wrapper, encode normally */
4095 name = mono_aot_wrapper_name (method);
4101 encode_value (1, p, &p);
4102 encode_value (method->wrapper_type, p, &p);
4103 strcpy ((char*)p, name);
4104 p += strlen (name ) + 1;
4107 encode_value (0, p, &p);
4108 encode_method_ref (acfg, method, p, &p);
4111 g_assert ((p - buf) < buf_size);
4114 g_assert ((p - buf) < buf_size);
4116 /* Emit method info */
4117 sprintf (symbol, "extra_method_info");
4118 emit_section_change (acfg, ".text", 1);
4119 emit_global (acfg, symbol, FALSE);
4120 emit_alignment (acfg, 8);
4121 emit_label (acfg, symbol);
4123 emit_bytes (acfg, buf, p - buf);
4128 * Construct a chained hash table for mapping indexes in extra_method_info to
4131 table_size = g_spaced_primes_closest ((int)(nmethods * 1.5));
4132 table = g_ptr_array_sized_new (table_size);
4133 for (i = 0; i < table_size; ++i)
4134 g_ptr_array_add (table, NULL);
4135 chain_lengths = g_new0 (int, table_size);
4136 max_chain_length = 0;
4137 for (i = 0; i < acfg->extra_methods->len; ++i) {
4138 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
4139 MonoCompile *cfg = g_hash_table_lookup (acfg->method_to_cfg, method);
4145 key = info_offsets [i];
4146 value = get_method_index (acfg, method);
4148 hash = mono_aot_method_hash (method) % table_size;
4150 chain_lengths [hash] ++;
4151 max_chain_length = MAX (max_chain_length, chain_lengths [hash]);
4153 /* FIXME: Allocate from the mempool */
4154 new_entry = g_new0 (HashEntry, 1);
4155 new_entry->key = key;
4156 new_entry->value = value;
4158 entry = g_ptr_array_index (table, hash);
4159 if (entry == NULL) {
4160 new_entry->index = hash;
4161 g_ptr_array_index (table, hash) = new_entry;
4164 entry = entry->next;
4166 entry->next = new_entry;
4167 new_entry->index = table->len;
4168 g_ptr_array_add (table, new_entry);
4172 //printf ("MAX: %d\n", max_chain_length);
4174 /* Emit the table */
4175 sprintf (symbol, "extra_method_table");
4176 emit_section_change (acfg, ".text", 0);
4177 emit_global (acfg, symbol, FALSE);
4178 emit_alignment (acfg, 8);
4179 emit_label (acfg, symbol);
4181 g_assert (table_size < 65000);
4182 emit_int32 (acfg, table_size);
4183 g_assert (table->len < 65000);
4184 for (i = 0; i < table->len; ++i) {
4185 HashEntry *entry = g_ptr_array_index (table, i);
4187 if (entry == NULL) {
4188 emit_int32 (acfg, 0);
4189 emit_int32 (acfg, 0);
4190 emit_int32 (acfg, 0);
4192 g_assert (entry->key > 0);
4193 emit_int32 (acfg, entry->key);
4194 emit_int32 (acfg, entry->value);
4196 emit_int32 (acfg, entry->next->index);
4198 emit_int32 (acfg, 0);
4203 * Emit a table reverse mapping method indexes to their index in extra_method_info.
4204 * This is used by mono_aot_find_jit_info ().
4206 sprintf (symbol, "extra_method_info_offsets");
4207 emit_section_change (acfg, ".text", 0);
4208 emit_global (acfg, symbol, FALSE);
4209 emit_alignment (acfg, 8);
4210 emit_label (acfg, symbol);
4212 emit_int32 (acfg, acfg->extra_methods->len);
4213 for (i = 0; i < acfg->extra_methods->len; ++i) {
4214 MonoMethod *method = g_ptr_array_index (acfg->extra_methods, i);
4216 emit_int32 (acfg, get_method_index (acfg, method));
4217 emit_int32 (acfg, info_offsets [i]);
4222 emit_method_order (MonoAotCompile *acfg)
4228 sprintf (symbol, "method_order");
4229 emit_section_change (acfg, ".text", 1);
4230 emit_global (acfg, symbol, FALSE);
4231 emit_alignment (acfg, 8);
4232 emit_label (acfg, symbol);
4234 /* First emit an index table */
4237 for (l = acfg->method_order; l != NULL; l = l->next) {
4238 i = GPOINTER_TO_UINT (l->data);
4240 if (acfg->cfgs [i]) {
4241 if ((index % 1024) == 0) {
4242 emit_int32 (acfg, i);
4250 emit_int32 (acfg, 0xffffff);
4252 /* Then emit the whole method order */
4253 for (l = acfg->method_order; l != NULL; l = l->next) {
4254 i = GPOINTER_TO_UINT (l->data);
4256 if (acfg->cfgs [i]) {
4257 emit_int32 (acfg, i);
4262 sprintf (symbol, "method_order_end");
4263 emit_section_change (acfg, ".text", 1);
4264 emit_global (acfg, symbol, FALSE);
4265 emit_label (acfg, symbol);
4269 emit_exception_info (MonoAotCompile *acfg)
4274 sprintf (symbol, "ex_info");
4275 emit_section_change (acfg, ".text", 1);
4276 emit_global (acfg, symbol, FALSE);
4277 emit_alignment (acfg, 8);
4278 emit_label (acfg, symbol);
4280 /* To reduce size of generated assembly */
4281 sprintf (symbol, "ex");
4282 emit_label (acfg, symbol);
4284 for (i = 0; i < acfg->nmethods; ++i) {
4286 emit_exception_debug_info (acfg, acfg->cfgs [i]);
4289 sprintf (symbol, "ex_info_offsets");
4290 emit_section_change (acfg, ".text", 1);
4291 emit_global (acfg, symbol, FALSE);
4292 emit_alignment (acfg, 8);
4293 emit_label (acfg, symbol);
4295 for (i = 0; i < acfg->nmethods; ++i) {
4296 if (acfg->cfgs [i]) {
4297 sprintf (symbol, "%se_%x_p", acfg->temp_prefix, i);
4298 emit_symbol_diff (acfg, symbol, "ex", 0);
4300 emit_int32 (acfg, 0);
4307 emit_unwind_info (MonoAotCompile *acfg)
4313 * The unwind info contains a lot of duplicates so we emit each unique
4314 * entry once, and only store the offset from the start of the table in the
4318 sprintf (symbol, "unwind_info");
4319 emit_section_change (acfg, ".text", 1);
4320 emit_alignment (acfg, 8);
4321 emit_label (acfg, symbol);
4322 emit_global (acfg, symbol, FALSE);
4324 for (i = 0; i < acfg->unwind_ops->len; ++i) {
4325 guint32 index = GPOINTER_TO_UINT (g_ptr_array_index (acfg->unwind_ops, i));
4326 guint8 *unwind_info;
4327 guint32 unwind_info_len;
4331 unwind_info = mono_get_cached_unwind_info (index, &unwind_info_len);
4334 encode_value (unwind_info_len, p, &p);
4335 emit_bytes (acfg, buf, p - buf);
4336 emit_bytes (acfg, unwind_info, unwind_info_len);
4338 acfg->stats.unwind_info_size += (p - buf) + unwind_info_len;
4343 emit_class_info (MonoAotCompile *acfg)
4348 sprintf (symbol, "class_info");
4349 emit_section_change (acfg, ".text", 1);
4350 emit_global (acfg, symbol, FALSE);
4351 emit_alignment (acfg, 8);
4352 emit_label (acfg, symbol);
4354 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
4355 emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
4357 sprintf (symbol, "class_info_offsets");
4358 emit_section_change (acfg, ".text", 1);
4359 emit_global (acfg, symbol, FALSE);
4360 emit_alignment (acfg, 8);
4361 emit_label (acfg, symbol);
4363 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
4364 sprintf (symbol, "%sK_I_%x", acfg->temp_prefix, i);
4365 emit_symbol_diff (acfg, symbol, "class_info", 0);
4370 typedef struct ClassNameTableEntry {
4371 guint32 token, index;
4372 struct ClassNameTableEntry *next;
4373 } ClassNameTableEntry;
4376 emit_class_name_table (MonoAotCompile *acfg)
4379 guint32 token, hash;
4384 ClassNameTableEntry *entry, *new_entry;
4387 * Construct a chained hash table for mapping class names to typedef tokens.
4389 table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
4390 table = g_ptr_array_sized_new (table_size);
4391 for (i = 0; i < table_size; ++i)
4392 g_ptr_array_add (table, NULL);
4393 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
4394 token = MONO_TOKEN_TYPE_DEF | (i + 1);
4395 klass = mono_class_get (acfg->image, token);
4396 full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
4397 hash = mono_aot_str_hash (full_name) % table_size;
4400 /* FIXME: Allocate from the mempool */
4401 new_entry = g_new0 (ClassNameTableEntry, 1);
4402 new_entry->token = token;
4404 entry = g_ptr_array_index (table, hash);
4405 if (entry == NULL) {
4406 new_entry->index = hash;
4407 g_ptr_array_index (table, hash) = new_entry;
4410 entry = entry->next;
4412 entry->next = new_entry;
4413 new_entry->index = table->len;
4414 g_ptr_array_add (table, new_entry);
4418 /* Emit the table */
4419 sprintf (symbol, "class_name_table");
4420 emit_section_change (acfg, ".text", 0);
4421 emit_global (acfg, symbol, FALSE);
4422 emit_alignment (acfg, 8);
4423 emit_label (acfg, symbol);
4425 /* FIXME: Optimize memory usage */
4426 g_assert (table_size < 65000);
4427 emit_int16 (acfg, table_size);
4428 g_assert (table->len < 65000);
4429 for (i = 0; i < table->len; ++i) {
4430 ClassNameTableEntry *entry = g_ptr_array_index (table, i);
4432 if (entry == NULL) {
4433 emit_int16 (acfg, 0);
4434 emit_int16 (acfg, 0);
4436 emit_int16 (acfg, mono_metadata_token_index (entry->token));
4438 emit_int16 (acfg, entry->next->index);
4440 emit_int16 (acfg, 0);
4446 emit_image_table (MonoAotCompile *acfg)
4452 * The image table is small but referenced in a lot of places.
4453 * So we emit it at once, and reference its elements by an index.
4456 sprintf (symbol, "mono_image_table");
4457 emit_section_change (acfg, ".text", 1);
4458 emit_global (acfg, symbol, FALSE);
4459 emit_alignment (acfg, 8);
4460 emit_label (acfg, symbol);
4462 emit_int32 (acfg, acfg->image_table->len);
4463 for (i = 0; i < acfg->image_table->len; i++) {
4464 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
4465 MonoAssemblyName *aname = &image->assembly->aname;
4467 /* FIXME: Support multi-module assemblies */
4468 g_assert (image->assembly->image == image);
4470 emit_string (acfg, image->assembly_name);
4471 emit_string (acfg, image->guid);
4472 emit_string (acfg, aname->culture ? aname->culture : "");
4473 emit_string (acfg, (const char*)aname->public_key_token);
4475 emit_alignment (acfg, 8);
4476 emit_int32 (acfg, aname->flags);
4477 emit_int32 (acfg, aname->major);
4478 emit_int32 (acfg, aname->minor);
4479 emit_int32 (acfg, aname->build);
4480 emit_int32 (acfg, aname->revision);
4485 emit_got_info (MonoAotCompile *acfg)
4488 int i, first_plt_got_patch, buf_size;
4490 guint32 *got_info_offsets;
4492 /* Add the patches needed by the PLT to the GOT */
4493 acfg->plt_got_offset_base = acfg->got_offset;
4494 first_plt_got_patch = acfg->got_patches->len;
4495 for (i = 1; i < acfg->plt_offset; ++i) {
4496 MonoJumpInfo *patch_info = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
4498 g_ptr_array_add (acfg->got_patches, patch_info);
4501 acfg->got_offset += acfg->plt_offset;
4505 * - optimize offsets table.
4506 * - reduce number of exported symbols.
4507 * - emit info for a klass only once.
4508 * - determine when a method uses a GOT slot which is guaranteed to be already
4510 * - clean up and document the code.
4511 * - use String.Empty in class libs.
4514 /* Encode info required to decode shared GOT entries */
4515 buf_size = acfg->got_patches->len * 64;
4516 p = buf = mono_mempool_alloc (acfg->mempool, buf_size);
4517 got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->got_patches->len * sizeof (guint32));
4518 acfg->plt_got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->plt_offset * sizeof (guint32));
4520 if (acfg->plt_offset)
4521 acfg->plt_got_info_offsets [0] = 0;
4522 for (i = 0; i < acfg->got_patches->len; ++i) {
4523 MonoJumpInfo *ji = g_ptr_array_index (acfg->got_patches, i);
4525 got_info_offsets [i] = p - buf;
4526 if (i >= first_plt_got_patch)
4527 acfg->plt_got_info_offsets [i - first_plt_got_patch + 1] = got_info_offsets [i];
4528 encode_value (ji->type, p, &p);
4529 encode_patch (acfg, ji, p, &p);
4532 g_assert (p - buf <= buf_size);
4534 acfg->stats.got_info_size = p - buf;
4536 /* Emit got_info table */
4537 sprintf (symbol, "got_info");
4538 emit_section_change (acfg, ".text", 1);
4539 emit_global (acfg, symbol, FALSE);
4540 emit_alignment (acfg, 8);
4541 emit_label (acfg, symbol);
4543 emit_bytes (acfg, buf, p - buf);
4545 /* Emit got_info_offsets table */
4546 sprintf (symbol, "got_info_offsets");
4547 emit_section_change (acfg, ".text", 1);
4548 emit_global (acfg, symbol, FALSE);
4549 emit_alignment (acfg, 8);
4550 emit_label (acfg, symbol);
4552 /* No need to emit offsets for the got plt entries, the plt embeds them directly */
4553 for (i = 0; i < first_plt_got_patch; ++i)
4554 emit_int32 (acfg, got_info_offsets [i]);
4556 acfg->stats.got_info_offsets_size = acfg->got_patches->len * 4;
4560 emit_got (MonoAotCompile *acfg)
4564 /* Don't make GOT global so accesses to it don't need relocations */
4565 sprintf (symbol, acfg->got_symbol);
4566 emit_section_change (acfg, ".bss", 0);
4567 emit_alignment (acfg, 8);
4568 emit_local_symbol (acfg, symbol, "got_end", FALSE);
4569 emit_label (acfg, symbol);
4570 if (acfg->got_offset > 0)
4571 emit_zero_bytes (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
4573 sprintf (symbol, "got_end");
4574 emit_label (acfg, symbol);
4576 sprintf (symbol, "mono_aot_got_addr");
4577 emit_section_change (acfg, ".data", 0);
4578 emit_global (acfg, symbol, FALSE);
4579 emit_alignment (acfg, 8);
4580 emit_label (acfg, symbol);
4581 emit_pointer (acfg, acfg->got_symbol);
4585 emit_globals (MonoAotCompile *acfg)
4590 emit_string_symbol (acfg, "mono_assembly_guid" , acfg->image->guid);
4592 emit_string_symbol (acfg, "mono_aot_version", MONO_AOT_FILE_VERSION);
4594 opts_str = g_strdup_printf ("%d", acfg->opts);
4595 emit_string_symbol (acfg, "mono_aot_opt_flags", opts_str);
4598 emit_string_symbol (acfg, "mono_aot_full_aot", acfg->aot_opts.full_aot ? "TRUE" : "FALSE");
4600 if (acfg->aot_opts.bind_to_runtime_version) {
4601 build_info = mono_get_runtime_build_info ();
4602 emit_string_symbol (acfg, "mono_runtime_version", build_info);
4603 g_free (build_info);
4605 emit_string_symbol (acfg, "mono_runtime_version", "");
4609 * When static linking, we emit a global which will point to the symbol table.
4611 if (acfg->aot_opts.static_link) {
4616 /* Emit a string holding the assembly name */
4617 emit_string_symbol (acfg, "mono_aot_assembly_name", acfg->image->assembly->aname.name);
4619 /* Emit the names */
4620 for (i = 0; i < acfg->globals->len; ++i) {
4621 char *name = g_ptr_array_index (acfg->globals, i);
4623 sprintf (symbol, "name_%d", i);
4624 emit_section_change (acfg, ".text", 1);
4625 emit_label (acfg, symbol);
4626 emit_string (acfg, name);
4629 /* Emit the globals table */
4630 sprintf (symbol, "globals");
4631 emit_section_change (acfg, ".data", 0);
4632 /* This is not a global, since it is accessed by the init function */
4633 emit_alignment (acfg, 8);
4634 emit_label (acfg, symbol);
4636 for (i = 0; i < acfg->globals->len; ++i) {
4637 char *name = g_ptr_array_index (acfg->globals, i);
4639 sprintf (symbol, "name_%d", i);
4640 emit_pointer (acfg, symbol);
4642 sprintf (symbol, "%s", name);
4643 emit_pointer (acfg, symbol);
4645 /* Null terminate the table */
4646 emit_int32 (acfg, 0);
4647 emit_int32 (acfg, 0);
4650 * Emit a global symbol which can be passed by an embedding app to
4651 * mono_aot_register_module ().
4653 #if defined(__MACH__)
4654 sprintf (symbol, "_mono_aot_module_%s_info", acfg->image->assembly->aname.name);
4656 sprintf (symbol, "mono_aot_module_%s_info", acfg->image->assembly->aname.name);
4659 /* Get rid of characters which cannot occur in symbols */
4661 for (p = symbol; *p; ++p) {
4662 if (!(isalnum (*p) || *p == '_'))
4665 acfg->static_linking_symbol = g_strdup (symbol);
4666 emit_global_inner (acfg, symbol, FALSE);
4667 emit_alignment (acfg, 8);
4668 emit_label (acfg, symbol);
4669 emit_pointer (acfg, "globals");
4674 emit_mem_end (MonoAotCompile *acfg)
4678 sprintf (symbol, "mem_end");
4679 emit_section_change (acfg, ".text", 1);
4680 emit_global (acfg, symbol, FALSE);
4681 emit_alignment (acfg, 8);
4682 emit_label (acfg, symbol);
4686 * Emit a structure containing all the information not stored elsewhere.
4689 emit_file_info (MonoAotCompile *acfg)
4694 sprintf (symbol, "mono_aot_file_info");
4695 emit_section_change (acfg, ".data", 0);
4696 emit_alignment (acfg, 8);
4697 emit_label (acfg, symbol);
4698 emit_global (acfg, symbol, FALSE);
4700 /* The data emitted here must match MonoAotFileInfo in aot-runtime.c. */
4701 emit_int32 (acfg, acfg->plt_got_offset_base);
4702 emit_int32 (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
4703 emit_int32 (acfg, acfg->plt_offset);
4705 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
4706 emit_int32 (acfg, acfg->num_trampolines [i]);
4707 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
4708 emit_int32 (acfg, acfg->trampoline_got_offset_base [i]);
4709 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
4710 emit_int32 (acfg, acfg->trampoline_size [i]);
4714 emit_dwarf_info (MonoAotCompile *acfg)
4716 #ifdef EMIT_DWARF_INFO
4718 char symbol [128], symbol2 [128];
4720 /* DIEs for methods */
4721 for (i = 0; i < acfg->nmethods; ++i) {
4722 MonoCompile *cfg = acfg->cfgs [i];
4727 sprintf (symbol, "%sm_%x", acfg->temp_prefix, i);
4728 sprintf (symbol2, "%sme_%x", acfg->temp_prefix, i);
4730 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 ()));
4736 collect_methods (MonoAotCompile *acfg)
4739 MonoImage *image = acfg->image;
4741 /* Collect methods */
4742 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
4744 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
4746 method = mono_get_method (acfg->image, token, NULL);
4749 printf ("Failed to load method 0x%x from '%s'.\n", token, image->name);
4753 /* Load all methods eagerly to skip the slower lazy loading code */
4754 mono_class_setup_methods (method->klass);
4756 if (acfg->aot_opts.full_aot && method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
4757 /* Compile the wrapper instead */
4758 /* We do this here instead of add_wrappers () because it is easy to do it here */
4759 MonoMethod *wrapper = mono_marshal_get_native_wrapper (method, check_for_pending_exc, TRUE);
4763 /* Since we add the normal methods first, their index will be equal to their zero based token index */
4764 add_method_with_index (acfg, method, i, FALSE);
4765 acfg->method_index ++;
4768 add_generic_instances (acfg);
4770 if (acfg->aot_opts.full_aot)
4771 add_wrappers (acfg);
4775 compile_methods (MonoAotCompile *acfg)
4779 if (acfg->aot_opts.nthreads > 0) {
4784 gpointer *user_data;
4785 MonoMethod **methods;
4787 methods_len = acfg->methods->len;
4789 len = acfg->methods->len / acfg->aot_opts.nthreads;
4792 * Partition the list of methods into fragments, and hand it to threads to
4795 threads = g_ptr_array_new ();
4796 /* Make a copy since acfg->methods is modified by compile_method () */
4797 methods = g_new0 (MonoMethod*, methods_len);
4798 //memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
4799 for (i = 0; i < methods_len; ++i)
4800 methods [i] = g_ptr_array_index (acfg->methods, i);
4802 while (i < methods_len) {
4803 frag = g_ptr_array_new ();
4804 for (j = 0; j < len; ++j) {
4805 if (i < methods_len) {
4806 g_ptr_array_add (frag, methods [i]);
4811 user_data = g_new0 (gpointer, 3);
4812 user_data [0] = mono_domain_get ();
4813 user_data [1] = acfg;
4814 user_data [2] = frag;
4816 handle = mono_create_thread (NULL, 0, (gpointer)compile_thread_main, user_data, 0, NULL);
4817 g_ptr_array_add (threads, handle);
4821 for (i = 0; i < threads->len; ++i) {
4822 WaitForSingleObjectEx (g_ptr_array_index (threads, i), INFINITE, FALSE);
4828 /* Compile methods added by compile_method () or all methods if nthreads == 0 */
4829 for (i = methods_len; i < acfg->methods->len; ++i) {
4830 /* This can new methods to acfg->methods */
4831 compile_method (acfg, g_ptr_array_index (acfg->methods, i));
4836 compile_asm (MonoAotCompile *acfg)
4838 char *command, *objfile;
4839 char *outfile_name, *tmp_outfile_name;
4841 #if defined(TARGET_AMD64)
4842 #define AS_OPTIONS "--64"
4843 #elif defined(TARGET_POWERPC64)
4844 #define AS_OPTIONS "-a64 -mppc64"
4845 #define LD_OPTIONS "-m elf64ppc"
4846 #elif defined(sparc) && SIZEOF_VOID_P == 8
4847 #define AS_OPTIONS "-xarch=v9"
4849 #define AS_OPTIONS ""
4853 #define LD_OPTIONS ""
4856 if (acfg->aot_opts.asm_only) {
4857 printf ("Output file: '%s'.\n", acfg->tmpfname);
4858 if (acfg->aot_opts.static_link)
4859 printf ("Linking symbol: '%s'.\n", acfg->static_linking_symbol);
4863 if (acfg->aot_opts.static_link) {
4864 if (acfg->aot_opts.outfile)
4865 objfile = g_strdup_printf ("%s", acfg->aot_opts.outfile);
4867 objfile = g_strdup_printf ("%s.o", acfg->image->name);
4869 objfile = g_strdup_printf ("%s.o", acfg->tmpfname);
4871 command = g_strdup_printf ("as %s %s -o %s", AS_OPTIONS, acfg->tmpfname, objfile);
4872 printf ("Executing the native assembler: %s\n", command);
4873 if (system (command) != 0) {
4881 if (acfg->aot_opts.static_link) {
4882 printf ("Output file: '%s'.\n", objfile);
4883 printf ("Linking symbol: '%s'.\n", acfg->static_linking_symbol);
4888 if (acfg->aot_opts.outfile)
4889 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
4891 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
4893 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
4896 command = g_strdup_printf ("ld -shared -G -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
4897 #elif defined(__ppc__) && defined(__MACH__)
4898 command = g_strdup_printf ("gcc -dynamiclib -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
4899 #elif defined(PLATFORM_WIN32)
4900 command = g_strdup_printf ("gcc -shared --dll -mno-cygwin -o %s %s.o", tmp_outfile_name, acfg->tmpfname);
4902 command = g_strdup_printf ("ld %s -shared -o %s %s.o", LD_OPTIONS, tmp_outfile_name, acfg->tmpfname);
4904 printf ("Executing the native linker: %s\n", command);
4905 if (system (command) != 0) {
4906 g_free (tmp_outfile_name);
4907 g_free (outfile_name);
4915 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, SHARED_EXT);
4916 printf ("Stripping the binary: %s\n", com);
4920 #if defined(TARGET_ARM) && !defined(__MACH__)
4922 * gas generates 'mapping symbols' each time code and data is mixed, which
4923 * happens a lot in emit_and_reloc_code (), so we need to get rid of them.
4925 command = g_strdup_printf ("strip --strip-symbol=\\$a --strip-symbol=\\$d %s", tmp_outfile_name);
4926 printf ("Stripping the binary: %s\n", command);
4927 if (system (command) != 0) {
4928 g_free (tmp_outfile_name);
4929 g_free (outfile_name);
4936 rename (tmp_outfile_name, outfile_name);
4938 g_free (tmp_outfile_name);
4939 g_free (outfile_name);
4942 if (acfg->aot_opts.save_temps)
4943 printf ("Retained input file.\n");
4945 unlink (acfg->tmpfname);
4950 static MonoAotCompile*
4951 acfg_create (MonoAssembly *ass, guint32 opts)
4953 MonoImage *image = ass->image;
4954 MonoAotCompile *acfg;
4956 acfg = g_new0 (MonoAotCompile, 1);
4957 acfg->methods = g_ptr_array_new ();
4958 acfg->method_indexes = g_hash_table_new (NULL, NULL);
4959 acfg->plt_offset_to_patch = g_hash_table_new (NULL, NULL);
4960 acfg->patch_to_plt_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
4961 acfg->patch_to_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
4962 acfg->got_patches = g_ptr_array_new ();
4963 acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
4964 acfg->token_info_hash = g_hash_table_new_full (NULL, NULL, NULL, g_free);
4965 acfg->image_hash = g_hash_table_new (NULL, NULL);
4966 acfg->image_table = g_ptr_array_new ();
4967 acfg->globals = g_ptr_array_new ();
4968 acfg->image = image;
4970 acfg->mempool = mono_mempool_new ();
4971 acfg->extra_methods = g_ptr_array_new ();
4972 acfg->unwind_info_offsets = g_hash_table_new (NULL, NULL);
4973 acfg->unwind_ops = g_ptr_array_new ();
4974 acfg->method_label_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
4975 InitializeCriticalSection (&acfg->mutex);
4981 acfg_free (MonoAotCompile *acfg)
4985 img_writer_destroy (acfg->w);
4986 for (i = 0; i < acfg->nmethods; ++i)
4988 g_free (acfg->cfgs [i]);
4989 g_free (acfg->cfgs);
4990 g_free (acfg->static_linking_symbol);
4991 g_free (acfg->got_symbol);
4992 g_free (acfg->plt_symbol);
4993 g_ptr_array_free (acfg->methods, TRUE);
4994 g_ptr_array_free (acfg->got_patches, TRUE);
4995 g_ptr_array_free (acfg->image_table, TRUE);
4996 g_ptr_array_free (acfg->globals, TRUE);
4997 g_ptr_array_free (acfg->unwind_ops, TRUE);
4998 g_hash_table_destroy (acfg->method_indexes);
4999 g_hash_table_destroy (acfg->plt_offset_to_patch);
5000 g_hash_table_destroy (acfg->patch_to_plt_offset);
5001 g_hash_table_destroy (acfg->patch_to_got_offset);
5002 g_hash_table_destroy (acfg->method_to_cfg);
5003 g_hash_table_destroy (acfg->token_info_hash);
5004 g_hash_table_destroy (acfg->image_hash);
5005 g_hash_table_destroy (acfg->unwind_info_offsets);
5006 g_hash_table_destroy (acfg->method_label_hash);
5007 mono_mempool_destroy (acfg->mempool);
5012 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
5014 MonoImage *image = ass->image;
5016 MonoAotCompile *acfg;
5017 char *outfile_name, *tmp_outfile_name, *p;
5021 printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
5023 acfg = acfg_create (ass, opts);
5025 memset (&acfg->aot_opts, 0, sizeof (acfg->aot_opts));
5026 acfg->aot_opts.write_symbols = TRUE;
5027 acfg->aot_opts.ntrampolines = 1024;
5029 mono_aot_parse_options (aot_options, &acfg->aot_opts);
5031 //acfg->aot_opts.print_skipped_methods = TRUE;
5033 #ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
5034 if (acfg->aot_opts.full_aot) {
5035 printf ("--aot=full is not supported on this platform.\n");
5040 if (acfg->aot_opts.static_link)
5041 acfg->aot_opts.asm_writer = TRUE;
5043 load_profile_files (acfg);
5045 acfg->num_trampolines [MONO_AOT_TRAMP_SPECIFIC] = acfg->aot_opts.full_aot ? acfg->aot_opts.ntrampolines : 0;
5046 #ifdef MONO_ARCH_HAVE_STATIC_RGCTX_TRAMPOLINE
5047 acfg->num_trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = acfg->aot_opts.full_aot ? 1024 : 0;
5049 acfg->num_trampolines [MONO_AOT_TRAMP_IMT_THUNK] = acfg->aot_opts.full_aot ? 128 : 0;
5051 acfg->got_symbol = g_strdup_printf ("mono_aot_%s_got", acfg->image->assembly->aname.name);
5052 acfg->plt_symbol = g_strdup_printf ("mono_aot_%s_plt", acfg->image->assembly->aname.name);
5054 /* Get rid of characters which cannot occur in symbols */
5055 for (p = acfg->got_symbol; *p; ++p) {
5056 if (!(isalnum (*p) || *p == '_'))
5059 for (p = acfg->plt_symbol; *p; ++p) {
5060 if (!(isalnum (*p) || *p == '_'))
5064 acfg->method_index = 1;
5066 collect_methods (acfg);
5068 acfg->cfgs_size = acfg->methods->len + 32;
5069 acfg->cfgs = g_new0 (MonoCompile*, acfg->cfgs_size);
5071 /* PLT offset 0 is reserved for the PLT trampoline */
5072 acfg->plt_offset = 1;
5074 /* GOT offset 0 is reserved for the address of the current assembly */
5078 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
5079 ji->type = MONO_PATCH_INFO_IMAGE;
5080 ji->data.image = acfg->image;
5082 get_got_offset (acfg, ji);
5084 /* Slot 1 is reserved for the mscorlib got addr */
5085 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
5086 ji->type = MONO_PATCH_INFO_MSCORLIB_GOT_ADDR;
5087 get_got_offset (acfg, ji);
5092 compile_methods (acfg);
5096 acfg->stats.jit_time = TV_ELAPSED (atv, btv);
5100 if (!acfg->aot_opts.asm_only && !acfg->aot_opts.asm_writer && bin_writer_supported ()) {
5101 if (acfg->aot_opts.outfile)
5102 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
5104 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
5107 * Can't use g_file_open_tmp () as it will be deleted at exit, and
5108 * it might be in another file system so the rename () won't work.
5110 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
5112 acfg->fp = fopen (tmp_outfile_name, "w");
5114 printf ("Unable to create temporary file '%s': %s\n", tmp_outfile_name, strerror (errno));
5118 acfg->w = img_writer_create (acfg->fp, TRUE);
5119 acfg->use_bin_writer = TRUE;
5121 if (acfg->aot_opts.asm_only) {
5122 if (acfg->aot_opts.outfile)
5123 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
5125 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->image->name);
5126 acfg->fp = fopen (acfg->tmpfname, "w+");
5128 int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
5129 acfg->fp = fdopen (i, "w+");
5131 g_assert (acfg->fp);
5133 acfg->w = img_writer_create (acfg->fp, FALSE);
5135 tmp_outfile_name = NULL;
5136 outfile_name = NULL;
5139 acfg->temp_prefix = img_writer_get_temp_label_prefix (acfg->w);
5141 if (!acfg->aot_opts.nodebug)
5142 acfg->dwarf = mono_dwarf_writer_create (acfg->w, NULL, FALSE);
5144 img_writer_emit_start (acfg->w);
5147 mono_dwarf_writer_emit_base_info (acfg->dwarf, arch_get_cie_program ());
5153 emit_extra_methods (acfg);
5155 emit_method_order (acfg);
5157 emit_trampolines (acfg);
5159 emit_class_name_table (acfg);
5161 emit_got_info (acfg);
5163 emit_exception_info (acfg);
5165 emit_unwind_info (acfg);
5167 emit_class_info (acfg);
5171 emit_image_table (acfg);
5175 emit_file_info (acfg);
5177 emit_globals (acfg);
5180 emit_dwarf_info (acfg);
5181 mono_dwarf_writer_close (acfg->dwarf);
5184 emit_mem_end (acfg);
5188 acfg->stats.gen_time = TV_ELAPSED (atv, btv);
5190 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 Offsets: %d\n", acfg->stats.code_size, acfg->stats.info_size, acfg->stats.ex_info_size, acfg->stats.unwind_info_size, acfg->stats.class_info_size, acfg->plt_offset, acfg->stats.got_info_size, acfg->stats.got_info_offsets_size, (int)(acfg->got_offset * sizeof (gpointer)), (int)(acfg->nmethods * 3 * sizeof (gpointer)));
5193 res = img_writer_emit_writeout (acfg->w);
5198 if (acfg->use_bin_writer) {
5199 int err = rename (tmp_outfile_name, outfile_name);
5202 printf ("Unable to rename '%s' to '%s': %s\n", tmp_outfile_name, outfile_name, strerror (errno));
5206 res = compile_asm (acfg);
5213 acfg->stats.link_time = TV_ELAPSED (atv, btv);
5215 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);
5216 if (acfg->stats.genericcount)
5217 printf ("%d methods are generic (%d%%)\n", acfg->stats.genericcount, acfg->stats.mcount ? (acfg->stats.genericcount * 100) / acfg->stats.mcount : 100);
5218 if (acfg->stats.abscount)
5219 printf ("%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
5220 if (acfg->stats.lmfcount)
5221 printf ("%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
5222 if (acfg->stats.ocount)
5223 printf ("%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
5224 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);
5225 printf ("Direct calls: %d (%d%%)\n", acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
5228 printf ("GOT slot distribution:\n");
5229 for (i = 0; i < MONO_PATCH_INFO_NONE; ++i)
5230 if (acfg->stats.got_slot_types [i])
5231 printf ("\t%s: %d\n", get_patch_name (i), acfg->stats.got_slot_types [i]);
5234 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);
5242 * Support for emitting debug info for JITted code.
5244 * This works as follows:
5245 * - the runtime writes out an xdb.s file containing DWARF debug info.
5246 * - the user calls a gdb macro
5247 * - the macro compiles and loads this shared library using add-symbol-file.
5249 * This is based on the xdebug functionality in the Kaffe Java VM.
5251 * We emit assembly code instead of using the ELF writer, so we can emit debug info
5252 * incrementally as each method is JITted, and the debugger doesn't have to call
5253 * into the runtime to emit the shared library, which would cause all kinds of
5254 * complications, like threading issues, and the fact that the ELF writer's
5255 * emit_writeout () function cannot be called more than once.
5258 /* The recommended gdb macro is: */
5261 shell rm -f xdb.so && as --64 -o xdb.o xdb.s && ld -shared -o xdb.so xdb.o
5262 add-symbol-file xdb.so 0
5266 static MonoDwarfWriter *xdebug_writer;
5267 static FILE *xdebug_fp;
5270 mono_xdebug_init (void)
5276 xdebug_fp = fopen ("xdb.s", "w");
5278 w = img_writer_create (xdebug_fp, FALSE);
5280 img_writer_emit_start (w);
5282 /* This file will contain the IL code for methods which don't have debug info */
5283 il_file = fopen ("xdb.il", "w");
5285 xdebug_writer = mono_dwarf_writer_create (w, il_file, TRUE);
5287 /* Emit something so the file has a text segment */
5288 img_writer_emit_section_change (w, ".text", 0);
5289 img_writer_emit_string (w, "");
5291 mono_dwarf_writer_emit_base_info (xdebug_writer, arch_get_cie_program ());
5295 * mono_save_xdebug_info:
5297 * Emit debugging info for METHOD into an assembly file which can be assembled
5298 * and loaded into gdb to provide debugging info for JITted code.
5299 * LOCKING: Acquires the loader lock.
5302 mono_save_xdebug_info (MonoCompile *cfg)
5307 mono_loader_lock ();
5308 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 ()));
5310 mono_loader_unlock ();
5314 * mono_save_trampoline_xdebug_info:
5316 * Same as mono_save_xdebug_info, but for trampolines.
5317 * LOCKING: Acquires the loader lock.
5320 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)
5325 mono_loader_lock ();
5326 mono_dwarf_writer_emit_trampoline (xdebug_writer, tramp_name, NULL, NULL, code, code_size, unwind_info);
5328 mono_loader_unlock ();
5336 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
5342 mono_xdebug_init (void)
5347 mono_save_xdebug_info (MonoCompile *cfg)
5352 mono_save_trampoline_xdebug_info (const char *tramp_name, guint8 *code, guint32 code_size, GSList *unwind_info)