2 * aot.c: mono Ahead of Time compiler
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
8 * (C) 2002 Ximian, Inc.
12 #include <sys/types.h>
18 #ifndef PLATFORM_WIN32
27 #include <limits.h> /* for PAGESIZE */
32 #include <mono/metadata/tabledefs.h>
33 #include <mono/metadata/class.h>
34 #include <mono/metadata/object.h>
35 #include <mono/metadata/tokentype.h>
36 #include <mono/metadata/appdomain.h>
37 #include <mono/metadata/debug-helpers.h>
38 #include <mono/metadata/assembly.h>
39 #include <mono/metadata/metadata-internals.h>
40 #include <mono/metadata/marshal.h>
41 #include <mono/metadata/gc-internal.h>
42 #include <mono/metadata/method-builder.h>
43 #include <mono/utils/mono-logger.h>
44 #include "mono/utils/mono-compiler.h"
52 #define SHARED_EXT ".dll"
53 #elif defined(__ppc__) && defined(__MACH__)
54 #define SHARED_EXT ".dylib"
56 #define SHARED_EXT ".so"
59 #if defined(sparc) || defined(__ppc__)
60 #define AS_STRING_DIRECTIVE ".asciz"
63 #define AS_STRING_DIRECTIVE ".string"
66 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
67 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
69 typedef struct MonoAotOptions {
72 gboolean write_symbols;
73 gboolean metadata_only;
74 gboolean bind_to_runtime_version;
78 typedef struct MonoAotStats {
79 int ccount, mcount, lmfcount, abscount, wrappercount, gcount, ocount, genericcount;
80 int code_size, info_size, ex_info_size, got_size, class_info_size, got_info_size, got_info_offsets_size;
81 int methods_without_got_slots, direct_calls, all_calls;
83 int got_slot_types [MONO_PATCH_INFO_NONE];
86 /*#define USE_ELF_WRITER 1*/
88 #if defined(USE_ELF_WRITER)
89 #define USE_BIN_WRITER 1
94 typedef struct _BinSymbol BinSymbol;
95 typedef struct _BinReloc BinReloc;
96 typedef struct _BinSection BinSection;
110 typedef struct MonoAotCompile {
113 GHashTable *method_indexes;
115 GHashTable *patch_to_plt_offset;
116 GHashTable **patch_to_plt_offset_wrapper;
117 GHashTable *plt_offset_to_patch;
118 GHashTable *patch_to_shared_got_offset;
119 GPtrArray *shared_patches;
120 GHashTable *image_hash;
121 GHashTable *method_to_cfg;
122 GHashTable *token_info_hash;
123 GPtrArray *image_table;
125 /* Number of trampolines emitted into the AOT file */
126 guint32 num_aot_trampolines;
127 guint32 got_offset, plt_offset;
128 /* Number of GOT entries reserved for trampolines */
129 guint32 num_trampoline_got_entries;
130 guint32 *method_got_offsets;
131 MonoAotOptions aot_opts;
134 MonoMemPool *mempool;
137 #ifdef USE_BIN_WRITER
139 BinSection *sections;
140 BinSection *cur_section;
141 BinReloc *relocations;
147 int mode; /* emit mode */
148 int col_count; /* bytes emitted per .byte line */
152 #ifdef HAVE_ARRAY_ELEM_INIT
153 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
154 #define MSGSTRFIELD1(line) str##line
155 static const struct msgstr_t {
156 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
157 #include "patch-info.h"
160 #define PATCH_INFO(a,b) b,
161 #include "patch-info.h"
164 static const gint16 opidx [] = {
165 #define PATCH_INFO(a,b) [MONO_PATCH_INFO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
166 #include "patch-info.h"
171 get_patch_name (int info)
173 return (const char*)&opstr + opidx [info];
177 #define PATCH_INFO(a,b) b,
178 static const char* const
179 patch_types [MONO_PATCH_INFO_NUM + 1] = {
180 #include "patch-info.h"
185 get_patch_name (int info)
187 return patch_types [info];
193 is_got_patch (MonoJumpInfoType patch_type)
198 static G_GNUC_UNUSED int
199 ilog2(register int value)
202 while (value & ~0xf) count += 4, value >>= 4;
203 while (value) count++, value >>= 1;
207 #ifdef USE_BIN_WRITER
209 typedef struct _BinLabel BinLabel;
220 BinSection *val2_section;
232 gboolean is_function;
250 emit_start (MonoAotCompile *acfg)
252 acfg->labels = g_hash_table_new (g_str_hash, g_str_equal);
256 emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
260 if (acfg->cur_section && acfg->cur_section->subsection == subsection_index
261 && strcmp (acfg->cur_section->name, section_name) == 0)
263 for (section = acfg->sections; section; section = section->next) {
264 if (section->subsection == subsection_index && strcmp (section->name, section_name) == 0) {
265 acfg->cur_section = section;
270 section = g_new0 (BinSection, 1);
271 section->name = g_strdup (section_name);
272 section->subsection = subsection_index;
273 section->next = acfg->sections;
274 acfg->sections = section;
275 acfg->cur_section = section;
280 emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
282 BinSymbol *symbol = g_new0 (BinSymbol, 1);
283 symbol->name = g_strdup (name);
284 symbol->is_function = func;
285 symbol->is_global = TRUE;
286 symbol->section = acfg->cur_section;
287 /* FIXME: we align after this call... */
288 symbol->offset = symbol->section->cur_offset;
289 symbol->next = acfg->symbols;
290 acfg->symbols = symbol;
294 emit_label (MonoAotCompile *acfg, const char *name)
296 BinLabel *label = g_new0 (BinLabel, 1);
297 label->name = g_strdup (name);
298 label->section = acfg->cur_section;
299 label->offset = acfg->cur_section->cur_offset;
300 g_hash_table_insert (acfg->labels, label->name, label);
304 emit_ensure_buffer (BinSection *section, int size)
306 int new_offset = section->cur_offset + size;
307 if (new_offset >= section->data_len) {
308 int new_size = section->data_len? section->data_len * 2: 256;
310 while (new_size <= new_offset)
312 data = g_malloc0 (new_size);
313 memcpy (data, section->data, section->data_len);
314 g_free (section->data);
315 section->data = data;
316 section->data_len = new_size;
321 emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
323 emit_ensure_buffer (acfg->cur_section, size);
324 memcpy (acfg->cur_section->data + acfg->cur_section->cur_offset, buf, size);
325 acfg->cur_section->cur_offset += size;
329 emit_string (MonoAotCompile *acfg, const char *value)
331 int size = strlen (value) + 1;
332 emit_bytes (acfg, (const guint8*)value, size);
336 emit_line (MonoAotCompile *acfg)
338 /* Nothing to do in binary writer */
342 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
344 emit_section_change (acfg, ".text", 1);
345 emit_global (acfg, name, FALSE);
346 emit_label (acfg, name);
347 emit_string (acfg, value);
351 emit_alignment (MonoAotCompile *acfg, int size)
353 int offset = acfg->cur_section->cur_offset;
355 offset += (size - 1);
356 offset &= ~(size - 1);
357 add = offset - acfg->cur_section->cur_offset;
359 emit_ensure_buffer (acfg->cur_section, add);
360 acfg->cur_section->cur_offset += add;
365 emit_pointer (MonoAotCompile *acfg, const char *target)
368 emit_alignment (acfg, sizeof (gpointer));
369 reloc = g_new0 (BinReloc, 1);
370 reloc->val1 = g_strdup (target);
371 reloc->section = acfg->cur_section;
372 reloc->section_offset = acfg->cur_section->cur_offset;
373 reloc->next = acfg->relocations;
374 acfg->relocations = reloc;
375 if (strcmp (reloc->section->name, ".data") == 0) {
377 g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
379 acfg->cur_section->cur_offset += sizeof (gpointer);
383 emit_int16 (MonoAotCompile *acfg, int value)
386 emit_ensure_buffer (acfg->cur_section, 2);
387 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
388 acfg->cur_section->cur_offset += 2;
389 /* FIXME: little endian */
391 data [1] = value >> 8;
395 emit_int32 (MonoAotCompile *acfg, int value)
398 emit_ensure_buffer (acfg->cur_section, 4);
399 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
400 acfg->cur_section->cur_offset += 4;
401 /* FIXME: little endian */
403 data [1] = value >> 8;
404 data [2] = value >> 16;
405 data [3] = value >> 24;
409 emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
412 reloc = g_new0 (BinReloc, 1);
413 reloc->val1 = g_strdup (end);
414 if (strcmp (start, ".") == 0) {
415 reloc->val2_section = acfg->cur_section;
416 reloc->val2_offset = acfg->cur_section->cur_offset;
418 reloc->val2 = g_strdup (start);
420 reloc->offset = offset;
421 reloc->section = acfg->cur_section;
422 reloc->section_offset = acfg->cur_section->cur_offset;
423 reloc->next = acfg->relocations;
424 acfg->relocations = reloc;
425 acfg->cur_section->cur_offset += 4;
426 /*if (strcmp (reloc->section->name, ".data") == 0) {
428 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
433 emit_zero_bytes (MonoAotCompile *acfg, int num)
435 emit_ensure_buffer (acfg->cur_section, num);
436 acfg->cur_section->cur_offset += num;
439 #ifdef USE_ELF_WRITER
474 DYN_RELCOUNT = 0x6ffffffa
477 static const char* section_names [] = {
493 static const guint8 section_type [] = {
498 static const guint8 section_link [] = {
499 0, 2, 3, 0, 2, 0, 3, 0, 0, 0, 0, 12, 0
502 static const guint8 section_esize [] = {
503 0, 4, 16, 0, 8, 0, 8, 4, 0, 0, 0, 16, 0
506 static const guint8 section_flags [] = {
507 0, 2, 2, 2, 2, 6, 3, 3, 3, 3, 0, 0, 0
510 static const guint16 section_align [] = {
511 0, 4, 4, 1, 4, 4096, 4, 4, 8, 8, 1, 4, 1
531 struct ElfSectHeader {
544 #if SIZEOF_VOID_P == 4
546 struct ElfProgHeader {
578 struct ElfProgHeader {
622 str_table_add (ElfStrTable *table, const char* value)
626 table->data = g_string_new_len ("", 1);
627 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
629 idx = GPOINTER_TO_UINT (g_hash_table_lookup (table->hash, value));
632 idx = table->data->len;
633 g_string_append (table->data, value);
634 g_string_append_c (table->data, 0);
635 g_hash_table_insert (table->hash, (void*)value, GUINT_TO_POINTER (idx));
640 append_subsection (MonoAotCompile *acfg, struct ElfSectHeader *sheaders, BinSection *sect, BinSection *add)
642 int offset = sect->cur_offset;
643 /*offset += (sheaders [sect->shidx].sh_addralign - 1);
644 offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
647 emit_ensure_buffer (sect, offset);
648 g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
649 sect->cur_offset = offset;
651 emit_ensure_buffer (sect, add->cur_offset);
652 memcpy (sect->data + sect->cur_offset, add->data, add->cur_offset);
654 sect->cur_offset += add->cur_offset;
655 add->cur_offset = offset; /* it becomes the offset in the parent section */
656 g_print ("subsection %d of %s added at offset %d (align: %d)\n", add->subsection, sect->name, add->cur_offset, sheaders [sect->shidx].sh_addralign);
661 /* merge the subsections */
663 collect_sections (MonoAotCompile *acfg, struct ElfSectHeader *sheaders, BinSection **out, int num)
665 int i, j, maxs, num_sections;
670 for (sect = acfg->sections; sect; sect = sect->next) {
671 if (sect->subsection == 0) {
672 out [num_sections++] = sect;
673 g_assert (num_sections < num);
674 if (strcmp (sect->name, ".text") == 0) {
675 sect->shidx = SECT_TEXT;
676 } else if (strcmp (sect->name, ".data") == 0) {
677 sect->shidx = SECT_DATA;
678 } else if (strcmp (sect->name, ".bss") == 0) {
679 sect->shidx = SECT_BSS;
682 maxs = MAX (maxs, sect->subsection);
684 for (i = 0; i < num_sections; i++) {
685 for (j = 1; j <= maxs; ++j) {
686 for (sect = acfg->sections; sect; sect = sect->next) {
687 if (sect->subsection == j && strcmp (out [i]->name, sect->name) == 0) {
688 append_subsection (acfg, sheaders, out [i], sect);
697 elf_hash (const unsigned char *name)
699 unsigned long h = 0, g;
701 h = (h << 4) + *name++;
702 if ((g = h & 0xf0000000))
709 #define NUM_BUCKETS 17
712 build_hash (MonoAotCompile *acfg, int num_sections, ElfStrTable *dynstr)
715 int num_symbols = 1 + num_sections + 3;
718 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
719 if (!symbol->is_global)
722 str_table_add (dynstr, symbol->name);
723 /*g_print ("adding sym: %s\n", symbol->name);*/
725 str_table_add (dynstr, "__bss_start");
726 str_table_add (dynstr, "_edata");
727 str_table_add (dynstr, "_end");
729 data = g_new0 (int, num_symbols + 2 + NUM_BUCKETS);
730 data [0] = NUM_BUCKETS;
731 data [1] = num_symbols;
737 get_label_addr (MonoAotCompile *acfg, const char *name)
744 lab = g_hash_table_lookup (acfg->labels, name);
745 section = lab->section;
746 offset = lab->offset;
747 if (section->parent) {
748 value = section->parent->file_offset + section->cur_offset + offset;
750 value = section->file_offset + offset;
756 collect_syms (MonoAotCompile *acfg, int *hash, ElfStrTable *strtab, struct ElfSectHeader *sheaders, int *num_syms)
767 symbols = g_new0 (ElfSymbol, hash [1]);
769 symbols = g_new0 (ElfSymbol, *num_syms + SECT_NUM + 10); /* FIXME */
771 /* the first symbol is undef, all zeroes */
775 for (j = 1; j < SECT_NUM; ++j) {
776 symbols [i].st_info = SYM_LOCAL | SYM_SECTION;
777 symbols [i].st_shndx = j;
778 symbols [i].st_value = sheaders [j].sh_addr;
782 for (section = acfg->sections; section; section = section->next) {
785 symbols [i].st_info = SYM_LOCAL | SYM_SECTION;
786 if (strcmp (section->name, ".text") == 0) {
787 symbols [i].st_shndx = SECT_TEXT;
788 section->shidx = SECT_TEXT;
789 section->file_offset = 4096;
790 symbols [i].st_value = section->file_offset;
791 } else if (strcmp (section->name, ".data") == 0) {
792 symbols [i].st_shndx = SECT_DATA;
793 section->shidx = SECT_DATA;
794 section->file_offset = 4096 + 28; /* FIXME */
795 symbols [i].st_value = section->file_offset;
796 } else if (strcmp (section->name, ".bss") == 0) {
797 symbols [i].st_shndx = SECT_BSS;
798 section->shidx = SECT_BSS;
799 section->file_offset = 4096 + 28 + 8; /* FIXME */
800 symbols [i].st_value = section->file_offset;
805 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
808 if (!symbol->is_global)
810 symbols [i].st_info = (symbol->is_function? SYM_FUNC : SYM_OBJECT) | SYM_GLOBAL;
811 symbols [i].st_name = str_table_add (strtab, symbol->name);
812 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
813 section = symbol->section;
814 symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
815 lab = g_hash_table_lookup (acfg->labels, symbol->name);
816 offset = lab->offset;
817 if (section->parent) {
818 symbols [i].st_value = section->parent->file_offset + section->cur_offset + offset;
820 symbols [i].st_value = section->file_offset + offset;
824 /* add special symbols */
825 symbols [i].st_name = str_table_add (strtab, "__bss_start");
826 symbols [i].st_shndx = 0xfff1;
827 symbols [i].st_info = SYM_GLOBAL;
829 symbols [i].st_name = str_table_add (strtab, "_edata");
830 symbols [i].st_shndx = 0xfff1;
831 symbols [i].st_info = SYM_GLOBAL;
833 symbols [i].st_name = str_table_add (strtab, "_end");
834 symbols [i].st_shndx = 0xfff1;
835 symbols [i].st_info = SYM_GLOBAL;
841 /* add to hash table */
844 chain = hash + 2 + hash [0];
845 for (i = 0; i < hash [1]; ++i) {
847 /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
848 if (!symbols [i].st_name)
850 hashc = elf_hash ((guint8*)strtab->data->str + symbols [i].st_name);
851 slot = hashc % hash [0];
852 /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
854 chain [i] = bucket [slot];
865 reloc_symbols (MonoAotCompile *acfg, ElfSymbol *symbols, struct ElfSectHeader *sheaders, ElfStrTable *strtab, gboolean dynamic)
873 for (section = acfg->sections; section; section = section->next) {
876 symbols [i].st_value = sheaders [section->shidx].sh_addr;
880 for (i = 1; i < SECT_NUM; ++i) {
881 symbols [i].st_value = sheaders [i].sh_addr;
884 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
887 if (dynamic && !symbol->is_global)
889 section = symbol->section;
890 lab = g_hash_table_lookup (acfg->labels, symbol->name);
891 offset = lab->offset;
892 if (section->parent) {
893 symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
895 symbols [i].st_value = sheaders [section->shidx].sh_addr + offset;
900 symbols [i].st_value = sheaders [SECT_BSS].sh_addr;
903 symbols [i].st_value = sheaders [SECT_DATA].sh_addr + sheaders [SECT_DATA].sh_size;
906 symbols [i].st_value = sheaders [SECT_BSS].sh_addr + sheaders [SECT_BSS].sh_size;
911 resolve_relocations (MonoAotCompile *acfg)
915 gsize end_val, start_val;
920 rr = g_new0 (ElfReloc, acfg->num_relocs);
923 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
924 end_val = get_label_addr (acfg, reloc->val1);
926 start_val = get_label_addr (acfg, reloc->val2);
927 } else if (reloc->val2_section) {
928 start_val = reloc->val2_offset;
929 if (reloc->val2_section->parent)
930 start_val += reloc->val2_section->parent->file_offset + reloc->val2_section->cur_offset;
932 start_val += reloc->val2_section->file_offset;
936 end_val = end_val - start_val + reloc->offset;
937 if (reloc->section->parent) {
938 data = reloc->section->parent->data;
939 data += reloc->section->cur_offset;
940 data += reloc->section_offset;
941 vaddr = reloc->section->parent->file_offset;
942 vaddr += reloc->section->cur_offset;
943 vaddr += reloc->section_offset;
945 data = reloc->section->data;
946 data += reloc->section_offset;
947 vaddr = reloc->section->file_offset;
948 vaddr += reloc->section_offset;
950 /* FIXME: little endian */
952 data [1] = end_val >> 8;
953 data [2] = end_val >> 16;
954 data [3] = end_val >> 24;
955 if (start_val == 0) {
957 rr [i].value = 8; /* FIXME: 386_RELATIVE */
959 g_assert (i <= acfg->num_relocs);
966 emit_writeout (MonoAotCompile *acfg)
968 char *outfile_name, *tmp_outfile_name;
970 struct ElfHeader header;
971 struct ElfProgHeader progh [3];
972 struct ElfSectHeader secth [SECT_NUM];
974 ElfStrTable str_table = {NULL, NULL};
975 ElfStrTable sh_str_table = {NULL, NULL};
976 ElfStrTable dyn_str_table = {NULL, NULL};
977 BinSection* sections [6];
978 BinSection *text_section = NULL, *data_section = NULL, *bss_section = NULL;
981 ElfDynamic dynamic [14];
983 int i, num_sections, file_offset, virt_offset, size, num_symtab;
986 if (acfg->aot_opts.outfile)
987 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
989 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
991 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
993 unlink (tmp_outfile_name);
994 file = fopen (tmp_outfile_name, "w");
997 /* Section headers */
998 memset (§h, 0, sizeof (secth));
999 memset (&dynamic, 0, sizeof (dynamic));
1000 memset (&header, 0, sizeof (header));
1002 for (i = 1; i < SECT_NUM; ++i) {
1003 secth [i].sh_name = str_table_add (&sh_str_table, section_names [i]);
1004 secth [i].sh_type = section_type [i];
1005 secth [i].sh_link = section_link [i];
1006 secth [i].sh_addralign = section_align [i];
1007 secth [i].sh_flags = section_flags [i];
1008 secth [i].sh_entsize = section_esize [i];
1010 secth [SECT_DYNSYM].sh_info = 4;
1011 secth [SECT_SYMTAB].sh_info = 20;
1013 num_sections = collect_sections (acfg, secth, sections, 6);
1014 hash = build_hash (acfg, num_sections, &dyn_str_table);
1015 num_symtab = hash [1]; /* FIXME */
1016 g_print ("num_sections: %d\n", num_sections);
1017 g_print ("dynsym: %d, dynstr size: %d\n", hash [1], dyn_str_table.data->len);
1018 for (i = 0; i < num_sections; ++i) {
1019 g_print ("section %s, size: %d, %x\n", sections [i]->name, sections [i]->cur_offset, sections [i]->cur_offset);
1022 /* at this point we know where in the file the first segment sections go */
1023 dynsym = collect_syms (acfg, hash, &dyn_str_table, NULL, NULL);
1024 num_local_syms = hash [1];
1025 symtab = collect_syms (acfg, NULL, &str_table, secth, &num_local_syms);
1027 for (i = 0; i < num_sections; ++i) {
1028 if (sections [i]->shidx == SECT_TEXT) {
1029 text_section = sections [i];
1030 } else if (sections [i]->shidx == SECT_DATA) {
1031 data_section = sections [i];
1032 } else if (sections [i]->shidx == SECT_BSS) {
1033 bss_section = sections [i];
1037 file_offset = virt_offset = sizeof (header) + sizeof (progh);
1038 secth [SECT_HASH].sh_addr = secth [SECT_HASH].sh_offset = file_offset;
1039 size = sizeof (int) * (2 + hash [0] + hash [1]);
1040 virt_offset = (file_offset += size);
1041 secth [SECT_HASH].sh_size = size;
1042 secth [SECT_DYNSYM].sh_addr = secth [SECT_DYNSYM].sh_offset = file_offset;
1043 size = sizeof (ElfSymbol) * hash [1];
1044 virt_offset = (file_offset += size);
1045 secth [SECT_DYNSYM].sh_size = size;
1046 secth [SECT_DYNSTR].sh_addr = secth [SECT_DYNSTR].sh_offset = file_offset;
1047 size = dyn_str_table.data->len;
1048 virt_offset = (file_offset += size);
1049 secth [SECT_DYNSTR].sh_size = size;
1051 file_offset &= ~(4-1);
1052 secth [SECT_REL_DYN].sh_addr = secth [SECT_REL_DYN].sh_offset = file_offset;
1053 size = sizeof (ElfReloc) * acfg->num_relocs;
1054 secth [SECT_REL_DYN].sh_size = size;
1055 virt_offset = (file_offset += size);
1056 secth [SECT_REL_DYN].sh_size = size;
1057 file_offset += 4096-1;
1058 file_offset &= ~(4096-1);
1059 virt_offset = file_offset;
1060 secth [SECT_TEXT].sh_addr = secth [SECT_TEXT].sh_offset = file_offset;
1061 size = text_section->cur_offset;
1062 secth [SECT_TEXT].sh_size = size;
1063 file_offset += size;
1065 file_offset &= ~(4-1);
1066 virt_offset = file_offset;
1067 /* .dynamic, .got.plt, .data, .bss here */
1068 secth [SECT_DYNAMIC].sh_addr = virt_offset;
1069 secth [SECT_DYNAMIC].sh_offset = file_offset;
1070 size = sizeof (dynamic);
1071 secth [SECT_DYNAMIC].sh_size = size;
1074 file_offset += size;
1075 virt_offset += size;
1076 secth [SECT_GOT_PLT].sh_addr = virt_offset;
1077 secth [SECT_GOT_PLT].sh_offset = file_offset;
1079 secth [SECT_GOT_PLT].sh_size = size;
1082 file_offset += size;
1083 virt_offset += size;
1084 secth [SECT_DATA].sh_addr = virt_offset;
1085 secth [SECT_DATA].sh_offset = file_offset;
1086 size = data_section->cur_offset;
1087 secth [SECT_DATA].sh_size = size;
1090 file_offset += size;
1091 virt_offset += size;
1092 secth [SECT_BSS].sh_addr = virt_offset;
1093 secth [SECT_BSS].sh_offset = file_offset;
1094 size = bss_section->cur_offset;
1095 secth [SECT_BSS].sh_size = size;
1097 /* virtual doesn't matter anymore */
1098 secth [SECT_SHSTRTAB].sh_offset = file_offset;
1099 size = sh_str_table.data->len;
1100 secth [SECT_SHSTRTAB].sh_size = size;
1103 file_offset += size;
1104 secth [SECT_SYMTAB].sh_offset = file_offset;
1105 size = sizeof (ElfSymbol) * num_local_syms;
1106 secth [SECT_SYMTAB].sh_size = size;
1107 file_offset += size;
1108 secth [SECT_STRTAB].sh_offset = file_offset;
1109 size = str_table.data->len;
1110 secth [SECT_STRTAB].sh_size = size;
1111 file_offset += size;
1113 file_offset &= ~(4-1);
1115 text_section->file_offset = secth [SECT_TEXT].sh_offset;
1116 data_section->file_offset = secth [SECT_DATA].sh_offset;
1117 bss_section->file_offset = secth [SECT_BSS].sh_offset;
1119 header.e_ident [0] = 0x7f; header.e_ident [1] = 'E';
1120 header.e_ident [2] = 'L'; header.e_ident [3] = 'F';
1121 header.e_ident [4] = SIZEOF_VOID_P == 4? 1: 2;
1122 header.e_ident [5] = 1; /* FIXME: little endian, bigendian is 2 */
1123 header.e_ident [6] = 1; /* version */
1124 header.e_ident [7] = 0; /* FIXME: */
1125 header.e_ident [8] = 0; /* FIXME: */
1126 for (i = 9; i < 16; ++i)
1127 header.e_ident [i] = 0;
1129 header.e_type = 3; /* shared library */
1130 header.e_machine = 3; /* FIXME: 386 */
1131 header.e_version = 1; /* FIXME: */
1133 header.e_phoff = sizeof (header);
1134 header.e_ehsize = sizeof (header);
1135 header.e_phentsize = sizeof (struct ElfProgHeader);
1137 header.e_entry = secth [SECT_TEXT].sh_addr;
1138 header.e_shstrndx = 10;
1139 header.e_shentsize = sizeof (struct ElfSectHeader);
1140 header.e_shnum = SECT_NUM;
1141 header.e_shoff = file_offset;
1145 dynamic [i].d_tag = DYN_HASH;
1146 dynamic [i].d_val = secth [SECT_HASH].sh_offset;
1148 dynamic [i].d_tag = DYN_STRTAB;
1149 dynamic [i].d_val = secth [SECT_DYNSTR].sh_offset;
1151 dynamic [i].d_tag = DYN_SYMTAB;
1152 dynamic [i].d_val = secth [SECT_DYNSYM].sh_offset;
1154 dynamic [i].d_tag = DYN_STRSZ;
1155 dynamic [i].d_val = dyn_str_table.data->len;
1157 dynamic [i].d_tag = DYN_SYMENT;
1158 dynamic [i].d_val = sizeof (ElfSymbol);
1160 dynamic [i].d_tag = DYN_REL;
1161 dynamic [i].d_val = secth [SECT_REL_DYN].sh_offset;
1163 dynamic [i].d_tag = DYN_RELSZ;
1164 dynamic [i].d_val = secth [SECT_REL_DYN].sh_size;
1166 dynamic [i].d_tag = DYN_RELENT;
1167 dynamic [i].d_val = sizeof (ElfReloc);
1169 dynamic [i].d_tag = DYN_RELCOUNT;
1170 dynamic [i].d_val = acfg->num_relocs;
1173 /* Program header */
1174 memset (&progh, 0, sizeof (progh));
1175 progh [0].p_type = 1; /* LOAD */
1176 progh [0].p_filesz = progh [0].p_memsz = secth [SECT_DYNAMIC].sh_offset;
1177 progh [0].p_align = 4096;
1178 progh [0].p_flags = 5;
1180 progh [1].p_type = 1;
1181 progh [1].p_offset = secth [SECT_DYNAMIC].sh_offset;
1182 progh [1].p_vaddr = progh [1].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1183 progh [1].p_filesz = secth [SECT_BSS].sh_offset - secth [SECT_DYNAMIC].sh_offset;
1184 progh [1].p_memsz = secth [SECT_BSS].sh_addr + secth [SECT_BSS].sh_size - secth [SECT_DYNAMIC].sh_addr;
1185 progh [1].p_align = 4096;
1186 progh [1].p_flags = 6;
1188 progh [2].p_type = 2; /* DYNAMIC */
1189 progh [2].p_offset = secth [SECT_DYNAMIC].sh_offset;
1190 progh [2].p_vaddr = progh [2].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1191 progh [2].p_filesz = progh [2].p_memsz = secth [SECT_DYNAMIC].sh_size;
1192 progh [2].p_align = 4;
1193 progh [2].p_flags = 6;
1195 reloc_symbols (acfg, dynsym, secth, &dyn_str_table, TRUE);
1196 reloc_symbols (acfg, symtab, secth, &str_table, FALSE);
1197 relocs = resolve_relocations (acfg);
1199 fwrite (&header, sizeof (header), 1, file);
1200 fwrite (&progh, sizeof (progh), 1, file);
1201 fwrite (hash, sizeof (int) * (hash [0] + hash [1] + 2), 1, file);
1202 fwrite (dynsym, sizeof (ElfSymbol) * hash [1], 1, file);
1203 fwrite (dyn_str_table.data->str, dyn_str_table.data->len, 1, file);
1205 fseek (file, secth [SECT_REL_DYN].sh_offset, SEEK_SET);
1206 fwrite (relocs, sizeof (ElfReloc), acfg->num_relocs, file);
1208 fseek (file, secth [SECT_TEXT].sh_offset, SEEK_SET);
1209 /* write .text, .data, .bss sections */
1210 fwrite (text_section->data, text_section->cur_offset, 1, file);
1213 fwrite (dynamic, sizeof (dynamic), 1, file);
1215 size = secth [SECT_DYNAMIC].sh_addr;
1216 fwrite (&size, sizeof (size), 1, file);
1217 fseek (file, secth [SECT_DATA].sh_offset, SEEK_SET);
1218 fwrite (data_section->data, data_section->cur_offset, 1, file);
1220 fseek (file, secth [SECT_SHSTRTAB].sh_offset, SEEK_SET);
1221 fwrite (sh_str_table.data->str, sh_str_table.data->len, 1, file);
1222 fseek (file, secth [SECT_SYMTAB].sh_offset, SEEK_SET);
1223 fwrite (symtab, sizeof (ElfSymbol) * num_local_syms, 1, file);
1224 fseek (file, secth [SECT_STRTAB].sh_offset, SEEK_SET);
1225 fwrite (str_table.data->str, str_table.data->len, 1, file);
1226 /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1227 /*g_assert (file_offset >= ftell (file));*/
1228 fseek (file, file_offset, SEEK_SET);
1229 fwrite (§h, sizeof (secth), 1, file);
1231 rename (tmp_outfile_name, outfile_name);
1233 g_free (tmp_outfile_name);
1234 g_free (outfile_name);
1239 #endif /* USE_ELF_WRITER */
1244 emit_start (MonoAotCompile *acfg)
1246 int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
1247 acfg->fp = fdopen (i, "w+");
1248 g_assert (acfg->fp);
1252 emit_unset_mode (MonoAotCompile *acfg)
1254 if (acfg->mode == EMIT_NONE)
1256 fprintf (acfg->fp, "\n");
1257 acfg->mode = EMIT_NONE;
1261 emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
1263 emit_unset_mode (acfg);
1264 #if defined(PLATFORM_WIN32)
1265 fprintf (acfg->fp, ".section %s\n", section_name);
1266 #elif defined(sparc) || defined(__arm__)
1267 /* For solaris as, GNU as should accept the same */
1268 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1269 #elif defined(__ppc__) && defined(__MACH__)
1270 /* This needs to be made more precise on mach. */
1271 fprintf (acfg->fp, "%s\n", subsection_index == 0 ? ".text" : ".data");
1273 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1278 emit_symbol_type (MonoAotCompile *acfg, const char *name, gboolean func)
1287 emit_unset_mode (acfg);
1288 #if defined(sparc) || defined(__arm__)
1289 fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
1290 #elif defined(PLATFORM_WIN32)
1292 #elif !(defined(__ppc__) && defined(__MACH__))
1293 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1294 #elif defined(__x86_64__) || defined(__i386__)
1295 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1300 emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
1302 emit_unset_mode (acfg);
1303 #if (defined(__ppc__) && defined(__MACH__)) || defined(PLATFORM_WIN32)
1304 // mach-o always uses a '_' prefix.
1305 fprintf (acfg->fp, "\t.globl _%s\n", name);
1307 fprintf (acfg->fp, "\t.globl %s\n", name);
1310 emit_symbol_type (acfg, name, func);
1314 emit_label (MonoAotCompile *acfg, const char *name)
1316 emit_unset_mode (acfg);
1317 #if (defined(__ppc__) && defined(__MACH__)) || defined(PLATFORM_WIN32)
1318 // mach-o always uses a '_' prefix.
1319 fprintf (acfg->fp, "_%s:\n", name);
1321 fprintf (acfg->fp, "%s:\n", name);
1324 #if defined(PLATFORM_WIN32)
1325 /* Emit a normal label too */
1326 fprintf (acfg->fp, "%s:\n", name);
1331 emit_string (MonoAotCompile *acfg, const char *value)
1333 emit_unset_mode (acfg);
1334 fprintf (acfg->fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
1338 emit_line (MonoAotCompile *acfg)
1340 emit_unset_mode (acfg);
1341 fprintf (acfg->fp, "\n");
1345 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
1347 emit_unset_mode (acfg);
1348 emit_section_change (acfg, ".text", 1);
1349 emit_global (acfg, name, FALSE);
1350 emit_label (acfg, name);
1351 emit_string (acfg, value);
1355 emit_alignment (MonoAotCompile *acfg, int size)
1357 emit_unset_mode (acfg);
1358 #if defined(__arm__)
1359 fprintf (acfg->fp, "\t.align %d\n", ilog2 (size));
1360 #elif defined(__ppc__) && defined(__MACH__)
1361 // the mach-o assembler specifies alignments as powers of 2.
1362 fprintf (acfg->fp, "\t.align %d\t; ilog2\n", ilog2(size));
1363 #elif defined(__powerpc__)
1364 /* ignore on linux/ppc */
1366 fprintf (acfg->fp, "\t.align %d\n", size);
1371 emit_pointer (MonoAotCompile *acfg, const char *target)
1373 emit_unset_mode (acfg);
1374 emit_alignment (acfg, sizeof (gpointer));
1375 #if defined(__x86_64__)
1376 fprintf (acfg->fp, "\t.quad %s\n", target);
1377 #elif defined(sparc) && SIZEOF_VOID_P == 8
1378 fprintf (acfg->fp, "\t.xword %s\n", target);
1380 fprintf (acfg->fp, "\t.long %s\n", target);
1385 emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
1388 if (acfg->mode != EMIT_BYTE) {
1389 acfg->mode = EMIT_BYTE;
1390 acfg->col_count = 0;
1392 for (i = 0; i < size; ++i, ++acfg->col_count) {
1393 if ((acfg->col_count % 32) == 0)
1394 fprintf (acfg->fp, "\n\t.byte ");
1396 fprintf (acfg->fp, ", ");
1397 fprintf (acfg->fp, "0x%x", buf [i]);
1402 emit_int16 (MonoAotCompile *acfg, int value)
1404 if (acfg->mode != EMIT_WORD) {
1405 acfg->mode = EMIT_WORD;
1406 acfg->col_count = 0;
1408 if ((acfg->col_count++ % 8) == 0)
1409 #if defined(__arm__)
1410 /* FIXME: Use .hword on other archs as well */
1411 fprintf (acfg->fp, "\n\t.hword ");
1413 fprintf (acfg->fp, "\n\t.word ");
1416 fprintf (acfg->fp, ", ");
1417 fprintf (acfg->fp, "%d", value);
1421 emit_int32 (MonoAotCompile *acfg, int value)
1423 if (acfg->mode != EMIT_LONG) {
1424 acfg->mode = EMIT_LONG;
1425 acfg->col_count = 0;
1427 if ((acfg->col_count++ % 8) == 0)
1428 fprintf (acfg->fp, "\n\t.long ");
1430 fprintf (acfg->fp, ", ");
1431 fprintf (acfg->fp, "%d", value);
1435 emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
1437 if (acfg->mode != EMIT_LONG) {
1438 acfg->mode = EMIT_LONG;
1439 acfg->col_count = 0;
1441 if ((acfg->col_count++ % 8) == 0)
1442 fprintf (acfg->fp, "\n\t.long ");
1444 fprintf (acfg->fp, ", ");
1446 fprintf (acfg->fp, "%s - %s %c %d", end, start, offset < 0? ' ': '+', offset);
1448 fprintf (acfg->fp, "%s - %s", end, start);
1452 emit_zero_bytes (MonoAotCompile *acfg, int num)
1454 emit_unset_mode (acfg);
1455 fprintf (acfg->fp, "\t.skip %d\n", num);
1459 emit_writeout (MonoAotCompile *acfg)
1461 char *command, *objfile;
1462 char *outfile_name, *tmp_outfile_name;
1466 #if defined(__x86_64__)
1467 #define AS_OPTIONS "--64"
1468 #elif defined(sparc) && SIZEOF_VOID_P == 8
1469 #define AS_OPTIONS "-xarch=v9"
1471 #define AS_OPTIONS ""
1473 command = g_strdup_printf ("as %s %s -o %s.o", AS_OPTIONS, acfg->tmpfname, acfg->tmpfname);
1474 printf ("Executing the native assembler: %s\n", command);
1475 if (system (command) != 0) {
1482 if (acfg->aot_opts.outfile)
1483 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
1485 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
1487 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
1490 command = g_strdup_printf ("ld -shared -G -o %s %s.o", outfile_name, acfg->tmpfname);
1491 #elif defined(__ppc__) && defined(__MACH__)
1492 command = g_strdup_printf ("gcc -dynamiclib -o %s %s.o", outfile_name, acfg->tmpfname);
1493 #elif defined(PLATFORM_WIN32)
1494 command = g_strdup_printf ("gcc -shared --dll -mno-cygwin -o %s %s.o", outfile_name, acfg->tmpfname);
1496 command = g_strdup_printf ("ld -shared -o %s %s.o", outfile_name, acfg->tmpfname);
1498 printf ("Executing the native linker: %s\n", command);
1499 if (system (command) != 0) {
1500 g_free (tmp_outfile_name);
1501 g_free (outfile_name);
1507 objfile = g_strdup_printf ("%s.o", acfg->tmpfname);
1510 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, SHARED_EXT);
1511 printf ("Stripping the binary: %s\n", com);
1515 rename (tmp_outfile_name, outfile_name);
1517 g_free (tmp_outfile_name);
1518 g_free (outfile_name);
1520 if (acfg->aot_opts.save_temps)
1521 printf ("Retained input file.\n");
1523 unlink (acfg->tmpfname);
1528 #endif /* ASM_WRITER */
1531 emit_byte (MonoAotCompile *acfg, guint8 val)
1533 emit_bytes (acfg, &val, 1);
1537 mono_get_field_token (MonoClassField *field)
1539 MonoClass *klass = field->parent;
1542 for (i = 0; i < klass->field.count; ++i) {
1543 if (field == &klass->fields [i])
1544 return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
1547 g_assert_not_reached ();
1552 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
1556 //printf ("ENCODE: %d 0x%x.\n", value, value);
1559 * Same encoding as the one used in the metadata, extended to handle values
1560 * greater than 0x1fffffff.
1562 if ((value >= 0) && (value <= 127))
1564 else if ((value >= 0) && (value <= 16383)) {
1565 p [0] = 0x80 | (value >> 8);
1566 p [1] = value & 0xff;
1568 } else if ((value >= 0) && (value <= 0x1fffffff)) {
1569 p [0] = (value >> 24) | 0xc0;
1570 p [1] = (value >> 16) & 0xff;
1571 p [2] = (value >> 8) & 0xff;
1572 p [3] = value & 0xff;
1577 p [1] = (value >> 24) & 0xff;
1578 p [2] = (value >> 16) & 0xff;
1579 p [3] = (value >> 8) & 0xff;
1580 p [4] = value & 0xff;
1588 get_image_index (MonoAotCompile *cfg, MonoImage *image)
1592 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
1596 index = g_hash_table_size (cfg->image_hash);
1597 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
1598 g_ptr_array_add (cfg->image_table, image);
1604 find_typespec_for_class (MonoAotCompile *acfg, MonoClass *klass)
1607 MonoClass *k = NULL;
1609 /* FIXME: Search referenced images as well */
1610 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
1611 k = mono_class_get_full (acfg->image, MONO_TOKEN_TYPE_SPEC | (i + 1), NULL);
1616 if (i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows)
1617 return MONO_TOKEN_TYPE_SPEC | (i + 1);
1623 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
1625 if (klass->generic_class) {
1627 g_assert (klass->type_token);
1629 /* Find a typespec for a class if possible */
1630 token = find_typespec_for_class (acfg, klass);
1632 encode_value (token, buf, &buf);
1633 encode_value (get_image_index (acfg, acfg->image), buf, &buf);
1635 MonoClass *gclass = klass->generic_class->container_class;
1636 MonoGenericInst *inst = klass->generic_class->context.class_inst;
1639 /* Encode it ourselves */
1641 encode_value (MONO_TOKEN_TYPE_SPEC, buf, &buf);
1642 encode_klass_ref (acfg, gclass, buf, &buf);
1643 encode_value (inst->type_argc, buf, &buf);
1644 for (i = 0; i < inst->type_argc; ++i)
1645 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), buf, &buf);
1647 } else if (klass->type_token) {
1648 g_assert (mono_metadata_token_code (klass->type_token) == MONO_TOKEN_TYPE_DEF);
1649 encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, buf, &buf);
1650 encode_value (get_image_index (acfg, klass->image), buf, &buf);
1653 g_assert (klass->rank > 0);
1654 encode_value (MONO_TOKEN_TYPE_DEF, buf, &buf);
1655 encode_value (get_image_index (acfg, klass->image), buf, &buf);
1656 encode_value (klass->rank, buf, &buf);
1657 encode_klass_ref (acfg, klass->element_class, buf, &buf);
1663 encode_field_info (MonoAotCompile *cfg, MonoClassField *field, guint8 *buf, guint8 **endbuf)
1665 guint32 token = mono_get_field_token (field);
1667 encode_klass_ref (cfg, field->parent, buf, &buf);
1668 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
1669 encode_value (token - MONO_TOKEN_FIELD_DEF, buf, &buf);
1675 find_methodspec_for_method (MonoAotCompile *acfg, MonoMethod *method)
1678 MonoMethod *m = NULL;
1680 /* FIXME: Search referenced images as well */
1681 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHODSPEC].rows; ++i) {
1682 /* Since we don't compile generic methods, the context is empty */
1683 m = mono_get_method_full (acfg->image, MONO_TOKEN_METHOD_SPEC | (i + 1), NULL, NULL);
1688 g_assert (i < acfg->image->tables [MONO_TABLE_METHODSPEC].rows);
1690 return MONO_TOKEN_METHOD_SPEC | (i + 1);
1695 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf)
1697 guint32 image_index = get_image_index (acfg, method->klass->image);
1698 guint32 token = method->token;
1699 MonoJumpInfoToken *ji;
1701 g_assert (image_index < 255);
1703 if (mono_method_signature (method)->is_inflated) {
1705 * This is a generic method, find the original token which referenced it and
1708 /* This doesn't work for some reason */
1710 image_index = get_image_index (acfg, acfg->image);
1711 g_assert (image_index < 255);
1712 token = find_methodspec_for_method (acfg, method);
1714 /* Obtain the token from information recorded by the JIT */
1715 ji = g_hash_table_lookup (acfg->token_info_hash, method);
1717 image_index = get_image_index (acfg, ji->image);
1718 g_assert (image_index < 255);
1722 encode_value ((255 << 24), buf, &buf);
1723 encode_value (image_index, buf, &buf);
1724 encode_value (token, buf, &buf);
1725 } else if (token == 0) {
1726 /* This might be a method of a constructed type like int[,].Set */
1727 /* Obtain the token from information recorded by the JIT */
1728 ji = g_hash_table_lookup (acfg->token_info_hash, method);
1730 image_index = get_image_index (acfg, ji->image);
1731 g_assert (image_index < 255);
1735 encode_value ((255 << 24), buf, &buf);
1736 encode_value (image_index, buf, &buf);
1737 encode_value (token, buf, &buf);
1739 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
1740 encode_value ((image_index << 24) | mono_metadata_token_index (token), buf, &buf);
1746 compare_patches (gconstpointer a, gconstpointer b)
1750 i = (*(MonoJumpInfo**)a)->ip.i;
1751 j = (*(MonoJumpInfo**)b)->ip.i;
1763 get_plt_index (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
1767 GHashTable *hash = acfg->patch_to_plt_offset;
1769 switch (patch_info->type) {
1770 case MONO_PATCH_INFO_METHOD:
1771 case MONO_PATCH_INFO_WRAPPER:
1772 case MONO_PATCH_INFO_INTERNAL_METHOD:
1773 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
1774 case MONO_PATCH_INFO_CLASS_INIT: {
1775 MonoJumpInfo *new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
1776 gpointer patch_id = NULL;
1778 /* First check for an existing patch */
1779 switch (patch_info->type) {
1780 case MONO_PATCH_INFO_METHOD:
1781 patch_id = patch_info->data.method;
1783 case MONO_PATCH_INFO_INTERNAL_METHOD:
1784 patch_id = (gpointer)patch_info->data.name;
1786 case MONO_PATCH_INFO_CLASS_INIT:
1787 patch_id = patch_info->data.klass;
1789 case MONO_PATCH_INFO_WRAPPER:
1790 hash = acfg->patch_to_plt_offset_wrapper [patch_info->data.method->wrapper_type];
1792 acfg->patch_to_plt_offset_wrapper [patch_info->data.method->wrapper_type] = g_hash_table_new (NULL, NULL);
1793 hash = acfg->patch_to_plt_offset_wrapper [patch_info->data.method->wrapper_type];
1795 patch_id = patch_info->data.method;
1797 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
1798 /* Each addr should only occur once */
1801 g_assert_not_reached ();
1805 idx = GPOINTER_TO_UINT (g_hash_table_lookup (hash, patch_id));
1809 g_hash_table_insert (hash, patch_id, GUINT_TO_POINTER (acfg->plt_offset));
1813 res = acfg->plt_offset;
1814 g_hash_table_insert (acfg->plt_offset_to_patch, GUINT_TO_POINTER (acfg->plt_offset), new_ji);
1815 acfg->plt_offset ++;
1818 /* Nullify the patch */
1819 patch_info->type = MONO_PATCH_INFO_NONE;
1831 * Returns the offset of the GOT slot where the runtime object resulting from resolving
1832 * JI could be found if it exists, otherwise allocates a new one.
1835 get_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
1839 got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_shared_got_offset, ji));
1841 return got_offset - 1;
1843 got_offset = acfg->got_offset;
1844 acfg->got_offset ++;
1846 acfg->stats.got_slots ++;
1847 acfg->stats.got_slot_types [ji->type] ++;
1853 get_shared_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
1858 if (!g_hash_table_lookup (acfg->patch_to_shared_got_offset, ji)) {
1859 got_offset = get_got_offset (acfg, ji);
1860 copy = mono_patch_info_dup_mp (acfg->mempool, ji);
1861 g_hash_table_insert (acfg->patch_to_shared_got_offset, copy, GUINT_TO_POINTER (got_offset + 1));
1862 g_ptr_array_add (acfg->shared_patches, copy);
1865 return get_got_offset (acfg, ji);
1868 /* Add a method to the list of methods which need to be emitted */
1870 add_method_with_index (MonoAotCompile *acfg, MonoMethod *method, int index)
1872 if (!g_hash_table_lookup (acfg->method_indexes, method)) {
1873 g_ptr_array_add (acfg->methods, method);
1874 g_hash_table_insert (acfg->method_indexes, method, GUINT_TO_POINTER (index + 1));
1879 get_method_index (MonoAotCompile *acfg, MonoMethod *method)
1881 int index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
1889 add_method (MonoAotCompile *acfg, MonoMethod *method)
1891 int index = acfg->method_index;
1893 add_method_with_index (acfg, method, index);
1895 /* FIXME: Fix quadratic behavior */
1896 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (index));
1898 acfg->method_index ++;
1904 add_jit_icall_wrapper (gpointer key, gpointer value, gpointer user_data)
1906 MonoAotCompile *acfg = user_data;
1907 MonoJitICallInfo *callinfo = value;
1908 MonoMethod *wrapper;
1914 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
1915 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
1918 add_method (acfg, wrapper);
1922 get_runtime_invoke_sig (MonoMethodSignature *sig)
1924 MonoMethodBuilder *mb;
1927 mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
1928 m = mono_mb_create_method (mb, sig, 16);
1929 return mono_marshal_get_runtime_invoke (m);
1933 get_runtime_invoke (const char *signature)
1935 return get_runtime_invoke_sig (mono_create_icall_signature (signature));
1939 add_wrappers (MonoAotCompile *acfg)
1943 MonoMethodSignature *csig;
1946 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
1947 * so there is only one wrapper of a given type, or inlining their contents into their
1951 /* FIXME: Collect these automatically */
1953 /* Runtime invoke wrappers */
1955 /* void runtime-invoke () [.cctor] */
1956 add_method (acfg, get_runtime_invoke ("void"));
1958 /* void runtime-invoke (string) [exception ctor] */
1959 add_method (acfg, get_runtime_invoke ("void string"));
1961 /* void runtime-invoke (string, string) [exception ctor] */
1962 add_method (acfg, get_runtime_invoke ("void string string"));
1964 /* string runtime-invoke () [Exception.ToString ()] */
1965 add_method (acfg, get_runtime_invoke ("string"));
1967 /* void runtime-invoke (string, Exception) [exception ctor] */
1968 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
1970 csig->ret = &mono_defaults.void_class->byval_arg;
1971 csig->params [0] = &mono_defaults.string_class->byval_arg;
1972 csig->params [1] = &mono_defaults.exception_class->byval_arg;
1973 add_method (acfg, get_runtime_invoke_sig (csig));
1975 /* Assembly runtime-invoke (string, bool) [DoAssemblyResolve] */
1976 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
1978 csig->ret = &(mono_class_from_name (
1979 mono_defaults.corlib, "System.Reflection", "Assembly"))->byval_arg;
1980 csig->params [0] = &mono_defaults.string_class->byval_arg;
1981 csig->params [1] = &mono_defaults.boolean_class->byval_arg;
1982 add_method (acfg, get_runtime_invoke_sig (csig));
1984 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1986 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1988 method = mono_get_method (acfg->image, token, NULL);
1990 if (!strcmp (method->name, "Main"))
1991 add_method (acfg, mono_marshal_get_runtime_invoke (method));
1994 /* JIT icall wrappers */
1995 /* FIXME: locking */
1996 g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
1998 /* Managed Allocators */
1999 nallocators = mono_gc_get_managed_allocator_types ();
2000 for (i = 0; i < nallocators; ++i) {
2001 m = mono_gc_get_managed_allocator_by_type (i);
2003 add_method (acfg, m);
2007 add_method (acfg, mono_marshal_get_stelemref ());
2009 /* remoting-invoke wrappers */
2010 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2012 MonoMethodSignature *sig;
2013 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
2015 method = mono_get_method (acfg->image, token, NULL);
2017 sig = mono_method_signature (method);
2019 if (sig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
2020 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
2021 m = mono_marshal_get_remoting_invoke_with_check (method);
2023 add_method (acfg, m);
2029 emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, guint32 code_len, MonoJumpInfo *relocs, gboolean got_only)
2031 int i, pindex, start_index, method_index;
2033 MonoJumpInfo *patch_info;
2034 MonoMethodHeader *header;
2039 header = mono_method_get_header (method);
2041 method_index = get_method_index (acfg, method);
2044 /* Collect and sort relocations */
2045 patches = g_ptr_array_new ();
2046 for (patch_info = relocs; patch_info; patch_info = patch_info->next)
2047 g_ptr_array_add (patches, patch_info);
2048 g_ptr_array_sort (patches, compare_patches);
2051 for (i = 0; i < code_len; i++) {
2053 for (pindex = start_index; pindex < patches->len; ++pindex) {
2054 patch_info = g_ptr_array_index (patches, pindex);
2055 if (patch_info->ip.i >= i)
2059 #ifdef MONO_ARCH_AOT_SUPPORTED
2061 if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
2062 start_index = pindex;
2064 switch (patch_info->type) {
2065 case MONO_PATCH_INFO_NONE:
2067 case MONO_PATCH_INFO_GOT_OFFSET: {
2068 guint32 offset = mono_arch_get_patch_offset (code + i);
2069 emit_bytes (acfg, code + i, offset);
2070 emit_symbol_diff (acfg, "got", ".", offset);
2072 i += offset + 4 - 1;
2078 char *direct_call_target;
2080 if (!is_got_patch (patch_info->type))
2084 * If this patch is a call, try emitting a direct call instead of
2085 * through a PLT entry. This is possible if the called method is in
2086 * the same assembly and requires no initialization.
2088 direct_call_target = NULL;
2089 if (!got_only && (patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == method->klass->image)) {
2090 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
2092 if (!callee_cfg->has_got_slots && (callee_cfg->method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)) {
2093 //printf ("DIRECT: %s %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (callee_cfg->method, TRUE));
2094 direct_call_target = g_strdup_printf (".Lm_%x", get_method_index (acfg, callee_cfg->method));
2095 patch_info->type = MONO_PATCH_INFO_NONE;
2096 acfg->stats.direct_calls ++;
2100 acfg->stats.all_calls ++;
2103 if (!got_only && !direct_call_target) {
2104 plt_index = get_plt_index (acfg, patch_info);
2105 if (plt_index != -1) {
2106 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
2107 direct_call_target = g_strdup_printf (".Lp_%d", plt_index);
2111 if (direct_call_target) {
2112 #if defined(__i386__) || defined(__x86_64__)
2113 g_assert (code [i] == 0xe8);
2114 /* Need to make sure this is exactly 5 bytes long */
2115 emit_byte (acfg, '\xe8');
2116 emit_symbol_diff (acfg, direct_call_target, ".", -4);
2118 #elif defined(__arm__)
2119 #ifdef USE_BIN_WRITER
2120 /* FIXME: Can't encode this using the current symbol writer functions */
2121 g_assert_not_reached ();
2123 emit_unset_mode (acfg);
2124 fprintf (acfg->fp, "bl %s\n", direct_call_target);
2129 got_slot = get_got_offset (acfg, patch_info);
2131 emit_bytes (acfg, code + i, mono_arch_get_patch_offset (code + i));
2133 emit_symbol_diff (acfg, "got", ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
2134 #elif defined(__i386__)
2135 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
2136 #elif defined(__arm__)
2137 emit_symbol_diff (acfg, "got", ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
2139 g_assert_not_reached ();
2142 i += mono_arch_get_patch_offset (code + i) + 4 - 1;
2148 #endif /* MONO_ARCH_AOT_SUPPORTED */
2151 emit_bytes (acfg, code + i, 1);
2156 emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
2162 int func_alignment = 16;
2163 MonoMethodHeader *header;
2165 method = cfg->method;
2166 code = cfg->native_code;
2167 header = mono_method_get_header (method);
2169 method_index = get_method_index (acfg, method);
2171 /* Make the labels local */
2172 symbol = g_strdup_printf (".Lm_%x", method_index);
2174 emit_alignment (acfg, func_alignment);
2175 emit_label (acfg, symbol);
2176 if (acfg->aot_opts.write_symbols)
2177 emit_global (acfg, symbol, TRUE);
2179 if (cfg->verbose_level > 0)
2180 g_print ("Method %s emitted as %s\n", mono_method_full_name (method, TRUE), symbol);
2182 acfg->stats.code_size += cfg->code_len;
2184 acfg->method_got_offsets [method_index] = acfg->got_offset;
2186 emit_and_reloc_code (acfg, method, code, cfg->code_len, cfg->patch_info, FALSE);
2194 * Encode PATCH_INFO into its disk representation. If SHARED is true, encode some types
2195 * of patches by allocating a GOT entry for them, and encode the GOT offset instead.
2198 encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf, gboolean shared)
2202 switch (patch_info->type) {
2203 case MONO_PATCH_INFO_NONE:
2205 case MONO_PATCH_INFO_IMAGE:
2206 encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
2208 case MONO_PATCH_INFO_METHOD_REL:
2209 encode_value ((gint)patch_info->data.offset, p, &p);
2211 case MONO_PATCH_INFO_SWITCH: {
2212 gpointer *table = (gpointer *)patch_info->data.table->table;
2215 encode_value (patch_info->data.table->table_size, p, &p);
2216 for (k = 0; k < patch_info->data.table->table_size; k++)
2217 encode_value ((int)(gssize)table [k], p, &p);
2220 case MONO_PATCH_INFO_METHODCONST:
2221 case MONO_PATCH_INFO_METHOD:
2222 case MONO_PATCH_INFO_METHOD_JUMP:
2223 case MONO_PATCH_INFO_ICALL_ADDR:
2224 encode_method_ref (acfg, patch_info->data.method, p, &p);
2226 case MONO_PATCH_INFO_INTERNAL_METHOD:
2227 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2228 guint32 len = strlen (patch_info->data.name);
2230 encode_value (len, p, &p);
2232 memcpy (p, patch_info->data.name, len);
2237 case MONO_PATCH_INFO_LDSTR: {
2238 guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
2239 guint32 token = patch_info->data.token->token;
2240 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
2241 encode_value (image_index, p, &p);
2242 encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
2245 case MONO_PATCH_INFO_RVA:
2246 case MONO_PATCH_INFO_DECLSEC:
2247 case MONO_PATCH_INFO_LDTOKEN:
2248 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2250 guint32 offset = get_got_offset (acfg, patch_info);
2251 encode_value (offset, p, &p);
2253 encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
2254 encode_value (patch_info->data.token->token, p, &p);
2257 case MONO_PATCH_INFO_EXC_NAME: {
2258 MonoClass *ex_class;
2261 mono_class_from_name (mono_defaults.exception_class->image,
2262 "System", patch_info->data.target);
2263 g_assert (ex_class);
2264 encode_klass_ref (acfg, ex_class, p, &p);
2267 case MONO_PATCH_INFO_R4:
2268 encode_value (*((guint32 *)patch_info->data.target), p, &p);
2270 case MONO_PATCH_INFO_R8:
2271 encode_value (*((guint32 *)patch_info->data.target), p, &p);
2272 encode_value (*(((guint32 *)patch_info->data.target) + 1), p, &p);
2274 case MONO_PATCH_INFO_VTABLE:
2275 case MONO_PATCH_INFO_CLASS:
2276 case MONO_PATCH_INFO_IID:
2277 case MONO_PATCH_INFO_ADJUSTED_IID:
2279 guint32 offset = get_got_offset (acfg, patch_info);
2280 encode_value (offset, p, &p);
2282 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
2285 case MONO_PATCH_INFO_CLASS_INIT:
2286 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2287 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
2289 case MONO_PATCH_INFO_FIELD:
2290 case MONO_PATCH_INFO_SFLDA:
2292 guint32 offset = get_got_offset (acfg, patch_info);
2293 encode_value (offset, p, &p);
2295 encode_field_info (acfg, patch_info->data.field, p, &p);
2298 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2300 case MONO_PATCH_INFO_WRAPPER:
2301 encode_value (patch_info->data.method->wrapper_type, p, &p);
2303 switch (patch_info->data.method->wrapper_type) {
2304 case MONO_WRAPPER_REMOTING_INVOKE:
2305 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
2306 case MONO_WRAPPER_XDOMAIN_INVOKE: {
2308 guint32 image_index;
2311 m = mono_marshal_method_from_wrapper (patch_info->data.method);
2312 image_index = get_image_index (acfg, m->klass->image);
2314 g_assert (image_index < 256);
2315 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
2317 encode_value ((image_index << 24) + (mono_metadata_token_index (token)), p, &p);
2320 case MONO_WRAPPER_PROXY_ISINST:
2321 case MONO_WRAPPER_LDFLD:
2322 case MONO_WRAPPER_LDFLDA:
2323 case MONO_WRAPPER_STFLD:
2324 case MONO_WRAPPER_ISINST: {
2325 MonoClass *proxy_class = (MonoClass*)mono_marshal_method_from_wrapper (patch_info->data.method);
2326 encode_klass_ref (acfg, proxy_class, p, &p);
2329 case MONO_WRAPPER_LDFLD_REMOTE:
2330 case MONO_WRAPPER_STFLD_REMOTE:
2332 case MONO_WRAPPER_ALLOC: {
2333 int alloc_type = mono_gc_get_managed_allocator_type (patch_info->data.method);
2334 g_assert (alloc_type != -1);
2335 encode_value (alloc_type, p, &p);
2338 case MONO_WRAPPER_STELEMREF:
2341 g_assert_not_reached ();
2345 g_warning ("unable to handle jump info %d", patch_info->type);
2346 g_assert_not_reached ();
2353 encode_patch_list (MonoAotCompile *acfg, GPtrArray *patches, int n_patches, int first_got_offset, guint8 *buf, guint8 **endbuf)
2356 guint32 last_offset, j, pindex;
2357 MonoJumpInfo *patch_info;
2359 encode_value (n_patches, p, &p);
2362 encode_value (first_got_offset, p, &p);
2364 /* First encode the type+position table */
2367 for (pindex = 0; pindex < patches->len; ++pindex) {
2369 patch_info = g_ptr_array_index (patches, pindex);
2371 if (patch_info->type == MONO_PATCH_INFO_NONE)
2376 //printf ("T: %d O: %d.\n", patch_info->type, patch_info->ip.i);
2377 offset = patch_info->ip.i - last_offset;
2378 last_offset = patch_info->ip.i;
2380 /* Only the type is needed */
2381 *p = patch_info->type;
2385 /* Then encode the other info */
2386 for (pindex = 0; pindex < patches->len; ++pindex) {
2387 patch_info = g_ptr_array_index (patches, pindex);
2389 encode_patch (acfg, patch_info, p, &p, TRUE);
2396 emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
2400 int pindex, buf_size, n_patches;
2404 MonoJumpInfo *patch_info;
2405 MonoMethodHeader *header;
2406 guint32 method_index;
2408 guint32 first_got_offset;
2410 method = cfg->method;
2411 code = cfg->native_code;
2412 header = mono_method_get_header (method);
2414 method_index = get_method_index (acfg, method);
2416 /* Make the labels local */
2417 symbol = g_strdup_printf (".Lm_%x_p", method_index);
2419 /* Sort relocations */
2420 patches = g_ptr_array_new ();
2421 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
2422 g_ptr_array_add (patches, patch_info);
2423 g_ptr_array_sort (patches, compare_patches);
2425 first_got_offset = acfg->method_got_offsets [method_index];
2427 /**********************/
2428 /* Encode method info */
2429 /**********************/
2431 buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
2432 p = buf = g_malloc (buf_size);
2434 if (mono_class_get_cctor (method->klass))
2435 encode_klass_ref (acfg, method->klass, p, &p);
2437 /* Not needed when loading the method */
2438 encode_value (0, p, &p);
2441 if (cfg->opt & MONO_OPT_SHARED) {
2442 encode_value (g_list_length (cfg->ldstr_list), p, &p);
2443 for (l = cfg->ldstr_list; l; l = l->next) {
2444 encode_value ((long)l->data, p, &p);
2448 /* Used only in shared mode */
2449 g_assert (!cfg->ldstr_list);
2452 for (pindex = 0; pindex < patches->len; ++pindex) {
2453 patch_info = g_ptr_array_index (patches, pindex);
2455 if ((patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
2456 (patch_info->type == MONO_PATCH_INFO_NONE)) {
2457 patch_info->type = MONO_PATCH_INFO_NONE;
2462 if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
2463 /* Stored in a GOT slot initialized at module load time */
2464 patch_info->type = MONO_PATCH_INFO_NONE;
2468 if ((patch_info->type == MONO_PATCH_INFO_METHOD) ||
2469 (patch_info->type == MONO_PATCH_INFO_INTERNAL_METHOD) ||
2470 (patch_info->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) ||
2471 (patch_info->type == MONO_PATCH_INFO_WRAPPER) ||
2472 (patch_info->type == MONO_PATCH_INFO_CLASS_INIT)) {
2473 /* Calls are made through the PLT */
2474 patch_info->type = MONO_PATCH_INFO_NONE;
2482 g_assert (cfg->has_got_slots);
2484 encode_patch_list (acfg, patches, n_patches, first_got_offset, p, &p);
2486 acfg->stats.info_size += p - buf;
2488 /* Emit method info */
2490 emit_label (acfg, symbol);
2492 g_assert (p - buf < buf_size);
2493 emit_bytes (acfg, buf, p - buf);
2500 emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
2503 int k, buf_size, method_index;
2504 guint32 debug_info_size;
2507 MonoMethodHeader *header;
2508 guint8 *p, *buf, *debug_info;
2510 method = cfg->method;
2511 code = cfg->native_code;
2512 header = mono_method_get_header (method);
2514 method_index = get_method_index (acfg, method);
2516 /* Make the labels local */
2517 symbol = g_strdup_printf (".Le_%x_p", method_index);
2519 mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
2521 buf_size = header->num_clauses * 256 + debug_info_size + 128;
2522 p = buf = g_malloc (buf_size);
2524 encode_value (cfg->code_len, p, &p);
2525 encode_value (cfg->used_int_regs, p, &p);
2527 /* Exception table */
2528 if (header->num_clauses) {
2529 MonoJitInfo *jinfo = cfg->jit_info;
2531 for (k = 0; k < header->num_clauses; ++k) {
2532 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
2534 encode_value (ei->exvar_offset, p, &p);
2536 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
2537 encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
2539 encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
2540 encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
2541 encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
2545 g_assert (debug_info_size < buf_size);
2547 encode_value (debug_info_size, p, &p);
2548 if (debug_info_size) {
2549 memcpy (p, debug_info, debug_info_size);
2550 p += debug_info_size;
2551 g_free (debug_info);
2554 acfg->stats.ex_info_size += p - buf;
2558 emit_label (acfg, symbol);
2560 g_assert (p - buf < buf_size);
2561 emit_bytes (acfg, buf, p - buf);
2568 emit_klass_info (MonoAotCompile *acfg, guint32 token)
2570 MonoClass *klass = mono_class_get (acfg->image, token);
2574 gboolean no_special_static, cant_encode;
2576 buf_size = 10240 + (klass->vtable_size * 16);
2577 p = buf = g_malloc (buf_size);
2581 mono_class_init (klass);
2583 mono_class_setup_vtable (klass);
2586 * Emit all the information which is required for creating vtables so
2587 * the runtime does not need to create the MonoMethod structures which
2588 * take up a lot of space.
2591 no_special_static = !mono_class_has_special_static_fields (klass);
2593 /* Check whenever we have enough info to encode the vtable */
2594 cant_encode = FALSE;
2595 for (i = 0; i < klass->vtable_size; ++i) {
2596 MonoMethod *cm = klass->vtable [i];
2598 if (cm && mono_method_signature (cm)->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, cm))
2602 if (klass->generic_container || cant_encode) {
2603 encode_value (-1, p, &p);
2605 encode_value (klass->vtable_size, p, &p);
2606 encode_value ((no_special_static << 7) | (klass->has_static_refs << 6) | (klass->has_references << 5) | ((klass->blittable << 4) | (klass->nested_classes ? 1 : 0) << 3) | (klass->has_cctor << 2) | (klass->has_finalize << 1) | klass->ghcimpl, p, &p);
2607 if (klass->has_cctor)
2608 encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
2609 if (klass->has_finalize)
2610 encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
2612 encode_value (klass->instance_size, p, &p);
2613 encode_value (mono_class_data_size (klass), p, &p);
2614 encode_value (klass->packing_size, p, &p);
2615 encode_value (klass->min_align, p, &p);
2617 for (i = 0; i < klass->vtable_size; ++i) {
2618 MonoMethod *cm = klass->vtable [i];
2621 encode_method_ref (acfg, cm, p, &p);
2623 encode_value (0, p, &p);
2627 acfg->stats.class_info_size += p - buf;
2630 label = g_strdup_printf (".LK_I_%x", token - MONO_TOKEN_TYPE_DEF - 1);
2631 emit_label (acfg, label);
2633 g_assert (p - buf < buf_size);
2634 emit_bytes (acfg, buf, p - buf);
2639 * Calls made from AOTed code are routed through a table of jumps similar to the
2640 * ELF PLT (Program Linkage Table). The differences are the following:
2641 * - the ELF PLT entries make an indirect jump though the GOT so they expect the
2642 * GOT pointer to be in EBX. We want to avoid this, so our table contains direct
2643 * jumps. This means the jumps need to be patched when the address of the callee is
2644 * known. Initially the PLT entries jump to code which transfers control to the
2645 * AOT runtime through the first PLT entry.
2648 emit_plt (MonoAotCompile *acfg)
2653 guint32 *plt_info_offsets;
2656 * Encode info need to resolve PLT entries.
2658 buf_size = acfg->plt_offset * 128;
2659 p = buf = g_malloc (buf_size);
2661 plt_info_offsets = g_new0 (guint32, acfg->plt_offset);
2663 for (i = 1; i < acfg->plt_offset; ++i) {
2664 MonoJumpInfo *patch_info = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
2666 plt_info_offsets [i] = p - buf;
2667 encode_value (patch_info->type, p, &p);
2668 encode_patch (acfg, patch_info, p, &p, FALSE);
2672 symbol = g_strdup_printf ("plt");
2674 /* This section will be made read-write by the AOT loader */
2675 emit_section_change (acfg, ".text", 0);
2676 emit_global (acfg, symbol, TRUE);
2677 emit_alignment (acfg, PAGESIZE);
2678 emit_label (acfg, symbol);
2680 #if defined(USE_BIN_WRITER) && defined(__arm__)
2682 g_assert_not_reached ();
2686 * The first plt entry is used to transfer code to the AOT loader.
2688 emit_label (acfg, ".Lp_0");
2689 #if defined(__i386__)
2690 /* It is filled up during loading by the AOT loader. */
2691 emit_zero_bytes (acfg, 16);
2692 #elif defined(__x86_64__)
2693 /* This should be exactly 16 bytes long */
2694 /* jmpq *<offset>(%rip) */
2695 emit_byte (acfg, '\xff');
2696 emit_byte (acfg, '\x25');
2697 emit_symbol_diff (acfg, "plt_jump_table", ".", -4);
2698 emit_zero_bytes (acfg, 10);
2699 #elif defined(__arm__)
2700 /* This is 8 bytes long, init_plt () depends on this */
2701 emit_unset_mode (acfg);
2702 fprintf (acfg->fp, "\tldr pc, [pc, #-4]\n");
2703 /* This is filled up during loading by the AOT loader */
2704 fprintf (acfg->fp, "\t.word 0\n");
2706 g_assert_not_reached ();
2709 for (i = 1; i < acfg->plt_offset; ++i) {
2712 label = g_strdup_printf (".Lp_%d", i);
2713 emit_label (acfg, label);
2715 #if defined(__i386__)
2716 /* Need to make sure this is 5 bytes long */
2717 emit_byte (acfg, '\xe9');
2718 label = g_strdup_printf (".Lpd_%d", i);
2719 emit_symbol_diff (acfg, label, ".", -4);
2721 #elif defined(__x86_64__)
2723 * We can't emit jumps because they are 32 bits only so they can't be patched.
2724 * So we emit a jump table instead whose entries are patched by the AOT loader to
2725 * point to .Lpd entries. ELF stores these in the GOT too, but we don't, since
2726 * methods with GOT entries can't be called directly.
2727 * We also emit the default PLT code here since the PLT code will not be patched.
2728 * An x86_64 plt entry is 16 bytes long, init_plt () depends on this.
2730 /* jmpq *<offset>(%rip) */
2731 emit_byte (acfg, '\xff');
2732 emit_byte (acfg, '\x25');
2733 emit_symbol_diff (acfg, "plt_jump_table", ".", (i * sizeof (gpointer)) -4);
2734 /* mov <plt info offset>, %eax */
2735 emit_byte (acfg, '\xb8');
2736 emit_int32 (acfg, plt_info_offsets [i]);
2738 emit_byte (acfg, '\xe9');
2739 emit_symbol_diff (acfg, ".Lp_0", ".", -4);
2740 #elif defined(__arm__)
2742 * Emit an indirect call since branch displacements are limited to 24 bits on
2743 * ARM. Put the jump table entries inline since offsets are even smaller on
2746 * - optimize OP_AOTCONST implementation
2747 * - optimize the PLT entries
2748 * - optimize SWITCH AOT implementation
2749 * - implement IMT support
2751 /* This is 8 bytes long, init_plt () depends on this */
2752 emit_unset_mode (acfg);
2753 fprintf (acfg->fp, "\tldr pc, [pc, #-4]\n");
2754 /* This is filled up during loading by the AOT loader */
2755 fprintf (acfg->fp, "\t.word 0\n");
2757 g_assert_not_reached ();
2761 symbol = g_strdup_printf ("plt_end");
2762 emit_global (acfg, symbol, TRUE);
2763 emit_label (acfg, symbol);
2766 * Emit the default targets for the PLT entries separately since these will not
2767 * be modified at runtime.
2769 for (i = 1; i < acfg->plt_offset; ++i) {
2772 label = g_strdup_printf (".Lpd_%d", i);
2773 emit_label (acfg, label);
2776 /* Put the offset into the register expected by mono_aot_plt_trampoline */
2777 #if defined(__i386__)
2778 /* movl $const, %eax */
2779 emit_byte (acfg, '\xb8');
2780 emit_int32 (acfg, plt_info_offsets [i]);
2782 emit_byte (acfg, '\xe9');
2783 emit_symbol_diff (acfg, ".Lp_0", ".", -4);
2784 #elif defined(__x86_64__)
2785 /* Emitted along with the PLT entries since they will not be patched */
2786 #elif defined(__arm__)
2787 /* This is 12 bytes long, init_plt () depends on this */
2788 emit_unset_mode (acfg);
2789 fprintf (acfg->fp, "\tldr ip, [pc, #0]\n");
2790 fprintf (acfg->fp, "\tb .Lp_0\n");
2791 fprintf (acfg->fp, "\t.word %d\n", plt_info_offsets [i]);
2793 g_assert_not_reached ();
2798 symbol = g_strdup_printf ("plt_info");
2799 emit_global (acfg, symbol, FALSE);
2800 emit_label (acfg, symbol);
2802 g_assert (p - buf < buf_size);
2803 emit_bytes (acfg, buf, p - buf);
2806 symbol = g_strdup_printf ("plt_jump_table_addr");
2807 emit_section_change (acfg, ".data", 0);
2808 emit_global (acfg, symbol, FALSE);
2809 emit_alignment (acfg, 8);
2810 emit_label (acfg, symbol);
2811 emit_pointer (acfg, "plt_jump_table");
2813 symbol = g_strdup_printf ("plt_jump_table_size");
2814 emit_section_change (acfg, ".data", 0);
2815 emit_global (acfg, symbol, FALSE);
2816 emit_alignment (acfg, 8);
2817 emit_label (acfg, symbol);
2818 emit_symbol_diff (acfg, "plt_jump_table_end", "plt_jump_table", 0);
2820 /* Don't make this a global so accesses don't need relocations */
2821 symbol = g_strdup_printf ("plt_jump_table");
2822 emit_section_change (acfg, ".bss", 0);
2823 emit_label (acfg, symbol);
2826 emit_zero_bytes (acfg, (int)(acfg->plt_offset * sizeof (gpointer)));
2829 symbol = g_strdup_printf ("plt_jump_table_end");
2830 emit_label (acfg, symbol);
2834 emit_named_code (MonoAotCompile *acfg, const char *name, guint8 *code,
2835 guint32 code_size, int got_offset, MonoJumpInfo *ji)
2839 MonoJumpInfo *patch_info;
2845 symbol = g_strdup_printf ("%s", name);
2847 emit_section_change (acfg, ".text", 0);
2848 emit_global (acfg, symbol, TRUE);
2849 emit_label (acfg, symbol);
2853 * The code should access everything through the GOT, so we pass
2856 emit_and_reloc_code (acfg, NULL, code, code_size, ji, TRUE);
2860 /* Sort relocations */
2861 patches = g_ptr_array_new ();
2862 for (patch_info = ji; patch_info; patch_info = patch_info->next)
2863 g_ptr_array_add (patches, patch_info);
2864 g_ptr_array_sort (patches, compare_patches);
2866 buf_size = patches->len * 128 + 128;
2867 buf = g_malloc (buf_size);
2870 encode_patch_list (acfg, patches, patches->len, got_offset, p, &p);
2871 g_assert (p - buf < buf_size);
2873 symbol = g_strdup_printf ("%s_p", name);
2875 emit_section_change (acfg, ".text", 0);
2876 emit_global (acfg, symbol, TRUE);
2877 emit_label (acfg, symbol);
2880 emit_bytes (acfg, buf, p - buf);
2884 * When running in aot-only mode, we can't create trampolines at runtime, so we create
2885 * a few, and save them in the AOT file. Normal trampolines embed their argument as a
2886 * literal inside the trampoline code, we can't do that here, so instead we embed an offset
2887 * which needs to be added to the trampoline address to get the address of the GOT slot
2888 * which contains the argument value.
2889 * The generated trampolines jump to the generic trampolines using another GOT slot, which
2890 * will be setup by the AOT loader to point to the generic trampoline code of the given
2892 * Currently, we only emit trampolines into the mscorlib AOT image.
2895 emit_trampolines (MonoAotCompile *acfg)
2898 int tramp_type, i, offset;
2899 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
2905 if (!acfg->aot_opts.full_aot)
2908 g_assert (acfg->image->assembly);
2909 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") != 0)
2912 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
2914 * Emit the generic trampolines.
2916 * We could save some code by treating the generic trampolines as a wrapper
2917 * method, but that approach has its own complexities, so we choose the simpler
2920 for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
2921 code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, TRUE);
2923 /* Emit trampoline code */
2925 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2927 emit_named_code (acfg, symbol, code, code_size, acfg->got_offset, ji);
2932 code = mono_arch_get_nullified_class_init_trampoline (&code_size);
2933 emit_named_code (acfg, "nullified_class_init_trampoline", code, code_size, acfg->got_offset, NULL);
2935 /* Emit the exception related code pieces */
2936 code = mono_arch_get_restore_context_full (&code_size, &ji, TRUE);
2937 emit_named_code (acfg, "restore_context", code, code_size, acfg->got_offset, ji);
2938 code = mono_arch_get_call_filter_full (&code_size, &ji, TRUE);
2939 emit_named_code (acfg, "call_filter", code, code_size, acfg->got_offset, ji);
2940 code = mono_arch_get_throw_exception_full (&code_size, &ji, TRUE);
2941 emit_named_code (acfg, "throw_exception", code, code_size, acfg->got_offset, ji);
2942 code = mono_arch_get_rethrow_exception_full (&code_size, &ji, TRUE);
2943 emit_named_code (acfg, "rethrow_exception", code, code_size, acfg->got_offset, ji);
2944 code = mono_arch_get_throw_exception_by_name_full (&code_size, &ji, TRUE);
2945 emit_named_code (acfg, "throw_exception_by_name", code, code_size, acfg->got_offset, ji);
2946 code = mono_arch_get_throw_corlib_exception_full (&code_size, &ji, TRUE);
2947 emit_named_code (acfg, "throw_corlib_exception", code, code_size, acfg->got_offset, ji);
2951 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for each
2955 /* Reserve some entries at the end of the GOT for our use */
2956 acfg->num_trampoline_got_entries = acfg->num_aot_trampolines * 2;
2958 symbol = g_strdup_printf ("trampolines");
2960 emit_section_change (acfg, ".text", 0);
2961 emit_global (acfg, symbol, TRUE);
2962 emit_label (acfg, symbol);
2964 for (i = 0; i < acfg->num_aot_trampolines; ++i) {
2965 offset = acfg->got_offset + (i * 2);
2967 #if defined(__x86_64__)
2968 /* This should be exactly 16 bytes long */
2969 /* It should work together with the generic trampoline code in tramp-amd64.c */
2970 /* call *<offset>(%rip) */
2971 emit_byte (acfg, '\x41');
2972 emit_byte (acfg, '\xff');
2973 emit_byte (acfg, '\x15');
2974 emit_symbol_diff (acfg, "got", ".", (offset * sizeof (gpointer)) - 4);
2975 /* This should be relative to the start of the trampoline */
2976 emit_symbol_diff (acfg, "got", ".", (offset * sizeof (gpointer)) - 4 + 19);
2977 emit_zero_bytes (acfg, 5);
2979 g_assert_not_reached ();
2983 symbol = g_strdup_printf ("trampolines_info");
2985 emit_section_change (acfg, ".text", 0);
2986 emit_global (acfg, symbol, TRUE);
2987 emit_alignment (acfg, PAGESIZE);
2988 emit_label (acfg, symbol);
2990 emit_int32 (acfg, acfg->num_aot_trampolines);
2991 emit_int32 (acfg, acfg->got_offset);
2995 str_begins_with (const char *str1, const char *str2)
2997 int len = strlen (str2);
2998 return strncmp (str1, str2, len) == 0;
3002 mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
3004 gchar **args, **ptr;
3006 memset (opts, 0, sizeof (*opts));
3008 args = g_strsplit (aot_options ? aot_options : "", ",", -1);
3009 for (ptr = args; ptr && *ptr; ptr ++) {
3010 const char *arg = *ptr;
3012 if (str_begins_with (arg, "outfile=")) {
3013 opts->outfile = g_strdup (arg + strlen ("outfile="));
3014 } else if (str_begins_with (arg, "save-temps")) {
3015 opts->save_temps = TRUE;
3016 } else if (str_begins_with (arg, "keep-temps")) {
3017 opts->save_temps = TRUE;
3018 } else if (str_begins_with (arg, "write-symbols")) {
3019 opts->write_symbols = TRUE;
3020 } else if (str_begins_with (arg, "metadata-only")) {
3021 opts->metadata_only = TRUE;
3022 } else if (str_begins_with (arg, "bind-to-runtime-version")) {
3023 opts->bind_to_runtime_version = TRUE;
3024 } else if (str_begins_with (arg, "full")) {
3025 opts->full_aot = TRUE;
3027 fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
3034 add_token_info_hash (gpointer key, gpointer value, gpointer user_data)
3036 MonoMethod *method = (MonoMethod*)key;
3037 MonoJumpInfoToken *ji = (MonoJumpInfoToken*)value;
3038 MonoJumpInfoToken *new_ji = g_new0 (MonoJumpInfoToken, 1);
3039 MonoAotCompile *acfg = user_data;
3041 new_ji->image = ji->image;
3042 new_ji->token = ji->token;
3043 g_hash_table_insert (acfg->token_info_hash, method, new_ji);
3047 compile_method (MonoAotCompile *acfg, MonoMethod *method)
3050 MonoJumpInfo *patch_info;
3054 if (acfg->aot_opts.metadata_only)
3057 index = get_method_index (acfg, method);
3059 /* fixme: maybe we can also precompile wrapper methods */
3060 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3061 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3062 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
3063 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
3067 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
3070 acfg->stats.mcount++;
3072 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
3074 * FIXME: Enabling this causes virtual-sync.exe to fail, since the trampoline
3075 * code can't determine that it needs to insert a sync wrapper in the AOT case.
3080 //acfg->opts &= ~MONO_OPT_GSHARED;
3082 if (!(acfg->opts & MONO_OPT_GSHARED)) {
3083 if (method->is_generic || method->klass->generic_container) {
3084 acfg->stats.genericcount ++;
3090 * Since these methods are the only ones which are compiled with
3091 * AOT support, and they are not used by runtime startup/shutdown code,
3092 * the runtime will not see AOT methods during AOT compilation,so it
3093 * does not need to support them by creating a fake GOT etc.
3095 cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), FALSE, TRUE, 0);
3096 if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
3097 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
3098 acfg->stats.genericcount ++;
3101 if (cfg->exception_type != MONO_EXCEPTION_NONE) {
3102 /* Let the exception happen at runtime */
3106 if (cfg->disable_aot) {
3107 //printf ("Skip (other): %s\n", mono_method_full_name (method, TRUE));
3108 acfg->stats.ocount++;
3109 mono_destroy_compile (cfg);
3113 /* Nullify patches which need no aot processing */
3114 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3115 switch (patch_info->type) {
3116 case MONO_PATCH_INFO_LABEL:
3117 case MONO_PATCH_INFO_BB:
3118 patch_info->type = MONO_PATCH_INFO_NONE;
3125 /* Collect method->token associations from the cfg */
3126 g_hash_table_foreach (cfg->token_info_hash, add_token_info_hash, acfg);
3129 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3130 switch (patch_info->type) {
3131 case MONO_PATCH_INFO_ABS:
3132 /* unable to handle this */
3133 //printf ("Skip (abs addr): %s %d\n", mono_method_full_name (method, TRUE), patch_info->type);
3142 acfg->stats.abscount++;
3143 mono_destroy_compile (cfg);
3148 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3149 if (patch_info->type == MONO_PATCH_INFO_METHOD_JUMP) {
3151 * FIXME: We can't handle this because mono_jit_compile_method_inner will try
3152 * to patch the AOT code when the target of the jump is compiled.
3160 acfg->stats.ocount++;
3161 mono_destroy_compile (cfg);
3165 /* some wrappers are very common */
3166 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3167 if (patch_info->type == MONO_PATCH_INFO_METHODCONST) {
3168 switch (patch_info->data.method->wrapper_type) {
3169 case MONO_WRAPPER_PROXY_ISINST:
3170 patch_info->type = MONO_PATCH_INFO_WRAPPER;
3174 if (patch_info->type == MONO_PATCH_INFO_METHOD) {
3175 switch (patch_info->data.method->wrapper_type) {
3176 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
3177 case MONO_WRAPPER_XDOMAIN_INVOKE:
3178 case MONO_WRAPPER_STFLD:
3179 case MONO_WRAPPER_LDFLD:
3180 case MONO_WRAPPER_LDFLDA:
3181 case MONO_WRAPPER_LDFLD_REMOTE:
3182 case MONO_WRAPPER_STFLD_REMOTE:
3183 case MONO_WRAPPER_STELEMREF:
3184 case MONO_WRAPPER_ISINST:
3185 case MONO_WRAPPER_PROXY_ISINST:
3186 case MONO_WRAPPER_ALLOC:
3187 case MONO_WRAPPER_REMOTING_INVOKE:
3188 patch_info->type = MONO_PATCH_INFO_WRAPPER;
3195 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3196 switch (patch_info->type) {
3197 case MONO_PATCH_INFO_METHOD:
3198 case MONO_PATCH_INFO_METHODCONST:
3199 if (patch_info->data.method->wrapper_type) {
3200 /* unable to handle this */
3201 //printf ("Skip (wrapper call): %s %d -> %s\n", mono_method_full_name (method, TRUE), patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
3205 if (!patch_info->data.method->token) {
3206 /* The method is part of a constructed type like Int[,].Set (). */
3207 if (!g_hash_table_lookup (acfg->token_info_hash, patch_info->data.method))
3210 if (patch_info->data.method->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, patch_info->data.method))
3211 /* FIXME: Can't encode these */
3214 case MONO_PATCH_INFO_VTABLE:
3215 case MONO_PATCH_INFO_CLASS_INIT:
3216 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
3217 case MONO_PATCH_INFO_CLASS:
3218 case MONO_PATCH_INFO_IID:
3219 case MONO_PATCH_INFO_ADJUSTED_IID:
3220 if (!patch_info->data.klass->type_token)
3221 if (!patch_info->data.klass->element_class->type_token && !(patch_info->data.klass->element_class->rank && patch_info->data.klass->element_class->element_class->type_token))
3230 acfg->stats.wrappercount++;
3231 mono_destroy_compile (cfg);
3235 /* Determine whenever the method has GOT slots */
3236 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3237 switch (patch_info->type) {
3238 case MONO_PATCH_INFO_GOT_OFFSET:
3239 case MONO_PATCH_INFO_NONE:
3240 case MONO_PATCH_INFO_METHOD:
3241 case MONO_PATCH_INFO_INTERNAL_METHOD:
3242 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
3243 case MONO_PATCH_INFO_WRAPPER:
3245 case MONO_PATCH_INFO_IMAGE:
3246 if (patch_info->data.image == acfg->image)
3247 /* Stored in GOT slot 0 */
3251 cfg->has_got_slots = TRUE;
3256 if (!cfg->has_got_slots)
3257 acfg->stats.methods_without_got_slots ++;
3259 /* Make a copy of the patch info which is in the mempool */
3261 MonoJumpInfo *patches = NULL, *patches_end = NULL;
3263 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3264 MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
3267 patches = new_patch_info;
3269 patches_end->next = new_patch_info;
3270 patches_end = new_patch_info;
3272 cfg->patch_info = patches;
3275 /* Free some fields used by cfg to conserve memory */
3276 mono_mempool_destroy (cfg->mempool);
3277 cfg->mempool = NULL;
3278 g_free (cfg->varinfo);
3279 cfg->varinfo = NULL;
3283 mono_regstate_free (cfg->rs);
3287 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
3289 acfg->cfgs [index] = cfg;
3291 g_hash_table_insert (acfg->method_to_cfg, cfg->method, cfg);
3293 acfg->stats.ccount++;
3297 load_profile_files (MonoAotCompile *acfg)
3301 int file_index, res, method_index, i;
3308 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);
3310 if (!g_file_test (tmp, G_FILE_TEST_IS_REGULAR))
3313 infile = fopen (tmp, "r");
3316 printf ("Using profile data file '%s'\n", tmp);
3320 res = fscanf (infile, "%32s\n", ver);
3321 if ((res != 1) || strcmp (ver, "#VER:1") != 0) {
3322 printf ("Profile file has wrong version or invalid.\n");
3328 res = fscanf (infile, "%d\n", &token);
3332 method_index = mono_metadata_token_index (token) - 1;
3334 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (method_index)))
3335 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (method_index));
3340 /* Add missing methods */
3342 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3343 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (i)))
3344 unordered = g_list_prepend (unordered, GUINT_TO_POINTER (i));
3346 unordered = g_list_reverse (unordered);
3347 if (acfg->method_order)
3348 g_list_last (acfg->method_order)->next = unordered;
3350 acfg->method_order = unordered;
3356 * Collect all patches which have shared GOT entries and alloc entries for them. The
3357 * rest will get entries allocated during emit_code ().
3360 alloc_got_slots (MonoAotCompile *acfg)
3366 /* Slot 0 is reserved for the address of the current assembly */
3367 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
3368 ji->type = MONO_PATCH_INFO_IMAGE;
3369 ji->data.image = acfg->image;
3371 get_shared_got_offset (acfg, ji);
3373 for (l = acfg->method_order; l != NULL; l = l->next) {
3374 i = GPOINTER_TO_UINT (l->data);
3376 if (acfg->cfgs [i]) {
3377 MonoCompile *cfg = acfg->cfgs [i];
3379 for (ji = cfg->patch_info; ji; ji = ji->next) {
3381 case MONO_PATCH_INFO_VTABLE:
3382 case MONO_PATCH_INFO_CLASS:
3383 case MONO_PATCH_INFO_IID:
3384 case MONO_PATCH_INFO_ADJUSTED_IID:
3385 case MONO_PATCH_INFO_FIELD:
3386 case MONO_PATCH_INFO_SFLDA:
3387 case MONO_PATCH_INFO_DECLSEC:
3388 case MONO_PATCH_INFO_LDTOKEN:
3389 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3390 case MONO_PATCH_INFO_RVA:
3391 get_shared_got_offset (acfg, ji);
3402 emit_code (MonoAotCompile *acfg)
3408 symbol = g_strdup_printf ("methods");
3409 emit_section_change (acfg, ".text", 0);
3410 emit_global (acfg, symbol, TRUE);
3411 emit_alignment (acfg, 8);
3412 emit_label (acfg, symbol);
3414 for (l = acfg->method_order; l != NULL; l = l->next) {
3415 i = GPOINTER_TO_UINT (l->data);
3418 emit_method_code (acfg, acfg->cfgs [i]);
3421 symbol = g_strdup_printf ("methods_end");
3422 emit_section_change (acfg, ".text", 0);
3423 emit_global (acfg, symbol, FALSE);
3424 emit_alignment (acfg, 8);
3425 emit_label (acfg, symbol);
3427 symbol = g_strdup_printf ("method_offsets");
3428 emit_section_change (acfg, ".text", 1);
3429 emit_global (acfg, symbol, FALSE);
3430 emit_alignment (acfg, 8);
3431 emit_label (acfg, symbol);
3433 for (i = 0; i < acfg->nmethods; ++i) {
3434 if (acfg->cfgs [i]) {
3435 symbol = g_strdup_printf (".Lm_%x", i);
3436 emit_symbol_diff (acfg, symbol, "methods", 0);
3438 emit_int32 (acfg, 0xffffffff);
3445 emit_info (MonoAotCompile *acfg)
3451 /* Emit method info */
3452 symbol = g_strdup_printf ("method_info");
3453 emit_section_change (acfg, ".text", 1);
3454 emit_global (acfg, symbol, FALSE);
3455 emit_alignment (acfg, 8);
3456 emit_label (acfg, symbol);
3458 /* To reduce size of generated assembly code */
3459 symbol = g_strdup_printf ("mi");
3460 emit_label (acfg, symbol);
3462 for (l = acfg->method_order; l != NULL; l = l->next) {
3463 i = GPOINTER_TO_UINT (l->data);
3466 emit_method_info (acfg, acfg->cfgs [i]);
3469 symbol = g_strdup_printf ("method_info_offsets");
3470 emit_section_change (acfg, ".text", 1);
3471 emit_global (acfg, symbol, FALSE);
3472 emit_alignment (acfg, 8);
3473 emit_label (acfg, symbol);
3475 for (i = 0; i < acfg->nmethods; ++i) {
3476 if (acfg->cfgs [i]) {
3477 symbol = g_strdup_printf (".Lm_%x_p", i);
3478 emit_symbol_diff (acfg, symbol, "mi", 0);
3480 emit_int32 (acfg, 0);
3487 emit_wrapper_info (MonoAotCompile *acfg)
3493 /* Emit method info */
3494 symbol = g_strdup_printf ("wrapper_info");
3495 emit_section_change (acfg, ".text", 1);
3496 emit_global (acfg, symbol, FALSE);
3497 emit_alignment (acfg, 8);
3498 emit_label (acfg, symbol);
3500 if (!acfg->aot_opts.full_aot)
3503 for (i = 0; i < acfg->nmethods; ++i) {
3504 MonoCompile *cfg = acfg->cfgs [i];
3506 if (!cfg || !cfg->method->wrapper_type)
3509 index = get_method_index (acfg, cfg->method);
3511 // FIXME: Optimize disk usage and lookup speed
3512 name = mono_method_full_name (cfg->method, TRUE);
3513 emit_string (acfg, name);
3514 emit_alignment (acfg, 4);
3515 emit_int32 (acfg, index);
3518 emit_byte (acfg, 0);
3524 emit_method_order (MonoAotCompile *acfg)
3530 symbol = g_strdup_printf ("method_order");
3531 emit_section_change (acfg, ".text", 1);
3532 emit_global (acfg, symbol, FALSE);
3533 emit_alignment (acfg, 8);
3534 emit_label (acfg, symbol);
3536 /* First emit an index table */
3539 for (l = acfg->method_order; l != NULL; l = l->next) {
3540 i = GPOINTER_TO_UINT (l->data);
3542 if (acfg->cfgs [i]) {
3543 if ((index % 1024) == 0) {
3544 emit_int32 (acfg, i);
3552 emit_int32 (acfg, 0xffffff);
3554 /* Then emit the whole method order */
3555 for (l = acfg->method_order; l != NULL; l = l->next) {
3556 i = GPOINTER_TO_UINT (l->data);
3558 if (acfg->cfgs [i]) {
3559 emit_int32 (acfg, i);
3564 symbol = g_strdup_printf ("method_order_end");
3565 emit_section_change (acfg, ".text", 1);
3566 emit_global (acfg, symbol, FALSE);
3567 emit_label (acfg, symbol);
3571 emit_exception_info (MonoAotCompile *acfg)
3576 symbol = g_strdup_printf ("ex_info");
3577 emit_section_change (acfg, ".text", 1);
3578 emit_global (acfg, symbol, FALSE);
3579 emit_alignment (acfg, 8);
3580 emit_label (acfg, symbol);
3582 /* To reduce size of generated assembly */
3583 symbol = g_strdup_printf ("ex");
3584 emit_label (acfg, symbol);
3586 for (i = 0; i < acfg->nmethods; ++i) {
3588 emit_exception_debug_info (acfg, acfg->cfgs [i]);
3591 symbol = g_strdup_printf ("ex_info_offsets");
3592 emit_section_change (acfg, ".text", 1);
3593 emit_global (acfg, symbol, FALSE);
3594 emit_alignment (acfg, 8);
3595 emit_label (acfg, symbol);
3597 for (i = 0; i < acfg->nmethods; ++i) {
3598 if (acfg->cfgs [i]) {
3599 symbol = g_strdup_printf (".Le_%x_p", i);
3600 emit_symbol_diff (acfg, symbol, "ex", 0);
3602 emit_int32 (acfg, 0);
3609 emit_class_info (MonoAotCompile *acfg)
3614 symbol = g_strdup_printf ("class_info");
3615 emit_section_change (acfg, ".text", 1);
3616 emit_global (acfg, symbol, FALSE);
3617 emit_alignment (acfg, 8);
3618 emit_label (acfg, symbol);
3620 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
3621 emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
3623 symbol = g_strdup_printf ("class_info_offsets");
3624 emit_section_change (acfg, ".text", 1);
3625 emit_global (acfg, symbol, FALSE);
3626 emit_alignment (acfg, 8);
3627 emit_label (acfg, symbol);
3629 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3630 symbol = g_strdup_printf (".LK_I_%x", i);
3631 emit_symbol_diff (acfg, symbol, "class_info", 0);
3636 typedef struct ClassNameTableEntry {
3637 guint32 token, index;
3638 struct ClassNameTableEntry *next;
3639 } ClassNameTableEntry;
3642 emit_class_name_table (MonoAotCompile *acfg)
3645 guint32 token, hash;
3650 ClassNameTableEntry *entry, *new_entry;
3653 * Construct a chained hash table for mapping class names to typedef tokens.
3655 table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
3656 table = g_ptr_array_sized_new (table_size);
3657 for (i = 0; i < table_size; ++i)
3658 g_ptr_array_add (table, NULL);
3659 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3660 token = MONO_TOKEN_TYPE_DEF | (i + 1);
3661 klass = mono_class_get (acfg->image, token);
3662 full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
3663 hash = g_str_hash (full_name) % table_size;
3666 /* FIXME: Allocate from the mempool */
3667 new_entry = g_new0 (ClassNameTableEntry, 1);
3668 new_entry->token = token;
3670 entry = g_ptr_array_index (table, hash);
3671 if (entry == NULL) {
3672 new_entry->index = hash;
3673 g_ptr_array_index (table, hash) = new_entry;
3676 entry = entry->next;
3678 entry->next = new_entry;
3679 new_entry->index = table->len;
3680 g_ptr_array_add (table, new_entry);
3684 /* Emit the table */
3685 symbol = g_strdup_printf ("class_name_table");
3686 emit_section_change (acfg, ".text", 0);
3687 emit_global (acfg, symbol, FALSE);
3688 emit_alignment (acfg, 8);
3689 emit_label (acfg, symbol);
3691 /* FIXME: Optimize memory usage */
3692 g_assert (table_size < 65000);
3693 emit_int16 (acfg, table_size);
3694 g_assert (table->len < 65000);
3695 for (i = 0; i < table->len; ++i) {
3696 ClassNameTableEntry *entry = g_ptr_array_index (table, i);
3698 if (entry == NULL) {
3699 emit_int16 (acfg, 0);
3700 emit_int16 (acfg, 0);
3702 emit_int16 (acfg, mono_metadata_token_index (entry->token));
3704 emit_int16 (acfg, entry->next->index);
3706 emit_int16 (acfg, 0);
3712 emit_image_table (MonoAotCompile *acfg)
3718 * The image table is small but referenced in a lot of places.
3719 * So we emit it at once, and reference its elements by an index.
3722 symbol = g_strdup_printf ("mono_image_table");
3723 emit_section_change (acfg, ".text", 1);
3724 emit_global (acfg, symbol, FALSE);
3725 emit_alignment (acfg, 8);
3726 emit_label (acfg, symbol);
3727 emit_int32 (acfg, acfg->image_table->len);
3728 for (i = 0; i < acfg->image_table->len; i++) {
3729 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
3730 MonoAssemblyName *aname = &image->assembly->aname;
3732 /* FIXME: Support multi-module assemblies */
3733 g_assert (image->assembly->image == image);
3735 emit_string (acfg, image->assembly_name);
3736 emit_string (acfg, image->guid);
3737 emit_string (acfg, aname->culture ? aname->culture : "");
3738 emit_string (acfg, (const char*)aname->public_key_token);
3740 emit_alignment (acfg, 8);
3741 emit_int32 (acfg, aname->flags);
3742 emit_int32 (acfg, aname->major);
3743 emit_int32 (acfg, aname->minor);
3744 emit_int32 (acfg, aname->build);
3745 emit_int32 (acfg, aname->revision);
3750 emit_got_info (MonoAotCompile *acfg)
3755 guint32 *got_info_offsets;
3759 * - optimize offsets table.
3760 * - reduce number of exported symbols.
3761 * - emit info for a klass only once.
3762 * - determine when a method uses a GOT slot which is guaranteed to be already
3764 * - clean up and document the code.
3765 * - use String.Empty in class libs.
3768 /* Encode info required to decode shared GOT entries */
3769 buf_size = acfg->shared_patches->len * 64;
3770 p = buf = mono_mempool_alloc (acfg->mempool, buf_size);
3771 got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->shared_patches->len * sizeof (guint32));
3772 for (i = 0; i < acfg->shared_patches->len; ++i) {
3773 MonoJumpInfo *ji = g_ptr_array_index (acfg->shared_patches, i);
3775 /* No need to encode the patch type */
3776 got_info_offsets [i] = p - buf;
3777 encode_patch (acfg, ji, p, &p, FALSE);
3780 g_assert (p - buf <= buf_size);
3782 acfg->stats.got_info_size = p - buf;
3784 /* Emit got_info table */
3785 symbol = g_strdup_printf ("got_info");
3786 emit_section_change (acfg, ".text", 1);
3787 emit_global (acfg, symbol, FALSE);
3788 emit_alignment (acfg, 8);
3789 emit_label (acfg, symbol);
3791 emit_bytes (acfg, buf, p - buf);
3793 /* Emit got_info_offsets table */
3794 symbol = g_strdup_printf ("got_info_offsets");
3795 emit_section_change (acfg, ".text", 1);
3796 emit_global (acfg, symbol, FALSE);
3797 emit_alignment (acfg, 8);
3798 emit_label (acfg, symbol);
3800 for (i = 0; i < acfg->shared_patches->len; ++i)
3801 emit_int32 (acfg, got_info_offsets [i]);
3803 acfg->stats.got_info_offsets_size = acfg->shared_patches->len * 4;
3807 emit_got (MonoAotCompile *acfg)
3811 /* Don't make GOT global so accesses to it don't need relocations */
3812 symbol = g_strdup_printf ("got");
3813 emit_section_change (acfg, ".bss", 1);
3814 emit_alignment (acfg, 8);
3815 emit_label (acfg, symbol);
3816 if ((acfg->got_offset + acfg->num_trampoline_got_entries) > 0)
3817 emit_zero_bytes (acfg, (int)((acfg->got_offset + acfg->num_trampoline_got_entries) * sizeof (gpointer)));
3819 symbol = g_strdup_printf ("got_addr");
3820 emit_section_change (acfg, ".data", 1);
3821 emit_global (acfg, symbol, FALSE);
3822 emit_alignment (acfg, 8);
3823 emit_label (acfg, symbol);
3824 emit_pointer (acfg, "got");
3826 symbol = g_strdup_printf ("got_size");
3827 emit_section_change (acfg, ".data", 1);
3828 emit_global (acfg, symbol, FALSE);
3829 emit_alignment (acfg, 8);
3830 emit_label (acfg, symbol);
3831 emit_int32 (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
3835 emit_globals (MonoAotCompile *acfg)
3839 emit_string_symbol (acfg, "mono_assembly_guid" , acfg->image->guid);
3841 emit_string_symbol (acfg, "mono_aot_version", MONO_AOT_FILE_VERSION);
3843 opts_str = g_strdup_printf ("%d", acfg->opts);
3844 emit_string_symbol (acfg, "mono_aot_opt_flags", opts_str);
3847 emit_string_symbol (acfg, "mono_aot_full_aot", acfg->aot_opts.full_aot ? "TRUE" : "FALSE");
3849 if (acfg->aot_opts.bind_to_runtime_version)
3850 emit_string_symbol (acfg, "mono_runtime_version", FULL_VERSION);
3852 emit_string_symbol (acfg, "mono_runtime_version", "");
3856 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
3858 MonoImage *image = ass->image;
3861 MonoAotCompile *acfg;
3863 printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
3865 acfg = g_new0 (MonoAotCompile, 1);
3866 acfg->methods = g_ptr_array_new ();
3867 acfg->method_indexes = g_hash_table_new (NULL, NULL);
3868 acfg->plt_offset_to_patch = g_hash_table_new (NULL, NULL);
3869 acfg->patch_to_plt_offset = g_hash_table_new (NULL, NULL);
3870 acfg->patch_to_plt_offset_wrapper = g_malloc0 (sizeof (GHashTable*) * 128);
3871 acfg->patch_to_shared_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
3872 acfg->shared_patches = g_ptr_array_new ();
3873 acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
3874 acfg->token_info_hash = g_hash_table_new (NULL, NULL);
3875 acfg->image_hash = g_hash_table_new (NULL, NULL);
3876 acfg->image_table = g_ptr_array_new ();
3877 acfg->image = image;
3879 acfg->mempool = mono_mempool_new ();
3881 mono_aot_parse_options (aot_options, &acfg->aot_opts);
3883 load_profile_files (acfg);
3887 acfg->num_aot_trampolines = acfg->aot_opts.full_aot ? 1024 : 0;
3889 acfg->method_index = 1;
3891 /* Collect methods */
3892 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
3894 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3896 method = mono_get_method (acfg->image, token, NULL);
3898 if (acfg->aot_opts.full_aot && method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
3899 /* Compile the wrapper instead */
3900 /* We do this here instead of add_wrappers () because it is easy to do it here */
3901 MonoMethod *wrapper = mono_marshal_get_native_wrapper (method, check_for_pending_exc);
3905 /* Since we add the normal methods first, their index will be equal to their zero based token index */
3906 add_method_with_index (acfg, method, i);
3907 acfg->method_index ++;
3910 if (acfg->aot_opts.full_aot)
3911 add_wrappers (acfg);
3913 acfg->nmethods = acfg->methods->len;
3914 acfg->cfgs = g_new0 (MonoCompile*, acfg->nmethods + 32);
3915 acfg->method_got_offsets = g_new0 (guint32, acfg->nmethods + 32);
3917 /* PLT offset 0 is reserved for the PLT trampoline */
3918 acfg->plt_offset = 1;
3920 /* Compile methods */
3921 for (i = 0; i < acfg->methods->len; ++i) {
3922 compile_method (acfg, g_ptr_array_index (acfg->methods, i));
3925 alloc_got_slots (acfg);
3931 emit_wrapper_info (acfg);
3933 emit_method_order (acfg);
3935 emit_trampolines (acfg);
3937 emit_class_name_table (acfg);
3939 emit_got_info (acfg);
3941 emit_exception_info (acfg);
3943 emit_class_info (acfg);
3947 emit_image_table (acfg);
3951 emit_globals (acfg);
3953 symbol = g_strdup_printf ("mem_end");
3954 emit_section_change (acfg, ".text", 1);
3955 emit_global (acfg, symbol, FALSE);
3956 emit_alignment (acfg, 8);
3957 emit_label (acfg, symbol);
3959 printf ("Code: %d Info: %d Ex Info: %d Class Info: %d PLT: %d GOT Info: %d GOT Info Offsets: %d GOT: %d\n", acfg->stats.code_size, acfg->stats.info_size, acfg->stats.ex_info_size, acfg->stats.class_info_size, acfg->plt_offset, acfg->stats.got_info_size, acfg->stats.got_info_offsets_size, (int)(acfg->got_offset * sizeof (gpointer)));
3961 res = emit_writeout (acfg);
3965 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);
3966 printf ("%d methods are generic (%d%%)\n", acfg->stats.genericcount, acfg->stats.mcount ? (acfg->stats.genericcount * 100) / acfg->stats.mcount : 100);
3967 printf ("%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
3968 printf ("%d methods contain wrapper references (%d%%)\n", acfg->stats.wrappercount, acfg->stats.mcount ? (acfg->stats.wrappercount * 100) / acfg->stats.mcount : 100);
3969 printf ("%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
3970 printf ("%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
3971 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);
3972 printf ("Direct calls: %d (%d%%)\n", acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
3974 printf ("GOT slot distribution:\n");
3975 for (i = 0; i < MONO_PATCH_INFO_NONE; ++i)
3976 if (acfg->stats.got_slot_types [i])
3977 printf ("\t%s: %d\n", get_patch_name (i), acfg->stats.got_slot_types [i]);
3987 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)