/*
- * aot.c: mono Ahead of Time compiler
+ * aot-runtime.c: mono Ahead of Time compiler
*
* Author:
* Dietmar Maurer (dietmar@ximian.com)
#endif
#include <fcntl.h>
#include <string.h>
-#ifndef PLATFORM_WIN32
+#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
-#else
+#endif
+
+#if PLATFORM_WIN32
#include <winsock2.h>
#include <windows.h>
#endif
#include <mono/metadata/marshal.h>
#include <mono/metadata/gc-internal.h>
#include <mono/metadata/monitor.h>
+#include <mono/metadata/threads-types.h>
#include <mono/utils/mono-logger.h>
#include "mono/utils/mono-compiler.h"
GHashTable *extra_methods;
/* Maps methods to their code */
GHashTable *method_to_code;
+ /* Maps pointers into the method info to the methods themselves */
+ GHashTable *method_ref_to_method;
MonoAssemblyName *image_names;
char **image_guids;
MonoAssembly *assembly;
MonoImage **image_table;
guint32 image_table_len;
- guint32 plt_first_got_ofset;
gboolean out_of_date;
gboolean plt_inited;
guint8 *mem_begin;
guint8 *code_end;
guint8 *plt;
guint8 *plt_end;
- guint32 plt_first_got_offset;
+ guint32 plt_got_offset_base;
guint32 *code_offsets;
guint8 *method_info;
guint32 *method_info_offsets;
guint32 *extra_method_info_offsets;
guint8 *extra_method_info;
guint8 *trampolines;
- guint32 num_trampolines, first_trampoline_got_offset, trampoline_index;
+ guint32 num_trampolines, trampoline_got_offset_base, trampoline_index;
+ guint32 specific_trampoline_size;
gpointer *globals;
MonoDl *sofile;
} MonoAotModule;
+/* This structure is stored in the AOT file */
+typedef struct MonoAotFileInfo
+{
+ guint32 plt_got_offset_base;
+ guint32 trampoline_got_offset_base;
+ guint32 num_trampolines;
+ guint32 got_size;
+ guint32 plt_size;
+ guint32 specific_trampoline_size;
+ gpointer *got;
+} MonoAotFileInfo;
+
static GHashTable *aot_modules;
#define mono_aot_lock() EnterCriticalSection (&aot_mutex)
#define mono_aot_unlock() LeaveCriticalSection (&aot_mutex)
mono_metadata_free_type (type);
} else if ((type == MONO_TYPE_VAR) || (type == MONO_TYPE_MVAR)) {
MonoType *t;
- gboolean is_method;
MonoGenericContainer *container;
- // FIXME: Maybe use types directly to avoid
- // the overhead of creating MonoClass-es
-
- // FIXME: Memory management
- t = g_new0 (MonoType, 1);
- t->type = type;
- t->data.generic_param = g_new0 (MonoGenericParam, 1);
- t->data.generic_param->num = decode_value (p, &p);
- t->data.generic_param->name = "T";
+ int num = decode_value (p, &p);
+ gboolean is_method = decode_value (p, &p);
- is_method = decode_value (p, &p);
if (is_method) {
- MonoMethod *method_def = decode_method_ref_2 (module, p, &p);
-
- if (!method_def) {
- g_free (t->data.generic_param);
- g_free (t);
+ MonoMethod *method_def;
+ g_assert (type == MONO_TYPE_MVAR);
+ method_def = decode_method_ref_2 (module, p, &p);
+ if (!method_def)
return NULL;
- }
container = mono_method_get_generic_container (method_def);
} else {
- MonoClass *class_def = decode_klass_ref (module, p, &p);
-
- if (!class_def) {
- g_free (t->data.generic_param);
- g_free (t);
+ MonoClass *class_def;
+ g_assert (type == MONO_TYPE_VAR);
+ class_def = decode_klass_ref (module, p, &p);
+ if (!class_def)
return NULL;
- }
container = class_def->generic_container;
}
g_assert (container);
- t->data.generic_param->owner = container;
+ // FIXME: Memory management
+ t = g_new0 (MonoType, 1);
+ t->type = type;
+ t->data.generic_param = mono_generic_container_get_param (container, num);
+
+ // FIXME: Maybe use types directly to avoid
+ // the overhead of creating MonoClass-es
klass = mono_class_from_mono_type (t);
+
+ g_free (t);
} else {
g_assert_not_reached ();
}
return mono_class_get_field (klass, token);
}
+/*
+ * can_method_ref_match_method:
+ *
+ * Determine if calling decode_method_ref_2 on P could return the same method as
+ * METHOD. This is an optimization to avoid calling decode_method_ref_2 () which
+ * would create MonoMethods which are not needed etc.
+ */
+static gboolean
+can_method_ref_match_method (MonoAotModule *module, guint8 *buf, MonoMethod *method)
+{
+ guint8 *p = buf;
+ guint32 image_index, value;
+
+ /* Keep this in sync with decode_method_ref () */
+ value = decode_value (p, &p);
+ image_index = value >> 24;
+
+ if (image_index == MONO_AOT_METHODREF_WRAPPER) {
+ guint32 wrapper_type;
+
+ if (!method->wrapper_type)
+ return FALSE;
+
+ wrapper_type = decode_value (p, &p);
+
+ if (method->wrapper_type != wrapper_type)
+ return FALSE;
+ } else if (image_index < MONO_AOT_METHODREF_MIN || image_index == MONO_AOT_METHODREF_METHODSPEC || image_index == MONO_AOT_METHODREF_GINST) {
+ if (method->wrapper_type)
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
/*
* decode_method_ref:
*
decode_method_ref (MonoAotModule *module, guint32 *token, MonoMethod **method, gboolean *no_aot_trampoline, guint8 *buf, guint8 **endbuf)
{
guint32 image_index, value;
- MonoImage *image;
+ MonoImage *image = NULL;
guint8 *p = buf;
if (method)
value = decode_value (p, &p);
image_index = value >> 24;
- if (image_index == 252) {
+ if (image_index == MONO_AOT_METHODREF_NO_AOT_TRAMPOLINE) {
if (no_aot_trampoline)
*no_aot_trampoline = TRUE;
value = decode_value (p, &p);
image_index = value >> 24;
}
- if (image_index == 253) {
- /* Wrapper */
+ if (image_index == MONO_AOT_METHODREF_WRAPPER) {
guint32 wrapper_type;
wrapper_type = decode_value (p, &p);
default:
g_assert_not_reached ();
}
- } else if (image_index == 255) {
- /* Methodspec */
+ } else if (image_index == MONO_AOT_METHODREF_WRAPPER_NAME) {
+ /* Can't decode these */
+ g_assert_not_reached ();
+ } else if (image_index == MONO_AOT_METHODREF_METHODSPEC) {
image_index = decode_value (p, &p);
*token = decode_value (p, &p);
image = load_image (module, image_index);
if (!image)
return NULL;
- } else if (image_index == 254) {
- /* Method on generic instance */
+ } else if (image_index == MONO_AOT_METHODREF_GINST) {
MonoClass *klass;
MonoGenericContext ctx;
return NULL;
*method = mono_class_inflate_generic_method_full (*method, klass, &ctx);
+ } else if (image_index == MONO_AOT_METHODREF_ARRAY) {
+ MonoClass *klass;
+ int method_type;
+
+ klass = decode_klass_ref (module, p, &p);
+ if (!klass)
+ return NULL;
+ method_type = decode_value (p, &p);
+ *token = 0;
+ switch (method_type) {
+ case 0:
+ *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank);
+ break;
+ case 1:
+ *method = mono_class_get_method_from_name (klass, ".ctor", klass->rank * 2);
+ break;
+ case 2:
+ *method = mono_class_get_method_from_name (klass, "Get", -1);
+ break;
+ case 3:
+ *method = mono_class_get_method_from_name (klass, "Address", -1);
+ break;
+ case 4:
+ *method = mono_class_get_method_from_name (klass, "Set", -1);
+ break;
+ default:
+ g_assert_not_reached ();
+ }
} else {
+ g_assert (image_index < MONO_AOT_METHODREF_MIN);
*token = MONO_TOKEN_METHOD_DEF | (value & 0xffffff);
image = load_image (module, image_index);
char *opt_flags = NULL;
gpointer *globals;
gboolean full_aot = FALSE;
- guint32 *plt_first_got_offset = NULL;
- guint32 *trampolines_info = NULL;
- gpointer *got_addr = NULL;
- gpointer *got = NULL;
- guint32 *got_size_ptr = NULL;
- guint32 *plt_size = NULL;
+ MonoAotFileInfo *file_info = NULL;
int i;
if (mono_compile_aot)
if (assembly->image->dynamic)
return;
+ if (mono_security_get_mode () == MONO_SECURITY_MODE_CAS)
+ return;
+
mono_aot_lock ();
if (static_aot_modules)
globals = g_hash_table_lookup (static_aot_modules, assembly->aname.name);
return;
}
- find_symbol (sofile, globals, "got_addr", (gpointer *)&got_addr);
- g_assert (got_addr);
- got = (gpointer*)*got_addr;
- g_assert (got);
- find_symbol (sofile, globals, "got_size", (gpointer *)&got_size_ptr);
- g_assert (got_size_ptr);
+ find_symbol (sofile, globals, "mono_aot_file_info", (gpointer*)&file_info);
+ g_assert (file_info);
amodule = g_new0 (MonoAotModule, 1);
amodule->aot_name = aot_name;
amodule->assembly = assembly;
- amodule->got = got;
- amodule->got_size = *got_size_ptr;
+ amodule->plt_got_offset_base = file_info->plt_got_offset_base;
+ amodule->num_trampolines = file_info->num_trampolines;
+ amodule->trampoline_got_offset_base = file_info->trampoline_got_offset_base;
+ amodule->got_size = file_info->got_size;
+ amodule->plt_size = file_info->plt_size;
+ amodule->specific_trampoline_size = file_info->specific_trampoline_size;
+ amodule->got = file_info->got;
amodule->got [0] = assembly->image;
amodule->globals = globals;
amodule->sofile = sofile;
find_symbol (sofile, globals, "plt", (gpointer*)&amodule->plt);
find_symbol (sofile, globals, "plt_end", (gpointer*)&amodule->plt_end);
- find_symbol (sofile, globals, "plt_size", (gpointer *)&plt_size);
- g_assert (plt_size);
- amodule->plt_size = *plt_size;
-
- find_symbol (sofile, globals, "plt_first_got_offset", (gpointer *)&plt_first_got_offset);
- g_assert (plt_first_got_offset);
- amodule->plt_first_got_offset = *plt_first_got_offset;
-
- find_symbol (sofile, globals, "trampolines_info", (gpointer *)&trampolines_info);
- if (trampolines_info) {
- amodule->num_trampolines = trampolines_info [0];
- amodule->first_trampoline_got_offset = trampolines_info [1];
- }
-
if (make_unreadable) {
#ifndef PLATFORM_WIN32
guint8 *addr;
return TRUE;
}
+/*
+ * LOCKING: Acquires the domain lock.
+ */
static MonoJitInfo*
decode_exception_debug_info (MonoAotModule *aot_module, MonoDomain *domain,
MonoMethod *method, guint8* ex_info, guint8 *code)
{
int i, buf_len;
MonoJitInfo *jinfo;
- guint code_len, used_int_regs;
- gboolean has_generic_jit_info;
- guint8 *p;
+ guint code_len, used_int_regs, flags;
+ gboolean has_generic_jit_info, has_dwarf_unwind_info;
+ guint8 *p, *unwind_info_block;
MonoMethodHeader *header;
- int generic_info_size;
+ int generic_info_size, unwind_info_len;
header = mono_method_get_header (method);
p = ex_info;
code_len = decode_value (p, &p);
- used_int_regs = decode_value (p, &p);
- has_generic_jit_info = decode_value (p, &p);
+ flags = decode_value (p, &p);
+ has_generic_jit_info = (flags & 1) != 0;
+ has_dwarf_unwind_info = (flags & 2) != 0;
+ unwind_info_block = p;
+ if (has_dwarf_unwind_info) {
+ gssize offset;
+
+ unwind_info_len = decode_value (p, &p);
+ offset = unwind_info_block - (guint8*)aot_module->ex_info;
+ g_assert (offset > 0 && offset < (1 << 30));
+ used_int_regs = offset;
+ p += unwind_info_len;
+ } else {
+ used_int_regs = decode_value (p, &p);
+ }
if (has_generic_jit_info)
generic_info_size = sizeof (MonoGenericJitInfo);
else
ei->handler_start = code + decode_value (p, &p);
}
}
- else
+ else {
jinfo = mono_domain_alloc0 (domain, sizeof (MonoJitInfo) + generic_info_size);
+ }
jinfo->code_size = code_len;
jinfo->used_regs = used_int_regs;
jinfo->method = method;
jinfo->code_start = code;
jinfo->domain_neutral = 0;
+ jinfo->from_aot = 1;
if (has_generic_jit_info) {
MonoGenericJitInfo *gi;
return jinfo;
}
+/*
+ * mono_aot_get_unwind_info:
+ *
+ * Return a pointer to the DWARF unwind info belonging to JI.
+ */
+guint8*
+mono_aot_get_unwind_info (MonoJitInfo *ji, guint32 *unwind_info_len)
+{
+ MonoAotModule *amodule = ji->method->klass->image->aot_module;
+ guint8 *p;
+
+ g_assert (amodule);
+ g_assert (ji->from_aot);
+
+ p = amodule->ex_info + ji->used_regs;
+ *unwind_info_len = decode_value (p, &p);
+ return p;
+}
+
MonoJitInfo *
mono_aot_find_jit_info (MonoDomain *domain, MonoImage *image, gpointer addr)
{
case MONO_PATCH_INFO_TYPE_FROM_HANDLE:
case MONO_PATCH_INFO_RVA:
case MONO_PATCH_INFO_METHODCONST:
+ case MONO_PATCH_INFO_IMAGE:
return TRUE;
default:
return FALSE;
return FALSE;
}
+/*
+ * decode_got_entry:
+ *
+ * Decode a reference to a GOT entry. GOT_OFFSET is set to the index of the got
+ * entry. If that got entry is not already filled out, then JI is filled out with
+ * the information required to resolve the value of the GOT entry.
+ * FIXME: Clean up this confusing API.
+ */
static gboolean
decode_got_entry (MonoAotModule *aot_module, MonoMemPool *mp, MonoJumpInfo *ji, guint8 *buf, guint8 **endbuf, guint32 *got_offset)
{
jinfo = decode_exception_debug_info (aot_module, domain, method, ex_info, code);
}
- mono_domain_lock (domain);
- code2 = mono_code_manager_reserve (domain->code_mp, jinfo->code_size);
- mono_domain_unlock (domain);
+ code2 = mono_domain_code_reserve (domain, jinfo->code_size);
memcpy (code2, code, jinfo->code_size);
mono_arch_flush_icache (code2, jinfo->code_size);
code = code2;
mono_mempool_destroy (mp);
}
- mono_aot_lock ();
-
- mono_jit_stats.methods_aot++;
-
if (mono_trace_is_traced (G_LOG_LEVEL_DEBUG, MONO_TRACE_AOT)) {
char *full_name;
g_free (full_name);
}
+ mono_aot_lock ();
+
+ mono_jit_stats.methods_aot++;
+
aot_module->methods_loaded [method_index / 32] |= 1 << (method_index % 32);
init_plt (aot_module);
guint32 table_size, entry_size, hash;
guint32 *table, *entry;
char *full_name = NULL;
+ int num_checks = 0;
if (!amodule)
return 0xffffff;
char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
full_name = g_strdup_printf ("(wrapper runtime-invoke):%s (%s)", method->name, tmpsig);
g_free (tmpsig);
+ } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_INVOKE) {
+ char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
+ full_name = g_strdup_printf ("(wrapper delegate-invoke):%s (%s)", method->name, tmpsig);
+ g_free (tmpsig);
+ } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_BEGIN_INVOKE) {
+ char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
+ full_name = g_strdup_printf ("(wrapper delegate-begin-invoke):%s (%s)", method->name, tmpsig);
+ g_free (tmpsig);
+ } else if (method->wrapper_type == MONO_WRAPPER_DELEGATE_END_INVOKE) {
+ char *tmpsig = mono_signature_get_desc (mono_method_signature (method), TRUE);
+ full_name = g_strdup_printf ("(wrapper delegate-end-invoke):%s (%s)", method->name, tmpsig);
+ g_free (tmpsig);
} else {
full_name = mono_method_full_name (method, TRUE);
}
}
- if (method->wrapper_type)
- hash = g_str_hash (method->name) % table_size;
- else
- hash = 0 % table_size;
+ hash = mono_aot_method_hash (method) % table_size;
entry = &table [hash * entry_size];
- if (entry [0] != 0) {
- while (TRUE) {
- guint32 key = entry [0];
- guint32 value = entry [1];
- guint32 next = entry [entry_size - 1];
- MonoMethod *m;
- guint8 *p;
- int is_wrapper;
-
- // FIXME: Avoid fully decoding the method ref
- p = amodule->extra_method_info + key;
- is_wrapper = decode_value (p, &p);
- if (method->wrapper_type && is_wrapper) {
- if (!strcmp (full_name, (char*)p))
- return value;
- } else {
- m = decode_method_ref_2 (amodule, p, &p);
- if (m == method)
- return value;
- }
+ if (entry [0] == 0)
+ return 0xffffff;
- if (next != 0) {
- entry = &table [next * entry_size];
- } else {
- break;
+ while (TRUE) {
+ guint32 key = entry [0];
+ guint32 value = entry [1];
+ guint32 next = entry [entry_size - 1];
+ MonoMethod *m;
+ guint8 *p;
+ int is_wrapper;
+
+ p = amodule->extra_method_info + key;
+ is_wrapper = decode_value (p, &p);
+ if (is_wrapper) {
+ if (full_name && !strcmp (full_name, (char*)p))
+ return value;
+ } else if (can_method_ref_match_method (amodule, p, method)) {
+ num_checks ++;
+ mono_aot_lock ();
+ if (!amodule->method_ref_to_method)
+ amodule->method_ref_to_method = g_hash_table_new (NULL, NULL);
+ m = g_hash_table_lookup (amodule->method_ref_to_method, p);
+ mono_aot_unlock ();
+ if (!m) {
+ guint8 *orig_p = p;
+ m = decode_method_ref_2 (amodule, p, &p);
+ if (m) {
+ mono_aot_lock ();
+ g_hash_table_insert (amodule->method_ref_to_method, orig_p, m);
+ mono_aot_unlock ();
+ }
}
+ /*
+ if (m)
+ printf ("%d %s %s\n", num_checks, mono_method_full_name (method, TRUE), mono_method_full_name (m, TRUE));
+ */
+ if (m == method)
+ return value;
}
+
+ if (next != 0)
+ entry = &table [next * entry_size];
+ else
+ break;
}
return 0xffffff;
*/
/* The first entry points to the AOT trampoline */
- ((gpointer*)info->got)[info->plt_first_got_offset] = tramp;
+ ((gpointer*)info->got)[info->plt_got_offset_base] = tramp;
for (i = 1; i < info->plt_size; ++i)
/* All the default entries point to the first entry */
- ((gpointer*)info->got)[info->plt_first_got_offset + i] = info->plt;
+ ((gpointer*)info->got)[info->plt_got_offset_base + i] = info->plt;
#else
g_assert_not_reached ();
#endif
MonoJumpInfo *ji = &patches [pindex];
gpointer target;
+ if (amodule->got [got_slots [pindex]])
+ continue;
+
/*
* When this code is executed, the runtime may not yet initalized, so
* resolve the patch info by hand.
} else if (!strcmp (ji->data.name, "mono_amd64_throw_exception")) {
target = mono_amd64_throw_exception;
#endif
+#ifdef __x86_64__
+ } else if (!strcmp (ji->data.name, "mono_amd64_get_original_ip")) {
+ target = mono_amd64_get_original_ip;
+#endif
#ifdef __arm__
} else if (!strcmp (ji->data.name, "mono_arm_throw_exception")) {
target = mono_arm_throw_exception;
+ } else if (!strcmp (ji->data.name, "mono_arm_throw_exception_by_token")) {
+ target = mono_arm_throw_exception_by_token;
#endif
} else if (strstr (ji->data.name, "trampoline_func_") == ji->data.name) {
int tramp_type2 = atoi (ji->data.name + strlen ("trampoline_func_"));
target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_ENTER, mono_get_root_domain (), NULL);
} else if (!strcmp (ji->data.name, "specific_trampoline_monitor_exit")) {
target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_MONITOR_EXIT, mono_get_root_domain (), NULL);
+ } else if (!strcmp (ji->data.name, "specific_trampoline_generic_class_init")) {
+ target = mono_create_specific_trampoline (NULL, MONO_TRAMPOLINE_GENERIC_CLASS_INIT, mono_get_root_domain (), NULL);
+ } else if (!strcmp (ji->data.name, "mono_thread_get_and_clear_pending_exception")) {
+ target = mono_thread_get_and_clear_pending_exception;
} else {
fprintf (stderr, "Unknown relocation '%s'\n", ji->data.name);
g_assert_not_reached ();
* domain to be set.
*/
target = mono_resolve_patch_target (NULL, NULL, code, ji, FALSE);
+ g_assert (target);
}
amodule->got [got_slots [pindex]] = target;
tramp = generic_trampolines [tramp_type];
g_assert (tramp);
- amodule->got [amodule->first_trampoline_got_offset + (index *2)] = tramp;
- amodule->got [amodule->first_trampoline_got_offset + (index *2) + 1] = arg1;
+ amodule->got [amodule->trampoline_got_offset_base + (index *2)] = tramp;
+ amodule->got [amodule->trampoline_got_offset_base + (index *2) + 1] = arg1;
-#ifdef __x86_64__
- tramp_size = 16;
-#elif defined(__arm__)
- tramp_size = 28;
-#else
- tramp_size = -1;
- g_assert_not_reached ();
-#endif
+ tramp_size = amodule->specific_trampoline_size;
code = amodule->trampolines + (index * tramp_size);
if (code_len)
char *symbol;
gpointer code;
- amodule = method->klass->image->aot_module;
- g_assert (amodule);
+ if (method->is_inflated) {
+ guint32 index = find_extra_method (method, &amodule);
+
+ g_assert (index != 0xffffff);
+
+ symbol = g_strdup_printf ("ut_e_%d", index);
+ } else {
+ amodule = method->klass->image->aot_module;
+ g_assert (amodule);
- symbol = g_strdup_printf ("unbox_trampoline_%d", method_index);
+ symbol = g_strdup_printf ("ut_%d", method_index);
+ }
code = load_named_code (amodule, symbol);
g_free (symbol);
return code;
return NULL;
}
+guint32
+mono_aot_get_plt_info_offset (gssize *regs, guint8 *code)
+{
+ g_assert_not_reached ();
+
+ return 0;
+}
+
gpointer
-mono_aot_create_specific_trampolines (gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
+mono_aot_create_specific_trampoline (MonoImage *image, gpointer arg1, MonoTrampolineType tramp_type, MonoDomain *domain, guint32 *code_len)
{
g_assert_not_reached ();
return NULL;
}
gpointer
-mono_aot_get_named_code (char *name)
+mono_aot_get_named_code (const char *name)
{
g_assert_not_reached ();
return NULL;
return NULL;
}
+gpointer
+mono_aot_get_lazy_fetch_trampoline (guint32 slot)
+{
+ g_assert_not_reached ();
+ return NULL;
+}
+
#endif