2 * aot-compiler.c: mono Ahead of Time compiler
5 * Dietmar Maurer (dietmar@ximian.com)
6 * Zoltan Varga (vargaz@gmail.com)
7 * Johan Lorensson (lateralusx.github@gmail.com)
9 * (C) 2002 Ximian, Inc.
10 * Copyright 2003-2011 Novell, Inc
11 * Copyright 2011 Xamarin Inc (http://www.xamarin.com)
12 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
16 #include <sys/types.h>
36 #include <mono/metadata/abi-details.h>
37 #include <mono/metadata/tabledefs.h>
38 #include <mono/metadata/class.h>
39 #include <mono/metadata/object.h>
40 #include <mono/metadata/tokentype.h>
41 #include <mono/metadata/appdomain.h>
42 #include <mono/metadata/debug-helpers.h>
43 #include <mono/metadata/assembly.h>
44 #include <mono/metadata/metadata-internals.h>
45 #include <mono/metadata/reflection-internals.h>
46 #include <mono/metadata/marshal.h>
47 #include <mono/metadata/gc-internals.h>
48 #include <mono/metadata/mempool-internals.h>
49 #include <mono/metadata/mono-endian.h>
50 #include <mono/metadata/threads-types.h>
51 #include <mono/utils/mono-logger-internals.h>
52 #include <mono/utils/mono-compiler.h>
53 #include <mono/utils/mono-time.h>
54 #include <mono/utils/mono-mmap.h>
55 #include <mono/utils/mono-rand.h>
56 #include <mono/utils/json.h>
57 #include <mono/utils/mono-threads-coop.h>
59 #include "aot-compiler.h"
60 #include "seq-points.h"
61 #include "image-writer.h"
62 #include "dwarfwriter.h"
64 #include "mini-llvm.h"
66 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
68 // Use MSVC toolchain, Clang for MSVC using MSVC codegen and linker, when compiling for AMD64
69 // targeting WIN32 platforms running AOT compiler on WIN32 platform with VS installation.
70 #if defined(TARGET_AMD64) && defined(TARGET_WIN32) && defined(HOST_WIN32) && defined(_MSC_VER)
71 #define TARGET_X86_64_WIN32_MSVC
74 #if defined(TARGET_X86_64_WIN32_MSVC)
75 #define TARGET_WIN32_MSVC
78 #if defined(__linux__) || defined(__native_client_codegen__)
79 #define RODATA_SECT ".rodata"
80 #elif defined(TARGET_MACH)
81 #define RODATA_SECT ".section __TEXT, __const"
82 #elif defined(TARGET_WIN32_MSVC)
83 #define RODATA_SECT ".rdata"
85 #define RODATA_SECT ".text"
88 #define TV_DECLARE(name) gint64 name
89 #define TV_GETTIME(tv) tv = mono_100ns_ticks ()
90 #define TV_ELAPSED(start,end) (((end) - (start)) / 10)
92 #define ALIGN_TO(val,align) ((((guint64)val) + ((align) - 1)) & ~((align) - 1))
93 #define ALIGN_PTR_TO(ptr,align) (gpointer)((((gssize)(ptr)) + (align - 1)) & (~(align - 1)))
94 #define ROUND_DOWN(VALUE,SIZE) ((VALUE) & ~((SIZE) - 1))
96 /* predefined values for static readonly fields without needed to run the .cctor */
97 typedef struct _ReadOnlyValue ReadOnlyValue
;
98 struct _ReadOnlyValue
{
101 int type
; /* to be used later for typechecking to prevent user errors */
110 static ReadOnlyValue
*readonly_values
;
112 typedef struct MonoAotOptions
{
117 gboolean write_symbols
;
118 gboolean metadata_only
;
119 gboolean bind_to_runtime_version
;
122 gboolean static_link
;
126 gboolean dwarf_debug
;
128 gboolean log_generics
;
129 gboolean log_instances
;
130 gboolean gen_msym_dir
;
131 char *gen_msym_dir_path
;
132 gboolean direct_pinvoke
;
133 gboolean direct_icalls
;
134 gboolean no_direct_calls
;
135 gboolean use_trampolines_page
;
136 gboolean no_instances
;
142 int nrgctx_trampolines
;
143 int nimt_trampolines
;
144 int ngsharedvt_arg_trampolines
;
145 int nrgctx_fetch_trampolines
;
146 gboolean print_skipped_methods
;
153 char *instances_logfile_path
;
160 METHOD_CAT_GSHAREDVT
,
166 typedef struct MonoAotStats
{
167 int ccount
, mcount
, lmfcount
, abscount
, gcount
, ocount
, genericcount
;
168 gint64 code_size
, info_size
, ex_info_size
, unwind_info_size
, got_size
, class_info_size
, got_info_size
, plt_size
;
169 int methods_without_got_slots
, direct_calls
, all_calls
, llvm_count
;
170 int got_slots
, offsets_size
;
171 int method_categories
[METHOD_CAT_NUM
];
172 int got_slot_types
[MONO_PATCH_INFO_NUM
];
173 int got_slot_info_sizes
[MONO_PATCH_INFO_NUM
];
174 int jit_time
, gen_time
, link_time
;
177 typedef struct GotInfo
{
178 GHashTable
*patch_to_got_offset
;
179 GHashTable
**patch_to_got_offset_by_type
;
180 GPtrArray
*got_patches
;
183 typedef struct MonoAotCompile
{
186 GHashTable
*method_indexes
;
187 GHashTable
*method_depth
;
190 GHashTable
**patch_to_plt_entry
;
191 GHashTable
*plt_offset_to_entry
;
192 //GHashTable *patch_to_got_offset;
193 //GHashTable **patch_to_got_offset_by_type;
194 //GPtrArray *got_patches;
195 GotInfo got_info
, llvm_got_info
;
196 GHashTable
*image_hash
;
197 GHashTable
*method_to_cfg
;
198 GHashTable
*token_info_hash
;
199 GHashTable
*method_to_pinvoke_import
;
200 GPtrArray
*extra_methods
;
201 GPtrArray
*image_table
;
203 GPtrArray
*method_order
;
204 GHashTable
*export_names
;
205 /* Maps MonoClass* -> blob offset */
206 GHashTable
*klass_blob_hash
;
207 /* Maps MonoMethod* -> blob offset */
208 GHashTable
*method_blob_hash
;
209 GHashTable
*gsharedvt_in_signatures
;
210 GHashTable
*gsharedvt_out_signatures
;
211 guint32
*plt_got_info_offsets
;
212 guint32 got_offset
, llvm_got_offset
, plt_offset
, plt_got_offset_base
, nshared_got_entries
;
213 /* Number of GOT entries reserved for trampolines */
214 guint32 num_trampoline_got_entries
;
215 guint32 tramp_page_size
;
217 guint32 table_offsets
[MONO_AOT_TABLE_NUM
];
218 guint32 num_trampolines
[MONO_AOT_TRAMP_NUM
];
219 guint32 trampoline_got_offset_base
[MONO_AOT_TRAMP_NUM
];
220 guint32 trampoline_size
[MONO_AOT_TRAMP_NUM
];
221 guint32 tramp_page_code_offsets
[MONO_AOT_TRAMP_NUM
];
223 MonoAotOptions aot_opts
;
227 MonoMemPool
*mempool
;
230 char *static_linking_symbol
;
232 gboolean gas_line_numbers
;
233 /* Whenever to emit an object file directly from llc */
234 gboolean llvm_owriter
;
235 gboolean llvm_owriter_supported
;
237 MonoDwarfWriter
*dwarf
;
244 GHashTable
*unwind_info_offsets
;
245 GPtrArray
*unwind_ops
;
246 guint32 unwind_info_offset
;
249 char *llvm_got_symbol
;
251 char *llvm_eh_frame_symbol
;
252 GHashTable
*method_label_hash
;
253 const char *temp_prefix
;
254 const char *user_symbol_prefix
;
255 const char *llvm_label_prefix
;
256 const char *inst_directive
;
258 guint32 label_generator
;
260 gboolean has_jitted_code
;
261 MonoAotFileFlags flags
;
262 MonoDynamicStream blob
;
263 gboolean blob_closed
;
264 GHashTable
*typespec_classes
;
267 char *assembly_name_sym
;
268 GHashTable
*plt_entry_debug_sym_cache
;
269 gboolean thumb_mixed
, need_no_dead_strip
, need_pt_gnu_stack
;
270 GHashTable
*ginst_hash
;
271 GHashTable
*dwarf_ln_filenames
;
272 gboolean global_symbols
;
273 int objc_selector_index
, objc_selector_index_2
;
274 GPtrArray
*objc_selectors
;
275 GHashTable
*objc_selector_to_index
;
277 FILE *instances_logfile
;
285 char *symbol
, *llvm_symbol
, *debug_sym
;
287 gboolean jit_used
, llvm_used
;
290 #define mono_acfg_lock(acfg) mono_os_mutex_lock (&((acfg)->mutex))
291 #define mono_acfg_unlock(acfg) mono_os_mutex_unlock (&((acfg)->mutex))
293 /* This points to the current acfg in LLVM mode */
294 static MonoAotCompile
*llvm_acfg
;
296 #ifdef HAVE_ARRAY_ELEM_INIT
297 #define MSGSTRFIELD(line) MSGSTRFIELD1(line)
298 #define MSGSTRFIELD1(line) str##line
299 static const struct msgstr_t
{
300 #define PATCH_INFO(a,b) char MSGSTRFIELD(__LINE__) [sizeof (b)];
301 #include "patch-info.h"
304 #define PATCH_INFO(a,b) b,
305 #include "patch-info.h"
308 static const gint16 opidx
[] = {
309 #define PATCH_INFO(a,b) [MONO_PATCH_INFO_ ## a] = offsetof (struct msgstr_t, MSGSTRFIELD(__LINE__)),
310 #include "patch-info.h"
314 static G_GNUC_UNUSED
const char*
315 get_patch_name (int info
)
317 return (const char*)&opstr
+ opidx
[info
];
321 #define PATCH_INFO(a,b) b,
322 static const char* const
323 patch_types
[MONO_PATCH_INFO_NUM
+ 1] = {
324 #include "patch-info.h"
328 static G_GNUC_UNUSED
const char*
329 get_patch_name (int info
)
331 return patch_types
[info
];
337 get_unwind_info_offset (MonoAotCompile
*acfg
, guint8
*encoded
, guint32 encoded_len
);
340 get_plt_entry_debug_sym (MonoAotCompile
*acfg
, MonoJumpInfo
*ji
, GHashTable
*cache
);
343 add_gsharedvt_wrappers (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
, gboolean gsharedvt_in
, gboolean gsharedvt_out
);
346 aot_printf (MonoAotCompile
*acfg
, const gchar
*format
, ...)
352 output
= acfg
->logfile
;
356 va_start (args
, format
);
357 vfprintf (output
, format
, args
);
362 aot_printerrf (MonoAotCompile
*acfg
, const gchar
*format
, ...)
368 output
= acfg
->logfile
;
372 va_start (args
, format
);
373 vfprintf (output
, format
, args
);
378 report_loader_error (MonoAotCompile
*acfg
, MonoError
*error
, const char *format
, ...)
383 if (mono_error_ok (error
))
387 output
= acfg
->logfile
;
391 va_start (args
, format
);
392 vfprintf (output
, format
, args
);
394 mono_error_cleanup (error
);
396 g_error ("FullAOT cannot continue if there are loader errors");
399 /* Wrappers around the image writer functions */
401 #define MAX_SYMBOL_SIZE 256
403 static inline const char *
404 mangle_symbol (const char * symbol
, char * mangled_symbol
, gsize length
)
406 gsize needed_size
= length
;
408 g_assert (NULL
!= symbol
);
409 g_assert (NULL
!= mangled_symbol
);
410 g_assert (0 != length
);
412 #if defined(TARGET_WIN32) && defined(TARGET_X86)
413 if (symbol
&& '_' != symbol
[0]) {
414 needed_size
= g_snprintf (mangled_symbol
, length
, "_%s", symbol
);
416 needed_size
= g_snprintf (mangled_symbol
, length
, "%s", symbol
);
419 needed_size
= g_snprintf (mangled_symbol
, length
, "%s", symbol
);
422 g_assert (0 <= needed_size
&& needed_size
< length
);
423 return mangled_symbol
;
427 mangle_symbol_alloc (const char * symbol
)
429 g_assert (NULL
!= symbol
);
431 #if defined(TARGET_WIN32) && defined(TARGET_X86)
432 if (symbol
&& '_' != symbol
[0]) {
433 return g_strdup_printf ("_%s", symbol
);
436 return g_strdup_printf ("%s", symbol
);
439 return g_strdup_printf ("%s", symbol
);
444 emit_section_change (MonoAotCompile
*acfg
, const char *section_name
, int subsection_index
)
446 mono_img_writer_emit_section_change (acfg
->w
, section_name
, subsection_index
);
449 #if defined(TARGET_WIN32) && defined(TARGET_X86)
452 emit_local_symbol (MonoAotCompile
*acfg
, const char *name
, const char *end_label
, gboolean func
)
454 const char * mangled_symbol_name
= name
;
455 char * mangled_symbol_name_alloc
= NULL
;
458 mangled_symbol_name_alloc
= mangle_symbol_alloc (name
);
459 mangled_symbol_name
= mangled_symbol_name_alloc
;
462 if (name
!= mangled_symbol_name
&& 0 != g_strcasecmp (name
, mangled_symbol_name
)) {
463 mono_img_writer_emit_label (acfg
->w
, mangled_symbol_name
);
465 mono_img_writer_emit_local_symbol (acfg
->w
, mangled_symbol_name
, end_label
, func
);
467 if (NULL
!= mangled_symbol_name_alloc
) {
468 g_free (mangled_symbol_name_alloc
);
475 emit_local_symbol (MonoAotCompile
*acfg
, const char *name
, const char *end_label
, gboolean func
)
477 mono_img_writer_emit_local_symbol (acfg
->w
, name
, end_label
, func
);
483 emit_label (MonoAotCompile
*acfg
, const char *name
)
485 mono_img_writer_emit_label (acfg
->w
, name
);
489 emit_bytes (MonoAotCompile
*acfg
, const guint8
* buf
, int size
)
491 mono_img_writer_emit_bytes (acfg
->w
, buf
, size
);
495 emit_string (MonoAotCompile
*acfg
, const char *value
)
497 mono_img_writer_emit_string (acfg
->w
, value
);
501 emit_line (MonoAotCompile
*acfg
)
503 mono_img_writer_emit_line (acfg
->w
);
507 emit_alignment (MonoAotCompile
*acfg
, int size
)
509 mono_img_writer_emit_alignment (acfg
->w
, size
);
513 emit_alignment_code (MonoAotCompile
*acfg
, int size
)
515 if (acfg
->align_pad_value
)
516 mono_img_writer_emit_alignment_fill (acfg
->w
, size
, acfg
->align_pad_value
);
518 mono_img_writer_emit_alignment (acfg
->w
, size
);
522 emit_padding (MonoAotCompile
*acfg
, int size
)
527 if (acfg
->align_pad_value
) {
528 for (i
= 0; i
< 16; ++i
)
529 buf
[i
] = acfg
->align_pad_value
;
531 memset (buf
, 0, sizeof (buf
));
534 for (i
= 0; i
< size
; i
+= 16) {
536 emit_bytes (acfg
, buf
, size
- i
);
538 emit_bytes (acfg
, buf
, 16);
543 emit_pointer (MonoAotCompile
*acfg
, const char *target
)
545 mono_img_writer_emit_pointer (acfg
->w
, target
);
549 emit_pointer_2 (MonoAotCompile
*acfg
, const char *prefix
, const char *target
)
551 if (prefix
[0] != '\0') {
552 char *s
= g_strdup_printf ("%s%s", prefix
, target
);
553 mono_img_writer_emit_pointer (acfg
->w
, s
);
556 mono_img_writer_emit_pointer (acfg
->w
, target
);
561 emit_int16 (MonoAotCompile
*acfg
, int value
)
563 mono_img_writer_emit_int16 (acfg
->w
, value
);
567 emit_int32 (MonoAotCompile
*acfg
, int value
)
569 mono_img_writer_emit_int32 (acfg
->w
, value
);
573 emit_symbol_diff (MonoAotCompile
*acfg
, const char *end
, const char* start
, int offset
)
575 mono_img_writer_emit_symbol_diff (acfg
->w
, end
, start
, offset
);
579 emit_zero_bytes (MonoAotCompile
*acfg
, int num
)
581 mono_img_writer_emit_zero_bytes (acfg
->w
, num
);
585 emit_byte (MonoAotCompile
*acfg
, guint8 val
)
587 mono_img_writer_emit_byte (acfg
->w
, val
);
590 #if defined(TARGET_WIN32) && defined(TARGET_X86)
592 static G_GNUC_UNUSED
void
593 emit_global_inner (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
595 const char * mangled_symbol_name
= name
;
596 char * mangled_symbol_name_alloc
= NULL
;
598 mangled_symbol_name_alloc
= mangle_symbol_alloc (name
);
599 mangled_symbol_name
= mangled_symbol_name_alloc
;
601 if (0 != g_strcasecmp (name
, mangled_symbol_name
)) {
602 mono_img_writer_emit_label (acfg
->w
, mangled_symbol_name
);
604 mono_img_writer_emit_global (acfg
->w
, mangled_symbol_name
, func
);
606 if (NULL
!= mangled_symbol_name_alloc
) {
607 g_free (mangled_symbol_name_alloc
);
613 static G_GNUC_UNUSED
void
614 emit_global_inner (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
616 mono_img_writer_emit_global (acfg
->w
, name
, func
);
621 static inline gboolean
622 link_shared_library (MonoAotCompile
*acfg
)
624 return !acfg
->aot_opts
.static_link
&& !acfg
->aot_opts
.asm_only
;
627 static inline gboolean
628 add_to_global_symbol_table (MonoAotCompile
*acfg
)
630 #ifdef TARGET_WIN32_MSVC
631 return acfg
->aot_opts
.no_dlsym
|| link_shared_library (acfg
);
633 return acfg
->aot_opts
.no_dlsym
;
638 emit_global (MonoAotCompile
*acfg
, const char *name
, gboolean func
)
640 if (add_to_global_symbol_table (acfg
))
641 g_ptr_array_add (acfg
->globals
, g_strdup (name
));
643 if (acfg
->aot_opts
.no_dlsym
) {
644 mono_img_writer_emit_local_symbol (acfg
->w
, name
, NULL
, func
);
646 emit_global_inner (acfg
, name
, func
);
651 emit_symbol_size (MonoAotCompile
*acfg
, const char *name
, const char *end_label
)
653 mono_img_writer_emit_symbol_size (acfg
->w
, name
, end_label
);
656 /* Emit a symbol which is referenced by the MonoAotFileInfo structure */
658 emit_info_symbol (MonoAotCompile
*acfg
, const char *name
)
660 char symbol
[MAX_SYMBOL_SIZE
];
663 emit_label (acfg
, name
);
664 /* LLVM generated code references this */
665 sprintf (symbol
, "%s%s%s", acfg
->user_symbol_prefix
, acfg
->global_prefix
, name
);
666 emit_label (acfg
, symbol
);
667 emit_global_inner (acfg
, symbol
, FALSE
);
669 emit_label (acfg
, name
);
674 emit_string_symbol (MonoAotCompile
*acfg
, const char *name
, const char *value
)
677 mono_llvm_emit_aot_data (name
, (guint8
*)value
, strlen (value
) + 1);
681 mono_img_writer_emit_section_change (acfg
->w
, RODATA_SECT
, 1);
683 /* On apple, all symbols need to be aligned to avoid warnings from ld */
684 emit_alignment (acfg
, 4);
686 mono_img_writer_emit_label (acfg
->w
, name
);
687 mono_img_writer_emit_string (acfg
->w
, value
);
690 static G_GNUC_UNUSED
void
691 emit_uleb128 (MonoAotCompile
*acfg
, guint32 value
)
694 guint8 b
= value
& 0x7f;
696 if (value
!= 0) /* more bytes to come */
702 static G_GNUC_UNUSED
void
703 emit_sleb128 (MonoAotCompile
*acfg
, gint64 value
)
706 gboolean negative
= (value
< 0);
713 /* the following is unnecessary if the
714 * implementation of >>= uses an arithmetic rather
715 * than logical shift for a signed left operand
719 value
|= - ((gint64
)1 <<(size
- 7));
720 /* sign bit of byte is second high order bit (0x40) */
721 if ((value
== 0 && !(byte
& 0x40)) ||
722 (value
== -1 && (byte
& 0x40)))
726 emit_byte (acfg
, byte
);
730 static G_GNUC_UNUSED
void
731 encode_uleb128 (guint32 value
, guint8
*buf
, guint8
**endbuf
)
736 guint8 b
= value
& 0x7f;
738 if (value
!= 0) /* more bytes to come */
746 static G_GNUC_UNUSED
void
747 encode_sleb128 (gint32 value
, guint8
*buf
, guint8
**endbuf
)
750 gboolean negative
= (value
< 0);
758 /* the following is unnecessary if the
759 * implementation of >>= uses an arithmetic rather
760 * than logical shift for a signed left operand
764 value
|= - (1 <<(size
- 7));
765 /* sign bit of byte is second high order bit (0x40) */
766 if ((value
== 0 && !(byte
& 0x40)) ||
767 (value
== -1 && (byte
& 0x40)))
778 encode_int (gint32 val
, guint8
*buf
, guint8
**endbuf
)
781 buf
[0] = (val
>> 0) & 0xff;
782 buf
[1] = (val
>> 8) & 0xff;
783 buf
[2] = (val
>> 16) & 0xff;
784 buf
[3] = (val
>> 24) & 0xff;
790 encode_int16 (guint16 val
, guint8
*buf
, guint8
**endbuf
)
792 buf
[0] = (val
>> 0) & 0xff;
793 buf
[1] = (val
>> 8) & 0xff;
799 encode_string (const char *s
, guint8
*buf
, guint8
**endbuf
)
801 int len
= strlen (s
);
803 memcpy (buf
, s
, len
+ 1);
804 *endbuf
= buf
+ len
+ 1;
808 emit_unset_mode (MonoAotCompile
*acfg
)
810 mono_img_writer_emit_unset_mode (acfg
->w
);
813 static G_GNUC_UNUSED
void
814 emit_set_thumb_mode (MonoAotCompile
*acfg
)
816 emit_unset_mode (acfg
);
817 fprintf (acfg
->fp
, ".code 16\n");
820 static G_GNUC_UNUSED
void
821 emit_set_arm_mode (MonoAotCompile
*acfg
)
823 emit_unset_mode (acfg
);
824 fprintf (acfg
->fp
, ".code 32\n");
828 emit_code_bytes (MonoAotCompile
*acfg
, const guint8
* buf
, int size
)
833 g_assert (size
% 4 == 0);
834 emit_unset_mode (acfg
);
835 for (i
= 0; i
< size
; i
+= 4)
836 fprintf (acfg
->fp
, "%s 0x%x\n", acfg
->inst_directive
, *(guint32
*)(buf
+ i
));
838 emit_bytes (acfg
, buf
, size
);
842 /* ARCHITECTURE SPECIFIC CODE */
844 #if defined(TARGET_X86) || defined(TARGET_AMD64) || defined(TARGET_ARM) || defined(TARGET_POWERPC) || defined(TARGET_ARM64)
845 #define EMIT_DWARF_INFO 1
848 #ifdef TARGET_WIN32_MSVC
849 #undef EMIT_DWARF_INFO
852 #if defined(TARGET_ARM)
853 #define AOT_FUNC_ALIGNMENT 4
855 #define AOT_FUNC_ALIGNMENT 16
858 #if defined(TARGET_POWERPC64) && !defined(__mono_ilp32__)
859 #define PPC_LD_OP "ld"
860 #define PPC_LDX_OP "ldx"
862 #define PPC_LD_OP "lwz"
863 #define PPC_LDX_OP "lwzx"
866 #ifdef TARGET_X86_64_WIN32_MSVC
867 #define AOT_TARGET_STR "AMD64 (WIN32) (MSVC codegen)"
869 #define AOT_TARGET_STR "AMD64"
874 #define AOT_TARGET_STR "ARM (MACH)"
876 #define AOT_TARGET_STR "ARM (!MACH)"
882 #define AOT_TARGET_STR "ARM64 (MACH)"
884 #define AOT_TARGET_STR "ARM64 (!MACH)"
888 #ifdef TARGET_POWERPC64
889 #ifdef __mono_ilp32__
890 #define AOT_TARGET_STR "POWERPC64 (mono ilp32)"
892 #define AOT_TARGET_STR "POWERPC64 (!mono ilp32)"
895 #ifdef TARGET_POWERPC
896 #ifdef __mono_ilp32__
897 #define AOT_TARGET_STR "POWERPC (mono ilp32)"
899 #define AOT_TARGET_STR "POWERPC (!mono ilp32)"
906 #define AOT_TARGET_STR "X86 (WIN32)"
907 #elif defined(__native_client_codegen__)
908 #define AOT_TARGET_STR "X86 (native client codegen)"
910 #define AOT_TARGET_STR "X86 (!native client codegen)"
914 #ifndef AOT_TARGET_STR
915 #define AOT_TARGET_STR ""
919 arch_init (MonoAotCompile
*acfg
)
921 acfg
->llc_args
= g_string_new ("");
922 acfg
->as_args
= g_string_new ("");
923 acfg
->llvm_owriter_supported
= TRUE
;
926 * The prefix LLVM likes to put in front of symbol names on darwin.
927 * The mach-os specs require this for globals, but LLVM puts them in front of all
928 * symbols. We need to handle this, since we need to refer to LLVM generated
931 acfg
->llvm_label_prefix
= "";
932 acfg
->user_symbol_prefix
= "";
934 #if defined(TARGET_X86)
935 g_string_append (acfg
->llc_args
, " -march=x86 -mattr=sse4.1");
938 #if defined(TARGET_AMD64)
939 g_string_append (acfg
->llc_args
, " -march=x86-64 -mattr=sse4.1");
941 acfg
->align_pad_value
= 0x90;
945 if (acfg
->aot_opts
.mtriple
&& strstr (acfg
->aot_opts
.mtriple
, "darwin")) {
946 g_string_append (acfg
->llc_args
, "-mattr=+v6");
948 #if defined(ARM_FPU_VFP_HARD)
949 g_string_append (acfg
->llc_args
, " -mattr=+vfp2,-neon,+d16 -float-abi=hard");
950 g_string_append (acfg
->as_args
, " -mfpu=vfp3");
951 #elif defined(ARM_FPU_VFP)
952 g_string_append (acfg
->llc_args
, " -mattr=+vfp2,-neon,+d16");
953 g_string_append (acfg
->as_args
, " -mfpu=vfp3");
955 g_string_append (acfg
->llc_args
, " -soft-float");
958 if (acfg
->aot_opts
.mtriple
&& strstr (acfg
->aot_opts
.mtriple
, "thumb"))
959 acfg
->thumb_mixed
= TRUE
;
961 if (acfg
->aot_opts
.mtriple
)
962 mono_arch_set_target (acfg
->aot_opts
.mtriple
);
966 acfg
->inst_directive
= ".inst";
967 if (acfg
->aot_opts
.mtriple
)
968 mono_arch_set_target (acfg
->aot_opts
.mtriple
);
972 acfg
->user_symbol_prefix
= "_";
973 acfg
->llvm_label_prefix
= "_";
974 acfg
->inst_directive
= ".word";
975 acfg
->need_no_dead_strip
= TRUE
;
976 acfg
->aot_opts
.gnu_asm
= TRUE
;
979 #if defined(__linux__) && !defined(TARGET_ARM)
980 acfg
->need_pt_gnu_stack
= TRUE
;
984 acfg
->global_symbols
= TRUE
;
987 #ifdef TARGET_ANDROID
988 acfg
->llvm_owriter_supported
= FALSE
;
995 /* Load the contents of GOT_SLOT into dreg, clobbering ip0 */
997 arm64_emit_load_got_slot (MonoAotCompile
*acfg
, int dreg
, int got_slot
)
1001 g_assert (acfg
->fp
);
1002 emit_unset_mode (acfg
);
1004 offset
= (int)(got_slot
* sizeof (gpointer
));
1006 /* clang's integrated assembler */
1007 fprintf (acfg
->fp
, "adrp x16, %s@PAGE+%d\n", acfg
->got_symbol
, offset
& 0xfffff000);
1008 fprintf (acfg
->fp
, "add x16, x16, %s@PAGEOFF\n", acfg
->got_symbol
);
1009 fprintf (acfg
->fp
, "ldr x%d, [x16, #%d]\n", dreg
, offset
& 0xfff);
1012 fprintf (acfg
->fp
, "adrp x16, %s+%d\n", acfg
->got_symbol
, offset
& 0xfffff000);
1013 fprintf (acfg
->fp
, "add x16, x16, :lo12:%s\n", acfg
->got_symbol
);
1014 fprintf (acfg
->fp
, "ldr x%d, [x16, %d]\n", dreg
, offset
& 0xfff);
1019 arm64_emit_objc_selector_ref (MonoAotCompile
*acfg
, guint8
*code
, int index
, int *code_size
)
1023 g_assert (acfg
->fp
);
1024 emit_unset_mode (acfg
);
1026 /* ldr rt, target */
1027 reg
= arm_get_ldr_lit_reg (code
);
1029 fprintf (acfg
->fp
, "adrp x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGE\n", reg
, index
);
1030 fprintf (acfg
->fp
, "add x%d, x%d, L_OBJC_SELECTOR_REFERENCES_%d@PAGEOFF\n", reg
, reg
, index
);
1031 fprintf (acfg
->fp
, "ldr x%d, [x%d]\n", reg
, reg
);
1037 arm64_emit_direct_call (MonoAotCompile
*acfg
, const char *target
, gboolean external
, gboolean thumb
, MonoJumpInfo
*ji
, int *call_size
)
1039 g_assert (acfg
->fp
);
1040 emit_unset_mode (acfg
);
1041 if (ji
&& ji
->relocation
== MONO_R_ARM64_B
) {
1042 fprintf (acfg
->fp
, "b %s\n", target
);
1045 g_assert (ji
->relocation
== MONO_R_ARM64_BL
);
1046 fprintf (acfg
->fp
, "bl %s\n", target
);
1052 arm64_emit_got_access (MonoAotCompile
*acfg
, guint8
*code
, int got_slot
, int *code_size
)
1056 /* ldr rt, target */
1057 reg
= arm_get_ldr_lit_reg (code
);
1058 arm64_emit_load_got_slot (acfg
, reg
, got_slot
);
1063 arm64_emit_plt_entry (MonoAotCompile
*acfg
, const char *got_symbol
, int offset
, int info_offset
)
1065 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
/ sizeof (gpointer
));
1066 fprintf (acfg
->fp
, "br x16\n");
1067 /* Used by mono_aot_get_plt_info_offset () */
1068 fprintf (acfg
->fp
, "%s %d\n", acfg
->inst_directive
, info_offset
);
1072 arm64_emit_tramp_page_common_code (MonoAotCompile
*acfg
, int pagesize
, int arg_reg
, int *size
)
1078 /* The common code */
1081 /* The trampoline address is in IP0 */
1082 arm_movzx (code
, ARMREG_IP1
, imm
& 0xffff, 0);
1083 arm_movkx (code
, ARMREG_IP1
, (imm
>> 16) & 0xffff, 16);
1084 /* Compute the data slot address */
1085 arm_subx (code
, ARMREG_IP0
, ARMREG_IP0
, ARMREG_IP1
);
1086 /* Trampoline argument */
1087 arm_ldrx (code
, arg_reg
, ARMREG_IP0
, 0);
1089 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP0
, 8);
1090 arm_brx (code
, ARMREG_IP0
);
1093 emit_code_bytes (acfg
, buf
, code
- buf
);
1099 arm64_emit_tramp_page_specific_code (MonoAotCompile
*acfg
, int pagesize
, int common_tramp_size
, int specific_tramp_size
)
1105 count
= (pagesize
- common_tramp_size
) / specific_tramp_size
;
1106 for (i
= 0; i
< count
; ++i
) {
1108 arm_adrx (code
, ARMREG_IP0
, code
);
1109 /* Branch to the generic code */
1110 arm_b (code
, code
- 4 - (i
* specific_tramp_size
) - common_tramp_size
);
1111 /* This has to be 2 pointers long */
1114 g_assert (code
- buf
== specific_tramp_size
);
1115 emit_code_bytes (acfg
, buf
, code
- buf
);
1120 arm64_emit_specific_trampoline_pages (MonoAotCompile
*acfg
)
1124 guint8
*labels
[16];
1125 int common_tramp_size
;
1126 int specific_tramp_size
= 2 * 8;
1130 if (!acfg
->aot_opts
.use_trampolines_page
)
1134 /* Have to match the target pagesize */
1137 pagesize
= mono_pagesize ();
1139 acfg
->tramp_page_size
= pagesize
;
1141 /* The specific trampolines */
1142 sprintf (symbol
, "%sspecific_trampolines_page", acfg
->user_symbol_prefix
);
1143 emit_alignment (acfg
, pagesize
);
1144 emit_global (acfg
, symbol
, TRUE
);
1145 emit_label (acfg
, symbol
);
1147 /* The common code */
1148 arm64_emit_tramp_page_common_code (acfg
, pagesize
, ARMREG_IP1
, &common_tramp_size
);
1149 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_SPECIFIC
] = common_tramp_size
;
1151 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1153 /* The rgctx trampolines */
1154 /* These are the same as the specific trampolines, but they load the argument into MONO_ARCH_RGCTX_REG */
1155 sprintf (symbol
, "%srgctx_trampolines_page", acfg
->user_symbol_prefix
);
1156 emit_alignment (acfg
, pagesize
);
1157 emit_global (acfg
, symbol
, TRUE
);
1158 emit_label (acfg
, symbol
);
1160 /* The common code */
1161 arm64_emit_tramp_page_common_code (acfg
, pagesize
, MONO_ARCH_RGCTX_REG
, &common_tramp_size
);
1162 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_STATIC_RGCTX
] = common_tramp_size
;
1164 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1166 /* The gsharedvt arg trampolines */
1167 /* These are the same as the specific trampolines */
1168 sprintf (symbol
, "%sgsharedvt_arg_trampolines_page", acfg
->user_symbol_prefix
);
1169 emit_alignment (acfg
, pagesize
);
1170 emit_global (acfg
, symbol
, TRUE
);
1171 emit_label (acfg
, symbol
);
1173 arm64_emit_tramp_page_common_code (acfg
, pagesize
, ARMREG_IP1
, &common_tramp_size
);
1174 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = common_tramp_size
;
1176 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1178 /* The IMT trampolines */
1179 sprintf (symbol
, "%simt_trampolines_page", acfg
->user_symbol_prefix
);
1180 emit_alignment (acfg
, pagesize
);
1181 emit_global (acfg
, symbol
, TRUE
);
1182 emit_label (acfg
, symbol
);
1186 /* The trampoline address is in IP0 */
1187 arm_movzx (code
, ARMREG_IP1
, imm
& 0xffff, 0);
1188 arm_movkx (code
, ARMREG_IP1
, (imm
>> 16) & 0xffff, 16);
1189 /* Compute the data slot address */
1190 arm_subx (code
, ARMREG_IP0
, ARMREG_IP0
, ARMREG_IP1
);
1191 /* Trampoline argument */
1192 arm_ldrx (code
, ARMREG_IP1
, ARMREG_IP0
, 0);
1194 /* Same as arch_emit_imt_trampoline () */
1196 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 0);
1197 arm_cmpx (code
, ARMREG_IP0
, MONO_ARCH_RGCTX_REG
);
1199 arm_bcc (code
, ARMCOND_EQ
, 0);
1201 /* End-of-loop check */
1203 arm_cbzx (code
, ARMREG_IP0
, 0);
1206 arm_addx_imm (code
, ARMREG_IP1
, ARMREG_IP1
, 2 * 8);
1207 arm_b (code
, labels
[0]);
1210 mono_arm_patch (labels
[1], code
, MONO_R_ARM64_BCC
);
1211 /* Load vtable slot addr */
1212 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 8);
1213 /* Load vtable slot */
1214 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP0
, 0);
1215 arm_brx (code
, ARMREG_IP0
);
1218 mono_arm_patch (labels
[2], code
, MONO_R_ARM64_CBZ
);
1219 /* Load fail addr */
1220 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 8);
1221 arm_brx (code
, ARMREG_IP0
);
1223 emit_code_bytes (acfg
, buf
, code
- buf
);
1225 common_tramp_size
= code
- buf
;
1226 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_IMT
] = common_tramp_size
;
1228 arm64_emit_tramp_page_specific_code (acfg
, pagesize
, common_tramp_size
, specific_tramp_size
);
1232 arm64_emit_specific_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1234 /* Load argument from second GOT slot */
1235 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
+ 1);
1236 /* Load generic trampoline address from first GOT slot */
1237 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
);
1238 fprintf (acfg
->fp
, "br x16\n");
1239 *tramp_size
= 7 * 4;
1243 arm64_emit_unbox_trampoline (MonoAotCompile
*acfg
, MonoCompile
*cfg
, MonoMethod
*method
, const char *call_target
)
1245 emit_unset_mode (acfg
);
1246 fprintf (acfg
->fp
, "add x0, x0, %d\n", (int)(sizeof (MonoObject
)));
1247 fprintf (acfg
->fp
, "b %s\n", call_target
);
1251 arm64_emit_static_rgctx_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1253 /* Similar to the specific trampolines, but use the rgctx reg instead of ip1 */
1255 /* Load argument from first GOT slot */
1256 arm64_emit_load_got_slot (acfg
, MONO_ARCH_RGCTX_REG
, offset
);
1257 /* Load generic trampoline address from second GOT slot */
1258 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
+ 1);
1259 fprintf (acfg
->fp
, "br x16\n");
1260 *tramp_size
= 7 * 4;
1264 arm64_emit_imt_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1267 guint8
*code
, *labels
[16];
1269 /* Load parameter from GOT slot into ip1 */
1270 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
);
1274 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 0);
1275 arm_cmpx (code
, ARMREG_IP0
, MONO_ARCH_RGCTX_REG
);
1277 arm_bcc (code
, ARMCOND_EQ
, 0);
1279 /* End-of-loop check */
1281 arm_cbzx (code
, ARMREG_IP0
, 0);
1284 arm_addx_imm (code
, ARMREG_IP1
, ARMREG_IP1
, 2 * 8);
1285 arm_b (code
, labels
[0]);
1288 mono_arm_patch (labels
[1], code
, MONO_R_ARM64_BCC
);
1289 /* Load vtable slot addr */
1290 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 8);
1291 /* Load vtable slot */
1292 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP0
, 0);
1293 arm_brx (code
, ARMREG_IP0
);
1296 mono_arm_patch (labels
[2], code
, MONO_R_ARM64_CBZ
);
1297 /* Load fail addr */
1298 arm_ldrx (code
, ARMREG_IP0
, ARMREG_IP1
, 8);
1299 arm_brx (code
, ARMREG_IP0
);
1301 emit_code_bytes (acfg
, buf
, code
- buf
);
1303 *tramp_size
= code
- buf
+ (3 * 4);
1307 arm64_emit_gsharedvt_arg_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1309 /* Similar to the specific trampolines, but the address is in the second slot */
1310 /* Load argument from first GOT slot */
1311 arm64_emit_load_got_slot (acfg
, ARMREG_R17
, offset
);
1312 /* Load generic trampoline address from second GOT slot */
1313 arm64_emit_load_got_slot (acfg
, ARMREG_R16
, offset
+ 1);
1314 fprintf (acfg
->fp
, "br x16\n");
1315 *tramp_size
= 7 * 4;
1321 #ifdef MONO_ARCH_AOT_SUPPORTED
1323 * arch_emit_direct_call:
1325 * Emit a direct call to the symbol TARGET. CALL_SIZE is set to the size of the
1329 arch_emit_direct_call (MonoAotCompile
*acfg
, const char *target
, gboolean external
, gboolean thumb
, MonoJumpInfo
*ji
, int *call_size
)
1331 #if defined(TARGET_X86) || defined(TARGET_AMD64)
1332 /* Need to make sure this is exactly 5 bytes long */
1333 emit_unset_mode (acfg
);
1334 fprintf (acfg
->fp
, "call %s\n", target
);
1336 #elif defined(TARGET_ARM)
1337 emit_unset_mode (acfg
);
1339 fprintf (acfg
->fp
, "blx %s\n", target
);
1341 fprintf (acfg
->fp
, "bl %s\n", target
);
1343 #elif defined(TARGET_ARM64)
1344 arm64_emit_direct_call (acfg
, target
, external
, thumb
, ji
, call_size
);
1345 #elif defined(TARGET_POWERPC)
1346 emit_unset_mode (acfg
);
1347 fprintf (acfg
->fp
, "bl %s\n", target
);
1350 g_assert_not_reached ();
1357 * - we use an approach similar to the x86 abi: reserve a register (r30) to hold
1359 * - The full-aot trampolines need access to the GOT of mscorlib, so we store
1360 * in in the 2. slot of every GOT, and require every method to place the GOT
1361 * address in r30, even when it doesn't access the GOT otherwise. This way,
1362 * the trampolines can compute the mscorlib GOT address by loading 4(r30).
1367 * PPC64 uses function descriptors which greatly complicate all code, since
1368 * these are used very inconsistently in the runtime. Some functions like
1369 * mono_compile_method () return ftn descriptors, while others like the
1370 * trampoline creation functions do not.
1371 * We assume that all GOT slots contain function descriptors, and create
1372 * descriptors in aot-runtime.c when needed.
1373 * The ppc64 abi uses r2 to hold the address of the TOC/GOT, which is loaded
1374 * from function descriptors, we could do the same, but it would require
1375 * rewriting all the ppc/aot code to handle function descriptors properly.
1376 * So instead, we use the same approach as on PPC32.
1377 * This is a horrible mess, but fixing it would probably lead to an even bigger
1383 * - similar to the PPC32 design, we reserve EBX to hold the GOT pointer.
1386 #ifdef MONO_ARCH_AOT_SUPPORTED
1388 * arch_emit_got_offset:
1390 * The memory pointed to by CODE should hold native code for computing the GOT
1391 * address (OP_LOAD_GOTADDR). Emit this code while patching it with the offset
1392 * between code and the GOT. CODE_SIZE is set to the number of bytes emitted.
1395 arch_emit_got_offset (MonoAotCompile
*acfg
, guint8
*code
, int *code_size
)
1397 #if defined(TARGET_POWERPC64)
1398 emit_unset_mode (acfg
);
1400 * The ppc32 code doesn't seem to work on ppc64, the assembler complains about
1401 * unsupported relocations. So we store the got address into the .Lgot_addr
1402 * symbol which is in the text segment, compute its address, and load it.
1404 fprintf (acfg
->fp
, ".L%d:\n", acfg
->label_generator
);
1405 fprintf (acfg
->fp
, "lis 0, (.Lgot_addr + 4 - .L%d)@h\n", acfg
->label_generator
);
1406 fprintf (acfg
->fp
, "ori 0, 0, (.Lgot_addr + 4 - .L%d)@l\n", acfg
->label_generator
);
1407 fprintf (acfg
->fp
, "add 30, 30, 0\n");
1408 fprintf (acfg
->fp
, "%s 30, 0(30)\n", PPC_LD_OP
);
1409 acfg
->label_generator
++;
1411 #elif defined(TARGET_POWERPC)
1412 emit_unset_mode (acfg
);
1413 fprintf (acfg
->fp
, ".L%d:\n", acfg
->label_generator
);
1414 fprintf (acfg
->fp
, "lis 0, (%s + 4 - .L%d)@h\n", acfg
->got_symbol
, acfg
->label_generator
);
1415 fprintf (acfg
->fp
, "ori 0, 0, (%s + 4 - .L%d)@l\n", acfg
->got_symbol
, acfg
->label_generator
);
1416 acfg
->label_generator
++;
1419 guint32 offset
= mono_arch_get_patch_offset (code
);
1420 emit_bytes (acfg
, code
, offset
);
1421 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", offset
);
1423 *code_size
= offset
+ 4;
1428 * arch_emit_got_access:
1430 * The memory pointed to by CODE should hold native code for loading a GOT
1431 * slot (OP_AOTCONST/OP_GOT_ENTRY). Emit this code while patching it so it accesses the
1432 * GOT slot GOT_SLOT. CODE_SIZE is set to the number of bytes emitted.
1435 arch_emit_got_access (MonoAotCompile
*acfg
, const char *got_symbol
, guint8
*code
, int got_slot
, int *code_size
)
1438 /* mov reg, got+offset(%rip) */
1440 /* The GOT symbol is in the LLVM module, the clang assembler has problems emitting symbol diffs for it */
1444 /* Decode reg, see amd64_mov_reg_membase () */
1445 rex_r
= code
[0] & AMD64_REX_R
;
1446 g_assert (code
[0] == 0x49 + rex_r
);
1447 g_assert (code
[1] == 0x8b);
1448 dreg
= ((code
[2] >> 3) & 0x7) + (rex_r
? 8 : 0);
1450 emit_unset_mode (acfg
);
1451 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %s\n", got_symbol
, (unsigned int) ((got_slot
* sizeof (gpointer
))), mono_arch_regname (dreg
));
1454 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1455 emit_symbol_diff (acfg
, got_symbol
, ".", (unsigned int) ((got_slot
* sizeof (gpointer
)) - 4));
1456 *code_size
= mono_arch_get_patch_offset (code
) + 4;
1458 #elif defined(TARGET_X86)
1459 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1460 emit_int32 (acfg
, (unsigned int) ((got_slot
* sizeof (gpointer
))));
1461 *code_size
= mono_arch_get_patch_offset (code
) + 4;
1462 #elif defined(TARGET_ARM)
1463 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1464 emit_symbol_diff (acfg
, got_symbol
, ".", (unsigned int) ((got_slot
* sizeof (gpointer
))) - 12);
1465 *code_size
= mono_arch_get_patch_offset (code
) + 4;
1466 #elif defined(TARGET_ARM64)
1467 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1468 arm64_emit_got_access (acfg
, code
, got_slot
, code_size
);
1469 #elif defined(TARGET_POWERPC)
1473 emit_bytes (acfg
, code
, mono_arch_get_patch_offset (code
));
1475 ppc_load32 (code
, ppc_r0
, got_slot
* sizeof (gpointer
));
1476 g_assert (code
- buf
== 8);
1477 emit_bytes (acfg
, buf
, code
- buf
);
1478 *code_size
= code
- buf
;
1481 g_assert_not_reached ();
1487 #ifdef MONO_ARCH_AOT_SUPPORTED
1489 * arch_emit_objc_selector_ref:
1491 * Emit the implementation of OP_OBJC_GET_SELECTOR, which itself implements @selector(foo:) in objective-c.
1494 arch_emit_objc_selector_ref (MonoAotCompile
*acfg
, guint8
*code
, int index
, int *code_size
)
1496 #if defined(TARGET_ARM)
1497 char symbol1
[MAX_SYMBOL_SIZE
];
1498 char symbol2
[MAX_SYMBOL_SIZE
];
1499 int lindex
= acfg
->objc_selector_index_2
++;
1501 /* Emit ldr.imm/b */
1502 emit_bytes (acfg
, code
, 8);
1504 sprintf (symbol1
, "L_OBJC_SELECTOR_%d", lindex
);
1505 sprintf (symbol2
, "L_OBJC_SELECTOR_REFERENCES_%d", index
);
1507 emit_label (acfg
, symbol1
);
1508 mono_img_writer_emit_unset_mode (acfg
->w
);
1509 fprintf (acfg
->fp
, ".long %s-(%s+12)", symbol2
, symbol1
);
1512 #elif defined(TARGET_ARM64)
1513 arm64_emit_objc_selector_ref (acfg
, code
, index
, code_size
);
1515 g_assert_not_reached ();
1521 * arch_emit_plt_entry:
1523 * Emit code for the PLT entry.
1524 * The plt entry should look like this:
1525 * <indirect jump to GOT_SYMBOL + OFFSET>
1526 * <INFO_OFFSET embedded into the instruction stream>
1529 arch_emit_plt_entry (MonoAotCompile
*acfg
, const char *got_symbol
, int offset
, int info_offset
)
1531 #if defined(TARGET_X86)
1532 /* jmp *<offset>(%ebx) */
1533 emit_byte (acfg
, 0xff);
1534 emit_byte (acfg
, 0xa3);
1535 emit_int32 (acfg
, offset
);
1536 /* Used by mono_aot_get_plt_info_offset */
1537 emit_int32 (acfg
, info_offset
);
1538 #elif defined(TARGET_AMD64)
1539 emit_unset_mode (acfg
);
1540 fprintf (acfg
->fp
, "jmp *%s+%d(%%rip)\n", got_symbol
, offset
);
1541 /* Used by mono_aot_get_plt_info_offset */
1542 emit_int32 (acfg
, info_offset
);
1543 acfg
->stats
.plt_size
+= 10;
1544 #elif defined(TARGET_ARM)
1549 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 0);
1550 ARM_LDR_REG_REG (code
, ARMREG_PC
, ARMREG_PC
, ARMREG_IP
);
1551 emit_bytes (acfg
, buf
, code
- buf
);
1552 emit_symbol_diff (acfg
, got_symbol
, ".", offset
- 4);
1553 /* Used by mono_aot_get_plt_info_offset */
1554 emit_int32 (acfg
, info_offset
);
1555 #elif defined(TARGET_ARM64)
1556 arm64_emit_plt_entry (acfg
, got_symbol
, offset
, info_offset
);
1557 #elif defined(TARGET_POWERPC)
1558 /* The GOT address is guaranteed to be in r30 by OP_LOAD_GOTADDR */
1559 emit_unset_mode (acfg
);
1560 fprintf (acfg
->fp
, "lis 11, %d@h\n", offset
);
1561 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", offset
);
1562 fprintf (acfg
->fp
, "add 11, 11, 30\n");
1563 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
1564 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1565 fprintf (acfg
->fp
, "%s 2, %d(11)\n", PPC_LD_OP
, (int)sizeof (gpointer
));
1566 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
1568 fprintf (acfg
->fp
, "mtctr 11\n");
1569 fprintf (acfg
->fp
, "bctr\n");
1570 emit_int32 (acfg
, info_offset
);
1572 g_assert_not_reached ();
1577 * arch_emit_llvm_plt_entry:
1579 * Same as arch_emit_plt_entry, but handles calls from LLVM generated code.
1580 * This is only needed on arm to handle thumb interop.
1583 arch_emit_llvm_plt_entry (MonoAotCompile
*acfg
, const char *got_symbol
, int offset
, int info_offset
)
1585 #if defined(TARGET_ARM)
1586 /* LLVM calls the PLT entries using bl, so these have to be thumb2 */
1587 /* The caller already transitioned to thumb */
1588 /* The code below should be 12 bytes long */
1589 /* clang has trouble encoding these instructions, so emit the binary */
1591 fprintf (acfg
->fp
, "ldr ip, [pc, #8]\n");
1592 /* thumb can't encode ld pc, [pc, ip] */
1593 fprintf (acfg
->fp
, "add ip, pc, ip\n");
1594 fprintf (acfg
->fp
, "ldr ip, [ip, #0]\n");
1595 fprintf (acfg
->fp
, "bx ip\n");
1597 emit_set_thumb_mode (acfg
);
1598 fprintf (acfg
->fp
, ".4byte 0xc008f8df\n");
1599 fprintf (acfg
->fp
, ".2byte 0x44fc\n");
1600 fprintf (acfg
->fp
, ".4byte 0xc000f8dc\n");
1601 fprintf (acfg
->fp
, ".2byte 0x4760\n");
1602 emit_symbol_diff (acfg
, got_symbol
, ".", offset
+ 4);
1603 emit_int32 (acfg
, info_offset
);
1604 emit_unset_mode (acfg
);
1605 emit_set_arm_mode (acfg
);
1607 g_assert_not_reached ();
1611 /* Save unwind_info in the module and emit the offset to the information at symbol */
1612 static void save_unwind_info (MonoAotCompile
*acfg
, char *symbol
, GSList
*unwind_ops
)
1614 guint32 uw_offset
, encoded_len
;
1617 emit_section_change (acfg
, RODATA_SECT
, 0);
1618 emit_global (acfg
, symbol
, FALSE
);
1619 emit_label (acfg
, symbol
);
1621 encoded
= mono_unwind_ops_encode (unwind_ops
, &encoded_len
);
1622 uw_offset
= get_unwind_info_offset (acfg
, encoded
, encoded_len
);
1624 emit_int32 (acfg
, uw_offset
);
1628 * arch_emit_specific_trampoline_pages:
1630 * Emits a page full of trampolines: each trampoline uses its own address to
1631 * lookup both the generic trampoline code and the data argument.
1632 * This page can be remapped in process multiple times so we can get an
1633 * unlimited number of trampolines.
1634 * Specifically this implementation uses the following trick: two memory pages
1635 * are allocated, with the first containing the data and the second containing the trampolines.
1636 * To reduce trampoline size, each trampoline jumps at the start of the page where a common
1637 * implementation does all the lifting.
1638 * Note that the ARM single trampoline size is 8 bytes, exactly like the data that needs to be stored
1639 * on the arm 32 bit system.
1642 arch_emit_specific_trampoline_pages (MonoAotCompile
*acfg
)
1644 #if defined(TARGET_ARM)
1647 guint8
*loop_start
, *loop_branch_back
, *loop_end_check
, *imt_found_check
;
1649 int pagesize
= MONO_AOT_TRAMP_PAGE_SIZE
;
1650 GSList
*unwind_ops
= NULL
;
1651 #define COMMON_TRAMP_SIZE 16
1652 int count
= (pagesize
- COMMON_TRAMP_SIZE
) / 8;
1653 int imm8
, rot_amount
;
1656 if (!acfg
->aot_opts
.use_trampolines_page
)
1659 acfg
->tramp_page_size
= pagesize
;
1661 sprintf (symbol
, "%sspecific_trampolines_page", acfg
->user_symbol_prefix
);
1662 emit_alignment (acfg
, pagesize
);
1663 emit_global (acfg
, symbol
, TRUE
);
1664 emit_label (acfg
, symbol
);
1666 /* emit the generic code first, the trampoline address + 8 is in the lr register */
1668 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1669 ARM_SUB_REG_IMM (code
, ARMREG_LR
, ARMREG_LR
, imm8
, rot_amount
);
1670 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_LR
, -8);
1671 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_LR
, -4);
1673 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
1676 emit_bytes (acfg
, buf
, code
- buf
);
1678 for (i
= 0; i
< count
; ++i
) {
1680 ARM_PUSH (code
, 0x5fff);
1682 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
1683 g_assert (code
- buf
== 8);
1684 emit_bytes (acfg
, buf
, code
- buf
);
1687 /* now the rgctx trampolines: each specific trampolines puts in the ip register
1688 * the instruction pointer address, so the generic trampoline at the start of the page
1689 * subtracts 4096 to get to the data page and loads the values
1690 * We again fit the generic trampiline in 16 bytes.
1692 sprintf (symbol
, "%srgctx_trampolines_page", acfg
->user_symbol_prefix
);
1693 emit_global (acfg
, symbol
, TRUE
);
1694 emit_label (acfg
, symbol
);
1696 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1697 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
1698 ARM_LDR_IMM (code
, MONO_ARCH_RGCTX_REG
, ARMREG_IP
, -8);
1699 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_IP
, -4);
1701 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
1704 emit_bytes (acfg
, buf
, code
- buf
);
1706 for (i
= 0; i
< count
; ++i
) {
1708 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
1710 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
1711 g_assert (code
- buf
== 8);
1712 emit_bytes (acfg
, buf
, code
- buf
);
1716 * gsharedvt arg trampolines: see arch_emit_gsharedvt_arg_trampoline ()
1718 sprintf (symbol
, "%sgsharedvt_arg_trampolines_page", acfg
->user_symbol_prefix
);
1719 emit_global (acfg
, symbol
, TRUE
);
1720 emit_label (acfg
, symbol
);
1722 ARM_PUSH (code
, (1 << ARMREG_R0
) | (1 << ARMREG_R1
) | (1 << ARMREG_R2
) | (1 << ARMREG_R3
));
1723 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1724 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
1725 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_IP
, -8);
1726 ARM_LDR_IMM (code
, ARMREG_PC
, ARMREG_IP
, -4);
1727 g_assert (code
- buf
== COMMON_TRAMP_SIZE
);
1729 emit_bytes (acfg
, buf
, code
- buf
);
1731 for (i
= 0; i
< count
; ++i
) {
1733 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
1735 arm_patch (code
- 4, code
- COMMON_TRAMP_SIZE
- 8 * (i
+ 1));
1736 g_assert (code
- buf
== 8);
1737 emit_bytes (acfg
, buf
, code
- buf
);
1740 /* now the imt trampolines: each specific trampolines puts in the ip register
1741 * the instruction pointer address, so the generic trampoline at the start of the page
1742 * subtracts 4096 to get to the data page and loads the values
1744 #define IMT_TRAMP_SIZE 72
1745 sprintf (symbol
, "%simt_trampolines_page", acfg
->user_symbol_prefix
);
1746 emit_global (acfg
, symbol
, TRUE
);
1747 emit_label (acfg
, symbol
);
1749 /* Need at least two free registers, plus a slot for storing the pc */
1750 ARM_PUSH (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_R2
));
1752 imm8
= mono_arm_is_rotated_imm8 (pagesize
, &rot_amount
);
1753 ARM_SUB_REG_IMM (code
, ARMREG_IP
, ARMREG_IP
, imm8
, rot_amount
);
1754 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_IP
, -8);
1756 /* The IMT method is in v5, r0 has the imt array address */
1759 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_R0
, 0);
1760 ARM_CMP_REG_REG (code
, ARMREG_R1
, ARMREG_V5
);
1761 imt_found_check
= code
;
1762 ARM_B_COND (code
, ARMCOND_EQ
, 0);
1764 /* End-of-loop check */
1765 ARM_CMP_REG_IMM (code
, ARMREG_R1
, 0, 0);
1766 loop_end_check
= code
;
1767 ARM_B_COND (code
, ARMCOND_EQ
, 0);
1770 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, sizeof (gpointer
) * 2);
1771 loop_branch_back
= code
;
1773 arm_patch (loop_branch_back
, loop_start
);
1776 arm_patch (imt_found_check
, code
);
1777 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
1778 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 0);
1779 /* Save it to the third stack slot */
1780 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
1781 /* Restore the registers and branch */
1782 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
1785 arm_patch (loop_end_check
, code
);
1786 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
1787 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
1788 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
1792 g_assert (code
- buf
== IMT_TRAMP_SIZE
);
1793 emit_bytes (acfg
, buf
, code
- buf
);
1795 for (i
= 0; i
< count
; ++i
) {
1797 ARM_MOV_REG_REG (code
, ARMREG_IP
, ARMREG_PC
);
1799 arm_patch (code
- 4, code
- IMT_TRAMP_SIZE
- 8 * (i
+ 1));
1800 g_assert (code
- buf
== 8);
1801 emit_bytes (acfg
, buf
, code
- buf
);
1804 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_SPECIFIC
] = 16;
1805 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_STATIC_RGCTX
] = 16;
1806 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_IMT
] = 72;
1807 acfg
->tramp_page_code_offsets
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = 16;
1809 /* Unwind info for specifc trampolines */
1810 sprintf (symbol
, "%sspecific_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
1811 /* We unwind to the original caller, from the stack, since lr is clobbered */
1812 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 14 * sizeof (mgreg_t
));
1813 mono_add_unwind_op_offset (unwind_ops
, 0, 0, ARMREG_LR
, -4);
1814 save_unwind_info (acfg
, symbol
, unwind_ops
);
1815 mono_free_unwind_info (unwind_ops
);
1817 sprintf (symbol
, "%sspecific_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
1818 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
1819 mono_add_unwind_op_def_cfa_offset (unwind_ops
, 4, 0, 14 * sizeof (mgreg_t
));
1820 save_unwind_info (acfg
, symbol
, unwind_ops
);
1821 mono_free_unwind_info (unwind_ops
);
1823 /* Unwind info for rgctx trampolines */
1824 sprintf (symbol
, "%srgctx_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
1825 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
1826 save_unwind_info (acfg
, symbol
, unwind_ops
);
1828 sprintf (symbol
, "%srgctx_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
1829 save_unwind_info (acfg
, symbol
, unwind_ops
);
1830 mono_free_unwind_info (unwind_ops
);
1832 /* Unwind info for gsharedvt trampolines */
1833 sprintf (symbol
, "%sgsharedvt_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
1834 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
1835 mono_add_unwind_op_def_cfa_offset (unwind_ops
, 4, 0, 4 * sizeof (mgreg_t
));
1836 save_unwind_info (acfg
, symbol
, unwind_ops
);
1837 mono_free_unwind_info (unwind_ops
);
1839 sprintf (symbol
, "%sgsharedvt_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
1840 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
1841 save_unwind_info (acfg
, symbol
, unwind_ops
);
1842 mono_free_unwind_info (unwind_ops
);
1844 /* Unwind info for imt trampolines */
1845 sprintf (symbol
, "%simt_trampolines_page_gen_p", acfg
->user_symbol_prefix
);
1846 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
1847 mono_add_unwind_op_def_cfa_offset (unwind_ops
, 4, 0, 3 * sizeof (mgreg_t
));
1848 save_unwind_info (acfg
, symbol
, unwind_ops
);
1849 mono_free_unwind_info (unwind_ops
);
1851 sprintf (symbol
, "%simt_trampolines_page_sp_p", acfg
->user_symbol_prefix
);
1852 mono_add_unwind_op_def_cfa (unwind_ops
, 0, 0, ARMREG_SP
, 0);
1853 save_unwind_info (acfg
, symbol
, unwind_ops
);
1854 mono_free_unwind_info (unwind_ops
);
1855 #elif defined(TARGET_ARM64)
1856 arm64_emit_specific_trampoline_pages (acfg
);
1861 * arch_emit_specific_trampoline:
1863 * Emit code for a specific trampoline. OFFSET is the offset of the first of
1864 * two GOT slots which contain the generic trampoline address and the trampoline
1865 * argument. TRAMP_SIZE is set to the size of the emitted trampoline.
1868 arch_emit_specific_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
1871 * The trampolines created here are variations of the specific
1872 * trampolines created in mono_arch_create_specific_trampoline (). The
1874 * - the generic trampoline address is taken from a got slot.
1875 * - the offset of the got slot where the trampoline argument is stored
1876 * is embedded in the instruction stream, and the generic trampoline
1877 * can load the argument by loading the offset, adding it to the
1878 * address of the trampoline to get the address of the got slot, and
1879 * loading the argument from there.
1880 * - all the trampolines should be of the same length.
1882 #if defined(TARGET_AMD64)
1883 /* This should be exactly 8 bytes long */
1885 /* call *<offset>(%rip) */
1887 emit_unset_mode (acfg
);
1888 fprintf (acfg
->fp
, "call *%s+%d(%%rip)\n", acfg
->got_symbol
, (int)(offset
* sizeof (gpointer
)));
1889 emit_zero_bytes (acfg
, 2);
1891 emit_byte (acfg
, '\x41');
1892 emit_byte (acfg
, '\xff');
1893 emit_byte (acfg
, '\x15');
1894 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (gpointer
)) - 4);
1895 emit_zero_bytes (acfg
, 1);
1897 #elif defined(TARGET_ARM)
1901 /* This should be exactly 20 bytes long */
1904 ARM_PUSH (code
, 0x5fff);
1905 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_PC
, 4);
1906 /* Load the value from the GOT */
1907 ARM_LDR_REG_REG (code
, ARMREG_R1
, ARMREG_PC
, ARMREG_R1
);
1909 ARM_BLX_REG (code
, ARMREG_R1
);
1911 g_assert (code
- buf
== 16);
1914 emit_bytes (acfg
, buf
, code
- buf
);
1916 * Only one offset is needed, since the second one would be equal to the
1919 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (gpointer
)) - 4 + 4);
1920 //emit_symbol_diff (acfg, acfg->got_symbol, ".", ((offset + 1) * sizeof (gpointer)) - 4 + 8);
1921 #elif defined(TARGET_ARM64)
1922 arm64_emit_specific_trampoline (acfg
, offset
, tramp_size
);
1923 #elif defined(TARGET_POWERPC)
1931 * PPC has no ip relative addressing, so we need to compute the address
1932 * of the mscorlib got. That is slow and complex, so instead, we store it
1933 * in the second got slot of every aot image. The caller already computed
1934 * the address of its got and placed it into r30.
1936 emit_unset_mode (acfg
);
1937 /* Load mscorlib got address */
1938 fprintf (acfg
->fp
, "%s 0, %d(30)\n", PPC_LD_OP
, (int)sizeof (gpointer
));
1939 /* Load generic trampoline address */
1940 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)(offset
* sizeof (gpointer
)));
1941 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)(offset
* sizeof (gpointer
)));
1942 fprintf (acfg
->fp
, "%s 11, 11, 0\n", PPC_LDX_OP
);
1943 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1944 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
1946 fprintf (acfg
->fp
, "mtctr 11\n");
1947 /* Load trampoline argument */
1948 /* On ppc, we pass it normally to the generic trampoline */
1949 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)((offset
+ 1) * sizeof (gpointer
)));
1950 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)((offset
+ 1) * sizeof (gpointer
)));
1951 fprintf (acfg
->fp
, "%s 0, 11, 0\n", PPC_LDX_OP
);
1952 /* Branch to generic trampoline */
1953 fprintf (acfg
->fp
, "bctr\n");
1955 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
1956 *tramp_size
= 10 * 4;
1958 *tramp_size
= 9 * 4;
1960 #elif defined(TARGET_X86)
1964 /* Similar to the PPC code above */
1966 /* FIXME: Could this clobber the register needed by get_vcall_slot () ? */
1969 /* Load mscorlib got address */
1970 x86_mov_reg_membase (code
, X86_ECX
, MONO_ARCH_GOT_REG
, sizeof (gpointer
), 4);
1971 /* Push trampoline argument */
1972 x86_push_membase (code
, X86_ECX
, (offset
+ 1) * sizeof (gpointer
));
1973 /* Load generic trampoline address */
1974 x86_mov_reg_membase (code
, X86_ECX
, X86_ECX
, offset
* sizeof (gpointer
), 4);
1975 /* Branch to generic trampoline */
1976 x86_jump_reg (code
, X86_ECX
);
1978 emit_bytes (acfg
, buf
, code
- buf
);
1981 g_assert (code
- buf
== *tramp_size
);
1983 g_assert_not_reached ();
1988 * arch_emit_unbox_trampoline:
1990 * Emit code for the unbox trampoline for METHOD used in the full-aot case.
1991 * CALL_TARGET is the symbol pointing to the native code of METHOD.
1994 arch_emit_unbox_trampoline (MonoAotCompile
*acfg
, MonoCompile
*cfg
, MonoMethod
*method
, const char *call_target
)
1996 #if defined(TARGET_AMD64)
2001 this_reg
= mono_arch_get_this_arg_reg (NULL
);
2003 amd64_alu_reg_imm (code
, X86_ADD
, this_reg
, sizeof (MonoObject
));
2005 emit_bytes (acfg
, buf
, code
- buf
);
2008 emit_unset_mode (acfg
);
2009 fprintf (acfg
->fp
, "jmp %s\n", call_target
);
2011 emit_byte (acfg
, '\xe9');
2012 emit_symbol_diff (acfg
, call_target
, ".", -4);
2014 #elif defined(TARGET_X86)
2021 x86_alu_membase_imm (code
, X86_ADD
, X86_ESP
, this_pos
, sizeof (MonoObject
));
2023 emit_bytes (acfg
, buf
, code
- buf
);
2026 emit_byte (acfg
, '\xe9');
2027 emit_symbol_diff (acfg
, call_target
, ".", -4);
2028 #elif defined(TARGET_ARM)
2032 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
) {
2033 fprintf (acfg
->fp
, "add r0, r0, #%d\n", (int)sizeof (MonoObject
));
2034 fprintf (acfg
->fp
, "b %s\n", call_target
);
2035 fprintf (acfg
->fp
, ".arm\n");
2036 fprintf (acfg
->fp
, ".align 2\n");
2042 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, sizeof (MonoObject
));
2044 emit_bytes (acfg
, buf
, code
- buf
);
2045 /* jump to method */
2046 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
)
2047 fprintf (acfg
->fp
, "\n\tbx %s\n", call_target
);
2049 fprintf (acfg
->fp
, "\n\tb %s\n", call_target
);
2050 #elif defined(TARGET_ARM64)
2051 arm64_emit_unbox_trampoline (acfg
, cfg
, method
, call_target
);
2052 #elif defined(TARGET_POWERPC)
2055 fprintf (acfg
->fp
, "\n\taddi %d, %d, %d\n", this_pos
, this_pos
, (int)sizeof (MonoObject
));
2056 fprintf (acfg
->fp
, "\n\tb %s\n", call_target
);
2058 g_assert_not_reached ();
2063 * arch_emit_static_rgctx_trampoline:
2065 * Emit code for a static rgctx trampoline. OFFSET is the offset of the first of
2066 * two GOT slots which contain the rgctx argument, and the method to jump to.
2067 * TRAMP_SIZE is set to the size of the emitted trampoline.
2068 * These kinds of trampolines cannot be enumerated statically, since there could
2069 * be one trampoline per method instantiation, so we emit the same code for all
2070 * trampolines, and parameterize them using two GOT slots.
2073 arch_emit_static_rgctx_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2075 #if defined(TARGET_AMD64)
2076 /* This should be exactly 13 bytes long */
2080 emit_unset_mode (acfg
);
2081 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %%r10\n", acfg
->got_symbol
, (int)(offset
* sizeof (gpointer
)));
2082 fprintf (acfg
->fp
, "jmp *%s+%d(%%rip)\n", acfg
->got_symbol
, (int)((offset
+ 1) * sizeof (gpointer
)));
2084 /* mov <OFFSET>(%rip), %r10 */
2085 emit_byte (acfg
, '\x4d');
2086 emit_byte (acfg
, '\x8b');
2087 emit_byte (acfg
, '\x15');
2088 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (gpointer
)) - 4);
2090 /* jmp *<offset>(%rip) */
2091 emit_byte (acfg
, '\xff');
2092 emit_byte (acfg
, '\x25');
2093 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", ((offset
+ 1) * sizeof (gpointer
)) - 4);
2095 #elif defined(TARGET_ARM)
2099 /* This should be exactly 24 bytes long */
2102 /* Load rgctx value */
2103 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 8);
2104 ARM_LDR_REG_REG (code
, MONO_ARCH_RGCTX_REG
, ARMREG_PC
, ARMREG_IP
);
2105 /* Load branch addr + branch */
2106 ARM_LDR_IMM (code
, ARMREG_IP
, ARMREG_PC
, 4);
2107 ARM_LDR_REG_REG (code
, ARMREG_PC
, ARMREG_PC
, ARMREG_IP
);
2109 g_assert (code
- buf
== 16);
2112 emit_bytes (acfg
, buf
, code
- buf
);
2113 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (gpointer
)) - 4 + 8);
2114 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", ((offset
+ 1) * sizeof (gpointer
)) - 4 + 4);
2115 #elif defined(TARGET_ARM64)
2116 arm64_emit_static_rgctx_trampoline (acfg
, offset
, tramp_size
);
2117 #elif defined(TARGET_POWERPC)
2125 * PPC has no ip relative addressing, so we need to compute the address
2126 * of the mscorlib got. That is slow and complex, so instead, we store it
2127 * in the second got slot of every aot image. The caller already computed
2128 * the address of its got and placed it into r30.
2130 emit_unset_mode (acfg
);
2131 /* Load mscorlib got address */
2132 fprintf (acfg
->fp
, "%s 0, %d(30)\n", PPC_LD_OP
, (int)sizeof (gpointer
));
2134 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)(offset
* sizeof (gpointer
)));
2135 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)(offset
* sizeof (gpointer
)));
2136 fprintf (acfg
->fp
, "%s %d, 11, 0\n", PPC_LDX_OP
, MONO_ARCH_RGCTX_REG
);
2137 /* Load target address */
2138 fprintf (acfg
->fp
, "lis 11, %d@h\n", (int)((offset
+ 1) * sizeof (gpointer
)));
2139 fprintf (acfg
->fp
, "ori 11, 11, %d@l\n", (int)((offset
+ 1) * sizeof (gpointer
)));
2140 fprintf (acfg
->fp
, "%s 11, 11, 0\n", PPC_LDX_OP
);
2141 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2142 fprintf (acfg
->fp
, "%s 2, %d(11)\n", PPC_LD_OP
, (int)sizeof (gpointer
));
2143 fprintf (acfg
->fp
, "%s 11, 0(11)\n", PPC_LD_OP
);
2145 fprintf (acfg
->fp
, "mtctr 11\n");
2146 /* Branch to the target address */
2147 fprintf (acfg
->fp
, "bctr\n");
2149 #ifdef PPC_USES_FUNCTION_DESCRIPTOR
2150 *tramp_size
= 11 * 4;
2152 *tramp_size
= 9 * 4;
2155 #elif defined(TARGET_X86)
2159 /* Similar to the PPC code above */
2161 g_assert (MONO_ARCH_RGCTX_REG
!= X86_ECX
);
2164 /* Load mscorlib got address */
2165 x86_mov_reg_membase (code
, X86_ECX
, MONO_ARCH_GOT_REG
, sizeof (gpointer
), 4);
2167 x86_mov_reg_membase (code
, MONO_ARCH_RGCTX_REG
, X86_ECX
, offset
* sizeof (gpointer
), 4);
2168 /* Branch to the target address */
2169 x86_jump_membase (code
, X86_ECX
, (offset
+ 1) * sizeof (gpointer
));
2171 emit_bytes (acfg
, buf
, code
- buf
);
2174 g_assert (code
- buf
== *tramp_size
);
2176 g_assert_not_reached ();
2181 * arch_emit_imt_trampoline:
2183 * Emit an IMT trampoline usable in full-aot mode. The trampoline uses 1 got slot which
2184 * points to an array of pointer pairs. The pairs of the form [key, ptr], where
2185 * key is the IMT key, and ptr holds the address of a memory location holding
2186 * the address to branch to if the IMT arg matches the key. The array is
2187 * terminated by a pair whose key is NULL, and whose ptr is the address of the
2189 * TRAMP_SIZE is set to the size of the emitted trampoline.
2192 arch_emit_imt_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2194 #if defined(TARGET_AMD64)
2196 guint8
*labels
[16];
2198 guint8
*mov_buf_ptr
= mov_buf
;
2200 const int kSizeOfMove
= 7;
2202 code
= buf
= (guint8
*)g_malloc (256);
2204 /* FIXME: Optimize this, i.e. use binary search etc. */
2205 /* Maybe move the body into a separate function (slower, but much smaller) */
2207 /* MONO_ARCH_IMT_SCRATCH_REG is a free register */
2210 emit_unset_mode (acfg
);
2211 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %s\n", acfg
->got_symbol
, (int)(offset
* sizeof (gpointer
)), mono_arch_regname (MONO_ARCH_IMT_SCRATCH_REG
));
2215 amd64_alu_membase_imm (code
, X86_CMP
, MONO_ARCH_IMT_SCRATCH_REG
, 0, 0);
2217 amd64_branch8 (code
, X86_CC_Z
, 0, FALSE
);
2220 amd64_alu_membase_reg_size (code
, X86_CMP
, MONO_ARCH_IMT_SCRATCH_REG
, 0, MONO_ARCH_IMT_REG
, sizeof (gpointer
));
2222 amd64_branch8 (code
, X86_CC_Z
, 0, FALSE
);
2225 amd64_alu_reg_imm (code
, X86_ADD
, MONO_ARCH_IMT_SCRATCH_REG
, 2 * sizeof (gpointer
));
2226 amd64_jump_code (code
, labels
[0]);
2229 mono_amd64_patch (labels
[2], code
);
2230 amd64_mov_reg_membase (code
, MONO_ARCH_IMT_SCRATCH_REG
, MONO_ARCH_IMT_SCRATCH_REG
, sizeof (gpointer
), sizeof (gpointer
));
2231 amd64_jump_membase (code
, MONO_ARCH_IMT_SCRATCH_REG
, 0);
2234 mono_amd64_patch (labels
[1], code
);
2235 /* Load fail tramp */
2236 amd64_alu_reg_imm (code
, X86_ADD
, MONO_ARCH_IMT_SCRATCH_REG
, sizeof (gpointer
));
2237 /* Check if there is a fail tramp */
2238 amd64_alu_membase_imm (code
, X86_CMP
, MONO_ARCH_IMT_SCRATCH_REG
, 0, 0);
2240 amd64_branch8 (code
, X86_CC_Z
, 0, FALSE
);
2241 /* Jump to fail tramp */
2242 amd64_jump_membase (code
, MONO_ARCH_IMT_SCRATCH_REG
, 0);
2245 mono_amd64_patch (labels
[3], code
);
2246 x86_breakpoint (code
);
2249 /* mov <OFFSET>(%rip), MONO_ARCH_IMT_SCRATCH_REG */
2250 amd64_emit_rex (mov_buf_ptr
, sizeof(gpointer
), MONO_ARCH_IMT_SCRATCH_REG
, 0, AMD64_RIP
);
2251 *(mov_buf_ptr
)++ = (unsigned char)0x8b; /* mov opcode */
2252 x86_address_byte (mov_buf_ptr
, 0, MONO_ARCH_IMT_SCRATCH_REG
& 0x7, 5);
2253 emit_bytes (acfg
, mov_buf
, mov_buf_ptr
- mov_buf
);
2254 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (gpointer
)) - 4);
2256 emit_bytes (acfg
, buf
, code
- buf
);
2258 *tramp_size
= code
- buf
+ kSizeOfMove
;
2262 #elif defined(TARGET_X86)
2264 guint8
*labels
[16];
2266 code
= buf
= g_malloc (256);
2268 /* Allocate a temporary stack slot */
2269 x86_push_reg (code
, X86_EAX
);
2271 x86_push_reg (code
, X86_EAX
);
2273 /* Load mscorlib got address */
2274 x86_mov_reg_membase (code
, X86_EAX
, MONO_ARCH_GOT_REG
, sizeof (gpointer
), 4);
2276 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, offset
* sizeof (gpointer
), 4);
2279 x86_alu_membase_imm (code
, X86_CMP
, X86_EAX
, 0, 0);
2281 x86_branch8 (code
, X86_CC_Z
, FALSE
, 0);
2284 x86_alu_membase_reg (code
, X86_CMP
, X86_EAX
, 0, MONO_ARCH_IMT_REG
);
2286 x86_branch8 (code
, X86_CC_Z
, FALSE
, 0);
2289 x86_alu_reg_imm (code
, X86_ADD
, X86_EAX
, 2 * sizeof (gpointer
));
2290 x86_jump_code (code
, labels
[0]);
2293 mono_x86_patch (labels
[2], code
);
2294 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, sizeof (gpointer
), 4);
2295 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, 0, 4);
2296 /* Save the target address to the temporary stack location */
2297 x86_mov_membase_reg (code
, X86_ESP
, 4, X86_EAX
, 4);
2299 x86_pop_reg (code
, X86_EAX
);
2300 /* Jump to the target address */
2304 mono_x86_patch (labels
[1], code
);
2305 /* Load fail tramp */
2306 x86_mov_reg_membase (code
, X86_EAX
, X86_EAX
, sizeof (gpointer
), 4);
2307 x86_alu_membase_imm (code
, X86_CMP
, X86_EAX
, 0, 0);
2309 x86_branch8 (code
, X86_CC_Z
, FALSE
, 0);
2310 /* Jump to fail tramp */
2311 x86_mov_membase_reg (code
, X86_ESP
, 4, X86_EAX
, 4);
2312 x86_pop_reg (code
, X86_EAX
);
2316 mono_x86_patch (labels
[3], code
);
2317 x86_breakpoint (code
);
2319 emit_bytes (acfg
, buf
, code
- buf
);
2321 *tramp_size
= code
- buf
;
2325 #elif defined(TARGET_ARM)
2327 guint8
*code
, *code2
, *labels
[16];
2331 /* The IMT method is in v5 */
2333 /* Need at least two free registers, plus a slot for storing the pc */
2334 ARM_PUSH (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_R2
));
2336 /* Load the parameter from the GOT */
2337 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_PC
, 0);
2338 ARM_LDR_REG_REG (code
, ARMREG_R0
, ARMREG_PC
, ARMREG_R0
);
2341 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_R0
, 0);
2342 ARM_CMP_REG_REG (code
, ARMREG_R1
, ARMREG_V5
);
2344 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2346 /* End-of-loop check */
2347 ARM_CMP_REG_IMM (code
, ARMREG_R1
, 0, 0);
2349 ARM_B_COND (code
, ARMCOND_EQ
, 0);
2352 ARM_ADD_REG_IMM8 (code
, ARMREG_R0
, ARMREG_R0
, sizeof (gpointer
) * 2);
2355 arm_patch (labels
[4], labels
[1]);
2358 arm_patch (labels
[2], code
);
2359 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2360 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 0);
2361 /* Save it to the third stack slot */
2362 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2363 /* Restore the registers and branch */
2364 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2367 arm_patch (labels
[3], code
);
2368 ARM_LDR_IMM (code
, ARMREG_R0
, ARMREG_R0
, 4);
2369 ARM_STR_IMM (code
, ARMREG_R0
, ARMREG_SP
, 8);
2370 ARM_POP (code
, (1 << ARMREG_R0
)|(1 << ARMREG_R1
)|(1 << ARMREG_PC
));
2374 ARM_LDR_IMM (code2
, ARMREG_R0
, ARMREG_PC
, (code
- (labels
[0] + 8)));
2376 emit_bytes (acfg
, buf
, code
- buf
);
2377 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (gpointer
)) + (code
- (labels
[0] + 8)) - 4);
2379 *tramp_size
= code
- buf
+ 4;
2380 #elif defined(TARGET_ARM64)
2381 arm64_emit_imt_trampoline (acfg
, offset
, tramp_size
);
2382 #elif defined(TARGET_POWERPC)
2384 guint8
*code
, *labels
[16];
2388 /* Load the mscorlib got address */
2389 ppc_ldptr (code
, ppc_r12
, sizeof (gpointer
), ppc_r30
);
2390 /* Load the parameter from the GOT */
2391 ppc_load (code
, ppc_r0
, offset
* sizeof (gpointer
));
2392 ppc_ldptr_indexed (code
, ppc_r12
, ppc_r12
, ppc_r0
);
2394 /* Load and check key */
2396 ppc_ldptr (code
, ppc_r0
, 0, ppc_r12
);
2397 ppc_cmp (code
, 0, sizeof (gpointer
) == 8 ? 1 : 0, ppc_r0
, MONO_ARCH_IMT_REG
);
2399 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
2401 /* End-of-loop check */
2402 ppc_cmpi (code
, 0, sizeof (gpointer
) == 8 ? 1 : 0, ppc_r0
, 0);
2404 ppc_bc (code
, PPC_BR_TRUE
, PPC_BR_EQ
, 0);
2407 ppc_addi (code
, ppc_r12
, ppc_r12
, 2 * sizeof (gpointer
));
2410 mono_ppc_patch (labels
[4], labels
[1]);
2413 mono_ppc_patch (labels
[2], code
);
2414 ppc_ldptr (code
, ppc_r12
, sizeof (gpointer
), ppc_r12
);
2415 /* r12 now contains the value of the vtable slot */
2416 /* this is not a function descriptor on ppc64 */
2417 ppc_ldptr (code
, ppc_r12
, 0, ppc_r12
);
2418 ppc_mtctr (code
, ppc_r12
);
2419 ppc_bcctr (code
, PPC_BR_ALWAYS
, 0);
2422 mono_ppc_patch (labels
[3], code
);
2426 *tramp_size
= code
- buf
;
2428 emit_bytes (acfg
, buf
, code
- buf
);
2430 g_assert_not_reached ();
2435 #if defined (TARGET_AMD64)
2438 amd64_emit_load_got_slot (MonoAotCompile
*acfg
, int dreg
, int got_slot
)
2441 g_assert (acfg
->fp
);
2442 emit_unset_mode (acfg
);
2444 fprintf (acfg
->fp
, "mov %s+%d(%%rip), %s\n", acfg
->got_symbol
, (unsigned int) ((got_slot
* sizeof (gpointer
))), mono_arch_regname (dreg
));
2451 * arch_emit_gsharedvt_arg_trampoline:
2453 * Emit code for a gsharedvt arg trampoline. OFFSET is the offset of the first of
2454 * two GOT slots which contain the argument, and the code to jump to.
2455 * TRAMP_SIZE is set to the size of the emitted trampoline.
2456 * These kinds of trampolines cannot be enumerated statically, since there could
2457 * be one trampoline per method instantiation, so we emit the same code for all
2458 * trampolines, and parameterize them using two GOT slots.
2461 arch_emit_gsharedvt_arg_trampoline (MonoAotCompile
*acfg
, int offset
, int *tramp_size
)
2463 #if defined(TARGET_X86)
2467 /* Similar to the PPC code above */
2469 g_assert (MONO_ARCH_RGCTX_REG
!= X86_ECX
);
2472 /* Load mscorlib got address */
2473 x86_mov_reg_membase (code
, X86_ECX
, MONO_ARCH_GOT_REG
, sizeof (gpointer
), 4);
2475 x86_mov_reg_membase (code
, X86_EAX
, X86_ECX
, offset
* sizeof (gpointer
), 4);
2476 /* Branch to the target address */
2477 x86_jump_membase (code
, X86_ECX
, (offset
+ 1) * sizeof (gpointer
));
2479 emit_bytes (acfg
, buf
, code
- buf
);
2482 g_assert (code
- buf
== *tramp_size
);
2483 #elif defined(TARGET_ARM)
2487 /* The same as mono_arch_get_gsharedvt_arg_trampoline (), but for AOT */
2488 /* Similar to arch_emit_specific_trampoline () */
2491 ARM_PUSH (code
, (1 << ARMREG_R0
) | (1 << ARMREG_R1
) | (1 << ARMREG_R2
) | (1 << ARMREG_R3
));
2492 ARM_LDR_IMM (code
, ARMREG_R1
, ARMREG_PC
, 8);
2493 /* Load the arg value from the GOT */
2494 ARM_LDR_REG_REG (code
, ARMREG_R0
, ARMREG_PC
, ARMREG_R1
);
2495 /* Load the addr from the GOT */
2496 ARM_LDR_REG_REG (code
, ARMREG_R1
, ARMREG_PC
, ARMREG_R1
);
2498 ARM_BX (code
, ARMREG_R1
);
2500 g_assert (code
- buf
== 20);
2503 emit_bytes (acfg
, buf
, code
- buf
);
2504 emit_symbol_diff (acfg
, acfg
->got_symbol
, ".", (offset
* sizeof (gpointer
)) + 4);
2505 #elif defined(TARGET_ARM64)
2506 arm64_emit_gsharedvt_arg_trampoline (acfg
, offset
, tramp_size
);
2507 #elif defined (TARGET_AMD64)
2509 amd64_emit_load_got_slot (acfg
, AMD64_RAX
, offset
);
2510 amd64_emit_load_got_slot (acfg
, MONO_ARCH_IMT_SCRATCH_REG
, offset
+ 1);
2511 g_assert (AMD64_R11
== MONO_ARCH_IMT_SCRATCH_REG
);
2512 fprintf (acfg
->fp
, "jmp *%%r11\n");
2516 g_assert_not_reached ();
2520 /* END OF ARCH SPECIFIC CODE */
2523 mono_get_field_token (MonoClassField
*field
)
2525 MonoClass
*klass
= field
->parent
;
2528 int fcount
= mono_class_get_field_count (klass
);
2529 for (i
= 0; i
< fcount
; ++i
) {
2530 if (field
== &klass
->fields
[i
])
2531 return MONO_TOKEN_FIELD_DEF
| (mono_class_get_first_field_idx (klass
) + 1 + i
);
2534 g_assert_not_reached ();
2539 encode_value (gint32 value
, guint8
*buf
, guint8
**endbuf
)
2543 //printf ("ENCODE: %d 0x%x.\n", value, value);
2546 * Same encoding as the one used in the metadata, extended to handle values
2547 * greater than 0x1fffffff.
2549 if ((value
>= 0) && (value
<= 127))
2551 else if ((value
>= 0) && (value
<= 16383)) {
2552 p
[0] = 0x80 | (value
>> 8);
2553 p
[1] = value
& 0xff;
2555 } else if ((value
>= 0) && (value
<= 0x1fffffff)) {
2556 p
[0] = (value
>> 24) | 0xc0;
2557 p
[1] = (value
>> 16) & 0xff;
2558 p
[2] = (value
>> 8) & 0xff;
2559 p
[3] = value
& 0xff;
2564 p
[1] = (value
>> 24) & 0xff;
2565 p
[2] = (value
>> 16) & 0xff;
2566 p
[3] = (value
>> 8) & 0xff;
2567 p
[4] = value
& 0xff;
2575 stream_init (MonoDynamicStream
*sh
)
2578 sh
->alloc_size
= 4096;
2579 sh
->data
= (char *)g_malloc (4096);
2581 /* So offsets are > 0 */
2587 make_room_in_stream (MonoDynamicStream
*stream
, int size
)
2589 if (size
<= stream
->alloc_size
)
2592 while (stream
->alloc_size
<= size
) {
2593 if (stream
->alloc_size
< 4096)
2594 stream
->alloc_size
= 4096;
2596 stream
->alloc_size
*= 2;
2599 stream
->data
= (char *)g_realloc (stream
->data
, stream
->alloc_size
);
2603 add_stream_data (MonoDynamicStream
*stream
, const char *data
, guint32 len
)
2607 make_room_in_stream (stream
, stream
->index
+ len
);
2608 memcpy (stream
->data
+ stream
->index
, data
, len
);
2609 idx
= stream
->index
;
2610 stream
->index
+= len
;
2617 * Add data to the binary blob inside the aot image. Returns the offset inside the
2618 * blob where the data was stored.
2621 add_to_blob (MonoAotCompile
*acfg
, const guint8
*data
, guint32 data_len
)
2623 g_assert (!acfg
->blob_closed
);
2625 if (acfg
->blob
.alloc_size
== 0)
2626 stream_init (&acfg
->blob
);
2628 return add_stream_data (&acfg
->blob
, (char*)data
, data_len
);
2632 add_to_blob_aligned (MonoAotCompile
*acfg
, const guint8
*data
, guint32 data_len
, guint32 align
)
2637 if (acfg
->blob
.alloc_size
== 0)
2638 stream_init (&acfg
->blob
);
2640 count
= acfg
->blob
.index
% align
;
2642 /* we assume the stream data will be aligned */
2644 add_stream_data (&acfg
->blob
, buf
, 4 - count
);
2646 return add_stream_data (&acfg
->blob
, (char*)data
, data_len
);
2649 /* Emit a table of data into the aot image */
2651 emit_aot_data (MonoAotCompile
*acfg
, MonoAotFileTable table
, const char *symbol
, guint8
*data
, int size
)
2653 if (acfg
->data_outfile
) {
2654 acfg
->table_offsets
[(int)table
] = acfg
->datafile_offset
;
2655 fwrite (data
,1, size
, acfg
->data_outfile
);
2656 acfg
->datafile_offset
+= size
;
2657 // align the data to 8 bytes. Put zeros in the file (so that every build results in consistent output).
2658 int align
= 8 - size
% 8;
2659 acfg
->datafile_offset
+= align
;
2660 guint8 align_buf
[16];
2661 memset (&align_buf
, 0, sizeof (align_buf
));
2662 fwrite (align_buf
, align
, 1, acfg
->data_outfile
);
2663 } else if (acfg
->llvm
) {
2664 mono_llvm_emit_aot_data (symbol
, data
, size
);
2666 emit_section_change (acfg
, RODATA_SECT
, 0);
2667 emit_alignment (acfg
, 8);
2668 emit_label (acfg
, symbol
);
2669 emit_bytes (acfg
, data
, size
);
2674 * emit_offset_table:
2676 * Emit a table of increasing offsets in a compact form using differential encoding.
2677 * There is an index entry for each GROUP_SIZE number of entries. The greater the
2678 * group size, the more compact the table becomes, but the slower it becomes to compute
2679 * a given entry. Returns the size of the table.
2682 emit_offset_table (MonoAotCompile
*acfg
, const char *symbol
, MonoAotFileTable table
, int noffsets
, int group_size
, gint32
*offsets
)
2684 gint32 current_offset
;
2685 int i
, buf_size
, ngroups
, index_entry_size
;
2687 guint8
*data_p
, *data_buf
;
2688 guint32
*index_offsets
;
2690 ngroups
= (noffsets
+ (group_size
- 1)) / group_size
;
2692 index_offsets
= g_new0 (guint32
, ngroups
);
2694 buf_size
= noffsets
* 4;
2695 p
= buf
= (guint8
*)g_malloc0 (buf_size
);
2698 for (i
= 0; i
< noffsets
; ++i
) {
2699 //printf ("D: %d -> %d\n", i, offsets [i]);
2700 if ((i
% group_size
) == 0) {
2701 index_offsets
[i
/ group_size
] = p
- buf
;
2702 /* Emit the full value for these entries */
2703 encode_value (offsets
[i
], p
, &p
);
2705 /* The offsets are allowed to be non-increasing */
2706 //g_assert (offsets [i] >= current_offset);
2707 encode_value (offsets
[i
] - current_offset
, p
, &p
);
2709 current_offset
= offsets
[i
];
2714 if (ngroups
&& index_offsets
[ngroups
- 1] < 65000)
2715 index_entry_size
= 2;
2717 index_entry_size
= 4;
2719 buf_size
= (data_p
- data_buf
) + (ngroups
* 4) + 16;
2720 p
= buf
= (guint8
*)g_malloc0 (buf_size
);
2722 /* Emit the header */
2723 encode_int (noffsets
, p
, &p
);
2724 encode_int (group_size
, p
, &p
);
2725 encode_int (ngroups
, p
, &p
);
2726 encode_int (index_entry_size
, p
, &p
);
2728 /* Emit the index */
2729 for (i
= 0; i
< ngroups
; ++i
) {
2730 if (index_entry_size
== 2)
2731 encode_int16 (index_offsets
[i
], p
, &p
);
2733 encode_int (index_offsets
[i
], p
, &p
);
2736 memcpy (p
, data_buf
, data_p
- data_buf
);
2737 p
+= data_p
- data_buf
;
2739 g_assert (p
- buf
<= buf_size
);
2741 emit_aot_data (acfg
, table
, symbol
, buf
, p
- buf
);
2746 return (int)(p
- buf
);
2750 get_image_index (MonoAotCompile
*cfg
, MonoImage
*image
)
2754 index
= GPOINTER_TO_UINT (g_hash_table_lookup (cfg
->image_hash
, image
));
2758 index
= g_hash_table_size (cfg
->image_hash
);
2759 g_hash_table_insert (cfg
->image_hash
, image
, GUINT_TO_POINTER (index
+ 1));
2760 g_ptr_array_add (cfg
->image_table
, image
);
2766 find_typespec_for_class (MonoAotCompile
*acfg
, MonoClass
*klass
)
2769 int len
= acfg
->image
->tables
[MONO_TABLE_TYPESPEC
].rows
;
2771 /* FIXME: Search referenced images as well */
2772 if (!acfg
->typespec_classes
) {
2773 acfg
->typespec_classes
= g_hash_table_new (NULL
, NULL
);
2774 for (i
= 0; i
< len
; i
++) {
2776 int typespec
= MONO_TOKEN_TYPE_SPEC
| (i
+ 1);
2777 MonoClass
*klass_key
= mono_class_get_and_inflate_typespec_checked (acfg
->image
, typespec
, NULL
, &error
);
2778 if (!is_ok (&error
)) {
2779 mono_error_cleanup (&error
);
2782 g_hash_table_insert (acfg
->typespec_classes
, klass_key
, GINT_TO_POINTER (typespec
));
2785 return GPOINTER_TO_INT (g_hash_table_lookup (acfg
->typespec_classes
, klass
));
2789 encode_method_ref (MonoAotCompile
*acfg
, MonoMethod
*method
, guint8
*buf
, guint8
**endbuf
);
2792 encode_klass_ref (MonoAotCompile
*acfg
, MonoClass
*klass
, guint8
*buf
, guint8
**endbuf
);
2795 encode_ginst (MonoAotCompile
*acfg
, MonoGenericInst
*inst
, guint8
*buf
, guint8
**endbuf
);
2798 encode_type (MonoAotCompile
*acfg
, MonoType
*t
, guint8
*buf
, guint8
**endbuf
);
2801 encode_klass_ref_inner (MonoAotCompile
*acfg
, MonoClass
*klass
, guint8
*buf
, guint8
**endbuf
)
2806 * The encoding begins with one of the MONO_AOT_TYPEREF values, followed by additional
2810 if (mono_class_is_ginst (klass
)) {
2812 g_assert (klass
->type_token
);
2814 /* Find a typespec for a class if possible */
2815 token
= find_typespec_for_class (acfg
, klass
);
2817 encode_value (MONO_AOT_TYPEREF_TYPESPEC_TOKEN
, p
, &p
);
2818 encode_value (token
, p
, &p
);
2820 MonoClass
*gclass
= mono_class_get_generic_class (klass
)->container_class
;
2821 MonoGenericInst
*inst
= mono_class_get_generic_class (klass
)->context
.class_inst
;
2822 static int count
= 0;
2825 encode_value (MONO_AOT_TYPEREF_GINST
, p
, &p
);
2826 encode_klass_ref (acfg
, gclass
, p
, &p
);
2827 encode_ginst (acfg
, inst
, p
, &p
);
2831 } else if (klass
->type_token
) {
2832 int iindex
= get_image_index (acfg
, klass
->image
);
2834 g_assert (mono_metadata_token_code (klass
->type_token
) == MONO_TOKEN_TYPE_DEF
);
2836 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX
, p
, &p
);
2837 encode_value (klass
->type_token
- MONO_TOKEN_TYPE_DEF
, p
, &p
);
2839 encode_value (MONO_AOT_TYPEREF_TYPEDEF_INDEX_IMAGE
, p
, &p
);
2840 encode_value (klass
->type_token
- MONO_TOKEN_TYPE_DEF
, p
, &p
);
2841 encode_value (get_image_index (acfg
, klass
->image
), p
, &p
);
2843 } else if ((klass
->byval_arg
.type
== MONO_TYPE_VAR
) || (klass
->byval_arg
.type
== MONO_TYPE_MVAR
)) {
2844 MonoGenericContainer
*container
= mono_type_get_generic_param_owner (&klass
->byval_arg
);
2845 MonoGenericParam
*par
= klass
->byval_arg
.data
.generic_param
;
2847 encode_value (MONO_AOT_TYPEREF_VAR
, p
, &p
);
2849 encode_value (par
->gshared_constraint
? 1 : 0, p
, &p
);
2850 if (par
->gshared_constraint
) {
2851 MonoGSharedGenericParam
*gpar
= (MonoGSharedGenericParam
*)par
;
2852 encode_type (acfg
, par
->gshared_constraint
, p
, &p
);
2853 encode_klass_ref (acfg
, mono_class_from_generic_parameter (gpar
->parent
, NULL
, klass
->byval_arg
.type
== MONO_TYPE_MVAR
), p
, &p
);
2855 encode_value (klass
->byval_arg
.type
, p
, &p
);
2856 encode_value (mono_type_get_generic_param_num (&klass
->byval_arg
), p
, &p
);
2858 encode_value (container
->is_anonymous
? 0 : 1, p
, &p
);
2860 if (!container
->is_anonymous
) {
2861 encode_value (container
->is_method
, p
, &p
);
2862 if (container
->is_method
)
2863 encode_method_ref (acfg
, container
->owner
.method
, p
, &p
);
2865 encode_klass_ref (acfg
, container
->owner
.klass
, p
, &p
);
2868 } else if (klass
->byval_arg
.type
== MONO_TYPE_PTR
) {
2869 encode_value (MONO_AOT_TYPEREF_PTR
, p
, &p
);
2870 encode_type (acfg
, &klass
->byval_arg
, p
, &p
);
2873 g_assert (klass
->rank
> 0);
2874 encode_value (MONO_AOT_TYPEREF_ARRAY
, p
, &p
);
2875 encode_value (klass
->rank
, p
, &p
);
2876 encode_klass_ref (acfg
, klass
->element_class
, p
, &p
);
2884 * Encode a reference to KLASS. We use our home-grown encoding instead of the
2885 * standard metadata encoding.
2888 encode_klass_ref (MonoAotCompile
*acfg
, MonoClass
*klass
, guint8
*buf
, guint8
**endbuf
)
2890 gboolean shared
= FALSE
;
2893 * The encoding of generic instances is large so emit them only once.
2895 if (mono_class_is_ginst (klass
)) {
2897 g_assert (klass
->type_token
);
2899 /* Find a typespec for a class if possible */
2900 token
= find_typespec_for_class (acfg
, klass
);
2903 } else if ((klass
->byval_arg
.type
== MONO_TYPE_VAR
) || (klass
->byval_arg
.type
== MONO_TYPE_MVAR
)) {
2908 guint offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->klass_blob_hash
, klass
));
2912 buf2
= (guint8
*)g_malloc (1024);
2915 encode_klass_ref_inner (acfg
, klass
, p
, &p
);
2916 g_assert (p
- buf2
< 1024);
2918 offset
= add_to_blob (acfg
, buf2
, p
- buf2
);
2921 g_hash_table_insert (acfg
->klass_blob_hash
, klass
, GUINT_TO_POINTER (offset
+ 1));
2927 encode_value (MONO_AOT_TYPEREF_BLOB_INDEX
, p
, &p
);
2928 encode_value (offset
, p
, &p
);
2933 encode_klass_ref_inner (acfg
, klass
, buf
, endbuf
);
2937 encode_field_info (MonoAotCompile
*cfg
, MonoClassField
*field
, guint8
*buf
, guint8
**endbuf
)
2939 guint32 token
= mono_get_field_token (field
);
2942 encode_klass_ref (cfg
, field
->parent
, p
, &p
);
2943 g_assert (mono_metadata_token_code (token
) == MONO_TOKEN_FIELD_DEF
);
2944 encode_value (token
- MONO_TOKEN_FIELD_DEF
, p
, &p
);
2949 encode_ginst (MonoAotCompile
*acfg
, MonoGenericInst
*inst
, guint8
*buf
, guint8
**endbuf
)
2954 encode_value (inst
->type_argc
, p
, &p
);
2955 for (i
= 0; i
< inst
->type_argc
; ++i
)
2956 encode_klass_ref (acfg
, mono_class_from_mono_type (inst
->type_argv
[i
]), p
, &p
);
2961 encode_generic_context (MonoAotCompile
*acfg
, MonoGenericContext
*context
, guint8
*buf
, guint8
**endbuf
)
2964 MonoGenericInst
*inst
;
2966 inst
= context
->class_inst
;
2968 g_assert (inst
->type_argc
);
2969 encode_ginst (acfg
, inst
, p
, &p
);
2971 encode_value (0, p
, &p
);
2973 inst
= context
->method_inst
;
2975 g_assert (inst
->type_argc
);
2976 encode_ginst (acfg
, inst
, p
, &p
);
2978 encode_value (0, p
, &p
);
2984 encode_type (MonoAotCompile
*acfg
, MonoType
*t
, guint8
*buf
, guint8
**endbuf
)
2988 g_assert (t
->num_mods
== 0);
2989 /* t->attrs can be ignored */
2990 //g_assert (t->attrs == 0);
2993 *p
= MONO_TYPE_PINNED
;
2997 *p
= MONO_TYPE_BYREF
;
3005 case MONO_TYPE_VOID
:
3006 case MONO_TYPE_BOOLEAN
:
3007 case MONO_TYPE_CHAR
:
3020 case MONO_TYPE_STRING
:
3021 case MONO_TYPE_OBJECT
:
3022 case MONO_TYPE_TYPEDBYREF
:
3024 case MONO_TYPE_VALUETYPE
:
3025 case MONO_TYPE_CLASS
:
3026 encode_klass_ref (acfg
, mono_class_from_mono_type (t
), p
, &p
);
3028 case MONO_TYPE_SZARRAY
:
3029 encode_klass_ref (acfg
, t
->data
.klass
, p
, &p
);
3032 encode_type (acfg
, t
->data
.type
, p
, &p
);
3034 case MONO_TYPE_GENERICINST
: {
3035 MonoClass
*gclass
= t
->data
.generic_class
->container_class
;
3036 MonoGenericInst
*inst
= t
->data
.generic_class
->context
.class_inst
;
3038 encode_klass_ref (acfg
, gclass
, p
, &p
);
3039 encode_ginst (acfg
, inst
, p
, &p
);
3042 case MONO_TYPE_ARRAY
: {
3043 MonoArrayType
*array
= t
->data
.array
;
3046 encode_klass_ref (acfg
, array
->eklass
, p
, &p
);
3047 encode_value (array
->rank
, p
, &p
);
3048 encode_value (array
->numsizes
, p
, &p
);
3049 for (i
= 0; i
< array
->numsizes
; ++i
)
3050 encode_value (array
->sizes
[i
], p
, &p
);
3051 encode_value (array
->numlobounds
, p
, &p
);
3052 for (i
= 0; i
< array
->numlobounds
; ++i
)
3053 encode_value (array
->lobounds
[i
], p
, &p
);
3057 case MONO_TYPE_MVAR
:
3058 encode_klass_ref (acfg
, mono_class_from_mono_type (t
), p
, &p
);
3061 g_assert_not_reached ();
3068 encode_signature (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
, guint8
*buf
, guint8
**endbuf
)
3074 /* Similar to the metadata encoding */
3075 if (sig
->generic_param_count
)
3079 if (sig
->explicit_this
)
3081 flags
|= (sig
->call_convention
& 0x0F);
3085 if (sig
->generic_param_count
)
3086 encode_value (sig
->generic_param_count
, p
, &p
);
3087 encode_value (sig
->param_count
, p
, &p
);
3089 encode_type (acfg
, sig
->ret
, p
, &p
);
3090 for (i
= 0; i
< sig
->param_count
; ++i
) {
3091 if (sig
->sentinelpos
== i
) {
3092 *p
= MONO_TYPE_SENTINEL
;
3095 encode_type (acfg
, sig
->params
[i
], p
, &p
);
3101 #define MAX_IMAGE_INDEX 250
3104 encode_method_ref (MonoAotCompile
*acfg
, MonoMethod
*method
, guint8
*buf
, guint8
**endbuf
)
3106 guint32 image_index
= get_image_index (acfg
, method
->klass
->image
);
3107 guint32 token
= method
->token
;
3108 MonoJumpInfoToken
*ji
;
3112 * The encoding for most methods is as follows:
3113 * - image index encoded as a leb128
3114 * - token index encoded as a leb128
3115 * Values of image index >= MONO_AOT_METHODREF_MIN are used to mark additional
3116 * types of method encodings.
3119 /* Mark methods which can't use aot trampolines because they need the further
3120 * processing in mono_magic_trampoline () which requires a MonoMethod*.
3122 if ((method
->is_generic
&& (method
->flags
& METHOD_ATTRIBUTE_VIRTUAL
)) ||
3123 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
))
3124 encode_value ((MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE
<< 24), p
, &p
);
3126 if (method
->wrapper_type
) {
3127 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
3129 encode_value ((MONO_AOT_METHODREF_WRAPPER
<< 24), p
, &p
);
3131 encode_value (method
->wrapper_type
, p
, &p
);
3133 switch (method
->wrapper_type
) {
3134 case MONO_WRAPPER_REMOTING_INVOKE
:
3135 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
3136 case MONO_WRAPPER_XDOMAIN_INVOKE
: {
3139 m
= mono_marshal_method_from_wrapper (method
);
3141 encode_method_ref (acfg
, m
, p
, &p
);
3144 case MONO_WRAPPER_PROXY_ISINST
:
3145 case MONO_WRAPPER_LDFLD
:
3146 case MONO_WRAPPER_LDFLDA
:
3147 case MONO_WRAPPER_STFLD
:
3148 case MONO_WRAPPER_ISINST
: {
3150 encode_klass_ref (acfg
, info
->d
.proxy
.klass
, p
, &p
);
3153 case MONO_WRAPPER_ALLOC
: {
3154 /* The GC name is saved once in MonoAotFileInfo */
3155 g_assert (info
->d
.alloc
.alloc_type
!= -1);
3156 encode_value (info
->d
.alloc
.alloc_type
, p
, &p
);
3159 case MONO_WRAPPER_WRITE_BARRIER
: {
3163 case MONO_WRAPPER_STELEMREF
: {
3165 encode_value (info
->subtype
, p
, &p
);
3166 if (info
->subtype
== WRAPPER_SUBTYPE_VIRTUAL_STELEMREF
)
3167 encode_value (info
->d
.virtual_stelemref
.kind
, p
, &p
);
3170 case MONO_WRAPPER_UNKNOWN
: {
3172 encode_value (info
->subtype
, p
, &p
);
3173 if (info
->subtype
== WRAPPER_SUBTYPE_PTR_TO_STRUCTURE
||
3174 info
->subtype
== WRAPPER_SUBTYPE_STRUCTURE_TO_PTR
)
3175 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3176 else if (info
->subtype
== WRAPPER_SUBTYPE_SYNCHRONIZED_INNER
)
3177 encode_method_ref (acfg
, info
->d
.synchronized_inner
.method
, p
, &p
);
3178 else if (info
->subtype
== WRAPPER_SUBTYPE_ARRAY_ACCESSOR
)
3179 encode_method_ref (acfg
, info
->d
.array_accessor
.method
, p
, &p
);
3180 else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
)
3181 encode_signature (acfg
, info
->d
.gsharedvt
.sig
, p
, &p
);
3182 else if (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
)
3183 encode_signature (acfg
, info
->d
.gsharedvt
.sig
, p
, &p
);
3186 case MONO_WRAPPER_MANAGED_TO_NATIVE
: {
3188 encode_value (info
->subtype
, p
, &p
);
3189 if (info
->subtype
== WRAPPER_SUBTYPE_ICALL_WRAPPER
) {
3190 strcpy ((char*)p
, method
->name
);
3191 p
+= strlen (method
->name
) + 1;
3192 } else if (info
->subtype
== WRAPPER_SUBTYPE_NATIVE_FUNC_AOT
) {
3193 encode_method_ref (acfg
, info
->d
.managed_to_native
.method
, p
, &p
);
3195 g_assert (info
->subtype
== WRAPPER_SUBTYPE_NONE
|| info
->subtype
== WRAPPER_SUBTYPE_PINVOKE
);
3196 encode_method_ref (acfg
, info
->d
.managed_to_native
.method
, p
, &p
);
3200 case MONO_WRAPPER_SYNCHRONIZED
: {
3203 m
= mono_marshal_method_from_wrapper (method
);
3205 g_assert (m
!= method
);
3206 encode_method_ref (acfg
, m
, p
, &p
);
3209 case MONO_WRAPPER_MANAGED_TO_MANAGED
: {
3211 encode_value (info
->subtype
, p
, &p
);
3213 if (info
->subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
) {
3214 encode_value (info
->d
.element_addr
.rank
, p
, &p
);
3215 encode_value (info
->d
.element_addr
.elem_size
, p
, &p
);
3216 } else if (info
->subtype
== WRAPPER_SUBTYPE_STRING_CTOR
) {
3217 encode_method_ref (acfg
, info
->d
.string_ctor
.method
, p
, &p
);
3219 g_assert_not_reached ();
3223 case MONO_WRAPPER_CASTCLASS
: {
3225 encode_value (info
->subtype
, p
, &p
);
3228 case MONO_WRAPPER_RUNTIME_INVOKE
: {
3230 encode_value (info
->subtype
, p
, &p
);
3231 if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_DIRECT
|| info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_VIRTUAL
)
3232 encode_method_ref (acfg
, info
->d
.runtime_invoke
.method
, p
, &p
);
3233 else if (info
->subtype
== WRAPPER_SUBTYPE_RUNTIME_INVOKE_NORMAL
)
3234 encode_signature (acfg
, info
->d
.runtime_invoke
.sig
, p
, &p
);
3237 case MONO_WRAPPER_DELEGATE_INVOKE
:
3238 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
3239 case MONO_WRAPPER_DELEGATE_END_INVOKE
: {
3240 if (method
->is_inflated
) {
3241 /* These wrappers are identified by their class */
3242 encode_value (1, p
, &p
);
3243 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3245 MonoMethodSignature
*sig
= mono_method_signature (method
);
3246 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
3248 encode_value (0, p
, &p
);
3249 if (method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
3250 encode_value (info
? info
->subtype
: 0, p
, &p
);
3251 encode_signature (acfg
, sig
, p
, &p
);
3255 case MONO_WRAPPER_NATIVE_TO_MANAGED
: {
3257 encode_method_ref (acfg
, info
->d
.native_to_managed
.method
, p
, &p
);
3258 encode_klass_ref (acfg
, info
->d
.native_to_managed
.klass
, p
, &p
);
3262 g_assert_not_reached ();
3264 } else if (mono_method_signature (method
)->is_inflated
) {
3266 * This is a generic method, find the original token which referenced it and
3268 * Obtain the token from information recorded by the JIT.
3270 ji
= (MonoJumpInfoToken
*)g_hash_table_lookup (acfg
->token_info_hash
, method
);
3272 image_index
= get_image_index (acfg
, ji
->image
);
3273 g_assert (image_index
< MAX_IMAGE_INDEX
);
3276 encode_value ((MONO_AOT_METHODREF_METHODSPEC
<< 24), p
, &p
);
3277 encode_value (image_index
, p
, &p
);
3278 encode_value (token
, p
, &p
);
3280 MonoMethod
*declaring
;
3281 MonoGenericContext
*context
= mono_method_get_context (method
);
3283 g_assert (method
->is_inflated
);
3284 declaring
= ((MonoMethodInflated
*)method
)->declaring
;
3287 * This might be a non-generic method of a generic instance, which
3288 * doesn't have a token since the reference is generated by the JIT
3289 * like Nullable:Box/Unbox, or by generic sharing.
3291 encode_value ((MONO_AOT_METHODREF_GINST
<< 24), p
, &p
);
3292 /* Encode the klass */
3293 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3294 /* Encode the method */
3295 image_index
= get_image_index (acfg
, method
->klass
->image
);
3296 g_assert (image_index
< MAX_IMAGE_INDEX
);
3297 g_assert (declaring
->token
);
3298 token
= declaring
->token
;
3299 g_assert (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
);
3300 encode_value (image_index
, p
, &p
);
3301 encode_value (token
, p
, &p
);
3302 encode_generic_context (acfg
, context
, p
, &p
);
3304 } else if (token
== 0) {
3305 /* This might be a method of a constructed type like int[,].Set */
3306 /* Obtain the token from information recorded by the JIT */
3307 ji
= (MonoJumpInfoToken
*)g_hash_table_lookup (acfg
->token_info_hash
, method
);
3309 image_index
= get_image_index (acfg
, ji
->image
);
3310 g_assert (image_index
< MAX_IMAGE_INDEX
);
3313 encode_value ((MONO_AOT_METHODREF_METHODSPEC
<< 24), p
, &p
);
3314 encode_value (image_index
, p
, &p
);
3315 encode_value (token
, p
, &p
);
3318 g_assert (method
->klass
->rank
);
3320 /* Encode directly */
3321 encode_value ((MONO_AOT_METHODREF_ARRAY
<< 24), p
, &p
);
3322 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
3323 if (!strcmp (method
->name
, ".ctor") && mono_method_signature (method
)->param_count
== method
->klass
->rank
)
3324 encode_value (0, p
, &p
);
3325 else if (!strcmp (method
->name
, ".ctor") && mono_method_signature (method
)->param_count
== method
->klass
->rank
* 2)
3326 encode_value (1, p
, &p
);
3327 else if (!strcmp (method
->name
, "Get"))
3328 encode_value (2, p
, &p
);
3329 else if (!strcmp (method
->name
, "Address"))
3330 encode_value (3, p
, &p
);
3331 else if (!strcmp (method
->name
, "Set"))
3332 encode_value (4, p
, &p
);
3334 g_assert_not_reached ();
3337 g_assert (mono_metadata_token_table (token
) == MONO_TABLE_METHOD
);
3339 if (image_index
>= MONO_AOT_METHODREF_MIN
) {
3340 encode_value ((MONO_AOT_METHODREF_LARGE_IMAGE_INDEX
<< 24), p
, &p
);
3341 encode_value (image_index
, p
, &p
);
3342 encode_value (mono_metadata_token_index (token
), p
, &p
);
3344 encode_value ((image_index
<< 24) | mono_metadata_token_index (token
), p
, &p
);
3351 compare_patches (gconstpointer a
, gconstpointer b
)
3355 i
= (*(MonoJumpInfo
**)a
)->ip
.i
;
3356 j
= (*(MonoJumpInfo
**)b
)->ip
.i
;
3367 static G_GNUC_UNUSED
char*
3368 patch_to_string (MonoJumpInfo
*patch_info
)
3372 str
= g_string_new ("");
3374 g_string_append_printf (str
, "%s(", get_patch_name (patch_info
->type
));
3376 switch (patch_info
->type
) {
3377 case MONO_PATCH_INFO_VTABLE
:
3378 mono_type_get_desc (str
, &patch_info
->data
.klass
->byval_arg
, TRUE
);
3383 g_string_append_printf (str
, ")");
3384 return g_string_free (str
, FALSE
);
3390 * Return whenever PATCH_INFO refers to a direct call, and thus requires a
3393 static inline gboolean
3394 is_plt_patch (MonoJumpInfo
*patch_info
)
3396 switch (patch_info
->type
) {
3397 case MONO_PATCH_INFO_METHOD
:
3398 case MONO_PATCH_INFO_INTERNAL_METHOD
:
3399 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
3400 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
3401 case MONO_PATCH_INFO_RGCTX_FETCH
:
3411 * Return the symbol identifying the plt entry PLT_OFFSET.
3414 get_plt_symbol (MonoAotCompile
*acfg
, int plt_offset
, MonoJumpInfo
*patch_info
)
3418 * The Apple linker reorganizes object files, so it doesn't like branches to local
3419 * labels, since those have no relocations.
3421 return g_strdup_printf ("%sp_%d", acfg
->llvm_label_prefix
, plt_offset
);
3423 return g_strdup_printf ("%sp_%d", acfg
->temp_prefix
, plt_offset
);
3430 * Return a PLT entry which belongs to the method identified by PATCH_INFO.
3432 static MonoPltEntry
*
3433 get_plt_entry (MonoAotCompile
*acfg
, MonoJumpInfo
*patch_info
)
3436 gboolean synchronized
= FALSE
;
3437 static int synchronized_symbol_idx
;
3439 if (!is_plt_patch (patch_info
))
3442 if (!acfg
->patch_to_plt_entry
[patch_info
->type
])
3443 acfg
->patch_to_plt_entry
[patch_info
->type
] = g_hash_table_new (mono_patch_info_hash
, mono_patch_info_equal
);
3444 res
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->patch_to_plt_entry
[patch_info
->type
], patch_info
);
3446 if (!acfg
->llvm
&& patch_info
->type
== MONO_PATCH_INFO_METHOD
&& (patch_info
->data
.method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
)) {
3448 * Allocate a separate PLT slot for each such patch, since some plt
3449 * entries will refer to the method itself, and some will refer to the
3453 synchronized
= TRUE
;
3457 MonoJumpInfo
*new_ji
;
3459 new_ji
= mono_patch_info_dup_mp (acfg
->mempool
, patch_info
);
3461 res
= (MonoPltEntry
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoPltEntry
));
3462 res
->plt_offset
= acfg
->plt_offset
;
3464 res
->symbol
= get_plt_symbol (acfg
, res
->plt_offset
, patch_info
);
3465 if (acfg
->aot_opts
.write_symbols
)
3466 res
->debug_sym
= get_plt_entry_debug_sym (acfg
, res
->ji
, acfg
->plt_entry_debug_sym_cache
);
3468 /* Avoid duplicate symbols because we don't cache */
3469 res
->symbol
= g_strdup_printf ("%s_%d", res
->symbol
, synchronized_symbol_idx
);
3471 res
->debug_sym
= g_strdup_printf ("%s_%d", res
->debug_sym
, synchronized_symbol_idx
);
3472 synchronized_symbol_idx
++;
3475 res
->llvm_symbol
= g_strdup_printf ("%s_%s_llvm", res
->symbol
, res
->debug_sym
);
3477 res
->llvm_symbol
= g_strdup_printf ("%s_llvm", res
->symbol
);
3479 g_hash_table_insert (acfg
->patch_to_plt_entry
[new_ji
->type
], new_ji
, res
);
3481 g_hash_table_insert (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (res
->plt_offset
), res
);
3483 //g_assert (mono_patch_info_equal (patch_info, new_ji));
3484 //mono_print_ji (patch_info); printf ("\n");
3485 //g_hash_table_print_stats (acfg->patch_to_plt_entry);
3487 acfg
->plt_offset
++;
3496 * Returns the offset of the GOT slot where the runtime object resulting from resolving
3497 * JI could be found if it exists, otherwise allocates a new one.
3500 get_got_offset (MonoAotCompile
*acfg
, gboolean llvm
, MonoJumpInfo
*ji
)
3503 GotInfo
*info
= llvm
? &acfg
->llvm_got_info
: &acfg
->got_info
;
3505 got_offset
= GPOINTER_TO_UINT (g_hash_table_lookup (info
->patch_to_got_offset_by_type
[ji
->type
], ji
));
3507 return got_offset
- 1;
3510 got_offset
= acfg
->llvm_got_offset
;
3511 acfg
->llvm_got_offset
++;
3513 got_offset
= acfg
->got_offset
;
3514 acfg
->got_offset
++;
3517 acfg
->stats
.got_slots
++;
3518 acfg
->stats
.got_slot_types
[ji
->type
] ++;
3520 g_hash_table_insert (info
->patch_to_got_offset
, ji
, GUINT_TO_POINTER (got_offset
+ 1));
3521 g_hash_table_insert (info
->patch_to_got_offset_by_type
[ji
->type
], ji
, GUINT_TO_POINTER (got_offset
+ 1));
3522 g_ptr_array_add (info
->got_patches
, ji
);
3527 /* Add a method to the list of methods which need to be emitted */
3529 add_method_with_index (MonoAotCompile
*acfg
, MonoMethod
*method
, int index
, gboolean extra
)
3532 if (!g_hash_table_lookup (acfg
->method_indexes
, method
)) {
3533 g_ptr_array_add (acfg
->methods
, method
);
3534 g_hash_table_insert (acfg
->method_indexes
, method
, GUINT_TO_POINTER (index
+ 1));
3535 acfg
->nmethods
= acfg
->methods
->len
+ 1;
3538 if (method
->wrapper_type
|| extra
)
3539 g_ptr_array_add (acfg
->extra_methods
, method
);
3543 prefer_gsharedvt_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
3545 /* One instantiation with valuetypes is generated for each async method */
3546 if (method
->klass
->image
== mono_defaults
.corlib
&& (!strcmp (method
->klass
->name
, "AsyncMethodBuilderCore") || !strcmp (method
->klass
->name
, "AsyncVoidMethodBuilder")))
3553 get_method_index (MonoAotCompile
*acfg
, MonoMethod
*method
)
3555 int index
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_indexes
, method
));
3563 add_method_full (MonoAotCompile
*acfg
, MonoMethod
*method
, gboolean extra
, int depth
)
3567 index
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_indexes
, method
));
3571 index
= acfg
->method_index
;
3572 add_method_with_index (acfg
, method
, index
, extra
);
3574 g_ptr_array_add (acfg
->method_order
, GUINT_TO_POINTER (index
));
3576 g_hash_table_insert (acfg
->method_depth
, method
, GUINT_TO_POINTER (depth
));
3578 acfg
->method_index
++;
3584 add_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
3586 return add_method_full (acfg
, method
, FALSE
, 0);
3590 add_extra_method_with_depth (MonoAotCompile
*acfg
, MonoMethod
*method
, int depth
)
3592 if (mono_method_is_generic_sharable_full (method
, TRUE
, TRUE
, FALSE
))
3593 method
= mini_get_shared_method (method
);
3594 else if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && prefer_gsharedvt_method (acfg
, method
) && mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, TRUE
))
3595 /* Use the gsharedvt version */
3596 method
= mini_get_shared_method_full (method
, TRUE
, TRUE
);
3598 if (acfg
->aot_opts
.log_generics
)
3599 aot_printf (acfg
, "%*sAdding method %s.\n", depth
, "", mono_method_get_full_name (method
));
3601 add_method_full (acfg
, method
, TRUE
, depth
);
3605 add_extra_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
3607 add_extra_method_with_depth (acfg
, method
, 0);
3611 add_jit_icall_wrapper (gpointer key
, gpointer value
, gpointer user_data
)
3613 MonoAotCompile
*acfg
= (MonoAotCompile
*)user_data
;
3614 MonoJitICallInfo
*callinfo
= (MonoJitICallInfo
*)value
;
3615 MonoMethod
*wrapper
;
3621 name
= g_strdup_printf ("__icall_wrapper_%s", callinfo
->name
);
3622 wrapper
= mono_marshal_get_icall_wrapper (callinfo
->sig
, name
, callinfo
->func
, TRUE
);
3625 add_method (acfg
, wrapper
);
3629 get_runtime_invoke_sig (MonoMethodSignature
*sig
)
3631 MonoMethodBuilder
*mb
;
3634 mb
= mono_mb_new (mono_defaults
.object_class
, "FOO", MONO_WRAPPER_NONE
);
3635 m
= mono_mb_create_method (mb
, sig
, 16);
3636 MonoMethod
*invoke
= mono_marshal_get_runtime_invoke (m
, FALSE
);
3642 get_runtime_invoke (MonoAotCompile
*acfg
, MonoMethod
*method
, gboolean virtual_
)
3644 return mono_marshal_get_runtime_invoke (method
, virtual_
);
3648 can_marshal_struct (MonoClass
*klass
)
3650 MonoClassField
*field
;
3651 gboolean can_marshal
= TRUE
;
3652 gpointer iter
= NULL
;
3653 MonoMarshalType
*info
;
3656 if (mono_class_is_auto_layout (klass
))
3659 info
= mono_marshal_load_type_info (klass
);
3661 /* Only allow a few field types to avoid asserts in the marshalling code */
3662 while ((field
= mono_class_get_fields (klass
, &iter
))) {
3663 if ((field
->type
->attrs
& FIELD_ATTRIBUTE_STATIC
))
3666 switch (field
->type
->type
) {
3671 case MONO_TYPE_BOOLEAN
:
3674 case MONO_TYPE_CHAR
:
3682 case MONO_TYPE_STRING
:
3684 case MONO_TYPE_VALUETYPE
:
3685 if (!mono_class_from_mono_type (field
->type
)->enumtype
&& !can_marshal_struct (mono_class_from_mono_type (field
->type
)))
3686 can_marshal
= FALSE
;
3688 case MONO_TYPE_SZARRAY
: {
3689 gboolean has_mspec
= FALSE
;
3692 for (i
= 0; i
< info
->num_fields
; ++i
) {
3693 if (info
->fields
[i
].field
== field
&& info
->fields
[i
].mspec
)
3698 can_marshal
= FALSE
;
3702 can_marshal
= FALSE
;
3708 /* Its hard to compute whenever these can be marshalled or not */
3709 if (!strcmp (klass
->name_space
, "System.Net.NetworkInformation.MacOsStructs") && strcmp (klass
->name
, "sockaddr_dl"))
3716 create_gsharedvt_inst (MonoAotCompile
*acfg
, MonoMethod
*method
, MonoGenericContext
*ctx
)
3718 /* Create a vtype instantiation */
3719 MonoGenericContext shared_context
;
3721 MonoGenericInst
*inst
;
3722 MonoGenericContainer
*container
;
3723 MonoClass
**constraints
;
3726 memset (ctx
, 0, sizeof (MonoGenericContext
));
3728 if (mono_class_is_gtd (method
->klass
)) {
3729 shared_context
= mono_class_get_generic_container (method
->klass
)->context
;
3730 inst
= shared_context
.class_inst
;
3732 args
= g_new0 (MonoType
*, inst
->type_argc
);
3733 for (i
= 0; i
< inst
->type_argc
; ++i
) {
3734 args
[i
] = &mono_defaults
.int_class
->byval_arg
;
3736 ctx
->class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, args
);
3738 if (method
->is_generic
) {
3739 container
= mono_method_get_generic_container (method
);
3740 shared_context
= container
->context
;
3741 inst
= shared_context
.method_inst
;
3743 args
= g_new0 (MonoType
*, inst
->type_argc
);
3744 for (i
= 0; i
< container
->type_argc
; ++i
) {
3745 MonoGenericParamInfo
*info
= &container
->type_params
[i
].info
;
3746 gboolean ref_only
= FALSE
;
3748 if (info
&& info
->constraints
) {
3749 constraints
= info
->constraints
;
3751 while (*constraints
) {
3752 MonoClass
*cklass
= *constraints
;
3753 if (!(cklass
== mono_defaults
.object_class
|| (cklass
->image
== mono_defaults
.corlib
&& !strcmp (cklass
->name
, "ValueType"))))
3754 /* Inflaring the method with our vtype would not be valid */
3761 args
[i
] = &mono_defaults
.object_class
->byval_arg
;
3763 args
[i
] = &mono_defaults
.int_class
->byval_arg
;
3765 ctx
->method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, args
);
3770 add_wrappers (MonoAotCompile
*acfg
)
3772 MonoMethod
*method
, *m
;
3774 MonoMethodSignature
*sig
, *csig
;
3778 * FIXME: Instead of AOTing all the wrappers, it might be better to redesign them
3779 * so there is only one wrapper of a given type, or inlining their contents into their
3782 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
3785 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
3786 gboolean skip
= FALSE
;
3788 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, &error
);
3789 report_loader_error (acfg
, &error
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (&error
));
3791 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
3792 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
3793 (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
))
3796 /* Skip methods which can not be handled by get_runtime_invoke () */
3797 sig
= mono_method_signature (method
);
3800 if ((sig
->ret
->type
== MONO_TYPE_PTR
) ||
3801 (sig
->ret
->type
== MONO_TYPE_TYPEDBYREF
))
3803 if (mono_class_is_open_constructed_type (sig
->ret
))
3806 for (j
= 0; j
< sig
->param_count
; j
++) {
3807 if (sig
->params
[j
]->type
== MONO_TYPE_TYPEDBYREF
)
3809 if (mono_class_is_open_constructed_type (sig
->params
[j
]))
3813 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
3814 if (!mono_class_is_contextbound (method
->klass
)) {
3815 MonoDynCallInfo
*info
= mono_arch_dyn_call_prepare (sig
);
3816 gboolean has_nullable
= FALSE
;
3818 for (j
= 0; j
< sig
->param_count
; j
++) {
3819 if (sig
->params
[j
]->type
== MONO_TYPE_GENERICINST
&& mono_class_is_nullable (mono_class_from_mono_type (sig
->params
[j
])))
3820 has_nullable
= TRUE
;
3823 if (info
&& !has_nullable
&& !acfg
->aot_opts
.llvm_only
) {
3824 /* Supported by the dynamic runtime-invoke wrapper */
3828 mono_arch_dyn_call_free (info
);
3832 if (acfg
->aot_opts
.llvm_only
)
3833 /* Supported by the gsharedvt based runtime-invoke wrapper */
3837 //printf ("%s\n", mono_method_full_name (method, TRUE));
3838 add_method (acfg
, get_runtime_invoke (acfg
, method
, FALSE
));
3842 if (strcmp (acfg
->image
->assembly
->aname
.name
, "mscorlib") == 0) {
3845 /* Runtime invoke wrappers */
3847 /* void runtime-invoke () [.cctor] */
3848 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
3849 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
3850 add_method (acfg
, get_runtime_invoke_sig (csig
));
3852 /* void runtime-invoke () [Finalize] */
3853 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
3855 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
3856 add_method (acfg
, get_runtime_invoke_sig (csig
));
3858 /* void runtime-invoke (string) [exception ctor] */
3859 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 1);
3861 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
3862 csig
->params
[0] = &mono_defaults
.string_class
->byval_arg
;
3863 add_method (acfg
, get_runtime_invoke_sig (csig
));
3865 /* void runtime-invoke (string, string) [exception ctor] */
3866 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 2);
3868 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
3869 csig
->params
[0] = &mono_defaults
.string_class
->byval_arg
;
3870 csig
->params
[1] = &mono_defaults
.string_class
->byval_arg
;
3871 add_method (acfg
, get_runtime_invoke_sig (csig
));
3873 /* string runtime-invoke () [Exception.ToString ()] */
3874 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 0);
3876 csig
->ret
= &mono_defaults
.string_class
->byval_arg
;
3877 add_method (acfg
, get_runtime_invoke_sig (csig
));
3879 /* void runtime-invoke (string, Exception) [exception ctor] */
3880 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 2);
3882 csig
->ret
= &mono_defaults
.void_class
->byval_arg
;
3883 csig
->params
[0] = &mono_defaults
.string_class
->byval_arg
;
3884 csig
->params
[1] = &mono_defaults
.exception_class
->byval_arg
;
3885 add_method (acfg
, get_runtime_invoke_sig (csig
));
3887 /* Assembly runtime-invoke (string, Assembly, bool) [DoAssemblyResolve] */
3888 csig
= mono_metadata_signature_alloc (mono_defaults
.corlib
, 3);
3890 csig
->ret
= &(mono_class_load_from_name (
3891 mono_defaults
.corlib
, "System.Reflection", "Assembly"))->byval_arg
;
3892 csig
->params
[0] = &mono_defaults
.string_class
->byval_arg
;
3893 csig
->params
[1] = &(mono_class_load_from_name (mono_defaults
.corlib
, "System.Reflection", "Assembly"))->byval_arg
;
3894 csig
->params
[2] = &mono_defaults
.boolean_class
->byval_arg
;
3895 add_method (acfg
, get_runtime_invoke_sig (csig
));
3897 /* runtime-invoke used by finalizers */
3898 add_method (acfg
, get_runtime_invoke (acfg
, mono_class_get_method_from_name_flags (mono_defaults
.object_class
, "Finalize", 0, 0), TRUE
));
3900 /* This is used by mono_runtime_capture_context () */
3901 method
= mono_get_context_capture_method ();
3903 add_method (acfg
, get_runtime_invoke (acfg
, method
, FALSE
));
3905 #ifdef MONO_ARCH_DYN_CALL_SUPPORTED
3906 if (!acfg
->aot_opts
.llvm_only
)
3907 add_method (acfg
, mono_marshal_get_runtime_invoke_dynamic ());
3910 /* These are used by mono_jit_runtime_invoke () to calls gsharedvt out wrappers */
3911 if (acfg
->aot_opts
.llvm_only
) {
3914 /* Create simplified signatures which match the signature used by the gsharedvt out wrappers */
3915 for (variants
= 0; variants
< 4; ++variants
) {
3916 for (i
= 0; i
< 16; ++i
) {
3917 sig
= mini_get_gsharedvt_out_sig_wrapper_signature ((variants
& 1) > 0, (variants
& 2) > 0, i
);
3918 add_extra_method (acfg
, mono_marshal_get_runtime_invoke_for_sig (sig
));
3926 add_method (acfg
, mono_marshal_get_stelemref ());
3928 if (MONO_ARCH_HAVE_TLS_GET
) {
3929 /* Managed Allocators */
3930 nallocators
= mono_gc_get_managed_allocator_types ();
3931 for (i
= 0; i
< nallocators
; ++i
) {
3932 if ((m
= mono_gc_get_managed_allocator_by_type (i
, MANAGED_ALLOCATOR_REGULAR
)))
3933 add_method (acfg
, m
);
3934 if ((m
= mono_gc_get_managed_allocator_by_type (i
, MANAGED_ALLOCATOR_SLOW_PATH
)))
3935 add_method (acfg
, m
);
3939 /* write barriers */
3940 if (mono_gc_is_moving ()) {
3941 add_method (acfg
, mono_gc_get_specific_write_barrier (FALSE
));
3942 add_method (acfg
, mono_gc_get_specific_write_barrier (TRUE
));
3945 /* Stelemref wrappers */
3947 MonoMethod
**wrappers
;
3950 wrappers
= mono_marshal_get_virtual_stelemref_wrappers (&nwrappers
);
3951 for (i
= 0; i
< nwrappers
; ++i
)
3952 add_method (acfg
, wrappers
[i
]);
3956 /* castclass_with_check wrapper */
3957 add_method (acfg
, mono_marshal_get_castclass_with_cache ());
3958 /* isinst_with_check wrapper */
3959 add_method (acfg
, mono_marshal_get_isinst_with_cache ());
3961 /* JIT icall wrappers */
3962 /* FIXME: locking - this is "safe" as full-AOT threads don't mutate the icall hash*/
3963 g_hash_table_foreach (mono_get_jit_icall_info (), add_jit_icall_wrapper
, acfg
);
3967 * remoting-invoke-with-check wrappers are very frequent, so avoid emitting them,
3968 * we use the original method instead at runtime.
3969 * Since full-aot doesn't support remoting, this is not a problem.
3972 /* remoting-invoke wrappers */
3973 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
3975 MonoMethodSignature
*sig
;
3977 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
3978 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, &error
);
3979 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
3981 sig
= mono_method_signature (method
);
3983 if (sig
->hasthis
&& (method
->klass
->marshalbyref
|| method
->klass
== mono_defaults
.object_class
)) {
3984 m
= mono_marshal_get_remoting_invoke_with_check (method
);
3986 add_method (acfg
, m
);
3991 /* delegate-invoke wrappers */
3992 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
) {
3995 MonoCustomAttrInfo
*cattr
;
3997 token
= MONO_TOKEN_TYPE_DEF
| (i
+ 1);
3998 klass
= mono_class_get_checked (acfg
->image
, token
, &error
);
4001 mono_error_cleanup (&error
);
4005 if (!klass
->delegate
|| klass
== mono_defaults
.delegate_class
|| klass
== mono_defaults
.multicastdelegate_class
)
4008 if (!mono_class_is_gtd (klass
)) {
4009 method
= mono_get_delegate_invoke (klass
);
4011 m
= mono_marshal_get_delegate_invoke (method
, NULL
);
4013 add_method (acfg
, m
);
4015 method
= mono_class_get_method_from_name_flags (klass
, "BeginInvoke", -1, 0);
4017 add_method (acfg
, mono_marshal_get_delegate_begin_invoke (method
));
4019 method
= mono_class_get_method_from_name_flags (klass
, "EndInvoke", -1, 0);
4021 add_method (acfg
, mono_marshal_get_delegate_end_invoke (method
));
4023 cattr
= mono_custom_attrs_from_class_checked (klass
, &error
);
4024 if (!is_ok (&error
)) {
4025 mono_error_cleanup (&error
);
4032 for (j
= 0; j
< cattr
->num_attrs
; ++j
)
4033 if (cattr
->attrs
[j
].ctor
&& (!strcmp (cattr
->attrs
[j
].ctor
->klass
->name
, "MonoNativeFunctionWrapperAttribute") || !strcmp (cattr
->attrs
[j
].ctor
->klass
->name
, "UnmanagedFunctionPointerAttribute")))
4035 if (j
< cattr
->num_attrs
) {
4037 MonoMethod
*wrapper
;
4038 MonoMethod
*del_invoke
;
4040 /* Add wrappers needed by mono_ftnptr_to_delegate () */
4041 invoke
= mono_get_delegate_invoke (klass
);
4042 wrapper
= mono_marshal_get_native_func_wrapper_aot (klass
);
4043 del_invoke
= mono_marshal_get_delegate_invoke_internal (invoke
, FALSE
, TRUE
, wrapper
);
4044 add_method (acfg
, wrapper
);
4045 add_method (acfg
, del_invoke
);
4048 } else if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && mono_class_is_gtd (klass
)) {
4050 MonoGenericContext ctx
;
4051 MonoMethod
*inst
, *gshared
;
4054 * Emit gsharedvt versions of the generic delegate-invoke wrappers
4057 method
= mono_get_delegate_invoke (klass
);
4058 create_gsharedvt_inst (acfg
, method
, &ctx
);
4060 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, &error
);
4061 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
4063 m
= mono_marshal_get_delegate_invoke (inst
, NULL
);
4064 g_assert (m
->is_inflated
);
4066 gshared
= mini_get_shared_method_full (m
, FALSE
, TRUE
);
4067 add_extra_method (acfg
, gshared
);
4070 method
= mono_get_delegate_begin_invoke (klass
);
4071 create_gsharedvt_inst (acfg
, method
, &ctx
);
4073 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, &error
);
4074 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
4076 m
= mono_marshal_get_delegate_begin_invoke (inst
);
4077 g_assert (m
->is_inflated
);
4079 gshared
= mini_get_shared_method_full (m
, FALSE
, TRUE
);
4080 add_extra_method (acfg
, gshared
);
4083 method
= mono_get_delegate_end_invoke (klass
);
4084 create_gsharedvt_inst (acfg
, method
, &ctx
);
4086 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, &error
);
4087 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
4089 m
= mono_marshal_get_delegate_end_invoke (inst
);
4090 g_assert (m
->is_inflated
);
4092 gshared
= mini_get_shared_method_full (m
, FALSE
, TRUE
);
4093 add_extra_method (acfg
, gshared
);
4098 /* array access wrappers */
4099 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPESPEC
].rows
; ++i
) {
4103 token
= MONO_TOKEN_TYPE_SPEC
| (i
+ 1);
4104 klass
= mono_class_get_checked (acfg
->image
, token
, &error
);
4107 mono_error_cleanup (&error
);
4111 if (klass
->rank
&& MONO_TYPE_IS_PRIMITIVE (&klass
->element_class
->byval_arg
)) {
4112 MonoMethod
*m
, *wrapper
;
4114 /* Add runtime-invoke wrappers too */
4116 m
= mono_class_get_method_from_name (klass
, "Get", -1);
4118 wrapper
= mono_marshal_get_array_accessor_wrapper (m
);
4119 add_extra_method (acfg
, wrapper
);
4120 if (!acfg
->aot_opts
.llvm_only
)
4121 add_extra_method (acfg
, get_runtime_invoke (acfg
, wrapper
, FALSE
));
4123 m
= mono_class_get_method_from_name (klass
, "Set", -1);
4125 wrapper
= mono_marshal_get_array_accessor_wrapper (m
);
4126 add_extra_method (acfg
, wrapper
);
4127 if (!acfg
->aot_opts
.llvm_only
)
4128 add_extra_method (acfg
, get_runtime_invoke (acfg
, wrapper
, FALSE
));
4132 /* Synchronized wrappers */
4133 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4135 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4136 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, &error
);
4137 report_loader_error (acfg
, &error
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (&error
));
4139 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
) {
4140 if (method
->is_generic
) {
4142 } else if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && mono_class_is_gtd (method
->klass
)) {
4144 MonoGenericContext ctx
;
4145 MonoMethod
*inst
, *gshared
, *m
;
4148 * Create a generic wrapper for a generic instance, and AOT that.
4150 create_gsharedvt_inst (acfg
, method
, &ctx
);
4151 inst
= mono_class_inflate_generic_method_checked (method
, &ctx
, &error
);
4152 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
4153 m
= mono_marshal_get_synchronized_wrapper (inst
);
4154 g_assert (m
->is_inflated
);
4155 gshared
= mini_get_shared_method_full (m
, FALSE
, TRUE
);
4156 add_method (acfg
, gshared
);
4158 add_method (acfg
, mono_marshal_get_synchronized_wrapper (method
));
4163 /* pinvoke wrappers */
4164 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4167 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4169 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, &error
);
4170 report_loader_error (acfg
, &error
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (&error
));
4172 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
4173 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)) {
4174 add_method (acfg
, mono_marshal_get_native_wrapper (method
, TRUE
, TRUE
));
4177 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
4178 if (acfg
->aot_opts
.llvm_only
) {
4179 /* The wrappers have a different signature (hasthis is not set) so need to add this too */
4180 add_gsharedvt_wrappers (acfg
, mono_method_signature (method
), FALSE
, TRUE
);
4185 /* native-to-managed wrappers */
4186 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
4189 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
4190 MonoCustomAttrInfo
*cattr
;
4193 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, &error
);
4194 report_loader_error (acfg
, &error
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (&error
));
4197 * Only generate native-to-managed wrappers for methods which have an
4198 * attribute named MonoPInvokeCallbackAttribute. We search for the attribute by
4199 * name to avoid defining a new assembly to contain it.
4201 cattr
= mono_custom_attrs_from_method_checked (method
, &error
);
4202 if (!is_ok (&error
)) {
4203 char *name
= mono_method_get_full_name (method
);
4204 report_loader_error (acfg
, &error
, "Failed to load custom attributes from method %s due to %s\n", name
, mono_error_get_message (&error
));
4209 for (j
= 0; j
< cattr
->num_attrs
; ++j
)
4210 if (cattr
->attrs
[j
].ctor
&& !strcmp (cattr
->attrs
[j
].ctor
->klass
->name
, "MonoPInvokeCallbackAttribute"))
4212 if (j
< cattr
->num_attrs
) {
4213 MonoCustomAttrEntry
*e
= &cattr
->attrs
[j
];
4214 MonoMethodSignature
*sig
= mono_method_signature (e
->ctor
);
4215 const char *p
= (const char*)e
->data
;
4217 int slen
, num_named
, named_type
;
4221 char *export_name
= NULL
;
4222 MonoMethod
*wrapper
;
4224 /* this cannot be enforced by the C# compiler so we must give the user some warning before aborting */
4225 if (!(method
->flags
& METHOD_ATTRIBUTE_STATIC
)) {
4226 g_warning ("AOT restriction: Method '%s' must be static since it is decorated with [MonoPInvokeCallback]. See http://ios.xamarin.com/Documentation/Limitations#Reverse_Callbacks",
4227 mono_method_full_name (method
, TRUE
));
4231 g_assert (sig
->param_count
== 1);
4232 g_assert (sig
->params
[0]->type
== MONO_TYPE_CLASS
&& !strcmp (mono_class_from_mono_type (sig
->params
[0])->name
, "Type"));
4235 * Decode the cattr manually since we can't create objects
4236 * during aot compilation.
4242 /* From load_cattr_value () in reflection.c */
4243 slen
= mono_metadata_decode_value (p
, &p
);
4244 n
= (char *)g_memdup (p
, slen
+ 1);
4246 t
= mono_reflection_type_from_name_checked (n
, acfg
->image
, &error
);
4248 mono_error_assert_ok (&error
);
4251 klass
= mono_class_from_mono_type (t
);
4252 g_assert (klass
->parent
== mono_defaults
.multicastdelegate_class
);
4256 num_named
= read16 (p
);
4259 g_assert (num_named
< 2);
4260 if (num_named
== 1) {
4264 /* parse ExportSymbol attribute */
4266 named_type
= *named
;
4268 /* data_type = *named; */
4271 name_len
= mono_metadata_decode_blob_size (named
, &named
);
4272 name
= (char *)g_malloc (name_len
+ 1);
4273 memcpy (name
, named
, name_len
);
4274 name
[name_len
] = 0;
4277 g_assert (named_type
== 0x54);
4278 g_assert (!strcmp (name
, "ExportSymbol"));
4280 /* load_cattr_value (), string case */
4281 g_assert (*named
!= (char)0xff);
4282 slen
= mono_metadata_decode_value (named
, &named
);
4283 export_name
= (char *)g_malloc (slen
+ 1);
4284 memcpy (export_name
, named
, slen
);
4285 export_name
[slen
] = 0;
4289 wrapper
= mono_marshal_get_managed_wrapper (method
, klass
, 0);
4290 add_method (acfg
, wrapper
);
4292 g_hash_table_insert (acfg
->export_names
, wrapper
, export_name
);
4297 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
4298 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)) {
4299 add_method (acfg
, mono_marshal_get_native_wrapper (method
, TRUE
, TRUE
));
4303 /* StructureToPtr/PtrToStructure wrappers */
4304 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
) {
4308 token
= MONO_TOKEN_TYPE_DEF
| (i
+ 1);
4309 klass
= mono_class_get_checked (acfg
->image
, token
, &error
);
4312 mono_error_cleanup (&error
);
4316 if (klass
->valuetype
&& !mono_class_is_gtd (klass
) && can_marshal_struct (klass
) &&
4317 !(klass
->nested_in
&& strstr (klass
->nested_in
->name
, "<PrivateImplementationDetails>") == klass
->nested_in
->name
)) {
4318 add_method (acfg
, mono_marshal_get_struct_to_ptr (klass
));
4319 add_method (acfg
, mono_marshal_get_ptr_to_struct (klass
));
4325 has_type_vars (MonoClass
*klass
)
4327 if ((klass
->byval_arg
.type
== MONO_TYPE_VAR
) || (klass
->byval_arg
.type
== MONO_TYPE_MVAR
))
4330 return has_type_vars (klass
->element_class
);
4331 if (mono_class_is_ginst (klass
)) {
4332 MonoGenericContext
*context
= &mono_class_get_generic_class (klass
)->context
;
4333 if (context
->class_inst
) {
4336 for (i
= 0; i
< context
->class_inst
->type_argc
; ++i
)
4337 if (has_type_vars (mono_class_from_mono_type (context
->class_inst
->type_argv
[i
])))
4341 if (mono_class_is_gtd (klass
))
4347 is_vt_inst (MonoGenericInst
*inst
)
4351 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4352 MonoType
*t
= inst
->type_argv
[i
];
4353 if (MONO_TYPE_ISSTRUCT (t
) || t
->type
== MONO_TYPE_VALUETYPE
)
4360 method_has_type_vars (MonoMethod
*method
)
4362 if (has_type_vars (method
->klass
))
4365 if (method
->is_inflated
) {
4366 MonoGenericContext
*context
= mono_method_get_context (method
);
4367 if (context
->method_inst
) {
4370 for (i
= 0; i
< context
->method_inst
->type_argc
; ++i
)
4371 if (has_type_vars (mono_class_from_mono_type (context
->method_inst
->type_argv
[i
])))
4379 gboolean
mono_aot_mode_is_full (MonoAotOptions
*opts
)
4381 return opts
->mode
== MONO_AOT_MODE_FULL
;
4385 gboolean
mono_aot_mode_is_hybrid (MonoAotOptions
*opts
)
4387 return opts
->mode
== MONO_AOT_MODE_HYBRID
;
4390 static void add_generic_class_with_depth (MonoAotCompile
*acfg
, MonoClass
*klass
, int depth
, const char *ref
);
4393 add_generic_class (MonoAotCompile
*acfg
, MonoClass
*klass
, gboolean force
, const char *ref
)
4395 /* This might lead to a huge code blowup so only do it if neccesary */
4396 if (!mono_aot_mode_is_full (&acfg
->aot_opts
) && !mono_aot_mode_is_hybrid (&acfg
->aot_opts
) && !force
)
4399 add_generic_class_with_depth (acfg
, klass
, 0, ref
);
4403 check_type_depth (MonoType
*t
, int depth
)
4411 case MONO_TYPE_GENERICINST
: {
4412 MonoGenericClass
*gklass
= t
->data
.generic_class
;
4413 MonoGenericInst
*ginst
= gklass
->context
.class_inst
;
4416 for (i
= 0; i
< ginst
->type_argc
; ++i
) {
4417 if (check_type_depth (ginst
->type_argv
[i
], depth
+ 1))
4431 add_types_from_method_header (MonoAotCompile
*acfg
, MonoMethod
*method
);
4434 * add_generic_class:
4436 * Add all methods of a generic class.
4439 add_generic_class_with_depth (MonoAotCompile
*acfg
, MonoClass
*klass
, int depth
, const char *ref
)
4442 MonoClassField
*field
;
4444 gboolean use_gsharedvt
= FALSE
;
4446 if (!acfg
->ginst_hash
)
4447 acfg
->ginst_hash
= g_hash_table_new (NULL
, NULL
);
4449 mono_class_init (klass
);
4451 if (mono_class_is_ginst (klass
) && mono_class_get_generic_class (klass
)->context
.class_inst
->is_open
)
4454 if (has_type_vars (klass
))
4457 if (!mono_class_is_ginst (klass
) && !klass
->rank
)
4460 if (mono_class_has_failure (klass
))
4463 if (!acfg
->ginst_hash
)
4464 acfg
->ginst_hash
= g_hash_table_new (NULL
, NULL
);
4466 if (g_hash_table_lookup (acfg
->ginst_hash
, klass
))
4469 if (check_type_depth (&klass
->byval_arg
, 0))
4472 if (acfg
->aot_opts
.log_generics
)
4473 aot_printf (acfg
, "%*sAdding generic instance %s [%s].\n", depth
, "", mono_type_full_name (&klass
->byval_arg
), ref
);
4475 g_hash_table_insert (acfg
->ginst_hash
, klass
, klass
);
4478 * Use gsharedvt for generic collections with vtype arguments to avoid code blowup.
4479 * Enable this only for some classes since gsharedvt might not support all methods.
4481 if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && klass
->image
== 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
) &&
4482 (!strcmp (klass
->name
, "Dictionary`2") || !strcmp (klass
->name
, "List`1") || !strcmp (klass
->name
, "ReadOnlyCollection`1")))
4483 use_gsharedvt
= TRUE
;
4486 while ((method
= mono_class_get_methods (klass
, &iter
))) {
4487 if ((acfg
->opts
& MONO_OPT_GSHAREDVT
) && method
->is_inflated
&& mono_method_get_context (method
)->method_inst
) {
4489 * This is partial sharing, and we can't handle it yet
4494 if (mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, use_gsharedvt
)) {
4496 add_types_from_method_header (acfg
, method
);
4500 if (method
->is_generic
)
4505 * FIXME: Instances which are referenced by these methods are not added,
4506 * for example Array.Resize<int> for List<int>.Add ().
4508 add_extra_method_with_depth (acfg
, method
, depth
+ 1);
4512 while ((field
= mono_class_get_fields (klass
, &iter
))) {
4513 if (field
->type
->type
== MONO_TYPE_GENERICINST
)
4514 add_generic_class_with_depth (acfg
, mono_class_from_mono_type (field
->type
), depth
+ 1, "field");
4517 if (klass
->delegate
) {
4518 method
= mono_get_delegate_invoke (klass
);
4520 method
= mono_marshal_get_delegate_invoke (method
, NULL
);
4522 if (acfg
->aot_opts
.log_generics
)
4523 aot_printf (acfg
, "%*sAdding method %s.\n", depth
, "", mono_method_get_full_name (method
));
4525 add_method (acfg
, method
);
4528 /* Add superclasses */
4530 add_generic_class_with_depth (acfg
, klass
->parent
, depth
, "parent");
4533 * For ICollection<T>, add instances of the helper methods
4534 * in Array, since a T[] could be cast to ICollection<T>.
4536 if (klass
->image
== mono_defaults
.corlib
&& !strcmp (klass
->name_space
, "System.Collections.Generic") &&
4537 (!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"))) {
4538 MonoClass
*tclass
= mono_class_from_mono_type (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
4539 MonoClass
*array_class
= mono_bounded_array_class_get (tclass
, 1, FALSE
);
4543 if (!strcmp (klass
->name
, "IEnumerator`1"))
4544 name_prefix
= g_strdup_printf ("%s.%s", klass
->name_space
, "IEnumerable`1");
4546 name_prefix
= g_strdup_printf ("%s.%s", klass
->name_space
, klass
->name
);
4548 /* Add the T[]/InternalEnumerator class */
4549 if (!strcmp (klass
->name
, "IEnumerable`1") || !strcmp (klass
->name
, "IEnumerator`1")) {
4554 while ((nclass
= mono_class_get_nested_types (array_class
->parent
, &iter
))) {
4555 if (!strcmp (nclass
->name
, "InternalEnumerator`1"))
4559 nclass
= mono_class_inflate_generic_class_checked (nclass
, mono_generic_class_get_context (mono_class_get_generic_class (klass
)), &error
);
4560 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
4561 add_generic_class (acfg
, nclass
, FALSE
, "ICollection<T>");
4565 while ((method
= mono_class_get_methods (array_class
, &iter
))) {
4566 if (strstr (method
->name
, name_prefix
)) {
4567 MonoMethod
*m
= mono_aot_get_array_helper_from_wrapper (method
);
4569 add_extra_method_with_depth (acfg
, m
, depth
);
4573 g_free (name_prefix
);
4576 /* Add an instance of GenericComparer<T> which is created dynamically by Comparer<T> */
4577 if (klass
->image
== mono_defaults
.corlib
&& !strcmp (klass
->name_space
, "System.Collections.Generic") && !strcmp (klass
->name
, "Comparer`1")) {
4579 MonoClass
*tclass
= mono_class_from_mono_type (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
4580 MonoClass
*icomparable
, *gcomparer
, *icomparable_inst
;
4581 MonoGenericContext ctx
;
4582 MonoType
*args
[16];
4584 memset (&ctx
, 0, sizeof (ctx
));
4586 icomparable
= mono_class_load_from_name (mono_defaults
.corlib
, "System", "IComparable`1");
4588 args
[0] = &tclass
->byval_arg
;
4589 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
4591 icomparable_inst
= mono_class_inflate_generic_class_checked (icomparable
, &ctx
, &error
);
4592 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
4594 if (mono_class_is_assignable_from (icomparable_inst
, tclass
)) {
4595 MonoClass
*gcomparer_inst
;
4596 gcomparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "GenericComparer`1");
4597 gcomparer_inst
= mono_class_inflate_generic_class_checked (gcomparer
, &ctx
, &error
);
4598 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
4600 add_generic_class (acfg
, gcomparer_inst
, FALSE
, "Comparer<T>");
4604 /* Add an instance of GenericEqualityComparer<T> which is created dynamically by EqualityComparer<T> */
4605 if (klass
->image
== mono_defaults
.corlib
&& !strcmp (klass
->name_space
, "System.Collections.Generic") && !strcmp (klass
->name
, "EqualityComparer`1")) {
4607 MonoClass
*tclass
= mono_class_from_mono_type (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
4608 MonoClass
*iface
, *gcomparer
, *iface_inst
;
4609 MonoGenericContext ctx
;
4610 MonoType
*args
[16];
4612 memset (&ctx
, 0, sizeof (ctx
));
4614 iface
= mono_class_load_from_name (mono_defaults
.corlib
, "System", "IEquatable`1");
4616 args
[0] = &tclass
->byval_arg
;
4617 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
4619 iface_inst
= mono_class_inflate_generic_class_checked (iface
, &ctx
, &error
);
4620 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
4622 if (mono_class_is_assignable_from (iface_inst
, tclass
)) {
4623 MonoClass
*gcomparer_inst
;
4626 gcomparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "GenericEqualityComparer`1");
4627 gcomparer_inst
= mono_class_inflate_generic_class_checked (gcomparer
, &ctx
, &error
);
4628 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
4629 add_generic_class (acfg
, gcomparer_inst
, FALSE
, "EqualityComparer<T>");
4633 /* Add an instance of EnumComparer<T> which is created dynamically by EqualityComparer<T> for enums */
4634 if (klass
->image
== mono_defaults
.corlib
&& !strcmp (klass
->name_space
, "System.Collections.Generic") && !strcmp (klass
->name
, "EqualityComparer`1")) {
4635 MonoClass
*enum_comparer
;
4636 MonoClass
*tclass
= mono_class_from_mono_type (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
4637 MonoGenericContext ctx
;
4638 MonoType
*args
[16];
4640 if (mono_class_is_enum (tclass
)) {
4641 MonoClass
*enum_comparer_inst
;
4644 memset (&ctx
, 0, sizeof (ctx
));
4645 args
[0] = &tclass
->byval_arg
;
4646 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
4648 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "EnumEqualityComparer`1");
4649 enum_comparer_inst
= mono_class_inflate_generic_class_checked (enum_comparer
, &ctx
, &error
);
4650 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
4651 add_generic_class (acfg
, enum_comparer_inst
, FALSE
, "EqualityComparer<T>");
4655 /* Add an instance of ObjectComparer<T> which is created dynamically by Comparer<T> for enums */
4656 if (klass
->image
== mono_defaults
.corlib
&& !strcmp (klass
->name_space
, "System.Collections.Generic") && !strcmp (klass
->name
, "Comparer`1")) {
4657 MonoClass
*comparer
;
4658 MonoClass
*tclass
= mono_class_from_mono_type (mono_class_get_generic_class (klass
)->context
.class_inst
->type_argv
[0]);
4659 MonoGenericContext ctx
;
4660 MonoType
*args
[16];
4662 if (mono_class_is_enum (tclass
)) {
4663 MonoClass
*comparer_inst
;
4666 memset (&ctx
, 0, sizeof (ctx
));
4667 args
[0] = &tclass
->byval_arg
;
4668 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
4670 comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "ObjectComparer`1");
4671 comparer_inst
= mono_class_inflate_generic_class_checked (comparer
, &ctx
, &error
);
4672 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
4673 add_generic_class (acfg
, comparer_inst
, FALSE
, "Comparer<T>");
4679 add_instances_of (MonoAotCompile
*acfg
, MonoClass
*klass
, MonoType
**insts
, int ninsts
, gboolean force
)
4682 MonoGenericContext ctx
;
4683 MonoType
*args
[16];
4685 if (acfg
->aot_opts
.no_instances
)
4688 memset (&ctx
, 0, sizeof (ctx
));
4690 for (i
= 0; i
< ninsts
; ++i
) {
4692 MonoClass
*generic_inst
;
4693 args
[0] = insts
[i
];
4694 ctx
.class_inst
= mono_metadata_get_generic_inst (1, args
);
4695 generic_inst
= mono_class_inflate_generic_class_checked (klass
, &ctx
, &error
);
4696 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
4697 add_generic_class (acfg
, generic_inst
, force
, "");
4702 add_types_from_method_header (MonoAotCompile
*acfg
, MonoMethod
*method
)
4705 MonoMethodHeader
*header
;
4706 MonoMethodSignature
*sig
;
4709 depth
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_depth
, method
));
4711 sig
= mono_method_signature (method
);
4714 for (j
= 0; j
< sig
->param_count
; ++j
)
4715 if (sig
->params
[j
]->type
== MONO_TYPE_GENERICINST
)
4716 add_generic_class_with_depth (acfg
, mono_class_from_mono_type (sig
->params
[j
]), depth
+ 1, "arg");
4719 header
= mono_method_get_header_checked (method
, &error
);
4722 for (j
= 0; j
< header
->num_locals
; ++j
)
4723 if (header
->locals
[j
]->type
== MONO_TYPE_GENERICINST
)
4724 add_generic_class_with_depth (acfg
, mono_class_from_mono_type (header
->locals
[j
]), depth
+ 1, "local");
4725 mono_metadata_free_mh (header
);
4727 mono_error_cleanup (&error
); /* FIXME report the error */
4733 * add_generic_instances:
4735 * Add instances referenced by the METHODSPEC/TYPESPEC table.
4738 add_generic_instances (MonoAotCompile
*acfg
)
4743 MonoGenericContext
*context
;
4745 if (acfg
->aot_opts
.no_instances
)
4748 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_METHODSPEC
].rows
; ++i
) {
4750 token
= MONO_TOKEN_METHOD_SPEC
| (i
+ 1);
4751 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, &error
);
4754 aot_printerrf (acfg
, "Failed to load methodspec 0x%x due to %s.\n", token
, mono_error_get_message (&error
));
4755 aot_printerrf (acfg
, "Run with MONO_LOG_LEVEL=debug for more information.\n");
4756 mono_error_cleanup (&error
);
4760 if (method
->klass
->image
!= acfg
->image
)
4763 context
= mono_method_get_context (method
);
4765 if (context
&& ((context
->class_inst
&& context
->class_inst
->is_open
)))
4769 * For open methods, create an instantiation which can be passed to the JIT.
4770 * FIXME: Handle class_inst as well.
4772 if (context
&& context
->method_inst
&& context
->method_inst
->is_open
) {
4774 MonoGenericContext shared_context
;
4775 MonoGenericInst
*inst
;
4776 MonoType
**type_argv
;
4778 MonoMethod
*declaring_method
;
4779 gboolean supported
= TRUE
;
4781 /* Check that the context doesn't contain open constructed types */
4782 if (context
->class_inst
) {
4783 inst
= context
->class_inst
;
4784 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4785 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
)
4787 if (mono_class_is_open_constructed_type (inst
->type_argv
[i
]))
4791 if (context
->method_inst
) {
4792 inst
= context
->method_inst
;
4793 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4794 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
)
4796 if (mono_class_is_open_constructed_type (inst
->type_argv
[i
]))
4804 memset (&shared_context
, 0, sizeof (MonoGenericContext
));
4806 inst
= context
->class_inst
;
4808 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
4809 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4810 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
)
4811 type_argv
[i
] = &mono_defaults
.object_class
->byval_arg
;
4813 type_argv
[i
] = inst
->type_argv
[i
];
4816 shared_context
.class_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
4820 inst
= context
->method_inst
;
4822 type_argv
= g_new0 (MonoType
*, inst
->type_argc
);
4823 for (i
= 0; i
< inst
->type_argc
; ++i
) {
4824 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
)
4825 type_argv
[i
] = &mono_defaults
.object_class
->byval_arg
;
4827 type_argv
[i
] = inst
->type_argv
[i
];
4830 shared_context
.method_inst
= mono_metadata_get_generic_inst (inst
->type_argc
, type_argv
);
4834 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
))
4835 declaring_method
= method
;
4837 declaring_method
= mono_method_get_declaring_generic_method (method
);
4839 method
= mono_class_inflate_generic_method_checked (declaring_method
, &shared_context
, &error
);
4840 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
4844 * If the method is fully sharable, it was already added in place of its
4845 * generic definition.
4847 if (mono_method_is_generic_sharable_full (method
, FALSE
, FALSE
, FALSE
))
4851 * FIXME: Partially shared methods are not shared here, so we end up with
4852 * many identical methods.
4854 add_extra_method (acfg
, method
);
4857 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPESPEC
].rows
; ++i
) {
4861 token
= MONO_TOKEN_TYPE_SPEC
| (i
+ 1);
4863 klass
= mono_class_get_checked (acfg
->image
, token
, &error
);
4864 if (!klass
|| klass
->rank
) {
4865 mono_error_cleanup (&error
);
4869 add_generic_class (acfg
, klass
, FALSE
, "typespec");
4872 /* Add types of args/locals */
4873 for (i
= 0; i
< acfg
->methods
->len
; ++i
) {
4874 method
= (MonoMethod
*)g_ptr_array_index (acfg
->methods
, i
);
4875 add_types_from_method_header (acfg
, method
);
4878 if (acfg
->image
== mono_defaults
.corlib
) {
4880 MonoType
*insts
[256];
4883 insts
[ninsts
++] = &mono_defaults
.byte_class
->byval_arg
;
4884 insts
[ninsts
++] = &mono_defaults
.sbyte_class
->byval_arg
;
4885 insts
[ninsts
++] = &mono_defaults
.int16_class
->byval_arg
;
4886 insts
[ninsts
++] = &mono_defaults
.uint16_class
->byval_arg
;
4887 insts
[ninsts
++] = &mono_defaults
.int32_class
->byval_arg
;
4888 insts
[ninsts
++] = &mono_defaults
.uint32_class
->byval_arg
;
4889 insts
[ninsts
++] = &mono_defaults
.int64_class
->byval_arg
;
4890 insts
[ninsts
++] = &mono_defaults
.uint64_class
->byval_arg
;
4891 insts
[ninsts
++] = &mono_defaults
.single_class
->byval_arg
;
4892 insts
[ninsts
++] = &mono_defaults
.double_class
->byval_arg
;
4893 insts
[ninsts
++] = &mono_defaults
.char_class
->byval_arg
;
4894 insts
[ninsts
++] = &mono_defaults
.boolean_class
->byval_arg
;
4896 /* Add GenericComparer<T> instances for primitive types for Enum.ToString () */
4897 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "GenericComparer`1");
4899 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
4900 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "GenericEqualityComparer`1");
4902 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
4904 /* Add instances of EnumEqualityComparer which are created by EqualityComparer<T> for enums */
4906 MonoClass
*enum_comparer
;
4907 MonoType
*insts
[16];
4911 insts
[ninsts
++] = &mono_defaults
.int32_class
->byval_arg
;
4912 insts
[ninsts
++] = &mono_defaults
.uint32_class
->byval_arg
;
4913 insts
[ninsts
++] = &mono_defaults
.uint16_class
->byval_arg
;
4914 insts
[ninsts
++] = &mono_defaults
.byte_class
->byval_arg
;
4915 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "EnumEqualityComparer`1");
4916 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
4919 insts
[ninsts
++] = &mono_defaults
.int16_class
->byval_arg
;
4920 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "ShortEnumEqualityComparer`1");
4921 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
4924 insts
[ninsts
++] = &mono_defaults
.sbyte_class
->byval_arg
;
4925 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "SByteEnumEqualityComparer`1");
4926 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
4928 enum_comparer
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Collections.Generic", "LongEnumEqualityComparer`1");
4930 insts
[ninsts
++] = &mono_defaults
.int64_class
->byval_arg
;
4931 insts
[ninsts
++] = &mono_defaults
.uint64_class
->byval_arg
;
4932 add_instances_of (acfg
, enum_comparer
, insts
, ninsts
, FALSE
);
4935 /* Add instances of the array generic interfaces for primitive types */
4936 /* This will add instances of the InternalArray_ helper methods in Array too */
4937 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "ICollection`1");
4939 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
4941 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "IList`1");
4943 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
4945 klass
= mono_class_try_load_from_name (acfg
->image
, "System.Collections.Generic", "IEnumerable`1");
4947 add_instances_of (acfg
, klass
, insts
, ninsts
, TRUE
);
4950 * Add a managed-to-native wrapper of Array.GetGenericValueImpl<object>, which is
4951 * used for all instances of GetGenericValueImpl by the AOT runtime.
4954 MonoGenericContext ctx
;
4955 MonoType
*args
[16];
4956 MonoMethod
*get_method
;
4957 MonoClass
*array_klass
= mono_array_class_get (mono_defaults
.object_class
, 1)->parent
;
4959 get_method
= mono_class_get_method_from_name (array_klass
, "GetGenericValueImpl", 2);
4963 memset (&ctx
, 0, sizeof (ctx
));
4964 args
[0] = &mono_defaults
.object_class
->byval_arg
;
4965 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
4966 add_extra_method (acfg
, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (get_method
, &ctx
, &error
), TRUE
, TRUE
));
4967 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
4971 /* Same for CompareExchange<T>/Exchange<T> */
4973 MonoGenericContext ctx
;
4974 MonoType
*args
[16];
4976 MonoClass
*interlocked_klass
= mono_class_load_from_name (mono_defaults
.corlib
, "System.Threading", "Interlocked");
4977 gpointer iter
= NULL
;
4979 while ((m
= mono_class_get_methods (interlocked_klass
, &iter
))) {
4980 if ((!strcmp (m
->name
, "CompareExchange") || !strcmp (m
->name
, "Exchange")) && m
->is_generic
) {
4982 memset (&ctx
, 0, sizeof (ctx
));
4983 args
[0] = &mono_defaults
.object_class
->byval_arg
;
4984 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
4985 add_extra_method (acfg
, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m
, &ctx
, &error
), TRUE
, TRUE
));
4986 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
4991 /* Same for Volatile.Read/Write<T> */
4993 MonoGenericContext ctx
;
4994 MonoType
*args
[16];
4996 MonoClass
*volatile_klass
= mono_class_try_load_from_name (mono_defaults
.corlib
, "System.Threading", "Volatile");
4997 gpointer iter
= NULL
;
4999 if (volatile_klass
) {
5000 while ((m
= mono_class_get_methods (volatile_klass
, &iter
))) {
5001 if ((!strcmp (m
->name
, "Read") || !strcmp (m
->name
, "Write")) && m
->is_generic
) {
5003 memset (&ctx
, 0, sizeof (ctx
));
5004 args
[0] = &mono_defaults
.object_class
->byval_arg
;
5005 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
5006 add_extra_method (acfg
, mono_marshal_get_native_wrapper (mono_class_inflate_generic_method_checked (m
, &ctx
, &error
), TRUE
, TRUE
));
5007 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
5013 /* object[] accessor wrappers. */
5014 for (i
= 1; i
< 4; ++i
) {
5015 MonoClass
*obj_array_class
= mono_array_class_get (mono_defaults
.object_class
, i
);
5018 m
= mono_class_get_method_from_name (obj_array_class
, "Get", i
);
5021 m
= mono_marshal_get_array_accessor_wrapper (m
);
5022 add_extra_method (acfg
, m
);
5024 m
= mono_class_get_method_from_name (obj_array_class
, "Address", i
);
5027 m
= mono_marshal_get_array_accessor_wrapper (m
);
5028 add_extra_method (acfg
, m
);
5030 m
= mono_class_get_method_from_name (obj_array_class
, "Set", i
+ 1);
5033 m
= mono_marshal_get_array_accessor_wrapper (m
);
5034 add_extra_method (acfg
, m
);
5040 * is_direct_callable:
5042 * Return whenever the method identified by JI is directly callable without
5043 * going through the PLT.
5046 is_direct_callable (MonoAotCompile
*acfg
, MonoMethod
*method
, MonoJumpInfo
*patch_info
)
5048 if ((patch_info
->type
== MONO_PATCH_INFO_METHOD
) && (patch_info
->data
.method
->klass
->image
== acfg
->image
)) {
5049 MonoCompile
*callee_cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, patch_info
->data
.method
);
5051 gboolean direct_callable
= TRUE
;
5053 if (direct_callable
&& !(!callee_cfg
->has_got_slots
&& mono_class_is_before_field_init (callee_cfg
->method
->klass
)))
5054 direct_callable
= FALSE
;
5055 if ((callee_cfg
->method
->iflags
& METHOD_IMPL_ATTRIBUTE_SYNCHRONIZED
) && (!method
|| method
->wrapper_type
!= MONO_WRAPPER_SYNCHRONIZED
))
5056 // FIXME: Maybe call the wrapper directly ?
5057 direct_callable
= FALSE
;
5059 if (acfg
->aot_opts
.soft_debug
|| acfg
->aot_opts
.no_direct_calls
) {
5060 /* Disable this so all calls go through load_method (), see the
5061 * mini_get_debug_options ()->load_aot_jit_info_eagerly = TRUE; line in
5062 * mono_debugger_agent_init ().
5064 direct_callable
= FALSE
;
5067 if (callee_cfg
->method
->wrapper_type
== MONO_WRAPPER_ALLOC
)
5068 /* sgen does some initialization when the allocator method is created */
5069 direct_callable
= FALSE
;
5070 if (callee_cfg
->method
->wrapper_type
== MONO_WRAPPER_WRITE_BARRIER
)
5071 /* we don't know at compile time whether sgen is concurrent or not */
5072 direct_callable
= FALSE
;
5074 if (direct_callable
)
5077 } else if ((patch_info
->type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
&& patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
)) {
5078 if (acfg
->aot_opts
.direct_pinvoke
)
5080 } else if (patch_info
->type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
5081 if (acfg
->aot_opts
.direct_icalls
)
5089 #ifdef MONO_ARCH_AOT_SUPPORTED
5091 get_pinvoke_import (MonoAotCompile
*acfg
, MonoMethod
*method
)
5093 MonoImage
*image
= method
->klass
->image
;
5094 MonoMethodPInvoke
*piinfo
= (MonoMethodPInvoke
*) method
;
5095 MonoTableInfo
*tables
= image
->tables
;
5096 MonoTableInfo
*im
= &tables
[MONO_TABLE_IMPLMAP
];
5097 MonoTableInfo
*mr
= &tables
[MONO_TABLE_MODULEREF
];
5098 guint32 im_cols
[MONO_IMPLMAP_SIZE
];
5101 import
= (char *)g_hash_table_lookup (acfg
->method_to_pinvoke_import
, method
);
5105 if (!piinfo
->implmap_idx
|| piinfo
->implmap_idx
> im
->rows
)
5108 mono_metadata_decode_row (im
, piinfo
->implmap_idx
- 1, im_cols
, MONO_IMPLMAP_SIZE
);
5110 if (!im_cols
[MONO_IMPLMAP_SCOPE
] || im_cols
[MONO_IMPLMAP_SCOPE
] > mr
->rows
)
5113 import
= g_strdup_printf ("%s", mono_metadata_string_heap (image
, im_cols
[MONO_IMPLMAP_NAME
]));
5115 g_hash_table_insert (acfg
->method_to_pinvoke_import
, method
, import
);
5121 get_pinvoke_import (MonoAotCompile
*acfg
, MonoMethod
*method
)
5128 compare_lne (MonoDebugLineNumberEntry
*a
, MonoDebugLineNumberEntry
*b
)
5130 if (a
->native_offset
== b
->native_offset
)
5131 return a
->il_offset
- b
->il_offset
;
5133 return a
->native_offset
- b
->native_offset
;
5137 * compute_line_numbers:
5139 * Returns a sparse array of size CODE_SIZE containing MonoDebugSourceLocation* entries for the native offsets which have a corresponding line number
5142 static MonoDebugSourceLocation
**
5143 compute_line_numbers (MonoMethod
*method
, int code_size
, MonoDebugMethodJitInfo
*debug_info
)
5145 MonoDebugMethodInfo
*minfo
;
5146 MonoDebugLineNumberEntry
*ln_array
;
5147 MonoDebugSourceLocation
*loc
;
5148 int i
, prev_line
, prev_il_offset
;
5149 int *native_to_il_offset
= NULL
;
5150 MonoDebugSourceLocation
**res
;
5153 minfo
= mono_debug_lookup_method (method
);
5156 // FIXME: This seems to happen when two methods have the same cfg->method_to_register
5157 if (debug_info
->code_size
!= code_size
)
5160 g_assert (code_size
);
5162 /* Compute the native->IL offset mapping */
5164 ln_array
= g_new0 (MonoDebugLineNumberEntry
, debug_info
->num_line_numbers
);
5165 memcpy (ln_array
, debug_info
->line_numbers
, debug_info
->num_line_numbers
* sizeof (MonoDebugLineNumberEntry
));
5167 qsort (ln_array
, debug_info
->num_line_numbers
, sizeof (MonoDebugLineNumberEntry
), (int (*)(const void *, const void *))compare_lne
);
5169 native_to_il_offset
= g_new0 (int, code_size
+ 1);
5171 for (i
= 0; i
< debug_info
->num_line_numbers
; ++i
) {
5173 MonoDebugLineNumberEntry
*lne
= &ln_array
[i
];
5176 for (j
= 0; j
< lne
->native_offset
; ++j
)
5177 native_to_il_offset
[j
] = -1;
5180 if (i
< debug_info
->num_line_numbers
- 1) {
5181 MonoDebugLineNumberEntry
*lne_next
= &ln_array
[i
+ 1];
5183 for (j
= lne
->native_offset
; j
< lne_next
->native_offset
; ++j
)
5184 native_to_il_offset
[j
] = lne
->il_offset
;
5186 for (j
= lne
->native_offset
; j
< code_size
; ++j
)
5187 native_to_il_offset
[j
] = lne
->il_offset
;
5192 /* Compute the native->line number mapping */
5193 res
= g_new0 (MonoDebugSourceLocation
*, code_size
);
5194 prev_il_offset
= -1;
5197 for (i
= 0; i
< code_size
; ++i
) {
5198 int il_offset
= native_to_il_offset
[i
];
5200 if (il_offset
== -1 || il_offset
== prev_il_offset
)
5202 prev_il_offset
= il_offset
;
5203 loc
= mono_debug_symfile_lookup_location (minfo
, il_offset
);
5204 if (!(loc
&& loc
->source_file
))
5206 if (loc
->row
== prev_line
) {
5207 mono_debug_symfile_free_location (loc
);
5210 prev_line
= loc
->row
;
5211 //printf ("D: %s:%d il=%x native=%x\n", loc->source_file, loc->row, il_offset, i);
5213 /* This will cover the prolog too */
5223 get_file_index (MonoAotCompile
*acfg
, const char *source_file
)
5227 // FIXME: Free these
5228 if (!acfg
->dwarf_ln_filenames
)
5229 acfg
->dwarf_ln_filenames
= g_hash_table_new (g_str_hash
, g_str_equal
);
5230 findex
= GPOINTER_TO_INT (g_hash_table_lookup (acfg
->dwarf_ln_filenames
, source_file
));
5232 findex
= g_hash_table_size (acfg
->dwarf_ln_filenames
) + 1;
5233 g_hash_table_insert (acfg
->dwarf_ln_filenames
, g_strdup (source_file
), GINT_TO_POINTER (findex
));
5234 emit_unset_mode (acfg
);
5235 fprintf (acfg
->fp
, ".file %d \"%s\"\n", findex
, mono_dwarf_escape_path (source_file
));
5247 * emit_and_reloc_code:
5249 * Emit the native code in CODE, handling relocations along the way. If GOT_ONLY
5250 * is true, calls are made through the GOT too. This is used for emitting trampolines
5251 * in full-aot mode, since calls made from trampolines couldn't go through the PLT,
5252 * since trampolines are needed to make PTL work.
5255 emit_and_reloc_code (MonoAotCompile
*acfg
, MonoMethod
*method
, guint8
*code
, guint32 code_len
, MonoJumpInfo
*relocs
, gboolean got_only
, MonoDebugMethodJitInfo
*debug_info
)
5257 int i
, pindex
, start_index
;
5259 MonoJumpInfo
*patch_info
;
5260 MonoDebugSourceLocation
**locs
= NULL
;
5261 gboolean skip
, prologue_end
= FALSE
;
5262 #ifdef MONO_ARCH_AOT_SUPPORTED
5263 gboolean direct_call
, external_call
;
5265 const char *direct_call_target
= 0;
5266 const char *direct_pinvoke
;
5269 if (acfg
->gas_line_numbers
&& method
&& debug_info
) {
5270 locs
= compute_line_numbers (method
, code_len
, debug_info
);
5272 int findex
= get_file_index (acfg
, "<unknown>");
5273 emit_unset_mode (acfg
);
5274 fprintf (acfg
->fp
, ".loc %d %d 0\n", findex
, 1);
5278 /* Collect and sort relocations */
5279 patches
= g_ptr_array_new ();
5280 for (patch_info
= relocs
; patch_info
; patch_info
= patch_info
->next
)
5281 g_ptr_array_add (patches
, patch_info
);
5282 g_ptr_array_sort (patches
, compare_patches
);
5285 for (i
= 0; i
< code_len
; i
+= INST_LEN
) {
5287 for (pindex
= start_index
; pindex
< patches
->len
; ++pindex
) {
5288 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
5289 if (patch_info
->ip
.i
>= i
)
5293 if (locs
&& locs
[i
]) {
5294 MonoDebugSourceLocation
*loc
= locs
[i
];
5296 const char *options
;
5298 findex
= get_file_index (acfg
, loc
->source_file
);
5299 emit_unset_mode (acfg
);
5301 options
= " prologue_end";
5304 prologue_end
= TRUE
;
5305 fprintf (acfg
->fp
, ".loc %d %d 0%s\n", findex
, loc
->row
, options
);
5306 mono_debug_symfile_free_location (loc
);
5310 #ifdef MONO_ARCH_AOT_SUPPORTED
5311 if (patch_info
&& (patch_info
->ip
.i
== i
) && (pindex
< patches
->len
)) {
5312 start_index
= pindex
;
5314 switch (patch_info
->type
) {
5315 case MONO_PATCH_INFO_NONE
:
5317 case MONO_PATCH_INFO_GOT_OFFSET
: {
5320 arch_emit_got_offset (acfg
, code
+ i
, &code_size
);
5321 i
+= code_size
- INST_LEN
;
5323 patch_info
->type
= MONO_PATCH_INFO_NONE
;
5326 case MONO_PATCH_INFO_OBJC_SELECTOR_REF
: {
5327 int code_size
, index
;
5328 char *selector
= (char *)patch_info
->data
.target
;
5330 if (!acfg
->objc_selector_to_index
)
5331 acfg
->objc_selector_to_index
= g_hash_table_new (g_str_hash
, g_str_equal
);
5332 if (!acfg
->objc_selectors
)
5333 acfg
->objc_selectors
= g_ptr_array_new ();
5334 index
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->objc_selector_to_index
, selector
));
5338 index
= acfg
->objc_selector_index
;
5339 g_ptr_array_add (acfg
->objc_selectors
, (void*)patch_info
->data
.target
);
5340 g_hash_table_insert (acfg
->objc_selector_to_index
, selector
, GUINT_TO_POINTER (index
+ 1));
5341 acfg
->objc_selector_index
++;
5344 arch_emit_objc_selector_ref (acfg
, code
+ i
, index
, &code_size
);
5345 i
+= code_size
- INST_LEN
;
5347 patch_info
->type
= MONO_PATCH_INFO_NONE
;
5352 * If this patch is a call, try emitting a direct call instead of
5353 * through a PLT entry. This is possible if the called method is in
5354 * the same assembly and requires no initialization.
5356 direct_call
= FALSE
;
5357 external_call
= FALSE
;
5358 if ((patch_info
->type
== MONO_PATCH_INFO_METHOD
) && (patch_info
->data
.method
->klass
->image
== acfg
->image
)) {
5359 if (!got_only
&& is_direct_callable (acfg
, method
, patch_info
)) {
5360 MonoCompile
*callee_cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, patch_info
->data
.method
);
5361 //printf ("DIRECT: %s %s\n", method ? mono_method_full_name (method, TRUE) : "", mono_method_full_name (callee_cfg->method, TRUE));
5363 direct_call_target
= callee_cfg
->asm_symbol
;
5364 patch_info
->type
= MONO_PATCH_INFO_NONE
;
5365 acfg
->stats
.direct_calls
++;
5368 acfg
->stats
.all_calls
++;
5369 } else if (patch_info
->type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
5370 if (!got_only
&& is_direct_callable (acfg
, method
, patch_info
)) {
5371 if (!(patch_info
->data
.method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
5372 direct_pinvoke
= mono_lookup_icall_symbol (patch_info
->data
.method
);
5374 direct_pinvoke
= get_pinvoke_import (acfg
, patch_info
->data
.method
);
5375 if (direct_pinvoke
) {
5377 g_assert (strlen (direct_pinvoke
) < 1000);
5378 direct_call_target
= g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, direct_pinvoke
);
5381 } else if (patch_info
->type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
5382 const char *sym
= mono_lookup_jit_icall_symbol (patch_info
->data
.name
);
5383 if (!got_only
&& sym
&& acfg
->aot_opts
.direct_icalls
) {
5384 /* Call to a C function implementing a jit icall */
5386 external_call
= TRUE
;
5387 g_assert (strlen (sym
) < 1000);
5388 direct_call_target
= g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, sym
);
5390 } else if (patch_info
->type
== MONO_PATCH_INFO_INTERNAL_METHOD
) {
5391 MonoJitICallInfo
*info
= mono_find_jit_icall_by_name (patch_info
->data
.name
);
5392 const char *sym
= mono_lookup_jit_icall_symbol (patch_info
->data
.name
);
5393 if (!got_only
&& sym
&& acfg
->aot_opts
.direct_icalls
&& info
->func
== info
->wrapper
) {
5394 /* Call to a jit icall without a wrapper */
5396 external_call
= TRUE
;
5397 g_assert (strlen (sym
) < 1000);
5398 direct_call_target
= g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, sym
);
5403 patch_info
->type
= MONO_PATCH_INFO_NONE
;
5404 acfg
->stats
.direct_calls
++;
5407 if (!got_only
&& !direct_call
) {
5408 MonoPltEntry
*plt_entry
= get_plt_entry (acfg
, patch_info
);
5410 /* This patch has a PLT entry, so we must emit a call to the PLT entry */
5412 direct_call_target
= plt_entry
->symbol
;
5414 /* Nullify the patch */
5415 patch_info
->type
= MONO_PATCH_INFO_NONE
;
5416 plt_entry
->jit_used
= TRUE
;
5423 arch_emit_direct_call (acfg
, direct_call_target
, external_call
, FALSE
, patch_info
, &call_size
);
5424 i
+= call_size
- INST_LEN
;
5428 got_slot
= get_got_offset (acfg
, FALSE
, patch_info
);
5430 arch_emit_got_access (acfg
, acfg
->got_symbol
, code
+ i
, got_slot
, &code_size
);
5431 i
+= code_size
- INST_LEN
;
5437 #endif /* MONO_ARCH_AOT_SUPPORTED */
5440 /* Find next patch */
5442 for (pindex
= start_index
; pindex
< patches
->len
; ++pindex
) {
5443 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
5444 if (patch_info
->ip
.i
>= i
)
5448 /* Try to emit multiple bytes at once */
5449 if (pindex
< patches
->len
&& patch_info
->ip
.i
> i
) {
5452 for (limit
= i
+ INST_LEN
; limit
< patch_info
->ip
.i
; limit
+= INST_LEN
) {
5453 if (locs
&& locs
[limit
])
5457 emit_code_bytes (acfg
, code
+ i
, limit
- i
);
5458 i
= limit
- INST_LEN
;
5460 emit_code_bytes (acfg
, code
+ i
, INST_LEN
);
5465 g_ptr_array_free (patches
, TRUE
);
5472 * Return a modified version of S which only includes characters permissible in symbols.
5475 sanitize_symbol (MonoAotCompile
*acfg
, char *s
)
5477 gboolean process
= FALSE
;
5486 for (i
= 0; i
< len
; ++i
)
5487 if (!(s
[i
] <= 0x7f && (isalnum (s
[i
]) || s
[i
] == '_')))
5492 gs
= g_string_sized_new (len
);
5493 for (i
= 0; i
< len
; ++i
) {
5495 if (c
<= 0x7f && (isalnum (c
) || c
== '_')) {
5496 g_string_append_c (gs
, c
);
5497 } else if (c
> 0x7f) {
5498 /* multi-byte utf8 */
5499 g_string_append_printf (gs
, "_0x%x", c
);
5502 while (c
>> 6 == 0x2) {
5503 g_string_append_printf (gs
, "%x", c
);
5507 g_string_append_printf (gs
, "_");
5510 g_string_append_c (gs
, '_');
5514 res
= mono_mempool_strdup (acfg
->mempool
, gs
->str
);
5515 g_string_free (gs
, TRUE
);
5520 get_debug_sym (MonoMethod
*method
, const char *prefix
, GHashTable
*cache
)
5522 char *name1
, *name2
, *cached
;
5523 int i
, j
, len
, count
;
5524 MonoMethod
*cached_method
;
5526 name1
= mono_method_full_name (method
, TRUE
);
5529 // This is so that we don't accidentally create a local symbol (which starts with 'L')
5530 if ((!prefix
|| !*prefix
) && name1
[0] == 'L')
5534 #if defined(TARGET_WIN32) && defined(TARGET_X86)
5535 char adjustedPrefix
[MAX_SYMBOL_SIZE
];
5536 prefix
= mangle_symbol (prefix
, adjustedPrefix
, G_N_ELEMENTS (adjustedPrefix
));
5539 len
= strlen (name1
);
5540 name2
= (char *)malloc (strlen (prefix
) + len
+ 16);
5541 memcpy (name2
, prefix
, strlen (prefix
));
5542 j
= strlen (prefix
);
5543 for (i
= 0; i
< len
; ++i
) {
5544 if (i
== 0 && name1
[0] >= '0' && name1
[0] <= '9') {
5546 } else if (isalnum (name1
[i
])) {
5547 name2
[j
++] = name1
[i
];
5548 } else if (name1
[i
] == ' ' && name1
[i
+ 1] == '(' && name1
[i
+ 2] == ')') {
5550 } else if (name1
[i
] == ',' && name1
[i
+ 1] == ' ') {
5553 } else if (name1
[i
] == '(' || name1
[i
] == ')' || name1
[i
] == '>') {
5563 cached_method
= (MonoMethod
*)g_hash_table_lookup (cache
, name2
);
5564 if (!(cached_method
&& cached_method
!= method
))
5566 sprintf (name2
+ j
, "_%d", count
);
5570 cached
= g_strdup (name2
);
5571 g_hash_table_insert (cache
, cached
, method
);
5577 emit_method_code (MonoAotCompile
*acfg
, MonoCompile
*cfg
)
5582 char *debug_sym
= NULL
;
5583 char *symbol
= NULL
;
5584 int func_alignment
= AOT_FUNC_ALIGNMENT
;
5587 method
= cfg
->orig_method
;
5588 code
= cfg
->native_code
;
5590 method_index
= get_method_index (acfg
, method
);
5591 symbol
= g_strdup_printf ("%sme_%x", acfg
->temp_prefix
, method_index
);
5593 /* Make the labels local */
5594 emit_section_change (acfg
, ".text", 0);
5595 emit_alignment_code (acfg
, func_alignment
);
5597 if (acfg
->global_symbols
&& acfg
->need_no_dead_strip
)
5598 fprintf (acfg
->fp
, " .no_dead_strip %s\n", cfg
->asm_symbol
);
5600 emit_label (acfg
, cfg
->asm_symbol
);
5602 if (acfg
->aot_opts
.write_symbols
&& !acfg
->global_symbols
&& !acfg
->llvm
) {
5604 * Write a C style symbol for every method, this has two uses:
5605 * - it works on platforms where the dwarf debugging info is not
5607 * - it allows the setting of breakpoints of aot-ed methods.
5609 debug_sym
= get_debug_sym (method
, "", acfg
->method_label_hash
);
5610 cfg
->asm_debug_symbol
= g_strdup (debug_sym
);
5612 if (acfg
->need_no_dead_strip
)
5613 fprintf (acfg
->fp
, " .no_dead_strip %s\n", debug_sym
);
5614 emit_local_symbol (acfg
, debug_sym
, symbol
, TRUE
);
5615 emit_label (acfg
, debug_sym
);
5618 export_name
= (char *)g_hash_table_lookup (acfg
->export_names
, method
);
5620 /* Emit a global symbol for the method */
5621 emit_global_inner (acfg
, export_name
, TRUE
);
5622 emit_label (acfg
, export_name
);
5625 if (cfg
->verbose_level
> 0)
5626 g_print ("Method %s emitted as %s\n", mono_method_get_full_name (method
), cfg
->asm_symbol
);
5628 acfg
->stats
.code_size
+= cfg
->code_len
;
5630 acfg
->cfgs
[method_index
]->got_offset
= acfg
->got_offset
;
5632 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 ()));
5636 if (acfg
->aot_opts
.write_symbols
) {
5638 emit_symbol_size (acfg
, debug_sym
, ".");
5640 emit_symbol_size (acfg
, cfg
->asm_symbol
, ".");
5644 emit_label (acfg
, symbol
);
5651 * Encode PATCH_INFO into its disk representation.
5654 encode_patch (MonoAotCompile
*acfg
, MonoJumpInfo
*patch_info
, guint8
*buf
, guint8
**endbuf
)
5658 switch (patch_info
->type
) {
5659 case MONO_PATCH_INFO_NONE
:
5661 case MONO_PATCH_INFO_IMAGE
:
5662 encode_value (get_image_index (acfg
, patch_info
->data
.image
), p
, &p
);
5664 case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
:
5665 case MONO_PATCH_INFO_JIT_TLS_ID
:
5666 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
:
5667 case MONO_PATCH_INFO_GC_NURSERY_START
:
5668 case MONO_PATCH_INFO_GC_NURSERY_BITS
:
5670 case MONO_PATCH_INFO_CASTCLASS_CACHE
:
5671 encode_value (patch_info
->data
.index
, p
, &p
);
5673 case MONO_PATCH_INFO_METHOD_REL
:
5674 encode_value ((gint
)patch_info
->data
.offset
, p
, &p
);
5676 case MONO_PATCH_INFO_SWITCH
: {
5677 gpointer
*table
= (gpointer
*)patch_info
->data
.table
->table
;
5680 encode_value (patch_info
->data
.table
->table_size
, p
, &p
);
5681 for (k
= 0; k
< patch_info
->data
.table
->table_size
; k
++)
5682 encode_value ((int)(gssize
)table
[k
], p
, &p
);
5685 case MONO_PATCH_INFO_METHODCONST
:
5686 case MONO_PATCH_INFO_METHOD
:
5687 case MONO_PATCH_INFO_METHOD_JUMP
:
5688 case MONO_PATCH_INFO_ICALL_ADDR
:
5689 case MONO_PATCH_INFO_ICALL_ADDR_CALL
:
5690 case MONO_PATCH_INFO_METHOD_RGCTX
:
5691 case MONO_PATCH_INFO_METHOD_CODE_SLOT
:
5692 encode_method_ref (acfg
, patch_info
->data
.method
, p
, &p
);
5694 case MONO_PATCH_INFO_AOT_JIT_INFO
:
5695 encode_value (patch_info
->data
.index
, p
, &p
);
5697 case MONO_PATCH_INFO_INTERNAL_METHOD
:
5698 case MONO_PATCH_INFO_JIT_ICALL_ADDR
: {
5699 guint32 len
= strlen (patch_info
->data
.name
);
5701 encode_value (len
, p
, &p
);
5703 memcpy (p
, patch_info
->data
.name
, len
);
5708 case MONO_PATCH_INFO_LDSTR
: {
5709 guint32 image_index
= get_image_index (acfg
, patch_info
->data
.token
->image
);
5710 guint32 token
= patch_info
->data
.token
->token
;
5711 g_assert (mono_metadata_token_code (token
) == MONO_TOKEN_STRING
);
5712 encode_value (image_index
, p
, &p
);
5713 encode_value (patch_info
->data
.token
->token
- MONO_TOKEN_STRING
, p
, &p
);
5716 case MONO_PATCH_INFO_RVA
:
5717 case MONO_PATCH_INFO_DECLSEC
:
5718 case MONO_PATCH_INFO_LDTOKEN
:
5719 case MONO_PATCH_INFO_TYPE_FROM_HANDLE
:
5720 encode_value (get_image_index (acfg
, patch_info
->data
.token
->image
), p
, &p
);
5721 encode_value (patch_info
->data
.token
->token
, p
, &p
);
5722 encode_value (patch_info
->data
.token
->has_context
, p
, &p
);
5723 if (patch_info
->data
.token
->has_context
)
5724 encode_generic_context (acfg
, &patch_info
->data
.token
->context
, p
, &p
);
5726 case MONO_PATCH_INFO_EXC_NAME
: {
5727 MonoClass
*ex_class
;
5730 mono_class_load_from_name (mono_defaults
.exception_class
->image
,
5731 "System", (const char *)patch_info
->data
.target
);
5732 encode_klass_ref (acfg
, ex_class
, p
, &p
);
5735 case MONO_PATCH_INFO_R4
:
5736 encode_value (*((guint32
*)patch_info
->data
.target
), p
, &p
);
5738 case MONO_PATCH_INFO_R8
:
5739 encode_value (((guint32
*)patch_info
->data
.target
) [MINI_LS_WORD_IDX
], p
, &p
);
5740 encode_value (((guint32
*)patch_info
->data
.target
) [MINI_MS_WORD_IDX
], p
, &p
);
5742 case MONO_PATCH_INFO_VTABLE
:
5743 case MONO_PATCH_INFO_CLASS
:
5744 case MONO_PATCH_INFO_IID
:
5745 case MONO_PATCH_INFO_ADJUSTED_IID
:
5746 encode_klass_ref (acfg
, patch_info
->data
.klass
, p
, &p
);
5748 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
:
5749 encode_klass_ref (acfg
, patch_info
->data
.del_tramp
->klass
, p
, &p
);
5750 if (patch_info
->data
.del_tramp
->method
) {
5751 encode_value (1, p
, &p
);
5752 encode_method_ref (acfg
, patch_info
->data
.del_tramp
->method
, p
, &p
);
5754 encode_value (0, p
, &p
);
5756 encode_value (patch_info
->data
.del_tramp
->is_virtual
, p
, &p
);
5758 case MONO_PATCH_INFO_FIELD
:
5759 case MONO_PATCH_INFO_SFLDA
:
5760 encode_field_info (acfg
, patch_info
->data
.field
, p
, &p
);
5762 case MONO_PATCH_INFO_INTERRUPTION_REQUEST_FLAG
:
5764 case MONO_PATCH_INFO_RGCTX_FETCH
:
5765 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
5766 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
5771 * entry->method has a lenghtly encoding and multiple rgctx_fetch entries
5772 * reference the same method, so encode the method only once.
5774 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_blob_hash
, entry
->method
));
5776 buf2
= (guint8
*)g_malloc (1024);
5779 encode_method_ref (acfg
, entry
->method
, p2
, &p2
);
5780 g_assert (p2
- buf2
< 1024);
5782 offset
= add_to_blob (acfg
, buf2
, p2
- buf2
);
5785 g_hash_table_insert (acfg
->method_blob_hash
, entry
->method
, GUINT_TO_POINTER (offset
+ 1));
5790 encode_value (offset
, p
, &p
);
5791 g_assert ((int)entry
->info_type
< 256);
5792 g_assert (entry
->data
->type
< 256);
5793 encode_value ((entry
->in_mrgctx
? 1 : 0) | (entry
->info_type
<< 1) | (entry
->data
->type
<< 9), p
, &p
);
5794 encode_patch (acfg
, entry
->data
, p
, &p
);
5797 case MONO_PATCH_INFO_SEQ_POINT_INFO
:
5798 case MONO_PATCH_INFO_AOT_MODULE
:
5800 case MONO_PATCH_INFO_SIGNATURE
:
5801 case MONO_PATCH_INFO_GSHAREDVT_IN_WRAPPER
:
5802 encode_signature (acfg
, (MonoMethodSignature
*)patch_info
->data
.target
, p
, &p
);
5804 case MONO_PATCH_INFO_TLS_OFFSET
:
5805 encode_value (GPOINTER_TO_INT (patch_info
->data
.target
), p
, &p
);
5807 case MONO_PATCH_INFO_GSHAREDVT_CALL
:
5808 encode_signature (acfg
, (MonoMethodSignature
*)patch_info
->data
.gsharedvt
->sig
, p
, &p
);
5809 encode_method_ref (acfg
, patch_info
->data
.gsharedvt
->method
, p
, &p
);
5811 case MONO_PATCH_INFO_GSHAREDVT_METHOD
: {
5812 MonoGSharedVtMethodInfo
*info
= patch_info
->data
.gsharedvt_method
;
5815 encode_method_ref (acfg
, info
->method
, p
, &p
);
5816 encode_value (info
->num_entries
, p
, &p
);
5817 for (i
= 0; i
< info
->num_entries
; ++i
) {
5818 MonoRuntimeGenericContextInfoTemplate
*template_
= &info
->entries
[i
];
5820 encode_value (template_
->info_type
, p
, &p
);
5821 switch (mini_rgctx_info_type_to_patch_info_type (template_
->info_type
)) {
5822 case MONO_PATCH_INFO_CLASS
:
5823 encode_klass_ref (acfg
, mono_class_from_mono_type ((MonoType
*)template_
->data
), p
, &p
);
5825 case MONO_PATCH_INFO_FIELD
:
5826 encode_field_info (acfg
, (MonoClassField
*)template_
->data
, p
, &p
);
5829 g_assert_not_reached ();
5835 case MONO_PATCH_INFO_LDSTR_LIT
: {
5836 const char *s
= (const char *)patch_info
->data
.target
;
5837 int len
= strlen (s
);
5839 encode_value (len
, p
, &p
);
5840 memcpy (p
, s
, len
+ 1);
5844 case MONO_PATCH_INFO_VIRT_METHOD
:
5845 encode_klass_ref (acfg
, patch_info
->data
.virt_method
->klass
, p
, &p
);
5846 encode_method_ref (acfg
, patch_info
->data
.virt_method
->method
, p
, &p
);
5848 case MONO_PATCH_INFO_GC_SAFE_POINT_FLAG
:
5849 case MONO_PATCH_INFO_GET_TLS_TRAMP
:
5850 case MONO_PATCH_INFO_JIT_THREAD_ATTACH
:
5853 g_warning ("unable to handle jump info %d", patch_info
->type
);
5854 g_assert_not_reached ();
5861 encode_patch_list (MonoAotCompile
*acfg
, GPtrArray
*patches
, int n_patches
, gboolean llvm
, int first_got_offset
, guint8
*buf
, guint8
**endbuf
)
5864 guint32 pindex
, offset
;
5865 MonoJumpInfo
*patch_info
;
5867 encode_value (n_patches
, p
, &p
);
5869 for (pindex
= 0; pindex
< patches
->len
; ++pindex
) {
5870 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
5872 if (patch_info
->type
== MONO_PATCH_INFO_NONE
|| patch_info
->type
== MONO_PATCH_INFO_BB
)
5876 offset
= get_got_offset (acfg
, llvm
, patch_info
);
5877 encode_value (offset
, p
, &p
);
5884 emit_method_info (MonoAotCompile
*acfg
, MonoCompile
*cfg
)
5887 int pindex
, buf_size
, n_patches
;
5889 MonoJumpInfo
*patch_info
;
5890 guint32 method_index
;
5892 guint32 first_got_offset
;
5894 method
= cfg
->orig_method
;
5896 method_index
= get_method_index (acfg
, method
);
5898 /* Sort relocations */
5899 patches
= g_ptr_array_new ();
5900 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
)
5901 g_ptr_array_add (patches
, patch_info
);
5902 g_ptr_array_sort (patches
, compare_patches
);
5904 first_got_offset
= acfg
->cfgs
[method_index
]->got_offset
;
5906 /**********************/
5907 /* Encode method info */
5908 /**********************/
5910 buf_size
= (patches
->len
< 1000) ? 40960 : 40960 + (patches
->len
* 64);
5911 p
= buf
= (guint8
*)g_malloc (buf_size
);
5913 if (mono_class_get_cctor (method
->klass
)) {
5914 encode_value (1, p
, &p
);
5915 encode_klass_ref (acfg
, method
->klass
, p
, &p
);
5917 /* Not needed when loading the method */
5918 encode_value (0, p
, &p
);
5921 g_assert (!(cfg
->opt
& MONO_OPT_SHARED
));
5924 for (pindex
= 0; pindex
< patches
->len
; ++pindex
) {
5925 patch_info
= (MonoJumpInfo
*)g_ptr_array_index (patches
, pindex
);
5927 if ((patch_info
->type
== MONO_PATCH_INFO_GOT_OFFSET
) ||
5928 (patch_info
->type
== MONO_PATCH_INFO_NONE
)) {
5929 patch_info
->type
= MONO_PATCH_INFO_NONE
;
5934 if ((patch_info
->type
== MONO_PATCH_INFO_IMAGE
) && (patch_info
->data
.image
== acfg
->image
)) {
5935 /* Stored in a GOT slot initialized at module load time */
5936 patch_info
->type
= MONO_PATCH_INFO_NONE
;
5940 if (patch_info
->type
== MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
||
5941 patch_info
->type
== MONO_PATCH_INFO_GC_NURSERY_START
||
5942 patch_info
->type
== MONO_PATCH_INFO_GC_NURSERY_BITS
||
5943 patch_info
->type
== MONO_PATCH_INFO_AOT_MODULE
) {
5944 /* Stored in a GOT slot initialized at module load time */
5945 patch_info
->type
= MONO_PATCH_INFO_NONE
;
5949 if (is_plt_patch (patch_info
) && !(cfg
->compile_llvm
&& acfg
->aot_opts
.llvm_only
)) {
5950 /* Calls are made through the PLT */
5951 patch_info
->type
= MONO_PATCH_INFO_NONE
;
5959 g_assert (cfg
->has_got_slots
);
5961 encode_patch_list (acfg
, patches
, n_patches
, cfg
->compile_llvm
, first_got_offset
, p
, &p
);
5963 g_ptr_array_free (patches
, TRUE
);
5965 acfg
->stats
.info_size
+= p
- buf
;
5967 g_assert (p
- buf
< buf_size
);
5969 cfg
->method_info_offset
= add_to_blob (acfg
, buf
, p
- buf
);
5974 get_unwind_info_offset (MonoAotCompile
*acfg
, guint8
*encoded
, guint32 encoded_len
)
5976 guint32 cache_index
;
5979 /* Reuse the unwind module to canonize and store unwind info entries */
5980 cache_index
= mono_cache_unwind_info (encoded
, encoded_len
);
5982 /* Use +/- 1 to distinguish 0s from missing entries */
5983 offset
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->unwind_info_offsets
, GUINT_TO_POINTER (cache_index
+ 1)));
5991 * It would be easier to use assembler symbols, but the caller needs an
5994 offset
= acfg
->unwind_info_offset
;
5995 g_hash_table_insert (acfg
->unwind_info_offsets
, GUINT_TO_POINTER (cache_index
+ 1), GUINT_TO_POINTER (offset
+ 1));
5996 g_ptr_array_add (acfg
->unwind_ops
, GUINT_TO_POINTER (cache_index
));
5999 encode_value (encoded_len
, p
, &p
);
6001 acfg
->unwind_info_offset
+= encoded_len
+ (p
- buf
);
6007 emit_exception_debug_info (MonoAotCompile
*acfg
, MonoCompile
*cfg
, gboolean store_seq_points
)
6010 guint32 debug_info_size
, seq_points_size
;
6012 MonoMethodHeader
*header
;
6013 guint8
*p
, *buf
, *debug_info
;
6014 MonoJitInfo
*jinfo
= cfg
->jit_info
;
6016 gboolean use_unwind_ops
= FALSE
;
6017 MonoSeqPointInfo
*seq_points
;
6019 code
= cfg
->native_code
;
6020 header
= cfg
->header
;
6022 if (!acfg
->aot_opts
.nodebug
) {
6023 mono_debug_serialize_debug_info (cfg
, &debug_info
, &debug_info_size
);
6026 debug_info_size
= 0;
6029 seq_points
= cfg
->seq_point_info
;
6030 seq_points_size
= (store_seq_points
)? mono_seq_point_info_get_write_size (seq_points
) : 0;
6032 buf_size
= header
->num_clauses
* 256 + debug_info_size
+ 2048 + seq_points_size
+ cfg
->gc_map_size
;
6034 p
= buf
= (guint8
*)g_malloc (buf_size
);
6036 use_unwind_ops
= cfg
->unwind_ops
!= NULL
;
6038 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);
6040 encode_value (flags
, p
, &p
);
6042 if (use_unwind_ops
) {
6043 guint32 encoded_len
;
6045 guint32 unwind_desc
;
6047 encoded
= mono_unwind_ops_encode (cfg
->unwind_ops
, &encoded_len
);
6049 unwind_desc
= get_unwind_info_offset (acfg
, encoded
, encoded_len
);
6050 encode_value (unwind_desc
, p
, &p
);
6054 encode_value (jinfo
->unwind_info
, p
, &p
);
6057 /*Encode the number of holes before the number of clauses to make decoding easier*/
6058 if (jinfo
->has_try_block_holes
) {
6059 MonoTryBlockHoleTableJitInfo
*table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
6060 encode_value (table
->num_holes
, p
, &p
);
6063 if (jinfo
->has_arch_eh_info
) {
6065 * In AOT mode, the code length is calculated from the address of the previous method,
6066 * which could include alignment padding, so calculating the start of the epilog as
6067 * code_len - epilog_size is correct any more. Save the real code len as a workaround.
6069 encode_value (jinfo
->code_size
, p
, &p
);
6072 /* Exception table */
6073 if (cfg
->compile_llvm
) {
6075 * When using LLVM, we can't emit some data, like pc offsets, this reg/offset etc.,
6076 * since the information is only available to llc. Instead, we let llc save the data
6077 * into the LSDA, and read it from there at runtime.
6079 /* The assembly might be CIL stripped so emit the data ourselves */
6080 if (header
->num_clauses
)
6081 encode_value (header
->num_clauses
, p
, &p
);
6083 for (k
= 0; k
< header
->num_clauses
; ++k
) {
6084 MonoExceptionClause
*clause
;
6086 clause
= &header
->clauses
[k
];
6088 encode_value (clause
->flags
, p
, &p
);
6089 if (clause
->data
.catch_class
) {
6090 encode_value (1, p
, &p
);
6091 encode_klass_ref (acfg
, clause
->data
.catch_class
, p
, &p
);
6093 encode_value (0, p
, &p
);
6096 /* Emit the IL ranges too, since they might not be available at runtime */
6097 encode_value (clause
->try_offset
, p
, &p
);
6098 encode_value (clause
->try_len
, p
, &p
);
6099 encode_value (clause
->handler_offset
, p
, &p
);
6100 encode_value (clause
->handler_len
, p
, &p
);
6102 /* Emit a list of nesting clauses */
6103 for (i
= 0; i
< header
->num_clauses
; ++i
) {
6105 MonoExceptionClause
*clause1
= &header
->clauses
[cindex1
];
6107 MonoExceptionClause
*clause2
= &header
->clauses
[cindex2
];
6109 if (cindex1
!= cindex2
&& clause1
->try_offset
>= clause2
->try_offset
&& clause1
->handler_offset
<= clause2
->handler_offset
)
6110 encode_value (i
, p
, &p
);
6112 encode_value (-1, p
, &p
);
6115 if (jinfo
->num_clauses
)
6116 encode_value (jinfo
->num_clauses
, p
, &p
);
6118 for (k
= 0; k
< jinfo
->num_clauses
; ++k
) {
6119 MonoJitExceptionInfo
*ei
= &jinfo
->clauses
[k
];
6121 encode_value (ei
->flags
, p
, &p
);
6122 #ifdef MONO_CONTEXT_SET_LLVM_EXC_REG
6123 /* Not used for catch clauses */
6124 if (ei
->flags
!= MONO_EXCEPTION_CLAUSE_NONE
)
6125 encode_value (ei
->exvar_offset
, p
, &p
);
6127 encode_value (ei
->exvar_offset
, p
, &p
);
6130 if (ei
->flags
== MONO_EXCEPTION_CLAUSE_FILTER
|| ei
->flags
== MONO_EXCEPTION_CLAUSE_FINALLY
)
6131 encode_value ((gint
)((guint8
*)ei
->data
.filter
- code
), p
, &p
);
6133 if (ei
->data
.catch_class
) {
6137 buf2
= (guint8
*)g_malloc (4096);
6139 encode_klass_ref (acfg
, ei
->data
.catch_class
, p2
, &p2
);
6141 g_assert (len
< 4096);
6142 encode_value (len
, p
, &p
);
6143 memcpy (p
, buf2
, len
);
6147 encode_value (0, p
, &p
);
6151 encode_value ((gint
)((guint8
*)ei
->try_start
- code
), p
, &p
);
6152 encode_value ((gint
)((guint8
*)ei
->try_end
- code
), p
, &p
);
6153 encode_value ((gint
)((guint8
*)ei
->handler_start
- code
), p
, &p
);
6157 if (jinfo
->has_try_block_holes
) {
6158 MonoTryBlockHoleTableJitInfo
*table
= mono_jit_info_get_try_block_hole_table_info (jinfo
);
6159 for (i
= 0; i
< table
->num_holes
; ++i
) {
6160 MonoTryBlockHoleJitInfo
*hole
= &table
->holes
[i
];
6161 encode_value (hole
->clause
, p
, &p
);
6162 encode_value (hole
->length
, p
, &p
);
6163 encode_value (hole
->offset
, p
, &p
);
6167 if (jinfo
->has_arch_eh_info
) {
6168 MonoArchEHJitInfo
*eh_info
;
6170 eh_info
= mono_jit_info_get_arch_eh_info (jinfo
);
6171 encode_value (eh_info
->stack_size
, p
, &p
);
6172 encode_value (eh_info
->epilog_size
, p
, &p
);
6175 if (jinfo
->has_generic_jit_info
) {
6176 MonoGenericJitInfo
*gi
= mono_jit_info_get_generic_jit_info (jinfo
);
6177 MonoGenericSharingContext
* gsctx
= gi
->generic_sharing_context
;
6181 encode_value (gi
->nlocs
, p
, &p
);
6183 for (i
= 0; i
< gi
->nlocs
; ++i
) {
6184 MonoDwarfLocListEntry
*entry
= &gi
->locations
[i
];
6186 encode_value (entry
->is_reg
? 1 : 0, p
, &p
);
6187 encode_value (entry
->reg
, p
, &p
);
6189 encode_value (entry
->offset
, p
, &p
);
6191 g_assert (entry
->from
== 0);
6193 encode_value (entry
->from
, p
, &p
);
6194 encode_value (entry
->to
, p
, &p
);
6197 if (!cfg
->compile_llvm
) {
6198 encode_value (gi
->has_this
? 1 : 0, p
, &p
);
6199 encode_value (gi
->this_reg
, p
, &p
);
6200 encode_value (gi
->this_offset
, p
, &p
);
6205 * Need to encode jinfo->method too, since it is not equal to 'method'
6206 * when using generic sharing.
6208 buf2
= (guint8
*)g_malloc (4096);
6210 encode_method_ref (acfg
, jinfo
->d
.method
, p2
, &p2
);
6212 g_assert (len
< 4096);
6213 encode_value (len
, p
, &p
);
6214 memcpy (p
, buf2
, len
);
6218 if (gsctx
&& gsctx
->is_gsharedvt
) {
6219 encode_value (1, p
, &p
);
6221 encode_value (0, p
, &p
);
6225 if (seq_points_size
)
6226 p
+= mono_seq_point_info_write (seq_points
, p
);
6228 g_assert (debug_info_size
< buf_size
);
6230 encode_value (debug_info_size
, p
, &p
);
6231 if (debug_info_size
) {
6232 memcpy (p
, debug_info
, debug_info_size
);
6233 p
+= debug_info_size
;
6234 g_free (debug_info
);
6239 encode_value (cfg
->gc_map_size
, p
, &p
);
6240 /* The GC map requires 4 bytes of alignment */
6241 while ((gsize
)p
% 4)
6243 memcpy (p
, cfg
->gc_map
, cfg
->gc_map_size
);
6244 p
+= cfg
->gc_map_size
;
6247 acfg
->stats
.ex_info_size
+= p
- buf
;
6249 g_assert (p
- buf
< buf_size
);
6252 /* The GC Map requires 4 byte alignment */
6253 cfg
->ex_info_offset
= add_to_blob_aligned (acfg
, buf
, p
- buf
, cfg
->gc_map
? 4 : 1);
6258 emit_klass_info (MonoAotCompile
*acfg
, guint32 token
)
6261 MonoClass
*klass
= mono_class_get_checked (acfg
->image
, token
, &error
);
6263 int i
, buf_size
, res
;
6264 gboolean no_special_static
, cant_encode
;
6265 gpointer iter
= NULL
;
6268 mono_error_cleanup (&error
);
6272 p
= buf
= (guint8
*)g_malloc (buf_size
);
6274 /* Mark as unusable */
6275 encode_value (-1, p
, &p
);
6277 res
= add_to_blob (acfg
, buf
, p
- buf
);
6283 buf_size
= 10240 + (klass
->vtable_size
* 16);
6284 p
= buf
= (guint8
*)g_malloc (buf_size
);
6288 mono_class_init (klass
);
6290 mono_class_get_nested_types (klass
, &iter
);
6291 g_assert (klass
->nested_classes_inited
);
6293 mono_class_setup_vtable (klass
);
6296 * Emit all the information which is required for creating vtables so
6297 * the runtime does not need to create the MonoMethod structures which
6298 * take up a lot of space.
6301 no_special_static
= !mono_class_has_special_static_fields (klass
);
6303 /* Check whenever we have enough info to encode the vtable */
6304 cant_encode
= FALSE
;
6305 for (i
= 0; i
< klass
->vtable_size
; ++i
) {
6306 MonoMethod
*cm
= klass
->vtable
[i
];
6308 if (cm
&& mono_method_signature (cm
)->is_inflated
&& !g_hash_table_lookup (acfg
->token_info_hash
, cm
))
6312 mono_class_has_finalizer (klass
);
6313 if (mono_class_has_failure (klass
))
6316 if (mono_class_is_gtd (klass
) || cant_encode
) {
6317 encode_value (-1, p
, &p
);
6319 gboolean has_nested
= mono_class_get_nested_classes_property (klass
) != NULL
;
6320 encode_value (klass
->vtable_size
, p
, &p
);
6321 encode_value ((mono_class_is_gtd (klass
) ? (1 << 8) : 0) | (no_special_static
<< 7) | (klass
->has_static_refs
<< 6) | (klass
->has_references
<< 5) | ((klass
->blittable
<< 4) | (has_nested
? 1 : 0) << 3) | (klass
->has_cctor
<< 2) | (klass
->has_finalize
<< 1) | klass
->ghcimpl
, p
, &p
);
6322 if (klass
->has_cctor
)
6323 encode_method_ref (acfg
, mono_class_get_cctor (klass
), p
, &p
);
6324 if (klass
->has_finalize
)
6325 encode_method_ref (acfg
, mono_class_get_finalizer (klass
), p
, &p
);
6327 encode_value (klass
->instance_size
, p
, &p
);
6328 encode_value (mono_class_data_size (klass
), p
, &p
);
6329 encode_value (klass
->packing_size
, p
, &p
);
6330 encode_value (klass
->min_align
, p
, &p
);
6332 for (i
= 0; i
< klass
->vtable_size
; ++i
) {
6333 MonoMethod
*cm
= klass
->vtable
[i
];
6336 encode_method_ref (acfg
, cm
, p
, &p
);
6338 encode_value (0, p
, &p
);
6342 acfg
->stats
.class_info_size
+= p
- buf
;
6344 g_assert (p
- buf
< buf_size
);
6345 res
= add_to_blob (acfg
, buf
, p
- buf
);
6352 get_plt_entry_debug_sym (MonoAotCompile
*acfg
, MonoJumpInfo
*ji
, GHashTable
*cache
)
6354 char *debug_sym
= NULL
;
6357 if (acfg
->llvm
&& llvm_acfg
->aot_opts
.static_link
) {
6358 /* Need to add a prefix to create unique symbols */
6359 prefix
= g_strdup_printf ("plt_%s_", acfg
->assembly_name_sym
);
6361 #if defined(TARGET_WIN32) && defined(TARGET_X86)
6362 prefix
= mangle_symbol_alloc ("plt_");
6364 prefix
= g_strdup ("plt_");
6369 case MONO_PATCH_INFO_METHOD
:
6370 debug_sym
= get_debug_sym (ji
->data
.method
, prefix
, cache
);
6372 case MONO_PATCH_INFO_INTERNAL_METHOD
:
6373 debug_sym
= g_strdup_printf ("%s_jit_icall_%s", prefix
, ji
->data
.name
);
6375 case MONO_PATCH_INFO_RGCTX_FETCH
:
6376 debug_sym
= g_strdup_printf ("%s_rgctx_fetch_%d", prefix
, acfg
->label_generator
++);
6378 case MONO_PATCH_INFO_ICALL_ADDR
:
6379 case MONO_PATCH_INFO_ICALL_ADDR_CALL
: {
6380 char *s
= get_debug_sym (ji
->data
.method
, "", cache
);
6382 debug_sym
= g_strdup_printf ("%s_icall_native_%s", prefix
, s
);
6386 case MONO_PATCH_INFO_JIT_ICALL_ADDR
:
6387 debug_sym
= g_strdup_printf ("%s_jit_icall_native_%s", prefix
, ji
->data
.name
);
6395 return sanitize_symbol (acfg
, debug_sym
);
6399 * Calls made from AOTed code are routed through a table of jumps similar to the
6400 * ELF PLT (Program Linkage Table). Initially the PLT entries jump to code which transfers
6401 * control to the AOT runtime through a trampoline.
6404 emit_plt (MonoAotCompile
*acfg
)
6408 if (acfg
->aot_opts
.llvm_only
) {
6409 g_assert (acfg
->plt_offset
== 1);
6415 emit_section_change (acfg
, ".text", 0);
6416 emit_alignment_code (acfg
, 16);
6417 emit_info_symbol (acfg
, "plt");
6418 emit_label (acfg
, acfg
->plt_symbol
);
6420 for (i
= 0; i
< acfg
->plt_offset
; ++i
) {
6421 char *debug_sym
= NULL
;
6422 MonoPltEntry
*plt_entry
= NULL
;
6426 * The first plt entry is unused.
6430 plt_entry
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
6432 debug_sym
= plt_entry
->debug_sym
;
6434 if (acfg
->thumb_mixed
&& !plt_entry
->jit_used
)
6435 /* Emit only a thumb version */
6438 /* Skip plt entries not actually called */
6439 if (!plt_entry
->jit_used
&& !plt_entry
->llvm_used
)
6442 if (acfg
->llvm
&& !acfg
->thumb_mixed
) {
6443 emit_label (acfg
, plt_entry
->llvm_symbol
);
6445 emit_global_inner (acfg
, plt_entry
->llvm_symbol
, TRUE
);
6446 #if defined(TARGET_MACH)
6447 fprintf (acfg
->fp
, ".private_extern %s\n", plt_entry
->llvm_symbol
);
6453 if (acfg
->need_no_dead_strip
) {
6454 emit_unset_mode (acfg
);
6455 fprintf (acfg
->fp
, " .no_dead_strip %s\n", debug_sym
);
6457 emit_local_symbol (acfg
, debug_sym
, NULL
, TRUE
);
6458 emit_label (acfg
, debug_sym
);
6461 emit_label (acfg
, plt_entry
->symbol
);
6463 arch_emit_plt_entry (acfg
, acfg
->got_symbol
, (acfg
->plt_got_offset_base
+ i
) * sizeof (gpointer
), acfg
->plt_got_info_offsets
[i
]);
6466 emit_symbol_size (acfg
, debug_sym
, ".");
6469 if (acfg
->thumb_mixed
) {
6470 /* Make sure the ARM symbols don't alias the thumb ones */
6471 emit_zero_bytes (acfg
, 16);
6474 * Emit a separate set of PLT entries using thumb2 which is called by LLVM generated
6477 for (i
= 0; i
< acfg
->plt_offset
; ++i
) {
6478 char *debug_sym
= NULL
;
6479 MonoPltEntry
*plt_entry
= NULL
;
6484 plt_entry
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
6486 /* Skip plt entries not actually called by LLVM code */
6487 if (!plt_entry
->llvm_used
)
6490 if (acfg
->aot_opts
.write_symbols
) {
6491 if (plt_entry
->debug_sym
)
6492 debug_sym
= g_strdup_printf ("%s_thumb", plt_entry
->debug_sym
);
6496 #if defined(TARGET_MACH)
6497 fprintf (acfg
->fp
, " .thumb_func %s\n", debug_sym
);
6498 fprintf (acfg
->fp
, " .no_dead_strip %s\n", debug_sym
);
6500 emit_local_symbol (acfg
, debug_sym
, NULL
, TRUE
);
6501 emit_label (acfg
, debug_sym
);
6503 fprintf (acfg
->fp
, "\n.thumb_func\n");
6505 emit_label (acfg
, plt_entry
->llvm_symbol
);
6508 emit_global_inner (acfg
, plt_entry
->llvm_symbol
, TRUE
);
6510 arch_emit_llvm_plt_entry (acfg
, acfg
->got_symbol
, (acfg
->plt_got_offset_base
+ i
) * sizeof (gpointer
), acfg
->plt_got_info_offsets
[i
]);
6513 emit_symbol_size (acfg
, debug_sym
, ".");
6519 emit_symbol_size (acfg
, acfg
->plt_symbol
, ".");
6521 emit_info_symbol (acfg
, "plt_end");
6525 * emit_trampoline_full:
6527 * If EMIT_TINFO is TRUE, emit additional information which can be used to create a MonoJitInfo for this trampoline by
6528 * create_jit_info_for_trampoline ().
6530 static G_GNUC_UNUSED
void
6531 emit_trampoline_full (MonoAotCompile
*acfg
, int got_offset
, MonoTrampInfo
*info
, gboolean emit_tinfo
)
6533 char start_symbol
[MAX_SYMBOL_SIZE
];
6534 char end_symbol
[MAX_SYMBOL_SIZE
];
6535 char symbol
[MAX_SYMBOL_SIZE
];
6536 guint32 buf_size
, info_offset
;
6537 MonoJumpInfo
*patch_info
;
6550 code_size
= info
->code_size
;
6552 unwind_ops
= info
->unwind_ops
;
6556 sprintf (start_symbol
, "%s%s", acfg
->user_symbol_prefix
, name
);
6558 emit_section_change (acfg
, ".text", 0);
6559 emit_global (acfg
, start_symbol
, TRUE
);
6560 emit_alignment_code (acfg
, AOT_FUNC_ALIGNMENT
);
6561 emit_label (acfg
, start_symbol
);
6563 sprintf (symbol
, "%snamed_%s", acfg
->temp_prefix
, name
);
6564 emit_label (acfg
, symbol
);
6567 * The code should access everything through the GOT, so we pass
6570 emit_and_reloc_code (acfg
, NULL
, code
, code_size
, ji
, TRUE
, NULL
);
6572 emit_symbol_size (acfg
, start_symbol
, ".");
6575 sprintf (end_symbol
, "%snamede_%s", acfg
->temp_prefix
, name
);
6576 emit_label (acfg
, end_symbol
);
6581 /* Sort relocations */
6582 patches
= g_ptr_array_new ();
6583 for (patch_info
= ji
; patch_info
; patch_info
= patch_info
->next
)
6584 if (patch_info
->type
!= MONO_PATCH_INFO_NONE
)
6585 g_ptr_array_add (patches
, patch_info
);
6586 g_ptr_array_sort (patches
, compare_patches
);
6588 buf_size
= patches
->len
* 128 + 128;
6589 buf
= (guint8
*)g_malloc (buf_size
);
6592 encode_patch_list (acfg
, patches
, patches
->len
, FALSE
, got_offset
, p
, &p
);
6593 g_assert (p
- buf
< buf_size
);
6594 g_ptr_array_free (patches
, TRUE
);
6596 sprintf (symbol
, "%s%s_p", acfg
->user_symbol_prefix
, name
);
6598 info_offset
= add_to_blob (acfg
, buf
, p
- buf
);
6600 emit_section_change (acfg
, RODATA_SECT
, 0);
6601 emit_global (acfg
, symbol
, FALSE
);
6602 emit_label (acfg
, symbol
);
6604 emit_int32 (acfg
, info_offset
);
6608 guint32 encoded_len
;
6612 * Emit additional information which can be used to reconstruct a partial MonoTrampInfo.
6614 encoded
= mono_unwind_ops_encode (info
->unwind_ops
, &encoded_len
);
6615 uw_offset
= get_unwind_info_offset (acfg
, encoded
, encoded_len
);
6618 emit_symbol_diff (acfg
, end_symbol
, start_symbol
, 0);
6619 emit_int32 (acfg
, uw_offset
);
6622 /* Emit debug info */
6624 char symbol2
[MAX_SYMBOL_SIZE
];
6626 sprintf (symbol
, "%s", name
);
6627 sprintf (symbol2
, "%snamed_%s", acfg
->temp_prefix
, name
);
6630 mono_dwarf_writer_emit_trampoline (acfg
->dwarf
, symbol
, symbol2
, NULL
, NULL
, code_size
, unwind_ops
);
6636 static G_GNUC_UNUSED
void
6637 emit_trampoline (MonoAotCompile
*acfg
, int got_offset
, MonoTrampInfo
*info
)
6639 emit_trampoline_full (acfg
, got_offset
, info
, TRUE
);
6643 emit_trampolines (MonoAotCompile
*acfg
)
6645 char symbol
[MAX_SYMBOL_SIZE
];
6646 char end_symbol
[MAX_SYMBOL_SIZE
];
6647 int i
, tramp_got_offset
;
6649 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
6653 if (!mono_aot_mode_is_full (&acfg
->aot_opts
) || acfg
->aot_opts
.llvm_only
)
6656 g_assert (acfg
->image
->assembly
);
6658 /* Currently, we emit most trampolines into the mscorlib AOT image. */
6659 if (strcmp (acfg
->image
->assembly
->aname
.name
, "mscorlib") == 0) {
6660 #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
6661 MonoTrampInfo
*info
;
6664 * Emit the generic trampolines.
6666 * We could save some code by treating the generic trampolines as a wrapper
6667 * method, but that approach has its own complexities, so we choose the simpler
6670 for (tramp_type
= 0; tramp_type
< MONO_TRAMPOLINE_NUM
; ++tramp_type
) {
6671 /* we overload the boolean here to indicate the slightly different trampoline needed, see mono_arch_create_generic_trampoline() */
6672 #ifdef DISABLE_REMOTING
6673 if (tramp_type
== MONO_TRAMPOLINE_GENERIC_VIRTUAL_REMOTING
)
6676 #ifndef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD
6677 if (tramp_type
== MONO_TRAMPOLINE_HANDLER_BLOCK_GUARD
)
6680 mono_arch_create_generic_trampoline ((MonoTrampolineType
)tramp_type
, &info
, acfg
->aot_opts
.use_trampolines_page
? 2: TRUE
);
6681 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6684 /* Emit the exception related code pieces */
6685 mono_arch_get_restore_context (&info
, TRUE
);
6686 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6687 mono_arch_get_call_filter (&info
, TRUE
);
6688 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6689 mono_arch_get_throw_exception (&info
, TRUE
);
6690 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6691 mono_arch_get_rethrow_exception (&info
, TRUE
);
6692 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6693 mono_arch_get_throw_corlib_exception (&info
, TRUE
);
6694 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6696 #ifdef MONO_ARCH_HAVE_SDB_TRAMPOLINES
6697 mono_arch_create_sdb_trampoline (TRUE
, &info
, TRUE
);
6698 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6699 mono_arch_create_sdb_trampoline (FALSE
, &info
, TRUE
);
6700 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6703 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
6704 mono_arch_get_gsharedvt_trampoline (&info
, TRUE
);
6706 emit_trampoline_full (acfg
, acfg
->got_offset
, info
, TRUE
);
6708 /* Create a separate out trampoline for more information in stack traces */
6709 info
->name
= g_strdup ("gsharedvt_out_trampoline");
6710 emit_trampoline_full (acfg
, acfg
->got_offset
, info
, TRUE
);
6714 #if defined(MONO_ARCH_HAVE_GET_TRAMPOLINES)
6716 GSList
*l
= mono_arch_get_trampolines (TRUE
);
6719 MonoTrampInfo
*info
= (MonoTrampInfo
*)l
->data
;
6721 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6727 for (i
= 0; i
< acfg
->aot_opts
.nrgctx_fetch_trampolines
; ++i
) {
6730 offset
= MONO_RGCTX_SLOT_MAKE_RGCTX (i
);
6731 mono_arch_create_rgctx_lazy_fetch_trampoline (offset
, &info
, TRUE
);
6732 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6735 offset
= MONO_RGCTX_SLOT_MAKE_MRGCTX (i
);
6736 mono_arch_create_rgctx_lazy_fetch_trampoline (offset
, &info
, TRUE
);
6737 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6741 #ifdef MONO_ARCH_HAVE_GENERAL_RGCTX_LAZY_FETCH_TRAMPOLINE
6742 mono_arch_create_general_rgctx_lazy_fetch_trampoline (&info
, TRUE
);
6743 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6749 /* delegate_invoke_impl trampolines */
6750 l
= mono_arch_get_delegate_invoke_impls ();
6752 MonoTrampInfo
*info
= (MonoTrampInfo
*)l
->data
;
6754 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6759 #ifdef MONO_ARCH_HAVE_HANDLER_BLOCK_GUARD_AOT
6760 mono_arch_create_handler_block_trampoline (&info
, TRUE
);
6761 emit_trampoline (acfg
, acfg
->got_offset
, info
);
6764 #endif /* #ifdef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES */
6766 /* Emit trampolines which are numerous */
6769 * These include the following:
6770 * - specific trampolines
6771 * - static rgctx invoke trampolines
6773 * These trampolines have the same code, they are parameterized by GOT
6775 * They are defined in this file, in the arch_... routines instead of
6776 * in tramp-<ARCH>.c, since it is easier to do it this way.
6780 * When running in aot-only mode, we can't create specific trampolines at
6781 * runtime, so we create a few, and save them in the AOT file.
6782 * Normal trampolines embed their argument as a literal inside the
6783 * trampoline code, we can't do that here, so instead we embed an offset
6784 * which needs to be added to the trampoline address to get the address of
6785 * the GOT slot which contains the argument value.
6786 * The generated trampolines jump to the generic trampolines using another
6787 * GOT slot, which will be setup by the AOT loader to point to the
6788 * generic trampoline code of the given type.
6792 * FIXME: Maybe we should use more specific trampolines (i.e. one class init for
6796 emit_section_change (acfg
, ".text", 0);
6798 tramp_got_offset
= acfg
->got_offset
;
6800 for (ntype
= 0; ntype
< MONO_AOT_TRAMP_NUM
; ++ntype
) {
6802 case MONO_AOT_TRAMP_SPECIFIC
:
6803 sprintf (symbol
, "specific_trampolines");
6805 case MONO_AOT_TRAMP_STATIC_RGCTX
:
6806 sprintf (symbol
, "static_rgctx_trampolines");
6808 case MONO_AOT_TRAMP_IMT
:
6809 sprintf (symbol
, "imt_trampolines");
6811 case MONO_AOT_TRAMP_GSHAREDVT_ARG
:
6812 sprintf (symbol
, "gsharedvt_arg_trampolines");
6815 g_assert_not_reached ();
6818 sprintf (end_symbol
, "%s_e", symbol
);
6820 if (acfg
->aot_opts
.write_symbols
)
6821 emit_local_symbol (acfg
, symbol
, end_symbol
, TRUE
);
6823 emit_alignment_code (acfg
, AOT_FUNC_ALIGNMENT
);
6824 emit_info_symbol (acfg
, symbol
);
6826 acfg
->trampoline_got_offset_base
[ntype
] = tramp_got_offset
;
6828 for (i
= 0; i
< acfg
->num_trampolines
[ntype
]; ++i
) {
6832 case MONO_AOT_TRAMP_SPECIFIC
:
6833 arch_emit_specific_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
6834 tramp_got_offset
+= 2;
6836 case MONO_AOT_TRAMP_STATIC_RGCTX
:
6837 arch_emit_static_rgctx_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
6838 tramp_got_offset
+= 2;
6840 case MONO_AOT_TRAMP_IMT
:
6841 arch_emit_imt_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
6842 tramp_got_offset
+= 1;
6844 case MONO_AOT_TRAMP_GSHAREDVT_ARG
:
6845 arch_emit_gsharedvt_arg_trampoline (acfg
, tramp_got_offset
, &tramp_size
);
6846 tramp_got_offset
+= 2;
6849 g_assert_not_reached ();
6851 if (!acfg
->trampoline_size
[ntype
]) {
6852 g_assert (tramp_size
);
6853 acfg
->trampoline_size
[ntype
] = tramp_size
;
6857 emit_label (acfg
, end_symbol
);
6858 emit_int32 (acfg
, 0);
6861 arch_emit_specific_trampoline_pages (acfg
);
6863 /* Reserve some entries at the end of the GOT for our use */
6864 acfg
->num_trampoline_got_entries
= tramp_got_offset
- acfg
->got_offset
;
6867 acfg
->got_offset
+= acfg
->num_trampoline_got_entries
;
6871 str_begins_with (const char *str1
, const char *str2
)
6873 int len
= strlen (str2
);
6874 return strncmp (str1
, str2
, len
) == 0;
6878 mono_aot_readonly_field_override (MonoClassField
*field
)
6881 for (rdv
= readonly_values
; rdv
; rdv
= rdv
->next
) {
6882 char *p
= rdv
->name
;
6884 len
= strlen (field
->parent
->name_space
);
6885 if (strncmp (p
, field
->parent
->name_space
, len
))
6890 len
= strlen (field
->parent
->name
);
6891 if (strncmp (p
, field
->parent
->name
, len
))
6896 if (strcmp (p
, field
->name
))
6898 switch (rdv
->type
) {
6900 return &rdv
->value
.i1
;
6902 return &rdv
->value
.i2
;
6904 return &rdv
->value
.i4
;
6913 add_readonly_value (MonoAotOptions
*opts
, const char *val
)
6918 /* the format of val is:
6919 * namespace.typename.fieldname=type/value
6920 * type can be i1 for uint8/int8/boolean, i2 for uint16/int16/char, i4 for uint32/int32
6922 fval
= strrchr (val
, '/');
6924 fprintf (stderr
, "AOT : invalid format for readonly field '%s', missing /.\n", val
);
6927 tval
= strrchr (val
, '=');
6929 fprintf (stderr
, "AOT : invalid format for readonly field '%s', missing =.\n", val
);
6932 rdv
= g_new0 (ReadOnlyValue
, 1);
6933 rdv
->name
= (char *)g_malloc0 (tval
- val
+ 1);
6934 memcpy (rdv
->name
, val
, tval
- val
);
6937 if (strncmp (tval
, "i1", 2) == 0) {
6938 rdv
->value
.i1
= atoi (fval
);
6939 rdv
->type
= MONO_TYPE_I1
;
6940 } else if (strncmp (tval
, "i2", 2) == 0) {
6941 rdv
->value
.i2
= atoi (fval
);
6942 rdv
->type
= MONO_TYPE_I2
;
6943 } else if (strncmp (tval
, "i4", 2) == 0) {
6944 rdv
->value
.i4
= atoi (fval
);
6945 rdv
->type
= MONO_TYPE_I4
;
6947 fprintf (stderr
, "AOT : unsupported type for readonly field '%s'.\n", tval
);
6950 rdv
->next
= readonly_values
;
6951 readonly_values
= rdv
;
6955 clean_path (gchar
* path
)
6960 if (g_str_has_suffix (path
, G_DIR_SEPARATOR_S
))
6963 gchar
*clean
= g_strconcat (path
, G_DIR_SEPARATOR_S
, NULL
);
6970 wrap_path (gchar
* path
)
6976 // If the string contains no spaces, just return the original string.
6977 if (strstr (path
, " ") == NULL
)
6980 // If the string is already wrapped in quotes, return it.
6981 len
= strlen (path
);
6982 if (len
>= 2 && path
[0] == '\"' && path
[len
-1] == '\"')
6985 // If the string contains spaces, then wrap it in quotes.
6986 gchar
*clean
= g_strdup_printf ("\"%s\"", path
);
6991 // Duplicate a char range and add it to a ptrarray, but only if it is nonempty
6993 ptr_array_add_range_if_nonempty(GPtrArray
*args
, gchar
const *start
, gchar
const *end
)
6995 ptrdiff_t len
= end
-start
;
6997 g_ptr_array_add (args
, g_strndup (start
, len
));
7001 mono_aot_split_options (const char *aot_options
)
7003 enum MonoAotOptionState
{
7004 MONO_AOT_OPTION_STATE_DEFAULT
,
7005 MONO_AOT_OPTION_STATE_STRING
,
7006 MONO_AOT_OPTION_STATE_ESCAPE
,
7009 GPtrArray
*args
= g_ptr_array_new ();
7010 enum MonoAotOptionState state
= MONO_AOT_OPTION_STATE_DEFAULT
;
7011 gchar
const *opt_start
= aot_options
;
7012 gboolean end_of_string
= FALSE
;
7015 g_return_val_if_fail (aot_options
!= NULL
, NULL
);
7017 while ((cur
= *aot_options
) != '\0') {
7018 if (state
== MONO_AOT_OPTION_STATE_ESCAPE
)
7023 // If we find a quote, then if we're in the default case then
7024 // it means we've found the start of a string, if not then it
7025 // means we've found the end of the string and should switch
7026 // back to the default case.
7028 case MONO_AOT_OPTION_STATE_DEFAULT
:
7029 state
= MONO_AOT_OPTION_STATE_STRING
;
7031 case MONO_AOT_OPTION_STATE_STRING
:
7032 state
= MONO_AOT_OPTION_STATE_DEFAULT
;
7034 case MONO_AOT_OPTION_STATE_ESCAPE
:
7035 g_assert_not_reached ();
7040 // If we've found an escaping operator, then this means we
7041 // should not process the next character if inside a string.
7042 if (state
== MONO_AOT_OPTION_STATE_STRING
)
7043 state
= MONO_AOT_OPTION_STATE_ESCAPE
;
7046 // If we're in the default state then this means we've found
7047 // an option, store it for later processing.
7048 if (state
== MONO_AOT_OPTION_STATE_DEFAULT
)
7056 // If the next character is end of string, then process the last option.
7057 if (*(aot_options
) == '\0') {
7058 end_of_string
= TRUE
;
7064 ptr_array_add_range_if_nonempty (args
, opt_start
, aot_options
);
7065 opt_start
= ++aot_options
;
7068 goto restart
; // Check for null and continue loop
7075 mono_aot_parse_options (const char *aot_options
, MonoAotOptions
*opts
)
7079 args
= mono_aot_split_options (aot_options
? aot_options
: "");
7080 for (int i
= 0; i
< args
->len
; ++i
) {
7081 const char *arg
= (const char *)g_ptr_array_index (args
, i
);
7083 if (str_begins_with (arg
, "outfile=")) {
7084 opts
->outfile
= g_strdup (arg
+ strlen ("outfile="));
7085 } else if (str_begins_with (arg
, "llvm-outfile=")) {
7086 opts
->llvm_outfile
= g_strdup (arg
+ strlen ("llvm-outfile="));
7087 } else if (str_begins_with (arg
, "temp-path=")) {
7088 opts
->temp_path
= clean_path (g_strdup (arg
+ strlen ("temp-path=")));
7089 } else if (str_begins_with (arg
, "save-temps")) {
7090 opts
->save_temps
= TRUE
;
7091 } else if (str_begins_with (arg
, "keep-temps")) {
7092 opts
->save_temps
= TRUE
;
7093 } else if (str_begins_with (arg
, "write-symbols")) {
7094 opts
->write_symbols
= TRUE
;
7095 } else if (str_begins_with (arg
, "no-write-symbols")) {
7096 opts
->write_symbols
= FALSE
;
7097 } else if (str_begins_with (arg
, "metadata-only")) {
7098 opts
->metadata_only
= TRUE
;
7099 } else if (str_begins_with (arg
, "bind-to-runtime-version")) {
7100 opts
->bind_to_runtime_version
= TRUE
;
7101 } else if (str_begins_with (arg
, "full")) {
7102 opts
->mode
= MONO_AOT_MODE_FULL
;
7103 } else if (str_begins_with (arg
, "hybrid")) {
7104 opts
->mode
= MONO_AOT_MODE_HYBRID
;
7105 } else if (str_begins_with (arg
, "threads=")) {
7106 opts
->nthreads
= atoi (arg
+ strlen ("threads="));
7107 } else if (str_begins_with (arg
, "static")) {
7108 opts
->static_link
= TRUE
;
7109 opts
->no_dlsym
= TRUE
;
7110 } else if (str_begins_with (arg
, "asmonly")) {
7111 opts
->asm_only
= TRUE
;
7112 } else if (str_begins_with (arg
, "asmwriter")) {
7113 opts
->asm_writer
= TRUE
;
7114 } else if (str_begins_with (arg
, "nodebug")) {
7115 opts
->nodebug
= TRUE
;
7116 } else if (str_begins_with (arg
, "dwarfdebug")) {
7117 opts
->dwarf_debug
= TRUE
;
7118 } else if (str_begins_with (arg
, "nopagetrampolines")) {
7119 opts
->use_trampolines_page
= FALSE
;
7120 } else if (str_begins_with (arg
, "ntrampolines=")) {
7121 opts
->ntrampolines
= atoi (arg
+ strlen ("ntrampolines="));
7122 } else if (str_begins_with (arg
, "nrgctx-trampolines=")) {
7123 opts
->nrgctx_trampolines
= atoi (arg
+ strlen ("nrgctx-trampolines="));
7124 } else if (str_begins_with (arg
, "nrgctx-fetch-trampolines=")) {
7125 opts
->nrgctx_fetch_trampolines
= atoi (arg
+ strlen ("nrgctx-fetch-trampolines="));
7126 } else if (str_begins_with (arg
, "nimt-trampolines=")) {
7127 opts
->nimt_trampolines
= atoi (arg
+ strlen ("nimt-trampolines="));
7128 } else if (str_begins_with (arg
, "ngsharedvt-trampolines=")) {
7129 opts
->ngsharedvt_arg_trampolines
= atoi (arg
+ strlen ("ngsharedvt-trampolines="));
7130 } else if (str_begins_with (arg
, "tool-prefix=")) {
7131 opts
->tool_prefix
= g_strdup (arg
+ strlen ("tool-prefix="));
7132 } else if (str_begins_with (arg
, "ld-flags=")) {
7133 opts
->ld_flags
= g_strdup (arg
+ strlen ("ld-flags="));
7134 } else if (str_begins_with (arg
, "soft-debug")) {
7135 opts
->soft_debug
= TRUE
;
7136 } else if (str_begins_with (arg
, "gen-seq-points-file=")) {
7137 fprintf (stderr
, "Mono Warning: aot option gen-seq-points-file= is deprecated.\n");
7138 } else if (str_begins_with (arg
, "gen-seq-points-file")) {
7139 fprintf (stderr
, "Mono Warning: aot option gen-seq-points-file is deprecated.\n");
7140 } else if (str_begins_with (arg
, "msym-dir=")) {
7141 debug_options
.no_seq_points_compact_data
= FALSE
;
7142 opts
->gen_msym_dir
= TRUE
;
7143 opts
->gen_msym_dir_path
= g_strdup (arg
+ strlen ("msym_dir="));;
7144 } else if (str_begins_with (arg
, "direct-pinvoke")) {
7145 opts
->direct_pinvoke
= TRUE
;
7146 } else if (str_begins_with (arg
, "direct-icalls")) {
7147 opts
->direct_icalls
= TRUE
;
7148 } else if (str_begins_with (arg
, "no-direct-calls")) {
7149 opts
->no_direct_calls
= TRUE
;
7150 } else if (str_begins_with (arg
, "print-skipped")) {
7151 opts
->print_skipped_methods
= TRUE
;
7152 } else if (str_begins_with (arg
, "stats")) {
7154 } else if (str_begins_with (arg
, "no-instances")) {
7155 opts
->no_instances
= TRUE
;
7156 } else if (str_begins_with (arg
, "log-generics")) {
7157 opts
->log_generics
= TRUE
;
7158 } else if (str_begins_with (arg
, "log-instances=")) {
7159 opts
->log_instances
= TRUE
;
7160 opts
->instances_logfile_path
= g_strdup (arg
+ strlen ("log-instances="));
7161 } else if (str_begins_with (arg
, "log-instances")) {
7162 opts
->log_instances
= TRUE
;
7163 } else if (str_begins_with (arg
, "internal-logfile=")) {
7164 opts
->logfile
= g_strdup (arg
+ strlen ("internal-logfile="));
7165 } else if (str_begins_with (arg
, "mtriple=")) {
7166 opts
->mtriple
= g_strdup (arg
+ strlen ("mtriple="));
7167 } else if (str_begins_with (arg
, "llvm-path=")) {
7168 opts
->llvm_path
= clean_path (g_strdup (arg
+ strlen ("llvm-path=")));
7169 } else if (!strcmp (arg
, "llvm")) {
7171 } else if (str_begins_with (arg
, "readonly-value=")) {
7172 add_readonly_value (opts
, arg
+ strlen ("readonly-value="));
7173 } else if (str_begins_with (arg
, "info")) {
7174 printf ("AOT target setup: %s.\n", AOT_TARGET_STR
);
7176 } else if (str_begins_with (arg
, "gc-maps")) {
7177 mini_gc_enable_gc_maps_for_aot ();
7178 } else if (str_begins_with (arg
, "dump")) {
7179 opts
->dump_json
= TRUE
;
7180 } else if (str_begins_with (arg
, "llvmonly")) {
7181 opts
->mode
= MONO_AOT_MODE_FULL
;
7183 opts
->llvm_only
= TRUE
;
7184 } else if (str_begins_with (arg
, "data-outfile=")) {
7185 opts
->data_outfile
= g_strdup (arg
+ strlen ("data-outfile="));
7186 } else if (str_begins_with (arg
, "help") || str_begins_with (arg
, "?")) {
7187 printf ("Supported options for --aot:\n");
7188 printf (" outfile=\n");
7189 printf (" llvm-outfile=\n");
7190 printf (" llvm-path=\n");
7191 printf (" temp-path=\n");
7192 printf (" save-temps\n");
7193 printf (" keep-temps\n");
7194 printf (" write-symbols\n");
7195 printf (" metadata-only\n");
7196 printf (" bind-to-runtime-version\n");
7198 printf (" threads=\n");
7199 printf (" static\n");
7200 printf (" asmonly\n");
7201 printf (" asmwriter\n");
7202 printf (" nodebug\n");
7203 printf (" dwarfdebug\n");
7204 printf (" ntrampolines=\n");
7205 printf (" nrgctx-trampolines=\n");
7206 printf (" nimt-trampolines=\n");
7207 printf (" ngsharedvt-trampolines=\n");
7208 printf (" tool-prefix=\n");
7209 printf (" readonly-value=\n");
7210 printf (" soft-debug\n");
7211 printf (" msym-dir=\n");
7212 printf (" gc-maps\n");
7213 printf (" print-skipped\n");
7214 printf (" no-instances\n");
7215 printf (" stats\n");
7218 printf (" help/?\n");
7221 fprintf (stderr
, "AOT : Unknown argument '%s'.\n", arg
);
7225 g_free ((gpointer
) arg
);
7228 if (opts
->use_trampolines_page
) {
7229 opts
->ntrampolines
= 0;
7230 opts
->nrgctx_trampolines
= 0;
7231 opts
->nimt_trampolines
= 0;
7232 opts
->ngsharedvt_arg_trampolines
= 0;
7235 g_ptr_array_free (args
, /*free_seg=*/TRUE
);
7239 add_token_info_hash (gpointer key
, gpointer value
, gpointer user_data
)
7241 MonoMethod
*method
= (MonoMethod
*)key
;
7242 MonoJumpInfoToken
*ji
= (MonoJumpInfoToken
*)value
;
7243 MonoAotCompile
*acfg
= (MonoAotCompile
*)user_data
;
7244 MonoJumpInfoToken
*new_ji
;
7246 new_ji
= (MonoJumpInfoToken
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfoToken
));
7247 new_ji
->image
= ji
->image
;
7248 new_ji
->token
= ji
->token
;
7249 g_hash_table_insert (acfg
->token_info_hash
, method
, new_ji
);
7253 can_encode_class (MonoAotCompile
*acfg
, MonoClass
*klass
)
7255 if (klass
->type_token
)
7257 if ((klass
->byval_arg
.type
== MONO_TYPE_VAR
) || (klass
->byval_arg
.type
== MONO_TYPE_MVAR
) || (klass
->byval_arg
.type
== MONO_TYPE_PTR
))
7260 return can_encode_class (acfg
, klass
->element_class
);
7265 can_encode_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
7267 if (method
->wrapper_type
) {
7268 switch (method
->wrapper_type
) {
7269 case MONO_WRAPPER_NONE
:
7270 case MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
:
7271 case MONO_WRAPPER_XDOMAIN_INVOKE
:
7272 case MONO_WRAPPER_STFLD
:
7273 case MONO_WRAPPER_LDFLD
:
7274 case MONO_WRAPPER_LDFLDA
:
7275 case MONO_WRAPPER_STELEMREF
:
7276 case MONO_WRAPPER_ISINST
:
7277 case MONO_WRAPPER_PROXY_ISINST
:
7278 case MONO_WRAPPER_ALLOC
:
7279 case MONO_WRAPPER_REMOTING_INVOKE
:
7280 case MONO_WRAPPER_UNKNOWN
:
7281 case MONO_WRAPPER_WRITE_BARRIER
:
7282 case MONO_WRAPPER_DELEGATE_INVOKE
:
7283 case MONO_WRAPPER_DELEGATE_BEGIN_INVOKE
:
7284 case MONO_WRAPPER_DELEGATE_END_INVOKE
:
7285 case MONO_WRAPPER_SYNCHRONIZED
:
7287 case MONO_WRAPPER_MANAGED_TO_MANAGED
:
7288 case MONO_WRAPPER_CASTCLASS
: {
7289 WrapperInfo
*info
= mono_marshal_get_wrapper_info (method
);
7298 //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
7302 if (!method
->token
) {
7303 /* The method is part of a constructed type like Int[,].Set (). */
7304 if (!g_hash_table_lookup (acfg
->token_info_hash
, method
)) {
7305 if (method
->klass
->rank
)
7315 can_encode_patch (MonoAotCompile
*acfg
, MonoJumpInfo
*patch_info
)
7317 switch (patch_info
->type
) {
7318 case MONO_PATCH_INFO_METHOD
:
7319 case MONO_PATCH_INFO_METHODCONST
:
7320 case MONO_PATCH_INFO_METHOD_CODE_SLOT
: {
7321 MonoMethod
*method
= patch_info
->data
.method
;
7323 return can_encode_method (acfg
, method
);
7325 case MONO_PATCH_INFO_VTABLE
:
7326 case MONO_PATCH_INFO_CLASS
:
7327 case MONO_PATCH_INFO_IID
:
7328 case MONO_PATCH_INFO_ADJUSTED_IID
:
7329 if (!can_encode_class (acfg
, patch_info
->data
.klass
)) {
7330 //printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
7334 case MONO_PATCH_INFO_DELEGATE_TRAMPOLINE
: {
7335 if (!can_encode_class (acfg
, patch_info
->data
.del_tramp
->klass
)) {
7336 //printf ("Skip: %s\n", mono_type_full_name (&patch_info->data.klass->byval_arg));
7341 case MONO_PATCH_INFO_RGCTX_FETCH
:
7342 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
: {
7343 MonoJumpInfoRgctxEntry
*entry
= patch_info
->data
.rgctx_entry
;
7345 if (!can_encode_method (acfg
, entry
->method
))
7347 if (!can_encode_patch (acfg
, entry
->data
))
7359 is_concrete_type (MonoType
*t
)
7364 if (t
->type
== MONO_TYPE_VAR
|| t
->type
== MONO_TYPE_MVAR
)
7366 if (t
->type
== MONO_TYPE_GENERICINST
) {
7367 MonoGenericContext
*orig_ctx
;
7368 MonoGenericInst
*inst
;
7371 if (!MONO_TYPE_ISSTRUCT (t
))
7373 klass
= mono_class_from_mono_type (t
);
7374 orig_ctx
= &mono_class_get_generic_class (klass
)->context
;
7376 inst
= orig_ctx
->class_inst
;
7378 for (i
= 0; i
< inst
->type_argc
; ++i
) {
7379 arg
= mini_get_underlying_type (inst
->type_argv
[i
]);
7380 if (!is_concrete_type (arg
))
7384 inst
= orig_ctx
->method_inst
;
7386 for (i
= 0; i
< inst
->type_argc
; ++i
) {
7387 arg
= mini_get_underlying_type (inst
->type_argv
[i
]);
7388 if (!is_concrete_type (arg
))
7396 /* LOCKING: Assumes the loader lock is held */
7398 add_gsharedvt_wrappers (MonoAotCompile
*acfg
, MonoMethodSignature
*sig
, gboolean gsharedvt_in
, gboolean gsharedvt_out
)
7400 MonoMethod
*wrapper
;
7401 gboolean concrete
= TRUE
;
7402 gboolean add_in
= gsharedvt_in
;
7403 gboolean add_out
= gsharedvt_out
;
7405 if (gsharedvt_in
&& g_hash_table_lookup (acfg
->gsharedvt_in_signatures
, sig
))
7407 if (gsharedvt_out
&& g_hash_table_lookup (acfg
->gsharedvt_out_signatures
, sig
))
7410 if (!add_in
&& !add_out
)
7413 if (mini_is_gsharedvt_variable_signature (sig
))
7417 g_hash_table_insert (acfg
->gsharedvt_in_signatures
, sig
, sig
);
7419 g_hash_table_insert (acfg
->gsharedvt_out_signatures
, sig
, sig
);
7421 if (!sig
->has_type_parameters
) {
7422 //printf ("%s\n", mono_signature_full_name (sig));
7425 wrapper
= mini_get_gsharedvt_in_sig_wrapper (sig
);
7426 add_extra_method (acfg
, wrapper
);
7428 if (gsharedvt_out
) {
7429 wrapper
= mini_get_gsharedvt_out_sig_wrapper (sig
);
7430 add_extra_method (acfg
, wrapper
);
7433 /* For signatures creared during generic sharing, convert them to a concrete signature if possible */
7434 MonoMethodSignature
*copy
= mono_metadata_signature_dup (sig
);
7437 //printf ("%s\n", mono_signature_full_name (sig));
7439 copy
->ret
= mini_get_underlying_type (sig
->ret
);
7440 if (!is_concrete_type (copy
->ret
))
7442 for (i
= 0; i
< sig
->param_count
; ++i
) {
7443 copy
->params
[i
] = mini_get_underlying_type (sig
->params
[i
]);
7444 if (!is_concrete_type (copy
->params
[i
]))
7448 copy
->has_type_parameters
= 0;
7451 wrapper
= mini_get_gsharedvt_in_sig_wrapper (copy
);
7452 add_extra_method (acfg
, wrapper
);
7455 if (gsharedvt_out
) {
7456 wrapper
= mini_get_gsharedvt_out_sig_wrapper (copy
);
7457 add_extra_method (acfg
, wrapper
);
7460 //printf ("%s\n", mono_method_full_name (wrapper, 1));
7468 * AOT compile a given method.
7469 * This function might be called by multiple threads, so it must be thread-safe.
7472 compile_method (MonoAotCompile
*acfg
, MonoMethod
*method
)
7475 MonoJumpInfo
*patch_info
;
7478 MonoMethod
*wrapped
;
7482 if (acfg
->aot_opts
.metadata_only
)
7485 mono_acfg_lock (acfg
);
7486 index
= get_method_index (acfg
, method
);
7487 mono_acfg_unlock (acfg
);
7489 /* fixme: maybe we can also precompile wrapper methods */
7490 if ((method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
) ||
7491 (method
->iflags
& METHOD_IMPL_ATTRIBUTE_RUNTIME
) ||
7492 (method
->flags
& METHOD_ATTRIBUTE_ABSTRACT
)) {
7493 //printf ("Skip (impossible): %s\n", mono_method_full_name (method, TRUE));
7497 if (method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
)
7500 wrapped
= mono_marshal_method_from_wrapper (method
);
7501 if (wrapped
&& (wrapped
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) && wrapped
->is_generic
)
7502 // FIXME: The wrapper should be generic too, but it is not
7505 if (method
->wrapper_type
== MONO_WRAPPER_COMINTEROP
)
7508 InterlockedIncrement (&acfg
->stats
.mcount
);
7511 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
)) {
7512 InterlockedIncrement (&acfg
->stats
.genericcount
);
7517 //acfg->aot_opts.print_skipped_methods = TRUE;
7520 * Since these methods are the only ones which are compiled with
7521 * AOT support, and they are not used by runtime startup/shutdown code,
7522 * the runtime will not see AOT methods during AOT compilation,so it
7523 * does not need to support them by creating a fake GOT etc.
7525 flags
= JIT_FLAG_AOT
;
7526 if (mono_aot_mode_is_full (&acfg
->aot_opts
))
7527 flags
= (JitFlags
)(flags
| JIT_FLAG_FULL_AOT
);
7529 flags
= (JitFlags
)(flags
| JIT_FLAG_LLVM
);
7530 if (acfg
->aot_opts
.llvm_only
)
7531 flags
= (JitFlags
)(flags
| JIT_FLAG_LLVM_ONLY
| JIT_FLAG_EXPLICIT_NULL_CHECKS
);
7532 if (acfg
->aot_opts
.no_direct_calls
)
7533 flags
= (JitFlags
)(flags
| JIT_FLAG_NO_DIRECT_ICALLS
);
7534 if (acfg
->aot_opts
.direct_pinvoke
)
7535 flags
= (JitFlags
)(flags
| JIT_FLAG_DIRECT_PINVOKE
);
7537 jit_timer
= mono_time_track_start ();
7538 cfg
= mini_method_compile (method
, acfg
->opts
, mono_get_root_domain (), flags
, 0, index
);
7539 mono_time_track_end (&mono_jit_stats
.jit_time
, jit_timer
);
7541 if (cfg
->exception_type
== MONO_EXCEPTION_GENERIC_SHARING_FAILED
) {
7542 if (acfg
->aot_opts
.print_skipped_methods
)
7543 printf ("Skip (gshared failure): %s (%s)\n", mono_method_get_full_name (method
), cfg
->exception_message
);
7544 InterlockedIncrement (&acfg
->stats
.genericcount
);
7547 if (cfg
->exception_type
!= MONO_EXCEPTION_NONE
) {
7548 if (acfg
->aot_opts
.print_skipped_methods
)
7549 printf ("Skip (JIT failure): %s\n", mono_method_get_full_name (method
));
7550 /* Let the exception happen at runtime */
7554 if (cfg
->disable_aot
) {
7555 if (acfg
->aot_opts
.print_skipped_methods
)
7556 printf ("Skip (disabled): %s\n", mono_method_get_full_name (method
));
7557 InterlockedIncrement (&acfg
->stats
.ocount
);
7560 cfg
->method_index
= index
;
7562 /* Nullify patches which need no aot processing */
7563 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
7564 switch (patch_info
->type
) {
7565 case MONO_PATCH_INFO_LABEL
:
7566 case MONO_PATCH_INFO_BB
:
7567 patch_info
->type
= MONO_PATCH_INFO_NONE
;
7574 /* Collect method->token associations from the cfg */
7575 mono_acfg_lock (acfg
);
7576 g_hash_table_foreach (cfg
->token_info_hash
, add_token_info_hash
, acfg
);
7577 mono_acfg_unlock (acfg
);
7578 g_hash_table_destroy (cfg
->token_info_hash
);
7579 cfg
->token_info_hash
= NULL
;
7582 * Check for absolute addresses.
7585 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
7586 switch (patch_info
->type
) {
7587 case MONO_PATCH_INFO_ABS
:
7588 /* unable to handle this */
7597 if (acfg
->aot_opts
.print_skipped_methods
)
7598 printf ("Skip (abs call): %s\n", mono_method_get_full_name (method
));
7599 InterlockedIncrement (&acfg
->stats
.abscount
);
7603 /* Lock for the rest of the code */
7604 mono_acfg_lock (acfg
);
7607 acfg
->stats
.method_categories
[METHOD_CAT_GSHAREDVT
] ++;
7608 else if (cfg
->gshared
)
7609 acfg
->stats
.method_categories
[METHOD_CAT_INST
] ++;
7610 else if (cfg
->method
->wrapper_type
)
7611 acfg
->stats
.method_categories
[METHOD_CAT_WRAPPER
] ++;
7613 acfg
->stats
.method_categories
[METHOD_CAT_NORMAL
] ++;
7616 * Check for methods/klasses we can't encode.
7619 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
7620 if (!can_encode_patch (acfg
, patch_info
))
7625 if (acfg
->aot_opts
.print_skipped_methods
)
7626 printf ("Skip (patches): %s\n", mono_method_get_full_name (method
));
7627 acfg
->stats
.ocount
++;
7628 mono_acfg_unlock (acfg
);
7632 if (!cfg
->compile_llvm
)
7633 acfg
->has_jitted_code
= TRUE
;
7635 if (method
->is_inflated
&& acfg
->aot_opts
.log_instances
) {
7636 if (acfg
->instances_logfile
)
7637 fprintf (acfg
->instances_logfile
, "%s ### %d\n", mono_method_get_full_name (method
), cfg
->code_size
);
7639 printf ("%s ### %d\n", mono_method_get_full_name (method
), cfg
->code_size
);
7642 /* Adds generic instances referenced by this method */
7644 * The depth is used to avoid infinite loops when generic virtual recursion is
7647 depth
= GPOINTER_TO_UINT (g_hash_table_lookup (acfg
->method_depth
, method
));
7648 if (!acfg
->aot_opts
.no_instances
&& depth
< 32 && mono_aot_mode_is_full (&acfg
->aot_opts
)) {
7649 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
7650 switch (patch_info
->type
) {
7651 case MONO_PATCH_INFO_RGCTX_FETCH
:
7652 case MONO_PATCH_INFO_RGCTX_SLOT_INDEX
:
7653 case MONO_PATCH_INFO_METHOD
: {
7654 MonoMethod
*m
= NULL
;
7656 if (patch_info
->type
== MONO_PATCH_INFO_RGCTX_FETCH
|| patch_info
->type
== MONO_PATCH_INFO_RGCTX_SLOT_INDEX
) {
7657 MonoJumpInfoRgctxEntry
*e
= patch_info
->data
.rgctx_entry
;
7659 if (e
->info_type
== MONO_RGCTX_INFO_GENERIC_METHOD_CODE
)
7660 m
= e
->data
->data
.method
;
7662 m
= patch_info
->data
.method
;
7667 if (m
->is_inflated
&& mono_aot_mode_is_full (&acfg
->aot_opts
)) {
7668 if (!(mono_class_generic_sharing_enabled (m
->klass
) &&
7669 mono_method_is_generic_sharable_full (m
, FALSE
, FALSE
, FALSE
)) &&
7670 (!method_has_type_vars (m
) || mono_method_is_generic_sharable_full (m
, TRUE
, TRUE
, FALSE
))) {
7671 if (m
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
7672 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && !method_has_type_vars (m
))
7673 add_extra_method_with_depth (acfg
, mono_marshal_get_native_wrapper (m
, TRUE
, TRUE
), depth
+ 1);
7675 add_extra_method_with_depth (acfg
, m
, depth
+ 1);
7676 add_types_from_method_header (acfg
, m
);
7679 add_generic_class_with_depth (acfg
, m
->klass
, depth
+ 5, "method");
7681 if (m
->wrapper_type
== MONO_WRAPPER_MANAGED_TO_MANAGED
) {
7682 WrapperInfo
*info
= mono_marshal_get_wrapper_info (m
);
7684 if (info
&& info
->subtype
== WRAPPER_SUBTYPE_ELEMENT_ADDR
)
7685 add_extra_method_with_depth (acfg
, m
, depth
+ 1);
7689 case MONO_PATCH_INFO_VTABLE
: {
7690 MonoClass
*klass
= patch_info
->data
.klass
;
7692 if (mono_class_is_ginst (klass
) && !mini_class_is_generic_sharable (klass
))
7693 add_generic_class_with_depth (acfg
, klass
, depth
+ 5, "vtable");
7696 case MONO_PATCH_INFO_SFLDA
: {
7697 MonoClass
*klass
= patch_info
->data
.field
->parent
;
7699 /* The .cctor needs to run at runtime. */
7700 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
))
7701 add_extra_method_with_depth (acfg
, mono_class_get_cctor (klass
), depth
+ 1);
7710 /* Determine whenever the method has GOT slots */
7711 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
7712 switch (patch_info
->type
) {
7713 case MONO_PATCH_INFO_GOT_OFFSET
:
7714 case MONO_PATCH_INFO_NONE
:
7715 case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
:
7716 case MONO_PATCH_INFO_GC_NURSERY_START
:
7717 case MONO_PATCH_INFO_GC_NURSERY_BITS
:
7719 case MONO_PATCH_INFO_IMAGE
:
7720 /* The assembly is stored in GOT slot 0 */
7721 if (patch_info
->data
.image
!= acfg
->image
)
7722 cfg
->has_got_slots
= TRUE
;
7725 if (!is_plt_patch (patch_info
) || (cfg
->compile_llvm
&& acfg
->aot_opts
.llvm_only
))
7726 cfg
->has_got_slots
= TRUE
;
7731 if (!cfg
->has_got_slots
)
7732 InterlockedIncrement (&acfg
->stats
.methods_without_got_slots
);
7734 /* Add gsharedvt wrappers for signatures used by the method */
7735 if (acfg
->aot_opts
.llvm_only
) {
7738 if (!cfg
->method
->wrapper_type
|| cfg
->method
->wrapper_type
== MONO_WRAPPER_DELEGATE_INVOKE
)
7739 /* These only need out wrappers */
7740 add_gsharedvt_wrappers (acfg
, mono_method_signature (cfg
->method
), FALSE
, TRUE
);
7742 for (l
= cfg
->signatures
; l
; l
= l
->next
) {
7743 MonoMethodSignature
*sig
= mono_metadata_signature_dup ((MonoMethodSignature
*)l
->data
);
7745 /* These only need in wrappers */
7746 add_gsharedvt_wrappers (acfg
, sig
, TRUE
, FALSE
);
7751 * FIXME: Instead of this mess, allocate the patches from the aot mempool.
7753 /* Make a copy of the patch info which is in the mempool */
7755 MonoJumpInfo
*patches
= NULL
, *patches_end
= NULL
;
7757 for (patch_info
= cfg
->patch_info
; patch_info
; patch_info
= patch_info
->next
) {
7758 MonoJumpInfo
*new_patch_info
= mono_patch_info_dup_mp (acfg
->mempool
, patch_info
);
7761 patches
= new_patch_info
;
7763 patches_end
->next
= new_patch_info
;
7764 patches_end
= new_patch_info
;
7766 cfg
->patch_info
= patches
;
7768 /* Make a copy of the unwind info */
7770 GSList
*l
, *unwind_ops
;
7774 for (l
= cfg
->unwind_ops
; l
; l
= l
->next
) {
7775 op
= (MonoUnwindOp
*)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoUnwindOp
));
7776 memcpy (op
, l
->data
, sizeof (MonoUnwindOp
));
7777 unwind_ops
= g_slist_prepend_mempool (acfg
->mempool
, unwind_ops
, op
);
7779 cfg
->unwind_ops
= g_slist_reverse (unwind_ops
);
7781 /* Make a copy of the argument/local info */
7784 MonoInst
**args
, **locals
;
7785 MonoMethodSignature
*sig
;
7786 MonoMethodHeader
*header
;
7789 sig
= mono_method_signature (method
);
7790 args
= (MonoInst
**)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
*) * (sig
->param_count
+ sig
->hasthis
));
7791 for (i
= 0; i
< sig
->param_count
+ sig
->hasthis
; ++i
) {
7792 args
[i
] = (MonoInst
*)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
));
7793 memcpy (args
[i
], cfg
->args
[i
], sizeof (MonoInst
));
7797 header
= mono_method_get_header_checked (method
, &error
);
7798 mono_error_assert_ok (&error
); /* FIXME don't swallow the error */
7799 locals
= (MonoInst
**)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
*) * header
->num_locals
);
7800 for (i
= 0; i
< header
->num_locals
; ++i
) {
7801 locals
[i
] = (MonoInst
*)mono_mempool_alloc (acfg
->mempool
, sizeof (MonoInst
));
7802 memcpy (locals
[i
], cfg
->locals
[i
], sizeof (MonoInst
));
7804 mono_metadata_free_mh (header
);
7805 cfg
->locals
= locals
;
7808 /* Free some fields used by cfg to conserve memory */
7809 mono_empty_compile (cfg
);
7811 //printf ("Compile: %s\n", mono_method_full_name (method, TRUE));
7813 while (index
>= acfg
->cfgs_size
) {
7814 MonoCompile
**new_cfgs
;
7817 new_size
= acfg
->cfgs_size
* 2;
7818 new_cfgs
= g_new0 (MonoCompile
*, new_size
);
7819 memcpy (new_cfgs
, acfg
->cfgs
, sizeof (MonoCompile
*) * acfg
->cfgs_size
);
7820 g_free (acfg
->cfgs
);
7821 acfg
->cfgs
= new_cfgs
;
7822 acfg
->cfgs_size
= new_size
;
7824 acfg
->cfgs
[index
] = cfg
;
7826 g_hash_table_insert (acfg
->method_to_cfg
, cfg
->orig_method
, cfg
);
7828 mono_update_jit_stats (cfg
);
7831 if (cfg->orig_method->wrapper_type)
7832 g_ptr_array_add (acfg->extra_methods, cfg->orig_method);
7835 mono_acfg_unlock (acfg
);
7837 InterlockedIncrement (&acfg
->stats
.ccount
);
7840 static mono_thread_start_return_t WINAPI
7841 compile_thread_main (gpointer user_data
)
7843 MonoDomain
*domain
= ((MonoDomain
**)user_data
) [0];
7844 MonoAotCompile
*acfg
= ((MonoAotCompile
**)user_data
) [1];
7845 GPtrArray
*methods
= ((GPtrArray
**)user_data
) [2];
7849 MonoThread
*thread
= mono_thread_attach (domain
);
7850 mono_thread_set_name_internal (thread
->internal_thread
, mono_string_new (mono_get_root_domain (), "AOT compiler"), TRUE
, &error
);
7851 mono_error_assert_ok (&error
);
7853 for (i
= 0; i
< methods
->len
; ++i
)
7854 compile_method (acfg
, (MonoMethod
*)g_ptr_array_index (methods
, i
));
7860 load_profile_files (MonoAotCompile
*acfg
)
7864 int file_index
, res
, method_index
, i
;
7867 GList
*unordered
, *l
;
7872 tmp
= g_strdup_printf ("%s/.mono/aot-profile-data/%s-%d", g_get_home_dir (), acfg
->image
->assembly_name
, file_index
);
7874 if (!g_file_test (tmp
, G_FILE_TEST_IS_REGULAR
)) {
7879 infile
= fopen (tmp
, "r");
7882 printf ("Using profile data file '%s'\n", tmp
);
7887 res
= fscanf (infile
, "%32s\n", ver
);
7888 if ((res
!= 1) || strcmp (ver
, "#VER:2") != 0) {
7889 printf ("Profile file has wrong version or invalid.\n");
7896 MonoMethodDesc
*desc
;
7899 if (fgets (name
, 1023, infile
) == NULL
)
7902 /* Kill the newline */
7903 if (strlen (name
) > 0)
7904 name
[strlen (name
) - 1] = '\0';
7906 desc
= mono_method_desc_new (name
, TRUE
);
7908 method
= mono_method_desc_search_in_image (desc
, acfg
->image
);
7910 if (method
&& mono_method_get_token (method
)) {
7911 token
= mono_method_get_token (method
);
7912 method_index
= mono_metadata_token_index (token
) - 1;
7915 for (i
= 0; i
< acfg
->method_order
->len
; ++i
) {
7916 if (g_ptr_array_index (acfg
->method_order
, i
) == GUINT_TO_POINTER (method_index
)) {
7922 g_ptr_array_add (acfg
->method_order
, GUINT_TO_POINTER (method_index
));
7924 //printf ("No method found matching '%s'.\n", name);
7930 /* Add missing methods */
7932 for (method_index
= 0; method_index
< acfg
->image
->tables
[MONO_TABLE_METHOD
].rows
; ++method_index
) {
7934 for (i
= 0; i
< acfg
->method_order
->len
; ++i
) {
7935 if (g_ptr_array_index (acfg
->method_order
, i
) == GUINT_TO_POINTER (method_index
)) {
7941 unordered
= g_list_prepend (unordered
, GUINT_TO_POINTER (method_index
));
7943 unordered
= g_list_reverse (unordered
);
7944 for (l
= unordered
; l
; l
= l
->next
)
7945 g_ptr_array_add (acfg
->method_order
, l
->data
);
7948 /* Used by the LLVM backend */
7950 mono_aot_get_got_offset (MonoJumpInfo
*ji
)
7952 return get_got_offset (llvm_acfg
, TRUE
, ji
);
7956 * mono_aot_is_shared_got_offset:
7958 * Return whenever OFFSET refers to a GOT slot which is preinitialized
7959 * when the AOT image is loaded.
7962 mono_aot_is_shared_got_offset (int offset
)
7964 return offset
< llvm_acfg
->nshared_got_entries
;
7968 mono_aot_get_method_name (MonoCompile
*cfg
)
7970 if (llvm_acfg
->aot_opts
.static_link
)
7971 /* Include the assembly name too to avoid duplicate symbol errors */
7972 return g_strdup_printf ("%s_%s", llvm_acfg
->assembly_name_sym
, get_debug_sym (cfg
->orig_method
, "", llvm_acfg
->method_label_hash
));
7974 return get_debug_sym (cfg
->orig_method
, "", llvm_acfg
->method_label_hash
);
7978 * mono_aot_is_linkonce_method:
7980 * Return whenever METHOD should be emitted with linkonce linkage,
7981 * eliminating duplicate copies when compiling in static mode.
7984 mono_aot_is_linkonce_method (MonoMethod
*method
)
7990 // FIXME: Add more cases
7991 if (method
->wrapper_type
!= MONO_WRAPPER_UNKNOWN
)
7993 info
= mono_marshal_get_wrapper_info (method
);
7994 if ((info
&& (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
|| info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
)))
8001 append_mangled_type (GString
*s
, MonoType
*t
)
8004 g_string_append_printf (s
, "b");
8006 case MONO_TYPE_VOID
:
8007 g_string_append_printf (s
, "void_");
8010 g_string_append_printf (s
, "i1");
8013 g_string_append_printf (s
, "u1");
8016 g_string_append_printf (s
, "i2");
8019 g_string_append_printf (s
, "u2");
8022 g_string_append_printf (s
, "i4");
8025 g_string_append_printf (s
, "u4");
8028 g_string_append_printf (s
, "i8");
8031 g_string_append_printf (s
, "u8");
8034 g_string_append_printf (s
, "ii");
8037 g_string_append_printf (s
, "ui");
8040 g_string_append_printf (s
, "fl");
8043 g_string_append_printf (s
, "do");
8046 char *fullname
= mono_type_full_name (t
);
8052 * Have to create a mangled name which is:
8056 temp
= g_string_new ("");
8057 len
= strlen (fullname
);
8058 for (i
= 0; i
< len
; ++i
) {
8059 char c
= fullname
[i
];
8061 g_string_append_c (temp
, c
);
8062 } else if (c
== '_') {
8063 g_string_append_c (temp
, '_');
8064 g_string_append_c (temp
, '_');
8066 g_string_append_c (temp
, '_');
8067 g_string_append_printf (temp
, "%x", (int)c
);
8070 temps
= g_string_free (temp
, FALSE
);
8071 /* Include the length to avoid different length type names aliasing each other */
8072 g_string_append_printf (s
, "cl%x_%s_", strlen (temps
), temps
);
8081 append_mangled_signature (GString
*s
, MonoMethodSignature
*sig
)
8086 supported
= append_mangled_type (s
, sig
->ret
);
8090 g_string_append_printf (s
, "this_");
8091 for (i
= 0; i
< sig
->param_count
; ++i
) {
8092 supported
= append_mangled_type (s
, sig
->params
[i
]);
8101 * mono_aot_get_mangled_method_name:
8103 * Return a unique mangled name for METHOD, or NULL.
8106 mono_aot_get_mangled_method_name (MonoMethod
*method
)
8112 // FIXME: Add more cases
8113 if (method
->wrapper_type
!= MONO_WRAPPER_UNKNOWN
)
8115 info
= mono_marshal_get_wrapper_info (method
);
8116 if (!(info
&& (info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
|| info
->subtype
== WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
)))
8119 s
= g_string_new ("");
8121 g_string_append_printf (s
, "aot_method_w_");
8123 switch (info
->subtype
) {
8124 case WRAPPER_SUBTYPE_GSHAREDVT_IN_SIG
:
8125 g_string_append_printf (s
, "gsharedvt_in_");
8127 case WRAPPER_SUBTYPE_GSHAREDVT_OUT_SIG
:
8128 g_string_append_printf (s
, "gsharedvt_out_");
8131 g_assert_not_reached ();
8135 supported
= append_mangled_signature (s
, info
->d
.gsharedvt
.sig
);
8137 g_string_free (s
, TRUE
);
8141 return g_string_free (s
, FALSE
);
8145 mono_aot_is_direct_callable (MonoJumpInfo
*patch_info
)
8147 return is_direct_callable (llvm_acfg
, NULL
, patch_info
);
8151 mono_aot_mark_unused_llvm_plt_entry (MonoJumpInfo
*patch_info
)
8153 MonoPltEntry
*plt_entry
;
8155 plt_entry
= get_plt_entry (llvm_acfg
, patch_info
);
8156 plt_entry
->llvm_used
= FALSE
;
8160 mono_aot_get_direct_call_symbol (MonoJumpInfoType type
, gconstpointer data
)
8162 const char *sym
= NULL
;
8164 if (llvm_acfg
->aot_opts
.direct_icalls
) {
8165 if (type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
8166 /* Call to a C function implementing a jit icall */
8167 sym
= mono_lookup_jit_icall_symbol ((const char *)data
);
8168 } else if (type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
8169 MonoMethod
*method
= (MonoMethod
*)data
;
8170 if (!(method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
8171 sym
= mono_lookup_icall_symbol (method
);
8172 else if (llvm_acfg
->aot_opts
.direct_pinvoke
)
8173 sym
= get_pinvoke_import (llvm_acfg
, method
);
8176 return g_strdup (sym
);
8182 mono_aot_get_plt_symbol (MonoJumpInfoType type
, gconstpointer data
)
8184 MonoJumpInfo
*ji
= (MonoJumpInfo
*)mono_mempool_alloc (llvm_acfg
->mempool
, sizeof (MonoJumpInfo
));
8185 MonoPltEntry
*plt_entry
;
8186 const char *sym
= NULL
;
8189 ji
->data
.target
= data
;
8191 if (!can_encode_patch (llvm_acfg
, ji
))
8194 if (llvm_acfg
->aot_opts
.direct_icalls
) {
8195 if (type
== MONO_PATCH_INFO_JIT_ICALL_ADDR
) {
8196 /* Call to a C function implementing a jit icall */
8197 sym
= mono_lookup_jit_icall_symbol ((const char *)data
);
8198 } else if (type
== MONO_PATCH_INFO_ICALL_ADDR_CALL
) {
8199 MonoMethod
*method
= (MonoMethod
*)data
;
8200 if (!(method
->flags
& METHOD_ATTRIBUTE_PINVOKE_IMPL
))
8201 sym
= mono_lookup_icall_symbol (method
);
8204 return g_strdup (sym
);
8207 plt_entry
= get_plt_entry (llvm_acfg
, ji
);
8208 plt_entry
->llvm_used
= TRUE
;
8210 #if defined(TARGET_MACH)
8211 return g_strdup_printf (plt_entry
->llvm_symbol
+ strlen (llvm_acfg
->llvm_label_prefix
));
8213 return g_strdup_printf (plt_entry
->llvm_symbol
);
8218 mono_aot_get_method_index (MonoMethod
*method
)
8220 g_assert (llvm_acfg
);
8221 return get_method_index (llvm_acfg
, method
);
8225 mono_aot_patch_info_dup (MonoJumpInfo
* ji
)
8229 mono_acfg_lock (llvm_acfg
);
8230 res
= mono_patch_info_dup_mp (llvm_acfg
->mempool
, ji
);
8231 mono_acfg_unlock (llvm_acfg
);
8237 execute_system (const char * command
)
8242 // We need an extra set of quotes around the whole command to properly handle commands
8243 // with spaces since internally the command is called through "cmd /c.
8244 char * quoted_command
= g_strdup_printf ("\"%s\"", command
);
8246 int size
= MultiByteToWideChar (CP_UTF8
, 0 , quoted_command
, -1, NULL
, 0);
8247 wchar_t* wstr
= g_malloc (sizeof (wchar_t) * size
);
8248 MultiByteToWideChar (CP_UTF8
, 0, quoted_command
, -1, wstr
, size
);
8249 status
= _wsystem (wstr
);
8252 g_free (quoted_command
);
8253 #elif defined (HAVE_SYSTEM)
8254 status
= system (command
);
8256 g_assert_not_reached ();
8267 * Emit the LLVM code into an LLVM bytecode file, and compile it using the LLVM
8271 emit_llvm_file (MonoAotCompile
*acfg
)
8273 char *command
, *opts
, *tempbc
, *optbc
, *output_fname
;
8275 if (acfg
->aot_opts
.llvm_only
&& acfg
->aot_opts
.asm_only
) {
8276 tempbc
= g_strdup_printf ("%s.bc", acfg
->tmpbasename
);
8277 optbc
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
8279 tempbc
= g_strdup_printf ("%s.bc", acfg
->tmpbasename
);
8280 optbc
= g_strdup_printf ("%s.opt.bc", acfg
->tmpbasename
);
8283 mono_llvm_emit_aot_module (tempbc
, g_path_get_basename (acfg
->image
->name
));
8286 * FIXME: Experiment with adding optimizations, the -std-compile-opts set takes
8287 * a lot of time, and doesn't seem to save much space.
8288 * The following optimizations cannot be enabled:
8290 * - 'jump-threading' changes our blockaddress references to int constants.
8291 * - 'basiccg' fails because it contains:
8292 * if (CS && !isa<IntrinsicInst>(II)) {
8293 * and isa<IntrinsicInst> is false for invokes to intrinsics (iltests.exe).
8294 * - 'prune-eh' and 'functionattrs' depend on 'basiccg'.
8295 * The opt list below was produced by taking the output of:
8296 * llvm-as < /dev/null | opt -O2 -disable-output -debug-pass=Arguments
8297 * then removing tailcallelim + the global opts.
8298 * strip-dead-prototypes deletes unused intrinsics definitions.
8300 /* The dse pass is disabled because of #13734 and #17616 */
8302 * The dse bug is in DeadStoreElimination.cpp:isOverwrite ():
8303 * // If we have no DataLayout information around, then the size of the store
8304 * // is inferrable from the pointee type. If they are the same type, then
8305 * // we know that the store is safe.
8306 * if (AA.getDataLayout() == 0 &&
8307 * Later.Ptr->getType() == Earlier.Ptr->getType()) {
8308 * return OverwriteComplete;
8309 * Here, if 'Earlier' refers to a memset, and Later has no size info, it mistakenly thinks the memset is redundant.
8311 if (acfg
->aot_opts
.llvm_only
)
8312 // FIXME: This doesn't work yet
8313 opts
= g_strdup ("");
8315 #if LLVM_API_VERSION > 100
8316 opts
= g_strdup ("-O2 -disable-tail-calls");
8318 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");
8320 command
= g_strdup_printf ("\"%sopt\" -f %s -o \"%s\" \"%s\"", acfg
->aot_opts
.llvm_path
, opts
, optbc
, tempbc
);
8321 aot_printf (acfg
, "Executing opt: %s\n", command
);
8322 if (execute_system (command
) != 0)
8326 if (acfg
->aot_opts
.llvm_only
&& acfg
->aot_opts
.asm_only
)
8327 /* Nothing else to do */
8330 if (acfg
->aot_opts
.llvm_only
) {
8331 /* Use the stock clang from xcode */
8333 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
);
8335 aot_printf (acfg
, "Executing clang: %s\n", command
);
8336 if (execute_system (command
) != 0)
8341 if (!acfg
->llc_args
)
8342 acfg
->llc_args
= g_string_new ("");
8344 /* Verbose asm slows down llc greatly */
8345 g_string_append (acfg
->llc_args
, " -asm-verbose=false");
8347 if (acfg
->aot_opts
.mtriple
)
8348 g_string_append_printf (acfg
->llc_args
, " -mtriple=%s", acfg
->aot_opts
.mtriple
);
8350 g_string_append (acfg
->llc_args
, " -disable-gnu-eh-frame -enable-mono-eh-frame");
8352 g_string_append_printf (acfg
->llc_args
, " -mono-eh-frame-symbol=%s%s", acfg
->user_symbol_prefix
, acfg
->llvm_eh_frame_symbol
);
8354 #if LLVM_API_VERSION > 100
8355 g_string_append_printf (acfg
->llc_args
, " -disable-tail-calls");
8358 #if defined(TARGET_MACH) && defined(TARGET_ARM)
8359 /* ios requires PIC code now */
8360 g_string_append_printf (acfg
->llc_args
, " -relocation-model=pic");
8362 if (llvm_acfg
->aot_opts
.static_link
)
8363 g_string_append_printf (acfg
->llc_args
, " -relocation-model=static");
8365 g_string_append_printf (acfg
->llc_args
, " -relocation-model=pic");
8368 if (acfg
->llvm_owriter
) {
8369 /* Emit an object file directly */
8370 output_fname
= g_strdup_printf ("%s", acfg
->llvm_ofile
);
8371 g_string_append_printf (acfg
->llc_args
, " -filetype=obj");
8373 output_fname
= g_strdup_printf ("%s", acfg
->llvm_sfile
);
8375 command
= g_strdup_printf ("\"%sllc\" %s -o \"%s\" \"%s.opt.bc\"", acfg
->aot_opts
.llvm_path
, acfg
->llc_args
->str
, output_fname
, acfg
->tmpbasename
);
8376 g_free (output_fname
);
8378 aot_printf (acfg
, "Executing llc: %s\n", command
);
8380 if (execute_system (command
) != 0)
8387 emit_code (MonoAotCompile
*acfg
)
8389 int oindex
, i
, prev_index
;
8390 gboolean saved_unbox_info
= FALSE
;
8391 char symbol
[MAX_SYMBOL_SIZE
];
8393 if (acfg
->aot_opts
.llvm_only
)
8396 #if defined(TARGET_POWERPC64)
8397 sprintf (symbol
, ".Lgot_addr");
8398 emit_section_change (acfg
, ".text", 0);
8399 emit_alignment (acfg
, 8);
8400 emit_label (acfg
, symbol
);
8401 emit_pointer (acfg
, acfg
->got_symbol
);
8405 * This global symbol is used to compute the address of each method using the
8406 * code_offsets array. It is also used to compute the memory ranges occupied by
8407 * AOT code, so it must be equal to the address of the first emitted method.
8409 emit_section_change (acfg
, ".text", 0);
8410 emit_alignment_code (acfg
, 8);
8411 emit_info_symbol (acfg
, "jit_code_start");
8414 * Emit some padding so the local symbol for the first method doesn't have the
8415 * same address as 'methods'.
8417 emit_padding (acfg
, 16);
8419 for (oindex
= 0; oindex
< acfg
->method_order
->len
; ++oindex
) {
8423 i
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->method_order
, oindex
));
8425 cfg
= acfg
->cfgs
[i
];
8430 method
= cfg
->orig_method
;
8432 /* Emit unbox trampoline */
8433 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && cfg
->orig_method
->klass
->valuetype
) {
8434 sprintf (symbol
, "ut_%d", get_method_index (acfg
, method
));
8436 emit_section_change (acfg
, ".text", 0);
8438 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
) {
8439 emit_set_thumb_mode (acfg
);
8440 fprintf (acfg
->fp
, "\n.thumb_func\n");
8443 emit_label (acfg
, symbol
);
8445 arch_emit_unbox_trampoline (acfg
, cfg
, cfg
->orig_method
, cfg
->asm_symbol
);
8447 if (acfg
->thumb_mixed
&& cfg
->compile_llvm
)
8448 emit_set_arm_mode (acfg
);
8450 if (!saved_unbox_info
) {
8451 char user_symbol
[128];
8453 sprintf (user_symbol
, "%sunbox_trampoline_p", acfg
->user_symbol_prefix
);
8455 emit_label (acfg
, "ut_end");
8457 unwind_ops
= mono_unwind_get_cie_program ();
8458 save_unwind_info (acfg
, user_symbol
, unwind_ops
);
8459 mono_free_unwind_info (unwind_ops
);
8461 /* Save the unbox trampoline size */
8462 emit_symbol_diff (acfg
, "ut_end", symbol
, 0);
8464 saved_unbox_info
= TRUE
;
8468 if (cfg
->compile_llvm
)
8469 acfg
->stats
.llvm_count
++;
8471 emit_method_code (acfg
, cfg
);
8474 emit_section_change (acfg
, ".text", 0);
8475 emit_alignment_code (acfg
, 8);
8476 emit_info_symbol (acfg
, "jit_code_end");
8478 /* To distinguish it from the next symbol */
8479 emit_padding (acfg
, 4);
8482 * Add .no_dead_strip directives for all LLVM methods to prevent the OSX linker
8483 * from optimizing them away, since it doesn't see that code_offsets references them.
8484 * JITted methods don't need this since they are referenced using assembler local
8486 * FIXME: This is why write-symbols doesn't work on OSX ?
8488 if (acfg
->llvm
&& acfg
->need_no_dead_strip
) {
8489 fprintf (acfg
->fp
, "\n");
8490 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
8491 if (acfg
->cfgs
[i
] && acfg
->cfgs
[i
]->compile_llvm
)
8492 fprintf (acfg
->fp
, ".no_dead_strip %s\n", acfg
->cfgs
[i
]->asm_symbol
);
8497 * To work around linker issues, we emit a table of branches, and disassemble them at runtime.
8498 * This is PIE code, and the linker can update it if needed.
8501 sprintf (symbol
, "method_addresses");
8502 emit_section_change (acfg
, ".text", 1);
8503 emit_alignment_code (acfg
, 8);
8504 emit_info_symbol (acfg
, symbol
);
8505 if (acfg
->aot_opts
.write_symbols
)
8506 emit_local_symbol (acfg
, symbol
, "method_addresses_end", TRUE
);
8507 emit_unset_mode (acfg
);
8508 if (acfg
->need_no_dead_strip
)
8509 fprintf (acfg
->fp
, " .no_dead_strip %s\n", symbol
);
8511 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
8512 #ifdef MONO_ARCH_AOT_SUPPORTED
8515 if (acfg
->cfgs
[i
]) {
8516 arch_emit_direct_call (acfg
, acfg
->cfgs
[i
]->asm_symbol
, FALSE
, acfg
->thumb_mixed
&& acfg
->cfgs
[i
]->compile_llvm
, NULL
, &call_size
);
8518 arch_emit_direct_call (acfg
, symbol
, FALSE
, FALSE
, NULL
, &call_size
);
8523 sprintf (symbol
, "method_addresses_end");
8524 emit_label (acfg
, symbol
);
8527 /* Emit a sorted table mapping methods to the index of their unbox trampolines */
8528 sprintf (symbol
, "unbox_trampolines");
8529 emit_section_change (acfg
, RODATA_SECT
, 0);
8530 emit_alignment (acfg
, 8);
8531 emit_info_symbol (acfg
, symbol
);
8534 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
8539 cfg
= acfg
->cfgs
[i
];
8543 method
= cfg
->orig_method
;
8545 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && cfg
->orig_method
->klass
->valuetype
) {
8546 index
= get_method_index (acfg
, method
);
8548 emit_int32 (acfg
, index
);
8549 /* Make sure the table is sorted by index */
8550 g_assert (index
> prev_index
);
8554 sprintf (symbol
, "unbox_trampolines_end");
8555 emit_info_symbol (acfg
, symbol
);
8556 emit_int32 (acfg
, 0);
8558 /* Emit a separate table with the trampoline addresses/offsets */
8559 sprintf (symbol
, "unbox_trampoline_addresses");
8560 emit_section_change (acfg
, ".text", 0);
8561 emit_alignment_code (acfg
, 8);
8562 emit_info_symbol (acfg
, symbol
);
8564 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
8569 cfg
= acfg
->cfgs
[i
];
8573 method
= cfg
->orig_method
;
8575 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && cfg
->orig_method
->klass
->valuetype
) {
8576 #ifdef MONO_ARCH_AOT_SUPPORTED
8579 index
= get_method_index (acfg
, method
);
8580 sprintf (symbol
, "ut_%d", index
);
8582 arch_emit_direct_call (acfg
, symbol
, FALSE
, acfg
->thumb_mixed
&& cfg
->compile_llvm
, NULL
, &call_size
);
8586 emit_int32 (acfg
, 0);
8590 emit_info (MonoAotCompile
*acfg
)
8595 offsets
= g_new0 (gint32
, acfg
->nmethods
);
8597 for (oindex
= 0; oindex
< acfg
->method_order
->len
; ++oindex
) {
8598 i
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->method_order
, oindex
));
8600 if (acfg
->cfgs
[i
]) {
8601 emit_method_info (acfg
, acfg
->cfgs
[i
]);
8602 offsets
[i
] = acfg
->cfgs
[i
]->method_info_offset
;
8608 acfg
->stats
.offsets_size
+= emit_offset_table (acfg
, "method_info_offsets", MONO_AOT_TABLE_METHOD_INFO_OFFSETS
, acfg
->nmethods
, 10, offsets
);
8613 #endif /* #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
8615 #define rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
8616 #define mix(a,b,c) { \
8617 a -= c; a ^= rot(c, 4); c += b; \
8618 b -= a; b ^= rot(a, 6); a += c; \
8619 c -= b; c ^= rot(b, 8); b += a; \
8620 a -= c; a ^= rot(c,16); c += b; \
8621 b -= a; b ^= rot(a,19); a += c; \
8622 c -= b; c ^= rot(b, 4); b += a; \
8624 #define final(a,b,c) { \
8625 c ^= b; c -= rot(b,14); \
8626 a ^= c; a -= rot(c,11); \
8627 b ^= a; b -= rot(a,25); \
8628 c ^= b; c -= rot(b,16); \
8629 a ^= c; a -= rot(c,4); \
8630 b ^= a; b -= rot(a,14); \
8631 c ^= b; c -= rot(b,24); \
8635 mono_aot_type_hash (MonoType
*t1
)
8637 guint hash
= t1
->type
;
8639 hash
|= t1
->byref
<< 6; /* do not collide with t1->type values */
8641 case MONO_TYPE_VALUETYPE
:
8642 case MONO_TYPE_CLASS
:
8643 case MONO_TYPE_SZARRAY
:
8644 /* check if the distribution is good enough */
8645 return ((hash
<< 5) - hash
) ^ mono_metadata_str_hash (t1
->data
.klass
->name
);
8647 return ((hash
<< 5) - hash
) ^ mono_metadata_type_hash (t1
->data
.type
);
8648 case MONO_TYPE_ARRAY
:
8649 return ((hash
<< 5) - hash
) ^ mono_metadata_type_hash (&t1
->data
.array
->eklass
->byval_arg
);
8650 case MONO_TYPE_GENERICINST
:
8651 return ((hash
<< 5) - hash
) ^ 0;
8658 * mono_aot_method_hash:
8660 * Return a hash code for methods which only depends on metadata.
8663 mono_aot_method_hash (MonoMethod
*method
)
8665 MonoMethodSignature
*sig
;
8669 guint32
*hashes_start
, *hashes
;
8671 MonoGenericInst
*class_ginst
= NULL
;
8672 MonoGenericInst
*ginst
= NULL
;
8674 /* Similar to the hash in mono_method_get_imt_slot () */
8676 sig
= mono_method_signature (method
);
8678 if (mono_class_is_ginst (method
->klass
))
8679 class_ginst
= mono_class_get_generic_class (method
->klass
)->context
.class_inst
;
8680 if (method
->is_inflated
)
8681 ginst
= ((MonoMethodInflated
*)method
)->context
.method_inst
;
8683 hashes_count
= sig
->param_count
+ 5 + (class_ginst
? class_ginst
->type_argc
: 0) + (ginst
? ginst
->type_argc
: 0);
8684 hashes_start
= (guint32
*)g_malloc0 (hashes_count
* sizeof (guint32
));
8685 hashes
= hashes_start
;
8687 /* Some wrappers are assigned to random classes */
8688 if (!method
->wrapper_type
|| method
->wrapper_type
== MONO_WRAPPER_REMOTING_INVOKE_WITH_CHECK
)
8689 klass
= method
->klass
;
8691 klass
= mono_defaults
.object_class
;
8693 if (!method
->wrapper_type
) {
8696 if (mono_class_is_ginst (klass
))
8697 full_name
= mono_type_full_name (&mono_class_get_generic_class (klass
)->container_class
->byval_arg
);
8699 full_name
= mono_type_full_name (&klass
->byval_arg
);
8701 hashes
[0] = mono_metadata_str_hash (full_name
);
8705 hashes
[0] = mono_metadata_str_hash (klass
->name
);
8706 hashes
[1] = mono_metadata_str_hash (klass
->name_space
);
8708 if (method
->wrapper_type
== MONO_WRAPPER_STFLD
|| method
->wrapper_type
== MONO_WRAPPER_LDFLD
|| method
->wrapper_type
== MONO_WRAPPER_LDFLDA
)
8709 /* The method name includes a stringified pointer */
8712 hashes
[2] = mono_metadata_str_hash (method
->name
);
8713 hashes
[3] = method
->wrapper_type
;
8714 hashes
[4] = mono_aot_type_hash (sig
->ret
);
8716 for (i
= 0; i
< sig
->param_count
; i
++) {
8717 hashes
[hindex
++] = mono_aot_type_hash (sig
->params
[i
]);
8720 for (i
= 0; i
< class_ginst
->type_argc
; ++i
)
8721 hashes
[hindex
++] = mono_aot_type_hash (class_ginst
->type_argv
[i
]);
8724 for (i
= 0; i
< ginst
->type_argc
; ++i
)
8725 hashes
[hindex
++] = mono_aot_type_hash (ginst
->type_argv
[i
]);
8727 g_assert (hindex
== hashes_count
);
8729 /* Setup internal state */
8730 a
= b
= c
= 0xdeadbeef + (((guint32
)hashes_count
)<<2);
8732 /* Handle most of the hashes */
8733 while (hashes_count
> 3) {
8742 /* Handle the last 3 hashes (all the case statements fall through) */
8743 switch (hashes_count
) {
8744 case 3 : c
+= hashes
[2];
8745 case 2 : b
+= hashes
[1];
8746 case 1 : a
+= hashes
[0];
8748 case 0: /* nothing left to add */
8752 g_free (hashes_start
);
8761 * mono_aot_get_array_helper_from_wrapper;
8763 * Get the helper method in Array called by an array wrapper method.
8766 mono_aot_get_array_helper_from_wrapper (MonoMethod
*method
)
8770 MonoGenericContext ctx
;
8771 MonoType
*args
[16];
8772 char *mname
, *iname
, *s
, *s2
, *helper_name
= NULL
;
8774 prefix
= "System.Collections.Generic";
8775 s
= g_strdup_printf ("%s", method
->name
+ strlen (prefix
) + 1);
8776 s2
= strstr (s
, "`1.");
8782 //printf ("X: %s %s\n", iname, mname);
8784 if (!strcmp (iname
, "IList"))
8785 helper_name
= g_strdup_printf ("InternalArray__%s", mname
);
8787 helper_name
= g_strdup_printf ("InternalArray__%s_%s", iname
, mname
);
8788 m
= mono_class_get_method_from_name (mono_defaults
.array_class
, helper_name
, mono_method_signature (method
)->param_count
);
8790 g_free (helper_name
);
8793 if (m
->is_generic
) {
8795 memset (&ctx
, 0, sizeof (ctx
));
8796 args
[0] = &method
->klass
->element_class
->byval_arg
;
8797 ctx
.method_inst
= mono_metadata_get_generic_inst (1, args
);
8798 m
= mono_class_inflate_generic_method_checked (m
, &ctx
, &error
);
8799 g_assert (mono_error_ok (&error
)); /* FIXME don't swallow the error */
8805 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
8807 typedef struct HashEntry
{
8808 guint32 key
, value
, index
;
8809 struct HashEntry
*next
;
8813 * emit_extra_methods:
8815 * Emit methods which are not in the METHOD table, like wrappers.
8818 emit_extra_methods (MonoAotCompile
*acfg
)
8820 int i
, table_size
, buf_size
;
8822 guint32
*info_offsets
;
8825 HashEntry
*entry
, *new_entry
;
8826 int nmethods
, max_chain_length
;
8829 info_offsets
= g_new0 (guint32
, acfg
->extra_methods
->len
);
8831 /* Emit method info */
8833 for (i
= 0; i
< acfg
->extra_methods
->len
; ++i
) {
8834 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (acfg
->extra_methods
, i
);
8835 MonoCompile
*cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, method
);
8841 p
= buf
= (guint8
*)g_malloc (buf_size
);
8845 method
= cfg
->method_to_register
;
8847 encode_method_ref (acfg
, method
, p
, &p
);
8849 g_assert ((p
- buf
) < buf_size
);
8851 info_offsets
[i
] = add_to_blob (acfg
, buf
, p
- buf
);
8856 * Construct a chained hash table for mapping indexes in extra_method_info to
8859 table_size
= g_spaced_primes_closest ((int)(nmethods
* 1.5));
8860 table
= g_ptr_array_sized_new (table_size
);
8861 for (i
= 0; i
< table_size
; ++i
)
8862 g_ptr_array_add (table
, NULL
);
8863 chain_lengths
= g_new0 (int, table_size
);
8864 max_chain_length
= 0;
8865 for (i
= 0; i
< acfg
->extra_methods
->len
; ++i
) {
8866 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (acfg
->extra_methods
, i
);
8867 MonoCompile
*cfg
= (MonoCompile
*)g_hash_table_lookup (acfg
->method_to_cfg
, method
);
8873 key
= info_offsets
[i
];
8874 value
= get_method_index (acfg
, method
);
8876 hash
= mono_aot_method_hash (method
) % table_size
;
8877 //printf ("X: %s %x\n", mono_method_get_full_name (method), mono_aot_method_hash (method));
8879 chain_lengths
[hash
] ++;
8880 max_chain_length
= MAX (max_chain_length
, chain_lengths
[hash
]);
8882 new_entry
= (HashEntry
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (HashEntry
));
8883 new_entry
->key
= key
;
8884 new_entry
->value
= value
;
8886 entry
= (HashEntry
*)g_ptr_array_index (table
, hash
);
8887 if (entry
== NULL
) {
8888 new_entry
->index
= hash
;
8889 g_ptr_array_index (table
, hash
) = new_entry
;
8892 entry
= entry
->next
;
8894 entry
->next
= new_entry
;
8895 new_entry
->index
= table
->len
;
8896 g_ptr_array_add (table
, new_entry
);
8899 g_free (chain_lengths
);
8901 //printf ("MAX: %d\n", max_chain_length);
8903 buf_size
= table
->len
* 12 + 4;
8904 p
= buf
= (guint8
*)g_malloc (buf_size
);
8905 encode_int (table_size
, p
, &p
);
8907 for (i
= 0; i
< table
->len
; ++i
) {
8908 HashEntry
*entry
= (HashEntry
*)g_ptr_array_index (table
, i
);
8910 if (entry
== NULL
) {
8911 encode_int (0, p
, &p
);
8912 encode_int (0, p
, &p
);
8913 encode_int (0, p
, &p
);
8915 //g_assert (entry->key > 0);
8916 encode_int (entry
->key
, p
, &p
);
8917 encode_int (entry
->value
, p
, &p
);
8919 encode_int (entry
->next
->index
, p
, &p
);
8921 encode_int (0, p
, &p
);
8924 g_assert (p
- buf
<= buf_size
);
8926 /* Emit the table */
8927 emit_aot_data (acfg
, MONO_AOT_TABLE_EXTRA_METHOD_TABLE
, "extra_method_table", buf
, p
- buf
);
8932 * Emit a table reverse mapping method indexes to their index in extra_method_info.
8933 * This is used by mono_aot_find_jit_info ().
8935 buf_size
= acfg
->extra_methods
->len
* 8 + 4;
8936 p
= buf
= (guint8
*)g_malloc (buf_size
);
8937 encode_int (acfg
->extra_methods
->len
, p
, &p
);
8938 for (i
= 0; i
< acfg
->extra_methods
->len
; ++i
) {
8939 MonoMethod
*method
= (MonoMethod
*)g_ptr_array_index (acfg
->extra_methods
, i
);
8941 encode_int (get_method_index (acfg
, method
), p
, &p
);
8942 encode_int (info_offsets
[i
], p
, &p
);
8944 emit_aot_data (acfg
, MONO_AOT_TABLE_EXTRA_METHOD_INFO_OFFSETS
, "extra_method_info_offsets", buf
, p
- buf
);
8947 g_free (info_offsets
);
8948 g_ptr_array_free (table
, TRUE
);
8952 generate_aotid (guint8
* aotid
)
8954 gpointer rand_handle
;
8958 rand_handle
= mono_rand_init (NULL
, 0);
8960 mono_rand_try_get_bytes (&rand_handle
, aotid
, 16, &error
);
8961 mono_error_assert_ok (&error
);
8963 mono_rand_close (rand_handle
);
8967 emit_exception_info (MonoAotCompile
*acfg
)
8971 SeqPointData sp_data
;
8972 gboolean seq_points_to_file
= FALSE
;
8974 offsets
= g_new0 (gint32
, acfg
->nmethods
);
8975 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
8976 if (acfg
->cfgs
[i
]) {
8977 MonoCompile
*cfg
= acfg
->cfgs
[i
];
8979 // By design aot-runtime decode_exception_debug_info is not able to load sequence point debug data from a file.
8980 // As it is not possible to load debug data from a file its is also not possible to store it in a file.
8981 gboolean method_seq_points_to_file
= acfg
->aot_opts
.gen_msym_dir
&&
8982 cfg
->gen_seq_points
&& !cfg
->gen_sdb_seq_points
;
8983 gboolean method_seq_points_to_binary
= cfg
->gen_seq_points
&& !method_seq_points_to_file
;
8985 emit_exception_debug_info (acfg
, cfg
, method_seq_points_to_binary
);
8986 offsets
[i
] = cfg
->ex_info_offset
;
8988 if (method_seq_points_to_file
) {
8989 if (!seq_points_to_file
) {
8990 mono_seq_point_data_init (&sp_data
, acfg
->nmethods
);
8991 seq_points_to_file
= TRUE
;
8993 mono_seq_point_data_add (&sp_data
, cfg
->method
->token
, cfg
->method_index
, cfg
->seq_point_info
);
9000 if (seq_points_to_file
) {
9001 char *aotid
= mono_guid_to_string_minimal (acfg
->image
->aotid
);
9002 char *dir
= g_build_filename (acfg
->aot_opts
.gen_msym_dir_path
, aotid
, NULL
);
9003 char *image_basename
= g_path_get_basename (acfg
->image
->name
);
9004 char *aot_file
= g_strdup_printf("%s%s", image_basename
, SEQ_POINT_AOT_EXT
);
9005 char *aot_file_path
= g_build_filename (dir
, aot_file
, NULL
);
9007 if (g_ensure_directory_exists (aot_file_path
) == FALSE
) {
9008 fprintf (stderr
, "AOT : failed to create msym directory: %s\n", aot_file_path
);
9012 mono_seq_point_data_write (&sp_data
, aot_file_path
);
9013 mono_seq_point_data_free (&sp_data
);
9017 g_free (image_basename
);
9019 g_free (aot_file_path
);
9022 acfg
->stats
.offsets_size
+= emit_offset_table (acfg
, "ex_info_offsets", MONO_AOT_TABLE_EX_INFO_OFFSETS
, acfg
->nmethods
, 10, offsets
);
9027 emit_unwind_info (MonoAotCompile
*acfg
)
9032 if (acfg
->aot_opts
.llvm_only
) {
9033 g_assert (acfg
->unwind_ops
->len
== 0);
9038 * The unwind info contains a lot of duplicates so we emit each unique
9039 * entry once, and only store the offset from the start of the table in the
9043 sprintf (symbol
, "unwind_info");
9044 emit_section_change (acfg
, RODATA_SECT
, 1);
9045 emit_alignment (acfg
, 8);
9046 emit_info_symbol (acfg
, symbol
);
9048 for (i
= 0; i
< acfg
->unwind_ops
->len
; ++i
) {
9049 guint32 index
= GPOINTER_TO_UINT (g_ptr_array_index (acfg
->unwind_ops
, i
));
9050 guint8
*unwind_info
;
9051 guint32 unwind_info_len
;
9055 unwind_info
= mono_get_cached_unwind_info (index
, &unwind_info_len
);
9058 encode_value (unwind_info_len
, p
, &p
);
9059 emit_bytes (acfg
, buf
, p
- buf
);
9060 emit_bytes (acfg
, unwind_info
, unwind_info_len
);
9062 acfg
->stats
.unwind_info_size
+= (p
- buf
) + unwind_info_len
;
9067 emit_class_info (MonoAotCompile
*acfg
)
9072 offsets
= g_new0 (gint32
, acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
);
9073 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
)
9074 offsets
[i
] = emit_klass_info (acfg
, MONO_TOKEN_TYPE_DEF
| (i
+ 1));
9076 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
);
9080 typedef struct ClassNameTableEntry
{
9081 guint32 token
, index
;
9082 struct ClassNameTableEntry
*next
;
9083 } ClassNameTableEntry
;
9086 emit_class_name_table (MonoAotCompile
*acfg
)
9088 int i
, table_size
, buf_size
;
9089 guint32 token
, hash
;
9094 ClassNameTableEntry
*entry
, *new_entry
;
9097 * Construct a chained hash table for mapping class names to typedef tokens.
9099 table_size
= g_spaced_primes_closest ((int)(acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
* 1.5));
9100 table
= g_ptr_array_sized_new (table_size
);
9101 for (i
= 0; i
< table_size
; ++i
)
9102 g_ptr_array_add (table
, NULL
);
9103 for (i
= 0; i
< acfg
->image
->tables
[MONO_TABLE_TYPEDEF
].rows
; ++i
) {
9105 token
= MONO_TOKEN_TYPE_DEF
| (i
+ 1);
9106 klass
= mono_class_get_checked (acfg
->image
, token
, &error
);
9108 mono_error_cleanup (&error
);
9111 full_name
= mono_type_get_name_full (mono_class_get_type (klass
), MONO_TYPE_NAME_FORMAT_FULL_NAME
);
9112 hash
= mono_metadata_str_hash (full_name
) % table_size
;
9115 /* FIXME: Allocate from the mempool */
9116 new_entry
= g_new0 (ClassNameTableEntry
, 1);
9117 new_entry
->token
= token
;
9119 entry
= (ClassNameTableEntry
*)g_ptr_array_index (table
, hash
);
9120 if (entry
== NULL
) {
9121 new_entry
->index
= hash
;
9122 g_ptr_array_index (table
, hash
) = new_entry
;
9125 entry
= entry
->next
;
9127 entry
->next
= new_entry
;
9128 new_entry
->index
= table
->len
;
9129 g_ptr_array_add (table
, new_entry
);
9133 /* Emit the table */
9134 buf_size
= table
->len
* 4 + 4;
9135 p
= buf
= (guint8
*)g_malloc0 (buf_size
);
9137 /* FIXME: Optimize memory usage */
9138 g_assert (table_size
< 65000);
9139 encode_int16 (table_size
, p
, &p
);
9140 g_assert (table
->len
< 65000);
9141 for (i
= 0; i
< table
->len
; ++i
) {
9142 ClassNameTableEntry
*entry
= (ClassNameTableEntry
*)g_ptr_array_index (table
, i
);
9144 if (entry
== NULL
) {
9145 encode_int16 (0, p
, &p
);
9146 encode_int16 (0, p
, &p
);
9148 encode_int16 (mono_metadata_token_index (entry
->token
), p
, &p
);
9150 encode_int16 (entry
->next
->index
, p
, &p
);
9152 encode_int16 (0, p
, &p
);
9156 g_assert (p
- buf
<= buf_size
);
9157 g_ptr_array_free (table
, TRUE
);
9159 emit_aot_data (acfg
, MONO_AOT_TABLE_CLASS_NAME
, "class_name_table", buf
, p
- buf
);
9165 emit_image_table (MonoAotCompile
*acfg
)
9171 * The image table is small but referenced in a lot of places.
9172 * So we emit it at once, and reference its elements by an index.
9174 buf_size
= acfg
->image_table
->len
* 28 + 4;
9175 for (i
= 0; i
< acfg
->image_table
->len
; i
++) {
9176 MonoImage
*image
= (MonoImage
*)g_ptr_array_index (acfg
->image_table
, i
);
9177 MonoAssemblyName
*aname
= &image
->assembly
->aname
;
9179 buf_size
+= strlen (image
->assembly_name
) + strlen (image
->guid
) + (aname
->culture
? strlen (aname
->culture
) : 1) + strlen ((char*)aname
->public_key_token
) + 4;
9182 buf
= p
= (guint8
*)g_malloc0 (buf_size
);
9183 encode_int (acfg
->image_table
->len
, p
, &p
);
9184 for (i
= 0; i
< acfg
->image_table
->len
; i
++) {
9185 MonoImage
*image
= (MonoImage
*)g_ptr_array_index (acfg
->image_table
, i
);
9186 MonoAssemblyName
*aname
= &image
->assembly
->aname
;
9188 /* FIXME: Support multi-module assemblies */
9189 g_assert (image
->assembly
->image
== image
);
9191 encode_string (image
->assembly_name
, p
, &p
);
9192 encode_string (image
->guid
, p
, &p
);
9193 encode_string (aname
->culture
? aname
->culture
: "", p
, &p
);
9194 encode_string ((const char*)aname
->public_key_token
, p
, &p
);
9196 while (GPOINTER_TO_UINT (p
) % 8 != 0)
9199 encode_int (aname
->flags
, p
, &p
);
9200 encode_int (aname
->major
, p
, &p
);
9201 encode_int (aname
->minor
, p
, &p
);
9202 encode_int (aname
->build
, p
, &p
);
9203 encode_int (aname
->revision
, p
, &p
);
9205 g_assert (p
- buf
<= buf_size
);
9207 emit_aot_data (acfg
, MONO_AOT_TABLE_IMAGE_TABLE
, "image_table", buf
, p
- buf
);
9213 emit_got_info (MonoAotCompile
*acfg
, gboolean llvm
)
9215 int i
, first_plt_got_patch
= 0, buf_size
;
9217 guint32
*got_info_offsets
;
9218 GotInfo
*info
= llvm
? &acfg
->llvm_got_info
: &acfg
->got_info
;
9220 /* Add the patches needed by the PLT to the GOT */
9222 acfg
->plt_got_offset_base
= acfg
->got_offset
;
9223 first_plt_got_patch
= info
->got_patches
->len
;
9224 for (i
= 1; i
< acfg
->plt_offset
; ++i
) {
9225 MonoPltEntry
*plt_entry
= (MonoPltEntry
*)g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
9227 g_ptr_array_add (info
->got_patches
, plt_entry
->ji
);
9229 acfg
->stats
.got_slot_types
[plt_entry
->ji
->type
] ++;
9232 acfg
->got_offset
+= acfg
->plt_offset
;
9237 * - optimize offsets table.
9238 * - reduce number of exported symbols.
9239 * - emit info for a klass only once.
9240 * - determine when a method uses a GOT slot which is guaranteed to be already
9242 * - clean up and document the code.
9243 * - use String.Empty in class libs.
9246 /* Encode info required to decode shared GOT entries */
9247 buf_size
= info
->got_patches
->len
* 128;
9248 p
= buf
= (guint8
*)mono_mempool_alloc (acfg
->mempool
, buf_size
);
9249 got_info_offsets
= (guint32
*)mono_mempool_alloc (acfg
->mempool
, info
->got_patches
->len
* sizeof (guint32
));
9251 acfg
->plt_got_info_offsets
= (guint32
*)mono_mempool_alloc (acfg
->mempool
, acfg
->plt_offset
* sizeof (guint32
));
9253 if (acfg
->plt_offset
)
9254 acfg
->plt_got_info_offsets
[0] = 0;
9256 for (i
= 0; i
< info
->got_patches
->len
; ++i
) {
9257 MonoJumpInfo
*ji
= (MonoJumpInfo
*)g_ptr_array_index (info
->got_patches
, i
);
9262 encode_value (ji
->type
, p
, &p
);
9264 encode_patch (acfg
, ji
, p
, &p
);
9265 acfg
->stats
.got_slot_info_sizes
[ji
->type
] += p
- p2
;
9266 g_assert (p
- buf
<= buf_size
);
9267 got_info_offsets
[i
] = add_to_blob (acfg
, buf
, p
- buf
);
9269 if (!llvm
&& i
>= first_plt_got_patch
)
9270 acfg
->plt_got_info_offsets
[i
- first_plt_got_patch
+ 1] = got_info_offsets
[i
];
9271 acfg
->stats
.got_info_size
+= p
- buf
;
9274 /* Emit got_info_offsets table */
9276 /* No need to emit offsets for the got plt entries, the plt embeds them directly */
9277 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
);
9281 emit_got (MonoAotCompile
*acfg
)
9283 char symbol
[MAX_SYMBOL_SIZE
];
9285 if (acfg
->aot_opts
.llvm_only
)
9288 /* Don't make GOT global so accesses to it don't need relocations */
9289 sprintf (symbol
, "%s", acfg
->got_symbol
);
9292 emit_unset_mode (acfg
);
9293 fprintf (acfg
->fp
, ".section __DATA, __bss\n");
9294 emit_alignment (acfg
, 8);
9296 emit_info_symbol (acfg
, "jit_got");
9297 fprintf (acfg
->fp
, ".lcomm %s, %d\n", acfg
->got_symbol
, (int)(acfg
->got_offset
* sizeof (gpointer
)));
9299 emit_section_change (acfg
, ".bss", 0);
9300 emit_alignment (acfg
, 8);
9301 if (acfg
->aot_opts
.write_symbols
)
9302 emit_local_symbol (acfg
, symbol
, "got_end", FALSE
);
9303 emit_label (acfg
, symbol
);
9305 emit_info_symbol (acfg
, "jit_got");
9306 if (acfg
->got_offset
> 0)
9307 emit_zero_bytes (acfg
, (int)(acfg
->got_offset
* sizeof (gpointer
)));
9310 sprintf (symbol
, "got_end");
9311 emit_label (acfg
, symbol
);
9314 typedef struct GlobalsTableEntry
{
9315 guint32 value
, index
;
9316 struct GlobalsTableEntry
*next
;
9317 } GlobalsTableEntry
;
9319 #ifdef TARGET_WIN32_MSVC
9320 #define DLL_ENTRY_POINT "DllMain"
9323 emit_library_info (MonoAotCompile
*acfg
)
9325 // Only include for shared libraries linked directly from generated object.
9326 if (link_shared_library (acfg
)) {
9328 char symbol
[MAX_SYMBOL_SIZE
];
9330 // Ask linker to export all global symbols.
9331 emit_section_change (acfg
, ".drectve", 0);
9332 for (guint i
= 0; i
< acfg
->globals
->len
; ++i
) {
9333 name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
9334 g_assert (name
!= NULL
);
9335 sprintf_s (symbol
, MAX_SYMBOL_SIZE
, " /EXPORT:%s", name
);
9336 emit_string (acfg
, symbol
);
9339 // Emit DLLMain function, needed by MSVC linker for DLL's.
9340 // NOTE, DllMain should not go into exports above.
9341 emit_section_change (acfg
, ".text", 0);
9342 emit_global (acfg
, DLL_ENTRY_POINT
, TRUE
);
9343 emit_label (acfg
, DLL_ENTRY_POINT
);
9345 // Simple implementation of DLLMain, just returning TRUE.
9346 // For more information about DLLMain: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx
9347 fprintf (acfg
->fp
, "movl $1, %%eax\n");
9348 fprintf (acfg
->fp
, "ret\n");
9350 // Inform linker about our dll entry function.
9351 emit_section_change (acfg
, ".drectve", 0);
9352 emit_string (acfg
, "/ENTRY:" DLL_ENTRY_POINT
);
9360 emit_library_info (MonoAotCompile
*acfg
)
9367 emit_globals (MonoAotCompile
*acfg
)
9373 GlobalsTableEntry
*entry
, *new_entry
;
9375 if (!acfg
->aot_opts
.static_link
)
9378 if (acfg
->aot_opts
.llvm_only
) {
9379 g_assert (acfg
->globals
->len
== 0);
9384 * When static linking, we emit a table containing our globals.
9388 * Construct a chained hash table for mapping global names to their index in
9389 * the globals table.
9391 table_size
= g_spaced_primes_closest ((int)(acfg
->globals
->len
* 1.5));
9392 table
= g_ptr_array_sized_new (table_size
);
9393 for (i
= 0; i
< table_size
; ++i
)
9394 g_ptr_array_add (table
, NULL
);
9395 for (i
= 0; i
< acfg
->globals
->len
; ++i
) {
9396 char *name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
9398 hash
= mono_metadata_str_hash (name
) % table_size
;
9400 /* FIXME: Allocate from the mempool */
9401 new_entry
= g_new0 (GlobalsTableEntry
, 1);
9402 new_entry
->value
= i
;
9404 entry
= (GlobalsTableEntry
*)g_ptr_array_index (table
, hash
);
9405 if (entry
== NULL
) {
9406 new_entry
->index
= hash
;
9407 g_ptr_array_index (table
, hash
) = new_entry
;
9410 entry
= entry
->next
;
9412 entry
->next
= new_entry
;
9413 new_entry
->index
= table
->len
;
9414 g_ptr_array_add (table
, new_entry
);
9418 /* Emit the table */
9419 sprintf (symbol
, ".Lglobals_hash");
9420 emit_section_change (acfg
, RODATA_SECT
, 0);
9421 emit_alignment (acfg
, 8);
9422 emit_label (acfg
, symbol
);
9424 /* FIXME: Optimize memory usage */
9425 g_assert (table_size
< 65000);
9426 emit_int16 (acfg
, table_size
);
9427 for (i
= 0; i
< table
->len
; ++i
) {
9428 GlobalsTableEntry
*entry
= (GlobalsTableEntry
*)g_ptr_array_index (table
, i
);
9430 if (entry
== NULL
) {
9431 emit_int16 (acfg
, 0);
9432 emit_int16 (acfg
, 0);
9434 emit_int16 (acfg
, entry
->value
+ 1);
9436 emit_int16 (acfg
, entry
->next
->index
);
9438 emit_int16 (acfg
, 0);
9442 /* Emit the names */
9443 for (i
= 0; i
< acfg
->globals
->len
; ++i
) {
9444 char *name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
9446 sprintf (symbol
, "name_%d", i
);
9447 emit_section_change (acfg
, RODATA_SECT
, 1);
9449 emit_alignment (acfg
, 4);
9451 emit_label (acfg
, symbol
);
9452 emit_string (acfg
, name
);
9455 /* Emit the globals table */
9456 sprintf (symbol
, "globals");
9457 emit_section_change (acfg
, ".data", 0);
9458 /* This is not a global, since it is accessed by the init function */
9459 emit_alignment (acfg
, 8);
9460 emit_info_symbol (acfg
, symbol
);
9462 sprintf (symbol
, "%sglobals_hash", acfg
->temp_prefix
);
9463 emit_pointer (acfg
, symbol
);
9465 for (i
= 0; i
< acfg
->globals
->len
; ++i
) {
9466 char *name
= (char *)g_ptr_array_index (acfg
->globals
, i
);
9468 sprintf (symbol
, "name_%d", i
);
9469 emit_pointer (acfg
, symbol
);
9471 g_assert (strlen (name
) < sizeof (symbol
));
9472 sprintf (symbol
, "%s", name
);
9473 emit_pointer (acfg
, symbol
);
9475 /* Null terminate the table */
9476 emit_int32 (acfg
, 0);
9477 emit_int32 (acfg
, 0);
9481 emit_mem_end (MonoAotCompile
*acfg
)
9485 if (acfg
->aot_opts
.llvm_only
)
9488 sprintf (symbol
, "mem_end");
9489 emit_section_change (acfg
, ".text", 1);
9490 emit_alignment_code (acfg
, 8);
9491 emit_label (acfg
, symbol
);
9495 init_aot_file_info (MonoAotCompile
*acfg
, MonoAotFileInfo
*info
)
9499 info
->version
= MONO_AOT_FILE_VERSION
;
9500 info
->plt_got_offset_base
= acfg
->plt_got_offset_base
;
9501 info
->got_size
= acfg
->got_offset
* sizeof (gpointer
);
9502 info
->plt_size
= acfg
->plt_offset
;
9503 info
->nmethods
= acfg
->nmethods
;
9504 info
->flags
= acfg
->flags
;
9505 info
->opts
= acfg
->opts
;
9506 info
->simd_opts
= acfg
->simd_opts
;
9507 info
->gc_name_index
= acfg
->gc_name_offset
;
9508 info
->datafile_size
= acfg
->datafile_offset
;
9509 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
9510 info
->table_offsets
[i
] = acfg
->table_offsets
[i
];
9511 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
9512 info
->num_trampolines
[i
] = acfg
->num_trampolines
[i
];
9513 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
9514 info
->trampoline_got_offset_base
[i
] = acfg
->trampoline_got_offset_base
[i
];
9515 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
9516 info
->trampoline_size
[i
] = acfg
->trampoline_size
[i
];
9517 info
->num_rgctx_fetch_trampolines
= acfg
->aot_opts
.nrgctx_fetch_trampolines
;
9519 info
->double_align
= MONO_ABI_ALIGNOF (double);
9520 info
->long_align
= MONO_ABI_ALIGNOF (gint64
);
9521 info
->generic_tramp_num
= MONO_TRAMPOLINE_NUM
;
9522 info
->tramp_page_size
= acfg
->tramp_page_size
;
9523 info
->nshared_got_entries
= acfg
->nshared_got_entries
;
9524 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
9525 info
->tramp_page_code_offsets
[i
] = acfg
->tramp_page_code_offsets
[i
];
9527 memcpy(&info
->aotid
, acfg
->image
->aotid
, 16);
9531 emit_aot_file_info (MonoAotCompile
*acfg
, MonoAotFileInfo
*info
)
9533 char symbol
[MAX_SYMBOL_SIZE
];
9535 const char **symbols
;
9537 symbols
= g_new0 (const char *, MONO_AOT_FILE_INFO_NUM_SYMBOLS
);
9539 symbols
[sindex
++] = acfg
->got_symbol
;
9541 symbols
[sindex
++] = g_strdup_printf ("%s%s", acfg
->user_symbol_prefix
, acfg
->llvm_got_symbol
);
9542 symbols
[sindex
++] = acfg
->llvm_eh_frame_symbol
;
9544 symbols
[sindex
++] = NULL
;
9545 symbols
[sindex
++] = NULL
;
9547 /* llvm_get_method */
9548 symbols
[sindex
++] = NULL
;
9549 /* llvm_get_unbox_tramp */
9550 symbols
[sindex
++] = NULL
;
9551 if (!acfg
->aot_opts
.llvm_only
) {
9552 symbols
[sindex
++] = "jit_code_start";
9553 symbols
[sindex
++] = "jit_code_end";
9554 symbols
[sindex
++] = "method_addresses";
9556 symbols
[sindex
++] = NULL
;
9557 symbols
[sindex
++] = NULL
;
9558 symbols
[sindex
++] = NULL
;
9560 if (acfg
->data_outfile
) {
9561 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
9562 symbols
[sindex
++] = NULL
;
9564 symbols
[sindex
++] = "blob";
9565 symbols
[sindex
++] = "class_name_table";
9566 symbols
[sindex
++] = "class_info_offsets";
9567 symbols
[sindex
++] = "method_info_offsets";
9568 symbols
[sindex
++] = "ex_info_offsets";
9569 symbols
[sindex
++] = "extra_method_info_offsets";
9570 symbols
[sindex
++] = "extra_method_table";
9571 symbols
[sindex
++] = "got_info_offsets";
9573 symbols
[sindex
++] = "llvm_got_info_offsets";
9575 symbols
[sindex
++] = NULL
;
9576 symbols
[sindex
++] = "image_table";
9578 symbols
[sindex
++] = "mem_end";
9579 symbols
[sindex
++] = "assembly_guid";
9580 symbols
[sindex
++] = "runtime_version";
9581 if (acfg
->num_trampoline_got_entries
) {
9582 symbols
[sindex
++] = "specific_trampolines";
9583 symbols
[sindex
++] = "static_rgctx_trampolines";
9584 symbols
[sindex
++] = "imt_trampolines";
9585 symbols
[sindex
++] = "gsharedvt_arg_trampolines";
9587 symbols
[sindex
++] = NULL
;
9588 symbols
[sindex
++] = NULL
;
9589 symbols
[sindex
++] = NULL
;
9590 symbols
[sindex
++] = NULL
;
9592 if (acfg
->aot_opts
.static_link
) {
9593 symbols
[sindex
++] = "globals";
9595 symbols
[sindex
++] = NULL
;
9597 symbols
[sindex
++] = "assembly_name";
9598 symbols
[sindex
++] = "plt";
9599 symbols
[sindex
++] = "plt_end";
9600 symbols
[sindex
++] = "unwind_info";
9601 if (!acfg
->aot_opts
.llvm_only
) {
9602 symbols
[sindex
++] = "unbox_trampolines";
9603 symbols
[sindex
++] = "unbox_trampolines_end";
9604 symbols
[sindex
++] = "unbox_trampoline_addresses";
9606 symbols
[sindex
++] = NULL
;
9607 symbols
[sindex
++] = NULL
;
9608 symbols
[sindex
++] = NULL
;
9611 g_assert (sindex
== MONO_AOT_FILE_INFO_NUM_SYMBOLS
);
9613 sprintf (symbol
, "%smono_aot_file_info", acfg
->user_symbol_prefix
);
9614 emit_section_change (acfg
, ".data", 0);
9615 emit_alignment (acfg
, 8);
9616 emit_label (acfg
, symbol
);
9617 if (!acfg
->aot_opts
.static_link
)
9618 emit_global (acfg
, symbol
, FALSE
);
9620 /* The data emitted here must match MonoAotFileInfo. */
9622 emit_int32 (acfg
, info
->version
);
9623 emit_int32 (acfg
, info
->dummy
);
9626 * We emit pointers to our data structures instead of emitting global symbols which
9627 * point to them, to reduce the number of globals, and because using globals leads to
9628 * various problems (i.e. arm/thumb).
9630 for (i
= 0; i
< MONO_AOT_FILE_INFO_NUM_SYMBOLS
; ++i
)
9631 emit_pointer (acfg
, symbols
[i
]);
9633 emit_int32 (acfg
, info
->plt_got_offset_base
);
9634 emit_int32 (acfg
, info
->got_size
);
9635 emit_int32 (acfg
, info
->plt_size
);
9636 emit_int32 (acfg
, info
->nmethods
);
9637 emit_int32 (acfg
, info
->flags
);
9638 emit_int32 (acfg
, info
->opts
);
9639 emit_int32 (acfg
, info
->simd_opts
);
9640 emit_int32 (acfg
, info
->gc_name_index
);
9641 emit_int32 (acfg
, info
->num_rgctx_fetch_trampolines
);
9642 emit_int32 (acfg
, info
->double_align
);
9643 emit_int32 (acfg
, info
->long_align
);
9644 emit_int32 (acfg
, info
->generic_tramp_num
);
9645 emit_int32 (acfg
, info
->tramp_page_size
);
9646 emit_int32 (acfg
, info
->nshared_got_entries
);
9647 emit_int32 (acfg
, info
->datafile_size
);
9649 for (i
= 0; i
< MONO_AOT_TABLE_NUM
; ++i
)
9650 emit_int32 (acfg
, info
->table_offsets
[i
]);
9651 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
9652 emit_int32 (acfg
, info
->num_trampolines
[i
]);
9653 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
9654 emit_int32 (acfg
, info
->trampoline_got_offset_base
[i
]);
9655 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
9656 emit_int32 (acfg
, info
->trampoline_size
[i
]);
9657 for (i
= 0; i
< MONO_AOT_TRAMP_NUM
; ++i
)
9658 emit_int32 (acfg
, info
->tramp_page_code_offsets
[i
]);
9660 emit_bytes (acfg
, info
->aotid
, 16);
9662 if (acfg
->aot_opts
.static_link
) {
9663 emit_global_inner (acfg
, acfg
->static_linking_symbol
, FALSE
);
9664 emit_alignment (acfg
, sizeof (gpointer
));
9665 emit_label (acfg
, acfg
->static_linking_symbol
);
9666 emit_pointer_2 (acfg
, acfg
->user_symbol_prefix
, "mono_aot_file_info");
9671 * Emit a structure containing all the information not stored elsewhere.
9674 emit_file_info (MonoAotCompile
*acfg
)
9677 MonoAotFileInfo
*info
;
9679 if (acfg
->aot_opts
.bind_to_runtime_version
) {
9680 build_info
= mono_get_runtime_build_info ();
9681 emit_string_symbol (acfg
, "runtime_version", build_info
);
9682 g_free (build_info
);
9684 emit_string_symbol (acfg
, "runtime_version", "");
9687 emit_string_symbol (acfg
, "assembly_guid" , acfg
->image
->guid
);
9689 /* Emit a string holding the assembly name */
9690 emit_string_symbol (acfg
, "assembly_name", acfg
->image
->assembly
->aname
.name
);
9692 info
= g_new0 (MonoAotFileInfo
, 1);
9693 init_aot_file_info (acfg
, info
);
9695 if (acfg
->aot_opts
.static_link
) {
9696 char symbol
[MAX_SYMBOL_SIZE
];
9700 * Emit a global symbol which can be passed by an embedding app to
9701 * mono_aot_register_module (). The symbol points to a pointer to the the file info
9704 sprintf (symbol
, "%smono_aot_module_%s_info", acfg
->user_symbol_prefix
, acfg
->image
->assembly
->aname
.name
);
9706 /* Get rid of characters which cannot occur in symbols */
9708 for (p
= symbol
; *p
; ++p
) {
9709 if (!(isalnum (*p
) || *p
== '_'))
9712 acfg
->static_linking_symbol
= g_strdup (symbol
);
9716 mono_llvm_emit_aot_file_info (info
, acfg
->has_jitted_code
);
9718 emit_aot_file_info (acfg
, info
);
9722 emit_blob (MonoAotCompile
*acfg
)
9724 acfg
->blob_closed
= TRUE
;
9726 emit_aot_data (acfg
, MONO_AOT_TABLE_BLOB
, "blob", (guint8
*)acfg
->blob
.data
, acfg
->blob
.index
);
9730 emit_objc_selectors (MonoAotCompile
*acfg
)
9735 if (!acfg
->objc_selectors
|| acfg
->objc_selectors
->len
== 0)
9740 * cat > foo.m << EOF
9743 * return @selector(print:);
9748 mono_img_writer_emit_unset_mode (acfg
->w
);
9749 g_assert (acfg
->fp
);
9750 fprintf (acfg
->fp
, ".section __DATA,__objc_selrefs,literal_pointers,no_dead_strip\n");
9751 fprintf (acfg
->fp
, ".align 3\n");
9752 for (i
= 0; i
< acfg
->objc_selectors
->len
; ++i
) {
9753 sprintf (symbol
, "L_OBJC_SELECTOR_REFERENCES_%d", i
);
9754 emit_label (acfg
, symbol
);
9755 sprintf (symbol
, "L_OBJC_METH_VAR_NAME_%d", i
);
9756 emit_pointer (acfg
, symbol
);
9759 fprintf (acfg
->fp
, ".section __TEXT,__cstring,cstring_literals\n");
9760 for (i
= 0; i
< acfg
->objc_selectors
->len
; ++i
) {
9761 fprintf (acfg
->fp
, "L_OBJC_METH_VAR_NAME_%d:\n", i
);
9762 fprintf (acfg
->fp
, ".asciz \"%s\"\n", (char*)g_ptr_array_index (acfg
->objc_selectors
, i
));
9765 fprintf (acfg
->fp
, ".section __DATA,__objc_imageinfo,regular,no_dead_strip\n");
9766 fprintf (acfg
->fp
, ".align 3\n");
9767 fprintf (acfg
->fp
, "L_OBJC_IMAGE_INFO:\n");
9768 fprintf (acfg
->fp
, ".long 0\n");
9769 fprintf (acfg
->fp
, ".long 16\n");
9773 emit_dwarf_info (MonoAotCompile
*acfg
)
9775 #ifdef EMIT_DWARF_INFO
9779 /* DIEs for methods */
9780 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
9781 MonoCompile
*cfg
= acfg
->cfgs
[i
];
9786 // FIXME: LLVM doesn't define .Lme_...
9787 if (cfg
->compile_llvm
)
9790 sprintf (symbol2
, "%sme_%x", acfg
->temp_prefix
, i
);
9792 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 ()));
9798 collect_methods (MonoAotCompile
*acfg
)
9801 MonoImage
*image
= acfg
->image
;
9803 /* Collect methods */
9804 for (i
= 0; i
< image
->tables
[MONO_TABLE_METHOD
].rows
; ++i
) {
9807 guint32 token
= MONO_TOKEN_METHOD_DEF
| (i
+ 1);
9809 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, &error
);
9812 aot_printerrf (acfg
, "Failed to load method 0x%x from '%s' due to %s.\n", token
, image
->name
, mono_error_get_message (&error
));
9813 aot_printerrf (acfg
, "Run with MONO_LOG_LEVEL=debug for more information.\n");
9814 mono_error_cleanup (&error
);
9818 /* Load all methods eagerly to skip the slower lazy loading code */
9819 mono_class_setup_methods (method
->klass
);
9821 if (mono_aot_mode_is_full (&acfg
->aot_opts
) && method
->iflags
& METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL
) {
9822 /* Compile the wrapper instead */
9823 /* We do this here instead of add_wrappers () because it is easy to do it here */
9824 MonoMethod
*wrapper
= mono_marshal_get_native_wrapper (method
, TRUE
, TRUE
);
9828 /* FIXME: Some mscorlib methods don't have debug info */
9830 if (acfg->aot_opts.soft_debug && !method->wrapper_type) {
9831 if (!((method->flags & METHOD_ATTRIBUTE_PINVOKE_IMPL) ||
9832 (method->iflags & METHOD_IMPL_ATTRIBUTE_RUNTIME) ||
9833 (method->flags & METHOD_ATTRIBUTE_ABSTRACT) ||
9834 (method->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL))) {
9835 if (!mono_debug_lookup_method (method)) {
9836 fprintf (stderr, "Method %s has no debug info, probably the .mdb file for the assembly is missing.\n", mono_method_get_full_name (method));
9843 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
))
9844 /* Compile the ref shared version instead */
9845 method
= mini_get_shared_method (method
);
9847 /* Since we add the normal methods first, their index will be equal to their zero based token index */
9848 add_method_with_index (acfg
, method
, i
, FALSE
);
9849 acfg
->method_index
++;
9852 /* gsharedvt methods */
9853 for (mindex
= 0; mindex
< image
->tables
[MONO_TABLE_METHOD
].rows
; ++mindex
) {
9856 guint32 token
= MONO_TOKEN_METHOD_DEF
| (mindex
+ 1);
9858 if (!(acfg
->opts
& MONO_OPT_GSHAREDVT
))
9861 method
= mono_get_method_checked (acfg
->image
, token
, NULL
, NULL
, &error
);
9862 report_loader_error (acfg
, &error
, "Failed to load method token 0x%x due to %s\n", i
, mono_error_get_message (&error
));
9864 if (method
->is_generic
|| mono_class_is_gtd (method
->klass
)) {
9865 MonoMethod
*gshared
;
9867 gshared
= mini_get_shared_method_full (method
, TRUE
, TRUE
);
9868 add_extra_method (acfg
, gshared
);
9872 if (mono_aot_mode_is_full (&acfg
->aot_opts
))
9873 add_generic_instances (acfg
);
9875 if (mono_aot_mode_is_full (&acfg
->aot_opts
))
9876 add_wrappers (acfg
);
9881 compile_methods (MonoAotCompile
*acfg
)
9885 if (acfg
->aot_opts
.nthreads
> 0) {
9889 MonoThreadHandle
*thread_handle
;
9890 gpointer
*user_data
;
9891 MonoMethod
**methods
;
9893 methods_len
= acfg
->methods
->len
;
9895 len
= acfg
->methods
->len
/ acfg
->aot_opts
.nthreads
;
9898 * Partition the list of methods into fragments, and hand it to threads to
9901 threads
= g_ptr_array_new ();
9902 /* Make a copy since acfg->methods is modified by compile_method () */
9903 methods
= g_new0 (MonoMethod
*, methods_len
);
9904 //memcpy (methods, g_ptr_array_index (acfg->methods, 0), sizeof (MonoMethod*) * methods_len);
9905 for (i
= 0; i
< methods_len
; ++i
)
9906 methods
[i
] = (MonoMethod
*)g_ptr_array_index (acfg
->methods
, i
);
9908 while (i
< methods_len
) {
9909 frag
= g_ptr_array_new ();
9910 for (j
= 0; j
< len
; ++j
) {
9911 if (i
< methods_len
) {
9912 g_ptr_array_add (frag
, methods
[i
]);
9917 user_data
= g_new0 (gpointer
, 3);
9918 user_data
[0] = mono_domain_get ();
9919 user_data
[1] = acfg
;
9920 user_data
[2] = frag
;
9922 thread_handle
= mono_threads_create_thread (compile_thread_main
, (gpointer
) user_data
, NULL
, NULL
);
9923 g_ptr_array_add (threads
, thread_handle
);
9927 for (i
= 0; i
< threads
->len
; ++i
) {
9928 mono_thread_info_wait_one_handle (g_ptr_array_index (threads
, i
), INFINITE
, FALSE
);
9929 mono_threads_close_thread_handle (g_ptr_array_index (threads
, i
));
9935 /* Compile methods added by compile_method () or all methods if nthreads == 0 */
9936 for (i
= methods_len
; i
< acfg
->methods
->len
; ++i
) {
9937 /* This can new methods to acfg->methods */
9938 compile_method (acfg
, (MonoMethod
*)g_ptr_array_index (acfg
->methods
, i
));
9943 compile_asm (MonoAotCompile
*acfg
)
9945 char *command
, *objfile
;
9946 char *outfile_name
, *tmp_outfile_name
, *llvm_ofile
;
9947 const char *tool_prefix
= acfg
->aot_opts
.tool_prefix
? acfg
->aot_opts
.tool_prefix
: "";
9948 char *ld_flags
= acfg
->aot_opts
.ld_flags
? acfg
->aot_opts
.ld_flags
: g_strdup("");
9950 #ifdef TARGET_WIN32_MSVC
9951 #define AS_OPTIONS "-c -x assembler"
9952 #elif defined(TARGET_AMD64) && !defined(TARGET_MACH)
9953 #define AS_OPTIONS "--64"
9954 #elif defined(TARGET_POWERPC64)
9955 #define AS_OPTIONS "-a64 -mppc64"
9956 #elif defined(sparc) && SIZEOF_VOID_P == 8
9957 #define AS_OPTIONS "-xarch=v9"
9958 #elif defined(TARGET_X86) && defined(TARGET_MACH) && !defined(__native_client_codegen__)
9959 #define AS_OPTIONS "-arch i386"
9961 #define AS_OPTIONS ""
9964 #ifdef __native_client_codegen__
9965 #if defined(TARGET_AMD64)
9966 #define AS_NAME "nacl64-as"
9968 #define AS_NAME "nacl-as"
9970 #elif defined(TARGET_OSX)
9971 #define AS_NAME "clang"
9972 #elif defined(TARGET_WIN32_MSVC)
9973 #define AS_NAME "clang.exe"
9975 #define AS_NAME "as"
9978 #ifdef TARGET_WIN32_MSVC
9979 #define AS_OBJECT_FILE_SUFFIX "obj"
9981 #define AS_OBJECT_FILE_SUFFIX "o"
9985 #define LD_NAME "ld"
9986 #define LD_OPTIONS "-shared -G"
9987 #elif defined(__ppc__) && defined(TARGET_MACH)
9988 #define LD_NAME "gcc"
9989 #define LD_OPTIONS "-dynamiclib"
9990 #elif defined(TARGET_AMD64) && defined(TARGET_MACH)
9991 #define LD_NAME "clang"
9992 #define LD_OPTIONS "--shared"
9993 #elif defined(TARGET_WIN32_MSVC)
9994 #define LD_NAME "link.exe"
9995 #define LD_OPTIONS "/DLL /MACHINE:X64 /NOLOGO"
9996 #elif defined(TARGET_WIN32) && !defined(TARGET_ANDROID)
9997 #define LD_NAME "gcc"
9998 #define LD_OPTIONS "-shared"
9999 #elif defined(TARGET_X86) && defined(TARGET_MACH) && !defined(__native_client_codegen__)
10000 #define LD_NAME "clang"
10001 #define LD_OPTIONS "-m32 -dynamiclib"
10002 #elif defined(TARGET_ARM) && !defined(TARGET_ANDROID)
10003 #define LD_NAME "gcc"
10004 #define LD_OPTIONS "--shared"
10005 #elif defined(TARGET_POWERPC64)
10006 #define LD_OPTIONS "-m elf64ppc"
10010 #define LD_OPTIONS ""
10013 if (acfg
->aot_opts
.asm_only
) {
10014 aot_printf (acfg
, "Output file: '%s'.\n", acfg
->tmpfname
);
10015 if (acfg
->aot_opts
.static_link
)
10016 aot_printf (acfg
, "Linking symbol: '%s'.\n", acfg
->static_linking_symbol
);
10018 aot_printf (acfg
, "LLVM output file: '%s'.\n", acfg
->llvm_sfile
);
10022 if (acfg
->aot_opts
.static_link
) {
10023 if (acfg
->aot_opts
.outfile
)
10024 objfile
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
10026 objfile
= g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->image
->name
);
10028 objfile
= g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
);
10032 g_string_append (acfg
->as_args
, "-c -x assembler");
10035 command
= g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix
, AS_NAME
, AS_OPTIONS
,
10036 acfg
->as_args
? acfg
->as_args
->str
: "",
10037 wrap_path (objfile
), wrap_path (acfg
->tmpfname
));
10038 aot_printf (acfg
, "Executing the native assembler: %s\n", command
);
10039 if (execute_system (command
) != 0) {
10045 if (acfg
->llvm
&& !acfg
->llvm_owriter
) {
10046 command
= g_strdup_printf ("\"%s%s\" %s %s -o %s %s", tool_prefix
, AS_NAME
, AS_OPTIONS
,
10047 acfg
->as_args
? acfg
->as_args
->str
: "",
10048 wrap_path (acfg
->llvm_ofile
), wrap_path (acfg
->llvm_sfile
));
10049 aot_printf (acfg
, "Executing the native assembler: %s\n", command
);
10050 if (execute_system (command
) != 0) {
10059 if (acfg
->aot_opts
.static_link
) {
10060 aot_printf (acfg
, "Output file: '%s'.\n", objfile
);
10061 aot_printf (acfg
, "Linking symbol: '%s'.\n", acfg
->static_linking_symbol
);
10066 if (acfg
->aot_opts
.outfile
)
10067 outfile_name
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
10069 outfile_name
= g_strdup_printf ("%s%s", acfg
->image
->name
, MONO_SOLIB_EXT
);
10071 tmp_outfile_name
= g_strdup_printf ("%s.tmp", outfile_name
);
10074 llvm_ofile
= g_strdup_printf ("\"%s\"", acfg
->llvm_ofile
);
10076 llvm_ofile
= g_strdup ("");
10079 /* replace the ; flags separators with spaces */
10080 g_strdelimit (ld_flags
, ";", ' ');
10082 if (acfg
->aot_opts
.llvm_only
)
10083 ld_flags
= g_strdup_printf ("%s %s", ld_flags
, "-lstdc++");
10085 #ifdef TARGET_WIN32_MSVC
10086 g_assert (tmp_outfile_name
!= NULL
);
10087 g_assert (objfile
!= NULL
);
10088 command
= g_strdup_printf ("\"%s%s\" %s %s /OUT:\"%s\" \"%s\"", tool_prefix
, LD_NAME
, LD_OPTIONS
,
10089 ld_flags
, tmp_outfile_name
, objfile
);
10090 #elif defined(LD_NAME)
10091 command
= g_strdup_printf ("%s%s %s -o %s %s %s %s", tool_prefix
, LD_NAME
, LD_OPTIONS
,
10092 wrap_path (tmp_outfile_name
), wrap_path (llvm_ofile
),
10093 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
)), ld_flags
);
10096 if (acfg
->aot_opts
.tool_prefix
) {
10097 /* Cross compiling */
10098 command
= g_strdup_printf ("\"%sld\" %s -shared -o %s %s %s %s", tool_prefix
, LD_OPTIONS
,
10099 wrap_path (tmp_outfile_name
), wrap_path (llvm_ofile
),
10100 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
)), ld_flags
);
10102 char *args
= g_strdup_printf ("%s -shared -o %s %s %s %s", LD_OPTIONS
,
10103 wrap_path (tmp_outfile_name
), wrap_path (llvm_ofile
),
10104 wrap_path (g_strdup_printf ("%s." AS_OBJECT_FILE_SUFFIX
, acfg
->tmpfname
)), ld_flags
);
10107 command
= g_strdup_printf ("clang++ %s", args
);
10109 command
= g_strdup_printf ("\"%sld\" %s", tool_prefix
, args
);
10114 aot_printf (acfg
, "Executing the native linker: %s\n", command
);
10115 if (execute_system (command
) != 0) {
10116 g_free (tmp_outfile_name
);
10117 g_free (outfile_name
);
10126 /*com = g_strdup_printf ("strip --strip-unneeded %s%s", acfg->image->name, MONO_SOLIB_EXT);
10127 printf ("Stripping the binary: %s\n", com);
10128 execute_system (com);
10131 #if defined(TARGET_ARM) && !defined(TARGET_MACH)
10133 * gas generates 'mapping symbols' each time code and data is mixed, which
10134 * happens a lot in emit_and_reloc_code (), so we need to get rid of them.
10136 command
= g_strdup_printf ("\"%sstrip\" --strip-symbol=\\$a --strip-symbol=\\$d %s", wrap_path(tool_prefix
), wrap_path(tmp_outfile_name
));
10137 aot_printf (acfg
, "Stripping the binary: %s\n", command
);
10138 if (execute_system (command
) != 0) {
10139 g_free (tmp_outfile_name
);
10140 g_free (outfile_name
);
10147 if (0 != rename (tmp_outfile_name
, outfile_name
)) {
10148 if (G_FILE_ERROR_EXIST
== g_file_error_from_errno (errno
)) {
10149 /* Since we are rebuilding the module we need to be able to replace any old copies. Remove old file and retry rename operation. */
10150 unlink (outfile_name
);
10151 rename (tmp_outfile_name
, outfile_name
);
10155 #if defined(TARGET_MACH)
10156 command
= g_strdup_printf ("dsymutil \"%s\"", outfile_name
);
10157 aot_printf (acfg
, "Executing dsymutil: %s\n", command
);
10158 if (execute_system (command
) != 0) {
10163 if (!acfg
->aot_opts
.save_temps
)
10166 g_free (tmp_outfile_name
);
10167 g_free (outfile_name
);
10170 if (acfg
->aot_opts
.save_temps
)
10171 aot_printf (acfg
, "Retained input file.\n");
10173 unlink (acfg
->tmpfname
);
10178 static void init_got_info (GotInfo
*info
)
10182 info
->patch_to_got_offset
= g_hash_table_new (mono_patch_info_hash
, mono_patch_info_equal
);
10183 info
->patch_to_got_offset_by_type
= g_new0 (GHashTable
*, MONO_PATCH_INFO_NUM
);
10184 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
)
10185 info
->patch_to_got_offset_by_type
[i
] = g_hash_table_new (mono_patch_info_hash
, mono_patch_info_equal
);
10186 info
->got_patches
= g_ptr_array_new ();
10189 static MonoAotCompile
*
10190 acfg_create (MonoAssembly
*ass
, guint32 opts
)
10192 MonoImage
*image
= ass
->image
;
10193 MonoAotCompile
*acfg
;
10195 acfg
= g_new0 (MonoAotCompile
, 1);
10196 acfg
->methods
= g_ptr_array_new ();
10197 acfg
->method_indexes
= g_hash_table_new (NULL
, NULL
);
10198 acfg
->method_depth
= g_hash_table_new (NULL
, NULL
);
10199 acfg
->plt_offset_to_entry
= g_hash_table_new (NULL
, NULL
);
10200 acfg
->patch_to_plt_entry
= g_new0 (GHashTable
*, MONO_PATCH_INFO_NUM
);
10201 acfg
->method_to_cfg
= g_hash_table_new (NULL
, NULL
);
10202 acfg
->token_info_hash
= g_hash_table_new_full (NULL
, NULL
, NULL
, NULL
);
10203 acfg
->method_to_pinvoke_import
= g_hash_table_new_full (NULL
, NULL
, NULL
, g_free
);
10204 acfg
->image_hash
= g_hash_table_new (NULL
, NULL
);
10205 acfg
->image_table
= g_ptr_array_new ();
10206 acfg
->globals
= g_ptr_array_new ();
10207 acfg
->image
= image
;
10209 /* TODO: Write out set of SIMD instructions used, rather than just those available */
10210 acfg
->simd_opts
= mono_arch_cpu_enumerate_simd_versions ();
10211 acfg
->mempool
= mono_mempool_new ();
10212 acfg
->extra_methods
= g_ptr_array_new ();
10213 acfg
->unwind_info_offsets
= g_hash_table_new (NULL
, NULL
);
10214 acfg
->unwind_ops
= g_ptr_array_new ();
10215 acfg
->method_label_hash
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, NULL
);
10216 acfg
->method_order
= g_ptr_array_new ();
10217 acfg
->export_names
= g_hash_table_new (NULL
, NULL
);
10218 acfg
->klass_blob_hash
= g_hash_table_new (NULL
, NULL
);
10219 acfg
->method_blob_hash
= g_hash_table_new (NULL
, NULL
);
10220 acfg
->plt_entry_debug_sym_cache
= g_hash_table_new (g_str_hash
, g_str_equal
);
10221 acfg
->gsharedvt_in_signatures
= g_hash_table_new ((GHashFunc
)mono_signature_hash
, (GEqualFunc
)mono_metadata_signature_equal
);
10222 acfg
->gsharedvt_out_signatures
= g_hash_table_new ((GHashFunc
)mono_signature_hash
, (GEqualFunc
)mono_metadata_signature_equal
);
10223 mono_os_mutex_init_recursive (&acfg
->mutex
);
10225 init_got_info (&acfg
->got_info
);
10226 init_got_info (&acfg
->llvm_got_info
);
10232 got_info_free (GotInfo
*info
)
10236 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
)
10237 g_hash_table_destroy (info
->patch_to_got_offset_by_type
[i
]);
10238 g_free (info
->patch_to_got_offset_by_type
);
10239 g_hash_table_destroy (info
->patch_to_got_offset
);
10240 g_ptr_array_free (info
->got_patches
, TRUE
);
10244 acfg_free (MonoAotCompile
*acfg
)
10248 mono_img_writer_destroy (acfg
->w
);
10249 for (i
= 0; i
< acfg
->nmethods
; ++i
)
10250 if (acfg
->cfgs
[i
])
10251 mono_destroy_compile (acfg
->cfgs
[i
]);
10253 g_free (acfg
->cfgs
);
10255 g_free (acfg
->static_linking_symbol
);
10256 g_free (acfg
->got_symbol
);
10257 g_free (acfg
->plt_symbol
);
10258 g_ptr_array_free (acfg
->methods
, TRUE
);
10259 g_ptr_array_free (acfg
->image_table
, TRUE
);
10260 g_ptr_array_free (acfg
->globals
, TRUE
);
10261 g_ptr_array_free (acfg
->unwind_ops
, TRUE
);
10262 g_hash_table_destroy (acfg
->method_indexes
);
10263 g_hash_table_destroy (acfg
->method_depth
);
10264 g_hash_table_destroy (acfg
->plt_offset_to_entry
);
10265 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
) {
10266 if (acfg
->patch_to_plt_entry
[i
])
10267 g_hash_table_destroy (acfg
->patch_to_plt_entry
[i
]);
10269 g_free (acfg
->patch_to_plt_entry
);
10270 g_hash_table_destroy (acfg
->method_to_cfg
);
10271 g_hash_table_destroy (acfg
->token_info_hash
);
10272 g_hash_table_destroy (acfg
->method_to_pinvoke_import
);
10273 g_hash_table_destroy (acfg
->image_hash
);
10274 g_hash_table_destroy (acfg
->unwind_info_offsets
);
10275 g_hash_table_destroy (acfg
->method_label_hash
);
10276 if (acfg
->typespec_classes
)
10277 g_hash_table_destroy (acfg
->typespec_classes
);
10278 g_hash_table_destroy (acfg
->export_names
);
10279 g_hash_table_destroy (acfg
->plt_entry_debug_sym_cache
);
10280 g_hash_table_destroy (acfg
->klass_blob_hash
);
10281 g_hash_table_destroy (acfg
->method_blob_hash
);
10282 got_info_free (&acfg
->got_info
);
10283 got_info_free (&acfg
->llvm_got_info
);
10284 mono_mempool_destroy (acfg
->mempool
);
10288 #define WRAPPER(e,n) n,
10289 static const char* const
10290 wrapper_type_names
[MONO_WRAPPER_NUM
+ 1] = {
10291 #include "mono/metadata/wrapper-types.h"
10295 static G_GNUC_UNUSED
const char*
10296 get_wrapper_type_name (int type
)
10298 return wrapper_type_names
[type
];
10304 static void aot_dump (MonoAotCompile
*acfg
)
10310 mono_json_writer_init (&writer
);
10312 mono_json_writer_object_begin(&writer
);
10315 mono_json_writer_indent (&writer
);
10316 mono_json_writer_object_key(&writer
, "methods");
10317 mono_json_writer_array_begin (&writer
);
10320 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
10322 MonoMethod
*method
;
10325 cfg
= acfg
->cfgs
[i
];
10329 method
= cfg
->orig_method
;
10331 mono_json_writer_indent (&writer
);
10332 mono_json_writer_object_begin(&writer
);
10334 mono_json_writer_indent (&writer
);
10335 mono_json_writer_object_key(&writer
, "name");
10336 mono_json_writer_printf (&writer
, "\"%s\",\n", method
->name
);
10338 mono_json_writer_indent (&writer
);
10339 mono_json_writer_object_key(&writer
, "signature");
10340 mono_json_writer_printf (&writer
, "\"%s\",\n", mono_method_get_full_name (method
));
10342 mono_json_writer_indent (&writer
);
10343 mono_json_writer_object_key(&writer
, "code_size");
10344 mono_json_writer_printf (&writer
, "\"%d\",\n", cfg
->code_size
);
10346 klass
= method
->klass
;
10348 mono_json_writer_indent (&writer
);
10349 mono_json_writer_object_key(&writer
, "class");
10350 mono_json_writer_printf (&writer
, "\"%s\",\n", klass
->name
);
10352 mono_json_writer_indent (&writer
);
10353 mono_json_writer_object_key(&writer
, "namespace");
10354 mono_json_writer_printf (&writer
, "\"%s\",\n", klass
->name_space
);
10356 mono_json_writer_indent (&writer
);
10357 mono_json_writer_object_key(&writer
, "wrapper_type");
10358 mono_json_writer_printf (&writer
, "\"%s\",\n", get_wrapper_type_name(method
->wrapper_type
));
10360 mono_json_writer_indent_pop (&writer
);
10361 mono_json_writer_indent (&writer
);
10362 mono_json_writer_object_end (&writer
);
10363 mono_json_writer_printf (&writer
, ",\n");
10366 mono_json_writer_indent_pop (&writer
);
10367 mono_json_writer_indent (&writer
);
10368 mono_json_writer_array_end (&writer
);
10369 mono_json_writer_printf (&writer
, ",\n");
10373 mono_json_writer_indent_push (&writer
);
10374 mono_json_writer_indent (&writer
);
10375 mono_json_writer_object_key(&writer
, "plt");
10376 mono_json_writer_array_begin (&writer
);
10378 for (i
= 0; i
< acfg
->plt_offset
; ++i
) {
10379 MonoPltEntry
*plt_entry
= NULL
;
10384 * The first plt entry is unused.
10388 plt_entry
= g_hash_table_lookup (acfg
->plt_offset_to_entry
, GUINT_TO_POINTER (i
));
10389 ji
= plt_entry
->ji
;
10391 mono_json_writer_indent (&writer
);
10392 mono_json_writer_printf (&writer
, "{ ");
10393 mono_json_writer_object_key(&writer
, "symbol");
10394 mono_json_writer_printf (&writer
, "\"%s\" },\n", plt_entry
->symbol
);
10397 mono_json_writer_indent_pop (&writer
);
10398 mono_json_writer_indent (&writer
);
10399 mono_json_writer_array_end (&writer
);
10400 mono_json_writer_printf (&writer
, ",\n");
10405 mono_json_writer_indent_push (&writer
);
10406 mono_json_writer_indent (&writer
);
10407 mono_json_writer_object_key(&writer
, "got");
10408 mono_json_writer_array_begin (&writer
);
10410 mono_json_writer_indent_push (&writer
);
10411 for (i
= 0; i
< acfg
->got_info
.got_patches
->len
; ++i
) {
10412 MonoJumpInfo
*ji
= g_ptr_array_index (acfg
->got_info
.got_patches
, i
);
10414 mono_json_writer_indent (&writer
);
10415 mono_json_writer_printf (&writer
, "{ ");
10416 mono_json_writer_object_key(&writer
, "patch_name");
10417 mono_json_writer_printf (&writer
, "\"%s\" },\n", get_patch_name (ji
->type
));
10420 mono_json_writer_indent_pop (&writer
);
10421 mono_json_writer_indent (&writer
);
10422 mono_json_writer_array_end (&writer
);
10423 mono_json_writer_printf (&writer
, ",\n");
10426 mono_json_writer_indent_pop (&writer
);
10427 mono_json_writer_indent (&writer
);
10428 mono_json_writer_object_end (&writer
);
10430 dumpname
= g_strdup_printf ("%s.json", g_path_get_basename (acfg
->image
->name
));
10431 dumpfile
= fopen (dumpname
, "w+");
10434 fprintf (dumpfile
, "%s", writer
.text
->str
);
10437 mono_json_writer_destroy (&writer
);
10440 static const char *preinited_jit_icalls
[] = {
10441 "mono_aot_init_llvm_method",
10442 "mono_aot_init_gshared_method_this",
10443 "mono_aot_init_gshared_method_mrgctx",
10444 "mono_aot_init_gshared_method_vtable",
10445 "mono_llvm_throw_corlib_exception",
10446 "mono_init_vtable_slot",
10447 "mono_helper_ldstr_mscorlib"
10451 add_preinit_got_slots (MonoAotCompile
*acfg
)
10457 * Allocate the first few GOT entries to information which is needed frequently, or it is needed
10458 * during method initialization etc.
10461 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
10462 ji
->type
= MONO_PATCH_INFO_IMAGE
;
10463 ji
->data
.image
= acfg
->image
;
10464 get_got_offset (acfg
, FALSE
, ji
);
10465 get_got_offset (acfg
, TRUE
, ji
);
10467 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
10468 ji
->type
= MONO_PATCH_INFO_MSCORLIB_GOT_ADDR
;
10469 get_got_offset (acfg
, FALSE
, ji
);
10470 get_got_offset (acfg
, TRUE
, ji
);
10472 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
10473 ji
->type
= MONO_PATCH_INFO_GC_CARD_TABLE_ADDR
;
10474 get_got_offset (acfg
, FALSE
, ji
);
10475 get_got_offset (acfg
, TRUE
, ji
);
10477 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
10478 ji
->type
= MONO_PATCH_INFO_GC_NURSERY_START
;
10479 get_got_offset (acfg
, FALSE
, ji
);
10480 get_got_offset (acfg
, TRUE
, ji
);
10482 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
10483 ji
->type
= MONO_PATCH_INFO_JIT_TLS_ID
;
10484 get_got_offset (acfg
, FALSE
, ji
);
10485 get_got_offset (acfg
, TRUE
, ji
);
10487 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
10488 ji
->type
= MONO_PATCH_INFO_AOT_MODULE
;
10489 get_got_offset (acfg
, FALSE
, ji
);
10490 get_got_offset (acfg
, TRUE
, ji
);
10492 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
10493 ji
->type
= MONO_PATCH_INFO_GC_NURSERY_BITS
;
10494 get_got_offset (acfg
, FALSE
, ji
);
10495 get_got_offset (acfg
, TRUE
, ji
);
10497 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
10498 ji
->type
= MONO_PATCH_INFO_GET_TLS_TRAMP
;
10499 get_got_offset (acfg
, FALSE
, ji
);
10500 get_got_offset (acfg
, TRUE
, ji
);
10502 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoJumpInfo
));
10503 ji
->type
= MONO_PATCH_INFO_JIT_THREAD_ATTACH
;
10504 get_got_offset (acfg
, FALSE
, ji
);
10505 get_got_offset (acfg
, TRUE
, ji
);
10507 for (i
= 0; i
< sizeof (preinited_jit_icalls
) / sizeof (char*); ++i
) {
10508 ji
= (MonoJumpInfo
*)mono_mempool_alloc0 (acfg
->mempool
, sizeof (MonoAotCompile
));
10509 ji
->type
= MONO_PATCH_INFO_INTERNAL_METHOD
;
10510 ji
->data
.name
= preinited_jit_icalls
[i
];
10511 get_got_offset (acfg
, FALSE
, ji
);
10512 get_got_offset (acfg
, TRUE
, ji
);
10515 acfg
->nshared_got_entries
= acfg
->got_offset
;
10519 mono_compile_assembly (MonoAssembly
*ass
, guint32 opts
, const char *aot_options
)
10521 MonoImage
*image
= ass
->image
;
10524 MonoAotCompile
*acfg
;
10525 char *outfile_name
, *tmp_outfile_name
, *p
;
10526 char llvm_stats_msg
[256];
10530 acfg
= acfg_create (ass
, opts
);
10532 memset (&acfg
->aot_opts
, 0, sizeof (acfg
->aot_opts
));
10533 acfg
->aot_opts
.write_symbols
= TRUE
;
10534 acfg
->aot_opts
.ntrampolines
= 4096;
10535 acfg
->aot_opts
.nrgctx_trampolines
= 4096;
10536 acfg
->aot_opts
.nimt_trampolines
= 512;
10537 acfg
->aot_opts
.nrgctx_fetch_trampolines
= 128;
10538 acfg
->aot_opts
.ngsharedvt_arg_trampolines
= 512;
10539 acfg
->aot_opts
.llvm_path
= g_strdup ("");
10540 acfg
->aot_opts
.temp_path
= g_strdup ("");
10542 acfg
->aot_opts
.use_trampolines_page
= TRUE
;
10545 mono_aot_parse_options (aot_options
, &acfg
->aot_opts
);
10547 if (acfg
->aot_opts
.logfile
) {
10548 acfg
->logfile
= fopen (acfg
->aot_opts
.logfile
, "a+");
10551 if (acfg
->aot_opts
.data_outfile
) {
10552 acfg
->data_outfile
= fopen (acfg
->aot_opts
.data_outfile
, "w+");
10553 if (!acfg
->data_outfile
) {
10554 aot_printerrf (acfg
, "Unable to create file '%s': %s\n", acfg
->aot_opts
.data_outfile
, strerror (errno
));
10557 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_SEPARATE_DATA
);
10560 //acfg->aot_opts.print_skipped_methods = TRUE;
10562 #if !defined(MONO_ARCH_GSHAREDVT_SUPPORTED)
10563 if (acfg
->opts
& MONO_OPT_GSHAREDVT
) {
10564 aot_printerrf (acfg
, "-O=gsharedvt not supported on this platform.\n");
10567 if (acfg
->aot_opts
.llvm_only
) {
10568 aot_printerrf (acfg
, "--aot=llvmonly requires a runtime that supports gsharedvt.\n");
10572 if (acfg
->aot_opts
.llvm_only
|| mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))
10573 acfg
->opts
|= MONO_OPT_GSHAREDVT
;
10576 #if !defined(ENABLE_LLVM)
10577 if (acfg
->aot_opts
.llvm_only
) {
10578 aot_printerrf (acfg
, "--aot=llvmonly requires a runtime compiled with llvm support.\n");
10583 if (acfg
->opts
& MONO_OPT_GSHAREDVT
)
10584 mono_set_generic_sharing_vt_supported (TRUE
);
10586 aot_printf (acfg
, "Mono Ahead of Time compiler - compiling assembly %s\n", image
->name
);
10588 generate_aotid ((guint8
*) &acfg
->image
->aotid
);
10590 char *aotid
= mono_guid_to_string (acfg
->image
->aotid
);
10591 aot_printf (acfg
, "AOTID %s\n", aotid
);
10594 #ifndef MONO_ARCH_HAVE_FULL_AOT_TRAMPOLINES
10595 if (mono_aot_mode_is_full (&acfg
->aot_opts
)) {
10596 aot_printerrf (acfg
, "--aot=full is not supported on this platform.\n");
10601 if (acfg
->aot_opts
.direct_pinvoke
&& !acfg
->aot_opts
.static_link
) {
10602 aot_printerrf (acfg
, "The 'direct-pinvoke' AOT option also requires the 'static' AOT option.\n");
10606 if (acfg
->aot_opts
.static_link
)
10607 acfg
->aot_opts
.asm_writer
= TRUE
;
10609 if (acfg
->aot_opts
.soft_debug
) {
10610 MonoDebugOptions
*opt
= mini_get_debug_options ();
10612 opt
->mdb_optimizations
= TRUE
;
10613 opt
->gen_sdb_seq_points
= TRUE
;
10615 if (!mono_debug_enabled ()) {
10616 aot_printerrf (acfg
, "The soft-debug AOT option requires the --debug option.\n");
10619 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_DEBUG
);
10622 if (mono_use_llvm
|| acfg
->aot_opts
.llvm
) {
10624 acfg
->aot_opts
.asm_writer
= TRUE
;
10625 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_WITH_LLVM
);
10627 if (acfg
->aot_opts
.soft_debug
) {
10628 aot_printerrf (acfg
, "The 'soft-debug' option is not supported when compiling with LLVM.\n");
10634 if (acfg
->aot_opts
.asm_only
&& !acfg
->aot_opts
.llvm_outfile
) {
10635 aot_printerrf (acfg
, "Compiling with LLVM and the asm-only option requires the llvm-outfile= option.\n");
10640 if (mono_aot_mode_is_full (&acfg
->aot_opts
))
10641 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_FULL_AOT
);
10643 if (mono_threads_is_coop_enabled ())
10644 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_SAFEPOINTS
);
10646 if (acfg
->aot_opts
.instances_logfile_path
) {
10647 acfg
->instances_logfile
= fopen (acfg
->aot_opts
.instances_logfile_path
, "w");
10648 if (!acfg
->instances_logfile
) {
10649 aot_printerrf (acfg
, "Unable to create logfile: '%s'.\n", acfg
->aot_opts
.instances_logfile_path
);
10654 load_profile_files (acfg
);
10656 acfg
->num_trampolines
[MONO_AOT_TRAMP_SPECIFIC
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.ntrampolines
: 0;
10657 #ifdef MONO_ARCH_GSHARED_SUPPORTED
10658 acfg
->num_trampolines
[MONO_AOT_TRAMP_STATIC_RGCTX
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.nrgctx_trampolines
: 0;
10660 acfg
->num_trampolines
[MONO_AOT_TRAMP_IMT
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.nimt_trampolines
: 0;
10661 #ifdef MONO_ARCH_GSHAREDVT_SUPPORTED
10662 if (acfg
->opts
& MONO_OPT_GSHAREDVT
)
10663 acfg
->num_trampolines
[MONO_AOT_TRAMP_GSHAREDVT_ARG
] = mono_aot_mode_is_full (&acfg
->aot_opts
) ? acfg
->aot_opts
.ngsharedvt_arg_trampolines
: 0;
10666 acfg
->temp_prefix
= mono_img_writer_get_temp_label_prefix (NULL
);
10670 if (mono_use_llvm
|| acfg
->aot_opts
.llvm
) {
10673 * Emit all LLVM code into a separate assembly/object file and link with it
10676 if (!acfg
->aot_opts
.asm_only
&& acfg
->llvm_owriter_supported
) {
10677 acfg
->llvm_owriter
= TRUE
;
10678 } else if (acfg
->aot_opts
.llvm_outfile
) {
10679 int len
= strlen (acfg
->aot_opts
.llvm_outfile
);
10681 if (len
>= 2 && acfg
->aot_opts
.llvm_outfile
[len
- 2] == '.' && acfg
->aot_opts
.llvm_outfile
[len
- 1] == 'o')
10682 acfg
->llvm_owriter
= TRUE
;
10686 if (acfg
->llvm
&& acfg
->thumb_mixed
)
10687 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_LLVM_THUMB
);
10688 if (acfg
->aot_opts
.llvm_only
)
10689 acfg
->flags
= (MonoAotFileFlags
)(acfg
->flags
| MONO_AOT_FILE_FLAG_LLVM_ONLY
);
10691 acfg
->assembly_name_sym
= g_strdup (acfg
->image
->assembly
->aname
.name
);
10692 /* Get rid of characters which cannot occur in symbols */
10693 for (p
= acfg
->assembly_name_sym
; *p
; ++p
) {
10694 if (!(isalnum (*p
) || *p
== '_'))
10698 acfg
->global_prefix
= g_strdup_printf ("mono_aot_%s", acfg
->assembly_name_sym
);
10699 acfg
->plt_symbol
= g_strdup_printf ("%s_plt", acfg
->global_prefix
);
10700 acfg
->got_symbol
= g_strdup_printf ("%s_got", acfg
->global_prefix
);
10702 acfg
->llvm_got_symbol
= g_strdup_printf ("%s_llvm_got", acfg
->global_prefix
);
10703 acfg
->llvm_eh_frame_symbol
= g_strdup_printf ("%s_eh_frame", acfg
->global_prefix
);
10706 acfg
->method_index
= 1;
10708 if (mono_aot_mode_is_full (&acfg
->aot_opts
) || mono_aot_mode_is_hybrid (&acfg
->aot_opts
))
10709 mono_set_partial_sharing_supported (TRUE
);
10711 res
= collect_methods (acfg
);
10715 acfg
->cfgs_size
= acfg
->methods
->len
+ 32;
10716 acfg
->cfgs
= g_new0 (MonoCompile
*, acfg
->cfgs_size
);
10718 /* PLT offset 0 is reserved for the PLT trampoline */
10719 acfg
->plt_offset
= 1;
10720 add_preinit_got_slots (acfg
);
10725 mono_llvm_create_aot_module (acfg
->image
->assembly
, acfg
->global_prefix
, TRUE
, acfg
->aot_opts
.static_link
, acfg
->aot_opts
.llvm_only
);
10731 compile_methods (acfg
);
10735 acfg
->stats
.jit_time
= TV_ELAPSED (atv
, btv
);
10741 if (acfg
->aot_opts
.asm_only
) {
10742 if (acfg
->aot_opts
.outfile
) {
10743 acfg
->tmpfname
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
10744 acfg
->tmpbasename
= g_strdup (acfg
->tmpfname
);
10746 acfg
->tmpbasename
= g_strdup_printf ("%s", acfg
->image
->name
);
10747 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->tmpbasename
);
10749 g_assert (acfg
->aot_opts
.llvm_outfile
);
10750 acfg
->llvm_sfile
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
10751 if (acfg
->llvm_owriter
)
10752 acfg
->llvm_ofile
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
10754 acfg
->llvm_sfile
= g_strdup (acfg
->aot_opts
.llvm_outfile
);
10756 acfg
->tmpbasename
= (strcmp (acfg
->aot_opts
.temp_path
, "") == 0) ?
10757 g_strdup_printf ("%s", "temp") :
10758 g_build_filename (acfg
->aot_opts
.temp_path
, "temp", NULL
);
10760 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->tmpbasename
);
10761 acfg
->llvm_sfile
= g_strdup_printf ("%s-llvm.s", acfg
->tmpbasename
);
10762 acfg
->llvm_ofile
= g_strdup_printf ("%s-llvm.o", acfg
->tmpbasename
);
10767 if (acfg
->aot_opts
.asm_only
&& !acfg
->aot_opts
.llvm_only
) {
10768 if (acfg
->aot_opts
.outfile
)
10769 acfg
->tmpfname
= g_strdup_printf ("%s", acfg
->aot_opts
.outfile
);
10771 acfg
->tmpfname
= g_strdup_printf ("%s.s", acfg
->image
->name
);
10772 acfg
->fp
= fopen (acfg
->tmpfname
, "w+");
10774 int i
= g_file_open_tmp ("mono_aot_XXXXXX", &acfg
->tmpfname
, NULL
);
10775 acfg
->fp
= fdopen (i
, "w+");
10777 if (acfg
->fp
== 0 && !acfg
->aot_opts
.llvm_only
) {
10778 aot_printerrf (acfg
, "Unable to open file '%s': %s\n", acfg
->tmpfname
, strerror (errno
));
10782 acfg
->w
= mono_img_writer_create (acfg
->fp
, FALSE
);
10784 tmp_outfile_name
= NULL
;
10785 outfile_name
= NULL
;
10787 /* Compute symbols for methods */
10788 for (i
= 0; i
< acfg
->nmethods
; ++i
) {
10789 if (acfg
->cfgs
[i
]) {
10790 MonoCompile
*cfg
= acfg
->cfgs
[i
];
10791 int method_index
= get_method_index (acfg
, cfg
->orig_method
);
10793 if (COMPILE_LLVM (cfg
))
10794 cfg
->asm_symbol
= g_strdup_printf ("%s%s", acfg
->llvm_label_prefix
, cfg
->llvm_method_name
);
10795 else if (acfg
->global_symbols
|| acfg
->llvm
)
10796 cfg
->asm_symbol
= get_debug_sym (cfg
->orig_method
, "", acfg
->method_label_hash
);
10798 cfg
->asm_symbol
= g_strdup_printf ("%s%sm_%x", acfg
->temp_prefix
, acfg
->llvm_label_prefix
, method_index
);
10799 cfg
->asm_debug_symbol
= cfg
->asm_symbol
;
10803 if (acfg
->aot_opts
.dwarf_debug
&& acfg
->aot_opts
.gnu_asm
) {
10805 * CLANG supports GAS .file/.loc directives, so emit line number information this way
10807 acfg
->gas_line_numbers
= TRUE
;
10810 #ifdef EMIT_DWARF_INFO
10811 if ((!acfg
->aot_opts
.nodebug
|| acfg
->aot_opts
.dwarf_debug
) && acfg
->has_jitted_code
) {
10812 if (acfg
->aot_opts
.dwarf_debug
&& !mono_debug_enabled ()) {
10813 aot_printerrf (acfg
, "The dwarf AOT option requires the --debug option.\n");
10816 acfg
->dwarf
= mono_dwarf_writer_create (acfg
->w
, NULL
, 0, !acfg
->gas_line_numbers
);
10818 #endif /* EMIT_DWARF_INFO */
10821 mono_img_writer_emit_start (acfg
->w
);
10824 mono_dwarf_writer_emit_base_info (acfg
->dwarf
, g_path_get_basename (acfg
->image
->name
), mono_unwind_get_cie_program ());
10830 emit_extra_methods (acfg
);
10832 emit_trampolines (acfg
);
10834 emit_class_name_table (acfg
);
10836 emit_got_info (acfg
, FALSE
);
10838 emit_got_info (acfg
, TRUE
);
10840 emit_exception_info (acfg
);
10842 emit_unwind_info (acfg
);
10844 emit_class_info (acfg
);
10848 emit_image_table (acfg
);
10854 * The managed allocators are GC specific, so can't use an AOT image created by one GC
10857 const char *gc_name
= mono_gc_get_gc_name ();
10858 acfg
->gc_name_offset
= add_to_blob (acfg
, (guint8
*)gc_name
, strlen (gc_name
) + 1);
10863 emit_objc_selectors (acfg
);
10865 emit_globals (acfg
);
10867 emit_file_info (acfg
);
10869 emit_library_info (acfg
);
10872 emit_dwarf_info (acfg
);
10873 mono_dwarf_writer_close (acfg
->dwarf
);
10876 emit_mem_end (acfg
);
10878 if (acfg
->need_pt_gnu_stack
) {
10879 /* This is required so the .so doesn't have an executable stack */
10880 /* The bin writer already emits this */
10881 fprintf (acfg
->fp
, "\n.section .note.GNU-stack,\"\",@progbits\n");
10884 if (acfg
->aot_opts
.data_outfile
)
10885 fclose (acfg
->data_outfile
);
10891 res
= emit_llvm_file (acfg
);
10899 acfg
->stats
.gen_time
= TV_ELAPSED (atv
, btv
);
10902 sprintf (llvm_stats_msg
, ", LLVM: %d (%d%%)", acfg
->stats
.llvm_count
, acfg
->stats
.mcount
? (acfg
->stats
.llvm_count
* 100) / acfg
->stats
.mcount
: 100);
10904 strcpy (llvm_stats_msg
, "");
10906 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
;
10908 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",
10909 (int)acfg
->stats
.code_size
, (int)(acfg
->stats
.code_size
* 100 / all_sizes
),
10910 (int)acfg
->stats
.info_size
, (int)(acfg
->stats
.info_size
* 100 / all_sizes
),
10911 (int)acfg
->stats
.ex_info_size
, (int)(acfg
->stats
.ex_info_size
* 100 / all_sizes
),
10912 (int)acfg
->stats
.unwind_info_size
, (int)(acfg
->stats
.unwind_info_size
* 100 / all_sizes
),
10913 (int)acfg
->stats
.class_info_size
, (int)(acfg
->stats
.class_info_size
* 100 / all_sizes
),
10914 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,
10915 (int)acfg
->stats
.got_info_size
, (int)(acfg
->stats
.got_info_size
* 100 / all_sizes
),
10916 (int)acfg
->stats
.offsets_size
, (int)(acfg
->stats
.offsets_size
* 100 / all_sizes
),
10917 (int)(acfg
->got_offset
* sizeof (gpointer
)));
10918 aot_printf (acfg
, "Compiled: %d/%d (%d%%)%s, No GOT slots: %d (%d%%), Direct calls: %d (%d%%)\n",
10919 acfg
->stats
.ccount
, acfg
->stats
.mcount
, acfg
->stats
.mcount
? (acfg
->stats
.ccount
* 100) / acfg
->stats
.mcount
: 100,
10921 acfg
->stats
.methods_without_got_slots
, acfg
->stats
.mcount
? (acfg
->stats
.methods_without_got_slots
* 100) / acfg
->stats
.mcount
: 100,
10922 acfg
->stats
.direct_calls
, acfg
->stats
.all_calls
? (acfg
->stats
.direct_calls
* 100) / acfg
->stats
.all_calls
: 100);
10923 if (acfg
->stats
.genericcount
)
10924 aot_printf (acfg
, "%d methods are generic (%d%%)\n", acfg
->stats
.genericcount
, acfg
->stats
.mcount
? (acfg
->stats
.genericcount
* 100) / acfg
->stats
.mcount
: 100);
10925 if (acfg
->stats
.abscount
)
10926 aot_printf (acfg
, "%d methods contain absolute addresses (%d%%)\n", acfg
->stats
.abscount
, acfg
->stats
.mcount
? (acfg
->stats
.abscount
* 100) / acfg
->stats
.mcount
: 100);
10927 if (acfg
->stats
.lmfcount
)
10928 aot_printf (acfg
, "%d methods contain lmf pointers (%d%%)\n", acfg
->stats
.lmfcount
, acfg
->stats
.mcount
? (acfg
->stats
.lmfcount
* 100) / acfg
->stats
.mcount
: 100);
10929 if (acfg
->stats
.ocount
)
10930 aot_printf (acfg
, "%d methods have other problems (%d%%)\n", acfg
->stats
.ocount
, acfg
->stats
.mcount
? (acfg
->stats
.ocount
* 100) / acfg
->stats
.mcount
: 100);
10934 res
= mono_img_writer_emit_writeout (acfg
->w
);
10939 res
= compile_asm (acfg
);
10946 acfg
->stats
.link_time
= TV_ELAPSED (atv
, btv
);
10948 if (acfg
->aot_opts
.stats
) {
10951 aot_printf (acfg
, "GOT slot distribution:\n");
10952 for (i
= 0; i
< MONO_PATCH_INFO_NUM
; ++i
)
10953 if (acfg
->stats
.got_slot_types
[i
])
10954 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
]);
10955 aot_printf (acfg
, "\nMethod stats:\n");
10956 aot_printf (acfg
, "\tNormal: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_NORMAL
]);
10957 aot_printf (acfg
, "\tInstance: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_INST
]);
10958 aot_printf (acfg
, "\tGSharedvt: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_GSHAREDVT
]);
10959 aot_printf (acfg
, "\tWrapper: %d\n", acfg
->stats
.method_categories
[METHOD_CAT_WRAPPER
]);
10962 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);
10964 if (acfg
->aot_opts
.dump_json
)
10977 mono_aot_readonly_field_override (MonoClassField
*field
)
10983 mono_compile_assembly (MonoAssembly
*ass
, guint32 opts
, const char *aot_options
)
10989 mono_aot_is_shared_got_offset (int offset
)