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);
1726 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
1727 encode_value ((image_index << 24) | mono_metadata_token_index (token), buf, &buf);
1733 compare_patches (gconstpointer a, gconstpointer b)
1737 i = (*(MonoJumpInfo**)a)->ip.i;
1738 j = (*(MonoJumpInfo**)b)->ip.i;
1750 get_plt_index (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
1754 GHashTable *hash = acfg->patch_to_plt_offset;
1756 switch (patch_info->type) {
1757 case MONO_PATCH_INFO_METHOD:
1758 case MONO_PATCH_INFO_WRAPPER:
1759 case MONO_PATCH_INFO_INTERNAL_METHOD:
1760 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
1761 case MONO_PATCH_INFO_CLASS_INIT: {
1762 MonoJumpInfo *new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
1763 gpointer patch_id = NULL;
1765 /* First check for an existing patch */
1766 switch (patch_info->type) {
1767 case MONO_PATCH_INFO_METHOD:
1768 patch_id = patch_info->data.method;
1770 case MONO_PATCH_INFO_INTERNAL_METHOD:
1771 patch_id = (gpointer)patch_info->data.name;
1773 case MONO_PATCH_INFO_CLASS_INIT:
1774 patch_id = patch_info->data.klass;
1776 case MONO_PATCH_INFO_WRAPPER:
1777 hash = acfg->patch_to_plt_offset_wrapper [patch_info->data.method->wrapper_type];
1779 acfg->patch_to_plt_offset_wrapper [patch_info->data.method->wrapper_type] = g_hash_table_new (NULL, NULL);
1780 hash = acfg->patch_to_plt_offset_wrapper [patch_info->data.method->wrapper_type];
1782 patch_id = patch_info->data.method;
1784 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
1785 /* Each addr should only occur once */
1788 g_assert_not_reached ();
1792 idx = GPOINTER_TO_UINT (g_hash_table_lookup (hash, patch_id));
1796 g_hash_table_insert (hash, patch_id, GUINT_TO_POINTER (acfg->plt_offset));
1800 res = acfg->plt_offset;
1801 g_hash_table_insert (acfg->plt_offset_to_patch, GUINT_TO_POINTER (acfg->plt_offset), new_ji);
1802 acfg->plt_offset ++;
1805 /* Nullify the patch */
1806 patch_info->type = MONO_PATCH_INFO_NONE;
1818 * Returns the offset of the GOT slot where the runtime object resulting from resolving
1819 * JI could be found if it exists, otherwise allocates a new one.
1822 get_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
1826 got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_shared_got_offset, ji));
1828 return got_offset - 1;
1830 got_offset = acfg->got_offset;
1831 acfg->got_offset ++;
1833 acfg->stats.got_slots ++;
1834 acfg->stats.got_slot_types [ji->type] ++;
1840 get_shared_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
1845 if (!g_hash_table_lookup (acfg->patch_to_shared_got_offset, ji)) {
1846 got_offset = get_got_offset (acfg, ji);
1847 copy = mono_patch_info_dup_mp (acfg->mempool, ji);
1848 g_hash_table_insert (acfg->patch_to_shared_got_offset, copy, GUINT_TO_POINTER (got_offset + 1));
1849 g_ptr_array_add (acfg->shared_patches, copy);
1852 return get_got_offset (acfg, ji);
1855 /* Add a method to the list of methods which need to be emitted */
1857 add_method_with_index (MonoAotCompile *acfg, MonoMethod *method, int index)
1859 if (!g_hash_table_lookup (acfg->method_indexes, method)) {
1860 g_ptr_array_add (acfg->methods, method);
1861 g_hash_table_insert (acfg->method_indexes, method, GUINT_TO_POINTER (index + 1));
1866 get_method_index (MonoAotCompile *acfg, MonoMethod *method)
1868 int index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
1876 add_method (MonoAotCompile *acfg, MonoMethod *method)
1878 int index = acfg->method_index;
1880 add_method_with_index (acfg, method, index);
1882 /* FIXME: Fix quadratic behavior */
1883 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (index));
1885 acfg->method_index ++;
1891 add_jit_icall_wrapper (gpointer key, gpointer value, gpointer user_data)
1893 MonoAotCompile *acfg = user_data;
1894 MonoJitICallInfo *callinfo = value;
1895 MonoMethod *wrapper;
1901 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
1902 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, check_for_pending_exc);
1905 add_method (acfg, wrapper);
1909 get_runtime_invoke (const char *signature)
1911 MonoMethodBuilder *mb;
1914 mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
1915 m = mono_mb_create_method (mb, mono_create_icall_signature (signature), 16);
1916 return mono_marshal_get_runtime_invoke (m);
1920 add_wrappers (MonoAotCompile *acfg)
1926 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
1927 * so there is only one wrapper of a given type, or inlining their contents into their
1931 /* FIXME: Collect these automatically */
1933 /* Runtime invoke wrappers */
1935 /* void runtime-invoke () [.cctor] */
1936 add_method (acfg, get_runtime_invoke ("void"));
1938 /* void runtime-invoke (string) [exception ctor] */
1939 add_method (acfg, get_runtime_invoke ("void string"));
1941 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1943 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1945 method = mono_get_method (acfg->image, token, NULL);
1947 if (!strcmp (method->name, "Main"))
1948 add_method (acfg, mono_marshal_get_runtime_invoke (method));
1951 /* JIT icall wrappers */
1952 /* FIXME: locking */
1953 g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
1955 /* Managed Allocators */
1956 nallocators = mono_gc_get_managed_allocator_types ();
1957 for (i = 0; i < nallocators; ++i) {
1958 m = mono_gc_get_managed_allocator_by_type (i);
1960 add_method (acfg, m);
1964 add_method (acfg, mono_marshal_get_stelemref ());
1966 /* remoting-invoke wrappers */
1967 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
1969 MonoMethodSignature *sig;
1970 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
1972 method = mono_get_method (acfg->image, token, NULL);
1974 sig = mono_method_signature (method);
1976 if (sig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class) &&
1977 !(method->flags & METHOD_ATTRIBUTE_VIRTUAL)) {
1978 m = mono_marshal_get_remoting_invoke_with_check (method);
1980 add_method (acfg, m);
1986 emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, guint32 code_len, MonoJumpInfo *relocs, gboolean got_only)
1988 int i, pindex, start_index, method_index;
1990 MonoJumpInfo *patch_info;
1991 MonoMethodHeader *header;
1996 header = mono_method_get_header (method);
1998 method_index = get_method_index (acfg, method);
2001 /* Collect and sort relocations */
2002 patches = g_ptr_array_new ();
2003 for (patch_info = relocs; patch_info; patch_info = patch_info->next)
2004 g_ptr_array_add (patches, patch_info);
2005 g_ptr_array_sort (patches, compare_patches);
2008 for (i = 0; i < code_len; i++) {
2010 for (pindex = start_index; pindex < patches->len; ++pindex) {
2011 patch_info = g_ptr_array_index (patches, pindex);
2012 if (patch_info->ip.i >= i)
2016 #ifdef MONO_ARCH_AOT_SUPPORTED
2018 if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
2019 start_index = pindex;
2021 switch (patch_info->type) {
2022 case MONO_PATCH_INFO_NONE:
2024 case MONO_PATCH_INFO_GOT_OFFSET: {
2025 guint32 offset = mono_arch_get_patch_offset (code + i);
2026 emit_bytes (acfg, code + i, offset);
2027 emit_symbol_diff (acfg, "got", ".", offset);
2029 i += offset + 4 - 1;
2035 char *direct_call_target;
2037 if (!is_got_patch (patch_info->type))
2041 * If this patch is a call, try emitting a direct call instead of
2042 * through a PLT entry. This is possible if the called method is in
2043 * the same assembly and requires no initialization.
2045 direct_call_target = NULL;
2046 if (!got_only && (patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == method->klass->image)) {
2047 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
2049 if (!callee_cfg->has_got_slots && (callee_cfg->method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)) {
2050 //printf ("DIRECT: %s %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (callee_cfg->method, TRUE));
2051 direct_call_target = g_strdup_printf (".Lm_%x", get_method_index (acfg, callee_cfg->method));
2052 patch_info->type = MONO_PATCH_INFO_NONE;
2053 acfg->stats.direct_calls ++;
2057 acfg->stats.all_calls ++;
2060 if (!got_only && !direct_call_target) {
2061 plt_index = get_plt_index (acfg, patch_info);
2062 if (plt_index != -1) {
2063 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
2064 direct_call_target = g_strdup_printf (".Lp_%d", plt_index);
2068 if (direct_call_target) {
2069 #if defined(__i386__) || defined(__x86_64__)
2070 g_assert (code [i] == 0xe8);
2071 /* Need to make sure this is exactly 5 bytes long */
2072 emit_byte (acfg, '\xe8');
2073 emit_symbol_diff (acfg, direct_call_target, ".", -4);
2075 #elif defined(__arm__)
2076 #ifdef USE_BIN_WRITER
2077 /* FIXME: Can't encode this using the current symbol writer functions */
2078 g_assert_not_reached ();
2080 emit_unset_mode (acfg);
2081 fprintf (acfg->fp, "bl %s\n", direct_call_target);
2086 got_slot = get_got_offset (acfg, patch_info);
2088 emit_bytes (acfg, code + i, mono_arch_get_patch_offset (code + i));
2090 emit_symbol_diff (acfg, "got", ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
2091 #elif defined(__i386__)
2092 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
2093 #elif defined(__arm__)
2094 emit_symbol_diff (acfg, "got", ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
2096 g_assert_not_reached ();
2099 i += mono_arch_get_patch_offset (code + i) + 4 - 1;
2105 #endif /* MONO_ARCH_AOT_SUPPORTED */
2108 emit_bytes (acfg, code + i, 1);
2113 emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
2119 int func_alignment = 16;
2120 MonoMethodHeader *header;
2122 method = cfg->method;
2123 code = cfg->native_code;
2124 header = mono_method_get_header (method);
2126 method_index = get_method_index (acfg, method);
2128 /* Make the labels local */
2129 symbol = g_strdup_printf (".Lm_%x", method_index);
2131 emit_alignment (acfg, func_alignment);
2132 emit_label (acfg, symbol);
2133 if (acfg->aot_opts.write_symbols)
2134 emit_global (acfg, symbol, TRUE);
2136 if (cfg->verbose_level > 0)
2137 g_print ("Method %s emitted as %s\n", mono_method_full_name (method, TRUE), symbol);
2139 acfg->stats.code_size += cfg->code_len;
2141 acfg->method_got_offsets [method_index] = acfg->got_offset;
2143 emit_and_reloc_code (acfg, method, code, cfg->code_len, cfg->patch_info, FALSE);
2146 /* Collect and sort relocations */
2147 patches = g_ptr_array_new ();
2148 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
2149 g_ptr_array_add (patches, patch_info);
2150 g_ptr_array_sort (patches, compare_patches);
2153 for (i = 0; i < cfg->code_len; i++) {
2155 for (pindex = start_index; pindex < patches->len; ++pindex) {
2156 patch_info = g_ptr_array_index (patches, pindex);
2157 if (patch_info->ip.i >= i)
2161 #ifdef MONO_ARCH_AOT_SUPPORTED
2163 if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
2164 start_index = pindex;
2166 switch (patch_info->type) {
2167 case MONO_PATCH_INFO_NONE:
2169 case MONO_PATCH_INFO_GOT_OFFSET: {
2170 guint32 offset = mono_arch_get_patch_offset (code + i);
2171 emit_bytes (acfg, code + i, offset);
2172 emit_symbol_diff (acfg, "got", ".", offset);
2174 i += offset + 4 - 1;
2180 char *direct_call_target;
2182 if (!is_got_patch (patch_info->type))
2186 * If this patch is a call, try emitting a direct call instead of
2187 * through a PLT entry. This is possible if the called method is in
2188 * the same assembly and requires no initialization.
2190 direct_call_target = NULL;
2191 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == cfg->method->klass->image)) {
2192 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
2194 if (!callee_cfg->has_got_slots && (callee_cfg->method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)) {
2195 //printf ("DIRECT: %s %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (callee_cfg->method, TRUE));
2196 direct_call_target = g_strdup_printf (".Lm_%x", get_method_index (acfg, callee_cfg->method));
2197 patch_info->type = MONO_PATCH_INFO_NONE;
2198 acfg->stats.direct_calls ++;
2202 acfg->stats.all_calls ++;
2205 if (!direct_call_target) {
2206 plt_index = get_plt_index (acfg, patch_info);
2207 if (plt_index != -1) {
2208 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
2209 direct_call_target = g_strdup_printf (".Lp_%d", plt_index);
2213 if (direct_call_target) {
2214 #if defined(__i386__) || defined(__x86_64__)
2215 g_assert (code [i] == 0xe8);
2216 /* Need to make sure this is exactly 5 bytes long */
2217 emit_byte (acfg, '\xe8');
2218 emit_symbol_diff (acfg, direct_call_target, ".", -4);
2220 #elif defined(__arm__)
2221 #ifdef USE_BIN_WRITER
2222 /* FIXME: Can't encode this using the current symbol writer functions */
2223 g_assert_not_reached ();
2225 emit_unset_mode (acfg);
2226 fprintf (acfg->fp, "bl %s\n", direct_call_target);
2231 got_slot = get_got_offset (acfg, patch_info);
2233 emit_bytes (acfg, code + i, mono_arch_get_patch_offset (code + i));
2235 emit_symbol_diff (acfg, "got", ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
2236 #elif defined(__i386__)
2237 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
2238 #elif defined(__arm__)
2239 emit_symbol_diff (acfg, "got", ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
2241 g_assert_not_reached ();
2244 i += mono_arch_get_patch_offset (code + i) + 4 - 1;
2250 #endif /* MONO_ARCH_AOT_SUPPORTED */
2253 emit_bytes (acfg, code + i, 1);
2263 * Encode PATCH_INFO into its disk representation. If SHARED is true, encode some types
2264 * of patches by allocating a GOT entry for them, and encode the GOT offset instead.
2267 encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf, gboolean shared)
2271 switch (patch_info->type) {
2272 case MONO_PATCH_INFO_NONE:
2274 case MONO_PATCH_INFO_IMAGE:
2275 encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
2277 case MONO_PATCH_INFO_METHOD_REL:
2278 encode_value ((gint)patch_info->data.offset, p, &p);
2280 case MONO_PATCH_INFO_SWITCH: {
2281 gpointer *table = (gpointer *)patch_info->data.table->table;
2284 encode_value (patch_info->data.table->table_size, p, &p);
2285 for (k = 0; k < patch_info->data.table->table_size; k++)
2286 encode_value ((int)(gssize)table [k], p, &p);
2289 case MONO_PATCH_INFO_METHODCONST:
2290 case MONO_PATCH_INFO_METHOD:
2291 case MONO_PATCH_INFO_METHOD_JUMP:
2292 case MONO_PATCH_INFO_ICALL_ADDR:
2293 encode_method_ref (acfg, patch_info->data.method, p, &p);
2295 case MONO_PATCH_INFO_INTERNAL_METHOD:
2296 case MONO_PATCH_INFO_JIT_ICALL_ADDR: {
2297 guint32 len = strlen (patch_info->data.name);
2299 encode_value (len, p, &p);
2301 memcpy (p, patch_info->data.name, len);
2306 case MONO_PATCH_INFO_LDSTR: {
2307 guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
2308 guint32 token = patch_info->data.token->token;
2309 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
2310 encode_value (image_index, p, &p);
2311 encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
2314 case MONO_PATCH_INFO_RVA:
2315 case MONO_PATCH_INFO_DECLSEC:
2316 case MONO_PATCH_INFO_LDTOKEN:
2317 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2319 guint32 offset = get_got_offset (acfg, patch_info);
2320 encode_value (offset, p, &p);
2322 encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
2323 encode_value (patch_info->data.token->token, p, &p);
2326 case MONO_PATCH_INFO_EXC_NAME: {
2327 MonoClass *ex_class;
2330 mono_class_from_name (mono_defaults.exception_class->image,
2331 "System", patch_info->data.target);
2332 g_assert (ex_class);
2333 encode_klass_ref (acfg, ex_class, p, &p);
2336 case MONO_PATCH_INFO_R4:
2337 encode_value (*((guint32 *)patch_info->data.target), p, &p);
2339 case MONO_PATCH_INFO_R8:
2340 encode_value (*((guint32 *)patch_info->data.target), p, &p);
2341 encode_value (*(((guint32 *)patch_info->data.target) + 1), p, &p);
2343 case MONO_PATCH_INFO_VTABLE:
2344 case MONO_PATCH_INFO_CLASS:
2345 case MONO_PATCH_INFO_IID:
2346 case MONO_PATCH_INFO_ADJUSTED_IID:
2348 guint32 offset = get_got_offset (acfg, patch_info);
2349 encode_value (offset, p, &p);
2351 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
2354 case MONO_PATCH_INFO_CLASS_INIT:
2355 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
2356 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
2358 case MONO_PATCH_INFO_FIELD:
2359 case MONO_PATCH_INFO_SFLDA:
2361 guint32 offset = get_got_offset (acfg, patch_info);
2362 encode_value (offset, p, &p);
2364 encode_field_info (acfg, patch_info->data.field, p, &p);
2367 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
2369 case MONO_PATCH_INFO_WRAPPER:
2370 encode_value (patch_info->data.method->wrapper_type, p, &p);
2372 switch (patch_info->data.method->wrapper_type) {
2373 case MONO_WRAPPER_REMOTING_INVOKE:
2374 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
2375 case MONO_WRAPPER_XDOMAIN_INVOKE: {
2377 guint32 image_index;
2380 m = mono_marshal_method_from_wrapper (patch_info->data.method);
2381 image_index = get_image_index (acfg, m->klass->image);
2383 g_assert (image_index < 256);
2384 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
2386 encode_value ((image_index << 24) + (mono_metadata_token_index (token)), p, &p);
2389 case MONO_WRAPPER_PROXY_ISINST:
2390 case MONO_WRAPPER_LDFLD:
2391 case MONO_WRAPPER_LDFLDA:
2392 case MONO_WRAPPER_STFLD:
2393 case MONO_WRAPPER_ISINST: {
2394 MonoClass *proxy_class = (MonoClass*)mono_marshal_method_from_wrapper (patch_info->data.method);
2395 encode_klass_ref (acfg, proxy_class, p, &p);
2398 case MONO_WRAPPER_LDFLD_REMOTE:
2399 case MONO_WRAPPER_STFLD_REMOTE:
2401 case MONO_WRAPPER_ALLOC: {
2402 int alloc_type = mono_gc_get_managed_allocator_type (patch_info->data.method);
2403 g_assert (alloc_type != -1);
2404 encode_value (alloc_type, p, &p);
2407 case MONO_WRAPPER_STELEMREF:
2410 g_assert_not_reached ();
2414 g_warning ("unable to handle jump info %d", patch_info->type);
2415 g_assert_not_reached ();
2422 encode_patch_list (MonoAotCompile *acfg, GPtrArray *patches, int n_patches, int first_got_offset, guint8 *buf, guint8 **endbuf)
2425 guint32 last_offset, j, pindex;
2426 MonoJumpInfo *patch_info;
2428 encode_value (n_patches, p, &p);
2431 encode_value (first_got_offset, p, &p);
2433 /* First encode the type+position table */
2436 for (pindex = 0; pindex < patches->len; ++pindex) {
2438 patch_info = g_ptr_array_index (patches, pindex);
2440 if (patch_info->type == MONO_PATCH_INFO_NONE)
2445 //printf ("T: %d O: %d.\n", patch_info->type, patch_info->ip.i);
2446 offset = patch_info->ip.i - last_offset;
2447 last_offset = patch_info->ip.i;
2449 /* Only the type is needed */
2450 *p = patch_info->type;
2454 /* Then encode the other info */
2455 for (pindex = 0; pindex < patches->len; ++pindex) {
2456 patch_info = g_ptr_array_index (patches, pindex);
2458 encode_patch (acfg, patch_info, p, &p, TRUE);
2465 emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
2469 int pindex, buf_size, n_patches;
2473 MonoJumpInfo *patch_info;
2474 MonoMethodHeader *header;
2475 guint32 method_index;
2477 guint32 first_got_offset;
2479 method = cfg->method;
2480 code = cfg->native_code;
2481 header = mono_method_get_header (method);
2483 method_index = get_method_index (acfg, method);
2485 /* Make the labels local */
2486 symbol = g_strdup_printf (".Lm_%x_p", method_index);
2488 /* Sort relocations */
2489 patches = g_ptr_array_new ();
2490 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
2491 g_ptr_array_add (patches, patch_info);
2492 g_ptr_array_sort (patches, compare_patches);
2494 first_got_offset = acfg->method_got_offsets [method_index];
2496 /**********************/
2497 /* Encode method info */
2498 /**********************/
2500 buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
2501 p = buf = g_malloc (buf_size);
2503 if (mono_class_get_cctor (method->klass))
2504 encode_klass_ref (acfg, method->klass, p, &p);
2506 /* Not needed when loading the method */
2507 encode_value (0, p, &p);
2510 if (cfg->opt & MONO_OPT_SHARED) {
2511 encode_value (g_list_length (cfg->ldstr_list), p, &p);
2512 for (l = cfg->ldstr_list; l; l = l->next) {
2513 encode_value ((long)l->data, p, &p);
2517 /* Used only in shared mode */
2518 g_assert (!cfg->ldstr_list);
2521 for (pindex = 0; pindex < patches->len; ++pindex) {
2522 patch_info = g_ptr_array_index (patches, pindex);
2524 if ((patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
2525 (patch_info->type == MONO_PATCH_INFO_NONE)) {
2526 patch_info->type = MONO_PATCH_INFO_NONE;
2531 if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
2532 /* Stored in a GOT slot initialized at module load time */
2533 patch_info->type = MONO_PATCH_INFO_NONE;
2537 if ((patch_info->type == MONO_PATCH_INFO_METHOD) ||
2538 (patch_info->type == MONO_PATCH_INFO_INTERNAL_METHOD) ||
2539 (patch_info->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) ||
2540 (patch_info->type == MONO_PATCH_INFO_WRAPPER) ||
2541 (patch_info->type == MONO_PATCH_INFO_CLASS_INIT)) {
2542 /* Calls are made through the PLT */
2543 patch_info->type = MONO_PATCH_INFO_NONE;
2551 g_assert (cfg->has_got_slots);
2553 encode_patch_list (acfg, patches, n_patches, first_got_offset, p, &p);
2555 acfg->stats.info_size += p - buf;
2557 /* Emit method info */
2559 emit_label (acfg, symbol);
2561 g_assert (p - buf < buf_size);
2562 emit_bytes (acfg, buf, p - buf);
2569 emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
2572 int k, buf_size, method_index;
2573 guint32 debug_info_size;
2576 MonoMethodHeader *header;
2577 guint8 *p, *buf, *debug_info;
2579 method = cfg->method;
2580 code = cfg->native_code;
2581 header = mono_method_get_header (method);
2583 method_index = get_method_index (acfg, method);
2585 /* Make the labels local */
2586 symbol = g_strdup_printf (".Le_%x_p", method_index);
2588 mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
2590 buf_size = header->num_clauses * 256 + debug_info_size + 128;
2591 p = buf = g_malloc (buf_size);
2593 encode_value (cfg->code_len, p, &p);
2594 encode_value (cfg->used_int_regs, p, &p);
2596 /* Exception table */
2597 if (header->num_clauses) {
2598 MonoJitInfo *jinfo = cfg->jit_info;
2600 for (k = 0; k < header->num_clauses; ++k) {
2601 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
2603 encode_value (ei->exvar_offset, p, &p);
2605 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
2606 encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
2608 encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
2609 encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
2610 encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
2614 g_assert (debug_info_size < buf_size);
2616 encode_value (debug_info_size, p, &p);
2617 if (debug_info_size) {
2618 memcpy (p, debug_info, debug_info_size);
2619 p += debug_info_size;
2620 g_free (debug_info);
2623 acfg->stats.ex_info_size += p - buf;
2627 emit_label (acfg, symbol);
2629 g_assert (p - buf < buf_size);
2630 emit_bytes (acfg, buf, p - buf);
2637 emit_klass_info (MonoAotCompile *acfg, guint32 token)
2639 MonoClass *klass = mono_class_get (acfg->image, token);
2643 gboolean no_special_static, cant_encode;
2645 buf_size = 10240 + (klass->vtable_size * 16);
2646 p = buf = g_malloc (buf_size);
2650 mono_class_init (klass);
2652 mono_class_setup_vtable (klass);
2655 * Emit all the information which is required for creating vtables so
2656 * the runtime does not need to create the MonoMethod structures which
2657 * take up a lot of space.
2660 no_special_static = !mono_class_has_special_static_fields (klass);
2662 /* Check whenever we have enough info to encode the vtable */
2663 cant_encode = FALSE;
2664 for (i = 0; i < klass->vtable_size; ++i) {
2665 MonoMethod *cm = klass->vtable [i];
2667 if (cm && mono_method_signature (cm)->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, cm))
2671 if (klass->generic_container || cant_encode) {
2672 encode_value (-1, p, &p);
2674 encode_value (klass->vtable_size, p, &p);
2675 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);
2676 if (klass->has_cctor)
2677 encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
2678 if (klass->has_finalize)
2679 encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
2681 encode_value (klass->instance_size, p, &p);
2682 encode_value (mono_class_data_size (klass), p, &p);
2683 encode_value (klass->packing_size, p, &p);
2684 encode_value (klass->min_align, p, &p);
2686 for (i = 0; i < klass->vtable_size; ++i) {
2687 MonoMethod *cm = klass->vtable [i];
2690 encode_method_ref (acfg, cm, p, &p);
2692 encode_value (0, p, &p);
2696 acfg->stats.class_info_size += p - buf;
2699 label = g_strdup_printf (".LK_I_%x", token - MONO_TOKEN_TYPE_DEF - 1);
2700 emit_label (acfg, label);
2702 g_assert (p - buf < buf_size);
2703 emit_bytes (acfg, buf, p - buf);
2708 * Calls made from AOTed code are routed through a table of jumps similar to the
2709 * ELF PLT (Program Linkage Table). The differences are the following:
2710 * - the ELF PLT entries make an indirect jump though the GOT so they expect the
2711 * GOT pointer to be in EBX. We want to avoid this, so our table contains direct
2712 * jumps. This means the jumps need to be patched when the address of the callee is
2713 * known. Initially the PLT entries jump to code which transfers control to the
2714 * AOT runtime through the first PLT entry.
2717 emit_plt (MonoAotCompile *acfg)
2722 guint32 *plt_info_offsets;
2725 * Encode info need to resolve PLT entries.
2727 buf_size = acfg->plt_offset * 128;
2728 p = buf = g_malloc (buf_size);
2730 plt_info_offsets = g_new0 (guint32, acfg->plt_offset);
2732 for (i = 1; i < acfg->plt_offset; ++i) {
2733 MonoJumpInfo *patch_info = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
2735 plt_info_offsets [i] = p - buf;
2736 encode_value (patch_info->type, p, &p);
2737 encode_patch (acfg, patch_info, p, &p, FALSE);
2741 symbol = g_strdup_printf ("plt");
2743 /* This section will be made read-write by the AOT loader */
2744 emit_section_change (acfg, ".text", 0);
2745 emit_global (acfg, symbol, TRUE);
2746 emit_alignment (acfg, PAGESIZE);
2747 emit_label (acfg, symbol);
2749 #if defined(USE_BIN_WRITER) && defined(__arm__)
2751 g_assert_not_reached ();
2755 * The first plt entry is used to transfer code to the AOT loader.
2757 emit_label (acfg, ".Lp_0");
2758 #if defined(__i386__)
2759 /* It is filled up during loading by the AOT loader. */
2760 emit_zero_bytes (acfg, 16);
2761 #elif defined(__x86_64__)
2762 /* This should be exactly 16 bytes long */
2763 /* jmpq *<offset>(%rip) */
2764 emit_byte (acfg, '\xff');
2765 emit_byte (acfg, '\x25');
2766 emit_symbol_diff (acfg, "plt_jump_table", ".", -4);
2767 emit_zero_bytes (acfg, 10);
2768 #elif defined(__arm__)
2769 /* This is 8 bytes long, init_plt () depends on this */
2770 emit_unset_mode (acfg);
2771 fprintf (acfg->fp, "\tldr pc, [pc, #-4]\n");
2772 /* This is filled up during loading by the AOT loader */
2773 fprintf (acfg->fp, "\t.word 0\n");
2775 g_assert_not_reached ();
2778 for (i = 1; i < acfg->plt_offset; ++i) {
2781 label = g_strdup_printf (".Lp_%d", i);
2782 emit_label (acfg, label);
2784 #if defined(__i386__)
2785 /* Need to make sure this is 5 bytes long */
2786 emit_byte (acfg, '\xe9');
2787 label = g_strdup_printf (".Lpd_%d", i);
2788 emit_symbol_diff (acfg, label, ".", -4);
2790 #elif defined(__x86_64__)
2792 * We can't emit jumps because they are 32 bits only so they can't be patched.
2793 * So we emit a jump table instead whose entries are patched by the AOT loader to
2794 * point to .Lpd entries. ELF stores these in the GOT too, but we don't, since
2795 * methods with GOT entries can't be called directly.
2796 * We also emit the default PLT code here since the PLT code will not be patched.
2797 * An x86_64 plt entry is 16 bytes long, init_plt () depends on this.
2799 /* jmpq *<offset>(%rip) */
2800 emit_byte (acfg, '\xff');
2801 emit_byte (acfg, '\x25');
2802 emit_symbol_diff (acfg, "plt_jump_table", ".", (i * sizeof (gpointer)) -4);
2803 /* mov <plt info offset>, %eax */
2804 emit_byte (acfg, '\xb8');
2805 emit_int32 (acfg, plt_info_offsets [i]);
2807 emit_byte (acfg, '\xe9');
2808 emit_symbol_diff (acfg, ".Lp_0", ".", -4);
2809 #elif defined(__arm__)
2811 * Emit an indirect call since branch displacements are limited to 24 bits on
2812 * ARM. Put the jump table entries inline since offsets are even smaller on
2815 * - optimize OP_AOTCONST implementation
2816 * - optimize the PLT entries
2817 * - optimize SWITCH AOT implementation
2818 * - implement IMT support
2820 /* This is 8 bytes long, init_plt () depends on this */
2821 emit_unset_mode (acfg);
2822 fprintf (acfg->fp, "\tldr pc, [pc, #-4]\n");
2823 /* This is filled up during loading by the AOT loader */
2824 fprintf (acfg->fp, "\t.word 0\n");
2826 g_assert_not_reached ();
2830 symbol = g_strdup_printf ("plt_end");
2831 emit_global (acfg, symbol, TRUE);
2832 emit_label (acfg, symbol);
2835 * Emit the default targets for the PLT entries separately since these will not
2836 * be modified at runtime.
2838 for (i = 1; i < acfg->plt_offset; ++i) {
2841 label = g_strdup_printf (".Lpd_%d", i);
2842 emit_label (acfg, label);
2845 /* Put the offset into the register expected by mono_aot_plt_trampoline */
2846 #if defined(__i386__)
2847 /* movl $const, %eax */
2848 emit_byte (acfg, '\xb8');
2849 emit_int32 (acfg, plt_info_offsets [i]);
2851 emit_byte (acfg, '\xe9');
2852 emit_symbol_diff (acfg, ".Lp_0", ".", -4);
2853 #elif defined(__x86_64__)
2854 /* Emitted along with the PLT entries since they will not be patched */
2855 #elif defined(__arm__)
2856 /* This is 12 bytes long, init_plt () depends on this */
2857 emit_unset_mode (acfg);
2858 fprintf (acfg->fp, "\tldr ip, [pc, #0]\n");
2859 fprintf (acfg->fp, "\tb .Lp_0\n");
2860 fprintf (acfg->fp, "\t.word %d\n", plt_info_offsets [i]);
2862 g_assert_not_reached ();
2867 symbol = g_strdup_printf ("plt_info");
2868 emit_global (acfg, symbol, FALSE);
2869 emit_label (acfg, symbol);
2871 g_assert (p - buf < buf_size);
2872 emit_bytes (acfg, buf, p - buf);
2875 symbol = g_strdup_printf ("plt_jump_table_addr");
2876 emit_section_change (acfg, ".data", 0);
2877 emit_global (acfg, symbol, FALSE);
2878 emit_alignment (acfg, 8);
2879 emit_label (acfg, symbol);
2880 emit_pointer (acfg, "plt_jump_table");
2882 symbol = g_strdup_printf ("plt_jump_table_size");
2883 emit_section_change (acfg, ".data", 0);
2884 emit_global (acfg, symbol, FALSE);
2885 emit_alignment (acfg, 8);
2886 emit_label (acfg, symbol);
2887 emit_symbol_diff (acfg, "plt_jump_table_end", "plt_jump_table", 0);
2889 /* Don't make this a global so accesses don't need relocations */
2890 symbol = g_strdup_printf ("plt_jump_table");
2891 emit_section_change (acfg, ".bss", 0);
2892 emit_label (acfg, symbol);
2895 emit_zero_bytes (acfg, (int)(acfg->plt_offset * sizeof (gpointer)));
2898 symbol = g_strdup_printf ("plt_jump_table_end");
2899 emit_label (acfg, symbol);
2903 emit_named_code (MonoAotCompile *acfg, char *name, guint8 *code, guint32 code_size,
2904 int got_offset, MonoJumpInfo *ji)
2908 MonoJumpInfo *patch_info;
2914 symbol = g_strdup_printf ("%s", name);
2916 emit_section_change (acfg, ".text", 0);
2917 emit_global (acfg, symbol, TRUE);
2918 emit_label (acfg, symbol);
2922 * The code should access everything through the GOT, so we pass
2925 emit_and_reloc_code (acfg, NULL, code, code_size, ji, TRUE);
2929 /* Sort relocations */
2930 patches = g_ptr_array_new ();
2931 for (patch_info = ji; patch_info; patch_info = patch_info->next)
2932 g_ptr_array_add (patches, patch_info);
2933 g_ptr_array_sort (patches, compare_patches);
2935 buf_size = patches->len * 128;
2936 buf = g_malloc (buf_size);
2939 encode_patch_list (acfg, patches, patches->len, got_offset, p, &p);
2940 g_assert (p - buf < buf_size);
2942 symbol = g_strdup_printf ("%s_p", name);
2944 emit_section_change (acfg, ".text", 0);
2945 emit_global (acfg, symbol, TRUE);
2946 emit_label (acfg, symbol);
2949 emit_bytes (acfg, buf, p - buf);
2953 * When running in aot-only mode, we can't create trampolines at runtime, so we create
2954 * a few, and save them in the AOT file. Normal trampolines embed their argument as a
2955 * literal inside the trampoline code, we can't do that here, so instead we embed an offset
2956 * which needs to be added to the trampoline address to get the address of the GOT slot
2957 * which contains the argument value.
2958 * The generated trampolines jump to the generic trampolines using another GOT slot, which
2959 * will be setup by the AOT loader to point to the generic trampoline code of the given
2961 * Currently, we only emit trampolines into the mscorlib AOT image.
2964 emit_trampolines (MonoAotCompile *acfg)
2967 int tramp_type, i, offset;
2969 if (!acfg->aot_opts.full_aot)
2972 g_assert (acfg->image->assembly);
2973 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") != 0)
2976 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
2978 * Emit the generic trampolines.
2980 * We could save some code by treating the generic trampolines as a wrapper
2981 * method, but that approach has its own complexities, so we choose the simpler
2984 for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
2989 code = mono_arch_create_trampoline_code_full (tramp_type, &code_size, &ji, TRUE);
2991 /* Emit trampoline code */
2993 symbol = g_strdup_printf ("generic_trampoline_%d", tramp_type);
2995 emit_named_code (acfg, symbol, code, code_size, acfg->got_offset, ji);
3002 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for each
3006 /* Reserve some entries at the end of the GOT for our use */
3007 acfg->num_trampoline_got_entries = acfg->num_aot_trampolines * 2;
3009 symbol = g_strdup_printf ("trampolines");
3011 emit_section_change (acfg, ".text", 0);
3012 emit_global (acfg, symbol, TRUE);
3013 emit_label (acfg, symbol);
3015 for (i = 0; i < acfg->num_aot_trampolines; ++i) {
3016 offset = acfg->got_offset + (i * 2);
3018 #if defined(__x86_64__)
3019 /* This should be exactly 16 bytes long */
3020 /* It should work together with the generic trampoline code in tramp-amd64.c */
3021 /* call *<offset>(%rip) */
3022 emit_byte (acfg, '\x41');
3023 emit_byte (acfg, '\xff');
3024 emit_byte (acfg, '\x15');
3025 emit_symbol_diff (acfg, "got", ".", (offset * sizeof (gpointer)) - 4);
3026 /* This should be relative to the start of the trampoline */
3027 emit_symbol_diff (acfg, "got", ".", (offset * sizeof (gpointer)) - 4 + 19);
3028 emit_zero_bytes (acfg, 5);
3030 g_assert_not_reached ();
3034 symbol = g_strdup_printf ("trampolines_info");
3036 emit_section_change (acfg, ".text", 0);
3037 emit_global (acfg, symbol, TRUE);
3038 emit_alignment (acfg, PAGESIZE);
3039 emit_label (acfg, symbol);
3041 emit_int32 (acfg, acfg->num_aot_trampolines);
3042 emit_int32 (acfg, acfg->got_offset);
3046 str_begins_with (const char *str1, const char *str2)
3048 int len = strlen (str2);
3049 return strncmp (str1, str2, len) == 0;
3053 mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
3055 gchar **args, **ptr;
3057 memset (opts, 0, sizeof (*opts));
3059 args = g_strsplit (aot_options ? aot_options : "", ",", -1);
3060 for (ptr = args; ptr && *ptr; ptr ++) {
3061 const char *arg = *ptr;
3063 if (str_begins_with (arg, "outfile=")) {
3064 opts->outfile = g_strdup (arg + strlen ("outfile="));
3065 } else if (str_begins_with (arg, "save-temps")) {
3066 opts->save_temps = TRUE;
3067 } else if (str_begins_with (arg, "keep-temps")) {
3068 opts->save_temps = TRUE;
3069 } else if (str_begins_with (arg, "write-symbols")) {
3070 opts->write_symbols = TRUE;
3071 } else if (str_begins_with (arg, "metadata-only")) {
3072 opts->metadata_only = TRUE;
3073 } else if (str_begins_with (arg, "bind-to-runtime-version")) {
3074 opts->bind_to_runtime_version = TRUE;
3075 } else if (str_begins_with (arg, "full")) {
3076 opts->full_aot = TRUE;
3078 fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
3085 add_token_info_hash (gpointer key, gpointer value, gpointer user_data)
3087 MonoMethod *method = (MonoMethod*)key;
3088 MonoJumpInfoToken *ji = (MonoJumpInfoToken*)value;
3089 MonoJumpInfoToken *new_ji = g_new0 (MonoJumpInfoToken, 1);
3090 MonoAotCompile *acfg = user_data;
3092 new_ji->image = ji->image;
3093 new_ji->token = ji->token;
3094 g_hash_table_insert (acfg->token_info_hash, method, new_ji);
3098 compile_method (MonoAotCompile *acfg, MonoMethod *method)
3101 MonoJumpInfo *patch_info;
3105 if (acfg->aot_opts.metadata_only)
3108 index = get_method_index (acfg, method);
3110 /* fixme: maybe we can also precompile wrapper methods */
3111 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3112 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3113 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
3114 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
3118 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
3121 acfg->stats.mcount++;
3123 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
3125 * FIXME: Enabling this causes virtual-sync.exe to fail, since the trampoline
3126 * code can't determine that it needs to insert a sync wrapper in the AOT case.
3131 //acfg->opts &= ~MONO_OPT_GSHARED;
3133 if (!(acfg->opts & MONO_OPT_GSHARED)) {
3134 if (method->is_generic || method->klass->generic_container) {
3135 acfg->stats.genericcount ++;
3141 * Since these methods are the only ones which are compiled with
3142 * AOT support, and they are not used by runtime startup/shutdown code,
3143 * the runtime will not see AOT methods during AOT compilation,so it
3144 * does not need to support them by creating a fake GOT etc.
3146 cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), FALSE, TRUE, 0);
3147 if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
3148 //printf ("F: %s\n", mono_method_full_name (method, TRUE));
3149 acfg->stats.genericcount ++;
3152 if (cfg->exception_type != MONO_EXCEPTION_NONE) {
3153 /* Let the exception happen at runtime */
3157 if (cfg->disable_aot) {
3158 //printf ("Skip (other): %s\n", mono_method_full_name (method, TRUE));
3159 acfg->stats.ocount++;
3160 mono_destroy_compile (cfg);
3164 /* Nullify patches which need no aot processing */
3165 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3166 switch (patch_info->type) {
3167 case MONO_PATCH_INFO_LABEL:
3168 case MONO_PATCH_INFO_BB:
3169 patch_info->type = MONO_PATCH_INFO_NONE;
3176 /* Collect method->token associations from the cfg */
3177 g_hash_table_foreach (cfg->token_info_hash, add_token_info_hash, acfg);
3180 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3181 switch (patch_info->type) {
3182 case MONO_PATCH_INFO_ABS:
3183 /* unable to handle this */
3184 //printf ("Skip (abs addr): %s %d\n", mono_method_full_name (method, TRUE), patch_info->type);
3193 acfg->stats.abscount++;
3194 mono_destroy_compile (cfg);
3199 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3200 if (patch_info->type == MONO_PATCH_INFO_METHOD_JUMP) {
3202 * FIXME: We can't handle this because mono_jit_compile_method_inner will try
3203 * to patch the AOT code when the target of the jump is compiled.
3211 acfg->stats.ocount++;
3212 mono_destroy_compile (cfg);
3216 /* some wrappers are very common */
3217 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3218 if (patch_info->type == MONO_PATCH_INFO_METHODCONST) {
3219 switch (patch_info->data.method->wrapper_type) {
3220 case MONO_WRAPPER_PROXY_ISINST:
3221 patch_info->type = MONO_PATCH_INFO_WRAPPER;
3225 if (patch_info->type == MONO_PATCH_INFO_METHOD) {
3226 switch (patch_info->data.method->wrapper_type) {
3227 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
3228 case MONO_WRAPPER_XDOMAIN_INVOKE:
3229 case MONO_WRAPPER_STFLD:
3230 case MONO_WRAPPER_LDFLD:
3231 case MONO_WRAPPER_LDFLDA:
3232 case MONO_WRAPPER_LDFLD_REMOTE:
3233 case MONO_WRAPPER_STFLD_REMOTE:
3234 case MONO_WRAPPER_STELEMREF:
3235 case MONO_WRAPPER_ISINST:
3236 case MONO_WRAPPER_PROXY_ISINST:
3237 case MONO_WRAPPER_ALLOC:
3238 case MONO_WRAPPER_REMOTING_INVOKE:
3239 patch_info->type = MONO_PATCH_INFO_WRAPPER;
3246 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3247 switch (patch_info->type) {
3248 case MONO_PATCH_INFO_METHOD:
3249 case MONO_PATCH_INFO_METHODCONST:
3250 if (patch_info->data.method->wrapper_type) {
3251 /* unable to handle this */
3252 //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));
3256 if (!patch_info->data.method->token)
3258 * The method is part of a constructed type like Int[,].Set (). It doesn't
3259 * have a token, and we can't make one, since the parent type is part of
3260 * assembly which contains the element type, and not the assembly which
3261 * referenced this type.
3264 if (patch_info->data.method->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, patch_info->data.method))
3265 /* FIXME: Can't encode these */
3268 case MONO_PATCH_INFO_VTABLE:
3269 case MONO_PATCH_INFO_CLASS_INIT:
3270 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
3271 case MONO_PATCH_INFO_CLASS:
3272 case MONO_PATCH_INFO_IID:
3273 case MONO_PATCH_INFO_ADJUSTED_IID:
3274 if (!patch_info->data.klass->type_token)
3275 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))
3284 acfg->stats.wrappercount++;
3285 mono_destroy_compile (cfg);
3289 /* Determine whenever the method has GOT slots */
3290 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3291 switch (patch_info->type) {
3292 case MONO_PATCH_INFO_GOT_OFFSET:
3293 case MONO_PATCH_INFO_NONE:
3294 case MONO_PATCH_INFO_METHOD:
3295 case MONO_PATCH_INFO_INTERNAL_METHOD:
3296 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
3297 case MONO_PATCH_INFO_WRAPPER:
3299 case MONO_PATCH_INFO_IMAGE:
3300 if (patch_info->data.image == acfg->image)
3301 /* Stored in GOT slot 0 */
3305 cfg->has_got_slots = TRUE;
3310 if (!cfg->has_got_slots)
3311 acfg->stats.methods_without_got_slots ++;
3313 /* Make a copy of the patch info which is in the mempool */
3315 MonoJumpInfo *patches = NULL, *patches_end = NULL;
3317 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
3318 MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
3321 patches = new_patch_info;
3323 patches_end->next = new_patch_info;
3324 patches_end = new_patch_info;
3326 cfg->patch_info = patches;
3329 /* Free some fields used by cfg to conserve memory */
3330 mono_mempool_destroy (cfg->mempool);
3331 cfg->mempool = NULL;
3332 g_free (cfg->varinfo);
3333 cfg->varinfo = NULL;
3337 mono_regstate_free (cfg->rs);
3341 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
3343 acfg->cfgs [index] = cfg;
3345 g_hash_table_insert (acfg->method_to_cfg, cfg->method, cfg);
3347 acfg->stats.ccount++;
3351 load_profile_files (MonoAotCompile *acfg)
3355 int file_index, res, method_index, i;
3362 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);
3364 if (!g_file_test (tmp, G_FILE_TEST_IS_REGULAR))
3367 infile = fopen (tmp, "r");
3370 printf ("Using profile data file '%s'\n", tmp);
3374 res = fscanf (infile, "%32s\n", ver);
3375 if ((res != 1) || strcmp (ver, "#VER:1") != 0) {
3376 printf ("Profile file has wrong version or invalid.\n");
3382 res = fscanf (infile, "%d\n", &token);
3386 method_index = mono_metadata_token_index (token) - 1;
3388 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (method_index)))
3389 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (method_index));
3394 /* Add missing methods */
3396 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3397 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (i)))
3398 unordered = g_list_prepend (unordered, GUINT_TO_POINTER (i));
3400 unordered = g_list_reverse (unordered);
3401 if (acfg->method_order)
3402 g_list_last (acfg->method_order)->next = unordered;
3404 acfg->method_order = unordered;
3410 * Collect all patches which have shared GOT entries and alloc entries for them. The
3411 * rest will get entries allocated during emit_code ().
3414 alloc_got_slots (MonoAotCompile *acfg)
3420 /* Slot 0 is reserved for the address of the current assembly */
3421 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
3422 ji->type = MONO_PATCH_INFO_IMAGE;
3423 ji->data.image = acfg->image;
3425 get_shared_got_offset (acfg, ji);
3427 for (l = acfg->method_order; l != NULL; l = l->next) {
3428 i = GPOINTER_TO_UINT (l->data);
3430 if (acfg->cfgs [i]) {
3431 MonoCompile *cfg = acfg->cfgs [i];
3433 for (ji = cfg->patch_info; ji; ji = ji->next) {
3435 case MONO_PATCH_INFO_VTABLE:
3436 case MONO_PATCH_INFO_CLASS:
3437 case MONO_PATCH_INFO_IID:
3438 case MONO_PATCH_INFO_ADJUSTED_IID:
3439 case MONO_PATCH_INFO_FIELD:
3440 case MONO_PATCH_INFO_SFLDA:
3441 case MONO_PATCH_INFO_DECLSEC:
3442 case MONO_PATCH_INFO_LDTOKEN:
3443 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
3444 case MONO_PATCH_INFO_RVA:
3445 get_shared_got_offset (acfg, ji);
3456 emit_code (MonoAotCompile *acfg)
3462 symbol = g_strdup_printf ("methods");
3463 emit_section_change (acfg, ".text", 0);
3464 emit_global (acfg, symbol, TRUE);
3465 emit_alignment (acfg, 8);
3466 emit_label (acfg, symbol);
3468 for (l = acfg->method_order; l != NULL; l = l->next) {
3469 i = GPOINTER_TO_UINT (l->data);
3472 emit_method_code (acfg, acfg->cfgs [i]);
3475 symbol = g_strdup_printf ("methods_end");
3476 emit_section_change (acfg, ".text", 0);
3477 emit_global (acfg, symbol, FALSE);
3478 emit_alignment (acfg, 8);
3479 emit_label (acfg, symbol);
3481 symbol = g_strdup_printf ("method_offsets");
3482 emit_section_change (acfg, ".text", 1);
3483 emit_global (acfg, symbol, FALSE);
3484 emit_alignment (acfg, 8);
3485 emit_label (acfg, symbol);
3487 for (i = 0; i < acfg->nmethods; ++i) {
3488 if (acfg->cfgs [i]) {
3489 symbol = g_strdup_printf (".Lm_%x", i);
3490 emit_symbol_diff (acfg, symbol, "methods", 0);
3492 emit_int32 (acfg, 0xffffffff);
3499 emit_info (MonoAotCompile *acfg)
3505 /* Emit method info */
3506 symbol = g_strdup_printf ("method_info");
3507 emit_section_change (acfg, ".text", 1);
3508 emit_global (acfg, symbol, FALSE);
3509 emit_alignment (acfg, 8);
3510 emit_label (acfg, symbol);
3512 /* To reduce size of generated assembly code */
3513 symbol = g_strdup_printf ("mi");
3514 emit_label (acfg, symbol);
3516 for (l = acfg->method_order; l != NULL; l = l->next) {
3517 i = GPOINTER_TO_UINT (l->data);
3520 emit_method_info (acfg, acfg->cfgs [i]);
3523 symbol = g_strdup_printf ("method_info_offsets");
3524 emit_section_change (acfg, ".text", 1);
3525 emit_global (acfg, symbol, FALSE);
3526 emit_alignment (acfg, 8);
3527 emit_label (acfg, symbol);
3529 for (i = 0; i < acfg->nmethods; ++i) {
3530 if (acfg->cfgs [i]) {
3531 symbol = g_strdup_printf (".Lm_%x_p", i);
3532 emit_symbol_diff (acfg, symbol, "mi", 0);
3534 emit_int32 (acfg, 0);
3541 emit_wrapper_info (MonoAotCompile *acfg)
3547 /* Emit method info */
3548 symbol = g_strdup_printf ("wrapper_info");
3549 emit_section_change (acfg, ".text", 1);
3550 emit_global (acfg, symbol, FALSE);
3551 emit_alignment (acfg, 8);
3552 emit_label (acfg, symbol);
3554 if (!acfg->aot_opts.full_aot)
3557 for (i = 0; i < acfg->nmethods; ++i) {
3558 MonoCompile *cfg = acfg->cfgs [i];
3560 if (!cfg || !cfg->method->wrapper_type)
3563 index = get_method_index (acfg, cfg->method);
3565 // FIXME: Optimize disk usage and lookup speed
3566 name = mono_method_full_name (cfg->method, TRUE);
3567 emit_string (acfg, name);
3568 emit_alignment (acfg, 4);
3569 emit_int32 (acfg, index);
3572 emit_byte (acfg, 0);
3578 emit_method_order (MonoAotCompile *acfg)
3584 symbol = g_strdup_printf ("method_order");
3585 emit_section_change (acfg, ".text", 1);
3586 emit_global (acfg, symbol, FALSE);
3587 emit_alignment (acfg, 8);
3588 emit_label (acfg, symbol);
3590 /* First emit an index table */
3593 for (l = acfg->method_order; l != NULL; l = l->next) {
3594 i = GPOINTER_TO_UINT (l->data);
3596 if (acfg->cfgs [i]) {
3597 if ((index % 1024) == 0) {
3598 emit_int32 (acfg, i);
3606 emit_int32 (acfg, 0xffffff);
3608 /* Then emit the whole method order */
3609 for (l = acfg->method_order; l != NULL; l = l->next) {
3610 i = GPOINTER_TO_UINT (l->data);
3612 if (acfg->cfgs [i]) {
3613 emit_int32 (acfg, i);
3618 symbol = g_strdup_printf ("method_order_end");
3619 emit_section_change (acfg, ".text", 1);
3620 emit_global (acfg, symbol, FALSE);
3621 emit_label (acfg, symbol);
3625 emit_exception_info (MonoAotCompile *acfg)
3630 symbol = g_strdup_printf ("ex_info");
3631 emit_section_change (acfg, ".text", 1);
3632 emit_global (acfg, symbol, FALSE);
3633 emit_alignment (acfg, 8);
3634 emit_label (acfg, symbol);
3636 /* To reduce size of generated assembly */
3637 symbol = g_strdup_printf ("ex");
3638 emit_label (acfg, symbol);
3640 for (i = 0; i < acfg->nmethods; ++i) {
3642 emit_exception_debug_info (acfg, acfg->cfgs [i]);
3645 symbol = g_strdup_printf ("ex_info_offsets");
3646 emit_section_change (acfg, ".text", 1);
3647 emit_global (acfg, symbol, FALSE);
3648 emit_alignment (acfg, 8);
3649 emit_label (acfg, symbol);
3651 for (i = 0; i < acfg->nmethods; ++i) {
3652 if (acfg->cfgs [i]) {
3653 symbol = g_strdup_printf (".Le_%x_p", i);
3654 emit_symbol_diff (acfg, symbol, "ex", 0);
3656 emit_int32 (acfg, 0);
3663 emit_class_info (MonoAotCompile *acfg)
3668 symbol = g_strdup_printf ("class_info");
3669 emit_section_change (acfg, ".text", 1);
3670 emit_global (acfg, symbol, FALSE);
3671 emit_alignment (acfg, 8);
3672 emit_label (acfg, symbol);
3674 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
3675 emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
3677 symbol = g_strdup_printf ("class_info_offsets");
3678 emit_section_change (acfg, ".text", 1);
3679 emit_global (acfg, symbol, FALSE);
3680 emit_alignment (acfg, 8);
3681 emit_label (acfg, symbol);
3683 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3684 symbol = g_strdup_printf (".LK_I_%x", i);
3685 emit_symbol_diff (acfg, symbol, "class_info", 0);
3690 typedef struct ClassNameTableEntry {
3691 guint32 token, index;
3692 struct ClassNameTableEntry *next;
3693 } ClassNameTableEntry;
3696 emit_class_name_table (MonoAotCompile *acfg)
3699 guint32 token, hash;
3704 ClassNameTableEntry *entry, *new_entry;
3707 * Construct a chained hash table for mapping class names to typedef tokens.
3709 table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
3710 table = g_ptr_array_sized_new (table_size);
3711 for (i = 0; i < table_size; ++i)
3712 g_ptr_array_add (table, NULL);
3713 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3714 token = MONO_TOKEN_TYPE_DEF | (i + 1);
3715 klass = mono_class_get (acfg->image, token);
3716 full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
3717 hash = g_str_hash (full_name) % table_size;
3720 /* FIXME: Allocate from the mempool */
3721 new_entry = g_new0 (ClassNameTableEntry, 1);
3722 new_entry->token = token;
3724 entry = g_ptr_array_index (table, hash);
3725 if (entry == NULL) {
3726 new_entry->index = hash;
3727 g_ptr_array_index (table, hash) = new_entry;
3730 entry = entry->next;
3732 entry->next = new_entry;
3733 new_entry->index = table->len;
3734 g_ptr_array_add (table, new_entry);
3738 /* Emit the table */
3739 symbol = g_strdup_printf ("class_name_table");
3740 emit_section_change (acfg, ".text", 0);
3741 emit_global (acfg, symbol, FALSE);
3742 emit_alignment (acfg, 8);
3743 emit_label (acfg, symbol);
3745 /* FIXME: Optimize memory usage */
3746 g_assert (table_size < 65000);
3747 emit_int16 (acfg, table_size);
3748 g_assert (table->len < 65000);
3749 for (i = 0; i < table->len; ++i) {
3750 ClassNameTableEntry *entry = g_ptr_array_index (table, i);
3752 if (entry == NULL) {
3753 emit_int16 (acfg, 0);
3754 emit_int16 (acfg, 0);
3756 emit_int16 (acfg, mono_metadata_token_index (entry->token));
3758 emit_int16 (acfg, entry->next->index);
3760 emit_int16 (acfg, 0);
3766 emit_image_table (MonoAotCompile *acfg)
3772 * The image table is small but referenced in a lot of places.
3773 * So we emit it at once, and reference its elements by an index.
3776 symbol = g_strdup_printf ("mono_image_table");
3777 emit_section_change (acfg, ".text", 1);
3778 emit_global (acfg, symbol, FALSE);
3779 emit_alignment (acfg, 8);
3780 emit_label (acfg, symbol);
3781 emit_int32 (acfg, acfg->image_table->len);
3782 for (i = 0; i < acfg->image_table->len; i++) {
3783 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
3784 MonoAssemblyName *aname = &image->assembly->aname;
3786 /* FIXME: Support multi-module assemblies */
3787 g_assert (image->assembly->image == image);
3789 emit_string (acfg, image->assembly_name);
3790 emit_string (acfg, image->guid);
3791 emit_string (acfg, aname->culture ? aname->culture : "");
3792 emit_string (acfg, (const char*)aname->public_key_token);
3794 emit_alignment (acfg, 8);
3795 emit_int32 (acfg, aname->flags);
3796 emit_int32 (acfg, aname->major);
3797 emit_int32 (acfg, aname->minor);
3798 emit_int32 (acfg, aname->build);
3799 emit_int32 (acfg, aname->revision);
3804 emit_got_info (MonoAotCompile *acfg)
3809 guint32 *got_info_offsets;
3813 * - optimize offsets table.
3814 * - reduce number of exported symbols.
3815 * - emit info for a klass only once.
3816 * - determine when a method uses a GOT slot which is guaranteed to be already
3818 * - clean up and document the code.
3819 * - use String.Empty in class libs.
3822 /* Encode info required to decode shared GOT entries */
3823 buf_size = acfg->shared_patches->len * 64;
3824 p = buf = mono_mempool_alloc (acfg->mempool, buf_size);
3825 got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->shared_patches->len * sizeof (guint32));
3826 for (i = 0; i < acfg->shared_patches->len; ++i) {
3827 MonoJumpInfo *ji = g_ptr_array_index (acfg->shared_patches, i);
3829 /* No need to encode the patch type */
3830 got_info_offsets [i] = p - buf;
3831 encode_patch (acfg, ji, p, &p, FALSE);
3834 g_assert (p - buf <= buf_size);
3836 acfg->stats.got_info_size = p - buf;
3838 /* Emit got_info table */
3839 symbol = g_strdup_printf ("got_info");
3840 emit_section_change (acfg, ".text", 1);
3841 emit_global (acfg, symbol, FALSE);
3842 emit_alignment (acfg, 8);
3843 emit_label (acfg, symbol);
3845 emit_bytes (acfg, buf, p - buf);
3847 /* Emit got_info_offsets table */
3848 symbol = g_strdup_printf ("got_info_offsets");
3849 emit_section_change (acfg, ".text", 1);
3850 emit_global (acfg, symbol, FALSE);
3851 emit_alignment (acfg, 8);
3852 emit_label (acfg, symbol);
3854 for (i = 0; i < acfg->shared_patches->len; ++i)
3855 emit_int32 (acfg, got_info_offsets [i]);
3857 acfg->stats.got_info_offsets_size = acfg->shared_patches->len * 4;
3861 emit_got (MonoAotCompile *acfg)
3865 /* Don't make GOT global so accesses to it don't need relocations */
3866 symbol = g_strdup_printf ("got");
3867 emit_section_change (acfg, ".bss", 1);
3868 emit_alignment (acfg, 8);
3869 emit_label (acfg, symbol);
3870 if ((acfg->got_offset + acfg->num_trampoline_got_entries) > 0)
3871 emit_zero_bytes (acfg, (int)((acfg->got_offset + acfg->num_trampoline_got_entries) * sizeof (gpointer)));
3873 symbol = g_strdup_printf ("got_addr");
3874 emit_section_change (acfg, ".data", 1);
3875 emit_global (acfg, symbol, FALSE);
3876 emit_alignment (acfg, 8);
3877 emit_label (acfg, symbol);
3878 emit_pointer (acfg, "got");
3880 symbol = g_strdup_printf ("got_size");
3881 emit_section_change (acfg, ".data", 1);
3882 emit_global (acfg, symbol, FALSE);
3883 emit_alignment (acfg, 8);
3884 emit_label (acfg, symbol);
3885 emit_int32 (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
3889 emit_globals (MonoAotCompile *acfg)
3893 emit_string_symbol (acfg, "mono_assembly_guid" , acfg->image->guid);
3895 emit_string_symbol (acfg, "mono_aot_version", MONO_AOT_FILE_VERSION);
3897 opts_str = g_strdup_printf ("%d", acfg->opts);
3898 emit_string_symbol (acfg, "mono_aot_opt_flags", opts_str);
3901 emit_string_symbol (acfg, "mono_aot_full_aot", acfg->aot_opts.full_aot ? "TRUE" : "FALSE");
3903 if (acfg->aot_opts.bind_to_runtime_version)
3904 emit_string_symbol (acfg, "mono_runtime_version", FULL_VERSION);
3906 emit_string_symbol (acfg, "mono_runtime_version", "");
3910 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
3912 MonoImage *image = ass->image;
3915 MonoAotCompile *acfg;
3917 printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
3919 acfg = g_new0 (MonoAotCompile, 1);
3920 acfg->methods = g_ptr_array_new ();
3921 acfg->method_indexes = g_hash_table_new (NULL, NULL);
3922 acfg->plt_offset_to_patch = g_hash_table_new (NULL, NULL);
3923 acfg->patch_to_plt_offset = g_hash_table_new (NULL, NULL);
3924 acfg->patch_to_plt_offset_wrapper = g_malloc0 (sizeof (GHashTable*) * 128);
3925 acfg->patch_to_shared_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
3926 acfg->shared_patches = g_ptr_array_new ();
3927 acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
3928 acfg->token_info_hash = g_hash_table_new (NULL, NULL);
3929 acfg->image_hash = g_hash_table_new (NULL, NULL);
3930 acfg->image_table = g_ptr_array_new ();
3931 acfg->image = image;
3933 acfg->mempool = mono_mempool_new ();
3935 mono_aot_parse_options (aot_options, &acfg->aot_opts);
3937 load_profile_files (acfg);
3941 acfg->num_aot_trampolines = acfg->aot_opts.full_aot ? 1024 : 0;
3943 acfg->method_index = 1;
3945 /* Collect methods */
3946 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
3948 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3950 method = mono_get_method (acfg->image, token, NULL);
3952 if (acfg->aot_opts.full_aot && method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
3953 /* Compile the wrapper instead */
3954 /* We do this here instead of add_wrappers () because it is easy to do it here */
3955 MonoMethod *wrapper = mono_marshal_get_native_wrapper (method, check_for_pending_exc);
3959 /* Since we add the normal methods first, their index will be equal to their zero based token index */
3960 add_method_with_index (acfg, method, i);
3961 acfg->method_index ++;
3964 if (acfg->aot_opts.full_aot)
3965 add_wrappers (acfg);
3967 acfg->nmethods = acfg->methods->len;
3968 acfg->cfgs = g_new0 (MonoCompile*, acfg->nmethods + 32);
3969 acfg->method_got_offsets = g_new0 (guint32, acfg->nmethods + 32);
3971 /* PLT offset 0 is reserved for the PLT trampoline */
3972 acfg->plt_offset = 1;
3974 /* Compile methods */
3975 for (i = 0; i < acfg->methods->len; ++i) {
3976 compile_method (acfg, g_ptr_array_index (acfg->methods, i));
3979 alloc_got_slots (acfg);
3985 emit_wrapper_info (acfg);
3987 emit_method_order (acfg);
3989 emit_trampolines (acfg);
3991 emit_class_name_table (acfg);
3993 emit_got_info (acfg);
3995 emit_exception_info (acfg);
3997 emit_class_info (acfg);
4001 emit_image_table (acfg);
4005 emit_globals (acfg);
4007 symbol = g_strdup_printf ("mem_end");
4008 emit_section_change (acfg, ".text", 1);
4009 emit_global (acfg, symbol, FALSE);
4010 emit_alignment (acfg, 8);
4011 emit_label (acfg, symbol);
4013 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)));
4015 res = emit_writeout (acfg);
4019 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);
4020 printf ("%d methods are generic (%d%%)\n", acfg->stats.genericcount, acfg->stats.mcount ? (acfg->stats.genericcount * 100) / acfg->stats.mcount : 100);
4021 printf ("%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
4022 printf ("%d methods contain wrapper references (%d%%)\n", acfg->stats.wrappercount, acfg->stats.mcount ? (acfg->stats.wrappercount * 100) / acfg->stats.mcount : 100);
4023 printf ("%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
4024 printf ("%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
4025 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);
4026 printf ("Direct calls: %d (%d%%)\n", acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
4028 printf ("GOT slot distribution:\n");
4029 for (i = 0; i < MONO_PATCH_INFO_NONE; ++i)
4030 if (acfg->stats.got_slot_types [i])
4031 printf ("\t%s: %d\n", get_patch_name (i), acfg->stats.got_slot_types [i]);
4041 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)