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>
16 #ifndef PLATFORM_WIN32
25 #include <limits.h> /* for PAGESIZE */
30 #include <mono/metadata/tabledefs.h>
31 #include <mono/metadata/class.h>
32 #include <mono/metadata/object.h>
33 #include <mono/metadata/tokentype.h>
34 #include <mono/metadata/appdomain.h>
35 #include <mono/metadata/debug-helpers.h>
36 #include <mono/metadata/assembly.h>
37 #include <mono/metadata/metadata-internals.h>
38 #include <mono/metadata/marshal.h>
39 #include <mono/utils/mono-logger.h>
40 #include "mono/utils/mono-compiler.h"
47 #define SHARED_EXT ".dll"
48 #elif defined(__ppc__) && defined(__MACH__)
49 #define SHARED_EXT ".dylib"
51 #define SHARED_EXT ".so"
54 #if defined(sparc) || defined(__ppc__)
55 #define AS_STRING_DIRECTIVE ".asciz"
58 #define AS_STRING_DIRECTIVE ".string"
61 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
62 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
64 typedef struct MonoAotOptions {
67 gboolean write_symbols;
68 gboolean metadata_only;
71 typedef struct MonoAotStats {
72 int ccount, mcount, lmfcount, abscount, wrappercount, gcount, ocount;
73 int code_size, info_size, ex_info_size, got_size, class_info_size, got_info_size, got_info_offsets_size;
74 int methods_without_got_slots, direct_calls, all_calls;
76 int got_slot_types [MONO_PATCH_INFO_NONE];
79 /*#define USE_ELF_WRITER 1*/
81 #if defined(USE_ELF_WRITER)
82 #define USE_BIN_WRITER 1
87 typedef struct _BinSymbol BinSymbol;
88 typedef struct _BinReloc BinReloc;
89 typedef struct _BinSection BinSection;
103 typedef struct MonoAotCompile {
106 GHashTable *patch_to_plt_offset;
107 GHashTable *plt_offset_to_patch;
108 GHashTable *patch_to_shared_got_offset;
109 GPtrArray *shared_patches;
110 GHashTable *image_hash;
111 GHashTable *method_to_cfg;
112 GPtrArray *image_table;
114 guint32 got_offset, plt_offset;
115 guint32 *method_got_offsets;
116 gboolean *has_got_slots;
117 MonoAotOptions aot_opts;
120 MonoMemPool *mempool;
122 #ifdef USE_BIN_WRITER
124 BinSection *sections;
125 BinSection *cur_section;
126 BinReloc *relocations;
132 int mode; /* emit mode */
133 int col_count; /* bytes emitted per .byte line */
137 #ifdef HAVE_ARRAY_ELEM_INIT
138 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
139 #define MSGSTRFIELD1(line) str##line
140 static const struct msgstr_t {
141 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
142 #include "patch-info.h"
145 #define PATCH_INFO(a,b) b,
146 #include "patch-info.h"
149 static const gint16 opidx [] = {
150 #define PATCH_INFO(a,b) [MONO_PATCH_INFO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
151 #include "patch-info.h"
156 get_patch_name (int info)
158 return (const char*)&opstr + opidx [info];
162 #define PATCH_INFO(a,b) b,
163 static const char* const
164 patch_types [MONO_PATCH_INFO_NUM + 1] = {
165 #include "patch-info.h"
170 get_patch_name (int info)
172 return patch_types [info];
178 is_got_patch (MonoJumpInfoType patch_type)
182 #elif defined(__i386__)
189 #if defined(__ppc__) && defined(__MACH__)
191 ilog2(register int value)
194 while (value & ~0xf) count += 4, value >>= 4;
195 while (value) count++, value >>= 1;
200 #ifdef USE_BIN_WRITER
202 typedef struct _BinLabel BinLabel;
213 BinSection *val2_section;
225 gboolean is_function;
243 emit_start (MonoAotCompile *acfg)
245 acfg->labels = g_hash_table_new (g_str_hash, g_str_equal);
249 emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
253 if (acfg->cur_section && acfg->cur_section->subsection == subsection_index
254 && strcmp (acfg->cur_section->name, section_name) == 0)
256 for (section = acfg->sections; section; section = section->next) {
257 if (section->subsection == subsection_index && strcmp (section->name, section_name) == 0) {
258 acfg->cur_section = section;
263 section = g_new0 (BinSection, 1);
264 section->name = g_strdup (section_name);
265 section->subsection = subsection_index;
266 section->next = acfg->sections;
267 acfg->sections = section;
268 acfg->cur_section = section;
273 emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
275 BinSymbol *symbol = g_new0 (BinSymbol, 1);
276 symbol->name = g_strdup (name);
277 symbol->is_function = func;
278 symbol->is_global = TRUE;
279 symbol->section = acfg->cur_section;
280 /* FIXME: we align after this call... */
281 symbol->offset = symbol->section->cur_offset;
282 symbol->next = acfg->symbols;
283 acfg->symbols = symbol;
287 emit_label (MonoAotCompile *acfg, const char *name)
289 BinLabel *label = g_new0 (BinLabel, 1);
290 label->name = g_strdup (name);
291 label->section = acfg->cur_section;
292 label->offset = acfg->cur_section->cur_offset;
293 g_hash_table_insert (acfg->labels, label->name, label);
297 emit_ensure_buffer (BinSection *section, int size)
299 int new_offset = section->cur_offset + size;
300 if (new_offset >= section->data_len) {
301 int new_size = section->data_len? section->data_len * 2: 256;
303 while (new_size <= new_offset)
305 data = g_malloc0 (new_size);
306 memcpy (data, section->data, section->data_len);
307 g_free (section->data);
308 section->data = data;
309 section->data_len = new_size;
314 emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
316 emit_ensure_buffer (acfg->cur_section, size);
317 memcpy (acfg->cur_section->data + acfg->cur_section->cur_offset, buf, size);
318 acfg->cur_section->cur_offset += size;
322 emit_string (MonoAotCompile *acfg, const char *value)
324 int size = strlen (value) + 1;
325 emit_bytes (acfg, (const guint8*)value, size);
329 emit_line (MonoAotCompile *acfg)
331 /* Nothing to do in binary writer */
335 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
337 emit_section_change (acfg, ".text", 1);
338 emit_global (acfg, name, FALSE);
339 emit_label (acfg, name);
340 emit_string (acfg, value);
344 emit_alignment (MonoAotCompile *acfg, int size)
346 int offset = acfg->cur_section->cur_offset;
348 offset += (size - 1);
349 offset &= ~(size - 1);
350 add = offset - acfg->cur_section->cur_offset;
352 emit_ensure_buffer (acfg->cur_section, add);
353 acfg->cur_section->cur_offset += add;
358 emit_pointer (MonoAotCompile *acfg, const char *target)
361 emit_alignment (acfg, sizeof (gpointer));
362 reloc = g_new0 (BinReloc, 1);
363 reloc->val1 = g_strdup (target);
364 reloc->section = acfg->cur_section;
365 reloc->section_offset = acfg->cur_section->cur_offset;
366 reloc->next = acfg->relocations;
367 acfg->relocations = reloc;
368 if (strcmp (reloc->section->name, ".data") == 0) {
370 g_print ("reloc: %s at %d\n", target, acfg->cur_section->cur_offset);
372 acfg->cur_section->cur_offset += sizeof (gpointer);
376 emit_int16 (MonoAotCompile *acfg, int value)
379 emit_ensure_buffer (acfg->cur_section, 2);
380 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
381 acfg->cur_section->cur_offset += 2;
382 /* FIXME: little endian */
384 data [1] = value >> 8;
388 emit_int32 (MonoAotCompile *acfg, int value)
391 emit_ensure_buffer (acfg->cur_section, 4);
392 data = acfg->cur_section->data + acfg->cur_section->cur_offset;
393 acfg->cur_section->cur_offset += 4;
394 /* FIXME: little endian */
396 data [1] = value >> 8;
397 data [2] = value >> 16;
398 data [3] = value >> 24;
402 emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
405 reloc = g_new0 (BinReloc, 1);
406 reloc->val1 = g_strdup (end);
407 if (strcmp (start, ".") == 0) {
408 reloc->val2_section = acfg->cur_section;
409 reloc->val2_offset = acfg->cur_section->cur_offset;
411 reloc->val2 = g_strdup (start);
413 reloc->offset = offset;
414 reloc->section = acfg->cur_section;
415 reloc->section_offset = acfg->cur_section->cur_offset;
416 reloc->next = acfg->relocations;
417 acfg->relocations = reloc;
418 acfg->cur_section->cur_offset += 4;
419 /*if (strcmp (reloc->section->name, ".data") == 0) {
421 g_print ("reloc: %s - %s + %d at %d\n", end, start, offset, acfg->cur_section->cur_offset - 4);
426 emit_zero_bytes (MonoAotCompile *acfg, int num)
428 emit_ensure_buffer (acfg->cur_section, num);
429 acfg->cur_section->cur_offset += num;
432 #ifdef USE_ELF_WRITER
467 DYN_RELCOUNT = 0x6ffffffa
470 static const char* section_names [] = {
486 static const guint8 section_type [] = {
491 static const guint8 section_link [] = {
492 0, 2, 3, 0, 2, 0, 3, 0, 0, 0, 0, 12, 0
495 static const guint8 section_esize [] = {
496 0, 4, 16, 0, 8, 0, 8, 4, 0, 0, 0, 16, 0
499 static const guint8 section_flags [] = {
500 0, 2, 2, 2, 2, 6, 3, 3, 3, 3, 0, 0, 0
503 static const guint16 section_align [] = {
504 0, 4, 4, 1, 4, 4096, 4, 4, 8, 8, 1, 4, 1
524 struct ElfSectHeader {
537 #if SIZEOF_VOID_P == 4
539 struct ElfProgHeader {
571 struct ElfProgHeader {
615 str_table_add (ElfStrTable *table, const char* value)
619 table->data = g_string_new_len ("", 1);
620 table->hash = g_hash_table_new (g_str_hash, g_str_equal);
622 idx = GPOINTER_TO_UINT (g_hash_table_lookup (table->hash, value));
625 idx = table->data->len;
626 g_string_append (table->data, value);
627 g_string_append_c (table->data, 0);
628 g_hash_table_insert (table->hash, (void*)value, GUINT_TO_POINTER (idx));
633 append_subsection (MonoAotCompile *acfg, struct ElfSectHeader *sheaders, BinSection *sect, BinSection *add)
635 int offset = sect->cur_offset;
636 /*offset += (sheaders [sect->shidx].sh_addralign - 1);
637 offset &= ~(sheaders [sect->shidx].sh_addralign - 1);*/
640 emit_ensure_buffer (sect, offset);
641 g_print ("section %s aligned to %d from %d\n", sect->name, offset, sect->cur_offset);
642 sect->cur_offset = offset;
644 emit_ensure_buffer (sect, add->cur_offset);
645 memcpy (sect->data + sect->cur_offset, add->data, add->cur_offset);
647 sect->cur_offset += add->cur_offset;
648 add->cur_offset = offset; /* it becomes the offset in the parent section */
649 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);
654 /* merge the subsections */
656 collect_sections (MonoAotCompile *acfg, struct ElfSectHeader *sheaders, BinSection **out, int num)
658 int i, j, maxs, num_sections;
663 for (sect = acfg->sections; sect; sect = sect->next) {
664 if (sect->subsection == 0) {
665 out [num_sections++] = sect;
666 g_assert (num_sections < num);
667 if (strcmp (sect->name, ".text") == 0) {
668 sect->shidx = SECT_TEXT;
669 } else if (strcmp (sect->name, ".data") == 0) {
670 sect->shidx = SECT_DATA;
671 } else if (strcmp (sect->name, ".bss") == 0) {
672 sect->shidx = SECT_BSS;
675 maxs = MAX (maxs, sect->subsection);
677 for (i = 0; i < num_sections; i++) {
678 for (j = 1; j <= maxs; ++j) {
679 for (sect = acfg->sections; sect; sect = sect->next) {
680 if (sect->subsection == j && strcmp (out [i]->name, sect->name) == 0) {
681 append_subsection (acfg, sheaders, out [i], sect);
690 elf_hash (const unsigned char *name)
692 unsigned long h = 0, g;
694 h = (h << 4) + *name++;
695 if ((g = h & 0xf0000000))
702 #define NUM_BUCKETS 17
705 build_hash (MonoAotCompile *acfg, int num_sections, ElfStrTable *dynstr)
708 int num_symbols = 1 + num_sections + 3;
711 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
712 if (!symbol->is_global)
715 str_table_add (dynstr, symbol->name);
716 /*g_print ("adding sym: %s\n", symbol->name);*/
718 str_table_add (dynstr, "__bss_start");
719 str_table_add (dynstr, "_edata");
720 str_table_add (dynstr, "_end");
722 data = g_new0 (int, num_symbols + 2 + NUM_BUCKETS);
723 data [0] = NUM_BUCKETS;
724 data [1] = num_symbols;
730 get_label_addr (MonoAotCompile *acfg, const char *name)
737 lab = g_hash_table_lookup (acfg->labels, name);
738 section = lab->section;
739 offset = lab->offset;
740 if (section->parent) {
741 value = section->parent->file_offset + section->cur_offset + offset;
743 value = section->file_offset + offset;
749 collect_syms (MonoAotCompile *acfg, int *hash, ElfStrTable *strtab, struct ElfSectHeader *sheaders, int *num_syms)
760 symbols = g_new0 (ElfSymbol, hash [1]);
762 symbols = g_new0 (ElfSymbol, *num_syms + SECT_NUM + 10); /* FIXME */
764 /* the first symbol is undef, all zeroes */
768 for (j = 1; j < SECT_NUM; ++j) {
769 symbols [i].st_info = SYM_LOCAL | SYM_SECTION;
770 symbols [i].st_shndx = j;
771 symbols [i].st_value = sheaders [j].sh_addr;
775 for (section = acfg->sections; section; section = section->next) {
778 symbols [i].st_info = SYM_LOCAL | SYM_SECTION;
779 if (strcmp (section->name, ".text") == 0) {
780 symbols [i].st_shndx = SECT_TEXT;
781 section->shidx = SECT_TEXT;
782 section->file_offset = 4096;
783 symbols [i].st_value = section->file_offset;
784 } else if (strcmp (section->name, ".data") == 0) {
785 symbols [i].st_shndx = SECT_DATA;
786 section->shidx = SECT_DATA;
787 section->file_offset = 4096 + 28; /* FIXME */
788 symbols [i].st_value = section->file_offset;
789 } else if (strcmp (section->name, ".bss") == 0) {
790 symbols [i].st_shndx = SECT_BSS;
791 section->shidx = SECT_BSS;
792 section->file_offset = 4096 + 28 + 8; /* FIXME */
793 symbols [i].st_value = section->file_offset;
798 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
801 if (!symbol->is_global)
803 symbols [i].st_info = (symbol->is_function? SYM_FUNC : SYM_OBJECT) | SYM_GLOBAL;
804 symbols [i].st_name = str_table_add (strtab, symbol->name);
805 /*g_print ("sym name %s tabled to %d\n", symbol->name, symbols [i].st_name);*/
806 section = symbol->section;
807 symbols [i].st_shndx = section->parent? section->parent->shidx: section->shidx;
808 lab = g_hash_table_lookup (acfg->labels, symbol->name);
809 offset = lab->offset;
810 if (section->parent) {
811 symbols [i].st_value = section->parent->file_offset + section->cur_offset + offset;
813 symbols [i].st_value = section->file_offset + offset;
817 /* add special symbols */
818 symbols [i].st_name = str_table_add (strtab, "__bss_start");
819 symbols [i].st_shndx = 0xfff1;
820 symbols [i].st_info = SYM_GLOBAL;
822 symbols [i].st_name = str_table_add (strtab, "_edata");
823 symbols [i].st_shndx = 0xfff1;
824 symbols [i].st_info = SYM_GLOBAL;
826 symbols [i].st_name = str_table_add (strtab, "_end");
827 symbols [i].st_shndx = 0xfff1;
828 symbols [i].st_info = SYM_GLOBAL;
834 /* add to hash table */
837 chain = hash + 2 + hash [0];
838 for (i = 0; i < hash [1]; ++i) {
840 /*g_print ("checking %d '%s' (sym %d)\n", symbols [i].st_name, strtab->data->str + symbols [i].st_name, i);*/
841 if (!symbols [i].st_name)
843 hashc = elf_hash ((guint8*)strtab->data->str + symbols [i].st_name);
844 slot = hashc % hash [0];
845 /*g_print ("hashing '%s' at slot %d (sym %d)\n", strtab->data->str + symbols [i].st_name, slot, i);*/
847 chain [i] = bucket [slot];
858 reloc_symbols (MonoAotCompile *acfg, ElfSymbol *symbols, struct ElfSectHeader *sheaders, ElfStrTable *strtab, gboolean dynamic)
866 for (section = acfg->sections; section; section = section->next) {
869 symbols [i].st_value = sheaders [section->shidx].sh_addr;
873 for (i = 1; i < SECT_NUM; ++i) {
874 symbols [i].st_value = sheaders [i].sh_addr;
877 for (symbol = acfg->symbols; symbol; symbol = symbol->next) {
880 if (dynamic && !symbol->is_global)
882 section = symbol->section;
883 lab = g_hash_table_lookup (acfg->labels, symbol->name);
884 offset = lab->offset;
885 if (section->parent) {
886 symbols [i].st_value = sheaders [section->parent->shidx].sh_addr + section->cur_offset + offset;
888 symbols [i].st_value = sheaders [section->shidx].sh_addr + offset;
893 symbols [i].st_value = sheaders [SECT_BSS].sh_addr;
896 symbols [i].st_value = sheaders [SECT_DATA].sh_addr + sheaders [SECT_DATA].sh_size;
899 symbols [i].st_value = sheaders [SECT_BSS].sh_addr + sheaders [SECT_BSS].sh_size;
904 resolve_relocations (MonoAotCompile *acfg)
908 gsize end_val, start_val;
913 rr = g_new0 (ElfReloc, acfg->num_relocs);
916 for (reloc = acfg->relocations; reloc; reloc = reloc->next) {
917 end_val = get_label_addr (acfg, reloc->val1);
919 start_val = get_label_addr (acfg, reloc->val2);
920 } else if (reloc->val2_section) {
921 start_val = reloc->val2_offset;
922 if (reloc->val2_section->parent)
923 start_val += reloc->val2_section->parent->file_offset + reloc->val2_section->cur_offset;
925 start_val += reloc->val2_section->file_offset;
929 end_val = end_val - start_val + reloc->offset;
930 if (reloc->section->parent) {
931 data = reloc->section->parent->data;
932 data += reloc->section->cur_offset;
933 data += reloc->section_offset;
934 vaddr = reloc->section->parent->file_offset;
935 vaddr += reloc->section->cur_offset;
936 vaddr += reloc->section_offset;
938 data = reloc->section->data;
939 data += reloc->section_offset;
940 vaddr = reloc->section->file_offset;
941 vaddr += reloc->section_offset;
943 /* FIXME: little endian */
945 data [1] = end_val >> 8;
946 data [2] = end_val >> 16;
947 data [3] = end_val >> 24;
948 if (start_val == 0) {
950 rr [i].value = 8; /* FIXME: 386_RELATIVE */
952 g_assert (i <= acfg->num_relocs);
959 emit_writeout (MonoAotCompile *acfg)
961 char *outfile_name, *tmp_outfile_name;
963 struct ElfHeader header;
964 struct ElfProgHeader progh [3];
965 struct ElfSectHeader secth [SECT_NUM];
967 ElfStrTable str_table = {NULL, NULL};
968 ElfStrTable sh_str_table = {NULL, NULL};
969 ElfStrTable dyn_str_table = {NULL, NULL};
970 BinSection* sections [6];
971 BinSection *text_section = NULL, *data_section = NULL, *bss_section = NULL;
974 ElfDynamic dynamic [14];
976 int i, num_sections, file_offset, virt_offset, size, num_symtab;
979 if (acfg->aot_opts.outfile)
980 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
982 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
984 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
986 unlink (tmp_outfile_name);
987 file = fopen (tmp_outfile_name, "w");
990 /* Section headers */
991 memset (§h, 0, sizeof (secth));
992 memset (&dynamic, 0, sizeof (dynamic));
993 memset (&header, 0, sizeof (header));
995 for (i = 1; i < SECT_NUM; ++i) {
996 secth [i].sh_name = str_table_add (&sh_str_table, section_names [i]);
997 secth [i].sh_type = section_type [i];
998 secth [i].sh_link = section_link [i];
999 secth [i].sh_addralign = section_align [i];
1000 secth [i].sh_flags = section_flags [i];
1001 secth [i].sh_entsize = section_esize [i];
1003 secth [SECT_DYNSYM].sh_info = 4;
1004 secth [SECT_SYMTAB].sh_info = 20;
1006 num_sections = collect_sections (acfg, secth, sections, 6);
1007 hash = build_hash (acfg, num_sections, &dyn_str_table);
1008 num_symtab = hash [1]; /* FIXME */
1009 g_print ("num_sections: %d\n", num_sections);
1010 g_print ("dynsym: %d, dynstr size: %d\n", hash [1], dyn_str_table.data->len);
1011 for (i = 0; i < num_sections; ++i) {
1012 g_print ("section %s, size: %d, %x\n", sections [i]->name, sections [i]->cur_offset, sections [i]->cur_offset);
1015 /* at this point we know where in the file the first segment sections go */
1016 dynsym = collect_syms (acfg, hash, &dyn_str_table, NULL, NULL);
1017 num_local_syms = hash [1];
1018 symtab = collect_syms (acfg, NULL, &str_table, secth, &num_local_syms);
1020 for (i = 0; i < num_sections; ++i) {
1021 if (sections [i]->shidx == SECT_TEXT) {
1022 text_section = sections [i];
1023 } else if (sections [i]->shidx == SECT_DATA) {
1024 data_section = sections [i];
1025 } else if (sections [i]->shidx == SECT_BSS) {
1026 bss_section = sections [i];
1030 file_offset = virt_offset = sizeof (header) + sizeof (progh);
1031 secth [SECT_HASH].sh_addr = secth [SECT_HASH].sh_offset = file_offset;
1032 size = sizeof (int) * (2 + hash [0] + hash [1]);
1033 virt_offset = (file_offset += size);
1034 secth [SECT_HASH].sh_size = size;
1035 secth [SECT_DYNSYM].sh_addr = secth [SECT_DYNSYM].sh_offset = file_offset;
1036 size = sizeof (ElfSymbol) * hash [1];
1037 virt_offset = (file_offset += size);
1038 secth [SECT_DYNSYM].sh_size = size;
1039 secth [SECT_DYNSTR].sh_addr = secth [SECT_DYNSTR].sh_offset = file_offset;
1040 size = dyn_str_table.data->len;
1041 virt_offset = (file_offset += size);
1042 secth [SECT_DYNSTR].sh_size = size;
1044 file_offset &= ~(4-1);
1045 secth [SECT_REL_DYN].sh_addr = secth [SECT_REL_DYN].sh_offset = file_offset;
1046 size = sizeof (ElfReloc) * acfg->num_relocs;
1047 secth [SECT_REL_DYN].sh_size = size;
1048 virt_offset = (file_offset += size);
1049 secth [SECT_REL_DYN].sh_size = size;
1050 file_offset += 4096-1;
1051 file_offset &= ~(4096-1);
1052 virt_offset = file_offset;
1053 secth [SECT_TEXT].sh_addr = secth [SECT_TEXT].sh_offset = file_offset;
1054 size = text_section->cur_offset;
1055 secth [SECT_TEXT].sh_size = size;
1056 file_offset += size;
1058 file_offset &= ~(4-1);
1059 virt_offset = file_offset;
1060 /* .dynamic, .got.plt, .data, .bss here */
1061 secth [SECT_DYNAMIC].sh_addr = virt_offset;
1062 secth [SECT_DYNAMIC].sh_offset = file_offset;
1063 size = sizeof (dynamic);
1064 secth [SECT_DYNAMIC].sh_size = size;
1067 file_offset += size;
1068 virt_offset += size;
1069 secth [SECT_GOT_PLT].sh_addr = virt_offset;
1070 secth [SECT_GOT_PLT].sh_offset = file_offset;
1072 secth [SECT_GOT_PLT].sh_size = size;
1075 file_offset += size;
1076 virt_offset += size;
1077 secth [SECT_DATA].sh_addr = virt_offset;
1078 secth [SECT_DATA].sh_offset = file_offset;
1079 size = data_section->cur_offset;
1080 secth [SECT_DATA].sh_size = size;
1083 file_offset += size;
1084 virt_offset += size;
1085 secth [SECT_BSS].sh_addr = virt_offset;
1086 secth [SECT_BSS].sh_offset = file_offset;
1087 size = bss_section->cur_offset;
1088 secth [SECT_BSS].sh_size = size;
1090 /* virtual doesn't matter anymore */
1091 secth [SECT_SHSTRTAB].sh_offset = file_offset;
1092 size = sh_str_table.data->len;
1093 secth [SECT_SHSTRTAB].sh_size = size;
1096 file_offset += size;
1097 secth [SECT_SYMTAB].sh_offset = file_offset;
1098 size = sizeof (ElfSymbol) * num_local_syms;
1099 secth [SECT_SYMTAB].sh_size = size;
1100 file_offset += size;
1101 secth [SECT_STRTAB].sh_offset = file_offset;
1102 size = str_table.data->len;
1103 secth [SECT_STRTAB].sh_size = size;
1104 file_offset += size;
1106 file_offset &= ~(4-1);
1108 text_section->file_offset = secth [SECT_TEXT].sh_offset;
1109 data_section->file_offset = secth [SECT_DATA].sh_offset;
1110 bss_section->file_offset = secth [SECT_BSS].sh_offset;
1112 header.e_ident [0] = 0x7f; header.e_ident [1] = 'E';
1113 header.e_ident [2] = 'L'; header.e_ident [3] = 'F';
1114 header.e_ident [4] = SIZEOF_VOID_P == 4? 1: 2;
1115 header.e_ident [5] = 1; /* FIXME: little endian, bigendian is 2 */
1116 header.e_ident [6] = 1; /* version */
1117 header.e_ident [7] = 0; /* FIXME: */
1118 header.e_ident [8] = 0; /* FIXME: */
1119 for (i = 9; i < 16; ++i)
1120 header.e_ident [i] = 0;
1122 header.e_type = 3; /* shared library */
1123 header.e_machine = 3; /* FIXME: 386 */
1124 header.e_version = 1; /* FIXME: */
1126 header.e_phoff = sizeof (header);
1127 header.e_ehsize = sizeof (header);
1128 header.e_phentsize = sizeof (struct ElfProgHeader);
1130 header.e_entry = secth [SECT_TEXT].sh_addr;
1131 header.e_shstrndx = 10;
1132 header.e_shentsize = sizeof (struct ElfSectHeader);
1133 header.e_shnum = SECT_NUM;
1134 header.e_shoff = file_offset;
1138 dynamic [i].d_tag = DYN_HASH;
1139 dynamic [i].d_val = secth [SECT_HASH].sh_offset;
1141 dynamic [i].d_tag = DYN_STRTAB;
1142 dynamic [i].d_val = secth [SECT_DYNSTR].sh_offset;
1144 dynamic [i].d_tag = DYN_SYMTAB;
1145 dynamic [i].d_val = secth [SECT_DYNSYM].sh_offset;
1147 dynamic [i].d_tag = DYN_STRSZ;
1148 dynamic [i].d_val = dyn_str_table.data->len;
1150 dynamic [i].d_tag = DYN_SYMENT;
1151 dynamic [i].d_val = sizeof (ElfSymbol);
1153 dynamic [i].d_tag = DYN_REL;
1154 dynamic [i].d_val = secth [SECT_REL_DYN].sh_offset;
1156 dynamic [i].d_tag = DYN_RELSZ;
1157 dynamic [i].d_val = secth [SECT_REL_DYN].sh_size;
1159 dynamic [i].d_tag = DYN_RELENT;
1160 dynamic [i].d_val = sizeof (ElfReloc);
1162 dynamic [i].d_tag = DYN_RELCOUNT;
1163 dynamic [i].d_val = acfg->num_relocs;
1166 /* Program header */
1167 memset (&progh, 0, sizeof (progh));
1168 progh [0].p_type = 1; /* LOAD */
1169 progh [0].p_filesz = progh [0].p_memsz = secth [SECT_DYNAMIC].sh_offset;
1170 progh [0].p_align = 4096;
1171 progh [0].p_flags = 5;
1173 progh [1].p_type = 1;
1174 progh [1].p_offset = secth [SECT_DYNAMIC].sh_offset;
1175 progh [1].p_vaddr = progh [1].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1176 progh [1].p_filesz = secth [SECT_BSS].sh_offset - secth [SECT_DYNAMIC].sh_offset;
1177 progh [1].p_memsz = secth [SECT_BSS].sh_addr + secth [SECT_BSS].sh_size - secth [SECT_DYNAMIC].sh_addr;
1178 progh [1].p_align = 4096;
1179 progh [1].p_flags = 6;
1181 progh [2].p_type = 2; /* DYNAMIC */
1182 progh [2].p_offset = secth [SECT_DYNAMIC].sh_offset;
1183 progh [2].p_vaddr = progh [2].p_paddr = secth [SECT_DYNAMIC].sh_addr;
1184 progh [2].p_filesz = progh [2].p_memsz = secth [SECT_DYNAMIC].sh_size;
1185 progh [2].p_align = 4;
1186 progh [2].p_flags = 6;
1188 reloc_symbols (acfg, dynsym, secth, &dyn_str_table, TRUE);
1189 reloc_symbols (acfg, symtab, secth, &str_table, FALSE);
1190 relocs = resolve_relocations (acfg);
1192 fwrite (&header, sizeof (header), 1, file);
1193 fwrite (&progh, sizeof (progh), 1, file);
1194 fwrite (hash, sizeof (int) * (hash [0] + hash [1] + 2), 1, file);
1195 fwrite (dynsym, sizeof (ElfSymbol) * hash [1], 1, file);
1196 fwrite (dyn_str_table.data->str, dyn_str_table.data->len, 1, file);
1198 fseek (file, secth [SECT_REL_DYN].sh_offset, SEEK_SET);
1199 fwrite (relocs, sizeof (ElfReloc), acfg->num_relocs, file);
1201 fseek (file, secth [SECT_TEXT].sh_offset, SEEK_SET);
1202 /* write .text, .data, .bss sections */
1203 fwrite (text_section->data, text_section->cur_offset, 1, file);
1206 fwrite (dynamic, sizeof (dynamic), 1, file);
1208 size = secth [SECT_DYNAMIC].sh_addr;
1209 fwrite (&size, sizeof (size), 1, file);
1210 fseek (file, secth [SECT_DATA].sh_offset, SEEK_SET);
1211 fwrite (data_section->data, data_section->cur_offset, 1, file);
1213 fseek (file, secth [SECT_SHSTRTAB].sh_offset, SEEK_SET);
1214 fwrite (sh_str_table.data->str, sh_str_table.data->len, 1, file);
1215 fseek (file, secth [SECT_SYMTAB].sh_offset, SEEK_SET);
1216 fwrite (symtab, sizeof (ElfSymbol) * num_local_syms, 1, file);
1217 fseek (file, secth [SECT_STRTAB].sh_offset, SEEK_SET);
1218 fwrite (str_table.data->str, str_table.data->len, 1, file);
1219 /*g_print ("file_offset %d vs %d\n", file_offset, ftell (file));*/
1220 /*g_assert (file_offset >= ftell (file));*/
1221 fseek (file, file_offset, SEEK_SET);
1222 fwrite (§h, sizeof (secth), 1, file);
1224 rename (tmp_outfile_name, outfile_name);
1226 g_free (tmp_outfile_name);
1227 g_free (outfile_name);
1230 #endif /* USE_ELF_WRITER */
1235 emit_start (MonoAotCompile *acfg)
1237 int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
1238 acfg->fp = fdopen (i, "w+");
1239 g_assert (acfg->fp);
1243 emit_unset_mode (MonoAotCompile *acfg)
1245 if (acfg->mode == EMIT_NONE)
1247 fprintf (acfg->fp, "\n");
1248 acfg->mode = EMIT_NONE;
1252 emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
1254 emit_unset_mode (acfg);
1255 #if defined(PLATFORM_WIN32)
1256 fprintf (acfg->fp, ".section %s\n", section_name);
1257 #elif defined(sparc)
1258 /* For solaris as, GNU as should accept the same */
1259 fprintf (acfg->fp, ".section \"%s\"\n", section_name);
1260 #elif defined(__ppc__) && defined(__MACH__)
1261 /* This needs to be made more precise on mach. */
1262 fprintf (acfg->fp, "%s\n", subsection_index == 0 ? ".text" : ".data");
1264 fprintf (acfg->fp, "%s %d\n", section_name, subsection_index);
1269 emit_symbol_type (MonoAotCompile *acfg, const char *name, gboolean func)
1278 emit_unset_mode (acfg);
1280 fprintf (acfg->fp, "\t.type %s,#%s\n", name, stype);
1281 #elif defined(PLATFORM_WIN32)
1283 #elif !(defined(__ppc__) && defined(__MACH__))
1284 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1285 #elif defined(__x86_64__) || defined(__i386__)
1286 fprintf (acfg->fp, "\t.type %s,@%s\n", name, stype);
1291 emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
1293 emit_unset_mode (acfg);
1294 #if (defined(__ppc__) && defined(__MACH__)) || defined(PLATFORM_WIN32)
1295 // mach-o always uses a '_' prefix.
1296 fprintf (acfg->fp, "\t.globl _%s\n", name);
1298 fprintf (acfg->fp, "\t.globl %s\n", name);
1301 emit_symbol_type (acfg, name, func);
1305 emit_label (MonoAotCompile *acfg, const char *name)
1307 emit_unset_mode (acfg);
1308 #if (defined(__ppc__) && defined(__MACH__)) || defined(PLATFORM_WIN32)
1309 // mach-o always uses a '_' prefix.
1310 fprintf (acfg->fp, "_%s:\n", name);
1312 fprintf (acfg->fp, "%s:\n", name);
1315 #if defined(PLATFORM_WIN32)
1316 /* Emit a normal label too */
1317 fprintf (acfg->fp, "%s:\n", name);
1322 emit_string (MonoAotCompile *acfg, const char *value)
1324 emit_unset_mode (acfg);
1325 fprintf (acfg->fp, "\t%s \"%s\"\n", AS_STRING_DIRECTIVE, value);
1329 emit_line (MonoAotCompile *acfg)
1331 emit_unset_mode (acfg);
1332 fprintf (acfg->fp, "\n");
1336 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
1338 emit_unset_mode (acfg);
1339 emit_section_change (acfg, ".text", 1);
1340 emit_global (acfg, name, FALSE);
1341 emit_label (acfg, name);
1342 emit_string (acfg, value);
1346 emit_alignment (MonoAotCompile *acfg, int size)
1348 emit_unset_mode (acfg);
1349 #if defined(__ppc__) && defined(__MACH__)
1350 // the mach-o assembler specifies alignments as powers of 2.
1351 fprintf (acfg->fp, "\t.align %d\t; ilog2\n", ilog2(size));
1352 #elif defined(__powerpc__)
1353 /* ignore on linux/ppc */
1355 fprintf (acfg->fp, "\t.align %d\n", size);
1360 emit_pointer (MonoAotCompile *acfg, const char *target)
1362 emit_unset_mode (acfg);
1363 emit_alignment (acfg, sizeof (gpointer));
1364 #if defined(__x86_64__)
1365 fprintf (acfg->fp, "\t.quad %s\n", target);
1366 #elif defined(sparc) && SIZEOF_VOID_P == 8
1367 fprintf (acfg->fp, "\t.xword %s\n", target);
1369 fprintf (acfg->fp, "\t.long %s\n", target);
1374 emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
1377 if (acfg->mode != EMIT_BYTE) {
1378 acfg->mode = EMIT_BYTE;
1379 acfg->col_count = 0;
1381 for (i = 0; i < size; ++i, ++acfg->col_count) {
1382 if ((acfg->col_count % 32) == 0)
1383 fprintf (acfg->fp, "\n\t.byte ");
1385 fprintf (acfg->fp, ", ");
1386 fprintf (acfg->fp, "0x%x", buf [i]);
1391 emit_int16 (MonoAotCompile *acfg, int value)
1393 if (acfg->mode != EMIT_WORD) {
1394 acfg->mode = EMIT_WORD;
1395 acfg->col_count = 0;
1397 if ((acfg->col_count++ % 8) == 0)
1398 fprintf (acfg->fp, "\n\t.word ");
1400 fprintf (acfg->fp, ", ");
1401 fprintf (acfg->fp, "%d", value);
1405 emit_int32 (MonoAotCompile *acfg, int value)
1407 if (acfg->mode != EMIT_LONG) {
1408 acfg->mode = EMIT_LONG;
1409 acfg->col_count = 0;
1411 if ((acfg->col_count++ % 8) == 0)
1412 fprintf (acfg->fp, "\n\t.long ");
1414 fprintf (acfg->fp, ", ");
1415 fprintf (acfg->fp, "%d", value);
1419 emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
1421 if (acfg->mode != EMIT_LONG) {
1422 acfg->mode = EMIT_LONG;
1423 acfg->col_count = 0;
1425 if ((acfg->col_count++ % 8) == 0)
1426 fprintf (acfg->fp, "\n\t.long ");
1428 fprintf (acfg->fp, ", ");
1430 fprintf (acfg->fp, "%s - %s %c %d", end, start, offset < 0? ' ': '+', offset);
1432 fprintf (acfg->fp, "%s - %s", end, start);
1436 emit_zero_bytes (MonoAotCompile *acfg, int num)
1438 emit_unset_mode (acfg);
1439 fprintf (acfg->fp, "\t.skip %d\n", num);
1443 emit_writeout (MonoAotCompile *acfg)
1445 char *command, *objfile;
1446 char *outfile_name, *tmp_outfile_name;
1450 #if defined(__x86_64__)
1451 #define AS_OPTIONS "--64"
1452 #elif defined(sparc) && SIZEOF_VOID_P == 8
1453 #define AS_OPTIONS "-xarch=v9"
1455 #define AS_OPTIONS ""
1457 command = g_strdup_printf ("as %s %s -o %s.o", AS_OPTIONS, acfg->tmpfname, acfg->tmpfname);
1458 printf ("Executing the native assembler: %s\n", command);
1459 if (system (command) != 0) {
1466 if (acfg->aot_opts.outfile)
1467 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
1469 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, SHARED_EXT);
1471 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
1474 command = g_strdup_printf ("ld -shared -G -o %s %s.o", outfile_name, acfg->tmpfname);
1475 #elif defined(__ppc__) && defined(__MACH__)
1476 command = g_strdup_printf ("gcc -dynamiclib -o %s %s.o", outfile_name, acfg->tmpfname);
1477 #elif defined(PLATFORM_WIN32)
1478 command = g_strdup_printf ("gcc -shared --dll -mno-cygwin -o %s %s.o", outfile_name, acfg->tmpfname);
1480 command = g_strdup_printf ("ld -shared -o %s %s.o", outfile_name, acfg->tmpfname);
1482 printf ("Executing the native linker: %s\n", command);
1483 if (system (command) != 0) {
1484 g_free (tmp_outfile_name);
1485 g_free (outfile_name);
1491 objfile = g_strdup_printf ("%s.o", acfg->tmpfname);
1494 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, SHARED_EXT);
1495 printf ("Stripping the binary: %s\n", com);
1499 rename (tmp_outfile_name, outfile_name);
1501 g_free (tmp_outfile_name);
1502 g_free (outfile_name);
1504 if (acfg->aot_opts.save_temps)
1505 printf ("Retained input file.\n");
1507 unlink (acfg->tmpfname);
1511 #endif /* ASM_WRITER */
1514 emit_byte (MonoAotCompile *acfg, guint8 val)
1516 emit_bytes (acfg, &val, 1);
1520 mono_get_field_token (MonoClassField *field)
1522 MonoClass *klass = field->parent;
1525 for (i = 0; i < klass->field.count; ++i) {
1526 if (field == &klass->fields [i])
1527 return MONO_TOKEN_FIELD_DEF | (klass->field.first + 1 + i);
1530 g_assert_not_reached ();
1535 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
1539 //printf ("ENCODE: %d 0x%x.\n", value, value);
1542 * Same encoding as the one used in the metadata, extended to handle values
1543 * greater than 0x1fffffff.
1545 if ((value >= 0) && (value <= 127))
1547 else if ((value >= 0) && (value <= 16383)) {
1548 p [0] = 0x80 | (value >> 8);
1549 p [1] = value & 0xff;
1551 } else if ((value >= 0) && (value <= 0x1fffffff)) {
1552 p [0] = (value >> 24) | 0xc0;
1553 p [1] = (value >> 16) & 0xff;
1554 p [2] = (value >> 8) & 0xff;
1555 p [3] = value & 0xff;
1560 p [1] = (value >> 24) & 0xff;
1561 p [2] = (value >> 16) & 0xff;
1562 p [3] = (value >> 8) & 0xff;
1563 p [4] = value & 0xff;
1571 get_image_index (MonoAotCompile *cfg, MonoImage *image)
1575 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
1579 index = g_hash_table_size (cfg->image_hash);
1580 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
1581 g_ptr_array_add (cfg->image_table, image);
1587 encode_klass_info (MonoAotCompile *cfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
1589 if (!klass->type_token) {
1593 g_assert (klass->rank > 0);
1594 encode_value (MONO_TOKEN_TYPE_DEF, buf, &buf);
1595 encode_value (get_image_index (cfg, klass->image), buf, &buf);
1596 token = klass->element_class->type_token;
1599 g_assert (klass->element_class->rank);
1600 encode_value (0, buf, &buf);
1601 encode_value (klass->element_class->rank, buf, &buf);
1602 token = klass->element_class->element_class->type_token;
1604 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_TYPE_DEF);
1605 encode_value (token - MONO_TOKEN_TYPE_DEF, buf, &buf);
1606 encode_value (klass->rank, buf, &buf);
1609 g_assert (mono_metadata_token_code (klass->type_token) == MONO_TOKEN_TYPE_DEF);
1610 encode_value (klass->type_token - MONO_TOKEN_TYPE_DEF, buf, &buf);
1611 encode_value (get_image_index (cfg, klass->image), buf, &buf);
1617 encode_field_info (MonoAotCompile *cfg, MonoClassField *field, guint8 *buf, guint8 **endbuf)
1619 guint32 token = mono_get_field_token (field);
1621 encode_klass_info (cfg, field->parent, buf, &buf);
1622 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
1623 encode_value (token - MONO_TOKEN_FIELD_DEF, buf, &buf);
1628 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf)
1630 guint32 image_index = get_image_index (acfg, method->klass->image);
1631 guint32 token = method->token;
1632 g_assert (image_index < 256);
1633 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
1635 encode_value ((image_index << 24) + (mono_metadata_token_index (token)), buf, &buf);
1640 compare_patches (gconstpointer a, gconstpointer b)
1644 i = (*(MonoJumpInfo**)a)->ip.i;
1645 j = (*(MonoJumpInfo**)b)->ip.i;
1657 get_plt_index (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
1662 switch (patch_info->type) {
1663 case MONO_PATCH_INFO_METHOD:
1664 case MONO_PATCH_INFO_WRAPPER:
1665 case MONO_PATCH_INFO_INTERNAL_METHOD:
1666 case MONO_PATCH_INFO_CLASS_INIT: {
1667 MonoJumpInfo *new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
1668 gpointer patch_id = NULL;
1670 /* First check for an existing patch */
1671 switch (patch_info->type) {
1672 case MONO_PATCH_INFO_METHOD:
1673 patch_id = patch_info->data.method;
1675 case MONO_PATCH_INFO_INTERNAL_METHOD:
1676 patch_id = (gpointer)patch_info->data.name;
1678 case MONO_PATCH_INFO_CLASS_INIT:
1679 patch_id = patch_info->data.klass;
1681 case MONO_PATCH_INFO_WRAPPER:
1682 /* A bit ugly, but works */
1683 g_assert (patch_info->data.method->wrapper_type < sizeof (MonoMethod));
1684 patch_id = (gpointer)(((guint8*)patch_info->data.method) + patch_info->data.method->wrapper_type);
1687 g_assert_not_reached ();
1691 idx = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_plt_offset, patch_id));
1695 g_hash_table_insert (acfg->patch_to_plt_offset, patch_id, GUINT_TO_POINTER (acfg->plt_offset));
1699 res = acfg->plt_offset;
1700 g_hash_table_insert (acfg->plt_offset_to_patch, GUINT_TO_POINTER (acfg->plt_offset), new_ji);
1701 acfg->plt_offset ++;
1704 /* Nullify the patch */
1705 patch_info->type = MONO_PATCH_INFO_NONE;
1717 * Returns the offset of the GOT slot where the runtime object resulting from resolving
1718 * JI could be found if it exists, otherwise allocates a new one.
1721 get_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
1725 got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->patch_to_shared_got_offset, ji));
1727 return got_offset - 1;
1729 got_offset = acfg->got_offset;
1730 acfg->got_offset ++;
1732 acfg->stats.got_slots ++;
1733 acfg->stats.got_slot_types [ji->type] ++;
1739 get_shared_got_offset (MonoAotCompile *acfg, MonoJumpInfo *ji)
1744 if (!g_hash_table_lookup (acfg->patch_to_shared_got_offset, ji)) {
1745 got_offset = get_got_offset (acfg, ji);
1746 copy = mono_patch_info_dup_mp (acfg->mempool, ji);
1747 g_hash_table_insert (acfg->patch_to_shared_got_offset, copy, GUINT_TO_POINTER (got_offset + 1));
1748 g_ptr_array_add (acfg->shared_patches, copy);
1751 return get_got_offset (acfg, ji);
1755 emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
1758 int i, pindex, method_index;
1761 int func_alignment = 16;
1763 MonoJumpInfo *patch_info;
1764 MonoMethodHeader *header;
1768 method = cfg->method;
1769 code = cfg->native_code;
1770 header = mono_method_get_header (method);
1772 method_index = mono_metadata_token_index (method->token);
1774 /* Make the labels local */
1775 symbol = g_strdup_printf (".Lm_%x", method_index);
1777 emit_alignment (acfg, func_alignment);
1778 emit_label (acfg, symbol);
1779 if (acfg->aot_opts.write_symbols)
1780 emit_global (acfg, symbol, TRUE);
1782 if (cfg->verbose_level > 0)
1783 g_print ("Method %s emitted as %s\n", mono_method_full_name (method, TRUE), symbol);
1785 acfg->stats.code_size += cfg->code_len;
1787 /* Collect and sort relocations */
1788 patches = g_ptr_array_new ();
1789 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
1790 g_ptr_array_add (patches, patch_info);
1791 g_ptr_array_sort (patches, compare_patches);
1793 acfg->method_got_offsets [method_index] = acfg->got_offset;
1794 for (i = 0; i < cfg->code_len; i++) {
1796 for (pindex = 0; pindex < patches->len; ++pindex) {
1797 patch_info = g_ptr_array_index (patches, pindex);
1798 if (patch_info->ip.i == i)
1802 #ifdef MONO_ARCH_HAVE_PIC_AOT
1805 if (patch_info && (pindex < patches->len)) {
1806 switch (patch_info->type) {
1807 case MONO_PATCH_INFO_LABEL:
1808 case MONO_PATCH_INFO_BB:
1809 case MONO_PATCH_INFO_NONE:
1811 case MONO_PATCH_INFO_GOT_OFFSET: {
1812 guint32 offset = mono_arch_get_patch_offset (code + i);
1813 emit_bytes (acfg, code + i, offset);
1814 emit_symbol_diff (acfg, "got", ".", offset);
1816 i += offset + 4 - 1;
1822 char *direct_call_target;
1824 if (!is_got_patch (patch_info->type))
1828 * If this patch is a call, try emitting a direct call instead of
1829 * through a PLT entry. This is possible if the called method is in
1830 * the same assembly and requires no initialization.
1832 direct_call_target = NULL;
1833 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (patch_info->data.method->klass->image == cfg->method->klass->image)) {
1834 MonoCompile *callee_cfg = g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
1836 guint32 callee_idx = mono_metadata_token_index (callee_cfg->method->token);
1837 if (!acfg->has_got_slots [callee_idx] && (callee_cfg->method->klass->flags & TYPE_ATTRIBUTE_BEFORE_FIELD_INIT)) {
1838 //printf ("DIRECT: %s %s\n", mono_method_full_name (cfg->method, TRUE), mono_method_full_name (callee_cfg->method, TRUE));
1839 direct_call_target = g_strdup_printf (".Lm_%x", mono_metadata_token_index (callee_cfg->method->token));
1840 patch_info->type = MONO_PATCH_INFO_NONE;
1841 acfg->stats.direct_calls ++;
1845 acfg->stats.all_calls ++;
1848 if (!direct_call_target) {
1849 plt_index = get_plt_index (acfg, patch_info);
1850 if (plt_index != -1) {
1851 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
1852 direct_call_target = g_strdup_printf (".Lp_%d", plt_index);
1856 if (direct_call_target) {
1857 #if defined(__i386__) || defined(__x86_64__)
1858 g_assert (code [i] == 0xe8);
1859 /* Need to make sure this is exactly 5 bytes long */
1860 emit_byte (acfg, '\xe8');
1861 emit_symbol_diff (acfg, direct_call_target, ".", -4);
1864 g_assert_not_reached ();
1867 got_slot = get_got_offset (acfg, patch_info);
1869 emit_bytes (acfg, code + i, mono_arch_get_patch_offset (code + i));
1871 emit_symbol_diff (acfg, "got", ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
1872 #elif defined(__i386__)
1873 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
1876 i += mono_arch_get_patch_offset (code + i) + 4 - 1;
1882 #endif /* MONO_ARCH_HAVE_PIC_AOT */
1885 emit_bytes (acfg, code + i, 1);
1893 * Encode PATCH_INFO into its disk representation. If SHARED is true, encode some types
1894 * of patches by allocating a GOT entry for them, and encode the GOT offset instead.
1897 encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf, gboolean shared)
1901 switch (patch_info->type) {
1902 case MONO_PATCH_INFO_NONE:
1904 case MONO_PATCH_INFO_IMAGE:
1905 encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
1907 case MONO_PATCH_INFO_METHOD_REL:
1908 encode_value ((gint)patch_info->data.offset, p, &p);
1910 case MONO_PATCH_INFO_SWITCH: {
1911 gpointer *table = (gpointer *)patch_info->data.table->table;
1914 encode_value (patch_info->data.table->table_size, p, &p);
1915 for (k = 0; k < patch_info->data.table->table_size; k++)
1916 encode_value ((int)(gssize)table [k], p, &p);
1919 case MONO_PATCH_INFO_METHODCONST:
1920 case MONO_PATCH_INFO_METHOD:
1921 case MONO_PATCH_INFO_METHOD_JUMP:
1922 encode_method_ref (acfg, patch_info->data.method, p, &p);
1924 case MONO_PATCH_INFO_INTERNAL_METHOD: {
1925 guint32 len = strlen (patch_info->data.name);
1927 encode_value (len, p, &p);
1929 memcpy (p, patch_info->data.name, len);
1934 case MONO_PATCH_INFO_LDSTR: {
1935 guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
1936 guint32 token = patch_info->data.token->token;
1937 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
1939 * An optimization would be to emit shared code for ldstr
1940 * statements followed by a throw.
1942 encode_value (image_index, p, &p);
1943 encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
1946 case MONO_PATCH_INFO_RVA:
1947 case MONO_PATCH_INFO_DECLSEC:
1948 case MONO_PATCH_INFO_LDTOKEN:
1949 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
1951 guint32 offset = get_got_offset (acfg, patch_info);
1952 encode_value (offset, p, &p);
1954 encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
1955 encode_value (patch_info->data.token->token, p, &p);
1958 case MONO_PATCH_INFO_EXC_NAME: {
1959 MonoClass *ex_class;
1962 mono_class_from_name (mono_defaults.exception_class->image,
1963 "System", patch_info->data.target);
1964 g_assert (ex_class);
1965 encode_klass_info (acfg, ex_class, p, &p);
1968 case MONO_PATCH_INFO_R4:
1969 encode_value (*((guint32 *)patch_info->data.target), p, &p);
1971 case MONO_PATCH_INFO_R8:
1972 encode_value (*((guint32 *)patch_info->data.target), p, &p);
1973 encode_value (*(((guint32 *)patch_info->data.target) + 1), p, &p);
1975 case MONO_PATCH_INFO_VTABLE:
1976 case MONO_PATCH_INFO_CLASS:
1977 case MONO_PATCH_INFO_IID:
1978 case MONO_PATCH_INFO_ADJUSTED_IID:
1980 guint32 offset = get_got_offset (acfg, patch_info);
1981 encode_value (offset, p, &p);
1983 encode_klass_info (acfg, patch_info->data.klass, p, &p);
1986 case MONO_PATCH_INFO_CLASS_INIT:
1987 encode_klass_info (acfg, patch_info->data.klass, p, &p);
1989 case MONO_PATCH_INFO_FIELD:
1990 case MONO_PATCH_INFO_SFLDA:
1992 guint32 offset = get_got_offset (acfg, patch_info);
1993 encode_value (offset, p, &p);
1995 encode_field_info (acfg, patch_info->data.field, p, &p);
1998 case MONO_PATCH_INFO_WRAPPER: {
1999 encode_value (patch_info->data.method->wrapper_type, p, &p);
2001 switch (patch_info->data.method->wrapper_type) {
2002 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK: {
2004 guint32 image_index;
2007 m = mono_marshal_method_from_wrapper (patch_info->data.method);
2008 image_index = get_image_index (acfg, m->klass->image);
2010 g_assert (image_index < 256);
2011 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
2013 encode_value ((image_index << 24) + (mono_metadata_token_index (token)), p, &p);
2016 case MONO_WRAPPER_PROXY_ISINST:
2017 case MONO_WRAPPER_LDFLD:
2018 case MONO_WRAPPER_LDFLDA:
2019 case MONO_WRAPPER_STFLD:
2020 case MONO_WRAPPER_LDFLD_REMOTE:
2021 case MONO_WRAPPER_STFLD_REMOTE:
2022 case MONO_WRAPPER_ISINST: {
2023 MonoClass *proxy_class = (MonoClass*)mono_marshal_method_from_wrapper (patch_info->data.method);
2024 encode_klass_info (acfg, proxy_class, p, &p);
2027 case MONO_WRAPPER_STELEMREF:
2030 g_assert_not_reached ();
2035 g_warning ("unable to handle jump info %d", patch_info->type);
2036 g_assert_not_reached ();
2043 emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
2047 int j, pindex, buf_size, n_patches;
2051 MonoJumpInfo *patch_info;
2052 MonoMethodHeader *header;
2053 guint32 last_offset, method_idx;
2055 guint32 first_got_offset;
2057 method = cfg->method;
2058 code = cfg->native_code;
2059 header = mono_method_get_header (method);
2061 method_idx = mono_metadata_token_index (method->token);
2063 /* Make the labels local */
2064 symbol = g_strdup_printf (".Lm_%x_p", method_idx);
2066 /* Sort relocations */
2067 patches = g_ptr_array_new ();
2068 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
2069 g_ptr_array_add (patches, patch_info);
2070 g_ptr_array_sort (patches, compare_patches);
2072 first_got_offset = acfg->method_got_offsets [mono_metadata_token_index (cfg->method->token)];
2074 /**********************/
2075 /* Encode method info */
2076 /**********************/
2078 buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
2079 p = buf = g_malloc (buf_size);
2081 if (mono_class_get_cctor (method->klass))
2082 encode_klass_info (acfg, method->klass, p, &p);
2084 /* Not needed when loading the method */
2085 encode_value (0, p, &p);
2088 if (cfg->opt & MONO_OPT_SHARED) {
2089 encode_value (g_list_length (cfg->ldstr_list), p, &p);
2090 for (l = cfg->ldstr_list; l; l = l->next) {
2091 encode_value ((long)l->data, p, &p);
2095 /* Used only in shared mode */
2096 g_assert (!cfg->ldstr_list);
2099 for (pindex = 0; pindex < patches->len; ++pindex) {
2100 patch_info = g_ptr_array_index (patches, pindex);
2102 if ((patch_info->type == MONO_PATCH_INFO_LABEL) ||
2103 (patch_info->type == MONO_PATCH_INFO_BB) ||
2104 (patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
2105 (patch_info->type == MONO_PATCH_INFO_NONE)) {
2106 patch_info->type = MONO_PATCH_INFO_NONE;
2111 if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
2112 /* Stored in a GOT slot initialized at module load time */
2113 patch_info->type = MONO_PATCH_INFO_NONE;
2117 if ((patch_info->type == MONO_PATCH_INFO_METHOD) ||
2118 (patch_info->type == MONO_PATCH_INFO_INTERNAL_METHOD) ||
2119 (patch_info->type == MONO_PATCH_INFO_WRAPPER) ||
2120 (patch_info->type == MONO_PATCH_INFO_CLASS_INIT)) {
2121 /* Calls are made through the PLT */
2122 patch_info->type = MONO_PATCH_INFO_NONE;
2130 g_assert (acfg->has_got_slots [method_idx]);
2132 encode_value (n_patches, p, &p);
2135 encode_value (first_got_offset, p, &p);
2137 /* First encode the type+position table */
2140 for (pindex = 0; pindex < patches->len; ++pindex) {
2142 patch_info = g_ptr_array_index (patches, pindex);
2144 if (patch_info->type == MONO_PATCH_INFO_NONE)
2149 //printf ("T: %d O: %d.\n", patch_info->type, patch_info->ip.i);
2150 offset = patch_info->ip.i - last_offset;
2151 last_offset = patch_info->ip.i;
2153 /* Only the type is needed */
2154 *p = patch_info->type;
2160 printf ("%s:\n", mono_method_full_name (cfg->method, TRUE));
2161 for (pindex = 0; pindex < patches->len; ++pindex) {
2162 patch_info = g_ptr_array_index (patches, pindex);
2163 if (patch_info->type != MONO_PATCH_INFO_NONE) {
2164 printf ("\t%s", get_patch_name (patch_info->type));
2165 if (patch_info->type == MONO_PATCH_INFO_VTABLE)
2166 printf (": %s\n", patch_info->data.klass->name);
2174 /* Then encode the other info */
2175 for (pindex = 0; pindex < patches->len; ++pindex) {
2176 patch_info = g_ptr_array_index (patches, pindex);
2178 encode_patch (acfg, patch_info, p, &p, TRUE);
2181 acfg->stats.info_size += p - buf;
2183 /* Emit method info */
2185 emit_label (acfg, symbol);
2187 g_assert (p - buf < buf_size);
2188 emit_bytes (acfg, buf, p - buf);
2195 emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
2199 guint32 debug_info_size;
2202 MonoMethodHeader *header;
2203 guint8 *p, *buf, *debug_info;
2205 method = cfg->method;
2206 code = cfg->native_code;
2207 header = mono_method_get_header (method);
2209 /* Make the labels local */
2210 symbol = g_strdup_printf (".Le_%x_p", mono_metadata_token_index (method->token));
2212 buf_size = header->num_clauses * 256 + 128;
2213 p = buf = g_malloc (buf_size);
2215 encode_value (cfg->code_len, p, &p);
2216 encode_value (cfg->used_int_regs, p, &p);
2218 /* Exception table */
2219 if (header->num_clauses) {
2220 MonoJitInfo *jinfo = cfg->jit_info;
2222 for (k = 0; k < header->num_clauses; ++k) {
2223 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
2225 encode_value (ei->exvar_offset, p, &p);
2227 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER)
2228 encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
2230 encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
2231 encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
2232 encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
2236 mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
2238 encode_value (debug_info_size, p, &p);
2239 if (debug_info_size) {
2240 memcpy (p, debug_info, debug_info_size);
2241 p += debug_info_size;
2242 g_free (debug_info);
2245 acfg->stats.ex_info_size += p - buf;
2249 emit_label (acfg, symbol);
2251 g_assert (p - buf < buf_size);
2252 emit_bytes (acfg, buf, p - buf);
2259 emit_klass_info (MonoAotCompile *acfg, guint32 token)
2261 MonoClass *klass = mono_class_get (acfg->image, token);
2265 gboolean no_special_static;
2268 p = buf = g_malloc (buf_size);
2272 mono_class_init (klass);
2275 * Emit all the information which is required for creating vtables so
2276 * the runtime does not need to create the MonoMethod structures which
2277 * take up a lot of space.
2280 no_special_static = !mono_class_has_special_static_fields (klass);
2283 encode_value (klass->vtable_size, p, &p);
2284 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);
2285 if (klass->has_cctor)
2286 encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
2287 if (klass->has_finalize)
2288 encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
2290 encode_value (klass->instance_size, p, &p);
2291 encode_value (mono_class_data_size (klass), p, &p);
2292 encode_value (klass->packing_size, p, &p);
2293 encode_value (klass->min_align, p, &p);
2295 for (i = 0; i < klass->vtable_size; ++i) {
2296 MonoMethod *cm = klass->vtable [i];
2299 encode_method_ref (acfg, cm, p, &p);
2301 encode_value (0, p, &p);
2305 acfg->stats.class_info_size += p - buf;
2308 label = g_strdup_printf (".LK_I_%x", token - MONO_TOKEN_TYPE_DEF - 1);
2309 emit_label (acfg, label);
2311 g_assert (p - buf < buf_size);
2312 emit_bytes (acfg, buf, p - buf);
2317 * Calls made from AOTed code are routed through a table of jumps similar to the
2318 * ELF PLT (Program Linkage Table). The differences are the following:
2319 * - the ELF PLT entries make an indirect jump though the GOT so they expect the
2320 * GOT pointer to be in EBX. We want to avoid this, so our table contains direct
2321 * jumps. This means the jumps need to be patched when the address of the callee is
2322 * known. Initially the PLT entries jump to code which transfer control to the
2323 * AOT runtime through the first PLT entry.
2326 emit_plt (MonoAotCompile *acfg)
2331 guint32 *plt_info_offsets;
2334 * Encode info need to resolve PLT entries.
2336 buf_size = acfg->plt_offset * 128;
2337 p = buf = g_malloc (buf_size);
2339 plt_info_offsets = g_new0 (guint32, acfg->plt_offset);
2341 for (i = 1; i < acfg->plt_offset; ++i) {
2342 MonoJumpInfo *patch_info = g_hash_table_lookup (acfg->plt_offset_to_patch, GUINT_TO_POINTER (i));
2344 plt_info_offsets [i] = p - buf;
2345 encode_value (patch_info->type, p, &p);
2346 encode_patch (acfg, patch_info, p, &p, FALSE);
2350 symbol = g_strdup_printf ("plt");
2352 /* This section will be made read-write by the AOT loader */
2353 emit_section_change (acfg, ".text", 0);
2354 emit_global (acfg, symbol, TRUE);
2355 emit_alignment (acfg, PAGESIZE);
2356 emit_label (acfg, symbol);
2359 * The first plt entry is used to transfer code to the AOT loader.
2361 emit_label (acfg, ".Lp_0");
2362 #if defined(__i386__)
2363 /* It is filled up during loading by the AOT loader. */
2364 emit_zero_bytes (acfg, 16);
2365 #elif defined(__x86_64__)
2366 /* This should be exactly 16 bytes long */
2367 /* jmpq *<offset>(%rip) */
2368 emit_byte (acfg, '\xff');
2369 emit_byte (acfg, '\x25');
2370 emit_symbol_diff (acfg, "plt_jump_table", ".", -4);
2371 emit_zero_bytes (acfg, 10);
2373 g_assert_not_reached ();
2376 for (i = 1; i < acfg->plt_offset; ++i) {
2379 label = g_strdup_printf (".Lp_%d", i);
2380 emit_label (acfg, label);
2382 #if defined(__i386__)
2383 /* Need to make sure this is 5 bytes long */
2384 emit_byte (acfg, '\xe9');
2385 label = g_strdup_printf (".Lpd_%d", i);
2386 emit_symbol_diff (acfg, label, ".", -4);
2388 #elif defined(__x86_64__)
2390 * We can't emit jumps because they are 32 bits only so they can't be patched.
2391 * So we emit a jump table instead whose entries are patched by the AOT loader to
2392 * point to .Lpd entries. ELF stores these in the GOT too, but we don't, since
2393 * methods with GOT entries can't be called directly.
2394 * We also emit the default PLT code here since the PLT code will not be patched.
2395 * An x86_64 plt entry is 16 bytes long, init_plt () depends on this.
2397 /* jmpq *<offset>(%rip) */
2398 emit_byte (acfg, '\xff');
2399 emit_byte (acfg, '\x25');
2400 emit_symbol_diff (acfg, "plt_jump_table", ".", (i * sizeof (gpointer)) -4);
2401 /* mov <plt info offset>, %eax */
2402 emit_byte (acfg, '\xb8');
2403 emit_int32 (acfg, plt_info_offsets [i]);
2405 emit_byte (acfg, '\xe9');
2406 emit_symbol_diff (acfg, ".Lp_0", ".", -4);
2408 g_assert_not_reached ();
2412 symbol = g_strdup_printf ("plt_end");
2413 emit_global (acfg, symbol, TRUE);
2414 emit_label (acfg, symbol);
2417 * Emit the default targets for the PLT entries separately since these will not
2418 * be modified at runtime.
2420 for (i = 1; i < acfg->plt_offset; ++i) {
2423 label = g_strdup_printf (".Lpd_%d", i);
2424 emit_label (acfg, label);
2427 /* Put the offset into the register expected by mono_aot_plt_trampoline */
2428 #if defined(__i386__)
2429 /* movl $const, %eax */
2430 emit_byte (acfg, '\xb8');
2431 emit_int32 (acfg, plt_info_offsets [i]);
2433 emit_byte (acfg, '\xe9');
2434 emit_symbol_diff (acfg, ".Lp_0", ".", -4);
2435 #elif defined(__x86_64__)
2436 /* Emitted along with the PLT entries since they will not be patched */
2438 g_assert_not_reached ();
2443 symbol = g_strdup_printf ("plt_info");
2444 emit_global (acfg, symbol, FALSE);
2445 emit_label (acfg, symbol);
2447 g_assert (p - buf < buf_size);
2448 emit_bytes (acfg, buf, p - buf);
2451 symbol = g_strdup_printf ("plt_jump_table_addr");
2452 emit_section_change (acfg, ".data", 0);
2453 emit_global (acfg, symbol, FALSE);
2454 emit_alignment (acfg, 8);
2455 emit_label (acfg, symbol);
2456 emit_pointer (acfg, "plt_jump_table");
2458 symbol = g_strdup_printf ("plt_jump_table_size");
2459 emit_section_change (acfg, ".data", 0);
2460 emit_global (acfg, symbol, FALSE);
2461 emit_alignment (acfg, 8);
2462 emit_label (acfg, symbol);
2463 emit_symbol_diff (acfg, "plt_jump_table_end", "plt_jump_table", 0);
2465 /* Don't make this a global so accesses don't need relocations */
2466 symbol = g_strdup_printf ("plt_jump_table");
2467 emit_section_change (acfg, ".bss", 0);
2468 emit_label (acfg, symbol);
2471 emit_zero_bytes (acfg, (int)(acfg->plt_offset * sizeof (gpointer)));
2474 symbol = g_strdup_printf ("plt_jump_table_end");
2475 emit_label (acfg, symbol);
2479 str_begins_with (const char *str1, const char *str2)
2481 int len = strlen (str2);
2482 return strncmp (str1, str2, len) == 0;
2486 mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
2488 gchar **args, **ptr;
2490 memset (opts, 0, sizeof (*opts));
2492 args = g_strsplit (aot_options ? aot_options : "", ",", -1);
2493 for (ptr = args; ptr && *ptr; ptr ++) {
2494 const char *arg = *ptr;
2496 if (str_begins_with (arg, "outfile=")) {
2497 opts->outfile = g_strdup (arg + strlen ("outfile="));
2498 } else if (str_begins_with (arg, "save-temps")) {
2499 opts->save_temps = TRUE;
2500 } else if (str_begins_with (arg, "keep-temps")) {
2501 opts->save_temps = TRUE;
2502 } else if (str_begins_with (arg, "write-symbols")) {
2503 opts->write_symbols = TRUE;
2504 } else if (str_begins_with (arg, "metadata-only")) {
2505 opts->metadata_only = TRUE;
2507 fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
2514 compile_method (MonoAotCompile *acfg, int index)
2518 MonoJumpInfo *patch_info;
2520 guint32 token = MONO_TOKEN_METHOD_DEF | (index + 1);
2523 if (acfg->aot_opts.metadata_only)
2526 method = mono_get_method (acfg->image, token, NULL);
2528 method_idx = mono_metadata_token_index (method->token);
2530 /* fixme: maybe we can also precompile wrapper methods */
2531 if ((method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
2532 (method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
2533 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
2534 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
2535 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
2539 acfg->stats.mcount++;
2541 /* fixme: we need to patch the IP for the LMF in that case */
2542 if (method->save_lmf) {
2543 //printf ("Skip (needs lmf): %s\n", mono_method_full_name (method, TRUE));
2544 acfg->stats.lmfcount++;
2549 * Since these methods are the only ones which are compiled with
2550 * AOT support, and they are not used by runtime startup/shutdown code,
2551 * the runtime will not see AOT methods during AOT compilation,so it
2552 * does not need to support them by creating a fake GOT etc.
2554 cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), FALSE, TRUE, 0);
2555 if (cfg->exception_type != MONO_EXCEPTION_NONE) {
2556 /* Let the exception happen at runtime */
2560 if (cfg->disable_aot) {
2561 //printf ("Skip (other): %s\n", mono_method_full_name (method, TRUE));
2562 acfg->stats.ocount++;
2563 mono_destroy_compile (cfg);
2568 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2569 if (patch_info->type == MONO_PATCH_INFO_ABS) {
2570 /* unable to handle this */
2571 //printf ("Skip (abs addr): %s %d\n", mono_method_full_name (method, TRUE), patch_info->type);
2578 acfg->stats.abscount++;
2579 mono_destroy_compile (cfg);
2584 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2585 if (patch_info->type == MONO_PATCH_INFO_METHOD_JUMP) {
2587 * FIXME: We can't handle this because mono_jit_compile_method_inner will try
2588 * to patch the AOT code when the target of the jump is compiled.
2596 acfg->stats.ocount++;
2597 mono_destroy_compile (cfg);
2601 /* some wrappers are very common */
2602 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2603 if (patch_info->type == MONO_PATCH_INFO_METHODCONST) {
2604 switch (patch_info->data.method->wrapper_type) {
2605 case MONO_WRAPPER_PROXY_ISINST:
2606 patch_info->type = MONO_PATCH_INFO_WRAPPER;
2610 if (patch_info->type == MONO_PATCH_INFO_METHOD) {
2611 switch (patch_info->data.method->wrapper_type) {
2612 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
2613 case MONO_WRAPPER_STFLD:
2614 case MONO_WRAPPER_LDFLD:
2615 case MONO_WRAPPER_LDFLDA:
2616 case MONO_WRAPPER_LDFLD_REMOTE:
2617 case MONO_WRAPPER_STFLD_REMOTE:
2618 case MONO_WRAPPER_STELEMREF:
2619 case MONO_WRAPPER_ISINST:
2620 case MONO_WRAPPER_PROXY_ISINST:
2621 patch_info->type = MONO_PATCH_INFO_WRAPPER;
2628 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2629 switch (patch_info->type) {
2630 case MONO_PATCH_INFO_METHOD:
2631 case MONO_PATCH_INFO_METHODCONST:
2632 if (patch_info->data.method->wrapper_type) {
2633 /* unable to handle this */
2634 //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));
2638 if (!patch_info->data.method->token)
2640 * The method is part of a constructed type like Int[,].Set (). It doesn't
2641 * have a token, and we can't make one, since the parent type is part of
2642 * assembly which contains the element type, and not the assembly which
2643 * referenced this type.
2647 case MONO_PATCH_INFO_VTABLE:
2648 case MONO_PATCH_INFO_CLASS_INIT:
2649 case MONO_PATCH_INFO_CLASS:
2650 case MONO_PATCH_INFO_IID:
2651 case MONO_PATCH_INFO_ADJUSTED_IID:
2652 if (!patch_info->data.klass->type_token)
2653 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))
2662 acfg->stats.wrappercount++;
2663 mono_destroy_compile (cfg);
2667 /* Determine whenever the method has GOT slots */
2668 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2669 switch (patch_info->type) {
2670 case MONO_PATCH_INFO_LABEL:
2671 case MONO_PATCH_INFO_BB:
2672 case MONO_PATCH_INFO_GOT_OFFSET:
2673 case MONO_PATCH_INFO_NONE:
2674 case MONO_PATCH_INFO_METHOD:
2675 case MONO_PATCH_INFO_INTERNAL_METHOD:
2676 case MONO_PATCH_INFO_WRAPPER:
2678 case MONO_PATCH_INFO_IMAGE:
2679 if (patch_info->data.image == acfg->image)
2680 /* Stored in GOT slot 0 */
2684 acfg->has_got_slots [method_idx] = TRUE;
2689 if (!acfg->has_got_slots [method_idx])
2690 acfg->stats.methods_without_got_slots ++;
2692 /* Make a copy of the patch info which is in the mempool */
2694 MonoJumpInfo *patches = NULL, *patches_end = NULL;
2696 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
2697 MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
2700 patches = new_patch_info;
2702 patches_end->next = new_patch_info;
2703 patches_end = new_patch_info;
2705 cfg->patch_info = patches;
2708 /* Free some fields used by cfg to conserve memory */
2709 mono_mempool_destroy (cfg->mempool);
2710 cfg->mempool = NULL;
2711 g_free (cfg->varinfo);
2712 cfg->varinfo = NULL;
2716 mono_regstate_free (cfg->rs);
2720 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
2722 acfg->cfgs [index] = cfg;
2724 g_hash_table_insert (acfg->method_to_cfg, cfg->method, cfg);
2726 acfg->stats.ccount++;
2730 load_profile_files (MonoAotCompile *acfg)
2734 int file_index, res, method_index, i;
2740 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);
2742 if (!g_file_test (tmp, G_FILE_TEST_IS_REGULAR))
2745 infile = fopen (tmp, "r");
2748 printf ("Using profile data file '%s'\n", tmp);
2752 res = fscanf (infile, "%32s\n", ver);
2753 if ((res != 1) || strcmp (ver, "#VER:1") != 0) {
2754 printf ("Profile file has wrong version or invalid.\n");
2760 res = fscanf (infile, "%d\n", &token);
2764 method_index = mono_metadata_token_index (token) - 1;
2766 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (method_index)))
2767 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (method_index));
2772 /* Add missing methods */
2773 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2774 if (!g_list_find (acfg->method_order, GUINT_TO_POINTER (i)))
2775 acfg->method_order = g_list_append (acfg->method_order, GUINT_TO_POINTER (i));
2782 * Collect all patches which have shared GOT entries and alloc entries for them. The
2783 * rest will get entries allocated during emit_code ().
2786 alloc_got_slots (MonoAotCompile *acfg)
2792 /* Slot 0 is reserved for the address of the current assembly */
2793 ji = mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
2794 ji->type = MONO_PATCH_INFO_IMAGE;
2795 ji->data.image = acfg->image;
2797 get_shared_got_offset (acfg, ji);
2799 for (l = acfg->method_order; l != NULL; l = l->next) {
2800 i = GPOINTER_TO_UINT (l->data);
2802 if (acfg->cfgs [i]) {
2803 MonoCompile *cfg = acfg->cfgs [i];
2805 for (ji = cfg->patch_info; ji; ji = ji->next) {
2807 case MONO_PATCH_INFO_VTABLE:
2808 case MONO_PATCH_INFO_CLASS:
2809 case MONO_PATCH_INFO_IID:
2810 case MONO_PATCH_INFO_ADJUSTED_IID:
2811 case MONO_PATCH_INFO_FIELD:
2812 case MONO_PATCH_INFO_SFLDA:
2813 case MONO_PATCH_INFO_DECLSEC:
2814 case MONO_PATCH_INFO_LDTOKEN:
2815 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
2816 case MONO_PATCH_INFO_RVA:
2817 get_shared_got_offset (acfg, ji);
2828 emit_code (MonoAotCompile *acfg)
2834 symbol = g_strdup_printf ("methods");
2835 emit_section_change (acfg, ".text", 0);
2836 emit_global (acfg, symbol, TRUE);
2837 emit_alignment (acfg, 8);
2838 emit_label (acfg, symbol);
2840 for (l = acfg->method_order; l != NULL; l = l->next) {
2841 i = GPOINTER_TO_UINT (l->data);
2844 emit_method_code (acfg, acfg->cfgs [i]);
2847 symbol = g_strdup_printf ("methods_end");
2848 emit_section_change (acfg, ".text", 0);
2849 emit_global (acfg, symbol, FALSE);
2850 emit_alignment (acfg, 8);
2851 emit_label (acfg, symbol);
2853 symbol = g_strdup_printf ("method_offsets");
2854 emit_section_change (acfg, ".text", 1);
2855 emit_global (acfg, symbol, FALSE);
2856 emit_alignment (acfg, 8);
2857 emit_label (acfg, symbol);
2859 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2860 if (acfg->cfgs [i]) {
2861 symbol = g_strdup_printf (".Lm_%x", i + 1);
2862 emit_symbol_diff (acfg, symbol, "methods", 0);
2864 emit_int32 (acfg, 0xffffffff);
2871 emit_info (MonoAotCompile *acfg)
2877 /* Emit method info */
2878 symbol = g_strdup_printf ("method_info");
2879 emit_section_change (acfg, ".text", 1);
2880 emit_global (acfg, symbol, FALSE);
2881 emit_alignment (acfg, 8);
2882 emit_label (acfg, symbol);
2884 /* To reduce size of generated assembly code */
2885 symbol = g_strdup_printf ("mi");
2886 emit_label (acfg, symbol);
2888 for (l = acfg->method_order; l != NULL; l = l->next) {
2889 i = GPOINTER_TO_UINT (l->data);
2892 emit_method_info (acfg, acfg->cfgs [i]);
2895 symbol = g_strdup_printf ("method_info_offsets");
2896 emit_section_change (acfg, ".text", 1);
2897 emit_global (acfg, symbol, FALSE);
2898 emit_alignment (acfg, 8);
2899 emit_label (acfg, symbol);
2901 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2902 if (acfg->cfgs [i]) {
2903 symbol = g_strdup_printf (".Lm_%x_p", i + 1);
2904 emit_symbol_diff (acfg, symbol, "mi", 0);
2906 emit_int32 (acfg, 0);
2913 emit_method_order (MonoAotCompile *acfg)
2919 symbol = g_strdup_printf ("method_order");
2920 emit_section_change (acfg, ".text", 1);
2921 emit_global (acfg, symbol, FALSE);
2922 emit_alignment (acfg, 8);
2923 emit_label (acfg, symbol);
2925 /* First emit an index table */
2928 for (l = acfg->method_order; l != NULL; l = l->next) {
2929 i = GPOINTER_TO_UINT (l->data);
2931 if (acfg->cfgs [i]) {
2932 if ((index % 1024) == 0) {
2933 emit_int32 (acfg, i);
2941 emit_int32 (acfg, 0xffffff);
2943 /* Then emit the whole method order */
2944 for (l = acfg->method_order; l != NULL; l = l->next) {
2945 i = GPOINTER_TO_UINT (l->data);
2947 if (acfg->cfgs [i]) {
2948 emit_int32 (acfg, i);
2953 symbol = g_strdup_printf ("method_order_end");
2954 emit_section_change (acfg, ".text", 1);
2955 emit_global (acfg, symbol, FALSE);
2956 emit_label (acfg, symbol);
2960 emit_exception_info (MonoAotCompile *acfg)
2965 symbol = g_strdup_printf ("ex_info");
2966 emit_section_change (acfg, ".text", 1);
2967 emit_global (acfg, symbol, FALSE);
2968 emit_alignment (acfg, 8);
2969 emit_label (acfg, symbol);
2971 /* To reduce size of generate assembly */
2972 symbol = g_strdup_printf ("ex");
2973 emit_label (acfg, symbol);
2975 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2977 emit_exception_debug_info (acfg, acfg->cfgs [i]);
2980 symbol = g_strdup_printf ("ex_info_offsets");
2981 emit_section_change (acfg, ".text", 1);
2982 emit_global (acfg, symbol, FALSE);
2983 emit_alignment (acfg, 8);
2984 emit_label (acfg, symbol);
2986 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
2987 if (acfg->cfgs [i]) {
2988 symbol = g_strdup_printf (".Le_%x_p", i + 1);
2989 emit_symbol_diff (acfg, symbol, "ex", 0);
2991 emit_int32 (acfg, 0);
2998 emit_class_info (MonoAotCompile *acfg)
3003 symbol = g_strdup_printf ("class_info");
3004 emit_section_change (acfg, ".text", 1);
3005 emit_global (acfg, symbol, FALSE);
3006 emit_alignment (acfg, 8);
3007 emit_label (acfg, symbol);
3009 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
3010 emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
3012 symbol = g_strdup_printf ("class_info_offsets");
3013 emit_section_change (acfg, ".text", 1);
3014 emit_global (acfg, symbol, FALSE);
3015 emit_alignment (acfg, 8);
3016 emit_label (acfg, symbol);
3018 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3019 symbol = g_strdup_printf (".LK_I_%x", i);
3020 emit_symbol_diff (acfg, symbol, "class_info", 0);
3025 typedef struct ClassNameTableEntry {
3026 guint32 token, index;
3027 struct ClassNameTableEntry *next;
3028 } ClassNameTableEntry;
3031 emit_class_name_table (MonoAotCompile *acfg)
3034 guint32 token, hash;
3039 ClassNameTableEntry *entry, *new_entry;
3042 * Construct a chained hash table for mapping class names to typedef tokens.
3044 table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
3045 table = g_ptr_array_sized_new (table_size);
3046 for (i = 0; i < table_size; ++i)
3047 g_ptr_array_add (table, NULL);
3048 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
3049 token = MONO_TOKEN_TYPE_DEF | (i + 1);
3050 klass = mono_class_get (acfg->image, token);
3051 full_name = mono_type_get_full_name (klass);
3052 hash = g_str_hash (full_name) % table_size;
3054 //printf ("A: '%s' %d %d\n", full_name, g_str_hash (full_name), hash);
3056 /* FIXME: Allocate from the mempool */
3057 new_entry = g_new0 (ClassNameTableEntry, 1);
3058 new_entry->token = token;
3060 entry = g_ptr_array_index (table, hash);
3061 if (entry == NULL) {
3062 new_entry->index = hash;
3063 g_ptr_array_index (table, hash) = new_entry;
3066 entry = entry->next;
3068 entry->next = new_entry;
3069 new_entry->index = table->len;
3070 g_ptr_array_add (table, new_entry);
3074 /* Emit the table */
3075 symbol = g_strdup_printf ("class_name_table");
3076 emit_section_change (acfg, ".text", 0);
3077 emit_global (acfg, symbol, FALSE);
3078 emit_alignment (acfg, 8);
3079 emit_label (acfg, symbol);
3081 /* FIXME: Optimize memory usage */
3082 g_assert (table_size < 65000);
3083 emit_int16 (acfg, table_size);
3084 g_assert (table->len < 65000);
3085 for (i = 0; i < table->len; ++i) {
3086 ClassNameTableEntry *entry = g_ptr_array_index (table, i);
3088 if (entry == NULL) {
3089 emit_int16 (acfg, 0);
3090 emit_int16 (acfg, 0);
3092 emit_int16 (acfg, mono_metadata_token_index (entry->token));
3094 emit_int16 (acfg, entry->next->index);
3096 emit_int16 (acfg, 0);
3102 emit_image_table (MonoAotCompile *acfg)
3108 * The image table is small but referenced in a lot of places.
3109 * So we emit it at once, and reference its elements by an index.
3112 symbol = g_strdup_printf ("mono_image_table");
3113 emit_section_change (acfg, ".text", 1);
3114 emit_global (acfg, symbol, FALSE);
3115 emit_alignment (acfg, 8);
3116 emit_label (acfg, symbol);
3117 emit_int32 (acfg, acfg->image_table->len);
3118 for (i = 0; i < acfg->image_table->len; i++) {
3119 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
3120 MonoAssemblyName *aname = &image->assembly->aname;
3122 /* FIXME: Support multi-module assemblies */
3123 g_assert (image->assembly->image == image);
3125 emit_string (acfg, image->assembly_name);
3126 emit_string (acfg, image->guid);
3127 emit_string (acfg, aname->culture ? aname->culture : "");
3128 emit_string (acfg, (const char*)aname->public_key_token);
3130 emit_alignment (acfg, 8);
3131 emit_int32 (acfg, aname->flags);
3132 emit_int32 (acfg, aname->major);
3133 emit_int32 (acfg, aname->minor);
3134 emit_int32 (acfg, aname->build);
3135 emit_int32 (acfg, aname->revision);
3140 emit_got_info (MonoAotCompile *acfg)
3145 guint32 *got_info_offsets;
3149 * - optimize offsets table.
3150 * - reduce number of exported symbols.
3151 * - emit info for a klass only once.
3152 * - determine when a method uses a GOT slot which is guaranteed to be already
3154 * - clean up and document the code.
3155 * - use String.Empty in class libs.
3158 /* Encode info required to decode shared GOT entries */
3159 buf_size = acfg->shared_patches->len * 64;
3160 p = buf = mono_mempool_alloc (acfg->mempool, buf_size);
3161 got_info_offsets = mono_mempool_alloc (acfg->mempool, acfg->shared_patches->len * sizeof (guint32));
3162 for (i = 0; i < acfg->shared_patches->len; ++i) {
3163 MonoJumpInfo *ji = g_ptr_array_index (acfg->shared_patches, i);
3165 /* No need to encode the patch type */
3166 got_info_offsets [i] = p - buf;
3167 encode_patch (acfg, ji, p, &p, FALSE);
3170 g_assert (p - buf <= buf_size);
3172 acfg->stats.got_info_size = p - buf;
3174 /* Emit got_info table */
3175 symbol = g_strdup_printf ("got_info");
3176 emit_section_change (acfg, ".text", 1);
3177 emit_global (acfg, symbol, FALSE);
3178 emit_alignment (acfg, 8);
3179 emit_label (acfg, symbol);
3181 emit_bytes (acfg, buf, p - buf);
3183 /* Emit got_info_offsets table */
3184 symbol = g_strdup_printf ("got_info_offsets");
3185 emit_section_change (acfg, ".text", 1);
3186 emit_global (acfg, symbol, FALSE);
3187 emit_alignment (acfg, 8);
3188 emit_label (acfg, symbol);
3190 for (i = 0; i < acfg->shared_patches->len; ++i)
3191 emit_int32 (acfg, got_info_offsets [i]);
3193 acfg->stats.got_info_offsets_size = acfg->shared_patches->len * 4;
3197 emit_got (MonoAotCompile *acfg)
3201 /* Don't make GOT global so accesses to it don't need relocations */
3202 symbol = g_strdup_printf ("got");
3203 emit_section_change (acfg, ".bss", 1);
3204 emit_alignment (acfg, 8);
3205 emit_label (acfg, symbol);
3206 if (acfg->got_offset > 0)
3207 emit_zero_bytes (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
3209 symbol = g_strdup_printf ("got_addr");
3210 emit_section_change (acfg, ".data", 1);
3211 emit_global (acfg, symbol, FALSE);
3212 emit_alignment (acfg, 8);
3213 emit_label (acfg, symbol);
3214 emit_pointer (acfg, "got");
3216 symbol = g_strdup_printf ("got_size");
3217 emit_section_change (acfg, ".data", 1);
3218 emit_global (acfg, symbol, FALSE);
3219 emit_alignment (acfg, 8);
3220 emit_label (acfg, symbol);
3221 emit_int32 (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
3225 emit_globals (MonoAotCompile *acfg)
3229 emit_string_symbol (acfg, "mono_assembly_guid" , acfg->image->guid);
3231 emit_string_symbol (acfg, "mono_aot_version", MONO_AOT_FILE_VERSION);
3233 opts_str = g_strdup_printf ("%d", acfg->opts);
3234 emit_string_symbol (acfg, "mono_aot_opt_flags", opts_str);
3239 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
3241 MonoImage *image = ass->image;
3244 MonoAotCompile *acfg;
3247 printf ("Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
3249 acfg = g_new0 (MonoAotCompile, 1);
3250 acfg->plt_offset_to_patch = g_hash_table_new (NULL, NULL);
3251 acfg->patch_to_plt_offset = g_hash_table_new (NULL, NULL);
3252 acfg->patch_to_shared_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
3253 acfg->shared_patches = g_ptr_array_new ();
3254 acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
3255 acfg->image_hash = g_hash_table_new (NULL, NULL);
3256 acfg->image_table = g_ptr_array_new ();
3257 acfg->image = image;
3259 acfg->mempool = mono_mempool_new ();
3261 mono_aot_parse_options (aot_options, &acfg->aot_opts);
3263 load_profile_files (acfg);
3265 if (mono_defaults.generic_nullable_class) {
3267 * FIXME: Its hard to skip generic methods or methods which use generics.
3269 printf ("Error: Can't AOT Net 2.0 assemblies.\n");
3275 cfgs = g_new0 (MonoCompile*, image->tables [MONO_TABLE_METHOD].rows + 32);
3277 acfg->nmethods = image->tables [MONO_TABLE_METHOD].rows;
3278 acfg->method_got_offsets = g_new0 (guint32, image->tables [MONO_TABLE_METHOD].rows + 32);
3279 acfg->has_got_slots = g_new0 (gboolean, image->tables [MONO_TABLE_METHOD].rows + 32);
3281 /* PLT offset 0 is reserved for the PLT trampoline */
3282 acfg->plt_offset = 1;
3284 /* Compile methods */
3285 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i)
3286 compile_method (acfg, i);
3288 alloc_got_slots (acfg);
3294 emit_method_order (acfg);
3296 emit_class_name_table (acfg);
3298 emit_got_info (acfg);
3300 emit_exception_info (acfg);
3302 emit_class_info (acfg);
3306 emit_image_table (acfg);
3310 emit_globals (acfg);
3312 symbol = g_strdup_printf ("mem_end");
3313 emit_section_change (acfg, ".text", 1);
3314 emit_global (acfg, symbol, FALSE);
3315 emit_alignment (acfg, 8);
3316 emit_label (acfg, symbol);
3318 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)));
3320 emit_writeout (acfg);
3322 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);
3323 printf ("%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
3324 printf ("%d methods contain wrapper references (%d%%)\n", acfg->stats.wrappercount, acfg->stats.mcount ? (acfg->stats.wrappercount * 100) / acfg->stats.mcount : 100);
3325 printf ("%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
3326 printf ("%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
3327 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);
3328 printf ("Direct calls: %d (%d%%)\n", acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
3330 printf ("GOT slot distribution:\n");
3331 for (i = 0; i < MONO_PATCH_INFO_NONE; ++i)
3332 if (acfg->stats.got_slot_types [i])
3333 printf ("\t%s: %d\n", get_patch_name (i), acfg->stats.got_slot_types [i]);
3343 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)