[interp] Include lmf wrapper in aot image
[mono-project.git] / mono / mini / aot-compiler.c
blobf856e987af17aa1ad13fcf8286875de294e51843
1 /**
2 * \file
3 * mono Ahead of Time compiler
5 * Author:
6 * Dietmar Maurer (dietmar@ximian.com)
7 * Zoltan Varga (vargaz@gmail.com)
8 * Johan Lorensson (lateralusx.github@gmail.com)
10 * (C) 2002 Ximian, Inc.
11 * Copyright 2003-2011 Novell, Inc
12 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include "config.h"
17 #include <sys/types.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #ifdef HAVE_STDINT_H
22 #include <stdint.h>
23 #endif
24 #include <fcntl.h>
25 #include <ctype.h>
26 #include <string.h>
27 #ifndef HOST_WIN32
28 #include <sys/time.h>
29 #else
30 #include <winsock2.h>
31 #include <windows.h>
32 #endif
34 #include <errno.h>
35 #include <sys/stat.h>
37 #include <mono/metadata/abi-details.h>
38 #include <mono/metadata/tabledefs.h>
39 #include <mono/metadata/class.h>
40 #include <mono/metadata/object.h>
41 #include <mono/metadata/tokentype.h>
42 #include <mono/metadata/appdomain.h>
43 #include <mono/metadata/debug-helpers.h>
44 #include <mono/metadata/assembly.h>
45 #include <mono/metadata/metadata-internals.h>
46 #include <mono/metadata/reflection-internals.h>
47 #include <mono/metadata/marshal.h>
48 #include <mono/metadata/gc-internals.h>
49 #include <mono/metadata/mempool-internals.h>
50 #include <mono/metadata/mono-endian.h>
51 #include <mono/metadata/threads-types.h>
52 #include <mono/metadata/custom-attrs-internals.h>
53 #include <mono/utils/mono-logger-internals.h>
54 #include <mono/utils/mono-compiler.h>
55 #include <mono/utils/mono-time.h>
56 #include <mono/utils/mono-mmap.h>
57 #include <mono/utils/mono-rand.h>
58 #include <mono/utils/json.h>
59 #include <mono/utils/mono-threads-coop.h>
60 #include <mono/profiler/aot.h>
61 #include <mono/utils/w32api.h>
63 #include "aot-compiler.h"
64 #include "aot-runtime.h"
65 #include "seq-points.h"
66 #include "image-writer.h"
67 #include "dwarfwriter.h"
68 #include "mini-gc.h"
69 #include "mini-llvm.h"
70 #include "mini-runtime.h"
72 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
74 // Use MSVC toolchain, Clang for MSVC using MSVC codegen and linker, when compiling for AMD64
75 // targeting WIN32 platforms running AOT compiler on WIN32 platform with VS installation.
76 #if defined(TARGET_AMD64) && defined(TARGET_WIN32) && defined(HOST_WIN32) && defined(_MSC_VER)
77 #define TARGET_X86_64_WIN32_MSVC
78 #endif
80 #if defined(TARGET_X86_64_WIN32_MSVC)
81 #define TARGET_WIN32_MSVC
82 #endif
84 // Emit native unwind info on Windows platforms (different from DWARF). Emitted unwind info
85 // works when using the MSVC toolchain using Clang for MSVC codegen and linker. Only supported when
86 // compiling for AMD64 (Windows x64 platforms).
87 #if defined(TARGET_WIN32_MSVC) && defined(MONO_ARCH_HAVE_UNWIND_TABLE)
88 #define EMIT_WIN32_UNWIND_INFO
89 #endif
91 #if defined(__linux__)
92 #define RODATA_SECT ".rodata"
93 #elif defined(TARGET_MACH)
94 #define RODATA_SECT ".section __TEXT, __const"
95 #elif defined(TARGET_WIN32_MSVC)
96 #define RODATA_SECT ".rdata"
97 #else
98 #define RODATA_SECT ".text"
99 #endif
101 #define TV_DECLARE(name) gint64 name
102 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
103 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
105 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
107 typedef struct {
108 char *name;
109 MonoImage *image;
110 } ImageProfileData;
112 typedef struct ClassProfileData ClassProfileData;
114 typedef struct {
115 int argc;
116 ClassProfileData **argv;
117 MonoGenericInst *inst;
118 } GInstProfileData;
120 struct ClassProfileData {
121 ImageProfileData *image;
122 char *ns, *name;
123 GInstProfileData *inst;
124 MonoClass *klass;
127 typedef struct {
128 ClassProfileData *klass;
129 int id;
130 char *name;
131 int param_count;
132 char *signature;
133 GInstProfileData *inst;
134 MonoMethod *method;
135 } MethodProfileData;
137 typedef struct {
138 GHashTable *images, *classes, *ginsts, *methods;
139 } ProfileData;
141 /* predefined values for static readonly fields without needed to run the .cctor */
142 typedef struct _ReadOnlyValue ReadOnlyValue;
143 struct _ReadOnlyValue {
144 ReadOnlyValue *next;
145 char *name;
146 int type; /* to be used later for typechecking to prevent user errors */
147 union {
148 guint8 i1;
149 guint16 i2;
150 guint32 i4;
151 guint64 i8;
152 gpointer ptr;
153 } value;
155 static ReadOnlyValue *readonly_values;
157 typedef struct MonoAotOptions {
158 char *outfile;
159 char *llvm_outfile;
160 char *data_outfile;
161 GList *profile_files;
162 gboolean save_temps;
163 gboolean write_symbols;
164 gboolean metadata_only;
165 gboolean bind_to_runtime_version;
166 MonoAotMode mode;
167 gboolean interp;
168 gboolean no_dlsym;
169 gboolean static_link;
170 gboolean asm_only;
171 gboolean asm_writer;
172 gboolean nodebug;
173 gboolean dwarf_debug;
174 gboolean soft_debug;
175 gboolean log_generics;
176 gboolean log_instances;
177 gboolean gen_msym_dir;
178 char *gen_msym_dir_path;
179 gboolean direct_pinvoke;
180 gboolean direct_icalls;
181 gboolean no_direct_calls;
182 gboolean use_trampolines_page;
183 gboolean no_instances;
184 // We are collecting inflated methods and emitting non-inflated
185 gboolean dedup;
186 // The name of the assembly for which the AOT module is going to have all deduped methods moved to.
187 // When set, we are emitting inflated methods only
188 char *dedup_include;
189 gboolean gnu_asm;
190 gboolean try_llvm;
191 gboolean llvm;
192 gboolean llvm_only;
193 int nthreads;
194 int ntrampolines;
195 int nrgctx_trampolines;
196 int nimt_trampolines;
197 int ngsharedvt_arg_trampolines;
198 int nrgctx_fetch_trampolines;
199 gboolean print_skipped_methods;
200 gboolean stats;
201 gboolean verbose;
202 char *tool_prefix;
203 char *ld_flags;
204 char *mtriple;
205 char *llvm_path;
206 char *temp_path;
207 char *instances_logfile_path;
208 char *logfile;
209 char *llvm_opts;
210 gboolean dump_json;
211 gboolean profile_only;
212 } MonoAotOptions;
214 typedef enum {
215 METHOD_CAT_NORMAL,
216 METHOD_CAT_GSHAREDVT,
217 METHOD_CAT_INST,
218 METHOD_CAT_WRAPPER,
219 METHOD_CAT_NUM
220 } MethodCategory;
222 typedef struct MonoAotStats {
223 int ccount, mcount, lmfcount, abscount, gcount, ocount, genericcount;
224 gint64 code_size, info_size, ex_info_size, unwind_info_size, got_size, class_info_size, got_info_size, plt_size;
225 int methods_without_got_slots, direct_calls, all_calls, llvm_count;
226 int got_slots, offsets_size;
227 int method_categories [METHOD_CAT_NUM];
228 int got_slot_types [MONO_PATCH_INFO_NUM];
229 int got_slot_info_sizes [MONO_PATCH_INFO_NUM];
230 int jit_time, gen_time, link_time;
231 } MonoAotStats;
233 typedef struct GotInfo {
234 GHashTable *patch_to_got_offset;
235 GHashTable **patch_to_got_offset_by_type;
236 GPtrArray *got_patches;
237 } GotInfo;
239 #ifdef EMIT_WIN32_UNWIND_INFO
240 typedef struct _UnwindInfoSectionCacheItem {
241 char *xdata_section_label;
242 PUNWIND_INFO unwind_info;
243 gboolean xdata_section_emitted;
244 } UnwindInfoSectionCacheItem;
245 #endif
247 typedef struct MonoAotCompile {
248 MonoImage *image;
249 GPtrArray *methods;
250 GHashTable *method_indexes;
251 GHashTable *method_depth;
252 MonoCompile **cfgs;
253 int cfgs_size;
254 GHashTable **patch_to_plt_entry;
255 GHashTable *plt_offset_to_entry;
256 //GHashTable *patch_to_got_offset;
257 //GHashTable **patch_to_got_offset_by_type;
258 //GPtrArray *got_patches;
259 GotInfo got_info, llvm_got_info;
260 GHashTable *image_hash;
261 GHashTable *method_to_cfg;
262 GHashTable *token_info_hash;
263 GHashTable *method_to_pinvoke_import;
264 GPtrArray *extra_methods;
265 GPtrArray *image_table;
266 GPtrArray *globals;
267 GPtrArray *method_order;
268 GHashTable *dedup_stats;
269 GHashTable *dedup_cache;
270 gboolean dedup_cache_changed;
271 GHashTable *export_names;
272 /* Maps MonoClass* -> blob offset */
273 GHashTable *klass_blob_hash;
274 /* Maps MonoMethod* -> blob offset */
275 GHashTable *method_blob_hash;
276 GHashTable *gsharedvt_in_signatures;
277 GHashTable *gsharedvt_out_signatures;
278 guint32 *plt_got_info_offsets;
279 guint32 got_offset, llvm_got_offset, plt_offset, plt_got_offset_base, nshared_got_entries;
280 /* Number of GOT entries reserved for trampolines */
281 guint32 num_trampoline_got_entries;
282 guint32 tramp_page_size;
284 guint32 table_offsets [MONO_AOT_TABLE_NUM];
285 guint32 num_trampolines [MONO_AOT_TRAMP_NUM];
286 guint32 trampoline_got_offset_base [MONO_AOT_TRAMP_NUM];
287 guint32 trampoline_size [MONO_AOT_TRAMP_NUM];
288 guint32 tramp_page_code_offsets [MONO_AOT_TRAMP_NUM];
290 MonoAotOptions aot_opts;
291 guint32 nmethods;
292 guint32 opts;
293 guint32 simd_opts;
294 MonoMemPool *mempool;
295 MonoAotStats stats;
296 int method_index;
297 char *static_linking_symbol;
298 mono_mutex_t mutex;
299 gboolean gas_line_numbers;
300 /* Whenever to emit an object file directly from llc */
301 gboolean llvm_owriter;
302 gboolean llvm_owriter_supported;
303 MonoImageWriter *w;
304 MonoDwarfWriter *dwarf;
305 FILE *fp;
306 char *tmpbasename;
307 char *tmpfname;
308 char *llvm_sfile;
309 char *llvm_ofile;
310 GSList *cie_program;
311 GHashTable *unwind_info_offsets;
312 GPtrArray *unwind_ops;
313 guint32 unwind_info_offset;
314 char *global_prefix;
315 char *got_symbol;
316 char *llvm_got_symbol;
317 char *plt_symbol;
318 char *llvm_eh_frame_symbol;
319 GHashTable *method_label_hash;
320 const char *temp_prefix;
321 const char *user_symbol_prefix;
322 const char *llvm_label_prefix;
323 const char *inst_directive;
324 int align_pad_value;
325 guint32 label_generator;
326 gboolean llvm;
327 gboolean has_jitted_code;
328 gboolean is_full_aot;
329 MonoAotFileFlags flags;
330 MonoDynamicStream blob;
331 gboolean blob_closed;
332 GHashTable *typespec_classes;
333 GString *llc_args;
334 GString *as_args;
335 char *assembly_name_sym;
336 GHashTable *plt_entry_debug_sym_cache;
337 gboolean thumb_mixed, need_no_dead_strip, need_pt_gnu_stack;
338 GHashTable *ginst_hash;
339 GHashTable *dwarf_ln_filenames;
340 gboolean global_symbols;
341 int objc_selector_index, objc_selector_index_2;
342 GPtrArray *objc_selectors;
343 GHashTable *objc_selector_to_index;
344 GList *profile_data;
345 GHashTable *profile_methods;
346 #ifdef EMIT_WIN32_UNWIND_INFO
347 GList *unwind_info_section_cache;
348 #endif
349 FILE *logfile;
350 FILE *instances_logfile;
351 FILE *data_outfile;
352 int datafile_offset;
353 int gc_name_offset;
354 // In this mode, we are emitting dedupable methods that we encounter
355 gboolean dedup_emit_mode;
356 } MonoAotCompile;
358 typedef struct {
359 int plt_offset;
360 char *symbol, *llvm_symbol, *debug_sym;
361 MonoJumpInfo *ji;
362 gboolean jit_used, llvm_used;
363 } MonoPltEntry;
365 #define mono_acfg_lock(acfg) mono_os_mutex_lock (&((acfg)->mutex))
366 #define mono_acfg_unlock(acfg) mono_os_mutex_unlock (&((acfg)->mutex))
368 /* This points to the current acfg in LLVM mode */
369 static MonoAotCompile *llvm_acfg;
371 #ifdef HAVE_ARRAY_ELEM_INIT
372 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
373 #define MSGSTRFIELD1(line) str##line
374 static const struct msgstr_t {
375 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
376 #include "patch-info.h"
377 #undef PATCH_INFO
378 } opstr = {
379 #define PATCH_INFO(a,b) b,
380 #include "patch-info.h"
381 #undef PATCH_INFO
383 static const gint16 opidx [] = {
384 #define PATCH_INFO(a,b) [MONO_PATCH_INFO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
385 #include "patch-info.h"
386 #undef PATCH_INFO
389 static G_GNUC_UNUSED const char*
390 get_patch_name (int info)
392 return (const char*)&opstr + opidx [info];
395 #else
396 #define PATCH_INFO(a,b) b,
397 static const char* const
398 patch_types [MONO_PATCH_INFO_NUM + 1] = {
399 #include "patch-info.h"
400 NULL
403 static G_GNUC_UNUSED const char*
404 get_patch_name (int info)
406 return patch_types [info];
409 #endif
411 static void
412 mono_flush_method_cache (MonoAotCompile *acfg);
414 static void
415 mono_read_method_cache (MonoAotCompile *acfg);
417 static guint32
418 get_unwind_info_offset (MonoAotCompile *acfg, guint8 *encoded, guint32 encoded_len);
420 static char*
421 get_plt_entry_debug_sym (MonoAotCompile *acfg, MonoJumpInfo *ji, GHashTable *cache);
423 static void
424 add_gsharedvt_wrappers (MonoAotCompile *acfg, MonoMethodSignature *sig, gboolean gsharedvt_in, gboolean gsharedvt_out, gboolean interp_in);
426 static void
427 add_profile_instances (MonoAotCompile *acfg, ProfileData *data);
429 static inline gboolean
430 ignore_cfg (MonoCompile *cfg)
432 return !cfg || cfg->skip;
435 static void
436 aot_printf (MonoAotCompile *acfg, const gchar *format, ...)
438 FILE *output;
439 va_list args;
441 if (acfg->logfile)
442 output = acfg->logfile;
443 else
444 output = stdout;
446 va_start (args, format);
447 vfprintf (output, format, args);
448 va_end (args);
451 static void
452 aot_printerrf (MonoAotCompile *acfg, const gchar *format, ...)
454 FILE *output;
455 va_list args;
457 if (acfg->logfile)
458 output = acfg->logfile;
459 else
460 output = stderr;
462 va_start (args, format);
463 vfprintf (output, format, args);
464 va_end (args);
467 static void
468 report_loader_error (MonoAotCompile *acfg, MonoError *error, gboolean fatal, const char *format, ...)
470 FILE *output;
471 va_list args;
473 if (mono_error_ok (error))
474 return;
476 if (acfg->logfile)
477 output = acfg->logfile;
478 else
479 output = stderr;
481 va_start (args, format);
482 vfprintf (output, format, args);
483 va_end (args);
484 mono_error_cleanup (error);
486 if (acfg->is_full_aot && fatal) {
487 fprintf (output, "FullAOT cannot continue if there are loader errors.\n");
488 exit (1);
492 /* Wrappers around the image writer functions */
494 #define MAX_SYMBOL_SIZE 256
496 static inline const char *
497 mangle_symbol (const char * symbol, char * mangled_symbol, gsize length)
499 gsize needed_size = length;
501 g_assert (NULL != symbol);
502 g_assert (NULL != mangled_symbol);
503 g_assert (0 != length);
505 #if defined(TARGET_WIN32) && defined(TARGET_X86)
506 if (symbol && '_' != symbol [0]) {
507 needed_size = g_snprintf (mangled_symbol, length, "_%s", symbol);
508 } else {
509 needed_size = g_snprintf (mangled_symbol, length, "%s", symbol);
511 #else
512 needed_size = g_snprintf (mangled_symbol, length, "%s", symbol);
513 #endif
515 g_assert (0 <= needed_size && needed_size < length);
516 return mangled_symbol;
519 static inline char *
520 mangle_symbol_alloc (const char * symbol)
522 g_assert (NULL != symbol);
524 #if defined(TARGET_WIN32) && defined(TARGET_X86)
525 if (symbol && '_' != symbol [0]) {
526 return g_strdup_printf ("_%s", symbol);
528 else {
529 return g_strdup_printf ("%s", symbol);
531 #else
532 return g_strdup_printf ("%s", symbol);
533 #endif
536 static inline void
537 emit_section_change (MonoAotCompile *acfg, const char *section_name, int subsection_index)
539 mono_img_writer_emit_section_change (acfg->w, section_name, subsection_index);
542 #if defined(TARGET_WIN32) && defined(TARGET_X86)
544 static inline void
545 emit_local_symbol (MonoAotCompile *acfg, const char *name, const char *end_label, gboolean func)
547 const char * mangled_symbol_name = name;
548 char * mangled_symbol_name_alloc = NULL;
550 if (TRUE == func) {
551 mangled_symbol_name_alloc = mangle_symbol_alloc (name);
552 mangled_symbol_name = mangled_symbol_name_alloc;
555 if (name != mangled_symbol_name && 0 != g_strcasecmp (name, mangled_symbol_name)) {
556 mono_img_writer_emit_label (acfg->w, mangled_symbol_name);
558 mono_img_writer_emit_local_symbol (acfg->w, mangled_symbol_name, end_label, func);
560 if (NULL != mangled_symbol_name_alloc) {
561 g_free (mangled_symbol_name_alloc);
565 #else
567 static inline void
568 emit_local_symbol (MonoAotCompile *acfg, const char *name, const char *end_label, gboolean func)
570 mono_img_writer_emit_local_symbol (acfg->w, name, end_label, func);
573 #endif
575 static inline void
576 emit_label (MonoAotCompile *acfg, const char *name)
578 mono_img_writer_emit_label (acfg->w, name);
581 static inline void
582 emit_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
584 mono_img_writer_emit_bytes (acfg->w, buf, size);
587 static inline void
588 emit_string (MonoAotCompile *acfg, const char *value)
590 mono_img_writer_emit_string (acfg->w, value);
593 static inline void
594 emit_line (MonoAotCompile *acfg)
596 mono_img_writer_emit_line (acfg->w);
599 static inline void
600 emit_alignment (MonoAotCompile *acfg, int size)
602 mono_img_writer_emit_alignment (acfg->w, size);
605 static inline void
606 emit_alignment_code (MonoAotCompile *acfg, int size)
608 if (acfg->align_pad_value)
609 mono_img_writer_emit_alignment_fill (acfg->w, size, acfg->align_pad_value);
610 else
611 mono_img_writer_emit_alignment (acfg->w, size);
614 static inline void
615 emit_padding (MonoAotCompile *acfg, int size)
617 int i;
618 guint8 buf [16];
620 if (acfg->align_pad_value) {
621 for (i = 0; i < 16; ++i)
622 buf [i] = acfg->align_pad_value;
623 } else {
624 memset (buf, 0, sizeof (buf));
627 for (i = 0; i < size; i += 16) {
628 if (size - i < 16)
629 emit_bytes (acfg, buf, size - i);
630 else
631 emit_bytes (acfg, buf, 16);
635 static inline void
636 emit_pointer (MonoAotCompile *acfg, const char *target)
638 mono_img_writer_emit_pointer (acfg->w, target);
641 static inline void
642 emit_pointer_2 (MonoAotCompile *acfg, const char *prefix, const char *target)
644 if (prefix [0] != '\0') {
645 char *s = g_strdup_printf ("%s%s", prefix, target);
646 mono_img_writer_emit_pointer (acfg->w, s);
647 g_free (s);
648 } else {
649 mono_img_writer_emit_pointer (acfg->w, target);
653 static inline void
654 emit_int16 (MonoAotCompile *acfg, int value)
656 mono_img_writer_emit_int16 (acfg->w, value);
659 static inline void
660 emit_int32 (MonoAotCompile *acfg, int value)
662 mono_img_writer_emit_int32 (acfg->w, value);
665 static inline void
666 emit_symbol_diff (MonoAotCompile *acfg, const char *end, const char* start, int offset)
668 mono_img_writer_emit_symbol_diff (acfg->w, end, start, offset);
671 static inline void
672 emit_zero_bytes (MonoAotCompile *acfg, int num)
674 mono_img_writer_emit_zero_bytes (acfg->w, num);
677 static inline void
678 emit_byte (MonoAotCompile *acfg, guint8 val)
680 mono_img_writer_emit_byte (acfg->w, val);
683 #if defined(TARGET_WIN32) && defined(TARGET_X86)
685 static G_GNUC_UNUSED void
686 emit_global_inner (MonoAotCompile *acfg, const char *name, gboolean func)
688 const char * mangled_symbol_name = name;
689 char * mangled_symbol_name_alloc = NULL;
691 mangled_symbol_name_alloc = mangle_symbol_alloc (name);
692 mangled_symbol_name = mangled_symbol_name_alloc;
694 if (0 != g_strcasecmp (name, mangled_symbol_name)) {
695 mono_img_writer_emit_label (acfg->w, mangled_symbol_name);
697 mono_img_writer_emit_global (acfg->w, mangled_symbol_name, func);
699 if (NULL != mangled_symbol_name_alloc) {
700 g_free (mangled_symbol_name_alloc);
704 #else
706 static G_GNUC_UNUSED void
707 emit_global_inner (MonoAotCompile *acfg, const char *name, gboolean func)
709 mono_img_writer_emit_global (acfg->w, name, func);
712 #endif
714 static inline gboolean
715 link_shared_library (MonoAotCompile *acfg)
717 return !acfg->aot_opts.static_link && !acfg->aot_opts.asm_only;
720 static inline gboolean
721 add_to_global_symbol_table (MonoAotCompile *acfg)
723 #ifdef TARGET_WIN32_MSVC
724 return acfg->aot_opts.no_dlsym || link_shared_library (acfg);
725 #else
726 return acfg->aot_opts.no_dlsym;
727 #endif
730 static void
731 emit_global (MonoAotCompile *acfg, const char *name, gboolean func)
733 if (add_to_global_symbol_table (acfg))
734 g_ptr_array_add (acfg->globals, g_strdup (name));
736 if (acfg->aot_opts.no_dlsym) {
737 mono_img_writer_emit_local_symbol (acfg->w, name, NULL, func);
738 } else {
739 emit_global_inner (acfg, name, func);
743 static void
744 emit_symbol_size (MonoAotCompile *acfg, const char *name, const char *end_label)
746 mono_img_writer_emit_symbol_size (acfg->w, name, end_label);
749 /* Emit a symbol which is referenced by the MonoAotFileInfo structure */
750 static void
751 emit_info_symbol (MonoAotCompile *acfg, const char *name)
753 char symbol [MAX_SYMBOL_SIZE];
755 if (acfg->llvm) {
756 emit_label (acfg, name);
757 /* LLVM generated code references this */
758 sprintf (symbol, "%s%s%s", acfg->user_symbol_prefix, acfg->global_prefix, name);
759 emit_label (acfg, symbol);
760 emit_global_inner (acfg, symbol, FALSE);
761 } else {
762 emit_label (acfg, name);
766 static void
767 emit_string_symbol (MonoAotCompile *acfg, const char *name, const char *value)
769 if (acfg->llvm) {
770 mono_llvm_emit_aot_data (name, (guint8*)value, strlen (value) + 1);
771 return;
774 mono_img_writer_emit_section_change (acfg->w, RODATA_SECT, 1);
775 #ifdef TARGET_MACH
776 /* On apple, all symbols need to be aligned to avoid warnings from ld */
777 emit_alignment (acfg, 4);
778 #endif
779 mono_img_writer_emit_label (acfg->w, name);
780 mono_img_writer_emit_string (acfg->w, value);
783 static G_GNUC_UNUSED void
784 emit_uleb128 (MonoAotCompile *acfg, guint32 value)
786 do {
787 guint8 b = value & 0x7f;
788 value >>= 7;
789 if (value != 0) /* more bytes to come */
790 b |= 0x80;
791 emit_byte (acfg, b);
792 } while (value);
795 static G_GNUC_UNUSED void
796 emit_sleb128 (MonoAotCompile *acfg, gint64 value)
798 gboolean more = 1;
799 gboolean negative = (value < 0);
800 guint32 size = 64;
801 guint8 byte;
803 while (more) {
804 byte = value & 0x7f;
805 value >>= 7;
806 /* the following is unnecessary if the
807 * implementation of >>= uses an arithmetic rather
808 * than logical shift for a signed left operand
810 if (negative)
811 /* sign extend */
812 value |= - ((gint64)1 <<(size - 7));
813 /* sign bit of byte is second high order bit (0x40) */
814 if ((value == 0 && !(byte & 0x40)) ||
815 (value == -1 && (byte & 0x40)))
816 more = 0;
817 else
818 byte |= 0x80;
819 emit_byte (acfg, byte);
823 static G_GNUC_UNUSED void
824 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
826 guint8 *p = buf;
828 do {
829 guint8 b = value & 0x7f;
830 value >>= 7;
831 if (value != 0) /* more bytes to come */
832 b |= 0x80;
833 *p ++ = b;
834 } while (value);
836 *endbuf = p;
839 static G_GNUC_UNUSED void
840 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
842 gboolean more = 1;
843 gboolean negative = (value < 0);
844 guint32 size = 32;
845 guint8 byte;
846 guint8 *p = buf;
848 while (more) {
849 byte = value & 0x7f;
850 value >>= 7;
851 /* the following is unnecessary if the
852 * implementation of >>= uses an arithmetic rather
853 * than logical shift for a signed left operand
855 if (negative)
856 /* sign extend */
857 value |= - (1 <<(size - 7));
858 /* sign bit of byte is second high order bit (0x40) */
859 if ((value == 0 && !(byte & 0x40)) ||
860 (value == -1 && (byte & 0x40)))
861 more = 0;
862 else
863 byte |= 0x80;
864 *p ++= byte;
867 *endbuf = p;
870 static void
871 encode_int (gint32 val, guint8 *buf, guint8 **endbuf)
873 // FIXME: Big-endian
874 buf [0] = (val >> 0) & 0xff;
875 buf [1] = (val >> 8) & 0xff;
876 buf [2] = (val >> 16) & 0xff;
877 buf [3] = (val >> 24) & 0xff;
879 *endbuf = buf + 4;
882 static void
883 encode_int16 (guint16 val, guint8 *buf, guint8 **endbuf)
885 buf [0] = (val >> 0) & 0xff;
886 buf [1] = (val >> 8) & 0xff;
888 *endbuf = buf + 2;
891 static void
892 encode_string (const char *s, guint8 *buf, guint8 **endbuf)
894 int len = strlen (s);
896 memcpy (buf, s, len + 1);
897 *endbuf = buf + len + 1;
900 static void
901 emit_unset_mode (MonoAotCompile *acfg)
903 mono_img_writer_emit_unset_mode (acfg->w);
906 static G_GNUC_UNUSED void
907 emit_set_thumb_mode (MonoAotCompile *acfg)
909 emit_unset_mode (acfg);
910 fprintf (acfg->fp, ".code 16\n");
913 static G_GNUC_UNUSED void
914 emit_set_arm_mode (MonoAotCompile *acfg)
916 emit_unset_mode (acfg);
917 fprintf (acfg->fp, ".code 32\n");
920 static inline void
921 emit_code_bytes (MonoAotCompile *acfg, const guint8* buf, int size)
923 #ifdef TARGET_ARM64
924 int i;
926 g_assert (size % 4 == 0);
927 emit_unset_mode (acfg);
928 for (i = 0; i < size; i += 4)
929 fprintf (acfg->fp, "%s 0x%x\n", acfg->inst_directive, *(guint32*)(buf + i));
930 #else
931 emit_bytes (acfg, buf, size);
932 #endif
935 /* ARCHITECTURE SPECIFIC CODE */
937 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC) || defined(TARGET_ARM64)
938 #define EMIT_DWARF_INFO 1
939 #endif
941 #ifdef TARGET_WIN32_MSVC
942 #undef EMIT_DWARF_INFO
943 #define EMIT_WIN32_CODEVIEW_INFO
944 #endif
946 #ifdef EMIT_WIN32_UNWIND_INFO
947 static UnwindInfoSectionCacheItem *
948 get_cached_unwind_info_section_item_win32 (MonoAotCompile *acfg, const char *function_start, const char *function_end, GSList *unwind_ops);
950 static void
951 free_unwind_info_section_cache_win32 (MonoAotCompile *acfg);
953 static void
954 emit_unwind_info_data_win32 (MonoAotCompile *acfg, PUNWIND_INFO unwind_info);
956 static void
957 emit_unwind_info_sections_win32 (MonoAotCompile *acfg, const char *function_start, const char *function_end, GSList *unwind_ops);
958 #endif
960 static void
961 arch_free_unwind_info_section_cache (MonoAotCompile *acfg)
963 #ifdef EMIT_WIN32_UNWIND_INFO
964 free_unwind_info_section_cache_win32 (acfg);
965 #endif
968 static void
969 arch_emit_unwind_info_sections (MonoAotCompile *acfg, const char *function_start, const char *function_end, GSList *unwind_ops)
971 #ifdef EMIT_WIN32_UNWIND_INFO
972 gboolean own_unwind_ops = FALSE;
973 if (!unwind_ops) {
974 unwind_ops = mono_unwind_get_cie_program ();
975 own_unwind_ops = TRUE;
978 emit_unwind_info_sections_win32 (acfg, function_start, function_end, unwind_ops);
980 if (own_unwind_ops)
981 mono_free_unwind_info (unwind_ops);
982 #endif
985 #if defined(TARGET_ARM)
986 #define AOT_FUNC_ALIGNMENT 4
987 #else
988 #define AOT_FUNC_ALIGNMENT 16
989 #endif
991 #if defined(TARGET_POWERPC64) && !defined(__mono_ilp32__)
992 #define PPC_LD_OP "ld"
993 #define PPC_LDX_OP "ldx"
994 #else
995 #define PPC_LD_OP "lwz"
996 #define PPC_LDX_OP "lwzx"
997 #endif
999 #ifdef TARGET_X86_64_WIN32_MSVC
1000 #define AOT_TARGET_STR "AMD64 (WIN32) (MSVC codegen)"
1001 #elif TARGET_AMD64
1002 #define AOT_TARGET_STR "AMD64"
1003 #endif
1005 #ifdef TARGET_ARM
1006 #ifdef TARGET_MACH
1007 #define AOT_TARGET_STR "ARM (MACH)"
1008 #else
1009 #define AOT_TARGET_STR "ARM (!MACH)"
1010 #endif
1011 #endif
1013 #ifdef TARGET_ARM64
1014 #ifdef TARGET_MACH
1015 #define AOT_TARGET_STR "ARM64 (MACH)"
1016 #else
1017 #define AOT_TARGET_STR "ARM64 (!MACH)"
1018 #endif
1019 #endif
1021 #ifdef TARGET_POWERPC64
1022 #ifdef __mono_ilp32__
1023 #define AOT_TARGET_STR "POWERPC64 (mono ilp32)"
1024 #else
1025 #define AOT_TARGET_STR "POWERPC64 (!mono ilp32)"
1026 #endif
1027 #else
1028 #ifdef TARGET_POWERPC
1029 #ifdef __mono_ilp32__
1030 #define AOT_TARGET_STR "POWERPC (mono ilp32)"
1031 #else
1032 #define AOT_TARGET_STR "POWERPC (!mono ilp32)"
1033 #endif
1034 #endif
1035 #endif
1037 #ifdef TARGET_X86
1038 #ifdef TARGET_WIN32
1039 #define AOT_TARGET_STR "X86 (WIN32)"
1040 #else
1041 #define AOT_TARGET_STR "X86"
1042 #endif
1043 #endif
1045 #ifndef AOT_TARGET_STR
1046 #define AOT_TARGET_STR ""
1047 #endif
1049 static void
1050 arch_init (MonoAotCompile *acfg)
1052 acfg->llc_args = g_string_new ("");
1053 acfg->as_args = g_string_new ("");
1054 acfg->llvm_owriter_supported = TRUE;
1057 * The prefix LLVM likes to put in front of symbol names on darwin.
1058 * The mach-os specs require this for globals, but LLVM puts them in front of all
1059 * symbols. We need to handle this, since we need to refer to LLVM generated
1060 * symbols.
1062 acfg->llvm_label_prefix = "";
1063 acfg->user_symbol_prefix = "";
1065 #if defined(TARGET_X86)
1066 g_string_append (acfg->llc_args, " -march=x86 -mattr=sse4.1");
1067 #endif
1069 #if defined(TARGET_AMD64)
1070 g_string_append (acfg->llc_args, " -march=x86-64 -mattr=sse4.1");
1071 /* NOP */
1072 acfg->align_pad_value = 0x90;
1073 #endif
1075 #ifdef TARGET_ARM
1076 if (acfg->aot_opts.mtriple && strstr (acfg->aot_opts.mtriple, "darwin")) {
1077 g_string_append (acfg->llc_args, "-mattr=+v6");
1078 } else {
1079 #if defined(ARM_FPU_VFP_HARD)
1080 g_string_append (acfg->llc_args, " -mattr=+vfp2,-neon,+d16 -float-abi=hard");
1081 g_string_append (acfg->as_args, " -mfpu=vfp3");
1082 #elif defined(ARM_FPU_VFP)
1083 g_string_append (acfg->llc_args, " -mattr=+vfp2,-neon,+d16");
1084 g_string_append (acfg->as_args, " -mfpu=vfp3");
1085 #else
1086 g_string_append (acfg->llc_args, " -soft-float");
1087 #endif
1089 if (acfg->aot_opts.mtriple && strstr (acfg->aot_opts.mtriple, "thumb"))
1090 acfg->thumb_mixed = TRUE;
1092 if (acfg->aot_opts.mtriple)
1093 mono_arch_set_target (acfg->aot_opts.mtriple);
1094 #endif
1096 #ifdef TARGET_ARM64
1097 acfg->inst_directive = ".inst";
1098 if (acfg->aot_opts.mtriple)
1099 mono_arch_set_target (acfg->aot_opts.mtriple);
1100 #endif
1102 #ifdef TARGET_MACH
1103 acfg->user_symbol_prefix = "_";
1104 acfg->llvm_label_prefix = "_";
1105 acfg->inst_directive = ".word";
1106 acfg->need_no_dead_strip = TRUE;
1107 acfg->aot_opts.gnu_asm = TRUE;
1108 #endif
1110 #if defined(__linux__) && !defined(TARGET_ARM)
1111 acfg->need_pt_gnu_stack = TRUE;
1112 #endif
1114 #ifdef MONOTOUCH
1115 acfg->global_symbols = TRUE;
1116 #endif
1118 #ifdef TARGET_ANDROID
1119 acfg->llvm_owriter_supported = FALSE;
1120 #endif
1123 #ifdef TARGET_ARM64
1126 /* Load the contents of GOT_SLOT into dreg, clobbering ip0 */
1127 static void
1128 arm64_emit_load_got_slot (MonoAotCompile *acfg, int dreg, int got_slot)
1130 int offset;
1132 g_assert (acfg->fp);
1133 emit_unset_mode (acfg);
1134 /* r16==ip0 */
1135 offset = (int)(got_slot * sizeof (gpointer));
1136 #ifdef TARGET_MACH
1137 /* clang's integrated assembler */
1138 fprintf (acfg->fp, "adrp x16, %s@PAGE+%d\n", acfg->got_symbol, offset & 0xfffff000);
1139 fprintf (acfg->fp, "add x16, x16, %s@PAGEOFF\n", acfg->got_symbol);
1140 fprintf (acfg->fp, "ldr x%d, [x16, #%d]\n", dreg, offset & 0xfff);
1141 #else
1142 /* Linux GAS */
1143 fprintf (acfg->fp, "adrp x16, %s+%d\n", acfg->got_symbol, offset & 0xfffff000);
1144 fprintf (acfg->fp, "add x16, x16, :lo12:%s\n", acfg->got_symbol);
1145 fprintf (acfg->fp, "ldr x%d, [x16, %d]\n", dreg, offset & 0xfff);
1146 #endif
1149 static void
1150 arm64_emit_objc_selector_ref (MonoAotCompile *acfg, guint8 *code, int index, int *code_size)
1152 int reg;
1154 g_assert (acfg->fp);
1155 emit_unset_mode (acfg);
1157 /* ldr rt, target */
1158 reg = arm_get_ldr_lit_reg (code);
1160 fprintf (acfg->fp, "adrp x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGE\n", reg, index);
1161 fprintf (acfg->fp, "add x%d, x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGEOFF\n", reg, reg, index);
1162 fprintf (acfg->fp, "ldr x%d, [x%d]\n", reg, reg);
1164 *code_size = 12;
1167 static void
1168 arm64_emit_direct_call (MonoAotCompile *acfg, const char *target, gboolean external, gboolean thumb, MonoJumpInfo *ji, int *call_size)
1170 g_assert (acfg->fp);
1171 emit_unset_mode (acfg);
1172 if (ji && ji->relocation == MONO_R_ARM64_B) {
1173 fprintf (acfg->fp, "b %s\n", target);
1174 } else {
1175 if (ji)
1176 g_assert (ji->relocation == MONO_R_ARM64_BL);
1177 fprintf (acfg->fp, "bl %s\n", target);
1179 *call_size = 4;
1182 static void
1183 arm64_emit_got_access (MonoAotCompile *acfg, guint8 *code, int got_slot, int *code_size)
1185 int reg;
1187 /* ldr rt, target */
1188 reg = arm_get_ldr_lit_reg (code);
1189 arm64_emit_load_got_slot (acfg, reg, got_slot);
1190 *code_size = 12;
1193 static void
1194 arm64_emit_plt_entry (MonoAotCompile *acfg, const char *got_symbol, int offset, int info_offset)
1196 arm64_emit_load_got_slot (acfg, ARMREG_R16, offset / sizeof (gpointer));
1197 fprintf (acfg->fp, "br x16\n");
1198 /* Used by mono_aot_get_plt_info_offset () */
1199 fprintf (acfg->fp, "%s %d\n", acfg->inst_directive, info_offset);
1202 static void
1203 arm64_emit_tramp_page_common_code (MonoAotCompile *acfg, int pagesize, int arg_reg, int *size)
1205 guint8 buf [256];
1206 guint8 *code;
1207 int imm;
1209 /* The common code */
1210 code = buf;
1211 imm = pagesize;
1212 /* The trampoline address is in IP0 */
1213 arm_movzx (code, ARMREG_IP1, imm & 0xffff, 0);
1214 arm_movkx (code, ARMREG_IP1, (imm >> 16) & 0xffff, 16);
1215 /* Compute the data slot address */
1216 arm_subx (code, ARMREG_IP0, ARMREG_IP0, ARMREG_IP1);
1217 /* Trampoline argument */
1218 arm_ldrx (code, arg_reg, ARMREG_IP0, 0);
1219 /* Address */
1220 arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, 8);
1221 arm_brx (code, ARMREG_IP0);
1223 /* Emit it */
1224 emit_code_bytes (acfg, buf, code - buf);
1226 *size = code - buf;
1229 static void
1230 arm64_emit_tramp_page_specific_code (MonoAotCompile *acfg, int pagesize, int common_tramp_size, int specific_tramp_size)
1232 guint8 buf [256];
1233 guint8 *code;
1234 int i, count;
1236 count = (pagesize - common_tramp_size) / specific_tramp_size;
1237 for (i = 0; i < count; ++i) {
1238 code = buf;
1239 arm_adrx (code, ARMREG_IP0, code);
1240 /* Branch to the generic code */
1241 arm_b (code, code - 4 - (i * specific_tramp_size) - common_tramp_size);
1242 /* This has to be 2 pointers long */
1243 arm_nop (code);
1244 arm_nop (code);
1245 g_assert (code - buf == specific_tramp_size);
1246 emit_code_bytes (acfg, buf, code - buf);
1250 static void
1251 arm64_emit_specific_trampoline_pages (MonoAotCompile *acfg)
1253 guint8 buf [128];
1254 guint8 *code;
1255 guint8 *labels [16];
1256 int common_tramp_size;
1257 int specific_tramp_size = 2 * 8;
1258 int imm, pagesize;
1259 char symbol [128];
1261 if (!acfg->aot_opts.use_trampolines_page)
1262 return;
1264 #ifdef TARGET_MACH
1265 /* Have to match the target pagesize */
1266 pagesize = 16384;
1267 #else
1268 pagesize = mono_pagesize ();
1269 #endif
1270 acfg->tramp_page_size = pagesize;
1272 /* The specific trampolines */
1273 sprintf (symbol, "%sspecific_trampolines_page", acfg->user_symbol_prefix);
1274 emit_alignment (acfg, pagesize);
1275 emit_global (acfg, symbol, TRUE);
1276 emit_label (acfg, symbol);
1278 /* The common code */
1279 arm64_emit_tramp_page_common_code (acfg, pagesize, ARMREG_IP1, &common_tramp_size);
1280 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_SPECIFIC] = common_tramp_size;
1282 arm64_emit_tramp_page_specific_code (acfg, pagesize, common_tramp_size, specific_tramp_size);
1284 /* The rgctx trampolines */
1285 /* These are the same as the specific trampolines, but they load the argument into MONO_ARCH_RGCTX_REG */
1286 sprintf (symbol, "%srgctx_trampolines_page", acfg->user_symbol_prefix);
1287 emit_alignment (acfg, pagesize);
1288 emit_global (acfg, symbol, TRUE);
1289 emit_label (acfg, symbol);
1291 /* The common code */
1292 arm64_emit_tramp_page_common_code (acfg, pagesize, MONO_ARCH_RGCTX_REG, &common_tramp_size);
1293 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_STATIC_RGCTX] = common_tramp_size;
1295 arm64_emit_tramp_page_specific_code (acfg, pagesize, common_tramp_size, specific_tramp_size);
1297 /* The gsharedvt arg trampolines */
1298 /* These are the same as the specific trampolines */
1299 sprintf (symbol, "%sgsharedvt_arg_trampolines_page", acfg->user_symbol_prefix);
1300 emit_alignment (acfg, pagesize);
1301 emit_global (acfg, symbol, TRUE);
1302 emit_label (acfg, symbol);
1304 arm64_emit_tramp_page_common_code (acfg, pagesize, ARMREG_IP1, &common_tramp_size);
1305 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_GSHAREDVT_ARG] = common_tramp_size;
1307 arm64_emit_tramp_page_specific_code (acfg, pagesize, common_tramp_size, specific_tramp_size);
1309 /* The IMT trampolines */
1310 sprintf (symbol, "%simt_trampolines_page", acfg->user_symbol_prefix);
1311 emit_alignment (acfg, pagesize);
1312 emit_global (acfg, symbol, TRUE);
1313 emit_label (acfg, symbol);
1315 code = buf;
1316 imm = pagesize;
1317 /* The trampoline address is in IP0 */
1318 arm_movzx (code, ARMREG_IP1, imm & 0xffff, 0);
1319 arm_movkx (code, ARMREG_IP1, (imm >> 16) & 0xffff, 16);
1320 /* Compute the data slot address */
1321 arm_subx (code, ARMREG_IP0, ARMREG_IP0, ARMREG_IP1);
1322 /* Trampoline argument */
1323 arm_ldrx (code, ARMREG_IP1, ARMREG_IP0, 0);
1325 /* Same as arch_emit_imt_trampoline () */
1326 labels [0] = code;
1327 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 0);
1328 arm_cmpx (code, ARMREG_IP0, MONO_ARCH_RGCTX_REG);
1329 labels [1] = code;
1330 arm_bcc (code, ARMCOND_EQ, 0);
1332 /* End-of-loop check */
1333 labels [2] = code;
1334 arm_cbzx (code, ARMREG_IP0, 0);
1336 /* Loop footer */
1337 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 2 * 8);
1338 arm_b (code, labels [0]);
1340 /* Match */
1341 mono_arm_patch (labels [1], code, MONO_R_ARM64_BCC);
1342 /* Load vtable slot addr */
1343 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 8);
1344 /* Load vtable slot */
1345 arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, 0);
1346 arm_brx (code, ARMREG_IP0);
1348 /* No match */
1349 mono_arm_patch (labels [2], code, MONO_R_ARM64_CBZ);
1350 /* Load fail addr */
1351 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 8);
1352 arm_brx (code, ARMREG_IP0);
1354 emit_code_bytes (acfg, buf, code - buf);
1356 common_tramp_size = code - buf;
1357 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_IMT] = common_tramp_size;
1359 arm64_emit_tramp_page_specific_code (acfg, pagesize, common_tramp_size, specific_tramp_size);
1362 static void
1363 arm64_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1365 /* Load argument from second GOT slot */
1366 arm64_emit_load_got_slot (acfg, ARMREG_R17, offset + 1);
1367 /* Load generic trampoline address from first GOT slot */
1368 arm64_emit_load_got_slot (acfg, ARMREG_R16, offset);
1369 fprintf (acfg->fp, "br x16\n");
1370 *tramp_size = 7 * 4;
1373 static void
1374 arm64_emit_unbox_trampoline (MonoAotCompile *acfg, MonoCompile *cfg, MonoMethod *method, const char *call_target)
1376 emit_unset_mode (acfg);
1377 fprintf (acfg->fp, "add x0, x0, %d\n", (int)(sizeof (MonoObject)));
1378 fprintf (acfg->fp, "b %s\n", call_target);
1381 static void
1382 arm64_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1384 /* Similar to the specific trampolines, but use the rgctx reg instead of ip1 */
1386 /* Load argument from first GOT slot */
1387 arm64_emit_load_got_slot (acfg, MONO_ARCH_RGCTX_REG, offset);
1388 /* Load generic trampoline address from second GOT slot */
1389 arm64_emit_load_got_slot (acfg, ARMREG_R16, offset + 1);
1390 fprintf (acfg->fp, "br x16\n");
1391 *tramp_size = 7 * 4;
1394 static void
1395 arm64_emit_imt_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1397 guint8 buf [128];
1398 guint8 *code, *labels [16];
1400 /* Load parameter from GOT slot into ip1 */
1401 arm64_emit_load_got_slot (acfg, ARMREG_R17, offset);
1403 code = buf;
1404 labels [0] = code;
1405 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 0);
1406 arm_cmpx (code, ARMREG_IP0, MONO_ARCH_RGCTX_REG);
1407 labels [1] = code;
1408 arm_bcc (code, ARMCOND_EQ, 0);
1410 /* End-of-loop check */
1411 labels [2] = code;
1412 arm_cbzx (code, ARMREG_IP0, 0);
1414 /* Loop footer */
1415 arm_addx_imm (code, ARMREG_IP1, ARMREG_IP1, 2 * 8);
1416 arm_b (code, labels [0]);
1418 /* Match */
1419 mono_arm_patch (labels [1], code, MONO_R_ARM64_BCC);
1420 /* Load vtable slot addr */
1421 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 8);
1422 /* Load vtable slot */
1423 arm_ldrx (code, ARMREG_IP0, ARMREG_IP0, 0);
1424 arm_brx (code, ARMREG_IP0);
1426 /* No match */
1427 mono_arm_patch (labels [2], code, MONO_R_ARM64_CBZ);
1428 /* Load fail addr */
1429 arm_ldrx (code, ARMREG_IP0, ARMREG_IP1, 8);
1430 arm_brx (code, ARMREG_IP0);
1432 emit_code_bytes (acfg, buf, code - buf);
1434 *tramp_size = code - buf + (3 * 4);
1437 static void
1438 arm64_emit_gsharedvt_arg_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
1440 /* Similar to the specific trampolines, but the address is in the second slot */
1441 /* Load argument from first GOT slot */
1442 arm64_emit_load_got_slot (acfg, ARMREG_R17, offset);
1443 /* Load generic trampoline address from second GOT slot */
1444 arm64_emit_load_got_slot (acfg, ARMREG_R16, offset + 1);
1445 fprintf (acfg->fp, "br x16\n");
1446 *tramp_size = 7 * 4;
1450 #endif
1452 #ifdef MONO_ARCH_AOT_SUPPORTED
1454 * arch_emit_direct_call:
1456 * Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
1457 * calling code.
1459 static void
1460 arch_emit_direct_call (MonoAotCompile *acfg, const char *target, gboolean external, gboolean thumb, MonoJumpInfo *ji, int *call_size)
1462 #if defined(TARGET_X86) || defined(TARGET_AMD64)
1463 /* Need to make sure this is exactly 5 bytes long */
1464 emit_unset_mode (acfg);
1465 fprintf (acfg->fp, "call %s\n", target);
1466 *call_size = 5;
1467 #elif defined(TARGET_ARM)
1468 emit_unset_mode (acfg);
1469 if (thumb)
1470 fprintf (acfg->fp, "blx %s\n", target);
1471 else
1472 fprintf (acfg->fp, "bl %s\n", target);
1473 *call_size = 4;
1474 #elif defined(TARGET_ARM64)
1475 arm64_emit_direct_call (acfg, target, external, thumb, ji, call_size);
1476 #elif defined(TARGET_POWERPC)
1477 emit_unset_mode (acfg);
1478 fprintf (acfg->fp, "bl %s\n", target);
1479 *call_size = 4;
1480 #else
1481 g_assert_not_reached ();
1482 #endif
1484 #endif
1487 * PPC32 design:
1488 * - we use an approach similar to the x86 abi: reserve a register (r30) to hold
1489 * the GOT pointer.
1490 * - The full-aot trampolines need access to the GOT of mscorlib, so we store
1491 * in in the 2. slot of every GOT, and require every method to place the GOT
1492 * address in r30, even when it doesn't access the GOT otherwise. This way,
1493 * the trampolines can compute the mscorlib GOT address by loading 4(r30).
1497 * PPC64 design:
1498 * PPC64 uses function descriptors which greatly complicate all code, since
1499 * these are used very inconsistently in the runtime. Some functions like
1500 * mono_compile_method () return ftn descriptors, while others like the
1501 * trampoline creation functions do not.
1502 * We assume that all GOT slots contain function descriptors, and create
1503 * descriptors in aot-runtime.c when needed.
1504 * The ppc64 abi uses r2 to hold the address of the TOC/GOT, which is loaded
1505 * from function descriptors, we could do the same, but it would require
1506 * rewriting all the ppc/aot code to handle function descriptors properly.
1507 * So instead, we use the same approach as on PPC32.
1508 * This is a horrible mess, but fixing it would probably lead to an even bigger
1509 * one.
1513 * X86 design:
1514 * - similar to the PPC32 design, we reserve EBX to hold the GOT pointer.
1517 #ifdef MONO_ARCH_AOT_SUPPORTED
1519 * arch_emit_got_offset:
1521 * The memory pointed to by CODE should hold native code for computing the GOT
1522 * address (OP_LOAD_GOTADDR). Emit this code while patching it with the offset
1523 * between code and the GOT. CODE_SIZE is set to the number of bytes emitted.
1525 static void
1526 arch_emit_got_offset (MonoAotCompile *acfg, guint8 *code, int *code_size)
1528 #if defined(TARGET_POWERPC64)
1529 emit_unset_mode (acfg);
1531 * The ppc32 code doesn't seem to work on ppc64, the assembler complains about
1532 * unsupported relocations. So we store the got address into the .Lgot_addr
1533 * symbol which is in the text segment, compute its address, and load it.
1535 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
1536 fprintf (acfg->fp, "lis 0, (.Lgot_addr + 4 - .L%d)@h\n", acfg->label_generator);
1537 fprintf (acfg->fp, "ori 0, 0, (.Lgot_addr + 4 - .L%d)@l\n", acfg->label_generator);
1538 fprintf (acfg->fp, "add 30, 30, 0\n");
1539 fprintf (acfg->fp, "%s 30, 0(30)\n", PPC_LD_OP);
1540 acfg->label_generator ++;
1541 *code_size = 16;
1542 #elif defined(TARGET_POWERPC)
1543 emit_unset_mode (acfg);
1544 fprintf (acfg->fp, ".L%d:\n", acfg->label_generator);
1545 fprintf (acfg->fp, "lis 0, (%s + 4 - .L%d)@h\n", acfg->got_symbol, acfg->label_generator);
1546 fprintf (acfg->fp, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg->got_symbol, acfg->label_generator);
1547 acfg->label_generator ++;
1548 *code_size = 8;
1549 #else
1550 guint32 offset = mono_arch_get_patch_offset (code);
1551 emit_bytes (acfg, code, offset);
1552 emit_symbol_diff (acfg, acfg->got_symbol, ".", offset);
1554 *code_size = offset + 4;
1555 #endif
1559 * arch_emit_got_access:
1561 * The memory pointed to by CODE should hold native code for loading a GOT
1562 * slot (OP_AOTCONST/OP_GOT_ENTRY). Emit this code while patching it so it accesses the
1563 * GOT slot GOT_SLOT. CODE_SIZE is set to the number of bytes emitted.
1565 static void
1566 arch_emit_got_access (MonoAotCompile *acfg, const char *got_symbol, guint8 *code, int got_slot, int *code_size)
1568 #ifdef TARGET_AMD64
1569 /* mov reg, got+offset(%rip) */
1570 if (acfg->llvm) {
1571 /* The GOT symbol is in the LLVM module, the clang assembler has problems emitting symbol diffs for it */
1572 int dreg;
1573 int rex_r;
1575 /* Decode reg, see amd64_mov_reg_membase () */
1576 rex_r = code [0] & AMD64_REX_R;
1577 g_assert (code [0] == 0x49 + rex_r);
1578 g_assert (code [1] == 0x8b);
1579 dreg = ((code [2] >> 3) & 0x7) + (rex_r ? 8 : 0);
1581 emit_unset_mode (acfg);
1582 fprintf (acfg->fp, "mov %s+%d(%%rip), %s\n", got_symbol, (unsigned int) ((got_slot * sizeof (gpointer))), mono_arch_regname (dreg));
1583 *code_size = 7;
1584 } else {
1585 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1586 emit_symbol_diff (acfg, got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer)) - 4));
1587 *code_size = mono_arch_get_patch_offset (code) + 4;
1589 #elif defined(TARGET_X86)
1590 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1591 emit_int32 (acfg, (unsigned int) ((got_slot * sizeof (gpointer))));
1592 *code_size = mono_arch_get_patch_offset (code) + 4;
1593 #elif defined(TARGET_ARM)
1594 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1595 emit_symbol_diff (acfg, got_symbol, ".", (unsigned int) ((got_slot * sizeof (gpointer))) - 12);
1596 *code_size = mono_arch_get_patch_offset (code) + 4;
1597 #elif defined(TARGET_ARM64)
1598 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1599 arm64_emit_got_access (acfg, code, got_slot, code_size);
1600 #elif defined(TARGET_POWERPC)
1602 guint8 buf [32];
1604 emit_bytes (acfg, code, mono_arch_get_patch_offset (code));
1605 code = buf;
1606 ppc_load32 (code, ppc_r0, got_slot * sizeof (gpointer));
1607 g_assert (code - buf == 8);
1608 emit_bytes (acfg, buf, code - buf);
1609 *code_size = code - buf;
1611 #else
1612 g_assert_not_reached ();
1613 #endif
1616 #endif
1618 #ifdef MONO_ARCH_AOT_SUPPORTED
1620 * arch_emit_objc_selector_ref:
1622 * Emit the implementation of OP_OBJC_GET_SELECTOR, which itself implements @selector(foo:) in objective-c.
1624 static void
1625 arch_emit_objc_selector_ref (MonoAotCompile *acfg, guint8 *code, int index, int *code_size)
1627 #if defined(TARGET_ARM)
1628 char symbol1 [MAX_SYMBOL_SIZE];
1629 char symbol2 [MAX_SYMBOL_SIZE];
1630 int lindex = acfg->objc_selector_index_2 ++;
1632 /* Emit ldr.imm/b */
1633 emit_bytes (acfg, code, 8);
1635 sprintf (symbol1, "L_OBJC_SELECTOR_%d", lindex);
1636 sprintf (symbol2, "L_OBJC_SELECTOR_REFERENCES_%d", index);
1638 emit_label (acfg, symbol1);
1639 mono_img_writer_emit_unset_mode (acfg->w);
1640 fprintf (acfg->fp, ".long %s-(%s+12)", symbol2, symbol1);
1642 *code_size = 12;
1643 #elif defined(TARGET_ARM64)
1644 arm64_emit_objc_selector_ref (acfg, code, index, code_size);
1645 #else
1646 g_assert_not_reached ();
1647 #endif
1649 #endif
1652 * arch_emit_plt_entry:
1654 * Emit code for the PLT entry.
1655 * The plt entry should look like this:
1656 * <indirect jump to GOT_SYMBOL + OFFSET>
1657 * <INFO_OFFSET embedded into the instruction stream>
1659 static void
1660 arch_emit_plt_entry (MonoAotCompile *acfg, const char *got_symbol, int offset, int info_offset)
1662 #if defined(TARGET_X86)
1663 /* jmp *<offset>(%ebx) */
1664 emit_byte (acfg, 0xff);
1665 emit_byte (acfg, 0xa3);
1666 emit_int32 (acfg, offset);
1667 /* Used by mono_aot_get_plt_info_offset */
1668 emit_int32 (acfg, info_offset);
1669 #elif defined(TARGET_AMD64)
1670 emit_unset_mode (acfg);
1671 fprintf (acfg->fp, "jmp *%s+%d(%%rip)\n", got_symbol, offset);
1672 /* Used by mono_aot_get_plt_info_offset */
1673 emit_int32 (acfg, info_offset);
1674 acfg->stats.plt_size += 10;
1675 #elif defined(TARGET_ARM)
1676 guint8 buf [256];
1677 guint8 *code;
1679 code = buf;
1680 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 0);
1681 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
1682 emit_bytes (acfg, buf, code - buf);
1683 emit_symbol_diff (acfg, got_symbol, ".", offset - 4);
1684 /* Used by mono_aot_get_plt_info_offset */
1685 emit_int32 (acfg, info_offset);
1686 #elif defined(TARGET_ARM64)
1687 arm64_emit_plt_entry (acfg, got_symbol, offset, info_offset);
1688 #elif defined(TARGET_POWERPC)
1689 /* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
1690 emit_unset_mode (acfg);
1691 fprintf (acfg->fp, "lis 11, %d@h\n", offset);
1692 fprintf (acfg->fp, "ori 11, 11, %d@l\n", offset);
1693 fprintf (acfg->fp, "add 11, 11, 30\n");
1694 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1695 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1696 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
1697 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
1698 #endif
1699 fprintf (acfg->fp, "mtctr 11\n");
1700 fprintf (acfg->fp, "bctr\n");
1701 emit_int32 (acfg, info_offset);
1702 #else
1703 g_assert_not_reached ();
1704 #endif
1708 * arch_emit_llvm_plt_entry:
1710 * Same as arch_emit_plt_entry, but handles calls from LLVM generated code.
1711 * This is only needed on arm to handle thumb interop.
1713 static void
1714 arch_emit_llvm_plt_entry (MonoAotCompile *acfg, const char *got_symbol, int offset, int info_offset)
1716 #if defined(TARGET_ARM)
1717 /* LLVM calls the PLT entries using bl, so these have to be thumb2 */
1718 /* The caller already transitioned to thumb */
1719 /* The code below should be 12 bytes long */
1720 /* clang has trouble encoding these instructions, so emit the binary */
1721 #if 0
1722 fprintf (acfg->fp, "ldr ip, [pc, #8]\n");
1723 /* thumb can't encode ld pc, [pc, ip] */
1724 fprintf (acfg->fp, "add ip, pc, ip\n");
1725 fprintf (acfg->fp, "ldr ip, [ip, #0]\n");
1726 fprintf (acfg->fp, "bx ip\n");
1727 #endif
1728 emit_set_thumb_mode (acfg);
1729 fprintf (acfg->fp, ".4byte 0xc008f8df\n");
1730 fprintf (acfg->fp, ".2byte 0x44fc\n");
1731 fprintf (acfg->fp, ".4byte 0xc000f8dc\n");
1732 fprintf (acfg->fp, ".2byte 0x4760\n");
1733 emit_symbol_diff (acfg, got_symbol, ".", offset + 4);
1734 emit_int32 (acfg, info_offset);
1735 emit_unset_mode (acfg);
1736 emit_set_arm_mode (acfg);
1737 #else
1738 g_assert_not_reached ();
1739 #endif
1742 /* Save unwind_info in the module and emit the offset to the information at symbol */
1743 static void save_unwind_info (MonoAotCompile *acfg, char *symbol, GSList *unwind_ops)
1745 guint32 uw_offset, encoded_len;
1746 guint8 *encoded;
1748 emit_section_change (acfg, RODATA_SECT, 0);
1749 emit_global (acfg, symbol, FALSE);
1750 emit_label (acfg, symbol);
1752 encoded = mono_unwind_ops_encode (unwind_ops, &encoded_len);
1753 uw_offset = get_unwind_info_offset (acfg, encoded, encoded_len);
1754 g_free (encoded);
1755 emit_int32 (acfg, uw_offset);
1759 * arch_emit_specific_trampoline_pages:
1761 * Emits a page full of trampolines: each trampoline uses its own address to
1762 * lookup both the generic trampoline code and the data argument.
1763 * This page can be remapped in process multiple times so we can get an
1764 * unlimited number of trampolines.
1765 * Specifically this implementation uses the following trick: two memory pages
1766 * are allocated, with the first containing the data and the second containing the trampolines.
1767 * To reduce trampoline size, each trampoline jumps at the start of the page where a common
1768 * implementation does all the lifting.
1769 * Note that the ARM single trampoline size is 8 bytes, exactly like the data that needs to be stored
1770 * on the arm 32 bit system.
1772 static void
1773 arch_emit_specific_trampoline_pages (MonoAotCompile *acfg)
1775 #if defined(TARGET_ARM)
1776 guint8 buf [128];
1777 guint8 *code;
1778 guint8 *loop_start, *loop_branch_back, *loop_end_check, *imt_found_check;
1779 int i;
1780 int pagesize = MONO_AOT_TRAMP_PAGE_SIZE;
1781 GSList *unwind_ops = NULL;
1782 #define COMMON_TRAMP_SIZE 16
1783 int count = (pagesize - COMMON_TRAMP_SIZE) / 8;
1784 int imm8, rot_amount;
1785 char symbol [128];
1787 if (!acfg->aot_opts.use_trampolines_page)
1788 return;
1790 acfg->tramp_page_size = pagesize;
1792 sprintf (symbol, "%sspecific_trampolines_page", acfg->user_symbol_prefix);
1793 emit_alignment (acfg, pagesize);
1794 emit_global (acfg, symbol, TRUE);
1795 emit_label (acfg, symbol);
1797 /* emit the generic code first, the trampoline address + 8 is in the lr register */
1798 code = buf;
1799 imm8 = mono_arm_is_rotated_imm8 (pagesize, &rot_amount);
1800 ARM_SUB_REG_IMM (code, ARMREG_LR, ARMREG_LR, imm8, rot_amount);
1801 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_LR, -8);
1802 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_LR, -4);
1803 ARM_NOP (code);
1804 g_assert (code - buf == COMMON_TRAMP_SIZE);
1806 /* Emit it */
1807 emit_bytes (acfg, buf, code - buf);
1809 for (i = 0; i < count; ++i) {
1810 code = buf;
1811 ARM_PUSH (code, 0x5fff);
1812 ARM_BL (code, 0);
1813 arm_patch (code - 4, code - COMMON_TRAMP_SIZE - 8 * (i + 1));
1814 g_assert (code - buf == 8);
1815 emit_bytes (acfg, buf, code - buf);
1818 /* now the rgctx trampolines: each specific trampolines puts in the ip register
1819 * the instruction pointer address, so the generic trampoline at the start of the page
1820 * subtracts 4096 to get to the data page and loads the values
1821 * We again fit the generic trampiline in 16 bytes.
1823 sprintf (symbol, "%srgctx_trampolines_page", acfg->user_symbol_prefix);
1824 emit_global (acfg, symbol, TRUE);
1825 emit_label (acfg, symbol);
1826 code = buf;
1827 imm8 = mono_arm_is_rotated_imm8 (pagesize, &rot_amount);
1828 ARM_SUB_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
1829 ARM_LDR_IMM (code, MONO_ARCH_RGCTX_REG, ARMREG_IP, -8);
1830 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, -4);
1831 ARM_NOP (code);
1832 g_assert (code - buf == COMMON_TRAMP_SIZE);
1834 /* Emit it */
1835 emit_bytes (acfg, buf, code - buf);
1837 for (i = 0; i < count; ++i) {
1838 code = buf;
1839 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
1840 ARM_B (code, 0);
1841 arm_patch (code - 4, code - COMMON_TRAMP_SIZE - 8 * (i + 1));
1842 g_assert (code - buf == 8);
1843 emit_bytes (acfg, buf, code - buf);
1847 * gsharedvt arg trampolines: see arch_emit_gsharedvt_arg_trampoline ()
1849 sprintf (symbol, "%sgsharedvt_arg_trampolines_page", acfg->user_symbol_prefix);
1850 emit_global (acfg, symbol, TRUE);
1851 emit_label (acfg, symbol);
1852 code = buf;
1853 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3));
1854 imm8 = mono_arm_is_rotated_imm8 (pagesize, &rot_amount);
1855 ARM_SUB_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
1856 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_IP, -8);
1857 ARM_LDR_IMM (code, ARMREG_PC, ARMREG_IP, -4);
1858 g_assert (code - buf == COMMON_TRAMP_SIZE);
1859 /* Emit it */
1860 emit_bytes (acfg, buf, code - buf);
1862 for (i = 0; i < count; ++i) {
1863 code = buf;
1864 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
1865 ARM_B (code, 0);
1866 arm_patch (code - 4, code - COMMON_TRAMP_SIZE - 8 * (i + 1));
1867 g_assert (code - buf == 8);
1868 emit_bytes (acfg, buf, code - buf);
1871 /* now the imt trampolines: each specific trampolines puts in the ip register
1872 * the instruction pointer address, so the generic trampoline at the start of the page
1873 * subtracts 4096 to get to the data page and loads the values
1875 #define IMT_TRAMP_SIZE 72
1876 sprintf (symbol, "%simt_trampolines_page", acfg->user_symbol_prefix);
1877 emit_global (acfg, symbol, TRUE);
1878 emit_label (acfg, symbol);
1879 code = buf;
1880 /* Need at least two free registers, plus a slot for storing the pc */
1881 ARM_PUSH (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_R2));
1883 imm8 = mono_arm_is_rotated_imm8 (pagesize, &rot_amount);
1884 ARM_SUB_REG_IMM (code, ARMREG_IP, ARMREG_IP, imm8, rot_amount);
1885 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_IP, -8);
1887 /* The IMT method is in v5, r0 has the imt array address */
1889 loop_start = code;
1890 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, 0);
1891 ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
1892 imt_found_check = code;
1893 ARM_B_COND (code, ARMCOND_EQ, 0);
1895 /* End-of-loop check */
1896 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
1897 loop_end_check = code;
1898 ARM_B_COND (code, ARMCOND_EQ, 0);
1900 /* Loop footer */
1901 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (gpointer) * 2);
1902 loop_branch_back = code;
1903 ARM_B (code, 0);
1904 arm_patch (loop_branch_back, loop_start);
1906 /* Match */
1907 arm_patch (imt_found_check, code);
1908 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1909 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 0);
1910 /* Save it to the third stack slot */
1911 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1912 /* Restore the registers and branch */
1913 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1915 /* No match */
1916 arm_patch (loop_end_check, code);
1917 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
1918 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
1919 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
1920 ARM_NOP (code);
1922 /* Emit it */
1923 g_assert (code - buf == IMT_TRAMP_SIZE);
1924 emit_bytes (acfg, buf, code - buf);
1926 for (i = 0; i < count; ++i) {
1927 code = buf;
1928 ARM_MOV_REG_REG (code, ARMREG_IP, ARMREG_PC);
1929 ARM_B (code, 0);
1930 arm_patch (code - 4, code - IMT_TRAMP_SIZE - 8 * (i + 1));
1931 g_assert (code - buf == 8);
1932 emit_bytes (acfg, buf, code - buf);
1935 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_SPECIFIC] = 16;
1936 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_STATIC_RGCTX] = 16;
1937 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_IMT] = 72;
1938 acfg->tramp_page_code_offsets [MONO_AOT_TRAMP_GSHAREDVT_ARG] = 16;
1940 /* Unwind info for specifc trampolines */
1941 sprintf (symbol, "%sspecific_trampolines_page_gen_p", acfg->user_symbol_prefix);
1942 /* We unwind to the original caller, from the stack, since lr is clobbered */
1943 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 14 * sizeof (mgreg_t));
1944 mono_add_unwind_op_offset (unwind_ops, 0, 0, ARMREG_LR, -4);
1945 save_unwind_info (acfg, symbol, unwind_ops);
1946 mono_free_unwind_info (unwind_ops);
1948 sprintf (symbol, "%sspecific_trampolines_page_sp_p", acfg->user_symbol_prefix);
1949 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1950 mono_add_unwind_op_def_cfa_offset (unwind_ops, 4, 0, 14 * sizeof (mgreg_t));
1951 save_unwind_info (acfg, symbol, unwind_ops);
1952 mono_free_unwind_info (unwind_ops);
1954 /* Unwind info for rgctx trampolines */
1955 sprintf (symbol, "%srgctx_trampolines_page_gen_p", acfg->user_symbol_prefix);
1956 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1957 save_unwind_info (acfg, symbol, unwind_ops);
1959 sprintf (symbol, "%srgctx_trampolines_page_sp_p", acfg->user_symbol_prefix);
1960 save_unwind_info (acfg, symbol, unwind_ops);
1961 mono_free_unwind_info (unwind_ops);
1963 /* Unwind info for gsharedvt trampolines */
1964 sprintf (symbol, "%sgsharedvt_trampolines_page_gen_p", acfg->user_symbol_prefix);
1965 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1966 mono_add_unwind_op_def_cfa_offset (unwind_ops, 4, 0, 4 * sizeof (mgreg_t));
1967 save_unwind_info (acfg, symbol, unwind_ops);
1968 mono_free_unwind_info (unwind_ops);
1970 sprintf (symbol, "%sgsharedvt_trampolines_page_sp_p", acfg->user_symbol_prefix);
1971 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1972 save_unwind_info (acfg, symbol, unwind_ops);
1973 mono_free_unwind_info (unwind_ops);
1975 /* Unwind info for imt trampolines */
1976 sprintf (symbol, "%simt_trampolines_page_gen_p", acfg->user_symbol_prefix);
1977 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1978 mono_add_unwind_op_def_cfa_offset (unwind_ops, 4, 0, 3 * sizeof (mgreg_t));
1979 save_unwind_info (acfg, symbol, unwind_ops);
1980 mono_free_unwind_info (unwind_ops);
1982 sprintf (symbol, "%simt_trampolines_page_sp_p", acfg->user_symbol_prefix);
1983 mono_add_unwind_op_def_cfa (unwind_ops, 0, 0, ARMREG_SP, 0);
1984 save_unwind_info (acfg, symbol, unwind_ops);
1985 mono_free_unwind_info (unwind_ops);
1986 #elif defined(TARGET_ARM64)
1987 arm64_emit_specific_trampoline_pages (acfg);
1988 #endif
1992 * arch_emit_specific_trampoline:
1994 * Emit code for a specific trampoline. OFFSET is the offset of the first of
1995 * two GOT slots which contain the generic trampoline address and the trampoline
1996 * argument. TRAMP_SIZE is set to the size of the emitted trampoline.
1998 static void
1999 arch_emit_specific_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
2002 * The trampolines created here are variations of the specific
2003 * trampolines created in mono_arch_create_specific_trampoline (). The
2004 * differences are:
2005 * - the generic trampoline address is taken from a got slot.
2006 * - the offset of the got slot where the trampoline argument is stored
2007 * is embedded in the instruction stream, and the generic trampoline
2008 * can load the argument by loading the offset, adding it to the
2009 * address of the trampoline to get the address of the got slot, and
2010 * loading the argument from there.
2011 * - all the trampolines should be of the same length.
2013 #if defined(TARGET_AMD64)
2014 /* This should be exactly 8 bytes long */
2015 *tramp_size = 8;
2016 /* call *<offset>(%rip) */
2017 if (acfg->llvm) {
2018 emit_unset_mode (acfg);
2019 fprintf (acfg->fp, "call *%s+%d(%%rip)\n", acfg->got_symbol, (int)(offset * sizeof (gpointer)));
2020 emit_zero_bytes (acfg, 2);
2021 } else {
2022 emit_byte (acfg, '\x41');
2023 emit_byte (acfg, '\xff');
2024 emit_byte (acfg, '\x15');
2025 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
2026 emit_zero_bytes (acfg, 1);
2028 #elif defined(TARGET_ARM)
2029 guint8 buf [128];
2030 guint8 *code;
2032 /* This should be exactly 20 bytes long */
2033 *tramp_size = 20;
2034 code = buf;
2035 ARM_PUSH (code, 0x5fff);
2036 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 4);
2037 /* Load the value from the GOT */
2038 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
2039 /* Branch to it */
2040 ARM_BLX_REG (code, ARMREG_R1);
2042 g_assert (code - buf == 16);
2044 /* Emit it */
2045 emit_bytes (acfg, buf, code - buf);
2047 * Only one offset is needed, since the second one would be equal to the
2048 * first one.
2050 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 4);
2051 //emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 8);
2052 #elif defined(TARGET_ARM64)
2053 arm64_emit_specific_trampoline (acfg, offset, tramp_size);
2054 #elif defined(TARGET_POWERPC)
2055 guint8 buf [128];
2056 guint8 *code;
2058 *tramp_size = 4;
2059 code = buf;
2062 * PPC has no ip relative addressing, so we need to compute the address
2063 * of the mscorlib got. That is slow and complex, so instead, we store it
2064 * in the second got slot of every aot image. The caller already computed
2065 * the address of its got and placed it into r30.
2067 emit_unset_mode (acfg);
2068 /* Load mscorlib got address */
2069 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
2070 /* Load generic trampoline address */
2071 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
2072 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
2073 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
2074 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2075 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
2076 #endif
2077 fprintf (acfg->fp, "mtctr 11\n");
2078 /* Load trampoline argument */
2079 /* On ppc, we pass it normally to the generic trampoline */
2080 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
2081 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
2082 fprintf (acfg->fp, "%s 0, 11, 0\n", PPC_LDX_OP);
2083 /* Branch to generic trampoline */
2084 fprintf (acfg->fp, "bctr\n");
2086 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2087 *tramp_size = 10 * 4;
2088 #else
2089 *tramp_size = 9 * 4;
2090 #endif
2091 #elif defined(TARGET_X86)
2092 guint8 buf [128];
2093 guint8 *code;
2095 /* Similar to the PPC code above */
2097 /* FIXME: Could this clobber the register needed by get_vcall_slot () ? */
2099 code = buf;
2100 /* Load mscorlib got address */
2101 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
2102 /* Push trampoline argument */
2103 x86_push_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
2104 /* Load generic trampoline address */
2105 x86_mov_reg_membase (code, X86_ECX, X86_ECX, offset * sizeof (gpointer), 4);
2106 /* Branch to generic trampoline */
2107 x86_jump_reg (code, X86_ECX);
2109 emit_bytes (acfg, buf, code - buf);
2111 *tramp_size = 17;
2112 g_assert (code - buf == *tramp_size);
2113 #else
2114 g_assert_not_reached ();
2115 #endif
2119 * arch_emit_unbox_trampoline:
2121 * Emit code for the unbox trampoline for METHOD used in the full-aot case.
2122 * CALL_TARGET is the symbol pointing to the native code of METHOD.
2124 static void
2125 arch_emit_unbox_trampoline (MonoAotCompile *acfg, MonoCompile *cfg, MonoMethod *method, const char *call_target)
2127 #if defined(TARGET_AMD64)
2128 guint8 buf [32];
2129 guint8 *code;
2130 int this_reg;
2132 this_reg = mono_arch_get_this_arg_reg (NULL);
2133 code = buf;
2134 amd64_alu_reg_imm (code, X86_ADD, this_reg, sizeof (MonoObject));
2136 emit_bytes (acfg, buf, code - buf);
2137 /* jump <method> */
2138 if (acfg->llvm) {
2139 emit_unset_mode (acfg);
2140 fprintf (acfg->fp, "jmp %s\n", call_target);
2141 } else {
2142 emit_byte (acfg, '\xe9');
2143 emit_symbol_diff (acfg, call_target, ".", -4);
2145 #elif defined(TARGET_X86)
2146 guint8 buf [32];
2147 guint8 *code;
2148 int this_pos = 4;
2150 code = buf;
2152 x86_alu_membase_imm (code, X86_ADD, X86_ESP, this_pos, sizeof (MonoObject));
2154 emit_bytes (acfg, buf, code - buf);
2156 /* jump <method> */
2157 emit_byte (acfg, '\xe9');
2158 emit_symbol_diff (acfg, call_target, ".", -4);
2159 #elif defined(TARGET_ARM)
2160 guint8 buf [128];
2161 guint8 *code;
2163 if (acfg->thumb_mixed && cfg->compile_llvm) {
2164 fprintf (acfg->fp, "add r0, r0, #%d\n", (int)sizeof (MonoObject));
2165 fprintf (acfg->fp, "b %s\n", call_target);
2166 fprintf (acfg->fp, ".arm\n");
2167 fprintf (acfg->fp, ".align 2\n");
2168 return;
2171 code = buf;
2173 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (MonoObject));
2175 emit_bytes (acfg, buf, code - buf);
2176 /* jump to method */
2177 if (acfg->thumb_mixed && cfg->compile_llvm)
2178 fprintf (acfg->fp, "\n\tbx %s\n", call_target);
2179 else
2180 fprintf (acfg->fp, "\n\tb %s\n", call_target);
2181 #elif defined(TARGET_ARM64)
2182 arm64_emit_unbox_trampoline (acfg, cfg, method, call_target);
2183 #elif defined(TARGET_POWERPC)
2184 int this_pos = 3;
2186 fprintf (acfg->fp, "\n\taddi %d, %d, %d\n", this_pos, this_pos, (int)sizeof (MonoObject));
2187 fprintf (acfg->fp, "\n\tb %s\n", call_target);
2188 #else
2189 g_assert_not_reached ();
2190 #endif
2194 * arch_emit_static_rgctx_trampoline:
2196 * Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
2197 * two GOT slots which contain the rgctx argument, and the method to jump to.
2198 * TRAMP_SIZE is set to the size of the emitted trampoline.
2199 * These kinds of trampolines cannot be enumerated statically, since there could
2200 * be one trampoline per method instantiation, so we emit the same code for all
2201 * trampolines, and parameterize them using two GOT slots.
2203 static void
2204 arch_emit_static_rgctx_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
2206 #if defined(TARGET_AMD64)
2207 /* This should be exactly 13 bytes long */
2208 *tramp_size = 13;
2210 if (acfg->llvm) {
2211 emit_unset_mode (acfg);
2212 fprintf (acfg->fp, "mov %s+%d(%%rip), %%r10\n", acfg->got_symbol, (int)(offset * sizeof (gpointer)));
2213 fprintf (acfg->fp, "jmp *%s+%d(%%rip)\n", acfg->got_symbol, (int)((offset + 1) * sizeof (gpointer)));
2214 } else {
2215 /* mov <OFFSET>(%rip), %r10 */
2216 emit_byte (acfg, '\x4d');
2217 emit_byte (acfg, '\x8b');
2218 emit_byte (acfg, '\x15');
2219 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
2221 /* jmp *<offset>(%rip) */
2222 emit_byte (acfg, '\xff');
2223 emit_byte (acfg, '\x25');
2224 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4);
2226 #elif defined(TARGET_ARM)
2227 guint8 buf [128];
2228 guint8 *code;
2230 /* This should be exactly 24 bytes long */
2231 *tramp_size = 24;
2232 code = buf;
2233 /* Load rgctx value */
2234 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 8);
2235 ARM_LDR_REG_REG (code, MONO_ARCH_RGCTX_REG, ARMREG_PC, ARMREG_IP);
2236 /* Load branch addr + branch */
2237 ARM_LDR_IMM (code, ARMREG_IP, ARMREG_PC, 4);
2238 ARM_LDR_REG_REG (code, ARMREG_PC, ARMREG_PC, ARMREG_IP);
2240 g_assert (code - buf == 16);
2242 /* Emit it */
2243 emit_bytes (acfg, buf, code - buf);
2244 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4 + 8);
2245 emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 4);
2246 #elif defined(TARGET_ARM64)
2247 arm64_emit_static_rgctx_trampoline (acfg, offset, tramp_size);
2248 #elif defined(TARGET_POWERPC)
2249 guint8 buf [128];
2250 guint8 *code;
2252 *tramp_size = 4;
2253 code = buf;
2256 * PPC has no ip relative addressing, so we need to compute the address
2257 * of the mscorlib got. That is slow and complex, so instead, we store it
2258 * in the second got slot of every aot image. The caller already computed
2259 * the address of its got and placed it into r30.
2261 emit_unset_mode (acfg);
2262 /* Load mscorlib got address */
2263 fprintf (acfg->fp, "%s 0, %d(30)\n", PPC_LD_OP, (int)sizeof (gpointer));
2264 /* Load rgctx */
2265 fprintf (acfg->fp, "lis 11, %d@h\n", (int)(offset * sizeof (gpointer)));
2266 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)(offset * sizeof (gpointer)));
2267 fprintf (acfg->fp, "%s %d, 11, 0\n", PPC_LDX_OP, MONO_ARCH_RGCTX_REG);
2268 /* Load target address */
2269 fprintf (acfg->fp, "lis 11, %d@h\n", (int)((offset + 1) * sizeof (gpointer)));
2270 fprintf (acfg->fp, "ori 11, 11, %d@l\n", (int)((offset + 1) * sizeof (gpointer)));
2271 fprintf (acfg->fp, "%s 11, 11, 0\n", PPC_LDX_OP);
2272 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2273 fprintf (acfg->fp, "%s 2, %d(11)\n", PPC_LD_OP, (int)sizeof (gpointer));
2274 fprintf (acfg->fp, "%s 11, 0(11)\n", PPC_LD_OP);
2275 #endif
2276 fprintf (acfg->fp, "mtctr 11\n");
2277 /* Branch to the target address */
2278 fprintf (acfg->fp, "bctr\n");
2280 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2281 *tramp_size = 11 * 4;
2282 #else
2283 *tramp_size = 9 * 4;
2284 #endif
2286 #elif defined(TARGET_X86)
2287 guint8 buf [128];
2288 guint8 *code;
2290 /* Similar to the PPC code above */
2292 g_assert (MONO_ARCH_RGCTX_REG != X86_ECX);
2294 code = buf;
2295 /* Load mscorlib got address */
2296 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
2297 /* Load arg */
2298 x86_mov_reg_membase (code, MONO_ARCH_RGCTX_REG, X86_ECX, offset * sizeof (gpointer), 4);
2299 /* Branch to the target address */
2300 x86_jump_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
2302 emit_bytes (acfg, buf, code - buf);
2304 *tramp_size = 15;
2305 g_assert (code - buf == *tramp_size);
2306 #else
2307 g_assert_not_reached ();
2308 #endif
2312 * arch_emit_imt_trampoline:
2314 * Emit an IMT trampoline usable in full-aot mode. The trampoline uses 1 got slot which
2315 * points to an array of pointer pairs. The pairs of the form [key, ptr], where
2316 * key is the IMT key, and ptr holds the address of a memory location holding
2317 * the address to branch to if the IMT arg matches the key. The array is
2318 * terminated by a pair whose key is NULL, and whose ptr is the address of the
2319 * fail_tramp.
2320 * TRAMP_SIZE is set to the size of the emitted trampoline.
2322 static void
2323 arch_emit_imt_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
2325 #if defined(TARGET_AMD64)
2326 guint8 *buf, *code;
2327 guint8 *labels [16];
2328 guint8 mov_buf[3];
2329 guint8 *mov_buf_ptr = mov_buf;
2331 const int kSizeOfMove = 7;
2333 code = buf = (guint8 *)g_malloc (256);
2335 /* FIXME: Optimize this, i.e. use binary search etc. */
2336 /* Maybe move the body into a separate function (slower, but much smaller) */
2338 /* MONO_ARCH_IMT_SCRATCH_REG is a free register */
2340 if (acfg->llvm) {
2341 emit_unset_mode (acfg);
2342 fprintf (acfg->fp, "mov %s+%d(%%rip), %s\n", acfg->got_symbol, (int)(offset * sizeof (gpointer)), mono_arch_regname (MONO_ARCH_IMT_SCRATCH_REG));
2345 labels [0] = code;
2346 amd64_alu_membase_imm (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, 0);
2347 labels [1] = code;
2348 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
2350 /* Check key */
2351 amd64_alu_membase_reg_size (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, MONO_ARCH_IMT_REG, sizeof (gpointer));
2352 labels [2] = code;
2353 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
2355 /* Loop footer */
2356 amd64_alu_reg_imm (code, X86_ADD, MONO_ARCH_IMT_SCRATCH_REG, 2 * sizeof (gpointer));
2357 amd64_jump_code (code, labels [0]);
2359 /* Match */
2360 mono_amd64_patch (labels [2], code);
2361 amd64_mov_reg_membase (code, MONO_ARCH_IMT_SCRATCH_REG, MONO_ARCH_IMT_SCRATCH_REG, sizeof (gpointer), sizeof (gpointer));
2362 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
2364 /* No match */
2365 mono_amd64_patch (labels [1], code);
2366 /* Load fail tramp */
2367 amd64_alu_reg_imm (code, X86_ADD, MONO_ARCH_IMT_SCRATCH_REG, sizeof (gpointer));
2368 /* Check if there is a fail tramp */
2369 amd64_alu_membase_imm (code, X86_CMP, MONO_ARCH_IMT_SCRATCH_REG, 0, 0);
2370 labels [3] = code;
2371 amd64_branch8 (code, X86_CC_Z, 0, FALSE);
2372 /* Jump to fail tramp */
2373 amd64_jump_membase (code, MONO_ARCH_IMT_SCRATCH_REG, 0);
2375 /* Fail */
2376 mono_amd64_patch (labels [3], code);
2377 x86_breakpoint (code);
2379 if (!acfg->llvm) {
2380 /* mov <OFFSET>(%rip), MONO_ARCH_IMT_SCRATCH_REG */
2381 amd64_emit_rex (mov_buf_ptr, sizeof(gpointer), MONO_ARCH_IMT_SCRATCH_REG, 0, AMD64_RIP);
2382 *(mov_buf_ptr)++ = (unsigned char)0x8b; /* mov opcode */
2383 x86_address_byte (mov_buf_ptr, 0, MONO_ARCH_IMT_SCRATCH_REG & 0x7, 5);
2384 emit_bytes (acfg, mov_buf, mov_buf_ptr - mov_buf);
2385 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) - 4);
2387 emit_bytes (acfg, buf, code - buf);
2389 *tramp_size = code - buf + kSizeOfMove;
2391 g_free (buf);
2393 #elif defined(TARGET_X86)
2394 guint8 *buf, *code;
2395 guint8 *labels [16];
2397 code = buf = g_malloc (256);
2399 /* Allocate a temporary stack slot */
2400 x86_push_reg (code, X86_EAX);
2401 /* Save EAX */
2402 x86_push_reg (code, X86_EAX);
2404 /* Load mscorlib got address */
2405 x86_mov_reg_membase (code, X86_EAX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
2406 /* Load arg */
2407 x86_mov_reg_membase (code, X86_EAX, X86_EAX, offset * sizeof (gpointer), 4);
2409 labels [0] = code;
2410 x86_alu_membase_imm (code, X86_CMP, X86_EAX, 0, 0);
2411 labels [1] = code;
2412 x86_branch8 (code, X86_CC_Z, FALSE, 0);
2414 /* Check key */
2415 x86_alu_membase_reg (code, X86_CMP, X86_EAX, 0, MONO_ARCH_IMT_REG);
2416 labels [2] = code;
2417 x86_branch8 (code, X86_CC_Z, FALSE, 0);
2419 /* Loop footer */
2420 x86_alu_reg_imm (code, X86_ADD, X86_EAX, 2 * sizeof (gpointer));
2421 x86_jump_code (code, labels [0]);
2423 /* Match */
2424 mono_x86_patch (labels [2], code);
2425 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer), 4);
2426 x86_mov_reg_membase (code, X86_EAX, X86_EAX, 0, 4);
2427 /* Save the target address to the temporary stack location */
2428 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
2429 /* Restore EAX */
2430 x86_pop_reg (code, X86_EAX);
2431 /* Jump to the target address */
2432 x86_ret (code);
2434 /* No match */
2435 mono_x86_patch (labels [1], code);
2436 /* Load fail tramp */
2437 x86_mov_reg_membase (code, X86_EAX, X86_EAX, sizeof (gpointer), 4);
2438 x86_alu_membase_imm (code, X86_CMP, X86_EAX, 0, 0);
2439 labels [3] = code;
2440 x86_branch8 (code, X86_CC_Z, FALSE, 0);
2441 /* Jump to fail tramp */
2442 x86_mov_membase_reg (code, X86_ESP, 4, X86_EAX, 4);
2443 x86_pop_reg (code, X86_EAX);
2444 x86_ret (code);
2446 /* Fail */
2447 mono_x86_patch (labels [3], code);
2448 x86_breakpoint (code);
2450 emit_bytes (acfg, buf, code - buf);
2452 *tramp_size = code - buf;
2454 g_free (buf);
2456 #elif defined(TARGET_ARM)
2457 guint8 buf [128];
2458 guint8 *code, *code2, *labels [16];
2460 code = buf;
2462 /* The IMT method is in v5 */
2464 /* Need at least two free registers, plus a slot for storing the pc */
2465 ARM_PUSH (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_R2));
2466 labels [0] = code;
2467 /* Load the parameter from the GOT */
2468 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_PC, 0);
2469 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R0);
2471 labels [1] = code;
2472 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_R0, 0);
2473 ARM_CMP_REG_REG (code, ARMREG_R1, ARMREG_V5);
2474 labels [2] = code;
2475 ARM_B_COND (code, ARMCOND_EQ, 0);
2477 /* End-of-loop check */
2478 ARM_CMP_REG_IMM (code, ARMREG_R1, 0, 0);
2479 labels [3] = code;
2480 ARM_B_COND (code, ARMCOND_EQ, 0);
2482 /* Loop footer */
2483 ARM_ADD_REG_IMM8 (code, ARMREG_R0, ARMREG_R0, sizeof (gpointer) * 2);
2484 labels [4] = code;
2485 ARM_B (code, 0);
2486 arm_patch (labels [4], labels [1]);
2488 /* Match */
2489 arm_patch (labels [2], code);
2490 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
2491 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 0);
2492 /* Save it to the third stack slot */
2493 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
2494 /* Restore the registers and branch */
2495 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
2497 /* No match */
2498 arm_patch (labels [3], code);
2499 ARM_LDR_IMM (code, ARMREG_R0, ARMREG_R0, 4);
2500 ARM_STR_IMM (code, ARMREG_R0, ARMREG_SP, 8);
2501 ARM_POP (code, (1 << ARMREG_R0)|(1 << ARMREG_R1)|(1 << ARMREG_PC));
2503 /* Fixup offset */
2504 code2 = labels [0];
2505 ARM_LDR_IMM (code2, ARMREG_R0, ARMREG_PC, (code - (labels [0] + 8)));
2507 emit_bytes (acfg, buf, code - buf);
2508 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + (code - (labels [0] + 8)) - 4);
2510 *tramp_size = code - buf + 4;
2511 #elif defined(TARGET_ARM64)
2512 arm64_emit_imt_trampoline (acfg, offset, tramp_size);
2513 #elif defined(TARGET_POWERPC)
2514 guint8 buf [128];
2515 guint8 *code, *labels [16];
2517 code = buf;
2519 /* Load the mscorlib got address */
2520 ppc_ldptr (code, ppc_r12, sizeof (gpointer), ppc_r30);
2521 /* Load the parameter from the GOT */
2522 ppc_load (code, ppc_r0, offset * sizeof (gpointer));
2523 ppc_ldptr_indexed (code, ppc_r12, ppc_r12, ppc_r0);
2525 /* Load and check key */
2526 labels [1] = code;
2527 ppc_ldptr (code, ppc_r0, 0, ppc_r12);
2528 ppc_cmp (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, MONO_ARCH_IMT_REG);
2529 labels [2] = code;
2530 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
2532 /* End-of-loop check */
2533 ppc_cmpi (code, 0, sizeof (gpointer) == 8 ? 1 : 0, ppc_r0, 0);
2534 labels [3] = code;
2535 ppc_bc (code, PPC_BR_TRUE, PPC_BR_EQ, 0);
2537 /* Loop footer */
2538 ppc_addi (code, ppc_r12, ppc_r12, 2 * sizeof (gpointer));
2539 labels [4] = code;
2540 ppc_b (code, 0);
2541 mono_ppc_patch (labels [4], labels [1]);
2543 /* Match */
2544 mono_ppc_patch (labels [2], code);
2545 ppc_ldptr (code, ppc_r12, sizeof (gpointer), ppc_r12);
2546 /* r12 now contains the value of the vtable slot */
2547 /* this is not a function descriptor on ppc64 */
2548 ppc_ldptr (code, ppc_r12, 0, ppc_r12);
2549 ppc_mtctr (code, ppc_r12);
2550 ppc_bcctr (code, PPC_BR_ALWAYS, 0);
2552 /* Fail */
2553 mono_ppc_patch (labels [3], code);
2554 /* FIXME: */
2555 ppc_break (code);
2557 *tramp_size = code - buf;
2559 emit_bytes (acfg, buf, code - buf);
2560 #else
2561 g_assert_not_reached ();
2562 #endif
2566 #if defined (TARGET_AMD64)
2568 static void
2569 amd64_emit_load_got_slot (MonoAotCompile *acfg, int dreg, int got_slot)
2572 g_assert (acfg->fp);
2573 emit_unset_mode (acfg);
2575 fprintf (acfg->fp, "mov %s+%d(%%rip), %s\n", acfg->got_symbol, (unsigned int) ((got_slot * sizeof (gpointer))), mono_arch_regname (dreg));
2578 #endif
2582 * arch_emit_gsharedvt_arg_trampoline:
2584 * Emit code for a gsharedvt arg trampoline. OFFSET is the offset of the first of
2585 * two GOT slots which contain the argument, and the code to jump to.
2586 * TRAMP_SIZE is set to the size of the emitted trampoline.
2587 * These kinds of trampolines cannot be enumerated statically, since there could
2588 * be one trampoline per method instantiation, so we emit the same code for all
2589 * trampolines, and parameterize them using two GOT slots.
2591 static void
2592 arch_emit_gsharedvt_arg_trampoline (MonoAotCompile *acfg, int offset, int *tramp_size)
2594 #if defined(TARGET_X86)
2595 guint8 buf [128];
2596 guint8 *code;
2598 /* Similar to the PPC code above */
2600 g_assert (MONO_ARCH_RGCTX_REG != X86_ECX);
2602 code = buf;
2603 /* Load mscorlib got address */
2604 x86_mov_reg_membase (code, X86_ECX, MONO_ARCH_GOT_REG, sizeof (gpointer), 4);
2605 /* Load arg */
2606 x86_mov_reg_membase (code, X86_EAX, X86_ECX, offset * sizeof (gpointer), 4);
2607 /* Branch to the target address */
2608 x86_jump_membase (code, X86_ECX, (offset + 1) * sizeof (gpointer));
2610 emit_bytes (acfg, buf, code - buf);
2612 *tramp_size = 15;
2613 g_assert (code - buf == *tramp_size);
2614 #elif defined(TARGET_ARM)
2615 guint8 buf [128];
2616 guint8 *code;
2618 /* The same as mono_arch_get_gsharedvt_arg_trampoline (), but for AOT */
2619 /* Similar to arch_emit_specific_trampoline () */
2620 *tramp_size = 24;
2621 code = buf;
2622 ARM_PUSH (code, (1 << ARMREG_R0) | (1 << ARMREG_R1) | (1 << ARMREG_R2) | (1 << ARMREG_R3));
2623 ARM_LDR_IMM (code, ARMREG_R1, ARMREG_PC, 8);
2624 /* Load the arg value from the GOT */
2625 ARM_LDR_REG_REG (code, ARMREG_R0, ARMREG_PC, ARMREG_R1);
2626 /* Load the addr from the GOT */
2627 ARM_LDR_REG_REG (code, ARMREG_R1, ARMREG_PC, ARMREG_R1);
2628 /* Branch to it */
2629 ARM_BX (code, ARMREG_R1);
2631 g_assert (code - buf == 20);
2633 /* Emit it */
2634 emit_bytes (acfg, buf, code - buf);
2635 emit_symbol_diff (acfg, acfg->got_symbol, ".", (offset * sizeof (gpointer)) + 4);
2636 #elif defined(TARGET_ARM64)
2637 arm64_emit_gsharedvt_arg_trampoline (acfg, offset, tramp_size);
2638 #elif defined (TARGET_AMD64)
2640 amd64_emit_load_got_slot (acfg, AMD64_RAX, offset);
2641 amd64_emit_load_got_slot (acfg, MONO_ARCH_IMT_SCRATCH_REG, offset + 1);
2642 g_assert (AMD64_R11 == MONO_ARCH_IMT_SCRATCH_REG);
2643 fprintf (acfg->fp, "jmp *%%r11\n");
2645 *tramp_size = 0x11;
2646 #else
2647 g_assert_not_reached ();
2648 #endif
2651 /* END OF ARCH SPECIFIC CODE */
2653 static guint32
2654 mono_get_field_token (MonoClassField *field)
2656 MonoClass *klass = field->parent;
2657 int i;
2659 int fcount = mono_class_get_field_count (klass);
2660 MonoClassField *klass_fields = m_class_get_fields (klass);
2661 for (i = 0; i < fcount; ++i) {
2662 if (field == &klass_fields [i])
2663 return MONO_TOKEN_FIELD_DEF | (mono_class_get_first_field_idx (klass) + 1 + i);
2666 g_assert_not_reached ();
2667 return 0;
2670 static inline void
2671 encode_value (gint32 value, guint8 *buf, guint8 **endbuf)
2673 guint8 *p = buf;
2675 //printf ("ENCODE: %d 0x%x.\n", value, value);
2678 * Same encoding as the one used in the metadata, extended to handle values
2679 * greater than 0x1fffffff.
2681 if ((value >= 0) && (value <= 127))
2682 *p++ = value;
2683 else if ((value >= 0) && (value <= 16383)) {
2684 p [0] = 0x80 | (value >> 8);
2685 p [1] = value & 0xff;
2686 p += 2;
2687 } else if ((value >= 0) && (value <= 0x1fffffff)) {
2688 p [0] = (value >> 24) | 0xc0;
2689 p [1] = (value >> 16) & 0xff;
2690 p [2] = (value >> 8) & 0xff;
2691 p [3] = value & 0xff;
2692 p += 4;
2694 else {
2695 p [0] = 0xff;
2696 p [1] = (value >> 24) & 0xff;
2697 p [2] = (value >> 16) & 0xff;
2698 p [3] = (value >> 8) & 0xff;
2699 p [4] = value & 0xff;
2700 p += 5;
2702 if (endbuf)
2703 *endbuf = p;
2706 static void
2707 stream_init (MonoDynamicStream *sh)
2709 sh->index = 0;
2710 sh->alloc_size = 4096;
2711 sh->data = (char *)g_malloc (4096);
2713 /* So offsets are > 0 */
2714 sh->data [0] = 0;
2715 sh->index ++;
2718 static void
2719 make_room_in_stream (MonoDynamicStream *stream, int size)
2721 if (size <= stream->alloc_size)
2722 return;
2724 while (stream->alloc_size <= size) {
2725 if (stream->alloc_size < 4096)
2726 stream->alloc_size = 4096;
2727 else
2728 stream->alloc_size *= 2;
2731 stream->data = (char *)g_realloc (stream->data, stream->alloc_size);
2734 static guint32
2735 add_stream_data (MonoDynamicStream *stream, const char *data, guint32 len)
2737 guint32 idx;
2739 make_room_in_stream (stream, stream->index + len);
2740 memcpy (stream->data + stream->index, data, len);
2741 idx = stream->index;
2742 stream->index += len;
2743 return idx;
2747 * add_to_blob:
2749 * Add data to the binary blob inside the aot image. Returns the offset inside the
2750 * blob where the data was stored.
2752 static guint32
2753 add_to_blob (MonoAotCompile *acfg, const guint8 *data, guint32 data_len)
2755 g_assert (!acfg->blob_closed);
2757 if (acfg->blob.alloc_size == 0)
2758 stream_init (&acfg->blob);
2760 return add_stream_data (&acfg->blob, (char*)data, data_len);
2763 static guint32
2764 add_to_blob_aligned (MonoAotCompile *acfg, const guint8 *data, guint32 data_len, guint32 align)
2766 char buf [4] = {0};
2767 guint32 count;
2769 if (acfg->blob.alloc_size == 0)
2770 stream_init (&acfg->blob);
2772 count = acfg->blob.index % align;
2774 /* we assume the stream data will be aligned */
2775 if (count)
2776 add_stream_data (&acfg->blob, buf, 4 - count);
2778 return add_stream_data (&acfg->blob, (char*)data, data_len);
2781 /* Emit a table of data into the aot image */
2782 static void
2783 emit_aot_data (MonoAotCompile *acfg, MonoAotFileTable table, const char *symbol, guint8 *data, int size)
2785 if (acfg->data_outfile) {
2786 acfg->table_offsets [(int)table] = acfg->datafile_offset;
2787 fwrite (data,1, size, acfg->data_outfile);
2788 acfg->datafile_offset += size;
2789 // align the data to 8 bytes. Put zeros in the file (so that every build results in consistent output).
2790 int align = 8 - size % 8;
2791 acfg->datafile_offset += align;
2792 guint8 align_buf [16];
2793 memset (&align_buf, 0, sizeof (align_buf));
2794 fwrite (align_buf, align, 1, acfg->data_outfile);
2795 } else if (acfg->llvm) {
2796 mono_llvm_emit_aot_data (symbol, data, size);
2797 } else {
2798 emit_section_change (acfg, RODATA_SECT, 0);
2799 emit_alignment (acfg, 8);
2800 emit_label (acfg, symbol);
2801 emit_bytes (acfg, data, size);
2806 * emit_offset_table:
2808 * Emit a table of increasing offsets in a compact form using differential encoding.
2809 * There is an index entry for each GROUP_SIZE number of entries. The greater the
2810 * group size, the more compact the table becomes, but the slower it becomes to compute
2811 * a given entry. Returns the size of the table.
2813 static guint32
2814 emit_offset_table (MonoAotCompile *acfg, const char *symbol, MonoAotFileTable table, int noffsets, int group_size, gint32 *offsets)
2816 gint32 current_offset;
2817 int i, buf_size, ngroups, index_entry_size;
2818 guint8 *p, *buf;
2819 guint8 *data_p, *data_buf;
2820 guint32 *index_offsets;
2822 ngroups = (noffsets + (group_size - 1)) / group_size;
2824 index_offsets = g_new0 (guint32, ngroups);
2826 buf_size = noffsets * 4;
2827 p = buf = (guint8 *)g_malloc0 (buf_size);
2829 current_offset = 0;
2830 for (i = 0; i < noffsets; ++i) {
2831 //printf ("D: %d -> %d\n", i, offsets [i]);
2832 if ((i % group_size) == 0) {
2833 index_offsets [i / group_size] = p - buf;
2834 /* Emit the full value for these entries */
2835 encode_value (offsets [i], p, &p);
2836 } else {
2837 /* The offsets are allowed to be non-increasing */
2838 //g_assert (offsets [i] >= current_offset);
2839 encode_value (offsets [i] - current_offset, p, &p);
2841 current_offset = offsets [i];
2843 data_buf = buf;
2844 data_p = p;
2846 if (ngroups && index_offsets [ngroups - 1] < 65000)
2847 index_entry_size = 2;
2848 else
2849 index_entry_size = 4;
2851 buf_size = (data_p - data_buf) + (ngroups * 4) + 16;
2852 p = buf = (guint8 *)g_malloc0 (buf_size);
2854 /* Emit the header */
2855 encode_int (noffsets, p, &p);
2856 encode_int (group_size, p, &p);
2857 encode_int (ngroups, p, &p);
2858 encode_int (index_entry_size, p, &p);
2860 /* Emit the index */
2861 for (i = 0; i < ngroups; ++i) {
2862 if (index_entry_size == 2)
2863 encode_int16 (index_offsets [i], p, &p);
2864 else
2865 encode_int (index_offsets [i], p, &p);
2867 /* Emit the data */
2868 memcpy (p, data_buf, data_p - data_buf);
2869 p += data_p - data_buf;
2871 g_assert (p - buf <= buf_size);
2873 emit_aot_data (acfg, table, symbol, buf, p - buf);
2875 g_free (buf);
2876 g_free (data_buf);
2878 return (int)(p - buf);
2881 static guint32
2882 get_image_index (MonoAotCompile *cfg, MonoImage *image)
2884 guint32 index;
2886 index = GPOINTER_TO_UINT (g_hash_table_lookup (cfg->image_hash, image));
2887 if (index)
2888 return index - 1;
2889 else {
2890 index = g_hash_table_size (cfg->image_hash);
2891 g_hash_table_insert (cfg->image_hash, image, GUINT_TO_POINTER (index + 1));
2892 g_ptr_array_add (cfg->image_table, image);
2893 return index;
2897 static guint32
2898 find_typespec_for_class (MonoAotCompile *acfg, MonoClass *klass)
2900 int i;
2901 int len = acfg->image->tables [MONO_TABLE_TYPESPEC].rows;
2903 /* FIXME: Search referenced images as well */
2904 if (!acfg->typespec_classes) {
2905 acfg->typespec_classes = g_hash_table_new (NULL, NULL);
2906 for (i = 0; i < len; i++) {
2907 ERROR_DECL (error);
2908 int typespec = MONO_TOKEN_TYPE_SPEC | (i + 1);
2909 MonoClass *klass_key = mono_class_get_and_inflate_typespec_checked (acfg->image, typespec, NULL, error);
2910 if (!is_ok (error)) {
2911 mono_error_cleanup (error);
2912 continue;
2914 g_hash_table_insert (acfg->typespec_classes, klass_key, GINT_TO_POINTER (typespec));
2917 return GPOINTER_TO_INT (g_hash_table_lookup (acfg->typespec_classes, klass));
2920 static void
2921 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf);
2923 static void
2924 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf);
2926 static void
2927 encode_ginst (MonoAotCompile *acfg, MonoGenericInst *inst, guint8 *buf, guint8 **endbuf);
2929 static void
2930 encode_type (MonoAotCompile *acfg, MonoType *t, guint8 *buf, guint8 **endbuf);
2932 static void
2933 encode_klass_ref_inner (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
2935 guint8 *p = buf;
2938 * The encoding begins with one of the MONO_AOT_TYPEREF values, followed by additional
2939 * information.
2942 if (mono_class_is_ginst (klass)) {
2943 guint32 token;
2944 g_assert (m_class_get_type_token (klass));
2946 /* Find a typespec for a class if possible */
2947 token = find_typespec_for_class (acfg, klass);
2948 if (token) {
2949 encode_value (MONO_AOT_TYPEREF_TYPESPEC_TOKEN, p, &p);
2950 encode_value (token, p, &p);
2951 } else {
2952 MonoClass *gclass = mono_class_get_generic_class (klass)->container_class;
2953 MonoGenericInst *inst = mono_class_get_generic_class (klass)->context.class_inst;
2954 static int count = 0;
2955 guint8 *p1 = p;
2957 encode_value (MONO_AOT_TYPEREF_GINST, p, &p);
2958 encode_klass_ref (acfg, gclass, p, &p);
2959 encode_ginst (acfg, inst, p, &p);
2961 count += p - p1;
2963 } else if (m_class_get_type_token (klass)) {
2964 int iindex = get_image_index (acfg, m_class_get_image (klass));
2966 g_assert (mono_metadata_token_code (m_class_get_type_token (klass)) == MONO_TOKEN_TYPE_DEF);
2967 if (iindex == 0) {
2968 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX, p, &p);
2969 encode_value (m_class_get_type_token (klass) - MONO_TOKEN_TYPE_DEF, p, &p);
2970 } else {
2971 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE, p, &p);
2972 encode_value (m_class_get_type_token (klass) - MONO_TOKEN_TYPE_DEF, p, &p);
2973 encode_value (get_image_index (acfg, m_class_get_image (klass)), p, &p);
2975 } else if ((m_class_get_byval_arg (klass)->type == MONO_TYPE_VAR) || (m_class_get_byval_arg (klass)->type == MONO_TYPE_MVAR)) {
2976 MonoGenericContainer *container = mono_type_get_generic_param_owner (m_class_get_byval_arg (klass));
2977 MonoGenericParam *par = m_class_get_byval_arg (klass)->data.generic_param;
2979 encode_value (MONO_AOT_TYPEREF_VAR, p, &p);
2981 encode_value (par->gshared_constraint ? 1 : 0, p, &p);
2982 if (par->gshared_constraint) {
2983 MonoGSharedGenericParam *gpar = (MonoGSharedGenericParam*)par;
2984 encode_type (acfg, par->gshared_constraint, p, &p);
2985 encode_klass_ref (acfg, mono_class_create_generic_parameter (gpar->parent), p, &p);
2986 } else {
2987 encode_value (m_class_get_byval_arg (klass)->type, p, &p);
2988 encode_value (mono_type_get_generic_param_num (m_class_get_byval_arg (klass)), p, &p);
2990 encode_value (container->is_anonymous ? 0 : 1, p, &p);
2992 if (!container->is_anonymous) {
2993 encode_value (container->is_method, p, &p);
2994 if (container->is_method)
2995 encode_method_ref (acfg, container->owner.method, p, &p);
2996 else
2997 encode_klass_ref (acfg, container->owner.klass, p, &p);
3000 } else if (m_class_get_byval_arg (klass)->type == MONO_TYPE_PTR) {
3001 encode_value (MONO_AOT_TYPEREF_PTR, p, &p);
3002 encode_type (acfg, m_class_get_byval_arg (klass), p, &p);
3003 } else {
3004 /* Array class */
3005 g_assert (m_class_get_rank (klass) > 0);
3006 encode_value (MONO_AOT_TYPEREF_ARRAY, p, &p);
3007 encode_value (m_class_get_rank (klass), p, &p);
3008 encode_klass_ref (acfg, m_class_get_element_class (klass), p, &p);
3010 *endbuf = p;
3014 * encode_klass_ref:
3016 * Encode a reference to KLASS. We use our home-grown encoding instead of the
3017 * standard metadata encoding.
3019 static void
3020 encode_klass_ref (MonoAotCompile *acfg, MonoClass *klass, guint8 *buf, guint8 **endbuf)
3022 gboolean shared = FALSE;
3025 * The encoding of generic instances is large so emit them only once.
3027 if (mono_class_is_ginst (klass)) {
3028 guint32 token;
3029 g_assert (m_class_get_type_token (klass));
3031 /* Find a typespec for a class if possible */
3032 token = find_typespec_for_class (acfg, klass);
3033 if (!token)
3034 shared = TRUE;
3035 } else if ((m_class_get_byval_arg (klass)->type == MONO_TYPE_VAR) || (m_class_get_byval_arg (klass)->type == MONO_TYPE_MVAR)) {
3036 shared = TRUE;
3039 if (shared) {
3040 guint offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->klass_blob_hash, klass));
3041 guint8 *buf2, *p;
3043 if (!offset) {
3044 buf2 = (guint8 *)g_malloc (1024);
3045 p = buf2;
3047 encode_klass_ref_inner (acfg, klass, p, &p);
3048 g_assert (p - buf2 < 1024);
3050 offset = add_to_blob (acfg, buf2, p - buf2);
3051 g_free (buf2);
3053 g_hash_table_insert (acfg->klass_blob_hash, klass, GUINT_TO_POINTER (offset + 1));
3054 } else {
3055 offset --;
3058 p = buf;
3059 encode_value (MONO_AOT_TYPEREF_BLOB_INDEX, p, &p);
3060 encode_value (offset, p, &p);
3061 *endbuf = p;
3062 return;
3065 encode_klass_ref_inner (acfg, klass, buf, endbuf);
3068 static void
3069 encode_field_info (MonoAotCompile *cfg, MonoClassField *field, guint8 *buf, guint8 **endbuf)
3071 guint32 token = mono_get_field_token (field);
3072 guint8 *p = buf;
3074 encode_klass_ref (cfg, field->parent, p, &p);
3075 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_FIELD_DEF);
3076 encode_value (token - MONO_TOKEN_FIELD_DEF, p, &p);
3077 *endbuf = p;
3080 static void
3081 encode_ginst (MonoAotCompile *acfg, MonoGenericInst *inst, guint8 *buf, guint8 **endbuf)
3083 guint8 *p = buf;
3084 int i;
3086 encode_value (inst->type_argc, p, &p);
3087 for (i = 0; i < inst->type_argc; ++i)
3088 encode_klass_ref (acfg, mono_class_from_mono_type (inst->type_argv [i]), p, &p);
3089 *endbuf = p;
3092 static void
3093 encode_generic_context (MonoAotCompile *acfg, MonoGenericContext *context, guint8 *buf, guint8 **endbuf)
3095 guint8 *p = buf;
3096 MonoGenericInst *inst;
3098 inst = context->class_inst;
3099 if (inst) {
3100 g_assert (inst->type_argc);
3101 encode_ginst (acfg, inst, p, &p);
3102 } else {
3103 encode_value (0, p, &p);
3105 inst = context->method_inst;
3106 if (inst) {
3107 g_assert (inst->type_argc);
3108 encode_ginst (acfg, inst, p, &p);
3109 } else {
3110 encode_value (0, p, &p);
3112 *endbuf = p;
3115 static void
3116 encode_type (MonoAotCompile *acfg, MonoType *t, guint8 *buf, guint8 **endbuf)
3118 guint8 *p = buf;
3120 // Change memory allocation in decode_type if you change
3121 g_assert (!t->has_cmods);
3123 /* t->attrs can be ignored */
3124 //g_assert (t->attrs == 0);
3126 if (t->pinned) {
3127 *p = MONO_TYPE_PINNED;
3128 ++p;
3130 if (t->byref) {
3131 *p = MONO_TYPE_BYREF;
3132 ++p;
3135 *p = t->type;
3136 p ++;
3138 switch (t->type) {
3139 case MONO_TYPE_VOID:
3140 case MONO_TYPE_BOOLEAN:
3141 case MONO_TYPE_CHAR:
3142 case MONO_TYPE_I1:
3143 case MONO_TYPE_U1:
3144 case MONO_TYPE_I2:
3145 case MONO_TYPE_U2:
3146 case MONO_TYPE_I4:
3147 case MONO_TYPE_U4:
3148 case MONO_TYPE_I8:
3149 case MONO_TYPE_U8:
3150 case MONO_TYPE_R4:
3151 case MONO_TYPE_R8:
3152 case MONO_TYPE_I:
3153 case MONO_TYPE_U:
3154 case MONO_TYPE_STRING:
3155 case MONO_TYPE_OBJECT:
3156 case MONO_TYPE_TYPEDBYREF:
3157 break;
3158 case MONO_TYPE_VALUETYPE:
3159 case MONO_TYPE_CLASS:
3160 encode_klass_ref (acfg, mono_class_from_mono_type (t), p, &p);
3161 break;
3162 case MONO_TYPE_SZARRAY:
3163 encode_klass_ref (acfg, t->data.klass, p, &p);
3164 break;
3165 case MONO_TYPE_PTR:
3166 encode_type (acfg, t->data.type, p, &p);
3167 break;
3168 case MONO_TYPE_GENERICINST: {
3169 MonoClass *gclass = t->data.generic_class->container_class;
3170 MonoGenericInst *inst = t->data.generic_class->context.class_inst;
3172 encode_klass_ref (acfg, gclass, p, &p);
3173 encode_ginst (acfg, inst, p, &p);
3174 break;
3176 case MONO_TYPE_ARRAY: {
3177 MonoArrayType *array = t->data.array;
3178 int i;
3180 encode_klass_ref (acfg, array->eklass, p, &p);
3181 encode_value (array->rank, p, &p);
3182 encode_value (array->numsizes, p, &p);
3183 for (i = 0; i < array->numsizes; ++i)
3184 encode_value (array->sizes [i], p, &p);
3185 encode_value (array->numlobounds, p, &p);
3186 for (i = 0; i < array->numlobounds; ++i)
3187 encode_value (array->lobounds [i], p, &p);
3188 break;
3190 case MONO_TYPE_VAR:
3191 case MONO_TYPE_MVAR:
3192 encode_klass_ref (acfg, mono_class_from_mono_type (t), p, &p);
3193 break;
3194 default:
3195 g_assert_not_reached ();
3198 *endbuf = p;
3201 static void
3202 encode_signature (MonoAotCompile *acfg, MonoMethodSignature *sig, guint8 *buf, guint8 **endbuf)
3204 guint8 *p = buf;
3205 guint32 flags = 0;
3206 int i;
3208 /* Similar to the metadata encoding */
3209 if (sig->generic_param_count)
3210 flags |= 0x10;
3211 if (sig->hasthis)
3212 flags |= 0x20;
3213 if (sig->explicit_this)
3214 flags |= 0x40;
3215 flags |= (sig->call_convention & 0x0F);
3217 *p = flags;
3218 ++p;
3219 if (sig->generic_param_count)
3220 encode_value (sig->generic_param_count, p, &p);
3221 encode_value (sig->param_count, p, &p);
3223 encode_type (acfg, sig->ret, p, &p);
3224 for (i = 0; i < sig->param_count; ++i) {
3225 if (sig->sentinelpos == i) {
3226 *p = MONO_TYPE_SENTINEL;
3227 ++p;
3229 encode_type (acfg, sig->params [i], p, &p);
3232 *endbuf = p;
3235 #define MAX_IMAGE_INDEX 250
3237 static void
3238 encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8 **endbuf)
3240 guint32 image_index = get_image_index (acfg, m_class_get_image (method->klass));
3241 guint32 token = method->token;
3242 MonoJumpInfoToken *ji;
3243 guint8 *p = buf;
3246 * The encoding for most methods is as follows:
3247 * - image index encoded as a leb128
3248 * - token index encoded as a leb128
3249 * Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
3250 * types of method encodings.
3253 /* Mark methods which can't use aot trampolines because they need the further
3254 * processing in mono_magic_trampoline () which requires a MonoMethod*.
3256 if ((method->is_generic && (method->flags & METHOD_ATTRIBUTE_VIRTUAL)) ||
3257 (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED))
3258 encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE << 24), p, &p);
3260 if (method->wrapper_type) {
3261 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
3263 encode_value ((MONO_AOT_METHODREF_WRAPPER << 24), p, &p);
3265 encode_value (method->wrapper_type, p, &p);
3267 switch (method->wrapper_type) {
3268 case MONO_WRAPPER_REMOTING_INVOKE:
3269 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
3270 case MONO_WRAPPER_XDOMAIN_INVOKE: {
3271 MonoMethod *m;
3273 m = mono_marshal_method_from_wrapper (method);
3274 g_assert (m);
3275 encode_method_ref (acfg, m, p, &p);
3276 break;
3278 case MONO_WRAPPER_PROXY_ISINST:
3279 case MONO_WRAPPER_LDFLD:
3280 case MONO_WRAPPER_LDFLDA:
3281 case MONO_WRAPPER_STFLD: {
3282 g_assert (info);
3283 encode_klass_ref (acfg, info->d.proxy.klass, p, &p);
3284 break;
3286 case MONO_WRAPPER_ALLOC: {
3287 /* The GC name is saved once in MonoAotFileInfo */
3288 g_assert (info->d.alloc.alloc_type != -1);
3289 encode_value (info->d.alloc.alloc_type, p, &p);
3290 break;
3292 case MONO_WRAPPER_WRITE_BARRIER: {
3293 g_assert (info);
3294 break;
3296 case MONO_WRAPPER_STELEMREF: {
3297 g_assert (info);
3298 encode_value (info->subtype, p, &p);
3299 if (info->subtype == WRAPPER_SUBTYPE_VIRTUAL_STELEMREF)
3300 encode_value (info->d.virtual_stelemref.kind, p, &p);
3301 break;
3303 case MONO_WRAPPER_UNKNOWN: {
3304 g_assert (info);
3305 encode_value (info->subtype, p, &p);
3306 if (info->subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE ||
3307 info->subtype == WRAPPER_SUBTYPE_STRUCTURE_TO_PTR)
3308 encode_klass_ref (acfg, method->klass, p, &p);
3309 else if (info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER)
3310 encode_method_ref (acfg, info->d.synchronized_inner.method, p, &p);
3311 else if (info->subtype == WRAPPER_SUBTYPE_ARRAY_ACCESSOR)
3312 encode_method_ref (acfg, info->d.array_accessor.method, p, &p);
3313 else if (info->subtype == WRAPPER_SUBTYPE_INTERP_IN)
3314 encode_signature (acfg, info->d.interp_in.sig, p, &p);
3315 else if (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG)
3316 encode_signature (acfg, info->d.gsharedvt.sig, p, &p);
3317 else if (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG)
3318 encode_signature (acfg, info->d.gsharedvt.sig, p, &p);
3319 break;
3321 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
3322 g_assert (info);
3323 encode_value (info->subtype, p, &p);
3324 if (info->subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER) {
3325 strcpy ((char*)p, method->name);
3326 p += strlen (method->name) + 1;
3327 } else if (info->subtype == WRAPPER_SUBTYPE_NATIVE_FUNC_AOT) {
3328 encode_method_ref (acfg, info->d.managed_to_native.method, p, &p);
3329 } else {
3330 g_assert (info->subtype == WRAPPER_SUBTYPE_NONE || info->subtype == WRAPPER_SUBTYPE_PINVOKE);
3331 encode_method_ref (acfg, info->d.managed_to_native.method, p, &p);
3333 break;
3335 case MONO_WRAPPER_SYNCHRONIZED: {
3336 MonoMethod *m;
3338 m = mono_marshal_method_from_wrapper (method);
3339 g_assert (m);
3340 g_assert (m != method);
3341 encode_method_ref (acfg, m, p, &p);
3342 break;
3344 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
3345 g_assert (info);
3346 encode_value (info->subtype, p, &p);
3348 if (info->subtype == WRAPPER_SUBTYPE_ELEMENT_ADDR) {
3349 encode_value (info->d.element_addr.rank, p, &p);
3350 encode_value (info->d.element_addr.elem_size, p, &p);
3351 } else if (info->subtype == WRAPPER_SUBTYPE_STRING_CTOR) {
3352 encode_method_ref (acfg, info->d.string_ctor.method, p, &p);
3353 } else {
3354 g_assert_not_reached ();
3356 break;
3358 case MONO_WRAPPER_CASTCLASS: {
3359 g_assert (info);
3360 encode_value (info->subtype, p, &p);
3361 break;
3363 case MONO_WRAPPER_RUNTIME_INVOKE: {
3364 g_assert (info);
3365 encode_value (info->subtype, p, &p);
3366 if (info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT || info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL)
3367 encode_method_ref (acfg, info->d.runtime_invoke.method, p, &p);
3368 else if (info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL)
3369 encode_signature (acfg, info->d.runtime_invoke.sig, p, &p);
3370 break;
3372 case MONO_WRAPPER_DELEGATE_INVOKE:
3373 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
3374 case MONO_WRAPPER_DELEGATE_END_INVOKE: {
3375 if (method->is_inflated) {
3376 /* These wrappers are identified by their class */
3377 encode_value (1, p, &p);
3378 encode_klass_ref (acfg, method->klass, p, &p);
3379 } else {
3380 MonoMethodSignature *sig = mono_method_signature (method);
3381 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
3383 encode_value (0, p, &p);
3384 if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE)
3385 encode_value (info ? info->subtype : 0, p, &p);
3386 encode_signature (acfg, sig, p, &p);
3388 break;
3390 case MONO_WRAPPER_NATIVE_TO_MANAGED: {
3391 g_assert (info);
3392 encode_method_ref (acfg, info->d.native_to_managed.method, p, &p);
3393 encode_klass_ref (acfg, info->d.native_to_managed.klass, p, &p);
3394 break;
3396 default:
3397 g_assert_not_reached ();
3399 } else if (mono_method_signature (method)->is_inflated) {
3401 * This is a generic method, find the original token which referenced it and
3402 * encode that.
3403 * Obtain the token from information recorded by the JIT.
3405 ji = (MonoJumpInfoToken *)g_hash_table_lookup (acfg->token_info_hash, method);
3406 if (ji) {
3407 image_index = get_image_index (acfg, ji->image);
3408 g_assert (image_index < MAX_IMAGE_INDEX);
3409 token = ji->token;
3411 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
3412 encode_value (image_index, p, &p);
3413 encode_value (token, p, &p);
3414 } else {
3415 MonoMethod *declaring;
3416 MonoGenericContext *context = mono_method_get_context (method);
3418 g_assert (method->is_inflated);
3419 declaring = ((MonoMethodInflated*)method)->declaring;
3422 * This might be a non-generic method of a generic instance, which
3423 * doesn't have a token since the reference is generated by the JIT
3424 * like Nullable:Box/Unbox, or by generic sharing.
3426 encode_value ((MONO_AOT_METHODREF_GINST << 24), p, &p);
3427 /* Encode the klass */
3428 encode_klass_ref (acfg, method->klass, p, &p);
3429 /* Encode the method */
3430 image_index = get_image_index (acfg, m_class_get_image (method->klass));
3431 g_assert (image_index < MAX_IMAGE_INDEX);
3432 g_assert (declaring->token);
3433 token = declaring->token;
3434 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
3435 encode_value (image_index, p, &p);
3436 encode_value (token, p, &p);
3437 encode_generic_context (acfg, context, p, &p);
3439 } else if (token == 0) {
3440 /* This might be a method of a constructed type like int[,].Set */
3441 /* Obtain the token from information recorded by the JIT */
3442 ji = (MonoJumpInfoToken *)g_hash_table_lookup (acfg->token_info_hash, method);
3443 if (ji) {
3444 image_index = get_image_index (acfg, ji->image);
3445 g_assert (image_index < MAX_IMAGE_INDEX);
3446 token = ji->token;
3448 encode_value ((MONO_AOT_METHODREF_METHODSPEC << 24), p, &p);
3449 encode_value (image_index, p, &p);
3450 encode_value (token, p, &p);
3451 } else {
3452 /* Array methods */
3453 g_assert (m_class_get_rank (method->klass));
3455 /* Encode directly */
3456 encode_value ((MONO_AOT_METHODREF_ARRAY << 24), p, &p);
3457 encode_klass_ref (acfg, method->klass, p, &p);
3458 if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == m_class_get_rank (method->klass))
3459 encode_value (0, p, &p);
3460 else if (!strcmp (method->name, ".ctor") && mono_method_signature (method)->param_count == m_class_get_rank (method->klass) * 2)
3461 encode_value (1, p, &p);
3462 else if (!strcmp (method->name, "Get"))
3463 encode_value (2, p, &p);
3464 else if (!strcmp (method->name, "Address"))
3465 encode_value (3, p, &p);
3466 else if (!strcmp (method->name, "Set"))
3467 encode_value (4, p, &p);
3468 else
3469 g_assert_not_reached ();
3471 } else {
3472 g_assert (mono_metadata_token_table (token) == MONO_TABLE_METHOD);
3474 if (image_index >= MONO_AOT_METHODREF_MIN) {
3475 encode_value ((MONO_AOT_METHODREF_LARGE_IMAGE_INDEX << 24), p, &p);
3476 encode_value (image_index, p, &p);
3477 encode_value (mono_metadata_token_index (token), p, &p);
3478 } else {
3479 encode_value ((image_index << 24) | mono_metadata_token_index (token), p, &p);
3482 *endbuf = p;
3485 static gint
3486 compare_patches (gconstpointer a, gconstpointer b)
3488 int i, j;
3490 i = (*(MonoJumpInfo**)a)->ip.i;
3491 j = (*(MonoJumpInfo**)b)->ip.i;
3493 if (i < j)
3494 return -1;
3495 else
3496 if (i > j)
3497 return 1;
3498 else
3499 return 0;
3502 static G_GNUC_UNUSED char*
3503 patch_to_string (MonoJumpInfo *patch_info)
3505 GString *str;
3507 str = g_string_new ("");
3509 g_string_append_printf (str, "%s(", get_patch_name (patch_info->type));
3511 switch (patch_info->type) {
3512 case MONO_PATCH_INFO_VTABLE:
3513 mono_type_get_desc (str, m_class_get_byval_arg (patch_info->data.klass), TRUE);
3514 break;
3515 default:
3516 break;
3518 g_string_append_printf (str, ")");
3519 return g_string_free (str, FALSE);
3523 * is_plt_patch:
3525 * Return whenever PATCH_INFO refers to a direct call, and thus requires a
3526 * PLT entry.
3528 static inline gboolean
3529 is_plt_patch (MonoJumpInfo *patch_info)
3531 switch (patch_info->type) {
3532 case MONO_PATCH_INFO_METHOD:
3533 case MONO_PATCH_INFO_INTERNAL_METHOD:
3534 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
3535 case MONO_PATCH_INFO_ICALL_ADDR_CALL:
3536 case MONO_PATCH_INFO_RGCTX_FETCH:
3537 return TRUE;
3538 default:
3539 return FALSE;
3544 * get_plt_symbol:
3546 * Return the symbol identifying the plt entry PLT_OFFSET.
3548 static char*
3549 get_plt_symbol (MonoAotCompile *acfg, int plt_offset, MonoJumpInfo *patch_info)
3551 #ifdef TARGET_MACH
3553 * The Apple linker reorganizes object files, so it doesn't like branches to local
3554 * labels, since those have no relocations.
3556 return g_strdup_printf ("%sp_%d", acfg->llvm_label_prefix, plt_offset);
3557 #else
3558 return g_strdup_printf ("%sp_%d", acfg->temp_prefix, plt_offset);
3559 #endif
3563 * get_plt_entry:
3565 * Return a PLT entry which belongs to the method identified by PATCH_INFO.
3567 static MonoPltEntry*
3568 get_plt_entry (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
3570 MonoPltEntry *res;
3571 gboolean synchronized = FALSE;
3572 static int synchronized_symbol_idx;
3574 if (!is_plt_patch (patch_info))
3575 return NULL;
3577 if (!acfg->patch_to_plt_entry [patch_info->type])
3578 acfg->patch_to_plt_entry [patch_info->type] = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
3579 res = (MonoPltEntry *)g_hash_table_lookup (acfg->patch_to_plt_entry [patch_info->type], patch_info);
3581 if (!acfg->llvm && patch_info->type == MONO_PATCH_INFO_METHOD && (patch_info->data.method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED)) {
3583 * Allocate a separate PLT slot for each such patch, since some plt
3584 * entries will refer to the method itself, and some will refer to the
3585 * wrapper.
3587 res = NULL;
3588 synchronized = TRUE;
3591 if (!res) {
3592 MonoJumpInfo *new_ji;
3594 new_ji = mono_patch_info_dup_mp (acfg->mempool, patch_info);
3596 res = (MonoPltEntry *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoPltEntry));
3597 res->plt_offset = acfg->plt_offset;
3598 res->ji = new_ji;
3599 res->symbol = get_plt_symbol (acfg, res->plt_offset, patch_info);
3600 if (acfg->aot_opts.write_symbols)
3601 res->debug_sym = get_plt_entry_debug_sym (acfg, res->ji, acfg->plt_entry_debug_sym_cache);
3602 if (synchronized) {
3603 /* Avoid duplicate symbols because we don't cache */
3604 res->symbol = g_strdup_printf ("%s_%d", res->symbol, synchronized_symbol_idx);
3605 if (res->debug_sym)
3606 res->debug_sym = g_strdup_printf ("%s_%d", res->debug_sym, synchronized_symbol_idx);
3607 synchronized_symbol_idx ++;
3609 if (res->debug_sym)
3610 res->llvm_symbol = g_strdup_printf ("%s_%s_llvm", res->symbol, res->debug_sym);
3611 else
3612 res->llvm_symbol = g_strdup_printf ("%s_llvm", res->symbol);
3613 if (strstr (res->llvm_symbol, acfg->temp_prefix) == res->llvm_symbol) {
3614 /* The llvm symbol shouldn't be temporary, since the llvm generated object file references it */
3615 char *tmp = res->llvm_symbol;
3616 res->llvm_symbol = g_strdup (res->llvm_symbol + strlen (acfg->temp_prefix));
3617 g_free (tmp);
3620 g_hash_table_insert (acfg->patch_to_plt_entry [new_ji->type], new_ji, res);
3622 g_hash_table_insert (acfg->plt_offset_to_entry, GUINT_TO_POINTER (res->plt_offset), res);
3624 //g_assert (mono_patch_info_equal (patch_info, new_ji));
3625 //mono_print_ji (patch_info); printf ("\n");
3626 //g_hash_table_print_stats (acfg->patch_to_plt_entry);
3628 acfg->plt_offset ++;
3631 return res;
3635 * get_got_offset:
3637 * Returns the offset of the GOT slot where the runtime object resulting from resolving
3638 * JI could be found if it exists, otherwise allocates a new one.
3640 static guint32
3641 get_got_offset (MonoAotCompile *acfg, gboolean llvm, MonoJumpInfo *ji)
3643 guint32 got_offset;
3644 GotInfo *info = llvm ? &acfg->llvm_got_info : &acfg->got_info;
3646 got_offset = GPOINTER_TO_UINT (g_hash_table_lookup (info->patch_to_got_offset_by_type [ji->type], ji));
3647 if (got_offset)
3648 return got_offset - 1;
3650 if (llvm) {
3651 got_offset = acfg->llvm_got_offset;
3652 acfg->llvm_got_offset ++;
3653 } else {
3654 got_offset = acfg->got_offset;
3655 acfg->got_offset ++;
3658 acfg->stats.got_slots ++;
3659 acfg->stats.got_slot_types [ji->type] ++;
3661 g_hash_table_insert (info->patch_to_got_offset, ji, GUINT_TO_POINTER (got_offset + 1));
3662 g_hash_table_insert (info->patch_to_got_offset_by_type [ji->type], ji, GUINT_TO_POINTER (got_offset + 1));
3663 g_ptr_array_add (info->got_patches, ji);
3665 return got_offset;
3668 /* Add a method to the list of methods which need to be emitted */
3669 static void
3670 add_method_with_index (MonoAotCompile *acfg, MonoMethod *method, int index, gboolean extra)
3672 g_assert (method);
3673 if (!g_hash_table_lookup (acfg->method_indexes, method)) {
3674 g_ptr_array_add (acfg->methods, method);
3675 g_hash_table_insert (acfg->method_indexes, method, GUINT_TO_POINTER (index + 1));
3676 acfg->nmethods = acfg->methods->len + 1;
3679 if (method->wrapper_type || extra)
3680 g_ptr_array_add (acfg->extra_methods, method);
3683 static gboolean
3684 prefer_gsharedvt_method (MonoAotCompile *acfg, MonoMethod *method)
3686 /* One instantiation with valuetypes is generated for each async method */
3687 if (m_class_get_image (method->klass) == mono_defaults.corlib && (!strcmp (m_class_get_name (method->klass), "AsyncMethodBuilderCore") || !strcmp (m_class_get_name (method->klass), "AsyncVoidMethodBuilder")))
3688 return TRUE;
3689 else
3690 return FALSE;
3693 static guint32
3694 get_method_index (MonoAotCompile *acfg, MonoMethod *method)
3696 int index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
3698 g_assert (index);
3700 return index - 1;
3703 static int
3704 add_method_full (MonoAotCompile *acfg, MonoMethod *method, gboolean extra, int depth)
3706 int index;
3708 index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_indexes, method));
3709 if (index)
3710 return index - 1;
3712 index = acfg->method_index;
3713 add_method_with_index (acfg, method, index, extra);
3715 g_ptr_array_add (acfg->method_order, GUINT_TO_POINTER (index));
3717 g_hash_table_insert (acfg->method_depth, method, GUINT_TO_POINTER (depth));
3719 acfg->method_index ++;
3721 return index;
3724 static int
3725 add_method (MonoAotCompile *acfg, MonoMethod *method)
3727 return add_method_full (acfg, method, FALSE, 0);
3730 static void
3731 mono_dedup_cache_method (MonoAotCompile *acfg, MonoMethod *method)
3733 g_assert (acfg->dedup_stats);
3735 char *name = mono_aot_get_mangled_method_name (method);
3736 g_assert (name);
3738 // For stats
3739 char *stats_name = g_strdup (name);
3741 g_assert (acfg->dedup_cache);
3743 if (!g_hash_table_lookup (acfg->dedup_cache, name)) {
3744 // This AOTCompile owns this method
3745 // We do this to decide whether to write it to disk
3746 // during a dedup run (first phase, where we skip).
3748 // If never changed, then maybe can avoid a recompile
3749 // of the cache.
3751 // Files not read in during last phase.
3752 acfg->dedup_cache_changed = TRUE;
3754 // owns name
3755 g_hash_table_insert (acfg->dedup_cache, name, method);
3756 } else {
3757 // owns name
3758 g_free (name);
3761 guint count = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->dedup_stats, stats_name));
3762 count++;
3763 g_hash_table_insert (acfg->dedup_stats, stats_name, GUINT_TO_POINTER (count));
3766 static void
3767 add_extra_method_with_depth (MonoAotCompile *acfg, MonoMethod *method, int depth)
3769 ERROR_DECL (error);
3770 if (mono_method_is_generic_sharable_full (method, TRUE, TRUE, FALSE)) {
3771 method = mini_get_shared_method_full (method, SHARE_MODE_NONE, error);
3772 mono_error_assert_ok (error);
3774 else if ((acfg->opts & MONO_OPT_GSHAREDVT) && prefer_gsharedvt_method (acfg, method) && mono_method_is_generic_sharable_full (method, FALSE, FALSE, TRUE)) {
3775 /* Use the gsharedvt version */
3776 method = mini_get_shared_method_full (method, SHARE_MODE_GSHAREDVT, error);
3777 mono_error_assert_ok (error);
3780 if ((acfg->aot_opts.dedup || acfg->aot_opts.dedup_include) && mono_aot_can_dedup (method)) {
3781 mono_dedup_cache_method (acfg, method);
3783 if (!acfg->dedup_emit_mode)
3784 return;
3787 if (acfg->aot_opts.log_generics)
3788 aot_printf (acfg, "%*sAdding method %s.\n", depth, "", mono_method_get_full_name (method));
3790 add_method_full (acfg, method, TRUE, depth);
3793 static void
3794 add_extra_method (MonoAotCompile *acfg, MonoMethod *method)
3796 add_extra_method_with_depth (acfg, method, 0);
3799 static void
3800 add_jit_icall_wrapper (gpointer key, gpointer value, gpointer user_data)
3802 MonoAotCompile *acfg = (MonoAotCompile *)user_data;
3803 MonoJitICallInfo *callinfo = (MonoJitICallInfo *)value;
3804 MonoMethod *wrapper;
3805 char *name;
3807 if (!callinfo->sig)
3808 return;
3810 name = g_strdup_printf ("__icall_wrapper_%s", callinfo->name);
3811 wrapper = mono_marshal_get_icall_wrapper (callinfo->sig, name, callinfo->func, TRUE);
3812 g_free (name);
3814 add_method (acfg, wrapper);
3817 static MonoMethod*
3818 get_runtime_invoke_sig (MonoMethodSignature *sig)
3820 MonoMethodBuilder *mb;
3821 MonoMethod *m;
3823 mb = mono_mb_new (mono_defaults.object_class, "FOO", MONO_WRAPPER_NONE);
3824 m = mono_mb_create_method (mb, sig, 16);
3825 MonoMethod *invoke = mono_marshal_get_runtime_invoke (m, FALSE);
3826 mono_mb_free (mb);
3827 return invoke;
3830 static MonoMethod*
3831 get_runtime_invoke (MonoAotCompile *acfg, MonoMethod *method, gboolean virtual_)
3833 return mono_marshal_get_runtime_invoke (method, virtual_);
3836 static gboolean
3837 can_marshal_struct (MonoClass *klass)
3839 MonoClassField *field;
3840 gboolean can_marshal = TRUE;
3841 gpointer iter = NULL;
3842 MonoMarshalType *info;
3843 int i;
3845 if (mono_class_is_auto_layout (klass))
3846 return FALSE;
3848 info = mono_marshal_load_type_info (klass);
3850 /* Only allow a few field types to avoid asserts in the marshalling code */
3851 while ((field = mono_class_get_fields (klass, &iter))) {
3852 if ((field->type->attrs & FIELD_ATTRIBUTE_STATIC))
3853 continue;
3855 switch (field->type->type) {
3856 case MONO_TYPE_I4:
3857 case MONO_TYPE_U4:
3858 case MONO_TYPE_I1:
3859 case MONO_TYPE_U1:
3860 case MONO_TYPE_BOOLEAN:
3861 case MONO_TYPE_I2:
3862 case MONO_TYPE_U2:
3863 case MONO_TYPE_CHAR:
3864 case MONO_TYPE_I8:
3865 case MONO_TYPE_U8:
3866 case MONO_TYPE_I:
3867 case MONO_TYPE_U:
3868 case MONO_TYPE_PTR:
3869 case MONO_TYPE_R4:
3870 case MONO_TYPE_R8:
3871 case MONO_TYPE_STRING:
3872 break;
3873 case MONO_TYPE_VALUETYPE:
3874 if (!m_class_is_enumtype (mono_class_from_mono_type (field->type)) && !can_marshal_struct (mono_class_from_mono_type (field->type)))
3875 can_marshal = FALSE;
3876 break;
3877 case MONO_TYPE_SZARRAY: {
3878 gboolean has_mspec = FALSE;
3880 if (info) {
3881 for (i = 0; i < info->num_fields; ++i) {
3882 if (info->fields [i].field == field && info->fields [i].mspec)
3883 has_mspec = TRUE;
3886 if (!has_mspec)
3887 can_marshal = FALSE;
3888 break;
3890 default:
3891 can_marshal = FALSE;
3892 break;
3896 /* Special cases */
3897 /* Its hard to compute whenever these can be marshalled or not */
3898 if (!strcmp (m_class_get_name_space (klass), "System.Net.NetworkInformation.MacOsStructs") && strcmp (m_class_get_name (klass), "sockaddr_dl"))
3899 return TRUE;
3901 return can_marshal;
3904 static void
3905 create_gsharedvt_inst (MonoAotCompile *acfg, MonoMethod *method, MonoGenericContext *ctx)
3907 /* Create a vtype instantiation */
3908 MonoGenericContext shared_context;
3909 MonoType **args;
3910 MonoGenericInst *inst;
3911 MonoGenericContainer *container;
3912 MonoClass **constraints;
3913 int i;
3915 memset (ctx, 0, sizeof (MonoGenericContext));
3917 if (mono_class_is_gtd (method->klass)) {
3918 shared_context = mono_class_get_generic_container (method->klass)->context;
3919 inst = shared_context.class_inst;
3921 args = g_new0 (MonoType*, inst->type_argc);
3922 for (i = 0; i < inst->type_argc; ++i) {
3923 args [i] = mono_get_int_type ();
3925 ctx->class_inst = mono_metadata_get_generic_inst (inst->type_argc, args);
3927 if (method->is_generic) {
3928 container = mono_method_get_generic_container (method);
3929 g_assert (!container->is_anonymous && container->is_method);
3930 shared_context = container->context;
3931 inst = shared_context.method_inst;
3933 args = g_new0 (MonoType*, inst->type_argc);
3934 for (i = 0; i < container->type_argc; ++i) {
3935 MonoGenericParamInfo *info = mono_generic_param_info (&container->type_params [i]);
3936 gboolean ref_only = FALSE;
3938 if (info && info->constraints) {
3939 constraints = info->constraints;
3941 while (*constraints) {
3942 MonoClass *cklass = *constraints;
3943 if (!(cklass == mono_defaults.object_class || (m_class_get_image (cklass) == mono_defaults.corlib && !strcmp (m_class_get_name (cklass), "ValueType"))))
3944 /* Inflaring the method with our vtype would not be valid */
3945 ref_only = TRUE;
3946 constraints ++;
3950 if (ref_only)
3951 args [i] = mono_get_object_type ();
3952 else
3953 args [i] = mono_get_int_type ();
3955 ctx->method_inst = mono_metadata_get_generic_inst (inst->type_argc, args);
3959 static void
3960 add_wrappers (MonoAotCompile *acfg)
3962 MonoMethod *method, *m;
3963 int i, j;
3964 MonoMethodSignature *sig, *csig;
3965 guint32 token;
3968 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
3969 * so there is only one wrapper of a given type, or inlining their contents into their
3970 * callers.
3972 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
3973 ERROR_DECL (error);
3974 MonoMethod *method;
3975 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
3976 gboolean skip = FALSE;
3978 method = mono_get_method_checked (acfg->image, token, NULL, NULL, error);
3979 report_loader_error (acfg, error, TRUE, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (error));
3981 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
3982 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
3983 (method->flags & METHOD_ATTRIBUTE_ABSTRACT))
3984 skip = TRUE;
3986 /* Skip methods which can not be handled by get_runtime_invoke () */
3987 sig = mono_method_signature (method);
3988 if (!sig)
3989 continue;
3990 if ((sig->ret->type == MONO_TYPE_PTR) ||
3991 (sig->ret->type == MONO_TYPE_TYPEDBYREF))
3992 skip = TRUE;
3993 if (mono_class_is_open_constructed_type (sig->ret))
3994 skip = TRUE;
3996 for (j = 0; j < sig->param_count; j++) {
3997 if (sig->params [j]->type == MONO_TYPE_TYPEDBYREF)
3998 skip = TRUE;
3999 if (mono_class_is_open_constructed_type (sig->params [j]))
4000 skip = TRUE;
4003 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
4004 if (!mono_class_is_contextbound (method->klass)) {
4005 MonoDynCallInfo *info = mono_arch_dyn_call_prepare (sig);
4006 gboolean has_nullable = FALSE;
4008 for (j = 0; j < sig->param_count; j++) {
4009 if (sig->params [j]->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type (sig->params [j])))
4010 has_nullable = TRUE;
4013 if (info && !has_nullable && !acfg->aot_opts.llvm_only) {
4014 /* Supported by the dynamic runtime-invoke wrapper */
4015 skip = TRUE;
4017 if (info)
4018 mono_arch_dyn_call_free (info);
4020 #endif
4022 if (acfg->aot_opts.llvm_only)
4023 /* Supported by the gsharedvt based runtime-invoke wrapper */
4024 skip = TRUE;
4026 if (!skip) {
4027 //printf ("%s\n", mono_method_full_name (method, TRUE));
4028 add_method (acfg, get_runtime_invoke (acfg, method, FALSE));
4032 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
4033 int nallocators;
4035 /* Runtime invoke wrappers */
4037 MonoType *void_type = mono_get_void_type ();
4038 MonoType *string_type = m_class_get_byval_arg (mono_defaults.string_class);
4040 /* void runtime-invoke () [.cctor] */
4041 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4042 csig->ret = void_type;
4043 add_method (acfg, get_runtime_invoke_sig (csig));
4045 /* void runtime-invoke () [Finalize] */
4046 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4047 csig->hasthis = 1;
4048 csig->ret = void_type;
4049 add_method (acfg, get_runtime_invoke_sig (csig));
4051 /* void runtime-invoke (string) [exception ctor] */
4052 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 1);
4053 csig->hasthis = 1;
4054 csig->ret = void_type;
4055 csig->params [0] = string_type;
4056 add_method (acfg, get_runtime_invoke_sig (csig));
4058 /* void runtime-invoke (string, string) [exception ctor] */
4059 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4060 csig->hasthis = 1;
4061 csig->ret = void_type;
4062 csig->params [0] = string_type;
4063 csig->params [1] = string_type;
4064 add_method (acfg, get_runtime_invoke_sig (csig));
4066 /* string runtime-invoke () [Exception.ToString ()] */
4067 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 0);
4068 csig->hasthis = 1;
4069 csig->ret = string_type;
4070 add_method (acfg, get_runtime_invoke_sig (csig));
4072 /* void runtime-invoke (string, Exception) [exception ctor] */
4073 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 2);
4074 csig->hasthis = 1;
4075 csig->ret = void_type;
4076 csig->params [0] = string_type;
4077 csig->params [1] = m_class_get_byval_arg (mono_defaults.exception_class);
4078 add_method (acfg, get_runtime_invoke_sig (csig));
4080 /* Assembly runtime-invoke (string, Assembly, bool) [DoAssemblyResolve] */
4081 csig = mono_metadata_signature_alloc (mono_defaults.corlib, 3);
4082 csig->hasthis = 1;
4083 csig->ret = m_class_get_byval_arg (mono_class_load_from_name (mono_defaults.corlib, "System.Reflection", "Assembly"));
4084 csig->params [0] = string_type;
4085 csig->params [1] = m_class_get_byval_arg (mono_class_load_from_name (mono_defaults.corlib, "System.Reflection", "Assembly"));
4086 csig->params [2] = m_class_get_byval_arg (mono_defaults.boolean_class);
4087 add_method (acfg, get_runtime_invoke_sig (csig));
4089 /* runtime-invoke used by finalizers */
4090 add_method (acfg, get_runtime_invoke (acfg, mono_class_get_method_from_name_flags (mono_defaults.object_class, "Finalize", 0, 0), TRUE));
4092 /* This is used by mono_runtime_capture_context () */
4093 method = mono_get_context_capture_method ();
4094 if (method)
4095 add_method (acfg, get_runtime_invoke (acfg, method, FALSE));
4097 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
4098 if (!acfg->aot_opts.llvm_only)
4099 add_method (acfg, mono_marshal_get_runtime_invoke_dynamic ());
4100 #endif
4102 /* These are used by mono_jit_runtime_invoke () to calls gsharedvt out wrappers */
4103 if (acfg->aot_opts.llvm_only) {
4104 int variants;
4106 /* Create simplified signatures which match the signature used by the gsharedvt out wrappers */
4107 for (variants = 0; variants < 4; ++variants) {
4108 for (i = 0; i < 16; ++i) {
4109 sig = mini_get_gsharedvt_out_sig_wrapper_signature ((variants & 1) > 0, (variants & 2) > 0, i);
4110 add_extra_method (acfg, mono_marshal_get_runtime_invoke_for_sig (sig));
4112 g_free (sig);
4117 /* stelemref */
4118 add_method (acfg, mono_marshal_get_stelemref ());
4120 /* Managed Allocators */
4121 nallocators = mono_gc_get_managed_allocator_types ();
4122 for (i = 0; i < nallocators; ++i) {
4123 if ((m = mono_gc_get_managed_allocator_by_type (i, MANAGED_ALLOCATOR_REGULAR)))
4124 add_method (acfg, m);
4125 if ((m = mono_gc_get_managed_allocator_by_type (i, MANAGED_ALLOCATOR_SLOW_PATH)))
4126 add_method (acfg, m);
4127 if ((m = mono_gc_get_managed_allocator_by_type (i, MANAGED_ALLOCATOR_PROFILER)))
4128 add_method (acfg, m);
4131 /* write barriers */
4132 if (mono_gc_is_moving ()) {
4133 add_method (acfg, mono_gc_get_specific_write_barrier (FALSE));
4134 add_method (acfg, mono_gc_get_specific_write_barrier (TRUE));
4137 /* Stelemref wrappers */
4139 MonoMethod **wrappers;
4140 int nwrappers;
4142 wrappers = mono_marshal_get_virtual_stelemref_wrappers (&nwrappers);
4143 for (i = 0; i < nwrappers; ++i)
4144 add_method (acfg, wrappers [i]);
4145 g_free (wrappers);
4148 /* castclass_with_check wrapper */
4149 add_method (acfg, mono_marshal_get_castclass_with_cache ());
4150 /* isinst_with_check wrapper */
4151 add_method (acfg, mono_marshal_get_isinst_with_cache ());
4153 /* JIT icall wrappers */
4154 /* FIXME: locking - this is "safe" as full-AOT threads don't mutate the icall hash*/
4155 g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper, acfg);
4159 * remoting-invoke-with-check wrappers are very frequent, so avoid emitting them,
4160 * we use the original method instead at runtime.
4161 * Since full-aot doesn't support remoting, this is not a problem.
4163 #if 0
4164 /* remoting-invoke wrappers */
4165 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
4166 ERROR_DECL (error);
4167 MonoMethodSignature *sig;
4169 token = MONO_TOKEN_METHOD_DEF | (i + 1);
4170 method = mono_get_method_checked (acfg->image, token, NULL, NULL, error);
4171 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
4173 sig = mono_method_signature (method);
4175 if (sig->hasthis && (method->klass->marshalbyref || method->klass == mono_defaults.object_class)) {
4176 m = mono_marshal_get_remoting_invoke_with_check (method);
4178 add_method (acfg, m);
4181 #endif
4183 /* delegate-invoke wrappers */
4184 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
4185 ERROR_DECL (error);
4186 MonoClass *klass;
4187 MonoCustomAttrInfo *cattr;
4189 token = MONO_TOKEN_TYPE_DEF | (i + 1);
4190 klass = mono_class_get_checked (acfg->image, token, error);
4192 if (!klass) {
4193 mono_error_cleanup (error);
4194 continue;
4197 if (!m_class_is_delegate (klass) || klass == mono_defaults.delegate_class || klass == mono_defaults.multicastdelegate_class)
4198 continue;
4200 if (!mono_class_is_gtd (klass)) {
4201 method = mono_get_delegate_invoke (klass);
4203 m = mono_marshal_get_delegate_invoke (method, NULL);
4205 add_method (acfg, m);
4207 method = mono_class_get_method_from_name_flags (klass, "BeginInvoke", -1, 0);
4208 if (method)
4209 add_method (acfg, mono_marshal_get_delegate_begin_invoke (method));
4211 method = mono_class_get_method_from_name_flags (klass, "EndInvoke", -1, 0);
4212 if (method)
4213 add_method (acfg, mono_marshal_get_delegate_end_invoke (method));
4215 cattr = mono_custom_attrs_from_class_checked (klass, error);
4216 if (!is_ok (error)) {
4217 mono_error_cleanup (error);
4218 continue;
4221 if (cattr) {
4222 int j;
4224 for (j = 0; j < cattr->num_attrs; ++j)
4225 if (cattr->attrs [j].ctor && (!strcmp (m_class_get_name (cattr->attrs [j].ctor->klass), "MonoNativeFunctionWrapperAttribute") || !strcmp (m_class_get_name (cattr->attrs [j].ctor->klass), "UnmanagedFunctionPointerAttribute")))
4226 break;
4227 if (j < cattr->num_attrs) {
4228 MonoMethod *invoke;
4229 MonoMethod *wrapper;
4230 MonoMethod *del_invoke;
4232 /* Add wrappers needed by mono_ftnptr_to_delegate () */
4233 invoke = mono_get_delegate_invoke (klass);
4234 wrapper = mono_marshal_get_native_func_wrapper_aot (klass);
4235 del_invoke = mono_marshal_get_delegate_invoke_internal (invoke, FALSE, TRUE, wrapper);
4236 add_method (acfg, wrapper);
4237 add_method (acfg, del_invoke);
4240 } else if ((acfg->opts & MONO_OPT_GSHAREDVT) && mono_class_is_gtd (klass)) {
4241 ERROR_DECL (error);
4242 MonoGenericContext ctx;
4243 MonoMethod *inst, *gshared;
4246 * Emit gsharedvt versions of the generic delegate-invoke wrappers
4248 /* Invoke */
4249 method = mono_get_delegate_invoke (klass);
4250 create_gsharedvt_inst (acfg, method, &ctx);
4252 inst = mono_class_inflate_generic_method_checked (method, &ctx, error);
4253 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
4255 m = mono_marshal_get_delegate_invoke (inst, NULL);
4256 g_assert (m->is_inflated);
4258 gshared = mini_get_shared_method_full (m, SHARE_MODE_GSHAREDVT, error);
4259 mono_error_assert_ok (error);
4261 add_extra_method (acfg, gshared);
4263 /* begin-invoke */
4264 method = mono_get_delegate_begin_invoke (klass);
4265 if (method) {
4266 create_gsharedvt_inst (acfg, method, &ctx);
4268 inst = mono_class_inflate_generic_method_checked (method, &ctx, error);
4269 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
4271 m = mono_marshal_get_delegate_begin_invoke (inst);
4272 g_assert (m->is_inflated);
4274 gshared = mini_get_shared_method_full (m, SHARE_MODE_GSHAREDVT, error);
4275 mono_error_assert_ok (error);
4277 add_extra_method (acfg, gshared);
4280 /* end-invoke */
4281 method = mono_get_delegate_end_invoke (klass);
4282 if (method) {
4283 create_gsharedvt_inst (acfg, method, &ctx);
4285 inst = mono_class_inflate_generic_method_checked (method, &ctx, error);
4286 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
4288 m = mono_marshal_get_delegate_end_invoke (inst);
4289 g_assert (m->is_inflated);
4291 gshared = mini_get_shared_method_full (m, SHARE_MODE_GSHAREDVT, error);
4292 mono_error_assert_ok (error);
4294 add_extra_method (acfg, gshared);
4299 /* array access wrappers */
4300 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
4301 ERROR_DECL (error);
4302 MonoClass *klass;
4304 token = MONO_TOKEN_TYPE_SPEC | (i + 1);
4305 klass = mono_class_get_checked (acfg->image, token, error);
4307 if (!klass) {
4308 mono_error_cleanup (error);
4309 continue;
4312 if (m_class_get_rank (klass) && MONO_TYPE_IS_PRIMITIVE (m_class_get_byval_arg (m_class_get_element_class (klass)))) {
4313 MonoMethod *m, *wrapper;
4315 /* Add runtime-invoke wrappers too */
4317 m = mono_class_get_method_from_name (klass, "Get", -1);
4318 g_assert (m);
4319 wrapper = mono_marshal_get_array_accessor_wrapper (m);
4320 add_extra_method (acfg, wrapper);
4321 if (!acfg->aot_opts.llvm_only)
4322 add_extra_method (acfg, get_runtime_invoke (acfg, wrapper, FALSE));
4324 m = mono_class_get_method_from_name (klass, "Set", -1);
4325 g_assert (m);
4326 wrapper = mono_marshal_get_array_accessor_wrapper (m);
4327 add_extra_method (acfg, wrapper);
4328 if (!acfg->aot_opts.llvm_only)
4329 add_extra_method (acfg, get_runtime_invoke (acfg, wrapper, FALSE));
4333 /* Synchronized wrappers */
4334 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
4335 ERROR_DECL (error);
4336 token = MONO_TOKEN_METHOD_DEF | (i + 1);
4337 method = mono_get_method_checked (acfg->image, token, NULL, NULL, error);
4338 report_loader_error (acfg, error, TRUE, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (error));
4340 if (method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) {
4341 if (method->is_generic) {
4342 // FIXME:
4343 } else if ((acfg->opts & MONO_OPT_GSHAREDVT) && mono_class_is_gtd (method->klass)) {
4344 ERROR_DECL (error);
4345 MonoGenericContext ctx;
4346 MonoMethod *inst, *gshared, *m;
4349 * Create a generic wrapper for a generic instance, and AOT that.
4351 create_gsharedvt_inst (acfg, method, &ctx);
4352 inst = mono_class_inflate_generic_method_checked (method, &ctx, error);
4353 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
4354 m = mono_marshal_get_synchronized_wrapper (inst);
4355 g_assert (m->is_inflated);
4356 gshared = mini_get_shared_method_full (m, SHARE_MODE_GSHAREDVT, error);
4357 mono_error_assert_ok (error);
4359 add_method (acfg, gshared);
4360 } else {
4361 add_method (acfg, mono_marshal_get_synchronized_wrapper (method));
4366 /* pinvoke wrappers */
4367 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
4368 ERROR_DECL (error);
4369 MonoMethod *method;
4370 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
4372 method = mono_get_method_checked (acfg->image, token, NULL, NULL, error);
4373 report_loader_error (acfg, error, TRUE, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (error));
4375 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
4376 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
4377 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
4380 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
4381 if (acfg->aot_opts.llvm_only) {
4382 /* The wrappers have a different signature (hasthis is not set) so need to add this too */
4383 add_gsharedvt_wrappers (acfg, mono_method_signature (method), FALSE, TRUE, FALSE);
4388 /* native-to-managed wrappers */
4389 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHOD].rows; ++i) {
4390 ERROR_DECL (error);
4391 MonoMethod *method;
4392 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
4393 MonoCustomAttrInfo *cattr;
4394 int j;
4396 method = mono_get_method_checked (acfg->image, token, NULL, NULL, error);
4397 report_loader_error (acfg, error, TRUE, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (error));
4400 * Only generate native-to-managed wrappers for methods which have an
4401 * attribute named MonoPInvokeCallbackAttribute. We search for the attribute by
4402 * name to avoid defining a new assembly to contain it.
4404 cattr = mono_custom_attrs_from_method_checked (method, error);
4405 if (!is_ok (error)) {
4406 char *name = mono_method_get_full_name (method);
4407 report_loader_error (acfg, error, TRUE, "Failed to load custom attributes from method %s due to %s\n", name, mono_error_get_message (error));
4408 g_free (name);
4411 if (cattr) {
4412 for (j = 0; j < cattr->num_attrs; ++j)
4413 if (cattr->attrs [j].ctor && !strcmp (m_class_get_name (cattr->attrs [j].ctor->klass), "MonoPInvokeCallbackAttribute"))
4414 break;
4415 if (j < cattr->num_attrs) {
4416 MonoCustomAttrEntry *e = &cattr->attrs [j];
4417 MonoMethodSignature *sig = mono_method_signature (e->ctor);
4418 const char *p = (const char*)e->data;
4419 const char *named;
4420 int slen, num_named, named_type;
4421 char *n;
4422 MonoType *t;
4423 MonoClass *klass;
4424 char *export_name = NULL;
4425 MonoMethod *wrapper;
4427 /* this cannot be enforced by the C# compiler so we must give the user some warning before aborting */
4428 if (!(method->flags & METHOD_ATTRIBUTE_STATIC)) {
4429 g_warning ("AOT restriction: Method '%s' must be static since it is decorated with [MonoPInvokeCallback]. See https://docs.microsoft.com/xamarin/ios/internals/limitations#reverse-callbacks",
4430 mono_method_full_name (method, TRUE));
4431 exit (1);
4434 g_assert (sig->param_count == 1);
4435 g_assert (sig->params [0]->type == MONO_TYPE_CLASS && !strcmp (m_class_get_name (mono_class_from_mono_type (sig->params [0])), "Type"));
4438 * Decode the cattr manually since we can't create objects
4439 * during aot compilation.
4442 /* Skip prolog */
4443 p += 2;
4445 /* From load_cattr_value () in reflection.c */
4446 slen = mono_metadata_decode_value (p, &p);
4447 n = (char *)g_memdup (p, slen + 1);
4448 n [slen] = 0;
4449 t = mono_reflection_type_from_name_checked (n, acfg->image, error);
4450 g_assert (t);
4451 mono_error_assert_ok (error);
4452 g_free (n);
4454 klass = mono_class_from_mono_type (t);
4455 g_assert (m_class_get_parent (klass) == mono_defaults.multicastdelegate_class);
4457 p += slen;
4459 num_named = read16 (p);
4460 p += 2;
4462 g_assert (num_named < 2);
4463 if (num_named == 1) {
4464 int name_len;
4465 char *name;
4467 /* parse ExportSymbol attribute */
4468 named = p;
4469 named_type = *named;
4470 named += 1;
4471 /* data_type = *named; */
4472 named += 1;
4474 name_len = mono_metadata_decode_blob_size (named, &named);
4475 name = (char *)g_malloc (name_len + 1);
4476 memcpy (name, named, name_len);
4477 name [name_len] = 0;
4478 named += name_len;
4480 g_assert (named_type == 0x54);
4481 g_assert (!strcmp (name, "ExportSymbol"));
4483 /* load_cattr_value (), string case */
4484 g_assert (*named != (char)0xff);
4485 slen = mono_metadata_decode_value (named, &named);
4486 export_name = (char *)g_malloc (slen + 1);
4487 memcpy (export_name, named, slen);
4488 export_name [slen] = 0;
4489 named += slen;
4492 wrapper = mono_marshal_get_managed_wrapper (method, klass, 0, error);
4493 mono_error_assert_ok (error);
4495 add_method (acfg, wrapper);
4496 if (export_name)
4497 g_hash_table_insert (acfg->export_names, wrapper, export_name);
4499 g_free (cattr);
4502 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
4503 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)) {
4504 add_method (acfg, mono_marshal_get_native_wrapper (method, TRUE, TRUE));
4508 /* StructureToPtr/PtrToStructure wrappers */
4509 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
4510 ERROR_DECL (error);
4511 MonoClass *klass;
4513 token = MONO_TOKEN_TYPE_DEF | (i + 1);
4514 klass = mono_class_get_checked (acfg->image, token, error);
4516 if (!klass) {
4517 mono_error_cleanup (error);
4518 continue;
4521 if (m_class_is_valuetype (klass) && !mono_class_is_gtd (klass) && can_marshal_struct (klass) &&
4522 !(m_class_get_nested_in (klass) && strstr (m_class_get_name (m_class_get_nested_in (klass)), "<PrivateImplementationDetails>") == m_class_get_name (m_class_get_nested_in (klass)))) {
4523 add_method (acfg, mono_marshal_get_struct_to_ptr (klass));
4524 add_method (acfg, mono_marshal_get_ptr_to_struct (klass));
4529 static gboolean
4530 has_type_vars (MonoClass *klass)
4532 if ((m_class_get_byval_arg (klass)->type == MONO_TYPE_VAR) || (m_class_get_byval_arg (klass)->type == MONO_TYPE_MVAR))
4533 return TRUE;
4534 if (m_class_get_rank (klass))
4535 return has_type_vars (m_class_get_element_class (klass));
4536 if (mono_class_is_ginst (klass)) {
4537 MonoGenericContext *context = &mono_class_get_generic_class (klass)->context;
4538 if (context->class_inst) {
4539 int i;
4541 for (i = 0; i < context->class_inst->type_argc; ++i)
4542 if (has_type_vars (mono_class_from_mono_type (context->class_inst->type_argv [i])))
4543 return TRUE;
4546 if (mono_class_is_gtd (klass))
4547 return TRUE;
4548 return FALSE;
4551 static gboolean
4552 is_vt_inst (MonoGenericInst *inst)
4554 int i;
4556 for (i = 0; i < inst->type_argc; ++i) {
4557 MonoType *t = inst->type_argv [i];
4558 if (MONO_TYPE_ISSTRUCT (t) || t->type == MONO_TYPE_VALUETYPE)
4559 return TRUE;
4561 return FALSE;
4564 static gboolean
4565 method_has_type_vars (MonoMethod *method)
4567 if (has_type_vars (method->klass))
4568 return TRUE;
4570 if (method->is_inflated) {
4571 MonoGenericContext *context = mono_method_get_context (method);
4572 if (context->method_inst) {
4573 int i;
4575 for (i = 0; i < context->method_inst->type_argc; ++i)
4576 if (has_type_vars (mono_class_from_mono_type (context->method_inst->type_argv [i])))
4577 return TRUE;
4580 return FALSE;
4583 static
4584 gboolean mono_aot_mode_is_full (MonoAotOptions *opts)
4586 return opts->mode == MONO_AOT_MODE_FULL;
4589 static
4590 gboolean mono_aot_mode_is_interp (MonoAotOptions *opts)
4592 return opts->interp;
4595 static
4596 gboolean mono_aot_mode_is_hybrid (MonoAotOptions *opts)
4598 return opts->mode == MONO_AOT_MODE_HYBRID;
4601 static void add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth, const char *ref);
4603 static void
4604 add_generic_class (MonoAotCompile *acfg, MonoClass *klass, gboolean force, const char *ref)
4606 /* This might lead to a huge code blowup so only do it if neccesary */
4607 if (!mono_aot_mode_is_full (&acfg->aot_opts) && !mono_aot_mode_is_hybrid (&acfg->aot_opts) && !force)
4608 return;
4610 add_generic_class_with_depth (acfg, klass, 0, ref);
4613 static gboolean
4614 check_type_depth (MonoType *t, int depth)
4616 int i;
4618 if (depth > 8)
4619 return TRUE;
4621 switch (t->type) {
4622 case MONO_TYPE_GENERICINST: {
4623 MonoGenericClass *gklass = t->data.generic_class;
4624 MonoGenericInst *ginst = gklass->context.class_inst;
4626 if (ginst) {
4627 for (i = 0; i < ginst->type_argc; ++i) {
4628 if (check_type_depth (ginst->type_argv [i], depth + 1))
4629 return TRUE;
4632 break;
4634 default:
4635 break;
4638 return FALSE;
4641 static void
4642 add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method);
4645 * add_generic_class:
4647 * Add all methods of a generic class.
4649 static void
4650 add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth, const char *ref)
4652 MonoMethod *method;
4653 MonoClassField *field;
4654 gpointer iter;
4655 gboolean use_gsharedvt = FALSE;
4657 if (!acfg->ginst_hash)
4658 acfg->ginst_hash = g_hash_table_new (NULL, NULL);
4660 mono_class_init (klass);
4662 if (mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->context.class_inst->is_open)
4663 return;
4665 if (has_type_vars (klass))
4666 return;
4668 if (!mono_class_is_ginst (klass) && !m_class_get_rank (klass))
4669 return;
4671 if (mono_class_has_failure (klass))
4672 return;
4674 if (!acfg->ginst_hash)
4675 acfg->ginst_hash = g_hash_table_new (NULL, NULL);
4677 if (g_hash_table_lookup (acfg->ginst_hash, klass))
4678 return;
4680 if (check_type_depth (m_class_get_byval_arg (klass), 0))
4681 return;
4683 if (acfg->aot_opts.log_generics) {
4684 char *s = mono_type_full_name (m_class_get_byval_arg (klass));
4685 aot_printf (acfg, "%*sAdding generic instance %s [%s].\n", depth, "", s, ref);
4686 g_free (s);
4689 g_hash_table_insert (acfg->ginst_hash, klass, klass);
4692 * Use gsharedvt for generic collections with vtype arguments to avoid code blowup.
4693 * Enable this only for some classes since gsharedvt might not support all methods.
4695 if ((acfg->opts & MONO_OPT_GSHAREDVT) && m_class_get_image (klass) == mono_defaults.corlib && mono_class_is_ginst (klass) && mono_class_get_generic_class (klass)->context.class_inst && is_vt_inst (mono_class_get_generic_class (klass)->context.class_inst) &&
4696 (!strcmp (m_class_get_name (klass), "Dictionary`2") || !strcmp (m_class_get_name (klass), "List`1") || !strcmp (m_class_get_name (klass), "ReadOnlyCollection`1")))
4697 use_gsharedvt = TRUE;
4699 iter = NULL;
4700 while ((method = mono_class_get_methods (klass, &iter))) {
4701 if ((acfg->opts & MONO_OPT_GSHAREDVT) && method->is_inflated && mono_method_get_context (method)->method_inst) {
4703 * This is partial sharing, and we can't handle it yet
4705 continue;
4708 if (mono_method_is_generic_sharable_full (method, FALSE, FALSE, use_gsharedvt)) {
4709 /* Already added */
4710 add_types_from_method_header (acfg, method);
4711 continue;
4714 if (method->is_generic)
4715 /* FIXME: */
4716 continue;
4719 * FIXME: Instances which are referenced by these methods are not added,
4720 * for example Array.Resize<int> for List<int>.Add ().
4722 add_extra_method_with_depth (acfg, method, depth + 1);
4725 iter = NULL;
4726 while ((field = mono_class_get_fields (klass, &iter))) {
4727 if (field->type->type == MONO_TYPE_GENERICINST)
4728 add_generic_class_with_depth (acfg, mono_class_from_mono_type (field->type), depth + 1, "field");
4731 if (m_class_is_delegate (klass)) {
4732 method = mono_get_delegate_invoke (klass);
4734 method = mono_marshal_get_delegate_invoke (method, NULL);
4736 if (acfg->aot_opts.log_generics)
4737 aot_printf (acfg, "%*sAdding method %s.\n", depth, "", mono_method_get_full_name (method));
4739 add_method (acfg, method);
4742 /* Add superclasses */
4743 if (m_class_get_parent (klass))
4744 add_generic_class_with_depth (acfg, m_class_get_parent (klass), depth, "parent");
4746 const char *klass_name = m_class_get_name (klass);
4747 const char *klass_name_space = m_class_get_name_space (klass);
4748 const gboolean in_corlib = m_class_get_image (klass) == mono_defaults.corlib;
4750 * For ICollection<T>, add instances of the helper methods
4751 * in Array, since a T[] could be cast to ICollection<T>.
4753 if (in_corlib && !strcmp (klass_name_space, "System.Collections.Generic") &&
4754 (!strcmp(klass_name, "ICollection`1") || !strcmp (klass_name, "IEnumerable`1") || !strcmp (klass_name, "IList`1") || !strcmp (klass_name, "IEnumerator`1") || !strcmp (klass_name, "IReadOnlyList`1"))) {
4755 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4756 MonoClass *array_class = mono_class_create_bounded_array (tclass, 1, FALSE);
4757 gpointer iter;
4758 char *name_prefix;
4760 if (!strcmp (klass_name, "IEnumerator`1"))
4761 name_prefix = g_strdup_printf ("%s.%s", klass_name_space, "IEnumerable`1");
4762 else
4763 name_prefix = g_strdup_printf ("%s.%s", klass_name_space, klass_name);
4765 /* Add the T[]/InternalEnumerator class */
4766 if (!strcmp (klass_name, "IEnumerable`1") || !strcmp (klass_name, "IEnumerator`1")) {
4767 ERROR_DECL (error);
4768 MonoClass *nclass;
4770 iter = NULL;
4771 while ((nclass = mono_class_get_nested_types (m_class_get_parent (array_class), &iter))) {
4772 if (!strcmp (m_class_get_name (nclass), "InternalEnumerator`1"))
4773 break;
4775 g_assert (nclass);
4776 nclass = mono_class_inflate_generic_class_checked (nclass, mono_generic_class_get_context (mono_class_get_generic_class (klass)), error);
4777 mono_error_assert_ok (error); /* FIXME don't swallow the error */
4778 add_generic_class (acfg, nclass, FALSE, "ICollection<T>");
4781 iter = NULL;
4782 while ((method = mono_class_get_methods (array_class, &iter))) {
4783 if (strstr (method->name, name_prefix)) {
4784 MonoMethod *m = mono_aot_get_array_helper_from_wrapper (method);
4786 add_extra_method_with_depth (acfg, m, depth);
4790 g_free (name_prefix);
4793 /* Add an instance of GenericComparer<T> which is created dynamically by Comparer<T> */
4794 if (in_corlib && !strcmp (klass_name_space, "System.Collections.Generic") && !strcmp (klass_name, "Comparer`1")) {
4795 ERROR_DECL (error);
4796 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4797 MonoClass *icomparable, *gcomparer, *icomparable_inst;
4798 MonoGenericContext ctx;
4799 MonoType *args [16];
4801 memset (&ctx, 0, sizeof (ctx));
4803 icomparable = mono_class_load_from_name (mono_defaults.corlib, "System", "IComparable`1");
4805 args [0] = m_class_get_byval_arg (tclass);
4806 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4808 icomparable_inst = mono_class_inflate_generic_class_checked (icomparable, &ctx, error);
4809 mono_error_assert_ok (error); /* FIXME don't swallow the error */
4811 if (mono_class_is_assignable_from (icomparable_inst, tclass)) {
4812 MonoClass *gcomparer_inst;
4813 gcomparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericComparer`1");
4814 gcomparer_inst = mono_class_inflate_generic_class_checked (gcomparer, &ctx, error);
4815 mono_error_assert_ok (error); /* FIXME don't swallow the error */
4817 add_generic_class (acfg, gcomparer_inst, FALSE, "Comparer<T>");
4821 /* Add an instance of GenericEqualityComparer<T> which is created dynamically by EqualityComparer<T> */
4822 if (in_corlib && !strcmp (klass_name_space, "System.Collections.Generic") && !strcmp (klass_name, "EqualityComparer`1")) {
4823 ERROR_DECL (error);
4824 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4825 MonoClass *iface, *gcomparer, *iface_inst;
4826 MonoGenericContext ctx;
4827 MonoType *args [16];
4829 memset (&ctx, 0, sizeof (ctx));
4831 iface = mono_class_load_from_name (mono_defaults.corlib, "System", "IEquatable`1");
4832 g_assert (iface);
4833 args [0] = m_class_get_byval_arg (tclass);
4834 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4836 iface_inst = mono_class_inflate_generic_class_checked (iface, &ctx, error);
4837 mono_error_assert_ok (error); /* FIXME don't swallow the error */
4839 if (mono_class_is_assignable_from (iface_inst, tclass)) {
4840 MonoClass *gcomparer_inst;
4841 ERROR_DECL (error);
4843 gcomparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "GenericEqualityComparer`1");
4844 gcomparer_inst = mono_class_inflate_generic_class_checked (gcomparer, &ctx, error);
4845 mono_error_assert_ok (error); /* FIXME don't swallow the error */
4846 add_generic_class (acfg, gcomparer_inst, FALSE, "EqualityComparer<T>");
4850 /* Add an instance of EnumComparer<T> which is created dynamically by EqualityComparer<T> for enums */
4851 if (in_corlib && !strcmp (klass_name_space, "System.Collections.Generic") && !strcmp (klass_name, "EqualityComparer`1")) {
4852 MonoClass *enum_comparer;
4853 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4854 MonoGenericContext ctx;
4855 MonoType *args [16];
4857 if (mono_class_is_enum (tclass)) {
4858 MonoClass *enum_comparer_inst;
4859 ERROR_DECL (error);
4861 memset (&ctx, 0, sizeof (ctx));
4862 args [0] = m_class_get_byval_arg (tclass);
4863 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4865 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "EnumEqualityComparer`1");
4866 enum_comparer_inst = mono_class_inflate_generic_class_checked (enum_comparer, &ctx, error);
4867 mono_error_assert_ok (error); /* FIXME don't swallow the error */
4868 add_generic_class (acfg, enum_comparer_inst, FALSE, "EqualityComparer<T>");
4872 /* Add an instance of ObjectComparer<T> which is created dynamically by Comparer<T> for enums */
4873 if (in_corlib && !strcmp (klass_name_space, "System.Collections.Generic") && !strcmp (klass_name, "Comparer`1")) {
4874 MonoClass *comparer;
4875 MonoClass *tclass = mono_class_from_mono_type (mono_class_get_generic_class (klass)->context.class_inst->type_argv [0]);
4876 MonoGenericContext ctx;
4877 MonoType *args [16];
4879 if (mono_class_is_enum (tclass)) {
4880 MonoClass *comparer_inst;
4881 ERROR_DECL (error);
4883 memset (&ctx, 0, sizeof (ctx));
4884 args [0] = m_class_get_byval_arg (tclass);
4885 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4887 comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "ObjectComparer`1");
4888 comparer_inst = mono_class_inflate_generic_class_checked (comparer, &ctx, error);
4889 mono_error_assert_ok (error); /* FIXME don't swallow the error */
4890 add_generic_class (acfg, comparer_inst, FALSE, "Comparer<T>");
4895 static void
4896 add_instances_of (MonoAotCompile *acfg, MonoClass *klass, MonoType **insts, int ninsts, gboolean force)
4898 int i;
4899 MonoGenericContext ctx;
4900 MonoType *args [16];
4902 if (acfg->aot_opts.no_instances)
4903 return;
4905 memset (&ctx, 0, sizeof (ctx));
4907 for (i = 0; i < ninsts; ++i) {
4908 ERROR_DECL (error);
4909 MonoClass *generic_inst;
4910 args [0] = insts [i];
4911 ctx.class_inst = mono_metadata_get_generic_inst (1, args);
4912 generic_inst = mono_class_inflate_generic_class_checked (klass, &ctx, error);
4913 mono_error_assert_ok (error); /* FIXME don't swallow the error */
4914 add_generic_class (acfg, generic_inst, force, "");
4918 static void
4919 add_types_from_method_header (MonoAotCompile *acfg, MonoMethod *method)
4921 ERROR_DECL (error);
4922 MonoMethodHeader *header;
4923 MonoMethodSignature *sig;
4924 int j, depth;
4926 depth = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_depth, method));
4928 sig = mono_method_signature (method);
4930 if (sig) {
4931 for (j = 0; j < sig->param_count; ++j)
4932 if (sig->params [j]->type == MONO_TYPE_GENERICINST)
4933 add_generic_class_with_depth (acfg, mono_class_from_mono_type (sig->params [j]), depth + 1, "arg");
4936 header = mono_method_get_header_checked (method, error);
4938 if (header) {
4939 for (j = 0; j < header->num_locals; ++j)
4940 if (header->locals [j]->type == MONO_TYPE_GENERICINST)
4941 add_generic_class_with_depth (acfg, mono_class_from_mono_type (header->locals [j]), depth + 1, "local");
4942 mono_metadata_free_mh (header);
4943 } else {
4944 mono_error_cleanup (error); /* FIXME report the error */
4950 * add_generic_instances:
4952 * Add instances referenced by the METHODSPEC/TYPESPEC table.
4954 static void
4955 add_generic_instances (MonoAotCompile *acfg)
4957 int i;
4958 guint32 token;
4959 MonoMethod *method;
4960 MonoGenericContext *context;
4962 if (acfg->aot_opts.no_instances)
4963 return;
4965 for (i = 0; i < acfg->image->tables [MONO_TABLE_METHODSPEC].rows; ++i) {
4966 ERROR_DECL (error);
4967 token = MONO_TOKEN_METHOD_SPEC | (i + 1);
4968 method = mono_get_method_checked (acfg->image, token, NULL, NULL, error);
4970 if (!method) {
4971 aot_printerrf (acfg, "Failed to load methodspec 0x%x due to %s.\n", token, mono_error_get_message (error));
4972 aot_printerrf (acfg, "Run with MONO_LOG_LEVEL=debug for more information.\n");
4973 mono_error_cleanup (error);
4974 continue;
4977 if (m_class_get_image (method->klass) != acfg->image)
4978 continue;
4980 context = mono_method_get_context (method);
4982 if (context && ((context->class_inst && context->class_inst->is_open)))
4983 continue;
4986 * For open methods, create an instantiation which can be passed to the JIT.
4987 * FIXME: Handle class_inst as well.
4989 if (context && context->method_inst && context->method_inst->is_open) {
4990 ERROR_DECL (error);
4991 MonoGenericContext shared_context;
4992 MonoGenericInst *inst;
4993 MonoType **type_argv;
4994 int i;
4995 MonoMethod *declaring_method;
4996 gboolean supported = TRUE;
4998 /* Check that the context doesn't contain open constructed types */
4999 if (context->class_inst) {
5000 inst = context->class_inst;
5001 for (i = 0; i < inst->type_argc; ++i) {
5002 if (MONO_TYPE_IS_REFERENCE (inst->type_argv [i]) || inst->type_argv [i]->type == MONO_TYPE_VAR || inst->type_argv [i]->type == MONO_TYPE_MVAR)
5003 continue;
5004 if (mono_class_is_open_constructed_type (inst->type_argv [i]))
5005 supported = FALSE;
5008 if (context->method_inst) {
5009 inst = context->method_inst;
5010 for (i = 0; i < inst->type_argc; ++i) {
5011 if (MONO_TYPE_IS_REFERENCE (inst->type_argv [i]) || inst->type_argv [i]->type == MONO_TYPE_VAR || inst->type_argv [i]->type == MONO_TYPE_MVAR)
5012 continue;
5013 if (mono_class_is_open_constructed_type (inst->type_argv [i]))
5014 supported = FALSE;
5018 if (!supported)
5019 continue;
5021 memset (&shared_context, 0, sizeof (MonoGenericContext));
5023 inst = context->class_inst;
5024 if (inst) {
5025 type_argv = g_new0 (MonoType*, inst->type_argc);
5026 for (i = 0; i < inst->type_argc; ++i) {
5027 if (MONO_TYPE_IS_REFERENCE (inst->type_argv [i]) || inst->type_argv [i]->type == MONO_TYPE_VAR || inst->type_argv [i]->type == MONO_TYPE_MVAR)
5028 type_argv [i] = mono_get_object_type ();
5029 else
5030 type_argv [i] = inst->type_argv [i];
5033 shared_context.class_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
5034 g_free (type_argv);
5037 inst = context->method_inst;
5038 if (inst) {
5039 type_argv = g_new0 (MonoType*, inst->type_argc);
5040 for (i = 0; i < inst->type_argc; ++i) {
5041 if (MONO_TYPE_IS_REFERENCE (inst->type_argv [i]) || inst->type_argv [i]->type == MONO_TYPE_VAR || inst->type_argv [i]->type == MONO_TYPE_MVAR)
5042 type_argv [i] = mono_get_object_type ();
5043 else
5044 type_argv [i] = inst->type_argv [i];
5047 shared_context.method_inst = mono_metadata_get_generic_inst (inst->type_argc, type_argv);
5048 g_free (type_argv);
5051 if (method->is_generic || mono_class_is_gtd (method->klass))
5052 declaring_method = method;
5053 else
5054 declaring_method = mono_method_get_declaring_generic_method (method);
5056 method = mono_class_inflate_generic_method_checked (declaring_method, &shared_context, error);
5057 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
5061 * If the method is fully sharable, it was already added in place of its
5062 * generic definition.
5064 if (mono_method_is_generic_sharable_full (method, FALSE, FALSE, FALSE))
5065 continue;
5068 * FIXME: Partially shared methods are not shared here, so we end up with
5069 * many identical methods.
5071 add_extra_method (acfg, method);
5074 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPESPEC].rows; ++i) {
5075 ERROR_DECL (error);
5076 MonoClass *klass;
5078 token = MONO_TOKEN_TYPE_SPEC | (i + 1);
5080 klass = mono_class_get_checked (acfg->image, token, error);
5081 if (!klass || m_class_get_rank (klass)) {
5082 mono_error_cleanup (error);
5083 continue;
5086 add_generic_class (acfg, klass, FALSE, "typespec");
5089 /* Add types of args/locals */
5090 for (i = 0; i < acfg->methods->len; ++i) {
5091 method = (MonoMethod *)g_ptr_array_index (acfg->methods, i);
5092 add_types_from_method_header (acfg, method);
5095 if (acfg->image == mono_defaults.corlib) {
5096 MonoClass *klass;
5097 MonoType *insts [256];
5098 int ninsts = 0;
5100 MonoType *byte_type = m_class_get_byval_arg (mono_defaults.byte_class);
5101 MonoType *sbyte_type = m_class_get_byval_arg (mono_defaults.sbyte_class);
5102 MonoType *int16_type = m_class_get_byval_arg (mono_defaults.int16_class);
5103 MonoType *uint16_type = m_class_get_byval_arg (mono_defaults.uint16_class);
5104 MonoType *int32_type = mono_get_int32_type ();
5105 MonoType *uint32_type = m_class_get_byval_arg (mono_defaults.uint32_class);
5106 MonoType *int64_type = m_class_get_byval_arg (mono_defaults.int64_class);
5107 MonoType *uint64_type = m_class_get_byval_arg (mono_defaults.uint64_class);
5108 MonoType *object_type = mono_get_object_type ();
5110 insts [ninsts ++] = byte_type;
5111 insts [ninsts ++] = sbyte_type;
5112 insts [ninsts ++] = int16_type;
5113 insts [ninsts ++] = uint16_type;
5114 insts [ninsts ++] = int32_type;
5115 insts [ninsts ++] = uint32_type;
5116 insts [ninsts ++] = int64_type;
5117 insts [ninsts ++] = uint64_type;
5118 insts [ninsts ++] = m_class_get_byval_arg (mono_defaults.single_class);
5119 insts [ninsts ++] = m_class_get_byval_arg (mono_defaults.double_class);
5120 insts [ninsts ++] = m_class_get_byval_arg (mono_defaults.char_class);
5121 insts [ninsts ++] = m_class_get_byval_arg (mono_defaults.boolean_class);
5123 /* Add GenericComparer<T> instances for primitive types for Enum.ToString () */
5124 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "GenericComparer`1");
5125 if (klass)
5126 add_instances_of (acfg, klass, insts, ninsts, TRUE);
5127 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "GenericEqualityComparer`1");
5128 if (klass)
5129 add_instances_of (acfg, klass, insts, ninsts, TRUE);
5131 /* Add instances of EnumEqualityComparer which are created by EqualityComparer<T> for enums */
5133 MonoClass *enum_comparer;
5134 MonoType *insts [16];
5135 int ninsts;
5137 ninsts = 0;
5138 insts [ninsts ++] = int32_type;
5139 insts [ninsts ++] = uint32_type;
5140 insts [ninsts ++] = uint16_type;
5141 insts [ninsts ++] = byte_type;
5142 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "EnumEqualityComparer`1");
5143 add_instances_of (acfg, enum_comparer, insts, ninsts, FALSE);
5145 ninsts = 0;
5146 insts [ninsts ++] = int16_type;
5147 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "ShortEnumEqualityComparer`1");
5148 add_instances_of (acfg, enum_comparer, insts, ninsts, FALSE);
5150 ninsts = 0;
5151 insts [ninsts ++] = sbyte_type;
5152 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "SByteEnumEqualityComparer`1");
5153 add_instances_of (acfg, enum_comparer, insts, ninsts, FALSE);
5155 enum_comparer = mono_class_load_from_name (mono_defaults.corlib, "System.Collections.Generic", "LongEnumEqualityComparer`1");
5156 ninsts = 0;
5157 insts [ninsts ++] = int64_type;
5158 insts [ninsts ++] = uint64_type;
5159 add_instances_of (acfg, enum_comparer, insts, ninsts, FALSE);
5162 /* Add instances of the array generic interfaces for primitive types */
5163 /* This will add instances of the InternalArray_ helper methods in Array too */
5164 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "ICollection`1");
5165 if (klass)
5166 add_instances_of (acfg, klass, insts, ninsts, TRUE);
5168 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "IList`1");
5169 if (klass)
5170 add_instances_of (acfg, klass, insts, ninsts, TRUE);
5172 klass = mono_class_try_load_from_name (acfg->image, "System.Collections.Generic", "IEnumerable`1");
5173 if (klass)
5174 add_instances_of (acfg, klass, insts, ninsts, TRUE);
5177 * Add a managed-to-native wrapper of Array.GetGenericValueImpl<object>, which is
5178 * used for all instances of GetGenericValueImpl by the AOT runtime.
5181 MonoGenericContext ctx;
5182 MonoType *args [16];
5183 MonoMethod *get_method;
5184 MonoClass *array_klass = m_class_get_parent (mono_class_create_array (mono_defaults.object_class, 1));
5186 get_method = mono_class_get_method_from_name (array_klass, "GetGenericValueImpl", 2);
5188 if (get_method) {
5189 ERROR_DECL (error);
5190 memset (&ctx, 0, sizeof (ctx));
5191 args [0] = object_type;
5192 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
5193 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (get_method, &ctx, error), TRUE, TRUE));
5194 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
5198 /* Same for CompareExchange<T>/Exchange<T> */
5200 MonoGenericContext ctx;
5201 MonoType *args [16];
5202 MonoMethod *m;
5203 MonoClass *interlocked_klass = mono_class_load_from_name (mono_defaults.corlib, "System.Threading", "Interlocked");
5204 gpointer iter = NULL;
5206 while ((m = mono_class_get_methods (interlocked_klass, &iter))) {
5207 if ((!strcmp (m->name, "CompareExchange") || !strcmp (m->name, "Exchange")) && m->is_generic) {
5208 ERROR_DECL (error);
5209 memset (&ctx, 0, sizeof (ctx));
5210 args [0] = object_type;
5211 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
5212 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m, &ctx, error), TRUE, TRUE));
5213 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
5218 /* Same for Volatile.Read/Write<T> */
5220 MonoGenericContext ctx;
5221 MonoType *args [16];
5222 MonoMethod *m;
5223 MonoClass *volatile_klass = mono_class_try_load_from_name (mono_defaults.corlib, "System.Threading", "Volatile");
5224 gpointer iter = NULL;
5226 if (volatile_klass) {
5227 while ((m = mono_class_get_methods (volatile_klass, &iter))) {
5228 if ((!strcmp (m->name, "Read") || !strcmp (m->name, "Write")) && m->is_generic) {
5229 ERROR_DECL (error);
5230 memset (&ctx, 0, sizeof (ctx));
5231 args [0] = object_type;
5232 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
5233 add_extra_method (acfg, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m, &ctx, error), TRUE, TRUE));
5234 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
5240 /* object[] accessor wrappers. */
5241 for (i = 1; i < 4; ++i) {
5242 MonoClass *obj_array_class = mono_class_create_array (mono_defaults.object_class, i);
5243 MonoMethod *m;
5245 m = mono_class_get_method_from_name (obj_array_class, "Get", i);
5246 g_assert (m);
5248 m = mono_marshal_get_array_accessor_wrapper (m);
5249 add_extra_method (acfg, m);
5251 m = mono_class_get_method_from_name (obj_array_class, "Address", i);
5252 g_assert (m);
5254 m = mono_marshal_get_array_accessor_wrapper (m);
5255 add_extra_method (acfg, m);
5257 m = mono_class_get_method_from_name (obj_array_class, "Set", i + 1);
5258 g_assert (m);
5260 m = mono_marshal_get_array_accessor_wrapper (m);
5261 add_extra_method (acfg, m);
5267 * is_direct_callable:
5269 * Return whenever the method identified by JI is directly callable without
5270 * going through the PLT.
5272 static gboolean
5273 is_direct_callable (MonoAotCompile *acfg, MonoMethod *method, MonoJumpInfo *patch_info)
5275 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (m_class_get_image (patch_info->data.method->klass) == acfg->image)) {
5276 MonoCompile *callee_cfg = (MonoCompile *)g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
5277 if (callee_cfg) {
5278 gboolean direct_callable = TRUE;
5280 if (direct_callable && (acfg->aot_opts.dedup || acfg->aot_opts.dedup_include) && mono_aot_can_dedup (patch_info->data.method))
5281 direct_callable = FALSE;
5283 if (direct_callable && !(!callee_cfg->has_got_slots && mono_class_is_before_field_init (callee_cfg->method->klass)))
5284 direct_callable = FALSE;
5285 if ((callee_cfg->method->iflags & METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED) && (!method || method->wrapper_type != MONO_WRAPPER_SYNCHRONIZED))
5286 // FIXME: Maybe call the wrapper directly ?
5287 direct_callable = FALSE;
5289 if (acfg->aot_opts.soft_debug || acfg->aot_opts.no_direct_calls) {
5290 /* Disable this so all calls go through load_method (), see the
5291 * mini_get_debug_options ()->load_aot_jit_info_eagerly = TRUE; line in
5292 * mono_debugger_agent_init ().
5294 direct_callable = FALSE;
5297 if (callee_cfg->method->wrapper_type == MONO_WRAPPER_ALLOC)
5298 /* sgen does some initialization when the allocator method is created */
5299 direct_callable = FALSE;
5300 if (callee_cfg->method->wrapper_type == MONO_WRAPPER_WRITE_BARRIER)
5301 /* we don't know at compile time whether sgen is concurrent or not */
5302 direct_callable = FALSE;
5304 if (direct_callable)
5305 return TRUE;
5307 } else if ((patch_info->type == MONO_PATCH_INFO_ICALL_ADDR_CALL && patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL)) {
5308 if (acfg->aot_opts.direct_pinvoke)
5309 return TRUE;
5310 } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
5311 if (acfg->aot_opts.direct_icalls)
5312 return TRUE;
5313 return FALSE;
5316 return FALSE;
5319 #ifdef MONO_ARCH_AOT_SUPPORTED
5320 static const char *
5321 get_pinvoke_import (MonoAotCompile *acfg, MonoMethod *method)
5323 MonoImage *image = m_class_get_image (method->klass);
5324 MonoMethodPInvoke *piinfo = (MonoMethodPInvoke *) method;
5325 MonoTableInfo *tables = image->tables;
5326 MonoTableInfo *im = &tables [MONO_TABLE_IMPLMAP];
5327 MonoTableInfo *mr = &tables [MONO_TABLE_MODULEREF];
5328 guint32 im_cols [MONO_IMPLMAP_SIZE];
5329 char *import;
5331 import = (char *)g_hash_table_lookup (acfg->method_to_pinvoke_import, method);
5332 if (import != NULL)
5333 return import;
5335 if (!piinfo->implmap_idx || piinfo->implmap_idx > im->rows)
5336 return NULL;
5338 mono_metadata_decode_row (im, piinfo->implmap_idx - 1, im_cols, MONO_IMPLMAP_SIZE);
5340 if (!im_cols [MONO_IMPLMAP_SCOPE] || im_cols [MONO_IMPLMAP_SCOPE] > mr->rows)
5341 return NULL;
5343 import = g_strdup_printf ("%s", mono_metadata_string_heap (image, im_cols [MONO_IMPLMAP_NAME]));
5345 g_hash_table_insert (acfg->method_to_pinvoke_import, method, import);
5347 return import;
5349 #else
5350 static const char *
5351 get_pinvoke_import (MonoAotCompile *acfg, MonoMethod *method)
5353 return NULL;
5355 #endif
5357 static gint
5358 compare_lne (MonoDebugLineNumberEntry *a, MonoDebugLineNumberEntry *b)
5360 if (a->native_offset == b->native_offset)
5361 return a->il_offset - b->il_offset;
5362 else
5363 return a->native_offset - b->native_offset;
5367 * compute_line_numbers:
5369 * Returns a sparse array of size CODE_SIZE containing MonoDebugSourceLocation* entries for the native offsets which have a corresponding line number
5370 * entry.
5372 static MonoDebugSourceLocation**
5373 compute_line_numbers (MonoMethod *method, int code_size, MonoDebugMethodJitInfo *debug_info)
5375 MonoDebugMethodInfo *minfo;
5376 MonoDebugLineNumberEntry *ln_array;
5377 MonoDebugSourceLocation *loc;
5378 int i, prev_line, prev_il_offset;
5379 int *native_to_il_offset = NULL;
5380 MonoDebugSourceLocation **res;
5381 gboolean first;
5383 minfo = mono_debug_lookup_method (method);
5384 if (!minfo)
5385 return NULL;
5386 // FIXME: This seems to happen when two methods have the same cfg->method_to_register
5387 if (debug_info->code_size != code_size)
5388 return NULL;
5390 g_assert (code_size);
5392 /* Compute the native->IL offset mapping */
5394 ln_array = g_new0 (MonoDebugLineNumberEntry, debug_info->num_line_numbers);
5395 memcpy (ln_array, debug_info->line_numbers, debug_info->num_line_numbers * sizeof (MonoDebugLineNumberEntry));
5397 qsort (ln_array, debug_info->num_line_numbers, sizeof (MonoDebugLineNumberEntry), (int (*)(const void *, const void *))compare_lne);
5399 native_to_il_offset = g_new0 (int, code_size + 1);
5401 for (i = 0; i < debug_info->num_line_numbers; ++i) {
5402 int j;
5403 MonoDebugLineNumberEntry *lne = &ln_array [i];
5405 if (i == 0) {
5406 for (j = 0; j < lne->native_offset; ++j)
5407 native_to_il_offset [j] = -1;
5410 if (i < debug_info->num_line_numbers - 1) {
5411 MonoDebugLineNumberEntry *lne_next = &ln_array [i + 1];
5413 for (j = lne->native_offset; j < lne_next->native_offset; ++j)
5414 native_to_il_offset [j] = lne->il_offset;
5415 } else {
5416 for (j = lne->native_offset; j < code_size; ++j)
5417 native_to_il_offset [j] = lne->il_offset;
5420 g_free (ln_array);
5422 /* Compute the native->line number mapping */
5423 res = g_new0 (MonoDebugSourceLocation*, code_size);
5424 prev_il_offset = -1;
5425 prev_line = -1;
5426 first = TRUE;
5427 for (i = 0; i < code_size; ++i) {
5428 int il_offset = native_to_il_offset [i];
5430 if (il_offset == -1 || il_offset == prev_il_offset)
5431 continue;
5432 prev_il_offset = il_offset;
5433 loc = mono_debug_method_lookup_location (minfo, il_offset);
5434 if (!(loc && loc->source_file))
5435 continue;
5436 if (loc->row == prev_line) {
5437 mono_debug_free_source_location (loc);
5438 continue;
5440 prev_line = loc->row;
5441 //printf ("D: %s:%d il=%x native=%x\n", loc->source_file, loc->row, il_offset, i);
5442 if (first)
5443 /* This will cover the prolog too */
5444 res [0] = loc;
5445 else
5446 res [i] = loc;
5447 first = FALSE;
5449 return res;
5452 static int
5453 get_file_index (MonoAotCompile *acfg, const char *source_file)
5455 int findex;
5457 // FIXME: Free these
5458 if (!acfg->dwarf_ln_filenames)
5459 acfg->dwarf_ln_filenames = g_hash_table_new (g_str_hash, g_str_equal);
5460 findex = GPOINTER_TO_INT (g_hash_table_lookup (acfg->dwarf_ln_filenames, source_file));
5461 if (!findex) {
5462 findex = g_hash_table_size (acfg->dwarf_ln_filenames) + 1;
5463 g_hash_table_insert (acfg->dwarf_ln_filenames, g_strdup (source_file), GINT_TO_POINTER (findex));
5464 emit_unset_mode (acfg);
5465 fprintf (acfg->fp, ".file %d \"%s\"\n", findex, mono_dwarf_escape_path (source_file));
5467 return findex;
5470 #ifdef TARGET_ARM64
5471 #define INST_LEN 4
5472 #else
5473 #define INST_LEN 1
5474 #endif
5477 * emit_and_reloc_code:
5479 * Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
5480 * is true, calls are made through the GOT too. This is used for emitting trampolines
5481 * in full-aot mode, since calls made from trampolines couldn't go through the PLT,
5482 * since trampolines are needed to make PTL work.
5484 static void
5485 emit_and_reloc_code (MonoAotCompile *acfg, MonoMethod *method, guint8 *code, guint32 code_len, MonoJumpInfo *relocs, gboolean got_only, MonoDebugMethodJitInfo *debug_info)
5487 int i, pindex, start_index;
5488 GPtrArray *patches;
5489 MonoJumpInfo *patch_info;
5490 MonoDebugSourceLocation **locs = NULL;
5491 gboolean skip, prologue_end = FALSE;
5492 #ifdef MONO_ARCH_AOT_SUPPORTED
5493 gboolean direct_call, external_call;
5494 guint32 got_slot;
5495 const char *direct_call_target = 0;
5496 const char *direct_pinvoke;
5497 #endif
5499 if (acfg->gas_line_numbers && method && debug_info) {
5500 locs = compute_line_numbers (method, code_len, debug_info);
5501 if (!locs) {
5502 int findex = get_file_index (acfg, "<unknown>");
5503 emit_unset_mode (acfg);
5504 fprintf (acfg->fp, ".loc %d %d 0\n", findex, 1);
5508 /* Collect and sort relocations */
5509 patches = g_ptr_array_new ();
5510 for (patch_info = relocs; patch_info; patch_info = patch_info->next)
5511 g_ptr_array_add (patches, patch_info);
5512 g_ptr_array_sort (patches, compare_patches);
5514 start_index = 0;
5515 for (i = 0; i < code_len; i += INST_LEN) {
5516 patch_info = NULL;
5517 for (pindex = start_index; pindex < patches->len; ++pindex) {
5518 patch_info = (MonoJumpInfo *)g_ptr_array_index (patches, pindex);
5519 if (patch_info->ip.i >= i)
5520 break;
5523 if (locs && locs [i]) {
5524 MonoDebugSourceLocation *loc = locs [i];
5525 int findex;
5526 const char *options;
5528 findex = get_file_index (acfg, loc->source_file);
5529 emit_unset_mode (acfg);
5530 if (!prologue_end)
5531 options = " prologue_end";
5532 else
5533 options = "";
5534 prologue_end = TRUE;
5535 fprintf (acfg->fp, ".loc %d %d 0%s\n", findex, loc->row, options);
5536 mono_debug_free_source_location (loc);
5539 skip = FALSE;
5540 #ifdef MONO_ARCH_AOT_SUPPORTED
5541 if (patch_info && (patch_info->ip.i == i) && (pindex < patches->len)) {
5542 start_index = pindex;
5544 switch (patch_info->type) {
5545 case MONO_PATCH_INFO_NONE:
5546 break;
5547 case MONO_PATCH_INFO_GOT_OFFSET: {
5548 int code_size;
5550 arch_emit_got_offset (acfg, code + i, &code_size);
5551 i += code_size - INST_LEN;
5552 skip = TRUE;
5553 patch_info->type = MONO_PATCH_INFO_NONE;
5554 break;
5556 case MONO_PATCH_INFO_OBJC_SELECTOR_REF: {
5557 int code_size, index;
5558 char *selector = (char *)patch_info->data.target;
5560 if (!acfg->objc_selector_to_index)
5561 acfg->objc_selector_to_index = g_hash_table_new (g_str_hash, g_str_equal);
5562 if (!acfg->objc_selectors)
5563 acfg->objc_selectors = g_ptr_array_new ();
5564 index = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->objc_selector_to_index, selector));
5565 if (index)
5566 index --;
5567 else {
5568 index = acfg->objc_selector_index;
5569 g_ptr_array_add (acfg->objc_selectors, (void*)patch_info->data.target);
5570 g_hash_table_insert (acfg->objc_selector_to_index, selector, GUINT_TO_POINTER (index + 1));
5571 acfg->objc_selector_index ++;
5574 arch_emit_objc_selector_ref (acfg, code + i, index, &code_size);
5575 i += code_size - INST_LEN;
5576 skip = TRUE;
5577 patch_info->type = MONO_PATCH_INFO_NONE;
5578 break;
5580 default: {
5582 * If this patch is a call, try emitting a direct call instead of
5583 * through a PLT entry. This is possible if the called method is in
5584 * the same assembly and requires no initialization.
5586 direct_call = FALSE;
5587 external_call = FALSE;
5588 if ((patch_info->type == MONO_PATCH_INFO_METHOD) && (m_class_get_image (patch_info->data.method->klass) == acfg->image)) {
5589 if (!got_only && is_direct_callable (acfg, method, patch_info)) {
5590 MonoCompile *callee_cfg = (MonoCompile *)g_hash_table_lookup (acfg->method_to_cfg, patch_info->data.method);
5592 // Don't compile inflated methods if we're doing dedup
5593 if (acfg->aot_opts.dedup && !mono_aot_can_dedup (patch_info->data.method)) {
5594 char *name = mono_aot_get_mangled_method_name (patch_info->data.method);
5595 mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT, "DIRECT CALL: %s by %s", name, method ? mono_method_full_name (method, TRUE) : "");
5596 g_free (name);
5598 direct_call = TRUE;
5599 direct_call_target = callee_cfg->asm_symbol;
5600 patch_info->type = MONO_PATCH_INFO_NONE;
5601 acfg->stats.direct_calls ++;
5605 acfg->stats.all_calls ++;
5606 } else if (patch_info->type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
5607 if (!got_only && is_direct_callable (acfg, method, patch_info)) {
5608 if (!(patch_info->data.method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
5609 direct_pinvoke = mono_lookup_icall_symbol (patch_info->data.method);
5610 else
5611 direct_pinvoke = get_pinvoke_import (acfg, patch_info->data.method);
5612 if (direct_pinvoke) {
5613 direct_call = TRUE;
5614 g_assert (strlen (direct_pinvoke) < 1000);
5615 direct_call_target = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, direct_pinvoke);
5618 } else if (patch_info->type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
5619 const char *sym = mono_lookup_jit_icall_symbol (patch_info->data.name);
5620 if (!got_only && sym && acfg->aot_opts.direct_icalls) {
5621 /* Call to a C function implementing a jit icall */
5622 direct_call = TRUE;
5623 external_call = TRUE;
5624 g_assert (strlen (sym) < 1000);
5625 direct_call_target = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, sym);
5627 } else if (patch_info->type == MONO_PATCH_INFO_INTERNAL_METHOD) {
5628 MonoJitICallInfo *info = mono_find_jit_icall_by_name (patch_info->data.name);
5629 const char *sym = mono_lookup_jit_icall_symbol (patch_info->data.name);
5630 if (!got_only && sym && acfg->aot_opts.direct_icalls && info->func == info->wrapper) {
5631 /* Call to a jit icall without a wrapper */
5632 direct_call = TRUE;
5633 external_call = TRUE;
5634 g_assert (strlen (sym) < 1000);
5635 direct_call_target = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, sym);
5639 if (direct_call) {
5640 patch_info->type = MONO_PATCH_INFO_NONE;
5641 acfg->stats.direct_calls ++;
5644 if (!got_only && !direct_call) {
5645 MonoPltEntry *plt_entry = get_plt_entry (acfg, patch_info);
5646 if (plt_entry) {
5647 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
5648 direct_call = TRUE;
5649 direct_call_target = plt_entry->symbol;
5651 /* Nullify the patch */
5652 patch_info->type = MONO_PATCH_INFO_NONE;
5653 plt_entry->jit_used = TRUE;
5657 if (direct_call) {
5658 int call_size;
5660 arch_emit_direct_call (acfg, direct_call_target, external_call, FALSE, patch_info, &call_size);
5661 i += call_size - INST_LEN;
5662 } else {
5663 int code_size;
5665 got_slot = get_got_offset (acfg, FALSE, patch_info);
5667 arch_emit_got_access (acfg, acfg->got_symbol, code + i, got_slot, &code_size);
5668 i += code_size - INST_LEN;
5670 skip = TRUE;
5674 #endif /* MONO_ARCH_AOT_SUPPORTED */
5676 if (!skip) {
5677 /* Find next patch */
5678 patch_info = NULL;
5679 for (pindex = start_index; pindex < patches->len; ++pindex) {
5680 patch_info = (MonoJumpInfo *)g_ptr_array_index (patches, pindex);
5681 if (patch_info->ip.i >= i)
5682 break;
5685 /* Try to emit multiple bytes at once */
5686 if (pindex < patches->len && patch_info->ip.i > i) {
5687 int limit;
5689 for (limit = i + INST_LEN; limit < patch_info->ip.i; limit += INST_LEN) {
5690 if (locs && locs [limit])
5691 break;
5694 emit_code_bytes (acfg, code + i, limit - i);
5695 i = limit - INST_LEN;
5696 } else {
5697 emit_code_bytes (acfg, code + i, INST_LEN);
5702 g_ptr_array_free (patches, TRUE);
5703 g_free (locs);
5707 * sanitize_symbol:
5709 * Return a modified version of S which only includes characters permissible in symbols.
5711 static char*
5712 sanitize_symbol (MonoAotCompile *acfg, char *s)
5714 gboolean process = FALSE;
5715 int i, len;
5716 GString *gs;
5717 char *res;
5719 if (!s)
5720 return s;
5722 len = strlen (s);
5723 for (i = 0; i < len; ++i)
5724 if (!(s [i] <= 0x7f && (isalnum (s [i]) || s [i] == '_')))
5725 process = TRUE;
5726 if (!process)
5727 return s;
5729 gs = g_string_sized_new (len);
5730 for (i = 0; i < len; ++i) {
5731 guint8 c = s [i];
5732 if (c <= 0x7f && (isalnum (c) || c == '_')) {
5733 g_string_append_c (gs, c);
5734 } else if (c > 0x7f) {
5735 /* multi-byte utf8 */
5736 g_string_append_printf (gs, "_0x%x", c);
5737 i ++;
5738 c = s [i];
5739 while (c >> 6 == 0x2) {
5740 g_string_append_printf (gs, "%x", c);
5741 i ++;
5742 c = s [i];
5744 g_string_append_printf (gs, "_");
5745 i --;
5746 } else {
5747 g_string_append_c (gs, '_');
5751 res = mono_mempool_strdup (acfg->mempool, gs->str);
5752 g_string_free (gs, TRUE);
5753 return res;
5756 static char*
5757 get_debug_sym (MonoMethod *method, const char *prefix, GHashTable *cache)
5759 char *name1, *name2, *cached;
5760 int i, j, len, count;
5761 MonoMethod *cached_method;
5763 name1 = mono_method_full_name (method, TRUE);
5765 #ifdef TARGET_MACH
5766 // This is so that we don't accidentally create a local symbol (which starts with 'L')
5767 if ((!prefix || !*prefix) && name1 [0] == 'L')
5768 prefix = "_";
5769 #endif
5771 #if defined(TARGET_WIN32) && defined(TARGET_X86)
5772 char adjustedPrefix [MAX_SYMBOL_SIZE];
5773 prefix = mangle_symbol (prefix, adjustedPrefix, G_N_ELEMENTS (adjustedPrefix));
5774 #endif
5776 len = strlen (name1);
5777 name2 = (char *)malloc (strlen (prefix) + len + 16);
5778 memcpy (name2, prefix, strlen (prefix));
5779 j = strlen (prefix);
5780 for (i = 0; i < len; ++i) {
5781 if (i == 0 && name1 [0] >= '0' && name1 [0] <= '9') {
5782 name2 [j ++] = '_';
5783 } else if (isalnum (name1 [i])) {
5784 name2 [j ++] = name1 [i];
5785 } else if (name1 [i] == ' ' && name1 [i + 1] == '(' && name1 [i + 2] == ')') {
5786 i += 2;
5787 } else if (name1 [i] == ',' && name1 [i + 1] == ' ') {
5788 name2 [j ++] = '_';
5789 i++;
5790 } else if (name1 [i] == '(' || name1 [i] == ')' || name1 [i] == '>') {
5791 } else
5792 name2 [j ++] = '_';
5794 name2 [j] = '\0';
5796 g_free (name1);
5798 count = 0;
5799 while (TRUE) {
5800 cached_method = (MonoMethod *)g_hash_table_lookup (cache, name2);
5801 if (!(cached_method && cached_method != method))
5802 break;
5803 sprintf (name2 + j, "_%d", count);
5804 count ++;
5807 cached = g_strdup (name2);
5808 g_hash_table_insert (cache, cached, method);
5810 return name2;
5813 static void
5814 emit_method_code (MonoAotCompile *acfg, MonoCompile *cfg)
5816 MonoMethod *method;
5817 int method_index;
5818 guint8 *code;
5819 char *debug_sym = NULL;
5820 char *symbol = NULL;
5821 int func_alignment = AOT_FUNC_ALIGNMENT;
5822 char *export_name;
5824 g_assert (!ignore_cfg (cfg));
5826 method = cfg->orig_method;
5827 code = cfg->native_code;
5829 method_index = get_method_index (acfg, method);
5830 symbol = g_strdup_printf ("%sme_%x", acfg->temp_prefix, method_index);
5832 /* Make the labels local */
5833 emit_section_change (acfg, ".text", 0);
5834 emit_alignment_code (acfg, func_alignment);
5836 if (acfg->global_symbols && acfg->need_no_dead_strip)
5837 fprintf (acfg->fp, " .no_dead_strip %s\n", cfg->asm_symbol);
5839 emit_label (acfg, cfg->asm_symbol);
5841 if (acfg->aot_opts.write_symbols && !acfg->global_symbols && !acfg->llvm) {
5843 * Write a C style symbol for every method, this has two uses:
5844 * - it works on platforms where the dwarf debugging info is not
5845 * yet supported.
5846 * - it allows the setting of breakpoints of aot-ed methods.
5849 // Comment out to force dedup to link these symbols and forbid compiling
5850 // in duplicated code. This is an "assert when linking if broken" trick.
5851 /*if (mono_aot_can_dedup (method) && (acfg->aot_opts.dedup || acfg->aot_opts.dedup_include))*/
5852 /*debug_sym = mono_aot_get_mangled_method_name (method);*/
5853 /*else*/
5854 debug_sym = get_debug_sym (method, "", acfg->method_label_hash);
5856 cfg->asm_debug_symbol = g_strdup (debug_sym);
5858 if (acfg->need_no_dead_strip)
5859 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
5861 // Comment out to force dedup to link these symbols and forbid compiling
5862 // in duplicated code. This is an "assert when linking if broken" trick.
5863 /*if (mono_aot_can_dedup (method) && (acfg->aot_opts.dedup || acfg->aot_opts.dedup_include))*/
5864 /*emit_global_inner (acfg, debug_sym, TRUE);*/
5865 /*else*/
5866 emit_local_symbol (acfg, debug_sym, symbol, TRUE);
5868 emit_label (acfg, debug_sym);
5871 export_name = (char *)g_hash_table_lookup (acfg->export_names, method);
5872 if (export_name) {
5873 /* Emit a global symbol for the method */
5874 emit_global_inner (acfg, export_name, TRUE);
5875 emit_label (acfg, export_name);
5878 if (cfg->verbose_level > 0 && !ignore_cfg (cfg))
5879 g_print ("Method %s emitted as %s\n", mono_method_get_full_name (method), cfg->asm_symbol);
5881 acfg->stats.code_size += cfg->code_len;
5883 acfg->cfgs [method_index]->got_offset = acfg->got_offset;
5885 emit_and_reloc_code (acfg, method, code, cfg->code_len, cfg->patch_info, FALSE, mono_debug_find_method (cfg->jit_info->d.method, mono_domain_get ()));
5887 emit_line (acfg);
5889 if (acfg->aot_opts.write_symbols) {
5890 if (debug_sym)
5891 emit_symbol_size (acfg, debug_sym, ".");
5892 else
5893 emit_symbol_size (acfg, cfg->asm_symbol, ".");
5894 g_free (debug_sym);
5897 emit_label (acfg, symbol);
5899 arch_emit_unwind_info_sections (acfg, cfg->asm_symbol, symbol, cfg->unwind_ops);
5901 g_free (symbol);
5905 * encode_patch:
5907 * Encode PATCH_INFO into its disk representation.
5909 static void
5910 encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint8 **endbuf)
5912 guint8 *p = buf;
5914 switch (patch_info->type) {
5915 case MONO_PATCH_INFO_NONE:
5916 break;
5917 case MONO_PATCH_INFO_IMAGE:
5918 encode_value (get_image_index (acfg, patch_info->data.image), p, &p);
5919 break;
5920 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
5921 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
5922 case MONO_PATCH_INFO_GC_NURSERY_START:
5923 case MONO_PATCH_INFO_GC_NURSERY_BITS:
5924 break;
5925 case MONO_PATCH_INFO_CASTCLASS_CACHE:
5926 encode_value (patch_info->data.index, p, &p);
5927 break;
5928 case MONO_PATCH_INFO_METHOD_REL:
5929 encode_value ((gint)patch_info->data.offset, p, &p);
5930 break;
5931 case MONO_PATCH_INFO_SWITCH: {
5932 gpointer *table = (gpointer *)patch_info->data.table->table;
5933 int k;
5935 encode_value (patch_info->data.table->table_size, p, &p);
5936 for (k = 0; k < patch_info->data.table->table_size; k++)
5937 encode_value ((int)(gssize)table [k], p, &p);
5938 break;
5940 case MONO_PATCH_INFO_METHODCONST:
5941 case MONO_PATCH_INFO_METHOD:
5942 case MONO_PATCH_INFO_METHOD_JUMP:
5943 case MONO_PATCH_INFO_ICALL_ADDR:
5944 case MONO_PATCH_INFO_ICALL_ADDR_CALL:
5945 case MONO_PATCH_INFO_METHOD_RGCTX:
5946 case MONO_PATCH_INFO_METHOD_CODE_SLOT:
5947 encode_method_ref (acfg, patch_info->data.method, p, &p);
5948 break;
5949 case MONO_PATCH_INFO_AOT_JIT_INFO:
5950 case MONO_PATCH_INFO_GET_TLS_TRAMP:
5951 case MONO_PATCH_INFO_SET_TLS_TRAMP:
5952 encode_value (patch_info->data.index, p, &p);
5953 break;
5954 case MONO_PATCH_INFO_INTERNAL_METHOD:
5955 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
5956 case MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL: {
5957 guint32 len = strlen (patch_info->data.name);
5959 encode_value (len, p, &p);
5961 memcpy (p, patch_info->data.name, len);
5962 p += len;
5963 *p++ = '\0';
5964 break;
5966 case MONO_PATCH_INFO_LDSTR: {
5967 guint32 image_index = get_image_index (acfg, patch_info->data.token->image);
5968 guint32 token = patch_info->data.token->token;
5969 g_assert (mono_metadata_token_code (token) == MONO_TOKEN_STRING);
5970 encode_value (image_index, p, &p);
5971 encode_value (patch_info->data.token->token - MONO_TOKEN_STRING, p, &p);
5972 break;
5974 case MONO_PATCH_INFO_RVA:
5975 case MONO_PATCH_INFO_DECLSEC:
5976 case MONO_PATCH_INFO_LDTOKEN:
5977 case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
5978 encode_value (get_image_index (acfg, patch_info->data.token->image), p, &p);
5979 encode_value (patch_info->data.token->token, p, &p);
5980 encode_value (patch_info->data.token->has_context, p, &p);
5981 if (patch_info->data.token->has_context)
5982 encode_generic_context (acfg, &patch_info->data.token->context, p, &p);
5983 break;
5984 case MONO_PATCH_INFO_EXC_NAME: {
5985 MonoClass *ex_class;
5987 ex_class =
5988 mono_class_load_from_name (m_class_get_image (mono_defaults.exception_class),
5989 "System", (const char *)patch_info->data.target);
5990 encode_klass_ref (acfg, ex_class, p, &p);
5991 break;
5993 case MONO_PATCH_INFO_R4:
5994 encode_value (*((guint32 *)patch_info->data.target), p, &p);
5995 break;
5996 case MONO_PATCH_INFO_R8:
5997 encode_value (((guint32 *)patch_info->data.target) [MINI_LS_WORD_IDX], p, &p);
5998 encode_value (((guint32 *)patch_info->data.target) [MINI_MS_WORD_IDX], p, &p);
5999 break;
6000 case MONO_PATCH_INFO_VTABLE:
6001 case MONO_PATCH_INFO_CLASS:
6002 case MONO_PATCH_INFO_IID:
6003 case MONO_PATCH_INFO_ADJUSTED_IID:
6004 encode_klass_ref (acfg, patch_info->data.klass, p, &p);
6005 break;
6006 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE:
6007 encode_klass_ref (acfg, patch_info->data.del_tramp->klass, p, &p);
6008 if (patch_info->data.del_tramp->method) {
6009 encode_value (1, p, &p);
6010 encode_method_ref (acfg, patch_info->data.del_tramp->method, p, &p);
6011 } else {
6012 encode_value (0, p, &p);
6014 encode_value (patch_info->data.del_tramp->is_virtual, p, &p);
6015 break;
6016 case MONO_PATCH_INFO_FIELD:
6017 case MONO_PATCH_INFO_SFLDA:
6018 encode_field_info (acfg, patch_info->data.field, p, &p);
6019 break;
6020 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG:
6021 break;
6022 case MONO_PATCH_INFO_PROFILER_ALLOCATION_COUNT:
6023 case MONO_PATCH_INFO_PROFILER_CLAUSE_COUNT:
6024 break;
6025 case MONO_PATCH_INFO_RGCTX_FETCH:
6026 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX: {
6027 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
6028 guint32 offset;
6029 guint8 *buf2, *p2;
6032 * entry->method has a lenghtly encoding and multiple rgctx_fetch entries
6033 * reference the same method, so encode the method only once.
6035 offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_blob_hash, entry->method));
6036 if (!offset) {
6037 buf2 = (guint8 *)g_malloc (1024);
6038 p2 = buf2;
6040 encode_method_ref (acfg, entry->method, p2, &p2);
6041 g_assert (p2 - buf2 < 1024);
6043 offset = add_to_blob (acfg, buf2, p2 - buf2);
6044 g_free (buf2);
6046 g_hash_table_insert (acfg->method_blob_hash, entry->method, GUINT_TO_POINTER (offset + 1));
6047 } else {
6048 offset --;
6051 encode_value (offset, p, &p);
6052 g_assert ((int)entry->info_type < 256);
6053 g_assert (entry->data->type < 256);
6054 encode_value ((entry->in_mrgctx ? 1 : 0) | (entry->info_type << 1) | (entry->data->type << 9), p, &p);
6055 encode_patch (acfg, entry->data, p, &p);
6056 break;
6058 case MONO_PATCH_INFO_SEQ_POINT_INFO:
6059 case MONO_PATCH_INFO_AOT_MODULE:
6060 break;
6061 case MONO_PATCH_INFO_SIGNATURE:
6062 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER:
6063 encode_signature (acfg, (MonoMethodSignature*)patch_info->data.target, p, &p);
6064 break;
6065 case MONO_PATCH_INFO_GSHAREDVT_CALL:
6066 encode_signature (acfg, (MonoMethodSignature*)patch_info->data.gsharedvt->sig, p, &p);
6067 encode_method_ref (acfg, patch_info->data.gsharedvt->method, p, &p);
6068 break;
6069 case MONO_PATCH_INFO_GSHAREDVT_METHOD: {
6070 MonoGSharedVtMethodInfo *info = patch_info->data.gsharedvt_method;
6071 int i;
6073 encode_method_ref (acfg, info->method, p, &p);
6074 encode_value (info->num_entries, p, &p);
6075 for (i = 0; i < info->num_entries; ++i) {
6076 MonoRuntimeGenericContextInfoTemplate *template_ = &info->entries [i];
6078 encode_value (template_->info_type, p, &p);
6079 switch (mini_rgctx_info_type_to_patch_info_type (template_->info_type)) {
6080 case MONO_PATCH_INFO_CLASS:
6081 encode_klass_ref (acfg, mono_class_from_mono_type ((MonoType *)template_->data), p, &p);
6082 break;
6083 case MONO_PATCH_INFO_FIELD:
6084 encode_field_info (acfg, (MonoClassField *)template_->data, p, &p);
6085 break;
6086 default:
6087 g_assert_not_reached ();
6088 break;
6091 break;
6093 case MONO_PATCH_INFO_LDSTR_LIT: {
6094 const char *s = (const char *)patch_info->data.target;
6095 int len = strlen (s);
6097 encode_value (len, p, &p);
6098 memcpy (p, s, len + 1);
6099 p += len + 1;
6100 break;
6102 case MONO_PATCH_INFO_VIRT_METHOD:
6103 encode_klass_ref (acfg, patch_info->data.virt_method->klass, p, &p);
6104 encode_method_ref (acfg, patch_info->data.virt_method->method, p, &p);
6105 break;
6106 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG:
6107 case MONO_PATCH_INFO_JIT_THREAD_ATTACH:
6108 break;
6109 default:
6110 g_warning ("unable to handle jump info %d", patch_info->type);
6111 g_assert_not_reached ();
6114 *endbuf = p;
6117 static void
6118 encode_patch_list (MonoAotCompile *acfg, GPtrArray *patches, int n_patches, gboolean llvm, int first_got_offset, guint8 *buf, guint8 **endbuf)
6120 guint8 *p = buf;
6121 guint32 pindex, offset;
6122 MonoJumpInfo *patch_info;
6124 encode_value (n_patches, p, &p);
6126 for (pindex = 0; pindex < patches->len; ++pindex) {
6127 patch_info = (MonoJumpInfo *)g_ptr_array_index (patches, pindex);
6129 if (patch_info->type == MONO_PATCH_INFO_NONE || patch_info->type == MONO_PATCH_INFO_BB)
6130 /* Nothing to do */
6131 continue;
6133 offset = get_got_offset (acfg, llvm, patch_info);
6134 encode_value (offset, p, &p);
6137 *endbuf = p;
6140 static void
6141 emit_method_info (MonoAotCompile *acfg, MonoCompile *cfg)
6143 MonoMethod *method;
6144 int pindex, buf_size, n_patches;
6145 GPtrArray *patches;
6146 MonoJumpInfo *patch_info;
6147 guint32 method_index;
6148 guint8 *p, *buf;
6149 guint32 first_got_offset;
6151 method = cfg->orig_method;
6153 method_index = get_method_index (acfg, method);
6155 /* Sort relocations */
6156 patches = g_ptr_array_new ();
6157 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next)
6158 g_ptr_array_add (patches, patch_info);
6159 g_ptr_array_sort (patches, compare_patches);
6161 first_got_offset = acfg->cfgs [method_index]->got_offset;
6163 /**********************/
6164 /* Encode method info */
6165 /**********************/
6167 buf_size = (patches->len < 1000) ? 40960 : 40960 + (patches->len * 64);
6168 p = buf = (guint8 *)g_malloc (buf_size);
6170 if (mono_class_get_cctor (method->klass)) {
6171 encode_value (1, p, &p);
6172 encode_klass_ref (acfg, method->klass, p, &p);
6173 } else {
6174 /* Not needed when loading the method */
6175 encode_value (0, p, &p);
6178 g_assert (!(cfg->opt & MONO_OPT_SHARED));
6180 n_patches = 0;
6181 for (pindex = 0; pindex < patches->len; ++pindex) {
6182 patch_info = (MonoJumpInfo *)g_ptr_array_index (patches, pindex);
6184 if ((patch_info->type == MONO_PATCH_INFO_GOT_OFFSET) ||
6185 (patch_info->type == MONO_PATCH_INFO_NONE)) {
6186 patch_info->type = MONO_PATCH_INFO_NONE;
6187 /* Nothing to do */
6188 continue;
6191 if ((patch_info->type == MONO_PATCH_INFO_IMAGE) && (patch_info->data.image == acfg->image)) {
6192 /* Stored in a GOT slot initialized at module load time */
6193 patch_info->type = MONO_PATCH_INFO_NONE;
6194 continue;
6197 if (patch_info->type == MONO_PATCH_INFO_GC_CARD_TABLE_ADDR ||
6198 patch_info->type == MONO_PATCH_INFO_GC_NURSERY_START ||
6199 patch_info->type == MONO_PATCH_INFO_GC_NURSERY_BITS ||
6200 patch_info->type == MONO_PATCH_INFO_AOT_MODULE) {
6201 /* Stored in a GOT slot initialized at module load time */
6202 patch_info->type = MONO_PATCH_INFO_NONE;
6203 continue;
6206 if (is_plt_patch (patch_info) && !(cfg->compile_llvm && acfg->aot_opts.llvm_only)) {
6207 /* Calls are made through the PLT */
6208 patch_info->type = MONO_PATCH_INFO_NONE;
6209 continue;
6212 n_patches ++;
6215 if (n_patches)
6216 g_assert (cfg->has_got_slots);
6218 encode_patch_list (acfg, patches, n_patches, cfg->compile_llvm, first_got_offset, p, &p);
6220 g_ptr_array_free (patches, TRUE);
6222 acfg->stats.info_size += p - buf;
6224 g_assert (p - buf < buf_size);
6226 cfg->method_info_offset = add_to_blob (acfg, buf, p - buf);
6227 g_free (buf);
6230 static guint32
6231 get_unwind_info_offset (MonoAotCompile *acfg, guint8 *encoded, guint32 encoded_len)
6233 guint32 cache_index;
6234 guint32 offset;
6236 /* Reuse the unwind module to canonize and store unwind info entries */
6237 cache_index = mono_cache_unwind_info (encoded, encoded_len);
6239 /* Use +/- 1 to distinguish 0s from missing entries */
6240 offset = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1)));
6241 if (offset)
6242 return offset - 1;
6243 else {
6244 guint8 buf [16];
6245 guint8 *p;
6248 * It would be easier to use assembler symbols, but the caller needs an
6249 * offset now.
6251 offset = acfg->unwind_info_offset;
6252 g_hash_table_insert (acfg->unwind_info_offsets, GUINT_TO_POINTER (cache_index + 1), GUINT_TO_POINTER (offset + 1));
6253 g_ptr_array_add (acfg->unwind_ops, GUINT_TO_POINTER (cache_index));
6255 p = buf;
6256 encode_value (encoded_len, p, &p);
6258 acfg->unwind_info_offset += encoded_len + (p - buf);
6259 return offset;
6263 static void
6264 emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg, gboolean store_seq_points)
6266 int i, k, buf_size;
6267 guint32 debug_info_size, seq_points_size;
6268 guint8 *code;
6269 MonoMethodHeader *header;
6270 guint8 *p, *buf, *debug_info;
6271 MonoJitInfo *jinfo = cfg->jit_info;
6272 guint32 flags;
6273 gboolean use_unwind_ops = FALSE;
6274 MonoSeqPointInfo *seq_points;
6276 code = cfg->native_code;
6277 header = cfg->header;
6279 if (!acfg->aot_opts.nodebug) {
6280 mono_debug_serialize_debug_info (cfg, &debug_info, &debug_info_size);
6281 } else {
6282 debug_info = NULL;
6283 debug_info_size = 0;
6286 seq_points = cfg->seq_point_info;
6287 seq_points_size = (store_seq_points)? mono_seq_point_info_get_write_size (seq_points) : 0;
6289 buf_size = header->num_clauses * 256 + debug_info_size + 2048 + seq_points_size + cfg->gc_map_size;
6290 if (jinfo->has_try_block_holes) {
6291 MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
6292 buf_size += table->num_holes * 16;
6295 p = buf = (guint8 *)g_malloc (buf_size);
6297 use_unwind_ops = cfg->unwind_ops != NULL;
6299 flags = (jinfo->has_generic_jit_info ? 1 : 0) | (use_unwind_ops ? 2 : 0) | (header->num_clauses ? 4 : 0) | (seq_points_size ? 8 : 0) | (cfg->compile_llvm ? 16 : 0) | (jinfo->has_try_block_holes ? 32 : 0) | (cfg->gc_map ? 64 : 0) | (jinfo->has_arch_eh_info ? 128 : 0);
6301 encode_value (flags, p, &p);
6303 if (use_unwind_ops) {
6304 guint32 encoded_len;
6305 guint8 *encoded;
6306 guint32 unwind_desc;
6308 encoded = mono_unwind_ops_encode (cfg->unwind_ops, &encoded_len);
6310 unwind_desc = get_unwind_info_offset (acfg, encoded, encoded_len);
6311 encode_value (unwind_desc, p, &p);
6313 g_free (encoded);
6314 } else {
6315 encode_value (jinfo->unwind_info, p, &p);
6318 /*Encode the number of holes before the number of clauses to make decoding easier*/
6319 if (jinfo->has_try_block_holes) {
6320 MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
6321 encode_value (table->num_holes, p, &p);
6324 if (jinfo->has_arch_eh_info) {
6326 * In AOT mode, the code length is calculated from the address of the previous method,
6327 * which could include alignment padding, so calculating the start of the epilog as
6328 * code_len - epilog_size is correct any more. Save the real code len as a workaround.
6330 encode_value (jinfo->code_size, p, &p);
6333 /* Exception table */
6334 if (cfg->compile_llvm) {
6336 * When using LLVM, we can't emit some data, like pc offsets, this reg/offset etc.,
6337 * since the information is only available to llc. Instead, we let llc save the data
6338 * into the LSDA, and read it from there at runtime.
6340 /* The assembly might be CIL stripped so emit the data ourselves */
6341 if (header->num_clauses)
6342 encode_value (header->num_clauses, p, &p);
6344 for (k = 0; k < header->num_clauses; ++k) {
6345 MonoExceptionClause *clause;
6347 clause = &header->clauses [k];
6349 encode_value (clause->flags, p, &p);
6350 if (!(clause->flags == MONO_EXCEPTION_CLAUSE_FILTER || clause->flags == MONO_EXCEPTION_CLAUSE_FINALLY)) {
6351 if (clause->data.catch_class) {
6352 guint8 *buf2, *p2;
6353 int len;
6355 buf2 = (guint8 *)g_malloc (4096);
6356 p2 = buf2;
6357 encode_klass_ref (acfg, clause->data.catch_class, p2, &p2);
6358 len = p2 - buf2;
6359 g_assert (len < 4096);
6360 encode_value (len, p, &p);
6361 memcpy (p, buf2, len);
6362 p += p2 - buf2;
6363 g_free (buf2);
6364 } else {
6365 encode_value (0, p, &p);
6369 /* Emit the IL ranges too, since they might not be available at runtime */
6370 encode_value (clause->try_offset, p, &p);
6371 encode_value (clause->try_len, p, &p);
6372 encode_value (clause->handler_offset, p, &p);
6373 encode_value (clause->handler_len, p, &p);
6375 /* Emit a list of nesting clauses */
6376 for (i = 0; i < header->num_clauses; ++i) {
6377 gint32 cindex1 = k;
6378 MonoExceptionClause *clause1 = &header->clauses [cindex1];
6379 gint32 cindex2 = i;
6380 MonoExceptionClause *clause2 = &header->clauses [cindex2];
6382 if (cindex1 != cindex2 && clause1->try_offset >= clause2->try_offset && clause1->handler_offset <= clause2->handler_offset)
6383 encode_value (i, p, &p);
6385 encode_value (-1, p, &p);
6387 } else {
6388 if (jinfo->num_clauses)
6389 encode_value (jinfo->num_clauses, p, &p);
6391 for (k = 0; k < jinfo->num_clauses; ++k) {
6392 MonoJitExceptionInfo *ei = &jinfo->clauses [k];
6394 encode_value (ei->flags, p, &p);
6395 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
6396 /* Not used for catch clauses */
6397 if (ei->flags != MONO_EXCEPTION_CLAUSE_NONE)
6398 encode_value (ei->exvar_offset, p, &p);
6399 #else
6400 encode_value (ei->exvar_offset, p, &p);
6401 #endif
6403 if (ei->flags == MONO_EXCEPTION_CLAUSE_FILTER || ei->flags == MONO_EXCEPTION_CLAUSE_FINALLY)
6404 encode_value ((gint)((guint8*)ei->data.filter - code), p, &p);
6405 else {
6406 if (ei->data.catch_class) {
6407 guint8 *buf2, *p2;
6408 int len;
6410 buf2 = (guint8 *)g_malloc (4096);
6411 p2 = buf2;
6412 encode_klass_ref (acfg, ei->data.catch_class, p2, &p2);
6413 len = p2 - buf2;
6414 g_assert (len < 4096);
6415 encode_value (len, p, &p);
6416 memcpy (p, buf2, len);
6417 p += p2 - buf2;
6418 g_free (buf2);
6419 } else {
6420 encode_value (0, p, &p);
6424 encode_value ((gint)((guint8*)ei->try_start - code), p, &p);
6425 encode_value ((gint)((guint8*)ei->try_end - code), p, &p);
6426 encode_value ((gint)((guint8*)ei->handler_start - code), p, &p);
6430 if (jinfo->has_try_block_holes) {
6431 MonoTryBlockHoleTableJitInfo *table = mono_jit_info_get_try_block_hole_table_info (jinfo);
6432 for (i = 0; i < table->num_holes; ++i) {
6433 MonoTryBlockHoleJitInfo *hole = &table->holes [i];
6434 encode_value (hole->clause, p, &p);
6435 encode_value (hole->length, p, &p);
6436 encode_value (hole->offset, p, &p);
6440 if (jinfo->has_arch_eh_info) {
6441 MonoArchEHJitInfo *eh_info;
6443 eh_info = mono_jit_info_get_arch_eh_info (jinfo);
6444 encode_value (eh_info->stack_size, p, &p);
6445 encode_value (eh_info->epilog_size, p, &p);
6448 if (jinfo->has_generic_jit_info) {
6449 MonoGenericJitInfo *gi = mono_jit_info_get_generic_jit_info (jinfo);
6450 MonoGenericSharingContext* gsctx = gi->generic_sharing_context;
6451 guint8 *buf2, *p2;
6452 int len;
6454 encode_value (gi->nlocs, p, &p);
6455 if (gi->nlocs) {
6456 for (i = 0; i < gi->nlocs; ++i) {
6457 MonoDwarfLocListEntry *entry = &gi->locations [i];
6459 encode_value (entry->is_reg ? 1 : 0, p, &p);
6460 encode_value (entry->reg, p, &p);
6461 if (!entry->is_reg)
6462 encode_value (entry->offset, p, &p);
6463 if (i == 0)
6464 g_assert (entry->from == 0);
6465 else
6466 encode_value (entry->from, p, &p);
6467 encode_value (entry->to, p, &p);
6469 } else {
6470 if (!cfg->compile_llvm) {
6471 encode_value (gi->has_this ? 1 : 0, p, &p);
6472 encode_value (gi->this_reg, p, &p);
6473 encode_value (gi->this_offset, p, &p);
6478 * Need to encode jinfo->method too, since it is not equal to 'method'
6479 * when using generic sharing.
6481 buf2 = (guint8 *)g_malloc (4096);
6482 p2 = buf2;
6483 encode_method_ref (acfg, jinfo->d.method, p2, &p2);
6484 len = p2 - buf2;
6485 g_assert (len < 4096);
6486 encode_value (len, p, &p);
6487 memcpy (p, buf2, len);
6488 p += p2 - buf2;
6489 g_free (buf2);
6491 if (gsctx && gsctx->is_gsharedvt) {
6492 encode_value (1, p, &p);
6493 } else {
6494 encode_value (0, p, &p);
6498 if (seq_points_size)
6499 p += mono_seq_point_info_write (seq_points, p);
6501 g_assert (debug_info_size < buf_size);
6503 encode_value (debug_info_size, p, &p);
6504 if (debug_info_size) {
6505 memcpy (p, debug_info, debug_info_size);
6506 p += debug_info_size;
6507 g_free (debug_info);
6510 /* GC Map */
6511 if (cfg->gc_map) {
6512 encode_value (cfg->gc_map_size, p, &p);
6513 /* The GC map requires 4 bytes of alignment */
6514 while ((gsize)p % 4)
6515 p ++;
6516 memcpy (p, cfg->gc_map, cfg->gc_map_size);
6517 p += cfg->gc_map_size;
6520 acfg->stats.ex_info_size += p - buf;
6522 g_assert (p - buf < buf_size);
6524 /* Emit info */
6525 /* The GC Map requires 4 byte alignment */
6526 cfg->ex_info_offset = add_to_blob_aligned (acfg, buf, p - buf, cfg->gc_map ? 4 : 1);
6527 g_free (buf);
6530 static guint32
6531 emit_klass_info (MonoAotCompile *acfg, guint32 token)
6533 ERROR_DECL (error);
6534 MonoClass *klass = mono_class_get_checked (acfg->image, token, error);
6535 guint8 *p, *buf;
6536 int i, buf_size, res;
6537 gboolean no_special_static, cant_encode;
6538 gpointer iter = NULL;
6540 if (!klass) {
6541 mono_error_cleanup (error);
6543 buf_size = 16;
6545 p = buf = (guint8 *)g_malloc (buf_size);
6547 /* Mark as unusable */
6548 encode_value (-1, p, &p);
6550 res = add_to_blob (acfg, buf, p - buf);
6551 g_free (buf);
6553 return res;
6556 buf_size = 10240 + (m_class_get_vtable_size (klass) * 16);
6557 p = buf = (guint8 *)g_malloc (buf_size);
6559 g_assert (klass);
6561 mono_class_init (klass);
6563 mono_class_get_nested_types (klass, &iter);
6564 g_assert (m_class_is_nested_classes_inited (klass));
6566 mono_class_setup_vtable (klass);
6569 * Emit all the information which is required for creating vtables so
6570 * the runtime does not need to create the MonoMethod structures which
6571 * take up a lot of space.
6574 no_special_static = !mono_class_has_special_static_fields (klass);
6576 /* Check whenever we have enough info to encode the vtable */
6577 cant_encode = FALSE;
6578 MonoMethod **klass_vtable = m_class_get_vtable (klass);
6579 for (i = 0; i < m_class_get_vtable_size (klass); ++i) {
6580 MonoMethod *cm = klass_vtable [i];
6582 if (cm && mono_method_signature (cm)->is_inflated && !g_hash_table_lookup (acfg->token_info_hash, cm))
6583 cant_encode = TRUE;
6586 mono_class_has_finalizer (klass);
6587 if (mono_class_has_failure (klass))
6588 cant_encode = TRUE;
6590 if (mono_class_is_gtd (klass) || cant_encode) {
6591 encode_value (-1, p, &p);
6592 } else {
6593 gboolean has_nested = mono_class_get_nested_classes_property (klass) != NULL;
6594 encode_value (m_class_get_vtable_size (klass), p, &p);
6595 encode_value ((m_class_has_weak_fields (klass) << 9) | (mono_class_is_gtd (klass) ? (1 << 8) : 0) | (no_special_static << 7) | (m_class_has_static_refs (klass) << 6) | (m_class_has_references (klass) << 5) | ((m_class_is_blittable (klass) << 4) | (has_nested ? 1 : 0) << 3) | (m_class_has_cctor (klass) << 2) | (m_class_has_finalize (klass) << 1) | m_class_is_ghcimpl (klass), p, &p);
6596 if (m_class_has_cctor (klass))
6597 encode_method_ref (acfg, mono_class_get_cctor (klass), p, &p);
6598 if (m_class_has_finalize (klass))
6599 encode_method_ref (acfg, mono_class_get_finalizer (klass), p, &p);
6601 encode_value (m_class_get_instance_size (klass), p, &p);
6602 encode_value (mono_class_data_size (klass), p, &p);
6603 encode_value (m_class_get_packing_size (klass), p, &p);
6604 encode_value (m_class_get_min_align (klass), p, &p);
6606 for (i = 0; i < m_class_get_vtable_size (klass); ++i) {
6607 MonoMethod *cm = klass_vtable [i];
6609 if (cm)
6610 encode_method_ref (acfg, cm, p, &p);
6611 else
6612 encode_value (0, p, &p);
6616 acfg->stats.class_info_size += p - buf;
6618 g_assert (p - buf < buf_size);
6619 res = add_to_blob (acfg, buf, p - buf);
6620 g_free (buf);
6622 return res;
6625 static char*
6626 get_plt_entry_debug_sym (MonoAotCompile *acfg, MonoJumpInfo *ji, GHashTable *cache)
6628 char *debug_sym = NULL;
6629 char *prefix;
6631 if (acfg->llvm && llvm_acfg->aot_opts.static_link) {
6632 /* Need to add a prefix to create unique symbols */
6633 prefix = g_strdup_printf ("plt_%s_", acfg->assembly_name_sym);
6634 } else {
6635 #if defined(TARGET_WIN32) && defined(TARGET_X86)
6636 prefix = mangle_symbol_alloc ("plt_");
6637 #else
6638 prefix = g_strdup ("plt_");
6639 #endif
6642 switch (ji->type) {
6643 case MONO_PATCH_INFO_METHOD:
6644 debug_sym = get_debug_sym (ji->data.method, prefix, cache);
6645 break;
6646 case MONO_PATCH_INFO_INTERNAL_METHOD:
6647 debug_sym = g_strdup_printf ("%s_jit_icall_%s", prefix, ji->data.name);
6648 break;
6649 case MONO_PATCH_INFO_RGCTX_FETCH:
6650 debug_sym = g_strdup_printf ("%s_rgctx_fetch_%d", prefix, acfg->label_generator ++);
6651 break;
6652 case MONO_PATCH_INFO_ICALL_ADDR:
6653 case MONO_PATCH_INFO_ICALL_ADDR_CALL: {
6654 char *s = get_debug_sym (ji->data.method, "", cache);
6656 debug_sym = g_strdup_printf ("%s_icall_native_%s", prefix, s);
6657 g_free (s);
6658 break;
6660 case MONO_PATCH_INFO_JIT_ICALL_ADDR:
6661 debug_sym = g_strdup_printf ("%s_jit_icall_native_%s", prefix, ji->data.name);
6662 break;
6663 default:
6664 break;
6667 g_free (prefix);
6669 return sanitize_symbol (acfg, debug_sym);
6673 * Calls made from AOTed code are routed through a table of jumps similar to the
6674 * ELF PLT (Program Linkage Table). Initially the PLT entries jump to code which transfers
6675 * control to the AOT runtime through a trampoline.
6677 static void
6678 emit_plt (MonoAotCompile *acfg)
6680 int i;
6682 if (acfg->aot_opts.llvm_only) {
6683 g_assert (acfg->plt_offset == 1);
6684 return;
6687 emit_line (acfg);
6689 emit_section_change (acfg, ".text", 0);
6690 emit_alignment_code (acfg, 16);
6691 emit_info_symbol (acfg, "plt");
6692 emit_label (acfg, acfg->plt_symbol);
6694 for (i = 0; i < acfg->plt_offset; ++i) {
6695 char *debug_sym = NULL;
6696 MonoPltEntry *plt_entry = NULL;
6698 if (i == 0)
6700 * The first plt entry is unused.
6702 continue;
6704 plt_entry = (MonoPltEntry *)g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
6706 debug_sym = plt_entry->debug_sym;
6708 if (acfg->thumb_mixed && !plt_entry->jit_used)
6709 /* Emit only a thumb version */
6710 continue;
6712 /* Skip plt entries not actually called */
6713 if (!plt_entry->jit_used && !plt_entry->llvm_used)
6714 continue;
6716 if (acfg->llvm && !acfg->thumb_mixed) {
6717 emit_label (acfg, plt_entry->llvm_symbol);
6718 if (acfg->llvm) {
6719 emit_global_inner (acfg, plt_entry->llvm_symbol, TRUE);
6720 #if defined(TARGET_MACH)
6721 fprintf (acfg->fp, ".private_extern %s\n", plt_entry->llvm_symbol);
6722 #endif
6726 if (debug_sym) {
6727 if (acfg->need_no_dead_strip) {
6728 emit_unset_mode (acfg);
6729 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
6731 emit_local_symbol (acfg, debug_sym, NULL, TRUE);
6732 emit_label (acfg, debug_sym);
6735 emit_label (acfg, plt_entry->symbol);
6737 arch_emit_plt_entry (acfg, acfg->got_symbol, (acfg->plt_got_offset_base + i) * sizeof (gpointer), acfg->plt_got_info_offsets [i]);
6739 if (debug_sym)
6740 emit_symbol_size (acfg, debug_sym, ".");
6743 if (acfg->thumb_mixed) {
6744 /* Make sure the ARM symbols don't alias the thumb ones */
6745 emit_zero_bytes (acfg, 16);
6748 * Emit a separate set of PLT entries using thumb2 which is called by LLVM generated
6749 * code.
6751 for (i = 0; i < acfg->plt_offset; ++i) {
6752 char *debug_sym = NULL;
6753 MonoPltEntry *plt_entry = NULL;
6755 if (i == 0)
6756 continue;
6758 plt_entry = (MonoPltEntry *)g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
6760 /* Skip plt entries not actually called by LLVM code */
6761 if (!plt_entry->llvm_used)
6762 continue;
6764 if (acfg->aot_opts.write_symbols) {
6765 if (plt_entry->debug_sym)
6766 debug_sym = g_strdup_printf ("%s_thumb", plt_entry->debug_sym);
6769 if (debug_sym) {
6770 #if defined(TARGET_MACH)
6771 fprintf (acfg->fp, " .thumb_func %s\n", debug_sym);
6772 fprintf (acfg->fp, " .no_dead_strip %s\n", debug_sym);
6773 #endif
6774 emit_local_symbol (acfg, debug_sym, NULL, TRUE);
6775 emit_label (acfg, debug_sym);
6777 fprintf (acfg->fp, "\n.thumb_func\n");
6779 emit_label (acfg, plt_entry->llvm_symbol);
6781 if (acfg->llvm)
6782 emit_global_inner (acfg, plt_entry->llvm_symbol, TRUE);
6784 arch_emit_llvm_plt_entry (acfg, acfg->got_symbol, (acfg->plt_got_offset_base + i) * sizeof (gpointer), acfg->plt_got_info_offsets [i]);
6786 if (debug_sym) {
6787 emit_symbol_size (acfg, debug_sym, ".");
6788 g_free (debug_sym);
6793 emit_symbol_size (acfg, acfg->plt_symbol, ".");
6795 emit_info_symbol (acfg, "plt_end");
6797 arch_emit_unwind_info_sections (acfg, "plt", "plt_end", NULL);
6801 * emit_trampoline_full:
6803 * If EMIT_TINFO is TRUE, emit additional information which can be used to create a MonoJitInfo for this trampoline by
6804 * create_jit_info_for_trampoline ().
6806 static G_GNUC_UNUSED void
6807 emit_trampoline_full (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info, gboolean emit_tinfo)
6809 char start_symbol [MAX_SYMBOL_SIZE];
6810 char end_symbol [MAX_SYMBOL_SIZE];
6811 char symbol [MAX_SYMBOL_SIZE];
6812 guint32 buf_size, info_offset;
6813 MonoJumpInfo *patch_info;
6814 guint8 *buf, *p;
6815 GPtrArray *patches;
6816 char *name;
6817 guint8 *code;
6818 guint32 code_size;
6819 MonoJumpInfo *ji;
6820 GSList *unwind_ops;
6822 g_assert (info);
6824 name = info->name;
6825 code = info->code;
6826 code_size = info->code_size;
6827 ji = info->ji;
6828 unwind_ops = info->unwind_ops;
6830 /* Emit code */
6832 sprintf (start_symbol, "%s%s", acfg->user_symbol_prefix, name);
6834 emit_section_change (acfg, ".text", 0);
6835 emit_global (acfg, start_symbol, TRUE);
6836 emit_alignment_code (acfg, AOT_FUNC_ALIGNMENT);
6837 emit_label (acfg, start_symbol);
6839 sprintf (symbol, "%snamed_%s", acfg->temp_prefix, name);
6840 emit_label (acfg, symbol);
6843 * The code should access everything through the GOT, so we pass
6844 * TRUE here.
6846 emit_and_reloc_code (acfg, NULL, code, code_size, ji, TRUE, NULL);
6848 emit_symbol_size (acfg, start_symbol, ".");
6850 if (emit_tinfo) {
6851 sprintf (end_symbol, "%snamede_%s", acfg->temp_prefix, name);
6852 emit_label (acfg, end_symbol);
6855 /* Emit info */
6857 /* Sort relocations */
6858 patches = g_ptr_array_new ();
6859 for (patch_info = ji; patch_info; patch_info = patch_info->next)
6860 if (patch_info->type != MONO_PATCH_INFO_NONE)
6861 g_ptr_array_add (patches, patch_info);
6862 g_ptr_array_sort (patches, compare_patches);
6864 buf_size = patches->len * 128 + 128;
6865 buf = (guint8 *)g_malloc (buf_size);
6866 p = buf;
6868 encode_patch_list (acfg, patches, patches->len, FALSE, got_offset, p, &p);
6869 g_assert (p - buf < buf_size);
6870 g_ptr_array_free (patches, TRUE);
6872 sprintf (symbol, "%s%s_p", acfg->user_symbol_prefix, name);
6874 info_offset = add_to_blob (acfg, buf, p - buf);
6876 emit_section_change (acfg, RODATA_SECT, 0);
6877 emit_global (acfg, symbol, FALSE);
6878 emit_label (acfg, symbol);
6880 emit_int32 (acfg, info_offset);
6882 if (emit_tinfo) {
6883 guint8 *encoded;
6884 guint32 encoded_len;
6885 guint32 uw_offset;
6888 * Emit additional information which can be used to reconstruct a partial MonoTrampInfo.
6890 encoded = mono_unwind_ops_encode (info->unwind_ops, &encoded_len);
6891 uw_offset = get_unwind_info_offset (acfg, encoded, encoded_len);
6892 g_free (encoded);
6894 emit_symbol_diff (acfg, end_symbol, start_symbol, 0);
6895 emit_int32 (acfg, uw_offset);
6898 /* Emit debug info */
6899 if (unwind_ops) {
6900 char symbol2 [MAX_SYMBOL_SIZE];
6902 sprintf (symbol, "%s", name);
6903 sprintf (symbol2, "%snamed_%s", acfg->temp_prefix, name);
6905 arch_emit_unwind_info_sections (acfg, start_symbol, end_symbol, unwind_ops);
6907 if (acfg->dwarf)
6908 mono_dwarf_writer_emit_trampoline (acfg->dwarf, symbol, symbol2, NULL, NULL, code_size, unwind_ops);
6911 g_free (buf);
6914 static G_GNUC_UNUSED void
6915 emit_trampoline (MonoAotCompile *acfg, int got_offset, MonoTrampInfo *info)
6917 emit_trampoline_full (acfg, got_offset, info, TRUE);
6920 static void
6921 emit_trampolines (MonoAotCompile *acfg)
6923 char symbol [MAX_SYMBOL_SIZE];
6924 char end_symbol [MAX_SYMBOL_SIZE];
6925 int i, tramp_got_offset;
6926 int ntype;
6927 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
6928 int tramp_type;
6929 #endif
6931 if ((!mono_aot_mode_is_full (&acfg->aot_opts) || acfg->aot_opts.llvm_only) && !acfg->aot_opts.interp)
6932 return;
6934 g_assert (acfg->image->assembly);
6936 /* Currently, we emit most trampolines into the mscorlib AOT image. */
6937 if (strcmp (acfg->image->assembly->aname.name, "mscorlib") == 0) {
6938 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
6939 MonoTrampInfo *info;
6942 * Emit the generic trampolines.
6944 * We could save some code by treating the generic trampolines as a wrapper
6945 * method, but that approach has its own complexities, so we choose the simpler
6946 * method.
6948 for (tramp_type = 0; tramp_type < MONO_TRAMPOLINE_NUM; ++tramp_type) {
6949 /* we overload the boolean here to indicate the slightly different trampoline needed, see mono_arch_create_generic_trampoline() */
6950 #ifdef DISABLE_REMOTING
6951 if (tramp_type == MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING)
6952 continue;
6953 #endif
6954 mono_arch_create_generic_trampoline ((MonoTrampolineType)tramp_type, &info, acfg->aot_opts.use_trampolines_page? 2: TRUE);
6955 emit_trampoline (acfg, acfg->got_offset, info);
6956 mono_tramp_info_free (info);
6959 /* Emit the exception related code pieces */
6960 mono_arch_get_restore_context (&info, TRUE);
6961 emit_trampoline (acfg, acfg->got_offset, info);
6962 mono_tramp_info_free (info);
6964 mono_arch_get_call_filter (&info, TRUE);
6965 emit_trampoline (acfg, acfg->got_offset, info);
6966 mono_tramp_info_free (info);
6968 mono_arch_get_throw_exception (&info, TRUE);
6969 emit_trampoline (acfg, acfg->got_offset, info);
6970 mono_tramp_info_free (info);
6972 mono_arch_get_rethrow_exception (&info, TRUE);
6973 emit_trampoline (acfg, acfg->got_offset, info);
6974 mono_tramp_info_free (info);
6976 mono_arch_get_throw_corlib_exception (&info, TRUE);
6977 emit_trampoline (acfg, acfg->got_offset, info);
6978 mono_tramp_info_free (info);
6980 #ifdef MONO_ARCH_HAVE_SDB_TRAMPOLINES
6981 mono_arch_create_sdb_trampoline (TRUE, &info, TRUE);
6982 emit_trampoline (acfg, acfg->got_offset, info);
6983 mono_tramp_info_free (info);
6985 mono_arch_create_sdb_trampoline (FALSE, &info, TRUE);
6986 emit_trampoline (acfg, acfg->got_offset, info);
6987 mono_tramp_info_free (info);
6988 #endif
6990 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
6991 mono_arch_get_gsharedvt_trampoline (&info, TRUE);
6992 if (info) {
6993 emit_trampoline_full (acfg, acfg->got_offset, info, TRUE);
6995 /* Create a separate out trampoline for more information in stack traces */
6996 info->name = g_strdup ("gsharedvt_out_trampoline");
6997 emit_trampoline_full (acfg, acfg->got_offset, info, TRUE);
6998 mono_tramp_info_free (info);
7000 #endif
7002 #if defined(MONO_ARCH_HAVE_GET_TRAMPOLINES)
7004 GSList *l = mono_arch_get_trampolines (TRUE);
7006 while (l) {
7007 MonoTrampInfo *info = (MonoTrampInfo *)l->data;
7009 emit_trampoline (acfg, acfg->got_offset, info);
7010 l = l->next;
7013 #endif
7015 for (i = 0; i < acfg->aot_opts.nrgctx_fetch_trampolines; ++i) {
7016 int offset;
7018 offset = MONO_RGCTX_SLOT_MAKE_RGCTX (i);
7019 mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
7020 emit_trampoline (acfg, acfg->got_offset, info);
7021 mono_tramp_info_free (info);
7023 offset = MONO_RGCTX_SLOT_MAKE_MRGCTX (i);
7024 mono_arch_create_rgctx_lazy_fetch_trampoline (offset, &info, TRUE);
7025 emit_trampoline (acfg, acfg->got_offset, info);
7026 mono_tramp_info_free (info);
7029 #ifdef MONO_ARCH_HAVE_GENERAL_RGCTX_LAZY_FETCH_TRAMPOLINE
7030 mono_arch_create_general_rgctx_lazy_fetch_trampoline (&info, TRUE);
7031 emit_trampoline (acfg, acfg->got_offset, info);
7032 mono_tramp_info_free (info);
7033 #endif
7036 GSList *l;
7038 /* delegate_invoke_impl trampolines */
7039 l = mono_arch_get_delegate_invoke_impls ();
7040 while (l) {
7041 MonoTrampInfo *info = (MonoTrampInfo *)l->data;
7043 emit_trampoline (acfg, acfg->got_offset, info);
7044 l = l->next;
7048 if (mono_aot_mode_is_interp (&acfg->aot_opts)) {
7049 mono_arch_get_interp_to_native_trampoline (&info);
7050 emit_trampoline (acfg, acfg->got_offset, info);
7051 #ifdef MONO_ARCH_HAVE_INTERP_ENTRY_TRAMPOLINE
7052 mono_arch_get_native_to_interp_trampoline (&info);
7053 emit_trampoline (acfg, acfg->got_offset, info);
7054 #endif
7057 #endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
7059 /* Emit trampolines which are numerous */
7062 * These include the following:
7063 * - specific trampolines
7064 * - static rgctx invoke trampolines
7065 * - imt trampolines
7066 * These trampolines have the same code, they are parameterized by GOT
7067 * slots.
7068 * They are defined in this file, in the arch_... routines instead of
7069 * in tramp-<ARCH>.c, since it is easier to do it this way.
7073 * When running in aot-only mode, we can't create specific trampolines at
7074 * runtime, so we create a few, and save them in the AOT file.
7075 * Normal trampolines embed their argument as a literal inside the
7076 * trampoline code, we can't do that here, so instead we embed an offset
7077 * which needs to be added to the trampoline address to get the address of
7078 * the GOT slot which contains the argument value.
7079 * The generated trampolines jump to the generic trampolines using another
7080 * GOT slot, which will be setup by the AOT loader to point to the
7081 * generic trampoline code of the given type.
7085 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for
7086 * each class).
7089 emit_section_change (acfg, ".text", 0);
7091 tramp_got_offset = acfg->got_offset;
7093 for (ntype = 0; ntype < MONO_AOT_TRAMP_NUM; ++ntype) {
7094 switch (ntype) {
7095 case MONO_AOT_TRAMP_SPECIFIC:
7096 sprintf (symbol, "specific_trampolines");
7097 break;
7098 case MONO_AOT_TRAMP_STATIC_RGCTX:
7099 sprintf (symbol, "static_rgctx_trampolines");
7100 break;
7101 case MONO_AOT_TRAMP_IMT:
7102 sprintf (symbol, "imt_trampolines");
7103 break;
7104 case MONO_AOT_TRAMP_GSHAREDVT_ARG:
7105 sprintf (symbol, "gsharedvt_arg_trampolines");
7106 break;
7107 default:
7108 g_assert_not_reached ();
7111 sprintf (end_symbol, "%s_e", symbol);
7113 if (acfg->aot_opts.write_symbols)
7114 emit_local_symbol (acfg, symbol, end_symbol, TRUE);
7116 emit_alignment_code (acfg, AOT_FUNC_ALIGNMENT);
7117 emit_info_symbol (acfg, symbol);
7119 acfg->trampoline_got_offset_base [ntype] = tramp_got_offset;
7121 for (i = 0; i < acfg->num_trampolines [ntype]; ++i) {
7122 int tramp_size = 0;
7124 switch (ntype) {
7125 case MONO_AOT_TRAMP_SPECIFIC:
7126 arch_emit_specific_trampoline (acfg, tramp_got_offset, &tramp_size);
7127 tramp_got_offset += 2;
7128 break;
7129 case MONO_AOT_TRAMP_STATIC_RGCTX:
7130 arch_emit_static_rgctx_trampoline (acfg, tramp_got_offset, &tramp_size);
7131 tramp_got_offset += 2;
7132 break;
7133 case MONO_AOT_TRAMP_IMT:
7134 arch_emit_imt_trampoline (acfg, tramp_got_offset, &tramp_size);
7135 tramp_got_offset += 1;
7136 break;
7137 case MONO_AOT_TRAMP_GSHAREDVT_ARG:
7138 arch_emit_gsharedvt_arg_trampoline (acfg, tramp_got_offset, &tramp_size);
7139 tramp_got_offset += 2;
7140 break;
7141 default:
7142 g_assert_not_reached ();
7144 if (!acfg->trampoline_size [ntype]) {
7145 g_assert (tramp_size);
7146 acfg->trampoline_size [ntype] = tramp_size;
7150 emit_label (acfg, end_symbol);
7151 emit_int32 (acfg, 0);
7154 arch_emit_specific_trampoline_pages (acfg);
7156 /* Reserve some entries at the end of the GOT for our use */
7157 acfg->num_trampoline_got_entries = tramp_got_offset - acfg->got_offset;
7160 acfg->got_offset += acfg->num_trampoline_got_entries;
7163 static gboolean
7164 str_begins_with (const char *str1, const char *str2)
7166 int len = strlen (str2);
7167 return strncmp (str1, str2, len) == 0;
7170 void*
7171 mono_aot_readonly_field_override (MonoClassField *field)
7173 ReadOnlyValue *rdv;
7174 for (rdv = readonly_values; rdv; rdv = rdv->next) {
7175 char *p = rdv->name;
7176 int len;
7177 len = strlen (m_class_get_name_space (field->parent));
7178 if (strncmp (p, m_class_get_name_space (field->parent), len))
7179 continue;
7180 p += len;
7181 if (*p++ != '.')
7182 continue;
7183 len = strlen (m_class_get_name (field->parent));
7184 if (strncmp (p, m_class_get_name (field->parent), len))
7185 continue;
7186 p += len;
7187 if (*p++ != '.')
7188 continue;
7189 if (strcmp (p, field->name))
7190 continue;
7191 switch (rdv->type) {
7192 case MONO_TYPE_I1:
7193 return &rdv->value.i1;
7194 case MONO_TYPE_I2:
7195 return &rdv->value.i2;
7196 case MONO_TYPE_I4:
7197 return &rdv->value.i4;
7198 default:
7199 break;
7202 return NULL;
7205 static void
7206 add_readonly_value (MonoAotOptions *opts, const char *val)
7208 ReadOnlyValue *rdv;
7209 const char *fval;
7210 const char *tval;
7211 /* the format of val is:
7212 * namespace.typename.fieldname=type/value
7213 * type can be i1 for uint8/int8/boolean, i2 for uint16/int16/char, i4 for uint32/int32
7215 fval = strrchr (val, '/');
7216 if (!fval) {
7217 fprintf (stderr, "AOT : invalid format for readonly field '%s', missing /.\n", val);
7218 exit (1);
7220 tval = strrchr (val, '=');
7221 if (!tval) {
7222 fprintf (stderr, "AOT : invalid format for readonly field '%s', missing =.\n", val);
7223 exit (1);
7225 rdv = g_new0 (ReadOnlyValue, 1);
7226 rdv->name = (char *)g_malloc0 (tval - val + 1);
7227 memcpy (rdv->name, val, tval - val);
7228 tval++;
7229 fval++;
7230 if (strncmp (tval, "i1", 2) == 0) {
7231 rdv->value.i1 = atoi (fval);
7232 rdv->type = MONO_TYPE_I1;
7233 } else if (strncmp (tval, "i2", 2) == 0) {
7234 rdv->value.i2 = atoi (fval);
7235 rdv->type = MONO_TYPE_I2;
7236 } else if (strncmp (tval, "i4", 2) == 0) {
7237 rdv->value.i4 = atoi (fval);
7238 rdv->type = MONO_TYPE_I4;
7239 } else {
7240 fprintf (stderr, "AOT : unsupported type for readonly field '%s'.\n", tval);
7241 exit (1);
7243 rdv->next = readonly_values;
7244 readonly_values = rdv;
7247 static gchar *
7248 clean_path (gchar * path)
7250 if (!path)
7251 return NULL;
7253 if (g_str_has_suffix (path, G_DIR_SEPARATOR_S))
7254 return path;
7256 gchar *clean = g_strconcat (path, G_DIR_SEPARATOR_S, NULL);
7257 g_free (path);
7259 return clean;
7262 static gchar *
7263 wrap_path (gchar * path)
7265 int len;
7266 if (!path)
7267 return NULL;
7269 // If the string contains no spaces, just return the original string.
7270 if (strstr (path, " ") == NULL)
7271 return path;
7273 // If the string is already wrapped in quotes, return it.
7274 len = strlen (path);
7275 if (len >= 2 && path[0] == '\"' && path[len-1] == '\"')
7276 return path;
7278 // If the string contains spaces, then wrap it in quotes.
7279 gchar *clean = g_strdup_printf ("\"%s\"", path);
7281 return clean;
7284 // Duplicate a char range and add it to a ptrarray, but only if it is nonempty
7285 static void
7286 ptr_array_add_range_if_nonempty(GPtrArray *args, gchar const *start, gchar const *end)
7288 ptrdiff_t len = end-start;
7289 if (len > 0)
7290 g_ptr_array_add (args, g_strndup (start, len));
7293 static GPtrArray *
7294 mono_aot_split_options (const char *aot_options)
7296 enum MonoAotOptionState {
7297 MONO_AOT_OPTION_STATE_DEFAULT,
7298 MONO_AOT_OPTION_STATE_STRING,
7299 MONO_AOT_OPTION_STATE_ESCAPE,
7302 GPtrArray *args = g_ptr_array_new ();
7303 enum MonoAotOptionState state = MONO_AOT_OPTION_STATE_DEFAULT;
7304 gchar const *opt_start = aot_options;
7305 gboolean end_of_string = FALSE;
7306 gchar cur;
7308 g_return_val_if_fail (aot_options != NULL, NULL);
7310 while ((cur = *aot_options) != '\0') {
7311 if (state == MONO_AOT_OPTION_STATE_ESCAPE)
7312 goto next;
7314 switch (cur) {
7315 case '"':
7316 // If we find a quote, then if we're in the default case then
7317 // it means we've found the start of a string, if not then it
7318 // means we've found the end of the string and should switch
7319 // back to the default case.
7320 switch (state) {
7321 case MONO_AOT_OPTION_STATE_DEFAULT:
7322 state = MONO_AOT_OPTION_STATE_STRING;
7323 break;
7324 case MONO_AOT_OPTION_STATE_STRING:
7325 state = MONO_AOT_OPTION_STATE_DEFAULT;
7326 break;
7327 case MONO_AOT_OPTION_STATE_ESCAPE:
7328 g_assert_not_reached ();
7329 break;
7331 break;
7332 case '\\':
7333 // If we've found an escaping operator, then this means we
7334 // should not process the next character if inside a string.
7335 if (state == MONO_AOT_OPTION_STATE_STRING)
7336 state = MONO_AOT_OPTION_STATE_ESCAPE;
7337 break;
7338 case ',':
7339 // If we're in the default state then this means we've found
7340 // an option, store it for later processing.
7341 if (state == MONO_AOT_OPTION_STATE_DEFAULT)
7342 goto new_opt;
7343 break;
7346 next:
7347 aot_options++;
7348 restart:
7349 // If the next character is end of string, then process the last option.
7350 if (*(aot_options) == '\0') {
7351 end_of_string = TRUE;
7352 goto new_opt;
7354 continue;
7356 new_opt:
7357 ptr_array_add_range_if_nonempty (args, opt_start, aot_options);
7358 opt_start = ++aot_options;
7359 if (end_of_string)
7360 break;
7361 goto restart; // Check for null and continue loop
7364 return args;
7367 static void
7368 mono_aot_parse_options (const char *aot_options, MonoAotOptions *opts)
7370 GPtrArray* args;
7372 args = mono_aot_split_options (aot_options ? aot_options : "");
7373 for (int i = 0; i < args->len; ++i) {
7374 const char *arg = (const char *)g_ptr_array_index (args, i);
7376 if (str_begins_with (arg, "outfile=")) {
7377 opts->outfile = g_strdup (arg + strlen ("outfile="));
7378 } else if (str_begins_with (arg, "llvm-outfile=")) {
7379 opts->llvm_outfile = g_strdup (arg + strlen ("llvm-outfile="));
7380 } else if (str_begins_with (arg, "temp-path=")) {
7381 opts->temp_path = clean_path (g_strdup (arg + strlen ("temp-path=")));
7382 } else if (str_begins_with (arg, "save-temps")) {
7383 opts->save_temps = TRUE;
7384 } else if (str_begins_with (arg, "keep-temps")) {
7385 opts->save_temps = TRUE;
7386 } else if (str_begins_with (arg, "write-symbols")) {
7387 opts->write_symbols = TRUE;
7388 } else if (str_begins_with (arg, "no-write-symbols")) {
7389 opts->write_symbols = FALSE;
7390 // Intentionally undocumented -- one-off experiment
7391 } else if (str_begins_with (arg, "metadata-only")) {
7392 opts->metadata_only = TRUE;
7393 } else if (str_begins_with (arg, "bind-to-runtime-version")) {
7394 opts->bind_to_runtime_version = TRUE;
7395 } else if (str_begins_with (arg, "full")) {
7396 opts->mode = MONO_AOT_MODE_FULL;
7397 } else if (str_begins_with (arg, "hybrid")) {
7398 opts->mode = MONO_AOT_MODE_HYBRID;
7399 } else if (str_begins_with (arg, "interp")) {
7400 opts->interp = TRUE;
7401 } else if (str_begins_with (arg, "threads=")) {
7402 opts->nthreads = atoi (arg + strlen ("threads="));
7403 } else if (str_begins_with (arg, "static")) {
7404 opts->static_link = TRUE;
7405 opts->no_dlsym = TRUE;
7406 } else if (str_begins_with (arg, "asmonly")) {
7407 opts->asm_only = TRUE;
7408 } else if (str_begins_with (arg, "asmwriter")) {
7409 opts->asm_writer = TRUE;
7410 } else if (str_begins_with (arg, "nodebug")) {
7411 opts->nodebug = TRUE;
7412 } else if (str_begins_with (arg, "dwarfdebug")) {
7413 opts->dwarf_debug = TRUE;
7414 // Intentionally undocumented -- No one remembers what this does. It appears to be ARM-only
7415 } else if (str_begins_with (arg, "nopagetrampolines")) {
7416 opts->use_trampolines_page = FALSE;
7417 } else if (str_begins_with (arg, "ntrampolines=")) {
7418 opts->ntrampolines = atoi (arg + strlen ("ntrampolines="));
7419 } else if (str_begins_with (arg, "nrgctx-trampolines=")) {
7420 opts->nrgctx_trampolines = atoi (arg + strlen ("nrgctx-trampolines="));
7421 } else if (str_begins_with (arg, "nrgctx-fetch-trampolines=")) {
7422 opts->nrgctx_fetch_trampolines = atoi (arg + strlen ("nrgctx-fetch-trampolines="));
7423 } else if (str_begins_with (arg, "nimt-trampolines=")) {
7424 opts->nimt_trampolines = atoi (arg + strlen ("nimt-trampolines="));
7425 } else if (str_begins_with (arg, "ngsharedvt-trampolines=")) {
7426 opts->ngsharedvt_arg_trampolines = atoi (arg + strlen ("ngsharedvt-trampolines="));
7427 } else if (str_begins_with (arg, "tool-prefix=")) {
7428 opts->tool_prefix = g_strdup (arg + strlen ("tool-prefix="));
7429 } else if (str_begins_with (arg, "ld-flags=")) {
7430 opts->ld_flags = g_strdup (arg + strlen ("ld-flags="));
7431 } else if (str_begins_with (arg, "soft-debug")) {
7432 opts->soft_debug = TRUE;
7433 // Intentionally undocumented x2-- deprecated
7434 } else if (str_begins_with (arg, "gen-seq-points-file=")) {
7435 fprintf (stderr, "Mono Warning: aot option gen-seq-points-file= is deprecated.\n");
7436 } else if (str_begins_with (arg, "gen-seq-points-file")) {
7437 fprintf (stderr, "Mono Warning: aot option gen-seq-points-file is deprecated.\n");
7438 } else if (str_begins_with (arg, "msym-dir=")) {
7439 mini_debug_options.no_seq_points_compact_data = FALSE;
7440 opts->gen_msym_dir = TRUE;
7441 opts->gen_msym_dir_path = g_strdup (arg + strlen ("msym_dir="));;
7442 } else if (str_begins_with (arg, "direct-pinvoke")) {
7443 opts->direct_pinvoke = TRUE;
7444 } else if (str_begins_with (arg, "direct-icalls")) {
7445 opts->direct_icalls = TRUE;
7446 } else if (str_begins_with (arg, "no-direct-calls")) {
7447 opts->no_direct_calls = TRUE;
7448 } else if (str_begins_with (arg, "print-skipped")) {
7449 opts->print_skipped_methods = TRUE;
7450 } else if (str_begins_with (arg, "stats")) {
7451 opts->stats = TRUE;
7452 // Intentionally undocumented-- has no known function other than to debug the compiler
7453 } else if (str_begins_with (arg, "no-instances")) {
7454 opts->no_instances = TRUE;
7455 // Intentionally undocumented x4-- Used for internal debugging of compiler
7456 } else if (str_begins_with (arg, "log-generics")) {
7457 opts->log_generics = TRUE;
7458 } else if (str_begins_with (arg, "log-instances=")) {
7459 opts->log_instances = TRUE;
7460 opts->instances_logfile_path = g_strdup (arg + strlen ("log-instances="));
7461 } else if (str_begins_with (arg, "log-instances")) {
7462 opts->log_instances = TRUE;
7463 } else if (str_begins_with (arg, "internal-logfile=")) {
7464 opts->logfile = g_strdup (arg + strlen ("internal-logfile="));
7465 } else if (str_begins_with (arg, "dedup-skip")) {
7466 opts->dedup = TRUE;
7467 } else if (str_begins_with (arg, "dedup-include=")) {
7468 opts->dedup_include = g_strdup (arg + strlen ("dedup-include="));
7469 } else if (str_begins_with (arg, "mtriple=")) {
7470 opts->mtriple = g_strdup (arg + strlen ("mtriple="));
7471 } else if (str_begins_with (arg, "llvm-path=")) {
7472 opts->llvm_path = clean_path (g_strdup (arg + strlen ("llvm-path=")));
7473 } else if (!strcmp (arg, "try-llvm")) {
7474 // If we can load LLVM, use it
7475 // Note: if you call this function from anywhere but mono_compile_assembly,
7476 // this will only set the try_llvm attribute and not do the probing / set the
7477 // attribute.
7478 opts->try_llvm = TRUE;
7479 } else if (!strcmp (arg, "llvm")) {
7480 opts->llvm = TRUE;
7481 } else if (str_begins_with (arg, "readonly-value=")) {
7482 add_readonly_value (opts, arg + strlen ("readonly-value="));
7483 } else if (str_begins_with (arg, "info")) {
7484 printf ("AOT target setup: %s.\n", AOT_TARGET_STR);
7485 exit (0);
7486 // Intentionally undocumented: Used for precise stack maps, which are not available yet
7487 } else if (str_begins_with (arg, "gc-maps")) {
7488 mini_gc_enable_gc_maps_for_aot ();
7489 // Intentionally undocumented: Used for internal debugging
7490 } else if (str_begins_with (arg, "dump")) {
7491 opts->dump_json = TRUE;
7492 } else if (str_begins_with (arg, "llvmonly")) {
7493 opts->mode = MONO_AOT_MODE_FULL;
7494 opts->llvm = TRUE;
7495 opts->llvm_only = TRUE;
7496 } else if (str_begins_with (arg, "data-outfile=")) {
7497 opts->data_outfile = g_strdup (arg + strlen ("data-outfile="));
7498 } else if (str_begins_with (arg, "profile=")) {
7499 opts->profile_files = g_list_append (opts->profile_files, g_strdup (arg + strlen ("profile=")));
7500 } else if (!strcmp (arg, "profile-only")) {
7501 opts->profile_only = TRUE;
7502 } else if (!strcmp (arg, "verbose")) {
7503 opts->verbose = TRUE;
7504 } else if (str_begins_with (arg, "llvmopts=")){
7505 opts->llvm_opts = g_strdup (arg + strlen ("llvmopts="));
7506 } else if (str_begins_with (arg, "help") || str_begins_with (arg, "?")) {
7507 printf ("Supported options for --aot:\n");
7508 printf (" asmonly\n");
7509 printf (" bind-to-runtime-version\n");
7510 printf (" bitcode\n");
7511 printf (" data-outfile=\n");
7512 printf (" direct-icalls\n");
7513 printf (" direct-pinvoke\n");
7514 printf (" dwarfdebug\n");
7515 printf (" full\n");
7516 printf (" hybrid\n");
7517 printf (" info\n");
7518 printf (" keep-temps\n");
7519 printf (" llvm\n");
7520 printf (" llvmonly\n");
7521 printf (" llvm-outfile=\n");
7522 printf (" llvm-path=\n");
7523 printf (" msym-dir=\n");
7524 printf (" mtriple\n");
7525 printf (" nimt-trampolines=\n");
7526 printf (" nodebug\n");
7527 printf (" no-direct-calls\n");
7528 printf (" no-write-symbols\n");
7529 printf (" nrgctx-trampolines=\n");
7530 printf (" nrgctx-fetch-trampolines=\n");
7531 printf (" ngsharedvt-trampolines=\n");
7532 printf (" ntrampolines=\n");
7533 printf (" outfile=\n");
7534 printf (" profile=\n");
7535 printf (" profile-only\n");
7536 printf (" print-skipped-methods\n");
7537 printf (" readonly-value=\n");
7538 printf (" save-temps\n");
7539 printf (" soft-debug\n");
7540 printf (" static\n");
7541 printf (" stats\n");
7542 printf (" temp-path=\n");
7543 printf (" tool-prefix=\n");
7544 printf (" threads=\n");
7545 printf (" write-symbols\n");
7546 printf (" verbose\n");
7547 printf (" help/?\n");
7548 exit (0);
7549 } else {
7550 fprintf (stderr, "AOT : Unknown argument '%s'.\n", arg);
7551 exit (1);
7554 g_free ((gpointer) arg);
7557 if (opts->use_trampolines_page) {
7558 opts->ntrampolines = 0;
7559 opts->nrgctx_trampolines = 0;
7560 opts->nimt_trampolines = 0;
7561 opts->ngsharedvt_arg_trampolines = 0;
7564 g_ptr_array_free (args, /*free_seg=*/TRUE);
7567 static void
7568 add_token_info_hash (gpointer key, gpointer value, gpointer user_data)
7570 MonoMethod *method = (MonoMethod*)key;
7571 MonoJumpInfoToken *ji = (MonoJumpInfoToken*)value;
7572 MonoAotCompile *acfg = (MonoAotCompile *)user_data;
7573 MonoJumpInfoToken *new_ji;
7575 new_ji = (MonoJumpInfoToken *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfoToken));
7576 new_ji->image = ji->image;
7577 new_ji->token = ji->token;
7578 g_hash_table_insert (acfg->token_info_hash, method, new_ji);
7581 static gboolean
7582 can_encode_class (MonoAotCompile *acfg, MonoClass *klass)
7584 if (m_class_get_type_token (klass))
7585 return TRUE;
7586 if ((m_class_get_byval_arg (klass)->type == MONO_TYPE_VAR) || (m_class_get_byval_arg (klass)->type == MONO_TYPE_MVAR) || (m_class_get_byval_arg (klass)->type == MONO_TYPE_PTR))
7587 return TRUE;
7588 if (m_class_get_rank (klass))
7589 return can_encode_class (acfg, m_class_get_element_class (klass));
7590 return FALSE;
7593 static gboolean
7594 can_encode_method (MonoAotCompile *acfg, MonoMethod *method)
7596 if (method->wrapper_type) {
7597 switch (method->wrapper_type) {
7598 case MONO_WRAPPER_NONE:
7599 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
7600 case MONO_WRAPPER_XDOMAIN_INVOKE:
7601 case MONO_WRAPPER_STFLD:
7602 case MONO_WRAPPER_LDFLD:
7603 case MONO_WRAPPER_LDFLDA:
7604 case MONO_WRAPPER_STELEMREF:
7605 case MONO_WRAPPER_PROXY_ISINST:
7606 case MONO_WRAPPER_ALLOC:
7607 case MONO_WRAPPER_REMOTING_INVOKE:
7608 case MONO_WRAPPER_UNKNOWN:
7609 case MONO_WRAPPER_WRITE_BARRIER:
7610 case MONO_WRAPPER_DELEGATE_INVOKE:
7611 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
7612 case MONO_WRAPPER_DELEGATE_END_INVOKE:
7613 case MONO_WRAPPER_SYNCHRONIZED:
7614 break;
7615 case MONO_WRAPPER_MANAGED_TO_MANAGED:
7616 case MONO_WRAPPER_CASTCLASS: {
7617 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
7619 if (info)
7620 return TRUE;
7621 else
7622 return FALSE;
7623 break;
7625 default:
7626 //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
7627 return FALSE;
7629 } else {
7630 if (!method->token) {
7631 /* The method is part of a constructed type like Int[,].Set (). */
7632 if (!g_hash_table_lookup (acfg->token_info_hash, method)) {
7633 if (m_class_get_rank (method->klass))
7634 return TRUE;
7635 return FALSE;
7639 return TRUE;
7642 static gboolean
7643 can_encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
7645 switch (patch_info->type) {
7646 case MONO_PATCH_INFO_METHOD:
7647 case MONO_PATCH_INFO_METHODCONST:
7648 case MONO_PATCH_INFO_METHOD_CODE_SLOT: {
7649 MonoMethod *method = patch_info->data.method;
7651 return can_encode_method (acfg, method);
7653 case MONO_PATCH_INFO_VTABLE:
7654 case MONO_PATCH_INFO_CLASS:
7655 case MONO_PATCH_INFO_IID:
7656 case MONO_PATCH_INFO_ADJUSTED_IID:
7657 if (!can_encode_class (acfg, patch_info->data.klass)) {
7658 //printf ("Skip: %s\n", mono_type_full_name (m_class_get_byval_arg (patch_info->data.klass)));
7659 return FALSE;
7661 break;
7662 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE: {
7663 if (!can_encode_class (acfg, patch_info->data.del_tramp->klass)) {
7664 //printf ("Skip: %s\n", mono_type_full_name (m_class_get_byval_arg (patch_info->data.klass)));
7665 return FALSE;
7667 break;
7669 case MONO_PATCH_INFO_RGCTX_FETCH:
7670 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX: {
7671 MonoJumpInfoRgctxEntry *entry = patch_info->data.rgctx_entry;
7673 if (!can_encode_method (acfg, entry->method))
7674 return FALSE;
7675 if (!can_encode_patch (acfg, entry->data))
7676 return FALSE;
7677 break;
7679 default:
7680 break;
7683 return TRUE;
7686 static gboolean
7687 is_concrete_type (MonoType *t)
7689 MonoClass *klass;
7690 int i;
7692 if (t->type == MONO_TYPE_VAR || t->type == MONO_TYPE_MVAR)
7693 return FALSE;
7694 if (t->type == MONO_TYPE_GENERICINST) {
7695 MonoGenericContext *orig_ctx;
7696 MonoGenericInst *inst;
7697 MonoType *arg;
7699 if (!MONO_TYPE_ISSTRUCT (t))
7700 return TRUE;
7701 klass = mono_class_from_mono_type (t);
7702 orig_ctx = &mono_class_get_generic_class (klass)->context;
7704 inst = orig_ctx->class_inst;
7705 if (inst) {
7706 for (i = 0; i < inst->type_argc; ++i) {
7707 arg = mini_get_underlying_type (inst->type_argv [i]);
7708 if (!is_concrete_type (arg))
7709 return FALSE;
7712 inst = orig_ctx->method_inst;
7713 if (inst) {
7714 for (i = 0; i < inst->type_argc; ++i) {
7715 arg = mini_get_underlying_type (inst->type_argv [i]);
7716 if (!is_concrete_type (arg))
7717 return FALSE;
7721 return TRUE;
7724 /* LOCKING: Assumes the loader lock is held */
7725 static void
7726 add_gsharedvt_wrappers (MonoAotCompile *acfg, MonoMethodSignature *sig, gboolean gsharedvt_in, gboolean gsharedvt_out, gboolean interp_in)
7728 MonoMethod *wrapper;
7729 gboolean concrete = TRUE;
7730 gboolean add_in = gsharedvt_in;
7731 gboolean add_out = gsharedvt_out;
7733 if (gsharedvt_in && g_hash_table_lookup (acfg->gsharedvt_in_signatures, sig))
7734 add_in = FALSE;
7735 if (gsharedvt_out && g_hash_table_lookup (acfg->gsharedvt_out_signatures, sig))
7736 add_out = FALSE;
7738 if (!add_in && !add_out)
7739 return;
7741 if (mini_is_gsharedvt_variable_signature (sig))
7742 return;
7744 if (add_in)
7745 g_hash_table_insert (acfg->gsharedvt_in_signatures, sig, sig);
7746 if (add_out)
7747 g_hash_table_insert (acfg->gsharedvt_out_signatures, sig, sig);
7749 if (sig->has_type_parameters) {
7750 /* For signatures created during generic sharing, convert them to a concrete signature if possible */
7751 MonoMethodSignature *copy = mono_metadata_signature_dup (sig);
7752 int i;
7754 //printf ("%s\n", mono_signature_full_name (sig));
7756 copy->ret = mini_get_underlying_type (sig->ret);
7757 if (!is_concrete_type (copy->ret))
7758 concrete = FALSE;
7759 for (i = 0; i < sig->param_count; ++i) {
7760 copy->params [i] = mini_get_underlying_type (sig->params [i]);
7761 if (!is_concrete_type (copy->params [i]))
7762 concrete = FALSE;
7764 copy->has_type_parameters = 0;
7765 if (!concrete)
7766 return;
7767 sig = copy;
7770 //printf ("%s\n", mono_signature_full_name (sig));
7772 if (gsharedvt_in) {
7773 wrapper = mini_get_gsharedvt_in_sig_wrapper (sig);
7774 add_extra_method (acfg, wrapper);
7776 if (gsharedvt_out) {
7777 wrapper = mini_get_gsharedvt_out_sig_wrapper (sig);
7778 add_extra_method (acfg, wrapper);
7780 if (interp_in) {
7781 wrapper = mini_get_interp_in_wrapper (sig);
7782 add_extra_method (acfg, wrapper);
7783 //printf ("X: %s\n", mono_method_full_name (wrapper, 1));
7788 * compile_method:
7790 * AOT compile a given method.
7791 * This function might be called by multiple threads, so it must be thread-safe.
7793 static void
7794 compile_method (MonoAotCompile *acfg, MonoMethod *method)
7796 MonoCompile *cfg;
7797 MonoJumpInfo *patch_info;
7798 gboolean skip;
7799 int index, depth;
7800 MonoMethod *wrapped;
7801 GTimer *jit_timer;
7802 JitFlags flags;
7804 if (acfg->aot_opts.metadata_only)
7805 return;
7807 mono_acfg_lock (acfg);
7808 index = get_method_index (acfg, method);
7809 mono_acfg_unlock (acfg);
7811 /* fixme: maybe we can also precompile wrapper methods */
7812 if ((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
7813 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
7814 (method->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
7815 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
7816 return;
7819 if (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL)
7820 return;
7822 wrapped = mono_marshal_method_from_wrapper (method);
7823 if (wrapped && (wrapped->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) && wrapped->is_generic)
7824 // FIXME: The wrapper should be generic too, but it is not
7825 return;
7827 if (method->wrapper_type == MONO_WRAPPER_COMINTEROP)
7828 return;
7830 if (acfg->aot_opts.profile_only && !method->is_inflated && !g_hash_table_lookup (acfg->profile_methods, method))
7831 return;
7833 mono_atomic_inc_i32 (&acfg->stats.mcount);
7835 #if 0
7836 if (method->is_generic || mono_class_is_gtd (method->klass)) {
7837 mono_atomic_inc_i32 (&acfg->stats.genericcount);
7838 return;
7840 #endif
7842 //acfg->aot_opts.print_skipped_methods = TRUE;
7845 * Since these methods are the only ones which are compiled with
7846 * AOT support, and they are not used by runtime startup/shutdown code,
7847 * the runtime will not see AOT methods during AOT compilation,so it
7848 * does not need to support them by creating a fake GOT etc.
7850 flags = JIT_FLAG_AOT;
7851 if (mono_aot_mode_is_full (&acfg->aot_opts))
7852 flags = (JitFlags)(flags | JIT_FLAG_FULL_AOT);
7853 if (acfg->llvm)
7854 flags = (JitFlags)(flags | JIT_FLAG_LLVM);
7855 if (acfg->aot_opts.llvm_only)
7856 flags = (JitFlags)(flags | JIT_FLAG_LLVM_ONLY | JIT_FLAG_EXPLICIT_NULL_CHECKS);
7857 if (acfg->aot_opts.no_direct_calls)
7858 flags = (JitFlags)(flags | JIT_FLAG_NO_DIRECT_ICALLS);
7859 if (acfg->aot_opts.direct_pinvoke)
7860 flags = (JitFlags)(flags | JIT_FLAG_DIRECT_PINVOKE);
7862 jit_timer = mono_time_track_start ();
7863 cfg = mini_method_compile (method, acfg->opts, mono_get_root_domain (), flags, 0, index);
7864 mono_time_track_end (&mono_jit_stats.jit_time, jit_timer);
7866 if (cfg->exception_type == MONO_EXCEPTION_GENERIC_SHARING_FAILED) {
7867 if (acfg->aot_opts.print_skipped_methods)
7868 printf ("Skip (gshared failure): %s (%s)\n", mono_method_get_full_name (method), cfg->exception_message);
7869 mono_atomic_inc_i32 (&acfg->stats.genericcount);
7870 return;
7872 if (cfg->exception_type != MONO_EXCEPTION_NONE) {
7873 /* Some instances cannot be JITted due to constraints etc. */
7874 if (!method->is_inflated)
7875 report_loader_error (acfg, &cfg->error, FALSE, "Unable to compile method '%s' due to: '%s'.\n", mono_method_get_full_name (method), mono_error_get_message (&cfg->error));
7876 /* Let the exception happen at runtime */
7877 return;
7880 if (cfg->disable_aot) {
7881 if (acfg->aot_opts.print_skipped_methods)
7882 printf ("Skip (disabled): %s\n", mono_method_get_full_name (method));
7883 mono_atomic_inc_i32 (&acfg->stats.ocount);
7884 return;
7886 cfg->method_index = index;
7888 /* Nullify patches which need no aot processing */
7889 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7890 switch (patch_info->type) {
7891 case MONO_PATCH_INFO_LABEL:
7892 case MONO_PATCH_INFO_BB:
7893 patch_info->type = MONO_PATCH_INFO_NONE;
7894 break;
7895 default:
7896 break;
7900 /* Collect method->token associations from the cfg */
7901 mono_acfg_lock (acfg);
7902 g_hash_table_foreach (cfg->token_info_hash, add_token_info_hash, acfg);
7903 mono_acfg_unlock (acfg);
7904 g_hash_table_destroy (cfg->token_info_hash);
7905 cfg->token_info_hash = NULL;
7908 * Check for absolute addresses.
7910 skip = FALSE;
7911 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7912 switch (patch_info->type) {
7913 case MONO_PATCH_INFO_ABS:
7914 /* unable to handle this */
7915 skip = TRUE;
7916 break;
7917 default:
7918 break;
7922 if (skip) {
7923 if (acfg->aot_opts.print_skipped_methods)
7924 printf ("Skip (abs call): %s\n", mono_method_get_full_name (method));
7925 mono_atomic_inc_i32 (&acfg->stats.abscount);
7926 return;
7929 /* Lock for the rest of the code */
7930 mono_acfg_lock (acfg);
7932 if (cfg->gsharedvt)
7933 acfg->stats.method_categories [METHOD_CAT_GSHAREDVT] ++;
7934 else if (cfg->gshared)
7935 acfg->stats.method_categories [METHOD_CAT_INST] ++;
7936 else if (cfg->method->wrapper_type)
7937 acfg->stats.method_categories [METHOD_CAT_WRAPPER] ++;
7938 else
7939 acfg->stats.method_categories [METHOD_CAT_NORMAL] ++;
7942 * Check for methods/klasses we can't encode.
7944 skip = FALSE;
7945 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7946 if (!can_encode_patch (acfg, patch_info))
7947 skip = TRUE;
7950 if (skip) {
7951 if (acfg->aot_opts.print_skipped_methods)
7952 printf ("Skip (patches): %s\n", mono_method_get_full_name (method));
7953 acfg->stats.ocount++;
7954 mono_acfg_unlock (acfg);
7955 return;
7958 if (!cfg->compile_llvm)
7959 acfg->has_jitted_code = TRUE;
7961 if (method->is_inflated && acfg->aot_opts.log_instances) {
7962 if (acfg->instances_logfile)
7963 fprintf (acfg->instances_logfile, "%s ### %d\n", mono_method_get_full_name (method), cfg->code_size);
7964 else
7965 printf ("%s ### %d\n", mono_method_get_full_name (method), cfg->code_size);
7968 /* Adds generic instances referenced by this method */
7970 * The depth is used to avoid infinite loops when generic virtual recursion is
7971 * encountered.
7973 depth = GPOINTER_TO_UINT (g_hash_table_lookup (acfg->method_depth, method));
7974 if (!acfg->aot_opts.no_instances && depth < 32 && (mono_aot_mode_is_full (&acfg->aot_opts) || mono_aot_mode_is_hybrid (&acfg->aot_opts))) {
7975 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
7976 switch (patch_info->type) {
7977 case MONO_PATCH_INFO_RGCTX_FETCH:
7978 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX:
7979 case MONO_PATCH_INFO_METHOD:
7980 case MONO_PATCH_INFO_METHOD_RGCTX: {
7981 MonoMethod *m = NULL;
7983 if (patch_info->type == MONO_PATCH_INFO_RGCTX_FETCH || patch_info->type == MONO_PATCH_INFO_RGCTX_SLOT_INDEX) {
7984 MonoJumpInfoRgctxEntry *e = patch_info->data.rgctx_entry;
7986 if (e->info_type == MONO_RGCTX_INFO_GENERIC_METHOD_CODE)
7987 m = e->data->data.method;
7988 } else {
7989 m = patch_info->data.method;
7992 if (!m)
7993 break;
7994 if (m->is_inflated && (mono_aot_mode_is_full (&acfg->aot_opts) || mono_aot_mode_is_hybrid (&acfg->aot_opts))) {
7995 if (!(mono_class_generic_sharing_enabled (m->klass) &&
7996 mono_method_is_generic_sharable_full (m, FALSE, FALSE, FALSE)) &&
7997 (!method_has_type_vars (m) || mono_method_is_generic_sharable_full (m, TRUE, TRUE, FALSE))) {
7998 if (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
7999 if (mono_aot_mode_is_full (&acfg->aot_opts) && !method_has_type_vars (m))
8000 add_extra_method_with_depth (acfg, mono_marshal_get_native_wrapper (m, TRUE, TRUE), depth + 1);
8001 } else {
8002 add_extra_method_with_depth (acfg, m, depth + 1);
8003 add_types_from_method_header (acfg, m);
8006 add_generic_class_with_depth (acfg, m->klass, depth + 5, "method");
8008 if (m->wrapper_type == MONO_WRAPPER_MANAGED_TO_MANAGED) {
8009 WrapperInfo *info = mono_marshal_get_wrapper_info (m);
8011 if (info && info->subtype == WRAPPER_SUBTYPE_ELEMENT_ADDR)
8012 add_extra_method_with_depth (acfg, m, depth + 1);
8014 break;
8016 case MONO_PATCH_INFO_VTABLE: {
8017 MonoClass *klass = patch_info->data.klass;
8019 if (mono_class_is_ginst (klass) && !mini_class_is_generic_sharable (klass))
8020 add_generic_class_with_depth (acfg, klass, depth + 5, "vtable");
8021 break;
8023 case MONO_PATCH_INFO_SFLDA: {
8024 MonoClass *klass = patch_info->data.field->parent;
8026 /* The .cctor needs to run at runtime. */
8027 if (mono_class_is_ginst (klass) && !mono_generic_context_is_sharable_full (&mono_class_get_generic_class (klass)->context, FALSE, FALSE) && mono_class_get_cctor (klass))
8028 add_extra_method_with_depth (acfg, mono_class_get_cctor (klass), depth + 1);
8029 break;
8031 default:
8032 break;
8037 /* Determine whenever the method has GOT slots */
8038 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
8039 switch (patch_info->type) {
8040 case MONO_PATCH_INFO_GOT_OFFSET:
8041 case MONO_PATCH_INFO_NONE:
8042 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
8043 case MONO_PATCH_INFO_GC_NURSERY_START:
8044 case MONO_PATCH_INFO_GC_NURSERY_BITS:
8045 break;
8046 case MONO_PATCH_INFO_IMAGE:
8047 /* The assembly is stored in GOT slot 0 */
8048 if (patch_info->data.image != acfg->image)
8049 cfg->has_got_slots = TRUE;
8050 break;
8051 default:
8052 if (!is_plt_patch (patch_info) || (cfg->compile_llvm && acfg->aot_opts.llvm_only))
8053 cfg->has_got_slots = TRUE;
8054 break;
8058 if (!cfg->has_got_slots)
8059 mono_atomic_inc_i32 (&acfg->stats.methods_without_got_slots);
8061 /* Add gsharedvt wrappers for signatures used by the method */
8062 if (acfg->aot_opts.llvm_only) {
8063 GSList *l;
8065 if (!cfg->method->wrapper_type || cfg->method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE)
8066 /* These only need out wrappers */
8067 add_gsharedvt_wrappers (acfg, mono_method_signature (cfg->method), FALSE, TRUE, FALSE);
8069 for (l = cfg->signatures; l; l = l->next) {
8070 MonoMethodSignature *sig = mono_metadata_signature_dup ((MonoMethodSignature*)l->data);
8072 /* These only need in wrappers */
8073 add_gsharedvt_wrappers (acfg, sig, TRUE, FALSE, FALSE);
8075 } else if (mono_aot_mode_is_full (&acfg->aot_opts) && mono_aot_mode_is_interp (&acfg->aot_opts)) {
8076 /* The interpreter uses these wrappers to call aot-ed code */
8077 if (!cfg->method->wrapper_type || cfg->method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE)
8078 add_gsharedvt_wrappers (acfg, mono_method_signature (cfg->method), FALSE, TRUE, TRUE);
8082 * FIXME: Instead of this mess, allocate the patches from the aot mempool.
8084 /* Make a copy of the patch info which is in the mempool */
8086 MonoJumpInfo *patches = NULL, *patches_end = NULL;
8088 for (patch_info = cfg->patch_info; patch_info; patch_info = patch_info->next) {
8089 MonoJumpInfo *new_patch_info = mono_patch_info_dup_mp (acfg->mempool, patch_info);
8091 if (!patches)
8092 patches = new_patch_info;
8093 else
8094 patches_end->next = new_patch_info;
8095 patches_end = new_patch_info;
8097 cfg->patch_info = patches;
8099 /* Make a copy of the unwind info */
8101 GSList *l, *unwind_ops;
8102 MonoUnwindOp *op;
8104 unwind_ops = NULL;
8105 for (l = cfg->unwind_ops; l; l = l->next) {
8106 op = (MonoUnwindOp *)mono_mempool_alloc (acfg->mempool, sizeof (MonoUnwindOp));
8107 memcpy (op, l->data, sizeof (MonoUnwindOp));
8108 unwind_ops = g_slist_prepend_mempool (acfg->mempool, unwind_ops, op);
8110 cfg->unwind_ops = g_slist_reverse (unwind_ops);
8112 /* Make a copy of the argument/local info */
8114 ERROR_DECL (error);
8115 MonoInst **args, **locals;
8116 MonoMethodSignature *sig;
8117 MonoMethodHeader *header;
8118 int i;
8120 sig = mono_method_signature (method);
8121 args = (MonoInst **)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * (sig->param_count + sig->hasthis));
8122 for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
8123 args [i] = (MonoInst *)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
8124 memcpy (args [i], cfg->args [i], sizeof (MonoInst));
8126 cfg->args = args;
8128 header = mono_method_get_header_checked (method, error);
8129 mono_error_assert_ok (error); /* FIXME don't swallow the error */
8130 locals = (MonoInst **)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst*) * header->num_locals);
8131 for (i = 0; i < header->num_locals; ++i) {
8132 locals [i] = (MonoInst *)mono_mempool_alloc (acfg->mempool, sizeof (MonoInst));
8133 memcpy (locals [i], cfg->locals [i], sizeof (MonoInst));
8135 mono_metadata_free_mh (header);
8136 cfg->locals = locals;
8139 /* Free some fields used by cfg to conserve memory */
8140 mono_empty_compile (cfg);
8142 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
8144 while (index >= acfg->cfgs_size) {
8145 MonoCompile **new_cfgs;
8146 int new_size;
8148 new_size = acfg->cfgs_size * 2;
8149 new_cfgs = g_new0 (MonoCompile*, new_size);
8150 memcpy (new_cfgs, acfg->cfgs, sizeof (MonoCompile*) * acfg->cfgs_size);
8151 g_free (acfg->cfgs);
8152 acfg->cfgs = new_cfgs;
8153 acfg->cfgs_size = new_size;
8155 acfg->cfgs [index] = cfg;
8157 g_hash_table_insert (acfg->method_to_cfg, cfg->orig_method, cfg);
8159 /* Update global stats while holding a lock. */
8160 mono_update_jit_stats (cfg);
8163 if (cfg->orig_method->wrapper_type)
8164 g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
8167 mono_acfg_unlock (acfg);
8169 mono_atomic_inc_i32 (&acfg->stats.ccount);
8172 static mono_thread_start_return_t WINAPI
8173 compile_thread_main (gpointer user_data)
8175 MonoAotCompile *acfg = ((MonoAotCompile **)user_data) [0];
8176 GPtrArray *methods = ((GPtrArray **)user_data) [1];
8177 int i;
8179 ERROR_DECL (error);
8180 MonoInternalThread *internal = mono_thread_internal_current ();
8181 MonoString *str = mono_string_new_checked (mono_domain_get (), "AOT compiler", error);
8182 mono_error_assert_ok (error);
8183 mono_thread_set_name_internal (internal, str, TRUE, FALSE, error);
8184 mono_error_assert_ok (error);
8186 for (i = 0; i < methods->len; ++i)
8187 compile_method (acfg, (MonoMethod *)g_ptr_array_index (methods, i));
8189 return 0;
8192 /* Used by the LLVM backend */
8193 guint32
8194 mono_aot_get_got_offset (MonoJumpInfo *ji)
8196 return get_got_offset (llvm_acfg, TRUE, ji);
8200 * mono_aot_is_shared_got_offset:
8202 * Return whenever OFFSET refers to a GOT slot which is preinitialized
8203 * when the AOT image is loaded.
8205 gboolean
8206 mono_aot_is_shared_got_offset (int offset)
8208 return offset < llvm_acfg->nshared_got_entries;
8211 char*
8212 mono_aot_get_method_name (MonoCompile *cfg)
8214 if (llvm_acfg->aot_opts.static_link)
8215 /* Include the assembly name too to avoid duplicate symbol errors */
8216 return g_strdup_printf ("%s_%s", llvm_acfg->assembly_name_sym, get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash));
8217 else
8218 return get_debug_sym (cfg->orig_method, "", llvm_acfg->method_label_hash);
8222 * mono_aot_is_linkonce_method:
8224 * Return whenever METHOD should be emitted with linkonce linkage,
8225 * eliminating duplicate copies when compiling in static mode.
8227 gboolean
8228 mono_aot_is_linkonce_method (MonoMethod *method)
8230 return FALSE;
8231 #if 0
8232 WrapperInfo *info;
8234 // FIXME: Add more cases
8235 if (method->wrapper_type != MONO_WRAPPER_UNKNOWN)
8236 return FALSE;
8237 info = mono_marshal_get_wrapper_info (method);
8238 if ((info && (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG || info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG)))
8239 return TRUE;
8240 return FALSE;
8241 #endif
8244 static gboolean
8245 append_mangled_type (GString *s, MonoType *t)
8247 if (t->byref)
8248 g_string_append_printf (s, "b");
8249 switch (t->type) {
8250 case MONO_TYPE_VOID:
8251 g_string_append_printf (s, "void_");
8252 break;
8253 case MONO_TYPE_I1:
8254 g_string_append_printf (s, "i1");
8255 break;
8256 case MONO_TYPE_U1:
8257 g_string_append_printf (s, "u1");
8258 break;
8259 case MONO_TYPE_I2:
8260 g_string_append_printf (s, "i2");
8261 break;
8262 case MONO_TYPE_U2:
8263 g_string_append_printf (s, "u2");
8264 break;
8265 case MONO_TYPE_I4:
8266 g_string_append_printf (s, "i4");
8267 break;
8268 case MONO_TYPE_U4:
8269 g_string_append_printf (s, "u4");
8270 break;
8271 case MONO_TYPE_I8:
8272 g_string_append_printf (s, "i8");
8273 break;
8274 case MONO_TYPE_U8:
8275 g_string_append_printf (s, "u8");
8276 break;
8277 case MONO_TYPE_I:
8278 g_string_append_printf (s, "ii");
8279 break;
8280 case MONO_TYPE_U:
8281 g_string_append_printf (s, "ui");
8282 break;
8283 case MONO_TYPE_R4:
8284 g_string_append_printf (s, "fl");
8285 break;
8286 case MONO_TYPE_R8:
8287 g_string_append_printf (s, "do");
8288 break;
8289 default: {
8290 char *fullname = mono_type_full_name (t);
8291 GString *temp;
8292 char *temps;
8293 int i, len;
8296 * Have to create a mangled name which is:
8297 * - a valid symbol
8298 * - unique
8300 temp = g_string_new ("");
8301 len = strlen (fullname);
8302 for (i = 0; i < len; ++i) {
8303 char c = fullname [i];
8304 if (isalnum (c)) {
8305 g_string_append_c (temp, c);
8306 } else if (c == '_') {
8307 g_string_append_c (temp, '_');
8308 g_string_append_c (temp, '_');
8309 } else {
8310 g_string_append_c (temp, '_');
8311 g_string_append_printf (temp, "%x", (int)c);
8314 temps = g_string_free (temp, FALSE);
8315 /* Include the length to avoid different length type names aliasing each other */
8316 g_string_append_printf (s, "cl%x_%s_", strlen (temps), temps);
8317 g_free (temps);
8320 if (t->attrs)
8321 g_string_append_printf (s, "_attrs_%d", t->attrs);
8322 return TRUE;
8325 static gboolean
8326 append_mangled_signature (GString *s, MonoMethodSignature *sig)
8328 int i;
8329 gboolean supported;
8331 supported = append_mangled_type (s, sig->ret);
8332 if (!supported)
8333 return FALSE;
8334 if (sig->hasthis)
8335 g_string_append_printf (s, "this_");
8336 if (sig->pinvoke)
8337 g_string_append_printf (s, "pinvoke_");
8338 for (i = 0; i < sig->param_count; ++i) {
8339 supported = append_mangled_type (s, sig->params [i]);
8340 if (!supported)
8341 return FALSE;
8344 return TRUE;
8347 static void
8348 append_mangled_wrapper_type (GString *s, guint32 wrapper_type)
8350 const char *label;
8352 switch (wrapper_type) {
8353 case MONO_WRAPPER_REMOTING_INVOKE:
8354 label = "remoting_invoke";
8355 break;
8356 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
8357 label = "remoting_invoke_check";
8358 break;
8359 case MONO_WRAPPER_XDOMAIN_INVOKE:
8360 label = "remoting_invoke_xdomain";
8361 break;
8362 case MONO_WRAPPER_PROXY_ISINST:
8363 label = "proxy_isinst";
8364 break;
8365 case MONO_WRAPPER_LDFLD:
8366 label = "ldfld";
8367 break;
8368 case MONO_WRAPPER_LDFLDA:
8369 label = "ldflda";
8370 break;
8371 case MONO_WRAPPER_STFLD:
8372 label = "stfld";
8373 break;
8374 case MONO_WRAPPER_ALLOC:
8375 label = "alloc";
8376 break;
8377 case MONO_WRAPPER_WRITE_BARRIER:
8378 label = "write_barrier";
8379 break;
8380 case MONO_WRAPPER_STELEMREF:
8381 label = "stelemref";
8382 break;
8383 case MONO_WRAPPER_UNKNOWN:
8384 label = "unknown";
8385 break;
8386 case MONO_WRAPPER_MANAGED_TO_NATIVE:
8387 label = "man2native";
8388 break;
8389 case MONO_WRAPPER_SYNCHRONIZED:
8390 label = "synch";
8391 break;
8392 case MONO_WRAPPER_MANAGED_TO_MANAGED:
8393 label = "man2man";
8394 break;
8395 case MONO_WRAPPER_CASTCLASS:
8396 label = "castclass";
8397 break;
8398 case MONO_WRAPPER_RUNTIME_INVOKE:
8399 label = "run_invoke";
8400 break;
8401 case MONO_WRAPPER_DELEGATE_INVOKE:
8402 label = "del_inv";
8403 break;
8404 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
8405 label = "del_beg_inv";
8406 break;
8407 case MONO_WRAPPER_DELEGATE_END_INVOKE:
8408 label = "del_end_inv";
8409 break;
8410 case MONO_WRAPPER_NATIVE_TO_MANAGED:
8411 label = "native2man";
8412 break;
8413 default:
8414 g_assert_not_reached ();
8417 g_string_append_printf (s, "%s_", label);
8420 static void
8421 append_mangled_wrapper_subtype (GString *s, WrapperSubtype subtype)
8423 const char *label;
8425 switch (subtype)
8427 case WRAPPER_SUBTYPE_NONE:
8428 return;
8429 case WRAPPER_SUBTYPE_ELEMENT_ADDR:
8430 label = "elem_addr";
8431 break;
8432 case WRAPPER_SUBTYPE_STRING_CTOR:
8433 label = "str_ctor";
8434 break;
8435 case WRAPPER_SUBTYPE_VIRTUAL_STELEMREF:
8436 label = "virt_stelem";
8437 break;
8438 case WRAPPER_SUBTYPE_FAST_MONITOR_ENTER:
8439 label = "fast_mon_enter";
8440 break;
8441 case WRAPPER_SUBTYPE_FAST_MONITOR_ENTER_V4:
8442 label = "fast_mon_enter_4";
8443 break;
8444 case WRAPPER_SUBTYPE_FAST_MONITOR_EXIT:
8445 label = "fast_monitor_exit";
8446 break;
8447 case WRAPPER_SUBTYPE_PTR_TO_STRUCTURE:
8448 label = "ptr2struct";
8449 break;
8450 case WRAPPER_SUBTYPE_STRUCTURE_TO_PTR:
8451 label = "struct2ptr";
8452 break;
8453 case WRAPPER_SUBTYPE_CASTCLASS_WITH_CACHE:
8454 label = "castclass_w_cache";
8455 break;
8456 case WRAPPER_SUBTYPE_ISINST_WITH_CACHE:
8457 label = "isinst_w_cache";
8458 break;
8459 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL:
8460 label = "run_inv_norm";
8461 break;
8462 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_DYNAMIC:
8463 label = "run_inv_dyn";
8464 break;
8465 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT:
8466 label = "run_inv_dir";
8467 break;
8468 case WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL:
8469 label = "run_inv_vir";
8470 break;
8471 case WRAPPER_SUBTYPE_ICALL_WRAPPER:
8472 label = "icall";
8473 break;
8474 case WRAPPER_SUBTYPE_NATIVE_FUNC_AOT:
8475 label = "native_func_aot";
8476 break;
8477 case WRAPPER_SUBTYPE_PINVOKE:
8478 label = "pinvoke";
8479 break;
8480 case WRAPPER_SUBTYPE_SYNCHRONIZED_INNER:
8481 label = "synch_inner";
8482 break;
8483 case WRAPPER_SUBTYPE_GSHAREDVT_IN:
8484 label = "gshared_in";
8485 break;
8486 case WRAPPER_SUBTYPE_GSHAREDVT_OUT:
8487 label = "gshared_out";
8488 break;
8489 case WRAPPER_SUBTYPE_ARRAY_ACCESSOR:
8490 label = "array_acc";
8491 break;
8492 case WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER:
8493 label = "generic_arry_help";
8494 break;
8495 case WRAPPER_SUBTYPE_DELEGATE_INVOKE_VIRTUAL:
8496 label = "del_inv_virt";
8497 break;
8498 case WRAPPER_SUBTYPE_DELEGATE_INVOKE_BOUND:
8499 label = "del_inv_bound";
8500 break;
8501 case WRAPPER_SUBTYPE_INTERP_IN:
8502 label = "interp_in";
8503 break;
8504 case WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG:
8505 label = "gsharedvt_in_sig";
8506 break;
8507 case WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG:
8508 label = "gsharedvt_out_sig";
8509 break;
8510 default:
8511 g_assert_not_reached ();
8514 g_string_append_printf (s, "%s_", label);
8517 static char *
8518 sanitize_mangled_string (const char *input)
8520 GString *s = g_string_new ("");
8522 for (int i=0; input [i] != '\0'; i++) {
8523 char c = input [i];
8524 switch (c) {
8525 case '.':
8526 g_string_append (s, "_dot_");
8527 break;
8528 case ' ':
8529 g_string_append (s, "_");
8530 break;
8531 case '`':
8532 g_string_append (s, "_bt_");
8533 break;
8534 case '<':
8535 g_string_append (s, "_le_");
8536 break;
8537 case '>':
8538 g_string_append (s, "_gt_");
8539 break;
8540 case '/':
8541 g_string_append (s, "_sl_");
8542 break;
8543 case '[':
8544 g_string_append (s, "_lbrack_");
8545 break;
8546 case ']':
8547 g_string_append (s, "_rbrack_");
8548 break;
8549 case '(':
8550 g_string_append (s, "_lparen_");
8551 break;
8552 case '-':
8553 g_string_append (s, "_dash_");
8554 break;
8555 case ')':
8556 g_string_append (s, "_rparen_");
8557 break;
8558 case ',':
8559 g_string_append (s, "_comma_");
8560 break;
8561 case ':':
8562 g_string_append (s, "_colon_");
8563 break;
8564 default:
8565 g_string_append_c (s, c);
8569 return g_string_free (s, FALSE);
8572 static gboolean
8573 append_mangled_klass (GString *s, MonoClass *klass)
8575 char *klass_desc = mono_class_full_name (klass);
8576 g_string_append_printf (s, "_%s_%s_", m_class_get_name_space (klass), klass_desc);
8577 g_free (klass_desc);
8579 // Success
8580 return TRUE;
8583 static gboolean
8584 append_mangled_method (GString *s, MonoMethod *method);
8586 static gboolean
8587 append_mangled_wrapper (GString *s, MonoMethod *method)
8589 gboolean success = TRUE;
8590 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
8591 g_string_append_printf (s, "wrapper_");
8592 g_string_append_printf (s, "%s_", m_class_get_image (method->klass)->assembly->aname.name);
8594 append_mangled_wrapper_type (s, method->wrapper_type);
8596 switch (method->wrapper_type) {
8597 case MONO_WRAPPER_REMOTING_INVOKE:
8598 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK:
8599 case MONO_WRAPPER_XDOMAIN_INVOKE: {
8600 MonoMethod *m = mono_marshal_method_from_wrapper (method);
8601 g_assert (m);
8602 success = success && append_mangled_method (s, m);
8603 break;
8605 case MONO_WRAPPER_PROXY_ISINST:
8606 case MONO_WRAPPER_LDFLD:
8607 case MONO_WRAPPER_LDFLDA:
8608 case MONO_WRAPPER_STFLD: {
8609 g_assert (info);
8610 success = success && append_mangled_klass (s, info->d.proxy.klass);
8611 break;
8613 case MONO_WRAPPER_ALLOC: {
8614 /* The GC name is saved once in MonoAotFileInfo */
8615 g_assert (info->d.alloc.alloc_type != -1);
8616 g_string_append_printf (s, "%d_", info->d.alloc.alloc_type);
8617 // SlowAlloc, etc
8618 g_string_append_printf (s, "%s_", method->name);
8619 break;
8621 case MONO_WRAPPER_WRITE_BARRIER: {
8622 g_string_append_printf (s, "%s_", method->name);
8623 break;
8625 case MONO_WRAPPER_STELEMREF: {
8626 append_mangled_wrapper_subtype (s, info->subtype);
8627 if (info->subtype == WRAPPER_SUBTYPE_VIRTUAL_STELEMREF)
8628 g_string_append_printf (s, "%d", info->d.virtual_stelemref.kind);
8629 break;
8631 case MONO_WRAPPER_UNKNOWN: {
8632 append_mangled_wrapper_subtype (s, info->subtype);
8633 if (info->subtype == WRAPPER_SUBTYPE_PTR_TO_STRUCTURE ||
8634 info->subtype == WRAPPER_SUBTYPE_STRUCTURE_TO_PTR)
8635 success = success && append_mangled_klass (s, method->klass);
8636 else if (info->subtype == WRAPPER_SUBTYPE_SYNCHRONIZED_INNER)
8637 success = success && append_mangled_method (s, info->d.synchronized_inner.method);
8638 else if (info->subtype == WRAPPER_SUBTYPE_ARRAY_ACCESSOR)
8639 success = success && append_mangled_method (s, info->d.array_accessor.method);
8640 else if (info->subtype == WRAPPER_SUBTYPE_INTERP_IN)
8641 append_mangled_signature (s, info->d.interp_in.sig);
8642 else if (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG)
8643 append_mangled_signature (s, info->d.gsharedvt.sig);
8644 else if (info->subtype == WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG)
8645 append_mangled_signature (s, info->d.gsharedvt.sig);
8646 break;
8648 case MONO_WRAPPER_MANAGED_TO_NATIVE: {
8649 append_mangled_wrapper_subtype (s, info->subtype);
8650 if (info->subtype == WRAPPER_SUBTYPE_ICALL_WRAPPER) {
8651 g_string_append_printf (s, "%s", method->name);
8652 } else if (info->subtype == WRAPPER_SUBTYPE_NATIVE_FUNC_AOT) {
8653 success = success && append_mangled_method (s, info->d.managed_to_native.method);
8654 } else {
8655 g_assert (info->subtype == WRAPPER_SUBTYPE_NONE || info->subtype == WRAPPER_SUBTYPE_PINVOKE);
8656 success = success && append_mangled_method (s, info->d.managed_to_native.method);
8658 break;
8660 case MONO_WRAPPER_SYNCHRONIZED: {
8661 MonoMethod *m;
8663 m = mono_marshal_method_from_wrapper (method);
8664 g_assert (m);
8665 g_assert (m != method);
8666 success = success && append_mangled_method (s, m);
8667 break;
8669 case MONO_WRAPPER_MANAGED_TO_MANAGED: {
8670 append_mangled_wrapper_subtype (s, info->subtype);
8672 if (info->subtype == WRAPPER_SUBTYPE_ELEMENT_ADDR) {
8673 g_string_append_printf (s, "%d_", info->d.element_addr.rank);
8674 g_string_append_printf (s, "%d_", info->d.element_addr.elem_size);
8675 } else if (info->subtype == WRAPPER_SUBTYPE_STRING_CTOR) {
8676 success = success && append_mangled_method (s, info->d.string_ctor.method);
8677 } else if (info->subtype == WRAPPER_SUBTYPE_GENERIC_ARRAY_HELPER) {
8678 success = success && append_mangled_method (s, info->d.generic_array_helper.method);
8679 } else {
8680 success = FALSE;
8682 break;
8684 case MONO_WRAPPER_CASTCLASS: {
8685 append_mangled_wrapper_subtype (s, info->subtype);
8686 break;
8688 case MONO_WRAPPER_RUNTIME_INVOKE: {
8689 append_mangled_wrapper_subtype (s, info->subtype);
8690 if (info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT || info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL)
8691 success = success && append_mangled_method (s, info->d.runtime_invoke.method);
8692 else if (info->subtype == WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL)
8693 success = success && append_mangled_signature (s, info->d.runtime_invoke.sig);
8694 break;
8696 case MONO_WRAPPER_DELEGATE_INVOKE:
8697 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE:
8698 case MONO_WRAPPER_DELEGATE_END_INVOKE: {
8699 if (method->is_inflated) {
8700 /* These wrappers are identified by their class */
8701 g_string_append_printf (s, "i_");
8702 success = success && append_mangled_klass (s, method->klass);
8703 } else {
8704 WrapperInfo *info = mono_marshal_get_wrapper_info (method);
8706 g_string_append_printf (s, "u_");
8707 if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE)
8708 append_mangled_wrapper_subtype (s, info->subtype);
8709 g_string_append_printf (s, "u_sigstart");
8711 break;
8713 case MONO_WRAPPER_NATIVE_TO_MANAGED: {
8714 g_assert (info);
8715 success = success && append_mangled_method (s, info->d.native_to_managed.method);
8716 success = success && append_mangled_klass (s, method->klass);
8717 break;
8719 default:
8720 g_assert_not_reached ();
8722 return success && append_mangled_signature (s, mono_method_signature (method));
8725 static void
8726 append_mangled_ginst (GString *str, MonoGenericInst *ginst)
8728 int i;
8730 for (i = 0; i < ginst->type_argc; ++i) {
8731 if (i > 0)
8732 g_string_append (str, ", ");
8733 MonoType *type = ginst->type_argv [i];
8734 switch (type->type) {
8735 case MONO_TYPE_VAR:
8736 case MONO_TYPE_MVAR: {
8737 MonoType *constraint = NULL;
8738 if (type->data.generic_param)
8739 constraint = type->data.generic_param->gshared_constraint;
8740 if (constraint) {
8741 g_assert (constraint->type != MONO_TYPE_VAR && constraint->type != MONO_TYPE_MVAR);
8742 g_string_append (str, "gshared:");
8743 mono_type_get_desc (str, constraint, TRUE);
8744 break;
8746 // Else falls through to common case
8748 default:
8749 mono_type_get_desc (str, type, TRUE);
8754 static void
8755 append_mangled_context (GString *str, MonoGenericContext *context)
8757 GString *res = g_string_new ("");
8759 g_string_append_printf (res, "gens_");
8760 g_string_append (res, "00");
8762 gboolean good = context->class_inst && context->class_inst->type_argc > 0;
8763 good = good || (context->method_inst && context->method_inst->type_argc > 0);
8764 g_assert (good);
8766 if (context->class_inst)
8767 append_mangled_ginst (res, context->class_inst);
8768 if (context->method_inst) {
8769 if (context->class_inst)
8770 g_string_append (res, "11");
8771 append_mangled_ginst (res, context->method_inst);
8773 g_string_append_printf (str, "gens_%s", res->str);
8774 g_free (res);
8777 static gboolean
8778 append_mangled_method (GString *s, MonoMethod *method)
8780 if (method->wrapper_type)
8781 return append_mangled_wrapper (s, method);
8783 if (method->is_inflated) {
8784 g_string_append_printf (s, "inflated_");
8785 MonoMethodInflated *imethod = (MonoMethodInflated*) method;
8786 g_assert (imethod->context.class_inst != NULL || imethod->context.method_inst != NULL);
8788 append_mangled_context (s, &imethod->context);
8789 g_string_append_printf (s, "_declared_by_");
8790 append_mangled_method (s, imethod->declaring);
8791 } else if (method->is_generic) {
8792 g_string_append_printf (s, "%s_", m_class_get_image (method->klass)->assembly->aname.name);
8794 g_string_append_printf (s, "generic_");
8795 append_mangled_klass (s, method->klass);
8796 g_string_append_printf (s, "_%s_", method->name);
8798 MonoGenericContainer *container = mono_method_get_generic_container (method);
8799 g_string_append_printf (s, "_%s");
8800 append_mangled_context (s, &container->context);
8802 return append_mangled_signature (s, mono_method_signature (method));
8803 } else {
8804 g_string_append_printf (s, "_");
8805 append_mangled_klass (s, method->klass);
8806 g_string_append_printf (s, "_%s_", method->name);
8807 if (!append_mangled_signature (s, mono_method_signature (method))) {
8808 g_string_free (s, TRUE);
8809 return FALSE;
8813 return TRUE;
8817 * mono_aot_get_mangled_method_name:
8819 * Return a unique mangled name for METHOD, or NULL.
8821 char*
8822 mono_aot_get_mangled_method_name (MonoMethod *method)
8824 // FIXME: use static cache (mempool?)
8825 // We call this a *lot*
8827 GString *s = g_string_new ("aot_");
8828 if (!append_mangled_method (s, method)) {
8829 g_string_free (s, TRUE);
8830 return NULL;
8831 } else {
8832 char *out = g_string_free (s, FALSE);
8833 // Scrub method and class names
8834 char *cleaned = sanitize_mangled_string (out);
8835 g_free (out);
8836 return cleaned;
8840 gboolean
8841 mono_aot_is_direct_callable (MonoJumpInfo *patch_info)
8843 return is_direct_callable (llvm_acfg, NULL, patch_info);
8846 void
8847 mono_aot_mark_unused_llvm_plt_entry (MonoJumpInfo *patch_info)
8849 MonoPltEntry *plt_entry;
8851 plt_entry = get_plt_entry (llvm_acfg, patch_info);
8852 plt_entry->llvm_used = FALSE;
8855 char*
8856 mono_aot_get_direct_call_symbol (MonoJumpInfoType type, gconstpointer data)
8858 const char *sym = NULL;
8860 if (llvm_acfg->aot_opts.direct_icalls) {
8861 if (type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8862 /* Call to a C function implementing a jit icall */
8863 sym = mono_lookup_jit_icall_symbol ((const char *)data);
8864 } else if (type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
8865 MonoMethod *method = (MonoMethod *)data;
8866 if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
8867 sym = mono_lookup_icall_symbol (method);
8868 else if (llvm_acfg->aot_opts.direct_pinvoke)
8869 sym = get_pinvoke_import (llvm_acfg, method);
8871 if (sym)
8872 return g_strdup (sym);
8874 return NULL;
8877 char*
8878 mono_aot_get_plt_symbol (MonoJumpInfoType type, gconstpointer data)
8880 MonoJumpInfo *ji = (MonoJumpInfo *)mono_mempool_alloc (llvm_acfg->mempool, sizeof (MonoJumpInfo));
8881 MonoPltEntry *plt_entry;
8882 const char *sym = NULL;
8884 ji->type = type;
8885 ji->data.target = data;
8887 if (!can_encode_patch (llvm_acfg, ji))
8888 return NULL;
8890 if (llvm_acfg->aot_opts.direct_icalls) {
8891 if (type == MONO_PATCH_INFO_JIT_ICALL_ADDR) {
8892 /* Call to a C function implementing a jit icall */
8893 sym = mono_lookup_jit_icall_symbol ((const char *)data);
8894 } else if (type == MONO_PATCH_INFO_ICALL_ADDR_CALL) {
8895 MonoMethod *method = (MonoMethod *)data;
8896 if (!(method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL))
8897 sym = mono_lookup_icall_symbol (method);
8899 if (sym)
8900 return g_strdup (sym);
8903 plt_entry = get_plt_entry (llvm_acfg, ji);
8904 plt_entry->llvm_used = TRUE;
8906 #if defined(TARGET_MACH)
8907 return g_strdup_printf (plt_entry->llvm_symbol + strlen (llvm_acfg->llvm_label_prefix));
8908 #else
8909 return g_strdup_printf (plt_entry->llvm_symbol);
8910 #endif
8914 mono_aot_get_method_index (MonoMethod *method)
8916 g_assert (llvm_acfg);
8917 return get_method_index (llvm_acfg, method);
8920 MonoJumpInfo*
8921 mono_aot_patch_info_dup (MonoJumpInfo* ji)
8923 MonoJumpInfo *res;
8925 mono_acfg_lock (llvm_acfg);
8926 res = mono_patch_info_dup_mp (llvm_acfg->mempool, ji);
8927 mono_acfg_unlock (llvm_acfg);
8929 return res;
8932 static int
8933 execute_system (const char * command)
8935 int status = 0;
8937 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) && defined(HOST_WIN32)
8938 // We need an extra set of quotes around the whole command to properly handle commands
8939 // with spaces since internally the command is called through "cmd /c.
8940 char * quoted_command = g_strdup_printf ("\"%s\"", command);
8942 int size = MultiByteToWideChar (CP_UTF8, 0 , quoted_command , -1, NULL , 0);
8943 wchar_t* wstr = g_malloc (sizeof (wchar_t) * size);
8944 MultiByteToWideChar (CP_UTF8, 0, quoted_command, -1, wstr , size);
8945 status = _wsystem (wstr);
8946 g_free (wstr);
8948 g_free (quoted_command);
8949 #elif defined (HAVE_SYSTEM)
8950 status = system (command);
8951 #else
8952 g_assert_not_reached ();
8953 #endif
8955 return status;
8958 #ifdef ENABLE_LLVM
8961 * emit_llvm_file:
8963 * Emit the LLVM code into an LLVM bytecode file, and compile it using the LLVM
8964 * tools.
8966 static gboolean
8967 emit_llvm_file (MonoAotCompile *acfg)
8969 char *command, *opts, *tempbc, *optbc, *output_fname;
8971 if (acfg->aot_opts.llvm_only && acfg->aot_opts.asm_only) {
8972 tempbc = g_strdup_printf ("%s.bc", acfg->tmpbasename);
8973 optbc = g_strdup (acfg->aot_opts.llvm_outfile);
8974 } else {
8975 tempbc = g_strdup_printf ("%s.bc", acfg->tmpbasename);
8976 optbc = g_strdup_printf ("%s.opt.bc", acfg->tmpbasename);
8979 mono_llvm_emit_aot_module (tempbc, g_path_get_basename (acfg->image->name));
8982 * FIXME: Experiment with adding optimizations, the -std-compile-opts set takes
8983 * a lot of time, and doesn't seem to save much space.
8984 * The following optimizations cannot be enabled:
8985 * - 'tailcallelim'
8986 * - 'jump-threading' changes our blockaddress references to int constants.
8987 * - 'basiccg' fails because it contains:
8988 * if (CS && !isa<IntrinsicInst>(II)) {
8989 * and isa<IntrinsicInst> is false for invokes to intrinsics (iltests.exe).
8990 * - 'prune-eh' and 'functionattrs' depend on 'basiccg'.
8991 * The opt list below was produced by taking the output of:
8992 * llvm-as < /dev/null | opt -O2 -disable-output -debug-pass=Arguments
8993 * then removing tailcallelim + the global opts.
8994 * strip-dead-prototypes deletes unused intrinsics definitions.
8996 /* The dse pass is disabled because of #13734 and #17616 */
8998 * The dse bug is in DeadStoreElimination.cpp:isOverwrite ():
8999 * // If we have no DataLayout information around, then the size of the store
9000 * // is inferrable from the pointee type. If they are the same type, then
9001 * // we know that the store is safe.
9002 * if (AA.getDataLayout() == 0 &&
9003 * Later.Ptr->getType() == Earlier.Ptr->getType()) {
9004 * return OverwriteComplete;
9005 * Here, if 'Earlier' refers to a memset, and Later has no size info, it mistakenly thinks the memset is redundant.
9007 if (acfg->aot_opts.llvm_opts) {
9008 opts = g_strdup (acfg->aot_opts.llvm_opts);
9009 } else if (acfg->aot_opts.llvm_only) {
9010 // FIXME: This doesn't work yet
9011 opts = g_strdup ("");
9012 } else {
9013 #if LLVM_API_VERSION > 100
9014 opts = g_strdup ("-O2 -disable-tail-calls");
9015 #else
9016 opts = g_strdup ("-targetlibinfo -no-aa -basicaa -notti -instcombine -simplifycfg -inline-cost -inline -sroa -domtree -early-cse -lazy-value-info -correlated-propagation -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loop-simplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loop-simplify -lcssa -indvars -loop-idiom -loop-deletion -loop-unroll -memdep -gvn -memdep -memcpyopt -sccp -instcombine -lazy-value-info -correlated-propagation -domtree -memdep -adce -simplifycfg -instcombine -strip-dead-prototypes -domtree -verify");
9017 #endif
9020 command = g_strdup_printf ("\"%sopt\" -f %s -o \"%s\" \"%s\"", acfg->aot_opts.llvm_path, opts, optbc, tempbc);
9021 aot_printf (acfg, "Executing opt: %s\n", command);
9022 if (execute_system (command) != 0)
9023 return FALSE;
9024 g_free (opts);
9026 if (acfg->aot_opts.llvm_only && acfg->aot_opts.asm_only)
9027 /* Nothing else to do */
9028 return TRUE;
9030 if (acfg->aot_opts.llvm_only) {
9031 /* Use the stock clang from xcode */
9032 // FIXME: arch
9033 command = g_strdup_printf ("clang++ -fexceptions -march=x86-64 -fpic -msse -msse2 -msse3 -msse4 -O2 -fno-optimize-sibling-calls -Wno-override-module -c -o \"%s\" \"%s.opt.bc\"", acfg->llvm_ofile, acfg->tmpbasename);
9035 aot_printf (acfg, "Executing clang: %s\n", command);
9036 if (execute_system (command) != 0)
9037 return FALSE;
9038 return TRUE;
9041 if (!acfg->llc_args)
9042 acfg->llc_args = g_string_new ("");
9044 /* Verbose asm slows down llc greatly */
9045 g_string_append (acfg->llc_args, " -asm-verbose=false");
9047 if (acfg->aot_opts.mtriple)
9048 g_string_append_printf (acfg->llc_args, " -mtriple=%s", acfg->aot_opts.mtriple);
9050 g_string_append (acfg->llc_args, " -disable-gnu-eh-frame -enable-mono-eh-frame");
9052 g_string_append_printf (acfg->llc_args, " -mono-eh-frame-symbol=%s%s", acfg->user_symbol_prefix, acfg->llvm_eh_frame_symbol);
9054 #if LLVM_API_VERSION > 100
9055 g_string_append_printf (acfg->llc_args, " -disable-tail-calls");
9056 #endif
9058 #if ( defined(TARGET_MACH) && defined(TARGET_ARM) ) || defined(TARGET_ORBIS)
9059 /* ios requires PIC code now */
9060 g_string_append_printf (acfg->llc_args, " -relocation-model=pic");
9061 #else
9062 if (llvm_acfg->aot_opts.static_link)
9063 g_string_append_printf (acfg->llc_args, " -relocation-model=static");
9064 else
9065 g_string_append_printf (acfg->llc_args, " -relocation-model=pic");
9066 #endif
9068 if (acfg->llvm_owriter) {
9069 /* Emit an object file directly */
9070 output_fname = g_strdup_printf ("%s", acfg->llvm_ofile);
9071 g_string_append_printf (acfg->llc_args, " -filetype=obj");
9072 } else {
9073 output_fname = g_strdup_printf ("%s", acfg->llvm_sfile);
9075 command = g_strdup_printf ("\"%sllc\" %s -o \"%s\" \"%s.opt.bc\"", acfg->aot_opts.llvm_path, acfg->llc_args->str, output_fname, acfg->tmpbasename);
9076 g_free (output_fname);
9078 aot_printf (acfg, "Executing llc: %s\n", command);
9080 if (execute_system (command) != 0)
9081 return FALSE;
9082 return TRUE;
9084 #endif
9086 static void
9087 emit_code (MonoAotCompile *acfg)
9089 int oindex, i, prev_index;
9090 gboolean saved_unbox_info = FALSE;
9091 char symbol [MAX_SYMBOL_SIZE];
9093 if (acfg->aot_opts.llvm_only)
9094 return;
9096 #if defined(TARGET_POWERPC64)
9097 sprintf (symbol, ".Lgot_addr");
9098 emit_section_change (acfg, ".text", 0);
9099 emit_alignment (acfg, 8);
9100 emit_label (acfg, symbol);
9101 emit_pointer (acfg, acfg->got_symbol);
9102 #endif
9105 * This global symbol is used to compute the address of each method using the
9106 * code_offsets array. It is also used to compute the memory ranges occupied by
9107 * AOT code, so it must be equal to the address of the first emitted method.
9109 emit_section_change (acfg, ".text", 0);
9110 emit_alignment_code (acfg, 8);
9111 emit_info_symbol (acfg, "jit_code_start");
9114 * Emit some padding so the local symbol for the first method doesn't have the
9115 * same address as 'methods'.
9117 emit_padding (acfg, 16);
9119 for (oindex = 0; oindex < acfg->method_order->len; ++oindex) {
9120 MonoCompile *cfg;
9121 MonoMethod *method;
9123 i = GPOINTER_TO_UINT (g_ptr_array_index (acfg->method_order, oindex));
9125 cfg = acfg->cfgs [i];
9127 if (!cfg)
9128 continue;
9130 method = cfg->orig_method;
9132 gboolean dedup_collect = acfg->aot_opts.dedup || (acfg->aot_opts.dedup_include && !acfg->dedup_emit_mode);
9133 gboolean dedupable = mono_aot_can_dedup (method);
9135 // cfg->skip is vital for LLVM to work, can't just continue in this loop
9136 if (dedupable && strcmp (method->name, "wbarrier_conc") && dedup_collect) {
9137 mono_dedup_cache_method (acfg, method);
9139 // Don't compile inflated methods if we're in first phase of
9140 // dedup
9142 // In second phase, we emit methods that
9143 // are dedupable. We also emit later methods
9144 // which are referenced by them and added later.
9145 // For this reason, when in the dedup_include mode,
9146 // we never set skip.
9147 if (acfg->aot_opts.dedup)
9148 cfg->skip = TRUE;
9151 // Don't compile anything in this mode
9152 if (acfg->aot_opts.dedup_include && !acfg->dedup_emit_mode)
9153 cfg->skip = TRUE;
9155 // Compile everything in this mode
9156 if (acfg->aot_opts.dedup_include && acfg->dedup_emit_mode)
9157 cfg->skip = FALSE;
9159 /*if (dedup_collect) {*/
9160 /*char *name = mono_aot_get_mangled_method_name (method);*/
9162 /*if (ignore_cfg (cfg))*/
9163 /*aot_printf (acfg, "Dedup Skipping %s\n", acfg->image->name, name);*/
9164 /*else*/
9165 /*aot_printf (acfg, "Dedup Keeping %s\n", acfg->image->name, name);*/
9167 /*g_free (name);*/
9168 /*}*/
9170 if (ignore_cfg (cfg))
9171 continue;
9173 /* Emit unbox trampoline */
9174 if (mono_aot_mode_is_full (&acfg->aot_opts) && m_class_is_valuetype (cfg->orig_method->klass)) {
9175 sprintf (symbol, "ut_%d", get_method_index (acfg, method));
9177 emit_section_change (acfg, ".text", 0);
9179 if (acfg->thumb_mixed && cfg->compile_llvm) {
9180 emit_set_thumb_mode (acfg);
9181 fprintf (acfg->fp, "\n.thumb_func\n");
9184 emit_label (acfg, symbol);
9186 arch_emit_unbox_trampoline (acfg, cfg, cfg->orig_method, cfg->asm_symbol);
9188 if (acfg->thumb_mixed && cfg->compile_llvm)
9189 emit_set_arm_mode (acfg);
9191 if (!saved_unbox_info) {
9192 char user_symbol [128];
9193 GSList *unwind_ops;
9194 sprintf (user_symbol, "%sunbox_trampoline_p", acfg->user_symbol_prefix);
9196 emit_label (acfg, "ut_end");
9198 unwind_ops = mono_unwind_get_cie_program ();
9199 save_unwind_info (acfg, user_symbol, unwind_ops);
9200 mono_free_unwind_info (unwind_ops);
9202 /* Save the unbox trampoline size */
9203 emit_symbol_diff (acfg, "ut_end", symbol, 0);
9205 saved_unbox_info = TRUE;
9209 if (cfg->compile_llvm) {
9210 acfg->stats.llvm_count ++;
9211 } else {
9212 emit_method_code (acfg, cfg);
9216 emit_section_change (acfg, ".text", 0);
9217 emit_alignment_code (acfg, 8);
9218 emit_info_symbol (acfg, "jit_code_end");
9220 /* To distinguish it from the next symbol */
9221 emit_padding (acfg, 4);
9224 * Add .no_dead_strip directives for all LLVM methods to prevent the OSX linker
9225 * from optimizing them away, since it doesn't see that code_offsets references them.
9226 * JITted methods don't need this since they are referenced using assembler local
9227 * symbols.
9228 * FIXME: This is why write-symbols doesn't work on OSX ?
9230 if (acfg->llvm && acfg->need_no_dead_strip) {
9231 fprintf (acfg->fp, "\n");
9232 for (i = 0; i < acfg->nmethods; ++i) {
9233 if (acfg->cfgs [i] && acfg->cfgs [i]->compile_llvm)
9234 fprintf (acfg->fp, ".no_dead_strip %s\n", acfg->cfgs [i]->asm_symbol);
9239 * To work around linker issues, we emit a table of branches, and disassemble them at runtime.
9240 * This is PIE code, and the linker can update it if needed.
9243 sprintf (symbol, "method_addresses");
9244 emit_section_change (acfg, ".text", 1);
9245 emit_alignment_code (acfg, 8);
9246 emit_info_symbol (acfg, symbol);
9247 if (acfg->aot_opts.write_symbols)
9248 emit_local_symbol (acfg, symbol, "method_addresses_end", TRUE);
9249 emit_unset_mode (acfg);
9250 if (acfg->need_no_dead_strip)
9251 fprintf (acfg->fp, " .no_dead_strip %s\n", symbol);
9253 for (i = 0; i < acfg->nmethods; ++i) {
9254 #ifdef MONO_ARCH_AOT_SUPPORTED
9255 int call_size;
9257 if (!ignore_cfg (acfg->cfgs [i])) {
9258 arch_emit_direct_call (acfg, acfg->cfgs [i]->asm_symbol, FALSE, acfg->thumb_mixed && acfg->cfgs [i]->compile_llvm, NULL, &call_size);
9259 } else {
9260 arch_emit_direct_call (acfg, symbol, FALSE, FALSE, NULL, &call_size);
9262 #endif
9265 sprintf (symbol, "method_addresses_end");
9266 emit_label (acfg, symbol);
9267 emit_line (acfg);
9269 /* Emit a sorted table mapping methods to the index of their unbox trampolines */
9270 sprintf (symbol, "unbox_trampolines");
9271 emit_section_change (acfg, RODATA_SECT, 0);
9272 emit_alignment (acfg, 8);
9273 emit_info_symbol (acfg, symbol);
9275 prev_index = -1;
9276 for (i = 0; i < acfg->nmethods; ++i) {
9277 MonoCompile *cfg;
9278 MonoMethod *method;
9279 int index;
9281 cfg = acfg->cfgs [i];
9282 if (ignore_cfg (cfg))
9283 continue;
9285 method = cfg->orig_method;
9287 if (mono_aot_mode_is_full (&acfg->aot_opts) && m_class_is_valuetype (cfg->orig_method->klass)) {
9288 index = get_method_index (acfg, method);
9290 emit_int32 (acfg, index);
9291 /* Make sure the table is sorted by index */
9292 g_assert (index > prev_index);
9293 prev_index = index;
9296 sprintf (symbol, "unbox_trampolines_end");
9297 emit_info_symbol (acfg, symbol);
9298 emit_int32 (acfg, 0);
9300 /* Emit a separate table with the trampoline addresses/offsets */
9301 sprintf (symbol, "unbox_trampoline_addresses");
9302 emit_section_change (acfg, ".text", 0);
9303 emit_alignment_code (acfg, 8);
9304 emit_info_symbol (acfg, symbol);
9306 for (i = 0; i < acfg->nmethods; ++i) {
9307 MonoCompile *cfg;
9308 MonoMethod *method;
9309 int index;
9311 cfg = acfg->cfgs [i];
9312 if (ignore_cfg (cfg))
9313 continue;
9315 method = cfg->orig_method;
9317 if (mono_aot_mode_is_full (&acfg->aot_opts) && m_class_is_valuetype (cfg->orig_method->klass)) {
9318 #ifdef MONO_ARCH_AOT_SUPPORTED
9319 int call_size;
9321 index = get_method_index (acfg, method);
9322 sprintf (symbol, "ut_%d", index);
9324 arch_emit_direct_call (acfg, symbol, FALSE, acfg->thumb_mixed && cfg->compile_llvm, NULL, &call_size);
9325 #endif
9328 emit_int32 (acfg, 0);
9331 static void
9332 emit_info (MonoAotCompile *acfg)
9334 int oindex, i;
9335 gint32 *offsets;
9337 offsets = g_new0 (gint32, acfg->nmethods);
9339 for (oindex = 0; oindex < acfg->method_order->len; ++oindex) {
9340 i = GPOINTER_TO_UINT (g_ptr_array_index (acfg->method_order, oindex));
9342 if (acfg->cfgs [i]) {
9343 emit_method_info (acfg, acfg->cfgs [i]);
9344 offsets [i] = acfg->cfgs [i]->method_info_offset;
9345 } else {
9346 offsets [i] = 0;
9350 acfg->stats.offsets_size += emit_offset_table (acfg, "method_info_offsets", MONO_AOT_TABLE_METHOD_INFO_OFFSETS, acfg->nmethods, 10, offsets);
9352 g_free (offsets);
9355 #endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
9357 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
9358 #define mix(a,b,c) { \
9359 a -= c; a ^= rot(c, 4); c += b; \
9360 b -= a; b ^= rot(a, 6); a += c; \
9361 c -= b; c ^= rot(b, 8); b += a; \
9362 a -= c; a ^= rot(c,16); c += b; \
9363 b -= a; b ^= rot(a,19); a += c; \
9364 c -= b; c ^= rot(b, 4); b += a; \
9366 #define final(a,b,c) { \
9367 c ^= b; c -= rot(b,14); \
9368 a ^= c; a -= rot(c,11); \
9369 b ^= a; b -= rot(a,25); \
9370 c ^= b; c -= rot(b,16); \
9371 a ^= c; a -= rot(c,4); \
9372 b ^= a; b -= rot(a,14); \
9373 c ^= b; c -= rot(b,24); \
9376 static guint
9377 mono_aot_type_hash (MonoType *t1)
9379 guint hash = t1->type;
9381 hash |= t1->byref << 6; /* do not collide with t1->type values */
9382 switch (t1->type) {
9383 case MONO_TYPE_VALUETYPE:
9384 case MONO_TYPE_CLASS:
9385 case MONO_TYPE_SZARRAY:
9386 /* check if the distribution is good enough */
9387 return ((hash << 5) - hash) ^ mono_metadata_str_hash (m_class_get_name (t1->data.klass));
9388 case MONO_TYPE_PTR:
9389 return ((hash << 5) - hash) ^ mono_metadata_type_hash (t1->data.type);
9390 case MONO_TYPE_ARRAY:
9391 return ((hash << 5) - hash) ^ mono_metadata_type_hash (m_class_get_byval_arg (t1->data.array->eklass));
9392 case MONO_TYPE_GENERICINST:
9393 return ((hash << 5) - hash) ^ 0;
9394 default:
9395 return hash;
9400 * mono_aot_method_hash:
9402 * Return a hash code for methods which only depends on metadata.
9404 guint32
9405 mono_aot_method_hash (MonoMethod *method)
9407 MonoMethodSignature *sig;
9408 MonoClass *klass;
9409 int i, hindex;
9410 int hashes_count;
9411 guint32 *hashes_start, *hashes;
9412 guint32 a, b, c;
9413 MonoGenericInst *class_ginst = NULL;
9414 MonoGenericInst *ginst = NULL;
9416 /* Similar to the hash in mono_method_get_imt_slot () */
9418 sig = mono_method_signature (method);
9420 if (mono_class_is_ginst (method->klass))
9421 class_ginst = mono_class_get_generic_class (method->klass)->context.class_inst;
9422 if (method->is_inflated)
9423 ginst = ((MonoMethodInflated*)method)->context.method_inst;
9425 hashes_count = sig->param_count + 5 + (class_ginst ? class_ginst->type_argc : 0) + (ginst ? ginst->type_argc : 0);
9426 hashes_start = (guint32 *)g_malloc0 (hashes_count * sizeof (guint32));
9427 hashes = hashes_start;
9429 /* Some wrappers are assigned to random classes */
9430 if (!method->wrapper_type || method->wrapper_type == MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK)
9431 klass = method->klass;
9432 else
9433 klass = mono_defaults.object_class;
9435 if (!method->wrapper_type) {
9436 char *full_name;
9438 if (mono_class_is_ginst (klass))
9439 full_name = mono_type_full_name (m_class_get_byval_arg (mono_class_get_generic_class (klass)->container_class));
9440 else
9441 full_name = mono_type_full_name (m_class_get_byval_arg (klass));
9443 hashes [0] = mono_metadata_str_hash (full_name);
9444 hashes [1] = 0;
9445 g_free (full_name);
9446 } else {
9447 hashes [0] = mono_metadata_str_hash (m_class_get_name (klass));
9448 hashes [1] = mono_metadata_str_hash (m_class_get_name_space (klass));
9450 if (method->wrapper_type == MONO_WRAPPER_STFLD || method->wrapper_type == MONO_WRAPPER_LDFLD || method->wrapper_type == MONO_WRAPPER_LDFLDA)
9451 /* The method name includes a stringified pointer */
9452 hashes [2] = 0;
9453 else
9454 hashes [2] = mono_metadata_str_hash (method->name);
9455 hashes [3] = method->wrapper_type;
9456 hashes [4] = mono_aot_type_hash (sig->ret);
9457 hindex = 5;
9458 for (i = 0; i < sig->param_count; i++) {
9459 hashes [hindex ++] = mono_aot_type_hash (sig->params [i]);
9461 if (class_ginst) {
9462 for (i = 0; i < class_ginst->type_argc; ++i)
9463 hashes [hindex ++] = mono_aot_type_hash (class_ginst->type_argv [i]);
9465 if (ginst) {
9466 for (i = 0; i < ginst->type_argc; ++i)
9467 hashes [hindex ++] = mono_aot_type_hash (ginst->type_argv [i]);
9469 g_assert (hindex == hashes_count);
9471 /* Setup internal state */
9472 a = b = c = 0xdeadbeef + (((guint32)hashes_count)<<2);
9474 /* Handle most of the hashes */
9475 while (hashes_count > 3) {
9476 a += hashes [0];
9477 b += hashes [1];
9478 c += hashes [2];
9479 mix (a,b,c);
9480 hashes_count -= 3;
9481 hashes += 3;
9484 /* Handle the last 3 hashes (all the case statements fall through) */
9485 switch (hashes_count) {
9486 case 3 : c += hashes [2];
9487 case 2 : b += hashes [1];
9488 case 1 : a += hashes [0];
9489 final (a,b,c);
9490 case 0: /* nothing left to add */
9491 break;
9494 g_free (hashes_start);
9496 return c;
9498 #undef rot
9499 #undef mix
9500 #undef final
9503 * mono_aot_get_array_helper_from_wrapper;
9505 * Get the helper method in Array called by an array wrapper method.
9507 MonoMethod*
9508 mono_aot_get_array_helper_from_wrapper (MonoMethod *method)
9510 MonoMethod *m;
9511 const char *prefix;
9512 MonoGenericContext ctx;
9513 MonoType *args [16];
9514 char *mname, *iname, *s, *s2, *helper_name = NULL;
9516 prefix = "System.Collections.Generic";
9517 s = g_strdup_printf ("%s", method->name + strlen (prefix) + 1);
9518 s2 = strstr (s, "`1.");
9519 g_assert (s2);
9520 s2 [0] = '\0';
9521 iname = s;
9522 mname = s2 + 3;
9524 //printf ("X: %s %s\n", iname, mname);
9526 if (!strcmp (iname, "IList"))
9527 helper_name = g_strdup_printf ("InternalArray__%s", mname);
9528 else
9529 helper_name = g_strdup_printf ("InternalArray__%s_%s", iname, mname);
9530 m = mono_class_get_method_from_name (mono_defaults.array_class, helper_name, mono_method_signature (method)->param_count);
9531 g_assert (m);
9532 g_free (helper_name);
9533 g_free (s);
9535 if (m->is_generic) {
9536 ERROR_DECL (error);
9537 memset (&ctx, 0, sizeof (ctx));
9538 args [0] = m_class_get_byval_arg (m_class_get_element_class (method->klass));
9539 ctx.method_inst = mono_metadata_get_generic_inst (1, args);
9540 m = mono_class_inflate_generic_method_checked (m, &ctx, error);
9541 g_assert (mono_error_ok (error)); /* FIXME don't swallow the error */
9544 return m;
9547 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
9549 typedef struct HashEntry {
9550 guint32 key, value, index;
9551 struct HashEntry *next;
9552 } HashEntry;
9555 * emit_extra_methods:
9557 * Emit methods which are not in the METHOD table, like wrappers.
9559 static void
9560 emit_extra_methods (MonoAotCompile *acfg)
9562 int i, table_size, buf_size;
9563 guint8 *p, *buf;
9564 guint32 *info_offsets;
9565 guint32 hash;
9566 GPtrArray *table;
9567 HashEntry *entry, *new_entry;
9568 int nmethods, max_chain_length;
9569 int *chain_lengths;
9571 info_offsets = g_new0 (guint32, acfg->extra_methods->len);
9573 /* Emit method info */
9574 nmethods = 0;
9575 for (i = 0; i < acfg->extra_methods->len; ++i) {
9576 MonoMethod *method = (MonoMethod *)g_ptr_array_index (acfg->extra_methods, i);
9577 MonoCompile *cfg = (MonoCompile *)g_hash_table_lookup (acfg->method_to_cfg, method);
9579 if (ignore_cfg (cfg))
9580 continue;
9582 buf_size = 10240;
9583 p = buf = (guint8 *)g_malloc (buf_size);
9585 nmethods ++;
9587 method = cfg->method_to_register;
9589 encode_method_ref (acfg, method, p, &p);
9591 g_assert ((p - buf) < buf_size);
9593 info_offsets [i] = add_to_blob (acfg, buf, p - buf);
9594 g_free (buf);
9598 * Construct a chained hash table for mapping indexes in extra_method_info to
9599 * method indexes.
9601 table_size = g_spaced_primes_closest ((int)(nmethods * 1.5));
9602 table = g_ptr_array_sized_new (table_size);
9603 for (i = 0; i < table_size; ++i)
9604 g_ptr_array_add (table, NULL);
9605 chain_lengths = g_new0 (int, table_size);
9606 max_chain_length = 0;
9607 for (i = 0; i < acfg->extra_methods->len; ++i) {
9608 MonoMethod *method = (MonoMethod *)g_ptr_array_index (acfg->extra_methods, i);
9609 MonoCompile *cfg = (MonoCompile *)g_hash_table_lookup (acfg->method_to_cfg, method);
9610 guint32 key, value;
9612 if (ignore_cfg (cfg))
9613 continue;
9615 key = info_offsets [i];
9616 value = get_method_index (acfg, method);
9618 hash = mono_aot_method_hash (method) % table_size;
9619 //printf ("X: %s %x\n", mono_method_get_full_name (method), mono_aot_method_hash (method));
9621 chain_lengths [hash] ++;
9622 max_chain_length = MAX (max_chain_length, chain_lengths [hash]);
9624 new_entry = (HashEntry *)mono_mempool_alloc0 (acfg->mempool, sizeof (HashEntry));
9625 new_entry->key = key;
9626 new_entry->value = value;
9628 entry = (HashEntry *)g_ptr_array_index (table, hash);
9629 if (entry == NULL) {
9630 new_entry->index = hash;
9631 g_ptr_array_index (table, hash) = new_entry;
9632 } else {
9633 while (entry->next)
9634 entry = entry->next;
9636 entry->next = new_entry;
9637 new_entry->index = table->len;
9638 g_ptr_array_add (table, new_entry);
9641 g_free (chain_lengths);
9643 //printf ("MAX: %d\n", max_chain_length);
9645 buf_size = table->len * 12 + 4;
9646 p = buf = (guint8 *)g_malloc (buf_size);
9647 encode_int (table_size, p, &p);
9649 for (i = 0; i < table->len; ++i) {
9650 HashEntry *entry = (HashEntry *)g_ptr_array_index (table, i);
9652 if (entry == NULL) {
9653 encode_int (0, p, &p);
9654 encode_int (0, p, &p);
9655 encode_int (0, p, &p);
9656 } else {
9657 //g_assert (entry->key > 0);
9658 encode_int (entry->key, p, &p);
9659 encode_int (entry->value, p, &p);
9660 if (entry->next)
9661 encode_int (entry->next->index, p, &p);
9662 else
9663 encode_int (0, p, &p);
9666 g_assert (p - buf <= buf_size);
9668 /* Emit the table */
9669 emit_aot_data (acfg, MONO_AOT_TABLE_EXTRA_METHOD_TABLE, "extra_method_table", buf, p - buf);
9671 g_free (buf);
9674 * Emit a table reverse mapping method indexes to their index in extra_method_info.
9675 * This is used by mono_aot_find_jit_info ().
9677 buf_size = acfg->extra_methods->len * 8 + 4;
9678 p = buf = (guint8 *)g_malloc (buf_size);
9679 encode_int (acfg->extra_methods->len, p, &p);
9680 for (i = 0; i < acfg->extra_methods->len; ++i) {
9681 MonoMethod *method = (MonoMethod *)g_ptr_array_index (acfg->extra_methods, i);
9683 encode_int (get_method_index (acfg, method), p, &p);
9684 encode_int (info_offsets [i], p, &p);
9686 emit_aot_data (acfg, MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS, "extra_method_info_offsets", buf, p - buf);
9688 g_free (buf);
9689 g_free (info_offsets);
9690 g_ptr_array_free (table, TRUE);
9693 static void
9694 generate_aotid (guint8* aotid)
9696 gpointer rand_handle;
9697 ERROR_DECL (error);
9699 mono_rand_open ();
9700 rand_handle = mono_rand_init (NULL, 0);
9702 mono_rand_try_get_bytes (&rand_handle, aotid, 16, error);
9703 mono_error_assert_ok (error);
9705 mono_rand_close (rand_handle);
9708 static void
9709 emit_exception_info (MonoAotCompile *acfg)
9711 int i;
9712 gint32 *offsets;
9713 SeqPointData sp_data;
9714 gboolean seq_points_to_file = FALSE;
9716 offsets = g_new0 (gint32, acfg->nmethods);
9717 for (i = 0; i < acfg->nmethods; ++i) {
9718 if (acfg->cfgs [i]) {
9719 MonoCompile *cfg = acfg->cfgs [i];
9721 // By design aot-runtime decode_exception_debug_info is not able to load sequence point debug data from a file.
9722 // As it is not possible to load debug data from a file its is also not possible to store it in a file.
9723 gboolean method_seq_points_to_file = acfg->aot_opts.gen_msym_dir &&
9724 cfg->gen_seq_points && !cfg->gen_sdb_seq_points;
9725 gboolean method_seq_points_to_binary = cfg->gen_seq_points && !method_seq_points_to_file;
9727 emit_exception_debug_info (acfg, cfg, method_seq_points_to_binary);
9728 offsets [i] = cfg->ex_info_offset;
9730 if (method_seq_points_to_file) {
9731 if (!seq_points_to_file) {
9732 mono_seq_point_data_init (&sp_data, acfg->nmethods);
9733 seq_points_to_file = TRUE;
9735 mono_seq_point_data_add (&sp_data, cfg->method->token, cfg->method_index, cfg->seq_point_info);
9737 } else {
9738 offsets [i] = 0;
9742 if (seq_points_to_file) {
9743 char *aotid = mono_guid_to_string_minimal (acfg->image->aotid);
9744 char *dir = g_build_filename (acfg->aot_opts.gen_msym_dir_path, aotid, NULL);
9745 char *image_basename = g_path_get_basename (acfg->image->name);
9746 char *aot_file = g_strdup_printf("%s%s", image_basename, SEQ_POINT_AOT_EXT);
9747 char *aot_file_path = g_build_filename (dir, aot_file, NULL);
9749 if (g_ensure_directory_exists (aot_file_path) == FALSE) {
9750 fprintf (stderr, "AOT : failed to create msym directory: %s\n", aot_file_path);
9751 exit (1);
9754 mono_seq_point_data_write (&sp_data, aot_file_path);
9755 mono_seq_point_data_free (&sp_data);
9757 g_free (aotid);
9758 g_free (dir);
9759 g_free (image_basename);
9760 g_free (aot_file);
9761 g_free (aot_file_path);
9764 acfg->stats.offsets_size += emit_offset_table (acfg, "ex_info_offsets", MONO_AOT_TABLE_EX_INFO_OFFSETS, acfg->nmethods, 10, offsets);
9765 g_free (offsets);
9768 static void
9769 emit_unwind_info (MonoAotCompile *acfg)
9771 int i;
9772 char symbol [128];
9774 if (acfg->aot_opts.llvm_only) {
9775 g_assert (acfg->unwind_ops->len == 0);
9776 return;
9780 * The unwind info contains a lot of duplicates so we emit each unique
9781 * entry once, and only store the offset from the start of the table in the
9782 * exception info.
9785 sprintf (symbol, "unwind_info");
9786 emit_section_change (acfg, RODATA_SECT, 1);
9787 emit_alignment (acfg, 8);
9788 emit_info_symbol (acfg, symbol);
9790 for (i = 0; i < acfg->unwind_ops->len; ++i) {
9791 guint32 index = GPOINTER_TO_UINT (g_ptr_array_index (acfg->unwind_ops, i));
9792 guint8 *unwind_info;
9793 guint32 unwind_info_len;
9794 guint8 buf [16];
9795 guint8 *p;
9797 unwind_info = mono_get_cached_unwind_info (index, &unwind_info_len);
9799 p = buf;
9800 encode_value (unwind_info_len, p, &p);
9801 emit_bytes (acfg, buf, p - buf);
9802 emit_bytes (acfg, unwind_info, unwind_info_len);
9804 acfg->stats.unwind_info_size += (p - buf) + unwind_info_len;
9808 static void
9809 emit_class_info (MonoAotCompile *acfg)
9811 int i;
9812 gint32 *offsets;
9814 offsets = g_new0 (gint32, acfg->image->tables [MONO_TABLE_TYPEDEF].rows);
9815 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i)
9816 offsets [i] = emit_klass_info (acfg, MONO_TOKEN_TYPE_DEF | (i + 1));
9818 acfg->stats.offsets_size += emit_offset_table (acfg, "class_info_offsets", MONO_AOT_TABLE_CLASS_INFO_OFFSETS, acfg->image->tables [MONO_TABLE_TYPEDEF].rows, 10, offsets);
9819 g_free (offsets);
9822 typedef struct ClassNameTableEntry {
9823 guint32 token, index;
9824 struct ClassNameTableEntry *next;
9825 } ClassNameTableEntry;
9827 static void
9828 emit_class_name_table (MonoAotCompile *acfg)
9830 int i, table_size, buf_size;
9831 guint32 token, hash;
9832 MonoClass *klass;
9833 GPtrArray *table;
9834 char *full_name;
9835 guint8 *buf, *p;
9836 ClassNameTableEntry *entry, *new_entry;
9839 * Construct a chained hash table for mapping class names to typedef tokens.
9841 table_size = g_spaced_primes_closest ((int)(acfg->image->tables [MONO_TABLE_TYPEDEF].rows * 1.5));
9842 table = g_ptr_array_sized_new (table_size);
9843 for (i = 0; i < table_size; ++i)
9844 g_ptr_array_add (table, NULL);
9845 for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
9846 ERROR_DECL (error);
9847 token = MONO_TOKEN_TYPE_DEF | (i + 1);
9848 klass = mono_class_get_checked (acfg->image, token, error);
9849 if (!klass) {
9850 mono_error_cleanup (error);
9851 continue;
9853 full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
9854 hash = mono_metadata_str_hash (full_name) % table_size;
9855 g_free (full_name);
9857 /* FIXME: Allocate from the mempool */
9858 new_entry = g_new0 (ClassNameTableEntry, 1);
9859 new_entry->token = token;
9861 entry = (ClassNameTableEntry *)g_ptr_array_index (table, hash);
9862 if (entry == NULL) {
9863 new_entry->index = hash;
9864 g_ptr_array_index (table, hash) = new_entry;
9865 } else {
9866 while (entry->next)
9867 entry = entry->next;
9869 entry->next = new_entry;
9870 new_entry->index = table->len;
9871 g_ptr_array_add (table, new_entry);
9875 /* Emit the table */
9876 buf_size = table->len * 4 + 4;
9877 p = buf = (guint8 *)g_malloc0 (buf_size);
9879 /* FIXME: Optimize memory usage */
9880 g_assert (table_size < 65000);
9881 encode_int16 (table_size, p, &p);
9882 g_assert (table->len < 65000);
9883 for (i = 0; i < table->len; ++i) {
9884 ClassNameTableEntry *entry = (ClassNameTableEntry *)g_ptr_array_index (table, i);
9886 if (entry == NULL) {
9887 encode_int16 (0, p, &p);
9888 encode_int16 (0, p, &p);
9889 } else {
9890 encode_int16 (mono_metadata_token_index (entry->token), p, &p);
9891 if (entry->next)
9892 encode_int16 (entry->next->index, p, &p);
9893 else
9894 encode_int16 (0, p, &p);
9896 g_free (entry);
9898 g_assert (p - buf <= buf_size);
9899 g_ptr_array_free (table, TRUE);
9901 emit_aot_data (acfg, MONO_AOT_TABLE_CLASS_NAME, "class_name_table", buf, p - buf);
9903 g_free (buf);
9906 static void
9907 emit_image_table (MonoAotCompile *acfg)
9909 int i, buf_size;
9910 guint8 *buf, *p;
9913 * The image table is small but referenced in a lot of places.
9914 * So we emit it at once, and reference its elements by an index.
9916 buf_size = acfg->image_table->len * 28 + 4;
9917 for (i = 0; i < acfg->image_table->len; i++) {
9918 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
9919 MonoAssemblyName *aname = &image->assembly->aname;
9921 buf_size += strlen (image->assembly_name) + strlen (image->guid) + (aname->culture ? strlen (aname->culture) : 1) + strlen ((char*)aname->public_key_token) + 4;
9924 buf = p = (guint8 *)g_malloc0 (buf_size);
9925 encode_int (acfg->image_table->len, p, &p);
9926 for (i = 0; i < acfg->image_table->len; i++) {
9927 MonoImage *image = (MonoImage*)g_ptr_array_index (acfg->image_table, i);
9928 MonoAssemblyName *aname = &image->assembly->aname;
9930 /* FIXME: Support multi-module assemblies */
9931 g_assert (image->assembly->image == image);
9933 encode_string (image->assembly_name, p, &p);
9934 encode_string (image->guid, p, &p);
9935 encode_string (aname->culture ? aname->culture : "", p, &p);
9936 encode_string ((const char*)aname->public_key_token, p, &p);
9938 while (GPOINTER_TO_UINT (p) % 8 != 0)
9939 p ++;
9941 encode_int (aname->flags, p, &p);
9942 encode_int (aname->major, p, &p);
9943 encode_int (aname->minor, p, &p);
9944 encode_int (aname->build, p, &p);
9945 encode_int (aname->revision, p, &p);
9947 g_assert (p - buf <= buf_size);
9949 emit_aot_data (acfg, MONO_AOT_TABLE_IMAGE_TABLE, "image_table", buf, p - buf);
9951 g_free (buf);
9954 static void
9955 emit_weak_field_indexes (MonoAotCompile *acfg)
9957 GHashTable *indexes;
9958 GHashTableIter iter;
9959 gpointer key, value;
9960 int buf_size;
9961 guint8 *buf, *p;
9963 /* Emit a table of weak field indexes, since computing these at runtime is expensive */
9964 mono_assembly_init_weak_fields (acfg->image);
9965 indexes = acfg->image->weak_field_indexes;
9966 g_assert (indexes);
9968 buf_size = (g_hash_table_size (indexes) + 1) * 4;
9969 buf = p = (guint8 *)g_malloc0 (buf_size);
9971 encode_int (g_hash_table_size (indexes), p, &p);
9972 g_hash_table_iter_init (&iter, indexes);
9973 while (g_hash_table_iter_next (&iter, &key, &value)) {
9974 guint32 index = GPOINTER_TO_UINT (key);
9975 encode_int (index, p, &p);
9977 g_assert (p - buf <= buf_size);
9979 emit_aot_data (acfg, MONO_AOT_TABLE_WEAK_FIELD_INDEXES, "weak_field_indexes", buf, p - buf);
9981 g_free (buf);
9984 static void
9985 emit_got_info (MonoAotCompile *acfg, gboolean llvm)
9987 int i, first_plt_got_patch = 0, buf_size;
9988 guint8 *p, *buf;
9989 guint32 *got_info_offsets;
9990 GotInfo *info = llvm ? &acfg->llvm_got_info : &acfg->got_info;
9992 /* Add the patches needed by the PLT to the GOT */
9993 if (!llvm) {
9994 acfg->plt_got_offset_base = acfg->got_offset;
9995 first_plt_got_patch = info->got_patches->len;
9996 for (i = 1; i < acfg->plt_offset; ++i) {
9997 MonoPltEntry *plt_entry = (MonoPltEntry *)g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
9999 g_ptr_array_add (info->got_patches, plt_entry->ji);
10001 acfg->stats.got_slot_types [plt_entry->ji->type] ++;
10004 acfg->got_offset += acfg->plt_offset;
10008 * FIXME:
10009 * - optimize offsets table.
10010 * - reduce number of exported symbols.
10011 * - emit info for a klass only once.
10012 * - determine when a method uses a GOT slot which is guaranteed to be already
10013 * initialized.
10014 * - clean up and document the code.
10015 * - use String.Empty in class libs.
10018 /* Encode info required to decode shared GOT entries */
10019 buf_size = info->got_patches->len * 128;
10020 p = buf = (guint8 *)mono_mempool_alloc (acfg->mempool, buf_size);
10021 got_info_offsets = (guint32 *)mono_mempool_alloc (acfg->mempool, info->got_patches->len * sizeof (guint32));
10022 if (!llvm) {
10023 acfg->plt_got_info_offsets = (guint32 *)mono_mempool_alloc (acfg->mempool, acfg->plt_offset * sizeof (guint32));
10024 /* Unused */
10025 if (acfg->plt_offset)
10026 acfg->plt_got_info_offsets [0] = 0;
10028 for (i = 0; i < info->got_patches->len; ++i) {
10029 MonoJumpInfo *ji = (MonoJumpInfo *)g_ptr_array_index (info->got_patches, i);
10030 guint8 *p2;
10032 p = buf;
10034 encode_value (ji->type, p, &p);
10035 p2 = p;
10036 encode_patch (acfg, ji, p, &p);
10037 acfg->stats.got_slot_info_sizes [ji->type] += p - p2;
10038 g_assert (p - buf <= buf_size);
10039 got_info_offsets [i] = add_to_blob (acfg, buf, p - buf);
10041 if (!llvm && i >= first_plt_got_patch)
10042 acfg->plt_got_info_offsets [i - first_plt_got_patch + 1] = got_info_offsets [i];
10043 acfg->stats.got_info_size += p - buf;
10046 /* Emit got_info_offsets table */
10048 /* No need to emit offsets for the got plt entries, the plt embeds them directly */
10049 acfg->stats.offsets_size += emit_offset_table (acfg, llvm ? "llvm_got_info_offsets" : "got_info_offsets", llvm ? MONO_AOT_TABLE_LLVM_GOT_INFO_OFFSETS : MONO_AOT_TABLE_GOT_INFO_OFFSETS, llvm ? acfg->llvm_got_offset : first_plt_got_patch, 10, (gint32*)got_info_offsets);
10052 static void
10053 emit_got (MonoAotCompile *acfg)
10055 char symbol [MAX_SYMBOL_SIZE];
10057 if (acfg->aot_opts.llvm_only)
10058 return;
10060 /* Don't make GOT global so accesses to it don't need relocations */
10061 sprintf (symbol, "%s", acfg->got_symbol);
10063 #ifdef TARGET_MACH
10064 emit_unset_mode (acfg);
10065 fprintf (acfg->fp, ".section __DATA, __bss\n");
10066 emit_alignment (acfg, 8);
10067 if (acfg->llvm)
10068 emit_info_symbol (acfg, "jit_got");
10069 fprintf (acfg->fp, ".lcomm %s, %d\n", acfg->got_symbol, (int)(acfg->got_offset * sizeof (gpointer)));
10070 #else
10071 emit_section_change (acfg, ".bss", 0);
10072 emit_alignment (acfg, 8);
10073 if (acfg->aot_opts.write_symbols)
10074 emit_local_symbol (acfg, symbol, "got_end", FALSE);
10075 emit_label (acfg, symbol);
10076 if (acfg->llvm)
10077 emit_info_symbol (acfg, "jit_got");
10078 if (acfg->got_offset > 0)
10079 emit_zero_bytes (acfg, (int)(acfg->got_offset * sizeof (gpointer)));
10080 #endif
10082 sprintf (symbol, "got_end");
10083 emit_label (acfg, symbol);
10086 typedef struct GlobalsTableEntry {
10087 guint32 value, index;
10088 struct GlobalsTableEntry *next;
10089 } GlobalsTableEntry;
10091 #ifdef TARGET_WIN32_MSVC
10092 #define DLL_ENTRY_POINT "DllMain"
10094 static void
10095 emit_library_info (MonoAotCompile *acfg)
10097 // Only include for shared libraries linked directly from generated object.
10098 if (link_shared_library (acfg)) {
10099 char *name = NULL;
10100 char symbol [MAX_SYMBOL_SIZE];
10102 // Ask linker to export all global symbols.
10103 emit_section_change (acfg, ".drectve", 0);
10104 for (guint i = 0; i < acfg->globals->len; ++i) {
10105 name = (char *)g_ptr_array_index (acfg->globals, i);
10106 g_assert (name != NULL);
10107 sprintf_s (symbol, MAX_SYMBOL_SIZE, " /EXPORT:%s", name);
10108 emit_string (acfg, symbol);
10111 // Emit DLLMain function, needed by MSVC linker for DLL's.
10112 // NOTE, DllMain should not go into exports above.
10113 emit_section_change (acfg, ".text", 0);
10114 emit_global (acfg, DLL_ENTRY_POINT, TRUE);
10115 emit_label (acfg, DLL_ENTRY_POINT);
10117 // Simple implementation of DLLMain, just returning TRUE.
10118 // For more information about DLLMain: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx
10119 fprintf (acfg->fp, "movl $1, %%eax\n");
10120 fprintf (acfg->fp, "ret\n");
10122 // Inform linker about our dll entry function.
10123 emit_section_change (acfg, ".drectve", 0);
10124 emit_string (acfg, "/ENTRY:" DLL_ENTRY_POINT);
10125 return;
10129 #else
10131 static inline void
10132 emit_library_info (MonoAotCompile *acfg)
10134 return;
10136 #endif
10138 static void
10139 emit_globals (MonoAotCompile *acfg)
10141 int i, table_size;
10142 guint32 hash;
10143 GPtrArray *table;
10144 char symbol [1024];
10145 GlobalsTableEntry *entry, *new_entry;
10147 if (!acfg->aot_opts.static_link)
10148 return;
10150 if (acfg->aot_opts.llvm_only) {
10151 g_assert (acfg->globals->len == 0);
10152 return;
10156 * When static linking, we emit a table containing our globals.
10160 * Construct a chained hash table for mapping global names to their index in
10161 * the globals table.
10163 table_size = g_spaced_primes_closest ((int)(acfg->globals->len * 1.5));
10164 table = g_ptr_array_sized_new (table_size);
10165 for (i = 0; i < table_size; ++i)
10166 g_ptr_array_add (table, NULL);
10167 for (i = 0; i < acfg->globals->len; ++i) {
10168 char *name = (char *)g_ptr_array_index (acfg->globals, i);
10170 hash = mono_metadata_str_hash (name) % table_size;
10172 /* FIXME: Allocate from the mempool */
10173 new_entry = g_new0 (GlobalsTableEntry, 1);
10174 new_entry->value = i;
10176 entry = (GlobalsTableEntry *)g_ptr_array_index (table, hash);
10177 if (entry == NULL) {
10178 new_entry->index = hash;
10179 g_ptr_array_index (table, hash) = new_entry;
10180 } else {
10181 while (entry->next)
10182 entry = entry->next;
10184 entry->next = new_entry;
10185 new_entry->index = table->len;
10186 g_ptr_array_add (table, new_entry);
10190 /* Emit the table */
10191 sprintf (symbol, ".Lglobals_hash");
10192 emit_section_change (acfg, RODATA_SECT, 0);
10193 emit_alignment (acfg, 8);
10194 emit_label (acfg, symbol);
10196 /* FIXME: Optimize memory usage */
10197 g_assert (table_size < 65000);
10198 emit_int16 (acfg, table_size);
10199 for (i = 0; i < table->len; ++i) {
10200 GlobalsTableEntry *entry = (GlobalsTableEntry *)g_ptr_array_index (table, i);
10202 if (entry == NULL) {
10203 emit_int16 (acfg, 0);
10204 emit_int16 (acfg, 0);
10205 } else {
10206 emit_int16 (acfg, entry->value + 1);
10207 if (entry->next)
10208 emit_int16 (acfg, entry->next->index);
10209 else
10210 emit_int16 (acfg, 0);
10214 /* Emit the names */
10215 for (i = 0; i < acfg->globals->len; ++i) {
10216 char *name = (char *)g_ptr_array_index (acfg->globals, i);
10218 sprintf (symbol, "name_%d", i);
10219 emit_section_change (acfg, RODATA_SECT, 1);
10220 #ifdef TARGET_MACH
10221 emit_alignment (acfg, 4);
10222 #endif
10223 emit_label (acfg, symbol);
10224 emit_string (acfg, name);
10227 /* Emit the globals table */
10228 sprintf (symbol, "globals");
10229 emit_section_change (acfg, ".data", 0);
10230 /* This is not a global, since it is accessed by the init function */
10231 emit_alignment (acfg, 8);
10232 emit_info_symbol (acfg, symbol);
10234 sprintf (symbol, "%sglobals_hash", acfg->temp_prefix);
10235 emit_pointer (acfg, symbol);
10237 for (i = 0; i < acfg->globals->len; ++i) {
10238 char *name = (char *)g_ptr_array_index (acfg->globals, i);
10240 sprintf (symbol, "name_%d", i);
10241 emit_pointer (acfg, symbol);
10243 g_assert (strlen (name) < sizeof (symbol));
10244 sprintf (symbol, "%s", name);
10245 emit_pointer (acfg, symbol);
10247 /* Null terminate the table */
10248 emit_int32 (acfg, 0);
10249 emit_int32 (acfg, 0);
10252 static void
10253 emit_mem_end (MonoAotCompile *acfg)
10255 char symbol [128];
10257 if (acfg->aot_opts.llvm_only)
10258 return;
10260 sprintf (symbol, "mem_end");
10261 emit_section_change (acfg, ".text", 1);
10262 emit_alignment_code (acfg, 8);
10263 emit_label (acfg, symbol);
10266 static void
10267 init_aot_file_info (MonoAotCompile *acfg, MonoAotFileInfo *info)
10269 int i;
10271 info->version = MONO_AOT_FILE_VERSION;
10272 info->plt_got_offset_base = acfg->plt_got_offset_base;
10273 info->got_size = acfg->got_offset * sizeof (gpointer);
10274 info->plt_size = acfg->plt_offset;
10275 info->nmethods = acfg->nmethods;
10276 info->flags = acfg->flags;
10277 info->opts = acfg->opts;
10278 info->simd_opts = acfg->simd_opts;
10279 info->gc_name_index = acfg->gc_name_offset;
10280 info->datafile_size = acfg->datafile_offset;
10281 for (i = 0; i < MONO_AOT_TABLE_NUM; ++i)
10282 info->table_offsets [i] = acfg->table_offsets [i];
10283 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
10284 info->num_trampolines [i] = acfg->num_trampolines [i];
10285 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
10286 info->trampoline_got_offset_base [i] = acfg->trampoline_got_offset_base [i];
10287 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
10288 info->trampoline_size [i] = acfg->trampoline_size [i];
10289 info->num_rgctx_fetch_trampolines = acfg->aot_opts.nrgctx_fetch_trampolines;
10291 info->double_align = MONO_ABI_ALIGNOF (double);
10292 info->long_align = MONO_ABI_ALIGNOF (gint64);
10293 info->generic_tramp_num = MONO_TRAMPOLINE_NUM;
10294 info->tramp_page_size = acfg->tramp_page_size;
10295 info->nshared_got_entries = acfg->nshared_got_entries;
10296 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
10297 info->tramp_page_code_offsets [i] = acfg->tramp_page_code_offsets [i];
10299 memcpy(&info->aotid, acfg->image->aotid, 16);
10302 static void
10303 emit_aot_file_info (MonoAotCompile *acfg, MonoAotFileInfo *info)
10305 char symbol [MAX_SYMBOL_SIZE];
10306 int i, sindex;
10307 const char **symbols;
10309 symbols = g_new0 (const char *, MONO_AOT_FILE_INFO_NUM_SYMBOLS);
10310 sindex = 0;
10311 symbols [sindex ++] = acfg->got_symbol;
10312 if (acfg->llvm) {
10313 symbols [sindex ++] = g_strdup_printf ("%s%s", acfg->user_symbol_prefix, acfg->llvm_got_symbol);
10314 symbols [sindex ++] = acfg->llvm_eh_frame_symbol;
10315 } else {
10316 symbols [sindex ++] = NULL;
10317 symbols [sindex ++] = NULL;
10319 /* llvm_get_method */
10320 symbols [sindex ++] = NULL;
10321 /* llvm_get_unbox_tramp */
10322 symbols [sindex ++] = NULL;
10323 if (!acfg->aot_opts.llvm_only) {
10324 symbols [sindex ++] = "jit_code_start";
10325 symbols [sindex ++] = "jit_code_end";
10326 symbols [sindex ++] = "method_addresses";
10327 } else {
10328 symbols [sindex ++] = NULL;
10329 symbols [sindex ++] = NULL;
10330 symbols [sindex ++] = NULL;
10333 if (acfg->data_outfile) {
10334 for (i = 0; i < MONO_AOT_TABLE_NUM; ++i)
10335 symbols [sindex ++] = NULL;
10336 } else {
10337 symbols [sindex ++] = "blob";
10338 symbols [sindex ++] = "class_name_table";
10339 symbols [sindex ++] = "class_info_offsets";
10340 symbols [sindex ++] = "method_info_offsets";
10341 symbols [sindex ++] = "ex_info_offsets";
10342 symbols [sindex ++] = "extra_method_info_offsets";
10343 symbols [sindex ++] = "extra_method_table";
10344 symbols [sindex ++] = "got_info_offsets";
10345 if (acfg->llvm)
10346 symbols [sindex ++] = "llvm_got_info_offsets";
10347 else
10348 symbols [sindex ++] = NULL;
10349 symbols [sindex ++] = "image_table";
10350 symbols [sindex ++] = "weak_field_indexes";
10353 symbols [sindex ++] = "mem_end";
10354 symbols [sindex ++] = "assembly_guid";
10355 symbols [sindex ++] = "runtime_version";
10356 if (acfg->num_trampoline_got_entries) {
10357 symbols [sindex ++] = "specific_trampolines";
10358 symbols [sindex ++] = "static_rgctx_trampolines";
10359 symbols [sindex ++] = "imt_trampolines";
10360 symbols [sindex ++] = "gsharedvt_arg_trampolines";
10361 } else {
10362 symbols [sindex ++] = NULL;
10363 symbols [sindex ++] = NULL;
10364 symbols [sindex ++] = NULL;
10365 symbols [sindex ++] = NULL;
10367 if (acfg->aot_opts.static_link) {
10368 symbols [sindex ++] = "globals";
10369 } else {
10370 symbols [sindex ++] = NULL;
10372 symbols [sindex ++] = "assembly_name";
10373 symbols [sindex ++] = "plt";
10374 symbols [sindex ++] = "plt_end";
10375 symbols [sindex ++] = "unwind_info";
10376 if (!acfg->aot_opts.llvm_only) {
10377 symbols [sindex ++] = "unbox_trampolines";
10378 symbols [sindex ++] = "unbox_trampolines_end";
10379 symbols [sindex ++] = "unbox_trampoline_addresses";
10380 } else {
10381 symbols [sindex ++] = NULL;
10382 symbols [sindex ++] = NULL;
10383 symbols [sindex ++] = NULL;
10386 g_assert (sindex == MONO_AOT_FILE_INFO_NUM_SYMBOLS);
10388 sprintf (symbol, "%smono_aot_file_info", acfg->user_symbol_prefix);
10389 emit_section_change (acfg, ".data", 0);
10390 emit_alignment (acfg, 8);
10391 emit_label (acfg, symbol);
10392 if (!acfg->aot_opts.static_link)
10393 emit_global (acfg, symbol, FALSE);
10395 /* The data emitted here must match MonoAotFileInfo. */
10397 emit_int32 (acfg, info->version);
10398 emit_int32 (acfg, info->dummy);
10401 * We emit pointers to our data structures instead of emitting global symbols which
10402 * point to them, to reduce the number of globals, and because using globals leads to
10403 * various problems (i.e. arm/thumb).
10405 for (i = 0; i < MONO_AOT_FILE_INFO_NUM_SYMBOLS; ++i)
10406 emit_pointer (acfg, symbols [i]);
10408 emit_int32 (acfg, info->plt_got_offset_base);
10409 emit_int32 (acfg, info->got_size);
10410 emit_int32 (acfg, info->plt_size);
10411 emit_int32 (acfg, info->nmethods);
10412 emit_int32 (acfg, info->flags);
10413 emit_int32 (acfg, info->opts);
10414 emit_int32 (acfg, info->simd_opts);
10415 emit_int32 (acfg, info->gc_name_index);
10416 emit_int32 (acfg, info->num_rgctx_fetch_trampolines);
10417 emit_int32 (acfg, info->double_align);
10418 emit_int32 (acfg, info->long_align);
10419 emit_int32 (acfg, info->generic_tramp_num);
10420 emit_int32 (acfg, info->tramp_page_size);
10421 emit_int32 (acfg, info->nshared_got_entries);
10422 emit_int32 (acfg, info->datafile_size);
10424 for (i = 0; i < MONO_AOT_TABLE_NUM; ++i)
10425 emit_int32 (acfg, info->table_offsets [i]);
10426 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
10427 emit_int32 (acfg, info->num_trampolines [i]);
10428 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
10429 emit_int32 (acfg, info->trampoline_got_offset_base [i]);
10430 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
10431 emit_int32 (acfg, info->trampoline_size [i]);
10432 for (i = 0; i < MONO_AOT_TRAMP_NUM; ++i)
10433 emit_int32 (acfg, info->tramp_page_code_offsets [i]);
10435 emit_bytes (acfg, info->aotid, 16);
10437 if (acfg->aot_opts.static_link) {
10438 emit_global_inner (acfg, acfg->static_linking_symbol, FALSE);
10439 emit_alignment (acfg, sizeof (gpointer));
10440 emit_label (acfg, acfg->static_linking_symbol);
10441 emit_pointer_2 (acfg, acfg->user_symbol_prefix, "mono_aot_file_info");
10446 * Emit a structure containing all the information not stored elsewhere.
10448 static void
10449 emit_file_info (MonoAotCompile *acfg)
10451 char *build_info;
10452 MonoAotFileInfo *info;
10454 if (acfg->aot_opts.bind_to_runtime_version) {
10455 build_info = mono_get_runtime_build_info ();
10456 emit_string_symbol (acfg, "runtime_version", build_info);
10457 g_free (build_info);
10458 } else {
10459 emit_string_symbol (acfg, "runtime_version", "");
10462 emit_string_symbol (acfg, "assembly_guid" , acfg->image->guid);
10464 /* Emit a string holding the assembly name */
10465 emit_string_symbol (acfg, "assembly_name", acfg->image->assembly->aname.name);
10467 info = g_new0 (MonoAotFileInfo, 1);
10468 init_aot_file_info (acfg, info);
10470 if (acfg->aot_opts.static_link) {
10471 char symbol [MAX_SYMBOL_SIZE];
10472 char *p;
10475 * Emit a global symbol which can be passed by an embedding app to
10476 * mono_aot_register_module (). The symbol points to a pointer to the the file info
10477 * structure.
10479 sprintf (symbol, "%smono_aot_module_%s_info", acfg->user_symbol_prefix, acfg->image->assembly->aname.name);
10481 /* Get rid of characters which cannot occur in symbols */
10482 p = symbol;
10483 for (p = symbol; *p; ++p) {
10484 if (!(isalnum (*p) || *p == '_'))
10485 *p = '_';
10487 acfg->static_linking_symbol = g_strdup (symbol);
10490 if (acfg->llvm)
10491 mono_llvm_emit_aot_file_info (info, acfg->has_jitted_code);
10492 else
10493 emit_aot_file_info (acfg, info);
10496 static void
10497 emit_blob (MonoAotCompile *acfg)
10499 acfg->blob_closed = TRUE;
10501 emit_aot_data (acfg, MONO_AOT_TABLE_BLOB, "blob", (guint8*)acfg->blob.data, acfg->blob.index);
10504 static void
10505 emit_objc_selectors (MonoAotCompile *acfg)
10507 int i;
10508 char symbol [128];
10510 if (!acfg->objc_selectors || acfg->objc_selectors->len == 0)
10511 return;
10514 * From
10515 * cat > foo.m << EOF
10516 * void *ret ()
10518 * return @selector(print:);
10520 * EOF
10523 mono_img_writer_emit_unset_mode (acfg->w);
10524 g_assert (acfg->fp);
10525 fprintf (acfg->fp, ".section __DATA,__objc_selrefs,literal_pointers,no_dead_strip\n");
10526 fprintf (acfg->fp, ".align 3\n");
10527 for (i = 0; i < acfg->objc_selectors->len; ++i) {
10528 sprintf (symbol, "L_OBJC_SELECTOR_REFERENCES_%d", i);
10529 emit_label (acfg, symbol);
10530 sprintf (symbol, "L_OBJC_METH_VAR_NAME_%d", i);
10531 emit_pointer (acfg, symbol);
10534 fprintf (acfg->fp, ".section __TEXT,__cstring,cstring_literals\n");
10535 for (i = 0; i < acfg->objc_selectors->len; ++i) {
10536 fprintf (acfg->fp, "L_OBJC_METH_VAR_NAME_%d:\n", i);
10537 fprintf (acfg->fp, ".asciz \"%s\"\n", (char*)g_ptr_array_index (acfg->objc_selectors, i));
10540 fprintf (acfg->fp, ".section __DATA,__objc_imageinfo,regular,no_dead_strip\n");
10541 fprintf (acfg->fp, ".align 3\n");
10542 fprintf (acfg->fp, "L_OBJC_IMAGE_INFO:\n");
10543 fprintf (acfg->fp, ".long 0\n");
10544 fprintf (acfg->fp, ".long 16\n");
10547 static void
10548 emit_dwarf_info (MonoAotCompile *acfg)
10550 #ifdef EMIT_DWARF_INFO
10551 int i;
10552 char symbol2 [128];
10554 /* DIEs for methods */
10555 for (i = 0; i < acfg->nmethods; ++i) {
10556 MonoCompile *cfg = acfg->cfgs [i];
10558 if (ignore_cfg (cfg))
10559 continue;
10561 // FIXME: LLVM doesn't define .Lme_...
10562 if (cfg->compile_llvm)
10563 continue;
10565 sprintf (symbol2, "%sme_%x", acfg->temp_prefix, i);
10567 mono_dwarf_writer_emit_method (acfg->dwarf, cfg, cfg->method, cfg->asm_symbol, symbol2, cfg->asm_debug_symbol, (guint8 *)cfg->jit_info->code_start, cfg->jit_info->code_size, cfg->args, cfg->locals, cfg->unwind_ops, mono_debug_find_method (cfg->jit_info->d.method, mono_domain_get ()));
10569 #endif
10572 #ifdef EMIT_WIN32_CODEVIEW_INFO
10573 typedef struct _CodeViewSubSectionData
10575 gchar *start_section;
10576 gchar *end_section;
10577 gchar *start_section_record;
10578 gchar *end_section_record;
10579 int section_type;
10580 int section_record_type;
10581 int section_id;
10582 } CodeViewSubsectionData;
10584 typedef struct _CodeViewCompilerVersion
10586 gint major;
10587 gint minor;
10588 gint revision;
10589 gint patch;
10590 } CodeViewCompilerVersion;
10592 #define CODEVIEW_SUBSECTION_SYMBOL_TYPE 0xF1
10593 #define CODEVIEW_SUBSECTION_RECORD_COMPILER_TYPE 0x113c
10594 #define CODEVIEW_SUBSECTION_RECORD_FUNCTION_START_TYPE 0x1147
10595 #define CODEVIEW_SUBSECTION_RECORD_FUNCTION_END_TYPE 0x114F
10596 #define CODEVIEW_CSHARP_LANGUAGE_TYPE 0x0A
10597 #define CODEVIEW_CPU_TYPE 0x0
10598 #define CODEVIEW_MAGIC_HEADER 0x4
10600 static void
10601 codeview_clear_subsection_data (CodeViewSubsectionData *section_data)
10603 g_free (section_data->start_section);
10604 g_free (section_data->end_section);
10605 g_free (section_data->start_section_record);
10606 g_free (section_data->end_section_record);
10608 memset (section_data, 0, sizeof (CodeViewSubsectionData));
10611 static void
10612 codeview_parse_compiler_version (gchar *version, CodeViewCompilerVersion *data)
10614 gint values[4] = { 0 };
10615 gint *value = values;
10617 while (*version && (value < values + G_N_ELEMENTS (values))) {
10618 if (isdigit (*version)) {
10619 *value *= 10;
10620 *value += *version - '0';
10622 else if (*version == '.') {
10623 value++;
10626 version++;
10629 data->major = values[0];
10630 data->minor = values[1];
10631 data->revision = values[2];
10632 data->patch = values[3];
10635 static void
10636 emit_codeview_start_subsection (MonoAotCompile *acfg, int section_id, int section_type, int section_record_type, CodeViewSubsectionData *section_data)
10638 // Starting a new subsection, clear old data.
10639 codeview_clear_subsection_data (section_data);
10641 // Keep subsection data.
10642 section_data->section_id = section_id;
10643 section_data->section_type = section_type;
10644 section_data->section_record_type = section_record_type;
10646 // Allocate all labels used in subsection.
10647 section_data->start_section = g_strdup_printf ("%scvs_%d", acfg->temp_prefix, section_data->section_id);
10648 section_data->end_section = g_strdup_printf ("%scvse_%d", acfg->temp_prefix, section_data->section_id);
10649 section_data->start_section_record = g_strdup_printf ("%scvsr_%d", acfg->temp_prefix, section_data->section_id);
10650 section_data->end_section_record = g_strdup_printf ("%scvsre_%d", acfg->temp_prefix, section_data->section_id);
10652 // Subsection type, function symbol.
10653 emit_int32 (acfg, section_data->section_type);
10655 // Subsection size.
10656 emit_symbol_diff (acfg, section_data->end_section, section_data->start_section, 0);
10657 emit_label (acfg, section_data->start_section);
10659 // Subsection record size.
10660 fprintf (acfg->fp, "\t.word %s - %s\n", section_data->end_section_record, section_data->start_section_record);
10661 emit_label (acfg, section_data->start_section_record);
10663 // Subsection record type.
10664 emit_int16 (acfg, section_record_type);
10667 static void
10668 emit_codeview_end_subsection (MonoAotCompile *acfg, CodeViewSubsectionData *section_data, int *section_id)
10670 g_assert (section_data->start_section);
10671 g_assert (section_data->end_section);
10672 g_assert (section_data->start_section_record);
10673 g_assert (section_data->end_section_record);
10675 emit_label (acfg, section_data->end_section_record);
10677 if (section_data->section_record_type == CODEVIEW_SUBSECTION_RECORD_FUNCTION_START_TYPE) {
10678 // Emit record length.
10679 emit_int16 (acfg, 2);
10681 // Emit specific record type end.
10682 emit_int16 (acfg, CODEVIEW_SUBSECTION_RECORD_FUNCTION_END_TYPE);
10685 emit_label (acfg, section_data->end_section);
10687 // Next subsection needs to be 4 byte aligned.
10688 emit_alignment (acfg, 4);
10690 *section_id = section_data->section_id + 1;
10691 codeview_clear_subsection_data (section_data);
10694 inline static void
10695 emit_codeview_start_symbol_subsection (MonoAotCompile *acfg, int section_id, int section_record_type, CodeViewSubsectionData *section_data)
10697 emit_codeview_start_subsection (acfg, section_id, CODEVIEW_SUBSECTION_SYMBOL_TYPE, section_record_type, section_data);
10700 inline static void
10701 emit_codeview_end_symbol_subsection (MonoAotCompile *acfg, CodeViewSubsectionData *section_data, int *section_id)
10703 emit_codeview_end_subsection (acfg, section_data, section_id);
10706 static void
10707 emit_codeview_compiler_info (MonoAotCompile *acfg, int *section_id)
10709 CodeViewSubsectionData section_data = { 0 };
10710 CodeViewCompilerVersion compiler_version = { 0 };
10712 // Start new compiler record subsection.
10713 emit_codeview_start_symbol_subsection (acfg, *section_id, CODEVIEW_SUBSECTION_RECORD_COMPILER_TYPE, &section_data);
10715 emit_int32 (acfg, CODEVIEW_CSHARP_LANGUAGE_TYPE);
10716 emit_int16 (acfg, CODEVIEW_CPU_TYPE);
10718 // Get compiler version information.
10719 codeview_parse_compiler_version (VERSION, &compiler_version);
10721 // Compiler frontend version, 4 digits.
10722 emit_int16 (acfg, compiler_version.major);
10723 emit_int16 (acfg, compiler_version.minor);
10724 emit_int16 (acfg, compiler_version.revision);
10725 emit_int16 (acfg, compiler_version.patch);
10727 // Compiler backend version, 4 digits (currently same as frontend).
10728 emit_int16 (acfg, compiler_version.major);
10729 emit_int16 (acfg, compiler_version.minor);
10730 emit_int16 (acfg, compiler_version.revision);
10731 emit_int16 (acfg, compiler_version.patch);
10733 // Compiler string.
10734 emit_string (acfg, "Mono AOT compiler");
10736 // Done with section.
10737 emit_codeview_end_symbol_subsection (acfg, &section_data, section_id);
10740 static void
10741 emit_codeview_function_info (MonoAotCompile *acfg, MonoMethod *method, int *section_id, gchar *symbol, gchar *symbol_start, gchar *symbol_end)
10743 CodeViewSubsectionData section_data = { 0 };
10744 gchar *full_method_name = NULL;
10746 // Start new function record subsection.
10747 emit_codeview_start_symbol_subsection (acfg, *section_id, CODEVIEW_SUBSECTION_RECORD_FUNCTION_START_TYPE, &section_data);
10749 // Emit 3 int 0 byte padding, currently not used.
10750 emit_zero_bytes (acfg, sizeof (int) * 3);
10752 // Emit size of function.
10753 emit_symbol_diff (acfg, symbol_end, symbol_start, 0);
10755 // Emit 3 int 0 byte padding, currently not used.
10756 emit_zero_bytes (acfg, sizeof (int) * 3);
10758 // Emit reallocation info.
10759 fprintf (acfg->fp, "\t.secrel32 %s\n", symbol);
10760 fprintf (acfg->fp, "\t.secidx %s\n", symbol);
10762 // Emit flag, currently not used.
10763 emit_zero_bytes (acfg, 1);
10765 // Emit function name, exclude signature since it should be described by own metadata.
10766 full_method_name = mono_method_full_name (method, FALSE);
10767 emit_string (acfg, full_method_name ? full_method_name : "");
10768 g_free (full_method_name);
10770 // Done with section.
10771 emit_codeview_end_symbol_subsection (acfg, &section_data, section_id);
10774 static void
10775 emit_codeview_info (MonoAotCompile *acfg)
10777 int i;
10778 int section_id = 0;
10779 gchar symbol_buffer[MAX_SYMBOL_SIZE];
10781 // Emit codeview debug info section
10782 emit_section_change (acfg, ".debug$S", 0);
10784 // Emit magic header.
10785 emit_int32 (acfg, CODEVIEW_MAGIC_HEADER);
10787 emit_codeview_compiler_info (acfg, &section_id);
10789 for (i = 0; i < acfg->nmethods; ++i) {
10790 MonoCompile *cfg = acfg->cfgs[i];
10792 if (!cfg)
10793 continue;
10795 int ret = g_snprintf (symbol_buffer, G_N_ELEMENTS (symbol_buffer), "%sme_%x", acfg->temp_prefix, i);
10796 if (ret > 0 && ret < G_N_ELEMENTS (symbol_buffer))
10797 emit_codeview_function_info (acfg, cfg->method, &section_id, cfg->asm_debug_symbol, cfg->asm_symbol, symbol_buffer);
10800 #else
10801 static void
10802 emit_codeview_info (MonoAotCompile *acfg)
10805 #endif /* EMIT_WIN32_CODEVIEW_INFO */
10807 #ifdef EMIT_WIN32_UNWIND_INFO
10808 static UnwindInfoSectionCacheItem *
10809 get_cached_unwind_info_section_item_win32 (MonoAotCompile *acfg, const char *function_start, const char *function_end, GSList *unwind_ops)
10811 UnwindInfoSectionCacheItem *item = NULL;
10813 if (!acfg->unwind_info_section_cache)
10814 acfg->unwind_info_section_cache = g_list_alloc ();
10816 PUNWIND_INFO unwind_info = mono_arch_unwindinfo_alloc_unwind_info (unwind_ops);
10818 // Search for unwind info in cache.
10819 GList *list = acfg->unwind_info_section_cache;
10820 int list_size = 0;
10821 while (list && list->data) {
10822 item = (UnwindInfoSectionCacheItem*)list->data;
10823 if (!memcmp (unwind_info, item->unwind_info, sizeof (UNWIND_INFO))) {
10824 // Cache hit, return cached item.
10825 return item;
10827 list = list->next;
10828 list_size++;
10831 // Add to cache.
10832 if (acfg->unwind_info_section_cache) {
10833 item = g_new0 (UnwindInfoSectionCacheItem, 1);
10834 if (item) {
10835 // Format .xdata section label for function, used to get unwind info address RVA.
10836 // Since the unwind info is similar for most functions, the symbol will be reused.
10837 item->xdata_section_label = g_strdup_printf ("%sunwind_%d", acfg->temp_prefix, list_size);
10839 // Cache unwind info data, used when checking cache for matching unwind info. NOTE, cache takes
10840 //over ownership of unwind info.
10841 item->unwind_info = unwind_info;
10843 // Needs to be emitted once.
10844 item->xdata_section_emitted = FALSE;
10846 // Prepend to beginning of list to speed up inserts.
10847 acfg->unwind_info_section_cache = g_list_prepend (acfg->unwind_info_section_cache, (gpointer)item);
10851 return item;
10854 static void
10855 free_unwind_info_section_cache_win32 (MonoAotCompile *acfg)
10857 GList *list = acfg->unwind_info_section_cache;
10859 while (list) {
10860 UnwindInfoSectionCacheItem *item = (UnwindInfoSectionCacheItem *)list->data;
10861 if (item) {
10862 g_free (item->xdata_section_label);
10863 mono_arch_unwindinfo_free_unwind_info (item->unwind_info);
10865 g_free (item);
10866 list->data = NULL;
10869 list = list->next;
10872 g_list_free (acfg->unwind_info_section_cache);
10873 acfg->unwind_info_section_cache = NULL;
10876 static void
10877 emit_unwind_info_data_win32 (MonoAotCompile *acfg, PUNWIND_INFO unwind_info)
10879 // Emit the unwind info struct.
10880 emit_bytes (acfg, (guint8*)unwind_info, sizeof (UNWIND_INFO) - (sizeof (UNWIND_CODE) * MONO_MAX_UNWIND_CODES));
10882 // Emit all unwind codes encoded in unwind info struct.
10883 PUNWIND_CODE current_unwind_node = &unwind_info->UnwindCode[MONO_MAX_UNWIND_CODES - unwind_info->CountOfCodes];
10884 PUNWIND_CODE last_unwind_node = &unwind_info->UnwindCode[MONO_MAX_UNWIND_CODES];
10886 while (current_unwind_node < last_unwind_node) {
10887 guint8 node_count = 0;
10888 switch (current_unwind_node->UnwindOp) {
10889 case UWOP_PUSH_NONVOL:
10890 case UWOP_ALLOC_SMALL:
10891 case UWOP_SET_FPREG:
10892 case UWOP_PUSH_MACHFRAME:
10893 node_count = 1;
10894 break;
10895 case UWOP_SAVE_NONVOL:
10896 case UWOP_SAVE_XMM128:
10897 node_count = 2;
10898 break;
10899 case UWOP_SAVE_NONVOL_FAR:
10900 case UWOP_SAVE_XMM128_FAR:
10901 node_count = 3;
10902 break;
10903 case UWOP_ALLOC_LARGE:
10904 if (current_unwind_node->OpInfo == 0)
10905 node_count = 2;
10906 else
10907 node_count = 3;
10908 break;
10909 default:
10910 g_assert (!"Unknown unwind opcode.");
10913 while (node_count > 0) {
10914 g_assert (current_unwind_node < last_unwind_node);
10916 //Emit current node.
10917 emit_bytes (acfg, (guint8*)current_unwind_node, sizeof (UNWIND_CODE));
10919 node_count--;
10920 current_unwind_node++;
10925 // Emit unwind info sections for each function. Unwind info on Windows x64 is emitted into two different sections.
10926 // .pdata includes the serialized DWORD aligned RVA's of function start, end and address of serialized
10927 // UNWIND_INFO struct emitted into .xdata, see https://msdn.microsoft.com/en-us/library/ft9x1kdx.aspx.
10928 // .xdata section includes DWORD aligned serialized version of UNWIND_INFO struct, https://msdn.microsoft.com/en-us/library/ddssxxy8.aspx.
10929 static void
10930 emit_unwind_info_sections_win32 (MonoAotCompile *acfg, const char *function_start, const char *function_end, GSList *unwind_ops)
10932 char *pdata_section_label = NULL;
10934 int temp_prefix_len = (acfg->temp_prefix != NULL) ? strlen (acfg->temp_prefix) : 0;
10935 if (strncmp (function_start, acfg->temp_prefix, temp_prefix_len)) {
10936 temp_prefix_len = 0;
10939 // Format .pdata section label for function.
10940 pdata_section_label = g_strdup_printf ("%spdata_%s", acfg->temp_prefix, function_start + temp_prefix_len);
10942 UnwindInfoSectionCacheItem *cache_item = get_cached_unwind_info_section_item_win32 (acfg, function_start, function_end, unwind_ops);
10943 g_assert (cache_item && cache_item->xdata_section_label && cache_item->unwind_info);
10945 // Emit .pdata section.
10946 emit_section_change (acfg, ".pdata", 0);
10947 emit_alignment (acfg, sizeof (DWORD));
10948 emit_label (acfg, pdata_section_label);
10950 // Emit function start address RVA.
10951 fprintf (acfg->fp, "\t.long %s@IMGREL\n", function_start);
10953 // Emit function end address RVA.
10954 fprintf (acfg->fp, "\t.long %s@IMGREL\n", function_end);
10956 // Emit unwind info address RVA.
10957 fprintf (acfg->fp, "\t.long %s@IMGREL\n", cache_item->xdata_section_label);
10959 if (!cache_item->xdata_section_emitted) {
10960 // Emit .xdata section.
10961 emit_section_change (acfg, ".xdata", 0);
10962 emit_alignment (acfg, sizeof (DWORD));
10963 emit_label (acfg, cache_item->xdata_section_label);
10965 // Emit unwind info into .xdata section.
10966 emit_unwind_info_data_win32 (acfg, cache_item->unwind_info);
10967 cache_item->xdata_section_emitted = TRUE;
10970 g_free (pdata_section_label);
10972 #endif
10974 static gboolean
10975 collect_methods (MonoAotCompile *acfg)
10977 int mindex, i;
10978 MonoImage *image = acfg->image;
10980 /* Collect methods */
10981 for (i = 0; i < image->tables [MONO_TABLE_METHOD].rows; ++i) {
10982 ERROR_DECL (error);
10983 MonoMethod *method;
10984 guint32 token = MONO_TOKEN_METHOD_DEF | (i + 1);
10986 method = mono_get_method_checked (acfg->image, token, NULL, NULL, error);
10988 if (!method) {
10989 aot_printerrf (acfg, "Failed to load method 0x%x from '%s' due to %s.\n", token, image->name, mono_error_get_message (error));
10990 aot_printerrf (acfg, "Run with MONO_LOG_LEVEL=debug for more information.\n");
10991 mono_error_cleanup (error);
10992 return FALSE;
10995 /* Load all methods eagerly to skip the slower lazy loading code */
10996 mono_class_setup_methods (method->klass);
10998 if (mono_aot_mode_is_full (&acfg->aot_opts) && method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) {
10999 /* Compile the wrapper instead */
11000 /* We do this here instead of add_wrappers () because it is easy to do it here */
11001 MonoMethod *wrapper = mono_marshal_get_native_wrapper (method, TRUE, TRUE);
11002 method = wrapper;
11005 /* FIXME: Some mscorlib methods don't have debug info */
11007 if (acfg->aot_opts.soft_debug && !method->wrapper_type) {
11008 if (!((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
11009 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
11010 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
11011 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))) {
11012 if (!mono_debug_lookup_method (method)) {
11013 fprintf (stderr, "Method %s has no debug info, probably the .mdb file for the assembly is missing.\n", mono_method_get_full_name (method));
11014 exit (1);
11020 if (method->is_generic || mono_class_is_gtd (method->klass)) {
11021 /* Compile the ref shared version instead */
11022 method = mini_get_shared_method_full (method, SHARE_MODE_NONE, error);
11023 if (!method) {
11024 aot_printerrf (acfg, "Failed to load method 0x%x from '%s' due to %s.\n", token, image->name, mono_error_get_message (error));
11025 aot_printerrf (acfg, "Run with MONO_LOG_LEVEL=debug for more information.\n");
11026 mono_error_cleanup (error);
11027 return FALSE;
11031 /* Since we add the normal methods first, their index will be equal to their zero based token index */
11032 add_method_with_index (acfg, method, i, FALSE);
11033 acfg->method_index ++;
11036 /* gsharedvt methods */
11037 for (mindex = 0; mindex < image->tables [MONO_TABLE_METHOD].rows; ++mindex) {
11038 ERROR_DECL (error);
11039 MonoMethod *method;
11040 guint32 token = MONO_TOKEN_METHOD_DEF | (mindex + 1);
11042 if (!(acfg->opts & MONO_OPT_GSHAREDVT))
11043 continue;
11045 method = mono_get_method_checked (acfg->image, token, NULL, NULL, error);
11046 report_loader_error (acfg, error, TRUE, "Failed to load method token 0x%x due to %s\n", i, mono_error_get_message (error));
11048 if (method->is_generic || mono_class_is_gtd (method->klass)) {
11049 MonoMethod *gshared;
11051 gshared = mini_get_shared_method_full (method, SHARE_MODE_GSHAREDVT, error);
11052 mono_error_assert_ok (error);
11054 add_extra_method (acfg, gshared);
11058 if (mono_aot_mode_is_full (&acfg->aot_opts) || mono_aot_mode_is_hybrid (&acfg->aot_opts))
11059 add_generic_instances (acfg);
11061 if (mono_aot_mode_is_full (&acfg->aot_opts))
11062 add_wrappers (acfg);
11063 return TRUE;
11066 static void
11067 compile_methods (MonoAotCompile *acfg)
11069 int i, methods_len;
11071 if (acfg->aot_opts.nthreads > 0) {
11072 GPtrArray *frag;
11073 int len, j;
11074 GPtrArray *threads;
11075 MonoThreadHandle *thread_handle;
11076 gpointer *user_data;
11077 MonoMethod **methods;
11079 methods_len = acfg->methods->len;
11081 len = acfg->methods->len / acfg->aot_opts.nthreads;
11082 g_assert (len > 0);
11084 * Partition the list of methods into fragments, and hand it to threads to
11085 * process.
11087 threads = g_ptr_array_new ();
11088 /* Make a copy since acfg->methods is modified by compile_method () */
11089 methods = g_new0 (MonoMethod*, methods_len);
11090 //memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
11091 for (i = 0; i < methods_len; ++i)
11092 methods [i] = (MonoMethod *)g_ptr_array_index (acfg->methods, i);
11093 i = 0;
11094 while (i < methods_len) {
11095 ERROR_DECL (error);
11096 MonoInternalThread *thread;
11098 frag = g_ptr_array_new ();
11099 for (j = 0; j < len; ++j) {
11100 if (i < methods_len) {
11101 g_ptr_array_add (frag, methods [i]);
11102 i ++;
11106 user_data = g_new0 (gpointer, 3);
11107 user_data [0] = acfg;
11108 user_data [1] = frag;
11110 thread = mono_thread_create_internal (mono_domain_get (), compile_thread_main, (gpointer) user_data, MONO_THREAD_CREATE_FLAGS_NONE, error);
11111 mono_error_assert_ok (error);
11113 thread_handle = mono_threads_open_thread_handle (thread->handle);
11114 g_ptr_array_add (threads, thread_handle);
11116 g_free (methods);
11118 for (i = 0; i < threads->len; ++i) {
11119 mono_thread_info_wait_one_handle (g_ptr_array_index (threads, i), MONO_INFINITE_WAIT, FALSE);
11120 mono_threads_close_thread_handle (g_ptr_array_index (threads, i));
11122 } else {
11123 methods_len = 0;
11126 /* Compile methods added by compile_method () or all methods if nthreads == 0 */
11127 for (i = methods_len; i < acfg->methods->len; ++i) {
11128 /* This can add new methods to acfg->methods */
11129 compile_method (acfg, (MonoMethod *)g_ptr_array_index (acfg->methods, i));
11133 static int
11134 compile_asm (MonoAotCompile *acfg)
11136 char *command, *objfile;
11137 char *outfile_name, *tmp_outfile_name, *llvm_ofile;
11138 const char *tool_prefix = acfg->aot_opts.tool_prefix ? acfg->aot_opts.tool_prefix : "";
11139 char *ld_flags = acfg->aot_opts.ld_flags ? acfg->aot_opts.ld_flags : g_strdup("");
11141 #ifdef TARGET_WIN32_MSVC
11142 #define AS_OPTIONS "-c -x assembler"
11143 #elif defined(TARGET_AMD64) && !defined(TARGET_MACH)
11144 #define AS_OPTIONS "--64"
11145 #elif defined(TARGET_POWERPC64)
11146 #define AS_OPTIONS "-a64 -mppc64"
11147 #elif defined(sparc) && SIZEOF_VOID_P == 8
11148 #define AS_OPTIONS "-xarch=v9"
11149 #elif defined(TARGET_X86) && defined(TARGET_MACH)
11150 #define AS_OPTIONS "-arch i386"
11151 #else
11152 #define AS_OPTIONS ""
11153 #endif
11155 #if defined(TARGET_OSX)
11156 #define AS_NAME "clang"
11157 #elif defined(TARGET_WIN32_MSVC)
11158 #define AS_NAME "clang.exe"
11159 #else
11160 #define AS_NAME "as"
11161 #endif
11163 #ifdef TARGET_WIN32_MSVC
11164 #define AS_OBJECT_FILE_SUFFIX "obj"
11165 #else
11166 #define AS_OBJECT_FILE_SUFFIX "o"
11167 #endif
11169 #if defined(sparc)
11170 #define LD_NAME "ld"
11171 #define LD_OPTIONS "-shared -G"
11172 #elif defined(__ppc__) && defined(TARGET_MACH)
11173 #define LD_NAME "gcc"
11174 #define LD_OPTIONS "-dynamiclib"
11175 #elif defined(TARGET_AMD64) && defined(TARGET_MACH)
11176 #define LD_NAME "clang"
11177 #define LD_OPTIONS "--shared"
11178 #elif defined(TARGET_WIN32_MSVC)
11179 #define LD_NAME "link.exe"
11180 #define LD_OPTIONS "/DLL /MACHINE:X64 /NOLOGO /INCREMENTAL:NO"
11181 #define LD_DEBUG_OPTIONS LD_OPTIONS " /DEBUG"
11182 #elif defined(TARGET_WIN32) && !defined(TARGET_ANDROID)
11183 #define LD_NAME "gcc"
11184 #define LD_OPTIONS "-shared"
11185 #elif defined(TARGET_X86) && defined(TARGET_MACH)
11186 #define LD_NAME "clang"
11187 #define LD_OPTIONS "-m32 -dynamiclib"
11188 #elif defined(TARGET_ARM) && !defined(TARGET_ANDROID)
11189 #define LD_NAME "gcc"
11190 #define LD_OPTIONS "--shared"
11191 #elif defined(TARGET_POWERPC64)
11192 #define LD_OPTIONS "-m elf64ppc"
11193 #endif
11195 #ifndef LD_OPTIONS
11196 #define LD_OPTIONS ""
11197 #endif
11199 if (acfg->aot_opts.asm_only) {
11200 aot_printf (acfg, "Output file: '%s'.\n", acfg->tmpfname);
11201 if (acfg->aot_opts.static_link)
11202 aot_printf (acfg, "Linking symbol: '%s'.\n", acfg->static_linking_symbol);
11203 if (acfg->llvm)
11204 aot_printf (acfg, "LLVM output file: '%s'.\n", acfg->llvm_sfile);
11205 return 0;
11208 if (acfg->aot_opts.static_link) {
11209 if (acfg->aot_opts.outfile)
11210 objfile = g_strdup_printf ("%s", acfg->aot_opts.outfile);
11211 else
11212 objfile = g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->image->name);
11213 } else {
11214 objfile = g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->tmpfname);
11217 #ifdef TARGET_OSX
11218 g_string_append (acfg->as_args, "-c -x assembler");
11219 #endif
11221 command = g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix, AS_NAME, AS_OPTIONS,
11222 acfg->as_args ? acfg->as_args->str : "",
11223 wrap_path (objfile), wrap_path (acfg->tmpfname));
11224 aot_printf (acfg, "Executing the native assembler: %s\n", command);
11225 if (execute_system (command) != 0) {
11226 g_free (command);
11227 g_free (objfile);
11228 return 1;
11231 if (acfg->llvm && !acfg->llvm_owriter) {
11232 command = g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix, AS_NAME, AS_OPTIONS,
11233 acfg->as_args ? acfg->as_args->str : "",
11234 wrap_path (acfg->llvm_ofile), wrap_path (acfg->llvm_sfile));
11235 aot_printf (acfg, "Executing the native assembler: %s\n", command);
11236 if (execute_system (command) != 0) {
11237 g_free (command);
11238 g_free (objfile);
11239 return 1;
11243 g_free (command);
11245 if (acfg->aot_opts.static_link) {
11246 aot_printf (acfg, "Output file: '%s'.\n", objfile);
11247 aot_printf (acfg, "Linking symbol: '%s'.\n", acfg->static_linking_symbol);
11248 g_free (objfile);
11249 return 0;
11252 if (acfg->aot_opts.outfile)
11253 outfile_name = g_strdup_printf ("%s", acfg->aot_opts.outfile);
11254 else
11255 outfile_name = g_strdup_printf ("%s%s", acfg->image->name, MONO_SOLIB_EXT);
11257 tmp_outfile_name = g_strdup_printf ("%s.tmp", outfile_name);
11259 if (acfg->llvm) {
11260 llvm_ofile = g_strdup_printf ("\"%s\"", acfg->llvm_ofile);
11261 } else {
11262 llvm_ofile = g_strdup ("");
11265 /* replace the ; flags separators with spaces */
11266 g_strdelimit (ld_flags, ";", ' ');
11268 if (acfg->aot_opts.llvm_only)
11269 ld_flags = g_strdup_printf ("%s %s", ld_flags, "-lstdc++");
11271 #ifdef TARGET_WIN32_MSVC
11272 g_assert (tmp_outfile_name != NULL);
11273 g_assert (objfile != NULL);
11274 command = g_strdup_printf ("\"%s%s\" %s %s /OUT:\"%s\" \"%s\"", tool_prefix, LD_NAME,
11275 acfg->aot_opts.nodebug ? LD_OPTIONS : LD_DEBUG_OPTIONS, ld_flags, tmp_outfile_name, objfile);
11276 #elif defined(LD_NAME)
11277 command = g_strdup_printf ("%s%s %s -o %s %s %s %s", tool_prefix, LD_NAME, LD_OPTIONS,
11278 wrap_path (tmp_outfile_name), wrap_path (llvm_ofile),
11279 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->tmpfname)), ld_flags);
11280 #else
11281 // Default (linux)
11282 if (acfg->aot_opts.tool_prefix) {
11283 /* Cross compiling */
11284 command = g_strdup_printf ("\"%sld\" %s -shared -o %s %s %s %s", tool_prefix, LD_OPTIONS,
11285 wrap_path (tmp_outfile_name), wrap_path (llvm_ofile),
11286 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->tmpfname)), ld_flags);
11287 } else {
11288 char *args = g_strdup_printf ("%s -shared -o %s %s %s %s", LD_OPTIONS,
11289 wrap_path (tmp_outfile_name), wrap_path (llvm_ofile),
11290 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX, acfg->tmpfname)), ld_flags);
11292 if (acfg->aot_opts.llvm_only) {
11293 command = g_strdup_printf ("clang++ %s", args);
11294 } else {
11295 command = g_strdup_printf ("\"%sld\" %s", tool_prefix, args);
11297 g_free (args);
11299 #endif
11300 aot_printf (acfg, "Executing the native linker: %s\n", command);
11301 if (execute_system (command) != 0) {
11302 g_free (tmp_outfile_name);
11303 g_free (outfile_name);
11304 g_free (command);
11305 g_free (objfile);
11306 g_free (ld_flags);
11307 return 1;
11310 g_free (command);
11312 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, MONO_SOLIB_EXT);
11313 printf ("Stripping the binary: %s\n", com);
11314 execute_system (com);
11315 g_free (com);*/
11317 #if defined(TARGET_ARM) && !defined(TARGET_MACH)
11319 * gas generates 'mapping symbols' each time code and data is mixed, which
11320 * happens a lot in emit_and_reloc_code (), so we need to get rid of them.
11322 command = g_strdup_printf ("\"%sstrip\" --strip-symbol=\\$a --strip-symbol=\\$d %s", wrap_path(tool_prefix), wrap_path(tmp_outfile_name));
11323 aot_printf (acfg, "Stripping the binary: %s\n", command);
11324 if (execute_system (command) != 0) {
11325 g_free (tmp_outfile_name);
11326 g_free (outfile_name);
11327 g_free (command);
11328 g_free (objfile);
11329 return 1;
11331 #endif
11333 if (0 != rename (tmp_outfile_name, outfile_name)) {
11334 if (G_FILE_ERROR_EXIST == g_file_error_from_errno (errno)) {
11335 /* Since we are rebuilding the module we need to be able to replace any old copies. Remove old file and retry rename operation. */
11336 unlink (outfile_name);
11337 rename (tmp_outfile_name, outfile_name);
11341 #if defined(TARGET_MACH)
11342 command = g_strdup_printf ("dsymutil \"%s\"", outfile_name);
11343 aot_printf (acfg, "Executing dsymutil: %s\n", command);
11344 if (execute_system (command) != 0) {
11345 return 1;
11347 #endif
11349 if (!acfg->aot_opts.save_temps)
11350 unlink (objfile);
11352 g_free (tmp_outfile_name);
11353 g_free (outfile_name);
11354 g_free (objfile);
11356 if (acfg->aot_opts.save_temps)
11357 aot_printf (acfg, "Retained input file.\n");
11358 else
11359 unlink (acfg->tmpfname);
11361 return 0;
11364 static guint8
11365 profread_byte (FILE *infile)
11367 guint8 i;
11368 int res;
11370 res = fread (&i, 1, 1, infile);
11371 g_assert (res == 1);
11372 return i;
11375 static int
11376 profread_int (FILE *infile)
11378 int i, res;
11380 res = fread (&i, 4, 1, infile);
11381 g_assert (res == 1);
11382 return i;
11385 static char*
11386 profread_string (FILE *infile)
11388 int len, res;
11389 char *pbuf;
11391 len = profread_int (infile);
11392 pbuf = (char*)g_malloc (len + 1);
11393 res = fread (pbuf, 1, len, infile);
11394 g_assert (res == len);
11395 pbuf [len] = '\0';
11396 return pbuf;
11399 static void
11400 load_profile_file (MonoAotCompile *acfg, char *filename)
11402 FILE *infile;
11403 char buf [1024];
11404 int res, len, version;
11405 char magic [32];
11407 infile = fopen (filename, "r");
11408 if (!infile) {
11409 fprintf (stderr, "Unable to open file '%s': %s.\n", filename, strerror (errno));
11410 exit (1);
11413 printf ("Using profile data file '%s'\n", filename);
11415 sprintf (magic, AOT_PROFILER_MAGIC);
11416 len = strlen (magic);
11417 res = fread (buf, 1, len, infile);
11418 magic [len] = '\0';
11419 buf [len] = '\0';
11420 if ((res != len) || strcmp (buf, magic) != 0) {
11421 printf ("Profile file has wrong header: '%s'.\n", buf);
11422 fclose (infile);
11423 exit (1);
11425 guint32 expected_version = (AOT_PROFILER_MAJOR_VERSION << 16) | AOT_PROFILER_MINOR_VERSION;
11426 version = profread_int (infile);
11427 if (version != expected_version) {
11428 printf ("Profile file has wrong version 0x%4x, expected 0x%4x.\n", version, expected_version);
11429 fclose (infile);
11430 exit (1);
11433 ProfileData *data = g_new0 (ProfileData, 1);
11434 data->images = g_hash_table_new (NULL, NULL);
11435 data->classes = g_hash_table_new (NULL, NULL);
11436 data->ginsts = g_hash_table_new (NULL, NULL);
11437 data->methods = g_hash_table_new (NULL, NULL);
11439 while (TRUE) {
11440 int type = profread_byte (infile);
11441 int id = profread_int (infile);
11443 if (type == AOTPROF_RECORD_NONE)
11444 break;
11446 switch (type) {
11447 case AOTPROF_RECORD_IMAGE: {
11448 ImageProfileData *idata = g_new0 (ImageProfileData, 1);
11449 idata->name = profread_string (infile);
11450 char *mvid = profread_string (infile);
11451 g_free (mvid);
11452 g_hash_table_insert (data->images, GINT_TO_POINTER (id), idata);
11453 break;
11455 case AOTPROF_RECORD_GINST: {
11456 int i;
11457 int len = profread_int (infile);
11459 GInstProfileData *gdata = g_new0 (GInstProfileData, 1);
11460 gdata->argc = len;
11461 gdata->argv = g_new0 (ClassProfileData*, len);
11463 for (i = 0; i < len; ++i) {
11464 int class_id = profread_int (infile);
11466 gdata->argv [i] = g_hash_table_lookup (data->classes, GINT_TO_POINTER (class_id));
11467 g_assert (gdata->argv [i]);
11469 g_hash_table_insert (data->ginsts, GINT_TO_POINTER (id), gdata);
11470 break;
11472 case AOTPROF_RECORD_TYPE: {
11473 int type = profread_byte (infile);
11475 switch (type) {
11476 case MONO_TYPE_CLASS: {
11477 int image_id = profread_int (infile);
11478 int ginst_id = profread_int (infile);
11479 char *class_name = profread_string (infile);
11481 ImageProfileData *image = g_hash_table_lookup (data->images, GINT_TO_POINTER (image_id));
11482 g_assert (image);
11484 char *p = strrchr (class_name, '.');
11485 g_assert (p);
11486 *p = '\0';
11488 ClassProfileData *cdata = g_new0 (ClassProfileData, 1);
11489 cdata->image = image;
11490 cdata->ns = g_strdup (class_name);
11491 cdata->name = g_strdup (p + 1);
11493 if (ginst_id != -1) {
11494 cdata->inst = g_hash_table_lookup (data->ginsts, GINT_TO_POINTER (ginst_id));
11495 g_assert (cdata->inst);
11497 g_free (class_name);
11499 g_hash_table_insert (data->classes, GINT_TO_POINTER (id), cdata);
11500 break;
11502 #if 0
11503 case MONO_TYPE_SZARRAY: {
11504 int elem_id = profread_int (infile);
11505 // FIXME:
11506 break;
11508 #endif
11509 default:
11510 g_assert_not_reached ();
11511 break;
11513 break;
11515 case AOTPROF_RECORD_METHOD: {
11516 int class_id = profread_int (infile);
11517 int ginst_id = profread_int (infile);
11518 int param_count = profread_int (infile);
11519 char *method_name = profread_string (infile);
11520 char *sig = profread_string (infile);
11522 ClassProfileData *klass = g_hash_table_lookup (data->classes, GINT_TO_POINTER (class_id));
11523 g_assert (klass);
11525 MethodProfileData *mdata = g_new0 (MethodProfileData, 1);
11526 mdata->id = id;
11527 mdata->klass = klass;
11528 mdata->name = method_name;
11529 mdata->signature = sig;
11530 mdata->param_count = param_count;
11532 if (ginst_id != -1) {
11533 mdata->inst = g_hash_table_lookup (data->ginsts, GINT_TO_POINTER (ginst_id));
11534 g_assert (mdata->inst);
11536 g_hash_table_insert (data->methods, GINT_TO_POINTER (id), mdata);
11537 break;
11539 default:
11540 printf ("%d\n", type);
11541 g_assert_not_reached ();
11542 break;
11546 fclose (infile);
11547 acfg->profile_data = g_list_append (acfg->profile_data, data);
11550 static void
11551 resolve_class (ClassProfileData *cdata);
11553 static void
11554 resolve_ginst (GInstProfileData *inst_data)
11556 int i;
11558 if (inst_data->inst)
11559 return;
11561 for (i = 0; i < inst_data->argc; ++i) {
11562 resolve_class (inst_data->argv [i]);
11563 if (!inst_data->argv [i]->klass)
11564 return;
11566 MonoType **args = g_new0 (MonoType*, inst_data->argc);
11567 for (i = 0; i < inst_data->argc; ++i)
11568 args [i] = m_class_get_byval_arg (inst_data->argv [i]->klass);
11570 inst_data->inst = mono_metadata_get_generic_inst (inst_data->argc, args);
11573 static void
11574 resolve_class (ClassProfileData *cdata)
11576 ERROR_DECL (error);
11577 MonoClass *klass;
11579 if (!cdata->image->image)
11580 return;
11582 klass = mono_class_from_name_checked (cdata->image->image, cdata->ns, cdata->name, error);
11583 if (!klass) {
11584 //printf ("[%s] %s.%s\n", cdata->image->name, cdata->ns, cdata->name);
11585 return;
11587 if (cdata->inst) {
11588 resolve_ginst (cdata->inst);
11589 if (!cdata->inst->inst)
11590 return;
11591 MonoGenericContext ctx;
11593 memset (&ctx, 0, sizeof (ctx));
11594 ctx.class_inst = cdata->inst->inst;
11595 cdata->klass = mono_class_inflate_generic_class_checked (klass, &ctx, error);
11596 } else {
11597 cdata->klass = klass;
11602 * Resolve the profile data to the corresponding loaded classes/methods etc. if possible.
11604 static void
11605 resolve_profile_data (MonoAotCompile *acfg, ProfileData *data)
11607 GHashTableIter iter;
11608 gpointer key, value;
11609 int i;
11611 if (!data)
11612 return;
11614 /* Images */
11615 GPtrArray *assemblies = mono_domain_get_assemblies (mono_get_root_domain (), FALSE);
11616 g_hash_table_iter_init (&iter, data->images);
11617 while (g_hash_table_iter_next (&iter, &key, &value)) {
11618 ImageProfileData *idata = (ImageProfileData*)value;
11620 for (i = 0; i < assemblies->len; ++i) {
11621 MonoAssembly *ass = g_ptr_array_index (assemblies, i);
11623 if (!strcmp (ass->aname.name, idata->name)) {
11624 idata->image = ass->image;
11625 break;
11629 g_ptr_array_free (assemblies, TRUE);
11631 /* Classes */
11632 g_hash_table_iter_init (&iter, data->classes);
11633 while (g_hash_table_iter_next (&iter, &key, &value)) {
11634 ClassProfileData *cdata = (ClassProfileData*)value;
11636 if (!cdata->image->image) {
11637 if (acfg->aot_opts.verbose)
11638 printf ("Unable to load class '%s.%s' because its image '%s' is not loaded.\n", cdata->ns, cdata->name, cdata->image->name);
11639 continue;
11642 resolve_class (cdata);
11644 if (cdata->klass)
11645 printf ("%s %s %s\n", cdata->ns, cdata->name, mono_class_full_name (cdata->klass));
11649 /* Methods */
11650 g_hash_table_iter_init (&iter, data->methods);
11651 while (g_hash_table_iter_next (&iter, &key, &value)) {
11652 MethodProfileData *mdata = (MethodProfileData*)value;
11653 MonoClass *klass;
11654 MonoMethod *m;
11655 gpointer miter;
11657 resolve_class (mdata->klass);
11658 klass = mdata->klass->klass;
11659 if (!klass) {
11660 if (acfg->aot_opts.verbose)
11661 printf ("Unable to load method '%s' because its class '%s.%s' is not loaded.\n", mdata->name, mdata->klass->ns, mdata->klass->name);
11662 continue;
11664 miter = NULL;
11665 while ((m = mono_class_get_methods (klass, &miter))) {
11666 ERROR_DECL (error);
11668 if (strcmp (m->name, mdata->name))
11669 continue;
11670 MonoMethodSignature *sig = mono_method_signature (m);
11671 if (!sig)
11672 continue;
11673 if (sig->param_count != mdata->param_count)
11674 continue;
11675 if (mdata->inst) {
11676 resolve_ginst (mdata->inst);
11677 if (!mdata->inst->inst)
11678 continue;
11679 MonoGenericContext ctx;
11681 memset (&ctx, 0, sizeof (ctx));
11682 ctx.method_inst = mdata->inst->inst;
11684 m = mono_class_inflate_generic_method_checked (m, &ctx, error);
11685 if (!m)
11686 continue;
11687 sig = mono_method_signature_checked (m, error);
11688 if (!is_ok (error)) {
11689 mono_error_cleanup (error);
11690 continue;
11693 char *sig_str = mono_signature_full_name (sig);
11694 gboolean match = !strcmp (sig_str, mdata->signature);
11695 g_free (sig_str);
11696 if (!match)
11698 continue;
11699 //printf ("%s\n", mono_method_full_name (m, 1));
11700 mdata->method = m;
11701 break;
11703 if (!mdata->method) {
11704 if (acfg->aot_opts.verbose)
11705 printf ("Unable to load method '%s' from class '%s', not found.\n", mdata->name, mono_class_full_name (klass));
11710 static gboolean
11711 inst_references_image (MonoGenericInst *inst, MonoImage *image)
11713 int i;
11715 for (i = 0; i < inst->type_argc; ++i) {
11716 MonoClass *k = mono_class_from_mono_type (inst->type_argv [i]);
11717 if (m_class_get_image (k) == image)
11718 return TRUE;
11719 if (mono_class_is_ginst (k)) {
11720 MonoGenericInst *kinst = mono_class_get_context (k)->class_inst;
11721 if (inst_references_image (kinst, image))
11722 return TRUE;
11725 return FALSE;
11728 static gboolean
11729 is_local_inst (MonoGenericInst *inst, MonoImage *image)
11731 int i;
11733 for (i = 0; i < inst->type_argc; ++i) {
11734 MonoClass *k = mono_class_from_mono_type (inst->type_argv [i]);
11735 if (!MONO_TYPE_IS_PRIMITIVE (inst->type_argv [i]) && m_class_get_image (k) != image)
11736 return FALSE;
11738 return TRUE;
11741 static void
11742 add_profile_instances (MonoAotCompile *acfg, ProfileData *data)
11744 GHashTableIter iter;
11745 gpointer key, value;
11746 int count = 0;
11748 if (!data)
11749 return;
11751 if (acfg->aot_opts.profile_only) {
11752 /* Add methods referenced by the profile */
11753 g_hash_table_iter_init (&iter, data->methods);
11754 while (g_hash_table_iter_next (&iter, &key, &value)) {
11755 MethodProfileData *mdata = (MethodProfileData*)value;
11756 MonoMethod *m = mdata->method;
11758 if (!m)
11759 continue;
11760 if (m->is_inflated)
11761 continue;
11762 add_extra_method (acfg, m);
11763 g_hash_table_insert (acfg->profile_methods, m, m);
11764 count ++;
11769 * Add method instances 'related' to this assembly to the AOT image.
11771 g_hash_table_iter_init (&iter, data->methods);
11772 while (g_hash_table_iter_next (&iter, &key, &value)) {
11773 MethodProfileData *mdata = (MethodProfileData*)value;
11774 MonoMethod *m = mdata->method;
11775 MonoGenericContext *ctx;
11777 if (!m)
11778 continue;
11779 if (!m->is_inflated)
11780 continue;
11782 ctx = mono_method_get_context (m);
11783 /* For simplicity, add instances which reference the assembly we are compiling */
11784 if (((ctx->class_inst && inst_references_image (ctx->class_inst, acfg->image)) ||
11785 (ctx->method_inst && inst_references_image (ctx->method_inst, acfg->image))) &&
11786 !mono_method_is_generic_sharable_full (m, FALSE, FALSE, FALSE)) {
11787 //printf ("%s\n", mono_method_full_name (m, TRUE));
11788 add_extra_method (acfg, m);
11789 count ++;
11790 } else if (m_class_get_image (m->klass) == acfg->image &&
11791 ((ctx->class_inst && is_local_inst (ctx->class_inst, acfg->image)) ||
11792 (ctx->method_inst && is_local_inst (ctx->method_inst, acfg->image))) &&
11793 !mono_method_is_generic_sharable_full (m, FALSE, FALSE, FALSE)) {
11794 /* Add instances where the gtd is in the assembly and its inflated with types from this assembly or corlib */
11795 //printf ("%s\n", mono_method_full_name (m, TRUE));
11796 add_extra_method (acfg, m);
11797 count ++;
11800 * FIXME: We might skip some instances, for example:
11801 * Foo<Bar> won't be compiled when compiling Foo's assembly since it doesn't match the first case,
11802 * and it won't be compiled when compiling Bar's assembly if Foo's assembly is not loaded.
11806 printf ("Added %d methods from profile.\n", count);
11809 static void
11810 init_got_info (GotInfo *info)
11812 int i;
11814 info->patch_to_got_offset = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
11815 info->patch_to_got_offset_by_type = g_new0 (GHashTable*, MONO_PATCH_INFO_NUM);
11816 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
11817 info->patch_to_got_offset_by_type [i] = g_hash_table_new (mono_patch_info_hash, mono_patch_info_equal);
11818 info->got_patches = g_ptr_array_new ();
11821 static MonoAotCompile*
11822 acfg_create (MonoAssembly *ass, guint32 opts)
11824 MonoImage *image = ass->image;
11825 MonoAotCompile *acfg;
11827 acfg = g_new0 (MonoAotCompile, 1);
11828 acfg->methods = g_ptr_array_new ();
11829 acfg->method_indexes = g_hash_table_new (NULL, NULL);
11830 acfg->method_depth = g_hash_table_new (NULL, NULL);
11831 acfg->plt_offset_to_entry = g_hash_table_new (NULL, NULL);
11832 acfg->patch_to_plt_entry = g_new0 (GHashTable*, MONO_PATCH_INFO_NUM);
11833 acfg->method_to_cfg = g_hash_table_new (NULL, NULL);
11834 acfg->token_info_hash = g_hash_table_new_full (NULL, NULL, NULL, NULL);
11835 acfg->method_to_pinvoke_import = g_hash_table_new_full (NULL, NULL, NULL, g_free);
11836 acfg->image_hash = g_hash_table_new (NULL, NULL);
11837 acfg->image_table = g_ptr_array_new ();
11838 acfg->globals = g_ptr_array_new ();
11839 acfg->image = image;
11840 acfg->opts = opts;
11841 /* TODO: Write out set of SIMD instructions used, rather than just those available */
11842 acfg->simd_opts = mono_arch_cpu_enumerate_simd_versions ();
11843 acfg->mempool = mono_mempool_new ();
11844 acfg->extra_methods = g_ptr_array_new ();
11845 acfg->unwind_info_offsets = g_hash_table_new (NULL, NULL);
11846 acfg->unwind_ops = g_ptr_array_new ();
11847 acfg->method_label_hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
11848 acfg->method_order = g_ptr_array_new ();
11849 acfg->export_names = g_hash_table_new (NULL, NULL);
11850 acfg->klass_blob_hash = g_hash_table_new (NULL, NULL);
11851 acfg->method_blob_hash = g_hash_table_new (NULL, NULL);
11852 acfg->plt_entry_debug_sym_cache = g_hash_table_new (g_str_hash, g_str_equal);
11853 acfg->gsharedvt_in_signatures = g_hash_table_new ((GHashFunc)mono_signature_hash, (GEqualFunc)mono_metadata_signature_equal);
11854 acfg->gsharedvt_out_signatures = g_hash_table_new ((GHashFunc)mono_signature_hash, (GEqualFunc)mono_metadata_signature_equal);
11855 acfg->profile_methods = g_hash_table_new (NULL, NULL);
11856 mono_os_mutex_init_recursive (&acfg->mutex);
11858 init_got_info (&acfg->got_info);
11859 init_got_info (&acfg->llvm_got_info);
11861 return acfg;
11864 static void
11865 got_info_free (GotInfo *info)
11867 int i;
11869 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
11870 g_hash_table_destroy (info->patch_to_got_offset_by_type [i]);
11871 g_free (info->patch_to_got_offset_by_type);
11872 g_hash_table_destroy (info->patch_to_got_offset);
11873 g_ptr_array_free (info->got_patches, TRUE);
11876 static void
11877 acfg_free (MonoAotCompile *acfg)
11879 int i;
11881 mono_img_writer_destroy (acfg->w);
11882 for (i = 0; i < acfg->nmethods; ++i)
11883 if (acfg->cfgs [i])
11884 mono_destroy_compile (acfg->cfgs [i]);
11886 g_free (acfg->cfgs);
11888 g_free (acfg->static_linking_symbol);
11889 g_free (acfg->got_symbol);
11890 g_free (acfg->plt_symbol);
11891 g_ptr_array_free (acfg->methods, TRUE);
11892 g_ptr_array_free (acfg->image_table, TRUE);
11893 g_ptr_array_free (acfg->globals, TRUE);
11894 g_ptr_array_free (acfg->unwind_ops, TRUE);
11895 g_hash_table_destroy (acfg->method_indexes);
11896 g_hash_table_destroy (acfg->method_depth);
11897 g_hash_table_destroy (acfg->plt_offset_to_entry);
11898 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i) {
11899 if (acfg->patch_to_plt_entry [i])
11900 g_hash_table_destroy (acfg->patch_to_plt_entry [i]);
11902 g_free (acfg->patch_to_plt_entry);
11903 g_hash_table_destroy (acfg->method_to_cfg);
11904 g_hash_table_destroy (acfg->token_info_hash);
11905 g_hash_table_destroy (acfg->method_to_pinvoke_import);
11906 g_hash_table_destroy (acfg->image_hash);
11907 g_hash_table_destroy (acfg->unwind_info_offsets);
11908 g_hash_table_destroy (acfg->method_label_hash);
11909 if (acfg->typespec_classes)
11910 g_hash_table_destroy (acfg->typespec_classes);
11911 g_hash_table_destroy (acfg->export_names);
11912 g_hash_table_destroy (acfg->plt_entry_debug_sym_cache);
11913 g_hash_table_destroy (acfg->klass_blob_hash);
11914 g_hash_table_destroy (acfg->method_blob_hash);
11915 got_info_free (&acfg->got_info);
11916 got_info_free (&acfg->llvm_got_info);
11917 arch_free_unwind_info_section_cache (acfg);
11918 mono_mempool_destroy (acfg->mempool);
11919 g_free (acfg);
11922 #define WRAPPER(e,n) n,
11923 static const char* const
11924 wrapper_type_names [MONO_WRAPPER_NUM + 1] = {
11925 #include "mono/metadata/wrapper-types.h"
11926 NULL
11929 static G_GNUC_UNUSED const char*
11930 get_wrapper_type_name (int type)
11932 return wrapper_type_names [type];
11935 //#define DUMP_PLT
11936 //#define DUMP_GOT
11938 static void aot_dump (MonoAotCompile *acfg)
11940 FILE *dumpfile;
11941 char * dumpname;
11943 JsonWriter writer;
11944 mono_json_writer_init (&writer);
11946 mono_json_writer_object_begin(&writer);
11948 // Methods
11949 mono_json_writer_indent (&writer);
11950 mono_json_writer_object_key(&writer, "methods");
11951 mono_json_writer_array_begin (&writer);
11953 int i;
11954 for (i = 0; i < acfg->nmethods; ++i) {
11955 MonoCompile *cfg;
11956 MonoMethod *method;
11957 MonoClass *klass;
11959 cfg = acfg->cfgs [i];
11960 if (ignore_cfg (cfg))
11961 continue;
11963 method = cfg->orig_method;
11965 mono_json_writer_indent (&writer);
11966 mono_json_writer_object_begin(&writer);
11968 mono_json_writer_indent (&writer);
11969 mono_json_writer_object_key(&writer, "name");
11970 mono_json_writer_printf (&writer, "\"%s\",\n", method->name);
11972 mono_json_writer_indent (&writer);
11973 mono_json_writer_object_key(&writer, "signature");
11974 mono_json_writer_printf (&writer, "\"%s\",\n", mono_method_get_full_name (method));
11976 mono_json_writer_indent (&writer);
11977 mono_json_writer_object_key(&writer, "code_size");
11978 mono_json_writer_printf (&writer, "\"%d\",\n", cfg->code_size);
11980 klass = method->klass;
11982 mono_json_writer_indent (&writer);
11983 mono_json_writer_object_key(&writer, "class");
11984 mono_json_writer_printf (&writer, "\"%s\",\n", m_class_get_name (klass));
11986 mono_json_writer_indent (&writer);
11987 mono_json_writer_object_key(&writer, "namespace");
11988 mono_json_writer_printf (&writer, "\"%s\",\n", m_class_get_name_space (klass));
11990 mono_json_writer_indent (&writer);
11991 mono_json_writer_object_key(&writer, "wrapper_type");
11992 mono_json_writer_printf (&writer, "\"%s\",\n", get_wrapper_type_name(method->wrapper_type));
11994 mono_json_writer_indent_pop (&writer);
11995 mono_json_writer_indent (&writer);
11996 mono_json_writer_object_end (&writer);
11997 mono_json_writer_printf (&writer, ",\n");
12000 mono_json_writer_indent_pop (&writer);
12001 mono_json_writer_indent (&writer);
12002 mono_json_writer_array_end (&writer);
12003 mono_json_writer_printf (&writer, ",\n");
12005 // PLT entries
12006 #ifdef DUMP_PLT
12007 mono_json_writer_indent_push (&writer);
12008 mono_json_writer_indent (&writer);
12009 mono_json_writer_object_key(&writer, "plt");
12010 mono_json_writer_array_begin (&writer);
12012 for (i = 0; i < acfg->plt_offset; ++i) {
12013 MonoPltEntry *plt_entry = NULL;
12014 MonoJumpInfo *ji;
12016 if (i == 0)
12018 * The first plt entry is unused.
12020 continue;
12022 plt_entry = g_hash_table_lookup (acfg->plt_offset_to_entry, GUINT_TO_POINTER (i));
12023 ji = plt_entry->ji;
12025 mono_json_writer_indent (&writer);
12026 mono_json_writer_printf (&writer, "{ ");
12027 mono_json_writer_object_key(&writer, "symbol");
12028 mono_json_writer_printf (&writer, "\"%s\" },\n", plt_entry->symbol);
12031 mono_json_writer_indent_pop (&writer);
12032 mono_json_writer_indent (&writer);
12033 mono_json_writer_array_end (&writer);
12034 mono_json_writer_printf (&writer, ",\n");
12035 #endif
12037 // GOT entries
12038 #ifdef DUMP_GOT
12039 mono_json_writer_indent_push (&writer);
12040 mono_json_writer_indent (&writer);
12041 mono_json_writer_object_key(&writer, "got");
12042 mono_json_writer_array_begin (&writer);
12044 mono_json_writer_indent_push (&writer);
12045 for (i = 0; i < acfg->got_info.got_patches->len; ++i) {
12046 MonoJumpInfo *ji = g_ptr_array_index (acfg->got_info.got_patches, i);
12048 mono_json_writer_indent (&writer);
12049 mono_json_writer_printf (&writer, "{ ");
12050 mono_json_writer_object_key(&writer, "patch_name");
12051 mono_json_writer_printf (&writer, "\"%s\" },\n", get_patch_name (ji->type));
12054 mono_json_writer_indent_pop (&writer);
12055 mono_json_writer_indent (&writer);
12056 mono_json_writer_array_end (&writer);
12057 mono_json_writer_printf (&writer, ",\n");
12058 #endif
12060 mono_json_writer_indent_pop (&writer);
12061 mono_json_writer_indent (&writer);
12062 mono_json_writer_object_end (&writer);
12064 dumpname = g_strdup_printf ("%s.json", g_path_get_basename (acfg->image->name));
12065 dumpfile = fopen (dumpname, "w+");
12066 g_free (dumpname);
12068 fprintf (dumpfile, "%s", writer.text->str);
12069 fclose (dumpfile);
12071 mono_json_writer_destroy (&writer);
12074 static const char *preinited_jit_icalls[] = {
12075 "mono_aot_init_llvm_method",
12076 "mono_aot_init_gshared_method_this",
12077 "mono_aot_init_gshared_method_mrgctx",
12078 "mono_aot_init_gshared_method_vtable",
12079 "mono_llvm_throw_corlib_exception",
12080 "mono_init_vtable_slot",
12081 "mono_helper_ldstr_mscorlib"
12084 static void
12085 add_preinit_got_slots (MonoAotCompile *acfg)
12087 MonoJumpInfo *ji;
12088 int i;
12091 * Allocate the first few GOT entries to information which is needed frequently, or it is needed
12092 * during method initialization etc.
12095 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12096 ji->type = MONO_PATCH_INFO_IMAGE;
12097 ji->data.image = acfg->image;
12098 get_got_offset (acfg, FALSE, ji);
12099 get_got_offset (acfg, TRUE, ji);
12101 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12102 ji->type = MONO_PATCH_INFO_MSCORLIB_GOT_ADDR;
12103 get_got_offset (acfg, FALSE, ji);
12104 get_got_offset (acfg, TRUE, ji);
12106 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12107 ji->type = MONO_PATCH_INFO_GC_CARD_TABLE_ADDR;
12108 get_got_offset (acfg, FALSE, ji);
12109 get_got_offset (acfg, TRUE, ji);
12111 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12112 ji->type = MONO_PATCH_INFO_GC_NURSERY_START;
12113 get_got_offset (acfg, FALSE, ji);
12114 get_got_offset (acfg, TRUE, ji);
12116 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12117 ji->type = MONO_PATCH_INFO_AOT_MODULE;
12118 get_got_offset (acfg, FALSE, ji);
12119 get_got_offset (acfg, TRUE, ji);
12121 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12122 ji->type = MONO_PATCH_INFO_GC_NURSERY_BITS;
12123 get_got_offset (acfg, FALSE, ji);
12124 get_got_offset (acfg, TRUE, ji);
12126 for (i = 0; i < TLS_KEY_NUM; i++) {
12127 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12128 ji->type = MONO_PATCH_INFO_GET_TLS_TRAMP;
12129 ji->data.index = i;
12130 get_got_offset (acfg, FALSE, ji);
12131 get_got_offset (acfg, TRUE, ji);
12133 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12134 ji->type = MONO_PATCH_INFO_SET_TLS_TRAMP;
12135 ji->data.index = i;
12136 get_got_offset (acfg, FALSE, ji);
12137 get_got_offset (acfg, TRUE, ji);
12140 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12141 ji->type = MONO_PATCH_INFO_JIT_THREAD_ATTACH;
12142 get_got_offset (acfg, FALSE, ji);
12143 get_got_offset (acfg, TRUE, ji);
12145 /* Called by native-to-managed wrappers on possibly unattached threads */
12146 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoJumpInfo));
12147 ji->type = MONO_PATCH_INFO_JIT_ICALL_ADDR_NOCALL;
12148 ji->data.name = "mono_threads_attach_coop";
12149 get_got_offset (acfg, FALSE, ji);
12150 get_got_offset (acfg, TRUE, ji);
12152 for (i = 0; i < sizeof (preinited_jit_icalls) / sizeof (char*); ++i) {
12153 ji = (MonoJumpInfo *)mono_mempool_alloc0 (acfg->mempool, sizeof (MonoAotCompile));
12154 ji->type = MONO_PATCH_INFO_INTERNAL_METHOD;
12155 ji->data.name = preinited_jit_icalls [i];
12156 get_got_offset (acfg, FALSE, ji);
12157 get_got_offset (acfg, TRUE, ji);
12160 acfg->nshared_got_entries = acfg->got_offset;
12163 static void
12164 mono_dedup_log_stats (MonoAotCompile *acfg)
12166 GHashTableIter iter;
12167 g_assert (acfg->dedup_stats);
12169 // If dedup_emit_mode, acfg is the dummy dedup module that consolidates
12170 // deduped modules
12171 g_hash_table_iter_init (&iter, acfg->method_to_cfg);
12172 MonoCompile *dcfg = NULL;
12173 MonoMethod *method = NULL;
12175 size_t wrappers_size_saved = 0;
12176 size_t inflated_size_saved = 0;
12177 size_t copied_singles = 0;
12179 while (g_hash_table_iter_next (&iter, (gpointer *) &method, (gpointer *)&dcfg)) {
12180 gchar *dedup_name = mono_aot_get_mangled_method_name (method);
12181 guint count = GPOINTER_TO_UINT(g_hash_table_lookup (acfg->dedup_stats, dedup_name));
12183 if (count == 0)
12184 continue;
12186 if (acfg->dedup_emit_mode) {
12187 // Size *saved* is the size due to things not emitted.
12188 if (count < 2) {
12189 // Just moved, didn't save space / dedup
12190 copied_singles += dcfg->code_len;
12191 } else if (method->wrapper_type != MONO_WRAPPER_NONE) {
12192 wrappers_size_saved += dcfg->code_len * (count - 1);
12193 } else {
12194 inflated_size_saved += dcfg->code_len * (count - 1);
12197 if (acfg->aot_opts.dedup) {
12198 if (method->wrapper_type != MONO_WRAPPER_NONE) {
12199 wrappers_size_saved += dcfg->code_len * count;
12200 } else {
12201 inflated_size_saved += dcfg->code_len * count;
12206 aot_printf (acfg, "Dedup Pass: Size Saved From Deduped Wrappers:\t%zu bytes\n", wrappers_size_saved);
12207 aot_printf (acfg, "Dedup Pass: Size Saved From Inflated Methods:\t%zu bytes\n", inflated_size_saved);
12208 if (acfg->dedup_emit_mode)
12209 aot_printf (acfg, "Dedup Pass: Size of Moved But Not Deduped (only 1 copy) Methods:\t%zu bytes\n", copied_singles);
12211 g_hash_table_destroy (acfg->dedup_stats);
12212 acfg->dedup_stats = NULL;
12215 // Flush the cache to tell future calls what to skip
12216 static void
12217 mono_flush_method_cache (MonoAotCompile *acfg)
12219 GHashTable *method_cache = acfg->dedup_cache;
12220 char *filename = g_strdup_printf ("%s.dedup", acfg->image->name);
12221 if (!acfg->dedup_cache_changed || !acfg->aot_opts.dedup) {
12222 g_free (filename);
12223 return;
12226 acfg->dedup_cache = NULL;
12228 FILE *cache = fopen (filename, "w");
12230 if (!cache)
12231 g_error ("Could not create cache at %s because of error: %s\n", filename, strerror (errno));
12233 GHashTableIter iter;
12234 gchar *name = NULL;
12235 g_hash_table_iter_init (&iter, method_cache);
12236 gboolean cont = TRUE;
12237 while (cont && g_hash_table_iter_next (&iter, (gpointer *) &name, NULL)) {
12238 int res = fprintf (cache, "%s\n", name);
12239 cont = res >= 0;
12241 // FIXME: don't assert if error when flushing
12242 g_assert (cont);
12244 fclose (cache);
12245 g_free (filename);
12247 // The keys are all in the imageset, nothing to free
12248 // Values are just pointers to memory owned elsewhere, or sentinels
12249 g_hash_table_destroy (method_cache);
12252 // Read in what has been emitted by previous invocations,
12253 // what can be skipped
12254 static void
12255 mono_read_method_cache (MonoAotCompile *acfg)
12257 char *filename = g_strdup_printf ("%s.dedup", acfg->image->name);
12258 // Only do once, when dedup_cache is null
12259 if (acfg->dedup_cache)
12260 goto early_exit;
12262 if (acfg->aot_opts.dedup_include || acfg->aot_opts.dedup)
12263 g_assert (acfg->dedup_stats);
12265 // only in skip mode
12266 if (!acfg->aot_opts.dedup)
12267 goto early_exit;
12269 g_assert (acfg->dedup_cache);
12271 FILE *cache = fopen (filename, "r");
12272 if (!cache)
12273 goto early_exit;
12275 // Since we do pointer comparisons, and it can't be allocated at
12276 // the address 0x1 due to alignment, we use this as a sentinel
12277 gpointer other_acfg_sentinel = GINT_TO_POINTER (0x1);
12279 if (fseek (cache, 0L, SEEK_END))
12280 goto cleanup;
12282 size_t fileLength = ftell (cache);
12283 g_assert (fileLength > 0);
12285 if (fseek (cache, 0L, SEEK_SET))
12286 goto cleanup;
12288 // Avoid thousands of new malloc entries
12289 // FIXME: allocate into imageset, so we don't need to free.
12290 // put the other mangled names there too.
12291 char *bulk = g_malloc0 (fileLength * sizeof (char));
12292 size_t offset = 0;
12294 while (fgets (&bulk [offset], fileLength - offset, cache)) {
12295 // strip newline
12296 char *line = &bulk [offset];
12297 size_t len = strlen (line);
12298 if (len == 0)
12299 break;
12301 if (len >= 0 && line [len] == '\n')
12302 line [len] = '\0';
12303 offset += strlen (line) + 1;
12304 g_assert (fileLength >= offset);
12306 g_hash_table_insert (acfg->dedup_cache, line, other_acfg_sentinel);
12309 cleanup:
12310 fclose (cache);
12312 early_exit:
12313 g_free (filename);
12314 return;
12317 typedef struct {
12318 GHashTable *cache;
12319 GHashTable *stats;
12320 gboolean emit_inflated_methods;
12321 MonoAssembly *inflated_assembly;
12322 } MonoAotState;
12324 static MonoAotState *
12325 alloc_aot_state (void)
12327 MonoAotState *state = g_malloc (sizeof (MonoAotState));
12328 // FIXME: Should this own the memory?
12329 state->cache = g_hash_table_new (g_str_hash, g_str_equal);
12330 state->stats = g_hash_table_new (g_str_hash, g_str_equal);
12331 // Start in "collect mode"
12332 state->emit_inflated_methods = FALSE;
12333 state->inflated_assembly = NULL;
12334 return state;
12337 static void
12338 free_aot_state (MonoAotState *astate)
12340 g_hash_table_destroy (astate->cache);
12341 g_free (astate);
12344 static void
12345 mono_add_deferred_extra_methods (MonoAotCompile *acfg, MonoAotState *astate)
12347 GHashTableIter iter;
12348 gchar *name = NULL;
12349 MonoMethod *method = NULL;
12351 acfg->dedup_emit_mode = TRUE;
12353 g_hash_table_iter_init (&iter, astate->cache);
12354 while (g_hash_table_iter_next (&iter, (gpointer *) &name, (gpointer *) &method)) {
12355 add_method_full (acfg, method, TRUE, 0);
12357 return;
12360 static void
12361 mono_setup_dedup_state (MonoAotCompile *acfg, MonoAotState **global_aot_state, MonoAssembly *ass, MonoAotState **astate, gboolean *is_dedup_dummy)
12363 if (!acfg->aot_opts.dedup_include && !acfg->aot_opts.dedup)
12364 return;
12366 if (global_aot_state && *global_aot_state && acfg->aot_opts.dedup_include) {
12367 // Thread the state through when making the inflate pass
12368 *astate = *global_aot_state;
12371 if (!*astate) {
12372 *astate = alloc_aot_state ();
12373 *global_aot_state = *astate;
12376 acfg->dedup_cache = (*astate)->cache;
12377 acfg->dedup_stats = (*astate)->stats;
12379 // fills out acfg->dedup_cache
12380 if (acfg->aot_opts.dedup)
12381 mono_read_method_cache (acfg);
12383 if (!(*astate)->inflated_assembly && acfg->aot_opts.dedup_include) {
12384 gchar **asm_path = g_strsplit (ass->image->name, G_DIR_SEPARATOR_S, 0);
12385 gchar *asm_file = NULL;
12387 // Get the last part of the path, the filename
12388 for (int i=0; asm_path [i] != NULL; i++)
12389 asm_file = asm_path [i];
12391 if (!strcmp (acfg->aot_opts.dedup_include, asm_file)) {
12392 // Save
12393 *is_dedup_dummy = TRUE;
12394 (*astate)->inflated_assembly = ass;
12396 g_strfreev (asm_path);
12397 } else if ((*astate)->inflated_assembly) {
12398 *is_dedup_dummy = (ass == (*astate)->inflated_assembly);
12402 int
12403 mono_compile_deferred_assemblies (guint32 opts, const char *aot_options, gpointer **aot_state)
12405 // create assembly, loop and add extra_methods
12406 // in add_generic_instances , rip out what's in that for loop
12407 // and apply that to this aot_state inside of mono_compile_assembly
12408 MonoAotState *astate;
12409 astate = *(MonoAotState **)aot_state;
12410 g_assert (astate);
12412 // FIXME: allow suffixes?
12413 if (!astate->inflated_assembly) {
12414 char *inflate = strstr (aot_options, "dedup-inflate");
12415 if (!inflate)
12416 return 0;
12417 else
12418 g_error ("Error: mono was not given an assembly with the provided inflate name\n");
12421 // Switch modes
12422 astate->emit_inflated_methods = TRUE;
12424 int res = mono_compile_assembly (astate->inflated_assembly, opts, aot_options, aot_state);
12426 *aot_state = NULL;
12427 free_aot_state (astate);
12429 return res;
12432 static const char* interp_in_static_sigs[] = {
12433 "bool ptr int32 ptr&",
12434 "bool ptr ptr&",
12435 "int32 int32 ptr&",
12436 "int32 int32 ptr ptr&",
12437 "int32 ptr int32 ptr",
12438 "int32 ptr int32 ptr&",
12439 "int32 ptr ptr&",
12440 "object object ptr ptr ptr",
12441 "object",
12442 "ptr int32 ptr&",
12443 "ptr ptr int32 ptr ptr ptr&",
12444 "ptr ptr int32 ptr ptr&",
12445 "ptr ptr int32 ptr&",
12446 "ptr ptr ptr int32 ptr&",
12447 "ptr ptr ptr ptr& ptr&",
12448 "ptr ptr ptr ptr ptr&",
12449 "ptr ptr ptr ptr&",
12450 "ptr ptr ptr&",
12451 "ptr ptr uint32 ptr&",
12452 "ptr uint32 ptr&",
12453 "void object ptr ptr ptr",
12454 "void ptr ptr int32 ptr ptr& ptr ptr&",
12455 "void ptr ptr int32 ptr ptr&",
12456 "void ptr ptr ptr&",
12457 "void ptr ptr&",
12458 "void ptr",
12459 "void int32 ptr&",
12460 "void uint32 ptr&",
12461 "void"
12465 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options, gpointer **global_aot_state)
12467 MonoImage *image = ass->image;
12468 int i, res;
12469 gint64 all_sizes;
12470 MonoAotCompile *acfg;
12471 char *outfile_name, *tmp_outfile_name, *p;
12472 char llvm_stats_msg [256];
12473 TV_DECLARE (atv);
12474 TV_DECLARE (btv);
12476 acfg = acfg_create (ass, opts);
12478 memset (&acfg->aot_opts, 0, sizeof (acfg->aot_opts));
12479 acfg->aot_opts.write_symbols = TRUE;
12480 acfg->aot_opts.ntrampolines = 4096;
12481 acfg->aot_opts.nrgctx_trampolines = 4096;
12482 acfg->aot_opts.nimt_trampolines = 512;
12483 acfg->aot_opts.nrgctx_fetch_trampolines = 128;
12484 acfg->aot_opts.ngsharedvt_arg_trampolines = 512;
12485 acfg->aot_opts.llvm_path = g_strdup ("");
12486 acfg->aot_opts.temp_path = g_strdup ("");
12487 #ifdef MONOTOUCH
12488 acfg->aot_opts.use_trampolines_page = TRUE;
12489 #endif
12491 mono_aot_parse_options (aot_options, &acfg->aot_opts);
12493 // start dedup
12494 MonoAotState *astate = NULL;
12495 gboolean is_dedup_dummy = FALSE;
12496 mono_setup_dedup_state (acfg, (MonoAotState **) global_aot_state, ass, &astate, &is_dedup_dummy);
12498 // Process later
12499 if (is_dedup_dummy && astate && !astate->emit_inflated_methods)
12500 return 0;
12502 // end dedup
12504 if (acfg->aot_opts.logfile) {
12505 acfg->logfile = fopen (acfg->aot_opts.logfile, "a+");
12508 if (acfg->aot_opts.data_outfile) {
12509 acfg->data_outfile = fopen (acfg->aot_opts.data_outfile, "w+");
12510 if (!acfg->data_outfile) {
12511 aot_printerrf (acfg, "Unable to create file '%s': %s\n", acfg->aot_opts.data_outfile, strerror (errno));
12512 return 1;
12514 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_SEPARATE_DATA);
12517 //acfg->aot_opts.print_skipped_methods = TRUE;
12519 #if !defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
12520 if (acfg->opts & MONO_OPT_GSHAREDVT) {
12521 aot_printerrf (acfg, "-O=gsharedvt not supported on this platform.\n");
12522 return 1;
12524 if (acfg->aot_opts.llvm_only) {
12525 aot_printerrf (acfg, "--aot=llvmonly requires a runtime that supports gsharedvt.\n");
12526 return 1;
12528 #else
12529 if (acfg->aot_opts.llvm_only || mono_aot_mode_is_full (&acfg->aot_opts) || mono_aot_mode_is_hybrid (&acfg->aot_opts))
12530 acfg->opts |= MONO_OPT_GSHAREDVT;
12531 #endif
12533 #if !defined(ENABLE_LLVM)
12534 if (acfg->aot_opts.llvm_only) {
12535 aot_printerrf (acfg, "--aot=llvmonly requires a runtime compiled with llvm support.\n");
12536 return 1;
12538 #endif
12540 if (acfg->opts & MONO_OPT_GSHAREDVT)
12541 mono_set_generic_sharing_vt_supported (TRUE);
12543 aot_printf (acfg, "Mono Ahead of Time compiler - compiling assembly %s\n", image->name);
12545 generate_aotid ((guint8*) &acfg->image->aotid);
12547 char *aotid = mono_guid_to_string (acfg->image->aotid);
12548 aot_printf (acfg, "AOTID %s\n", aotid);
12549 g_free (aotid);
12551 #ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
12552 if (mono_aot_mode_is_full (&acfg->aot_opts)) {
12553 aot_printerrf (acfg, "--aot=full is not supported on this platform.\n");
12554 return 1;
12556 #endif
12558 if (acfg->aot_opts.direct_pinvoke && !acfg->aot_opts.static_link) {
12559 aot_printerrf (acfg, "The 'direct-pinvoke' AOT option also requires the 'static' AOT option.\n");
12560 return 1;
12563 if (acfg->aot_opts.static_link)
12564 acfg->aot_opts.asm_writer = TRUE;
12566 if (acfg->aot_opts.soft_debug) {
12567 MonoDebugOptions *opt = mini_get_debug_options ();
12569 opt->mdb_optimizations = TRUE;
12570 opt->gen_sdb_seq_points = TRUE;
12572 if (!mono_debug_enabled ()) {
12573 aot_printerrf (acfg, "The soft-debug AOT option requires the --debug option.\n");
12574 return 1;
12576 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_DEBUG);
12579 if (acfg->aot_opts.try_llvm)
12580 acfg->aot_opts.llvm = mini_llvm_init ();
12582 if (mono_use_llvm || acfg->aot_opts.llvm) {
12583 acfg->llvm = TRUE;
12584 acfg->aot_opts.asm_writer = TRUE;
12585 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_WITH_LLVM);
12587 if (acfg->aot_opts.soft_debug) {
12588 aot_printerrf (acfg, "The 'soft-debug' option is not supported when compiling with LLVM.\n");
12589 return 1;
12592 mini_llvm_init ();
12594 if (acfg->aot_opts.asm_only && !acfg->aot_opts.llvm_outfile) {
12595 aot_printerrf (acfg, "Compiling with LLVM and the asm-only option requires the llvm-outfile= option.\n");
12596 return 1;
12600 if (mono_aot_mode_is_full (&acfg->aot_opts)) {
12601 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_FULL_AOT);
12602 acfg->is_full_aot = TRUE;
12605 if (mono_threads_are_safepoints_enabled ())
12606 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_SAFEPOINTS);
12608 // The methods in dedup-emit amodules must be available on runtime startup
12609 // Note: Only one such amodule can have this attribute
12610 if (astate && astate->emit_inflated_methods)
12611 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_EAGER_LOAD);
12614 if (acfg->aot_opts.instances_logfile_path) {
12615 acfg->instances_logfile = fopen (acfg->aot_opts.instances_logfile_path, "w");
12616 if (!acfg->instances_logfile) {
12617 aot_printerrf (acfg, "Unable to create logfile: '%s'.\n", acfg->aot_opts.instances_logfile_path);
12618 return 1;
12622 if (acfg->aot_opts.profile_files) {
12623 GList *l;
12625 for (l = acfg->aot_opts.profile_files; l; l = l->next) {
12626 load_profile_file (acfg, (char*)l->data);
12630 if (!(acfg->aot_opts.interp && !mono_aot_mode_is_full (&acfg->aot_opts))) {
12631 for (int method_index = 0; method_index < acfg->image->tables [MONO_TABLE_METHOD].rows; ++method_index)
12632 g_ptr_array_add (acfg->method_order,GUINT_TO_POINTER (method_index));
12635 acfg->num_trampolines [MONO_AOT_TRAMP_SPECIFIC] = mono_aot_mode_is_full (&acfg->aot_opts) ? acfg->aot_opts.ntrampolines : 0;
12636 #ifdef MONO_ARCH_GSHARED_SUPPORTED
12637 acfg->num_trampolines [MONO_AOT_TRAMP_STATIC_RGCTX] = mono_aot_mode_is_full (&acfg->aot_opts) ? acfg->aot_opts.nrgctx_trampolines : 0;
12638 #endif
12639 acfg->num_trampolines [MONO_AOT_TRAMP_IMT] = mono_aot_mode_is_full (&acfg->aot_opts) ? acfg->aot_opts.nimt_trampolines : 0;
12640 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
12641 if (acfg->opts & MONO_OPT_GSHAREDVT)
12642 acfg->num_trampolines [MONO_AOT_TRAMP_GSHAREDVT_ARG] = mono_aot_mode_is_full (&acfg->aot_opts) ? acfg->aot_opts.ngsharedvt_arg_trampolines : 0;
12643 #endif
12645 acfg->temp_prefix = mono_img_writer_get_temp_label_prefix (NULL);
12647 arch_init (acfg);
12649 if (mono_use_llvm || acfg->aot_opts.llvm) {
12651 * Emit all LLVM code into a separate assembly/object file and link with it
12652 * normally.
12654 if (!acfg->aot_opts.asm_only && acfg->llvm_owriter_supported) {
12655 acfg->llvm_owriter = TRUE;
12656 } else if (acfg->aot_opts.llvm_outfile) {
12657 int len = strlen (acfg->aot_opts.llvm_outfile);
12659 if (len >= 2 && acfg->aot_opts.llvm_outfile [len - 2] == '.' && acfg->aot_opts.llvm_outfile [len - 1] == 'o')
12660 acfg->llvm_owriter = TRUE;
12664 if (acfg->llvm && acfg->thumb_mixed)
12665 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_LLVM_THUMB);
12666 if (acfg->aot_opts.llvm_only)
12667 acfg->flags = (MonoAotFileFlags)(acfg->flags | MONO_AOT_FILE_FLAG_LLVM_ONLY);
12669 acfg->assembly_name_sym = g_strdup (acfg->image->assembly->aname.name);
12670 /* Get rid of characters which cannot occur in symbols */
12671 for (p = acfg->assembly_name_sym; *p; ++p) {
12672 if (!(isalnum (*p) || *p == '_'))
12673 *p = '_';
12676 acfg->global_prefix = g_strdup_printf ("mono_aot_%s", acfg->assembly_name_sym);
12677 acfg->plt_symbol = g_strdup_printf ("%s_plt", acfg->global_prefix);
12678 acfg->got_symbol = g_strdup_printf ("%s_got", acfg->global_prefix);
12679 if (acfg->llvm) {
12680 acfg->llvm_got_symbol = g_strdup_printf ("%s_llvm_got", acfg->global_prefix);
12681 acfg->llvm_eh_frame_symbol = g_strdup_printf ("%s_eh_frame", acfg->global_prefix);
12684 acfg->method_index = 1;
12686 if (mono_aot_mode_is_full (&acfg->aot_opts) || mono_aot_mode_is_hybrid (&acfg->aot_opts))
12687 mono_set_partial_sharing_supported (TRUE);
12689 if (!(acfg->aot_opts.interp && !mono_aot_mode_is_full (&acfg->aot_opts))) {
12690 res = collect_methods (acfg);
12691 if (!res)
12692 return 1;
12695 // If we're emitting all of the inflated methods into a dummy
12696 // Assembly, then after extra_methods is set up, we're done
12697 // in this function.
12698 if (astate && astate->emit_inflated_methods)
12699 mono_add_deferred_extra_methods (acfg, astate);
12702 GList *l;
12704 for (l = acfg->profile_data; l; l = l->next)
12705 resolve_profile_data (acfg, (ProfileData*)l->data);
12706 for (l = acfg->profile_data; l; l = l->next)
12707 add_profile_instances (acfg, (ProfileData*)l->data);
12710 acfg->cfgs_size = acfg->methods->len + 32;
12711 acfg->cfgs = g_new0 (MonoCompile*, acfg->cfgs_size);
12713 /* PLT offset 0 is reserved for the PLT trampoline */
12714 acfg->plt_offset = 1;
12715 add_preinit_got_slots (acfg);
12717 #ifdef ENABLE_LLVM
12718 if (acfg->llvm) {
12719 llvm_acfg = acfg;
12720 mono_llvm_create_aot_module (acfg->image->assembly, acfg->global_prefix, acfg->nshared_got_entries, TRUE, acfg->aot_opts.static_link, acfg->aot_opts.llvm_only);
12722 #endif
12724 if (mono_aot_mode_is_interp (&acfg->aot_opts)) {
12725 MonoMethod *wrapper = mini_get_interp_lmf_wrapper ();
12726 add_method (acfg, wrapper);
12728 for (int i = 0; i < sizeof (interp_in_static_sigs) / sizeof (const char *); i++) {
12729 MonoMethodSignature *sig = mono_create_icall_signature (interp_in_static_sigs [i]);
12730 sig = mono_metadata_signature_dup_full (mono_get_corlib (), sig);
12731 sig->pinvoke = FALSE;
12732 wrapper = mini_get_interp_in_wrapper (sig);
12733 add_method (acfg, wrapper);
12737 TV_GETTIME (atv);
12739 compile_methods (acfg);
12741 TV_GETTIME (btv);
12743 acfg->stats.jit_time = TV_ELAPSED (atv, btv);
12745 TV_GETTIME (atv);
12747 #ifdef ENABLE_LLVM
12748 if (acfg->llvm) {
12749 if (acfg->aot_opts.asm_only) {
12750 if (acfg->aot_opts.outfile) {
12751 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
12752 acfg->tmpbasename = g_strdup (acfg->tmpfname);
12753 } else {
12754 acfg->tmpbasename = g_strdup_printf ("%s", acfg->image->name);
12755 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->tmpbasename);
12757 g_assert (acfg->aot_opts.llvm_outfile);
12758 acfg->llvm_sfile = g_strdup (acfg->aot_opts.llvm_outfile);
12759 if (acfg->llvm_owriter)
12760 acfg->llvm_ofile = g_strdup (acfg->aot_opts.llvm_outfile);
12761 else
12762 acfg->llvm_sfile = g_strdup (acfg->aot_opts.llvm_outfile);
12763 } else {
12764 gchar *temp_path;
12765 if (strcmp (acfg->aot_opts.temp_path, "") != 0) {
12766 temp_path = g_strdup (acfg->aot_opts.temp_path);
12767 } else {
12768 temp_path = mkdtemp(g_strdup ("mono_aot_XXXXXX"));
12769 g_assertf (temp_path, "mkdtemp failed, error = (%d) %s", errno, g_strerror (errno));
12772 acfg->tmpbasename = g_build_filename (temp_path, "temp", NULL);
12773 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->tmpbasename);
12774 acfg->llvm_sfile = g_strdup_printf ("%s-llvm.s", acfg->tmpbasename);
12775 acfg->llvm_ofile = g_strdup_printf ("%s-llvm.o", acfg->tmpbasename);
12777 g_free (temp_path);
12780 #endif
12782 if (acfg->aot_opts.asm_only && !acfg->aot_opts.llvm_only) {
12783 if (acfg->aot_opts.outfile)
12784 acfg->tmpfname = g_strdup_printf ("%s", acfg->aot_opts.outfile);
12785 else
12786 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->image->name);
12787 acfg->fp = fopen (acfg->tmpfname, "w+");
12788 } else {
12789 if (strcmp (acfg->aot_opts.temp_path, "") == 0) {
12790 int i = g_file_open_tmp ("mono_aot_XXXXXX", &acfg->tmpfname, NULL);
12791 acfg->fp = fdopen (i, "w+");
12792 } else {
12793 acfg->tmpbasename = g_build_filename (acfg->aot_opts.temp_path, "temp", NULL);
12794 acfg->tmpfname = g_strdup_printf ("%s.s", acfg->tmpbasename);
12795 acfg->fp = fopen (acfg->tmpfname, "w+");
12798 if (acfg->fp == 0 && !acfg->aot_opts.llvm_only) {
12799 aot_printerrf (acfg, "Unable to open file '%s': %s\n", acfg->tmpfname, strerror (errno));
12800 return 1;
12802 if (acfg->fp)
12803 acfg->w = mono_img_writer_create (acfg->fp, FALSE);
12805 tmp_outfile_name = NULL;
12806 outfile_name = NULL;
12808 /* Compute symbols for methods */
12809 for (i = 0; i < acfg->nmethods; ++i) {
12810 if (acfg->cfgs [i]) {
12811 MonoCompile *cfg = acfg->cfgs [i];
12812 int method_index = get_method_index (acfg, cfg->orig_method);
12814 if (COMPILE_LLVM (cfg))
12815 cfg->asm_symbol = g_strdup_printf ("%s%s", acfg->llvm_label_prefix, cfg->llvm_method_name);
12816 else if (acfg->global_symbols || acfg->llvm)
12817 cfg->asm_symbol = get_debug_sym (cfg->orig_method, "", acfg->method_label_hash);
12818 else
12819 cfg->asm_symbol = g_strdup_printf ("%s%sm_%x", acfg->temp_prefix, acfg->llvm_label_prefix, method_index);
12820 cfg->asm_debug_symbol = cfg->asm_symbol;
12824 if (acfg->aot_opts.dwarf_debug && acfg->aot_opts.gnu_asm) {
12826 * CLANG supports GAS .file/.loc directives, so emit line number information this way
12828 acfg->gas_line_numbers = TRUE;
12831 #ifdef EMIT_DWARF_INFO
12832 if ((!acfg->aot_opts.nodebug || acfg->aot_opts.dwarf_debug) && acfg->has_jitted_code) {
12833 if (acfg->aot_opts.dwarf_debug && !mono_debug_enabled ()) {
12834 aot_printerrf (acfg, "The dwarf AOT option requires the --debug option.\n");
12835 return 1;
12837 acfg->dwarf = mono_dwarf_writer_create (acfg->w, NULL, 0, !acfg->gas_line_numbers);
12839 #endif /* EMIT_DWARF_INFO */
12841 if (acfg->w)
12842 mono_img_writer_emit_start (acfg->w);
12844 if (acfg->dwarf)
12845 mono_dwarf_writer_emit_base_info (acfg->dwarf, g_path_get_basename (acfg->image->name), mono_unwind_get_cie_program ());
12847 emit_code (acfg);
12848 if (acfg->aot_opts.dedup)
12849 mono_flush_method_cache (acfg);
12850 if (acfg->aot_opts.dedup || acfg->dedup_emit_mode)
12851 mono_dedup_log_stats (acfg);
12853 emit_info (acfg);
12855 emit_extra_methods (acfg);
12857 if (acfg->aot_opts.dedup_include && !is_dedup_dummy) {
12858 fclose (acfg->fp);
12859 return 0;
12862 emit_trampolines (acfg);
12864 emit_class_name_table (acfg);
12866 emit_got_info (acfg, FALSE);
12867 if (acfg->llvm)
12868 emit_got_info (acfg, TRUE);
12870 emit_exception_info (acfg);
12872 emit_unwind_info (acfg);
12874 emit_class_info (acfg);
12876 emit_plt (acfg);
12878 emit_image_table (acfg);
12880 emit_weak_field_indexes (acfg);
12882 emit_got (acfg);
12886 * The managed allocators are GC specific, so can't use an AOT image created by one GC
12887 * in another.
12889 const char *gc_name = mono_gc_get_gc_name ();
12890 acfg->gc_name_offset = add_to_blob (acfg, (guint8*)gc_name, strlen (gc_name) + 1);
12893 emit_blob (acfg);
12895 emit_objc_selectors (acfg);
12897 emit_globals (acfg);
12899 emit_file_info (acfg);
12901 emit_library_info (acfg);
12903 if (acfg->dwarf) {
12904 emit_dwarf_info (acfg);
12905 mono_dwarf_writer_close (acfg->dwarf);
12906 } else {
12907 if (!acfg->aot_opts.nodebug)
12908 emit_codeview_info (acfg);
12911 emit_mem_end (acfg);
12913 if (acfg->need_pt_gnu_stack) {
12914 /* This is required so the .so doesn't have an executable stack */
12915 /* The bin writer already emits this */
12916 fprintf (acfg->fp, "\n.section .note.GNU-stack,\"\",@progbits\n");
12919 if (acfg->aot_opts.data_outfile)
12920 fclose (acfg->data_outfile);
12922 #ifdef ENABLE_LLVM
12923 if (acfg->llvm) {
12924 gboolean res;
12926 res = emit_llvm_file (acfg);
12927 if (!res)
12928 return 1;
12930 #endif
12932 TV_GETTIME (btv);
12934 acfg->stats.gen_time = TV_ELAPSED (atv, btv);
12936 if (acfg->llvm)
12937 sprintf (llvm_stats_msg, ", LLVM: %d (%d%%)", acfg->stats.llvm_count, acfg->stats.mcount ? (acfg->stats.llvm_count * 100) / acfg->stats.mcount : 100);
12938 else
12939 strcpy (llvm_stats_msg, "");
12941 all_sizes = acfg->stats.code_size + acfg->stats.info_size + acfg->stats.ex_info_size + acfg->stats.unwind_info_size + acfg->stats.class_info_size + acfg->stats.got_info_size + acfg->stats.offsets_size + acfg->stats.plt_size;
12943 aot_printf (acfg, "Code: %d(%d%%) Info: %d(%d%%) Ex Info: %d(%d%%) Unwind Info: %d(%d%%) Class Info: %d(%d%%) PLT: %d(%d%%) GOT Info: %d(%d%%) Offsets: %d(%d%%) GOT: %d\n",
12944 (int)acfg->stats.code_size, (int)(acfg->stats.code_size * 100 / all_sizes),
12945 (int)acfg->stats.info_size, (int)(acfg->stats.info_size * 100 / all_sizes),
12946 (int)acfg->stats.ex_info_size, (int)(acfg->stats.ex_info_size * 100 / all_sizes),
12947 (int)acfg->stats.unwind_info_size, (int)(acfg->stats.unwind_info_size * 100 / all_sizes),
12948 (int)acfg->stats.class_info_size, (int)(acfg->stats.class_info_size * 100 / all_sizes),
12949 acfg->stats.plt_size ? (int)acfg->stats.plt_size : (int)acfg->plt_offset, acfg->stats.plt_size ? (int)(acfg->stats.plt_size * 100 / all_sizes) : 0,
12950 (int)acfg->stats.got_info_size, (int)(acfg->stats.got_info_size * 100 / all_sizes),
12951 (int)acfg->stats.offsets_size, (int)(acfg->stats.offsets_size * 100 / all_sizes),
12952 (int)(acfg->got_offset * sizeof (gpointer)));
12953 aot_printf (acfg, "Compiled: %d/%d (%d%%)%s, No GOT slots: %d (%d%%), Direct calls: %d (%d%%)\n",
12954 acfg->stats.ccount, acfg->stats.mcount, acfg->stats.mcount ? (acfg->stats.ccount * 100) / acfg->stats.mcount : 100,
12955 llvm_stats_msg,
12956 acfg->stats.methods_without_got_slots, acfg->stats.mcount ? (acfg->stats.methods_without_got_slots * 100) / acfg->stats.mcount : 100,
12957 acfg->stats.direct_calls, acfg->stats.all_calls ? (acfg->stats.direct_calls * 100) / acfg->stats.all_calls : 100);
12958 if (acfg->stats.genericcount)
12959 aot_printf (acfg, "%d methods are generic (%d%%)\n", acfg->stats.genericcount, acfg->stats.mcount ? (acfg->stats.genericcount * 100) / acfg->stats.mcount : 100);
12960 if (acfg->stats.abscount)
12961 aot_printf (acfg, "%d methods contain absolute addresses (%d%%)\n", acfg->stats.abscount, acfg->stats.mcount ? (acfg->stats.abscount * 100) / acfg->stats.mcount : 100);
12962 if (acfg->stats.lmfcount)
12963 aot_printf (acfg, "%d methods contain lmf pointers (%d%%)\n", acfg->stats.lmfcount, acfg->stats.mcount ? (acfg->stats.lmfcount * 100) / acfg->stats.mcount : 100);
12964 if (acfg->stats.ocount)
12965 aot_printf (acfg, "%d methods have other problems (%d%%)\n", acfg->stats.ocount, acfg->stats.mcount ? (acfg->stats.ocount * 100) / acfg->stats.mcount : 100);
12967 TV_GETTIME (atv);
12968 if (acfg->w) {
12969 res = mono_img_writer_emit_writeout (acfg->w);
12970 if (res != 0) {
12971 acfg_free (acfg);
12972 return res;
12974 res = compile_asm (acfg);
12975 if (res != 0) {
12976 acfg_free (acfg);
12977 return res;
12980 TV_GETTIME (btv);
12981 acfg->stats.link_time = TV_ELAPSED (atv, btv);
12983 if (acfg->aot_opts.stats) {
12984 int i;
12986 aot_printf (acfg, "GOT slot distribution:\n");
12987 for (i = 0; i < MONO_PATCH_INFO_NUM; ++i)
12988 if (acfg->stats.got_slot_types [i])
12989 aot_printf (acfg, "\t%s: %d (%d)\n", get_patch_name (i), acfg->stats.got_slot_types [i], acfg->stats.got_slot_info_sizes [i]);
12990 aot_printf (acfg, "\nMethod stats:\n");
12991 aot_printf (acfg, "\tNormal: %d\n", acfg->stats.method_categories [METHOD_CAT_NORMAL]);
12992 aot_printf (acfg, "\tInstance: %d\n", acfg->stats.method_categories [METHOD_CAT_INST]);
12993 aot_printf (acfg, "\tGSharedvt: %d\n", acfg->stats.method_categories [METHOD_CAT_GSHAREDVT]);
12994 aot_printf (acfg, "\tWrapper: %d\n", acfg->stats.method_categories [METHOD_CAT_WRAPPER]);
12997 aot_printf (acfg, "JIT time: %d ms, Generation time: %d ms, Assembly+Link time: %d ms.\n", acfg->stats.jit_time / 1000, acfg->stats.gen_time / 1000, acfg->stats.link_time / 1000);
12999 if (acfg->aot_opts.dump_json)
13000 aot_dump (acfg);
13002 acfg_free (acfg);
13004 return 0;
13007 #else
13009 /* AOT disabled */
13011 void*
13012 mono_aot_readonly_field_override (MonoClassField *field)
13014 return NULL;
13018 mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options, gpointer **aot_state)
13020 return 0;
13023 gboolean
13024 mono_aot_is_shared_got_offset (int offset)
13026 return FALSE;
13030 mono_compile_deferred_assemblies (guint32 opts, const char *aot_options, gpointer **aot_state)
13032 g_assert_not_reached ();
13033 return 0;
13036 #endif